[issue11705] sys.excepthook doesn't work in imported modules
Elias Zamaria
report at bugs.python.org
Tue Mar 29 00:29:16 CEST 2011
New submission from Elias Zamaria <mikez302 at gmail.com>:
I am trying to design a Python program that logs all uncaught exceptions using the logging module. I am doing this by using the sys.excepthook function to override the default exception handling. I noticed that if I run the program directly from the command line, it works fine, but if I try to import the file, it doesn't work. It seems like the sys.excepthook function is unaware of the logging module. Here is an example:
#! /usr/bin/env python2.7
import logging, sys
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
logger.addHandler(logging.FileHandler("test.log"))
print "outside of exception handler: logger = %s" % logger
def handleException(excType, excValue, traceback):
#global logger # this function doesn't work whether or not I include this line
print "inside exception handler: logger = %s" % logger
logger.error("Uncaught exception", exc_info=(excType, excValue, traceback))
sys.excepthook = handleException
logger.debug("starting")
asdf # create an exception
If I run this from the command line (`./loggingTest.py`), it works fine. The exception gets logged, and I see this output:
outside of exception handler: logger = <logging.RootLogger object at 0x7f2022eab950>
inside exception handler: logger = <logging.RootLogger object at 0x7f2022eab950>
However, if I run the Python interpreter and try to import the file (`import loggingTest`), it acts strangely. The exception doesn't get logged and I see this:
outside of exception handler: logger = <logging.RootLogger object at 0x7f8ab04f3ad0>
inside exception handler: logger = None
Error in sys.excepthook:
Traceback (most recent call last):
File "loggingTest.py", line 13, in handleException
logger.error("Uncaught exception", exc_info=(excType, excValue, traceback))
AttributeError: 'NoneType' object has no attribute 'error'
Original exception was:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "loggingTest.py", line 18, in <module>
asdf # create an exception
NameError: name 'asdf' is not defined
I can maybe work around this problem by importing the logging module again within sys.excepthook, but I am still curious: why is this happening?
The above text was copied from my question on Stack Overflow (http://stackoverflow.com/questions/5451746/sys-excepthook-doesnt-work-in-imported-modules). Someone on there (Jochen Ritzel) has mentioned that he noticed this bug in Python 2.7.1 but not 2.7. To me, it seems like the sys.excepthook function is unaware of any imported modules, unless those modules are imported from inside the function.
----------
components: Library (Lib)
files: loggingTest.py
messages: 132437
nosy: mikez302
priority: normal
severity: normal
status: open
title: sys.excepthook doesn't work in imported modules
type: behavior
versions: Python 2.7
Added file: http://bugs.python.org/file21446/loggingTest.py
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue11705>
_______________________________________
More information about the Python-bugs-list
mailing list