diff --git a/Doc/library/mmap.rst b/Doc/library/mmap.rst index 7a901c9..6dbe893 100644 --- a/Doc/library/mmap.rst +++ b/Doc/library/mmap.rst @@ -190,11 +190,13 @@ To map anonymous memory, -1 should be passed as the fileno along with the length move will raise a :exc:`TypeError` exception. - .. method:: read(num) + .. method:: read(n=-1) - Return a :class:`bytes` containing up to *num* bytes starting from the - current file position; the file position is updated to point after the - bytes that were returned. + Return a :class:`bytes` containing up to *n* bytes starting from + the current file position. If the argument is omitted or + negative, return all bytes from the current file position to the + end of the mapping. The file position is updated to point after + the bytes that were returned. .. method:: read_byte() diff --git a/Lib/test/test_mmap.py b/Lib/test/test_mmap.py index 712378b..e2dcb24 100644 --- a/Lib/test/test_mmap.py +++ b/Lib/test/test_mmap.py @@ -417,6 +417,17 @@ class MmapTests(unittest.TestCase): m[x] = b self.assertEqual(m[x], b) + def test_read_all(self): + # With no parameters, reads all + m = mmap.mmap(-1, 16) + m.write(bytes(range(16))) + m.seek(0) + self.assertEqual(m.read(), bytes(range(16))) + m.seek(8) + self.assertEqual(m.read(), bytes(range(8, 16))) + m.seek(16) + self.assertEqual(m.read(), b'') + def test_extended_getslice(self): # Test extended slicing by comparing with list slicing. s = bytes(reversed(range(256))) diff --git a/Modules/mmapmodule.c b/Modules/mmapmodule.c index 36ca67d..007a205 100644 --- a/Modules/mmapmodule.c +++ b/Modules/mmapmodule.c @@ -244,11 +244,11 @@ static PyObject * mmap_read_method(mmap_object *self, PyObject *args) { - Py_ssize_t num_bytes, n; + Py_ssize_t num_bytes = -1, n; PyObject *result; CHECK_VALID(NULL); - if (!PyArg_ParseTuple(args, "n:read", &num_bytes)) + if (!PyArg_ParseTuple(args, "|n:read", &num_bytes)) return(NULL); /* silently 'adjust' out-of-range requests */