[pypy-commit] pypy py3.5: A bit closer to pypy2.7
arigo
pypy.commits at gmail.com
Mon Jan 15 08:11:05 EST 2018
Author: Armin Rigo <arigo at tunes.org>
Branch: py3.5
Changeset: r93670:4e1c19053bd9
Date: 2018-01-15 14:10 +0100
http://bitbucket.org/pypy/pypy/changeset/4e1c19053bd9/
Log: A bit closer to pypy2.7
diff --git a/lib-python/3/json/encoder.py b/lib-python/3/json/encoder.py
--- a/lib-python/3/json/encoder.py
+++ b/lib-python/3/json/encoder.py
@@ -4,18 +4,6 @@
from __pypy__.builders import StringBuilder
-try:
- from _json import encode_basestring_ascii as c_encode_basestring_ascii
-except ImportError:
- c_encode_basestring_ascii = None
-try:
- from _json import encode_basestring as c_encode_basestring
-except ImportError:
- c_encode_basestring = None
-try:
- from _json import make_encoder as c_make_encoder
-except ImportError:
- c_make_encoder = None
ESCAPE = re.compile(r'[\x00-\x1f\\"\b\f\n\r\t]')
ESCAPE_ASCII = re.compile(r'([\\"]|[^\ -~])')
@@ -35,19 +23,17 @@
INFINITY = float('inf')
-def py_encode_basestring(s):
+def raw_encode_basestring(s):
"""Return a JSON representation of a Python string
"""
def replace(match):
return ESCAPE_DCT[match.group(0)]
- return '"' + ESCAPE.sub(replace, s) + '"'
+ return ESCAPE.sub(replace, s)
+encode_basestring = lambda s: '"' + raw_encode_basestring(s) + '"'
-encode_basestring = (c_encode_basestring or py_encode_basestring)
-
-
-def py_encode_basestring_ascii(s):
+def raw_encode_basestring_ascii(s):
"""Return an ASCII-only JSON representation of a Python string
"""
@@ -66,12 +52,10 @@
s1 = 0xd800 | ((n >> 10) & 0x3ff)
s2 = 0xdc00 | (n & 0x3ff)
return '\\u{0:04x}\\u{1:04x}'.format(s1, s2)
- return '"' + ESCAPE_ASCII.sub(replace, s) + '"'
+ return ESCAPE_ASCII.sub(replace, s)
+encode_basestring_ascii = lambda s: '"' + raw_encode_basestring_ascii(s) + '"'
-encode_basestring_ascii = (
- c_encode_basestring_ascii or py_encode_basestring_ascii)
-
class JSONEncoder(object):
"""Extensible JSON <http://json.org> encoder for Python data structures.
@@ -148,6 +132,10 @@
self.skipkeys = skipkeys
self.ensure_ascii = ensure_ascii
+ if ensure_ascii:
+ self.__encoder = raw_encode_basestring_ascii
+ else:
+ self.__encoder = raw_encode_basestring
self.check_circular = check_circular
self.allow_nan = allow_nan
self.sort_keys = sort_keys
@@ -159,7 +147,7 @@
if default is not None:
self.default = default
- if indent is not None and not isinstance(ident, str):
+ if indent is not None and not isinstance(indent, str):
self.indent_str = ' ' * indent
else:
self.indent_str = indent
@@ -465,8 +453,8 @@
yield 'true'
elif value is False:
yield 'false'
- elif isinstance(value, (int, long)):
- yield str(value)
+ elif isinstance(value, int):
+ yield int.__str__(value)
elif isinstance(value, float):
yield self.__floatstr(value)
else:
@@ -495,8 +483,8 @@
yield 'true'
elif o is False:
yield 'false'
- elif isinstance(o, (int, long)):
- yield str(o)
+ elif isinstance(o, int):
+ yield int.__str__(o)
elif isinstance(o, float):
yield self.__floatstr(o)
elif isinstance(o, (list, tuple)):
@@ -513,10 +501,5 @@
# overwrite some helpers here with more efficient versions
try:
from _pypyjson import raw_encode_basestring_ascii
- def encode_basestring_ascii(s):
- encoded = raw_encode_basestring_ascii(s)
- if encoded is None:
- return '"' + s + '"'
- return encoded # on pypy3, includes the quotes already
except ImportError:
pass
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
@@ -1,7 +1,6 @@
from rpython.rlib.rstring import StringBuilder
from rpython.rlib.runicode import str_decode_utf_8
from pypy.interpreter import unicodehelper
-from pypy.interpreter.gateway import unwrap_spec
HEX = '0123456789abcdef'
@@ -17,19 +16,17 @@
for _i in range(32)]
- at unwrap_spec(u=unicode)
-def raw_encode_basestring_ascii(space, u):
+def raw_encode_basestring_ascii(space, w_unicode):
+ u = space.unicode_w(w_unicode)
for i in range(len(u)):
c = ord(u[i])
if c < 32 or c > 126 or c == ord('\\') or c == ord('"'):
break
else:
# The unicode string 'u' contains only safe characters.
- # Return None to mean this.
- return space.w_None
+ return w_unicode
sb = StringBuilder(len(u) + 20)
- sb.append('"')
for i in range(len(u)):
c = ord(u[i])
@@ -61,6 +58,5 @@
sb.append(HEX[(s2 >> 4) & 0x0f])
sb.append(HEX[s2 & 0x0f])
- sb.append('"')
res = sb.build()
return space.newtext(res)
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
@@ -214,13 +214,10 @@
def test_raw_encode_basestring_ascii(self):
import _pypyjson
- def check(inp_s):
- s = _pypyjson.raw_encode_basestring_ascii(inp_s)
- if s is None:
- return inp_s
+ def check(s):
+ s = _pypyjson.raw_encode_basestring_ascii(s)
assert type(s) is str
- assert len(s) >= 2 and s.startswith('"') and s.endswith('"')
- return s[1:-1]
+ return s
assert check("") == ""
assert check(u"") == ""
assert check("abc ") == "abc "
More information about the pypy-commit
mailing list