]> git.ipfire.org Git - pakfire.git/commitdiff
Throw a clean exception when Pakfire was initialised with an invalid architecture
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 13 Jan 2021 11:37:17 +0000 (11:37 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 13 Jan 2021 11:37:17 +0000 (11:37 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/_pakfire/pakfire.c
src/libpakfire/pakfire.c

index f2f4990f5045c6a51c471376b0818f034d6e1359..6c107a757976c48728854127ec097b0fe98f9a9d 100644 (file)
@@ -19,6 +19,7 @@
 #############################################################################*/
 
 #include <Python.h>
+#include <errno.h>
 
 #include <pakfire/constants.h>
 #include <pakfire/execute.h>
@@ -52,12 +53,23 @@ static int Pakfire_init(PakfireObject* self, PyObject* args, PyObject* kwds) {
                return -1;
 
        // Create a new Pakfire instance
-    self->pakfire = pakfire_create(path, arch);
+       self->pakfire = pakfire_create(path, arch);
+       if (!self->pakfire) {
+               switch (errno) {
+                       // Invalid architecture
+                       case -EINVAL:
+                               PyErr_SetString(PyExc_ValueError, "Invalid architecture");
+                               break;
+
+                       // Anything else
+                       default:
+                               PyErr_SetNone(PyExc_OSError);
+               }
 
-       if (self->pakfire)
-               return 0;
-       else
                return -1;
+    }
+
+       return 0;
 }
 
 static void Pakfire_dealloc(PakfireObject* self) {
index 07ff444d3cb5c5b39b577bf7bb3f31f6bc5dfbd0..4dfbdcfd735679dcedc878cc8afb3ded33921bb9 100644 (file)
@@ -87,8 +87,10 @@ PAKFIRE_EXPORT Pakfire pakfire_create(const char* path, const char* arch) {
                arch = pakfire_arch_native();
 
        // Check if the architecture is supported
-       if (!pakfire_arch_supported(arch))
+       if (!pakfire_arch_supported(arch)) {
+               errno = -EINVAL;
                return NULL;
+       }
 
        Pakfire pakfire = pakfire_calloc(1, sizeof(*pakfire));
        if (pakfire) {