This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author vstinner
Recipients Trundle, Valery.Lesin, amaury.forgeotdarc, benjamin.peterson, elias, pitrou, vstinner
Date 2011-04-01.17:28:32
SpamBayes Score 8.101104e-06
Marked as misclassified No
Message-id <1301678913.02.0.245587773146.issue11705@psf.upfronthosting.co.za>
In-reply-to
Content
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)
-----------
History
Date User Action Args
2011-04-01 17:28:33vstinnersetrecipients: + vstinner, amaury.forgeotdarc, pitrou, benjamin.peterson, Trundle, Valery.Lesin, elias
2011-04-01 17:28:33vstinnersetmessageid: <1301678913.02.0.245587773146.issue11705@psf.upfronthosting.co.za>
2011-04-01 17:28:32vstinnerlinkissue11705 messages
2011-04-01 17:28:32vstinnercreate