summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2007-07-16 19:29:02 (GMT)
committerGuido van Rossum <guido@python.org>2007-07-16 19:29:02 (GMT)
commitb358a2c4233f0e8c3cc58b86ed84007cc16ea74d (patch)
treefae988b888e2ce5ccb2daba682f3c3a7894d206f
parent88079f472ec290975f10f9bc02b24b67e4df60fa (diff)
downloadcpython-b358a2c4233f0e8c3cc58b86ed84007cc16ea74d.zip
cpython-b358a2c4233f0e8c3cc58b86ed84007cc16ea74d.tar.gz
cpython-b358a2c4233f0e8c3cc58b86ed84007cc16ea74d.tar.bz2
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.
-rw-r--r--Lib/test/test_mmap.py78
-rw-r--r--Modules/mmapmodule.c18
2 files changed, 49 insertions, 47 deletions
diff --git a/Lib/test/test_mmap.py b/Lib/test/test_mmap.py
index 605ad3d..efd1b1e 100644
--- a/Lib/test/test_mmap.py
+++ b/Lib/test/test_mmap.py
@@ -21,12 +21,12 @@ class MmapTests(unittest.TestCase):
# Test mmap module on Unix systems and Windows
# Create a file to be mmap'ed.
- f = open(TESTFN, 'w+')
+ f = open(TESTFN, 'bw+')
try:
# Write 2 pages worth of data to the file
- f.write('\0'* PAGESIZE)
- f.write('foo')
- f.write('\0'* (PAGESIZE-3) )
+ f.write(b'\0'* PAGESIZE)
+ f.write(b'foo')
+ f.write(b'\0'* (PAGESIZE-3) )
f.flush()
m = mmap.mmap(f.fileno(), 2 * PAGESIZE)
f.close()
@@ -38,17 +38,17 @@ class MmapTests(unittest.TestCase):
self.assertEqual(len(m), 2*PAGESIZE)
- self.assertEqual(m[0], '\0')
- self.assertEqual(m[0:3], '\0\0\0')
+ self.assertEqual(m[0], b'\0')
+ self.assertEqual(m[0:3], b'\0\0\0')
# Modify the file's content
- m[0] = '3'
- m[PAGESIZE +3: PAGESIZE +3+3] = 'bar'
+ m[0] = b'3'
+ m[PAGESIZE +3: PAGESIZE +3+3] = b'bar'
# Check that the modification worked
- self.assertEqual(m[0], '3')
- self.assertEqual(m[0:3], '3\0\0')
- self.assertEqual(m[PAGESIZE-1 : PAGESIZE + 7], '\0foobar\0')
+ self.assertEqual(m[0], b'3')
+ self.assertEqual(m[0:3], b'3\0\0')
+ self.assertEqual(m[PAGESIZE-1 : PAGESIZE + 7], b'\0foobar\0')
m.flush()
@@ -113,14 +113,14 @@ class MmapTests(unittest.TestCase):
def test_access_parameter(self):
# Test for "access" keyword parameter
mapsize = 10
- open(TESTFN, "wb").write("a"*mapsize)
+ open(TESTFN, "wb").write(b"a"*mapsize)
f = open(TESTFN, "rb")
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
try:
- m[:] = 'b'*mapsize
+ m[:] = b'b'*mapsize
except TypeError:
pass
else:
@@ -128,7 +128,7 @@ class MmapTests(unittest.TestCase):
# Ensuring that readonly mmap can't be item assigned
try:
- m[0] = 'b'
+ m[0] = b'b'
except TypeError:
pass
else:
@@ -137,7 +137,7 @@ class MmapTests(unittest.TestCase):
# Ensuring that readonly mmap can't be write() to
try:
m.seek(0,0)
- m.write('abc')
+ m.write(b'abc')
except TypeError:
pass
else:
@@ -146,7 +146,7 @@ class MmapTests(unittest.TestCase):
# Ensuring that readonly mmap can't be write_byte() to
try:
m.seek(0,0)
- m.write_byte('d')
+ m.write_byte(b'd')
except TypeError:
pass
else:
@@ -162,7 +162,7 @@ class MmapTests(unittest.TestCase):
else:
self.fail("Able to resize readonly memory map")
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")
# Opening mmap with size too big
@@ -193,8 +193,8 @@ class MmapTests(unittest.TestCase):
f = open(TESTFN, "r+b")
m = mmap.mmap(f.fileno(), mapsize, access=mmap.ACCESS_WRITE)
# Modifying write-through memory map
- m[:] = 'c'*mapsize
- self.assertEqual(m[:], 'c'*mapsize,
+ m[:] = b'c'*mapsize
+ self.assertEqual(m[:], b'c'*mapsize,
"Write-through memory map memory not updated properly.")
m.flush()
m.close()
@@ -202,18 +202,18 @@ class MmapTests(unittest.TestCase):
f = open(TESTFN, 'rb')
stuff = f.read()
f.close()
- self.assertEqual(stuff, 'c'*mapsize,
+ self.assertEqual(stuff, b'c'*mapsize,
"Write-through memory map data file not updated properly.")
# Opening mmap with access=ACCESS_COPY
f = open(TESTFN, "r+b")
m = mmap.mmap(f.fileno(), mapsize, access=mmap.ACCESS_COPY)
# Modifying copy-on-write memory map
- m[:] = 'd'*mapsize
- self.assertEqual(m[:], 'd' * mapsize,
+ m[:] = b'd'*mapsize
+ self.assertEqual(m[:], b'd' * mapsize,
"Copy-on-write memory map data not written correctly.")
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.")
# Ensuring copy-on-write maps cannot be resized
self.assertRaises(TypeError, m.resize, 2*mapsize)
@@ -239,9 +239,9 @@ class MmapTests(unittest.TestCase):
def test_tougher_find(self):
# 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+')
+ f = open(TESTFN, 'wb+')
- data = 'aabaac\x00deef\x00\x00aa\x00'
+ data = b'aabaac\x00deef\x00\x00aa\x00'
n = len(data)
f.write(data)
f.flush()
@@ -252,14 +252,14 @@ class MmapTests(unittest.TestCase):
for finish in range(start, n+1):
slice = data[start : finish]
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()
def test_double_close(self):
# 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 = open(TESTFN)
@@ -271,28 +271,28 @@ class MmapTests(unittest.TestCase):
def test_entire_file(self):
# test mapping of entire file by passing 0 for map length
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 = open(TESTFN, "rb+")
mf = mmap.mmap(f.fileno(), 0)
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()
f.close()
def test_move(self):
# 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()
mf = mmap.mmap(f.fileno(), 10)
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()
f.close()
@@ -300,11 +300,13 @@ class MmapTests(unittest.TestCase):
# anonymous mmap.mmap(-1, PAGE)
m = mmap.mmap(-1, 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):
- m[x] = ch = chr(x & 255)
- self.assertEqual(m[x], ch)
+ b[0] = x & 255
+ m[x] = b
+ self.assertEqual(m[x], b)
def test_main():
run_unittest(MmapTests)
diff --git a/Modules/mmapmodule.c b/Modules/mmapmodule.c
index d39169a..5afe715 100644
--- a/Modules/mmapmodule.c
+++ b/Modules/mmapmodule.c
@@ -206,7 +206,7 @@ mmap_read_line_method(mmap_object *self,
else
++eol; /* we're interested in the position after the
newline. */
- result = PyString_FromStringAndSize(start, (eol - start));
+ result = PyBytes_FromStringAndSize(start, (eol - start));
self->pos += (eol - start);
return result;
}
@@ -659,7 +659,7 @@ mmap_item(mmap_object *self, Py_ssize_t i)
PyErr_SetString(PyExc_IndexError, "mmap index out of range");
return NULL;
}
- return PyString_FromStringAndSize(self->data + i, 1);
+ return PyBytes_FromStringAndSize(self->data + i, 1);
}
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)
ihigh = self->size;
- return PyString_FromStringAndSize(self->data + ilow, ihigh-ilow);
+ return PyBytes_FromStringAndSize(self->data + ilow, ihigh-ilow);
}
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");
return -1;
}
- if (! (PyString_Check(v)) ) {
+ if (! (PyBytes_Check(v)) ) {
PyErr_SetString(PyExc_IndexError,
- "mmap slice assignment must be a string");
+ "mmap slice assignment must be bytes");
return -1;
}
- if (PyString_Size(v) != (ihigh - ilow)) {
+ if (PyBytes_Size(v) != (ihigh - ilow)) {
PyErr_SetString(PyExc_IndexError,
"mmap slice assignment is wrong size");
return -1;
}
if (!is_writeable(self))
return -1;
- buf = PyString_AsString(v);
+ buf = PyBytes_AsString(v);
memcpy(self->data + ilow, buf, ihigh-ilow);
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");
return -1;
}
- if (! (PyString_Check(v) && PyString_Size(v)==1) ) {
+ if (! (PyBytes_Check(v) && PyBytes_Size(v)==1) ) {
PyErr_SetString(PyExc_IndexError,
"mmap assignment must be single-character string");
return -1;
}
if (!is_writeable(self))
return -1;
- buf = PyString_AsString(v);
+ buf = PyBytes_AsString(v);
self->data[i] = buf[0];
return 0;
}