Skip to content
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

gh-107603: Argument Clinic can emit includes #108486

Merged
merged 3 commits into from
Aug 25, 2023

Conversation

vstinner
Copy link
Member

@vstinner vstinner commented Aug 25, 2023

Remove _PyLong_UnsignedShort_Converter() from the public C API: move the function to the internal pycore_long.h header.

  • Add Clinic.add_include() method
  • Add CConverter.include and CConverter.add_include()
  • Printer.print_block() gets a second parameter: clinic.

@vstinner
Copy link
Member Author

cc @erlend-aasland @AlexWaygood

I included _PyLong_UnsignedShort_Converter() removal in this PR to show how the feature can be used, but once reviewed, I can consider to extract this change into a separated PR.

Tools/clinic/clinic.py Outdated Show resolved Hide resolved
core_includes: bool = False
core_includes: bool = False,
# needed if core_includes is true
clinic: Clinic | None = None,
Copy link
Member

Choose a reason for hiding this comment

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

Should we just pass clinic.includes here, rather than the full clinic object?

Copy link
Member Author

Choose a reason for hiding this comment

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

I prefer to pass the Clinic class, it may useful later to get more attributes. But I don't understand well the AC design. IMO the whole idea of passing Clinic here is broken, it should not be the role of a "BlockPrinter" to generate these #include :-( Well, I prefer to move step by step, since AC is complicated and (for me) hard to modify.

Copy link
Member

Choose a reason for hiding this comment

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

Hmm, I agree with @AA-Turner here: I'd prefer if we only passed exactly what we need, rather than passing the whole clinic object. I think it would make the code more explicit, and make the function easier to understand just by looking at its signature

Copy link
Member

Choose a reason for hiding this comment

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

Opened #108581 as a sketch.

A

Tools/clinic/clinic.py Outdated Show resolved Hide resolved
Comment on lines +2162 to +2178
line = f'#include "{include}"'
line = line.ljust(35) + f'// {reason}\n'
Copy link
Member

Choose a reason for hiding this comment

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

Should we add a comment to explain the indent amount?

Suggested change
line = f'#include "{include}"'
line = line.ljust(35) + f'// {reason}\n'
include = f'"{include}"'
line = f'#include {include: <25} // {reason}\n'

Copy link
Member Author

Choose a reason for hiding this comment

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

35 is my arbitrary convention that I'm using in Python. I prefer to keep ljust(35) since it reminds me the 35th column :-)

Comment on lines +2436 to +2454
if name in self.includes:
# Mention a single reason is enough, no need to list all of them
Copy link
Member

Choose a reason for hiding this comment

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

For generated code we could list all the known reasons?

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't see the point. It would generate giant lines for little value.

For #include, I like to show at least one imported symbol to explain why the include was needed. In manually written code, I use it to remove unused imports time to time. IMO here it's interesting to repeat this habit to explain why an include is needed.

Tools/clinic/clinic.py Outdated Show resolved Hide resolved
Tools/clinic/clinic.py Outdated Show resolved Hide resolved
Tools/clinic/clinic.py Outdated Show resolved Hide resolved
* Add Clinic.add_include() method
* Add CConverter.include and CConverter.add_include()
* Printer.print_block() gets a second parameter: clinic.
* Remove duplicated declaration of "clinic" global variable.
Always require clinic.
@vstinner
Copy link
Member Author

I rebased my PR on top of the merge AC change adding support for the limited C API.

@vstinner vstinner merged commit 73d33c1 into python:main Aug 25, 2023
@vstinner vstinner deleted the clinic_includes branch August 25, 2023 22:01
@vstinner
Copy link
Member Author

As a follow-up, I wrote PR #108499 which removes private _PyLong converters from the public C API: move them to the internal C API. The change adds #include "pycore_include.h" to the few clinic files using it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants