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