[Tutor] Capturing assert source code line

Daniel Knierim DanK@CapitalStream.com
Thu, 8 Jun 2000 02:35:00 -0700


This message is in MIME format. Since your mail reader does not understand
this format, some or all of this message may not be legible.

------_=_NextPart_001_01BFD12C.D51B0FF0
Content-Type: text/plain;
	charset="iso-8859-1"

Hello folks,
 
I think the easiest way to explain my question is by introducing it with an
example.  I defined this function in file 'fassert.py':
def fassert(v):
    assert v, `v`
--- end of file 'fassert.py'
 
Now in the interpreter:
>>>import fassert
>>>fassert.fassert(0)
Traceback (innermost last):
  File "<stdin>", line 1, in ?
  File "fassert.py", line 2, in fassert
    assert v, `v`
Assertion Error: 0
>>>
--- end of interpreter fragment
 
Note the line "   assert v, `v`" in the interpreter's exception traceback,
showing the source code that hit the assertion.
 
Now back in the interpreter
>>>import sys
>>>try:
...    fassert.fassert(0)
...except AssertionError:
...    exc_info = sys.exc_info()
...
>>>exc_info
(<class exceptions.AssertionError at xxxxxx>, <exceptions.AssertionError
instance at xxxxxx>, <traceback object at xxxxxx>)
>>>exc_info[0].__dict__
{'__module__': 'exceptions', '__doc__': 'Assertion failed.'}
>>>exc_info[1].__dict__
{'args': ('0',)}
>>>tb = exc_info[2]
>>>next_frame = tb.tb_next.tb_frame
>>>next_frame.f_code
<code object fassert at xxxxxx, file "fassert.py", line 1>
>>>next_frame.f_lineno
2
>>>next_frame.f_locals
{'v': 0}
>>>
--- 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). 
 
Thanks
-- Dan K. 

============================ 
No important issue is ever completely resolved.  Welcome to the real world
-- Join the debate.  (Robert Scheer)
 

------_=_NextPart_001_01BFD12C.D51B0FF0
Content-Type: text/html;
	charset="iso-8859-1"

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">


