diff options
Diffstat (limited to 'source/blender/python/generic/IDProp.c')
-rw-r--r-- | source/blender/python/generic/IDProp.c | 144 |
1 files changed, 72 insertions, 72 deletions
diff --git a/source/blender/python/generic/IDProp.c b/source/blender/python/generic/IDProp.c index 73e60a384a3..a83359c31f7 100644 --- a/source/blender/python/generic/IDProp.c +++ b/source/blender/python/generic/IDProp.c @@ -1,4 +1,4 @@ -/** +/* * $Id$ * * ***** BEGIN GPL LICENSE BLOCK ***** @@ -23,10 +23,17 @@ * ***** END GPL LICENSE BLOCK ***** */ -#include "BKE_idprop.h" +#include <Python.h> + #include "IDProp.h" #include "MEM_guardedalloc.h" +#include "BLI_string.h" +#include "BLI_utildefines.h" + +#include "BKE_idprop.h" + + #define USE_STRING_COERCE #ifdef USE_STRING_COERCE @@ -34,7 +41,7 @@ #endif PyObject * PyC_UnicodeFromByte(const char *str); -const char * PuC_UnicodeAsByte(PyObject *py_str, PyObject **coerce); /* coerce must be NULL */ +const char * PyC_UnicodeAsByte(PyObject *py_str, PyObject **coerce); /* coerce must be NULL */ /*** Function to wrap ID properties ***/ PyObject *BPy_Wrap_IDProperty(ID *id, IDProperty *prop, IDProperty *parent); @@ -44,7 +51,7 @@ extern PyTypeObject IDGroup_Iter_Type; /*********************** ID Property Main Wrapper Stuff ***************/ -PyObject *IDGroup_repr( BPy_IDProperty *self ) +static PyObject *IDGroup_repr( BPy_IDProperty *self ) { return PyUnicode_FromFormat( "<bpy ID property from \"%s\">", self->id->name); } @@ -58,7 +65,7 @@ PyObject *BPy_IDGroup_WrapData( ID *id, IDProperty *prop ) #ifdef USE_STRING_COERCE return PyC_UnicodeFromByte(prop->data.pointer); #else - return PyUnicode_FromString( prop->data.pointer ); + return PyUnicode_FromString(prop->data.pointer); #endif case IDP_INT: return PyLong_FromLong( (long)prop->data.val ); @@ -88,7 +95,7 @@ PyObject *BPy_IDGroup_WrapData( ID *id, IDProperty *prop ) int i; if (!seq) { - PyErr_Format( PyExc_RuntimeError, "BPy_IDGroup_MapDataToPy, IDP_IDPARRAY: PyList_New(%d) failed", prop->len); + PyErr_Format(PyExc_RuntimeError, "BPy_IDGroup_MapDataToPy, IDP_IDPARRAY: PyList_New(%d) failed", prop->len); return NULL; } @@ -108,7 +115,8 @@ PyObject *BPy_IDGroup_WrapData( ID *id, IDProperty *prop ) Py_RETURN_NONE; } -int BPy_IDGroup_SetData(BPy_IDProperty *self, IDProperty *prop, PyObject *value) +#if 0 /* UNUSED, currenly assignment overwrites into new properties, rather then setting in-place */ +static int BPy_IDGroup_SetData(BPy_IDProperty *self, IDProperty *prop, PyObject *value) { switch (prop->type) { case IDP_STRING: @@ -123,10 +131,10 @@ int BPy_IDGroup_SetData(BPy_IDProperty *self, IDProperty *prop, PyObject *value) int alloc_len; PyObject *value_coerce= NULL; - st= (char *)PuC_UnicodeAsByte(value, &value_coerce); + st= (char *)PyC_UnicodeAsByte(value, &value_coerce); alloc_len= strlen(st) + 1; - st = _PyUnicode_AsString(value); + st = _PyUnicode_AsString(value); IDP_ResizeArray(prop, alloc_len); memcpy(prop->data.pointer, st, alloc_len); Py_XDECREF(value_coerce); @@ -176,13 +184,14 @@ int BPy_IDGroup_SetData(BPy_IDProperty *self, IDProperty *prop, PyObject *value) } return 0; } +#endif -PyObject *BPy_IDGroup_GetName(BPy_IDProperty *self, void *bleh) +static PyObject *BPy_IDGroup_GetName(BPy_IDProperty *self, void *UNUSED(closure)) { return PyUnicode_FromString(self->prop->name); } -static int BPy_IDGroup_SetName(BPy_IDProperty *self, PyObject *value, void *bleh) +static int BPy_IDGroup_SetName(BPy_IDProperty *self, PyObject *value, void *UNUSED(closure)) { char *st; if (!PyUnicode_Check(value)) { @@ -191,12 +200,12 @@ static int BPy_IDGroup_SetName(BPy_IDProperty *self, PyObject *value, void *bleh } st = _PyUnicode_AsString(value); - if (strlen(st) >= MAX_IDPROP_NAME) { + if (BLI_strnlen(st, MAX_IDPROP_NAME) == MAX_IDPROP_NAME) { PyErr_SetString(PyExc_TypeError, "string length cannot exceed 31 characters!"); return -1; } - strcpy(self->prop->name, st); + BLI_strncpy(self->prop->name, st, sizeof(self->prop->name)); return 0; } @@ -208,17 +217,14 @@ static PyObject *BPy_IDGroup_GetType(BPy_IDProperty *self) #endif static PyGetSetDef BPy_IDGroup_getseters[] = { - {"name", - (getter)BPy_IDGroup_GetName, (setter)BPy_IDGroup_SetName, - "The name of this Group.", - NULL}, + {(char *)"name", (getter)BPy_IDGroup_GetName, (setter)BPy_IDGroup_SetName, (char *)"The name of this Group.", NULL}, {NULL, NULL, NULL, NULL, NULL} }; static Py_ssize_t BPy_IDGroup_Map_Len(BPy_IDProperty *self) { if (self->prop->type != IDP_GROUP) { - PyErr_SetString( PyExc_TypeError, "len() of unsized object"); + PyErr_SetString(PyExc_TypeError, "len() of unsized object"); return -1; } @@ -231,21 +237,21 @@ static PyObject *BPy_IDGroup_Map_GetItem(BPy_IDProperty *self, PyObject *item) char *name; if (self->prop->type != IDP_GROUP) { - PyErr_SetString( PyExc_TypeError, "unsubscriptable object"); + PyErr_SetString(PyExc_TypeError, "unsubscriptable object"); return NULL; } name= _PyUnicode_AsString(item); if (name == NULL) { - PyErr_SetString( PyExc_TypeError, "only strings are allowed as keys of ID properties"); + PyErr_SetString(PyExc_TypeError, "only strings are allowed as keys of ID properties"); return NULL; } idprop= IDP_GetPropertyFromGroup(self->prop, name); if(idprop==NULL) { - PyErr_SetString( PyExc_KeyError, "key not in subgroup dict"); + PyErr_SetString(PyExc_KeyError, "key not in subgroup dict"); return NULL; } @@ -259,7 +265,7 @@ static int idp_sequence_type(PyObject *seq) PyObject *item; int type= IDP_INT; - int i, len = PySequence_Length(seq); + int i, len = PySequence_Size(seq); for (i=0; i < len; i++) { item = PySequence_GetItem(seq, i); if (PyFloat_Check(item)) { @@ -294,7 +300,7 @@ static int idp_sequence_type(PyObject *seq) } /* note: group can be a pointer array or a group */ -char *BPy_IDProperty_Map_ValidateAndCreate(char *name, IDProperty *group, PyObject *ob) +const char *BPy_IDProperty_Map_ValidateAndCreate(const char *name, IDProperty *group, PyObject *ob) { IDProperty *prop = NULL; IDPropertyTemplate val = {0}; @@ -311,7 +317,7 @@ char *BPy_IDProperty_Map_ValidateAndCreate(char *name, IDProperty *group, PyObje } else if (PyUnicode_Check(ob)) { #ifdef USE_STRING_COERCE PyObject *value_coerce= NULL; - val.str = (char *)PuC_UnicodeAsByte(ob, &value_coerce); + val.str = (char *)PyC_UnicodeAsByte(ob, &value_coerce); prop = IDP_New(IDP_STRING, val, name); Py_XDECREF(value_coerce); #else @@ -329,7 +335,7 @@ char *BPy_IDProperty_Map_ValidateAndCreate(char *name, IDProperty *group, PyObje we assume IDP_INT unless we hit a float number; then we assume it's */ - val.array.len = PySequence_Length(ob); + val.array.len = PySequence_Size(ob); switch(val.array.type) { case IDP_DOUBLE: @@ -351,7 +357,7 @@ char *BPy_IDProperty_Map_ValidateAndCreate(char *name, IDProperty *group, PyObje case IDP_IDPARRAY: prop= IDP_NewIDPArray(name); for (i=0; i<val.array.len; i++) { - char *error; + const char *error; item = PySequence_GetItem(ob, i); error= BPy_IDProperty_Map_ValidateAndCreate("", prop, item); Py_DECREF(item); @@ -414,7 +420,7 @@ char *BPy_IDProperty_Map_ValidateAndCreate(char *name, IDProperty *group, PyObje int BPy_Wrap_SetMapItem(IDProperty *prop, PyObject *key, PyObject *val) { if (prop->type != IDP_GROUP) { - PyErr_SetString( PyExc_TypeError, "unsubscriptable object"); + PyErr_SetString(PyExc_TypeError, "unsubscriptable object"); return -1; } @@ -426,21 +432,21 @@ int BPy_Wrap_SetMapItem(IDProperty *prop, PyObject *key, PyObject *val) MEM_freeN(pkey); return 0; } else { - PyErr_SetString( PyExc_KeyError, "property not found in group" ); + PyErr_SetString(PyExc_KeyError, "property not found in group"); return -1; } } else { - char *err; + const char *err; if (!PyUnicode_Check(key)) { - PyErr_SetString( PyExc_TypeError, "only strings are allowed as subgroup keys" ); + PyErr_SetString(PyExc_TypeError, "only strings are allowed as subgroup keys"); return -1; } err = BPy_IDProperty_Map_ValidateAndCreate(_PyUnicode_AsString(key), prop, val); if (err) { - PyErr_SetString( PyExc_KeyError, err ); + PyErr_SetString(PyExc_KeyError, err ); return -1; } @@ -488,7 +494,7 @@ static PyObject *BPy_IDGroup_MapDataToPy(IDProperty *prop) int i; if (!seq) { - PyErr_Format( PyExc_RuntimeError, "BPy_IDGroup_MapDataToPy, IDP_ARRAY: PyList_New(%d) failed", prop->len); + PyErr_Format(PyExc_RuntimeError, "BPy_IDGroup_MapDataToPy, IDP_ARRAY: PyList_New(%d) failed", prop->len); return NULL; } @@ -513,7 +519,7 @@ static PyObject *BPy_IDGroup_MapDataToPy(IDProperty *prop) int i; if (!seq) { - PyErr_Format( PyExc_RuntimeError, "BPy_IDGroup_MapDataToPy, IDP_IDPARRAY: PyList_New(%d) failed", prop->len); + PyErr_Format(PyExc_RuntimeError, "BPy_IDGroup_MapDataToPy, IDP_IDPARRAY: PyList_New(%d) failed", prop->len); return NULL; } @@ -555,7 +561,7 @@ static PyObject *BPy_IDGroup_Pop(BPy_IDProperty *self, PyObject *value) char *name = _PyUnicode_AsString(value); if (!name) { - PyErr_SetString( PyExc_TypeError, "pop expected at least 1 argument, got 0" ); + PyErr_SetString(PyExc_TypeError, "pop expected at least 1 argument, got 0"); return NULL; } @@ -575,7 +581,7 @@ static PyObject *BPy_IDGroup_Pop(BPy_IDProperty *self, PyObject *value) return pyform; } - PyErr_SetString( PyExc_KeyError, "item not in group" ); + PyErr_SetString(PyExc_KeyError, "item not in group"); return NULL; } @@ -692,7 +698,7 @@ static int BPy_IDGroup_Contains(BPy_IDProperty *self, PyObject *value) char *name = _PyUnicode_AsString(value); if (!name) { - PyErr_SetString( PyExc_TypeError, "expected a string"); + PyErr_SetString(PyExc_TypeError, "expected a string"); return -1; } @@ -705,7 +711,7 @@ static PyObject *BPy_IDGroup_Update(BPy_IDProperty *self, PyObject *value) Py_ssize_t i=0; if (!PyDict_Check(value)) { - PyErr_SetString( PyExc_TypeError, "expected an object derived from dict."); + PyErr_SetString(PyExc_TypeError, "expected an object derived from dict"); return NULL; } @@ -724,7 +730,7 @@ static PyObject *BPy_IDGroup_ConvertToPy(BPy_IDProperty *self) /* Matches python dict.get(key, [default]) */ -PyObject* BPy_IDGroup_Get(BPy_IDProperty *self, PyObject *args) +static PyObject* BPy_IDGroup_Get(BPy_IDProperty *self, PyObject *args) { IDProperty *idprop; char *key; @@ -761,23 +767,23 @@ static struct PyMethodDef BPy_IDGroup_methods[] = { "idprop.get(k[,d]) -> idprop[k] if k in idprop, else d. d defaults to None"}, {"convert_to_pyobject", (PyCFunction)BPy_IDGroup_ConvertToPy, METH_NOARGS, "return a purely python version of the group"}, - {0, NULL, 0, NULL} + {NULL, NULL, 0, NULL} }; static PySequenceMethods BPy_IDGroup_Seq = { - (lenfunc) BPy_IDGroup_Map_Len, /* lenfunc sq_length */ - 0, /* binaryfunc sq_concat */ - 0, /* ssizeargfunc sq_repeat */ - 0, /* ssizeargfunc sq_item */ /* TODO - setting this will allow PySequence_Check to return True */ - 0, /* intintargfunc ***was_sq_slice*** */ - 0, /* intobjargproc sq_ass_item */ - 0, /* ssizeobjargproc ***was_sq_ass_slice*** */ + (lenfunc) BPy_IDGroup_Map_Len, /* lenfunc sq_length */ + NULL, /* binaryfunc sq_concat */ + NULL, /* ssizeargfunc sq_repeat */ + NULL, /* ssizeargfunc sq_item */ /* TODO - setting this will allow PySequence_Check to return True */ + NULL, /* intintargfunc ***was_sq_slice*** */ + NULL, /* intobjargproc sq_ass_item */ + NULL, /* ssizeobjargproc ***was_sq_ass_slice*** */ (objobjproc) BPy_IDGroup_Contains, /* objobjproc sq_contains */ - 0, /* binaryfunc sq_inplace_concat */ - 0, /* ssizeargfunc sq_inplace_repeat */ + NULL, /* binaryfunc sq_inplace_concat */ + NULL, /* ssizeargfunc sq_inplace_repeat */ }; -PyMappingMethods BPy_IDGroup_Mapping = { +static PyMappingMethods BPy_IDGroup_Mapping = { (lenfunc)BPy_IDGroup_Map_Len, /*inquiry mp_length */ (binaryfunc)BPy_IDGroup_Map_GetItem, /*binaryfunc mp_subscript */ (objobjargproc)BPy_IDGroup_Map_SetItem, /*objobjargproc mp_ass_subscript */ @@ -860,7 +866,7 @@ PyObject *BPy_Wrap_IDProperty(ID *id, IDProperty *prop, IDProperty *parent) static PyObject *IDArray_repr(BPy_IDArray *self) { - return PyUnicode_FromString("(ID Array)"); + return PyUnicode_FromFormat("(ID Array [%d])", self->prop->len); } @@ -875,14 +881,8 @@ static PyObject *BPy_IDArray_GetLen(BPy_IDArray *self) } static PyGetSetDef BPy_IDArray_getseters[] = { - {"len", - (getter)BPy_IDArray_GetLen, (setter)NULL, - "The length of the array, can also be gotten with len(array).", - NULL}, - {"type", - (getter)BPy_IDArray_GetType, (setter)NULL, - "The type of the data in the array, is an ant.", - NULL}, + {(char *)"len", (getter)BPy_IDArray_GetLen, (setter)NULL, (char *)"The length of the array, can also be gotten with len(array).", NULL}, + {(char *)"type", (getter)BPy_IDArray_GetType, (setter)NULL, (char *)"The type of the data in the array, is an ant.", NULL}, {NULL, NULL, NULL, NULL, NULL}, }; @@ -894,7 +894,7 @@ static PyObject *BPy_IDArray_ConvertToPy(BPy_IDArray *self) static PyMethodDef BPy_IDArray_methods[] = { {"convert_to_pyobject", (PyCFunction)BPy_IDArray_ConvertToPy, METH_NOARGS, "return a purely python version of the group"}, - {0, NULL, 0, NULL} + {NULL, NULL, 0, NULL} }; static int BPy_IDArray_Len(BPy_IDArray *self) @@ -905,7 +905,7 @@ static int BPy_IDArray_Len(BPy_IDArray *self) static PyObject *BPy_IDArray_GetItem(BPy_IDArray *self, int index) { if (index < 0 || index >= self->prop->len) { - PyErr_SetString( PyExc_IndexError, "index out of range!"); + PyErr_SetString(PyExc_IndexError, "index out of range!"); return NULL; } @@ -921,7 +921,7 @@ static PyObject *BPy_IDArray_GetItem(BPy_IDArray *self, int index) break; } - PyErr_SetString( PyExc_RuntimeError, "invalid/corrupt array type!"); + PyErr_SetString(PyExc_RuntimeError, "invalid/corrupt array type!"); return NULL; } @@ -932,7 +932,7 @@ static int BPy_IDArray_SetItem(BPy_IDArray *self, int index, PyObject *value) double d; if (index < 0 || index >= self->prop->len) { - PyErr_SetString( PyExc_RuntimeError, "index out of range!"); + PyErr_SetString(PyExc_RuntimeError, "index out of range!"); return -1; } @@ -968,16 +968,16 @@ static int BPy_IDArray_SetItem(BPy_IDArray *self, int index, PyObject *value) static PySequenceMethods BPy_IDArray_Seq = { (lenfunc) BPy_IDArray_Len, /* inquiry sq_length */ - 0, /* binaryfunc sq_concat */ - 0, /* intargfunc sq_repeat */ + NULL, /* binaryfunc sq_concat */ + NULL, /* intargfunc sq_repeat */ (ssizeargfunc)BPy_IDArray_GetItem, /* intargfunc sq_item */ - 0, /* intintargfunc sq_slice */ - (ssizeobjargproc)BPy_IDArray_SetItem, /* intobjargproc sq_ass_item */ - 0, /* intintobjargproc sq_ass_slice */ - 0, /* objobjproc sq_contains */ + NULL, /* intintargfunc sq_slice */ + (ssizeobjargproc)BPy_IDArray_SetItem,/* intobjargproc sq_ass_item */ + NULL, /* intintobjargproc sq_ass_slice */ + NULL, /* objobjproc sq_contains */ /* Added in release 2.0 */ - 0, /* binaryfunc sq_inplace_concat */ - 0, /* intargfunc sq_inplace_repeat */ + NULL, /* binaryfunc sq_inplace_concat */ + NULL, /* intargfunc sq_inplace_repeat */ }; PyTypeObject IDArray_Type = { @@ -1071,7 +1071,7 @@ static PyObject *IDGroup_Iter_iterself(PyObject *self) static PyObject *IDGroup_Iter_repr(BPy_IDGroup_Iter *self) { - return PyUnicode_FromString("(ID Property Group)"); + return PyUnicode_FromFormat("(ID Property Group Iter \"%s\")", self->group->prop->name); } static PyObject *BPy_Group_Iter_Next(BPy_IDGroup_Iter *self) @@ -1091,7 +1091,7 @@ static PyObject *BPy_Group_Iter_Next(BPy_IDGroup_Iter *self) return PyUnicode_FromString(cur->name); } } else { - PyErr_SetString( PyExc_StopIteration, "iterator at end" ); + PyErr_SetString(PyExc_StopIteration, "iterator at end"); return NULL; } } |