Skip to content

Commit dbe416b

Browse files
[3.11] gh-106033: Get rid of new occurrences of PyDict_GetItem and Py… (#106040)
[3.11] gh-106033: Get rid of new occurrences of PyDict_GetItem and PyObject_HasAttr (GH-106034) These functions are broken by design because they discard any exceptions raised inside, including MemoryError and KeyboardInterrupt. They should not be used in new code.. (cherry picked from commit 1d33d53)
1 parent c69f29f commit dbe416b

File tree

3 files changed

+27
-29
lines changed

3 files changed

+27
-29
lines changed

Modules/_hashopenssl.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -384,14 +384,15 @@ py_digest_by_digestmod(PyObject *module, PyObject *digestmod, enum Py_hash_type
384384
} else {
385385
_hashlibstate *state = get_hashlib_state(module);
386386
// borrowed ref
387-
name_obj = PyDict_GetItem(state->constructs, digestmod);
387+
name_obj = PyDict_GetItemWithError(state->constructs, digestmod);
388388
}
389389
if (name_obj == NULL) {
390-
_hashlibstate *state = get_hashlib_state(module);
391-
PyErr_Clear();
392-
PyErr_Format(
393-
state->unsupported_digestmod_error,
394-
"Unsupported digestmod %R", digestmod);
390+
if (!PyErr_Occurred()) {
391+
_hashlibstate *state = get_hashlib_state(module);
392+
PyErr_Format(
393+
state->unsupported_digestmod_error,
394+
"Unsupported digestmod %R", digestmod);
395+
}
395396
return NULL;
396397
}
397398

Objects/exceptions.c

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -210,22 +210,21 @@ BaseException_add_note(PyObject *self, PyObject *note)
210210
return NULL;
211211
}
212212

213-
if (!PyObject_HasAttr(self, &_Py_ID(__notes__))) {
214-
PyObject *new_notes = PyList_New(0);
215-
if (new_notes == NULL) {
213+
PyObject *notes;
214+
if (_PyObject_LookupAttr(self, &_Py_ID(__notes__), &notes) < 0) {
215+
return NULL;
216+
}
217+
if (notes == NULL) {
218+
notes = PyList_New(0);
219+
if (notes == NULL) {
216220
return NULL;
217221
}
218-
if (PyObject_SetAttr(self, &_Py_ID(__notes__), new_notes) < 0) {
219-
Py_DECREF(new_notes);
222+
if (PyObject_SetAttr(self, &_Py_ID(__notes__), notes) < 0) {
223+
Py_DECREF(notes);
220224
return NULL;
221225
}
222-
Py_DECREF(new_notes);
223226
}
224-
PyObject *notes = PyObject_GetAttr(self, &_Py_ID(__notes__));
225-
if (notes == NULL) {
226-
return NULL;
227-
}
228-
if (!PyList_Check(notes)) {
227+
else if (!PyList_Check(notes)) {
229228
Py_DECREF(notes);
230229
PyErr_SetString(PyExc_TypeError, "Cannot add note: __notes__ is not a list");
231230
return NULL;
@@ -943,11 +942,11 @@ exceptiongroup_subset(
943942
PyException_SetContext(eg, PyException_GetContext(orig));
944943
PyException_SetCause(eg, PyException_GetCause(orig));
945944

946-
if (PyObject_HasAttr(orig, &_Py_ID(__notes__))) {
947-
PyObject *notes = PyObject_GetAttr(orig, &_Py_ID(__notes__));
948-
if (notes == NULL) {
949-
goto error;
950-
}
945+
PyObject *notes;
946+
if (_PyObject_LookupAttr(orig, &_Py_ID(__notes__), &notes) < 0) {
947+
goto error;
948+
}
949+
if (notes) {
951950
if (PySequence_Check(notes)) {
952951
/* Make a copy so the parts have independent notes lists. */
953952
PyObject *notes_copy = PySequence_List(notes);

Python/pythonrun.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,15 +1138,13 @@ print_exception_notes(struct exception_print_context *ctx, PyObject *value)
11381138
return 0;
11391139
}
11401140

1141-
if (!PyObject_HasAttr(value, &_Py_ID(__notes__))) {
1142-
return 0;
1143-
}
1144-
PyObject *notes = PyObject_GetAttr(value, &_Py_ID(__notes__));
1145-
if (notes == NULL) {
1146-
return -1;
1141+
PyObject *notes;
1142+
int res = _PyObject_LookupAttr(value, &_Py_ID(__notes__), &notes);
1143+
if (res <= 0) {
1144+
return res;
11471145
}
11481146
if (!PySequence_Check(notes)) {
1149-
int res = 0;
1147+
res = 0;
11501148
if (write_indented_margin(ctx, f) < 0) {
11511149
res = -1;
11521150
}

0 commit comments

Comments
 (0)