Commit 34d537b1 authored by Michael Tremer's avatar Michael Tremer

python: Correctly raise any errors when opening the database

Signed-off-by: default avatarMichael Tremer <michael.tremer@ipfire.org>
parent 8a330e55
...@@ -45,28 +45,36 @@ static void Database_dealloc(DatabaseObject* self) { ...@@ -45,28 +45,36 @@ static void Database_dealloc(DatabaseObject* self) {
static int Database_init(DatabaseObject* self, PyObject* args, PyObject* kwargs) { static int Database_init(DatabaseObject* self, PyObject* args, PyObject* kwargs) {
const char* path = NULL; const char* path = NULL;
FILE* f = NULL;
// Parse arguments
if (!PyArg_ParseTuple(args, "s", &path)) if (!PyArg_ParseTuple(args, "s", &path))
return -1; return -1;
// Copy path
self->path = strdup(path); self->path = strdup(path);
if (!self->path)
goto ERROR;
// Open the file for reading // Open the file for reading
FILE* f = fopen(self->path, "r"); f = fopen(self->path, "r");
if (!f) { if (!f)
PyErr_SetFromErrno(PyExc_IOError); goto ERROR;
return -1;
}
// Load the database // Load the database
int r = loc_database_new(loc_ctx, &self->db, f); int r = loc_database_new(loc_ctx, &self->db, f);
fclose(f);
// Return on any errors
if (r) if (r)
return -1; goto ERROR;
fclose(f);
return 0; return 0;
ERROR:
if (f)
fclose(f);
PyErr_SetFromErrno(PyExc_OSError);
return -1;
} }
static PyObject* Database_repr(DatabaseObject* self) { static PyObject* Database_repr(DatabaseObject* self) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment