diff options
author | Eli Bendersky <eliben@gmail.com> | 2012-05-29 03:02:56 (GMT) |
---|---|---|
committer | Eli Bendersky <eliben@gmail.com> | 2012-05-29 03:02:56 (GMT) |
commit | 737b173355b0473d134b1715dd8b1695eb023d8b (patch) | |
tree | 5921978a2a92a9298c2b01f93de7a94e6ee0d25a /Lib | |
parent | 6bed342b5895c816ac8d731d780ca6f01f8ea875 (diff) | |
download | cpython-737b173355b0473d134b1715dd8b1695eb023d8b.zip cpython-737b173355b0473d134b1715dd8b1695eb023d8b.tar.gz cpython-737b173355b0473d134b1715dd8b1695eb023d8b.tar.bz2 |
Issue 14814: Add namespaces keyword arg to find(*) methods in _elementtree.
Add attrib keyword to Element and SubElement in _elementtree.
Patch developed with Ezio Melotti.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/test/test_xml_etree.py | 66 | ||||
-rw-r--r-- | Lib/xml/etree/ElementTree.py | 3 |
2 files changed, 68 insertions, 1 deletions
diff --git a/Lib/test/test_xml_etree.py b/Lib/test/test_xml_etree.py index df1f771..cdba2b6 100644 --- a/Lib/test/test_xml_etree.py +++ b/Lib/test/test_xml_etree.py @@ -62,6 +62,22 @@ SAMPLE_XML_NS = """ </body> """ +SAMPLE_XML_NS_ELEMS = """ +<root> +<h:table xmlns:h="hello"> + <h:tr> + <h:td>Apples</h:td> + <h:td>Bananas</h:td> + </h:tr> +</h:table> + +<f:table xmlns:f="foo"> + <f:name>African Coffee Table</f:name> + <f:width>80</f:width> + <f:length>120</f:length> +</f:table> +</root> +""" def sanity(): """ @@ -1995,6 +2011,17 @@ class NoAcceleratorTest(unittest.TestCase): self.assertEqual(pyET.SubElement.__module__, 'xml.etree.ElementTree') +class NamespaceParseTest(unittest.TestCase): + def test_find_with_namespace(self): + nsmap = {'h': 'hello', 'f': 'foo'} + doc = ET.fromstring(SAMPLE_XML_NS_ELEMS) + + self.assertEqual(len(doc.findall('{hello}table', nsmap)), 1) + self.assertEqual(len(doc.findall('.//{hello}td', nsmap)), 2) + self.assertEqual(len(doc.findall('.//{foo}name', nsmap)), 1) + + + class ElementSlicingTest(unittest.TestCase): def _elem_tags(self, elemlist): return [e.tag for e in elemlist] @@ -2102,6 +2129,41 @@ class ParseErrorTest(unittest.TestCase): ERRORS.codes[ERRORS.XML_ERROR_SYNTAX]) +class KeywordArgsTest(unittest.TestCase): + # Test various issues with keyword arguments passed to ET.Element + # constructor and methods + def test_issue14818(self): + x = ET.XML("<a>foo</a>") + self.assertEqual(x.find('a', None), + x.find(path='a', namespaces=None)) + self.assertEqual(x.findtext('a', None, None), + x.findtext(path='a', default=None, namespaces=None)) + self.assertEqual(x.findall('a', None), + x.findall(path='a', namespaces=None)) + self.assertEqual(list(x.iterfind('a', None)), + list(x.iterfind(path='a', namespaces=None))) + + self.assertEqual(ET.Element('a').attrib, {}) + elements = [ + ET.Element('a', dict(href="#", id="foo")), + ET.Element('a', attrib=dict(href="#", id="foo")), + ET.Element('a', dict(href="#"), id="foo"), + ET.Element('a', href="#", id="foo"), + ET.Element('a', dict(href="#", id="foo"), href="#", id="foo"), + ] + for e in elements: + self.assertEqual(e.tag, 'a') + self.assertEqual(e.attrib, dict(href="#", id="foo")) + + e2 = ET.SubElement(elements[0], 'foobar', attrib={'key1': 'value1'}) + self.assertEqual(e2.attrib['key1'], 'value1') + + with self.assertRaisesRegex(TypeError, 'must be dict, not str'): + ET.Element('a', "I'm not a dict") + with self.assertRaisesRegex(TypeError, 'must be dict, not str'): + ET.Element('a', attrib="I'm not a dict") + + # -------------------------------------------------------------------- @@ -2157,7 +2219,9 @@ def test_main(module=pyET): StringIOTest, ParseErrorTest, ElementTreeTest, - TreeBuilderTest] + NamespaceParseTest, + TreeBuilderTest, + KeywordArgsTest] if module is pyET: # Run the tests specific to the Python implementation test_classes += [NoAcceleratorTest] diff --git a/Lib/xml/etree/ElementTree.py b/Lib/xml/etree/ElementTree.py index 5f974f6..e068fc2 100644 --- a/Lib/xml/etree/ElementTree.py +++ b/Lib/xml/etree/ElementTree.py @@ -205,6 +205,9 @@ class Element: # constructor def __init__(self, tag, attrib={}, **extra): + if not isinstance(attrib, dict): + raise TypeError("attrib must be dict, not %s" % ( + attrib.__class__.__name__,)) attrib = attrib.copy() attrib.update(extra) self.tag = tag |