Merge branch 'master' into blender2.8

This commit is contained in:
Campbell Barton 2018-05-05 09:57:45 +02:00
commit f91e9529da
3 changed files with 76 additions and 24 deletions

View File

@ -580,10 +580,9 @@ void IDP_ReplaceGroupInGroup(IDProperty *dest, const IDProperty *src)
void IDP_ReplaceInGroup_ex(IDProperty *group, IDProperty *prop, IDProperty *prop_exist)
{
BLI_assert(group->type == IDP_GROUP);
BLI_assert(prop_exist == IDP_GetPropertyFromGroup(group, prop->name));
if ((prop_exist = IDP_GetPropertyFromGroup(group, prop->name))) {
if (prop_exist != NULL) {
BLI_insertlinkreplace(&group->data.group, prop_exist, prop);
IDP_FreeProperty(prop_exist);
MEM_freeN(prop_exist);

View File

@ -855,7 +855,7 @@ static PyObject *BPy_IDGroup_MapDataToPy(IDProperty *prop)
}
PyDoc_STRVAR(BPy_IDGroup_pop_doc,
".. method:: pop(key)\n"
".. method:: pop(key, default)\n"
"\n"
" Remove an item from the group, returning a Python representation.\n"
"\n"
@ -863,38 +863,40 @@ PyDoc_STRVAR(BPy_IDGroup_pop_doc,
"\n"
" :arg key: Name of item to remove.\n"
" :type key: string\n"
" :arg default: Value to return when key isn't found, otherwise raise an exception.\n"
" :type default: Undefined\n"
);
static PyObject *BPy_IDGroup_pop(BPy_IDProperty *self, PyObject *value)
static PyObject *BPy_IDGroup_pop(BPy_IDProperty *self, PyObject *args)
{
IDProperty *idprop;
PyObject *pyform;
const char *name = _PyUnicode_AsString(value);
if (!name) {
PyErr_Format(PyExc_TypeError,
"pop expected at least a string argument, not %.200s",
Py_TYPE(value)->tp_name);
char *key;
PyObject *def = NULL;
if (!PyArg_ParseTuple(args, "s|O:get", &key, &def)) {
return NULL;
}
idprop = IDP_GetPropertyFromGroup(self->prop, name);
if (idprop) {
pyform = BPy_IDGroup_MapDataToPy(idprop);
if (!pyform) {
/* ok something bad happened with the pyobject,
* so don't remove the prop from the group. if pyform is
* NULL, then it already should have raised an exception.*/
idprop = IDP_GetPropertyFromGroup(self->prop, key);
if (idprop == NULL) {
if (def == NULL) {
PyErr_SetString(PyExc_KeyError, "item not in group");
return NULL;
}
IDP_RemoveFromGroup(self->prop, idprop);
return pyform;
return Py_INCREF_RET(def);
}
PyErr_SetString(PyExc_KeyError, "item not in group");
return NULL;
pyform = BPy_IDGroup_MapDataToPy(idprop);
if (pyform == NULL) {
/* ok something bad happened with the pyobject,
* so don't remove the prop from the group. if pyform is
* NULL, then it already should have raised an exception.*/
return NULL;
}
IDP_RemoveFromGroup(self->prop, idprop);
return pyform;
}
PyDoc_STRVAR(BPy_IDGroup_iter_items_doc,
@ -1126,7 +1128,7 @@ static PyObject *BPy_IDGroup_get(BPy_IDProperty *self, PyObject *args)
}
static struct PyMethodDef BPy_IDGroup_methods[] = {
{"pop", (PyCFunction)BPy_IDGroup_pop, METH_O, BPy_IDGroup_pop_doc},
{"pop", (PyCFunction)BPy_IDGroup_pop, METH_VARARGS, BPy_IDGroup_pop_doc},
{"iteritems", (PyCFunction)BPy_IDGroup_iter_items, METH_NOARGS, BPy_IDGroup_iter_items_doc},
{"keys", (PyCFunction)BPy_IDGroup_keys, METH_NOARGS, BPy_IDGroup_keys_doc},
{"values", (PyCFunction)BPy_IDGroup_values, METH_NOARGS, BPy_IDGroup_values_doc},

View File

@ -4714,6 +4714,56 @@ static PyObject *pyrna_struct_get(BPy_StructRNA *self, PyObject *args)
return Py_INCREF_RET(def);
}
PyDoc_STRVAR(pyrna_struct_pop_doc,
".. method:: pop(key, default=None)\n"
"\n"
" Remove and return the value of the custom property assigned to key or default\n"
" when not found (matches pythons dictionary function of the same name).\n"
"\n"
" :arg key: The key associated with the custom property.\n"
" :type key: string\n"
" :arg default: Optional argument for the value to return if\n"
" *key* is not found.\n"
" :type default: Undefined\n"
"\n"
BPY_DOC_ID_PROP_TYPE_NOTE
);
static PyObject *pyrna_struct_pop(BPy_StructRNA *self, PyObject *args)
{
IDProperty *group, *idprop;
const char *key;
PyObject *def = NULL;
PYRNA_STRUCT_CHECK_OBJ(self);
if (!PyArg_ParseTuple(args, "s|O:get", &key, &def))
return NULL;
/* mostly copied from BPy_IDGroup_Map_GetItem */
if (RNA_struct_idprops_check(self->ptr.type) == 0) {
PyErr_SetString(PyExc_TypeError, "this type doesn't support IDProperties");
return NULL;
}
group = RNA_struct_idprops(&self->ptr, 0);
if (group) {
idprop = IDP_GetPropertyFromGroup(group, key);
if (idprop) {
PyObject *ret = BPy_IDGroup_WrapData(self->ptr.id.data, idprop, group);
IDP_RemoveFromGroup(group, idprop);
return ret;
}
}
if (def == NULL) {
PyErr_SetString(PyExc_KeyError, "key not found");
return NULL;
}
return Py_INCREF_RET(def);
}
PyDoc_STRVAR(pyrna_struct_as_pointer_doc,
".. method:: as_pointer()\n"
"\n"
@ -5171,6 +5221,7 @@ static struct PyMethodDef pyrna_struct_methods[] = {
{"items", (PyCFunction)pyrna_struct_items, METH_NOARGS, pyrna_struct_items_doc},
{"get", (PyCFunction)pyrna_struct_get, METH_VARARGS, pyrna_struct_get_doc},
{"pop", (PyCFunction)pyrna_struct_pop, METH_VARARGS, pyrna_struct_pop_doc},
{"as_pointer", (PyCFunction)pyrna_struct_as_pointer, METH_NOARGS, pyrna_struct_as_pointer_doc},