diff options
author | Campbell Barton <ideasman42@gmail.com> | 2017-11-29 05:41:10 +0300 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2017-11-29 05:41:10 +0300 |
commit | 0b325ba201269b3c8aab6e61c70f15c1758737d8 (patch) | |
tree | 001886d8a4335f333d04c78297a6efa2d7254841 | |
parent | 0c7fbc435f4611d6f25a58b17f7f65318bef572d (diff) |
PyAPI: PyC_Err_PrintWithFunc utility function
Move function error printing utility into py_capi_utils.
-rw-r--r-- | source/blender/python/generic/py_capi_utils.c | 19 | ||||
-rw-r--r-- | source/blender/python/generic/py_capi_utils.h | 2 | ||||
-rw-r--r-- | source/blender/python/intern/bpy_props.c | 94 |
3 files changed, 60 insertions, 55 deletions
diff --git a/source/blender/python/generic/py_capi_utils.c b/source/blender/python/generic/py_capi_utils.c index d49f9514b8c..36609c6f29b 100644 --- a/source/blender/python/generic/py_capi_utils.c +++ b/source/blender/python/generic/py_capi_utils.c @@ -412,6 +412,25 @@ PyObject *PyC_Err_Format_Prefix(PyObject *exception_type_prefix, const char *for return NULL; } +/** + * Use for Python callbacks run directly from C, + * when we can't use normal methods of raising exceptions. + */ +void PyC_Err_PrintWithFunc(PyObject *py_func) +{ + /* since we return to C code we can't leave the error */ + PyCodeObject *f_code = (PyCodeObject *)PyFunction_GET_CODE(py_func); + PyErr_Print(); + PyErr_Clear(); + + /* use py style error */ + fprintf(stderr, "File \"%s\", line %d, in %s\n", + _PyUnicode_AsString(f_code->co_filename), + f_code->co_firstlineno, + _PyUnicode_AsString(((PyFunctionObject *)py_func)->func_name) + ); +} + /* returns the exception string as a new PyUnicode object, depends on external traceback module */ #if 0 diff --git a/source/blender/python/generic/py_capi_utils.h b/source/blender/python/generic/py_capi_utils.h index 327d4e60954..25c88799027 100644 --- a/source/blender/python/generic/py_capi_utils.h +++ b/source/blender/python/generic/py_capi_utils.h @@ -38,6 +38,8 @@ PyObject * PyC_ExceptionBuffer_Simple(void); PyObject * PyC_Object_GetAttrStringArgs(PyObject *o, Py_ssize_t n, ...); PyObject * PyC_FrozenSetFromStrings(const char **strings); PyObject * PyC_Err_Format_Prefix(PyObject *exception_type_prefix, const char *format, ...); +void PyC_Err_PrintWithFunc(PyObject *py_func); + void PyC_FileAndNum(const char **filename, int *lineno); void PyC_FileAndNum_Safe(const char **filename, int *lineno); /* checks python is running */ int PyC_AsArray_FAST( diff --git a/source/blender/python/intern/bpy_props.c b/source/blender/python/intern/bpy_props.c index daef323058c..e4e21587c79 100644 --- a/source/blender/python/intern/bpy_props.c +++ b/source/blender/python/intern/bpy_props.c @@ -180,22 +180,6 @@ static PyObject *pyrna_struct_as_instance(PointerRNA *ptr) return self; } -/* could be moved into bpy_utils */ -static void printf_func_error(PyObject *py_func) -{ - /* since we return to C code we can't leave the error */ - PyCodeObject *f_code = (PyCodeObject *)PyFunction_GET_CODE(py_func); - PyErr_Print(); - PyErr_Clear(); - - /* use py style error */ - fprintf(stderr, "File \"%s\", line %d, in %s\n", - _PyUnicode_AsString(f_code->co_filename), - f_code->co_firstlineno, - _PyUnicode_AsString(((PyFunctionObject *)py_func)->func_name) - ); -} - static void bpy_prop_assign_flag(PropertyRNA *prop, const int flag) { const int flag_mask = ((PROP_ANIMATABLE) & ~flag); @@ -261,12 +245,12 @@ static void bpy_prop_update_cb(struct bContext *C, struct PointerRNA *ptr, struc Py_DECREF(args); if (ret == NULL) { - printf_func_error(py_func); + PyC_Err_PrintWithFunc(py_func); } else { if (ret != Py_None) { PyErr_SetString(PyExc_ValueError, "the return value must be None"); - printf_func_error(py_func); + PyC_Err_PrintWithFunc(py_func); } Py_DECREF(ret); @@ -313,14 +297,14 @@ static int bpy_prop_boolean_get_cb(struct PointerRNA *ptr, struct PropertyRNA *p Py_DECREF(args); if (ret == NULL) { - printf_func_error(py_func); + PyC_Err_PrintWithFunc(py_func); value = false; } else { value = PyC_Long_AsI32(ret); if (value == -1 && PyErr_Occurred()) { - printf_func_error(py_func); + PyC_Err_PrintWithFunc(py_func); value = false; } @@ -372,12 +356,12 @@ static void bpy_prop_boolean_set_cb(struct PointerRNA *ptr, struct PropertyRNA * Py_DECREF(args); if (ret == NULL) { - printf_func_error(py_func); + PyC_Err_PrintWithFunc(py_func); } else { if (ret != Py_None) { PyErr_SetString(PyExc_ValueError, "the return value must be None"); - printf_func_error(py_func); + PyC_Err_PrintWithFunc(py_func); } Py_DECREF(ret); @@ -421,7 +405,7 @@ static int bpy_prop_poll_cb(struct PointerRNA *self, PointerRNA candidate, struc Py_DECREF(args); if (ret == NULL) { - printf_func_error(py_func); + PyC_Err_PrintWithFunc(py_func); result = false; } else { @@ -470,14 +454,14 @@ static void bpy_prop_boolean_array_get_cb(struct PointerRNA *ptr, struct Propert Py_DECREF(args); if (ret == NULL) { - printf_func_error(py_func); + PyC_Err_PrintWithFunc(py_func); for (i = 0; i < len; ++i) values[i] = false; } else { if (PyC_AsArray(values, ret, len, &PyBool_Type, false, "BoolVectorProperty get") == -1) { - printf_func_error(py_func); + PyC_Err_PrintWithFunc(py_func); for (i = 0; i < len; ++i) values[i] = false; @@ -535,12 +519,12 @@ static void bpy_prop_boolean_array_set_cb(struct PointerRNA *ptr, struct Propert Py_DECREF(args); if (ret == NULL) { - printf_func_error(py_func); + PyC_Err_PrintWithFunc(py_func); } else { if (ret != Py_None) { PyErr_SetString(PyExc_ValueError, "the return value must be None"); - printf_func_error(py_func); + PyC_Err_PrintWithFunc(py_func); } Py_DECREF(ret); @@ -588,14 +572,14 @@ static int bpy_prop_int_get_cb(struct PointerRNA *ptr, struct PropertyRNA *prop) Py_DECREF(args); if (ret == NULL) { - printf_func_error(py_func); + PyC_Err_PrintWithFunc(py_func); value = 0.0f; } else { value = PyC_Long_AsI32(ret); if (value == -1 && PyErr_Occurred()) { - printf_func_error(py_func); + PyC_Err_PrintWithFunc(py_func); value = 0; } @@ -647,12 +631,12 @@ static void bpy_prop_int_set_cb(struct PointerRNA *ptr, struct PropertyRNA *prop Py_DECREF(args); if (ret == NULL) { - printf_func_error(py_func); + PyC_Err_PrintWithFunc(py_func); } else { if (ret != Py_None) { PyErr_SetString(PyExc_ValueError, "the return value must be None"); - printf_func_error(py_func); + PyC_Err_PrintWithFunc(py_func); } Py_DECREF(ret); @@ -700,14 +684,14 @@ static void bpy_prop_int_array_get_cb(struct PointerRNA *ptr, struct PropertyRNA Py_DECREF(args); if (ret == NULL) { - printf_func_error(py_func); + PyC_Err_PrintWithFunc(py_func); for (i = 0; i < len; ++i) values[i] = 0; } else { if (PyC_AsArray(values, ret, len, &PyLong_Type, false, "IntVectorProperty get") == -1) { - printf_func_error(py_func); + PyC_Err_PrintWithFunc(py_func); for (i = 0; i < len; ++i) values[i] = 0; @@ -765,12 +749,12 @@ static void bpy_prop_int_array_set_cb(struct PointerRNA *ptr, struct PropertyRNA Py_DECREF(args); if (ret == NULL) { - printf_func_error(py_func); + PyC_Err_PrintWithFunc(py_func); } else { if (ret != Py_None) { PyErr_SetString(PyExc_ValueError, "the return value must be None"); - printf_func_error(py_func); + PyC_Err_PrintWithFunc(py_func); } Py_DECREF(ret); @@ -818,14 +802,14 @@ static float bpy_prop_float_get_cb(struct PointerRNA *ptr, struct PropertyRNA *p Py_DECREF(args); if (ret == NULL) { - printf_func_error(py_func); + PyC_Err_PrintWithFunc(py_func); value = 0.0f; } else { value = PyFloat_AsDouble(ret); if (value == -1.0f && PyErr_Occurred()) { - printf_func_error(py_func); + PyC_Err_PrintWithFunc(py_func); value = 0.0f; } @@ -877,12 +861,12 @@ static void bpy_prop_float_set_cb(struct PointerRNA *ptr, struct PropertyRNA *pr Py_DECREF(args); if (ret == NULL) { - printf_func_error(py_func); + PyC_Err_PrintWithFunc(py_func); } else { if (ret != Py_None) { PyErr_SetString(PyExc_ValueError, "the return value must be None"); - printf_func_error(py_func); + PyC_Err_PrintWithFunc(py_func); } Py_DECREF(ret); @@ -930,14 +914,14 @@ static void bpy_prop_float_array_get_cb(struct PointerRNA *ptr, struct PropertyR Py_DECREF(args); if (ret == NULL) { - printf_func_error(py_func); + PyC_Err_PrintWithFunc(py_func); for (i = 0; i < len; ++i) values[i] = 0.0f; } else { if (PyC_AsArray(values, ret, len, &PyFloat_Type, false, "FloatVectorProperty get") == -1) { - printf_func_error(py_func); + PyC_Err_PrintWithFunc(py_func); for (i = 0; i < len; ++i) values[i] = 0.0f; @@ -995,12 +979,12 @@ static void bpy_prop_float_array_set_cb(struct PointerRNA *ptr, struct PropertyR Py_DECREF(args); if (ret == NULL) { - printf_func_error(py_func); + PyC_Err_PrintWithFunc(py_func); } else { if (ret != Py_None) { PyErr_SetString(PyExc_ValueError, "the return value must be None"); - printf_func_error(py_func); + PyC_Err_PrintWithFunc(py_func); } Py_DECREF(ret); @@ -1047,14 +1031,14 @@ static void bpy_prop_string_get_cb(struct PointerRNA *ptr, struct PropertyRNA *p Py_DECREF(args); if (ret == NULL) { - printf_func_error(py_func); + PyC_Err_PrintWithFunc(py_func); value[0] = '\0'; } else if (!PyUnicode_Check(ret)) { PyErr_Format(PyExc_TypeError, "return value must be a string, not %.200s", Py_TYPE(ret)->tp_name); - printf_func_error(py_func); + PyC_Err_PrintWithFunc(py_func); value[0] = '\0'; Py_DECREF(ret); } @@ -1107,14 +1091,14 @@ static int bpy_prop_string_length_cb(struct PointerRNA *ptr, struct PropertyRNA Py_DECREF(args); if (ret == NULL) { - printf_func_error(py_func); + PyC_Err_PrintWithFunc(py_func); length = 0; } else if (!PyUnicode_Check(ret)) { PyErr_Format(PyExc_TypeError, "return value must be a string, not %.200s", Py_TYPE(ret)->tp_name); - printf_func_error(py_func); + PyC_Err_PrintWithFunc(py_func); length = 0; Py_DECREF(ret); } @@ -1167,7 +1151,7 @@ static void bpy_prop_string_set_cb(struct PointerRNA *ptr, struct PropertyRNA *p py_value = PyUnicode_FromString(value); if (!py_value) { PyErr_SetString(PyExc_ValueError, "the return value must be a string"); - printf_func_error(py_func); + PyC_Err_PrintWithFunc(py_func); } else PyTuple_SET_ITEM(args, 1, py_value); @@ -1177,12 +1161,12 @@ static void bpy_prop_string_set_cb(struct PointerRNA *ptr, struct PropertyRNA *p Py_DECREF(args); if (ret == NULL) { - printf_func_error(py_func); + PyC_Err_PrintWithFunc(py_func); } else { if (ret != Py_None) { PyErr_SetString(PyExc_ValueError, "the return value must be None"); - printf_func_error(py_func); + PyC_Err_PrintWithFunc(py_func); } Py_DECREF(ret); @@ -1230,14 +1214,14 @@ static int bpy_prop_enum_get_cb(struct PointerRNA *ptr, struct PropertyRNA *prop Py_DECREF(args); if (ret == NULL) { - printf_func_error(py_func); + PyC_Err_PrintWithFunc(py_func); value = RNA_property_enum_get_default(ptr, prop); } else { value = PyC_Long_AsI32(ret); if (value == -1 && PyErr_Occurred()) { - printf_func_error(py_func); + PyC_Err_PrintWithFunc(py_func); value = RNA_property_enum_get_default(ptr, prop); } @@ -1289,12 +1273,12 @@ static void bpy_prop_enum_set_cb(struct PointerRNA *ptr, struct PropertyRNA *pro Py_DECREF(args); if (ret == NULL) { - printf_func_error(py_func); + PyC_Err_PrintWithFunc(py_func); } else { if (ret != Py_None) { PyErr_SetString(PyExc_ValueError, "the return value must be None"); - printf_func_error(py_func); + PyC_Err_PrintWithFunc(py_func); } Py_DECREF(ret); @@ -1572,7 +1556,7 @@ static const EnumPropertyItem *bpy_prop_enum_itemf_cb(struct bContext *C, Pointe *r_free = true; } else { - printf_func_error(py_func); + PyC_Err_PrintWithFunc(py_func); eitems = DummyRNA_NULL_items; } |