Corrected inconsistencies in sizeof tests and addressed issue pointed

out by Jean Brouwers.
This commit is contained in:
Robert Schuppenies 2008-06-26 15:20:35 +00:00
parent 68dc052ca6
commit 161b92103c
2 changed files with 13 additions and 13 deletions

View File

@ -416,7 +416,7 @@ class SizeofTest(unittest.TestCase):
self.P = len(struct.pack('P', 0)) self.P = len(struct.pack('P', 0))
# due to missing size_t information from struct, it is assumed that # due to missing size_t information from struct, it is assumed that
# sizeof(Py_ssize_t) = sizeof(void*) # sizeof(Py_ssize_t) = sizeof(void*)
self.header = 'lP' self.header = 'PP'
if hasattr(sys, "gettotalrefcount"): if hasattr(sys, "gettotalrefcount"):
self.header += '2P' self.header += '2P'
self.file = open(test.test_support.TESTFN, 'wb') self.file = open(test.test_support.TESTFN, 'wb')
@ -480,7 +480,7 @@ class SizeofTest(unittest.TestCase):
self.check_sizeof(float(0), size(h + 'd')) self.check_sizeof(float(0), size(h + 'd'))
# function # function
def func(): pass def func(): pass
self.check_sizeof(func, size(h + '9l')) self.check_sizeof(func, size(h + '9P'))
class c(): class c():
@staticmethod @staticmethod
def foo(): def foo():
@ -489,9 +489,9 @@ class SizeofTest(unittest.TestCase):
def bar(cls): def bar(cls):
pass pass
# staticmethod # staticmethod
self.check_sizeof(foo, size(h + 'l')) self.check_sizeof(foo, size(h + 'P'))
# classmethod # classmethod
self.check_sizeof(bar, size(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(), size(h + 'Pi2P')) self.check_sizeof(get_gen(), size(h + 'Pi2P'))
@ -502,11 +502,11 @@ class SizeofTest(unittest.TestCase):
# module # module
self.check_sizeof(unittest, size(h + 'P')) self.check_sizeof(unittest, size(h + 'P'))
# xrange # xrange
self.check_sizeof(xrange(1), size(h + '3P')) self.check_sizeof(xrange(1), size(h + '3l'))
# slice # slice
self.check_sizeof(slice(0), size(h + '3P')) self.check_sizeof(slice(0), size(h + '3P'))
h += 'l' h += 'P'
# new-style class # new-style class
class class_newstyle(object): class class_newstyle(object):
def method(): def method():
@ -520,12 +520,9 @@ class SizeofTest(unittest.TestCase):
h = self.header h = self.header
size = self.calcsize size = self.calcsize
# dict # dict
self.check_sizeof({}, size(h + '3P3P') + 8*size('P2P')) 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, size(h + '3P3P') + (8+16)*size('P2P')) self.check_sizeof(longdict, size(h + '3P2P') + (8+16)*size('P2P'))
# list
self.check_sizeof([], size(h + 'lPP'))
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]
@ -544,7 +541,10 @@ 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 += '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(0L, size(h + 'H')) self.check_sizeof(0L, size(h + 'H'))
self.check_sizeof(1L, size(h + 'H')) self.check_sizeof(1L, size(h + 'H'))

View File

@ -2037,7 +2037,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 PyInt_FromSsize_t(res); return PyInt_FromSsize_t(res);