--- shell.py	2011-07-04 09:51:10.236447001 +0200
+++ bshell.py	2011-07-04 09:51:00.648447000 +0200
@@ -2,6 +2,13 @@
 from django.core.management.base import NoArgsCommand
 from optparse import make_option
 
+# BD-WSB looks like we need it to be in globals
+# else the test on readline.get_completer fails ???
+try: 
+    import readline
+except ImportError:
+    pass
+
 class Command(NoArgsCommand):
     option_list = NoArgsCommand.option_list + (
         make_option('--plain', action='store_true', dest='plain',
@@ -59,26 +66,38 @@
             # that tab completion works on objects that are imported at runtime.
             # See ticket 5082.
             imported_objects = {}
+
+            # BD-WSB: if pythonrc already defines a (possibly better) completion,
+            # this screw up everything, so better to *first* load pythonrc,
+            # then test if it's necessary to add completion
+
+            # XXX : cf http://code.djangoproject.com/ticket/5936
+            # the plain python shell __would__ use PYTHONSTARTUP and .pythonrc.py
+            # so WTF ?
+            # if not use_plain: 
+            pythonrc = os.environ.get("PYTHONSTARTUP") 
+            if pythonrc and os.path.isfile(pythonrc): 
+                try:
+                    #print "loading pythonrc file %s" % pythonrc
+                    execfile(pythonrc, imported_objects) 
+                except NameError: 
+                    pass
+            # This will import .pythonrc.py as a side-effect
+            import user
+            
+            # XXX
             try: # Try activating rlcompleter, because it's handy.
-                import readline
-            except ImportError:
+                readline
+            except NameError:
                 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
+                # XXX check if completer is already setup by the pythonrc
+                # else add a simple completion
+                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)
