Skip to content

Commit e446461

Browse files
committed
Add get_includes() methods
* Make Codegen.ifndef_symbols private. * Make Codegen.includes private. * Make CConverter.includes private.
1 parent a7479ba commit e446461

File tree

4 files changed

+21
-14
lines changed

4 files changed

+21
-14
lines changed

Tools/clinic/libclinic/app.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,9 +230,10 @@ def parse(self, input: str) -> str:
230230
pass
231231

232232
block.input = 'preserve\n'
233+
includes = self.codegen.get_includes()
234+
233235
printer_2 = BlockPrinter(self.language)
234-
printer_2.print_block(block,
235-
header_includes=self.codegen.includes)
236+
printer_2.print_block(block, header_includes=includes)
236237
libclinic.write_file(destination.filename,
237238
printer_2.f.getvalue())
238239
continue

Tools/clinic/libclinic/clanguage.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -956,7 +956,7 @@ def parser_body(
956956
# Copy includes from parameters to Clinic after parse_arg() has been
957957
# called above.
958958
for converter in converters:
959-
for include in converter.includes:
959+
for include in converter.get_includes():
960960
codegen.add_include(include.filename, include.reason,
961961
condition=include.condition)
962962

Tools/clinic/libclinic/codegen.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ def print_block(
104104
self,
105105
block: Block,
106106
*,
107-
header_includes: dict[str, Include] | None = None,
107+
header_includes: list[Include] | None = None,
108108
) -> None:
109109
input = block.input
110110
output = block.output
@@ -138,8 +138,7 @@ def print_block(
138138
output += '\n'
139139

140140
current_condition: str | None = None
141-
includes = sorted(header_includes.values(), key=Include.sort_key)
142-
for include in includes:
141+
for include in header_includes:
143142
if include.condition != current_condition:
144143
if current_condition:
145144
output += '#endif\n'
@@ -270,20 +269,20 @@ def dump(self) -> str:
270269
class Codegen:
271270
def __init__(self, limited_capi: bool) -> None:
272271
self.limited_capi = limited_capi
273-
self.ifndef_symbols: set[str] = set()
272+
self._ifndef_symbols: set[str] = set()
274273
# dict: include name => Include instance
275-
self.includes: dict[str, Include] = {}
274+
self._includes: dict[str, Include] = {}
276275

277276
def add_ifndef_symbol(self, name: str) -> bool:
278-
if name in self.ifndef_symbols:
277+
if name in self._ifndef_symbols:
279278
return False
280-
self.ifndef_symbols.add(name)
279+
self._ifndef_symbols.add(name)
281280
return True
282281

283282
def add_include(self, name: str, reason: str,
284283
*, condition: str | None = None) -> None:
285284
try:
286-
existing = self.includes[name]
285+
existing = self._includes[name]
287286
except KeyError:
288287
pass
289288
else:
@@ -296,4 +295,8 @@ def add_include(self, name: str, reason: str,
296295
# no need to list all of them.
297296
return
298297

299-
self.includes[name] = Include(name, reason, condition)
298+
self._includes[name] = Include(name, reason, condition)
299+
300+
def get_includes(self) -> list[Include]:
301+
return sorted(self._includes.values(),
302+
key=Include.sort_key)

Tools/clinic/libclinic/converter.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ def __init__(self,
180180
self.name = libclinic.ensure_legal_c_identifier(name)
181181
self.py_name = py_name
182182
self.unused = unused
183-
self.includes: list[Include] = []
183+
self._includes: list[Include] = []
184184

185185
if default is not unspecified:
186186
if (self.default_type
@@ -513,7 +513,10 @@ def parser_name(self) -> str:
513513
def add_include(self, name: str, reason: str,
514514
*, condition: str | None = None) -> None:
515515
include = Include(name, reason, condition)
516-
self.includes.append(include)
516+
self._includes.append(include)
517+
518+
def get_includes(self) -> list[Include]:
519+
return self._includes
517520

518521

519522
ConverterType = Callable[..., CConverter]

0 commit comments

Comments
 (0)