| 390 | } |
| 391 | |
| 392 | void InterpreterSingleton::runInteractiveString(const char* sCmd) |
| 393 | { |
| 394 | PyObject* module {}; |
| 395 | PyObject* dict {}; |
| 396 | PyObject* presult {}; |
| 397 | |
| 398 | PyGILStateLocker locker; |
| 399 | module = PP_Load_Module("__main__"); /* get module, init python */ |
| 400 | if (!module) { |
| 401 | throw PyException(); /* not incref'd */ |
| 402 | } |
| 403 | dict = PyModule_GetDict(module); /* get dict namespace */ |
| 404 | if (!dict) { |
| 405 | throw PyException(); /* not incref'd */ |
| 406 | } |
| 407 | |
| 408 | presult = PyRun_String(sCmd, Py_single_input, dict, dict); /* eval direct */ |
| 409 | if (!presult) { |
| 410 | if (PyErr_ExceptionMatches(PyExc_SystemExit)) { |
| 411 | throw SystemExitException(); |
| 412 | } |
| 413 | /* get latest python exception information */ |
| 414 | /* and print the error to the error output */ |
| 415 | PyObject* errobj {}; |
| 416 | PyObject* errdata {}; |
| 417 | PyObject* errtraceback {}; |
| 418 | PyErr_Fetch(&errobj, &errdata, &errtraceback); |
| 419 | |
| 420 | RuntimeError exc(""); // do not use PyException since this clears the error indicator |
| 421 | if (errdata) { |
| 422 | if (PyUnicode_Check(errdata)) { |
| 423 | exc.setMessage(PyUnicode_AsUTF8(errdata)); |
| 424 | } |
| 425 | } |
| 426 | PyErr_Restore(errobj, errdata, errtraceback); |
| 427 | if (PyErr_Occurred()) { |
| 428 | PyErr_Print(); |
| 429 | } |
| 430 | throw exc; |
| 431 | } |
| 432 | |
| 433 | Py_DECREF(presult); |
| 434 | } |
| 435 | |
| 436 | void InterpreterSingleton::runFile(const char* pxFileName, bool local) |
| 437 | { |