Code run with -c or a "code" launch gets __name__ == "builtins" instead of "__main__"
Maintainer antworten meist innerhalb von 1 Tag
Ein zugehöriger Pull Request wurde bereits gemerged.
- #2080 von @Om-singhaI — gemerged
Bewertung
- Schwierigkeit
- 2/5
- Geschätzter Aufwand
- 1-3 Stunden
- Anfängerfreundlichkeit
- 35/100
Rechercherichtung
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.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Beschreibung
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.
- Vorherrschende Sprache
- Python
- Sterne
- 2.5k
- Forks
- 209
- Ø Merge
- 1 T. 43 Min.
- Gemergte PRs (30 T.)
- 6
Entwicklungsumgebung
Startet den Dev-Container des Projekts im Browser, mit Ihrem eigenen GitHub-Konto.
- Kein Dockerfile und keine Docker-Compose-Datei
- Keine Pull-Request-Vorlage
- Beitragsleitfaden lesen
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus microsoft/debugpy
-
pydevd_comm.py start_client() does not set TCP_NODELAY on the connections it opensEvtl. vergeben @nightcityblade hat das vor 65 Tagen übernommen. Offen
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
microsoft/debugpy#2054 · 1 Kommentar ·
Maintainer antworten meist innerhalb von 1 Tag
-
Document "rules"-optionEvtl. vergeben @nightcityblade hat das vor 53 Tagen übernommen. Offendocumentation
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 68/100
microsoft/debugpy#1958 · 2 Kommentare ·
Maintainer antworten meist innerhalb von 1 Tag
-
wx.EventLoop() deprecation warningEvtl. vergeben @nightcityblade hat das vor 52 Tagen übernommen. Offen
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
microsoft/debugpy#1901 · 3 Kommentare · 1 Reaktion ·
Maintainer antworten meist innerhalb von 1 Tag
-
debugpy.configure(python=) is not properly documentedEvtl. vergeben @nightcityblade hat das vor 63 Tagen übernommen. Offendocumentation
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 62/100
microsoft/debugpy#1097 · 3 Kommentare ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 5/5 Über eine Woche Anfängerfreundlichkeit 15/100
microsoft/debugpy#2082 · 12 Kommentare ·
Maintainer antworten meist innerhalb von 1 Tag
Alle Issues in microsoft/debugpy
Ähnliche Issues
-
get_page_thumbnail modifies cached page images, affecting later thumbnails and picture exportsOffen
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 68/100
docling-project/docling-mcp#147 ·
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 80/100
Maintainer antworten meist innerhalb von 1 Tag
-
enhancement
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 76/100
autowarefoundation/autoware_carla_scenario#405 ·
Maintainer antworten meist innerhalb von 1 Tag
-
bug
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 79/100
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
mohitmishra786/readprism#322 ·
Maintainer antworten meist innerhalb von 1 Tag