Return a formatted dump of the tree in node. This is mainly useful for debugging purposes. If annotate_fields is true (by default), the returned string will show the names and the values for fields. If annotate_fields is false, the result string will be more compact by omittin
(
node, annotate_fields=True, include_attributes=False,
*,
indent=None, show_empty=False,
)
| 107 | |
| 108 | |
| 109 | def dump( |
| 110 | node, annotate_fields=True, include_attributes=False, |
| 111 | *, |
| 112 | indent=None, show_empty=False, |
| 113 | ): |
| 114 | """ |
| 115 | Return a formatted dump of the tree in node. This is mainly useful for |
| 116 | debugging purposes. If annotate_fields is true (by default), |
| 117 | the returned string will show the names and the values for fields. |
| 118 | If annotate_fields is false, the result string will be more compact by |
| 119 | omitting unambiguous field names. Attributes such as line |
| 120 | numbers and column offsets are not dumped by default. If this is wanted, |
| 121 | include_attributes can be set to true. If indent is a non-negative |
| 122 | integer or string, then the tree will be pretty-printed with that indent |
| 123 | level. None (the default) selects the single line representation. |
| 124 | If show_empty is False, then empty lists and fields that are None |
| 125 | will be omitted from the output for better readability. |
| 126 | """ |
| 127 | def _format(node, level=0): |
| 128 | if indent is not None: |
| 129 | level += 1 |
| 130 | prefix = '\n' + indent * level |
| 131 | sep = ',\n' + indent * level |
| 132 | else: |
| 133 | prefix = '' |
| 134 | sep = ', ' |
| 135 | if isinstance(node, AST): |
| 136 | cls = type(node) |
| 137 | args = [] |
| 138 | args_buffer = [] |
| 139 | allsimple = True |
| 140 | keywords = annotate_fields |
| 141 | for name in node._fields: |
| 142 | try: |
| 143 | value = getattr(node, name) |
| 144 | except AttributeError: |
| 145 | keywords = True |
| 146 | continue |
| 147 | if value is None and getattr(cls, name, ...) is None: |
| 148 | keywords = True |
| 149 | continue |
| 150 | if not show_empty: |
| 151 | if value == []: |
| 152 | field_type = cls._field_types.get(name, object) |
| 153 | if getattr(field_type, '__origin__', ...) is list: |
| 154 | if not keywords: |
| 155 | args_buffer.append(repr(value)) |
| 156 | continue |
| 157 | if not keywords: |
| 158 | args.extend(args_buffer) |
| 159 | args_buffer = [] |
| 160 | value, simple = _format(value, level) |
| 161 | allsimple = allsimple and simple |
| 162 | if keywords: |
| 163 | args.append('%s=%s' % (name, value)) |
| 164 | else: |
| 165 | args.append(value) |
| 166 | if include_attributes and node._attributes: |
no test coverage detected