Improper Backtraces in Exec'd Code

B

Burton Samograd

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.
 
P

Peter Otten

Burton said:
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.

exec code

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1, in f
File "<string>", line 1, in f
File "<string>", line 1, in f
File "<string>", line 1, in f
ZeroDivisionError: integer division or modulo by zero

If you make the compilation step explicit you can pass a filename:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "yadda.py", line 1, in f
File "yadda.py", line 1, in f
File "yadda.py", line 1, in f
File "yadda.py", line 1, in f
ZeroDivisionError: integer division or modulo by zero

Of course the offending line isn't quoted because there is no file
"yadda.py" on my harddisk. But I can change that:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "yadda.py", line 1, in f
I was fooled!
File "yadda.py", line 1, in f
I was fooled!
File "yadda.py", line 1, in f
I was fooled!
File "yadda.py", line 1, in f
I was fooled!
ZeroDivisionError: integer division or modulo by zero

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top