python: use the more modern PyCapsule over PyCObject (RhBug:623864).
authorAles Kozumplik <akozumpl@redhat.com>
Tue, 15 Nov 2011 14:49:33 +0000 (15:49 +0100)
committerAles Kozumplik <akozumpl@redhat.com>
Fri, 18 Nov 2011 09:53:53 +0000 (10:53 +0100)
- rpm.header.new() will still keep accepting PyCObject for now in case a
  client library depends on this.
- involves macro trickery to make rpm buildable against python 2.6 still.

python/header-py.c
python/rpmsystem-py.h
python/spec-py.c

index 6d6c1e9..ba6a949 100644 (file)
@@ -383,8 +383,8 @@ static PyObject *hdr_new(PyTypeObject *subtype, PyObject *args, PyObject *kwds)
 
     if (obj == NULL) {
        h = headerNew();
-    } else if (PyCObject_Check(obj)) {
-       h = PyCObject_AsVoidPtr(obj);
+    } else if (CAPSULE_CHECK(obj)) {
+       h = CAPSULE_EXTRACT(obj, "rpm._C_Header");
     } else if (hdrObject_Check(obj)) {
        h = headerCopy(((hdrObject*) obj)->h);
     } else if (PyBytes_Check(obj)) {
index 61ec8ce..b730973 100644 (file)
@@ -31,6 +31,16 @@ typedef Py_ssize_t (*lenfunc)(PyObject *);
 #define PyBytes_AsString PyString_AsString
 #endif
 
+#if ((PY_MAJOR_VERSION << 8) | (PY_MINOR_VERSION << 0)) < 0x0207
+#define CAPSULE_BUILD(ptr,name) PyCapsule_New(ptr, name, NULL)
+#define CAPSULE_CHECK(obj) PyCapsule_CheckExact(obj)
+#define CAPSULE_EXTRACT(obj,name) PyCapsule_GetPointer(obj, name)
+#else
+#define CAPSULE_BUILD(ptr,name) PyCObject_FromVoidPtr(ptr, NULL)
+#define CAPSULE_CHECK(obj) PyCObject_Check(obj)
+#define CAPSULE_EXTRACT(obj,name) PyCObject_AsVoidPtr(obj)
+#endif
+
 /* For Python 3, use the PyLong type throughout in place of PyInt */
 #if PY_MAJOR_VERSION >= 3
 #define PyInt_Check PyLong_Check
index a829539..49b9e1d 100644 (file)
@@ -34,7 +34,7 @@ static PyObject *makeHeader(Header h)
     PyObject *rpmmod = PyImport_ImportModuleNoBlock("rpm");
     if (rpmmod == NULL) return NULL;
 
-    PyObject *ptr = PyCObject_FromVoidPtr(h, NULL);
+    PyObject *ptr = CAPSULE_BUILD(h, "rpm._C_Header");
     PyObject *hdr = PyObject_CallMethod(rpmmod, "hdr", "(O)", ptr);
     Py_XDECREF(ptr);
     Py_XDECREF(rpmmod);