My sys.excepthook dies painfully

Steven D'Aprano steve at
Thu Jul 24 12:12:15 CEST 2014

On Thu, 24 Jul 2014 11:50:47 +1000, Chris Angelico wrote:

> On Thu, Jul 24, 2014 at 11:30 AM, Steven D'Aprano
> <steve+comp.lang.python at> wrote:
>> 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.
> In other words, the problem changed when you added the NameError trigger
> at the bottom of the script?

Not quite. The problem changed when I reduced the code from the real code 
(about a dozen modules) down to the short sample I've given. Except 
that's not quite either -- even with the original code, I wasn't 
originally getting the double traceback either.

I've just stuck some print statements inside the exception handler, and 
just before the "foo":

print 'sys, mylogger', sys, mylogger

They have their expected values just before "foo", but inside the 
excepthook function they are both None.

> Would it be possible to snapshot all critical globals with a closure, to
> force them to be held? Something like:

Probably. Or even as default argument parameters. But I'd like to know if 
that's actually fixing it or just perturbing the system enough that the 
bug won't show up until next time the moon is full.


More information about the Python-list mailing list