1
0
mirror of https://git.kernel.org/pub/scm/libs/libgpiod/libgpiod.git synced 2026-10-24 00:48:53 +03:00
Commit Graph

1426 Commits

Author SHA1 Message Date
Bartosz Golaszewski
bf0d733f4d bindings: python: gpiod v2.3.0
Minor gpiod package release.

Changelog:
- add fileno() for Chip and LineRequest
- relax the verbosity adjustment in build_tests.sh
- fix potential PyDict_Next() crash
- fix tests failing after a kernel fix to gpio-sim
- various documentation fixes
- rework the code treewide to make it conformant with mypy and ruff
  linter recommendations
- fix requesting lines by name with multiple entries
- fix a use-after-free bug in python bindings
- fix passing the event clock property to line requests in python
  bindings
- improve the output of LineSettings.__repr__()
- import gpiod attributes in external module
- specify a tar extract filter for python >= 3.12

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
python-v2.3.0
2025-02-19 13:36:05 +01:00
Bartosz Golaszewski
7439b03e9c doc: add missing .rst files to EXTRA_DIST
Several .rst files are not bundled in distro tarballs. Add them to
EXTRA_DIST.

Fixes: dda57bd63f ("doc: move README contents to sphinx docs")
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2025-02-19 12:56:39 +01:00
Bartosz Golaszewski
dda57bd63f doc: move README contents to sphinx docs
We now have comprehensive documentation available online on readthedocs.
Let's not duplicate docs in README - move all information into the
sphinx files.

While at it: make the remaining README use markdown.

Reviewed-by: Vincent Fazio <vfazio@xes-inc.com>
Link: https://lore.kernel.org/r/20250211-improve-docs-v4-17-dc56702c2ca8@linaro.org
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2025-02-17 09:18:27 +01:00
Bartosz Golaszewski
bbcb5d4461 doc: add documentation for D-Bus API, daemon and command-line client
Use the man pages for gpio-manager and gpiocli + some manual labor to
provide sphinx docs for the libgpiod D-Bus API.

Reviewed-by: Vincent Fazio <vfazio@xes-inc.com>
Link: https://lore.kernel.org/r/20250211-improve-docs-v4-16-dc56702c2ca8@linaro.org
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2025-02-17 09:18:24 +01:00
Bartosz Golaszewski
6f8b77c712 doc: add documentation for gpio-tools
We already generate man pages for gpio-tools. Let's use man2html to make
them part of the sphinx docs.

Reviewed-by: Vincent Fazio <vfazio@xes-inc.com>
Link: https://lore.kernel.org/r/20250211-improve-docs-v4-15-dc56702c2ca8@linaro.org
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2025-02-17 09:18:22 +01:00
Bartosz Golaszewski
f37aaf2a6a doc: add documentation for GLib bindings
The GObject docs are generated with gi-docgen and there doesn't seem to
be an easy way of converting them to sphinx. Let's put the GLib docs
statically into the sphinx output and reference them from the bindings
chapter.

Reviewed-by: Vincent Fazio <vfazio@xes-inc.com>
Link: https://lore.kernel.org/r/20250211-improve-docs-v4-14-dc56702c2ca8@linaro.org
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2025-02-17 09:18:20 +01:00
Bartosz Golaszewski
07da462186 doc: add documentation for python bindings
Integrate python docs with sphinx using autodoc and the import mock
feature which allows us to avoid having to build the C extension.

Reviewed-by: Vincent Fazio <vfazio@xes-inc.com>
Link: https://lore.kernel.org/r/20250211-improve-docs-v4-13-dc56702c2ca8@linaro.org
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2025-02-17 09:18:16 +01:00
Bartosz Golaszewski
7e11886fff doc: provide sphinx docs for the core C API and C++ bindings
Current sphinx/ contents are mostly WiP. Let's beat them into shape for
publishing on ReadTheDocs.

Integrate the API documentation generated with doxygen for the core C
API and C++ bindings into sphinx using breathe.

