classification
Title: pdb shows code from wrong module
Type: behavior Stage:
Components: Versions: Python 2.7
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: georg.brandl, meador.inge, ncoghlan, petri.lehtinen, xdegaye, yak
Priority: normal Keywords: patch

Created on 2011-11-03 00:03 by yak, last changed 2020-01-24 23:30 by brett.cannon.

Files
File name Uploaded Description Edit
pdb.diff yak, 2011-11-03 00:03
Messages (2)
msg146885 - (view) Author: Arkadiusz Wahlig (yak) Date: 2011-11-03 00:03
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.
msg146886 - (view) Author: Arkadiusz Wahlig (yak) Date: 2011-11-03 00:13
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.
History
Date User Action Args
2020-01-24 23:30:40brett.cannonsetnosy: - brett.cannon
2012-12-05 13:53:48xdegayesetnosy: + xdegaye
2011-11-06 03:17:11meador.ingesetnosy: + meador.inge
2011-11-03 08:06:58petri.lehtinensetnosy: + petri.lehtinen
2011-11-03 01:02:37pitrousetnosy: + brett.cannon, georg.brandl, ncoghlan
2011-11-03 00:13:25yaksetmessages: + msg146886
2011-11-03 00:03:17yakcreate