@@ -2779,7 +2779,7 @@ class CConverter(metaclass=CConverterAutoRegister):
2779
2779
# Only used by the 'O!' format unit (and the "object" converter).
2780
2780
subclass_of : str | None = None
2781
2781
2782
- # Do we want an adjacent '_length' variable for this variable?
2782
+ # See also the 'length_name' property.
2783
2783
# Only used by format units ending with '#'.
2784
2784
length = False
2785
2785
@@ -2873,12 +2873,12 @@ def _render_self(self, parameter: Parameter, data: CRenderData) -> None:
2873
2873
s = ("&" if self .impl_by_reference else "" ) + name
2874
2874
data .impl_arguments .append (s )
2875
2875
if self .length :
2876
- data .impl_arguments .append (self .length_name () )
2876
+ data .impl_arguments .append (self .length_name )
2877
2877
2878
2878
# impl_parameters
2879
2879
data .impl_parameters .append (self .simple_declaration (by_reference = self .impl_by_reference ))
2880
2880
if self .length :
2881
- data .impl_parameters .append ("Py_ssize_t " + self .length_name () )
2881
+ data .impl_parameters .append (f "Py_ssize_t { self .length_name } " )
2882
2882
2883
2883
def _render_non_self (
2884
2884
self ,
@@ -2937,6 +2937,7 @@ def render(self, parameter: Parameter, data: CRenderData) -> None:
2937
2937
self ._render_self (parameter , data )
2938
2938
self ._render_non_self (parameter , data )
2939
2939
2940
+ @functools .cached_property
2940
2941
def length_name (self ) -> str :
2941
2942
"""Computes the name of the associated "length" variable."""
2942
2943
assert self .length is not None
@@ -2960,7 +2961,7 @@ def parse_argument(self, args: list[str]) -> None:
2960
2961
args .append (s )
2961
2962
2962
2963
if self .length :
2963
- args .append ("&" + self .length_name () )
2964
+ args .append (f"& { self .length_name } " )
2964
2965
2965
2966
#
2966
2967
# All the functions after here are intended as extension points.
@@ -3005,9 +3006,8 @@ def declaration(self, *, in_parser: bool = False) -> str:
3005
3006
declaration .append (default )
3006
3007
declaration .append (";" )
3007
3008
if self .length :
3008
- declaration .append ('\n Py_ssize_t ' )
3009
- declaration .append (self .length_name ())
3010
- declaration .append (';' )
3009
+ declaration .append ('\n ' )
3010
+ declaration .append (f"Py_ssize_t { self .length_name } ;" )
3011
3011
return "" .join (declaration )
3012
3012
3013
3013
def initialize (self ) -> str :
@@ -3686,29 +3686,29 @@ def parse_arg(self, argname: str, displayname: str) -> str | None:
3686
3686
_PyArg_BadArgument("{{name}}", {displayname}, "str", {argname});
3687
3687
goto exit;
3688
3688
}}}}
3689
- Py_ssize_t {paramname}_length ;
3690
- {paramname} = PyUnicode_AsUTF8AndSize({argname}, &{paramname}_length );
3689
+ Py_ssize_t {length_name} ;
3690
+ {paramname} = PyUnicode_AsUTF8AndSize({argname}, &{length_name} );
3691
3691
if ({paramname} == NULL) {{{{
3692
3692
goto exit;
3693
3693
}}}}
3694
- if (strlen({paramname}) != (size_t){paramname}_length ) {{{{
3694
+ if (strlen({paramname}) != (size_t){length_name} ) {{{{
3695
3695
PyErr_SetString(PyExc_ValueError, "embedded null character");
3696
3696
goto exit;
3697
3697
}}}}
3698
3698
""" .format (argname = argname , paramname = self .parser_name ,
3699
- displayname = displayname )
3699
+ displayname = displayname , length_name = self . length_name )
3700
3700
if self .format_unit == 'z' :
3701
3701
return """
3702
3702
if ({argname} == Py_None) {{{{
3703
3703
{paramname} = NULL;
3704
3704
}}}}
3705
3705
else if (PyUnicode_Check({argname})) {{{{
3706
- Py_ssize_t {paramname}_length ;
3707
- {paramname} = PyUnicode_AsUTF8AndSize({argname}, &{paramname}_length );
3706
+ Py_ssize_t {length_name} ;
3707
+ {paramname} = PyUnicode_AsUTF8AndSize({argname}, &{length_name} );
3708
3708
if ({paramname} == NULL) {{{{
3709
3709
goto exit;
3710
3710
}}}}
3711
- if (strlen({paramname}) != (size_t){paramname}_length ) {{{{
3711
+ if (strlen({paramname}) != (size_t){length_name} ) {{{{
3712
3712
PyErr_SetString(PyExc_ValueError, "embedded null character");
3713
3713
goto exit;
3714
3714
}}}}
@@ -3718,7 +3718,7 @@ def parse_arg(self, argname: str, displayname: str) -> str | None:
3718
3718
goto exit;
3719
3719
}}}}
3720
3720
""" .format (argname = argname , paramname = self .parser_name ,
3721
- displayname = displayname )
3721
+ displayname = displayname , length_name = self . length_name )
3722
3722
return super ().parse_arg (argname , displayname )
3723
3723
3724
3724
#
0 commit comments