Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
4e8a976
Add the option to not reuse the initial tstate.
ericsnowcurrently Jun 20, 2024
120c2a6
Add a test to verify the initial "main" tstate isn't special.
ericsnowcurrently Jun 20, 2024
608139b
Add tests to check different "bad" runtime fini situations.
ericsnowcurrently Jun 20, 2024
592b214
Fix the code in test_audit_subinterpreter.
ericsnowcurrently Jun 20, 2024
e6f31ff
Always try reusing the main tstate (and drop interp.reuse_init_tstate).
ericsnowcurrently Jun 21, 2024
05c0cf3
Skip test_fini_in_subthread on Windows.
ericsnowcurrently Jun 25, 2024
8b241d2
Expect a failure on Windows.
ericsnowcurrently Jun 25, 2024
b66c2b7
Fail if PyFinalize() fails.
ericsnowcurrently Jun 25, 2024
88b1679
Add _Py_Finalize() and struct pyfinalize_args.
ericsnowcurrently Jun 20, 2024
d9d0e10
Use _Py_Finalize() in the new tests.
ericsnowcurrently Jun 21, 2024
72e961f
Factor out resolve_final_tstate().
ericsnowcurrently Jun 25, 2024
e2392c2
Fix the Windows returncode.
ericsnowcurrently Jun 25, 2024
6b1eb57
Export _Py_Finalize().
ericsnowcurrently Jun 25, 2024
7b2afb6
Do not print the error messages with Py_Exit().
ericsnowcurrently Jun 25, 2024
155dddc
Rename the macro.
ericsnowcurrently Jun 25, 2024
f37bcc7
Fix the macro.
ericsnowcurrently Jun 25, 2024
0cffd32
Merge branch 'main' into tests-runtime-lifecycle-bad-usage
ericsnowcurrently Jun 26, 2024
361d477
Fix tests.
ericsnowcurrently Jun 26, 2024
d3beea4
Tweaks to resolve_final_tstate().
ericsnowcurrently Jun 27, 2024
d78f655
Fix the test failures.
ericsnowcurrently Jun 27, 2024
e5c5a5f
More tweaking resolve_final_tstate().
ericsnowcurrently Jun 27, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add a test to verify the initial "main" tstate isn't special.
  • Loading branch information
ericsnowcurrently committed Jun 20, 2024
commit 120c2a6930233bd49534df0012f56a225b11d55a
13 changes: 13 additions & 0 deletions Lib/test/test_embed.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,19 @@ def test_repeated_init_and_inittab(self):
lines = "\n".join(lines) + "\n"
self.assertEqual(out, lines)

def test_replace_main_tstate(self):
with self.subTest(exec=False):
self.run_embedded_interpreter(
'test_replace_main_tstate',
)

with self.subTest(exec=True):
out, _ = self.run_embedded_interpreter(
'test_replace_main_tstate',
'print("spam!")'
)
self.assertEqual(out.strip(), 'spam!')

def test_forced_io_encoding(self):
# Checks forced configuration of embedded interpreter IO streams
env = dict(os.environ, PYTHONIOENCODING="utf-8:surrogateescape")
Expand Down
66 changes: 66 additions & 0 deletions Programs/_testembed.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include <Python.h>
#include "pycore_initconfig.h" // _PyConfig_InitCompatConfig()
#include "pycore_pystate.h" // _Py_IsMainInterpreter()
#include "pycore_runtime.h" // _PyRuntime
#include "pycore_import.h" // _PyImport_FrozenBootstrap
#include <inttypes.h>
Expand Down Expand Up @@ -207,6 +208,70 @@ static int test_repeated_simple_init(void)
}


/****************************************************************************
* Test discarding the initial main tstate
***************************************************************************/

static int test_replace_main_tstate(void)
{
int err = 0;

int hascode = 0;
for (int i = 2; i < main_argc; i++) {
if (strncmp(main_argv[i], "--", 2) == 0) {
fprintf(stderr, "ERROR: unsupported arg %s\n", main_argv[i]);
err = 1;
}
else {
hascode = 1;
}
if (err) {
fprintf(stderr, "usage: %s test_replace_main_tstate [CODE ...]\n",
PROGRAM);
return 1;
}
}

#ifdef Py_DEBUG
_testembed_Py_InitializeFromConfig();
PyThreadState *main_tstate = PyThreadState_Get();
PyInterpreterState *main_interp = main_tstate->interp;
assert(_Py_IsMainInterpreter(main_interp));

PyThreadState_Clear(main_tstate);
PyThreadState_DeleteCurrent();
assert(PyInterpreterState_ThreadHead(main_interp) == NULL);

main_interp->threads.reuse_init_tstate = 0;

PyThreadState *tstate = PyThreadState_New(main_interp);
// The runtime automatically re-uses the initial main tstate.
// (It is statically allocated as part of the global runtime state.)
assert(_PyThreadState_GET() != main_tstate);
(void)PyThreadState_Swap(tstate);

if (hascode) {
for (int i = 2; i < main_argc; i++) {
const char *code = main_argv[i];
if (PyRun_SimpleString(code) != 0) {
err = 1;
break;
}
}
}

assert(PyThreadState_Get() != main_tstate);
Py_Finalize();

return err;
#else
// We want to always force a new "main" tstate, which requires Py_DEBUG.
fprintf(stderr, "ERROR: Py_DEBUG is required\n");
return 1;
#endif
}


/*****************************************************
* Test forcing a particular IO encoding
*****************************************************/
Expand Down Expand Up @@ -2161,6 +2226,7 @@ static struct TestCase TestCases[] = {
{"test_forced_io_encoding", test_forced_io_encoding},
{"test_repeated_init_and_subinterpreters", test_repeated_init_and_subinterpreters},
{"test_repeated_init_and_inittab", test_repeated_init_and_inittab},
{"test_replace_main_tstate", test_replace_main_tstate},
{"test_pre_initialization_api", test_pre_initialization_api},
{"test_pre_initialization_sys_options", test_pre_initialization_sys_options},
{"test_bpo20891", test_bpo20891},
Expand Down