From: Nicolas Frattaroli <frattaroli.nicolas@gmail.com>
To: linux-gpio@vger.kernel.org
Cc: Nicolas Frattaroli <frattaroli.nicolas@gmail.com>
Subject: [libgpiod] [RFC PATCH] bindings: python: allow specifying infinite timeout
Date: Fri, 19 May 2023 19:46:19 +0200 [thread overview]
Message-ID: <20230519174619.58308-1-frattaroli.nicolas@gmail.com> (raw)
So far, libgpiod's Python bindings had no way to state that a
user wishes to wait for events indefinitely, as a timeout of
None would intentionally be converted to 0 seconds, i.e. return
from the select call in poll_fd immediately.
The usual Python convention and even the select convention is
to block indefinitely on a timeout=None. However, changing the
poll_fd function to do this now would change an (intentional)
API design choice by libgpiod 2.0 that API users presumably
rely on.
By allowing float("inf") (or in fact math.inf, or your favourite
other way to get an infinite float) to mean waiting infinitely
solves this by extending the API rather than changing it.
On gpiod Python bindings without this change, passing inf results
in an OverflowError being raised in select. API users who wish to
support older versions of the bindings can catch this exception and
act on it.
Signed-off-by: Nicolas Frattaroli <frattaroli.nicolas@gmail.com>
---
bindings/python/gpiod/chip.py | 3 ++-
bindings/python/gpiod/internal.py | 4 ++++
bindings/python/gpiod/line_request.py | 3 ++-
3 files changed, 8 insertions(+), 2 deletions(-)
diff --git a/bindings/python/gpiod/chip.py b/bindings/python/gpiod/chip.py
index 97ff340..95c5757 100644
--- a/bindings/python/gpiod/chip.py
+++ b/bindings/python/gpiod/chip.py
@@ -195,7 +195,8 @@ class Chip:
Args:
timeout:
Wait time limit represented as either a datetime.timedelta object
- or the number of seconds stored in a float.
+ or the number of seconds stored in a float. A timeout of None
+ returns immediately, use float("inf") to wait indefinitely.
Returns:
True if an info event is ready to be read from the chip, False if the
diff --git a/bindings/python/gpiod/internal.py b/bindings/python/gpiod/internal.py
index 37e8b62..141cfe9 100644
--- a/bindings/python/gpiod/internal.py
+++ b/bindings/python/gpiod/internal.py
@@ -2,6 +2,7 @@
# SPDX-FileCopyrightText: 2022 Bartosz Golaszewski <brgl@bgdev.pl>
from datetime import timedelta
+from math import inf
from select import select
from typing import Optional, Union
@@ -15,5 +16,8 @@ def poll_fd(fd: int, timeout: Optional[Union[timedelta, float]] = None) -> bool:
else:
sec = timeout
+ if sec == inf:
+ sec = None
+
readable, _, _ = select([fd], [], [], sec)
return True if fd in readable else False
diff --git a/bindings/python/gpiod/line_request.py b/bindings/python/gpiod/line_request.py
index a0f97b7..ae21835 100644
--- a/bindings/python/gpiod/line_request.py
+++ b/bindings/python/gpiod/line_request.py
@@ -178,7 +178,8 @@ class LineRequest:
Args:
timeout:
Wait time limit expressed as either a datetime.timedelta object
- or the number of seconds stored in a float.
+ or the number of seconds stored in a float. None returns
+ immediately. Use float("inf") to wait indefinitely.
Returns:
True if events are ready to be read. False on timeout.
--
2.40.1
next reply other threads:[~2023-05-19 17:47 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-05-19 17:46 Nicolas Frattaroli [this message]
2023-05-23 10:06 ` [libgpiod] [RFC PATCH] bindings: python: allow specifying infinite timeout Bartosz Golaszewski
2023-05-23 12:18 ` andy.shevchenko
2023-05-23 12:33 ` Bartosz Golaszewski
2023-05-23 12:38 ` Nicolas Frattaroli
2023-05-23 13:04 ` Bartosz Golaszewski
2023-05-23 13:18 ` Nicolas Frattaroli
2023-05-23 13:27 ` Bartosz Golaszewski
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=20230519174619.58308-1-frattaroli.nicolas@gmail.com \
--to=frattaroli.nicolas@gmail.com \
--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