bindings: python: drop dependency on distutils
cover bindings: python: drop dependency on distutils
Python tests depend on the distutils package which not only is an external dependency but is also deprecated. This series replaces it with version parsing implemented in the C extension.
---
Bartosz Golaszewski (3):
bindings: python: add .pyi files to EXTRA_DIST
bindings: python: tests: rename procname to system
bindings: python: tests: provide and use system.check_kernel_version()
bindings/python/MANIFEST.in | 2 +-
bindings/python/build_tests.py | 10 +--
bindings/python/gpiod/Makefile.am | 1 +
bindings/python/tests/Makefile.am | 2 +-
bindings/python/tests/__init__.py | 11 ++-
bindings/python/tests/__main__.py | 4 +-
bindings/python/tests/gpiosim/Makefile.am | 1 +
bindings/python/tests/procname/ext.c | 42 ------------
.../python/tests/{procname => system}/Makefile.am | 1 +
.../python/tests/{procname => system}/__init__.py | 4 +-
.../python/tests/{procname => system}/_ext.pyi | 1 +
bindings/python/tests/system/ext.c | 79 ++++++++++++++++++++++
configure.ac | 2 +-
13 files changed, 100 insertions(+), 60 deletions(-)---
base-commit: 0505dc36435b6d87523f530192d6025fc94222f3
change-id: 20241120-drop-distutils-afcfa1a24c13
Best regards,
Signature · 1 line
Bartosz Golaszewski <[email protected]>
PATCH 1/3 bindings: python: add .pyi files to EXTRA_DIST
From: Bartosz Golaszewski <[email protected]>
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.
--- bindings/python/gpiod/Makefile.am | 1 + bindings/python/tests/gpiosim/Makefile.am | 1 + bindings/python/tests/procname/Makefile.am | 1 + 3 files changed, 3 insertions(+)
bindings/python/gpiod/Makefile.am +1 -0
@@ -6,6 +6,7 @@ SUBDIRS = ext EXTRA_DIST = \ chip_info.py \ chip.py \ + _ext.pyi \ edge_event.py \ exception.py \ info_event.py \
bindings/python/tests/gpiosim/Makefile.am +1 -0
@@ -3,5 +3,6 @@ EXTRA_DIST = \ chip.py \ + _ext.pyi \ ext.c \ __init__.py
bindings/python/tests/procname/Makefile.am +1 -0
@@ -2,5 +2,6 @@ # SPDX-FileCopyrightText: 2022 Bartosz Golaszewski <[email protected]> EXTRA_DIST = \ + _ext.pyi \ ext.c \ __init__.py
Signature · 1 line
2.45.2
PATCH 2/3 bindings: python: tests: rename procname to system
In reply to Bartosz GolaszewskiFrom: Bartosz Golaszewski <[email protected]>
We'll be extending the functionality of that C extension with other OS-level helpers so change its name to 'system'.
---
bindings/python/MANIFEST.in | 2 +-
bindings/python/build_tests.py | 10 +++++-----
bindings/python/tests/Makefile.am | 2 +-
bindings/python/tests/__main__.py | 4 ++--
bindings/python/tests/{procname => system}/Makefile.am | 0
bindings/python/tests/{procname => system}/__init__.py | 0
bindings/python/tests/{procname => system}/_ext.pyi | 0
bindings/python/tests/{procname => system}/ext.c | 2 +-
configure.ac | 2 +-
9 files changed, 11 insertions(+), 11 deletions(-)bindings/python/MANIFEST.in +1 -1
@@ -13,7 +13,7 @@ recursive-include gpiod/ext *.c recursive-include gpiod/ext *.h recursive-include tests/gpiosim *.c -recursive-include tests/procname *.c +recursive-include tests/system *.c recursive-include lib *.c recursive-include lib *.h
bindings/python/build_tests.py +5 -5
@@ -11,7 +11,7 @@ polluted with artefacts in build/ Builds: tests/gpiosim/_ext.<target>.so - tests/procname/_ext.<target>.so + tests/system/_ext.<target>.so """
@@ -61,9 +61,9 @@ gpiosim_ext = Extension( ], ) -procname_ext = Extension( - "tests.procname._ext", - sources=["tests/procname/ext.c"], +system_ext = Extension( + "tests.system._ext", + sources=["tests/system/ext.c"], define_macros=[("_GNU_SOURCE", "1")], extra_compile_args=["-Wall", "-Wextra"], )
@@ -71,7 +71,7 @@ procname_ext = Extension( dist = Distribution( { "name": "gpiod", - "ext_modules": [gpiosim_ext, procname_ext, gpiod_ext], + "ext_modules": [gpiosim_ext, system_ext, gpiod_ext], "version": __version__, "platforms": ["linux"], }
bindings/python/tests/Makefile.am +1 -1
@@ -1,7 +1,7 @@ # SPDX-License-Identifier: LGPL-2.1-or-later # SPDX-FileCopyrightText: 2022 Bartosz Golaszewski <[email protected]> -SUBDIRS = gpiosim procname +SUBDIRS = gpiosim system EXTRA_DIST = \ helpers.py \
bindings/python/tests/__main__.py +2 -2
@@ -4,7 +4,7 @@ import unittest -from . import procname +from .system import set_process_name from .tests_chip import * from .tests_chip_info import * from .tests_edge_event import *
@@ -15,6 +15,6 @@ from .tests_line_request import * from .tests_line_settings import * from .tests_module import * -procname.set_process_name("python-gpiod") +set_process_name("python-gpiod") unittest.main()
bindings/python/tests/system/Makefile.am renamed from bindings/python/tests/procname/Makefile.am +0 -0
bindings/python/tests/system/__init__.py renamed from bindings/python/tests/procname/__init__.py +0 -0
bindings/python/tests/system/_ext.pyi renamed from bindings/python/tests/procname/_ext.pyi +0 -0
bindings/python/tests/system/ext.c renamed from bindings/python/tests/procname/ext.c +1 -1
@@ -32,7 +32,7 @@ static PyMethodDef module_methods[] = {static PyModuleDef module_def = { PyModuleDef_HEAD_INIT, - .m_name ="procname._ext" , + .m_name ="system._ext" , .m_methods = module_methods, };
configure.ac +1 -1
@@ -371,7 +371,7 @@ AC_CONFIG_FILES([Makefile bindings/python/examples/Makefile bindings/python/tests/Makefile bindings/python/tests/gpiosim/Makefile - bindings/python/tests/procname/Makefile + bindings/python/tests/system/Makefile bindings/rust/libgpiod-sys/src/Makefile bindings/rust/libgpiod-sys/Makefile bindings/rust/libgpiod/src/Makefile
Signature · 1 line
2.45.2
PATCH 3/3 bindings: python: tests: provide and use system.check_kernel_version()
In reply to Bartosz GolaszewskiFrom: Bartosz Golaszewski <[email protected]>
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.
--- bindings/python/tests/__init__.py | 11 +++++----- bindings/python/tests/system/__init__.py | 4 ++-- bindings/python/tests/system/_ext.pyi | 1 + bindings/python/tests/system/ext.c | 37 ++++++++++++++++++++++++++++++++ 4 files changed, 45 insertions(+), 8 deletions(-)
bindings/python/tests/__init__.py +5 -6
@@ -1,13 +1,12 @@ # SPDX-License-Identifier: LGPL-2.1-or-later # SPDX-FileCopyrightText: 2022 Bartosz Golaszewski <[email protected]> +# SPDX-FileCopyrightText: 2024 Bartosz Golaszewski <[email protected]> -import os -from distutils.version import LooseVersion +from .system import check_kernel_version -required_kernel_version = LooseVersion("5.19.0") -current_version = LooseVersion(os.uname().release.split("-")[0]) +_required_kernel_version = (5, 19, 0) -if current_version < required_kernel_version: +if not check_kernel_version(*_required_kernel_version): raise NotImplementedError( - f"linux kernel version must be at least {required_kernel_version} - got {current_version}" + f"linux kernel version must be at least v{'.'.join([str(i) for i in _required_kernel_version])}" )
bindings/python/tests/system/__init__.py +2 -2
@@ -1,6 +1,6 @@ # SPDX-License-Identifier: GPL-2.0-or-later # SPDX-FileCopyrightText: 2022 Bartosz Golaszewski <[email protected]> -from ._ext import set_process_name +from ._ext import check_kernel_version, set_process_name -__all__ = ["set_process_name"] +__all__ = ["check_kernel_version", "set_process_name"]
bindings/python/tests/system/_ext.pyi +1 -0
@@ -2,3 +2,4 @@ # SPDX-FileCopyrightText: 2024 Vincent Fazio <[email protected]> def set_process_name(name: str) -> None: ... +def check_kernel_version(major: int, minor: int, release: int) -> bool: ...
bindings/python/tests/system/ext.c +37 -0
@@ -1,8 +1,11 @@// SPDX-License-Identifier: LGPL-2.1-or-later // SPDX-FileCopyrightText: 2022 Bartosz Golaszewski <[email protected]> +// SPDX-FileCopyrightText: 2024 Bartosz Golaszewski <[email protected]> #include <Python.h> +#include <linux/version.h> #include <sys/prctl.h> +#include <sys/utsname.h> static PyObject *module_set_process_name (PyObject *Py_UNUSED (self ),PyObject *args)
@@ -21,12 +24,46 @@ module_set_process_name(PyObject *Py_UNUSED(self), PyObject *args) Py_RETURN_NONE; } +static PyObject * +module_check_kernel_version (PyObject *Py_UNUSED (self ),PyObject *args) +{ +unsigned int req_maj, req_min, req_rel, curr_maj, curr_min, curr_rel; +struct utsname un; +int ret; + + ret =PyArg_ParseTuple (args,"III" , &req_maj, &req_min, &req_rel); +if (!ret) +return NULL ; + + ret =uname (&un); +if (ret) +return PyErr_SetFromErrno (PyExc_OSError); + + ret =sscanf (un.release,"%u.%u.%u" , &curr_maj, &curr_min, &curr_rel); +if (ret !=3 ) { +PyErr_SetString (PyExc_RuntimeError, +"invalid linux version read from the kernel" ); +return NULL ; + } + +if (KERNEL_VERSION (curr_maj, curr_min, curr_rel) < +KERNEL_VERSION (req_maj, req_min, req_rel)) + Py_RETURN_FALSE; + + Py_RETURN_TRUE; +} +static PyMethodDef module_methods[] = { { .ml_name ="set_process_name" , .ml_meth = (PyCFunction )module_set_process_name, .ml_flags =METH_VARARGS , }, + { + .ml_name ="check_kernel_version" , + .ml_meth = (PyCFunction )module_check_kernel_version, + .ml_flags =METH_VARARGS , + }, { } };
Signature · 1 line
2.45.2
Bartosz Golaszewski
AppliedFrom: Bartosz Golaszewski <[email protected]>
Quoted 5 lines · Bartosz Golaszewski
Python tests depend on the distutils package which not only is an external dependency but is also deprecated. This series replaces it with version parsing implemented in the C extension.
Applied, thanks!
[1/3] bindings: python: add .pyi files to EXTRA_DIST
commit: 93613618d25d257d387fe146cb5e8edb1da728b0
[2/3] bindings: python: tests: rename procname to system
commit: aa5a1e92b1487f8ebaa71e78537226a36dd70632
[3/3] bindings: python: tests: provide and use system.check_kernel_version()
commit: db19ece3c5d5cf5f4f9af6473285de84a6a7e388Best regards,
Signature · 1 line
Bartosz Golaszewski <[email protected]>