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
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
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 :
67 else :
68 locals ( ) [ 'next' ] = next
69
70
71 try :
72 import pytest
73 except ImportError :
75 "Using a class because a function would bind into a method when used in classes"
77 - def __call__ ( self , func , * args ) : return func
78 else :
79 skipif = pytest . mark . skipif
80
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
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" ) :
99 from io import BytesIO as _BytesIO
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
116 else :
117
118 from __builtin__ import unicode
119 _chr = unichr
120 - def _str ( s , encoding = "UTF-8" ) :
125 - def _bytes ( s , encoding = "UTF-8" ) :
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
142
143 try :
144 skipIf = unittest . skipIf
145 except AttributeError :
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
161
162 - def parse ( self , text , parser = None ) :
165
169
170
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
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
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 ) :
223
225 - def __init__ ( self , charlen = 100 , depth = 4 , children = 5 ) :
230
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
244 _testdir = os . path . dirname ( __file__ )
245 return os . path . join ( _testdir , name )
246
248 return urlparse . urljoin (
249 'file:' , pathname2url ( path ) )
250
253
261
268
271
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
282