From 3d45afbb1508cd21fcae1aa3a747d1f794ae4dc5 Mon Sep 17 00:00:00 2001 From: Svein Seldal Date: Sun, 15 Jun 2025 00:20:24 +0200 Subject: [PATCH] Fix __getitem__ from OD without using subobject's __len__ --- canopen/objectdictionary/__init__.py | 4 +++- test/test_od.py | 12 ++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/canopen/objectdictionary/__init__.py b/canopen/objectdictionary/__init__.py index 09fe6e03..b753d3aa 100644 --- a/canopen/objectdictionary/__init__.py +++ b/canopen/objectdictionary/__init__.py @@ -133,7 +133,9 @@ def __getitem__( self, index: Union[int, str] ) -> Union[ODArray, ODRecord, ODVariable]: """Get object from object dictionary by name or index.""" - item = self.names.get(index) or self.indices.get(index) + item = self.names.get(index) + if item is None: + item = self.indices.get(index) if item is None: if isinstance(index, str) and '.' in index: idx, sub = index.split('.', maxsplit=1) diff --git a/test/test_od.py b/test/test_od.py index 52de86f8..9ab0e187 100644 --- a/test/test_od.py +++ b/test/test_od.py @@ -249,6 +249,18 @@ def test_get_item_dot(self): self.assertEqual(test_od["Test Array.Test Variable"], member1) self.assertEqual(test_od["Test Array.Test Variable 2"], member2) + def test_get_item_index(self): + test_od = od.ObjectDictionary() + array = od.ODArray("Test Array", 0x1000) + test_od.add_object(array) + item = test_od[0x1000] + self.assertIsInstance(item, od.ODArray) + self.assertIs(item, array) + item = test_od["Test Array"] + self.assertIsInstance(item, od.ODArray) + self.assertIs(item, array) + + class TestArray(unittest.TestCase): def test_subindexes(self):