How to print script line number in IronPython?

I am running an IronPython script inside a C # application, I am catching exceptions in a script, and I want to know the script line in which the exception is thrown. This needs to be done while the script is running, i.e. I do not want the script to exit to print the exception.

Is it possible?

+4
source share
2 answers

If inspect works as expected in IronPython (not quite sure), this might do the trick:

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

Edit : alternative solution:

 import sys frame = sys._getframe() print frame.f_lineno 
+1
source

Did not try 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 thrown. You can also do other things, and not just print, for example, print up to a line or receive stack frames. The traceback module documentation will tell you more.

0
source

Source: https://habr.com/ru/post/1314484/


All Articles