+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"""
+ try:
+ import ctypes
+ except ImportError:
+ # python before 2.5 didn't have the ctypes module; but those
+ # old systems probably also didn't have this readline bug, so
+ # just ignore it.
+ return
+ 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