Improper Backtraces in Exec'd Code

Burton Samograd burton at userful.com
Thu Jul 22 12:12:51 EDT 2010


Hello,

I have written an importing extension along the lines of PEP 302
(http://www.python.org/dev/peps/pep-0302/) and have run into a bit of a
problem with stack backtraces after exceptions.

When I run code with the using my importing extension, backtraces come
up looking like this:

Traceback (most recent call last):
  File "<string>", line 134, in <module>
  File "<string>", line 9, in <module>
  File "<string>", line 7, in a
  File "<string>", line 4, in b
TypeError

What I'm having a problem with is that the file names are no longer
being printed during the backtrace.  The following code is from my
importer function load_module:

       try:
            mod = sys.modules[module_name]
            already_in_sys_modules = True
        except KeyError:
            mod = sys.modules.setdefault(module_name, 
                                         imp.new_module(module_name))
            already_in_sys_modules = False
        mod.__file__ = "%s" % module_path
        mod.__loader__ = self
        if is_package:
            mod.__path__ = [ module_path ]
        mod.__dict__['__file__'] = module_path
        try:
            exec code in mod.__dict__
        except:
            import traceback
            print traceback.format_exc(5)
            print "ERROR: could not load module: %s" % module_path
            if not already_in_sys_modules:
                del sys.modules[module_name]
                mod = None
 
As shown in PEP 302, I am setting the mod.__file__ variable and as my
own idea I set __file__ in mod.__dict__.  Niether of these steps seem to
set the file properly in the backtrace.

So the question here is, where does the backtrace printing module get
it's __file__ parameters from, or what am I missing to set it properly?

Thanks in advance.

--
Burton Samograd




More information about the Python-list mailing list