##// END OF EJS Templates
inotify: Better implementation of the event string representation....
Renato Cunha -
r11548:dd2f356e default
parent child Browse files
Show More
@@ -15,6 +15,13 b''
15 #include <sys/ioctl.h>
15 #include <sys/ioctl.h>
16 #include <unistd.h>
16 #include <unistd.h>
17
17
18 /* Variables used in the event string representation */
19 static PyObject *join;
20 static PyObject *er_wm;
21 static PyObject *er_wmc;
22 static PyObject *er_wmn;
23 static PyObject *er_wmcn;
24
18 static PyObject *init(PyObject *self, PyObject *args)
25 static PyObject *init(PyObject *self, PyObject *args)
19 {
26 {
20 PyObject *ret = NULL;
27 PyObject *ret = NULL;
@@ -312,8 +319,8 b' static struct PyGetSetDef event_getsets['
312 };
319 };
313
320
314 PyDoc_STRVAR(
321 PyDoc_STRVAR(
315 event_doc,
322 event_doc,
316 "event: Structure describing an inotify event.");
323 "event: Structure describing an inotify event.");
317
324
318 static PyObject *event_new(PyTypeObject *t, PyObject *a, PyObject *k)
325 static PyObject *event_new(PyTypeObject *t, PyObject *a, PyObject *k)
319 {
326 {
@@ -332,15 +339,10 b' static void event_dealloc(struct event *'
332
339
333 static PyObject *event_repr(struct event *evt)
340 static PyObject *event_repr(struct event *evt)
334 {
341 {
335 int wd = PyInt_AsLong(evt->wd);
336 int cookie = evt->cookie == Py_None ? -1 : PyInt_AsLong(evt->cookie);
342 int cookie = evt->cookie == Py_None ? -1 : PyInt_AsLong(evt->cookie);
337 PyObject *ret = NULL, *pymasks = NULL, *pymask = NULL;
343 PyObject *ret = NULL, *pymasks = NULL, *pymask = NULL;
338 PyObject *join = NULL;
339 char *maskstr;
344 char *maskstr;
340
345 PyObject *tuple = NULL, *formatstr = NULL;
341 join = PyString_FromString("|");
342 if (join == NULL)
343 goto bail;
344
346
345 pymasks = decode_mask(PyInt_AsLong(evt->mask));
347 pymasks = decode_mask(PyInt_AsLong(evt->mask));
346 if (pymasks == NULL)
348 if (pymasks == NULL)
@@ -350,33 +352,35 b' static PyObject *event_repr(struct event'
350 if (pymask == NULL)
352 if (pymask == NULL)
351 goto bail;
353 goto bail;
352
354
353 maskstr = PyString_AsString(pymask);
354
355 if (evt->name != Py_None) {
355 if (evt->name != Py_None) {
356 PyObject *pyname = PyString_Repr(evt->name, 1);
356 if (cookie == -1) {
357 char *name = pyname ? PyString_AsString(pyname) : "???";
357 formatstr = er_wmn;
358
358 tuple = PyTuple_Pack(3, evt->wd, pymask, evt->name);
359 if (cookie == -1)
359 }
360 ret = PyString_FromFormat(
360 else {
361 "event(wd=%d, mask=%s, name=%s)",
361 formatstr = er_wmcn;
362 wd, maskstr, name);
362 tuple = PyTuple_Pack(4, evt->wd, pymask,
363 else
363 evt->cookie, evt->name);
364 ret = PyString_FromFormat("event(wd=%d, mask=%s, "
364 }
365 "cookie=0x%x, name=%s)",
366 wd, maskstr, cookie, name);
367
368 Py_XDECREF(pyname);
369 } else {
365 } else {
370 if (cookie == -1)
366 if (cookie == -1) {
371 ret = PyString_FromFormat("event(wd=%d, mask=%s)",
367 formatstr = er_wm;
372 wd, maskstr);
368 tuple = PyTuple_Pack(2, evt->wd, pymask);
369 }
373 else {
370 else {
374 ret = PyString_FromFormat(
371 formatstr = er_wmc;
375 "event(wd=%d, mask=%s, cookie=0x%x)",
372 tuple = PyTuple_Pack(3, evt->wd, pymask, evt->cookie);
376 wd, maskstr, cookie);
377 }
373 }
378 }
374 }
379
375
376 if (tuple == NULL)
377 goto bail;
378
379 ret = PyNumber_Remainder(formatstr, tuple);
380
381 if (ret == NULL)
382 goto bail;
383
380 goto done;
384 goto done;
381 bail:
385 bail:
382 Py_CLEAR(ret);
386 Py_CLEAR(ret);
@@ -384,7 +388,7 b' bail:'
384 done:
388 done:
385 Py_XDECREF(pymask);
389 Py_XDECREF(pymask);
386 Py_XDECREF(pymasks);
390 Py_XDECREF(pymasks);
387 Py_XDECREF(join);
391 Py_XDECREF(tuple);
388
392
389 return ret;
393 return ret;
390 }
394 }
@@ -561,6 +565,17 b' done:'
561 return ret;
565 return ret;
562 }
566 }
563
567
568 static int init_globals(void)
569 {
570 join = PyString_FromString("|");
571 er_wm = PyString_FromString("event(wd=%d, mask=%s)");
572 er_wmn = PyString_FromString("event(wd=%d, mask=%s, name=%s)");
573 er_wmc = PyString_FromString("event(wd=%d, mask=%s, cookie=0x%x)");
574 er_wmcn = PyString_FromString("event(wd=%d, mask=%s, cookie=0x%x, name=%s)");
575
576 return join && er_wm && er_wmn && er_wmc && er_wmcn;
577 }
578
564 PyDoc_STRVAR(
579 PyDoc_STRVAR(
565 read_doc,
580 read_doc,
566 "read(fd, bufsize[=65536]) -> list_of_events\n"
581 "read(fd, bufsize[=65536]) -> list_of_events\n"
@@ -592,6 +607,9 b' void init_inotify(void)'
592 if (PyType_Ready(&event_type) == -1)
607 if (PyType_Ready(&event_type) == -1)
593 return;
608 return;
594
609
610 if (!init_globals())
611 return;
612
595 mod = Py_InitModule3("_inotify", methods, doc);
613 mod = Py_InitModule3("_inotify", methods, doc);
596
614
597 dict = PyModule_GetDict(mod);
615 dict = PyModule_GetDict(mod);
General Comments 0
You need to be logged in to leave comments. Login now