Author: Carl Friedrich Bolz-Tereick <[email protected]>
Branch:
Changeset: r98499:9e44a2dcc537
Date: 2020-01-09 14:45 +0100
http://bitbucket.org/pypy/pypy/changeset/9e44a2dcc537/
Log: test and fix for #3146
JsonDictStrategy was missing a setitem_str default implementation,
so all strategies that were missing it would crash when using as a
__dict__ of an instance.
the JsonDictStrategy problem only occurs on PyPy3 because json dicts
have unicode keys, but this is still the correct change for other
dict strategies that don't want to define their own setitem_str even
on PyPy2
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
@@ -524,6 +524,10 @@
def get_empty_storage(self):
raise NotImplementedError
+ def setitem_str(self, w_dict, key, w_value):
+ w_dict.setitem(self.space.newtext(key), w_value)
+
+
@jit.look_inside_iff(lambda self, w_dict:
w_dict_unrolling_heuristic(w_dict))
def w_keys(self, w_dict):
diff --git a/pypy/objspace/std/jsondict.py b/pypy/objspace/std/jsondict.py
--- a/pypy/objspace/std/jsondict.py
+++ b/pypy/objspace/std/jsondict.py
@@ -89,6 +89,10 @@
self.switch_to_unicode_strategy(w_dict)
w_dict.setitem(w_key, w_value)
+ # for setitem_str use the default implementation
+ # because the jsonmap needs a wrapped key anyway
+
+
def setdefault(self, w_dict, w_key, w_default):
if self.is_correct_type(w_key):
w_result = self.getitem_unicode(w_dict, w_key)
diff --git a/pypy/objspace/std/test/test_jsondict.py
b/pypy/objspace/std/test/test_jsondict.py
--- a/pypy/objspace/std/test/test_jsondict.py
+++ b/pypy/objspace/std/test/test_jsondict.py
@@ -111,3 +111,16 @@
str(d)
repr(d)
+ def test_objdict_bug(self):
+ import _pypyjson
+ a = """{"foo": "bar"}"""
+ d = _pypyjson.loads(a)
+ d['foo'] = 'x'
+
+ class Obj(object):
+ pass
+
+ x = Obj()
+ x.__dict__ = d
+
+ x.foo = 'baz' # used to segfault on pypy3
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit