Author vstinner
Recipients eric.snow, nanjekyejoannah, ncoghlan, vstinner
Date 2019-11-20.15:42:03
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
Programs/_testembed.c contains the following test used by test_embed:

static int test_audit_subinterpreter(void)
    Py_IgnoreEnvironmentFlag = 0;
    PySys_AddAuditHook(_audit_subinterpreter_hook, NULL);


    switch (_audit_subinterpreter_interpreter_count) {
        case 3: return 0;
        case 0: return -1;
        default: return _audit_subinterpreter_interpreter_count;

When Py_Finalize() is called, the current interpreter is a subinterpreter (the 3rd interpreter), not the main interpreter.

* Is it correct to call Py_Finalize() in such case?
* Is Python supposed to magically destroy the 3 interpreters?

In bpo-38858, I'm trying to reuse the same code to initialize and finalize the "main" interpreter and subinterpreters. I had an issue with test_audit_subinterpreter() when working on the PR 17293.

I modified my PR 17293 to not expect that Py_Finalize() can only be called from the main interpreter, but actually check if the current interpreter is the main interpreter or not. It fix test_audit_subinterpreter() but again, I'm not sure what is the correct behavior?


Last year, we had a similar discussion about calling Py_Main() *after* Py_Initialize(). I hacked the code to make it possible because it was supported previously, even if the Py_Main() configuration is only partially applied. But I understood that Nick Coghlan would prefer to deprecate supporting to call Py_Initialize() before Py_Main().

PEP 587 added Py_RunMain() which provides a different solution to this problem:
Date User Action Args
2019-11-20 15:42:04vstinnersetrecipients: + vstinner, ncoghlan, eric.snow, nanjekyejoannah
2019-11-20 15:42:04vstinnersetmessageid: <>
2019-11-20 15:42:03vstinnerlinkissue38865 messages
2019-11-20 15:42:03vstinnercreate