https://github.com/python/cpython/commit/bd1ada6df35f29069fc478ce3b0fee3372df075a
commit: bd1ada6df35f29069fc478ce3b0fee3372df075a
branch: main
author: AN Long <[email protected]>
committer: encukou <[email protected]>
date: 2025-09-30T11:08:50+02:00
summary:

gh-138092: Allow calling mmap.flush with offset only (#138093)

files:
A Misc/NEWS.d/next/Library/2025-08-24-02-04-32.gh-issue-138092.V4-wTO.rst
M Doc/library/mmap.rst
M Lib/test/test_mmap.py
M Modules/mmapmodule.c

diff --git a/Doc/library/mmap.rst b/Doc/library/mmap.rst
index bd3f7229bdaf70..b6ffb5cebc020e 100644
--- a/Doc/library/mmap.rst
+++ b/Doc/library/mmap.rst
@@ -212,8 +212,7 @@ To map anonymous memory, -1 should be passed as the fileno 
along with the length
          Writable :term:`bytes-like object` is now accepted.
 
 
-   .. method:: flush()
-               flush(offset, size, /)
+   .. method:: flush([offset[, size]])
 
       Flushes changes made to the in-memory copy of a file back to disk. 
Without
       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
          on error under Windows.  A zero value was returned on success; an
          exception was raised on error under Unix.
 
+      .. versionchanged:: next
+         Allow specifying *offset* without *size*. Previously, both *offset*
+         and *size* parameters were required together. Now *offset* can be
+         specified alone, and the flush operation will extend from *offset*
+         to the end of the mmap.
+
 
    .. method:: madvise(option[, start[, length]])
 
diff --git a/Lib/test/test_mmap.py b/Lib/test/test_mmap.py
index 75ea1a671b67de..0571eed23f72dc 100644
--- a/Lib/test/test_mmap.py
+++ b/Lib/test/test_mmap.py
@@ -1145,6 +1145,18 @@ def test_access_violations(self):
         self.assertEqual(stdout.strip(), b'')
         self.assertEqual(stderr.strip(), b'')
 
+    def test_flush_parameters(self):
+        with open(TESTFN, 'wb+') as f:
+            f.write(b'x' * PAGESIZE * 3)
+            f.flush()
+
+            m = mmap.mmap(f.fileno(), PAGESIZE * 3)
+            self.addCleanup(m.close)
+
+            m.flush()
+            m.flush(PAGESIZE)
+            m.flush(PAGESIZE, PAGESIZE)
+
 
 class LargeMmapTests(unittest.TestCase):
 
diff --git 
a/Misc/NEWS.d/next/Library/2025-08-24-02-04-32.gh-issue-138092.V4-wTO.rst 
b/Misc/NEWS.d/next/Library/2025-08-24-02-04-32.gh-issue-138092.V4-wTO.rst
new file mode 100644
index 00000000000000..21c28c49628cba
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2025-08-24-02-04-32.gh-issue-138092.V4-wTO.rst
@@ -0,0 +1,2 @@
+Fixed a bug in :meth:`mmap.mmap.flush` where calling with only an offset
+parameter would fail.
diff --git a/Modules/mmapmodule.c b/Modules/mmapmodule.c
index 8caadde8ae211b..41d117162882d7 100644
--- a/Modules/mmapmodule.c
+++ b/Modules/mmapmodule.c
@@ -933,11 +933,15 @@ static PyObject *
 mmap_flush_method(PyObject *op, PyObject *args)
 {
     Py_ssize_t offset = 0;
+    Py_ssize_t size = -1;
     mmap_object *self = mmap_object_CAST(op);
-    Py_ssize_t size = self->size;
     CHECK_VALID(NULL);
-    if (!PyArg_ParseTuple(args, "|nn:flush", &offset, &size))
+    if (!PyArg_ParseTuple(args, "|nn:flush", &offset, &size)) {
         return NULL;
+    }
+    if (size == -1) {
+        size = self->size - offset;
+    }
     if (size < 0 || offset < 0 || self->size - offset < size) {
         PyErr_SetString(PyExc_ValueError, "flush values out of range");
         return NULL;

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to