aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2020-03-10 00:07:47 +0200
committerGitHub <noreply@github.com>2020-03-10 00:07:47 +0200
commitb7e9525f9c7ef02a1d2ad8253afdeb733b0951d4 (patch)
treea5a765156210e426d89e5f19a75f932dd2165834 /Parser/asdl_c.py
parentbpo-39877: Refactor take_gil() function (GH-18885) (diff)
downloadcpython-b7e9525f9c7ef02a1d2ad8253afdeb733b0951d4.tar.gz
cpython-b7e9525f9c7ef02a1d2ad8253afdeb733b0951d4.tar.bz2
cpython-b7e9525f9c7ef02a1d2ad8253afdeb733b0951d4.zip
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).
Diffstat (limited to 'Parser/asdl_c.py')
-rwxr-xr-xParser/asdl_c.py11
1 files changed, 11 insertions, 0 deletions
diff --git a/Parser/asdl_c.py b/Parser/asdl_c.py
index e81506cc9a..016c12ddf1 100755
--- a/Parser/asdl_c.py
+++ b/Parser/asdl_c.py
@@ -957,6 +957,8 @@ static int add_ast_fields(void)
(name, name, len(prod.attributes)), 1)
else:
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):
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)
else:
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)
for t in sum.types:
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)
self.emit("if (!state->%s_type) return 0;" % cons.name, 1)
self.emit_type("%s_type" % cons.name)
+ self.emit_defaults(cons.name, cons.fields, 1)
if simple:
self.emit("state->%s_singleton = PyType_GenericNew((PyTypeObject *)"
"state->%s_type, NULL, NULL);" %
(cons.name, 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):