Update configure.ac to check for sphinx-build in addition to doxygen and
make the main Makefile trigger a sphinx build on `make doc` (although
the docs can also be generated without starting the build system by
running: `sphinx-build ./docs/ docs/sphinx-output`).

Move the introduction text from the main header into the relevant .rst
file.

Remove obsolete Doxyfile.in and create a static Doxygen under doc/ where
all the documentation now lives.

Update .gitignore where needed.

Reviewed-by: Vincent Fazio <vfazio@xes-inc.com>
Link: https://lore.kernel.org/r/20250211-improve-docs-v4-12-dc56702c2ca8@linaro.org
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2025-02-17 09:18:11 +01:00
Bartosz Golaszewski
3b4a2ced02 doc: create man entries for gpio-manager and gpiocli
Extend the build files under man/ to also generate man pages for
gpio-manager, gpiocli and each of the former's sub-commands.

Reviewed-by: Vincent Fazio <vfazio@xes-inc.com>
Link: https://lore.kernel.org/r/20250211-improve-docs-v4-11-dc56702c2ca8@linaro.org
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2025-02-17 09:18:09 +01:00
Bartosz Golaszewski
91955a1f93 dbus: improve comments in API xml
The current comments don't follow any particular formatting so the
resulting .rst generated with gdbus-codegen is all mangled up. Use
proper .rst formatting to make docs better.

Reviewed-by: Vincent Fazio <vfazio@xes-inc.com>
Link: https://lore.kernel.org/r/20250211-improve-docs-v4-10-dc56702c2ca8@linaro.org
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2025-02-17 09:18:07 +01:00
Bartosz Golaszewski
1ddfa9fc4b dbus: client: tweak help text
help2man struggles to generate correct output with current help-text for
the sub-commands section. It doesn't break the lines correctly and the
resulting .rst generated with pandoc looks just as bad. Tweak the output
a bit to generate better man pages with correctly broken lines.

Reviewed-by: Vincent Fazio <vfazio@xes-inc.com>
Link: https://lore.kernel.org/r/20250211-improve-docs-v4-9-dc56702c2ca8@linaro.org
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2025-02-17 09:18:04 +01:00
Bartosz Golaszewski
681c0c2767 dbus: daemon: add a more detailed description to help text
In preparation for adding man pages for the daemon and the command-line
client, add a detailed description of what gpio-manager is and does to
the output presented to user when they pass --help as argument.

Reviewed-by: Vincent Fazio <vfazio@xes-inc.com>
Link: https://lore.kernel.org/r/20250211-improve-docs-v4-8-dc56702c2ca8@linaro.org
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2025-02-17 09:18:01 +01:00
Bartosz Golaszewski
d0139ffa9c bindings: glib: add the configuration file for gi-docgen
Without the gi-docgen configuration file, the GObject docs don't
contain information such as library version, author, license, etc. Add a
simple .toml to add missing bits and update the command in Makefile.

Reviewed-by: Vincent Fazio <vfazio@xes-inc.com>
Link: https://lore.kernel.org/r/20250211-improve-docs-v4-7-dc56702c2ca8@linaro.org
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2025-02-17 09:17:58 +01:00
Bartosz Golaszewski
7ac2650893 bindings: gpiod: reword the docstring for LineRequest.__init__()
Change the docstring for LineRequest.__init__() so that when it appears
in sphinx docs as part of the class description, it won't sound as if
the class was not to be used at all.

Reviewed-by: Vincent Fazio <vfazio@xes-inc.com>
Link: https://lore.kernel.org/r/20250211-improve-docs-v4-6-dc56702c2ca8@linaro.org
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2025-02-17 09:17:56 +01:00
Bartosz Golaszewski
d46e8f67aa bindings: python: doc: describe undocumented members
There are several public members in python bindings that are missing
docstrings. Document them for completeness.

