From 9476c62d61376b8ab4c04303929e443461162bf1 Mon Sep 17 00:00:00 2001 From: Kristoffel Pirard Date: Thu, 29 Aug 2024 14:40:22 +0200 Subject: [PATCH] prior: fix AttributeError for bad connections When constructing a ProScanIII object on a port that does not contain a prior controller, the _devices member is not defined. This causes the __del__ operation of the object to fail, because it will end up in the base class' __del__, which requires the `devices` property, which requires the `_devices` member to be present. Conceptually, the `abc.Controller` class requires the implementation to be a valid object. the `super().__init__` method may call any of the public base members, so we have to make sure they are valid before calling it. One may also argue that the object is not allowed to be constructed at all if not connected to a proper device; that would be an alternative fix. --- microscope/controllers/prior.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/microscope/controllers/prior.py b/microscope/controllers/prior.py index 28ab71d5..7beedfa4 100644 --- a/microscope/controllers/prior.py +++ b/microscope/controllers/prior.py @@ -211,16 +211,18 @@ class ProScanIII(microscope.abc.Controller): def __init__( self, port: str, baudrate: int = 9600, timeout: float = 0.5, **kwargs ) -> None: - super().__init__(**kwargs) - self._conn = _ProScanIIIConnection(port, baudrate, timeout) self._devices: Mapping[str, microscope.abc.Device] = {} + self._conn = _ProScanIIIConnection(port, baudrate, timeout) + # Can have up to three filter wheels, numbered 1 to 3. for number in range(1, 4): if self._conn.has_filterwheel(number): key = "filter %d" % number self._devices[key] = _ProScanIIIFilterWheel(self._conn, number) + super().__init__(**kwargs) + @property def devices(self) -> Mapping[str, microscope.abc.Device]: return self._devices