From 4ebc57ff2a32f7dcaa765995e928b5def6a5cee3 Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Mon, 1 Dec 2008 05:27:29 +0000 Subject: 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 --- lib/thandy/ClientCLI.py | 6 ++---- lib/thandy/ServerCLI.py | 7 ++----- lib/thandy/SignerCLI.py | 7 +++---- lib/thandy/formats.py | 8 +++----- lib/thandy/keys.py | 7 ++----- lib/thandy/repository.py | 5 +---- lib/thandy/util.py | 49 +++++++++++++++++++++++++++++++++++++++++++----- 7 files changed, 57 insertions(+), 32 deletions(-) (limited to 'lib') diff --git a/lib/thandy/ClientCLI.py b/lib/thandy/ClientCLI.py index fa73ab2..3f73662 100644 --- a/lib/thandy/ClientCLI.py +++ b/lib/thandy/ClientCLI.py @@ -7,10 +7,6 @@ import re 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.packagesys.PackageSystem import thandy.socksurls import thandy.encodeToXML +json = thandy.util.importJSON() + class ControlLogFormatter: def _formatStr(self, s): s = '"%s"' % re.sub(r'(["\\])', r'\\\1', s) diff --git a/lib/thandy/ServerCLI.py b/lib/thandy/ServerCLI.py index 5e2c424..98f55e1 100644 --- a/lib/thandy/ServerCLI.py +++ b/lib/thandy/ServerCLI.py @@ -5,15 +5,12 @@ import sys 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()) diff --git a/lib/thandy/SignerCLI.py b/lib/thandy/SignerCLI.py index 476e7d5..7427474 100644 --- a/lib/thandy/SignerCLI.py +++ b/lib/thandy/SignerCLI.py @@ -4,13 +4,12 @@ import os 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")) diff --git a/lib/thandy/formats.py b/lib/thandy/formats.py index d5d57a2..54b76f2 100644 --- a/lib/thandy/formats.py +++ b/lib/thandy/formats.py @@ -1,16 +1,14 @@ # 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 diff --git a/lib/thandy/keys.py b/lib/thandy/keys.py index a243fa7..aa9bbc8 100644 --- a/lib/thandy/keys.py +++ b/lib/thandy/keys.py @@ -13,14 +13,11 @@ import struct 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): diff --git a/lib/thandy/repository.py b/lib/thandy/repository.py index a9b0d19..e5d94ff 100644 --- a/lib/thandy/repository.py +++ b/lib/thandy/repository.py @@ -4,10 +4,7 @@ import thandy.formats import thandy.util import thandy.packagesys.PackageSystem -try: - import json -except ImportError: - import simplejson as json +json = thandy.util.importJSON() import logging import os 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 @@ -6,11 +6,6 @@ import sys import tempfile import random -try: - import json -except ImportError: - import simplejson as json - try: import _winreg except ImportError: @@ -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. -- cgit v1.2.3