From 4ef2b99d0e8eda4474ae21545abd51a6996f4857 Mon Sep 17 00:00:00 2001 From: Matthew Campbell Date: Fri, 20 Mar 2020 08:16:14 -0400 Subject: [PATCH] put exception gaurd on file opens --- gpio.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/gpio.py b/gpio.py index 62bc55c..effa24f 100644 --- a/gpio.py +++ b/gpio.py @@ -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