atexit._run_exitfuncs()
self.assertEqual(l, [5])
+ def test_atexit_with_unregistered_function(self):
+ # See bpo-46025 for more info
+ def func():
+ atexit.unregister(func)
+ 1/0
+ atexit.register(func)
+ try:
+ with support.catch_unraisable_exception() as cm:
+ atexit._run_exitfuncs()
+ self.assertEqual(cm.unraisable.object, func)
+ self.assertEqual(cm.unraisable.exc_type, ZeroDivisionError)
+ self.assertEqual(type(cm.unraisable.exc_value), ZeroDivisionError)
+ finally:
+ atexit.unregister(func)
+
if __name__ == "__main__":
unittest.main()
continue;
}
+ // bpo-46025: Increment the refcount of cb->func as the call itself may unregister it
+ PyObject* the_func = Py_NewRef(cb->func);
PyObject *res = PyObject_Call(cb->func, cb->args, cb->kwargs);
if (res == NULL) {
- _PyErr_WriteUnraisableMsg("in atexit callback", cb->func);
+ _PyErr_WriteUnraisableMsg("in atexit callback", the_func);
}
else {
Py_DECREF(res);
}
+ Py_DECREF(the_func);
}
atexit_cleanup(state);