Skip to content
Open
Show file tree
Hide file tree
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
28 changes: 23 additions & 5 deletions scapy/autorun.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,27 @@ def autorun_commands(_cmds, my_globals=None, verb=None):
continue

if sys.last_exc or sys.last_value: # type: ignore # An error occurred
if sys.version_info >= (3, 12):
traceback.print_last(file=sys.stdout)
else:
# traceback.print_last(file=...) is broken in CPython
# Affected versions: 3.12.0-3.12.9 and 3.13.0-3.13.2
# gh-130250: https://github.com/python/cpython/issues/130250
v = sys.version_info
broken_3_12 = (v[:2] == (3, 12) and v[2] < 10)
broken_3_13 = (v[:2] == (3, 13) and v[2] < 3)
if v < (3, 12):
traceback.print_exception(
sys.last_type,
sys.last_value,
sys.last_traceback.tb_next,
file=sys.stdout,
)
elif broken_3_12 or broken_3_13:
exc = sys.last_exc # type: ignore
traceback.print_exception(
type(exc), exc, exc.__traceback__,
file=sys.stdout,
)
else:
traceback.print_last(file=sys.stdout)
sys.last_exc = None # Python 3.12+
sys.last_value = None
return False
Expand Down Expand Up @@ -132,14 +144,20 @@ def autorun_commands_timeout(cmds, timeout=None, **kwargs):

def _runner():
# type: () -> None
q.put(autorun_commands(cmds, **kwargs))
try:
q.put(autorun_commands(cmds, **kwargs))
except BaseException as e:
q.put(e)
th = threading.Thread(target=_runner)
th.daemon = True
th.start()
th.join(timeout)
if th.is_alive():
raise StopAutorunTimeout
return q.get()
result = q.get()
if isinstance(result, BaseException):
raise result
return result


class StringWriter(StringIO):
Expand Down
Loading
Loading