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 | |
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.
-rw-r--r-- | source/blender/editors/include/ED_numinput.h | 5 | ||||
-rw-r--r-- | source/blender/editors/interface/interface.c | 16 | ||||
-rw-r--r-- | source/blender/editors/interface/interface_context_menu.c | 2 | ||||
-rw-r--r-- | source/blender/editors/interface/interface_region_tooltip.c | 10 | ||||
-rw-r--r-- | source/blender/editors/util/numinput.c | 25 | ||||
-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 |
8 files changed, 106 insertions, 44 deletions
diff --git a/source/blender/editors/include/ED_numinput.h b/source/blender/editors/include/ED_numinput.h index 50f1ce1efe2..d5685788ce1 100644 --- a/source/blender/editors/include/ED_numinput.h +++ b/source/blender/editors/include/ED_numinput.h @@ -107,8 +107,9 @@ bool user_string_to_number(bContext *C, const char *str, const struct UnitSettings *unit, int type, - const char *error_prefix, - double *r_value); + double *r_value, + const bool use_single_line_error, + char **r_error); /** \} */ diff --git a/source/blender/editors/interface/interface.c b/source/blender/editors/interface/interface.c index 6caee42ac33..3c0f0587741 100644 --- a/source/blender/editors/interface/interface.c +++ b/source/blender/editors/interface/interface.c @@ -52,6 +52,7 @@ #include "BKE_context.h" #include "BKE_idprop.h" #include "BKE_main.h" +#include "BKE_report.h" #include "BKE_scene.h" #include "BKE_screen.h" #include "BKE_unit.h" @@ -2912,7 +2913,14 @@ char *ui_but_string_get_dynamic(uiBut *but, int *r_str_size) static bool ui_number_from_string_units( bContext *C, const char *str, const int unit_type, const UnitSettings *unit, double *r_value) { - return user_string_to_number(C, str, unit, unit_type, UI_NUMBER_EVAL_ERROR_PREFIX, r_value); + char *error = NULL; + const bool ok = user_string_to_number(C, str, unit, unit_type, r_value, true, &error); + if (error) { + ReportList *reports = CTX_wm_reports(C); + BKE_reportf(reports, RPT_ERROR, "%s: %s", UI_NUMBER_EVAL_ERROR_PREFIX, error); + MEM_freeN(error); + } + return ok; } static bool ui_number_from_string_units_with_but(bContext *C, @@ -2929,7 +2937,11 @@ static bool ui_number_from_string(bContext *C, const char *str, double *r_value) { bool ok; #ifdef WITH_PYTHON - ok = BPY_run_string_as_number(C, NULL, str, UI_NUMBER_EVAL_ERROR_PREFIX, r_value); + struct BPy_RunErrInfo err_info = { + .reports = CTX_wm_reports(C), + .report_prefix = UI_NUMBER_EVAL_ERROR_PREFIX, + }; + ok = BPY_run_string_as_number(C, NULL, str, &err_info, r_value); #else UNUSED_VARS(C); *r_value = atof(str); diff --git a/source/blender/editors/interface/interface_context_menu.c b/source/blender/editors/interface/interface_context_menu.c index c594b61acd3..b142e383df0 100644 --- a/source/blender/editors/interface/interface_context_menu.c +++ b/source/blender/editors/interface/interface_context_menu.c @@ -399,7 +399,7 @@ static void ui_but_user_menu_add(bContext *C, uiBut *but, bUserMenu *um) "'%s').label", idname); char *expr_result = NULL; - if (BPY_run_string_as_string(C, expr_imports, expr, __func__, &expr_result)) { + if (BPY_run_string_as_string(C, expr_imports, expr, NULL, &expr_result)) { STRNCPY(drawstr, expr_result); MEM_freeN(expr_result); } diff --git a/source/blender/editors/interface/interface_region_tooltip.c b/source/blender/editors/interface/interface_region_tooltip.c index 244cf7622e0..07e17033ee5 100644 --- a/source/blender/editors/interface/interface_region_tooltip.c +++ b/source/blender/editors/interface/interface_region_tooltip.c @@ -428,7 +428,7 @@ static uiTooltipData *ui_tooltip_data_from_tool(bContext *C, uiBut *but, bool is if (has_valid_context == false) { expr_result = BLI_strdup(has_valid_context_error); } - else if (BPY_run_string_as_string(C, expr_imports, expr, __func__, &expr_result)) { + else if (BPY_run_string_as_string(C, expr_imports, expr, NULL, &expr_result)) { if (STREQ(expr_result, "")) { MEM_freeN(expr_result); expr_result = NULL; @@ -485,7 +485,7 @@ static uiTooltipData *ui_tooltip_data_from_tool(bContext *C, uiBut *but, bool is if (has_valid_context == false) { expr_result = BLI_strdup(has_valid_context_error); } - else if (BPY_run_string_as_string(C, expr_imports, expr, __func__, &expr_result)) { + else if (BPY_run_string_as_string(C, expr_imports, expr, NULL, &expr_result)) { if (STREQ(expr_result, ".")) { MEM_freeN(expr_result); expr_result = NULL; @@ -589,7 +589,7 @@ static uiTooltipData *ui_tooltip_data_from_tool(bContext *C, uiBut *but, bool is if (has_valid_context == false) { shortcut = BLI_strdup(has_valid_context_error); } - else if (BPY_run_string_as_intptr(C, expr_imports, expr, __func__, &expr_result)) { + else if (BPY_run_string_as_intptr(C, expr_imports, expr, NULL, &expr_result)) { if (expr_result != 0) { wmKeyMap *keymap = (wmKeyMap *)expr_result; LISTBASE_FOREACH (wmKeyMapItem *, kmi, &keymap->items) { @@ -654,7 +654,7 @@ static uiTooltipData *ui_tooltip_data_from_tool(bContext *C, uiBut *but, bool is /* pass */ } else if (BPY_run_string_as_string_and_size( - C, expr_imports, expr, __func__, &expr_result, &expr_result_len)) { + C, expr_imports, expr, NULL, &expr_result, &expr_result_len)) { /* pass. */ } } @@ -731,7 +731,7 @@ static uiTooltipData *ui_tooltip_data_from_tool(bContext *C, uiBut *but, bool is if (has_valid_context == false) { /* pass */ } - else if (BPY_run_string_as_intptr(C, expr_imports, expr, __func__, &expr_result)) { + else if (BPY_run_string_as_intptr(C, expr_imports, expr, NULL, &expr_result)) { if (expr_result != 0) { { uiTooltipField *field = text_field_add(data, diff --git a/source/blender/editors/util/numinput.c b/source/blender/editors/util/numinput.c index 91ec8546225..15d672dea56 100644 --- a/source/blender/editors/util/numinput.c +++ b/source/blender/editors/util/numinput.c @@ -29,6 +29,7 @@ #include "BLT_translation.h" #include "BKE_context.h" +#include "BKE_report.h" #include "BKE_scene.h" #include "BKE_unit.h" @@ -283,10 +284,15 @@ bool user_string_to_number(bContext *C, const char *str, const UnitSettings *unit, int type, - const char *error_prefix, - double *r_value) + double *r_value, + const bool use_single_line_error, + char **r_error) { #ifdef WITH_PYTHON + struct BPy_RunErrInfo err_info = { + .use_single_line_error = use_single_line_error, + .r_string = r_error, + }; double unit_scale = BKE_scene_unit_scale(unit, type, 1.0); if (BKE_unit_string_contains_unit(str, type)) { char str_unit_convert[256]; @@ -294,10 +300,10 @@ bool user_string_to_number(bContext *C, BKE_unit_replace_string( str_unit_convert, sizeof(str_unit_convert), str, unit_scale, unit->system, type); - return BPY_run_string_as_number(C, NULL, str_unit_convert, error_prefix, r_value); + return BPY_run_string_as_number(C, NULL, str_unit_convert, &err_info, r_value); } - int success = BPY_run_string_as_number(C, NULL, str, error_prefix, r_value); + int success = BPY_run_string_as_number(C, NULL, str, &err_info, r_value); *r_value = BKE_unit_apply_preferred_unit(unit, type, *r_value); *r_value /= unit_scale; return success; @@ -577,10 +583,19 @@ bool handleNumInput(bContext *C, NumInput *n, const wmEvent *event) if (n->str[0]) { const float val_prev = n->val[idx]; Scene *sce = CTX_data_scene(C); + char *error = NULL; double val; int success = user_string_to_number( - C, n->str, &sce->unit, n->unit_type[idx], IFACE_("Numeric input evaluation"), &val); + C, n->str, &sce->unit, n->unit_type[idx], &val, false, &error); + + if (error) { + ReportList *reports = CTX_wm_reports(C); + printf("%s\n", error); + BKE_report(reports, RPT_ERROR, error); + BKE_report(reports, RPT_ERROR, IFACE_("Numeric input evaluation")); + MEM_freeN(error); + } if (success) { n->val[idx] = (float)val; 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); |