bindings: python: examples: close chip objects where needed
authorBartosz Golaszewski <bartekgola@gmail.com>
Fri, 27 Jul 2018 11:52:18 +0000 (13:52 +0200)
committerBartosz Golaszewski <bartekgola@gmail.com>
Tue, 31 Jul 2018 12:30:46 +0000 (14:30 +0200)
Unlike the C++ bindings: we can't expect the python runtime to call
the Chip object's destructor when all references are dropped. We need
to close each chip explicitly. Either call the close() method directly
or use controlled execution in all examples.

Signed-off-by: Bartosz Golaszewski <bartekgola@gmail.com>
bindings/python/examples/gpiodetect.py
bindings/python/examples/gpiofind.py
bindings/python/examples/gpioget.py
bindings/python/examples/gpioinfo.py
bindings/python/examples/gpiomon.py
bindings/python/examples/gpioset.py

index c13d4b352d0e1eb34ae3e4ebdb518e4fe168de34..f539f04ccd0b6d92b97296412cd11e95b3f7b5db 100755 (executable)
@@ -15,3 +15,4 @@ for chip in gpiod.ChipIter():
     print('{} [{}] ({} lines)'.format(chip.name(),
                                       chip.label(),
                                       chip.num_lines()))
+    chip.close()
index 3e50bdcf2e9e960588f660055daa0bf5501553f6..77363f78efee05c9b481c1993e4c0e950968a164 100755 (executable)
@@ -14,3 +14,4 @@ import sys
 
 line = gpiod.find_line(sys.argv[1])
 print('{} {}'.format(line.owner().name(), line.offset()))
+line.owner().close()
index 587db06c07b9922b4bf95203453134a874e169d2..bc7645b46391ed0e60bf31febbb87e3782ef3464 100755 (executable)
@@ -15,16 +15,15 @@ import sys
 if len(sys.argv) < 3:
     raise TypeError('usage: gpioget.py <gpiochip> <offset1> <offset2> ...')
 
-chip = gpiod.Chip(sys.argv[1])
-
-offsets = []
-for off in sys.argv[2:]:
-    offsets.append(int(off))
-
-lines = chip.get_lines(offsets)
-lines.request(consumer=sys.argv[0], type=gpiod.LINE_REQ_DIR_IN)
-vals = lines.get_values()
-
-for val in vals:
-    print(val, end=' ')
-print()
+with gpiod.Chip(sys.argv[1]) as chip:
+    offsets = []
+    for off in sys.argv[2:]:
+        offsets.append(int(off))
+
+    lines = chip.get_lines(offsets)
+    lines.request(consumer=sys.argv[0], type=gpiod.LINE_REQ_DIR_IN)
+    vals = lines.get_values()
+
+    for val in vals:
+        print(val, end=' ')
+    print()
index 4612d5b2f42b7802321397ab30e9254077441747..f9d91bfefff111ecb45920043c66ba55ef1d8d73 100755 (executable)
@@ -27,3 +27,5 @@ for chip in gpiod.ChipIter():
                 'unused' if consumer is None else consumer,
                 'input' if direction == gpiod.Line.DIRECTION_INPUT else 'output',
                 'active-low' if active_state == gpiod.Line.ACTIVE_LOW else 'active-high'))
+
+    chip.close()
index 35e43109ae7f24909d9ae80ab7d5681847157fa2..9cb71daa9720a69c8daa056326347907a55f8c12 100755 (executable)
@@ -25,21 +25,20 @@ def print_event(event):
 if len(sys.argv) < 3:
     raise TypeError('usage: gpiomon.py <gpiochip> <offset1> <offset2> ...')
 
-chip = gpiod.Chip(sys.argv[1])
-
-offsets = []
-for off in sys.argv[2:]:
-    offsets.append(int(off))
-
-lines = chip.get_lines(offsets)
-lines.request(consumer=sys.argv[0], type=gpiod.LINE_REQ_EV_BOTH_EDGES)
-
-try:
-    while True:
-        ev_lines = lines.event_wait(sec=1)
-        if ev_lines:
-            for line in ev_lines:
-                event = line.event_read()
-                print_event(event)
-except KeyboardInterrupt:
-    sys.exit(130)
+with gpiod.Chip(sys.argv[1]) as chip:
+    offsets = []
+    for off in sys.argv[2:]:
+        offsets.append(int(off))
+
+    lines = chip.get_lines(offsets)
+    lines.request(consumer=sys.argv[0], type=gpiod.LINE_REQ_EV_BOTH_EDGES)
+
+    try:
+        while True:
+            ev_lines = lines.event_wait(sec=1)
+            if ev_lines:
+                for line in ev_lines:
+                    event = line.event_read()
+                    print_event(event)
+    except KeyboardInterrupt:
+        sys.exit(130)
index 9e76b986e043d0070a47238c8b3e7f328059ff3a..70fbcc98be4aaaabd85c074afc4ccc7e8c5ad91e 100755 (executable)
@@ -15,15 +15,14 @@ import sys
 if len(sys.argv) < 3:
     raise TypeError('usage: gpioset.py <gpiochip> <offset1>=<value1> ...')
 
-chip = gpiod.Chip(sys.argv[1])
+with gpiod.Chip(sys.argv[1]) as chip:
+    offsets = []
+    values = []
+    for arg in sys.argv[2:]:
+        arg = arg.split('=')
+        offsets.append(int(arg[0]))
+        values.append(int(arg[1]))
 
-offsets = []
-values = []
-for arg in sys.argv[2:]:
-    arg = arg.split('=')
-    offsets.append(int(arg[0]))
-    values.append(int(arg[1]))
-
-lines = chip.get_lines(offsets)
-lines.request(consumer=sys.argv[0], type=gpiod.LINE_REQ_DIR_OUT)
-vals = lines.set_values(values)
+    lines = chip.get_lines(offsets)
+    lines.request(consumer=sys.argv[0], type=gpiod.LINE_REQ_DIR_OUT)
+    vals = lines.set_values(values)