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-108494: Fix AC limited C API for kwargs #108516

Closed
wants to merge 1 commit into from

Conversation

vstinner
Copy link
Member

@vstinner vstinner commented Aug 26, 2023

Fix the limited C API code path in Argument Clinic to parse "positional or keywords" arguments.

Add an unit test in _testclinic_limited.

Fix the limited C API code path in Argument Clinic to parse
"positional or keywords" arguments.

Add an unit test in _testclinic_limited.
@vstinner
Copy link
Member Author

@erlend-aasland @AlexWaygood @serhiy-storchaka: Here is a fix for PyArg_ParseTupleAndKeywords(). It removes the unused argname_fmt in the PyArg_ParseTuple() code path.

""", indent=4)]
declarations = ""

else:
Copy link
Member

Choose a reason for hiding this comment

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

Does it work if requires_defining_class is true?

Copy link
Member Author

Choose a reason for hiding this comment

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

No, it doesn't. This change is incomplete, it can be completed later. Or do you mean that an exception should be raised instead of an compilation error?

Copy link
Member

Choose a reason for hiding this comment

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

Currently it can produce invalid code which is compiled. There are two opposite approaches:

  1. Raise an exception or ensure that the compiling the generated code will get a compilation error.
  2. Try to use the limited API if possible, but fallback to private API otherwise.

I am currently trying the second approach. It allows to re-generate all clinic code using the limited API and test that at least it works (I found many cases in which the current code does not generate working code). Please hold this PR, the current code is more suitable for this experiment. After testing it will be easy to switch to the first approach.

Copy link
Member Author

Choose a reason for hiding this comment

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

A module targeting the limited C API cannot use the private API. It fails at build.

# positional-only for the limited C API
flags = "METH_VARARGS"
elif clinic.limited_capi:
if not requires_defining_class and pos_only == len(parameters) - pseudo_args:
Copy link
Member

Choose a reason for hiding this comment

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

Does it work if pseudo_args is not zero?

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 have no idea. I don't know what are pseudo args.

{parse_arguments}))
goto exit;
""", indent=4)]
declarations = "char* _keywords[] = {{{keywords_c} NULL}};"
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
declarations = "char* _keywords[] = {{{keywords_c} NULL}};"
declarations = "static char *_keywords[] = {{{keywords_c} NULL}};"

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 reallly understand the difference between the two. The value is constant so the variable should not really exist on the stack, no?

Copy link
Member

Choose a reason for hiding this comment

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

There is a difference. It is an array of pointers. If it is a local variable, it should fill with initial values every time the function is called. If it is a static variable, it is only filled with initial values at the program's start (or when the function is called first time).

@serhiy-storchaka
Copy link
Member

See #108536.

@vstinner
Copy link
Member Author

Does your PR make this one useless, or should it be merged first?

@serhiy-storchaka
Copy link
Member

I think that it makes it useless, maybe except the test. But for testing I want to reuse existing tests, so the current Modules/_testclinic_limited.c will become obsolete too.

@vstinner
Copy link
Member Author

Serhiy's PR got merged and it's way more complete, I close this PR: #108536

@vstinner vstinner closed this Aug 28, 2023
@vstinner vstinner deleted the clinic_limited_kwds branch August 28, 2023 14:23
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.

3 participants