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

Class BlockPrinter

Tools/clinic/libclinic/codegen.py:96–176  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

94
95@dc.dataclass(slots=True)
96class BlockPrinter:
97 language: Language
98 f: io.StringIO = dc.field(default_factory=io.StringIO)
99
100 # '#include "header.h" // reason': column of '//' comment
101 INCLUDE_COMMENT_COLUMN: Final[int] = 35
102
103 def print_block(
104 self,
105 block: Block,
106 *,
107 header_includes: list[Include] | None = None,
108 ) -> None:
109 input = block.input
110 output = block.output
111 dsl_name = block.dsl_name
112 write = self.f.write
113
114 assert not ((dsl_name is None) ^ (output is None)), "you must specify dsl_name and output together, dsl_name " + repr(dsl_name)
115
116 if not dsl_name:
117 write(input)
118 return
119
120 write(self.language.start_line.format(dsl_name=dsl_name))
121 write("\n")
122
123 body_prefix = self.language.body_prefix.format(dsl_name=dsl_name)
124 if not body_prefix:
125 write(input)
126 else:
127 for line in input.split('\n'):
128 write(body_prefix)
129 write(line)
130 write("\n")
131
132 write(self.language.stop_line.format(dsl_name=dsl_name))
133 write("\n")
134
135 output = ''
136 if header_includes:
137 # Emit optional "#include" directives for C headers
138 output += '\n'
139
140 current_condition: str | None = None
141 for include in header_includes:
142 if include.condition != current_condition:
143 if current_condition:
144 output += '#endif\n'
145 current_condition = include.condition
146 if include.condition:
147 output += f'{include.condition}\n'
148
149 if current_condition:
150 line = f'# include "{include.filename}"'
151 else:
152 line = f'#include "{include.filename}"'
153 if include.reason:

Callers 3

_testMethod · 0.90
__init__Method · 0.90
parseMethod · 0.90

Calls 1

fieldMethod · 0.80

Tested by 1

_testMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…