Skip to content

Commit 8b4e2ef

Browse files
Fix for trackfd=False. Raise ValueError instead of OSError.
1 parent 833ea7d commit 8b4e2ef

File tree

3 files changed

+12
-7
lines changed

3 files changed

+12
-7
lines changed

Lib/test/test_mmap.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -282,9 +282,8 @@ def test_trackfd_parameter(self):
282282
if close_original_fd:
283283
f.close()
284284
self.assertEqual(len(m), size)
285-
with self.assertRaises(OSError) as err_cm:
285+
with self.assertRaises(ValueError):
286286
m.size()
287-
self.assertEqual(err_cm.exception.errno, errno.EBADF)
288287
with self.assertRaises(ValueError):
289288
m.resize(size * 2)
290289
with self.assertRaises(ValueError):
@@ -309,7 +308,7 @@ def test_trackfd_parameter(self):
309308
def test_trackfd_neg1(self):
310309
size = 64
311310
with mmap.mmap(-1, size, trackfd=False) as m:
312-
with self.assertRaises(OSError):
311+
with self.assertRaises(ValueError):
313312
m.size()
314313
with self.assertRaises(ValueError):
315314
m.resize(size // 2)

Misc/NEWS.d/next/Library/2021-03-07-16-31-36.bpo-43429.Koa0mf.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ The :meth:`~mmap.mmap.size` method of the :class:`mmap.mmap` class now
22
returns the size of an anonymous mapping on both Unix and Windows.
33
Previously, the size would be returned on Windows and an :exc:`OSError`
44
would be raised on Unix.
5+
Raise :exc:`ValueError` instead of :exc:`OSError` with ``trackfd=False``.

Modules/mmapmodule.c

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

742742
#ifdef UNIX
743-
{
744-
if (self->fd == -1) {
745-
return PyLong_FromSsize_t(self->size);
746-
}
743+
if (self->fd != -1) {
747744
struct _Py_stat_struct status;
748745
if (_Py_fstat(self->fd, &status) == -1)
749746
return NULL;
@@ -753,6 +750,14 @@ mmap_size_method(PyObject *op, PyObject *Py_UNUSED(ignored))
753750
return PyLong_FromLong(status.st_size);
754751
#endif
755752
}
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+
}
756761
#endif /* UNIX */
757762
}
758763

0 commit comments

Comments
 (0)