From 4d91f5c8efa002d1c111e4db693298f4282feb62 Mon Sep 17 00:00:00 2001 From: Jacques Lucke Date: Mon, 29 Sep 2025 13:54:08 +0200 Subject: [PATCH] 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 --- source/blender/python/generic/idprop_py_api.cc | 9 +++++++++ tests/python/bl_pyapi_idprop.py | 8 ++++++++ 2 files changed, 17 insertions(+) diff --git a/source/blender/python/generic/idprop_py_api.cc b/source/blender/python/generic/idprop_py_api.cc index 52678016496..7913f36c8cd 100644 --- a/source/blender/python/generic/idprop_py_api.cc +++ b/source/blender/python/generic/idprop_py_api.cc @@ -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; diff --git a/tests/python/bl_pyapi_idprop.py b/tests/python/bl_pyapi_idprop.py index b6d876cc883..b281c733a5e 100644 --- a/tests/python/bl_pyapi_idprop.py +++ b/tests/python/bl_pyapi_idprop.py @@ -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.