Home | Trees | Indices | Help |
|
---|
|
1 # -*- coding: utf-8 -*- 2 3 """ 4 Tests for the ElementTree API 5 6 Only test cases that apply equally well to etree and ElementTree 7 belong here. Note that there is a second test module called test_io.py 8 for IO related test cases. 9 """ 10 11 import unittest 12 import os, re, tempfile, copy, operator, gc, sys 13 14 this_dir = os.path.dirname(__file__) 15 if this_dir not in sys.path: 16 sys.path.insert(0, this_dir) # needed for Py3 17 18 from common_imports import StringIO, BytesIO, etree 19 from common_imports import ElementTree, cElementTree, ET_VERSION, CET_VERSION 20 from common_imports import filter_by_version, fileInTestDir, canonicalize, HelperTestCase 21 from common_imports import _str, _bytes, unicode 22 23 if cElementTree is not None and CET_VERSION <= (1,0,7): 24 cElementTree = None 25 26 if ElementTree is not None: 27 print("Comparing with ElementTree %s" % getattr(ElementTree, "VERSION", "?")) 28 29 if cElementTree is not None: 30 print("Comparing with cElementTree %s" % getattr(cElementTree, "VERSION", "?")) 31 32 try: 33 reversed 34 except NameError: 35 # Python 2.3 3941 etree = None 42 required_versions_ET = {} 43 required_versions_cET = {} 4422246 for i in range(10): 47 e = self.etree.Element('foo') 48 self.assertEquals(e.tag, 'foo') 49 self.assertEquals(e.text, None) 50 self.assertEquals(e.tail, None)5153 Element = self.etree.Element 54 55 root = Element('root') 56 root.append(Element('one')) 57 root.append(Element('two')) 58 root.append(Element('three')) 59 self.assertEquals(3, len(root)) 60 self.assertEquals('one', root[0].tag) 61 self.assertEquals('two', root[1].tag) 62 self.assertEquals('three', root[2].tag) 63 self.assertRaises(IndexError, operator.getitem, root, 3)64 65 # test weird dictionary interaction leading to segfault previously67 root = self.etree.Element('root') 68 self.assertEquals(root.tag, "root") 69 add = self.etree.ElementTree(file=BytesIO('<foo>Foo</foo>')) 70 self.assertEquals(add.getroot().tag, "foo") 71 self.assertEquals(add.getroot().text, "Foo") 72 root.append(self.etree.Element('baz')) 73 self.assertEquals(root.tag, "root") 74 self.assertEquals(root[0].tag, "baz")7577 Element = self.etree.Element 78 SubElement = self.etree.SubElement 79 80 root = Element('root') 81 SubElement(root, 'one') 82 SubElement(root, 'two') 83 SubElement(root, 'three') 84 self.assertEquals(3, len(root)) 85 self.assertEquals('one', root[0].tag) 86 self.assertEquals('two', root[1].tag) 87 self.assertEquals('three', root[2].tag)8890 Element = self.etree.Element 91 SubElement = self.etree.SubElement 92 93 root1 = Element('root') 94 SubElement(root1, 'one') 95 self.assert_(root1[0] in root1) 96 97 root2 = Element('root') 98 SubElement(root2, 'two') 99 SubElement(root2, 'three') 100 self.assert_(root2[0] in root2) 101 self.assert_(root2[1] in root2) 102 103 self.assertFalse(root1[0] in root2) 104 self.assertFalse(root2[0] in root1) 105 self.assertFalse(None in root2)106108 ElementTree = self.etree.ElementTree 109 110 f = BytesIO('<doc>Test<one>One</one></doc>') 111 doc = ElementTree(file=f) 112 root = doc.getroot() 113 self.assertEquals(1, len(root)) 114 self.assertEquals('one', root[0].tag) 115 self.assertRaises(IndexError, operator.getitem, root, 1)116118 ElementTree = self.etree.ElementTree 119 120 f = BytesIO('<doc><one>One</one><two>Two</two>hm<three>Three</three></doc>') 121 doc = ElementTree(file=f) 122 root = doc.getroot() 123 self.assertEquals(3, len(root)) 124 self.assertEquals('one', root[0].tag) 125 self.assertEquals('two', root[1].tag) 126 self.assertEquals('three', root[2].tag)127129 ElementTree = self.etree.ElementTree 130 131 f = BytesIO('<doc>Test</doc>') 132 doc = ElementTree(file=f) 133 root = doc.getroot() 134 self.assertEquals(0, len(root))135137 Element = self.etree.Element 138 SubElement = self.etree.SubElement 139 a = Element('a') 140 b = SubElement(a, 'b') 141 c = SubElement(a, 'c') 142 d = SubElement(a, 'd') 143 self.assertEquals(d, a[-1]) 144 self.assertEquals(c, a[-2]) 145 self.assertEquals(b, a[-3]) 146 self.assertRaises(IndexError, operator.getitem, a, -4) 147 a[-1] = e = Element('e') 148 self.assertEquals(e, a[-1]) 149 del a[-1] 150 self.assertEquals(2, len(a))151153 ElementTree = self.etree.ElementTree 154 155 f = BytesIO('<doc><one>One</one><two>Two</two></doc>') 156 doc = ElementTree(file=f) 157 root = doc.getroot() 158 self.assertEquals(2, len(root)) 159 self.assertEquals('one', root[0].tag) 160 self.assertEquals('two', root[1].tag)161163 ElementTree = self.etree.ElementTree 164 165 f = BytesIO('<doc>This is a text</doc>') 166 doc = ElementTree(file=f) 167 root = doc.getroot() 168 self.assertEquals('This is a text', root.text)169171 ElementTree = self.etree.ElementTree 172 173 f = BytesIO('<doc></doc>') 174 doc = ElementTree(file=f) 175 root = doc.getroot() 176 self.assertEquals(None, root.text)177179 ElementTree = self.etree.ElementTree 180 181 f = BytesIO('<doc><one>One</one></doc>') 182 doc = ElementTree(file=f) 183 root = doc.getroot() 184 self.assertEquals(None, root.text) 185 self.assertEquals('One', root[0].text)186188 ElementTree = self.etree.ElementTree 189 190 f = BytesIO('<doc>This is > than a text</doc>') 191 doc = ElementTree(file=f) 192 root = doc.getroot() 193 self.assertEquals('This is > than a text', root.text)194196 Element = self.etree.Element 197 198 a = Element("a") 199 a.text = "<>&" 200 self.assertXML(_bytes('<a><>&</a>'), 201 a)202204 tostring = self.etree.tostring 205 Element = self.etree.Element 206 207 a = Element("a") 208 a.text = "<>&" 209 self.assertEquals(_bytes('<a><>&</a>'), 210 tostring(a))211 217 218 a = Element("a") 219 a.text = strTest("text") 220 self.assertXML(_bytes('<a>text</a>'), 221 a)224 ElementTree = self.etree.ElementTree 225 226 f = BytesIO('<doc>This is <i>mixed</i> content.</doc>') 227 doc = ElementTree(file=f) 228 root = doc.getroot() 229 self.assertEquals(1, len(root)) 230 self.assertEquals('This is ', root.text) 231 self.assertEquals(None, root.tail) 232 self.assertEquals('mixed', root[0].text) 233 self.assertEquals(' content.', root[0].tail)234236 Element = self.etree.Element 237 SubElement = self.etree.SubElement 238 239 class strTest(str): 240 pass241 242 a = Element("a") 243 SubElement(a, "t").tail = strTest("tail") 244 self.assertXML(_bytes('<a><t></t>tail</a>'), 245 a) 246248 # this is discouraged for ET compat, should not be tested... 249 XML = self.etree.XML 250 251 root = XML(_bytes('<doc>This is <i>mixed</i> content.</doc>')) 252 self.assertEquals(1, len(root)) 253 self.assertEquals('This is ', root.text) 254 self.assertEquals(None, root.tail) 255 self.assertEquals('mixed', root[0].text) 256 self.assertEquals(' content.', root[0].tail) 257 258 del root[0].tail 259 260 self.assertEquals(1, len(root)) 261 self.assertEquals('This is ', root.text) 262 self.assertEquals(None, root.tail) 263 self.assertEquals('mixed', root[0].text) 264 self.assertEquals(None, root[0].tail) 265 266 root[0].tail = "TAIL" 267 268 self.assertEquals(1, len(root)) 269 self.assertEquals('This is ', root.text) 270 self.assertEquals(None, root.tail) 271 self.assertEquals('mixed', root[0].text) 272 self.assertEquals('TAIL', root[0].tail)273275 Element = self.etree.Element 276 ElementTree = self.etree.ElementTree 277 278 el = Element('hoi') 279 doc = ElementTree(el) 280 root = doc.getroot() 281 self.assertEquals(None, root.text) 282 self.assertEquals('hoi', root.tag)283285 ElementTree = self.etree.ElementTree 286 287 f = BytesIO('<doc one="One" two="Two"/>') 288 doc = ElementTree(file=f) 289 root = doc.getroot() 290 self.assertEquals('One', root.attrib['one']) 291 self.assertEquals('Two', root.attrib['two']) 292 self.assertRaises(KeyError, operator.getitem, root.attrib, 'three')293295 ElementTree = self.etree.ElementTree 296 297 f = BytesIO('<doc one="One" two="Two"/>') 298 doc = ElementTree(file=f) 299 root = doc.getroot() 300 self.assertEquals('One', root.attrib.get('one')) 301 self.assertEquals('Two', root.attrib.get('two')) 302 self.assertEquals(None, root.attrib.get('three')) 303 self.assertEquals('foo', root.attrib.get('three', 'foo'))304306 ElementTree = self.etree.ElementTree 307 308 f = BytesIO('<doc one="One" two="Two"/>') 309 doc = ElementTree(file=f) 310 root = doc.getroot() 311 self.assertEquals('One', root.get('one')) 312 self.assertEquals('Two', root.get('two')) 313 self.assertEquals(None, root.get('three')) 314 self.assertEquals('foo', root.get('three', 'foo'))315317 XML = self.etree.XML 318 319 root = XML(_bytes('<doc one="One" two="Two"/>')) 320 self.assertEquals('One', root.get('one')) 321 self.assertEquals('Two', root.get('two')) 322 root.attrib.clear() 323 self.assertEquals(None, root.get('one')) 324 self.assertEquals(None, root.get('two'))325327 Element = self.etree.Element 328 329 root = Element("root", one="One") 330 root.set("two", "Two") 331 self.assertEquals('One', root.get('one')) 332 self.assertEquals('Two', root.get('two')) 333 root.attrib.clear() 334 self.assertEquals(None, root.get('one')) 335 self.assertEquals(None, root.get('two'))336338 Element = self.etree.Element 339 SubElement = self.etree.SubElement 340 341 attribNS = '{http://foo/bar}x' 342 343 parent = Element('parent') 344 parent.set(attribNS, 'a') 345 child = SubElement(parent, 'child') 346 child.set(attribNS, 'b') 347 348 self.assertEquals('a', parent.get(attribNS)) 349 self.assertEquals('b', child.get(attribNS)) 350 351 parent.clear() 352 self.assertEquals(None, parent.get(attribNS)) 353 self.assertEquals('b', child.get(attribNS))354356 ElementTree = self.etree.ElementTree 357 358 f = BytesIO('<doc one="One" two="Two"/>') 359 doc = ElementTree(file=f) 360 root = doc.getroot() 361 self.assertEquals('One', root.attrib['one']) 362 self.assertEquals('Two', root.attrib['two']) 363 364 self.assertEquals('One', root.attrib.pop('one')) 365 366 self.assertEquals(None, root.attrib.get('one')) 367 self.assertEquals('Two', root.attrib['two'])368370 root = self.etree.XML(_bytes('<doc one="One" two="Two"/>')) 371 self.assertRaises(KeyError, root.attrib.pop, 'NONE') 372 373 self.assertEquals('One', root.attrib['one']) 374 self.assertEquals('Two', root.attrib['two'])375377 root = self.etree.XML(_bytes('<doc one="One" two="Two"/>')) 378 self.assertEquals('Three', root.attrib.pop('three', 'Three'))379381 root = self.etree.XML(_bytes('<doc/>')) 382 self.assertEquals('Three', root.attrib.pop('three', 'Three'))383385 root = self.etree.XML(_bytes('<doc one="One" two="Two"/>')) 386 self.assertRaises(TypeError, root.attrib.pop, 'One', None, None)387389 XML = self.etree.XML 390 391 root = XML(_bytes('<doc alpha="Alpha" beta="Beta"/>')) 392 items = list(root.attrib.items()) 393 items.sort() 394 self.assertEquals( 395 [('alpha', 'Alpha'), ('beta', 'Beta')], 396 items) 397 398 root.attrib.update({'alpha' : 'test', 'gamma' : 'Gamma'}) 399 400 items = list(root.attrib.items()) 401 items.sort() 402 self.assertEquals( 403 [('alpha', 'test'), ('beta', 'Beta'), ('gamma', 'Gamma')], 404 items)405407 XML = self.etree.XML 408 409 root = XML(_bytes('<doc alpha="Alpha" beta="Beta"/>')) 410 items = list(root.attrib.items()) 411 items.sort() 412 self.assertEquals( 413 [('alpha', 'Alpha'), ('beta', 'Beta')], 414 items) 415 416 root.attrib.update({'alpha' : 'test', 'gamma' : 'Gamma'}.items()) 417 418 items = list(root.attrib.items()) 419 items.sort() 420 self.assertEquals( 421 [('alpha', 'test'), ('beta', 'Beta'), ('gamma', 'Gamma')], 422 items)423425 XML = self.etree.XML 426 427 root = XML(_bytes('<doc alpha="Alpha" beta="Beta"/>')) 428 items = list(root.attrib.items()) 429 items.sort() 430 self.assertEquals( 431 [('alpha', 'Alpha'), ('beta', 'Beta')], 432 items) 433 434 root.attrib.update({'alpha' : 'test', 'gamma' : 'Gamma'}.items()) 435 436 items = list(root.attrib.items()) 437 items.sort() 438 self.assertEquals( 439 [('alpha', 'test'), ('beta', 'Beta'), ('gamma', 'Gamma')], 440 items)441443 XML = self.etree.XML 444 445 root = XML(_bytes('<doc alpha="Alpha" beta="Beta" gamma="Gamma"/>')) 446 keys = list(root.attrib.keys()) 447 keys.sort() 448 self.assertEquals(['alpha', 'beta', 'gamma'], keys)449451 XML = self.etree.XML 452 453 root = XML(_bytes('<doc alpha="Alpha" beta="Beta" gamma="Gamma"/>')) 454 keys = list(root.keys()) 455 keys.sort() 456 self.assertEquals(['alpha', 'beta', 'gamma'], keys)457459 XML = self.etree.XML 460 461 root = XML(_bytes('<doc alpha="Alpha" beta="Beta" gamma="Gamma"/>')) 462 items = list(root.items()) 463 items.sort() 464 self.assertEquals( 465 [('alpha','Alpha'), ('beta','Beta'), ('gamma','Gamma')], 466 items)467469 XML = self.etree.XML 470 471 root = XML(_bytes('<foo bar="Bar" xmlns:ns="http://ns.codespeak.net/test" ns:baz="Baz" />')) 472 keys = list(root.keys()) 473 keys.sort() 474 self.assertEquals(['bar', '{http://ns.codespeak.net/test}baz'], 475 keys)476478 XML = self.etree.XML 479 480 root = XML(_bytes('<doc alpha="Alpha" beta="Beta" gamma="Gamma"/>')) 481 values = list(root.attrib.values()) 482 values.sort() 483 self.assertEquals(['Alpha', 'Beta', 'Gamma'], values)484486 XML = self.etree.XML 487 488 root = XML(_bytes('<foo bar="Bar" xmlns:ns="http://ns.codespeak.net/test" ns:baz="Baz" />')) 489 values = list(root.attrib.values()) 490 values.sort() 491 self.assertEquals( 492 ['Bar', 'Baz'], values)493495 XML = self.etree.XML 496 497 root = XML(_bytes('<doc alpha="Alpha" beta="Beta" gamma="Gamma"/>')) 498 items = list(root.attrib.items()) 499 items.sort() 500 self.assertEquals([ 501 ('alpha', 'Alpha'), 502 ('beta', 'Beta'), 503 ('gamma', 'Gamma'), 504 ], 505 items)506508 XML = self.etree.XML 509 510 root = XML(_bytes('<foo bar="Bar" xmlns:ns="http://ns.codespeak.net/test" ns:baz="Baz" />')) 511 items = list(root.attrib.items()) 512 items.sort() 513 self.assertEquals( 514 [('bar', 'Bar'), ('{http://ns.codespeak.net/test}baz', 'Baz')], 515 items)516518 XML = self.etree.XML 519 520 expected = "{'{http://ns.codespeak.net/test}baz': 'Baz', 'bar': 'Bar'}" 521 alternative = "{'bar': 'Bar', '{http://ns.codespeak.net/test}baz': 'Baz'}" 522 523 root = XML(_bytes('<foo bar="Bar" xmlns:ns="http://ns.codespeak.net/test" ns:baz="Baz" />')) 524 try: 525 self.assertEquals(expected, str(root.attrib)) 526 except AssertionError: 527 self.assertEquals(alternative, str(root.attrib))528530 XML = self.etree.XML 531 532 root = XML(_bytes('<foo bar="Bar" xmlns:ns="http://ns.codespeak.net/test" ns:baz="Baz" />')) 533 self.assertEquals( 534 True, 'bar' in root.attrib) 535 self.assertEquals( 536 False, 'baz' in root.attrib) 537 self.assertEquals( 538 False, 'hah' in root.attrib) 539 self.assertEquals( 540 True, 541 '{http://ns.codespeak.net/test}baz' in root.attrib)542544 Element = self.etree.Element 545 546 root = Element("root") 547 root.set("attr", "TEST") 548 self.assertEquals("TEST", root.get("attr"))549551 XML = self.etree.XML 552 553 root = XML(_bytes('<doc alpha="Alpha" beta="Beta" gamma="Gamma" />')) 554 result = [] 555 for key in root.attrib: 556 result.append(key) 557 result.sort() 558 self.assertEquals(['alpha', 'beta', 'gamma'], result)559561 Element = self.etree.Element 562 563 a = Element('a') 564 a.attrib['foo'] = 'Foo' 565 a.attrib['bar'] = 'Bar' 566 self.assertEquals('Foo', a.attrib['foo']) 567 del a.attrib['foo'] 568 self.assertRaises(KeyError, operator.getitem, a.attrib, 'foo')569571 Element = self.etree.Element 572 573 a = Element('a') 574 a.attrib['{http://a/}foo'] = 'Foo' 575 a.attrib['{http://a/}bar'] = 'Bar' 576 self.assertEquals(None, a.get('foo')) 577 self.assertEquals('Foo', a.get('{http://a/}foo')) 578 self.assertEquals('Foo', a.attrib['{http://a/}foo']) 579 580 self.assertRaises(KeyError, operator.delitem, a.attrib, 'foo') 581 self.assertEquals('Foo', a.attrib['{http://a/}foo']) 582 583 del a.attrib['{http://a/}foo'] 584 self.assertRaises(KeyError, operator.getitem, a.attrib, 'foo')585587 XML = self.etree.XML 588 589 a = XML(_bytes('<a xmlns:nsa="http://a/" nsa:foo="FooNS" foo="Foo" />')) 590 591 self.assertEquals('Foo', a.attrib['foo']) 592 self.assertEquals('FooNS', a.attrib['{http://a/}foo']) 593 594 del a.attrib['foo'] 595 self.assertEquals('FooNS', a.attrib['{http://a/}foo']) 596 self.assertRaises(KeyError, operator.getitem, a.attrib, 'foo') 597 self.assertRaises(KeyError, operator.delitem, a.attrib, 'foo') 598 599 del a.attrib['{http://a/}foo'] 600 self.assertRaises(KeyError, operator.getitem, a.attrib, '{http://a/}foo') 601 self.assertRaises(KeyError, operator.getitem, a.attrib, 'foo') 602 603 a = XML(_bytes('<a xmlns:nsa="http://a/" foo="Foo" nsa:foo="FooNS" />')) 604 605 self.assertEquals('Foo', a.attrib['foo']) 606 self.assertEquals('FooNS', a.attrib['{http://a/}foo']) 607 608 del a.attrib['foo'] 609 self.assertEquals('FooNS', a.attrib['{http://a/}foo']) 610 self.assertRaises(KeyError, operator.getitem, a.attrib, 'foo') 611 612 del a.attrib['{http://a/}foo'] 613 self.assertRaises(KeyError, operator.getitem, a.attrib, '{http://a/}foo') 614 self.assertRaises(KeyError, operator.getitem, a.attrib, 'foo')615617 XML = self.etree.XML 618 619 root = XML(_bytes('<doc>This is a text.</doc>')) 620 self.assertEquals(0, len(root)) 621 self.assertEquals('This is a text.', root.text)622624 XMLID = self.etree.XMLID 625 XML = self.etree.XML 626 xml_text = _bytes(''' 627 <document> 628 <h1 id="chapter1">...</h1> 629 <p id="note1" class="note">...</p> 630 <p>Regular paragraph.</p> 631 <p xml:id="xmlid">XML:ID paragraph.</p> 632 <p id="warn1" class="warning">...</p> 633 </document> 634 ''') 635 636 root, dic = XMLID(xml_text) 637 root2 = XML(xml_text) 638 self.assertEquals(self._writeElement(root), 639 self._writeElement(root2)) 640 expected = { 641 "chapter1" : root[0], 642 "note1" : root[1], 643 "warn1" : root[4] 644 } 645 self.assertEquals(dic, expected)646648 fromstring = self.etree.fromstring 649 650 root = fromstring('<doc>This is a text.</doc>') 651 self.assertEquals(0, len(root)) 652 self.assertEquals('This is a text.', root.text)653 654 required_versions_ET['test_fromstringlist'] = (1,3)656 fromstringlist = self.etree.fromstringlist 657 658 root = fromstringlist(["<do", "c>T", "hi", "s is", 659 " a text.<", "/doc", ">"]) 660 self.assertEquals(0, len(root)) 661 self.assertEquals('This is a text.', root.text)662 663 required_versions_ET['test_fromstringlist_characters'] = (1,3)665 fromstringlist = self.etree.fromstringlist 666 667 root = fromstringlist(list('<doc>This is a text.</doc>')) 668 self.assertEquals(0, len(root)) 669 self.assertEquals('This is a text.', root.text)670 671 required_versions_ET['test_fromstringlist_single'] = (1,3)673 fromstringlist = self.etree.fromstringlist 674 675 root = fromstringlist(['<doc>This is a text.</doc>']) 676 self.assertEquals(0, len(root)) 677 self.assertEquals('This is a text.', root.text)678680 iselement = self.etree.iselement 681 Element = self.etree.Element 682 ElementTree = self.etree.ElementTree 683 XML = self.etree.XML 684 Comment = self.etree.Comment 685 ProcessingInstruction = self.etree.ProcessingInstruction 686 687 el = Element('hoi') 688 self.assert_(iselement(el)) 689 690 el2 = XML(_bytes('<foo/>')) 691 self.assert_(iselement(el2)) 692 693 tree = ElementTree(element=Element('dag')) 694 self.assert_(not iselement(tree)) 695 self.assert_(iselement(tree.getroot())) 696 697 c = Comment('test') 698 self.assert_(iselement(c)) 699 700 p = ProcessingInstruction("test", "some text") 701 self.assert_(iselement(p))702704 XML = self.etree.XML 705 706 root = XML(_bytes('<doc><one/><two>Two</two>Hm<three/></doc>')) 707 result = [] 708 for el in root: 709 result.append(el.tag) 710 self.assertEquals(['one', 'two', 'three'], result)711713 XML = self.etree.XML 714 715 root = XML(_bytes('<doc></doc>')) 716 result = [] 717 for el in root: 718 result.append(el.tag) 719 self.assertEquals([], result)720722 XML = self.etree.XML 723 724 root = XML(_bytes('<doc>Text</doc>')) 725 result = [] 726 for el in root: 727 result.append(el.tag) 728 self.assertEquals([], result)729731 # this would cause a crash in the past 732 fromstring = self.etree.fromstring 733 root = etree.fromstring('<html><p></p>x</html>') 734 for elem in root: 735 elem.tail = ''736738 XML = self.etree.XML 739 root = XML(_bytes('<doc><one/><two>Two</two>Hm<three/></doc>')) 740 result = [] 741 for el in reversed(root): 742 result.append(el.tag) 743 self.assertEquals(['three', 'two', 'one'], result)744746 XML = self.etree.XML 747 748 root = XML(_bytes('<doc><one/><two>Two</two>Hm<three/></doc>')) 749 result = [] 750 add = True 751 for el in root: 752 result.append(el.tag) 753 if add: 754 self.etree.SubElement(root, 'four') 755 add = False 756 self.assertEquals(['one', 'two', 'three', 'four'], result)757759 XML = self.etree.XML 760 761 root = XML(_bytes('<doc><one/><two>Two</two>Hm<three/></doc>')) 762 result = [] 763 for el in root: 764 result.append(el.tag) 765 del root[-1] 766 self.assertEquals(['one', 'two'], result)767769 XML = self.etree.XML 770 771 root = XML(_bytes('<doc><one/><two/></doc>')) 772 result = [] 773 for el0 in root: 774 result.append(el0.tag) 775 for el1 in root: 776 result.append(el1.tag) 777 self.assertEquals(['one','one', 'two', 'two', 'one', 'two'], result)778 779 required_versions_ET['test_itertext'] = (1,3)781 # ET 1.3+ 782 XML = self.etree.XML 783 root = XML(_bytes("<root>RTEXT<a></a>ATAIL<b/><c>CTEXT</c>CTAIL</root>")) 784 785 text = list(root.itertext()) 786 self.assertEquals(["RTEXT", "ATAIL", "CTEXT", "CTAIL"], 787 text)788 789 required_versions_ET['test_itertext_child'] = (1,3)791 # ET 1.3+ 792 XML = self.etree.XML 793 root = XML(_bytes("<root>RTEXT<a></a>ATAIL<b/><c>CTEXT</c>CTAIL</root>")) 794 795 text = list(root[2].itertext()) 796 self.assertEquals(["CTEXT"], 797 text)798800 XML = self.etree.XML 801 root = XML(_bytes('<a><b><c/></b><b/><c><b/></c></a>')) 802 self.assertEquals(len(list(root.findall("c"))), 1) 803 self.assertEquals(len(list(root.findall(".//c"))), 2) 804 self.assertEquals(len(list(root.findall(".//b"))), 3) 805 self.assertEquals(len(list(root.findall(".//b"))[0]), 1) 806 self.assertEquals(len(list(root.findall(".//b"))[1]), 0) 807 self.assertEquals(len(list(root.findall(".//b"))[2]), 0)808810 XML = self.etree.XML 811 root = XML(_bytes('<a xmlns:x="X" xmlns:y="Y"><x:b><c/></x:b><b/><c><x:b/><b/></c><b/></a>')) 812 self.assertEquals(len(list(root.findall(".//{X}b"))), 2) 813 self.assertEquals(len(list(root.findall(".//b"))), 3) 814 self.assertEquals(len(list(root.findall("b"))), 2)815817 Element = self.etree.Element 818 819 el = Element('tag', foo='Foo', bar='Bar') 820 self.assertEquals('Foo', el.attrib['foo']) 821 self.assertEquals('Bar', el.attrib['bar'])822824 Element = self.etree.Element 825 826 el = Element('tag', {'foo':'Foo', 'bar':'Bar'}) 827 self.assertEquals('Foo', el.attrib['foo']) 828 self.assertEquals('Bar', el.attrib['bar'])829831 Element = self.etree.Element 832 833 el = Element('tag', {'{ns1}foo':'Foo', '{ns2}bar':'Bar'}) 834 self.assertEquals('Foo', el.attrib['{ns1}foo']) 835 self.assertEquals('Bar', el.attrib['{ns2}bar'])836838 Element = self.etree.Element 839 SubElement = self.etree.SubElement 840 841 el = Element('tag') 842 SubElement(el, 'foo', {'foo':'Foo'}, baz="Baz") 843 self.assertEquals("Baz", el[0].attrib['baz']) 844 self.assertEquals('Foo', el[0].attrib['foo'])845847 Element = self.etree.Element 848 SubElement = self.etree.SubElement 849 850 el = Element('tag') 851 SubElement(el, 'foo', {'{ns1}foo':'Foo', '{ns2}bar':'Bar'}) 852 self.assertEquals('Foo', el[0].attrib['{ns1}foo']) 853 self.assertEquals('Bar', el[0].attrib['{ns2}bar'])854856 ElementTree = self.etree.ElementTree 857 XML = self.etree.XML 858 859 for i in range(10): 860 f = BytesIO() 861 root = XML(_bytes('<doc%s>This is a test.</doc%s>' % (i, i))) 862 tree = ElementTree(element=root) 863 tree.write(f) 864 data = f.getvalue() 865 self.assertEquals( 866 _bytes('<doc%s>This is a test.</doc%s>' % (i, i)), 867 canonicalize(data))868 869 required_versions_ET['test_write_method_html'] = (1,3)871 ElementTree = self.etree.ElementTree 872 Element = self.etree.Element 873 SubElement = self.etree.SubElement 874 875 html = Element('html') 876 body = SubElement(html, 'body') 877 p = SubElement(body, 'p') 878 p.text = "html" 879 SubElement(p, 'br').tail = "test" 880 881 tree = ElementTree(element=html) 882 f = BytesIO() 883 tree.write(f, method="html") 884 data = f.getvalue().replace(_bytes('\n'),_bytes('')) 885 886 self.assertEquals(_bytes('<html><body><p>html<br>test</p></body></html>'), 887 data)888 889 required_versions_ET['test_write_method_text'] = (1,3)891 ElementTree = self.etree.ElementTree 892 Element = self.etree.Element 893 SubElement = self.etree.SubElement 894 895 a = Element('a') 896 a.text = "A" 897 a.tail = "tail" 898 b = SubElement(a, 'b') 899 b.text = "B" 900 b.tail = "TAIL" 901 c = SubElement(a, 'c') 902 c.text = "C" 903 904 tree = ElementTree(element=a) 905 f = BytesIO() 906 tree.write(f, method="text") 907 data = f.getvalue() 908 909 self.assertEquals(_bytes('ABTAILCtail'), 910 data)911913 ElementTree = self.etree.ElementTree 914 XML = self.etree.XML 915 916 tree = ElementTree( XML(_bytes('<doc>This is a test.</doc>')) ) 917 self.assertRaises(IOError, tree.write, 918 "definitely////\\-\\nonexisting\\-\\////FILE")919 920 # this could trigger a crash, apparently because the document 921 # reference was prematurely garbage collected923 Element = self.etree.Element 924 925 element = Element('tag') 926 for i in range(10): 927 element.attrib['key'] = 'value' 928 value = element.attrib['key'] 929 self.assertEquals(value, 'value')930 931 # from doctest; for some reason this caused crashes too933 Element = self.etree.Element 934 ElementTree = self.etree.ElementTree 935 936 f = BytesIO() 937 for i in range(10): 938 element = Element('tag%s' % i) 939 self._check_element(element) 940 tree = ElementTree(element) 941 tree.write(f) 942 self._check_element_tree(tree)943945 Element = self.etree.Element 946 SubElement = self.etree.SubElement 947 948 el = Element('foo') 949 el2 = SubElement(el, 'bar') 950 el3 = SubElement(el2, 'baz') 951 952 al = Element('foo2') 953 al2 = SubElement(al, 'bar2') 954 al3 = SubElement(al2, 'baz2') 955 956 # now move al2 into el 957 el.append(al2) 958 959 # now change al3 directly 960 al3.text = 'baz2-modified' 961 962 # it should have changed through this route too 963 self.assertEquals( 964 'baz2-modified', 965 el[1][0].text)966968 Element = self.etree.Element 969 SubElement = self.etree.SubElement 970 971 a = Element('a') 972 b = SubElement(a, 'b') 973 a.text = 'hoi' 974 self.assertEquals( 975 'hoi', 976 a.text) 977 self.assertEquals( 978 'b', 979 a[0].tag)980982 Element = self.etree.Element 983 SubElement = self.etree.SubElement 984 985 a = Element('a') 986 a.text = 'hoi' 987 b = SubElement(a ,'b') 988 self.assertEquals( 989 'hoi', 990 a.text) 991 self.assertEquals( 992 'b', 993 a[0].tag)994996 Element = self.etree.Element 997 998 a = Element('a') 999 1000 a.text = 'foo' 1001 a.text = None 1002 1003 self.assertEquals( 1004 None, 1005 a.text) 1006 self.assertXML(_bytes('<a></a>'), a)10071009 Element = self.etree.Element 1010 1011 a = Element('a') 1012 self.assertEquals(None, a.text) 1013 1014 a.text = '' 1015 self.assertEquals('', a.text) 1016 self.assertXML(_bytes('<a></a>'), a)10171019 Element = self.etree.Element 1020 SubElement = self.etree.SubElement 1021 1022 a = Element('a') 1023 a.tail = 'dag' 1024 self.assertEquals('dag', 1025 a.tail) 1026 b = SubElement(a, 'b') 1027 b.tail = 'hoi' 1028 self.assertEquals('hoi', 1029 b.tail) 1030 self.assertEquals('dag', 1031 a.tail)10321034 Element = self.etree.Element 1035 1036 a = Element('a') 1037 b = Element('b') 1038 b.tail = 'b_tail' 1039 a.append(b) 1040 self.assertEquals('b_tail', 1041 b.tail)10421044 Element = self.etree.Element 1045 SubElement = self.etree.SubElement 1046 1047 a = Element('a') 1048 b = SubElement(a, 'b') 1049 b.tail = 'foo' 1050 b.tail = 'bar' 1051 self.assertEquals('bar', 1052 b.tail) 1053 self.assertXML(_bytes('<a><b></b>bar</a>'), a)10541056 Element = self.etree.Element 1057 a = Element('a') 1058 a.tail = 'foo' 1059 a.tail = None 1060 self.assertEquals( 1061 None, 1062 a.tail) 1063 self.assertXML(_bytes('<a></a>'), a)1064 1065 required_versions_ET['test_extend'] = (1,3)1067 root = self.etree.Element('foo') 1068 for i in range(3): 1069 element = self.etree.SubElement(root, 'a%s' % i) 1070 element.text = "text%d" % i 1071 element.tail = "tail%d" % i 1072 1073 elements = [] 1074 for i in range(3): 1075 new_element = self.etree.Element("test%s" % i) 1076 new_element.text = "TEXT%s" % i 1077 new_element.tail = "TAIL%s" % i 1078 elements.append(new_element) 1079 1080 root.extend(elements) 1081 1082 self.assertEquals( 1083 ["a0", "a1", "a2", "test0", "test1", "test2"], 1084 [ el.tag for el in root ]) 1085 self.assertEquals( 1086 ["text0", "text1", "text2", "TEXT0", "TEXT1", "TEXT2"], 1087 [ el.text for el in root ]) 1088 self.assertEquals( 1089 ["tail0", "tail1", "tail2", "TAIL0", "TAIL1", "TAIL2"], 1090 [ el.tail for el in root ])10911093 Element = self.etree.Element 1094 SubElement = self.etree.SubElement 1095 Comment = self.etree.Comment 1096 1097 a = Element('a') 1098 a.append(Comment('foo')) 1099 self.assertEquals(a[0].tag, Comment) 1100 self.assertEquals(a[0].text, 'foo')1101 1102 # ElementTree < 1.3 adds whitespace around comments 1103 required_versions_ET['test_comment_text'] = (1,3)1105 Element = self.etree.Element 1106 SubElement = self.etree.SubElement 1107 Comment = self.etree.Comment 1108 tostring = self.etree.tostring 1109 1110 a = Element('a') 1111 a.append(Comment('foo')) 1112 self.assertEquals(a[0].text, 'foo') 1113 1114 self.assertEquals( 1115 _bytes('<a><!--foo--></a>'), 1116 tostring(a)) 1117 1118 a[0].text = "TEST" 1119 self.assertEquals(a[0].text, 'TEST') 1120 1121 self.assertEquals( 1122 _bytes('<a><!--TEST--></a>'), 1123 tostring(a))1124 1125 # ElementTree < 1.3 adds whitespace around comments 1126 required_versions_ET['test_comment_whitespace'] = (1,3)1128 Element = self.etree.Element 1129 SubElement = self.etree.SubElement 1130 Comment = self.etree.Comment 1131 tostring = self.etree.tostring 1132 1133 a = Element('a') 1134 a.append(Comment(' foo ')) 1135 self.assertEquals(a[0].text, ' foo ') 1136 self.assertEquals( 1137 _bytes('<a><!-- foo --></a>'), 1138 tostring(a))11391141 Comment = self.etree.Comment 1142 c = Comment('foo') 1143 self.assertEquals({}, c.attrib) 1144 self.assertEquals([], list(c.keys())) 1145 self.assertEquals([], list(c.items())) 1146 self.assertEquals(None, c.get('hoi')) 1147 self.assertEquals(0, len(c)) 1148 # should not iterate 1149 for i in c: 1150 pass11511153 # lxml.etree separates target and text 1154 Element = self.etree.Element 1155 SubElement = self.etree.SubElement 1156 ProcessingInstruction = self.etree.ProcessingInstruction 1157 1158 a = Element('a') 1159 a.append(ProcessingInstruction('foo', 'some more text')) 1160 self.assertEquals(a[0].tag, ProcessingInstruction) 1161 self.assertXML(_bytes("<a><?foo some more text?></a>"), 1162 a)11631165 # lxml.etree separates target and text 1166 Element = self.etree.Element 1167 SubElement = self.etree.SubElement 1168 ProcessingInstruction = self.etree.PI 1169 1170 a = Element('a') 1171 a.append(ProcessingInstruction('foo', 'some more text')) 1172 self.assertEquals(a[0].tag, ProcessingInstruction) 1173 self.assertXML(_bytes("<a><?foo some more text?></a>"), 1174 a)11751177 ProcessingInstruction = self.etree.ProcessingInstruction 1178 pi = ProcessingInstruction('foo') 1179 self.assertEquals({}, pi.attrib) 1180 self.assertEquals([], list(pi.keys())) 1181 self.assertEquals([], list(pi.items())) 1182 self.assertEquals(None, pi.get('hoi')) 1183 self.assertEquals(0, len(pi)) 1184 # should not iterate 1185 for i in pi: 1186 pass11871189 Element = self.etree.Element 1190 SubElement = self.etree.SubElement 1191 1192 a = Element('a') 1193 b = SubElement(a, 'b') 1194 c = Element('c') 1195 a[0] = c 1196 self.assertEquals( 1197 c, 1198 a[0]) 1199 self.assertXML(_bytes('<a><c></c></a>'), 1200 a) 1201 self.assertXML(_bytes('<b></b>'), 1202 b)12031205 Element = self.etree.Element 1206 SubElement = self.etree.SubElement 1207 1208 a = Element('a') 1209 for i in range(5): 1210 b = SubElement(a, 'b%s' % i) 1211 c = SubElement(b, 'c') 1212 for i in range(5): 1213 d = Element('d') 1214 e = SubElement(d, 'e') 1215 a[i] = d 1216 self.assertXML( 1217 _bytes('<a><d><e></e></d><d><e></e></d><d><e></e></d><d><e></e></d><d><e></e></d></a>'), 1218 a) 1219 self.assertXML(_bytes('<c></c>'), 1220 c)12211223 Element = self.etree.Element 1224 SubElement = self.etree.SubElement 1225 1226 a = Element('a') 1227 SubElement(a, 'b') 1228 d = Element('d') 1229 a[0] = d 1230 self.assertXML(_bytes('<a><d></d></a>'), a)12311233 Element = self.etree.Element 1234 SubElement = self.etree.SubElement 1235 1236 a = Element('a') 1237 b = SubElement(a, 'b') 1238 1239 self.assertRaises(IndexError, operator.setitem, a, 1, Element('c'))12401242 Element = self.etree.Element 1243 SubElement = self.etree.SubElement 1244 1245 a = Element('a') 1246 b = SubElement(a, 'b') 1247 b.tail = 'B2' 1248 c = Element('c') 1249 c.tail = 'C2' 1250 1251 a[0] = c 1252 self.assertXML( 1253 _bytes('<a><c></c>C2</a>'), 1254 a)12551257 Element = self.etree.Element 1258 SubElement = self.etree.SubElement 1259 1260 a = Element('a') 1261 b = SubElement(a, 'b') 1262 1263 a.tag = 'c' 1264 1265 self.assertEquals( 1266 'c', 1267 a.tag) 1268 1269 self.assertXML( 1270 _bytes('<c><b></b></c>'), 1271 a)12721274 Element = self.etree.Element 1275 SubElement = self.etree.SubElement 1276 tostring = self.etree.tostring 1277 1278 a = Element('{a}a') 1279 b1 = SubElement(a, '{a}b') 1280 b2 = SubElement(a, '{b}b') 1281 1282 self.assertEquals('{a}b', b1.tag) 1283 1284 b1.tag = 'c' 1285 1286 # can't use C14N here! 1287 self.assertEquals('c', b1.tag) 1288 self.assertEquals(_bytes('<c'), tostring(b1)[:2]) 1289 self.assert_(_bytes('<c') in tostring(a))12901292 Element = self.etree.Element 1293 SubElement = self.etree.SubElement 1294 tostring = self.etree.tostring 1295 1296 a = Element('{a}a') 1297 b1 = SubElement(a, '{a}b') 1298 b2 = SubElement(a, '{b}b') 1299 1300 a.tag = 'c' 1301 1302 self.assertEquals( 1303 'c', 1304 a.tag) 1305 1306 # can't use C14N here! 1307 self.assertEquals('c', a.tag) 1308 self.assertEquals(_bytes('<c'), tostring(a)[:2])1309 1315 1316 a = Element("a") 1317 a.tag = strTest("TAG") 1318 self.assertXML(_bytes('<TAG></TAG>'), 1319 a) 13201322 Element = self.etree.Element 1323 SubElement = self.etree.SubElement 1324 1325 a = Element('a') 1326 b = SubElement(a, 'b') 1327 c = SubElement(a, 'c') 1328 d = SubElement(a, 'd') 1329 1330 del a[1] 1331 self.assertXML( 1332 _bytes('<a><b></b><d></d></a>'), 1333 a) 1334 1335 del a[0] 1336 self.assertXML( 1337 _bytes('<a><d></d></a>'), 1338 a) 1339 1340 del a[0] 1341 self.assertXML( 1342 _bytes('<a></a>'), 1343 a) 1344 # move deleted element into other tree afterwards 1345 other = Element('other') 1346 other.append(c) 1347 self.assertXML( 1348 _bytes('<other><c></c></other>'), 1349 other)13501352 Element = self.etree.Element 1353 SubElement = self.etree.SubElement 1354 1355 a = Element('a') 1356 b = SubElement(a, 'b') 1357 bs = SubElement(b, 'bs') 1358 c = SubElement(a, 'c') 1359 cs = SubElement(c, 'cs') 1360 1361 el = a[0] 1362 self.assertXML( 1363 _bytes('<a><b><bs></bs></b><c><cs></cs></c></a>'), 1364 a) 1365 self.assertXML(_bytes('<b><bs></bs></b>'), b) 1366 self.assertXML(_bytes('<c><cs></cs></c>'), c) 1367 1368 del a[0] 1369 self.assertXML( 1370 _bytes('<a><c><cs></cs></c></a>'), 1371 a) 1372 self.assertXML(_bytes('<b><bs></bs></b>'), b) 1373 self.assertXML(_bytes('<c><cs></cs></c>'), c) 1374 1375 a.insert(0, el) 1376 self.assertXML( 1377 _bytes('<a><b><bs></bs></b><c><cs></cs></c></a>'), 1378 a) 1379 self.assertXML(_bytes('<b><bs></bs></b>'), b) 1380 self.assertXML(_bytes('<c><cs></cs></c>'), c)13811383 Element = self.etree.Element 1384 SubElement = self.etree.SubElement 1385 1386 a = Element('a') 1387 b = SubElement(a, 'b') 1388 bs = SubElement(b, 'bs') 1389 c = SubElement(a, 'c') 1390 cs = SubElement(c, 'cs') 1391 1392 el = a[0] 1393 del a[0] 1394 a[0] = el 1395 self.assertXML( 1396 _bytes('<a><b><bs></bs></b></a>'), 1397 a) 1398 self.assertXML(_bytes('<b><bs></bs></b>'), b) 1399 self.assertXML(_bytes('<c><cs></cs></c>'), c)14001402 Element = self.etree.Element 1403 SubElement = self.etree.SubElement 1404 1405 a = Element('a') 1406 b = SubElement(a, 'b') 1407 bs = SubElement(b, 'bs') 1408 c = SubElement(a, 'c') 1409 cs = SubElement(c, 'cs') 1410 1411 el = a[0] 1412 del a[0] 1413 a[0:0] = [el] 1414 self.assertXML( 1415 _bytes('<a><b><bs></bs></b><c><cs></cs></c></a>'), 1416 a) 1417 self.assertXML(_bytes('<b><bs></bs></b>'), b) 1418 self.assertXML(_bytes('<c><cs></cs></c>'), c)14191421 XML = self.etree.XML 1422 a = XML(_bytes('<a><b></b>B2<c></c>C2</a>')) 1423 b, c = a 1424 1425 a[:] = [] 1426 1427 self.assertEquals("B2", b.tail) 1428 self.assertEquals("C2", c.tail)14291431 XML = self.etree.XML 1432 root = XML(_bytes( 1433 '<foo><bar xmlns:baz="http://huhu"><puh><baz:bump1 /><baz:bump2 /></puh></bar></foo>')) 1434 root[:] = root.findall('.//puh') # delete bar from hierarchy 1435 1436 # previously, this lost a namespace declaration on bump2 1437 result = self.etree.tostring(root) 1438 foo = self.etree.fromstring(result) 1439 1440 self.assertEquals('puh', foo[0].tag) 1441 self.assertEquals('{http://huhu}bump1', foo[0][0].tag) 1442 self.assertEquals('{http://huhu}bump2', foo[0][1].tag)14431445 ElementTree = self.etree.ElementTree 1446 f = BytesIO('<a><b></b>B2<c></c>C2</a>') 1447 doc = ElementTree(file=f) 1448 a = doc.getroot() 1449 del a[0] 1450 self.assertXML( 1451 _bytes('<a><c></c>C2</a>'), 1452 a)14531455 Element = self.etree.Element 1456 1457 a = Element('a') 1458 a.text = 'foo' 1459 a.tail = 'bar' 1460 a.set('hoi', 'dag') 1461 a.clear() 1462 self.assertEquals(None, a.text) 1463 self.assertEquals(None, a.tail) 1464 self.assertEquals(None, a.get('hoi')) 1465 self.assertEquals('a', a.tag)14661468 Element = self.etree.Element 1469 SubElement = self.etree.SubElement 1470 1471 a = Element('a') 1472 a.text = 'foo' 1473 a.tail = 'bar' 1474 a.set('hoi', 'dag') 1475 b = SubElement(a, 'b') 1476 c = SubElement(b, 'c') 1477 a.clear() 1478 self.assertEquals(None, a.text) 1479 self.assertEquals(None, a.tail) 1480 self.assertEquals(None, a.get('hoi')) 1481 self.assertEquals('a', a.tag) 1482 self.assertEquals(0, len(a)) 1483 self.assertXML(_bytes('<a></a>'), 1484 a) 1485 self.assertXML(_bytes('<b><c></c></b>'), 1486 b)14871489 ElementTree = self.etree.ElementTree 1490 f = BytesIO('<a><b></b>B2<c></c>C2</a>') 1491 doc = ElementTree(file=f) 1492 a = doc.getroot() 1493 a.clear() 1494 self.assertXML( 1495 _bytes('<a></a>'), 1496 a)14971499 Element = self.etree.Element 1500 SubElement = self.etree.SubElement 1501 1502 a = Element('a') 1503 b = SubElement(a, 'b') 1504 c = SubElement(a, 'c') 1505 d = Element('d') 1506 a.insert(0, d) 1507 1508 self.assertEquals( 1509 d, 1510 a[0]) 1511 1512 self.assertXML( 1513 _bytes('<a><d></d><b></b><c></c></a>'), 1514 a) 1515 1516 e = Element('e') 1517 a.insert(2, e) 1518 self.assertEquals( 1519 e, 1520 a[2]) 1521 self.assertXML( 1522 _bytes('<a><d></d><b></b><e></e><c></c></a>'), 1523 a)15241526 Element = self.etree.Element 1527 SubElement = self.etree.SubElement 1528 1529 a = Element('a') 1530 b = SubElement(a, 'b') 1531 c = Element('c') 1532 1533 a.insert(2, c) 1534 self.assertEquals( 1535 c, 1536 a[1]) 1537 self.assertXML( 1538 _bytes('<a><b></b><c></c></a>'), 1539 a)15401542 Element = self.etree.Element 1543 SubElement = self.etree.SubElement 1544 1545 a = Element('a') 1546 b = SubElement(a, 'b') 1547 c = SubElement(a, 'c') 1548 1549 d = Element('d') 1550 a.insert(-1, d) 1551 self.assertEquals( 1552 d, 1553 a[-2]) 1554 self.assertXML( 1555 _bytes('<a><b></b><d></d><c></c></a>'), 1556 a)15571559 Element = self.etree.Element 1560 SubElement = self.etree.SubElement 1561 1562 a = Element('a') 1563 b = SubElement(a, 'b') 1564 1565 c = Element('c') 1566 c.tail = 'C2' 1567 1568 a.insert(0, c) 1569 self.assertXML( 1570 _bytes('<a><c></c>C2<b></b></a>'), 1571 a)15721574 Element = self.etree.Element 1575 SubElement = self.etree.SubElement 1576 1577 a = Element('a') 1578 b = SubElement(a, 'b') 1579 c = SubElement(a, 'c') 1580 1581 a.remove(b) 1582 self.assertEquals( 1583 c, 1584 a[0]) 1585 self.assertXML( 1586 _bytes('<a><c></c></a>'), 1587 a)15881590 Element = self.etree.Element 1591 SubElement = self.etree.SubElement 1592 1593 a = Element('{http://test}a') 1594 b = SubElement(a, '{http://test}b') 1595 c = SubElement(a, '{http://test}c') 1596 1597 a.remove(b) 1598 self.assertXML( 1599 _bytes('<ns0:a xmlns:ns0="http://test"><ns0:c></ns0:c></ns0:a>'), 1600 a) 1601 self.assertXML( 1602 _bytes('<ns0:b xmlns:ns0="http://test"></ns0:b>'), 1603 b)16041606 Element = self.etree.Element 1607 SubElement = self.etree.SubElement 1608 1609 a = Element('a') 1610 b = SubElement(a, 'b') 1611 c = SubElement(a, 'c') 1612 d = Element('d') 1613 self.assertRaises( 1614 ValueError, a.remove, d)16151617 Element = self.etree.Element 1618 SubElement = self.etree.SubElement 1619 1620 a = Element('a') 1621 b = SubElement(a, 'b') 1622 b.tail = 'b2' 1623 a.remove(b) 1624 self.assertXML( 1625 _bytes('<a></a>'), 1626 a) 1627 self.assertEquals('b2', b.tail)16281630 Element = self.etree.Element 1631 SubElement = self.etree.SubElement 1632 1633 a = Element('a') 1634 b = SubElement(a, 'b') 1635 c = SubElement(a, 'c') 1636 d = SubElement(b, 'd') 1637 e = SubElement(c, 'e') 1638 self.assertXML( 1639 _bytes('<a><b><d></d></b><c><e></e></c></a>'), 1640 a) 1641 self.assertEquals( 1642 [b, c], 1643 a.getchildren()) 1644 self.assertEquals( 1645 [d], 1646 b.getchildren()) 1647 self.assertEquals( 1648 [], 1649 d.getchildren())16501652 Element = self.etree.Element 1653 1654 a = Element('a') 1655 b = a.makeelement('c', {'hoi':'dag'}) 1656 self.assertXML( 1657 _bytes('<c hoi="dag"></c>'), 1658 b)1659 1660 required_versions_ET['test_iter'] = (1,3)1662 Element = self.etree.Element 1663 SubElement = self.etree.SubElement 1664 1665 a = Element('a') 1666 b = SubElement(a, 'b') 1667 c = SubElement(a, 'c') 1668 d = SubElement(b, 'd') 1669 e = SubElement(c, 'e') 1670 1671 self.assertEquals( 1672 [a, b, d, c, e], 1673 list(a.iter())) 1674 self.assertEquals( 1675 [d], 1676 list(d.iter()))16771679 Element = self.etree.Element 1680 SubElement = self.etree.SubElement 1681 1682 a = Element('a') 1683 b = SubElement(a, 'b') 1684 c = SubElement(a, 'c') 1685 d = SubElement(b, 'd') 1686 e = SubElement(c, 'e') 1687 1688 self.assertEquals( 1689 [a, b, d, c, e], 1690 list(a.getiterator())) 1691 self.assertEquals( 1692 [d], 1693 list(d.getiterator()))16941696 Element = self.etree.Element 1697 SubElement = self.etree.SubElement 1698 1699 a = Element('a') 1700 b = SubElement(a, 'b') 1701 c = SubElement(a, 'c') 1702 d = SubElement(b, 'd') 1703 e = SubElement(c, 'e') 1704 1705 self.assertEquals( 1706 [], 1707 list(a.getiterator('none'))) 1708 self.assertEquals( 1709 [], 1710 list(e.getiterator('none'))) 1711 self.assertEquals( 1712 [e], 1713 list(e.getiterator()))17141716 Element = self.etree.Element 1717 SubElement = self.etree.SubElement 1718 1719 a = Element('a') 1720 b = SubElement(a, 'b') 1721 c = SubElement(a, 'c') 1722 d = SubElement(b, 'd') 1723 e = SubElement(c, 'e') 1724 1725 self.assertEquals( 1726 [a], 1727 list(a.getiterator('a'))) 1728 a2 = SubElement(e, 'a') 1729 self.assertEquals( 1730 [a, a2], 1731 list(a.getiterator('a'))) 1732 self.assertEquals( 1733 [a2], 1734 list(c.getiterator('a')))17351737 Element = self.etree.Element 1738 SubElement = self.etree.SubElement 1739 1740 a = Element('a') 1741 b = SubElement(a, 'b') 1742 c = SubElement(a, 'c') 1743 d = SubElement(b, 'd') 1744 e = SubElement(c, 'e') 1745 1746 self.assertEquals( 1747 [a, b, d, c, e], 1748 list(a.getiterator('*')))17491751 Element = self.etree.Element 1752 Comment = self.etree.Comment 1753 SubElement = self.etree.SubElement 1754 1755 a = Element('a') 1756 b = SubElement(a, 'b') 1757 comment_b = Comment("TEST-b") 1758 b.append(comment_b) 1759 1760 self.assertEquals( 1761 [comment_b], 1762 list(a.getiterator(Comment))) 1763 1764 comment_a = Comment("TEST-a") 1765 a.append(comment_a) 1766 1767 self.assertEquals( 1768 [comment_b, comment_a], 1769 list(a.getiterator(Comment))) 1770 1771 self.assertEquals( 1772 [comment_b], 1773 list(b.getiterator(Comment)))17741776 Element = self.etree.Element 1777 PI = self.etree.ProcessingInstruction 1778 SubElement = self.etree.SubElement 1779 1780 a = Element('a') 1781 b = SubElement(a, 'b') 1782 pi_b = PI("TEST-b") 1783 b.append(pi_b) 1784 1785 self.assertEquals( 1786 [pi_b], 1787 list(a.getiterator(PI))) 1788 1789 pi_a = PI("TEST-a") 1790 a.append(pi_a) 1791 1792 self.assertEquals( 1793 [pi_b, pi_a], 1794 list(a.getiterator(PI))) 1795 1796 self.assertEquals( 1797 [pi_b], 1798 list(b.getiterator(PI)))17991801 Element = self.etree.Element 1802 SubElement = self.etree.SubElement 1803 1804 a = Element('a') 1805 a.text = 'a' 1806 b = SubElement(a, 'b') 1807 b.text = 'b' 1808 b.tail = 'b1' 1809 c = SubElement(a, 'c') 1810 c.text = 'c' 1811 c.tail = 'c1' 1812 d = SubElement(b, 'd') 1813 c.text = 'd' 1814 c.tail = 'd1' 1815 e = SubElement(c, 'e') 1816 e.text = 'e' 1817 e.tail = 'e1' 1818 1819 self.assertEquals( 1820 [a, b, d, c, e], 1821 list(a.getiterator()))1822 #self.assertEquals( 1823 # [d], 1824 # list(d.getiterator())) 18251827 Element = self.etree.Element 1828 SubElement = self.etree.SubElement 1829 1830 a = Element('a') 1831 a.text = 'a' 1832 b = SubElement(a, 'b') 1833 b.text = 'b' 1834 b.tail = 'b1' 1835 c = SubElement(a, 'c') 1836 c.text = 'c' 1837 c.tail = 'c1' 1838 d = SubElement(b, 'd') 1839 c.text = 'd' 1840 c.tail = 'd1' 1841 e = SubElement(c, 'e') 1842 e.text = 'e' 1843 e.tail = 'e1' 1844 1845 self.assertEquals( 1846 [a], 1847 list(a.getiterator('a'))) 1848 a2 = SubElement(e, 'a') 1849 self.assertEquals( 1850 [a, a2], 1851 list(a.getiterator('a'))) 1852 self.assertEquals( 1853 [a2], 1854 list(e.getiterator('a')))18551857 Element = self.etree.Element 1858 SubElement = self.etree.SubElement 1859 1860 a = Element('a') 1861 b = SubElement(a, 'b') 1862 c = SubElement(a, 'c') 1863 d = SubElement(a, 'd') 1864 1865 self.assertEquals( 1866 [b, c], 1867 a[0:2]) 1868 self.assertEquals( 1869 [b, c, d], 1870 a[:]) 1871 self.assertEquals( 1872 [b, c, d], 1873 a[:10]) 1874 self.assertEquals( 1875 [b], 1876 a[0:1]) 1877 self.assertEquals( 1878 [], 1879 a[10:12])18801882 Element = self.etree.Element 1883 SubElement = self.etree.SubElement 1884 1885 a = Element('a') 1886 b = SubElement(a, 'b') 1887 c = SubElement(a, 'c') 1888 d = SubElement(a, 'd') 1889 1890 self.assertEquals( 1891 [d], 1892 a[-1:]) 1893 self.assertEquals( 1894 [c, d], 1895 a[-2:]) 1896 self.assertEquals( 1897 [c], 1898 a[-2:-1]) 1899 self.assertEquals( 1900 [b, c], 1901 a[-3:-1]) 1902 self.assertEquals( 1903 [b, c], 1904 a[-3:2])19051907 Element = self.etree.Element 1908 SubElement = self.etree.SubElement 1909 1910 a = Element('a') 1911 b = SubElement(a, 'b') 1912 c = SubElement(a, 'c') 1913 d = SubElement(a, 'd') 1914 e = SubElement(a, 'e') 1915 1916 self.assertEquals( 1917 [e,d,c,b], 1918 a[::-1]) 1919 self.assertEquals( 1920 [b,d], 1921 a[::2]) 1922 self.assertEquals( 1923 [e,c], 1924 a[::-2]) 1925 self.assertEquals( 1926 [d,c], 1927 a[-2:0:-1]) 1928 self.assertEquals( 1929 [e], 1930 a[:1:-2])19311933 ElementTree = self.etree.ElementTree 1934 1935 f = BytesIO('<a><b>B</b>B1<c>C</c>C1</a>') 1936 doc = ElementTree(file=f) 1937 a = doc.getroot() 1938 b = a[0] 1939 c = a[1] 1940 self.assertEquals( 1941 [b, c], 1942 a[:]) 1943 self.assertEquals( 1944 [b], 1945 a[0:1]) 1946 self.assertEquals( 1947 [c], 1948 a[1:])19491951 Element = self.etree.Element 1952 Comment = self.etree.Comment 1953 SubElement = self.etree.SubElement 1954 1955 a = Element('a') 1956 b = SubElement(a, 'b') 1957 foo = Comment('foo') 1958 a.append(foo) 1959 c = SubElement(a, 'c') 1960 self.assertEquals( 1961 [b, foo, c], 1962 a[:]) 1963 self.assertEquals( 1964 foo, 1965 a[1]) 1966 a[1] = new = Element('new') 1967 self.assertEquals( 1968 new, 1969 a[1]) 1970 self.assertXML( 1971 _bytes('<a><b></b><new></new><c></c></a>'), 1972 a)19731975 Element = self.etree.Element 1976 SubElement = self.etree.SubElement 1977 1978 a = Element('a') 1979 b = SubElement(a, 'b') 1980 c = SubElement(a, 'c') 1981 d = SubElement(a, 'd') 1982 e = SubElement(a, 'e') 1983 1984 del a[1:3] 1985 self.assertEquals( 1986 [b, e], 1987 list(a))19881990 Element = self.etree.Element 1991 SubElement = self.etree.SubElement 1992 1993 a = Element('a') 1994 b = SubElement(a, 'b') 1995 c = SubElement(a, 'c') 1996 d = SubElement(a, 'd') 1997 e = SubElement(a, 'e') 1998 1999 del a[1:-1] 2000 self.assertEquals( 2001 [b, e], 2002 list(a))20032005 Element = self.etree.Element 2006 SubElement = self.etree.SubElement 2007 2008 a = Element('a') 2009 b = SubElement(a, 'b') 2010 c = SubElement(a, 'c') 2011 d = SubElement(a, 'd') 2012 e = SubElement(a, 'e') 2013 2014 del a[-3:-1] 2015 self.assertEquals( 2016 [b, e], 2017 list(a))20182020 Element = self.etree.Element 2021 SubElement = self.etree.SubElement 2022 2023 a = Element('a') 2024 b = SubElement(a, 'b') 2025 c = SubElement(a, 'c') 2026 d = SubElement(a, 'd') 2027 e = SubElement(a, 'e') 2028 2029 del a[1::2] 2030 self.assertEquals( 2031 [b, d], 2032 list(a))20332035 Element = self.etree.Element 2036 SubElement = self.etree.SubElement 2037 2038 a = Element('a') 2039 b = SubElement(a, 'b') 2040 c = SubElement(a, 'c') 2041 d = SubElement(a, 'd') 2042 e = SubElement(a, 'e') 2043 2044 del a[::-1] 2045 self.assertEquals( 2046 [], 2047 list(a))20482050 Element = self.etree.Element 2051 SubElement = self.etree.SubElement 2052 2053 a = Element('a') 2054 b = SubElement(a, 'b') 2055 c = SubElement(a, 'c') 2056 d = SubElement(a, 'd') 2057 e = SubElement(a, 'e') 2058 2059 del a[::-2] 2060 self.assertEquals( 2061 [b, d], 2062 list(a))20632065 ElementTree = self.etree.ElementTree 2066 f = BytesIO('<a><b></b>B2<c></c>C2<d></d>D2<e></e>E2</a>') 2067 doc = ElementTree(file=f) 2068 a = doc.getroot() 2069 del a[1:3] 2070 self.assertXML( 2071 _bytes('<a><b></b>B2<e></e>E2</a>'), 2072 a)20732075 XML = self.etree.XML 2076 a = XML(_bytes('<a><b></b>B2<c></c>C2</a>')) 2077 b, c = a 2078 2079 del a[:] 2080 2081 self.assertEquals("B2", b.tail) 2082 self.assertEquals("C2", c.tail)20832085 # this could trigger a crash 2086 Element = self.etree.Element 2087 SubElement = self.etree.SubElement 2088 a = Element('a') 2089 b = SubElement(a, 'b') 2090 c = SubElement(b, 'c') 2091 del b # no more reference to b 2092 del a[:] 2093 self.assertEquals('c', c.tag)20942096 Element = self.etree.Element 2097 SubElement = self.etree.SubElement 2098 2099 a = Element('a') 2100 b = SubElement(a, 'b') 2101 c = SubElement(a, 'c') 2102 d = SubElement(a, 'd') 2103 2104 e = Element('e') 2105 f = Element('f') 2106 g = Element('g') 2107 2108 s = [e, f, g] 2109 a[1:2] = s 2110 self.assertEquals( 2111 [b, e, f, g, d], 2112 list(a))21132115 Element = self.etree.Element 2116 SubElement = self.etree.SubElement 2117 2118 a = Element('a') 2119 b = SubElement(a, 'b') 2120 c = SubElement(a, 'c') 2121 2122 e = Element('e') 2123 f = Element('f') 2124 g = Element('g') 2125 2126 s = [e, f, g] 2127 a[:] = s 2128 self.assertEquals( 2129 [e, f, g], 2130 list(a))21312133 Element = self.etree.Element 2134 SubElement = self.etree.SubElement 2135 2136 a = Element('a') 2137 2138 e = Element('e') 2139 f = Element('f') 2140 g = Element('g') 2141 2142 s = [e, f, g] 2143 a[:] = s 2144 self.assertEquals( 2145 [e, f, g], 2146 list(a))21472149 Element = self.etree.Element 2150 SubElement = self.etree.SubElement 2151 2152 a = Element('a') 2153 b = SubElement(a, 'b') 2154 c = SubElement(a, 'c') 2155 d = SubElement(a, 'd') 2156 2157 s = [b, c, d] 2158 a[:] = s 2159 self.assertEquals( 2160 [b, c, d], 2161 list(a))21622164 Element = self.etree.Element 2165 SubElement = self.etree.SubElement 2166 2167 a = Element('a') 2168 b = SubElement(a, 'b') 2169 c = SubElement(a, 'c') 2170 d = SubElement(a, 'd') 2171 2172 s = [d, c, b] 2173 a[:] = s 2174 self.assertEquals( 2175 [d, c, b], 2176 list(a))21772179 Element = self.etree.Element 2180 SubElement = self.etree.SubElement 2181 2182 a = Element('{ns}a') 2183 b = SubElement(a, '{ns}b', {'{ns1}a1': 'test'}) 2184 c = SubElement(a, '{ns}c', {'{ns2}a2': 'test'}) 2185 d = SubElement(a, '{ns}d', {'{ns3}a3': 'test'}) 2186 2187 s = [d, c, b] 2188 a[:] = s 2189 self.assertEquals( 2190 [d, c, b], 2191 list(a)) 2192 self.assertEquals( 2193 ['{ns}d', '{ns}c', '{ns}b'], 2194 [ child.tag for child in a ]) 2195 2196 self.assertEquals( 2197 [['{ns3}a3'], ['{ns2}a2'], ['{ns1}a1']], 2198 [ list(child.attrib.keys()) for child in a ])21992201 Element = self.etree.Element 2202 SubElement = self.etree.SubElement 2203 2204 a = Element('{ns}a') 2205 b = SubElement(a, '{ns1}b', {'{ns}a1': 'test'}) 2206 c = SubElement(a, '{ns2}c', {'{ns}a2': 'test'}) 2207 d = SubElement(a, '{ns3}d', {'{ns}a3': 'test'}) 2208 2209 s = [d, c, b] 2210 a[:] = s 2211 self.assertEquals( 2212 [d, c, b], 2213 list(a)) 2214 self.assertEquals( 2215 ['{ns3}d', '{ns2}c', '{ns1}b'], 2216 [ child.tag for child in a ]) 2217 2218 self.assertEquals( 2219 [['{ns}a3'], ['{ns}a2'], ['{ns}a1']], 2220 [ list(child.attrib.keys()) for child in a ])22212223 Element = self.etree.Element 2224 SubElement = self.etree.SubElement 2225 2226 a = Element('a') 2227 b = SubElement(a, 'b') 2228 c = SubElement(a, 'c') 2229 2230 e = Element('e') 2231 f = Element('f') 2232 g = Element('g') 2233 h = Element('h') 2234 2235 s = [e, f] 2236 a[99:] = s 2237 self.assertEquals( 2238 [a, b, e, f], 2239 list(a)) 2240 2241 s = [g, h] 2242 a[:0] = s 2243 self.assertEquals( 2244 [g, h, a, b, e, f], 2245 list(a))22462248 Element = self.etree.Element 2249 SubElement = self.etree.SubElement 2250 2251 a = Element('a') 2252 b = SubElement(a, 'b') 2253 c = SubElement(a, 'c') 2254 2255 e = Element('e') 2256 f = Element('f') 2257 2258 s = [e] 2259 a[0:1] = s 2260 self.assertEquals( 2261 [e, c], 2262 list(a)) 2263 2264 s = [f] 2265 a[1:2] = s 2266 self.assertEquals( 2267 [e, f], 2268 list(a))22692271 ElementTree = self.etree.ElementTree 2272 Element = self.etree.Element 2273 f = BytesIO('<a><b></b>B2<c></c>C2<d></d>D2<e></e>E2</a>') 2274 doc = ElementTree(file=f) 2275 a = doc.getroot() 2276 x = Element('x') 2277 y = Element('y') 2278 z = Element('z') 2279 x.tail = 'X2' 2280 y.tail = 'Y2' 2281 z.tail = 'Z2' 2282 a[1:3] = [x, y, z] 2283 self.assertXML( 2284 _bytes('<a><b></b>B2<x></x>X2<y></y>Y2<z></z>Z2<e></e>E2</a>'), 2285 a)22862288 Element = self.etree.Element 2289 SubElement = self.etree.SubElement 2290 2291 a = Element('a') 2292 b = SubElement(a, 'b') 2293 c = SubElement(a, 'c') 2294 d = SubElement(a, 'd') 2295 2296 x = Element('x') 2297 y = Element('y') 2298 2299 a[1:-1] = [x, y] 2300 self.assertEquals( 2301 [b, x, y, d], 2302 list(a))23032305 Element = self.etree.Element 2306 SubElement = self.etree.SubElement 2307 2308 a = Element('a') 2309 b = SubElement(a, 'b') 2310 c = SubElement(a, 'c') 2311 d = SubElement(a, 'd') 2312 2313 x = Element('x') 2314 y = Element('y') 2315 2316 a[1:-2] = [x, y] 2317 self.assertEquals( 2318 [b, x, y, c, d], 2319 list(a))23202322 Element = self.etree.Element 2323 SubElement = self.etree.SubElement 2324 2325 a = Element('a') 2326 b = SubElement(a, 'b') 2327 c = SubElement(a, 'c') 2328 d = SubElement(a, 'd') 2329 2330 e = Element('e') 2331 f = Element('f') 2332 g = Element('g') 2333 2334 s = [e, f, g] 2335 a[3:] = s 2336 self.assertEquals( 2337 [b, c, d, e, f, g], 2338 list(a))23392341 Element = self.etree.Element 2342 2343 a = Element('a') 2344 2345 b = Element('b') 2346 c = Element('c') 2347 2348 a[:] = [b, c] 2349 self.assertEquals( 2350 [b, c], 2351 list(a))23522354 Element = self.etree.Element 2355 ElementTree = self.etree.ElementTree 2356 2357 a = Element('a') 2358 a.tail = 'A2' 2359 t = ElementTree(element=a) 2360 self.assertEquals('A2', 2361 a.tail)23622364 Element = self.etree.Element 2365 SubElement = self.etree.SubElement 2366 ElementTree = self.etree.ElementTree 2367 2368 a = Element('a') 2369 b = SubElement(a, 'b') 2370 c = SubElement(a, 'c') 2371 d = SubElement(b, 'd') 2372 e = SubElement(c, 'e') 2373 t = ElementTree(element=a) 2374 2375 self.assertEquals( 2376 [a, b, d, c, e], 2377 list(t.getiterator()))23782380 Element = self.etree.Element 2381 SubElement = self.etree.SubElement 2382 ElementTree = self.etree.ElementTree 2383 a = Element('a') 2384 b = SubElement(a, 'b') 2385 c = SubElement(a, 'c') 2386 d = SubElement(b, 'd') 2387 e = SubElement(c, 'e') 2388 t = ElementTree(element=a) 2389 2390 self.assertEquals( 2391 [a], 2392 list(t.getiterator('a'))) 2393 a2 = SubElement(e, 'a') 2394 self.assertEquals( 2395 [a, a2], 2396 list(t.getiterator('a')))23972399 ElementTree = self.etree.ElementTree 2400 ns = 'http://xml.infrae.com/1' 2401 f = BytesIO('<x:a xmlns:x="%s"><x:b></x:b></x:a>' % ns) 2402 t = ElementTree(file=f) 2403 a = t.getroot() 2404 self.assertEquals('{%s}a' % ns, 2405 a.tag) 2406 self.assertEquals('{%s}b' % ns, 2407 a[0].tag)24082410 ElementTree = self.etree.ElementTree 2411 ns = 'http://xml.infrae.com/1' 2412 ns2 = 'http://xml.infrae.com/2' 2413 f = BytesIO('<x:a xmlns:x="%s" xmlns:y="%s"><x:b></x:b><y:b></y:b></x:a>' % (ns, ns2)) 2414 t = ElementTree(file=f) 2415 a = t.getroot() 2416 self.assertEquals('{%s}a' % ns, 2417 a.tag) 2418 self.assertEquals('{%s}b' % ns, 2419 a[0].tag) 2420 self.assertEquals('{%s}b' % ns2, 2421 a[1].tag)24222424 Element = self.etree.Element 2425 SubElement = self.etree.SubElement 2426 ns = 'http://xml.infrae.com/1' 2427 ns2 = 'http://xml.infrae.com/2' 2428 a = Element('{%s}a' % ns) 2429 b = SubElement(a, '{%s}b' % ns2) 2430 c = SubElement(a, '{%s}c' % ns) 2431 self.assertEquals('{%s}a' % ns, 2432 a.tag) 2433 self.assertEquals('{%s}b' % ns2, 2434 b.tag) 2435 self.assertEquals('{%s}c' % ns, 2436 c.tag) 2437 self.assertEquals('{%s}a' % ns, 2438 a.tag) 2439 self.assertEquals('{%s}b' % ns2, 2440 b.tag) 2441 self.assertEquals('{%s}c' % ns, 2442 c.tag)24432445 Element = self.etree.Element 2446 SubElement = self.etree.SubElement 2447 ElementTree = self.etree.ElementTree 2448 2449 ns = 'http://xml.infrae.com/1' 2450 ns2 = 'http://xml.infrae.com/2' 2451 f = BytesIO('<a xmlns="%s" xmlns:x="%s"><x:b></x:b><b></b></a>' % (ns, ns2)) 2452 t = ElementTree(file=f) 2453 2454 a = t.getroot() 2455 self.assertEquals('{%s}a' % ns, 2456 a.tag) 2457 self.assertEquals('{%s}b' % ns2, 2458 a[0].tag) 2459 self.assertEquals('{%s}b' % ns, 2460 a[1].tag)24612463 Element = self.etree.Element 2464 ns = 'http://xml.infrae.com/1' 2465 ns2 = 'http://xml.infrae.com/2' 2466 a = Element('a') 2467 a.set('{%s}foo' % ns, 'Foo') 2468 a.set('{%s}bar' % ns2, 'Bar') 2469 self.assertEquals( 2470 'Foo', 2471 a.get('{%s}foo' % ns)) 2472 self.assertEquals( 2473 'Bar', 2474 a.get('{%s}bar' % ns2)) 2475 try: 2476 self.assertXML( 2477 _bytes('<a xmlns:ns0="%s" xmlns:ns1="%s" ns0:foo="Foo" ns1:bar="Bar"></a>' % (ns, ns2)), 2478 a) 2479 except AssertionError: 2480 self.assertXML( 2481 _bytes('<a xmlns:ns0="%s" xmlns:ns1="%s" ns1:foo="Foo" ns0:bar="Bar"></a>' % (ns2, ns)), 2482 a)24832485 Element = self.etree.Element 2486 one = self.etree.fromstring( 2487 _bytes('<foo><bar xmlns:ns="http://a.b.c"><ns:baz/></bar></foo>')) 2488 baz = one[0][0] 2489 2490 two = Element('root') 2491 two.append(baz) 2492 # removing the originating document could cause a crash/error before 2493 # as namespace is not moved along with it 2494 del one, baz 2495 self.assertEquals('{http://a.b.c}baz', two[0].tag)24962498 tostring = self.etree.tostring 2499 root = self.etree.XML( 2500 _bytes('<foo><bar xmlns:ns="http://a.b.c"><ns:baz/></bar></foo>')) 2501 baz = root[0][0] 2502 2503 nsdecl = re.findall(_bytes("xmlns(?::[a-z0-9]+)?=[\"']([^\"']+)[\"']"), 2504 tostring(baz)) 2505 self.assertEquals([_bytes("http://a.b.c")], nsdecl)25062508 tostring = self.etree.tostring 2509 root = self.etree.XML( 2510 _bytes('<foo><bar xmlns="http://a.b.c"><baz/></bar></foo>')) 2511 baz = root[0][0] 2512 2513 nsdecl = re.findall(_bytes("xmlns(?::[a-z0-9]+)?=[\"']([^\"']+)[\"']"), 2514 tostring(baz)) 2515 self.assertEquals([_bytes("http://a.b.c")], nsdecl)25162518 tostring = self.etree.tostring 2519 root = self.etree.XML( 2520 _bytes('<foo xmlns:ns="http://a.b.c"><bar><ns:baz/></bar></foo>')) 2521 baz = root[0][0] 2522 2523 nsdecl = re.findall(_bytes("xmlns(?::[a-z0-9]+)?=[\"']([^\"']+)[\"']"), 2524 tostring(baz)) 2525 2526 self.assertEquals([_bytes("http://a.b.c")], nsdecl)25272529 Element = self.etree.Element 2530 SubElement = self.etree.SubElement 2531 2532 root = Element("foo") 2533 bar = SubElement(root, "{http://a.b.c}bar") 2534 baz = SubElement(bar, "{http://a.b.c}baz") 2535 2536 nsdecl = re.findall(_bytes("xmlns(?::[a-z0-9]+)?=[\"']([^\"']+)[\"']"), 2537 self.etree.tostring(baz)) 2538 2539 self.assertEquals([_bytes("http://a.b.c")], nsdecl)25402542 Element = self.etree.Element 2543 2544 root = Element('element') 2545 2546 subelement = Element('subelement', 2547 {"{http://www.w3.org/XML/1998/namespace}id": "foo"}) 2548 self.assertEquals(1, len(subelement.attrib)) 2549 self.assertEquals( 2550 "foo", 2551 subelement.get("{http://www.w3.org/XML/1998/namespace}id")) 2552 2553 root.append(subelement) 2554 self.assertEquals(1, len(subelement.attrib)) 2555 self.assertEquals( 2556 list({"{http://www.w3.org/XML/1998/namespace}id" : "foo"}.items()), 2557 list(subelement.attrib.items())) 2558 self.assertEquals( 2559 "foo", 2560 subelement.get("{http://www.w3.org/XML/1998/namespace}id"))25612563 parse = self.etree.parse 2564 tostring = self.etree.tostring 2565 2566 ns_href = "http://a.b.c" 2567 one = parse( 2568 BytesIO('<foo><bar xmlns:ns="%s"><ns:baz/></bar></foo>' % ns_href)) 2569 baz = one.getroot()[0][0] 2570 2571 parsed = parse(BytesIO( tostring(baz) )).getroot() 2572 self.assertEquals('{%s}baz' % ns_href, parsed.tag)25732575 fromstring = self.etree.fromstring 2576 tostring = self.etree.tostring 2577 2578 ns_href = "http://a.b.c" 2579 xml = _bytes('<root xmlns="%s" xmlns:x="%s"><el x:a="test" /></root>' % ( 2580 ns_href,ns_href)) 2581 root = fromstring(xml) 2582 self.assertEquals('test', root[0].get('{%s}a' % ns_href)) 2583 2584 xml2 = tostring(root) 2585 self.assertTrue(_bytes(':a=') in xml2, xml2) 2586 2587 root2 = fromstring(xml2) 2588 self.assertEquals('test', root[0].get('{%s}a' % ns_href))25892591 fromstring = self.etree.fromstring 2592 tostring = self.etree.tostring 2593 2594 ns_href = "http://a.b.c" 2595 xml = _bytes('<root xmlns="%s" xmlns:x="%s"><el x:a="test" /></root>' % ( 2596 ns_href,ns_href)) 2597 root = fromstring(xml) 2598 self.assertEquals('test', root[0].get('{%s}a' % ns_href)) 2599 2600 root[0].set('{%s}a' % ns_href, 'TEST') 2601 2602 xml2 = tostring(root) 2603 self.assertTrue(_bytes(':a=') in xml2, xml2) 2604 2605 root2 = fromstring(xml2) 2606 self.assertEquals('TEST', root[0].get('{%s}a' % ns_href))2607 2608 required_versions_ET['test_register_namespace'] = (1,3)2610 # ET 1.3+ 2611 Element = self.etree.Element 2612 tostring = self.etree.tostring 2613 prefix = 'TESTPREFIX' 2614 namespace = 'http://seriously.unknown/namespace/URI' 2615 2616 el = Element('{%s}test' % namespace) 2617 self.assertEquals(_bytes('<ns0:test xmlns:ns0="%s"></ns0:test>' % namespace), 2618 self._writeElement(el)) 2619 2620 self.etree.register_namespace(prefix, namespace) 2621 el = Element('{%s}test' % namespace) 2622 self.assertEquals(_bytes('<%s:test xmlns:%s="%s"></%s:test>' % ( 2623 prefix, prefix, namespace, prefix)), 2624 self._writeElement(el)) 2625 2626 self.assertRaises(ValueError, self.etree.register_namespace, 'ns25', namespace)26272629 tostring = self.etree.tostring 2630 Element = self.etree.Element 2631 SubElement = self.etree.SubElement 2632 2633 a = Element('a') 2634 b = SubElement(a, 'b') 2635 c = SubElement(a, 'c') 2636 2637 self.assertEquals(_bytes('<a><b></b><c></c></a>'), 2638 canonicalize(tostring(a)))26392641 tostring = self.etree.tostring 2642 Element = self.etree.Element 2643 SubElement = self.etree.SubElement 2644 2645 a = Element('a') 2646 b = SubElement(a, 'b') 2647 c = SubElement(a, 'c') 2648 d = SubElement(c, 'd') 2649 self.assertEquals(_bytes('<b></b>'), 2650 canonicalize(tostring(b))) 2651 self.assertEquals(_bytes('<c><d></d></c>'), 2652 canonicalize(tostring(c)))26532655 tostring = self.etree.tostring 2656 Element = self.etree.Element 2657 SubElement = self.etree.SubElement 2658 2659 a = Element('a') 2660 b = SubElement(a, 'b') 2661 c = SubElement(a, 'c') 2662 d = SubElement(c, 'd') 2663 b.tail = 'Foo' 2664 2665 self.assert_(tostring(b) == _bytes('<b/>Foo') or 2666 tostring(b) == _bytes('<b />Foo'))2667 2668 required_versions_ET['test_tostring_method_html'] = (1,3)2670 tostring = self.etree.tostring 2671 Element = self.etree.Element 2672 SubElement = self.etree.SubElement 2673 2674 html = Element('html') 2675 body = SubElement(html, 'body') 2676 p = SubElement(body, 'p') 2677 p.text = "html" 2678 SubElement(p, 'br').tail = "test" 2679 2680 self.assertEquals(_bytes('<html><body><p>html<br>test</p></body></html>'), 2681 tostring(html, method="html"))2682 2683 required_versions_ET['test_tostring_method_text'] = (1,3)2685 tostring = self.etree.tostring 2686 Element = self.etree.Element 2687 SubElement = self.etree.SubElement 2688 2689 a = Element('a') 2690 a.text = "A" 2691 a.tail = "tail" 2692 b = SubElement(a, 'b') 2693 b.text = "B" 2694 b.tail = "TAIL" 2695 c = SubElement(a, 'c') 2696 c.text = "C" 2697 2698 self.assertEquals(_bytes('ABTAILCtail'), 2699 tostring(a, method="text"))27002702 iterparse = self.etree.iterparse 2703 f = BytesIO('<a><b></b><c/></a>') 2704 2705 iterator = iterparse(f) 2706 self.assertEquals(None, 2707 iterator.root) 2708 events = list(iterator) 2709 root = iterator.root 2710 self.assertEquals( 2711 [('end', root[0]), ('end', root[1]), ('end', root)], 2712 events)27132715 iterparse = self.etree.iterparse 2716 iterator = iterparse(fileInTestDir("test.xml")) 2717 self.assertEquals(None, 2718 iterator.root) 2719 events = list(iterator) 2720 root = iterator.root 2721 self.assertEquals( 2722 [('end', root[0]), ('end', root)], 2723 events)27242726 iterparse = self.etree.iterparse 2727 f = BytesIO('<a><b></b><c/></a>') 2728 2729 iterator = iterparse(f, events=('start',)) 2730 events = list(iterator) 2731 root = iterator.root 2732 self.assertEquals( 2733 [('start', root), ('start', root[0]), ('start', root[1])], 2734 events)27352737 iterparse = self.etree.iterparse 2738 f = BytesIO('<a><b></b><c/></a>') 2739 2740 iterator = iterparse(f, events=('start','end')) 2741 events = list(iterator) 2742 root = iterator.root 2743 self.assertEquals( 2744 [('start', root), ('start', root[0]), ('end', root[0]), 2745 ('start', root[1]), ('end', root[1]), ('end', root)], 2746 events)27472749 iterparse = self.etree.iterparse 2750 f = BytesIO('<a><b></b><c/></a>') 2751 2752 iterator = iterparse(f) 2753 for event, elem in iterator: 2754 elem.clear() 2755 2756 root = iterator.root 2757 self.assertEquals(0, 2758 len(root))27592761 iterparse = self.etree.iterparse 2762 CHILD_COUNT = 12345 2763 f = BytesIO('<a>%s</a>' % ('<b>test</b>'*CHILD_COUNT)) 2764 2765 i = 0 2766 for key in iterparse(f): 2767 event, element = key 2768 i += 1 2769 self.assertEquals(i, CHILD_COUNT + 1)27702772 iterparse = self.etree.iterparse 2773 f = BytesIO('<a xmlns="http://ns1/"><b><c xmlns="http://ns2/"/></b></a>') 2774 2775 attr_name = '{http://testns/}bla' 2776 events = [] 2777 iterator = iterparse(f, events=('start','end','start-ns','end-ns')) 2778 for event, elem in iterator: 2779 events.append(event) 2780 if event == 'start': 2781 if elem.tag != '{http://ns1/}a': 2782 elem.set(attr_name, 'value') 2783 2784 self.assertEquals( 2785 ['start-ns', 'start', 'start', 'start-ns', 'start', 2786 'end', 'end-ns', 'end', 'end', 'end-ns'], 2787 events) 2788 2789 root = iterator.root 2790 self.assertEquals( 2791 None, 2792 root.get(attr_name)) 2793 self.assertEquals( 2794 'value', 2795 root[0].get(attr_name))27962798 iterparse = self.etree.iterparse 2799 f = BytesIO('<a><b><d/></b><c/></a>') 2800 2801 counts = [] 2802 for event, elem in iterparse(f): 2803 counts.append(len(list(elem.getiterator()))) 2804 self.assertEquals( 2805 [1,2,1,4], 2806 counts)28072809 iterparse = self.etree.iterparse 2810 f = BytesIO('<a><b><d/></b><c/></a>') 2811 2812 for event, node in etree.iterparse(f): pass 2813 2814 root = etree.Element('new_root', {}) 2815 root[:] = node[:] 2816 2817 self.assertEquals( 2818 ['b', 'c'], 2819 [ el.tag for el in root ])28202822 tostring = self.etree.tostring 2823 f = BytesIO('<root><![CDATA[test]]></root>') 2824 context = self.etree.iterparse(f) 2825 content = [ el.text for event,el in context ] 2826 2827 self.assertEquals(['test'], content) 2828 self.assertEquals(_bytes('<root>test</root>'), 2829 tostring(context.root))28302832 parse = self.etree.parse 2833 # from file 2834 tree = parse(fileInTestDir('test.xml')) 2835 self.assertXML( 2836 _bytes('<a><b></b></a>'), 2837 tree.getroot())2838 2842 2846 2847 required_versions_ET['test_parse_error'] = (1,3)2849 # ET < 1.3 raises ExpatError 2850 parse = self.etree.parse 2851 f = BytesIO('<a><b></c></b></a>') 2852 self.assertRaises(SyntaxError, parse, f) 2853 f.close()2854 2855 required_versions_ET['test_parse_error_from_file'] = (1,3)2857 parse = self.etree.parse 2858 # from file 2859 f = open(fileInTestDir('test_broken.xml'), 'rb') 2860 self.assertRaises(SyntaxError, parse, f) 2861 f.close()28622864 parse = self.etree.parse 2865 # from file object 2866 f = open(fileInTestDir('test.xml'), 'rb') 2867 tree = parse(f) 2868 f.close() 2869 self.assertXML( 2870 _bytes('<a><b></b></a>'), 2871 tree.getroot())28722874 parse = self.etree.parse 2875 f = BytesIO('<a><b></b></a>') 2876 tree = parse(f) 2877 f.close() 2878 self.assertXML( 2879 _bytes('<a><b></b></a>'), 2880 tree.getroot() 2881 )28822884 tostring = self.etree.tostring 2885 root = self.etree.XML(_bytes('<root><![CDATA[test]]></root>')) 2886 2887 self.assertEquals('test', root.text) 2888 self.assertEquals(_bytes('<root>test</root>'), 2889 tostring(root))28902892 # this can fail in libxml2 <= 2.6.22 2893 parse = self.etree.parse 2894 tree = parse(BytesIO('<?xml version="1.0" encoding="ascii"?><html/>')) 2895 self.assertXML(_bytes('<html></html>'), 2896 tree.getroot())28972899 Element = self.etree.Element 2900 2901 a = Element('a') 2902 a.text = _str('Søk på nettet') 2903 self.assertXML( 2904 _str('<a>Søk på nettet</a>').encode('UTF-8'), 2905 a, 'utf-8')29062908 ElementTree = self.etree.ElementTree 2909 Element = self.etree.Element 2910 2911 a = Element('a') 2912 a.text = _str('Søk på nettet') 2913 2914 f = BytesIO() 2915 tree = ElementTree(element=a) 2916 tree.write(f, encoding='utf-8') 2917 self.assertEquals(_str('<a>Søk på nettet</a>').encode('UTF-8'), 2918 f.getvalue().replace(_bytes('\n'),_bytes('')))29192921 parse = self.etree.parse 2922 # from file 2923 tree = parse(fileInTestDir('test-string.xml')) 2924 self.assertXML( 2925 _str('<a>Søk på nettet</a>').encode('UTF-8'), 2926 tree.getroot(), 'UTF-8')29272929 parse = self.etree.parse 2930 # from file object 2931 f = open(fileInTestDir('test-string.xml'), 'rb') 2932 tree = parse(f) 2933 f.close() 2934 self.assertXML( 2935 _str('<a>Søk på nettet</a>').encode('UTF-8'), 2936 tree.getroot(), 'UTF-8')29372939 ElementTree = self.etree.ElementTree 2940 Element = self.etree.Element 2941 2942 a = Element('a') 2943 a.text = _str('Søk på nettet') 2944 2945 f = BytesIO() 2946 tree = ElementTree(element=a) 2947 tree.write(f, encoding='iso-8859-1') 2948 result = f.getvalue() 2949 declaration = _bytes("<?xml version=\'1.0\' encoding=\'iso-8859-1\'?>") 2950 self.assertEncodingDeclaration(result, _bytes('iso-8859-1')) 2951 result = result.split(_bytes('?>'), 1)[-1].replace(_bytes('\n'),_bytes('')) 2952 self.assertEquals(_str('<a>Søk på nettet</a>').encode('iso-8859-1'), 2953 result)2954 2955 required_versions_ET['test_parse_encoding_8bit_explicit'] = (1,3)2957 XMLParser = self.etree.XMLParser 2958 2959 text = _str('Søk på nettet') 2960 xml_latin1 = (_str('<a>%s</a>') % text).encode('iso-8859-1') 2961 2962 self.assertRaises(self.etree.ParseError, 2963 self.etree.parse, 2964 BytesIO(xml_latin1)) 2965 2966 tree = self.etree.parse(BytesIO(xml_latin1), 2967 XMLParser(encoding="iso-8859-1")) 2968 a = tree.getroot() 2969 self.assertEquals(a.text, text)2970 2971 required_versions_ET['test_parse_encoding_8bit_override'] = (1,3)2973 XMLParser = self.etree.XMLParser 2974 2975 text = _str('Søk på nettet') 2976 wrong_declaration = _str("<?xml version='1.0' encoding='UTF-8'?>") 2977 xml_latin1 = (_str('%s<a>%s</a>') % (wrong_declaration, text) 2978 ).encode('iso-8859-1') 2979 2980 self.assertRaises(self.etree.ParseError, 2981 self.etree.parse, 2982 BytesIO(xml_latin1)) 2983 2984 tree = self.etree.parse(BytesIO(xml_latin1), 2985 XMLParser(encoding="iso-8859-1")) 2986 a = tree.getroot() 2987 self.assertEquals(a.text, text)29882990 # raise error on wrong encoding declaration in unicode strings 2991 XML = self.etree.XML 2992 test_utf = (_str('<?xml version="1.0" encoding="iso-8859-1"?>') + 2993 _str('<a>Søk på nettet</a>')) 2994 self.assertRaises(SyntaxError, XML, test_utf)29952997 ElementTree = self.etree.ElementTree 2998 Element = self.etree.Element 2999 3000 a = Element('a') 3001 a.text = _str('Søk på nettet') 3002 3003 f = BytesIO() 3004 tree = ElementTree(element=a) 3005 tree.write(f) 3006 data = f.getvalue().replace(_bytes('\n'),_bytes('')) 3007 self.assertEquals( 3008 _str('<a>Søk på nettet</a>').encode('ASCII', 'xmlcharrefreplace'), 3009 data)30103012 Element = self.etree.Element 3013 tostring = self.etree.tostring 3014 3015 a = Element('a') 3016 a.text = _str('Søk på nettet') 3017 self.assertEquals(_str('<a>Søk på nettet</a>').encode('UTF-8'), 3018 tostring(a, encoding='utf-8'))30193021 Element = self.etree.Element 3022 tostring = self.etree.tostring 3023 3024 a = Element('a') 3025 a.text = _str('Søk på nettet') 3026 self.assertRaises(LookupError, tostring, a, 3027 encoding='Invalid Encoding')30283030 Element = self.etree.Element 3031 SubElement = self.etree.SubElement 3032 tostring = self.etree.tostring 3033 3034 a = Element('a') 3035 b = SubElement(a, 'b') 3036 b.text = _str('Søk på nettet') 3037 self.assertEquals(_str('<b>Søk på nettet</b>').encode('UTF-8'), 3038 tostring(b, encoding='utf-8'))30393041 Element = self.etree.Element 3042 SubElement = self.etree.SubElement 3043 tostring = self.etree.tostring 3044 3045 a = Element('a') 3046 b = SubElement(a, 'b') 3047 b.text = _str('Søk på nettet') 3048 b.tail = _str('Søk') 3049 self.assertEquals(_str('<b>Søk på nettet</b>Søk').encode('UTF-8'), 3050 tostring(b, encoding='utf-8'))30513053 Element = self.etree.Element 3054 SubElement = self.etree.SubElement 3055 tostring = self.etree.tostring 3056 3057 a = Element('a') 3058 a.text = _str('Søk på nettet') 3059 3060 expected = _bytes('<a>Søk på nettet</a>') 3061 self.assertEquals( 3062 expected, 3063 tostring(a))30643066 Element = self.etree.Element 3067 SubElement = self.etree.SubElement 3068 tostring = self.etree.tostring 3069 3070 a = Element('a') 3071 b = SubElement(a, 'b') 3072 b.text = _str('Søk på nettet') 3073 3074 expected = _bytes('<b>Søk på nettet</b>') 3075 self.assertEquals( 3076 expected, 3077 tostring(b))30783080 utext = _str('Søk på nettet') 3081 uxml = _str('<p>%s</p>') % utext 3082 prologue = _bytes('<?xml version="1.0" encoding="iso-8859-1" ?>') 3083 isoxml = prologue + uxml.encode('iso-8859-1') 3084 tree = self.etree.XML(isoxml) 3085 self.assertEquals(utext, tree.text)30863088 utext = _str('Søk på nettet') 3089 uxml = (_str('<?xml version="1.0" encoding="UTF-8"?>') + 3090 _str('<p>%s</p>') % utext) 3091 bom = _bytes('\\xEF\\xBB\\xBF').decode("unicode_escape").encode("latin1") 3092 xml = bom + uxml.encode("utf-8") 3093 tree = etree.XML(xml) 3094 self.assertEquals(utext, tree.text)30953097 utext = _str('Søk på nettet') 3098 uxml = _str('<p>%s</p>') % utext 3099 prologue = _bytes('<?xml version="1.0" encoding="iso-8859-1" ?>') 3100 isoxml = prologue + uxml.encode('iso-8859-1') 3101 el = self.etree.parse(BytesIO(isoxml)).getroot() 3102 self.assertEquals(utext, el.text)31033105 Element = self.etree.Element 3106 ElementTree = self.etree.ElementTree 3107 3108 a = Element('a') 3109 a.text = "Foo" 3110 atree = ElementTree(a) 3111 3112 btree = copy.deepcopy(atree) 3113 self.assertEqual("Foo", atree.getroot().text) 3114 self.assertEqual("Foo", btree.getroot().text) 3115 self.assertFalse(btree is atree) 3116 self.assertFalse(btree.getroot() is atree.getroot())31173119 Element = self.etree.Element 3120 3121 a = Element('a') 3122 a.text = 'Foo' 3123 3124 b = copy.deepcopy(a) 3125 self.assertEquals('Foo', b.text) 3126 3127 b.text = 'Bar' 3128 self.assertEquals('Bar', b.text) 3129 self.assertEquals('Foo', a.text) 3130 3131 del a 3132 self.assertEquals('Bar', b.text)31333135 Element = self.etree.Element 3136 3137 a = Element('a') 3138 a.tail = 'Foo' 3139 3140 b = copy.deepcopy(a) 3141 self.assertEquals('Foo', b.tail) 3142 3143 b.tail = 'Bar' 3144 self.assertEquals('Bar', b.tail) 3145 self.assertEquals('Foo', a.tail) 3146 3147 del a 3148 self.assertEquals('Bar', b.tail)31493151 Element = self.etree.Element 3152 SubElement = self.etree.SubElement 3153 3154 root = Element('root') 3155 a = SubElement(root, 'a') 3156 a.text = 'FooText' 3157 a.tail = 'FooTail' 3158 3159 b = copy.deepcopy(a) 3160 self.assertEquals('FooText', b.text) 3161 self.assertEquals('FooTail', b.tail) 3162 3163 b.text = 'BarText' 3164 b.tail = 'BarTail' 3165 self.assertEquals('BarTail', b.tail) 3166 self.assertEquals('FooTail', a.tail) 3167 self.assertEquals('BarText', b.text) 3168 self.assertEquals('FooText', a.text) 3169 3170 del a 3171 self.assertEquals('BarTail', b.tail) 3172 self.assertEquals('BarText', b.text)31733175 root = self.etree.XML(_bytes('''<doc xmlns="dns" xmlns:t="tns"> 3176 <parent><node t:foo="bar" /></parent> 3177 </doc>''')) 3178 self.assertEquals( 3179 root[0][0].get('{tns}foo'), 3180 copy.deepcopy(root[0])[0].get('{tns}foo') ) 3181 self.assertEquals( 3182 root[0][0].get('{tns}foo'), 3183 copy.deepcopy(root[0][0]).get('{tns}foo') )31843186 # previously caused a crash 3187 Element = self.etree.Element 3188 tostring = self.etree.tostring 3189 3190 a = Element('a') 3191 b = copy.deepcopy(a) 3192 a.append( Element('C') ) 3193 b.append( Element('X') ) 3194 3195 self.assertEquals(_bytes('<a><C/></a>'), 3196 tostring(a).replace(_bytes(' '), _bytes(''))) 3197 self.assertEquals(_bytes('<a><X/></a>'), 3198 tostring(b).replace(_bytes(' '), _bytes('')))31993201 # previously caused a crash 3202 # not supported by ET < 1.3! 3203 Comment = self.etree.Comment 3204 3205 a = Comment("ONE") 3206 b = copy.deepcopy(a) 3207 b.text = "ANOTHER" 3208 3209 self.assertEquals('ONE', a.text) 3210 self.assertEquals('ANOTHER', b.text)32113213 Element = self.etree.Element 3214 3215 a = Element('a') 3216 a.text = 'Foo' 3217 3218 b = copy.copy(a) 3219 self.assertEquals('Foo', b.text) 3220 3221 b.text = 'Bar' 3222 self.assertEquals('Bar', b.text) 3223 self.assertEquals('Foo', a.text)3224 # XXX ElementTree will share nodes, but lxml.etree won't.. 32253227 Element = self.etree.Element 3228 ElementTree = self.etree.ElementTree 3229 3230 a = Element('a') 3231 a.text = 'Foo' 3232 atree = ElementTree(a) 3233 3234 btree = copy.copy(atree) 3235 self.assertFalse(btree is atree) 3236 self.assert_(btree.getroot() is atree.getroot()) 3237 self.assertEquals('Foo', atree.getroot().text)32383240 # deprecated as of ET 1.3/lxml 2.0 3241 etree = self.etree 3242 e = etree.Element('foo') 3243 self.assertEquals(False, bool(e)) 3244 etree.SubElement(e, 'bar') 3245 self.assertEquals(True, bool(e)) 3246 e = etree.Element('foo') 3247 e.text = 'hey' 3248 self.assertEquals(False, bool(e)) 3249 e = etree.Element('foo') 3250 e.tail = 'bar' 3251 self.assertEquals(False, bool(e)) 3252 e = etree.Element('foo') 3253 e.set('bar', 'Bar') 3254 self.assertEquals(False, bool(e))32553257 etree = self.etree 3258 3259 a = etree.Element('a') 3260 b = etree.SubElement(a, 'b') 3261 3262 t = etree.ElementTree(a) 3263 self.assertEquals(self._rootstring(t), _bytes('<a><b/></a>')) 3264 3265 t1 = etree.ElementTree(a) 3266 self.assertEquals(self._rootstring(t1), _bytes('<a><b/></a>')) 3267 self.assertEquals(self._rootstring(t), _bytes('<a><b/></a>')) 3268 3269 t2 = etree.ElementTree(b) 3270 self.assertEquals(self._rootstring(t2), _bytes('<b/>')) 3271 self.assertEquals(self._rootstring(t1), _bytes('<a><b/></a>')) 3272 self.assertEquals(self._rootstring(t), _bytes('<a><b/></a>'))32733275 etree = self.etree 3276 qname = etree.QName('myns', 'a') 3277 a1 = etree.Element(qname) 3278 a2 = etree.SubElement(a1, qname) 3279 self.assertEquals(a1.tag, "{myns}a") 3280 self.assertEquals(a2.tag, "{myns}a")32813283 etree = self.etree 3284 qname1 = etree.QName('myns', 'a') 3285 qname2 = etree.QName('myns', 'a') 3286 self.assertEquals(qname1, "{myns}a") 3287 self.assertEquals("{myns}a", qname2) 3288 self.assertEquals(qname1, qname1) 3289 self.assertEquals(qname1, qname2)32903292 etree = self.etree 3293 qname = etree.QName('myns', 'a') 3294 3295 a = etree.Element(qname) 3296 a.set(qname, "value") 3297 3298 self.assertEquals(a.get(qname), "value") 3299 self.assertEquals(a.get("{myns}a"), "value")33003302 etree = self.etree 3303 qname = etree.QName('myns', 'a') 3304 3305 a = etree.Element(qname) 3306 a.attrib[qname] = "value" 3307 3308 self.assertEquals(a.attrib[qname], "value") 3309 self.assertEquals(a.attrib.get(qname), "value") 3310 3311 self.assertEquals(a.attrib["{myns}a"], "value") 3312 self.assertEquals(a.attrib.get("{myns}a"), "value")33133315 etree = self.etree 3316 qname = etree.QName('http://myns', 'a') 3317 a = etree.Element(qname) 3318 a.set(qname, qname) 3319 3320 self.assertXML( 3321 _bytes('<ns0:a xmlns:ns0="http://myns" ns0:a="ns0:a"></ns0:a>'), 3322 a)33233325 etree = self.etree 3326 qname = etree.QName('http://myns', 'a') 3327 a = etree.Element('a') 3328 a.set('a', qname) 3329 3330 self.assertXML( 3331 _bytes('<a xmlns:ns0="http://myns" a="ns0:a"></a>'), 3332 a)33333335 etree = self.etree 3336 qname = etree.QName('http://myns', 'a') 3337 a = etree.Element(qname) 3338 a.attrib[qname] = qname 3339 3340 self.assertXML( 3341 _bytes('<ns0:a xmlns:ns0="http://myns" ns0:a="ns0:a"></ns0:a>'), 3342 a)33433345 etree = self.etree 3346 parser = etree.XMLParser() 3347 if hasattr(parser, "version"): 3348 # ElementTree 1.3+, cET 3349 self.assert_(re.match("[^ ]+ [0-9.]+", parser.version))3350 3351 # feed parser interface 33523354 parser = self.etree.XMLParser() 3355 3356 parser.feed(_bytes('<?xml version=')) 3357 parser.feed(_bytes('"1.0"?><ro')) 3358 parser.feed(_bytes('ot><')) 3359 parser.feed(_bytes('a test="works"/')) 3360 parser.feed(_bytes('></root')) 3361 parser.feed(_bytes('>')) 3362 3363 root = parser.close() 3364 3365 self.assertEquals(root.tag, "root") 3366 self.assertEquals(root[0].tag, "a") 3367 self.assertEquals(root[0].get("test"), "works")33683370 parser = self.etree.XMLParser() 3371 3372 parser.feed(_str('<ro')) 3373 parser.feed(_str('ot><')) 3374 parser.feed(_str('a test="works"/')) 3375 parser.feed(_str('></root')) 3376 parser.feed(_str('>')) 3377 3378 root = parser.close() 3379 3380 self.assertEquals(root.tag, "root") 3381 self.assertEquals(root[0].tag, "a") 3382 self.assertEquals(root[0].get("test"), "works")3383 3384 required_versions_ET['test_feed_parser_error_close_empty'] = (1,3)3386 ParseError = self.etree.ParseError 3387 parser = self.etree.XMLParser() 3388 self.assertRaises(ParseError, parser.close)3389 3390 required_versions_ET['test_feed_parser_error_close_incomplete'] = (1,3)3392 ParseError = self.etree.ParseError 3393 parser = self.etree.XMLParser() 3394 3395 parser.feed('<?xml version=') 3396 parser.feed('"1.0"?><ro') 3397 3398 self.assertRaises(ParseError, parser.close)3399 3400 required_versions_ET['test_feed_parser_error_broken'] = (1,3)3402 ParseError = self.etree.ParseError 3403 parser = self.etree.XMLParser() 3404 3405 parser.feed('<?xml version=') 3406 parser.feed('"1.0"?><ro') 3407 try: 3408 parser.feed('<><><><><><><') 3409 except ParseError: 3410 # can raise, but not required before close() 3411 pass 3412 3413 self.assertRaises(ParseError, parser.close)3414 3415 required_versions_ET['test_feed_parser_error_position'] = (1,3)3417 ParseError = self.etree.ParseError 3418 parser = self.etree.XMLParser() 3419 try: 3420 parser.close() 3421 except ParseError: 3422 e = sys.exc_info()[1] 3423 self.assertNotEquals(None, e.code) 3424 self.assertNotEquals(0, e.code) 3425 self.assert_(isinstance(e.position, tuple)) 3426 self.assert_(e.position >= (0, 0))3427 3428 # parser target interface 3429 3430 required_versions_ET['test_parser_target_property'] = (1,3) 3434 3435 target = Target() 3436 parser = self.etree.XMLParser(target=target) 3437 3438 self.assertEquals(target, parser.target) 34393441 assertEquals = self.assertEquals 3442 assertFalse = self.assertFalse 3443 3444 events = [] 3445 class Target(object): 3446 def start(self, tag, attrib): 3447 events.append("start") 3448 assertFalse(attrib) 3449 assertEquals("TAG", tag)3450 def end(self, tag): 3451 events.append("end") 3452 assertEquals("TAG", tag) 3453 def close(self): 3454 return "DONE" 3455 3456 parser = self.etree.XMLParser(target=Target()) 3457 3458 parser.feed("<TAG/>") 3459 done = parser.close() 3460 3461 self.assertEquals("DONE", done) 3462 self.assertEquals(["start", "end"], events) 34633465 assertEquals = self.assertEquals 3466 assertFalse = self.assertFalse 3467 Element = self.etree.Element 3468 3469 events = [] 3470 class Target(object): 3471 def start(self, tag, attrib): 3472 events.append("start") 3473 assertFalse(attrib) 3474 assertEquals("TAG", tag)3475 def end(self, tag): 3476 events.append("end") 3477 assertEquals("TAG", tag) 3478 def close(self): 3479 return Element("DONE") 3480 3481 parser = self.etree.XMLParser(target=Target()) 3482 tree = self.etree.ElementTree() 3483 tree.parse(BytesIO("<TAG/>"), parser=parser) 3484 3485 self.assertEquals("DONE", tree.getroot().tag) 3486 self.assertEquals(["start", "end"], events) 34873489 assertEquals = self.assertEquals 3490 3491 events = [] 3492 class Target(object): 3493 def start(self, tag, attrib): 3494 events.append("start-" + tag) 3495 for name, value in attrib.items(): 3496 assertEquals(tag + name, value)3497 def end(self, tag): 3498 events.append("end-" + tag) 3499 def close(self): 3500 return "DONE" 3501 3502 parser = self.etree.XMLParser(target=Target()) 3503 3504 parser.feed('<root a="roota" b="rootb"><sub c="subc"/></root>') 3505 done = parser.close() 3506 3507 self.assertEquals("DONE", done) 3508 self.assertEquals(["start-root", "start-sub", "end-sub", "end-root"], 3509 events) 35103512 events = [] 3513 class Target(object): 3514 def start(self, tag, attrib): 3515 events.append("start-" + tag)3516 def end(self, tag): 3517 events.append("end-" + tag) 3518 def data(self, data): 3519 events.append("data-" + data) 3520 def close(self): 3521 return "DONE" 3522 3523 parser = self.etree.XMLParser(target=Target()) 3524 3525 parser.feed('<root>A<sub/>B</root>') 3526 done = parser.close() 3527 3528 self.assertEquals("DONE", done) 3529 self.assertEquals(["start-root", "data-A", "start-sub", 3530 "end-sub", "data-B", "end-root"], 3531 events) 3532 3538 def _flush_data(self): 3539 if self._data: 3540 events.append("data-" + ''.join(self._data)) 3541 del self._data[:] 3542 def start(self, tag, attrib): 3543 self._flush_data() 3544 events.append("start-" + tag) 3545 def end(self, tag): 3546 self._flush_data() 3547 events.append("end-" + tag) 3548 def data(self, data): 3549 self._data.append(data) 3550 def close(self): 3551 self._flush_data() 3552 return "DONE" 3553 3554 parser = self.etree.XMLParser(target=Target()) 3555 3556 dtd = ''' 3557 <!DOCTYPE root [ 3558 <!ELEMENT root (sub*)> 3559 <!ELEMENT sub (#PCDATA)> 3560 <!ENTITY ent "an entity"> 3561 ]> 3562 ''' 3563 parser.feed(dtd+'<root><sub/><sub>this is &ent;</sub><sub/></root>') 3564 done = parser.close() 3565 3566 self.assertEquals("DONE", done) 3567 self.assertEquals(["start-root", "start-sub", "end-sub", "start-sub", 3568 "data-this is an entity", 3569 "end-sub", "start-sub", "end-sub", "end-root"], 3570 events) 3571 3572 required_versions_ET['test_parser_target_entity_unknown'] = (1,3) 3578 def _flush_data(self): 3579 if self._data: 3580 events.append("data-" + ''.join(self._data)) 3581 del self._data[:] 3582 def start(self, tag, attrib): 3583 self._flush_data() 3584 events.append("start-" + tag) 3585 def end(self, tag): 3586 self._flush_data() 3587 events.append("end-" + tag) 3588 def data(self, data): 3589 self._data.append(data) 3590 def close(self): 3591 self._flush_data() 3592 return "DONE" 3593 3594 parser = self.etree.XMLParser(target=Target()) 3595 3596 def feed(): 3597 parser.feed('<root><sub/><sub>some &ent;</sub><sub/></root>') 3598 parser.close() 3599 3600 self.assertRaises(self.etree.ParseError, feed) 36013603 builder = self.etree.TreeBuilder() 3604 el = builder.start("root", {'a':'A', 'b':'B'}) 3605 self.assertEquals("root", el.tag) 3606 self.assertEquals({'a':'A', 'b':'B'}, el.attrib) 3607 builder.data("ROOTTEXT") 3608 el = builder.start("child", {'x':'X', 'y':'Y'}) 3609 self.assertEquals("child", el.tag) 3610 self.assertEquals({'x':'X', 'y':'Y'}, el.attrib) 3611 builder.data("CHILDTEXT") 3612 el = builder.end("child") 3613 self.assertEquals("child", el.tag) 3614 self.assertEquals({'x':'X', 'y':'Y'}, el.attrib) 3615 self.assertEquals("CHILDTEXT", el.text) 3616 self.assertEquals(None, el.tail) 3617 builder.data("CHILDTAIL") 3618 root = builder.end("root") 3619 3620 self.assertEquals("root", root.tag) 3621 self.assertEquals("ROOTTEXT", root.text) 3622 self.assertEquals("CHILDTEXT", root[0].text) 3623 self.assertEquals("CHILDTAIL", root[0].tail)36243626 parser = self.etree.XMLParser(target=self.etree.TreeBuilder()) 3627 parser.feed('<root>ROOTTEXT<child>CHILDTEXT</child>CHILDTAIL</root>') 3628 root = parser.close() 3629 3630 self.assertEquals("root", root.tag) 3631 self.assertEquals("ROOTTEXT", root.text) 3632 self.assertEquals("CHILDTEXT", root[0].text) 3633 self.assertEquals("CHILDTAIL", root[0].tail)3634 3635 # helper methods 36363638 """Write out element for comparison. 3639 """ 3640 data = self.etree.tostring(element, encoding=encoding) 3641 return canonicalize(data)36423644 """Write out element for comparison, using real file. 3645 """ 3646 ElementTree = self.etree.ElementTree 3647 handle, filename = tempfile.mkstemp() 3648 try: 3649 f = open(filename, 'wb') 3650 tree = ElementTree(element=element) 3651 tree.write(f, encoding=encoding) 3652 f.close() 3653 f = open(filename, 'rb') 3654 data = f.read() 3655 f.close() 3656 finally: 3657 os.close(handle) 3658 os.remove(filename) 3659 return canonicalize(data)36603662 """Writes element out and checks whether it is expected. 3663 3664 Does this two ways; once using BytesIO, once using a real file. 3665 """ 3666 if isinstance(expected, unicode): 3667 expected = expected.encode(encoding) 3668 self.assertEquals(expected, self._writeElement(element, encoding)) 3669 self.assertEquals(expected, self._writeElementFile(element, encoding))36703672 "Checks if the result XML byte string specifies the encoding." 3673 enc_re = r"<\?xml[^>]+ encoding=[\"']([^\"']+)[\"']" 3674 if isinstance(result, str): 3675 has_encoding = re.compile(enc_re).match 3676 else: 3677 has_encoding = re.compile(_bytes(enc_re)).match 3678 self.assert_(has_encoding(result)) 3679 result_encoding = has_encoding(result).group(1) 3680 self.assertEquals(result_encoding.upper(), encoding.upper())36813683 return self.etree.tostring(tree.getroot()).replace( 3684 _bytes(' '), _bytes('')).replace(_bytes('\n'), _bytes(''))3685 36883690 self.assert_(hasattr(element, 'tag')) 3691 self.assert_(hasattr(element, 'attrib')) 3692 self.assert_(hasattr(element, 'text')) 3693 self.assert_(hasattr(element, 'tail')) 3694 self._check_string(element.tag) 3695 self._check_mapping(element.attrib) 3696 if element.text != None: 3697 self._check_string(element.text) 3698 if element.tail != None: 3699 self._check_string(element.tail)37003702 len(string) 3703 for char in string: 3704 self.assertEquals(1, len(char)) 3705 new_string = string + "" 3706 new_string = string + " " 3707 string[:0]37083710 len(mapping) 3711 keys = mapping.keys() 3712 values = mapping.values() 3713 items = mapping.items() 3714 for key in keys: 3715 item = mapping[key] 3716 mapping["key"] = "value" 3717 self.assertEquals("value", mapping["key"])3718 3719 3720 if etree: 3723 3724 if ElementTree: 3727 3728 filter_by_version( 3729 ElementTreeTestCase, 3730 ElementTreeTestCase.required_versions_ET, ET_VERSION) 3731 3732 if cElementTree: 3735 3736 filter_by_version( 3737 CElementTreeTestCase, 3738 CElementTreeTestCase.required_versions_cET, CET_VERSION) 37393741 suite = unittest.TestSuite() 3742 if etree: 3743 suite.addTests([unittest.makeSuite(ETreeTestCase)]) 3744 if ElementTree: 3745 suite.addTests([unittest.makeSuite(ElementTreeTestCase)]) 3746 if cElementTree: 3747 suite.addTests([unittest.makeSuite(CElementTreeTestCase)]) 3748 return suite3749 3750 if __name__ == '__main__': 3751 print('to test use test.py %s' % __file__) 3752
Home | Trees | Indices | Help |
|
---|
Generated by Epydoc 3.0.1 on Fri Sep 28 21:21:00 2012 | http://epydoc.sourceforge.net |