Define KeyboardInterrupt Exception Handler in wmlindent

Note: The signal class object was used to avoid wrapping the entire main routine in a try...except clause.
This commit is contained in:
max-torch 2021-11-01 00:33:59 +08:00 committed by Elvish_Hunter
parent 178efca3a3
commit 56dc9830dc

View file

@ -61,7 +61,7 @@ if there's an indent open at end of file or if a closer occurs with
indent already zero; these two conditions strongly suggest unbalanced WML.
"""
import sys, os, argparse, filecmp, re, codecs
import sys, os, argparse, filecmp, re, codecs, signal
from wesnoth import wmltools3 as wmltools
closer_prefixes = ["{NEXT "]
@ -299,7 +299,16 @@ def convertor(linefilter, arglist, exclude):
print("wmlindent: no WML file found, exiting", file=sys.stderr)
sys.exit(1)
def sigint_handler(signal, frame):
"""This function defines what happens when the SIGINT signal is encountered by pressing ctrl-c during runtime.
When ctrl-c is pressed, a one-line message is displayed and Python exits with Status 0, which refers to successful termination.
This overrides Python's default behavior of displaying a traceback when ctrl-c is pressed.
"""
print ('Aborted by pressing ctrl-c')
sys.exit(0)
if __name__ == '__main__':
signal.signal(signal.SIGINT, sigint_handler)
parser = argparse.ArgumentParser(
formatter_class=argparse.RawTextHelpFormatter
)