Code run with -c or a "code" launch gets __name__ == "builtins" instead of "__main__"
已經有一個關聯 PR 被合併了。
- #2080 來自 @Om-singhaI —— 已合併
評估
研究方向
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.
由索引模型根據 Issue 內容生成。
描述
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.
- 主要語言
- Python
- 星號
- 2.5k
- 分支
- 212
- 平均合併
- 3 天 11 小時
- 30 天內合併 PR
- 6
環境準備
在瀏覽器裡用你自己的 GitHub 帳號啟動這個專案的開發容器。
- 沒有 Dockerfile 或 Docker Compose 檔案
- 沒有 Pull Request 範本
- 閱讀貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
microsoft/debugpy 的其他 Issue
-
pydevd_comm.py start_client() does not set TCP_NODELAY on the connections it opens可能已有人在做 @nightcityblade 於 81 天前認領。 未關閉
難度 2/5 1-3 小時 新手友好度 78/100
-
Document "rules"-option可能已有人在做 @nightcityblade 於 69 天前認領。 未關閉documentation
難度 2/5 1-3 小時 新手友好度 68/100
-
wx.EventLoop() deprecation warning可能已有人在做 @nightcityblade 於 68 天前認領。 未關閉
難度 2/5 1-3 小時 新手友好度 78/100
-
debugpy.configure(python=) is not properly documented可能已有人在做 @nightcityblade 於 79 天前認領。 未關閉documentation
難度 2/5 1-3 小時 新手友好度 62/100
-
難度 5/5 一週以上 新手友好度 15/100
查看 microsoft/debugpy 的全部 Issue
相似的 Issue
-
curriculum documentation quality
難度 2/5 1-3 小時 新手友好度 77/100
githubnext/gh-aw-workshop#5367 ·
維護者通常 3 天內回覆
-
Create Noise Latents ignores randomize_seed and produces the same noise every run可能已有人在做 @griptapeops 今天認領。 未關閉
難度 2/5 1-3 小時 新手友好度 68/100
griptape-ai/griptape-nodes-library-diffusers#127 ·
維護者通常 1 天內回覆
-
Jenkins capacity report: metrics history is lost when archiving a month fails可能已有人在做 @RaphaelFakhri 今天認領。 未關閉
難度 2/5 1-3 小時 新手友好度 78/100
adoptium/infrastructure#4568 ·
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 72/100
huggingface/deep-rl-class#723 ·
-
premises-missing
難度 2/5 1-3 小時 新手友好度 78/100
WingedGuardian/GENesis-AGI#3928 ·
維護者通常 1 天內回覆