MCPcopy Create free account
hub / github.com/PlotJuggler/PlotJuggler / ensureInterpreter

Function ensureInterpreter

pj_scripting/src/python_engine.cpp:30–51  ·  view source on GitHub ↗

Lazily start the one process-wide CPython interpreter. Both the guard and the GIL-release are intentionally leaked: finalizing embedded CPython at static destruction is fragile, and releasing the GIL lets every entry point re-acquire it with py::gil_scoped_acquire from whatever thread calls.

Source from the content-addressed store, hash-verified

28// destruction is fragile, and releasing the GIL lets every entry point re-acquire
29// it with py::gil_scoped_acquire from whatever thread calls.
30void ensureInterpreter() {
31 static const bool inited = []() {
32#ifdef PJ_PYTHON_HOME
33 // The embedded interpreter must find the stdlib (encodings, etc.) at startup.
34 // Point it at the CPython prefix baked at build time, unless the user already
35 // set PYTHONHOME. Dev-oriented: deployment would bundle Python.
36 // setenv is POSIX-only (MSVC lacks it); use _putenv_s on Windows. Set only if
37 // absent, mirroring setenv's overwrite=0 — sdk::getEnv is the portable read.
38 if (!sdk::getEnv("PYTHONHOME")) {
39#ifdef _WIN32
40 _putenv_s("PYTHONHOME", PJ_PYTHON_HOME);
41#else
42 ::setenv("PYTHONHOME", PJ_PYTHON_HOME, 1);
43#endif
44 }
45#endif
46 new py::scoped_interpreter(); // NOLINT: leaked on purpose (never finalize)
47 new py::gil_scoped_release(); // NOLINT: leaked on purpose (GIL handed to callers)
48 return true;
49 }();
50 (void)inited;
51}
52
53std::string attrString(const py::object& obj, const char* name, const std::string& fallback) {
54 if (!py::hasattr(obj, name)) {

Callers 2

inspectModuleMethod · 0.85
createInstanceMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected