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 try:
17 from elementtree import ElementTree
18 except ImportError:
19 try:
20 from xml.etree import ElementTree
21 except ImportError:
22 ElementTree = None
23
24 if hasattr(ElementTree, 'VERSION'):
25 ET_VERSION = make_version_tuple(ElementTree.VERSION)
26 else:
27 ET_VERSION = (0,0,0)
28
29 try:
30 import cElementTree
31 except ImportError:
32 try:
33 from xml.etree import cElementTree
34 except ImportError:
35 cElementTree = None
36
37 if hasattr(cElementTree, 'VERSION'):
38 CET_VERSION = make_version_tuple(cElementTree.VERSION)
39 else:
40 CET_VERSION = (0,0,0)
41
43 """Remove test methods that do not work with the current lib version.
44 """
45 find_required_version = version_dict.get
46 def dummy_test_method(self):
47 pass
48 for name in dir(test_class):
49 expected_version = find_required_version(name, (0,0,0))
50 if expected_version > current_version:
51 setattr(test_class, name, dummy_test_method)
52
53 try:
54 import doctest
55
56 doctest.DocFileSuite
57 doctest.DocTestParser
58 doctest.NORMALIZE_WHITESPACE
59 doctest.ELLIPSIS
60 except (ImportError, AttributeError):
61
62 import local_doctest as doctest
63
64 try:
65 sorted
66 except NameError:
68 seq = list(seq)
69 seq.sort(**kwargs)
70 return seq
71 else:
72 locals()['sorted'] = sorted
73
74 try:
75 import pytest
76 except ImportError:
78 "Using a class because a function would bind into a method when used in classes"
80 - def __call__(self, func, *args): return func
81 else:
82 skipif = pytest.mark.skipif
83
85 module = sys.modules[sys._getframe(frame_depth).f_globals['__name__']]
86 return os.path.normpath(os.path.join(
87 os.path.dirname(getattr(module, '__file__', '')), filename))
88
89 if sys.version_info[0] >= 3:
90
91 from builtins import str as unicode
92 - def _str(s, encoding="UTF-8"):
94 - def _bytes(s, encoding="UTF-8"):
96 from io import StringIO, BytesIO as _BytesIO
98 if args and isinstance(args[0], str):
99 args = (args[0].encode("UTF-8"),)
100 return _BytesIO(*args)
101
102 doctest_parser = doctest.DocTestParser()
103 _fix_unicode = re.compile(r'(\s+)u(["\'])').sub
104 _fix_exceptions = re.compile(r'(.*except [^(]*),\s*(.*:)').sub
113 else:
114
115 from __builtin__ import unicode
116 - def _str(s, encoding="UTF-8"):
118 - def _bytes(s, encoding="UTF-8"):
120 from StringIO import StringIO
121 BytesIO = StringIO
122
123 doctest_parser = doctest.DocTestParser()
124 _fix_traceback = re.compile(r'^(\s*)(?:\w+\.)+(\w*(?:Error|Exception|Invalid):)', re.M).sub
125 _fix_exceptions = re.compile(r'(.*except [^(]*)\s+as\s+(.*:)').sub
126 _fix_bytes = re.compile(r'(\s+)b(["\'])').sub
136
137 try:
138 skipIf = unittest.skipIf
139 except AttributeError:
140 - def skipIf(condition, why,
141 _skip=lambda test_method: None,
142 _keep=lambda test_method: test_method):
143 if condition:
144 return _skip
145 return _keep
146
150
151 - def parse(self, text, parser=None):
154
158
159
160 try:
161 unittest.TestCase.assertFalse
162 except AttributeError:
163 assertFalse = unittest.TestCase.failIf
164
167 self.xml_data = xml_data
168
169 - def read(self, amount=None):
170 if self.xml_data:
171 if amount:
172 data = self.xml_data[:amount]
173 self.xml_data = self.xml_data[amount:]
174 else:
175 data = self.xml_data
176 self.xml_data = _bytes('')
177 return data
178 return _bytes('')
179
181 - def __init__(self, charlen=100, depth=4, children=5):
182 self.data = BytesIO()
183 self.chars = _bytes('a') * charlen
184 self.children = range(children)
185 self.more = self.iterelements(depth)
186
188 yield _bytes('<root>')
189 depth -= 1
190 if depth > 0:
191 for child in self.children:
192 for element in self.iterelements(depth):
193 yield element
194 yield self.chars
195 else:
196 yield self.chars
197 yield _bytes('</root>')
198
199 - def read(self, amount=None):
200 data = self.data
201 append = data.write
202 if amount:
203 for element in self.more:
204 append(element)
205 if data.tell() >= amount:
206 break
207 else:
208 for element in self.more:
209 append(element)
210 result = data.getvalue()
211 data.seek(0)
212 data.truncate()
213 if amount:
214 append(result[amount:])
215 result = result[:amount]
216 return result
217
219 - def __init__(self, charlen=100, depth=4, children=5):
224
226 yield _str('<root>')
227 depth -= 1
228 if depth > 0:
229 for child in self.children:
230 for element in self.iterelements(depth):
231 yield element
232 yield self.chars
233 else:
234 yield self.chars
235 yield _str('</root>')
236
238 _testdir = os.path.dirname(__file__)
239 return os.path.join(_testdir, name)
240
248
255
258
264
269