summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorMartin v. Löwis <martin@v.loewis.de>2012-07-29 14:33:05 (GMT)
committerMartin v. Löwis <martin@v.loewis.de>2012-07-29 14:33:05 (GMT)
commit33f799725cb9ae21f0fed87eea4b13d7611ceeee (patch)
treeba45c2e081d23c66388b5530c584085d0920b932 /Lib
parent90bc2dbccef5d74f0dd6875c555ee33187cfec37 (diff)
downloadcpython-33f799725cb9ae21f0fed87eea4b13d7611ceeee.zip
cpython-33f799725cb9ae21f0fed87eea4b13d7611ceeee.tar.gz
cpython-33f799725cb9ae21f0fed87eea4b13d7611ceeee.tar.bz2
Issue #15467: Move helpers for __sizeof__ tests into test_support.
Patch by Serhiy Storchaka.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/support.py28
-rw-r--r--Lib/test/test_struct.py39
-rw-r--r--Lib/test/test_sys.py202
3 files changed, 121 insertions, 148 deletions
diff --git a/Lib/test/support.py b/Lib/test/support.py
index 79293ed..162805d 100644
--- a/Lib/test/support.py
+++ b/Lib/test/support.py
@@ -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.
diff --git a/Lib/test/test_struct.py b/Lib/test/test_struct.py
index b77982e..dcc73ab 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, 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()
diff --git a/Lib/test/test_sys.py b/Lib/test/test_sys.py
index 3268b1a..b055fcc 100644
--- a/Lib/test/test_sys.py
+++ b/Lib/test/test_sys.py
@@ -570,22 +570,8 @@ class SysModuleTest(unittest.TestCase):
class SizeofTest(unittest.TestCase):
- TPFLAGS_HAVE_GC = 1<<14
- TPFLAGS_HEAPTYPE = 1<<9
-
def setUp(self):
- self.c = len(struct.pack('c', b' '))
- self.H = len(struct.pack('H', 0))
- self.i = len(struct.pack('i', 0))
- self.l = len(struct.pack('l', 0))
- self.P = len(struct.pack('P', 0))
- # due to missing size_t information from struct, it is assumed that
- # sizeof(Py_ssize_t) = sizeof(void*)
- self.header = 'PP'
- self.vheader = self.header + 'P'
- if hasattr(sys, "gettotalrefcount"):
- self.header += '2P'
- self.vheader += '2P'
+ self.P = struct.calcsize('P')
self.longdigit = sys.int_info.sizeof_digit
import _testcapi
self.gc_headsize = _testcapi.SIZEOF_PYGC_HEAD
@@ -595,123 +581,102 @@ class SizeofTest(unittest.TestCase):
self.file.close()
test.support.unlink(test.support.TESTFN)
- def check_sizeof(self, o, size):
- result = sys.getsizeof(o)
- # add GC header size
- if ((type(o) == type) and (o.__flags__ & self.TPFLAGS_HEAPTYPE) or\
- ((type(o) != type) and (type(o).__flags__ & self.TPFLAGS_HAVE_GC))):
- size += self.gc_headsize
- msg = 'wrong size for %s: got %d, expected %d' \
- % (type(o), result, size)
- self.assertEqual(result, size, msg)
-
- def calcsize(self, 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')
+ check_sizeof = test.support.check_sizeof
def test_gc_head_size(self):
# Check that the gc header size is added to objects tracked by the gc.
- h = self.header
- vh = self.vheader
- size = self.calcsize
+ vsize = test.support.calcvobjsize
gc_header_size = self.gc_headsize
# bool objects are not gc tracked
- self.assertEqual(sys.getsizeof(True), size(vh) + self.longdigit)
+ self.assertEqual(sys.getsizeof(True), vsize('') + self.longdigit)
# but lists are
- self.assertEqual(sys.getsizeof([]), size(vh + 'PP') + gc_header_size)
+ self.assertEqual(sys.getsizeof([]), vsize('PP') + gc_header_size)
def test_default(self):
- h = self.header
- vh = self.vheader
- size = self.calcsize
- self.assertEqual(sys.getsizeof(True), size(vh) + self.longdigit)
- self.assertEqual(sys.getsizeof(True, -1), size(vh) + self.longdigit)
+ vsize = test.support.calcvobjsize
+ self.assertEqual(sys.getsizeof(True), vsize('') + self.longdigit)
+ self.assertEqual(sys.getsizeof(True, -1), vsize('') + self.longdigit)
def test_objecttypes(self):
# check all types defined in Objects/
- h = self.header
- vh = self.vheader
- size = self.calcsize
+ size = test.support.calcobjsize
+ vsize = test.support.calcvobjsize
check = self.check_sizeof
# bool
- check(True, size(vh) + self.longdigit)
+ check(True, vsize('') + self.longdigit)
# buffer
# XXX
# builtin_function_or_method
- check(len, size(h + '3P'))
+ check(len, size('3P')) # XXX check layout
# bytearray
samples = [b'', b'u'*100000]
for sample in samples:
x = bytearray(sample)
- check(x, size(vh + 'iPP') + x.__alloc__() * self.c)
+ check(x, vsize('iPP') + x.__alloc__())
# bytearray_iterator
- check(iter(bytearray()), size(h + 'PP'))
+ check(iter(bytearray()), size('PP'))
# cell
def get_cell():
x = 42
def inner():
return x
return inner
- check(get_cell().__closure__[0], size(h + 'P'))
+ check(get_cell().__closure__[0], size('P'))
# code
- check(get_cell().__code__, size(h + '5i8Pi3P'))
+ check(get_cell().__code__, size('5i8Pi3P'))
# complex
- check(complex(0,1), size(h + '2d'))
+ check(complex(0,1), size('2d'))
# method_descriptor (descriptor object)
- check(str.lower, size(h + '2PP'))
+ check(str.lower, size('2PP'))
# classmethod_descriptor (descriptor object)
# XXX
# member_descriptor (descriptor object)
import datetime
- check(datetime.timedelta.days, size(h + '2PP'))
+ check(datetime.timedelta.days, size('2PP'))
# getset_descriptor (descriptor object)
import collections
- check(collections.defaultdict.default_factory, size(h + '2PP'))
+ check(collections.defaultdict.default_factory, size('2PP'))
# wrapper_descriptor (descriptor object)
- check(int.__add__, size(h + '2P2P'))
+ check(int.__add__, size('2P2P'))
# method-wrapper (descriptor object)
- check({}.__iter__, size(h + '2P'))
+ check({}.__iter__, size('2P'))
# dict
- check({}, size(h + '3P2P' + 8*'P2P'))
+ check({}, size('3P2P' + 8*'P2P'))
longdict = {1:1, 2:2, 3:3, 4:4, 5:5, 6:6, 7:7, 8:8}
- check(longdict, size(h + '3P2P' + 8*'P2P') + 16*size('P2P'))
+ check(longdict, size('3P2P' + 8*'P2P') + 16*struct.calcsize('P2P'))
# dictionary-keyiterator
- check({}.keys(), size(h + 'P'))
+ check({}.keys(), size('P'))
# dictionary-valueiterator
- check({}.values(), size(h + 'P'))
+ check({}.values(), size('P'))
# dictionary-itemiterator
- check({}.items(), size(h + 'P'))
+ check({}.items(), size('P'))
+ # dictionary iterator
+ check(iter({}), size('P2PPP'))
# dictproxy
class C(object): pass
- check(C.__dict__, size(h + 'P'))
+ check(C.__dict__, size('P'))
# BaseException
- check(BaseException(), size(h + '5P'))
+ check(BaseException(), size('5P'))
# UnicodeEncodeError
- check(UnicodeEncodeError("", "", 0, 0, ""), size(h + '5P 2P2PP'))
+ check(UnicodeEncodeError("", "", 0, 0, ""), size('5P 2P2PP'))
# UnicodeDecodeError
- # XXX
-# check(UnicodeDecodeError("", "", 0, 0, ""), size(h + '5P2PP'))
+ check(UnicodeDecodeError("", b"", 0, 0, ""), size('5P 2P2PP'))
# UnicodeTranslateError
- check(UnicodeTranslateError("", 0, 1, ""), size(h + '5P 2P2PP'))
+ check(UnicodeTranslateError("", 0, 1, ""), size('5P 2P2PP'))
# ellipses
- check(Ellipsis, size(h + ''))
+ check(Ellipsis, size(''))
# EncodingMap
import codecs, encodings.iso8859_3
x = codecs.charmap_build(encodings.iso8859_3.decoding_table)
- check(x, size(h + '32B2iB'))
+ check(x, size('32B2iB'))
# enumerate
- check(enumerate([]), size(h + 'l3P'))
+ check(enumerate([]), size('l3P'))
# reverse
- check(reversed(''), size(h + 'PP'))
+ check(reversed(''), size('PP'))
# float
- check(float(0), size(h + 'd'))
+ check(float(0), size('d'))
# sys.floatinfo
- check(sys.float_info, size(vh) + self.P * len(sys.float_info))
+ check(sys.float_info, vsize('') + self.P * len(sys.float_info))
# frame
import inspect
CO_MAXBLOCKS = 20
@@ -720,10 +685,10 @@ class SizeofTest(unittest.TestCase):
nfrees = len(x.f_code.co_freevars)
extras = x.f_code.co_stacksize + x.f_code.co_nlocals +\
ncells + nfrees - 1
- check(x, size(vh + '12P3i' + CO_MAXBLOCKS*'3i' + 'P' + extras*'P'))
+ check(x, vsize('12P3i' + CO_MAXBLOCKS*'3i' + 'P' + extras*'P'))
# function
def func(): pass
- check(func, size(h + '11P'))
+ check(func, size('11P'))
class c():
@staticmethod
def foo():
@@ -732,68 +697,68 @@ class SizeofTest(unittest.TestCase):
def bar(cls):
pass
# staticmethod
- check(foo, size(h + 'P'))
+ check(foo, size('P'))
# classmethod
- check(bar, size(h + 'P'))
+ check(bar, size('P'))
# generator
def get_gen(): yield 1
- check(get_gen(), size(h + 'Pi2P'))
+ check(get_gen(), size('Pi2P'))
# iterator
- check(iter('abc'), size(h + 'lP'))
+ check(iter('abc'), size('lP'))
# callable-iterator
import re
- check(re.finditer('',''), size(h + '2P'))
+ check(re.finditer('',''), size('2P'))
# list
samples = [[], [1,2,3], ['1', '2', '3']]
for sample in samples:
- check(sample, size(vh + 'PP') + len(sample)*self.P)
+ check(sample, vsize('PP') + len(sample)*self.P)
# sortwrapper (list)
# XXX
# cmpwrapper (list)
# XXX
# listiterator (list)
- check(iter([]), size(h + 'lP'))
+ check(iter([]), size('lP'))
# listreverseiterator (list)
- check(reversed([]), size(h + 'lP'))
+ check(reversed([]), size('lP'))
# long
- check(0, size(vh))
- check(1, size(vh) + self.longdigit)
- check(-1, size(vh) + self.longdigit)
+ check(0, vsize(''))
+ check(1, vsize('') + self.longdigit)
+ check(-1, vsize('') + self.longdigit)
PyLong_BASE = 2**sys.int_info.bits_per_digit
- check(int(PyLong_BASE), size(vh) + 2*self.longdigit)
- check(int(PyLong_BASE**2-1), size(vh) + 2*self.longdigit)
- check(int(PyLong_BASE**2), size(vh) + 3*self.longdigit)
- # memory
- check(memoryview(b''), size(h + 'PP2P2i7P'))
+ check(int(PyLong_BASE), vsize('') + 2*self.longdigit)
+ check(int(PyLong_BASE**2-1), vsize('') + 2*self.longdigit)
+ check(int(PyLong_BASE**2), vsize('') + 3*self.longdigit)
+ # memoryview
+ check(memoryview(b''), size('PP2P2i7P'))
# module
- check(unittest, size(h + '3P'))
+ check(unittest, size('3P'))
# None
- check(None, size(h + ''))
+ check(None, size(''))
# NotImplementedType
- check(NotImplemented, size(h))
+ check(NotImplemented, size(''))
# object
- check(object(), size(h + ''))
+ check(object(), size(''))
# property (descriptor object)
class C(object):
def getx(self): return self.__x
def setx(self, value): self.__x = value
def delx(self): del self.__x
x = property(getx, setx, delx, "")
- check(x, size(h + '4Pi'))
+ check(x, size('4Pi'))
# PyCapsule
# XXX
# rangeiterator
- check(iter(range(1)), size(h + '4l'))
+ check(iter(range(1)), size('4l'))
# reverse
- check(reversed(''), size(h + 'PP'))
+ check(reversed(''), size('PP'))
# range
- check(range(1), size(h + '4P'))
- check(range(66000), size(h + '4P'))
+ check(range(1), size('4P'))
+ check(range(66000), size('4P'))
# set
# frozenset
PySet_MINSIZE = 8
samples = [[], range(10), range(50)]
- s = size(h + '3P2P' + PySet_MINSIZE*'lP' + 'lP')
+ s = size('3P2P' + PySet_MINSIZE*'PP' + 'PP')
for sample in samples:
minused = len(sample)
if minused == 0: tmp = 1
@@ -810,18 +775,18 @@ class SizeofTest(unittest.TestCase):
check(set(sample), s + newsize*struct.calcsize('lP'))
check(frozenset(sample), s + newsize*struct.calcsize('lP'))
# setiterator
- check(iter(set()), size(h + 'P3P'))
+ check(iter(set()), size('P3P'))
# slice
- check(slice(0), size(h + '3P'))
+ check(slice(0), size('3P'))
# super
- check(super(int), size(h + '3P'))
+ check(super(int), size('3P'))
# tuple
- check((), size(vh))
- check((1,2,3), size(vh) + 3*self.P)
+ check((), vsize(''))
+ check((1,2,3), vsize('') + 3*self.P)
# type
# (PyTypeObject + PyNumberMethods + PyMappingMethods +
# PySequenceMethods + PyBufferProcs)
- s = size(vh + 'P2P15Pl4PP9PP11PI') + size('16Pi17P 3P 10P 2P 2P')
+ s = vsize('P2P15Pl4PP9PP11PI') + struct.calcsize('16Pi17P 3P 10P 2P 2P')
check(int, s)
# class
class newstyleclass(object): pass
@@ -832,39 +797,38 @@ class SizeofTest(unittest.TestCase):
# we need to test for both sizes, because we don't know if the string
# has been cached
for s in samples:
- basicsize = size(h + 'PPPiP') + usize * (len(s) + 1)
+ basicsize = size('PPPiP') + usize * (len(s) + 1)
check(s, basicsize)
# weakref
import weakref
- check(weakref.ref(int), size(h + '2Pl2P'))
+ check(weakref.ref(int), size('2Pl2P'))
# weakproxy
# XXX
# weakcallableproxy
- check(weakref.proxy(int), size(h + '2Pl2P'))
+ check(weakref.proxy(int), size('2Pl2P'))
def test_pythontypes(self):
# check all types defined in Python/
- h = self.header
- vh = self.vheader
- size = self.calcsize
+ size = test.support.calcobjsize
+ vsize = test.support.calcvobjsize
check = self.check_sizeof
# _ast.AST
import _ast
- check(_ast.AST(), size(h + ''))
+ check(_ast.AST(), size(''))
# imp.NullImporter
import imp
- check(imp.NullImporter(self.file.name), size(h + ''))
+ check(imp.NullImporter(self.file.name), size(''))
try:
raise TypeError
except TypeError:
tb = sys.exc_info()[2]
# traceback
if tb != None:
- check(tb, size(h + '2P2i'))
+ check(tb, size('2P2i'))
# symtable entry
# XXX
# sys.flags
- check(sys.flags, size(vh) + self.P * len(sys.flags))
+ check(sys.flags, vsize('') + self.P * len(sys.flags))
def test_main():