Search code examples
pythonpython-3.xgeneratorpdbipdb

Possible bug in pdb module in Python 3 when using list generators


After running this code in Python 3:

import pdb

def foo():
    nums = [1, 2, 3]
    a = 5
    pdb.set_trace()

foo()

The following expressions work:

(Pdb) print(nums)
[1, 2, 3]

(Pdb) print(a)
5

(Pdb) [x for x in nums]
[1, 2, 3]

but the following expression fails:

(Pdb) [x*a for x in nums]
*** NameError: global name 'a' is not defined

The above works fine in Python 2.7.

Is this a bug or I am missing something?

Update: See the new accepted answer. This was indeed a bug (or a problematic design) which has been addressed now by introducing a new command and mode in pdb.


Solution

  • if you type interact in your [i]pdb session, you get an interactive session, and list comprehensions do work as expected in this mode

    source: http://bugs.python.org/msg215963