Author: Ronan Lamy <ronan.l...@gmail.com> Branch: release-pypy2.7-v7.x Changeset: r97752:4a68d8d3d2fc Date: 2019-10-04 18:02 +0100 http://bitbucket.org/pypy/pypy/changeset/4a68d8d3d2fc/
Log: Add _PyDict_GetItemWithError (part of the public API in py3) (grafted from d663ce56919c62a2bf5e242ee79b8bc3c640662a) 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 @@ -80,6 +80,13 @@ # XXX this is wrong with IntMutableCell. Hope it works... return w_dict.getitem(w_key) +@cpython_api([PyObject, PyObject], PyObject, result_borrowed=True) +def _PyDict_GetItemWithError(space, w_dict, w_key): + # Like PyDict_GetItem(), but doesn't swallow the error + if not isinstance(w_dict, W_DictMultiObject): + PyErr_BadInternalCall(space) + return w_dict.getitem(w_key) + @cpython_api([PyObject, PyObject, PyObject], rffi.INT_real, error=-1) def PyDict_SetItem(space, w_dict, w_key, w_obj): if not isinstance(w_dict, W_DictMultiObject): 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 @@ -173,6 +173,26 @@ ]) assert module.dict_proxy({'a': 1, 'b': 2}) == 2 + def test_getitemwitherror(self): + module = self.import_extension('foo', [ + ("dict_getitem", "METH_VARARGS", + """ + PyObject *d, *key, *result; + if (!PyArg_ParseTuple(args, "OO", &d, &key)) { + return NULL; + } + result = _PyDict_GetItemWithError(d, key); + if (result == NULL && !PyErr_Occurred()) + Py_RETURN_NONE; + Py_XINCREF(result); + return result; + """)]) + d = {'foo': 'bar'} + assert module.dict_getitem(d, 'foo') == 'bar' + assert module.dict_getitem(d, 'missing') is None + with raises(TypeError): + module.dict_getitem(d, []) + def test_update(self): module = self.import_extension('foo', [ ("update", "METH_VARARGS", _______________________________________________ pypy-commit mailing list pypy-commit@python.org https://mail.python.org/mailman/listinfo/pypy-commit