[Rpm-maint] [PATCH] python: use the more modern PyCapsule over PyCObject (RhBug:623864).
Panu Matilainen
pmatilai at laiskiainen.org
Fri Nov 18 08:31:39 UTC 2011
On 11/15/2011 04:56 PM, Ales Kozumplik wrote:
> rpm.header.new() will still keep accepting PyCObject for now in case a
> client library depends on this.
> ---
> python/header-py.c | 3 +++
> python/spec-py.c | 2 +-
> 2 files changed, 4 insertions(+), 1 deletions(-)
>
> diff --git a/python/header-py.c b/python/header-py.c
> index 6d6c1e9..e41e202 100644
> --- a/python/header-py.c
> +++ b/python/header-py.c
> @@ -384,7 +384,10 @@ static PyObject *hdr_new(PyTypeObject *subtype, PyObject *args, PyObject *kwds)
> if (obj == NULL) {
> h = headerNew();
> } else if (PyCObject_Check(obj)) {
> + /* initializating via PyCObject deprecated since rpm-4.9.90 */
> h = PyCObject_AsVoidPtr(obj);
> + } else if (PyCapsule_CheckExact(obj)) {
> + h = PyCapsule_GetPointer(obj, "rpm._C_Header");
> } else if (hdrObject_Check(obj)) {
> h = headerCopy(((hdrObject*) obj)->h);
> } else if (PyBytes_Check(obj)) {
> diff --git a/python/spec-py.c b/python/spec-py.c
> index a829539..9f60626 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 = PyCapsule_New(h, "rpm._C_Header", NULL);
> PyObject *hdr = PyObject_CallMethod(rpmmod, "hdr", "(O)", ptr);
> Py_XDECREF(ptr);
> Py_XDECREF(rpmmod);
The problem with this is that it bumps the python requirement to >= 2.7,
while we've been trying to stay compatible a couple of versions
backwards. In theory the bindings are supposed to be compatible back to
2.5, but support for < 2.6 was broken for quite some time without
anybody noticing so maybe nobody cares for 2.5 and older anymore.
Python 2.7 is relatively new still, I'd like to avoid making that as the
hard requirement yet. My idea wrt this (as mentioned in the bug
comments) was to handle this with wrapper macros, so that PyCapsule is
when available, others fall back to PyCObject. The interfaces are
similar enough that it shouldn't be particularly hard.
I doubt there are any clients relying to pass PyCObjects specifically,
the whole thing was added just to allow our creating headers from our
own build-module. OTOH keeping it alive isn't exactly expensive...
- Panu -
More information about the Rpm-maint
mailing list