Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Lib/test/clinic.test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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]
Expand Down
13 changes: 13 additions & 0 deletions Lib/test/test_clinic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix Argument Clinic to initialize the string length to match the default value when an optional string argument is omitted.
4 changes: 2 additions & 2 deletions Modules/clinic/_testclinic.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions Modules/clinic/_testclinic_depr.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 18 additions & 1 deletion Tools/clinic/libclinic/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
16 changes: 16 additions & 0 deletions Tools/clinic/libclinic/converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 ""
Expand Down
Loading