Skip to content

Commit ec06452

Browse files
authored
Fix fileno error on Windows (Serial bus) (#1333)
* Add conda-forge badge to readme * Update fileno method in serial_can.py Change implemented similar to that made in #1313. This means notifiers will now work with the Serial interface. * Format serial_test.py with black * Revert "Add conda-forge badge to readme" This reverts commit 59d0b3c.
1 parent cb3d4dc commit ec06452

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

can/interfaces/serial/serial_can.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
See the interface documentation for the format being used.
88
"""
99

10+
import io
1011
import logging
1112
import struct
1213
from typing import Any, List, Tuple, Optional
@@ -212,10 +213,14 @@ def _recv_internal(
212213
raise CanOperationError("could not read from serial") from error
213214

214215
def fileno(self) -> int:
215-
if hasattr(self._ser, "fileno"):
216+
try:
216217
return self._ser.fileno()
217-
# Return an invalid file descriptor on Windows
218-
return -1
218+
except io.UnsupportedOperation:
219+
raise NotImplementedError(
220+
"fileno is not implemented using current CAN bus on this platform"
221+
)
222+
except Exception as exception:
223+
raise CanOperationError("Cannot fetch fileno") from exception
219224

220225
@staticmethod
221226
def _detect_available_configs() -> List[AutoDetectedConfig]:

test/serial_test.py

+18
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,24 @@ def test_rx_tx_min_timestamp_error(self):
136136
msg = can.Message(timestamp=-1)
137137
self.assertRaises(ValueError, self.bus.send, msg)
138138

139+
def test_when_no_fileno(self):
140+
"""
141+
Tests for the fileno method catching the missing pyserial implementeation on the Windows platform
142+
"""
143+
try:
144+
fileno = self.bus.fileno()
145+
except NotImplementedError:
146+
pass # allow it to be left non-implemented for Windows platform
147+
else:
148+
fileno.__gt__ = (
149+
lambda self, compare: True
150+
) # Current platform implements fileno, so get the mock to respond to a greater than comparison
151+
self.assertIsNotNone(fileno)
152+
self.assertFalse(
153+
fileno == -1
154+
) # forcing the value to -1 is the old way of managing fileno on Windows but it is not compatible with notifiers
155+
self.assertTrue(fileno > 0)
156+
139157

140158
class SimpleSerialTest(unittest.TestCase, SimpleSerialTestBase):
141159
def __init__(self, *args, **kwargs):

0 commit comments

Comments
 (0)