Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'value,': 0.04; 'handler': 0.05; 'interpreter': 0.05; 'attribute': 0.07; 'subject:skip:s 10': 0.07; 'sys': 0.07; 'variables': 0.07; '%s"': 0.09; 'bug.': 0.09; 'msg': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'def': 0.12; 'suggest': 0.14; 'executed.': 0.16; 'finalization': 0.16; "handler')": 0.16; 'handler.': 0.16; 'logger': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'stderr': 0.16; 'exception': 0.16; 'variable': 0.18; 'starts': 0.20; 'code,': 0.22; 'import': 0.22; 'print': 0.22; 'install': 0.23; 'header:User-Agent:1': 0.23; 'error': 0.23; 'instance,': 0.24; 'skip:l 30': 0.24; 'tells': 0.24; 'logging': 0.26; 'skip:" 20': 0.27; 'header:X-Complaints-To:1': 0.27; 'sets': 0.30; 'code': 0.31; "d'aprano": 0.31; 'facility': 0.31; 'steven': 0.31; 'writes:': 0.31; 'file': 0.32; 'skip:m 30': 0.32; 'run': 0.32; '(most': 0.33; 'not.': 0.33; 'info': 0.35; 'but': 0.35; 'there': 0.35; 'charset:us-ascii': 0.36; 'skip:[ 10': 0.38; 'to:addr:python-list': 0.38; 'recent': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'received:217': 0.63; 'happen': 0.63; 'effectively': 0.66; 'capture': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: dieter Subject: Re: My sys.excepthook dies painfully Date: Thu, 24 Jul 2014 07:36:05 +0200 References: <53cf60d3$0$29897$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Gmane-NNTP-Posting-Host: pd9e09e4b.dip0.t-ipconnect.de User-Agent: Gnus/5.1008 (Gnus v5.10.8) XEmacs/21.4.22 (linux) Cancel-Lock: sha1:4tcA5G4huFa+sfhQpGhKwtgCSyE= X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 53 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1406180180 news.xs4all.nl 2845 [2001:888:2000:d::a6]:45881 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:75124 Steven D'Aprano 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.