Blender V5.3
bpy_interface_run.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
8
9#include <cstdio>
10#include <optional>
11
12#include <Python.h>
13
14#include "MEM_guardedalloc.h"
15
16#include "BLI_fileops.hh"
17#include "BLI_function_ref.hh"
18#include "BLI_listbase.hh"
19#include "BLI_path_utils.hh"
20#include "BLI_string.hh"
21#include "BLI_string_utils.hh"
22
23#include "BKE_context.hh"
24#include "BKE_idprop.hh"
25#include "BKE_library.hh"
26#include "BKE_main.hh"
27#include "BKE_report.hh"
28#include "BKE_text.h"
29
30#include "DNA_text_types.h"
31
32#include "BPY_extern.hh"
33#include "BPY_extern_run.hh"
34
35#include "bpy_capi_utils.hh"
36#include "bpy_traceback.hh"
37
40
41namespace blender {
42
43/* -------------------------------------------------------------------- */
46
47static void python_script_error_jump_text(Text *text, const char *filepath)
48{
49 int lineno, lineno_end;
50 int offset, offset_end;
51 if (python_script_error_jump(filepath, &lineno, &offset, &lineno_end, &offset_end)) {
52 /* Start at the end so cursor motion that looses the selection,
53 * leaves the cursor from the most useful place.
54 * Also, the end can't always be set, so don't give it priority. */
55 txt_move_to(text, lineno_end - 1, offset_end - 1, false);
56 txt_move_to(text, lineno - 1, offset - 1, true);
57 }
58}
59
63static void bpy_text_filepath_get(char *filepath,
64 const size_t filepath_maxncpy,
65 const Main *bmain,
66 const Text *text)
67{
69 filepath, filepath_maxncpy, SEP, ID_BLEND_PATH(bmain, &text->id), text->id.name + 2);
70}
71
76 const char *filepath,
77 const int start,
78 PyObject *globals,
79 PyObject *locals,
80 const int closeit,
81 PyCompilerFlags *flags)
82{
83 /* Previously we used #PyRun_File to run directly the code on a FILE
84 * object, but as written in the Python/C API Ref Manual, chapter 2,
85 * 'FILE structs for different C libraries can be different and incompatible'.
86 * So now we load the script file data to a buffer on MS-Windows. */
87#ifdef _WIN32
88 bool use_file_handle_workaround = true;
89#else
90 bool use_file_handle_workaround = false;
91#endif
92
93 if (!use_file_handle_workaround) {
94 return PyRun_FileExFlags(fp, filepath, start, globals, locals, closeit, flags);
95 }
96
97 PyObject *py_result = nullptr;
98 size_t buf_len;
99 char *buf = static_cast<char *>(BLI_file_read_data_as_mem_from_handle(fp, false, 1, &buf_len));
100 if (closeit) {
101 fclose(fp);
102 }
103
104 if (buf == nullptr) [[unlikely]] {
105 PyErr_Format(PyExc_IOError, "Python file \"%s\" could not read buffer", filepath);
106 }
107 else {
108 buf[buf_len] = '\0';
109 PyObject *filepath_py = PyC_UnicodeFromBytes(filepath);
110 PyObject *compiled = Py_CompileStringObject(buf, filepath_py, Py_file_input, flags, -1);
111 MEM_delete(buf);
112 Py_DECREF(filepath_py);
113
114 if (compiled == nullptr) {
115 /* Based on Python's internal usage, an error must always be set. */
116 BLI_assert(PyErr_Occurred());
117 }
118 else {
119 py_result = PyEval_EvalCode(compiled, globals, locals);
120 Py_DECREF(compiled);
121 }
122 }
123 return py_result;
124}
125
135 bContext *C, const char *filepath, Text *text, ReportList *reports, const bool do_jump)
136{
137 BLI_assert(filepath || text);
138 if (filepath == nullptr && text == nullptr) {
139 return false;
140 }
141
142 PyGILState_STATE gilstate;
143 bpy_context_set(C, &gilstate);
144
145 Main *bmain_old = CTX_data_main(C);
146 PyObject *py_dict = nullptr, *py_result = nullptr;
147
148 char filepath_dummy[FILE_MAX];
150 const char *filepath_namespace = nullptr;
151
152 PyObject *main_mod = PyC_MainModule_Backup();
153
154 if (text) {
155 bpy_text_filepath_get(filepath_dummy, sizeof(filepath_dummy), bmain_old, text);
156 filepath_namespace = filepath_dummy;
157
158 if (text->compiled == nullptr) { /* if it wasn't already compiled, do it now */
159 PyObject *filepath_dummy_py = PyC_UnicodeFromBytes(filepath_dummy);
160 size_t buf_len_dummy;
161 char *buf = txt_to_buf(text, &buf_len_dummy);
162 text->compiled = Py_CompileStringObject(buf, filepath_dummy_py, Py_file_input, nullptr, -1);
163 MEM_delete(buf);
164 Py_DECREF(filepath_dummy_py);
165 }
166
167 if (text->compiled) {
168 py_dict = PyC_DefaultNameSpace(filepath_dummy);
169 py_result = PyEval_EvalCode(static_cast<PyObject *>(text->compiled), py_dict, py_dict);
170 }
171 }
172 else {
173 FILE *fp = BLI_fopen(filepath, "rb");
174 filepath_namespace = filepath;
175
176 if (fp) {
177 /* Matches behavior of running Python with a directory argument.
178 * Without the `fstat`, the directory will execute & return None. */
179 BLI_stat_t st;
180 if (BLI_fstat(fileno(fp), &st) == 0 && S_ISDIR(st.st_mode)) {
181 PyErr_Format(PyExc_IsADirectoryError, "Python file \"%s\" is a directory", filepath);
182 BLI_assert(py_result == nullptr);
183 fclose(fp);
184 }
185 else {
186 /* Calls `fclose(fp)`, run the script with one fewer open files. */
187 const int closeit = 1;
188 py_dict = PyC_DefaultNameSpace(filepath);
190 fp, filepath, Py_file_input, py_dict, py_dict, closeit, nullptr);
191 }
192 }
193 else {
194 PyErr_Format(
195 PyExc_IOError, "Python file \"%s\" could not be opened: %s", filepath, strerror(errno));
196 BLI_assert(py_result == nullptr);
197 }
198 }
199
200 if (!py_result) {
201 if (reports) {
202 BPy_errors_to_report(reports);
203 }
204 else {
206 }
207 if (text) {
208 if (do_jump) {
209 /* ensure text is valid before use, the script may have freed itself */
210 Main *bmain_new = CTX_data_main(C);
211 if ((bmain_old == bmain_new) && (BLI_findindex(&bmain_new->texts, text) != -1)) {
212 python_script_error_jump_text(text, filepath_namespace);
213 }
214 }
215 }
216 if (reports) {
217 PyErr_Clear();
218 }
219 else {
220 PyErr_Print();
221 }
222 }
223 else {
224 Py_DECREF(py_result);
225 }
226
227 PyC_MainModule_Restore(main_mod);
228
229 /* Flush `stdout` & `stderr` to ensure the script output is visible.
230 * Using `fflush(stdout)` does not solve it. */
232
233 bpy_context_clear(C, &gilstate);
234
235 return (py_result != nullptr);
236}
237
239
240/* -------------------------------------------------------------------- */
243
244bool BPY_run_filepath(bContext *C, const char *filepath, ReportList *reports)
245{
246 return python_script_exec(C, filepath, nullptr, reports, false);
247}
248
249bool BPY_run_text(bContext *C, Text *text, ReportList *reports, const bool do_jump)
250{
251 return python_script_exec(C, nullptr, text, reports, do_jump);
252}
253
254bool BPY_string_compile_check(const char *expr)
255{
256 if (!expr || expr[0] == '\0') {
257 return true;
258 }
259 PyGILState_STATE gilstate = PyGILState_Ensure();
260 bool success = false;
261 if (PyObject *retval = Py_CompileString(expr, "<expression>", Py_eval_input)) {
262 Py_DECREF(retval);
263 success = true;
264 }
265 else {
266 PyErr_Clear();
267 }
268 PyGILState_Release(gilstate);
269 return success;
270}
271
277{
278 bContext *C_prev = BPY_context_get();
279 PyGILState_STATE gilstate;
280 const bool context_set = bpy_context_set_allow_null(C, &gilstate);
281
282 fn();
283
284 /* Failure to restore `C_prev` may leave it null, see #160604. */
285 if (context_set && (C != C_prev)) {
286 BPY_context_update(C_prev);
287 }
288 bpy_context_clear(C, &gilstate);
289}
290
296 const char *imports[],
297 const char *expr,
298 const int mode)
299{
300 BLI_assert(expr);
301 bool ok = true;
302
303 if (expr[0] == '\0') {
304 return ok;
305 }
306
308 PyObject *py_dict, *retval;
309
310 PyObject *main_mod = PyC_MainModule_Backup();
311
312 py_dict = PyC_DefaultNameSpace("<blender string>");
313
314 if (imports && !PyC_NameSpace_ImportArray(py_dict, imports)) {
315 retval = nullptr;
316 }
317 else {
318 retval = PyRun_String(expr, mode, py_dict, py_dict);
319 }
320
321 if (retval == nullptr) {
322 ok = false;
323 if (ReportList *wm_reports = C ? CTX_wm_reports(C) : nullptr) {
324 BPy_errors_to_report(wm_reports);
325 }
326 else {
328 }
329 PyErr_Print();
330 }
331 else {
332 Py_DECREF(retval);
333 }
334
335 PyC_MainModule_Restore(main_mod);
336 });
337
338 return ok;
339}
340
341bool BPY_run_string_eval(bContext *C, const char *imports[], const char *expr)
342{
343 return bpy_run_string_impl(C, imports, expr, Py_eval_input);
344}
345
346bool BPY_run_string_exec(bContext *C, const char *imports[], const char *expr)
347{
348 return bpy_run_string_impl(C, imports, expr, Py_file_input);
349}
350
360static IDProperty *pyobject_to_idprop(const StringRefNull prop_name, PyObject *py_object)
361{
362 if (py_object == Py_None) {
363 return nullptr;
364 }
365 return BPy_IDProperty_FromPyObject(nullptr, prop_name.c_str(), py_object, false, true);
366}
367
376 const StringRefNull script,
377 IDProperty &locals,
378 FunctionRef<void(PyObject *py_locals)> on_exec_ok)
379{
380 /* Set up locals & globals. */
381 BLI_assert(locals.type == IDP_GROUP);
382 PyObject *py_locals = BPy_IDGroup_MapDataToPy(&locals);
383 if (!py_locals) {
384 /* Leave the printing of the exception to the caller. */
385 return false;
386 }
387
388 PyObject *py_globals = PyC_DefaultNameSpace("<BPY_run_string_exec_with_locals>");
389 BLI_assert(py_globals);
390
391 /* Run the script. The result object itself is not used, but its existence
392 * indicates that the script ran without uncaught exceptions. The printing of
393 * any exception is left to the caller. */
394 PyObject *result = PyRun_String(script.c_str(), Py_file_input, py_globals, py_locals);
395 const bool ok = (result != nullptr);
396 if (ok) {
397 Py_DECREF(result);
398 if (on_exec_ok) {
399 on_exec_ok(py_locals);
400 }
401 }
402
403 /* Clean up references. */
404 Py_DECREF(py_locals);
405
406 return ok;
407}
408
410 bContext *C,
411 const StringRefNull script,
412 IDProperty &locals,
413 FunctionRef<void(PyObject *py_locals)> on_exec_ok)
414{
415 bool ok = false;
416
418 PyObject *main_mod_backup = PyC_MainModule_Backup();
419
420 ok = bpy_run_string_exec_with_locals_assume_gil(script, locals, on_exec_ok);
421 if (!ok) {
422 if (ReportList *wm_reports = C ? CTX_wm_reports(C) : nullptr) {
423 BPy_errors_to_report(wm_reports);
424 }
425 else {
427 }
428 PyErr_Print();
429 }
430
431 PyC_MainModule_Restore(main_mod_backup);
432 });
433
434 return ok;
435}
436
438{
439 return bpy_run_string_exec_with_locals_acquire_gil(C, script, locals, nullptr);
440}
441
443 bContext *C,
444 const StringRefNull script,
445 IDProperty &locals,
446 const StringRefNull result_var_name)
447{
448 BLI_assert(!result_var_name.is_empty());
449
450 std::optional<IDProperty *> result_idprop;
451
452 const auto on_exec_ok = [&result_var_name, &result_idprop](PyObject *py_locals) {
453 PyObject *py_ret = PyDict_GetItemString(py_locals, result_var_name.c_str());
454 if (!py_ret) {
455 /* _result was not defined by the script, translates to 'no value'. */
456 return;
457 }
458
459 if (py_ret == Py_None) {
460 /* _result = None, which translates to a nullptr value. */
461 result_idprop = nullptr;
462 return;
463 }
464
465 result_idprop = pyobject_to_idprop(result_var_name, py_ret);
466 if (!result_idprop) {
467 PyErr_Print();
468 }
469 };
470
471 const bool exec_ok = bpy_run_string_exec_with_locals_acquire_gil(C, script, locals, on_exec_ok);
472 if (!exec_ok) {
473 BLI_assert(!result_idprop.has_value());
474 return std::nullopt;
475 }
476
477 return result_idprop;
478}
479
481
482/* -------------------------------------------------------------------- */
488
490{
491 BLI_assert(PyErr_Occurred());
492
493 if (err_info == nullptr) {
494 PyErr_Print();
495 return;
496 }
497
498 /* Signal to do nothing. */
499 if (!(err_info->reports || err_info->r_string)) {
500 PyErr_Clear();
501 return;
502 }
503
504 PyObject *py_err_str = err_info->use_single_line_error ? PyC_ExceptionBuffer_Simple() :
506 const char *err_str = PyUnicode_AsUTF8(py_err_str);
507 PyErr_Clear();
508
509 if (err_info->reports != nullptr) {
510 if (err_info->report_prefix) {
511 BKE_reportf(err_info->reports, RPT_ERROR, "%s: %s", err_info->report_prefix, err_str);
512 }
513 else {
514 BKE_report(err_info->reports, RPT_ERROR, err_str);
515 }
516 }
517
518 /* Print the reports if they were not printed already. */
519 if ((err_info->reports == nullptr) || !BKE_reports_print_test(err_info->reports, RPT_ERROR)) {
520 if (err_info->report_prefix) {
521 fprintf(stderr, "%s: ", err_info->report_prefix);
522 }
523 fprintf(stderr, "%s\n", err_str);
524 }
525
526 if (err_info->r_string != nullptr) {
527 *err_info->r_string = BLI_strdup(err_str);
528 }
529
530 Py_XDECREF(py_err_str);
531}
532
534 const char *imports[],
535 const char *expr,
536 BPy_RunErrInfo *err_info,
537 double *r_value)
538{
539 bool ok = true;
540
541 if (expr[0] == '\0') {
542 *r_value = 0.0;
543 return ok;
544 }
545
547 ok = PyC_RunString_AsNumber(imports, expr, "<expr as number>", r_value);
548
549 if (ok == false) {
550 run_string_handle_error(err_info);
551 }
552 });
553
554 return ok;
555}
556
558 const char *imports[],
559 const char *expr,
560 BPy_RunErrInfo *err_info,
561 char **r_value,
562 size_t *r_value_len)
563{
564 bool ok = true;
565
566 if (expr[0] == '\0') {
567 *r_value = nullptr;
568 return ok;
569 }
570
572 ok = PyC_RunString_AsStringAndSize(imports, expr, "<expr as str>", r_value, r_value_len);
573
574 if (ok == false) {
575 run_string_handle_error(err_info);
576 }
577 });
578
579 return ok;
580}
581
583 bContext *C, const char *imports[], const char *expr, BPy_RunErrInfo *err_info, char **r_value)
584{
585 size_t value_dummy_len;
586 return BPY_run_string_as_string_and_len(C, imports, expr, err_info, r_value, &value_dummy_len);
587}
588
590 const char *imports[],
591 const char *expr,
592 BPy_RunErrInfo *err_info,
593 char **r_value,
594 size_t *r_value_len)
595{
596 bool ok = true;
597
598 if (expr[0] == '\0') {
599 *r_value = nullptr;
600 return ok;
601 }
602
605 imports, expr, "<expr as str or none>", r_value, r_value_len);
606
607 if (ok == false) {
608 run_string_handle_error(err_info);
609 }
610 });
611
612 return ok;
613}
614
616 bContext *C, const char *imports[], const char *expr, BPy_RunErrInfo *err_info, char **r_value)
617{
618 size_t value_dummy_len;
620 C, imports, expr, err_info, r_value, &value_dummy_len);
621}
622
624 const char *imports[],
625 const char *expr,
626 BPy_RunErrInfo *err_info,
627 intptr_t *r_value)
628{
629 bool ok = true;
630
631 if (expr[0] == '\0') {
632 *r_value = 0;
633 return ok;
634 }
635
637 ok = PyC_RunString_AsIntPtr(imports, expr, "<expr as intptr>", r_value);
638
639 if (ok == false) {
640 run_string_handle_error(err_info);
641 }
642 });
643
644 return ok;
645}
646
648
649} // namespace blender
File and directory operations.
#define FILE_MAX
#define BLI_string_join_by_sep_char(...)
#define S_ISDIR(x)
#define ID_BLEND_PATH(_bmain, _id)
Definition DNA_ID.h:688
Read Blender memory allocation and freeing.
#define C
Definition RandGen.cpp:29
constexpr const char * c_str() const
constexpr bool is_empty() const
BMesh Iterators.
static bool bpy_run_string_exec_with_locals_acquire_gil(bContext *C, const StringRefNull script, IDProperty &locals, FunctionRef< void(PyObject *py_locals)> on_exec_ok)
PyObject * PyC_MainModule_Backup()
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
static PyObject * python_compat_wrapper_PyRun_FileExFlags(FILE *fp, const char *filepath, const int start, PyObject *globals, PyObject *locals, const int closeit, PyCompilerFlags *flags)
int BLI_fstat(int fd, BLI_stat_t *buffer) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
bool PyC_RunString_AsNumber(const char *imports[], const char *expr, const char *filename, double *r_value)
bool BPY_run_string_exec_with_locals(bContext *C, StringRefNull script, IDProperty &locals)
static void python_script_error_jump_text(Text *text, const char *filepath)
static void bpy_text_filepath_get(char *filepath, const size_t filepath_maxncpy, const Main *bmain, const Text *text)
bool BPY_run_string_as_number(bContext *C, const char *imports[], const char *expr, BPy_RunErrInfo *err_info, double *r_value) ATTR_NONNULL(1
bool bool bool BPY_run_string_as_string_and_len(bContext *C, const char *imports[], const char *expr, BPy_RunErrInfo *err_info, char **r_value, size_t *r_value_len) ATTR_NONNULL(1
FILE * BLI_fopen(const char *filepath, const char *mode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
static void run_string_handle_error(BPy_RunErrInfo *err_info)
PyObject * BPy_IDGroup_MapDataToPy(IDProperty *prop)
bool bool BPY_run_text(bContext *C, Text *text, ReportList *reports, bool do_jump) ATTR_NONNULL(1
PyObject * PyC_UnicodeFromBytes(const char *str)
bool BPy_errors_to_report(ReportList *reports)
void * BLI_file_read_data_as_mem_from_handle(FILE *fp, bool read_size_exact, size_t pad_bytes, size_t *r_size)
Definition storage.cc:482
int BLI_findindex(const ListBase *listbase, const void *vlink) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:593
bool BPY_run_filepath(bContext *C, const char *filepath, ReportList *reports) ATTR_NONNULL(1
PyObject * PyC_DefaultNameSpace(const char *filename)
struct bContext * BPY_context_get()
static bool bpy_run_string_impl(bContext *C, const char *imports[], const char *expr, const int mode)
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:162
bool PyC_RunString_AsStringAndSizeOrNone(const char *imports[], const char *expr, const char *filename, char **r_value, size_t *r_value_size)
bool python_script_error_jump(const char *filepath, int *r_lineno, int *r_offset, int *r_lineno_end, int *r_offset_end)
bool BPY_run_string_eval(bContext *C, const char *imports[], const char *expr)
PyObject * PyC_ExceptionBuffer_Simple()
bool bpy_context_set_allow_null(struct bContext *C, PyGILState_STATE *gilstate)
bool PyC_RunString_AsStringAndSize(const char *imports[], const char *expr, const char *filename, char **r_value, size_t *r_value_size)
bool PyC_RunString_AsIntPtr(const char *imports[], const char *expr, const char *filename, intptr_t *r_value)
bool bool BPY_run_string_as_intptr(bContext *C, const char *imports[], const char *expr, BPy_RunErrInfo *err_info, intptr_t *r_value) ATTR_NONNULL(1
static void bpy_run_with_context_or_null(bContext *C, FunctionRef< void()> fn)
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
Definition report.cc:191
Main * CTX_data_main(const bContext *C)
char * BLI_strdup(const char *str) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1) ATTR_MALLOC
bool bpy_context_set(struct bContext *C, PyGILState_STATE *gilstate) ATTR_NONNULL(1)
void PyC_StdFilesFlush()
std::optional< IDProperty * > BPY_run_string_exec_with_locals_return_idprop(bContext *C, StringRefNull script, IDProperty &locals, StringRefNull result_var_name)
static IDProperty * pyobject_to_idprop(const StringRefNull prop_name, PyObject *py_object)
char * txt_to_buf(struct Text *text, size_t *r_buf_strlen) ATTR_NONNULL(1
Definition text.cc:1434
bool BPY_string_compile_check(const char *expr)
static bool python_script_exec(bContext *C, const char *filepath, Text *text, ReportList *reports, const bool do_jump)
void PyC_MainModule_Restore(PyObject *main_mod)
static bool bpy_run_string_exec_with_locals_assume_gil(const StringRefNull script, IDProperty &locals, FunctionRef< void(PyObject *py_locals)> on_exec_ok)
bool bool bool bool bool BPY_run_string_as_string_and_len_or_none(bContext *C, const char *imports[], const char *expr, BPy_RunErrInfo *err_info, char **r_value, size_t *r_value_len) ATTR_NONNULL(1
bool BPY_run_string_exec(bContext *C, const char *imports[], const char *expr)
PyObject * PyC_ExceptionBuffer()
struct stat BLI_stat_t
IDProperty * BPy_IDProperty_FromPyObject(IDProperty *prop_exist, const char *name, PyObject *ob, const bool do_conversion, const bool can_create)
bool PyC_NameSpace_ImportArray(PyObject *py_dict, const char *imports[])
void BPY_context_update(bContext *C)
constexpr char SEP
ReportList * CTX_wm_reports(const bContext *C)
bool PyC_Err_CaptureSystemExitCode()
void bpy_context_clear(struct bContext *C, const PyGILState_STATE *gilstate)
bool BKE_reports_print_test(const ReportList *reports, eReportType type)
Definition report.cc:336
void txt_move_to(struct Text *text, unsigned int line, unsigned int ch, bool sel)
Definition text.cc:1104
bool bool bool bool BPY_run_string_as_string(bContext *C, const char *imports[], const char *expr, BPy_RunErrInfo *err_info, char **r_value) ATTR_NONNULL(1
_W64 int intptr_t
Definition BKE_text.h:119
bool bool bool bool bool bool BPY_run_string_as_string_or_none(bContext *C, const char *imports[], const char *expr, BPy_RunErrInfo *err_info, char **r_value) ATTR_NONNULL(1
eIDPropertyType type
Definition DNA_ID.h:145
ListBaseT< Text > texts
Definition BKE_main.hh:402