Commits

Michał Górny committed de2be0d

Add support for Python 3.

The solution is based by one implemented in pyh2o.

Comments (0)

Files changed (8)

 include COPYING
+include src/*.h
 			Extension('pymountboot',
 				extra_compile_args = cflags,
 				extra_link_args = libs,
-				sources = ['src/pymountboot.c'])
+				sources = ['src/pymountboot.c',
+					'src/module.c', 'src/module2.c', 'src/module3.c'])
 		],
 
 		classifiers = [
+/* pymountboot -- (re)mount /boot (for eclean-kernel)
+ * (c) 2012 Michał Górny
+ * Released under the terms of the 2-clause BSD license
+ */
+
+#include "module.h"
+#include "pymountboot.h"
+
+int types_preinit()
+{
+	return PyType_Ready(&BootMountpointType) >= 0;
+}
+
+void types_postinit(PyObject* m)
+{
+	Py_INCREF(&BootMountpointType);
+	PyModule_AddObject(m, "BootMountpoint", (PyObject *)&BootMountpointType);
+}
+/* pymountboot -- (re)mount /boot (for eclean-kernel)
+ * (c) 2012 Michał Górny
+ * Released under the terms of the 2-clause BSD license
+ */
+
+#pragma once
+
+#ifndef _MODULE_H
+#define _MODULE_H 1
+
+#include <Python.h>
+
+static const char module_name[] = "pymountboot";
+static const char module_doc[] = "Module used to handle /boot auto(re)mounting.";
+
+int types_preinit();
+void types_postinit(PyObject* m);
+
+#endif /*_MODULE_H*/
+/* pymountboot -- (re)mount /boot (for eclean-kernel)
+ * (c) 2012 Michał Górny
+ * Released under the terms of the 2-clause BSD license
+ */
+
+#include "module.h"
+
+#if PY_MAJOR_VERSION < 3
+
+static PyMethodDef global_methods[] = {
+	{NULL}
+};
+
+void initpymountboot()
+{
+	PyObject* m;
+
+	if (!types_preinit())
+		return;
+
+	m = Py_InitModule3(module_name, global_methods, module_doc);
+
+	types_postinit(m);
+}
+
+#endif
+/* pymountboot -- (re)mount /boot (for eclean-kernel)
+ * (c) 2012 Michał Górny
+ * Released under the terms of the 2-clause BSD license
+ */
+
+#include "module.h"
+
+#if PY_MAJOR_VERSION >= 3
+
+static PyModuleDef pymountboot_module = {
+	PyModuleDef_HEAD_INIT, /* m_base */
+
+	module_name, /* m_name */
+	module_doc, /* m_doc */
+	-1, /* m_size */
+	NULL /* m_methods */
+};
+
+PyObject* PyInit_pymountboot()
+{
+	PyObject* m;
+
+	if (!types_preinit())
+		return NULL;
+
+	m = PyModule_Create(&pymountboot_module);
+
+	if (!m)
+		return NULL;
+
+	types_postinit(m);
+
+	return m;
+}
+
+#endif

src/pymountboot.c

  * Released under the terms of the 2-clause BSD license.
  */
 
-#include <Python.h>
-
-#include <libmount.h>
-
-enum mountpoint_status
-{
-	MOUNTPOINT_NONE,
-	MOUNTPOINT_MOUNTED,
-	MOUNTPOINT_REMOUNTED_RW
-};
-
-typedef struct
-{
-	PyObject_HEAD
-
-	struct libmnt_context *mnt_context;
-	enum mountpoint_status status;
-} BootMountpoint;
+#include "pymountboot.h"
 
 static void BootMountpoint_dealloc(PyObject *o);
 static PyObject *BootMountpoint_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
 	{ NULL }
 };
 
-static PyTypeObject BootMountpointType =
+PyTypeObject BootMountpointType =
 {
-	PyObject_HEAD_INIT(NULL)
-	0, /* ob_size */
+	PyVarObject_HEAD_INIT(NULL, 0)
 	"pymountboot.BootMountpoint", /* tp_name */
 	sizeof(BootMountpoint), /* tp_basicsize */
 	0, /* tp_itemsize */
 	BootMountpoint_new /* tp_new */
 };
 
-static PyMethodDef pymountboot_methods[] =
-{
-	{ NULL }
-};
-
-#ifndef PyMODINIT_FUNC /* declarations for DLL import/export */
-#	define PyMODINIT_FUNC void
-#endif
-
-PyMODINIT_FUNC initpymountboot(void)
-{
-	PyObject *m;
-
-	if (PyType_Ready(&BootMountpointType) < 0)
-		return;
-
-	m = Py_InitModule3("pymountboot", pymountboot_methods,
-		"Module used to handle /boot auto(re)mounting.");
-
-	Py_INCREF(&BootMountpointType);
-	PyModule_AddObject(m, "BootMountpoint", (PyObject *)&BootMountpointType);
-}
-
 static void BootMountpoint_dealloc(PyObject *o)
 {
 	BootMountpoint* const b = (BootMountpoint*) o;

src/pymountboot.h

+/* pymountboot -- (re)mount /boot (for eclean-kernel)
+ * (c) 2012 Michał Górny
+ * Released under the terms of the 2-clause BSD license
+ */
+
+#pragma once
+
+#ifndef PYMOUNTBOOT_H
+#define PYMOUNTBOOT_H 1
+
+#include <libmount.h>
+
+#include <Python.h>
+
+enum mountpoint_status
+{
+	MOUNTPOINT_NONE,
+	MOUNTPOINT_MOUNTED,
+	MOUNTPOINT_REMOUNTED_RW
+};
+
+typedef struct
+{
+	PyObject_HEAD
+
+	struct libmnt_context *mnt_context;
+	enum mountpoint_status status;
+} BootMountpoint;
+
+PyTypeObject BootMountpointType;
+
+#endif /*PYMOUNTBOOT_H*/