@@ -677,15 +677,18 @@ def test_writestring(self):
677677 elem = ET .fromstring ("<html><body>text</body></html>" )
678678 self .assertEqual (ET .tostring (elem ), b'<html><body>text</body></html>' )
679679
680- def test_encoding (encoding ):
681- def check (encoding ):
682- ET .XML ("<?xml version='1.0' encoding='%s'?><xml />" % encoding )
683- check ("ascii" )
684- check ("us-ascii" )
685- check ("iso-8859-1" )
686- check ("iso-8859-15" )
687- check ("cp437" )
688- check ("mac-roman" )
680+ def test_encoding (self ):
681+ def check (encoding , body = '' ):
682+ xml = ("<?xml version='1.0' encoding='%s'?><xml>%s</xml>" %
683+ (encoding , body ))
684+ self .assertEqual (ET .XML (xml .encode (encoding )).text , body )
685+ self .assertEqual (ET .XML (xml ).text , body )
686+ check ("ascii" , 'a' )
687+ check ("us-ascii" , 'a' )
688+ check ("iso-8859-1" , '\xbd ' )
689+ check ("iso-8859-15" , '\u20ac ' )
690+ check ("cp437" , '\u221a ' )
691+ check ("mac-roman" , '\u02da ' )
689692
690693 def test_methods (self ):
691694 # Test serialization methods.
@@ -1842,11 +1845,13 @@ def close(self):
18421845
18431846
18441847class XMLParserTest (unittest .TestCase ):
1845- sample1 = '<file><line>22</line></file>'
1846- sample2 = ('<!DOCTYPE html PUBLIC'
1847- ' "-//W3C//DTD XHTML 1.0 Transitional//EN"'
1848- ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'
1849- '<html>text</html>' )
1848+ sample1 = b'<file><line>22</line></file>'
1849+ sample2 = (b'<!DOCTYPE html PUBLIC'
1850+ b' "-//W3C//DTD XHTML 1.0 Transitional//EN"'
1851+ b' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'
1852+ b'<html>text</html>' )
1853+ sample3 = ('<?xml version="1.0" encoding="iso-8859-1"?>\n '
1854+ '<money value="$\xa3 \u20ac \U0001017b ">$\xa3 \u20ac \U0001017b </money>' )
18501855
18511856 def _check_sample_element (self , e ):
18521857 self .assertEqual (e .tag , 'file' )
@@ -1882,12 +1887,21 @@ def doctype(self, name, pubid, system):
18821887 _doctype = (name , pubid , system )
18831888
18841889 parser = MyParserWithDoctype ()
1885- parser .feed (self .sample2 )
1890+ with self .assertWarns (DeprecationWarning ):
1891+ parser .feed (self .sample2 )
18861892 parser .close ()
18871893 self .assertEqual (_doctype ,
18881894 ('html' , '-//W3C//DTD XHTML 1.0 Transitional//EN' ,
18891895 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd' ))
18901896
1897+ def test_parse_string (self ):
1898+ parser = ET .XMLParser (target = ET .TreeBuilder ())
1899+ parser .feed (self .sample3 )
1900+ e = parser .close ()
1901+ self .assertEqual (e .tag , 'money' )
1902+ self .assertEqual (e .attrib ['value' ], '$\xa3 \u20ac \U0001017b ' )
1903+ self .assertEqual (e .text , '$\xa3 \u20ac \U0001017b ' )
1904+
18911905
18921906class NamespaceParseTest (unittest .TestCase ):
18931907 def test_find_with_namespace (self ):
@@ -2297,6 +2311,7 @@ def test_main(module=None):
22972311 ElementFindTest ,
22982312 ElementIterTest ,
22992313 TreeBuilderTest ,
2314+ XMLParserTest ,
23002315 BugsTest ,
23012316 ]
23022317
0 commit comments