Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Merged
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
4 changes: 4 additions & 0 deletions 4 Doc/library/mmap.rst
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Suggested change
For anonymous mapping, return its size.
For an anonymous mapping, return its size.


.. versionchanged:: next
Supports anonymous mapping on Unix.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Suggested change
Supports anonymous mapping on Unix.
Anonymous mappings are now supported on Unix.


Comment thread
arhadthedev marked this conversation as resolved.

.. method:: tell()
Expand Down
7 changes: 3 additions & 4 deletions 7 Lib/test/test_mmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -282,9 +281,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):
Expand All @@ -309,7 +307,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)
Expand Down Expand Up @@ -505,6 +503,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)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
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``.
Comment on lines +4 to +5

@ZackerySpytz ZackerySpytz Sep 2, 2025

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Suggested change
would be raised on Unix.
Raise :exc:`ValueError` instead of :exc:`OSError` with ``trackfd=False``.
would be raised on Unix. :exc:`ValueError` is now raised instead of
:exc:`OSError` when ``trackfd=False``.

10 changes: 9 additions & 1 deletion 10 Modules/mmapmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -740,7 +740,7 @@ mmap_size_method(PyObject *op, PyObject *Py_UNUSED(ignored))
#endif /* MS_WINDOWS */

#ifdef UNIX
{
if (self->fd != -1) {
struct _Py_stat_struct status;
if (_Py_fstat(self->fd, &status) == -1)
return NULL;
Expand All @@ -750,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 */
}

Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.