OGS
ApplicationsLib Namespace Reference

Namespaces

namespace  anonymous_namespace{ogs_embedded_python.cpp}
namespace  detail

Classes

struct  LinearSolverLibrarySetup
class  TestDefinition

Functions

pybind11::scoped_interpreter setupEmbeddedPython ()
void setupEmbeddedPythonVenvPaths ()

Function Documentation

◆ setupEmbeddedPython()

OGS_EXPORT_SYMBOL pybind11::scoped_interpreter ApplicationsLib::setupEmbeddedPython ( )

Sets up an embedded Python interpreter and makes sure that the OpenGeoSys Python module is not removed by the linker.

Definition at line 34 of file ogs_embedded_python.cpp.

35{
36 // Allows ogs to be interrupted by SIGINT, which otherwise is handled by
37 // python. See
38 // https://docs.python.org/3/c-api/exceptions.html#c.PyErr_CheckSignals and
39 // https://pybind11.readthedocs.io/en/stable/faq.html#how-can-i-properly-handle-ctrl-c-in-long-running-functions
40 constexpr bool init_signal_handlers = false;
41 return pybind11::scoped_interpreter{init_signal_handlers};
42}

Referenced by main().

◆ setupEmbeddedPythonVenvPaths()

OGS_EXPORT_SYMBOL void ApplicationsLib::setupEmbeddedPythonVenvPaths ( )

Checks for activated and matching virtual environment and adds it to sys.path.

Definition at line 235 of file ogs_embedded_python.cpp.

236{
237 namespace py = pybind11;
238 namespace fs = std::filesystem;
239
240 // Get embedded Python version
241 py::object const version_info =
242 py::module_::import("sys").attr("version_info");
243 int const emb_major = version_info.attr("major").cast<int>();
244 int const emb_minor = version_info.attr("minor").cast<int>();
245
246 // Check for virtual environment
247 char const* const venv = std::getenv("VIRTUAL_ENV");
248 if (venv == nullptr)
249 {
250 DBUG("No virtual environment detected (VIRTUAL_ENV not set).");
251 return;
252 }
253
254 fs::path const venv_path(venv);
255 DBUG("Virtual environment detected at: {}", venv_path.string());
256
257 // Find and validate site-packages path for the embedded interpreter
258 // version.
259 fs::path const site_packages =
260 findSitePackagesPath(venv_path, emb_major, emb_minor);
261 INFO("Using virtual environment site-packages: {}", site_packages.string());
262
263 // Add to sys.path (insert at beginning for highest priority)
264 py::list sys_path = py::module_::import("sys").attr("path");
265 sys_path.insert(0, py::str(site_packages.string()));
266}
void INFO(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:28
void DBUG(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:22

References DBUG(), and INFO().

Referenced by main().