From: Michael Tremer Date: Mon, 8 Jan 2018 17:46:17 +0000 (+0000) Subject: python: Save path when opening the database X-Git-Tag: 0.9.0~105 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=6fd96715ab92bff791e20026f53ea80a4605cce9;p=location%2Flibloc.git python: Save path when opening the database Signed-off-by: Michael Tremer --- diff --git a/src/python/database.c b/src/python/database.c index 7bac3bc..f04a44d 100644 --- a/src/python/database.c +++ b/src/python/database.c @@ -34,6 +34,9 @@ static void Database_dealloc(DatabaseObject* self) { if (self->db) loc_database_unref(self->db); + if (self->path) + free(self->path); + Py_TYPE(self)->tp_free((PyObject* )self); } @@ -43,8 +46,10 @@ static int Database_init(DatabaseObject* self, PyObject* args, PyObject* kwargs) if (!PyArg_ParseTuple(args, "s", &path)) return -1; + self->path = strdup(path); + // Open the file for reading - FILE* f = fopen(path, "r"); + FILE* f = fopen(self->path, "r"); if (!f) return -1; @@ -59,6 +64,10 @@ static int Database_init(DatabaseObject* self, PyObject* args, PyObject* kwargs) return 0; } +static PyObject* Database_repr(DatabaseObject* self) { + return PyUnicode_FromFormat("", self->path); +} + static PyObject* Database_get_description(DatabaseObject* self) { const char* description = loc_database_get_description(self->db); @@ -181,4 +190,5 @@ PyTypeObject DatabaseType = { tp_doc: "Database object", tp_methods: Database_methods, tp_getset: Database_getsetters, + tp_repr: (reprfunc)Database_repr, }; diff --git a/src/python/database.h b/src/python/database.h index 2609d1b..4dc9ea4 100644 --- a/src/python/database.h +++ b/src/python/database.h @@ -24,6 +24,7 @@ typedef struct { PyObject_HEAD struct loc_database* db; + char* path; } DatabaseObject; extern PyTypeObject DatabaseType;