Python stable ABI: Make calls to `tp_free` go through PyType_GetSlot

The Python type struct changes between versions, and thus direct access to
its fields is not allowed in Stable ABI.
The members can, however, be retrieved using `PyType_GetSlot`.

Commit generated with this semantic patch (see https://coccinelle.lip6.fr ):
```
@@
identifier s;
type t;
@@

- Py_TYPE(s)->tp_free((t*) s);
+ Py_TYPE(s)->tp_free(s);

@@
@@

- Py_TYPE(s)->tp_free(s);
+ freefunc free = PyType_GetSlot(Py_TYPE(s), Py_tp_free);
+ free(s);
```

applied using:

    spatch --sp-file patch.cocci --dir python/ --in-place
This commit is contained in:
Petr Viktorin 2023-01-11 13:25:50 +01:00 committed by Panu Matilainen
parent 5ad6c96285
commit a532a0d8c2
13 changed files with 30 additions and 15 deletions

View File

@ -327,7 +327,8 @@ static PyObject *hdr_new(PyTypeObject *subtype, PyObject *args, PyObject *kwds)
static void hdr_dealloc(hdrObject * s)
{
if (s->h) headerFree(s->h);
Py_TYPE(s)->tp_free((PyObject *)s);
freefunc free = PyType_GetSlot(Py_TYPE(s), Py_tp_free);
free(s);
}
static PyObject * hdr_iternext(hdrObject *s)

View File

@ -21,7 +21,8 @@ static void rpmarchive_dealloc(rpmarchiveObject * s)
rpmfilesFree(s->files);
rpmfiArchiveClose(s->archive);
rpmfiFree(s->archive);
Py_TYPE(s)->tp_free((PyObject *)s);
freefunc free = PyType_GetSlot(Py_TYPE(s), Py_tp_free);
free(s);
}
static PyObject *rpmarchive_error(int rc)

View File

@ -225,7 +225,8 @@ static void
rpmds_dealloc(rpmdsObject * s)
{
s->ds = rpmdsFree(s->ds);
Py_TYPE(s)->tp_free((PyObject *)s);
freefunc free = PyType_GetSlot(Py_TYPE(s), Py_tp_free);
free(s);
}
static Py_ssize_t rpmds_length(rpmdsObject * s)

View File

@ -140,7 +140,8 @@ static void rpmfd_dealloc(rpmfdObject *s)
Py_XDECREF(res);
free(s->mode);
free(s->flags);
Py_TYPE(s)->tp_free((PyObject *)s);
freefunc free = PyType_GetSlot(Py_TYPE(s), Py_tp_free);
free(s);
}
static PyObject *rpmfd_fileno(rpmfdObject *s)

View File

@ -20,7 +20,8 @@ struct rpmfileObject_s {
static void rpmfile_dealloc(rpmfileObject * s)
{
s->files = rpmfilesFree(s->files);
Py_TYPE(s)->tp_free((PyObject *)s);
freefunc free = PyType_GetSlot(Py_TYPE(s), Py_tp_free);
free(s);
}
static char rpmfile_doc[] =
@ -379,7 +380,8 @@ struct rpmfilesObject_s {
static void rpmfiles_dealloc(rpmfilesObject * s)
{
s->files = rpmfilesFree(s->files);
Py_TYPE(s)->tp_free((PyObject *)s);
freefunc free = PyType_GetSlot(Py_TYPE(s), Py_tp_free);
free(s);
}
static PyObject * rpmfiles_new(PyTypeObject * subtype, PyObject *args, PyObject *kwds)

View File

@ -86,7 +86,8 @@ static void rpmii_dealloc(rpmiiObject * s)
s->ii = rpmdbIndexIteratorFree(s->ii);
rpmtdFree(s->keytd);
Py_DECREF(s->ref);
Py_TYPE(s)->tp_free((PyObject *)s);
freefunc free = PyType_GetSlot(Py_TYPE(s), Py_tp_free);
free(s);
}
static int rpmii_bool(rpmiiObject *s)

View File

@ -11,7 +11,8 @@ struct rpmPubkeyObject_s {
static void rpmPubkey_dealloc(rpmPubkeyObject * s)
{
s->pubkey = rpmPubkeyFree(s->pubkey);
Py_TYPE(s)->tp_free((PyObject *)s);
freefunc free = PyType_GetSlot(Py_TYPE(s), Py_tp_free);
free(s);
}
static PyObject *rpmPubkey_new(PyTypeObject *subtype,
@ -108,7 +109,8 @@ struct rpmKeyringObject_s {
static void rpmKeyring_dealloc(rpmKeyringObject * s)
{
rpmKeyringFree(s->keyring);
Py_TYPE(s)->tp_free((PyObject *)s);
freefunc free = PyType_GetSlot(Py_TYPE(s), Py_tp_free);
free(s);
}
static PyObject *rpmKeyring_new(PyTypeObject *subtype,

View File

@ -129,7 +129,8 @@ static void rpmmi_dealloc(rpmmiObject * s)
{
s->mi = rpmdbFreeIterator(s->mi);
Py_DECREF(s->ref);
Py_TYPE(s)->tp_free((PyObject *)s);
freefunc free = PyType_GetSlot(Py_TYPE(s), Py_tp_free);
free(s);
}
static Py_ssize_t rpmmi_length(rpmmiObject * s)

View File

@ -67,7 +67,8 @@ static PyObject *rpmprob_str(rpmProblemObject *s)
static void rpmprob_dealloc(rpmProblemObject *s)
{
s->prob = rpmProblemFree(s->prob);
Py_TYPE(s)->tp_free((PyObject *)s);
freefunc free = PyType_GetSlot(Py_TYPE(s), Py_tp_free);
free(s);
}
PyTypeObject rpmProblem_Type = {

View File

@ -13,7 +13,8 @@ static char strpool_doc[] = "";
static void strpool_dealloc(rpmstrPoolObject *s)
{
s->pool = rpmstrPoolFree(s->pool);
Py_TYPE(s)->tp_free((PyObject *)s);
freefunc free = PyType_GetSlot(Py_TYPE(s), Py_tp_free);
free(s);
}
static PyObject *strpool_new(PyTypeObject *subtype,

View File

@ -838,7 +838,8 @@ static void rpmts_dealloc(rpmtsObject * s)
s->ts = rpmtsFree(s->ts);
Py_XDECREF(s->scriptFd);
Py_XDECREF(s->keyList);
Py_TYPE(s)->tp_free((PyObject *)s);
freefunc free = PyType_GetSlot(Py_TYPE(s), Py_tp_free);
free(s);
}
static PyObject * rpmts_new(PyTypeObject * subtype, PyObject *args, PyObject *kwds)

View File

@ -14,7 +14,8 @@ static char ver_doc[] = "";
static void ver_dealloc(rpmverObject *s)
{
s->ver = rpmverFree(s->ver);
Py_TYPE(s)->tp_free((PyObject *)s);
freefunc free = PyType_GetSlot(Py_TYPE(s), Py_tp_free);
free(s);
}
int verFromPyObject(PyObject *o, rpmver *ver)

View File

@ -144,7 +144,8 @@ spec_dealloc(specObject * s)
if (s->spec) {
s->spec=rpmSpecFree(s->spec);
}
Py_TYPE(s)->tp_free((PyObject *)s);
freefunc free = PyType_GetSlot(Py_TYPE(s), Py_tp_free);
free(s);
}
static PyObject * getSection(rpmSpec spec, int section)