You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Interpreter Methods For Main Interpreter Fail Unexpectedly #139458
importcontextlibfromconcurrentimportinterpretersassert__name__=='__main__'DIV1='v'*40DIV2='^'*40@contextlib.contextmanagerdefdump_traceback():
try:
yieldexceptException:
importtracebackprint()
print(DIV1)
traceback.print_exc()
print(DIV2)
# First try with the current interpreter.interp0=interpreters.get_current()
assertinterp0isinterpreters.get_main()
# Interpreters "running main" cannot exec or call.assertinterp0.is_runningwithdump_traceback():
try:
interp0.exec('None')
exceptinterpreters.InterpreterErrorasexc:
if'running main'notinstr(exc):
raisewithdump_traceback():
try:
interp0.call(print, 'spam!', flush=True)
exceptinterpreters.InterpreterErrorasexc:
if'running main'notinstr(exc):
raise# prepare_main() should still workassert'spam'notinglobals()
withdump_traceback():
interp0.prepare_main(spam=11)
assertspam==11# Try with a subinterpreterinterp1=interpreters.create()
assert'q'notinglobals()
withdump_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'inglobals()
val=q.pop()
assertval==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.
prepare_main(), exec(), and call() all pass restrict=True to the corresponding low-level functions, while is_running() uses the default (False). That's the key difference here. The question is why resolve_interp() in _interpretersmodule.c makes "restrict=True" for the main interpreter mean "not recognized".
FWIW, there is a separate question of if other interpreters not created through the interpreters module should be recognized. At the least the error message from the 3 methods should be more helpful than "cannot ... unrecognized interpreter X". Perhaps something more like "not managed by interpreters module".
Activity
ericsnowcurrently commentedon Sep 30, 2025
prepare_main(),exec(), andcall()all passrestrict=Trueto the corresponding low-level functions, whileis_running()uses the default (False). That's the key difference here. The question is whyresolve_interp()in _interpretersmodule.c makes "restrict=True" for the main interpreter mean "not recognized".ericsnowcurrently commentedon Oct 2, 2025
FWIW, there is a separate question of if other interpreters not created through the
interpretersmodule should be recognized. At the least the error message from the 3 methods should be more helpful than "cannot ... unrecognized interpreter X". Perhaps something more like "not managed by interpreters module".