Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

embed.h Python 3.11 config.use_environment=1 + PYTHONPATH test #4119

Merged
merged 10 commits into from
Aug 21, 2022
2 changes: 2 additions & 0 deletions include/pybind11/embed.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ inline void initialize_interpreter(bool init_signal_handlers = true,
#else
PyConfig config;
PyConfig_InitIsolatedConfig(&config);
config.isolated = 0;
config.use_environment = 1;
config.install_signal_handlers = init_signal_handlers ? 1 : 0;

PyStatus status = PyConfig_SetBytesArgv(&config, argc, const_cast<char *const *>(argv));
Expand Down
12 changes: 12 additions & 0 deletions tests/test_embed/test_interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,18 @@ bool has_pybind11_internals_static() {

TEST_CASE("Restart the interpreter") {
// Verify pre-restart state.
fprintf(stdout,
"\nLOOOK PYTHONPATH %s\n",
py::module_::import("os")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be cleaner to just use the py::print API here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is/was just for debugging, I'll remove this before taking this PR out of draft mode, after adding a unit test for PYTHONPATH processing.

I think py::print could do indirections. For debugging I generally try to go straight down to the bare metal.

.attr("environ")
.attr("get")("PYTHONPATH")
.attr("__str__")()
.cast<std::string>()
.c_str());
fflush(stdout);
auto sys_path = py::module_::import("sys").attr("path").attr("__str__")().cast<std::string>();
fprintf(stdout, "\nLOOOK sys.path %s\n", sys_path.c_str());
fflush(stdout);
REQUIRE(py::module_::import("widget_module").attr("add")(1, 2).cast<int>() == 3);
REQUIRE(has_pybind11_internals_builtin());
REQUIRE(has_pybind11_internals_static());
Expand Down