开发者

How do i print the script line number in IronPython?

I am running an IronPython script inside a c# application, i am catching exceptions within the script and i wish to find out the script line at which the exception is thrown. This has to be done while the script开发者_如何学Python is running ie. i do not wish the script to terminate in order to print the exception.

Is this even possible?


If inspect is working as expected under IronPython (not really sure) this could do the trick:

import inspect

filename, linenum, funcname = inspect.getframeinfo(inspect.currentframe())[:3]
print linenum

Edit: alternate solution:

import sys

frame = sys._getframe()
print frame.f_lineno


Haven't tried this on ironpython but:

import traceback

try:
    # something that raises exception
except YourException, _:
    traceback.print_exc()

This should show you the stack trace of the place where the exception was raised. You can also do other stuff than just print, like print to string, or get the stack frames. The traceback module documentation will tell you more.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