@@ -65,7 +65,7 @@ atexit_cleanup(struct atexit_state *state)
6565/* Installed into pylifecycle.c's atexit mechanism */
6666
6767static void
68- atexit_callfuncs (PyObject * module )
68+ atexit_callfuncs (PyObject * module , int ignore_exc )
6969{
7070 assert (!PyErr_Occurred ());
7171
@@ -87,18 +87,23 @@ atexit_callfuncs(PyObject *module)
8787
8888 PyObject * res = PyObject_Call (cb -> func , cb -> args , cb -> kwargs );
8989 if (res == NULL ) {
90- /* Maintain the last exception, but don't leak if there are
91- multiple exceptions. */
92- if (exc_type ) {
93- Py_DECREF (exc_type );
94- Py_XDECREF (exc_value );
95- Py_XDECREF (exc_tb );
90+ if (ignore_exc ) {
91+ _PyErr_WriteUnraisableMsg ("in atexit callback" , cb -> func );
9692 }
97- PyErr_Fetch (& exc_type , & exc_value , & exc_tb );
98- if (!PyErr_GivenExceptionMatches (exc_type , PyExc_SystemExit )) {
99- PySys_WriteStderr ("Error in atexit._run_exitfuncs:\n" );
100- PyErr_NormalizeException (& exc_type , & exc_value , & exc_tb );
101- PyErr_Display (exc_type , exc_value , exc_tb );
93+ else {
94+ /* Maintain the last exception, but don't leak if there are
95+ multiple exceptions. */
96+ if (exc_type ) {
97+ Py_DECREF (exc_type );
98+ Py_XDECREF (exc_value );
99+ Py_XDECREF (exc_tb );
100+ }
101+ PyErr_Fetch (& exc_type , & exc_value , & exc_tb );
102+ if (!PyErr_GivenExceptionMatches (exc_type , PyExc_SystemExit )) {
103+ PySys_WriteStderr ("Error in atexit._run_exitfuncs:\n" );
104+ PyErr_NormalizeException (& exc_type , & exc_value , & exc_tb );
105+ PyErr_Display (exc_type , exc_value , exc_tb );
106+ }
102107 }
103108 }
104109 else {
@@ -108,11 +113,24 @@ atexit_callfuncs(PyObject *module)
108113
109114 atexit_cleanup (state );
110115
111- if (exc_type ) {
112- PyErr_Restore (exc_type , exc_value , exc_tb );
116+ if (ignore_exc ) {
117+ assert (!PyErr_Occurred ());
118+ }
119+ else {
120+ if (exc_type ) {
121+ PyErr_Restore (exc_type , exc_value , exc_tb );
122+ }
113123 }
114124}
115125
126+
127+ void
128+ _PyAtExit_Call (PyObject * module )
129+ {
130+ atexit_callfuncs (module , 1 );
131+ }
132+
133+
116134/* ===================================================================== */
117135/* Module methods. */
118136
@@ -180,7 +198,7 @@ Run all registered exit functions.");
180198static PyObject *
181199atexit_run_exitfuncs (PyObject * module , PyObject * unused )
182200{
183- atexit_callfuncs (module );
201+ atexit_callfuncs (module , 0 );
184202 if (PyErr_Occurred ()) {
185203 return NULL ;
186204 }
@@ -308,9 +326,8 @@ atexit_exec(PyObject *module)
308326 return -1 ;
309327 }
310328
311- PyInterpreterState * is = _PyInterpreterState_GET ();
312- is -> atexit_func = atexit_callfuncs ;
313- is -> atexit_module = module ;
329+ PyInterpreterState * interp = _PyInterpreterState_GET ();
330+ interp -> atexit_module = module ;
314331 return 0 ;
315332}
316333
0 commit comments