MCPcopy Index your code
hub / github.com/python/cpython / _format

Function _format

Lib/ast.py:135–198  ·  view source on GitHub ↗
(node, level=0)

Source from the content-addressed store, hash-verified

133 will be omitted from the output for better readability.
134 """
135 def _format(node, level=0):
136 if indent is not None:
137 level += 1
138 prefix = '\n' + indent * level
139 sep = ',\n' + indent * level
140 else:
141 prefix = ''
142 sep = ', '
143 if isinstance(node, AST):
144 cls = type(node)
145 args = []
146 args_buffer = []
147 allsimple = True
148 keywords = annotate_fields
149 for name in node._fields:
150 try:
151 value = getattr(node, name)
152 except AttributeError:
153 keywords = True
154 continue
155 if value is None and getattr(cls, name, ...) is None:
156 keywords = True
157 continue
158 if not show_empty:
159 if value == []:
160 field_type = cls._field_types.get(name, object)
161 if getattr(field_type, '__origin__', ...) is list:
162 if not keywords:
163 args_buffer.append(repr(value))
164 continue
165 elif isinstance(value, Load):
166 field_type = cls._field_types.get(name, object)
167 if field_type is expr_context:
168 if not keywords:
169 args_buffer.append(repr(value))
170 continue
171 if not keywords:
172 args.extend(args_buffer)
173 args_buffer = []
174 value, simple = _format(value, level)
175 allsimple = allsimple and simple
176 if keywords:
177 args.append('%s=%s' % (name, value))
178 else:
179 args.append(value)
180 if include_attributes and node._attributes:
181 for name in node._attributes:
182 try:
183 value = getattr(node, name)
184 except AttributeError:
185 continue
186 if value is None and getattr(cls, name, ...) is None:
187 continue
188 value, simple = _format(value, level)
189 allsimple = allsimple and simple
190 args.append('%s=%s' % (name, value))
191 if allsimple and len(args) <= 3:
192 return '%s(%s)' % (node.__class__.__name__, ', '.join(args)), not args

Callers 1

dumpFunction · 0.70

Calls 4

getMethod · 0.45
appendMethod · 0.45
extendMethod · 0.45
joinMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…