summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoel Rivera <rivera@joel.mx>2013-04-27 13:19:21 -0500
committerJoel Rivera <rivera@joel.mx>2013-04-27 13:19:21 -0500
commit81bd425e8d2b25ecec5bf9e6874fa9e6823d1234 (patch)
treeb8e82ff399fb9d67a17dc81df63fb78dc2edd86d
parent18f9db3cc08202b1ffdcc4f15cecc500d7c8924c (diff)
downloadcherrypy-81bd425e8d2b25ecec5bf9e6874fa9e6823d1234.tar.gz
Wrap the json encoder when is python3 regardless if is using simplejson
or the standard library, we need bytes not str which is the output of the json encoders in python3. Closing issue #1247
-rw-r--r--cherrypy/_cpcompat.py27
1 files changed, 14 insertions, 13 deletions
diff --git a/cherrypy/_cpcompat.py b/cherrypy/_cpcompat.py
index 42b7cda3..68c82b69 100644
--- a/cherrypy/_cpcompat.py
+++ b/cherrypy/_cpcompat.py
@@ -273,28 +273,29 @@ try:
# Prefer simplejson, which is usually more advanced than the builtin module.
import simplejson as json
json_decode = json.JSONDecoder().decode
- json_encode = json.JSONEncoder().iterencode
+ _json_encode = json.JSONEncoder().iterencode
except ImportError:
- if py3k:
- # Python 3.0: json is part of the standard library,
- # but outputs unicode. We need bytes.
+ if sys.version_info >= (2, 6):
+ # Python >=2.6 : json is part of the standard library
import json
json_decode = json.JSONDecoder().decode
_json_encode = json.JSONEncoder().iterencode
- def json_encode(value):
- for chunk in _json_encode(value):
- yield chunk.encode('utf8')
- elif sys.version_info >= (2, 6):
- # Python 2.6: json is part of the standard library
- import json
- json_decode = json.JSONDecoder().decode
- json_encode = json.JSONEncoder().iterencode
else:
json = None
def json_decode(s):
raise ValueError('No JSON library is available')
- def json_encode(s):
+ def _json_encode(s):
raise ValueError('No JSON library is available')
+finally:
+ if json and py3k:
+ # The two Python 3 implementations (simplejson/json)
+ # outputs str. We need bytes.
+ def json_encode(value):
+ for chunk in _json_encode(value):
+ yield chunk.encode('utf8')
+ else:
+ json_encode = _json_encode
+
try:
import cPickle as pickle