[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[or-cvs] r17422: {updater} Add a possible workaround for coderman's Ubuntu's simplejson (updater/trunk/lib/thandy)
Author: nickm
Date: 2008-12-01 00:27:29 -0500 (Mon, 01 Dec 2008)
New Revision: 17422
Modified:
updater/trunk/lib/thandy/ClientCLI.py
updater/trunk/lib/thandy/ServerCLI.py
updater/trunk/lib/thandy/SignerCLI.py
updater/trunk/lib/thandy/formats.py
updater/trunk/lib/thandy/keys.py
updater/trunk/lib/thandy/repository.py
updater/trunk/lib/thandy/util.py
Log:
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.
Modified: updater/trunk/lib/thandy/ClientCLI.py
===================================================================
--- updater/trunk/lib/thandy/ClientCLI.py 2008-11-30 16:00:52 UTC (rev 17421)
+++ updater/trunk/lib/thandy/ClientCLI.py 2008-12-01 05:27:29 UTC (rev 17422)
@@ -7,10 +7,6 @@
import sys
import time
import traceback
-try:
- import json
-except ImportError:
- import simplejson as json
import thandy.formats
import thandy.util
@@ -22,6 +18,8 @@
import thandy.socksurls
import thandy.encodeToXML
+json = thandy.util.importJSON()
+
class ControlLogFormatter:
def _formatStr(self, s):
s = '"%s"' % re.sub(r'(["\\])', r'\\\1', s)
Modified: updater/trunk/lib/thandy/ServerCLI.py
===================================================================
--- updater/trunk/lib/thandy/ServerCLI.py 2008-11-30 16:00:52 UTC (rev 17421)
+++ updater/trunk/lib/thandy/ServerCLI.py 2008-12-01 05:27:29 UTC (rev 17422)
@@ -5,15 +5,12 @@
import getopt
import time
-try:
- import json
-except:
- import simplejson as json
-
import thandy.formats
import thandy.util
import thandy.keys
+json = thandy.util.importJSON()
+
def tstamp():
return time.strftime("%Y%m%d_%H%M%S", time.localtime())
Modified: updater/trunk/lib/thandy/SignerCLI.py
===================================================================
--- updater/trunk/lib/thandy/SignerCLI.py 2008-11-30 16:00:52 UTC (rev 17421)
+++ updater/trunk/lib/thandy/SignerCLI.py 2008-12-01 05:27:29 UTC (rev 17422)
@@ -4,14 +4,13 @@
import getopt
import sys
import logging
-try:
- import json
-except ImportError:
- import simplejson as json
import thandy.keys
import thandy.formats
+import thandy.util
+json = thandy.util.importJSON()
+
def getKeyStore():
return thandy.keys.KeyStore(thandy.util.userFilename("secret_keys"))
Modified: updater/trunk/lib/thandy/formats.py
===================================================================
--- updater/trunk/lib/thandy/formats.py 2008-11-30 16:00:52 UTC (rev 17421)
+++ updater/trunk/lib/thandy/formats.py 2008-12-01 05:27:29 UTC (rev 17422)
@@ -1,17 +1,15 @@
# Copyright 2008 The Tor Project, Inc. See LICENSE for licensing information.
-try:
- import json
-except ImportError:
- import simplejson as json
-
import time
import re
import binascii
import calendar
import thandy.checkJson
+import thandy.util
+json = thandy.util.importJSON()
+
import Crypto.Hash.SHA256
class KeyDB:
Modified: updater/trunk/lib/thandy/keys.py
===================================================================
--- updater/trunk/lib/thandy/keys.py 2008-11-30 16:00:52 UTC (rev 17421)
+++ updater/trunk/lib/thandy/keys.py 2008-12-01 05:27:29 UTC (rev 17422)
@@ -13,14 +13,11 @@
import sys
import getpass
-try:
- import json
-except ImportError:
- import simplejson as json
-
import thandy.formats
import thandy.util
+json = thandy.util.importJSON()
+
class PublicKey:
"""Abstract base class for public keys."""
def __init__(self):
Modified: updater/trunk/lib/thandy/repository.py
===================================================================
--- updater/trunk/lib/thandy/repository.py 2008-11-30 16:00:52 UTC (rev 17421)
+++ updater/trunk/lib/thandy/repository.py 2008-12-01 05:27:29 UTC (rev 17422)
@@ -4,10 +4,7 @@
import thandy.util
import thandy.packagesys.PackageSystem
-try:
- import json
-except ImportError:
- import simplejson as json
+json = thandy.util.importJSON()
import logging
import os
Modified: updater/trunk/lib/thandy/util.py
===================================================================
--- updater/trunk/lib/thandy/util.py 2008-11-30 16:00:52 UTC (rev 17421)
+++ updater/trunk/lib/thandy/util.py 2008-12-01 05:27:29 UTC (rev 17422)
@@ -7,11 +7,6 @@
import random
try:
- import json
-except ImportError:
- import simplejson as json
-
-try:
import _winreg
except ImportError:
_winreg = None
@@ -20,6 +15,50 @@
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.