MCPcopy Index your code
hub / github.com/python/mypy / c_array_initializer

Function c_array_initializer

mypyc/codegen/emit.py:1451–1480  ·  view source on GitHub ↗

Construct an initializer for a C array variable. Components are C expressions valid in an initializer. For example, if components are ["1", "2"], the result would be "{1, 2}", which can be used like this: int a[] = {1, 2}; If the result is long, split it into multiple lin

(components: list[str], *, indented: bool = False)

Source from the content-addressed store, hash-verified

1449
1450
1451def c_array_initializer(components: list[str], *, indented: bool = False) -> str:
1452 """Construct an initializer for a C array variable.
1453
1454 Components are C expressions valid in an initializer.
1455
1456 For example, if components are ["1", "2"], the result
1457 would be "{1, 2}", which can be used like this:
1458
1459 int a[] = {1, 2};
1460
1461 If the result is long, split it into multiple lines.
1462 """
1463 indent = " " * 4 if indented else ""
1464 res = []
1465 current: list[str] = []
1466 cur_len = 0
1467 for c in components:
1468 if not current or cur_len + 2 + len(indent) + len(c) < 70:
1469 current.append(c)
1470 cur_len += len(c) + 2
1471 else:
1472 res.append(indent + ", ".join(current))
1473 current = [c]
1474 cur_len = len(c)
1475 if not res:
1476 # Result fits on a single line
1477 return "{%s}" % ", ".join(current)
1478 # Multi-line result
1479 res.append(indent + ", ".join(current))
1480 return "{\n " + ",\n ".join(res) + "\n" + indent + "}"
1481
1482
1483def native_function_doc_initializer(func: FuncIR) -> str:

Callers 2

visit_assign_multiMethod · 0.90

Calls 3

lenFunction · 0.85
appendMethod · 0.80
joinMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…