Skip to content
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

gh-106672: C API: Report indiscriminately ignored errors #106674

Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Doc/whatsnew/3.13.rst
Original file line number Diff line number Diff line change
Expand Up @@ -970,6 +970,15 @@ Changes in the Python API
The result is now the same if ``wantobjects`` is set to ``0``.
(Contributed by Serhiy Storchaka in :gh:`97928`.)

* 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`.
You can consider to replace these functions with other functions as
recomended in the documentation.
(Contributed by Serhiy Storchaka in :gh:`106672`.)


Build Changes
=============
Expand Down
104 changes: 89 additions & 15 deletions Lib/test/test_capi/test_abstract.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import unittest
from collections import OrderedDict
from test import support
from test.support import import_helper

_testcapi = import_helper.import_module('_testcapi')
Expand Down Expand Up @@ -109,8 +110,18 @@ def test_object_hasattr(self):
self.assertFalse(xhasattr(obj, 'b'))
self.assertTrue(xhasattr(obj, '\U0001f40d'))

self.assertFalse(xhasattr(obj, 'evil'))
self.assertFalse(xhasattr(obj, 1))
with support.catch_unraisable_exception() as cm:
self.assertFalse(xhasattr(obj, 'evil'))
self.assertEqual(cm.unraisable.exc_type, RuntimeError)
self.assertEqual(str(cm.unraisable.exc_value),
'do not get evil')

with support.catch_unraisable_exception() as cm:
self.assertFalse(xhasattr(obj, 1))
self.assertEqual(cm.unraisable.exc_type, TypeError)
self.assertEqual(str(cm.unraisable.exc_value),
"attribute name must be string, not 'int'")

# CRASHES xhasattr(obj, NULL)
# CRASHES xhasattr(NULL, 'a')

Expand All @@ -123,8 +134,18 @@ def test_object_hasattrstring(self):
self.assertFalse(hasattrstring(obj, b'b'))
self.assertTrue(hasattrstring(obj, '\U0001f40d'.encode()))

self.assertFalse(hasattrstring(obj, b'evil'))
self.assertFalse(hasattrstring(obj, b'\xff'))
with support.catch_unraisable_exception() as cm:
self.assertFalse(hasattrstring(obj, b'evil'))
self.assertEqual(cm.unraisable.exc_type, RuntimeError)
self.assertEqual(str(cm.unraisable.exc_value),
'do not get evil')

with support.catch_unraisable_exception() as cm:
self.assertFalse(hasattrstring(obj, b'\xff'))
self.assertEqual(cm.unraisable.exc_type, UnicodeDecodeError)
self.assertRegex(str(cm.unraisable.exc_value),
"'utf-8' codec can't decode")

# CRASHES hasattrstring(obj, NULL)
# CRASHES hasattrstring(NULL, b'a')

Expand Down Expand Up @@ -342,12 +363,41 @@ def test_mapping_haskey(self):

self.assertTrue(haskey(['a', 'b', 'c'], 1))

self.assertFalse(haskey(42, 'a'))
self.assertFalse(haskey({}, [])) # unhashable
self.assertFalse(haskey({}, NULL))
self.assertFalse(haskey([], 1))
self.assertFalse(haskey([], 'a'))
self.assertFalse(haskey(NULL, 'a'))
with support.catch_unraisable_exception() as cm:
self.assertFalse(haskey(42, 'a'))
self.assertEqual(cm.unraisable.exc_type, TypeError)
self.assertEqual(str(cm.unraisable.exc_value),
"'int' object is not subscriptable")

with support.catch_unraisable_exception() as cm:
self.assertFalse(haskey({}, []))
self.assertEqual(cm.unraisable.exc_type, TypeError)
self.assertEqual(str(cm.unraisable.exc_value),
"unhashable type: 'list'")

with support.catch_unraisable_exception() as cm:
self.assertFalse(haskey([], 1))
self.assertEqual(cm.unraisable.exc_type, IndexError)
self.assertEqual(str(cm.unraisable.exc_value),
'list index out of range')

with support.catch_unraisable_exception() as cm:
self.assertFalse(haskey([], 'a'))
self.assertEqual(cm.unraisable.exc_type, TypeError)
self.assertEqual(str(cm.unraisable.exc_value),
'list indices must be integers or slices, not str')

with support.catch_unraisable_exception() as cm:
self.assertFalse(haskey({}, NULL))
self.assertEqual(cm.unraisable.exc_type, SystemError)
self.assertEqual(str(cm.unraisable.exc_value),
'null argument to internal routine')

with support.catch_unraisable_exception() as cm:
self.assertFalse(haskey(NULL, 'a'))
self.assertEqual(cm.unraisable.exc_type, SystemError)
self.assertEqual(str(cm.unraisable.exc_value),
'null argument to internal routine')

def test_mapping_haskeystring(self):
haskeystring = _testcapi.mapping_haskeystring
Expand All @@ -360,11 +410,35 @@ def test_mapping_haskeystring(self):
self.assertTrue(haskeystring(dct2, b'a'))
self.assertFalse(haskeystring(dct2, b'b'))

self.assertFalse(haskeystring(42, b'a'))
self.assertFalse(haskeystring({}, b'\xff'))
self.assertFalse(haskeystring({}, NULL))
self.assertFalse(haskeystring([], b'a'))
self.assertFalse(haskeystring(NULL, b'a'))
with support.catch_unraisable_exception() as cm:
self.assertFalse(haskeystring(42, b'a'))
self.assertEqual(cm.unraisable.exc_type, TypeError)
self.assertEqual(str(cm.unraisable.exc_value),
"'int' object is not subscriptable")

with support.catch_unraisable_exception() as cm:
self.assertFalse(haskeystring({}, b'\xff'))
self.assertEqual(cm.unraisable.exc_type, UnicodeDecodeError)
self.assertRegex(str(cm.unraisable.exc_value),
"'utf-8' codec can't decode")

with support.catch_unraisable_exception() as cm:
self.assertFalse(haskeystring({}, NULL))
self.assertEqual(cm.unraisable.exc_type, SystemError)
self.assertEqual(str(cm.unraisable.exc_value),
"null argument to internal routine")

with support.catch_unraisable_exception() as cm:
self.assertFalse(haskeystring([], b'a'))
self.assertEqual(cm.unraisable.exc_type, TypeError)
self.assertEqual(str(cm.unraisable.exc_value),
'list indices must be integers or slices, not str')

with support.catch_unraisable_exception() as cm:
self.assertFalse(haskeystring(NULL, b'a'))
self.assertEqual(cm.unraisable.exc_type, SystemError)
self.assertEqual(str(cm.unraisable.exc_value),
"null argument to internal routine")

def test_mapping_haskeywitherror(self):
haskey = _testcapi.mapping_haskeywitherror
Expand Down
19 changes: 15 additions & 4 deletions Lib/test/test_capi/test_dict.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import unittest
from collections import OrderedDict, UserDict
from types import MappingProxyType
from test import support
import _testcapi


Expand Down Expand Up @@ -30,7 +31,7 @@ def test_dict_check(self):
self.assertFalse(check(UserDict({1: 2})))
self.assertFalse(check([1, 2]))
self.assertFalse(check(object()))
#self.assertFalse(check(NULL))
# CRASHES check(NULL)

def test_dict_checkexact(self):
check = _testcapi.dict_checkexact
Expand All @@ -39,7 +40,7 @@ def test_dict_checkexact(self):
self.assertFalse(check(UserDict({1: 2})))
self.assertFalse(check([1, 2]))
self.assertFalse(check(object()))
#self.assertFalse(check(NULL))
# CRASHES check(NULL)

def test_dict_new(self):
dict_new = _testcapi.dict_new
Expand Down Expand Up @@ -118,7 +119,12 @@ def test_dict_getitem(self):
self.assertEqual(getitem(dct2, 'a'), 1)
self.assertIs(getitem(dct2, 'b'), KeyError)

self.assertIs(getitem({}, []), KeyError) # unhashable
with support.catch_unraisable_exception() as cm:
self.assertIs(getitem({}, []), KeyError) # unhashable
self.assertEqual(cm.unraisable.exc_type, TypeError)
self.assertEqual(str(cm.unraisable.exc_value),
"unhashable type: 'list'")

self.assertIs(getitem(42, 'a'), KeyError)
self.assertIs(getitem([1], 0), KeyError)
# CRASHES getitem({}, NULL)
Expand All @@ -135,7 +141,12 @@ def test_dict_getitemstring(self):
self.assertEqual(getitemstring(dct2, b'a'), 1)
self.assertIs(getitemstring(dct2, b'b'), KeyError)

self.assertIs(getitemstring({}, INVALID_UTF8), KeyError)
with support.catch_unraisable_exception() as cm:
self.assertIs(getitemstring({}, INVALID_UTF8), KeyError)
self.assertEqual(cm.unraisable.exc_type, UnicodeDecodeError)
self.assertRegex(str(cm.unraisable.exc_value),
"'utf-8' codec can't decode")

self.assertIs(getitemstring(42, b'a'), KeyError)
self.assertIs(getitemstring([], b'a'), KeyError)
# CRASHES getitemstring({}, NULL)
Expand Down
6 changes: 5 additions & 1 deletion Lib/test/test_capi/test_sys.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ def test_sys_getobject(self):
self.assertEqual(getobject('\U0001f40d'.encode()), 42)

self.assertIs(getobject(b'nonexisting'), AttributeError)
self.assertIs(getobject(b'\xff'), AttributeError)
with support.catch_unraisable_exception() as cm:
self.assertIs(getobject(b'\xff'), AttributeError)
self.assertEqual(cm.unraisable.exc_type, UnicodeDecodeError)
self.assertRegex(str(cm.unraisable.exc_value),
"'utf-8' codec can't decode")
# CRASHES getobject(NULL)

@support.cpython_only
Expand Down
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`.
serhiy-storchaka marked this conversation as resolved.
Show resolved Hide resolved
76 changes: 58 additions & 18 deletions Objects/abstract.c
Original file line number Diff line number Diff line change
Expand Up @@ -2468,31 +2468,71 @@ PyMapping_HasKeyWithError(PyObject *obj, PyObject *key)
}

