1
0
mirror of https://github.com/django/django.git synced 2024-12-22 09:05:43 +00:00

Fixed #36016 -- Prevented traceback when quitting makemigrations with Ctrl-C.

This commit is contained in:
Aman Sharma 2024-12-17 14:00:33 +05:30 committed by Sarah Boyce
parent 3ee4c6a27a
commit f05edb2b43
2 changed files with 26 additions and 4 deletions

View File

@ -111,17 +111,19 @@ class InteractiveMigrationQuestioner(MigrationQuestioner):
for i, choice in enumerate(choices): for i, choice in enumerate(choices):
self.prompt_output.write(" %s) %s" % (i + 1, choice)) self.prompt_output.write(" %s) %s" % (i + 1, choice))
self.prompt_output.write("Select an option: ", ending="") self.prompt_output.write("Select an option: ", ending="")
result = input()
while True: while True:
try: try:
result = input()
value = int(result) value = int(result)
except ValueError: except ValueError:
pass pass
except KeyboardInterrupt:
self.prompt_output.write("\nCancelled.")
sys.exit(1)
else: else:
if 0 < value <= len(choices): if 0 < value <= len(choices):
return value return value
self.prompt_output.write("Please select a valid option: ", ending="") self.prompt_output.write("Please select a valid option: ", ending="")
result = input()
def _ask_default(self, default=""): def _ask_default(self, default=""):
""" """
@ -148,7 +150,11 @@ class InteractiveMigrationQuestioner(MigrationQuestioner):
else: else:
prompt = ">>> " prompt = ">>> "
self.prompt_output.write(prompt, ending="") self.prompt_output.write(prompt, ending="")
try:
code = input() code = input()
except KeyboardInterrupt:
self.prompt_output.write("\nCancelled.")
sys.exit(1)
if not code and default: if not code and default:
code = default code = default
if not code: if not code:

View File

@ -85,8 +85,9 @@ class QuestionerHelperMethodsTests(SimpleTestCase):
@mock.patch("builtins.input", side_effect=[KeyboardInterrupt()]) @mock.patch("builtins.input", side_effect=[KeyboardInterrupt()])
def test_questioner_no_default_keyboard_interrupt(self, mock_input): def test_questioner_no_default_keyboard_interrupt(self, mock_input):
with self.assertRaises(KeyboardInterrupt): with self.assertRaises(SystemExit):
self.questioner._ask_default() self.questioner._ask_default()
self.assertIn("Cancelled.\n", self.prompt.getvalue())
@mock.patch("builtins.input", side_effect=["", "n"]) @mock.patch("builtins.input", side_effect=["", "n"])
def test_questioner_no_default_no_user_entry_boolean(self, mock_input): def test_questioner_no_default_no_user_entry_boolean(self, mock_input):
@ -105,3 +106,18 @@ class QuestionerHelperMethodsTests(SimpleTestCase):
expected_msg = f"{question}\n" f" 1) a\n" f" 2) b\n" f" 3) c\n" expected_msg = f"{question}\n" f" 1) a\n" f" 2) b\n" f" 3) c\n"
self.assertIn(expected_msg, self.prompt.getvalue()) self.assertIn(expected_msg, self.prompt.getvalue())
self.assertEqual(value, 1) self.assertEqual(value, 1)
@mock.patch("builtins.input", side_effect=[KeyboardInterrupt()])
def test_questioner_no_choice_keyboard_interrupt(self, mock_input):
question = "Make a choice:"
with self.assertRaises(SystemExit):
self.questioner._choice_input(question, choices="abc")
expected_msg = (
f"{question}\n"
f" 1) a\n"
f" 2) b\n"
f" 3) c\n"
f"Select an option: \n"
f"Cancelled.\n"
)
self.assertIn(expected_msg, self.prompt.getvalue())