Show More
@@ -1,112 +1,112 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 os |
|
13 | 13 | import re |
|
14 | 14 | import sys |
|
15 | 15 | import time |
|
16 | 16 | import warnings |
|
17 | 17 | |
|
18 | 18 | from subprocess import Popen, PIPE |
|
19 | 19 | |
|
20 | 20 | try: |
|
21 | 21 | from readline import * |
|
22 | 22 | import readline as _rl |
|
23 | 23 | have_readline = True |
|
24 | 24 | except ImportError: |
|
25 | 25 | try: |
|
26 | 26 | from pyreadline import * |
|
27 | 27 | import pyreadline as _rl |
|
28 | 28 | have_readline = True |
|
29 | 29 | except ImportError: |
|
30 | 30 | have_readline = False |
|
31 | 31 | |
|
32 | 32 | if have_readline and hasattr(_rl, 'rlmain'): |
|
33 | 33 | # patch add_history to allow for strings in pyreadline <= 1.5: |
|
34 | 34 | # fix copied from pyreadline 1.6 |
|
35 | 35 | import pyreadline |
|
36 | 36 | if pyreadline.release.version <= '1.5': |
|
37 | 37 | def add_history(line): |
|
38 | 38 | """add a line to the history buffer.""" |
|
39 | 39 | from pyreadline import lineobj |
|
40 | 40 | if not isinstance(line, lineobj.TextLine): |
|
41 | 41 | line = lineobj.TextLine(line) |
|
42 | 42 | return _rl.add_history(line) |
|
43 | 43 | |
|
44 | 44 | if sys.platform == 'win32' and have_readline: |
|
45 | 45 | try: |
|
46 | 46 | _outputfile=_rl.GetOutputFile() |
|
47 | 47 | except AttributeError: |
|
48 |
|
|
|
48 | warnings.warn("Failed GetOutputFile") | |
|
49 | 49 | have_readline = False |
|
50 | 50 | |
|
51 | 51 | # Test to see if libedit is being used instead of GNU readline. |
|
52 | 52 | # Thanks to Boyd Waters for the original patch. |
|
53 | 53 | uses_libedit = False |
|
54 | 54 | if sys.platform == 'darwin' and have_readline: |
|
55 | 55 | # Previously this used commands.getstatusoutput, which uses os.popen. |
|
56 | 56 | # Switching to subprocess.Popen, and exponential falloff for EINTR |
|
57 | 57 | # seems to make this better behaved in environments such as PyQt and gdb |
|
58 | 58 | dt = 1e-3 |
|
59 | 59 | while dt < 1: |
|
60 | 60 | try: |
|
61 | 61 | p = Popen(['otool', '-L', _rl.__file__], stdout=PIPE, stderr=PIPE) |
|
62 | 62 | except OSError: |
|
63 | 63 | try: |
|
64 | 64 | # otool not available (no XCode), use lsof instead. |
|
65 | 65 | # This *could* have a false positive |
|
66 | 66 | # if another package that uses libedit explicitly |
|
67 | 67 | # has been imported prior to this test. |
|
68 | 68 | p = Popen(['lsof', '-p', str(os.getpid())], stdout=PIPE, stderr=PIPE) |
|
69 | 69 | except OSError: |
|
70 | 70 | # This is highly unlikely, but let's be sure |
|
71 | 71 | # we don't crash IPython just because we can't find lsof |
|
72 | 72 | p = out = err = None |
|
73 | 73 | warnings.warn("libedit detection failed") |
|
74 | 74 | break |
|
75 | 75 | |
|
76 | 76 | out,err = p.communicate() |
|
77 | 77 | |
|
78 | 78 | if p.returncode == 4: |
|
79 | 79 | # EINTR |
|
80 | 80 | time.sleep(dt) |
|
81 | 81 | dt *= 2 |
|
82 | 82 | continue |
|
83 | 83 | elif p is None or p.returncode: |
|
84 | 84 | warnings.warn("libedit detection failed: %s"%err) |
|
85 | 85 | break |
|
86 | 86 | else: |
|
87 | 87 | break |
|
88 | 88 | |
|
89 | if p is not None and p.returncode == 0 and re.search(r'/libedit[\.\d+]*\.dylib\s', out): | |
|
89 | if p is not None and p.returncode == 0 and re.search(br'/libedit[\.\d+]*\.dylib\s', out): | |
|
90 | 90 | # we are bound to libedit - new in Leopard |
|
91 | 91 | _rl.parse_and_bind("bind ^I rl_complete") |
|
92 | 92 | warnings.warn("Leopard libedit detected - readline will not be well behaved " |
|
93 | 93 | "including some crashes on tab completion, and incorrect history navigation. " |
|
94 | 94 | "It is highly recommended that you install readline, " |
|
95 | 95 | "which is easy_installable with: 'easy_install readline'", |
|
96 | 96 | RuntimeWarning) |
|
97 | 97 | uses_libedit = True |
|
98 | 98 | # cleanup names |
|
99 | 99 | del dt,p,out,err |
|
100 | 100 | |
|
101 | 101 | # the clear_history() function was only introduced in Python 2.4 and is |
|
102 | 102 | # actually optional in the readline API, so we must explicitly check for its |
|
103 | 103 | # existence. Some known platforms actually don't have it. This thread: |
|
104 | 104 | # http://mail.python.org/pipermail/python-dev/2003-August/037845.html |
|
105 | 105 | # has the original discussion. |
|
106 | 106 | |
|
107 | 107 | if have_readline: |
|
108 | 108 | try: |
|
109 | 109 | _rl.clear_history |
|
110 | 110 | except AttributeError: |
|
111 | 111 | def clear_history(): pass |
|
112 | 112 | _rl.clear_history = clear_history |
General Comments 0
You need to be logged in to leave comments.
Login now