Blender V5.3
bpy_capi_utils.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
11
12#include <Python.h>
13
14#include "bpy_capi_utils.hh"
15
16#include "MEM_guardedalloc.h"
17
18#include "BKE_report.hh"
19
21
22namespace blender {
23
24short BPy_reports_to_error(ReportList *reports, PyObject *exception, const bool clear)
25{
26 char *report_str;
27
28 report_str = BKE_reports_string(reports, RPT_ERROR);
29
30 if (clear == true) {
31 BKE_reports_free(reports);
32 }
33
34 if (report_str) {
35 PyErr_SetString(exception, report_str);
36 MEM_delete(report_str);
37 }
38
39 return (report_str == nullptr) ? 0 : -1;
40}
41
42void BPy_reports_write_stdout(const ReportList *reports, const char *header)
43{
44 const Report *report;
45 for (report = reports->list.first(); report; report = report->next) {
46 if (report->type < reports->printlevel) {
47 continue;
48 }
49 break;
50 }
51 if (report == nullptr) {
52 return;
53 }
54
55 if (header) {
56 PySys_WriteStdout("%s\n", header);
57 }
58
59 for (; report; report = report->next) {
60 if (report->type < reports->printlevel) {
61 continue;
62 }
63 PySys_WriteStdout("%s: %s\n", report->typestr, report->message);
64 }
65}
66
68 const char *error_prefix,
69 const bool use_full,
70 const bool use_location)
71{
72
73 if (!PyErr_Occurred()) {
74 return true;
75 }
76
77 PyObject *err_str_py = use_full ? PyC_ExceptionBuffer() : PyC_ExceptionBuffer_Simple();
78
79 /* Strip trailing newlines so the report doesn't show a blank-line in the info space. */
80 Py_ssize_t err_str_len;
81 const char *err_str = PyUnicode_AsUTF8AndSize(err_str_py, &err_str_len);
82 while (err_str_len > 0 && err_str[err_str_len - 1] == '\n') {
83 err_str_len -= 1;
84 }
85
86 if (error_prefix == nullptr) {
87 /* Not very helpful, better than nothing. */
88 error_prefix = "Python";
89 }
90
91 const char *location_filepath = nullptr;
92 int location_line_number = -1;
93
94 /* Give some additional context. */
95 if (use_location) {
96 PyC_FileAndNum(&location_filepath, &location_line_number);
97 }
98
99 /* Create a temporary report list so none of the reports are printed (only stored).
100 * In practically all cases printing should be handled by #PyErr_Print since this invokes
101 * `sys.excepthook` as expected. */
102 ReportList _reports_buf = {{nullptr}};
103 ReportList *reports_orig = reports;
104 if ((reports->flag & RPT_PRINT_HANDLED_BY_OWNER) == 0) {
105 reports = &_reports_buf;
106 BKE_reports_init(reports, reports_orig->flag | RPT_PRINT_HANDLED_BY_OWNER);
107 reports->storelevel = reports_orig->storelevel;
108 }
109
110 if (location_filepath) {
111 BKE_reportf(reports,
112 RPT_ERROR,
113 "%s: %.*s\n"
114 /* Location (when available). */
115 "Location: %s:%d",
116 error_prefix,
117 int(err_str_len),
118 err_str,
119 location_filepath,
120 location_line_number);
121 }
122 else {
123 BKE_reportf(reports, RPT_ERROR, "%s: %.*s", error_prefix, int(err_str_len), err_str);
124 }
125
126 if (reports != reports_orig) {
127 BKE_reports_move_to_reports(reports_orig, reports);
128 BKE_reports_free(reports);
129 }
130
131 /* Ensure this is _always_ printed to the output so developers don't miss exceptions. */
132 Py_DECREF(err_str_py);
133 return true;
134}
135
137{
138 return BPy_errors_to_report_ex(reports, nullptr, true, true);
139}
140
141} // namespace blender
Read Blender memory allocation and freeing.
BMesh Iterators.
void BKE_reports_free(ReportList *reports)
Definition report.cc:106
void BKE_reports_init(ReportList *reports, int flag)
Definition report.cc:91
void BPy_reports_write_stdout(const ReportList *reports, const char *header)
void BKE_reports_move_to_reports(ReportList *reports_dst, ReportList *reports_src)
Definition report.cc:150
bool BPy_errors_to_report(ReportList *reports)
PyObject * PyC_ExceptionBuffer_Simple()
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
Definition report.cc:191
void PyC_FileAndNum(const char **r_filename, int *r_lineno)
bool BPy_errors_to_report_ex(ReportList *reports, const char *error_prefix, const bool use_full, const bool use_location)
PyObject * PyC_ExceptionBuffer()
short BPy_reports_to_error(ReportList *reports, PyObject *exception, const bool clear)
@ RPT_PRINT_HANDLED_BY_OWNER
Definition BKE_report.hh:62
char * BKE_reports_string(ReportList *reports, eReportType level)
Definition report.cc:307
ListBaseT< Report > list
Definition BKE_report.hh:79
const char * typestr
Definition BKE_report.hh:72
const char * message
Definition BKE_report.hh:73