Skip to content

gh-113317: AC converter: Use add_include() in bad_argument() #114324

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

Closed
wants to merge 1 commit into from
Closed
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
36 changes: 20 additions & 16 deletions Tools/clinic/clinic.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,11 @@ def warn_or_fail(
line_number: int | None = None,
) -> None:
joined = " ".join([str(a) for a in args])
if clinic:
if global_clinic:
if filename is None:
filename = clinic.filename
if getattr(clinic, 'block_parser', None) and (line_number is None):
line_number = clinic.block_parser.line_number
filename = global_clinic.filename
if getattr(global_clinic, 'block_parser', None) and (line_number is None):
line_number = global_clinic.block_parser.line_number
error = ClinicError(joined, filename=filename, lineno=line_number)
if fail:
raise error
Expand Down 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 @@ -1455,6 +1449,14 @@ def parser_body(
if compiler_warning:
parser_definition = compiler_warning + "\n\n" + parser_definition

# Copy converters includes at the end, since includes can be
# added late
for converter in converters:
include = converter.include
if include is not None:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that can be made an assert.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not all converters need a special include. Do you mean that include variable is never None?

clinic.add_include(include.filename, include.reason,
condition=include.condition)

d = {
"docstring_prototype" : docstring_prototype,
"docstring_definition" : docstring_definition,
Expand Down Expand Up @@ -2221,7 +2223,7 @@ def __init__(self, clinic: Clinic) -> None: ...
def parse(self, block: Block) -> None: ...


clinic: Clinic | None = None
global_clinic: Clinic | None = None
class Clinic:

presets_text = """
Expand Down Expand Up @@ -2346,8 +2348,8 @@ def __init__(
assert name in self.destination_buffers
preset[name] = buffer

global clinic
clinic = self
global global_clinic
global_clinic = self

def add_include(self, name: str, reason: str,
*, condition: str | None = None) -> None:
Expand Down Expand Up @@ -3263,8 +3265,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 +3337,12 @@ def parser_name(self) -> str:

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

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