summaryrefslogtreecommitdiff
path: root/lib/thandy/util.py
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2008-12-01 05:27:29 +0000
committerNick Mathewson <nickm@torproject.org>2008-12-01 05:27:29 +0000
commit4ebc57ff2a32f7dcaa765995e928b5def6a5cee3 (patch)
tree2947084793572d2fde66c303d3a51bd60ddb563c /lib/thandy/util.py
parent2b288c14e157343c568e0bcc4eb217901bec1c01 (diff)
Add a possible workaround for coderman's Ubuntu's simplejson snafus. 1) Only use json instead of simplejson if json actually works. Apparently there is sometimes a json module that is not the kind of json module you get with python2.6. 2) Check to see if json thinks you should escape /, and try to override that "helpful" choice.
git-svn-id: file:///home/or/svnrepo/updater/trunk@17422 55e972cd-5a19-0410-ae62-a4d7a52db4cd
Diffstat (limited to 'lib/thandy/util.py')
-rw-r--r--lib/thandy/util.py49
1 files changed, 44 insertions, 5 deletions
diff --git a/lib/thandy/util.py b/lib/thandy/util.py
index 1fbff81..e01600e 100644
--- a/lib/thandy/util.py
+++ b/lib/thandy/util.py
@@ -7,11 +7,6 @@ import tempfile
import random
try:
- import json
-except ImportError:
- import simplejson as json
-
-try:
import _winreg
except ImportError:
_winreg = None
@@ -20,6 +15,50 @@ import thandy.formats
import thandy.keys
import thandy.master_keys
+_jsonModule = None
+
+def importJSON():
+ global _jsonModule
+ if _jsonModule is not None:
+ return _jsonModule
+
+ for name in [ "json", "simplejson" ]:
+ try:
+ mod = __import__(name)
+ except ImportError:
+ continue
+ if not hasattr(mod, "dumps"):
+ # Some versions of Ubuntu have a module called 'json' that is
+ # not a recognizable simplejson module. Naughty.
+ if name == 'json':
+ logging.warn("Your operating system has a nonfunctional json "
+ "module. That's going to break any programs that "
+ "use the real json module in Python 2.6. Trying "
+ "simplejson instead.")
+ continue
+
+ # Some old versions of simplejson escape / as \/ in a misguided and
+ # inadequate attempt to fix XSS attacks. Make them not do that. This
+ # code is not guaranteed to work on all broken versions of simplejson:
+ # it replaces an entry in the internal character-replacement
+ # dictionary so that "/" is translated to itself rather than to \/.
+ try:
+ escape_dct = mod.encoder.ESCAPE_DCT
+ except NameError:
+ pass
+ else:
+ if escape_dct.has_key("/"):
+ escape_dct["/"] = "/"
+ logging.warn("Your operating system has an old broken "
+ "simplejson module. I tried to fix it for you.")
+
+ _jsonModule = mod
+ return mod
+
+ raise ImportError("Couldn't import a working json module")
+
+json = importJSON()
+
def moveFile(fromLocation, toLocation):
"""Move the file from fromLocation to toLocation, removing any file
in toLocation.