[Python-checkins] cpython: Issue #26335: Make mmap.write() return the number of bytes written like

berker.peksag python-checkins at python.org
Wed Mar 2 12:29:49 EST 2016


https://hg.python.org/cpython/rev/ba71aecec943
changeset:   100402:ba71aecec943
user:        Berker Peksag <berker.peksag at gmail.com>
date:        Wed Mar 02 19:30:18 2016 +0200
summary:
  Issue #26335: Make mmap.write() return the number of bytes written like
other write methods.

Patch by Jakub Stasiak.

files:
  Doc/library/mmap.rst  |  9 +++++++--
  Lib/test/test_mmap.py |  8 ++++++++
  Misc/NEWS             |  3 +++
  Modules/mmapmodule.c  |  5 +++--
  4 files changed, 21 insertions(+), 4 deletions(-)


diff --git a/Doc/library/mmap.rst b/Doc/library/mmap.rst
--- a/Doc/library/mmap.rst
+++ b/Doc/library/mmap.rst
@@ -263,13 +263,18 @@
    .. 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
-      were written. If the mmap was created with :const:`ACCESS_READ`, then
+      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.
+
 
    .. method:: write_byte(byte)
 
diff --git a/Lib/test/test_mmap.py b/Lib/test/test_mmap.py
--- a/Lib/test/test_mmap.py
+++ b/Lib/test/test_mmap.py
@@ -713,6 +713,14 @@
         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""), 0)
+        self.assertEqual(mm.write(b"x"), 1)
+        self.assertEqual(mm.write(b"yz"), 2)
+        self.assertEqual(mm.write(b"python"), 6)
+
+
 class LargeMmapTests(unittest.TestCase):
 
     def setUp(self):
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -198,6 +198,9 @@
 Library
 -------
 
+- Issue #26335: Make mmap.write() return the number of bytes written like
+  other write methods.  Patch by Jakub Stasiak.
+
 - Issue #26457: Fixed the subnets() methods in IP network classes for the case
   when resulting prefix length is equal to maximal prefix length.
   Based on patch by Xiang Zhang.
diff --git a/Modules/mmapmodule.c b/Modules/mmapmodule.c
--- a/Modules/mmapmodule.c
+++ b/Modules/mmapmodule.c
@@ -389,6 +389,7 @@
                   PyObject *args)
 {
     Py_buffer data;
+    PyObject *result;
 
     CHECK_VALID(NULL);
     if (!PyArg_ParseTuple(args, "y*:write", &data))
@@ -406,9 +407,9 @@
     }
     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 *

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list