What is the final code then?
When I run
When I run
class TimeOut: def __init__(self, timeout): self.timeout = timeout self._orig_handler = signal.signal(signal.SIGALRM, self._timeout) def __enter__(self): signal.alarm(self.timeout) return self def __exit__(self, *args): signal.signal(signal.SIGALRM, self._orig_handler) def _timeout(self, *args): raise TimeoutError with TimeOut(5): try: input('Answer in 5 seconds: ') except TimeoutError: print('\nTime is up!')I get:
Error:Traceback (most recent call last):
File "Untitled", line 13
with TimeOut(5):
File "Untitled", line 4, in __init__
self._orig_handler = signal.signal(signal.SIGALRM, self._timeout)
NameError: global name 'signal' is not defined