Getting better traceback info on exec and execfile - introspection?

Fernando Perez fperez.net at gmail.com
Sun Jan 15 05:35:20 CET 2006


R. Bernstein wrote:

> In doing the extension to the python debugger which I have here:
>  
http://sourceforge.net/project/showfiles.php?group_id=61395&package_id=175827
> 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
'<string>'.

Regards,

f




More information about the Python-list mailing list