From: Bartosz Golaszewski Date: Fri, 27 Jul 2018 11:52:18 +0000 (+0200) Subject: bindings: python: examples: close chip objects where needed X-Git-Url: http://git.maquefel.me/?a=commitdiff_plain;h=1d87088b1e4dfe0801845f5c1ce6c95ac8184ebc;p=qemu-gpiodev%2Flibgpiod.git bindings: python: examples: close chip objects where needed 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 --- diff --git a/bindings/python/examples/gpiodetect.py b/bindings/python/examples/gpiodetect.py index c13d4b3..f539f04 100755 --- a/bindings/python/examples/gpiodetect.py +++ b/bindings/python/examples/gpiodetect.py @@ -15,3 +15,4 @@ for chip in gpiod.ChipIter(): print('{} [{}] ({} lines)'.format(chip.name(), chip.label(), chip.num_lines())) + chip.close() diff --git a/bindings/python/examples/gpiofind.py b/bindings/python/examples/gpiofind.py index 3e50bdc..77363f7 100755 --- a/bindings/python/examples/gpiofind.py +++ b/bindings/python/examples/gpiofind.py @@ -14,3 +14,4 @@ import sys line = gpiod.find_line(sys.argv[1]) print('{} {}'.format(line.owner().name(), line.offset())) +line.owner().close() diff --git a/bindings/python/examples/gpioget.py b/bindings/python/examples/gpioget.py index 587db06..bc7645b 100755 --- a/bindings/python/examples/gpioget.py +++ b/bindings/python/examples/gpioget.py @@ -15,16 +15,15 @@ import sys if len(sys.argv) < 3: raise TypeError('usage: gpioget.py ...') -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() diff --git a/bindings/python/examples/gpioinfo.py b/bindings/python/examples/gpioinfo.py index 4612d5b..f9d91bf 100755 --- a/bindings/python/examples/gpioinfo.py +++ b/bindings/python/examples/gpioinfo.py @@ -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() diff --git a/bindings/python/examples/gpiomon.py b/bindings/python/examples/gpiomon.py index 35e4310..9cb71da 100755 --- a/bindings/python/examples/gpiomon.py +++ b/bindings/python/examples/gpiomon.py @@ -25,21 +25,20 @@ def print_event(event): if len(sys.argv) < 3: raise TypeError('usage: gpiomon.py ...') -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) diff --git a/bindings/python/examples/gpioset.py b/bindings/python/examples/gpioset.py index 9e76b98..70fbcc9 100755 --- a/bindings/python/examples/gpioset.py +++ b/bindings/python/examples/gpioset.py @@ -15,15 +15,14 @@ import sys if len(sys.argv) < 3: raise TypeError('usage: gpioset.py = ...') -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)