2002-07-23 16:04:11 -03:00
|
|
|
from test.test_support import verify, vereq, TESTFN
|
2000-03-30 17:15:29 -04:00
|
|
|
import mmap
|
2001-05-11 11:29:21 -03:00
|
|
|
import os, re
|
2000-03-30 17:15:29 -04:00
|
|
|
|
|
|
|
PAGESIZE = mmap.PAGESIZE
|
|
|
|
|
2000-03-30 21:09:14 -04:00
|
|
|
def test_both():
|
|
|
|
"Test mmap module on Unix systems and Windows"
|
2000-10-23 14:22:08 -03:00
|
|
|
|
2001-05-10 17:03:04 -03:00
|
|
|
# Create a file to be mmap'ed.
|
2001-05-11 11:29:21 -03:00
|
|
|
if os.path.exists(TESTFN):
|
|
|
|
os.unlink(TESTFN)
|
2001-05-10 17:03:04 -03:00
|
|
|
f = open(TESTFN, 'w+')
|
|
|
|
|
|
|
|
try: # unlink TESTFN no matter what
|
|
|
|
# Write 2 pages worth of data to the file
|
|
|
|
f.write('\0'* PAGESIZE)
|
|
|
|
f.write('foo')
|
|
|
|
f.write('\0'* (PAGESIZE-3) )
|
2002-04-23 20:07:28 -03:00
|
|
|
f.flush()
|
2001-05-10 17:03:04 -03:00
|
|
|
m = mmap.mmap(f.fileno(), 2 * PAGESIZE)
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
# Simple sanity checks
|
|
|
|
|
|
|
|
print type(m) # SF bug 128713: segfaulted on Linux
|
|
|
|
print ' Position of foo:', m.find('foo') / float(PAGESIZE), 'pages'
|
2001-11-13 19:11:19 -04:00
|
|
|
vereq(m.find('foo'), PAGESIZE)
|
2001-05-10 17:03:04 -03:00
|
|
|
|
|
|
|
print ' Length of file:', len(m) / float(PAGESIZE), 'pages'
|
2001-11-13 19:11:19 -04:00
|
|
|
vereq(len(m), 2*PAGESIZE)
|
2001-05-10 17:03:04 -03:00
|
|
|
|
|
|
|
print ' Contents of byte 0:', repr(m[0])
|
2001-11-13 19:11:19 -04:00
|
|
|
vereq(m[0], '\0')
|
2001-05-10 17:03:04 -03:00
|
|
|
print ' Contents of first 3 bytes:', repr(m[0:3])
|
2001-11-13 19:11:19 -04:00
|
|
|
vereq(m[0:3], '\0\0\0')
|
2001-05-10 17:03:04 -03:00
|
|
|
|
|
|
|
# Modify the file's content
|
|
|
|
print "\n Modifying file's content..."
|
|
|
|
m[0] = '3'
|
2001-05-11 11:29:21 -03:00
|
|
|
m[PAGESIZE +3: PAGESIZE +3+3] = 'bar'
|
2001-05-10 17:03:04 -03:00
|
|
|
|
|
|
|
# Check that the modification worked
|
|
|
|
print ' Contents of byte 0:', repr(m[0])
|
2001-11-13 19:11:19 -04:00
|
|
|
vereq(m[0], '3')
|
2001-05-10 17:03:04 -03:00
|
|
|
print ' Contents of first 3 bytes:', repr(m[0:3])
|
2001-11-13 19:11:19 -04:00
|
|
|
vereq(m[0:3], '3\0\0')
|
2001-05-10 17:03:04 -03:00
|
|
|
print ' Contents of second page:', repr(m[PAGESIZE-1 : PAGESIZE + 7])
|
2001-11-13 19:11:19 -04:00
|
|
|
vereq(m[PAGESIZE-1 : PAGESIZE + 7], '\0foobar\0')
|
2001-05-10 17:03:04 -03:00
|
|
|
|
|
|
|
m.flush()
|
|
|
|
|
|
|
|
# Test doing a regular expression match in an mmap'ed file
|
2001-05-11 11:29:21 -03:00
|
|
|
match = re.search('[A-Za-z]+', m)
|
2001-05-10 17:03:04 -03:00
|
|
|
if match is None:
|
|
|
|
print ' ERROR: regex match on mmap failed!'
|
|
|
|
else:
|
|
|
|
start, end = match.span(0)
|
|
|
|
length = end - start
|
|
|
|
|
|
|
|
print ' Regex match on mmap (page start, length of match):',
|
|
|
|
print start / float(PAGESIZE), length
|
|
|
|
|
2001-11-13 19:11:19 -04:00
|
|
|
vereq(start, PAGESIZE)
|
|
|
|
vereq(end, PAGESIZE + 6)
|
2001-05-10 17:03:04 -03:00
|
|
|
|
|
|
|
# test seeking around (try to overflow the seek implementation)
|
|
|
|
m.seek(0,0)
|
|
|
|
print ' Seek to zeroth byte'
|
2001-11-13 19:11:19 -04:00
|
|
|
vereq(m.tell(), 0)
|
2001-05-10 17:03:04 -03:00
|
|
|
m.seek(42,1)
|
|
|
|
print ' Seek to 42nd byte'
|
2001-11-13 19:11:19 -04:00
|
|
|
vereq(m.tell(), 42)
|
2001-05-10 17:03:04 -03:00
|
|
|
m.seek(0,2)
|
|
|
|
print ' Seek to last byte'
|
2001-11-13 19:11:19 -04:00
|
|
|
vereq(m.tell(), len(m))
|
2001-05-10 17:03:04 -03:00
|
|
|
|
|
|
|
print ' Try to seek to negative position...'
|
2000-07-30 12:38:35 -03:00
|
|
|
try:
|
2001-05-10 17:03:04 -03:00
|
|
|
m.seek(-1)
|
2000-07-30 12:38:35 -03:00
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
else:
|
2001-05-10 17:03:04 -03:00
|
|
|
verify(0, 'expected a ValueError but did not get it')
|
|
|
|
|
|
|
|
print ' Try to seek beyond end of mmap...'
|
|
|
|
try:
|
|
|
|
m.seek(1,2)
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
verify(0, 'expected a ValueError but did not get it')
|
|
|
|
|
|
|
|
print ' Try to seek to negative position...'
|
|
|
|
try:
|
|
|
|
m.seek(-len(m)-1,2)
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
verify(0, 'expected a ValueError but did not get it')
|
|
|
|
|
|
|
|
# Try resizing map
|
|
|
|
print ' Attempting resize()'
|
|
|
|
try:
|
2002-09-10 17:49:15 -03:00
|
|
|
m.resize(512)
|
2001-05-10 17:03:04 -03:00
|
|
|
except SystemError:
|
|
|
|
# resize() not supported
|
|
|
|
# No messages are printed, since the output of this test suite
|
|
|
|
# would then be different across platforms.
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
# resize() is supported
|
|
|
|
verify(len(m) == 512,
|
|
|
|
"len(m) is %d, but expecting 512" % (len(m),) )
|
|
|
|
# Check that we can no longer seek beyond the new size.
|
|
|
|
try:
|
|
|
|
m.seek(513,0)
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
verify(0, 'Could seek beyond the new size')
|
|
|
|
|
2005-08-24 04:17:40 -03:00
|
|
|
# Check that the underlying file is truncated too
|
|
|
|
# (bug #728515)
|
|
|
|
f = open(TESTFN)
|
|
|
|
f.seek(0, 2)
|
|
|
|
verify(f.tell() == 512, 'Underlying file not truncated')
|
|
|
|
f.close()
|
2005-08-26 12:20:46 -03:00
|
|
|
verify(m.size() == 512, 'New size not reflected in file')
|
2005-08-24 04:17:40 -03:00
|
|
|
|
2001-05-10 17:03:04 -03:00
|
|
|
m.close()
|
|
|
|
|
|
|
|
finally:
|
|
|
|
try:
|
|
|
|
f.close()
|
|
|
|
except OSError:
|
|
|
|
pass
|
|
|
|
try:
|
2001-05-11 11:29:21 -03:00
|
|
|
os.unlink(TESTFN)
|
2001-05-10 17:03:04 -03:00
|
|
|
except OSError:
|
|
|
|
pass
|
2000-10-23 14:22:08 -03:00
|
|
|
|
2001-11-13 19:11:19 -04:00
|
|
|
# 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")
|
|
|
|
|
2002-09-05 18:48:07 -03:00
|
|
|
print " Opening mmap with size too big"
|
|
|
|
import sys
|
|
|
|
f = open(TESTFN, "r+b")
|
|
|
|
try:
|
|
|
|
m = mmap.mmap(f.fileno(), mapsize+1)
|
|
|
|
except ValueError:
|
|
|
|
# we do not expect a ValueError on Windows
|
2002-09-10 17:49:15 -03:00
|
|
|
# CAUTION: This also changes the size of the file on disk, and
|
|
|
|
# later tests assume that the length hasn't changed. We need to
|
|
|
|
# repair that.
|
2002-09-05 18:48:07 -03:00
|
|
|
if sys.platform.startswith('win'):
|
|
|
|
verify(0, "Opening mmap with size+1 should work on Windows.")
|
|
|
|
else:
|
|
|
|
# we expect a ValueError on Unix, but not on Windows
|
|
|
|
if not sys.platform.startswith('win'):
|
|
|
|
verify(0, "Opening mmap with size+1 should raise ValueError.")
|
2002-09-10 23:56:42 -03:00
|
|
|
m.close()
|
2002-09-10 17:49:15 -03:00
|
|
|
f.close()
|
|
|
|
if sys.platform.startswith('win'):
|
|
|
|
# Repair damage from the resizing test.
|
|
|
|
f = open(TESTFN, 'r+b')
|
|
|
|
f.truncate(mapsize)
|
|
|
|
f.close()
|
2002-09-05 18:48:07 -03:00
|
|
|
|
2001-11-13 19:11:19 -04:00
|
|
|
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()
|
2002-09-10 18:19:55 -03:00
|
|
|
m.close()
|
|
|
|
f.close()
|
2002-09-10 17:49:15 -03:00
|
|
|
f = open(TESTFN, 'rb')
|
|
|
|
stuff = f.read()
|
|
|
|
f.close()
|
2002-09-10 18:19:55 -03:00
|
|
|
verify(stuff == 'c'*mapsize,
|
2001-11-13 19:11:19 -04:00
|
|
|
"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":
|
2001-11-13 19:39:47 -04:00
|
|
|
# Try incompatible flags, prot and access parameters.
|
|
|
|
f = open(TESTFN, "r+b")
|
2001-11-13 19:11:19 -04:00
|
|
|
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.")
|
2002-04-18 01:30:18 -03:00
|
|
|
f.close()
|
2001-11-13 19:11:19 -04:00
|
|
|
finally:
|
|
|
|
try:
|
|
|
|
os.unlink(TESTFN)
|
|
|
|
except OSError:
|
|
|
|
pass
|
|
|
|
|
2006-01-11 04:54:45 -04:00
|
|
|
print ' Try opening a bad file descriptor...'
|
|
|
|
try:
|
2006-02-05 01:45:43 -04:00
|
|
|
mmap.mmap(-2, 4096)
|
2006-01-11 04:54:45 -04:00
|
|
|
except mmap.error:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
verify(0, 'expected a mmap.error but did not get it')
|
|
|
|
|
2002-03-08 01:43:32 -04:00
|
|
|
# Do a tougher .find() test. SF bug 515943 pointed out that, in 2.2,
|
|
|
|
# searching for data with embedded \0 bytes didn't work.
|
|
|
|
f = open(TESTFN, 'w+')
|
|
|
|
|
|
|
|
try: # unlink TESTFN no matter what
|
|
|
|
data = 'aabaac\x00deef\x00\x00aa\x00'
|
|
|
|
n = len(data)
|
|
|
|
f.write(data)
|
2002-04-18 01:30:18 -03:00
|
|
|
f.flush()
|
2002-03-08 01:43:32 -04:00
|
|
|
m = mmap.mmap(f.fileno(), n)
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
for start in range(n+1):
|
|
|
|
for finish in range(start, n+1):
|
|
|
|
slice = data[start : finish]
|
|
|
|
vereq(m.find(slice), data.find(slice))
|
|
|
|
vereq(m.find(slice + 'x'), -1)
|
2003-01-13 17:38:45 -04:00
|
|
|
m.close()
|
2002-03-08 01:43:32 -04:00
|
|
|
|
|
|
|
finally:
|
2003-01-13 17:38:45 -04:00
|
|
|
os.unlink(TESTFN)
|
2002-03-08 01:43:32 -04:00
|
|
|
|
2003-01-10 16:52:16 -04:00
|
|
|
# make sure a double close doesn't crash on Solaris (Bug# 665913)
|
|
|
|
f = open(TESTFN, 'w+')
|
|
|
|
|
|
|
|
try: # unlink TESTFN no matter what
|
2003-01-13 17:38:45 -04:00
|
|
|
f.write(2**16 * 'a') # Arbitrary character
|
2003-01-10 16:52:16 -04:00
|
|
|
f.close()
|
|
|
|
|
|
|
|
f = open(TESTFN)
|
2003-01-13 17:38:45 -04:00
|
|
|
mf = mmap.mmap(f.fileno(), 2**16, access=mmap.ACCESS_READ)
|
2003-01-10 16:52:16 -04:00
|
|
|
mf.close()
|
|
|
|
mf.close()
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
finally:
|
2003-01-13 17:38:45 -04:00
|
|
|
os.unlink(TESTFN)
|
2002-03-08 01:43:32 -04:00
|
|
|
|
2005-03-03 07:22:44 -04:00
|
|
|
# test mapping of entire file by passing 0 for map length
|
|
|
|
if hasattr(os, "stat"):
|
|
|
|
print " Ensuring that passing 0 as map length sets map size to current file size."
|
|
|
|
f = open(TESTFN, "w+")
|
2002-03-08 01:43:32 -04:00
|
|
|
|
2005-03-03 07:22:44 -04:00
|
|
|
try:
|
|
|
|
f.write(2**16 * 'm') # Arbitrary character
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
f = open(TESTFN, "rb+")
|
2005-03-27 21:08:02 -04:00
|
|
|
mf = mmap.mmap(f.fileno(), 0)
|
2005-03-03 07:22:44 -04:00
|
|
|
verify(len(mf) == 2**16, "Map size should equal file size.")
|
|
|
|
vereq(mf.read(2**16), 2**16 * "m")
|
|
|
|
mf.close()
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
finally:
|
|
|
|
os.unlink(TESTFN)
|
2005-03-27 21:08:02 -04:00
|
|
|
|
2005-03-03 07:22:44 -04:00
|
|
|
# test mapping of entire file by passing 0 for map length
|
|
|
|
if hasattr(os, "stat"):
|
|
|
|
print " Ensuring that passing 0 as map length sets map size to current file size."
|
|
|
|
f = open(TESTFN, "w+")
|
|
|
|
try:
|
|
|
|
f.write(2**16 * 'm') # Arbitrary character
|
|
|
|
f.close()
|
2005-03-27 21:08:02 -04:00
|
|
|
|
2005-03-03 07:22:44 -04:00
|
|
|
f = open(TESTFN, "rb+")
|
2005-03-27 21:08:02 -04:00
|
|
|
mf = mmap.mmap(f.fileno(), 0)
|
2005-03-03 07:22:44 -04:00
|
|
|
verify(len(mf) == 2**16, "Map size should equal file size.")
|
|
|
|
vereq(mf.read(2**16), 2**16 * "m")
|
|
|
|
mf.close()
|
|
|
|
f.close()
|
2005-03-27 21:08:02 -04:00
|
|
|
|
2005-03-03 07:22:44 -04:00
|
|
|
finally:
|
|
|
|
os.unlink(TESTFN)
|
2005-03-27 21:08:02 -04:00
|
|
|
|
2005-12-17 23:34:22 -04:00
|
|
|
# make move works everywhere (64-bit format problem earlier)
|
|
|
|
f = open(TESTFN, 'w+')
|
|
|
|
|
|
|
|
try: # unlink TESTFN no matter what
|
|
|
|
f.write("ABCDEabcde") # Arbitrary character
|
|
|
|
f.flush()
|
|
|
|
|
|
|
|
mf = mmap.mmap(f.fileno(), 10)
|
|
|
|
mf.move(5, 0, 5)
|
|
|
|
verify(mf[:] == "ABCDEABCDE", "Map move should have duplicated front 5")
|
|
|
|
mf.close()
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
finally:
|
|
|
|
os.unlink(TESTFN)
|
|
|
|
|
2006-02-05 01:45:43 -04:00
|
|
|
def test_anon():
|
|
|
|
print " anonymous mmap.mmap(-1, PAGESIZE)..."
|
|
|
|
m = mmap.mmap(-1, PAGESIZE)
|
|
|
|
for x in xrange(PAGESIZE):
|
|
|
|
verify(m[x] == '\0', "anonymously mmap'ed contents should be zero")
|
|
|
|
|
|
|
|
for x in xrange(PAGESIZE):
|
|
|
|
m[x] = ch = chr(x & 255)
|
|
|
|
vereq(m[x], ch)
|
2000-03-30 17:15:29 -04:00
|
|
|
|
2000-03-30 21:09:14 -04:00
|
|
|
test_both()
|
2006-02-05 01:45:43 -04:00
|
|
|
test_anon()
|
|
|
|
print ' Test passed'
|