Skip to content

gh-113317: Don't use global clinic instance in bad_argument() #114330

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 11 additions & 12 deletions Tools/clinic/clinic.py
Original file line number Diff line number Diff line change
Expand Up @@ -818,12 +818,6 @@ def output_templates(
del parameters[0]
converters = [p.converter for p in parameters]

# Copy includes from parameters to Clinic
for converter in converters:
include = converter.include
if include:
clinic.add_include(include.filename, include.reason,
condition=include.condition)
if f.critical_section:
clinic.add_include('pycore_critical_section.h', 'Py_BEGIN_CRITICAL_SECTION()')
has_option_groups = parameters and (parameters[0].group or parameters[-1].group)
Expand Down Expand Up @@ -1367,6 +1361,13 @@ def parser_body(
declarations=declarations)


# Copy includes from parameters to Clinic after parse_arg() has been
# called above.
for converter in converters:
for include in converter.includes:
clinic.add_include(include.filename, include.reason,
condition=include.condition)

if new_or_init:
methoddef_define = ''

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

include: Include | None = None
broken_limited_capi: bool = False

# keep in sync with self_converter.__init__!
Expand All @@ -3008,6 +3008,7 @@ def __init__(self,
self.name = ensure_legal_c_identifier(name)
self.py_name = py_name
self.unused = unused
self.includes: list[Include] = []

if default is not unspecified:
if (self.default_type
Expand Down Expand Up @@ -3263,8 +3264,7 @@ def bad_argument(self, displayname: str, expected: str, *, limited_capi: bool, e
else:
if expected_literal:
expected = f'"{expected}"'
if clinic is not None:
clinic.add_include('pycore_modsupport.h', '_PyArg_BadArgument()')
self.add_include('pycore_modsupport.h', '_PyArg_BadArgument()')
return f'_PyArg_BadArgument("{{{{name}}}}", "{displayname}", {expected}, {{argname}});'

def format_code(self, fmt: str, *,
Expand Down Expand Up @@ -3336,9 +3336,8 @@ def parser_name(self) -> str:

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

type_checks = {
'&PyLong_Type': ('PyLong_Check', 'int'),
Expand Down