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
75 module = sys.modules[sys._getframe(frame_depth).f_globals['__name__']]
76 return os.path.normpath(os.path.join(
77 os.path.dirname(getattr(module, '__file__', '')), filename))
78
79 if sys.version_info[0] >= 3:
80
81 from builtins import str as unicode
82 - def _str(s, encoding="UTF-8"):
84 - def _bytes(s, encoding="UTF-8"):
86 from io import StringIO, BytesIO as _BytesIO
88 if args and isinstance(args[0], str):
89 args = (args[0].encode("UTF-8"),)
90 return _BytesIO(*args)
91
92 doctest_parser = doctest.DocTestParser()
93 _fix_unicode = re.compile(r'(\s+)u(["\'])').sub
94 _fix_exceptions = re.compile(r'(.*except [^(]*),\s*(.*:)').sub
103 else:
104
105 from __builtin__ import unicode
106 - def _str(s, encoding="UTF-8"):
108 - def _bytes(s, encoding="UTF-8"):
110 from StringIO import StringIO
111 BytesIO = StringIO
112
113 doctest_parser = doctest.DocTestParser()
114 _fix_traceback = re.compile(r'^(\s*)(?:\w+\.)+(\w*(?:Error|Exception|Invalid):)', re.M).sub
115 _fix_exceptions = re.compile(r'(.*except [^(]*)\s+as\s+(.*:)').sub
116 _fix_bytes = re.compile(r'(\s+)b(["\'])').sub
126
130
131 - def parse(self, text, parser=None):
134
138
139
140 try:
141 unittest.TestCase.assertFalse
142 except AttributeError:
143 assertFalse = unittest.TestCase.failIf
144
147 self.xml_data = xml_data
148
149 - def read(self, amount=None):
150 if self.xml_data:
151 if amount:
152 data = self.xml_data[:amount]
153 self.xml_data = self.xml_data[amount:]
154 else:
155 data = self.xml_data
156 self.xml_data = _bytes('')
157 return data
158 return _bytes('')
159
161 - def __init__(self, charlen=100, depth=4, children=5):
162 self.data = BytesIO()
163 self.chars = _bytes('a') * charlen
164 self.children = range(children)
165 self.more = self.iterelements(depth)
166
168 yield _bytes('<root>')
169 depth -= 1
170 if depth > 0:
171 for child in self.children:
172 for element in self.iterelements(depth):
173 yield element
174 yield self.chars
175 else:
176 yield self.chars
177 yield _bytes('</root>')
178
179 - def read(self, amount=None):
180 data = self.data
181 append = data.write
182 if amount:
183 for element in self.more:
184 append(element)
185 if data.tell() >= amount:
186 break
187 else:
188 for element in self.more:
189 append(element)
190 result = data.getvalue()
191 data.seek(0)
192 data.truncate()
193 if amount:
194 append(result[amount:])
195 result = result[:amount]
196 return result
197
199 - def __init__(self, charlen=100, depth=4, children=5):
204
206 yield _str('<root>')
207 depth -= 1
208 if depth > 0:
209 for child in self.children:
210 for element in self.iterelements(depth):
211 yield element
212 yield self.chars
213 else:
214 yield self.chars
215 yield _str('</root>')
216
218 _testdir = os.path.dirname(__file__)
219 return os.path.join(_testdir, name)
220
228
235
238
244
249