Skip to content

Commit a4db920

Browse files
committed
Merge branch 'lurch-close_exception_safe'
2 parents 58eda1e + 73b31eb commit a4db920

File tree

8 files changed

+43
-44
lines changed

8 files changed

+43
-44
lines changed

gpiozero/boards.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,6 @@ class LEDCollection(CompositeOutputDevice):
215215
:class:`LEDBoard` and :class:`LEDBarGraph`.
216216
"""
217217
def __init__(self, *args, **kwargs):
218-
self._blink_thread = None
219218
pwm = kwargs.pop('pwm', False)
220219
active_high = kwargs.pop('active_high', True)
221220
initial_value = kwargs.pop('initial_value', False)
@@ -313,12 +312,16 @@ class LEDBoard(LEDCollection):
313312
create trees of LEDs.
314313
"""
315314
def __init__(self, *args, **kwargs):
315+
self._blink_thread = None
316316
self._blink_leds = []
317317
self._blink_lock = Lock()
318318
super(LEDBoard, self).__init__(*args, **kwargs)
319319

320320
def close(self):
321-
self._stop_blink()
321+
try:
322+
self._stop_blink()
323+
except AttributeError:
324+
pass
322325
super(LEDBoard, self).close()
323326

324327
def on(self, *args):
@@ -1373,10 +1376,10 @@ def __init__(self, pin_factory=None):
13731376
)
13741377

13751378
def close(self):
1376-
if self._lock:
1379+
if getattr(self, '_lock', None):
13771380
with self._lock:
13781381
super(_EnergenieMaster, self).close()
1379-
self._lock = None
1382+
self._lock = None
13801383

13811384
@classmethod
13821385
def _shared_key(cls, pin_factory):
@@ -1442,10 +1445,9 @@ def __init__(self, socket=None, initial_value=False, pin_factory=None):
14421445
self.off()
14431446

14441447
def close(self):
1445-
if self._master:
1446-
m = self._master
1447-
self._master = None
1448-
m.close()
1448+
if getattr(self, '_master', None):
1449+
self._master.close()
1450+
self._master = None
14491451

14501452
@property
14511453
def closed(self):

gpiozero/devices.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -328,9 +328,10 @@ def all(self):
328328
return self._all
329329

330330
def close(self):
331-
if self._all:
331+
if getattr(self, '_all', None):
332332
for device in self._all:
333-
device.close()
333+
if isinstance(device, Device):
334+
device.close()
334335
self._all = ()
335336

336337
@property
@@ -388,10 +389,10 @@ def _read(self):
388389

389390
def close(self):
390391
super(GPIODevice, self).close()
391-
if self._pin is not None:
392+
if getattr(self, '_pin', None) is not None:
392393
self.pin_factory.release_pins(self, self._pin.number)
393394
self._pin.close()
394-
self._pin = None
395+
self._pin = None
395396

396397
@property
397398
def closed(self):

gpiozero/input_devices.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -162,16 +162,15 @@ def close(self):
162162
try:
163163
self._queue.stop()
164164
except AttributeError:
165-
# If the queue isn't initialized (it's None) ignore the error
166-
# because we're trying to close anyway
167-
if self._queue is not None:
165+
# If the queue isn't initialized (it's None), or _queue hasn't been
166+
# set ignore the error because we're trying to close anyway
167+
if getattr(self, '_queue', None) is not None:
168168
raise
169169
except RuntimeError:
170170
# Cannot join thread before it starts; we don't care about this
171171
# because we're trying to close the thread anyway
172172
pass
173-
else:
174-
self._queue = None
173+
self._queue = None
175174
super(SmoothedInputDevice, self).close()
176175

177176
def __repr__(self):
@@ -662,10 +661,9 @@ def close(self):
662661
try:
663662
self._trigger.close()
664663
except AttributeError:
665-
if self._trigger is not None:
664+
if getattr(self, '_trigger', None) is not None:
666665
raise
667-
else:
668-
self._trigger = None
666+
self._trigger = None
669667
super(DistanceSensor, self).close()
670668

671669
@property

gpiozero/mixins.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,9 @@ def source(self):
108108

