My sys.excepthook dies painfully

Steven D'Aprano steve+comp.lang.python at
Thu Jul 24 03:30:41 CEST 2014

On Wed, 23 Jul 2014 13:02:51 -0700, Jason Swails wrote:

> I'm not sure how the "mylogger" variable is getting set to None in your
> my_error_handler callback, but I don't see how that can possibly be
> happening with the provided code...

Dammit, it's a Heisenbug... now it's gone away for me too.

However, I think I have a glimmer of an idea for how the global variable 
might be set to None. When the Python interpreter shuts down, it sets 
global variables to None in some arbitrary order. If the excepthook 
function isn't called until after the shutdown process begins, then 
depending on the phase of the moon, it's possible that ``mylogger`` may 
have been set to None by the time it is called.

It's quite common for __del__ methods and daemon threads to be called 
during interpreter shutdown, but I've never come across an excepthook 
doing this.

I wonder whether I ought to use atexit to register the function, rather 
than mess with sys.excepthook directly?


More information about the Python-list mailing list