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

Function _iterencode_list

Lib/json/encoder.py:269–326  ·  view source on GitHub ↗
(lst, _current_indent_level)

Source from the content-addressed store, hash-verified

267 ):
268
269 def _iterencode_list(lst, _current_indent_level):
270 if not lst:
271 yield '[]'
272 return
273 if markers is not None:
274 markerid = id(lst)
275 if markerid in markers:
276 raise ValueError("Circular reference detected")
277 markers[markerid] = lst
278 buf = '['
279 if _indent is not None:
280 _current_indent_level += 1
281 newline_indent = '\n' + _indent * _current_indent_level
282 separator = _item_separator + newline_indent
283 buf += newline_indent
284 else:
285 newline_indent = None
286 separator = _item_separator
287 for i, value in enumerate(lst):
288 if i:
289 buf = separator
290 try:
291 if isinstance(value, str):
292 yield buf + _encoder(value)
293 elif value is None:
294 yield buf + 'null'
295 elif value is True:
296 yield buf + 'true'
297 elif value is False:
298 yield buf + 'false'
299 elif isinstance(value, int):
300 # Subclasses of int/float may override __repr__, but we still
301 # want to encode them as integers/floats in JSON. One example
302 # within the standard library is IntEnum.
303 yield buf + int.__repr__(value)
304 elif isinstance(value, float):
305 # see comment above for int
306 yield buf + _floatstr(value)
307 else:
308 yield buf
309 if isinstance(value, (list, tuple)):
310 chunks = _iterencode_list(value, _current_indent_level)
311 elif isinstance(value, (dict, frozendict)):
312 chunks = _iterencode_dict(value, _current_indent_level)
313 else:
314 chunks = _iterencode(value, _current_indent_level)
315 yield from chunks
316 except GeneratorExit:
317 raise
318 except BaseException as exc:
319 exc.add_note(f'when serializing {type(lst).__name__} item {i}')
320 raise
321 if newline_indent is not None:
322 _current_indent_level -= 1
323 yield '\n' + _indent * _current_indent_level
324 yield ']'
325 if markers is not None:
326 del markers[markerid]

Callers 2

_iterencode_dictFunction · 0.85
_iterencodeFunction · 0.85

Calls 5

idFunction · 0.85
enumerateFunction · 0.85
_iterencode_dictFunction · 0.85
_iterencodeFunction · 0.85
__repr__Method · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…