diff --git a/Doc/library/mmap.rst b/Doc/library/mmap.rst index 33baf2b..ce59b89 100644 --- a/Doc/library/mmap.rst +++ b/Doc/library/mmap.rst @@ -263,13 +263,18 @@ 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. .. versionchanged: 3.5 Writable :term:`bytes-like object` is now accepted. + .. versionchanged:: 3.6 + The number of bytes written is now returned (previously: *None*). + .. method:: write_byte(byte) diff --git a/Doc/whatsnew/3.6.rst b/Doc/whatsnew/3.6.rst index 5c02b7d..189faed 100644 --- a/Doc/whatsnew/3.6.rst +++ b/Doc/whatsnew/3.6.rst @@ -122,6 +122,12 @@ directives ``%G``, ``%u`` and ``%V``. (Contributed by Ashley Anderson in :issue:`12006`.) +mmap +---- + +The :meth:`~mmap.mmap.write` method now returns the number of bytes written. + + os -- diff --git a/Lib/test/test_mmap.py b/Lib/test/test_mmap.py index 0f25742..97aab01 100644 --- a/Lib/test/test_mmap.py +++ b/Lib/test/test_mmap.py @@ -713,6 +713,10 @@ class MmapTests(unittest.TestCase): gc_collect() self.assertIs(wr(), None) + def test_write_returning_the_number_of_bytes_written(self): + mm = mmap.mmap(-1, 16) + self.assertEqual(mm.write(b"x"), 1) + class LargeMmapTests(unittest.TestCase): def setUp(self): diff --git a/Misc/NEWS b/Misc/NEWS index ba48048..9a2f580 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -603,6 +603,9 @@ Issue #26186: Remove an invalid type check in importlib.util.LazyLoader. - Issue #26406: Avoid unnecessary serialization of getaddrinfo(3) calls on current versions of OpenBSD and NetBSD. Patch by A. Jesse Jiryu Davis. +- Issue #26335: Make mmap.write return the number of bytes written like + other write methods. + IDLE ---- diff --git a/Modules/mmapmodule.c b/Modules/mmapmodule.c index bb98a99..50cec24 100644 --- a/Modules/mmapmodule.c +++ b/Modules/mmapmodule.c @@ -389,6 +389,7 @@ mmap_write_method(mmap_object *self, PyObject *args) { Py_buffer data; + PyObject *result; CHECK_VALID(NULL); if (!PyArg_ParseTuple(args, "y*:write", &data)) @@ -406,9 +407,9 @@ mmap_write_method(mmap_object *self, } memcpy(self->data + self->pos, data.buf, data.len); self->pos = self->pos + data.len; + result = PyLong_FromSsize_t(data.len); PyBuffer_Release(&data); - Py_INCREF(Py_None); - return Py_None; + return result; } static PyObject *