[Bf-blender-cvs] [847eea677f1] temp-gpencil-bezier-stroke-type: PyAPI: support retrieving the exception when running a string
Campbell Barton
noreply at git.blender.org
Tue Apr 13 11:29:20 CEST 2021
Commit: 847eea677f1b43cb4f5cdf4b3a4b38b4c5b30613
Author: Campbell Barton
Date: Mon Apr 12 21:39:56 2021 +1000
Branches: temp-gpencil-bezier-stroke-type
https://developer.blender.org/rB847eea677f1b43cb4f5cdf4b3a4b38b4c5b30613
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.
===================================================================
M source/blender/editors/include/ED_numinput.h
M source/blender/editors/interface/interface.c
M source/blender/editors/interface/interface_context_menu.c
M source/blender/editors/interface/interface_region_tooltip.c
M source/blender/editors/util/numinput.c
M source/blender/python/BPY_extern_run.h
M source/blender/python/generic/py_capi_utils.c
M source/blender/python/intern/bpy_interface_run.c
===================================================================
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
@@ Diff output truncated at 10240 characters. @@
More information about the Bf-blender-cvs
mailing list