Skip to content

Commit bab7bd1

Browse files
committed
Centralize KeyboardInterrupt handling
Utilities will now automatically exit with status 130 (without displaying a stack trace) upon `KeyboardInterrupt` exception, unless a `try...except` for `KeyboardInterrupt` is registered by a specific utility (e.g. for performing any necessary cleanup functions).
1 parent c939331 commit bab7bd1

File tree

5 files changed

+30
-37
lines changed

5 files changed

+30
-37
lines changed

userland/core/command.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,13 @@ def create_utility(
4444
func: Callable[[Values, list[Any]], int],
4545
) -> Callable[[], None]:
4646
def execute_utility():
47-
sys.exit(
47+
try:
48+
sys.exit(
4849
func(*parser.parse_args()) if parser else func(Values(), sys.argv[1:])
4950
)
51+
except KeyboardInterrupt:
52+
print()
53+
sys.exit(130)
5054

5155
return execute_utility
5256

userland/utilities/factor.py

+20-24
Original file line numberDiff line numberDiff line change
@@ -111,29 +111,25 @@ def format_exponents(factors: Iterable[int]) -> str:
111111
def python_userland_factor(opts, args: list[str]) -> int:
112112
failed = False
113113

114-
try:
115-
for arg in args or core.readwords_stdin():
116-
try:
117-
num = int(arg)
118-
if num < 0:
119-
raise ValueError
120-
except ValueError:
121-
failed = True
122-
core.perror(f"'{arg}' is not a valid positive integer")
123-
continue
124-
125-
if num < 2:
126-
print(f"{num}:")
127-
continue
128-
129-
factors = sorted(factorize(num))
130-
131-
print(
132-
f"{num}: {format_exponents(factors) if opts.exponents
133-
else " ".join(map(str, factors))}"
134-
)
135-
except KeyboardInterrupt:
136-
print()
137-
return 130
114+
for arg in args or core.readwords_stdin():
115+
try:
116+
num = int(arg)
117+
if num < 0:
118+
raise ValueError
119+
except ValueError:
120+
failed = True
121+
core.perror(f"'{arg}' is not a valid positive integer")
122+
continue
123+
124+
if num < 2:
125+
print(f"{num}:")
126+
continue
127+
128+
factors = sorted(factorize(num))
129+
130+
print(
131+
f"{num}: {format_exponents(factors) if opts.exponents
132+
else " ".join(map(str, factors))}"
133+
)
138134

139135
return int(failed)

userland/utilities/reset.py

+3-7
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,9 @@ def python_userland_reset(opts, args: list[str]) -> int:
3636
if opts.q:
3737
if not term:
3838
core.perror("unknown terminal type ")
39-
try:
40-
while True:
41-
if term := input("Terminal type? "):
42-
break
43-
except KeyboardInterrupt:
44-
print()
45-
return 130
39+
while True:
40+
if term := input("Terminal type? "):
41+
break
4642

4743
print(term)
4844
return 0

userland/utilities/sleep.py

+1-5
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,6 @@ def python_userland_sleep(_, args) -> int:
2828
parser.error(f"invalid duration: {spec}")
2929
total_secs += Decimal(spec[:-1]) * multiplier
3030

31-
try:
32-
time.sleep(float(total_secs))
33-
except KeyboardInterrupt:
34-
print()
35-
return 130
31+
time.sleep(float(total_secs))
3632

3733
return 0

userland/utilities/yes.py

+1
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ def python_userland_yes(_, args) -> int:
1414
while True:
1515
print(string)
1616
except KeyboardInterrupt:
17+
# Do not emit a trailing newline on keyboard interrupt.
1718
return 130

0 commit comments

Comments
 (0)