]> granicus.if.org Git - python/commitdiff
Issue #15696: Add a __sizeof__ implementation for mmap objects on Windows.
authorSerhiy Storchaka <storchaka@gmail.com>
Tue, 19 Aug 2014 14:11:20 +0000 (17:11 +0300)
committerSerhiy Storchaka <storchaka@gmail.com>
Tue, 19 Aug 2014 14:11:20 +0000 (17:11 +0300)
Lib/test/test_mmap.py
Misc/NEWS
Modules/mmapmodule.c

index 6ca5e1b73096741ed48c3a2d567568b2d2bcbab2..fa693f3ffa9088e95998b34fc2c6ea89909aaaa9 100644 (file)
@@ -1,5 +1,5 @@
 from test.support import (TESTFN, run_unittest, import_module, unlink,
-                          requires, _2G, _4G, gc_collect)
+                          requires, _2G, _4G, gc_collect, cpython_only)
 import unittest
 import os
 import re
@@ -639,6 +639,15 @@ class MmapTests(unittest.TestCase):
         m2.close()
         m1.close()
 
+    @cpython_only
+    @unittest.skipUnless(os.name == 'nt', 'requires Windows')
+    def test_sizeof(self):
+        m1 = mmap.mmap(-1, 100)
+        tagname = "foo"
+        m2 = mmap.mmap(-1, 100, tagname=tagname)
+        self.assertEqual(sys.getsize(m2),
+                         sys.getsize(m1) + len(tagname) + 1)
+
     @unittest.skipUnless(os.name == 'nt', 'requires Windows')
     def test_crasher_on_windows(self):
         # Should not crash (Issue 1733986)
index 58d23dda1777ec319f7b6dc38b11e065de28f9c2..397ca5da5bd4ff6a6078587fb5781543ec8b4125 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -27,6 +27,8 @@ Core and Builtins
 Library
 -------
 
+- Issue #15696: Add a __sizeof__ implementation for mmap objects on Windows.
+
 - Issue #22068: Avoided reference loops with Variables and Fonts in Tkinter.
 
 - Issue #22165: SimpleHTTPRequestHandler now supports undecodable file names.
index 07b5c6bcd1df4b2757bdc94384740d3b79ce68e0..137142420f4febb430cdced19554cabeba7cd45d 100644 (file)
@@ -709,6 +709,19 @@ mmap__exit__method(PyObject *self, PyObject *args)
     return _PyObject_CallMethodId(self, &PyId_close, NULL);
 }
 
+#ifdef MS_WINDOWS
+static PyObject *
+mmap__sizeof__method(mmap_object *self, void *unused)
+{
+    Py_ssize_t res;
+
+    res = sizeof(mmap_object);
+    if (self->tagname)
+        res += strlen(self->tagname) + 1;
+    return PyLong_FromSsize_t(res);
+}
+#endif
+
 static struct PyMethodDef mmap_object_methods[] = {
     {"close",           (PyCFunction) mmap_close_method,        METH_NOARGS},
     {"find",            (PyCFunction) mmap_find_method,         METH_VARARGS},
@@ -726,6 +739,9 @@ static struct PyMethodDef mmap_object_methods[] = {
     {"write_byte",      (PyCFunction) mmap_write_byte_method,   METH_VARARGS},
     {"__enter__",       (PyCFunction) mmap__enter__method,      METH_NOARGS},
     {"__exit__",        (PyCFunction) mmap__exit__method,       METH_VARARGS},
+#ifdef MS_WINDOWS
+    {"__sizeof__",      (PyCFunction) mmap__sizeof__method,     METH_NOARGS},
+#endif
     {NULL,         NULL}       /* sentinel */
 };