开发者

How do i print the script line number in IronPython?

开发者 https://www.devze.com 2023-01-05 22:48 出处:网络
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

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

精彩评论

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