From c90c396d2178ad7c19fa72f7b1475e911ea3b9f5 Mon Sep 17 00:00:00 2001 From: Zackery Spytz Date: Sun, 7 Mar 2021 16:32:09 -0700 Subject: [PATCH 1/3] 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/3] 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/3] 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.