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-122943: Add the varpos parameter in _PyArg_UnpackKeywords #126564

Merged

Conversation

serhiy-storchaka
Copy link
Member

@serhiy-storchaka serhiy-storchaka commented Nov 8, 2024

Remove _PyArg_UnpackKeywordsWithVararg.
Add comments for integer arguments of _PyArg_UnpackKeywords.

This is a follow up of #122945. It was not included in #122945 to minimize the diff.

Remove _PyArg_UnpackKeywordsWithVararg.
Add comments for integer arguments of _PyArg_UnpackKeywords.
Copy link
Contributor

@erlend-aasland erlend-aasland left a comment

Choose a reason for hiding this comment

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

Neat. Consider this style amendment for IMO slightly more readable generated code:

diff
diff --git a/Tools/clinic/libclinic/parse_args.py b/Tools/clinic/libclinic/parse_args.py
index 8b7afbcf3e4..8d1714b1ebe 100644
--- a/Tools/clinic/libclinic/parse_args.py
+++ b/Tools/clinic/libclinic/parse_args.py
@@ -695,13 +695,21 @@ def parse_general(self, clang: CLanguage) -> None:
                 if has_optional_kw:
                     self.declarations += "\nPy_ssize_t noptargs = %s + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - %d;" % (nargs, self.min_pos + self.min_kw_only)
                 unpack_args = '_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL'
-            parser_code = [libclinic.normalize_snippet(f"""
-                {argsname} = _PyArg_UnpackKeywords({unpack_args}, &_parser,
-                        /*minpos*/ {self.min_pos}, /*maxpos*/ {self.max_pos}, /*minkw*/ {self.min_kw_only}, /*varpos*/ {1 if self.varpos else 0}, argsbuf);
+            snippet = f"""
+                {argsname} = _PyArg_UnpackKeywords(
+                    {unpack_args}, &_parser,
+                    /*minpos*/ {self.min_pos},
+                    /*maxpos*/ {self.max_pos},
+                    /*minkw*/ {self.min_kw_only},
+                    /*varpos*/ {1 if self.varpos else 0},
+                    argsbuf
+                );
                 if (!{argsname}) {{{{
                     goto exit;
                 }}}}
-                """, indent=4)]
+            """
+            snippet = libclinic.normalize_snippet(snippet, indent=4)
+            parser_code = [snippet]
 
         if self.requires_defining_class:
             self.flags = 'METH_METHOD|' + self.flags

@skirpichev
Copy link
Member

Consider this style amendment for IMO slightly more readable generated code:

(This is why auto-formatting for clinic output (with indent-like tool) might be not a bad idea.)

But if we are going for multi-line code snippet, lets add also spaces to comments: /*minpos*/ -> /* minpos */.

@erlend-aasland
Copy link
Contributor

erlend-aasland commented Nov 8, 2024

(This is why auto-formatting for clinic output (with indent-like tool) might be not a bad idea.)

We already do auto-formatting of clinic output1; see Tools/clinic/libclinic/formatting.py. But I don't think coercing Argument Clinic to produce "100% correct PEP-7 code" is good use of anyones time. Time is better spent on resolving real Argument Clinic issues.

Footnotes

  1. its just that some parts of the generator is not producing very readable code (yet)

@serhiy-storchaka
Copy link
Member Author

Consider this style amendment for IMO slightly more readable generated code:

It would be less readable to me. I have vision problems so I get lost in very scattered code.

But if we are going for multi-line code snippet, lets add also spaces to comments: /*minpos*/ -> /* minpos */.

I initially wrote it as /* min_pos */, but it was too noisy. It was more difficult to find the code between comments.

@serhiy-storchaka serhiy-storchaka merged commit 061e50f into python:main Nov 8, 2024
51 checks passed
@serhiy-storchaka serhiy-storchaka deleted the clinic-_PyArg_UnpackKeywords branch November 8, 2024 12:23
picnixz pushed a commit to picnixz/cpython that referenced this pull request Dec 8, 2024
…ythonGH-126564)

Remove _PyArg_UnpackKeywordsWithVararg.
Add comments for integer arguments of _PyArg_UnpackKeywords.
ebonnal pushed a commit to ebonnal/cpython that referenced this pull request Jan 12, 2025
…ythonGH-126564)

Remove _PyArg_UnpackKeywordsWithVararg.
Add comments for integer arguments of _PyArg_UnpackKeywords.
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