Skip to content

Commit bd1ada6

Browse files
authored
gh-138092: Allow calling mmap.flush with offset only (#138093)
1 parent 6b5f156 commit bd1ada6

File tree

4 files changed

+27
-4
lines changed

4 files changed

+27
-4
lines changed

Doc/library/mmap.rst

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,7 @@ To map anonymous memory, -1 should be passed as the fileno along with the length
212212
Writable :term:`bytes-like object` is now accepted.
213213

214214

215-
.. method:: flush()
216-
flush(offset, size, /)
215+
.. method:: flush([offset[, size]])
217216

218217
Flushes changes made to the in-memory copy of a file back to disk. Without
219218
use of this call there is no guarantee that changes are written back before
@@ -230,6 +229,12 @@ To map anonymous memory, -1 should be passed as the fileno along with the length
230229
on error under Windows. A zero value was returned on success; an
231230
exception was raised on error under Unix.
232231

232+
.. versionchanged:: next
233+
Allow specifying *offset* without *size*. Previously, both *offset*
234+
and *size* parameters were required together. Now *offset* can be
235+
specified alone, and the flush operation will extend from *offset*
236+
to the end of the mmap.
237+
233238

234239
.. method:: madvise(option[, start[, length]])
235240

Lib/test/test_mmap.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1145,6 +1145,18 @@ def test_access_violations(self):
11451145
self.assertEqual(stdout.strip(), b'')
11461146
self.assertEqual(stderr.strip(), b'')
11471147

1148+
def test_flush_parameters(self):
1149+
with open(TESTFN, 'wb+') as f:
1150+
f.write(b'x' * PAGESIZE * 3)
1151+
f.flush()
1152+
1153+
m = mmap.mmap(f.fileno(), PAGESIZE * 3)
1154+
self.addCleanup(m.close)
1155+
1156+
m.flush()
1157+
m.flush(PAGESIZE)
1158+
m.flush(PAGESIZE, PAGESIZE)
1159+
11481160

11491161
class LargeMmapTests(unittest.TestCase):
11501162

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fixed a bug in :meth:`mmap.mmap.flush` where calling with only an offset
2+
parameter would fail.

Modules/mmapmodule.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -933,11 +933,15 @@ static PyObject *
933933
mmap_flush_method(PyObject *op, PyObject *args)
934934
{
935935
Py_ssize_t offset = 0;
936+
Py_ssize_t size = -1;
936937
mmap_object *self = mmap_object_CAST(op);
937-
Py_ssize_t size = self->size;
938938
CHECK_VALID(NULL);
939-
if (!PyArg_ParseTuple(args, "|nn:flush", &offset, &size))
939+
if (!PyArg_ParseTuple(args, "|nn:flush", &offset, &size)) {
940940
return NULL;
941+
}
942+
if (size == -1) {
943+
size = self->size - offset;
944+
}
941945
if (size < 0 || offset < 0 || self->size - offset < size) {
942946
PyErr_SetString(PyExc_ValueError, "flush values out of range");
943947
return NULL;

0 commit comments

Comments
 (0)