Issue 3147: Fixed SizeofTest failure for LLP64 systems.

This commit is contained in:
Robert Schuppenies 2008-06-25 09:20:03 +00:00
parent aa164e1bd3
commit 41a7ce0a2e
1 changed files with 60 additions and 71 deletions

View File

@ -1,6 +1,7 @@
# -*- coding: iso-8859-1 -*- # -*- coding: iso-8859-1 -*-
import unittest, test.test_support import unittest, test.test_support
import sys, cStringIO, os import sys, cStringIO, os
import struct
class SysModuleTest(unittest.TestCase): class SysModuleTest(unittest.TestCase):
@ -408,13 +409,16 @@ class SysModuleTest(unittest.TestCase):
class SizeofTest(unittest.TestCase): class SizeofTest(unittest.TestCase):
def setUp(self): def setUp(self):
import struct self.c = len(struct.pack('c', ' '))
self.H = len(struct.pack('H', 0))
self.i = len(struct.pack('i', 0)) self.i = len(struct.pack('i', 0))
self.l = len(struct.pack('l', 0)) self.l = len(struct.pack('l', 0))
self.p = len(struct.pack('P', 0)) self.P = len(struct.pack('P', 0))
self.headersize = self.l + self.p # due to missing size_t information from struct, it is assumed that
# sizeof(Py_ssize_t) = sizeof(void*)
self.header = 'lP'
if hasattr(sys, "gettotalrefcount"): if hasattr(sys, "gettotalrefcount"):
self.headersize += 2 * self.p self.header += '2P'
self.file = open(test.test_support.TESTFN, 'wb') self.file = open(test.test_support.TESTFN, 'wb')
def tearDown(self): def tearDown(self):
@ -430,64 +434,53 @@ class SizeofTest(unittest.TestCase):
else: else:
self.assertEqual(result, size, msg + str(size)) self.assertEqual(result, size, msg + str(size))
def align(self, value): def calcsize(self, fmt):
mod = value % self.p """Wrapper around struct.calcsize which enforces the alignment of the
if mod != 0: end of a structure to the alignment requirement of pointer.
return value - mod + self.p
else:
return value
def test_align(self): Note: This wrapper should only be used if a pointer member is included
self.assertEqual(self.align(0) % self.p, 0) and no member with a size larger than a pointer exists.
self.assertEqual(self.align(1) % self.p, 0) """
self.assertEqual(self.align(3) % self.p, 0) return struct.calcsize(fmt + '0P')
self.assertEqual(self.align(4) % self.p, 0)
self.assertEqual(self.align(7) % self.p, 0)
self.assertEqual(self.align(8) % self.p, 0)
self.assertEqual(self.align(9) % self.p, 0)
def test_standardtypes(self): def test_standardtypes(self):
i = self.i h = self.header
l = self.l size = self.calcsize
p = self.p
h = self.headersize
# bool # bool
self.check_sizeof(True, h + l) self.check_sizeof(True, size(h + 'l'))
# buffer # buffer
self.check_sizeof(buffer(''), h + 2*p + 2*l + self.align(i) +l) self.check_sizeof(buffer(''), size(h + '2P2Pil'))
# cell # cell
def get_cell(): def get_cell():
x = 42 x = 42
def inner(): def inner():
return x return x
return inner return inner
self.check_sizeof(get_cell().func_closure[0], h + p) self.check_sizeof(get_cell().func_closure[0], size(h + 'P'))
# old-style class # old-style class
class class_oldstyle(): class class_oldstyle():
def method(): def method():
pass pass
self.check_sizeof(class_oldstyle, h + 6*p) self.check_sizeof(class_oldstyle, size(h + '6P'))
# instance # instance
self.check_sizeof(class_oldstyle(), h + 3*p) self.check_sizeof(class_oldstyle(), size(h + '3P'))
# method # method
self.check_sizeof(class_oldstyle().method, h + 4*p) self.check_sizeof(class_oldstyle().method, size(h + '4P'))
# code # code
self.check_sizeof(get_cell().func_code, h + self.align(4*i) + 8*p +\ self.check_sizeof(get_cell().func_code, size(h + '4i8Pi2P'))
self.align(i) + 2*p)
# complex # complex
self.check_sizeof(complex(0,1), h + 2*8) self.check_sizeof(complex(0,1), size(h + '2d'))
# enumerate # enumerate
self.check_sizeof(enumerate([]), h + l + 3*p) self.check_sizeof(enumerate([]), size(h + 'l3P'))
# reverse # reverse
self.check_sizeof(reversed(''), h + l + p ) self.check_sizeof(reversed(''), size(h + 'PP'))
# file # file
self.check_sizeof(self.file, h + 4*p + self.align(2*i) + 4*p +\ self.check_sizeof(self.file, size(h + '4P2i4P3i3Pi'))
self.align(3*i) + 3*p + self.align(i))
# float # float
self.check_sizeof(float(0), h + 8) self.check_sizeof(float(0), size(h + 'd'))
# function # function
def func(): pass def func(): pass
self.check_sizeof(func, h + 9 * l) self.check_sizeof(func, size(h + '9l'))
class c(): class c():
@staticmethod @staticmethod
def foo(): def foo():
@ -496,54 +489,50 @@ class SizeofTest(unittest.TestCase):
def bar(cls): def bar(cls):
pass pass
# staticmethod # staticmethod
self.check_sizeof(foo, h + l) self.check_sizeof(foo, size(h + 'l'))
# classmethod # classmethod
self.check_sizeof(bar, h + l) self.check_sizeof(bar, size(h + 'l'))
# generator # generator
def get_gen(): yield 1 def get_gen(): yield 1
self.check_sizeof(get_gen(), h + p + self.align(i) + 2*p) self.check_sizeof(get_gen(), size(h + 'Pi2P'))
# integer # integer
self.check_sizeof(1, h + l) self.check_sizeof(1, size(h + 'l'))
# builtin_function_or_method # builtin_function_or_method
self.check_sizeof(abs, h + 3*p) self.check_sizeof(abs, size(h + '3P'))
# module # module
self.check_sizeof(unittest, h + p) self.check_sizeof(unittest, size(h + 'P'))
# xrange # xrange
self.check_sizeof(xrange(1), h + 3*p) self.check_sizeof(xrange(1), size(h + '3P'))
# slice # slice
self.check_sizeof(slice(0), h + 3*p) self.check_sizeof(slice(0), size(h + '3P'))
h += l h += 'l'
# new-style class # new-style class
class class_newstyle(object): class class_newstyle(object):
def method(): def method():
pass pass
# type (PyTypeObject + PyNumberMethods + PyMappingMethods + # type (PyTypeObject + PyNumberMethods + PyMappingMethods +
# PySequenceMethods + PyBufferProcs) # PySequenceMethods + PyBufferProcs)
len_typeobject = p + 2*l + 15*p + l + 4*p + l + 9*p +\ self.check_sizeof(class_newstyle, size('P2P15Pl4PP9PP11PI') +\
l + 11*p + self.align(4) size(h + '41P 10P 3P 6P'))
self.check_sizeof(class_newstyle,
h + len_typeobject + 41*p + 10*p + 3*p + 6*p)
def test_specialtypes(self): def test_specialtypes(self):
i = self.i h = self.header
l = self.l size = self.calcsize
p = self.p
h = self.headersize
# dict # dict
self.check_sizeof({}, h + 3*l + 3*p + 8*(l + 2*p)) self.check_sizeof({}, size(h + '3P3P') + 8*size('P2P'))
longdict = {1:1, 2:2, 3:3, 4:4, 5:5, 6:6, 7:7, 8:8} longdict = {1:1, 2:2, 3:3, 4:4, 5:5, 6:6, 7:7, 8:8}
self.check_sizeof(longdict, h + 3*l + 3*p + 8*(l + 2*p) + 16*(l + 2*p)) self.check_sizeof(longdict, size(h + '3P3P') + (8+16)*size('P2P'))
# list # list
self.check_sizeof([], h + l + p + l) self.check_sizeof([], size(h + 'lPP'))
self.check_sizeof([1, 2, 3], h + l + p + l + 3*l) self.check_sizeof([1, 2, 3], size(h + 'lPP') + 3*self.P)
# unicode # unicode
usize = len(u'\0'.encode('unicode-internal')) usize = len(u'\0'.encode('unicode-internal'))
samples = [u'', u'1'*100] samples = [u'', u'1'*100]
# 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:
basicsize = h + l + p + l + p + usize * (len(s) + 1) basicsize = size(h + 'PPlP') + usize * (len(s) + 1)
self.check_sizeof(s, basicsize,\ self.check_sizeof(s, basicsize,\
size2=basicsize + sys.getsizeof(str(s))) size2=basicsize + sys.getsizeof(str(s)))
# XXX trigger caching encoded version as Python string # XXX trigger caching encoded version as Python string
@ -555,20 +544,20 @@ class SizeofTest(unittest.TestCase):
finally: finally:
self.check_sizeof(s, basicsize + sys.getsizeof(str(s))) self.check_sizeof(s, basicsize + sys.getsizeof(str(s)))
h += l h += 'l'
# long # long
self.check_sizeof(0L, h + self.align(2)) self.check_sizeof(0L, size(h + 'H'))
self.check_sizeof(1L, h + self.align(2)) self.check_sizeof(1L, size(h + 'H'))
self.check_sizeof(-1L, h + self.align(2)) self.check_sizeof(-1L, size(h + 'H'))
self.check_sizeof(32768L, h + self.align(2) + 2) self.check_sizeof(32768L, size(h + 'H') + self.H)
self.check_sizeof(32768L*32768L-1, h + self.align(2) + 2) self.check_sizeof(32768L*32768L-1, size(h + 'H') + self.H)
self.check_sizeof(32768L*32768L, h + self.align(2) + 4) self.check_sizeof(32768L*32768L, size(h + 'H') + 2*self.H)
# string # string
self.check_sizeof('', h + l + self.align(i + 1)) self.check_sizeof('', size(h + 'lic'))
self.check_sizeof('abc', h + l + self.align(i + 1) + 3) self.check_sizeof('abc', size(h + 'lic') + 3*self.c)
# tuple # tuple
self.check_sizeof((), h) self.check_sizeof((), size(h))
self.check_sizeof((1,2,3), h + 3*p) self.check_sizeof((1,2,3), size(h) + 3*self.P)
def test_main(): def test_main():