1010import html
1111import io
1212import itertools
13- import locale
1413import operator
1514import os
1615import pickle
@@ -978,15 +977,13 @@ def test_tostring_xml_declaration(self):
978977
979978 def test_tostring_xml_declaration_unicode_encoding (self ):
980979 elem = ET .XML ('<body><tag/></body>' )
981- preferredencoding = locale .getpreferredencoding ()
982980 self .assertEqual (
983- f"<?xml version='1.0' encoding='{ preferredencoding } '?> \n <body><tag /></body>" ,
984- ET . tostring ( elem , encoding = 'unicode' , xml_declaration = True )
981+ ET . tostring ( elem , encoding = 'unicode' , xml_declaration = True ) ,
982+ "<?xml version='1.0' encoding='utf-8'?> \n <body><tag /></body>"
985983 )
986984
987985 def test_tostring_xml_declaration_cases (self ):
988986 elem = ET .XML ('<body><tag>ø</tag></body>' )
989- preferredencoding = locale .getpreferredencoding ()
990987 TESTCASES = [
991988 # (expected_retval, encoding, xml_declaration)
992989 # ... xml_declaration = None
@@ -1013,7 +1010,7 @@ def test_tostring_xml_declaration_cases(self):
10131010 b"<body><tag>ø</tag></body>" , 'US-ASCII' , True ),
10141011 (b"<?xml version='1.0' encoding='ISO-8859-1'?>\n "
10151012 b"<body><tag>\xf8 </tag></body>" , 'ISO-8859-1' , True ),
1016- (f "<?xml version='1.0' encoding='{ preferredencoding } '?>\n "
1013+ ("<?xml version='1.0' encoding='utf-8 '?>\n "
10171014 "<body><tag>ø</tag></body>" , 'unicode' , True ),
10181015
10191016 ]
@@ -1051,11 +1048,10 @@ def test_tostringlist_xml_declaration(self):
10511048 b"<?xml version='1.0' encoding='us-ascii'?>\n <body><tag /></body>"
10521049 )
10531050
1054- preferredencoding = locale .getpreferredencoding ()
10551051 stringlist = ET .tostringlist (elem , encoding = 'unicode' , xml_declaration = True )
10561052 self .assertEqual (
10571053 '' .join (stringlist ),
1058- f "<?xml version='1.0' encoding='{ preferredencoding } '?>\n <body><tag /></body>"
1054+ "<?xml version='1.0' encoding='utf-8 '?>\n <body><tag /></body>"
10591055 )
10601056 self .assertRegex (stringlist [0 ], r"^<\?xml version='1.0' encoding='.+'?>" )
10611057 self .assertEqual (['<body' , '>' , '<tag' , ' />' , '</body>' ], stringlist [1 :])
@@ -3740,17 +3736,16 @@ def test_write_to_filename_as_unicode(self):
37403736 encoding = f .encoding
37413737 os_helper .unlink (TESTFN )
37423738
3743- try :
3744- '\xf8 ' .encode (encoding )
3745- except UnicodeEncodeError :
3746- self .skipTest (f'default file encoding { encoding } not supported' )
3747-
37483739 tree = ET .ElementTree (ET .XML ('''<site>\xf8 </site>''' ))
37493740 tree .write (TESTFN , encoding = 'unicode' )
37503741 with open (TESTFN , 'rb' ) as f :
37513742 data = f .read ()
37523743 expected = "<site>\xf8 </site>" .encode (encoding , 'xmlcharrefreplace' )
3753- self .assertEqual (data , expected )
3744+ if encoding .lower () in ('utf-8' , 'ascii' ):
3745+ self .assertEqual (data , expected )
3746+ else :
3747+ self .assertIn (b"<?xml version='1.0' encoding=" , data )
3748+ self .assertIn (expected , data )
37543749
37553750 def test_write_to_text_file (self ):
37563751 self .addCleanup (os_helper .unlink , TESTFN )
@@ -3765,13 +3760,17 @@ def test_write_to_text_file(self):
37653760 tree .write (f , encoding = 'unicode' )
37663761 self .assertFalse (f .closed )
37673762 with open (TESTFN , 'rb' ) as f :
3768- self .assertEqual (f .read (), b'''<site>ø</site>''' )
3763+ self .assertEqual (f .read (), convlinesep (
3764+ b'''<?xml version='1.0' encoding='ascii'?>\n '''
3765+ b'''<site>ø</site>''' ))
37693766
37703767 with open (TESTFN , 'w' , encoding = 'ISO-8859-1' ) as f :
37713768 tree .write (f , encoding = 'unicode' )
37723769 self .assertFalse (f .closed )
37733770 with open (TESTFN , 'rb' ) as f :
3774- self .assertEqual (f .read (), b'''<site>\xf8 </site>''' )
3771+ self .assertEqual (f .read (), convlinesep (
3772+ b'''<?xml version='1.0' encoding='ISO-8859-1'?>\n '''
3773+ b'''<site>\xf8 </site>''' ))
37753774
37763775 def test_write_to_binary_file (self ):
37773776 self .addCleanup (os_helper .unlink , TESTFN )
0 commit comments