UI: improve errors when evaluating a number button fails

Showing the Python error without any explanation is often
not enough information and doesn't hint that the error was in the
user input.

The error report from a invalid expression such as '..1' used to be:
   ('invalid syntax', ('<string>', 1, 1, '..1'))

Now reads:
   Error evaluating number, see Info editor for details: invalid syntax

Address issue raised by T78913.
This commit is contained in:
Campbell Barton
2020-07-27 13:46:58 +10:00
parent 7beef1fd33
commit 565d7f75cc
9 changed files with 80 additions and 39 deletions

View File

@@ -82,23 +82,23 @@ bool BPY_execute_text(struct bContext *C,
bool BPY_execute_string_as_number(struct bContext *C,
const char *imports[],
const char *expr,
const bool verbose,
const char *report_prefix,
double *r_value);
bool BPY_execute_string_as_intptr(struct bContext *C,
const char *imports[],
const char *expr,
const bool verbose,
const char *report_prefix,
intptr_t *r_value);
bool BPY_execute_string_as_string_and_size(struct bContext *C,
const char *imports[],
const char *expr,
const bool verbose,
const char *report_prefix,
char **r_value,
size_t *r_value_size);
bool BPY_execute_string_as_string(struct bContext *C,
const char *imports[],
const char *expr,
const bool verbose,
const char *report_prefix,
char **r_value);
bool BPY_execute_string_ex(struct bContext *C,

View File

@@ -97,7 +97,10 @@ void BPy_reports_write_stdout(const ReportList *reports, const char *header)
}
}
bool BPy_errors_to_report_ex(ReportList *reports, const bool use_full, const bool use_location)
bool BPy_errors_to_report_ex(ReportList *reports,
const char *error_prefix,
const bool use_full,
const bool use_location)
{
PyObject *pystring;
@@ -124,6 +127,11 @@ bool BPy_errors_to_report_ex(ReportList *reports, const bool use_full, const boo
return 0;
}
if (error_prefix == NULL) {
/* Not very helpful, better than nothing. */
error_prefix = "Python";
}
if (use_location) {
const char *filename;
int lineno;
@@ -135,17 +143,22 @@ bool BPy_errors_to_report_ex(ReportList *reports, const bool use_full, const boo
BKE_reportf(reports,
RPT_ERROR,
TIP_("%s\nlocation: %s:%d\n"),
TIP_("%s: %s\nlocation: %s:%d\n"),
error_prefix,
_PyUnicode_AsString(pystring),
filename,
lineno);
/* Not exactly needed. Useful for developers tracking down issues. */
fprintf(
stderr, TIP_("%s\nlocation: %s:%d\n"), _PyUnicode_AsString(pystring), filename, lineno);
fprintf(stderr,
TIP_("%s: %s\nlocation: %s:%d\n"),
error_prefix,
_PyUnicode_AsString(pystring),
filename,
lineno);
}
else {
BKE_report(reports, RPT_ERROR, _PyUnicode_AsString(pystring));
BKE_reportf(reports, RPT_ERROR, "%s: %s", error_prefix, _PyUnicode_AsString(pystring));
}
Py_DECREF(pystring);
@@ -154,5 +167,5 @@ bool BPy_errors_to_report_ex(ReportList *reports, const bool use_full, const boo
bool BPy_errors_to_report(ReportList *reports)
{
return BPy_errors_to_report_ex(reports, true, true);
return BPy_errors_to_report_ex(reports, NULL, true, true);
}

View File

@@ -39,8 +39,10 @@ char *BPy_enum_as_string(const struct EnumPropertyItem *item);
short BPy_reports_to_error(struct ReportList *reports, PyObject *exception, const bool clear);
void BPy_reports_write_stdout(const struct ReportList *reports, const char *header);
bool BPy_errors_to_report_ex(struct ReportList *reports,
const char *error_prefix,
const bool use_full,
const bool use_location);
bool BPy_errors_to_report_brief_with_prefix(struct ReportList *reports, const char *error_prefix);
bool BPy_errors_to_report(struct ReportList *reports);
/* TODO - find a better solution! */

View File

@@ -617,8 +617,11 @@ void BPY_DECREF_RNA_INVALIDATE(void *pyob_ptr)
/**
* \return success
*/
bool BPY_execute_string_as_number(
bContext *C, const char *imports[], const char *expr, const bool verbose, double *r_value)
bool BPY_execute_string_as_number(bContext *C,
const char *imports[],
const char *expr,
const char *report_prefix,
double *r_value)
{
PyGILState_STATE gilstate;
bool ok = true;
@@ -637,8 +640,8 @@ bool BPY_execute_string_as_number(
ok = PyC_RunString_AsNumber(imports, expr, "<expr as number>", r_value);
if (ok == false) {
if (verbose) {
BPy_errors_to_report_ex(CTX_wm_reports(C), false, false);
if (report_prefix != NULL) {
BPy_errors_to_report_ex(CTX_wm_reports(C), report_prefix, false, false);
}
else {
PyErr_Clear();
@@ -656,7 +659,7 @@ bool BPY_execute_string_as_number(
bool BPY_execute_string_as_string_and_size(bContext *C,
const char *imports[],
const char *expr,
const bool verbose,
const char *report_prefix,
char **r_value,
size_t *r_value_size)
{
@@ -674,8 +677,8 @@ bool BPY_execute_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 (verbose) {
BPy_errors_to_report_ex(CTX_wm_reports(C), false, false);
if (report_prefix != NULL) {
BPy_errors_to_report_ex(CTX_wm_reports(C), false, false, report_prefix);
}
else {
PyErr_Clear();
@@ -687,12 +690,15 @@ bool BPY_execute_string_as_string_and_size(bContext *C,
return ok;
}
bool BPY_execute_string_as_string(
bContext *C, const char *imports[], const char *expr, const bool verbose, char **r_value)
bool BPY_execute_string_as_string(bContext *C,
const char *imports[],
const char *expr,
const char *report_prefix,
char **r_value)
{
size_t value_dummy_size;
return BPY_execute_string_as_string_and_size(
C, imports, expr, verbose, r_value, &value_dummy_size);
C, imports, expr, report_prefix, r_value, &value_dummy_size);
}
/**
@@ -700,8 +706,11 @@ bool BPY_execute_string_as_string(
*
* \return success
*/
bool BPY_execute_string_as_intptr(
bContext *C, const char *imports[], const char *expr, const bool verbose, intptr_t *r_value)
bool BPY_execute_string_as_intptr(bContext *C,
const char *imports[],
const char *expr,
const char *report_prefix,
intptr_t *r_value)
{
BLI_assert(r_value && expr);
PyGILState_STATE gilstate;
@@ -717,8 +726,8 @@ bool BPY_execute_string_as_intptr(
ok = PyC_RunString_AsIntPtr(imports, expr, "<expr as intptr>", r_value);
if (ok == false) {
if (verbose) {
BPy_errors_to_report_ex(CTX_wm_reports(C), false, false);
if (report_prefix != NULL) {
BPy_errors_to_report_ex(CTX_wm_reports(C), report_prefix, false, false);
}
else {
PyErr_Clear();