From 2a3f90c09a4be82f3f772e59884810e3b97159a5 Mon Sep 17 00:00:00 2001 From: JR Date: Sun, 29 Nov 2026 20:35:37 +0100 Subject: [PATCH 1/6] etas: keep the filters passed to the constructor EtasBus installed can_filters, then called BusABC.__init__ without them, and its set_filters(None) replaced them with an accept-all filter. Pass can_filters to BusABC.__init__, which installs them once. --- can/interfaces/etas/__init__.py | 3 +- .../+etas-constructor-filters.fixed.rst | 1 + test/test_etas.py | 113 ++++++++++++++++++ 3 files changed, 115 insertions(+), 2 deletions(-) create mode 100644 doc/changelog.d/+etas-constructor-filters.fixed.rst create mode 100644 test/test_etas.py diff --git a/can/interfaces/etas/__init__.py b/can/interfaces/etas/__init__.py index f8364a3fd..a73fd9a63 100644 --- a/can/interfaces/etas/__init__.py +++ b/can/interfaces/etas/__init__.py @@ -92,7 +92,6 @@ def __init__( ) self._oci_filters = None - self.filters = can_filters # TX @@ -118,7 +117,7 @@ def __init__( self.channel_info = channel # Super call must be after child init since super calls set_filters - super().__init__(channel=channel, **kwargs) + super().__init__(channel=channel, can_filters=can_filters, **kwargs) def _recv_internal(self, timeout: float | None) -> tuple[can.Message | None, bool]: ociMsgs = (ctypes.POINTER(OCI_CANMessageEx) * 1)() diff --git a/doc/changelog.d/+etas-constructor-filters.fixed.rst b/doc/changelog.d/+etas-constructor-filters.fixed.rst new file mode 100644 index 000000000..f9f96a4c4 --- /dev/null +++ b/doc/changelog.d/+etas-constructor-filters.fixed.rst @@ -0,0 +1 @@ +Fixed ``EtasBus`` discarding the ``can_filters`` passed to its constructor: the bus installed them and then replaced them with an accept-all filter, so it received every frame. diff --git a/test/test_etas.py b/test/test_etas.py new file mode 100644 index 000000000..4c2332526 --- /dev/null +++ b/test/test_etas.py @@ -0,0 +1,113 @@ +""" +Tests for the ETAS BOA interface, with the BOA libraries replaced by stubs. +""" + +import ctypes +import importlib +import sys +import unittest +from unittest.mock import Mock, patch + +import can + + +class _FakeLibrary: + """Every BOA function succeeds; the frame filter calls are recorded.""" + + def __init__(self, installed: list[tuple[int, int, int, int]]) -> None: + self._installed = installed + + def __getattr__(self, name: str) -> Mock: + if name == "OCI_AddCANFrameFilterEx": + function = Mock(side_effect=self._add) + elif name == "OCI_RemoveCANFrameFilterEx": + function = Mock(side_effect=self._remove) + elif name == "OCI_GetTimerCapabilities": + function = Mock(side_effect=self._timer_capabilities) + else: + function = Mock(return_value=0) + setattr(self, name, function) + return function + + @staticmethod + def _filters(filter_list, count: int) -> list[tuple[int, int, int, int]]: + return [ + ( + filter_list[i].contents.frameIDValue, + filter_list[i].contents.frameIDMask, + filter_list[i].contents.flagsValue, + filter_list[i].contents.flagsMask, + ) + for i in range(count) + ] + + @staticmethod + def _timer_capabilities(_controller, capabilities) -> int: + capabilities._obj.tickFrequency = 1_000_000 + return 0 + + def _add(self, _queue, filter_list, count: int) -> int: + self._installed.extend(self._filters(filter_list, count)) + return 0 + + def _remove(self, _queue, filter_list, count: int) -> int: + for entry in self._filters(filter_list, count): + self._installed.remove(entry) + return 0 + + +class EtasBusTest(unittest.TestCase): + def setUp(self) -> None: + self.installed: list[tuple[int, int, int, int]] = [] + library = _FakeLibrary(self.installed) + windll = Mock() + windll.LoadLibrary.return_value = library + + modules = patch.dict(sys.modules) + modules.start() + self.addCleanup(modules.stop) + for name in ("can.interfaces.etas", "can.interfaces.etas.boa"): + sys.modules.pop(name, None) + windll_patch = patch.object(ctypes, "windll", windll, create=True) + windll_patch.start() + self.addCleanup(windll_patch.stop) + self.etas = importlib.import_module("can.interfaces.etas") + + def _bus(self, **kwargs) -> can.BusABC: + bus = can.Bus(interface="etas", channel="ETAS://USB/ES581.4:1/CAN:1", **kwargs) + self.assertIsInstance(bus, self.etas.EtasBus) + self.addCleanup(bus.shutdown) + return bus + + def test_constructor_filters_are_installed(self): + selfreception = self.etas.OCI_CAN_MSG_FLAG_SELFRECEPTION + extended = self.etas.OCI_CAN_MSG_FLAG_EXTENDED + self._bus( + can_filters=[ + {"can_id": 0x123, "can_mask": 0x7FF, "extended": False}, + {"can_id": 0x18FF0000, "can_mask": 0x1FFF0000, "extended": True}, + ] + ) + self.assertEqual( + self.installed, + [ + (0x123, 0x7FF, 0, selfreception), + (0x18FF0000, 0x1FFF0000, extended, selfreception | extended), + ], + ) + + def test_no_constructor_filters_accepts_all(self): + self._bus(receive_own_messages=True) + self.assertEqual(self.installed, [(0, 0, 0, 0)]) + + def test_set_filters_replaces_constructor_filters(self): + bus = self._bus(can_filters=[{"can_id": 0x123, "can_mask": 0x7FF}]) + bus.set_filters([{"can_id": 0x456, "can_mask": 0x7FF}]) + self.assertEqual( + self.installed, + [(0x456, 0x7FF, 0, self.etas.OCI_CAN_MSG_FLAG_SELFRECEPTION)], + ) + + +if __name__ == "__main__": + unittest.main() From 40a7d093c3dd858aa21f8c0bf59f435a89c9dc3b Mon Sep 17 00:00:00 2001 From: JR Date: Sun, 29 Nov 2026 20:44:54 +0100 Subject: [PATCH 2/6] etas: describe the constructor filter fix precisely in the news fragment --- doc/changelog.d/+etas-constructor-filters.fixed.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/changelog.d/+etas-constructor-filters.fixed.rst b/doc/changelog.d/+etas-constructor-filters.fixed.rst index f9f96a4c4..61494dbcc 100644 --- a/doc/changelog.d/+etas-constructor-filters.fixed.rst +++ b/doc/changelog.d/+etas-constructor-filters.fixed.rst @@ -1 +1 @@ -Fixed ``EtasBus`` discarding the ``can_filters`` passed to its constructor: the bus installed them and then replaced them with an accept-all filter, so it received every frame. +Fixed ``EtasBus`` ignoring the ``can_filters`` passed to its constructor: the base class then installed an accept-all filter next to them, so frames with any identifier were received. From 8f1a4c688352d746774504215124ba38ecd239d3 Mon Sep 17 00:00:00 2001 From: JR Date: Sun, 29 Nov 2026 20:48:14 +0100 Subject: [PATCH 3/6] etas: news fragment wording --- doc/changelog.d/+etas-constructor-filters.fixed.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/changelog.d/+etas-constructor-filters.fixed.rst b/doc/changelog.d/+etas-constructor-filters.fixed.rst index 61494dbcc..0e79960d1 100644 --- a/doc/changelog.d/+etas-constructor-filters.fixed.rst +++ b/doc/changelog.d/+etas-constructor-filters.fixed.rst @@ -1 +1 @@ -Fixed ``EtasBus`` ignoring the ``can_filters`` passed to its constructor: the base class then installed an accept-all filter next to them, so frames with any identifier were received. +Fixed ``EtasBus`` ignoring the ``can_filters`` passed to its constructor: the base class then installed an accept-all filter, so frames with any identifier were received. From 7c60244a3fec1dbd198394a35bf87cc84ba108ff Mon Sep 17 00:00:00 2001 From: JR Date: Sun, 29 Nov 2026 20:56:11 +0100 Subject: [PATCH 4/6] Do not pin the extended=False flag mask in the ETAS test; reword fragment --- doc/changelog.d/+etas-constructor-filters.fixed.rst | 2 +- test/test_etas.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/changelog.d/+etas-constructor-filters.fixed.rst b/doc/changelog.d/+etas-constructor-filters.fixed.rst index 0e79960d1..cfd66d53c 100644 --- a/doc/changelog.d/+etas-constructor-filters.fixed.rst +++ b/doc/changelog.d/+etas-constructor-filters.fixed.rst @@ -1 +1 @@ -Fixed ``EtasBus`` ignoring the ``can_filters`` passed to its constructor: the base class then installed an accept-all filter, so frames with any identifier were received. +Fixed ``EtasBus`` ignoring the ``can_filters`` passed to its constructor: the base class then replaced them with an accept-all filter. diff --git a/test/test_etas.py b/test/test_etas.py index 4c2332526..92083fcaf 100644 --- a/test/test_etas.py +++ b/test/test_etas.py @@ -84,7 +84,7 @@ def test_constructor_filters_are_installed(self): extended = self.etas.OCI_CAN_MSG_FLAG_EXTENDED self._bus( can_filters=[ - {"can_id": 0x123, "can_mask": 0x7FF, "extended": False}, + {"can_id": 0x123, "can_mask": 0x7FF}, {"can_id": 0x18FF0000, "can_mask": 0x1FFF0000, "extended": True}, ] ) From 97ca584092978265d924dded9e80d5556cf298ad Mon Sep 17 00:00:00 2001 From: JR Date: Sun, 29 Nov 2026 21:00:37 +0100 Subject: [PATCH 5/6] Keep the ETAS tests independent of local can configuration --- test/test_etas.py | 1 + 1 file changed, 1 insertion(+) diff --git a/test/test_etas.py b/test/test_etas.py index 92083fcaf..357b543d1 100644 --- a/test/test_etas.py +++ b/test/test_etas.py @@ -74,6 +74,7 @@ def setUp(self) -> None: self.etas = importlib.import_module("can.interfaces.etas") def _bus(self, **kwargs) -> can.BusABC: + kwargs.setdefault("receive_own_messages", False) bus = can.Bus(interface="etas", channel="ETAS://USB/ES581.4:1/CAN:1", **kwargs) self.assertIsInstance(bus, self.etas.EtasBus) self.addCleanup(bus.shutdown) From 7aebc0b2550d4596a85f71eba6594ca37cc357e3 Mon Sep 17 00:00:00 2001 From: JR Date: Sun, 29 Nov 2026 21:04:37 +0100 Subject: [PATCH 6/6] Ignore local can configuration in the ETAS tests; reword fragment --- doc/changelog.d/+etas-constructor-filters.fixed.rst | 2 +- test/test_etas.py | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/doc/changelog.d/+etas-constructor-filters.fixed.rst b/doc/changelog.d/+etas-constructor-filters.fixed.rst index cfd66d53c..31860a6b8 100644 --- a/doc/changelog.d/+etas-constructor-filters.fixed.rst +++ b/doc/changelog.d/+etas-constructor-filters.fixed.rst @@ -1 +1 @@ -Fixed ``EtasBus`` ignoring the ``can_filters`` passed to its constructor: the base class then replaced them with an accept-all filter. +Fixed ``EtasBus`` ignoring the ``can_filters`` passed to its constructor: the base class then added an accept-all filter that overrode them. diff --git a/test/test_etas.py b/test/test_etas.py index 357b543d1..b44802381 100644 --- a/test/test_etas.py +++ b/test/test_etas.py @@ -74,8 +74,12 @@ def setUp(self) -> None: self.etas = importlib.import_module("can.interfaces.etas") def _bus(self, **kwargs) -> can.BusABC: - kwargs.setdefault("receive_own_messages", False) - bus = can.Bus(interface="etas", channel="ETAS://USB/ES581.4:1/CAN:1", **kwargs) + bus = can.Bus( + interface="etas", + channel="ETAS://USB/ES581.4:1/CAN:1", + ignore_config=True, + **kwargs, + ) self.assertIsInstance(bus, self.etas.EtasBus) self.addCleanup(bus.shutdown) return bus