[TIP] figleaf tests
Ondrej Certik
ondrej at certik.cz
Tue Jun 24 05:42:13 PDT 2008
On Tue, Jun 24, 2008 at 1:59 PM, Ondrej Certik <ondrej at certik.cz> wrote:
> On Tue, Jun 24, 2008 at 1:53 PM, Ondrej Certik <ondrej at certik.cz> wrote:
>> On Tue, Jun 24, 2008 at 1:42 PM, Ned Batchelder <ned at nedbatchelder.com> wrote:
>>> 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:
>>>
>>> def disassemble(co):
>>> """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:
>>> lines.update(disassemble(const))
>>> return lines
Now I just discovered this:
In [1]: import trace
In [2]: trace.find_executable_linenos??
Type: function
Base Class: <type 'function'>
String Form: <function find_executable_linenos at 0xb7c1856c>
Namespace: Interactive
File: /usr/lib/python2.5/trace.py
Definition: trace.find_executable_linenos(filename)
Source:
def find_executable_linenos(filename):
"""Return dict where keys are line numbers in the line number table."""
try:
prog = open(filename, "rU").read()
except IOError, err:
print >> sys.stderr, ("Not printing coverage data for %r: %s"
% (filename, err))
return {}
code = compile(prog, filename, "exec")
strs = find_strings(filename)
return find_lines(code, strs)
In [3]: trace.find_executable_linenos("t.py")
Out[3]: {2: 1, 4: 1, 5: 1, 9: 1, 10: 1, 12: 1, 14: 1, 15: 1}
However, as you can see, it fails to report he line number "1", so
imho our own function performs better than the one in standard
library.
Ondrej
More information about the testing-in-python
mailing list