Skip to content

Commit 32c7640

Browse files
authored
Invert default value logic for BusABC._is_shutdown. (#1774)
The destructor of BusABC gives a warning when shutdown() was not previously called on the object after construction. However, if the construction fails (e.g. when the derived bus class constructor raises an exception), there is no way to call shutdown() on the unfinished object, and it is not necessary either. Initialize the _is_shutdown flag to False initially and flip it to True only when the parent class constructor runs, which usually happens last in derived classes. That avoids the shutdown warning for objects that failed to initialize at all.
1 parent ed98a0f commit 32c7640

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

can/bus.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ class BusABC(metaclass=ABCMeta):
6565
#: Log level for received messages
6666
RECV_LOGGING_LEVEL = 9
6767

68-
_is_shutdown: bool = False
68+
#: Assume that no cleanup is needed until something was initialized
69+
_is_shutdown: bool = True
6970
_can_protocol: CanProtocol = CanProtocol.CAN_20
7071

7172
@abstractmethod
@@ -97,6 +98,10 @@ def __init__(
9798
"""
9899
self._periodic_tasks: List[_SelfRemovingCyclicTask] = []
99100
self.set_filters(can_filters)
101+
# Flip the class default value when the constructor finishes. That
102+
# usually means the derived class constructor was also successful,
103+
# since it calls this parent constructor last.
104+
self._is_shutdown: bool = False
100105

101106
def __str__(self) -> str:
102107
return self.channel_info

0 commit comments

Comments
 (0)