Skip to content

Commit 0bd2ba5

Browse files
authored
GH-107603: Clinic: Pass specific attributes to print_block() (#108581)
1 parent cf7ba83 commit 0bd2ba5

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

Lib/test/test_clinic.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -834,7 +834,7 @@ def _test(self, input, output):
834834
writer = clinic.BlockPrinter(language)
835835
c = _make_clinic()
836836
for block in blocks:
837-
writer.print_block(block, clinic=c)
837+
writer.print_block(block, limited_capi=c.limited_capi, header_includes=c.includes)
838838
output = writer.f.getvalue()
839839
assert output == input, "output != input!\n\noutput " + repr(output) + "\n\n input " + repr(input)
840840

Tools/clinic/clinic.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2172,8 +2172,9 @@ def print_block(
21722172
self,
21732173
block: Block,
21742174
*,
2175-
clinic: Clinic,
21762175
core_includes: bool = False,
2176+
limited_capi: bool,
2177+
header_includes: dict[str, str],
21772178
) -> None:
21782179
input = block.input
21792180
output = block.output
@@ -2203,7 +2204,7 @@ def print_block(
22032204

22042205
output = ''
22052206
if core_includes:
2206-
if not clinic.limited_capi:
2207+
if not limited_capi:
22072208
output += textwrap.dedent("""
22082209
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
22092210
# include "pycore_gc.h" // PyGC_Head
@@ -2212,12 +2213,10 @@ def print_block(
22122213
22132214
""")
22142215

2215-
if clinic is not None:
2216-
# Emit optional includes
2217-
for include, reason in sorted(clinic.includes.items()):
2218-
line = f'#include "{include}"'
2219-
line = line.ljust(35) + f'// {reason}\n'
2220-
output += line
2216+
# Emit optional "#include" directives for C headers
2217+
for include, reason in sorted(header_includes.items()):
2218+
line = f'#include "{include}"'.ljust(35) + f'// {reason}\n'
2219+
output += line
22212220

22222221
input = ''.join(block.input)
22232222
output += ''.join(block.output)
@@ -2531,7 +2530,9 @@ def parse(self, input: str) -> str:
25312530
self.parsers[dsl_name] = parsers[dsl_name](self)
25322531
parser = self.parsers[dsl_name]
25332532
parser.parse(block)
2534-
printer.print_block(block, clinic=self)
2533+
printer.print_block(block,
2534+
limited_capi=self.limited_capi,
2535+
header_includes=self.includes)
25352536

25362537
# these are destinations not buffers
25372538
for name, destination in self.destinations.items():
@@ -2546,7 +2547,9 @@ def parse(self, input: str) -> str:
25462547
block.input = "dump " + name + "\n"
25472548
warn("Destination buffer " + repr(name) + " not empty at end of file, emptying.")
25482549
printer.write("\n")
2549-
printer.print_block(block, clinic=self)
2550+
printer.print_block(block,
2551+
limited_capi=self.limited_capi,
2552+
header_includes=self.includes)
25502553
continue
25512554

25522555
if destination.type == 'file':
@@ -2571,7 +2574,10 @@ def parse(self, input: str) -> str:
25712574

25722575
block.input = 'preserve\n'
25732576
printer_2 = BlockPrinter(self.language)
2574-
printer_2.print_block(block, core_includes=True, clinic=self)
2577+
printer_2.print_block(block,
2578+
core_includes=True,
2579+
limited_capi=self.limited_capi,
2580+
header_includes=self.includes)
25752581
write_file(destination.filename, printer_2.f.getvalue())
25762582
continue
25772583

0 commit comments

Comments
 (0)