Merged revisions 64518,64521-64525,64528-64533 via svnmerge from

svn+ssh://pythondev@svn.python.org/python/trunk

........
  r64518 | robert.schuppenies | 2008-06-25 11:20:03 +0200 (Wed, 25 Jun 2008) | 2 lines

  Issue 3147: Fixed SizeofTest failure for LLP64 systems.
........
  r64533 | robert.schuppenies | 2008-06-26 17:20:35 +0200 (Thu, 26 Jun 2008) | 3 lines

  Corrected inconsistencies in sizeof tests and addressed issue pointed
  out by Jean Brouwers.
........
This commit is contained in:
Robert Schuppenies 2008-06-28 23:58:47 +00:00
parent 42806bad9b
commit 4d45bfe4c5
2 changed files with 54 additions and 72 deletions

View File

@ -1,6 +1,7 @@
# -*- coding: iso-8859-1 -*- # -*- coding: iso-8859-1 -*-
import unittest, test.support import unittest, test.support
import sys, io, os import sys, io, os
import struct
class SysModuleTest(unittest.TestCase): class SysModuleTest(unittest.TestCase):
@ -373,13 +374,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 = 'PP'
if hasattr(sys, "gettotalrefcount"): if hasattr(sys, "gettotalrefcount"):
self.headersize += 2 * self.p self.header += '2P'
self.file = open(test.support.TESTFN, 'wb') self.file = open(test.support.TESTFN, 'wb')
def tearDown(self): def tearDown(self):
@ -395,52 +399,38 @@ 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
self.check_sizeof(True, h + 2*l)
# bytearray
self.check_sizeof(bytes(), h + self.align(i) + l + p)
# 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().__closure__[0], h + p) self.check_sizeof(get_cell().__closure__[0], size(h + 'P'))
# code # code
self.check_sizeof(get_cell().__code__, h + self.align(5*i) + 8*p +\ self.check_sizeof(get_cell().__code__, size(h + '5i8Pi2P'))
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'))
# 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 + 11 * p) self.check_sizeof(func, size(h + '11P'))
class c(): class c():
@staticmethod @staticmethod
def foo(): def foo():
@ -449,58 +439,47 @@ 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 + 'P'))
# classmethod # classmethod
self.check_sizeof(bar, h + l) self.check_sizeof(bar, size(h + 'P'))
# 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'))
# 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 + 3*p) self.check_sizeof(unittest, size(h + '3P'))
# range # range
self.check_sizeof(range(1), h + 3*p) self.check_sizeof(range(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 += 'P'
# bool
self.check_sizeof(True, size(h + 'H'))
# 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)
self.check_sizeof(class_newstyle, h + self.check_sizeof(class_newstyle, size(h + 'P2P15Pl4PP9PP11PI') +\
# PyTypeObject size('16Pi17P 3P 10P 2P 2P'))
p + 2*l + 15*p + l + 4*p + l + 9*p + l + 11*p +
self.align(4) +
# PyNumberMethods
16*p + self.align(i) + 17*p +
3*p + # PyMappingMethods
10*p + # PySequenceMethods
2*p + # PyBufferProcs
2*p) # *ht_name and *ht_slots
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 + '3P2P') + 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 + '3P2P') + (8+16)*size('P2P'))
# list
self.check_sizeof([], h + l + p + l)
self.check_sizeof([1, 2, 3], h + l + p + l + 3*l)
# unicode # unicode
usize = len('\0'.encode('unicode-internal')) usize = len('\0'.encode('unicode-internal'))
samples = ['', '1'*100] samples = ['', '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 + l + p + usize * (len(s) + 1) basicsize = size(h + 'PPliP') + usize * (len(s) + 1)
defenc = bytes(s, 'ascii') defenc = bytes(s, 'ascii')
self.check_sizeof(s, basicsize, self.check_sizeof(s, basicsize,
size2=basicsize + sys.getsizeof(defenc)) size2=basicsize + sys.getsizeof(defenc))
@ -512,17 +491,20 @@ class SizeofTest(unittest.TestCase):
finally: finally:
self.check_sizeof(s, basicsize + sys.getsizeof(defenc)) self.check_sizeof(s, basicsize + sys.getsizeof(defenc))
h += l h += 'P'
# list
self.check_sizeof([], size(h + 'PP'))
self.check_sizeof([1, 2, 3], size(h + 'PP') + 3*self.P)
# long # long
self.check_sizeof(0, h + self.align(2)) self.check_sizeof(0, size(h + 'H'))
self.check_sizeof(1, h + self.align(2)) self.check_sizeof(1, size(h + 'H'))
self.check_sizeof(-1, h + self.align(2)) self.check_sizeof(-1, size(h + 'H'))
self.check_sizeof(32768, h + self.align(2) + 2) self.check_sizeof(32768, size(h + 'H') + self.H)
self.check_sizeof(32768*32768-1, h + self.align(2) + 2) self.check_sizeof(32768*32768-1, size(h + 'H') + self.H)
self.check_sizeof(32768*32768, h + self.align(2) + 4) self.check_sizeof(32768*32768, size(h + 'H') + 2*self.H)
# 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():

View File

@ -1845,7 +1845,7 @@ dict_sizeof(PyDictObject *mp)
{ {
Py_ssize_t res; Py_ssize_t res;
res = sizeof(PyDictObject) + sizeof(mp->ma_table); res = sizeof(PyDictObject);
if (mp->ma_table != mp->ma_smalltable) if (mp->ma_table != mp->ma_smalltable)
res = res + (mp->ma_mask + 1) * sizeof(PyDictEntry); res = res + (mp->ma_mask + 1) * sizeof(PyDictEntry);
return PyLong_FromSsize_t(res); return PyLong_FromSsize_t(res);