bpo-36287: Make ast.dump() not output optional fields and attributes with default values. (GH-18843)

The default values for optional fields and attributes of AST nodes are now set
as class attributes (e.g. Constant.kind is set to None).
This commit is contained in:
Serhiy Storchaka 2020-03-10 00:07:47 +02:00 committed by GitHub
parent 85f5a69ae1
commit b7e9525f9c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 226 additions and 134 deletions

View File

@ -141,7 +141,7 @@ Literals
>>> print(ast.dump(ast.parse('123', mode='eval'), indent=4)) >>> print(ast.dump(ast.parse('123', mode='eval'), indent=4))
Expression( Expression(
body=Constant(value=123, kind=None)) body=Constant(value=123))
.. class:: FormattedValue(value, conversion, format_spec) .. class:: FormattedValue(value, conversion, format_spec)
@ -175,12 +175,11 @@ Literals
Expression( Expression(
body=JoinedStr( body=JoinedStr(
values=[ values=[
Constant(value='sin(', kind=None), Constant(value='sin('),
FormattedValue( FormattedValue(
value=Name(id='a', ctx=Load()), value=Name(id='a', ctx=Load()),
conversion=-1, conversion=-1),
format_spec=None), Constant(value=') is '),
Constant(value=') is ', kind=None),
FormattedValue( FormattedValue(
value=Call( value=Call(
func=Name(id='sin', ctx=Load()), func=Name(id='sin', ctx=Load()),
@ -190,7 +189,7 @@ Literals
conversion=-1, conversion=-1,
format_spec=JoinedStr( format_spec=JoinedStr(
values=[ values=[
Constant(value='.3', kind=None)]))])) Constant(value='.3')]))]))
.. class:: List(elts, ctx) .. class:: List(elts, ctx)
@ -206,17 +205,17 @@ Literals
Expression( Expression(
body=List( body=List(
elts=[ elts=[
Constant(value=1, kind=None), Constant(value=1),
Constant(value=2, kind=None), Constant(value=2),
Constant(value=3, kind=None)], Constant(value=3)],
ctx=Load())) ctx=Load()))
>>> print(ast.dump(ast.parse('(1, 2, 3)', mode='eval'), indent=4)) >>> print(ast.dump(ast.parse('(1, 2, 3)', mode='eval'), indent=4))
Expression( Expression(
body=Tuple( body=Tuple(
elts=[ elts=[
Constant(value=1, kind=None), Constant(value=1),
Constant(value=2, kind=None), Constant(value=2),
Constant(value=3, kind=None)], Constant(value=3)],
ctx=Load())) ctx=Load()))
@ -230,9 +229,9 @@ Literals
Expression( Expression(
body=Set( body=Set(
elts=[ elts=[
Constant(value=1, kind=None), Constant(value=1),
Constant(value=2, kind=None), Constant(value=2),
Constant(value=3, kind=None)])) Constant(value=3)]))
.. class:: Dict(keys, values) .. class:: Dict(keys, values)
@ -251,10 +250,10 @@ Literals
Expression( Expression(
body=Dict( body=Dict(
keys=[ keys=[
Constant(value='a', kind=None), Constant(value='a'),
None], None],
values=[ values=[
Constant(value=1, kind=None), Constant(value=1),
Name(id='d', ctx=Load())])) Name(id='d', ctx=Load())]))
@ -290,8 +289,7 @@ Variables
Assign( Assign(
targets=[ targets=[
Name(id='a', ctx=Store())], Name(id='a', ctx=Store())],
value=Constant(value=1, kind=None), value=Constant(value=1))],
type_comment=None)],
type_ignores=[]) type_ignores=[])
>>> print(ast.dump(ast.parse('del a'), indent=4)) >>> print(ast.dump(ast.parse('del a'), indent=4))
@ -323,8 +321,7 @@ Variables
value=Name(id='b', ctx=Store()), value=Name(id='b', ctx=Store()),
ctx=Store())], ctx=Store())],
ctx=Store())], ctx=Store())],
value=Name(id='it', ctx=Load()), value=Name(id='it', ctx=Load()))],
type_comment=None)],
type_ignores=[]) type_ignores=[])
@ -442,13 +439,13 @@ Expressions
>>> print(ast.dump(ast.parse('1 <= a < 10', mode='eval'), indent=4)) >>> print(ast.dump(ast.parse('1 <= a < 10', mode='eval'), indent=4))
Expression( Expression(
body=Compare( body=Compare(
left=Constant(value=1, kind=None), left=Constant(value=1),
ops=[ ops=[
LtE(), LtE(),
Lt()], Lt()],
comparators=[ comparators=[
Name(id='a', ctx=Load()), Name(id='a', ctx=Load()),
Constant(value=10, kind=None)])) Constant(value=10)]))
.. class:: Eq .. class:: Eq
@ -493,7 +490,6 @@ Expressions
arg='b', arg='b',
value=Name(id='c', ctx=Load())), value=Name(id='c', ctx=Load())),
keyword( keyword(
arg=None,
value=Name(id='e', ctx=Load()))])) value=Name(id='e', ctx=Load()))]))
@ -548,7 +544,7 @@ Expressions
Expression( Expression(
body=NamedExpr( body=NamedExpr(
target=Name(id='x', ctx=Store()), target=Name(id='x', ctx=Store()),
value=Constant(value=4, kind=None))) value=Constant(value=4)))
Subscripting Subscripting
@ -573,7 +569,7 @@ Subscripting
body=Subscript( body=Subscript(
value=Name(id='l', ctx=Load()), value=Name(id='l', ctx=Load()),
slice=Index( slice=Index(
value=Constant(value=1, kind=None)), value=Constant(value=1)),
ctx=Load())) ctx=Load()))
@ -588,9 +584,8 @@ Subscripting
body=Subscript( body=Subscript(
value=Name(id='l', ctx=Load()), value=Name(id='l', ctx=Load()),
slice=Slice( slice=Slice(
lower=Constant(value=1, kind=None), lower=Constant(value=1),
upper=Constant(value=2, kind=None), upper=Constant(value=2)),
step=None),
ctx=Load())) ctx=Load()))
@ -608,11 +603,10 @@ Subscripting
slice=ExtSlice( slice=ExtSlice(
dims=[ dims=[
Slice( Slice(
lower=Constant(value=1, kind=None), lower=Constant(value=1),
upper=Constant(value=2, kind=None), upper=Constant(value=2)),
step=None),
Index( Index(
value=Constant(value=3, kind=None))]), value=Constant(value=3))]),
ctx=Load())) ctx=Load()))
@ -649,7 +643,7 @@ Comprehensions
value=BinOp( value=BinOp(
left=Name(id='x', ctx=Load()), left=Name(id='x', ctx=Load()),
op=Pow(), op=Pow(),
right=Constant(value=2, kind=None)), right=Constant(value=2)),
generators=[ generators=[
comprehension( comprehension(
target=Name(id='x', ctx=Store()), target=Name(id='x', ctx=Store()),
@ -708,7 +702,7 @@ Comprehensions
elt=BinOp( elt=BinOp(
left=Name(id='n', ctx=Load()), left=Name(id='n', ctx=Load()),
op=Pow(), op=Pow(),
right=Constant(value=2, kind=None)), right=Constant(value=2)),
generators=[ generators=[
comprehension( comprehension(
target=Name(id='n', ctx=Store()), target=Name(id='n', ctx=Store()),
@ -719,13 +713,13 @@ Comprehensions
ops=[ ops=[
Gt()], Gt()],
comparators=[ comparators=[
Constant(value=5, kind=None)]), Constant(value=5)]),
Compare( Compare(
left=Name(id='n', ctx=Load()), left=Name(id='n', ctx=Load()),
ops=[ ops=[
Lt()], Lt()],
comparators=[ comparators=[
Constant(value=10, kind=None)])], Constant(value=10)])],
is_async=0)])) is_async=0)]))
>>> print(ast.dump(ast.parse('[i async for i in soc]', mode='eval'), >>> print(ast.dump(ast.parse('[i async for i in soc]', mode='eval'),
@ -764,8 +758,7 @@ Statements
targets=[ targets=[
Name(id='a', ctx=Store()), Name(id='a', ctx=Store()),
Name(id='b', ctx=Store())], Name(id='b', ctx=Store())],
value=Constant(value=1, kind=None), value=Constant(value=1))],
type_comment=None)],
type_ignores=[]) type_ignores=[])
>>> print(ast.dump(ast.parse('a,b = c'), indent=4)) # Unpacking >>> print(ast.dump(ast.parse('a,b = c'), indent=4)) # Unpacking
@ -778,8 +771,7 @@ Statements
Name(id='a', ctx=Store()), Name(id='a', ctx=Store()),
Name(id='b', ctx=Store())], Name(id='b', ctx=Store())],
ctx=Store())], ctx=Store())],
value=Name(id='c', ctx=Load()), value=Name(id='c', ctx=Load()))],
type_comment=None)],
type_ignores=[]) type_ignores=[])
@ -800,7 +792,6 @@ Statements
AnnAssign( AnnAssign(
target=Name(id='c', ctx=Store()), target=Name(id='c', ctx=Store()),
annotation=Name(id='int', ctx=Load()), annotation=Name(id='int', ctx=Load()),
value=None,
simple=1)], simple=1)],
type_ignores=[]) type_ignores=[])
@ -810,7 +801,7 @@ Statements
AnnAssign( AnnAssign(
target=Name(id='a', ctx=Store()), target=Name(id='a', ctx=Store()),
annotation=Name(id='int', ctx=Load()), annotation=Name(id='int', ctx=Load()),
value=Constant(value=1, kind=None), value=Constant(value=1),
simple=0)], simple=0)],
type_ignores=[]) type_ignores=[])
@ -823,7 +814,6 @@ Statements
attr='b', attr='b',
ctx=Store()), ctx=Store()),
annotation=Name(id='int', ctx=Load()), annotation=Name(id='int', ctx=Load()),
value=None,
simple=0)], simple=0)],
type_ignores=[]) type_ignores=[])
@ -834,10 +824,9 @@ Statements
target=Subscript( target=Subscript(
value=Name(id='a', ctx=Load()), value=Name(id='a', ctx=Load()),
slice=Index( slice=Index(
value=Constant(value=1, kind=None)), value=Constant(value=1)),
ctx=Store()), ctx=Store()),
annotation=Name(id='int', ctx=Load()), annotation=Name(id='int', ctx=Load()),
value=None,
simple=0)], simple=0)],
type_ignores=[]) type_ignores=[])
@ -860,7 +849,7 @@ Statements
AugAssign( AugAssign(
target=Name(id='x', ctx=Store()), target=Name(id='x', ctx=Store()),
op=Add(), op=Add(),
value=Constant(value=2, kind=None))], value=Constant(value=2))],
type_ignores=[]) type_ignores=[])
@ -945,9 +934,9 @@ Imports
body=[ body=[
Import( Import(
names=[ names=[
alias(name='x', asname=None), alias(name='x'),
alias(name='y', asname=None), alias(name='y'),
alias(name='z', asname=None)])], alias(name='z')])],
type_ignores=[]) type_ignores=[])
@ -966,9 +955,9 @@ Imports
ImportFrom( ImportFrom(
module='y', module='y',
names=[ names=[
alias(name='x', asname=None), alias(name='x'),
alias(name='y', asname=None), alias(name='y'),
alias(name='z', asname=None)], alias(name='z')],
level=0)], level=0)],
type_ignores=[]) type_ignores=[])
@ -987,7 +976,7 @@ Imports
module='foo.bar', module='foo.bar',
names=[ names=[
alias(name='a', asname='b'), alias(name='a', asname='b'),
alias(name='c', asname=None)], alias(name='c')],
level=2)], level=2)],
type_ignores=[]) type_ignores=[])
@ -1023,16 +1012,16 @@ Control flow
test=Name(id='x', ctx=Load()), test=Name(id='x', ctx=Load()),
body=[ body=[
Expr( Expr(
value=Constant(value=Ellipsis, kind=None))], value=Constant(value=Ellipsis))],
orelse=[ orelse=[
If( If(
test=Name(id='y', ctx=Load()), test=Name(id='y', ctx=Load()),
body=[ body=[
Expr( Expr(
value=Constant(value=Ellipsis, kind=None))], value=Constant(value=Ellipsis))],
orelse=[ orelse=[
Expr( Expr(
value=Constant(value=Ellipsis, kind=None))])])], value=Constant(value=Ellipsis))])])],
type_ignores=[]) type_ignores=[])
@ -1063,11 +1052,10 @@ Control flow
iter=Name(id='y', ctx=Load()), iter=Name(id='y', ctx=Load()),
body=[ body=[
Expr( Expr(
value=Constant(value=Ellipsis, kind=None))], value=Constant(value=Ellipsis))],
orelse=[ orelse=[
Expr( Expr(
value=Constant(value=Ellipsis, kind=None))], value=Constant(value=Ellipsis))])],
type_comment=None)],
type_ignores=[]) type_ignores=[])
@ -1090,10 +1078,10 @@ Control flow
test=Name(id='x', ctx=Load()), test=Name(id='x', ctx=Load()),
body=[ body=[
Expr( Expr(
value=Constant(value=Ellipsis, kind=None))], value=Constant(value=Ellipsis))],
orelse=[ orelse=[
Expr( Expr(
value=Constant(value=Ellipsis, kind=None))])], value=Constant(value=Ellipsis))])],
type_ignores=[]) type_ignores=[])
@ -1124,13 +1112,12 @@ Control flow
ops=[ ops=[
Gt()], Gt()],
comparators=[ comparators=[
Constant(value=5, kind=None)]), Constant(value=5)]),
body=[ body=[
Break()], Break()],
orelse=[ orelse=[
Continue()])], Continue()])],
orelse=[], orelse=[])],
type_comment=None)],
type_ignores=[]) type_ignores=[])
@ -1158,26 +1145,25 @@ Control flow
Try( Try(
body=[ body=[
Expr( Expr(
value=Constant(value=Ellipsis, kind=None))], value=Constant(value=Ellipsis))],
handlers=[ handlers=[
ExceptHandler( ExceptHandler(
type=Name(id='Exception', ctx=Load()), type=Name(id='Exception', ctx=Load()),
name=None,
body=[ body=[
Expr( Expr(
value=Constant(value=Ellipsis, kind=None))]), value=Constant(value=Ellipsis))]),
ExceptHandler( ExceptHandler(
type=Name(id='OtherException', ctx=Load()), type=Name(id='OtherException', ctx=Load()),
name='e', name='e',
body=[ body=[
Expr( Expr(
value=Constant(value=Ellipsis, kind=None))])], value=Constant(value=Ellipsis))])],
orelse=[ orelse=[
Expr( Expr(
value=Constant(value=Ellipsis, kind=None))], value=Constant(value=Ellipsis))],
finalbody=[ finalbody=[
Expr( Expr(
value=Constant(value=Ellipsis, kind=None))])], value=Constant(value=Ellipsis))])],
type_ignores=[]) type_ignores=[])
@ -1204,11 +1190,10 @@ Control flow
value=BinOp( value=BinOp(
left=Name(id='a', ctx=Load()), left=Name(id='a', ctx=Load()),
op=Add(), op=Add(),
right=Constant(value=1, kind=None)))], right=Constant(value=1)))],
handlers=[ handlers=[
ExceptHandler( ExceptHandler(
type=Name(id='TypeError', ctx=Load()), type=Name(id='TypeError', ctx=Load()),
name=None,
body=[ body=[
Pass()])], Pass()])],
orelse=[], orelse=[],
@ -1256,8 +1241,7 @@ Control flow
args=[ args=[
Name(id='b', ctx=Load()), Name(id='b', ctx=Load()),
Name(id='d', ctx=Load())], Name(id='d', ctx=Load())],
keywords=[]))], keywords=[]))])],
type_comment=None)],
type_ignores=[]) type_ignores=[])
@ -1295,14 +1279,12 @@ Function and class definitions
args=arguments( args=arguments(
posonlyargs=[], posonlyargs=[],
args=[ args=[
arg(arg='x', annotation=None, type_comment=None), arg(arg='x'),
arg(arg='y', annotation=None, type_comment=None)], arg(arg='y')],
vararg=None,
kwonlyargs=[], kwonlyargs=[],
kw_defaults=[], kw_defaults=[],
kwarg=None,
defaults=[]), defaults=[]),
body=Constant(value=Ellipsis, kind=None)))], body=Constant(value=Ellipsis)))],
type_ignores=[]) type_ignores=[])
@ -1347,28 +1329,26 @@ Function and class definitions
args=[ args=[
arg( arg(
arg='a', arg='a',
annotation=Constant(value='annotation', kind=None), annotation=Constant(value='annotation')),
type_comment=None), arg(arg='b'),
arg(arg='b', annotation=None, type_comment=None), arg(arg='c')],
arg(arg='c', annotation=None, type_comment=None)], vararg=arg(arg='d'),
vararg=arg(arg='d', annotation=None, type_comment=None),
kwonlyargs=[ kwonlyargs=[
arg(arg='e', annotation=None, type_comment=None), arg(arg='e'),
arg(arg='f', annotation=None, type_comment=None)], arg(arg='f')],
kw_defaults=[ kw_defaults=[
None, None,
Constant(value=3, kind=None)], Constant(value=3)],
kwarg=arg(arg='g', annotation=None, type_comment=None), kwarg=arg(arg='g'),
defaults=[ defaults=[
Constant(value=1, kind=None), Constant(value=1),
Constant(value=2, kind=None)]), Constant(value=2)]),
body=[ body=[
Pass()], Pass()],
decorator_list=[ decorator_list=[
Name(id='decorator1', ctx=Load()), Name(id='decorator1', ctx=Load()),
Name(id='decorator2', ctx=Load())], Name(id='decorator2', ctx=Load())],
returns=Constant(value='return annotation', kind=None), returns=Constant(value='return annotation'))],
type_comment=None)],
type_ignores=[]) type_ignores=[])
@ -1382,7 +1362,7 @@ Function and class definitions
Module( Module(
body=[ body=[
Return( Return(
value=Constant(value=4, kind=None))], value=Constant(value=4))],
type_ignores=[]) type_ignores=[])
@ -1508,10 +1488,8 @@ Async and await
args=arguments( args=arguments(
posonlyargs=[], posonlyargs=[],
args=[], args=[],
vararg=None,
kwonlyargs=[], kwonlyargs=[],
kw_defaults=[], kw_defaults=[],
kwarg=None,
defaults=[]), defaults=[]),
body=[ body=[
Expr( Expr(
@ -1520,9 +1498,7 @@ Async and await
func=Name(id='other_func', ctx=Load()), func=Name(id='other_func', ctx=Load()),
args=[], args=[],
keywords=[])))], keywords=[])))],
decorator_list=[], decorator_list=[])],
returns=None,
type_comment=None)],
type_ignores=[]) type_ignores=[])

