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