mirror of
https://github.com/python/cpython.git
synced 2025-09-16 13:47:31 +00:00
Closes #15512: Correct __sizeof__ support for parser
This commit is contained in:
parent
1fa9f7b3d1
commit
3e3192d8f7
5 changed files with 103 additions and 4 deletions
|
@ -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,
|
||||
)
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue