@@ -668,15 +668,18 @@ def test_writestring(self):
668668 elem = ET .fromstring ("<html><body>text</body></html>" )
669669 self .assertEqual (ET .tostring (elem ), b'<html><body>text</body></html>' )
670670
671- def test_encoding (encoding ):
672- def check (encoding ):
673- ET .XML ("<?xml version='1.0' encoding='%s'?><xml />" % encoding )
674- check ("ascii" )
675- check ("us-ascii" )
676- check ("iso-8859-1" )
677- check ("iso-8859-15" )
678- check ("cp437" )
679- check ("mac-roman" )
671+ def test_encoding (self ):
672+ def check (encoding , body = '' ):
673+ xml = ("<?xml version='1.0' encoding='%s'?><xml>%s</xml>" %
674+ (encoding , body ))
675+ self .assertEqual (ET .XML (xml .encode (encoding )).text , body )
676+ self .assertEqual (ET .XML (xml ).text , body )
677+ check ("ascii" , 'a' )
678+ check ("us-ascii" , 'a' )
679+ check ("iso-8859-1" , '\xbd ' )
680+ check ("iso-8859-15" , '\u20ac ' )
681+ check ("cp437" , '\u221a ' )
682+ check ("mac-roman" , '\u02da ' )
680683
681684 def test_methods (self ):
682685 # Test serialization methods.
@@ -2002,11 +2005,13 @@ def close(self):
20022005
20032006
20042007class XMLParserTest (unittest .TestCase ):
2005- sample1 = '<file><line>22</line></file>'
2006- sample2 = ('<!DOCTYPE html PUBLIC'
2007- ' "-//W3C//DTD XHTML 1.0 Transitional//EN"'
2008- ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'
2009- '<html>text</html>' )
2008+ sample1 = b'<file><line>22</line></file>'
2009+ sample2 = (b'<!DOCTYPE html PUBLIC'
2010+ b' "-//W3C//DTD XHTML 1.0 Transitional//EN"'
2011+ b' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'
2012+ b'<html>text</html>' )
2013+ sample3 = ('<?xml version="1.0" encoding="iso-8859-1"?>\n '
2014+ '<money value="$\xa3 \u20ac \U0001017b ">$\xa3 \u20ac \U0001017b </money>' )
20102015
20112016 def _check_sample_element (self , e ):
20122017 self .assertEqual (e .tag , 'file' )
@@ -2042,12 +2047,21 @@ def doctype(self, name, pubid, system):
20422047 _doctype = (name , pubid , system )
20432048
20442049 parser = MyParserWithDoctype ()
2045- parser .feed (self .sample2 )
2050+ with self .assertWarns (DeprecationWarning ):
2051+ parser .feed (self .sample2 )
20462052 parser .close ()
20472053 self .assertEqual (_doctype ,
20482054 ('html' , '-//W3C//DTD XHTML 1.0 Transitional//EN' ,
20492055 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd' ))
20502056
2057+ def test_parse_string (self ):
2058+ parser = ET .XMLParser (target = ET .TreeBuilder ())
2059+ parser .feed (self .sample3 )
2060+ e = parser .close ()
2061+ self .assertEqual (e .tag , 'money' )
2062+ self .assertEqual (e .attrib ['value' ], '$\xa3 \u20ac \U0001017b ' )
2063+ self .assertEqual (e .text , '$\xa3 \u20ac \U0001017b ' )
2064+
20512065
20522066class NamespaceParseTest (unittest .TestCase ):
20532067 def test_find_with_namespace (self ):
@@ -2473,6 +2487,7 @@ def test_main(module=None):
24732487 ElementFindTest ,
24742488 ElementIterTest ,
24752489 TreeBuilderTest ,
2490+ XMLParserTest ,
24762491 BugsTest ,
24772492 ]
24782493
0 commit comments