Commit 72d530eb authored by Ronan Lamy's avatar Ronan Lamy
Browse files

Add _PyDict_GetItemWithError (part of the public API in py3)

(grafted from d663ce56919c)

branch : release-pypy2.7-v7.x
parent 73aff81440f6
......@@ -80,6 +80,13 @@ def PyDict_GetItem(space, w_dict, w_key):
# 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):
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):
......@@ -173,6 +173,26 @@ class AppTestDictObject(AppTestCpythonExtensionBase):
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())
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",
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment