Skip to content

gh-138092: Fix mmap.flush argument handling #138093

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Lib/test/test_mmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -1136,6 +1136,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()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

flush can raise on some platforms, let's skip this test if this happens.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, I see other test call flush directly:

m.flush()
, so I think there are nothing special to do here?

m.flush(PAGESIZE)
m.flush(PAGESIZE, PAGESIZE)


class LargeMmapTests(unittest.TestCase):

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fixed a bug in :meth:`mmap.mmap.flush` where calling with only an offset
parameter would fail.
70 changes: 70 additions & 0 deletions Modules/clinic/mmapmodule.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 31 additions & 7 deletions Modules/mmapmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@
#include "pycore_fileutils.h" // _Py_stat_struct
#include "pycore_weakref.h" // FT_CLEAR_WEAKREFS()

/*[clinic input]
module mmap
class mmap.mmap "mmap_object *" "mmap_object_type"
[clinic start generated code]*/
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=4ebde54549b9daa7]*/

#include <stddef.h> // offsetof()
#ifndef MS_WINDOWS
# include <unistd.h> // close()
Expand Down Expand Up @@ -128,6 +134,8 @@ typedef struct {

#define mmap_object_CAST(op) ((mmap_object *)(op))

#include "clinic/mmapmodule.c.h"

static int
mmap_object_traverse(PyObject *op, visitproc visit, void *arg)
{
Expand Down Expand Up @@ -924,15 +932,31 @@ mmap_tell_method(PyObject *op, PyObject *Py_UNUSED(ignored))
return PyLong_FromSize_t(self->pos);
}

/*[clinic input]
mmap.mmap.flush

offset: Py_ssize_t = 0
size: Py_ssize_t(c_default="-1") = None
/

Flushes changes made to the in-memory copy of a file back to disk.

If offset and size are specified, only the specified range will
be flushed. If not specified, the entire mapped region will be
flushed.
[clinic start generated code]*/

static PyObject *
mmap_flush_method(PyObject *op, PyObject *args)
mmap_mmap_flush_impl(mmap_object *self, Py_ssize_t offset, Py_ssize_t size)
/*[clinic end generated code: output=956ced67466149cf input=07c2c6d4e69263a4]*/
{
Py_ssize_t offset = 0;
mmap_object *self = mmap_object_CAST(op);
Py_ssize_t size = self->size;
CHECK_VALID(NULL);
if (!PyArg_ParseTuple(args, "|nn:flush", &offset, &size))
return NULL;

// If size is -1 (default), calculate size from offset to end.
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;
Expand Down Expand Up @@ -1194,7 +1218,7 @@ static struct PyMethodDef mmap_object_methods[] = {
{"close", mmap_close_method, METH_NOARGS},
{"find", mmap_find_method, METH_VARARGS},
{"rfind", mmap_rfind_method, METH_VARARGS},
{"flush", mmap_flush_method, METH_VARARGS},
MMAP_MMAP_FLUSH_METHODDEF
#ifdef HAVE_MADVISE
{"madvise", mmap_madvise_method, METH_VARARGS},
#endif
Expand Down
Loading