[Python-checkins] cpython (2.7): Issue #15402: Simplify Struct.__sizeof__ and make tests more precise.

meador.inge python-checkins at python.org
Sun Jul 29 05:36:16 CEST 2012


http://hg.python.org/cpython/rev/37554bda2014
changeset:   78311:37554bda2014
branch:      2.7
parent:      78296:4e85e4743757
user:        Meador Inge <meadori at gmail.com>
date:        Sat Jul 28 21:58:44 2012 -0500
summary:
  Issue #15402: Simplify Struct.__sizeof__ and make tests more precise.

files:
  Lib/test/test_struct.py |  52 +++++++++++++++++++++++-----
  Modules/_struct.c       |   8 +---
  2 files changed, 44 insertions(+), 16 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
@@ -3,7 +3,8 @@
 import unittest
 import struct
 import inspect
-from test.test_support import run_unittest, check_warnings, check_py3k_warnings
+from test.test_support import (run_unittest, check_warnings,
+                               check_py3k_warnings, cpython_only)
 
 import sys
 ISBIGENDIAN = sys.byteorder == "big"
@@ -32,6 +33,33 @@
 
 class StructTest(unittest.TestCase):
 
+    def setUp(self):
+        # due to missing size_t information from struct, it is assumed that
+        # sizeof(Py_ssize_t) = sizeof(void*)
+        self.header = 'PP'
+        if hasattr(sys, "gettotalrefcount"):
+            self.header += '2P'
+
+    def check_sizeof(self, format_str, number_of_codes):
+        def size(fmt):
+            """Wrapper around struct.calcsize which enforces the alignment
+            of the end of a structure to the alignment requirement of pointer.
+
+            Note: This wrapper should only be used if a pointer member is
+            included and no member with a size larger than a pointer exists.
+            """
+            return struct.calcsize(fmt + '0P')
+
+        struct_obj = struct.Struct(format_str)
+        # The size of 'PyStructObject'
+        totalsize = size(self.header + '5P')
+        # The size taken up by the 'formatcode' dynamic array
+        totalsize += size('3P') * (number_of_codes + 1)
+        result = sys.getsizeof(struct_obj)
+        msg = 'wrong size for %s: got %d, expected %d' \
+              % (type(struct_obj), result, totalsize)
+        self.assertEqual(result, totalsize, msg)
+
     def check_float_coerce(self, format, number):
         # SF bug 1530559. struct.pack raises TypeError where it used
         # to convert.
@@ -544,15 +572,19 @@
         hugecount2 = '{}b{}H'.format(sys.maxsize//2, sys.maxsize//2)
         self.assertRaises(struct.error, struct.calcsize, hugecount2)
 
-    def test_sizeof(self):
-        self.assertGreater(sys.getsizeof(struct.Struct('BHILfdspP')),
-                           sys.getsizeof(struct.Struct('B')))
-        self.assertGreater(sys.getsizeof(struct.Struct('123B')),
-                                sys.getsizeof(struct.Struct('B')))
-        self.assertGreater(sys.getsizeof(struct.Struct('B' * 1234)),
-                                sys.getsizeof(struct.Struct('123B')))
-        self.assertGreater(sys.getsizeof(struct.Struct('1234B')),
-                                sys.getsizeof(struct.Struct('123B')))
+    @cpython_only
+    def test__sizeof__(self):
+        for code in integer_codes:
+            self.check_sizeof(code, 1)
+        self.check_sizeof('BHILfdspP', 9)
+        self.check_sizeof('B' * 1234, 1234)
+        self.check_sizeof('fd', 2)
+        self.check_sizeof('xxxxxxxxxxxxxx', 0)
+        self.check_sizeof('100H', 100)
+        self.check_sizeof('187s', 1)
+        self.check_sizeof('20p', 1)
+        self.check_sizeof('0s', 1)
+        self.check_sizeof('0c', 0)
 
 def test_main():
     run_unittest(StructTest)
diff --git a/Modules/_struct.c b/Modules/_struct.c
--- a/Modules/_struct.c
+++ b/Modules/_struct.c
@@ -1697,15 +1697,11 @@
 "S.__sizeof__() -> size of S in memory, in bytes");
 
 static PyObject *
-s_sizeof(PyStructObject *self)
+s_sizeof(PyStructObject *self, void *unused)
 {
     Py_ssize_t size;
-    formatcode *code;
 
-    size = sizeof(PyStructObject) + sizeof(formatcode);
-    for (code = self->s_codes; code->fmtdef != NULL; code++) {
-        size += sizeof(formatcode);
-    }
+    size = sizeof(PyStructObject) + sizeof(formatcode) * (self->s_len + 1);
     return PyLong_FromSsize_t(size);
 }
 

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


More information about the Python-checkins mailing list