From 0c980a6949793a18d6fd6cbed5a50c6d0bc29c78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Colomb?= Date: Tue, 7 Apr 2026 11:04:03 +0200 Subject: [PATCH 1/2] Test for PDO access by nonexistant communication parameter index. Must raise a KeyError, but currently does not. --- test/test_pdo.py | 1 + 1 file changed, 1 insertion(+) diff --git a/test/test_pdo.py b/test/test_pdo.py index 4857ca7a..1b641147 100644 --- a/test/test_pdo.py +++ b/test/test_pdo.py @@ -80,6 +80,7 @@ def test_pdo_getitem(self): self.assertRaises(KeyError, lambda: node.pdo['DOES NOT EXIST']) self.assertRaises(KeyError, lambda: node.pdo[0x1BFF]) self.assertRaises(KeyError, lambda: node.tpdo[0x1BFF]) + self.assertRaises(KeyError, lambda: node.pdo[0x15FF]) def test_pdo_iterate(self): node = self.node From c90b878fcd51812619b1f3bd8e72e857f285c879 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Colomb?= Date: Tue, 7 Apr 2026 12:17:59 +0200 Subject: [PATCH 2/2] Skip the fallback lookup by parameter index without record offsets. Fixes the test failure introduced with previous commit. --- canopen/pdo/base.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/canopen/pdo/base.py b/canopen/pdo/base.py index 04f451d3..771ed5af 100644 --- a/canopen/pdo/base.py +++ b/canopen/pdo/base.py @@ -175,10 +175,12 @@ def __getitem__(self, key: int) -> PdoMap: try: return self.maps[key] except KeyError: - with contextlib.suppress(KeyError): - return self.maps[key + 1 - self.map_offset] - with contextlib.suppress(KeyError): - return self.maps[key + 1 - self.com_offset] + if self.map_offset: + with contextlib.suppress(KeyError): + return self.maps[key + 1 - self.map_offset] + if self.com_offset: + with contextlib.suppress(KeyError): + return self.maps[key + 1 - self.com_offset] raise def __iter__(self) -> Iterator[int]: