| 280 | */ |
| 281 | |
| 282 | std::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 | |
| 317 | Py::Object InterpreterSingleton::runStringObject(const char* sCmd) |
| 318 | { |