From 43e946dd497cc6ff0067b8a8f85c620376dfd4cd Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Mon, 9 Nov 2020 18:19:31 +0100 Subject: [PATCH 1/2] Build the Python extension with PY_SSIZE_T_CLEAN The Python extension module now uses Py_ssize_t rather than int for string lengths. This change makes the extension compatible with Python 3.10. Fixes #203. --- python/generator.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) From b3db67629465823f042a5f3303ecdf8e4bd09a76 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Tue, 10 Nov 2020 15:42:36 +0100 Subject: [PATCH 2/2] Convert python/libxml.c to PY_SSIZE_T_CLEAN Define PY_SSIZE_T_CLEAN macro in python/libxml.c and cast the string length (int len) explicitly to Py_ssize_t when passing a string to a function call using PyObject_CallMethod() with the "s#" format. --- python/libxml.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/python/generator.py b/python/generator.py index c0cb3add..59d45e00 100755 --- a/python/generator.py +++ b/python/generator.py @@ -393,7 +393,7 @@ def print_function_wrapper(name, output, export, include): format_args = format_args + ", &%s" % (arg[0]) if f == 's#': format_args = format_args + ", &py_buffsize%d" % num_bufs - c_args = c_args + " int py_buffsize%d;\n" % num_bufs + c_args = c_args + " Py_ssize_t py_buffsize%d;\n" % num_bufs num_bufs = num_bufs + 1 if c_call != "": c_call = c_call + ", " @@ -555,6 +555,7 @@ def buildStubs(): export.write("/* Generated */\n\n") wrapper = open("libxml2-py.c", "w") wrapper.write("/* Generated */\n\n") + wrapper.write("#define PY_SSIZE_T_CLEAN\n") wrapper.write("#include \n") wrapper.write("#include \n") wrapper.write("#include \n") diff --git a/python/libxml.c b/python/libxml.c index 81e709f3..3b66bd61 100644 --- a/python/libxml.c +++ b/python/libxml.c @@ -11,6 +11,7 @@ * * daniel@veillard.com */ +#define PY_SSIZE_T_CLEAN #include #include /* #include "config.h" */ @@ -1048,10 +1049,10 @@ pythonCharacters(void *user_data, const xmlChar * ch, int len) if (type != 0) { if (type == 1) result = PyObject_CallMethod(handler, (char *) "characters", - (char *) "s#", ch, len); + (char *) "s#", ch, (Py_ssize_t)len); else if (type == 2) result = PyObject_CallMethod(handler, (char *) "data", - (char *) "s#", ch, len); + (char *) "s#", ch, (Py_ssize_t)len); if (PyErr_Occurred()) PyErr_Print(); Py_XDECREF(result); @@ -1078,11 +1079,11 @@ pythonIgnorableWhitespace(void *user_data, const xmlChar * ch, int len) result = PyObject_CallMethod(handler, (char *) "ignorableWhitespace", - (char *) "s#", ch, len); + (char *) "s#", ch, (Py_ssize_t)len); else if (type == 2) result = PyObject_CallMethod(handler, (char *) "data", - (char *) "s#", ch, len); + (char *) "s#", ch, (Py_ssize_t)len); Py_XDECREF(result); } } @@ -1223,11 +1224,11 @@ pythonCdataBlock(void *user_data, const xmlChar * ch, int len) if (type == 1) result = PyObject_CallMethod(handler, (char *) "cdataBlock", - (char *) "s#", ch, len); + (char *) "s#", ch, (Py_ssize_t)len); else if (type == 2) result = PyObject_CallMethod(handler, (char *) "cdata", - (char *) "s#", ch, len); + (char *) "s#", ch, (Py_ssize_t)len); if (PyErr_Occurred()) PyErr_Print(); Py_XDECREF(result); -- 2.26.2