Skip to content

Commit a07df96

Browse files
committed
mv sigint handling to __main__
This code is only relevant to the CLI, having it in __main__ directly simplifies to control flow around setting the sigint handler.
1 parent e4822ec commit a07df96

File tree

2 files changed

+22
-40
lines changed

2 files changed

+22
-40
lines changed

stagpy/__init__.py

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -9,44 +9,7 @@
99
to issue warnings normally and raise StagpyError.
1010
"""
1111

12-
from __future__ import annotations
13-
14-
import os
15-
import signal
16-
import sys
17-
import typing
18-
19-
if typing.TYPE_CHECKING:
20-
from typing import Any, NoReturn
21-
22-
23-
DEBUG = os.getenv("STAGPY_DEBUG") is not None
24-
25-
26-
def sigint_handler(*_: Any) -> NoReturn:
27-
"""Handler of SIGINT signal.
28-
29-
It is set when you use StagPy as a command line tool to handle gracefully
30-
keyboard interruption.
31-
"""
32-
print("\nSo long, and thanks for all the fish.")
33-
sys.exit()
34-
35-
36-
if DEBUG:
37-
print(
38-
"StagPy runs in DEBUG mode because the environment variable",
39-
'STAGPY_DEBUG is set to "True"',
40-
sep="\n",
41-
end="\n\n",
42-
)
43-
else:
44-
_PREV_INT = signal.signal(signal.SIGINT, sigint_handler)
45-
4612
try:
4713
from ._version import version as __version__
4814
except ImportError:
4915
__version__ = "unknown"
50-
51-
if not DEBUG:
52-
signal.signal(signal.SIGINT, _PREV_INT)

stagpy/__main__.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,36 @@
11
"""The stagpy module is callable."""
22

3+
from __future__ import annotations
4+
5+
import os
36
import signal
47
import sys
58
import warnings
9+
from typing import Any, NoReturn
10+
11+
12+
def sigint_handler(*_: Any) -> NoReturn:
13+
"""Handler of SIGINT signal.
614
7-
from . import DEBUG, sigint_handler
15+
It is set when you use StagPy as a command line tool to handle gracefully
16+
keyboard interruption.
17+
"""
18+
print("\nSo long, and thanks for all the fish.")
19+
sys.exit()
820

921

1022
def main() -> None:
1123
"""Implement StagPy entry point."""
12-
if not DEBUG:
24+
debug = os.getenv("STAGPY_DEBUG") is not None
25+
if debug:
26+
print(
27+
"env variable 'STAGPY_DEBUG' is set: StagPy runs in DEBUG mode",
28+
end="\n\n",
29+
)
30+
else:
1331
signal.signal(signal.SIGINT, sigint_handler)
1432
warnings.simplefilter("ignore")
33+
1534
from . import args, config, error
1635

1736
conf = config.Config.default_()
@@ -21,7 +40,7 @@ def main() -> None:
2140
try:
2241
args.parse_args(conf)(conf)
2342
except error.StagpyError as err:
24-
if DEBUG:
43+
if debug:
2544
raise
2645
errtype = type(err).__name__
2746
print(

0 commit comments

Comments
 (0)