[Libguestfs] [PATCH v2 1/2] python: fix call of Python handlers of events

Pino Toscano ptoscano at redhat.com
Tue Jan 22 13:37:03 UTC 2019


Make sure to reference the arguments, to make sure they are kept alive
during the function call; this is visible when setting an event handler
for the CLOSE event, and testing it with Python 3.

This does not seem to create a memory leak e.g. with Python 2.

Also, switch away from the quasi-internal PyEval_CallObject to the
public PyObject_CallObject, which takes care of doing safety checks.
---
 python/handle.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/python/handle.c b/python/handle.c
index e84a04195..adba1b823 100644
--- a/python/handle.c
+++ b/python/handle.c
@@ -136,11 +136,12 @@ guestfs_int_py_event_callback_wrapper (guestfs_h *g,
   args = Py_BuildValue ("(Kis#O)",
                         (unsigned PY_LONG_LONG) event, event_handle,
                         buf, buf_len, py_array);
+  Py_INCREF (args);
 
   if (PyEval_ThreadsInitialized ())
     py_save = PyGILState_Ensure ();
 
-  py_r = PyEval_CallObject (py_callback, args);
+  py_r = PyObject_CallObject (py_callback, args);
 
   if (PyEval_ThreadsInitialized ())
     PyGILState_Release (py_save);
-- 
2.20.1




More information about the Libguestfs mailing list