* [libgpiod][PATCH v2] bindings: python: allow closing a Chip multiple times
@ 2026-04-03 15:46 Vincent Fazio
0 siblings, 0 replies; only message in thread
From: Vincent Fazio @ 2026-04-03 15:46 UTC (permalink / raw)
To: linux-gpio; +Cc: brgl, Vincent Fazio
In Python, it is common for objects to allow their `close` method to be
executed multiple times out of convenience. An example of this is the
`IOBase` class [0] which is the base class of most file-like classes.
Update `Chip.close` to follow this example.
[0]: https://docs.python.org/release/3.10.0/library/io.html#io.IOBase.close
Signed-off-by: Vincent Fazio <vfazio@gmail.com>
---
bindings/python/gpiod/chip.py | 6 +++---
bindings/python/tests/tests_chip.py | 8 --------
2 files changed, 3 insertions(+), 11 deletions(-)
diff --git a/bindings/python/gpiod/chip.py b/bindings/python/gpiod/chip.py
index 8113fa9..6b7b32b 100644
--- a/bindings/python/gpiod/chip.py
+++ b/bindings/python/gpiod/chip.py
@@ -99,9 +99,9 @@ class Chip:
Close the associated GPIO chip descriptor. The chip object must no
longer be used after this method is called.
"""
- self._check_closed()
- cast("_ext.Chip", self._chip).close()
- self._chip = None
+ if self._chip is not None:
+ self._chip.close()
+ self._chip = None
def get_info(self) -> ChipInfo:
"""
diff --git a/bindings/python/tests/tests_chip.py b/bindings/python/tests/tests_chip.py
index d5a64b3..9007885 100644
--- a/bindings/python/tests/tests_chip.py
+++ b/bindings/python/tests/tests_chip.py
@@ -184,14 +184,6 @@ class ClosedChipCannotBeUsed(TestCase):
with chip:
_ = chip.fd
- def test_close_chip_twice(self) -> None:
- sim = gpiosim.Chip(label="foobar")
- chip = gpiod.Chip(sim.dev_path)
- chip.close()
-
- with self.assertRaises(gpiod.ChipClosedError):
- chip.close()
-
class StringRepresentation(TestCase):
def setUp(self) -> None:
--
2.43.0
^ permalink raw reply related [flat|nested] only message in thread
only message in thread, other threads:[~2026-04-03 15:46 UTC | newest]
Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-04-03 15:46 [libgpiod][PATCH v2] bindings: python: allow closing a Chip multiple times Vincent Fazio
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox