Skip to content

Commit

Permalink
Invert default value logic for BusABC._is_shutdown. (#1774)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
acolomb committed Apr 29, 2024
1 parent ed98a0f commit 32c7640
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion can/bus.py
Expand Up @@ -65,7 +65,8 @@ class BusABC(metaclass=ABCMeta):
#: Log level for received messages
RECV_LOGGING_LEVEL = 9

_is_shutdown: bool = False
#: Assume that no cleanup is needed until something was initialized
_is_shutdown: bool = True
_can_protocol: CanProtocol = CanProtocol.CAN_20

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

def __str__(self) -> str:
return self.channel_info
Expand Down

0 comments on commit 32c7640

Please sign in to comment.