Author: Ronan Lamy <[email protected]>
Branch:
Changeset: r97801:183ffc0a3903
Date: 2019-10-17 18:26 +0100
http://bitbucket.org/pypy/pypy/changeset/183ffc0a3903/
Log: Correctly swallow exceptions happening inside PyDict_GetItem()
(#3098)
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
@@ -78,7 +78,10 @@
# *values* as full objects, which stay alive as long as the dict is
# alive and not modified. So we can return a borrowed ref.
# XXX this is wrong with IntMutableCell. Hope it works...
- return w_dict.getitem(w_key)
+ try:
+ return w_dict.getitem(w_key)
+ except OperationError:
+ return None
@cpython_api([PyObject, PyObject], PyObject, result_borrowed=True)
def _PyDict_GetItemWithError(space, w_dict, w_key):
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
@@ -394,3 +394,20 @@
d[1] = 2
assert d[1] == 42
assert module.dict_getitem(d, 1) == 2
+
+ def test_getitem_error(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_GetItem(d, key);
+ if (!result) Py_RETURN_NONE;
+ Py_XINCREF(result);
+ return result;
+ """),
+ ])
+ assert module.dict_getitem(42, 43) is None
+ assert module.dict_getitem({}, []) is None
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit