Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions src/scancode/interrupt.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,20 @@ def handler(signum, frame):
raise TimeoutError

try:
create_signal(SIGALRM, handler)
setitimer(ITIMER_REAL, timeout)
return NO_ERROR, func(*(args or ()), **(kwargs or {}))
create_signal(SIGALRM, handler)
setitimer(ITIMER_REAL, timeout)
except (ValueError, TypeError):
# Signals only work in the main thread.
# If we cannot install them, continue without interrupt support.
return NO_ERROR, func(*(args or ()), **(kwargs or {}))

try:
return NO_ERROR, func(*(args or ()), **(kwargs or {}))
except TimeoutError:
return TIMEOUT_MSG % locals(), NO_VALUE
except Exception:
return ERROR_MSG + traceback_format_exc(), NO_VALUE


except TimeoutError:
return TIMEOUT_MSG % locals(), NO_VALUE
Expand Down Expand Up @@ -167,7 +178,10 @@ def async_raise(tid, exctype=Exception):
from http://tomerfiliba.com/recipes/Thread2/
license: public domain.
"""
assert isinstance(tid, int), 'Invalid thread id: must an integer'
if not isinstance(tid, int):
raise TypeError(
f"Invalid thread id: must be an integer, got {type(tid).__name__}"
)

tid = c_long(tid)
exception = py_object(Exception)
Expand Down
Loading