@@ -198,17 +198,32 @@ TEST_CASE("Custom PyConfig") {
198
198
}
199
199
200
200
TEST_CASE (" scoped_interpreter with PyConfig_InitIsolatedConfig and argv" ) {
201
+ std::vector<std::string> path;
202
+ for (auto p : py::module::import (" sys" ).attr (" path" )) {
203
+ path.emplace_back (py::str (p));
204
+ }
205
+
201
206
py::finalize_interpreter ();
202
207
{
203
208
PyConfig config;
204
209
PyConfig_InitIsolatedConfig (&config);
205
210
char *argv[] = {strdup (" a.out" )};
206
- py::scoped_interpreter argv_scope{&config, 1 , argv};
211
+ py::scoped_interpreter argv_scope{&config, 1 , argv, true };
207
212
std::free (argv[0 ]);
208
- auto module = py::module::import (" test_interpreter" );
209
- auto py_widget = module .attr (" DerivedWidget" )(" The question" );
210
- const auto &cpp_widget = py_widget.cast <const Widget &>();
211
- REQUIRE (cpp_widget.argv0 () == " a.out" );
213
+ // Because this config is isolated, setting the path during init will not work, we have to
214
+ // set it manually. If we don't set it, then we can't import "test_interpreter"
215
+ for (auto &&p : path) {
216
+ py::list (py::module::import (" sys" ).attr (" path" )).append (p);
217
+ }
218
+ try {
219
+ auto module = py::module::import (" test_interpreter" );
220
+ auto py_widget = module .attr (" DerivedWidget" )(" The question" );
221
+ const auto &cpp_widget = py_widget.cast <const Widget &>();
222
+ REQUIRE (cpp_widget.argv0 () == " a.out" );
223
+ } catch (py::error_already_set &e) {
224
+ // catch here so that the exception doesn't escape the interpreter that owns it
225
+ FAIL (e.what ());
226
+ }
212
227
}
213
228
py::initialize_interpreter ();
214
229
}
0 commit comments