@@ -14,6 +14,11 @@ PYBIND11_WARNING_DISABLE_MSVC(4996)
1414namespace py = pybind11;
1515using namespace py ::literals;
1616
17+ size_t get_sys_path_size () {
18+ auto sys_path = py::module::import (" sys" ).attr (" path" );
19+ return py::len (sys_path);
20+ }
21+
1722class Widget {
1823public:
1924 explicit Widget (std::string message) : message(std::move(message)) {}
@@ -196,41 +201,39 @@ TEST_CASE("Custom PyConfig with argv") {
196201}
197202#endif
198203
199- TEST_CASE (" Add program dir to path" ) {
200- static auto get_sys_path_size = []() -> size_t {
201- auto sys_path = py::module::import (" sys" ).attr (" path" );
202- return py::len (sys_path);
203- };
204- static auto validate_path_len = [](size_t default_len) {
205- #if PY_VERSION_HEX < 0x030A0000
206- // It seems a value remains in sys.path
207- // left by the previous call of scoped_interpreter ctor.
208- REQUIRE (get_sys_path_size () > default_len);
209- #else
210- REQUIRE (get_sys_path_size () == default_len + 1 );
211- #endif
212- };
204+ TEST_CASE (" Add program dir to path pre-PyConfig" ) {
213205 py::finalize_interpreter ();
214-
215- size_t sys_path_default_size = 0 ;
206+ size_t path_size_add_program_dir_to_path_false = 0 ;
216207 {
217208 py::scoped_interpreter scoped_interp{true , 0 , nullptr , false };
218- sys_path_default_size = get_sys_path_size ();
209+ path_size_add_program_dir_to_path_false = get_sys_path_size ();
219210 }
220211 {
221- py::scoped_interpreter scoped_interp{}; // expected to append some to sys.path
222- validate_path_len (sys_path_default_size );
212+ py::scoped_interpreter scoped_interp{};
213+ REQUIRE ( get_sys_path_size () == path_size_add_program_dir_to_path_false + 1 );
223214 }
215+ py::initialize_interpreter ();
216+ }
217+
224218#if PY_VERSION_HEX >= PYBIND11_PYCONFIG_SUPPORT_PY_VERSION_HEX
219+ TEST_CASE (" Add program dir to path using PyConfig" ) {
220+ py::finalize_interpreter ();
221+ size_t path_size_add_program_dir_to_path_false = 0 ;
225222 {
226223 PyConfig config;
227224 PyConfig_InitPythonConfig (&config);
228- py::scoped_interpreter scoped_interp{&config}; // expected to append some to sys.path
229- validate_path_len (sys_path_default_size);
225+ py::scoped_interpreter scoped_interp{&config, 0 , nullptr , false };
226+ path_size_add_program_dir_to_path_false = get_sys_path_size ();
227+ }
228+ {
229+ PyConfig config;
230+ PyConfig_InitPythonConfig (&config);
231+ py::scoped_interpreter scoped_interp{&config};
232+ REQUIRE (get_sys_path_size () == path_size_add_program_dir_to_path_false + 1 );
230233 }
231- #endif
232234 py::initialize_interpreter ();
233235}
236+ #endif
234237
235238bool has_pybind11_internals_builtin () {
236239 auto builtins = py::handle (PyEval_GetBuiltins ());
0 commit comments