<META content="MSHTML 5.00.2314.1000" name=GENERATOR></HEAD>
<BODY>
<DIV><FONT face=Fixedsys size=2><SPAN class=197223007-08062000>Hello 
folks,</SPAN></FONT></DIV>
<DIV><FONT size=2><SPAN class=197223007-08062000></SPAN></FONT>&nbsp;</DIV>
<DIV><FONT face=Fixedsys size=2><SPAN class=197223007-08062000>I think the 
easiest way to explain my question is by introducing it with an example.&nbsp; I 
defined&nbsp;this function in file 'fassert.py':</SPAN></FONT></DIV>
<DIV><FONT face=Fixedsys size=2><SPAN class=197223007-08062000>def 
fassert(v):<BR>&nbsp;&nbsp;&nbsp; assert v, `v`</SPAN></FONT></DIV>
<DIV><FONT face=Fixedsys size=2><SPAN class=197223007-08062000>--- end of file 
'fassert.py'</SPAN></FONT></DIV>
<DIV><FONT size=2><SPAN class=197223007-08062000></SPAN></FONT>&nbsp;</DIV>
<DIV><FONT face=Fixedsys size=2><SPAN class=197223007-08062000>Now in the 
interpreter:</SPAN></FONT></DIV>
<DIV><FONT face=Fixedsys size=2><SPAN 
class=197223007-08062000>&gt;&gt;&gt;import fassert</SPAN></FONT></DIV>
<DIV><FONT face=Fixedsys size=2><SPAN 
class=197223007-08062000>&gt;&gt;&gt;fassert.fassert(0)</SPAN></FONT></DIV>
<DIV><FONT face=Fixedsys size=2><SPAN class=197223007-08062000>Traceback 
(innermost last):</SPAN></FONT></DIV>
<DIV><FONT face=Fixedsys size=2><SPAN class=197223007-08062000>&nbsp; File 
"&lt;stdin&gt;", line 1, in ?</SPAN></FONT></DIV>
<DIV><FONT face=Fixedsys size=2><SPAN class=197223007-08062000>&nbsp; File 
"fassert.py", line 2, in fassert</SPAN></FONT></DIV>
<DIV><FONT face=Fixedsys size=2><SPAN 
class=197223007-08062000>&nbsp;&nbsp;&nbsp; assert v, `v`</SPAN></FONT></DIV>
<DIV><FONT face=Fixedsys size=2><SPAN class=197223007-08062000>Assertion Error: 
0</SPAN></FONT></DIV>
<DIV><FONT face=Fixedsys size=2><SPAN 
class=197223007-08062000>&gt;&gt;&gt;</SPAN></FONT></DIV>
<DIV><FONT face=Fixedsys size=2><SPAN class=197223007-08062000>--- end of 
interpreter fragment</SPAN></FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=Fixedsys size=2><SPAN class=197223007-08062000>Note the 
line&nbsp;"&nbsp;&nbsp; assert v, `v`" in the interpreter's exception traceback, 
showing the source code that hit the assertion.</SPAN></FONT></DIV>
<DIV><FONT size=2><SPAN class=197223007-08062000></SPAN></FONT>&nbsp;</DIV>
<DIV><FONT face=Fixedsys size=2><SPAN class=197223007-08062000>Now back in the 
interpreter</SPAN></FONT></DIV>
<DIV><FONT face=Fixedsys size=2><SPAN 
class=197223007-08062000>&gt;&gt;&gt;import sys</SPAN></FONT></DIV>
<DIV><FONT face=Fixedsys size=2><SPAN 
class=197223007-08062000>&gt;&gt;&gt;try:</SPAN></FONT></DIV>
<DIV><FONT face=Fixedsys size=2><SPAN 
class=197223007-08062000>...&nbsp;&nbsp;&nbsp; 
fassert.fassert(0)</SPAN></FONT></DIV>
<DIV><FONT face=Fixedsys size=2><SPAN class=197223007-08062000>...except 
AssertionError:</SPAN></FONT></DIV>
<DIV><FONT face=Fixedsys size=2><SPAN 
class=197223007-08062000>...&nbsp;&nbsp;&nbsp; exc_info = 
sys.exc_info()</SPAN></FONT></DIV>
<DIV><FONT face=Fixedsys size=2><SPAN 
class=197223007-08062000>...</SPAN></FONT></DIV>
<DIV><FONT face=Fixedsys size=2><SPAN 
class=197223007-08062000>&gt;&gt;&gt;exc_info</SPAN></FONT></DIV>
<DIV><FONT face=Fixedsys size=2><SPAN class=197223007-08062000>(&lt;class 
exceptions.AssertionError at xxxxxx&gt;, &lt;exceptions.AssertionError instance 
at xxxxxx&gt;, &lt;traceback object at xxxxxx&gt;)</SPAN></FONT></DIV>
<DIV><FONT face=Fixedsys size=2><SPAN 
class=197223007-08062000>&gt;&gt;&gt;exc_info[0].__dict__</SPAN></FONT></DIV>
<DIV><FONT face=Fixedsys size=2><SPAN class=197223007-08062000>{'__module__': 
'exceptions', '__doc__': 'Assertion failed.'}</SPAN></FONT></DIV>
<DIV><FONT face=Fixedsys size=2><SPAN 
class=197223007-08062000>&gt;&gt;&gt;exc_info[1].__dict__</SPAN></FONT></DIV>
<DIV><FONT face=Fixedsys size=2><SPAN class=197223007-08062000>{'args': 
('0',)}</SPAN></FONT></DIV>
<DIV><FONT face=Fixedsys size=2><SPAN class=197223007-08062000>&gt;&gt;&gt;tb = 
exc_info[2]</SPAN></FONT></DIV>
<DIV><FONT face=Fixedsys size=2><SPAN 
class=197223007-08062000>&gt;&gt;&gt;next_frame = 
tb.tb_next.tb_frame</SPAN></FONT></DIV>
<DIV><FONT face=Fixedsys size=2><SPAN 
class=197223007-08062000>&gt;&gt;&gt;next_frame.f_code</SPAN></FONT></DIV>
<DIV><FONT face=Fixedsys size=2><SPAN class=197223007-08062000>&lt;code object 
fassert at xxxxxx, file "fassert.py", line 1&gt;</SPAN></FONT></DIV>
<DIV><FONT face=Fixedsys size=2><SPAN 
class=197223007-08062000>&gt;&gt;&gt;next_frame.f_lineno</SPAN></FONT></DIV>
<DIV><FONT face=Fixedsys size=2><SPAN 
class=197223007-08062000>2</SPAN></FONT></DIV>
<DIV><FONT face=Fixedsys size=2><SPAN 
class=197223007-08062000>&gt;&gt;&gt;next_frame.f_locals</SPAN></FONT></DIV>
<DIV><FONT face=Fixedsys size=2><SPAN class=197223007-08062000>{'v': 
0}</SPAN></FONT></DIV>
<DIV><FONT face=Fixedsys size=2><SPAN 
class=197223007-08062000>&gt;&gt;&gt;</SPAN></FONT></DIV>
<DIV><FONT face=Fixedsys size=2><SPAN class=197223007-08062000>--- end of 
interpreter fragment</SPAN></FONT></DIV>
<DIV><FONT face=Fixedsys size=2><SPAN 
class=197223007-08062000></SPAN></FONT>&nbsp;</DIV>
<DIV><FONT face=Fixedsys size=2><SPAN class=197223007-08062000>So I see how to 
retrieve most of the information the interpreter displays in its exception 
traceback.</SPAN></FONT></DIV>
<DIV><FONT face=Fixedsys size=2><SPAN 
class=197223007-08062000></SPAN></FONT>&nbsp;</DIV>
<DIV><FONT face=Fixedsys size=2><SPAN class=197223007-08062000>Now my question 
is -- how can I capture the source code line that hit the assertion?&nbsp; 
("assert v, `v`" in this example). </SPAN></FONT></DIV>
<DIV><FONT face=Fixedsys size=2><SPAN 
class=197223007-08062000></SPAN></FONT>&nbsp;</DIV>
<DIV><FONT face=Fixedsys size=2><SPAN 
class=197223007-08062000>Thanks</SPAN></FONT></DIV>
<DIV><FONT face=Fixedsys><FONT size=2>-- Dan K.</FONT> </FONT></DIV>
<DIV><FONT face=Fixedsys><BR><FONT size=2>============================</FONT> 
<BR><FONT size=2>No important issue is ever completely 
resolved.&nbsp;&nbsp;<SPAN class=517431910-12052000>Welcome to the real world -- 
</SPAN>Join the debate.&nbsp; (Robert Scheer)</FONT></FONT></DIV>
<DIV>&nbsp;</DIV></BODY></HTML>

------_=_NextPart_001_01BFD12C.D51B0FF0--