use c99 init for struct init
[python-kmod.git] / libkmod.c
1 /*
2  * Libkmod -- Python interface to kmod API.
3  *
4  * Copyright (C) 2012 Red Hat, Inc. All rights reserved.
5  *
6  * This copyrighted material is made available to anyone wishing to use,
7  * modify, copy, or redistribute it subject to the terms and conditions
8  * of the GNU Lesser General Public License v.2.1.
9  *
10  * You should have received a copy of the GNU Lesser General Public License
11  * along with this program; if not, write to the Free Software Foundation,
12  * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
13  *
14  * Author: Andy Grover <agrover redhat com>
15  *
16  */
17
18 #include <Python.h>
19 #include <libkmod.h>
20
21 typedef struct {
22     PyObject_HEAD
23     struct kmod_ctx *ctx;
24 } kmodobject;
25
26 static PyTypeObject KmodType;
27
28 static PyObject *LibKmodError;
29
30
31 /* ----------------------------------------------------------------------
32  * kmod object initialization/deallocation
33  */
34
35 static int
36 libkmod_init(PyObject *self, PyObject *args, PyObject *kwds)
37 {
38     kmodobject *kmod = (kmodobject *)self;
39     char *mod_dir = NULL;
40
41     if (!PyArg_ParseTuple(args, "|s", &mod_dir))
42        return -1;
43
44     kmod->ctx = kmod_new(mod_dir, NULL);
45     if (!kmod->ctx) {
46         PyErr_SetFromErrno(PyExc_OSError);
47         return -1;
48     }
49
50     return 0;
51 }
52
53 static void
54 libkmod_dealloc(PyObject *self)
55 {
56     kmodobject *kmod = (kmodobject *)self;
57
58     /* if already closed, don't reclose it */
59     if (kmod->ctx != NULL){
60             kmod_unref(kmod->ctx);
61     }
62     //self->ob_type->tp_free((PyObject*)self);
63     PyObject_Del(self);
64 }
65
66 static PyObject *
67 libkmod_library_get_version(kmodobject *self)
68 {
69     return Py_BuildValue("s", "whatup dude v1.234");
70 }
71
72 /* ----------------------------------------------------------------------
73  * Method tables and other bureaucracy
74  */
75
76 static PyMethodDef Libkmod_methods[] = {
77     /* LVM methods */
78     { "getVersion",          (PyCFunction)libkmod_library_get_version, METH_NOARGS },
79     { NULL,             NULL}           /* sentinel */
80 };
81
82
83 static PyTypeObject KmodType = {
84     PyObject_HEAD_INIT(NULL)
85     .tp_name = "kmod.kmod",
86     .tp_basicsize = sizeof(kmodobject),
87     //.tp_new = PyType_GenericNew,
88     .tp_init = libkmod_init,
89     .tp_dealloc = libkmod_dealloc,
90     .tp_flags =Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
91     .tp_doc = "Libkmod objects",
92     .tp_methods = Libkmod_methods,
93 };
94
95 #ifndef PyMODINIT_FUNC    /* declarations for DLL import/export */
96 #define PyMODINIT_FUNC void
97 #endif
98 PyMODINIT_FUNC
99 initlibkmod(void)
100 {
101     PyObject *m;
102
103     KmodType.tp_new = PyType_GenericNew;
104     if (PyType_Ready(&KmodType) < 0)
105         return;
106
107     m = Py_InitModule3("libkmod", Libkmod_methods, "Libkmod module");
108     if (m == NULL)
109         return;
110
111     Py_INCREF(&KmodType);
112     PyModule_AddObject(m, "Kmod", (PyObject *)&KmodType);
113
114     LibKmodError = PyErr_NewException("Libkmod.LibKmodError",
115                                       NULL, NULL);
116     if (LibKmodError) {
117         /* Each call to PyModule_AddObject decrefs it; compensate: */
118         Py_INCREF(LibKmodError);
119         Py_INCREF(LibKmodError);
120         PyModule_AddObject(m, "error", LibKmodError);
121         PyModule_AddObject(m, "LibKmodError", LibKmodError);
122     }
123
124 }
125