Commit 59c4c53b authored by Martin v. Löwis's avatar Martin v. Löwis

Issue #15467: Move helpers for __sizeof__ tests into test_support.

Patch by Serhiy Storchaka.
parent cc6afead
......@@ -23,6 +23,9 @@ import time
import sysconfig
import fnmatch
import logging.handlers
import struct
import tempfile
import _testcapi
try:
import _thread, threading
......@@ -984,6 +987,31 @@ def python_is_optimized():
return final_opt and final_opt != '-O0'
_header = '2P'
if hasattr(sys, "gettotalrefcount"):
_header = '2P' + _header
_vheader = _header + 'P'
def calcobjsize(fmt):
return struct.calcsize(_header + fmt + '0P')
def calcvobjsize(fmt):
return struct.calcsize(_vheader + fmt + '0P')
_TPFLAGS_HAVE_GC = 1<<14
_TPFLAGS_HEAPTYPE = 1<<9
def check_sizeof(test, o, size):
result = sys.getsizeof(o)
# add GC header size
if ((type(o) == type) and (o.__flags__ & _TPFLAGS_HEAPTYPE) or\
((type(o) != type) and (type(o).__flags__ & _TPFLAGS_HAVE_GC))):
size += _testcapi.SIZEOF_PYGC_HEAD
msg = 'wrong size for %s: got %d, expected %d' \
% (type(o), result, size)
test.assertEqual(result, size, msg)
#=======================================================================
# Decorator for running a function in a different locale, correctly resetting
# it afterwards.
......
......@@ -3,7 +3,7 @@ import unittest
import struct
import sys
from test.support import run_unittest, cpython_only
from test import support
ISBIGENDIAN = sys.byteorder == "big"
IS32BIT = sys.maxsize == 0x7fffffff
......@@ -30,32 +30,6 @@ 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)
......@@ -583,7 +557,14 @@ class StructTest(unittest.TestCase):
s = struct.Struct('i')
s.__init__('ii')
@cpython_only
def check_sizeof(self, format_str, number_of_codes):
# The size of 'PyStructObject'
totalsize = support.calcobjsize('5P')
# The size taken up by the 'formatcode' dynamic array
totalsize += struct.calcsize('3P') * (number_of_codes + 1)
support.check_sizeof(self, struct.Struct(format_str), totalsize)
@support.cpython_only
def test__sizeof__(self):
for code in integer_codes:
self.check_sizeof(code, 1)
......@@ -598,7 +579,7 @@ class StructTest(unittest.TestCase):
self.check_sizeof('0c', 0)
def test_main():
run_unittest(StructTest)
support.run_unittest(StructTest)
if __name__ == '__main__':
test_main()
This diff is collapsed.
......@@ -383,6 +383,9 @@ Extension Modules
Tests
-----
- Issue #15467: Move helpers for __sizeof__ tests into test_support.
Patch by Serhiy Storchaka.
- Issue #15320: Make iterating the list of tests thread-safe when running
tests in multiprocess mode. Patch by Chris Jerdonek.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment