Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
10 changes: 10 additions & 0 deletions Lib/test/test_pyexpat.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,16 @@ def test_unknown_encoding(self):
with self.assertRaises(LookupError):
parser.Parse(data, True)

@support.subTests('sample,exception', [
(b'<x> \xa1</x>', UnicodeDecodeError), # crashed
(b'<x> \xa1</x', UnicodeDecodeError), # crashed
(b'<x> \xa1', expat.ExpatError),
])
def test_multibyte_encoding_errors(self, sample, exception):
parser = expat.ParserCreate()
data = b'<?xml version="1.0" encoding="EUC-JP"?>\n' + sample
with self.assertRaises(exception):
parser.Parse(data, True)

class NamespaceSeparatorTest(unittest.TestCase):
def test_legal(self):
Expand Down
11 changes: 11 additions & 0 deletions Lib/test/test_xml_etree.py
Original file line number Diff line number Diff line change
Expand Up @@ -1064,6 +1064,17 @@ def bxml(encoding, body=''):
self.assertRaises(ValueError, ET.XML, xml('undefined').encode('ascii'))
self.assertRaises(LookupError, ET.XML, xml('xxx').encode('ascii'))

@support.subTests('sample,exception', [
(b'<x> \xa1</x>', UnicodeDecodeError), # crashed
(b'<x> \xa1</x', UnicodeDecodeError), # crashed
(b'<x> \xa1', None), # ET.ParseError
])
def test_multibyte_encoding_errors(self, sample, exception):
exception = exception or ET.ParseError
data = b'<?xml version="1.0" encoding="EUC-JP"?>\n' + sample
with self.assertRaises(exception):
ET.XML(data)

def test_methods(self):
# Test serialization methods.

Expand Down
3 changes: 3 additions & 0 deletions Modules/pyexpat.c
Original file line number Diff line number Diff line change
Expand Up @@ -1473,6 +1473,9 @@ pyexpat_encoding_create(const char *name, PyObject *mapping)
static int
pyexpat_encoding_convert(void *data, const char *s)
{
if (PyErr_Occurred()) {
return -1;
}
pyexpat_encoding_info *info = (pyexpat_encoding_info *)data;
int i = (unsigned char)s[0];
assert(info->map[i] < -1);
Expand Down
Loading