[issue11705] sys.excepthook doesn't work in imported modules

2015-03-08 Thread Claudiu Popa

Changes by Claudiu Popa pcmantic...@gmail.com:


--
resolution:  - not a bug
stage:  - resolved
status: open - closed

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue11705
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue11705] sys.excepthook doesn't work in imported modules

2011-04-01 Thread Éric Araujo

Changes by Éric Araujo mer...@netwok.org:


--
nosy: +Valery.Lesin, amaury.forgeotdarc, benjamin.peterson, haypo, pitrou

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue11705
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue11705] sys.excepthook doesn't work in imported modules

2011-04-01 Thread STINNER Victor

STINNER Victor victor.stin...@haypocalc.com added the comment:

python -c import loggingTest calls PyRun_SimpleStringFlags(). python 
import_loggingTest.py (import_loggingTest.py just contains import 
loggingTest) calls PyRun_SimpleFileExFlags(). Both functions calls 
PyErr_Print() on error.

An error occurs (raise Exception in loggingTest.py) while importing the 
module, in PyImport_ExecCodeModuleEx().

The real problem is that the module is cleared because it raised an error. 
Extract of PyImport_ExecCodeModuleEx:

v = PyEval_EvalCode((PyCodeObject *)co, d, d);
if (v == NULL)
goto error;
...
  error:
remove_module(name);

(remove_module() does something like del sys.modules['loggingTest']: because 
there is only once reference, the destructor of the module is called)

--

You can workaround this corner case by keeping a reference to all used objects 
using a closure:

---
def create_closure(logger, print_exception):
def handleException(excType, excValue, traceback):
print_exception(excType, excValue, traceback)
print inside exception handler: logger = %s % logger
logger.error(Uncaught exception, exc_info=(excType, excValue, 
traceback))
return handleException

sys.excepthook = create_closure(logger, tb.print_exception)
---

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue11705
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue11705] sys.excepthook doesn't work in imported modules

2011-03-28 Thread Elias Zamaria

New submission from Elias Zamaria mikez...@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 rep...@bugs.python.org
http://bugs.python.org/issue11705
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue11705] sys.excepthook doesn't work in imported modules

2011-03-28 Thread Andreas Stührk

Andreas Stührk andy-pyt...@hammerhartes.de added the comment:

I really doubt that this issue has anything to do with `sys.excepthook` at all. 
It's rather that module members are set to `None` during module cleanup. 
Because the module raises an exception when one tries to import it, it will be 
cleaned up immediately (see `PyImport_ExecCodeModuleEx`: it calls 
`remove_module` which will call at some point `_PyModule_Clear`). At the time 
when `sys.excepthook` is called, all module members are already set to `None`.

One can easily demonstrate that this is really the problem by removing the 
exception raise in your example code: If one then imports loggingTest in an 
interactive session and raises an exception, the expected behaviour can be 
observed.

Another workaround is to bind `logger` as a default argument (i.e. ``def 
handleException(excType, excValue, traceback, logger=logger):``).

--
nosy: +Trundle

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue11705
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue11705] sys.excepthook doesn't work in imported modules

2011-03-28 Thread Elias Zamaria

Elias Zamaria mikez...@gmail.com added the comment:

Andreas, your explanation seems plausible. What has changed between 2.7 and 
2.7.1 such that doing the same thing produces different results?

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue11705
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue11705] sys.excepthook doesn't work in imported modules

2011-03-28 Thread Andreas Stührk

Andreas Stührk andy-pyt...@hammerhartes.de added the comment:

The difference between 2.7 and 2.7.1 is issue #10068 (e2aa3b1d08bc).

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue11705
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue11705] sys.excepthook doesn't work in imported modules

2011-03-28 Thread Elias Zamaria

Elias Zamaria mikez...@gmail.com added the comment:

Is my issue a bug or is it expected behavior?

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue11705
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue11705] sys.excepthook doesn't work in imported modules

2011-03-28 Thread Andreas Stührk

Andreas Stührk andy-pyt...@hammerhartes.de added the comment:

In my opinion, it is expected behaviour (but then, I'm not a core developer).

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue11705
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com