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

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