Reviewed-by: Vincent Fazio <vfazio@xes-inc.com>
Link: https://lore.kernel.org/r/20250211-improve-docs-v4-5-dc56702c2ca8@linaro.org
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2025-02-17 09:17:53 +01:00
Bartosz Golaszewski
b1bc6664f6 bindings: python: doc: make code examples appear as such in sphinx
In order for code examples inside docstrings to be interpreted as such
by sphinx, we need to use a double colon ("Example::").

Reviewed-by: Vincent Fazio <vfazio@xes-inc.com>
Link: https://lore.kernel.org/r/20250211-improve-docs-v4-4-dc56702c2ca8@linaro.org
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2025-02-17 09:17:50 +01:00
Bartosz Golaszewski
8aa4d84b6f bindings: python: doc: update the docstring for gpiod.request_lines()
The global request_lines() function was updated to take explicitly typed
arguments instead of just passing *args and **kwargs down to
Chip.request_lines(). However, the doc remained unchanged. Update it now
to reflect the actual function parameters.

Reviewed-by: Vincent Fazio <vfazio@xes-inc.com>
Link: https://lore.kernel.org/r/20250211-improve-docs-v4-3-dc56702c2ca8@linaro.org
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2025-02-17 09:17:48 +01:00
Bartosz Golaszewski
e025e1cb5c bindings: cxx: doc: remove the gpiod_cxx doxygen group
Groups make sense for the C API as they allow us to nicely order
interfaces manipulating a given structure thematically but for C++ they
make less sense as methods are already typically part of a class.
Additionally for global functions, they seem to make it difficult for
breathe to look up the correct symbols.

Remove the gpiod_cxx group from Doxygen comments.

Reviewed-by: Vincent Fazio <vfazio@xes-inc.com>
Link: https://lore.kernel.org/r/20250211-improve-docs-v4-2-dc56702c2ca8@linaro.org
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2025-02-17 09:17:45 +01:00
Bartosz Golaszewski
588a196abb build: set PACKAGE_URL
We don't set the package URL in AC_INIT() so the URL section in
pkgconfig files is empty. Set it to the address of the main git repo.

Reviewed-by: Vincent Fazio <vfazio@xes-inc.com>
Link: https://lore.kernel.org/r/20250211-improve-docs-v4-1-dc56702c2ca8@linaro.org
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2025-02-17 09:17:36 +01:00
Bartosz Golaszewski
e3c641ec7c gpiosim: selftests: check that a disabled device can be re-enabled
Extend the test sequence to check that a device can be disabled and
re-enabled without problems.

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2025-02-17 09:15:13 +01:00
Bartosz Golaszewski
3dac2c5d9c gpiosim: defer removal of bank entries when device is not disabled
Linux kernel commit 8bd76b3d3f3a ("gpio: sim: lock up configfs that an
instantiated device depends on") has uncovered an issue in libgpiosim:
we use reference counting for all objects (to make RAII easier for
bindings) but, with the above change, if the user doesn't explicitly
disable the device before dropping the last reference to a bank object,
the parent device will never get correctly cleaned up.

This is because we remove the hog and line directories in bank's
release() callback unconditionally. With the directories being locked
now, this fails if device is active and then when we eventually call the
device object's release(), we'll end up stuck with leftover bank
directories and never actually clean-up correctly.

Extend the gpiosim device with a list of deferred banks. When releasing
a bank on an active device, defer any actual cleanup until the time the
device gets deactivated which may be triggered by the user explictly or
will happen automatically when the last reference to the device object is
dropped. For inactive devices, there's no functional change.

Link: https://lore.kernel.org/r/20250214102648.71382-1-brgl@bgdev.pl
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2025-02-17 09:14:54 +01:00
Bartosz Golaszewski
5486f1f0ff bindings: glib: tests: make waiting for info events more reliable
One GMainContext iteration may not be enough for the info event to be
emitted by the kernel after the triggering event. Let's wait a bit
longer. This fixes sporadic failures in the affected test cases
depending on the system load.

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2025-02-05 14:18:47 +01:00
Bartosz Golaszewski
f52874b19b dbus: client: tests: fix the way we wait for simulated chips to appear
The org.freedesktop.DBus.Peer.Ping() method isn't correct for checking
if a given object is exposed by a service. It only lets us know if the
service is available no matter the object on which it's called (in fact:
it will not return an error even if it's called on a nonexistent object).

While this has worked for most part by accident, if the timing isn't
right, we may start calling methods in chips which are not yet fully
registered. We should try to read the chip's property instead and keep
on waiting for as long as org.freedesktop.DBus.Properties.Get() returns
an error.

While at it: fix the counter increment as its syntax is invalid.

Fixes: a5ab76da1e ("dbus: add the D-Bus daemon, command-line client and tests")
Link: https://lore.kernel.org/r/20250203-fix-dbus-wait-for-chip-v1-1-563f7132c04f@linaro.org
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2025-02-05 10:50:14 +01:00
Bartosz Golaszewski
b376eaa862 bindings: rust: tests: disable device before dropping the resources
Linux kernel commit 8bd76b3d3f3a ("gpio: sim: lock up configfs that an
instantiated device depends on") uncovered an issue in Rust bindings
tests where the GPIO simulator device is not disabled before removing its
configfs entries. Implenent Drop for the Sim struct in order to disable
the simulator first before dropping its bank and device objects.

Acked-by: Viresh Kumar <viresh.kumar@linaro.org>
Link: https://lore.kernel.org/r/20250203-fix-gpiosim-in-bindings-v1-3-d2c9e0f3a29d@linaro.org
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2025-02-05 10:49:51 +01:00
Bartosz Golaszewski
9949cdaf84 bindings: python: tests: disable device before releasing the bank
Linux kernel commit 8bd76b3d3f3a ("gpio: sim: lock up configfs that an
instantiated device depends on") uncovered an issue in Python bindings
tests where the GPIO simulator device is not disabled before removing its
configfs entries. Reorder the operations in chip_finalize() in order to
disable the device first (if needed) before releasing any other
resources.

Link: https://lore.kernel.org/r/20250203-fix-gpiosim-in-bindings-v1-2-d2c9e0f3a29d@linaro.org
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2025-02-05 10:49:35 +01:00
Bartosz Golaszewski
33a2c3dd58 bindings: cxx: tests: disable GPIO simulator before releasing it
Linux kernel commit 8bd76b3d3f3a ("gpio: sim: lock up configfs that an
instantiated device depends on") uncovered an issue in C++ bindings
tests where the GPIO simulator device is not disabled before removing its
configfs entries. Add a call to gpiosim_dev_disable() to the
gpiosim::chip's destructor.

Link: https://lore.kernel.org/r/20250203-fix-gpiosim-in-bindings-v1-1-d2c9e0f3a29d@linaro.org
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2025-02-05 10:49:35 +01:00
Erik Schilling
d6457b28e2 bindings: rust: libpgiod: fix clippy::empty_line_after_doc_comments lint
clippy warned about this:

    warning: empty line after doc comment
      --> libgpiod/src/event_buffer.rs:15:1
       |
    15 | / /// An iterator over the elements of type `Event`.
    16 | |
       | |_
    17 |   pub struct Events<'a> {
       |   --------------------- the comment documents this struct
       |
       = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#empty_line_after_doc_comments
       = note: `#[warn(clippy::empty_line_after_doc_comments)]` on by default
       = help: if the empty line is unintentional remove it

Signed-off-by: Erik Schilling <erik.schilling@linaro.org>
Link: https://lore.kernel.org/r/20250103-lifetime-fix-v2-3-63902dc8cae1@linaro.org
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2025-01-03 16:40:58 +01:00
Erik Schilling
520f9c9419 bindings: rust: libgpiod: turn standalone doc comment into normal comment
This was complained about by clippy:

    warning: empty line after doc comment
       --> libgpiod/src/lib.rs:461:1
        |
    461 | / /// Various libgpiod-related functions.
    462 | |
        | |_
    ...
    467 |   pub fn is_gpiochip_device<P: AsRef<Path>>(path: &P) -> bool {
        |   ----------------------------------------------------------- the comment documents this function
        |
        = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#empty_line_after_doc_comments
        = help: if the empty line is unintentional remove it
    help: if the documentation should include the empty line include it in the comment
        |
    462 | ///
        |

The comment more seems to be of structural matter and should not be tied
to the function immediately below it. So lets turn it into a normal
non-doc comment.

Signed-off-by: Erik Schilling <erik.schilling@linaro.org>
Link: https://lore.kernel.org/r/20250103-lifetime-fix-v2-2-63902dc8cae1@linaro.org
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2025-01-03 16:40:58 +01:00
Erik Schilling
314aa08822 bindings: rust: libgpiod: make read_edge_events lifetimes more explicit
Newer rustc versions warn on elided named lifetimes. In thise case, it
actually revealed a misleading function signature.

The function defined &self with 'a. But there is no dependency on the
Request reference that extends longer than this function call. Yet,
the code expresses such a dependency and abuses it to force the buffer
argument to match &self lifetime (and thus the return lifetime too!).

Instead we want to explicitly break up the tie to the &self and tie it
to the buffer instead.

The 'a on &self caused a 'a on the return. With that being removed we
would get an automatic '0. So we are now forced to add the 'a on the
return that also fixes the compiler warning.

Reported-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
Co-authored-by: Kent Gibson <warthog618@gmail.com>
Link: https://lore.kernel.org/r/CAMRc=Me-QNmJ2L1K-gGzFtVZacsDiLsNUfh3QaWPdbVzyxUduA@mail.gmail.com/
Signed-off-by: Erik Schilling <erik.schilling@linaro.org>
Link: https://lore.kernel.org/r/20250103-lifetime-fix-v2-1-63902dc8cae1@linaro.org
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2025-01-03 16:40:58 +01:00
Bartosz Golaszewski
38adbae195 bindings: python: provide fileno() for Chip and LineRequest
Certain polling APIs in the standard library - most notably: the
select() function and the poll class - allow to poll any object that
implements the fileno() method returning the underlying file descriptor
number.

Implement fileno() for Chip and LineRequest which allows users to do:

  rd, _, _ = select([chip/request], [], [], 1)

where rd will contain the actual object passed to select which makes for
easier reading of events afterwards.

Reviewed-by: Vincent Fazio <vfazio@xes-inc.com>
Link: https://lore.kernel.org/r/20241211-python-fileno-v2-1-84c642572485@linaro.org
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2024-12-12 15:35:04 +01:00
Bartosz Golaszewski
6d9133a259 bindings: python: fix license tags
Several files under bindings/python/ have wrong license tags.

In general:
- Makefiles should be covered by GPL
- all python sources under bindings/python/gpiod/ and
  bindings/python/tests/ that are part of importable modules should be
  licensed under LGPL
- executable examples should be licensed under GPL

Reported-by: Vincent Fazio <vfazio@xes-inc.com>
Closes: https://github.com/brgl/libgpiod/issues/110
Acked-by: Vincent Fazio <vfazio@xes-inc.com>
Link: https://lore.kernel.org/r/20241127150529.126896-1-brgl@bgdev.pl
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2024-12-05 13:56:15 +01:00
George Harker
7f12d44b31 bindings: python: fix potential PyDict_Next() crash
PyDict_Next() does not guarantee pos is contiguous, and pypy increments
it past the end of the dict size. Fix reliance on pos for constructing
args for libgpiod call.

Fixes: b7ba732e6a ("treewide: libgpiod v2 implementation")
Signed-off-by: George Harker <george@george-graphics.co.uk>
Link: https://lore.kernel.org/r/20241202171302.51437-1-george@george-graphics.co.uk
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2024-12-02 20:45:17 +01:00
Bartosz Golaszewski
27ec1af8b1 bindings: python: relax the verbosity adjustment in build_tests.sh
When building tests using the build_tests.py script, we try to increase
the verbosity using setuptools.logging.configure() preferably but
falling back to distutils.log.set_verbosity() if the former is missing.

This however creates a hard dependency on distutils on older environments
missing the recently added setuptools method. The build however, can work
fine even with default (low) verbosity so instead of bailing out if the
second import fails, just keep going.

Reviewed-by: Vincent Fazio <vfazio@xes-inc.com>
Closes: https://github.com/brgl/libgpiod/issues/109
Link: https://lore.kernel.org/r/20241127133139.120107-1-brgl@bgdev.pl
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2024-12-01 13:54:14 +01:00
Vincent Fazio
1ee51f95c5 bindings: python: migrate metadata to pyproject.toml
PEP 621 [0] designated pyproject.toml as the standard place to store
project metadata.

Setuptools, the build system used by this project, has support for
reading metadata from this location. As such, migrate the majority of
the metadata out of `setup.py`.

For now, the external modules are left declared in `setup.py` because
support within pyproject.toml is still experimental [1].

New metadata has been added/updated, namely:
  * Add Trove classifiers
  * Add a link to the issues page
  * Update license from LGPL-2.1 to LGPL-2.1+ to match SPDX identifier

[0]: https://peps.python.org/pep-0621/
[1]: https://setuptools.pypa.io/en/stable/userguide/ext_modules.html#pyproject-toml

Closes: https://github.com/brgl/libgpiod/issues/107
Signed-off-by: Vincent Fazio <vfazio@xes-inc.com>
Link: https://lore.kernel.org/r/20241122171027.762311-1-vfazio@xes-inc.com
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2024-11-27 11:33:57 +01:00
Bartosz Golaszewski
db19ece3c5 bindings: python: tests: provide and use system.check_kernel_version()
We claim that libgpiod python bindings have no dependencies other than
the python standard library but this is not true - the tests do depend
on deprecated distutils for kernel version parsing.

As distutils is deprecated, the recommended improvement is to use the
Version class from packaging.version but this would too entail pulling
in an external module.

Let's instead implement the kernel version check in C using uname() and
put it into the tests.system extension. This allows us to entirely drop
the distutils import.

Reviewed-by: Vincent Fazio <vfazio@xes-inc.com>
Link: https://lore.kernel.org/r/20241120-drop-distutils-v1-3-7498e8b3babe@linaro.org
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2024-11-25 10:03:54 +01:00
Bartosz Golaszewski
aa5a1e92b1 bindings: python: tests: rename procname to system
We'll be extending the functionality of that C extension with other
OS-level helpers so change its name to 'system'.

Reviewed-by: Vincent Fazio <vfazio@xes-inc.com>
Link: https://lore.kernel.org/r/20241120-drop-distutils-v1-2-7498e8b3babe@linaro.org
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2024-11-25 10:03:50 +01:00
Bartosz Golaszewski
93613618d2 bindings: python: add .pyi files to EXTRA_DIST
While python sdist and wheels are built separately these days, we still
want to package all the extra files in the regular libgpiod release
tarballs. Add new .pyi files to automake's EXTRA_DIST.

Reviewed-by: Vincent Fazio <vfazio@xes-inc.com>
Link: https://lore.kernel.org/r/20241120-drop-distutils-v1-1-7498e8b3babe@linaro.org
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2024-11-25 10:03:45 +01:00
Bartosz Golaszewski
2ede434e67 bindings: python: build: update makefile after renaming of internal.py
The module is now called _internal.py so update the Makefile. Without
this, we're no longer able to generate the distro tarball.

Fixes: b306ce694a ("bindings: python: make internal a private submodule")
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2024-11-20 14:56:34 +01:00
Vincent Fazio
0505dc3643 bindings: python: configure and document dev dependencies
Mypy [0] is a popular static type checker that validates attribute and
variable use and ensures function arguments adhere to type annotations.

Ruff [1] is a popular Rust-based Python linter and code formatter. It
has support for a large set of linting rules [2] and largely complies
with the Black format [3].

Add documentation to README.md for how to run the tools.

[0]: https://mypy.readthedocs.io/en/stable/
[1]: https://docs.astral.sh/ruff/
[2]: https://docs.astral.sh/ruff/rules/
[3]: https://docs.astral.sh/ruff/formatter/#black-compatibility

Signed-off-by: Vincent Fazio <vfazio@xes-inc.com>
Link: https://lore.kernel.org/r/20241114145116.2123714-24-vfazio@xes-inc.com
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2024-11-19 15:23:58 +01:00
Vincent Fazio
3bb5368ba2 bindings: python: tests: selectively use f-strings
Since their inclusion in Python 3.6, f-strings have become the preferred
way to format strings with variable values as they are generally more
readable as the value substitution is in place and doesn't have to be
parsed from the list or arguments to `.format()`.

Where it does not impact readability (when the line is <120 characters),
swap usage of `.format()` to an f-string.

For lines that are not converted, inform the linter to ignore attempts
to upgrade those instances to f-strings [0]

[0]: https://docs.astral.sh/ruff/rules/f-string/

Signed-off-by: Vincent Fazio <vfazio@xes-inc.com>
Link: https://lore.kernel.org/r/20241114145116.2123714-23-vfazio@xes-inc.com
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2024-11-19 15:23:58 +01:00
Vincent Fazio
632ea4df4b bindings: python: tests: ignore purposeful type errors
Some of the unit tests intentionally call methods or assign properties
with invalid values to ensure they fail in an expected way.

Type checkers complain for these instances so inform them via directive
that these lines should be ignore for specific errors.

Additionally, some lines that access properties without assigning the
value look like they perform no action to linters.

To appease the linter, read the value into a throw-away variable.

Signed-off-by: Vincent Fazio <vfazio@xes-inc.com>
Link: https://lore.kernel.org/r/20241114145116.2123714-22-vfazio@xes-inc.com
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2024-11-19 15:23:58 +01:00
Vincent Fazio
f04f58ae1d bindings: python: tests: add type hints to internal members
Add type hints for internal members of the test cases so type checkers
can ensure the code properly constrains to these types and accounts for
scenarios where the values are `None`.

In most cases, the type checker is allowed to assume the inferred type
upon assignment.

An example is any test case that creates a `Chip` as part of its `setUp`
method. Many of these tests subsequently set the reference to `None` in
`tearDown` to free up resources to be collected by the GC.

If the member was "properly" typed to be `Optional`, all references in
the unit tests would need to be guarded by an assert or a cast or
ignored by the type checker. This is noisy and doesn't add value since
for the life of the test it should always be a valid reference.

Instead, allow `tearDown` to violate the inferred type and inform the
type checker to ignore the assignment via directive.

If the tests are ever changed to set the member to something other than
the inferred type outside of `tearDown`, explicit type hints and proper
checks should be added.

Signed-off-by: Vincent Fazio <vfazio@xes-inc.com>
Link: https://lore.kernel.org/r/20241114145116.2123714-21-vfazio@xes-inc.com
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2024-11-19 15:23:58 +01:00
Vincent Fazio
d9313c18a0 bindings: python: tests: add missing method type hints
Add type hints for all method arguments and return values.

Signed-off-by: Vincent Fazio <vfazio@xes-inc.com>
Link: https://lore.kernel.org/r/20241114145116.2123714-20-vfazio@xes-inc.com
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2024-11-19 15:23:58 +01:00
Vincent Fazio
72f2eede08 bindings: python: tests: add type stubs for external modules
Add type stubs for the compiled external modules so that types and
methods used from the modules are accurately type checked.

Signed-off-by: Vincent Fazio <vfazio@xes-inc.com>
Link: https://lore.kernel.org/r/20241114145116.2123714-19-vfazio@xes-inc.com
[Bartosz: add missing SPDX license and copyright tags]
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2024-11-19 15:23:24 +01:00
Vincent Fazio
66c12b72eb bindings: python: tests: make EventType private to prevent export
Previously, both tests_edge_event and tests_info_event had a public
module level EventType member.

The __main__ module for unittests does a star import from both modules
which led to an import conflict between the two.

It is not necessary to import EventType to run unit tests.

Now, EventType is private to avoid being imported via star syntax.

Signed-off-by: Vincent Fazio <vfazio@xes-inc.com>
Link: https://lore.kernel.org/r/20241114145116.2123714-18-vfazio@xes-inc.com
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2024-11-19 15:21:01 +01:00
Vincent Fazio
171577eb41 bindings: python: tests: clean up imports and exports
Remove unused imports and sort the remainder following isort rules.

Update the submodules to use `__all__` to advertise available imports.

Remove an unnecessary blank line in the VersionString class to abide by
the ruff formatter's rules (compatible with black's formatting).

Signed-off-by: Vincent Fazio <vfazio@xes-inc.com>
Link: https://lore.kernel.org/r/20241114145116.2123714-17-vfazio@xes-inc.com
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2024-11-19 15:21:01 +01:00
Vincent Fazio
0f40c01a09 bindings: python: tests: fix duplicate test name
Previously, the `test_rising_edge_event` test was defined twice.

The second instance of the test actually tested the falling edge event
and was shadowing the first test which tested the rising edge event.

Now, the second instance is renamed to `test_falling_edge_event` to
reflect what it is testing and to avoid the naming conflict.

Signed-off-by: Vincent Fazio <vfazio@xes-inc.com>
Link: https://lore.kernel.org/r/20241114145116.2123714-16-vfazio@xes-inc.com
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2024-11-19 15:21:01 +01:00
Vincent Fazio
42308c2df1 bindings: python: selectively use f-strings
Since their inclusion in Python 3.6, f-strings have become the preferred
way to format strings with variable values as they are generally more
readable as the value substitution is in place and doesn't have to be
parsed from the list or arguments to `.format()`.

Where it does not impact readability (when the line is <120 characters),
swap usage of `.format()` to an f-string.

For lines that are not converted, inform the linter to ignore attempts
to upgrade those instances to f-strings [0]

[0]: https://docs.astral.sh/ruff/rules/f-string/

Signed-off-by: Vincent Fazio <vfazio@xes-inc.com>
Link: https://lore.kernel.org/r/20241114145116.2123714-15-vfazio@xes-inc.com
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2024-11-19 15:21:01 +01:00
Vincent Fazio
f3444341f4 bindings: python: cast return value of LineRequest.get_values
The `values` argument of `_ext.Request.get_values` uses a preallocated
`list[None]` as a buffer that is populated with `Value`s by the external
module that are then returned from the function.

Use `cast` to inform the type checker it's a `list[Value]` despite how
it's allocated.

Also, as `lines` is typed as an `Iterable`, there is no guarantee it has
a `__len__` method. Instead, use the size of the `offsets` array to
allocate the buffer.

Signed-off-by: Vincent Fazio <vfazio@xes-inc.com>
Link: https://lore.kernel.org/r/20241114145116.2123714-14-vfazio@xes-inc.com
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2024-11-19 15:21:01 +01:00
Vincent Fazio
fcddb71b45 bindings: python: convert lines to offsets in LineRequest
Remove `_check_line_name` in favor of a new function, `_line_to_offset`,
that converts a line reference to an offset.

This new function helps narrow types and simplifies the iteration logic
that is used to build objects to interface with `_ext.Request`.

Signed-off-by: Vincent Fazio <vfazio@xes-inc.com>
Link: https://lore.kernel.org/r/20241114145116.2123714-13-vfazio@xes-inc.com
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2024-11-19 15:21:01 +01:00