Code run with -c or a "code" launch gets __name__ == "builtins" instead of "__main__"
Um pull request relacionado já foi integrado.
- #2080 de @Om-singhaI — integrado
Avaliação
- Dificuldade
- 2/5
- Tempo estimado
- 1-3 horas
- Facilidade para iniciantes
- 35/100
Direção de pesquisa
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.
Escrita pelo modelo de indexação a partir do texto da issue.
Descrição
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.
- Linguagem predominante
- Python
- Estrelas
- 2.5k
- Forks
- 205
- Merge médio
- 1d 3h
- PRs com merge (30d)
- 5
Preparar o ambiente
Inicia o contêiner de desenvolvimento do projeto no navegador, com a sua própria conta do GitHub.
- Sem Dockerfile nem arquivo Docker Compose
- Sem modelo de pull request
- Ler o guia de contribuição
Primeiros passos
- Leia a issue inteira e depois o guia de contribuição do projeto.
- Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
- Faça um fork do repositório e trabalhe em uma branch.
- Abra um pull request que referencie o número da issue.
Mais de microsoft/debugpy
-
pydevd_comm.py start_client() does not set TCP_NODELAY on the connections it opensTalvez já em andamento @nightcityblade assumiu há 54 dias. Aberta
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 78/100
-
Document "rules"-optionTalvez já em andamento @nightcityblade assumiu há 42 dias. Abertadocumentation
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 68/100
-
wx.EventLoop() deprecation warningTalvez já em andamento @nightcityblade assumiu há 41 dias. Aberta
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 78/100
-
debugpy.configure(python=) is not properly documentedTalvez já em andamento @nightcityblade assumiu há 52 dias. Abertadocumentation
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 62/100
-
Dificuldade 5/5 Mais de uma semana Facilidade para iniciantes 15/100
Todas as issues de microsoft/debugpy
Issues semelhantes
-
HTML: <template> content is extracted as document textTalvez já em andamento @ryanmeowy assumiu hoje. Abertabug html
Dificuldade 1/5 Menos de uma hora Facilidade para iniciantes 82/100
docling-project/docling#4714 · 2 comentários ·
Mantenedores costumam responder em até 1 dia
-
[BUG] Qdrant RAG client applies score_threshold to raw cosine similarity, not the 0-1 score it returnsTalvez já em andamento @roydonsequeira assumiu hoje. Abertabug
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 70/100
Mantenedores costumam responder em até 1 dia
-
Host test failure in core/direct_io.zig on Linux kernel 6.17: O_DIRECT open succeeds on procfs, so the test's 'plain' fd is not plainTalvez já em andamento Um pull request vinculado a esta issue está aberto ou já foi mesclado. Aberta
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 72/100
ashhart/TensorFold#536 ·
Mantenedores costumam responder em até 1 dia
-
area/install-update comp/cli duplicate P2 python:uv sweeper:risk-compatibility type/bug
Dificuldade 1/5 Menos de uma hora Facilidade para iniciantes 62/100
NousResearch/hermes-agent#135440 · 1 comentário ·
Mantenedores costumam responder em até 1 dia
-
bug
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 85/100
Deepak3699/Ai_Mentor#244 ·
Mantenedores costumam responder em até 1 dia