Getting better traceback info on exec and execfile - introspection?

Fernando Perez at
Sat Jan 14 23:35:20 EST 2006

R. Bernstein wrote:

> In doing the extension to the python debugger which I have here:
> I came across one little thing that it would be nice to get done better.
> I notice on stack traces and tracebacks, an exec or execfile command
> appears as a stack entry -- probably as it should since it creates a
> new environment.
> However the frame information for exec or execfile looks like this:
>   File "<string>", line 1, in ?

That comes from how the code object was compiled:

>>> code = compile('error','Anything you want goes here','single')
>>> exec code
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "Anything you want goes here", line 1, in ?
NameError: name 'error' is not defined

For example, in ipython:

In [1]: error
exceptions.NameError                                 Traceback (most recent
call last)

/home/fperez/<ipython console>

NameError: name 'error' is not defined

So any regexp-matching based approach here is likely to be fairly brittle,
unless you restrict your tool to the standard python interpreter, and you
get some guarantee that it will always tag interactive code with



More information about the Python-list mailing list