Skip to content
This repository was archived by the owner on Dec 27, 2023. It is now read-only.

Commit f097797

Browse files
committed
Enable usage of Timeout class without 'with' block
Signed-off-by: Andrea Cervesato <[email protected]>
1 parent c7b84a5 commit f097797

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

ltp/tests/test_utils.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,11 @@ def test_timeout_custom_all(self):
5555
with Timeout(0.01) as timeout:
5656
time.sleep(0.01)
5757
timeout.check(err_msg='error message', exc=TimeoutError)
58+
59+
def test_no_with(self):
60+
"""
61+
Test class without 'with' block.
62+
"""
63+
timeout = Timeout(1)
64+
time.sleep(0.01)
65+
timeout.check()

ltp/utils.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ def check(self, err_msg: str = None, exc: Exception = None):
4141
"""
4242
Check if time is out.
4343
"""
44+
if not self._end:
45+
self._end = time.time() + self._timeout
46+
4447
if self._end > time.time():
4548
return
4649

0 commit comments

Comments
 (0)