View File

@ -123,31 +123,36 @@ def dump(node, annotate_fields=True, include_attributes=False, *, indent=None):
prefix = '' prefix = ''
sep = ', ' sep = ', '
if isinstance(node, AST): if isinstance(node, AST):
cls = type(node)
args = [] args = []
allsimple = True allsimple = True
keywords = annotate_fields keywords = annotate_fields
for field in node._fields: for name in node._fields:
try: try:
value = getattr(node, field) value = getattr(node, name)
except AttributeError: except AttributeError:
keywords = True keywords = True
continue
if value is None and getattr(cls, name, ...) is None:
keywords = True
continue
value, simple = _format(value, level)
allsimple = allsimple and simple
if keywords:
args.append('%s=%s' % (name, value))
else: else:
args.append(value)
if include_attributes and node._attributes:
for name in node._attributes:
try:
value = getattr(node, name)
except AttributeError:
continue
if value is None and getattr(cls, name, ...) is None:
continue
value, simple = _format(value, level) value, simple = _format(value, level)
allsimple = allsimple and simple allsimple = allsimple and simple
if keywords: args.append('%s=%s' % (name, value))
args.append('%s=%s' % (field, value))
else:
args.append(value)
if include_attributes and node._attributes:
for attr in node._attributes:
try:
value = getattr(node, attr)
except AttributeError:
pass
else:
value, simple = _format(value, level)
allsimple = allsimple and simple
args.append('%s=%s' % (attr, value))
if allsimple and len(args) <= 3: if allsimple and len(args) <= 3:
return '%s(%s)' % (node.__class__.__name__, ', '.join(args)), not args return '%s(%s)' % (node.__class__.__name__, ', '.join(args)), not args
return '%s(%s%s)' % (node.__class__.__name__, prefix, sep.join(args)), False return '%s(%s%s)' % (node.__class__.__name__, prefix, sep.join(args)), False
@ -170,9 +175,10 @@ def copy_location(new_node, old_node):
attributes) from *old_node* to *new_node* if possible, and return *new_node*. attributes) from *old_node* to *new_node* if possible, and return *new_node*.
""" """
for attr in 'lineno', 'col_offset', 'end_lineno', 'end_col_offset': for attr in 'lineno', 'col_offset', 'end_lineno', 'end_col_offset':
if attr in old_node._attributes and attr in new_node._attributes \ if attr in old_node._attributes and attr in new_node._attributes:
and hasattr(old_node, attr): value = getattr(old_node, attr, None)
setattr(new_node, attr, getattr(old_node, attr)) if value is not None:
setattr(new_node, attr, value)
return new_node return new_node
@ -191,7 +197,7 @@ def fix_missing_locations(node):
else: else:
lineno = node.lineno lineno = node.lineno
if 'end_lineno' in node._attributes: if 'end_lineno' in node._attributes:
if not hasattr(node, 'end_lineno'): if getattr(node, 'end_lineno', None) is None:
node.end_lineno = end_lineno node.end_lineno = end_lineno
else: else:
end_lineno = node.end_lineno end_lineno = node.end_lineno
@ -201,7 +207,7 @@ def fix_missing_locations(node):
else: else:
col_offset = node.col_offset col_offset = node.col_offset
if 'end_col_offset' in node._attributes: if 'end_col_offset' in node._attributes:
if not hasattr(node, 'end_col_offset'): if getattr(node, 'end_col_offset', None) is None:
node.end_col_offset = end_col_offset node.end_col_offset = end_col_offset
else: else:
end_col_offset = node.end_col_offset end_col_offset = node.end_col_offset

