Python equivalent of __LINE__ macro?

Jason Orendorff jason at jorendorff.com
Mon Jan 7 15:59:28 EST 2002


> I'm writing a set of Python scripts to be integrated
> with Developer Studio as part of a custom build.  If
> the script encounters an error, I'd like to be able to
> print the line number the error happened, so that
> DevStoo users can hit F4 and be taken to the offending
> script line.
> 
> Is there an easy way to do this?  The handiest thing
> would be to have a variable like __LINE__ hanging
> around, but in a pinch I suppose we could just use the
> traceback and trap all exceptions....  Though I wonder
> if there isn't an easier way.

You want inspect.trace()!

import inspect

try:
    ... your code here ...
except:
    f, file, lineno, fn_name, lines, n = inspect.trace()[-1]
    print "*** failed in %s at line %i" % (file, lineno)


I'm curious how __LINE__ would have solved your problem,
though, if you were writing all this in C/C++.

## Jason Orendorff    http://www.jorendorff.com/




More information about the Python-list mailing list