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
@@ -960,15 +959,13 @@ def test_tostring_xml_declaration(self):
960
959
961
960
def test_tostring_xml_declaration_unicode_encoding (self ):
962
961
elem = ET .XML ('<body><tag/></body>' )
963
- preferredencoding = locale .getpreferredencoding ()
964
962
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>"
967
965
)
968
966
969
967
def test_tostring_xml_declaration_cases (self ):
970
968
elem = ET .XML ('<body><tag>ø</tag></body>' )
971
- preferredencoding = locale .getpreferredencoding ()
972
969
TESTCASES = [
973
970
# (expected_retval, encoding, xml_declaration)
974
971
# ... xml_declaration = None
@@ -995,7 +992,7 @@ def test_tostring_xml_declaration_cases(self):
995
992
b"<body><tag>ø</tag></body>" , 'US-ASCII' , True ),
996
993
(b"<?xml version='1.0' encoding='ISO-8859-1'?>\n "
997
994
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 "
999
996
"<body><tag>ø</tag></body>" , 'unicode' , True ),
1000
997
1001
998
]
@@ -1033,11 +1030,10 @@ def test_tostringlist_xml_declaration(self):
1033
1030
b"<?xml version='1.0' encoding='us-ascii'?>\n <body><tag /></body>"
1034
1031
)
1035
1032
1036
- preferredencoding = locale .getpreferredencoding ()
1037
1033
stringlist = ET .tostringlist (elem , encoding = 'unicode' , xml_declaration = True )
1038
1034
self .assertEqual (
1039
1035
'' .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>"
1041
1037
)
1042
1038
self .assertRegex (stringlist [0 ], r"^<\?xml version='1.0' encoding='.+'?>" )
1043
1039
self .assertEqual (['<body' , '>' , '<tag' , ' />' , '</body>' ], stringlist [1 :])
@@ -3681,17 +3677,16 @@ def test_write_to_filename_as_unicode(self):
3681
3677
encoding = f .encoding
3682
3678
support .unlink (TESTFN )
3683
3679
3684
- try :
3685
- '\xf8 ' .encode (encoding )
3686
- except UnicodeEncodeError :
3687
- self .skipTest (f'default file encoding { encoding } not supported' )
3688
-
3689
3680
tree = ET .ElementTree (ET .XML ('''<site>\xf8 </site>''' ))
3690
3681
tree .write (TESTFN , encoding = 'unicode' )
3691
3682
with open (TESTFN , 'rb' ) as f :
3692
3683
data = f .read ()
3693
3684
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 )
3695
3690
3696
3691
def test_write_to_text_file (self ):
3697
3692
self .addCleanup (support .unlink , TESTFN )
@@ -3706,13 +3701,17 @@ def test_write_to_text_file(self):
3706
3701
tree .write (f , encoding = 'unicode' )
3707
3702
self .assertFalse (f .closed )
3708
3703
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>''' ))
3710
3707
3711
3708
with open (TESTFN , 'w' , encoding = 'ISO-8859-1' ) as f :
3712
3709
tree .write (f , encoding = 'unicode' )
3713
3710
self .assertFalse (f .closed )
3714
3711
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>''' ))
3716
3715
3717
3716
def test_write_to_binary_file (self ):
3718
3717
self .addCleanup (support .unlink , TESTFN )
0 commit comments