summaryrefslogtreecommitdiffstats
path: root/Tools/clinic
diff options
context:
space:
mode:
authorErlend E. Aasland <erlend@python.org>2023-08-04 17:49:07 (GMT)
committerGitHub <noreply@github.com>2023-08-04 17:49:07 (GMT)
commit321f0f79325adfe7656645060c2008d5779e1a7f (patch)
tree660b68265d77adf08eb621b3de8ffb39abcaba12 /Tools/clinic
parent400835ea1626c8c6dcd967c7eabe0dad4a923182 (diff)
downloadcpython-321f0f79325adfe7656645060c2008d5779e1a7f.zip
cpython-321f0f79325adfe7656645060c2008d5779e1a7f.tar.gz
cpython-321f0f79325adfe7656645060c2008d5779e1a7f.tar.bz2
gh-104683: Argument Clinic: Use CConverter.length_name where possible (#107638)
Also make it a cached property. Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Diffstat (limited to 'Tools/clinic')
-rwxr-xr-xTools/clinic/clinic.py30
1 files changed, 15 insertions, 15 deletions
diff --git a/Tools/clinic/clinic.py b/Tools/clinic/clinic.py
index 6eb2c55..917f1bf 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 @@ class CConverter(metaclass=CConverterAutoRegister):
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 @@ class CConverter(metaclass=CConverterAutoRegister):
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 @@ class CConverter(metaclass=CConverterAutoRegister):
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 @@ class CConverter(metaclass=CConverterAutoRegister):
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 @@ class str_converter(CConverter):
_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 @@ class str_converter(CConverter):
goto exit;
}}}}
""".format(argname=argname, paramname=self.parser_name,
- displayname=displayname)
+ displayname=displayname, length_name=self.length_name)
return super().parse_arg(argname, displayname)
#