mirror of https://github.com/python/cpython
[3.13] gh-122888: Fix crash on certain calls to str() (GH-122889) (#122947)
Fixes GH-122888
(cherry picked from commit 53ebb6232a
)
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
This commit is contained in:
parent
8b64ce4eb4
commit
55aede7342
|
@ -1736,8 +1736,6 @@ class StrTest(string_tests.StringLikeTest,
|
||||||
'character buffers are decoded to unicode'
|
'character buffers are decoded to unicode'
|
||||||
)
|
)
|
||||||
|
|
||||||
self.assertRaises(TypeError, str, 42, 42, 42)
|
|
||||||
|
|
||||||
def test_constructor_keyword_args(self):
|
def test_constructor_keyword_args(self):
|
||||||
"""Pass various keyword argument combinations to the constructor."""
|
"""Pass various keyword argument combinations to the constructor."""
|
||||||
# The object argument can be passed as a keyword.
|
# The object argument can be passed as a keyword.
|
||||||
|
@ -2652,22 +2650,45 @@ class StrTest(string_tests.StringLikeTest,
|
||||||
self.assertEqual(proc.rc, 10, proc)
|
self.assertEqual(proc.rc, 10, proc)
|
||||||
|
|
||||||
def test_str_invalid_call(self):
|
def test_str_invalid_call(self):
|
||||||
check = lambda *a, **kw: self.assertRaises(TypeError, str, *a, **kw)
|
|
||||||
|
|
||||||
# too many args
|
# 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
|
# 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
|
# 'encoding' must be str
|
||||||
check(1, encoding=1)
|
with self.assertRaisesRegex(TypeError, r"str\(\) argument 'encoding' must be str, not int"):
|
||||||
check(1, 1)
|
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
|
# 'errors' must be str
|
||||||
check(1, errors=1)
|
with self.assertRaisesRegex(TypeError, r"str\(\) argument 'encoding' must be str, not int"):
|
||||||
check(1, "", errors=1)
|
str(1, 1, 1)
|
||||||
check(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):
|
class StringModuleTest(unittest.TestCase):
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
Fix crash on certain calls to ``str()`` with positional arguments of the
|
||||||
|
wrong type. Patch by Jelle Zijlstra.
|
|
@ -14817,7 +14817,16 @@ unicode_vectorcall(PyObject *type, PyObject *const *args,
|
||||||
return PyObject_Str(object);
|
return PyObject_Str(object);
|
||||||
}
|
}
|
||||||
const char *encoding = arg_as_utf8(args[1], "encoding");
|
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);
|
return PyUnicode_FromEncodedObject(object, encoding, errors);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue