summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2023-06-23 17:10:32 (GMT)
committerGitHub <noreply@github.com>2023-06-23 17:10:32 (GMT)
commit1d33d5378058671bfabb6f4d4b5bfd4726973ff9 (patch)
treec522809e945151a3525780025b88475695445ad3 /Objects
parent41ad4dfc04c201728ce9fa12b1a96922dd15a368 (diff)
downloadcpython-1d33d5378058671bfabb6f4d4b5bfd4726973ff9.zip
cpython-1d33d5378058671bfabb6f4d4b5bfd4726973ff9.tar.gz
cpython-1d33d5378058671bfabb6f4d4b5bfd4726973ff9.tar.bz2
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.
Diffstat (limited to 'Objects')
-rw-r--r--Objects/exceptions.c31
-rw-r--r--Objects/typeobject.c5
2 files changed, 19 insertions, 17 deletions
diff --git a/Objects/exceptions.c b/Objects/exceptions.c
index e2a3f89..04ea22c 100644
--- a/Objects/exceptions.c
+++ b/Objects/exceptions.c
@@ -207,22 +207,21 @@ BaseException_add_note(PyObject *self, PyObject *note)
return NULL;
}
- if (!PyObject_HasAttr(self, &_Py_ID(__notes__))) {
- PyObject *new_notes = PyList_New(0);
- if (new_notes == NULL) {
+ PyObject *notes;
+ if (_PyObject_LookupAttr(self, &_Py_ID(__notes__), &notes) < 0) {
+ return NULL;
+ }
+ if (notes == NULL) {
+ notes = PyList_New(0);
+ if (notes == NULL) {
return NULL;
}
- if (PyObject_SetAttr(self, &_Py_ID(__notes__), new_notes) < 0) {
- Py_DECREF(new_notes);
+ if (PyObject_SetAttr(self, &_Py_ID(__notes__), notes) < 0) {
+ Py_DECREF(notes);
return NULL;
}
- Py_DECREF(new_notes);
}
- PyObject *notes = PyObject_GetAttr(self, &_Py_ID(__notes__));
- if (notes == NULL) {
- return NULL;
- }
- if (!PyList_Check(notes)) {
+ else if (!PyList_Check(notes)) {
Py_DECREF(notes);
PyErr_SetString(PyExc_TypeError, "Cannot add note: __notes__ is not a list");
return NULL;
@@ -941,11 +940,11 @@ exceptiongroup_subset(
PyException_SetContext(eg, PyException_GetContext(orig));
PyException_SetCause(eg, PyException_GetCause(orig));
- if (PyObject_HasAttr(orig, &_Py_ID(__notes__))) {
- PyObject *notes = PyObject_GetAttr(orig, &_Py_ID(__notes__));
- if (notes == NULL) {
- goto error;
- }
+ PyObject *notes;
+ if (_PyObject_LookupAttr(orig, &_Py_ID(__notes__), &notes) < 0) {
+ goto error;
+ }
+ if (notes) {
if (PySequence_Check(notes)) {
/* Make a copy so the parts have independent notes lists. */
PyObject *notes_copy = PySequence_List(notes);
diff --git a/Objects/typeobject.c b/Objects/typeobject.c
index d427ecd..9aba53d 100644
--- a/Objects/typeobject.c
+++ b/Objects/typeobject.c
@@ -1508,11 +1508,14 @@ type_set_annotations(PyTypeObject *type, PyObject *value, void *context)
static PyObject *
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) {
return Py_NewRef(params);
}
+ if (PyErr_Occurred()) {
+ return NULL;
+ }
return PyTuple_New(0);
}