mirror of
https://github.com/python/cpython.git
synced 2025-08-22 09:45:06 +00:00
gh-102192: Replace PyErr_Fetch/Restore etc by more efficient alternatives (in Objects/) (#102218)
This commit is contained in:
parent
b097925858
commit
11a2c6ce51
8 changed files with 51 additions and 106 deletions
|
@ -370,13 +370,12 @@ _PyObject_Dump(PyObject* op)
|
|||
fflush(stderr);
|
||||
|
||||
PyGILState_STATE gil = PyGILState_Ensure();
|
||||
PyObject *error_type, *error_value, *error_traceback;
|
||||
PyErr_Fetch(&error_type, &error_value, &error_traceback);
|
||||
PyObject *exc = PyErr_GetRaisedException();
|
||||
|
||||
(void)PyObject_Print(op, stderr, 0);
|
||||
fflush(stderr);
|
||||
|
||||
PyErr_Restore(error_type, error_value, error_traceback);
|
||||
PyErr_SetRaisedException(exc);
|
||||
PyGILState_Release(gil);
|
||||
|
||||
fprintf(stderr, "\n");
|
||||
|
@ -860,25 +859,22 @@ set_attribute_error_context(PyObject* v, PyObject* name)
|
|||
return 0;
|
||||
}
|
||||
// Intercept AttributeError exceptions and augment them to offer suggestions later.
|
||||
PyObject *type, *value, *traceback;
|
||||
PyErr_Fetch(&type, &value, &traceback);
|
||||
PyErr_NormalizeException(&type, &value, &traceback);
|
||||
// Check if the normalized exception is indeed an AttributeError
|
||||
if (!PyErr_GivenExceptionMatches(value, PyExc_AttributeError)) {
|
||||
PyObject *exc = PyErr_GetRaisedException();
|
||||
if (!PyErr_GivenExceptionMatches(exc, PyExc_AttributeError)) {
|
||||
goto restore;
|
||||
}
|
||||
PyAttributeErrorObject* the_exc = (PyAttributeErrorObject*) value;
|
||||
PyAttributeErrorObject* the_exc = (PyAttributeErrorObject*) exc;
|
||||
// Check if this exception was already augmented
|
||||
if (the_exc->name || the_exc->obj) {
|
||||
goto restore;
|
||||
}
|
||||
// Augment the exception with the name and object
|
||||
if (PyObject_SetAttr(value, &_Py_ID(name), name) ||
|
||||
PyObject_SetAttr(value, &_Py_ID(obj), v)) {
|
||||
if (PyObject_SetAttr(exc, &_Py_ID(name), name) ||
|
||||
PyObject_SetAttr(exc, &_Py_ID(obj), v)) {
|
||||
return 1;
|
||||
}
|
||||
restore:
|
||||
PyErr_Restore(type, value, traceback);
|
||||
PyErr_SetRaisedException(exc);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -2190,9 +2186,8 @@ Py_ReprLeave(PyObject *obj)
|
|||
PyObject *dict;
|
||||
PyObject *list;
|
||||
Py_ssize_t i;
|
||||
PyObject *error_type, *error_value, *error_traceback;
|
||||
|
||||
PyErr_Fetch(&error_type, &error_value, &error_traceback);
|
||||
PyObject *exc = PyErr_GetRaisedException();
|
||||
|
||||
dict = PyThreadState_GetDict();
|
||||
if (dict == NULL)
|
||||
|
@ -2213,7 +2208,7 @@ Py_ReprLeave(PyObject *obj)
|
|||
|
||||
finally:
|
||||
/* ignore exceptions because there is no way to report them. */
|
||||
PyErr_Restore(error_type, error_value, error_traceback);
|
||||
PyErr_SetRaisedException(exc);
|
||||
}
|
||||
|
||||
/* Trashcan support. */
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue