8000 gh-106672: C API: Report indiscriminately ignored errors by serhiy-storchaka · Pull Request #106674 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

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

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Fix regressions in handling NULLs.
  • Loading branch information
serhiy-storchaka committed Aug 7, 2023
commit b5a661bbd929bb3d066e56c56f1825eb6c187aed
19 changes: 16 additions & 3 deletions Lib/test/test_capi/test_abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,8 +322,17 @@ def test_mapping_haskey(self):
self.assertEqual(str(cm.unraisable.exc_value),
'list indices must be integers or slices, not str')

# CRASHES haskey({}, NULL)
# CRASHES haskey(NULL, 'a')
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 Down Expand Up @@ -360,7 +369,11 @@ def test_mapping_haskeystring(self):
self.assertEqual(str(cm.unraisable.exc_value),
'list indices must be integers or slices, not str')

# CRASHES haskeystring(NULL, b'a')
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_object_setitem(self):
setitem = _testcapi.object_setitem
Expand Down
22 changes: 20 additions & 2 deletions Objects/abstract.c
Original file line number Diff line number Diff line change
Expand Up @@ -2429,7 +2429,16 @@ int
PyMapping_HasKeyString(PyObject *obj, const char *key)
{
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 = PyMapping_GetOptionalItemString(obj, key, &dummy);
int rc;
if (obj == NULL) {
// For backward compatibility.
// PyMapping_GetOptionalItemString() crashes if it is NULL.
null_error();
rc = -1;
}
else {
rc = PyMapping_GetOptionalItemString(obj, key, &dummy);
}
if (rc < 0) {
_PyErr_WriteUnraisableMsg(
"in PyMapping_HasKeyString(); consider using "
Expand All @@ -2454,7 +2463,16 @@ int
PyMapping_HasKey(PyObject *obj, PyObject *key)
{
PyObject *dummy;
int rc = PyMapping_GetOptionalItem(obj, key, &dummy);
int rc;
if (obj == NULL || key == NULL) {
// For backward compatibility.
// PyMapping_GetOptionalItem() crashes if any of them is NULL.
null_error();
rc = -1;
}
else {
rc = PyMapping_GetOptionalItem(obj, key, &dummy);
}
if (rc < 0) {
_PyErr_WriteUnraisableMsg(
"in PyMapping_HasKey(); consider using "
Expand Down
0