Author: Carl Friedrich Bolz-Tereick <[email protected]>
Branch:
Changeset: r92445:64e7df28f623
Date: 2017-09-22 21:19 +0200
http://bitbucket.org/pypy/pypy/changeset/64e7df28f623/
Log: create a dict with the unicode strategy directly
(also fix targetjson)
diff --git a/pypy/module/_pypyjson/interp_decoder.py
b/pypy/module/_pypyjson/interp_decoder.py
--- a/pypy/module/_pypyjson/interp_decoder.py
+++ b/pypy/module/_pypyjson/interp_decoder.py
@@ -264,19 +264,16 @@
def decode_object(self, i):
start = i
- w_dict = self.space.newdict()
- #
+
i = self.skip_whitespace(i)
if self.ll_chars[i] == '}':
self.pos = i+1
- return w_dict
- #
+ return self.space.newdict()
+
+ d = {}
while True:
# parse a key: value
- self.last_type = TYPE_UNKNOWN
- w_name = self.decode_any(i)
- if self.last_type != TYPE_STRING:
- self._raise("Key name must be string for object starting at
char %d", start)
+ name = self.decode_key(i)
i = self.skip_whitespace(self.pos)
ch = self.ll_chars[i]
if ch != ':':
@@ -285,13 +282,13 @@
i = self.skip_whitespace(i)
#
w_value = self.decode_any(i)
- self.space.setitem(w_dict, w_name, w_value)
+ d[name] = w_value
i = self.skip_whitespace(self.pos)
ch = self.ll_chars[i]
i += 1
if ch == '}':
self.pos = i
- return w_dict
+ return self._create_dict(d)
elif ch == ',':
pass
elif ch == '\0':
@@ -300,6 +297,9 @@
self._raise("Unexpected '%s' when decoding object (char %d)",
ch, i-1)
+ def _create_dict(self, d):
+ from pypy.objspace.std.dictmultiobject import from_unicode_key_dict
+ return from_unicode_key_dict(self.space, d)
def decode_string(self, i):
start = i
diff --git a/pypy/module/_pypyjson/targetjson.py
b/pypy/module/_pypyjson/targetjson.py
--- a/pypy/module/_pypyjson/targetjson.py
+++ b/pypy/module/_pypyjson/targetjson.py
@@ -5,9 +5,15 @@
import time
from pypy.interpreter.error import OperationError
-from pypy.module._pypyjson.interp_decoder import loads
+from pypy.module._pypyjson.interp_decoder import loads, JSONDecoder
from rpython.rlib.objectmodel import specialize, dont_inline
+def _create_dict(self, d):
+ w_res = W_Dict()
+ w_res.dictval = d
+ return w_res
+
+JSONDecoder._create_dict = _create_dict
## MSG = open('msg.json').read()
@@ -65,10 +71,14 @@
def isinstance_w(self, w_x, w_type):
return isinstance(w_x, w_type)
- def str_w(self, w_x):
+ def bytes_w(self, w_x):
assert isinstance(w_x, W_String)
return w_x.strval
+ def unicode_w(self, w_x):
+ assert isinstance(w_x, W_Unicode)
+ return w_x.unival
+
@dont_inline
def call_method(self, obj, name, arg):
assert name == 'append'
@@ -83,13 +93,17 @@
assert isinstance(key, W_Unicode)
d.dictval[key.unival] = value
- def wrapunicode(self, x):
+ def newunicode(self, x):
return W_Unicode(x)
- def wrapint(self, x):
+ def newtext(self, x):
+ return W_String(x)
+ newbytes = newtext
+
+ def newint(self, x):
return W_Int(x)
- def wrapfloat(self, x):
+ def newfloat(self, x):
return W_Float(x)
@specialize.argtype(1)
diff --git a/pypy/objspace/std/dictmultiobject.py
b/pypy/objspace/std/dictmultiobject.py
--- a/pypy/objspace/std/dictmultiobject.py
+++ b/pypy/objspace/std/dictmultiobject.py
@@ -1257,6 +1257,12 @@
create_iterator_classes(UnicodeDictStrategy)
+def from_unicode_key_dict(space, d):
+ strategy = space.fromcache(UnicodeDictStrategy)
+ storage = strategy.erase(d)
+ return W_DictObject(space, strategy, storage)
+
+
class IntDictStrategy(AbstractTypedStrategy, DictStrategy):
erase, unerase = rerased.new_erasing_pair("int")
erase = staticmethod(erase)
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit