Author: Armin Rigo <[email protected]>
Branch: 
Changeset: r73264:fcbba01341eb
Date: 2014-09-01 11:28 +0200
http://bitbucket.org/pypy/pypy/changeset/fcbba01341eb/

Log:    Remove this special case. See comments.

diff --git a/pypy/module/_pypyjson/interp_encoder.py 
b/pypy/module/_pypyjson/interp_encoder.py
--- a/pypy/module/_pypyjson/interp_encoder.py
+++ b/pypy/module/_pypyjson/interp_encoder.py
@@ -37,16 +37,14 @@
         sb = StringBuilder(len(u))
         sb.append_slice(s, 0, first)
     else:
+        # We used to check if 'u' contains only safe characters, and return
+        # 'w_string' directly.  But this requires an extra pass over all
+        # characters, and the expected use case of this function, from
+        # json.encoder, will anyway re-encode a unicode result back to
+        # a string (with the ascii encoding).  So we may as well directly
+        # turn it into a string from here, and avoid the extra pass over
+        # all characters here.
         u = space.unicode_w(w_string)
-        for i in range(len(u)):
-            c = u[i]
-            if c >= u' ' and c <= u'~' and c != u'"' and c != u'\\':
-                pass
-            else:
-                break
-        else:
-            # the input is a unicode with only non-special ascii chars
-            return w_string
         sb = StringBuilder(len(u))
         first = 0
 
diff --git a/pypy/module/_pypyjson/test/test__pypyjson.py 
b/pypy/module/_pypyjson/test/test__pypyjson.py
--- a/pypy/module/_pypyjson/test/test__pypyjson.py
+++ b/pypy/module/_pypyjson/test/test__pypyjson.py
@@ -192,14 +192,14 @@
 
     def test_raw_encode_basestring_ascii(self):
         import _pypyjson
-        def check(s, expected_type=str):
+        def check(s):
             s = _pypyjson.raw_encode_basestring_ascii(s)
-            assert type(s) is expected_type
+            assert type(s) is str
             return s
         assert check("") == ""
-        assert check(u"", expected_type=unicode) == u""
+        assert check(u"") == ""
         assert check("abc ") == "abc "
-        assert check(u"abc ", expected_type=unicode) == u"abc "
+        assert check(u"abc ") == "abc "
         raises(UnicodeDecodeError, check, "\xc0")
         assert check("\xc2\x84") == "\\u0084"
         assert check("\xf0\x92\x8d\x85") == "\\ud808\\udf45"
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to