mirror of
https://github.com/python/cpython.git
synced 2025-08-03 16:39:00 +00:00
Issue #6472: The xml.etree package is updated to ElementTree 1.3. The cElementTree module is updated too.
This commit is contained in:
parent
4478662f83
commit
3e8c189faa
11 changed files with 3323 additions and 1207 deletions
7
Lib/test/samples/simple-ns.xml
Normal file
7
Lib/test/samples/simple-ns.xml
Normal file
|
@ -0,0 +1,7 @@
|
|||
<?pi data?>
|
||||
<!-- comment -->
|
||||
<root xmlns='namespace'>
|
||||
<element key='value'>text</element>
|
||||
<element>text</element>tail
|
||||
<empty-element/>
|
||||
</root>
|
6
Lib/test/samples/simple.xml
Normal file
6
Lib/test/samples/simple.xml
Normal file
|
@ -0,0 +1,6 @@
|
|||
<!-- comment -->
|
||||
<root>
|
||||
<element key='value'>text</element>
|
||||
<element>text</element>tail
|
||||
<empty-element/>
|
||||
</root>
|
File diff suppressed because it is too large
Load diff
|
@ -1,30 +1,11 @@
|
|||
# xml.etree test for cElementTree
|
||||
|
||||
import sys
|
||||
|
||||
from test import test_support
|
||||
|
||||
ET = test_support.import_module('xml.etree.cElementTree')
|
||||
cET = test_support.import_module('xml.etree.cElementTree')
|
||||
|
||||
SAMPLE_XML = """
|
||||
<body>
|
||||
<tag>text</tag>
|
||||
<tag />
|
||||
<section>
|
||||
<tag>subtext</tag>
|
||||
</section>
|
||||
</body>
|
||||
"""
|
||||
|
||||
SAMPLE_XML_NS = """
|
||||
<body xmlns="http://effbot.org/ns">
|
||||
<tag>text</tag>
|
||||
<tag />
|
||||
<section>
|
||||
<tag>subtext</tag>
|
||||
</section>
|
||||
</body>
|
||||
"""
|
||||
# cElementTree specific tests
|
||||
|
||||
def sanity():
|
||||
"""
|
||||
|
@ -33,191 +14,21 @@ def sanity():
|
|||
>>> from xml.etree import cElementTree
|
||||
"""
|
||||
|
||||
def check_method(method):
|
||||
if not hasattr(method, '__call__'):
|
||||
print method, "not callable"
|
||||
|
||||
def serialize(ET, elem, encoding=None):
|
||||
import StringIO
|
||||
file = StringIO.StringIO()
|
||||
tree = ET.ElementTree(elem)
|
||||
if encoding:
|
||||
tree.write(file, encoding)
|
||||
else:
|
||||
tree.write(file)
|
||||
return file.getvalue()
|
||||
|
||||
def summarize(elem):
|
||||
return elem.tag
|
||||
|
||||
def summarize_list(seq):
|
||||
return map(summarize, seq)
|
||||
|
||||
def interface():
|
||||
"""
|
||||
Test element tree interface.
|
||||
|
||||
>>> element = ET.Element("tag", key="value")
|
||||
>>> tree = ET.ElementTree(element)
|
||||
|
||||
Make sure all standard element methods exist.
|
||||
|
||||
>>> check_method(element.append)
|
||||
>>> check_method(element.insert)
|
||||
>>> check_method(element.remove)
|
||||
>>> check_method(element.getchildren)
|
||||
>>> check_method(element.find)
|
||||
>>> check_method(element.findall)
|
||||
>>> check_method(element.findtext)
|
||||
>>> check_method(element.clear)
|
||||
>>> check_method(element.get)
|
||||
>>> check_method(element.set)
|
||||
>>> check_method(element.keys)
|
||||
>>> check_method(element.items)
|
||||
>>> check_method(element.getiterator)
|
||||
|
||||
Basic method sanity checks.
|
||||
|
||||
>>> serialize(ET, element) # 1
|
||||
'<tag key="value" />'
|
||||
>>> subelement = ET.Element("subtag")
|
||||
>>> element.append(subelement)
|
||||
>>> serialize(ET, element) # 2
|
||||
'<tag key="value"><subtag /></tag>'
|
||||
>>> element.insert(0, subelement)
|
||||
>>> serialize(ET, element) # 3
|
||||
'<tag key="value"><subtag /><subtag /></tag>'
|
||||
>>> element.remove(subelement)
|
||||
>>> serialize(ET, element) # 4
|
||||
'<tag key="value"><subtag /></tag>'
|
||||
>>> element.remove(subelement)
|
||||
>>> serialize(ET, element) # 5
|
||||
'<tag key="value" />'
|
||||
>>> element.remove(subelement)
|
||||
Traceback (most recent call last):
|
||||
ValueError: list.remove(x): x not in list
|
||||
>>> serialize(ET, element) # 6
|
||||
'<tag key="value" />'
|
||||
"""
|
||||
|
||||
def find():
|
||||
"""
|
||||
Test find methods (including xpath syntax).
|
||||
|
||||
>>> elem = ET.XML(SAMPLE_XML)
|
||||
>>> elem.find("tag").tag
|
||||
'tag'
|
||||
>>> ET.ElementTree(elem).find("tag").tag
|
||||
'tag'
|
||||
>>> elem.find("section/tag").tag
|
||||
'tag'
|
||||
>>> ET.ElementTree(elem).find("section/tag").tag
|
||||
'tag'
|
||||
>>> elem.findtext("tag")
|
||||
'text'
|
||||
>>> elem.findtext("tog")
|
||||
>>> elem.findtext("tog", "default")
|
||||
'default'
|
||||
>>> ET.ElementTree(elem).findtext("tag")
|
||||
'text'
|
||||
>>> elem.findtext("section/tag")
|
||||
'subtext'
|
||||
>>> ET.ElementTree(elem).findtext("section/tag")
|
||||
'subtext'
|
||||
>>> summarize_list(elem.findall("tag"))
|
||||
['tag', 'tag']
|
||||
>>> summarize_list(elem.findall("*"))
|
||||
['tag', 'tag', 'section']
|
||||
>>> summarize_list(elem.findall(".//tag"))
|
||||
['tag', 'tag', 'tag']
|
||||
>>> summarize_list(elem.findall("section/tag"))
|
||||
['tag']
|
||||
>>> summarize_list(elem.findall("section//tag"))
|
||||
['tag']
|
||||
>>> summarize_list(elem.findall("section/*"))
|
||||
['tag']
|
||||
>>> summarize_list(elem.findall("section//*"))
|
||||
['tag']
|
||||
>>> summarize_list(elem.findall("section/.//*"))
|
||||
['tag']
|
||||
>>> summarize_list(elem.findall("*/*"))
|
||||
['tag']
|
||||
>>> summarize_list(elem.findall("*//*"))
|
||||
['tag']
|
||||
>>> summarize_list(elem.findall("*/tag"))
|
||||
['tag']
|
||||
>>> summarize_list(elem.findall("*/./tag"))
|
||||
['tag']
|
||||
>>> summarize_list(elem.findall("./tag"))
|
||||
['tag', 'tag']
|
||||
>>> summarize_list(elem.findall(".//tag"))
|
||||
['tag', 'tag', 'tag']
|
||||
>>> summarize_list(elem.findall("././tag"))
|
||||
['tag', 'tag']
|
||||
>>> summarize_list(ET.ElementTree(elem).findall("/tag"))
|
||||
['tag', 'tag']
|
||||
>>> summarize_list(ET.ElementTree(elem).findall("./tag"))
|
||||
['tag', 'tag']
|
||||
>>> elem = ET.XML(SAMPLE_XML_NS)
|
||||
>>> summarize_list(elem.findall("tag"))
|
||||
[]
|
||||
>>> summarize_list(elem.findall("{http://effbot.org/ns}tag"))
|
||||
['{http://effbot.org/ns}tag', '{http://effbot.org/ns}tag']
|
||||
>>> summarize_list(elem.findall(".//{http://effbot.org/ns}tag"))
|
||||
['{http://effbot.org/ns}tag', '{http://effbot.org/ns}tag', '{http://effbot.org/ns}tag']
|
||||
"""
|
||||
|
||||
def parseliteral():
|
||||
r"""
|
||||
|
||||
>>> element = ET.XML("<html><body>text</body></html>")
|
||||
>>> ET.ElementTree(element).write(sys.stdout)
|
||||
<html><body>text</body></html>
|
||||
>>> element = ET.fromstring("<html><body>text</body></html>")
|
||||
>>> ET.ElementTree(element).write(sys.stdout)
|
||||
<html><body>text</body></html>
|
||||
>>> print ET.tostring(element)
|
||||
<html><body>text</body></html>
|
||||
>>> print ET.tostring(element, "ascii")
|
||||
<?xml version='1.0' encoding='ascii'?>
|
||||
<html><body>text</body></html>
|
||||
>>> _, ids = ET.XMLID("<html><body>text</body></html>")
|
||||
>>> len(ids)
|
||||
0
|
||||
>>> _, ids = ET.XMLID("<html><body id='body'>text</body></html>")
|
||||
>>> len(ids)
|
||||
1
|
||||
>>> ids["body"].tag
|
||||
'body'
|
||||
"""
|
||||
|
||||
def check_encoding(encoding):
|
||||
"""
|
||||
>>> check_encoding("ascii")
|
||||
>>> check_encoding("us-ascii")
|
||||
>>> check_encoding("iso-8859-1")
|
||||
>>> check_encoding("iso-8859-15")
|
||||
>>> check_encoding("cp437")
|
||||
>>> check_encoding("mac-roman")
|
||||
"""
|
||||
ET.XML(
|
||||
"<?xml version='1.0' encoding='%s'?><xml />" % encoding
|
||||
)
|
||||
|
||||
def bug_1534630():
|
||||
"""
|
||||
>>> bob = ET.TreeBuilder()
|
||||
>>> e = bob.data("data")
|
||||
>>> e = bob.start("tag", {})
|
||||
>>> e = bob.end("tag")
|
||||
>>> e = bob.close()
|
||||
>>> serialize(ET, e)
|
||||
'<tag />'
|
||||
"""
|
||||
|
||||
def test_main():
|
||||
from test import test_xml_etree_c
|
||||
from test import test_xml_etree, test_xml_etree_c
|
||||
|
||||
# Run the tests specific to the C implementation
|
||||
test_support.run_doctest(test_xml_etree_c, verbosity=True)
|
||||
|
||||
# Assign the C implementation before running the doctests
|
||||
pyET = test_xml_etree.ET
|
||||
test_xml_etree.ET = cET
|
||||
try:
|
||||
# Run the same test suite as xml.etree.ElementTree
|
||||
test_xml_etree.test_main(module_name='xml.etree.cElementTree')
|
||||
finally:
|
||||
test_xml_etree.ET = pyET
|
||||
|
||||
if __name__ == '__main__':
|
||||
test_main()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue