summaryrefslogtreecommitdiffstats
path: root/Lib/test/support.py
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/test/support.py
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/test/support.py')
-rw-r--r--Lib/test/support.py28
1 files changed, 28 insertions, 0 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.