summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRamiro Morales <cramm0@gmail.com>2013-01-24 21:21:26 -0300
committerRamiro Morales <cramm0@gmail.com>2013-01-24 22:01:47 -0300
commit1f6b2e7a658594e6ae9507c5f98eb429d19c0c9d (patch)
tree2d0e3f5495c9fe6f407b0c4eab455c91289fd434
parenteaa716a4130bb019204669d32389db8b399c0f71 (diff)
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.
-rw-r--r--django/core/management/commands/shell.py24
-rw-r--r--docs/ref/django-admin.txt12
2 files changed, 27 insertions, 9 deletions
diff --git a/django/core/management/commands/shell.py b/django/core/management/commands/shell.py
index f883fb95d8..851d4e3cfb 100644
--- a/django/core/management/commands/shell.py
+++ b/django/core/management/commands/shell.py
@@ -9,6 +9,8 @@ class Command(NoArgsCommand):
option_list = NoArgsCommand.option_list + (
make_option('--plain', action='store_true', dest='plain',
help='Tells Django to use plain Python, not IPython or bpython.'),
+ make_option('--no-startup', action='store_true', dest='no_startup',
+ help='When using plain Python, ignore the PYTHONSTARTUP environment variable and ~/.pythonrc.py script.'),
make_option('-i', '--interface', action='store', type='choice', choices=shells,
dest='interface',
help='Specify an interactive interpreter interface. Available options: "ipython" and "bpython"'),
@@ -56,6 +58,7 @@ class Command(NoArgsCommand):
get_models()
use_plain = options.get('plain', False)
+ no_startup = options.get('no_startup', False)
interface = options.get('interface', None)
try:
@@ -83,13 +86,16 @@ class Command(NoArgsCommand):
# We want to honor both $PYTHONSTARTUP and .pythonrc.py, so follow system
# conventions and get $PYTHONSTARTUP first then .pythonrc.py.
- if not use_plain:
- for pythonrc in (os.environ.get("PYTHONSTARTUP"),
- os.path.expanduser('~/.pythonrc.py')):
- if pythonrc and os.path.isfile(pythonrc):
- try:
- with open(pythonrc) as handle:
- exec(compile(handle.read(), pythonrc, 'exec'))
- except NameError:
- pass
+ if not no_startup:
+ for pythonrc in (os.environ.get("PYTHONSTARTUP"), '~/.pythonrc.py'):
+ if not pythonrc:
+ continue
+ pythonrc = os.path.expanduser(pythonrc)
+ if not os.path.isfile(pythonrc):
+ continue
+ try:
+ with open(pythonrc) as handle:
+ exec(compile(handle.read(), pythonrc, 'exec'), imported_objects)
+ except NameError:
+ pass
code.interact(local=imported_objects)
diff --git a/docs/ref/django-admin.txt b/docs/ref/django-admin.txt
index 06ec8e2031..8f6664edb7 100644
--- a/docs/ref/django-admin.txt
+++ b/docs/ref/django-admin.txt
@@ -779,6 +779,18 @@ bpython::
.. _IPython: http://ipython.scipy.org/
.. _bpython: http://bpython-interpreter.org/
+When the "plain" Python interactive interpreter starts (be it because
+``--plain`` was specified or because no other interactive interface is
+available) it reads the script pointed to by the :envvar:`PYTHONSTARTUP`
+environment variable and the ``~/.pythonrc.py`` script. If you don't wish this
+behavior you can use the ``--no-startup`` option. e.g.::
+
+ django-admin.py shell --plain --no-startup
+
+.. versionadded:: 1.6
+
+The ``--no-startup`` option was added in Django 1.6.
+
sql <appname appname ...>
-------------------------