Skip to contentSkip to conversation

bindings: python: allow closing Chip multiple times

PATCH Superseded Vincent Fazio 2 participants 3 messages Apr 2 – Apr 3 GPIO

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

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

Allow closing a Chip mulitple times without throwing a ChipClosedError to adhere to common Python semantics.

---
 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 6 lines · Vincent Fazio

Allow closing a Chip mulitple times without throwing a ChipClosedError to adhere to common Python semantics.

---

Would you mind just elaborating on what these common semantics are in the commit message for python noobs like myself? :)

Bartosz

#2 lore raw
Quoted 3 lines · Bartosz Golaszewski

Would you mind just elaborating on what these common semantics are in the commit message for python noobs like myself? :)

Will do