From: Peter McCurdy Date: Fri, 16 Apr 2010 07:11:13 +0000 (-0400) Subject: Work around extra space added by some readline versions. X-Git-Tag: bup-0.15~1 X-Git-Url: https://arthur.barton.de/gitweb/?a=commitdiff_plain;h=0e8b3a8509dd2de6805ec171f1724ef7ce477d72;p=bup.git Work around extra space added by some readline versions. 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 --- diff --git a/cmd/ftp-cmd.py b/cmd/ftp-cmd.py index d40e72a..7c2823f 100755 --- a/cmd/ftp-cmd.py +++ b/cmd/ftp-cmd.py @@ -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: