summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_struct.py
diff options
context:
space:
mode:
authorMeador Inge <meadori@gmail.com>2012-07-29 03:32:50 (GMT)
committerMeador Inge <meadori@gmail.com>2012-07-29 03:32:50 (GMT)
commitbb9b1c165d0193e6783981c1ac1e0ab9bf171c4c (patch)
tree8fb3f90d5b8ba914e18c4bb1cde3c1c36199b7c4 /Lib/test/test_struct.py
parentdbf616d9c3311fd03479c7c336563a79cd04b2eb (diff)
parent90bc2dbccef5d74f0dd6875c555ee33187cfec37 (diff)
downloadcpython-bb9b1c165d0193e6783981c1ac1e0ab9bf171c4c.zip
cpython-bb9b1c165d0193e6783981c1ac1e0ab9bf171c4c.tar.gz
cpython-bb9b1c165d0193e6783981c1ac1e0ab9bf171c4c.tar.bz2
Issue #15402: Simplify Struct.__sizeof__ and make tests more precise.
Diffstat (limited to 'Lib/test/test_struct.py')
-rw-r--r--Lib/test/test_struct.py51
1 files changed, 41 insertions, 10 deletions
diff --git a/Lib/test/test_struct.py b/Lib/test/test_struct.py
index 53c0875..919cd63 100644
--- a/Lib/test/test_struct.py
+++ b/Lib/test/test_struct.py
@@ -3,7 +3,7 @@ import unittest
import struct
import sys
-from test.support import run_unittest
+from test.support import run_unittest, cpython_only
ISBIGENDIAN = sys.byteorder == "big"
IS32BIT = sys.maxsize == 0x7fffffff
@@ -40,6 +40,33 @@ def bigendian_to_native(value):
return string_reverse(value)
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 test_isbigendian(self):
self.assertEqual((struct.pack('=i', 1)[0] == 0), ISBIGENDIAN)
@@ -572,15 +599,19 @@ class StructTest(unittest.TestCase):
s = struct.Struct('i')
s.__init__('ii')
- 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)