Ticket #9990: shell-r16501.patch

File shell-r16501.patch, 3.3 KB (added by bruno desthuilliers <bruno.desthuilliers@…>, 13 years ago)

patch against r16501, cleaned up comments and better handlin of "use_plain"

  • .py

    old new  
    22from django.core.management.base import NoArgsCommand
    33from optparse import make_option
    44
     5# Looks like we need it to be in globals else the test on
     6# readline.get_completer fails (but don't ask me why)
     7try:
     8    import readline
     9except ImportError:
     10    readline = None
     11
    512class Command(NoArgsCommand):
    613    option_list = NoArgsCommand.option_list + (
    714        make_option('--plain', action='store_true', dest='plain',
     
    5360                # Don't bother loading IPython, because the user wants plain Python.
    5461                raise ImportError
    5562            self.run_shell()
     63
    5664        except ImportError:
    5765            import code
    5866            # Set up a dictionary to serve as the environment for the shell, so
    5967            # that tab completion works on objects that are imported at runtime.
    6068            # See ticket 5082.
    6169            imported_objects = {}
    62             try: # Try activating rlcompleter, because it's handy.
    63                 import readline
    64             except ImportError:
    65                 pass
    66             else:
    67                 # We don't have to wrap the following import in a 'try', because
    68                 # we already know 'readline' was imported successfully.
    69                 import rlcompleter
    70                 readline.set_completer(rlcompleter.Completer(imported_objects).complete)
    71                 readline.parse_and_bind("tab:complete")
    72 
    73             # We want to honor both $PYTHONSTARTUP and .pythonrc.py, so follow system
    74             # conventions and get $PYTHONSTARTUP first then import user.
    75             if not use_plain:
    76                 pythonrc = os.environ.get("PYTHONSTARTUP")
    77                 if pythonrc and os.path.isfile(pythonrc):
    78                     try:
    79                         execfile(pythonrc)
    80                     except NameError:
    81                         pass
    82                 # This will import .pythonrc.py as a side-effect
    83                 import user
     70           
     71            # ticket #9990 :
     72            # We want to first load pythonrc then test if it already defines a
     73            # completion, and only add our one if necessary.
     74            # NB : We honor the pythonrc without checking for 'use_plain' because that's
     75            # how the 'plain' shell would work
     76            pythonrc = os.environ.get("PYTHONSTARTUP")
     77            if pythonrc and os.path.isfile(pythonrc):
     78                try:
     79                    execfile(pythonrc, imported_objects)
     80                except NameError:
     81                    pass
     82
     83            # This will import .pythonrc.py as a side-effect
     84            import user
     85           
     86            if readline and not use_plain:
     87                # Try activating rlcompleter, because it's handy - but only
     88                # if the pythonrc didn't already took care of this
     89                if not readline.get_completer():
     90                    # We don't have to wrap the following import in a 'try', because
     91                    # we already know 'readline' was imported successfully.
     92                    import rlcompleter
     93                    readline.set_completer(rlcompleter.Completer(imported_objects).complete)
     94                    readline.parse_and_bind("tab:complete")
     95           
    8496            code.interact(local=imported_objects)
Back to Top