diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2019-09-01 11:01:05 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-09-01 11:01:05 (GMT) |
commit | 353053d9ad08fea0e205e6c008b8a4350c0188e6 (patch) | |
tree | 3bd4434c152e934fb260c1c1651c080c3df29a14 /Modules | |
parent | 6922b9e4fce635339cb94c2fdef6bba4e2a99621 (diff) | |
download | cpython-353053d9ad08fea0e205e6c008b8a4350c0188e6.zip cpython-353053d9ad08fea0e205e6c008b8a4350c0188e6.tar.gz cpython-353053d9ad08fea0e205e6c008b8a4350c0188e6.tar.bz2 |
[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)
Diffstat (limited to 'Modules')
-rw-r--r-- | Modules/_csv.c | 5 | ||||
-rw-r--r-- | Modules/_datetimemodule.c | 21 | ||||
-rw-r--r-- | Modules/_pickle.c | 13 | ||||
-rw-r--r-- | Modules/_threadmodule.c | 7 | ||||
-rw-r--r-- | Modules/pyexpat.c | 4 |
5 files changed, 28 insertions, 22 deletions
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 @@ csv_writer(PyObject *module, PyObject *args, PyObject *keyword_args) 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 @@ tzinfo_reduce(PyObject *self, PyObject *Py_UNUSED(ignored)) _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 @@ tzinfo_reduce(PyObject *self, PyObject *Py_UNUSED(ignored)) } 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 @@ save(PicklerObject *self, PyObject *obj, int pers_save) _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 @@ save(PicklerObject *self, PyObject *obj, int pers_save) } } 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 @@ do_append(UnpicklerObject *self, Py_ssize_t x) 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 @@ do_append(UnpicklerObject *self, Py_ssize_t x) /* 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 @@ static int 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 @@ thread_excepthook_file(PyObject *file, PyObject *exc_type, PyObject *exc_value, 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 @@ thread_excepthook_file(PyObject *file, PyObject *exc_type, PyObject *exc_value, 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 @@ pyexpat_xmlparser_ParseFile(xmlparseobject *self, PyObject *file) 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"); |