28
28
if HAVE_SIGALRM :
29
29
DEFAULT_METHOD = "signal"
30
30
else :
31
- DEFAULT_METHOD = "exception "
31
+ DEFAULT_METHOD = "thread "
32
32
TIMEOUT_DESC = """
33
33
Timeout in seconds before dumping the stacks. Default is 0 which
34
34
means no timeout.
@@ -61,14 +61,14 @@ def pytest_addoption(parser):
61
61
group .addoption (
62
62
"--timeout_method" ,
63
63
action = "store" ,
64
- choices = ["signal" , "thread" , "exception" ],
64
+ choices = ["signal" , "thread" ],
65
65
help = "Deprecated, use --timeout-method" ,
66
66
)
67
67
group .addoption (
68
68
"--timeout-method" ,
69
69
dest = "timeout_method" ,
70
70
action = "store" ,
71
- choices = ["signal" , "thread" , "exception" ],
71
+ choices = ["signal" , "thread" ],
72
72
help = METHOD_DESC ,
73
73
)
74
74
parser .addini ("timeout" , TIMEOUT_DESC )
@@ -199,11 +199,11 @@ def timeout_setup(item):
199
199
if params .timeout is None or params .timeout <= 0 :
200
200
return
201
201
202
- if params .method == "signal" and not (current_thread () is main_thread ()):
203
- params .method = "thread"
204
-
205
- if params .method == "signal" :
202
+ timeout_method = params .method
203
+ if timeout_method == "signal" and not (current_thread () is main_thread ()):
204
+ timeout_method = "thread"
206
205
206
+ if timeout_method == "signal" :
207
207
def handler (signum , frame ):
208
208
__tracebackhide__ = True
209
209
timeout_sigalrm (item , params .timeout )
@@ -215,18 +215,8 @@ def cancel():
215
215
item .cancel_timeout = cancel
216
216
signal .signal (signal .SIGALRM , handler )
217
217
signal .setitimer (signal .ITIMER_REAL , params .timeout )
218
- elif params .method == "thread" :
219
- timer = threading .Timer (params .timeout , timeout_timer , (item , params .timeout ))
220
- timer .name = "%s %s" % (__name__ , item .nodeid )
221
-
222
- def cancel ():
223
- timer .cancel ()
224
- timer .join ()
225
-
226
- item .cancel_timeout = cancel
227
- timer .start ()
228
- elif params .method == "exception" :
229
- timer = threading .Timer (params .timeout , timeout_exception , (current_thread (), item , params .timeout ))
218
+ elif timeout_method == "thread" :
219
+ timer = threading .Timer (params .timeout , timeout_timer , (item , params .timeout , current_thread ()))
230
220
timer .name = "%s %s" % (__name__ , item .nodeid )
231
221
232
222
def cancel ():
@@ -394,49 +384,7 @@ def timeout_sigalrm(item, timeout):
394
384
pytest .fail ("Timeout >%ss" % timeout )
395
385
396
386
397
- def timeout_timer (item , timeout ):
398
- """Dump stack of threads and call os._exit().
399
-
400
- This disables the capturemanager and dumps stdout and stderr.
401
- Then the stacks are dumped and os._exit(1) is called.
402
- """
403
- if is_debugging ():
404
- return
405
- try :
406
- capman = item .config .pluginmanager .getplugin ("capturemanager" )
407
- if capman :
408
- pytest_version = LooseVersion (pytest .__version__ )
409
- if pytest_version >= LooseVersion ("3.7.3" ):
410
- capman .suspend_global_capture (item )
411
- stdout , stderr = capman .read_global_capture ()
412
- else :
413
- stdout , stderr = capman .suspend_global_capture (item )
414
- else :
415
- stdout , stderr = None , None
416
- write_title ("Timeout" , sep = "+" )
417
- caplog = item .config .pluginmanager .getplugin ("_capturelog" )
418
- if caplog and hasattr (item , "capturelog_handler" ):
419
- log = item .capturelog_handler .stream .getvalue ()
420
- if log :
421
- write_title ("Captured log" )
422
- write (log )
423
- if stdout :
424
- write_title ("Captured stdout" )
425
- write (stdout )
426
- if stderr :
427
- write_title ("Captured stderr" )
428
- write (stderr )
429
- dump_stacks ()
430
- write_title ("Timeout" , sep = "+" )
431
- except Exception :
432
- traceback .print_exc ()
433
- finally :
434
- sys .stdout .flush ()
435
- sys .stderr .flush ()
436
- os ._exit (1 )
437
-
438
-
439
- def timeout_exception (thread , item , timeout ):
387
+ def timeout_timer (item , timeout , thread ):
440
388
"""Dump stack of threads and call os._exit().
441
389
442
390
This disables the capturemanager and dumps stdout and stderr.
0 commit comments