From c90c396d2178ad7c19fa72f7b1475e911ea3b9f5 Mon Sep 17 00:00:00 2001 From: Zackery Spytz Date: Sun, 7 Mar 2021 16:32:09 -0700 Subject: [PATCH 1/6] bpo-43429: mmap.size() now returns the size on Unix for anonymous memory Previously, the size would be returned on Windows and an OSError would be raised on Unix. --- Doc/library/mmap.rst | 5 +++++ Doc/whatsnew/3.10.rst | 6 ++++++ Lib/test/test_mmap.py | 1 + .../next/Library/2021-03-07-16-31-36.bpo-43429.Koa0mf.rst | 4 ++++ Modules/mmapmodule.c | 3 +++ 5 files changed, 19 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2021-03-07-16-31-36.bpo-43429.Koa0mf.rst diff --git a/Doc/library/mmap.rst b/Doc/library/mmap.rst index d9825b47c71333..ae2faf9e727d00 100644 --- a/Doc/library/mmap.rst +++ b/Doc/library/mmap.rst @@ -281,6 +281,11 @@ To map anonymous memory, -1 should be passed as the fileno along with the length Return the length of the file, which can be larger than the size of the memory-mapped area. + .. versionchanged:: 3.10 + For a mapping of anonymous memory, the size is now returned on both + Unix and Windows. Previously, the size would be returned on Windows + and an :exc:`OSError` would be raised on Unix. + .. method:: tell() diff --git a/Doc/whatsnew/3.10.rst b/Doc/whatsnew/3.10.rst index db71f061f14df4..7abe6f94fb5642 100644 --- a/Doc/whatsnew/3.10.rst +++ b/Doc/whatsnew/3.10.rst @@ -1098,6 +1098,12 @@ Changes in the Python API also inherits the current builtins. (Contributed by Victor Stinner in :issue:`42990`.) +* The :meth:`~mmap.mmap.size` method of the :class:`mmap.mmap` class + now returns the size of an anonymous mapping on both Unix and Windows. + Previously, the size would be returned on Windows and an :exc:`OSError` would + be raised on Unix. + (Contributed by Zackery Spytz in :issue:`43429`.) + CPython bytecode changes ======================== diff --git a/Lib/test/test_mmap.py b/Lib/test/test_mmap.py index 8f34c182f82eaf..b39c17bf03bfab 100644 --- a/Lib/test/test_mmap.py +++ b/Lib/test/test_mmap.py @@ -405,6 +405,7 @@ def test_anonymous(self): b = x & 0xff m[x] = b self.assertEqual(m[x], b) + self.assertEqual(m.size(), PAGESIZE) def test_read_all(self): m = mmap.mmap(-1, 16) diff --git a/Misc/NEWS.d/next/Library/2021-03-07-16-31-36.bpo-43429.Koa0mf.rst b/Misc/NEWS.d/next/Library/2021-03-07-16-31-36.bpo-43429.Koa0mf.rst new file mode 100644 index 00000000000000..bb823173c24e53 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2021-03-07-16-31-36.bpo-43429.Koa0mf.rst @@ -0,0 +1,4 @@ +The :meth:`~mmap.mmap.size` method of the :class:`mmap.mmap` class now +returns the size of an anonymous mapping on both Unix and Windows. +Previously, the size would be returned on Windows and an :exc:`OSError` +would be raised on Unix. diff --git a/Modules/mmapmodule.c b/Modules/mmapmodule.c index 1e66962d37b0e0..3a9efd7c2f1267 100644 --- a/Modules/mmapmodule.c +++ b/Modules/mmapmodule.c @@ -458,6 +458,9 @@ mmap_size_method(mmap_object *self, #ifdef UNIX { + if (self->fd == -1) { + return PyLong_FromSsize_t(self->size); + } struct _Py_stat_struct status; if (_Py_fstat(self->fd, &status) == -1) return NULL; From 7e54ba0883f849bcc8c6aac7de70a8d9d1f99564 Mon Sep 17 00:00:00 2001 From: Oleg Iarygin Date: Fri, 12 May 2023 21:16:10 +0400 Subject: [PATCH 2/6] Bump `versionchanged` to 3.12 --- Doc/library/mmap.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/mmap.rst b/Doc/library/mmap.rst index ae2faf9e727d00..d7b8dd5948e13a 100644 --- a/Doc/library/mmap.rst +++ b/Doc/library/mmap.rst @@ -281,7 +281,7 @@ To map anonymous memory, -1 should be passed as the fileno along with the length Return the length of the file, which can be larger than the size of the memory-mapped area. - .. versionchanged:: 3.10 + .. versionchanged:: 3.12 For a mapping of anonymous memory, the size is now returned on both Unix and Windows. Previously, the size would be returned on Windows and an :exc:`OSError` would be raised on Unix. From 73598fb5ffaf1a6ee0c8c29a3f238dacf694ce6a Mon Sep 17 00:00:00 2001 From: Oleg Iarygin Date: Sun, 14 May 2023 16:49:13 +0400 Subject: [PATCH 3/6] Address the review --- Doc/library/mmap.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/mmap.rst b/Doc/library/mmap.rst index 584e509149efa0..8e5ff5904c2194 100644 --- a/Doc/library/mmap.rst +++ b/Doc/library/mmap.rst @@ -291,7 +291,7 @@ To map anonymous memory, -1 should be passed as the fileno along with the length Return the length of the file, which can be larger than the size of the memory-mapped area. - .. versionchanged:: 3.12 + .. versionchanged:: 3.11 For a mapping of anonymous memory, the size is now returned on both Unix and Windows. Previously, the size would be returned on Windows and an :exc:`OSError` would be raised on Unix. From 833ea7d67e1a53c8f1e6a383c2fa1912c5c69a59 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Tue, 2 Sep 2025 19:18:44 +0300 Subject: [PATCH 4/6] Remove excessive documentation. Re-target to 3.15. --- Doc/library/mmap.rst | 7 +++---- Doc/whatsnew/3.12.rst | 9 --------- 2 files changed, 3 insertions(+), 13 deletions(-) diff --git a/Doc/library/mmap.rst b/Doc/library/mmap.rst index e8a6d6b9a0b3db..5d81477443ca31 100644 --- a/Doc/library/mmap.rst +++ b/Doc/library/mmap.rst @@ -312,11 +312,10 @@ To map anonymous memory, -1 should be passed as the fileno along with the length Return the length of the file, which can be larger than the size of the memory-mapped area. + For anonymous mapping, return its size. - .. versionchanged:: 3.11 - For a mapping of anonymous memory, the size is now returned on both - Unix and Windows. Previously, the size would be returned on Windows - and an :exc:`OSError` would be raised on Unix. + .. versionchanged:: next + Supports anonymous mapping on Unix. .. method:: tell() diff --git a/Doc/whatsnew/3.12.rst b/Doc/whatsnew/3.12.rst index a798322f666617..7cfdc287b7fad7 100644 --- a/Doc/whatsnew/3.12.rst +++ b/Doc/whatsnew/3.12.rst @@ -753,15 +753,6 @@ math for moving up or down multiple steps at a time. (Contributed by Matthias Goergens, Mark Dickinson, and Raymond Hettinger in :gh:`94906`.) -mmap ----- - -* The :meth:`~mmap.mmap.size` method of the :class:`mmap.mmap` class - now returns the size of an anonymous mapping on both Unix and Windows. - Previously, the size would be returned on Windows and an :exc:`OSError` would - be raised on Unix. - (Contributed by Zackery Spytz in :issue:`43429`.) - os -- From 8b4e2efe23176498d00ac579e97feaa10c2c3cdb Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Tue, 2 Sep 2025 19:32:26 +0300 Subject: [PATCH 5/6] Fix for trackfd=False. Raise ValueError instead of OSError. --- Lib/test/test_mmap.py | 5 ++--- .../2021-03-07-16-31-36.bpo-43429.Koa0mf.rst | 1 + Modules/mmapmodule.c | 13 +++++++++---- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/Lib/test/test_mmap.py b/Lib/test/test_mmap.py index e3a3a3f72975d5..1f0d59546f8f1a 100644 --- a/Lib/test/test_mmap.py +++ b/Lib/test/test_mmap.py @@ -282,9 +282,8 @@ def test_trackfd_parameter(self): if close_original_fd: f.close() self.assertEqual(len(m), size) - with self.assertRaises(OSError) as err_cm: + with self.assertRaises(ValueError): m.size() - self.assertEqual(err_cm.exception.errno, errno.EBADF) with self.assertRaises(ValueError): m.resize(size * 2) with self.assertRaises(ValueError): @@ -309,7 +308,7 @@ def test_trackfd_parameter(self): def test_trackfd_neg1(self): size = 64 with mmap.mmap(-1, size, trackfd=False) as m: - with self.assertRaises(OSError): + with self.assertRaises(ValueError): m.size() with self.assertRaises(ValueError): m.resize(size // 2) diff --git a/Misc/NEWS.d/next/Library/2021-03-07-16-31-36.bpo-43429.Koa0mf.rst b/Misc/NEWS.d/next/Library/2021-03-07-16-31-36.bpo-43429.Koa0mf.rst index bb823173c24e53..a6221ba9c88585 100644 --- a/Misc/NEWS.d/next/Library/2021-03-07-16-31-36.bpo-43429.Koa0mf.rst +++ b/Misc/NEWS.d/next/Library/2021-03-07-16-31-36.bpo-43429.Koa0mf.rst @@ -2,3 +2,4 @@ The :meth:`~mmap.mmap.size` method of the :class:`mmap.mmap` class now returns the size of an anonymous mapping on both Unix and Windows. Previously, the size would be returned on Windows and an :exc:`OSError` would be raised on Unix. +Raise :exc:`ValueError` instead of :exc:`OSError` with ``trackfd=False``. diff --git a/Modules/mmapmodule.c b/Modules/mmapmodule.c index af6324f805e619..1c300546c33fe8 100644 --- a/Modules/mmapmodule.c +++ b/Modules/mmapmodule.c @@ -740,10 +740,7 @@ mmap_size_method(PyObject *op, PyObject *Py_UNUSED(ignored)) #endif /* MS_WINDOWS */ #ifdef UNIX - { - if (self->fd == -1) { - return PyLong_FromSsize_t(self->size); - } + if (self->fd != -1) { struct _Py_stat_struct status; if (_Py_fstat(self->fd, &status) == -1) return NULL; @@ -753,6 +750,14 @@ mmap_size_method(PyObject *op, PyObject *Py_UNUSED(ignored)) return PyLong_FromLong(status.st_size); #endif } + else if (self->trackfd) { + return PyLong_FromSsize_t(self->size); + } + else { + PyErr_SetString(PyExc_ValueError, + "can't get size with trackfd=False"); + return NULL; + } #endif /* UNIX */ } From e6fa21898d83c5dec4ec2344e527774e991029b2 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Tue, 2 Sep 2025 19:35:58 +0300 Subject: [PATCH 6/6] Remove unused import. --- Lib/test/test_mmap.py | 1 - 1 file changed, 1 deletion(-) diff --git a/Lib/test/test_mmap.py b/Lib/test/test_mmap.py index 1f0d59546f8f1a..32881d36dcae10 100644 --- a/Lib/test/test_mmap.py +++ b/Lib/test/test_mmap.py @@ -6,7 +6,6 @@ from test.support.os_helper import TESTFN, unlink from test.support.script_helper import assert_python_ok import unittest -import errno import os import re import itertools