[pypy-svn] r61338 - pypy/trunk/pypy/module/posix

afa at codespeak.net afa at codespeak.net
Sun Jan 25 23:08:41 CET 2009


Author: afa
Date: Sun Jan 25 23:08:41 2009
New Revision: 61338

Modified:
   pypy/trunk/pypy/module/posix/app_posix.py
Log:
(Mostly an indentation change:)
This implementation of popen is specific to posix.


Modified: pypy/trunk/pypy/module/posix/app_posix.py
==============================================================================
--- pypy/trunk/pypy/module/posix/app_posix.py	(original)
+++ pypy/trunk/pypy/module/posix/app_posix.py	Sun Jan 25 23:08:41 2009
@@ -71,70 +71,71 @@
     return f
 
 
-# __________ only if we have os.fork() __________
+# Implement popen() for platforms which have os.fork()
+if osname == 'posix':
 
-class popenfile(file):
-    _childpid = None
+    class popenfile(file):
+        _childpid = None
 
-    def close(self):
-        import os
-        file.close(self)
-        pid = self._childpid
-        if pid is not None:
-            self._childpid = None
-            return os.waitpid(pid, 0)[1]
-        return 0
-    __del__ = close     # as in CPython, __del__ may call os.waitpid()
-
-def popen(command, mode='r', bufsize=-1):
-    """popen(command [, mode='r' [, bufsize]]) -> pipe
-    
-    Open a pipe to/from a command returning a file object."""
-
-    from popen2 import MAXFD
-    import os, gc
-
-    def try_close(fd):
-        try:
-            os.close(fd)
-        except OSError:
-            pass
-
-    if not mode.startswith('r') and not mode.startswith('w'):
-        raise ValueError("invalid mode %r" % (mode,))
-    read_end, write_end = os.pipe()
-    try:
-        gc.disable_finalizers()
+        def close(self):
+            import os
+            file.close(self)
+            pid = self._childpid
+            if pid is not None:
+                self._childpid = None
+                return os.waitpid(pid, 0)[1]
+            return 0
+        __del__ = close     # as in CPython, __del__ may call os.waitpid()
+
+    def popen(command, mode='r', bufsize=-1):
+        """popen(command [, mode='r' [, bufsize]]) -> pipe
+
+        Open a pipe to/from a command returning a file object."""
+
+        from popen2 import MAXFD
+        import os, gc
+
+        def try_close(fd):
+            try:
+                os.close(fd)
+            except OSError:
+                pass
+
+        if not mode.startswith('r') and not mode.startswith('w'):
+            raise ValueError("invalid mode %r" % (mode,))
+        read_end, write_end = os.pipe()
         try:
-            childpid = os.fork()
-            if childpid == 0:
-                # in the child
-                try:
-                    if mode.startswith('r'):
-                        os.dup2(write_end, 1)
-                        os.close(read_end)
-                    else:
-                        os.dup2(read_end, 0)
-                        os.close(write_end)
-                    os.closerange(3, MAXFD)
-                    cmd = ['/bin/sh', '-c', command]
-                    os.execvp(cmd[0], cmd)
-                finally:
-                    os._exit(1)
-        finally:
-            gc.enable_finalizers()
-
-        if mode.startswith('r'):
-            os.close(write_end)
-            fd = read_end
-        else:
-            os.close(read_end)
-            fd = write_end
-        g = popenfile.fdopen(fd, mode, bufsize)
-        g._childpid = childpid
-        return g
-
-    except Exception, e:
-        try_close(write_end)
-        try_close(read_end)
-        raise Exception, e     # bare 'raise' does not work here :-(
+            gc.disable_finalizers()
+            try:
+                childpid = os.fork()
+                if childpid == 0:
+                    # in the child
+                    try:
+                        if mode.startswith('r'):
+                            os.dup2(write_end, 1)
+                            os.close(read_end)
+                        else:
+                            os.dup2(read_end, 0)
+                            os.close(write_end)
+                        os.closerange(3, MAXFD)
+                        cmd = ['/bin/sh', '-c', command]
+                        os.execvp(cmd[0], cmd)
+                    finally:
+                        os._exit(1)
+            finally:
+                gc.enable_finalizers()
+
+            if mode.startswith('r'):
+                os.close(write_end)
+                fd = read_end
+            else:
+                os.close(read_end)
+                fd = write_end
+            g = popenfile.fdopen(fd, mode, bufsize)
+            g._childpid = childpid
+            return g
+
+        except Exception, e:
+            try_close(write_end)
+            try_close(read_end)
+            raise Exception, e     # bare 'raise' does not work here :-(



More information about the Pypy-commit mailing list