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:: next
+ Supports anonymous mapping on Unix.
.. method:: tell()
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
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):
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)
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)
--- /dev/null
+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``.
#endif /* MS_WINDOWS */
#ifdef UNIX
- {
+ if (self->fd != -1) {
struct _Py_stat_struct status;
if (_Py_fstat(self->fd, &status) == -1)
return NULL;
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 */
}