GCC Code Coverage Report


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 65.5% 127 / 0 / 194
Functions: 77.3% 17 / 0 / 22
Branches: 51.3% 81 / 0 / 158

python/engine/PythonEngine.cpp
Line Branch Exec Source
1 /***********************************************************************************************************************
2 * OpenStudio(R), Copyright (c) Alliance for Energy Innovation, LLC.
3 * See also https://openstudio.net/license
4 ***********************************************************************************************************************/
5
6 #include "PythonEngine.hpp"
7 #include <utilities/core/ApplicationPathHelpers.hpp>
8 #include "../../src/utilities/core/Filesystem.hpp"
9 #include <fmt/format.h>
10
11 #include <stdexcept>
12 #include <string>
13 #include <type_traits>
14
15 #ifdef __GNUC__
16 # pragma GCC diagnostic push
17 # pragma GCC diagnostic ignored "-Wmissing-field-initializers"
18 #endif
19
20 #include <PythonConfig.hxx>
21 #include <Python.h>
22 #include <SWIGPythonRuntime.hxx>
23
24 #ifdef __GNUC__
25 # pragma GCC diagnostic pop
26 #endif
27
28 // >= 3.13.0
29 // #if PY_VERSION_HEX >= 0x30d0000
30 #if PY_MAJOR_VERSION > 3 || (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION > 12)
31 extern "C"
32 {
33 // These functions was removed from Python 3.13 API but are still exported
34 // for the stable ABI. Until we upgrade to PyConfig, just keep using it
35 extern void Py_SetPath(const wchar_t* path);
36 }
37 #endif
38
39 namespace openstudio {
40
41 73 void addToPythonPath(const openstudio::path& includePath) {
42
43
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 73 times.
73 if (includePath.empty()) {
44 ✗ return;
45 }
46
47 // fmt::print("Prepending '{}' to sys.path\n", includePath);
48
49 73 PyObject* unicodeIncludePath = nullptr;
50 if constexpr (std::is_same_v<typename openstudio::path::value_type, wchar_t>) {
51 const std::wstring ws = includePath.generic_wstring();
52 unicodeIncludePath = PyUnicode_FromWideChar(ws.c_str(), static_cast<Py_ssize_t>(ws.size())); // New reference
53 } else {
54
1/1
✓ Branch 1 taken 73 times.
73 const std::string s = includePath.generic_string();
55
1/1
✓ Branch 2 taken 73 times.
73 unicodeIncludePath = PyUnicode_FromString(s.c_str()); // New reference
56 73 }
57
58
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 73 times.
73 if (unicodeIncludePath == nullptr) {
59 ✗ throw std::runtime_error(fmt::format("Unable to convert path '{}' for addition to sys.path in Python", includePath.generic_string()));
60 }
61
62 73 PyObject* sysPath = PySys_GetObject("path"); // Borrowed reference
63 73 int ret = PyList_Insert(sysPath, 0, unicodeIncludePath);
64 Py_DECREF(unicodeIncludePath);
65
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 73 times.
73 if (ret != 0) {
66 ✗ throw std::runtime_error(fmt::format("Unable to add path '{}' to the sys.path in Python", includePath.generic_string()));
67 }
68 }
69
70 35 void PythonEngine::pyimport(const std::string& importName, const std::string& includePath) {
71
2/2
✓ Branch 1 taken 35 times.
✓ Branch 4 taken 35 times.
35 addToPythonPath(includePath);
72 35 PyImport_ImportModule(importName.c_str());
73 35 }
74
75 29 void PythonEngine::setupPythonPath(const std::vector<openstudio::path>& includeDirs) {
76
77 // Iterate in reverse order since addToPythonPath always inserts at pos 0
78 // --python_path path1 --python_path path2 => includeDirs = ["path1", "path2"]
79 // std::ranges::reverse_view needs modern compilers
80
2/2
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 29 times.
31 for (auto it = includeDirs.rbegin(); it != includeDirs.rend(); it++) {
81
1/1
✓ Branch 2 taken 2 times.
2 addToPythonPath(*it);
82 }
83 29 }
84
85
1/1
✓ Branch 2 taken 35 times.
35 PythonEngine::PythonEngine(int argc, char* argv[]) : ScriptEngine(argc, argv), program(Py_DecodeLocale(pythonProgramName, nullptr)) {
86 // TODO: modernize and use PyConfig (new in 3.8): https://docs.python.org/3/c-api/init_config.html
87
88 // this frozen flag tells Python that the package and library have been frozen for embedding, so it shouldn't warn about missing prefixes
89 35 Py_FrozenFlag = 1;
90
91 // Path to the E+ shipped standard library
92
1/1
✓ Branch 1 taken 35 times.
70 auto pathToPythonPackages = getEnergyPlusDirectory() / "python_lib";
93
94 // The PYTHONPATH / PYTHONHOME should be set before initializing Python
95 // If this Py_SetPath is called before Py_Initialize, then Py_GetPath won't attempt to compute a default search path
96 // The default search path is affected by the Py_SetPythonHome
97 // * if the user passed --python_home, we use that as the Python Home, and do not use Py_SetPath. But later we add the E+ standard_lib anyways
98 // so it takes precedence (to limit incompatibility issues...)
99 // * If the user didn't pass it, we use Py_SetPath set to the E+ standard_lib
100
101
1/1
✓ Branch 1 taken 35 times.
70 std::vector<std::string> args(argv, std::next(argv, static_cast<std::ptrdiff_t>(argc)));
102 35 bool pythonHomePassed = false;
103
1/1
✓ Branch 3 taken 35 times.
35 auto it = std::find(args.cbegin(), args.cend(), "--python_home");
104
2/2
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 34 times.
35 if (it != args.cend()) {
105
1/1
✓ Branch 2 taken 1 time.
1 openstudio::path pythonHomeDir(*std::next(it));
106
1/1
✓ Branch 3 taken 1 time.
1 wchar_t* h = Py_DecodeLocale(pythonHomeDir.make_preferred().string().c_str(), nullptr);
107
1/1
✓ Branch 1 taken 1 time.
1 Py_SetPythonHome(h);
108 1 pythonHomePassed = true;
109 1 } else {
110
1/1
✓ Branch 3 taken 34 times.
34 wchar_t* a = Py_DecodeLocale(pathToPythonPackages.make_preferred().string().c_str(), nullptr);
111
1/1
✓ Branch 1 taken 34 times.
34 Py_SetPath(a);
112 }
113
114
1/1
✓ Branch 1 taken 35 times.
35 Py_SetProgramName(program); // optional but recommended
115
116
1/1
✓ Branch 1 taken 35 times.
35 Py_Initialize();
117
118
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 34 times.
35 if (pythonHomePassed) {
119
1/1
✓ Branch 1 taken 1 time.
1 addToPythonPath(pathToPythonPackages);
120 }
121 #if defined(__APPLE__) || defined(__linux___) || defined(__unix__)
122
2/2
✓ Branch 1 taken 35 times.
✓ Branch 4 taken 35 times.
70 addToPythonPath(pathToPythonPackages / "lib-dynload");
123 #endif
124
125
1/1
✓ Branch 1 taken 35 times.
35 PyObject* m = PyImport_AddModule("__main__");
126
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 35 times.
35 if (m == nullptr) {
127 ✗ throw std::runtime_error("Unable to add module __main__ for python script execution");
128 }
129
1/1
✓ Branch 1 taken 35 times.
35 m_globalDict = PyModule_GetDict(m);
130
131 //PyRun_SimpleString("from time import time,ctime\n"
132 // "print('Today is', ctime(time()))\n");
133
1/1
✓ Branch 1 taken 35 times.
35 importOpenStudio();
134 35 }
135
136 68 PythonEngine::~PythonEngine() {
137
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 34 times.
34 if (Py_FinalizeEx() < 0) {
138 ✗ exit(120);
139 }
140 34 PyMem_RawFree(program);
141 68 }
142
143 35 void PythonEngine::importOpenStudio() {
144 #if defined(__APPLE__)
145 // RTLD_LOCAL is import an Apple so that that Python and Ruby do not conflict
146 const std::string set_dlflags_cmd = R"(
147 import sys
148 import os
149 pre_os_dl_open_flags = sys.getdlopenflags()
150 sys.setdlopenflags(os.RTLD_LOCAL)
151 )";
152
153 exec(set_dlflags_cmd.c_str());
154 #endif
155
156 // generic_string() converts to a POSIX path, with forward slashes, so that pyimport doesn't choke on backslashes understood as escape sequence
157
1/2
✓ Branch 1 taken 35 times.
✗ Branch 2 not taken.
35 if (moduleIsRunningFromBuildDirectory()) {
158
1/1
✓ Branch 1 taken 35 times.
35 const auto bindingsDir = getOpenStudioModuleDirectory();
159
3/3
✓ Branch 1 taken 35 times.
✓ Branch 4 taken 35 times.
✓ Branch 7 taken 35 times.
105 pyimport("openstudiodev", bindingsDir.generic_string());
160 35 } else {
161 ✗ const auto bindingsDir = getOpenStudioModuleDirectory() / "../Python";
162 ✗ pyimport("openstudio", bindingsDir.generic_string());
163 ✗ }
164 // Somehow that doesn't suffice to register it...
165
1/1
✓ Branch 2 taken 35 times.
35 exec("import openstudio");
166
167 #if defined(__APPLE__)
168 // Reset the dlopen flags to the value prior to importOpenStudio
169 const std::string reset_dlflags_cmd = R"(
170 sys.setdlopenflags(pre_os_dl_open_flags)
171 )";
172
173 exec(reset_dlflags_cmd.c_str());
174 #endif
175 35 }
176
177 struct PythonObject
178 {
179 PythonObject() = default;
180
181 20 PythonObject(PyObject* obj) noexcept : obj_(obj) {
182
1/2
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
20 if (obj_) {
183
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 Py_INCREF(obj_);
184 }
185 20 }
186
187 52 PythonObject(const PythonObject& other) noexcept : obj_(other.obj_) {
188
1/2
✓ Branch 0 taken 52 times.
✗ Branch 1 not taken.
52 if (obj_) {
189
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 52 times.
52 Py_INCREF(obj_);
190 }
191 52 }
192
193 20 PythonObject(PythonObject&& other) noexcept : obj_(other.obj_) {
194 // no reason to inc/dec, we just stole the ref counted object
195 // from other
196 20 other.obj_ = nullptr;
197 20 }
198
199 PythonObject& operator=(const PythonObject& rhs) noexcept {
200 if (&rhs != this) {
201 obj_ = rhs.obj_;
202
203 if (obj_) {
204 Py_INCREF(obj_);
205 }
206 }
207
208 return *this;
209 }
210
211 PythonObject& operator=(PythonObject&& rhs) noexcept {
212 if (&rhs != this) {
213 obj_ = rhs.obj_;
214 rhs.obj_ = nullptr;
215 }
216
217 return *this;
218 }
219
220 92 ~PythonObject() {
221
2/2
✓ Branch 0 taken 72 times.
✓ Branch 1 taken 20 times.
92 if (obj_) {
222 72 Py_DECREF(obj_);
223 }
224 92 }
225
226 PyObject* obj_ = nullptr;
227 };
228
229 78 void PythonEngine::exec(std::string_view sv) {
230
1/1
✓ Branch 1 taken 78 times.
78 std::string command{sv};
231
232
1/1
✓ Branch 2 taken 78 times.
78 PyObject* v = PyRun_String(command.c_str(), Py_file_input, m_globalDict, m_globalDict);
233 // PyObject* v = PyRun_SimpleString(command.c_str());
234
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 77 times.
78 if (v == nullptr) {
235
0/1
✗ Branch 1 not taken.
1 PyErr_Print();
236 ✗ throw std::runtime_error("Error executing Python code");
237 }
238
239 Py_DECREF(v);
240 77 }
241
242 20 ScriptObject PythonEngine::eval(std::string_view sv) {
243
1/1
✓ Branch 1 taken 20 times.
20 std::string command{sv};
244
245
1/1
✓ Branch 2 taken 20 times.
20 PyObject* v = PyRun_String(command.c_str(), Py_eval_input, m_globalDict, m_globalDict);
246
247
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 if (v == nullptr) {
248 ✗ PyErr_Print();
249 ✗ throw std::runtime_error("Error executing Python code");
250 }
251
252 // share in ownership
253 20 PythonObject return_value(v);
254
255 // decref count returned from Python
256 Py_DECREF(v);
257
258
1/1
✓ Branch 1 taken 20 times.
40 return ScriptObject{return_value};
259 20 }
260
261 18 void* PythonEngine::getAs_impl(ScriptObject& obj, const std::type_info& ti) {
262
263
1/1
✓ Branch 1 taken 18 times.
18 auto val = std::any_cast<PythonObject>(obj.object);
264
265 18 void* return_value = nullptr;
266
267
1/1
✓ Branch 1 taken 18 times.
18 const auto& type_name = getRegisteredTypeName(ti);
268
269
1/1
✓ Branch 2 taken 18 times.
18 auto* type = SWIG_Python_TypeQuery(type_name.c_str());
270
271
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
18 if (!type) {
272 ✗ throw std::runtime_error("Unable to find type in SWIG");
273 }
274
275
1/1
✓ Branch 1 taken 18 times.
18 const auto result = SWIG_Python_ConvertPtr(val.obj_, &return_value, type, 0);
276
277
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
18 if (!SWIG_IsOK(result)) {
278 ✗ throw std::runtime_error(fmt::format("Error getting object from SWIG/Python of supposed type {}, {}\n", ti.name(), type_name.c_str()));
279 }
280
281 18 return return_value;
282 18 }
283
284 ✗ bool PythonEngine::getAs_impl_bool(ScriptObject& obj) {
285 ✗ auto val = std::any_cast<PythonObject>(obj.object);
286 ✗ if (!PyBool_Check(val.obj_)) {
287 ✗ throw std::runtime_error("PyObject is not a bool");
288 }
289 ✗ return static_cast<bool>(PyObject_IsTrue(val.obj_));
290 ✗ }
291
292 ✗ int PythonEngine::getAs_impl_int(ScriptObject& obj) {
293 ✗ auto val = std::any_cast<PythonObject>(obj.object);
294 ✗ if (!PyLong_Check(val.obj_)) {
295 ✗ throw std::runtime_error("PyObject is not a PyLong");
296 }
297
298 ✗ return static_cast<int>(PyLong_AsLong(val.obj_));
299 ✗ }
300
301 ✗ double PythonEngine::getAs_impl_double(ScriptObject& obj) {
302 ✗ auto val = std::any_cast<PythonObject>(obj.object);
303 ✗ if (!PyFloat_Check(val.obj_)) {
304 ✗ throw std::runtime_error("PyObject is not a PyFloat");
305 }
306
307 ✗ return PyFloat_AsDouble(val.obj_);
308 ✗ }
309
310 ✗ std::string PythonEngine::getAs_impl_string(ScriptObject& obj) {
311 ✗ auto val = std::any_cast<PythonObject>(obj.object);
312
313 ✗ if (!PyUnicode_Check(val.obj_)) {
314 ✗ throw std::runtime_error("PyObject is not a String");
315 }
316
317 ✗ Py_ssize_t size = 0;
318 ✗ char const* pc = PyUnicode_AsUTF8AndSize(val.obj_, &size); // No decref needed
319 ✗ if (!pc) {
320 ✗ throw std::runtime_error("Unable to convert to std::string in SWIG Python");
321 }
322
323 ✗ return std::string{pc, static_cast<size_t>(size)};
324 ✗ }
325
326 ✗ std::string PythonEngine::inferMeasureClassName(const openstudio::path& measureScriptPath) {
327
328 auto inferClassNameCmd = fmt::format(R"python(
329 import importlib.util
330 import inspect
331 spec = importlib.util.spec_from_file_location(f"throwaway", "{}")
332
333 module = importlib.util.module_from_spec(spec)
334 spec.loader.exec_module(module)
335 class_members = inspect.getmembers(module, lambda x: inspect.isclass(x) and issubclass(x, openstudio.measure.OSMeasure))
336 assert len(class_members) == 1
337 measure_name, measure_typeinfo = class_members[0]
338 )python",
339 ✗ measureScriptPath.generic_string());
340
341 ✗ std::string className;
342 try {
343 ✗ exec(inferClassNameCmd);
344 ✗ ScriptObject measureClassNameObject = eval("measure_name");
345 ✗ className = getAs<std::string>(measureClassNameObject);
346 ✗ } catch (const std::runtime_error& e) {
347 ✗ auto msg = fmt::format("Failed to infer measure name from {}: {}", measureScriptPath.generic_string(), e.what());
348 ✗ fmt::print(stderr, "{}\n", msg);
349 ✗ }
350
351 ✗ return className;
352 ✗ }
353
354 // Ideally this would return a openstudio::measure::OSMeasure* or a shared_ptr<openstudio::measure::OSMeasure> but this poses memory management
355 // issue for the underlying ScriptObject (and VALUE or PyObject), so just return the ScriptObject
356 20 ScriptObject PythonEngine::loadMeasure(const openstudio::path& measureScriptPath, std::string_view className) {
357
358 20 ScriptObject result;
359
360 auto importCmd = fmt::format(R"python(
361 import importlib.util
362 spec = importlib.util.spec_from_file_location('{}', r'{}')
363 module = importlib.util.module_from_spec(spec)
364 spec.loader.exec_module(module)
365 )python",
366
1/1
✓ Branch 1 taken 20 times.
40 className, measureScriptPath.generic_string());
367
368 // fmt::print("\nimportCmd:\n{}\n", importCmd);
369 try {
370
1/1
✓ Branch 2 taken 20 times.
20 exec(importCmd);
371 ✗ } catch (const std::runtime_error& e) {
372 ✗ auto msg = fmt::format("Failed to load measure '{}' from '{}': {}", className, measureScriptPath.generic_string(), e.what());
373 ✗ fmt::print(stderr, "{}\n", msg);
374 ✗ }
375
376 try {
377
1/1
✓ Branch 3 taken 20 times.
40 result = eval(fmt::format("module.{}()", className));
378 ✗ } catch (const std::runtime_error& e) {
379 ✗ auto msg = fmt::format("Failed to instantiate measure '{}' from '{}': {}", className, measureScriptPath.generic_string(), e.what());
380 ✗ fmt::print(stderr, "{}\n", msg);
381 ✗ }
382
383 40 return result;
384 20 }
385
386 6 int PythonEngine::numberOfArguments(ScriptObject& methodObject, std::string_view methodName) {
387
388 6 int numberOfArguments = -1;
389
390
1/1
✓ Branch 1 taken 6 times.
6 auto val = std::any_cast<PythonObject>(methodObject.object);
391
2/3
✓ Branch 2 taken 6 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 6 times.
6 if (PyObject_HasAttrString(val.obj_, methodName.data()) == 0) {
392 // FAILED
393 ✗ return numberOfArguments;
394 }
395
396
1/1
✓ Branch 2 taken 6 times.
6 PyObject* method = PyObject_GetAttrString(val.obj_, methodName.data()); // New reference
397
1/2
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
6 if (PyMethod_Check(method)) {
398
1/1
✓ Branch 1 taken 6 times.
6 PyObject* func = PyMethod_Function(method); // Borrowed
399
2/3
✓ Branch 1 taken 6 times.
✓ Branch 3 taken 6 times.
✗ Branch 4 not taken.
6 if (auto* code = PyFunction_GetCode(func)) { // Borrowed
400 // cppcheck-suppress cstyleCast
401 6 const auto* co = (PyCodeObject*)code;
402 6 numberOfArguments = co->co_argcount - 1; // This includes `self`
403 }
404 ✗ } else if (PyFunction_Check(method)) {
405 // Shouldn't enter this block here
406 ✗ if (auto* code = PyFunction_GetCode(method)) {
407 // cppcheck-suppress cstyleCast
408 ✗ const auto* co = (PyCodeObject*)code;
409 ✗ numberOfArguments = co->co_argcount;
410 }
411 }
412 Py_DECREF(method);
413 6 return numberOfArguments;
414 6 }
415
416 8 bool PythonEngine::hasMethod(ScriptObject& methodObject, std::string_view methodName, bool overriden_only) {
417
1/1
✓ Branch 1 taken 8 times.
8 auto val = std::any_cast<PythonObject>(methodObject.object);
418
3/3
✓ Branch 2 taken 8 times.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 6 times.
8 if (PyObject_HasAttrString(val.obj_, methodName.data()) == 0) {
419 2 return false;
420 }
421
1/1
✓ Branch 2 taken 6 times.
6 PyObject* method = PyObject_GetAttrString(val.obj_, methodName.data()); // New reference
422
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
6 if (PyMethod_Check(method) == 0) {
423 // Should never happen with modelOutputRequests since the Base class (C++) has it
424 ✗ return false;
425 }
426 Py_DECREF(method);
427
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4 times.
6 if (!overriden_only) {
428 2 return true;
429 }
430
431 // equivalent to getattr(instance_obj.__class__, method_name) == getattr(instance_obj.__class__.__bases__[0], method_name)
432 4 PyTypeObject* class_type = Py_TYPE(val.obj_); // PyObject_Type returns a strong (New) reference, not needed for us
433 // cppcheck-suppress cstyleCast
434
1/1
✓ Branch 2 taken 4 times.
4 PyObject* class_method = PyObject_GetAttrString((PyObject*)class_type, methodName.data()); // New reference
435
436 assert(class_type->tp_base != nullptr);
437 // cppcheck-suppress cstyleCast
438 4 auto* base = (PyTypeObject*)class_type->tp_base;
439 // cppcheck-suppress cstyleCast
440
1/1
✓ Branch 2 taken 4 times.
4 PyObject* base_method = PyObject_GetAttrString((PyObject*)base, methodName.data()); // New reference
441
442 4 bool result = class_method != base_method;
443 Py_DECREF(class_method);
444 Py_DECREF(base_method);
445
446 4 return result;
447 8 }
448
449 } // namespace openstudio
450
451 extern "C"
452 {
453 35 openstudio::ScriptEngine* makeScriptEngine(int argc, char* argv[]) {
454
1/3
✓ Branch 2 taken 35 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
35 return new openstudio::PythonEngine(argc, argv);
455 }
456 }
457