You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@allura.apache.org by br...@apache.org on 2020/08/27 20:35:38 UTC

[allura] 13/16: [#8375] simpler tg.jsonify.JSONEncoder patch

This is an automated email from the ASF dual-hosted git repository.

brondsem pushed a commit to branch db/8375
in repository https://gitbox.apache.org/repos/asf/allura.git

commit 18788c6c5cd12d0bfb36c634e8cac42599a54d5c
Author: Dave Brondsema <da...@brondsema.net>
AuthorDate: Wed Aug 26 14:04:19 2020 -0400

    [#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
---
 Allura/allura/lib/patches.py                      | 10 +---------
 ForgeWiki/forgewiki/tests/functional/test_rest.py |  2 +-
 2 files changed, 2 insertions(+), 10 deletions(-)

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 @@ def apply():
     # 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 @@ class TestWikiApi(TestRestApiBase):
 
     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('<'), '"<"')