1
0
mirror of https://github.com/django/django.git synced 2025-03-03 13:34:26 +00:00

Fixed #27670 -- Prevented shell crash on error in .pythonrc.

This commit is contained in:
Peter Inglesby 2017-06-29 12:28:30 +01:00 committed by Tim Graham
parent cf57ecb221
commit 0ba57c3957

View File

@ -1,6 +1,7 @@
import os import os
import select import select
import sys import sys
import traceback
from contextlib import suppress from contextlib import suppress
from django.core.management import BaseCommand, CommandError from django.core.management import BaseCommand, CommandError
@ -69,9 +70,15 @@ class Command(BaseCommand):
continue continue
if not os.path.isfile(pythonrc): if not os.path.isfile(pythonrc):
continue continue
with suppress(NameError): with open(pythonrc) as handle:
with open(pythonrc) as handle: pythonrc_code = handle.read()
exec(compile(handle.read(), pythonrc, 'exec'), imported_objects) # 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) code.interact(local=imported_objects)
def handle(self, **options): def handle(self, **options):