[Urwid] vterm.Terminal patches to enable easier subclassing

the grugq the.grugq at gmail.com
Sat Nov 26 19:46:32 EST 2011


I've been working on an Urwid application that uses the vterm.Terminal widget 
and integrates properly with Twisted. I've already submitted some minor patches 
to make the raw_display.Screen() object more friendly to subclassing, now I'd 
like to get the Terminal() patched as well. 

The issue is that Terminal assumes the self.master attribute is a file 
descriptor and it will inline calls to os.read()/os.write() on this attribute. 
If the attribute is anything other than a file descriptor, it will raise an 
exception and die. This makes it convoluted to have the master attribute as a 
ProcessProtocol, which is the simplest solution for a Twisted application 
subclassing and implementing the Terminal.

The best solution, from my POV, is to have all access to the the self.master 
attribute wrapped in method calls which can be overwritten. There are two 
options, either make the Terminal widget know how to do I/O on self.master, or 
make self.master an object. That is, either:

# original code:
  os.write(self.master, buf)

# terminal knows too much, code:

# I can't believe master's a file(), code:

In the attached patch I've implemented the latter approach. I promote the master 
to a file() object (with disabled buffering and non-blocking enabled, so it 
doesn't interfere with Twisted).

As the Gmane.org interface doesn't seem to allow for attachments, I've included 
a pastebin. Ian has the full patch as well, if he wants to forward it to the 


I'd like to see a solution where it is possible to use, for example, the key 
code parsing code of Terminal without having to cut and paste. 
Terminal.keypress() has only a single line which causes problems with twisted, 
but due to that line my subclass ends up with cut&paste of the whole method. 
This is brittle and non-optimal. Lets make it simpler and cleaner.



More information about the Urwid mailing list