diff --git a/Lib/test/clinic.test.c b/Lib/test/clinic.test.c index 3b59db65f23b6f..6d0b2563fdb969 100644 --- a/Lib/test/clinic.test.c +++ b/Lib/test/clinic.test.c @@ -2043,11 +2043,11 @@ test_str_converter(PyObject *module, PyObject *const *args, Py_ssize_t nargs) const char *d = "cef"; const char *e = "gh"; const char *f = "ij"; - Py_ssize_t f_length; + Py_ssize_t f_length = 2; const char *g = "kl"; - Py_ssize_t g_length; + Py_ssize_t g_length = 2; const char *h = "mn"; - Py_ssize_t h_length; + Py_ssize_t h_length = 2; if (!PyArg_ParseArray(args, nargs, "|sssyzy#s#z#:test_str_converter", &a, &b, &c, &d, &e, &f, &f_length, &g, &g_length, &h, &h_length)) { @@ -2065,7 +2065,7 @@ test_str_converter_impl(PyObject *module, const char *a, const char *b, const char *f, Py_ssize_t f_length, const char *g, Py_ssize_t g_length, const char *h, Py_ssize_t h_length) -/*[clinic end generated code: output=58dc0fad26cd4c5d input=8afe9da8185cd38c]*/ +/*[clinic end generated code: output=e251efc38d275ad6 input=8afe9da8185cd38c]*/ /*[clinic input] diff --git a/Lib/test/test_clinic.py b/Lib/test/test_clinic.py index 4e0b0137e9faa0..8017f468331e9c 100644 --- a/Lib/test/test_clinic.py +++ b/Lib/test/test_clinic.py @@ -1711,6 +1711,17 @@ def test_param_default_parameters_out_of_order(self): """ self.expect_failure(block, err, lineno=3) + def test_param_optional_str_length_without_c_default(self): + err = ( + "str_converter: cannot compute the default value for 'a_length'" + ) + block = """ + module os + os.access + a: str(accept={str, robuffer}, zeroes=True, c_default="some_global") = unknown + """ + self.expect_failure(block, err, lineno=2) + def disabled_test_converter_arguments(self): function = self.parse_function(""" module os @@ -4909,6 +4920,8 @@ def test_str_converter(self): ac_tester.str_converter('a', 'b', 'c') with self.assertRaises(ValueError): ac_tester.str_converter('a', b'b\0b', 'c') + self.assertEqual(ac_tester.str_converter(), ('a', 'b', 'c')) + self.assertEqual(ac_tester.str_converter('a', b'b'), ('a', 'b', 'c')) self.assertEqual(ac_tester.str_converter('a', b'b', 'c'), ('a', 'b', 'c')) self.assertEqual(ac_tester.str_converter('a', b'b', b'c'), ('a', 'b', 'c')) self.assertEqual(ac_tester.str_converter('a', b'b', 'c\0c'), ('a', 'b', 'c\0c')) diff --git a/Misc/NEWS.d/next/Tools-Demos/2026-11-16-10-27-44.gh-issue-162098.AE-wik.rst b/Misc/NEWS.d/next/Tools-Demos/2026-11-16-10-27-44.gh-issue-162098.AE-wik.rst new file mode 100644 index 00000000000000..d1938908bee729 --- /dev/null +++ b/Misc/NEWS.d/next/Tools-Demos/2026-11-16-10-27-44.gh-issue-162098.AE-wik.rst @@ -0,0 +1 @@ +Fix Argument Clinic to initialize the string length to match the default value when an optional string argument is omitted. diff --git a/Modules/clinic/_testclinic.c.h b/Modules/clinic/_testclinic.c.h index dcc60c7341cbc3..a2f3708a25ca1a 100644 --- a/Modules/clinic/_testclinic.c.h +++ b/Modules/clinic/_testclinic.c.h @@ -1528,7 +1528,7 @@ str_converter(PyObject *module, PyObject *const *args, Py_ssize_t nargs) const char *a = "a"; const char *b = "b"; const char *c = "c"; - Py_ssize_t c_length; + Py_ssize_t c_length = 1; if (!PyArg_ParseArray(args, nargs, "|sys#:str_converter", &a, &b, &c, &c_length)) { @@ -5309,4 +5309,4 @@ vc_kwonly_vectorcall(PyObject *type, PyObject *const *args, kwnames ? PyTuple_GET_SIZE(kwnames) : 0, NULL, kwnames); } -/*[clinic end generated code: output=b54b37db716469cd input=a9049054013a1b77]*/ +/*[clinic end generated code: output=1824c307178c9611 input=a9049054013a1b77]*/ diff --git a/Modules/clinic/_testclinic_depr.c.h b/Modules/clinic/_testclinic_depr.c.h index 1652e9223e54f9..74e45dec658952 100644 --- a/Modules/clinic/_testclinic_depr.c.h +++ b/Modules/clinic/_testclinic_depr.c.h @@ -410,7 +410,7 @@ depr_star_init_noinline(PyObject *self, PyObject *args, PyObject *kwargs) PyObject *b; PyObject *c = Py_None; const char *d = ""; - Py_ssize_t d_length; + Py_ssize_t d_length = 0; if (nargs > 1 && nargs <= 3) { if (PyErr_WarnEx(PyExc_DeprecationWarning, @@ -654,7 +654,7 @@ depr_kwd_init_noinline(PyObject *self, PyObject *args, PyObject *kwargs) PyObject *b; PyObject *c = Py_None; const char *d = ""; - Py_ssize_t d_length; + Py_ssize_t d_length = 0; if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, &a, &b, &c, &d, &d_length)) { @@ -1515,7 +1515,7 @@ depr_star_noinline(PyObject *module, PyObject *const *args, Py_ssize_t nargs, Py PyObject *b; PyObject *c = Py_None; const char *d = ""; - Py_ssize_t d_length; + Py_ssize_t d_length = 0; if (nargs > 1 && nargs <= 3) { if (PyErr_WarnEx(PyExc_DeprecationWarning, @@ -2242,7 +2242,7 @@ depr_kwd_noinline(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyO PyObject *b; PyObject *c = Py_None; const char *d = ""; - Py_ssize_t d_length; + Py_ssize_t d_length = 0; if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser, &a, &b, &c, &d, &d_length)) { @@ -2682,4 +2682,4 @@ depr_multi(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject * exit: return return_value; } -/*[clinic end generated code: output=abe0af8d8c637c36 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=17d7bf6d51872e2c input=a9049054013a1b77]*/ diff --git a/Tools/clinic/libclinic/converter.py b/Tools/clinic/libclinic/converter.py index 2f9f54f7e29df4..f1fe55d5e646c3 100644 --- a/Tools/clinic/libclinic/converter.py +++ b/Tools/clinic/libclinic/converter.py @@ -286,6 +286,13 @@ def converter_init(self) -> None: def c_default_init(self) -> None: return + def length_c_default(self) -> str | None: + """ + The C expression the associated "length" variable is initialized + with, or None if the converter cannot compute it. + """ + return None + # An alternative name of a preceding parameter: they share # the same C variable. alias_of: Parameter | None = None @@ -445,7 +452,17 @@ def declaration(self, *, in_parser: bool = False) -> str: declaration.append(";") if self.length: declaration.append('\n') - declaration.append(f"Py_ssize_t {self.length_name};") + declaration.append(f"Py_ssize_t {self.length_name}") + # The parser only writes the length when the argument is + # passed, so an optional parameter needs its length + # initialized from the default value. + if self.is_optional(): + length_c_default = self.length_c_default() + if length_c_default is None: + fail(f"{self.__class__.__name__}: cannot compute " + f"the default value for {self.length_name!r}") + declaration.append(f" = {length_c_default}") + declaration.append(";") return "".join(declaration) def initialize(self) -> str: diff --git a/Tools/clinic/libclinic/converters.py b/Tools/clinic/libclinic/converters.py index e3571eb597f24a..fe3c605f8f824b 100644 --- a/Tools/clinic/libclinic/converters.py +++ b/Tools/clinic/libclinic/converters.py @@ -828,6 +828,18 @@ def c_default_init(self) -> None: if self.default is None: self.c_default = "NULL" + def length_c_default(self) -> str | None: + # The byte length of the default value; the parser only writes + # it when the argument is passed. + default = self.default + if default is NULL or default is None: + return "0" + elif isinstance(default, str): + return str(len(default.encode('utf-8'))) + elif isinstance(default, bytes): + return str(len(default)) + return None + def post_parsing(self) -> str: if self.encoding: name = self.name @@ -1065,6 +1077,10 @@ def converter_init( fail(f"Py_UNICODE_converter: illegal 'accept' argument {accept!r}") self.c_default = "NULL" + def length_c_default(self) -> str | None: + # The C default value is always NULL. + return "0" + def cleanup(self) -> str: if self.length: return ""