[PATCH 6 of 7] inotify extension: Better implementation of the event string representation

Renato Cunha renatoc at gmail.com
Thu Jul 1 17:19:19 CDT 2010


 hgext/inotify/linux/_inotify.c |  75 ++++++++++++++++++++++++++---------------
 1 files changed, 48 insertions(+), 27 deletions(-)


# HG changeset patch
# User Renato Cunha <renatoc at gmail.com>
# Date 1278022747 10800
# Node ID 791c6d61d084454be7bd8feab04a68df3eeea9ee
# Parent  de47085e5d2b56a173f2bb25785ef132df1d6a87
inotify extension: Better implementation of the event string representation.

This patch reimplements the event_repr function. It got mostly rewritten to
eliminate the need for conditional compilation of the module when building in
py3k. The trick there (thanks to Antoine Pitrou) is to use the % operator to
let the python interpreter format the string to be returned.

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,13 @@
 #include <sys/ioctl.h>
 #include <unistd.h>
 
+/* Variables used in the event string representation */
+static PyObject *join;
+static PyObject *er_wm;
+static PyObject *er_wmc;
+static PyObject *er_wmn;
+static PyObject *er_wmcn;
+
 static PyObject *init(PyObject *self, PyObject *args)
 {
 	PyObject *ret = NULL;
@@ -335,12 +342,8 @@
 	int wd = PyInt_AsLong(evt->wd);
 	int cookie = evt->cookie == Py_None ? -1 : PyInt_AsLong(evt->cookie);
 	PyObject *ret = NULL, *pymasks = NULL, *pymask = NULL;
-	PyObject *join = NULL;
 	char *maskstr;
-
-	join = PyString_FromString("|");
-	if (join == NULL)
-		goto bail;
+	PyObject *tuple = NULL, *formatstr = NULL;
 
 	pymasks = decode_mask(PyInt_AsLong(evt->mask));
 	if (pymasks == NULL)
@@ -350,33 +353,34 @@
 	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);
-		else
-			ret = PyString_FromFormat("event(wd=%d, mask=%s, "
-						  "cookie=0x%x, name=%s)",
-						  wd, maskstr, cookie, name);
-
-		Py_XDECREF(pyname);
+		if (cookie == -1) {
+			formatstr = er_wmn;
+			tuple = PyTuple_Pack(3,	wd, pymask, evt->name);
+		}
+		else {
+			formatstr = er_wmcn;
+			tuple = PyTuple_Pack(4,	wd, pymask, evt->cookie, evt->name);
+		}
 	} else {
-		if (cookie == -1)
-			ret = PyString_FromFormat("event(wd=%d, mask=%s)",
-						  wd, maskstr);
+		if (cookie == -1) {
+			formatstr = er_wm;
+			tuple = PyTuple_Pack(2,	wd, pymask);
+		}
 		else {
-			ret = PyString_FromFormat(
-				"event(wd=%d, mask=%s, cookie=0x%x)",
-				wd, maskstr, cookie);
+			formatstr = er_wmc;
+			tuple = PyTuple_Pack(3, wd, pymask, evt->cookie);
 		}
 	}
 
+	if (tuple == NULL)
+		goto bail;
+
+	ret = PyNumber_Remainder(formatstr, tuple);
+
+	if (ret == NULL)
+		goto bail;
+
 	goto done;
 bail:
 	Py_CLEAR(ret);
@@ -384,7 +388,7 @@
 done:
 	Py_XDECREF(pymask);
 	Py_XDECREF(pymasks);
-	Py_XDECREF(join);
+	Py_XDECREF(tuple);
 
 	return ret;
 }
@@ -561,6 +565,20 @@
 	return ret;
 }
 
+static int init_globals(void)
+{
+    join = PyString_FromString("|");
+    er_wm = PyString_FromString("event(wd=%d, mask=%s)");
+    er_wmn = PyString_FromString("event(wd=%d, mask=%s, name=%s)");
+    er_wmc = PyString_FromString("event(wd=%d, mask=%s, cookie=0x%x)");
+    er_wmcn = PyString_FromString("event(wd=%d, mask=%s, cookie=0x%x, name=%s)");
+
+	if (!join || !er_wm || !er_wmn || !er_wmc || !er_wmcn)
+		return 0;
+
+    return 1;
+}
+
 PyDoc_STRVAR(
 	read_doc,
 	"read(fd, bufsize[=65536]) -> list_of_events\n"
@@ -592,6 +610,9 @@
 	if (PyType_Ready(&event_type) == -1)
 		return;
 
+    if (init_globals() == 0)
+        return;
+
 	mod = Py_InitModule3("_inotify", methods, doc);
 
 	dict = PyModule_GetDict(mod);


More information about the Mercurial-devel mailing list