1010import html
1111import io
1212import itertools
13- import locale
1413import operator
1514import os
1615import pickle
@@ -960,15 +959,13 @@ def test_tostring_xml_declaration(self):
960959
961960 def test_tostring_xml_declaration_unicode_encoding (self ):
962961 elem = ET .XML ('<body><tag/></body>' )
963- preferredencoding = locale .getpreferredencoding ()
964962 self .assertEqual (
965- f"<?xml version='1.0' encoding='{ preferredencoding } '?> \n <body><tag /></body>" ,
966- ET . tostring ( elem , encoding = 'unicode' , xml_declaration = True )
963+ ET . tostring ( elem , encoding = 'unicode' , xml_declaration = True ) ,
964+ "<?xml version='1.0' encoding='utf-8'?> \n <body><tag /></body>"
967965 )
968966
969967 def test_tostring_xml_declaration_cases (self ):
970968 elem = ET .XML ('<body><tag>ø</tag></body>' )
971- preferredencoding = locale .getpreferredencoding ()
972969 TESTCASES = [
973970 # (expected_retval, encoding, xml_declaration)
974971 # ... xml_declaration = None
@@ -995,7 +992,7 @@ def test_tostring_xml_declaration_cases(self):
995992 b"<body><tag>ø</tag></body>" , 'US-ASCII' , True ),
996993 (b"<?xml version='1.0' encoding='ISO-8859-1'?>\n "
997994 b"<body><tag>\xf8 </tag></body>" , 'ISO-8859-1' , True ),
998- (f "<?xml version='1.0' encoding='{ preferredencoding } '?>\n "
995+ ("<?xml version='1.0' encoding='utf-8 '?>\n "
999996 "<body><tag>ø</tag></body>" , 'unicode' , True ),
1000997
1001998 ]
@@ -1033,11 +1030,10 @@ def test_tostringlist_xml_declaration(self):
10331030 b"<?xml version='1.0' encoding='us-ascii'?>\n <body><tag /></body>"
10341031 )
10351032
1036- preferredencoding = locale .getpreferredencoding ()
10371033 stringlist = ET .tostringlist (elem , encoding = 'unicode' , xml_declaration = True )
10381034 self .assertEqual (
10391035 '' .join (stringlist ),
1040- f "<?xml version='1.0' encoding='{ preferredencoding } '?>\n <body><tag /></body>"
1036+ "<?xml version='1.0' encoding='utf-8 '?>\n <body><tag /></body>"
10411037 )
10421038 self .assertRegex (stringlist [0 ], r"^<\?xml version='1.0' encoding='.+'?>" )
10431039 self .assertEqual (['<body' , '>' , '<tag' , ' />' , '</body>' ], stringlist [1 :])
@@ -3681,17 +3677,16 @@ def test_write_to_filename_as_unicode(self):
36813677 encoding = f .encoding
36823678 support .unlink (TESTFN )
36833679
3684- try :
3685- '\xf8 ' .encode (encoding )
3686- except UnicodeEncodeError :
3687- self .skipTest (f'default file encoding { encoding } not supported' )
3688-
36893680 tree = ET .ElementTree (ET .XML ('''<site>\xf8 </site>''' ))
36903681 tree .write (TESTFN , encoding = 'unicode' )
36913682 with open (TESTFN , 'rb' ) as f :
36923683 data = f .read ()
36933684 expected = "<site>\xf8 </site>" .encode (encoding , 'xmlcharrefreplace' )
3694- self .assertEqual (data , expected )
3685+ if encoding .lower () in ('utf-8' , 'ascii' ):
3686+ self .assertEqual (data , expected )
3687+ else :
3688+ self .assertIn (b"<?xml version='1.0' encoding=" , data )
3689+ self .assertIn (expected , data )
36953690
36963691 def test_write_to_text_file (self ):
36973692 self .addCleanup (support .unlink , TESTFN )
@@ -3706,13 +3701,17 @@ def test_write_to_text_file(self):
37063701 tree .write (f , encoding = 'unicode' )
37073702 self .assertFalse (f .closed )
37083703 with open (TESTFN , 'rb' ) as f :
3709- self .assertEqual (f .read (), b'''<site>ø</site>''' )
3704+ self .assertEqual (f .read (), convlinesep (
3705+ b'''<?xml version='1.0' encoding='ascii'?>\n '''
3706+ b'''<site>ø</site>''' ))
37103707
37113708 with open (TESTFN , 'w' , encoding = 'ISO-8859-1' ) as f :
37123709 tree .write (f , encoding = 'unicode' )
37133710 self .assertFalse (f .closed )
37143711 with open (TESTFN , 'rb' ) as f :
3715- self .assertEqual (f .read (), b'''<site>\xf8 </site>''' )
3712+ self .assertEqual (f .read (), convlinesep (
3713+ b'''<?xml version='1.0' encoding='ISO-8859-1'?>\n '''
3714+ b'''<site>\xf8 </site>''' ))
37163715
37173716 def test_write_to_binary_file (self ):
37183717 self .addCleanup (support .unlink , TESTFN )
0 commit comments