exec and traceback

dieter dieter at handshake.de
Tue Jan 23 03:12:25 EST 2018


ken.py at gameofy.com writes:

> I'm using exec() to run a (multi-line) string of python code. If an
> exception occurs, I get a traceback containing a stack frame for the
> string. I've labeled the code object with a "file name" so I can
> identify it easily, and when I debug, I find that I can interact with
> the context of that stack frame, which is pretty handy.
>
> What I would like to also be able to do is make the code string
> visible to the debugger so I can look at and step through the code in
> the string as if it were from a python file.

The debugger may not be prepared for this kind of source; thus, you
might need a much more intelligent debugger.

The "exec" itself works with arbitary strings, not only with string
constants coming from a source file. It has no way to reliably
associate a filename and a line number in the respective file with
the code lines. The debugger (at least currently) expects to locate
the source code in a file (maybe an archive) given a line number in that
file.

Based on this, I expect that your wish will be difficult to fulfill.




More information about the Python-list mailing list