]> git.ipfire.org Git - location/libloc.git/blobdiff - src/python/database.c
python: Save path when opening the database
[location/libloc.git] / src / python / database.c
index 931b54d4040e3650cec27b265b8ff61ac065d24e..f04a44dc951cb4598d71ea4858eaa13e17827ecb 100644 (file)
@@ -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("<Database %s>", self->path);
+}
+
 static PyObject* Database_get_description(DatabaseObject* self) {
        const char* description = loc_database_get_description(self->db);
 
@@ -86,19 +95,21 @@ static PyObject* Database_get_as(DatabaseObject* self, PyObject* args) {
 
        // Try to retrieve the AS
        int r = loc_database_get_as(self->db, &as, number);
-       if (r)
-               return NULL;
 
-       // Create an AS object
-       if (as) {
+       // We got an AS
+       if (r == 0) {
                PyObject* obj = new_as(&ASType, as);
                loc_as_unref(as);
 
                return obj;
-       }
 
        // Nothing found
-       Py_RETURN_NONE;
+       } else if (r == 1) {
+               Py_RETURN_NONE;
+       }
+
+       // Unexpected error
+       return NULL;
 }
 
 static PyObject* Database_lookup(DatabaseObject* self, PyObject* args) {
@@ -179,4 +190,5 @@ PyTypeObject DatabaseType = {
        tp_doc:                 "Database object",
        tp_methods:             Database_methods,
        tp_getset:              Database_getsetters,
+       tp_repr:                (reprfunc)Database_repr,
 };