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

Matt Mackall mpm at selenic.com
Thu Jun 17 17:07:56 CDT 2010


On Thu, 2010-06-17 at 18:47 -0300, Renato Cunha wrote:
> 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;

I spot gratuitous code movement

> +	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;
> +	}

Yuck. Do it like this:

static PyObject *pystr1, *pystr2..;

pystr1 = PyString_FromString("....");
pystr2 = PyString_FromString("....");
...

if (!pystr1 || !pystr2 ...)
	goto bail;

It wouldn't hurt to give these things real names either.

Note that you also don't have to get all the way to py3k in one step.
It'd be easier to read this one if you did only the string changes, for
instance.

-- 
Mathematics is the supreme nostalgia of our time.




More information about the Mercurial-devel mailing list