From e72e55d1fc7ecdaa77b254d08b9ccd4d5cd0fec0 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Thu, 14 May 2026 11:29:51 +0300 Subject: [PATCH 1/3] gh-148821: Add more tests for invalid XML encodings --- Lib/test/test_pyexpat.py | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/Lib/test/test_pyexpat.py b/Lib/test/test_pyexpat.py index 4fe2e02326f04f..7cfe46311f981d 100644 --- a/Lib/test/test_pyexpat.py +++ b/Lib/test/test_pyexpat.py @@ -341,6 +341,46 @@ def test_unsupportes_ecodings(self, encoding): with self.assertRaises(ValueError): parser.Parse(data, True) + parser = expat.ParserCreate() + data = (f'\n' + '').encode() + with self.assertRaises(ValueError): + parser.Parse(data, True) + + @support.subTests('encoding', [ + 'cp037', 'cp273', 'cp424', 'cp500', 'cp864', 'cp875', + 'cp1026', 'cp1140', + 'mac_arabic', 'mac_farsi', + ]) + def test_incompatible_ecodings(self, encoding): + parser = expat.ParserCreate() + data = (f'\n' + '').encode(encoding) + with self.assertRaises(expat.ExpatError): + parser.Parse(data, True) + + parser = expat.ParserCreate() + data = (f'\n' + '').encode() + with self.assertRaisesRegex(expat.ExpatError, 'unknown encoding'): + parser.Parse(data, True) + + @support.subTests('encoding', [ + 'hex_codec', 'rot_13', + ]) + def test_non_text_ecodings(self, encoding): + parser = expat.ParserCreate() + data = (f'\n' + '').encode() + with self.assertRaises(LookupError): + parser.Parse(data, True) + + def test_undefined_ecoding(self): + parser = expat.ParserCreate() + data = b'\n' + with self.assertRaises(UnicodeError): + parser.Parse(data, True) + def test_unknown_ecoding(self): parser = expat.ParserCreate() data = b'\n' From 45faf341873b9002cf168184fbfce3fb746edb10 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Thu, 14 May 2026 12:04:40 +0300 Subject: [PATCH 2/3] Fix typos. --- Lib/test/test_pyexpat.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Lib/test/test_pyexpat.py b/Lib/test/test_pyexpat.py index 7cfe46311f981d..6f0470931ddb27 100644 --- a/Lib/test/test_pyexpat.py +++ b/Lib/test/test_pyexpat.py @@ -289,7 +289,7 @@ def test_parse_again(self): 'mac-roman', 'mac-turkish', 'koi8-r', 'koi8-t', 'koi8-u', 'kz1048', 'ptcp154', ]) - def test_supported_ecodings(self, encoding): + def test_supported_encodings(self, encoding): out = self.Outputter() parser = expat.ParserCreate() self._hookup_callbacks(parser, out) @@ -308,7 +308,7 @@ def test_supported_ecodings(self, encoding): 'UTF-8', 'utf-8', 'utf-16', 'utf-16le', 'utf-16be', 'koi8-u', 'cp1125', 'cp1251', 'iso8859-5', 'mac-cyrillic', ]) - def test_supported_ecodings2(self, encoding): + def test_supported_encodings2(self, encoding): out = self.Outputter() parser = expat.ParserCreate() self._hookup_callbacks(parser, out) @@ -334,7 +334,7 @@ def test_supported_ecodings2(self, encoding): "johab", "Shift_JIS", "Shift_JIS-2004", "Shift_JISX0213", ]) - def test_unsupportes_ecodings(self, encoding): + def test_unsupportes_encodings(self, encoding): parser = expat.ParserCreate() data = (f'\n' '').encode(encoding) @@ -352,7 +352,7 @@ def test_unsupportes_ecodings(self, encoding): 'cp1026', 'cp1140', 'mac_arabic', 'mac_farsi', ]) - def test_incompatible_ecodings(self, encoding): + def test_incompatible_encodings(self, encoding): parser = expat.ParserCreate() data = (f'\n' '').encode(encoding) @@ -368,20 +368,20 @@ def test_incompatible_ecodings(self, encoding): @support.subTests('encoding', [ 'hex_codec', 'rot_13', ]) - def test_non_text_ecodings(self, encoding): + def test_non_text_encodings(self, encoding): parser = expat.ParserCreate() data = (f'\n' '').encode() with self.assertRaises(LookupError): parser.Parse(data, True) - def test_undefined_ecoding(self): + def test_undefined_encoding(self): parser = expat.ParserCreate() data = b'\n' with self.assertRaises(UnicodeError): parser.Parse(data, True) - def test_unknown_ecoding(self): + def test_unknown_encoding(self): parser = expat.ParserCreate() data = b'\n' with self.assertRaises(LookupError): From a9df12167d95141933e224af1c38126ebbaa31c0 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Thu, 14 May 2026 12:29:42 +0300 Subject: [PATCH 3/3] Update Lib/test/test_pyexpat.py Co-authored-by: Stan Ulbrych --- Lib/test/test_pyexpat.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_pyexpat.py b/Lib/test/test_pyexpat.py index 6f0470931ddb27..10dca684accee3 100644 --- a/Lib/test/test_pyexpat.py +++ b/Lib/test/test_pyexpat.py @@ -334,7 +334,7 @@ def test_supported_encodings2(self, encoding): "johab", "Shift_JIS", "Shift_JIS-2004", "Shift_JISX0213", ]) - def test_unsupportes_encodings(self, encoding): + def test_unsupported_encodings(self, encoding): parser = expat.ParserCreate() data = (f'\n' '').encode(encoding)