bpo-36227: ElementTree.tostring() default_namespace and xml_declarati… · python/cpython@ffca16e (original) (raw)

`@@ -9,6 +9,7 @@

`

9

9

`import functools

`

10

10

`import html

`

11

11

`import io

`

``

12

`+

import locale

`

12

13

`import operator

`

13

14

`import pickle

`

14

15

`import sys

`

`@@ -756,6 +757,128 @@ def test_writestring(self):

`

756

757

`elem = ET.fromstring("text")

`

757

758

`self.assertEqual(ET.tostring(elem), b'text')

`

758

759

``

``

760

`+

def test_tostring_default_namespace(self):

`

``

761

`+

elem = ET.XML('')

`

``

762

`+

self.assertEqual(

`

``

763

`+

ET.tostring(elem, encoding='unicode'),

`

``

764

`+

'<ns0:body xmlns:ns0="" title="undefined" rel="noopener noreferrer">http://effbot.org/ns"><ns0:tag />'

`

``

765

`+

)

`

``

766

`+

self.assertEqual(

`

``

767

`+

ET.tostring(elem, encoding='unicode', default_namespace='http://effbot.org/ns'),

`

``

768

`+

''

`

``

769

`+

)

`

``

770

+

``

771

`+

def test_tostring_default_namespace_different_namespace(self):

`

``

772

`+

elem = ET.XML('')

`

``

773

`+

self.assertEqual(

`

``

774

`+

ET.tostring(elem, encoding='unicode', default_namespace='foobar'),

`

``

775

`+

'<ns1:body xmlns="foobar" xmlns:ns1="" title="undefined" rel="noopener noreferrer">http://effbot.org/ns"><ns1:tag />'

`

``

776

`+

)

`

``

777

+

``

778

`+

def test_tostring_default_namespace_original_no_namespace(self):

`

``

779

`+

elem = ET.XML('')

`

``

780

`+

EXPECTED_MSG = '^cannot use non-qualified names with default_namespace option$'

`

``

781

`+

with self.assertRaisesRegex(ValueError, EXPECTED_MSG):

`

``

782

`+

ET.tostring(elem, encoding='unicode', default_namespace='foobar')

`

``

783

+

``

784

`+

def test_tostring_no_xml_declaration(self):

`

``

785

`+

elem = ET.XML('')

`

``

786

`+

self.assertEqual(

`

``

787

`+

ET.tostring(elem, encoding='unicode'),

`

``

788

`+

''

`

``

789

`+

)

`

``

790

+

``

791

`+

def test_tostring_xml_declaration(self):

`

``

792

`+

elem = ET.XML('')

`

``

793

`+

self.assertEqual(

`

``

794

`+

ET.tostring(elem, encoding='utf8', xml_declaration=True),

`

``

795

`+

b"\n"

`

``

796

`+

)

`

``

797

+

``

798

`+

def test_tostring_xml_declaration_unicode_encoding(self):

`

``

799

`+

elem = ET.XML('')

`

``

800

`+

preferredencoding = locale.getpreferredencoding()

`

``

801

`+

self.assertEqual(

`

``

802

`+

f"\n",

`

``

803

`+

ET.tostring(elem, encoding='unicode', xml_declaration=True)

`

``

804

`+

)

`

``

805

+

``

806

`+

def test_tostring_xml_declaration_cases(self):

`

``

807

`+

elem = ET.XML('ø')

`

``

808

`+

preferredencoding = locale.getpreferredencoding()

`

``

809

`+

TESTCASES = [

`

``

810

`+

(expected_retval, encoding, xml_declaration)

`

``

811

`+

... xml_declaration = None

`

``

812

`+

(b'ø', None, None),

`

``

813

`+

(b'\xc3\xb8', 'UTF-8', None),

`

``

814

`+

(b'ø', 'US-ASCII', None),

`

``

815

`+

(b"\n"

`

``

816

`+

b"\xf8", 'ISO-8859-1', None),

`

``

817

`+

('ø', 'unicode', None),

`

``

818

+

``

819

`+

... xml_declaration = False

`

``

820

`+

(b"ø", None, False),

`

``

821

`+

(b"\xc3\xb8", 'UTF-8', False),

`

``

822

`+

(b"ø", 'US-ASCII', False),

`

``

823

`+

(b"\xf8", 'ISO-8859-1', False),

`

``

824

`+

("ø", 'unicode', False),

`

``

825

+

``

826

`+

... xml_declaration = True

`

``

827

`+

(b"\n"

`

``

828

`+

b"ø", None, True),

`

``

829

`+

(b"\n"

`

``

830

`+

b"\xc3\xb8", 'UTF-8', True),

`

``

831

`+

(b"\n"

`

``

832

`+

b"ø", 'US-ASCII', True),

`

``

833

`+

(b"\n"

`

``

834

`+

b"\xf8", 'ISO-8859-1', True),

`

``

835

`+

(f"\n"

`

``

836

`+

"ø", 'unicode', True),

`

``

837

+

``

838

`+

]

`

``

839

`+

for expected_retval, encoding, xml_declaration in TESTCASES:

`

``

840

`+

with self.subTest(f'encoding={encoding} '

`

``

841

`+

f'xml_declaration={xml_declaration}'):

`

``

842

`+

self.assertEqual(

`

``

843

`+

ET.tostring(

`

``

844

`+

elem,

`

``

845

`+

encoding=encoding,

`

``

846

`+

xml_declaration=xml_declaration

`

``

847

`+

),

`

``

848

`+

expected_retval

`

``

849

`+

)

`

``

850

+

``

851

`+

def test_tostringlist_default_namespace(self):

`

``

852

`+

elem = ET.XML('')

`

``

853

`+

self.assertEqual(

`

``

854

`+

''.join(ET.tostringlist(elem, encoding='unicode')),

`

``

855

`+

'<ns0:body xmlns:ns0="" title="undefined" rel="noopener noreferrer">http://effbot.org/ns"><ns0:tag />'

`

``

856

`+

)

`

``

857

`+

self.assertEqual(

`

``

858

`+

''.join(ET.tostringlist(elem, encoding='unicode', default_namespace='http://effbot.org/ns')),

`

``

859

`+

''

`

``

860

`+

)

`

``

861

+

``

862

`+

def test_tostringlist_xml_declaration(self):

`

``

863

`+

elem = ET.XML('')

`

``

864

`+

self.assertEqual(

`

``

865

`+

''.join(ET.tostringlist(elem, encoding='unicode')),

`

``

866

`+

''

`

``

867

`+

)

`

``

868

`+

self.assertEqual(

`

``

869

`+

b''.join(ET.tostringlist(elem, xml_declaration=True)),

`

``

870

`+

b"\n"

`

``

871

`+

)

`

``

872

+

``

873

`+

preferredencoding = locale.getpreferredencoding()

`

``

874

`+

stringlist = ET.tostringlist(elem, encoding='unicode', xml_declaration=True)

`

``

875

`+

self.assertEqual(

`

``

876

`+

''.join(stringlist),

`

``

877

`+

f"\n"

`

``

878

`+

)

`

``

879

`+

self.assertRegex(stringlist[0], r"^<?xml version='1.0' encoding='.+'?>")

`

``

880

`+

self.assertEqual(['<body', '>', '<tag', ' />', ''], stringlist[1:])

`

``

881

+

759

882

`def test_encoding(self):

`

760

883

`def check(encoding, body=''):

`

761

884

`xml = ("%s" %

`