[Python-checkins] CVS: python/dist/src/Lib/test test_mmap.py,1.17,1.18

Tim Peters tim_one@users.sourceforge.net
Tue, 13 Nov 2001 15:11:21 -0800


Update of /cvsroot/python/python/dist/src/Lib/test
In directory usw-pr-cvs1:/tmp/cvs-serv31339/python/Lib/test

Modified Files:
	test_mmap.py 
Log Message:
CVS patch #477161:  New "access" keyword for mmap, from Jay T Miller.
This gives mmap() on Windows the ability to create read-only, write-
through and copy-on-write mmaps.  A new keyword argument is introduced
because the mmap() signatures diverged between Windows and Unix, so
while they (now) both support this functionality, there wasn't a way to
spell it in a common way without introducing a new spelling gimmick.
The old spellings are still accepted, so there isn't a backward-
compatibility issue here.


Index: test_mmap.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/test/test_mmap.py,v
retrieving revision 1.17
retrieving revision 1.18
diff -C2 -d -r1.17 -r1.18
*** test_mmap.py	2001/05/11 14:29:21	1.17
--- test_mmap.py	2001/11/13 23:11:19	1.18
***************
*** 1,3 ****
! from test_support import verify, TESTFN
  import mmap
  import os, re
--- 1,3 ----
! from test_support import verify, vereq, TESTFN
  import mmap
  import os, re
***************
*** 26,38 ****
          print type(m)  # SF bug 128713:  segfaulted on Linux
          print '  Position of foo:', m.find('foo') / float(PAGESIZE), 'pages'
!         verify(m.find('foo') == PAGESIZE)
  
          print '  Length of file:', len(m) / float(PAGESIZE), 'pages'
!         verify(len(m) == 2*PAGESIZE)
  
          print '  Contents of byte 0:', repr(m[0])
!         verify(m[0] == '\0')
          print '  Contents of first 3 bytes:', repr(m[0:3])
!         verify(m[0:3] == '\0\0\0')
  
          # Modify the file's content
--- 26,38 ----
          print type(m)  # SF bug 128713:  segfaulted on Linux
          print '  Position of foo:', m.find('foo') / float(PAGESIZE), 'pages'
!         vereq(m.find('foo'), PAGESIZE)
  
          print '  Length of file:', len(m) / float(PAGESIZE), 'pages'
!         vereq(len(m), 2*PAGESIZE)
  
          print '  Contents of byte 0:', repr(m[0])
!         vereq(m[0], '\0')
          print '  Contents of first 3 bytes:', repr(m[0:3])
!         vereq(m[0:3], '\0\0\0')
  
          # Modify the file's content
***************
*** 43,51 ****
          # Check that the modification worked
          print '  Contents of byte 0:', repr(m[0])
!         verify(m[0] == '3')
          print '  Contents of first 3 bytes:', repr(m[0:3])
!         verify(m[0:3] == '3\0\0')
          print '  Contents of second page:',  repr(m[PAGESIZE-1 : PAGESIZE + 7])
!         verify(m[PAGESIZE-1 : PAGESIZE + 7] == '\0foobar\0')
  
          m.flush()
--- 43,51 ----
          # Check that the modification worked
          print '  Contents of byte 0:', repr(m[0])
!         vereq(m[0], '3')
          print '  Contents of first 3 bytes:', repr(m[0:3])
!         vereq(m[0:3], '3\0\0')
          print '  Contents of second page:',  repr(m[PAGESIZE-1 : PAGESIZE + 7])
!         vereq(m[PAGESIZE-1 : PAGESIZE + 7], '\0foobar\0')
  
          m.flush()
***************
*** 62,78 ****
              print start / float(PAGESIZE), length
  
!             verify(start == PAGESIZE)
!             verify(end == PAGESIZE + 6)
  
          # test seeking around (try to overflow the seek implementation)
          m.seek(0,0)
          print '  Seek to zeroth byte'
!         verify(m.tell() == 0)
          m.seek(42,1)
          print '  Seek to 42nd byte'
!         verify(m.tell() == 42)
          m.seek(0,2)
          print '  Seek to last byte'
!         verify(m.tell() == len(m))
  
          print '  Try to seek to negative position...'
--- 62,78 ----
              print start / float(PAGESIZE), length
  
!             vereq(start, PAGESIZE)
!             vereq(end, PAGESIZE + 6)
  
          # test seeking around (try to overflow the seek implementation)
          m.seek(0,0)
          print '  Seek to zeroth byte'
