[Python-checkins] gh-104683: Argument Clinic: Use CConverter.length_name where possible (#107638)

erlend-aasland webhook-mailer at python.org
Fri Aug 4 13:49:11 EDT 2023


https://github.com/python/cpython/commit/321f0f79325adfe7656645060c2008d5779e1a7f
commit: 321f0f79325adfe7656645060c2008d5779e1a7f
branch: main
author: Erlend E. Aasland <erlend at python.org>
committer: erlend-aasland <erlend.aasland at protonmail.com>
date: 2023-08-04T17:49:07Z
summary:

gh-104683: Argument Clinic: Use CConverter.length_name where possible (#107638)

Also make it a cached property.

Co-authored-by: Alex Waygood <Alex.Waygood at Gmail.com>

files:
M Tools/clinic/clinic.py

diff --git a/Tools/clinic/clinic.py b/Tools/clinic/clinic.py
index 6eb2c550e696f..917f1bfeb1d25 100755
--- a/Tools/clinic/clinic.py
+++ b/Tools/clinic/clinic.py
@@ -2779,7 +2779,7 @@ class CConverter(metaclass=CConverterAutoRegister):
     # Only used by the 'O!' format unit (and the "object" converter).
     subclass_of: str | None = None
 
-    # Do we want an adjacent '_length' variable for this variable?
+    # See also the 'length_name' property.
     # Only used by format units ending with '#'.
     length = False
 
@@ -2873,12 +2873,12 @@ def _render_self(self, parameter: Parameter, data: CRenderData) -> None:
         s = ("&" if self.impl_by_reference else "") + name
         data.impl_arguments.append(s)
         if self.length:
-            data.impl_arguments.append(self.length_name())
+            data.impl_arguments.append(self.length_name)
 
         # impl_parameters
         data.impl_parameters.append(self.simple_declaration(by_reference=self.impl_by_reference))
         if self.length:
-            data.impl_parameters.append("Py_ssize_t " + self.length_name())
+            data.impl_parameters.append(f"Py_ssize_t {self.length_name}")
 
     def _render_non_self(
             self,
@@ -2937,6 +2937,7 @@ def render(self, parameter: Parameter, data: CRenderData) -> None:
         self._render_self(parameter, data)
         self._render_non_self(parameter, data)
 
+    @functools.cached_property
     def length_name(self) -> str:
         """Computes the name of the associated "length" variable."""
         assert self.length is not None
@@ -2960,7 +2961,7 @@ def parse_argument(self, args: list[str]) -> None:
         args.append(s)
 
         if self.length:
-            args.append("&" + self.length_name())
+            args.append(f"&{self.length_name}")
 
     #
     # All the functions after here are intended as extension points.
@@ -3005,9 +3006,8 @@ def declaration(self, *, in_parser: bool = False) -> str:
             declaration.append(default)
         declaration.append(";")
         if self.length:
-            declaration.append('\nPy_ssize_t ')
-            declaration.append(self.length_name())
-            declaration.append(';')
+            declaration.append('\n')
+            declaration.append(f"Py_ssize_t {self.length_name};")
         return "".join(declaration)
 
     def initialize(self) -> str:
@@ -3686,29 +3686,29 @@ def parse_arg(self, argname: str, displayname: str) -> str | None:
                     _PyArg_BadArgument("{{name}}", {displayname}, "str", {argname});
                     goto exit;
                 }}}}
-                Py_ssize_t {paramname}_length;
-                {paramname} = PyUnicode_AsUTF8AndSize({argname}, &{paramname}_length);
+                Py_ssize_t {length_name};
+                {paramname} = PyUnicode_AsUTF8AndSize({argname}, &{length_name});
                 if ({paramname} == NULL) {{{{
                     goto exit;
                 }}}}
-                if (strlen({paramname}) != (size_t){paramname}_length) {{{{
+                if (strlen({paramname}) != (size_t){length_name}) {{{{
                     PyErr_SetString(PyExc_ValueError, "embedded null character");
                     goto exit;
                 }}}}
                 """.format(argname=argname, paramname=self.parser_name,
-                           displayname=displayname)
+                           displayname=displayname, length_name=self.length_name)
         if self.format_unit == 'z':
             return """
                 if ({argname} == Py_None) {{{{
                     {paramname} = NULL;
                 }}}}
                 else if (PyUnicode_Check({argname})) {{{{
-                    Py_ssize_t {paramname}_length;
-                    {paramname} = PyUnicode_AsUTF8AndSize({argname}, &{paramname}_length);
+                    Py_ssize_t {length_name};
+                    {paramname} = PyUnicode_AsUTF8AndSize({argname}, &{length_name});
                     if ({paramname} == NULL) {{{{
                         goto exit;
                     }}}}
-                    if (strlen({paramname}) != (size_t){paramname}_length) {{{{
+                    if (strlen({paramname}) != (size_t){length_name}) {{{{
                         PyErr_SetString(PyExc_ValueError, "embedded null character");
                         goto exit;
                     }}}}
@@ -3718,7 +3718,7 @@ def parse_arg(self, argname: str, displayname: str) -> str | None:
                     goto exit;
                 }}}}
                 """.format(argname=argname, paramname=self.parser_name,
-                           displayname=displayname)
+                           displayname=displayname, length_name=self.length_name)
         return super().parse_arg(argname, displayname)
 
 #



More information about the Python-checkins mailing list