Skip to content

Commit 31ef2b6

Browse files
[3.14] gh-148821: Add more tests for invalid XML encodings (GH-149820) (GH-149822)
(cherry picked from commit c6f7368) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
1 parent 5f8fdda commit 31ef2b6

1 file changed

Lines changed: 44 additions & 4 deletions

File tree

Lib/test/test_pyexpat.py

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ def test_parse_again(self):
290290
'mac-roman', 'mac-turkish',
291291
'koi8-r', 'koi8-t', 'koi8-u', 'kz1048', 'ptcp154',
292292
])
293-
def test_supported_ecodings(self, encoding):
293+
def test_supported_encodings(self, encoding):
294294
out = self.Outputter()
295295
parser = expat.ParserCreate()
296296
self._hookup_callbacks(parser, out)
@@ -309,7 +309,7 @@ def test_supported_ecodings(self, encoding):
309309
'UTF-8', 'utf-8', 'utf-16', 'utf-16le', 'utf-16be',
310310
'koi8-u', 'cp1125', 'cp1251', 'iso8859-5', 'mac-cyrillic',
311311
])
312-
def test_supported_ecodings2(self, encoding):
312+
def test_supported_encodings2(self, encoding):
313313
out = self.Outputter()
314314
parser = expat.ParserCreate()
315315
self._hookup_callbacks(parser, out)
@@ -335,14 +335,54 @@ def test_supported_ecodings2(self, encoding):
335335
"johab",
336336
"Shift_JIS", "Shift_JIS-2004", "Shift_JISX0213",
337337
])
338-
def test_unsupportes_ecodings(self, encoding):
338+
def test_unsupported_encodings(self, encoding):
339339
parser = expat.ParserCreate()
340340
data = (f'<?xml version="1.0" encoding="{encoding}"?>\n'
341341
'<root></root>').encode(encoding)
342342
with self.assertRaises(ValueError):
343343
parser.Parse(data, True)
344344

345-
def test_unknown_ecoding(self):
345+
parser = expat.ParserCreate()
346+
data = (f'<?xml version="1.0" encoding="{encoding}"?>\n'
347+
'<root></root>').encode()
348+
with self.assertRaises(ValueError):
349+
parser.Parse(data, True)
350+
351+
@support.subTests('encoding', [
352+
'cp037', 'cp273', 'cp424', 'cp500', 'cp864', 'cp875',
353+
'cp1026', 'cp1140',
354+
'mac_arabic', 'mac_farsi',
355+
])
356+
def test_incompatible_encodings(self, encoding):
357+
parser = expat.ParserCreate()
358+
data = (f'<?xml version="1.0" encoding="{encoding}"?>\n'
359+
'<root></root>').encode(encoding)
360+
with self.assertRaises(expat.ExpatError):
361+
parser.Parse(data, True)
362+
363+
parser = expat.ParserCreate()
364+
data = (f'<?xml version="1.0" encoding="{encoding}"?>\n'
365+
'<root></root>').encode()
366+
with self.assertRaisesRegex(expat.ExpatError, 'unknown encoding'):
367+
parser.Parse(data, True)
368+
369+
@support.subTests('encoding', [
370+
'hex_codec', 'rot_13',
371+
])
372+
def test_non_text_encodings(self, encoding):
373+
parser = expat.ParserCreate()
374+
data = (f'<?xml version="1.0" encoding="{encoding}"?>\n'
375+
'<root></root>').encode()
376+
with self.assertRaises(LookupError):
377+
parser.Parse(data, True)
378+
379+
def test_undefined_encoding(self):
380+
parser = expat.ParserCreate()
381+
data = b'<?xml version="1.0" encoding="undefined"?>\n<root></root>'
382+
with self.assertRaises(UnicodeError):
383+
parser.Parse(data, True)
384+
385+
def test_unknown_encoding(self):
346386
parser = expat.ParserCreate()
347387
data = b'<?xml version="1.0" encoding="xyz"?>\n<root></root>'
348388
with self.assertRaises(LookupError):

0 commit comments

Comments
 (0)