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

Function _make_iterencode

Lib/json/encoder.py:265–459  ·  view source on GitHub ↗
(markers, _default, _encoder, _indent, _floatstr,
        _key_separator, _item_separator, _sort_keys, _skipkeys, _one_shot,
    )

Source from the content-addressed store, hash-verified

263 return _iterencode(o, 0)
264
265def _make_iterencode(markers, _default, _encoder, _indent, _floatstr,
266 _key_separator, _item_separator, _sort_keys, _skipkeys, _one_shot,
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

Callers 1

iterencodeMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…