public inbox for linux-gpio@vger.kernel.org
 help / color / mirror / Atom feed
From: Vincent Fazio <vfazio@gmail.com>
To: linux-gpio@vger.kernel.org
Cc: brgl@kernel.org, Vincent Fazio <vfazio@gmail.com>
Subject: [libgpiod][PATCH v2] bindings: python: allow closing a Chip multiple times
Date: Fri,  3 Apr 2026 10:46:04 -0500	[thread overview]
Message-ID: <20260403154604.3944747-1-vfazio@gmail.com> (raw)

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


                 reply	other threads:[~2026-04-03 15:46 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260403154604.3944747-1-vfazio@gmail.com \
    --to=vfazio@gmail.com \
    --cc=brgl@kernel.org \
    --cc=linux-gpio@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox