<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
I'm glad it worked out, but I'm confused by your code.&nbsp; I think all you
need to do is take the set of linenumbers returned by
dis.findlinestarts.&nbsp; Why do you need to walk the opcodes?&nbsp; I think this
should work for your disassemble function:<br>
<br>
<tt>def disassemble(co):<br>
&nbsp;&nbsp;&nbsp; """Disassemble a code object and return line numbers."""<br>
&nbsp;&nbsp;&nbsp; code = co.co_code<br>
&nbsp;&nbsp;&nbsp; lines = set(l for (o, l) in dis.findlinestarts(co))<br>
&nbsp;&nbsp;&nbsp; for const in co.co_consts:<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if type(const) == types.CodeType:<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; lines.update(disassemble(const))<br>
&nbsp;&nbsp;&nbsp; return lines<br>
</tt><br>
It returns the same values as your code in the files I've run it on.
(And maybe name it something other than disassemble! :-) ).<br>
<br>
--Ned.<br>
<a class="moz-txt-link-freetext" href="http://nedbatchelder.com">http://nedbatchelder.com</a><br>
<br>
Ondrej Certik wrote:
<blockquote
 cite="mid:85b5c3130806240413t558d91bbo39a04f14c6f6c3de@mail.gmail.com"
 type="cite">
  <pre wrap="">On Tue, Jun 24, 2008 at 8:32 AM, Ondrej Certik <a class="moz-txt-link-rfc2396E" href="mailto:ondrej@certik.cz">&lt;ondrej@certik.cz&gt;</a> wrote:
  </pre>
  <blockquote type="cite">
    <pre wrap="">On Tue, Jun 24, 2008 at 4:26 AM, C. Titus Brown <a class="moz-txt-link-rfc2396E" href="mailto:ctb@msu.edu">&lt;ctb@msu.edu&gt;</a> wrote:
    </pre>
    <blockquote type="cite">
      <pre wrap="">On Mon, Jun 23, 2008 at 09:37:41PM -0400, Ned Batchelder wrote:
-&gt; I've been down these paths too, and I completely understand Titus' wariness.
-&gt;
-&gt; I think the stdlib module trace.py has the right idea: rather than
-&gt; analyze the code at all, simply compile it, and look at the table of
-&gt; line numbers in the code object.  This is by definition the set of lines
-&gt; that sys.settrace can return, and is very simple to read.  It has the
-&gt; advantage that as the compiler changes from version to version, changing
-&gt; 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!

-&gt; For more exotic ideas along these lines, you can lie about line numbers:
-&gt; <a class="moz-txt-link-freetext" href="http://nedbatchelder.com/blog/200804/wicked_hack_python_bytecode_tracing.html">http://nedbatchelder.com/blog/200804/wicked_hack_python_bytecode_tracing.html</a>

Ummm... :)
      </pre>
    </blockquote>
    <pre wrap="">Wow, I didn't know that, thanks Ned!

I was just playing with:

<a class="moz-txt-link-freetext" href="http://docs.python.org/lib/module-dis.html">http://docs.python.org/lib/module-dis.html</a>

and it really works and produce the correct line numbers. :)

So now we just need to adapt your line hack example from:

<a class="moz-txt-link-freetext" href="http://nedbatchelder.com/blog/200804/wicked_hack_python_bytecode_tracing.html">http://nedbatchelder.com/blog/200804/wicked_hack_python_bytecode_tracing.html</a>

and that should be it.

I am also actually flying soon, but I'll try to hack on it in the plane.
    </pre>
  </blockquote>
  <pre wrap=""><!---->
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
  </pre>
</blockquote>
<br>
<pre class="moz-signature" cols="72">-- 
Ned Batchelder, <a class="moz-txt-link-freetext" href="http://nedbatchelder.com">http://nedbatchelder.com</a>
</pre>
</body>
</html>