Skip to content

Commit 68e6fda

Browse files
rwgkSkylion007
andauthored
embed.h Python 3.11 config.use_environment=1 + PYTHONPATH test (#4119)
* Add debug fprintf to test_interpreter.cpp * Update `sys.path` from `PYTHONPATH` in Python >= 3.11 branch of `initialize_interpreter()` * Use `config.isolated = 0; config.use_environment = 1;` As suggsted by @vstinner here: #4119 (comment) * Add `TEST_CASE("PYTHONPATH is used to update sys.path")` * Fix clang-tidy error. * Use `_putenv_s()` under Windows. * Fix clang-tidy error: argument name ... in comment does not match parameter name * Remove slash from PYTHONPATH addition, to work around Windows slash-vs-backslash issue. * Use `py::str(...)` instead of `.attr("__str__")` as suggested by @Skylion007 Co-authored-by: Aaron Gokaslan <[email protected]> Co-authored-by: Aaron Gokaslan <[email protected]>
1 parent 81f35d2 commit 68e6fda

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

include/pybind11/embed.h

+2
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,8 @@ inline void initialize_interpreter(bool init_signal_handlers = true,
150150
#else
151151
PyConfig config;
152152
PyConfig_InitIsolatedConfig(&config);
153+
config.isolated = 0;
154+
config.use_environment = 1;
153155
config.install_signal_handlers = init_signal_handlers ? 1 : 0;
154156

155157
PyStatus status = PyConfig_SetBytesArgv(&config, argc, const_cast<char *const *>(argv));

tests/test_embed/catch.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,25 @@
2020
namespace py = pybind11;
2121

2222
int main(int argc, char *argv[]) {
23+
// Setup for TEST_CASE in test_interpreter.cpp, tagging on a large random number:
24+
std::string updated_pythonpath("pybind11_test_embed_PYTHONPATH_2099743835476552");
25+
const char *preexisting_pythonpath = getenv("PYTHONPATH");
26+
if (preexisting_pythonpath != nullptr) {
27+
#if defined(_WIN32)
28+
updated_pythonpath += ';';
29+
#else
30+
updated_pythonpath += ':';
31+
#endif
32+
updated_pythonpath += preexisting_pythonpath;
33+
}
34+
#if defined(_WIN32)
35+
_putenv_s("PYTHONPATH", updated_pythonpath.c_str());
36+
#else
37+
setenv("PYTHONPATH", updated_pythonpath.c_str(), /*replace=*/1);
38+
#endif
39+
2340
py::scoped_interpreter guard{};
41+
2442
auto result = Catch::Session().run(argc, argv);
2543

2644
return result < 0xff ? result : 0xff;

tests/test_embed/test_interpreter.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,13 @@ PYBIND11_EMBEDDED_MODULE(throw_error_already_set, ) {
7575
d["missing"].cast<py::object>();
7676
}
7777

78+
TEST_CASE("PYTHONPATH is used to update sys.path") {
79+
// The setup for this TEST_CASE is in catch.cpp!
80+
auto sys_path = py::str(py::module_::import("sys").attr("path")).cast<std::string>();
81+
REQUIRE_THAT(sys_path,
82+
Catch::Matchers::Contains("pybind11_test_embed_PYTHONPATH_2099743835476552"));
83+
}
84+
7885
TEST_CASE("Pass classes and data between modules defined in C++ and Python") {
7986
auto module_ = py::module_::import("test_interpreter");
8087
REQUIRE(py::hasattr(module_, "DerivedWidget"));

0 commit comments

Comments
 (0)