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

del doesn't delete object and x = None doesn't create None object #1138

Closed
rcasero opened this issue Nov 30, 2022 · 2 comments
Closed

del doesn't delete object and x = None doesn't create None object #1138

rcasero opened this issue Nov 30, 2022 · 2 comments
Labels
bug Something isn't working

Comments

@rcasero
Copy link

rcasero commented Nov 30, 2022

Before creating a new issue, please check the FAQ to see if your question is answered there.

Environment data

  • debugpy version: 1.6.3 (run import debugpy; print(debugpy.__version__) if uncertain)
  • OS and version: MacOS X Ventura 13.0.1
  • Python version (& distribution if applicable, e.g. Anaconda): 3.8.13, miniconda 3
  • Using VS Code or Visual Studio: VS Code Version: 1.73.1 (Universal)

Actual behavior

In Debug Console

  • del x is ignored.
  • x = None doesn't create an object.

Expected behavior

  • del x should delete the object.
  • x = None should create an object with None value.

Steps to reproduce:

  1. Create dummy script with just
    import sys
    and place a breakpoint on that line
  2. Start debugging session using remote -SSH extension on server running Linux CentOS Stream 8, with Microsoft Python extension and following launch.json. The script
    {
        "version": "0.2.0",
        "configurations": [
            {
                "name":       "Python: Current File",
                "type":       "python",
                "request":    "launch",
                "cwd":        "${workspaceFolder}/bin",
                "program":    "${file}",
                "console":    "integratedTerminal",
                "redirectOutput": true,
                "justMyCode": true,
                "env": {"PYDEVD_WARN_SLOW_RESOLVE_TIMEOUT": "2"}
            }
        ]
    }
  3. Go to Debug Console
  4. Create variable x=5
  5. Print variable print(x). This shows 5
  6. Delete variable del x.
  7. Print deleted variable print(x). This still shows 5. Even after import gc; gc.collect(), x still exists.
  8. Create new variable with None value. y = None
  9. Print new variable print(y). This produces the error
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
    NameError: name 'y' is not defined
    
  10. However, if a previously defined variable is assigned None, e.g. x = None, then the variable is still defined.
@int19h int19h added bug Something isn't working and removed needs investigation labels Nov 30, 2022
@int19h
Copy link
Contributor

int19h commented Dec 2, 2022

The part about assigning to None not working is because it happens to hit this check:

for key, val in updated_globals.items():
if initial_globals.get(key) is not val:

del should probably also be handled in this code, by doing the comparison in the other direction (loop over initial vars, check presence in updated).

@fabioz
Copy link
Collaborator

fabioz commented Dec 8, 2022

I've just provided a PR for this.

So, it works in the case given, but still, it has caveats in that it's not possible to del a var from a variable depending on how it's set (it works for variables you create in the frame during evaluation but may not work for variables already defined in the frame -- this is a CPython limitation).

The example below shows what I mean:

import ctypes
import sys

def method():
    A = 10
    return sys._getframe()

frame = method()

del frame.f_locals['A']
ctypes.pythonapi.PyFrame_LocalsToFast(ctypes.py_object(frame), ctypes.c_int(0))

# "A" is still there even though we deleted it...
print(frame.f_locals)

@fabioz fabioz closed this as completed in 1c79248 Dec 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants