Thanks to visit codestin.com
Credit goes to github.com

Skip to content

bpo-21161: pdb: default: handle list comprehensions #15194

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Lib/pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,8 @@ def displayhook(self, obj):
def default(self, line):
if line[:1] == '!': line = line[1:]
locals = self.curframe_locals
globals = self.curframe.f_globals
ns = self.curframe.f_globals.copy()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What it the code that is being debugged changes globals()? This would mean any changes to self.curframe.f_globals by the calling code is dropped, is it not?

ns.update(locals)
try:
code = compile(line + '\n', '<stdin>', 'single')
save_stdout = sys.stdout
Expand All @@ -372,7 +373,7 @@ def default(self, line):
sys.stdin = self.stdin
sys.stdout = self.stdout
sys.displayhook = self.displayhook
exec(code, globals, locals)
exec(code, ns, locals)
finally:
sys.stdout = save_stdout
sys.stdin = save_stdin
Expand Down
26 changes: 26 additions & 0 deletions Lib/test/test_pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -1562,6 +1562,32 @@ def test_errors_in_command(self):
'(Pdb) ',
])

def test_list_comprehensions(self):
script = """
def f():
mylocal = "init_mylocal" # noqa: F841
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does the comment mean?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Local variable name is assigned to but never used (F841)

import pdb; pdb.Pdb(readrc=False).set_trace()
print("SUCCESS")

f()
"""
commands = """
continue

p "mylocal:" + mylocal
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you need this line in the test?

["mylocal_1:%s" % mylocal for x in range(1)]
mylocal = 42
["mylocal_2:%s" % mylocal for x in range(1)]

continue
"""
stdout, stderr = self.run_pdb_module(script, commands)
lines = stdout.splitlines()
self.assertTrue(any("['mylocal_1:init_mylocal']" in l for l in lines), stdout)
self.assertTrue(any("['mylocal_2:42']" in l for l in lines), stdout)
self.assertTrue(all("NameError" not in l for l in lines), stdout)
self.assertTrue(any("SUCCESS" in l for l in lines), stdout)

def load_tests(*args):
from test import test_pdb
suites = [
Expand Down