mirror of
https://github.com/python/cpython.git
synced 2025-11-26 21:33:10 +00:00
Issue #26335: Make mmap.write() return the number of bytes written like
other write methods. Patch by Jakub Stasiak.
This commit is contained in:
parent
d2dc15b26b
commit
6282e656e9
4 changed files with 21 additions and 4 deletions
|
|
@ -263,13 +263,18 @@ To map anonymous memory, -1 should be passed as the fileno along with the length
|
||||||
.. method:: write(bytes)
|
.. method:: write(bytes)
|
||||||
|
|
||||||
Write the bytes in *bytes* into memory at the current position of the
|
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
|
||||||
were written. If the mmap was created with :const:`ACCESS_READ`, then
|
``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.
|
writing to it will raise a :exc:`TypeError` exception.
|
||||||
|
|
||||||
.. versionchanged:: 3.5
|
.. versionchanged:: 3.5
|
||||||
Writable :term:`bytes-like object` is now accepted.
|
Writable :term:`bytes-like object` is now accepted.
|
||||||
|
|
||||||
|
.. versionchanged:: 3.6
|
||||||
|
The number of bytes written is now returned.
|
||||||
|
|
||||||
|
|
||||||
.. method:: write_byte(byte)
|
.. method:: write_byte(byte)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -713,6 +713,14 @@ class MmapTests(unittest.TestCase):
|
||||||
gc_collect()
|
gc_collect()
|
||||||
self.assertIs(wr(), None)
|
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):
|
class LargeMmapTests(unittest.TestCase):
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
|
|
|
||||||
|
|
@ -198,6 +198,9 @@ Core and Builtins
|
||||||
Library
|
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
|
- Issue #26457: Fixed the subnets() methods in IP network classes for the case
|
||||||
when resulting prefix length is equal to maximal prefix length.
|
when resulting prefix length is equal to maximal prefix length.
|
||||||
Based on patch by Xiang Zhang.
|
Based on patch by Xiang Zhang.
|
||||||
|
|
|
||||||
|
|
@ -389,6 +389,7 @@ mmap_write_method(mmap_object *self,
|
||||||
PyObject *args)
|
PyObject *args)
|
||||||
{
|
{
|
||||||
Py_buffer data;
|
Py_buffer data;
|
||||||
|
PyObject *result;
|
||||||
|
|
||||||
CHECK_VALID(NULL);
|
CHECK_VALID(NULL);
|
||||||
if (!PyArg_ParseTuple(args, "y*:write", &data))
|
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);
|
memcpy(self->data + self->pos, data.buf, data.len);
|
||||||
self->pos = self->pos + data.len;
|
self->pos = self->pos + data.len;
|
||||||
|
result = PyLong_FromSsize_t(data.len);
|
||||||
PyBuffer_Release(&data);
|
PyBuffer_Release(&data);
|
||||||
Py_INCREF(Py_None);
|
return result;
|
||||||
return Py_None;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static PyObject *
|
static PyObject *
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue