Skip to contentSkip to conversation

pylibfdt: fix with Python 3.10

PATCH In review Ross Burton 3 participants 3 messages Nov 11, 2021 – Nov 12, 2021 1 message not in the archive

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 pylibfdt: fix with Python 3.10

Since Python 2.5 the argument parsing functions when parsing expressions such as s# (string plus length) expect the length to be an int or a ssize_t, depending on whether PY_SSIZE_T_CLEAN is defined or not.

Python 3.8 deprecated the use of int, and with Python 3.10 this symbol must be defined and ssize_t used[1].

Define the magic symbol when building the extension, and cast the ints from the libfdt API to ssize_t as appropriate.

[1] https://docs.python.org/3.10/whatsnew/3.10.html#id2

---
 pylibfdt/libfdt.i | 4 ++--
 pylibfdt/setup.py | 1 +
 2 files changed, 3 insertions(+), 2 deletions(-)
pylibfdt/libfdt.i +2 -2
index 51ee801..075ef70 100644
@@ -1044,9 +1044,9 @@ typedef uint32_t fdt32_t;
 		$result = Py_None;
 	else
         %#if PY_VERSION_HEX >= 0x03000000
-            $result = Py_BuildValue("y#", $1, *arg4);
+            $result = Py_BuildValue("y#", $1, (Py_ssize_t)*arg4);
         %#else
-            $result = Py_BuildValue("s#", $1, *arg4);
+            $result = Py_BuildValue("s#", $1, (Py_ssize_t)*arg4);
         %#endif
 }
 
pylibfdt/setup.py +1 -0
index ef40f15..81e161a 100755
@@ -42,6 +42,7 @@ def get_version():
 libfdt_module = Extension(
     '_libfdt',
     sources=[os.path.join(srcdir, 'libfdt.i')],
+    define_macros=[('PY_SSIZE_T_CLEAN', None)],
     include_dirs=[os.path.join(srcdir, '../libfdt')],
     libraries=['fdt'],
     library_dirs=[os.path.join(top_builddir, 'libfdt')],
Signature · 1 line
2.25.1

A message that is not in the archive (sent off-list or not yet received): 20211111160536.2516573-1-ross.burton-5wv7dgnIgG8@public.gmane.org

Tested-by
#2 lore raw
Quoted 11 lines · Ross Burton

Since Python 2.5 the argument parsing functions when parsing expressions such as s# (string plus length) expect the length to be an int or a ssize_t, depending on whether PY_SSIZE_T_CLEAN is defined or not.

Python 3.8 deprecated the use of int, and with Python 3.10 this symbol must be defined and ssize_t used[1].

Define the magic symbol when building the extension, and cast the ints from the libfdt API to ssize_t as appropriate.

Quoted 6 lines

[1] https://docs.python.org/3.10/whatsnew/3.10.html#id2

---
 pylibfdt/libfdt.i | 4 ++--
 pylibfdt/setup.py | 1 +

setup.py just moved up a level.

With that fixed up,

#3 lore raw
In reply to a message not in the archive
Quoted 7 lines · Ross Burton

Since Python 2.5 the argument parsing functions when parsing expressions such as s# (string plus length) expect the length to be an int or a ssize_t, depending on whether PY_SSIZE_T_CLEAN is defined or not.

Python 3.8 deprecated the use of int, and with Python 3.10 this symbol must be defined and ssize_t used[1].

Quoted 6 lines

Define the magic symbol when building the extension, and cast the ints from the libfdt API to ssize_t as appropriate.

[1] https://docs.python.org/3.10/whatsnew/3.10.html#id2

Applied (with the correct to the setup.py location). Thanks!

Quoted 33 lines
---
 pylibfdt/libfdt.i | 4 ++--
 pylibfdt/setup.py | 1 +
 2 files changed, 3 insertions(+), 2 deletions(-)
pylibfdt/libfdt.i +2 -2
index 51ee801..075ef70 100644
@@ -1044,9 +1044,9 @@ typedef uint32_t fdt32_t;
 		$result = Py_None;
 	else
         %#if PY_VERSION_HEX >= 0x03000000
-            $result = Py_BuildValue("y#", $1, *arg4);
+            $result = Py_BuildValue("y#", $1, (Py_ssize_t)*arg4);
         %#else
-            $result = Py_BuildValue("s#", $1, *arg4);
+            $result = Py_BuildValue("s#", $1, (Py_ssize_t)*arg4);
         %#endif
 }
 
pylibfdt/setup.py +1 -0
index ef40f15..81e161a 100755
@@ -42,6 +42,7 @@ def get_version():
 libfdt_module = Extension(
     '_libfdt',
     sources=[os.path.join(srcdir, 'libfdt.i')],
+    define_macros=[('PY_SSIZE_T_CLEAN', None)],
     include_dirs=[os.path.join(srcdir, '../libfdt')],
     libraries=['fdt'],
     library_dirs=[os.path.join(top_builddir, 'libfdt')],
Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson