[TIP] Fwd: figleaf tests

Ondrej Certik ondrej at certik.cz
Tue Jun 24 15:22:15 PDT 2008


I was only emailing to Titus by a mistake. The final patch is attached.


---------- Forwarded message ----------
From: Ondrej Certik <ondrej at certik.cz>
Date: Wed, Jun 25, 2008 at 12:19 AM
Subject: Re: [TIP] figleaf tests
To: titus at idyll.org


> The patch is also simple:
>
> diff --git a/figleaf/internals.py b/figleaf/internals.py
> --- a/figleaf/internals.py
> +++ b/figleaf/internals.py
> @@ -4,6 +4,7 @@ Coverage tracking internals.
>
>  import sys
>  import threading
> +import dis
>
>  err = sys.stderr
>
> @@ -39,47 +40,36 @@ class LineGrabber:
>         """
>         self.lines = set()
>
> -        src = fp.read().strip() + "\n"
> -
> +        src = fp.read()
> +        code = compile(src, "", "exec")
> +        self.lines = self.disassemble(code)
> +
> +        src = src.strip() + "\n"
> +
>         self.ast = parser.suite(src)
> -
> +
>         self.tree = parser.ast2tuple(self.ast, True)
>
> -        self.find_terminal_nodes(self.tree)
> +    def disassemble(self, co):
> +        """Disassemble a code object and return line numbers."""
> +        code = co.co_code
> +        linestarts = dict(dis.findlinestarts(co))
> +        n = len(code)
> +        i = 0
> +        lines = set([])
> +        while i < n:
> +            c = code[i]
> +            op = ord(c)
> +            if i in linestarts:
> +                lines.add(linestarts[i])
>
> -    def find_terminal_nodes(self, tup):
> -        """
> -        Recursively eat an AST in tuple form, finding the first line
> -        number for "interesting" code.
> -        """
> -        (sym, rest) = tup[0], tup[1:]
> -
> -        line_nos = set()
> -        if type(rest[0]) == types.TupleType:  ### node
> -
> -            for x in rest:
> -                token_line_no = self.find_terminal_nodes(x)
> -                if token_line_no is not None:
> -                    line_nos.add(token_line_no)
> -
> -            if symbol.sym_name[sym] in ('stmt', 'suite', 'lambdef',
> -                                        'except_clause') and line_nos:
> -                # store the line number that this statement started at
> -                self.lines.add(min(line_nos))
> -            elif symbol.sym_name[sym] in ('if_stmt',):
> -                # add all lines under this
> -                self.lines.update(line_nos)
> -            elif symbol.sym_name[sym] in ('global_stmt',): # IGNORE
> -                return
> -            else:
> -                if line_nos:
> -                    return min(line_nos)
> -
> -        else:                               ### leaf
> -            if sym not in (NEWLINE, STRING, INDENT, DEDENT, COLON) and \
> -               tup[1] != 'else':
> -                return tup[2]
> -            return None
> +            i = i+1
> +            if op >= dis.HAVE_ARGUMENT:
> +                i = i+2
> +        for const in co.co_consts:
> +            if type(const) == types.CodeType:
> +                lines.update(self.disassemble(const))
> +        return lines
>
>     def pretty_print(self, tup=None, indent=0):
>         """
>
>
>
>
> However, the problem is that figleaf sometimes puts wrong filenames in
> .figleaf and I have no idea why (it is unrelated to the patch above
> though). For example when I execute this:
>
> ondra at fuji:~/ext/figleaf-latest$ python bin/figleaf /usr/bin/py.test
> /home/ondra/ext/figleaf-latest/sympy/core/
>
> it leaves these paths in .figleaf:
>
> /home/ondra/sympy/something....
>
> and then figleaf2html fails to find them, because those file do not
> exist on my system anymore.  Now I tried to delete .figleaf and tried
> again, now it works... Does .figleaf need to be deleted before each
> run?

Yes, it seems so. Then all (almost) works. I'll report another bug I
discovered in another email.

The final patch (with Ned's improvements) is attached. All tests pass.

Ondrej
-------------- next part --------------
A non-text attachment was scrubbed...
Name: lines.patch
Type: text/x-patch
Size: 2585 bytes
Desc: not available
Url : http://lists.idyll.org/pipermail/testing-in-python/attachments/20080625/07dfcedc/attachment.bin 


More information about the testing-in-python mailing list