python / cpython

The Python programming language
https://www.python.org
Other
62.65k stars 30.05k forks source link

pdb shows code from wrong module #57537

Open e994aa09-14d6-47b9-a306-aac92a95b301 opened 12 years ago

e994aa09-14d6-47b9-a306-aac92a95b301 commented 12 years ago
BPO 13328
Nosy @birkenfeld, @ncoghlan, @meadori, @xdegaye, @akheron
Files
  • pdb.diff
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields: ```python assignee = None closed_at = None created_at = labels = ['type-bug'] title = 'pdb shows code from wrong module' updated_at = user = 'https://bugs.python.org/yak' ``` bugs.python.org fields: ```python activity = actor = 'brett.cannon' assignee = 'none' closed = False closed_date = None closer = None components = [] creation = creator = 'yak' dependencies = [] files = ['23601'] hgrepos = [] issue_num = 13328 keywords = ['patch'] message_count = 2.0 messages = ['146885', '146886'] nosy_count = 6.0 nosy_names = ['georg.brandl', 'ncoghlan', 'meador.inge', 'yak', 'xdegaye', 'petri.lehtinen'] pr_nums = [] priority = 'normal' resolution = None stage = None status = 'open' superseder = None type = 'behavior' url = 'https://bugs.python.org/issue13328' versions = ['Python 2.7'] ```

    e994aa09-14d6-47b9-a306-aac92a95b301 commented 12 years ago

    If pdb is used to debug code using zipimport, it may end up displaying source code from wrong module. Python will execute the correct code but the source code lines displayed by pdb (including the "list" command) will come from an unrelated module.

    Reason:

    The pdb obtains lines of code using the linecache module. When used with zipimported modules, linecache requires the module's globals dict to be passed in along with the filename. The filename is then used as a cache key for future lookups.

    A bug in pdb causes it to pass filename and globals from different modules when calling linecache thus polluting the cache with bogus data.

    A patch for 2.7.2 is attached that fixes the problem.

    The patch also fixes another problem:

    When Bdb (Pdb parent class from bdb.py) calls linecache, it calls Bdb.canonic(filename) before passing the filename to it. It doesn't pass the module's globals though. This isn't a problem because the call is always made after Pdb has queried source lines for the same module (and it does pass the globals). However, because Pdb doesn't call Bdb.canonic() on the filename, the cache key is different and Bdb's call fails.

    To fix this, the patch adds calls to Bdb.canonic(filename) whenever Pdb passes a filename to linecache.

    e994aa09-14d6-47b9-a306-aac92a95b301 commented 12 years ago

    How to reproduce:

    Given a module foo.py imported using zipimport with a function bar, if we try to set a breakpoint in bar using:

    break foo.bar

    pdb would take filename and lineno from the function object but would use current frame to get module globals.

    linecache would return a line from current module but also store it in cache under foo's filename.