PyAPI: add optional imports to expression eval API
Avoids having to use `__import__` to access modules.
This commit is contained in:
@@ -2369,7 +2369,7 @@ static bool ui_set_but_string_eval_num_unit(bContext *C, uiBut *but, const char
|
||||
str_unit_convert, sizeof(str_unit_convert), but->drawstr,
|
||||
ui_get_but_scale_unit(but, 1.0), but->block->unit->system, RNA_SUBTYPE_UNIT_VALUE(unit_type));
|
||||
|
||||
return BPY_execute_string_as_number(C, str_unit_convert, true, r_value);
|
||||
return BPY_execute_string_as_number(C, NULL, str_unit_convert, true, r_value);
|
||||
}
|
||||
|
||||
#endif /* WITH_PYTHON */
|
||||
@@ -2384,7 +2384,7 @@ bool ui_but_string_set_eval_num(bContext *C, uiBut *but, const char *str, double
|
||||
if (str[0] != '\0') {
|
||||
bool is_unit_but = (ui_but_is_float(but) && ui_but_is_unit(but));
|
||||
/* only enable verbose if we won't run again with units */
|
||||
if (BPY_execute_string_as_number(C, str, is_unit_but == false, r_value)) {
|
||||
if (BPY_execute_string_as_number(C, NULL, str, is_unit_but == false, r_value)) {
|
||||
/* if the value parsed ok without unit conversion this button may still need a unit multiplier */
|
||||
if (is_unit_but) {
|
||||
char str_new[128];
|
||||
|
@@ -498,7 +498,7 @@ bool handleNumInput(bContext *C, NumInput *n, const wmEvent *event)
|
||||
n->unit_sys, n->unit_type[idx]);
|
||||
|
||||
/* Note: with angles, we always get values as radians here... */
|
||||
if (BPY_execute_string_as_number(C, str_unit_convert, false, &val)) {
|
||||
if (BPY_execute_string_as_number(C, NULL, str_unit_convert, false, &val)) {
|
||||
n->val[idx] = (float)val;
|
||||
n->val_flag[idx] &= ~NUM_INVALID;
|
||||
}
|
||||
|
@@ -75,9 +75,11 @@ void BPY_thread_restore(BPy_ThreadStatePtr tstate);
|
||||
|
||||
bool BPY_execute_filepath(struct bContext *C, const char *filepath, struct ReportList *reports);
|
||||
bool BPY_execute_text(struct bContext *C, struct Text *text, struct ReportList *reports, const bool do_jump);
|
||||
bool BPY_execute_string_as_number(struct bContext *C, const char *expr, const bool verbose, double *r_value);
|
||||
bool BPY_execute_string_as_intptr(struct bContext *C, const char *expr, const bool verbose, intptr_t *r_value);
|
||||
bool BPY_execute_string_as_string(struct bContext *C, const char *expr, const bool verbose, char **r_value);
|
||||
|
||||
bool BPY_execute_string_as_number(struct bContext *C, const char *imports[], const char *expr, const bool verbose, double *r_value);
|
||||
bool BPY_execute_string_as_intptr(struct bContext *C, const char *imports[], const char *expr, const bool verbose, intptr_t *r_value);
|
||||
bool BPY_execute_string_as_string(struct bContext *C, const char *imports[], const char *expr, const bool verbose, char **r_value);
|
||||
|
||||
bool BPY_execute_string_ex(struct bContext *C, const char *expr, bool use_eval);
|
||||
bool BPY_execute_string(struct bContext *C, const char *expr);
|
||||
|
||||
|
@@ -716,6 +716,26 @@ PyObject *PyC_DefaultNameSpace(const char *filename)
|
||||
return PyModule_GetDict(mod_main);
|
||||
}
|
||||
|
||||
bool PyC_NameSpace_ImportArray(PyObject *py_dict, const char *imports[])
|
||||
{
|
||||
for (int i = 0; imports[i]; i++) {
|
||||
PyObject *name = PyUnicode_FromString(imports[i]);
|
||||
PyObject *mod = PyImport_ImportModuleLevelObject(name, NULL, NULL, 0, 0);
|
||||
bool ok = false;
|
||||
if (mod) {
|
||||
PyDict_SetItem(py_dict, name, mod);
|
||||
ok = true;
|
||||
Py_DECREF(mod);
|
||||
}
|
||||
Py_DECREF(name);
|
||||
|
||||
if (!ok) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/* restore MUST be called after this */
|
||||
void PyC_MainModule_Backup(PyObject **main_mod)
|
||||
{
|
||||
@@ -1076,7 +1096,7 @@ PyObject *PyC_FlagSet_FromBitfield(PyC_FlagSet *items, int flag)
|
||||
*
|
||||
* \note it is caller's responsibility to acquire & release GIL!
|
||||
*/
|
||||
bool PyC_RunString_AsNumber(const char *expr, const char *filename, double *r_value)
|
||||
bool PyC_RunString_AsNumber(const char *imports[], const char *expr, const char *filename, double *r_value)
|
||||
{
|
||||
PyObject *py_dict, *mod, *retval;
|
||||
bool ok = true;
|
||||
@@ -1096,9 +1116,10 @@ bool PyC_RunString_AsNumber(const char *expr, const char *filename, double *r_va
|
||||
PyErr_Clear();
|
||||
}
|
||||
|
||||
retval = PyRun_String(expr, Py_eval_input, py_dict, py_dict);
|
||||
|
||||
if (retval == NULL) {
|
||||
if (imports && (!PyC_NameSpace_ImportArray(py_dict, imports))) {
|
||||
ok = false;
|
||||
}
|
||||
else if ((retval = PyRun_String(expr, Py_eval_input, py_dict, py_dict)) == NULL) {
|
||||
ok = false;
|
||||
}
|
||||
else {
|
||||
@@ -1140,7 +1161,7 @@ bool PyC_RunString_AsNumber(const char *expr, const char *filename, double *r_va
|
||||
return ok;
|
||||
}
|
||||
|
||||
bool PyC_RunString_AsIntPtr(const char *expr, const char *filename, intptr_t *r_value)
|
||||
bool PyC_RunString_AsIntPtr(const char *imports[], const char *expr, const char *filename, intptr_t *r_value)
|
||||
{
|
||||
PyObject *py_dict, *retval;
|
||||
bool ok = true;
|
||||
@@ -1150,9 +1171,10 @@ bool PyC_RunString_AsIntPtr(const char *expr, const char *filename, intptr_t *r_
|
||||
|
||||
py_dict = PyC_DefaultNameSpace(filename);
|
||||
|
||||
retval = PyRun_String(expr, Py_eval_input, py_dict, py_dict);
|
||||
|
||||
if (retval == NULL) {
|
||||
if (imports && (!PyC_NameSpace_ImportArray(py_dict, imports))) {
|
||||
ok = false;
|
||||
}
|
||||
else if ((retval = PyRun_String(expr, Py_eval_input, py_dict, py_dict)) == NULL) {
|
||||
ok = false;
|
||||
}
|
||||
else {
|
||||
@@ -1174,7 +1196,7 @@ bool PyC_RunString_AsIntPtr(const char *expr, const char *filename, intptr_t *r_
|
||||
return ok;
|
||||
}
|
||||
|
||||
bool PyC_RunString_AsString(const char *expr, const char *filename, char **r_value)
|
||||
bool PyC_RunString_AsString(const char *imports[], const char *expr, const char *filename, char **r_value)
|
||||
{
|
||||
PyObject *py_dict, *retval;
|
||||
bool ok = true;
|
||||
@@ -1184,9 +1206,10 @@ bool PyC_RunString_AsString(const char *expr, const char *filename, char **r_val
|
||||
|
||||
py_dict = PyC_DefaultNameSpace(filename);
|
||||
|
||||
retval = PyRun_String(expr, Py_eval_input, py_dict, py_dict);
|
||||
|
||||
if (retval == NULL) {
|
||||
if (imports && (!PyC_NameSpace_ImportArray(py_dict, imports))) {
|
||||
ok = false;
|
||||
}
|
||||
else if ((retval = PyRun_String(expr, Py_eval_input, py_dict, py_dict)) == NULL) {
|
||||
ok = false;
|
||||
}
|
||||
else {
|
||||
|
@@ -77,8 +77,9 @@ const char * PyC_UnicodeAsByte(PyObject *py_str, PyObject **coerce); /* coerc
|
||||
const char * PyC_UnicodeAsByteAndSize(PyObject *py_str, Py_ssize_t *size, PyObject **coerce);
|
||||
|
||||
/* name namespace function for bpy & bge */
|
||||
PyObject * PyC_DefaultNameSpace(const char *filename);
|
||||
void PyC_RunQuicky(const char *filepath, int n, ...);
|
||||
PyObject *PyC_DefaultNameSpace(const char *filename);
|
||||
void PyC_RunQuicky(const char *filepath, int n, ...);
|
||||
bool PyC_NameSpace_ImportArray(PyObject *py_dict, const char *imports[]);
|
||||
|
||||
void PyC_MainModule_Backup(PyObject **main_mod);
|
||||
void PyC_MainModule_Restore(PyObject *main_mod);
|
||||
@@ -101,9 +102,9 @@ int PyC_FlagSet_ValueFromID(PyC_FlagSet *item, const char *identifier, int
|
||||
int PyC_FlagSet_ToBitfield(PyC_FlagSet *items, PyObject *value, int *r_value, const char *error_prefix);
|
||||
PyObject *PyC_FlagSet_FromBitfield(PyC_FlagSet *items, int flag);
|
||||
|
||||
bool PyC_RunString_AsNumber(const char *expr, const char *filename, double *r_value);
|
||||
bool PyC_RunString_AsIntPtr(const char *expr, const char *filename, intptr_t *r_value);
|
||||
bool PyC_RunString_AsString(const char *expr, const char *filename, char **r_value);
|
||||
bool PyC_RunString_AsNumber(const char **imports, const char *expr, const char *filename, double *r_value);
|
||||
bool PyC_RunString_AsIntPtr(const char **imports, const char *expr, const char *filename, intptr_t *r_value);
|
||||
bool PyC_RunString_AsString(const char **imports, const char *expr, const char *filename, char **r_value);
|
||||
|
||||
int PyC_ParseBool(PyObject *o, void *p);
|
||||
|
||||
|
@@ -588,7 +588,9 @@ void BPY_DECREF_RNA_INVALIDATE(void *pyob_ptr)
|
||||
/**
|
||||
* \return success
|
||||
*/
|
||||
bool BPY_execute_string_as_number(bContext *C, const char *expr, const bool verbose, double *r_value)
|
||||
bool BPY_execute_string_as_number(
|
||||
bContext *C, const char *imports[],
|
||||
const char *expr, const bool verbose, double *r_value)
|
||||
{
|
||||
PyGILState_STATE gilstate;
|
||||
bool ok = true;
|
||||
@@ -604,7 +606,7 @@ bool BPY_execute_string_as_number(bContext *C, const char *expr, const bool verb
|
||||
|
||||
bpy_context_set(C, &gilstate);
|
||||
|
||||
ok = PyC_RunString_AsNumber(expr, "<blender button>", r_value);
|
||||
ok = PyC_RunString_AsNumber(imports, expr, "<expr as number>", r_value);
|
||||
|
||||
if (ok == false) {
|
||||
if (verbose) {
|
||||
@@ -623,7 +625,9 @@ bool BPY_execute_string_as_number(bContext *C, const char *expr, const bool verb
|
||||
/**
|
||||
* \return success
|
||||
*/
|
||||
bool BPY_execute_string_as_string(bContext *C, const char *expr, const bool verbose, char **r_value)
|
||||
bool BPY_execute_string_as_string(
|
||||
bContext *C, const char *imports[],
|
||||
const char *expr, const bool verbose, char **r_value)
|
||||
{
|
||||
BLI_assert(r_value && expr);
|
||||
PyGILState_STATE gilstate;
|
||||
@@ -636,7 +640,7 @@ bool BPY_execute_string_as_string(bContext *C, const char *expr, const bool verb
|
||||
|
||||
bpy_context_set(C, &gilstate);
|
||||
|
||||
ok = PyC_RunString_AsString(expr, "<blender button>", r_value);
|
||||
ok = PyC_RunString_AsString(imports, expr, "<expr as str>", r_value);
|
||||
|
||||
if (ok == false) {
|
||||
if (verbose) {
|
||||
@@ -657,7 +661,9 @@ bool BPY_execute_string_as_string(bContext *C, const char *expr, const bool verb
|
||||
*
|
||||
* \return success
|
||||
*/
|
||||
bool BPY_execute_string_as_intptr(bContext *C, const char *expr, const bool verbose, intptr_t *r_value)
|
||||
bool BPY_execute_string_as_intptr(
|
||||
bContext *C, const char *imports[],
|
||||
const char *expr, const bool verbose, intptr_t *r_value)
|
||||
{
|
||||
BLI_assert(r_value && expr);
|
||||
PyGILState_STATE gilstate;
|
||||
@@ -670,7 +676,7 @@ bool BPY_execute_string_as_intptr(bContext *C, const char *expr, const bool verb
|
||||
|
||||
bpy_context_set(C, &gilstate);
|
||||
|
||||
ok = PyC_RunString_AsIntPtr(expr, "<blender button>", r_value);
|
||||
ok = PyC_RunString_AsIntPtr(imports, expr, "<expr as intptr>", r_value);
|
||||
|
||||
if (ok == false) {
|
||||
if (verbose) {
|
||||
|
@@ -204,7 +204,7 @@ static PyObject *bpyunits_to_value(PyObject *UNUSED(self), PyObject *args, PyObj
|
||||
|
||||
bUnit_ReplaceString(str, (int)str_len, uref, scale, usys, ucat);
|
||||
|
||||
if (!PyC_RunString_AsNumber(str, "<bpy_units_api>", &result)) {
|
||||
if (!PyC_RunString_AsNumber(NULL, str, "<bpy_units_api>", &result)) {
|
||||
if (PyErr_Occurred()) {
|
||||
PyErr_Print();
|
||||
PyErr_Clear();
|
||||
|
Reference in New Issue
Block a user