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