Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #75124

Re: My sys.excepthook dies painfully

From dieter <dieter@handshake.de>
Subject Re: My sys.excepthook dies painfully
Date 2014-07-24 07:36 +0200
References <53cf60d3$0$29897$c3e8da3$5496439d@news.astraweb.com>
Newsgroups comp.lang.python
Message-ID <mailman.12266.1406180180.18130.python-list@python.org> (permalink)

Show all headers | View raw


Steven D'Aprano <steve@pearwood.info> writes:
> I have some code which sets up a logger instance, then installs it as 
> sys.excepthook to capture any uncaught exceptions:
>
>
>
> import logging
> import logging.handlers
> import sys
>
> FACILITY = logging.handlers.SysLogHandler.LOG_LOCAL6
> mylogger = logging.getLogger('spam')
> handler = logging.handlers.SysLogHandler(
>         address='/dev/log', facility=FACILITY)
> formatter = logging.Formatter("%(levelname)s:%(message)s [%(module)s]")
> handler.setFormatter(formatter)
> mylogger.addHandler(handler)
> mylogger.setLevel(logging.DEBUG)
> mylogger.info('started logging')
>
> def my_error_handler(type, value, tb):
>     msg = "Uncaught %s: %s" % (type, value)
>     mylogger.exception(msg)
>     sys.__excepthook__(type, value, tb)  # print the traceback to stderr
>
> # Install exception handler.
> mylogger.info('installing error handler')
> sys.excepthook = my_error_handler
>
> foo  # Die with uncaught NameError.
>
>
>
> If I run this code, the INFO logging messages are logged, but the 
> exception is not. Instead it is printed to the console:
>
>
> Error in sys.excepthook:
> Traceback (most recent call last):
>   File "/home/steve/mylogging.py", line 28, in my_error_handler
>     mylogger.exception(msg)
> AttributeError: 'NoneType' object has no attribute 'exception'

This tells you that "mylogger" is "None".

This can happen during finalization. When the interpreter is shut down,
it unbinds all variables in a complex process (somewhere, there
is a description how it proceeds). Unbinding a variable effectively
means bindiung it to "None".

This would suggest that the finalization starts before the "excepthook"
has been executed. I would consider this a bug.

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

My sys.excepthook dies painfully Steven D'Aprano <steve@pearwood.info> - 2014-07-23 07:14 +0000
  Re: My sys.excepthook dies painfully Chris Angelico <rosuav@gmail.com> - 2014-07-23 17:36 +1000
  Re: My sys.excepthook dies painfully Steven D'Aprano <steve@pearwood.info> - 2014-07-23 07:46 +0000
    Re: My sys.excepthook dies painfully Chris Angelico <rosuav@gmail.com> - 2014-07-23 18:02 +1000
    Re: My sys.excepthook dies painfully Jason Swails <jason.swails@gmail.com> - 2014-07-23 13:02 -0700
      Re: My sys.excepthook dies painfully Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-07-24 01:30 +0000
        Re: My sys.excepthook dies painfully Chris Angelico <rosuav@gmail.com> - 2014-07-24 11:50 +1000
          Re: My sys.excepthook dies painfully Steven D'Aprano <steve@pearwood.info> - 2014-07-24 10:12 +0000
            Re: My sys.excepthook dies painfully Chris Angelico <rosuav@gmail.com> - 2014-07-24 20:20 +1000
        Re: My sys.excepthook dies painfully Steven D'Aprano <steve@pearwood.info> - 2014-07-24 05:51 +0000
        Re: My sys.excepthook dies painfully Jason Swails <jason.swails@gmail.com> - 2014-07-23 23:46 -0700
  Re: My sys.excepthook dies painfully dieter <dieter@handshake.de> - 2014-07-24 07:36 +0200

csiph-web