Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #6682 -- Made shell's REPL actually execute $PYTHONSTARTUP and …

…`~/.pythonrc.py`.

Also:

* Added a ``--no-startup`` option to disable this behavior. Previous
  logic to try to execute the code in charge of this funcionality was
  flawed (it only tried to do so if the user asked for ipython/bpython
  and they weren't found)
* Expand ``~`` in PYTHONSTARTUP value.

Thanks hekevintran at gmail dot com  for the report and initial patch.

Refs #3381.
  • Loading branch information...
commit 1f6b2e7a658594e6ae9507c5f98eb429d19c0c9d 1 parent eaa716a
Ramiro Morales authored
24  django/core/management/commands/shell.py
@@ -9,6 +9,8 @@ class Command(NoArgsCommand):
9 9
     option_list = NoArgsCommand.option_list + (
10 10
         make_option('--plain', action='store_true', dest='plain',
11 11
             help='Tells Django to use plain Python, not IPython or bpython.'),
  12
+        make_option('--no-startup', action='store_true', dest='no_startup',
  13
+            help='When using plain Python, ignore the PYTHONSTARTUP environment variable and ~/.pythonrc.py script.'),
12 14
         make_option('-i', '--interface', action='store', type='choice', choices=shells,
13 15
                     dest='interface',
14 16
             help='Specify an interactive interpreter interface. Available options: "ipython" and "bpython"'),
@@ -56,6 +58,7 @@ def handle_noargs(self, **options):
56 58
         get_models()
57 59
 
58 60
         use_plain = options.get('plain', False)
  61
+        no_startup = options.get('no_startup', False)
59 62
         interface = options.get('interface', None)
60 63
 
61 64
         try:
@@ -83,13 +86,16 @@ def handle_noargs(self, **options):
83 86
 
84 87
             # We want to honor both $PYTHONSTARTUP and .pythonrc.py, so follow system
85 88
             # conventions and get $PYTHONSTARTUP first then .pythonrc.py.
86  
-            if not use_plain:
87  
-                for pythonrc in (os.environ.get("PYTHONSTARTUP"),
88  
-                                 os.path.expanduser('~/.pythonrc.py')):
89  
-                    if pythonrc and os.path.isfile(pythonrc):
90  
-                        try:
91  
-                            with open(pythonrc) as handle:
92  
-                                exec(compile(handle.read(), pythonrc, 'exec'))
93  
-                        except NameError:
94  
-                            pass
  89
+            if not no_startup:
  90
+                for pythonrc in (os.environ.get("PYTHONSTARTUP"), '~/.pythonrc.py'):
  91
+                    if not pythonrc:
  92
+                        continue
  93
+                    pythonrc = os.path.expanduser(pythonrc)
  94
+                    if not os.path.isfile(pythonrc):
  95
+                        continue
  96
+                    try:
  97
+                        with open(pythonrc) as handle:
  98
+                            exec(compile(handle.read(), pythonrc, 'exec'), imported_objects)
  99
+                    except NameError:
  100
+                        pass
95 101
             code.interact(local=imported_objects)
12  docs/ref/django-admin.txt
@@ -779,6 +779,18 @@ bpython::
779 779
 .. _IPython: http://ipython.scipy.org/
780 780
 .. _bpython: http://bpython-interpreter.org/
781 781
 
  782
+When the "plain" Python interactive interpreter starts (be it because
  783
+``--plain`` was specified or because no other interactive interface is
  784
+available) it reads the script pointed to by the :envvar:`PYTHONSTARTUP`
  785
+environment variable and the ``~/.pythonrc.py`` script. If you don't wish this
  786
+behavior you can use the ``--no-startup`` option. e.g.::
  787
+
  788
+    django-admin.py shell --plain --no-startup
  789
+
  790
+.. versionadded:: 1.6
  791
+
  792
+The ``--no-startup`` option was added in Django 1.6.
  793
+
782 794
 sql <appname appname ...>
783 795
 -------------------------
784 796
 

0 notes on commit 1f6b2e7

Please sign in to comment.
Something went wrong with that request. Please try again.