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