1
1
# -*- coding: utf-8 -*-
2
2
3
3
import py
4
+ import pytest
5
+ from test_source import astonly
6
+
4
7
from py ._code .code import FormattedExcinfo , ReprExceptionInfo
5
8
queue = py .builtin ._tryimport ('queue' , 'Queue' )
6
9
7
10
failsonjython = py .test .mark .xfail ("sys.platform.startswith('java')" )
8
- from test_source import astonly
9
11
10
12
try :
11
13
import importlib
14
16
else :
15
17
invalidate_import_caches = getattr (importlib , "invalidate_caches" , None )
16
18
17
- import pytest
19
+
18
20
pytest_version_info = tuple (map (int , pytest .__version__ .split ("." )[:3 ]))
19
21
22
+
20
23
class TWMock :
21
24
def __init__ (self ):
22
25
self .lines = []
26
+
23
27
def sep (self , sep , line = None ):
24
28
self .lines .append ((sep , line ))
29
+
25
30
def line (self , line , ** kw ):
26
31
self .lines .append (line )
32
+
27
33
def markup (self , text , ** kw ):
28
34
return text
29
35
30
36
fullwidth = 80
31
37
38
+
32
39
def test_excinfo_simple ():
33
40
try :
34
41
raise ValueError
35
42
except ValueError :
36
43
info = py .code .ExceptionInfo ()
37
44
assert info .type == ValueError
38
45
46
+
39
47
def test_excinfo_getstatement ():
40
48
def g ():
41
49
raise ValueError
50
+
42
51
def f ():
43
52
g ()
44
53
try :
45
54
f ()
46
55
except ValueError :
47
56
excinfo = py .code .ExceptionInfo ()
48
- linenumbers = [py .code .getrawcode (f ).co_firstlineno - 1 + 3 ,
49
- py .code .getrawcode (f ).co_firstlineno - 1 + 1 ,
50
- py .code .getrawcode (g ).co_firstlineno - 1 + 1 ,]
57
+ linenumbers = [
58
+ py .code .getrawcode (f ).co_firstlineno - 1 + 3 ,
59
+ py .code .getrawcode (f ).co_firstlineno - 1 + 1 ,
60
+ py .code .getrawcode (g ).co_firstlineno - 1 + 1 ,
61
+ ]
51
62
l = list (excinfo .traceback )
52
63
foundlinenumbers = [x .lineno for x in l ]
53
64
assert foundlinenumbers == linenumbers
@@ -92,7 +103,7 @@ def test_traceback_entries(self):
92
103
93
104
def test_traceback_entry_getsource (self ):
94
105
tb = self .excinfo .traceback
95
- s = str (tb [- 1 ].getsource () )
106
+ s = str (tb [- 1 ].getsource ())
96
107
assert s .startswith ("def f():" )
97
108
assert s .endswith ("raise ValueError" )
98
109
@@ -164,10 +175,12 @@ def f(n):
164
175
def test_traceback_no_recursion_index (self ):
165
176
def do_stuff ():
166
177
raise RuntimeError
178
+
167
179
def reraise_me ():
168
180
import sys
169
181
exc , val , tb = sys .exc_info ()
170
182
py .builtin ._reraise (exc , val , tb )
183
+
171
184
def f (n ):
172
185
try :
173
186
do_stuff ()
@@ -179,7 +192,7 @@ def f(n):
179
192
assert recindex is None
180
193
181
194
def test_traceback_messy_recursion (self ):
182
- #XXX: simplified locally testable version
195
+ # XXX: simplified locally testable version
183
196
decorator = py .test .importorskip ('decorator' ).decorator
184
197
185
198
def log (f , * k , ** kw ):
@@ -195,17 +208,18 @@ def fail():
195
208
excinfo = py .test .raises (ValueError , fail )
196
209
assert excinfo .traceback .recursionindex () is None
197
210
198
-
199
-
200
211
def test_traceback_getcrashentry (self ):
201
212
def i ():
202
213
__tracebackhide__ = True
203
214
raise ValueError
215
+
204
216
def h ():
205
217
i ()
218
+
206
219
def g ():
207
220
__tracebackhide__ = True
208
221
h ()
222
+
209
223
def f ():
210
224
g ()
211
225
@@ -221,6 +235,7 @@ def test_traceback_getcrashentry_empty(self):
221
235
def g ():
222
236
__tracebackhide__ = True
223
237
raise ValueError
238
+
224
239
def f ():
225
240
__tracebackhide__ = True
226
241
g ()
@@ -233,9 +248,11 @@ def f():
233
248
assert entry .lineno == co .firstlineno + 2
234
249
assert entry .frame .code .name == 'g'
235
250
251
+
236
252
def hello (x ):
237
253
x + 5
238
254
255
+
239
256
def test_tbentry_reinterpret ():
240
257
try :
241
258
hello ("hello" )
@@ -245,6 +262,7 @@ def test_tbentry_reinterpret():
245
262
msg = tbentry .reinterpret ()
246
263
assert msg .startswith ("TypeError: ('hello' + 5)" )
247
264
265
+
248
266
def test_excinfo_exconly ():
249
267
excinfo = py .test .raises (ValueError , h )
250
268
assert excinfo .exconly ().startswith ('ValueError' )
@@ -254,22 +272,26 @@ def test_excinfo_exconly():
254
272
assert msg .startswith ('ValueError' )
255
273
assert msg .endswith ("world" )
256
274
275
+
257
276
def test_excinfo_repr ():
258
277
excinfo = py .test .raises (ValueError , h )
259
278
s = repr (excinfo )
260
279
assert s == "<ExceptionInfo ValueError tblen=4>"
261
280
281
+
262
282
def test_excinfo_str ():
263
283
excinfo = py .test .raises (ValueError , h )
264
284
s = str (excinfo )
265
285
assert s .startswith (__file__ [:- 9 ]) # pyc file and $py.class
266
286
assert s .endswith ("ValueError" )
267
287
assert len (s .split (":" )) >= 3 # on windows it's 4
268
288
289
+
269
290
def test_excinfo_errisinstance ():
270
291
excinfo = py .test .raises (ValueError , h )
271
292
assert excinfo .errisinstance (ValueError )
272
293
294
+
273
295
def test_excinfo_no_sourcecode ():
274
296
try :
275
297
exec ("raise ValueError()" )
@@ -281,6 +303,7 @@ def test_excinfo_no_sourcecode():
281
303
else :
282
304
assert s == " File '<string>':1 in <module>\n ???\n "
283
305
306
+
284
307
def test_excinfo_no_python_sourcecode (tmpdir ):
285
308
#XXX: simplified locally testable version
286
309
tmpdir .join ('test.txt' ).write ("{{ h()}}:" )
@@ -292,7 +315,7 @@ def test_excinfo_no_python_sourcecode(tmpdir):
292
315
excinfo = py .test .raises (ValueError ,
293
316
template .render , h = h )
294
317
for item in excinfo .traceback :
295
- print (item ) #XXX: for some reason jinja.Template.render is printed in full
318
+ print (item ) # XXX: for some reason jinja.Template.render is printed in full
296
319
item .source # shouldnt fail
297
320
if item .path .basename == 'test.txt' :
298
321
assert str (item .source ) == '{{ h()}}:'
@@ -309,6 +332,7 @@ def test_entrysource_Queue_example():
309
332
s = str (source ).strip ()
310
333
assert s .startswith ("def get" )
311
334
335
+
312
336
def test_codepath_Queue_example ():
313
337
try :
314
338
queue .Queue ().get (timeout = 0.001 )
@@ -320,6 +344,7 @@ def test_codepath_Queue_example():
320
344
assert path .basename .lower () == "queue.py"
321
345
assert path .check ()
322
346
347
+
323
348
class TestFormattedExcinfo :
324
349
def pytest_funcarg__importasmod (self , request ):
325
350
def importasmod (source ):
@@ -372,7 +397,6 @@ def f():
372
397
'E assert 0'
373
398
]
374
399
375
-
376
400
def test_repr_source_not_existing (self ):
377
401
pr = FormattedExcinfo ()
378
402
co = compile ("raise ValueError()" , "" , "exec" )
@@ -842,14 +866,16 @@ def f():
842
866
finally :
843
867
old .chdir ()
844
868
845
- @py .test .mark .multi (reproptions = [
846
- {'style' : style , 'showlocals' : showlocals ,
847
- 'funcargs' : funcargs , 'tbfilter' : tbfilter
848
- } for style in ("long" , "short" , "no" )
849
- for showlocals in (True , False )
850
- for tbfilter in (True , False )
851
- for funcargs in (True , False )])
852
- def test_format_excinfo (self , importasmod , reproptions ):
869
+ @pytest .mark .parametrize ('style' , ("long" , "short" , "no" ))
870
+ @pytest .mark .parametrize ('showlocals' , (True , False ),
871
+ ids = ['locals' , 'nolocals' ])
872
+ @pytest .mark .parametrize ('tbfilter' , (True , False ),
873
+ ids = ['tbfilter' , 'nofilter' ])
874
+ @pytest .mark .parametrize ('funcargs' , (True , False ),
875
+ ids = ['funcargs' , 'nofuncargs' ])
876
+ def test_format_excinfo (self , importasmod ,
877
+ style , showlocals , tbfilter , funcargs ):
878
+
853
879
mod = importasmod ("""
854
880
def g(x):
855
881
raise ValueError(x)
@@ -858,11 +884,15 @@ def f():
858
884
""" )
859
885
excinfo = py .test .raises (ValueError , mod .f )
860
886
tw = py .io .TerminalWriter (stringio = True )
861
- repr = excinfo .getrepr (** reproptions )
887
+ repr = excinfo .getrepr (
888
+ style = style ,
889
+ showlocals = showlocals ,
890
+ funcargs = funcargs ,
891
+ tbfilter = tbfilter
892
+ )
862
893
repr .toterminal (tw )
863
894
assert tw .stringio .getvalue ()
864
895
865
-
866
896
def test_native_style (self ):
867
897
excinfo = self .excinfo_from_exec ("""
868
898
assert 0
0 commit comments