Python: Use new PyCapsule API where supported.
authorRichard Jones <rjones@redhat.com>
Tue, 17 Aug 2010 09:31:39 +0000 (10:31 +0100)
committerRichard Jones <rjones@redhat.com>
Tue, 17 Aug 2010 09:31:39 +0000 (10:31 +0100)
See:
http://lists.fedoraproject.org/pipermail/devel/2010-August/141064.html

configure.ac
src/generator.ml

index a14dfd9..1922773 100644 (file)
@@ -551,6 +551,11 @@ if test "x$PYTHON" != "xno"; then
         fi
         AC_MSG_RESULT([not found])
     done
         fi
         AC_MSG_RESULT([not found])
     done
+
+    old_LIBS="$LIBS"
+    LIBS="$LIBS -lpython$PYTHON_VERSION"
+    AC_CHECK_FUNCS([PyCapsule_New])
+    LIBS="$old_LIBS"
 fi
 
 AC_SUBST(PYTHON_PREFIX)
 fi
 
 AC_SUBST(PYTHON_PREFIX)
index 52e7aba..a3333ed 100755 (executable)
@@ -9496,25 +9496,35 @@ typedef int Py_ssize_t;
 
 #include \"guestfs.h\"
 
 
 #include \"guestfs.h\"
 
+#ifndef HAVE_PYCAPSULE_NEW
 typedef struct {
   PyObject_HEAD
   guestfs_h *g;
 } Pyguestfs_Object;
 typedef struct {
   PyObject_HEAD
   guestfs_h *g;
 } Pyguestfs_Object;
+#endif
 
 static guestfs_h *
 get_handle (PyObject *obj)
 {
   assert (obj);
   assert (obj != Py_None);
 
 static guestfs_h *
 get_handle (PyObject *obj)
 {
   assert (obj);
   assert (obj != Py_None);
+#ifndef HAVE_PYCAPSULE_NEW
   return ((Pyguestfs_Object *) obj)->g;
   return ((Pyguestfs_Object *) obj)->g;
+#else
+  return (guestfs_h*) PyCapsule_GetPointer(obj, \"guestfs_h\");
+#endif
 }
 
 static PyObject *
 put_handle (guestfs_h *g)
 {
   assert (g);
 }
 
 static PyObject *
 put_handle (guestfs_h *g)
 {
   assert (g);
+#ifndef HAVE_PYCAPSULE_NEW
   return
     PyCObject_FromVoidPtrAndDesc ((void *) g, (char *) \"guestfs_h\", NULL);
   return
     PyCObject_FromVoidPtrAndDesc ((void *) g, (char *) \"guestfs_h\", NULL);
+#else
+  return PyCapsule_New ((void *) g, \"guestfs_h\", NULL);
+#endif
 }
 
 /* This list should be freed (but not the strings) after use. */
 }
 
 /* This list should be freed (but not the strings) after use. */
@@ -9608,6 +9618,9 @@ py_guestfs_create (PyObject *self, PyObject *args)
     return NULL;
   }
   guestfs_set_error_handler (g, NULL, NULL);
     return NULL;
   }
   guestfs_set_error_handler (g, NULL, NULL);
+  /* This can return NULL, but in that case put_handle will have
+   * set the Python error string.
+   */
   return put_handle (g);
 }
 
   return put_handle (g);
 }