Python equivt of __FILE__ and __LINE__

Bill Davy Bill at SynectixLtd.com
Tue Feb 12 07:06:05 EST 2008


"thebjorn" <BjornSteinarFjeldPettersen at gmail.com> wrote in message 
news:a9946b83-22f8-4da5-8ae4-fed8c367ff90 at i7g2000prf.googlegroups.com...
> On Feb 11, 4:55 pm, Gary Herron <gher... at islandtraining.com> wrote:
>> Bill Davy wrote:
>> > Writing a quick and dirty assembler and want to give the user the 
>> > location
>> > of an error.  The "assembly language" is Python.  If the user wants to
>> > generat some object code they write something  like:
>>
>> > Label(LoopLable)
>> >     Load(R4)
>> >     Dec()
>> >     JNZ(LoopLabel)
>>
>> > I can use Python to do all the expression evalutaion, conversion from 
>> > Python
>> > FP to target FP, include files, macros (done as function definitions). 
>> > The
>> > functions like Load() generate the approproyte object code.
>>
>> > So, for example, when a label is defined or referenced, I save the 
>> > File,Line
>> > so if there is not exactly one defintion or no references, I can report 
>> > the
>> > file location(s) to be considered.  In the example, I would want to 
>> > report
>> > that LoopLable is not referenced, and LoopLabel is not defined.
>>
>> > TIA,
>> >     Bill
>>
>> >
>>
>> You *can* get at that kind of information: The traceback module has a
>> function called "extract_stack" which can give you a pointer to the
>> whole execution stack.  From that can be generated all the usual stuff
>> you see in a traceback -- including file and line information.  *How*
>> you extract that stuff, I'll leave as an exercises for the reader.
>> (Meaning I haven't a clue.)
>>
>> Gary Herron
>
> I think the inspect module might be more useful... the getfile() and
> getsourcelines() look promising.
>
> -- bjorn


I think I'll go with the tarceback route because if the user defines 
functions to emit code, I can traceback silently from where the error is 
found to the call to Load() or Store() etc, and then trace back visibly, the 
user will get a traceback of their code (and not my implementation details).

But very interesting and nice to know about these functions/modules.  What a 
lovely langauge.

Bill

PS www.SynectixLtd.com is not relevant 





More information about the Python-list mailing list