Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

model.base: fix ConstrainedList.clear() atomicity #153

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions basyx/aas/model/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1311,6 +1311,10 @@ def extend(self, values: Iterable[_T]) -> None:
self._item_add_hook(v, self._list + v_list[:idx])
self._list = self._list + v_list

def clear(self) -> None:
# clear() repeatedly deletes the last item by default, making it not atomic
del self[:]

@overload
def __getitem__(self, index: int) -> _T: ...

Expand Down
10 changes: 9 additions & 1 deletion test/model/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1213,7 +1213,15 @@ def hook(itm: int, _list: List[int]) -> None:
self.assertEqual(c_list, [1, 2, 3])
with self.assertRaises(ValueError):
c_list.clear()
self.assertEqual(c_list, [1, 2, 3])
# the default clear() implementation seems to repeatedly delete the last item until the list is empty
# in this case, the last item is 3, which cannot be deleted because it is > 2, thus leaving it unclear whether
# clear() really is atomic. to work around this, the list is reversed, making 1 the last item, and attempting
# to clear again.
c_list.reverse()
with self.assertRaises(ValueError):
c_list.clear()
self.assertEqual(c_list, [3, 2, 1])
c_list.reverse()
del c_list[0:2]
self.assertEqual(c_list, [3])

Expand Down
Loading