[Python-Dev] Tracing at a more granular level (branch coverage)

Amaury Forgeot d'Arc amauryfa at gmail.com
Tue Apr 1 22:49:25 CEST 2008


On Tue, Apr 1, 2008 at 9:02 PM, m h wrote:
> Howdy Folks-
>  I hope this is not too off topic.  Sadly the current sys.settrace only
>  allows tracing at the line level.  This isn't sufficient to do branch
>  and path coverage.  The main problem is that many boolean operations
>  can appear on a single line, and so you can't be sure which
>  conditional branch was taken using the current tracing method.
>  After speaking about code complexity/testing at Pycon [0], with a few
>  people there, and seeing a few people have made some strides or shown
>  interested in metrics/code graphs/flow and branch coverage I think
>  that there is interest in this.
>  One proposed method of getting branch coverage was to use Dalke's
>  Python4Ply [1] to translate code so that all branches occur on their
>  own line.  Then using line coverage on that and converting it back to
>  branch coverage of the original code.
>  The maintainer of coverage.py suggested that we look into patching
>  python instead to trace at a more granular level.  His feeling was
>  that there would be too many corner cases and the translation would
>  get hairy quite quickly.
>  Sadly in my 8 years of python experience I've yet to touch any c based
>  guts of python.  I'm looking for advice on how to get finer grain
>  tracing with sys.settrace.
>  Any advice or suggestions?  There is a quorum of people (at least 5
>  others) who would be very interested in this functionality because it
>  could lead to some cool tools built on top of it.  (With the end goal
>  that python code be cleaner, simpler and better tested).

You can start by looking at the file Python/ceval.c, which contains
the interpreter loop.
More precisely, the function maybe_call_line_trace() is responsible to call
sys.settrace. I think you will have to change the logic there.

Amaury Forgeot d'Arc

More information about the Python-Dev mailing list