[Tutor] Capturing assert source code line

Remco Gerlich scarblac@pino.selwerd.nl
Thu, 8 Jun 2000 11:52:07 +0200


On Thu, Jun 08, 2000 at 02:35:00AM -0700, Daniel Knierim wrote:
> <code object fassert at xxxxxx, file "fassert.py", line 1>
> >>>next_frame.f_lineno
> 2

> --- end of interpreter fragment
>  
> So I see how to retrieve most of the information the interpreter displays in
> its exception traceback.
>  
> Now my question is -- how can I capture the source code line that hit the
> assertion?  ("assert v, `v`" in this example). 

The interpreter just looks up the line in the file (try it - import fassert
first, then change fassert.py to random noise - the traceback will show the
noise).

So you can reconstruct it with the code object (its co_filename variable)
and the lineno from the frame.

This is also the reason why the interpreter only shows the first line if
your expression spans multiple lines... very annoying.

-- 
Remco Gerlich,  scarblac@pino.selwerd.nl