Skip to content

Commit

Permalink
put exception gaurd on file opens
Browse files Browse the repository at this point in the history
  • Loading branch information
mcampbellizo committed Mar 20, 2020
1 parent 26bcdc9 commit 4ef2b99
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions gpio.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,16 @@ def wrapped(pin, *args, **kwargs):
with _export_lock:
with open(pjoin(gpio_root, 'export'), 'w') as f:
_write(f, pin)
value = open(pjoin(ppath, 'value'), FMODE)
direction = open(pjoin(ppath, 'direction'), FMODE)
active_low = open(pjoin(ppath, 'active_low'), FMODE)
value, direction, active_low = None, None, None
try:
value = open(pjoin(ppath, 'value'), FMODE)
direction = open(pjoin(ppath, 'direction'), FMODE)
active_low = open(pjoin(ppath, 'active_low'), FMODE)
except Exception as e:
if value: value.close()
if direction: direction.close()
if active_low: active_low.close()
throw e
_open[pin] = PinState(value=value, direction=direction, active_low=active_low)
return function(pin, *args, **kwargs)
return wrapped
Expand Down

0 comments on commit 4ef2b99

Please sign in to comment.