Skip to content

Commit f82ba60

Browse files
committed
fixup
1 parent 02111fb commit f82ba60

File tree

1 file changed

+10
-62
lines changed

1 file changed

+10
-62
lines changed

pytest_timeout.py

Lines changed: 10 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
if HAVE_SIGALRM:
2929
DEFAULT_METHOD = "signal"
3030
else:
31-
DEFAULT_METHOD = "exception"
31+
DEFAULT_METHOD = "thread"
3232
TIMEOUT_DESC = """
3333
Timeout in seconds before dumping the stacks. Default is 0 which
3434
means no timeout.
@@ -61,14 +61,14 @@ def pytest_addoption(parser):
6161
group.addoption(
6262
"--timeout_method",
6363
action="store",
64-
choices=["signal", "thread", "exception"],
64+
choices=["signal", "thread"],
6565
help="Deprecated, use --timeout-method",
6666
)
6767
group.addoption(
6868
"--timeout-method",
6969
dest="timeout_method",
7070
action="store",
71-
choices=["signal", "thread", "exception"],
71+
choices=["signal", "thread"],
7272
help=METHOD_DESC,
7373
)
7474
parser.addini("timeout", TIMEOUT_DESC)
@@ -199,11 +199,11 @@ def timeout_setup(item):
199199
if params.timeout is None or params.timeout <= 0:
200200
return
201201

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"
206205

206+
if timeout_method == "signal":
207207
def handler(signum, frame):
208208
__tracebackhide__ = True
209209
timeout_sigalrm(item, params.timeout)
@@ -215,18 +215,8 @@ def cancel():
215215
item.cancel_timeout = cancel
216216
signal.signal(signal.SIGALRM, handler)
217217
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()))
230220
timer.name = "%s %s" % (__name__, item.nodeid)
231221

232222
def cancel():
@@ -394,49 +384,7 @@ def timeout_sigalrm(item, timeout):
394384
pytest.fail("Timeout >%ss" % timeout)
395385

396386

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):
440388
"""Dump stack of threads and call os._exit().
441389
442390
This disables the capturemanager and dumps stdout and stderr.

0 commit comments

Comments
 (0)