From 34d537b1877d295ace950e0dce33d275e1ff3e92 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Mon, 22 Aug 2022 13:45:44 +0000 Subject: [PATCH] python: Correctly raise any errors when opening the database Signed-off-by: Michael Tremer --- src/python/database.c | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/src/python/database.c b/src/python/database.c index f5797db..de45044 100644 --- a/src/python/database.c +++ b/src/python/database.c @@ -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) { -- 2.39.5