[Python-checkins] cpython (merge 3.2 -> default): Issue #15402: Add a __sizeof__ method to struct.Struct.

meador.inge python-checkins at python.org
Mon Jul 23 17:25:49 CEST 2012


http://hg.python.org/cpython/rev/03063e718f5f
changeset:   78257:03063e718f5f
parent:      78254:db0973994844
parent:      78256:3e7b517e1b68
user:        Meador Inge <meadori at gmail.com>
date:        Mon Jul 23 10:22:36 2012 -0500
summary:
  Issue #15402: Add a __sizeof__ method to struct.Struct.

Initial patch by Serhiy Storchaka.

files:
  Lib/test/test_struct.py |  10 ++++++++++
  Misc/NEWS               |   4 ++++
  Modules/_struct.c       |  17 +++++++++++++++++
  3 files changed, 31 insertions(+), 0 deletions(-)


diff --git a/Lib/test/test_struct.py b/Lib/test/test_struct.py
--- a/Lib/test/test_struct.py
+++ b/Lib/test/test_struct.py
@@ -572,6 +572,16 @@
         s = struct.Struct('i')
         s.__init__('ii')
 
+    def test_sizeof(self):
+        self.assertGreater(sys.getsizeof(struct.Struct('BHILfdspP')),
+                           sys.getsizeof(struct.Struct('B')))
+        self.assertGreaterEqual(sys.getsizeof(struct.Struct('123B')),
+                                sys.getsizeof(struct.Struct('B')))
+        self.assertGreaterEqual(sys.getsizeof(struct.Struct('B' * 123)),
+                                sys.getsizeof(struct.Struct('123B')))
+        self.assertGreaterEqual(sys.getsizeof(struct.Struct('123xB')),
+                                sys.getsizeof(struct.Struct('B')))
+
 def test_main():
     run_unittest(StructTest)
 
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -52,6 +52,10 @@
 Library
 -------
 
+- Issue #15402: An issue in the struct module that caused sys.getsizeof to
+  return incorrect results for struct.Struct instances has been fixed.
+  Initial patch by Serhiy Storchaka.
+
 - Issue #15232: when mangle_from is True, email.Generator now correctly mangles
   lines that start with 'From' that occur in a MIME preamble or epilogue.
 
diff --git a/Modules/_struct.c b/Modules/_struct.c
--- a/Modules/_struct.c
+++ b/Modules/_struct.c
@@ -1752,6 +1752,22 @@
     return PyLong_FromSsize_t(self->s_size);
 }
 
+PyDoc_STRVAR(s_sizeof__doc__,
+"S.__sizeof__() -> size of S in memory, in bytes");
+
+static PyObject *
+s_sizeof(PyStructObject *self)
+{
+    Py_ssize_t size;
+    formatcode *code;
+
+    size = sizeof(PyStructObject) + sizeof(formatcode);
+    for (code = self->s_codes; code->fmtdef != NULL; code++) {
+        size += sizeof(formatcode);
+    }
+    return PyLong_FromSsize_t(size);
+}
+
 /* List of functions */
 
 static struct PyMethodDef s_methods[] = {
@@ -1760,6 +1776,7 @@
     {"unpack",          s_unpack,       METH_O, s_unpack__doc__},
     {"unpack_from",     (PyCFunction)s_unpack_from, METH_VARARGS|METH_KEYWORDS,
                     s_unpack_from__doc__},
+    {"__sizeof__",      (PyCFunction)s_sizeof, METH_NOARGS, s_sizeof__doc__},
     {NULL,       NULL}          /* sentinel */
 };
 

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list