-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
gh-108494: Fix AC limited C API for kwargs #108516
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -1250,18 +1250,32 @@ def parser_body( | |||||
parser_prototype = self.PARSER_PROTOTYPE_VARARGS | ||||||
parser_definition = parser_body(parser_prototype, ' {option_group_parsing}') | ||||||
|
||||||
elif not requires_defining_class and pos_only == len(parameters) - pseudo_args and clinic.limited_capi: | ||||||
# 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: | ||||||
# positional-only for the limited C API: | ||||||
# PyArg_ParseTuple() | ||||||
flags = "METH_VARARGS" | ||||||
|
||||||
parser_prototype = self.PARSER_PROTOTYPE_VARARGS | ||||||
parser_code = [normalize_snippet(""" | ||||||
if (!PyArg_ParseTuple(args, "{format_units}:{name}", | ||||||
{parse_arguments})) | ||||||
goto exit; | ||||||
""", indent=4)] | ||||||
argname_fmt = 'args[%d]' | ||||||
declarations = "" | ||||||
parser_prototype = self.PARSER_PROTOTYPE_VARARGS | ||||||
parser_code = [normalize_snippet(""" | ||||||
if (!PyArg_ParseTuple(args, "{format_units}:{name}", | ||||||
{parse_arguments})) | ||||||
goto exit; | ||||||
""", indent=4)] | ||||||
declarations = "" | ||||||
|
||||||
else: | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does it work if There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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:
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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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-or-keyword arguments for the limited C API: | ||||||
# PyArg_ParseTupleAndKeywords() | ||||||
flags = "METH_VARARGS|METH_KEYWORDS" | ||||||
|
||||||
parser_prototype = self.PARSER_PROTOTYPE_KEYWORD | ||||||
parser_code = [normalize_snippet(""" | ||||||
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "{format_units}:{name}", _keywords, | ||||||
{parse_arguments})) | ||||||
goto exit; | ||||||
""", indent=4)] | ||||||
declarations = "char* _keywords[] = {{{keywords_c} NULL}};" | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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). |
||||||
|
||||||
parser_definition = parser_body(parser_prototype, *parser_code, | ||||||
declarations=declarations) | ||||||
|
@@ -1384,20 +1398,7 @@ def parser_body( | |||||
) | ||||||
nargs = f"Py_MIN(nargs, {max_pos})" if max_pos else "0" | ||||||
|
||||||
if clinic.limited_capi: | ||||||
# positional-or-keyword arguments | ||||||
flags = "METH_VARARGS|METH_KEYWORDS" | ||||||
|
||||||
parser_prototype = self.PARSER_PROTOTYPE_KEYWORD | ||||||
parser_code = [normalize_snippet(""" | ||||||
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "{format_units}:{name}", _keywords, | ||||||
{parse_arguments})) | ||||||
goto exit; | ||||||
""", indent=4)] | ||||||
argname_fmt = 'args[%d]' | ||||||
declarations = "" | ||||||
|
||||||
elif not new_or_init: | ||||||
if not new_or_init: | ||||||
flags = "METH_FASTCALL|METH_KEYWORDS" | ||||||
parser_prototype = self.PARSER_PROTOTYPE_FASTCALL_KEYWORDS | ||||||
argname_fmt = 'args[%d]' | ||||||
|
There was a problem hiding this comment.
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?There was a problem hiding this comment.
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.