Skip to content

Commit 2733b20

Browse files
committed
pythongh-93018: Fix for the compatibility problems with expat
This is another take on python#93022, fixes python#93018. 1. I believe it is inappropriate to assert on the error message for the external API. Error messages are not part of the provided contract with its users. 2. I don't think we are really care what exact exception is thrown, it is much more important that non-well-formed XML is discovered. 3. Changing tests based on the version of library seems as a code smell to me, but that's personal opinion.
1 parent e37ac5f commit 2733b20

File tree

2 files changed

+10
-15
lines changed

2 files changed

+10
-15
lines changed

Lib/test/test_minidom.py

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,14 +1163,10 @@ def testEncodings(self):
11631163

11641164
# Verify that character decoding errors raise exceptions instead
11651165
# of crashing
1166-
if pyexpat.version_info >= (2, 4, 5):
1167-
self.assertRaises(ExpatError, parseString,
1168-
b'<fran\xe7ais></fran\xe7ais>')
1169-
self.assertRaises(ExpatError, parseString,
1170-
b'<franais>Comment \xe7a va ? Tr\xe8s bien ?</franais>')
1171-
else:
1172-
self.assertRaises(UnicodeDecodeError, parseString,
1173-
b'<fran\xe7ais>Comment \xe7a va ? Tr\xe8s bien ?</fran\xe7ais>')
1166+
with self.assertRaises((UnicodeDecodeError, ExpatError)):
1167+
parseString(
1168+
b'<fran\xe7ais>Comment \xe7a va ? Tr\xe8s bien ?</fran\xe7ais>'
1169+
)
11741170

11751171
doc.unlink()
11761172

@@ -1631,13 +1627,11 @@ def testEmptyXMLNSValue(self):
16311627
self.confirm(doc2.namespaceURI == xml.dom.EMPTY_NAMESPACE)
16321628

16331629
def testExceptionOnSpacesInXMLNSValue(self):
1634-
if pyexpat.version_info >= (2, 4, 5):
1635-
context = self.assertRaisesRegex(ExpatError, 'syntax error')
1636-
else:
1637-
context = self.assertRaisesRegex(ValueError, 'Unsupported syntax')
1638-
1639-
with context:
1640-
parseString('<element xmlns:abc="http:abc.com/de f g/hi/j k"><abc:foo /></element>')
1630+
with self.assertRaises((ValueError, ExpatError)):
1631+
parseString(
1632+
'<element xmlns:abc="http:abc.com/de f g/hi/j k">' +
1633+
'<abc:foo /></element>'
1634+
)
16411635

16421636
def testDocRemoveChild(self):
16431637
doc = parse(tstfile)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix for the compatibility problems with expat made more robust.

0 commit comments

Comments
 (0)