Skip to content

Commit 32032ee

Browse files
gh-87595: Support mmap.size() for anonymous mapping on Unix (GH-24781)
Previously, the size would be returned on Windows and an OSError would be raised on Unix. Also, raise ValueError instead of OSError for trackfd=False. Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
1 parent e4e2390 commit 32032ee

File tree

4 files changed

+21
-5
lines changed

4 files changed

+21
-5
lines changed

Doc/library/mmap.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,10 @@ To map anonymous memory, -1 should be passed as the fileno along with the length
312312

313313
Return the length of the file, which can be larger than the size of the
314314
memory-mapped area.
315+
For anonymous mapping, return its size.
316+
317+
.. versionchanged:: next
318+
Supports anonymous mapping on Unix.
315319

316320

317321
.. method:: tell()

Lib/test/test_mmap.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from test.support.os_helper import TESTFN, unlink
77
from test.support.script_helper import assert_python_ok
88
import unittest
9-
import errno
109
import os
1110
import re
1211
import itertools
@@ -282,9 +281,8 @@ def test_trackfd_parameter(self):
282281
if close_original_fd:
283282
f.close()
284283
self.assertEqual(len(m), size)
285-
with self.assertRaises(OSError) as err_cm:
284+
with self.assertRaises(ValueError):
286285
m.size()
287-
self.assertEqual(err_cm.exception.errno, errno.EBADF)
288286
with self.assertRaises(ValueError):
289287
m.resize(size * 2)
290288
with self.assertRaises(ValueError):
@@ -309,7 +307,7 @@ def test_trackfd_parameter(self):
309307
def test_trackfd_neg1(self):
310308
size = 64
311309
with mmap.mmap(-1, size, trackfd=False) as m:
312-
with self.assertRaises(OSError):
310+
with self.assertRaises(ValueError):
313311
m.size()
314312
with self.assertRaises(ValueError):
315313
m.resize(size // 2)
@@ -505,6 +503,7 @@ def test_anonymous(self):
505503
b = x & 0xff
506504
m[x] = b
507505
self.assertEqual(m[x], b)
506+
self.assertEqual(m.size(), PAGESIZE)
508507

509508
def test_read_all(self):
510509
m = mmap.mmap(-1, 16)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
The :meth:`~mmap.mmap.size` method of the :class:`mmap.mmap` class now
2+
returns the size of an anonymous mapping on both Unix and Windows.
3+
Previously, the size would be returned on Windows and an :exc:`OSError`
4+
would be raised on Unix.
5+
Raise :exc:`ValueError` instead of :exc:`OSError` with ``trackfd=False``.

Modules/mmapmodule.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -740,7 +740,7 @@ mmap_size_method(PyObject *op, PyObject *Py_UNUSED(ignored))
740740
#endif /* MS_WINDOWS */
741741

742742
#ifdef UNIX
743-
{
743+
if (self->fd != -1) {
744744
struct _Py_stat_struct status;
745745
if (_Py_fstat(self->fd, &status) == -1)
746746
return NULL;
@@ -750,6 +750,14 @@ mmap_size_method(PyObject *op, PyObject *Py_UNUSED(ignored))
750750
return PyLong_FromLong(status.st_size);
751751
#endif
752752
}
753+
else if (self->trackfd) {
754+
return PyLong_FromSsize_t(self->size);
755+
}
756+
else {
757+
PyErr_SetString(PyExc_ValueError,
758+
"can't get size with trackfd=False");
759+
return NULL;
760+
}
753761
#endif /* UNIX */
754762
}
755763

0 commit comments

Comments
 (0)