Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 76b4765

Browse files
Issue #15696: Add a __sizeof__ implementation for mmap objects on Windows.
1 parent d6ec309 commit 76b4765

3 files changed

Lines changed: 28 additions & 1 deletion

File tree

Lib/test/test_mmap.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from test.support import (TESTFN, run_unittest, import_module, unlink,
2-
requires, _2G, _4G, gc_collect)
2+
requires, _2G, _4G, gc_collect, cpython_only)
33
import unittest
44
import os
55
import re
@@ -639,6 +639,15 @@ def test_tagname(self):
639639
m2.close()
640640
m1.close()
641641

642+
@cpython_only
643+
@unittest.skipUnless(os.name == 'nt', 'requires Windows')
644+
def test_sizeof(self):
645+
m1 = mmap.mmap(-1, 100)
646+
tagname = "foo"
647+
m2 = mmap.mmap(-1, 100, tagname=tagname)
648+
self.assertEqual(sys.getsize(m2),
649+
sys.getsize(m1) + len(tagname) + 1)
650+
642651
@unittest.skipUnless(os.name == 'nt', 'requires Windows')
643652
def test_crasher_on_windows(self):
644653
# Should not crash (Issue 1733986)

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ Core and Builtins
2727
Library
2828
-------
2929

30+
- Issue #15696: Add a __sizeof__ implementation for mmap objects on Windows.
31+
3032
- Issue #22068: Avoided reference loops with Variables and Fonts in Tkinter.
3133

3234
- Issue #22165: SimpleHTTPRequestHandler now supports undecodable file names.

Modules/mmapmodule.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,19 @@ mmap__exit__method(PyObject *self, PyObject *args)
709709
return _PyObject_CallMethodId(self, &PyId_close, NULL);
710710
}
711711

712+
#ifdef MS_WINDOWS
713+
static PyObject *
714+
mmap__sizeof__method(mmap_object *self, void *unused)
715+
{
716+
Py_ssize_t res;
717+
718+
res = sizeof(mmap_object);
719+
if (self->tagname)
720+
res += strlen(self->tagname) + 1;
721+
return PyLong_FromSsize_t(res);
722+
}
723+
#endif
724+
712725
static struct PyMethodDef mmap_object_methods[] = {
713726
{"close", (PyCFunction) mmap_close_method, METH_NOARGS},
714727
{"find", (PyCFunction) mmap_find_method, METH_VARARGS},
@@ -726,6 +739,9 @@ static struct PyMethodDef mmap_object_methods[] = {
726739
{"write_byte", (PyCFunction) mmap_write_byte_method, METH_VARARGS},
727740
{"__enter__", (PyCFunction) mmap__enter__method, METH_NOARGS},
728741
{"__exit__", (PyCFunction) mmap__exit__method, METH_VARARGS},
742+
#ifdef MS_WINDOWS
743+
{"__sizeof__", (PyCFunction) mmap__sizeof__method, METH_NOARGS},
744+
#endif
729745
{NULL, NULL} /* sentinel */
730746
};
731747

0 commit comments

Comments
 (0)