[Rpm-maint] [PATCH] python: use the more modern PyCapsule over PyCObject (RhBug:623864).

Ales Kozumplik akozumpl at redhat.com
Fri Nov 18 09:52:27 UTC 2011


- 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    |    4 ++--
 python/rpmsystem-py.h |   10 ++++++++++
 python/spec-py.c      |    2 +-
 3 files changed, 13 insertions(+), 3 deletions(-)

diff --git a/python/header-py.c b/python/header-py.c
index 6d6c1e9..ba6a949 100644
--- a/python/header-py.c
+++ b/python/header-py.c
@@ -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)) {
diff --git a/python/rpmsystem-py.h b/python/rpmsystem-py.h
index 61ec8ce..b730973 100644
--- a/python/rpmsystem-py.h
+++ b/python/rpmsystem-py.h
@@ -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
diff --git a/python/spec-py.c b/python/spec-py.c
index a829539..49b9e1d 100644
--- a/python/spec-py.c
+++ b/python/spec-py.c
@@ -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);
-- 
1.7.7.3



More information about the Rpm-maint mailing list