1 import gc
2 import os
3 import os.path
4 import re
5 import sys
6 import tempfile
7 import unittest
8 from contextlib import contextmanager
9
10 try:
11 import urlparse
12 except ImportError:
13 import urllib.parse as urlparse
14
15 try:
16 from urllib import pathname2url
17 except:
18 from urllib.request import pathname2url
19
20 from lxml import etree, html
23 return tuple(
24 int(part) if part.isdigit() else part
25 for part in re.findall('([0-9]+|[^0-9.]+)', version_string)
26 )
27
28 IS_PYPY = (getattr(sys, 'implementation', None) == 'pypy' or
29 getattr(sys, 'pypy_version_info', None) is not None)
30
31 IS_PYTHON3 = sys.version_info[0] >= 3
32 IS_PYTHON2 = sys.version_info[0] < 3
33
34 from xml.etree import ElementTree
35
36 if hasattr(ElementTree, 'VERSION'):
37 ET_VERSION = make_version_tuple(ElementTree.VERSION)
38 else:
39 ET_VERSION = (0,0,0)
40
41 if IS_PYTHON2:
42 from xml.etree import cElementTree
43
44 if hasattr(cElementTree, 'VERSION'):
45 CET_VERSION = make_version_tuple(cElementTree.VERSION)
46 else:
47 CET_VERSION = (0,0,0)
48 else:
49 CET_VERSION = (0, 0, 0)
50 cElementTree = None
54 """Remove test methods that do not work with the current lib version.
55 """
56 find_required_version = version_dict.get
57 def dummy_test_method(self):
58 pass
59 for name in dir(test_class):
60 expected_version = find_required_version(name, (0,0,0))
61 if expected_version > current_version:
62 setattr(test_class, name, dummy_test_method)
63
64 import doctest
65
66 try:
67 next
68 except NameError:
71 else:
72 locals()['next'] = next
73
74
75 try:
76 import pytest
77 except ImportError:
79 "Using a class because a function would bind into a method when used in classes"
81 - def __call__(self, func, *args): return func
82 else:
83 skipif = pytest.mark.skipif
86 module = sys.modules[sys._getframe(frame_depth).f_globals['__name__']]
87 return os.path.normpath(os.path.join(
88 os.path.dirname(getattr(module, '__file__', '')), filename))
89
90 from io import StringIO
91
92 unichr_escape = re.compile(r'\\u[0-9a-fA-F]{4}|\\U[0-9a-fA-F]{8}')
93
94 if sys.version_info[0] >= 3:
95
96 from builtins import str as unicode
97 from codecs import unicode_escape_decode
98 _chr = chr
99 - def _str(s, encoding="UTF-8"):
100 return unichr_escape.sub(lambda x: unicode_escape_decode(x.group(0))[0], s)
101 - def _bytes(s, encoding="UTF-8"):
103 from io import BytesIO as _BytesIO
105 if args and isinstance(args[0], str):
106 args = (args[0].encode("UTF-8"),)
107 return _BytesIO(*args)
108
109 doctest_parser = doctest.DocTestParser()
110 _fix_unicode = re.compile(r'(\s+)u(["\'])').sub
111 _fix_exceptions = re.compile(r'(.*except [^(]*),\s*(.*:)').sub
120 else:
121
122 from __builtin__ import unicode
123 _chr = unichr
124 - def _str(s, encoding="UTF-8"):
129 - def _bytes(s, encoding="UTF-8"):
131 from io import BytesIO
132
133 doctest_parser = doctest.DocTestParser()
134 _fix_traceback = re.compile(r'^(\s*)(?:\w+\.)+(\w*(?:Error|Exception|Invalid):)', re.M).sub
135 _fix_exceptions = re.compile(r'(.*except [^(]*)\s+as\s+(.*:)').sub
136 _fix_bytes = re.compile(r'(\s+)b(["\'])').sub
146
147 try:
148 skipIf = unittest.skipIf
149 except AttributeError:
151 def _skip(thing):
152 import types
153 if isinstance(thing, (type, types.ClassType)):
154 return type(thing.__name__, (object,), {})
155 else:
156 return None
157 if condition:
158 return _skip
159 return lambda thing: thing
160
165
166 - def parse(self, text, parser=None):
169
173
177 self.xml_data = xml_data
178
179 - def read(self, amount=None):
180 if self.xml_data:
181 if amount:
182 data = self.xml_data[:amount]
183 self.xml_data = self.xml_data[amount:]
184 else:
185 data = self.xml_data
186 self.xml_data = _bytes('')
187 return data
188 return _bytes('')
189
191 - def __init__(self, charlen=100, depth=4, children=5):
192 self.data = BytesIO()
193 self.chars = _bytes('a') * charlen
194 self.children = range(children)
195 self.more = self.iterelements(depth)
196
198 yield _bytes('<root>')
199 depth -= 1
200 if depth > 0:
201 for child in self.children:
202 for element in self.iterelements(depth):
203 yield element
204 yield self.chars
205 else:
206 yield self.chars
207 yield _bytes('</root>')
208
209 - def read(self, amount=None):
227
229 - def __init__(self, charlen=100, depth=4, children=5):
234
236 yield _str('<root>')
237 depth -= 1
238 if depth > 0:
239 for child in self.children:
240 for element in self.iterelements(depth):
241 yield element
242 yield self.chars
243 else:
244 yield self.chars
245 yield _str('</root>')
246
248 _testdir = os.path.dirname(__file__)
249 return os.path.join(_testdir, name)
250
252 return urlparse.urljoin(
253 'file:', pathname2url(path))
254
257
262
266
269
271 tree = etree.parse(BytesIO(xml) if isinstance(xml, bytes) else StringIO(xml))
272 f = BytesIO()
273 tree.write_c14n(f)
274 return f.getvalue()
275
276
277 @contextmanager
278 -def tmpfile(**kwargs):
285