]> git.ipfire.org Git - people/ms/libloc.git/commitdiff
python: Create a global loc_ctx* context
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 29 Dec 2017 12:02:21 +0000 (12:02 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 29 Dec 2017 12:02:21 +0000 (12:02 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
Makefile.am
src/python/locationmodule.c
src/python/locationmodule.h [new file with mode: 0644]

index ea82663e0a03e93c7bea83075013c6c055d1ee91..7061b055eeeb738a89fdb42970b0cd0a8afb5470 100644 (file)
@@ -85,6 +85,7 @@ pkgpyexec_LTLIBRARIES = \
 
 src_python_location_la_SOURCES = \
        src/python/locationmodule.c \
+       src/python/locationmodule.h \
        src/python/as.c \
        src/python/as.h \
        src/python/database.c \
index 39a7a1d30a4f30a6a9d656de04076e8af6fa0a55..67f796df7d025d3d27cffe69fe9aa2683af08f9f 100644 (file)
 
 #include <Python.h>
 
+#include "locationmodule.h"
 #include "as.h"
 #include "database.h"
 
 PyMODINIT_FUNC PyInit_location(void);
 
+static void location_free(void) {
+       // Release context
+       if (loc_ctx)
+               loc_unref(loc_ctx);
+}
+
 static PyMethodDef location_module_methods[] = {
        { NULL },
 };
@@ -31,9 +38,15 @@ static struct PyModuleDef location_module = {
        .m_size = -1,
        .m_doc = "Python module for libloc",
        .m_methods = location_module_methods,
+       .m_free = (freefunc)location_free,
 };
 
 PyMODINIT_FUNC PyInit_location(void) {
+       // Initialise loc context
+       int r = loc_new(&loc_ctx);
+       if (r)
+               return NULL;
+
        PyObject* m = PyModule_Create(&location_module);
        if (!m)
                return NULL;
diff --git a/src/python/locationmodule.h b/src/python/locationmodule.h
new file mode 100644 (file)
index 0000000..2847366
--- /dev/null
@@ -0,0 +1,25 @@
+/*
+       libloc - A library to determine the location of someone on the Internet
+
+       Copyright (C) 2017 IPFire Development Team <info@ipfire.org>
+
+       This library is free software; you can redistribute it and/or
+       modify it under the terms of the GNU Lesser General Public
+       License as published by the Free Software Foundation; either
+       version 2.1 of the License, or (at your option) any later version.
+
+       This library is distributed in the hope that it will be useful,
+       but WITHOUT ANY WARRANTY; without even the implied warranty of
+       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+       Lesser General Public License for more details.
+*/
+
+#ifndef PYTHON_LOCATION_MODULE_H
+#define PYTHON_LOCATION_MODULE_H
+
+#include <loc/libloc.h>
+#include <loc/as.h>
+
+struct loc_ctx* loc_ctx;
+
+#endif /* PYTHON_LOCATION_MODULE_H */