Skip to content

Commit ac6e3ef

Browse files
authored
Add a default fileno function to the BusABC class. (#877)
Modify Notifier to handle the exception raised if the bus doesn't override it.
1 parent 2fa99c3 commit ac6e3ef

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

can/bus.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ def send_periodic(
199199
Disable to instead manage tasks manually.
200200
:return:
201201
A started task instance. Note the task can be stopped (and depending on
202-
the backend modified) by calling the :meth:`stop` method.
202+
the backend modified) by calling the task's :meth:`stop` method.
203203
204204
.. note::
205205
@@ -430,3 +430,6 @@ def _detect_available_configs() -> List[can.typechecking.AutoDetectedConfig]:
430430
for usage in the interface's bus constructor.
431431
"""
432432
raise NotImplementedError()
433+
434+
def fileno(self) -> int:
435+
raise NotImplementedError("fileno is not implemented using current CAN bus")

can/notifier.py

+14-11
Original file line numberDiff line numberDiff line change
@@ -61,23 +61,26 @@ def add_bus(self, bus: BusABC):
6161
:param bus:
6262
CAN bus instance.
6363
"""
64-
if (
65-
self._loop is not None
66-
and hasattr(bus, "fileno")
67-
and bus.fileno() >= 0 # type: ignore
68-
):
69-
# Use file descriptor to watch for messages
70-
reader = bus.fileno() # type: ignore
64+
reader: int = -1
65+
try:
66+
reader = bus.fileno()
67+
except NotImplementedError:
68+
# Bus doesn't support fileno, we fall back to thread based reader
69+
pass
70+
71+
if self._loop is not None and reader >= 0:
72+
# Use bus file descriptor to watch for messages
7173
self._loop.add_reader(reader, self._on_message_available, bus)
74+
self._readers.append(reader)
7275
else:
73-
reader = threading.Thread(
76+
reader_thread = threading.Thread(
7477
target=self._rx_thread,
7578
args=(bus,),
7679
name='can.notifier for bus "{}"'.format(bus.channel_info),
7780
)
78-
reader.daemon = True
79-
reader.start()
80-
self._readers.append(reader)
81+
reader_thread.daemon = True
82+
reader_thread.start()
83+
self._readers.append(reader_thread)
8184

8285
def stop(self, timeout: float = 5):
8386
"""Stop notifying Listeners when new :class:`~can.Message` objects arrive

0 commit comments

Comments
 (0)