linux-gpio.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [libgpiod][PATCH 0/2] support casting line.Value to bool
@ 2024-05-22  0:46 Kent Gibson
  2024-05-22  0:46 ` [libgpiod][PATCH 1/2] bindings: python: tests: add test for " Kent Gibson
                   ` (3 more replies)
  0 siblings, 4 replies; 8+ messages in thread
From: Kent Gibson @ 2024-05-22  0:46 UTC (permalink / raw)
  To: linux-gpio, brgl; +Cc: Kent Gibson

While writing a gpiod plugin for gpiozero (Python), I had to map line.Value
to its bool equivalent.  Casting seemed the obvious way to go, as it is
essentially a boolean, but that didn't work as I expected - it always
returned True. This is the case for any Python type that does not provide
a suitable conversion operator.

This series adds support for casting line.Value to bool.

Patch 1 adds a test that comfirms the existing behaviour.
Patch 2 adds the __bool__() operator to make the Value behave as one
might expect.

As an aside, I couldn't for the life of me work out how to run the complete
python test suite.  There are no hints in the documentation.

There is a python-tests-run target in the Makefile, but that didn't work:

~/libgpiod/bindings/python$ make python-tests-run
PYTHONPATH=/home/kent/libgpiod/bindings/python
LD_LIBRARY_PATH=/home/kent/libgpiod/lib/.libs/:\
	/home/kent/libgpiod/tests/gpiosim/.libs/ \
python3 -B -m tests
/bin/bash: line 2: /home/kent/libgpiod/tests/gpiosim/.libs/: Is a directory
make: *** [Makefile:677: python-tests-run] Error 126

