8000 gh-113317: Don't use global clinic instance in bad_argument() (#114330) · python/cpython@e14930f · GitHub
[go: up one dir, main page]

Skip to content

Commit e14930f

Browse files
gh-113317: Don't use global clinic instance in bad_argument() (#114330)
Make it possible for a converter to have multiple includes, by collecting them in a list on the converter instance. This implies converter includes are added during template generation, so we have to add them to the clinic instance at the end of the template generation instead of in the beginning.
1 parent 8edc802 commit e14930f

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

Tools/clinic/clinic.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -818,12 +818,6 @@ def output_templates(
818818
del parameters[0]
819819
converters = [p.converter for p in parameters]
820820

821-
# Copy includes from parameters to Clinic
822-
for converter in converters:
823-
include = converter.include
824-
if include:
825-
clinic.add_include(include.filename, include.reason,
826-
condition=include.condition)
827821
if f.critical_section:
828822
clinic.add_include('pycore_critical_section.h', 'Py_BEGIN_CRITICAL_SECTION()')
829823
has_option_groups = parameters and (parameters[0].group or parameters[-1].group)
@@ -1367,6 +1361,13 @@ def parser_body(
13671361
declarations=declarations)
13681362

13691363

1364+
# Copy includes from parameters to Clinic after parse_arg() has been
1365+
# called above.
1366+
for converter in converters:
1367+
for include in converter.includes:
1368+
clinic.add_include(include.filename, include.reason,
1369+
condition=include.condition)
1370+
13701371
if new_or_init:
13711372
methoddef_define = ''
13721373

@@ -2988,7 +2989,6 @@ class CConverter(metaclass=CConverterAutoRegister):
29882989
# Only set by self_converter.
29892990
signature_name: str | None = None
29902991

2991-
include: Include | None = None
29922992
broken_limited_capi: bool = False
29932993

29942994
# keep in sync with self_converter.__init__!
@@ -3008,6 +3008,7 @@ def __init__(self,
30083008
self.name = ensure_legal_c_identifier(name)
30093009
self.py_name = py_name
30103010
self.unused = unused
3011+
self.includes: list[Include] = []
30113012

30123013
if default is not unspecified:
30133014
if (self.default_type
@@ -3263,8 +3264,7 @@ def bad_argument(self, displayname: str, expected: str, *, limited_capi: bool, e
32633264
else:
32643265
if expected_literal:
32653266
expected = f'"{expected}"'
3266-
if clinic is not None:
3267-
clinic.add_include('pycore_modsupport.h', '_PyArg_BadArgument()')
3267+
self.add_include('pycore_modsupport.h', '_PyArg_BadArgument()')
32683268
return f'_PyArg_BadArgument("{{{{name}}}}", "{displayname}", {expected}, {{argname}});'
32693269

32703270
def format_code(self, fmt: str, *,
@@ -3336,9 +3336,8 @@ def parser_name(self) -> str:
33363336

33373337
def add_include(self, name: str, reason: str,
33383338
*, condition: str | None = None) -> None:
3339-
if self.include is not None:
3340-
raise ValueError("a converter only supports a single include")
3341-
self.include = Include(name, reason, condition)
3339+
include = Include(name, reason, condition)
3340+
self.includes.append(include)
33423341

33433342
type_checks = {
33443343
'&PyLong_Type': ('PyLong_Check', 'int'),

0 commit comments

Comments
 (0)
0