[#8375] simpler tg.jsonify.JSONEncoder patch
works even if stdlib json has more functions in C and not patchable
also fixes test that was incorrectly changed in [a15d3ecc1] but happened to be ok on py2 still
diff --git a/Allura/allura/lib/patches.py b/Allura/allura/lib/patches.py
index 7080fa4..5eefc39 100644
--- a/Allura/allura/lib/patches.py
+++ b/Allura/allura/lib/patches.py
@@ -99,18 +99,10 @@
# This is to avoid IE9 and earlier, which don't know the json content type
# and may attempt to render JSON data as HTML if the URL ends in .html
original_tg_jsonify_JSONEncoder_encode = tg.jsonify.JSONEncoder.encode
- escape_pattern_with_lt = re.compile(
- json.encoder.ESCAPE.pattern.rstrip(']') + '<' + ']')
@h.monkeypatch(tg.jsonify.JSONEncoder)
def encode(self, o):
- # ensure_ascii=False forces encode_basestring() to be called instead of
- # encode_basestring_ascii() and encode_basestring_ascii may likely be c-compiled
- # and thus not monkeypatchable
- with h.push_config(self, ensure_ascii=False), \
- h.push_config(json.encoder, ESCAPE=escape_pattern_with_lt), \
- mock.patch.dict(json.encoder.ESCAPE_DCT, {'<': r'\u003C'}):
- return original_tg_jsonify_JSONEncoder_encode(self, o)
+ return original_tg_jsonify_JSONEncoder_encode(self, o).replace('<', r'\u003C')
old_controller_call = tg.controllers.DecoratedController._call
diff --git a/ForgeWiki/forgewiki/tests/functional/test_rest.py b/ForgeWiki/forgewiki/tests/functional/test_rest.py
index e15f90f..a41f3f7 100644
--- a/ForgeWiki/forgewiki/tests/functional/test_rest.py
+++ b/ForgeWiki/forgewiki/tests/functional/test_rest.py
@@ -119,7 +119,7 @@
def test_json_encoding_directly(self):
# used in @expose('json'), monkey-patched in our patches.py
- assert_equal(tg.jsonify.encode('<'), '"\u003C"')
+ assert_equal(tg.jsonify.encode('<'), r'"\u003C"')
# make sure these are unchanged
assert_equal(json.dumps('<'), '"<"')