coverage.py doesn't seem to work with pdb.set_trace()

Issue #647 closed
Former user created an issue

First, I've been a long-time user of coverage.py - thanks so much for creating it!

I recently have been working on a personal project of mine and uncovered a behavior with coverage.py that has me stumped.

Without going into too many gory details, my project is an extension to Python's pdb debugger that allows you to debug non-interactive Python programs over a telnet session. I've written some tests for it and am attempting to use coverage.py, but I can't get coverage.py to properly measure coverage even though I know certain lines of code are being hit in my tests.

It seems to come down to coverage.py seemingly not playing well with pdb.set_trace(). Here's a very simple script that illustrates the problem I'm having:

import pdb

class FriendlyPdb(pdb.Pdb):

    def cmdloop(self):
        print("Look, a custom pdb!")
        return pdb.Pdb.cmdloop(self)

if __name__ == '__main__':
    FriendlyPdb().set_trace()

If I run this Python script, it behaves as you'd expect:

$ coverage run example.py
--Return--
> /Users/ryanpetrello/example.py(11)<module>()->None
-> FriendlyPdb().set_trace()
Look, a custom pdb!
(Pdb) c

Yet the coverage report says that lines 6-7 (the contents of the cmdloop function) are missing:

$ coverage report -m
Name         Stmts   Miss  Cover   Missing
------------------------------------------
example.py       7      2    71%   6-7

Comments (4)

  1. ryanpetrello

    Apologies for creating this anonymously - I didn't realize Bitbucket allowed that. I'm the person who filed this issue.

  2. Ned Batchelder repo owner

    Both coverage.py and debuggers use the trace function feature. If everyone was careful to chain the trace functions, it might work, but I don't think pdb invokes a previous trace function in its function. I'm not sure coverage.py can do anything to make this work.

  3. ryanpetrello

    Ned,

    Thanks for taking the time to reply and confirming I wasn't missing something obvious. Please feel free to close this ticket :).

  4. Log in to comment