Repository navigation
gh-106672: C API: Report indiscriminately ignored errors #106674
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 3 commits
e134bad
00ce038
168f92a
884da66
58dbac5
07c8250
56e3a49
4e13132
b5a661b
ab5eb8f
c681ce2
82efd1c
c687ff8
310ae51
241576a
fd50ea9
587b3cc
bfb206a
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 |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| Functions :c:func:`PyDict_GetItem`, :c:func:`PyDict_GetItemString`, | ||
| :c:func:`PyMapping_HasKey`, :c:func:`PyMapping_HasKeyString`, | ||
| :c:func:`PyObject_HasAttr`, :c:func:`PyObject_HasAttrString`, and | ||
| :c:func:`PySys_GetObject`, which clear all errors occurred during calling | ||
| the function, report now them using :func:`sys.unraisablehook`. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2426,31 +2426,51 @@ PyMapping_SetItemString(PyObject *o, const char *key, PyObject *value) | |
| } | ||
|
|
||
| int | ||
| PyMapping_HasKeyString(PyObject *o, const char *key) | ||
| { | ||
| PyObject *v; | ||
|
|
||
| v = PyMapping_GetItemString(o, key); | ||
| if (v) { | ||
| Py_DECREF(v); | ||
| return 1; | ||
| PyMapping_HasKeyString(PyObject *obj, const char *key) | ||
| { | ||
| PyObject *dummy; | ||
|
Member
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. Maybe rename to 'item' or 'value'.
Member
Author
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. Done. |
||
| int rc = PyMapping_GetOptionalItemString(obj, key, &dummy); | ||
| if (rc < 0) { | ||
| _PyErr_WriteUnraisableMsg( | ||
| "in PyMapping_HasKeyString(); consider using " | ||
| "PyMapping_GetOptionalItemString() or PyMapping_GetItemString()", | ||
| NULL); | ||
| return 0; | ||
| } | ||
| PyErr_Clear(); | ||
| return 0; | ||
| // PyMapping_HasKeyString() also clears the error set before it's call | ||
|
serhiy-storchaka marked this conversation as resolved.
Resolved
|
||
| // if the key is not found. | ||
| if (rc == 0 && PyErr_Occurred()) { | ||
| _PyErr_WriteUnraisableMsg( | ||
| "in PyMapping_HasKeyString(); consider using " | ||
| "PyMapping_GetOptionalItemString() or PyMapping_GetItemString()", | ||
|
Member
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. This case is very subtle and the error message is not heplful. It's non-obvious to me that the exception was already set before the function was called. Maybe the error message should be something like: "Ignore exception set before calling in PyMapping_HasKeyString()". Instead of "Exception ignored in PyMapping_HasKeyString()".
Member
Author
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. Unfortunately |
||
| NULL); | ||
| return 0; | ||
| } | ||
| Py_XDECREF(dummy); | ||
| return rc; | ||
| } | ||
|
|
||
| int | ||
| PyMapping_HasKey(PyObject *o, PyObject *key) | ||
| { | ||
| PyObject *v; | ||
|
|
||
| v = PyObject_GetItem(o, key); | ||
| if (v) { | ||
| Py_DECREF(v); | ||
| return 1; | ||
| PyMapping_HasKey(PyObject *obj, PyObject *key) | ||
| { | ||
| PyObject *dummy; | ||
| int rc = PyMapping_GetOptionalItem(obj, key, &dummy); | ||
| if (rc < 0) { | ||
| _PyErr_WriteUnraisableMsg( | ||
| "in PyMapping_HasKey(); consider using " | ||
| "PyMapping_GetOptionalItem() or PyObject_GetItem()", NULL); | ||
| return 0; | ||
| } | ||
| PyErr_Clear(); | ||
| return 0; | ||
| // PyMapping_HasKey() also clears the error set before it's call | ||
| // if the key is not found. | ||
| if (rc == 0 && PyErr_Occurred()) { | ||
| _PyErr_WriteUnraisableMsg( | ||
| "in PyMapping_HasKey(); consider using " | ||
| "PyMapping_GetOptionalItem() or PyObject_GetItem()", NULL); | ||
| return 0; | ||
| } | ||
| Py_XDECREF(dummy); | ||
| return rc; | ||
| } | ||
|
|
||
| /* This function is quite similar to PySequence_Fast(), but specialized to be | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1662,7 +1662,7 @@ _PyDict_FromItems(PyObject *const *keys, Py_ssize_t keys_offset, | |
| * even if the key is present. | ||
| */ | ||
| PyObject * | ||
| PyDict_GetItem(PyObject *op, PyObject *key) | ||
| dict_getitem(PyObject *op, PyObject *key, const char *warnmsg) | ||
| { | ||
| if (!PyDict_Check(op)) { | ||
| return NULL; | ||
|
|
@@ -1673,7 +1673,7 @@ PyDict_GetItem(PyObject *op, PyObject *key) | |
| if (!PyUnicode_CheckExact(key) || (hash = unicode_get_hash(key)) == -1) { | ||
| hash = PyObject_Hash(key); | ||
| if (hash == -1) { | ||
| PyErr_Clear(); | ||
| _PyErr_WriteUnraisableMsg(warnmsg, NULL); | ||
| return NULL; | ||
| } | ||
| } | ||
|
|
@@ -1694,13 +1694,25 @@ PyDict_GetItem(PyObject *op, PyObject *key) | |
| ix = _Py_dict_lookup(mp, key, hash, &value); | ||
|
|
||
| /* Ignore any exception raised by the lookup */ | ||
| PyObject *exc2 = _PyErr_Occurred(tstate); | ||
| if (exc2 && !PyErr_GivenExceptionMatches(exc2, PyExc_KeyError)) { | ||
| _PyErr_WriteUnraisableMsg(warnmsg, NULL); | ||
| } | ||
| _PyErr_SetRaisedException(tstate, exc); | ||
|
|
||
|
|
||
| assert(ix >= 0 || value == NULL); | ||
| return value; | ||
| } | ||
|
|
||
| PyObject * | ||
| PyDict_GetItem(PyObject *op, PyObject *key) | ||
| { | ||
| return dict_getitem(op, key, | ||
| "in PyDict_GetItem(); consider using " | ||
| "PyDict_GetItemWithError()"); | ||
|
Member
Author
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. There will be new |
||
| } | ||
|
|
||
| Py_ssize_t | ||
| _PyDict_LookupIndex(PyDictObject *mp, PyObject *key) | ||
| { | ||
|
|
@@ -3889,10 +3901,12 @@ PyDict_GetItemString(PyObject *v, const char *key) | |
| PyObject *kv, *rv; | ||
| kv = PyUnicode_FromString(key); | ||
| if (kv == NULL) { | ||
| PyErr_Clear(); | ||
| _PyErr_WriteUnraisableMsg( | ||
| "in PyDict_GetItemString()", NULL); | ||
|
Member
Author
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. No replacement currently. There will be new |
||
| return NULL; | ||
| } | ||
| rv = PyDict_GetItem(v, kv); | ||
| rv = dict_getitem(v, kv, "in PyDict_GetItemString()"); | ||
| Py_DECREF(kv); | ||
| return rv; | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -98,6 +98,9 @@ PySys_GetObject(const char *name) | |
| PyObject *value = _PySys_GetObject(tstate->interp, name); | ||
| /* XXX Suppress a new exception if it was raised and restore | ||
| * the old one. */ | ||
| if (_PyErr_Occurred(tstate)) { | ||
| _PyErr_WriteUnraisableMsg("in PySys_GetObject()", NULL); | ||
|
Member
Author
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. No replacement currently.
Member
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. Yeah, I'm waiting until PyDict_GetItemRef() is accepted to consider proposing a variant for PySys_GetObject(). I'm tracking functions returning borrowed references and replacement at: https://pythoncapi.readthedocs.io/bad_api.html#functions |
||
| } | ||
| _PyErr_SetRaisedException(tstate, exc); | ||
| return value; | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.