[3.8] bpo-37994: Fix silencing all errors if an attribute lookup fails. (GH-15630) (GH-15635)
Only AttributeError should be silenced.
(cherry picked from commit 41c57b335330ff48af098d47e379e0f9ba09d233)
diff --git a/Modules/_csv.c b/Modules/_csv.c
index 014cbb4..46d4143 100644
--- a/Modules/_csv.c
+++ b/Modules/_csv.c
@@ -1382,7 +1382,10 @@
Py_DECREF(self);
return NULL;
}
- self->write = _PyObject_GetAttrId(output_file, &PyId_write);
+ if (_PyObject_LookupAttrId(output_file, &PyId_write, &self->write) < 0) {
+ Py_DECREF(self);
+ return NULL;
+ }
if (self->write == NULL || !PyCallable_Check(self->write)) {
PyErr_SetString(PyExc_TypeError,
"argument 1 must have a \"write\" method");
diff --git a/Modules/_datetimemodule.c b/Modules/_datetimemodule.c
index d3c3f09..41c3f34 100644
--- a/Modules/_datetimemodule.c
+++ b/Modules/_datetimemodule.c
@@ -3610,24 +3610,24 @@
_Py_IDENTIFIER(__getinitargs__);
_Py_IDENTIFIER(__getstate__);
- getinitargs = _PyObject_GetAttrId(self, &PyId___getinitargs__);
+ if (_PyObject_LookupAttrId(self, &PyId___getinitargs__, &getinitargs) < 0) {
+ return NULL;
+ }
if (getinitargs != NULL) {
args = _PyObject_CallNoArg(getinitargs);
Py_DECREF(getinitargs);
- if (args == NULL) {
- return NULL;
- }
}
else {
- PyErr_Clear();
-
args = PyTuple_New(0);
- if (args == NULL) {
- return NULL;
- }
+ }
+ if (args == NULL) {
+ return NULL;
}
- getstate = _PyObject_GetAttrId(self, &PyId___getstate__);
+ if (_PyObject_LookupAttrId(self, &PyId___getstate__, &getstate) < 0) {
+ Py_DECREF(args);
+ return NULL;
+ }
if (getstate != NULL) {
state = _PyObject_CallNoArg(getstate);
Py_DECREF(getstate);
@@ -3638,7 +3638,6 @@
}
else {
PyObject **dictptr;
- PyErr_Clear();
state = Py_None;
dictptr = _PyObject_GetDictPtr(self);
if (dictptr && *dictptr && PyDict_GET_SIZE(*dictptr)) {
diff --git a/Modules/_pickle.c b/Modules/_pickle.c
index bbe2603..fa2ef7d 100644
--- a/Modules/_pickle.c
+++ b/Modules/_pickle.c
@@ -4385,7 +4385,6 @@
_Py_IDENTIFIER(__reduce__);
_Py_IDENTIFIER(__reduce_ex__);
-
/* XXX: If the __reduce__ method is defined, __reduce_ex__ is
automatically defined as __reduce__. While this is convenient, this
make it impossible to know which method was actually called. Of
@@ -4406,14 +4405,15 @@
}
}
else {
- PickleState *st = _Pickle_GetGlobalState();
-
/* Check for a __reduce__ method. */
- reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce__);
+ if (_PyObject_LookupAttrId(obj, &PyId___reduce__, &reduce_func) < 0) {
+ goto error;
+ }
if (reduce_func != NULL) {
reduce_value = _PyObject_CallNoArg(reduce_func);
}
else {
+ PickleState *st = _Pickle_GetGlobalState();
PyErr_Format(st->PicklingError,
"can't pickle '%.200s' object: %R",
type->tp_name, obj);
@@ -6448,7 +6448,9 @@
PyObject *extend_func;
_Py_IDENTIFIER(extend);
- extend_func = _PyObject_GetAttrId(list, &PyId_extend);
+ if (_PyObject_LookupAttrId(list, &PyId_extend, &extend_func) < 0) {
+ return -1;
+ }
if (extend_func != NULL) {
slice = Pdata_poplist(self->stack, x);
if (!slice) {
@@ -6468,7 +6470,6 @@
/* Even if the PEP 307 requires extend() and append() methods,
fall back on append() if the object has no extend() method
for backward compatibility. */
- PyErr_Clear();
append_func = _PyObject_GetAttrId(list, &PyId_append);
if (append_func == NULL)
return -1;
diff --git a/Modules/_threadmodule.c b/Modules/_threadmodule.c
index d5e40ef..fadf57a 100644
--- a/Modules/_threadmodule.c
+++ b/Modules/_threadmodule.c
@@ -1305,6 +1305,7 @@
thread_excepthook_file(PyObject *file, PyObject *exc_type, PyObject *exc_value,
PyObject *exc_traceback, PyObject *thread)
{
+ _Py_IDENTIFIER(name);
/* print(f"Exception in thread {thread.name}:", file=file) */
if (PyFile_WriteString("Exception in thread ", file) < 0) {
return -1;
@@ -1312,7 +1313,9 @@
PyObject *name = NULL;
if (thread != Py_None) {
- name = PyObject_GetAttrString(thread, "name");
+ if (_PyObject_LookupAttrId(thread, &PyId_name, &name) < 0) {
+ return -1;
+ }
}
if (name != NULL) {
if (PyFile_WriteObject(name, file, Py_PRINT_RAW) < 0) {
@@ -1322,8 +1325,6 @@
Py_DECREF(name);
}
else {
- PyErr_Clear();
-
unsigned long ident = PyThread_get_thread_ident();
PyObject *str = PyUnicode_FromFormat("%lu", ident);
if (str != NULL) {
diff --git a/Modules/pyexpat.c b/Modules/pyexpat.c
index 45a1e68..bb5e25c 100644
--- a/Modules/pyexpat.c
+++ b/Modules/pyexpat.c
@@ -810,7 +810,9 @@
PyObject *readmethod = NULL;
_Py_IDENTIFIER(read);
- readmethod = _PyObject_GetAttrId(file, &PyId_read);
+ if (_PyObject_LookupAttrId(file, &PyId_read, &readmethod) < 0) {
+ return NULL;
+ }
if (readmethod == NULL) {
PyErr_SetString(PyExc_TypeError,
"argument must have 'read' attribute");