Fix #146588: raise exception when attempting invalid idproperty renaming in Python
Each name has to be unique within a group, so when renaming an idproperty, one has to make sure that the parent group does not contain duplicates afterwards. This patch raises a `NameError` when setting the name to one that exists already. Alternatively, one could delete the already-existing property, but that seems unexpected and the user should rather do that explicitly. This also adds a new unit test for this case. Pull Request: https://projects.blender.org/blender/blender/pulls/146892
This commit is contained in:
@@ -272,6 +272,15 @@ static int BPy_IDGroup_SetName(BPy_IDProperty *self, PyObject *value, void * /*c
|
||||
PyErr_SetString(PyExc_TypeError, "string length cannot exceed 63 characters!");
|
||||
return -1;
|
||||
}
|
||||
if (STREQ(name, self->prop->name)) {
|
||||
return 0;
|
||||
}
|
||||
if (IDProperty *parent = self->parent) {
|
||||
if (IDP_GetPropertyFromGroup(parent, name)) {
|
||||
PyErr_SetString(PyExc_NameError, "property name already exists in parent group");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
memcpy(self->prop->name, name, name_len + 1);
|
||||
return 0;
|
||||
|
||||
@@ -203,6 +203,14 @@ class TestIdPropertyCreation(TestHelper, unittest.TestCase):
|
||||
with self.assertRaises(TypeError):
|
||||
self.id[self.key_id] = self
|
||||
|
||||
def test_rename(self):
|
||||
self.id["foo"] = {"a": 1}
|
||||
self.id["bar"] = {"b": 2}
|
||||
self.id["foo"].name = "foo"
|
||||
self.id["bar"].name = "bar"
|
||||
with self.assertRaises(NameError):
|
||||
self.id["foo"].name = "bar"
|
||||
|
||||
|
||||
class TestIdPropertyUIData(TestHelper, unittest.TestCase):
|
||||
# Default testing idprop key identifier.
|
||||
|
||||
Reference in New Issue
Block a user