Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6282e65

Browse files
committed
Issue #26335: Make mmap.write() return the number of bytes written like
other write methods. Patch by Jakub Stasiak.
1 parent d2dc15b commit 6282e65

4 files changed

Lines changed: 21 additions & 4 deletions

File tree

Doc/library/mmap.rst

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,13 +263,18 @@ To map anonymous memory, -1 should be passed as the fileno along with the length
263263
.. method:: write(bytes)
264264

265265
Write the bytes in *bytes* into memory at the current position of the
266-
file pointer; the file position is updated to point after the bytes that
267-
were written. If the mmap was created with :const:`ACCESS_READ`, then
266+
file pointer and return the number of bytes written (never less than
267+
``len(bytes)``, since if the write fails, a :exc:`ValueError` will be
268+
raised). The file position is updated to point after the bytes that
269+
were written. If the mmap was created with :const:`ACCESS_READ`, then
268270
writing to it will raise a :exc:`TypeError` exception.
269271

270272
.. versionchanged:: 3.5
271273
Writable :term:`bytes-like object` is now accepted.
272274

275+
.. versionchanged:: 3.6
276+
The number of bytes written is now returned.
277+
273278

274279
.. method:: write_byte(byte)
275280

Lib/test/test_mmap.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,14 @@ def test_weakref(self):
713713
gc_collect()
714714
self.assertIs(wr(), None)
715715

716+
def test_write_returning_the_number_of_bytes_written(self):
717+
mm = mmap.mmap(-1, 16)
718+
self.assertEqual(mm.write(b""), 0)
719+
self.assertEqual(mm.write(b"x"), 1)
720+
self.assertEqual(mm.write(b"yz"), 2)
721+
self.assertEqual(mm.write(b"python"), 6)
722+
723+
716724
class LargeMmapTests(unittest.TestCase):
717725

718726
def setUp(self):

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,9 @@ Core and Builtins
198198
Library
199199
-------
200200

201+
- Issue #26335: Make mmap.write() return the number of bytes written like
202+
other write methods. Patch by Jakub Stasiak.
203+
201204
- Issue #26457: Fixed the subnets() methods in IP network classes for the case
202205
when resulting prefix length is equal to maximal prefix length.
203206
Based on patch by Xiang Zhang.

Modules/mmapmodule.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,7 @@ mmap_write_method(mmap_object *self,
389389
PyObject *args)
390390
{
391391
Py_buffer data;
392+
PyObject *result;
392393

393394
CHECK_VALID(NULL);
394395
if (!PyArg_ParseTuple(args, "y*:write", &data))
@@ -406,9 +407,9 @@ mmap_write_method(mmap_object *self,
406407
}
407408
memcpy(self->data + self->pos, data.buf, data.len);
408409
self->pos = self->pos + data.len;
410+
result = PyLong_FromSsize_t(data.len);
409411
PyBuffer_Release(&data);
410-
Py_INCREF(Py_None);
411-
return Py_None;
412+
return result;
412413
}
413414

414415
static PyObject *

0 commit comments

Comments
 (0)