[Libguestfs] [PATCH libnbd 6/7] python: Use free callback to free closure root.

Richard W.M. Jones rjones at redhat.com
Mon Aug 12 16:08:50 UTC 2019


Same as the previous commit, but for Python.
---
 generator/generator | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)

diff --git a/generator/generator b/generator/generator
index 109fad6..a0322ee 100755
--- a/generator/generator
+++ b/generator/generator
@@ -4315,9 +4315,6 @@ let print_python_binding name { args; optargs; ret; may_set_error } =
        ) cbargs;
        pr "  }\n";
        pr "\n";
-       pr "  if (valid_flag & LIBNBD_CALLBACK_FREE)\n";
-       pr "    Py_DECREF ((PyObject *)user_data);\n";
-       pr "\n";
        pr "  return ret;\n";
        pr "}\n";
        pr "\n"
@@ -4454,6 +4451,11 @@ let print_python_binding name { args; optargs; ret; may_set_error } =
        pr "    PyErr_SetString (PyExc_TypeError,\n";
        pr "                     \"callback parameter %s is not callable\");\n" cbname;
        pr "    return NULL;\n";
+       pr "  }\n";
+       pr "  if (nbd_add_free_callback (h, %s_user_data,\n" cbname;
+       pr "                             decref, NULL) == -1) {\n";
+       pr "    PyErr_NoMemory ();\n";
+       pr "    return NULL;\n";
        pr "  }\n"
     | Enum _ -> ()
     | Flags (n, _) -> pr "  %s_u32 = %s;\n" n n
@@ -4600,6 +4602,12 @@ let generate_python_methods_c () =
   pr "\n";
   pr "#include <methods.h>\n";
   pr "\n";
+  pr "static void\n";
+  pr "decref (void *ptr, void *user_data)\n";
+  pr "{\n";
+  pr "  Py_DECREF (ptr);\n";
+  pr "}\n";
+  pr "\n";
   List.iter (
     fun (name, fn) ->
       print_python_binding name fn
-- 
2.22.0




More information about the Libguestfs mailing list