10
10
import html
11
11
import io
12
12
import itertools
13
- import locale
14
13
import operator
15
14
import os
16
15
import pickle
@@ -978,15 +977,13 @@ def test_tostring_xml_declaration(self):
978
977
979
978
def test_tostring_xml_declaration_unicode_encoding (self ):
980
979
elem = ET .XML ('<body><tag/></body>' )
981
- preferredencoding = locale .getpreferredencoding ()
982
980
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>"
985
983
)
986
984
987
985
def test_tostring_xml_declaration_cases (self ):
988
986
elem = ET .XML ('<body><tag>ø</tag></body>' )
989
- preferredencoding = locale .getpreferredencoding ()
990
987
TESTCASES = [
991
988
# (expected_retval, encoding, xml_declaration)
992
989
# ... xml_declaration = None
@@ -1013,7 +1010,7 @@ def test_tostring_xml_declaration_cases(self):
1013
1010
b"<body><tag>ø</tag></body>" , 'US-ASCII' , True ),
1014
1011
(b"<?xml version='1.0' encoding='ISO-8859-1'?>\n "
1015
1012
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 "
1017
1014
"<body><tag>ø</tag></body>" , 'unicode' , True ),
1018
1015
1019
1016
]
@@ -1051,11 +1048,10 @@ def test_tostringlist_xml_declaration(self):
1051
1048
b"<?xml version='1.0' encoding='us-ascii'?>\n <body><tag /></body>"
1052
1049
)
1053
1050
1054
- preferredencoding = locale .getpreferredencoding ()
1055
1051
stringlist = ET .tostringlist (elem , encoding = 'unicode' , xml_declaration = True )
1056
1052
self .assertEqual (
1057
1053
'' .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>"
1059
1055
)
1060
1056
self .assertRegex (stringlist [0 ], r"^<\?xml version='1.0' encoding='.+'?>" )
1061
1057
self .assertEqual (['<body' , '>' , '<tag' , ' />' , '</body>' ], stringlist [1 :])
@@ -3740,17 +3736,16 @@ def test_write_to_filename_as_unicode(self):
3740
3736
encoding = f .encoding
3741
3737
os_helper .unlink (TESTFN )
3742
3738
3743
- try :
3744
- '\xf8 ' .encode (encoding )
3745
- except UnicodeEncodeError :
3746
- self .skipTest (f'default file encoding { encoding } not supported' )
3747
-
3748
3739
tree = ET .ElementTree (ET .XML ('''<site>\xf8 </site>''' ))
3749
3740
tree .write (TESTFN , encoding = 'unicode' )
3750
3741
with open (TESTFN , 'rb' ) as f :
3751
3742
data = f .read ()
3752
3743
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 )
3754
3749
3755
3750
def test_write_to_text_file (self ):
3756
3751
self .addCleanup (os_helper .unlink , TESTFN )
@@ -3765,13 +3760,17 @@ def test_write_to_text_file(self):
3765
3760
tree .write (f , encoding = 'unicode' )
3766
3761
self .assertFalse (f .closed )
3767
3762
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>''' ))
3769
3766
3770
3767
with open (TESTFN , 'w' , encoding = 'ISO-8859-1' ) as f :
3771
3768
tree .write (f , encoding = 'unicode' )
3772
3769
self .assertFalse (f .closed )
3773
3770
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>''' ))
3775
3774
3776
3775
def test_write_to_binary_file (self ):
3777
3776
self .addCleanup (os_helper .unlink , TESTFN )
0 commit comments