microsoft / debugpy

An implementation of the Debug Adapter Protocol for Python
https://pypi.org/project/debugpy/
Other
1.84k stars 138 forks source link

Support PEP 582 (__pypackages__) for just-my-code and user-uncaught exceptions #750

Open Bruceforce opened 3 years ago

Bruceforce commented 3 years ago

Environment data

Actual behavior

First, sorry for the stupid title but I couldn't find a better one. Feel free to change.

I'm using VSCode with the python extension version 2021.10.1317843341 (debugpy version 1.5.0). Every time I try to debug a very simple code which uses the requests module I receive lot's of different exceptions starting with

Exception has occurred: KeyError       (note: full exception trace is shown but execution is paused at: __getitem__)
'proxy-authorization'
  File "C:\Users\USER\VisualSutdioCode\testmodule\__pypackages__\3.8\lib\requests\structures.py", line 54, in __getitem__ (Current frame)
    return self._store[key.lower()][1]
  File "C:\Users\USER\VisualSutdioCode\testmodule\__pypackages__\3.8\lib\requests\sessions.py", line 301, in rebuild_proxies
    if 'Proxy-Authorization' in headers:
  File "C:\Users\USER\VisualSutdioCode\testmodule\__pypackages__\3.8\lib\requests\sessions.py", line 636, in send
    kwargs.setdefault('proxies', self.rebuild_proxies(request, self.proxies))
  File "C:\Users\USER\VisualSutdioCode\testmodule\__pypackages__\3.8\lib\requests\sessions.py", line 542, in request
    resp = self.send(prep, **send_kwargs)
  File "C:\Users\USER\VisualSutdioCode\testmodule\__pypackages__\3.8\lib\requests\api.py", line 61, in request
    return session.request(method=method, url=url, **kwargs)
  File "C:\Users\USER\VisualSutdioCode\testmodule\__pypackages__\3.8\lib\requests\api.py", line 75, in get
    return request('get', url, params=params, **kwargs)
  File "C:\Users\USER\VisualSutdioCode\testmodule\app.py", line 9, in <module>
    requests.get("https://google.com")

Expected behavior

After downgrading the VSCode python plugin to version 2021.9.1246542782 (debugpy version 1.4.3) everything is fine. No exceptions.

Steps to reproduce:

I'm using pdm if that matters.

  1. pdm init (default settings)
  2. pdm add requests==2.26.0

Run debug on the following code

import requests

if __name__ == "__main__":
    requests.get("https://google.com")

settings.json

{
    "python.autoComplete.extraPaths": ["__pypackages__/3.8/lib"],
    "python.analysis.extraPaths": ["__pypackages__/3.8/lib"]
}

launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: app.py",
            "type": "python",
            "request": "launch",
            "program": "${workspaceFolder}/app.py",
            "console": "integratedTerminal",
            "env": {"PYTHONPATH": "__pypackages__/3.8/lib"}
        }
    ]
}
int19h commented 3 years ago

The exception in question is handled up the stack, so if you continue (F5) past this one and other similar exceptions, it should run fine.

You don't normally see those exceptions because debugpy filters out anything that happens inside the standard library and installed packages, unless the exception flows out into your code. However, this logic is not aware of PEP 582, so the debugger ends up being confused about which code counts as yours, and which does not.

Try unchecking "User Uncaught Exceptions" in the Breakpoints pane - this should let you get past it without having to manually resume all the time.

Bruceforce commented 3 years ago

I see. Feeling a bit stupid now :-). Thanks for the quick reply and for pointing that out 👍