@@ -26,7 +26,7 @@ def __init__(self, *parts, **kwargs):
26
26
for part in parts :
27
27
if not part :
28
28
partlines = []
29
- if isinstance (part , Source ):
29
+ elif isinstance (part , Source ):
30
30
partlines = part .lines
31
31
elif isinstance (part , (tuple , list )):
32
32
partlines = [x .rstrip ("\n " ) for x in part ]
@@ -98,14 +98,14 @@ def indent(self, indent=' ' * 4):
98
98
newsource .lines = [(indent + line ) for line in self .lines ]
99
99
return newsource
100
100
101
- def getstatement (self , lineno , assertion = False ):
101
+ def getstatement (self , lineno ):
102
102
""" return Source statement which contains the
103
103
given linenumber (counted from 0).
104
104
"""
105
- start , end = self .getstatementrange (lineno , assertion )
105
+ start , end = self .getstatementrange (lineno )
106
106
return self [start :end ]
107
107
108
- def getstatementrange (self , lineno , assertion = False ):
108
+ def getstatementrange (self , lineno ):
109
109
""" return (start, end) tuple which spans the minimal
110
110
statement region which containing the given lineno.
111
111
"""
@@ -131,13 +131,7 @@ def isparseable(self, deindent=True):
131
131
""" return True if source is parseable, heuristically
132
132
deindenting it by default.
133
133
"""
134
- try :
135
- import parser
136
- except ImportError :
137
- def syntax_checker (x ):
138
- return compile (x , 'asd' , 'exec' )
139
- else :
140
- syntax_checker = parser .suite
134
+ from parser import suite as syntax_checker
141
135
142
136
if deindent :
143
137
source = str (self .deindent ())
@@ -219,9 +213,9 @@ def getfslineno(obj):
219
213
""" Return source location (path, lineno) for the given object.
220
214
If the source cannot be determined return ("", -1)
221
215
"""
222
- import _pytest . _code
216
+ from . code import Code
223
217
try :
224
- code = _pytest . _code . Code (obj )
218
+ code = Code (obj )
225
219
except TypeError :
226
220
try :
227
221
fn = inspect .getsourcefile (obj ) or inspect .getfile (obj )
@@ -259,8 +253,8 @@ def findsource(obj):
259
253
260
254
261
255
def getsource (obj , ** kwargs ):
262
- import _pytest . _code
263
- obj = _pytest . _code . getrawcode (obj )
256
+ from . code import getrawcode
257
+ obj = getrawcode (obj )
264
258
try :
265
259
strsrc = inspect .getsource (obj )
266
260
except IndentationError :
@@ -286,8 +280,6 @@ def deindent(lines, offset=None):
286
280
def readline_generator (lines ):
287
281
for line in lines :
288
282
yield line + '\n '
289
- while True :
290
- yield ''
291
283
292
284
it = readline_generator (lines )
293
285
@@ -318,9 +310,9 @@ def get_statement_startend2(lineno, node):
318
310
# AST's line numbers start indexing at 1
319
311
values = []
320
312
for x in ast .walk (node ):
321
- if isinstance (x , ast .stmt ) or isinstance ( x , ast .ExceptHandler ):
313
+ if isinstance (x , ( ast .stmt , ast .ExceptHandler ) ):
322
314
values .append (x .lineno - 1 )
323
- for name in "finalbody" , "orelse" :
315
+ for name in ( "finalbody" , "orelse" ) :
324
316
val = getattr (x , name , None )
325
317
if val :
326
318
# treat the finally/orelse part as its own statement
@@ -338,11 +330,8 @@ def get_statement_startend2(lineno, node):
338
330
def getstatementrange_ast (lineno , source , assertion = False , astnode = None ):
339
331
if astnode is None :
340
332
content = str (source )
341
- try :
342
- astnode = compile (content , "source" , "exec" , 1024 ) # 1024 for AST
343
- except ValueError :
344
- start , end = getstatementrange_old (lineno , source , assertion )
345
- return None , start , end
333
+ astnode = compile (content , "source" , "exec" , 1024 ) # 1024 for AST
334
+
346
335
start , end = get_statement_startend2 (lineno , astnode )
347
336
# we need to correct the end:
348
337
# - ast-parsing strips comments
@@ -374,38 +363,3 @@ def getstatementrange_ast(lineno, source, assertion=False, astnode=None):
374
363
else :
375
364
break
376
365
return astnode , start , end
377
-
378
-
379
- def getstatementrange_old (lineno , source , assertion = False ):
380
- """ return (start, end) tuple which spans the minimal
381
- statement region which containing the given lineno.
382
- raise an IndexError if no such statementrange can be found.
383
- """
384
- # XXX this logic is only used on python2.4 and below
385
- # 1. find the start of the statement
386
- from codeop import compile_command
387
- for start in range (lineno , - 1 , - 1 ):
388
- if assertion :
389
- line = source .lines [start ]
390
- # the following lines are not fully tested, change with care
391
- if 'super' in line and 'self' in line and '__init__' in line :
392
- raise IndexError ("likely a subclass" )
393
- if "assert" not in line and "raise" not in line :
394
- continue
395
- trylines = source .lines [start :lineno + 1 ]
396
- # quick hack to prepare parsing an indented line with
397
- # compile_command() (which errors on "return" outside defs)
398
- trylines .insert (0 , 'def xxx():' )
399
- trysource = '\n ' .join (trylines )
400
- # ^ space here
401
- try :
402
- compile_command (trysource )
403
- except (SyntaxError , OverflowError , ValueError ):
404
- continue
405
-
406
- # 2. find the end of the statement
407
- for end in range (lineno + 1 , len (source ) + 1 ):
408
- trysource = source [start :end ]
409
- if trysource .isparseable ():
410
- return start , end
411
- raise SyntaxError ("no valid source range around line %d " % (lineno ,))
0 commit comments