Skip to content

Commit f23b1f5

Browse files
fundakolnashif
authored andcommitted
twister: Improve handling SystemExit exceptions
If twister exits by sys.exit function or by raising the SystemExit exception, catch that exceptions and return proper exit code. Signed-off-by: Łukasz Fundakowski <[email protected]>
1 parent 39a0ee1 commit f23b1f5

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

scripts/pylib/twister/twisterlib/twister_main.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,24 @@ def init_color(colorama_strip):
3333
colorama.init(strip=colorama_strip)
3434

3535

36+
def catch_system_exit_exception(func):
37+
"""Decorator to catch SystemExit exception."""
38+
39+
def _inner(*args, **kwargs):
40+
try:
41+
return func(*args, **kwargs)
42+
except SystemExit as exc:
43+
if isinstance(exc.code, int):
44+
return exc.code
45+
if exc.code is None:
46+
return 0
47+
# if exc.code is not int/None consider it is not zero
48+
return 1
49+
50+
return _inner
51+
52+
53+
@catch_system_exit_exception
3654
def twister(options: argparse.Namespace, default_options: argparse.Namespace) -> int:
3755
start_time = time.time()
3856

@@ -120,7 +138,7 @@ def twister(options: argparse.Namespace, default_options: argparse.Namespace) ->
120138
if i.status in [TwisterStatus.SKIP,TwisterStatus.FILTER]:
121139
if options.platform and not tplan.check_platform(i.platform, options.platform):
122140
continue
123-
# Filtered tests should be visable only when verbosity > 1
141+
# Filtered tests should be visible only when verbosity > 1
124142
if options.verbose < 2 and i.status == TwisterStatus.FILTER:
125143
continue
126144
res = i.reason

0 commit comments

Comments
 (0)