diff --git a/9d25b4e5-python-add-simple-wrappers-for-PyObject-string-funct.patch b/9d25b4e5-python-add-simple-wrappers-for-PyObject-string-funct.patch new file mode 100644 index 0000000..d6b11cb --- /dev/null +++ b/9d25b4e5-python-add-simple-wrappers-for-PyObject-string-funct.patch @@ -0,0 +1,261 @@ +From 9d25b4e56471f9c33ea6229a8b620fc800c240f8 Mon Sep 17 00:00:00 2001 +From: Pino Toscano +Date: Tue, 9 May 2017 15:12:40 +0200 +Subject: [PATCH] python: add simple wrappers for PyObject<->string functions + +The current need for #ifdef's based on the presence of +PyString_FromString makes both the OCaml code of the generator, and the +generated C code a mess to read. + +Hence, add three simple wrappers to make both the OCaml, and C code more +readable, and easier to tweak in the future. + +This should be just refactoring, with no actual behaviour changes. + +Thanks to: Matteo Cafasso +--- + generator/python.ml | 72 ++++++++++++----------------------------------------- + python/handle.c | 65 ++++++++++++++++++++++++++--------------------- + 2 files changed, 53 insertions(+), 84 deletions(-) + +diff --git a/generator/python.ml b/generator/python.ml +index 0162733f9..cf0829489 100644 +--- a/generator/python.ml ++++ b/generator/python.ml +@@ -91,6 +91,9 @@ extern PyObject *guestfs_int_py_event_to_string (PyObject *self, PyObject *args) + extern char **guestfs_int_py_get_string_list (PyObject *obj); + extern PyObject *guestfs_int_py_put_string_list (char * const * const argv); + extern PyObject *guestfs_int_py_put_table (char * const * const argv); ++extern PyObject *guestfs_int_py_fromstring (const char *str); ++extern PyObject *guestfs_int_py_fromstringsize (const char *str, size_t size); ++extern char *guestfs_int_py_asstring (PyObject *obj); + + "; + +@@ -178,31 +181,16 @@ and generate_python_structs () = + function + | name, FString -> + pr " PyDict_SetItemString (dict, \"%s\",\n" name; +- pr "#ifdef HAVE_PYSTRING_ASSTRING\n"; +- pr " PyString_FromString (%s->%s));\n" +- typ name; +- pr "#else\n"; +- pr " PyUnicode_FromString (%s->%s));\n" +- typ name; +- pr "#endif\n" ++ pr " guestfs_int_py_fromstring (%s->%s));\n" ++ typ name + | name, FBuffer -> + pr " PyDict_SetItemString (dict, \"%s\",\n" name; +- pr "#ifdef HAVE_PYSTRING_ASSTRING\n"; +- pr " PyString_FromStringAndSize (%s->%s, %s->%s_len));\n" +- typ name typ name; +- pr "#else\n"; +- pr " PyBytes_FromStringAndSize (%s->%s, %s->%s_len));\n" +- typ name typ name; +- pr "#endif\n" ++ pr " guestfs_int_py_fromstringsize (%s->%s, %s->%s_len));\n" ++ typ name typ name + | name, FUUID -> + pr " PyDict_SetItemString (dict, \"%s\",\n" name; +- pr "#ifdef HAVE_PYSTRING_ASSTRING\n"; +- pr " PyString_FromStringAndSize (%s->%s, 32));\n" +- typ name; +- pr "#else\n"; +- pr " PyBytes_FromStringAndSize (%s->%s, 32));\n" +- typ name; +- pr "#endif\n" ++ pr " guestfs_int_py_fromstringsize (%s->%s, 32));\n" ++ typ name + | name, (FBytes|FUInt64) -> + pr " PyDict_SetItemString (dict, \"%s\",\n" name; + pr " PyLong_FromUnsignedLongLong (%s->%s));\n" +@@ -229,15 +217,9 @@ and generate_python_structs () = + pr " PyDict_SetItemString (dict, \"%s\", Py_None);\n" name; + pr " }\n" + | name, FChar -> +- pr "#ifdef HAVE_PYSTRING_ASSTRING\n"; +- pr " PyDict_SetItemString (dict, \"%s\",\n" name; +- pr " PyString_FromStringAndSize (&%s->%s, 1));\n" +- typ name; +- pr "#else\n"; + pr " PyDict_SetItemString (dict, \"%s\",\n" name; +- pr " PyUnicode_FromStringAndSize (&%s->%s, 1));\n" +- typ name; +- pr "#endif\n" ++ pr " guestfs_int_py_fromstringsize (&%s->%s, 1));\n" ++ typ name + ) cols; + pr " return dict;\n"; + pr "};\n"; +@@ -419,13 +401,7 @@ and generate_python_actions actions () = + pr " optargs_s.%s = PyLong_AsLongLong (py_%s);\n" n n; + pr " if (PyErr_Occurred ()) goto out;\n" + | OString _ -> +- pr "#ifdef HAVE_PYSTRING_ASSTRING\n"; +- pr " optargs_s.%s = PyString_AsString (py_%s);\n" n n; +- pr "#else\n"; +- pr " PyObject *bytes;\n"; +- pr " bytes = PyUnicode_AsUTF8String (py_%s);\n" n; +- pr " optargs_s.%s = PyBytes_AS_STRING (bytes);\n" n; +- pr "#endif\n"; ++ pr " optargs_s.%s = guestfs_int_py_asstring (py_%s);\n" n n + | OStringList _ -> + pr " optargs_s.%s = guestfs_int_py_get_string_list (py_%s);\n" n n; + pr " if (!optargs_s.%s) goto out;\n" n; +@@ -480,30 +456,18 @@ and generate_python_actions actions () = + | RBool _ -> pr " py_r = PyLong_FromLong ((long) r);\n" + | RInt64 _ -> pr " py_r = PyLong_FromLongLong (r);\n" + | RConstString _ -> +- pr "#ifdef HAVE_PYSTRING_ASSTRING\n"; +- pr " py_r = PyString_FromString (r);\n"; +- pr "#else\n"; +- pr " py_r = PyUnicode_FromString (r);\n"; +- pr "#endif\n"; ++ pr " py_r = guestfs_int_py_fromstring (r);\n"; + pr " if (py_r == NULL) goto out;\n"; + | RConstOptString _ -> + pr " if (r) {\n"; +- pr "#ifdef HAVE_PYSTRING_ASSTRING\n"; +- pr " py_r = PyString_FromString (r);\n"; +- pr "#else\n"; +- pr " py_r = PyUnicode_FromString (r);\n"; +- pr "#endif\n"; ++ pr " py_r = guestfs_int_py_fromstring (r);\n"; + pr " } else {\n"; + pr " Py_INCREF (Py_None);\n"; + pr " py_r = Py_None;\n"; + pr " }\n"; + pr " if (py_r == NULL) goto out;\n"; + | RString _ -> +- pr "#ifdef HAVE_PYSTRING_ASSTRING\n"; +- pr " py_r = PyString_FromString (r);\n"; +- pr "#else\n"; +- pr " py_r = PyUnicode_FromString (r);\n"; +- pr "#endif\n"; ++ pr " py_r = guestfs_int_py_fromstring (r);\n"; + pr " free (r);\n"; + pr " if (py_r == NULL) goto out;\n"; + | RStringList _ -> +@@ -519,11 +483,7 @@ and generate_python_actions actions () = + pr " py_r = guestfs_int_py_put_table (r);\n"; + pr " guestfs_int_free_string_list (r);\n" + | RBufferOut _ -> +- pr "#ifdef HAVE_PYSTRING_ASSTRING\n"; +- pr " py_r = PyString_FromStringAndSize (r, size);\n"; +- pr "#else\n"; +- pr " py_r = PyBytes_FromStringAndSize (r, size);\n"; +- pr "#endif\n"; ++ pr " py_r = guestfs_int_py_fromstringsize (r, size);\n"; + pr " free (r);\n"; + pr " if (py_r == NULL) goto out;\n"; + ); +diff --git a/python/handle.c b/python/handle.c +index 806408f91..f347c00d1 100644 +--- a/python/handle.c ++++ b/python/handle.c +@@ -241,11 +241,7 @@ guestfs_int_py_event_to_string (PyObject *self, PyObject *args) + return NULL; + } + +-#ifdef HAVE_PYSTRING_ASSTRING +- py_r = PyString_FromString (str); +-#else +- py_r = PyUnicode_FromString (str); +-#endif ++ py_r = guestfs_int_py_fromstring (str); + free (str); + + return py_r; +@@ -298,9 +294,6 @@ guestfs_int_py_get_string_list (PyObject *obj) + { + size_t i, len; + char **r; +-#ifndef HAVE_PYSTRING_ASSTRING +- PyObject *bytes; +-#endif + + assert (obj); + +@@ -321,14 +314,8 @@ guestfs_int_py_get_string_list (PyObject *obj) + return NULL; + } + +- for (i = 0; i < len; ++i) { +-#ifdef HAVE_PYSTRING_ASSTRING +- r[i] = PyString_AsString (PyList_GetItem (obj, i)); +-#else +- bytes = PyUnicode_AsUTF8String (PyList_GetItem (obj, i)); +- r[i] = PyBytes_AS_STRING (bytes); +-#endif +- } ++ for (i = 0; i < len; ++i) ++ r[i] = guestfs_int_py_asstring (PyList_GetItem (obj, i)); + r[len] = NULL; + + return r; +@@ -345,11 +332,7 @@ guestfs_int_py_put_string_list (char * const * const argv) + + list = PyList_New (argc); + for (i = 0; i < argc; ++i) { +-#ifdef HAVE_PYSTRING_ASSTRING +- PyList_SetItem (list, i, PyString_FromString (argv[i])); +-#else +- PyList_SetItem (list, i, PyUnicode_FromString (argv[i])); +-#endif ++ PyList_SetItem (list, i, guestfs_int_py_fromstring (argv[i])); + } + + return list; +@@ -367,15 +350,41 @@ guestfs_int_py_put_table (char * const * const argv) + list = PyList_New (argc >> 1); + for (i = 0; i < argc; i += 2) { + item = PyTuple_New (2); +-#ifdef HAVE_PYSTRING_ASSTRING +- PyTuple_SetItem (item, 0, PyString_FromString (argv[i])); +- PyTuple_SetItem (item, 1, PyString_FromString (argv[i+1])); +-#else +- PyTuple_SetItem (item, 0, PyUnicode_FromString (argv[i])); +- PyTuple_SetItem (item, 1, PyUnicode_FromString (argv[i+1])); +-#endif ++ PyTuple_SetItem (item, 0, guestfs_int_py_fromstring (argv[i])); ++ PyTuple_SetItem (item, 1, guestfs_int_py_fromstring (argv[i+1])); + PyList_SetItem (list, i >> 1, item); + } + + return list; + } ++ ++PyObject * ++guestfs_int_py_fromstring (const char *str) ++{ ++#ifdef HAVE_PYSTRING_ASSTRING ++ return PyString_FromString (str); ++#else ++ return PyUnicode_FromString (str); ++#endif ++} ++ ++PyObject * ++guestfs_int_py_fromstringsize (const char *str, size_t size) ++{ ++#ifdef HAVE_PYSTRING_ASSTRING ++ return PyString_FromStringAndSize (str, size); ++#else ++ return PyString_FromStringAndSize (str, size); ++#endif ++} ++ ++char * ++guestfs_int_py_asstring (PyObject *obj) ++{ ++#ifdef HAVE_PYSTRING_ASSTRING ++ return PyString_AsString (obj); ++#else ++ PyObject *bytes = PyUnicode_AsUTF8String (obj); ++ return PyBytes_AS_STRING (bytes); ++#endif ++} +-- +2.13.2 + diff --git a/appliance.patch b/appliance.patch index e7f4aad..dd4d28a 100644 --- a/appliance.patch +++ b/appliance.patch @@ -13,22 +13,3 @@ Index: libguestfs-1.36.5/appliance/init echo run_bash_with_ctty echo -Index: libguestfs-1.36.5/m4/guestfs_appliance.m4 -=================================================================== ---- libguestfs-1.36.5.orig/m4/guestfs_appliance.m4 -+++ libguestfs-1.36.5/m4/guestfs_appliance.m4 -@@ -97,9 +97,13 @@ AC_MSG_CHECKING([which Linux distro for - if test -f /etc/os-release; then - ( . /etc/os-release && echo $ID | tr '@<:@:lower:@:>@' '@<:@:upper:@:>@' ) >&AS_MESSAGE_LOG_FD - DISTRO="`. /etc/os-release && echo $ID | tr '@<:@:lower:@:>@' '@<:@:upper:@:>@'`" -+ if test -z "$DISTRO"; then -+ ( . /etc/os-release && echo $ID_LIKE | tr '@<:@:lower:@:>@' '@<:@:upper:@:>@' ) >&AS_MESSAGE_LOG_FD -+ DISTRO="`. /etc/os-release && echo $ID_LIKE | tr '@<:@:lower:@:>@' '@<:@:upper:@:>@'`" -+ fi - AS_CASE([$DISTRO], - [FEDORA | RHEL | CENTOS],[DISTRO=REDHAT], -- [OPENSUSE | SLED | SLES],[DISTRO=SUSE], -+ [OPENSUSE | SLED | SLES | SUSE],[DISTRO=SUSE], - [ARCH],[DISTRO=ARCHLINUX]) - elif test -f /etc/debian_version; then - DISTRO=DEBIAN diff --git a/f3f99a09-python-use-right-func-when-PyString_FromStringAndSiz.patch b/f3f99a09-python-use-right-func-when-PyString_FromStringAndSiz.patch new file mode 100644 index 0000000..4952deb --- /dev/null +++ b/f3f99a09-python-use-right-func-when-PyString_FromStringAndSiz.patch @@ -0,0 +1,29 @@ +From f3f99a09eadb17bd80d5939ea00499da5ee1408e Mon Sep 17 00:00:00 2001 +From: Pino Toscano +Date: Tue, 9 May 2017 17:47:32 +0200 +Subject: [PATCH] python: use right func when PyString_FromStringAndSize is not + there + +Fixes commit 9d25b4e56471f9c33ea6229a8b620fc800c240f8. + +Thanks to: Matteo Cafasso +--- + python/handle.c | 2 +- + 1 file changed, 1 insertion(+), 1 deletion(-) + +diff --git a/python/handle.c b/python/handle.c +index f347c00d1..88024e184 100644 +--- a/python/handle.c ++++ b/python/handle.c +@@ -374,7 +374,7 @@ guestfs_int_py_fromstringsize (const char *str, size_t size) + #ifdef HAVE_PYSTRING_ASSTRING + return PyString_FromStringAndSize (str, size); + #else +- return PyString_FromStringAndSize (str, size); ++ return PyUnicode_FromStringAndSize (str, size); + #endif + } + +-- +2.13.2 + diff --git a/libguestfs.changes b/libguestfs.changes index 615cef1..f178929 100644 --- a/libguestfs.changes +++ b/libguestfs.changes @@ -1,3 +1,13 @@ +------------------------------------------------------------------- +Thu Aug 31 15:25:06 UTC 2017 - cbosdonnat@suse.com + +- Fix python binding for python3 + add patches: + * 9d25b4e5-python-add-simple-wrappers-for-PyObject-string-funct.patch + * f3f99a09-python-use-right-func-when-PyString_FromStringAndSiz.patch +- Better fix for SUSE distro detection using ID_LIKE as fallback: + * use-idlike.patch + ------------------------------------------------------------------- Tue Jul 18 14:07:54 UTC 2017 - cbosdonnat@suse.com diff --git a/libguestfs.spec b/libguestfs.spec index 11e4ef9..755f159 100644 --- a/libguestfs.spec +++ b/libguestfs.spec @@ -140,6 +140,11 @@ Summary: Compatibility package for guestfs-tools License: GPL-2.0 Group: System/Filesystems Patch0: 0000-hotfix.patch +# PATCH-FIX-UPSTREAM - python3 fixes +Patch1: 9d25b4e5-python-add-simple-wrappers-for-PyObject-string-funct.patch +Patch2: f3f99a09-python-use-right-func-when-PyString_FromStringAndSiz.patch +# PATCH-FIX-OPENSUSE - pending upstream review, fixing distro detection in OBS +Patch3: use-idlike.patch Patch100: appliance.patch Source0: http://download.libguestfs.org/1.36-stable/libguestfs-%{version}.tar.gz @@ -301,6 +306,7 @@ Requires: guestfs-data >= %{version} Allows lua scripts to directly use libguestfs. %endif # + %if %{with python_bindings} %{?!python_module:%define python_module() python-%{**} python3-%{**}} %package -n python2-libguestfs @@ -550,6 +556,9 @@ It can import a variety of guest operating systems from libvirt-managed hosts. : _ignore_exclusive_arch '%{?_ignore_exclusive_arch}' %setup -q -a 789653 %patch0 -p1 +%patch1 -p1 +%patch2 -p1 +%patch3 -p1 %patch100 -p1 %build diff --git a/use-idlike.patch b/use-idlike.patch new file mode 100644 index 0000000..07d8fc2 --- /dev/null +++ b/use-idlike.patch @@ -0,0 +1,19 @@ +Index: libguestfs-1.36.5/m4/guestfs_appliance.m4 +=================================================================== +--- libguestfs-1.36.5.orig/m4/guestfs_appliance.m4 ++++ libguestfs-1.36.5/m4/guestfs_appliance.m4 +@@ -101,6 +101,14 @@ if test -f /etc/os-release; then + [FEDORA | RHEL | CENTOS],[DISTRO=REDHAT], + [OPENSUSE | SLED | SLES],[DISTRO=SUSE], + [ARCH],[DISTRO=ARCHLINUX]) ++ dnl All SUSE-based distros have ID_LIKE containing 'suse', check for it if ++ dnl ID wasn't helpful. ++ if test -z "$DISTRO"; then ++ DISTRO_LIKE="`. /etc/os-release && echo $ID_LIKE`" ++ if echo $DISTRO_LIKE | tr " " "\n" | grep -i "^SUSE$"; then ++ DISTRO=SUSE ++ fi ++ fi + elif test -f /etc/debian_version; then + DISTRO=DEBIAN + if grep -q 'DISTRIB_ID=Ubuntu' /etc/lsb-release 2>&AS_MESSAGE_LOG_FD; then