Skip to content

Commit

Permalink
Merge branch 'lurch-close_exception_safe'
Browse files Browse the repository at this point in the history
  • Loading branch information
waveform80 committed Feb 19, 2018
2 parents 58eda1e + 73b31eb commit a4db920
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 44 deletions.
18 changes: 10 additions & 8 deletions gpiozero/boards.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,6 @@ class LEDCollection(CompositeOutputDevice):
:class:`LEDBoard` and :class:`LEDBarGraph`.
"""
def __init__(self, *args, **kwargs):
self._blink_thread = None
pwm = kwargs.pop('pwm', False)
active_high = kwargs.pop('active_high', True)
initial_value = kwargs.pop('initial_value', False)
Expand Down Expand Up @@ -313,12 +312,16 @@ class LEDBoard(LEDCollection):
create trees of LEDs.
"""
def __init__(self, *args, **kwargs):
self._blink_thread = None
self._blink_leds = []
self._blink_lock = Lock()
super(LEDBoard, self).__init__(*args, **kwargs)

def close(self):
self._stop_blink()
try:
self._stop_blink()
except AttributeError:
pass
super(LEDBoard, self).close()

def on(self, *args):
Expand Down Expand Up @@ -1373,10 +1376,10 @@ def __init__(self, pin_factory=None):
)

def close(self):
if self._lock:
if getattr(self, '_lock', None):
with self._lock:
super(_EnergenieMaster, self).close()
self._lock = None
self._lock = None

@classmethod
def _shared_key(cls, pin_factory):
Expand Down Expand Up @@ -1442,10 +1445,9 @@ def __init__(self, socket=None, initial_value=False, pin_factory=None):
self.off()

def close(self):
if self._master:
m = self._master
self._master = None
m.close()
if getattr(self, '_master', None):
self._master.close()
self._master = None

@property
def closed(self):
Expand Down
9 changes: 5 additions & 4 deletions gpiozero/devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,9 +328,10 @@ def all(self):
return self._all

def close(self):
if self._all:
if getattr(self, '_all', None):
for device in self._all:
device.close()
if isinstance(device, Device):
device.close()
self._all = ()

@property
Expand Down Expand Up @@ -388,10 +389,10 @@ def _read(self):

def close(self):
super(GPIODevice, self).close()
if self._pin is not None:
if getattr(self, '_pin', None) is not None:
self.pin_factory.release_pins(self, self._pin.number)
self._pin.close()
self._pin = None
self._pin = None

@property
def closed(self):
Expand Down
14 changes: 6 additions & 8 deletions gpiozero/input_devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,16 +162,15 @@ def close(self):
try:
self._queue.stop()
except AttributeError:
# If the queue isn't initialized (it's None) ignore the error
# because we're trying to close anyway
if self._queue is not None:
# If the queue isn't initialized (it's None), or _queue hasn't been
# set ignore the error because we're trying to close anyway
if getattr(self, '_queue', None) is not None:
raise
except RuntimeError:
# Cannot join thread before it starts; we don't care about this
# because we're trying to close the thread anyway
pass
else:
self._queue = None
self._queue = None
super(SmoothedInputDevice, self).close()

def __repr__(self):
Expand Down Expand Up @@ -662,10 +661,9 @@ def close(self):
try:
self._trigger.close()
except AttributeError:
if self._trigger is not None:
if getattr(self, '_trigger', None) is not None:
raise
else:
self._trigger = None
self._trigger = None
super(DistanceSensor, self).close()

@property
Expand Down
8 changes: 4 additions & 4 deletions gpiozero/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,9 @@ def source(self):

@source.setter
def source(self, value):
if self._source_thread is not None:
if getattr(self, '_source_thread', None):
self._source_thread.stop()
self._source_thread = None
self._source_thread = None
self._source = value
if value is not None:
self._source_thread = GPIOThread(target=self._copy_values, args=(value,))
Expand Down Expand Up @@ -343,9 +343,9 @@ def __init__(self, *args, **kwargs):
self._hold_thread = HoldThread(self)

def close(self):
if self._hold_thread:
if getattr(self, '_hold_thread', None):
self._hold_thread.stop()
self._hold_thread = None
self._hold_thread = None
try:
super(HoldMixin, self).close()
except AttributeError:
Expand Down
17 changes: 10 additions & 7 deletions gpiozero/output_devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,12 +191,12 @@ def blink(self, on_time=1, off_time=1, n=None, background=True):
self._blink_thread = None

def _stop_blink(self):
if self._controller:
if getattr(self, '_controller', None):
self._controller._stop_blink(self)
self._controller = None
if self._blink_thread:
self._controller = None
if getattr(self, '_blink_thread', None):
self._blink_thread.stop()
self._blink_thread = None
self._blink_thread = None

def _blink_device(self, on_time, off_time, n):
iterable = repeat(0) if n is None else repeat(0, n)
Expand Down Expand Up @@ -335,7 +335,10 @@ def __init__(
raise

def close(self):
self._stop_blink()
try:
self._stop_blink()
except AttributeError:
pass
try:
self.pin.frequency = None
except AttributeError:
Expand Down Expand Up @@ -607,11 +610,11 @@ def __init__(
blue = _led_property(2)

def close(self):
if self._leds:
if getattr(self, '_leds', None):
self._stop_blink()
for led in self._leds:
led.close()
self._leds = ()
self._leds = ()
super(RGBLED, self).close()

@property
Expand Down
13 changes: 5 additions & 8 deletions gpiozero/pins/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,9 @@ def __init__(self, factory, port, device):
self._interface.max_speed_hz = 500000

def close(self):
if self._interface:
try:
self._interface.close()
finally:
self._interface = None
if getattr(self, '_interface', None):
self._interface.close()
self._interface = None
self.pin_factory.release_all(self)
super(LocalPiHardwareSPI, self).close()

Expand Down Expand Up @@ -169,9 +167,9 @@ def _conflicts_with(self, other):
)

def close(self):
if self._bus:
if getattr(self, '_bus', None):
self._bus.close()
self._bus = None
self._bus = None
super(LocalPiSoftwareSPI, self).close()

@property
Expand Down Expand Up @@ -242,4 +240,3 @@ class LocalPiSoftwareSPIShared(SharedMixin, LocalPiSoftwareSPI):
@classmethod
def _shared_key(cls, factory, clock_pin, mosi_pin, miso_pin, select_pin):
return (select_pin,)

6 changes: 2 additions & 4 deletions gpiozero/pins/spi.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def __init__(self, clock_pin, mosi_pin, miso_pin):

def close(self):
super(SPISoftwareBus, self).close()
if self.lock:
if getattr(self, 'lock', None):
with self.lock:
if self.miso is not None:
self.miso.close()
Expand All @@ -46,7 +46,7 @@ def close(self):
if self.clock is not None:
self.clock.close()
self.clock = None
self.lock = None
self.lock = None

@property
def closed(self):
Expand Down Expand Up @@ -94,5 +94,3 @@ def transfer(self, data, clock_phase=False, lsb_first=False, bits_per_word=8):
mask = shift(mask, 1)
result.append(read_word)
return result


2 changes: 1 addition & 1 deletion gpiozero/spi_devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def __init__(self, **spi_args):
self._spi = self.pin_factory.spi(**spi_args)

def close(self):
if self._spi:
if getattr(self, '_spi', None):
self._spi.close()
self._spi = None
super(SPIDevice, self).close()
Expand Down

0 comments on commit a4db920

Please sign in to comment.