|
1 | 1 | # -*- coding: iso-8859-1 -*- |
2 | 2 | import unittest, test.test_support |
3 | 3 | from test.script_helper import assert_python_ok, assert_python_failure |
4 | | -import sys, os, cStringIO |
5 | | -import struct |
| 4 | +import cStringIO |
| 5 | +import gc |
6 | 6 | import operator |
| 7 | +import os |
| 8 | +import struct |
| 9 | +import sys |
7 | 10 |
|
8 | 11 | class SysModuleTest(unittest.TestCase): |
9 | 12 |
|
@@ -754,6 +757,32 @@ class newstyleclass(object): |
754 | 757 | check(xrange(1), size('3l')) |
755 | 758 | check(xrange(66000), size('3l')) |
756 | 759 |
|
| 760 | + def check_slots(self, obj, base, extra): |
| 761 | + expected = sys.getsizeof(base) + struct.calcsize(extra) |
| 762 | + if gc.is_tracked(obj) and not gc.is_tracked(base): |
| 763 | + expected += struct.calcsize('3P') # PyGC_Head |
| 764 | + self.assertEqual(sys.getsizeof(obj), expected) |
| 765 | + |
| 766 | + def test_slots(self): |
| 767 | + # check all subclassable types defined in Objects/ that allow |
| 768 | + # non-empty __slots__ |
| 769 | + check = self.check_slots |
| 770 | + class BA(bytearray): |
| 771 | + __slots__ = 'a', 'b', 'c' |
| 772 | + check(BA(), bytearray(), '3P') |
| 773 | + class D(dict): |
| 774 | + __slots__ = 'a', 'b', 'c' |
| 775 | + check(D(x=[]), {'x': []}, '3P') |
| 776 | + class L(list): |
| 777 | + __slots__ = 'a', 'b', 'c' |
| 778 | + check(L(), [], '3P') |
| 779 | + class S(set): |
| 780 | + __slots__ = 'a', 'b', 'c' |
| 781 | + check(S(), set(), '3P') |
| 782 | + class FS(frozenset): |
| 783 | + __slots__ = 'a', 'b', 'c' |
| 784 | + check(FS(), frozenset(), '3P') |
| 785 | + |
757 | 786 | def test_pythontypes(self): |
758 | 787 | # check all types defined in Python/ |
759 | 788 | size = test.test_support.calcobjsize |
|
0 commit comments