From d82537c4778900d533448ab69556fab13b3de054 Mon Sep 17 00:00:00 2001 From: AN Long Date: Thu, 28 Aug 2025 01:26:33 +0900 Subject: [PATCH 1/2] Fix size method on anonymous mmap object --- Lib/test/test_mmap.py | 2 ++ Modules/mmapmodule.c | 3 +++ 2 files changed, 5 insertions(+) diff --git a/Lib/test/test_mmap.py b/Lib/test/test_mmap.py index b2a299ed172967..dff8d056aa375e 100644 --- a/Lib/test/test_mmap.py +++ b/Lib/test/test_mmap.py @@ -506,6 +506,8 @@ def test_anonymous(self): m[x] = b self.assertEqual(m[x], b) + self.assertEqual(m.size(), PAGESIZE) + def test_read_all(self): m = mmap.mmap(-1, 16) self.addCleanup(m.close) diff --git a/Modules/mmapmodule.c b/Modules/mmapmodule.c index 0cb4b62d734550..0ef91861d7d6e0 100644 --- a/Modules/mmapmodule.c +++ b/Modules/mmapmodule.c @@ -747,6 +747,9 @@ mmap_size_method(PyObject *op, PyObject *Py_UNUSED(ignored)) #ifdef UNIX { + if (self->trackfd && self->fd == -1) { + return PyLong_FromSsize_t(self->size); + } struct _Py_stat_struct status; if (_Py_fstat(self->fd, &status) == -1) return NULL; From 4eb2eac0527f91fb6266f33afb21dcff78da4897 Mon Sep 17 00:00:00 2001 From: AN Long Date: Thu, 28 Aug 2025 01:29:07 +0900 Subject: [PATCH 2/2] Add news entry --- .../next/Library/2025-08-28-01-29-01.gh-issue-87595.Ncgrwo.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2025-08-28-01-29-01.gh-issue-87595.Ncgrwo.rst diff --git a/Misc/NEWS.d/next/Library/2025-08-28-01-29-01.gh-issue-87595.Ncgrwo.rst b/Misc/NEWS.d/next/Library/2025-08-28-01-29-01.gh-issue-87595.Ncgrwo.rst new file mode 100644 index 00000000000000..d978133992068c --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-08-28-01-29-01.gh-issue-87595.Ncgrwo.rst @@ -0,0 +1 @@ +Fix :meth:`mmap.mmap.size` method for anonymous mmap files on Unix.