diff --git a/Doc/library/mmap.rst b/Doc/library/mmap.rst index b74a823..027d013 100644 --- a/Doc/library/mmap.rst +++ b/Doc/library/mmap.rst @@ -263,7 +263,9 @@ To map anonymous memory, -1 should be passed as the fileno along with the length .. method:: write(bytes) Write the bytes in *bytes* into memory at the current position of the - file pointer; the file position is updated to point after the bytes that + file pointer and return the number of bytes written (never less than + len(bytes), since if the write fails a :exc:`ValueError` will be + raised). The file position is updated to point after the bytes that were written. If the mmap was created with :const:`ACCESS_READ`, then writing to it will raise a :exc:`TypeError` exception. diff --git a/Lib/test/test_mmap.py b/Lib/test/test_mmap.py index 0f25742..4851f8c 100644 --- a/Lib/test/test_mmap.py +++ b/Lib/test/test_mmap.py @@ -32,9 +32,9 @@ class MmapTests(unittest.TestCase): f = open(TESTFN, 'bw+') try: # Write 2 pages worth of data to the file - f.write(b'\0'* PAGESIZE) - f.write(b'foo') - f.write(b'\0'* (PAGESIZE-3) ) + self.assertEqual(f.write(b'\0'* PAGESIZE), PAGESIZE) + self.assertEqual(f.write(b'foo'), 3) + self.assertEqual(f.write(b'\0'* (PAGESIZE-3) ), PAGESIZE - 3) f.flush() m = mmap.mmap(f.fileno(), 2 * PAGESIZE) finally: diff --git a/Modules/mmapmodule.c b/Modules/mmapmodule.c index bb98a99..28a6b83 100644 --- a/Modules/mmapmodule.c +++ b/Modules/mmapmodule.c @@ -408,7 +408,7 @@ mmap_write_method(mmap_object *self, self->pos = self->pos + data.len; PyBuffer_Release(&data); Py_INCREF(Py_None); - return Py_None; + return PyLong_FromSsize_t(data.len); } static PyObject *