##// END OF EJS Templates
Follow-up to 387dcd6a, `_rl.__doc__` is `None` with pyreadline
Christian Boos -
Show More
@@ -1,124 +1,124 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """ Imports and provides the 'correct' version of readline for the platform.
2 """ Imports and provides the 'correct' version of readline for the platform.
3
3
4 Readline is used throughout IPython as::
4 Readline is used throughout IPython as::
5
5
6 import IPython.utils.rlineimpl as readline
6 import IPython.utils.rlineimpl as readline
7
7
8 In addition to normal readline stuff, this module provides have_readline
8 In addition to normal readline stuff, this module provides have_readline
9 boolean and _outputfile variable used in IPython.utils.
9 boolean and _outputfile variable used in IPython.utils.
10 """
10 """
11
11
12 import os
12 import os
13 import re
13 import re
14 import sys
14 import sys
15 import time
15 import time
16 import warnings
16 import warnings
17
17
18 from subprocess import Popen, PIPE
18 from subprocess import Popen, PIPE
19
19
20 if sys.platform == 'darwin':
20 if sys.platform == 'darwin':
21 # dirty trick, to skip the system readline, because pip-installed readline
21 # dirty trick, to skip the system readline, because pip-installed readline
22 # will never be found on OSX, since lib-dynload always comes ahead of site-packages
22 # will never be found on OSX, since lib-dynload always comes ahead of site-packages
23 from distutils import sysconfig
23 from distutils import sysconfig
24 lib_dynload = sysconfig.get_config_var('DESTSHARED')
24 lib_dynload = sysconfig.get_config_var('DESTSHARED')
25 del sysconfig
25 del sysconfig
26 try:
26 try:
27 dynload_idx = sys.path.index(lib_dynload)
27 dynload_idx = sys.path.index(lib_dynload)
28 except ValueError:
28 except ValueError:
29 dynload_idx = None
29 dynload_idx = None
30 else:
30 else:
31 sys.path.pop(dynload_idx)
31 sys.path.pop(dynload_idx)
32 try:
32 try:
33 from readline import *
33 from readline import *
34 import readline as _rl
34 import readline as _rl
35 have_readline = True
35 have_readline = True
36 except ImportError:
36 except ImportError:
37 try:
37 try:
38 from pyreadline import *
38 from pyreadline import *
39 import pyreadline as _rl
39 import pyreadline as _rl
40 have_readline = True
40 have_readline = True
41 except ImportError:
41 except ImportError:
42 have_readline = False
42 have_readline = False
43
43
44 if sys.platform == 'darwin':
44 if sys.platform == 'darwin':
45 # dirty trick, part II:
45 # dirty trick, part II:
46 if dynload_idx is not None:
46 if dynload_idx is not None:
47 # restore path
47 # restore path
48 sys.path.insert(dynload_idx, lib_dynload)
48 sys.path.insert(dynload_idx, lib_dynload)
49 if not have_readline:
49 if not have_readline:
50 # *only* have system readline, try import again
50 # *only* have system readline, try import again
51 try:
51 try:
52 from readline import *
52 from readline import *
53 import readline as _rl
53 import readline as _rl
54 have_readline = True
54 have_readline = True
55 except ImportError:
55 except ImportError:
56 have_readline = False
56 have_readline = False
57 else:
57 else:
58 # if we want to warn about EPD / Fink having bad readline
58 # if we want to warn about EPD / Fink having bad readline
59 # we would do it here
59 # we would do it here
60 pass
60 pass
61 # cleanup dirty trick vars
61 # cleanup dirty trick vars
62 del dynload_idx, lib_dynload
62 del dynload_idx, lib_dynload
63
63
64 if have_readline and hasattr(_rl, 'rlmain'):
64 if have_readline and hasattr(_rl, 'rlmain'):
65 # patch add_history to allow for strings in pyreadline <= 1.5:
65 # patch add_history to allow for strings in pyreadline <= 1.5:
66 # fix copied from pyreadline 1.6
66 # fix copied from pyreadline 1.6
67 import pyreadline
67 import pyreadline
68 if pyreadline.release.version <= '1.5':
68 if pyreadline.release.version <= '1.5':
69 def add_history(line):
69 def add_history(line):
70 """add a line to the history buffer."""
70 """add a line to the history buffer."""
71 from pyreadline import lineobj
71 from pyreadline import lineobj
72 if not isinstance(line, lineobj.TextLine):
72 if not isinstance(line, lineobj.TextLine):
73 line = lineobj.TextLine(line)
73 line = lineobj.TextLine(line)
74 return _rl.add_history(line)
74 return _rl.add_history(line)
75
75
76 if sys.platform == 'win32' and have_readline:
76 if sys.platform == 'win32' and have_readline:
77 try:
77 try:
78 _outputfile=_rl.GetOutputFile()
78 _outputfile=_rl.GetOutputFile()
79 except AttributeError:
79 except AttributeError:
80 warnings.warn("Failed GetOutputFile")
80 warnings.warn("Failed GetOutputFile")
81 have_readline = False
81 have_readline = False
82
82
83 # Test to see if libedit is being used instead of GNU readline.
83 # Test to see if libedit is being used instead of GNU readline.
84 # Thanks to Boyd Waters for the original patch.
84 # Thanks to Boyd Waters for the original patch.
85 uses_libedit = False
85 uses_libedit = False
86
86
87 if have_readline:
87 if have_readline:
88 # Official Python docs state that 'libedit' is in the docstring for libedit readline:
88 # Official Python docs state that 'libedit' is in the docstring for libedit readline:
89 uses_libedit = 'libedit' in _rl.__doc__
89 uses_libedit = _rl.__doc__ and 'libedit' in _rl.__doc__
90 # Note that many non-System Pythons also do not use proper readline,
90 # Note that many non-System Pythons also do not use proper readline,
91 # but do not report libedit at all, nor are they linked dynamically against libedit.
91 # but do not report libedit at all, nor are they linked dynamically against libedit.
92 # known culprits of this include: EPD, Fink
92 # known culprits of this include: EPD, Fink
93 # There is not much we can do to detect this, until we find a specific failure
93 # There is not much we can do to detect this, until we find a specific failure
94 # case, rather than relying on the readline module to self-identify as broken.
94 # case, rather than relying on the readline module to self-identify as broken.
95
95
96 if uses_libedit and sys.platform == 'darwin':
96 if uses_libedit and sys.platform == 'darwin':
97 _rl.parse_and_bind("bind ^I rl_complete")
97 _rl.parse_and_bind("bind ^I rl_complete")
98 warnings.warn('\n'.join(['', "*"*78,
98 warnings.warn('\n'.join(['', "*"*78,
99 "libedit detected - readline will not be well behaved, including but not limited to:",
99 "libedit detected - readline will not be well behaved, including but not limited to:",
100 " * crashes on tab completion",
100 " * crashes on tab completion",
101 " * incorrect history navigation",
101 " * incorrect history navigation",
102 " * corrupting long-lines",
102 " * corrupting long-lines",
103 " * failure to wrap or indent lines properly",
103 " * failure to wrap or indent lines properly",
104 "It is highly recommended that you install readline, which is easy_installable:",
104 "It is highly recommended that you install readline, which is easy_installable:",
105 " easy_install readline",
105 " easy_install readline",
106 "Note that `pip install readline` generally DOES NOT WORK, because",
106 "Note that `pip install readline` generally DOES NOT WORK, because",
107 "it installs to site-packages, which come *after* lib-dynload in sys.path,",
107 "it installs to site-packages, which come *after* lib-dynload in sys.path,",
108 "where readline is located. It must be `easy_install readline`, or to a custom",
108 "where readline is located. It must be `easy_install readline`, or to a custom",
109 "location on your PYTHONPATH (even --user comes after lib-dyload).",
109 "location on your PYTHONPATH (even --user comes after lib-dyload).",
110 "*"*78]),
110 "*"*78]),
111 RuntimeWarning)
111 RuntimeWarning)
112
112
113 # the clear_history() function was only introduced in Python 2.4 and is
113 # the clear_history() function was only introduced in Python 2.4 and is
114 # actually optional in the readline API, so we must explicitly check for its
114 # actually optional in the readline API, so we must explicitly check for its
115 # existence. Some known platforms actually don't have it. This thread:
115 # existence. Some known platforms actually don't have it. This thread:
116 # http://mail.python.org/pipermail/python-dev/2003-August/037845.html
116 # http://mail.python.org/pipermail/python-dev/2003-August/037845.html
117 # has the original discussion.
117 # has the original discussion.
118
118
119 if have_readline:
119 if have_readline:
120 try:
120 try:
121 _rl.clear_history
121 _rl.clear_history
122 except AttributeError:
122 except AttributeError:
123 def clear_history(): pass
123 def clear_history(): pass
124 _rl.clear_history = clear_history
124 _rl.clear_history = clear_history
General Comments 0
You need to be logged in to leave comments. Login now