Mercurial > cpython-lru_cache-weakref
diff Lib/test/support.py @ 78315:efade142ef01 3.2
Issue #15467: Move helpers for __sizeof__ tests into test_support.
Patch by Serhiy Storchaka.
| author | Martin v. Löwis <martin@v.loewis.de> |
|---|---|
| date | Sun, 29 Jul 2012 16:33:05 +0200 |
| parents | f23222d734bd |
| children | fcad4566910b d43ff8eb4cb3 |
line wrap: on
line diff
--- a/Lib/test/support.py Sat Jul 28 22:16:39 2012 -0500 +++ b/Lib/test/support.py Sun Jul 29 16:33:05 2012 +0200 @@ -23,6 +23,9 @@ import sysconfig import fnmatch import logging.handlers +import struct +import tempfile +import _testcapi try: import _thread, threading @@ -984,6 +987,31 @@ 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.
