diff options
Diffstat (limited to 'source/blender/python/intern/bpy_capi_utils.c')
-rw-r--r-- | source/blender/python/intern/bpy_capi_utils.c | 72 |
1 files changed, 31 insertions, 41 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; } |