Better traceback information?
Alex Martelli
aleaxit at yahoo.com
Sun Oct 22 18:18:13 EDT 2000
"Olivier Dagenais" <olivierS.dagenaisP at canadaA.comM> wrote in message
news:wrGI5.370597$1h3.9889167 at news20.bellglobal.com...
> In my app, I exec code and class definitions that I just fished out from a
> database. The problem is, if an exception is thrown from inside one of
> these definitions, the traceback usually gives me '<string>' as the
One way to attach the 'filename' you want to the strings you're
about to exec is to compile them -- e.g.:
Python 2.0 (#8, Oct 16 2000, 17:27:58) [MSC 32 bit (Intel)] on win32
Type "copyright", "credits" or "license" for more information.
IDLE 0.6 -- press F1 for help
>>> astatement="print 1/0"
>>> exec astatement
Traceback (innermost last):
File "<pyshell#1>", line 1, in ?
exec astatement
File "<string>", line 1, in ?
ZeroDivisionError: integer division or modulo
>>> acompiled=compile(astatement, "BeautifulName", "exec")
>>> exec acompiled
Traceback (innermost last):
File "<pyshell#3>", line 1, in ?
exec acompiled
File "BeautifulName", line 1, in ?
ZeroDivisionError: integer division or modulo
>>>
If you're just "fishing them out of the DB", you might consider
keeping there the pickled compiled codeobject, for speed. But
really it's not so bad -- compile+exec of resulting bytecode is
roughly the same amout of work as exec of sourcestring, which
must, after all, internally compile, anyway.
Changing the linenumber seems harder -- something for the
[in]famous bytecodehacks, maybe...?
Alex
More information about the Python-list
mailing list