Linux GPIO subsystem development
 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 04/14] bindings: python: setup: use a context manager to manage files
Date: Mon, 19 Oct 2026 19:37:18 -0500	[thread overview]
Message-ID: <20261020003728.13669-5-vfazio@gmail.com> (raw)
In-Reply-To: <20261020003728.13669-1-vfazio@gmail.com>

Previously, a number of places in setup.py did not use a context manager
to manage the lifetime of the underlying file descriptor for reading and
writing the `libgpiod-version.txt` file.

While the previous method works, it relies on the returned file object
being assigned to a temporary Python object that has its refcount
decremented immediately after statement execution. This then relies on
the file object's finalizer to close the underlying handle which can
emit ResourceWarnings:

  /bindings/python/setup.py:193: ResourceWarning: unclosed file <_io.TextIOWrapper name='libgpiod-version.txt' mode='w' encoding='UTF-8'>
    open("libgpiod-version.txt", "w").write(LIBGPIOD_VERSION)

Now, we specifically use a context manager to close the file descriptor
when done to explicitly manage its lifetime.

Signed-off-by: Vincent Fazio <vfazio@gmail.com>
---
 bindings/python/setup.py | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)

diff --git a/bindings/python/setup.py b/bindings/python/setup.py
index 7c9a03f..4297615 100644
--- a/bindings/python/setup.py
+++ b/bindings/python/setup.py
@@ -87,10 +87,11 @@ def fetch_tarball(func: Callable[[T], None]) -> Callable[[T], None]:
         # If the version in "libgpiod-version.txt" already matches our
         # requested tarball, then skip the fetch altogether.
         try:
-            if open("libgpiod-version.txt").read() == LIBGPIOD_VERSION:
-                log.info("skipping tarball fetch")
-                func(cmd)
-                return
+            with open("libgpiod-version.txt") as f:
+                if f.read() == LIBGPIOD_VERSION:
+                    log.info("skipping tarball fetch")
+                    func(cmd)
+                    return
         except OSError:
             pass
 
@@ -185,7 +186,8 @@ def fetch_tarball(func: Callable[[T], None]) -> Callable[[T], None]:
                     cmd.distribution.metadata.license_files = [LICENSE_FILE]  # type: ignore[attr-defined]
 
         # Save the libgpiod version for sdist
-        open("libgpiod-version.txt", "w").write(LIBGPIOD_VERSION)
+        with open("libgpiod-version.txt", "w") as f:
+            f.write(LIBGPIOD_VERSION)
 
         # Run the command
         func(cmd)
@@ -217,7 +219,8 @@ class build_ext(orig_build_ext):
         # Try to get the gpiod version from the .txt file included in sdist
         libgpiod_version: str | None
         try:
-            libgpiod_version = open("libgpiod-version.txt").read()
+            with open("libgpiod-version.txt") as f:
+                libgpiod_version = f.read()
         except OSError:
             libgpiod_version = LIBGPIOD_VERSION
 
-- 
2.43.0


  parent reply	other threads:[~2026-10-20  0:37 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-20  0:37 [libgpiod][PATCH 00/14] bindings: python: cleanup and support Python 3.15 Vincent Fazio
2026-10-20  0:37 ` [libgpiod][PATCH 01/14] bindings: python: _internal: sort exports Vincent Fazio
2026-10-20  0:37 ` [libgpiod][PATCH 02/14] bindings: python: _internal: simplify poll_fd calculation Vincent Fazio
2026-10-20  0:37 ` [libgpiod][PATCH 03/14] bindings: python: _internal: collapse isinstance checks Vincent Fazio
2026-10-20  0:37 ` Vincent Fazio [this message]
2026-10-20  0:37 ` [libgpiod][PATCH 05/14] bindings: python: line: sort exports Vincent Fazio
2026-10-20  0:37 ` [libgpiod][PATCH 06/14] bindings: python: line_request: simplify __bool__ expression Vincent Fazio
2026-10-20  0:37 ` [libgpiod][PATCH 07/14] bindings: python: chip: " Vincent Fazio
2026-10-20  0:37 ` [libgpiod][PATCH 08/14] bindings: python: chip: drop redundant exception in reraise Vincent Fazio
2026-10-20  0:37 ` [libgpiod][PATCH 09/14] bindings: python: chip: use literals for dict/list and declare types Vincent Fazio
2026-10-20  0:37 ` [libgpiod][PATCH 10/14] bindings: python: bump ruff linter dependency Vincent Fazio
2026-10-20  0:37 ` [libgpiod][PATCH 11/14] bindings: python: include Python.h before other headers Vincent Fazio
2026-10-20  0:37 ` [libgpiod][PATCH 12/14] bindings: python: advertise Python 3.15 support Vincent Fazio
2026-10-20  0:37 ` [libgpiod][PATCH 13/14] bindings: python: fix directory validation in artifact script Vincent Fazio
2026-10-20  0:37 ` [libgpiod][PATCH 14/14] bindings: python: bump dependencies " Vincent Fazio

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=20261020003728.13669-5-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