[IronPython] Debugging IronPython scripts + simulate a filename when loading a string

David DiCato ddicato at microsoft.com
Fri Jun 5 14:53:08 PDT 2009


Hi Patrick,

I'm in the process of looking into this for you. As you mentioned, the traceback module (e.g. traceback.print_exc()) is the way to go, and it should work in 2.6. Which version of IronPython are you using?

As a temporary workaround, you can use the sys module to get most of the same functionality; traceback.py is essentially just a pretty-printer for the information provided by sys.exc_info. For example, to print the stack trace to stderr, you can mimic the interpreter:

import sys
try:
    <bugged code>
except:
    sys.excepthook(*sys.except_info())

To load a string as its own module, it is necessary to mimic the behavior of the importer, except that the code is already in memory. You'll want something akin to the following helper function:

import imp
def import_string(modulename, filename, source):
    code_obj = compile(source, filename, 'exec')
    scope = imp.new_module(modulename)
    exec(code_obj, scope.__dict__)
    return scope

This should provide the proper filename/lineno debugging information, for example:

>>> my_module = import_string('my_module ', '<my_package.zip>\\my_module.py ', '\n\n\ndef err():\n  raise SystemError\n\n')
>>> my_module.err()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<my_package.zip>\my_module.py ", line 5, in err
SystemError

Best of luck!

- David

From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of Patrick Deschênes
Sent: Wednesday, June 03, 2009 10:59 AM
To: users at lists.ironpython.com
Subject: [IronPython] Debugging IronPython scripts + simulate a filename when loading a string

Hi,

I'm currently using IronPython to develop extensions to my program.  My python scripts are compressed in a zip file along with other resources.

Everything works fine but I would be very happy if I could find a simple way to debug my scripts.  I don't want anything complex for now.  If I could only do something like this :

try:
   bugged code
except:
   print debug.stack.filename + " : " debug.stack.lineno

It would be perfect.  I looked for the traceback module but it doesn't seems to work with IronPython.  Also, since I load my script from a zip file, is there a way to
simulate a filename when loading a string.

So, if I could :

a) load a string as if it was a file
b) intercept the filename/linenum in the catch block

That would be a huge improvement.


- Patrick
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ironpython.com/pipermail/users-ironpython.com/attachments/20090605/600529b9/attachment.htm>


More information about the Users mailing list