LoudNPossiblyWrong
LoudNPossiblyWrong

Reputation: 3893

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 is running ie. i do not wish the script to terminate in order to print the exception.

Is this even possible?

Upvotes: 4

Views: 1500

Answers (2)

Mattias Nilsson
Mattias Nilsson

Reputation: 3757

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.

Upvotes: 0

ChristopheD
ChristopheD

Reputation: 116267

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

Upvotes: 1

Related Questions