Skip to content

Commit

Permalink
Modernise the tp_dealloc functions
Browse files Browse the repository at this point in the history
While in Python 2.7, it was recommended to explicitly check for
errors, and only do the save exception/restore exception dance, modern
Python documentation says to always do this, unconditionally. For
example, in
https://docs.python.org/3.11/extending/newtypes.html#finalization-and-de-allocation.

So let's switch to this, and to the more proper deallocation using the
tp_free member - not because these are subclassable types, but because
the initialisation is also done using tp_alloc, so consistency++.
  • Loading branch information
iustin committed Apr 22, 2023
1 parent e8d6976 commit 522cbed
Showing 1 changed file with 9 additions and 18 deletions.
27 changes: 9 additions & 18 deletions acl.c
Original file line number Diff line number Diff line change
Expand Up @@ -245,15 +245,12 @@ static int ACL_init(PyObject* obj, PyObject* args, PyObject *keywds) {
static void ACL_dealloc(PyObject* obj) {
ACL_Object *self = (ACL_Object*) obj;
PyObject *err_type, *err_value, *err_traceback;
int have_error = PyErr_Occurred() ? 1 : 0;

if (have_error)
PyErr_Fetch(&err_type, &err_value, &err_traceback);
PyErr_Fetch(&err_type, &err_value, &err_traceback);
if(self->acl != NULL && acl_free(self->acl) != 0)
PyErr_WriteUnraisable(obj); /* LCOV_EXCL_LINE */
if (have_error)
PyErr_Restore(err_type, err_value, err_traceback);
PyObject_DEL(self);
PyErr_Restore(err_type, err_value, err_traceback);
Py_TYPE(obj)->tp_free(obj);
}

/* Converts the acl to a text format */
Expand Down Expand Up @@ -825,17 +822,14 @@ static int Entry_init(PyObject* obj, PyObject* args, PyObject *keywds) {
static void Entry_dealloc(PyObject* obj) {
Entry_Object *self = (Entry_Object*) obj;
PyObject *err_type, *err_value, *err_traceback;
int have_error = PyErr_Occurred() ? 1 : 0;

if (have_error)
PyErr_Fetch(&err_type, &err_value, &err_traceback);
PyErr_Fetch(&err_type, &err_value, &err_traceback);
if(self->parent_acl != NULL) {
Py_DECREF(self->parent_acl);
self->parent_acl = NULL;
}
if (have_error)
PyErr_Restore(err_type, err_value, err_traceback);
PyObject_DEL(self);
PyErr_Restore(err_type, err_value, err_traceback);
Py_TYPE(obj)->tp_free(obj);
}

/* Converts the entry to a text format */
Expand Down Expand Up @@ -1144,17 +1138,14 @@ static int Permset_init(PyObject* obj, PyObject* args, PyObject *keywds) {
static void Permset_dealloc(PyObject* obj) {
Permset_Object *self = (Permset_Object*) obj;
PyObject *err_type, *err_value, *err_traceback;
int have_error = PyErr_Occurred() ? 1 : 0;

if (have_error)
PyErr_Fetch(&err_type, &err_value, &err_traceback);
PyErr_Fetch(&err_type, &err_value, &err_traceback);
if(self->parent_entry != NULL) {
Py_DECREF(self->parent_entry);
self->parent_entry = NULL;
}
if (have_error)
PyErr_Restore(err_type, err_value, err_traceback);
PyObject_DEL(self);
PyErr_Restore(err_type, err_value, err_traceback);
Py_TYPE(obj)->tp_free((PyObject *)obj);
}

/* Permset string representation */
Expand Down

0 comments on commit 522cbed

Please sign in to comment.