diff options
author | Jesus Cea <jcea@jcea.es> | 2012-08-03 12:29:26 (GMT) |
---|---|---|
committer | Jesus Cea <jcea@jcea.es> | 2012-08-03 12:29:26 (GMT) |
commit | 88ca04e6a8d1e5b31da69717e0e0cde2c069d7eb (patch) | |
tree | 5a7af12a79f62a6ee89b489c65ca8d143af74a0d /Lib/test/test_parser.py | |
parent | 5323173dee1f20fc53c2df4707ec6d266534b748 (diff) | |
parent | e9c5318967e1e62e940b72cd47502a1a3b559b95 (diff) | |
download | cpython-88ca04e6a8d1e5b31da69717e0e0cde2c069d7eb.zip cpython-88ca04e6a8d1e5b31da69717e0e0cde2c069d7eb.tar.gz cpython-88ca04e6a8d1e5b31da69717e0e0cde2c069d7eb.tar.bz2 |
MERGE: Closes #15512: Correct __sizeof__ support for parser
Diffstat (limited to 'Lib/test/test_parser.py')
-rw-r--r-- | Lib/test/test_parser.py | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/Lib/test/test_parser.py b/Lib/test/test_parser.py index e199728..52f278b 100644 --- a/Lib/test/test_parser.py +++ b/Lib/test/test_parser.py @@ -2,6 +2,7 @@ import parser import unittest import sys import operator +import struct from test import support # @@ -679,6 +680,46 @@ class STObjectTestCase(unittest.TestCase): self.assertRaises(TypeError, operator.lt, st1, 1815) self.assertRaises(TypeError, operator.gt, b'waterloo', st2) + check_sizeof = support.check_sizeof + + @support.cpython_only + def test_sizeof(self): + def XXXROUNDUP(n): + if n <= 1: + return n + if n <= 128: + return (n + 3) & ~3 + return 1 << (n - 1).bit_length() + + basesize = support.calcobjsize('Pii') + nodesize = struct.calcsize('hP3iP0h') + def sizeofchildren(node): + if node is None: + return 0 + res = 0 + hasstr = len(node) > 1 and isinstance(node[-1], str) + if hasstr: + res += len(node[-1]) + 1 + children = node[1:-1] if hasstr else node[1:] + if children: + res += XXXROUNDUP(len(children)) * nodesize + res1 = res + if children: + for child in children: + res += sizeofchildren(child) + return res + + def check_st_sizeof(st): + self.check_sizeof(st, basesize + nodesize + + sizeofchildren(st.totuple())) + + check_st_sizeof(parser.expr('2 + 3')) + check_st_sizeof(parser.expr('2 + 3 + 4')) + check_st_sizeof(parser.suite('x = 2 + 3')) + check_st_sizeof(parser.suite('')) + check_st_sizeof(parser.suite('# -*- coding: utf-8 -*-')) + check_st_sizeof(parser.expr('[' + '2,' * 1000 + ']')) + # XXX tests for pickling and unpickling of ST objects should go here |