On 8/12/19 11:08 AM, Richard W.M. Jones wrote:
Same as the previous commit, but for Python. --- generator/generator | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-)
@@ -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";
Well, in the OCaml case, you were actively using the fact that ptr and user_data might be different, and freeing the second; where in the Python case you are only ever caring about the ptr argument and always passing NULL for the second (meaning you had to write a shim around Py_DecRef, as hidden under the Py_DECREF macro). -- Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3226 Virtualization: qemu.org | libvirt.org