From: Michael Tremer Date: Mon, 27 Jan 2025 16:24:44 +0000 (+0000) Subject: python: Minor code cleanups in the Package class X-Git-Tag: 0.9.30~347 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3b5ab877944cde9283b18689f7711d969663d612;p=pakfire.git python: Minor code cleanups in the Package class Signed-off-by: Michael Tremer --- diff --git a/src/python/package.c b/src/python/package.c index 3813cc3e..bec769c9 100644 --- a/src/python/package.c +++ b/src/python/package.c @@ -54,8 +54,8 @@ static PyObject* Package_new(PyTypeObject* type, PyObject* args, PyObject* kwds) } static void Package_dealloc(PackageObject* self) { - pakfire_package_unref(self->package); - + if (self->package) + pakfire_package_unref(self->package); if (self->ctx) pakfire_ctx_unref(self->ctx); @@ -63,21 +63,23 @@ static void Package_dealloc(PackageObject* self) { } static int Package_init(PackageObject* self, PyObject* args, PyObject* kwds) { - PakfireObject* pakfire; - RepoObject* repo; + PakfireObject* pakfire = NULL; + RepoObject* repo = NULL; const char* name = NULL; const char* evr = NULL; const char* arch = NULL; + int r; - if (!PyArg_ParseTuple(args, "O!O!sss", &PakfireType, &pakfire, &RepoType, &repo, - &name, &evr, &arch)) + if (!PyArg_ParseTuple(args, "O!O!sss", + &PakfireType, &pakfire, &RepoType, &repo, &name, &evr, &arch)) return -1; // Store a reference to the context self->ctx = pakfire_ctx_ref(pakfire->ctx); - int r = pakfire_package_create(&self->package, pakfire->pakfire, repo->repo, name, evr, arch); - if (r) { + // Create the package object + r = pakfire_package_create(&self->package, pakfire->pakfire, repo->repo, name, evr, arch); + if (r < 0) { PyErr_SetFromErrno(PyExc_OSError); return -1; } @@ -91,27 +93,36 @@ static Py_hash_t Package_hash(PackageObject* self) { static PyObject* Package_repr(PackageObject* self) { const char* nevra = pakfire_package_get_string(self->package, PAKFIRE_PKG_NEVRA); + if (!nevra) { + PyErr_SetFromErrno(PyExc_OSError); + return NULL; + } return PyUnicode_FromFormat("<_pakfire.Package %s>", nevra); } static PyObject* Package_str(PackageObject* self) { const char* nevra = pakfire_package_get_string(self->package, PAKFIRE_PKG_NEVRA); + if (!nevra) { + PyErr_SetFromErrno(PyExc_OSError); + return NULL; + } return PyUnicode_FromString(nevra); } -static PyObject* Package_richcompare(PackageObject* self, PyObject* _other, int op) { - if (!PyType_IsSubtype(_other->ob_type, &PackageType)) { +static PyObject* Package_richcompare(PackageObject* self, PyObject* o, int op) { + PackageObject* other = (PackageObject *)o; + + // Check if the other object is of the correct type + if (!PyType_IsSubtype(o->ob_type, &PackageType)) { PyErr_SetString(PyExc_TypeError, "Expected a Package object"); return NULL; } - PackageObject* other = (PackageObject *)_other; - long result = pakfire_package_cmp(self->package, other->package); - switch (op) { + switch (result) { case Py_EQ: if (result == 0) Py_RETURN_TRUE; @@ -150,13 +161,6 @@ static PyObject* Package_richcompare(PackageObject* self, PyObject* _other, int Py_RETURN_FALSE; } -static const char* PyUnicode_FromValue(PyObject* value) { - if (value == Py_None) - return NULL; - - return PyUnicode_AsUTF8(value); -} - static PyObject* Package_get_name(PackageObject* self) { const char* name = pakfire_package_get_string(self->package, PAKFIRE_PKG_NAME); if (!name)