@@ -14,6 +14,11 @@ PYBIND11_WARNING_DISABLE_MSVC(4996)
14
14
namespace py = pybind11;
15
15
using namespace py ::literals;
16
16
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
+
17
22
class Widget {
18
23
public:
19
24
explicit Widget (std::string message) : message(std::move(message)) {}
@@ -196,41 +201,39 @@ TEST_CASE("Custom PyConfig with argv") {
196
201
}
197
202
#endif
198
203
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" ) {
213
205
py::finalize_interpreter ();
214
-
215
- size_t sys_path_default_size = 0 ;
206
+ size_t path_size_add_program_dir_to_path_false = 0 ;
216
207
{
217
208
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 ();
219
210
}
220
211
{
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 );
223
214
}
215
+ py::initialize_interpreter ();
216
+ }
217
+
224
218
#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 ;
225
222
{
226
223
PyConfig config;
227
224
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 );
230
233
}
231
- #endif
232
234
py::initialize_interpreter ();
233
235
}
236
+ #endif
234
237
235
238
bool has_pybind11_internals_builtin () {
236
239
auto builtins = py::handle (PyEval_GetBuiltins ());
0 commit comments