diff options
author | Campbell Barton <ideasman42@gmail.com> | 2011-09-15 14:43:55 +0400 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2011-09-15 14:43:55 +0400 |
commit | 264c63ef03dceeac3ecd2177bbfd26391125a4b7 (patch) | |
tree | 73a9b917084ccd328f4853abfd71ca09dd5ed996 /source/blender/python/generic | |
parent | 5ba213a424185e723a4de5833931847f0fe38c49 (diff) |
New C/Py api utility function PyC_Err_Format_Prefix() which raises an error with the existing error as a suffix.
Use this to raise errors when assigning a string property fails even though the value to assign *is* a string.
Before:
TypeError: bpy_struct: item.attr= val: Object.name expected a string type, not str
After:
TypeError: bpy_struct: item.attr= val: Object.name error assigning string, UnicodeEncodeError('utf-8' codec can't encode character '\udce9' in position 23: surrogates not allowed)
Diffstat (limited to 'source/blender/python/generic')
-rw-r--r-- | source/blender/python/generic/py_capi_utils.c | 38 | ||||
-rw-r--r-- | source/blender/python/generic/py_capi_utils.h | 1 |
2 files changed, 39 insertions, 0 deletions
diff --git a/source/blender/python/generic/py_capi_utils.c b/source/blender/python/generic/py_capi_utils.c index a1571dc028c..d5bd44fc288 100644 --- a/source/blender/python/generic/py_capi_utils.c +++ b/source/blender/python/generic/py_capi_utils.c @@ -208,6 +208,44 @@ PyObject *PyC_Object_GetAttrStringArgs(PyObject *o, Py_ssize_t n, ...) return item; } +/* similar to PyErr_Format(), + * + * implimentation - we cant actually preprend the existing exception, + * because it could have _any_ argiments given to it, so instead we get its + * __str__ output and raise our own exception including it. + */ +PyObject *PyC_Err_Format_Prefix(PyObject *exception_type_prefix, const char *format, ...) +{ + PyObject *error_value_prefix; + va_list args; + + va_start(args, format); + error_value_prefix= PyUnicode_FromFormatV(format, args); /* can fail and be NULL */ + va_end(args); + + if(PyErr_Occurred()) { + PyObject *error_type, *error_value, *error_traceback; + PyErr_Fetch(&error_type, &error_value, &error_traceback); + PyErr_Format(exception_type_prefix, + "%S, %.200s(%S)", + error_value_prefix, + Py_TYPE(error_value)->tp_name, + error_value + ); + } + else { + PyErr_SetObject(exception_type_prefix, + error_value_prefix + ); + } + + Py_XDECREF(error_value_prefix); + + /* dumb to always return NULL but matches PyErr_Format */ + return NULL; +} + + /* 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 96c93ab71f8..03a8637710e 100644 --- a/source/blender/python/generic/py_capi_utils.h +++ b/source/blender/python/generic/py_capi_utils.h @@ -34,6 +34,7 @@ void PyC_ObSpit(const char *name, PyObject *var); void PyC_LineSpit(void); PyObject * PyC_ExceptionBuffer(void); PyObject * PyC_Object_GetAttrStringArgs(PyObject *o, Py_ssize_t n, ...); +PyObject * PyC_Err_Format_Prefix(PyObject *exception_type_prefix, const char *format, ...); void PyC_FileAndNum(const char **filename, int *lineno); int PyC_AsArray(void *array, PyObject *value, const int length, const PyTypeObject *type, const short is_double, const char *error_prefix); |