[pypy-dev] Possibly of use in the quest to parse pypy better

Laura Creighton lac at openend.se
Sun Mar 9 10:24:21 CET 2008

------- Forwarded Message

From: Andrew Dalke <dalke at dalkescientific.com>
Date: Sun, 9 Mar 2008 03:29:29 +0100
To: testing-in-python at lists.idyll.org
X-Mailer: Apple Mail (2.753)
Subject: Re: [TIP] branch coverage

In late January I mentioned I was working on a parser for Python  
using PLY as the parser engine.  I wanted to experiment with branch  
coverage in Python by reading the source .py file, building a parse  
tree, adding instrumentation to the AST, and saving .pyc files with  
the instrumentation.  Then run the regression tests and see which  
branches weren't covered.

I'm not there yet.

What I have done is write a Python parser using PLY.  It's called  
"python4ply" and is available at:

What took even longer than writing the code was writing the  
tutorial.  It shows examples of how to change the lexer, parser, AST,  
and bytecode generation.  I worked through two examples  where I  
instrumented the code to improve testing.

The first of these assumes there are problems like this

     assert 0 not in results, "problem in: %r" % data

where the "data" variable should be "results".  Assert statements are  
rarely fully tested and so prone to failure if they ever occur.  My  
example transforms the code to something like

_$assert = Y
assert X, _$assert

where '$' is something that normal Python code can't generate but AST  
manipulation code can.

Details at: http://dalkescientific.com/Python/python4ply- 

The second shows a way to add statement coverage without using  
settrace.  Details at:


I didn't get to the point where I could do branch coverage.  I didn't  
have a good enough use cases so wasn't sure what I should implement.   
Perhaps you've got an idea of how to do it, or something else  
involving parsing Python code?  (If so, take a look at the _ast  
module from the standard library.  It's probably a better solution.)   
If so, I hope this grammar and code helps.

Download:  http://dalkescientific.com/Python/python4ply.html
Tutorial:  http://dalkescientific.com/Python/python4ply-tutorial.html

				dalke at dalkescientific.com

------- End of Forwarded Message

More information about the Pypy-dev mailing list