2023-08-16 00:20:26 +10:00
|
|
|
/* SPDX-FileCopyrightText: 2023 Blender Authors
|
2023-05-31 16:19:06 +02:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later */
|
2020-08-17 17:46:06 +10:00
|
|
|
|
|
|
|
|
/** \file
|
|
|
|
|
* \ingroup pythonintern
|
|
|
|
|
*/
|
|
|
|
|
|
2023-07-21 16:05:33 +10:00
|
|
|
#include <cstdio>
|
2020-08-17 17:46:06 +10:00
|
|
|
|
|
|
|
|
#include <Python.h>
|
|
|
|
|
|
|
|
|
|
#include "MEM_guardedalloc.h"
|
|
|
|
|
|
|
|
|
|
#include "BLI_fileops.h"
|
|
|
|
|
#include "BLI_listbase.h"
|
2024-09-26 21:13:39 +10:00
|
|
|
#include "BLI_path_utils.hh"
|
2020-08-17 17:46:06 +10:00
|
|
|
#include "BLI_string.h"
|
2025-07-27 02:47:45 +00:00
|
|
|
#include "BLI_string_utils.hh"
|
2020-08-17 17:46:06 +10:00
|
|
|
|
2023-11-16 11:41:55 +01:00
|
|
|
#include "BKE_context.hh"
|
2025-02-07 17:47:16 +01:00
|
|
|
#include "BKE_library.hh"
|
2023-12-01 19:43:16 +01:00
|
|
|
#include "BKE_main.hh"
|
2024-02-10 18:34:29 +01:00
|
|
|
#include "BKE_report.hh"
|
2020-08-17 17:46:06 +10:00
|
|
|
#include "BKE_text.h"
|
|
|
|
|
|
|
|
|
|
#include "DNA_text_types.h"
|
|
|
|
|
|
2024-09-24 17:07:49 +02:00
|
|
|
#include "BPY_extern_run.hh"
|
2020-08-17 17:46:06 +10:00
|
|
|
|
2024-09-24 15:25:36 +02:00
|
|
|
#include "bpy_capi_utils.hh"
|
2024-09-24 12:23:25 +02:00
|
|
|
#include "bpy_traceback.hh"
|
2020-08-17 17:46:06 +10:00
|
|
|
|
2024-09-24 15:25:36 +02:00
|
|
|
#include "../generic/py_capi_utils.hh"
|
2020-08-17 17:46:06 +10:00
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Private Utilities
|
|
|
|
|
* \{ */
|
|
|
|
|
|
2022-03-28 16:37:09 +11:00
|
|
|
static void python_script_error_jump_text(Text *text, const char *filepath)
|
2020-08-17 17:46:06 +10:00
|
|
|
{
|
2022-03-28 17:04:19 +11:00
|
|
|
int lineno, lineno_end;
|
|
|
|
|
int offset, offset_end;
|
2022-03-28 17:11:34 +11:00
|
|
|
if (python_script_error_jump(filepath, &lineno, &offset, &lineno_end, &offset_end)) {
|
2022-03-28 17:04:19 +11:00
|
|
|
/* Start at the end so cursor motion that looses the selection,
|
|
|
|
|
* leaves the cursor from the most useful place.
|
|
|
|
|
* Also, the end can't always be set, so don't give it priority. */
|
2022-04-06 13:14:21 +10:00
|
|
|
txt_move_to(text, lineno_end - 1, offset_end - 1, false);
|
|
|
|
|
txt_move_to(text, lineno - 1, offset - 1, true);
|
2020-08-17 17:46:06 +10:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-28 16:54:31 +11:00
|
|
|
/**
|
|
|
|
|
* Generate a `filepath` from a text-block so we can tell what file a text block comes from.
|
|
|
|
|
*/
|
|
|
|
|
static void bpy_text_filepath_get(char *filepath,
|
2023-05-07 15:22:58 +10:00
|
|
|
const size_t filepath_maxncpy,
|
2022-03-28 16:54:31 +11:00
|
|
|
const Main *bmain,
|
|
|
|
|
const Text *text)
|
2020-08-17 17:46:06 +10:00
|
|
|
{
|
2025-07-27 02:47:45 +00:00
|
|
|
BLI_string_join_by_sep_char(
|
|
|
|
|
filepath, filepath_maxncpy, SEP, ID_BLEND_PATH(bmain, &text->id), text->id.name + 2);
|
2020-08-17 17:46:06 +10:00
|
|
|
}
|
|
|
|
|
|
2023-11-28 17:45:25 +11:00
|
|
|
/**
|
|
|
|
|
* Compatibility wrapper for #PyRun_FileExFlags.
|
|
|
|
|
*/
|
|
|
|
|
static PyObject *python_compat_wrapper_PyRun_FileExFlags(FILE *fp,
|
|
|
|
|
const char *filepath,
|
|
|
|
|
const int start,
|
|
|
|
|
PyObject *globals,
|
|
|
|
|
PyObject *locals,
|
|
|
|
|
const int closeit,
|
|
|
|
|
PyCompilerFlags *flags)
|
|
|
|
|
{
|
|
|
|
|
/* Previously we used #PyRun_File to run directly the code on a FILE
|
|
|
|
|
* object, but as written in the Python/C API Ref Manual, chapter 2,
|
|
|
|
|
* 'FILE structs for different C libraries can be different and incompatible'.
|
|
|
|
|
* So now we load the script file data to a buffer on MS-Windows. */
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
bool use_file_handle_workaround = true;
|
|
|
|
|
#else
|
|
|
|
|
bool use_file_handle_workaround = false;
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
if (!use_file_handle_workaround) {
|
|
|
|
|
return PyRun_FileExFlags(fp, filepath, start, globals, locals, closeit, flags);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PyObject *py_result = nullptr;
|
|
|
|
|
size_t buf_len;
|
|
|
|
|
char *buf = static_cast<char *>(BLI_file_read_data_as_mem_from_handle(fp, false, 1, &buf_len));
|
|
|
|
|
if (closeit) {
|
|
|
|
|
fclose(fp);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (UNLIKELY(buf == nullptr)) {
|
|
|
|
|
PyErr_Format(PyExc_IOError, "Python file \"%s\" could not read buffer", filepath);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
buf[buf_len] = '\0';
|
|
|
|
|
PyObject *filepath_py = PyC_UnicodeFromBytes(filepath);
|
|
|
|
|
PyObject *compiled = Py_CompileStringObject(buf, filepath_py, Py_file_input, flags, -1);
|
|
|
|
|
MEM_freeN(buf);
|
|
|
|
|
Py_DECREF(filepath_py);
|
|
|
|
|
|
|
|
|
|
if (compiled == nullptr) {
|
2023-11-28 22:15:10 +11:00
|
|
|
/* Based on Python's internal usage, an error must always be set. */
|
|
|
|
|
BLI_assert(PyErr_Occurred());
|
2023-11-28 17:45:25 +11:00
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
py_result = PyEval_EvalCode(compiled, globals, locals);
|
|
|
|
|
Py_DECREF(compiled);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return py_result;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-13 13:09:14 +11:00
|
|
|
/**
|
|
|
|
|
* Execute a file-path or text-block.
|
|
|
|
|
*
|
2023-07-21 02:18:59 +02:00
|
|
|
* \param reports: Report exceptions as errors (may be nullptr).
|
2021-12-13 13:09:14 +11:00
|
|
|
* \param do_jump: See #BPY_run_text.
|
|
|
|
|
*
|
|
|
|
|
* \note Share a function for this since setup/cleanup logic is the same.
|
|
|
|
|
*/
|
2023-06-03 08:36:28 +10:00
|
|
|
static bool python_script_exec(
|
|
|
|
|
bContext *C, const char *filepath, Text *text, ReportList *reports, const bool do_jump)
|
2020-08-17 17:46:06 +10:00
|
|
|
{
|
2022-03-28 16:54:31 +11:00
|
|
|
BLI_assert(filepath || text);
|
2023-07-21 02:18:59 +02:00
|
|
|
if (filepath == nullptr && text == nullptr) {
|
2023-07-22 11:36:59 +10:00
|
|
|
return false;
|
2020-08-17 17:46:06 +10:00
|
|
|
}
|
|
|
|
|
|
2025-02-13 17:56:42 +11:00
|
|
|
PyGILState_STATE gilstate;
|
2020-08-17 17:46:06 +10:00
|
|
|
bpy_context_set(C, &gilstate);
|
|
|
|
|
|
2025-02-13 17:56:42 +11:00
|
|
|
Main *bmain_old = CTX_data_main(C);
|
|
|
|
|
PyObject *py_dict = nullptr, *py_result = nullptr;
|
|
|
|
|
|
|
|
|
|
char filepath_dummy[FILE_MAX];
|
|
|
|
|
/** The `__file__` added into the name-space. */
|
|
|
|
|
const char *filepath_namespace = nullptr;
|
|
|
|
|
|
2024-05-04 16:42:08 +10:00
|
|
|
PyObject *main_mod = PyC_MainModule_Backup();
|
2020-08-17 17:46:06 +10:00
|
|
|
|
|
|
|
|
if (text) {
|
2022-03-28 16:54:31 +11:00
|
|
|
bpy_text_filepath_get(filepath_dummy, sizeof(filepath_dummy), bmain_old, text);
|
|
|
|
|
filepath_namespace = filepath_dummy;
|
2020-08-17 17:46:06 +10:00
|
|
|
|
2023-07-21 02:18:59 +02:00
|
|
|
if (text->compiled == nullptr) { /* if it wasn't already compiled, do it now */
|
2023-11-28 22:15:10 +11:00
|
|
|
PyObject *filepath_dummy_py = PyC_UnicodeFromBytes(filepath_dummy);
|
2022-03-14 14:25:33 +11:00
|
|
|
size_t buf_len_dummy;
|
2023-11-28 22:15:10 +11:00
|
|
|
char *buf = txt_to_buf(text, &buf_len_dummy);
|
2023-07-21 02:18:59 +02:00
|
|
|
text->compiled = Py_CompileStringObject(buf, filepath_dummy_py, Py_file_input, nullptr, -1);
|
2020-08-17 17:46:06 +10:00
|
|
|
MEM_freeN(buf);
|
2022-03-28 16:54:31 +11:00
|
|
|
Py_DECREF(filepath_dummy_py);
|
2020-08-17 17:46:06 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (text->compiled) {
|
2022-03-28 16:54:31 +11:00
|
|
|
py_dict = PyC_DefaultNameSpace(filepath_dummy);
|
2023-07-21 02:18:59 +02:00
|
|
|
py_result = PyEval_EvalCode(static_cast<PyObject *>(text->compiled), py_dict, py_dict);
|
2020-08-17 17:46:06 +10:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
2023-11-28 16:59:52 +11:00
|
|
|
FILE *fp = BLI_fopen(filepath, "rb");
|
2022-03-28 16:54:31 +11:00
|
|
|
filepath_namespace = filepath;
|
2020-08-17 17:46:06 +10:00
|
|
|
|
|
|
|
|
if (fp) {
|
2023-11-28 22:15:10 +11:00
|
|
|
/* Matches behavior of running Python with a directory argument.
|
|
|
|
|
* Without the `fstat`, the directory will execute & return None. */
|
2023-11-28 17:40:07 +11:00
|
|
|
BLI_stat_t st;
|
|
|
|
|
if (BLI_fstat(fileno(fp), &st) == 0 && S_ISDIR(st.st_mode)) {
|
|
|
|
|
PyErr_Format(PyExc_IsADirectoryError, "Python file \"%s\" is a directory", filepath);
|
2023-11-28 22:15:10 +11:00
|
|
|
BLI_assert(py_result == nullptr);
|
|
|
|
|
fclose(fp);
|
2020-08-17 17:46:06 +10:00
|
|
|
}
|
2023-11-28 17:40:07 +11:00
|
|
|
else {
|
2023-11-28 22:15:10 +11:00
|
|
|
/* Calls `fclose(fp)`, run the script with one fewer open files. */
|
|
|
|
|
const int closeit = 1;
|
2023-11-28 17:45:25 +11:00
|
|
|
py_dict = PyC_DefaultNameSpace(filepath);
|
|
|
|
|
py_result = python_compat_wrapper_PyRun_FileExFlags(
|
2023-11-28 22:15:10 +11:00
|
|
|
fp, filepath, Py_file_input, py_dict, py_dict, closeit, nullptr);
|
2023-11-28 17:40:07 +11:00
|
|
|
}
|
2020-08-17 17:46:06 +10:00
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
PyErr_Format(
|
2022-03-28 16:54:31 +11:00
|
|
|
PyExc_IOError, "Python file \"%s\" could not be opened: %s", filepath, strerror(errno));
|
2023-11-28 22:15:10 +11:00
|
|
|
BLI_assert(py_result == nullptr);
|
2020-08-17 17:46:06 +10:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!py_result) {
|
2023-11-28 11:26:28 +11:00
|
|
|
if (reports) {
|
|
|
|
|
BPy_errors_to_report(reports);
|
|
|
|
|
}
|
2020-08-17 17:46:06 +10:00
|
|
|
if (text) {
|
|
|
|
|
if (do_jump) {
|
2022-01-23 22:34:56 -06:00
|
|
|
/* ensure text is valid before use, the script may have freed itself */
|
2020-08-17 17:46:06 +10:00
|
|
|
Main *bmain_new = CTX_data_main(C);
|
|
|
|
|
if ((bmain_old == bmain_new) && (BLI_findindex(&bmain_new->texts, text) != -1)) {
|
2022-03-28 16:54:31 +11:00
|
|
|
python_script_error_jump_text(text, filepath_namespace);
|
2020-08-17 17:46:06 +10:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-07-04 10:20:56 +10:00
|
|
|
if (reports) {
|
|
|
|
|
PyErr_Clear();
|
|
|
|
|
}
|
|
|
|
|
else {
|
2024-02-04 15:16:47 +11:00
|
|
|
PyErr_Print();
|
|
|
|
|
}
|
2020-08-17 17:46:06 +10:00
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
Py_DECREF(py_result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PyC_MainModule_Restore(main_mod);
|
|
|
|
|
|
2025-04-02 03:02:01 +00:00
|
|
|
/* Flush `stdout` & `stderr` to ensure the script output is visible.
|
|
|
|
|
* Using `fflush(stdout)` does not solve it. */
|
2025-04-01 12:41:38 +02:00
|
|
|
PyC_StdFilesFlush();
|
|
|
|
|
|
2020-08-17 17:46:06 +10:00
|
|
|
bpy_context_clear(C, &gilstate);
|
|
|
|
|
|
2023-07-21 02:18:59 +02:00
|
|
|
return (py_result != nullptr);
|
2020-08-17 17:46:06 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** \} */
|
|
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Run Text / Filename / String
|
|
|
|
|
* \{ */
|
|
|
|
|
|
2023-06-03 08:36:28 +10:00
|
|
|
bool BPY_run_filepath(bContext *C, const char *filepath, ReportList *reports)
|
2020-08-17 17:46:06 +10:00
|
|
|
{
|
2023-07-21 02:18:59 +02:00
|
|
|
return python_script_exec(C, filepath, nullptr, reports, false);
|
2020-08-17 17:46:06 +10:00
|
|
|
}
|
|
|
|
|
|
2023-06-03 08:36:28 +10:00
|
|
|
bool BPY_run_text(bContext *C, Text *text, ReportList *reports, const bool do_jump)
|
2020-08-17 17:46:06 +10:00
|
|
|
{
|
2023-07-21 02:18:59 +02:00
|
|
|
return python_script_exec(C, nullptr, text, reports, do_jump);
|
2020-08-17 17:46:06 +10:00
|
|
|
}
|
|
|
|
|
|
2020-08-17 18:16:54 +10:00
|
|
|
/**
|
|
|
|
|
* \param mode: Passed to #PyRun_String, matches Python's `compile` functions mode argument.
|
|
|
|
|
* #Py_eval_input for `eval`, #Py_file_input for `exec`.
|
|
|
|
|
*/
|
|
|
|
|
static bool bpy_run_string_impl(bContext *C,
|
|
|
|
|
const char *imports[],
|
|
|
|
|
const char *expr,
|
|
|
|
|
const int mode)
|
2020-08-17 17:46:06 +10:00
|
|
|
{
|
|
|
|
|
BLI_assert(expr);
|
|
|
|
|
PyGILState_STATE gilstate;
|
|
|
|
|
PyObject *py_dict, *retval;
|
|
|
|
|
bool ok = true;
|
|
|
|
|
|
|
|
|
|
if (expr[0] == '\0') {
|
|
|
|
|
return ok;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bpy_context_set(C, &gilstate);
|
|
|
|
|
|
2024-05-04 16:42:08 +10:00
|
|
|
PyObject *main_mod = PyC_MainModule_Backup();
|
2020-08-17 17:46:06 +10:00
|
|
|
|
|
|
|
|
py_dict = PyC_DefaultNameSpace("<blender string>");
|
|
|
|
|
|
2022-10-07 22:52:53 +11:00
|
|
|
if (imports && !PyC_NameSpace_ImportArray(py_dict, imports)) {
|
2020-08-17 17:46:06 +10:00
|
|
|
Py_DECREF(py_dict);
|
2023-07-21 02:18:59 +02:00
|
|
|
retval = nullptr;
|
2020-08-17 17:46:06 +10:00
|
|
|
}
|
|
|
|
|
else {
|
2020-08-17 18:16:54 +10:00
|
|
|
retval = PyRun_String(expr, mode, py_dict, py_dict);
|
2020-08-17 17:46:06 +10:00
|
|
|
}
|
|
|
|
|
|
2023-07-21 02:18:59 +02:00
|
|
|
if (retval == nullptr) {
|
2020-08-17 17:46:06 +10:00
|
|
|
ok = false;
|
2024-04-05 12:32:17 +11:00
|
|
|
if (ReportList *wm_reports = C ? CTX_wm_reports(C) : nullptr) {
|
2023-11-28 22:15:10 +11:00
|
|
|
BPy_errors_to_report(wm_reports);
|
|
|
|
|
}
|
2023-11-28 11:26:28 +11:00
|
|
|
PyErr_Print();
|
2020-08-17 17:46:06 +10:00
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
Py_DECREF(retval);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PyC_MainModule_Restore(main_mod);
|
|
|
|
|
|
|
|
|
|
bpy_context_clear(C, &gilstate);
|
|
|
|
|
|
|
|
|
|
return ok;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-17 18:16:54 +10:00
|
|
|
bool BPY_run_string_eval(bContext *C, const char *imports[], const char *expr)
|
|
|
|
|
{
|
|
|
|
|
return bpy_run_string_impl(C, imports, expr, Py_eval_input);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool BPY_run_string_exec(bContext *C, const char *imports[], const char *expr)
|
2020-08-17 17:46:06 +10:00
|
|
|
{
|
2020-08-17 18:16:54 +10:00
|
|
|
return bpy_run_string_impl(C, imports, expr, Py_file_input);
|
2020-08-17 17:46:06 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** \} */
|
|
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Run Python & Evaluate Utilities
|
|
|
|
|
*
|
|
|
|
|
* Return values as plain C types, useful to run Python scripts
|
|
|
|
|
* in code that doesn't deal with Python data-types.
|
|
|
|
|
* \{ */
|
|
|
|
|
|
2023-07-21 10:59:54 +10:00
|
|
|
static void run_string_handle_error(BPy_RunErrInfo *err_info)
|
2021-04-12 21:39:56 +10:00
|
|
|
{
|
2023-09-18 22:09:19 +10:00
|
|
|
BLI_assert(PyErr_Occurred());
|
|
|
|
|
|
2023-07-21 02:18:59 +02:00
|
|
|
if (err_info == nullptr) {
|
2021-04-12 21:39:56 +10:00
|
|
|
PyErr_Print();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Signal to do nothing. */
|
|
|
|
|
if (!(err_info->reports || err_info->r_string)) {
|
|
|
|
|
PyErr_Clear();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PyObject *py_err_str = err_info->use_single_line_error ? PyC_ExceptionBuffer_Simple() :
|
|
|
|
|
PyC_ExceptionBuffer();
|
2023-09-18 22:09:19 +10:00
|
|
|
const char *err_str = PyUnicode_AsUTF8(py_err_str);
|
2022-05-03 17:54:37 +10:00
|
|
|
PyErr_Clear();
|
2021-04-12 21:39:56 +10:00
|
|
|
|
2023-07-21 02:18:59 +02:00
|
|
|
if (err_info->reports != nullptr) {
|
2021-04-12 21:39:56 +10:00
|
|
|
if (err_info->report_prefix) {
|
|
|
|
|
BKE_reportf(err_info->reports, RPT_ERROR, "%s: %s", err_info->report_prefix, err_str);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
BKE_report(err_info->reports, RPT_ERROR, err_str);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-06 16:15:11 +10:00
|
|
|
/* Print the reports if they were not printed already. */
|
2023-07-21 02:18:59 +02:00
|
|
|
if ((err_info->reports == nullptr) || !BKE_reports_print_test(err_info->reports, RPT_ERROR)) {
|
2022-04-06 16:15:11 +10:00
|
|
|
if (err_info->report_prefix) {
|
|
|
|
|
fprintf(stderr, "%s: ", err_info->report_prefix);
|
|
|
|
|
}
|
|
|
|
|
fprintf(stderr, "%s\n", err_str);
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-21 02:18:59 +02:00
|
|
|
if (err_info->r_string != nullptr) {
|
2021-04-12 21:39:56 +10:00
|
|
|
*err_info->r_string = BLI_strdup(err_str);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Py_XDECREF(py_err_str);
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-17 17:46:06 +10:00
|
|
|
bool BPY_run_string_as_number(bContext *C,
|
|
|
|
|
const char *imports[],
|
|
|
|
|
const char *expr,
|
2023-07-21 10:59:54 +10:00
|
|
|
BPy_RunErrInfo *err_info,
|
2020-08-17 17:46:06 +10:00
|
|
|
double *r_value)
|
|
|
|
|
{
|
|
|
|
|
bool ok = true;
|
|
|
|
|
|
|
|
|
|
if (expr[0] == '\0') {
|
|
|
|
|
*r_value = 0.0;
|
|
|
|
|
return ok;
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-13 17:56:42 +11:00
|
|
|
PyGILState_STATE gilstate;
|
2020-08-17 17:46:06 +10:00
|
|
|
bpy_context_set(C, &gilstate);
|
|
|
|
|
|
|
|
|
|
ok = PyC_RunString_AsNumber(imports, expr, "<expr as number>", r_value);
|
|
|
|
|
|
|
|
|
|
if (ok == false) {
|
2021-04-12 21:39:56 +10:00
|
|
|
run_string_handle_error(err_info);
|
2020-08-17 17:46:06 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bpy_context_clear(C, &gilstate);
|
|
|
|
|
|
|
|
|
|
return ok;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-19 10:21:59 +10:00
|
|
|
bool BPY_run_string_as_string_and_len(bContext *C,
|
|
|
|
|
const char *imports[],
|
|
|
|
|
const char *expr,
|
2023-07-21 10:59:54 +10:00
|
|
|
BPy_RunErrInfo *err_info,
|
2023-06-19 10:21:59 +10:00
|
|
|
char **r_value,
|
|
|
|
|
size_t *r_value_len)
|
2020-08-17 17:46:06 +10:00
|
|
|
{
|
|
|
|
|
bool ok = true;
|
|
|
|
|
|
|
|
|
|
if (expr[0] == '\0') {
|
2023-07-21 02:18:59 +02:00
|
|
|
*r_value = nullptr;
|
2020-08-17 17:46:06 +10:00
|
|
|
return ok;
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-13 17:56:42 +11:00
|
|
|
PyGILState_STATE gilstate;
|
2020-08-17 17:46:06 +10:00
|
|
|
bpy_context_set(C, &gilstate);
|
|
|
|
|
|
2023-06-19 10:21:59 +10:00
|
|
|
ok = PyC_RunString_AsStringAndSize(imports, expr, "<expr as str>", r_value, r_value_len);
|
2020-08-17 17:46:06 +10:00
|
|
|
|
|
|
|
|
if (ok == false) {
|
2021-04-12 21:39:56 +10:00
|
|
|
run_string_handle_error(err_info);
|
2020-08-17 17:46:06 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bpy_context_clear(C, &gilstate);
|
|
|
|
|
|
|
|
|
|
return ok;
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-21 10:59:54 +10:00
|
|
|
bool BPY_run_string_as_string(
|
|
|
|
|
bContext *C, const char *imports[], const char *expr, BPy_RunErrInfo *err_info, char **r_value)
|
2020-08-17 17:46:06 +10:00
|
|
|
{
|
2023-06-19 10:21:59 +10:00
|
|
|
size_t value_dummy_len;
|
|
|
|
|
return BPY_run_string_as_string_and_len(C, imports, expr, err_info, r_value, &value_dummy_len);
|
2020-08-17 17:46:06 +10:00
|
|
|
}
|
|
|
|
|
|
2024-04-18 10:35:51 +10:00
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
bool ok = true;
|
|
|
|
|
|
|
|
|
|
if (expr[0] == '\0') {
|
|
|
|
|
*r_value = nullptr;
|
|
|
|
|
return ok;
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-13 17:56:42 +11:00
|
|
|
PyGILState_STATE gilstate;
|
2024-04-18 10:35:51 +10:00
|
|
|
bpy_context_set(C, &gilstate);
|
|
|
|
|
|
|
|
|
|
ok = PyC_RunString_AsStringAndSizeOrNone(
|
|
|
|
|
imports, expr, "<expr as str or none>", r_value, r_value_len);
|
|
|
|
|
|
|
|
|
|
if (ok == false) {
|
|
|
|
|
run_string_handle_error(err_info);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bpy_context_clear(C, &gilstate);
|
|
|
|
|
|
|
|
|
|
return ok;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool BPY_run_string_as_string_or_none(
|
|
|
|
|
bContext *C, const char *imports[], const char *expr, BPy_RunErrInfo *err_info, char **r_value)
|
|
|
|
|
{
|
|
|
|
|
size_t value_dummy_len;
|
|
|
|
|
return BPY_run_string_as_string_and_len_or_none(
|
|
|
|
|
C, imports, expr, err_info, r_value, &value_dummy_len);
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-17 17:46:06 +10:00
|
|
|
bool BPY_run_string_as_intptr(bContext *C,
|
|
|
|
|
const char *imports[],
|
|
|
|
|
const char *expr,
|
2023-07-21 10:59:54 +10:00
|
|
|
BPy_RunErrInfo *err_info,
|
2020-08-17 17:46:06 +10:00
|
|
|
intptr_t *r_value)
|
|
|
|
|
{
|
|
|
|
|
bool ok = true;
|
|
|
|
|
|
|
|
|
|
if (expr[0] == '\0') {
|
|
|
|
|
*r_value = 0;
|
|
|
|
|
return ok;
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-13 17:56:42 +11:00
|
|
|
PyGILState_STATE gilstate;
|
2020-08-17 17:46:06 +10:00
|
|
|
bpy_context_set(C, &gilstate);
|
|
|
|
|
|
|
|
|
|
ok = PyC_RunString_AsIntPtr(imports, expr, "<expr as intptr>", r_value);
|
|
|
|
|
|
|
|
|
|
if (ok == false) {
|
2021-04-12 21:39:56 +10:00
|
|
|
run_string_handle_error(err_info);
|
2020-08-17 17:46:06 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bpy_context_clear(C, &gilstate);
|
|
|
|
|
|
|
|
|
|
return ok;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** \} */
|