I tried fixing that but I still couldn't satisfy ld wrt the gpiosim
(I don't have libgpiod installed - just using the local build),
so gave up and called this particular test directly with

$ python -m unittest tests_line.py

While that passes, I can't guarantee it hasn't caused some other
breakage, though it seems very unlikely.

Kent Gibson (2):
  bindings: python: tests: add test for casting line.Value to bool
  bindings: python: support casting line.Value to bool

 bindings/python/gpiod/line.py       |  3 +++
 bindings/python/tests/Makefile.am   |  1 +
 bindings/python/tests/tests_line.py | 11 +++++++++++
 3 files changed, 15 insertions(+)
 create mode 100644 bindings/python/tests/tests_line.py

--
2.39.2


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [libgpiod][PATCH 1/2] bindings: python: tests: add test for casting line.Value to bool
  2024-05-22  0:46 [libgpiod][PATCH 0/2] support casting line.Value to bool Kent Gibson
@ 2024-05-22  0:46 ` Kent Gibson
  2024-05-22  0:46 ` [libgpiod][PATCH 2/2] bindings: python: support " Kent Gibson
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 8+ messages in thread
From: Kent Gibson @ 2024-05-22  0:46 UTC (permalink / raw)
  To: linux-gpio, brgl; +Cc: Kent Gibson

The line.Value represents the logical line state, so intuitively you
would expect it to be able to be cast to bool, with ACTIVE
corresponding to True, and INACTIVE to False.

Add a test that line.Value can be cast to bool.

Signed-off-by: Kent Gibson <warthog618@gmail.com>
---
 bindings/python/tests/Makefile.am   |  1 +
 bindings/python/tests/tests_line.py | 11 +++++++++++
 2 files changed, 12 insertions(+)
 create mode 100644 bindings/python/tests/tests_line.py

diff --git a/bindings/python/tests/Makefile.am b/bindings/python/tests/Makefile.am
index c89241e..3118d5f 100644
--- a/bindings/python/tests/Makefile.am
+++ b/bindings/python/tests/Makefile.am
@@ -11,6 +11,7 @@ EXTRA_DIST = \
 	tests_chip.py \
 	tests_edge_event.py \
 	tests_info_event.py \
+	tests_line.py \
 	tests_line_info.py \
 	tests_line_request.py \
 	tests_line_settings.py \
diff --git a/bindings/python/tests/tests_line.py b/bindings/python/tests/tests_line.py
new file mode 100644
index 0000000..70aa09b
--- /dev/null
+++ b/bindings/python/tests/tests_line.py
@@ -0,0 +1,11 @@
+# SPDX-License-Identifier: LGPL-2.1-or-later
+# SPDX-FileCopyrightText: 2024 Kent Gibson <warthog618@gmail.com>
+
+from gpiod.line import Value
+from unittest import TestCase
+
+
+class LineValue(TestCase):
+    def test_cast_bool(self):
+        self.assertTrue(bool(Value.ACTIVE))
+        self.assertFalse(bool(Value.INACTIVE))
-- 
2.39.2


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [libgpiod][PATCH 2/2] bindings: python: support casting line.Value to bool
  2024-05-22  0:46 [libgpiod][PATCH 0/2] support casting line.Value to bool Kent Gibson
  2024-05-22  0:46 ` [libgpiod][PATCH 1/2] bindings: python: tests: add test for " Kent Gibson
@ 2024-05-22  0:46 ` Kent Gibson
  2024-05-22 16:22 ` [libgpiod][PATCH 0/2] " brgl
  2024-05-23  7:55 ` Bartosz Golaszewski
  3 siblings, 0 replies; 8+ messages in thread
From: Kent Gibson @ 2024-05-22  0:46 UTC (permalink / raw)
  To: linux-gpio, brgl; +Cc: Kent Gibson

Python types default to being truthy when cast to bool, so casting
line.Value to bool always returns True.

Add a line.Value.__bool__() operator to map the line value to bool as
one would intuitively expect, so ACTIVE is True and INACTIVE is False.

Signed-off-by: Kent Gibson <warthog618@gmail.com>
---
 bindings/python/gpiod/line.py | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/bindings/python/gpiod/line.py b/bindings/python/gpiod/line.py
index 1cc512f..d088fb4 100644
--- a/bindings/python/gpiod/line.py
+++ b/bindings/python/gpiod/line.py
@@ -14,6 +14,9 @@ class Value(Enum):
     INACTIVE = _ext.VALUE_INACTIVE
     ACTIVE = _ext.VALUE_ACTIVE
 
+    def __bool__(self):
+        return self == self.ACTIVE
+
 
 class Direction(Enum):
     """Direction settings."""
-- 
2.39.2


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [libgpiod][PATCH 0/2] support casting line.Value to bool
  2024-05-22  0:46 [libgpiod][PATCH 0/2] support casting line.Value to bool Kent Gibson
  2024-05-22  0:46 ` [libgpiod][PATCH 1/2] bindings: python: tests: add test for " Kent Gibson
  2024-05-22  0:46 ` [libgpiod][PATCH 2/2] bindings: python: support " Kent Gibson
@ 2024-05-22 16:22 ` brgl
  2024-05-22 23:57   ` Kent Gibson
  2024-05-23  7:55 ` Bartosz Golaszewski
  3 siblings, 1 reply; 8+ messages in thread
From: brgl @ 2024-05-22 16:22 UTC (permalink / raw)
  To: Kent Gibson; +Cc: linux-gpio, brgl

On Wed, 22 May 2024 02:46:41 +0200, Kent Gibson <warthog618@gmail.com> said:
> While writing a gpiod plugin for gpiozero (Python), I had to map line.Value
> to its bool equivalent.  Casting seemed the obvious way to go, as it is
> essentially a boolean, but that didn't work as I expected - it always
> returned True. This is the case for any Python type that does not provide
> a suitable conversion operator.
>
> This series adds support for casting line.Value to bool.
>

Ha! Interesting. Do you think we may need it anywhere else too?

> Patch 1 adds a test that comfirms the existing behaviour.
> Patch 2 adds the __bool__() operator to make the Value behave as one
> might expect.
>
> As an aside, I couldn't for the life of me work out how to run the complete
> python test suite.  There are no hints in the documentation.
>
> There is a python-tests-run target in the Makefile, but that didn't work:
>
> ~/libgpiod/bindings/python$ make python-tests-run
> PYTHONPATH=/home/kent/libgpiod/bindings/python
> LD_LIBRARY_PATH=/home/kent/libgpiod/lib/.libs/:\
> 	/home/kent/libgpiod/tests/gpiosim/.libs/ \
> python3 -B -m tests
> /bin/bash: line 2: /home/kent/libgpiod/tests/gpiosim/.libs/: Is a directory
> make: *** [Makefile:677: python-tests-run] Error 126
>
> I tried fixing that but I still couldn't satisfy ld wrt the gpiosim
> (I don't have libgpiod installed - just using the local build),
> so gave up and called this particular test directly with
>

I typically run it like this:

    PYTHONPATH=./bindings/python
LD_LIBRARY_PATH=./lib/.libs/:./tests/gpiosim/.libs/:bindings/python/
python -B -m tests

Bart

> $ python -m unittest tests_line.py
>
> While that passes, I can't guarantee it hasn't caused some other
> breakage, though it seems very unlikely.
>
> Kent Gibson (2):
>   bindings: python: tests: add test for casting line.Value to bool
>   bindings: python: support casting line.Value to bool
>
>  bindings/python/gpiod/line.py       |  3 +++
>  bindings/python/tests/Makefile.am   |  1 +
>  bindings/python/tests/tests_line.py | 11 +++++++++++
>  3 files changed, 15 insertions(+)
>  create mode 100644 bindings/python/tests/tests_line.py
>
> --
> 2.39.2
>
>

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [libgpiod][PATCH 0/2] support casting line.Value to bool
  2024-05-22 16:22 ` [libgpiod][PATCH 0/2] " brgl
@ 2024-05-22 23:57   ` Kent Gibson
  2024-05-23  0:32     ` Kent Gibson
  0 siblings, 1 reply; 8+ messages in thread
From: Kent Gibson @ 2024-05-22 23:57 UTC (permalink / raw)
  To: brgl; +Cc: linux-gpio

On Wed, May 22, 2024 at 09:22:50AM -0700, brgl@bgdev.pl wrote:
> On Wed, 22 May 2024 02:46:41 +0200, Kent Gibson <warthog618@gmail.com> said:
> > While writing a gpiod plugin for gpiozero (Python), I had to map line.Value
> > to its bool equivalent.  Casting seemed the obvious way to go, as it is
> > essentially a boolean, but that didn't work as I expected - it always
> > returned True. This is the case for any Python type that does not provide
> > a suitable conversion operator.
> >
> > This series adds support for casting line.Value to bool.
> >
>
> Ha! Interesting. Do you think we may need it anywhere else too?
>

I guess the same applies to the C++ and Rust bindings - I'll have to
check.

> > Patch 1 adds a test that comfirms the existing behaviour.
> > Patch 2 adds the __bool__() operator to make the Value behave as one
> > might expect.
> >
> > As an aside, I couldn't for the life of me work out how to run the complete
> > python test suite.  There are no hints in the documentation.
> >
> > There is a python-tests-run target in the Makefile, but that didn't work:
> >
> > ~/libgpiod/bindings/python$ make python-tests-run
> > PYTHONPATH=/home/kent/libgpiod/bindings/python
> > LD_LIBRARY_PATH=/home/kent/libgpiod/lib/.libs/:\
> > 	/home/kent/libgpiod/tests/gpiosim/.libs/ \
> > python3 -B -m tests
> > /bin/bash: line 2: /home/kent/libgpiod/tests/gpiosim/.libs/: Is a directory
> > make: *** [Makefile:677: python-tests-run] Error 126
> >
> > I tried fixing that but I still couldn't satisfy ld wrt the gpiosim
> > (I don't have libgpiod installed - just using the local build),
> > so gave up and called this particular test directly with
> >
>
> I typically run it like this:
>
>     PYTHONPATH=./bindings/python
> LD_LIBRARY_PATH=./lib/.libs/:./tests/gpiosim/.libs/:bindings/python/
> python -B -m tests
>

So a one-liner?

That gives me:

$ PYTHONPATH=./bindings/python LD_LIBRARY_PATH=./lib/.libs/:./tests/gpiosim/.libs/:bindings/python/ python3 -B -m tests
Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "/home/kent/libgpiod/bindings/python/tests/__main__.py", line 7, in <module>
    from .tests_chip import *
  File "/home/kent/libgpiod/bindings/python/tests/tests_chip.py", line 8, in <module>
    from . import gpiosim
  File "/home/kent/libgpiod/bindings/python/tests/gpiosim/__init__.py", line 4, in <module>
    from .chip import Chip
  File "/home/kent/libgpiod/bindings/python/tests/gpiosim/chip.py", line 4, in <module>
    from . import _ext
FileNotFoundError: [Errno 2] No such file or directory

That might also be as far as I got previously - I may've been mis-remembering
that the final hurdle was an import error, not an ld issue, though both are
looking for a module they can't find, so basically the same thing.

Cheers,
Kent.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [libgpiod][PATCH 0/2] support casting line.Value to bool
  2024-05-22 23:57   ` Kent Gibson
@ 2024-05-23  0:32     ` Kent Gibson
  2024-05-23  7:50       ` Bartosz Golaszewski
  0 siblings, 1 reply; 8+ messages in thread
From: Kent Gibson @ 2024-05-23  0:32 UTC (permalink / raw)
  To: brgl; +Cc: linux-gpio

On Thu, May 23, 2024 at 07:57:12AM +0800, Kent Gibson wrote:
> On Wed, May 22, 2024 at 09:22:50AM -0700, brgl@bgdev.pl wrote:
> > On Wed, 22 May 2024 02:46:41 +0200, Kent Gibson <warthog618@gmail.com> said:
> > > While writing a gpiod plugin for gpiozero (Python), I had to map line.Value
> > > to its bool equivalent.  Casting seemed the obvious way to go, as it is
> > > essentially a boolean, but that didn't work as I expected - it always
> > > returned True. This is the case for any Python type that does not provide
> > > a suitable conversion operator.
> > >
> > > This series adds support for casting line.Value to bool.
> > >
> >
> > Ha! Interesting. Do you think we may need it anywhere else too?
> >
>
> I guess the same applies to the C++ and Rust bindings - I'll have to
> check.
>

Or do you mean other fields?  There wasn't anything in line.py - all the
other enums are non-binary.  And nothing else springs to mind.

Cheers,
Kent.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [libgpiod][PATCH 0/2] support casting line.Value to bool
  2024-05-23  0:32     ` Kent Gibson
@ 2024-05-23  7:50       ` Bartosz Golaszewski
  0 siblings, 0 replies; 8+ messages in thread
From: Bartosz Golaszewski @ 2024-05-23  7:50 UTC (permalink / raw)
  To: Kent Gibson, Viresh Kumar; +Cc: linux-gpio

On Thu, May 23, 2024 at 2:32 AM Kent Gibson <warthog618@gmail.com> wrote:
>
> On Thu, May 23, 2024 at 07:57:12AM +0800, Kent Gibson wrote:
> > On Wed, May 22, 2024 at 09:22:50AM -0700, brgl@bgdev.pl wrote:
> > > On Wed, 22 May 2024 02:46:41 +0200, Kent Gibson <warthog618@gmail.com> said:
> > > > While writing a gpiod plugin for gpiozero (Python), I had to map line.Value
> > > > to its bool equivalent.  Casting seemed the obvious way to go, as it is
> > > > essentially a boolean, but that didn't work as I expected - it always
> > > > returned True. This is the case for any Python type that does not provide
> > > > a suitable conversion operator.
> > > >
> > > > This series adds support for casting line.Value to bool.
> > > >
> > >
> > > Ha! Interesting. Do you think we may need it anywhere else too?
> > >
> >
> > I guess the same applies to the C++ and Rust bindings - I'll have to
> > check.
> >
>
> Or do you mean other fields?  There wasn't anything in line.py - all the
> other enums are non-binary.  And nothing else springs to mind.

Yeah, I meant other Python classes. C++ is fine, the cast from enum to
int is unambiguous. For Rust I don't know but I'm seeing things like
IntMap<Value> in the code, I suppose, enums map fine if nobody
complained yet (given rust would most likely not build at all if it
didn't handle this case).

Bart

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [libgpiod][PATCH 0/2] support casting line.Value to bool
  2024-05-22  0:46 [libgpiod][PATCH 0/2] support casting line.Value to bool Kent Gibson
                   ` (2 preceding siblings ...)
  2024-05-22 16:22 ` [libgpiod][PATCH 0/2] " brgl
@ 2024-05-23  7:55 ` Bartosz Golaszewski
  3 siblings, 0 replies; 8+ messages in thread
From: Bartosz Golaszewski @ 2024-05-23  7:55 UTC (permalink / raw)
  To: linux-gpio, brgl, Kent Gibson; +Cc: Bartosz Golaszewski

From: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>


On Wed, 22 May 2024 08:46:41 +0800, Kent Gibson wrote:
> While writing a gpiod plugin for gpiozero (Python), I had to map line.Value
> to its bool equivalent.  Casting seemed the obvious way to go, as it is
> essentially a boolean, but that didn't work as I expected - it always
> returned True. This is the case for any Python type that does not provide
> a suitable conversion operator.
> 
> This series adds support for casting line.Value to bool.
> 
> [...]

Applied, thanks!

[1/2] bindings: python: tests: add test for casting line.Value to bool
      commit: c8e3ae0499c800955cd77d8959be0f14e4b514cc
[2/2] bindings: python: support casting line.Value to bool
      commit: 5f9152b0ca8ad7ac8a8591553931b38dc10c5db0

Best regards,
-- 
Bartosz Golaszewski <bartosz.golaszewski@linaro.org>

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2024-05-23  7:55 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-05-22  0:46 [libgpiod][PATCH 0/2] support casting line.Value to bool Kent Gibson
2024-05-22  0:46 ` [libgpiod][PATCH 1/2] bindings: python: tests: add test for " Kent Gibson
2024-05-22  0:46 ` [libgpiod][PATCH 2/2] bindings: python: support " Kent Gibson
2024-05-22 16:22 ` [libgpiod][PATCH 0/2] " brgl
2024-05-22 23:57   ` Kent Gibson
2024-05-23  0:32     ` Kent Gibson
2024-05-23  7:50       ` Bartosz Golaszewski
2024-05-23  7:55 ` Bartosz Golaszewski

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).