diff options
author | Campbell Barton <ideasman42@gmail.com> | 2009-11-19 20:04:28 +0300 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2009-11-19 20:04:28 +0300 |
commit | ac8ff25b2d6465e3ad2b79c359d74b4bab4985e4 (patch) | |
tree | 7d3c51e1e54ea595830a601d53e7fb270d12e2c5 /source/blender | |
parent | 3f10b91235bdcc931bd4fd05ccf6233ddc579229 (diff) |
idprop.get(key, default=None)
matching pythons dict.get()
removed checks for a failed PyObject_New(), if python cant make a new object your probably going to crash anyway.
Diffstat (limited to 'source/blender')
-rw-r--r-- | source/blender/python/generic/IDProp.c | 50 |
1 files changed, 24 insertions, 26 deletions
diff --git a/source/blender/python/generic/IDProp.c b/source/blender/python/generic/IDProp.c index 3af2fece258..b3679c8f4a2 100644 --- a/source/blender/python/generic/IDProp.c +++ b/source/blender/python/generic/IDProp.c @@ -64,11 +64,6 @@ PyObject *BPy_IDGroup_WrapData( ID *id, IDProperty *prop ) /*blegh*/ { BPy_IDProperty *group = PyObject_New(BPy_IDProperty, &IDGroup_Type); - if (!group) { - PyErr_SetString( PyExc_RuntimeError, "PyObject_New() failed" ); - return NULL; - } - group->id = id; group->prop = prop; return (PyObject*) group; @@ -76,10 +71,6 @@ PyObject *BPy_IDGroup_WrapData( ID *id, IDProperty *prop ) case IDP_ARRAY: { BPy_IDProperty *array = PyObject_New(BPy_IDProperty, &IDArray_Type); - if (!array) { - PyErr_SetString( PyExc_RuntimeError, "PyObject_New() failed" ); - return NULL; - } array->id = id; array->prop = prop; return (PyObject*) array; @@ -352,11 +343,6 @@ static int BPy_IDGroup_Map_SetItem(BPy_IDProperty *self, PyObject *key, PyObject static PyObject *BPy_IDGroup_SpawnIterator(BPy_IDProperty *self) { BPy_IDGroup_Iter *iter = PyObject_New(BPy_IDGroup_Iter, &IDGroup_Iter_Type); - - if (!iter) { - PyErr_SetString( PyExc_RuntimeError, "PyObject_New() failed" ); - return NULL; - } iter->group = self; iter->mode = IDPROP_ITER_KEYS; iter->cur = self->prop->data.group.first; @@ -464,12 +450,6 @@ static PyObject *BPy_IDGroup_Pop(BPy_IDProperty *self, PyObject *value) static PyObject *BPy_IDGroup_IterItems(BPy_IDProperty *self) { BPy_IDGroup_Iter *iter = PyObject_New(BPy_IDGroup_Iter, &IDGroup_Iter_Type); - - if (!iter) { - PyErr_SetString( PyExc_RuntimeError, "PyObject_New() failed" ); - return NULL; - } - iter->group = self; iter->mode = IDPROP_ITER_ITEMS; iter->cur = self->prop->data.group.first; @@ -607,6 +587,28 @@ static PyObject *BPy_IDGroup_ConvertToPy(BPy_IDProperty *self) return BPy_IDGroup_MapDataToPy(self->prop); } + +/* Matches python dict.get(key, [default]) */ +PyObject* BPy_IDGroup_Get(BPy_IDProperty *self, PyObject *args) +{ + IDProperty *idprop; + char *key; + PyObject* def = Py_None; + + if (!PyArg_ParseTuple(args, "s|O:get", &key, &def)) + return NULL; + + idprop= IDP_GetPropertyFromGroup(self->prop, key); + if (idprop) { + PyObject* pyobj = BPy_IDGroup_WrapData(self->id, idprop); + if (pyobj) + return pyobj; + } + + Py_INCREF(def); + return def; +} + static struct PyMethodDef BPy_IDGroup_methods[] = { {"pop", (PyCFunction)BPy_IDGroup_Pop, METH_O, "pop an item from the group; raises KeyError if the item doesn't exist."}, @@ -620,6 +622,8 @@ static struct PyMethodDef BPy_IDGroup_methods[] = { "get the items associated with this group."}, {"update", (PyCFunction)BPy_IDGroup_Update, METH_O, "updates the values in the group with the values of another or a dict."}, + {"get", (PyCFunction)BPy_IDGroup_Get, METH_VARARGS, + "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} @@ -709,12 +713,6 @@ PyTypeObject IDGroup_Type = { PyObject *BPy_Wrap_IDProperty(ID *id, IDProperty *prop, IDProperty *parent) { BPy_IDProperty *wrap = PyObject_New(BPy_IDProperty, &IDGroup_Type); - - if (!wrap) { - PyErr_SetString( PyExc_RuntimeError, "PyObject_New() failed" ); - return NULL; - } - wrap->prop = prop; wrap->parent = parent; wrap->id = id; |