summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@python.org>2023-10-20 15:59:29 (GMT)
committerGitHub <noreply@github.com>2023-10-20 15:59:29 (GMT)
commitd731579bfb9a497cfb0076cb6b221058a20088fe (patch)
tree63eb8f9b8dfb7af9a5de6d4499ab375b80f208d4 /Lib
parent59ea0f523e155ac1a471cd292b41a76241fccd36 (diff)
downloadcpython-d731579bfb9a497cfb0076cb6b221058a20088fe.zip
cpython-d731579bfb9a497cfb0076cb6b221058a20088fe.tar.gz
cpython-d731579bfb9a497cfb0076cb6b221058a20088fe.tar.bz2
gh-111089: PyUnicode_AsUTF8() now raises on embedded NUL (#111091)
* PyUnicode_AsUTF8() now raises an exception if the string contains embedded null characters. * Update related C API tests (test_capi.test_unicode). * type_new_set_doc() uses PyUnicode_AsUTF8AndSize() to silently truncate doc containing null bytes. Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/test_capi/test_unicode.py5
1 files changed, 4 insertions, 1 deletions
diff --git a/Lib/test/test_capi/test_unicode.py b/Lib/test/test_capi/test_unicode.py
index 3ec27a2..8ab5590 100644
--- a/Lib/test/test_capi/test_unicode.py
+++ b/Lib/test/test_capi/test_unicode.py
@@ -882,7 +882,10 @@ class CAPITest(unittest.TestCase):
self.assertEqual(unicode_asutf8('abc', 4), b'abc\0')
self.assertEqual(unicode_asutf8('абв', 7), b'\xd0\xb0\xd0\xb1\xd0\xb2\0')
self.assertEqual(unicode_asutf8('\U0001f600', 5), b'\xf0\x9f\x98\x80\0')
- self.assertEqual(unicode_asutf8('abc\0def', 8), b'abc\0def\0')
+
+ # disallow embedded null characters
+ self.assertRaises(ValueError, unicode_asutf8, 'abc\0', 0)
+ self.assertRaises(ValueError, unicode_asutf8, 'abc\0def', 0)
self.assertRaises(UnicodeEncodeError, unicode_asutf8, '\ud8ff', 0)
self.assertRaises(TypeError, unicode_asutf8, b'abc', 0)