Newbie question - Getting current line number in embedded python

Calvelo Daniel dcalvelo at pharion.univ-lille2.fr
Mon Sep 4 06:41:18 EDT 2000


Guido Piasenza <g.piasenza at fidia.it> wrote:
: I am currently evaluating different scripting languages to
: embed in a plc-like application. So far embedded python
: looks very promising, but I have a problem: once I call
: PyEval_EvalCode on a precompiled code object (created through
: PyNode_Compile) I would like to know what line number
: of my source file is being executed (in a different thread,
: of course).

You have the line number in the bytecode:

>>> def initive( arguments ):
...   say = None
...   print "remarks"
... 
>>> initive.func_code.co_code 
'\177\001\000\177\002\000t\000\000}\001\000\177\003\000d\001\000GHd\000\000S'

The '\177' bytecode is LINENO; the two following bytes are the line
number.

: I browsed the source code of the executor and found that
: there is an endless loop (in eval_code2) which extracts
: opcodes and executes them, so I think this would be a
: nice place to wedge some code to extract this information.
: However, I found no way to get the "current line number"
: from the code block. I still have the parse tree and the
: compiled block stored in my application... do I have any
: way to map the offset of the current instruction to the
: node which previously generated it during the compilation
: phase ? Or any other way to extract the current line number ?

If you fiddle with eval_code2, then yes, sure, use the bytecode.

Ciao.
Daniel.

-- Daniel Calvelo Aros
     calvelo at lifl.fr



More information about the Python-list mailing list