| 1 |
# IMPORTANT: the same tests are run from "test_xml_etree_c" in order |
| 2 |
# to ensure consistency between the C implementation and the Python |
| 3 |
# implementation. |
| 4 |
# |
| 5 |
# For this purpose, the module-level "ET" symbol is temporarily |
| 6 |
# monkey-patched when running the "test_xml_etree_c" test suite. |
| 7 |
|
| 8 |
import copy |
| 9 |
import functools |
| 10 |
import html |
| 11 |
import io |
| 12 |
import itertools |
| 13 |
import operator |
| 14 |
import os |
| 15 |
import pickle |
| 16 |
import pyexpat |
| 17 |
import sys |
| 18 |
import textwrap |
| 19 |
import types |
| 20 |
import unittest |
| 21 |
import unittest.mock as mock |
| 22 |
import warnings |
| 23 |
import weakref |
| 24 |
|
| 25 |
from contextlib import nullcontext |
| 26 |
from functools import partial |
| 27 |
from itertools import product, islice |
| 28 |
from test import support |
| 29 |
from test.support import os_helper |
| 30 |
from test.support import warnings_helper |
| 31 |
from test.support import findfile, gc_collect, swap_attr, swap_item |
| 32 |
from test.support.import_helper import import_fresh_module |
| 33 |
from test.support.os_helper import TESTFN |
| 34 |
|
| 35 |
|
| 36 |
# pyET is the pure-Python implementation. |
| 37 |
# |
| 38 |
# ET is pyET in test_xml_etree and is the C accelerated version in |
| 39 |
# test_xml_etree_c. |
| 40 |
pyET = None |
| 41 |
ET = None |
| 42 |
|
| 43 |
SIMPLE_XMLFILE = findfile("simple.xml", subdir="xmltestdata") |
| 44 |
try: |
| 45 |
SIMPLE_XMLFILE.encode("utf-8") |
| 46 |
except UnicodeEncodeError: |
| 47 |
raise unittest.SkipTest("filename is not encodable to utf8") |
| 48 |
SIMPLE_NS_XMLFILE = findfile("simple-ns.xml", subdir="xmltestdata") |
| 49 |
UTF8_BUG_XMLFILE = findfile("expat224_utf8_bug.xml", subdir="xmltestdata") |
| 50 |
|
| 51 |
SAMPLE_XML = """\ |
| 52 |
<body> |
| 53 |
<tag class='a'>text</tag> |
| 54 |
<tag class='b' /> |
| 55 |
<section> |
| 56 |
<tag class='b' id='inner'>subtext</tag> |
| 57 |
</section> |
| 58 |
</body> |
| 59 |
""" |
| 60 |
|
| 61 |
SAMPLE_SECTION = """\ |
| 62 |
<section> |
| 63 |
<tag class='b' id='inner'>subtext</tag> |
| 64 |
<nexttag /> |
| 65 |
<nextsection> |
| 66 |
<tag /> |
| 67 |
</nextsection> |
| 68 |
</section> |
| 69 |
""" |
| 70 |
|
| 71 |
SAMPLE_XML_NS = """ |
| 72 |
<body xmlns="http://effbot.org/ns"> |
| 73 |
<tag>text</tag> |
| 74 |
<tag /> |
| 75 |
<section> |
| 76 |
<tag>subtext</tag> |
| 77 |
</section> |
| 78 |
</body> |
| 79 |
""" |
| 80 |
|
| 81 |
SAMPLE_XML_NS_ELEMS = """ |
| 82 |
<root> |
| 83 |
<h:table xmlns:h="hello"> |
| 84 |
<h:tr> |
| 85 |
<h:td>Apples</h:td> |
| 86 |
<h:td>Bananas</h:td> |
| 87 |
</h:tr> |
| 88 |
</h:table> |
| 89 |
|
| 90 |
<f:table xmlns:f="foo"> |
| 91 |
<f:name>African Coffee Table</f:name> |
| 92 |
<f:width>80</f:width> |
| 93 |
<f:length>120</f:length> |
| 94 |
</f:table> |
| 95 |
</root> |
| 96 |
""" |
| 97 |
|
| 98 |
ENTITY_XML = """\ |
| 99 |
<!DOCTYPE points [ |
| 100 |
<!ENTITY % user-entities SYSTEM 'user-entities.xml'> |
| 101 |
%user-entities; |
| 102 |
]> |
| 103 |
<document>&entity;</document> |
| 104 |
""" |
| 105 |
|
| 106 |
EXTERNAL_ENTITY_XML = """\ |
| 107 |
<!DOCTYPE points [ |
| 108 |
<!ENTITY entity SYSTEM "file:///non-existing-file.xml"> |
| 109 |
]> |
| 110 |
<document>&entity;</document> |
| 111 |
""" |
| 112 |
|
| 113 |
ATTLIST_XML = """\ |
| 114 |
<?xml version="1.0" encoding="UTF-8"?> |
| 115 |
<!DOCTYPE Foo [ |
| 116 |
<!ELEMENT foo (bar*)> |
| 117 |
<!ELEMENT bar (#PCDATA)*> |
| 118 |
<!ATTLIST bar xml:lang CDATA "eng"> |
| 119 |
<!ENTITY qux "quux"> |
| 120 |
]> |
| 121 |
<foo> |
| 122 |
<bar>&qux;</bar> |
| 123 |
</foo> |
| 124 |
""" |
| 125 |
|
| 126 |
def is_python_implementation(): |
| 127 |
assert ET is not None, "ET must be initialized" |
| 128 |
assert pyET is not None, "pyET must be initialized" |
| 129 |
return ET is pyET |
| 130 |
|
| 131 |
|
| 132 |
def equal_wrapper(cls): |
| 133 |
"""Mock cls.__eq__ to check whether it has been called or not. |
| 134 |
|
| 135 |
The behaviour of cls.__eq__ (side-effects included) is left as is. |
| 136 |
""" |
| 137 |
eq = cls.__eq__ |
| 138 |
return mock.patch.object(cls, "__eq__", autospec=True, wraps=eq) |
| 139 |
|
| 140 |
|
| 141 |
def checkwarnings(*filters, quiet=False): |
| 142 |
def decorator(test): |
| 143 |
def newtest(*args, **kwargs): |
| 144 |
with warnings_helper.check_warnings(*filters, quiet=quiet): |
| 145 |
test(*args, **kwargs) |
| 146 |
functools.update_wrapper(newtest, test) |
| 147 |
return newtest |
| 148 |
return decorator |
| 149 |
|
| 150 |
def convlinesep(data): |
| 151 |
return data.replace(b'\n', os.linesep.encode()) |
| 152 |
|
| 153 |
|
| 154 |
class ModuleTest(unittest.TestCase): |
| 155 |
def test_sanity(self): |
| 156 |
# Import sanity. |
| 157 |
|
| 158 |
from xml.etree import ElementTree # noqa: F401 |
| 159 |
from xml.etree import ElementInclude # noqa: F401 |
| 160 |
from xml.etree import ElementPath # noqa: F401 |
| 161 |
|
| 162 |
def test_all(self): |
| 163 |
names = ("xml.etree.ElementTree", "_elementtree") |
| 164 |
support.check__all__(self, ET, names, not_exported=("HTML_EMPTY",)) |
| 165 |
|
| 166 |
|
| 167 |
def serialize(elem, to_string=True, encoding='unicode', **options): |
| 168 |
if encoding != 'unicode': |
| 169 |
file = io.BytesIO() |
| 170 |
else: |
| 171 |
file = io.StringIO() |
| 172 |
tree = ET.ElementTree(elem) |
| 173 |
tree.write(file, encoding=encoding, **options) |
| 174 |
if to_string: |
| 175 |
return file.getvalue() |
| 176 |
else: |
| 177 |
file.seek(0) |
| 178 |
return file |
| 179 |
|
| 180 |
def summarize_list(seq): |
| 181 |
return [elem.tag for elem in seq] |
| 182 |
|
| 183 |
|
| 184 |
class ElementTestCase: |
| 185 |
@classmethod |
| 186 |
def setUpClass(cls): |
| 187 |
cls.modules = {pyET, ET} |
| 188 |
|
| 189 |
def pickleRoundTrip(self, obj, name, dumper, loader, proto): |
| 190 |
try: |
| 191 |
with swap_item(sys.modules, name, dumper): |
| 192 |
temp = pickle.dumps(obj, proto) |
| 193 |
with swap_item(sys.modules, name, loader): |
| 194 |
result = pickle.loads(temp) |
| 195 |
except pickle.PicklingError as pe: |
| 196 |
# pyET must be second, because pyET may be (equal to) ET. |
| 197 |
human = dict([(ET, "cET"), (pyET, "pyET")]) |
| 198 |
raise support.TestFailed("Failed to round-trip %r from %r to %r" |
| 199 |
% (obj, |
| 200 |
human.get(dumper, dumper), |
| 201 |
human.get(loader, loader))) from pe |
| 202 |
return result |
| 203 |
|
| 204 |
def assertEqualElements(self, alice, bob): |
| 205 |
self.assertIsInstance(alice, (ET.Element, pyET.Element)) |
| 206 |
self.assertIsInstance(bob, (ET.Element, pyET.Element)) |
| 207 |
self.assertEqual(len(list(alice)), len(list(bob))) |
| 208 |
for x, y in zip(alice, bob): |
| 209 |
self.assertEqualElements(x, y) |
| 210 |
properties = operator.attrgetter('tag', 'tail', 'text', 'attrib') |
| 211 |
self.assertEqual(properties(alice), properties(bob)) |
| 212 |
|
| 213 |
# -------------------------------------------------------------------- |
| 214 |
# element tree tests |
| 215 |
|
| 216 |
class ElementTreeTest(unittest.TestCase): |
| 217 |
|
| 218 |
def serialize_check(self, elem, expected): |
| 219 |
self.assertEqual(serialize(elem), expected) |
| 220 |
|
| 221 |
def test_constructor(self): |
| 222 |
# Test constructor behavior. |
| 223 |
|
| 224 |
with self.assertRaises(TypeError): |
| 225 |
tree = ET.ElementTree("") |
| 226 |
with self.assertRaises(TypeError): |
| 227 |
tree = ET.ElementTree(ET.ElementTree()) |
| 228 |
|
| 229 |
def test_setroot(self): |
| 230 |
# Test _setroot behavior. |
| 231 |
|
| 232 |
tree = ET.ElementTree() |
| 233 |
element = ET.Element("tag") |
| 234 |
tree._setroot(element) |
| 235 |
self.assertEqual(tree.getroot().tag, "tag") |
| 236 |
self.assertEqual(tree.getroot(), element) |
| 237 |
|
| 238 |
# Test behavior with an invalid root element |
| 239 |
|
| 240 |
tree = ET.ElementTree() |
| 241 |
with self.assertRaises(TypeError): |
| 242 |
tree._setroot("") |
| 243 |
with self.assertRaises(TypeError): |
| 244 |
tree._setroot(ET.ElementTree()) |
| 245 |
with self.assertRaises(TypeError): |
| 246 |
tree._setroot(None) |
| 247 |
|
| 248 |
def test_interface(self): |
| 249 |
# Test element tree interface. |
| 250 |
|
| 251 |
def check_element(element): |
| 252 |
self.assertTrue(ET.iselement(element), msg="not an element") |
| 253 |
direlem = dir(element) |
| 254 |
for attr in 'tag', 'attrib', 'text', 'tail': |
| 255 |
self.assertHasAttr(element, attr) |
| 256 |
self.assertIn(attr, direlem, |
| 257 |
msg='no %s visible by dir' % attr) |
| 258 |
|
| 259 |
self.assertIsInstance(element.tag, str) |
| 260 |
self.assertIsInstance(element.attrib, dict) |
| 261 |
if element.text is not None: |
| 262 |
self.assertIsInstance(element.text, str) |
| 263 |
if element.tail is not None: |
| 264 |
self.assertIsInstance(element.tail, str) |
| 265 |
for elem in element: |
| 266 |
check_element(elem) |
| 267 |
|
| 268 |
element = ET.Element("tag") |
| 269 |
check_element(element) |
| 270 |
tree = ET.ElementTree(element) |
| 271 |
check_element(tree.getroot()) |
| 272 |
element = ET.Element("t\xe4g", key="value") |
| 273 |
tree = ET.ElementTree(element) |
| 274 |
self.assertRegex(repr(element), r"^<Element 't\xe4g' at 0x.*>$") |
| 275 |
element = ET.Element("tag", key="value") |
| 276 |
|
| 277 |
# Make sure all standard element methods exist. |
| 278 |
|
| 279 |
def check_method(method): |
| 280 |
self.assertHasAttr(method, '__call__', |
| 281 |
msg="%s not callable" % method) |
| 282 |
|
| 283 |
check_method(element.append) |
| 284 |
check_method(element.extend) |
| 285 |
check_method(element.insert) |
| 286 |
check_method(element.remove) |
| 287 |
check_method(element.find) |
| 288 |
check_method(element.iterfind) |
| 289 |
check_method(element.findall) |
| 290 |
check_method(element.findtext) |
| 291 |
check_method(element.clear) |
| 292 |
check_method(element.get) |
| 293 |
check_method(element.set) |
| 294 |
check_method(element.keys) |
| 295 |
check_method(element.items) |
| 296 |
check_method(element.iter) |
| 297 |
check_method(element.itertext) |
| 298 |
|
| 299 |
# These methods return an iterable. See bug 6472. |
| 300 |
|
| 301 |
def check_iter(it): |
| 302 |
check_method(it.__next__) |
| 303 |
|
| 304 |
check_iter(element.iterfind("tag")) |
| 305 |
check_iter(element.iterfind("*")) |
| 306 |
check_iter(tree.iterfind("tag")) |
| 307 |
check_iter(tree.iterfind("*")) |
| 308 |
|
| 309 |
# These aliases are provided: |
| 310 |
|
| 311 |
self.assertEqual(ET.XML, ET.fromstring) |
| 312 |
self.assertEqual(ET.PI, ET.ProcessingInstruction) |
| 313 |
|
| 314 |
def test_set_attribute(self): |
| 315 |
element = ET.Element('tag') |
| 316 |
|
| 317 |
self.assertEqual(element.tag, 'tag') |
| 318 |
element.tag = 'Tag' |
| 319 |
self.assertEqual(element.tag, 'Tag') |
| 320 |
element.tag = 'TAG' |
| 321 |
self.assertEqual(element.tag, 'TAG') |
| 322 |
|
| 323 |
self.assertIsNone(element.text) |
| 324 |
element.text = 'Text' |
| 325 |
self.assertEqual(element.text, 'Text') |
| 326 |
element.text = 'TEXT' |
| 327 |
self.assertEqual(element.text, 'TEXT') |
| 328 |
|
| 329 |
self.assertIsNone(element.tail) |
| 330 |
element.tail = 'Tail' |
| 331 |
self.assertEqual(element.tail, 'Tail') |
| 332 |
element.tail = 'TAIL' |
| 333 |
self.assertEqual(element.tail, 'TAIL') |
| 334 |
|
| 335 |
self.assertEqual(element.attrib, {}) |
| 336 |
element.attrib = {'a': 'b', 'c': 'd'} |
| 337 |
self.assertEqual(element.attrib, {'a': 'b', 'c': 'd'}) |
| 338 |
element.attrib = {'A': 'B', 'C': 'D'} |
| 339 |
self.assertEqual(element.attrib, {'A': 'B', 'C': 'D'}) |
| 340 |
|
| 341 |
def test_simpleops(self): |
| 342 |
# Basic method sanity checks. |
| 343 |
|
| 344 |
elem = ET.XML("<body><tag/></body>") |
| 345 |
self.serialize_check(elem, '<body><tag /></body>') |
| 346 |
e = ET.Element("tag2") |
| 347 |
elem.append(e) |
| 348 |
self.serialize_check(elem, '<body><tag /><tag2 /></body>') |
| 349 |
elem.remove(e) |
| 350 |
self.serialize_check(elem, '<body><tag /></body>') |
| 351 |
elem.insert(0, e) |
| 352 |
self.serialize_check(elem, '<body><tag2 /><tag /></body>') |
| 353 |
elem.remove(e) |
| 354 |
elem.extend([e]) |
| 355 |
self.serialize_check(elem, '<body><tag /><tag2 /></body>') |
| 356 |
elem.remove(e) |
| 357 |
elem.extend(iter([e])) |
| 358 |
self.serialize_check(elem, '<body><tag /><tag2 /></body>') |
| 359 |
elem.remove(e) |
| 360 |
|
| 361 |
element = ET.Element("tag", key="value") |
| 362 |
self.serialize_check(element, '<tag key="value" />') # 1 |
| 363 |
subelement = ET.Element("subtag") |
| 364 |
element.append(subelement) |
| 365 |
self.serialize_check(element, '<tag key="value"><subtag /></tag>') # 2 |
| 366 |
element.insert(0, subelement) |
| 367 |
self.serialize_check(element, |
| 368 |
'<tag key="value"><subtag /><subtag /></tag>') # 3 |
| 369 |
element.remove(subelement) |
| 370 |
self.serialize_check(element, '<tag key="value"><subtag /></tag>') # 4 |
| 371 |
element.remove(subelement) |
| 372 |
self.serialize_check(element, '<tag key="value" />') # 5 |
| 373 |
with self.assertRaisesRegex(ValueError, |
| 374 |
r'Element\.remove\(.+\): element not found'): |
| 375 |
element.remove(subelement) |
| 376 |
self.serialize_check(element, '<tag key="value" />') # 6 |
| 377 |
element[0:0] = [subelement, subelement, subelement] |
| 378 |
self.serialize_check(element[1], '<subtag />') |
| 379 |
self.assertEqual(element[1:9], [element[1], element[2]]) |
| 380 |
self.assertEqual(element[:9:2], [element[0], element[2]]) |
| 381 |
del element[1:2] |
| 382 |
self.serialize_check(element, |
| 383 |
'<tag key="value"><subtag /><subtag /></tag>') |
| 384 |
|
| 385 |
def test_cdata(self): |
| 386 |
# Test CDATA handling (etc). |
| 387 |
|
| 388 |
self.serialize_check(ET.XML("<tag>hello</tag>"), |
| 389 |
'<tag>hello</tag>') |
| 390 |
self.serialize_check(ET.XML("<tag>hello</tag>"), |
| 391 |
'<tag>hello</tag>') |
| 392 |
self.serialize_check(ET.XML("<tag><![CDATA[hello]]></tag>"), |
| 393 |
'<tag>hello</tag>') |
| 394 |
|
| 395 |
def test_file_init(self): |
| 396 |
stringfile = io.BytesIO(SAMPLE_XML.encode("utf-8")) |
| 397 |
tree = ET.ElementTree(file=stringfile) |
| 398 |
self.assertEqual(tree.find("tag").tag, 'tag') |
| 399 |
self.assertEqual(tree.find("section/tag").tag, 'tag') |
| 400 |
|
| 401 |
tree = ET.ElementTree(file=SIMPLE_XMLFILE) |
| 402 |
self.assertEqual(tree.find("element").tag, 'element') |
| 403 |
self.assertEqual(tree.find("element/../empty-element").tag, |
| 404 |
'empty-element') |
| 405 |
|
| 406 |
def test_path_cache(self): |
| 407 |
# Check that the path cache behaves sanely. |
| 408 |
|
| 409 |
from xml.etree import ElementPath |
| 410 |
|
| 411 |
elem = ET.XML(SAMPLE_XML) |
| 412 |
ElementPath._cache.clear() |
| 413 |
for i in range(10): ET.ElementTree(elem).find('./'+str(i)) |
| 414 |
cache_len_10 = len(ElementPath._cache) |
| 415 |
for i in range(10): ET.ElementTree(elem).find('./'+str(i)) |
| 416 |
self.assertEqual(len(ElementPath._cache), cache_len_10) |
| 417 |
for i in range(20): ET.ElementTree(elem).find('./'+str(i)) |
| 418 |
self.assertGreater(len(ElementPath._cache), cache_len_10) |
| 419 |
for i in range(600): ET.ElementTree(elem).find('./'+str(i)) |
| 420 |
self.assertLess(len(ElementPath._cache), 500) |
| 421 |
|
| 422 |
def test_copy(self): |
| 423 |
# Test copy handling (etc). |
| 424 |
|
| 425 |
import copy |
| 426 |
e1 = ET.XML("<tag>hello<foo/></tag>") |
| 427 |
e2 = copy.copy(e1) |
| 428 |
e3 = copy.deepcopy(e1) |
| 429 |
e1.find("foo").tag = "bar" |
| 430 |
self.serialize_check(e1, '<tag>hello<bar /></tag>') |
| 431 |
self.serialize_check(e2, '<tag>hello<bar /></tag>') |
| 432 |
self.serialize_check(e3, '<tag>hello<foo /></tag>') |
| 433 |
|
| 434 |
def test_attrib(self): |
| 435 |
# Test attribute handling. |
| 436 |
|
| 437 |
elem = ET.Element("tag") |
| 438 |
elem.get("key") # 1.1 |
| 439 |
self.assertEqual(elem.get("key", "default"), 'default') # 1.2 |
| 440 |
|
| 441 |
elem.set("key", "value") |
| 442 |
self.assertEqual(elem.get("key"), 'value') # 1.3 |
| 443 |
|
| 444 |
elem = ET.Element("tag", key="value") |
| 445 |
self.assertEqual(elem.get("key"), 'value') # 2.1 |
| 446 |
self.assertEqual(elem.attrib, {'key': 'value'}) # 2.2 |
| 447 |
|
| 448 |
attrib = {"key": "value"} |
| 449 |
elem = ET.Element("tag", attrib) |
| 450 |
attrib.clear() # check for aliasing issues |
| 451 |
self.assertEqual(elem.get("key"), 'value') # 3.1 |
| 452 |
self.assertEqual(elem.attrib, {'key': 'value'}) # 3.2 |
| 453 |
|
| 454 |
attrib = {"key": "value"} |
| 455 |
elem = ET.Element("tag", **attrib) |
| 456 |
attrib.clear() # check for aliasing issues |
| 457 |
self.assertEqual(elem.get("key"), 'value') # 4.1 |
| 458 |
self.assertEqual(elem.attrib, {'key': 'value'}) # 4.2 |
| 459 |
|
| 460 |
elem = ET.Element("tag", {"key": "other"}, key="value") |
| 461 |
self.assertEqual(elem.get("key"), 'value') # 5.1 |
| 462 |
self.assertEqual(elem.attrib, {'key': 'value'}) # 5.2 |
| 463 |
|
| 464 |
elem = ET.Element('test') |
| 465 |
elem.text = "aa" |
| 466 |
elem.set('testa', 'testval') |
| 467 |
elem.set('testb', 'test2') |
| 468 |
self.assertEqual(ET.tostring(elem), |
| 469 |
b'<test testa="testval" testb="test2">aa</test>') |
| 470 |
self.assertEqual(sorted(elem.keys()), ['testa', 'testb']) |
| 471 |
self.assertEqual(sorted(elem.items()), |
| 472 |
[('testa', 'testval'), ('testb', 'test2')]) |
| 473 |
self.assertEqual(elem.attrib['testb'], 'test2') |
| 474 |
elem.attrib['testb'] = 'test1' |
| 475 |
elem.attrib['testc'] = 'test2' |
| 476 |
self.assertEqual(ET.tostring(elem), |
| 477 |
b'<test testa="testval" testb="test1" testc="test2">aa</test>') |
| 478 |
|
| 479 |
# Test preserving white space chars in attributes |
| 480 |
elem = ET.Element('test') |
| 481 |
elem.set('a', '\r') |
| 482 |
elem.set('b', '\r\n') |
| 483 |
elem.set('c', '\t\n\r ') |
| 484 |
elem.set('d', '\n\n\r\r\t\t ') |
| 485 |
self.assertEqual(ET.tostring(elem), |
| 486 |
b'<test a=" " b=" " c="	 " d=" 		 " />') |
| 487 |
|
| 488 |
def test_makeelement(self): |
| 489 |
# Test makeelement handling. |
| 490 |
|
| 491 |
elem = ET.Element("tag") |
| 492 |
attrib = {"key": "value"} |
| 493 |
subelem = elem.makeelement("subtag", attrib) |
| 494 |
self.assertIsNot(subelem.attrib, attrib, msg="attrib aliasing") |
| 495 |
elem.append(subelem) |
| 496 |
self.serialize_check(elem, '<tag><subtag key="value" /></tag>') |
| 497 |
|
| 498 |
elem.clear() |
| 499 |
self.serialize_check(elem, '<tag />') |
| 500 |
elem.append(subelem) |
| 501 |
self.serialize_check(elem, '<tag><subtag key="value" /></tag>') |
| 502 |
elem.extend([subelem, subelem]) |
| 503 |
self.serialize_check(elem, |
| 504 |
'<tag><subtag key="value" /><subtag key="value" /><subtag key="value" /></tag>') |
| 505 |
elem[:] = [subelem] |
| 506 |
self.serialize_check(elem, '<tag><subtag key="value" /></tag>') |
| 507 |
elem[:] = tuple([subelem]) |
| 508 |
self.serialize_check(elem, '<tag><subtag key="value" /></tag>') |
| 509 |
|
| 510 |
def test_parsefile(self): |
| 511 |
# Test parsing from file. |
| 512 |
|
| 513 |
tree = ET.parse(SIMPLE_XMLFILE) |
| 514 |
stream = io.StringIO() |
| 515 |
tree.write(stream, encoding='unicode') |
| 516 |
self.assertEqual(stream.getvalue(), |
| 517 |
'<root>\n' |
| 518 |
' <element key="value">text</element>\n' |
| 519 |
' <element>text</element>tail\n' |
| 520 |
' <empty-element />\n' |
| 521 |
'</root>') |
| 522 |
tree = ET.parse(SIMPLE_NS_XMLFILE) |
| 523 |
stream = io.StringIO() |
| 524 |
tree.write(stream, encoding='unicode') |
| 525 |
self.assertEqual(stream.getvalue(), |
| 526 |
'<ns0:root xmlns:ns0="namespace">\n' |
| 527 |
' <ns0:element key="value">text</ns0:element>\n' |
| 528 |
' <ns0:element>text</ns0:element>tail\n' |
| 529 |
' <ns0:empty-element />\n' |
| 530 |
'</ns0:root>') |
| 531 |
|
| 532 |
with open(SIMPLE_XMLFILE) as f: |
| 533 |
data = f.read() |
| 534 |
|
| 535 |
parser = ET.XMLParser() |
| 536 |
self.assertRegex(parser.version, r'^Expat ') |
| 537 |
parser.feed(data) |
| 538 |
self.serialize_check(parser.close(), |
| 539 |
'<root>\n' |
| 540 |
' <element key="value">text</element>\n' |
| 541 |
' <element>text</element>tail\n' |
| 542 |
' <empty-element />\n' |
| 543 |
'</root>') |
| 544 |
|
| 545 |
target = ET.TreeBuilder() |
| 546 |
parser = ET.XMLParser(target=target) |
| 547 |
parser.feed(data) |
| 548 |
self.serialize_check(parser.close(), |
| 549 |
'<root>\n' |
| 550 |
' <element key="value">text</element>\n' |
| 551 |
' <element>text</element>tail\n' |
| 552 |
' <empty-element />\n' |
| 553 |
'</root>') |
| 554 |
|
| 555 |
def test_parseliteral(self): |
| 556 |
element = ET.XML("<html><body>text</body></html>") |
| 557 |
self.assertEqual(ET.tostring(element, encoding='unicode'), |
| 558 |
'<html><body>text</body></html>') |
| 559 |
element = ET.fromstring("<html><body>text</body></html>") |
| 560 |
self.assertEqual(ET.tostring(element, encoding='unicode'), |
| 561 |
'<html><body>text</body></html>') |
| 562 |
sequence = ["<html><body>", "text</bo", "dy></html>"] |
| 563 |
element = ET.fromstringlist(sequence) |
| 564 |
self.assertEqual(ET.tostring(element), |
| 565 |
b'<html><body>text</body></html>') |
| 566 |
self.assertEqual(b"".join(ET.tostringlist(element)), |
| 567 |
b'<html><body>text</body></html>') |
| 568 |
self.assertEqual(ET.tostring(element, "ascii"), |
| 569 |
b"<?xml version='1.0' encoding='ascii'?>\n" |
| 570 |
b"<html><body>text</body></html>") |
| 571 |
_, ids = ET.XMLID("<html><body>text</body></html>") |
| 572 |
self.assertEqual(len(ids), 0) |
| 573 |
_, ids = ET.XMLID("<html><body id='body'>text</body></html>") |
| 574 |
self.assertEqual(len(ids), 1) |
| 575 |
self.assertEqual(ids["body"].tag, 'body') |
| 576 |
|
| 577 |
def test_writefile(self): |
| 578 |
elem = ET.Element("tag") |
| 579 |
elem.text = "text" |
| 580 |
self.serialize_check(elem, '<tag>text</tag>') |
| 581 |
ET.SubElement(elem, "subtag").text = "subtext" |
| 582 |
self.serialize_check(elem, '<tag>text<subtag>subtext</subtag></tag>') |
| 583 |
|
| 584 |
# Test tag suppression |
| 585 |
elem.tag = None |
| 586 |
self.serialize_check(elem, 'text<subtag>subtext</subtag>') |
| 587 |
elem.insert(0, ET.Comment("comment")) |
| 588 |
self.serialize_check(elem, |
| 589 |
'text<!--comment--><subtag>subtext</subtag>') # assumes 1.3 |
| 590 |
|
| 591 |
elem[0] = ET.PI("key", "value") |
| 592 |
self.serialize_check(elem, 'text<?key value?><subtag>subtext</subtag>') |
| 593 |
|
| 594 |
def test_custom_builder(self): |
| 595 |
# Test parser w. custom builder. |
| 596 |
|
| 597 |
with open(SIMPLE_XMLFILE) as f: |
| 598 |
data = f.read() |
| 599 |
class Builder(list): |
| 600 |
def start(self, tag, attrib): |
| 601 |
self.append(("start", tag)) |
| 602 |
def end(self, tag): |
| 603 |
self.append(("end", tag)) |
| 604 |
def data(self, text): |
| 605 |
pass |
| 606 |
builder = Builder() |
| 607 |
parser = ET.XMLParser(target=builder) |
| 608 |
parser.feed(data) |
| 609 |
self.assertEqual(builder, [ |
| 610 |
('start', 'root'), |
| 611 |
('start', 'element'), |
| 612 |
('end', 'element'), |
| 613 |
('start', 'element'), |
| 614 |
('end', 'element'), |
| 615 |
('start', 'empty-element'), |
| 616 |
('end', 'empty-element'), |
| 617 |
('end', 'root'), |
| 618 |
]) |
| 619 |
|
| 620 |
with open(SIMPLE_NS_XMLFILE) as f: |
| 621 |
data = f.read() |
| 622 |
class Builder(list): |
| 623 |
def start(self, tag, attrib): |
| 624 |
self.append(("start", tag)) |
| 625 |
def end(self, tag): |
| 626 |
self.append(("end", tag)) |
| 627 |
def data(self, text): |
| 628 |
pass |
| 629 |
def pi(self, target, data): |
| 630 |
self.append(("pi", target, data)) |
| 631 |
def comment(self, data): |
| 632 |
self.append(("comment", data)) |
| 633 |
def start_ns(self, prefix, uri): |
| 634 |
self.append(("start-ns", prefix, uri)) |
| 635 |
def end_ns(self, prefix): |
| 636 |
self.append(("end-ns", prefix)) |
| 637 |
builder = Builder() |
| 638 |
parser = ET.XMLParser(target=builder) |
| 639 |
parser.feed(data) |
| 640 |
self.assertEqual(builder, [ |
| 641 |
('pi', 'pi', 'data'), |
| 642 |
('comment', ' comment '), |
| 643 |
('start-ns', '', 'namespace'), |
| 644 |
('start', '{namespace}root'), |
| 645 |
('start', '{namespace}element'), |
| 646 |
('end', '{namespace}element'), |
| 647 |
('start', '{namespace}element'), |
| 648 |
('end', '{namespace}element'), |
| 649 |
('start', '{namespace}empty-element'), |
| 650 |
('end', '{namespace}empty-element'), |
| 651 |
('end', '{namespace}root'), |
| 652 |
('end-ns', ''), |
| 653 |
]) |
| 654 |
|
| 655 |
def test_custom_builder_only_end_ns(self): |
| 656 |
class Builder(list): |
| 657 |
def end_ns(self, prefix): |
| 658 |
self.append(("end-ns", prefix)) |
| 659 |
|
| 660 |
builder = Builder() |
| 661 |
parser = ET.XMLParser(target=builder) |
| 662 |
parser.feed(textwrap.dedent("""\ |
| 663 |
<?pi data?> |
| 664 |
<!-- comment --> |
| 665 |
<root xmlns='namespace' xmlns:p='pns' xmlns:a='ans'> |
| 666 |
<a:element key='value'>text</a:element> |
| 667 |
<p:element>text</p:element>tail |
| 668 |
<empty-element/> |
| 669 |
</root> |
| 670 |
""")) |
| 671 |
self.assertEqual(builder, [ |
| 672 |
('end-ns', 'a'), |
| 673 |
('end-ns', 'p'), |
| 674 |
('end-ns', ''), |
| 675 |
]) |
| 676 |
|
| 677 |
def test_initialize_parser_without_target(self): |
| 678 |
# Explicit None |
| 679 |
parser = ET.XMLParser(target=None) |
| 680 |
self.assertIsInstance(parser.target, ET.TreeBuilder) |
| 681 |
|
| 682 |
# Implicit None |
| 683 |
parser2 = ET.XMLParser() |
| 684 |
self.assertIsInstance(parser2.target, ET.TreeBuilder) |
| 685 |
|
| 686 |
def test_children(self): |
| 687 |
# Test Element children iteration |
| 688 |
|
| 689 |
with open(SIMPLE_XMLFILE, "rb") as f: |
| 690 |
tree = ET.parse(f) |
| 691 |
self.assertEqual([summarize_list(elem) |
| 692 |
for elem in tree.getroot().iter()], [ |
| 693 |
['element', 'element', 'empty-element'], |
| 694 |
[], |
| 695 |
[], |
| 696 |
[], |
| 697 |
]) |
| 698 |
self.assertEqual([summarize_list(elem) |
| 699 |
for elem in tree.iter()], [ |
| 700 |
['element', 'element', 'empty-element'], |
| 701 |
[], |
| 702 |
[], |
| 703 |
[], |
| 704 |
]) |
| 705 |
|
| 706 |
elem = ET.XML(SAMPLE_XML) |
| 707 |
self.assertEqual(len(list(elem)), 3) |
| 708 |
self.assertEqual(len(list(elem[2])), 1) |
| 709 |
self.assertEqual(elem[:], list(elem)) |
| 710 |
child1 = elem[0] |
| 711 |
child2 = elem[2] |
| 712 |
del elem[1:2] |
| 713 |
self.assertEqual(len(list(elem)), 2) |
| 714 |
self.assertEqual(child1, elem[0]) |
| 715 |
self.assertEqual(child2, elem[1]) |
| 716 |
elem[0:2] = [child2, child1] |
| 717 |
self.assertEqual(child2, elem[0]) |
| 718 |
self.assertEqual(child1, elem[1]) |
| 719 |
self.assertNotEqual(child1, elem[0]) |
| 720 |
elem.clear() |
| 721 |
self.assertEqual(list(elem), []) |
| 722 |
|
| 723 |
def test_writestring(self): |
| 724 |
elem = ET.XML("<html><body>text</body></html>") |
| 725 |
self.assertEqual(ET.tostring(elem), b'<html><body>text</body></html>') |
| 726 |
elem = ET.fromstring("<html><body>text</body></html>") |
| 727 |
self.assertEqual(ET.tostring(elem), b'<html><body>text</body></html>') |
| 728 |
|
| 729 |
def test_indent(self): |
| 730 |
elem = ET.XML("<root></root>") |
| 731 |
ET.indent(elem) |
| 732 |
self.assertEqual(ET.tostring(elem), b'<root />') |
| 733 |
|
| 734 |
elem = ET.XML("<html><body>text</body></html>") |
| 735 |
ET.indent(elem) |
| 736 |
self.assertEqual(ET.tostring(elem), b'<html>\n <body>text</body>\n</html>') |
| 737 |
|
| 738 |
elem = ET.XML("<html> <body>text</body> </html>") |
| 739 |
ET.indent(elem) |
| 740 |
self.assertEqual(ET.tostring(elem), b'<html>\n <body>text</body>\n</html>') |
| 741 |
|
| 742 |
elem = ET.XML("<html><body>text</body>tail</html>") |
| 743 |
ET.indent(elem) |
| 744 |
self.assertEqual(ET.tostring(elem), b'<html>\n <body>text</body>tail</html>') |
| 745 |
|
| 746 |
elem = ET.XML("<html><body><p>par</p>\n<p>text</p>\t<p><br/></p></body></html>") |
| 747 |
ET.indent(elem) |
| 748 |
self.assertEqual( |
| 749 |
ET.tostring(elem), |
| 750 |
b'<html>\n' |
| 751 |
b' <body>\n' |
| 752 |
b' <p>par</p>\n' |
| 753 |
b' <p>text</p>\n' |
| 754 |
b' <p>\n' |
| 755 |
b' <br />\n' |
| 756 |
b' </p>\n' |
| 757 |
b' </body>\n' |
| 758 |
b'</html>' |
| 759 |
) |
| 760 |
|
| 761 |
elem = ET.XML("<html><body><p>pre<br/>post</p><p>text</p></body></html>") |
| 762 |
ET.indent(elem) |
| 763 |
self.assertEqual( |
| 764 |
ET.tostring(elem), |
| 765 |
b'<html>\n' |
| 766 |
b' <body>\n' |
| 767 |
b' <p>pre<br />post</p>\n' |
| 768 |
b' <p>text</p>\n' |
| 769 |
b' </body>\n' |
| 770 |
b'</html>' |
| 771 |
) |
| 772 |
|
| 773 |
def test_indent_space(self): |
| 774 |
elem = ET.XML("<html><body><p>pre<br/>post</p><p>text</p></body></html>") |
| 775 |
ET.indent(elem, space='\t') |
| 776 |
self.assertEqual( |
| 777 |
ET.tostring(elem), |
| 778 |
b'<html>\n' |
| 779 |
b'\t<body>\n' |
| 780 |
b'\t\t<p>pre<br />post</p>\n' |
| 781 |
b'\t\t<p>text</p>\n' |
| 782 |
b'\t</body>\n' |
| 783 |
b'</html>' |
| 784 |
) |
| 785 |
|
| 786 |
elem = ET.XML("<html><body><p>pre<br/>post</p><p>text</p></body></html>") |
| 787 |
ET.indent(elem, space='') |
| 788 |
self.assertEqual( |
| 789 |
ET.tostring(elem), |
| 790 |
b'<html>\n' |
| 791 |
b'<body>\n' |
| 792 |
b'<p>pre<br />post</p>\n' |
| 793 |
b'<p>text</p>\n' |
| 794 |
b'</body>\n' |
| 795 |
b'</html>' |
| 796 |
) |
| 797 |
|
| 798 |
def test_indent_space_caching(self): |
| 799 |
elem = ET.XML("<html><body><p>par</p><p>text</p><p><br/></p><p /></body></html>") |
| 800 |
ET.indent(elem) |
| 801 |
self.assertEqual( |
| 802 |
{el.tail for el in elem.iter()}, |
| 803 |
{None, "\n", "\n ", "\n "} |
| 804 |
) |
| 805 |
self.assertEqual( |
| 806 |
{el.text for el in elem.iter()}, |
| 807 |
{None, "\n ", "\n ", "\n ", "par", "text"} |
| 808 |
) |
| 809 |
self.assertEqual( |
| 810 |
len({el.tail for el in elem.iter()}), |
| 811 |
len({id(el.tail) for el in elem.iter()}), |
| 812 |
) |
| 813 |
|
| 814 |
def test_indent_level(self): |
| 815 |
elem = ET.XML("<html><body><p>pre<br/>post</p><p>text</p></body></html>") |
| 816 |
with self.assertRaises(ValueError): |
| 817 |
ET.indent(elem, level=-1) |
| 818 |
self.assertEqual( |
| 819 |
ET.tostring(elem), |
| 820 |
b"<html><body><p>pre<br />post</p><p>text</p></body></html>" |
| 821 |
) |
| 822 |
|
| 823 |
ET.indent(elem, level=2) |
| 824 |
self.assertEqual( |
| 825 |
ET.tostring(elem), |
| 826 |
b'<html>\n' |
| 827 |
b' <body>\n' |
| 828 |
b' <p>pre<br />post</p>\n' |
| 829 |
b' <p>text</p>\n' |
| 830 |
b' </body>\n' |
| 831 |
b' </html>' |
| 832 |
) |
| 833 |
|
| 834 |
elem = ET.XML("<html><body><p>pre<br/>post</p><p>text</p></body></html>") |
| 835 |
ET.indent(elem, level=1, space=' ') |
| 836 |
self.assertEqual( |
| 837 |
ET.tostring(elem), |
| 838 |
b'<html>\n' |
| 839 |
b' <body>\n' |
| 840 |
b' <p>pre<br />post</p>\n' |
| 841 |
b' <p>text</p>\n' |
| 842 |
b' </body>\n' |
| 843 |
b' </html>' |
| 844 |
) |
| 845 |
|
| 846 |
def test_tostring_default_namespace(self): |
| 847 |
elem = ET.XML('<body xmlns="http://effbot.org/ns"><tag/></body>') |
| 848 |
self.assertEqual( |
| 849 |
ET.tostring(elem, encoding='unicode'), |
| 850 |
'<ns0:body xmlns:ns0="http://effbot.org/ns"><ns0:tag /></ns0:body>' |
| 851 |
) |
| 852 |
self.assertEqual( |
| 853 |
ET.tostring(elem, encoding='unicode', default_namespace='http://effbot.org/ns'), |
| 854 |
'<body xmlns="http://effbot.org/ns"><tag /></body>' |
| 855 |
) |
| 856 |
|
| 857 |
def test_tostring_default_namespace_different_namespace(self): |
| 858 |
elem = ET.XML('<body xmlns="http://effbot.org/ns"><tag/></body>') |
| 859 |
self.assertEqual( |
| 860 |
ET.tostring(elem, encoding='unicode', default_namespace='foobar'), |
| 861 |
'<ns1:body xmlns="foobar" xmlns:ns1="http://effbot.org/ns"><ns1:tag /></ns1:body>' |
| 862 |
) |
| 863 |
|
| 864 |
def test_tostring_default_namespace_original_no_namespace(self): |
| 865 |
elem = ET.XML('<body><tag/></body>') |
| 866 |
EXPECTED_MSG = '^cannot use non-qualified names with default_namespace option$' |
| 867 |
with self.assertRaisesRegex(ValueError, EXPECTED_MSG): |
| 868 |
ET.tostring(elem, encoding='unicode', default_namespace='foobar') |
| 869 |
|
| 870 |
def test_tostring_no_xml_declaration(self): |
| 871 |
elem = ET.XML('<body><tag/></body>') |
| 872 |
self.assertEqual( |
| 873 |
ET.tostring(elem, encoding='unicode'), |
| 874 |
'<body><tag /></body>' |
| 875 |
) |
| 876 |
|
| 877 |
def test_tostring_xml_declaration(self): |
| 878 |
elem = ET.XML('<body><tag/></body>') |
| 879 |
self.assertEqual( |
| 880 |
ET.tostring(elem, encoding='utf8', xml_declaration=True), |
| 881 |
b"<?xml version='1.0' encoding='utf8'?>\n<body><tag /></body>" |
| 882 |
) |
| 883 |
|
| 884 |
def test_tostring_xml_declaration_unicode_encoding(self): |
| 885 |
elem = ET.XML('<body><tag/></body>') |
| 886 |
self.assertEqual( |
| 887 |
ET.tostring(elem, encoding='unicode', xml_declaration=True), |
| 888 |
"<?xml version='1.0' encoding='utf-8'?>\n<body><tag /></body>" |
| 889 |
) |
| 890 |
|
| 891 |
def test_tostring_xml_declaration_cases(self): |
| 892 |
elem = ET.XML('<body><tag>ø</tag></body>') |
| 893 |
TESTCASES = [ |
| 894 |
# (expected_retval, encoding, xml_declaration) |
| 895 |
# ... xml_declaration = None |
| 896 |
(b'<body><tag>ø</tag></body>', None, None), |
| 897 |
(b'<body><tag>\xc3\xb8</tag></body>', 'UTF-8', None), |
| 898 |
(b'<body><tag>ø</tag></body>', 'US-ASCII', None), |
| 899 |
(b"<?xml version='1.0' encoding='ISO-8859-1'?>\n" |
| 900 |
b"<body><tag>\xf8</tag></body>", 'ISO-8859-1', None), |
| 901 |
('<body><tag>ø</tag></body>', 'unicode', None), |
| 902 |
|
| 903 |
# ... xml_declaration = False |
| 904 |
(b"<body><tag>ø</tag></body>", None, False), |
| 905 |
(b"<body><tag>\xc3\xb8</tag></body>", 'UTF-8', False), |
| 906 |
(b"<body><tag>ø</tag></body>", 'US-ASCII', False), |
| 907 |
(b"<body><tag>\xf8</tag></body>", 'ISO-8859-1', False), |
| 908 |
("<body><tag>ø</tag></body>", 'unicode', False), |
| 909 |
|
| 910 |
# ... xml_declaration = True |
| 911 |
(b"<?xml version='1.0' encoding='us-ascii'?>\n" |
| 912 |
b"<body><tag>ø</tag></body>", None, True), |
| 913 |
(b"<?xml version='1.0' encoding='UTF-8'?>\n" |
| 914 |
b"<body><tag>\xc3\xb8</tag></body>", 'UTF-8', True), |
| 915 |
(b"<?xml version='1.0' encoding='US-ASCII'?>\n" |
| 916 |
b"<body><tag>ø</tag></body>", 'US-ASCII', True), |
| 917 |
(b"<?xml version='1.0' encoding='ISO-8859-1'?>\n" |
| 918 |
b"<body><tag>\xf8</tag></body>", 'ISO-8859-1', True), |
| 919 |
("<?xml version='1.0' encoding='utf-8'?>\n" |
| 920 |
"<body><tag>ø</tag></body>", 'unicode', True), |
| 921 |
|
| 922 |
] |
| 923 |
for expected_retval, encoding, xml_declaration in TESTCASES: |
| 924 |
with self.subTest(f'encoding={encoding} ' |
| 925 |
f'xml_declaration={xml_declaration}'): |
| 926 |
self.assertEqual( |
| 927 |
ET.tostring( |
| 928 |
elem, |
| 929 |
encoding=encoding, |
| 930 |
xml_declaration=xml_declaration |
| 931 |
), |
| 932 |
expected_retval |
| 933 |
) |
| 934 |
|
| 935 |
def test_tostringlist_default_namespace(self): |
| 936 |
elem = ET.XML('<body xmlns="http://effbot.org/ns"><tag/></body>') |
| 937 |
self.assertEqual( |
| 938 |
''.join(ET.tostringlist(elem, encoding='unicode')), |
| 939 |
'<ns0:body xmlns:ns0="http://effbot.org/ns"><ns0:tag /></ns0:body>' |
| 940 |
) |
| 941 |
self.assertEqual( |
| 942 |
''.join(ET.tostringlist(elem, encoding='unicode', default_namespace='http://effbot.org/ns')), |
| 943 |
'<body xmlns="http://effbot.org/ns"><tag /></body>' |
| 944 |
) |
| 945 |
|
| 946 |
def test_tostringlist_xml_declaration(self): |
| 947 |
elem = ET.XML('<body><tag/></body>') |
| 948 |
self.assertEqual( |
| 949 |
''.join(ET.tostringlist(elem, encoding='unicode')), |
| 950 |
'<body><tag /></body>' |
| 951 |
) |
| 952 |
self.assertEqual( |
| 953 |
b''.join(ET.tostringlist(elem, xml_declaration=True)), |
| 954 |
b"<?xml version='1.0' encoding='us-ascii'?>\n<body><tag /></body>" |
| 955 |
) |
| 956 |
|
| 957 |
stringlist = ET.tostringlist(elem, encoding='unicode', xml_declaration=True) |
| 958 |
self.assertEqual( |
| 959 |
''.join(stringlist), |
| 960 |
"<?xml version='1.0' encoding='utf-8'?>\n<body><tag /></body>" |
| 961 |
) |
| 962 |
self.assertRegex(stringlist[0], r"^<\?xml version='1.0' encoding='.+'?>") |
| 963 |
self.assertEqual(['<body', '>', '<tag', ' />', '</body>'], stringlist[1:]) |
| 964 |
|
| 965 |
def test_encoding(self): |
| 966 |
def check(encoding, body=''): |
| 967 |
xml = ("<?xml version='1.0' encoding='%s'?><xml>%s</xml>" % |
| 968 |
(encoding, body)) |
| 969 |
self.assertEqual(ET.XML(xml.encode(encoding)).text, body) |
| 970 |
self.assertEqual(ET.XML(xml).text, body) |
| 971 |
check("ascii", 'a') |
| 972 |
check("us-ascii", 'a') |
| 973 |
check("iso-8859-1", '\xbd') |
| 974 |
check("iso-8859-15", '\u20ac') |
| 975 |
check("cp437", '\u221a') |
| 976 |
check("mac-roman", '\u02da') |
| 977 |
|
| 978 |
def xml(encoding, body=''): |
| 979 |
return "<?xml version='1.0' encoding='%s'?><xml>%s</xml>" % (encoding, body) |
| 980 |
def bxml(encoding, body=''): |
| 981 |
return xml(encoding, body).encode(encoding) |
| 982 |
supported_encodings = [ |
| 983 |
'utf-8', 'utf-16', 'utf-16be', 'utf-16le', |
| 984 |
'iso8859-1', 'iso8859-2', 'iso8859-3', 'iso8859-4', 'iso8859-5', |
| 985 |
'iso8859-6', 'iso8859-7', 'iso8859-8', 'iso8859-9', 'iso8859-10', |
| 986 |
'iso8859-13', 'iso8859-14', 'iso8859-15', 'iso8859-16', |
| 987 |
'cp437', 'cp720', 'cp737', 'cp775', 'cp850', 'cp852', |
| 988 |
'cp855', 'cp856', 'cp857', 'cp858', 'cp860', 'cp861', 'cp862', |
| 989 |
'cp863', 'cp865', 'cp866', 'cp869', 'cp874', 'cp1006', 'cp1125', |
| 990 |
'cp1250', 'cp1251', 'cp1252', 'cp1253', 'cp1254', 'cp1255', |
| 991 |
'cp1256', 'cp1257', 'cp1258', |
| 992 |
'mac-cyrillic', 'mac-greek', 'mac-iceland', 'mac-latin2', |
| 993 |
'mac-roman', 'mac-turkish', |
| 994 |
'koi8-r', 'koi8-t', 'koi8-u', 'kz1048', 'ptcp154', |
| 995 |
] |
| 996 |
for encoding in supported_encodings: |
| 997 |
with self.subTest(encoding=encoding): |
| 998 |
self.assertEqual(ET.tostring(ET.XML(bxml(encoding))), b'<xml />') |
| 999 |
c = 'éπя\u05d0\u060c€'.encode(encoding, 'ignore').decode(encoding)[0] |
| 1000 |
self.assertEqual(ET.tostring(ET.XML(bxml(encoding, c))), |
| 1001 |
('<xml>&#%d;</xml>' % ord(c)).encode()) |
| 1002 |
|
| 1003 |
unsupported_ascii_compatible_encodings = [ |
| 1004 |
'big5', 'big5hkscs', |
| 1005 |
'cp932', 'cp949', 'cp950', |
| 1006 |
'euc-jp', 'euc-jis-2004', 'euc-jisx0213', 'euc-kr', |
| 1007 |
'gb2312', 'gbk', 'gb18030', |
| 1008 |
'iso2022-kr', 'johab', |
| 1009 |
'shift-jis', 'shift-jis-2004', 'shift-jisx0213', |
| 1010 |
'utf-7', |
| 1011 |
] |
| 1012 |
for encoding in unsupported_ascii_compatible_encodings: |
| 1013 |
with self.subTest(encoding=encoding): |
| 1014 |
self.assertRaises(ValueError, ET.XML, bxml(encoding)) |
| 1015 |
|
| 1016 |
unsupported_ascii_incompatible_encodings = [ |
| 1017 |
'cp037', 'cp424', 'cp500', 'cp864', 'cp875', 'cp1026', 'cp1140', |
| 1018 |
'utf_32', 'utf_32_be', 'utf_32_le', |
| 1019 |
] |
| 1020 |
for encoding in unsupported_ascii_incompatible_encodings: |
| 1021 |
with self.subTest(encoding=encoding): |
| 1022 |
self.assertRaises(ET.ParseError, ET.XML, bxml(encoding)) |
| 1023 |
|
| 1024 |
self.assertRaises(ValueError, ET.XML, xml('undefined').encode('ascii')) |
| 1025 |
self.assertRaises(LookupError, ET.XML, xml('xxx').encode('ascii')) |
| 1026 |
|
| 1027 |
def test_methods(self): |
| 1028 |
# Test serialization methods. |
| 1029 |
|
| 1030 |
e = ET.XML("<html><link/><script>1 < 2</script></html>") |
| 1031 |
e.tail = "\n" |
| 1032 |
self.assertEqual(serialize(e), |
| 1033 |
'<html><link /><script>1 < 2</script></html>\n') |
| 1034 |
self.assertEqual(serialize(e, method=None), |
| 1035 |
'<html><link /><script>1 < 2</script></html>\n') |
| 1036 |
self.assertEqual(serialize(e, method="xml"), |
| 1037 |
'<html><link /><script>1 < 2</script></html>\n') |
| 1038 |
self.assertEqual(serialize(e, method="html"), |
| 1039 |
'<html><link><script>1 < 2</script></html>\n') |
| 1040 |
self.assertEqual(serialize(e, method="text"), '1 < 2\n') |
| 1041 |
|
| 1042 |
def test_issue18347(self): |
| 1043 |
e = ET.XML('<html><CamelCase>text</CamelCase></html>') |
| 1044 |
self.assertEqual(serialize(e), |
| 1045 |
'<html><CamelCase>text</CamelCase></html>') |
| 1046 |
self.assertEqual(serialize(e, method="html"), |
| 1047 |
'<html><CamelCase>text</CamelCase></html>') |
| 1048 |
|
| 1049 |
def test_entity(self): |
| 1050 |
# Test entity handling. |
| 1051 |
|
| 1052 |
# 1) good entities |
| 1053 |
|
| 1054 |
e = ET.XML("<document title='舰'>test</document>") |
| 1055 |
self.assertEqual(serialize(e, encoding="us-ascii"), |
| 1056 |
b'<document title="舰">test</document>') |
| 1057 |
self.serialize_check(e, '<document title="\u8230">test</document>') |
| 1058 |
|
| 1059 |
# 2) bad entities |
| 1060 |
|
| 1061 |
with self.assertRaises(ET.ParseError) as cm: |
| 1062 |
ET.XML("<document>&entity;</document>") |
| 1063 |
self.assertEqual(str(cm.exception), |
| 1064 |
'undefined entity: line 1, column 10') |
| 1065 |
|
| 1066 |
with self.assertRaises(ET.ParseError) as cm: |
| 1067 |
ET.XML(ENTITY_XML) |
| 1068 |
self.assertEqual(str(cm.exception), |
| 1069 |
'undefined entity &entity;: line 5, column 10') |
| 1070 |
|
| 1071 |
# 3) custom entity |
| 1072 |
|
| 1073 |
parser = ET.XMLParser() |
| 1074 |
parser.entity["entity"] = "text" |
| 1075 |
parser.feed(ENTITY_XML) |
| 1076 |
root = parser.close() |
| 1077 |
self.serialize_check(root, '<document>text</document>') |
| 1078 |
|
| 1079 |
# 4) external (SYSTEM) entity |
| 1080 |
|
| 1081 |
with self.assertRaises(ET.ParseError) as cm: |
| 1082 |
ET.XML(EXTERNAL_ENTITY_XML) |
| 1083 |
self.assertEqual(str(cm.exception), |
| 1084 |
'undefined entity &entity;: line 4, column 10') |
| 1085 |
|
| 1086 |
def test_namespace(self): |
| 1087 |
# Test namespace issues. |
| 1088 |
|
| 1089 |
# 1) xml namespace |
| 1090 |
|
| 1091 |
elem = ET.XML("<tag xml:lang='en' />") |
| 1092 |
self.serialize_check(elem, '<tag xml:lang="en" />') # 1.1 |
| 1093 |
|
| 1094 |
# 2) other "well-known" namespaces |
| 1095 |
|
| 1096 |
elem = ET.XML("<rdf:RDF xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#' />") |
| 1097 |
self.serialize_check(elem, |
| 1098 |
'<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" />') # 2.1 |
| 1099 |
|
| 1100 |
elem = ET.XML("<html:html xmlns:html='http://www.w3.org/1999/xhtml' />") |
| 1101 |
self.serialize_check(elem, |
| 1102 |
'<html:html xmlns:html="http://www.w3.org/1999/xhtml" />') # 2.2 |
| 1103 |
|
| 1104 |
elem = ET.XML("<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope' />") |
| 1105 |
self.serialize_check(elem, |
| 1106 |
'<ns0:Envelope xmlns:ns0="http://schemas.xmlsoap.org/soap/envelope" />') # 2.3 |
| 1107 |
|
| 1108 |
# 3) unknown namespaces |
| 1109 |
elem = ET.XML(SAMPLE_XML_NS) |
| 1110 |
self.serialize_check(elem, |
| 1111 |
'<ns0:body xmlns:ns0="http://effbot.org/ns">\n' |
| 1112 |
' <ns0:tag>text</ns0:tag>\n' |
| 1113 |
' <ns0:tag />\n' |
| 1114 |
' <ns0:section>\n' |
| 1115 |
' <ns0:tag>subtext</ns0:tag>\n' |
| 1116 |
' </ns0:section>\n' |
| 1117 |
'</ns0:body>') |
| 1118 |
|
| 1119 |
def test_qname(self): |
| 1120 |
# Test QName handling. |
| 1121 |
|
| 1122 |
# 1) decorated tags |
| 1123 |
|
| 1124 |
elem = ET.Element("{uri}tag") |
| 1125 |
self.serialize_check(elem, '<ns0:tag xmlns:ns0="uri" />') # 1.1 |
| 1126 |
elem = ET.Element(ET.QName("{uri}tag")) |
| 1127 |
self.serialize_check(elem, '<ns0:tag xmlns:ns0="uri" />') # 1.2 |
| 1128 |
elem = ET.Element(ET.QName("uri", "tag")) |
| 1129 |
self.serialize_check(elem, '<ns0:tag xmlns:ns0="uri" />') # 1.3 |
| 1130 |
elem = ET.Element(ET.QName("uri", "tag")) |
| 1131 |
subelem = ET.SubElement(elem, ET.QName("uri", "tag1")) |
| 1132 |
subelem = ET.SubElement(elem, ET.QName("uri", "tag2")) |
| 1133 |
self.serialize_check(elem, |
| 1134 |
'<ns0:tag xmlns:ns0="uri"><ns0:tag1 /><ns0:tag2 /></ns0:tag>') # 1.4 |
| 1135 |
|
| 1136 |
# 2) decorated attributes |
| 1137 |
|
| 1138 |
elem.clear() |
| 1139 |
elem.attrib["{uri}key"] = "value" |
| 1140 |
self.serialize_check(elem, |
| 1141 |
'<ns0:tag xmlns:ns0="uri" ns0:key="value" />') # 2.1 |
| 1142 |
|
| 1143 |
elem.clear() |
| 1144 |
elem.attrib[ET.QName("{uri}key")] = "value" |
| 1145 |
self.serialize_check(elem, |
| 1146 |
'<ns0:tag xmlns:ns0="uri" ns0:key="value" />') # 2.2 |
| 1147 |
|
| 1148 |
# 3) decorated values are not converted by default, but the |
| 1149 |
# QName wrapper can be used for values |
| 1150 |
|
| 1151 |
elem.clear() |
| 1152 |
elem.attrib["{uri}key"] = "{uri}value" |
| 1153 |
self.serialize_check(elem, |
| 1154 |
'<ns0:tag xmlns:ns0="uri" ns0:key="{uri}value" />') # 3.1 |
| 1155 |
|
| 1156 |
elem.clear() |
| 1157 |
elem.attrib["{uri}key"] = ET.QName("{uri}value") |
| 1158 |
self.serialize_check(elem, |
| 1159 |
'<ns0:tag xmlns:ns0="uri" ns0:key="ns0:value" />') # 3.2 |
| 1160 |
|
| 1161 |
elem.clear() |
| 1162 |
subelem = ET.Element("tag") |
| 1163 |
subelem.attrib["{uri1}key"] = ET.QName("{uri2}value") |
| 1164 |
elem.append(subelem) |
| 1165 |
elem.append(subelem) |
| 1166 |
self.serialize_check(elem, |
| 1167 |
'<ns0:tag xmlns:ns0="uri" xmlns:ns1="uri1" xmlns:ns2="uri2">' |
| 1168 |
'<tag ns1:key="ns2:value" />' |
| 1169 |
'<tag ns1:key="ns2:value" />' |
| 1170 |
'</ns0:tag>') # 3.3 |
| 1171 |
|
| 1172 |
# 4) Direct QName tests |
| 1173 |
|
| 1174 |
self.assertEqual(str(ET.QName('ns', 'tag')), '{ns}tag') |
| 1175 |
self.assertEqual(str(ET.QName('{ns}tag')), '{ns}tag') |
| 1176 |
q1 = ET.QName('ns', 'tag') |
| 1177 |
q2 = ET.QName('ns', 'tag') |
| 1178 |
self.assertEqual(q1, q2) |
| 1179 |
q2 = ET.QName('ns', 'other-tag') |
| 1180 |
self.assertNotEqual(q1, q2) |
| 1181 |
self.assertNotEqual(q1, 'ns:tag') |
| 1182 |
self.assertEqual(q1, '{ns}tag') |
| 1183 |
|
| 1184 |
def test_doctype_public(self): |
| 1185 |
# Test PUBLIC doctype. |
| 1186 |
|
| 1187 |
elem = ET.XML('<!DOCTYPE html PUBLIC' |
| 1188 |
' "-//W3C//DTD XHTML 1.0 Transitional//EN"' |
| 1189 |
' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">' |
| 1190 |
'<html>text</html>') |
| 1191 |
|
| 1192 |
def test_xpath_tokenizer(self): |
| 1193 |
# Test the XPath tokenizer. |
| 1194 |
from xml.etree import ElementPath |
| 1195 |
def check(p, expected, namespaces=None): |
| 1196 |
self.assertEqual([op or tag |
| 1197 |
for op, tag in ElementPath.xpath_tokenizer(p, namespaces)], |
| 1198 |
expected) |
| 1199 |
|
| 1200 |
# tests from the xml specification |
| 1201 |
check("*", ['*']) |
| 1202 |
check("text()", ['text', '()']) |
| 1203 |
check("@name", ['@', 'name']) |
| 1204 |
check("@*", ['@', '*']) |
| 1205 |
check("para[1]", ['para', '[', '1', ']']) |
| 1206 |
check("para[last()]", ['para', '[', 'last', '()', ']']) |
| 1207 |
check("*/para", ['*', '/', 'para']) |
| 1208 |
check("/doc/chapter[5]/section[2]", |
| 1209 |
['/', 'doc', '/', 'chapter', '[', '5', ']', |
| 1210 |
'/', 'section', '[', '2', ']']) |
| 1211 |
check("chapter//para", ['chapter', '//', 'para']) |
| 1212 |
check("//para", ['//', 'para']) |
| 1213 |
check("//olist/item", ['//', 'olist', '/', 'item']) |
| 1214 |
check(".", ['.']) |
| 1215 |
check(".//para", ['.', '//', 'para']) |
| 1216 |
check("..", ['..']) |
| 1217 |
check("../@lang", ['..', '/', '@', 'lang']) |
| 1218 |
check("chapter[title]", ['chapter', '[', 'title', ']']) |
| 1219 |
check("employee[@secretary and @assistant]", ['employee', |
| 1220 |
'[', '@', 'secretary', '', 'and', '', '@', 'assistant', ']']) |
| 1221 |
|
| 1222 |
# additional tests |
| 1223 |
check("@{ns}attr", ['@', '{ns}attr']) |
| 1224 |
check("{http://spam}egg", ['{http://spam}egg']) |
| 1225 |
check("./spam.egg", ['.', '/', 'spam.egg']) |
| 1226 |
check(".//{http://spam}egg", ['.', '//', '{http://spam}egg']) |
| 1227 |
|
| 1228 |
# wildcard tags |
| 1229 |
check("{ns}*", ['{ns}*']) |
| 1230 |
check("{}*", ['{}*']) |
| 1231 |
check("{*}tag", ['{*}tag']) |
| 1232 |
check("{*}*", ['{*}*']) |
| 1233 |
check(".//{*}tag", ['.', '//', '{*}tag']) |
| 1234 |
|
| 1235 |
# namespace prefix resolution |
| 1236 |
check("./xsd:type", ['.', '/', '{http://www.w3.org/2001/XMLSchema}type'], |
| 1237 |
{'xsd': 'http://www.w3.org/2001/XMLSchema'}) |
| 1238 |
check("type", ['{http://www.w3.org/2001/XMLSchema}type'], |
| 1239 |
{'': 'http://www.w3.org/2001/XMLSchema'}) |
| 1240 |
check("@xsd:type", ['@', '{http://www.w3.org/2001/XMLSchema}type'], |
| 1241 |
{'xsd': 'http://www.w3.org/2001/XMLSchema'}) |
| 1242 |
check("@type", ['@', 'type'], |
| 1243 |
{'': 'http://www.w3.org/2001/XMLSchema'}) |
| 1244 |
check("@{*}type", ['@', '{*}type'], |
| 1245 |
{'': 'http://www.w3.org/2001/XMLSchema'}) |
| 1246 |
check("@{ns}attr", ['@', '{ns}attr'], |
| 1247 |
{'': 'http://www.w3.org/2001/XMLSchema', |
| 1248 |
'ns': 'http://www.w3.org/2001/XMLSchema'}) |
| 1249 |
|
| 1250 |
def test_comment_serialization(self): |
| 1251 |
comm = ET.Comment('<spam> & ham') |
| 1252 |
# comments are not escaped |
| 1253 |
self.assertEqual(ET.tostring(comm), b'<!--<spam> & ham-->') |
| 1254 |
|
| 1255 |
def test_processinginstruction_serialization(self): |
| 1256 |
# Test ProcessingInstruction directly |
| 1257 |
|
| 1258 |
self.assertEqual(ET.tostring(ET.ProcessingInstruction('test', 'instruction')), |
| 1259 |
b'<?test instruction?>') |
| 1260 |
self.assertEqual(ET.tostring(ET.PI('test', 'instruction')), |
| 1261 |
b'<?test instruction?>') |
| 1262 |
|
| 1263 |
# Issue #2746 |
| 1264 |
# processing instructions are not escaped |
| 1265 |
self.assertEqual(ET.tostring(ET.PI('test', '<testing&>')), |
| 1266 |
b'<?test <testing&>?>') |
| 1267 |
self.assertEqual(ET.tostring(ET.PI('test', '<testing&>\xe3'), 'latin-1'), |
| 1268 |
b"<?xml version='1.0' encoding='latin-1'?>\n" |
| 1269 |
b"<?test <testing&>\xe3?>") |
| 1270 |
|
| 1271 |
@support.subTests('tag', ("script", "style", "xmp", "iframe", "noembed", "noframes")) |
| 1272 |
def test_html_cdata_elems_serialization(self, tag): |
| 1273 |
# content of raw text elements is not escaped in html |
| 1274 |
tag = tag.title() |
| 1275 |
elem = ET.Element(tag) |
| 1276 |
elem.text = '<spam>&ham' |
| 1277 |
self.assertEqual(ET.tostring(elem, method='html'), |
| 1278 |
('<%s><spam>&ham</%s>' % (tag, tag)).encode()) |
| 1279 |
|
| 1280 |
def test_html_empty_elems_serialization(self): |
| 1281 |
# issue 15970 |
| 1282 |
# from http://www.w3.org/TR/html401/index/elements.html |
| 1283 |
for element in ['AREA', 'BASE', 'BASEFONT', 'BR', 'COL', 'EMBED', 'FRAME', |
| 1284 |
'HR', 'IMG', 'INPUT', 'ISINDEX', 'LINK', 'META', 'PARAM', |
| 1285 |
'SOURCE', 'TRACK', 'WBR']: |
| 1286 |
for elem in [element, element.lower()]: |
| 1287 |
expected = '<%s>' % elem |
| 1288 |
serialized = serialize(ET.XML('<%s />' % elem), method='html') |
| 1289 |
self.assertEqual(serialized, expected) |
| 1290 |
serialized = serialize(ET.XML('<%s></%s>' % (elem,elem)), |
| 1291 |
method='html') |
| 1292 |
self.assertEqual(serialized, expected) |
| 1293 |
|
| 1294 |
def test_html_plaintext_serialization(self): |
| 1295 |
# content of plaintext is not escaped in html |
| 1296 |
# no end tag for plaintext |
| 1297 |
elem = ET.Element('PlainText') |
| 1298 |
elem.text = '<spam>&ham' |
| 1299 |
self.assertEqual(ET.tostring(elem, method='html'), |
| 1300 |
b'<PlainText><spam>&ham') |
| 1301 |
|
| 1302 |
def test_dump_attribute_order(self): |
| 1303 |
# See BPO 34160 |
| 1304 |
e = ET.Element('cirriculum', status='public', company='example') |
| 1305 |
with support.captured_stdout() as stdout: |
| 1306 |
ET.dump(e) |
| 1307 |
self.assertEqual(stdout.getvalue(), |
| 1308 |
'<cirriculum status="public" company="example" />\n') |
| 1309 |
|
| 1310 |
def test_tree_write_attribute_order(self): |
| 1311 |
# See BPO 34160 |
| 1312 |
root = ET.Element('cirriculum', status='public', company='example') |
| 1313 |
self.assertEqual(serialize(root), |
| 1314 |
'<cirriculum status="public" company="example" />') |
| 1315 |
self.assertEqual(serialize(root, method='html'), |
| 1316 |
'<cirriculum status="public" company="example"></cirriculum>') |
| 1317 |
|
| 1318 |
def test_attlist_default(self): |
| 1319 |
# Test default attribute values; See BPO 42151. |
| 1320 |
root = ET.fromstring(ATTLIST_XML) |
| 1321 |
self.assertEqual(root[0].attrib, |
| 1322 |
{'{http://www.w3.org/XML/1998/namespace}lang': 'eng'}) |
| 1323 |
|
| 1324 |
|
| 1325 |
class IterparseTest(unittest.TestCase): |
| 1326 |
# Test iterparse interface. |
| 1327 |
|
| 1328 |
def test_basic(self): |
| 1329 |
iterparse = ET.iterparse |
| 1330 |
|
| 1331 |
it = iterparse(SIMPLE_XMLFILE) |
| 1332 |
self.assertIsNone(it.root) |
| 1333 |
action, elem = next(it) |
| 1334 |
self.assertIsNone(it.root) |
| 1335 |
self.assertEqual((action, elem.tag), ('end', 'element')) |
| 1336 |
self.assertEqual([(action, elem.tag) for action, elem in it], [ |
| 1337 |
('end', 'element'), |
| 1338 |
('end', 'empty-element'), |
| 1339 |
('end', 'root'), |
| 1340 |
]) |
| 1341 |
self.assertEqual(it.root.tag, 'root') |
| 1342 |
it.close() |
| 1343 |
|
| 1344 |
it = iterparse(SIMPLE_NS_XMLFILE) |
| 1345 |
self.assertEqual([(action, elem.tag) for action, elem in it], [ |
| 1346 |
('end', '{namespace}element'), |
| 1347 |
('end', '{namespace}element'), |
| 1348 |
('end', '{namespace}empty-element'), |
| 1349 |
('end', '{namespace}root'), |
| 1350 |
]) |
| 1351 |
it.close() |
| 1352 |
|
| 1353 |
def test_external_file(self): |
| 1354 |
with open(SIMPLE_XMLFILE, 'rb') as source: |
| 1355 |
it = ET.iterparse(source) |
| 1356 |
action, elem = next(it) |
| 1357 |
self.assertEqual((action, elem.tag), ('end', 'element')) |
| 1358 |
self.assertEqual([(action, elem.tag) for action, elem in it], [ |
| 1359 |
('end', 'element'), |
| 1360 |
('end', 'empty-element'), |
| 1361 |
('end', 'root'), |
| 1362 |
]) |
| 1363 |
self.assertEqual(it.root.tag, 'root') |
| 1364 |
|
| 1365 |
def test_events(self): |
| 1366 |
iterparse = ET.iterparse |
| 1367 |
|
| 1368 |
events = () |
| 1369 |
it = iterparse(SIMPLE_XMLFILE, events) |
| 1370 |
self.assertEqual([(action, elem.tag) for action, elem in it], []) |
| 1371 |
it.close() |
| 1372 |
|
| 1373 |
events = () |
| 1374 |
it = iterparse(SIMPLE_XMLFILE, events=events) |
| 1375 |
self.assertEqual([(action, elem.tag) for action, elem in it], []) |
| 1376 |
it.close() |
| 1377 |
|
| 1378 |
events = ("start", "end") |
| 1379 |
it = iterparse(SIMPLE_XMLFILE, events) |
| 1380 |
self.assertEqual([(action, elem.tag) for action, elem in it], [ |
| 1381 |
('start', 'root'), |
| 1382 |
('start', 'element'), |
| 1383 |
('end', 'element'), |
| 1384 |
('start', 'element'), |
| 1385 |
('end', 'element'), |
| 1386 |
('start', 'empty-element'), |
| 1387 |
('end', 'empty-element'), |
| 1388 |
('end', 'root'), |
| 1389 |
]) |
| 1390 |
it.close() |
| 1391 |
|
| 1392 |
def test_namespace_events(self): |
| 1393 |
iterparse = ET.iterparse |
| 1394 |
|
| 1395 |
events = ("start", "end", "start-ns", "end-ns") |
| 1396 |
it = iterparse(SIMPLE_NS_XMLFILE, events) |
| 1397 |
self.assertEqual([(action, elem.tag) if action in ("start", "end") |
| 1398 |
else (action, elem) |
| 1399 |
for action, elem in it], [ |
| 1400 |
('start-ns', ('', 'namespace')), |
| 1401 |
('start', '{namespace}root'), |
| 1402 |
('start', '{namespace}element'), |
| 1403 |
('end', '{namespace}element'), |
| 1404 |
('start', '{namespace}element'), |
| 1405 |
('end', '{namespace}element'), |
| 1406 |
('start', '{namespace}empty-element'), |
| 1407 |
('end', '{namespace}empty-element'), |
| 1408 |
('end', '{namespace}root'), |
| 1409 |
('end-ns', None), |
| 1410 |
]) |
| 1411 |
it.close() |
| 1412 |
|
| 1413 |
events = ('start-ns', 'end-ns') |
| 1414 |
it = iterparse(io.BytesIO(br"<root xmlns=''/>"), events) |
| 1415 |
res = [action for action, elem in it] |
| 1416 |
self.assertEqual(res, ['start-ns', 'end-ns']) |
| 1417 |
it.close() |
| 1418 |
|
| 1419 |
def test_unknown_events(self): |
| 1420 |
iterparse = ET.iterparse |
| 1421 |
|
| 1422 |
events = ("start", "end", "bogus") |
| 1423 |
with open(SIMPLE_XMLFILE, "rb") as f: |
| 1424 |
with self.assertRaises(ValueError) as cm: |
| 1425 |
iterparse(f, events) |
| 1426 |
self.assertFalse(f.closed) |
| 1427 |
self.assertEqual(str(cm.exception), "unknown event 'bogus'") |
| 1428 |
|
| 1429 |
with warnings_helper.check_no_resource_warning(self): |
| 1430 |
with self.assertRaises(ValueError) as cm: |
| 1431 |
iterparse(SIMPLE_XMLFILE, events) |
| 1432 |
self.assertEqual(str(cm.exception), "unknown event 'bogus'") |
| 1433 |
del cm |
| 1434 |
gc_collect() |
| 1435 |
|
| 1436 |
def test_non_utf8(self): |
| 1437 |
source = io.BytesIO( |
| 1438 |
b"<?xml version='1.0' encoding='iso-8859-1'?>\n" |
| 1439 |
b"<body xmlns='http://éffbot.org/ns'\n" |
| 1440 |
b" xmlns:cl\xe9='http://effbot.org/ns'>text</body>\n") |
| 1441 |
events = ("start-ns",) |
| 1442 |
it = ET.iterparse(source, events) |
| 1443 |
self.assertEqual([(action, elem) for action, elem in it], [ |
| 1444 |
('start-ns', ('', 'http://\xe9ffbot.org/ns')), |
| 1445 |
('start-ns', ('cl\xe9', 'http://effbot.org/ns')), |
| 1446 |
]) |
| 1447 |
|
| 1448 |
def test_parsing_error(self): |
| 1449 |
source = io.BytesIO(b"<document />junk") |
| 1450 |
it = ET.iterparse(source) |
| 1451 |
action, elem = next(it) |
| 1452 |
self.assertEqual((action, elem.tag), ('end', 'document')) |
| 1453 |
with self.assertRaises(ET.ParseError) as cm: |
| 1454 |
next(it) |
| 1455 |
self.assertEqual(str(cm.exception), |
| 1456 |
'junk after document element: line 1, column 12') |
| 1457 |
|
| 1458 |
def test_nonexistent_file(self): |
| 1459 |
with self.assertRaises(FileNotFoundError): |
| 1460 |
ET.iterparse("nonexistent") |
| 1461 |
|
| 1462 |
def test_resource_warnings_not_exhausted(self): |
| 1463 |
# Not exhausting the iterator still closes the underlying file (bpo-43292) |
| 1464 |
it = ET.iterparse(SIMPLE_XMLFILE) |
| 1465 |
with warnings_helper.check_no_resource_warning(self): |
| 1466 |
del it |
| 1467 |
gc_collect() |
| 1468 |
|
| 1469 |
it = ET.iterparse(SIMPLE_XMLFILE) |
| 1470 |
with warnings_helper.check_no_resource_warning(self): |
| 1471 |
action, elem = next(it) |
| 1472 |
self.assertEqual((action, elem.tag), ('end', 'element')) |
| 1473 |
del it, elem |
| 1474 |
gc_collect() |
| 1475 |
|
| 1476 |
def test_resource_warnings_failed_iteration(self): |
| 1477 |
self.addCleanup(os_helper.unlink, TESTFN) |
| 1478 |
with open(TESTFN, "wb") as f: |
| 1479 |
f.write(b"<document />junk") |
| 1480 |
|
| 1481 |
it = ET.iterparse(TESTFN) |
| 1482 |
action, elem = next(it) |
| 1483 |
self.assertEqual((action, elem.tag), ('end', 'document')) |
| 1484 |
with warnings_helper.check_no_resource_warning(self): |
| 1485 |
with self.assertRaises(ET.ParseError) as cm: |
| 1486 |
next(it) |
| 1487 |
self.assertEqual(str(cm.exception), |
| 1488 |
'junk after document element: line 1, column 12') |
| 1489 |
del cm, it |
| 1490 |
gc_collect() |
| 1491 |
|
| 1492 |
def test_resource_warnings_exhausted(self): |
| 1493 |
it = ET.iterparse(SIMPLE_XMLFILE) |
| 1494 |
with warnings_helper.check_no_resource_warning(self): |
| 1495 |
list(it) |
| 1496 |
del it |
| 1497 |
gc_collect() |
| 1498 |
|
| 1499 |
def test_close_not_exhausted(self): |
| 1500 |
iterparse = ET.iterparse |
| 1501 |
|
| 1502 |
it = iterparse(SIMPLE_XMLFILE) |
| 1503 |
it.close() |
| 1504 |
with self.assertRaises(StopIteration): |
| 1505 |
next(it) |
| 1506 |
it.close() # idempotent |
| 1507 |
|
| 1508 |
with open(SIMPLE_XMLFILE, 'rb') as source: |
| 1509 |
it = iterparse(source) |
| 1510 |
it.close() |
| 1511 |
self.assertFalse(source.closed) |
| 1512 |
with self.assertRaises(StopIteration): |
| 1513 |
next(it) |
| 1514 |
it.close() # idempotent |
| 1515 |
|
| 1516 |
it = iterparse(SIMPLE_XMLFILE) |
| 1517 |
action, elem = next(it) |
| 1518 |
self.assertEqual((action, elem.tag), ('end', 'element')) |
| 1519 |
it.close() |
| 1520 |
with self.assertRaises(StopIteration): |
| 1521 |
next(it) |
| 1522 |
it.close() # idempotent |
| 1523 |
|
| 1524 |
with open(SIMPLE_XMLFILE, 'rb') as source: |
| 1525 |
it = iterparse(source) |
| 1526 |
action, elem = next(it) |
| 1527 |
self.assertEqual((action, elem.tag), ('end', 'element')) |
| 1528 |
it.close() |
| 1529 |
self.assertFalse(source.closed) |
| 1530 |
with self.assertRaises(StopIteration): |
| 1531 |
next(it) |
| 1532 |
it.close() # idempotent |
| 1533 |
|
| 1534 |
def test_close_exhausted(self): |
| 1535 |
iterparse = ET.iterparse |
| 1536 |
it = iterparse(SIMPLE_XMLFILE) |
| 1537 |
list(it) |
| 1538 |
it.close() |
| 1539 |
with self.assertRaises(StopIteration): |
| 1540 |
next(it) |
| 1541 |
it.close() # idempotent |
| 1542 |
|
| 1543 |
with open(SIMPLE_XMLFILE, 'rb') as source: |
| 1544 |
it = iterparse(source) |
| 1545 |
list(it) |
| 1546 |
it.close() |
| 1547 |
self.assertFalse(source.closed) |
| 1548 |
with self.assertRaises(StopIteration): |
| 1549 |
next(it) |
| 1550 |
it.close() # idempotent |
| 1551 |
|
| 1552 |
|
| 1553 |
class XMLPullParserTest(unittest.TestCase): |
| 1554 |
|
| 1555 |
def _feed(self, parser, data, chunk_size=None, flush=False): |
| 1556 |
if chunk_size is None: |
| 1557 |
parser.feed(data) |
| 1558 |
else: |
| 1559 |
for i in range(0, len(data), chunk_size): |
| 1560 |
parser.feed(data[i:i+chunk_size]) |
| 1561 |
if flush: |
| 1562 |
parser.flush() |
| 1563 |
|
| 1564 |
def assert_events(self, parser, expected, max_events=None): |
| 1565 |
self.assertEqual( |
| 1566 |
[(event, (elem.tag, elem.text)) |
| 1567 |
for event, elem in islice(parser.read_events(), max_events)], |
| 1568 |
expected) |
| 1569 |
|
| 1570 |
def assert_event_tuples(self, parser, expected, max_events=None): |
| 1571 |
self.assertEqual( |
| 1572 |
list(islice(parser.read_events(), max_events)), |
| 1573 |
expected) |
| 1574 |
|
| 1575 |
def assert_event_tags(self, parser, expected, max_events=None): |
| 1576 |
events = islice(parser.read_events(), max_events) |
| 1577 |
self.assertEqual([(action, elem.tag) for action, elem in events], |
| 1578 |
expected) |
| 1579 |
|
| 1580 |
def test_simple_xml(self, chunk_size=None, flush=False): |
| 1581 |
parser = ET.XMLPullParser() |
| 1582 |
self.assert_event_tags(parser, []) |
| 1583 |
self._feed(parser, "<!-- comment -->\n", chunk_size, flush) |
| 1584 |
self.assert_event_tags(parser, []) |
| 1585 |
self._feed(parser, |
| 1586 |
"<root>\n <element key='value'>text</element", |
| 1587 |
chunk_size, flush) |
| 1588 |
self.assert_event_tags(parser, []) |
| 1589 |
self._feed(parser, ">\n", chunk_size, flush) |
| 1590 |
self.assert_event_tags(parser, [('end', 'element')]) |
| 1591 |
self._feed(parser, "<element>text</element>tail\n", chunk_size, flush) |
| 1592 |
self._feed(parser, "<empty-element/>\n", chunk_size, flush) |
| 1593 |
self.assert_event_tags(parser, [ |
| 1594 |
('end', 'element'), |
| 1595 |
('end', 'empty-element'), |
| 1596 |
]) |
| 1597 |
self._feed(parser, "</root>\n", chunk_size, flush) |
| 1598 |
self.assert_event_tags(parser, [('end', 'root')]) |
| 1599 |
self.assertIsNone(parser.close()) |
| 1600 |
|
| 1601 |
def test_simple_xml_chunk_1(self): |
| 1602 |
self.test_simple_xml(chunk_size=1, flush=True) |
| 1603 |
|
| 1604 |
def test_simple_xml_chunk_5(self): |
| 1605 |
self.test_simple_xml(chunk_size=5, flush=True) |
| 1606 |
|
| 1607 |
def test_simple_xml_chunk_22(self): |
| 1608 |
self.test_simple_xml(chunk_size=22) |
| 1609 |
|
| 1610 |
def test_feed_while_iterating(self): |
| 1611 |
parser = ET.XMLPullParser() |
| 1612 |
it = parser.read_events() |
| 1613 |
self._feed(parser, "<root>\n <element key='value'>text</element>\n") |
| 1614 |
action, elem = next(it) |
| 1615 |
self.assertEqual((action, elem.tag), ('end', 'element')) |
| 1616 |
self._feed(parser, "</root>\n") |
| 1617 |
action, elem = next(it) |
| 1618 |
self.assertEqual((action, elem.tag), ('end', 'root')) |
| 1619 |
with self.assertRaises(StopIteration): |
| 1620 |
next(it) |
| 1621 |
|
| 1622 |
def test_simple_xml_with_ns(self): |
| 1623 |
parser = ET.XMLPullParser() |
| 1624 |
self.assert_event_tags(parser, []) |
| 1625 |
self._feed(parser, "<!-- comment -->\n") |
| 1626 |
self.assert_event_tags(parser, []) |
| 1627 |
self._feed(parser, "<root xmlns='namespace'>\n") |
| 1628 |
self.assert_event_tags(parser, []) |
| 1629 |
self._feed(parser, "<element key='value'>text</element") |
| 1630 |
self.assert_event_tags(parser, []) |
| 1631 |
self._feed(parser, ">\n") |
| 1632 |
self.assert_event_tags(parser, [('end', '{namespace}element')]) |
| 1633 |
self._feed(parser, "<element>text</element>tail\n") |
| 1634 |
self._feed(parser, "<empty-element/>\n") |
| 1635 |
self.assert_event_tags(parser, [ |
| 1636 |
('end', '{namespace}element'), |
| 1637 |
('end', '{namespace}empty-element'), |
| 1638 |
]) |
| 1639 |
self._feed(parser, "</root>\n") |
| 1640 |
self.assert_event_tags(parser, [('end', '{namespace}root')]) |
| 1641 |
self.assertIsNone(parser.close()) |
| 1642 |
|
| 1643 |
def test_ns_events(self): |
| 1644 |
parser = ET.XMLPullParser(events=('start-ns', 'end-ns')) |
| 1645 |
self._feed(parser, "<!-- comment -->\n") |
| 1646 |
self._feed(parser, "<root xmlns='namespace'>\n") |
| 1647 |
self.assertEqual( |
| 1648 |
list(parser.read_events()), |
| 1649 |
[('start-ns', ('', 'namespace'))]) |
| 1650 |
self._feed(parser, "<element key='value'>text</element") |
| 1651 |
self._feed(parser, ">\n") |
| 1652 |
self._feed(parser, "<element>text</element>tail\n") |
| 1653 |
self._feed(parser, "<empty-element/>\n") |
| 1654 |
self._feed(parser, "</root>\n") |
| 1655 |
self.assertEqual(list(parser.read_events()), [('end-ns', None)]) |
| 1656 |
self.assertIsNone(parser.close()) |
| 1657 |
|
| 1658 |
def test_ns_events_start(self): |
| 1659 |
parser = ET.XMLPullParser(events=('start-ns', 'start', 'end')) |
| 1660 |
self._feed(parser, "<tag xmlns='abc' xmlns:p='xyz'>\n") |
| 1661 |
self.assert_event_tuples(parser, [ |
| 1662 |
('start-ns', ('', 'abc')), |
| 1663 |
('start-ns', ('p', 'xyz')), |
| 1664 |
], max_events=2) |
| 1665 |
self.assert_event_tags(parser, [ |
| 1666 |
('start', '{abc}tag'), |
| 1667 |
], max_events=1) |
| 1668 |
|
| 1669 |
self._feed(parser, "<child />\n") |
| 1670 |
self.assert_event_tags(parser, [ |
| 1671 |
('start', '{abc}child'), |
| 1672 |
('end', '{abc}child'), |
| 1673 |
]) |
| 1674 |
|
| 1675 |
self._feed(parser, "</tag>\n") |
| 1676 |
parser.close() |
| 1677 |
self.assert_event_tags(parser, [ |
| 1678 |
('end', '{abc}tag'), |
| 1679 |
]) |
| 1680 |
|
| 1681 |
def test_ns_events_start_end(self): |
| 1682 |
parser = ET.XMLPullParser(events=('start-ns', 'start', 'end', 'end-ns')) |
| 1683 |
self._feed(parser, "<tag xmlns='abc' xmlns:p='xyz'>\n") |
| 1684 |
self.assert_event_tuples(parser, [ |
| 1685 |
('start-ns', ('', 'abc')), |
| 1686 |
('start-ns', ('p', 'xyz')), |
| 1687 |
], max_events=2) |
| 1688 |
self.assert_event_tags(parser, [ |
| 1689 |
('start', '{abc}tag'), |
| 1690 |
], max_events=1) |
| 1691 |
|
| 1692 |
self._feed(parser, "<child />\n") |
| 1693 |
self.assert_event_tags(parser, [ |
| 1694 |
('start', '{abc}child'), |
| 1695 |
('end', '{abc}child'), |
| 1696 |
]) |
| 1697 |
|
| 1698 |
self._feed(parser, "</tag>\n") |
| 1699 |
parser.close() |
| 1700 |
self.assert_event_tags(parser, [ |
| 1701 |
('end', '{abc}tag'), |
| 1702 |
], max_events=1) |
| 1703 |
self.assert_event_tuples(parser, [ |
| 1704 |
('end-ns', None), |
| 1705 |
('end-ns', None), |
| 1706 |
]) |
| 1707 |
|
| 1708 |
def test_events(self): |
| 1709 |
parser = ET.XMLPullParser(events=()) |
| 1710 |
self._feed(parser, "<root/>\n") |
| 1711 |
self.assert_event_tags(parser, []) |
| 1712 |
|
| 1713 |
parser = ET.XMLPullParser(events=('start', 'end')) |
| 1714 |
self._feed(parser, "<!-- text here -->\n") |
| 1715 |
self.assert_events(parser, []) |
| 1716 |
|
| 1717 |
parser = ET.XMLPullParser(events=('start', 'end')) |
| 1718 |
self._feed(parser, "<root>\n") |
| 1719 |
self.assert_event_tags(parser, [('start', 'root')]) |
| 1720 |
self._feed(parser, "<element key='value'>text</element") |
| 1721 |
self.assert_event_tags(parser, [('start', 'element')]) |
| 1722 |
self._feed(parser, ">\n") |
| 1723 |
self.assert_event_tags(parser, [('end', 'element')]) |
| 1724 |
self._feed(parser, |
| 1725 |
"<element xmlns='foo'>text<empty-element/></element>tail\n") |
| 1726 |
self.assert_event_tags(parser, [ |
| 1727 |
('start', '{foo}element'), |
| 1728 |
('start', '{foo}empty-element'), |
| 1729 |
('end', '{foo}empty-element'), |
| 1730 |
('end', '{foo}element'), |
| 1731 |
]) |
| 1732 |
self._feed(parser, "</root>") |
| 1733 |
self.assertIsNone(parser.close()) |
| 1734 |
self.assert_event_tags(parser, [('end', 'root')]) |
| 1735 |
|
| 1736 |
parser = ET.XMLPullParser(events=('start',)) |
| 1737 |
self._feed(parser, "<!-- comment -->\n") |
| 1738 |
self.assert_event_tags(parser, []) |
| 1739 |
self._feed(parser, "<root>\n") |
| 1740 |
self.assert_event_tags(parser, [('start', 'root')]) |
| 1741 |
self._feed(parser, "<element key='value'>text</element") |
| 1742 |
self.assert_event_tags(parser, [('start', 'element')]) |
| 1743 |
self._feed(parser, ">\n") |
| 1744 |
self.assert_event_tags(parser, []) |
| 1745 |
self._feed(parser, |
| 1746 |
"<element xmlns='foo'>text<empty-element/></element>tail\n") |
| 1747 |
self.assert_event_tags(parser, [ |
| 1748 |
('start', '{foo}element'), |
| 1749 |
('start', '{foo}empty-element'), |
| 1750 |
]) |
| 1751 |
self._feed(parser, "</root>") |
| 1752 |
self.assertIsNone(parser.close()) |
| 1753 |
|
| 1754 |
def test_events_comment(self): |
| 1755 |
parser = ET.XMLPullParser(events=('start', 'comment', 'end')) |
| 1756 |
self._feed(parser, "<!-- text here -->\n") |
| 1757 |
self.assert_events(parser, [('comment', (ET.Comment, ' text here '))]) |
| 1758 |
self._feed(parser, "<!-- more text here -->\n") |
| 1759 |
self.assert_events(parser, [('comment', (ET.Comment, ' more text here '))]) |
| 1760 |
self._feed(parser, "<root-tag>text") |
| 1761 |
self.assert_event_tags(parser, [('start', 'root-tag')]) |
| 1762 |
self._feed(parser, "<!-- inner comment-->\n") |
| 1763 |
self.assert_events(parser, [('comment', (ET.Comment, ' inner comment'))]) |
| 1764 |
self._feed(parser, "</root-tag>\n") |
| 1765 |
self.assert_event_tags(parser, [('end', 'root-tag')]) |
| 1766 |
self._feed(parser, "<!-- outer comment -->\n") |
| 1767 |
self.assert_events(parser, [('comment', (ET.Comment, ' outer comment '))]) |
| 1768 |
|
| 1769 |
parser = ET.XMLPullParser(events=('comment',)) |
| 1770 |
self._feed(parser, "<!-- text here -->\n") |
| 1771 |
self.assert_events(parser, [('comment', (ET.Comment, ' text here '))]) |
| 1772 |
|
| 1773 |
def test_events_pi(self): |
| 1774 |
parser = ET.XMLPullParser(events=('start', 'pi', 'end')) |
| 1775 |
self._feed(parser, "<?pitarget?>\n") |
| 1776 |
self.assert_events(parser, [('pi', (ET.PI, 'pitarget'))]) |
| 1777 |
parser = ET.XMLPullParser(events=('pi',)) |
| 1778 |
self._feed(parser, "<?pitarget some text ?>\n") |
| 1779 |
self.assert_events(parser, [('pi', (ET.PI, 'pitarget some text '))]) |
| 1780 |
|
| 1781 |
def test_events_sequence(self): |
| 1782 |
# Test that events can be some sequence that's not just a tuple or list |
| 1783 |
eventset = {'end', 'start'} |
| 1784 |
parser = ET.XMLPullParser(events=eventset) |
| 1785 |
self._feed(parser, "<foo>bar</foo>") |
| 1786 |
self.assert_event_tags(parser, [('start', 'foo'), ('end', 'foo')]) |
| 1787 |
|
| 1788 |
class DummyIter: |
| 1789 |
def __init__(self): |
| 1790 |
self.events = iter(['start', 'end', 'start-ns']) |
| 1791 |
def __iter__(self): |
| 1792 |
return self |
| 1793 |
def __next__(self): |
| 1794 |
return next(self.events) |
| 1795 |
|
| 1796 |
parser = ET.XMLPullParser(events=DummyIter()) |
| 1797 |
self._feed(parser, "<foo>bar</foo>") |
| 1798 |
self.assert_event_tags(parser, [('start', 'foo'), ('end', 'foo')]) |
| 1799 |
|
| 1800 |
def test_unknown_event(self): |
| 1801 |
with self.assertRaises(ValueError): |
| 1802 |
ET.XMLPullParser(events=('start', 'end', 'bogus')) |
| 1803 |
with self.assertRaisesRegex(ValueError, "unknown event 'bogus'"): |
| 1804 |
ET.XMLPullParser(events=(x.decode() for x in (b'start', b'end', b'bogus'))) |
| 1805 |
|
| 1806 |
@unittest.skipIf(pyexpat.version_info < (2, 6, 0), |
| 1807 |
f'Expat {pyexpat.version_info} does not ' |
| 1808 |
'support reparse deferral') |
| 1809 |
def test_flush_reparse_deferral_enabled(self): |
| 1810 |
parser = ET.XMLPullParser(events=('start', 'end')) |
| 1811 |
|
| 1812 |
for chunk in ("<doc", ">"): |
| 1813 |
parser.feed(chunk) |
| 1814 |
|
| 1815 |
self.assert_event_tags(parser, []) # i.e. no elements started |
| 1816 |
if ET is pyET: |
| 1817 |
self.assertTrue(parser._parser._parser.GetReparseDeferralEnabled()) |
| 1818 |
|
| 1819 |
parser.flush() |
| 1820 |
|
| 1821 |
self.assert_event_tags(parser, [('start', 'doc')]) |
| 1822 |
if ET is pyET: |
| 1823 |
self.assertTrue(parser._parser._parser.GetReparseDeferralEnabled()) |
| 1824 |
|
| 1825 |
parser.feed("</doc>") |
| 1826 |
parser.close() |
| 1827 |
|
| 1828 |
self.assert_event_tags(parser, [('end', 'doc')]) |
| 1829 |
|
| 1830 |
def test_flush_reparse_deferral_disabled(self): |
| 1831 |
parser = ET.XMLPullParser(events=('start', 'end')) |
| 1832 |
|
| 1833 |
for chunk in ("<doc", ">"): |
| 1834 |
parser.feed(chunk) |
| 1835 |
|
| 1836 |
if pyexpat.version_info >= (2, 6, 0): |
| 1837 |
if not ET is pyET: |
| 1838 |
self.skipTest(f'XMLParser.(Get|Set)ReparseDeferralEnabled ' |
| 1839 |
'methods not available in C') |
| 1840 |
parser._parser._parser.SetReparseDeferralEnabled(False) |
| 1841 |
self.assert_event_tags(parser, []) # i.e. no elements started |
| 1842 |
|
| 1843 |
if ET is pyET: |
| 1844 |
self.assertFalse(parser._parser._parser.GetReparseDeferralEnabled()) |
| 1845 |
|
| 1846 |
parser.flush() |
| 1847 |
|
| 1848 |
self.assert_event_tags(parser, [('start', 'doc')]) |
| 1849 |
if ET is pyET: |
| 1850 |
self.assertFalse(parser._parser._parser.GetReparseDeferralEnabled()) |
| 1851 |
|
| 1852 |
parser.feed("</doc>") |
| 1853 |
parser.close() |
| 1854 |
|
| 1855 |
self.assert_event_tags(parser, [('end', 'doc')]) |
| 1856 |
|
| 1857 |
# |
| 1858 |
# xinclude tests (samples from appendix C of the xinclude specification) |
| 1859 |
|
| 1860 |
XINCLUDE = {} |
| 1861 |
|
| 1862 |
XINCLUDE["C1.xml"] = """\ |
| 1863 |
<?xml version='1.0'?> |
| 1864 |
<document xmlns:xi="http://www.w3.org/2001/XInclude"> |
| 1865 |
<p>120 Mz is adequate for an average home user.</p> |
| 1866 |
<xi:include href="disclaimer.xml"/> |
| 1867 |
</document> |
| 1868 |
""" |
| 1869 |
|
| 1870 |
XINCLUDE["disclaimer.xml"] = """\ |
| 1871 |
<?xml version='1.0'?> |
| 1872 |
<disclaimer> |
| 1873 |
<p>The opinions represented herein represent those of the individual |
| 1874 |
and should not be interpreted as official policy endorsed by this |
| 1875 |
organization.</p> |
| 1876 |
</disclaimer> |
| 1877 |
""" |
| 1878 |
|
| 1879 |
XINCLUDE["C2.xml"] = """\ |
| 1880 |
<?xml version='1.0'?> |
| 1881 |
<document xmlns:xi="http://www.w3.org/2001/XInclude"> |
| 1882 |
<p>This document has been accessed |
| 1883 |
<xi:include href="count.txt" parse="text"/> times.</p> |
| 1884 |
</document> |
| 1885 |
""" |
| 1886 |
|
| 1887 |
XINCLUDE["count.txt"] = "324387" |
| 1888 |
|
| 1889 |
XINCLUDE["C2b.xml"] = """\ |
| 1890 |
<?xml version='1.0'?> |
| 1891 |
<document xmlns:xi="http://www.w3.org/2001/XInclude"> |
| 1892 |
<p>This document has been <em>accessed</em> |
| 1893 |
<xi:include href="count.txt" parse="text"/> times.</p> |
| 1894 |
</document> |
| 1895 |
""" |
| 1896 |
|
| 1897 |
XINCLUDE["C3.xml"] = """\ |
| 1898 |
<?xml version='1.0'?> |
| 1899 |
<document xmlns:xi="http://www.w3.org/2001/XInclude"> |
| 1900 |
<p>The following is the source of the "data.xml" resource:</p> |
| 1901 |
<example><xi:include href="data.xml" parse="text"/></example> |
| 1902 |
</document> |
| 1903 |
""" |
| 1904 |
|
| 1905 |
XINCLUDE["data.xml"] = """\ |
| 1906 |
<?xml version='1.0'?> |
| 1907 |
<data> |
| 1908 |
<item><![CDATA[Brooks & Shields]]></item> |
| 1909 |
</data> |
| 1910 |
""" |
| 1911 |
|
| 1912 |
XINCLUDE["C5.xml"] = """\ |
| 1913 |
<?xml version='1.0'?> |
| 1914 |
<div xmlns:xi="http://www.w3.org/2001/XInclude"> |
| 1915 |
<xi:include href="example.txt" parse="text"> |
| 1916 |
<xi:fallback> |
| 1917 |
<xi:include href="fallback-example.txt" parse="text"> |
| 1918 |
<xi:fallback><a href="mailto:[email protected]">Report error</a></xi:fallback> |
| 1919 |
</xi:include> |
| 1920 |
</xi:fallback> |
| 1921 |
</xi:include> |
| 1922 |
</div> |
| 1923 |
""" |
| 1924 |
|
| 1925 |
XINCLUDE["default.xml"] = """\ |
| 1926 |
<?xml version='1.0'?> |
| 1927 |
<document xmlns:xi="http://www.w3.org/2001/XInclude"> |
| 1928 |
<p>Example.</p> |
| 1929 |
<xi:include href="{}"/> |
| 1930 |
</document> |
| 1931 |
""".format(html.escape(SIMPLE_XMLFILE, True)) |
| 1932 |
|
| 1933 |
XINCLUDE["include_c1_repeated.xml"] = """\ |
| 1934 |
<?xml version='1.0'?> |
| 1935 |
<document xmlns:xi="http://www.w3.org/2001/XInclude"> |
| 1936 |
<p>The following is the source code of Recursive1.xml:</p> |
| 1937 |
<xi:include href="C1.xml"/> |
| 1938 |
<xi:include href="C1.xml"/> |
| 1939 |
<xi:include href="C1.xml"/> |
| 1940 |
<xi:include href="C1.xml"/> |
| 1941 |
</document> |
| 1942 |
""" |
| 1943 |
|
| 1944 |
# |
| 1945 |
# badly formatted xi:include tags |
| 1946 |
|
| 1947 |
XINCLUDE_BAD = {} |
| 1948 |
|
| 1949 |
XINCLUDE_BAD["B1.xml"] = """\ |
| 1950 |
<?xml version='1.0'?> |
| 1951 |
<document xmlns:xi="http://www.w3.org/2001/XInclude"> |
| 1952 |
<p>120 Mz is adequate for an average home user.</p> |
| 1953 |
<xi:include href="disclaimer.xml" parse="BAD_TYPE"/> |
| 1954 |
</document> |
| 1955 |
""" |
| 1956 |
|
| 1957 |
XINCLUDE_BAD["B2.xml"] = """\ |
| 1958 |
<?xml version='1.0'?> |
| 1959 |
<div xmlns:xi="http://www.w3.org/2001/XInclude"> |
| 1960 |
<xi:fallback></xi:fallback> |
| 1961 |
</div> |
| 1962 |
""" |
| 1963 |
|
| 1964 |
XINCLUDE["Recursive1.xml"] = """\ |
| 1965 |
<?xml version='1.0'?> |
| 1966 |
<document xmlns:xi="http://www.w3.org/2001/XInclude"> |
| 1967 |
<p>The following is the source code of Recursive2.xml:</p> |
| 1968 |
<xi:include href="Recursive2.xml"/> |
| 1969 |
</document> |
| 1970 |
""" |
| 1971 |
|
| 1972 |
XINCLUDE["Recursive2.xml"] = """\ |
| 1973 |
<?xml version='1.0'?> |
| 1974 |
<document xmlns:xi="http://www.w3.org/2001/XInclude"> |
| 1975 |
<p>The following is the source code of Recursive3.xml:</p> |
| 1976 |
<xi:include href="Recursive3.xml"/> |
| 1977 |
</document> |
| 1978 |
""" |
| 1979 |
|
| 1980 |
XINCLUDE["Recursive3.xml"] = """\ |
| 1981 |
<?xml version='1.0'?> |
| 1982 |
<document xmlns:xi="http://www.w3.org/2001/XInclude"> |
| 1983 |
<p>The following is the source code of Recursive1.xml:</p> |
| 1984 |
<xi:include href="Recursive1.xml"/> |
| 1985 |
</document> |
| 1986 |
""" |
| 1987 |
|
| 1988 |
|
| 1989 |
class XIncludeTest(unittest.TestCase): |
| 1990 |
|
| 1991 |
def xinclude_loader(self, href, parse="xml", encoding=None): |
| 1992 |
try: |
| 1993 |
data = XINCLUDE[href] |
| 1994 |
except KeyError: |
| 1995 |
raise OSError("resource not found") |
| 1996 |
if parse == "xml": |
| 1997 |
data = ET.XML(data) |
| 1998 |
return data |
| 1999 |
|
| 2000 |
def none_loader(self, href, parser, encoding=None): |
| 2001 |
return None |
| 2002 |
|
| 2003 |
def _my_loader(self, href, parse): |
| 2004 |
# Used to avoid a test-dependency problem where the default loader |
| 2005 |
# of ElementInclude uses the pyET parser for cET tests. |
| 2006 |
if parse == 'xml': |
| 2007 |
with open(href, 'rb') as f: |
| 2008 |
return ET.parse(f).getroot() |
| 2009 |
else: |
| 2010 |
return None |
| 2011 |
|
| 2012 |
def test_xinclude_default(self): |
| 2013 |
from xml.etree import ElementInclude |
| 2014 |
doc = self.xinclude_loader('default.xml') |
| 2015 |
ElementInclude.include(doc, self._my_loader) |
| 2016 |
self.assertEqual(serialize(doc), |
| 2017 |
'<document>\n' |
| 2018 |
' <p>Example.</p>\n' |
| 2019 |
' <root>\n' |
| 2020 |
' <element key="value">text</element>\n' |
| 2021 |
' <element>text</element>tail\n' |
| 2022 |
' <empty-element />\n' |
| 2023 |
'</root>\n' |
| 2024 |
'</document>') |
| 2025 |
|
| 2026 |
def test_xinclude(self): |
| 2027 |
from xml.etree import ElementInclude |
| 2028 |
|
| 2029 |
# Basic inclusion example (XInclude C.1) |
| 2030 |
document = self.xinclude_loader("C1.xml") |
| 2031 |
ElementInclude.include(document, self.xinclude_loader) |
| 2032 |
self.assertEqual(serialize(document), |
| 2033 |
'<document>\n' |
| 2034 |
' <p>120 Mz is adequate for an average home user.</p>\n' |
| 2035 |
' <disclaimer>\n' |
| 2036 |
' <p>The opinions represented herein represent those of the individual\n' |
| 2037 |
' and should not be interpreted as official policy endorsed by this\n' |
| 2038 |
' organization.</p>\n' |
| 2039 |
'</disclaimer>\n' |
| 2040 |
'</document>') # C1 |
| 2041 |
|
| 2042 |
# Textual inclusion example (XInclude C.2) |
| 2043 |
document = self.xinclude_loader("C2.xml") |
| 2044 |
ElementInclude.include(document, self.xinclude_loader) |
| 2045 |
self.assertEqual(serialize(document), |
| 2046 |
'<document>\n' |
| 2047 |
' <p>This document has been accessed\n' |
| 2048 |
' 324387 times.</p>\n' |
| 2049 |
'</document>') # C2 |
| 2050 |
|
| 2051 |
# Textual inclusion after sibling element (based on modified XInclude C.2) |
| 2052 |
document = self.xinclude_loader("C2b.xml") |
| 2053 |
ElementInclude.include(document, self.xinclude_loader) |
| 2054 |
self.assertEqual(serialize(document), |
| 2055 |
'<document>\n' |
| 2056 |
' <p>This document has been <em>accessed</em>\n' |
| 2057 |
' 324387 times.</p>\n' |
| 2058 |
'</document>') # C2b |
| 2059 |
|
| 2060 |
# Textual inclusion of XML example (XInclude C.3) |
| 2061 |
document = self.xinclude_loader("C3.xml") |
| 2062 |
ElementInclude.include(document, self.xinclude_loader) |
| 2063 |
self.assertEqual(serialize(document), |
| 2064 |
'<document>\n' |
| 2065 |
' <p>The following is the source of the "data.xml" resource:</p>\n' |
| 2066 |
" <example><?xml version='1.0'?>\n" |
| 2067 |
'<data>\n' |
| 2068 |
' <item><![CDATA[Brooks & Shields]]></item>\n' |
| 2069 |
'</data>\n' |
| 2070 |
'</example>\n' |
| 2071 |
'</document>') # C3 |
| 2072 |
|
| 2073 |
# Fallback example (XInclude C.5) |
| 2074 |
# Note! Fallback support is not yet implemented |
| 2075 |
document = self.xinclude_loader("C5.xml") |
| 2076 |
with self.assertRaises(OSError) as cm: |
| 2077 |
ElementInclude.include(document, self.xinclude_loader) |
| 2078 |
self.assertEqual(str(cm.exception), 'resource not found') |
| 2079 |
self.assertEqual(serialize(document), |
| 2080 |
'<div xmlns:ns0="http://www.w3.org/2001/XInclude">\n' |
| 2081 |
' <ns0:include href="example.txt" parse="text">\n' |
| 2082 |
' <ns0:fallback>\n' |
| 2083 |
' <ns0:include href="fallback-example.txt" parse="text">\n' |
| 2084 |
' <ns0:fallback><a href="mailto:[email protected]">Report error</a></ns0:fallback>\n' |
| 2085 |
' </ns0:include>\n' |
| 2086 |
' </ns0:fallback>\n' |
| 2087 |
' </ns0:include>\n' |
| 2088 |
'</div>') # C5 |
| 2089 |
|
| 2090 |
def test_xinclude_repeated(self): |
| 2091 |
from xml.etree import ElementInclude |
| 2092 |
|
| 2093 |
document = self.xinclude_loader("include_c1_repeated.xml") |
| 2094 |
ElementInclude.include(document, self.xinclude_loader) |
| 2095 |
self.assertEqual(1+4*2, len(document.findall(".//p"))) |
| 2096 |
|
| 2097 |
def test_xinclude_failures(self): |
| 2098 |
from xml.etree import ElementInclude |
| 2099 |
|
| 2100 |
# Test failure to locate included XML file. |
| 2101 |
document = ET.XML(XINCLUDE["C1.xml"]) |
| 2102 |
with self.assertRaises(ElementInclude.FatalIncludeError) as cm: |
| 2103 |
ElementInclude.include(document, loader=self.none_loader) |
| 2104 |
self.assertEqual(str(cm.exception), |
| 2105 |
"cannot load 'disclaimer.xml' as 'xml'") |
| 2106 |
|
| 2107 |
# Test failure to locate included text file. |
| 2108 |
document = ET.XML(XINCLUDE["C2.xml"]) |
| 2109 |
with self.assertRaises(ElementInclude.FatalIncludeError) as cm: |
| 2110 |
ElementInclude.include(document, loader=self.none_loader) |
| 2111 |
self.assertEqual(str(cm.exception), |
| 2112 |
"cannot load 'count.txt' as 'text'") |
| 2113 |
|
| 2114 |
# Test bad parse type. |
| 2115 |
document = ET.XML(XINCLUDE_BAD["B1.xml"]) |
| 2116 |
with self.assertRaises(ElementInclude.FatalIncludeError) as cm: |
| 2117 |
ElementInclude.include(document, loader=self.none_loader) |
| 2118 |
self.assertEqual(str(cm.exception), |
| 2119 |
"unknown parse type in xi:include tag ('BAD_TYPE')") |
| 2120 |
|
| 2121 |
# Test xi:fallback outside xi:include. |
| 2122 |
document = ET.XML(XINCLUDE_BAD["B2.xml"]) |
| 2123 |
with self.assertRaises(ElementInclude.FatalIncludeError) as cm: |
| 2124 |
ElementInclude.include(document, loader=self.none_loader) |
| 2125 |
self.assertEqual(str(cm.exception), |
| 2126 |
"xi:fallback tag must be child of xi:include " |
| 2127 |
"('{http://www.w3.org/2001/XInclude}fallback')") |
| 2128 |
|
| 2129 |
# Test infinitely recursive includes. |
| 2130 |
document = self.xinclude_loader("Recursive1.xml") |
| 2131 |
with self.assertRaises(ElementInclude.FatalIncludeError) as cm: |
| 2132 |
ElementInclude.include(document, self.xinclude_loader) |
| 2133 |
self.assertEqual(str(cm.exception), |
| 2134 |
"recursive include of Recursive2.xml") |
| 2135 |
|
| 2136 |
# Test 'max_depth' limitation. |
| 2137 |
document = self.xinclude_loader("Recursive1.xml") |
| 2138 |
with self.assertRaises(ElementInclude.FatalIncludeError) as cm: |
| 2139 |
ElementInclude.include(document, self.xinclude_loader, max_depth=None) |
| 2140 |
self.assertEqual(str(cm.exception), |
| 2141 |
"recursive include of Recursive2.xml") |
| 2142 |
|
| 2143 |
document = self.xinclude_loader("Recursive1.xml") |
| 2144 |
with self.assertRaises(ElementInclude.LimitedRecursiveIncludeError) as cm: |
| 2145 |
ElementInclude.include(document, self.xinclude_loader, max_depth=0) |
| 2146 |
self.assertEqual(str(cm.exception), |
| 2147 |
"maximum xinclude depth reached when including file Recursive2.xml") |
| 2148 |
|
| 2149 |
document = self.xinclude_loader("Recursive1.xml") |
| 2150 |
with self.assertRaises(ElementInclude.LimitedRecursiveIncludeError) as cm: |
| 2151 |
ElementInclude.include(document, self.xinclude_loader, max_depth=1) |
| 2152 |
self.assertEqual(str(cm.exception), |
| 2153 |
"maximum xinclude depth reached when including file Recursive3.xml") |
| 2154 |
|
| 2155 |
document = self.xinclude_loader("Recursive1.xml") |
| 2156 |
with self.assertRaises(ElementInclude.LimitedRecursiveIncludeError) as cm: |
| 2157 |
ElementInclude.include(document, self.xinclude_loader, max_depth=2) |
| 2158 |
self.assertEqual(str(cm.exception), |
| 2159 |
"maximum xinclude depth reached when including file Recursive1.xml") |
| 2160 |
|
| 2161 |
document = self.xinclude_loader("Recursive1.xml") |
| 2162 |
with self.assertRaises(ElementInclude.FatalIncludeError) as cm: |
| 2163 |
ElementInclude.include(document, self.xinclude_loader, max_depth=3) |
| 2164 |
self.assertEqual(str(cm.exception), |
| 2165 |
"recursive include of Recursive2.xml") |
| 2166 |
|
| 2167 |
|
| 2168 |
# -------------------------------------------------------------------- |
| 2169 |
# reported bugs |
| 2170 |
|
| 2171 |
class BugsTest(unittest.TestCase): |
| 2172 |
|
| 2173 |
def test_bug_xmltoolkit21(self): |
| 2174 |
# marshaller gives obscure errors for non-string values |
| 2175 |
|
| 2176 |
def check(elem): |
| 2177 |
with self.assertRaises(TypeError) as cm: |
| 2178 |
serialize(elem) |
| 2179 |
self.assertEqual(str(cm.exception), |
| 2180 |
'cannot serialize 123 (type int)') |
| 2181 |
|
| 2182 |
elem = ET.Element(123) |
| 2183 |
check(elem) # tag |
| 2184 |
|
| 2185 |
elem = ET.Element("elem") |
| 2186 |
elem.text = 123 |
| 2187 |
check(elem) # text |
| 2188 |
|
| 2189 |
elem = ET.Element("elem") |
| 2190 |
elem.tail = 123 |
| 2191 |
check(elem) # tail |
| 2192 |
|
| 2193 |
elem = ET.Element("elem") |
| 2194 |
elem.set(123, "123") |
| 2195 |
check(elem) # attribute key |
| 2196 |
|
| 2197 |
elem = ET.Element("elem") |
| 2198 |
elem.set("123", 123) |
| 2199 |
check(elem) # attribute value |
| 2200 |
|
| 2201 |
def test_bug_xmltoolkit25(self): |
| 2202 |
# typo in ElementTree.findtext |
| 2203 |
|
| 2204 |
elem = ET.XML(SAMPLE_XML) |
| 2205 |
tree = ET.ElementTree(elem) |
| 2206 |
self.assertEqual(tree.findtext("tag"), 'text') |
| 2207 |
self.assertEqual(tree.findtext("section/tag"), 'subtext') |
| 2208 |
|
| 2209 |
def test_bug_xmltoolkit28(self): |
| 2210 |
# .//tag causes exceptions |
| 2211 |
|
| 2212 |
tree = ET.XML("<doc><table><tbody/></table></doc>") |
| 2213 |
self.assertEqual(summarize_list(tree.findall(".//thead")), []) |
| 2214 |
self.assertEqual(summarize_list(tree.findall(".//tbody")), ['tbody']) |
| 2215 |
|
| 2216 |
def test_bug_xmltoolkitX1(self): |
| 2217 |
# dump() doesn't flush the output buffer |
| 2218 |
|
| 2219 |
tree = ET.XML("<doc><table><tbody/></table></doc>") |
| 2220 |
with support.captured_stdout() as stdout: |
| 2221 |
ET.dump(tree) |
| 2222 |
self.assertEqual(stdout.getvalue(), '<doc><table><tbody /></table></doc>\n') |
| 2223 |
|
| 2224 |
def test_bug_xmltoolkit39(self): |
| 2225 |
# non-ascii element and attribute names doesn't work |
| 2226 |
|
| 2227 |
tree = ET.XML(b"<?xml version='1.0' encoding='iso-8859-1'?><t\xe4g />") |
| 2228 |
self.assertEqual(ET.tostring(tree, "utf-8"), b'<t\xc3\xa4g />') |
| 2229 |
|
| 2230 |
tree = ET.XML(b"<?xml version='1.0' encoding='iso-8859-1'?>" |
| 2231 |
b"<tag \xe4ttr='välue' />") |
| 2232 |
self.assertEqual(tree.attrib, {'\xe4ttr': 'v\xe4lue'}) |
| 2233 |
self.assertEqual(ET.tostring(tree, "utf-8"), |
| 2234 |
b'<tag \xc3\xa4ttr="v\xc3\xa4lue" />') |
| 2235 |
|
| 2236 |
tree = ET.XML(b"<?xml version='1.0' encoding='iso-8859-1'?>" |
| 2237 |
b'<t\xe4g>text</t\xe4g>') |
| 2238 |
self.assertEqual(ET.tostring(tree, "utf-8"), |
| 2239 |
b'<t\xc3\xa4g>text</t\xc3\xa4g>') |
| 2240 |
|
| 2241 |
tree = ET.Element("t\u00e4g") |
| 2242 |
self.assertEqual(ET.tostring(tree, "utf-8"), b'<t\xc3\xa4g />') |
| 2243 |
|
| 2244 |
tree = ET.Element("tag") |
| 2245 |
tree.set("\u00e4ttr", "v\u00e4lue") |
| 2246 |
self.assertEqual(ET.tostring(tree, "utf-8"), |
| 2247 |
b'<tag \xc3\xa4ttr="v\xc3\xa4lue" />') |
| 2248 |
|
| 2249 |
def test_bug_xmltoolkit54(self): |
| 2250 |
# problems handling internally defined entities |
| 2251 |
|
| 2252 |
e = ET.XML("<!DOCTYPE doc [<!ENTITY ldots '舰'>]>" |
| 2253 |
'<doc>&ldots;</doc>') |
| 2254 |
self.assertEqual(serialize(e, encoding="us-ascii"), |
| 2255 |
b'<doc>舰</doc>') |
| 2256 |
self.assertEqual(serialize(e), '<doc>\u8230</doc>') |
| 2257 |
|
| 2258 |
def test_bug_xmltoolkit55(self): |
| 2259 |
# make sure we're reporting the first error, not the last |
| 2260 |
|
| 2261 |
with self.assertRaises(ET.ParseError) as cm: |
| 2262 |
ET.XML(b"<!DOCTYPE doc SYSTEM 'doc.dtd'>" |
| 2263 |
b'<doc>&ldots;&ndots;&rdots;</doc>') |
| 2264 |
self.assertEqual(str(cm.exception), |
| 2265 |
'undefined entity &ldots;: line 1, column 36') |
| 2266 |
|
| 2267 |
def test_bug_xmltoolkit60(self): |
| 2268 |
# Handle crash in stream source. |
| 2269 |
|
| 2270 |
class ExceptionFile: |
| 2271 |
def read(self, x): |
| 2272 |
raise OSError |
| 2273 |
|
| 2274 |
self.assertRaises(OSError, ET.parse, ExceptionFile()) |
| 2275 |
|
| 2276 |
def test_bug_xmltoolkit62(self): |
| 2277 |
# Don't crash when using custom entities. |
| 2278 |
|
| 2279 |
ENTITIES = {'rsquo': '\u2019', 'lsquo': '\u2018'} |
| 2280 |
parser = ET.XMLParser() |
| 2281 |
parser.entity.update(ENTITIES) |
| 2282 |
parser.feed("""<?xml version="1.0" encoding="UTF-8"?> |
| 2283 |
<!DOCTYPE patent-application-publication SYSTEM "pap-v15-2001-01-31.dtd" []> |
| 2284 |
<patent-application-publication> |
| 2285 |
<subdoc-abstract> |
| 2286 |
<paragraph id="A-0001" lvl="0">A new cultivar of Begonia plant named ‘BCT9801BEG’.</paragraph> |
| 2287 |
</subdoc-abstract> |
| 2288 |
</patent-application-publication>""") |
| 2289 |
t = parser.close() |
| 2290 |
self.assertEqual(t.find('.//paragraph').text, |
| 2291 |
'A new cultivar of Begonia plant named \u2018BCT9801BEG\u2019.') |
| 2292 |
|
| 2293 |
@unittest.skipIf(sys.gettrace(), "Skips under coverage.") |
| 2294 |
def test_bug_xmltoolkit63(self): |
| 2295 |
# Check reference leak. |
| 2296 |
def xmltoolkit63(): |
| 2297 |
tree = ET.TreeBuilder() |
| 2298 |
tree.start("tag", {}) |
| 2299 |
tree.data("text") |
| 2300 |
tree.end("tag") |
| 2301 |
|
| 2302 |
xmltoolkit63() |
| 2303 |
count = sys.getrefcount(None) |
| 2304 |
for i in range(1000): |
| 2305 |
xmltoolkit63() |
| 2306 |
self.assertEqual(sys.getrefcount(None), count) |
| 2307 |
|
| 2308 |
def test_bug_200708_newline(self): |
| 2309 |
# Preserve newlines in attributes. |
| 2310 |
|
| 2311 |
e = ET.Element('SomeTag', text="def _f():\n return 3\n") |
| 2312 |
self.assertEqual(ET.tostring(e), |
| 2313 |
b'<SomeTag text="def _f(): return 3 " />') |
| 2314 |
self.assertEqual(ET.XML(ET.tostring(e)).get("text"), |
| 2315 |
'def _f():\n return 3\n') |
| 2316 |
self.assertEqual(ET.tostring(ET.XML(ET.tostring(e))), |
| 2317 |
b'<SomeTag text="def _f(): return 3 " />') |
| 2318 |
|
| 2319 |
def test_bug_200708_close(self): |
| 2320 |
# Test default builder. |
| 2321 |
parser = ET.XMLParser() # default |
| 2322 |
parser.feed("<element>some text</element>") |
| 2323 |
self.assertEqual(parser.close().tag, 'element') |
| 2324 |
|
| 2325 |
# Test custom builder. |
| 2326 |
class EchoTarget: |
| 2327 |
def close(self): |
| 2328 |
return ET.Element("element") # simulate root |
| 2329 |
parser = ET.XMLParser(target=EchoTarget()) |
| 2330 |
parser.feed("<element>some text</element>") |
| 2331 |
self.assertEqual(parser.close().tag, 'element') |
| 2332 |
|
| 2333 |
def test_bug_200709_default_namespace(self): |
| 2334 |
e = ET.Element("{default}elem") |
| 2335 |
s = ET.SubElement(e, "{default}elem") |
| 2336 |
self.assertEqual(serialize(e, default_namespace="default"), # 1 |
| 2337 |
'<elem xmlns="default"><elem /></elem>') |
| 2338 |
|
| 2339 |
e = ET.Element("{default}elem") |
| 2340 |
s = ET.SubElement(e, "{default}elem") |
| 2341 |
s = ET.SubElement(e, "{not-default}elem") |
| 2342 |
self.assertEqual(serialize(e, default_namespace="default"), # 2 |
| 2343 |
'<elem xmlns="default" xmlns:ns1="not-default">' |
| 2344 |
'<elem />' |
| 2345 |
'<ns1:elem />' |
| 2346 |
'</elem>') |
| 2347 |
|
| 2348 |
e = ET.Element("{default}elem") |
| 2349 |
s = ET.SubElement(e, "{default}elem") |
| 2350 |
s = ET.SubElement(e, "elem") # unprefixed name |
| 2351 |
with self.assertRaises(ValueError) as cm: |
| 2352 |
serialize(e, default_namespace="default") # 3 |
| 2353 |
self.assertEqual(str(cm.exception), |
| 2354 |
'cannot use non-qualified names with default_namespace option') |
| 2355 |
|
| 2356 |
def test_bug_200709_register_namespace(self): |
| 2357 |
e = ET.Element("{http://namespace.invalid/does/not/exist/}title") |
| 2358 |
self.assertEqual(ET.tostring(e), |
| 2359 |
b'<ns0:title xmlns:ns0="http://namespace.invalid/does/not/exist/" />') |
| 2360 |
ET.register_namespace("foo", "http://namespace.invalid/does/not/exist/") |
| 2361 |
e = ET.Element("{http://namespace.invalid/does/not/exist/}title") |
| 2362 |
self.assertEqual(ET.tostring(e), |
| 2363 |
b'<foo:title xmlns:foo="http://namespace.invalid/does/not/exist/" />') |
| 2364 |
|
| 2365 |
# And the Dublin Core namespace is in the default list: |
| 2366 |
|
| 2367 |
e = ET.Element("{http://purl.org/dc/elements/1.1/}title") |
| 2368 |
self.assertEqual(ET.tostring(e), |
| 2369 |
b'<dc:title xmlns:dc="http://purl.org/dc/elements/1.1/" />') |
| 2370 |
|
| 2371 |
def test_bug_200709_element_comment(self): |
| 2372 |
# Not sure if this can be fixed, really (since the serializer needs |
| 2373 |
# ET.Comment, not cET.comment). |
| 2374 |
|
| 2375 |
a = ET.Element('a') |
| 2376 |
a.append(ET.Comment('foo')) |
| 2377 |
self.assertEqual(a[0].tag, ET.Comment) |
| 2378 |
|
| 2379 |
a = ET.Element('a') |
| 2380 |
a.append(ET.PI('foo')) |
| 2381 |
self.assertEqual(a[0].tag, ET.PI) |
| 2382 |
|
| 2383 |
def test_bug_200709_element_insert(self): |
| 2384 |
a = ET.Element('a') |
| 2385 |
b = ET.SubElement(a, 'b') |
| 2386 |
c = ET.SubElement(a, 'c') |
| 2387 |
d = ET.Element('d') |
| 2388 |
a.insert(0, d) |
| 2389 |
self.assertEqual(summarize_list(a), ['d', 'b', 'c']) |
| 2390 |
a.insert(-1, d) |
| 2391 |
self.assertEqual(summarize_list(a), ['d', 'b', 'd', 'c']) |
| 2392 |
|
| 2393 |
def test_bug_200709_iter_comment(self): |
| 2394 |
a = ET.Element('a') |
| 2395 |
b = ET.SubElement(a, 'b') |
| 2396 |
comment_b = ET.Comment("TEST-b") |
| 2397 |
b.append(comment_b) |
| 2398 |
self.assertEqual(summarize_list(a.iter(ET.Comment)), [ET.Comment]) |
| 2399 |
|
| 2400 |
# -------------------------------------------------------------------- |
| 2401 |
# reported on bugs.python.org |
| 2402 |
|
| 2403 |
def test_bug_1534630(self): |
| 2404 |
bob = ET.TreeBuilder() |
| 2405 |
e = bob.data("data") |
| 2406 |
e = bob.start("tag", {}) |
| 2407 |
e = bob.end("tag") |
| 2408 |
e = bob.close() |
| 2409 |
self.assertEqual(serialize(e), '<tag />') |
| 2410 |
|
| 2411 |
def test_issue6233(self): |
| 2412 |
e = ET.XML(b"<?xml version='1.0' encoding='utf-8'?>" |
| 2413 |
b'<body>t\xc3\xa3g</body>') |
| 2414 |
self.assertEqual(ET.tostring(e, 'ascii'), |
| 2415 |
b"<?xml version='1.0' encoding='ascii'?>\n" |
| 2416 |
b'<body>tãg</body>') |
| 2417 |
e = ET.XML(b"<?xml version='1.0' encoding='iso-8859-1'?>" |
| 2418 |
b'<body>t\xe3g</body>') |
| 2419 |
self.assertEqual(ET.tostring(e, 'ascii'), |
| 2420 |
b"<?xml version='1.0' encoding='ascii'?>\n" |
| 2421 |
b'<body>tãg</body>') |
| 2422 |
|
| 2423 |
def test_issue6565(self): |
| 2424 |
elem = ET.XML("<body><tag/></body>") |
| 2425 |
self.assertEqual(summarize_list(elem), ['tag']) |
| 2426 |
newelem = ET.XML(SAMPLE_XML) |
| 2427 |
elem[:] = newelem[:] |
| 2428 |
self.assertEqual(summarize_list(elem), ['tag', 'tag', 'section']) |
| 2429 |
|
| 2430 |
def test_issue10777(self): |
| 2431 |
# Registering a namespace twice caused a "dictionary changed size during |
| 2432 |
# iteration" bug. |
| 2433 |
|
| 2434 |
ET.register_namespace('test10777', 'http://myuri/') |
| 2435 |
ET.register_namespace('test10777', 'http://myuri/') |
| 2436 |
|
| 2437 |
def test_lost_text(self): |
| 2438 |
# Issue #25902: Borrowed text can disappear |
| 2439 |
class Text: |
| 2440 |
def __bool__(self): |
| 2441 |
e.text = 'changed' |
| 2442 |
return True |
| 2443 |
|
| 2444 |
e = ET.Element('tag') |
| 2445 |
e.text = Text() |
| 2446 |
i = e.itertext() |
| 2447 |
t = next(i) |
| 2448 |
self.assertIsInstance(t, Text) |
| 2449 |
self.assertIsInstance(e.text, str) |
| 2450 |
self.assertEqual(e.text, 'changed') |
| 2451 |
|
| 2452 |
def test_lost_tail(self): |
| 2453 |
# Issue #25902: Borrowed tail can disappear |
| 2454 |
class Text: |
| 2455 |
def __bool__(self): |
| 2456 |
e[0].tail = 'changed' |
| 2457 |
return True |
| 2458 |
|
| 2459 |
e = ET.Element('root') |
| 2460 |
e.append(ET.Element('tag')) |
| 2461 |
e[0].tail = Text() |
| 2462 |
i = e.itertext() |
| 2463 |
t = next(i) |
| 2464 |
self.assertIsInstance(t, Text) |
| 2465 |
self.assertIsInstance(e[0].tail, str) |
| 2466 |
self.assertEqual(e[0].tail, 'changed') |
| 2467 |
|
| 2468 |
def test_lost_elem(self): |
| 2469 |
# Issue #25902: Borrowed element can disappear |
| 2470 |
class Tag: |
| 2471 |
def __eq__(self, other): |
| 2472 |
e[0] = ET.Element('changed') |
| 2473 |
next(i) |
| 2474 |
return True |
| 2475 |
|
| 2476 |
e = ET.Element('root') |
| 2477 |
e.append(ET.Element(Tag())) |
| 2478 |
e.append(ET.Element('tag')) |
| 2479 |
i = e.iter('tag') |
| 2480 |
try: |
| 2481 |
t = next(i) |
| 2482 |
except ValueError: |
| 2483 |
self.skipTest('generators are not reentrant') |
| 2484 |
self.assertIsInstance(t.tag, Tag) |
| 2485 |
self.assertIsInstance(e[0].tag, str) |
| 2486 |
self.assertEqual(e[0].tag, 'changed') |
| 2487 |
|
| 2488 |
def check_expat224_utf8_bug(self, text): |
| 2489 |
xml = b'<a b="%s"/>' % text |
| 2490 |
root = ET.XML(xml) |
| 2491 |
self.assertEqual(root.get('b'), text.decode('utf-8')) |
| 2492 |
|
| 2493 |
def test_expat224_utf8_bug(self): |
| 2494 |
# bpo-31170: Expat 2.2.3 had a bug in its UTF-8 decoder. |
| 2495 |
# Check that Expat 2.2.4 fixed the bug. |
| 2496 |
# |
| 2497 |
# Test buffer bounds at odd and even positions. |
| 2498 |
|
| 2499 |
text = b'\xc3\xa0' * 1024 |
| 2500 |
self.check_expat224_utf8_bug(text) |
| 2501 |
|
| 2502 |
text = b'x' + b'\xc3\xa0' * 1024 |
| 2503 |
self.check_expat224_utf8_bug(text) |
| 2504 |
|
| 2505 |
def test_expat224_utf8_bug_file(self): |
| 2506 |
with open(UTF8_BUG_XMLFILE, 'rb') as fp: |
| 2507 |
raw = fp.read() |
| 2508 |
root = ET.fromstring(raw) |
| 2509 |
xmlattr = root.get('b') |
| 2510 |
|
| 2511 |
# "Parse" manually the XML file to extract the value of the 'b' |
| 2512 |
# attribute of the <a b='xxx' /> XML element |
| 2513 |
text = raw.decode('utf-8').strip() |
| 2514 |
text = text.replace('\r\n', ' ') |
| 2515 |
text = text[6:-4] |
| 2516 |
self.assertEqual(root.get('b'), text) |
| 2517 |
|
| 2518 |
def test_39495_treebuilder_start(self): |
| 2519 |
self.assertRaises(TypeError, ET.TreeBuilder().start, "tag") |
| 2520 |
self.assertRaises(TypeError, ET.TreeBuilder().start, "tag", None) |
| 2521 |
|
| 2522 |
def test_issue123213_correct_extend_exception(self): |
| 2523 |
# Does not hide the internal exception when extending the element |
| 2524 |
self.assertRaises(ZeroDivisionError, ET.Element('tag').extend, |
| 2525 |
(1/0 for i in range(2))) |
| 2526 |
|
| 2527 |
# Still raises the TypeError when extending with a non-iterable |
| 2528 |
self.assertRaises(TypeError, ET.Element('tag').extend, None) |
| 2529 |
|
| 2530 |
# Preserves the TypeError message when extending with a generator |
| 2531 |
def f(): |
| 2532 |
raise TypeError("mymessage") |
| 2533 |
|
| 2534 |
self.assertRaisesRegex( |
| 2535 |
TypeError, 'mymessage', |
| 2536 |
ET.Element('tag').extend, (f() for i in range(2))) |
| 2537 |
|
| 2538 |
|
| 2539 |
|
| 2540 |
# -------------------------------------------------------------------- |
| 2541 |
|
| 2542 |
|
| 2543 |
class BasicElementTest(ElementTestCase, unittest.TestCase): |
| 2544 |
|
| 2545 |
def test___init__(self): |
| 2546 |
tag = "foo" |
| 2547 |
attrib = { "zix": "wyp" } |
| 2548 |
|
| 2549 |
element_foo = ET.Element(tag, attrib) |
| 2550 |
|
| 2551 |
# traits of an element |
| 2552 |
self.assertIsInstance(element_foo, ET.Element) |
| 2553 |
self.assertIn("tag", dir(element_foo)) |
| 2554 |
self.assertIn("attrib", dir(element_foo)) |
| 2555 |
self.assertIn("text", dir(element_foo)) |
| 2556 |
self.assertIn("tail", dir(element_foo)) |
| 2557 |
|
| 2558 |
# string attributes have expected values |
| 2559 |
self.assertEqual(element_foo.tag, tag) |
| 2560 |
self.assertIsNone(element_foo.text) |
| 2561 |
self.assertIsNone(element_foo.tail) |
| 2562 |
|
| 2563 |
# attrib is a copy |
| 2564 |
self.assertIsNot(element_foo.attrib, attrib) |
| 2565 |
self.assertEqual(element_foo.attrib, attrib) |
| 2566 |
|
| 2567 |
# attrib isn't linked |
| 2568 |
attrib["bar"] = "baz" |
| 2569 |
self.assertIsNot(element_foo.attrib, attrib) |
| 2570 |
self.assertNotEqual(element_foo.attrib, attrib) |
| 2571 |
|
| 2572 |
def test___copy__(self): |
| 2573 |
element_foo = ET.Element("foo", { "zix": "wyp" }) |
| 2574 |
element_foo.append(ET.Element("bar", { "baz": "qix" })) |
| 2575 |
|
| 2576 |
element_foo2 = copy.copy(element_foo) |
| 2577 |
|
| 2578 |
# elements are not the same |
| 2579 |
self.assertIsNot(element_foo2, element_foo) |
| 2580 |
|
| 2581 |
# string attributes are equal |
| 2582 |
self.assertEqual(element_foo2.tag, element_foo.tag) |
| 2583 |
self.assertEqual(element_foo2.text, element_foo.text) |
| 2584 |
self.assertEqual(element_foo2.tail, element_foo.tail) |
| 2585 |
|
| 2586 |
# number of children is the same |
| 2587 |
self.assertEqual(len(element_foo2), len(element_foo)) |
| 2588 |
|
| 2589 |
# children are the same |
| 2590 |
for (child1, child2) in itertools.zip_longest(element_foo, element_foo2): |
| 2591 |
self.assertIs(child1, child2) |
| 2592 |
|
| 2593 |
# attrib is a copy |
| 2594 |
self.assertEqual(element_foo2.attrib, element_foo.attrib) |
| 2595 |
|
| 2596 |
def test___deepcopy__(self): |
| 2597 |
element_foo = ET.Element("foo", { "zix": "wyp" }) |
| 2598 |
element_foo.append(ET.Element("bar", { "baz": "qix" })) |
| 2599 |
|
| 2600 |
element_foo2 = copy.deepcopy(element_foo) |
| 2601 |
|
| 2602 |
# elements are not the same |
| 2603 |
self.assertIsNot(element_foo2, element_foo) |
| 2604 |
|
| 2605 |
# string attributes are equal |
| 2606 |
self.assertEqual(element_foo2.tag, element_foo.tag) |
| 2607 |
self.assertEqual(element_foo2.text, element_foo.text) |
| 2608 |
self.assertEqual(element_foo2.tail, element_foo.tail) |
| 2609 |
|
| 2610 |
# number of children is the same |
| 2611 |
self.assertEqual(len(element_foo2), len(element_foo)) |
| 2612 |
|
| 2613 |
# children are not the same |
| 2614 |
for (child1, child2) in itertools.zip_longest(element_foo, element_foo2): |
| 2615 |
self.assertIsNot(child1, child2) |
| 2616 |
|
| 2617 |
# attrib is a copy |
| 2618 |
self.assertIsNot(element_foo2.attrib, element_foo.attrib) |
| 2619 |
self.assertEqual(element_foo2.attrib, element_foo.attrib) |
| 2620 |
|
| 2621 |
# attrib isn't linked |
| 2622 |
element_foo.attrib["bar"] = "baz" |
| 2623 |
self.assertIsNot(element_foo2.attrib, element_foo.attrib) |
| 2624 |
self.assertNotEqual(element_foo2.attrib, element_foo.attrib) |
| 2625 |
|
| 2626 |
def test_augmentation_type_errors(self): |
| 2627 |
e = ET.Element('joe') |
| 2628 |
self.assertRaises(TypeError, e.append, 'b') |
| 2629 |
self.assertRaises(TypeError, e.extend, [ET.Element('bar'), 'foo']) |
| 2630 |
self.assertRaises(TypeError, e.insert, 0, 'foo') |
| 2631 |
e[:] = [ET.Element('bar')] |
| 2632 |
with self.assertRaises(TypeError): |
| 2633 |
e[0] = 'foo' |
| 2634 |
with self.assertRaises(TypeError): |
| 2635 |
e[:] = [ET.Element('bar'), 'foo'] |
| 2636 |
|
| 2637 |
if hasattr(e, '__setstate__'): |
| 2638 |
state = { |
| 2639 |
'tag': 'tag', |
| 2640 |
'_children': [None], # non-Element |
| 2641 |
'attrib': 'attr', |
| 2642 |
'tail': 'tail', |
| 2643 |
'text': 'text', |
| 2644 |
} |
| 2645 |
self.assertRaises(TypeError, e.__setstate__, state) |
| 2646 |
|
| 2647 |
if hasattr(e, '__deepcopy__'): |
| 2648 |
class E(ET.Element): |
| 2649 |
def __deepcopy__(self, memo): |
| 2650 |
return None # non-Element |
| 2651 |
e[:] = [E('bar')] |
| 2652 |
self.assertRaises(TypeError, copy.deepcopy, e) |
| 2653 |
|
| 2654 |
def test_cyclic_gc(self): |
| 2655 |
class Dummy: |
| 2656 |
pass |
| 2657 |
|
| 2658 |
# Test the shortest cycle: d->element->d |
| 2659 |
d = Dummy() |
| 2660 |
d.dummyref = ET.Element('joe', attr=d) |
| 2661 |
wref = weakref.ref(d) |
| 2662 |
del d |
| 2663 |
gc_collect() |
| 2664 |
self.assertIsNone(wref()) |
| 2665 |
|
| 2666 |
# A longer cycle: d->e->e2->d |
| 2667 |
e = ET.Element('joe') |
| 2668 |
d = Dummy() |
| 2669 |
d.dummyref = e |
| 2670 |
wref = weakref.ref(d) |
| 2671 |
e2 = ET.SubElement(e, 'foo', attr=d) |
| 2672 |
del d, e, e2 |
| 2673 |
gc_collect() |
| 2674 |
self.assertIsNone(wref()) |
| 2675 |
|
| 2676 |
# A cycle between Element objects as children of one another |
| 2677 |
# e1->e2->e3->e1 |
| 2678 |
e1 = ET.Element('e1') |
| 2679 |
e2 = ET.Element('e2') |
| 2680 |
e3 = ET.Element('e3') |
| 2681 |
e3.append(e1) |
| 2682 |
e2.append(e3) |
| 2683 |
e1.append(e2) |
| 2684 |
wref = weakref.ref(e1) |
| 2685 |
del e1, e2, e3 |
| 2686 |
gc_collect() |
| 2687 |
self.assertIsNone(wref()) |
| 2688 |
|
| 2689 |
def test_weakref(self): |
| 2690 |
flag = False |
| 2691 |
def wref_cb(w): |
| 2692 |
nonlocal flag |
| 2693 |
flag = True |
| 2694 |
e = ET.Element('e') |
| 2695 |
wref = weakref.ref(e, wref_cb) |
| 2696 |
self.assertEqual(wref().tag, 'e') |
| 2697 |
del e |
| 2698 |
gc_collect() # For PyPy or other GCs. |
| 2699 |
self.assertEqual(flag, True) |
| 2700 |
self.assertEqual(wref(), None) |
| 2701 |
|
| 2702 |
def test_get_keyword_args(self): |
| 2703 |
e1 = ET.Element('foo' , x=1, y=2, z=3) |
| 2704 |
self.assertEqual(e1.get('x', default=7), 1) |
| 2705 |
self.assertEqual(e1.get('w', default=7), 7) |
| 2706 |
|
| 2707 |
def test_pickle(self): |
| 2708 |
# issue #16076: the C implementation wasn't pickleable. |
| 2709 |
for proto in range(2, pickle.HIGHEST_PROTOCOL + 1): |
| 2710 |
for dumper, loader in product(self.modules, repeat=2): |
| 2711 |
e = dumper.Element('foo', bar=42) |
| 2712 |
e.text = "text goes here" |
| 2713 |
e.tail = "opposite of head" |
| 2714 |
dumper.SubElement(e, 'child').append(dumper.Element('grandchild')) |
| 2715 |
e.append(dumper.Element('child')) |
| 2716 |
e.findall('.//grandchild')[0].set('attr', 'other value') |
| 2717 |
|
| 2718 |
e2 = self.pickleRoundTrip(e, 'xml.etree.ElementTree', |
| 2719 |
dumper, loader, proto) |
| 2720 |
|
| 2721 |
self.assertEqual(e2.tag, 'foo') |
| 2722 |
self.assertEqual(e2.attrib['bar'], 42) |
| 2723 |
self.assertEqual(len(e2), 2) |
| 2724 |
self.assertEqualElements(e, e2) |
| 2725 |
|
| 2726 |
def test_pickle_issue18997(self): |
| 2727 |
for proto in range(2, pickle.HIGHEST_PROTOCOL + 1): |
| 2728 |
for dumper, loader in product(self.modules, repeat=2): |
| 2729 |
XMLTEXT = """<?xml version="1.0"?> |
| 2730 |
<group><dogs>4</dogs> |
| 2731 |
</group>""" |
| 2732 |
e1 = dumper.fromstring(XMLTEXT) |
| 2733 |
self.assertEqual(e1.__getstate__()['tag'], 'group') |
| 2734 |
e2 = self.pickleRoundTrip(e1, 'xml.etree.ElementTree', |
| 2735 |
dumper, loader, proto) |
| 2736 |
self.assertEqual(e2.tag, 'group') |
| 2737 |
self.assertEqual(e2[0].tag, 'dogs') |
| 2738 |
|
| 2739 |
|
| 2740 |
class BadElementTest(ElementTestCase, unittest.TestCase): |
| 2741 |
|
| 2742 |
def test_extend_mutable_list(self): |
| 2743 |
class X: |
| 2744 |
@property |
| 2745 |
def __class__(self): |
| 2746 |
L[:] = [ET.Element('baz')] |
| 2747 |
return ET.Element |
| 2748 |
L = [X()] |
| 2749 |
e = ET.Element('foo') |
| 2750 |
try: |
| 2751 |
e.extend(L) |
| 2752 |
except TypeError: |
| 2753 |
pass |
| 2754 |
|
| 2755 |
class Y(X, ET.Element): |
| 2756 |
pass |
| 2757 |
L = [Y('x')] |
| 2758 |
e = ET.Element('foo') |
| 2759 |
e.extend(L) |
| 2760 |
|
| 2761 |
def test_extend_mutable_list2(self): |
| 2762 |
class X: |
| 2763 |
@property |
| 2764 |
def __class__(self): |
| 2765 |
del L[:] |
| 2766 |
return ET.Element |
| 2767 |
L = [X(), ET.Element('baz')] |
| 2768 |
e = ET.Element('foo') |
| 2769 |
try: |
| 2770 |
e.extend(L) |
| 2771 |
except TypeError: |
| 2772 |
pass |
| 2773 |
|
| 2774 |
class Y(X, ET.Element): |
| 2775 |
pass |
| 2776 |
L = [Y('bar'), ET.Element('baz')] |
| 2777 |
e = ET.Element('foo') |
| 2778 |
e.extend(L) |
| 2779 |
|
| 2780 |
def test_remove_with_clear_assume_missing(self): |
| 2781 |
# gh-126033: Check that a concurrent clear() for an assumed-to-be |
| 2782 |
# missing element does not make the interpreter crash. |
| 2783 |
self.do_test_remove_with_clear(raises=True) |
| 2784 |
|
| 2785 |
def test_remove_with_clear_assume_existing(self): |
| 2786 |
# gh-126033: Check that a concurrent clear() for an assumed-to-be |
| 2787 |
# existing element does not make the interpreter crash. |
| 2788 |
self.do_test_remove_with_clear(raises=False) |
| 2789 |
|
| 2790 |
def do_test_remove_with_clear(self, *, raises): |
| 2791 |
|
| 2792 |
# Until the discrepency between "del root[:]" and "root.clear()" is |
| 2793 |
# resolved, we need to keep two tests. Previously, using "del root[:]" |
| 2794 |
# did not crash with the reproducer of gh-126033 while "root.clear()" |
| 2795 |
# did. |
| 2796 |
|
| 2797 |
class E(ET.Element): |
| 2798 |
"""Local class to be able to mock E.__eq__ for introspection.""" |
| 2799 |
|
| 2800 |
class X(E): |
| 2801 |
def __eq__(self, o): |
| 2802 |
del root[:] |
| 2803 |
return not raises |
| 2804 |
|
| 2805 |
class Y(E): |
| 2806 |
def __eq__(self, o): |
| 2807 |
root.clear() |
| 2808 |
return not raises |
| 2809 |
|
| 2810 |
if raises: |
| 2811 |
get_checker_context = lambda: self.assertRaises(ValueError) |
| 2812 |
else: |
| 2813 |
get_checker_context = nullcontext |
| 2814 |
|
| 2815 |
self.assertIs(E.__eq__, object.__eq__) |
| 2816 |
|
| 2817 |
for Z, side_effect in [(X, 'del root[:]'), (Y, 'root.clear()')]: |
| 2818 |
self.enterContext(self.subTest(side_effect=side_effect)) |
| 2819 |
|
| 2820 |
# test removing R() from [U()] |
| 2821 |
for R, U, description in [ |
| 2822 |
(E, Z, "remove missing E() from [Z()]"), |
| 2823 |
(Z, E, "remove missing Z() from [E()]"), |
| 2824 |
(Z, Z, "remove missing Z() from [Z()]"), |
| 2825 |
]: |
| 2826 |
with self.subTest(description): |
| 2827 |
root = E('top') |
| 2828 |
root.extend([U('one')]) |
| 2829 |
with get_checker_context(): |
| 2830 |
root.remove(R('missing')) |
| 2831 |
|
| 2832 |
# test removing R() from [U(), V()] |
| 2833 |
cases = self.cases_for_remove_missing_with_mutations(E, Z) |
| 2834 |
for R, U, V, description in cases: |
| 2835 |
with self.subTest(description): |
| 2836 |
root = E('top') |
| 2837 |
root.extend([U('one'), V('two')]) |
| 2838 |
with get_checker_context(): |
| 2839 |
root.remove(R('missing')) |
| 2840 |
|
| 2841 |
# Test removing root[0] from [Z()]. |
| 2842 |
# |
| 2843 |
# Since we call root.remove() with root[0], Z.__eq__() |
| 2844 |
# will not be called (we branch on the fast Py_EQ path). |
| 2845 |
with self.subTest("remove root[0] from [Z()]"): |
| 2846 |
root = E('top') |
| 2847 |
root.append(Z('rem')) |
| 2848 |
with equal_wrapper(E) as f, equal_wrapper(Z) as g: |
| 2849 |
root.remove(root[0]) |
| 2850 |
f.assert_not_called() |
| 2851 |
g.assert_not_called() |
| 2852 |
|
| 2853 |
# Test removing root[1] (of type R) from [U(), R()]. |
| 2854 |
is_special = is_python_implementation() and raises and Z is Y |
| 2855 |
if is_python_implementation() and raises and Z is Y: |
| 2856 |
# In pure Python, using root.clear() sets the children |
| 2857 |
# list to [] without calling list.clear(). |
| 2858 |
# |
| 2859 |
# For this reason, the call to root.remove() first |
| 2860 |
# checks root[0] and sets the children list to [] |
| 2861 |
# since either root[0] or root[1] is an evil element. |
| 2862 |
# |
| 2863 |
# Since checking root[1] still uses the old reference |
| 2864 |
# to the children list, PyObject_RichCompareBool() branches |
| 2865 |
# to the fast Py_EQ path and Y.__eq__() is called exactly |
| 2866 |
# once (when checking root[0]). |
| 2867 |
continue |
| 2868 |
else: |
| 2869 |
cases = self.cases_for_remove_existing_with_mutations(E, Z) |
| 2870 |
for R, U, description in cases: |
| 2871 |
with self.subTest(description): |
| 2872 |
root = E('top') |
| 2873 |
root.extend([U('one'), R('rem')]) |
| 2874 |
with get_checker_context(): |
| 2875 |
root.remove(root[1]) |
| 2876 |
|
| 2877 |
def test_remove_with_mutate_root_assume_missing(self): |
| 2878 |
# gh-126033: Check that a concurrent mutation for an assumed-to-be |
| 2879 |
# missing element does not make the interpreter crash. |
| 2880 |
self.do_test_remove_with_mutate_root(raises=True) |
| 2881 |
|
| 2882 |
def test_remove_with_mutate_root_assume_existing(self): |
| 2883 |
# gh-126033: Check that a concurrent mutation for an assumed-to-be |
| 2884 |
# existing element does not make the interpreter crash. |
| 2885 |
self.do_test_remove_with_mutate_root(raises=False) |
| 2886 |
|
| 2887 |
def do_test_remove_with_mutate_root(self, *, raises): |
| 2888 |
E = ET.Element |
| 2889 |
|
| 2890 |
class Z(E): |
| 2891 |
def __eq__(self, o): |
| 2892 |
del root[0] |
| 2893 |
return not raises |
| 2894 |
|
| 2895 |
if raises: |
| 2896 |
get_checker_context = lambda: self.assertRaises(ValueError) |
| 2897 |
else: |
| 2898 |
get_checker_context = nullcontext |
| 2899 |
|
| 2900 |
# test removing R() from [U(), V()] |
| 2901 |
cases = self.cases_for_remove_missing_with_mutations(E, Z) |
| 2902 |
for R, U, V, description in cases: |
| 2903 |
with self.subTest(description): |
| 2904 |
root = E('top') |
| 2905 |
root.extend([U('one'), V('two')]) |
| 2906 |
with get_checker_context(): |
| 2907 |
root.remove(R('missing')) |
| 2908 |
|
| 2909 |
# test removing root[1] (of type R) from [U(), R()] |
| 2910 |
cases = self.cases_for_remove_existing_with_mutations(E, Z) |
| 2911 |
for R, U, description in cases: |
| 2912 |
with self.subTest(description): |
| 2913 |
root = E('top') |
| 2914 |
root.extend([U('one'), R('rem')]) |
| 2915 |
with get_checker_context(): |
| 2916 |
root.remove(root[1]) |
| 2917 |
|
| 2918 |
def cases_for_remove_missing_with_mutations(self, E, Z): |
| 2919 |
# Cases for removing R() from [U(), V()]. |
| 2920 |
# The case U = V = R = E is not interesting as there is no mutation. |
| 2921 |
for U, V in [(E, Z), (Z, E), (Z, Z)]: |
| 2922 |
description = (f"remove missing {E.__name__}() from " |
| 2923 |
f"[{U.__name__}(), {V.__name__}()]") |
| 2924 |
yield E, U, V, description |
| 2925 |
|
| 2926 |
for U, V in [(E, E), (E, Z), (Z, E), (Z, Z)]: |
| 2927 |
description = (f"remove missing {Z.__name__}() from " |
| 2928 |
f"[{U.__name__}(), {V.__name__}()]") |
| 2929 |
yield Z, U, V, description |
| 2930 |
|
| 2931 |
def cases_for_remove_existing_with_mutations(self, E, Z): |
| 2932 |
# Cases for removing root[1] (of type R) from [U(), R()]. |
| 2933 |
# The case U = R = E is not interesting as there is no mutation. |
| 2934 |
for U, R, description in [ |
| 2935 |
(E, Z, "remove root[1] from [E(), Z()]"), |
| 2936 |
(Z, E, "remove root[1] from [Z(), E()]"), |
| 2937 |
(Z, Z, "remove root[1] from [Z(), Z()]"), |
| 2938 |
]: |
| 2939 |
description = (f"remove root[1] (of type {R.__name__}) " |
| 2940 |
f"from [{U.__name__}(), {R.__name__}()]") |
| 2941 |
yield R, U, description |
| 2942 |
|
| 2943 |
@support.infinite_recursion(25) |
| 2944 |
def test_recursive_repr(self): |
| 2945 |
# Issue #25455 |
| 2946 |
e = ET.Element('foo') |
| 2947 |
with swap_attr(e, 'tag', e): |
| 2948 |
with self.assertRaises(RuntimeError): |
| 2949 |
repr(e) # Should not crash |
| 2950 |
|
| 2951 |
def test_element_get_text(self): |
| 2952 |
# Issue #27863 |
| 2953 |
class X(str): |
| 2954 |
def __del__(self): |
| 2955 |
try: |
| 2956 |
elem.text |
| 2957 |
except NameError: |
| 2958 |
pass |
| 2959 |
|
| 2960 |
b = ET.TreeBuilder() |
| 2961 |
b.start('tag', {}) |
| 2962 |
b.data('ABCD') |
| 2963 |
b.data(X('EFGH')) |
| 2964 |
b.data('IJKL') |
| 2965 |
b.end('tag') |
| 2966 |
|
| 2967 |
elem = b.close() |
| 2968 |
self.assertEqual(elem.text, 'ABCDEFGHIJKL') |
| 2969 |
|
| 2970 |
def test_element_get_tail(self): |
| 2971 |
# Issue #27863 |
| 2972 |
class X(str): |
| 2973 |
def __del__(self): |
| 2974 |
try: |
| 2975 |
elem[0].tail |
| 2976 |
except NameError: |
| 2977 |
pass |
| 2978 |
|
| 2979 |
b = ET.TreeBuilder() |
| 2980 |
b.start('root', {}) |
| 2981 |
b.start('tag', {}) |
| 2982 |
b.end('tag') |
| 2983 |
b.data('ABCD') |
| 2984 |
b.data(X('EFGH')) |
| 2985 |
b.data('IJKL') |
| 2986 |
b.end('root') |
| 2987 |
|
| 2988 |
elem = b.close() |
| 2989 |
self.assertEqual(elem[0].tail, 'ABCDEFGHIJKL') |
| 2990 |
|
| 2991 |
def test_subscr_with_clear(self): |
| 2992 |
# See https://github.com/python/cpython/issues/143200. |
| 2993 |
self.do_test_subscr_with_mutating_slice(use_clear_method=True) |
| 2994 |
|
| 2995 |
def test_subscr_with_delete(self): |
| 2996 |
# See https://github.com/python/cpython/issues/72050. |
| 2997 |
self.do_test_subscr_with_mutating_slice(use_clear_method=False) |
| 2998 |
|
| 2999 |
def do_test_subscr_with_mutating_slice(self, *, use_clear_method): |
| 3000 |
class X: |
| 3001 |
def __init__(self, i=0): |
| 3002 |
self.i = i |
| 3003 |
def __index__(self): |
| 3004 |
if use_clear_method: |
| 3005 |
e.clear() |
| 3006 |
else: |
| 3007 |
del e[:] |
| 3008 |
return self.i |
| 3009 |
|
| 3010 |
for s in self.get_mutating_slices(X, 10): |
| 3011 |
with self.subTest(s): |
| 3012 |
e = ET.Element('elem') |
| 3013 |
e.extend([ET.Element(f'c{i}') for i in range(10)]) |
| 3014 |
e[s] # shouldn't crash |
| 3015 |
|
| 3016 |
def test_ass_subscr_with_mutating_slice(self): |
| 3017 |
# See https://github.com/python/cpython/issues/72050 |
| 3018 |
# and https://github.com/python/cpython/issues/143200. |
| 3019 |
|
| 3020 |
class X: |
| 3021 |
def __init__(self, i=0): |
| 3022 |
self.i = i |
| 3023 |
def __index__(self): |
| 3024 |
e[:] = [] |
| 3025 |
return self.i |
| 3026 |
|
| 3027 |
for s in self.get_mutating_slices(X, 10): |
| 3028 |
with self.subTest(s): |
| 3029 |
e = ET.Element('elem') |
| 3030 |
e.extend([ET.Element(f'c{i}') for i in range(10)]) |
| 3031 |
e[s] = [] # shouldn't crash |
| 3032 |
|
| 3033 |
def get_mutating_slices(self, index_class, n_children): |
| 3034 |
self.assertGreaterEqual(n_children, 10) |
| 3035 |
return [ |
| 3036 |
slice(index_class(), None, None), |
| 3037 |
slice(index_class(2), None, None), |
| 3038 |
slice(None, index_class(), None), |
| 3039 |
slice(None, index_class(2), None), |
| 3040 |
slice(0, 2, index_class(1)), |
| 3041 |
slice(0, 2, index_class(2)), |
| 3042 |
slice(0, n_children, index_class(1)), |
| 3043 |
slice(0, n_children, index_class(2)), |
| 3044 |
slice(0, 2 * n_children, index_class(1)), |
| 3045 |
slice(0, 2 * n_children, index_class(2)), |
| 3046 |
] |
| 3047 |
|
| 3048 |
def test_ass_subscr_with_mutating_iterable_value(self): |
| 3049 |
class V: |
| 3050 |
def __iter__(self): |
| 3051 |
e.clear() |
| 3052 |
return iter([ET.Element('a'), ET.Element('b')]) |
| 3053 |
|
| 3054 |
e = ET.Element('elem') |
| 3055 |
e.extend([ET.Element(f'c{i}') for i in range(10)]) |
| 3056 |
e[:] = V() |
| 3057 |
|
| 3058 |
def test_treebuilder_start(self): |
| 3059 |
# Issue #27863 |
| 3060 |
def element_factory(x, y): |
| 3061 |
return [] |
| 3062 |
b = ET.TreeBuilder(element_factory=element_factory) |
| 3063 |
|
| 3064 |
b.start('tag', {}) |
| 3065 |
b.data('ABCD') |
| 3066 |
self.assertRaises(AttributeError, b.start, 'tag2', {}) |
| 3067 |
del b |
| 3068 |
gc_collect() |
| 3069 |
|
| 3070 |
def test_treebuilder_end(self): |
| 3071 |
# Issue #27863 |
| 3072 |
def element_factory(x, y): |
| 3073 |
return [] |
| 3074 |
b = ET.TreeBuilder(element_factory=element_factory) |
| 3075 |
|
| 3076 |
b.start('tag', {}) |
| 3077 |
b.data('ABCD') |
| 3078 |
self.assertRaises(AttributeError, b.end, 'tag') |
| 3079 |
del b |
| 3080 |
gc_collect() |
| 3081 |
|
| 3082 |
def test_deepcopy_clear(self): |
| 3083 |
# Prevent crashes when __deepcopy__() clears the children list. |
| 3084 |
# See https://github.com/python/cpython/issues/133009. |
| 3085 |
class X(ET.Element): |
| 3086 |
def __deepcopy__(self, memo): |
| 3087 |
root.clear() |
| 3088 |
return self |
| 3089 |
|
| 3090 |
root = ET.Element('a') |
| 3091 |
evil = X('x') |
| 3092 |
root.extend([evil, ET.Element('y')]) |
| 3093 |
if is_python_implementation(): |
| 3094 |
# Mutating a list over which we iterate raises an error. |
| 3095 |
self.assertRaises(RuntimeError, copy.deepcopy, root) |
| 3096 |
else: |
| 3097 |
c = copy.deepcopy(root) |
| 3098 |
# In the C implementation, we can still copy the evil element. |
| 3099 |
self.assertListEqual(list(c), [evil]) |
| 3100 |
|
| 3101 |
def test_deepcopy_grow(self): |
| 3102 |
# Prevent crashes when __deepcopy__() mutates the children list. |
| 3103 |
# See https://github.com/python/cpython/issues/133009. |
| 3104 |
a = ET.Element('a') |
| 3105 |
b = ET.Element('b') |
| 3106 |
c = ET.Element('c') |
| 3107 |
|
| 3108 |
class X(ET.Element): |
| 3109 |
def __deepcopy__(self, memo): |
| 3110 |
root.append(a) |
| 3111 |
root.append(b) |
| 3112 |
return self |
| 3113 |
|
| 3114 |
root = ET.Element('top') |
| 3115 |
evil1, evil2 = X('1'), X('2') |
| 3116 |
root.extend([evil1, c, evil2]) |
| 3117 |
children = list(copy.deepcopy(root)) |
| 3118 |
# mock deep copies |
| 3119 |
self.assertIs(children[0], evil1) |
| 3120 |
self.assertIs(children[2], evil2) |
| 3121 |
# true deep copies |
| 3122 |
self.assertEqual(children[1].tag, c.tag) |
| 3123 |
self.assertEqual([c.tag for c in children[3:]], |
| 3124 |
[a.tag, b.tag, a.tag, b.tag]) |
| 3125 |
|
| 3126 |
@support.skip_if_unlimited_stack_size |
| 3127 |
@support.skip_emscripten_stack_overflow() |
| 3128 |
@support.skip_wasi_stack_overflow() |
| 3129 |
def test_deeply_nested_deepcopy(self): |
| 3130 |
# This should raise a RecursionError and not crash. |
| 3131 |
# See https://github.com/python/cpython/issues/148801. |
| 3132 |
root = cur = ET.Element('s') |
| 3133 |
for _ in range(500_000): |
| 3134 |
cur = ET.SubElement(cur, 'u') |
| 3135 |
with support.infinite_recursion(): |
| 3136 |
with self.assertRaises(RecursionError): |
| 3137 |
copy.deepcopy(root) |
| 3138 |
|
| 3139 |
|
| 3140 |
class MutationDeleteElementPath(str): |
| 3141 |
def __new__(cls, elem, *args): |
| 3142 |
self = str.__new__(cls, *args) |
| 3143 |
self.elem = elem |
| 3144 |
return self |
| 3145 |
|
| 3146 |
def __eq__(self, o): |
| 3147 |
del self.elem[:] |
| 3148 |
return True |
| 3149 |
|
| 3150 |
__hash__ = str.__hash__ |
| 3151 |
|
| 3152 |
|
| 3153 |
class MutationClearElementPath(str): |
| 3154 |
def __new__(cls, elem, *args): |
| 3155 |
self = str.__new__(cls, *args) |
| 3156 |
self.elem = elem |
| 3157 |
return self |
| 3158 |
|
| 3159 |
def __eq__(self, o): |
| 3160 |
self.elem.clear() |
| 3161 |
return True |
| 3162 |
|
| 3163 |
__hash__ = str.__hash__ |
| 3164 |
|
| 3165 |
|
| 3166 |
class BadElementPath(str): |
| 3167 |
def __eq__(self, o): |
| 3168 |
raise 1/0 |
| 3169 |
|
| 3170 |
__hash__ = str.__hash__ |
| 3171 |
|
| 3172 |
|
| 3173 |
class BadElementPathTest(ElementTestCase, unittest.TestCase): |
| 3174 |
def setUp(self): |
| 3175 |
super().setUp() |
| 3176 |
from xml.etree import ElementPath |
| 3177 |
self.path_cache = ElementPath._cache |
| 3178 |
ElementPath._cache = {} |
| 3179 |
|
| 3180 |
def tearDown(self): |
| 3181 |
from xml.etree import ElementPath |
| 3182 |
ElementPath._cache = self.path_cache |
| 3183 |
super().tearDown() |
| 3184 |
|
| 3185 |
def test_find_with_mutating(self): |
| 3186 |
for cls in [MutationDeleteElementPath, MutationClearElementPath]: |
| 3187 |
with self.subTest(cls): |
| 3188 |
e = ET.Element('foo') |
| 3189 |
e.extend([ET.Element('bar')]) |
| 3190 |
e.find(cls(e, 'x')) |
| 3191 |
|
| 3192 |
def test_find_with_error(self): |
| 3193 |
e = ET.Element('foo') |
| 3194 |
e.extend([ET.Element('bar')]) |
| 3195 |
try: |
| 3196 |
e.find(BadElementPath('x')) |
| 3197 |
except ZeroDivisionError: |
| 3198 |
pass |
| 3199 |
|
| 3200 |
def test_findtext_with_mutating(self): |
| 3201 |
for cls in [MutationDeleteElementPath, MutationClearElementPath]: |
| 3202 |
with self.subTest(cls): |
| 3203 |
e = ET.Element('foo') |
| 3204 |
e.extend([ET.Element('bar')]) |
| 3205 |
e.findtext(cls(e, 'x')) |
| 3206 |
|
| 3207 |
def test_findtext_with_mutating_non_none_text(self): |
| 3208 |
for cls in [MutationDeleteElementPath, MutationClearElementPath]: |
| 3209 |
with self.subTest(cls): |
| 3210 |
e = ET.Element('foo') |
| 3211 |
child = ET.Element('bar') |
| 3212 |
child.text = str(object()) |
| 3213 |
e.append(child) |
| 3214 |
del child |
| 3215 |
repr(e.findtext(cls(e, 'x'))) |
| 3216 |
|
| 3217 |
def test_findtext_with_error(self): |
| 3218 |
e = ET.Element('foo') |
| 3219 |
e.extend([ET.Element('bar')]) |
| 3220 |
try: |
| 3221 |
e.findtext(BadElementPath('x')) |
| 3222 |
except ZeroDivisionError: |
| 3223 |
pass |
| 3224 |
|
| 3225 |
def test_findtext_with_falsey_text_attribute(self): |
| 3226 |
root_elem = ET.Element('foo') |
| 3227 |
sub_elem = ET.SubElement(root_elem, 'bar') |
| 3228 |
falsey = ["", 0, False, [], (), {}] |
| 3229 |
for val in falsey: |
| 3230 |
sub_elem.text = val |
| 3231 |
self.assertEqual(root_elem.findtext('./bar'), val) |
| 3232 |
|
| 3233 |
def test_findtext_with_none_text_attribute(self): |
| 3234 |
root_elem = ET.Element('foo') |
| 3235 |
sub_elem = ET.SubElement(root_elem, 'bar') |
| 3236 |
sub_elem.text = None |
| 3237 |
self.assertEqual(root_elem.findtext('./bar'), '') |
| 3238 |
|
| 3239 |
def test_findall_with_mutating(self): |
| 3240 |
for cls in [MutationDeleteElementPath, MutationClearElementPath]: |
| 3241 |
with self.subTest(cls): |
| 3242 |
e = ET.Element('foo') |
| 3243 |
e.extend([ET.Element('bar')]) |
| 3244 |
e.findall(cls(e, 'x')) |
| 3245 |
|
| 3246 |
def test_findall_with_error(self): |
| 3247 |
e = ET.Element('foo') |
| 3248 |
e.extend([ET.Element('bar')]) |
| 3249 |
try: |
| 3250 |
e.findall(BadElementPath('x')) |
| 3251 |
except ZeroDivisionError: |
| 3252 |
pass |
| 3253 |
|
| 3254 |
|
| 3255 |
class ElementTreeTypeTest(unittest.TestCase): |
| 3256 |
def test_istype(self): |
| 3257 |
self.assertIsInstance(ET.ParseError, type) |
| 3258 |
self.assertIsInstance(ET.QName, type) |
| 3259 |
self.assertIsInstance(ET.ElementTree, type) |
| 3260 |
self.assertIsInstance(ET.Element, type) |
| 3261 |
self.assertIsInstance(ET.TreeBuilder, type) |
| 3262 |
self.assertIsInstance(ET.XMLParser, type) |
| 3263 |
|
| 3264 |
def test_Element_subclass_trivial(self): |
| 3265 |
class MyElement(ET.Element): |
| 3266 |
pass |
| 3267 |
|
| 3268 |
mye = MyElement('foo') |
| 3269 |
self.assertIsInstance(mye, ET.Element) |
| 3270 |
self.assertIsInstance(mye, MyElement) |
| 3271 |
self.assertEqual(mye.tag, 'foo') |
| 3272 |
|
| 3273 |
# test that attribute assignment works (issue 14849) |
| 3274 |
mye.text = "joe" |
| 3275 |
self.assertEqual(mye.text, "joe") |
| 3276 |
|
| 3277 |
def test_Element_subclass_constructor(self): |
| 3278 |
class MyElement(ET.Element): |
| 3279 |
def __init__(self, tag, attrib={}, **extra): |
| 3280 |
super(MyElement, self).__init__(tag + '__', attrib, **extra) |
| 3281 |
|
| 3282 |
mye = MyElement('foo', {'a': 1, 'b': 2}, c=3, d=4) |
| 3283 |
self.assertEqual(mye.tag, 'foo__') |
| 3284 |
self.assertEqual(sorted(mye.items()), |
| 3285 |
[('a', 1), ('b', 2), ('c', 3), ('d', 4)]) |
| 3286 |
|
| 3287 |
def test_Element_subclass_new_method(self): |
| 3288 |
class MyElement(ET.Element): |
| 3289 |
def newmethod(self): |
| 3290 |
return self.tag |
| 3291 |
|
| 3292 |
mye = MyElement('joe') |
| 3293 |
self.assertEqual(mye.newmethod(), 'joe') |
| 3294 |
|
| 3295 |
def test_Element_subclass_find(self): |
| 3296 |
class MyElement(ET.Element): |
| 3297 |
pass |
| 3298 |
|
| 3299 |
e = ET.Element('foo') |
| 3300 |
e.text = 'text' |
| 3301 |
sub = MyElement('bar') |
| 3302 |
sub.text = 'subtext' |
| 3303 |
e.append(sub) |
| 3304 |
self.assertEqual(e.findtext('bar'), 'subtext') |
| 3305 |
self.assertEqual(e.find('bar').tag, 'bar') |
| 3306 |
found = list(e.findall('bar')) |
| 3307 |
self.assertEqual(len(found), 1, found) |
| 3308 |
self.assertEqual(found[0].tag, 'bar') |
| 3309 |
|
| 3310 |
|
| 3311 |
class ElementFindTest(unittest.TestCase): |
| 3312 |
def test_find_simple(self): |
| 3313 |
e = ET.XML(SAMPLE_XML) |
| 3314 |
self.assertEqual(e.find('tag').tag, 'tag') |
| 3315 |
self.assertEqual(e.find('section/tag').tag, 'tag') |
| 3316 |
self.assertEqual(e.find('./tag').tag, 'tag') |
| 3317 |
|
| 3318 |
e[2] = ET.XML(SAMPLE_SECTION) |
| 3319 |
self.assertEqual(e.find('section/nexttag').tag, 'nexttag') |
| 3320 |
|
| 3321 |
self.assertEqual(e.findtext('./tag'), 'text') |
| 3322 |
self.assertEqual(e.findtext('section/tag'), 'subtext') |
| 3323 |
|
| 3324 |
# section/nexttag is found but has no text |
| 3325 |
self.assertEqual(e.findtext('section/nexttag'), '') |
| 3326 |
self.assertEqual(e.findtext('section/nexttag', 'default'), '') |
| 3327 |
|
| 3328 |
# tog doesn't exist and 'default' kicks in |
| 3329 |
self.assertIsNone(e.findtext('tog')) |
| 3330 |
self.assertEqual(e.findtext('tog', 'default'), 'default') |
| 3331 |
|
| 3332 |
# Issue #16922 |
| 3333 |
self.assertEqual(ET.XML('<tag><empty /></tag>').findtext('empty'), '') |
| 3334 |
|
| 3335 |
def test_find_xpath(self): |
| 3336 |
LINEAR_XML = ''' |
| 3337 |
<body> |
| 3338 |
<tag class='a'/> |
| 3339 |
<tag class='b'/> |
| 3340 |
<tag class='c'/> |
| 3341 |
<tag class='d'/> |
| 3342 |
</body>''' |
| 3343 |
e = ET.XML(LINEAR_XML) |
| 3344 |
|
| 3345 |
# Test for numeric indexing and last() |
| 3346 |
self.assertEqual(e.find('./tag[1]').attrib['class'], 'a') |
| 3347 |
self.assertEqual(e.find('./tag[2]').attrib['class'], 'b') |
| 3348 |
self.assertEqual(e.find('./tag[last()]').attrib['class'], 'd') |
| 3349 |
self.assertEqual(e.find('./tag[last()-1]').attrib['class'], 'c') |
| 3350 |
self.assertEqual(e.find('./tag[last()-2]').attrib['class'], 'b') |
| 3351 |
|
| 3352 |
self.assertRaisesRegex(SyntaxError, 'XPath', e.find, './tag[0]') |
| 3353 |
self.assertRaisesRegex(SyntaxError, 'XPath', e.find, './tag[-1]') |
| 3354 |
self.assertRaisesRegex(SyntaxError, 'XPath', e.find, './tag[last()-0]') |
| 3355 |
self.assertRaisesRegex(SyntaxError, 'XPath', e.find, './tag[last()+1]') |
| 3356 |
|
| 3357 |
def test_findall(self): |
| 3358 |
e = ET.XML(SAMPLE_XML) |
| 3359 |
e[2] = ET.XML(SAMPLE_SECTION) |
| 3360 |
self.assertEqual(summarize_list(e.findall('.')), ['body']) |
| 3361 |
self.assertEqual(summarize_list(e.findall('tag')), ['tag', 'tag']) |
| 3362 |
self.assertEqual(summarize_list(e.findall('tog')), []) |
| 3363 |
self.assertEqual(summarize_list(e.findall('tog/foo')), []) |
| 3364 |
self.assertEqual(summarize_list(e.findall('*')), |
| 3365 |
['tag', 'tag', 'section']) |
| 3366 |
self.assertEqual(summarize_list(e.findall('.//tag')), |
| 3367 |
['tag'] * 4) |
| 3368 |
self.assertEqual(summarize_list(e.findall('section/tag')), ['tag']) |
| 3369 |
self.assertEqual(summarize_list(e.findall('section//tag')), ['tag'] * 2) |
| 3370 |
self.assertEqual(summarize_list(e.findall('section/*')), |
| 3371 |
['tag', 'nexttag', 'nextsection']) |
| 3372 |
self.assertEqual(summarize_list(e.findall('section//*')), |
| 3373 |
['tag', 'nexttag', 'nextsection', 'tag']) |
| 3374 |
self.assertEqual(summarize_list(e.findall('section/.//*')), |
| 3375 |
['tag', 'nexttag', 'nextsection', 'tag']) |
| 3376 |
self.assertEqual(summarize_list(e.findall('*/*')), |
| 3377 |
['tag', 'nexttag', 'nextsection']) |
| 3378 |
self.assertEqual(summarize_list(e.findall('*//*')), |
| 3379 |
['tag', 'nexttag', 'nextsection', 'tag']) |
| 3380 |
self.assertEqual(summarize_list(e.findall('*/tag')), ['tag']) |
| 3381 |
self.assertEqual(summarize_list(e.findall('*/./tag')), ['tag']) |
| 3382 |
self.assertEqual(summarize_list(e.findall('./tag')), ['tag'] * 2) |
| 3383 |
self.assertEqual(summarize_list(e.findall('././tag')), ['tag'] * 2) |
| 3384 |
|
| 3385 |
self.assertEqual(summarize_list(e.findall('.//tag[@class]')), |
| 3386 |
['tag'] * 3) |
| 3387 |
self.assertEqual(summarize_list(e.findall('.//tag[@class="a"]')), |
| 3388 |
['tag']) |
| 3389 |
self.assertEqual(summarize_list(e.findall('.//tag[@class!="a"]')), |
| 3390 |
['tag'] * 2) |
| 3391 |
self.assertEqual(summarize_list(e.findall('.//tag[@class="b"]')), |
| 3392 |
['tag'] * 2) |
| 3393 |
self.assertEqual(summarize_list(e.findall('.//tag[@class!="b"]')), |
| 3394 |
['tag']) |
| 3395 |
self.assertEqual(summarize_list(e.findall('.//tag[@id]')), |
| 3396 |
['tag']) |
| 3397 |
self.assertEqual(summarize_list(e.findall('.//section[tag]')), |
| 3398 |
['section']) |
| 3399 |
self.assertEqual(summarize_list(e.findall('.//section[element]')), []) |
| 3400 |
self.assertEqual(summarize_list(e.findall('../tag')), []) |
| 3401 |
self.assertEqual(summarize_list(e.findall('section/../tag')), |
| 3402 |
['tag'] * 2) |
| 3403 |
self.assertEqual(e.findall('section//'), e.findall('section//*')) |
| 3404 |
|
| 3405 |
self.assertEqual(summarize_list(e.findall(".//section[tag='subtext']")), |
| 3406 |
['section']) |
| 3407 |
self.assertEqual(summarize_list(e.findall(".//section[tag ='subtext']")), |
| 3408 |
['section']) |
| 3409 |
self.assertEqual(summarize_list(e.findall(".//section[tag= 'subtext']")), |
| 3410 |
['section']) |
| 3411 |
self.assertEqual(summarize_list(e.findall(".//section[tag = 'subtext']")), |
| 3412 |
['section']) |
| 3413 |
self.assertEqual(summarize_list(e.findall(".//section[ tag = 'subtext' ]")), |
| 3414 |
['section']) |
| 3415 |
|
| 3416 |
# Negations of above tests. They match nothing because the sole section |
| 3417 |
# tag has subtext. |
| 3418 |
self.assertEqual(summarize_list(e.findall(".//section[tag!='subtext']")), |
| 3419 |
[]) |
| 3420 |
self.assertEqual(summarize_list(e.findall(".//section[tag !='subtext']")), |
| 3421 |
[]) |
| 3422 |
self.assertEqual(summarize_list(e.findall(".//section[tag!= 'subtext']")), |
| 3423 |
[]) |
| 3424 |
self.assertEqual(summarize_list(e.findall(".//section[tag != 'subtext']")), |
| 3425 |
[]) |
| 3426 |
self.assertEqual(summarize_list(e.findall(".//section[ tag != 'subtext' ]")), |
| 3427 |
[]) |
| 3428 |
|
| 3429 |
self.assertEqual(summarize_list(e.findall(".//tag[.='subtext']")), |
| 3430 |
['tag']) |
| 3431 |
self.assertEqual(summarize_list(e.findall(".//tag[. ='subtext']")), |
| 3432 |
['tag']) |
| 3433 |
self.assertEqual(summarize_list(e.findall('.//tag[.= "subtext"]')), |
| 3434 |
['tag']) |
| 3435 |
self.assertEqual(summarize_list(e.findall('.//tag[ . = "subtext" ]')), |
| 3436 |
['tag']) |
| 3437 |
self.assertEqual(summarize_list(e.findall(".//tag[. = 'subtext']")), |
| 3438 |
['tag']) |
| 3439 |
self.assertEqual(summarize_list(e.findall(".//tag[. = 'subtext ']")), |
| 3440 |
[]) |
| 3441 |
self.assertEqual(summarize_list(e.findall(".//tag[.= ' subtext']")), |
| 3442 |
[]) |
| 3443 |
|
| 3444 |
# Negations of above tests. |
| 3445 |
# Matches everything but the tag containing subtext |
| 3446 |
self.assertEqual(summarize_list(e.findall(".//tag[.!='subtext']")), |
| 3447 |
['tag'] * 3) |
| 3448 |
self.assertEqual(summarize_list(e.findall(".//tag[. !='subtext']")), |
| 3449 |
['tag'] * 3) |
| 3450 |
self.assertEqual(summarize_list(e.findall('.//tag[.!= "subtext"]')), |
| 3451 |
['tag'] * 3) |
| 3452 |
self.assertEqual(summarize_list(e.findall('.//tag[ . != "subtext" ]')), |
| 3453 |
['tag'] * 3) |
| 3454 |
self.assertEqual(summarize_list(e.findall(".//tag[. != 'subtext']")), |
| 3455 |
['tag'] * 3) |
| 3456 |
# Matches all tags. |
| 3457 |
self.assertEqual(summarize_list(e.findall(".//tag[. != 'subtext ']")), |
| 3458 |
['tag'] * 4) |
| 3459 |
self.assertEqual(summarize_list(e.findall(".//tag[.!= ' subtext']")), |
| 3460 |
['tag'] * 4) |
| 3461 |
|
| 3462 |
# duplicate section => 2x tag matches |
| 3463 |
e[1] = e[2] |
| 3464 |
self.assertEqual(summarize_list(e.findall(".//section[tag = 'subtext']")), |
| 3465 |
['section', 'section']) |
| 3466 |
self.assertEqual(summarize_list(e.findall(".//tag[. = 'subtext']")), |
| 3467 |
['tag', 'tag']) |
| 3468 |
|
| 3469 |
def test_test_find_with_ns(self): |
| 3470 |
e = ET.XML(SAMPLE_XML_NS) |
| 3471 |
self.assertEqual(summarize_list(e.findall('tag')), []) |
| 3472 |
self.assertEqual( |
| 3473 |
summarize_list(e.findall("{http://effbot.org/ns}tag")), |
| 3474 |
['{http://effbot.org/ns}tag'] * 2) |
| 3475 |
self.assertEqual( |
| 3476 |
summarize_list(e.findall(".//{http://effbot.org/ns}tag")), |
| 3477 |
['{http://effbot.org/ns}tag'] * 3) |
| 3478 |
|
| 3479 |
def test_findall_different_nsmaps(self): |
| 3480 |
root = ET.XML(''' |
| 3481 |
<a xmlns:x="X" xmlns:y="Y"> |
| 3482 |
<x:b><c/></x:b> |
| 3483 |
<b/> |
| 3484 |
<c><x:b/><b/></c><y:b/> |
| 3485 |
</a>''') |
| 3486 |
nsmap = {'xx': 'X'} |
| 3487 |
self.assertEqual(len(root.findall(".//xx:b", namespaces=nsmap)), 2) |
| 3488 |
self.assertEqual(len(root.findall(".//b", namespaces=nsmap)), 2) |
| 3489 |
nsmap = {'xx': 'Y'} |
| 3490 |
self.assertEqual(len(root.findall(".//xx:b", namespaces=nsmap)), 1) |
| 3491 |
self.assertEqual(len(root.findall(".//b", namespaces=nsmap)), 2) |
| 3492 |
nsmap = {'xx': 'X', '': 'Y'} |
| 3493 |
self.assertEqual(len(root.findall(".//xx:b", namespaces=nsmap)), 2) |
| 3494 |
self.assertEqual(len(root.findall(".//b", namespaces=nsmap)), 1) |
| 3495 |
|
| 3496 |
def test_findall_wildcard(self): |
| 3497 |
root = ET.XML(''' |
| 3498 |
<a xmlns:x="X" xmlns:y="Y"> |
| 3499 |
<x:b><c/></x:b> |
| 3500 |
<b/> |
| 3501 |
<c><x:b/><b/></c><y:b/> |
| 3502 |
</a>''') |
| 3503 |
root.append(ET.Comment('test')) |
| 3504 |
|
| 3505 |
self.assertEqual(summarize_list(root.findall("{*}b")), |
| 3506 |
['{X}b', 'b', '{Y}b']) |
| 3507 |
self.assertEqual(summarize_list(root.findall("{*}c")), |
| 3508 |
['c']) |
| 3509 |
self.assertEqual(summarize_list(root.findall("{X}*")), |
| 3510 |
['{X}b']) |
| 3511 |
self.assertEqual(summarize_list(root.findall("{Y}*")), |
| 3512 |
['{Y}b']) |
| 3513 |
self.assertEqual(summarize_list(root.findall("{}*")), |
| 3514 |
['b', 'c']) |
| 3515 |
self.assertEqual(summarize_list(root.findall("{}b")), # only for consistency |
| 3516 |
['b']) |
| 3517 |
self.assertEqual(summarize_list(root.findall("{}b")), |
| 3518 |
summarize_list(root.findall("b"))) |
| 3519 |
self.assertEqual(summarize_list(root.findall("{*}*")), |
| 3520 |
['{X}b', 'b', 'c', '{Y}b']) |
| 3521 |
# This is an unfortunate difference, but that's how find('*') works. |
| 3522 |
self.assertEqual(summarize_list(root.findall("{*}*") + [root[-1]]), |
| 3523 |
summarize_list(root.findall("*"))) |
| 3524 |
|
| 3525 |
self.assertEqual(summarize_list(root.findall(".//{*}b")), |
| 3526 |
['{X}b', 'b', '{X}b', 'b', '{Y}b']) |
| 3527 |
self.assertEqual(summarize_list(root.findall(".//{*}c")), |
| 3528 |
['c', 'c']) |
| 3529 |
self.assertEqual(summarize_list(root.findall(".//{X}*")), |
| 3530 |
['{X}b', '{X}b']) |
| 3531 |
self.assertEqual(summarize_list(root.findall(".//{Y}*")), |
| 3532 |
['{Y}b']) |
| 3533 |
self.assertEqual(summarize_list(root.findall(".//{}*")), |
| 3534 |
['c', 'b', 'c', 'b']) |
| 3535 |
self.assertEqual(summarize_list(root.findall(".//{}b")), # only for consistency |
| 3536 |
['b', 'b']) |
| 3537 |
self.assertEqual(summarize_list(root.findall(".//{}b")), |
| 3538 |
summarize_list(root.findall(".//b"))) |
| 3539 |
|
| 3540 |
def test_bad_find(self): |
| 3541 |
e = ET.XML(SAMPLE_XML) |
| 3542 |
with self.assertRaisesRegex(SyntaxError, 'cannot use absolute path'): |
| 3543 |
e.findall('/tag') |
| 3544 |
|
| 3545 |
def test_find_through_ElementTree(self): |
| 3546 |
e = ET.XML(SAMPLE_XML) |
| 3547 |
self.assertEqual(ET.ElementTree(e).find('tag').tag, 'tag') |
| 3548 |
self.assertEqual(ET.ElementTree(e).findtext('tag'), 'text') |
| 3549 |
self.assertEqual(summarize_list(ET.ElementTree(e).findall('tag')), |
| 3550 |
['tag'] * 2) |
| 3551 |
# this produces a warning |
| 3552 |
msg = ("This search is broken in 1.3 and earlier, and will be fixed " |
| 3553 |
"in a future version. If you rely on the current behaviour, " |
| 3554 |
"change it to '.+'") |
| 3555 |
with self.assertWarnsRegex(FutureWarning, msg): |
| 3556 |
it = ET.ElementTree(e).findall('//tag') |
| 3557 |
self.assertEqual(summarize_list(it), ['tag'] * 3) |
| 3558 |
|
| 3559 |
|
| 3560 |
class ElementIterTest(unittest.TestCase): |
| 3561 |
def _ilist(self, elem, tag=None): |
| 3562 |
return summarize_list(elem.iter(tag)) |
| 3563 |
|
| 3564 |
def test_basic(self): |
| 3565 |
doc = ET.XML("<html><body>this is a <i>paragraph</i>.</body>..</html>") |
| 3566 |
self.assertEqual(self._ilist(doc), ['html', 'body', 'i']) |
| 3567 |
self.assertEqual(self._ilist(doc.find('body')), ['body', 'i']) |
| 3568 |
self.assertEqual(next(doc.iter()).tag, 'html') |
| 3569 |
self.assertEqual(''.join(doc.itertext()), 'this is a paragraph...') |
| 3570 |
self.assertEqual(''.join(doc.find('body').itertext()), |
| 3571 |
'this is a paragraph.') |
| 3572 |
self.assertEqual(next(doc.itertext()), 'this is a ') |
| 3573 |
|
| 3574 |
# iterparse should return an iterator |
| 3575 |
sourcefile = serialize(doc, to_string=False) |
| 3576 |
self.assertEqual(next(ET.iterparse(sourcefile))[0], 'end') |
| 3577 |
|
| 3578 |
# With an explicit parser too (issue #9708) |
| 3579 |
sourcefile = serialize(doc, to_string=False) |
| 3580 |
parser = ET.XMLParser(target=ET.TreeBuilder()) |
| 3581 |
self.assertEqual(next(ET.iterparse(sourcefile, parser=parser))[0], 'end') |
| 3582 |
|
| 3583 |
tree = ET.ElementTree(None) |
| 3584 |
self.assertRaises(AttributeError, tree.iter) |
| 3585 |
|
| 3586 |
# Issue #16913 |
| 3587 |
doc = ET.XML("<root>a&<sub>b&</sub>c&</root>") |
| 3588 |
self.assertEqual(''.join(doc.itertext()), 'a&b&c&') |
| 3589 |
|
| 3590 |
def test_comment(self): |
| 3591 |
e = ET.Element('root') |
| 3592 |
e.text = 'before' |
| 3593 |
comment = ET.Comment('comment') |
| 3594 |
self.assertEqual(comment.text, 'comment') |
| 3595 |
comment.tail = 'after' |
| 3596 |
e.append(comment) |
| 3597 |
self.assertEqual(''.join(e.itertext()), 'beforeafter') |
| 3598 |
self.assertEqual(list(e.iter()), [e, comment]) |
| 3599 |
self.assertEqual(list(e.iter('root')), [e]) |
| 3600 |
self.assertEqual(''.join(comment.itertext()), '') |
| 3601 |
self.assertEqual(list(comment.iter()), [comment]) |
| 3602 |
|
| 3603 |
def test_processinginstruction(self): |
| 3604 |
e = ET.Element('root') |
| 3605 |
e.text = 'before' |
| 3606 |
pi = ET.PI('test', 'instruction') |
| 3607 |
self.assertEqual(pi.text, 'test instruction') |
| 3608 |
pi.tail = 'after' |
| 3609 |
e.append(pi) |
| 3610 |
self.assertEqual(''.join(e.itertext()), 'beforeafter') |
| 3611 |
self.assertEqual(list(e.iter()), [e, pi]) |
| 3612 |
self.assertEqual(list(e.iter('root')), [e]) |
| 3613 |
self.assertEqual(''.join(pi.itertext()), '') |
| 3614 |
self.assertEqual(list(pi.iter()), [pi]) |
| 3615 |
|
| 3616 |
def test_corners(self): |
| 3617 |
# single root, no subelements |
| 3618 |
a = ET.Element('a') |
| 3619 |
self.assertEqual(self._ilist(a), ['a']) |
| 3620 |
|
| 3621 |
# one child |
| 3622 |
b = ET.SubElement(a, 'b') |
| 3623 |
self.assertEqual(self._ilist(a), ['a', 'b']) |
| 3624 |
|
| 3625 |
# one child and one grandchild |
| 3626 |
c = ET.SubElement(b, 'c') |
| 3627 |
self.assertEqual(self._ilist(a), ['a', 'b', 'c']) |
| 3628 |
|
| 3629 |
# two children, only first with grandchild |
| 3630 |
d = ET.SubElement(a, 'd') |
| 3631 |
self.assertEqual(self._ilist(a), ['a', 'b', 'c', 'd']) |
| 3632 |
|
| 3633 |
# replace first child by second |
| 3634 |
a[0] = a[1] |
| 3635 |
del a[1] |
| 3636 |
self.assertEqual(self._ilist(a), ['a', 'd']) |
| 3637 |
|
| 3638 |
def test_iter_by_tag(self): |
| 3639 |
doc = ET.XML(''' |
| 3640 |
<document> |
| 3641 |
<house> |
| 3642 |
<room>bedroom1</room> |
| 3643 |
<room>bedroom2</room> |
| 3644 |
</house> |
| 3645 |
<shed>nothing here |
| 3646 |
</shed> |
| 3647 |
<house> |
| 3648 |
<room>bedroom8</room> |
| 3649 |
</house> |
| 3650 |
</document>''') |
| 3651 |
|
| 3652 |
self.assertEqual(self._ilist(doc, 'room'), ['room'] * 3) |
| 3653 |
self.assertEqual(self._ilist(doc, 'house'), ['house'] * 2) |
| 3654 |
|
| 3655 |
# test that iter also accepts 'tag' as a keyword arg |
| 3656 |
self.assertEqual( |
| 3657 |
summarize_list(doc.iter(tag='room')), |
| 3658 |
['room'] * 3) |
| 3659 |
|
| 3660 |
# make sure both tag=None and tag='*' return all tags |
| 3661 |
all_tags = ['document', 'house', 'room', 'room', |
| 3662 |
'shed', 'house', 'room'] |
| 3663 |
self.assertEqual(summarize_list(doc.iter()), all_tags) |
| 3664 |
self.assertEqual(self._ilist(doc), all_tags) |
| 3665 |
self.assertEqual(self._ilist(doc, '*'), all_tags) |
| 3666 |
|
| 3667 |
def test_copy(self): |
| 3668 |
a = ET.Element('a') |
| 3669 |
it = a.iter() |
| 3670 |
with self.assertRaises(TypeError): |
| 3671 |
copy.copy(it) |
| 3672 |
|
| 3673 |
def test_pickle(self): |
| 3674 |
a = ET.Element('a') |
| 3675 |
it = a.iter() |
| 3676 |
for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
| 3677 |
with self.assertRaises((TypeError, pickle.PicklingError)): |
| 3678 |
pickle.dumps(it, proto) |
| 3679 |
|
| 3680 |
|
| 3681 |
class TreeBuilderTest(unittest.TestCase): |
| 3682 |
sample1 = ('<!DOCTYPE html PUBLIC' |
| 3683 |
' "-//W3C//DTD XHTML 1.0 Transitional//EN"' |
| 3684 |
' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">' |
| 3685 |
'<html>text<div>subtext</div>tail</html>') |
| 3686 |
|
| 3687 |
sample2 = '''<toplevel>sometext</toplevel>''' |
| 3688 |
|
| 3689 |
def _check_sample1_element(self, e): |
| 3690 |
self.assertEqual(e.tag, 'html') |
| 3691 |
self.assertEqual(e.text, 'text') |
| 3692 |
self.assertEqual(e.tail, None) |
| 3693 |
self.assertEqual(e.attrib, {}) |
| 3694 |
children = list(e) |
| 3695 |
self.assertEqual(len(children), 1) |
| 3696 |
child = children[0] |
| 3697 |
self.assertEqual(child.tag, 'div') |
| 3698 |
self.assertEqual(child.text, 'subtext') |
| 3699 |
self.assertEqual(child.tail, 'tail') |
| 3700 |
self.assertEqual(child.attrib, {}) |
| 3701 |
|
| 3702 |
def test_dummy_builder(self): |
| 3703 |
class BaseDummyBuilder: |
| 3704 |
def close(self): |
| 3705 |
return 42 |
| 3706 |
|
| 3707 |
class DummyBuilder(BaseDummyBuilder): |
| 3708 |
data = start = end = lambda *a: None |
| 3709 |
|
| 3710 |
parser = ET.XMLParser(target=DummyBuilder()) |
| 3711 |
parser.feed(self.sample1) |
| 3712 |
self.assertEqual(parser.close(), 42) |
| 3713 |
|
| 3714 |
parser = ET.XMLParser(target=BaseDummyBuilder()) |
| 3715 |
parser.feed(self.sample1) |
| 3716 |
self.assertEqual(parser.close(), 42) |
| 3717 |
|
| 3718 |
parser = ET.XMLParser(target=object()) |
| 3719 |
parser.feed(self.sample1) |
| 3720 |
self.assertIsNone(parser.close()) |
| 3721 |
|
| 3722 |
def test_treebuilder_comment(self): |
| 3723 |
b = ET.TreeBuilder() |
| 3724 |
self.assertEqual(b.comment('ctext').tag, ET.Comment) |
| 3725 |
self.assertEqual(b.comment('ctext').text, 'ctext') |
| 3726 |
|
| 3727 |
b = ET.TreeBuilder(comment_factory=ET.Comment) |
| 3728 |
self.assertEqual(b.comment('ctext').tag, ET.Comment) |
| 3729 |
self.assertEqual(b.comment('ctext').text, 'ctext') |
| 3730 |
|
| 3731 |
b = ET.TreeBuilder(comment_factory=len) |
| 3732 |
self.assertEqual(b.comment('ctext'), len('ctext')) |
| 3733 |
|
| 3734 |
def test_treebuilder_pi(self): |
| 3735 |
b = ET.TreeBuilder() |
| 3736 |
self.assertEqual(b.pi('target', None).tag, ET.PI) |
| 3737 |
self.assertEqual(b.pi('target', None).text, 'target') |
| 3738 |
|
| 3739 |
b = ET.TreeBuilder(pi_factory=ET.PI) |
| 3740 |
self.assertEqual(b.pi('target').tag, ET.PI) |
| 3741 |
self.assertEqual(b.pi('target').text, "target") |
| 3742 |
self.assertEqual(b.pi('pitarget', ' text ').tag, ET.PI) |
| 3743 |
self.assertEqual(b.pi('pitarget', ' text ').text, "pitarget text ") |
| 3744 |
|
| 3745 |
b = ET.TreeBuilder(pi_factory=lambda target, text: (len(target), text)) |
| 3746 |
self.assertEqual(b.pi('target'), (len('target'), None)) |
| 3747 |
self.assertEqual(b.pi('pitarget', ' text '), (len('pitarget'), ' text ')) |
| 3748 |
|
| 3749 |
def test_late_tail(self): |
| 3750 |
# Issue #37399: The tail of an ignored comment could overwrite the text before it. |
| 3751 |
class TreeBuilderSubclass(ET.TreeBuilder): |
| 3752 |
pass |
| 3753 |
|
| 3754 |
xml = "<a>text<!-- comment -->tail</a>" |
| 3755 |
a = ET.fromstring(xml) |
| 3756 |
self.assertEqual(a.text, "texttail") |
| 3757 |
|
| 3758 |
parser = ET.XMLParser(target=TreeBuilderSubclass()) |
| 3759 |
parser.feed(xml) |
| 3760 |
a = parser.close() |
| 3761 |
self.assertEqual(a.text, "texttail") |
| 3762 |
|
| 3763 |
xml = "<a>text<?pi data?>tail</a>" |
| 3764 |
a = ET.fromstring(xml) |
| 3765 |
self.assertEqual(a.text, "texttail") |
| 3766 |
|
| 3767 |
xml = "<a>text<?pi data?>tail</a>" |
| 3768 |
parser = ET.XMLParser(target=TreeBuilderSubclass()) |
| 3769 |
parser.feed(xml) |
| 3770 |
a = parser.close() |
| 3771 |
self.assertEqual(a.text, "texttail") |
| 3772 |
|
| 3773 |
def test_late_tail_mix_pi_comments(self): |
| 3774 |
# Issue #37399: The tail of an ignored comment could overwrite the text before it. |
| 3775 |
# Test appending tails to comments/pis. |
| 3776 |
class TreeBuilderSubclass(ET.TreeBuilder): |
| 3777 |
pass |
| 3778 |
|
| 3779 |
xml = "<a>text<?pi1?> <!-- comment -->\n<?pi2?>tail</a>" |
| 3780 |
parser = ET.XMLParser(target=ET.TreeBuilder(insert_comments=True)) |
| 3781 |
parser.feed(xml) |
| 3782 |
a = parser.close() |
| 3783 |
self.assertEqual(a[0].text, ' comment ') |
| 3784 |
self.assertEqual(a[0].tail, '\ntail') |
| 3785 |
self.assertEqual(a.text, "text ") |
| 3786 |
|
| 3787 |
parser = ET.XMLParser(target=TreeBuilderSubclass(insert_comments=True)) |
| 3788 |
parser.feed(xml) |
| 3789 |
a = parser.close() |
| 3790 |
self.assertEqual(a[0].text, ' comment ') |
| 3791 |
self.assertEqual(a[0].tail, '\ntail') |
| 3792 |
self.assertEqual(a.text, "text ") |
| 3793 |
|
| 3794 |
xml = "<a>text<!-- comment -->\n<?pi data?>tail</a>" |
| 3795 |
parser = ET.XMLParser(target=ET.TreeBuilder(insert_pis=True)) |
| 3796 |
parser.feed(xml) |
| 3797 |
a = parser.close() |
| 3798 |
self.assertEqual(a[0].text, 'pi data') |
| 3799 |
self.assertEqual(a[0].tail, 'tail') |
| 3800 |
self.assertEqual(a.text, "text\n") |
| 3801 |
|
| 3802 |
parser = ET.XMLParser(target=TreeBuilderSubclass(insert_pis=True)) |
| 3803 |
parser.feed(xml) |
| 3804 |
a = parser.close() |
| 3805 |
self.assertEqual(a[0].text, 'pi data') |
| 3806 |
self.assertEqual(a[0].tail, 'tail') |
| 3807 |
self.assertEqual(a.text, "text\n") |
| 3808 |
|
| 3809 |
def test_treebuilder_elementfactory_none(self): |
| 3810 |
parser = ET.XMLParser(target=ET.TreeBuilder(element_factory=None)) |
| 3811 |
parser.feed(self.sample1) |
| 3812 |
e = parser.close() |
| 3813 |
self._check_sample1_element(e) |
| 3814 |
|
| 3815 |
def test_subclass(self): |
| 3816 |
class MyTreeBuilder(ET.TreeBuilder): |
| 3817 |
def foobar(self, x): |
| 3818 |
return x * 2 |
| 3819 |
|
| 3820 |
tb = MyTreeBuilder() |
| 3821 |
self.assertEqual(tb.foobar(10), 20) |
| 3822 |
|
| 3823 |
parser = ET.XMLParser(target=tb) |
| 3824 |
parser.feed(self.sample1) |
| 3825 |
|
| 3826 |
e = parser.close() |
| 3827 |
self._check_sample1_element(e) |
| 3828 |
|
| 3829 |
def test_subclass_comment_pi(self): |
| 3830 |
class MyTreeBuilder(ET.TreeBuilder): |
| 3831 |
def foobar(self, x): |
| 3832 |
return x * 2 |
| 3833 |
|
| 3834 |
tb = MyTreeBuilder(comment_factory=ET.Comment, pi_factory=ET.PI) |
| 3835 |
self.assertEqual(tb.foobar(10), 20) |
| 3836 |
|
| 3837 |
parser = ET.XMLParser(target=tb) |
| 3838 |
parser.feed(self.sample1) |
| 3839 |
parser.feed('<!-- a comment--><?and a pi?>') |
| 3840 |
|
| 3841 |
e = parser.close() |
| 3842 |
self._check_sample1_element(e) |
| 3843 |
|
| 3844 |
def test_element_factory(self): |
| 3845 |
lst = [] |
| 3846 |
def myfactory(tag, attrib): |
| 3847 |
nonlocal lst |
| 3848 |
lst.append(tag) |
| 3849 |
return ET.Element(tag, attrib) |
| 3850 |
|
| 3851 |
tb = ET.TreeBuilder(element_factory=myfactory) |
| 3852 |
parser = ET.XMLParser(target=tb) |
| 3853 |
parser.feed(self.sample2) |
| 3854 |
parser.close() |
| 3855 |
|
| 3856 |
self.assertEqual(lst, ['toplevel']) |
| 3857 |
|
| 3858 |
def _check_element_factory_class(self, cls): |
| 3859 |
tb = ET.TreeBuilder(element_factory=cls) |
| 3860 |
|
| 3861 |
parser = ET.XMLParser(target=tb) |
| 3862 |
parser.feed(self.sample1) |
| 3863 |
e = parser.close() |
| 3864 |
self.assertIsInstance(e, cls) |
| 3865 |
self._check_sample1_element(e) |
| 3866 |
|
| 3867 |
def test_element_factory_subclass(self): |
| 3868 |
class MyElement(ET.Element): |
| 3869 |
pass |
| 3870 |
self._check_element_factory_class(MyElement) |
| 3871 |
|
| 3872 |
def test_element_factory_pure_python_subclass(self): |
| 3873 |
# Mimic SimpleTAL's behaviour (issue #16089): both versions of |
| 3874 |
# TreeBuilder should be able to cope with a subclass of the |
| 3875 |
# pure Python Element class. |
| 3876 |
base = ET._Element_Py |
| 3877 |
# Not from a C extension |
| 3878 |
self.assertEqual(base.__module__, 'xml.etree.ElementTree') |
| 3879 |
# Force some multiple inheritance with a C class to make things |
| 3880 |
# more interesting. |
| 3881 |
class MyElement(base, ValueError): |
| 3882 |
pass |
| 3883 |
self._check_element_factory_class(MyElement) |
| 3884 |
|
| 3885 |
def test_doctype(self): |
| 3886 |
class DoctypeParser: |
| 3887 |
_doctype = None |
| 3888 |
|
| 3889 |
def doctype(self, name, pubid, system): |
| 3890 |
self._doctype = (name, pubid, system) |
| 3891 |
|
| 3892 |
def close(self): |
| 3893 |
return self._doctype |
| 3894 |
|
| 3895 |
parser = ET.XMLParser(target=DoctypeParser()) |
| 3896 |
parser.feed(self.sample1) |
| 3897 |
|
| 3898 |
self.assertEqual(parser.close(), |
| 3899 |
('html', '-//W3C//DTD XHTML 1.0 Transitional//EN', |
| 3900 |
'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd')) |
| 3901 |
|
| 3902 |
def test_builder_lookup_errors(self): |
| 3903 |
class RaisingBuilder: |
| 3904 |
def __init__(self, raise_in=None, what=ValueError): |
| 3905 |
self.raise_in = raise_in |
| 3906 |
self.what = what |
| 3907 |
|
| 3908 |
def __getattr__(self, name): |
| 3909 |
if name == self.raise_in: |
| 3910 |
raise self.what(self.raise_in) |
| 3911 |
def handle(*args): |
| 3912 |
pass |
| 3913 |
return handle |
| 3914 |
|
| 3915 |
ET.XMLParser(target=RaisingBuilder()) |
| 3916 |
# cET also checks for 'close' and 'doctype', PyET does it only at need |
| 3917 |
for event in ('start', 'data', 'end', 'comment', 'pi'): |
| 3918 |
with self.assertRaisesRegex(ValueError, event): |
| 3919 |
ET.XMLParser(target=RaisingBuilder(event)) |
| 3920 |
|
| 3921 |
ET.XMLParser(target=RaisingBuilder(what=AttributeError)) |
| 3922 |
for event in ('start', 'data', 'end', 'comment', 'pi'): |
| 3923 |
parser = ET.XMLParser(target=RaisingBuilder(event, what=AttributeError)) |
| 3924 |
parser.feed(self.sample1) |
| 3925 |
self.assertIsNone(parser.close()) |
| 3926 |
|
| 3927 |
|
| 3928 |
class XMLParserTest(unittest.TestCase): |
| 3929 |
sample1 = b'<file><line>22</line></file>' |
| 3930 |
sample2 = (b'<!DOCTYPE html PUBLIC' |
| 3931 |
b' "-//W3C//DTD XHTML 1.0 Transitional//EN"' |
| 3932 |
b' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">' |
| 3933 |
b'<html>text</html>') |
| 3934 |
sample3 = ('<?xml version="1.0" encoding="iso-8859-1"?>\n' |
| 3935 |
'<money value="$\xa3\u20ac\U0001017b">$\xa3\u20ac\U0001017b</money>') |
| 3936 |
|
| 3937 |
def _check_sample_element(self, e): |
| 3938 |
self.assertEqual(e.tag, 'file') |
| 3939 |
self.assertEqual(e[0].tag, 'line') |
| 3940 |
self.assertEqual(e[0].text, '22') |
| 3941 |
|
| 3942 |
def test_constructor_args(self): |
| 3943 |
parser2 = ET.XMLParser(encoding='utf-8', |
| 3944 |
target=ET.TreeBuilder()) |
| 3945 |
parser2.feed(self.sample1) |
| 3946 |
self._check_sample_element(parser2.close()) |
| 3947 |
|
| 3948 |
def test_subclass(self): |
| 3949 |
class MyParser(ET.XMLParser): |
| 3950 |
pass |
| 3951 |
parser = MyParser() |
| 3952 |
parser.feed(self.sample1) |
| 3953 |
self._check_sample_element(parser.close()) |
| 3954 |
|
| 3955 |
def test_doctype_warning(self): |
| 3956 |
with warnings.catch_warnings(): |
| 3957 |
warnings.simplefilter('error', DeprecationWarning) |
| 3958 |
parser = ET.XMLParser() |
| 3959 |
parser.feed(self.sample2) |
| 3960 |
parser.close() |
| 3961 |
|
| 3962 |
def test_subclass_doctype(self): |
| 3963 |
_doctype = None |
| 3964 |
class MyParserWithDoctype(ET.XMLParser): |
| 3965 |
def doctype(self, *args, **kwargs): |
| 3966 |
nonlocal _doctype |
| 3967 |
_doctype = (args, kwargs) |
| 3968 |
|
| 3969 |
parser = MyParserWithDoctype() |
| 3970 |
with self.assertWarnsRegex(RuntimeWarning, 'doctype'): |
| 3971 |
parser.feed(self.sample2) |
| 3972 |
parser.close() |
| 3973 |
self.assertIsNone(_doctype) |
| 3974 |
|
| 3975 |
_doctype = _doctype2 = None |
| 3976 |
with warnings.catch_warnings(): |
| 3977 |
warnings.simplefilter('error', DeprecationWarning) |
| 3978 |
warnings.simplefilter('error', RuntimeWarning) |
| 3979 |
class DoctypeParser: |
| 3980 |
def doctype(self, name, pubid, system): |
| 3981 |
nonlocal _doctype2 |
| 3982 |
_doctype2 = (name, pubid, system) |
| 3983 |
|
| 3984 |
parser = MyParserWithDoctype(target=DoctypeParser()) |
| 3985 |
parser.feed(self.sample2) |
| 3986 |
parser.close() |
| 3987 |
self.assertIsNone(_doctype) |
| 3988 |
self.assertEqual(_doctype2, |
| 3989 |
('html', '-//W3C//DTD XHTML 1.0 Transitional//EN', |
| 3990 |
'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd')) |
| 3991 |
|
| 3992 |
def test_inherited_doctype(self): |
| 3993 |
'''Ensure that ordinary usage is not deprecated (Issue 19176)''' |
| 3994 |
with warnings.catch_warnings(): |
| 3995 |
warnings.simplefilter('error', DeprecationWarning) |
| 3996 |
warnings.simplefilter('error', RuntimeWarning) |
| 3997 |
class MyParserWithoutDoctype(ET.XMLParser): |
| 3998 |
pass |
| 3999 |
parser = MyParserWithoutDoctype() |
| 4000 |
parser.feed(self.sample2) |
| 4001 |
parser.close() |
| 4002 |
|
| 4003 |
def test_parse_string(self): |
| 4004 |
parser = ET.XMLParser(target=ET.TreeBuilder()) |
| 4005 |
parser.feed(self.sample3) |
| 4006 |
e = parser.close() |
| 4007 |
self.assertEqual(e.tag, 'money') |
| 4008 |
self.assertEqual(e.attrib['value'], '$\xa3\u20ac\U0001017b') |
| 4009 |
self.assertEqual(e.text, '$\xa3\u20ac\U0001017b') |
| 4010 |
|
| 4011 |
|
| 4012 |
class NamespaceParseTest(unittest.TestCase): |
| 4013 |
def test_find_with_namespace(self): |
| 4014 |
nsmap = {'h': 'hello', 'f': 'foo'} |
| 4015 |
doc = ET.fromstring(SAMPLE_XML_NS_ELEMS) |
| 4016 |
|
| 4017 |
self.assertEqual(len(doc.findall('{hello}table', nsmap)), 1) |
| 4018 |
self.assertEqual(len(doc.findall('.//{hello}td', nsmap)), 2) |
| 4019 |
self.assertEqual(len(doc.findall('.//{foo}name', nsmap)), 1) |
| 4020 |
|
| 4021 |
|
| 4022 |
class ElementSlicingTest(unittest.TestCase): |
| 4023 |
def _elem_tags(self, elemlist): |
| 4024 |
return [e.tag for e in elemlist] |
| 4025 |
|
| 4026 |
def _subelem_tags(self, elem): |
| 4027 |
return self._elem_tags(list(elem)) |
| 4028 |
|
| 4029 |
def _make_elem_with_children(self, numchildren): |
| 4030 |
"""Create an Element with a tag 'a', with the given amount of children |
| 4031 |
named 'a0', 'a1' ... and so on. |
| 4032 |
|
| 4033 |
""" |
| 4034 |
e = ET.Element('a') |
| 4035 |
for i in range(numchildren): |
| 4036 |
ET.SubElement(e, 'a%s' % i) |
| 4037 |
return e |
| 4038 |
|
| 4039 |
def test_getslice_single_index(self): |
| 4040 |
e = self._make_elem_with_children(10) |
| 4041 |
|
| 4042 |
self.assertEqual(e[1].tag, 'a1') |
| 4043 |
self.assertEqual(e[-2].tag, 'a8') |
| 4044 |
|
| 4045 |
self.assertRaises(IndexError, lambda: e[12]) |
| 4046 |
self.assertRaises(IndexError, lambda: e[-12]) |
| 4047 |
|
| 4048 |
def test_getslice_range(self): |
| 4049 |
e = self._make_elem_with_children(6) |
| 4050 |
|
| 4051 |
self.assertEqual(self._elem_tags(e[3:]), ['a3', 'a4', 'a5']) |
| 4052 |
self.assertEqual(self._elem_tags(e[3:6]), ['a3', 'a4', 'a5']) |
| 4053 |
self.assertEqual(self._elem_tags(e[3:16]), ['a3', 'a4', 'a5']) |
| 4054 |
self.assertEqual(self._elem_tags(e[3:5]), ['a3', 'a4']) |
| 4055 |
self.assertEqual(self._elem_tags(e[3:-1]), ['a3', 'a4']) |
| 4056 |
self.assertEqual(self._elem_tags(e[:2]), ['a0', 'a1']) |
| 4057 |
|
| 4058 |
def test_getslice_steps(self): |
| 4059 |
e = self._make_elem_with_children(10) |
| 4060 |
|
| 4061 |
self.assertEqual(self._elem_tags(e[8:10:1]), ['a8', 'a9']) |
| 4062 |
self.assertEqual(self._elem_tags(e[::3]), ['a0', 'a3', 'a6', 'a9']) |
| 4063 |
self.assertEqual(self._elem_tags(e[::8]), ['a0', 'a8']) |
| 4064 |
self.assertEqual(self._elem_tags(e[1::8]), ['a1', 'a9']) |
| 4065 |
self.assertEqual(self._elem_tags(e[3::sys.maxsize]), ['a3']) |
| 4066 |
self.assertEqual(self._elem_tags(e[3::sys.maxsize<<64]), ['a3']) |
| 4067 |
|
| 4068 |
def test_getslice_negative_steps(self): |
| 4069 |
e = self._make_elem_with_children(4) |
| 4070 |
|
| 4071 |
self.assertEqual(self._elem_tags(e[::-1]), ['a3', 'a2', 'a1', 'a0']) |
| 4072 |
self.assertEqual(self._elem_tags(e[::-2]), ['a3', 'a1']) |
| 4073 |
self.assertEqual(self._elem_tags(e[3::-sys.maxsize]), ['a3']) |
| 4074 |
self.assertEqual(self._elem_tags(e[3::-sys.maxsize-1]), ['a3']) |
| 4075 |
self.assertEqual(self._elem_tags(e[3::-sys.maxsize<<64]), ['a3']) |
| 4076 |
|
| 4077 |
def test_delslice(self): |
| 4078 |
e = self._make_elem_with_children(4) |
| 4079 |
del e[0:2] |
| 4080 |
self.assertEqual(self._subelem_tags(e), ['a2', 'a3']) |
| 4081 |
|
| 4082 |
e = self._make_elem_with_children(4) |
| 4083 |
del e[0:] |
| 4084 |
self.assertEqual(self._subelem_tags(e), []) |
| 4085 |
|
| 4086 |
e = self._make_elem_with_children(4) |
| 4087 |
del e[::-1] |
| 4088 |
self.assertEqual(self._subelem_tags(e), []) |
| 4089 |
|
| 4090 |
e = self._make_elem_with_children(4) |
| 4091 |
del e[::-2] |
| 4092 |
self.assertEqual(self._subelem_tags(e), ['a0', 'a2']) |
| 4093 |
|
| 4094 |
e = self._make_elem_with_children(4) |
| 4095 |
del e[1::2] |
| 4096 |
self.assertEqual(self._subelem_tags(e), ['a0', 'a2']) |
| 4097 |
|
| 4098 |
e = self._make_elem_with_children(2) |
| 4099 |
del e[::2] |
| 4100 |
self.assertEqual(self._subelem_tags(e), ['a1']) |
| 4101 |
|
| 4102 |
def test_setslice_single_index(self): |
| 4103 |
e = self._make_elem_with_children(4) |
| 4104 |
e[1] = ET.Element('b') |
| 4105 |
self.assertEqual(self._subelem_tags(e), ['a0', 'b', 'a2', 'a3']) |
| 4106 |
|
| 4107 |
e[-2] = ET.Element('c') |
| 4108 |
self.assertEqual(self._subelem_tags(e), ['a0', 'b', 'c', 'a3']) |
| 4109 |
|
| 4110 |
with self.assertRaises(IndexError): |
| 4111 |
e[5] = ET.Element('d') |
| 4112 |
with self.assertRaises(IndexError): |
| 4113 |
e[-5] = ET.Element('d') |
| 4114 |
self.assertEqual(self._subelem_tags(e), ['a0', 'b', 'c', 'a3']) |
| 4115 |
|
| 4116 |
def test_setslice_range(self): |
| 4117 |
e = self._make_elem_with_children(4) |
| 4118 |
e[1:3] = [ET.Element('b%s' % i) for i in range(2)] |
| 4119 |
self.assertEqual(self._subelem_tags(e), ['a0', 'b0', 'b1', 'a3']) |
| 4120 |
|
| 4121 |
e = self._make_elem_with_children(4) |
| 4122 |
e[1:3] = [ET.Element('b')] |
| 4123 |
self.assertEqual(self._subelem_tags(e), ['a0', 'b', 'a3']) |
| 4124 |
|
| 4125 |
e = self._make_elem_with_children(4) |
| 4126 |
e[1:3] = [ET.Element('b%s' % i) for i in range(3)] |
| 4127 |
self.assertEqual(self._subelem_tags(e), ['a0', 'b0', 'b1', 'b2', 'a3']) |
| 4128 |
|
| 4129 |
def test_setslice_steps(self): |
| 4130 |
e = self._make_elem_with_children(6) |
| 4131 |
e[1:5:2] = [ET.Element('b%s' % i) for i in range(2)] |
| 4132 |
self.assertEqual(self._subelem_tags(e), ['a0', 'b0', 'a2', 'b1', 'a4', 'a5']) |
| 4133 |
|
| 4134 |
e = self._make_elem_with_children(6) |
| 4135 |
with self.assertRaises(ValueError): |
| 4136 |
e[1:5:2] = [ET.Element('b')] |
| 4137 |
with self.assertRaises(ValueError): |
| 4138 |
e[1:5:2] = [ET.Element('b%s' % i) for i in range(3)] |
| 4139 |
with self.assertRaises(ValueError): |
| 4140 |
e[1:5:2] = [] |
| 4141 |
self.assertEqual(self._subelem_tags(e), ['a0', 'a1', 'a2', 'a3', 'a4', 'a5']) |
| 4142 |
|
| 4143 |
e = self._make_elem_with_children(4) |
| 4144 |
e[1::sys.maxsize] = [ET.Element('b')] |
| 4145 |
self.assertEqual(self._subelem_tags(e), ['a0', 'b', 'a2', 'a3']) |
| 4146 |
e[1::sys.maxsize<<64] = [ET.Element('c')] |
| 4147 |
self.assertEqual(self._subelem_tags(e), ['a0', 'c', 'a2', 'a3']) |
| 4148 |
|
| 4149 |
def test_setslice_negative_steps(self): |
| 4150 |
e = self._make_elem_with_children(4) |
| 4151 |
e[2:0:-1] = [ET.Element('b%s' % i) for i in range(2)] |
| 4152 |
self.assertEqual(self._subelem_tags(e), ['a0', 'b1', 'b0', 'a3']) |
| 4153 |
|
| 4154 |
e = self._make_elem_with_children(4) |
| 4155 |
with self.assertRaises(ValueError): |
| 4156 |
e[2:0:-1] = [ET.Element('b')] |
| 4157 |
with self.assertRaises(ValueError): |
| 4158 |
e[2:0:-1] = [ET.Element('b%s' % i) for i in range(3)] |
| 4159 |
with self.assertRaises(ValueError): |
| 4160 |
e[2:0:-1] = [] |
| 4161 |
self.assertEqual(self._subelem_tags(e), ['a0', 'a1', 'a2', 'a3']) |
| 4162 |
|
| 4163 |
e = self._make_elem_with_children(4) |
| 4164 |
e[1::-sys.maxsize] = [ET.Element('b')] |
| 4165 |
self.assertEqual(self._subelem_tags(e), ['a0', 'b', 'a2', 'a3']) |
| 4166 |
e[1::-sys.maxsize-1] = [ET.Element('c')] |
| 4167 |
self.assertEqual(self._subelem_tags(e), ['a0', 'c', 'a2', 'a3']) |
| 4168 |
e[1::-sys.maxsize<<64] = [ET.Element('d')] |
| 4169 |
self.assertEqual(self._subelem_tags(e), ['a0', 'd', 'a2', 'a3']) |
| 4170 |
|
| 4171 |
def test_issue123213_setslice_exception(self): |
| 4172 |
e = ET.Element('tag') |
| 4173 |
# Does not hide the internal exception when assigning to the element |
| 4174 |
with self.assertRaises(ZeroDivisionError): |
| 4175 |
e[:1] = (1/0 for i in range(2)) |
| 4176 |
|
| 4177 |
# Still raises the TypeError when assigning with a non-iterable |
| 4178 |
with self.assertRaises(TypeError): |
| 4179 |
e[:1] = None |
| 4180 |
|
| 4181 |
# Preserve the original TypeError message when assigning. |
| 4182 |
def f(): |
| 4183 |
raise TypeError("mymessage") |
| 4184 |
|
| 4185 |
with self.assertRaisesRegex(TypeError, 'mymessage'): |
| 4186 |
e[:1] = (f() for i in range(2)) |
| 4187 |
|
| 4188 |
class IOTest(unittest.TestCase): |
| 4189 |
def test_encoding(self): |
| 4190 |
# Test encoding issues. |
| 4191 |
elem = ET.Element("tag") |
| 4192 |
elem.text = "abc" |
| 4193 |
self.assertEqual(serialize(elem), '<tag>abc</tag>') |
| 4194 |
for enc in ("utf-8", "us-ascii"): |
| 4195 |
with self.subTest(enc): |
| 4196 |
self.assertEqual(serialize(elem, encoding=enc), |
| 4197 |
b'<tag>abc</tag>') |
| 4198 |
self.assertEqual(serialize(elem, encoding=enc.upper()), |
| 4199 |
b'<tag>abc</tag>') |
| 4200 |
for enc in ("iso-8859-1", "utf-16", "utf-32"): |
| 4201 |
with self.subTest(enc): |
| 4202 |
self.assertEqual(serialize(elem, encoding=enc), |
| 4203 |
("<?xml version='1.0' encoding='%s'?>\n" |
| 4204 |
"<tag>abc</tag>" % enc).encode(enc)) |
| 4205 |
upper = enc.upper() |
| 4206 |
self.assertEqual(serialize(elem, encoding=upper), |
| 4207 |
("<?xml version='1.0' encoding='%s'?>\n" |
| 4208 |
"<tag>abc</tag>" % upper).encode(enc)) |
| 4209 |
|
| 4210 |
elem = ET.Element("tag") |
| 4211 |
elem.text = "<&\"\'>" |
| 4212 |
self.assertEqual(serialize(elem), '<tag><&"\'></tag>') |
| 4213 |
self.assertEqual(serialize(elem, encoding="utf-8"), |
| 4214 |
b'<tag><&"\'></tag>') |
| 4215 |
self.assertEqual(serialize(elem, encoding="us-ascii"), |
| 4216 |
b'<tag><&"\'></tag>') |
| 4217 |
for enc in ("iso-8859-1", "utf-16", "utf-32"): |
| 4218 |
self.assertEqual(serialize(elem, encoding=enc), |
| 4219 |
("<?xml version='1.0' encoding='%s'?>\n" |
| 4220 |
"<tag><&\"'></tag>" % enc).encode(enc)) |
| 4221 |
|
| 4222 |
elem = ET.Element("tag") |
| 4223 |
elem.attrib["key"] = "<&\"\'>" |
| 4224 |
self.assertEqual(serialize(elem), '<tag key="<&"\'>" />') |
| 4225 |
self.assertEqual(serialize(elem, encoding="utf-8"), |
| 4226 |
b'<tag key="<&"\'>" />') |
| 4227 |
self.assertEqual(serialize(elem, encoding="us-ascii"), |
| 4228 |
b'<tag key="<&"\'>" />') |
| 4229 |
for enc in ("iso-8859-1", "utf-16", "utf-32"): |
| 4230 |
self.assertEqual(serialize(elem, encoding=enc), |
| 4231 |
("<?xml version='1.0' encoding='%s'?>\n" |
| 4232 |
"<tag key=\"<&"'>\" />" % enc).encode(enc)) |
| 4233 |
|
| 4234 |
elem = ET.Element("tag") |
| 4235 |
elem.text = '\xe5\xf6\xf6<>' |
| 4236 |
self.assertEqual(serialize(elem), '<tag>\xe5\xf6\xf6<></tag>') |
| 4237 |
self.assertEqual(serialize(elem, encoding="utf-8"), |
| 4238 |
b'<tag>\xc3\xa5\xc3\xb6\xc3\xb6<></tag>') |
| 4239 |
self.assertEqual(serialize(elem, encoding="us-ascii"), |
| 4240 |
b'<tag>åöö<></tag>') |
| 4241 |
for enc in ("iso-8859-1", "utf-16", "utf-32"): |
| 4242 |
self.assertEqual(serialize(elem, encoding=enc), |
| 4243 |
("<?xml version='1.0' encoding='%s'?>\n" |
| 4244 |
"<tag>åöö<></tag>" % enc).encode(enc)) |
| 4245 |
|
| 4246 |
elem = ET.Element("tag") |
| 4247 |
elem.attrib["key"] = '\xe5\xf6\xf6<>' |
| 4248 |
self.assertEqual(serialize(elem), '<tag key="\xe5\xf6\xf6<>" />') |
| 4249 |
self.assertEqual(serialize(elem, encoding="utf-8"), |
| 4250 |
b'<tag key="\xc3\xa5\xc3\xb6\xc3\xb6<>" />') |
| 4251 |
self.assertEqual(serialize(elem, encoding="us-ascii"), |
| 4252 |
b'<tag key="åöö<>" />') |
| 4253 |
for enc in ("iso-8859-1", "utf-16", "utf-16le", "utf-16be", "utf-32"): |
| 4254 |
self.assertEqual(serialize(elem, encoding=enc), |
| 4255 |
("<?xml version='1.0' encoding='%s'?>\n" |
| 4256 |
"<tag key=\"åöö<>\" />" % enc).encode(enc)) |
| 4257 |
|
| 4258 |
def test_write_to_filename(self): |
| 4259 |
self.addCleanup(os_helper.unlink, TESTFN) |
| 4260 |
tree = ET.ElementTree(ET.XML('''<site>\xf8</site>''')) |
| 4261 |
tree.write(TESTFN) |
| 4262 |
with open(TESTFN, 'rb') as f: |
| 4263 |
self.assertEqual(f.read(), b'''<site>ø</site>''') |
| 4264 |
|
| 4265 |
def test_write_to_filename_with_encoding(self): |
| 4266 |
self.addCleanup(os_helper.unlink, TESTFN) |
| 4267 |
tree = ET.ElementTree(ET.XML('''<site>\xf8</site>''')) |
| 4268 |
tree.write(TESTFN, encoding='utf-8') |
| 4269 |
with open(TESTFN, 'rb') as f: |
| 4270 |
self.assertEqual(f.read(), b'''<site>\xc3\xb8</site>''') |
| 4271 |
|
| 4272 |
tree.write(TESTFN, encoding='ISO-8859-1') |
| 4273 |
with open(TESTFN, 'rb') as f: |
| 4274 |
self.assertEqual(f.read(), convlinesep( |
| 4275 |
b'''<?xml version='1.0' encoding='ISO-8859-1'?>\n''' |
| 4276 |
b'''<site>\xf8</site>''')) |
| 4277 |
|
| 4278 |
def test_write_to_filename_as_unicode(self): |
| 4279 |
self.addCleanup(os_helper.unlink, TESTFN) |
| 4280 |
with open(TESTFN, 'w') as f: |
| 4281 |
encoding = f.encoding |
| 4282 |
os_helper.unlink(TESTFN) |
| 4283 |
|
| 4284 |
tree = ET.ElementTree(ET.XML('''<site>\xf8</site>''')) |
| 4285 |
tree.write(TESTFN, encoding='unicode') |
| 4286 |
with open(TESTFN, 'rb') as f: |
| 4287 |
self.assertEqual(f.read(), b"<site>\xc3\xb8</site>") |
| 4288 |
|
| 4289 |
def test_write_to_text_file(self): |
| 4290 |
self.addCleanup(os_helper.unlink, TESTFN) |
| 4291 |
tree = ET.ElementTree(ET.XML('''<site>\xf8</site>''')) |
| 4292 |
with open(TESTFN, 'w', encoding='utf-8') as f: |
| 4293 |
tree.write(f, encoding='unicode') |
| 4294 |
self.assertFalse(f.closed) |
| 4295 |
with open(TESTFN, 'rb') as f: |
| 4296 |
self.assertEqual(f.read(), b'''<site>\xc3\xb8</site>''') |
| 4297 |
|
| 4298 |
with open(TESTFN, 'w', encoding='ascii', errors='xmlcharrefreplace') as f: |
| 4299 |
tree.write(f, encoding='unicode') |
| 4300 |
self.assertFalse(f.closed) |
| 4301 |
with open(TESTFN, 'rb') as f: |
| 4302 |
self.assertEqual(f.read(), b'''<site>ø</site>''') |
| 4303 |
|
| 4304 |
with open(TESTFN, 'w', encoding='ISO-8859-1') as f: |
| 4305 |
tree.write(f, encoding='unicode') |
| 4306 |
self.assertFalse(f.closed) |
| 4307 |
with open(TESTFN, 'rb') as f: |
| 4308 |
self.assertEqual(f.read(), b'''<site>\xf8</site>''') |
| 4309 |
|
| 4310 |
def test_write_to_binary_file(self): |
| 4311 |
self.addCleanup(os_helper.unlink, TESTFN) |
| 4312 |
tree = ET.ElementTree(ET.XML('''<site>\xf8</site>''')) |
| 4313 |
with open(TESTFN, 'wb') as f: |
| 4314 |
tree.write(f) |
| 4315 |
self.assertFalse(f.closed) |
| 4316 |
with open(TESTFN, 'rb') as f: |
| 4317 |
self.assertEqual(f.read(), b'''<site>ø</site>''') |
| 4318 |
|
| 4319 |
def test_write_to_binary_file_with_encoding(self): |
| 4320 |
self.addCleanup(os_helper.unlink, TESTFN) |
| 4321 |
tree = ET.ElementTree(ET.XML('''<site>\xf8</site>''')) |
| 4322 |
with open(TESTFN, 'wb') as f: |
| 4323 |
tree.write(f, encoding='utf-8') |
| 4324 |
self.assertFalse(f.closed) |
| 4325 |
with open(TESTFN, 'rb') as f: |
| 4326 |
self.assertEqual(f.read(), b'''<site>\xc3\xb8</site>''') |
| 4327 |
|
| 4328 |
with open(TESTFN, 'wb') as f: |
| 4329 |
tree.write(f, encoding='ISO-8859-1') |
| 4330 |
self.assertFalse(f.closed) |
| 4331 |
with open(TESTFN, 'rb') as f: |
| 4332 |
self.assertEqual(f.read(), |
| 4333 |
b'''<?xml version='1.0' encoding='ISO-8859-1'?>\n''' |
| 4334 |
b'''<site>\xf8</site>''') |
| 4335 |
|
| 4336 |
def test_write_to_binary_file_with_bom(self): |
| 4337 |
self.addCleanup(os_helper.unlink, TESTFN) |
| 4338 |
tree = ET.ElementTree(ET.XML('''<site>\xf8</site>''')) |
| 4339 |
# test BOM writing to buffered file |
| 4340 |
with open(TESTFN, 'wb') as f: |
| 4341 |
tree.write(f, encoding='utf-16') |
| 4342 |
self.assertFalse(f.closed) |
| 4343 |
with open(TESTFN, 'rb') as f: |
| 4344 |
self.assertEqual(f.read(), |
| 4345 |
'''<?xml version='1.0' encoding='utf-16'?>\n''' |
| 4346 |
'''<site>\xf8</site>'''.encode("utf-16")) |
| 4347 |
# test BOM writing to non-buffered file |
| 4348 |
with open(TESTFN, 'wb', buffering=0) as f: |
| 4349 |
tree.write(f, encoding='utf-16') |
| 4350 |
self.assertFalse(f.closed) |
| 4351 |
with open(TESTFN, 'rb') as f: |
| 4352 |
self.assertEqual(f.read(), |
| 4353 |
'''<?xml version='1.0' encoding='utf-16'?>\n''' |
| 4354 |
'''<site>\xf8</site>'''.encode("utf-16")) |
| 4355 |
|
| 4356 |
def test_read_from_stringio(self): |
| 4357 |
tree = ET.ElementTree() |
| 4358 |
stream = io.StringIO('''<?xml version="1.0"?><site></site>''') |
| 4359 |
tree.parse(stream) |
| 4360 |
self.assertEqual(tree.getroot().tag, 'site') |
| 4361 |
|
| 4362 |
def test_write_to_stringio(self): |
| 4363 |
tree = ET.ElementTree(ET.XML('''<site>\xf8</site>''')) |
| 4364 |
stream = io.StringIO() |
| 4365 |
tree.write(stream, encoding='unicode') |
| 4366 |
self.assertEqual(stream.getvalue(), '''<site>\xf8</site>''') |
| 4367 |
|
| 4368 |
def test_read_from_bytesio(self): |
| 4369 |
tree = ET.ElementTree() |
| 4370 |
raw = io.BytesIO(b'''<?xml version="1.0"?><site></site>''') |
| 4371 |
tree.parse(raw) |
| 4372 |
self.assertEqual(tree.getroot().tag, 'site') |
| 4373 |
|
| 4374 |
def test_write_to_bytesio(self): |
| 4375 |
tree = ET.ElementTree(ET.XML('''<site>\xf8</site>''')) |
| 4376 |
raw = io.BytesIO() |
| 4377 |
tree.write(raw) |
| 4378 |
self.assertEqual(raw.getvalue(), b'''<site>ø</site>''') |
| 4379 |
|
| 4380 |
class dummy: |
| 4381 |
pass |
| 4382 |
|
| 4383 |
def test_read_from_user_text_reader(self): |
| 4384 |
stream = io.StringIO('''<?xml version="1.0"?><site></site>''') |
| 4385 |
reader = self.dummy() |
| 4386 |
reader.read = stream.read |
| 4387 |
tree = ET.ElementTree() |
| 4388 |
tree.parse(reader) |
| 4389 |
self.assertEqual(tree.getroot().tag, 'site') |
| 4390 |
|
| 4391 |
def test_write_to_user_text_writer(self): |
| 4392 |
tree = ET.ElementTree(ET.XML('''<site>\xf8</site>''')) |
| 4393 |
stream = io.StringIO() |
| 4394 |
writer = self.dummy() |
| 4395 |
writer.write = stream.write |
| 4396 |
tree.write(writer, encoding='unicode') |
| 4397 |
self.assertEqual(stream.getvalue(), '''<site>\xf8</site>''') |
| 4398 |
|
| 4399 |
def test_read_from_user_binary_reader(self): |
| 4400 |
raw = io.BytesIO(b'''<?xml version="1.0"?><site></site>''') |
| 4401 |
reader = self.dummy() |
| 4402 |
reader.read = raw.read |
| 4403 |
tree = ET.ElementTree() |
| 4404 |
tree.parse(reader) |
| 4405 |
self.assertEqual(tree.getroot().tag, 'site') |
| 4406 |
tree = ET.ElementTree() |
| 4407 |
|
| 4408 |
def test_write_to_user_binary_writer(self): |
| 4409 |
tree = ET.ElementTree(ET.XML('''<site>\xf8</site>''')) |
| 4410 |
raw = io.BytesIO() |
| 4411 |
writer = self.dummy() |
| 4412 |
writer.write = raw.write |
| 4413 |
tree.write(writer) |
| 4414 |
self.assertEqual(raw.getvalue(), b'''<site>ø</site>''') |
| 4415 |
|
| 4416 |
def test_write_to_user_binary_writer_with_bom(self): |
| 4417 |
tree = ET.ElementTree(ET.XML('''<site />''')) |
| 4418 |
raw = io.BytesIO() |
| 4419 |
writer = self.dummy() |
| 4420 |
writer.write = raw.write |
| 4421 |
writer.seekable = lambda: True |
| 4422 |
writer.tell = raw.tell |
| 4423 |
tree.write(writer, encoding="utf-16") |
| 4424 |
self.assertEqual(raw.getvalue(), |
| 4425 |
'''<?xml version='1.0' encoding='utf-16'?>\n''' |
| 4426 |
'''<site />'''.encode("utf-16")) |
| 4427 |
|
| 4428 |
def test_tostringlist_invariant(self): |
| 4429 |
root = ET.fromstring('<tag>foo</tag>') |
| 4430 |
self.assertEqual( |
| 4431 |
ET.tostring(root, 'unicode'), |
| 4432 |
''.join(ET.tostringlist(root, 'unicode'))) |
| 4433 |
self.assertEqual( |
| 4434 |
ET.tostring(root, 'utf-16'), |
| 4435 |
b''.join(ET.tostringlist(root, 'utf-16'))) |
| 4436 |
|
| 4437 |
def test_short_empty_elements(self): |
| 4438 |
root = ET.fromstring('<tag>a<x />b<y></y>c</tag>') |
| 4439 |
self.assertEqual( |
| 4440 |
ET.tostring(root, 'unicode'), |
| 4441 |
'<tag>a<x />b<y />c</tag>') |
| 4442 |
self.assertEqual( |
| 4443 |
ET.tostring(root, 'unicode', short_empty_elements=True), |
| 4444 |
'<tag>a<x />b<y />c</tag>') |
| 4445 |
self.assertEqual( |
| 4446 |
ET.tostring(root, 'unicode', short_empty_elements=False), |
| 4447 |
'<tag>a<x></x>b<y></y>c</tag>') |
| 4448 |
|
| 4449 |
|
| 4450 |
class ParseErrorTest(unittest.TestCase): |
| 4451 |
def test_subclass(self): |
| 4452 |
self.assertIsInstance(ET.ParseError(), SyntaxError) |
| 4453 |
|
| 4454 |
def _get_error(self, s): |
| 4455 |
try: |
| 4456 |
ET.fromstring(s) |
| 4457 |
except ET.ParseError as e: |
| 4458 |
return e |
| 4459 |
|
| 4460 |
def test_error_position(self): |
| 4461 |
self.assertEqual(self._get_error('foo').position, (1, 0)) |
| 4462 |
self.assertEqual(self._get_error('<tag>&foo;</tag>').position, (1, 5)) |
| 4463 |
self.assertEqual(self._get_error('foobar<').position, (1, 6)) |
| 4464 |
|
| 4465 |
def test_error_code(self): |
| 4466 |
import xml.parsers.expat.errors as ERRORS |
| 4467 |
self.assertEqual(self._get_error('foo').code, |
| 4468 |
ERRORS.codes[ERRORS.XML_ERROR_SYNTAX]) |
| 4469 |
|
| 4470 |
|
| 4471 |
class KeywordArgsTest(unittest.TestCase): |
| 4472 |
# Test various issues with keyword arguments passed to ET.Element |
| 4473 |
# constructor and methods |
| 4474 |
def test_issue14818(self): |
| 4475 |
x = ET.XML("<a>foo</a>") |
| 4476 |
self.assertEqual(x.find('a', None), |
| 4477 |
x.find(path='a', namespaces=None)) |
| 4478 |
self.assertEqual(x.findtext('a', None, None), |
| 4479 |
x.findtext(path='a', default=None, namespaces=None)) |
| 4480 |
self.assertEqual(x.findall('a', None), |
| 4481 |
x.findall(path='a', namespaces=None)) |
| 4482 |
self.assertEqual(list(x.iterfind('a', None)), |
| 4483 |
list(x.iterfind(path='a', namespaces=None))) |
| 4484 |
|
| 4485 |
self.assertEqual(ET.Element('a').attrib, {}) |
| 4486 |
elements = [ |
| 4487 |
ET.Element('a', dict(href="#", id="foo")), |
| 4488 |
ET.Element('a', attrib=dict(href="#", id="foo")), |
| 4489 |
ET.Element('a', dict(href="#"), id="foo"), |
| 4490 |
ET.Element('a', href="#", id="foo"), |
| 4491 |
ET.Element('a', dict(href="#", id="foo"), href="#", id="foo"), |
| 4492 |
] |
| 4493 |
for e in elements: |
| 4494 |
self.assertEqual(e.tag, 'a') |
| 4495 |
self.assertEqual(e.attrib, dict(href="#", id="foo")) |
| 4496 |
|
| 4497 |
e2 = ET.SubElement(elements[0], 'foobar', attrib={'key1': 'value1'}) |
| 4498 |
self.assertEqual(e2.attrib['key1'], 'value1') |
| 4499 |
|
| 4500 |
with self.assertRaisesRegex(TypeError, 'must be dict, not str'): |
| 4501 |
ET.Element('a', "I'm not a dict") |
| 4502 |
with self.assertRaisesRegex(TypeError, 'must be dict, not str'): |
| 4503 |
ET.Element('a', attrib="I'm not a dict") |
| 4504 |
|
| 4505 |
# -------------------------------------------------------------------- |
| 4506 |
|
| 4507 |
class NoAcceleratorTest(unittest.TestCase): |
| 4508 |
@classmethod |
| 4509 |
def setUpClass(cls): |
| 4510 |
if ET is not pyET: |
| 4511 |
raise unittest.SkipTest('only for the Python version') |
| 4512 |
|
| 4513 |
# Test that the C accelerator was not imported for pyET |
| 4514 |
def test_correct_import_pyET(self): |
| 4515 |
# The type of methods defined in Python code is types.FunctionType, |
| 4516 |
# while the type of methods defined inside _elementtree is |
| 4517 |
# <class 'wrapper_descriptor'> |
| 4518 |
self.assertIsInstance(pyET.Element.__init__, types.FunctionType) |
| 4519 |
self.assertIsInstance(pyET.XMLParser.__init__, types.FunctionType) |
| 4520 |
|
| 4521 |
# -------------------------------------------------------------------- |
| 4522 |
|
| 4523 |
class BoolTest(unittest.TestCase): |
| 4524 |
def test_warning(self): |
| 4525 |
e = ET.fromstring('<a style="new"></a>') |
| 4526 |
msg = ( |
| 4527 |
r"Testing an element's truth value will always return True in " |
| 4528 |
r"future versions. " |
| 4529 |
r"Use specific 'len\(elem\)' or 'elem is not None' test instead.") |
| 4530 |
with self.assertWarnsRegex(DeprecationWarning, msg): |
| 4531 |
result = bool(e) |
| 4532 |
# Emulate prior behavior for now |
| 4533 |
self.assertIs(result, False) |
| 4534 |
|
| 4535 |
# Element with children |
| 4536 |
ET.SubElement(e, 'b') |
| 4537 |
with self.assertWarnsRegex(DeprecationWarning, msg): |
| 4538 |
new_result = bool(e) |
| 4539 |
self.assertIs(new_result, True) |
| 4540 |
|
| 4541 |
# -------------------------------------------------------------------- |
| 4542 |
|
| 4543 |
def c14n_roundtrip(xml, **options): |
| 4544 |
return pyET.canonicalize(xml, **options) |
| 4545 |
|
| 4546 |
|
| 4547 |
class C14NTest(unittest.TestCase): |
| 4548 |
maxDiff = None |
| 4549 |
|
| 4550 |
# |
| 4551 |
# simple roundtrip tests (from c14n.py) |
| 4552 |
|
| 4553 |
def test_simple_roundtrip(self): |
| 4554 |
# Basics |
| 4555 |
self.assertEqual(c14n_roundtrip("<doc/>"), '<doc></doc>') |
| 4556 |
self.assertEqual(c14n_roundtrip("<doc xmlns='uri'/>"), # FIXME |
| 4557 |
'<doc xmlns="uri"></doc>') |
| 4558 |
self.assertEqual(c14n_roundtrip("<prefix:doc xmlns:prefix='uri'/>"), |
| 4559 |
'<prefix:doc xmlns:prefix="uri"></prefix:doc>') |
| 4560 |
self.assertEqual(c14n_roundtrip("<doc xmlns:prefix='uri'><prefix:bar/></doc>"), |
| 4561 |
'<doc><prefix:bar xmlns:prefix="uri"></prefix:bar></doc>') |
| 4562 |
self.assertEqual(c14n_roundtrip("<elem xmlns:wsu='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd' xmlns:SOAP-ENV='http://schemas.xmlsoap.org/soap/envelope/' />"), |
| 4563 |
'<elem></elem>') |
| 4564 |
|
| 4565 |
# C14N spec |
| 4566 |
self.assertEqual(c14n_roundtrip("<doc>Hello, world!<!-- Comment 1 --></doc>"), |
| 4567 |
'<doc>Hello, world!</doc>') |
| 4568 |
self.assertEqual(c14n_roundtrip("<value>2</value>"), |
| 4569 |
'<value>2</value>') |
| 4570 |
self.assertEqual(c14n_roundtrip('<compute><![CDATA[value>"0" && value<"10" ?"valid":"error"]]></compute>'), |
| 4571 |
'<compute>value>"0" && value<"10" ?"valid":"error"</compute>') |
| 4572 |
self.assertEqual(c14n_roundtrip('''<compute expr='value>"0" && value<"10" ?"valid":"error"'>valid</compute>'''), |
| 4573 |
'<compute expr="value>"0" && value<"10" ?"valid":"error"">valid</compute>') |
| 4574 |
self.assertEqual(c14n_roundtrip("<norm attr=' '   
	 ' '/>"), |
| 4575 |
'<norm attr=" \' 
	 \' "></norm>') |
| 4576 |
self.assertEqual(c14n_roundtrip("<normNames attr=' A   
	 B '/>"), |
| 4577 |
'<normNames attr=" A 
	 B "></normNames>') |
| 4578 |
self.assertEqual(c14n_roundtrip("<normId id=' '   
	 ' '/>"), |
| 4579 |
'<normId id=" \' 
	 \' "></normId>') |
| 4580 |
|
| 4581 |
# fragments from PJ's tests |
| 4582 |
#self.assertEqual(c14n_roundtrip("<doc xmlns:x='http://example.com/x' xmlns='http://example.com/default'><b y:a1='1' xmlns='http://example.com/default' a3='3' xmlns:y='http://example.com/y' y:a2='2'/></doc>"), |
| 4583 |
#'<doc xmlns:x="http://example.com/x"><b xmlns:y="http://example.com/y" a3="3" y:a1="1" y:a2="2"></b></doc>') |
| 4584 |
|
| 4585 |
# Namespace issues |
| 4586 |
xml = '<X xmlns="http://nps/a"><Y targets="abc,xyz"></Y></X>' |
| 4587 |
self.assertEqual(c14n_roundtrip(xml), xml) |
| 4588 |
xml = '<X xmlns="http://nps/a"><Y xmlns="http://nsp/b" targets="abc,xyz"></Y></X>' |
| 4589 |
self.assertEqual(c14n_roundtrip(xml), xml) |
| 4590 |
xml = '<X xmlns="http://nps/a"><Y xmlns:b="http://nsp/b" b:targets="abc,xyz"></Y></X>' |
| 4591 |
self.assertEqual(c14n_roundtrip(xml), xml) |
| 4592 |
|
| 4593 |
def test_c14n_exclusion(self): |
| 4594 |
xml = textwrap.dedent("""\ |
| 4595 |
<root xmlns:x="http://example.com/x"> |
| 4596 |
<a x:attr="attrx"> |
| 4597 |
<b>abtext</b> |
| 4598 |
</a> |
| 4599 |
<b>btext</b> |
| 4600 |
<c> |
| 4601 |
<x:d>dtext</x:d> |
| 4602 |
</c> |
| 4603 |
</root> |
| 4604 |
""") |
| 4605 |
self.assertEqual( |
| 4606 |
c14n_roundtrip(xml, strip_text=True), |
| 4607 |
'<root>' |
| 4608 |
'<a xmlns:x="http://example.com/x" x:attr="attrx"><b>abtext</b></a>' |
| 4609 |
'<b>btext</b>' |
| 4610 |
'<c><x:d xmlns:x="http://example.com/x">dtext</x:d></c>' |
| 4611 |
'</root>') |
| 4612 |
self.assertEqual( |
| 4613 |
c14n_roundtrip(xml, strip_text=True, exclude_attrs=['{http://example.com/x}attr']), |
| 4614 |
'<root>' |
| 4615 |
'<a><b>abtext</b></a>' |
| 4616 |
'<b>btext</b>' |
| 4617 |
'<c><x:d xmlns:x="http://example.com/x">dtext</x:d></c>' |
| 4618 |
'</root>') |
| 4619 |
self.assertEqual( |
| 4620 |
c14n_roundtrip(xml, strip_text=True, exclude_tags=['{http://example.com/x}d']), |
| 4621 |
'<root>' |
| 4622 |
'<a xmlns:x="http://example.com/x" x:attr="attrx"><b>abtext</b></a>' |
| 4623 |
'<b>btext</b>' |
| 4624 |
'<c></c>' |
| 4625 |
'</root>') |
| 4626 |
self.assertEqual( |
| 4627 |
c14n_roundtrip(xml, strip_text=True, exclude_attrs=['{http://example.com/x}attr'], |
| 4628 |
exclude_tags=['{http://example.com/x}d']), |
| 4629 |
'<root>' |
| 4630 |
'<a><b>abtext</b></a>' |
| 4631 |
'<b>btext</b>' |
| 4632 |
'<c></c>' |
| 4633 |
'</root>') |
| 4634 |
self.assertEqual( |
| 4635 |
c14n_roundtrip(xml, strip_text=True, exclude_tags=['a', 'b']), |
| 4636 |
'<root>' |
| 4637 |
'<c><x:d xmlns:x="http://example.com/x">dtext</x:d></c>' |
| 4638 |
'</root>') |
| 4639 |
self.assertEqual( |
| 4640 |
c14n_roundtrip(xml, exclude_tags=['a', 'b']), |
| 4641 |
'<root>\n' |
| 4642 |
' \n' |
| 4643 |
' \n' |
| 4644 |
' <c>\n' |
| 4645 |
' <x:d xmlns:x="http://example.com/x">dtext</x:d>\n' |
| 4646 |
' </c>\n' |
| 4647 |
'</root>') |
| 4648 |
self.assertEqual( |
| 4649 |
c14n_roundtrip(xml, strip_text=True, exclude_tags=['{http://example.com/x}d', 'b']), |
| 4650 |
'<root>' |
| 4651 |
'<a xmlns:x="http://example.com/x" x:attr="attrx"></a>' |
| 4652 |
'<c></c>' |
| 4653 |
'</root>') |
| 4654 |
self.assertEqual( |
| 4655 |
c14n_roundtrip(xml, exclude_tags=['{http://example.com/x}d', 'b']), |
| 4656 |
'<root>\n' |
| 4657 |
' <a xmlns:x="http://example.com/x" x:attr="attrx">\n' |
| 4658 |
' \n' |
| 4659 |
' </a>\n' |
| 4660 |
' \n' |
| 4661 |
' <c>\n' |
| 4662 |
' \n' |
| 4663 |
' </c>\n' |
| 4664 |
'</root>') |
| 4665 |
|
| 4666 |
# |
| 4667 |
# basic method=c14n tests from the c14n 2.0 specification. uses |
| 4668 |
# test files under xmltestdata/c14n-20. |
| 4669 |
|
| 4670 |
# note that this uses generated C14N versions of the standard ET.write |
| 4671 |
# output, not roundtripped C14N (see above). |
| 4672 |
|
| 4673 |
def test_xml_c14n2(self): |
| 4674 |
datadir = findfile("c14n-20", subdir="xmltestdata") |
| 4675 |
full_path = partial(os.path.join, datadir) |
| 4676 |
|
| 4677 |
files = [filename[:-4] for filename in sorted(os.listdir(datadir)) |
| 4678 |
if filename.endswith('.xml')] |
| 4679 |
input_files = [ |
| 4680 |
filename for filename in files |
| 4681 |
if filename.startswith('in') |
| 4682 |
] |
| 4683 |
configs = { |
| 4684 |
filename: { |
| 4685 |
# <c14n2:PrefixRewrite>sequential</c14n2:PrefixRewrite> |
| 4686 |
option.tag.split('}')[-1]: ((option.text or '').strip(), option) |
| 4687 |
for option in ET.parse(full_path(filename) + ".xml").getroot() |
| 4688 |
} |
| 4689 |
for filename in files |
| 4690 |
if filename.startswith('c14n') |
| 4691 |
} |
| 4692 |
|
| 4693 |
tests = { |
| 4694 |
input_file: [ |
| 4695 |
(filename, configs[filename.rsplit('_', 1)[-1]]) |
| 4696 |
for filename in files |
| 4697 |
if filename.startswith(f'out_{input_file}_') |
| 4698 |
and filename.rsplit('_', 1)[-1] in configs |
| 4699 |
] |
| 4700 |
for input_file in input_files |
| 4701 |
} |
| 4702 |
|
| 4703 |
# Make sure we found all test cases. |
| 4704 |
self.assertEqual(30, len([ |
| 4705 |
output_file for output_files in tests.values() |
| 4706 |
for output_file in output_files])) |
| 4707 |
|
| 4708 |
def get_option(config, option_name, default=None): |
| 4709 |
return config.get(option_name, (default, ()))[0] |
| 4710 |
|
| 4711 |
for input_file, output_files in tests.items(): |
| 4712 |
for output_file, config in output_files: |
| 4713 |
keep_comments = get_option( |
| 4714 |
config, 'IgnoreComments') == 'true' # no, it's right :) |
| 4715 |
strip_text = get_option( |
| 4716 |
config, 'TrimTextNodes') == 'true' |
| 4717 |
rewrite_prefixes = get_option( |
| 4718 |
config, 'PrefixRewrite') == 'sequential' |
| 4719 |
if 'QNameAware' in config: |
| 4720 |
qattrs = [ |
| 4721 |
f"{{{el.get('NS')}}}{el.get('Name')}" |
| 4722 |
for el in config['QNameAware'][1].findall( |
| 4723 |
'{http://www.w3.org/2010/xml-c14n2}QualifiedAttr') |
| 4724 |
] |
| 4725 |
qtags = [ |
| 4726 |
f"{{{el.get('NS')}}}{el.get('Name')}" |
| 4727 |
for el in config['QNameAware'][1].findall( |
| 4728 |
'{http://www.w3.org/2010/xml-c14n2}Element') |
| 4729 |
] |
| 4730 |
else: |
| 4731 |
qtags = qattrs = None |
| 4732 |
|
| 4733 |
# Build subtest description from config. |
| 4734 |
config_descr = ','.join( |
| 4735 |
f"{name}={value or ','.join(c.tag.split('}')[-1] for c in children)}" |
| 4736 |
for name, (value, children) in sorted(config.items()) |
| 4737 |
) |
| 4738 |
|
| 4739 |
with self.subTest(f"{output_file}({config_descr})"): |
| 4740 |
if input_file == 'inNsRedecl' and not rewrite_prefixes: |
| 4741 |
self.skipTest( |
| 4742 |
f"Redeclared namespace handling is not supported in {output_file}") |
| 4743 |
if input_file == 'inNsSuperfluous' and not rewrite_prefixes: |
| 4744 |
self.skipTest( |
| 4745 |
f"Redeclared namespace handling is not supported in {output_file}") |
| 4746 |
if 'QNameAware' in config and config['QNameAware'][1].find( |
| 4747 |
'{http://www.w3.org/2010/xml-c14n2}XPathElement') is not None: |
| 4748 |
self.skipTest( |
| 4749 |
f"QName rewriting in XPath text is not supported in {output_file}") |
| 4750 |
|
| 4751 |
f = full_path(input_file + ".xml") |
| 4752 |
if input_file == 'inC14N5': |
| 4753 |
# Hack: avoid setting up external entity resolution in the parser. |
| 4754 |
with open(full_path('world.txt'), 'rb') as entity_file: |
| 4755 |
with open(f, 'rb') as f: |
| 4756 |
f = io.BytesIO(f.read().replace(b'&ent2;', entity_file.read())) |
| 4757 |
|
| 4758 |
text = ET.canonicalize( |
| 4759 |
from_file=f, |
| 4760 |
with_comments=keep_comments, |
| 4761 |
strip_text=strip_text, |
| 4762 |
rewrite_prefixes=rewrite_prefixes, |
| 4763 |
qname_aware_tags=qtags, qname_aware_attrs=qattrs) |
| 4764 |
|
| 4765 |
with open(full_path(output_file + ".xml"), 'r', encoding='utf8') as f: |
| 4766 |
expected = f.read() |
| 4767 |
if input_file == 'inC14N3': |
| 4768 |
# FIXME: cET resolves default attributes but ET does not! |
| 4769 |
expected = expected.replace(' attr="default"', '') |
| 4770 |
text = text.replace(' attr="default"', '') |
| 4771 |
self.assertEqual(expected, text) |
| 4772 |
|
| 4773 |
# -------------------------------------------------------------------- |
| 4774 |
|
| 4775 |
def setUpModule(module=None): |
| 4776 |
# When invoked without a module, runs the Python ET tests by loading pyET. |
| 4777 |
# Otherwise, uses the given module as the ET. |
| 4778 |
global pyET |
| 4779 |
pyET = import_fresh_module('xml.etree.ElementTree', |
| 4780 |
blocked=['_elementtree']) |
| 4781 |
if module is None: |
| 4782 |
module = pyET |
| 4783 |
|
| 4784 |
global ET |
| 4785 |
ET = module |
| 4786 |
|
| 4787 |
# don't interfere with subsequent tests |
| 4788 |
def cleanup(): |
| 4789 |
global ET, pyET |
| 4790 |
ET = pyET = None |
| 4791 |
unittest.addModuleCleanup(cleanup) |
| 4792 |
|
| 4793 |
# Provide default namespace mapping and path cache. |
| 4794 |
from xml.etree import ElementPath |
| 4795 |
nsmap = ET.register_namespace._namespace_map |
| 4796 |
# Copy the default namespace mapping |
| 4797 |
nsmap_copy = nsmap.copy() |
| 4798 |
unittest.addModuleCleanup(nsmap.update, nsmap_copy) |
| 4799 |
unittest.addModuleCleanup(nsmap.clear) |
| 4800 |
|
| 4801 |
# Copy the path cache (should be empty) |
| 4802 |
path_cache = ElementPath._cache |
| 4803 |
unittest.addModuleCleanup(setattr, ElementPath, "_cache", path_cache) |
| 4804 |
ElementPath._cache = path_cache.copy() |
| 4805 |
|
| 4806 |
# Align the Comment/PI factories. |
| 4807 |
if hasattr(ET, '_set_factories'): |
| 4808 |
old_factories = ET._set_factories(ET.Comment, ET.PI) |
| 4809 |
unittest.addModuleCleanup(ET._set_factories, *old_factories) |
| 4810 |
|
| 4811 |
|
| 4812 |
if __name__ == '__main__': |
| 4813 |
unittest.main() |