View File

@ -353,9 +353,11 @@ class AST_Tests(unittest.TestCase):
'kw_defaults', 'kwarg', 'defaults')) 'kw_defaults', 'kwarg', 'defaults'))
with self.assertRaises(AttributeError): with self.assertRaises(AttributeError):
x.vararg x.args
self.assertIsNone(x.vararg)
x = ast.arguments(*range(1, 8)) x = ast.arguments(*range(1, 8))
self.assertEqual(x.args, 2)
self.assertEqual(x.vararg, 3) self.assertEqual(x.vararg, 3)
def test_field_attr_writable(self): def test_field_attr_writable(self):
@ -650,18 +652,18 @@ class ASTHelpers_Test(unittest.TestCase):
node = ast.parse('spam(eggs, "and cheese")') node = ast.parse('spam(eggs, "and cheese")')
self.assertEqual(ast.dump(node), self.assertEqual(ast.dump(node),
"Module(body=[Expr(value=Call(func=Name(id='spam', ctx=Load()), " "Module(body=[Expr(value=Call(func=Name(id='spam', ctx=Load()), "
"args=[Name(id='eggs', ctx=Load()), Constant(value='and cheese', kind=None)], " "args=[Name(id='eggs', ctx=Load()), Constant(value='and cheese')], "
"keywords=[]))], type_ignores=[])" "keywords=[]))], type_ignores=[])"
) )
self.assertEqual(ast.dump(node, annotate_fields=False), self.assertEqual(ast.dump(node, annotate_fields=False),
"Module([Expr(Call(Name('spam', Load()), [Name('eggs', Load()), " "Module([Expr(Call(Name('spam', Load()), [Name('eggs', Load()), "
"Constant('and cheese', None)], []))], [])" "Constant('and cheese')], []))], [])"
) )
self.assertEqual(ast.dump(node, include_attributes=True), self.assertEqual(ast.dump(node, include_attributes=True),
"Module(body=[Expr(value=Call(func=Name(id='spam', ctx=Load(), " "Module(body=[Expr(value=Call(func=Name(id='spam', ctx=Load(), "
"lineno=1, col_offset=0, end_lineno=1, end_col_offset=4), " "lineno=1, col_offset=0, end_lineno=1, end_col_offset=4), "
"args=[Name(id='eggs', ctx=Load(), lineno=1, col_offset=5, " "args=[Name(id='eggs', ctx=Load(), lineno=1, col_offset=5, "
"end_lineno=1, end_col_offset=9), Constant(value='and cheese', kind=None, " "end_lineno=1, end_col_offset=9), Constant(value='and cheese', "
"lineno=1, col_offset=11, end_lineno=1, end_col_offset=23)], keywords=[], " "lineno=1, col_offset=11, end_lineno=1, end_col_offset=23)], keywords=[], "
"lineno=1, col_offset=0, end_lineno=1, end_col_offset=24), " "lineno=1, col_offset=0, end_lineno=1, end_col_offset=24), "
"lineno=1, col_offset=0, end_lineno=1, end_col_offset=24)], type_ignores=[])" "lineno=1, col_offset=0, end_lineno=1, end_col_offset=24)], type_ignores=[])"
@ -677,7 +679,7 @@ Module(
func=Name(id='spam', ctx=Load()), func=Name(id='spam', ctx=Load()),
args=[ args=[
Name(id='eggs', ctx=Load()), Name(id='eggs', ctx=Load()),
Constant(value='and cheese', kind=None)], Constant(value='and cheese')],
keywords=[]))], keywords=[]))],
type_ignores=[])""") type_ignores=[])""")
self.assertEqual(ast.dump(node, annotate_fields=False, indent='\t'), """\ self.assertEqual(ast.dump(node, annotate_fields=False, indent='\t'), """\
@ -688,7 +690,7 @@ Module(
\t\t\t\tName('spam', Load()), \t\t\t\tName('spam', Load()),
\t\t\t\t[ \t\t\t\t[
\t\t\t\t\tName('eggs', Load()), \t\t\t\t\tName('eggs', Load()),
\t\t\t\t\tConstant('and cheese', None)], \t\t\t\t\tConstant('and cheese')],
\t\t\t\t[]))], \t\t\t\t[]))],
\t[])""") \t[])""")
self.assertEqual(ast.dump(node, include_attributes=True, indent=3), """\ self.assertEqual(ast.dump(node, include_attributes=True, indent=3), """\
@ -713,7 +715,6 @@ Module(
end_col_offset=9), end_col_offset=9),
Constant( Constant(
value='and cheese', value='and cheese',
kind=None,
lineno=1, lineno=1,
col_offset=11, col_offset=11,
end_lineno=1, end_lineno=1,
@ -762,7 +763,7 @@ Module(
src = ast.parse('1 + 1', mode='eval') src = ast.parse('1 + 1', mode='eval')
src.body.right = ast.copy_location(ast.Num(2), src.body.right) src.body.right = ast.copy_location(ast.Num(2), src.body.right)
self.assertEqual(ast.dump(src, include_attributes=True), self.assertEqual(ast.dump(src, include_attributes=True),
'Expression(body=BinOp(left=Constant(value=1, kind=None, lineno=1, col_offset=0, ' 'Expression(body=BinOp(left=Constant(value=1, lineno=1, col_offset=0, '
'end_lineno=1, end_col_offset=1), op=Add(), right=Constant(value=2, ' 'end_lineno=1, end_col_offset=1), op=Add(), right=Constant(value=2, '
'lineno=1, col_offset=4, end_lineno=1, end_col_offset=5), lineno=1, ' 'lineno=1, col_offset=4, end_lineno=1, end_col_offset=5), lineno=1, '
'col_offset=0, end_lineno=1, end_col_offset=5))' 'col_offset=0, end_lineno=1, end_col_offset=5))'
@ -777,7 +778,7 @@ Module(
self.assertEqual(ast.dump(src, include_attributes=True), self.assertEqual(ast.dump(src, include_attributes=True),
"Module(body=[Expr(value=Call(func=Name(id='write', ctx=Load(), " "Module(body=[Expr(value=Call(func=Name(id='write', ctx=Load(), "
"lineno=1, col_offset=0, end_lineno=1, end_col_offset=5), " "lineno=1, col_offset=0, end_lineno=1, end_col_offset=5), "
"args=[Constant(value='spam', kind=None, lineno=1, col_offset=6, end_lineno=1, " "args=[Constant(value='spam', lineno=1, col_offset=6, end_lineno=1, "
"end_col_offset=12)], keywords=[], lineno=1, col_offset=0, end_lineno=1, " "end_col_offset=12)], keywords=[], lineno=1, col_offset=0, end_lineno=1, "
"end_col_offset=13), lineno=1, col_offset=0, end_lineno=1, " "end_col_offset=13), lineno=1, col_offset=0, end_lineno=1, "
"end_col_offset=13), Expr(value=Call(func=Name(id='spam', ctx=Load(), " "end_col_offset=13), Expr(value=Call(func=Name(id='spam', ctx=Load(), "
@ -792,8 +793,8 @@ Module(
src = ast.parse('1 + 1', mode='eval') src = ast.parse('1 + 1', mode='eval')
self.assertEqual(ast.increment_lineno(src, n=3), src) self.assertEqual(ast.increment_lineno(src, n=3), src)
self.assertEqual(ast.dump(src, include_attributes=True), self.assertEqual(ast.dump(src, include_attributes=True),
'Expression(body=BinOp(left=Constant(value=1, kind=None, lineno=4, col_offset=0, ' 'Expression(body=BinOp(left=Constant(value=1, lineno=4, col_offset=0, '
'end_lineno=4, end_col_offset=1), op=Add(), right=Constant(value=1, kind=None, ' 'end_lineno=4, end_col_offset=1), op=Add(), right=Constant(value=1, '
'lineno=4, col_offset=4, end_lineno=4, end_col_offset=5), lineno=4, ' 'lineno=4, col_offset=4, end_lineno=4, end_col_offset=5), lineno=4, '
'col_offset=0, end_lineno=4, end_col_offset=5))' 'col_offset=0, end_lineno=4, end_col_offset=5))'
) )
@ -801,8 +802,8 @@ Module(
src = ast.parse('1 + 1', mode='eval') src = ast.parse('1 + 1', mode='eval')
self.assertEqual(ast.increment_lineno(src.body, n=3), src.body) self.assertEqual(ast.increment_lineno(src.body, n=3), src.body)
self.assertEqual(ast.dump(src, include_attributes=True), self.assertEqual(ast.dump(src, include_attributes=True),
'Expression(body=BinOp(left=Constant(value=1, kind=None, lineno=4, col_offset=0, ' 'Expression(body=BinOp(left=Constant(value=1, lineno=4, col_offset=0, '
'end_lineno=4, end_col_offset=1), op=Add(), right=Constant(value=1, kind=None, ' 'end_lineno=4, end_col_offset=1), op=Add(), right=Constant(value=1, '
'lineno=4, col_offset=4, end_lineno=4, end_col_offset=5), lineno=4, ' 'lineno=4, col_offset=4, end_lineno=4, end_col_offset=5), lineno=4, '
'col_offset=0, end_lineno=4, end_col_offset=5))' 'col_offset=0, end_lineno=4, end_col_offset=5))'
) )
@ -821,7 +822,7 @@ Module(
self.assertEqual(next(iterator).value, 23) self.assertEqual(next(iterator).value, 23)
self.assertEqual(next(iterator).value, 42) self.assertEqual(next(iterator).value, 42)
self.assertEqual(ast.dump(next(iterator)), self.assertEqual(ast.dump(next(iterator)),
"keyword(arg='eggs', value=Constant(value='leek', kind=None))" "keyword(arg='eggs', value=Constant(value='leek'))"
) )
def test_get_docstring(self): def test_get_docstring(self):

View File

@ -0,0 +1,4 @@
:func:`ast.dump()` no longer outputs optional fields and attributes with
default values. The default values for optional fields and attributes of
AST nodes are now set as class attributes (e.g. ``Constant.kind`` is set
to ``None``).

View File

@ -957,6 +957,8 @@ static int add_ast_fields(void)
(name, name, len(prod.attributes)), 1) (name, name, len(prod.attributes)), 1)
else: else:
self.emit("if (!add_attributes(state->%s_type, NULL, 0)) return 0;" % name, 1) self.emit("if (!add_attributes(state->%s_type, NULL, 0)) return 0;" % name, 1)
self.emit_defaults(name, prod.fields, 1)
self.emit_defaults(name, prod.attributes, 1)
def visitSum(self, sum, name): def visitSum(self, sum, name):
self.emit('state->%s_type = make_type("%s", state->AST_type, NULL, 0);' % self.emit('state->%s_type = make_type("%s", state->AST_type, NULL, 0);' %
@ -968,6 +970,7 @@ static int add_ast_fields(void)
(name, name, len(sum.attributes)), 1) (name, name, len(sum.attributes)), 1)
else: else:
self.emit("if (!add_attributes(state->%s_type, NULL, 0)) return 0;" % name, 1) self.emit("if (!add_attributes(state->%s_type, NULL, 0)) return 0;" % name, 1)
self.emit_defaults(name, sum.attributes, 1)
simple = is_simple(sum) simple = is_simple(sum)
for t in sum.types: for t in sum.types:
self.visitConstructor(t, name, simple) self.visitConstructor(t, name, simple)
@ -981,12 +984,20 @@ static int add_ast_fields(void)
(cons.name, cons.name, name, fields, len(cons.fields)), 1) (cons.name, cons.name, name, fields, len(cons.fields)), 1)
self.emit("if (!state->%s_type) return 0;" % cons.name, 1) self.emit("if (!state->%s_type) return 0;" % cons.name, 1)
self.emit_type("%s_type" % cons.name) self.emit_type("%s_type" % cons.name)
self.emit_defaults(cons.name, cons.fields, 1)
if simple: if simple:
self.emit("state->%s_singleton = PyType_GenericNew((PyTypeObject *)" self.emit("state->%s_singleton = PyType_GenericNew((PyTypeObject *)"
"state->%s_type, NULL, NULL);" % "state->%s_type, NULL, NULL);" %
(cons.name, cons.name), 1) (cons.name, cons.name), 1)
self.emit("if (!state->%s_singleton) return 0;" % cons.name, 1) self.emit("if (!state->%s_singleton) return 0;" % cons.name, 1)
def emit_defaults(self, name, fields, depth):
for field in fields:
if field.opt:
self.emit('if (PyObject_SetAttr(state->%s_type, state->%s, Py_None) == -1)' %
(name, field.name), depth)
self.emit("return 0;", depth+1)
class ASTModuleVisitor(PickleVisitor): class ASTModuleVisitor(PickleVisitor):

94
Python/Python-ast.c generated
View File

@ -1439,58 +1439,102 @@ static int init_types(void)
state->stmt_type = make_type("stmt", state->AST_type, NULL, 0); state->stmt_type = make_type("stmt", state->AST_type, NULL, 0);
if (!state->stmt_type) return 0; if (!state->stmt_type) return 0;
if (!add_attributes(state->stmt_type, stmt_attributes, 4)) return 0; if (!add_attributes(state->stmt_type, stmt_attributes, 4)) return 0;
if (PyObject_SetAttr(state->stmt_type, state->end_lineno, Py_None) == -1)
return 0;
if (PyObject_SetAttr(state->stmt_type, state->end_col_offset, Py_None) ==
-1)
return 0;
state->FunctionDef_type = make_type("FunctionDef", state->stmt_type, state->FunctionDef_type = make_type("FunctionDef", state->stmt_type,
FunctionDef_fields, 6); FunctionDef_fields, 6);
if (!state->FunctionDef_type) return 0; if (!state->FunctionDef_type) return 0;
if (PyObject_SetAttr(state->FunctionDef_type, state->returns, Py_None) ==
-1)
return 0;
if (PyObject_SetAttr(state->FunctionDef_type, state->type_comment, Py_None)
== -1)
return 0;
state->AsyncFunctionDef_type = make_type("AsyncFunctionDef", state->AsyncFunctionDef_type = make_type("AsyncFunctionDef",
state->stmt_type, state->stmt_type,
AsyncFunctionDef_fields, 6); AsyncFunctionDef_fields, 6);
if (!state->AsyncFunctionDef_type) return 0; if (!state->AsyncFunctionDef_type) return 0;
if (PyObject_SetAttr(state->AsyncFunctionDef_type, state->returns, Py_None)
== -1)
return 0;
if (PyObject_SetAttr(state->AsyncFunctionDef_type, state->type_comment,
Py_None) == -1)
return 0;
state->ClassDef_type = make_type("ClassDef", state->stmt_type, state->ClassDef_type = make_type("ClassDef", state->stmt_type,
ClassDef_fields, 5); ClassDef_fields, 5);
if (!state->ClassDef_type) return 0; if (!state->ClassDef_type) return 0;
state->Return_type = make_type("Return", state->stmt_type, Return_fields, state->Return_type = make_type("Return", state->stmt_type, Return_fields,
1); 1);
if (!state->Return_type) return 0; if (!state->Return_type) return 0;
if (PyObject_SetAttr(state->Return_type, state->value, Py_None) == -1)
return 0;
state->Delete_type = make_type("Delete", state->stmt_type, Delete_fields, state->Delete_type = make_type("Delete", state->stmt_type, Delete_fields,
1); 1);
if (!state->Delete_type) return 0; if (!state->Delete_type) return 0;
state->Assign_type = make_type("Assign", state->stmt_type, Assign_fields, state->Assign_type = make_type("Assign", state->stmt_type, Assign_fields,
3); 3);
if (!state->Assign_type) return 0; if (!state->Assign_type) return 0;
if (PyObject_SetAttr(state->Assign_type, state->type_comment, Py_None) ==
-1)
return 0;
state->AugAssign_type = make_type("AugAssign", state->stmt_type, state->AugAssign_type = make_type("AugAssign", state->stmt_type,
AugAssign_fields, 3); AugAssign_fields, 3);
if (!state->AugAssign_type) return 0; if (!state->AugAssign_type) return 0;
state->AnnAssign_type = make_type("AnnAssign", state->stmt_type, state->AnnAssign_type = make_type("AnnAssign", state->stmt_type,
AnnAssign_fields, 4); AnnAssign_fields, 4);
if (!state->AnnAssign_type) return 0; if (!state->AnnAssign_type) return 0;
if (PyObject_SetAttr(state->AnnAssign_type, state->value, Py_None) == -1)
return 0;
state->For_type = make_type("For", state->stmt_type, For_fields, 5); state->For_type = make_type("For", state->stmt_type, For_fields, 5);
if (!state->For_type) return 0; if (!state->For_type) return 0;
if (PyObject_SetAttr(state->For_type, state->type_comment, Py_None) == -1)
return 0;
state->AsyncFor_type = make_type("AsyncFor", state->stmt_type, state->AsyncFor_type = make_type("AsyncFor", state->stmt_type,
AsyncFor_fields, 5); AsyncFor_fields, 5);
if (!state->AsyncFor_type) return 0; if (!state->AsyncFor_type) return 0;
if (PyObject_SetAttr(state->AsyncFor_type, state->type_comment, Py_None) ==
-1)
return 0;
state->While_type = make_type("While", state->stmt_type, While_fields, 3); state->While_type = make_type("While", state->stmt_type, While_fields, 3);
if (!state->While_type) return 0; if (!state->While_type) return 0;
state->If_type = make_type("If", state->stmt_type, If_fields, 3); state->If_type = make_type("If", state->stmt_type, If_fields, 3);
if (!state->If_type) return 0; if (!state->If_type) return 0;
state->With_type = make_type("With", state->stmt_type, With_fields, 3); state->With_type = make_type("With", state->stmt_type, With_fields, 3);
if (!state->With_type) return 0; if (!state->With_type) return 0;
if (PyObject_SetAttr(state->With_type, state->type_comment, Py_None) == -1)
return 0;
state->AsyncWith_type = make_type("AsyncWith", state->stmt_type, state->AsyncWith_type = make_type("AsyncWith", state->stmt_type,
AsyncWith_fields, 3); AsyncWith_fields, 3);
if (!state->AsyncWith_type) return 0; if (!state->AsyncWith_type) return 0;
if (PyObject_SetAttr(state->AsyncWith_type, state->type_comment, Py_None)
== -1)
return 0;
state->Raise_type = make_type("Raise", state->stmt_type, Raise_fields, 2); state->Raise_type = make_type("Raise", state->stmt_type, Raise_fields, 2);
if (!state->Raise_type) return 0; if (!state->Raise_type) return 0;
if (PyObject_SetAttr(state->Raise_type, state->exc, Py_None) == -1)
return 0;
if (PyObject_SetAttr(state->Raise_type, state->cause, Py_None) == -1)
return 0;
state->Try_type = make_type("Try", state->stmt_type, Try_fields, 4); state->Try_type = make_type("Try", state->stmt_type, Try_fields, 4);
if (!state->Try_type) return 0; if (!state->Try_type) return 0;
state->Assert_type = make_type("Assert", state->stmt_type, Assert_fields, state->Assert_type = make_type("Assert", state->stmt_type, Assert_fields,
2); 2);
if (!state->Assert_type) return 0; if (!state->Assert_type) return 0;
if (PyObject_SetAttr(state->Assert_type, state->msg, Py_None) == -1)
return 0;
state->Import_type = make_type("Import", state->stmt_type, Import_fields, state->Import_type = make_type("Import", state->stmt_type, Import_fields,
1); 1);
if (!state->Import_type) return 0; if (!state->Import_type) return 0;
state->ImportFrom_type = make_type("ImportFrom", state->stmt_type, state->ImportFrom_type = make_type("ImportFrom", state->stmt_type,
ImportFrom_fields, 3); ImportFrom_fields, 3);
if (!state->ImportFrom_type) return 0; if (!state->ImportFrom_type) return 0;
if (PyObject_SetAttr(state->ImportFrom_type, state->module, Py_None) == -1)
return 0;
if (PyObject_SetAttr(state->ImportFrom_type, state->level, Py_None) == -1)
return 0;
state->Global_type = make_type("Global", state->stmt_type, Global_fields, state->Global_type = make_type("Global", state->stmt_type, Global_fields,
1); 1);
if (!state->Global_type) return 0; if (!state->Global_type) return 0;
@ -1508,6 +1552,11 @@ static int init_types(void)
state->expr_type = make_type("expr", state->AST_type, NULL, 0); state->expr_type = make_type("expr", state->AST_type, NULL, 0);
if (!state->expr_type) return 0; if (!state->expr_type) return 0;
if (!add_attributes(state->expr_type, expr_attributes, 4)) return 0; if (!add_attributes(state->expr_type, expr_attributes, 4)) return 0;
if (PyObject_SetAttr(state->expr_type, state->end_lineno, Py_None) == -1)
return 0;
if (PyObject_SetAttr(state->expr_type, state->end_col_offset, Py_None) ==
-1)
return 0;
state->BoolOp_type = make_type("BoolOp", state->expr_type, BoolOp_fields, state->BoolOp_type = make_type("BoolOp", state->expr_type, BoolOp_fields,
2); 2);
if (!state->BoolOp_type) return 0; if (!state->BoolOp_type) return 0;
@ -1544,6 +1593,8 @@ static int init_types(void)
if (!state->Await_type) return 0; if (!state->Await_type) return 0;
state->Yield_type = make_type("Yield", state->expr_type, Yield_fields, 1); state->Yield_type = make_type("Yield", state->expr_type, Yield_fields, 1);
if (!state->Yield_type) return 0; if (!state->Yield_type) return 0;
if (PyObject_SetAttr(state->Yield_type, state->value, Py_None) == -1)
return 0;
state->YieldFrom_type = make_type("YieldFrom", state->expr_type, state->YieldFrom_type = make_type("YieldFrom", state->expr_type,
YieldFrom_fields, 1); YieldFrom_fields, 1);
if (!state->YieldFrom_type) return 0; if (!state->YieldFrom_type) return 0;
@ -1555,12 +1606,20 @@ static int init_types(void)
state->FormattedValue_type = make_type("FormattedValue", state->expr_type, state->FormattedValue_type = make_type("FormattedValue", state->expr_type,
FormattedValue_fields, 3); FormattedValue_fields, 3);
if (!state->FormattedValue_type) return 0; if (!state->FormattedValue_type) return 0;
if (PyObject_SetAttr(state->FormattedValue_type, state->conversion,
Py_None) == -1)
return 0;
if (PyObject_SetAttr(state->FormattedValue_type, state->format_spec,
Py_None) == -1)
return 0;
state->JoinedStr_type = make_type("JoinedStr", state->expr_type, state->JoinedStr_type = make_type("JoinedStr", state->expr_type,
JoinedStr_fields, 1); JoinedStr_fields, 1);
if (!state->JoinedStr_type) return 0; if (!state->JoinedStr_type) return 0;
state->Constant_type = make_type("Constant", state->expr_type, state->Constant_type = make_type("Constant", state->expr_type,
Constant_fields, 2); Constant_fields, 2);
if (!state->Constant_type) return 0; if (!state->Constant_type) return 0;
if (PyObject_SetAttr(state->Constant_type, state->kind, Py_None) == -1)
return 0;
state->Attribute_type = make_type("Attribute", state->expr_type, state->Attribute_type = make_type("Attribute", state->expr_type,
Attribute_fields, 3); Attribute_fields, 3);
if (!state->Attribute_type) return 0; if (!state->Attribute_type) return 0;
@ -1619,6 +1678,12 @@ static int init_types(void)
if (!add_attributes(state->slice_type, NULL, 0)) return 0; if (!add_attributes(state->slice_type, NULL, 0)) return 0;
state->Slice_type = make_type("Slice", state->slice_type, Slice_fields, 3); state->Slice_type = make_type("Slice", state->slice_type, Slice_fields, 3);
if (!state->Slice_type) return 0; if (!state->Slice_type) return 0;
if (PyObject_SetAttr(state->Slice_type, state->lower, Py_None) == -1)
return 0;
if (PyObject_SetAttr(state->Slice_type, state->upper, Py_None) == -1)
return 0;
if (PyObject_SetAttr(state->Slice_type, state->step, Py_None) == -1)
return 0;
state->ExtSlice_type = make_type("ExtSlice", state->slice_type, state->ExtSlice_type = make_type("ExtSlice", state->slice_type,
ExtSlice_fields, 1); ExtSlice_fields, 1);
if (!state->ExtSlice_type) return 0; if (!state->ExtSlice_type) return 0;
@ -1797,28 +1862,57 @@ static int init_types(void)
if (!state->excepthandler_type) return 0; if (!state->excepthandler_type) return 0;
if (!add_attributes(state->excepthandler_type, excepthandler_attributes, if (!add_attributes(state->excepthandler_type, excepthandler_attributes,
4)) return 0; 4)) return 0;
if (PyObject_SetAttr(state->excepthandler_type, state->end_lineno, Py_None)
== -1)
return 0;
if (PyObject_SetAttr(state->excepthandler_type, state->end_col_offset,
Py_None) == -1)
return 0;
state->ExceptHandler_type = make_type("ExceptHandler", state->ExceptHandler_type = make_type("ExceptHandler",
state->excepthandler_type, state->excepthandler_type,
ExceptHandler_fields, 3); ExceptHandler_fields, 3);
if (!state->ExceptHandler_type) return 0; if (!state->ExceptHandler_type) return 0;
if (PyObject_SetAttr(state->ExceptHandler_type, state->type, Py_None) == -1)
return 0;
if (PyObject_SetAttr(state->ExceptHandler_type, state->name, Py_None) == -1)
return 0;
state->arguments_type = make_type("arguments", state->AST_type, state->arguments_type = make_type("arguments", state->AST_type,
arguments_fields, 7); arguments_fields, 7);
if (!state->arguments_type) return 0; if (!state->arguments_type) return 0;
if (!add_attributes(state->arguments_type, NULL, 0)) return 0; if (!add_attributes(state->arguments_type, NULL, 0)) return 0;
if (PyObject_SetAttr(state->arguments_type, state->vararg, Py_None) == -1)
return 0;
if (PyObject_SetAttr(state->arguments_type, state->kwarg, Py_None) == -1)
return 0;
state->arg_type = make_type("arg", state->AST_type, arg_fields, 3); state->arg_type = make_type("arg", state->AST_type, arg_fields, 3);
if (!state->arg_type) return 0; if (!state->arg_type) return 0;
if (!add_attributes(state->arg_type, arg_attributes, 4)) return 0; if (!add_attributes(state->arg_type, arg_attributes, 4)) return 0;
if (PyObject_SetAttr(state->arg_type, state->annotation, Py_None) == -1)
return 0;
if (PyObject_SetAttr(state->arg_type, state->type_comment, Py_None) == -1)
return 0;
if (PyObject_SetAttr(state->arg_type, state->end_lineno, Py_None) == -1)
return 0;
if (PyObject_SetAttr(state->arg_type, state->end_col_offset, Py_None) == -1)
return 0;
state->keyword_type = make_type("keyword", state->AST_type, keyword_fields, state->keyword_type = make_type("keyword", state->AST_type, keyword_fields,
2); 2);
if (!state->keyword_type) return 0; if (!state->keyword_type) return 0;
if (!add_attributes(state->keyword_type, NULL, 0)) return 0; if (!add_attributes(state->keyword_type, NULL, 0)) return 0;
if (PyObject_SetAttr(state->keyword_type, state->arg, Py_None) == -1)
return 0;
state->alias_type = make_type("alias", state->AST_type, alias_fields, 2); state->alias_type = make_type("alias", state->AST_type, alias_fields, 2);
if (!state->alias_type) return 0; if (!state->alias_type) return 0;
if (!add_attributes(state->alias_type, NULL, 0)) return 0; if (!add_attributes(state->alias_type, NULL, 0)) return 0;
if (PyObject_SetAttr(state->alias_type, state->asname, Py_None) == -1)
return 0;
state->withitem_type = make_type("withitem", state->AST_type, state->withitem_type = make_type("withitem", state->AST_type,
withitem_fields, 2); withitem_fields, 2);
if (!state->withitem_type) return 0; if (!state->withitem_type) return 0;
if (!add_attributes(state->withitem_type, NULL, 0)) return 0; if (!add_attributes(state->withitem_type, NULL, 0)) return 0;
if (PyObject_SetAttr(state->withitem_type, state->optional_vars, Py_None)
== -1)
return 0;
state->type_ignore_type = make_type("type_ignore", state->AST_type, NULL, state->type_ignore_type = make_type("type_ignore", state->AST_type, NULL,
0); 0);
if (!state->type_ignore_type) return 0; if (!state->type_ignore_type) return 0;