From: Justin Findlay Date: Thu, 15 Sep 2016 17:37:33 +0000 (-0600) Subject: suppress user cancel stack traces in interactive mode X-Git-Url: http://git.99rst.org/?a=commitdiff_plain;h=e3daef0a2a0c5cf21c68e85719ac7dc1c631b89c;p=redacted-XKCD-password-generator.git suppress user cancel stack traces in interactive mode --- diff --git a/xkcdpass/xkcd_password.py b/xkcdpass/xkcd_password.py index 32efa1c..49b484a 100755 --- a/xkcdpass/xkcd_password.py +++ b/xkcdpass/xkcd_password.py @@ -192,6 +192,18 @@ def choose_words(wordlist, numwords): return s +def try_input(prompt): + """ + Suppress stack trace on user cancel: + """ + try: + return raw_input(prompt) + except (KeyboardInterrupt, EOFError): + # user cancelled + print("") + sys.exit(0) + + def generate_xkcdpassword(wordlist, numwords=6, interactive=False, @@ -218,7 +230,7 @@ def generate_xkcdpassword(wordlist, # else, interactive session if not acrostic: - custom_n_words = raw_input("Enter number of words (default 6): ") + custom_n_words = try_input("Enter number of words (default 6): ") if custom_n_words: numwords = int(custom_n_words) @@ -233,7 +245,7 @@ def generate_xkcdpassword(wordlist, else: passwd = delimiter.join(find_acrostic(acrostic, worddict)) print("Generated: ", passwd) - accepted = raw_input("Accept? [yN] ") + accepted = try_input("Accept? [yN] ") return passwd