Ticket #9990: shell.diff

File shell.diff, 3.0 KB (added by bruno, 16 years ago)
  • django/core/management/commands/shell.py

     
    22from django.core.management.base import NoArgsCommand
    33from optparse import make_option
    44
     5# XXX looks like we need it to be in globals
     6# else the test on readline.get_completer fails ???
     7try:
     8    import readline
     9except ImportError:
     10    pass
     11
     12
    513class Command(NoArgsCommand):
    614    option_list = NoArgsCommand.option_list + (
    715        make_option('--plain', action='store_true', dest='plain',
     
    3442            # that tab completion works on objects that are imported at runtime.
    3543            # See ticket 5082.
    3644            imported_objects = {}
    37             try: # Try activating rlcompleter, because it's handy.
    38                 import readline
    39             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")
    4745
    48             # We want to honor both $PYTHONSTARTUP and .pythonrc.py, so follow system
    49             # conventions and get $PYTHONSTARTUP first then import user.
     46            # XXX if pythonrc already defines a (possibly better) completion,
     47            # this screw up everything.
     48            # so better to *first* load pythonrc, then test if it's necessary
     49            # to add completion
    5050            if not use_plain:
    5151                pythonrc = os.environ.get("PYTHONSTARTUP")
    5252                if pythonrc and os.path.isfile(pythonrc):
    53                     try:
    54                         execfile(pythonrc)
     53                    try:
     54                        print "loading pythonrc file %s" % pythonrc
     55                        execfile(pythonrc, imported_objects)
    5556                    except NameError:
    5657                        pass
    5758                # This will import .pythonrc.py as a side-effect
    5859                import user
     60
     61            # XXX
     62            try: # Try activating rlcompleter, because it's handy.
     63                readline
     64            except NameError:
     65                pass
     66            else:
     67                # XXX check if completer is already setup by the pythonrc
     68                # else add a simple completion
     69                if not readline.get_completer():
     70                    # We don't have to wrap the following import in a 'try', because
     71                    # we already know 'readline' was imported successfully.
     72                    import rlcompleter
     73                    readline.set_completer(rlcompleter.Completer(imported_objects).complete)
     74                    readline.parse_and_bind("tab:complete")
     75
    5976            code.interact(local=imported_objects)
Back to Top