SetupLocalPythonEnvironment.hpp
1 // Distributed under the MIT License.
2 // See LICENSE.txt for details.
3 
4 #pragma once
5 
6 #include <cstddef> // IWYU pragma: keep
7 #include <patchlevel.h> // for PY_MAJOR_VERSION
8 #include <string>
9 
10 /// Contains all functions for pypp
11 namespace pypp {
12 /// Enable calling of python in the local scope, and add directory(ies) to the
13 /// front of the search path for modules. The directory which is appended to the
14 /// path is relative to the `tests/Unit` directory.
17  const std::string& cur_dir_relative_to_unit_test_path);
18 
19  ~SetupLocalPythonEnvironment() = default;
20 
23  delete;
26  delete;
27 
28  /// \cond
29  // In the case where we run all the non-failure tests at once we must ensure
30  // that we only initialize and finalize the python env once. Initialization is
31  // done in the constructor of SetupLocalPythonEnvironment, while finalization
32  // is done in the constructor of RunTests.
33  static void finalize_env();
34  /// \endcond
35 
36  private:
37 // In order to use NumPy's API, import_array() must be called. However it is a
38 // macro which contains a return statement, returning NULL in python 3 and void
39 // in python 2. As such it needs to be factored into its own function which
40 // returns either nullptr or void depending on the version.
41 #if PY_MAJOR_VERSION == 3
42  std::nullptr_t init_numpy();
43 #else
44  void init_numpy();
45 #endif
46  static bool initialized;
47  static bool finalized;
48 };
49 } // namespace pypp
Contains all functions for calling python from C++.
Definition: CheckWithRandomValues.hpp:59
Enable calling of python in the local scope, and add directory(ies) to the front of the search path f...
Definition: SetupLocalPythonEnvironment.hpp:15