bindings: python: include Python.h before other headers
PATCH v2 11/14 bindings: python: include Python.h before other headers
Per the Python C API reference manual [0], Python.h should be included before other headers since it specifies some pre-processor definitions that may differ from system defaults.
This was noticed when testing on CPython 3.15 [1] which emitted warnings due to redefinitions of _XOPEN_SOURCE and _POSIX_C_SOURCE during builds of the Python bindings.
Note: despite the manual stating that PY_SSIZE_T_CLEAN should also be defined, it is intentionally not included. The Python bindings do not use the # format specifier when parsing any arguments so defining it provides no value [2] and it is deprecated in future versions.
[0]: https://docs.python.org/3/c-api/intro.html#include-files
[1]: https://github.com/python/cpython/commit/927eb448aad6436d794ec9f55780013e25609600
[2]: https://docs.python.org/3/c-api/arg.html#strings-and-buffers
---
Changes in v2
- Added white space to separate the Python.h include from others (Bartosz)
- Added a comment beside the include to provide rationale (vfazio)
- Expanded to include tests/system/ext.c for consistency (vfazio)
--- bindings/python/gpiod/ext/internal.h | 4 +++- bindings/python/gpiod/ext/module.c | 4 +++- bindings/python/tests/gpiosim/ext.c | 4 +++- bindings/python/tests/system/ext.c | 2 ++ 4 files changed, 11 insertions(+), 3 deletions(-)
bindings/python/gpiod/ext/internal.h +3 -1
@@ -4,9 +4,11 @@#ifndef __LIBGPIOD_PYTHON_MODULE_H__#define __LIBGPIOD_PYTHON_MODULE_H__ -#include <gpiod.h> +/* Import Python.h before other headers per API documentation */ #include <Python.h> +#include <gpiod.h> +PyObject *Py_gpiod_SetErrFromErrno (void );unsigned int Py_gpiod_PyLongAsUnsignedInt (PyObject *pylong);void Py_gpiod_dealloc (PyObject *self);
bindings/python/gpiod/ext/module.c +3 -1
@@ -1,9 +1,11 @@// SPDX-License-Identifier: LGPL-2.1-or-later // SPDX-FileCopyrightText: 2022 Bartosz Golaszewski <[email protected]> -#include <gpiod.h> +/* Import Python.h before other headers per API documentation */ #include <Python.h> +#include <gpiod.h> +struct module_const {const char *name;long val;
bindings/python/tests/gpiosim/ext.c +3 -1
@@ -1,9 +1,11 @@// SPDX-License-Identifier: LGPL-2.1-or-later // SPDX-FileCopyrightText: 2022 Bartosz Golaszewski <[email protected]> -#include <gpiosim.h> +/* Import Python.h before other headers per API documentation */ #include <Python.h> +#include <gpiosim.h> +struct module_const {const char *name;long val;
bindings/python/tests/system/ext.c +2 -0
@@ -2,7 +2,9 @@// SPDX-FileCopyrightText: 2022 Bartosz Golaszewski <[email protected]> // SPDX-FileCopyrightText: 2024 Bartosz Golaszewski <[email protected]> +/* Import Python.h before other headers per API documentation */ #include <Python.h> +#include <linux/version.h> #include <sys/prctl.h> #include <sys/utsname.h>
Signature · 1 line
2.43.0
Bartosz Golaszewski
maintainerQuoted 36 lines · Vincent Fazio
Per the Python C API reference manual [0],
Python.hshould be included before other headers since it specifies some pre-processor definitions that may differ from system defaults.This was noticed when testing on CPython 3.15 [1] which emitted warnings due to redefinitions of
_XOPEN_SOURCEand_POSIX_C_SOURCEduring builds of the Python bindings.Note: despite the manual stating that
PY_SSIZE_T_CLEANshould also be defined, it is intentionally not included. The Python bindings do not use the#format specifier when parsing any arguments so defining it provides no value [2] and it is deprecated in future versions.[0]: https://docs.python.org/3/c-api/intro.html#include-files
[1]: https://github.com/python/cpython/commit/927eb448aad6436d794ec9f55780013e25609600
[2]: https://docs.python.org/3/c-api/arg.html#strings-and-buffersSigned-off-by Vincent Fazio <[email protected]>---
Changes in v2
- Added white space to separate thePython.hinclude from others (Bartosz)
- Added a comment beside the include to provide rationale (vfazio)
- Expanded to includetests/system/ext.cfor consistency (vfazio)--- bindings/python/gpiod/ext/internal.h | 4 +++- bindings/python/gpiod/ext/module.c | 4 +++- bindings/python/tests/gpiosim/ext.c | 4 +++- bindings/python/tests/system/ext.c | 2 ++ 4 files changed, 11 insertions(+), 3 deletions(-)bindings/python/gpiod/ext/internal.h +0
-0
bindings/python/gpiod/ext/internal.h line 9
Quoted 7 lines
bindings/python/gpiod/ext/internal.h +1
-1@@ -4,9 +4,11 @@#ifndef __LIBGPIOD_PYTHON_MODULE_H__#define __LIBGPIOD_PYTHON_MODULE_H__ -#include <gpiod.h> +/* Import Python.h before other headers per API documentation */ #include <Python.h>
No need for this comment, I think the break alone tells the story.
Bart