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