Skip to content

Commit 211694d

Browse files
author
Daniel Jacobs
committed
add unit test for default python configuration
1 parent b91d98e commit 211694d

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

tests/test_embed/test_interpreter.cpp

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

187+
# include <iostream>
188+
187189
TEST_CASE("Custom PyConfig with argv") {
188190
py::finalize_interpreter();
189191
{
@@ -197,6 +199,22 @@ TEST_CASE("Custom PyConfig with argv") {
197199
const auto &cpp_widget = py_widget.cast<const Widget &>();
198200
REQUIRE(cpp_widget.argv0() == "a.out");
199201
}
202+
203+
{
204+
PyConfig config;
205+
PyConfig_InitPythonConfig(&config);
206+
207+
// Parse arguments the way python normally does, and strip the first argument (which is
208+
// expected to be "python")
209+
char *argv[] = {strdup("python"), strdup("a.out")};
210+
py::scoped_interpreter argv_scope(&config, 2, argv);
211+
std::free(argv[0]);
212+
std::free(argv[1]);
213+
auto module = py::module::import("test_interpreter");
214+
auto py_widget = module.attr("DerivedWidget")("The question");
215+
const auto &cpp_widget = py_widget.cast<const Widget &>();
216+
REQUIRE(cpp_widget.argv0() == "a.out");
217+
}
200218
py::initialize_interpreter();
201219
}
202220
#endif

0 commit comments

Comments
 (0)