Changeset View
Changeset View
Standalone View
Standalone View
source/blender/python/generic/py_capi_utils.c
| Show First 20 Lines • Show All 898 Lines • ▼ Show 20 Lines | if (items->value & flag) { | ||||
| pystr = PyUnicode_FromString(items->identifier); | pystr = PyUnicode_FromString(items->identifier); | ||||
| PySet_Add(ret, pystr); | PySet_Add(ret, pystr); | ||||
| Py_DECREF(pystr); | Py_DECREF(pystr); | ||||
| } | } | ||||
| } | } | ||||
| return ret; | return ret; | ||||
| } | } | ||||
| /** | |||||
| * \return -1 on error, else 0 | |||||
| * | |||||
| * \note it is caller's responsibility to acquire & release GIL! | |||||
| */ | |||||
| int PyC_RunString_AsNumber(const char *expr, double *value, const char *filename) | |||||
| { | |||||
| PyObject *py_dict, *mod, *retval; | |||||
| int error_ret = 0; | |||||
| PyObject *main_mod = NULL; | |||||
| PyC_MainModule_Backup(&main_mod); | |||||
| py_dict = PyC_DefaultNameSpace(filename); | |||||
| mod = PyImport_ImportModule("math"); | |||||
| if (mod) { | |||||
| PyDict_Merge(py_dict, PyModule_GetDict(mod), 0); /* 0 - don't overwrite existing values */ | |||||
| Py_DECREF(mod); | |||||
| } | |||||
| else { /* highly unlikely but possibly */ | |||||
| PyErr_Print(); | |||||
| PyErr_Clear(); | |||||
| } | |||||
| retval = PyRun_String(expr, Py_eval_input, py_dict, py_dict); | |||||
| if (retval == NULL) { | |||||
| error_ret = -1; | |||||
| } | |||||
| else { | |||||
| double val; | |||||
| if (PyTuple_Check(retval)) { | |||||
| /* Users my have typed in 10km, 2m | |||||
| * add up all values */ | |||||
| int i; | |||||
| val = 0.0; | |||||
| for (i = 0; i < PyTuple_GET_SIZE(retval); i++) { | |||||
| const double val_item = PyFloat_AsDouble(PyTuple_GET_ITEM(retval, i)); | |||||
| if (val_item == -1 && PyErr_Occurred()) { | |||||
| val = -1; | |||||
| break; | |||||
| } | |||||
| val += val_item; | |||||
| } | |||||
| } | |||||
| else { | |||||
| val = PyFloat_AsDouble(retval); | |||||
| } | |||||
| Py_DECREF(retval); | |||||
| if (val == -1 && PyErr_Occurred()) { | |||||
| error_ret = -1; | |||||
| } | |||||
| else if (!finite(val)) { | |||||
| *value = 0.0; | |||||
| } | |||||
| else { | |||||
| *value = val; | |||||
| } | |||||
| } | |||||
| PyC_MainModule_Restore(main_mod); | |||||
| return error_ret; | |||||
| } | |||||