int
PyMapping_HasKeyString(PyObject *o, const char *key)
PyMapping_HasKeyString(PyObject *obj, const char *key)
{
PyObject *v;

v = PyMapping_GetItemString(o, key);
if (v) {
Py_DECREF(v);
return 1;
PyObject *dummy;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe rename to 'item' or 'value'.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

int rc;
if (obj == NULL) {
// For backward compatibility.
// PyMapping_GetOptionalItemString() crashes if it is NULL.
serhiy-storchaka marked this conversation as resolved.
Show resolved Hide resolved
null_error();
rc = -1;
}
PyErr_Clear();
return 0;
else {
rc = PyMapping_GetOptionalItemString(obj, key, &dummy);
}
if (rc < 0) {
PyErr_FormatUnraisable(
"Exception ignored in PyMapping_HasKeyString(); consider using "
"PyMapping_HasKeyStringWithError(), "
"PyMapping_GetOptionalItemString() or PyMapping_GetItemString()");
return 0;
}
// PyMapping_HasKeyString() also clears the error set before it's call
serhiy-storchaka marked this conversation as resolved.
Show resolved Hide resolved
// if the key is not found.
if (rc == 0 && PyErr_Occurred()) {
PyErr_FormatUnraisable(
"Ignore exception set before calling in PyMapping_HasKeyString(); "
"consider using PyMapping_HasKeyStringWithError(), "
"PyMapping_GetOptionalItemString() or PyMapping_GetItemString()");
Copy link
Member

@vstinner vstinner Nov 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can these 3 functions be called with an exception set? They don't override the exception? That sounds surprising. I would prefer suggesting to not call the function with an exception set. What do you think?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got catch. Indeed, PyMapping_GetOptionalItemString() can only return 0 if no exception is set, so this condition is always false. Also, the alternative functions also can clear exceptions.

I think that we should classify the C API by classes:

  1. Function that can be called when an exception is set, and they do not change it.
  2. Function that can be called when an exception is set, and they do not change it unless they fail.
  3. Function that can be called when an exception is set, but they can change it even at success.
  4. Function that can be called when an exception is set, but the result is ambiguous in some cases (you cannot distinguish some successful results from failure).
  5. Function that should never be called when an exception is set.

There may be more classes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the general case, to write safe code handling a raised exception, I think the safest option is to keep the exception aside using PyErr_GetRaisedException(). Maybe today some functions are perfectly fine and never override the currently raised exception. But what if tomorrow their implementation changes, and they may start to clear the currently raised exception?

In Python, in an except: block, there is no "currently raised exception" in the C level, even if sys.exc_info() returns the exception. The difference between PyThreadState.exc_info and PyThreadState.current_exception is subtle.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is why it should be clearly documented.

Obviously you can call PyErr_Occurred(), PyErr_GetRaisedException() or PyErr_WriteUnraisable() when an exception is set.

return 0;
}
Py_XDECREF(dummy);
return rc;
}

int
PyMapping_HasKey(PyObject *o, PyObject *key)
PyMapping_HasKey(PyObject *obj, PyObject *key)
{
PyObject *v;

v = PyObject_GetItem(o, key);
if (v) {
Py_DECREF(v);
return 1;
PyObject *dummy;
int rc;
if (obj == NULL || key == NULL) {
// For backward compatibility.
// PyMapping_GetOptionalItem() crashes if any of them is NULL.
null_error();
rc = -1;
}
PyErr_Clear();
return 0;
else {
rc = PyMapping_GetOptionalItem(obj, key, &dummy);
}
if (rc < 0) {
PyErr_FormatUnraisable(
"Exception ignored in PyMapping_HasKey(); consider using "
"PyMapping_HasKeyWithError(), "
"PyMapping_GetOptionalItem() or PyObject_GetItem()");
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_FormatUnraisable(
"Ignore exception set before calling in PyMapping_HasKey(); "
"consider using PyMapping_HasKeyWithError(), "
"PyMapping_GetOptionalItem() or PyObject_GetItem()");
return 0;
}
Py_XDECREF(dummy);
return rc;
}

/* This function is quite similar to PySequence_Fast(), but specialized to be
Expand Down
26 changes: 21 additions & 5 deletions Objects/dictobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1663,8 +1663,8 @@ _PyDict_FromItems(PyObject *const *keys, Py_ssize_t keys_offset,
* function hits a stack-depth error, which can cause this to return NULL
* even if the key is present.
*/
PyObject *
PyDict_GetItem(PyObject *op, PyObject *key)
static PyObject *
dict_getitem(PyObject *op, PyObject *key, const char *warnmsg)
{
if (!PyDict_Check(op)) {
return NULL;
Expand All @@ -1675,7 +1675,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_FormatUnraisable(warnmsg);
return NULL;
}
}
Expand All @@ -1696,12 +1696,24 @@ 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_FormatUnraisable(warnmsg);
}
_PyErr_SetRaisedException(tstate, exc);

assert(ix >= 0 || value == NULL);
return value; // borrowed reference
}

PyObject *
PyDict_GetItem(PyObject *op, PyObject *key)
{
return dict_getitem(op, key,
"Exception ignored in PyDict_GetItem(); consider using "
"PyDict_GetItemRef() or PyDict_GetItemWithError()");
}

Py_ssize_t
_PyDict_LookupIndex(PyDictObject *mp, PyObject *key)
{
Expand Down Expand Up @@ -3925,10 +3937,14 @@ PyDict_GetItemString(PyObject *v, const char *key)
PyObject *kv, *rv;
kv = PyUnicode_FromString(key);
if (kv == NULL) {
PyErr_Clear();
PyErr_FormatUnraisable(
"Exception ignored in PyDict_GetItemString(); consider using "
"PyDict_GetItemRefString()");
return NULL;
}
rv = PyDict_GetItem(v, kv);
rv = dict_getitem(v, kv,
"Exception ignored in PyDict_GetItemString(); consider using "
"PyDict_GetItemRefString()");
Py_DECREF(kv);
return rv; // borrowed reference
}
Expand Down
Loading
Loading