* \param mode: Passed to #PyRun_String, matches Python's `compile` functions mode argument. * #Py_eval_input for `eval`, #Py_file_input for `exec`. */
| 255 | * #Py_eval_input for `eval`, #Py_file_input for `exec`. |
| 256 | */ |
| 257 | static bool bpy_run_string_impl(bContext *C, |
| 258 | const char *imports[], |
| 259 | const char *expr, |
| 260 | const int mode) |
| 261 | { |
| 262 | BLI_assert(expr); |
| 263 | PyGILState_STATE gilstate; |
| 264 | PyObject *py_dict, *retval; |
| 265 | bool ok = true; |
| 266 | |
| 267 | if (expr[0] == '\0') { |
| 268 | return ok; |
| 269 | } |
| 270 | |
| 271 | bpy_context_set(C, &gilstate); |
| 272 | |
| 273 | PyObject *main_mod = PyC_MainModule_Backup(); |
| 274 | |
| 275 | py_dict = PyC_DefaultNameSpace("<blender string>"); |
| 276 | |
| 277 | if (imports && !PyC_NameSpace_ImportArray(py_dict, imports)) { |
| 278 | Py_DECREF(py_dict); |
| 279 | retval = nullptr; |
| 280 | } |
| 281 | else { |
| 282 | retval = PyRun_String(expr, mode, py_dict, py_dict); |
| 283 | } |
| 284 | |
| 285 | if (retval == nullptr) { |
| 286 | ok = false; |
| 287 | if (ReportList *wm_reports = C ? CTX_wm_reports(C) : nullptr) { |
| 288 | BPy_errors_to_report(wm_reports); |
| 289 | } |
| 290 | else { |
| 291 | PyC_Err_CaptureSystemExitCode(); |
| 292 | } |
| 293 | PyErr_Print(); |
| 294 | } |
| 295 | else { |
| 296 | Py_DECREF(retval); |
| 297 | } |
| 298 | |
| 299 | PyC_MainModule_Restore(main_mod); |
| 300 | |
| 301 | bpy_context_clear(C, &gilstate); |
| 302 | |
| 303 | return ok; |
| 304 | } |
| 305 | |
| 306 | bool BPY_run_string_eval(bContext *C, const char *imports[], const char *expr) |
| 307 | { |
no test coverage detected