Show More
@@ -1,76 +1,74 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 | _rlmod_names = ['gnureadline', 'readline'] |
|
15 | _rlmod_names = ['gnureadline', 'readline'] | |
16 | if sys.platform == 'win32' or sys.platform == 'cli': |
|
|||
17 | _rlmod_names.append('pyreadline') |
|
|||
18 |
|
16 | |||
19 | have_readline = False |
|
17 | have_readline = False | |
20 | for _rlmod_name in _rlmod_names: |
|
18 | for _rlmod_name in _rlmod_names: | |
21 | try: |
|
19 | try: | |
22 | # import readline as _rl |
|
20 | # import readline as _rl | |
23 | _rl = __import__(_rlmod_name) |
|
21 | _rl = __import__(_rlmod_name) | |
24 | # from readline import * |
|
22 | # from readline import * | |
25 | globals().update({k:v for k,v in _rl.__dict__.items() if not k.startswith('_')}) |
|
23 | globals().update({k:v for k,v in _rl.__dict__.items() if not k.startswith('_')}) | |
26 | except ImportError: |
|
24 | except ImportError: | |
27 | pass |
|
25 | pass | |
28 | else: |
|
26 | else: | |
29 | have_readline = True |
|
27 | have_readline = True | |
30 | break |
|
28 | break | |
31 |
|
29 | |||
32 | if _rlmod_name == 'pyreadline': |
|
30 | if sys.platform == 'win32' or sys.platform == 'cli': | |
33 | try: |
|
31 | try: | |
34 | _outputfile=_rl.GetOutputFile() |
|
32 | _outputfile=_rl.GetOutputFile() | |
35 | except AttributeError: |
|
33 | except AttributeError: | |
36 | warnings.warn("Failed GetOutputFile") |
|
34 | warnings.warn("Failed GetOutputFile") | |
37 | have_readline = False |
|
35 | have_readline = False | |
38 |
|
36 | |||
39 | # Test to see if libedit is being used instead of GNU readline. |
|
37 | # Test to see if libedit is being used instead of GNU readline. | |
40 | # Thanks to Boyd Waters for the original patch. |
|
38 | # Thanks to Boyd Waters for the original patch. | |
41 | uses_libedit = False |
|
39 | uses_libedit = False | |
42 |
|
40 | |||
43 | if have_readline: |
|
41 | if have_readline: | |
44 | # Official Python docs state that 'libedit' is in the docstring for libedit readline: |
|
42 | # Official Python docs state that 'libedit' is in the docstring for libedit readline: | |
45 | uses_libedit = _rl.__doc__ and 'libedit' in _rl.__doc__ |
|
43 | uses_libedit = _rl.__doc__ and 'libedit' in _rl.__doc__ | |
46 | # Note that many non-System Pythons also do not use proper readline, |
|
44 | # Note that many non-System Pythons also do not use proper readline, | |
47 | # but do not report libedit at all, nor are they linked dynamically against libedit. |
|
45 | # but do not report libedit at all, nor are they linked dynamically against libedit. | |
48 | # known culprits of this include: EPD, Fink |
|
46 | # known culprits of this include: EPD, Fink | |
49 | # There is not much we can do to detect this, until we find a specific failure |
|
47 | # There is not much we can do to detect this, until we find a specific failure | |
50 | # case, rather than relying on the readline module to self-identify as broken. |
|
48 | # case, rather than relying on the readline module to self-identify as broken. | |
51 |
|
49 | |||
52 | if uses_libedit and sys.platform == 'darwin': |
|
50 | if uses_libedit and sys.platform == 'darwin': | |
53 | _rl.parse_and_bind("bind ^I rl_complete") |
|
51 | _rl.parse_and_bind("bind ^I rl_complete") | |
54 | warnings.warn('\n'.join(['', "*"*78, |
|
52 | warnings.warn('\n'.join(['', "*"*78, | |
55 | "libedit detected - readline will not be well behaved, including but not limited to:", |
|
53 | "libedit detected - readline will not be well behaved, including but not limited to:", | |
56 | " * crashes on tab completion", |
|
54 | " * crashes on tab completion", | |
57 | " * incorrect history navigation", |
|
55 | " * incorrect history navigation", | |
58 | " * corrupting long-lines", |
|
56 | " * corrupting long-lines", | |
59 | " * failure to wrap or indent lines properly", |
|
57 | " * failure to wrap or indent lines properly", | |
60 | "It is highly recommended that you install gnureadline, which is installable with:", |
|
58 | "It is highly recommended that you install gnureadline, which is installable with:", | |
61 | " pip install gnureadline", |
|
59 | " pip install gnureadline", | |
62 | "*"*78]), |
|
60 | "*"*78]), | |
63 | RuntimeWarning) |
|
61 | RuntimeWarning) | |
64 |
|
62 | |||
65 | # the clear_history() function was only introduced in Python 2.4 and is |
|
63 | # the clear_history() function was only introduced in Python 2.4 and is | |
66 | # actually optional in the readline API, so we must explicitly check for its |
|
64 | # actually optional in the readline API, so we must explicitly check for its | |
67 | # existence. Some known platforms actually don't have it. This thread: |
|
65 | # existence. Some known platforms actually don't have it. This thread: | |
68 | # http://mail.python.org/pipermail/python-dev/2003-August/037845.html |
|
66 | # http://mail.python.org/pipermail/python-dev/2003-August/037845.html | |
69 | # has the original discussion. |
|
67 | # has the original discussion. | |
70 |
|
68 | |||
71 | if have_readline: |
|
69 | if have_readline: | |
72 | try: |
|
70 | try: | |
73 | _rl.clear_history |
|
71 | _rl.clear_history | |
74 | except AttributeError: |
|
72 | except AttributeError: | |
75 | def clear_history(): pass |
|
73 | def clear_history(): pass | |
76 | _rl.clear_history = clear_history |
|
74 | _rl.clear_history = clear_history |
General Comments 0
You need to be logged in to leave comments.
Login now