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