summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorJesus Cea <jcea@jcea.es>2012-08-03 12:28:37 (GMT)
committerJesus Cea <jcea@jcea.es>2012-08-03 12:28:37 (GMT)
commite9c5318967e1e62e940b72cd47502a1a3b559b95 (patch)
tree34c525b3471a0d8f9ae478c4e4e78142f9081ca4 /Lib
parenta9a53c7dc055b54133f2dee33f1834d7566de842 (diff)
downloadcpython-e9c5318967e1e62e940b72cd47502a1a3b559b95.zip
cpython-e9c5318967e1e62e940b72cd47502a1a3b559b95.tar.gz
cpython-e9c5318967e1e62e940b72cd47502a1a3b559b95.tar.bz2
Closes #15512: Correct __sizeof__ support for parser
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/test_parser.py41
1 files changed, 41 insertions, 0 deletions
diff --git a/Lib/test/test_parser.py b/Lib/test/test_parser.py
index cae09df..f11c92e 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
#
@@ -675,6 +676,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