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.
This commit is contained in:
Serhiy Storchaka 2023-06-23 20:10:32 +03:00 committed by GitHub
parent 41ad4dfc04
commit 1d33d53780
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 35 additions and 34 deletions

View File

@ -383,14 +383,15 @@ py_digest_by_digestmod(PyObject *module, PyObject *digestmod, enum Py_hash_type
} else { } else {
_hashlibstate *state = get_hashlib_state(module); _hashlibstate *state = get_hashlib_state(module);
// borrowed ref // borrowed ref
name_obj = PyDict_GetItem(state->constructs, digestmod); name_obj = PyDict_GetItemWithError(state->constructs, digestmod);
} }
if (name_obj == NULL) { if (name_obj == NULL) {
_hashlibstate *state = get_hashlib_state(module); if (!PyErr_Occurred()) {
PyErr_Clear(); _hashlibstate *state = get_hashlib_state(module);
PyErr_Format( PyErr_Format(
state->unsupported_digestmod_error, state->unsupported_digestmod_error,
"Unsupported digestmod %R", digestmod); "Unsupported digestmod %R", digestmod);
}
return NULL; return NULL;
} }

View File

@ -9,7 +9,7 @@ get_code_extra_index(PyInterpreterState* interp) {
PyObject *interp_dict = PyInterpreterState_GetDict(interp); // borrowed PyObject *interp_dict = PyInterpreterState_GetDict(interp); // borrowed
assert(interp_dict); // real users would handle missing dict... somehow assert(interp_dict); // real users would handle missing dict... somehow
PyObject *index_obj = PyDict_GetItemString(interp_dict, key); // borrowed PyObject *index_obj = _PyDict_GetItemStringWithError(interp_dict, key); // borrowed
Py_ssize_t index = 0; Py_ssize_t index = 0;
if (!index_obj) { if (!index_obj) {
if (PyErr_Occurred()) { if (PyErr_Occurred()) {

View File

@ -207,22 +207,21 @@ BaseException_add_note(PyObject *self, PyObject *note)
return NULL; return NULL;
} }
if (!PyObject_HasAttr(self, &_Py_ID(__notes__))) { PyObject *notes;
PyObject *new_notes = PyList_New(0); if (_PyObject_LookupAttr(self, &_Py_ID(__notes__), &notes) < 0) {
if (new_notes == NULL) {
return NULL;
}
if (PyObject_SetAttr(self, &_Py_ID(__notes__), new_notes) < 0) {
Py_DECREF(new_notes);
return NULL;
}
Py_DECREF(new_notes);
}
PyObject *notes = PyObject_GetAttr(self, &_Py_ID(__notes__));
if (notes == NULL) {
return NULL; return NULL;
} }
if (!PyList_Check(notes)) { if (notes == NULL) {
notes = PyList_New(0);
if (notes == NULL) {
return NULL;
}
if (PyObject_SetAttr(self, &_Py_ID(__notes__), notes) < 0) {
Py_DECREF(notes);
return NULL;
}
}
else if (!PyList_Check(notes)) {
Py_DECREF(notes); Py_DECREF(notes);
PyErr_SetString(PyExc_TypeError, "Cannot add note: __notes__ is not a list"); PyErr_SetString(PyExc_TypeError, "Cannot add note: __notes__ is not a list");
return NULL; return NULL;
@ -941,11 +940,11 @@ exceptiongroup_subset(
PyException_SetContext(eg, PyException_GetContext(orig)); PyException_SetContext(eg, PyException_GetContext(orig));
PyException_SetCause(eg, PyException_GetCause(orig)); PyException_SetCause(eg, PyException_GetCause(orig));
if (PyObject_HasAttr(orig, &_Py_ID(__notes__))) { PyObject *notes;
PyObject *notes = PyObject_GetAttr(orig, &_Py_ID(__notes__)); if (_PyObject_LookupAttr(orig, &_Py_ID(__notes__), &notes) < 0) {
if (notes == NULL) { goto error;
goto error; }
} if (notes) {
if (PySequence_Check(notes)) { if (PySequence_Check(notes)) {
/* Make a copy so the parts have independent notes lists. */ /* Make a copy so the parts have independent notes lists. */
PyObject *notes_copy = PySequence_List(notes); PyObject *notes_copy = PySequence_List(notes);

View File

@ -1508,11 +1508,14 @@ type_set_annotations(PyTypeObject *type, PyObject *value, void *context)
static PyObject * static PyObject *
type_get_type_params(PyTypeObject *type, void *context) type_get_type_params(PyTypeObject *type, void *context)
{ {
PyObject *params = PyDict_GetItem(lookup_tp_dict(type), &_Py_ID(__type_params__)); PyObject *params = PyDict_GetItemWithError(lookup_tp_dict(type), &_Py_ID(__type_params__));
if (params) { if (params) {
return Py_NewRef(params); return Py_NewRef(params);
} }
if (PyErr_Occurred()) {
return NULL;
}
return PyTuple_New(0); return PyTuple_New(0);
} }

View File

@ -1134,15 +1134,13 @@ print_exception_notes(struct exception_print_context *ctx, PyObject *value)
return 0; return 0;
} }
if (!PyObject_HasAttr(value, &_Py_ID(__notes__))) { PyObject *notes;
return 0; int res = _PyObject_LookupAttr(value, &_Py_ID(__notes__), &notes);
} if (res <= 0) {
PyObject *notes = PyObject_GetAttr(value, &_Py_ID(__notes__)); return res;
if (notes == NULL) {
return -1;
} }
if (!PySequence_Check(notes) || PyUnicode_Check(notes) || PyBytes_Check(notes)) { if (!PySequence_Check(notes) || PyUnicode_Check(notes) || PyBytes_Check(notes)) {
int res = 0; res = 0;
if (write_indented_margin(ctx, f) < 0) { if (write_indented_margin(ctx, f) < 0) {
res = -1; res = -1;
} }