Line numbers outside exceptions

Calvelo Daniel dcalvelo at pharion.univ-lille2.fr
Tue Jul 18 13:33:53 EDT 2000


Ivan Van Laningham <ivanlan at home.com> wrote:
: Hi All--

Hola.

: Is there a standard way, not too horrific, to retrieve the current line
: number in a script at run-time outside of an exception context?

"Not too horrific" I would say no. You have "somewhat hacky" ways of 
retrieving the line number from the bytecode of function spam:

Python 1.5.2 (#0, Apr  3 2000, 14:46:48)  [GCC 2.95.2 20000313 
(Debian GNU/Linux)] on linux2
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> def spam(n="eggs"):
...   print n
...   return n
... 
>>> spam.func_code.co_code 
'\177\001\000\177\002\000|\000\000GH\177\003\000|\000\000Sd\000\000S'

In that last string, the two bytes (this may be platform-specific) after
a '\177' are line numbers. See 'dis'.

"At run-time"? You mean for the current line being executed? Really really 
dunno...

: Metta,
: Ivan

HTH

Daniel.

-- Daniel Calvelo Aros
     calvelo at lifl.fr



More information about the Python-list mailing list