Skip to content

Commit 93d02ee

Browse files
committed
tests: Add program dir to path, Custom PyConfig with argv
1 parent b94d6ba commit 93d02ee

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

tests/test_embed/test_interpreter.cpp

+39-1
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,44 @@ TEST_CASE("Custom PyConfig") {
180180
}
181181
py::initialize_interpreter();
182182
}
183+
184+
TEST_CASE("Custom PyConfig with argv") {
185+
py::finalize_interpreter();
186+
{
187+
PyConfig config;
188+
PyConfig_InitPythonConfig(&config);
189+
config.install_signal_handlers = 1;
190+
char *argv[] = {strdup("a.out")};
191+
py::scoped_interpreter argv_scope{&config, 1, argv};
192+
std::free(argv[0]);
193+
auto module = py::module::import("test_interpreter");
194+
auto py_widget = module.attr("DerivedWidget")("The question");
195+
const auto &cpp_widget = py_widget.cast<const Widget &>();
196+
REQUIRE(cpp_widget.argv0() == "");
197+
}
198+
py::initialize_interpreter();
199+
200+
}
201+
202+
TEST_CASE("Add program dir to path") {
203+
static auto get_sys_path_size = []() -> size_t {
204+
auto sys_path = py::module::import("sys").attr("path");
205+
return py::len(sys_path);
206+
};
207+
py::finalize_interpreter();
208+
PyConfig config;
209+
PyConfig_InitPythonConfig(&config);
210+
size_t sys_path_default_size;
211+
{
212+
py::scoped_interpreter scoped_interp{&config, 0, nullptr, false};
213+
sys_path_default_size = get_sys_path_size();
214+
}
215+
{
216+
py::scoped_interpreter scoped_interp{&config}; // expected to append 1 elem to sys.path
217+
REQUIRE(get_sys_path_size() == sys_path_default_size + 1);
218+
}
219+
py::initialize_interpreter();
220+
}
183221
#endif
184222

185223
bool has_pybind11_internals_builtin() {
@@ -412,4 +450,4 @@ TEST_CASE("make_iterator can be called before then after finalizing an interpret
412450
}());
413451

414452
py::initialize_interpreter();
415-
}
453+
}

0 commit comments

Comments
 (0)