Blame SOURCES/0058-python-change-types-for-RBufferOut-FBuffer-with-Pyth.patch

d0ea73
From f4c25f88b1187bfd1ff73c8f824d9a020716ae79 Mon Sep 17 00:00:00 2001
d0ea73
From: Pino Toscano <ptoscano@redhat.com>
d0ea73
Date: Tue, 22 Jan 2019 13:04:59 +0100
d0ea73
Subject: [PATCH] python: change types for RBufferOut/FBuffer with Python 3
d0ea73
 (RHBZ#1661871)
d0ea73
d0ea73
So far RBufferOut return values, and FBuffer struct fields are 'str' on
d0ea73
all the versions of Python.  Python 3 distinguishes between 'str'
d0ea73
(unicode strings), and 'bytes', with 'str' no more able to hold
d0ea73
arbitrary data.
d0ea73
d0ea73
For this reason, switch the return value of RBufferOut functions, and
d0ea73
FBuffer struct fields to bytes on Python 3: while this is a potentially
d0ea73
incompatibile change, this is the only way to handle safely sequences
d0ea73
of arbitrary bytes.
d0ea73
d0ea73
(cherry picked from commit 0ee02e0117527b86a31b2a88a14994ce7f15571f)
d0ea73
---
d0ea73
 generator/python.ml | 9 +++++++++
d0ea73
 1 file changed, 9 insertions(+)
d0ea73
d0ea73
diff --git a/generator/python.ml b/generator/python.ml
d0ea73
index 8fa0b17c0..a75b5f375 100644
d0ea73
--- a/generator/python.ml
d0ea73
+++ b/generator/python.ml
d0ea73
@@ -195,8 +195,13 @@ and generate_python_structs () =
d0ea73
             pr "    goto err;\n";
d0ea73
             pr "  PyDict_SetItemString (dict, \"%s\", value);\n" name;
d0ea73
         | name, FBuffer ->
d0ea73
+            pr "#if PY_MAJOR_VERSION >= 3\n";
d0ea73
+            pr "  value = PyBytes_FromStringAndSize (%s->%s, %s->%s_len);\n"
d0ea73
+              typ name typ name;
d0ea73
+            pr "#else\n";
d0ea73
             pr "  value = guestfs_int_py_fromstringsize (%s->%s, %s->%s_len);\n"
d0ea73
               typ name typ name;
d0ea73
+            pr "#endif\n";
d0ea73
             pr "  if (value == NULL)\n";
d0ea73
             pr "    goto err;\n";
d0ea73
             pr "  PyDict_SetItemString (dict, \"%s\", value);\n" name;
d0ea73
@@ -511,7 +516,11 @@ and generate_python_actions actions () =
d0ea73
            pr "  guestfs_int_free_string_list (r);\n";
d0ea73
            pr "  if (py_r == NULL) goto out;\n";
d0ea73
        | RBufferOut _ ->
d0ea73
+           pr "#if PY_MAJOR_VERSION >= 3\n";
d0ea73
+           pr "  py_r = PyBytes_FromStringAndSize (r, size);\n";
d0ea73
+           pr "#else\n";
d0ea73
            pr "  py_r = guestfs_int_py_fromstringsize (r, size);\n";
d0ea73
+           pr "#endif\n";
d0ea73
            pr "  free (r);\n";
d0ea73
            pr "  if (py_r == NULL) goto out;\n";
d0ea73
       );
d0ea73
-- 
d0ea73
2.20.1
d0ea73