Skip to contentSkip to conversation

bindings: python: allow closing a Chip multiple times

PATCH Applied Vincent Fazio 2 participants 2 messages Apr 3 – Apr 7 GPIO

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 bindings: python: allow closing a Chip multiple times

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

---
 bindings/python/gpiod/chip.py       | 6 +++---
 bindings/python/tests/tests_chip.py | 8 --------
 2 files changed, 3 insertions(+), 11 deletions(-)
bindings/python/gpiod/chip.py +3 -3
index 8113fa9..6b7b32b 100644
@@ -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:
         """
bindings/python/tests/tests_chip.py +0 -8
index d5a64b3..9007885 100644
@@ -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:
Signature · 1 line
2.43.0
#1 lore raw
Quoted 9 lines · 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

[...]

Applied, thanks!

[1/1] bindings: python: allow closing a Chip multiple times

      https://git.kernel.org/brgl/c/711a05b2548718b22faf8924a8b301a4e8cf4852

Best regards,

Signature · 1 line
Bartosz Golaszewski <[email protected]>