2022-02-11 09:07:11 +11:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0-or-later */
|
2008-12-28 08:41:49 +00:00
|
|
|
|
2019-02-18 08:08:12 +11:00
|
|
|
/** \file
|
|
|
|
|
* \ingroup pythonintern
|
2011-11-05 08:21:12 +00:00
|
|
|
*
|
2017-11-29 21:11:29 +11:00
|
|
|
* This file contains Blender/Python utility functions to help implementing API's.
|
|
|
|
|
* This is not related to a particular module.
|
2011-02-27 20:10:08 +00:00
|
|
|
*/
|
|
|
|
|
|
2011-02-14 04:15:25 +00:00
|
|
|
#include <Python.h>
|
|
|
|
|
|
2008-12-28 08:41:49 +00:00
|
|
|
#include "BLI_dynstr.h"
|
2020-04-03 17:38:58 +02:00
|
|
|
#include "BLI_listbase.h"
|
2020-03-19 09:33:03 +01:00
|
|
|
#include "BLI_utildefines.h"
|
2013-01-07 05:26:12 +00:00
|
|
|
|
2017-11-29 21:11:29 +11:00
|
|
|
#include "bpy_capi_utils.h"
|
2013-01-07 05:26:12 +00:00
|
|
|
|
2008-12-28 08:41:49 +00:00
|
|
|
#include "MEM_guardedalloc.h"
|
2013-01-07 05:26:12 +00:00
|
|
|
|
2010-09-01 14:13:48 +00:00
|
|
|
#include "BKE_context.h"
|
2020-03-19 09:33:03 +01:00
|
|
|
#include "BKE_report.h"
|
2008-12-28 08:41:49 +00:00
|
|
|
|
2015-08-16 17:32:01 +10:00
|
|
|
#include "BLT_translation.h"
|
2012-10-14 15:29:09 +00:00
|
|
|
|
2010-09-01 14:13:48 +00:00
|
|
|
#include "../generic/py_capi_utils.h"
|
2009-05-25 13:48:44 +00:00
|
|
|
|
2013-01-07 05:26:12 +00:00
|
|
|
short BPy_reports_to_error(ReportList *reports, PyObject *exception, const bool clear)
|
2009-04-19 13:37:59 +00:00
|
|
|
{
|
|
|
|
|
char *report_str;
|
|
|
|
|
|
2011-12-26 12:26:11 +00:00
|
|
|
report_str = BKE_reports_string(reports, RPT_ERROR);
|
2009-04-19 13:37:59 +00:00
|
|
|
|
2013-01-07 05:26:12 +00:00
|
|
|
if (clear == true) {
|
2010-12-31 05:40:30 +00:00
|
|
|
BKE_reports_clear(reports);
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-13 01:29:08 +00:00
|
|
|
if (report_str) {
|
2011-03-12 15:18:08 +00:00
|
|
|
PyErr_SetString(exception, report_str);
|
2009-04-19 13:37:59 +00:00
|
|
|
MEM_freeN(report_str);
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-12 15:18:08 +00:00
|
|
|
return (report_str == NULL) ? 0 : -1;
|
2009-04-19 13:37:59 +00:00
|
|
|
}
|
|
|
|
|
|
2019-03-27 14:01:25 +11:00
|
|
|
void BPy_reports_write_stdout(const ReportList *reports, const char *header)
|
|
|
|
|
{
|
|
|
|
|
if (header) {
|
|
|
|
|
PySys_WriteStdout("%s\n", header);
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-03 19:15:01 +02:00
|
|
|
LISTBASE_FOREACH (const Report *, report, &reports->list) {
|
2019-03-27 14:01:25 +11:00
|
|
|
PySys_WriteStdout("%s: %s\n", report->typestr, report->message);
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-06-14 12:53:47 +00:00
|
|
|
|
2020-07-27 13:46:58 +10:00
|
|
|
bool BPy_errors_to_report_ex(ReportList *reports,
|
2022-04-06 16:15:11 +10:00
|
|
|
const char *err_prefix,
|
2020-07-27 13:46:58 +10:00
|
|
|
const bool use_full,
|
|
|
|
|
const bool use_location)
|
2009-06-14 12:53:47 +00:00
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-03-30 06:12:48 +11:00
|
|
|
if (!PyErr_Occurred()) {
|
2009-06-14 12:53:47 +00:00
|
|
|
return 1;
|
2019-03-30 06:12:48 +11:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-04-06 16:15:11 +10:00
|
|
|
PyObject *err_str_py = use_full ? PyC_ExceptionBuffer() : PyC_ExceptionBuffer_Simple();
|
|
|
|
|
if (err_str_py == NULL) {
|
2012-10-26 17:32:50 +00:00
|
|
|
BKE_report(reports, RPT_ERROR, "Unknown py-exception, could not convert");
|
2009-06-14 12:53:47 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-04-06 19:08:10 +10:00
|
|
|
/* Strip trailing newlines so the report doesn't show a blank-line in the info space. */
|
2022-04-06 16:15:11 +10:00
|
|
|
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) {
|
2020-07-27 13:46:58 +10:00
|
|
|
/* Not very helpful, better than nothing. */
|
2022-04-06 16:15:11 +10:00
|
|
|
err_prefix = "Python";
|
2020-07-27 13:46:58 +10:00
|
|
|
}
|
|
|
|
|
|
2022-04-06 16:15:11 +10:00
|
|
|
const char *location_filepath = NULL;
|
|
|
|
|
int location_line_number = -1;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-04-06 16:15:11 +10:00
|
|
|
/* Give some additional context. */
|
|
|
|
|
if (use_location) {
|
|
|
|
|
PyC_FileAndNum(&location_filepath, &location_line_number);
|
|
|
|
|
}
|
2015-05-18 09:12:26 +10:00
|
|
|
|
2022-04-06 16:15:11 +10:00
|
|
|
if (location_filepath) {
|
2019-04-17 08:24:14 +02:00
|
|
|
BKE_reportf(reports,
|
|
|
|
|
RPT_ERROR,
|
2022-04-06 16:15:11 +10:00
|
|
|
"%s: %.*s\n"
|
|
|
|
|
/* Location (when available). */
|
|
|
|
|
"Location: %s:%d",
|
|
|
|
|
err_prefix,
|
|
|
|
|
(int)err_str_len,
|
|
|
|
|
err_str,
|
|
|
|
|
location_filepath,
|
|
|
|
|
location_line_number);
|
2015-05-18 09:12:26 +10:00
|
|
|
}
|
|
|
|
|
else {
|
2022-04-06 16:15:11 +10:00
|
|
|
BKE_reportf(reports, RPT_ERROR, "%s: %.*s", err_prefix, (int)err_str_len, err_str);
|
2015-05-18 09:12:26 +10:00
|
|
|
}
|
2012-10-15 09:11:17 +00:00
|
|
|
|
2022-04-06 16:15:11 +10:00
|
|
|
/* Ensure this is _always_ printed to the output so developers don't miss exceptions. */
|
|
|
|
|
Py_DECREF(err_str_py);
|
2009-06-14 12:53:47 +00:00
|
|
|
return 1;
|
|
|
|
|
}
|
2015-05-18 09:12:26 +10:00
|
|
|
|
|
|
|
|
bool BPy_errors_to_report(ReportList *reports)
|
|
|
|
|
{
|
2020-07-27 13:46:58 +10:00
|
|
|
return BPy_errors_to_report_ex(reports, NULL, true, true);
|
2016-05-16 00:48:02 +02:00
|
|
|
}
|