Skip to content

Commit 7fdcd00

Browse files
author
Daniel Jacobs
committed
add unit test for default python configuration
1 parent 0ed72f4 commit 7fdcd00

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

tests/test_embed/test_interpreter.cpp

+21-1
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ TEST_CASE("Custom PyConfig") {
184184
py::initialize_interpreter();
185185
}
186186

187-
TEST_CASE("Custom PyConfig with argv") {
187+
TEST_CASE("scoped_interpreter with PyConfig_InitIsolatedConfig and argv") {
188188
py::finalize_interpreter();
189189
{
190190
PyConfig config;
@@ -199,6 +199,26 @@ TEST_CASE("Custom PyConfig with argv") {
199199
}
200200
py::initialize_interpreter();
201201
}
202+
203+
TEST_CASE("scoped_interpreter with PyConfig_InitPythonConfig and argv") {
204+
py::finalize_interpreter();
205+
{
206+
PyConfig config;
207+
PyConfig_InitPythonConfig(&config);
208+
209+
// `initialize_interpreter() overrides the default value for config.parse_argv (`1`) by
210+
// changing it to `0`. This test exercises `scoped_interpreter` with the default config.
211+
char *argv[] = {strdup("a.out"), strdup("arg1")};
212+
py::scoped_interpreter argv_scope(&config, 2, argv);
213+
std::free(argv[0]);
214+
std::free(argv[1]);
215+
auto module = py::module::import("test_interpreter");
216+
auto py_widget = module.attr("DerivedWidget")("The question");
217+
const auto &cpp_widget = py_widget.cast<const Widget &>();
218+
REQUIRE(cpp_widget.argv0() == "arg1");
219+
}
220+
py::initialize_interpreter();
221+
}
202222
#endif
203223

204224
TEST_CASE("Add program dir to path pre-PyConfig") {

0 commit comments

Comments
 (0)