summaryrefslogtreecommitdiffstats
path: root/Lib/test
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2015-12-19 18:07:11 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2015-12-19 18:07:11 (GMT)
commita9406e77fa7bd3618c16edd248c24010af7035c1 (patch)
tree885dac8e53616cebafbb37bc5613040513bd9581 /Lib/test
parenta254921cd4c1ca22d725872601292c48b7caa20a (diff)
parent5c4064e8bd199d70eefd7ec24766957c22f1b8e8 (diff)
downloadcpython-a9406e77fa7bd3618c16edd248c24010af7035c1.zip
cpython-a9406e77fa7bd3618c16edd248c24010af7035c1.tar.gz
cpython-a9406e77fa7bd3618c16edd248c24010af7035c1.tar.bz2
Issue #25421: __sizeof__ methods of builtin types now use dynamic basic size.
This allows sys.getsize() to work correctly with their subclasses with __slots__ defined.
Diffstat (limited to 'Lib/test')
-rw-r--r--Lib/test/test_sys.py30
1 files changed, 30 insertions, 0 deletions
diff --git a/Lib/test/test_sys.py b/Lib/test/test_sys.py
index 2d95653..4f1577d 100644
--- a/Lib/test/test_sys.py
+++ b/Lib/test/test_sys.py
@@ -1112,6 +1112,36 @@ class SizeofTest(unittest.TestCase):
# weakcallableproxy
check(weakref.proxy(int), size('2Pn2P'))
+ def check_slots(self, obj, base, extra):
+ expected = sys.getsizeof(base) + struct.calcsize(extra)
+ if gc.is_tracked(obj) and not gc.is_tracked(base):
+ expected += struct.calcsize('2Pn') # PyGC_Head
+ self.assertEqual(sys.getsizeof(obj), expected)
+
+ def test_slots(self):
+ # check all subclassable types defined in Objects/ that allow
+ # non-empty __slots__
+ check = self.check_slots
+ class BA(bytearray):
+ __slots__ = 'a', 'b', 'c'
+ check(BA(), bytearray(), '3P')
+ class D(dict):
+ __slots__ = 'a', 'b', 'c'
+ check(D(x=[]), {'x': []}, '3P')
+ class L(list):
+ __slots__ = 'a', 'b', 'c'
+ check(L(), [], '3P')
+ class S(set):
+ __slots__ = 'a', 'b', 'c'
+ check(S(), set(), '3P')
+ class FS(frozenset):
+ __slots__ = 'a', 'b', 'c'
+ check(FS(), frozenset(), '3P')
+ from collections import OrderedDict
+ class OD(OrderedDict):
+ __slots__ = 'a', 'b', 'c'
+ check(OD(x=[]), OrderedDict(x=[]), '3P')
+
def test_pythontypes(self):
# check all types defined in Python/
size = test.support.calcobjsize