MCPcopy Create free account
hub / github.com/FreeCAD/FreeCAD / runStringWithKey

Method runStringWithKey

src/Base/Interpreter.cpp:282–315  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

280 */
281
282std::string InterpreterSingleton::runStringWithKey(
283 const char* psCmd,
284 const char* key,
285 const char* key_initial_value
286)
287{
288 PyGILStateLocker locker;
289 Py::Module module("__main__");
290 Py::Dict globalDictionary = module.getDict();
291 Py::Dict localDictionary;
292 Py::String initial_value(key_initial_value);
293 localDictionary.setItem(key, initial_value);
294
295 PyObject* presult
296 = PyRun_String(psCmd, Py_file_input, globalDictionary.ptr(), localDictionary.ptr());
297 if (!presult) {
298 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
299 throw SystemExitException();
300 }
301
302 PyException::throwException();
303 return {}; // just to quieten code analyzers
304 }
305 Py_DECREF(presult);
306
307 Py::Object key_return_value = localDictionary.getItem(key);
308 if (!key_return_value.isString()) {
309 key_return_value = key_return_value.str(); // NOLINT
310 }
311
312 Py::Bytes str = Py::String(key_return_value).encode("utf-8");
313 std::string result = static_cast<std::string>(str);
314 return result;
315}
316
317Py::Object InterpreterSingleton::runStringObject(const char* sCmd)
318{

Callers 1

WorkbenchMethod · 0.80

Calls 7

SystemExitExceptionFunction · 0.85
getDictMethod · 0.80
encodeMethod · 0.80
StringClass · 0.70
ptrMethod · 0.45
getItemMethod · 0.45
strMethod · 0.45

Tested by

no test coverage detected