2 * Libkmod -- Python interface to kmod API.
4 * Copyright (C) 2012 Red Hat, Inc. All rights reserved.
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.
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
14 * Author: Andy Grover <agrover redhat com>
26 static PyTypeObject KmodType;
28 static PyObject *LibKmodError;
31 /* ----------------------------------------------------------------------
32 * kmod object initialization/deallocation
36 libkmod_init(PyObject *self, PyObject *args, PyObject *kwds)
38 kmodobject *kmod = (kmodobject *)self;
41 if (!PyArg_ParseTuple(args, "|s", &mod_dir))
44 kmod->ctx = kmod_new(mod_dir, NULL);
46 PyErr_SetFromErrno(PyExc_OSError);
54 libkmod_dealloc(PyObject *self)
56 kmodobject *kmod = (kmodobject *)self;
58 /* if already closed, don't reclose it */
59 if (kmod->ctx != NULL){
60 kmod_unref(kmod->ctx);
62 //self->ob_type->tp_free((PyObject*)self);
67 libkmod_library_get_version(kmodobject *self)
69 return Py_BuildValue("s", "whatup dude v1.234");
72 /* ----------------------------------------------------------------------
73 * Method tables and other bureaucracy
76 static PyMethodDef Libkmod_methods[] = {
78 { "getVersion", (PyCFunction)libkmod_library_get_version, METH_NOARGS },
79 { NULL, NULL} /* sentinel */
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,
95 #ifndef PyMODINIT_FUNC /* declarations for DLL import/export */
96 #define PyMODINIT_FUNC void
103 KmodType.tp_new = PyType_GenericNew;
104 if (PyType_Ready(&KmodType) < 0)
107 m = Py_InitModule3("libkmod", Libkmod_methods, "Libkmod module");
111 Py_INCREF(&KmodType);
112 PyModule_AddObject(m, "Kmod", (PyObject *)&KmodType);
114 LibKmodError = PyErr_NewException("Libkmod.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);