From 0ba57c395796a5644cd0f880ac9fa7052d318021 Mon Sep 17 00:00:00 2001 From: Peter Inglesby Date: Thu, 29 Jun 2017 12:28:30 +0100 Subject: [PATCH] Fixed #27670 -- Prevented shell crash on error in .pythonrc. --- django/core/management/commands/shell.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/django/core/management/commands/shell.py b/django/core/management/commands/shell.py index 1bf9197269..ce013bcec0 100644 --- a/django/core/management/commands/shell.py +++ b/django/core/management/commands/shell.py @@ -1,6 +1,7 @@ import os import select import sys +import traceback from contextlib import suppress from django.core.management import BaseCommand, CommandError @@ -69,9 +70,15 @@ class Command(BaseCommand): continue if not os.path.isfile(pythonrc): continue - with suppress(NameError): - with open(pythonrc) as handle: - exec(compile(handle.read(), pythonrc, 'exec'), imported_objects) + with open(pythonrc) as handle: + pythonrc_code = handle.read() + # Match the behavior of the cpython shell where an error in + # PYTHONSTARTUP prints an exception and continues. + try: + exec(compile(pythonrc_code, pythonrc, 'exec'), imported_objects) + except Exception: + traceback.print_exc() + code.interact(local=imported_objects) def handle(self, **options):