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

builtins.globals() fails to update after a comprehension with colliding variable in Python 3.12 #1849

Closed
kycutler opened this issue Feb 28, 2025 · 5 comments
Assignees
Labels
needs repro Issue has not been reproduced yet

Comments

@kycutler
Copy link

Environment data

  • debugpy version: 1.8.12
  • OS and version: Windows 11
  • Python version (& distribution if applicable, e.g. Anaconda): 3.12.8
  • Using VS Code or Visual Studio: VS Code

Actual behavior

Run and debug the following, with a breakpoint on the last line:

# Global variable
foo = None

# Comprehension with colliding local variable
[foo for foo in [0]]

# Breakpoint here
debugger()

While paused, evaluate the following in debug console:

import builtins
builtins.globals()["abc"] = 123
builtins.globals()["abc"]

Notice the last evaluation fails with a KeyError, even though it should have been set.

This issue only reproduces under the following circumstances:

  • Python 3.12 (any patch version -- but 3.11 and 3.13 both work)
  • The code is in global scope
  • The iteration variable in the comprehension collides with the global variable

Expected behavior

The assignment to builtins.globals() should persist between evaluations

@github-actions github-actions bot added the needs repro Issue has not been reproduced yet label Feb 28, 2025
@rchiodo
Copy link
Contributor

rchiodo commented Feb 28, 2025

Python 3.12 (any patch version -- but 3.11 and 3.13 both work)

If that's the case, it's likely this was a bug in the CPython runtime.

@rchiodo rchiodo closed this as completed Feb 28, 2025
@kycutler
Copy link
Author

kycutler commented Mar 1, 2025

@rchiodo I'm sure it's related to the Python 3.12 implementation, but it may not be a bug there either -- rather I suspect it's specific to the way debugpy handles globals during evaluation, e.g. here. I'm wondering if it might be more appropriate for debugpy to work around the difference.

Also, it appears to be more general than just the case with builtins above -- the issue also appears when just evaluating:

x = 1 # OK

y = 1 # OK

y     # NameError: name 'y' is not defined

@rchiodo
Copy link
Contributor

rchiodo commented Mar 1, 2025

I can't seem to reproduce it myself.

Image

That was with 3.12.

There was something similar a while back though.
#1636

Are you sure you're using 1.8.12 for debugpy?

@kycutler
Copy link
Author

kycutler commented Mar 3, 2025

Strange -- did you use the script above, with breakpoint at the end? It repros consistently for me across multiple machines / environment types (venv, conda) under those conditions.

It seems likely that #1636 is the same issue. We originally encountered this in a larger script (with lots of variables / chances for collision) and gradually boiled it down to the above.

Here's the full screenshot of my repro / versions:

Image

@rchiodo
Copy link
Contributor

rchiodo commented Mar 3, 2025

No, I didn't use your original script. I can now reproduce your issue with 3.12, but not 3.13. I wonder what in that script is breaking things.

I don't think it would be worth investigating though if the workaround is update to 3.13. This code has been around for many years (only change recently was to change single quotes to double quotes)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs repro Issue has not been reproduced yet
Projects
None yet
Development

No branches or pull requests

3 participants