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

Renato Cunha renatoc at gmail.com
Thu Jun 17 17:51:53 CDT 2010


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


# HG changeset patch
# User Renato Cunha <renatoc at gmail.com>
# Date 1276814707 10800
# Node ID bd9094ef035b3635b66b8334e78648f809fceacb
# Parent  0d5fe990dc8d42f69ee3ac5da3fc21050a56b6b1
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
@@ -337,6 +337,16 @@
 	PyObject *ret = NULL, *pymasks = NULL, *pymask = NULL;
 	PyObject *join = NULL;
 	char *maskstr;
+	static PyObject *er_wmn = NULL, *er_wmcn = NULL;
+	static PyObject *er_wm = NULL, *er_wmc = NULL;
+
+	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(!er_wmn || !er_wmcn || !er_wm || !er_wmc)
+		goto bail;
 
 	join = PyString_FromString("|");
 	if (join == NULL)
@@ -350,33 +360,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(er_wmn,
+					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(er_wmcn,
+					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(er_wm,
+					PyTuple_Pack(2,	wd, pymask));
 		else {
-			ret = PyString_FromFormat(
-				"event(wd=%d, mask=%s, cookie=0x%x)",
-				wd, maskstr, cookie);
+			ret = PyNumber_Remainder(er_wmc,
+					PyTuple_Pack(3,	wd, pymask, evt->cookie));
 		}
 	}
 
+	if (ret == NULL)
+		goto bail;
+
 	goto done;
 bail:
 	Py_CLEAR(ret);


More information about the Mercurial-devel mailing list