python / python/cpython

Interpreter Methods For Main Interpreter Fail Unexpectedly

未關閉
#139,458 2 則留言 0 個 reaction 已指派 1 人 在 GitHub 檢視

@ericsnowcurrently 已經在處理了。

開始於 2025年9月30日。

3.14 3.15 stdlib topic-subinterpreters type-bug
主要語言
Python
星號
77.2k
分支
36k
PR 合併指標
PR 指標待擷取

描述

Bug report

Bug description:

The following should run without error:

import contextlib
from concurrent import interpreters

assert __name__ == '__main__'

DIV1 = 'v' * 40
DIV2 = '^' * 40

@contextlib.contextmanager
def dump_traceback():
    try:
        yield
    except Exception:
        import traceback
        print()
        print(DIV1)
        traceback.print_exc()
        print(DIV2)

# First try with the current interpreter.
interp0 = interpreters.get_current()
assert interp0 is interpreters.get_main()

# Interpreters "running main" cannot exec or call.
assert interp0.is_running
with dump_traceback():
    try:
        interp0.exec('None')
    except interpreters.InterpreterError as exc:
        if 'running main' not in str(exc):
            raise
with dump_traceback():
    try:
        interp0.call(print, 'spam!', flush=True)
    except interpreters.InterpreterError as exc:
        if 'running main' not in str(exc):
            raise

# prepare_main() should still work
assert 'spam' not in globals()
with dump_traceback():
    interp0.prepare_main(spam=11)
    assert spam == 11

# Try with a subinterpreter
interp1 = interpreters.create()

assert 'q' not in globals()
with dump_traceback():
    interp1.exec("""if True:
        from concurrent import interpreters
        q = interpreters.create_queue()
        interp0 = interpreters.get_main()
        interp0.prepare_main(q=q)
        q.put_nowait(42)
        """)
    assert 'q' in globals()
    val = q.pop()
    assert val == 42

Instead, the four different calls against interp0 raise InterpreterError with "cannot ... unrecognized interpreter 0". The calls to exec() and call() should fail with something like "running main". The call to prepare_main() should work. Whether prepare_main() should work or not is a separate matter. Regardless, the main interpreter should be recognized.

Note that interp0.is_running does work, as expected.

CPython versions tested on:

3.15, 3.14

Operating systems tested on:

No response

Linked PRs
  • gh-139521

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

評估

這個 Issue 還沒有評估資料。

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。