From 44e936822252df9e6420b42dc2520d622f430bc3 Mon Sep 17 00:00:00 2001 From: Daniel Jacobs <44678615+danielcjacobs@users.noreply.github.com> Date: Wed, 1 Feb 2023 02:42:05 -0500 Subject: [PATCH] Use PyConfig_InitPythonConfig instead of PyConfig_InitIsolatedConfig (#4473) * Use PyConfig_InitPythonConfig instead of PyConfig_InitIsolatedConfig * add unit test for default python configuration --------- Co-authored-by: Daniel Jacobs --- include/pybind11/embed.h | 7 ++++--- tests/test_embed/test_interpreter.cpp | 22 +++++++++++++++++++++- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/include/pybind11/embed.h b/include/pybind11/embed.h index 749c75beb..5a175b134 100644 --- a/include/pybind11/embed.h +++ b/include/pybind11/embed.h @@ -198,9 +198,10 @@ inline void initialize_interpreter(bool init_signal_handlers = true, init_signal_handlers, argc, argv, add_program_dir_to_path); #else PyConfig config; - PyConfig_InitIsolatedConfig(&config); - config.isolated = 0; - config.use_environment = 1; + PyConfig_InitPythonConfig(&config); + // See PR #4473 for background + config.parse_argv = 0; + config.install_signal_handlers = init_signal_handlers ? 1 : 0; initialize_interpreter(&config, argc, argv, add_program_dir_to_path); #endif diff --git a/tests/test_embed/test_interpreter.cpp b/tests/test_embed/test_interpreter.cpp index 10b20f371..e54e82270 100644 --- a/tests/test_embed/test_interpreter.cpp +++ b/tests/test_embed/test_interpreter.cpp @@ -184,7 +184,7 @@ TEST_CASE("Custom PyConfig") { py::initialize_interpreter(); } -TEST_CASE("Custom PyConfig with argv") { +TEST_CASE("scoped_interpreter with PyConfig_InitIsolatedConfig and argv") { py::finalize_interpreter(); { PyConfig config; @@ -199,6 +199,26 @@ TEST_CASE("Custom PyConfig with argv") { } py::initialize_interpreter(); } + +TEST_CASE("scoped_interpreter with PyConfig_InitPythonConfig and argv") { + py::finalize_interpreter(); + { + PyConfig config; + PyConfig_InitPythonConfig(&config); + + // `initialize_interpreter() overrides the default value for config.parse_argv (`1`) by + // changing it to `0`. This test exercises `scoped_interpreter` with the default config. + char *argv[] = {strdup("a.out"), strdup("arg1")}; + py::scoped_interpreter argv_scope(&config, 2, argv); + std::free(argv[0]); + std::free(argv[1]); + auto module = py::module::import("test_interpreter"); + auto py_widget = module.attr("DerivedWidget")("The question"); + const auto &cpp_widget = py_widget.cast(); + REQUIRE(cpp_widget.argv0() == "arg1"); + } + py::initialize_interpreter(); +} #endif TEST_CASE("Add program dir to path pre-PyConfig") {