Author: Carl Friedrich Bolz <cfb...@gmx.de> Branch: Changeset: r88582:436f95cd8184 Date: 2016-11-23 17:25 +0100 http://bitbucket.org/pypy/pypy/changeset/436f95cd8184/
Log: test and fix: PyDict_Update is not actually the same as dict.update in CPython (that would be too simple) diff --git a/pypy/module/cpyext/dictobject.py b/pypy/module/cpyext/dictobject.py --- a/pypy/module/cpyext/dictobject.py +++ b/pypy/module/cpyext/dictobject.py @@ -137,8 +137,7 @@ """This is the same as PyDict_Merge(a, b, 1) in C, or a.update(b) in Python. Return 0 on success or -1 if an exception was raised. """ - space.call_method(space.w_dict, "update", w_obj, w_other) - return 0 + return PyDict_Merge(space, w_obj, w_other, 1) @cpython_api([PyObject], PyObject) def PyDict_Keys(space, w_obj): diff --git a/pypy/module/cpyext/test/test_dictobject.py b/pypy/module/cpyext/test/test_dictobject.py --- a/pypy/module/cpyext/test/test_dictobject.py +++ b/pypy/module/cpyext/test/test_dictobject.py @@ -103,6 +103,17 @@ api.PyDict_Update(w_d, w_d2) assert space.unwrap(w_d) == dict(a='b', c='d', e='f') + def test_update_doesnt_accept_list_of_tuples(self, space, api): + w_d = space.newdict() + space.setitem(w_d, space.wrap("a"), space.wrap("b")) + + w_d2 = space.wrap([("c", "d"), ("e", "f")]) + + api.PyDict_Update(w_d, w_d2) + assert api.PyErr_Occurred() is space.w_AttributeError + api.PyErr_Clear() + assert space.unwrap(w_d) == dict(a='b') # unchanged + def test_iter(self, space, api): w_dict = space.sys.getdict(space) py_dict = make_ref(space, w_dict) @@ -199,3 +210,18 @@ """), ]) assert module.dict_proxy({'a': 1, 'b': 2}) == 2 + + def test_update(self): + module = self.import_extension('foo', [ + ("update", "METH_VARARGS", + ''' + if (PyDict_Update(PyTuple_GetItem(args, 0), PyTuple_GetItem(args, 1))) + return NULL; + Py_RETURN_NONE; + ''')]) + d = {"a": 1} + module.update(d, {"c": 2}) + assert d == dict(a=1, c=2) + d = {"a": 1} + raises(AttributeError, module.update, d, [("c", 2)]) + _______________________________________________ pypy-commit mailing list pypy-commit@python.org https://mail.python.org/mailman/listinfo/pypy-commit