Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-122888: Fix crash on certain calls to str() #122889

Merged
merged 4 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
39 changes: 32 additions & 7 deletions Lib/test/test_str.py
Original file line number Diff line number Diff line change
Expand Up @@ -2655,19 +2655,44 @@ def test_str_invalid_call(self):
check = lambda *a, **kw: self.assertRaises(TypeError, str, *a, **kw)
serhiy-storchaka marked this conversation as resolved.
Show resolved Hide resolved

# too many args
check(1, "", "", 1)
with self.assertRaisesRegex(TypeError, r"str expected at most 3 arguments, got 4"):
str("too", "many", "argu", "ments")
with self.assertRaisesRegex(TypeError, r"str expected at most 3 arguments, got 4"):
str(1, "", "", 1)

# no such kw arg
check(test=1)
with self.assertRaisesRegex(TypeError, r"str\(\) got an unexpected keyword argument 'test'"):
str(test=1)

# 'encoding' must be str
check(1, encoding=1)
check(1, 1)
with self.assertRaisesRegex(TypeError, r"str\(\) argument 'encoding' must be str, not int"):
str(1, 1)
with self.assertRaisesRegex(TypeError, r"str\(\) argument 'encoding' must be str, not int"):
str(1, encoding=1)
with self.assertRaisesRegex(TypeError, r"str\(\) argument 'encoding' must be str, not bytes"):
str(b"x", b"ascii")
with self.assertRaisesRegex(TypeError, r"str\(\) argument 'encoding' must be str, not bytes"):
str(b"x", encoding=b"ascii")

# 'errors' must be str
check(1, errors=1)
check(1, "", errors=1)
check(1, 1, 1)
with self.assertRaisesRegex(TypeError, r"str\(\) argument 'encoding' must be str, not int"):
str(1, 1, 1)
with self.assertRaisesRegex(TypeError, r"str\(\) argument 'errors' must be str, not int"):
str(1, errors=1)
with self.assertRaisesRegex(TypeError, r"str\(\) argument 'errors' must be str, not int"):
str(1, "", errors=1)
with self.assertRaisesRegex(TypeError, r"str\(\) argument 'errors' must be str, not bytes"):
str(b"x", "ascii", b"strict")
with self.assertRaisesRegex(TypeError, r"str\(\) argument 'errors' must be str, not bytes"):
str(b"x", "ascii", errors=b"strict")

# both positional and kwarg
with self.assertRaisesRegex(TypeError, r"argument for str\(\) given by name \('encoding'\) and position \(2\)"):
str(b"x", "utf-8", encoding="ascii")
with self.assertRaisesRegex(TypeError, r"str\(\) takes at most 3 arguments \(4 given\)"):
str(b"x", "utf-8", "ignore", encoding="ascii")
with self.assertRaisesRegex(TypeError, r"str\(\) takes at most 3 arguments \(4 given\)"):
str(b"x", "utf-8", "strict", errors="ignore")


class StringModuleTest(unittest.TestCase):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix crash on certain calls to ``str()`` with positional arguments of the
wrong type. Patch by Jelle Zijlstra.
11 changes: 10 additions & 1 deletion Objects/unicodeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -15121,7 +15121,16 @@ unicode_vectorcall(PyObject *type, PyObject *const *args,
return PyObject_Str(object);
}
const char *encoding = arg_as_utf8(args[1], "encoding");
const char *errors = (nargs == 3) ? arg_as_utf8(args[2], "errors") : NULL;
if (encoding == NULL) {
return NULL;
}
const char *errors = NULL;
if (nargs == 3) {
errors = arg_as_utf8(args[2], "errors");
if (errors == NULL) {
return NULL;
}
}
return PyUnicode_FromEncodedObject(object, encoding, errors);
}

Expand Down
Loading