Ticket #11542: shell.py.diff

File shell.py.diff, 5.1 KB (added by Seamus, 15 years ago)
  • shell.py

    old new class Command(NoArgsCommand):  
    66    option_list = NoArgsCommand.option_list + (
    77        make_option('--plain', action='store_true', dest='plain',
    88            help='Tells Django to use plain Python, not IPython.'),
     9        make_option('--bpython', action='store_true', dest='bpython',
     10            help='Tells Django to use bpython, not IPython.'),
    911    )
    10     help = "Runs a Python interactive interpreter. Tries to use IPython, if it's available."
     12    help = "Runs a Python interactive interpreter. Tries to use IPython, if it's available.  If IPython is not available, tries to use bpython. "
    1113
    1214    requires_model_validation = False
    1315
    class Command(NoArgsCommand):  
    1820        loaded_models = get_models()
    1921
    2022        use_plain = options.get('plain', False)
    21 
     23        use_bpython = options.get('bpython',False)
    2224        try:
    23             if use_plain:
    24                 # Don't bother loading IPython, because the user wants plain Python.
     25            if use_plain or use_bpython:
     26                if use_bpython:
     27                    use_plain = None
    2528                raise ImportError
    2629            import IPython
    27             # Explicitly pass an empty list as arguments, because otherwise IPython
    28             # would use sys.argv from this script.
    2930            shell = IPython.Shell.IPShell(argv=[])
    3031            shell.mainloop()
    3132        except ImportError:
    32             import code
    33             # Set up a dictionary to serve as the environment for the shell, so
    34             # that tab completion works on objects that are imported at runtime.
    35             # See ticket 5082.
    36             imported_objects = {}
    37             try: # Try activating rlcompleter, because it's handy.
    38                 import readline
     33            try:
     34                if use_plain:
     35                    raise ImportError
     36                import bpython
     37                # Older versions of bpython do not support embed().  When
     38                # specifying the --bpython flag, an attributeError is raised
     39                # telling the user to upgrade.  Otherwise, fall back to the
     40                # plain interpreter.
     41                try:
     42                    bpython.embed()
     43                except AttributeError:
     44                    if use_bpython:
     45                        raise AttributeError("Your version of bpython does not support embedding.  Please upgrade to the latest version.")
     46                    else:
     47                        raise ImportError
    3948            except ImportError:
    40                 pass
    41             else:
    42                 # We don't have to wrap the following import in a 'try', because
    43                 # we already know 'readline' was imported successfully.
    44                 import rlcompleter
    45                 readline.set_completer(rlcompleter.Completer(imported_objects).complete)
    46                 readline.parse_and_bind("tab:complete")
     49                # When specifying the --bpython flag and the import fails,
     50                # do not fall back to the plain interpreter.
     51                if use_bpython:
     52                    raise ImportError("You do not have bpython installed")
     53                import code
     54                # Set up a dictionary to serve as the environment for the shell, so
     55                # that tab completion works on objects that are imported at runtime.
     56                # See ticket 5082.
     57                imported_objects = {}
     58                try: # Try activating rlcompleter, because it's handy.
     59                    import readline
     60                except ImportError:
     61                    pass
     62                else:
     63                    # We don't have to wrap the following import in a 'try', because
     64                    # we already know 'readline' was imported successfully.
     65                    import rlcompleter
     66                    readline.set_completer(rlcompleter.Completer(imported_objects).complete)
     67                    readline.parse_and_bind("tab:complete")
    4768
    48             # We want to honor both $PYTHONSTARTUP and .pythonrc.py, so follow system
    49             # conventions and get $PYTHONSTARTUP first then import user.
    50             if not use_plain:
    51                 pythonrc = os.environ.get("PYTHONSTARTUP")
    52                 if pythonrc and os.path.isfile(pythonrc):
    53                     try:
    54                         execfile(pythonrc)
    55                     except NameError:
    56                         pass
     69                # We want to honor both $PYTHONSTARTUP and .pythonrc.py, so follow system
     70                # conventions and get $PYTHONSTARTUP first then import user.
     71                if not use_plain:
     72                    pythonrc = os.environ.get("PYTHONSTARTUP")
     73                    if pythonrc and os.path.isfile(pythonrc):
     74                        try:
     75                            execfile(pythonrc)
     76                        except NameError:
     77                            pass
    5778                # This will import .pythonrc.py as a side-effect
    58                 import user
    59             code.interact(local=imported_objects)
     79                    import user
     80                code.interact(local=imported_objects)
     81
Back to Top