15
15
from threading import RLock
16
16
from random import randint
17
17
18
- from can import CanError
18
+ from can import CanOperationError
19
19
from can .bus import BusABC
20
20
from can .message import Message
21
+ from can .typechecking import AutoDetectedConfig
21
22
22
23
logger = logging .getLogger (__name__ )
23
24
24
25
25
26
# Channels are lists of queues, one for each connection
26
27
if TYPE_CHECKING :
27
- # https://mypy.readthedocs.io/en/stable/common_issues .html#using-classes-that-are-generic-in-stubs-but-not-at-runtime
28
+ # https://mypy.readthedocs.io/en/stable/runtime_troubles .html#using-classes-that-are-generic-in-stubs-but-not-at-runtime
28
29
channels : Dict [Optional [Any ], List [queue .Queue [Message ]]] = {}
29
30
else :
30
31
channels = {}
@@ -58,7 +59,7 @@ def __init__(
58
59
channel : Any = None ,
59
60
receive_own_messages : bool = False ,
60
61
rx_queue_size : int = 0 ,
61
- ** kwargs : Any
62
+ ** kwargs : Any ,
62
63
) -> None :
63
64
super ().__init__ (
64
65
channel = channel , receive_own_messages = receive_own_messages , ** kwargs
@@ -81,12 +82,12 @@ def __init__(
81
82
self .channel .append (self .queue )
82
83
83
84
def _check_if_open (self ) -> None :
84
- """Raises CanError if the bus is not open.
85
+ """Raises :class:`~can.CanOperationError` if the bus is not open.
85
86
86
87
Has to be called in every method that accesses the bus.
87
88
"""
88
89
if not self ._open :
89
- raise CanError ( "Operation on closed bus" )
90
+ raise CanOperationError ( "Cannot operate on a closed bus" )
90
91
91
92
def _recv_internal (
92
93
self , timeout : Optional [float ]
@@ -116,8 +117,9 @@ def send(self, msg: Message, timeout: Optional[float] = None) -> None:
116
117
bus_queue .put (msg_copy , block = True , timeout = timeout )
117
118
except queue .Full :
118
119
all_sent = False
120
+
119
121
if not all_sent :
120
- raise CanError ("Could not send message to one or more recipients" )
122
+ raise CanOperationError ("Could not send message to one or more recipients" )
121
123
122
124
def shutdown (self ) -> None :
123
125
if self ._open :
@@ -131,7 +133,7 @@ def shutdown(self) -> None:
131
133
del channels [self .channel_id ]
132
134
133
135
@staticmethod
134
- def _detect_available_configs ():
136
+ def _detect_available_configs () -> List [ AutoDetectedConfig ] :
135
137
"""
136
138
Returns all currently used channels as well as
137
139
one other currently unused channel.
@@ -146,7 +148,9 @@ def _detect_available_configs():
146
148
available_channels = list (channels .keys ())
147
149
148
150
# find a currently unused channel
149
- get_extra = lambda : "channel-{}" .format (randint (0 , 9999 ))
151
+ def get_extra ():
152
+ return f"channel-{ randint (0 , 9999 )} "
153
+
150
154
extra = get_extra ()
151
155
while extra in available_channels :
152
156
extra = get_extra ()
0 commit comments