Skip to content
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

PR: Test that the debugger handles the namespace of a comprehension #17231

Merged
merged 5 commits into from
Jan 25, 2022
Merged
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
4 changes: 2 additions & 2 deletions external-deps/spyder-kernels/.gitrepo
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
[subrepo]
remote = https://github.com/spyder-ide/spyder-kernels.git
branch = 2.x
commit = 52f9b9c09a4c07f1b9ab00ada87a0d741341db64
parent = 97e14d2e8876460f7fdf1306fbc852274edb2ea4
commit = 7478e850164cbba0de8425110accf9cb1bb66249
parent = b2a48c91215f16f1f01c76789715bbfae8cc4506
method = merge
cmdver = 0.4.3
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,14 @@ def default(self, line):

# Create a function and load the locals
globals["_spyderpdb_locals"] = locals
# Save builtins locals in case it is shadowed
globals["_spyderpdb_builtins_locals"] = builtins.locals
indent = " "
code = ["def _spyderpdb_code():"]
# Load locals if they have a valid name
# In comprehensions, locals could contain ".0" for example
code += [indent + "{k} = _spyderpdb_locals['{k}']".format(
k=k) for k in locals]
k=k) for k in locals if isidentifier(k)]

# Run the code
if print_ret:
Expand All @@ -218,7 +222,8 @@ def default(self, line):
code += [indent + l for l in line.splitlines()]

# Update the locals
code += [indent + "_spyderpdb_locals.update(locals())"]
code += [indent + "_spyderpdb_locals.update("
"_spyderpdb_builtins_locals())"]

# Run the function
code += ["_spyderpdb_code()"]
Expand All @@ -228,6 +233,7 @@ def default(self, line):
finally:
globals.pop("_spyderpdb_locals", None)
globals.pop("_spyderpdb_code", None)
globals.pop("_spyderpdb_builtins_locals", None)
else:
try:
code = compile(line + '\n', '<stdin>', 'single')
Expand Down
50 changes: 50 additions & 0 deletions spyder/plugins/ipythonconsole/tests/test_ipythonconsole.py
Original file line number Diff line number Diff line change
Expand Up @@ -2095,5 +2095,55 @@ def test_shutdown_kernel(ipyconsole, qtbot, tmpdir):
assert not shell.get_value('kernel_exists')


def test_pdb_comprehension_namespace(ipyconsole, qtbot, tmpdir):
"""Check that the debugger handles the namespace of a comprehension."""
shell = ipyconsole.get_current_shellwidget()
qtbot.waitUntil(lambda: shell._prompt_html is not None,
timeout=SHELL_TIMEOUT)
control = ipyconsole.get_widget().get_focus_widget()

# Code to run
code = "locals = 1\nx = [locals + i for i in range(2)]"

# Write code to file on disk
file = tmpdir.join('test_breakpoint.py')
file.write(code)

# Run file
with qtbot.waitSignal(shell.executed):
shell.execute(f"debugfile(filename=r'{str(file)}')")

# steps 4 times
for i in range(4):
with qtbot.waitSignal(shell.executed):
shell.pdb_execute("s")
assert "Error" not in control.toPlainText()

with qtbot.waitSignal(shell.executed):
shell.pdb_execute("print('test', locals + i + 10)")

assert "Error" not in control.toPlainText()
assert "test 11" in control.toPlainText()

settings = {
'check_all': False,
'exclude_callables_and_modules': True,
'exclude_capitalized': False,
'exclude_private': True,
'exclude_unsupported': False,
'exclude_uppercase': True,
'excluded_names': [],
'minmax': False,
'show_callable_attributes': True,
'show_special_attributes': False}

shell.call_kernel(
interrupt=True
).set_namespace_view_settings(settings)
namespace = shell.call_kernel(blocking=True).get_namespace_view()
for key in namespace:
assert "_spyderpdb" not in key


if __name__ == "__main__":
pytest.main()