mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
Issue #15467: Move helpers for __sizeof__ tests into test_support.
Patch by Serhiy Storchaka.
This commit is contained in:
parent
6812346808
commit
c02e1e65c4
4 changed files with 129 additions and 151 deletions
|
@ -3,8 +3,8 @@ import array
|
||||||
import unittest
|
import unittest
|
||||||
import struct
|
import struct
|
||||||
import inspect
|
import inspect
|
||||||
from test.test_support import (run_unittest, check_warnings,
|
from test import test_support as support
|
||||||
check_py3k_warnings, cpython_only)
|
from test.test_support import (check_warnings, check_py3k_warnings)
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
ISBIGENDIAN = sys.byteorder == "big"
|
ISBIGENDIAN = sys.byteorder == "big"
|
||||||
|
@ -33,33 +33,6 @@ def bigendian_to_native(value):
|
||||||
|
|
||||||
class StructTest(unittest.TestCase):
|
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):
|
def check_float_coerce(self, format, number):
|
||||||
# SF bug 1530559. struct.pack raises TypeError where it used
|
# SF bug 1530559. struct.pack raises TypeError where it used
|
||||||
# to convert.
|
# to convert.
|
||||||
|
@ -572,7 +545,14 @@ class StructTest(unittest.TestCase):
|
||||||
hugecount2 = '{}b{}H'.format(sys.maxsize//2, sys.maxsize//2)
|
hugecount2 = '{}b{}H'.format(sys.maxsize//2, sys.maxsize//2)
|
||||||
self.assertRaises(struct.error, struct.calcsize, hugecount2)
|
self.assertRaises(struct.error, struct.calcsize, hugecount2)
|
||||||
|
|
||||||
@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):
|
def test__sizeof__(self):
|
||||||
for code in integer_codes:
|
for code in integer_codes:
|
||||||
self.check_sizeof(code, 1)
|
self.check_sizeof(code, 1)
|
||||||
|
@ -587,7 +567,7 @@ class StructTest(unittest.TestCase):
|
||||||
self.check_sizeof('0c', 0)
|
self.check_sizeof('0c', 0)
|
||||||
|
|
||||||
def test_main():
|
def test_main():
|
||||||
run_unittest(StructTest)
|
support.run_unittest(StructTest)
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
test_main()
|
test_main()
|
||||||
|
|
|
@ -18,6 +18,8 @@ import importlib
|
||||||
import UserDict
|
import UserDict
|
||||||
import re
|
import re
|
||||||
import time
|
import time
|
||||||
|
import struct
|
||||||
|
import _testcapi
|
||||||
try:
|
try:
|
||||||
import thread
|
import thread
|
||||||
except ImportError:
|
except ImportError:
|
||||||
|
@ -861,6 +863,32 @@ def gc_collect():
|
||||||
gc.collect()
|
gc.collect()
|
||||||
|
|
||||||
|
|
||||||
|
_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
|
# Decorator for running a function in a different locale, correctly resetting
|
||||||
# it afterwards.
|
# it afterwards.
|
||||||
|
|
|
@ -490,22 +490,8 @@ class SysModuleTest(unittest.TestCase):
|
||||||
|
|
||||||
class SizeofTest(unittest.TestCase):
|
class SizeofTest(unittest.TestCase):
|
||||||
|
|
||||||
TPFLAGS_HAVE_GC = 1<<14
|
|
||||||
TPFLAGS_HEAPTYPE = 1L<<9
|
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.c = len(struct.pack('c', ' '))
|
self.P = struct.calcsize('P')
|
||||||
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.longdigit = sys.long_info.sizeof_digit
|
self.longdigit = sys.long_info.sizeof_digit
|
||||||
import _testcapi
|
import _testcapi
|
||||||
self.gc_headsize = _testcapi.SIZEOF_PYGC_HEAD
|
self.gc_headsize = _testcapi.SIZEOF_PYGC_HEAD
|
||||||
|
@ -515,128 +501,109 @@ class SizeofTest(unittest.TestCase):
|
||||||
self.file.close()
|
self.file.close()
|
||||||
test.test_support.unlink(test.test_support.TESTFN)
|
test.test_support.unlink(test.test_support.TESTFN)
|
||||||
|
|
||||||
def check_sizeof(self, o, size):
|
check_sizeof = test.test_support.check_sizeof
|
||||||
result = sys.getsizeof(o)
|
|
||||||
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')
|
|
||||||
|
|
||||||
def test_gc_head_size(self):
|
def test_gc_head_size(self):
|
||||||
# Check that the gc header size is added to objects tracked by the gc.
|
# Check that the gc header size is added to objects tracked by the gc.
|
||||||
h = self.header
|
size = test.test_support.calcobjsize
|
||||||
size = self.calcsize
|
|
||||||
gc_header_size = self.gc_headsize
|
gc_header_size = self.gc_headsize
|
||||||
# bool objects are not gc tracked
|
# bool objects are not gc tracked
|
||||||
self.assertEqual(sys.getsizeof(True), size(h + 'l'))
|
self.assertEqual(sys.getsizeof(True), size('l'))
|
||||||
# but lists are
|
# but lists are
|
||||||
self.assertEqual(sys.getsizeof([]), size(h + 'P PP') + gc_header_size)
|
self.assertEqual(sys.getsizeof([]), size('P PP') + gc_header_size)
|
||||||
|
|
||||||
def test_default(self):
|
def test_default(self):
|
||||||
h = self.header
|
size = test.test_support.calcobjsize
|
||||||
size = self.calcsize
|
self.assertEqual(sys.getsizeof(True, -1), size('l'))
|
||||||
self.assertEqual(sys.getsizeof(True, -1), size(h + 'l'))
|
|
||||||
|
|
||||||
def test_objecttypes(self):
|
def test_objecttypes(self):
|
||||||
# check all types defined in Objects/
|
# check all types defined in Objects/
|
||||||
h = self.header
|
size = test.test_support.calcobjsize
|
||||||
vh = self.vheader
|
vsize = test.test_support.calcvobjsize
|
||||||
size = self.calcsize
|
|
||||||
check = self.check_sizeof
|
check = self.check_sizeof
|
||||||
# bool
|
# bool
|
||||||
check(True, size(h + 'l'))
|
check(True, size('l'))
|
||||||
# buffer
|
# buffer
|
||||||
with test.test_support.check_py3k_warnings():
|
with test.test_support.check_py3k_warnings():
|
||||||
check(buffer(''), size(h + '2P2Pil'))
|
check(buffer(''), size('2P2Pil'))
|
||||||
# builtin_function_or_method
|
# builtin_function_or_method
|
||||||
check(len, size(h + '3P'))
|
check(len, size('3P'))
|
||||||
# bytearray
|
# bytearray
|
||||||
samples = ['', 'u'*100000]
|
samples = ['', 'u'*100000]
|
||||||
for sample in samples:
|
for sample in samples:
|
||||||
x = bytearray(sample)
|
x = bytearray(sample)
|
||||||
check(x, size(vh + 'iPP') + x.__alloc__() * self.c)
|
check(x, vsize('iPP') + x.__alloc__())
|
||||||
# bytearray_iterator
|
# bytearray_iterator
|
||||||
check(iter(bytearray()), size(h + 'PP'))
|
check(iter(bytearray()), size('PP'))
|
||||||
# cell
|
# cell
|
||||||
def get_cell():
|
def get_cell():
|
||||||
x = 42
|
x = 42
|
||||||
def inner():
|
def inner():
|
||||||
return x
|
return x
|
||||||
return inner
|
return inner
|
||||||
check(get_cell().func_closure[0], size(h + 'P'))
|
check(get_cell().func_closure[0], size('P'))
|
||||||
# classobj (old-style class)
|
# classobj (old-style class)
|
||||||
class class_oldstyle():
|
class class_oldstyle():
|
||||||
def method():
|
def method():
|
||||||
pass
|
pass
|
||||||
check(class_oldstyle, size(h + '7P'))
|
check(class_oldstyle, size('7P'))
|
||||||
# instance (old-style class)
|
# instance (old-style class)
|
||||||
check(class_oldstyle(), size(h + '3P'))
|
check(class_oldstyle(), size('3P'))
|
||||||
# instancemethod (old-style class)
|
# instancemethod (old-style class)
|
||||||
check(class_oldstyle().method, size(h + '4P'))
|
check(class_oldstyle().method, size('4P'))
|
||||||
# complex
|
# complex
|
||||||
check(complex(0,1), size(h + '2d'))
|
check(complex(0,1), size('2d'))
|
||||||
# code
|
# code
|
||||||
check(get_cell().func_code, size(h + '4i8Pi3P'))
|
check(get_cell().func_code, size('4i8Pi3P'))
|
||||||
# BaseException
|
# BaseException
|
||||||
check(BaseException(), size(h + '3P'))
|
check(BaseException(), size('3P'))
|
||||||
# UnicodeEncodeError
|
# UnicodeEncodeError
|
||||||
check(UnicodeEncodeError("", u"", 0, 0, ""), size(h + '5P2PP'))
|
check(UnicodeEncodeError("", u"", 0, 0, ""), size('5P2PP'))
|
||||||
# UnicodeDecodeError
|
# UnicodeDecodeError
|
||||||
check(UnicodeDecodeError("", "", 0, 0, ""), size(h + '5P2PP'))
|
check(UnicodeDecodeError("", "", 0, 0, ""), size('5P2PP'))
|
||||||
# UnicodeTranslateError
|
# UnicodeTranslateError
|
||||||
check(UnicodeTranslateError(u"", 0, 1, ""), size(h + '5P2PP'))
|
check(UnicodeTranslateError(u"", 0, 1, ""), size('5P2PP'))
|
||||||
# method_descriptor (descriptor object)
|
# method_descriptor (descriptor object)
|
||||||
check(str.lower, size(h + '2PP'))
|
check(str.lower, size('2PP'))
|
||||||
# classmethod_descriptor (descriptor object)
|
# classmethod_descriptor (descriptor object)
|
||||||
# XXX
|
# XXX
|
||||||
# member_descriptor (descriptor object)
|
# member_descriptor (descriptor object)
|
||||||
import datetime
|
import datetime
|
||||||
check(datetime.timedelta.days, size(h + '2PP'))
|
check(datetime.timedelta.days, size('2PP'))
|
||||||
# getset_descriptor (descriptor object)
|
# getset_descriptor (descriptor object)
|
||||||
import __builtin__
|
import __builtin__
|
||||||
check(__builtin__.file.closed, size(h + '2PP'))
|
check(__builtin__.file.closed, size('2PP'))
|
||||||
# wrapper_descriptor (descriptor object)
|
# wrapper_descriptor (descriptor object)
|
||||||
check(int.__add__, size(h + '2P2P'))
|
check(int.__add__, size('2P2P'))
|
||||||
# dictproxy
|
# dictproxy
|
||||||
class C(object): pass
|
class C(object): pass
|
||||||
check(C.__dict__, size(h + 'P'))
|
check(C.__dict__, size('P'))
|
||||||
# method-wrapper (descriptor object)
|
# method-wrapper (descriptor object)
|
||||||
check({}.__iter__, size(h + '2P'))
|
check({}.__iter__, size('2P'))
|
||||||
# dict
|
# dict
|
||||||
check({}, size(h + '3P2P' + 8*'P2P'))
|
check({}, size('3P2P' + 8*'P2P'))
|
||||||
x = {1:1, 2:2, 3:3, 4:4, 5:5, 6:6, 7:7, 8:8}
|
x = {1:1, 2:2, 3:3, 4:4, 5:5, 6:6, 7:7, 8:8}
|
||||||
check(x, size(h + '3P2P' + 8*'P2P') + 16*size('P2P'))
|
check(x, size('3P2P' + 8*'P2P') + 16*struct.calcsize('P2P'))
|
||||||
# dictionary-keyiterator
|
# dictionary-keyiterator
|
||||||
check({}.iterkeys(), size(h + 'P2PPP'))
|
check({}.iterkeys(), size('P2PPP'))
|
||||||
# dictionary-valueiterator
|
# dictionary-valueiterator
|
||||||
check({}.itervalues(), size(h + 'P2PPP'))
|
check({}.itervalues(), size('P2PPP'))
|
||||||
# dictionary-itemiterator
|
# dictionary-itemiterator
|
||||||
check({}.iteritems(), size(h + 'P2PPP'))
|
check({}.iteritems(), size('P2PPP'))
|
||||||
# ellipses
|
# ellipses
|
||||||
check(Ellipsis, size(h + ''))
|
check(Ellipsis, size(''))
|
||||||
# EncodingMap
|
# EncodingMap
|
||||||
import codecs, encodings.iso8859_3
|
import codecs, encodings.iso8859_3
|
||||||
x = codecs.charmap_build(encodings.iso8859_3.decoding_table)
|
x = codecs.charmap_build(encodings.iso8859_3.decoding_table)
|
||||||
check(x, size(h + '32B2iB'))
|
check(x, size('32B2iB'))
|
||||||
# enumerate
|
# enumerate
|
||||||
check(enumerate([]), size(h + 'l3P'))
|
check(enumerate([]), size('l3P'))
|
||||||
# file
|
# file
|
||||||
check(self.file, size(h + '4P2i4P3i3P3i'))
|
check(self.file, size('4P2i4P3i3P3i'))
|
||||||
# float
|
# float
|
||||||
check(float(0), size(h + 'd'))
|
check(float(0), size('d'))
|
||||||
# sys.floatinfo
|
# 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
|
# frame
|
||||||
import inspect
|
import inspect
|
||||||
CO_MAXBLOCKS = 20
|
CO_MAXBLOCKS = 20
|
||||||
|
@ -645,10 +612,10 @@ class SizeofTest(unittest.TestCase):
|
||||||
nfrees = len(x.f_code.co_freevars)
|
nfrees = len(x.f_code.co_freevars)
|
||||||
extras = x.f_code.co_stacksize + x.f_code.co_nlocals +\
|
extras = x.f_code.co_stacksize + x.f_code.co_nlocals +\
|
||||||
ncells + nfrees - 1
|
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
|
# function
|
||||||
def func(): pass
|
def func(): pass
|
||||||
check(func, size(h + '9P'))
|
check(func, size('9P'))
|
||||||
class c():
|
class c():
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def foo():
|
def foo():
|
||||||
|
@ -657,65 +624,65 @@ class SizeofTest(unittest.TestCase):
|
||||||
def bar(cls):
|
def bar(cls):
|
||||||
pass
|
pass
|
||||||
# staticmethod
|
# staticmethod
|
||||||
check(foo, size(h + 'P'))
|
check(foo, size('P'))
|
||||||
# classmethod
|
# classmethod
|
||||||
check(bar, size(h + 'P'))
|
check(bar, size('P'))
|
||||||
# generator
|
# generator
|
||||||
def get_gen(): yield 1
|
def get_gen(): yield 1
|
||||||
check(get_gen(), size(h + 'Pi2P'))
|
check(get_gen(), size('Pi2P'))
|
||||||
# integer
|
# integer
|
||||||
check(1, size(h + 'l'))
|
check(1, size('l'))
|
||||||
check(100, size(h + 'l'))
|
check(100, size('l'))
|
||||||
# iterator
|
# iterator
|
||||||
check(iter('abc'), size(h + 'lP'))
|
check(iter('abc'), size('lP'))
|
||||||
# callable-iterator
|
# callable-iterator
|
||||||
import re
|
import re
|
||||||
check(re.finditer('',''), size(h + '2P'))
|
check(re.finditer('',''), size('2P'))
|
||||||
# list
|
# list
|
||||||
samples = [[], [1,2,3], ['1', '2', '3']]
|
samples = [[], [1,2,3], ['1', '2', '3']]
|
||||||
for sample in samples:
|
for sample in samples:
|
||||||
check(sample, size(vh + 'PP') + len(sample)*self.P)
|
check(sample, vsize('PP') + len(sample)*self.P)
|
||||||
# sortwrapper (list)
|
# sortwrapper (list)
|
||||||
# XXX
|
# XXX
|
||||||
# cmpwrapper (list)
|
# cmpwrapper (list)
|
||||||
# XXX
|
# XXX
|
||||||
# listiterator (list)
|
# listiterator (list)
|
||||||
check(iter([]), size(h + 'lP'))
|
check(iter([]), size('lP'))
|
||||||
# listreverseiterator (list)
|
# listreverseiterator (list)
|
||||||
check(reversed([]), size(h + 'lP'))
|
check(reversed([]), size('lP'))
|
||||||
# long
|
# long
|
||||||
check(0L, size(vh))
|
check(0L, vsize(''))
|
||||||
check(1L, size(vh) + self.longdigit)
|
check(1L, vsize('') + self.longdigit)
|
||||||
check(-1L, size(vh) + self.longdigit)
|
check(-1L, vsize('') + self.longdigit)
|
||||||
PyLong_BASE = 2**sys.long_info.bits_per_digit
|
PyLong_BASE = 2**sys.long_info.bits_per_digit
|
||||||
check(long(PyLong_BASE), size(vh) + 2*self.longdigit)
|
check(long(PyLong_BASE), vsize('') + 2*self.longdigit)
|
||||||
check(long(PyLong_BASE**2-1), size(vh) + 2*self.longdigit)
|
check(long(PyLong_BASE**2-1), vsize('') + 2*self.longdigit)
|
||||||
check(long(PyLong_BASE**2), size(vh) + 3*self.longdigit)
|
check(long(PyLong_BASE**2), vsize('') + 3*self.longdigit)
|
||||||
# module
|
# module
|
||||||
check(unittest, size(h + 'P'))
|
check(unittest, size('P'))
|
||||||
# None
|
# None
|
||||||
check(None, size(h + ''))
|
check(None, size(''))
|
||||||
# object
|
# object
|
||||||
check(object(), size(h + ''))
|
check(object(), size(''))
|
||||||
# property (descriptor object)
|
# property (descriptor object)
|
||||||
class C(object):
|
class C(object):
|
||||||
def getx(self): return self.__x
|
def getx(self): return self.__x
|
||||||
def setx(self, value): self.__x = value
|
def setx(self, value): self.__x = value
|
||||||
def delx(self): del self.__x
|
def delx(self): del self.__x
|
||||||
x = property(getx, setx, delx, "")
|
x = property(getx, setx, delx, "")
|
||||||
check(x, size(h + '4Pi'))
|
check(x, size('4Pi'))
|
||||||
# PyCObject
|
# PyCObject
|
||||||
# PyCapsule
|
# PyCapsule
|
||||||
# XXX
|
# XXX
|
||||||
# rangeiterator
|
# rangeiterator
|
||||||
check(iter(xrange(1)), size(h + '4l'))
|
check(iter(xrange(1)), size('4l'))
|
||||||
# reverse
|
# reverse
|
||||||
check(reversed(''), size(h + 'PP'))
|
check(reversed(''), size('PP'))
|
||||||
# set
|
# set
|
||||||
# frozenset
|
# frozenset
|
||||||
PySet_MINSIZE = 8
|
PySet_MINSIZE = 8
|
||||||
samples = [[], range(10), range(50)]
|
samples = [[], range(10), range(50)]
|
||||||
s = size(h + '3P2P' + PySet_MINSIZE*'lP' + 'lP')
|
s = size('3P2P' + PySet_MINSIZE*'lP' + 'lP')
|
||||||
for sample in samples:
|
for sample in samples:
|
||||||
minused = len(sample)
|
minused = len(sample)
|
||||||
if minused == 0: tmp = 1
|
if minused == 0: tmp = 1
|
||||||
|
@ -732,23 +699,24 @@ class SizeofTest(unittest.TestCase):
|
||||||
check(set(sample), s + newsize*struct.calcsize('lP'))
|
check(set(sample), s + newsize*struct.calcsize('lP'))
|
||||||
check(frozenset(sample), s + newsize*struct.calcsize('lP'))
|
check(frozenset(sample), s + newsize*struct.calcsize('lP'))
|
||||||
# setiterator
|
# setiterator
|
||||||
check(iter(set()), size(h + 'P3P'))
|
check(iter(set()), size('P3P'))
|
||||||
# slice
|
# slice
|
||||||
check(slice(1), size(h + '3P'))
|
check(slice(1), size('3P'))
|
||||||
# str
|
# str
|
||||||
check('', struct.calcsize(vh + 'li') + 1)
|
vh = test.test_support._vheader
|
||||||
check('abc', struct.calcsize(vh + 'li') + 1 + 3*self.c)
|
check('', struct.calcsize(vh + 'lic'))
|
||||||
|
check('abc', struct.calcsize(vh + 'lic') + 3)
|
||||||
# super
|
# super
|
||||||
check(super(int), size(h + '3P'))
|
check(super(int), size('3P'))
|
||||||
# tuple
|
# tuple
|
||||||
check((), size(vh))
|
check((), vsize(''))
|
||||||
check((1,2,3), size(vh) + 3*self.P)
|
check((1,2,3), vsize('') + 3*self.P)
|
||||||
# tupleiterator
|
# tupleiterator
|
||||||
check(iter(()), size(h + 'lP'))
|
check(iter(()), size('lP'))
|
||||||
# type
|
# type
|
||||||
# (PyTypeObject + PyNumberMethods + PyMappingMethods +
|
# (PyTypeObject + PyNumberMethods + PyMappingMethods +
|
||||||
# PySequenceMethods + PyBufferProcs)
|
# PySequenceMethods + PyBufferProcs)
|
||||||
s = size(vh + 'P2P15Pl4PP9PP11PI') + size('41P 10P 3P 6P')
|
s = vsize('P2P15Pl4PP9PP11PI') + struct.calcsize('41P 10P 3P 6P')
|
||||||
class newstyleclass(object):
|
class newstyleclass(object):
|
||||||
pass
|
pass
|
||||||
check(newstyleclass, s)
|
check(newstyleclass, s)
|
||||||
|
@ -763,41 +731,40 @@ class SizeofTest(unittest.TestCase):
|
||||||
# we need to test for both sizes, because we don't know if the string
|
# we need to test for both sizes, because we don't know if the string
|
||||||
# has been cached
|
# has been cached
|
||||||
for s in samples:
|
for s in samples:
|
||||||
check(s, size(h + 'PPlP') + usize * (len(s) + 1))
|
check(s, size('PPlP') + usize * (len(s) + 1))
|
||||||
# weakref
|
# weakref
|
||||||
import weakref
|
import weakref
|
||||||
check(weakref.ref(int), size(h + '2Pl2P'))
|
check(weakref.ref(int), size('2Pl2P'))
|
||||||
# weakproxy
|
# weakproxy
|
||||||
# XXX
|
# XXX
|
||||||
# weakcallableproxy
|
# weakcallableproxy
|
||||||
check(weakref.proxy(int), size(h + '2Pl2P'))
|
check(weakref.proxy(int), size('2Pl2P'))
|
||||||
# xrange
|
# xrange
|
||||||
check(xrange(1), size(h + '3l'))
|
check(xrange(1), size('3l'))
|
||||||
check(xrange(66000), size(h + '3l'))
|
check(xrange(66000), size('3l'))
|
||||||
|
|
||||||
def test_pythontypes(self):
|
def test_pythontypes(self):
|
||||||
# check all types defined in Python/
|
# check all types defined in Python/
|
||||||
h = self.header
|
size = test.test_support.calcobjsize
|
||||||
vh = self.vheader
|
vsize = test.test_support.calcvobjsize
|
||||||
size = self.calcsize
|
|
||||||
check = self.check_sizeof
|
check = self.check_sizeof
|
||||||
# _ast.AST
|
# _ast.AST
|
||||||
import _ast
|
import _ast
|
||||||
check(_ast.AST(), size(h + ''))
|
check(_ast.AST(), size(''))
|
||||||
# imp.NullImporter
|
# imp.NullImporter
|
||||||
import imp
|
import imp
|
||||||
check(imp.NullImporter(self.file.name), size(h + ''))
|
check(imp.NullImporter(self.file.name), size(''))
|
||||||
try:
|
try:
|
||||||
raise TypeError
|
raise TypeError
|
||||||
except TypeError:
|
except TypeError:
|
||||||
tb = sys.exc_info()[2]
|
tb = sys.exc_info()[2]
|
||||||
# traceback
|
# traceback
|
||||||
if tb != None:
|
if tb != None:
|
||||||
check(tb, size(h + '2P2i'))
|
check(tb, size('2P2i'))
|
||||||
# symtable entry
|
# symtable entry
|
||||||
# XXX
|
# XXX
|
||||||
# sys.flags
|
# sys.flags
|
||||||
check(sys.flags, size(vh) + self.P * len(sys.flags))
|
check(sys.flags, vsize('') + self.P * len(sys.flags))
|
||||||
|
|
||||||
|
|
||||||
def test_main():
|
def test_main():
|
||||||
|
|
|
@ -887,6 +887,9 @@ Tools/Demos
|
||||||
Tests
|
Tests
|
||||||
-----
|
-----
|
||||||
|
|
||||||
|
- Issue #15467: Move helpers for __sizeof__ tests into test_support.
|
||||||
|
Patch by Serhiy Storchaka.
|
||||||
|
|
||||||
- Issue #11689: Fix a variable scoping error in an sqlite3 test.
|
- Issue #11689: Fix a variable scoping error in an sqlite3 test.
|
||||||
Initial patch by Torsten Landschoff.
|
Initial patch by Torsten Landschoff.
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue