summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_parser.py
diff options
context:
space:
mode:
authorJesus Cea <jcea@jcea.es>2012-08-03 12:25:53 (GMT)
committerJesus Cea <jcea@jcea.es>2012-08-03 12:25:53 (GMT)
commit3e3192d8f76ca6bfbf111ed2059eaef8e49a53e5 (patch)
tree6f6107e37955012a786fb83b30da9f38dd1212e2 /Lib/test/test_parser.py
parent1fa9f7b3d1d5c90cb231946a6edaf7f748e07436 (diff)
downloadcpython-3e3192d8f76ca6bfbf111ed2059eaef8e49a53e5.zip
cpython-3e3192d8f76ca6bfbf111ed2059eaef8e49a53e5.tar.gz
cpython-3e3192d8f76ca6bfbf111ed2059eaef8e49a53e5.tar.bz2
Closes #15512: Correct __sizeof__ support for parser
Diffstat (limited to 'Lib/test/test_parser.py')
-rw-r--r--Lib/test/test_parser.py52
1 files changed, 50 insertions, 2 deletions
diff --git a/Lib/test/test_parser.py b/Lib/test/test_parser.py
index 33b91bd..c6900de 100644
--- a/Lib/test/test_parser.py
+++ b/Lib/test/test_parser.py
@@ -1,7 +1,8 @@
import parser
import unittest
import sys
-from test import test_support
+import struct
+from test import test_support as support
#
# First, we test that we can generate trees from valid source fragments,
@@ -583,12 +584,59 @@ class ParserStackLimitTestCase(unittest.TestCase):
print >>sys.stderr, "Expecting 's_push: parser stack overflow' in next line"
self.assertRaises(MemoryError, parser.expr, e)
+class STObjectTestCase(unittest.TestCase):
+ """Test operations on ST objects themselves"""
+
+ 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
+
def test_main():
- test_support.run_unittest(
+ support.run_unittest(
RoundtripLegalSyntaxTestCase,
IllegalSyntaxTestCase,
CompileTestCase,
ParserStackLimitTestCase,
+ STObjectTestCase,
)