summaryrefslogtreecommitdiff
path: root/openid
diff options
context:
space:
mode:
authorVlastimil Zíma <vlastimil.zima@nic.cz>2018-01-26 16:17:41 +0100
committerVlastimil Zíma <vlastimil.zima@nic.cz>2018-03-07 15:06:24 +0100
commitff6b54f3b66bcad8e3e9beaab9a7e6ab492fbf2a (patch)
treeb6e53d54986aa441d0c91c38ef66500d3ff41e69 /openid
parent6006ed0594ed4b120ae7999d75d4594f65fc1f1c (diff)
downloadopenid-ff6b54f3b66bcad8e3e9beaab9a7e6ab492fbf2a.tar.gz
Use lxml for XML
Diffstat (limited to 'openid')
-rw-r--r--openid/message.py12
-rw-r--r--openid/oidutil.py43
-rw-r--r--openid/test/test_message.py14
-rw-r--r--openid/yadis/etxrd.py21
4 files changed, 9 insertions, 81 deletions
diff --git a/openid/message.py b/openid/message.py
index 378ffb5..ff8bcb5 100644
--- a/openid/message.py
+++ b/openid/message.py
@@ -8,14 +8,9 @@ import copy
import urllib
import warnings
-from openid import kvform, oidutil
+from lxml import etree as ElementTree
-try:
- ElementTree = oidutil.importElementTree()
-except ImportError:
- # No elementtree found, so give up, but don't fail to import,
- # since we have fallbacks.
- ElementTree = None
+from openid import kvform, oidutil
# This doesn't REALLY belong here, but where is better?
IDENTIFIER_SELECT = 'http://specs.openid.net/auth/2.0/identifier_select'
@@ -349,9 +344,6 @@ class Message(object):
encodes the values in this Message object.
@rtype: str or unicode
"""
- if ElementTree is None:
- raise RuntimeError('This function requires ElementTree.')
-
assert action_url is not None
form = ElementTree.Element(u'form')
diff --git a/openid/oidutil.py b/openid/oidutil.py
index 13954b7..70384d3 100644
--- a/openid/oidutil.py
+++ b/openid/oidutil.py
@@ -13,14 +13,6 @@ from urllib import urlencode
_LOGGER = logging.getLogger(__name__)
-elementtree_modules = [
- 'lxml.etree',
- 'xml.etree.cElementTree',
- 'xml.etree.ElementTree',
- 'cElementTree',
- 'elementtree.ElementTree',
-]
-
def toUnicode(value):
"""Returns the given argument as a unicode object.
@@ -54,41 +46,6 @@ for (var i = 0; i < elements.length; i++) {
""" % (title, form)
-def importElementTree(module_names=None):
- """Find a working ElementTree implementation, trying the standard
- places that such a thing might show up.
-
- >>> ElementTree = importElementTree()
-
- @param module_names: The names of modules to try to use as
- ElementTree. Defaults to C{L{elementtree_modules}}
-
- @returns: An ElementTree module
- """
- if module_names is None:
- module_names = elementtree_modules
-
- for mod_name in module_names:
- try:
- ElementTree = __import__(mod_name, None, None, ['unused'])
- except ImportError:
- pass
- else:
- # Make sure it can actually parse XML
- try:
- ElementTree.XML('<unused/>')
- except Exception:
- logging.exception('Not using ElementTree library %r because it failed to parse a trivial document: %s',
- mod_name)
- else:
- return ElementTree
- else:
- raise ImportError('No ElementTree library found. '
- 'You may need to install one. '
- 'Tried importing %r' % (module_names,)
- )
-
-
def log(message, level=0):
"""Handle a log message from the OpenID library.
diff --git a/openid/test/test_message.py b/openid/test/test_message.py
index 32d8ee6..f00182a 100644
--- a/openid/test/test_message.py
+++ b/openid/test/test_message.py
@@ -4,9 +4,9 @@ import urllib
import warnings
from urlparse import parse_qs
+from lxml import etree as ElementTree
from testfixtures import ShouldWarn
-from openid import oidutil
from openid.extensions import sreg
from openid.message import (BARE_NS, NULL_NAMESPACE, OPENID1_NS, OPENID2_NS, OPENID_NS, OPENID_PROTOCOL_FIELDS,
THE_OTHER_OPENID1_NS, InvalidNamespace, InvalidOpenIDNamespace, Message, NamespaceMap,
@@ -727,10 +727,8 @@ class MessageTest(unittest.TestCase):
def _checkForm(self, html, message_, action_url,
form_tag_attrs, submit_text):
- E = oidutil.importElementTree()
-
# Build element tree from HTML source
- input_tree = E.ElementTree(E.fromstring(html))
+ input_tree = ElementTree.ElementTree(ElementTree.fromstring(html))
# Get root element
form = input_tree.getroot()
@@ -803,14 +801,10 @@ class MessageTest(unittest.TestCase):
'ünicöde_key': 'ünicöde_välüe',
}
m = Message.fromPostArgs(postargs)
- # Calling m.toFormMarkup with lxml used for ElementTree will throw
- # a ValueError.
html = m.toFormMarkup(self.action_url, self.form_tag_attrs,
self.submit_text)
- # Using the (c)ElementTree from stdlib will result in the UTF-8
- # encoded strings to be converted to XML character references,
- # "ünicöde_key" becomes "&#195;&#188;nic&#195;&#182;de_key" and
- # "ünicöde_välüe" becomes "&#195;&#188;nic&#195;&#182;de_v&#195;&#164;l&#195;&#188;e"
+ self.assertIn('ünicöde_key', html)
+ self.assertIn('ünicöde_välüe', html)
self.assertNotIn('&#195;&#188;nic&#195;&#182;de_key', html,
'UTF-8 bytes should not convert to XML character references')
self.assertNotIn('&#195;&#188;nic&#195;&#182;de_v&#195;&#164;l&#195;&#188;e', html,
diff --git a/openid/yadis/etxrd.py b/openid/yadis/etxrd.py
index 563a1f2..a536617 100644
--- a/openid/yadis/etxrd.py
+++ b/openid/yadis/etxrd.py
@@ -19,27 +19,12 @@ __all__ = [
]
import random
-import sys
from datetime import datetime
from time import strptime
-from openid.oidutil import importElementTree
-from openid.yadis import xri
-
-ElementTree = importElementTree()
+from lxml import etree as ElementTree
-# the different elementtree modules don't have a common exception
-# model. We just want to be able to catch the exceptions that signify
-# malformed XML data and wrap them, so that the other library code
-# doesn't have to know which XML library we're using.
-try:
- # Make the parser raise an exception so we can sniff out the type
- # of exceptions
- ElementTree.XML('> purposely malformed XML <')
-except (MemoryError, AssertionError, ImportError):
- raise
-except Exception:
- XMLError = sys.exc_info()[0]
+from openid.yadis import xri
class XRDSError(Exception):
@@ -65,7 +50,7 @@ def parseXRDS(text):
"""
try:
element = ElementTree.XML(text)
- except XMLError as why:
+ except ElementTree.Error as why:
exc = XRDSError('Error parsing document as XML')
exc.reason = why
raise exc