bpo-34206: Improve docs and test coverage for pre-init functions (#8023)

- move the Py_Main documentation from the very high level API section
  to the initialization and finalization section
- make it clear that it encapsulates a full Py_Initialize/Finalize
  cycle of its own
- point out that exactly which settings will be read and applied
  correctly when Py_Main is called after a separate runtime
  initialization call is version dependent
- be explicit that Py_IsInitialized can be called prior to
  initialization
- actually test that Py_IsInitialized can be called prior to
  initialization
- flush stdout in the embedding tests that run code so it appears
  in the expected order when running with "-vv"
- make "-vv" on the subinterpreter embedding tests less spammy

---------

Co-authored-by: Carol Willing <carolcode@willingconsulting.com>
This commit is contained in:
Alyssa Coghlan
2024-10-08 18:34:11 +10:00
committed by GitHub
parent 93b9e6bd7d
commit 7c4b6a68f2
7 changed files with 198 additions and 77 deletions

View File

@@ -311,14 +311,36 @@ static int test_pre_initialization_api(void)
_Py_EMBED_PREINIT_CHECK("Checking Py_SetProgramName\n");
Py_SetProgramName(program);
_Py_EMBED_PREINIT_CHECK("Checking !Py_IsInitialized pre-initialization\n");
if (Py_IsInitialized()) {
fprintf(stderr, "Fatal error: initialized before initialization!\n");
return 1;
}
_Py_EMBED_PREINIT_CHECK("Initializing interpreter\n");
Py_Initialize();
_Py_EMBED_PREINIT_CHECK("Checking Py_IsInitialized post-initialization\n");
if (!Py_IsInitialized()) {
fprintf(stderr, "Fatal error: not initialized after initialization!\n");
return 1;
}
_Py_EMBED_PREINIT_CHECK("Check sys module contents\n");
PyRun_SimpleString("import sys; "
"print('sys.executable:', sys.executable)");
PyRun_SimpleString(
"import sys; "
"print('sys.executable:', sys.executable); "
"sys.stdout.flush(); "
);
_Py_EMBED_PREINIT_CHECK("Finalizing interpreter\n");
Py_Finalize();
_Py_EMBED_PREINIT_CHECK("Checking !Py_IsInitialized post-finalization\n");
if (Py_IsInitialized()) {
fprintf(stderr, "Fatal error: still initialized after finalization!\n");
return 1;
}
_Py_EMBED_PREINIT_CHECK("Freeing memory allocated by Py_DecodeLocale\n");
PyMem_RawFree(program);
return 0;
@@ -364,12 +386,15 @@ static int test_pre_initialization_sys_options(void)
_Py_EMBED_PREINIT_CHECK("Initializing interpreter\n");
_testembed_Py_InitializeFromConfig();
_Py_EMBED_PREINIT_CHECK("Check sys module contents\n");
PyRun_SimpleString("import sys; "
"print('sys.warnoptions:', sys.warnoptions); "
"print('sys._xoptions:', sys._xoptions); "
"warnings = sys.modules['warnings']; "
"latest_filters = [f[0] for f in warnings.filters[:3]]; "
"print('warnings.filters[:3]:', latest_filters)");
PyRun_SimpleString(
"import sys; "
"print('sys.warnoptions:', sys.warnoptions); "
"print('sys._xoptions:', sys._xoptions); "
"warnings = sys.modules['warnings']; "
"latest_filters = [f[0] for f in warnings.filters[:3]]; "
"print('warnings.filters[:3]:', latest_filters); "
"sys.stdout.flush(); "
);
_Py_EMBED_PREINIT_CHECK("Finalizing interpreter\n");
Py_Finalize();