@@ -71,14 +71,14 @@ def check_method(method):
7171 if not hasattr (method , '__call__' ):
7272 print (method , "not callable" )
7373
74- def serialize (elem , to_string = True , ** options ):
74+ def serialize (elem , to_string = True , encoding = 'unicode' , ** options ):
7575 import io
76- if options . get ( " encoding" ) :
76+ if encoding != 'unicode' :
7777 file = io .BytesIO ()
7878 else :
7979 file = io .StringIO ()
8080 tree = ET .ElementTree (elem )
81- tree .write (file , ** options )
81+ tree .write (file , encoding = encoding , ** options )
8282 if to_string :
8383 return file .getvalue ()
8484 else :
@@ -537,7 +537,7 @@ def attrib():
537537 >>> elem.set('testa', 'testval')
538538 >>> elem.set('testb', 'test2')
539539 >>> ET.tostring(elem)
540- '<test testa="testval" testb="test2">aa</test>'
540+ b '<test testa="testval" testb="test2">aa</test>'
541541 >>> sorted(elem.keys())
542542 ['testa', 'testb']
543543 >>> sorted(elem.items())
@@ -547,7 +547,7 @@ def attrib():
547547 >>> elem.attrib['testb'] = 'test1'
548548 >>> elem.attrib['testc'] = 'test2'
549549 >>> ET.tostring(elem)
550- '<test testa="testval" testb="test1" testc="test2">aa</test>'
550+ b '<test testa="testval" testb="test1" testc="test2">aa</test>'
551551 """
552552
553553def makeelement ():
@@ -587,15 +587,15 @@ def parsefile():
587587
588588 >>> tree = ET.parse(SIMPLE_XMLFILE)
589589 >>> normalize_crlf(tree)
590- >>> tree.write(sys.stdout)
590+ >>> tree.write(sys.stdout, encoding='unicode' )
591591 <root>
592592 <element key="value">text</element>
593593 <element>text</element>tail
594594 <empty-element />
595595 </root>
596596 >>> tree = ET.parse(SIMPLE_NS_XMLFILE)
597597 >>> normalize_crlf(tree)
598- >>> tree.write(sys.stdout)
598+ >>> tree.write(sys.stdout, encoding='unicode' )
599599 <ns0:root xmlns:ns0="namespace">
600600 <ns0:element key="value">text</ns0:element>
601601 <ns0:element>text</ns0:element>tail
@@ -636,17 +636,17 @@ def parsefile():
636636def parseliteral ():
637637 """
638638 >>> element = ET.XML("<html><body>text</body></html>")
639- >>> ET.ElementTree(element).write(sys.stdout)
639+ >>> ET.ElementTree(element).write(sys.stdout, encoding='unicode' )
640640 <html><body>text</body></html>
641641 >>> element = ET.fromstring("<html><body>text</body></html>")
642- >>> ET.ElementTree(element).write(sys.stdout)
642+ >>> ET.ElementTree(element).write(sys.stdout, encoding='unicode' )
643643 <html><body>text</body></html>
644644 >>> sequence = ["<html><body>", "text</bo", "dy></html>"]
645645 >>> element = ET.fromstringlist(sequence)
646646 >>> print(ET.tostring(element))
647- <html><body>text</body></html>
648- >>> print("".join(ET.tostringlist(element)))
649- <html><body>text</body></html>
647+ b' <html><body>text</body></html>'
648+ >>> print(b "".join(ET.tostringlist(element)))
649+ b' <html><body>text</body></html>'
650650 >>> ET.tostring(element, "ascii")
651651 b"<?xml version='1.0' encoding='ascii'?>\\ n<html><body>text</body></html>"
652652 >>> _, ids = ET.XMLID("<html><body>text</body></html>")
@@ -875,10 +875,10 @@ def writestring():
875875 """
876876 >>> elem = ET.XML("<html><body>text</body></html>")
877877 >>> ET.tostring(elem)
878- '<html><body>text</body></html>'
878+ b '<html><body>text</body></html>'
879879 >>> elem = ET.fromstring("<html><body>text</body></html>")
880880 >>> ET.tostring(elem)
881- '<html><body>text</body></html>'
881+ b '<html><body>text</body></html>'
882882 """
883883
884884def check_encoding (encoding ):
@@ -1233,14 +1233,14 @@ def processinginstruction():
12331233 Test ProcessingInstruction directly
12341234
12351235 >>> ET.tostring(ET.ProcessingInstruction('test', 'instruction'))
1236- '<?test instruction?>'
1236+ b '<?test instruction?>'
12371237 >>> ET.tostring(ET.PI('test', 'instruction'))
1238- '<?test instruction?>'
1238+ b '<?test instruction?>'
12391239
12401240 Issue #2746
12411241
12421242 >>> ET.tostring(ET.PI('test', '<testing&>'))
1243- '<?test <testing&>?>'
1243+ b '<?test <testing&>?>'
12441244 >>> ET.tostring(ET.PI('test', '<testing&>\xe3 '), 'latin1')
12451245 b"<?xml version='1.0' encoding='latin1'?>\\ n<?test <testing&>\\ xe3?>"
12461246 """
@@ -1643,11 +1643,11 @@ def bug_200708_newline():
16431643
16441644 >>> e = ET.Element('SomeTag', text="def _f():\n return 3\n")
16451645 >>> ET.tostring(e)
1646- '<SomeTag text="def _f(): return 3 " />'
1646+ b '<SomeTag text="def _f(): return 3 " />'
16471647 >>> ET.XML(ET.tostring(e)).get("text")
16481648 'def _f():\n return 3\n'
16491649 >>> ET.tostring(ET.XML(ET.tostring(e)))
1650- '<SomeTag text="def _f(): return 3 " />'
1650+ b '<SomeTag text="def _f(): return 3 " />'
16511651
16521652 """
16531653
@@ -1698,15 +1698,15 @@ def bug_200709_register_namespace():
16981698 """
16991699
17001700 >>> ET.tostring(ET.Element("{http://namespace.invalid/does/not/exist/}title"))
1701- '<ns0:title xmlns:ns0="http://namespace.invalid/does/not/exist/" />'
1701+ b '<ns0:title xmlns:ns0="http://namespace.invalid/does/not/exist/" />'
17021702 >>> ET.register_namespace("foo", "http://namespace.invalid/does/not/exist/")
17031703 >>> ET.tostring(ET.Element("{http://namespace.invalid/does/not/exist/}title"))
1704- '<foo:title xmlns:foo="http://namespace.invalid/does/not/exist/" />'
1704+ b '<foo:title xmlns:foo="http://namespace.invalid/does/not/exist/" />'
17051705
17061706 And the Dublin Core namespace is in the default list:
17071707
17081708 >>> ET.tostring(ET.Element("{http://purl.org/dc/elements/1.1/}title"))
1709- '<dc:title xmlns:dc="http://purl.org/dc/elements/1.1/" />'
1709+ b '<dc:title xmlns:dc="http://purl.org/dc/elements/1.1/" />'
17101710
17111711 """
17121712
@@ -1792,7 +1792,7 @@ def check_issue3151():
17921792 '{${stuff}}localname'
17931793 >>> t = ET.ElementTree(e)
17941794 >>> ET.tostring(e)
1795- '<ns0:localname xmlns:ns0="${stuff}" />'
1795+ b '<ns0:localname xmlns:ns0="${stuff}" />'
17961796
17971797 """
17981798
0 commit comments