de-duplicate some idproperty py api code, also improve some exception messages.

This commit is contained in:
2011-11-15 10:19:44 +00:00
parent 995ca539d0
commit 4de917326b
3 changed files with 227 additions and 193 deletions

View File

@@ -21,7 +21,7 @@
* ***** END GPL LICENSE BLOCK ***** * ***** END GPL LICENSE BLOCK *****
*/ */
/** \file blender/python/generic/IDProp.c /** \file blender/python/generic/idprop_py_api.c
* \ingroup pygen * \ingroup pygen
*/ */
@@ -49,6 +49,83 @@ extern PyTypeObject BPy_IDGroup_Type;
/*********************** ID Property Main Wrapper Stuff ***************/ /*********************** ID Property Main Wrapper Stuff ***************/
/* ----------------------------------------------------------------------------
* static conversion functions to avoid duplicate code, no type checking.
*/
static PyObject *idprop_py_from_idp_string(IDProperty *prop)
{
if (prop->subtype == IDP_STRING_SUB_BYTE) {
return PyBytes_FromStringAndSize(IDP_Array(prop), prop->len);
}
else {
#ifdef USE_STRING_COERCE
return PyC_UnicodeFromByteAndSize(IDP_Array(prop), prop->len - 1);
#else
return PyUnicode_FromStringAndSize(IDP_Array(prop), prop->len - 1);
#endif
}
}
static PyObject *idprop_py_from_idp_int(IDProperty *prop)
{
return PyLong_FromLong((long)prop->data.val);
}
static PyObject *idprop_py_from_idp_float(IDProperty *prop)
{
return PyFloat_FromDouble((double)(*(float*)(&prop->data.val)));
}
static PyObject *idprop_py_from_idp_double(IDProperty *prop)
{
return PyFloat_FromDouble((*(double*)(&prop->data.val)));
}
static PyObject *idprop_py_from_idp_group(ID *id, IDProperty *prop, IDProperty *parent)
{
BPy_IDProperty *group= PyObject_New(BPy_IDProperty, &BPy_IDGroup_Type);
group->id = id;
group->prop = prop;
group->parent = parent; /* can be NULL */
return (PyObject*) group;
}
static PyObject *idprop_py_from_idp_array(ID *id, IDProperty *prop)
{
BPy_IDProperty *array = PyObject_New(BPy_IDProperty, &BPy_IDArray_Type);
array->id = id;
array->prop = prop;
return (PyObject*) array;
}
static PyObject *idprop_py_from_idp_idparray(ID *id, IDProperty *prop)
{
PyObject *seq = PyList_New(prop->len), *wrap;
IDProperty *array= IDP_IDPArray(prop);
int i;
if (!seq) {
PyErr_Format(PyExc_RuntimeError,
"%s: IDP_IDPARRAY: PyList_New(%d) failed",
__func__, prop->len);
return NULL;
}
for (i=0; i<prop->len; i++) {
wrap= BPy_IDGroup_WrapData(id, array++, prop);
if (!wrap) /* BPy_IDGroup_MapDataToPy sets the error */
return NULL;
PyList_SET_ITEM(seq, i, wrap);
}
return seq;
}
/* -------------------------------------------------------------------------- */
/* use for both array and group */ /* use for both array and group */
static long BPy_IDGroup_hash(BPy_IDProperty *self) static long BPy_IDGroup_hash(BPy_IDProperty *self)
{ {
@@ -60,68 +137,26 @@ static PyObject *BPy_IDGroup_repr(BPy_IDProperty *self)
return PyUnicode_FromFormat( "<bpy id property from \"%s\">", self->id->name); return PyUnicode_FromFormat( "<bpy id property from \"%s\">", self->id->name);
} }
PyObject *BPy_IDGroup_WrapData( ID *id, IDProperty *prop ) PyObject *BPy_IDGroup_WrapData(ID *id, IDProperty *prop, IDProperty *parent)
{ {
switch ( prop->type ) { switch (prop->type) {
case IDP_STRING: case IDP_STRING:
return idprop_py_from_idp_string(prop);
if (prop->subtype == IDP_STRING_SUB_BYTE) {
return PyBytes_FromStringAndSize(IDP_Array(prop), prop->len);
}
else {
#ifdef USE_STRING_COERCE
return PyC_UnicodeFromByteAndSize(IDP_Array(prop), prop->len - 1);
#else
return PyUnicode_FromStringAndSize(IDP_Array(prop), prop->len - 1);
#endif
}
case IDP_INT: case IDP_INT:
return PyLong_FromLong( (long)prop->data.val ); return idprop_py_from_idp_int(prop);
case IDP_FLOAT: case IDP_FLOAT:
return PyFloat_FromDouble( (double)(*(float*)(&prop->data.val)) ); return idprop_py_from_idp_float(prop);
case IDP_DOUBLE: case IDP_DOUBLE:
return PyFloat_FromDouble( (*(double*)(&prop->data.val)) ); return idprop_py_from_idp_double(prop);
case IDP_GROUP: case IDP_GROUP:
/*blegh*/ return idprop_py_from_idp_group(id, prop, parent);
{
BPy_IDProperty *group = PyObject_New(BPy_IDProperty, &BPy_IDGroup_Type);
group->id = id;
group->prop = prop;
return (PyObject*) group;
}
case IDP_ARRAY: case IDP_ARRAY:
{ return idprop_py_from_idp_idparray(id, prop);
BPy_IDProperty *array = PyObject_New(BPy_IDProperty, &BPy_IDArray_Type);
array->id = id;
array->prop = prop;
return (PyObject*) array;
}
case IDP_IDPARRAY: /* this could be better a internal type */ case IDP_IDPARRAY: /* this could be better a internal type */
{ idprop_py_from_idp_array(id, prop);
PyObject *seq = PyList_New(prop->len), *wrap; default:
IDProperty *array= IDP_IDPArray(prop);
int i;
if (!seq) {
PyErr_Format(PyExc_RuntimeError, "BPy_IDGroup_MapDataToPy, IDP_IDPARRAY: PyList_New(%d) failed", prop->len);
return NULL;
}
for (i=0; i<prop->len; i++) {
wrap= BPy_IDGroup_WrapData(id, array++);
if (!wrap) /* BPy_IDGroup_MapDataToPy sets the error */
return NULL;
PyList_SET_ITEM(seq, i, wrap);
}
return seq;
}
/* case IDP_IDPARRAY: TODO */
}
Py_RETURN_NONE; Py_RETURN_NONE;
}
} }
#if 0 /* UNUSED, currenly assignment overwrites into new properties, rather than setting in-place */ #if 0 /* UNUSED, currenly assignment overwrites into new properties, rather than setting in-place */
@@ -135,6 +170,7 @@ static int BPy_IDGroup_SetData(BPy_IDProperty *self, IDProperty *prop, PyObject
PyErr_SetString(PyExc_TypeError, "expected a string!"); PyErr_SetString(PyExc_TypeError, "expected a string!");
return -1; return -1;
} }
/* NOTE: if this code is enabled, bytes support needs to be added */
#ifdef USE_STRING_COERCE #ifdef USE_STRING_COERCE
{ {
int alloc_len; int alloc_len;
@@ -267,8 +303,7 @@ static PyObject *BPy_IDGroup_Map_GetItem(BPy_IDProperty *self, PyObject *item)
return NULL; return NULL;
} }
return BPy_IDGroup_WrapData(self->id, idprop); return BPy_IDGroup_WrapData(self->id, idprop, self->prop);
} }
/*returns NULL on success, error string on failure*/ /*returns NULL on success, error string on failure*/
@@ -506,37 +541,27 @@ static PyObject *BPy_IDGroup_iter(BPy_IDProperty *self)
return (PyObject*) iter; return (PyObject*) iter;
} }
/* for simple, non nested types this is the same as BPy_IDGroup_WrapData */
static PyObject *BPy_IDGroup_MapDataToPy(IDProperty *prop) static PyObject *BPy_IDGroup_MapDataToPy(IDProperty *prop)
{ {
switch (prop->type) { switch (prop->type) {
case IDP_STRING: case IDP_STRING:
if (prop->subtype == IDP_STRING_SUB_BYTE) { return idprop_py_from_idp_string(prop);
return PyBytes_FromStringAndSize(IDP_Array(prop), prop->len);
}
else {
#ifdef USE_STRING_COERCE
return PyC_UnicodeFromByteAndSize(IDP_Array(prop), prop->len - 1);
#else
return PyUnicode_FromStringAndSize(IDP_Array(prop), prop->len - 1);
#endif
}
break;
case IDP_FLOAT:
return PyFloat_FromDouble(*((float*)&prop->data.val));
break;
case IDP_DOUBLE:
return PyFloat_FromDouble(*((double*)&prop->data.val));
break;
case IDP_INT: case IDP_INT:
return PyLong_FromSsize_t( prop->data.val ); return idprop_py_from_idp_int(prop);
break; case IDP_FLOAT:
return idprop_py_from_idp_float(prop);
case IDP_DOUBLE:
return idprop_py_from_idp_double(prop);
case IDP_ARRAY: case IDP_ARRAY:
{ {
PyObject *seq = PyList_New(prop->len); PyObject *seq = PyList_New(prop->len);
int i; int i;
if (!seq) { if (!seq) {
PyErr_Format(PyExc_RuntimeError, "BPy_IDGroup_MapDataToPy, IDP_ARRAY: PyList_New(%d) failed", prop->len); PyErr_Format(PyExc_RuntimeError,
"%s: IDP_ARRAY: PyList_New(%d) failed",
__func__, prop->len);
return NULL; return NULL;
} }
@@ -566,7 +591,9 @@ static PyObject *BPy_IDGroup_MapDataToPy(IDProperty *prop)
break; break;
} }
default: default:
PyErr_SetString(PyExc_RuntimeError, "invalid/corrupt array type!"); PyErr_Format(PyExc_RuntimeError,
"%s: invalid/corrupt array type '%d'!",
__func__, prop->subtype);
Py_DECREF(seq); Py_DECREF(seq);
return NULL; return NULL;
} }
@@ -580,7 +607,9 @@ static PyObject *BPy_IDGroup_MapDataToPy(IDProperty *prop)
int i; int i;
if (!seq) { if (!seq) {
PyErr_Format(PyExc_RuntimeError, "BPy_IDGroup_MapDataToPy, IDP_IDPARRAY: PyList_New(%d) failed", prop->len); PyErr_Format(PyExc_RuntimeError,
"%s: IDP_IDPARRAY: PyList_New(%d) failed",
__func__, prop->len);
return NULL; return NULL;
} }
@@ -612,7 +641,9 @@ static PyObject *BPy_IDGroup_MapDataToPy(IDProperty *prop)
} }
} }
PyErr_Format(PyExc_RuntimeError, "eek!! '%s' property exists with a bad type code '%d' !!!", prop->name, prop->type); PyErr_Format(PyExc_RuntimeError,
"%s ERROR: '%s' property exists with a bad type code '%d'!",
__func__, prop->name, prop->type);
return NULL; return NULL;
} }
@@ -623,7 +654,9 @@ static PyObject *BPy_IDGroup_Pop(BPy_IDProperty *self, PyObject *value)
const char *name = _PyUnicode_AsString(value); const char *name = _PyUnicode_AsString(value);
if (!name) { if (!name) {
PyErr_SetString(PyExc_TypeError, "pop expected at least 1 argument, got 0"); PyErr_Format(PyExc_TypeError,
"pop expected at least a string argument, not %.200s",
Py_TYPE(value)->tp_name);
return NULL; return NULL;
} }
@@ -676,44 +709,44 @@ static void BPy_IDGroup_CorrectListLen(IDProperty *prop, PyObject *seq, int len,
PyObject *BPy_Wrap_GetKeys(IDProperty *prop) PyObject *BPy_Wrap_GetKeys(IDProperty *prop)
{ {
PyObject *seq = PyList_New(prop->len); PyObject *list = PyList_New(prop->len);
IDProperty *loop; IDProperty *loop;
int i; int i;
for (i=0, loop=prop->data.group.first; loop && (i < prop->len); loop=loop->next, i++) for (i=0, loop=prop->data.group.first; loop && (i < prop->len); loop=loop->next, i++)
PyList_SET_ITEM(seq, i, PyUnicode_FromString(loop->name)); PyList_SET_ITEM(list, i, PyUnicode_FromString(loop->name));
/* if the id prop is corrupt, count the remaining */ /* if the id prop is corrupt, count the remaining */
for (; loop; loop=loop->next, i++) {} for (; loop; loop=loop->next, i++) {}
if (i != prop->len) { /* if the loop didnt finish, we know the length is wrong */ if (i != prop->len) { /* if the loop didnt finish, we know the length is wrong */
BPy_IDGroup_CorrectListLen(prop, seq, i, __func__); BPy_IDGroup_CorrectListLen(prop, list, i, __func__);
Py_DECREF(seq); /*free the list*/ Py_DECREF(list); /*free the list*/
/*call self again*/ /*call self again*/
return BPy_Wrap_GetKeys(prop); return BPy_Wrap_GetKeys(prop);
} }
return seq; return list;
} }
PyObject *BPy_Wrap_GetValues(ID *id, IDProperty *prop) PyObject *BPy_Wrap_GetValues(ID *id, IDProperty *prop)
{ {
PyObject *seq = PyList_New(prop->len); PyObject *list = PyList_New(prop->len);
IDProperty *loop; IDProperty *loop;
int i; int i;
for (i=0, loop=prop->data.group.first; loop; loop=loop->next, i++) { for (i=0, loop=prop->data.group.first; loop; loop=loop->next, i++) {
PyList_SET_ITEM(seq, i, BPy_IDGroup_WrapData(id, loop)); PyList_SET_ITEM(list, i, BPy_IDGroup_WrapData(id, loop, prop));
} }
if (i != prop->len) { if (i != prop->len) {
BPy_IDGroup_CorrectListLen(prop, seq, i, __func__); BPy_IDGroup_CorrectListLen(prop, list, i, __func__);
Py_DECREF(seq); /*free the list*/ Py_DECREF(list); /*free the list*/
/*call self again*/ /*call self again*/
return BPy_Wrap_GetValues(id, prop); return BPy_Wrap_GetValues(id, prop);
} }
return seq; return list;
} }
PyObject *BPy_Wrap_GetItems(ID *id, IDProperty *prop) PyObject *BPy_Wrap_GetItems(ID *id, IDProperty *prop)
@@ -725,7 +758,7 @@ PyObject *BPy_Wrap_GetItems(ID *id, IDProperty *prop)
for (i=0, loop=prop->data.group.first; loop; loop=loop->next, i++) { for (i=0, loop=prop->data.group.first; loop; loop=loop->next, i++) {
PyObject *item= PyTuple_New(2); PyObject *item= PyTuple_New(2);
PyTuple_SET_ITEM(item, 0, PyUnicode_FromString(loop->name)); PyTuple_SET_ITEM(item, 0, PyUnicode_FromString(loop->name));
PyTuple_SET_ITEM(item, 1, BPy_IDGroup_WrapData(id, loop)); PyTuple_SET_ITEM(item, 1, BPy_IDGroup_WrapData(id, loop, prop));
PyList_SET_ITEM(seq, i, item); PyList_SET_ITEM(seq, i, item);
} }
@@ -760,7 +793,9 @@ static int BPy_IDGroup_Contains(BPy_IDProperty *self, PyObject *value)
const char *name = _PyUnicode_AsString(value); const char *name = _PyUnicode_AsString(value);
if (!name) { if (!name) {
PyErr_SetString(PyExc_TypeError, "expected a string"); PyErr_Format(PyExc_TypeError,
"expected a string, not a %.200s",
Py_TYPE(value)->tp_name);
return -1; return -1;
} }
@@ -773,7 +808,9 @@ static PyObject *BPy_IDGroup_Update(BPy_IDProperty *self, PyObject *value)
Py_ssize_t i=0; Py_ssize_t i=0;
if (!PyDict_Check(value)) { if (!PyDict_Check(value)) {
PyErr_SetString(PyExc_TypeError, "expected an object derived from dict"); PyErr_Format(PyExc_TypeError,
"expected a dict not a %.200s",
Py_TYPE(value)->tp_name);
return NULL; return NULL;
} }
@@ -803,7 +840,7 @@ static PyObject* BPy_IDGroup_Get(BPy_IDProperty *self, PyObject *args)
idprop= IDP_GetPropertyFromGroup(self->prop, key); idprop= IDP_GetPropertyFromGroup(self->prop, key);
if (idprop) { if (idprop) {
PyObject* pyobj = BPy_IDGroup_WrapData(self->id, idprop); PyObject* pyobj = BPy_IDGroup_WrapData(self->id, idprop, self->prop);
if (pyobj) if (pyobj)
return pyobj; return pyobj;
} }
@@ -912,18 +949,6 @@ PyTypeObject BPy_IDGroup_Type = {
BPy_IDGroup_getseters, /* struct PyGetSetDef *tp_getset; */ BPy_IDGroup_getseters, /* struct PyGetSetDef *tp_getset; */
}; };
/*********** Main external wrapping function *******/
PyObject *BPy_Wrap_IDProperty(ID *id, IDProperty *prop, IDProperty *parent)
{
BPy_IDProperty *wrap = PyObject_New(BPy_IDProperty, &BPy_IDGroup_Type);
wrap->prop = prop;
wrap->parent = parent;
wrap->id = id;
//wrap->destroy = 0;
return (PyObject*) wrap;
}
/********Array Wrapper********/ /********Array Wrapper********/
static PyTypeObject *idp_array_py_type(BPy_IDArray *self, short *is_double) static PyTypeObject *idp_array_py_type(BPy_IDArray *self, short *is_double)
@@ -960,7 +985,10 @@ static PyObject *BPy_IDArray_GetType(BPy_IDArray *self)
return PyUnicode_FromString("i"); return PyUnicode_FromString("i");
} }
PyErr_SetString(PyExc_RuntimeError, "invalid/corrupt array type!"); PyErr_Format(PyExc_RuntimeError,
"%s: invalid/corrupt array type '%d'!",
__func__, self->prop->subtype);
return NULL; return NULL;
} }
@@ -1002,7 +1030,10 @@ static PyObject *BPy_IDArray_GetItem(BPy_IDArray *self, int index)
return PyLong_FromLong((long)((int*)IDP_Array(self->prop))[index]); return PyLong_FromLong((long)((int*)IDP_Array(self->prop))[index]);
} }
PyErr_SetString(PyExc_RuntimeError, "invalid/corrupt array type!"); PyErr_Format(PyExc_RuntimeError,
"%s: invalid/corrupt array type '%d'!",
__func__, self->prop->subtype);
return NULL; return NULL;
} }
@@ -1165,7 +1196,9 @@ static PyObject *BPy_IDArray_subscript(BPy_IDArray* self, PyObject* item)
} }
} }
else { else {
PyErr_Format(PyExc_TypeError, "vector indices must be integers, not %.200s", Py_TYPE(item)->tp_name); PyErr_Format(PyExc_TypeError,
"vector indices must be integers, not %.200s",
__func__, Py_TYPE(item)->tp_name);
return NULL; return NULL;
} }
} }
@@ -1194,7 +1227,9 @@ static int BPy_IDArray_ass_subscript(BPy_IDArray* self, PyObject* item, PyObject
} }
} }
else { else {
PyErr_Format(PyExc_TypeError, "vector indices must be integers, not %.200s", Py_TYPE(item)->tp_name); PyErr_Format(PyExc_TypeError,
"vector indices must be integers, not %.200s",
Py_TYPE(item)->tp_name);
return -1; return -1;
} }
} }
@@ -1311,7 +1346,7 @@ static PyObject *BPy_Group_Iter_Next(BPy_IDGroup_Iter *self)
if (self->mode == IDPROP_ITER_ITEMS) { if (self->mode == IDPROP_ITER_ITEMS) {
ret = PyTuple_New(2); ret = PyTuple_New(2);
PyTuple_SET_ITEM(ret, 0, PyUnicode_FromString(cur->name)); PyTuple_SET_ITEM(ret, 0, PyUnicode_FromString(cur->name));
PyTuple_SET_ITEM(ret, 1, BPy_IDGroup_WrapData(self->group->id, cur)); PyTuple_SET_ITEM(ret, 1, BPy_IDGroup_WrapData(self->group->id, cur, self->group->prop));
return ret; return ret;
} }
else { else {

View File

@@ -53,14 +53,13 @@ typedef struct BPy_IDGroup_Iter {
int mode; int mode;
} BPy_IDGroup_Iter; } BPy_IDGroup_Iter;
PyObject *BPy_Wrap_IDProperty(struct ID *id, struct IDProperty *prop, struct IDProperty *parent);
PyObject *BPy_Wrap_GetKeys(struct IDProperty *prop); PyObject *BPy_Wrap_GetKeys(struct IDProperty *prop);
PyObject *BPy_Wrap_GetValues(struct ID *id, struct IDProperty *prop); PyObject *BPy_Wrap_GetValues(struct ID *id, struct IDProperty *prop);
PyObject *BPy_Wrap_GetItems(struct ID *id, struct IDProperty *prop); PyObject *BPy_Wrap_GetItems(struct ID *id, struct IDProperty *prop);
int BPy_Wrap_SetMapItem(struct IDProperty *prop, PyObject *key, PyObject *val); int BPy_Wrap_SetMapItem(struct IDProperty *prop, PyObject *key, PyObject *val);
PyObject *BPy_IDGroup_WrapData(struct ID *id, struct IDProperty *prop ); PyObject *BPy_IDGroup_WrapData(struct ID *id, struct IDProperty *prop, struct IDProperty *parent);
const char *BPy_IDProperty_Map_ValidateAndCreate(PyObject *key, struct IDProperty *group, PyObject *ob); const char *BPy_IDProperty_Map_ValidateAndCreate(PyObject *key, struct IDProperty *group, PyObject *ob);
void IDProp_Init_Types(void); void IDProp_Init_Types(void);

View File

@@ -2782,7 +2782,7 @@ static PyObject *pyrna_struct_subscript(BPy_StructRNA *self, PyObject *key)
return NULL; return NULL;
} }
return BPy_IDGroup_WrapData(self->ptr.id.data, idprop); return BPy_IDGroup_WrapData(self->ptr.id.data, idprop, group);
} }
static int pyrna_struct_ass_subscript(BPy_StructRNA *self, PyObject *key, PyObject *value) static int pyrna_struct_ass_subscript(BPy_StructRNA *self, PyObject *key, PyObject *value)
@@ -3892,7 +3892,7 @@ static PyObject *pyrna_struct_get(BPy_StructRNA *self, PyObject *args)
idprop= IDP_GetPropertyFromGroup(group, key); idprop= IDP_GetPropertyFromGroup(group, key);
if (idprop) { if (idprop) {
return BPy_IDGroup_WrapData(self->ptr.id.data, idprop); return BPy_IDGroup_WrapData(self->ptr.id.data, idprop, group);
} }
} }