diff options
Diffstat (limited to 'Lib/ast.py')
| -rw-r--r-- | Lib/ast.py | 46 |
1 files changed, 26 insertions, 20 deletions
diff --git a/Lib/ast.py b/Lib/ast.py index 9a3d3806eb..0bce4a49dc 100644 --- a/Lib/ast.py +++ b/Lib/ast.py @@ -123,31 +123,36 @@ def dump(node, annotate_fields=True, include_attributes=False, *, indent=None): prefix = '' sep = ', ' if isinstance(node, AST): + cls = type(node) args = [] allsimple = True keywords = annotate_fields - for field in node._fields: + for name in node._fields: try: - value = getattr(node, field) + value = getattr(node, name) except AttributeError: 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: - value, simple = _format(value, level) - allsimple = allsimple and simple - if keywords: - args.append('%s=%s' % (field, value)) - else: - args.append(value) + args.append(value) if include_attributes and node._attributes: - for attr in node._attributes: + for name in node._attributes: try: - value = getattr(node, attr) + value = getattr(node, name) except AttributeError: - pass - else: - value, simple = _format(value, level) - allsimple = allsimple and simple - args.append('%s=%s' % (attr, value)) + continue + if value is None and getattr(cls, name, ...) is None: + continue + value, simple = _format(value, level) + allsimple = allsimple and simple + args.append('%s=%s' % (name, value)) if allsimple and len(args) <= 3: return '%s(%s)' % (node.__class__.__name__, ', '.join(args)), not args 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*. """ for attr in 'lineno', 'col_offset', 'end_lineno', 'end_col_offset': - if attr in old_node._attributes and attr in new_node._attributes \ - and hasattr(old_node, attr): - setattr(new_node, attr, getattr(old_node, attr)) + if attr in old_node._attributes and attr in new_node._attributes: + value = getattr(old_node, attr, None) + if value is not None: + setattr(new_node, attr, value) return new_node @@ -191,7 +197,7 @@ def fix_missing_locations(node): else: lineno = node.lineno 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 else: end_lineno = node.end_lineno @@ -201,7 +207,7 @@ def fix_missing_locations(node): else: col_offset = node.col_offset 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 else: end_col_offset = node.end_col_offset |
