1 import os
2 import os.path
3 import re
4 import gc
5 import sys
6 import unittest
7
8 try:
9 import urlparse
10 except ImportError:
11 import urllib.parse as urlparse
12
13 try:
14 from urllib import pathname2url
15 except:
16 from urllib.request import pathname2url
17
18 from lxml import etree
19
21 l = []
22 for part in re.findall('([0-9]+|[^0-9.]+)', version_string):
23 try:
24 l.append(int(part))
25 except ValueError:
26 l.append(part)
27 return tuple(l)
28
29 IS_PYPY = (getattr(sys, 'implementation', None) == 'pypy' or
30 getattr(sys, 'pypy_version_info', None) is not None)
31
32 IS_PYTHON3 = sys.version_info[0] >= 3
33
34 try:
35 from xml.etree import ElementTree
36 except ImportError:
37 try:
38 from elementtree import ElementTree
39 except ImportError:
40 ElementTree = None
41
42 if hasattr(ElementTree, 'VERSION'):
43 ET_VERSION = make_version_tuple(ElementTree.VERSION)
44 else:
45 ET_VERSION = (0,0,0)
46
47 try:
48 from xml.etree import cElementTree
49 except ImportError:
50 try:
51 import cElementTree
52 except ImportError:
53 cElementTree = None
54
55 if hasattr(cElementTree, 'VERSION'):
56 CET_VERSION = make_version_tuple(cElementTree.VERSION)
57 else:
58 CET_VERSION = (0,0,0)
59
61 """Remove test methods that do not work with the current lib version.
62 """
63 find_required_version = version_dict.get
64 def dummy_test_method(self):
65 pass
66 for name in dir(test_class):
67 expected_version = find_required_version(name, (0,0,0))
68 if expected_version > current_version:
69 setattr(test_class, name, dummy_test_method)
70
71 try:
72 import doctest
73
74 doctest.DocFileSuite
75 doctest.DocTestParser
76 doctest.NORMALIZE_WHITESPACE
77 doctest.ELLIPSIS
78 except (ImportError, AttributeError):
79
80 import local_doctest as doctest
81
82 try:
83 sorted
84 except NameError:
86 seq = list(seq)
87 seq.sort(**kwargs)
88 return seq
89 else:
90 locals()['sorted'] = sorted
91
92
93 try:
94 next
95 except NameError:
98 else:
99 locals()['next'] = next
100
101
102 try:
103 import pytest
104 except ImportError:
106 "Using a class because a function would bind into a method when used in classes"
108 - def __call__(self, func, *args): return func
109 else:
110 skipif = pytest.mark.skipif
111
113 module = sys.modules[sys._getframe(frame_depth).f_globals['__name__']]
114 return os.path.normpath(os.path.join(
115 os.path.dirname(getattr(module, '__file__', '')), filename))
116
117 if sys.version_info[0] >= 3:
118
119 from builtins import str as unicode
120 - def _str(s, encoding="UTF-8"):
122 - def _bytes(s, encoding="UTF-8"):
124 from io import StringIO, BytesIO as _BytesIO
126 if args and isinstance(args[0], str):
127 args = (args[0].encode("UTF-8"),)
128 return _BytesIO(*args)
129
130 doctest_parser = doctest.DocTestParser()
131 _fix_unicode = re.compile(r'(\s+)u(["\'])').sub
132 _fix_exceptions = re.compile(r'(.*except [^(]*),\s*(.*:)').sub
141 else:
142
143 from __builtin__ import unicode
144 - def _str(s, encoding="UTF-8"):
146 - def _bytes(s, encoding="UTF-8"):
148 from StringIO import StringIO
149 BytesIO = StringIO
150
151 doctest_parser = doctest.DocTestParser()
152 _fix_traceback = re.compile(r'^(\s*)(?:\w+\.)+(\w*(?:Error|Exception|Invalid):)', re.M).sub
153 _fix_exceptions = re.compile(r'(.*except [^(]*)\s+as\s+(.*:)').sub
154 _fix_bytes = re.compile(r'(\s+)b(["\'])').sub
164
165 try:
166 skipIf = unittest.skipIf
167 except AttributeError:
168 - def skipIf(condition, why,
169 _skip=lambda test_method: None,
170 _keep=lambda test_method: test_method):
171 if condition:
172 return _skip
173 return _keep
174
178
179 - def parse(self, text, parser=None):
182
186
187
188 try:
189 unittest.TestCase.assertFalse
190 except AttributeError:
191 assertFalse = unittest.TestCase.failIf
192
195 self.xml_data = xml_data
196
197 - def read(self, amount=None):
198 if self.xml_data:
199 if amount:
200 data = self.xml_data[:amount]
201 self.xml_data = self.xml_data[amount:]
202 else:
203 data = self.xml_data
204 self.xml_data = _bytes('')
205 return data
206 return _bytes('')
207
209 - def __init__(self, charlen=100, depth=4, children=5):
210 self.data = BytesIO()
211 self.chars = _bytes('a') * charlen
212 self.children = range(children)
213 self.more = self.iterelements(depth)
214
216 yield _bytes('<root>')
217 depth -= 1
218 if depth > 0:
219 for child in self.children:
220 for element in self.iterelements(depth):
221 yield element
222 yield self.chars
223 else:
224 yield self.chars
225 yield _bytes('</root>')
226
227 - def read(self, amount=None):
228 data = self.data
229 append = data.write
230 if amount:
231 for element in self.more:
232 append(element)
233 if data.tell() >= amount:
234 break
235 else:
236 for element in self.more:
237 append(element)
238 result = data.getvalue()
239 data.seek(0)
240 data.truncate()
241 if amount:
242 append(result[amount:])
243 result = result[:amount]
244 return result
245
247 - def __init__(self, charlen=100, depth=4, children=5):
252
254 yield _str('<root>')
255 depth -= 1
256 if depth > 0:
257 for child in self.children:
258 for element in self.iterelements(depth):
259 yield element
260 yield self.chars
261 else:
262 yield self.chars
263 yield _str('</root>')
264
266 _testdir = os.path.dirname(__file__)
267 return os.path.join(_testdir, name)
268
270 return urlparse.urljoin(
271 'file:', pathname2url(path))
272
275
283
290
293
299
304