109109
@source.setter
110110
def source(self, value):
111-
if self._source_thread is not None:
111+
if getattr(self, '_source_thread', None):
112112
self._source_thread.stop()
113-
self._source_thread = None
113+
self._source_thread = None
114114
self._source = value
115115
if value is not None:
116116
self._source_thread = GPIOThread(target=self._copy_values, args=(value,))
@@ -343,9 +343,9 @@ def __init__(self, *args, **kwargs):
343343
self._hold_thread = HoldThread(self)
344344

345345
def close(self):
346-
if self._hold_thread:
346+
if getattr(self, '_hold_thread', None):
347347
self._hold_thread.stop()
348-
self._hold_thread = None
348+
self._hold_thread = None
349349
try:
350350
super(HoldMixin, self).close()
351351
except AttributeError:

gpiozero/output_devices.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -191,12 +191,12 @@ def blink(self, on_time=1, off_time=1, n=None, background=True):
191191
self._blink_thread = None
192192

193193
def _stop_blink(self):
194-
if self._controller:
194+
if getattr(self, '_controller', None):
195195
self._controller._stop_blink(self)
196-
self._controller = None
197-
if self._blink_thread:
196+
self._controller = None
197+
if getattr(self, '_blink_thread', None):
198198
self._blink_thread.stop()
199-
self._blink_thread = None
199+
self._blink_thread = None
200200

201201
def _blink_device(self, on_time, off_time, n):
202202
iterable = repeat(0) if n is None else repeat(0, n)
@@ -335,7 +335,10 @@ def __init__(
335335
raise
336336

337337
def close(self):
338-
self._stop_blink()
338+
try:
339+
self._stop_blink()
340+
except AttributeError:
341+
pass
339342
try:
340343
self.pin.frequency = None
341344
except AttributeError:
@@ -607,11 +610,11 @@ def __init__(
607610
blue = _led_property(2)
608611

609612
def close(self):
610-
if self._leds:
613+
if getattr(self, '_leds', None):
611614
self._stop_blink()
612615
for led in self._leds:
613616
led.close()
614-
self._leds = ()
617+
self._leds = ()
615618
super(RGBLED, self).close()
616619

617620
@property

gpiozero/pins/local.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,9 @@ def __init__(self, factory, port, device):
9696
self._interface.max_speed_hz = 500000
9797

9898
def close(self):
99-
if self._interface:
100-
try:
101-
self._interface.close()
102-
finally:
103-
self._interface = None
99+
if getattr(self, '_interface', None):
100+
self._interface.close()
101+
self._interface = None
104102
self.pin_factory.release_all(self)
105103
super(LocalPiHardwareSPI, self).close()
106104

@@ -169,9 +167,9 @@ def _conflicts_with(self, other):
169167
)
170168

171169
def close(self):
172-
if self._bus:
170+
if getattr(self, '_bus', None):
173171
self._bus.close()
174-
self._bus = None
172+
self._bus = None
175173
super(LocalPiSoftwareSPI, self).close()
176174

177175
@property
@@ -242,4 +240,3 @@ class LocalPiSoftwareSPIShared(SharedMixin, LocalPiSoftwareSPI):
242240
@classmethod
243241
def _shared_key(cls, factory, clock_pin, mosi_pin, miso_pin, select_pin):
244242
return (select_pin,)
245-

gpiozero/pins/spi.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def __init__(self, clock_pin, mosi_pin, miso_pin):
3535

3636
def close(self):
3737
super(SPISoftwareBus, self).close()
38-
if self.lock:
38+
if getattr(self, 'lock', None):
3939
with self.lock:
4040
if self.miso is not None:
4141
self.miso.close()
@@ -46,7 +46,7 @@ def close(self):
4646
if self.clock is not None:
4747
self.clock.close()
4848
self.clock = None
49-
self.lock = None
49+
self.lock = None
5050

5151
@property
5252
def closed(self):
@@ -94,5 +94,3 @@ def transfer(self, data, clock_phase=False, lsb_first=False, bits_per_word=8):
9494
mask = shift(mask, 1)
9595
result.append(read_word)
9696
return result
97-
98-

gpiozero/spi_devices.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def __init__(self, **spi_args):
3434
self._spi = self.pin_factory.spi(**spi_args)
3535

3636
def close(self):
37-
if self._spi:
37+
if getattr(self, '_spi', None):
3838
self._spi.close()
3939
self._spi = None
4040
super(SPIDevice, self).close()

0 commit comments

Comments
 (0)