have been renamed to *exc*.
(Contributed by Zackery Spytz and Matthias Bussonnier in :issue:`26389`.)
+* :mod:`atexit`: At Python exit, if a callback registered with
+ :func:`atexit.register` fails, its exception is now logged. Previously, only
+ some exceptions were logged, and the last exception was always silently
+ ignored.
+ (Contributed by Victor Stinner in :issue:`42639`.)
+
+
CPython bytecode changes
========================
* :mod:`sqlite3` requires SQLite 3.7.3 or higher.
(Contributed by Sergey Fedoseev and Erlend E. Aasland :issue:`40744`.)
+* The :mod:`atexit` module must now always be built as a built-in module.
+ (Contributed by Victor Stinner in :issue:`42639`.)
C API Changes
/* Installed into pylifecycle.c's atexit mechanism */
static void
-atexit_callfuncs(PyObject *module)
+atexit_callfuncs(PyObject *module, int ignore_exc)
{
assert(!PyErr_Occurred());
PyObject *res = PyObject_Call(cb->func, cb->args, cb->kwargs);
if (res == NULL) {
- /* Maintain the last exception, but don't leak if there are
- multiple exceptions. */
- if (exc_type) {
- Py_DECREF(exc_type);
- Py_XDECREF(exc_value);
- Py_XDECREF(exc_tb);
+ if (ignore_exc) {
+ _PyErr_WriteUnraisableMsg("in atexit callback", cb->func);
}
- PyErr_Fetch(&exc_type, &exc_value, &exc_tb);
- if (!PyErr_GivenExceptionMatches(exc_type, PyExc_SystemExit)) {
- PySys_WriteStderr("Error in atexit._run_exitfuncs:\n");
- PyErr_NormalizeException(&exc_type, &exc_value, &exc_tb);
- PyErr_Display(exc_type, exc_value, exc_tb);
+ else {
+ /* Maintain the last exception, but don't leak if there are
+ multiple exceptions. */
+ if (exc_type) {
+ Py_DECREF(exc_type);
+ Py_XDECREF(exc_value);
+ Py_XDECREF(exc_tb);
+ }
+ PyErr_Fetch(&exc_type, &exc_value, &exc_tb);
+ if (!PyErr_GivenExceptionMatches(exc_type, PyExc_SystemExit)) {
+ PySys_WriteStderr("Error in atexit._run_exitfuncs:\n");
+ PyErr_NormalizeException(&exc_type, &exc_value, &exc_tb);
+ PyErr_Display(exc_type, exc_value, exc_tb);
+ }
}
}
else {
atexit_cleanup(state);
- if (exc_type) {
- PyErr_Restore(exc_type, exc_value, exc_tb);
+ if (ignore_exc) {
+ assert(!PyErr_Occurred());
+ }
+ else {
+ if (exc_type) {
+ PyErr_Restore(exc_type, exc_value, exc_tb);
+ }
}
}
+
+void
+_PyAtExit_Call(PyObject *module)
+{
+ atexit_callfuncs(module, 1);
+}
+
+
/* ===================================================================== */
/* Module methods. */
static PyObject *
atexit_run_exitfuncs(PyObject *module, PyObject *unused)
{
- atexit_callfuncs(module);
+ atexit_callfuncs(module, 0);
if (PyErr_Occurred()) {
return NULL;
}
return -1;
}
- PyInterpreterState *is = _PyInterpreterState_GET();
- is->atexit_func = atexit_callfuncs;
- is->atexit_module = module;
+ PyInterpreterState *interp = _PyInterpreterState_GET();
+ interp->atexit_module = module;
return 0;
}
}
}
+
/* Clean up and exit */
static void
call_py_exitfuncs(PyThreadState *tstate)
{
- PyInterpreterState *interp = tstate->interp;
- if (interp->atexit_func == NULL)
- return;
-
- interp->atexit_func(interp->atexit_module);
- _PyErr_Clear(tstate);
+ _PyAtExit_Call(tstate->interp->atexit_module);
}
+
/* Wait until threading._shutdown completes, provided
the threading module was imported in the first place.
The shutdown routine will wait until all non-daemon
# C-optimized pickle replacement
self.add(Extension("_pickle", ["_pickle.c"],
extra_compile_args=['-DPy_BUILD_CORE_MODULE']))
- # atexit
- self.add(Extension("atexit", ["atexitmodule.c"]))
# _json speedups
self.add(Extension("_json", ["_json.c"],
extra_compile_args=['-DPy_BUILD_CORE_MODULE']))