-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Check dict item accesses where it isn't already checked #2863
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -485,6 +485,43 @@ inline handle get_function(handle value) { | |
return value; | ||
} | ||
|
||
// Reimplementation of python's dict helper functions to ensure that exceptions | ||
// aren't swallowed (see #2862) | ||
|
||
// copied from cpython _PyDict_GetItemStringWithError | ||
inline PyObject * dict_getitemstring(PyObject *v, const char *key) | ||
{ | ||
#if PY_MAJOR_VERSION >= 3 | ||
PyObject *kv, *rv; | ||
kv = PyUnicode_FromString(key); | ||
if (kv == NULL) { | ||
throw error_already_set(); | ||
} | ||
|
||
rv = PyDict_GetItemWithError(v, kv); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. At this point, can't you just call There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nope, still has potential to raise an exception. See https://bugs.python.org/issue35459 As copied from cpython @ https://github.com/python/cpython/blob/d5fc99873769f0d0d5c5d5d99059177a75a4e46e/Objects/dictobject.c#L1545 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My bad, I was thinking about calling your There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not quite the same, Py_DECREF needs to be called on the key before the error is thrown. |
||
Py_DECREF(kv); | ||
if (rv == NULL && PyErr_Occurred()) { | ||
throw error_already_set(); | ||
} | ||
return rv; | ||
#else | ||
return PyDict_GetItemString(v, key); | ||
#endif | ||
} | ||
|
||
inline PyObject * dict_getitem(PyObject *v, PyObject *key) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why isn't this a member of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because the code that is intended to call this (function dispatcher) uses the raw CPython API, presumably for performance reasons? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fair enough. |
||
{ | ||
#if PY_MAJOR_VERSION >= 3 | ||
PyObject *rv = PyDict_GetItemWithError(v, key); | ||
if (rv == NULL && PyErr_Occurred()) { | ||
throw error_already_set(); | ||
} | ||
return rv; | ||
#else | ||
return PyDict_GetItem(v, key); | ||
#endif | ||
} | ||
|
||
// Helper aliases/functions to support implicit casting of values given to python accessors/methods. | ||
// When given a pyobject, this simply returns the pyobject as-is; for other C++ type, the value goes | ||
// through pybind11::cast(obj) to convert it to an `object`. | ||
|
Uh oh!
There was an error while loading. Please reload this page.