!         vereq(m.tell(), 0)
          m.seek(42,1)
          print '  Seek to 42nd byte'
!         vereq(m.tell(), 42)
          m.seek(0,2)
          print '  Seek to last byte'
!         vereq(m.tell(), len(m))
  
          print '  Try to seek to negative position...'
***************
*** 128,131 ****
--- 128,243 ----
          except OSError:
              pass
+         try:
+             os.unlink(TESTFN)
+         except OSError:
+             pass
+ 
+     # Test for "access" keyword parameter
+     try:
+         mapsize = 10
+         print "  Creating", mapsize, "byte test data file."
+         open(TESTFN, "wb").write("a"*mapsize)
+         print "  Opening mmap with access=ACCESS_READ"
+         f = open(TESTFN, "rb")
+         m = mmap.mmap(f.fileno(), mapsize, access=mmap.ACCESS_READ)
+         verify(m[:] == 'a'*mapsize, "Readonly memory map data incorrect.")
+ 
+         print "  Ensuring that readonly mmap can't be slice assigned."
+         try:
+             m[:] = 'b'*mapsize
+         except TypeError:
+             pass
+         else:
+             verify(0, "Able to write to readonly memory map")
+ 
+         print "  Ensuring that readonly mmap can't be item assigned."
+         try:
+             m[0] = 'b'
+         except TypeError:
+             pass
+         else:
+             verify(0, "Able to write to readonly memory map")
+ 
+         print "  Ensuring that readonly mmap can't be write() to."
+         try:
+             m.seek(0,0)
+             m.write('abc')
+         except TypeError:
+             pass
+         else:
+             verify(0, "Able to write to readonly memory map")
+ 
+         print "  Ensuring that readonly mmap can't be write_byte() to."
+         try:
+             m.seek(0,0)
+             m.write_byte('d')
+         except TypeError:
+             pass
+         else:
+             verify(0, "Able to write to readonly memory map")
+ 
+         print "  Ensuring that readonly mmap can't be resized."
+         try:
+             m.resize(2*mapsize)
+         except SystemError:   # resize is not universally supported
+             pass
+         except TypeError:
+             pass
+         else:
+             verify(0, "Able to resize readonly memory map")
+         del m, f
+         verify(open(TESTFN, "rb").read() == 'a'*mapsize,
+                "Readonly memory map data file was modified")
+ 
+         print "  Opening mmap with access=ACCESS_WRITE"
+         f = open(TESTFN, "r+b")
+         m = mmap.mmap(f.fileno(), mapsize, access=mmap.ACCESS_WRITE)
+         print "  Modifying write-through memory map."
+         m[:] = 'c'*mapsize
+         verify(m[:] == 'c'*mapsize,
+                "Write-through memory map memory not updated properly.")
+         m.flush()
+         del m, f
+         verify(open(TESTFN).read() == 'c'*mapsize,
+                "Write-through memory map data file not updated properly.")
+ 
+         print "  Opening mmap with access=ACCESS_COPY"
+         f = open(TESTFN, "r+b")
+         m = mmap.mmap(f.fileno(), mapsize, access=mmap.ACCESS_COPY)
+         print "  Modifying copy-on-write memory map."
+         m[:] = 'd'*mapsize
+         verify(m[:] == 'd' * mapsize,
+                "Copy-on-write memory map data not written correctly.")
+         m.flush()
+         verify(open(TESTFN, "rb").read() == 'c'*mapsize,
+                "Copy-on-write test data file should not be modified.")
+         try:
+             print "  Ensuring copy-on-write maps cannot be resized."
+             m.resize(2*mapsize)
+         except TypeError:
+             pass
+         else:
+             verify(0, "Copy-on-write mmap resize did not raise exception.")
+         del m, f
+         try:
+             print "  Ensuring invalid access parameter raises exception."
+             f = open(TESTFN, "r+b")
+             m = mmap.mmap(f.fileno(), mapsize, access=4)
+         except ValueError:
+             pass
+         else:
+             verify(0, "Invalid access code should have raised exception.")
+ 
+         if os.name == "posix":
+             print "  Trying incompatible flags, prot and access parameters."
+             f=open(TESTFN, "r+b")
+             try:
+                 m = mmap.mmap(f.fileno(), mapsize, flags=mmap.MAP_PRIVATE,
+                               prot=mmap.PROT_READ, access=mmap.ACCESS_WRITE)
+             except ValueError:
+                 pass
+             else:
+                 verify(0, "Incompatible parameters should raise ValueError.")
+     finally:
          try:
              os.unlink(TESTFN)