[TIP] figleaf tests

Ondrej Certik ondrej at certik.cz
Tue Jun 24 04:13:23 PDT 2008


On Tue, Jun 24, 2008 at 8:32 AM, Ondrej Certik <ondrej at certik.cz> wrote:
> On Tue, Jun 24, 2008 at 4:26 AM, C. Titus Brown <ctb at msu.edu> wrote:
>> On Mon, Jun 23, 2008 at 09:37:41PM -0400, Ned Batchelder wrote:
>> -> I've been down these paths too, and I completely understand Titus' wariness.
>> ->
>> -> I think the stdlib module trace.py has the right idea: rather than
>> -> analyze the code at all, simply compile it, and look at the table of
>> -> line numbers in the code object.  This is by definition the set of lines
>> -> that sys.settrace can return, and is very simple to read.  It has the
>> -> advantage that as the compiler changes from version to version, changing
>> -> the lines that can be returned, trace.py is immune to the changes.
>>
>> Hmm, I didn't realize that the table of line numbers in the code object
>> is the set of lines that can be returned by sys.settrace!  That makes my
>> life *much* easier, thanks!
>>
>> -> For more exotic ideas along these lines, you can lie about line numbers:
>> -> http://nedbatchelder.com/blog/200804/wicked_hack_python_bytecode_tracing.html
>>
>> Ummm... :)
>
> Wow, I didn't know that, thanks Ned!
>
> I was just playing with:
>
> http://docs.python.org/lib/module-dis.html
>
> and it really works and produce the correct line numbers. :)
>
> So now we just need to adapt your line hack example from:
>
> http://nedbatchelder.com/blog/200804/wicked_hack_python_bytecode_tracing.html
>
> and that should be it.
>
> I am also actually flying soon, but I'll try to hack on it in the plane.

Ok, that was extremely easy, I hacked it up in the train from
Neuchatel do Geneva airport. Get the attached script show.py, make it
executable and do:

$ ./show.py t.py
set([1, 2, 4, 5, 9, 10, 12, 14, 15])
$ ./show.py show.py
set([3, 5, 6, 7, 8, 10, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 23,
24, 25, 26, 27, 28, 29, 31])


The t.py was the example I posted in my original email, so as you can
see, the line numbers are correct. The line counting algorithm is just
20 lines long and it only needs the "dis" module. I basically stripped
down the dis.disassemble() function.

Now I am going to hack this into figleaf and prepare a patch.

Ondrej
-------------- next part --------------
A non-text attachment was scrubbed...
Name: show.py
Type: text/x-python
Size: 710 bytes
Desc: not available
Url : http://lists.idyll.org/pipermail/testing-in-python/attachments/20080624/670abb6b/attachment-0001.py 


More information about the testing-in-python mailing list