8000 Fix regressions in handling NULLs. · python/cpython@b5a661b · GitHub
[go: up one dir, main page]

Skip to content

Commit b5a661b

Browse files
Fix regressions in handling NULLs.
1 parent 4e13132 commit b5a661b

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

Lib/test/test_capi/test_abstract.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -322,8 +322,17 @@ def test_mapping_haskey(self):
322322
self.assertEqual(str(cm.unraisable.exc_value),
323323
'list indices must be integers or slices, not str')
324324

325-
# CRASHES haskey({}, NULL)
326-
# CRASHES haskey(NULL, 'a')
325+
with support.catch_unraisable_exception() as cm:
326+
self.assertFalse(haskey({}, NULL))
327+
self.assertEqual(cm.unraisable.exc_type, SystemError)
328+
self.assertEqual(str(cm.unraisable.exc_value),
329+
'null argument to internal routine')
330+
331< 8000 /code>+
with support.catch_unraisable_exception() as cm:
332+
self.assertFalse(haskey(NULL, 'a'))
333+
self.assertEqual(cm.unraisable.exc_type, SystemError)
334+
self.assertEqual(str(cm.unraisable.exc_value),
335+
'null argument to internal routine')
327336

328337
def test_mapping_haskeystring(self):
329338
haskeystring = _testcapi.mapping_haskeystring
@@ -360,7 +369,11 @@ def test_mapping_haskeystring(self):
360369
self.assertEqual(str(cm.unraisable.exc_value),
361370
'list indices must be integers or slices, not str')
362371

363-
# CRASHES haskeystring(NULL, b'a')
372+
with support.catch_unraisable_exception() as cm:
373+
self.assertFalse(haskeystring(NULL, b'a'))
374+
self.assertEqual(cm.unraisable.exc_type, SystemError)
375+
self.assertEqual(str(cm.unraisable.exc_value),
376+
"null argument to internal routine")
364377

365378
def test_object_setitem(self):
366379
setitem = _testcapi.object_setitem

Objects/abstract.c

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2429,7 +2429,16 @@ int
24292429
PyMapping_HasKeyString(PyObject *obj, const char *key)
24302430
{
24312431
PyObject *dummy;
2432-
int rc = PyMapping_GetOptionalItemString(obj, key, &dummy);
2432+
int rc;
2433+
if (obj == NULL) {
2434+
// For backward compatibility.
2435+
// PyMapping_GetOptionalItemString() crashes if it is NULL.
2436+
null_error();
2437+
rc = -1;
2438+
}
2439+
else {
2440+
rc = PyMapping_GetOptionalItemString(obj, key, &dummy);
2441+
}
24332442
if (rc < 0) {
24342443
_PyErr_WriteUnraisableMsg(
24352444
"in PyMapping_HasKeyString(); consider using "
@@ -2454,7 +2463,16 @@ int
24542463
PyMapping_HasKey(PyObject *obj, PyObject *key)
24552464
{
24562465
PyObject *dummy;
2457-
int rc = PyMapping_GetOptionalItem(obj, key, &dummy);
2466+
int rc;
2467+
if (obj == NULL || key == NULL) {
2468+
// For backward compatibility.
2469+
// PyMapping_GetOptionalItem() crashes if any of them is NULL.
2470+
null_error();
2471+
rc = -1;
2472+
}
2473+
else {
2474+
rc = PyMapping_GetOptionalItem(obj, key, &dummy);
2475+
}
24582476
if (rc < 0) {
24592477
_PyErr_WriteUnraisableMsg(
24602478
"in PyMapping_HasKey(); consider using "

0 commit comments

Comments
 (0)
0