- added popen.popen2/popen3/popen4 support for
  windows.

- added optional mode argument to popen2/popen3
  for unix; if the second argument is an integer,
  it's assumed to be the buffer size.

- changed nt.popen2/popen3/popen4 return values
  to match the popen2 module (stdout first, not
  stdin).
diff --git a/Lib/popen2.py b/Lib/popen2.py
index eb8fb9a..73ff77c 100644
--- a/Lib/popen2.py
+++ b/Lib/popen2.py
@@ -89,31 +89,55 @@
             _active.remove(self)
         return self.sts
 
-def popen2(cmd, bufsize=-1):
-    """Execute the shell command 'cmd' in a sub-process.  If 'bufsize' is
-    specified, it sets the buffer size for the I/O pipes.  The file objects
-    (child_stdout, child_stdin) are returned."""
-    _cleanup()
-    inst = Popen3(cmd, 0, bufsize)
-    return inst.fromchild, inst.tochild
+try:
+    from os import popen2
+except NameError:
+    def popen2(cmd, mode='t', bufsize=-1):
+        """Execute the shell command 'cmd' in a sub-process.  If 'bufsize' is
+        specified, it sets the buffer size for the I/O pipes.  The file objects
+        (child_stdout, child_stdin) are returned."""
+        if type(mode) is type(0) and bufsize == -1:
+            bufsize = mode
+            mode = 't'
+        assert mode in ('t', 'b')
+        _cleanup()
+        inst = Popen3(cmd, 0, bufsize)
+        return inst.fromchild, inst.tochild
 
-def popen3(cmd, bufsize=-1):
-    """Execute the shell command 'cmd' in a sub-process.  If 'bufsize' is
-    specified, it sets the buffer size for the I/O pipes.  The file objects
-    (child_stdout, child_stdin, child_stderr) are returned."""
-    _cleanup()
-    inst = Popen3(cmd, 1, bufsize)
-    return inst.fromchild, inst.tochild, inst.childerr
+try:
+    from os import popen3
+except NameError:
+    def popen3(cmd, mode='t', bufsize=-1):
+        """Execute the shell command 'cmd' in a sub-process.  If 'bufsize' is
+        specified, it sets the buffer size for the I/O pipes.  The file objects
+        (child_stdout, child_stdin, child_stderr) are returned."""
+        if type(mode) is type(0) and bufsize == -1:
+            bufsize = mode
+            mode = 't'
+        assert mode in ('t', 'b')
+        _cleanup()
+        inst = Popen3(cmd, 1, bufsize)
+        return inst.fromchild, inst.tochild, inst.childerr
+
+try:
+    from os import popen4
+except NameError:
+    pass # not on unix
 
 def _test():
     teststr = "abc\n"
     print "testing popen2..."
     r, w = popen2('cat')
+    print r, w
     w.write(teststr)
     w.close()
     assert r.read() == teststr
     print "testing popen3..."
-    r, w, e = popen3(['cat'])
+    try:
+        r, w, e = popen3(['cat'])
+    except:
+        r, w, e = popen3('cat')
+    print r, w, e
     w.write(teststr)
     w.close()
     assert r.read() == teststr