From 3b3be05a164da43f201e35b6dafbc840993a4d18 Mon Sep 17 00:00:00 2001 From: Pablo Galindo Salgado Date: Mon, 7 Mar 2022 12:23:11 +0000 Subject: bpo-46940: Don't override existing AttributeError suggestion information (GH-31710) When an exception is created in a nested call to PyObject_GetAttr, any external calls will override the context information of the AttributeError that we have already placed in the most internal call. This will cause the suggestions we create to nor work properly as the attribute name and object that we will be using are the incorrect ones. To avoid this, we need to check first if these attributes are already set and bail out if that's the case. --- Python/ceval.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'Python/ceval.c') diff --git a/Python/ceval.c b/Python/ceval.c index 0743894c45..7439710ae4 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -7607,9 +7607,12 @@ format_exc_check_arg(PyThreadState *tstate, PyObject *exc, PyErr_Fetch(&type, &value, &traceback); PyErr_NormalizeException(&type, &value, &traceback); if (PyErr_GivenExceptionMatches(value, PyExc_NameError)) { - // We do not care if this fails because we are going to restore the - // NameError anyway. - (void)PyObject_SetAttr(value, &_Py_ID(name), obj); + PyNameErrorObject* exc = (PyNameErrorObject*) value; + if (exc->name == NULL) { + // We do not care if this fails because we are going to restore the + // NameError anyway. + (void)PyObject_SetAttr(value, &_Py_ID(name), obj); + } } PyErr_Restore(type, value, traceback); } -- cgit v1.2.1