Blender  V2.93
bpy_interface_run.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
21 #include <stdio.h>
22 
23 #include <Python.h>
24 
25 #include "MEM_guardedalloc.h"
26 
27 #include "BLI_fileops.h"
28 #include "BLI_listbase.h"
29 #include "BLI_path_util.h"
30 #include "BLI_string.h"
31 
32 #include "BKE_context.h"
33 #include "BKE_main.h"
34 #include "BKE_report.h"
35 #include "BKE_text.h"
36 
37 #include "DNA_text_types.h"
38 
39 #include "BPY_extern.h"
40 #include "BPY_extern_run.h"
41 
42 #include "bpy_capi_utils.h"
43 #include "bpy_intern_string.h"
44 #include "bpy_traceback.h"
45 
46 #include "../generic/py_capi_utils.h"
47 
48 /* -------------------------------------------------------------------- */
53 {
54  int lineno;
55  int offset;
56  python_script_error_jump(text->id.name + 2, &lineno, &offset);
57  if (lineno != -1) {
58  /* select the line with the error */
59  txt_move_to(text, lineno - 1, INT_MAX, false);
60  txt_move_to(text, lineno - 1, offset, true);
61  }
62 }
63 
64 /* returns a dummy filename for a textblock so we can tell what file a text block comes from */
65 static void bpy_text_filename_get(char *fn, const Main *bmain, size_t fn_len, const Text *text)
66 {
67  BLI_snprintf(fn, fn_len, "%s%c%s", ID_BLEND_PATH(bmain, &text->id), SEP, text->id.name + 2);
68 }
69 
70 /* Very annoying! Undo #_PyModule_Clear(), see T23871. */
71 #define PYMODULE_CLEAR_WORKAROUND
72 
73 #ifdef PYMODULE_CLEAR_WORKAROUND
74 /* bad!, we should never do this, but currently only safe way I could find to keep namespace.
75  * from being cleared. - campbell */
76 typedef struct {
77  PyObject_HEAD PyObject *md_dict;
78  /* omit other values, we only want the dict. */
80 #endif
81 
82 static bool python_script_exec(
83  bContext *C, const char *fn, struct Text *text, struct ReportList *reports, const bool do_jump)
84 {
85  Main *bmain_old = CTX_data_main(C);
86  PyObject *main_mod = NULL;
87  PyObject *py_dict = NULL, *py_result = NULL;
88  PyGILState_STATE gilstate;
89 
90  BLI_assert(fn || text);
91 
92  if (fn == NULL && text == NULL) {
93  return 0;
94  }
95 
96  bpy_context_set(C, &gilstate);
97 
98  PyC_MainModule_Backup(&main_mod);
99 
100  if (text) {
101  char fn_dummy[FILE_MAXDIR];
102  bpy_text_filename_get(fn_dummy, bmain_old, sizeof(fn_dummy), text);
103 
104  if (text->compiled == NULL) { /* if it wasn't already compiled, do it now */
105  char *buf;
106  PyObject *fn_dummy_py;
107 
108  fn_dummy_py = PyC_UnicodeFromByte(fn_dummy);
109 
110  buf = txt_to_buf(text, NULL);
111  text->compiled = Py_CompileStringObject(buf, fn_dummy_py, Py_file_input, NULL, -1);
112  MEM_freeN(buf);
113 
114  Py_DECREF(fn_dummy_py);
115 
116  if (PyErr_Occurred()) {
117  if (do_jump) {
119  }
120  BPY_text_free_code(text);
121  }
122  }
123 
124  if (text->compiled) {
125  py_dict = PyC_DefaultNameSpace(fn_dummy);
126  py_result = PyEval_EvalCode(text->compiled, py_dict, py_dict);
127  }
128  }
129  else {
130  FILE *fp = BLI_fopen(fn, "r");
131 
132  if (fp) {
133  py_dict = PyC_DefaultNameSpace(fn);
134 
135 #ifdef _WIN32
136  /* Previously we used PyRun_File to run directly the code on a FILE
137  * object, but as written in the Python/C API Ref Manual, chapter 2,
138  * 'FILE structs for different C libraries can be different and
139  * incompatible'.
140  * So now we load the script file data to a buffer.
141  *
142  * Note on use of 'globals()', it's important not copy the dictionary because
143  * tools may inspect 'sys.modules["__main__"]' for variables defined in the code
144  * where using a copy of 'globals()' causes code execution
145  * to leave the main namespace untouched. see: T51444
146  *
147  * This leaves us with the problem of variables being included,
148  * currently this is worked around using 'dict.__del__' it's ugly but works.
149  */
150  {
151  const char *pystring =
152  "with open(__file__, 'rb') as f:"
153  "exec(compile(f.read(), __file__, 'exec'), globals().__delitem__('f') or globals())";
154 
155  fclose(fp);
156 
157  py_result = PyRun_String(pystring, Py_file_input, py_dict, py_dict);
158  }
159 #else
160  py_result = PyRun_File(fp, fn, Py_file_input, py_dict, py_dict);
161  fclose(fp);
162 #endif
163  }
164  else {
165  PyErr_Format(
166  PyExc_IOError, "Python file \"%s\" could not be opened: %s", fn, strerror(errno));
167  py_result = NULL;
168  }
169  }
170 
171  if (!py_result) {
172  if (text) {
173  if (do_jump) {
174  /* ensure text is valid before use, the script may have freed its self */
175  Main *bmain_new = CTX_data_main(C);
176  if ((bmain_old == bmain_new) && (BLI_findindex(&bmain_new->texts, text) != -1)) {
178  }
179  }
180  }
181  BPy_errors_to_report(reports);
182  }
183  else {
184  Py_DECREF(py_result);
185  }
186 
187  if (py_dict) {
188 #ifdef PYMODULE_CLEAR_WORKAROUND
189  PyModuleObject *mmod = (PyModuleObject *)PyDict_GetItem(PyImport_GetModuleDict(),
191  PyObject *dict_back = mmod->md_dict;
192  /* freeing the module will clear the namespace,
193  * gives problems running classes defined in this namespace being used later. */
194  mmod->md_dict = NULL;
195  Py_DECREF(dict_back);
196 #endif
197 
198 #undef PYMODULE_CLEAR_WORKAROUND
199  }
200 
201  PyC_MainModule_Restore(main_mod);
202 
203  bpy_context_clear(C, &gilstate);
204 
205  return (py_result != NULL);
206 }
207 
210 /* -------------------------------------------------------------------- */
214 /* Can run a file or text block */
215 bool BPY_run_filepath(bContext *C, const char *filepath, struct ReportList *reports)
216 {
217  return python_script_exec(C, filepath, NULL, reports, false);
218 }
219 
220 bool BPY_run_text(bContext *C, struct Text *text, struct ReportList *reports, const bool do_jump)
221 {
222  return python_script_exec(C, NULL, text, reports, do_jump);
223 }
224 
230  const char *imports[],
231  const char *expr,
232  const int mode)
233 {
234  BLI_assert(expr);
235  PyGILState_STATE gilstate;
236  PyObject *main_mod = NULL;
237  PyObject *py_dict, *retval;
238  bool ok = true;
239 
240  if (expr[0] == '\0') {
241  return ok;
242  }
243 
244  bpy_context_set(C, &gilstate);
245 
246  PyC_MainModule_Backup(&main_mod);
247 
248  py_dict = PyC_DefaultNameSpace("<blender string>");
249 
250  if (imports && (!PyC_NameSpace_ImportArray(py_dict, imports))) {
251  Py_DECREF(py_dict);
252  retval = NULL;
253  }
254  else {
255  retval = PyRun_String(expr, mode, py_dict, py_dict);
256  }
257 
258  if (retval == NULL) {
259  ok = false;
261  }
262  else {
263  Py_DECREF(retval);
264  }
265 
266  PyC_MainModule_Restore(main_mod);
267 
268  bpy_context_clear(C, &gilstate);
269 
270  return ok;
271 }
272 
276 bool BPY_run_string_eval(bContext *C, const char *imports[], const char *expr)
277 {
278  return bpy_run_string_impl(C, imports, expr, Py_eval_input);
279 }
280 
284 bool BPY_run_string_exec(bContext *C, const char *imports[], const char *expr)
285 {
286  return bpy_run_string_impl(C, imports, expr, Py_file_input);
287 }
288 
291 /* -------------------------------------------------------------------- */
298 static void run_string_handle_error(struct BPy_RunErrInfo *err_info)
299 {
300  if (err_info == NULL) {
301  PyErr_Print();
302  PyErr_Clear();
303  return;
304  }
305 
306  /* Signal to do nothing. */
307  if (!(err_info->reports || err_info->r_string)) {
308  PyErr_Clear();
309  return;
310  }
311 
312  PyObject *py_err_str = err_info->use_single_line_error ? PyC_ExceptionBuffer_Simple() :
314  const char *err_str = py_err_str ? PyUnicode_AsUTF8(py_err_str) : "Unable to extract exception";
315 
316  if (err_info->reports != NULL) {
317  if (err_info->report_prefix) {
318  BKE_reportf(err_info->reports, RPT_ERROR, "%s: %s", err_info->report_prefix, err_str);
319  }
320  else {
321  BKE_report(err_info->reports, RPT_ERROR, err_str);
322  }
323  }
324 
325  if (err_info->r_string != NULL) {
326  *err_info->r_string = BLI_strdup(err_str);
327  }
328 
329  Py_XDECREF(py_err_str);
330 }
331 
336  const char *imports[],
337  const char *expr,
338  struct BPy_RunErrInfo *err_info,
339  double *r_value)
340 {
341  PyGILState_STATE gilstate;
342  bool ok = true;
343 
344  if (!r_value || !expr) {
345  return -1;
346  }
347 
348  if (expr[0] == '\0') {
349  *r_value = 0.0;
350  return ok;
351  }
352 
353  bpy_context_set(C, &gilstate);
354 
355  ok = PyC_RunString_AsNumber(imports, expr, "<expr as number>", r_value);
356 
357  if (ok == false) {
358  run_string_handle_error(err_info);
359  }
360 
361  bpy_context_clear(C, &gilstate);
362 
363  return ok;
364 }
365 
370  const char *imports[],
371  const char *expr,
372  struct BPy_RunErrInfo *err_info,
373  char **r_value,
374  size_t *r_value_size)
375 {
376  BLI_assert(r_value && expr);
377  PyGILState_STATE gilstate;
378  bool ok = true;
379 
380  if (expr[0] == '\0') {
381  *r_value = NULL;
382  return ok;
383  }
384 
385  bpy_context_set(C, &gilstate);
386 
387  ok = PyC_RunString_AsStringAndSize(imports, expr, "<expr as str>", r_value, r_value_size);
388 
389  if (ok == false) {
390  run_string_handle_error(err_info);
391  }
392 
393  bpy_context_clear(C, &gilstate);
394 
395  return ok;
396 }
397 
399  const char *imports[],
400  const char *expr,
401  struct BPy_RunErrInfo *err_info,
402  char **r_value)
403 {
404  size_t value_dummy_size;
405  return BPY_run_string_as_string_and_size(C, imports, expr, err_info, r_value, &value_dummy_size);
406 }
407 
414  const char *imports[],
415  const char *expr,
416  struct BPy_RunErrInfo *err_info,
417  intptr_t *r_value)
418 {
419  BLI_assert(r_value && expr);
420  PyGILState_STATE gilstate;
421  bool ok = true;
422 
423  if (expr[0] == '\0') {
424  *r_value = 0;
425  return ok;
426  }
427 
428  bpy_context_set(C, &gilstate);
429 
430  ok = PyC_RunString_AsIntPtr(imports, expr, "<expr as intptr>", r_value);
431 
432  if (ok == false) {
433  run_string_handle_error(err_info);
434  }
435 
436  bpy_context_clear(C, &gilstate);
437 
438  return ok;
439 }
440 
struct ReportList * CTX_wm_reports(const bContext *C)
Definition: context.c:751
struct Main * CTX_data_main(const bContext *C)
Definition: context.c:1018
void BKE_report(ReportList *reports, ReportType type, const char *message)
Definition: report.c:104
void BKE_reportf(ReportList *reports, ReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
char * txt_to_buf(struct Text *text, int *r_buf_strlen)
Definition: text.c:1478
void txt_move_to(struct Text *text, unsigned int line, unsigned int ch, const bool sel)
Definition: text.c:1137
#define BLI_assert(a)
Definition: BLI_assert.h:58
File and directory operations.
FILE * BLI_fopen(const char *filename, const char *mode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition: fileops.c:1003
int BLI_findindex(const struct ListBase *listbase, const void *vlink) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
#define SEP
#define FILE_MAXDIR
char * BLI_strdup(const char *str) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL() ATTR_MALLOC
Definition: string.c:70
size_t BLI_snprintf(char *__restrict dst, size_t maxncpy, const char *__restrict format,...) ATTR_NONNULL(1
void BPY_text_free_code(struct Text *text)
#define ID_BLEND_PATH(_bmain, _id)
Definition: DNA_ID.h:419
Read Guarded memory(de)allocation.
#define C
Definition: RandGen.cpp:39
bool BPy_errors_to_report(ReportList *reports)
void bpy_context_clear(struct bContext *C, const PyGILState_STATE *gilstate)
void bpy_context_set(struct bContext *C, PyGILState_STATE *gilstate)
bool BPY_run_string_as_intptr(bContext *C, const char *imports[], const char *expr, struct BPy_RunErrInfo *err_info, intptr_t *r_value)
static bool python_script_exec(bContext *C, const char *fn, struct Text *text, struct ReportList *reports, const bool do_jump)
static void bpy_text_filename_get(char *fn, const Main *bmain, size_t fn_len, const Text *text)
bool BPY_run_string_eval(bContext *C, const char *imports[], const char *expr)
bool BPY_run_string_exec(bContext *C, const char *imports[], const char *expr)
bool BPY_run_string_as_number(bContext *C, const char *imports[], const char *expr, struct BPy_RunErrInfo *err_info, double *r_value)
static void run_string_handle_error(struct BPy_RunErrInfo *err_info)
bool BPY_run_string_as_string(bContext *C, const char *imports[], const char *expr, struct BPy_RunErrInfo *err_info, char **r_value)
bool BPY_run_filepath(bContext *C, const char *filepath, struct ReportList *reports)
bool BPY_run_string_as_string_and_size(bContext *C, const char *imports[], const char *expr, struct BPy_RunErrInfo *err_info, char **r_value, size_t *r_value_size)
bool BPY_run_text(bContext *C, struct Text *text, struct ReportList *reports, const bool do_jump)
static bool bpy_run_string_impl(bContext *C, const char *imports[], const char *expr, const int mode)
static void python_script_error_jump_text(Text *text)
PyObject * bpy_intern_str___main__
void python_script_error_jump(const char *filepath, int *lineno, int *offset)
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:41
PyObject * PyC_DefaultNameSpace(const char *filename)
PyObject * PyC_ExceptionBuffer(void)
bool PyC_RunString_AsNumber(const char *imports[], const char *expr, const char *filename, double *r_value)
PyObject * PyC_ExceptionBuffer_Simple(void)
void PyC_MainModule_Backup(PyObject **r_main_mod)
bool PyC_RunString_AsStringAndSize(const char *imports[], const char *expr, const char *filename, char **r_value, size_t *r_value_size)
bool PyC_NameSpace_ImportArray(PyObject *py_dict, const char *imports[])
void PyC_MainModule_Restore(PyObject *main_mod)
PyObject * PyC_UnicodeFromByte(const char *str)
bool PyC_RunString_AsIntPtr(const char *imports[], const char *expr, const char *filename, intptr_t *r_value)
_W64 int intptr_t
Definition: stdint.h:121
const char * report_prefix
struct ReportList * reports
bool use_single_line_error
char name[66]
Definition: DNA_ID.h:283
Definition: BKE_main.h:116
ListBase texts
Definition: BKE_main.h:163
PyObject_HEAD PyObject * md_dict
void * compiled