SF patch# 1752647 by Joe Gregorio.

Fixes to make test_mmap.py pass unit tests.
Moves the interface from being string based to being bytes based.
This commit is contained in:
Guido van Rossum 2007-07-16 19:29:02 +00:00
parent 88079f472e
commit b358a2c423
2 changed files with 49 additions and 47 deletions

View File

@ -21,12 +21,12 @@ class MmapTests(unittest.TestCase):
# Test mmap module on Unix systems and Windows # Test mmap module on Unix systems and Windows
# Create a file to be mmap'ed. # Create a file to be mmap'ed.
f = open(TESTFN, 'w+') f = open(TESTFN, 'bw+')
try: try:
# Write 2 pages worth of data to the file # Write 2 pages worth of data to the file
f.write('\0'* PAGESIZE) f.write(b'\0'* PAGESIZE)
f.write('foo') f.write(b'foo')
f.write('\0'* (PAGESIZE-3) ) f.write(b'\0'* (PAGESIZE-3) )
f.flush() f.flush()
m = mmap.mmap(f.fileno(), 2 * PAGESIZE) m = mmap.mmap(f.fileno(), 2 * PAGESIZE)
f.close() f.close()
@ -38,17 +38,17 @@ class MmapTests(unittest.TestCase):
self.assertEqual(len(m), 2*PAGESIZE) self.assertEqual(len(m), 2*PAGESIZE)
self.assertEqual(m[0], '\0') self.assertEqual(m[0], b'\0')
self.assertEqual(m[0:3], '\0\0\0') self.assertEqual(m[0:3], b'\0\0\0')
# Modify the file's content # Modify the file's content
m[0] = '3' m[0] = b'3'
m[PAGESIZE +3: PAGESIZE +3+3] = 'bar' m[PAGESIZE +3: PAGESIZE +3+3] = b'bar'
# Check that the modification worked # Check that the modification worked
self.assertEqual(m[0], '3') self.assertEqual(m[0], b'3')
self.assertEqual(m[0:3], '3\0\0') self.assertEqual(m[0:3], b'3\0\0')
self.assertEqual(m[PAGESIZE-1 : PAGESIZE + 7], '\0foobar\0') self.assertEqual(m[PAGESIZE-1 : PAGESIZE + 7], b'\0foobar\0')
m.flush() m.flush()
@ -113,14 +113,14 @@ class MmapTests(unittest.TestCase):
def test_access_parameter(self): def test_access_parameter(self):
# Test for "access" keyword parameter # Test for "access" keyword parameter
mapsize = 10 mapsize = 10
open(TESTFN, "wb").write("a"*mapsize) open(TESTFN, "wb").write(b"a"*mapsize)
f = open(TESTFN, "rb") f = open(TESTFN, "rb")
m = mmap.mmap(f.fileno(), mapsize, access=mmap.ACCESS_READ) m = mmap.mmap(f.fileno(), mapsize, access=mmap.ACCESS_READ)
self.assertEqual(m[:], 'a'*mapsize, "Readonly memory map data incorrect.") self.assertEqual(m[:], b'a'*mapsize, "Readonly memory map data incorrect.")
# Ensuring that readonly mmap can't be slice assigned # Ensuring that readonly mmap can't be slice assigned
try: try:
m[:] = 'b'*mapsize m[:] = b'b'*mapsize
except TypeError: except TypeError:
pass pass
else: else:
@ -128,7 +128,7 @@ class MmapTests(unittest.TestCase):
# Ensuring that readonly mmap can't be item assigned # Ensuring that readonly mmap can't be item assigned
try: try:
m[0] = 'b' m[0] = b'b'
except TypeError: except TypeError:
pass pass
else: else:
@ -137,7 +137,7 @@ class MmapTests(unittest.TestCase):
# Ensuring that readonly mmap can't be write() to # Ensuring that readonly mmap can't be write() to
try: try:
m.seek(0,0) m.seek(0,0)
m.write('abc') m.write(b'abc')
except TypeError: except TypeError:
pass pass
else: else:
@ -146,7 +146,7 @@ class MmapTests(unittest.TestCase):
# Ensuring that readonly mmap can't be write_byte() to # Ensuring that readonly mmap can't be write_byte() to
try: try:
m.seek(0,0) m.seek(0,0)
m.write_byte('d') m.write_byte(b'd')
except TypeError: except TypeError:
pass pass
else: else:
@ -162,7 +162,7 @@ class MmapTests(unittest.TestCase):
else: else:
self.fail("Able to resize readonly memory map") self.fail("Able to resize readonly memory map")
del m, f del m, f
self.assertEqual(open(TESTFN, "rb").read(), 'a'*mapsize, self.assertEqual(open(TESTFN, "rb").read(), b'a'*mapsize,
"Readonly memory map data file was modified") "Readonly memory map data file was modified")
# Opening mmap with size too big # Opening mmap with size too big
@ -193,8 +193,8 @@ class MmapTests(unittest.TestCase):
f = open(TESTFN, "r+b") f = open(TESTFN, "r+b")
m = mmap.mmap(f.fileno(), mapsize, access=mmap.ACCESS_WRITE) m = mmap.mmap(f.fileno(), mapsize, access=mmap.ACCESS_WRITE)
# Modifying write-through memory map # Modifying write-through memory map
m[:] = 'c'*mapsize m[:] = b'c'*mapsize
self.assertEqual(m[:], 'c'*mapsize, self.assertEqual(m[:], b'c'*mapsize,
"Write-through memory map memory not updated properly.") "Write-through memory map memory not updated properly.")
m.flush() m.flush()
m.close() m.close()
@ -202,18 +202,18 @@ class MmapTests(unittest.TestCase):
f = open(TESTFN, 'rb') f = open(TESTFN, 'rb')
stuff = f.read() stuff = f.read()
f.close() f.close()
self.assertEqual(stuff, 'c'*mapsize, self.assertEqual(stuff, b'c'*mapsize,
"Write-through memory map data file not updated properly.") "Write-through memory map data file not updated properly.")
# Opening mmap with access=ACCESS_COPY # Opening mmap with access=ACCESS_COPY
f = open(TESTFN, "r+b") f = open(TESTFN, "r+b")
m = mmap.mmap(f.fileno(), mapsize, access=mmap.ACCESS_COPY) m = mmap.mmap(f.fileno(), mapsize, access=mmap.ACCESS_COPY)
# Modifying copy-on-write memory map # Modifying copy-on-write memory map
m[:] = 'd'*mapsize m[:] = b'd'*mapsize
self.assertEqual(m[:], 'd' * mapsize, self.assertEqual(m[:], b'd' * mapsize,
"Copy-on-write memory map data not written correctly.") "Copy-on-write memory map data not written correctly.")
m.flush() m.flush()
self.assertEqual(open(TESTFN, "rb").read(), 'c'*mapsize, self.assertEqual(open(TESTFN, "rb").read(), b'c'*mapsize,
"Copy-on-write test data file should not be modified.") "Copy-on-write test data file should not be modified.")
# Ensuring copy-on-write maps cannot be resized # Ensuring copy-on-write maps cannot be resized
self.assertRaises(TypeError, m.resize, 2*mapsize) self.assertRaises(TypeError, m.resize, 2*mapsize)
@ -239,9 +239,9 @@ class MmapTests(unittest.TestCase):
def test_tougher_find(self): def test_tougher_find(self):
# Do a tougher .find() test. SF bug 515943 pointed out that, in 2.2, # Do a tougher .find() test. SF bug 515943 pointed out that, in 2.2,
# searching for data with embedded \0 bytes didn't work. # searching for data with embedded \0 bytes didn't work.
f = open(TESTFN, 'w+') f = open(TESTFN, 'wb+')
data = 'aabaac\x00deef\x00\x00aa\x00' data = b'aabaac\x00deef\x00\x00aa\x00'
n = len(data) n = len(data)
f.write(data) f.write(data)
f.flush() f.flush()
@ -252,14 +252,14 @@ class MmapTests(unittest.TestCase):
for finish in range(start, n+1): for finish in range(start, n+1):
slice = data[start : finish] slice = data[start : finish]
self.assertEqual(m.find(slice), data.find(slice)) self.assertEqual(m.find(slice), data.find(slice))
self.assertEqual(m.find(slice + 'x'), -1) self.assertEqual(m.find(slice + b'x'), -1)
m.close() m.close()
def test_double_close(self): def test_double_close(self):
# make sure a double close doesn't crash on Solaris (Bug# 665913) # make sure a double close doesn't crash on Solaris (Bug# 665913)
f = open(TESTFN, 'w+') f = open(TESTFN, 'wb+')
f.write(2**16 * 'a') # Arbitrary character f.write(2**16 * b'a') # Arbitrary character
f.close() f.close()
f = open(TESTFN) f = open(TESTFN)
@ -271,28 +271,28 @@ class MmapTests(unittest.TestCase):
def test_entire_file(self): def test_entire_file(self):
# test mapping of entire file by passing 0 for map length # test mapping of entire file by passing 0 for map length
if hasattr(os, "stat"): if hasattr(os, "stat"):
f = open(TESTFN, "w+") f = open(TESTFN, "wb+")
f.write(2**16 * 'm') # Arbitrary character f.write(2**16 * b'm') # Arbitrary character
f.close() f.close()
f = open(TESTFN, "rb+") f = open(TESTFN, "rb+")
mf = mmap.mmap(f.fileno(), 0) mf = mmap.mmap(f.fileno(), 0)
self.assertEqual(len(mf), 2**16, "Map size should equal file size.") self.assertEqual(len(mf), 2**16, "Map size should equal file size.")
self.assertEqual(mf.read(2**16), 2**16 * "m") self.assertEqual(mf.read(2**16), 2**16 * b"m")
mf.close() mf.close()
f.close() f.close()
def test_move(self): def test_move(self):
# make move works everywhere (64-bit format problem earlier) # make move works everywhere (64-bit format problem earlier)
f = open(TESTFN, 'w+') f = open(TESTFN, 'wb+')
f.write("ABCDEabcde") # Arbitrary character f.write(b"ABCDEabcde") # Arbitrary character
f.flush() f.flush()
mf = mmap.mmap(f.fileno(), 10) mf = mmap.mmap(f.fileno(), 10)
mf.move(5, 0, 5) mf.move(5, 0, 5)
self.assertEqual(mf[:], "ABCDEABCDE", "Map move should have duplicated front 5") self.assertEqual(mf[:], b"ABCDEABCDE", "Map move should have duplicated front 5")
mf.close() mf.close()
f.close() f.close()
@ -300,11 +300,13 @@ class MmapTests(unittest.TestCase):
# anonymous mmap.mmap(-1, PAGE) # anonymous mmap.mmap(-1, PAGE)
m = mmap.mmap(-1, PAGESIZE) m = mmap.mmap(-1, PAGESIZE)
for x in range(PAGESIZE): for x in range(PAGESIZE):
self.assertEqual(m[x], '\0', "anonymously mmap'ed contents should be zero") self.assertEqual(m[x], b'\0', "anonymously mmap'ed contents should be zero")
b = bytes(1)
for x in range(PAGESIZE): for x in range(PAGESIZE):
m[x] = ch = chr(x & 255) b[0] = x & 255
self.assertEqual(m[x], ch) m[x] = b
self.assertEqual(m[x], b)
def test_main(): def test_main():
run_unittest(MmapTests) run_unittest(MmapTests)

View File

@ -206,7 +206,7 @@ mmap_read_line_method(mmap_object *self,
else else
++eol; /* we're interested in the position after the ++eol; /* we're interested in the position after the
newline. */ newline. */
result = PyString_FromStringAndSize(start, (eol - start)); result = PyBytes_FromStringAndSize(start, (eol - start));
self->pos += (eol - start); self->pos += (eol - start);
return result; return result;
} }
@ -659,7 +659,7 @@ mmap_item(mmap_object *self, Py_ssize_t i)
PyErr_SetString(PyExc_IndexError, "mmap index out of range"); PyErr_SetString(PyExc_IndexError, "mmap index out of range");
return NULL; return NULL;
} }
return PyString_FromStringAndSize(self->data + i, 1); return PyBytes_FromStringAndSize(self->data + i, 1);
} }
static PyObject * static PyObject *
@ -677,7 +677,7 @@ mmap_slice(mmap_object *self, Py_ssize_t ilow, Py_ssize_t ihigh)
else if ((size_t)ihigh > self->size) else if ((size_t)ihigh > self->size)
ihigh = self->size; ihigh = self->size;
return PyString_FromStringAndSize(self->data + ilow, ihigh-ilow); return PyBytes_FromStringAndSize(self->data + ilow, ihigh-ilow);
} }
static PyObject * static PyObject *
@ -720,19 +720,19 @@ mmap_ass_slice(mmap_object *self, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v
"mmap object doesn't support slice deletion"); "mmap object doesn't support slice deletion");
return -1; return -1;
} }
if (! (PyString_Check(v)) ) { if (! (PyBytes_Check(v)) ) {
PyErr_SetString(PyExc_IndexError, PyErr_SetString(PyExc_IndexError,
"mmap slice assignment must be a string"); "mmap slice assignment must be bytes");
return -1; return -1;
} }
if (PyString_Size(v) != (ihigh - ilow)) { if (PyBytes_Size(v) != (ihigh - ilow)) {
PyErr_SetString(PyExc_IndexError, PyErr_SetString(PyExc_IndexError,
"mmap slice assignment is wrong size"); "mmap slice assignment is wrong size");
return -1; return -1;
} }
if (!is_writeable(self)) if (!is_writeable(self))
return -1; return -1;
buf = PyString_AsString(v); buf = PyBytes_AsString(v);
memcpy(self->data + ilow, buf, ihigh-ilow); memcpy(self->data + ilow, buf, ihigh-ilow);
return 0; return 0;
} }
@ -752,14 +752,14 @@ mmap_ass_item(mmap_object *self, Py_ssize_t i, PyObject *v)
"mmap object doesn't support item deletion"); "mmap object doesn't support item deletion");
return -1; return -1;
} }
if (! (PyString_Check(v) && PyString_Size(v)==1) ) { if (! (PyBytes_Check(v) && PyBytes_Size(v)==1) ) {
PyErr_SetString(PyExc_IndexError, PyErr_SetString(PyExc_IndexError,
"mmap assignment must be single-character string"); "mmap assignment must be single-character string");
return -1; return -1;
} }
if (!is_writeable(self)) if (!is_writeable(self))
return -1; return -1;
buf = PyString_AsString(v); buf = PyBytes_AsString(v);
self->data[i] = buf[0]; self->data[i] = buf[0];
return 0; return 0;
} }