Skip to content

Commit

Permalink
prior: fix AttributeError for bad connections
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Kristoffel Pirard committed Aug 29, 2024
1 parent 2c282da commit 9476c62
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions microscope/controllers/prior.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 9476c62

Please sign in to comment.