[pypy-svn] r54443 - in pypy/dist/pypy: module/posix module/posix/test rlib

arigo at codespeak.net arigo at codespeak.net
Mon May 5 17:41:13 CEST 2008


Author: arigo
Date: Mon May  5 17:41:10 2008
New Revision: 54443

Modified:
   pypy/dist/pypy/module/posix/__init__.py
   pypy/dist/pypy/module/posix/app_posix.py
   pypy/dist/pypy/module/posix/interp_posix.py
   pypy/dist/pypy/module/posix/test/test_posix2.py
   pypy/dist/pypy/rlib/rposix.py
Log:
Move this loop at interp-level, using an os.closerange()-alike
helper (it's a new built-in in Python 2.6).


Modified: pypy/dist/pypy/module/posix/__init__.py
==============================================================================
--- pypy/dist/pypy/module/posix/__init__.py	(original)
+++ pypy/dist/pypy/module/posix/__init__.py	Mon May  5 17:41:10 2008
@@ -27,6 +27,7 @@
     'isatty'    : 'interp_posix.isatty',
     'read'      : 'interp_posix.read',
     'close'     : 'interp_posix.close',
+    'closerange': 'interp_posix.closerange',
     'fstat'     : 'interp_posix.fstat',
     'stat'      : 'interp_posix.stat',
     'lstat'     : 'interp_posix.lstat',

Modified: pypy/dist/pypy/module/posix/app_posix.py
==============================================================================
--- pypy/dist/pypy/module/posix/app_posix.py	(original)
+++ pypy/dist/pypy/module/posix/app_posix.py	Mon May  5 17:41:10 2008
@@ -115,8 +115,7 @@
                     else:
                         os.dup2(read_end, 0)
                         os.close(write_end)
-                    for i in range(3, MAXFD):    # XXX this loop can be slow
-                        try_close(i)
+                    os.closerange(3, MAXFD)
                     cmd = ['/bin/sh', '-c', command]
                     os.execvp(cmd[0], cmd)
                 finally:

Modified: pypy/dist/pypy/module/posix/interp_posix.py
==============================================================================
--- pypy/dist/pypy/module/posix/interp_posix.py	(original)
+++ pypy/dist/pypy/module/posix/interp_posix.py	Mon May  5 17:41:10 2008
@@ -1,4 +1,5 @@
 from pypy.interpreter.baseobjspace import ObjSpace, W_Root
+from pypy.rlib import rposix
 from pypy.rlib.rarithmetic import r_longlong
 from pypy.rlib.unroll import unrolling_iterable
 from pypy.interpreter.error import OperationError, wrap_oserror
@@ -70,6 +71,11 @@
         raise wrap_oserror(space, e) 
 close.unwrap_spec = [ObjSpace, int]
 
+def closerange(fd_low, fd_high):
+    """Closes all file descriptors in [fd_low, fd_high), ignoring errors."""
+    rposix.closerange(fd_low, fd_high)
+closerange.unwrap_spec = [int, int]
+
 def ftruncate(space, fd, length):
     """Truncate a file to a specified length."""
     try:

Modified: pypy/dist/pypy/module/posix/test/test_posix2.py
==============================================================================
--- pypy/dist/pypy/module/posix/test/test_posix2.py	(original)
+++ pypy/dist/pypy/module/posix/test/test_posix2.py	Mon May  5 17:41:10 2008
@@ -377,6 +377,23 @@
             assert os.WTERMSIG(status1) == self.SIGABRT
         pass # <- please, inspect.getsource(), don't crash
 
+    def test_closerange(self):
+        os = self.posix
+        if not hasattr(os, 'closerange'):
+            skip("missing os.closerange()")
+        fds = [os.open(self.path + str(i), os.O_CREAT|os.O_WRONLY, 0777)
+               for i in range(15)]
+        fds.sort()
+        start = fds.pop()
+        stop = start + 1
+        while len(fds) > 3 and fds[-1] == start - 1:
+            start = fds.pop()
+        os.closerange(start, stop)
+        for fd in fds:
+            os.close(fd)     # should not have been closed
+        for fd in range(start, stop):
+            raises(OSError, os.fstat, fd)   # should have been closed
+
 
 class AppTestEnvironment(object):
     def setup_class(cls): 

Modified: pypy/dist/pypy/rlib/rposix.py
==============================================================================
--- pypy/dist/pypy/rlib/rposix.py	(original)
+++ pypy/dist/pypy/rlib/rposix.py	Mon May  5 17:41:10 2008
@@ -1,3 +1,4 @@
+import os
 from pypy.rpython.lltypesystem.rffi import CConstant, CExternVariable, INT
 from pypy.rpython.lltypesystem import lltype, ll2ctypes
 from pypy.translator.tool.cbuild import ExternalCompilationInfo
@@ -26,3 +27,11 @@
 
 def get_errno():
     return intmask(_get_errno())
+
+def closerange(fd_low, fd_high):
+    # this behaves like os.closerange() from Python 2.6.
+    for fd in xrange(fd_low, fd_high):
+        try:
+            os.close(fd)
+        except OSError:
+            pass


More information about the pypy-svn mailing list