]> arthur.barton.de Git - bup.git/commitdiff
Work around extra space added by some readline versions.
authorPeter McCurdy <petermccurdy@alumni.uwaterloo.ca>
Fri, 16 Apr 2010 07:11:13 +0000 (03:11 -0400)
committerAvery Pennarun <apenwarr@gmail.com>
Thu, 3 Jun 2010 07:03:08 +0000 (03:03 -0400)
Apparently some versions of readline (6.0, for me) in some versions of
Python (Ubuntu's python2.6.4-0ubuntu1, for me) have an irritating bug
where they add an extra space to the end of all completions.  This is
particularly annoying for directory completions, as you can't
tab-complete your way into the contents of the directory.  See
http://bugs.python.org/issue5833

This patch, borrowed mostly from Trac, goes in and twiddles the
appropriate variable inside the readline library to make it stop doing
that.  See http://trac.edgewall.org/ticket/8711 for the discussion.

Signed-off-by: Peter McCurdy <petermccurdy@alumni.uwaterloo.ca>
cmd/ftp-cmd.py

index d40e72ac1956205eba97230c6a1e9de3fabdc516..7c2823fa1f7f214a6bf04324d9ac50500fc686d0 100755 (executable)
@@ -56,11 +56,44 @@ def _completer_get_subs(line):
     return (dir, name, qtype, lastword, subs)
 
 
+def find_readline_lib():
+    """Return the name (and possibly the full path) of the readline library
+    linked to the given readline module.
+    """
+    import readline
+    f = open(readline.__file__, "rb")
+    try:
+        data = f.read()
+    finally:
+        f.close()
+    import re
+    m = re.search('\0([^\0]*libreadline[^\0]*)\0', data)
+    if m:
+        return m.group(1)
+    return None
+
+
+def init_readline_vars():
+    """Work around trailing space automatically inserted by readline.
+    See http://bugs.python.org/issue5833"""
+    import ctypes
+    lib_name = find_readline_lib()
+    if lib_name is not None:
+        lib = ctypes.cdll.LoadLibrary(lib_name)
+        global rl_completion_suppress_append
+        rl_completion_suppress_append = ctypes.c_int.in_dll(lib,
+                                    "rl_completion_suppress_append")
+
+
+rl_completion_suppress_append = None
 _last_line = None
 _last_res = None
 def completer(text, state):
     global _last_line
     global _last_res
+    global rl_completion_suppress_append
+    if rl_completion_suppress_append is not None:
+        rl_completion_suppress_append.value = 1
     try:
         line = readline.get_line_buffer()[:readline.get_endidx()]
         if _last_line != line:
@@ -81,7 +114,7 @@ def completer(text, state):
     except Exception, e:
         log('\nerror in completion: %s\n' % e)
 
-            
+
 optspec = """
 bup ftp
 """
@@ -100,6 +133,7 @@ else:
         readline.set_completer_delims(' \t\n\r/')
         readline.set_completer(completer)
         readline.parse_and_bind("tab: complete")
+        init_readline_vars()
     lines = inputiter()
 
 for line in lines: