49 int lineno, lineno_end;
50 int offset, offset_end;
55 txt_move_to(text, lineno_end - 1, offset_end - 1,
false);
64 const size_t filepath_maxncpy,
69 filepath, filepath_maxncpy,
SEP,
ID_BLEND_PATH(bmain, &text->id), text->id.name + 2);
81 PyCompilerFlags *flags)
88 bool use_file_handle_workaround =
true;
90 bool use_file_handle_workaround =
false;
93 if (!use_file_handle_workaround) {
94 return PyRun_FileExFlags(fp, filepath, start, globals, locals, closeit, flags);
97 PyObject *py_result =
nullptr;
104 if (buf ==
nullptr) [[unlikely]] {
105 PyErr_Format(PyExc_IOError,
"Python file \"%s\" could not read buffer", filepath);
110 PyObject *compiled = Py_CompileStringObject(buf, filepath_py, Py_file_input, flags, -1);
112 Py_DECREF(filepath_py);
114 if (compiled ==
nullptr) {
119 py_result = PyEval_EvalCode(compiled, globals, locals);
138 if (filepath ==
nullptr && text ==
nullptr) {
142 PyGILState_STATE gilstate;
146 PyObject *py_dict =
nullptr, *py_result =
nullptr;
150 const char *filepath_namespace =
nullptr;
156 filepath_namespace = filepath_dummy;
158 if (text->compiled ==
nullptr) {
160 size_t buf_len_dummy;
162 text->compiled = Py_CompileStringObject(buf, filepath_dummy_py, Py_file_input,
nullptr, -1);
164 Py_DECREF(filepath_dummy_py);
167 if (text->compiled) {
169 py_result = PyEval_EvalCode(
static_cast<PyObject *
>(text->compiled), py_dict, py_dict);
174 filepath_namespace = filepath;
181 PyErr_Format(PyExc_IsADirectoryError,
"Python file \"%s\" is a directory", filepath);
187 const int closeit = 1;
190 fp, filepath, Py_file_input, py_dict, py_dict, closeit,
nullptr);
195 PyExc_IOError,
"Python file \"%s\" could not be opened: %s", filepath, strerror(errno));
224 Py_DECREF(py_result);
235 return (py_result !=
nullptr);
256 if (!expr || expr[0] ==
'\0') {
259 PyGILState_STATE gilstate = PyGILState_Ensure();
260 bool success =
false;
261 if (PyObject *retval = Py_CompileString(expr,
"<expression>", Py_eval_input)) {
268 PyGILState_Release(gilstate);
279 PyGILState_STATE gilstate;
285 if (context_set && (
C != C_prev)) {
296 const char *imports[],
303 if (expr[0] ==
'\0') {
308 PyObject *py_dict, *retval;
318 retval = PyRun_String(expr, mode, py_dict, py_dict);
321 if (retval ==
nullptr) {
362 if (py_object == Py_None) {
394 PyObject *
result = PyRun_String(script.
c_str(), Py_file_input, py_globals, py_locals);
395 const bool ok = (
result !=
nullptr);
399 on_exec_ok(py_locals);
404 Py_DECREF(py_locals);
450 std::optional<IDProperty *> result_idprop;
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());
459 if (py_ret == Py_None) {
461 result_idprop =
nullptr;
466 if (!result_idprop) {
477 return result_idprop;
493 if (err_info ==
nullptr) {
506 const char *err_str = PyUnicode_AsUTF8(py_err_str);
509 if (err_info->
reports !=
nullptr) {
523 fprintf(stderr,
"%s\n", err_str);
526 if (err_info->
r_string !=
nullptr) {
530 Py_XDECREF(py_err_str);
534 const char *imports[],
541 if (expr[0] ==
'\0') {
558 const char *imports[],
566 if (expr[0] ==
'\0') {
585 size_t value_dummy_len;
590 const char *imports[],
598 if (expr[0] ==
'\0') {
605 imports, expr,
"<expr as str or none>", r_value, r_value_len);
618 size_t value_dummy_len;
620 C, imports, expr, err_info, r_value, &value_dummy_len);
624 const char *imports[],
631 if (expr[0] ==
'\0') {
File and directory operations.
#define BLI_string_join_by_sep_char(...)
#define ID_BLEND_PATH(_bmain, _id)
Read Blender memory allocation and freeing.
constexpr const char * c_str() const
constexpr bool is_empty() const
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)
int BLI_findindex(const ListBase *listbase, const void *vlink) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
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)
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
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)
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
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()
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)
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)
void txt_move_to(struct Text *text, unsigned int line, unsigned int ch, bool sel)
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
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
const char * report_prefix
bool use_single_line_error