Skip to content

Commit 886313e

Browse files
jkhsjdhjss-heppner
authored andcommitted
model.base: fix ConstrainedList.clear() atomicity
The default inherited `clear()` implementation repeatedly deletes the last item of the list until the list is empty. If the last item can be deleted successfully, but an item in front of it that will be deleted later cannot, this makes `clear()` non-atomic. Thus, the `clear()` method is now overriden in an atomic way. Furthermore, the ConstrainedList atomicity test is fixed to correctly test for this as well.
1 parent 5eb895e commit 886313e

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

basyx/aas/model/base.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1311,6 +1311,10 @@ def extend(self, values: Iterable[_T]) -> None:
13111311
self._item_add_hook(v, self._list + v_list[:idx])
13121312
self._list = self._list + v_list
13131313

1314+
def clear(self) -> None:
1315+
# clear() repeatedly deletes the last item by default, making it not atomic
1316+
del self[:]
1317+
13141318
@overload
13151319
def __getitem__(self, index: int) -> _T: ...
13161320

test/model/test_base.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1213,7 +1213,15 @@ def hook(itm: int, _list: List[int]) -> None:
12131213
self.assertEqual(c_list, [1, 2, 3])
12141214
with self.assertRaises(ValueError):
12151215
c_list.clear()
1216-
self.assertEqual(c_list, [1, 2, 3])
1216+
# the default clear() implementation seems to repeatedly delete the last item until the list is empty
1217+
# in this case, the last item is 3, which cannot be deleted because it is > 2, thus leaving it unclear whether
1218+
# clear() really is atomic. to work around this, the list is reversed, making 1 the last item, and attempting
1219+
# to clear again.
1220+
c_list.reverse()
1221+
with self.assertRaises(ValueError):
1222+
c_list.clear()
1223+
self.assertEqual(c_list, [3, 2, 1])
1224+
c_list.reverse()
12171225
del c_list[0:2]
12181226
self.assertEqual(c_list, [3])
12191227

0 commit comments

Comments
 (0)