mirror of
https://github.com/python/cpython.git
synced 2025-08-30 21:48:47 +00:00
gh-108082: Use PyErr_FormatUnraisable() (GH-111580)
Replace most of calls of _PyErr_WriteUnraisableMsg() and some calls of PyErr_WriteUnraisable(NULL) with PyErr_FormatUnraisable(). Co-authored-by: Victor Stinner <vstinner@python.org>
This commit is contained in:
parent
a12f624a9d
commit
970e719a7a
18 changed files with 83 additions and 114 deletions
|
@ -2235,7 +2235,7 @@ PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
|
|||
PyThreadState *tstate = _PyThreadState_GET();
|
||||
if (_PyEval_SetProfile(tstate, func, arg) < 0) {
|
||||
/* Log _PySys_Audit() error */
|
||||
_PyErr_WriteUnraisableMsg("in PyEval_SetProfile", NULL);
|
||||
PyErr_FormatUnraisable("Exception ignored in PyEval_SetProfile");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -2252,7 +2252,7 @@ PyEval_SetProfileAllThreads(Py_tracefunc func, PyObject *arg)
|
|||
|
||||
while (ts) {
|
||||
if (_PyEval_SetProfile(ts, func, arg) < 0) {
|
||||
_PyErr_WriteUnraisableMsg("in PyEval_SetProfileAllThreads", NULL);
|
||||
PyErr_FormatUnraisable("Exception ignored in PyEval_SetProfileAllThreads");
|
||||
}
|
||||
HEAD_LOCK(runtime);
|
||||
ts = PyThreadState_Next(ts);
|
||||
|
@ -2266,7 +2266,7 @@ PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
|
|||
PyThreadState *tstate = _PyThreadState_GET();
|
||||
if (_PyEval_SetTrace(tstate, func, arg) < 0) {
|
||||
/* Log _PySys_Audit() error */
|
||||
_PyErr_WriteUnraisableMsg("in PyEval_SetTrace", NULL);
|
||||
PyErr_FormatUnraisable("Exception ignored in PyEval_SetTrace");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -2283,7 +2283,7 @@ PyEval_SetTraceAllThreads(Py_tracefunc func, PyObject *arg)
|
|||
|
||||
while (ts) {
|
||||
if (_PyEval_SetTrace(ts, func, arg) < 0) {
|
||||
_PyErr_WriteUnraisableMsg("in PyEval_SetTraceAllThreads", NULL);
|
||||
PyErr_FormatUnraisable("Exception ignored in PyEval_SetTraceAllThreads");
|
||||
}
|
||||
HEAD_LOCK(runtime);
|
||||
ts = PyThreadState_Next(ts);
|
||||
|
|
|
@ -34,7 +34,6 @@
|
|||
#include "pycore_flowgraph.h"
|
||||
#include "pycore_intrinsics.h"
|
||||
#include "pycore_long.h" // _PyLong_GetZero()
|
||||
#include "pycore_pyerrors.h" // _PyErr_WriteUnraisableMsg()
|
||||
#include "pycore_pystate.h" // _Py_GetConfig()
|
||||
#include "pycore_setobject.h" // _PySet_NextEntry()
|
||||
#include "pycore_symtable.h" // PySTEntryObject, _PyFuture_FromAST()
|
||||
|
@ -1407,8 +1406,8 @@ compiler_exit_scope(struct compiler *c)
|
|||
assert(c->u);
|
||||
/* we are deleting from a list so this really shouldn't fail */
|
||||
if (PySequence_DelItem(c->c_stack, n) < 0) {
|
||||
_PyErr_WriteUnraisableMsg("on removing the last compiler "
|
||||
"stack item", NULL);
|
||||
PyErr_FormatUnraisable("Exception ignored on removing "
|
||||
"the last compiler stack item");
|
||||
}
|
||||
}
|
||||
else {
|
||||
|
|
|
@ -584,7 +584,7 @@ _PyImport_ClearModulesByIndex(PyInterpreterState *interp)
|
|||
if (PyList_SetSlice(MODULES_BY_INDEX(interp),
|
||||
0, PyList_GET_SIZE(MODULES_BY_INDEX(interp)),
|
||||
NULL)) {
|
||||
PyErr_WriteUnraisable(MODULES_BY_INDEX(interp));
|
||||
PyErr_FormatUnraisable("Exception ignored on clearing interpreters module list");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -3156,13 +3156,13 @@ _PyImport_FiniCore(PyInterpreterState *interp)
|
|||
int verbose = _PyInterpreterState_GetConfig(interp)->verbose;
|
||||
|
||||
if (_PySys_ClearAttrString(interp, "meta_path", verbose) < 0) {
|
||||
PyErr_WriteUnraisable(NULL);
|
||||
PyErr_FormatUnraisable("Exception ignored on clearing sys.meta_path");
|
||||
}
|
||||
|
||||
// XXX Pull in most of finalize_modules() in pylifecycle.c.
|
||||
|
||||
if (_PySys_ClearAttrString(interp, "modules", verbose) < 0) {
|
||||
PyErr_WriteUnraisable(NULL);
|
||||
PyErr_FormatUnraisable("Exception ignored on clearing sys.modules");
|
||||
}
|
||||
|
||||
if (IMPORT_LOCK(interp) != NULL) {
|
||||
|
@ -3242,10 +3242,10 @@ _PyImport_FiniExternal(PyInterpreterState *interp)
|
|||
// XXX Uninstall importlib metapath importers here?
|
||||
|
||||
if (_PySys_ClearAttrString(interp, "path_importer_cache", verbose) < 0) {
|
||||
PyErr_WriteUnraisable(NULL);
|
||||
PyErr_FormatUnraisable("Exception ignored on clearing sys.path_importer_cache");
|
||||
}
|
||||
if (_PySys_ClearAttrString(interp, "path_hooks", verbose) < 0) {
|
||||
PyErr_WriteUnraisable(NULL);
|
||||
PyErr_FormatUnraisable("Exception ignored on clearing sys.path_hooks");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -133,7 +133,6 @@ any DWARF information available for them).
|
|||
#include "pycore_ceval.h" // _PyPerf_Callbacks
|
||||
#include "pycore_frame.h"
|
||||
#include "pycore_interp.h"
|
||||
#include "pycore_pyerrors.h" // _PyErr_WriteUnraisableMsg()
|
||||
|
||||
|
||||
#ifdef PY_HAVE_PERF_TRAMPOLINE
|
||||
|
@ -236,8 +235,7 @@ new_code_arena(void)
|
|||
0); // offset (not used here)
|
||||
if (!memory) {
|
||||
PyErr_SetFromErrno(PyExc_OSError);
|
||||
_PyErr_WriteUnraisableMsg(
|
||||
"Failed to create new mmap for perf trampoline", NULL);
|
||||
PyErr_FormatUnraisable("Failed to create new mmap for perf trampoline");
|
||||
perf_status = PERF_STATUS_FAILED;
|
||||
return -1;
|
||||
}
|
||||
|
@ -261,9 +259,8 @@ new_code_arena(void)
|
|||
if (res == -1) {
|
||||
PyErr_SetFromErrno(PyExc_OSError);
|
||||
munmap(memory, mem_size);
|
||||
_PyErr_WriteUnraisableMsg(
|
||||
"Failed to set mmap for perf trampoline to PROT_READ | PROT_EXEC",
|
||||
NULL);
|
||||
PyErr_FormatUnraisable("Failed to set mmap for perf trampoline to "
|
||||
"PROT_READ | PROT_EXEC");
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
@ -277,8 +274,7 @@ new_code_arena(void)
|
|||
if (new_arena == NULL) {
|
||||
PyErr_NoMemory();
|
||||
munmap(memory, mem_size);
|
||||
_PyErr_WriteUnraisableMsg("Failed to allocate new code arena struct",
|
||||
NULL);
|
||||
PyErr_FormatUnraisable("Failed to allocate new code arena struct for perf trampoline");
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
|
|
@ -1419,13 +1419,13 @@ finalize_modules_delete_special(PyThreadState *tstate, int verbose)
|
|||
PySys_WriteStderr("# clear builtins._\n");
|
||||
}
|
||||
if (PyDict_SetItemString(interp->builtins, "_", Py_None) < 0) {
|
||||
PyErr_WriteUnraisable(NULL);
|
||||
PyErr_FormatUnraisable("Exception ignored on setting builtin variable _");
|
||||
}
|
||||
|
||||
const char * const *p;
|
||||
for (p = sys_deletes; *p != NULL; p++) {
|
||||
if (_PySys_ClearAttrString(interp, *p, verbose) < 0) {
|
||||
PyErr_WriteUnraisable(NULL);
|
||||
PyErr_FormatUnraisable("Exception ignored on clearing sys.%s", *p);
|
||||
}
|
||||
}
|
||||
for (p = sys_files; *p != NULL; p+=2) {
|
||||
|
@ -1436,13 +1436,13 @@ finalize_modules_delete_special(PyThreadState *tstate, int verbose)
|
|||
}
|
||||
PyObject *value;
|
||||
if (PyDict_GetItemStringRef(interp->sysdict, orig_name, &value) < 0) {
|
||||
PyErr_WriteUnraisable(NULL);
|
||||
PyErr_FormatUnraisable("Exception ignored on restoring sys.%s", name);
|
||||
}
|
||||
if (value == NULL) {
|
||||
value = Py_NewRef(Py_None);
|
||||
}
|
||||
if (PyDict_SetItemString(interp->sysdict, name, value) < 0) {
|
||||
PyErr_WriteUnraisable(NULL);
|
||||
PyErr_FormatUnraisable("Exception ignored on restoring sys.%s", name);
|
||||
}
|
||||
Py_DECREF(value);
|
||||
}
|
||||
|
@ -1454,7 +1454,7 @@ finalize_remove_modules(PyObject *modules, int verbose)
|
|||
{
|
||||
PyObject *weaklist = PyList_New(0);
|
||||
if (weaklist == NULL) {
|
||||
PyErr_WriteUnraisable(NULL);
|
||||
PyErr_FormatUnraisable("Exception ignored on removing modules");
|
||||
}
|
||||
|
||||
#define STORE_MODULE_WEAKREF(name, mod) \
|
||||
|
@ -1463,13 +1463,13 @@ finalize_remove_modules(PyObject *modules, int verbose)
|
|||
if (wr) { \
|
||||
PyObject *tup = PyTuple_Pack(2, name, wr); \
|
||||
if (!tup || PyList_Append(weaklist, tup) < 0) { \
|
||||
PyErr_WriteUnraisable(NULL); \
|
||||
PyErr_FormatUnraisable("Exception ignored on removing modules"); \
|
||||
} \
|
||||
Py_XDECREF(tup); \
|
||||
Py_DECREF(wr); \
|
||||
} \
|
||||
else { \
|
||||
PyErr_WriteUnraisable(NULL); \
|
||||
PyErr_FormatUnraisable("Exception ignored on removing modules"); \
|
||||
} \
|
||||
}
|
||||
|
||||
|
@ -1480,7 +1480,7 @@ finalize_remove_modules(PyObject *modules, int verbose)
|
|||
} \
|
||||
STORE_MODULE_WEAKREF(name, mod); \
|
||||
if (PyObject_SetItem(modules, name, Py_None) < 0) { \
|
||||
PyErr_WriteUnraisable(NULL); \
|
||||
PyErr_FormatUnraisable("Exception ignored on removing modules"); \
|
||||
} \
|
||||
}
|
||||
|
||||
|
@ -1494,14 +1494,14 @@ finalize_remove_modules(PyObject *modules, int verbose)
|
|||
else {
|
||||
PyObject *iterator = PyObject_GetIter(modules);
|
||||
if (iterator == NULL) {
|
||||
PyErr_WriteUnraisable(NULL);
|
||||
PyErr_FormatUnraisable("Exception ignored on removing modules");
|
||||
}
|
||||
else {
|
||||
PyObject *key;
|
||||
while ((key = PyIter_Next(iterator))) {
|
||||
PyObject *value = PyObject_GetItem(modules, key);
|
||||
if (value == NULL) {
|
||||
PyErr_WriteUnraisable(NULL);
|
||||
PyErr_FormatUnraisable("Exception ignored on removing modules");
|
||||
continue;
|
||||
}
|
||||
CLEAR_MODULE(key, value);
|
||||
|
@ -1509,7 +1509,7 @@ finalize_remove_modules(PyObject *modules, int verbose)
|
|||
Py_DECREF(key);
|
||||
}
|
||||
if (PyErr_Occurred()) {
|
||||
PyErr_WriteUnraisable(NULL);
|
||||
PyErr_FormatUnraisable("Exception ignored on removing modules");
|
||||
}
|
||||
Py_DECREF(iterator);
|
||||
}
|
||||
|
@ -1529,7 +1529,7 @@ finalize_clear_modules_dict(PyObject *modules)
|
|||
}
|
||||
else {
|
||||
if (PyObject_CallMethodNoArgs(modules, &_Py_ID(clear)) == NULL) {
|
||||
PyErr_WriteUnraisable(NULL);
|
||||
PyErr_FormatUnraisable("Exception ignored on clearing sys.modules");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1541,11 +1541,11 @@ finalize_restore_builtins(PyThreadState *tstate)
|
|||
PyInterpreterState *interp = tstate->interp;
|
||||
PyObject *dict = PyDict_Copy(interp->builtins);
|
||||
if (dict == NULL) {
|
||||
PyErr_WriteUnraisable(NULL);
|
||||
PyErr_FormatUnraisable("Exception ignored on restoring builtins");
|
||||
}
|
||||
PyDict_Clear(interp->builtins);
|
||||
if (PyDict_Update(interp->builtins, interp->builtins_copy)) {
|
||||
PyErr_WriteUnraisable(NULL);
|
||||
PyErr_FormatUnraisable("Exception ignored on restoring builtins");
|
||||
}
|
||||
Py_XDECREF(dict);
|
||||
}
|
||||
|
@ -1707,7 +1707,7 @@ flush_std_files(void)
|
|||
|
||||
if (fout != NULL && fout != Py_None && !file_is_closed(fout)) {
|
||||
if (_PyFile_Flush(fout) < 0) {
|
||||
PyErr_WriteUnraisable(fout);
|
||||
PyErr_FormatUnraisable("Exception ignored on flushing sys.stdout");
|
||||
status = -1;
|
||||
}
|
||||
}
|
||||
|
@ -3037,14 +3037,14 @@ wait_for_thread_shutdown(PyThreadState *tstate)
|
|||
PyObject *threading = PyImport_GetModule(&_Py_ID(threading));
|
||||
if (threading == NULL) {
|
||||
if (_PyErr_Occurred(tstate)) {
|
||||
PyErr_WriteUnraisable(NULL);
|
||||
PyErr_FormatUnraisable("Exception ignored on threading shutdown");
|
||||
}
|
||||
/* else: threading not imported */
|
||||
return;
|
||||
}
|
||||
result = PyObject_CallMethodNoArgs(threading, &_Py_ID(_shutdown));
|
||||
if (result == NULL) {
|
||||
PyErr_WriteUnraisable(threading);
|
||||
PyErr_FormatUnraisable("Exception ignored on threading shutdown");
|
||||
}
|
||||
else {
|
||||
Py_DECREF(result);
|
||||
|
|
|
@ -650,7 +650,7 @@ _PyErr_PrintEx(PyThreadState *tstate, int set_sys_last_vars)
|
|||
PyErr_Clear();
|
||||
goto done;
|
||||
}
|
||||
_PyErr_WriteUnraisableMsg("in audit hook", NULL);
|
||||
PyErr_FormatUnraisable("Exception ignored in audit hook");
|
||||
}
|
||||
if (hook) {
|
||||
PyObject* args[3] = {typ, exc, tb};
|
||||
|
@ -1093,7 +1093,8 @@ fallback:
|
|||
_PyRuntime.signals.unhandled_keyboard_interrupt = unhandled_keyboard_interrupt;
|
||||
#ifdef Py_DEBUG
|
||||
if (PyErr_Occurred()) {
|
||||
_PyErr_WriteUnraisableMsg("in the internal traceback machinery", NULL);
|
||||
PyErr_FormatUnraisable(
|
||||
"Exception ignored in the internal traceback machinery");
|
||||
}
|
||||
#endif
|
||||
PyErr_Clear();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue