Package lxml :: Package tests :: Module common_imports
[hide private]
[frames] | no frames]

Source Code for Module lxml.tests.common_imports

  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, html 
 19   
20 -def make_version_tuple(version_string):
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 IS_PYTHON2 = sys.version_info[0] < 3 34 35 from xml.etree import ElementTree 36 37 if hasattr(ElementTree, 'VERSION'): 38 ET_VERSION = make_version_tuple(ElementTree.VERSION) 39 else: 40 ET_VERSION = (0,0,0) 41 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
49 -def filter_by_version(test_class, version_dict, current_version):
50 """Remove test methods that do not work with the current lib version. 51 """ 52 find_required_version = version_dict.get 53 def dummy_test_method(self): 54 pass
55 for name in dir(test_class): 56 expected_version = find_required_version(name, (0,0,0)) 57 if expected_version > current_version: 58 setattr(test_class, name, dummy_test_method) 59 60 import doctest 61 62 try: 63 next 64 except NameError:
65 - def next(it):
66 return it.next()
67 else: 68 locals()['next'] = next 69 70 71 try: 72 import pytest 73 except ImportError:
74 - class skipif(object):
75 "Using a class because a function would bind into a method when used in classes"
76 - def __init__(self, *args): pass
77 - def __call__(self, func, *args): return func
78 else: 79 skipif = pytest.mark.skipif 80
81 -def _get_caller_relative_path(filename, frame_depth=2):
82 module = sys.modules[sys._getframe(frame_depth).f_globals['__name__']] 83 return os.path.normpath(os.path.join( 84 os.path.dirname(getattr(module, '__file__', '')), filename))
85 86 from io import StringIO 87 88 unichr_escape = re.compile(r'\\u[0-9a-fA-F]{4}|\\U[0-9a-fA-F]{8}') 89 90 if sys.version_info[0] >= 3: 91 # Python 3 92 from builtins import str as unicode 93 from codecs import unicode_escape_decode 94 _chr = chr
95 - def _str(s, encoding="UTF-8"):
96 return unichr_escape.sub(lambda x: unicode_escape_decode(x.group(0))[0], s)
97 - def _bytes(s, encoding="UTF-8"):
98 return s.encode(encoding)
99 from io import BytesIO as _BytesIO
100 - def BytesIO(*args):
101 if args and isinstance(args[0], str): 102 args = (args[0].encode("UTF-8"),) 103 return _BytesIO(*args)
104 105 doctest_parser = doctest.DocTestParser() 106 _fix_unicode = re.compile(r'(\s+)u(["\'])').sub 107 _fix_exceptions = re.compile(r'(.*except [^(]*),\s*(.*:)').sub
108 - def make_doctest(filename):
109 filename = _get_caller_relative_path(filename) 110 doctests = read_file(filename) 111 doctests = _fix_unicode(r'\1\2', doctests) 112 doctests = _fix_exceptions(r'\1 as \2', doctests) 113 return doctest.DocTestCase( 114 doctest_parser.get_doctest( 115 doctests, {}, os.path.basename(filename), filename, 0))
116 else: 117 # Python 2 118 from __builtin__ import unicode 119 _chr = unichr
120 - def _str(s, encoding="UTF-8"):
121 s = unicode(s, encoding=encoding) 122 return unichr_escape.sub(lambda x: 123 x.group(0).decode('unicode-escape'), 124 s)
125 - def _bytes(s, encoding="UTF-8"):
126 return s
127 from io import BytesIO 128 129 doctest_parser = doctest.DocTestParser() 130 _fix_traceback = re.compile(r'^(\s*)(?:\w+\.)+(\w*(?:Error|Exception|Invalid):)', re.M).sub 131 _fix_exceptions = re.compile(r'(.*except [^(]*)\s+as\s+(.*:)').sub 132 _fix_bytes = re.compile(r'(\s+)b(["\'])').sub
133 - def make_doctest(filename):
134 filename = _get_caller_relative_path(filename) 135 doctests = read_file(filename) 136 doctests = _fix_traceback(r'\1\2', doctests) 137 doctests = _fix_exceptions(r'\1, \2', doctests) 138 doctests = _fix_bytes(r'\1\2', doctests) 139 return doctest.DocTestCase( 140 doctest_parser.get_doctest( 141 doctests, {}, os.path.basename(filename), filename, 0))
142 143 try: 144 skipIf = unittest.skipIf 145 except AttributeError:
146 - def skipIf(condition, why):
147 def _skip(thing): 148 import types 149 if isinstance(thing, (type, types.ClassType)): 150 return type(thing.__name__, (object,), {}) 151 else: 152 return None
153 if condition: 154 return _skip 155 return lambda thing: thing 156 157
158 -class HelperTestCase(unittest.TestCase):
159 - def tearDown(self):
160 gc.collect()
161
162 - def parse(self, text, parser=None):
163 f = BytesIO(text) if isinstance(text, bytes) else StringIO(text) 164 return etree.parse(f, parser=parser)
165
166 - def _rootstring(self, tree):
167 return etree.tostring(tree.getroot()).replace( 168 _bytes(' '), _bytes('')).replace(_bytes('\n'), _bytes(''))
169 170
171 -class SillyFileLike:
172 - def __init__(self, xml_data=_bytes('<foo><bar/></foo>')):
173 self.xml_data = xml_data
174
175 - def read(self, amount=None):
176 if self.xml_data: 177 if amount: 178 data = self.xml_data[:amount] 179 self.xml_data = self.xml_data[amount:] 180 else: 181 data = self.xml_data 182 self.xml_data = _bytes('') 183 return data 184 return _bytes('')
185
186 -class LargeFileLike:
187 - def __init__(self, charlen=100, depth=4, children=5):
188 self.data = BytesIO() 189 self.chars = _bytes('a') * charlen 190 self.children = range(children) 191 self.more = self.iterelements(depth)
192
193 - def iterelements(self, depth):
194 yield _bytes('<root>') 195 depth -= 1 196 if depth > 0: 197 for child in self.children: 198 for element in self.iterelements(depth): 199 yield element 200 yield self.chars 201 else: 202 yield self.chars 203 yield _bytes('</root>')
204
205 - def read(self, amount=None):
206 data = self.data 207 append = data.write 208 if amount: 209 for element in self.more: 210 append(element) 211 if data.tell() >= amount: 212 break 213 else: 214 for element in self.more: 215 append(element) 216 result = data.getvalue() 217 data.seek(0) 218 data.truncate() 219 if amount: 220 append(result[amount:]) 221 result = result[:amount] 222 return result
223
224 -class LargeFileLikeUnicode(LargeFileLike):
225 - def __init__(self, charlen=100, depth=4, children=5):
226 LargeFileLike.__init__(self, charlen, depth, children) 227 self.data = StringIO() 228 self.chars = _str('a') * charlen 229 self.more = self.iterelements(depth)
230
231 - def iterelements(self, depth):
232 yield _str('<root>') 233 depth -= 1 234 if depth > 0: 235 for child in self.children: 236 for element in self.iterelements(depth): 237 yield element 238 yield self.chars 239 else: 240 yield self.chars 241 yield _str('</root>')
242
243 -def fileInTestDir(name):
244 _testdir = os.path.dirname(__file__) 245 return os.path.join(_testdir, name)
246
247 -def path2url(path):
248 return urlparse.urljoin( 249 'file:', pathname2url(path))
250
251 -def fileUrlInTestDir(name):
252 return path2url(fileInTestDir(name))
253
254 -def read_file(name, mode='r'):
255 f = open(name, mode) 256 try: 257 data = f.read() 258 finally: 259 f.close() 260 return data
261
262 -def write_to_file(name, data, mode='w'):
263 f = open(name, mode) 264 try: 265 data = f.write(data) 266 finally: 267 f.close()
268
269 -def readFileInTestDir(name, mode='r'):
270 return read_file(fileInTestDir(name), mode)
271
272 -def canonicalize(xml):
273 tree = etree.parse(BytesIO(xml) if isinstance(xml, bytes) else StringIO(xml)) 274 f = BytesIO() 275 tree.write_c14n(f) 276 return f.getvalue()
277
278 -def unentitify(xml):
279 for entity_name, value in re.findall("(&#([0-9]+);)", xml): 280 xml = xml.replace(entity_name, unichr(int(value))) 281 return xml
282