]> git.ipfire.org Git - people/ms/libloc.git/commitdiff
python: Correctly raise any errors when opening the database
authorMichael Tremer <michael.tremer@ipfire.org>
Mon, 22 Aug 2022 13:45:44 +0000 (13:45 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Mon, 22 Aug 2022 13:45:44 +0000 (13:45 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/python/database.c

index f5797dbb31d8ef2b279d5dfffad17a7e6de02a6e..de450448e7f752b2855732663b6ddfb81c7bd06c 100644 (file)
@@ -45,28 +45,36 @@ static void Database_dealloc(DatabaseObject* self) {
 
 static int Database_init(DatabaseObject* self, PyObject* args, PyObject* kwargs) {
        const char* path = NULL;
+       FILE* f = NULL;
 
+       // Parse arguments
        if (!PyArg_ParseTuple(args, "s", &path))
                return -1;
 
+       // Copy path
        self->path = strdup(path);
+       if (!self->path)
+               goto ERROR;
 
        // Open the file for reading
-       FILE* f = fopen(self->path, "r");
-       if (!f) {
-               PyErr_SetFromErrno(PyExc_IOError);
-               return -1;
-       }
+       f = fopen(self->path, "r");
+       if (!f)
+               goto ERROR;
 
        // Load the database
        int r = loc_database_new(loc_ctx, &self->db, f);
-       fclose(f);
-
-       // Return on any errors
        if (r)
-               return -1;
+               goto ERROR;
 
+       fclose(f);
        return 0;
+
+ERROR:
+       if (f)
+               fclose(f);
+
+       PyErr_SetFromErrno(PyExc_OSError);
+       return -1;
 }
 
 static PyObject* Database_repr(DatabaseObject* self) {