diff options
author | Campbell Barton <ideasman42@gmail.com> | 2021-04-12 14:39:56 +0300 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2021-04-13 08:28:43 +0300 |
commit | f5060bc90118c88593fd213728bf58894fa6394c (patch) | |
tree | d58797c31faea554ee2075fcfc21d734d541083a /source/blender/python | |
parent | 8d9fd0427dd6cf9556f2bb4415ead82c73300d32 (diff) |
PyAPI: support retrieving the exception when running a string
- Optionally get the error as a single line.
- Support access the error as an allocated string.
- PyC_ExceptionBuffer_Simple was always printing to the `stdout` while
PyC_ExceptionBuffer didn't, now either print to the output.
Without this, callers are unable to do anything with the error string.
Diffstat (limited to 'source/blender/python')
-rw-r--r-- | source/blender/python/BPY_extern_run.h | 24 | ||||
-rw-r--r-- | source/blender/python/generic/py_capi_utils.c | 1 | ||||
-rw-r--r-- | source/blender/python/intern/bpy_interface_run.c | 67 |
3 files changed, 63 insertions, 29 deletions
diff --git a/source/blender/python/BPY_extern_run.h b/source/blender/python/BPY_extern_run.h index 5f12ada4ff2..b65b5d61b9d 100644 --- a/source/blender/python/BPY_extern_run.h +++ b/source/blender/python/BPY_extern_run.h @@ -42,27 +42,43 @@ bool BPY_run_text(struct bContext *C, bool BPY_run_string_exec(struct bContext *C, const char *imports[], const char *expr); bool BPY_run_string_eval(struct bContext *C, const char *imports[], const char *expr); +/** + * \note When this struct is passed in as NULL, + * print errors to the `stdout` and clear. + */ +struct BPy_RunErrInfo { + /** Brief text, single line (can show this in status bar for e.g.). */ + bool use_single_line_error; + + /** Report with optional prefix (when non-NULL). */ + struct ReportList *reports; + const char *report_prefix; + + /** Allocated exception text (assign when non-NULL). */ + char **r_string; +}; + /* Run, evaluating to fixed type result. */ bool BPY_run_string_as_number(struct bContext *C, const char *imports[], const char *expr, - const char *report_prefix, + struct BPy_RunErrInfo *err_info, double *r_value); bool BPY_run_string_as_intptr(struct bContext *C, const char *imports[], const char *expr, - const char *report_prefix, + struct BPy_RunErrInfo *err_info, intptr_t *r_value); bool BPY_run_string_as_string_and_size(struct bContext *C, const char *imports[], const char *expr, - const char *report_prefix, + struct BPy_RunErrInfo *err_info, char **r_value, size_t *r_value_size); bool BPY_run_string_as_string(struct bContext *C, const char *imports[], const char *expr, - const char *report_prefix, + struct BPy_RunErrInfo *err_info, char **r_value); #ifdef __cplusplus diff --git a/source/blender/python/generic/py_capi_utils.c b/source/blender/python/generic/py_capi_utils.c index 78f5b9ba2cc..9824d5f17c4 100644 --- a/source/blender/python/generic/py_capi_utils.c +++ b/source/blender/python/generic/py_capi_utils.c @@ -733,7 +733,6 @@ PyObject *PyC_ExceptionBuffer_Simple(void) PyErr_Restore(error_type, error_value, error_traceback); - PyErr_Print(); PyErr_Clear(); return string_io_buf; } diff --git a/source/blender/python/intern/bpy_interface_run.c b/source/blender/python/intern/bpy_interface_run.c index a7593ae7d79..f7272a87a17 100644 --- a/source/blender/python/intern/bpy_interface_run.c +++ b/source/blender/python/intern/bpy_interface_run.c @@ -31,6 +31,7 @@ #include "BKE_context.h" #include "BKE_main.h" +#include "BKE_report.h" #include "BKE_text.h" #include "DNA_text_types.h" @@ -294,13 +295,47 @@ bool BPY_run_string_exec(bContext *C, const char *imports[], const char *expr) * in code that doesn't deal with Python data-types. * \{ */ +static void run_string_handle_error(struct BPy_RunErrInfo *err_info) +{ + if (err_info == NULL) { + PyErr_Print(); + PyErr_Clear(); + return; + } + + /* Signal to do nothing. */ + if (!(err_info->reports || err_info->r_string)) { + PyErr_Clear(); + return; + } + + PyObject *py_err_str = err_info->use_single_line_error ? PyC_ExceptionBuffer_Simple() : + PyC_ExceptionBuffer(); + const char *err_str = py_err_str ? PyUnicode_AsUTF8(py_err_str) : "Unable to extract exception"; + + if (err_info->reports != NULL) { + if (err_info->report_prefix) { + BKE_reportf(err_info->reports, RPT_ERROR, "%s: %s", err_info->report_prefix, err_str); + } + else { + BKE_report(err_info->reports, RPT_ERROR, err_str); + } + } + + if (err_info->r_string != NULL) { + *err_info->r_string = BLI_strdup(err_str); + } + + Py_XDECREF(py_err_str); +} + /** * \return success */ bool BPY_run_string_as_number(bContext *C, const char *imports[], const char *expr, - const char *report_prefix, + struct BPy_RunErrInfo *err_info, double *r_value) { PyGILState_STATE gilstate; @@ -320,12 +355,7 @@ bool BPY_run_string_as_number(bContext *C, ok = PyC_RunString_AsNumber(imports, expr, "<expr as number>", r_value); if (ok == false) { - if (report_prefix != NULL) { - BPy_errors_to_report_ex(CTX_wm_reports(C), report_prefix, false, false); - } - else { - PyErr_Clear(); - } + run_string_handle_error(err_info); } bpy_context_clear(C, &gilstate); @@ -339,7 +369,7 @@ bool BPY_run_string_as_number(bContext *C, bool BPY_run_string_as_string_and_size(bContext *C, const char *imports[], const char *expr, - const char *report_prefix, + struct BPy_RunErrInfo *err_info, char **r_value, size_t *r_value_size) { @@ -357,12 +387,7 @@ bool BPY_run_string_as_string_and_size(bContext *C, ok = PyC_RunString_AsStringAndSize(imports, expr, "<expr as str>", r_value, r_value_size); if (ok == false) { - if (report_prefix != NULL) { - BPy_errors_to_report_ex(CTX_wm_reports(C), false, false, report_prefix); - } - else { - PyErr_Clear(); - } + run_string_handle_error(err_info); } bpy_context_clear(C, &gilstate); @@ -373,12 +398,11 @@ bool BPY_run_string_as_string_and_size(bContext *C, bool BPY_run_string_as_string(bContext *C, const char *imports[], const char *expr, - const char *report_prefix, + struct BPy_RunErrInfo *err_info, char **r_value) { size_t value_dummy_size; - return BPY_run_string_as_string_and_size( - C, imports, expr, report_prefix, r_value, &value_dummy_size); + return BPY_run_string_as_string_and_size(C, imports, expr, err_info, r_value, &value_dummy_size); } /** @@ -389,7 +413,7 @@ bool BPY_run_string_as_string(bContext *C, bool BPY_run_string_as_intptr(bContext *C, const char *imports[], const char *expr, - const char *report_prefix, + struct BPy_RunErrInfo *err_info, intptr_t *r_value) { BLI_assert(r_value && expr); @@ -406,12 +430,7 @@ bool BPY_run_string_as_intptr(bContext *C, ok = PyC_RunString_AsIntPtr(imports, expr, "<expr as intptr>", r_value); if (ok == false) { - if (report_prefix != NULL) { - BPy_errors_to_report_ex(CTX_wm_reports(C), report_prefix, false, false); - } - else { - PyErr_Clear(); - } + run_string_handle_error(err_info); } bpy_context_clear(C, &gilstate); |