[pypy-commit] pypy win32-cleanup2: mmap(-1,...) test and fix

mattip noreply at buildbot.pypy.org
Mon Apr 16 05:59:07 CEST 2012


Author: Matti Picus <matti.picus at gmail.com>
Branch: win32-cleanup2
Changeset: r54381:0691f4fdc66f
Date: 2012-04-16 06:58 +0300
http://bitbucket.org/pypy/pypy/changeset/0691f4fdc66f/

Log:	mmap(-1,...) test and fix

diff --git a/pypy/rlib/rmmap.py b/pypy/rlib/rmmap.py
--- a/pypy/rlib/rmmap.py
+++ b/pypy/rlib/rmmap.py
@@ -739,6 +739,8 @@
         # assume -1 and 0 both mean invalid file descriptor
         # to 'anonymously' map memory.
         if fileno != -1 and fileno != 0:
+            if not rposix.validate_fd(fileno):
+                raise OSError(rposix.get_errno(), 'Bad file descriptor')
             fh = rwin32._get_osfhandle(fileno)
             if fh == INVALID_HANDLE:
                 errno = rposix.get_errno()
@@ -747,24 +749,24 @@
             # SEEK_SET = 0
             # libc._lseek(fileno, 0, SEEK_SET)
 
-        # check file size
-        try:
-            st = os.fstat(fileno)
-        except OSError:
-            pass     # ignore errors and trust map_size
-        else:
-            mode = st[stat.ST_MODE]
-            size = st[stat.ST_SIZE]
-            if stat.S_ISREG(mode):
-                if map_size == 0:
-                    if offset > size:
-                        raise RValueError(
-                            "mmap offset is greater than file size")
-                    map_size = int(size - offset)
-                    if map_size != size - offset:
-                        raise RValueError("mmap length is too large")
-                elif offset + map_size > size:
-                    raise RValueError("mmap length is greater than file size")
+            # check file size
+            try:
+                st = os.fstat(fileno)
+            except OSError:
+                pass     # ignore errors and trust map_size
+            else:
+                mode = st[stat.ST_MODE]
+                size = st[stat.ST_SIZE]
+                if stat.S_ISREG(mode):
+                    if map_size == 0:
+                        if offset > size:
+                            raise RValueError(
+                                "mmap offset is greater than file size")
+                        map_size = int(size - offset)
+                        if map_size != size - offset:
+                            raise RValueError("mmap length is too large")
+                    elif offset + map_size > size:
+                        raise RValueError("mmap length is greater than file size")
 
         m = MMap(access, offset)
         m.file_handle = INVALID_HANDLE
diff --git a/pypy/rlib/test/test_rmmap.py b/pypy/rlib/test/test_rmmap.py
--- a/pypy/rlib/test/test_rmmap.py
+++ b/pypy/rlib/test/test_rmmap.py
@@ -33,8 +33,6 @@
         interpret(f, [])
 
     def test_file_size(self):
-        if os.name == "nt":
-            skip("Only Unix checks file size")
         def func(no):
 
             try:
@@ -433,15 +431,16 @@
     def test_windows_crasher_1(self):
         if sys.platform != "win32":
             skip("Windows-only test")
-
-        m = mmap.mmap(-1, 1000, tagname="foo")
-        # same tagname, but larger size
-        try:
-            m2 = mmap.mmap(-1, 5000, tagname="foo")
-            m2.getitem(4500)
-        except WindowsError:
-            pass
-        m.close()
+        def func():
+            m = mmap.mmap(-1, 1000, tagname="foo")
+            # same tagname, but larger size
+            try:
+                m2 = mmap.mmap(-1, 5000, tagname="foo")
+                m2.getitem(4500)
+            except WindowsError:
+                pass
+            m.close()
+        interpret(func, [])
 
     def test_windows_crasher_2(self):
         if sys.platform != "win32":


More information about the pypy-commit mailing list