[Rpm-maint] [PATCH 06/15] Update module initialization to work with both Python 2.* and Python 3.*
Panu Matilainen
pmatilai at redhat.com
Mon Oct 19 11:19:46 UTC 2009
On Thu, 15 Oct 2009, David Malcolm wrote:
> Introduce macros and conditional compilation to deal with the major
> changes to the way that Python extension modules are initialized between
> Python 2 and 3 (PEP 3121).
Hmm, this is starting to look a bit more painful :)
> diff --git a/python/header-py.h b/python/header-py.h
> index 8c9dd68..277ef96 100644
> --- a/python/header-py.h
> +++ b/python/header-py.h
> @@ -12,6 +12,13 @@ extern PyTypeObject hdr_Type;
> #define DEPRECATED_METHOD(_msg) \
> PyErr_WarnEx(PyExc_PendingDeprecationWarning, (_msg), 2);
>
> +/*
> + FIXME: this is "global" module state and for Python 3 this ought to be split
> + out into a module state struct as per PEP 3121
> +
> + Unfortunately we don't have convenient module ptrs available at each point
> + of use, so I've punted it for now.
> +*/
> extern PyObject * pyrpmError;
FWIW, the current plan is to push much more work over to python side where
possible, ie make the C-level bindings just a thin layer over the librpm C
API and pythonize things in python instead. The rpm-specific exception(s)
is one of the things I'd like to push over to python side completely. Not
possible yet though, but among other things it would avoid having to deal
with these incompatibilities.
[...]
I didn't try out what it'd actually look like, but it'd seem to me that
the module initialization version differences could be made more obvious
(less #ifdefs) by splitting out much of the init work into a separate
function, ie something like
static int initModule(PyObject *m)
{
/* ... init the types, dicts et ... */
/* return 0/1 on error/success */
}
#if PY_MAJOR_VERSION >= 3
static struct PyModuleDef moduledef = {
...
}
...
PyObject * PyInit__rpm(void)
{
PyObject *m = PyModule_Create(&moduledef);
if (m == NULL || initModule(m) == 0) {
Py_XDECREF(m);
m = NULL;
}
return m;
}
#else
void init_rpm(void)
{
PyObject *m = Py_InitModule3("_rpm", rpmModuleMethods, rpm__doc__);
if (m) initModule(m);
return;
}
#endif
- Panu -
More information about the Rpm-maint
mailing list