Code run with -c or a "code" launch gets __name__ == "builtins" instead of "__main__"
Une pull request liée a déjà été fusionnée.
- #2080 par @Om-singhaI — fusionnée
Évaluation
- Difficulté
- 2/5
- Temps estimé
- 1-3 heures
- Accessibilité débutants
- 35/100
Piste de recherche
Start with run_code() in src/debugpy/server/cli.py and the CLI tests in tests/debugpy/server/test_cli.py; compare its execution setup with run_file() using runpy.run_path(). Run the focused CLI tests and reproduce with python3 -m debugpy --listen 127.0.0.1:0 -c "$CODE". Done when code runs with __name__ == "__main__", its main block runs, and the reported pickling failure is covered by a test.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Description
Before creating a new issue, please check the FAQ to see if your question is answered there.
Environment data
- debugpy version: 1.8.22+3.g751502d (running from source at
751502d, currentmain) - OS and version: macOS 26.6.2 (25G83)
- Python version (& distribution if applicable, e.g. Anaconda): CPython 3.10.6 from python.org, and 3.14.3 from Homebrew
- Using VS Code or Visual Studio: neither. I used the CLI, and also a
"code"launch sent straight to the adapter.
Actual behavior
Code passed with -c, or with "code" in a launch configuration, doesn't run as __main__. __name__ is "builtins", so an if __name__ == "__main__": block is skipped, and a class the code defines can't be pickled:
__name__ = builtins
pickle failed: Can't pickle <class 'Point'>: attribute lookup Point on builtins failed
That is from 3.10.6. 3.14.3 gives the same result, with the pickle error worded as it's not found as builtins.Point.
run_code() in src/debugpy/server/cli.py ends with eval(code, {}). That globals dict has no __name__, so the name is looked up in builtins and comes back as "builtins". A class the code defines gets that as its __module__, which is where pickle then looks for it. A file target doesn't have this problem, because run_file() uses runpy.run_path(target, run_name="__main__"), which puts a temporary __main__ module in place.
A "code" launch goes through the same function, since the adapter turns it into a -c argument for the debugpy CLI in the debuggee. With "code": ["print('__name__ is', __name__)", "if __name__ == '__main__':", " print('main block ran')"] and "console": "internalConsole", the debuggee prints __name__ is builtins and nothing else.
Expected behavior
The same as python -c:
__name__ = __main__
main block ran
pickle ok
Steps to reproduce:
CODE='import pickle
class Point: pass
print("__name__ =", __name__)
if __name__ == "__main__":
print("main block ran")
try:
pickle.dumps(Point()); print("pickle ok")
except Exception as e:
print("pickle failed:", e)'
python3 -c "$CODE"
python3 -m debugpy --listen 127.0.0.1:0 -c "$CODE"
The first command prints the expected output above and the second prints the actual output. Nothing needs to attach; the code runs right away because --wait-for-client isn't given.
I have a fix and can open a PR. It runs the code in a temporary __main__ module, the same way run_path() does for a file, and adds a test to tests/debugpy/server/test_cli.py.
- Langage dominant
- Python
- Étoiles
- 2.5k
- Forks
- 205
- Merge moyen
- 1 j 5 h
- PR mergées (30 j)
- 4
Préparer son environnement
Lance le conteneur de développement du projet dans votre navigateur, avec votre propre compte GitHub.
- Aucun Dockerfile ni fichier Docker Compose
- Aucun modèle de pull request
- Lire le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Autres issues de microsoft/debugpy
-
pydevd_comm.py start_client() does not set TCP_NODELAY on the connections it opensPeut-être pris @nightcityblade l’a pris il y a 52 jours. Ouverte
Difficulté 2/5 1-3 heures Accessibilité débutants 78/100
-
Document "rules"-optionPeut-être pris @nightcityblade l’a pris il y a 41 jours. Ouvertedocumentation
Difficulté 2/5 1-3 heures Accessibilité débutants 68/100
-
wx.EventLoop() deprecation warningPeut-être pris @nightcityblade l’a pris il y a 39 jours. Ouverte
Difficulté 2/5 1-3 heures Accessibilité débutants 78/100
-
debugpy.configure(python=) is not properly documentedPeut-être pris @nightcityblade l’a pris il y a 50 jours. Ouvertedocumentation
Difficulté 2/5 1-3 heures Accessibilité débutants 62/100
-
Adapter bootstrap fails with ConnectionRefusedError on WSL2 VirtioProxy or hostAddressLoopbackPeut-être pris @nightcityblade l’a pris il y a 6 jours. Ouverte
Difficulté 3/5 1-2 jours Accessibilité débutants 68/100
Toutes les issues de microsoft/debugpy
Issues similaires
-
area/install reliability
Difficulté 2/5 1-3 heures Accessibilité débutants 75/100
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 2/5 1-3 heures Accessibilité débutants 83/100
FluidNumerics/fluid-walk-blocker#191 ·
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 2/5 1-3 heures Accessibilité débutants 62/100
TransformerLensOrg/TransformerLens#1868 ·
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 2/5 1-3 heures Accessibilité débutants 76/100
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 1/5 Moins d'une heure Accessibilité débutants 85/100
climate-analytics-lab/jax-gcm#1057 ·
Les mainteneurs répondent en général sous 1 jour