--- shell.py	2011-07-04 10:14:55.144447002 +0200
+++ bshell.py	2011-07-04 12:12:18.816447000 +0200
@@ -2,6 +2,13 @@
 from django.core.management.base import NoArgsCommand
 from optparse import make_option
 
+# Looks like we need it to be in globals else the test on
+# readline.get_completer fails (but don't ask me why)
+try: 
+    import readline
+except ImportError:
+    readline = None
+
 class Command(NoArgsCommand):
     option_list = NoArgsCommand.option_list + (
         make_option('--plain', action='store_true', dest='plain',
@@ -53,32 +60,37 @@
                 # Don't bother loading IPython, because the user wants plain Python.
                 raise ImportError
             self.run_shell()
+
         except ImportError:
             import code
             # Set up a dictionary to serve as the environment for the shell, so
             # that tab completion works on objects that are imported at runtime.
             # See ticket 5082.
             imported_objects = {}
-            try: # Try activating rlcompleter, because it's handy.
-                import readline
-            except ImportError:
-                pass
-            else:
-                # We don't have to wrap the following import in a 'try', because
-                # we already know 'readline' was imported successfully.
-                import rlcompleter
-                readline.set_completer(rlcompleter.Completer(imported_objects).complete)
-                readline.parse_and_bind("tab:complete")
-
-            # We want to honor both $PYTHONSTARTUP and .pythonrc.py, so follow system
-            # conventions and get $PYTHONSTARTUP first then import user.
-            if not use_plain: 
-                pythonrc = os.environ.get("PYTHONSTARTUP") 
-                if pythonrc and os.path.isfile(pythonrc): 
-                    try: 
-                        execfile(pythonrc) 
-                    except NameError: 
-                        pass
-                # This will import .pythonrc.py as a side-effect
-                import user
+            
+            # ticket #9990 :
+            # We want to first load pythonrc then test if it already defines a
+            # completion, and only add our one if necessary.
+            # NB : We honor the pythonrc without checking for 'use_plain' because that's
+            # how the 'plain' shell would work
+            pythonrc = os.environ.get("PYTHONSTARTUP") 
+            if pythonrc and os.path.isfile(pythonrc): 
+                try:
+                    execfile(pythonrc, imported_objects) 
+                except NameError: 
+                    pass
+
+            # This will import .pythonrc.py as a side-effect
+            import user
+            
+            if readline and not use_plain:
+                # Try activating rlcompleter, because it's handy - but only
+                # if the pythonrc didn't already took care of this
+                if not readline.get_completer():
+                    # We don't have to wrap the following import in a 'try', because
+                    # we already know 'readline' was imported successfully.
+                    import rlcompleter
+                    readline.set_completer(rlcompleter.Completer(imported_objects).complete)
+                    readline.parse_and_bind("tab:complete")
+            
             code.interact(local=imported_objects)
