Show More
@@ -1,74 +1,86 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 | try: |
|
15 | try: | |
16 | from readline import * |
|
16 | from readline import * | |
17 | import readline as _rl |
|
17 | import readline as _rl | |
18 | have_readline = True |
|
18 | have_readline = True | |
19 | except ImportError: |
|
19 | except ImportError: | |
20 | try: |
|
20 | try: | |
21 | from pyreadline import * |
|
21 | from pyreadline import * | |
22 | import pyreadline as _rl |
|
22 | import pyreadline as _rl | |
23 | have_readline = True |
|
23 | have_readline = True | |
24 |
except ImportError: |
|
24 | except ImportError: | |
25 | have_readline = False |
|
25 | have_readline = False | |
26 |
|
26 | |||
|
27 | if have_readline and hasattr(_rl, 'rlmain'): | |||
|
28 | # patch add_history to allow for strings in pyreadline <= 1.5: | |||
|
29 | # fix copied from pyreadline 1.6 | |||
|
30 | import pyreadline | |||
|
31 | if pyreadline.release.version <= '1.5': | |||
|
32 | def add_history(line): | |||
|
33 | """add a line to the history buffer.""" | |||
|
34 | from pyreadline import lineobj | |||
|
35 | if not isinstance(line, lineobj.TextLine): | |||
|
36 | line = lineobj.TextLine(line) | |||
|
37 | return _rl.add_history(line) | |||
|
38 | ||||
27 | if sys.platform == 'win32' and have_readline: |
|
39 | if sys.platform == 'win32' and have_readline: | |
28 | try: |
|
40 | try: | |
29 | _outputfile=_rl.GetOutputFile() |
|
41 | _outputfile=_rl.GetOutputFile() | |
30 | except AttributeError: |
|
42 | except AttributeError: | |
31 | print "Failed GetOutputFile" |
|
43 | print "Failed GetOutputFile" | |
32 | have_readline = False |
|
44 | have_readline = False | |
33 |
|
45 | |||
34 | # Test to see if libedit is being used instead of GNU readline. |
|
46 | # Test to see if libedit is being used instead of GNU readline. | |
35 | # Thanks to Boyd Waters for this patch. |
|
47 | # Thanks to Boyd Waters for this patch. | |
36 | uses_libedit = False |
|
48 | uses_libedit = False | |
37 | if sys.platform == 'darwin' and have_readline: |
|
49 | if sys.platform == 'darwin' and have_readline: | |
38 | import commands |
|
50 | import commands | |
39 | # Boyd's patch had a 'while True' here, I'm always a little worried about |
|
51 | # Boyd's patch had a 'while True' here, I'm always a little worried about | |
40 | # infinite loops with such code, so for now I'm taking a more conservative |
|
52 | # infinite loops with such code, so for now I'm taking a more conservative | |
41 | # approach. See https://bugs.launchpad.net/ipython/+bug/411599. |
|
53 | # approach. See https://bugs.launchpad.net/ipython/+bug/411599. | |
42 | for i in range(10): |
|
54 | for i in range(10): | |
43 | try: |
|
55 | try: | |
44 | (status, result) = commands.getstatusoutput( "otool -L %s | grep libedit" % _rl.__file__ ) |
|
56 | (status, result) = commands.getstatusoutput( "otool -L %s | grep libedit" % _rl.__file__ ) | |
45 | break |
|
57 | break | |
46 | except IOError, (errno, strerror): |
|
58 | except IOError, (errno, strerror): | |
47 | if errno == 4: |
|
59 | if errno == 4: | |
48 | continue |
|
60 | continue | |
49 | else: |
|
61 | else: | |
50 | break |
|
62 | break | |
51 |
|
63 | |||
52 | if status == 0 and len(result) > 0: |
|
64 | if status == 0 and len(result) > 0: | |
53 | # we are bound to libedit - new in Leopard |
|
65 | # we are bound to libedit - new in Leopard | |
54 | _rl.parse_and_bind("bind ^I rl_complete") |
|
66 | _rl.parse_and_bind("bind ^I rl_complete") | |
55 | warnings.warn("Leopard libedit detected - readline will not be well behaved " |
|
67 | warnings.warn("Leopard libedit detected - readline will not be well behaved " | |
56 | "including some crashes on tab completion, and incorrect history navigation. " |
|
68 | "including some crashes on tab completion, and incorrect history navigation. " | |
57 | "It is highly recommended that you install readline, " |
|
69 | "It is highly recommended that you install readline, " | |
58 | "which is easy_installable with: 'easy_install readline'", |
|
70 | "which is easy_installable with: 'easy_install readline'", | |
59 | RuntimeWarning) |
|
71 | RuntimeWarning) | |
60 | uses_libedit = True |
|
72 | uses_libedit = True | |
61 |
|
73 | |||
62 |
|
74 | |||
63 | # the clear_history() function was only introduced in Python 2.4 and is |
|
75 | # the clear_history() function was only introduced in Python 2.4 and is | |
64 | # actually optional in the readline API, so we must explicitly check for its |
|
76 | # actually optional in the readline API, so we must explicitly check for its | |
65 | # existence. Some known platforms actually don't have it. This thread: |
|
77 | # existence. Some known platforms actually don't have it. This thread: | |
66 | # http://mail.python.org/pipermail/python-dev/2003-August/037845.html |
|
78 | # http://mail.python.org/pipermail/python-dev/2003-August/037845.html | |
67 | # has the original discussion. |
|
79 | # has the original discussion. | |
68 |
|
80 | |||
69 | if have_readline: |
|
81 | if have_readline: | |
70 | try: |
|
82 | try: | |
71 | _rl.clear_history |
|
83 | _rl.clear_history | |
72 | except AttributeError: |
|
84 | except AttributeError: | |
73 | def clear_history(): pass |
|
85 | def clear_history(): pass | |
74 | _rl.clear_history = clear_history |
|
86 | _rl.clear_history = clear_history |
General Comments 0
You need to be logged in to leave comments.
Login now