[Urwid] MainLoop's pipe is not removed after pipe data ends.

Andrew Wu andrewwu.tw at gmail.com
Fri Apr 6 08:29:55 EDT 2012


Test code:
===
import urwid
import os

txt = urwid.Text(u"Hello World")
fill = urwid.Filler(txt, 'top')

def show_or_exit(input):
    if input in ('q', 'Q'):
        raise urwid.ExitMainLoop()

loop = urwid.MainLoop(fill, unhandled_input=show_or_exit)

def callback(data):
    if data == "":
        # data end, return False, watch will be removed.
        return False
    return True

fd = loop.watch_pipe(callback)
# write some data into pipe.
f = os.fdopen(fd, "w", 1)
f.write("hello")
f.close()

print "before loop.run():"
print "loop.event_loop._watch_files:"
print loop.event_loop._watch_files
print "loop._watch_pipes:"
print loop._watch_pipes

# press Q to break.
loop.run()

print "after loop.run():"
print "loop.event_loop._watch_files:"
print loop.event_loop._watch_files
print "loop._watch_pipes:"
print loop._watch_pipes
===
Run the test code and press Q to quit, the output is:

before loop.run():
loop.event_loop._watch_files:
{5: <function cb at 0xb7c36144>}
loop._watch_pipes:
{6: (5, 5)}

after loop.run():
loop.event_loop._watch_files:
{}
loop._watch_pipes:
{6: (5, 5)}

You can see that pipe in _watch_files list is removed correctly, but pipe
in _watch_pipes list is not removed. I think it is not right.

But if I do this patch:
===
--- urwid_bak/main_loop.py    2012-04-03 19:20:16.000000000 +0800
+++ urwid/main_loop.py    2012-04-06 20:18:20.000000000 +0800
@@ -186,8 +186,7 @@
             data = os.read(pipe_rd, PIPE_BUFFER_READ_SIZE)
             rval = callback(data)
             if rval is False:
-                self.event_loop.remove_watch_file(watch_handle)
-                os.close(pipe_rd)
+                self.remove_watch_pipe(pipe_wr)

         watch_handle = self.event_loop.watch_file(pipe_rd, cb)
         self._watch_pipes[pipe_wr] = (watch_handle, pipe_rd)
===

The output becomes:

before loop.run():
loop.event_loop._watch_files:
{5: <function cb at 0xb7b7b9cc>}
loop._watch_pipes:
{6: (5, 5)}

after loop.run():
loop.event_loop._watch_files:
{}
loop._watch_pipes:
{}

Both _watch_pipes list and _watch_files list are now correct.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.excess.org/pipermail/urwid/attachments/20120406/bca70f97/attachment.htm 


More information about the Urwid mailing list