Skip to content

Instantly share code, notes, and snippets.

@mmaybeno
Last active October 28, 2015 20:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mmaybeno/b4e0ef7cb8515df70500 to your computer and use it in GitHub Desktop.
Save mmaybeno/b4e0ef7cb8515df70500 to your computer and use it in GitHub Desktop.
Python related
# ElementTree XML API _cache test
# Matt Maybeno 2015
import unittest
import xml.etree.ElementTree as ET
class TestElementTreeXml(unittest.TestCase):
def setUp(self):
self.xml1 = """<?xml version="1.0"?>
<actors xmlns:fictional="http://characters.example.com"
xmlns="http://people.example.com">
<actor>
<name>John Cleese</name>
<fictional:character>Lancelot</fictional:character>
<fictional:character>Archie Leach</fictional:character>
</actor>
<actor>
<name>Eric Idle</name>
<fictional:character>Sir Robin</fictional:character>
<fictional:character>Gunther</fictional:character>
<fictional:character>Commander Clement</fictional:character>
</actor>
</actors>
"""
self.ns1 = {'real_person': 'http://people.example.com',
'role': 'http://characters.example.com'}
self.xml2 = """<?xml version="1.0"?>
<actors xmlns:fictional="http://characters.example_2.com"
xmlns="http://people.example_2.com">
<actor>
<name>John Cleese</name>
<fictional:character>Lancelot</fictional:character>
<fictional:character>Archie Leach</fictional:character>
</actor>
<actor>
<name>Eric Idle</name>
<fictional:character>Sir Robin</fictional:character>
<fictional:character>Gunther</fictional:character>
<fictional:character>Commander Clement</fictional:character>
</actor>
</actors>
"""
self.ns2 = {'real_person': 'http://people.example_2.com',
'role': 'http://characters.example_2.com'}
# Pre find element to build ElementPath _cache
eltree = ET.fromstring(self.xml1)
eltree.find('.real_person:actor', self.ns1)
def test_xml_ns1(self):
"""
Finds the xml element using the namespace from the ns1 dictionary.
This works because it is using the existing _cache from the
namespace
"""
eltree1 = ET.fromstring(self.xml1)
self.assertIsNotNone(eltree1.find('.real_person:actor', self.ns1))
def test_xml_ns2(self):
"""
Attempts to find the xml element using the namespace from the
ns2 dictionary but fails because ns1 is already placed in
ElementPath _cache using the same dict keys.
https://hg.python.org/cpython/file/2.7/Lib/xml/etree/ElementPath.py#l235
"""
eltree2 = ET.fromstring(self.xml2)
# This should not return None I believe
self.assertIsNone(eltree2.find('.real_person:actor', self.ns2))
# Manually clear cache from the ElementPath module
ET.ElementPath._cache.clear()
self.assertIsNotNone(eltree2.find('.real_person:actor', self.ns2))
if __name__ == '__main__':
unittest.main()
@mmaybeno
Copy link
Author

given this bug report: http://bugs.python.org/issue17011, the issue is fixed in python > 3.3 and the test breaks when running it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment