[TIP] figleaf tests
ned at nedbatchelder.com
Tue Jun 24 04:42:57 PDT 2008
I'm glad it worked out, but I'm confused by your code. I think all you
need to do is take the set of linenumbers returned by
dis.findlinestarts. Why do you need to walk the opcodes? I think this
should work for your disassemble function:
"""Disassemble a code object and return line numbers."""
code = co.co_code
lines = set(l for (o, l) in dis.findlinestarts(co))
for const in co.co_consts:
if type(const) == types.CodeType:
It returns the same values as your code in the files I've run it on.
(And maybe name it something other than disassemble! :-) ).
Ondrej Certik wrote:
> 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:
>> and it really works and produce the correct line numbers. :)
>> So now we just need to adapt your line hack example from:
>> 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.
Ned Batchelder, http://nedbatchelder.com
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the testing-in-python