Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCampbell Barton <campbell@blender.org>2022-04-06 09:15:11 +0300
committerCampbell Barton <campbell@blender.org>2022-04-06 11:02:58 +0300
commit2d2baeaf04d481f284bc2f098fb6d7ee9268151f (patch)
treedee60d7075b6febe402323d5b12085bdff544630 /source/blender/python
parente4f71e5ef345070dc0b21df35a049184d3e46645 (diff)
Fix: T78228 Send all python errors to info editor
Python exceptions are now shown in the info editor, this also resolves an old bug where errors were printed twice. This was originally based on D9752 by @ShadowChaser although many changes have been made from the original patch. Details: - BPy_errors_to_report no longer prints additional output. - BKE_report_print_test was added so it's possible to check if calling BKE_report also printed to the stdout. - Callers to BPy_errors_to_report are responsible for ensuring output is printed to the stdout/stderr. - Python exceptions no longer add a trailing newline, needed to avoid blank-space when displayed in the info-editor.
Diffstat (limited to 'source/blender/python')
-rw-r--r--source/blender/python/intern/bpy_capi_utils.c72
-rw-r--r--source/blender/python/intern/bpy_capi_utils.h13
-rw-r--r--source/blender/python/intern/bpy_interface_run.c26
-rw-r--r--source/blender/python/intern/bpy_traceback.c5
4 files changed, 70 insertions, 46 deletions
diff --git a/source/blender/python/intern/bpy_capi_utils.c b/source/blender/python/intern/bpy_capi_utils.c
index 742c04de3d7..f7f8253edac 100644
--- a/source/blender/python/intern/bpy_capi_utils.c
+++ b/source/blender/python/intern/bpy_capi_utils.c
@@ -54,70 +54,60 @@ void BPy_reports_write_stdout(const ReportList *reports, const char *header)
}
bool BPy_errors_to_report_ex(ReportList *reports,
- const char *error_prefix,
+ const char *err_prefix,
const bool use_full,
const bool use_location)
{
- PyObject *pystring;
if (!PyErr_Occurred()) {
return 1;
}
- /* less hassle if we allow NULL */
- if (reports == NULL) {
- PyErr_Print();
- PyErr_Clear();
- return 1;
- }
-
- if (use_full) {
- pystring = PyC_ExceptionBuffer();
- }
- else {
- pystring = PyC_ExceptionBuffer_Simple();
- }
-
- if (pystring == NULL) {
+ PyObject *err_str_py = use_full ? PyC_ExceptionBuffer() : PyC_ExceptionBuffer_Simple();
+ if (err_str_py == NULL) {
BKE_report(reports, RPT_ERROR, "Unknown py-exception, could not convert");
return 0;
}
- if (error_prefix == NULL) {
+ /* Trim trailing newlines so the report doesn't contain a trailing new-line.
+ * This would add a blank-line in the info space. */
+ Py_ssize_t err_str_len;
+ const char *err_str = PyUnicode_AsUTF8AndSize(err_str_py, &err_str_len);
+ while (err_str_len > 0 && err_str[err_str_len - 1] == '\n') {
+ err_str_len -= 1;
+ }
+
+ if (err_prefix == NULL) {
/* Not very helpful, better than nothing. */
- error_prefix = "Python";
+ err_prefix = "Python";
}
- if (use_location) {
- const char *filename;
- int lineno;
+ const char *location_filepath = NULL;
+ int location_line_number = -1;
- PyC_FileAndNum(&filename, &lineno);
- if (filename == NULL) {
- filename = "<unknown location>";
- }
+ /* Give some additional context. */
+ if (use_location) {
+ PyC_FileAndNum(&location_filepath, &location_line_number);
+ }
+ if (location_filepath) {
BKE_reportf(reports,
RPT_ERROR,
- TIP_("%s: %s\nlocation: %s:%d\n"),
- error_prefix,
- PyUnicode_AsUTF8(pystring),
- filename,
- lineno);
-
- /* Not exactly needed. Useful for developers tracking down issues. */
- fprintf(stderr,
- TIP_("%s: %s\nlocation: %s:%d\n"),
- error_prefix,
- PyUnicode_AsUTF8(pystring),
- filename,
- lineno);
+ "%s: %.*s\n"
+ /* Location (when available). */
+ "Location: %s:%d",
+ err_prefix,
+ (int)err_str_len,
+ err_str,
+ location_filepath,
+ location_line_number);
}
else {
- BKE_reportf(reports, RPT_ERROR, "%s: %s", error_prefix, PyUnicode_AsUTF8(pystring));
+ BKE_reportf(reports, RPT_ERROR, "%s: %.*s", err_prefix, (int)err_str_len, err_str);
}
- Py_DECREF(pystring);
+ /* Ensure this is _always_ printed to the output so developers don't miss exceptions. */
+ Py_DECREF(err_str_py);
return 1;
}
diff --git a/source/blender/python/intern/bpy_capi_utils.h b/source/blender/python/intern/bpy_capi_utils.h
index 223c6ad5f7e..ab5ce7818f3 100644
--- a/source/blender/python/intern/bpy_capi_utils.h
+++ b/source/blender/python/intern/bpy_capi_utils.h
@@ -27,7 +27,18 @@ bool BPy_errors_to_report_ex(struct ReportList *reports,
const char *error_prefix,
bool use_full,
bool use_location);
-bool BPy_errors_to_report_brief_with_prefix(struct ReportList *reports, const char *error_prefix);
+/**
+ * \param reports: When set, an error will be added to this report, when NULL, print the error.
+ *
+ * \note Unless the caller handles printing the reports (or reports is NULL) it's best to ensure
+ * the output is printed to the `stdout/stderr`:
+ * \code{.cc}
+ * BPy_errors_to_report(reports);
+ * if (!BKE_reports_print_test(reports)) {
+ * BKE_reports_print(reports);
+ * }
+ * \endcode
+ */
bool BPy_errors_to_report(struct ReportList *reports);
struct bContext *BPY_context_get(void);
diff --git a/source/blender/python/intern/bpy_interface_run.c b/source/blender/python/intern/bpy_interface_run.c
index 147c6cf8187..50a2722c276 100644
--- a/source/blender/python/intern/bpy_interface_run.c
+++ b/source/blender/python/intern/bpy_interface_run.c
@@ -271,7 +271,23 @@ static bool bpy_run_string_impl(bContext *C,
if (retval == NULL) {
ok = false;
- BPy_errors_to_report(CTX_wm_reports(C));
+
+ ReportList reports;
+ BKE_reports_init(&reports, RPT_STORE);
+ BPy_errors_to_report(&reports);
+
+ /* Ensure the reports are printed. */
+ if (!BKE_reports_print_test(&reports, RPT_ERROR)) {
+ BKE_reports_print(&reports, RPT_ERROR);
+ }
+
+ ReportList *wm_reports = CTX_wm_reports(C);
+ if (wm_reports) {
+ BLI_movelisttolist(&wm_reports->list, &reports.list);
+ }
+ else {
+ BKE_reports_clear(&reports);
+ }
}
else {
Py_DECREF(retval);
@@ -330,6 +346,14 @@ static void run_string_handle_error(struct BPy_RunErrInfo *err_info)
}
}
+ /* Print the reports if they were not printed already. */
+ if ((err_info->reports == NULL) || !BKE_reports_print_test(err_info->reports, RPT_ERROR)) {
+ if (err_info->report_prefix) {
+ fprintf(stderr, "%s: ", err_info->report_prefix);
+ }
+ fprintf(stderr, "%s\n", err_str);
+ }
+
if (err_info->r_string != NULL) {
*err_info->r_string = BLI_strdup(err_str);
}
diff --git a/source/blender/python/intern/bpy_traceback.c b/source/blender/python/intern/bpy_traceback.c
index 40478f3613c..1c545774203 100644
--- a/source/blender/python/intern/bpy_traceback.c
+++ b/source/blender/python/intern/bpy_traceback.c
@@ -205,12 +205,9 @@ bool python_script_error_jump(
}
}
}
- PyErr_Restore(exception, value, (PyObject *)tb); /* takes away reference! */
}
else {
PyErr_NormalizeException(&exception, &value, (PyObject **)&tb);
- PyErr_Restore(exception, value, (PyObject *)tb); /* takes away reference! */
- PyErr_Print();
for (tb = (PyTracebackObject *)PySys_GetObject("last_traceback");
tb && (PyObject *)tb != Py_None;
@@ -230,5 +227,7 @@ bool python_script_error_jump(
}
}
+ PyErr_Restore(exception, value, (PyObject *)tb); /* takes away reference! */
+
return success;
}