[PATCH] inotify extension: Port of the C module to py3k

Renato Cunha renatoc at gmail.com
Thu Jun 17 16:47:24 CDT 2010


 hgext/inotify/linux/_inotify.c |  89 ++++++++++++++++++++++++++++++++---------
 1 files changed, 68 insertions(+), 21 deletions(-)


# HG changeset patch
# User Renato Cunha <renatoc at gmail.com>
# Date 1276810543 10800
# Node ID 4daf4db62497a5735b0de3c55ca3a97cb34f76d4
# Parent  0d5fe990dc8d42f69ee3ac5da3fc21050a56b6b1
inotify extension: Port of the C module to py3k.

This patch implements a py3k port for the inotify C module extension. The
event_repr got mostly rewritten to eliminate the need for conditional
compilation of the module. The trick there (thanks to Antoine Pitrou) is to
use the % operator to let the python interpreter format the string to be
returned. There was, also, the #inclusion of the "util.h" file to enable
correct compilation in both python 2 and 3.

diff --git a/hgext/inotify/linux/_inotify.c b/hgext/inotify/linux/_inotify.c
--- a/hgext/inotify/linux/_inotify.c
+++ b/hgext/inotify/linux/_inotify.c
@@ -15,6 +15,8 @@
 #include <sys/ioctl.h>
 #include <unistd.h>
 
+#include <util.h>
+
 static PyObject *init(PyObject *self, PyObject *args)
 {
 	PyObject *ret = NULL;
@@ -334,9 +336,35 @@
 {
 	int wd = PyInt_AsLong(evt->wd);
 	int cookie = evt->cookie == Py_None ? -1 : PyInt_AsLong(evt->cookie);
+	char *maskstr;
 	PyObject *ret = NULL, *pymasks = NULL, *pymask = NULL;
 	PyObject *join = NULL;
-	char *maskstr;
+	static PyObject *pystr1 = NULL, *pystr2 = NULL;
+	static PyObject *pystr3 = NULL, *pystr4 = NULL;
+
+	if (pystr1 == NULL) {
+		pystr1 = PyString_FromString("event(wd=%d, mask=%s, name=%s)");
+		if (pystr1 == NULL)
+			goto bail;
+	}
+
+	if (pystr2 == NULL) {
+		pystr2 = PyString_FromString("event(wd=%d, mask=%s, cookie=0x%x, name=%s)");
+		if (pystr2 == NULL)
+			goto bail;
+	}
+
+	if (pystr3 == NULL) {
+		pystr3 = PyString_FromString("event(wd=%d, mask=%s)");
+		if (pystr3 == NULL)
+			goto bail;
+	}
+
+	if (pystr4 == NULL) {
+		pystr4 = PyString_FromString("event(wd=%d, mask=%s, cookie=0x%x)");
+		if (pystr4 == NULL)
+			goto bail;
+	}
 
 	join = PyString_FromString("|");
 	if (join == NULL)
@@ -350,33 +378,26 @@
 	if (pymask == NULL)
 		goto bail;
 
-	maskstr = PyString_AsString(pymask);
-
 	if (evt->name != Py_None) {
-		PyObject *pyname = PyString_Repr(evt->name, 1);
-		char *name = pyname ? PyString_AsString(pyname) : "???";
-
 		if (cookie == -1)
-			ret = PyString_FromFormat(
-				"event(wd=%d, mask=%s, name=%s)",
-				wd, maskstr, name);
+			ret = PyNumber_Remainder(pystr1,
+					PyTuple_Pack(3,	wd, pymask, evt->name));
 		else
-			ret = PyString_FromFormat("event(wd=%d, mask=%s, "
-						  "cookie=0x%x, name=%s)",
-						  wd, maskstr, cookie, name);
-
-		Py_XDECREF(pyname);
+			ret = PyNumber_Remainder(pystr2,
+					PyTuple_Pack(4,	wd, pymask, evt->cookie, evt->name));
 	} else {
 		if (cookie == -1)
-			ret = PyString_FromFormat("event(wd=%d, mask=%s)",
-						  wd, maskstr);
+			ret = PyNumber_Remainder(pystr3,
+					PyTuple_Pack(2,	wd, pymask));
 		else {
-			ret = PyString_FromFormat(
-				"event(wd=%d, mask=%s, cookie=0x%x)",
-				wd, maskstr, cookie);
+			ret = PyNumber_Remainder(pystr4,
+					PyTuple_Pack(3,	wd, pymask, evt->cookie));
 		}
 	}
 
+	if (ret == NULL)
+		goto bail;
+
 	goto done;
 bail:
 	Py_CLEAR(ret);
@@ -390,8 +411,7 @@
 }
 
 static PyTypeObject event_type = {
-	PyObject_HEAD_INIT(NULL)
-	0,                         /*ob_size*/
+	PyVarObject_HEAD_INIT(NULL, 0)
 	"_inotify.event",             /*tp_name*/
 	sizeof(struct event), /*tp_basicsize*/
 	0,                         /*tp_itemsize*/
@@ -585,6 +605,32 @@
 	{NULL},
 };
 
+#ifdef IS_PY3K
+static struct PyModuleDef _inotify_module = {
+	PyModuleDef_HEAD_INIT,
+	"_inotify",
+	doc,
+	-1,
+	methods
+};
+
+PyMODINIT_FUNC PyInit__inotify(void)
+{
+	PyObject *mod, *dict;
+
+	mod = PyModule_Create(&_inotify_module);
+
+	if (mod == NULL)
+		return NULL;
+
+	dict = PyModule_GetDict(mod);
+
+	if (dict)
+		define_consts(dict);
+
+	return mod;
+}
+#else
 void init_inotify(void)
 {
 	PyObject *mod, *dict;
@@ -599,3 +645,4 @@
 	if (dict)
 		define_consts(dict);
 }
+#endif


More information about the Mercurial-devel mailing list