From ef8011ee537535cbb96385c0ea7029af6c99c59d Mon Sep 17 00:00:00 2001 From: Adam Turner <9087854+aa-turner@users.noreply.github.com> Date: Mon, 28 Aug 2023 20:57:57 +0100 Subject: [PATCH 1/5] Clinic: Only pass ``includes`` to ``print_block()`` --- Tools/clinic/clinic.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/Tools/clinic/clinic.py b/Tools/clinic/clinic.py index 39a776f02b5bf5..bf0d0bb4e635d4 100755 --- a/Tools/clinic/clinic.py +++ b/Tools/clinic/clinic.py @@ -2172,8 +2172,8 @@ def print_block( self, block: Block, *, - clinic: Clinic, core_includes: bool = False, + header_includes: dict[str, str] | None = None, ) -> None: input = block.input output = block.output @@ -2212,11 +2212,10 @@ def print_block( """) - if clinic is not None: - # Emit optional includes - for include, reason in sorted(clinic.includes.items()): - line = f'#include "{include}"' - line = line.ljust(35) + f'// {reason}\n' + if header_includes is not None: + # Emit optional "#include" directives for C headers + for include, reason in sorted(header_includes.items()): + line = f'#include "{include}"'.ljust(35) + f'// {reason}\n' output += line input = ''.join(block.input) @@ -2531,7 +2530,7 @@ def parse(self, input: str) -> str: self.parsers[dsl_name] = parsers[dsl_name](self) parser = self.parsers[dsl_name] parser.parse(block) - printer.print_block(block, clinic=self) + printer.print_block(block, header_includes=self.includes) # these are destinations not buffers for name, destination in self.destinations.items(): @@ -2546,7 +2545,7 @@ def parse(self, input: str) -> str: block.input = "dump " + name + "\n" warn("Destination buffer " + repr(name) + " not empty at end of file, emptying.") printer.write("\n") - printer.print_block(block, clinic=self) + printer.print_block(block, header_includes=self.includes) continue if destination.type == 'file': @@ -2571,7 +2570,7 @@ def parse(self, input: str) -> str: block.input = 'preserve\n' printer_2 = BlockPrinter(self.language) - printer_2.print_block(block, core_includes=True, clinic=self) + printer_2.print_block(block, core_includes=True, header_includes=self.includes) write_file(destination.filename, printer_2.f.getvalue()) continue From 373f13eb50383ba92d1cec769a28992cb18b7d18 Mon Sep 17 00:00:00 2001 From: Adam Turner <9087854+aa-turner@users.noreply.github.com> Date: Mon, 28 Aug 2023 21:08:03 +0100 Subject: [PATCH 2/5] limited_capi, too --- Tools/clinic/clinic.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Tools/clinic/clinic.py b/Tools/clinic/clinic.py index bf0d0bb4e635d4..591fa84088fd2b 100755 --- a/Tools/clinic/clinic.py +++ b/Tools/clinic/clinic.py @@ -2173,6 +2173,7 @@ def print_block( block: Block, *, core_includes: bool = False, + limited_capi: bool = False, header_includes: dict[str, str] | None = None, ) -> None: input = block.input @@ -2203,7 +2204,7 @@ def print_block( output = '' if core_includes: - if not clinic.limited_capi: + if not limited_capi: output += textwrap.dedent(""" #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) # include "pycore_gc.h" // PyGC_Head From 91456c772be9c6cc5d741b532fefddd5f242ccd3 Mon Sep 17 00:00:00 2001 From: Adam Turner <9087854+aa-turner@users.noreply.github.com> Date: Mon, 28 Aug 2023 21:17:21 +0100 Subject: [PATCH 3/5] tighten header_includes --- Tools/clinic/clinic.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/Tools/clinic/clinic.py b/Tools/clinic/clinic.py index 591fa84088fd2b..0519b5cba6178c 100755 --- a/Tools/clinic/clinic.py +++ b/Tools/clinic/clinic.py @@ -2174,7 +2174,7 @@ def print_block( *, core_includes: bool = False, limited_capi: bool = False, - header_includes: dict[str, str] | None = None, + header_includes: dict[str, str], ) -> None: input = block.input output = block.output @@ -2213,11 +2213,10 @@ def print_block( """) - if header_includes is not None: - # Emit optional "#include" directives for C headers - for include, reason in sorted(header_includes.items()): - line = f'#include "{include}"'.ljust(35) + f'// {reason}\n' - output += line + # Emit optional "#include" directives for C headers + for include, reason in sorted(header_includes.items()): + line = f'#include "{include}"'.ljust(35) + f'// {reason}\n' + output += line input = ''.join(block.input) output += ''.join(block.output) From 72b499993d7091a890a0345fe8466c005496f852 Mon Sep 17 00:00:00 2001 From: Adam Turner <9087854+aa-turner@users.noreply.github.com> Date: Mon, 28 Aug 2023 21:19:24 +0100 Subject: [PATCH 4/5] tighten limited_capi --- Tools/clinic/clinic.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/Tools/clinic/clinic.py b/Tools/clinic/clinic.py index 0519b5cba6178c..2b016685e84ce5 100755 --- a/Tools/clinic/clinic.py +++ b/Tools/clinic/clinic.py @@ -2173,7 +2173,7 @@ def print_block( block: Block, *, core_includes: bool = False, - limited_capi: bool = False, + limited_capi: bool, header_includes: dict[str, str], ) -> None: input = block.input @@ -2530,7 +2530,9 @@ def parse(self, input: str) -> str: self.parsers[dsl_name] = parsers[dsl_name](self) parser = self.parsers[dsl_name] parser.parse(block) - printer.print_block(block, header_includes=self.includes) + printer.print_block(block, + limited_capi=self.limited_capi, + header_includes=self.includes) # these are destinations not buffers for name, destination in self.destinations.items(): @@ -2545,7 +2547,9 @@ def parse(self, input: str) -> str: block.input = "dump " + name + "\n" warn("Destination buffer " + repr(name) + " not empty at end of file, emptying.") printer.write("\n") - printer.print_block(block, header_includes=self.includes) + printer.print_block(block, + limited_capi=self.limited_capi, + header_includes=self.includes) continue if destination.type == 'file': @@ -2570,7 +2574,10 @@ def parse(self, input: str) -> str: block.input = 'preserve\n' printer_2 = BlockPrinter(self.language) - printer_2.print_block(block, core_includes=True, header_includes=self.includes) + printer_2.print_block(block, + core_includes=True, + limited_capi=self.limited_capi, + header_includes=self.includes) write_file(destination.filename, printer_2.f.getvalue()) continue From 1512b9de56b7e74baa03386f5367eed6189444cf Mon Sep 17 00:00:00 2001 From: Adam Turner <9087854+aa-turner@users.noreply.github.com> Date: Mon, 28 Aug 2023 21:36:47 +0100 Subject: [PATCH 5/5] tests --- Lib/test/test_clinic.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_clinic.py b/Lib/test/test_clinic.py index 0592c2e3a2b291..b29f2ea55516fe 100644 --- a/Lib/test/test_clinic.py +++ b/Lib/test/test_clinic.py @@ -834,7 +834,7 @@ def _test(self, input, output): writer = clinic.BlockPrinter(language) c = _make_clinic() for block in blocks: - writer.print_block(block, clinic=c) + writer.print_block(block, limited_capi=c.limited_capi, header_includes=c.includes) output = writer.f.getvalue() assert output == input, "output != input!\n\noutput " + repr(output) + "\n\n input " + repr(input)