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

Renato Cunha renatoc at gmail.com
Fri Jun 18 10:43:45 CDT 2010


 hgext/inotify/linux/_inotify.c |  63 ++++++++++++++++++++++++++----------------
 1 files changed, 39 insertions(+), 24 deletions(-)


# HG changeset patch
# User Renato Cunha <renatoc at gmail.com>
# Date 1276875647 10800
# Node ID 4b4edacbefcbfebb6fae348492c701380f083444
# Parent  36984edd907d89085d463765ed39d69665d5944e
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
@@ -335,11 +335,24 @@
 	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;
+	static PyObject *join = NULL;
 	char *maskstr;
+	PyObject *tuple = NULL, *formatstr = NULL;
+	static PyObject *er_wm = NULL, *er_wmc = NULL;
+	static PyObject *er_wmn = NULL, *er_wmcn = NULL;
 
-	join = PyString_FromString("|");
-	if (join == NULL)
+	if (!join)
+		join = PyString_FromString("|");
+	if (!er_wm)
+		er_wm = PyString_FromString("event(wd=%d, mask=%s)");
+	if (!er_wmn)
+		er_wmn = PyString_FromString("event(wd=%d, mask=%s, name=%s)");
+	if (!er_wmc)
+		er_wmc = PyString_FromString("event(wd=%d, mask=%s, cookie=0x%x)");
+	if (!er_wmcn)
+		er_wmcn = PyString_FromString("event(wd=%d, mask=%s, cookie=0x%x, name=%s)");
+
+	if (!join || !er_wm || !er_wmn || !er_wmc || !er_wmcn)
 		goto bail;
 
 	pymasks = decode_mask(PyInt_AsLong(evt->mask));
@@ -350,33 +363,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);
@@ -385,6 +399,7 @@
 	Py_XDECREF(pymask);
 	Py_XDECREF(pymasks);
 	Py_XDECREF(join);
+	Py_XDECREF(tuple);
 
 	return ret;
 }


More information about the Mercurial-devel mailing list