Show More
@@ -0,0 +1,73 b'' | |||||
|
1 | from IPython.core.debugger import Pdb | |||
|
2 | ||||
|
3 | from IPython.core.completer import IPCompleter | |||
|
4 | from .ptutils import IPythonPTCompleter | |||
|
5 | ||||
|
6 | from prompt_toolkit.token import Token | |||
|
7 | from prompt_toolkit.shortcuts import create_prompt_application | |||
|
8 | from prompt_toolkit.interface import CommandLineInterface | |||
|
9 | from prompt_toolkit.enums import EditingMode | |||
|
10 | ||||
|
11 | class TerminalPdb(Pdb): | |||
|
12 | def __init__(self, *args, **kwargs): | |||
|
13 | Pdb.__init__(self, *args, **kwargs) | |||
|
14 | self._ptcomp = None | |||
|
15 | self.pt_init() | |||
|
16 | ||||
|
17 | def pt_init(self): | |||
|
18 | def get_prompt_tokens(cli): | |||
|
19 | return [(Token.Prompt, self.prompt)] | |||
|
20 | ||||
|
21 | if self._ptcomp is None: | |||
|
22 | compl = IPCompleter(shell=self.shell, | |||
|
23 | namespace={}, | |||
|
24 | global_namespace={}, | |||
|
25 | use_readline=False, | |||
|
26 | parent=self.shell, | |||
|
27 | ) | |||
|
28 | self._ptcomp = IPythonPTCompleter(compl) | |||
|
29 | ||||
|
30 | self._pt_app = create_prompt_application( | |||
|
31 | editing_mode=getattr(EditingMode, self.shell.editing_mode.upper()), | |||
|
32 | history=self.shell.debugger_history, | |||
|
33 | completer= self._ptcomp, | |||
|
34 | enable_history_search=True, | |||
|
35 | mouse_support=self.shell.mouse_support, | |||
|
36 | get_prompt_tokens=get_prompt_tokens | |||
|
37 | ) | |||
|
38 | self.pt_cli = CommandLineInterface(self._pt_app, eventloop=self.shell._eventloop) | |||
|
39 | ||||
|
40 | def cmdloop(self, intro=None): | |||
|
41 | """Repeatedly issue a prompt, accept input, parse an initial prefix | |||
|
42 | off the received input, and dispatch to action methods, passing them | |||
|
43 | the remainder of the line as argument. | |||
|
44 | ||||
|
45 | override the same methods from cmd.Cmd to provide prompt toolkit replacement. | |||
|
46 | """ | |||
|
47 | if not self.use_rawinput: | |||
|
48 | raise ValueError('Sorry ipdb does not support use_rawinput=False') | |||
|
49 | ||||
|
50 | self.preloop() | |||
|
51 | ||||
|
52 | try: | |||
|
53 | if intro is not None: | |||
|
54 | self.intro = intro | |||
|
55 | if self.intro: | |||
|
56 | self.stdout.write(str(self.intro)+"\n") | |||
|
57 | stop = None | |||
|
58 | while not stop: | |||
|
59 | if self.cmdqueue: | |||
|
60 | line = self.cmdqueue.pop(0) | |||
|
61 | else: | |||
|
62 | self._ptcomp.ipy_completer.namespace = self.curframe_locals | |||
|
63 | self._ptcomp.ipy_completer.global_namespace = self.curframe.f_globals | |||
|
64 | try: | |||
|
65 | line = self.pt_cli.run(reset_current_buffer=True).text | |||
|
66 | except EOFError: | |||
|
67 | line = 'EOF' | |||
|
68 | line = self.precmd(line) | |||
|
69 | stop = self.onecmd(line) | |||
|
70 | stop = self.postcmd(stop, line) | |||
|
71 | self.postloop() | |||
|
72 | except Exception: | |||
|
73 | raise |
@@ -0,0 +1,62 b'' | |||||
|
1 | import unicodedata | |||
|
2 | from wcwidth import wcwidth | |||
|
3 | ||||
|
4 | from IPython.utils.py3compat import PY3 | |||
|
5 | ||||
|
6 | from prompt_toolkit.completion import Completer, Completion | |||
|
7 | from prompt_toolkit.layout.lexers import Lexer | |||
|
8 | from prompt_toolkit.layout.lexers import PygmentsLexer | |||
|
9 | ||||
|
10 | from pygments.lexers import Python3Lexer, BashLexer, PythonLexer | |||
|
11 | ||||
|
12 | class IPythonPTCompleter(Completer): | |||
|
13 | """Adaptor to provide IPython completions to prompt_toolkit""" | |||
|
14 | def __init__(self, ipy_completer): | |||
|
15 | self.ipy_completer = ipy_completer | |||
|
16 | ||||
|
17 | def get_completions(self, document, complete_event): | |||
|
18 | if not document.current_line.strip(): | |||
|
19 | return | |||
|
20 | ||||
|
21 | used, matches = self.ipy_completer.complete( | |||
|
22 | line_buffer=document.current_line, | |||
|
23 | cursor_pos=document.cursor_position_col | |||
|
24 | ) | |||
|
25 | start_pos = -len(used) | |||
|
26 | for m in matches: | |||
|
27 | m = unicodedata.normalize('NFC', m) | |||
|
28 | ||||
|
29 | # When the first character of the completion has a zero length, | |||
|
30 | # then it's probably a decomposed unicode character. E.g. caused by | |||
|
31 | # the "\dot" completion. Try to compose again with the previous | |||
|
32 | # character. | |||
|
33 | if wcwidth(m[0]) == 0: | |||
|
34 | if document.cursor_position + start_pos > 0: | |||
|
35 | char_before = document.text[document.cursor_position + start_pos - 1] | |||
|
36 | m = unicodedata.normalize('NFC', char_before + m) | |||
|
37 | ||||
|
38 | # Yield the modified completion instead, if this worked. | |||
|
39 | if wcwidth(m[0:1]) == 1: | |||
|
40 | yield Completion(m, start_position=start_pos - 1) | |||
|
41 | continue | |||
|
42 | ||||
|
43 | # TODO: Use Jedi to determine meta_text | |||
|
44 | # (Jedi currently has a bug that results in incorrect information.) | |||
|
45 | # meta_text = '' | |||
|
46 | # yield Completion(m, start_position=start_pos, | |||
|
47 | # display_meta=meta_text) | |||
|
48 | yield Completion(m, start_position=start_pos) | |||
|
49 | ||||
|
50 | class IPythonPTLexer(Lexer): | |||
|
51 | """ | |||
|
52 | Wrapper around PythonLexer and BashLexer. | |||
|
53 | """ | |||
|
54 | def __init__(self): | |||
|
55 | self.python_lexer = PygmentsLexer(Python3Lexer if PY3 else PythonLexer) | |||
|
56 | self.shell_lexer = PygmentsLexer(BashLexer) | |||
|
57 | ||||
|
58 | def lex_document(self, cli, document): | |||
|
59 | if document.text.startswith('!'): | |||
|
60 | return self.shell_lexer.lex_document(cli, document) | |||
|
61 | else: | |||
|
62 | return self.python_lexer.lex_document(cli, document) |
@@ -1,1265 +1,1271 b'' | |||||
1 | # encoding: utf-8 |
|
1 | # encoding: utf-8 | |
2 | """Word completion for IPython. |
|
2 | """Word completion for IPython. | |
3 |
|
3 | |||
4 | This module is a fork of the rlcompleter module in the Python standard |
|
4 | This module is a fork of the rlcompleter module in the Python standard | |
5 | library. The original enhancements made to rlcompleter have been sent |
|
5 | library. The original enhancements made to rlcompleter have been sent | |
6 | upstream and were accepted as of Python 2.3, but we need a lot more |
|
6 | upstream and were accepted as of Python 2.3, but we need a lot more | |
7 | functionality specific to IPython, so this module will continue to live as an |
|
7 | functionality specific to IPython, so this module will continue to live as an | |
8 | IPython-specific utility. |
|
8 | IPython-specific utility. | |
9 |
|
9 | |||
10 | Original rlcompleter documentation: |
|
10 | Original rlcompleter documentation: | |
11 |
|
11 | |||
12 | This requires the latest extension to the readline module (the |
|
12 | This requires the latest extension to the readline module (the | |
13 | completes keywords, built-ins and globals in __main__; when completing |
|
13 | completes keywords, built-ins and globals in __main__; when completing | |
14 | NAME.NAME..., it evaluates (!) the expression up to the last dot and |
|
14 | NAME.NAME..., it evaluates (!) the expression up to the last dot and | |
15 | completes its attributes. |
|
15 | completes its attributes. | |
16 |
|
16 | |||
17 | It's very cool to do "import string" type "string.", hit the |
|
17 | It's very cool to do "import string" type "string.", hit the | |
18 | completion key (twice), and see the list of names defined by the |
|
18 | completion key (twice), and see the list of names defined by the | |
19 | string module! |
|
19 | string module! | |
20 |
|
20 | |||
21 | Tip: to use the tab key as the completion key, call |
|
21 | Tip: to use the tab key as the completion key, call | |
22 |
|
22 | |||
23 | readline.parse_and_bind("tab: complete") |
|
23 | readline.parse_and_bind("tab: complete") | |
24 |
|
24 | |||
25 | Notes: |
|
25 | Notes: | |
26 |
|
26 | |||
27 | - Exceptions raised by the completer function are *ignored* (and |
|
27 | - Exceptions raised by the completer function are *ignored* (and | |
28 | generally cause the completion to fail). This is a feature -- since |
|
28 | generally cause the completion to fail). This is a feature -- since | |
29 | readline sets the tty device in raw (or cbreak) mode, printing a |
|
29 | readline sets the tty device in raw (or cbreak) mode, printing a | |
30 | traceback wouldn't work well without some complicated hoopla to save, |
|
30 | traceback wouldn't work well without some complicated hoopla to save, | |
31 | reset and restore the tty state. |
|
31 | reset and restore the tty state. | |
32 |
|
32 | |||
33 | - The evaluation of the NAME.NAME... form may cause arbitrary |
|
33 | - The evaluation of the NAME.NAME... form may cause arbitrary | |
34 | application defined code to be executed if an object with a |
|
34 | application defined code to be executed if an object with a | |
35 | ``__getattr__`` hook is found. Since it is the responsibility of the |
|
35 | ``__getattr__`` hook is found. Since it is the responsibility of the | |
36 | application (or the user) to enable this feature, I consider this an |
|
36 | application (or the user) to enable this feature, I consider this an | |
37 | acceptable risk. More complicated expressions (e.g. function calls or |
|
37 | acceptable risk. More complicated expressions (e.g. function calls or | |
38 | indexing operations) are *not* evaluated. |
|
38 | indexing operations) are *not* evaluated. | |
39 |
|
39 | |||
40 | - GNU readline is also used by the built-in functions input() and |
|
40 | - GNU readline is also used by the built-in functions input() and | |
41 | raw_input(), and thus these also benefit/suffer from the completer |
|
41 | raw_input(), and thus these also benefit/suffer from the completer | |
42 | features. Clearly an interactive application can benefit by |
|
42 | features. Clearly an interactive application can benefit by | |
43 | specifying its own completer function and using raw_input() for all |
|
43 | specifying its own completer function and using raw_input() for all | |
44 | its input. |
|
44 | its input. | |
45 |
|
45 | |||
46 | - When the original stdin is not a tty device, GNU readline is never |
|
46 | - When the original stdin is not a tty device, GNU readline is never | |
47 | used, and this module (and the readline module) are silently inactive. |
|
47 | used, and this module (and the readline module) are silently inactive. | |
48 | """ |
|
48 | """ | |
49 |
|
49 | |||
50 | # Copyright (c) IPython Development Team. |
|
50 | # Copyright (c) IPython Development Team. | |
51 | # Distributed under the terms of the Modified BSD License. |
|
51 | # Distributed under the terms of the Modified BSD License. | |
52 | # |
|
52 | # | |
53 | # Some of this code originated from rlcompleter in the Python standard library |
|
53 | # Some of this code originated from rlcompleter in the Python standard library | |
54 | # Copyright (C) 2001 Python Software Foundation, www.python.org |
|
54 | # Copyright (C) 2001 Python Software Foundation, www.python.org | |
55 |
|
55 | |||
56 | from __future__ import print_function |
|
56 | from __future__ import print_function | |
57 |
|
57 | |||
58 | import __main__ |
|
58 | import __main__ | |
59 | import glob |
|
59 | import glob | |
60 | import inspect |
|
60 | import inspect | |
61 | import itertools |
|
61 | import itertools | |
62 | import keyword |
|
62 | import keyword | |
63 | import os |
|
63 | import os | |
64 | import re |
|
64 | import re | |
65 | import sys |
|
65 | import sys | |
66 | import unicodedata |
|
66 | import unicodedata | |
67 | import string |
|
67 | import string | |
68 |
|
68 | |||
69 | from traitlets.config.configurable import Configurable |
|
69 | from traitlets.config.configurable import Configurable | |
70 | from IPython.core.error import TryNext |
|
70 | from IPython.core.error import TryNext | |
71 | from IPython.core.inputsplitter import ESC_MAGIC |
|
71 | from IPython.core.inputsplitter import ESC_MAGIC | |
72 | from IPython.core.latex_symbols import latex_symbols, reverse_latex_symbol |
|
72 | from IPython.core.latex_symbols import latex_symbols, reverse_latex_symbol | |
73 | from IPython.utils import generics |
|
73 | from IPython.utils import generics | |
74 | from IPython.utils.decorators import undoc |
|
74 | from IPython.utils.decorators import undoc | |
75 | from IPython.utils.dir2 import dir2, get_real_method |
|
75 | from IPython.utils.dir2 import dir2, get_real_method | |
76 | from IPython.utils.process import arg_split |
|
76 | from IPython.utils.process import arg_split | |
77 | from IPython.utils.py3compat import builtin_mod, string_types, PY3, cast_unicode_py2 |
|
77 | from IPython.utils.py3compat import builtin_mod, string_types, PY3, cast_unicode_py2 | |
78 | from traitlets import Bool, Enum, observe |
|
78 | from traitlets import Bool, Enum, observe | |
79 |
|
79 | |||
80 | #----------------------------------------------------------------------------- |
|
80 | #----------------------------------------------------------------------------- | |
81 | # Globals |
|
81 | # Globals | |
82 | #----------------------------------------------------------------------------- |
|
82 | #----------------------------------------------------------------------------- | |
83 |
|
83 | |||
84 | # Public API |
|
84 | # Public API | |
85 | __all__ = ['Completer','IPCompleter'] |
|
85 | __all__ = ['Completer','IPCompleter'] | |
86 |
|
86 | |||
87 | if sys.platform == 'win32': |
|
87 | if sys.platform == 'win32': | |
88 | PROTECTABLES = ' ' |
|
88 | PROTECTABLES = ' ' | |
89 | else: |
|
89 | else: | |
90 | PROTECTABLES = ' ()[]{}?=\\|;:\'#*"^&' |
|
90 | PROTECTABLES = ' ()[]{}?=\\|;:\'#*"^&' | |
91 |
|
91 | |||
92 |
|
92 | |||
93 | #----------------------------------------------------------------------------- |
|
93 | #----------------------------------------------------------------------------- | |
94 | # Main functions and classes |
|
94 | # Main functions and classes | |
95 | #----------------------------------------------------------------------------- |
|
95 | #----------------------------------------------------------------------------- | |
96 |
|
96 | |||
97 | def has_open_quotes(s): |
|
97 | def has_open_quotes(s): | |
98 | """Return whether a string has open quotes. |
|
98 | """Return whether a string has open quotes. | |
99 |
|
99 | |||
100 | This simply counts whether the number of quote characters of either type in |
|
100 | This simply counts whether the number of quote characters of either type in | |
101 | the string is odd. |
|
101 | the string is odd. | |
102 |
|
102 | |||
103 | Returns |
|
103 | Returns | |
104 | ------- |
|
104 | ------- | |
105 | If there is an open quote, the quote character is returned. Else, return |
|
105 | If there is an open quote, the quote character is returned. Else, return | |
106 | False. |
|
106 | False. | |
107 | """ |
|
107 | """ | |
108 | # We check " first, then ', so complex cases with nested quotes will get |
|
108 | # We check " first, then ', so complex cases with nested quotes will get | |
109 | # the " to take precedence. |
|
109 | # the " to take precedence. | |
110 | if s.count('"') % 2: |
|
110 | if s.count('"') % 2: | |
111 | return '"' |
|
111 | return '"' | |
112 | elif s.count("'") % 2: |
|
112 | elif s.count("'") % 2: | |
113 | return "'" |
|
113 | return "'" | |
114 | else: |
|
114 | else: | |
115 | return False |
|
115 | return False | |
116 |
|
116 | |||
117 |
|
117 | |||
118 | def protect_filename(s): |
|
118 | def protect_filename(s): | |
119 | """Escape a string to protect certain characters.""" |
|
119 | """Escape a string to protect certain characters.""" | |
120 |
|
120 | |||
121 | return "".join([(ch in PROTECTABLES and '\\' + ch or ch) |
|
121 | return "".join([(ch in PROTECTABLES and '\\' + ch or ch) | |
122 | for ch in s]) |
|
122 | for ch in s]) | |
123 |
|
123 | |||
124 | def expand_user(path): |
|
124 | def expand_user(path): | |
125 | """Expand '~'-style usernames in strings. |
|
125 | """Expand '~'-style usernames in strings. | |
126 |
|
126 | |||
127 | This is similar to :func:`os.path.expanduser`, but it computes and returns |
|
127 | This is similar to :func:`os.path.expanduser`, but it computes and returns | |
128 | extra information that will be useful if the input was being used in |
|
128 | extra information that will be useful if the input was being used in | |
129 | computing completions, and you wish to return the completions with the |
|
129 | computing completions, and you wish to return the completions with the | |
130 | original '~' instead of its expanded value. |
|
130 | original '~' instead of its expanded value. | |
131 |
|
131 | |||
132 | Parameters |
|
132 | Parameters | |
133 | ---------- |
|
133 | ---------- | |
134 | path : str |
|
134 | path : str | |
135 | String to be expanded. If no ~ is present, the output is the same as the |
|
135 | String to be expanded. If no ~ is present, the output is the same as the | |
136 | input. |
|
136 | input. | |
137 |
|
137 | |||
138 | Returns |
|
138 | Returns | |
139 | ------- |
|
139 | ------- | |
140 | newpath : str |
|
140 | newpath : str | |
141 | Result of ~ expansion in the input path. |
|
141 | Result of ~ expansion in the input path. | |
142 | tilde_expand : bool |
|
142 | tilde_expand : bool | |
143 | Whether any expansion was performed or not. |
|
143 | Whether any expansion was performed or not. | |
144 | tilde_val : str |
|
144 | tilde_val : str | |
145 | The value that ~ was replaced with. |
|
145 | The value that ~ was replaced with. | |
146 | """ |
|
146 | """ | |
147 | # Default values |
|
147 | # Default values | |
148 | tilde_expand = False |
|
148 | tilde_expand = False | |
149 | tilde_val = '' |
|
149 | tilde_val = '' | |
150 | newpath = path |
|
150 | newpath = path | |
151 |
|
151 | |||
152 | if path.startswith('~'): |
|
152 | if path.startswith('~'): | |
153 | tilde_expand = True |
|
153 | tilde_expand = True | |
154 | rest = len(path)-1 |
|
154 | rest = len(path)-1 | |
155 | newpath = os.path.expanduser(path) |
|
155 | newpath = os.path.expanduser(path) | |
156 | if rest: |
|
156 | if rest: | |
157 | tilde_val = newpath[:-rest] |
|
157 | tilde_val = newpath[:-rest] | |
158 | else: |
|
158 | else: | |
159 | tilde_val = newpath |
|
159 | tilde_val = newpath | |
160 |
|
160 | |||
161 | return newpath, tilde_expand, tilde_val |
|
161 | return newpath, tilde_expand, tilde_val | |
162 |
|
162 | |||
163 |
|
163 | |||
164 | def compress_user(path, tilde_expand, tilde_val): |
|
164 | def compress_user(path, tilde_expand, tilde_val): | |
165 | """Does the opposite of expand_user, with its outputs. |
|
165 | """Does the opposite of expand_user, with its outputs. | |
166 | """ |
|
166 | """ | |
167 | if tilde_expand: |
|
167 | if tilde_expand: | |
168 | return path.replace(tilde_val, '~') |
|
168 | return path.replace(tilde_val, '~') | |
169 | else: |
|
169 | else: | |
170 | return path |
|
170 | return path | |
171 |
|
171 | |||
172 |
|
172 | |||
173 | def completions_sorting_key(word): |
|
173 | def completions_sorting_key(word): | |
174 | """key for sorting completions |
|
174 | """key for sorting completions | |
175 |
|
175 | |||
176 | This does several things: |
|
176 | This does several things: | |
177 |
|
177 | |||
178 | - Lowercase all completions, so they are sorted alphabetically with |
|
178 | - Lowercase all completions, so they are sorted alphabetically with | |
179 | upper and lower case words mingled |
|
179 | upper and lower case words mingled | |
180 | - Demote any completions starting with underscores to the end |
|
180 | - Demote any completions starting with underscores to the end | |
181 | - Insert any %magic and %%cellmagic completions in the alphabetical order |
|
181 | - Insert any %magic and %%cellmagic completions in the alphabetical order | |
182 | by their name |
|
182 | by their name | |
183 | """ |
|
183 | """ | |
184 | # Case insensitive sort |
|
184 | # Case insensitive sort | |
185 | word = word.lower() |
|
185 | word = word.lower() | |
186 |
|
186 | |||
187 | prio1, prio2 = 0, 0 |
|
187 | prio1, prio2 = 0, 0 | |
188 |
|
188 | |||
189 | if word.startswith('__'): |
|
189 | if word.startswith('__'): | |
190 | prio1 = 2 |
|
190 | prio1 = 2 | |
191 | elif word.startswith('_'): |
|
191 | elif word.startswith('_'): | |
192 | prio1 = 1 |
|
192 | prio1 = 1 | |
193 |
|
193 | |||
194 | if word.endswith('='): |
|
194 | if word.endswith('='): | |
195 | prio1 = -1 |
|
195 | prio1 = -1 | |
196 |
|
196 | |||
197 | if word.startswith('%%'): |
|
197 | if word.startswith('%%'): | |
198 | # If there's another % in there, this is something else, so leave it alone |
|
198 | # If there's another % in there, this is something else, so leave it alone | |
199 | if not "%" in word[2:]: |
|
199 | if not "%" in word[2:]: | |
200 | word = word[2:] |
|
200 | word = word[2:] | |
201 | prio2 = 2 |
|
201 | prio2 = 2 | |
202 | elif word.startswith('%'): |
|
202 | elif word.startswith('%'): | |
203 | if not "%" in word[1:]: |
|
203 | if not "%" in word[1:]: | |
204 | word = word[1:] |
|
204 | word = word[1:] | |
205 | prio2 = 1 |
|
205 | prio2 = 1 | |
206 |
|
206 | |||
207 | return prio1, word, prio2 |
|
207 | return prio1, word, prio2 | |
208 |
|
208 | |||
209 |
|
209 | |||
210 | @undoc |
|
210 | @undoc | |
211 | class Bunch(object): pass |
|
211 | class Bunch(object): pass | |
212 |
|
212 | |||
213 |
|
213 | |||
214 | DELIMS = ' \t\n`!@#$^&*()=+[{]}\\|;:\'",<>?' |
|
214 | DELIMS = ' \t\n`!@#$^&*()=+[{]}\\|;:\'",<>?' | |
215 | GREEDY_DELIMS = ' =\r\n' |
|
215 | GREEDY_DELIMS = ' =\r\n' | |
216 |
|
216 | |||
217 |
|
217 | |||
218 | class CompletionSplitter(object): |
|
218 | class CompletionSplitter(object): | |
219 | """An object to split an input line in a manner similar to readline. |
|
219 | """An object to split an input line in a manner similar to readline. | |
220 |
|
220 | |||
221 | By having our own implementation, we can expose readline-like completion in |
|
221 | By having our own implementation, we can expose readline-like completion in | |
222 | a uniform manner to all frontends. This object only needs to be given the |
|
222 | a uniform manner to all frontends. This object only needs to be given the | |
223 | line of text to be split and the cursor position on said line, and it |
|
223 | line of text to be split and the cursor position on said line, and it | |
224 | returns the 'word' to be completed on at the cursor after splitting the |
|
224 | returns the 'word' to be completed on at the cursor after splitting the | |
225 | entire line. |
|
225 | entire line. | |
226 |
|
226 | |||
227 | What characters are used as splitting delimiters can be controlled by |
|
227 | What characters are used as splitting delimiters can be controlled by | |
228 | setting the `delims` attribute (this is a property that internally |
|
228 | setting the `delims` attribute (this is a property that internally | |
229 | automatically builds the necessary regular expression)""" |
|
229 | automatically builds the necessary regular expression)""" | |
230 |
|
230 | |||
231 | # Private interface |
|
231 | # Private interface | |
232 |
|
232 | |||
233 | # A string of delimiter characters. The default value makes sense for |
|
233 | # A string of delimiter characters. The default value makes sense for | |
234 | # IPython's most typical usage patterns. |
|
234 | # IPython's most typical usage patterns. | |
235 | _delims = DELIMS |
|
235 | _delims = DELIMS | |
236 |
|
236 | |||
237 | # The expression (a normal string) to be compiled into a regular expression |
|
237 | # The expression (a normal string) to be compiled into a regular expression | |
238 | # for actual splitting. We store it as an attribute mostly for ease of |
|
238 | # for actual splitting. We store it as an attribute mostly for ease of | |
239 | # debugging, since this type of code can be so tricky to debug. |
|
239 | # debugging, since this type of code can be so tricky to debug. | |
240 | _delim_expr = None |
|
240 | _delim_expr = None | |
241 |
|
241 | |||
242 | # The regular expression that does the actual splitting |
|
242 | # The regular expression that does the actual splitting | |
243 | _delim_re = None |
|
243 | _delim_re = None | |
244 |
|
244 | |||
245 | def __init__(self, delims=None): |
|
245 | def __init__(self, delims=None): | |
246 | delims = CompletionSplitter._delims if delims is None else delims |
|
246 | delims = CompletionSplitter._delims if delims is None else delims | |
247 | self.delims = delims |
|
247 | self.delims = delims | |
248 |
|
248 | |||
249 | @property |
|
249 | @property | |
250 | def delims(self): |
|
250 | def delims(self): | |
251 | """Return the string of delimiter characters.""" |
|
251 | """Return the string of delimiter characters.""" | |
252 | return self._delims |
|
252 | return self._delims | |
253 |
|
253 | |||
254 | @delims.setter |
|
254 | @delims.setter | |
255 | def delims(self, delims): |
|
255 | def delims(self, delims): | |
256 | """Set the delimiters for line splitting.""" |
|
256 | """Set the delimiters for line splitting.""" | |
257 | expr = '[' + ''.join('\\'+ c for c in delims) + ']' |
|
257 | expr = '[' + ''.join('\\'+ c for c in delims) + ']' | |
258 | self._delim_re = re.compile(expr) |
|
258 | self._delim_re = re.compile(expr) | |
259 | self._delims = delims |
|
259 | self._delims = delims | |
260 | self._delim_expr = expr |
|
260 | self._delim_expr = expr | |
261 |
|
261 | |||
262 | def split_line(self, line, cursor_pos=None): |
|
262 | def split_line(self, line, cursor_pos=None): | |
263 | """Split a line of text with a cursor at the given position. |
|
263 | """Split a line of text with a cursor at the given position. | |
264 | """ |
|
264 | """ | |
265 | l = line if cursor_pos is None else line[:cursor_pos] |
|
265 | l = line if cursor_pos is None else line[:cursor_pos] | |
266 | return self._delim_re.split(l)[-1] |
|
266 | return self._delim_re.split(l)[-1] | |
267 |
|
267 | |||
268 |
|
268 | |||
269 | class Completer(Configurable): |
|
269 | class Completer(Configurable): | |
270 |
|
270 | |||
271 | greedy = Bool(False, |
|
271 | greedy = Bool(False, | |
272 | help="""Activate greedy completion |
|
272 | help="""Activate greedy completion | |
273 | PENDING DEPRECTION. this is now mostly taken care of with Jedi. |
|
273 | PENDING DEPRECTION. this is now mostly taken care of with Jedi. | |
274 |
|
274 | |||
275 | This will enable completion on elements of lists, results of function calls, etc., |
|
275 | This will enable completion on elements of lists, results of function calls, etc., | |
276 | but can be unsafe because the code is actually evaluated on TAB. |
|
276 | but can be unsafe because the code is actually evaluated on TAB. | |
277 | """ |
|
277 | """ | |
278 | ).tag(config=True) |
|
278 | ).tag(config=True) | |
279 |
|
279 | |||
280 |
|
280 | |||
281 | def __init__(self, namespace=None, global_namespace=None, **kwargs): |
|
281 | def __init__(self, namespace=None, global_namespace=None, **kwargs): | |
282 | """Create a new completer for the command line. |
|
282 | """Create a new completer for the command line. | |
283 |
|
283 | |||
284 | Completer(namespace=ns, global_namespace=ns2) -> completer instance. |
|
284 | Completer(namespace=ns, global_namespace=ns2) -> completer instance. | |
285 |
|
285 | |||
286 | If unspecified, the default namespace where completions are performed |
|
286 | If unspecified, the default namespace where completions are performed | |
287 | is __main__ (technically, __main__.__dict__). Namespaces should be |
|
287 | is __main__ (technically, __main__.__dict__). Namespaces should be | |
288 | given as dictionaries. |
|
288 | given as dictionaries. | |
289 |
|
289 | |||
290 | An optional second namespace can be given. This allows the completer |
|
290 | An optional second namespace can be given. This allows the completer | |
291 | to handle cases where both the local and global scopes need to be |
|
291 | to handle cases where both the local and global scopes need to be | |
292 | distinguished. |
|
292 | distinguished. | |
293 |
|
293 | |||
294 | Completer instances should be used as the completion mechanism of |
|
294 | Completer instances should be used as the completion mechanism of | |
295 | readline via the set_completer() call: |
|
295 | readline via the set_completer() call: | |
296 |
|
296 | |||
297 | readline.set_completer(Completer(my_namespace).complete) |
|
297 | readline.set_completer(Completer(my_namespace).complete) | |
298 | """ |
|
298 | """ | |
299 |
|
299 | |||
300 | # Don't bind to namespace quite yet, but flag whether the user wants a |
|
300 | # Don't bind to namespace quite yet, but flag whether the user wants a | |
301 | # specific namespace or to use __main__.__dict__. This will allow us |
|
301 | # specific namespace or to use __main__.__dict__. This will allow us | |
302 | # to bind to __main__.__dict__ at completion time, not now. |
|
302 | # to bind to __main__.__dict__ at completion time, not now. | |
303 | if namespace is None: |
|
303 | if namespace is None: | |
304 | self.use_main_ns = 1 |
|
304 | self.use_main_ns = 1 | |
305 | else: |
|
305 | else: | |
306 | self.use_main_ns = 0 |
|
306 | self.use_main_ns = 0 | |
307 | self.namespace = namespace |
|
307 | self.namespace = namespace | |
308 |
|
308 | |||
309 | # The global namespace, if given, can be bound directly |
|
309 | # The global namespace, if given, can be bound directly | |
310 | if global_namespace is None: |
|
310 | if global_namespace is None: | |
311 | self.global_namespace = {} |
|
311 | self.global_namespace = {} | |
312 | else: |
|
312 | else: | |
313 | self.global_namespace = global_namespace |
|
313 | self.global_namespace = global_namespace | |
314 |
|
314 | |||
315 | super(Completer, self).__init__(**kwargs) |
|
315 | super(Completer, self).__init__(**kwargs) | |
316 |
|
316 | |||
317 | def complete(self, text, state): |
|
317 | def complete(self, text, state): | |
318 | """Return the next possible completion for 'text'. |
|
318 | """Return the next possible completion for 'text'. | |
319 |
|
319 | |||
320 | This is called successively with state == 0, 1, 2, ... until it |
|
320 | This is called successively with state == 0, 1, 2, ... until it | |
321 | returns None. The completion should begin with 'text'. |
|
321 | returns None. The completion should begin with 'text'. | |
322 |
|
322 | |||
323 | """ |
|
323 | """ | |
324 | if self.use_main_ns: |
|
324 | if self.use_main_ns: | |
325 | self.namespace = __main__.__dict__ |
|
325 | self.namespace = __main__.__dict__ | |
326 |
|
326 | |||
327 | if state == 0: |
|
327 | if state == 0: | |
328 | if "." in text: |
|
328 | if "." in text: | |
329 | self.matches = self.attr_matches(text) |
|
329 | self.matches = self.attr_matches(text) | |
330 | else: |
|
330 | else: | |
331 | self.matches = self.global_matches(text) |
|
331 | self.matches = self.global_matches(text) | |
332 | try: |
|
332 | try: | |
333 | return self.matches[state] |
|
333 | return self.matches[state] | |
334 | except IndexError: |
|
334 | except IndexError: | |
335 | return None |
|
335 | return None | |
336 |
|
336 | |||
337 | def global_matches(self, text): |
|
337 | def global_matches(self, text): | |
338 | """Compute matches when text is a simple name. |
|
338 | """Compute matches when text is a simple name. | |
339 |
|
339 | |||
340 | Return a list of all keywords, built-in functions and names currently |
|
340 | Return a list of all keywords, built-in functions and names currently | |
341 | defined in self.namespace or self.global_namespace that match. |
|
341 | defined in self.namespace or self.global_namespace that match. | |
342 |
|
342 | |||
343 | """ |
|
343 | """ | |
344 | matches = [] |
|
344 | matches = [] | |
345 | match_append = matches.append |
|
345 | match_append = matches.append | |
346 | n = len(text) |
|
346 | n = len(text) | |
347 | for lst in [keyword.kwlist, |
|
347 | for lst in [keyword.kwlist, | |
348 | builtin_mod.__dict__.keys(), |
|
348 | builtin_mod.__dict__.keys(), | |
349 | self.namespace.keys(), |
|
349 | self.namespace.keys(), | |
350 | self.global_namespace.keys()]: |
|
350 | self.global_namespace.keys()]: | |
351 | for word in lst: |
|
351 | for word in lst: | |
352 | if word[:n] == text and word != "__builtins__": |
|
352 | if word[:n] == text and word != "__builtins__": | |
353 | match_append(word) |
|
353 | match_append(word) | |
354 | return [cast_unicode_py2(m) for m in matches] |
|
354 | return [cast_unicode_py2(m) for m in matches] | |
355 |
|
355 | |||
356 | def attr_matches(self, text): |
|
356 | def attr_matches(self, text): | |
357 | """Compute matches when text contains a dot. |
|
357 | """Compute matches when text contains a dot. | |
358 |
|
358 | |||
359 | Assuming the text is of the form NAME.NAME....[NAME], and is |
|
359 | Assuming the text is of the form NAME.NAME....[NAME], and is | |
360 | evaluatable in self.namespace or self.global_namespace, it will be |
|
360 | evaluatable in self.namespace or self.global_namespace, it will be | |
361 | evaluated and its attributes (as revealed by dir()) are used as |
|
361 | evaluated and its attributes (as revealed by dir()) are used as | |
362 | possible completions. (For class instances, class members are are |
|
362 | possible completions. (For class instances, class members are are | |
363 | also considered.) |
|
363 | also considered.) | |
364 |
|
364 | |||
365 | WARNING: this can still invoke arbitrary C code, if an object |
|
365 | WARNING: this can still invoke arbitrary C code, if an object | |
366 | with a __getattr__ hook is evaluated. |
|
366 | with a __getattr__ hook is evaluated. | |
367 |
|
367 | |||
368 | """ |
|
368 | """ | |
369 |
|
369 | |||
370 | # Another option, seems to work great. Catches things like ''.<tab> |
|
370 | # Another option, seems to work great. Catches things like ''.<tab> | |
371 | m = re.match(r"(\S+(\.\w+)*)\.(\w*)$", text) |
|
371 | m = re.match(r"(\S+(\.\w+)*)\.(\w*)$", text) | |
372 |
|
372 | |||
373 | if m: |
|
373 | if m: | |
374 | expr, attr = m.group(1, 3) |
|
374 | expr, attr = m.group(1, 3) | |
375 | elif self.greedy: |
|
375 | elif self.greedy: | |
376 | m2 = re.match(r"(.+)\.(\w*)$", self.line_buffer) |
|
376 | m2 = re.match(r"(.+)\.(\w*)$", self.line_buffer) | |
377 | if not m2: |
|
377 | if not m2: | |
378 | return [] |
|
378 | return [] | |
379 | expr, attr = m2.group(1,2) |
|
379 | expr, attr = m2.group(1,2) | |
380 | else: |
|
380 | else: | |
381 | return [] |
|
381 | return [] | |
382 |
|
382 | |||
383 | try: |
|
383 | try: | |
384 | obj = eval(expr, self.namespace) |
|
384 | obj = eval(expr, self.namespace) | |
385 | except: |
|
385 | except: | |
386 | try: |
|
386 | try: | |
387 | obj = eval(expr, self.global_namespace) |
|
387 | obj = eval(expr, self.global_namespace) | |
388 | except: |
|
388 | except: | |
389 | return [] |
|
389 | return [] | |
390 |
|
390 | |||
391 | if self.limit_to__all__ and hasattr(obj, '__all__'): |
|
391 | if self.limit_to__all__ and hasattr(obj, '__all__'): | |
392 | words = get__all__entries(obj) |
|
392 | words = get__all__entries(obj) | |
393 | else: |
|
393 | else: | |
394 | words = dir2(obj) |
|
394 | words = dir2(obj) | |
395 |
|
395 | |||
396 | try: |
|
396 | try: | |
397 | words = generics.complete_object(obj, words) |
|
397 | words = generics.complete_object(obj, words) | |
398 | except TryNext: |
|
398 | except TryNext: | |
399 | pass |
|
399 | pass | |
400 | except Exception: |
|
400 | except Exception: | |
401 | # Silence errors from completion function |
|
401 | # Silence errors from completion function | |
402 | #raise # dbg |
|
402 | #raise # dbg | |
403 | pass |
|
403 | pass | |
404 | # Build match list to return |
|
404 | # Build match list to return | |
405 | n = len(attr) |
|
405 | n = len(attr) | |
406 | return [u"%s.%s" % (expr, w) for w in words if w[:n] == attr ] |
|
406 | return [u"%s.%s" % (expr, w) for w in words if w[:n] == attr ] | |
407 |
|
407 | |||
408 |
|
408 | |||
409 | def get__all__entries(obj): |
|
409 | def get__all__entries(obj): | |
410 | """returns the strings in the __all__ attribute""" |
|
410 | """returns the strings in the __all__ attribute""" | |
411 | try: |
|
411 | try: | |
412 | words = getattr(obj, '__all__') |
|
412 | words = getattr(obj, '__all__') | |
413 | except: |
|
413 | except: | |
414 | return [] |
|
414 | return [] | |
415 |
|
415 | |||
416 | return [cast_unicode_py2(w) for w in words if isinstance(w, string_types)] |
|
416 | return [cast_unicode_py2(w) for w in words if isinstance(w, string_types)] | |
417 |
|
417 | |||
418 |
|
418 | |||
419 | def match_dict_keys(keys, prefix, delims): |
|
419 | def match_dict_keys(keys, prefix, delims): | |
420 | """Used by dict_key_matches, matching the prefix to a list of keys""" |
|
420 | """Used by dict_key_matches, matching the prefix to a list of keys""" | |
421 | if not prefix: |
|
421 | if not prefix: | |
422 | return None, 0, [repr(k) for k in keys |
|
422 | return None, 0, [repr(k) for k in keys | |
423 | if isinstance(k, (string_types, bytes))] |
|
423 | if isinstance(k, (string_types, bytes))] | |
424 | quote_match = re.search('["\']', prefix) |
|
424 | quote_match = re.search('["\']', prefix) | |
425 | quote = quote_match.group() |
|
425 | quote = quote_match.group() | |
426 | try: |
|
426 | try: | |
427 | prefix_str = eval(prefix + quote, {}) |
|
427 | prefix_str = eval(prefix + quote, {}) | |
428 | except Exception: |
|
428 | except Exception: | |
429 | return None, 0, [] |
|
429 | return None, 0, [] | |
430 |
|
430 | |||
431 | pattern = '[^' + ''.join('\\' + c for c in delims) + ']*$' |
|
431 | pattern = '[^' + ''.join('\\' + c for c in delims) + ']*$' | |
432 | token_match = re.search(pattern, prefix, re.UNICODE) |
|
432 | token_match = re.search(pattern, prefix, re.UNICODE) | |
433 | token_start = token_match.start() |
|
433 | token_start = token_match.start() | |
434 | token_prefix = token_match.group() |
|
434 | token_prefix = token_match.group() | |
435 |
|
435 | |||
436 | # TODO: support bytes in Py3k |
|
436 | # TODO: support bytes in Py3k | |
437 | matched = [] |
|
437 | matched = [] | |
438 | for key in keys: |
|
438 | for key in keys: | |
439 | try: |
|
439 | try: | |
440 | if not key.startswith(prefix_str): |
|
440 | if not key.startswith(prefix_str): | |
441 | continue |
|
441 | continue | |
442 | except (AttributeError, TypeError, UnicodeError): |
|
442 | except (AttributeError, TypeError, UnicodeError): | |
443 | # Python 3+ TypeError on b'a'.startswith('a') or vice-versa |
|
443 | # Python 3+ TypeError on b'a'.startswith('a') or vice-versa | |
444 | continue |
|
444 | continue | |
445 |
|
445 | |||
446 | # reformat remainder of key to begin with prefix |
|
446 | # reformat remainder of key to begin with prefix | |
447 | rem = key[len(prefix_str):] |
|
447 | rem = key[len(prefix_str):] | |
448 | # force repr wrapped in ' |
|
448 | # force repr wrapped in ' | |
449 | rem_repr = repr(rem + '"') |
|
449 | rem_repr = repr(rem + '"') | |
450 | if rem_repr.startswith('u') and prefix[0] not in 'uU': |
|
450 | if rem_repr.startswith('u') and prefix[0] not in 'uU': | |
451 | # Found key is unicode, but prefix is Py2 string. |
|
451 | # Found key is unicode, but prefix is Py2 string. | |
452 | # Therefore attempt to interpret key as string. |
|
452 | # Therefore attempt to interpret key as string. | |
453 | try: |
|
453 | try: | |
454 | rem_repr = repr(rem.encode('ascii') + '"') |
|
454 | rem_repr = repr(rem.encode('ascii') + '"') | |
455 | except UnicodeEncodeError: |
|
455 | except UnicodeEncodeError: | |
456 | continue |
|
456 | continue | |
457 |
|
457 | |||
458 | rem_repr = rem_repr[1 + rem_repr.index("'"):-2] |
|
458 | rem_repr = rem_repr[1 + rem_repr.index("'"):-2] | |
459 | if quote == '"': |
|
459 | if quote == '"': | |
460 | # The entered prefix is quoted with ", |
|
460 | # The entered prefix is quoted with ", | |
461 | # but the match is quoted with '. |
|
461 | # but the match is quoted with '. | |
462 | # A contained " hence needs escaping for comparison: |
|
462 | # A contained " hence needs escaping for comparison: | |
463 | rem_repr = rem_repr.replace('"', '\\"') |
|
463 | rem_repr = rem_repr.replace('"', '\\"') | |
464 |
|
464 | |||
465 | # then reinsert prefix from start of token |
|
465 | # then reinsert prefix from start of token | |
466 | matched.append('%s%s' % (token_prefix, rem_repr)) |
|
466 | matched.append('%s%s' % (token_prefix, rem_repr)) | |
467 | return quote, token_start, matched |
|
467 | return quote, token_start, matched | |
468 |
|
468 | |||
469 |
|
469 | |||
470 | def _safe_isinstance(obj, module, class_name): |
|
470 | def _safe_isinstance(obj, module, class_name): | |
471 | """Checks if obj is an instance of module.class_name if loaded |
|
471 | """Checks if obj is an instance of module.class_name if loaded | |
472 | """ |
|
472 | """ | |
473 | return (module in sys.modules and |
|
473 | return (module in sys.modules and | |
474 | isinstance(obj, getattr(__import__(module), class_name))) |
|
474 | isinstance(obj, getattr(__import__(module), class_name))) | |
475 |
|
475 | |||
476 |
|
476 | |||
477 | def back_unicode_name_matches(text): |
|
477 | def back_unicode_name_matches(text): | |
478 | u"""Match unicode characters back to unicode name |
|
478 | u"""Match unicode characters back to unicode name | |
479 |
|
479 | |||
480 | This does β -> \\snowman |
|
480 | This does β -> \\snowman | |
481 |
|
481 | |||
482 | Note that snowman is not a valid python3 combining character but will be expanded. |
|
482 | Note that snowman is not a valid python3 combining character but will be expanded. | |
483 | Though it will not recombine back to the snowman character by the completion machinery. |
|
483 | Though it will not recombine back to the snowman character by the completion machinery. | |
484 |
|
484 | |||
485 | This will not either back-complete standard sequences like \\n, \\b ... |
|
485 | This will not either back-complete standard sequences like \\n, \\b ... | |
486 |
|
486 | |||
487 | Used on Python 3 only. |
|
487 | Used on Python 3 only. | |
488 | """ |
|
488 | """ | |
489 | if len(text)<2: |
|
489 | if len(text)<2: | |
490 | return u'', () |
|
490 | return u'', () | |
491 | maybe_slash = text[-2] |
|
491 | maybe_slash = text[-2] | |
492 | if maybe_slash != '\\': |
|
492 | if maybe_slash != '\\': | |
493 | return u'', () |
|
493 | return u'', () | |
494 |
|
494 | |||
495 | char = text[-1] |
|
495 | char = text[-1] | |
496 | # no expand on quote for completion in strings. |
|
496 | # no expand on quote for completion in strings. | |
497 | # nor backcomplete standard ascii keys |
|
497 | # nor backcomplete standard ascii keys | |
498 | if char in string.ascii_letters or char in ['"',"'"]: |
|
498 | if char in string.ascii_letters or char in ['"',"'"]: | |
499 | return u'', () |
|
499 | return u'', () | |
500 | try : |
|
500 | try : | |
501 | unic = unicodedata.name(char) |
|
501 | unic = unicodedata.name(char) | |
502 | return '\\'+char,['\\'+unic] |
|
502 | return '\\'+char,['\\'+unic] | |
503 | except KeyError: |
|
503 | except KeyError: | |
504 | pass |
|
504 | pass | |
505 | return u'', () |
|
505 | return u'', () | |
506 |
|
506 | |||
507 | def back_latex_name_matches(text): |
|
507 | def back_latex_name_matches(text): | |
508 | u"""Match latex characters back to unicode name |
|
508 | u"""Match latex characters back to unicode name | |
509 |
|
509 | |||
510 | This does ->\\sqrt |
|
510 | This does ->\\sqrt | |
511 |
|
511 | |||
512 | Used on Python 3 only. |
|
512 | Used on Python 3 only. | |
513 | """ |
|
513 | """ | |
514 | if len(text)<2: |
|
514 | if len(text)<2: | |
515 | return u'', () |
|
515 | return u'', () | |
516 | maybe_slash = text[-2] |
|
516 | maybe_slash = text[-2] | |
517 | if maybe_slash != '\\': |
|
517 | if maybe_slash != '\\': | |
518 | return u'', () |
|
518 | return u'', () | |
519 |
|
519 | |||
520 |
|
520 | |||
521 | char = text[-1] |
|
521 | char = text[-1] | |
522 | # no expand on quote for completion in strings. |
|
522 | # no expand on quote for completion in strings. | |
523 | # nor backcomplete standard ascii keys |
|
523 | # nor backcomplete standard ascii keys | |
524 | if char in string.ascii_letters or char in ['"',"'"]: |
|
524 | if char in string.ascii_letters or char in ['"',"'"]: | |
525 | return u'', () |
|
525 | return u'', () | |
526 | try : |
|
526 | try : | |
527 | latex = reverse_latex_symbol[char] |
|
527 | latex = reverse_latex_symbol[char] | |
528 | # '\\' replace the \ as well |
|
528 | # '\\' replace the \ as well | |
529 | return '\\'+char,[latex] |
|
529 | return '\\'+char,[latex] | |
530 | except KeyError: |
|
530 | except KeyError: | |
531 | pass |
|
531 | pass | |
532 | return u'', () |
|
532 | return u'', () | |
533 |
|
533 | |||
534 |
|
534 | |||
535 | class IPCompleter(Completer): |
|
535 | class IPCompleter(Completer): | |
536 | """Extension of the completer class with IPython-specific features""" |
|
536 | """Extension of the completer class with IPython-specific features""" | |
537 |
|
537 | |||
538 | @observe('greedy') |
|
538 | @observe('greedy') | |
539 | def _greedy_changed(self, change): |
|
539 | def _greedy_changed(self, change): | |
540 | """update the splitter and readline delims when greedy is changed""" |
|
540 | """update the splitter and readline delims when greedy is changed""" | |
541 | if change['new']: |
|
541 | if change['new']: | |
542 | self.splitter.delims = GREEDY_DELIMS |
|
542 | self.splitter.delims = GREEDY_DELIMS | |
543 | else: |
|
543 | else: | |
544 | self.splitter.delims = DELIMS |
|
544 | self.splitter.delims = DELIMS | |
545 |
|
545 | |||
546 | if self.readline: |
|
546 | if self.readline: | |
547 | self.readline.set_completer_delims(self.splitter.delims) |
|
547 | self.readline.set_completer_delims(self.splitter.delims) | |
548 |
|
548 | |||
549 | merge_completions = Bool(True, |
|
549 | merge_completions = Bool(True, | |
550 | help="""Whether to merge completion results into a single list |
|
550 | help="""Whether to merge completion results into a single list | |
551 |
|
551 | |||
552 | If False, only the completion results from the first non-empty |
|
552 | If False, only the completion results from the first non-empty | |
553 | completer will be returned. |
|
553 | completer will be returned. | |
554 | """ |
|
554 | """ | |
555 | ).tag(config=True) |
|
555 | ).tag(config=True) | |
556 | omit__names = Enum((0,1,2), default_value=2, |
|
556 | omit__names = Enum((0,1,2), default_value=2, | |
557 | help="""Instruct the completer to omit private method names |
|
557 | help="""Instruct the completer to omit private method names | |
558 |
|
558 | |||
559 | Specifically, when completing on ``object.<tab>``. |
|
559 | Specifically, when completing on ``object.<tab>``. | |
560 |
|
560 | |||
561 | When 2 [default]: all names that start with '_' will be excluded. |
|
561 | When 2 [default]: all names that start with '_' will be excluded. | |
562 |
|
562 | |||
563 | When 1: all 'magic' names (``__foo__``) will be excluded. |
|
563 | When 1: all 'magic' names (``__foo__``) will be excluded. | |
564 |
|
564 | |||
565 | When 0: nothing will be excluded. |
|
565 | When 0: nothing will be excluded. | |
566 | """ |
|
566 | """ | |
567 | ).tag(config=True) |
|
567 | ).tag(config=True) | |
568 | limit_to__all__ = Bool(False, |
|
568 | limit_to__all__ = Bool(False, | |
569 | help=""" |
|
569 | help=""" | |
570 | DEPRECATED as of version 5.0. |
|
570 | DEPRECATED as of version 5.0. | |
571 |
|
571 | |||
572 | Instruct the completer to use __all__ for the completion |
|
572 | Instruct the completer to use __all__ for the completion | |
573 |
|
573 | |||
574 | Specifically, when completing on ``object.<tab>``. |
|
574 | Specifically, when completing on ``object.<tab>``. | |
575 |
|
575 | |||
576 | When True: only those names in obj.__all__ will be included. |
|
576 | When True: only those names in obj.__all__ will be included. | |
577 |
|
577 | |||
578 | When False [default]: the __all__ attribute is ignored |
|
578 | When False [default]: the __all__ attribute is ignored | |
579 | """, |
|
579 | """, | |
580 | ).tag(config=True) |
|
580 | ).tag(config=True) | |
581 |
|
581 | |||
582 | def __init__(self, shell=None, namespace=None, global_namespace=None, |
|
582 | def __init__(self, shell=None, namespace=None, global_namespace=None, | |
583 | use_readline=True, config=None, **kwargs): |
|
583 | use_readline=True, config=None, **kwargs): | |
584 | """IPCompleter() -> completer |
|
584 | """IPCompleter() -> completer | |
585 |
|
585 | |||
586 | Return a completer object suitable for use by the readline library |
|
586 | Return a completer object suitable for use by the readline library | |
587 | via readline.set_completer(). |
|
587 | via readline.set_completer(). | |
588 |
|
588 | |||
589 | Inputs: |
|
589 | Inputs: | |
590 |
|
590 | |||
591 | - shell: a pointer to the ipython shell itself. This is needed |
|
591 | - shell: a pointer to the ipython shell itself. This is needed | |
592 | because this completer knows about magic functions, and those can |
|
592 | because this completer knows about magic functions, and those can | |
593 | only be accessed via the ipython instance. |
|
593 | only be accessed via the ipython instance. | |
594 |
|
594 | |||
595 | - namespace: an optional dict where completions are performed. |
|
595 | - namespace: an optional dict where completions are performed. | |
596 |
|
596 | |||
597 | - global_namespace: secondary optional dict for completions, to |
|
597 | - global_namespace: secondary optional dict for completions, to | |
598 | handle cases (such as IPython embedded inside functions) where |
|
598 | handle cases (such as IPython embedded inside functions) where | |
599 | both Python scopes are visible. |
|
599 | both Python scopes are visible. | |
600 |
|
600 | |||
601 | use_readline : bool, optional |
|
601 | use_readline : bool, optional | |
602 | If true, use the readline library. This completer can still function |
|
602 | If true, use the readline library. This completer can still function | |
603 | without readline, though in that case callers must provide some extra |
|
603 | without readline, though in that case callers must provide some extra | |
604 | information on each call about the current line.""" |
|
604 | information on each call about the current line.""" | |
605 |
|
605 | |||
606 | self.magic_escape = ESC_MAGIC |
|
606 | self.magic_escape = ESC_MAGIC | |
607 | self.splitter = CompletionSplitter() |
|
607 | self.splitter = CompletionSplitter() | |
608 |
|
608 | |||
609 | # Readline configuration, only used by the rlcompleter method. |
|
609 | # Readline configuration, only used by the rlcompleter method. | |
610 | if use_readline: |
|
610 | if use_readline: | |
611 | # We store the right version of readline so that later code |
|
611 | # We store the right version of readline so that later code | |
612 | import IPython.utils.rlineimpl as readline |
|
612 | import IPython.utils.rlineimpl as readline | |
613 | self.readline = readline |
|
613 | self.readline = readline | |
614 | else: |
|
614 | else: | |
615 | self.readline = None |
|
615 | self.readline = None | |
616 |
|
616 | |||
617 | # _greedy_changed() depends on splitter and readline being defined: |
|
617 | # _greedy_changed() depends on splitter and readline being defined: | |
618 | Completer.__init__(self, namespace=namespace, global_namespace=global_namespace, |
|
618 | Completer.__init__(self, namespace=namespace, global_namespace=global_namespace, | |
619 | config=config, **kwargs) |
|
619 | config=config, **kwargs) | |
620 |
|
620 | |||
621 | # List where completion matches will be stored |
|
621 | # List where completion matches will be stored | |
622 | self.matches = [] |
|
622 | self.matches = [] | |
623 | self.shell = shell |
|
623 | self.shell = shell | |
624 | # Regexp to split filenames with spaces in them |
|
624 | # Regexp to split filenames with spaces in them | |
625 | self.space_name_re = re.compile(r'([^\\] )') |
|
625 | self.space_name_re = re.compile(r'([^\\] )') | |
626 | # Hold a local ref. to glob.glob for speed |
|
626 | # Hold a local ref. to glob.glob for speed | |
627 | self.glob = glob.glob |
|
627 | self.glob = glob.glob | |
628 |
|
628 | |||
629 | # Determine if we are running on 'dumb' terminals, like (X)Emacs |
|
629 | # Determine if we are running on 'dumb' terminals, like (X)Emacs | |
630 | # buffers, to avoid completion problems. |
|
630 | # buffers, to avoid completion problems. | |
631 | term = os.environ.get('TERM','xterm') |
|
631 | term = os.environ.get('TERM','xterm') | |
632 | self.dumb_terminal = term in ['dumb','emacs'] |
|
632 | self.dumb_terminal = term in ['dumb','emacs'] | |
633 |
|
633 | |||
634 | # Special handling of backslashes needed in win32 platforms |
|
634 | # Special handling of backslashes needed in win32 platforms | |
635 | if sys.platform == "win32": |
|
635 | if sys.platform == "win32": | |
636 | self.clean_glob = self._clean_glob_win32 |
|
636 | self.clean_glob = self._clean_glob_win32 | |
637 | else: |
|
637 | else: | |
638 | self.clean_glob = self._clean_glob |
|
638 | self.clean_glob = self._clean_glob | |
639 |
|
639 | |||
640 | #regexp to parse docstring for function signature |
|
640 | #regexp to parse docstring for function signature | |
641 | self.docstring_sig_re = re.compile(r'^[\w|\s.]+\(([^)]*)\).*') |
|
641 | self.docstring_sig_re = re.compile(r'^[\w|\s.]+\(([^)]*)\).*') | |
642 | self.docstring_kwd_re = re.compile(r'[\s|\[]*(\w+)(?:\s*=\s*.*)') |
|
642 | self.docstring_kwd_re = re.compile(r'[\s|\[]*(\w+)(?:\s*=\s*.*)') | |
643 | #use this if positional argument name is also needed |
|
643 | #use this if positional argument name is also needed | |
644 | #= re.compile(r'[\s|\[]*(\w+)(?:\s*=?\s*.*)') |
|
644 | #= re.compile(r'[\s|\[]*(\w+)(?:\s*=?\s*.*)') | |
645 |
|
645 | |||
646 | # All active matcher routines for completion |
|
646 | # All active matcher routines for completion | |
647 | self.matchers = [ |
|
647 | self.matchers = [ | |
648 | self.python_matches, |
|
648 | self.python_matches, | |
649 | self.file_matches, |
|
649 | self.file_matches, | |
650 | self.magic_matches, |
|
650 | self.magic_matches, | |
651 | self.python_func_kw_matches, |
|
651 | self.python_func_kw_matches, | |
652 | self.dict_key_matches, |
|
652 | self.dict_key_matches, | |
653 | ] |
|
653 | ] | |
654 |
|
654 | |||
|
655 | # This is set externally by InteractiveShell | |||
|
656 | self.custom_completers = None | |||
|
657 | ||||
655 | def all_completions(self, text): |
|
658 | def all_completions(self, text): | |
656 | """ |
|
659 | """ | |
657 | Wrapper around the complete method for the benefit of emacs. |
|
660 | Wrapper around the complete method for the benefit of emacs. | |
658 | """ |
|
661 | """ | |
659 | return self.complete(text)[1] |
|
662 | return self.complete(text)[1] | |
660 |
|
663 | |||
661 | def _clean_glob(self, text): |
|
664 | def _clean_glob(self, text): | |
662 | return self.glob("%s*" % text) |
|
665 | return self.glob("%s*" % text) | |
663 |
|
666 | |||
664 | def _clean_glob_win32(self,text): |
|
667 | def _clean_glob_win32(self,text): | |
665 | return [f.replace("\\","/") |
|
668 | return [f.replace("\\","/") | |
666 | for f in self.glob("%s*" % text)] |
|
669 | for f in self.glob("%s*" % text)] | |
667 |
|
670 | |||
668 | def file_matches(self, text): |
|
671 | def file_matches(self, text): | |
669 | """Match filenames, expanding ~USER type strings. |
|
672 | """Match filenames, expanding ~USER type strings. | |
670 |
|
673 | |||
671 | Most of the seemingly convoluted logic in this completer is an |
|
674 | Most of the seemingly convoluted logic in this completer is an | |
672 | attempt to handle filenames with spaces in them. And yet it's not |
|
675 | attempt to handle filenames with spaces in them. And yet it's not | |
673 | quite perfect, because Python's readline doesn't expose all of the |
|
676 | quite perfect, because Python's readline doesn't expose all of the | |
674 | GNU readline details needed for this to be done correctly. |
|
677 | GNU readline details needed for this to be done correctly. | |
675 |
|
678 | |||
676 | For a filename with a space in it, the printed completions will be |
|
679 | For a filename with a space in it, the printed completions will be | |
677 | only the parts after what's already been typed (instead of the |
|
680 | only the parts after what's already been typed (instead of the | |
678 | full completions, as is normally done). I don't think with the |
|
681 | full completions, as is normally done). I don't think with the | |
679 | current (as of Python 2.3) Python readline it's possible to do |
|
682 | current (as of Python 2.3) Python readline it's possible to do | |
680 | better.""" |
|
683 | better.""" | |
681 |
|
684 | |||
682 | # chars that require escaping with backslash - i.e. chars |
|
685 | # chars that require escaping with backslash - i.e. chars | |
683 | # that readline treats incorrectly as delimiters, but we |
|
686 | # that readline treats incorrectly as delimiters, but we | |
684 | # don't want to treat as delimiters in filename matching |
|
687 | # don't want to treat as delimiters in filename matching | |
685 | # when escaped with backslash |
|
688 | # when escaped with backslash | |
686 | if text.startswith('!'): |
|
689 | if text.startswith('!'): | |
687 | text = text[1:] |
|
690 | text = text[1:] | |
688 | text_prefix = u'!' |
|
691 | text_prefix = u'!' | |
689 | else: |
|
692 | else: | |
690 | text_prefix = u'' |
|
693 | text_prefix = u'' | |
691 |
|
694 | |||
692 | text_until_cursor = self.text_until_cursor |
|
695 | text_until_cursor = self.text_until_cursor | |
693 | # track strings with open quotes |
|
696 | # track strings with open quotes | |
694 | open_quotes = has_open_quotes(text_until_cursor) |
|
697 | open_quotes = has_open_quotes(text_until_cursor) | |
695 |
|
698 | |||
696 | if '(' in text_until_cursor or '[' in text_until_cursor: |
|
699 | if '(' in text_until_cursor or '[' in text_until_cursor: | |
697 | lsplit = text |
|
700 | lsplit = text | |
698 | else: |
|
701 | else: | |
699 | try: |
|
702 | try: | |
700 | # arg_split ~ shlex.split, but with unicode bugs fixed by us |
|
703 | # arg_split ~ shlex.split, but with unicode bugs fixed by us | |
701 | lsplit = arg_split(text_until_cursor)[-1] |
|
704 | lsplit = arg_split(text_until_cursor)[-1] | |
702 | except ValueError: |
|
705 | except ValueError: | |
703 | # typically an unmatched ", or backslash without escaped char. |
|
706 | # typically an unmatched ", or backslash without escaped char. | |
704 | if open_quotes: |
|
707 | if open_quotes: | |
705 | lsplit = text_until_cursor.split(open_quotes)[-1] |
|
708 | lsplit = text_until_cursor.split(open_quotes)[-1] | |
706 | else: |
|
709 | else: | |
707 | return [] |
|
710 | return [] | |
708 | except IndexError: |
|
711 | except IndexError: | |
709 | # tab pressed on empty line |
|
712 | # tab pressed on empty line | |
710 | lsplit = "" |
|
713 | lsplit = "" | |
711 |
|
714 | |||
712 | if not open_quotes and lsplit != protect_filename(lsplit): |
|
715 | if not open_quotes and lsplit != protect_filename(lsplit): | |
713 | # if protectables are found, do matching on the whole escaped name |
|
716 | # if protectables are found, do matching on the whole escaped name | |
714 | has_protectables = True |
|
717 | has_protectables = True | |
715 | text0,text = text,lsplit |
|
718 | text0,text = text,lsplit | |
716 | else: |
|
719 | else: | |
717 | has_protectables = False |
|
720 | has_protectables = False | |
718 | text = os.path.expanduser(text) |
|
721 | text = os.path.expanduser(text) | |
719 |
|
722 | |||
720 | if text == "": |
|
723 | if text == "": | |
721 | return [text_prefix + cast_unicode_py2(protect_filename(f)) for f in self.glob("*")] |
|
724 | return [text_prefix + cast_unicode_py2(protect_filename(f)) for f in self.glob("*")] | |
722 |
|
725 | |||
723 | # Compute the matches from the filesystem |
|
726 | # Compute the matches from the filesystem | |
724 | m0 = self.clean_glob(text.replace('\\','')) |
|
727 | m0 = self.clean_glob(text.replace('\\','')) | |
725 |
|
728 | |||
726 | if has_protectables: |
|
729 | if has_protectables: | |
727 | # If we had protectables, we need to revert our changes to the |
|
730 | # If we had protectables, we need to revert our changes to the | |
728 | # beginning of filename so that we don't double-write the part |
|
731 | # beginning of filename so that we don't double-write the part | |
729 | # of the filename we have so far |
|
732 | # of the filename we have so far | |
730 | len_lsplit = len(lsplit) |
|
733 | len_lsplit = len(lsplit) | |
731 | matches = [text_prefix + text0 + |
|
734 | matches = [text_prefix + text0 + | |
732 | protect_filename(f[len_lsplit:]) for f in m0] |
|
735 | protect_filename(f[len_lsplit:]) for f in m0] | |
733 | else: |
|
736 | else: | |
734 | if open_quotes: |
|
737 | if open_quotes: | |
735 | # if we have a string with an open quote, we don't need to |
|
738 | # if we have a string with an open quote, we don't need to | |
736 | # protect the names at all (and we _shouldn't_, as it |
|
739 | # protect the names at all (and we _shouldn't_, as it | |
737 | # would cause bugs when the filesystem call is made). |
|
740 | # would cause bugs when the filesystem call is made). | |
738 | matches = m0 |
|
741 | matches = m0 | |
739 | else: |
|
742 | else: | |
740 | matches = [text_prefix + |
|
743 | matches = [text_prefix + | |
741 | protect_filename(f) for f in m0] |
|
744 | protect_filename(f) for f in m0] | |
742 |
|
745 | |||
743 | # Mark directories in input list by appending '/' to their names. |
|
746 | # Mark directories in input list by appending '/' to their names. | |
744 | return [cast_unicode_py2(x+'/') if os.path.isdir(x) else x for x in matches] |
|
747 | return [cast_unicode_py2(x+'/') if os.path.isdir(x) else x for x in matches] | |
745 |
|
748 | |||
746 | def magic_matches(self, text): |
|
749 | def magic_matches(self, text): | |
747 | """Match magics""" |
|
750 | """Match magics""" | |
748 | # Get all shell magics now rather than statically, so magics loaded at |
|
751 | # Get all shell magics now rather than statically, so magics loaded at | |
749 | # runtime show up too. |
|
752 | # runtime show up too. | |
750 | lsm = self.shell.magics_manager.lsmagic() |
|
753 | lsm = self.shell.magics_manager.lsmagic() | |
751 | line_magics = lsm['line'] |
|
754 | line_magics = lsm['line'] | |
752 | cell_magics = lsm['cell'] |
|
755 | cell_magics = lsm['cell'] | |
753 | pre = self.magic_escape |
|
756 | pre = self.magic_escape | |
754 | pre2 = pre+pre |
|
757 | pre2 = pre+pre | |
755 |
|
758 | |||
756 | # Completion logic: |
|
759 | # Completion logic: | |
757 | # - user gives %%: only do cell magics |
|
760 | # - user gives %%: only do cell magics | |
758 | # - user gives %: do both line and cell magics |
|
761 | # - user gives %: do both line and cell magics | |
759 | # - no prefix: do both |
|
762 | # - no prefix: do both | |
760 | # In other words, line magics are skipped if the user gives %% explicitly |
|
763 | # In other words, line magics are skipped if the user gives %% explicitly | |
761 | bare_text = text.lstrip(pre) |
|
764 | bare_text = text.lstrip(pre) | |
762 | comp = [ pre2+m for m in cell_magics if m.startswith(bare_text)] |
|
765 | comp = [ pre2+m for m in cell_magics if m.startswith(bare_text)] | |
763 | if not text.startswith(pre2): |
|
766 | if not text.startswith(pre2): | |
764 | comp += [ pre+m for m in line_magics if m.startswith(bare_text)] |
|
767 | comp += [ pre+m for m in line_magics if m.startswith(bare_text)] | |
765 | return [cast_unicode_py2(c) for c in comp] |
|
768 | return [cast_unicode_py2(c) for c in comp] | |
766 |
|
769 | |||
767 |
|
770 | |||
768 | def python_matches(self, text): |
|
771 | def python_matches(self, text): | |
769 | """Match attributes or global python names""" |
|
772 | """Match attributes or global python names""" | |
770 | if "." in text: |
|
773 | if "." in text: | |
771 | try: |
|
774 | try: | |
772 | matches = self.attr_matches(text) |
|
775 | matches = self.attr_matches(text) | |
773 | if text.endswith('.') and self.omit__names: |
|
776 | if text.endswith('.') and self.omit__names: | |
774 | if self.omit__names == 1: |
|
777 | if self.omit__names == 1: | |
775 | # true if txt is _not_ a __ name, false otherwise: |
|
778 | # true if txt is _not_ a __ name, false otherwise: | |
776 | no__name = (lambda txt: |
|
779 | no__name = (lambda txt: | |
777 | re.match(r'.*\.__.*?__',txt) is None) |
|
780 | re.match(r'.*\.__.*?__',txt) is None) | |
778 | else: |
|
781 | else: | |
779 | # true if txt is _not_ a _ name, false otherwise: |
|
782 | # true if txt is _not_ a _ name, false otherwise: | |
780 | no__name = (lambda txt: |
|
783 | no__name = (lambda txt: | |
781 | re.match(r'\._.*?',txt[txt.rindex('.'):]) is None) |
|
784 | re.match(r'\._.*?',txt[txt.rindex('.'):]) is None) | |
782 | matches = filter(no__name, matches) |
|
785 | matches = filter(no__name, matches) | |
783 | except NameError: |
|
786 | except NameError: | |
784 | # catches <undefined attributes>.<tab> |
|
787 | # catches <undefined attributes>.<tab> | |
785 | matches = [] |
|
788 | matches = [] | |
786 | else: |
|
789 | else: | |
787 | matches = self.global_matches(text) |
|
790 | matches = self.global_matches(text) | |
788 | return matches |
|
791 | return matches | |
789 |
|
792 | |||
790 | def _default_arguments_from_docstring(self, doc): |
|
793 | def _default_arguments_from_docstring(self, doc): | |
791 | """Parse the first line of docstring for call signature. |
|
794 | """Parse the first line of docstring for call signature. | |
792 |
|
795 | |||
793 | Docstring should be of the form 'min(iterable[, key=func])\n'. |
|
796 | Docstring should be of the form 'min(iterable[, key=func])\n'. | |
794 | It can also parse cython docstring of the form |
|
797 | It can also parse cython docstring of the form | |
795 | 'Minuit.migrad(self, int ncall=10000, resume=True, int nsplit=1)'. |
|
798 | 'Minuit.migrad(self, int ncall=10000, resume=True, int nsplit=1)'. | |
796 | """ |
|
799 | """ | |
797 | if doc is None: |
|
800 | if doc is None: | |
798 | return [] |
|
801 | return [] | |
799 |
|
802 | |||
800 | #care only the firstline |
|
803 | #care only the firstline | |
801 | line = doc.lstrip().splitlines()[0] |
|
804 | line = doc.lstrip().splitlines()[0] | |
802 |
|
805 | |||
803 | #p = re.compile(r'^[\w|\s.]+\(([^)]*)\).*') |
|
806 | #p = re.compile(r'^[\w|\s.]+\(([^)]*)\).*') | |
804 | #'min(iterable[, key=func])\n' -> 'iterable[, key=func]' |
|
807 | #'min(iterable[, key=func])\n' -> 'iterable[, key=func]' | |
805 | sig = self.docstring_sig_re.search(line) |
|
808 | sig = self.docstring_sig_re.search(line) | |
806 | if sig is None: |
|
809 | if sig is None: | |
807 | return [] |
|
810 | return [] | |
808 | # iterable[, key=func]' -> ['iterable[' ,' key=func]'] |
|
811 | # iterable[, key=func]' -> ['iterable[' ,' key=func]'] | |
809 | sig = sig.groups()[0].split(',') |
|
812 | sig = sig.groups()[0].split(',') | |
810 | ret = [] |
|
813 | ret = [] | |
811 | for s in sig: |
|
814 | for s in sig: | |
812 | #re.compile(r'[\s|\[]*(\w+)(?:\s*=\s*.*)') |
|
815 | #re.compile(r'[\s|\[]*(\w+)(?:\s*=\s*.*)') | |
813 | ret += self.docstring_kwd_re.findall(s) |
|
816 | ret += self.docstring_kwd_re.findall(s) | |
814 | return ret |
|
817 | return ret | |
815 |
|
818 | |||
816 | def _default_arguments(self, obj): |
|
819 | def _default_arguments(self, obj): | |
817 | """Return the list of default arguments of obj if it is callable, |
|
820 | """Return the list of default arguments of obj if it is callable, | |
818 | or empty list otherwise.""" |
|
821 | or empty list otherwise.""" | |
819 | call_obj = obj |
|
822 | call_obj = obj | |
820 | ret = [] |
|
823 | ret = [] | |
821 | if inspect.isbuiltin(obj): |
|
824 | if inspect.isbuiltin(obj): | |
822 | pass |
|
825 | pass | |
823 | elif not (inspect.isfunction(obj) or inspect.ismethod(obj)): |
|
826 | elif not (inspect.isfunction(obj) or inspect.ismethod(obj)): | |
824 | if inspect.isclass(obj): |
|
827 | if inspect.isclass(obj): | |
825 | #for cython embededsignature=True the constructor docstring |
|
828 | #for cython embededsignature=True the constructor docstring | |
826 | #belongs to the object itself not __init__ |
|
829 | #belongs to the object itself not __init__ | |
827 | ret += self._default_arguments_from_docstring( |
|
830 | ret += self._default_arguments_from_docstring( | |
828 | getattr(obj, '__doc__', '')) |
|
831 | getattr(obj, '__doc__', '')) | |
829 | # for classes, check for __init__,__new__ |
|
832 | # for classes, check for __init__,__new__ | |
830 | call_obj = (getattr(obj, '__init__', None) or |
|
833 | call_obj = (getattr(obj, '__init__', None) or | |
831 | getattr(obj, '__new__', None)) |
|
834 | getattr(obj, '__new__', None)) | |
832 | # for all others, check if they are __call__able |
|
835 | # for all others, check if they are __call__able | |
833 | elif hasattr(obj, '__call__'): |
|
836 | elif hasattr(obj, '__call__'): | |
834 | call_obj = obj.__call__ |
|
837 | call_obj = obj.__call__ | |
835 | ret += self._default_arguments_from_docstring( |
|
838 | ret += self._default_arguments_from_docstring( | |
836 | getattr(call_obj, '__doc__', '')) |
|
839 | getattr(call_obj, '__doc__', '')) | |
837 |
|
840 | |||
838 | if PY3: |
|
841 | if PY3: | |
839 | _keeps = (inspect.Parameter.KEYWORD_ONLY, |
|
842 | _keeps = (inspect.Parameter.KEYWORD_ONLY, | |
840 | inspect.Parameter.POSITIONAL_OR_KEYWORD) |
|
843 | inspect.Parameter.POSITIONAL_OR_KEYWORD) | |
841 | signature = inspect.signature |
|
844 | signature = inspect.signature | |
842 | else: |
|
845 | else: | |
843 | import IPython.utils.signatures |
|
846 | import IPython.utils.signatures | |
844 | _keeps = (IPython.utils.signatures.Parameter.KEYWORD_ONLY, |
|
847 | _keeps = (IPython.utils.signatures.Parameter.KEYWORD_ONLY, | |
845 | IPython.utils.signatures.Parameter.POSITIONAL_OR_KEYWORD) |
|
848 | IPython.utils.signatures.Parameter.POSITIONAL_OR_KEYWORD) | |
846 | signature = IPython.utils.signatures.signature |
|
849 | signature = IPython.utils.signatures.signature | |
847 |
|
850 | |||
848 | try: |
|
851 | try: | |
849 | sig = signature(call_obj) |
|
852 | sig = signature(call_obj) | |
850 | ret.extend(k for k, v in sig.parameters.items() if |
|
853 | ret.extend(k for k, v in sig.parameters.items() if | |
851 | v.kind in _keeps) |
|
854 | v.kind in _keeps) | |
852 | except ValueError: |
|
855 | except ValueError: | |
853 | pass |
|
856 | pass | |
854 |
|
857 | |||
855 | return list(set(ret)) |
|
858 | return list(set(ret)) | |
856 |
|
859 | |||
857 | def python_func_kw_matches(self,text): |
|
860 | def python_func_kw_matches(self,text): | |
858 | """Match named parameters (kwargs) of the last open function""" |
|
861 | """Match named parameters (kwargs) of the last open function""" | |
859 |
|
862 | |||
860 | if "." in text: # a parameter cannot be dotted |
|
863 | if "." in text: # a parameter cannot be dotted | |
861 | return [] |
|
864 | return [] | |
862 | try: regexp = self.__funcParamsRegex |
|
865 | try: regexp = self.__funcParamsRegex | |
863 | except AttributeError: |
|
866 | except AttributeError: | |
864 | regexp = self.__funcParamsRegex = re.compile(r''' |
|
867 | regexp = self.__funcParamsRegex = re.compile(r''' | |
865 | '.*?(?<!\\)' | # single quoted strings or |
|
868 | '.*?(?<!\\)' | # single quoted strings or | |
866 | ".*?(?<!\\)" | # double quoted strings or |
|
869 | ".*?(?<!\\)" | # double quoted strings or | |
867 | \w+ | # identifier |
|
870 | \w+ | # identifier | |
868 | \S # other characters |
|
871 | \S # other characters | |
869 | ''', re.VERBOSE | re.DOTALL) |
|
872 | ''', re.VERBOSE | re.DOTALL) | |
870 | # 1. find the nearest identifier that comes before an unclosed |
|
873 | # 1. find the nearest identifier that comes before an unclosed | |
871 | # parenthesis before the cursor |
|
874 | # parenthesis before the cursor | |
872 | # e.g. for "foo (1+bar(x), pa<cursor>,a=1)", the candidate is "foo" |
|
875 | # e.g. for "foo (1+bar(x), pa<cursor>,a=1)", the candidate is "foo" | |
873 | tokens = regexp.findall(self.text_until_cursor) |
|
876 | tokens = regexp.findall(self.text_until_cursor) | |
874 | tokens.reverse() |
|
877 | tokens.reverse() | |
875 | iterTokens = iter(tokens); openPar = 0 |
|
878 | iterTokens = iter(tokens); openPar = 0 | |
876 |
|
879 | |||
877 | for token in iterTokens: |
|
880 | for token in iterTokens: | |
878 | if token == ')': |
|
881 | if token == ')': | |
879 | openPar -= 1 |
|
882 | openPar -= 1 | |
880 | elif token == '(': |
|
883 | elif token == '(': | |
881 | openPar += 1 |
|
884 | openPar += 1 | |
882 | if openPar > 0: |
|
885 | if openPar > 0: | |
883 | # found the last unclosed parenthesis |
|
886 | # found the last unclosed parenthesis | |
884 | break |
|
887 | break | |
885 | else: |
|
888 | else: | |
886 | return [] |
|
889 | return [] | |
887 | # 2. Concatenate dotted names ("foo.bar" for "foo.bar(x, pa" ) |
|
890 | # 2. Concatenate dotted names ("foo.bar" for "foo.bar(x, pa" ) | |
888 | ids = [] |
|
891 | ids = [] | |
889 | isId = re.compile(r'\w+$').match |
|
892 | isId = re.compile(r'\w+$').match | |
890 |
|
893 | |||
891 | while True: |
|
894 | while True: | |
892 | try: |
|
895 | try: | |
893 | ids.append(next(iterTokens)) |
|
896 | ids.append(next(iterTokens)) | |
894 | if not isId(ids[-1]): |
|
897 | if not isId(ids[-1]): | |
895 | ids.pop(); break |
|
898 | ids.pop(); break | |
896 | if not next(iterTokens) == '.': |
|
899 | if not next(iterTokens) == '.': | |
897 | break |
|
900 | break | |
898 | except StopIteration: |
|
901 | except StopIteration: | |
899 | break |
|
902 | break | |
900 | # lookup the candidate callable matches either using global_matches |
|
903 | # lookup the candidate callable matches either using global_matches | |
901 | # or attr_matches for dotted names |
|
904 | # or attr_matches for dotted names | |
902 | if len(ids) == 1: |
|
905 | if len(ids) == 1: | |
903 | callableMatches = self.global_matches(ids[0]) |
|
906 | callableMatches = self.global_matches(ids[0]) | |
904 | else: |
|
907 | else: | |
905 | callableMatches = self.attr_matches('.'.join(ids[::-1])) |
|
908 | callableMatches = self.attr_matches('.'.join(ids[::-1])) | |
906 | argMatches = [] |
|
909 | argMatches = [] | |
907 | for callableMatch in callableMatches: |
|
910 | for callableMatch in callableMatches: | |
908 | try: |
|
911 | try: | |
909 | namedArgs = self._default_arguments(eval(callableMatch, |
|
912 | namedArgs = self._default_arguments(eval(callableMatch, | |
910 | self.namespace)) |
|
913 | self.namespace)) | |
911 | except: |
|
914 | except: | |
912 | continue |
|
915 | continue | |
913 |
|
916 | |||
914 | for namedArg in namedArgs: |
|
917 | for namedArg in namedArgs: | |
915 | if namedArg.startswith(text): |
|
918 | if namedArg.startswith(text): | |
916 | argMatches.append(u"%s=" %namedArg) |
|
919 | argMatches.append(u"%s=" %namedArg) | |
917 | return argMatches |
|
920 | return argMatches | |
918 |
|
921 | |||
919 | def dict_key_matches(self, text): |
|
922 | def dict_key_matches(self, text): | |
920 | "Match string keys in a dictionary, after e.g. 'foo[' " |
|
923 | "Match string keys in a dictionary, after e.g. 'foo[' " | |
921 | def get_keys(obj): |
|
924 | def get_keys(obj): | |
922 | # Objects can define their own completions by defining an |
|
925 | # Objects can define their own completions by defining an | |
923 | # _ipy_key_completions_() method. |
|
926 | # _ipy_key_completions_() method. | |
924 | method = get_real_method(obj, '_ipython_key_completions_') |
|
927 | method = get_real_method(obj, '_ipython_key_completions_') | |
925 | if method is not None: |
|
928 | if method is not None: | |
926 | return method() |
|
929 | return method() | |
927 |
|
930 | |||
928 | # Special case some common in-memory dict-like types |
|
931 | # Special case some common in-memory dict-like types | |
929 | if isinstance(obj, dict) or\ |
|
932 | if isinstance(obj, dict) or\ | |
930 | _safe_isinstance(obj, 'pandas', 'DataFrame'): |
|
933 | _safe_isinstance(obj, 'pandas', 'DataFrame'): | |
931 | try: |
|
934 | try: | |
932 | return list(obj.keys()) |
|
935 | return list(obj.keys()) | |
933 | except Exception: |
|
936 | except Exception: | |
934 | return [] |
|
937 | return [] | |
935 | elif _safe_isinstance(obj, 'numpy', 'ndarray') or\ |
|
938 | elif _safe_isinstance(obj, 'numpy', 'ndarray') or\ | |
936 | _safe_isinstance(obj, 'numpy', 'void'): |
|
939 | _safe_isinstance(obj, 'numpy', 'void'): | |
937 | return obj.dtype.names or [] |
|
940 | return obj.dtype.names or [] | |
938 | return [] |
|
941 | return [] | |
939 |
|
942 | |||
940 | try: |
|
943 | try: | |
941 | regexps = self.__dict_key_regexps |
|
944 | regexps = self.__dict_key_regexps | |
942 | except AttributeError: |
|
945 | except AttributeError: | |
943 | dict_key_re_fmt = r'''(?x) |
|
946 | dict_key_re_fmt = r'''(?x) | |
944 | ( # match dict-referring expression wrt greedy setting |
|
947 | ( # match dict-referring expression wrt greedy setting | |
945 | %s |
|
948 | %s | |
946 | ) |
|
949 | ) | |
947 | \[ # open bracket |
|
950 | \[ # open bracket | |
948 | \s* # and optional whitespace |
|
951 | \s* # and optional whitespace | |
949 | ([uUbB]? # string prefix (r not handled) |
|
952 | ([uUbB]? # string prefix (r not handled) | |
950 | (?: # unclosed string |
|
953 | (?: # unclosed string | |
951 | '(?:[^']|(?<!\\)\\')* |
|
954 | '(?:[^']|(?<!\\)\\')* | |
952 | | |
|
955 | | | |
953 | "(?:[^"]|(?<!\\)\\")* |
|
956 | "(?:[^"]|(?<!\\)\\")* | |
954 | ) |
|
957 | ) | |
955 | )? |
|
958 | )? | |
956 | $ |
|
959 | $ | |
957 | ''' |
|
960 | ''' | |
958 | regexps = self.__dict_key_regexps = { |
|
961 | regexps = self.__dict_key_regexps = { | |
959 | False: re.compile(dict_key_re_fmt % ''' |
|
962 | False: re.compile(dict_key_re_fmt % ''' | |
960 | # identifiers separated by . |
|
963 | # identifiers separated by . | |
961 | (?!\d)\w+ |
|
964 | (?!\d)\w+ | |
962 | (?:\.(?!\d)\w+)* |
|
965 | (?:\.(?!\d)\w+)* | |
963 | '''), |
|
966 | '''), | |
964 | True: re.compile(dict_key_re_fmt % ''' |
|
967 | True: re.compile(dict_key_re_fmt % ''' | |
965 | .+ |
|
968 | .+ | |
966 | ''') |
|
969 | ''') | |
967 | } |
|
970 | } | |
968 |
|
971 | |||
969 | match = regexps[self.greedy].search(self.text_until_cursor) |
|
972 | match = regexps[self.greedy].search(self.text_until_cursor) | |
970 | if match is None: |
|
973 | if match is None: | |
971 | return [] |
|
974 | return [] | |
972 |
|
975 | |||
973 | expr, prefix = match.groups() |
|
976 | expr, prefix = match.groups() | |
974 | try: |
|
977 | try: | |
975 | obj = eval(expr, self.namespace) |
|
978 | obj = eval(expr, self.namespace) | |
976 | except Exception: |
|
979 | except Exception: | |
977 | try: |
|
980 | try: | |
978 | obj = eval(expr, self.global_namespace) |
|
981 | obj = eval(expr, self.global_namespace) | |
979 | except Exception: |
|
982 | except Exception: | |
980 | return [] |
|
983 | return [] | |
981 |
|
984 | |||
982 | keys = get_keys(obj) |
|
985 | keys = get_keys(obj) | |
983 | if not keys: |
|
986 | if not keys: | |
984 | return keys |
|
987 | return keys | |
985 | closing_quote, token_offset, matches = match_dict_keys(keys, prefix, self.splitter.delims) |
|
988 | closing_quote, token_offset, matches = match_dict_keys(keys, prefix, self.splitter.delims) | |
986 | if not matches: |
|
989 | if not matches: | |
987 | return matches |
|
990 | return matches | |
988 |
|
991 | |||
989 | # get the cursor position of |
|
992 | # get the cursor position of | |
990 | # - the text being completed |
|
993 | # - the text being completed | |
991 | # - the start of the key text |
|
994 | # - the start of the key text | |
992 | # - the start of the completion |
|
995 | # - the start of the completion | |
993 | text_start = len(self.text_until_cursor) - len(text) |
|
996 | text_start = len(self.text_until_cursor) - len(text) | |
994 | if prefix: |
|
997 | if prefix: | |
995 | key_start = match.start(2) |
|
998 | key_start = match.start(2) | |
996 | completion_start = key_start + token_offset |
|
999 | completion_start = key_start + token_offset | |
997 | else: |
|
1000 | else: | |
998 | key_start = completion_start = match.end() |
|
1001 | key_start = completion_start = match.end() | |
999 |
|
1002 | |||
1000 | # grab the leading prefix, to make sure all completions start with `text` |
|
1003 | # grab the leading prefix, to make sure all completions start with `text` | |
1001 | if text_start > key_start: |
|
1004 | if text_start > key_start: | |
1002 | leading = '' |
|
1005 | leading = '' | |
1003 | else: |
|
1006 | else: | |
1004 | leading = text[text_start:completion_start] |
|
1007 | leading = text[text_start:completion_start] | |
1005 |
|
1008 | |||
1006 | # the index of the `[` character |
|
1009 | # the index of the `[` character | |
1007 | bracket_idx = match.end(1) |
|
1010 | bracket_idx = match.end(1) | |
1008 |
|
1011 | |||
1009 | # append closing quote and bracket as appropriate |
|
1012 | # append closing quote and bracket as appropriate | |
1010 | # this is *not* appropriate if the opening quote or bracket is outside |
|
1013 | # this is *not* appropriate if the opening quote or bracket is outside | |
1011 | # the text given to this method |
|
1014 | # the text given to this method | |
1012 | suf = '' |
|
1015 | suf = '' | |
1013 | continuation = self.line_buffer[len(self.text_until_cursor):] |
|
1016 | continuation = self.line_buffer[len(self.text_until_cursor):] | |
1014 | if key_start > text_start and closing_quote: |
|
1017 | if key_start > text_start and closing_quote: | |
1015 | # quotes were opened inside text, maybe close them |
|
1018 | # quotes were opened inside text, maybe close them | |
1016 | if continuation.startswith(closing_quote): |
|
1019 | if continuation.startswith(closing_quote): | |
1017 | continuation = continuation[len(closing_quote):] |
|
1020 | continuation = continuation[len(closing_quote):] | |
1018 | else: |
|
1021 | else: | |
1019 | suf += closing_quote |
|
1022 | suf += closing_quote | |
1020 | if bracket_idx > text_start: |
|
1023 | if bracket_idx > text_start: | |
1021 | # brackets were opened inside text, maybe close them |
|
1024 | # brackets were opened inside text, maybe close them | |
1022 | if not continuation.startswith(']'): |
|
1025 | if not continuation.startswith(']'): | |
1023 | suf += ']' |
|
1026 | suf += ']' | |
1024 |
|
1027 | |||
1025 | return [leading + k + suf for k in matches] |
|
1028 | return [leading + k + suf for k in matches] | |
1026 |
|
1029 | |||
1027 | def unicode_name_matches(self, text): |
|
1030 | def unicode_name_matches(self, text): | |
1028 | u"""Match Latex-like syntax for unicode characters base |
|
1031 | u"""Match Latex-like syntax for unicode characters base | |
1029 | on the name of the character. |
|
1032 | on the name of the character. | |
1030 |
|
1033 | |||
1031 | This does \\GREEK SMALL LETTER ETA -> Ξ· |
|
1034 | This does \\GREEK SMALL LETTER ETA -> Ξ· | |
1032 |
|
1035 | |||
1033 | Works only on valid python 3 identifier, or on combining characters that |
|
1036 | Works only on valid python 3 identifier, or on combining characters that | |
1034 | will combine to form a valid identifier. |
|
1037 | will combine to form a valid identifier. | |
1035 |
|
1038 | |||
1036 | Used on Python 3 only. |
|
1039 | Used on Python 3 only. | |
1037 | """ |
|
1040 | """ | |
1038 | slashpos = text.rfind('\\') |
|
1041 | slashpos = text.rfind('\\') | |
1039 | if slashpos > -1: |
|
1042 | if slashpos > -1: | |
1040 | s = text[slashpos+1:] |
|
1043 | s = text[slashpos+1:] | |
1041 | try : |
|
1044 | try : | |
1042 | unic = unicodedata.lookup(s) |
|
1045 | unic = unicodedata.lookup(s) | |
1043 | # allow combining chars |
|
1046 | # allow combining chars | |
1044 | if ('a'+unic).isidentifier(): |
|
1047 | if ('a'+unic).isidentifier(): | |
1045 | return '\\'+s,[unic] |
|
1048 | return '\\'+s,[unic] | |
1046 | except KeyError: |
|
1049 | except KeyError: | |
1047 | pass |
|
1050 | pass | |
1048 | return u'', [] |
|
1051 | return u'', [] | |
1049 |
|
1052 | |||
1050 |
|
1053 | |||
1051 |
|
1054 | |||
1052 |
|
1055 | |||
1053 | def latex_matches(self, text): |
|
1056 | def latex_matches(self, text): | |
1054 | u"""Match Latex syntax for unicode characters. |
|
1057 | u"""Match Latex syntax for unicode characters. | |
1055 |
|
1058 | |||
1056 | This does both \\alp -> \\alpha and \\alpha -> Ξ± |
|
1059 | This does both \\alp -> \\alpha and \\alpha -> Ξ± | |
1057 |
|
1060 | |||
1058 | Used on Python 3 only. |
|
1061 | Used on Python 3 only. | |
1059 | """ |
|
1062 | """ | |
1060 | slashpos = text.rfind('\\') |
|
1063 | slashpos = text.rfind('\\') | |
1061 | if slashpos > -1: |
|
1064 | if slashpos > -1: | |
1062 | s = text[slashpos:] |
|
1065 | s = text[slashpos:] | |
1063 | if s in latex_symbols: |
|
1066 | if s in latex_symbols: | |
1064 | # Try to complete a full latex symbol to unicode |
|
1067 | # Try to complete a full latex symbol to unicode | |
1065 | # \\alpha -> Ξ± |
|
1068 | # \\alpha -> Ξ± | |
1066 | return s, [latex_symbols[s]] |
|
1069 | return s, [latex_symbols[s]] | |
1067 | else: |
|
1070 | else: | |
1068 | # If a user has partially typed a latex symbol, give them |
|
1071 | # If a user has partially typed a latex symbol, give them | |
1069 | # a full list of options \al -> [\aleph, \alpha] |
|
1072 | # a full list of options \al -> [\aleph, \alpha] | |
1070 | matches = [k for k in latex_symbols if k.startswith(s)] |
|
1073 | matches = [k for k in latex_symbols if k.startswith(s)] | |
1071 | return s, matches |
|
1074 | return s, matches | |
1072 | return u'', [] |
|
1075 | return u'', [] | |
1073 |
|
1076 | |||
1074 | def dispatch_custom_completer(self, text): |
|
1077 | def dispatch_custom_completer(self, text): | |
|
1078 | if not self.custom_completers: | |||
|
1079 | return | |||
|
1080 | ||||
1075 | line = self.line_buffer |
|
1081 | line = self.line_buffer | |
1076 | if not line.strip(): |
|
1082 | if not line.strip(): | |
1077 | return None |
|
1083 | return None | |
1078 |
|
1084 | |||
1079 | # Create a little structure to pass all the relevant information about |
|
1085 | # Create a little structure to pass all the relevant information about | |
1080 | # the current completion to any custom completer. |
|
1086 | # the current completion to any custom completer. | |
1081 | event = Bunch() |
|
1087 | event = Bunch() | |
1082 | event.line = line |
|
1088 | event.line = line | |
1083 | event.symbol = text |
|
1089 | event.symbol = text | |
1084 | cmd = line.split(None,1)[0] |
|
1090 | cmd = line.split(None,1)[0] | |
1085 | event.command = cmd |
|
1091 | event.command = cmd | |
1086 | event.text_until_cursor = self.text_until_cursor |
|
1092 | event.text_until_cursor = self.text_until_cursor | |
1087 |
|
1093 | |||
1088 | # for foo etc, try also to find completer for %foo |
|
1094 | # for foo etc, try also to find completer for %foo | |
1089 | if not cmd.startswith(self.magic_escape): |
|
1095 | if not cmd.startswith(self.magic_escape): | |
1090 | try_magic = self.custom_completers.s_matches( |
|
1096 | try_magic = self.custom_completers.s_matches( | |
1091 | self.magic_escape + cmd) |
|
1097 | self.magic_escape + cmd) | |
1092 | else: |
|
1098 | else: | |
1093 | try_magic = [] |
|
1099 | try_magic = [] | |
1094 |
|
1100 | |||
1095 | for c in itertools.chain(self.custom_completers.s_matches(cmd), |
|
1101 | for c in itertools.chain(self.custom_completers.s_matches(cmd), | |
1096 | try_magic, |
|
1102 | try_magic, | |
1097 | self.custom_completers.flat_matches(self.text_until_cursor)): |
|
1103 | self.custom_completers.flat_matches(self.text_until_cursor)): | |
1098 | try: |
|
1104 | try: | |
1099 | res = c(event) |
|
1105 | res = c(event) | |
1100 | if res: |
|
1106 | if res: | |
1101 | # first, try case sensitive match |
|
1107 | # first, try case sensitive match | |
1102 | withcase = [cast_unicode_py2(r) for r in res if r.startswith(text)] |
|
1108 | withcase = [cast_unicode_py2(r) for r in res if r.startswith(text)] | |
1103 | if withcase: |
|
1109 | if withcase: | |
1104 | return withcase |
|
1110 | return withcase | |
1105 | # if none, then case insensitive ones are ok too |
|
1111 | # if none, then case insensitive ones are ok too | |
1106 | text_low = text.lower() |
|
1112 | text_low = text.lower() | |
1107 | return [cast_unicode_py2(r) for r in res if r.lower().startswith(text_low)] |
|
1113 | return [cast_unicode_py2(r) for r in res if r.lower().startswith(text_low)] | |
1108 | except TryNext: |
|
1114 | except TryNext: | |
1109 | pass |
|
1115 | pass | |
1110 |
|
1116 | |||
1111 | return None |
|
1117 | return None | |
1112 |
|
1118 | |||
1113 | def complete(self, text=None, line_buffer=None, cursor_pos=None): |
|
1119 | def complete(self, text=None, line_buffer=None, cursor_pos=None): | |
1114 | """Find completions for the given text and line context. |
|
1120 | """Find completions for the given text and line context. | |
1115 |
|
1121 | |||
1116 | Note that both the text and the line_buffer are optional, but at least |
|
1122 | Note that both the text and the line_buffer are optional, but at least | |
1117 | one of them must be given. |
|
1123 | one of them must be given. | |
1118 |
|
1124 | |||
1119 | Parameters |
|
1125 | Parameters | |
1120 | ---------- |
|
1126 | ---------- | |
1121 | text : string, optional |
|
1127 | text : string, optional | |
1122 | Text to perform the completion on. If not given, the line buffer |
|
1128 | Text to perform the completion on. If not given, the line buffer | |
1123 | is split using the instance's CompletionSplitter object. |
|
1129 | is split using the instance's CompletionSplitter object. | |
1124 |
|
1130 | |||
1125 | line_buffer : string, optional |
|
1131 | line_buffer : string, optional | |
1126 | If not given, the completer attempts to obtain the current line |
|
1132 | If not given, the completer attempts to obtain the current line | |
1127 | buffer via readline. This keyword allows clients which are |
|
1133 | buffer via readline. This keyword allows clients which are | |
1128 | requesting for text completions in non-readline contexts to inform |
|
1134 | requesting for text completions in non-readline contexts to inform | |
1129 | the completer of the entire text. |
|
1135 | the completer of the entire text. | |
1130 |
|
1136 | |||
1131 | cursor_pos : int, optional |
|
1137 | cursor_pos : int, optional | |
1132 | Index of the cursor in the full line buffer. Should be provided by |
|
1138 | Index of the cursor in the full line buffer. Should be provided by | |
1133 | remote frontends where kernel has no access to frontend state. |
|
1139 | remote frontends where kernel has no access to frontend state. | |
1134 |
|
1140 | |||
1135 | Returns |
|
1141 | Returns | |
1136 | ------- |
|
1142 | ------- | |
1137 | text : str |
|
1143 | text : str | |
1138 | Text that was actually used in the completion. |
|
1144 | Text that was actually used in the completion. | |
1139 |
|
1145 | |||
1140 | matches : list |
|
1146 | matches : list | |
1141 | A list of completion matches. |
|
1147 | A list of completion matches. | |
1142 | """ |
|
1148 | """ | |
1143 | # if the cursor position isn't given, the only sane assumption we can |
|
1149 | # if the cursor position isn't given, the only sane assumption we can | |
1144 | # make is that it's at the end of the line (the common case) |
|
1150 | # make is that it's at the end of the line (the common case) | |
1145 | if cursor_pos is None: |
|
1151 | if cursor_pos is None: | |
1146 | cursor_pos = len(line_buffer) if text is None else len(text) |
|
1152 | cursor_pos = len(line_buffer) if text is None else len(text) | |
1147 |
|
1153 | |||
1148 | if PY3: |
|
1154 | if PY3: | |
1149 |
|
1155 | |||
1150 | base_text = text if not line_buffer else line_buffer[:cursor_pos] |
|
1156 | base_text = text if not line_buffer else line_buffer[:cursor_pos] | |
1151 | latex_text, latex_matches = self.latex_matches(base_text) |
|
1157 | latex_text, latex_matches = self.latex_matches(base_text) | |
1152 | if latex_matches: |
|
1158 | if latex_matches: | |
1153 | return latex_text, latex_matches |
|
1159 | return latex_text, latex_matches | |
1154 | name_text = '' |
|
1160 | name_text = '' | |
1155 | name_matches = [] |
|
1161 | name_matches = [] | |
1156 | for meth in (self.unicode_name_matches, back_latex_name_matches, back_unicode_name_matches): |
|
1162 | for meth in (self.unicode_name_matches, back_latex_name_matches, back_unicode_name_matches): | |
1157 | name_text, name_matches = meth(base_text) |
|
1163 | name_text, name_matches = meth(base_text) | |
1158 | if name_text: |
|
1164 | if name_text: | |
1159 | return name_text, name_matches |
|
1165 | return name_text, name_matches | |
1160 |
|
1166 | |||
1161 | # if text is either None or an empty string, rely on the line buffer |
|
1167 | # if text is either None or an empty string, rely on the line buffer | |
1162 | if not text: |
|
1168 | if not text: | |
1163 | text = self.splitter.split_line(line_buffer, cursor_pos) |
|
1169 | text = self.splitter.split_line(line_buffer, cursor_pos) | |
1164 |
|
1170 | |||
1165 | # If no line buffer is given, assume the input text is all there was |
|
1171 | # If no line buffer is given, assume the input text is all there was | |
1166 | if line_buffer is None: |
|
1172 | if line_buffer is None: | |
1167 | line_buffer = text |
|
1173 | line_buffer = text | |
1168 |
|
1174 | |||
1169 | self.line_buffer = line_buffer |
|
1175 | self.line_buffer = line_buffer | |
1170 | self.text_until_cursor = self.line_buffer[:cursor_pos] |
|
1176 | self.text_until_cursor = self.line_buffer[:cursor_pos] | |
1171 |
|
1177 | |||
1172 | # Start with a clean slate of completions |
|
1178 | # Start with a clean slate of completions | |
1173 | self.matches[:] = [] |
|
1179 | self.matches[:] = [] | |
1174 | custom_res = self.dispatch_custom_completer(text) |
|
1180 | custom_res = self.dispatch_custom_completer(text) | |
1175 | if custom_res is not None: |
|
1181 | if custom_res is not None: | |
1176 | # did custom completers produce something? |
|
1182 | # did custom completers produce something? | |
1177 | self.matches = custom_res |
|
1183 | self.matches = custom_res | |
1178 | else: |
|
1184 | else: | |
1179 | # Extend the list of completions with the results of each |
|
1185 | # Extend the list of completions with the results of each | |
1180 | # matcher, so we return results to the user from all |
|
1186 | # matcher, so we return results to the user from all | |
1181 | # namespaces. |
|
1187 | # namespaces. | |
1182 | if self.merge_completions: |
|
1188 | if self.merge_completions: | |
1183 | self.matches = [] |
|
1189 | self.matches = [] | |
1184 | for matcher in self.matchers: |
|
1190 | for matcher in self.matchers: | |
1185 | try: |
|
1191 | try: | |
1186 | self.matches.extend(matcher(text)) |
|
1192 | self.matches.extend(matcher(text)) | |
1187 | except: |
|
1193 | except: | |
1188 | # Show the ugly traceback if the matcher causes an |
|
1194 | # Show the ugly traceback if the matcher causes an | |
1189 | # exception, but do NOT crash the kernel! |
|
1195 | # exception, but do NOT crash the kernel! | |
1190 | sys.excepthook(*sys.exc_info()) |
|
1196 | sys.excepthook(*sys.exc_info()) | |
1191 | else: |
|
1197 | else: | |
1192 | for matcher in self.matchers: |
|
1198 | for matcher in self.matchers: | |
1193 | self.matches = matcher(text) |
|
1199 | self.matches = matcher(text) | |
1194 | if self.matches: |
|
1200 | if self.matches: | |
1195 | break |
|
1201 | break | |
1196 | # FIXME: we should extend our api to return a dict with completions for |
|
1202 | # FIXME: we should extend our api to return a dict with completions for | |
1197 | # different types of objects. The rlcomplete() method could then |
|
1203 | # different types of objects. The rlcomplete() method could then | |
1198 | # simply collapse the dict into a list for readline, but we'd have |
|
1204 | # simply collapse the dict into a list for readline, but we'd have | |
1199 | # richer completion semantics in other evironments. |
|
1205 | # richer completion semantics in other evironments. | |
1200 | self.matches = sorted(set(self.matches), key=completions_sorting_key) |
|
1206 | self.matches = sorted(set(self.matches), key=completions_sorting_key) | |
1201 |
|
1207 | |||
1202 | return text, self.matches |
|
1208 | return text, self.matches | |
1203 |
|
1209 | |||
1204 | def rlcomplete(self, text, state): |
|
1210 | def rlcomplete(self, text, state): | |
1205 | """Return the state-th possible completion for 'text'. |
|
1211 | """Return the state-th possible completion for 'text'. | |
1206 |
|
1212 | |||
1207 | This is called successively with state == 0, 1, 2, ... until it |
|
1213 | This is called successively with state == 0, 1, 2, ... until it | |
1208 | returns None. The completion should begin with 'text'. |
|
1214 | returns None. The completion should begin with 'text'. | |
1209 |
|
1215 | |||
1210 | Parameters |
|
1216 | Parameters | |
1211 | ---------- |
|
1217 | ---------- | |
1212 | text : string |
|
1218 | text : string | |
1213 | Text to perform the completion on. |
|
1219 | Text to perform the completion on. | |
1214 |
|
1220 | |||
1215 | state : int |
|
1221 | state : int | |
1216 | Counter used by readline. |
|
1222 | Counter used by readline. | |
1217 | """ |
|
1223 | """ | |
1218 | if state==0: |
|
1224 | if state==0: | |
1219 |
|
1225 | |||
1220 | self.line_buffer = line_buffer = self.readline.get_line_buffer() |
|
1226 | self.line_buffer = line_buffer = self.readline.get_line_buffer() | |
1221 | cursor_pos = self.readline.get_endidx() |
|
1227 | cursor_pos = self.readline.get_endidx() | |
1222 |
|
1228 | |||
1223 | #io.rprint("\nRLCOMPLETE: %r %r %r" % |
|
1229 | #io.rprint("\nRLCOMPLETE: %r %r %r" % | |
1224 | # (text, line_buffer, cursor_pos) ) # dbg |
|
1230 | # (text, line_buffer, cursor_pos) ) # dbg | |
1225 |
|
1231 | |||
1226 | # if there is only a tab on a line with only whitespace, instead of |
|
1232 | # if there is only a tab on a line with only whitespace, instead of | |
1227 | # the mostly useless 'do you want to see all million completions' |
|
1233 | # the mostly useless 'do you want to see all million completions' | |
1228 | # message, just do the right thing and give the user his tab! |
|
1234 | # message, just do the right thing and give the user his tab! | |
1229 | # Incidentally, this enables pasting of tabbed text from an editor |
|
1235 | # Incidentally, this enables pasting of tabbed text from an editor | |
1230 | # (as long as autoindent is off). |
|
1236 | # (as long as autoindent is off). | |
1231 |
|
1237 | |||
1232 | # It should be noted that at least pyreadline still shows file |
|
1238 | # It should be noted that at least pyreadline still shows file | |
1233 | # completions - is there a way around it? |
|
1239 | # completions - is there a way around it? | |
1234 |
|
1240 | |||
1235 | # don't apply this on 'dumb' terminals, such as emacs buffers, so |
|
1241 | # don't apply this on 'dumb' terminals, such as emacs buffers, so | |
1236 | # we don't interfere with their own tab-completion mechanism. |
|
1242 | # we don't interfere with their own tab-completion mechanism. | |
1237 | if not (self.dumb_terminal or line_buffer.strip()): |
|
1243 | if not (self.dumb_terminal or line_buffer.strip()): | |
1238 | self.readline.insert_text('\t') |
|
1244 | self.readline.insert_text('\t') | |
1239 | sys.stdout.flush() |
|
1245 | sys.stdout.flush() | |
1240 | return None |
|
1246 | return None | |
1241 |
|
1247 | |||
1242 | # Note: debugging exceptions that may occur in completion is very |
|
1248 | # Note: debugging exceptions that may occur in completion is very | |
1243 | # tricky, because readline unconditionally silences them. So if |
|
1249 | # tricky, because readline unconditionally silences them. So if | |
1244 | # during development you suspect a bug in the completion code, turn |
|
1250 | # during development you suspect a bug in the completion code, turn | |
1245 | # this flag on temporarily by uncommenting the second form (don't |
|
1251 | # this flag on temporarily by uncommenting the second form (don't | |
1246 | # flip the value in the first line, as the '# dbg' marker can be |
|
1252 | # flip the value in the first line, as the '# dbg' marker can be | |
1247 | # automatically detected and is used elsewhere). |
|
1253 | # automatically detected and is used elsewhere). | |
1248 | DEBUG = False |
|
1254 | DEBUG = False | |
1249 | #DEBUG = True # dbg |
|
1255 | #DEBUG = True # dbg | |
1250 | if DEBUG: |
|
1256 | if DEBUG: | |
1251 | try: |
|
1257 | try: | |
1252 | self.complete(text, line_buffer, cursor_pos) |
|
1258 | self.complete(text, line_buffer, cursor_pos) | |
1253 | except: |
|
1259 | except: | |
1254 | import traceback; traceback.print_exc() |
|
1260 | import traceback; traceback.print_exc() | |
1255 | else: |
|
1261 | else: | |
1256 | # The normal production version is here |
|
1262 | # The normal production version is here | |
1257 |
|
1263 | |||
1258 | # This method computes the self.matches array |
|
1264 | # This method computes the self.matches array | |
1259 | self.complete(text, line_buffer, cursor_pos) |
|
1265 | self.complete(text, line_buffer, cursor_pos) | |
1260 |
|
1266 | |||
1261 | try: |
|
1267 | try: | |
1262 | return self.matches[state] |
|
1268 | return self.matches[state] | |
1263 | except IndexError: |
|
1269 | except IndexError: | |
1264 | return None |
|
1270 | return None | |
1265 |
|
1271 |
@@ -1,602 +1,577 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 | """ |
|
2 | """ | |
3 | Pdb debugger class. |
|
3 | Pdb debugger class. | |
4 |
|
4 | |||
5 | Modified from the standard pdb.Pdb class to avoid including readline, so that |
|
5 | Modified from the standard pdb.Pdb class to avoid including readline, so that | |
6 | the command line completion of other programs which include this isn't |
|
6 | the command line completion of other programs which include this isn't | |
7 | damaged. |
|
7 | damaged. | |
8 |
|
8 | |||
9 | In the future, this class will be expanded with improvements over the standard |
|
9 | In the future, this class will be expanded with improvements over the standard | |
10 | pdb. |
|
10 | pdb. | |
11 |
|
11 | |||
12 | The code in this file is mainly lifted out of cmd.py in Python 2.2, with minor |
|
12 | The code in this file is mainly lifted out of cmd.py in Python 2.2, with minor | |
13 | changes. Licensing should therefore be under the standard Python terms. For |
|
13 | changes. Licensing should therefore be under the standard Python terms. For | |
14 | details on the PSF (Python Software Foundation) standard license, see: |
|
14 | details on the PSF (Python Software Foundation) standard license, see: | |
15 |
|
15 | |||
16 | http://www.python.org/2.2.3/license.html""" |
|
16 | http://www.python.org/2.2.3/license.html""" | |
17 |
|
17 | |||
18 | #***************************************************************************** |
|
18 | #***************************************************************************** | |
19 | # |
|
19 | # | |
20 | # This file is licensed under the PSF license. |
|
20 | # This file is licensed under the PSF license. | |
21 | # |
|
21 | # | |
22 | # Copyright (C) 2001 Python Software Foundation, www.python.org |
|
22 | # Copyright (C) 2001 Python Software Foundation, www.python.org | |
23 | # Copyright (C) 2005-2006 Fernando Perez. <fperez@colorado.edu> |
|
23 | # Copyright (C) 2005-2006 Fernando Perez. <fperez@colorado.edu> | |
24 | # |
|
24 | # | |
25 | # |
|
25 | # | |
26 | #***************************************************************************** |
|
26 | #***************************************************************************** | |
27 | from __future__ import print_function |
|
27 | from __future__ import print_function | |
28 |
|
28 | |||
29 | import bdb |
|
29 | import bdb | |
30 | import functools |
|
30 | import functools | |
31 | import inspect |
|
31 | import inspect | |
32 | import sys |
|
32 | import sys | |
33 |
|
33 | |||
34 | from IPython import get_ipython |
|
34 | from IPython import get_ipython | |
35 | from IPython.utils import PyColorize, ulinecache |
|
35 | from IPython.utils import PyColorize, ulinecache | |
36 | from IPython.utils import coloransi, py3compat |
|
36 | from IPython.utils import coloransi, py3compat | |
37 | from IPython.core.excolors import exception_colors |
|
37 | from IPython.core.excolors import exception_colors | |
38 | from IPython.testing.skipdoctest import skip_doctest |
|
38 | from IPython.testing.skipdoctest import skip_doctest | |
39 |
|
39 | |||
|
40 | ||||
40 | prompt = 'ipdb> ' |
|
41 | prompt = 'ipdb> ' | |
41 |
|
42 | |||
42 | #We have to check this directly from sys.argv, config struct not yet available |
|
43 | #We have to check this directly from sys.argv, config struct not yet available | |
43 | from pdb import Pdb as OldPdb |
|
44 | from pdb import Pdb as OldPdb | |
44 |
|
45 | |||
45 | # Allow the set_trace code to operate outside of an ipython instance, even if |
|
46 | # Allow the set_trace code to operate outside of an ipython instance, even if | |
46 | # it does so with some limitations. The rest of this support is implemented in |
|
47 | # it does so with some limitations. The rest of this support is implemented in | |
47 | # the Tracer constructor. |
|
48 | # the Tracer constructor. | |
48 |
|
49 | |||
49 | def make_arrow(pad): |
|
50 | def make_arrow(pad): | |
50 | """generate the leading arrow in front of traceback or debugger""" |
|
51 | """generate the leading arrow in front of traceback or debugger""" | |
51 | if pad >= 2: |
|
52 | if pad >= 2: | |
52 | return '-'*(pad-2) + '> ' |
|
53 | return '-'*(pad-2) + '> ' | |
53 | elif pad == 1: |
|
54 | elif pad == 1: | |
54 | return '>' |
|
55 | return '>' | |
55 | return '' |
|
56 | return '' | |
56 |
|
57 | |||
57 |
|
58 | |||
58 | def BdbQuit_excepthook(et, ev, tb, excepthook=None): |
|
59 | def BdbQuit_excepthook(et, ev, tb, excepthook=None): | |
59 | """Exception hook which handles `BdbQuit` exceptions. |
|
60 | """Exception hook which handles `BdbQuit` exceptions. | |
60 |
|
61 | |||
61 | All other exceptions are processed using the `excepthook` |
|
62 | All other exceptions are processed using the `excepthook` | |
62 | parameter. |
|
63 | parameter. | |
63 | """ |
|
64 | """ | |
64 | if et==bdb.BdbQuit: |
|
65 | if et==bdb.BdbQuit: | |
65 | print('Exiting Debugger.') |
|
66 | print('Exiting Debugger.') | |
66 | elif excepthook is not None: |
|
67 | elif excepthook is not None: | |
67 | excepthook(et, ev, tb) |
|
68 | excepthook(et, ev, tb) | |
68 | else: |
|
69 | else: | |
69 | # Backwards compatibility. Raise deprecation warning? |
|
70 | # Backwards compatibility. Raise deprecation warning? | |
70 | BdbQuit_excepthook.excepthook_ori(et,ev,tb) |
|
71 | BdbQuit_excepthook.excepthook_ori(et,ev,tb) | |
71 |
|
72 | |||
72 | def BdbQuit_IPython_excepthook(self,et,ev,tb,tb_offset=None): |
|
73 | def BdbQuit_IPython_excepthook(self,et,ev,tb,tb_offset=None): | |
73 | print('Exiting Debugger.') |
|
74 | print('Exiting Debugger.') | |
74 |
|
75 | |||
75 |
|
76 | |||
76 | class Tracer(object): |
|
77 | class Tracer(object): | |
77 | """Class for local debugging, similar to pdb.set_trace. |
|
78 | """Class for local debugging, similar to pdb.set_trace. | |
78 |
|
79 | |||
79 | Instances of this class, when called, behave like pdb.set_trace, but |
|
80 | Instances of this class, when called, behave like pdb.set_trace, but | |
80 | providing IPython's enhanced capabilities. |
|
81 | providing IPython's enhanced capabilities. | |
81 |
|
82 | |||
82 | This is implemented as a class which must be initialized in your own code |
|
83 | This is implemented as a class which must be initialized in your own code | |
83 | and not as a standalone function because we need to detect at runtime |
|
84 | and not as a standalone function because we need to detect at runtime | |
84 | whether IPython is already active or not. That detection is done in the |
|
85 | whether IPython is already active or not. That detection is done in the | |
85 | constructor, ensuring that this code plays nicely with a running IPython, |
|
86 | constructor, ensuring that this code plays nicely with a running IPython, | |
86 | while functioning acceptably (though with limitations) if outside of it. |
|
87 | while functioning acceptably (though with limitations) if outside of it. | |
87 | """ |
|
88 | """ | |
88 |
|
89 | |||
89 | @skip_doctest |
|
90 | @skip_doctest | |
90 | def __init__(self, colors=None): |
|
91 | def __init__(self, colors=None): | |
91 | """Create a local debugger instance. |
|
92 | """Create a local debugger instance. | |
92 |
|
93 | |||
93 | Parameters |
|
94 | Parameters | |
94 | ---------- |
|
95 | ---------- | |
95 |
|
96 | |||
96 | colors : str, optional |
|
97 | colors : str, optional | |
97 | The name of the color scheme to use, it must be one of IPython's |
|
98 | The name of the color scheme to use, it must be one of IPython's | |
98 | valid color schemes. If not given, the function will default to |
|
99 | valid color schemes. If not given, the function will default to | |
99 | the current IPython scheme when running inside IPython, and to |
|
100 | the current IPython scheme when running inside IPython, and to | |
100 | 'NoColor' otherwise. |
|
101 | 'NoColor' otherwise. | |
101 |
|
102 | |||
102 | Examples |
|
103 | Examples | |
103 | -------- |
|
104 | -------- | |
104 | :: |
|
105 | :: | |
105 |
|
106 | |||
106 | from IPython.core.debugger import Tracer; debug_here = Tracer() |
|
107 | from IPython.core.debugger import Tracer; debug_here = Tracer() | |
107 |
|
108 | |||
108 | Later in your code:: |
|
109 | Later in your code:: | |
109 |
|
110 | |||
110 | debug_here() # -> will open up the debugger at that point. |
|
111 | debug_here() # -> will open up the debugger at that point. | |
111 |
|
112 | |||
112 | Once the debugger activates, you can use all of its regular commands to |
|
113 | Once the debugger activates, you can use all of its regular commands to | |
113 | step through code, set breakpoints, etc. See the pdb documentation |
|
114 | step through code, set breakpoints, etc. See the pdb documentation | |
114 | from the Python standard library for usage details. |
|
115 | from the Python standard library for usage details. | |
115 | """ |
|
116 | """ | |
116 |
|
117 | |||
117 | ip = get_ipython() |
|
118 | ip = get_ipython() | |
118 | if ip is None: |
|
119 | if ip is None: | |
119 | # Outside of ipython, we set our own exception hook manually |
|
120 | # Outside of ipython, we set our own exception hook manually | |
120 | sys.excepthook = functools.partial(BdbQuit_excepthook, |
|
121 | sys.excepthook = functools.partial(BdbQuit_excepthook, | |
121 | excepthook=sys.excepthook) |
|
122 | excepthook=sys.excepthook) | |
122 | def_colors = 'NoColor' |
|
123 | def_colors = 'NoColor' | |
123 | try: |
|
|||
124 | # Limited tab completion support |
|
|||
125 | import readline |
|
|||
126 | readline.parse_and_bind('tab: complete') |
|
|||
127 | except ImportError: |
|
|||
128 | pass |
|
|||
129 | else: |
|
124 | else: | |
130 | # In ipython, we use its custom exception handler mechanism |
|
125 | # In ipython, we use its custom exception handler mechanism | |
131 | def_colors = ip.colors |
|
126 | def_colors = ip.colors | |
132 | ip.set_custom_exc((bdb.BdbQuit,), BdbQuit_IPython_excepthook) |
|
127 | ip.set_custom_exc((bdb.BdbQuit,), BdbQuit_IPython_excepthook) | |
133 |
|
128 | |||
134 | if colors is None: |
|
129 | if colors is None: | |
135 | colors = def_colors |
|
130 | colors = def_colors | |
136 |
|
131 | |||
137 | # The stdlib debugger internally uses a modified repr from the `repr` |
|
132 | # The stdlib debugger internally uses a modified repr from the `repr` | |
138 | # module, that limits the length of printed strings to a hardcoded |
|
133 | # module, that limits the length of printed strings to a hardcoded | |
139 | # limit of 30 characters. That much trimming is too aggressive, let's |
|
134 | # limit of 30 characters. That much trimming is too aggressive, let's | |
140 | # at least raise that limit to 80 chars, which should be enough for |
|
135 | # at least raise that limit to 80 chars, which should be enough for | |
141 | # most interactive uses. |
|
136 | # most interactive uses. | |
142 | try: |
|
137 | try: | |
143 | try: |
|
138 | try: | |
144 | from reprlib import aRepr # Py 3 |
|
139 | from reprlib import aRepr # Py 3 | |
145 | except ImportError: |
|
140 | except ImportError: | |
146 | from repr import aRepr # Py 2 |
|
141 | from repr import aRepr # Py 2 | |
147 | aRepr.maxstring = 80 |
|
142 | aRepr.maxstring = 80 | |
148 | except: |
|
143 | except: | |
149 | # This is only a user-facing convenience, so any error we encounter |
|
144 | # This is only a user-facing convenience, so any error we encounter | |
150 | # here can be warned about but can be otherwise ignored. These |
|
145 | # here can be warned about but can be otherwise ignored. These | |
151 | # printouts will tell us about problems if this API changes |
|
146 | # printouts will tell us about problems if this API changes | |
152 | import traceback |
|
147 | import traceback | |
153 | traceback.print_exc() |
|
148 | traceback.print_exc() | |
154 |
|
149 | |||
155 | self.debugger = Pdb(colors) |
|
150 | self.debugger = Pdb(colors) | |
156 |
|
151 | |||
157 | def __call__(self): |
|
152 | def __call__(self): | |
158 | """Starts an interactive debugger at the point where called. |
|
153 | """Starts an interactive debugger at the point where called. | |
159 |
|
154 | |||
160 | This is similar to the pdb.set_trace() function from the std lib, but |
|
155 | This is similar to the pdb.set_trace() function from the std lib, but | |
161 | using IPython's enhanced debugger.""" |
|
156 | using IPython's enhanced debugger.""" | |
162 |
|
157 | |||
163 | self.debugger.set_trace(sys._getframe().f_back) |
|
158 | self.debugger.set_trace(sys._getframe().f_back) | |
164 |
|
159 | |||
165 |
|
160 | |||
166 | def decorate_fn_with_doc(new_fn, old_fn, additional_text=""): |
|
161 | def decorate_fn_with_doc(new_fn, old_fn, additional_text=""): | |
167 | """Make new_fn have old_fn's doc string. This is particularly useful |
|
162 | """Make new_fn have old_fn's doc string. This is particularly useful | |
168 | for the ``do_...`` commands that hook into the help system. |
|
163 | for the ``do_...`` commands that hook into the help system. | |
169 | Adapted from from a comp.lang.python posting |
|
164 | Adapted from from a comp.lang.python posting | |
170 | by Duncan Booth.""" |
|
165 | by Duncan Booth.""" | |
171 | def wrapper(*args, **kw): |
|
166 | def wrapper(*args, **kw): | |
172 | return new_fn(*args, **kw) |
|
167 | return new_fn(*args, **kw) | |
173 | if old_fn.__doc__: |
|
168 | if old_fn.__doc__: | |
174 | wrapper.__doc__ = old_fn.__doc__ + additional_text |
|
169 | wrapper.__doc__ = old_fn.__doc__ + additional_text | |
175 | return wrapper |
|
170 | return wrapper | |
176 |
|
171 | |||
177 |
|
172 | |||
178 | def _file_lines(fname): |
|
173 | def _file_lines(fname): | |
179 | """Return the contents of a named file as a list of lines. |
|
174 | """Return the contents of a named file as a list of lines. | |
180 |
|
175 | |||
181 | This function never raises an IOError exception: if the file can't be |
|
176 | This function never raises an IOError exception: if the file can't be | |
182 | read, it simply returns an empty list.""" |
|
177 | read, it simply returns an empty list.""" | |
183 |
|
178 | |||
184 | try: |
|
179 | try: | |
185 | outfile = open(fname) |
|
180 | outfile = open(fname) | |
186 | except IOError: |
|
181 | except IOError: | |
187 | return [] |
|
182 | return [] | |
188 | else: |
|
183 | else: | |
189 | out = outfile.readlines() |
|
184 | out = outfile.readlines() | |
190 | outfile.close() |
|
185 | outfile.close() | |
191 | return out |
|
186 | return out | |
192 |
|
187 | |||
193 |
|
188 | |||
194 | class Pdb(OldPdb, object): |
|
189 | class Pdb(OldPdb, object): | |
195 | """Modified Pdb class, does not load readline.""" |
|
190 | """Modified Pdb class, does not load readline.""" | |
196 |
|
191 | |||
197 | def __init__(self,color_scheme='NoColor',completekey=None, |
|
192 | def __init__(self,color_scheme='NoColor',completekey=None, | |
198 | stdin=None, stdout=None, context=5): |
|
193 | stdin=None, stdout=None, context=5): | |
199 |
|
194 | |||
200 | # Parent constructor: |
|
195 | # Parent constructor: | |
201 | try: |
|
196 | try: | |
202 | self.context=int(context) |
|
197 | self.context=int(context) | |
203 | if self.context <= 0: |
|
198 | if self.context <= 0: | |
204 | raise ValueError("Context must be a positive integer") |
|
199 | raise ValueError("Context must be a positive integer") | |
205 | except (TypeError, ValueError): |
|
200 | except (TypeError, ValueError): | |
206 | raise ValueError("Context must be a positive integer") |
|
201 | raise ValueError("Context must be a positive integer") | |
207 |
|
202 | |||
208 | OldPdb.__init__(self,completekey,stdin,stdout) |
|
203 | OldPdb.__init__(self, completekey, stdin, stdout) | |
209 |
|
204 | |||
210 | # IPython changes... |
|
205 | # IPython changes... | |
211 | self.shell = get_ipython() |
|
206 | self.shell = get_ipython() | |
212 |
|
207 | |||
213 | if self.shell is None: |
|
208 | if self.shell is None: | |
214 | # No IPython instance running, we must create one |
|
209 | # No IPython instance running, we must create one | |
215 | from IPython.terminal.interactiveshell import \ |
|
210 | from IPython.terminal.interactiveshell import \ | |
216 | TerminalInteractiveShell |
|
211 | TerminalInteractiveShell | |
217 | self.shell = TerminalInteractiveShell.instance() |
|
212 | self.shell = TerminalInteractiveShell.instance() | |
218 |
|
213 | |||
219 | self.aliases = {} |
|
214 | self.aliases = {} | |
220 |
|
215 | |||
221 | # Create color table: we copy the default one from the traceback |
|
216 | # Create color table: we copy the default one from the traceback | |
222 | # module and add a few attributes needed for debugging |
|
217 | # module and add a few attributes needed for debugging | |
223 | self.color_scheme_table = exception_colors() |
|
218 | self.color_scheme_table = exception_colors() | |
224 |
|
219 | |||
225 | # shorthands |
|
220 | # shorthands | |
226 | C = coloransi.TermColors |
|
221 | C = coloransi.TermColors | |
227 | cst = self.color_scheme_table |
|
222 | cst = self.color_scheme_table | |
228 |
|
223 | |||
229 | cst['NoColor'].colors.prompt = C.NoColor |
|
224 | cst['NoColor'].colors.prompt = C.NoColor | |
230 | cst['NoColor'].colors.breakpoint_enabled = C.NoColor |
|
225 | cst['NoColor'].colors.breakpoint_enabled = C.NoColor | |
231 | cst['NoColor'].colors.breakpoint_disabled = C.NoColor |
|
226 | cst['NoColor'].colors.breakpoint_disabled = C.NoColor | |
232 |
|
227 | |||
233 | cst['Linux'].colors.prompt = C.Green |
|
228 | cst['Linux'].colors.prompt = C.Green | |
234 | cst['Linux'].colors.breakpoint_enabled = C.LightRed |
|
229 | cst['Linux'].colors.breakpoint_enabled = C.LightRed | |
235 | cst['Linux'].colors.breakpoint_disabled = C.Red |
|
230 | cst['Linux'].colors.breakpoint_disabled = C.Red | |
236 |
|
231 | |||
237 | cst['LightBG'].colors.prompt = C.Blue |
|
232 | cst['LightBG'].colors.prompt = C.Blue | |
238 | cst['LightBG'].colors.breakpoint_enabled = C.LightRed |
|
233 | cst['LightBG'].colors.breakpoint_enabled = C.LightRed | |
239 | cst['LightBG'].colors.breakpoint_disabled = C.Red |
|
234 | cst['LightBG'].colors.breakpoint_disabled = C.Red | |
240 |
|
235 | |||
241 | self.set_colors(color_scheme) |
|
236 | self.set_colors(color_scheme) | |
242 |
|
237 | |||
243 | # Add a python parser so we can syntax highlight source while |
|
238 | # Add a python parser so we can syntax highlight source while | |
244 | # debugging. |
|
239 | # debugging. | |
245 | self.parser = PyColorize.Parser() |
|
240 | self.parser = PyColorize.Parser() | |
246 |
|
241 | |||
247 | # Set the prompt - the default prompt is '(Pdb)' |
|
242 | # Set the prompt - the default prompt is '(Pdb)' | |
248 | Colors = cst.active_colors |
|
243 | self.prompt = prompt | |
249 | if color_scheme == 'NoColor': |
|
|||
250 | self.prompt = prompt |
|
|||
251 | else: |
|
|||
252 | # The colour markers are wrapped by bytes 01 and 02 so that readline |
|
|||
253 | # can calculate the width. |
|
|||
254 | self.prompt = u'\x01%s\x02%s\x01%s\x02' % (Colors.prompt, prompt, Colors.Normal) |
|
|||
255 |
|
244 | |||
256 | def set_colors(self, scheme): |
|
245 | def set_colors(self, scheme): | |
257 | """Shorthand access to the color table scheme selector method.""" |
|
246 | """Shorthand access to the color table scheme selector method.""" | |
258 | self.color_scheme_table.set_active_scheme(scheme) |
|
247 | self.color_scheme_table.set_active_scheme(scheme) | |
259 |
|
248 | |||
260 | def interaction(self, frame, traceback): |
|
249 | def interaction(self, frame, traceback): | |
261 | self.shell.set_completer_frame(frame) |
|
250 | try: | |
262 | while True: |
|
251 | OldPdb.interaction(self, frame, traceback) | |
263 | try: |
|
252 | except KeyboardInterrupt: | |
264 | OldPdb.interaction(self, frame, traceback) |
|
253 | sys.stdout.write('\n' + self.shell.get_exception_only()) | |
265 | break |
|
|||
266 | except KeyboardInterrupt: |
|
|||
267 | self.shell.write('\n' + self.shell.get_exception_only()) |
|
|||
268 | break |
|
|||
269 | finally: |
|
|||
270 | # Pdb sets readline delimiters, so set them back to our own |
|
|||
271 | if self.shell.readline is not None: |
|
|||
272 | self.shell.readline.set_completer_delims(self.shell.readline_delims) |
|
|||
273 |
|
254 | |||
274 | def parseline(self, line): |
|
255 | def parseline(self, line): | |
275 | if line.startswith("!!"): |
|
256 | if line.startswith("!!"): | |
276 | # Force standard behavior. |
|
257 | # Force standard behavior. | |
277 | return super(Pdb, self).parseline(line[2:]) |
|
258 | return super(Pdb, self).parseline(line[2:]) | |
278 | # "Smart command mode" from pdb++: don't execute commands if a variable |
|
259 | # "Smart command mode" from pdb++: don't execute commands if a variable | |
279 | # with the same name exists. |
|
260 | # with the same name exists. | |
280 | cmd, arg, newline = super(Pdb, self).parseline(line) |
|
261 | cmd, arg, newline = super(Pdb, self).parseline(line) | |
281 | if cmd in self.curframe.f_globals or cmd in self.curframe.f_locals: |
|
262 | if cmd in self.curframe.f_globals or cmd in self.curframe.f_locals: | |
282 | return super(Pdb, self).parseline("!" + line) |
|
263 | return super(Pdb, self).parseline("!" + line) | |
283 | return super(Pdb, self).parseline(line) |
|
264 | return super(Pdb, self).parseline(line) | |
284 |
|
265 | |||
285 | def new_do_up(self, arg): |
|
266 | def new_do_up(self, arg): | |
286 | OldPdb.do_up(self, arg) |
|
267 | OldPdb.do_up(self, arg) | |
287 | self.shell.set_completer_frame(self.curframe) |
|
|||
288 | do_u = do_up = decorate_fn_with_doc(new_do_up, OldPdb.do_up) |
|
268 | do_u = do_up = decorate_fn_with_doc(new_do_up, OldPdb.do_up) | |
289 |
|
269 | |||
290 | def new_do_down(self, arg): |
|
270 | def new_do_down(self, arg): | |
291 | OldPdb.do_down(self, arg) |
|
271 | OldPdb.do_down(self, arg) | |
292 | self.shell.set_completer_frame(self.curframe) |
|
|||
293 |
|
272 | |||
294 | do_d = do_down = decorate_fn_with_doc(new_do_down, OldPdb.do_down) |
|
273 | do_d = do_down = decorate_fn_with_doc(new_do_down, OldPdb.do_down) | |
295 |
|
274 | |||
296 | def new_do_frame(self, arg): |
|
275 | def new_do_frame(self, arg): | |
297 | OldPdb.do_frame(self, arg) |
|
276 | OldPdb.do_frame(self, arg) | |
298 | self.shell.set_completer_frame(self.curframe) |
|
|||
299 |
|
277 | |||
300 | def new_do_quit(self, arg): |
|
278 | def new_do_quit(self, arg): | |
301 |
|
279 | |||
302 | if hasattr(self, 'old_all_completions'): |
|
280 | if hasattr(self, 'old_all_completions'): | |
303 | self.shell.Completer.all_completions=self.old_all_completions |
|
281 | self.shell.Completer.all_completions=self.old_all_completions | |
304 |
|
282 | |||
305 | return OldPdb.do_quit(self, arg) |
|
283 | return OldPdb.do_quit(self, arg) | |
306 |
|
284 | |||
307 | do_q = do_quit = decorate_fn_with_doc(new_do_quit, OldPdb.do_quit) |
|
285 | do_q = do_quit = decorate_fn_with_doc(new_do_quit, OldPdb.do_quit) | |
308 |
|
286 | |||
309 | def new_do_restart(self, arg): |
|
287 | def new_do_restart(self, arg): | |
310 | """Restart command. In the context of ipython this is exactly the same |
|
288 | """Restart command. In the context of ipython this is exactly the same | |
311 | thing as 'quit'.""" |
|
289 | thing as 'quit'.""" | |
312 | self.msg("Restart doesn't make sense here. Using 'quit' instead.") |
|
290 | self.msg("Restart doesn't make sense here. Using 'quit' instead.") | |
313 | return self.do_quit(arg) |
|
291 | return self.do_quit(arg) | |
314 |
|
292 | |||
315 | def postloop(self): |
|
|||
316 | self.shell.set_completer_frame(None) |
|
|||
317 |
|
||||
318 | def print_stack_trace(self, context=None): |
|
293 | def print_stack_trace(self, context=None): | |
319 | if context is None: |
|
294 | if context is None: | |
320 | context = self.context |
|
295 | context = self.context | |
321 | try: |
|
296 | try: | |
322 | context=int(context) |
|
297 | context=int(context) | |
323 | if context <= 0: |
|
298 | if context <= 0: | |
324 | raise ValueError("Context must be a positive integer") |
|
299 | raise ValueError("Context must be a positive integer") | |
325 | except (TypeError, ValueError): |
|
300 | except (TypeError, ValueError): | |
326 | raise ValueError("Context must be a positive integer") |
|
301 | raise ValueError("Context must be a positive integer") | |
327 | try: |
|
302 | try: | |
328 | for frame_lineno in self.stack: |
|
303 | for frame_lineno in self.stack: | |
329 | self.print_stack_entry(frame_lineno, context=context) |
|
304 | self.print_stack_entry(frame_lineno, context=context) | |
330 | except KeyboardInterrupt: |
|
305 | except KeyboardInterrupt: | |
331 | pass |
|
306 | pass | |
332 |
|
307 | |||
333 | def print_stack_entry(self,frame_lineno,prompt_prefix='\n-> ', |
|
308 | def print_stack_entry(self,frame_lineno,prompt_prefix='\n-> ', | |
334 | context=None): |
|
309 | context=None): | |
335 | if context is None: |
|
310 | if context is None: | |
336 | context = self.context |
|
311 | context = self.context | |
337 | try: |
|
312 | try: | |
338 | context=int(context) |
|
313 | context=int(context) | |
339 | if context <= 0: |
|
314 | if context <= 0: | |
340 | raise ValueError("Context must be a positive integer") |
|
315 | raise ValueError("Context must be a positive integer") | |
341 | except (TypeError, ValueError): |
|
316 | except (TypeError, ValueError): | |
342 | raise ValueError("Context must be a positive integer") |
|
317 | raise ValueError("Context must be a positive integer") | |
343 | print(self.format_stack_entry(frame_lineno, '', context)) |
|
318 | print(self.format_stack_entry(frame_lineno, '', context)) | |
344 |
|
319 | |||
345 | # vds: >> |
|
320 | # vds: >> | |
346 | frame, lineno = frame_lineno |
|
321 | frame, lineno = frame_lineno | |
347 | filename = frame.f_code.co_filename |
|
322 | filename = frame.f_code.co_filename | |
348 | self.shell.hooks.synchronize_with_editor(filename, lineno, 0) |
|
323 | self.shell.hooks.synchronize_with_editor(filename, lineno, 0) | |
349 | # vds: << |
|
324 | # vds: << | |
350 |
|
325 | |||
351 | def format_stack_entry(self, frame_lineno, lprefix=': ', context=None): |
|
326 | def format_stack_entry(self, frame_lineno, lprefix=': ', context=None): | |
352 | if context is None: |
|
327 | if context is None: | |
353 | context = self.context |
|
328 | context = self.context | |
354 | try: |
|
329 | try: | |
355 | context=int(context) |
|
330 | context=int(context) | |
356 | if context <= 0: |
|
331 | if context <= 0: | |
357 | print("Context must be a positive integer") |
|
332 | print("Context must be a positive integer") | |
358 | except (TypeError, ValueError): |
|
333 | except (TypeError, ValueError): | |
359 | print("Context must be a positive integer") |
|
334 | print("Context must be a positive integer") | |
360 | try: |
|
335 | try: | |
361 | import reprlib # Py 3 |
|
336 | import reprlib # Py 3 | |
362 | except ImportError: |
|
337 | except ImportError: | |
363 | import repr as reprlib # Py 2 |
|
338 | import repr as reprlib # Py 2 | |
364 |
|
339 | |||
365 | ret = [] |
|
340 | ret = [] | |
366 |
|
341 | |||
367 | Colors = self.color_scheme_table.active_colors |
|
342 | Colors = self.color_scheme_table.active_colors | |
368 | ColorsNormal = Colors.Normal |
|
343 | ColorsNormal = Colors.Normal | |
369 | tpl_link = u'%s%%s%s' % (Colors.filenameEm, ColorsNormal) |
|
344 | tpl_link = u'%s%%s%s' % (Colors.filenameEm, ColorsNormal) | |
370 | tpl_call = u'%s%%s%s%%s%s' % (Colors.vName, Colors.valEm, ColorsNormal) |
|
345 | tpl_call = u'%s%%s%s%%s%s' % (Colors.vName, Colors.valEm, ColorsNormal) | |
371 | tpl_line = u'%%s%s%%s %s%%s' % (Colors.lineno, ColorsNormal) |
|
346 | tpl_line = u'%%s%s%%s %s%%s' % (Colors.lineno, ColorsNormal) | |
372 | tpl_line_em = u'%%s%s%%s %s%%s%s' % (Colors.linenoEm, Colors.line, |
|
347 | tpl_line_em = u'%%s%s%%s %s%%s%s' % (Colors.linenoEm, Colors.line, | |
373 | ColorsNormal) |
|
348 | ColorsNormal) | |
374 |
|
349 | |||
375 | frame, lineno = frame_lineno |
|
350 | frame, lineno = frame_lineno | |
376 |
|
351 | |||
377 | return_value = '' |
|
352 | return_value = '' | |
378 | if '__return__' in frame.f_locals: |
|
353 | if '__return__' in frame.f_locals: | |
379 | rv = frame.f_locals['__return__'] |
|
354 | rv = frame.f_locals['__return__'] | |
380 | #return_value += '->' |
|
355 | #return_value += '->' | |
381 | return_value += reprlib.repr(rv) + '\n' |
|
356 | return_value += reprlib.repr(rv) + '\n' | |
382 | ret.append(return_value) |
|
357 | ret.append(return_value) | |
383 |
|
358 | |||
384 | #s = filename + '(' + `lineno` + ')' |
|
359 | #s = filename + '(' + `lineno` + ')' | |
385 | filename = self.canonic(frame.f_code.co_filename) |
|
360 | filename = self.canonic(frame.f_code.co_filename) | |
386 | link = tpl_link % py3compat.cast_unicode(filename) |
|
361 | link = tpl_link % py3compat.cast_unicode(filename) | |
387 |
|
362 | |||
388 | if frame.f_code.co_name: |
|
363 | if frame.f_code.co_name: | |
389 | func = frame.f_code.co_name |
|
364 | func = frame.f_code.co_name | |
390 | else: |
|
365 | else: | |
391 | func = "<lambda>" |
|
366 | func = "<lambda>" | |
392 |
|
367 | |||
393 | call = '' |
|
368 | call = '' | |
394 | if func != '?': |
|
369 | if func != '?': | |
395 | if '__args__' in frame.f_locals: |
|
370 | if '__args__' in frame.f_locals: | |
396 | args = reprlib.repr(frame.f_locals['__args__']) |
|
371 | args = reprlib.repr(frame.f_locals['__args__']) | |
397 | else: |
|
372 | else: | |
398 | args = '()' |
|
373 | args = '()' | |
399 | call = tpl_call % (func, args) |
|
374 | call = tpl_call % (func, args) | |
400 |
|
375 | |||
401 | # The level info should be generated in the same format pdb uses, to |
|
376 | # The level info should be generated in the same format pdb uses, to | |
402 | # avoid breaking the pdbtrack functionality of python-mode in *emacs. |
|
377 | # avoid breaking the pdbtrack functionality of python-mode in *emacs. | |
403 | if frame is self.curframe: |
|
378 | if frame is self.curframe: | |
404 | ret.append('> ') |
|
379 | ret.append('> ') | |
405 | else: |
|
380 | else: | |
406 | ret.append(' ') |
|
381 | ret.append(' ') | |
407 | ret.append(u'%s(%s)%s\n' % (link,lineno,call)) |
|
382 | ret.append(u'%s(%s)%s\n' % (link,lineno,call)) | |
408 |
|
383 | |||
409 | start = lineno - 1 - context//2 |
|
384 | start = lineno - 1 - context//2 | |
410 | lines = ulinecache.getlines(filename) |
|
385 | lines = ulinecache.getlines(filename) | |
411 | start = min(start, len(lines) - context) |
|
386 | start = min(start, len(lines) - context) | |
412 | start = max(start, 0) |
|
387 | start = max(start, 0) | |
413 | lines = lines[start : start + context] |
|
388 | lines = lines[start : start + context] | |
414 |
|
389 | |||
415 | for i,line in enumerate(lines): |
|
390 | for i,line in enumerate(lines): | |
416 | show_arrow = (start + 1 + i == lineno) |
|
391 | show_arrow = (start + 1 + i == lineno) | |
417 | linetpl = (frame is self.curframe or show_arrow) \ |
|
392 | linetpl = (frame is self.curframe or show_arrow) \ | |
418 | and tpl_line_em \ |
|
393 | and tpl_line_em \ | |
419 | or tpl_line |
|
394 | or tpl_line | |
420 | ret.append(self.__format_line(linetpl, filename, |
|
395 | ret.append(self.__format_line(linetpl, filename, | |
421 | start + 1 + i, line, |
|
396 | start + 1 + i, line, | |
422 | arrow = show_arrow) ) |
|
397 | arrow = show_arrow) ) | |
423 | return ''.join(ret) |
|
398 | return ''.join(ret) | |
424 |
|
399 | |||
425 | def __format_line(self, tpl_line, filename, lineno, line, arrow = False): |
|
400 | def __format_line(self, tpl_line, filename, lineno, line, arrow = False): | |
426 | bp_mark = "" |
|
401 | bp_mark = "" | |
427 | bp_mark_color = "" |
|
402 | bp_mark_color = "" | |
428 |
|
403 | |||
429 | scheme = self.color_scheme_table.active_scheme_name |
|
404 | scheme = self.color_scheme_table.active_scheme_name | |
430 | new_line, err = self.parser.format2(line, 'str', scheme) |
|
405 | new_line, err = self.parser.format2(line, 'str', scheme) | |
431 | if not err: line = new_line |
|
406 | if not err: line = new_line | |
432 |
|
407 | |||
433 | bp = None |
|
408 | bp = None | |
434 | if lineno in self.get_file_breaks(filename): |
|
409 | if lineno in self.get_file_breaks(filename): | |
435 | bps = self.get_breaks(filename, lineno) |
|
410 | bps = self.get_breaks(filename, lineno) | |
436 | bp = bps[-1] |
|
411 | bp = bps[-1] | |
437 |
|
412 | |||
438 | if bp: |
|
413 | if bp: | |
439 | Colors = self.color_scheme_table.active_colors |
|
414 | Colors = self.color_scheme_table.active_colors | |
440 | bp_mark = str(bp.number) |
|
415 | bp_mark = str(bp.number) | |
441 | bp_mark_color = Colors.breakpoint_enabled |
|
416 | bp_mark_color = Colors.breakpoint_enabled | |
442 | if not bp.enabled: |
|
417 | if not bp.enabled: | |
443 | bp_mark_color = Colors.breakpoint_disabled |
|
418 | bp_mark_color = Colors.breakpoint_disabled | |
444 |
|
419 | |||
445 | numbers_width = 7 |
|
420 | numbers_width = 7 | |
446 | if arrow: |
|
421 | if arrow: | |
447 | # This is the line with the error |
|
422 | # This is the line with the error | |
448 | pad = numbers_width - len(str(lineno)) - len(bp_mark) |
|
423 | pad = numbers_width - len(str(lineno)) - len(bp_mark) | |
449 | num = '%s%s' % (make_arrow(pad), str(lineno)) |
|
424 | num = '%s%s' % (make_arrow(pad), str(lineno)) | |
450 | else: |
|
425 | else: | |
451 | num = '%*s' % (numbers_width - len(bp_mark), str(lineno)) |
|
426 | num = '%*s' % (numbers_width - len(bp_mark), str(lineno)) | |
452 |
|
427 | |||
453 | return tpl_line % (bp_mark_color + bp_mark, num, line) |
|
428 | return tpl_line % (bp_mark_color + bp_mark, num, line) | |
454 |
|
429 | |||
455 |
|
430 | |||
456 | def print_list_lines(self, filename, first, last): |
|
431 | def print_list_lines(self, filename, first, last): | |
457 | """The printing (as opposed to the parsing part of a 'list' |
|
432 | """The printing (as opposed to the parsing part of a 'list' | |
458 | command.""" |
|
433 | command.""" | |
459 | try: |
|
434 | try: | |
460 | Colors = self.color_scheme_table.active_colors |
|
435 | Colors = self.color_scheme_table.active_colors | |
461 | ColorsNormal = Colors.Normal |
|
436 | ColorsNormal = Colors.Normal | |
462 | tpl_line = '%%s%s%%s %s%%s' % (Colors.lineno, ColorsNormal) |
|
437 | tpl_line = '%%s%s%%s %s%%s' % (Colors.lineno, ColorsNormal) | |
463 | tpl_line_em = '%%s%s%%s %s%%s%s' % (Colors.linenoEm, Colors.line, ColorsNormal) |
|
438 | tpl_line_em = '%%s%s%%s %s%%s%s' % (Colors.linenoEm, Colors.line, ColorsNormal) | |
464 | src = [] |
|
439 | src = [] | |
465 | if filename == "<string>" and hasattr(self, "_exec_filename"): |
|
440 | if filename == "<string>" and hasattr(self, "_exec_filename"): | |
466 | filename = self._exec_filename |
|
441 | filename = self._exec_filename | |
467 |
|
442 | |||
468 | for lineno in range(first, last+1): |
|
443 | for lineno in range(first, last+1): | |
469 | line = ulinecache.getline(filename, lineno) |
|
444 | line = ulinecache.getline(filename, lineno) | |
470 | if not line: |
|
445 | if not line: | |
471 | break |
|
446 | break | |
472 |
|
447 | |||
473 | if lineno == self.curframe.f_lineno: |
|
448 | if lineno == self.curframe.f_lineno: | |
474 | line = self.__format_line(tpl_line_em, filename, lineno, line, arrow = True) |
|
449 | line = self.__format_line(tpl_line_em, filename, lineno, line, arrow = True) | |
475 | else: |
|
450 | else: | |
476 | line = self.__format_line(tpl_line, filename, lineno, line, arrow = False) |
|
451 | line = self.__format_line(tpl_line, filename, lineno, line, arrow = False) | |
477 |
|
452 | |||
478 | src.append(line) |
|
453 | src.append(line) | |
479 | self.lineno = lineno |
|
454 | self.lineno = lineno | |
480 |
|
455 | |||
481 | print(''.join(src)) |
|
456 | print(''.join(src)) | |
482 |
|
457 | |||
483 | except KeyboardInterrupt: |
|
458 | except KeyboardInterrupt: | |
484 | pass |
|
459 | pass | |
485 |
|
460 | |||
486 | def do_list(self, arg): |
|
461 | def do_list(self, arg): | |
487 | self.lastcmd = 'list' |
|
462 | self.lastcmd = 'list' | |
488 | last = None |
|
463 | last = None | |
489 | if arg: |
|
464 | if arg: | |
490 | try: |
|
465 | try: | |
491 | x = eval(arg, {}, {}) |
|
466 | x = eval(arg, {}, {}) | |
492 | if type(x) == type(()): |
|
467 | if type(x) == type(()): | |
493 | first, last = x |
|
468 | first, last = x | |
494 | first = int(first) |
|
469 | first = int(first) | |
495 | last = int(last) |
|
470 | last = int(last) | |
496 | if last < first: |
|
471 | if last < first: | |
497 | # Assume it's a count |
|
472 | # Assume it's a count | |
498 | last = first + last |
|
473 | last = first + last | |
499 | else: |
|
474 | else: | |
500 | first = max(1, int(x) - 5) |
|
475 | first = max(1, int(x) - 5) | |
501 | except: |
|
476 | except: | |
502 | print('*** Error in argument:', repr(arg)) |
|
477 | print('*** Error in argument:', repr(arg)) | |
503 | return |
|
478 | return | |
504 | elif self.lineno is None: |
|
479 | elif self.lineno is None: | |
505 | first = max(1, self.curframe.f_lineno - 5) |
|
480 | first = max(1, self.curframe.f_lineno - 5) | |
506 | else: |
|
481 | else: | |
507 | first = self.lineno + 1 |
|
482 | first = self.lineno + 1 | |
508 | if last is None: |
|
483 | if last is None: | |
509 | last = first + 10 |
|
484 | last = first + 10 | |
510 | self.print_list_lines(self.curframe.f_code.co_filename, first, last) |
|
485 | self.print_list_lines(self.curframe.f_code.co_filename, first, last) | |
511 |
|
486 | |||
512 | # vds: >> |
|
487 | # vds: >> | |
513 | lineno = first |
|
488 | lineno = first | |
514 | filename = self.curframe.f_code.co_filename |
|
489 | filename = self.curframe.f_code.co_filename | |
515 | self.shell.hooks.synchronize_with_editor(filename, lineno, 0) |
|
490 | self.shell.hooks.synchronize_with_editor(filename, lineno, 0) | |
516 | # vds: << |
|
491 | # vds: << | |
517 |
|
492 | |||
518 | do_l = do_list |
|
493 | do_l = do_list | |
519 |
|
494 | |||
520 | def getsourcelines(self, obj): |
|
495 | def getsourcelines(self, obj): | |
521 | lines, lineno = inspect.findsource(obj) |
|
496 | lines, lineno = inspect.findsource(obj) | |
522 | if inspect.isframe(obj) and obj.f_globals is obj.f_locals: |
|
497 | if inspect.isframe(obj) and obj.f_globals is obj.f_locals: | |
523 | # must be a module frame: do not try to cut a block out of it |
|
498 | # must be a module frame: do not try to cut a block out of it | |
524 | return lines, 1 |
|
499 | return lines, 1 | |
525 | elif inspect.ismodule(obj): |
|
500 | elif inspect.ismodule(obj): | |
526 | return lines, 1 |
|
501 | return lines, 1 | |
527 | return inspect.getblock(lines[lineno:]), lineno+1 |
|
502 | return inspect.getblock(lines[lineno:]), lineno+1 | |
528 |
|
503 | |||
529 | def do_longlist(self, arg): |
|
504 | def do_longlist(self, arg): | |
530 | self.lastcmd = 'longlist' |
|
505 | self.lastcmd = 'longlist' | |
531 | try: |
|
506 | try: | |
532 | lines, lineno = self.getsourcelines(self.curframe) |
|
507 | lines, lineno = self.getsourcelines(self.curframe) | |
533 | except OSError as err: |
|
508 | except OSError as err: | |
534 | self.error(err) |
|
509 | self.error(err) | |
535 | return |
|
510 | return | |
536 | last = lineno + len(lines) |
|
511 | last = lineno + len(lines) | |
537 | self.print_list_lines(self.curframe.f_code.co_filename, lineno, last) |
|
512 | self.print_list_lines(self.curframe.f_code.co_filename, lineno, last) | |
538 | do_ll = do_longlist |
|
513 | do_ll = do_longlist | |
539 |
|
514 | |||
540 | def do_pdef(self, arg): |
|
515 | def do_pdef(self, arg): | |
541 | """Print the call signature for any callable object. |
|
516 | """Print the call signature for any callable object. | |
542 |
|
517 | |||
543 | The debugger interface to %pdef""" |
|
518 | The debugger interface to %pdef""" | |
544 | namespaces = [('Locals', self.curframe.f_locals), |
|
519 | namespaces = [('Locals', self.curframe.f_locals), | |
545 | ('Globals', self.curframe.f_globals)] |
|
520 | ('Globals', self.curframe.f_globals)] | |
546 | self.shell.find_line_magic('pdef')(arg, namespaces=namespaces) |
|
521 | self.shell.find_line_magic('pdef')(arg, namespaces=namespaces) | |
547 |
|
522 | |||
548 | def do_pdoc(self, arg): |
|
523 | def do_pdoc(self, arg): | |
549 | """Print the docstring for an object. |
|
524 | """Print the docstring for an object. | |
550 |
|
525 | |||
551 | The debugger interface to %pdoc.""" |
|
526 | The debugger interface to %pdoc.""" | |
552 | namespaces = [('Locals', self.curframe.f_locals), |
|
527 | namespaces = [('Locals', self.curframe.f_locals), | |
553 | ('Globals', self.curframe.f_globals)] |
|
528 | ('Globals', self.curframe.f_globals)] | |
554 | self.shell.find_line_magic('pdoc')(arg, namespaces=namespaces) |
|
529 | self.shell.find_line_magic('pdoc')(arg, namespaces=namespaces) | |
555 |
|
530 | |||
556 | def do_pfile(self, arg): |
|
531 | def do_pfile(self, arg): | |
557 | """Print (or run through pager) the file where an object is defined. |
|
532 | """Print (or run through pager) the file where an object is defined. | |
558 |
|
533 | |||
559 | The debugger interface to %pfile. |
|
534 | The debugger interface to %pfile. | |
560 | """ |
|
535 | """ | |
561 | namespaces = [('Locals', self.curframe.f_locals), |
|
536 | namespaces = [('Locals', self.curframe.f_locals), | |
562 | ('Globals', self.curframe.f_globals)] |
|
537 | ('Globals', self.curframe.f_globals)] | |
563 | self.shell.find_line_magic('pfile')(arg, namespaces=namespaces) |
|
538 | self.shell.find_line_magic('pfile')(arg, namespaces=namespaces) | |
564 |
|
539 | |||
565 | def do_pinfo(self, arg): |
|
540 | def do_pinfo(self, arg): | |
566 | """Provide detailed information about an object. |
|
541 | """Provide detailed information about an object. | |
567 |
|
542 | |||
568 | The debugger interface to %pinfo, i.e., obj?.""" |
|
543 | The debugger interface to %pinfo, i.e., obj?.""" | |
569 | namespaces = [('Locals', self.curframe.f_locals), |
|
544 | namespaces = [('Locals', self.curframe.f_locals), | |
570 | ('Globals', self.curframe.f_globals)] |
|
545 | ('Globals', self.curframe.f_globals)] | |
571 | self.shell.find_line_magic('pinfo')(arg, namespaces=namespaces) |
|
546 | self.shell.find_line_magic('pinfo')(arg, namespaces=namespaces) | |
572 |
|
547 | |||
573 | def do_pinfo2(self, arg): |
|
548 | def do_pinfo2(self, arg): | |
574 | """Provide extra detailed information about an object. |
|
549 | """Provide extra detailed information about an object. | |
575 |
|
550 | |||
576 | The debugger interface to %pinfo2, i.e., obj??.""" |
|
551 | The debugger interface to %pinfo2, i.e., obj??.""" | |
577 | namespaces = [('Locals', self.curframe.f_locals), |
|
552 | namespaces = [('Locals', self.curframe.f_locals), | |
578 | ('Globals', self.curframe.f_globals)] |
|
553 | ('Globals', self.curframe.f_globals)] | |
579 | self.shell.find_line_magic('pinfo2')(arg, namespaces=namespaces) |
|
554 | self.shell.find_line_magic('pinfo2')(arg, namespaces=namespaces) | |
580 |
|
555 | |||
581 | def do_psource(self, arg): |
|
556 | def do_psource(self, arg): | |
582 | """Print (or run through pager) the source code for an object.""" |
|
557 | """Print (or run through pager) the source code for an object.""" | |
583 | namespaces = [('Locals', self.curframe.f_locals), |
|
558 | namespaces = [('Locals', self.curframe.f_locals), | |
584 | ('Globals', self.curframe.f_globals)] |
|
559 | ('Globals', self.curframe.f_globals)] | |
585 | self.shell.find_line_magic('psource')(arg, namespaces=namespaces) |
|
560 | self.shell.find_line_magic('psource')(arg, namespaces=namespaces) | |
586 |
|
561 | |||
587 | if sys.version_info > (3, ): |
|
562 | if sys.version_info > (3, ): | |
588 | def do_where(self, arg): |
|
563 | def do_where(self, arg): | |
589 | """w(here) |
|
564 | """w(here) | |
590 | Print a stack trace, with the most recent frame at the bottom. |
|
565 | Print a stack trace, with the most recent frame at the bottom. | |
591 | An arrow indicates the "current frame", which determines the |
|
566 | An arrow indicates the "current frame", which determines the | |
592 | context of most commands. 'bt' is an alias for this command. |
|
567 | context of most commands. 'bt' is an alias for this command. | |
593 |
|
568 | |||
594 | Take a number as argument as an (optional) number of context line to |
|
569 | Take a number as argument as an (optional) number of context line to | |
595 | print""" |
|
570 | print""" | |
596 | if arg: |
|
571 | if arg: | |
597 | context = int(arg) |
|
572 | context = int(arg) | |
598 | self.print_stack_trace(context) |
|
573 | self.print_stack_trace(context) | |
599 | else: |
|
574 | else: | |
600 | self.print_stack_trace() |
|
575 | self.print_stack_trace() | |
601 |
|
576 | |||
602 | do_w = do_where |
|
577 | do_w = do_where |
@@ -1,3240 +1,3244 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 | """Main IPython class.""" |
|
2 | """Main IPython class.""" | |
3 |
|
3 | |||
4 | #----------------------------------------------------------------------------- |
|
4 | #----------------------------------------------------------------------------- | |
5 | # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> |
|
5 | # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> | |
6 | # Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu> |
|
6 | # Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu> | |
7 | # Copyright (C) 2008-2011 The IPython Development Team |
|
7 | # Copyright (C) 2008-2011 The IPython Development Team | |
8 | # |
|
8 | # | |
9 | # Distributed under the terms of the BSD License. The full license is in |
|
9 | # Distributed under the terms of the BSD License. The full license is in | |
10 | # the file COPYING, distributed as part of this software. |
|
10 | # the file COPYING, distributed as part of this software. | |
11 | #----------------------------------------------------------------------------- |
|
11 | #----------------------------------------------------------------------------- | |
12 |
|
12 | |||
13 | from __future__ import absolute_import, print_function |
|
13 | from __future__ import absolute_import, print_function | |
14 |
|
14 | |||
15 | import __future__ |
|
15 | import __future__ | |
16 | import abc |
|
16 | import abc | |
17 | import ast |
|
17 | import ast | |
18 | import atexit |
|
18 | import atexit | |
19 | import functools |
|
19 | import functools | |
20 | import os |
|
20 | import os | |
21 | import re |
|
21 | import re | |
22 | import runpy |
|
22 | import runpy | |
23 | import sys |
|
23 | import sys | |
24 | import tempfile |
|
24 | import tempfile | |
25 | import traceback |
|
25 | import traceback | |
26 | import types |
|
26 | import types | |
27 | import subprocess |
|
27 | import subprocess | |
28 | import warnings |
|
28 | import warnings | |
29 | from io import open as io_open |
|
29 | from io import open as io_open | |
30 |
|
30 | |||
31 | from pickleshare import PickleShareDB |
|
31 | from pickleshare import PickleShareDB | |
32 |
|
32 | |||
33 | from traitlets.config.configurable import SingletonConfigurable |
|
33 | from traitlets.config.configurable import SingletonConfigurable | |
34 | from IPython.core import oinspect |
|
34 | from IPython.core import oinspect | |
35 | from IPython.core import magic |
|
35 | from IPython.core import magic | |
36 | from IPython.core import page |
|
36 | from IPython.core import page | |
37 | from IPython.core import prefilter |
|
37 | from IPython.core import prefilter | |
38 | from IPython.core import shadowns |
|
38 | from IPython.core import shadowns | |
39 | from IPython.core import ultratb |
|
39 | from IPython.core import ultratb | |
40 | from IPython.core.alias import Alias, AliasManager |
|
40 | from IPython.core.alias import Alias, AliasManager | |
41 | from IPython.core.autocall import ExitAutocall |
|
41 | from IPython.core.autocall import ExitAutocall | |
42 | from IPython.core.builtin_trap import BuiltinTrap |
|
42 | from IPython.core.builtin_trap import BuiltinTrap | |
43 | from IPython.core.events import EventManager, available_events |
|
43 | from IPython.core.events import EventManager, available_events | |
44 | from IPython.core.compilerop import CachingCompiler, check_linecache_ipython |
|
44 | from IPython.core.compilerop import CachingCompiler, check_linecache_ipython | |
|
45 | from IPython.core.debugger import Pdb | |||
45 | from IPython.core.display_trap import DisplayTrap |
|
46 | from IPython.core.display_trap import DisplayTrap | |
46 | from IPython.core.displayhook import DisplayHook |
|
47 | from IPython.core.displayhook import DisplayHook | |
47 | from IPython.core.displaypub import DisplayPublisher |
|
48 | from IPython.core.displaypub import DisplayPublisher | |
48 | from IPython.core.error import InputRejected, UsageError |
|
49 | from IPython.core.error import InputRejected, UsageError | |
49 | from IPython.core.extensions import ExtensionManager |
|
50 | from IPython.core.extensions import ExtensionManager | |
50 | from IPython.core.formatters import DisplayFormatter |
|
51 | from IPython.core.formatters import DisplayFormatter | |
51 | from IPython.core.history import HistoryManager |
|
52 | from IPython.core.history import HistoryManager | |
52 | from IPython.core.inputsplitter import ESC_MAGIC, ESC_MAGIC2 |
|
53 | from IPython.core.inputsplitter import ESC_MAGIC, ESC_MAGIC2 | |
53 | from IPython.core.logger import Logger |
|
54 | from IPython.core.logger import Logger | |
54 | from IPython.core.macro import Macro |
|
55 | from IPython.core.macro import Macro | |
55 | from IPython.core.payload import PayloadManager |
|
56 | from IPython.core.payload import PayloadManager | |
56 | from IPython.core.prefilter import PrefilterManager |
|
57 | from IPython.core.prefilter import PrefilterManager | |
57 | from IPython.core.profiledir import ProfileDir |
|
58 | from IPython.core.profiledir import ProfileDir | |
58 | from IPython.core.prompts import PromptManager |
|
59 | from IPython.core.prompts import PromptManager | |
59 | from IPython.core.usage import default_banner |
|
60 | from IPython.core.usage import default_banner | |
60 | from IPython.testing.skipdoctest import skip_doctest_py2, skip_doctest |
|
61 | from IPython.testing.skipdoctest import skip_doctest_py2, skip_doctest | |
61 | from IPython.utils import PyColorize |
|
62 | from IPython.utils import PyColorize | |
62 | from IPython.utils import io |
|
63 | from IPython.utils import io | |
63 | from IPython.utils import py3compat |
|
64 | from IPython.utils import py3compat | |
64 | from IPython.utils import openpy |
|
65 | from IPython.utils import openpy | |
65 | from IPython.utils.contexts import NoOpContext |
|
66 | from IPython.utils.contexts import NoOpContext | |
66 | from IPython.utils.decorators import undoc |
|
67 | from IPython.utils.decorators import undoc | |
67 | from IPython.utils.io import ask_yes_no |
|
68 | from IPython.utils.io import ask_yes_no | |
68 | from IPython.utils.ipstruct import Struct |
|
69 | from IPython.utils.ipstruct import Struct | |
69 | from IPython.paths import get_ipython_dir |
|
70 | from IPython.paths import get_ipython_dir | |
70 | from IPython.utils.path import get_home_dir, get_py_filename, unquote_filename, ensure_dir_exists |
|
71 | from IPython.utils.path import get_home_dir, get_py_filename, unquote_filename, ensure_dir_exists | |
71 | from IPython.utils.process import system, getoutput |
|
72 | from IPython.utils.process import system, getoutput | |
72 | from IPython.utils.py3compat import (builtin_mod, unicode_type, string_types, |
|
73 | from IPython.utils.py3compat import (builtin_mod, unicode_type, string_types, | |
73 | with_metaclass, iteritems) |
|
74 | with_metaclass, iteritems) | |
74 | from IPython.utils.strdispatch import StrDispatch |
|
75 | from IPython.utils.strdispatch import StrDispatch | |
75 | from IPython.utils.syspathcontext import prepended_to_syspath |
|
76 | from IPython.utils.syspathcontext import prepended_to_syspath | |
76 | from IPython.utils.text import (format_screen, LSString, SList, |
|
77 | from IPython.utils.text import (format_screen, LSString, SList, | |
77 | DollarFormatter) |
|
78 | DollarFormatter) | |
78 | from traitlets import ( |
|
79 | from traitlets import ( | |
79 | Integer, Bool, CaselessStrEnum, Enum, List, Dict, Unicode, Instance, Type, |
|
80 | Integer, Bool, CaselessStrEnum, Enum, List, Dict, Unicode, Instance, Type, | |
80 | observe, default, |
|
81 | observe, default, | |
81 | ) |
|
82 | ) | |
82 | from warnings import warn |
|
83 | from warnings import warn | |
83 | from logging import error |
|
84 | from logging import error | |
84 | import IPython.core.hooks |
|
85 | import IPython.core.hooks | |
85 |
|
86 | |||
86 | #----------------------------------------------------------------------------- |
|
87 | #----------------------------------------------------------------------------- | |
87 | # Globals |
|
88 | # Globals | |
88 | #----------------------------------------------------------------------------- |
|
89 | #----------------------------------------------------------------------------- | |
89 |
|
90 | |||
90 | # compiled regexps for autoindent management |
|
91 | # compiled regexps for autoindent management | |
91 | dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass') |
|
92 | dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass') | |
92 |
|
93 | |||
93 | #----------------------------------------------------------------------------- |
|
94 | #----------------------------------------------------------------------------- | |
94 | # Utilities |
|
95 | # Utilities | |
95 | #----------------------------------------------------------------------------- |
|
96 | #----------------------------------------------------------------------------- | |
96 |
|
97 | |||
97 | @undoc |
|
98 | @undoc | |
98 | def softspace(file, newvalue): |
|
99 | def softspace(file, newvalue): | |
99 | """Copied from code.py, to remove the dependency""" |
|
100 | """Copied from code.py, to remove the dependency""" | |
100 |
|
101 | |||
101 | oldvalue = 0 |
|
102 | oldvalue = 0 | |
102 | try: |
|
103 | try: | |
103 | oldvalue = file.softspace |
|
104 | oldvalue = file.softspace | |
104 | except AttributeError: |
|
105 | except AttributeError: | |
105 | pass |
|
106 | pass | |
106 | try: |
|
107 | try: | |
107 | file.softspace = newvalue |
|
108 | file.softspace = newvalue | |
108 | except (AttributeError, TypeError): |
|
109 | except (AttributeError, TypeError): | |
109 | # "attribute-less object" or "read-only attributes" |
|
110 | # "attribute-less object" or "read-only attributes" | |
110 | pass |
|
111 | pass | |
111 | return oldvalue |
|
112 | return oldvalue | |
112 |
|
113 | |||
113 | @undoc |
|
114 | @undoc | |
114 | def no_op(*a, **kw): pass |
|
115 | def no_op(*a, **kw): pass | |
115 |
|
116 | |||
116 |
|
117 | |||
117 | class SpaceInInput(Exception): pass |
|
118 | class SpaceInInput(Exception): pass | |
118 |
|
119 | |||
119 |
|
120 | |||
120 | def get_default_colors(): |
|
121 | def get_default_colors(): | |
121 | if sys.platform=='darwin': |
|
122 | if sys.platform=='darwin': | |
122 | return "LightBG" |
|
123 | return "LightBG" | |
123 | elif os.name=='nt': |
|
124 | elif os.name=='nt': | |
124 | return 'Linux' |
|
125 | return 'Linux' | |
125 | else: |
|
126 | else: | |
126 | return 'Linux' |
|
127 | return 'Linux' | |
127 |
|
128 | |||
128 |
|
129 | |||
129 | class SeparateUnicode(Unicode): |
|
130 | class SeparateUnicode(Unicode): | |
130 | r"""A Unicode subclass to validate separate_in, separate_out, etc. |
|
131 | r"""A Unicode subclass to validate separate_in, separate_out, etc. | |
131 |
|
132 | |||
132 | This is a Unicode based trait that converts '0'->'' and ``'\\n'->'\n'``. |
|
133 | This is a Unicode based trait that converts '0'->'' and ``'\\n'->'\n'``. | |
133 | """ |
|
134 | """ | |
134 |
|
135 | |||
135 | def validate(self, obj, value): |
|
136 | def validate(self, obj, value): | |
136 | if value == '0': value = '' |
|
137 | if value == '0': value = '' | |
137 | value = value.replace('\\n','\n') |
|
138 | value = value.replace('\\n','\n') | |
138 | return super(SeparateUnicode, self).validate(obj, value) |
|
139 | return super(SeparateUnicode, self).validate(obj, value) | |
139 |
|
140 | |||
140 |
|
141 | |||
141 | @undoc |
|
142 | @undoc | |
142 | class DummyMod(object): |
|
143 | class DummyMod(object): | |
143 | """A dummy module used for IPython's interactive module when |
|
144 | """A dummy module used for IPython's interactive module when | |
144 | a namespace must be assigned to the module's __dict__.""" |
|
145 | a namespace must be assigned to the module's __dict__.""" | |
145 | pass |
|
146 | pass | |
146 |
|
147 | |||
147 |
|
148 | |||
148 | class ExecutionResult(object): |
|
149 | class ExecutionResult(object): | |
149 | """The result of a call to :meth:`InteractiveShell.run_cell` |
|
150 | """The result of a call to :meth:`InteractiveShell.run_cell` | |
150 |
|
151 | |||
151 | Stores information about what took place. |
|
152 | Stores information about what took place. | |
152 | """ |
|
153 | """ | |
153 | execution_count = None |
|
154 | execution_count = None | |
154 | error_before_exec = None |
|
155 | error_before_exec = None | |
155 | error_in_exec = None |
|
156 | error_in_exec = None | |
156 | result = None |
|
157 | result = None | |
157 |
|
158 | |||
158 | @property |
|
159 | @property | |
159 | def success(self): |
|
160 | def success(self): | |
160 | return (self.error_before_exec is None) and (self.error_in_exec is None) |
|
161 | return (self.error_before_exec is None) and (self.error_in_exec is None) | |
161 |
|
162 | |||
162 | def raise_error(self): |
|
163 | def raise_error(self): | |
163 | """Reraises error if `success` is `False`, otherwise does nothing""" |
|
164 | """Reraises error if `success` is `False`, otherwise does nothing""" | |
164 | if self.error_before_exec is not None: |
|
165 | if self.error_before_exec is not None: | |
165 | raise self.error_before_exec |
|
166 | raise self.error_before_exec | |
166 | if self.error_in_exec is not None: |
|
167 | if self.error_in_exec is not None: | |
167 | raise self.error_in_exec |
|
168 | raise self.error_in_exec | |
168 |
|
169 | |||
169 |
|
170 | |||
170 | class InteractiveShell(SingletonConfigurable): |
|
171 | class InteractiveShell(SingletonConfigurable): | |
171 | """An enhanced, interactive shell for Python.""" |
|
172 | """An enhanced, interactive shell for Python.""" | |
172 |
|
173 | |||
173 | _instance = None |
|
174 | _instance = None | |
174 |
|
175 | |||
175 | ast_transformers = List([], help= |
|
176 | ast_transformers = List([], help= | |
176 | """ |
|
177 | """ | |
177 | A list of ast.NodeTransformer subclass instances, which will be applied |
|
178 | A list of ast.NodeTransformer subclass instances, which will be applied | |
178 | to user input before code is run. |
|
179 | to user input before code is run. | |
179 | """ |
|
180 | """ | |
180 | ).tag(config=True) |
|
181 | ).tag(config=True) | |
181 |
|
182 | |||
182 | autocall = Enum((0,1,2), default_value=0, help= |
|
183 | autocall = Enum((0,1,2), default_value=0, help= | |
183 | """ |
|
184 | """ | |
184 | Make IPython automatically call any callable object even if you didn't |
|
185 | Make IPython automatically call any callable object even if you didn't | |
185 | type explicit parentheses. For example, 'str 43' becomes 'str(43)' |
|
186 | type explicit parentheses. For example, 'str 43' becomes 'str(43)' | |
186 | automatically. The value can be '0' to disable the feature, '1' for |
|
187 | automatically. The value can be '0' to disable the feature, '1' for | |
187 | 'smart' autocall, where it is not applied if there are no more |
|
188 | 'smart' autocall, where it is not applied if there are no more | |
188 | arguments on the line, and '2' for 'full' autocall, where all callable |
|
189 | arguments on the line, and '2' for 'full' autocall, where all callable | |
189 | objects are automatically called (even if no arguments are present). |
|
190 | objects are automatically called (even if no arguments are present). | |
190 | """ |
|
191 | """ | |
191 | ).tag(config=True) |
|
192 | ).tag(config=True) | |
192 | # TODO: remove all autoindent logic and put into frontends. |
|
193 | # TODO: remove all autoindent logic and put into frontends. | |
193 | # We can't do this yet because even runlines uses the autoindent. |
|
194 | # We can't do this yet because even runlines uses the autoindent. | |
194 | autoindent = Bool(True, help= |
|
195 | autoindent = Bool(True, help= | |
195 | """ |
|
196 | """ | |
196 | Autoindent IPython code entered interactively. |
|
197 | Autoindent IPython code entered interactively. | |
197 | """ |
|
198 | """ | |
198 | ).tag(config=True) |
|
199 | ).tag(config=True) | |
199 | automagic = Bool(True, help= |
|
200 | automagic = Bool(True, help= | |
200 | """ |
|
201 | """ | |
201 | Enable magic commands to be called without the leading %. |
|
202 | Enable magic commands to be called without the leading %. | |
202 | """ |
|
203 | """ | |
203 | ).tag(config=True) |
|
204 | ).tag(config=True) | |
204 |
|
205 | |||
205 | banner1 = Unicode(default_banner, |
|
206 | banner1 = Unicode(default_banner, | |
206 | help="""The part of the banner to be printed before the profile""" |
|
207 | help="""The part of the banner to be printed before the profile""" | |
207 | ).tag(config=True) |
|
208 | ).tag(config=True) | |
208 | banner2 = Unicode('', |
|
209 | banner2 = Unicode('', | |
209 | help="""The part of the banner to be printed after the profile""" |
|
210 | help="""The part of the banner to be printed after the profile""" | |
210 | ).tag(config=True) |
|
211 | ).tag(config=True) | |
211 |
|
212 | |||
212 | cache_size = Integer(1000, help= |
|
213 | cache_size = Integer(1000, help= | |
213 | """ |
|
214 | """ | |
214 | Set the size of the output cache. The default is 1000, you can |
|
215 | Set the size of the output cache. The default is 1000, you can | |
215 | change it permanently in your config file. Setting it to 0 completely |
|
216 | change it permanently in your config file. Setting it to 0 completely | |
216 | disables the caching system, and the minimum value accepted is 20 (if |
|
217 | disables the caching system, and the minimum value accepted is 20 (if | |
217 | you provide a value less than 20, it is reset to 0 and a warning is |
|
218 | you provide a value less than 20, it is reset to 0 and a warning is | |
218 | issued). This limit is defined because otherwise you'll spend more |
|
219 | issued). This limit is defined because otherwise you'll spend more | |
219 | time re-flushing a too small cache than working |
|
220 | time re-flushing a too small cache than working | |
220 | """ |
|
221 | """ | |
221 | ).tag(config=True) |
|
222 | ).tag(config=True) | |
222 | color_info = Bool(True, help= |
|
223 | color_info = Bool(True, help= | |
223 | """ |
|
224 | """ | |
224 | Use colors for displaying information about objects. Because this |
|
225 | Use colors for displaying information about objects. Because this | |
225 | information is passed through a pager (like 'less'), and some pagers |
|
226 | information is passed through a pager (like 'less'), and some pagers | |
226 | get confused with color codes, this capability can be turned off. |
|
227 | get confused with color codes, this capability can be turned off. | |
227 | """ |
|
228 | """ | |
228 | ).tag(config=True) |
|
229 | ).tag(config=True) | |
229 | colors = CaselessStrEnum(('NoColor','LightBG','Linux'), |
|
230 | colors = CaselessStrEnum(('NoColor','LightBG','Linux'), | |
230 | default_value=get_default_colors(), |
|
231 | default_value=get_default_colors(), | |
231 | help="Set the color scheme (NoColor, Linux, or LightBG)." |
|
232 | help="Set the color scheme (NoColor, Linux, or LightBG)." | |
232 | ).tag(config=True) |
|
233 | ).tag(config=True) | |
233 | colors_force = Bool(False, help= |
|
234 | colors_force = Bool(False, help= | |
234 | """ |
|
235 | """ | |
235 | Force use of ANSI color codes, regardless of OS and readline |
|
236 | Force use of ANSI color codes, regardless of OS and readline | |
236 | availability. |
|
237 | availability. | |
237 | """ |
|
238 | """ | |
238 | # FIXME: This is essentially a hack to allow ZMQShell to show colors |
|
239 | # FIXME: This is essentially a hack to allow ZMQShell to show colors | |
239 | # without readline on Win32. When the ZMQ formatting system is |
|
240 | # without readline on Win32. When the ZMQ formatting system is | |
240 | # refactored, this should be removed. |
|
241 | # refactored, this should be removed. | |
241 | ) |
|
242 | ) | |
242 | debug = Bool(False).tag(config=True) |
|
243 | debug = Bool(False).tag(config=True) | |
243 | deep_reload = Bool(False, help= |
|
244 | deep_reload = Bool(False, help= | |
244 | """ |
|
245 | """ | |
245 | **Deprecated** |
|
246 | **Deprecated** | |
246 |
|
247 | |||
247 | Will be removed in IPython 6.0 |
|
248 | Will be removed in IPython 6.0 | |
248 |
|
249 | |||
249 | Enable deep (recursive) reloading by default. IPython can use the |
|
250 | Enable deep (recursive) reloading by default. IPython can use the | |
250 | deep_reload module which reloads changes in modules recursively (it |
|
251 | deep_reload module which reloads changes in modules recursively (it | |
251 | replaces the reload() function, so you don't need to change anything to |
|
252 | replaces the reload() function, so you don't need to change anything to | |
252 | use it). `deep_reload` forces a full reload of modules whose code may |
|
253 | use it). `deep_reload` forces a full reload of modules whose code may | |
253 | have changed, which the default reload() function does not. When |
|
254 | have changed, which the default reload() function does not. When | |
254 | deep_reload is off, IPython will use the normal reload(), but |
|
255 | deep_reload is off, IPython will use the normal reload(), but | |
255 | deep_reload will still be available as dreload(). |
|
256 | deep_reload will still be available as dreload(). | |
256 | """ |
|
257 | """ | |
257 | ).tag(config=True) |
|
258 | ).tag(config=True) | |
258 | disable_failing_post_execute = Bool(False, |
|
259 | disable_failing_post_execute = Bool(False, | |
259 | help="Don't call post-execute functions that have failed in the past." |
|
260 | help="Don't call post-execute functions that have failed in the past." | |
260 | ).tag(config=True) |
|
261 | ).tag(config=True) | |
261 | display_formatter = Instance(DisplayFormatter, allow_none=True) |
|
262 | display_formatter = Instance(DisplayFormatter, allow_none=True) | |
262 | displayhook_class = Type(DisplayHook) |
|
263 | displayhook_class = Type(DisplayHook) | |
263 | display_pub_class = Type(DisplayPublisher) |
|
264 | display_pub_class = Type(DisplayPublisher) | |
264 | data_pub_class = None |
|
265 | data_pub_class = None | |
265 |
|
266 | |||
266 | exit_now = Bool(False) |
|
267 | exit_now = Bool(False) | |
267 | exiter = Instance(ExitAutocall) |
|
268 | exiter = Instance(ExitAutocall) | |
268 | @default('exiter') |
|
269 | @default('exiter') | |
269 | def _exiter_default(self): |
|
270 | def _exiter_default(self): | |
270 | return ExitAutocall(self) |
|
271 | return ExitAutocall(self) | |
271 | # Monotonically increasing execution counter |
|
272 | # Monotonically increasing execution counter | |
272 | execution_count = Integer(1) |
|
273 | execution_count = Integer(1) | |
273 | filename = Unicode("<ipython console>") |
|
274 | filename = Unicode("<ipython console>") | |
274 | ipython_dir= Unicode('').tag(config=True) # Set to get_ipython_dir() in __init__ |
|
275 | ipython_dir= Unicode('').tag(config=True) # Set to get_ipython_dir() in __init__ | |
275 |
|
276 | |||
276 | # Input splitter, to transform input line by line and detect when a block |
|
277 | # Input splitter, to transform input line by line and detect when a block | |
277 | # is ready to be executed. |
|
278 | # is ready to be executed. | |
278 | input_splitter = Instance('IPython.core.inputsplitter.IPythonInputSplitter', |
|
279 | input_splitter = Instance('IPython.core.inputsplitter.IPythonInputSplitter', | |
279 | (), {'line_input_checker': True}) |
|
280 | (), {'line_input_checker': True}) | |
280 |
|
281 | |||
281 | # This InputSplitter instance is used to transform completed cells before |
|
282 | # This InputSplitter instance is used to transform completed cells before | |
282 | # running them. It allows cell magics to contain blank lines. |
|
283 | # running them. It allows cell magics to contain blank lines. | |
283 | input_transformer_manager = Instance('IPython.core.inputsplitter.IPythonInputSplitter', |
|
284 | input_transformer_manager = Instance('IPython.core.inputsplitter.IPythonInputSplitter', | |
284 | (), {'line_input_checker': False}) |
|
285 | (), {'line_input_checker': False}) | |
285 |
|
286 | |||
286 | logstart = Bool(False, help= |
|
287 | logstart = Bool(False, help= | |
287 | """ |
|
288 | """ | |
288 | Start logging to the default log file in overwrite mode. |
|
289 | Start logging to the default log file in overwrite mode. | |
289 | Use `logappend` to specify a log file to **append** logs to. |
|
290 | Use `logappend` to specify a log file to **append** logs to. | |
290 | """ |
|
291 | """ | |
291 | ).tag(config=True) |
|
292 | ).tag(config=True) | |
292 | logfile = Unicode('', help= |
|
293 | logfile = Unicode('', help= | |
293 | """ |
|
294 | """ | |
294 | The name of the logfile to use. |
|
295 | The name of the logfile to use. | |
295 | """ |
|
296 | """ | |
296 | ).tag(config=True) |
|
297 | ).tag(config=True) | |
297 | logappend = Unicode('', help= |
|
298 | logappend = Unicode('', help= | |
298 | """ |
|
299 | """ | |
299 | Start logging to the given file in append mode. |
|
300 | Start logging to the given file in append mode. | |
300 | Use `logfile` to specify a log file to **overwrite** logs to. |
|
301 | Use `logfile` to specify a log file to **overwrite** logs to. | |
301 | """ |
|
302 | """ | |
302 | ).tag(config=True) |
|
303 | ).tag(config=True) | |
303 | object_info_string_level = Enum((0,1,2), default_value=0, |
|
304 | object_info_string_level = Enum((0,1,2), default_value=0, | |
304 | ).tag(config=True) |
|
305 | ).tag(config=True) | |
305 | pdb = Bool(False, help= |
|
306 | pdb = Bool(False, help= | |
306 | """ |
|
307 | """ | |
307 | Automatically call the pdb debugger after every exception. |
|
308 | Automatically call the pdb debugger after every exception. | |
308 | """ |
|
309 | """ | |
309 | ).tag(config=True) |
|
310 | ).tag(config=True) | |
310 | multiline_history = Bool(sys.platform != 'win32', |
|
311 | multiline_history = Bool(sys.platform != 'win32', | |
311 | help="Save multi-line entries as one entry in readline history" |
|
312 | help="Save multi-line entries as one entry in readline history" | |
312 | ).tag(config=True) |
|
313 | ).tag(config=True) | |
313 | display_page = Bool(False, |
|
314 | display_page = Bool(False, | |
314 | help="""If True, anything that would be passed to the pager |
|
315 | help="""If True, anything that would be passed to the pager | |
315 | will be displayed as regular output instead.""" |
|
316 | will be displayed as regular output instead.""" | |
316 | ).tag(config=True) |
|
317 | ).tag(config=True) | |
317 |
|
318 | |||
318 | # deprecated prompt traits: |
|
319 | # deprecated prompt traits: | |
319 |
|
320 | |||
320 | prompt_in1 = Unicode('In [\\#]: ', |
|
321 | prompt_in1 = Unicode('In [\\#]: ', | |
321 | help="Deprecated, will be removed in IPython 5.0, use PromptManager.in_template" |
|
322 | help="Deprecated, will be removed in IPython 5.0, use PromptManager.in_template" | |
322 | ).tag(config=True) |
|
323 | ).tag(config=True) | |
323 | prompt_in2 = Unicode(' .\\D.: ', |
|
324 | prompt_in2 = Unicode(' .\\D.: ', | |
324 | help="Deprecated, will be removed in IPython 5.0, use PromptManager.in2_template" |
|
325 | help="Deprecated, will be removed in IPython 5.0, use PromptManager.in2_template" | |
325 | ).tag(config=True) |
|
326 | ).tag(config=True) | |
326 | prompt_out = Unicode('Out[\\#]: ', |
|
327 | prompt_out = Unicode('Out[\\#]: ', | |
327 | help="Deprecated, will be removed in IPython 5.0, use PromptManager.out_template" |
|
328 | help="Deprecated, will be removed in IPython 5.0, use PromptManager.out_template" | |
328 | ).tag(config=True) |
|
329 | ).tag(config=True) | |
329 | prompts_pad_left = Bool(True, |
|
330 | prompts_pad_left = Bool(True, | |
330 | help="Deprecated, will be removed in IPython 5.0, use PromptManager.justify" |
|
331 | help="Deprecated, will be removed in IPython 5.0, use PromptManager.justify" | |
331 | ).tag(config=True) |
|
332 | ).tag(config=True) | |
332 |
|
333 | |||
333 | @observe('prompt_in1', 'prompt_in2', 'prompt_out', 'prompt_pad_left') |
|
334 | @observe('prompt_in1', 'prompt_in2', 'prompt_out', 'prompt_pad_left') | |
334 | def _prompt_trait_changed(self, change): |
|
335 | def _prompt_trait_changed(self, change): | |
335 | table = { |
|
336 | table = { | |
336 | 'prompt_in1' : 'in_template', |
|
337 | 'prompt_in1' : 'in_template', | |
337 | 'prompt_in2' : 'in2_template', |
|
338 | 'prompt_in2' : 'in2_template', | |
338 | 'prompt_out' : 'out_template', |
|
339 | 'prompt_out' : 'out_template', | |
339 | 'prompts_pad_left' : 'justify', |
|
340 | 'prompts_pad_left' : 'justify', | |
340 | } |
|
341 | } | |
341 | name = change['name'] |
|
342 | name = change['name'] | |
342 | warn("InteractiveShell.{name} is deprecated, use PromptManager.{newname}".format( |
|
343 | warn("InteractiveShell.{name} is deprecated, use PromptManager.{newname}".format( | |
343 | name=name, newname=table[name]) |
|
344 | name=name, newname=table[name]) | |
344 | ) |
|
345 | ) | |
345 | # protect against weird cases where self.config may not exist: |
|
346 | # protect against weird cases where self.config may not exist: | |
346 | if self.config is not None: |
|
347 | if self.config is not None: | |
347 | # propagate to corresponding PromptManager trait |
|
348 | # propagate to corresponding PromptManager trait | |
348 | setattr(self.config.PromptManager, table[name], change['new']) |
|
349 | setattr(self.config.PromptManager, table[name], change['new']) | |
349 |
|
350 | |||
350 | show_rewritten_input = Bool(True, |
|
351 | show_rewritten_input = Bool(True, | |
351 | help="Show rewritten input, e.g. for autocall." |
|
352 | help="Show rewritten input, e.g. for autocall." | |
352 | ).tag(config=True) |
|
353 | ).tag(config=True) | |
353 |
|
354 | |||
354 | quiet = Bool(False).tag(config=True) |
|
355 | quiet = Bool(False).tag(config=True) | |
355 |
|
356 | |||
356 | history_length = Integer(10000, |
|
357 | history_length = Integer(10000, | |
357 | help='Total length of command history' |
|
358 | help='Total length of command history' | |
358 | ).tag(config=True) |
|
359 | ).tag(config=True) | |
359 |
|
360 | |||
360 | history_load_length = Integer(1000, help= |
|
361 | history_load_length = Integer(1000, help= | |
361 | """ |
|
362 | """ | |
362 | The number of saved history entries to be loaded |
|
363 | The number of saved history entries to be loaded | |
363 | into the readline buffer at startup. |
|
364 | into the readline buffer at startup. | |
364 | """ |
|
365 | """ | |
365 | ).tag(config=True) |
|
366 | ).tag(config=True) | |
366 |
|
367 | |||
367 | # The readline stuff will eventually be moved to the terminal subclass |
|
368 | # The readline stuff will eventually be moved to the terminal subclass | |
368 | # but for now, we can't do that as readline is welded in everywhere. |
|
369 | # but for now, we can't do that as readline is welded in everywhere. | |
369 | readline_use = Bool(True).tag(config=True) |
|
370 | readline_use = Bool(True).tag(config=True) | |
370 | readline_remove_delims = Unicode('-/~').tag(config=True) |
|
371 | readline_remove_delims = Unicode('-/~').tag(config=True) | |
371 | readline_delims = Unicode() # set by init_readline() |
|
372 | readline_delims = Unicode() # set by init_readline() | |
372 | # don't use \M- bindings by default, because they |
|
373 | # don't use \M- bindings by default, because they | |
373 | # conflict with 8-bit encodings. See gh-58,gh-88 |
|
374 | # conflict with 8-bit encodings. See gh-58,gh-88 | |
374 | readline_parse_and_bind = List([ |
|
375 | readline_parse_and_bind = List([ | |
375 | 'tab: complete', |
|
376 | 'tab: complete', | |
376 | '"\C-l": clear-screen', |
|
377 | '"\C-l": clear-screen', | |
377 | 'set show-all-if-ambiguous on', |
|
378 | 'set show-all-if-ambiguous on', | |
378 | '"\C-o": tab-insert', |
|
379 | '"\C-o": tab-insert', | |
379 | '"\C-r": reverse-search-history', |
|
380 | '"\C-r": reverse-search-history', | |
380 | '"\C-s": forward-search-history', |
|
381 | '"\C-s": forward-search-history', | |
381 | '"\C-p": history-search-backward', |
|
382 | '"\C-p": history-search-backward', | |
382 | '"\C-n": history-search-forward', |
|
383 | '"\C-n": history-search-forward', | |
383 | '"\e[A": history-search-backward', |
|
384 | '"\e[A": history-search-backward', | |
384 | '"\e[B": history-search-forward', |
|
385 | '"\e[B": history-search-forward', | |
385 | '"\C-k": kill-line', |
|
386 | '"\C-k": kill-line', | |
386 | '"\C-u": unix-line-discard', |
|
387 | '"\C-u": unix-line-discard', | |
387 | ]).tag(config=True) |
|
388 | ]).tag(config=True) | |
388 |
|
389 | |||
389 | _custom_readline_config = False |
|
390 | _custom_readline_config = False | |
390 |
|
391 | |||
391 | @observe('readline_parse_and_bind') |
|
392 | @observe('readline_parse_and_bind') | |
392 | def _readline_parse_and_bind_changed(self, change): |
|
393 | def _readline_parse_and_bind_changed(self, change): | |
393 | # notice that readline config is customized |
|
394 | # notice that readline config is customized | |
394 | # indicates that it should have higher priority than inputrc |
|
395 | # indicates that it should have higher priority than inputrc | |
395 | self._custom_readline_config = True |
|
396 | self._custom_readline_config = True | |
396 |
|
397 | |||
397 | ast_node_interactivity = Enum(['all', 'last', 'last_expr', 'none'], |
|
398 | ast_node_interactivity = Enum(['all', 'last', 'last_expr', 'none'], | |
398 | default_value='last_expr', |
|
399 | default_value='last_expr', | |
399 | help=""" |
|
400 | help=""" | |
400 | 'all', 'last', 'last_expr' or 'none', specifying which nodes should be |
|
401 | 'all', 'last', 'last_expr' or 'none', specifying which nodes should be | |
401 | run interactively (displaying output from expressions).""" |
|
402 | run interactively (displaying output from expressions).""" | |
402 | ).tag(config=True) |
|
403 | ).tag(config=True) | |
403 |
|
404 | |||
404 | # TODO: this part of prompt management should be moved to the frontends. |
|
405 | # TODO: this part of prompt management should be moved to the frontends. | |
405 | # Use custom TraitTypes that convert '0'->'' and '\\n'->'\n' |
|
406 | # Use custom TraitTypes that convert '0'->'' and '\\n'->'\n' | |
406 | separate_in = SeparateUnicode('\n').tag(config=True) |
|
407 | separate_in = SeparateUnicode('\n').tag(config=True) | |
407 | separate_out = SeparateUnicode('').tag(config=True) |
|
408 | separate_out = SeparateUnicode('').tag(config=True) | |
408 | separate_out2 = SeparateUnicode('').tag(config=True) |
|
409 | separate_out2 = SeparateUnicode('').tag(config=True) | |
409 | wildcards_case_sensitive = Bool(True).tag(config=True) |
|
410 | wildcards_case_sensitive = Bool(True).tag(config=True) | |
410 | xmode = CaselessStrEnum(('Context','Plain', 'Verbose'), |
|
411 | xmode = CaselessStrEnum(('Context','Plain', 'Verbose'), | |
411 | default_value='Context').tag(config=True) |
|
412 | default_value='Context').tag(config=True) | |
412 |
|
413 | |||
413 | # Subcomponents of InteractiveShell |
|
414 | # Subcomponents of InteractiveShell | |
414 | alias_manager = Instance('IPython.core.alias.AliasManager', allow_none=True) |
|
415 | alias_manager = Instance('IPython.core.alias.AliasManager', allow_none=True) | |
415 | prefilter_manager = Instance('IPython.core.prefilter.PrefilterManager', allow_none=True) |
|
416 | prefilter_manager = Instance('IPython.core.prefilter.PrefilterManager', allow_none=True) | |
416 | builtin_trap = Instance('IPython.core.builtin_trap.BuiltinTrap', allow_none=True) |
|
417 | builtin_trap = Instance('IPython.core.builtin_trap.BuiltinTrap', allow_none=True) | |
417 | display_trap = Instance('IPython.core.display_trap.DisplayTrap', allow_none=True) |
|
418 | display_trap = Instance('IPython.core.display_trap.DisplayTrap', allow_none=True) | |
418 | extension_manager = Instance('IPython.core.extensions.ExtensionManager', allow_none=True) |
|
419 | extension_manager = Instance('IPython.core.extensions.ExtensionManager', allow_none=True) | |
419 | payload_manager = Instance('IPython.core.payload.PayloadManager', allow_none=True) |
|
420 | payload_manager = Instance('IPython.core.payload.PayloadManager', allow_none=True) | |
420 | history_manager = Instance('IPython.core.history.HistoryAccessorBase', allow_none=True) |
|
421 | history_manager = Instance('IPython.core.history.HistoryAccessorBase', allow_none=True) | |
421 | magics_manager = Instance('IPython.core.magic.MagicsManager', allow_none=True) |
|
422 | magics_manager = Instance('IPython.core.magic.MagicsManager', allow_none=True) | |
422 |
|
423 | |||
423 | profile_dir = Instance('IPython.core.application.ProfileDir', allow_none=True) |
|
424 | profile_dir = Instance('IPython.core.application.ProfileDir', allow_none=True) | |
424 | @property |
|
425 | @property | |
425 | def profile(self): |
|
426 | def profile(self): | |
426 | if self.profile_dir is not None: |
|
427 | if self.profile_dir is not None: | |
427 | name = os.path.basename(self.profile_dir.location) |
|
428 | name = os.path.basename(self.profile_dir.location) | |
428 | return name.replace('profile_','') |
|
429 | return name.replace('profile_','') | |
429 |
|
430 | |||
430 |
|
431 | |||
431 | # Private interface |
|
432 | # Private interface | |
432 | _post_execute = Dict() |
|
433 | _post_execute = Dict() | |
433 |
|
434 | |||
434 | # Tracks any GUI loop loaded for pylab |
|
435 | # Tracks any GUI loop loaded for pylab | |
435 | pylab_gui_select = None |
|
436 | pylab_gui_select = None | |
436 |
|
437 | |||
437 | def __init__(self, ipython_dir=None, profile_dir=None, |
|
438 | def __init__(self, ipython_dir=None, profile_dir=None, | |
438 | user_module=None, user_ns=None, |
|
439 | user_module=None, user_ns=None, | |
439 | custom_exceptions=((), None), **kwargs): |
|
440 | custom_exceptions=((), None), **kwargs): | |
440 |
|
441 | |||
441 | # This is where traits with a config_key argument are updated |
|
442 | # This is where traits with a config_key argument are updated | |
442 | # from the values on config. |
|
443 | # from the values on config. | |
443 | super(InteractiveShell, self).__init__(**kwargs) |
|
444 | super(InteractiveShell, self).__init__(**kwargs) | |
444 | self.configurables = [self] |
|
445 | self.configurables = [self] | |
445 |
|
446 | |||
446 | # These are relatively independent and stateless |
|
447 | # These are relatively independent and stateless | |
447 | self.init_ipython_dir(ipython_dir) |
|
448 | self.init_ipython_dir(ipython_dir) | |
448 | self.init_profile_dir(profile_dir) |
|
449 | self.init_profile_dir(profile_dir) | |
449 | self.init_instance_attrs() |
|
450 | self.init_instance_attrs() | |
450 | self.init_environment() |
|
451 | self.init_environment() | |
451 |
|
452 | |||
452 | # Check if we're in a virtualenv, and set up sys.path. |
|
453 | # Check if we're in a virtualenv, and set up sys.path. | |
453 | self.init_virtualenv() |
|
454 | self.init_virtualenv() | |
454 |
|
455 | |||
455 | # Create namespaces (user_ns, user_global_ns, etc.) |
|
456 | # Create namespaces (user_ns, user_global_ns, etc.) | |
456 | self.init_create_namespaces(user_module, user_ns) |
|
457 | self.init_create_namespaces(user_module, user_ns) | |
457 | # This has to be done after init_create_namespaces because it uses |
|
458 | # This has to be done after init_create_namespaces because it uses | |
458 | # something in self.user_ns, but before init_sys_modules, which |
|
459 | # something in self.user_ns, but before init_sys_modules, which | |
459 | # is the first thing to modify sys. |
|
460 | # is the first thing to modify sys. | |
460 | # TODO: When we override sys.stdout and sys.stderr before this class |
|
461 | # TODO: When we override sys.stdout and sys.stderr before this class | |
461 | # is created, we are saving the overridden ones here. Not sure if this |
|
462 | # is created, we are saving the overridden ones here. Not sure if this | |
462 | # is what we want to do. |
|
463 | # is what we want to do. | |
463 | self.save_sys_module_state() |
|
464 | self.save_sys_module_state() | |
464 | self.init_sys_modules() |
|
465 | self.init_sys_modules() | |
465 |
|
466 | |||
466 | # While we're trying to have each part of the code directly access what |
|
467 | # While we're trying to have each part of the code directly access what | |
467 | # it needs without keeping redundant references to objects, we have too |
|
468 | # it needs without keeping redundant references to objects, we have too | |
468 | # much legacy code that expects ip.db to exist. |
|
469 | # much legacy code that expects ip.db to exist. | |
469 | self.db = PickleShareDB(os.path.join(self.profile_dir.location, 'db')) |
|
470 | self.db = PickleShareDB(os.path.join(self.profile_dir.location, 'db')) | |
470 |
|
471 | |||
471 | self.init_history() |
|
472 | self.init_history() | |
472 | self.init_encoding() |
|
473 | self.init_encoding() | |
473 | self.init_prefilter() |
|
474 | self.init_prefilter() | |
474 |
|
475 | |||
475 | self.init_syntax_highlighting() |
|
476 | self.init_syntax_highlighting() | |
476 | self.init_hooks() |
|
477 | self.init_hooks() | |
477 | self.init_events() |
|
478 | self.init_events() | |
478 | self.init_pushd_popd_magic() |
|
479 | self.init_pushd_popd_magic() | |
479 | # self.init_traceback_handlers use to be here, but we moved it below |
|
480 | # self.init_traceback_handlers use to be here, but we moved it below | |
480 | # because it and init_io have to come after init_readline. |
|
481 | # because it and init_io have to come after init_readline. | |
481 | self.init_user_ns() |
|
482 | self.init_user_ns() | |
482 | self.init_logger() |
|
483 | self.init_logger() | |
483 | self.init_builtins() |
|
484 | self.init_builtins() | |
484 |
|
485 | |||
485 | # The following was in post_config_initialization |
|
486 | # The following was in post_config_initialization | |
486 | self.init_inspector() |
|
487 | self.init_inspector() | |
487 | # init_readline() must come before init_io(), because init_io uses |
|
488 | # init_readline() must come before init_io(), because init_io uses | |
488 | # readline related things. |
|
489 | # readline related things. | |
489 | self.init_readline() |
|
490 | self.init_readline() | |
490 | # We save this here in case user code replaces raw_input, but it needs |
|
491 | # We save this here in case user code replaces raw_input, but it needs | |
491 | # to be after init_readline(), because PyPy's readline works by replacing |
|
492 | # to be after init_readline(), because PyPy's readline works by replacing | |
492 | # raw_input. |
|
493 | # raw_input. | |
493 | if py3compat.PY3: |
|
494 | if py3compat.PY3: | |
494 | self.raw_input_original = input |
|
495 | self.raw_input_original = input | |
495 | else: |
|
496 | else: | |
496 | self.raw_input_original = raw_input |
|
497 | self.raw_input_original = raw_input | |
497 | # init_completer must come after init_readline, because it needs to |
|
498 | # init_completer must come after init_readline, because it needs to | |
498 | # know whether readline is present or not system-wide to configure the |
|
499 | # know whether readline is present or not system-wide to configure the | |
499 | # completers, since the completion machinery can now operate |
|
500 | # completers, since the completion machinery can now operate | |
500 | # independently of readline (e.g. over the network) |
|
501 | # independently of readline (e.g. over the network) | |
501 | self.init_completer() |
|
502 | self.init_completer() | |
502 | # TODO: init_io() needs to happen before init_traceback handlers |
|
503 | # TODO: init_io() needs to happen before init_traceback handlers | |
503 | # because the traceback handlers hardcode the stdout/stderr streams. |
|
504 | # because the traceback handlers hardcode the stdout/stderr streams. | |
504 | # This logic in in debugger.Pdb and should eventually be changed. |
|
505 | # This logic in in debugger.Pdb and should eventually be changed. | |
505 | self.init_io() |
|
506 | self.init_io() | |
506 | self.init_traceback_handlers(custom_exceptions) |
|
507 | self.init_traceback_handlers(custom_exceptions) | |
507 | self.init_prompts() |
|
508 | self.init_prompts() | |
508 | self.init_display_formatter() |
|
509 | self.init_display_formatter() | |
509 | self.init_display_pub() |
|
510 | self.init_display_pub() | |
510 | self.init_data_pub() |
|
511 | self.init_data_pub() | |
511 | self.init_displayhook() |
|
512 | self.init_displayhook() | |
512 | self.init_magics() |
|
513 | self.init_magics() | |
513 | self.init_alias() |
|
514 | self.init_alias() | |
514 | self.init_logstart() |
|
515 | self.init_logstart() | |
515 | self.init_pdb() |
|
516 | self.init_pdb() | |
516 | self.init_extension_manager() |
|
517 | self.init_extension_manager() | |
517 | self.init_payload() |
|
518 | self.init_payload() | |
518 | self.init_deprecation_warnings() |
|
519 | self.init_deprecation_warnings() | |
519 | self.hooks.late_startup_hook() |
|
520 | self.hooks.late_startup_hook() | |
520 | self.events.trigger('shell_initialized', self) |
|
521 | self.events.trigger('shell_initialized', self) | |
521 | atexit.register(self.atexit_operations) |
|
522 | atexit.register(self.atexit_operations) | |
522 |
|
523 | |||
523 | def get_ipython(self): |
|
524 | def get_ipython(self): | |
524 | """Return the currently running IPython instance.""" |
|
525 | """Return the currently running IPython instance.""" | |
525 | return self |
|
526 | return self | |
526 |
|
527 | |||
527 | #------------------------------------------------------------------------- |
|
528 | #------------------------------------------------------------------------- | |
528 | # Trait changed handlers |
|
529 | # Trait changed handlers | |
529 | #------------------------------------------------------------------------- |
|
530 | #------------------------------------------------------------------------- | |
530 | @observe('ipython_dir') |
|
531 | @observe('ipython_dir') | |
531 | def _ipython_dir_changed(self, change): |
|
532 | def _ipython_dir_changed(self, change): | |
532 | ensure_dir_exists(change['new']) |
|
533 | ensure_dir_exists(change['new']) | |
533 |
|
534 | |||
534 | def set_autoindent(self,value=None): |
|
535 | def set_autoindent(self,value=None): | |
535 | """Set the autoindent flag. |
|
536 | """Set the autoindent flag. | |
536 |
|
537 | |||
537 | If called with no arguments, it acts as a toggle.""" |
|
538 | If called with no arguments, it acts as a toggle.""" | |
538 | if value is None: |
|
539 | if value is None: | |
539 | self.autoindent = not self.autoindent |
|
540 | self.autoindent = not self.autoindent | |
540 | else: |
|
541 | else: | |
541 | self.autoindent = value |
|
542 | self.autoindent = value | |
542 |
|
543 | |||
543 | #------------------------------------------------------------------------- |
|
544 | #------------------------------------------------------------------------- | |
544 | # init_* methods called by __init__ |
|
545 | # init_* methods called by __init__ | |
545 | #------------------------------------------------------------------------- |
|
546 | #------------------------------------------------------------------------- | |
546 |
|
547 | |||
547 | def init_ipython_dir(self, ipython_dir): |
|
548 | def init_ipython_dir(self, ipython_dir): | |
548 | if ipython_dir is not None: |
|
549 | if ipython_dir is not None: | |
549 | self.ipython_dir = ipython_dir |
|
550 | self.ipython_dir = ipython_dir | |
550 | return |
|
551 | return | |
551 |
|
552 | |||
552 | self.ipython_dir = get_ipython_dir() |
|
553 | self.ipython_dir = get_ipython_dir() | |
553 |
|
554 | |||
554 | def init_profile_dir(self, profile_dir): |
|
555 | def init_profile_dir(self, profile_dir): | |
555 | if profile_dir is not None: |
|
556 | if profile_dir is not None: | |
556 | self.profile_dir = profile_dir |
|
557 | self.profile_dir = profile_dir | |
557 | return |
|
558 | return | |
558 | self.profile_dir =\ |
|
559 | self.profile_dir =\ | |
559 | ProfileDir.create_profile_dir_by_name(self.ipython_dir, 'default') |
|
560 | ProfileDir.create_profile_dir_by_name(self.ipython_dir, 'default') | |
560 |
|
561 | |||
561 | def init_instance_attrs(self): |
|
562 | def init_instance_attrs(self): | |
562 | self.more = False |
|
563 | self.more = False | |
563 |
|
564 | |||
564 | # command compiler |
|
565 | # command compiler | |
565 | self.compile = CachingCompiler() |
|
566 | self.compile = CachingCompiler() | |
566 |
|
567 | |||
567 | # Make an empty namespace, which extension writers can rely on both |
|
568 | # Make an empty namespace, which extension writers can rely on both | |
568 | # existing and NEVER being used by ipython itself. This gives them a |
|
569 | # existing and NEVER being used by ipython itself. This gives them a | |
569 | # convenient location for storing additional information and state |
|
570 | # convenient location for storing additional information and state | |
570 | # their extensions may require, without fear of collisions with other |
|
571 | # their extensions may require, without fear of collisions with other | |
571 | # ipython names that may develop later. |
|
572 | # ipython names that may develop later. | |
572 | self.meta = Struct() |
|
573 | self.meta = Struct() | |
573 |
|
574 | |||
574 | # Temporary files used for various purposes. Deleted at exit. |
|
575 | # Temporary files used for various purposes. Deleted at exit. | |
575 | self.tempfiles = [] |
|
576 | self.tempfiles = [] | |
576 | self.tempdirs = [] |
|
577 | self.tempdirs = [] | |
577 |
|
578 | |||
578 | # Keep track of readline usage (later set by init_readline) |
|
579 | # Keep track of readline usage (later set by init_readline) | |
579 | self.has_readline = False |
|
580 | self.has_readline = False | |
580 |
|
581 | |||
581 | # keep track of where we started running (mainly for crash post-mortem) |
|
582 | # keep track of where we started running (mainly for crash post-mortem) | |
582 | # This is not being used anywhere currently. |
|
583 | # This is not being used anywhere currently. | |
583 | self.starting_dir = py3compat.getcwd() |
|
584 | self.starting_dir = py3compat.getcwd() | |
584 |
|
585 | |||
585 | # Indentation management |
|
586 | # Indentation management | |
586 | self.indent_current_nsp = 0 |
|
587 | self.indent_current_nsp = 0 | |
587 |
|
588 | |||
588 | # Dict to track post-execution functions that have been registered |
|
589 | # Dict to track post-execution functions that have been registered | |
589 | self._post_execute = {} |
|
590 | self._post_execute = {} | |
590 |
|
591 | |||
591 | def init_environment(self): |
|
592 | def init_environment(self): | |
592 | """Any changes we need to make to the user's environment.""" |
|
593 | """Any changes we need to make to the user's environment.""" | |
593 | pass |
|
594 | pass | |
594 |
|
595 | |||
595 | def init_encoding(self): |
|
596 | def init_encoding(self): | |
596 | # Get system encoding at startup time. Certain terminals (like Emacs |
|
597 | # Get system encoding at startup time. Certain terminals (like Emacs | |
597 | # under Win32 have it set to None, and we need to have a known valid |
|
598 | # under Win32 have it set to None, and we need to have a known valid | |
598 | # encoding to use in the raw_input() method |
|
599 | # encoding to use in the raw_input() method | |
599 | try: |
|
600 | try: | |
600 | self.stdin_encoding = sys.stdin.encoding or 'ascii' |
|
601 | self.stdin_encoding = sys.stdin.encoding or 'ascii' | |
601 | except AttributeError: |
|
602 | except AttributeError: | |
602 | self.stdin_encoding = 'ascii' |
|
603 | self.stdin_encoding = 'ascii' | |
603 |
|
604 | |||
604 | def init_syntax_highlighting(self): |
|
605 | def init_syntax_highlighting(self): | |
605 | # Python source parser/formatter for syntax highlighting |
|
606 | # Python source parser/formatter for syntax highlighting | |
606 | pyformat = PyColorize.Parser().format |
|
607 | pyformat = PyColorize.Parser().format | |
607 | self.pycolorize = lambda src: pyformat(src,'str',self.colors) |
|
608 | self.pycolorize = lambda src: pyformat(src,'str',self.colors) | |
608 |
|
609 | |||
609 | def init_pushd_popd_magic(self): |
|
610 | def init_pushd_popd_magic(self): | |
610 | # for pushd/popd management |
|
611 | # for pushd/popd management | |
611 | self.home_dir = get_home_dir() |
|
612 | self.home_dir = get_home_dir() | |
612 |
|
613 | |||
613 | self.dir_stack = [] |
|
614 | self.dir_stack = [] | |
614 |
|
615 | |||
615 | def init_logger(self): |
|
616 | def init_logger(self): | |
616 | self.logger = Logger(self.home_dir, logfname='ipython_log.py', |
|
617 | self.logger = Logger(self.home_dir, logfname='ipython_log.py', | |
617 | logmode='rotate') |
|
618 | logmode='rotate') | |
618 |
|
619 | |||
619 | def init_logstart(self): |
|
620 | def init_logstart(self): | |
620 | """Initialize logging in case it was requested at the command line. |
|
621 | """Initialize logging in case it was requested at the command line. | |
621 | """ |
|
622 | """ | |
622 | if self.logappend: |
|
623 | if self.logappend: | |
623 | self.magic('logstart %s append' % self.logappend) |
|
624 | self.magic('logstart %s append' % self.logappend) | |
624 | elif self.logfile: |
|
625 | elif self.logfile: | |
625 | self.magic('logstart %s' % self.logfile) |
|
626 | self.magic('logstart %s' % self.logfile) | |
626 | elif self.logstart: |
|
627 | elif self.logstart: | |
627 | self.magic('logstart') |
|
628 | self.magic('logstart') | |
628 |
|
629 | |||
629 | def init_deprecation_warnings(self): |
|
630 | def init_deprecation_warnings(self): | |
630 | """ |
|
631 | """ | |
631 | register default filter for deprecation warning. |
|
632 | register default filter for deprecation warning. | |
632 |
|
633 | |||
633 | This will allow deprecation warning of function used interactively to show |
|
634 | This will allow deprecation warning of function used interactively to show | |
634 | warning to users, and still hide deprecation warning from libraries import. |
|
635 | warning to users, and still hide deprecation warning from libraries import. | |
635 | """ |
|
636 | """ | |
636 | warnings.filterwarnings("default", category=DeprecationWarning, module=self.user_ns.get("__name__")) |
|
637 | warnings.filterwarnings("default", category=DeprecationWarning, module=self.user_ns.get("__name__")) | |
637 |
|
638 | |||
638 | def init_builtins(self): |
|
639 | def init_builtins(self): | |
639 | # A single, static flag that we set to True. Its presence indicates |
|
640 | # A single, static flag that we set to True. Its presence indicates | |
640 | # that an IPython shell has been created, and we make no attempts at |
|
641 | # that an IPython shell has been created, and we make no attempts at | |
641 | # removing on exit or representing the existence of more than one |
|
642 | # removing on exit or representing the existence of more than one | |
642 | # IPython at a time. |
|
643 | # IPython at a time. | |
643 | builtin_mod.__dict__['__IPYTHON__'] = True |
|
644 | builtin_mod.__dict__['__IPYTHON__'] = True | |
644 |
|
645 | |||
645 | # In 0.11 we introduced '__IPYTHON__active' as an integer we'd try to |
|
646 | # In 0.11 we introduced '__IPYTHON__active' as an integer we'd try to | |
646 | # manage on enter/exit, but with all our shells it's virtually |
|
647 | # manage on enter/exit, but with all our shells it's virtually | |
647 | # impossible to get all the cases right. We're leaving the name in for |
|
648 | # impossible to get all the cases right. We're leaving the name in for | |
648 | # those who adapted their codes to check for this flag, but will |
|
649 | # those who adapted their codes to check for this flag, but will | |
649 | # eventually remove it after a few more releases. |
|
650 | # eventually remove it after a few more releases. | |
650 | builtin_mod.__dict__['__IPYTHON__active'] = \ |
|
651 | builtin_mod.__dict__['__IPYTHON__active'] = \ | |
651 | 'Deprecated, check for __IPYTHON__' |
|
652 | 'Deprecated, check for __IPYTHON__' | |
652 |
|
653 | |||
653 | self.builtin_trap = BuiltinTrap(shell=self) |
|
654 | self.builtin_trap = BuiltinTrap(shell=self) | |
654 |
|
655 | |||
655 | def init_inspector(self): |
|
656 | def init_inspector(self): | |
656 | # Object inspector |
|
657 | # Object inspector | |
657 | self.inspector = oinspect.Inspector(oinspect.InspectColors, |
|
658 | self.inspector = oinspect.Inspector(oinspect.InspectColors, | |
658 | PyColorize.ANSICodeColors, |
|
659 | PyColorize.ANSICodeColors, | |
659 | 'NoColor', |
|
660 | 'NoColor', | |
660 | self.object_info_string_level) |
|
661 | self.object_info_string_level) | |
661 |
|
662 | |||
662 | def init_io(self): |
|
663 | def init_io(self): | |
663 | # This will just use sys.stdout and sys.stderr. If you want to |
|
664 | # This will just use sys.stdout and sys.stderr. If you want to | |
664 | # override sys.stdout and sys.stderr themselves, you need to do that |
|
665 | # override sys.stdout and sys.stderr themselves, you need to do that | |
665 | # *before* instantiating this class, because io holds onto |
|
666 | # *before* instantiating this class, because io holds onto | |
666 | # references to the underlying streams. |
|
667 | # references to the underlying streams. | |
667 | if (sys.platform == 'win32' or sys.platform == 'cli') and self.has_readline: |
|
668 | if (sys.platform == 'win32' or sys.platform == 'cli') and self.has_readline: | |
668 | io.stdout = io.stderr = io.IOStream(self.readline._outputfile) |
|
669 | io.stdout = io.stderr = io.IOStream(self.readline._outputfile) | |
669 | else: |
|
670 | else: | |
670 | io.stdout = io.IOStream(sys.stdout) |
|
671 | io.stdout = io.IOStream(sys.stdout) | |
671 | io.stderr = io.IOStream(sys.stderr) |
|
672 | io.stderr = io.IOStream(sys.stderr) | |
672 |
|
673 | |||
673 | def init_prompts(self): |
|
674 | def init_prompts(self): | |
674 | self.prompt_manager = PromptManager(shell=self, parent=self) |
|
675 | self.prompt_manager = PromptManager(shell=self, parent=self) | |
675 | self.configurables.append(self.prompt_manager) |
|
676 | self.configurables.append(self.prompt_manager) | |
676 | # Set system prompts, so that scripts can decide if they are running |
|
677 | # Set system prompts, so that scripts can decide if they are running | |
677 | # interactively. |
|
678 | # interactively. | |
678 | sys.ps1 = 'In : ' |
|
679 | sys.ps1 = 'In : ' | |
679 | sys.ps2 = '...: ' |
|
680 | sys.ps2 = '...: ' | |
680 | sys.ps3 = 'Out: ' |
|
681 | sys.ps3 = 'Out: ' | |
681 |
|
682 | |||
682 | def init_display_formatter(self): |
|
683 | def init_display_formatter(self): | |
683 | self.display_formatter = DisplayFormatter(parent=self) |
|
684 | self.display_formatter = DisplayFormatter(parent=self) | |
684 | self.configurables.append(self.display_formatter) |
|
685 | self.configurables.append(self.display_formatter) | |
685 |
|
686 | |||
686 | def init_display_pub(self): |
|
687 | def init_display_pub(self): | |
687 | self.display_pub = self.display_pub_class(parent=self) |
|
688 | self.display_pub = self.display_pub_class(parent=self) | |
688 | self.configurables.append(self.display_pub) |
|
689 | self.configurables.append(self.display_pub) | |
689 |
|
690 | |||
690 | def init_data_pub(self): |
|
691 | def init_data_pub(self): | |
691 | if not self.data_pub_class: |
|
692 | if not self.data_pub_class: | |
692 | self.data_pub = None |
|
693 | self.data_pub = None | |
693 | return |
|
694 | return | |
694 | self.data_pub = self.data_pub_class(parent=self) |
|
695 | self.data_pub = self.data_pub_class(parent=self) | |
695 | self.configurables.append(self.data_pub) |
|
696 | self.configurables.append(self.data_pub) | |
696 |
|
697 | |||
697 | def init_displayhook(self): |
|
698 | def init_displayhook(self): | |
698 | # Initialize displayhook, set in/out prompts and printing system |
|
699 | # Initialize displayhook, set in/out prompts and printing system | |
699 | self.displayhook = self.displayhook_class( |
|
700 | self.displayhook = self.displayhook_class( | |
700 | parent=self, |
|
701 | parent=self, | |
701 | shell=self, |
|
702 | shell=self, | |
702 | cache_size=self.cache_size, |
|
703 | cache_size=self.cache_size, | |
703 | ) |
|
704 | ) | |
704 | self.configurables.append(self.displayhook) |
|
705 | self.configurables.append(self.displayhook) | |
705 | # This is a context manager that installs/revmoes the displayhook at |
|
706 | # This is a context manager that installs/revmoes the displayhook at | |
706 | # the appropriate time. |
|
707 | # the appropriate time. | |
707 | self.display_trap = DisplayTrap(hook=self.displayhook) |
|
708 | self.display_trap = DisplayTrap(hook=self.displayhook) | |
708 |
|
709 | |||
709 | def init_virtualenv(self): |
|
710 | def init_virtualenv(self): | |
710 | """Add a virtualenv to sys.path so the user can import modules from it. |
|
711 | """Add a virtualenv to sys.path so the user can import modules from it. | |
711 | This isn't perfect: it doesn't use the Python interpreter with which the |
|
712 | This isn't perfect: it doesn't use the Python interpreter with which the | |
712 | virtualenv was built, and it ignores the --no-site-packages option. A |
|
713 | virtualenv was built, and it ignores the --no-site-packages option. A | |
713 | warning will appear suggesting the user installs IPython in the |
|
714 | warning will appear suggesting the user installs IPython in the | |
714 | virtualenv, but for many cases, it probably works well enough. |
|
715 | virtualenv, but for many cases, it probably works well enough. | |
715 |
|
716 | |||
716 | Adapted from code snippets online. |
|
717 | Adapted from code snippets online. | |
717 |
|
718 | |||
718 | http://blog.ufsoft.org/2009/1/29/ipython-and-virtualenv |
|
719 | http://blog.ufsoft.org/2009/1/29/ipython-and-virtualenv | |
719 | """ |
|
720 | """ | |
720 | if 'VIRTUAL_ENV' not in os.environ: |
|
721 | if 'VIRTUAL_ENV' not in os.environ: | |
721 | # Not in a virtualenv |
|
722 | # Not in a virtualenv | |
722 | return |
|
723 | return | |
723 |
|
724 | |||
724 | # venv detection: |
|
725 | # venv detection: | |
725 | # stdlib venv may symlink sys.executable, so we can't use realpath. |
|
726 | # stdlib venv may symlink sys.executable, so we can't use realpath. | |
726 | # but others can symlink *to* the venv Python, so we can't just use sys.executable. |
|
727 | # but others can symlink *to* the venv Python, so we can't just use sys.executable. | |
727 | # So we just check every item in the symlink tree (generally <= 3) |
|
728 | # So we just check every item in the symlink tree (generally <= 3) | |
728 | p = os.path.normcase(sys.executable) |
|
729 | p = os.path.normcase(sys.executable) | |
729 | paths = [p] |
|
730 | paths = [p] | |
730 | while os.path.islink(p): |
|
731 | while os.path.islink(p): | |
731 | p = os.path.normcase(os.path.join(os.path.dirname(p), os.readlink(p))) |
|
732 | p = os.path.normcase(os.path.join(os.path.dirname(p), os.readlink(p))) | |
732 | paths.append(p) |
|
733 | paths.append(p) | |
733 | p_venv = os.path.normcase(os.environ['VIRTUAL_ENV']) |
|
734 | p_venv = os.path.normcase(os.environ['VIRTUAL_ENV']) | |
734 | if any(p.startswith(p_venv) for p in paths): |
|
735 | if any(p.startswith(p_venv) for p in paths): | |
735 | # Running properly in the virtualenv, don't need to do anything |
|
736 | # Running properly in the virtualenv, don't need to do anything | |
736 | return |
|
737 | return | |
737 |
|
738 | |||
738 | warn("Attempting to work in a virtualenv. If you encounter problems, please " |
|
739 | warn("Attempting to work in a virtualenv. If you encounter problems, please " | |
739 | "install IPython inside the virtualenv.") |
|
740 | "install IPython inside the virtualenv.") | |
740 | if sys.platform == "win32": |
|
741 | if sys.platform == "win32": | |
741 | virtual_env = os.path.join(os.environ['VIRTUAL_ENV'], 'Lib', 'site-packages') |
|
742 | virtual_env = os.path.join(os.environ['VIRTUAL_ENV'], 'Lib', 'site-packages') | |
742 | else: |
|
743 | else: | |
743 | virtual_env = os.path.join(os.environ['VIRTUAL_ENV'], 'lib', |
|
744 | virtual_env = os.path.join(os.environ['VIRTUAL_ENV'], 'lib', | |
744 | 'python%d.%d' % sys.version_info[:2], 'site-packages') |
|
745 | 'python%d.%d' % sys.version_info[:2], 'site-packages') | |
745 |
|
746 | |||
746 | import site |
|
747 | import site | |
747 | sys.path.insert(0, virtual_env) |
|
748 | sys.path.insert(0, virtual_env) | |
748 | site.addsitedir(virtual_env) |
|
749 | site.addsitedir(virtual_env) | |
749 |
|
750 | |||
750 | #------------------------------------------------------------------------- |
|
751 | #------------------------------------------------------------------------- | |
751 | # Things related to injections into the sys module |
|
752 | # Things related to injections into the sys module | |
752 | #------------------------------------------------------------------------- |
|
753 | #------------------------------------------------------------------------- | |
753 |
|
754 | |||
754 | def save_sys_module_state(self): |
|
755 | def save_sys_module_state(self): | |
755 | """Save the state of hooks in the sys module. |
|
756 | """Save the state of hooks in the sys module. | |
756 |
|
757 | |||
757 | This has to be called after self.user_module is created. |
|
758 | This has to be called after self.user_module is created. | |
758 | """ |
|
759 | """ | |
759 | self._orig_sys_module_state = {'stdin': sys.stdin, |
|
760 | self._orig_sys_module_state = {'stdin': sys.stdin, | |
760 | 'stdout': sys.stdout, |
|
761 | 'stdout': sys.stdout, | |
761 | 'stderr': sys.stderr, |
|
762 | 'stderr': sys.stderr, | |
762 | 'excepthook': sys.excepthook} |
|
763 | 'excepthook': sys.excepthook} | |
763 | self._orig_sys_modules_main_name = self.user_module.__name__ |
|
764 | self._orig_sys_modules_main_name = self.user_module.__name__ | |
764 | self._orig_sys_modules_main_mod = sys.modules.get(self.user_module.__name__) |
|
765 | self._orig_sys_modules_main_mod = sys.modules.get(self.user_module.__name__) | |
765 |
|
766 | |||
766 | def restore_sys_module_state(self): |
|
767 | def restore_sys_module_state(self): | |
767 | """Restore the state of the sys module.""" |
|
768 | """Restore the state of the sys module.""" | |
768 | try: |
|
769 | try: | |
769 | for k, v in iteritems(self._orig_sys_module_state): |
|
770 | for k, v in iteritems(self._orig_sys_module_state): | |
770 | setattr(sys, k, v) |
|
771 | setattr(sys, k, v) | |
771 | except AttributeError: |
|
772 | except AttributeError: | |
772 | pass |
|
773 | pass | |
773 | # Reset what what done in self.init_sys_modules |
|
774 | # Reset what what done in self.init_sys_modules | |
774 | if self._orig_sys_modules_main_mod is not None: |
|
775 | if self._orig_sys_modules_main_mod is not None: | |
775 | sys.modules[self._orig_sys_modules_main_name] = self._orig_sys_modules_main_mod |
|
776 | sys.modules[self._orig_sys_modules_main_name] = self._orig_sys_modules_main_mod | |
776 |
|
777 | |||
777 | #------------------------------------------------------------------------- |
|
778 | #------------------------------------------------------------------------- | |
778 | # Things related to the banner |
|
779 | # Things related to the banner | |
779 | #------------------------------------------------------------------------- |
|
780 | #------------------------------------------------------------------------- | |
780 |
|
781 | |||
781 | @property |
|
782 | @property | |
782 | def banner(self): |
|
783 | def banner(self): | |
783 | banner = self.banner1 |
|
784 | banner = self.banner1 | |
784 | if self.profile and self.profile != 'default': |
|
785 | if self.profile and self.profile != 'default': | |
785 | banner += '\nIPython profile: %s\n' % self.profile |
|
786 | banner += '\nIPython profile: %s\n' % self.profile | |
786 | if self.banner2: |
|
787 | if self.banner2: | |
787 | banner += '\n' + self.banner2 |
|
788 | banner += '\n' + self.banner2 | |
788 | return banner |
|
789 | return banner | |
789 |
|
790 | |||
790 | def show_banner(self, banner=None): |
|
791 | def show_banner(self, banner=None): | |
791 | if banner is None: |
|
792 | if banner is None: | |
792 | banner = self.banner |
|
793 | banner = self.banner | |
793 | sys.stdout.write(banner) |
|
794 | sys.stdout.write(banner) | |
794 |
|
795 | |||
795 | #------------------------------------------------------------------------- |
|
796 | #------------------------------------------------------------------------- | |
796 | # Things related to hooks |
|
797 | # Things related to hooks | |
797 | #------------------------------------------------------------------------- |
|
798 | #------------------------------------------------------------------------- | |
798 |
|
799 | |||
799 | def init_hooks(self): |
|
800 | def init_hooks(self): | |
800 | # hooks holds pointers used for user-side customizations |
|
801 | # hooks holds pointers used for user-side customizations | |
801 | self.hooks = Struct() |
|
802 | self.hooks = Struct() | |
802 |
|
803 | |||
803 | self.strdispatchers = {} |
|
804 | self.strdispatchers = {} | |
804 |
|
805 | |||
805 | # Set all default hooks, defined in the IPython.hooks module. |
|
806 | # Set all default hooks, defined in the IPython.hooks module. | |
806 | hooks = IPython.core.hooks |
|
807 | hooks = IPython.core.hooks | |
807 | for hook_name in hooks.__all__: |
|
808 | for hook_name in hooks.__all__: | |
808 | # default hooks have priority 100, i.e. low; user hooks should have |
|
809 | # default hooks have priority 100, i.e. low; user hooks should have | |
809 | # 0-100 priority |
|
810 | # 0-100 priority | |
810 | self.set_hook(hook_name,getattr(hooks,hook_name), 100, _warn_deprecated=False) |
|
811 | self.set_hook(hook_name,getattr(hooks,hook_name), 100, _warn_deprecated=False) | |
811 |
|
812 | |||
812 | if self.display_page: |
|
813 | if self.display_page: | |
813 | self.set_hook('show_in_pager', page.as_hook(page.display_page), 90) |
|
814 | self.set_hook('show_in_pager', page.as_hook(page.display_page), 90) | |
814 |
|
815 | |||
815 | def set_hook(self,name,hook, priority=50, str_key=None, re_key=None, |
|
816 | def set_hook(self,name,hook, priority=50, str_key=None, re_key=None, | |
816 | _warn_deprecated=True): |
|
817 | _warn_deprecated=True): | |
817 | """set_hook(name,hook) -> sets an internal IPython hook. |
|
818 | """set_hook(name,hook) -> sets an internal IPython hook. | |
818 |
|
819 | |||
819 | IPython exposes some of its internal API as user-modifiable hooks. By |
|
820 | IPython exposes some of its internal API as user-modifiable hooks. By | |
820 | adding your function to one of these hooks, you can modify IPython's |
|
821 | adding your function to one of these hooks, you can modify IPython's | |
821 | behavior to call at runtime your own routines.""" |
|
822 | behavior to call at runtime your own routines.""" | |
822 |
|
823 | |||
823 | # At some point in the future, this should validate the hook before it |
|
824 | # At some point in the future, this should validate the hook before it | |
824 | # accepts it. Probably at least check that the hook takes the number |
|
825 | # accepts it. Probably at least check that the hook takes the number | |
825 | # of args it's supposed to. |
|
826 | # of args it's supposed to. | |
826 |
|
827 | |||
827 | f = types.MethodType(hook,self) |
|
828 | f = types.MethodType(hook,self) | |
828 |
|
829 | |||
829 | # check if the hook is for strdispatcher first |
|
830 | # check if the hook is for strdispatcher first | |
830 | if str_key is not None: |
|
831 | if str_key is not None: | |
831 | sdp = self.strdispatchers.get(name, StrDispatch()) |
|
832 | sdp = self.strdispatchers.get(name, StrDispatch()) | |
832 | sdp.add_s(str_key, f, priority ) |
|
833 | sdp.add_s(str_key, f, priority ) | |
833 | self.strdispatchers[name] = sdp |
|
834 | self.strdispatchers[name] = sdp | |
834 | return |
|
835 | return | |
835 | if re_key is not None: |
|
836 | if re_key is not None: | |
836 | sdp = self.strdispatchers.get(name, StrDispatch()) |
|
837 | sdp = self.strdispatchers.get(name, StrDispatch()) | |
837 | sdp.add_re(re.compile(re_key), f, priority ) |
|
838 | sdp.add_re(re.compile(re_key), f, priority ) | |
838 | self.strdispatchers[name] = sdp |
|
839 | self.strdispatchers[name] = sdp | |
839 | return |
|
840 | return | |
840 |
|
841 | |||
841 | dp = getattr(self.hooks, name, None) |
|
842 | dp = getattr(self.hooks, name, None) | |
842 | if name not in IPython.core.hooks.__all__: |
|
843 | if name not in IPython.core.hooks.__all__: | |
843 | print("Warning! Hook '%s' is not one of %s" % \ |
|
844 | print("Warning! Hook '%s' is not one of %s" % \ | |
844 | (name, IPython.core.hooks.__all__ )) |
|
845 | (name, IPython.core.hooks.__all__ )) | |
845 |
|
846 | |||
846 | if _warn_deprecated and (name in IPython.core.hooks.deprecated): |
|
847 | if _warn_deprecated and (name in IPython.core.hooks.deprecated): | |
847 | alternative = IPython.core.hooks.deprecated[name] |
|
848 | alternative = IPython.core.hooks.deprecated[name] | |
848 | warn("Hook {} is deprecated. Use {} instead.".format(name, alternative)) |
|
849 | warn("Hook {} is deprecated. Use {} instead.".format(name, alternative)) | |
849 |
|
850 | |||
850 | if not dp: |
|
851 | if not dp: | |
851 | dp = IPython.core.hooks.CommandChainDispatcher() |
|
852 | dp = IPython.core.hooks.CommandChainDispatcher() | |
852 |
|
853 | |||
853 | try: |
|
854 | try: | |
854 | dp.add(f,priority) |
|
855 | dp.add(f,priority) | |
855 | except AttributeError: |
|
856 | except AttributeError: | |
856 | # it was not commandchain, plain old func - replace |
|
857 | # it was not commandchain, plain old func - replace | |
857 | dp = f |
|
858 | dp = f | |
858 |
|
859 | |||
859 | setattr(self.hooks,name, dp) |
|
860 | setattr(self.hooks,name, dp) | |
860 |
|
861 | |||
861 | #------------------------------------------------------------------------- |
|
862 | #------------------------------------------------------------------------- | |
862 | # Things related to events |
|
863 | # Things related to events | |
863 | #------------------------------------------------------------------------- |
|
864 | #------------------------------------------------------------------------- | |
864 |
|
865 | |||
865 | def init_events(self): |
|
866 | def init_events(self): | |
866 | self.events = EventManager(self, available_events) |
|
867 | self.events = EventManager(self, available_events) | |
867 |
|
868 | |||
868 | self.events.register("pre_execute", self._clear_warning_registry) |
|
869 | self.events.register("pre_execute", self._clear_warning_registry) | |
869 |
|
870 | |||
870 | def register_post_execute(self, func): |
|
871 | def register_post_execute(self, func): | |
871 | """DEPRECATED: Use ip.events.register('post_run_cell', func) |
|
872 | """DEPRECATED: Use ip.events.register('post_run_cell', func) | |
872 |
|
873 | |||
873 | Register a function for calling after code execution. |
|
874 | Register a function for calling after code execution. | |
874 | """ |
|
875 | """ | |
875 | warn("ip.register_post_execute is deprecated, use " |
|
876 | warn("ip.register_post_execute is deprecated, use " | |
876 | "ip.events.register('post_run_cell', func) instead.") |
|
877 | "ip.events.register('post_run_cell', func) instead.") | |
877 | self.events.register('post_run_cell', func) |
|
878 | self.events.register('post_run_cell', func) | |
878 |
|
879 | |||
879 | def _clear_warning_registry(self): |
|
880 | def _clear_warning_registry(self): | |
880 | # clear the warning registry, so that different code blocks with |
|
881 | # clear the warning registry, so that different code blocks with | |
881 | # overlapping line number ranges don't cause spurious suppression of |
|
882 | # overlapping line number ranges don't cause spurious suppression of | |
882 | # warnings (see gh-6611 for details) |
|
883 | # warnings (see gh-6611 for details) | |
883 | if "__warningregistry__" in self.user_global_ns: |
|
884 | if "__warningregistry__" in self.user_global_ns: | |
884 | del self.user_global_ns["__warningregistry__"] |
|
885 | del self.user_global_ns["__warningregistry__"] | |
885 |
|
886 | |||
886 | #------------------------------------------------------------------------- |
|
887 | #------------------------------------------------------------------------- | |
887 | # Things related to the "main" module |
|
888 | # Things related to the "main" module | |
888 | #------------------------------------------------------------------------- |
|
889 | #------------------------------------------------------------------------- | |
889 |
|
890 | |||
890 | def new_main_mod(self, filename, modname): |
|
891 | def new_main_mod(self, filename, modname): | |
891 | """Return a new 'main' module object for user code execution. |
|
892 | """Return a new 'main' module object for user code execution. | |
892 |
|
893 | |||
893 | ``filename`` should be the path of the script which will be run in the |
|
894 | ``filename`` should be the path of the script which will be run in the | |
894 | module. Requests with the same filename will get the same module, with |
|
895 | module. Requests with the same filename will get the same module, with | |
895 | its namespace cleared. |
|
896 | its namespace cleared. | |
896 |
|
897 | |||
897 | ``modname`` should be the module name - normally either '__main__' or |
|
898 | ``modname`` should be the module name - normally either '__main__' or | |
898 | the basename of the file without the extension. |
|
899 | the basename of the file without the extension. | |
899 |
|
900 | |||
900 | When scripts are executed via %run, we must keep a reference to their |
|
901 | When scripts are executed via %run, we must keep a reference to their | |
901 | __main__ module around so that Python doesn't |
|
902 | __main__ module around so that Python doesn't | |
902 | clear it, rendering references to module globals useless. |
|
903 | clear it, rendering references to module globals useless. | |
903 |
|
904 | |||
904 | This method keeps said reference in a private dict, keyed by the |
|
905 | This method keeps said reference in a private dict, keyed by the | |
905 | absolute path of the script. This way, for multiple executions of the |
|
906 | absolute path of the script. This way, for multiple executions of the | |
906 | same script we only keep one copy of the namespace (the last one), |
|
907 | same script we only keep one copy of the namespace (the last one), | |
907 | thus preventing memory leaks from old references while allowing the |
|
908 | thus preventing memory leaks from old references while allowing the | |
908 | objects from the last execution to be accessible. |
|
909 | objects from the last execution to be accessible. | |
909 | """ |
|
910 | """ | |
910 | filename = os.path.abspath(filename) |
|
911 | filename = os.path.abspath(filename) | |
911 | try: |
|
912 | try: | |
912 | main_mod = self._main_mod_cache[filename] |
|
913 | main_mod = self._main_mod_cache[filename] | |
913 | except KeyError: |
|
914 | except KeyError: | |
914 | main_mod = self._main_mod_cache[filename] = types.ModuleType( |
|
915 | main_mod = self._main_mod_cache[filename] = types.ModuleType( | |
915 | py3compat.cast_bytes_py2(modname), |
|
916 | py3compat.cast_bytes_py2(modname), | |
916 | doc="Module created for script run in IPython") |
|
917 | doc="Module created for script run in IPython") | |
917 | else: |
|
918 | else: | |
918 | main_mod.__dict__.clear() |
|
919 | main_mod.__dict__.clear() | |
919 | main_mod.__name__ = modname |
|
920 | main_mod.__name__ = modname | |
920 |
|
921 | |||
921 | main_mod.__file__ = filename |
|
922 | main_mod.__file__ = filename | |
922 | # It seems pydoc (and perhaps others) needs any module instance to |
|
923 | # It seems pydoc (and perhaps others) needs any module instance to | |
923 | # implement a __nonzero__ method |
|
924 | # implement a __nonzero__ method | |
924 | main_mod.__nonzero__ = lambda : True |
|
925 | main_mod.__nonzero__ = lambda : True | |
925 |
|
926 | |||
926 | return main_mod |
|
927 | return main_mod | |
927 |
|
928 | |||
928 | def clear_main_mod_cache(self): |
|
929 | def clear_main_mod_cache(self): | |
929 | """Clear the cache of main modules. |
|
930 | """Clear the cache of main modules. | |
930 |
|
931 | |||
931 | Mainly for use by utilities like %reset. |
|
932 | Mainly for use by utilities like %reset. | |
932 |
|
933 | |||
933 | Examples |
|
934 | Examples | |
934 | -------- |
|
935 | -------- | |
935 |
|
936 | |||
936 | In [15]: import IPython |
|
937 | In [15]: import IPython | |
937 |
|
938 | |||
938 | In [16]: m = _ip.new_main_mod(IPython.__file__, 'IPython') |
|
939 | In [16]: m = _ip.new_main_mod(IPython.__file__, 'IPython') | |
939 |
|
940 | |||
940 | In [17]: len(_ip._main_mod_cache) > 0 |
|
941 | In [17]: len(_ip._main_mod_cache) > 0 | |
941 | Out[17]: True |
|
942 | Out[17]: True | |
942 |
|
943 | |||
943 | In [18]: _ip.clear_main_mod_cache() |
|
944 | In [18]: _ip.clear_main_mod_cache() | |
944 |
|
945 | |||
945 | In [19]: len(_ip._main_mod_cache) == 0 |
|
946 | In [19]: len(_ip._main_mod_cache) == 0 | |
946 | Out[19]: True |
|
947 | Out[19]: True | |
947 | """ |
|
948 | """ | |
948 | self._main_mod_cache.clear() |
|
949 | self._main_mod_cache.clear() | |
949 |
|
950 | |||
950 | #------------------------------------------------------------------------- |
|
951 | #------------------------------------------------------------------------- | |
951 | # Things related to debugging |
|
952 | # Things related to debugging | |
952 | #------------------------------------------------------------------------- |
|
953 | #------------------------------------------------------------------------- | |
953 |
|
954 | |||
954 | def init_pdb(self): |
|
955 | def init_pdb(self): | |
955 | # Set calling of pdb on exceptions |
|
956 | # Set calling of pdb on exceptions | |
956 | # self.call_pdb is a property |
|
957 | # self.call_pdb is a property | |
957 | self.call_pdb = self.pdb |
|
958 | self.call_pdb = self.pdb | |
958 |
|
959 | |||
959 | def _get_call_pdb(self): |
|
960 | def _get_call_pdb(self): | |
960 | return self._call_pdb |
|
961 | return self._call_pdb | |
961 |
|
962 | |||
962 | def _set_call_pdb(self,val): |
|
963 | def _set_call_pdb(self,val): | |
963 |
|
964 | |||
964 | if val not in (0,1,False,True): |
|
965 | if val not in (0,1,False,True): | |
965 | raise ValueError('new call_pdb value must be boolean') |
|
966 | raise ValueError('new call_pdb value must be boolean') | |
966 |
|
967 | |||
967 | # store value in instance |
|
968 | # store value in instance | |
968 | self._call_pdb = val |
|
969 | self._call_pdb = val | |
969 |
|
970 | |||
970 | # notify the actual exception handlers |
|
971 | # notify the actual exception handlers | |
971 | self.InteractiveTB.call_pdb = val |
|
972 | self.InteractiveTB.call_pdb = val | |
972 |
|
973 | |||
973 | call_pdb = property(_get_call_pdb,_set_call_pdb,None, |
|
974 | call_pdb = property(_get_call_pdb,_set_call_pdb,None, | |
974 | 'Control auto-activation of pdb at exceptions') |
|
975 | 'Control auto-activation of pdb at exceptions') | |
975 |
|
976 | |||
976 | def debugger(self,force=False): |
|
977 | def debugger(self,force=False): | |
977 | """Call the pdb debugger. |
|
978 | """Call the pdb debugger. | |
978 |
|
979 | |||
979 | Keywords: |
|
980 | Keywords: | |
980 |
|
981 | |||
981 | - force(False): by default, this routine checks the instance call_pdb |
|
982 | - force(False): by default, this routine checks the instance call_pdb | |
982 | flag and does not actually invoke the debugger if the flag is false. |
|
983 | flag and does not actually invoke the debugger if the flag is false. | |
983 | The 'force' option forces the debugger to activate even if the flag |
|
984 | The 'force' option forces the debugger to activate even if the flag | |
984 | is false. |
|
985 | is false. | |
985 | """ |
|
986 | """ | |
986 |
|
987 | |||
987 | if not (force or self.call_pdb): |
|
988 | if not (force or self.call_pdb): | |
988 | return |
|
989 | return | |
989 |
|
990 | |||
990 | if not hasattr(sys,'last_traceback'): |
|
991 | if not hasattr(sys,'last_traceback'): | |
991 | error('No traceback has been produced, nothing to debug.') |
|
992 | error('No traceback has been produced, nothing to debug.') | |
992 | return |
|
993 | return | |
993 |
|
994 | |||
994 |
|
995 | |||
995 | with self.readline_no_record: |
|
996 | with self.readline_no_record: | |
996 | self.InteractiveTB.debugger(force=True) |
|
997 | self.InteractiveTB.debugger(force=True) | |
997 |
|
998 | |||
998 | #------------------------------------------------------------------------- |
|
999 | #------------------------------------------------------------------------- | |
999 | # Things related to IPython's various namespaces |
|
1000 | # Things related to IPython's various namespaces | |
1000 | #------------------------------------------------------------------------- |
|
1001 | #------------------------------------------------------------------------- | |
1001 | default_user_namespaces = True |
|
1002 | default_user_namespaces = True | |
1002 |
|
1003 | |||
1003 | def init_create_namespaces(self, user_module=None, user_ns=None): |
|
1004 | def init_create_namespaces(self, user_module=None, user_ns=None): | |
1004 | # Create the namespace where the user will operate. user_ns is |
|
1005 | # Create the namespace where the user will operate. user_ns is | |
1005 | # normally the only one used, and it is passed to the exec calls as |
|
1006 | # normally the only one used, and it is passed to the exec calls as | |
1006 | # the locals argument. But we do carry a user_global_ns namespace |
|
1007 | # the locals argument. But we do carry a user_global_ns namespace | |
1007 | # given as the exec 'globals' argument, This is useful in embedding |
|
1008 | # given as the exec 'globals' argument, This is useful in embedding | |
1008 | # situations where the ipython shell opens in a context where the |
|
1009 | # situations where the ipython shell opens in a context where the | |
1009 | # distinction between locals and globals is meaningful. For |
|
1010 | # distinction between locals and globals is meaningful. For | |
1010 | # non-embedded contexts, it is just the same object as the user_ns dict. |
|
1011 | # non-embedded contexts, it is just the same object as the user_ns dict. | |
1011 |
|
1012 | |||
1012 | # FIXME. For some strange reason, __builtins__ is showing up at user |
|
1013 | # FIXME. For some strange reason, __builtins__ is showing up at user | |
1013 | # level as a dict instead of a module. This is a manual fix, but I |
|
1014 | # level as a dict instead of a module. This is a manual fix, but I | |
1014 | # should really track down where the problem is coming from. Alex |
|
1015 | # should really track down where the problem is coming from. Alex | |
1015 | # Schmolck reported this problem first. |
|
1016 | # Schmolck reported this problem first. | |
1016 |
|
1017 | |||
1017 | # A useful post by Alex Martelli on this topic: |
|
1018 | # A useful post by Alex Martelli on this topic: | |
1018 | # Re: inconsistent value from __builtins__ |
|
1019 | # Re: inconsistent value from __builtins__ | |
1019 | # Von: Alex Martelli <aleaxit@yahoo.com> |
|
1020 | # Von: Alex Martelli <aleaxit@yahoo.com> | |
1020 | # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends |
|
1021 | # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends | |
1021 | # Gruppen: comp.lang.python |
|
1022 | # Gruppen: comp.lang.python | |
1022 |
|
1023 | |||
1023 | # Michael Hohn <hohn@hooknose.lbl.gov> wrote: |
|
1024 | # Michael Hohn <hohn@hooknose.lbl.gov> wrote: | |
1024 | # > >>> print type(builtin_check.get_global_binding('__builtins__')) |
|
1025 | # > >>> print type(builtin_check.get_global_binding('__builtins__')) | |
1025 | # > <type 'dict'> |
|
1026 | # > <type 'dict'> | |
1026 | # > >>> print type(__builtins__) |
|
1027 | # > >>> print type(__builtins__) | |
1027 | # > <type 'module'> |
|
1028 | # > <type 'module'> | |
1028 | # > Is this difference in return value intentional? |
|
1029 | # > Is this difference in return value intentional? | |
1029 |
|
1030 | |||
1030 | # Well, it's documented that '__builtins__' can be either a dictionary |
|
1031 | # Well, it's documented that '__builtins__' can be either a dictionary | |
1031 | # or a module, and it's been that way for a long time. Whether it's |
|
1032 | # or a module, and it's been that way for a long time. Whether it's | |
1032 | # intentional (or sensible), I don't know. In any case, the idea is |
|
1033 | # intentional (or sensible), I don't know. In any case, the idea is | |
1033 | # that if you need to access the built-in namespace directly, you |
|
1034 | # that if you need to access the built-in namespace directly, you | |
1034 | # should start with "import __builtin__" (note, no 's') which will |
|
1035 | # should start with "import __builtin__" (note, no 's') which will | |
1035 | # definitely give you a module. Yeah, it's somewhat confusing:-(. |
|
1036 | # definitely give you a module. Yeah, it's somewhat confusing:-(. | |
1036 |
|
1037 | |||
1037 | # These routines return a properly built module and dict as needed by |
|
1038 | # These routines return a properly built module and dict as needed by | |
1038 | # the rest of the code, and can also be used by extension writers to |
|
1039 | # the rest of the code, and can also be used by extension writers to | |
1039 | # generate properly initialized namespaces. |
|
1040 | # generate properly initialized namespaces. | |
1040 | if (user_ns is not None) or (user_module is not None): |
|
1041 | if (user_ns is not None) or (user_module is not None): | |
1041 | self.default_user_namespaces = False |
|
1042 | self.default_user_namespaces = False | |
1042 | self.user_module, self.user_ns = self.prepare_user_module(user_module, user_ns) |
|
1043 | self.user_module, self.user_ns = self.prepare_user_module(user_module, user_ns) | |
1043 |
|
1044 | |||
1044 | # A record of hidden variables we have added to the user namespace, so |
|
1045 | # A record of hidden variables we have added to the user namespace, so | |
1045 | # we can list later only variables defined in actual interactive use. |
|
1046 | # we can list later only variables defined in actual interactive use. | |
1046 | self.user_ns_hidden = {} |
|
1047 | self.user_ns_hidden = {} | |
1047 |
|
1048 | |||
1048 | # Now that FakeModule produces a real module, we've run into a nasty |
|
1049 | # Now that FakeModule produces a real module, we've run into a nasty | |
1049 | # problem: after script execution (via %run), the module where the user |
|
1050 | # problem: after script execution (via %run), the module where the user | |
1050 | # code ran is deleted. Now that this object is a true module (needed |
|
1051 | # code ran is deleted. Now that this object is a true module (needed | |
1051 | # so doctest and other tools work correctly), the Python module |
|
1052 | # so doctest and other tools work correctly), the Python module | |
1052 | # teardown mechanism runs over it, and sets to None every variable |
|
1053 | # teardown mechanism runs over it, and sets to None every variable | |
1053 | # present in that module. Top-level references to objects from the |
|
1054 | # present in that module. Top-level references to objects from the | |
1054 | # script survive, because the user_ns is updated with them. However, |
|
1055 | # script survive, because the user_ns is updated with them. However, | |
1055 | # calling functions defined in the script that use other things from |
|
1056 | # calling functions defined in the script that use other things from | |
1056 | # the script will fail, because the function's closure had references |
|
1057 | # the script will fail, because the function's closure had references | |
1057 | # to the original objects, which are now all None. So we must protect |
|
1058 | # to the original objects, which are now all None. So we must protect | |
1058 | # these modules from deletion by keeping a cache. |
|
1059 | # these modules from deletion by keeping a cache. | |
1059 | # |
|
1060 | # | |
1060 | # To avoid keeping stale modules around (we only need the one from the |
|
1061 | # To avoid keeping stale modules around (we only need the one from the | |
1061 | # last run), we use a dict keyed with the full path to the script, so |
|
1062 | # last run), we use a dict keyed with the full path to the script, so | |
1062 | # only the last version of the module is held in the cache. Note, |
|
1063 | # only the last version of the module is held in the cache. Note, | |
1063 | # however, that we must cache the module *namespace contents* (their |
|
1064 | # however, that we must cache the module *namespace contents* (their | |
1064 | # __dict__). Because if we try to cache the actual modules, old ones |
|
1065 | # __dict__). Because if we try to cache the actual modules, old ones | |
1065 | # (uncached) could be destroyed while still holding references (such as |
|
1066 | # (uncached) could be destroyed while still holding references (such as | |
1066 | # those held by GUI objects that tend to be long-lived)> |
|
1067 | # those held by GUI objects that tend to be long-lived)> | |
1067 | # |
|
1068 | # | |
1068 | # The %reset command will flush this cache. See the cache_main_mod() |
|
1069 | # The %reset command will flush this cache. See the cache_main_mod() | |
1069 | # and clear_main_mod_cache() methods for details on use. |
|
1070 | # and clear_main_mod_cache() methods for details on use. | |
1070 |
|
1071 | |||
1071 | # This is the cache used for 'main' namespaces |
|
1072 | # This is the cache used for 'main' namespaces | |
1072 | self._main_mod_cache = {} |
|
1073 | self._main_mod_cache = {} | |
1073 |
|
1074 | |||
1074 | # A table holding all the namespaces IPython deals with, so that |
|
1075 | # A table holding all the namespaces IPython deals with, so that | |
1075 | # introspection facilities can search easily. |
|
1076 | # introspection facilities can search easily. | |
1076 | self.ns_table = {'user_global':self.user_module.__dict__, |
|
1077 | self.ns_table = {'user_global':self.user_module.__dict__, | |
1077 | 'user_local':self.user_ns, |
|
1078 | 'user_local':self.user_ns, | |
1078 | 'builtin':builtin_mod.__dict__ |
|
1079 | 'builtin':builtin_mod.__dict__ | |
1079 | } |
|
1080 | } | |
1080 |
|
1081 | |||
1081 | @property |
|
1082 | @property | |
1082 | def user_global_ns(self): |
|
1083 | def user_global_ns(self): | |
1083 | return self.user_module.__dict__ |
|
1084 | return self.user_module.__dict__ | |
1084 |
|
1085 | |||
1085 | def prepare_user_module(self, user_module=None, user_ns=None): |
|
1086 | def prepare_user_module(self, user_module=None, user_ns=None): | |
1086 | """Prepare the module and namespace in which user code will be run. |
|
1087 | """Prepare the module and namespace in which user code will be run. | |
1087 |
|
1088 | |||
1088 | When IPython is started normally, both parameters are None: a new module |
|
1089 | When IPython is started normally, both parameters are None: a new module | |
1089 | is created automatically, and its __dict__ used as the namespace. |
|
1090 | is created automatically, and its __dict__ used as the namespace. | |
1090 |
|
1091 | |||
1091 | If only user_module is provided, its __dict__ is used as the namespace. |
|
1092 | If only user_module is provided, its __dict__ is used as the namespace. | |
1092 | If only user_ns is provided, a dummy module is created, and user_ns |
|
1093 | If only user_ns is provided, a dummy module is created, and user_ns | |
1093 | becomes the global namespace. If both are provided (as they may be |
|
1094 | becomes the global namespace. If both are provided (as they may be | |
1094 | when embedding), user_ns is the local namespace, and user_module |
|
1095 | when embedding), user_ns is the local namespace, and user_module | |
1095 | provides the global namespace. |
|
1096 | provides the global namespace. | |
1096 |
|
1097 | |||
1097 | Parameters |
|
1098 | Parameters | |
1098 | ---------- |
|
1099 | ---------- | |
1099 | user_module : module, optional |
|
1100 | user_module : module, optional | |
1100 | The current user module in which IPython is being run. If None, |
|
1101 | The current user module in which IPython is being run. If None, | |
1101 | a clean module will be created. |
|
1102 | a clean module will be created. | |
1102 | user_ns : dict, optional |
|
1103 | user_ns : dict, optional | |
1103 | A namespace in which to run interactive commands. |
|
1104 | A namespace in which to run interactive commands. | |
1104 |
|
1105 | |||
1105 | Returns |
|
1106 | Returns | |
1106 | ------- |
|
1107 | ------- | |
1107 | A tuple of user_module and user_ns, each properly initialised. |
|
1108 | A tuple of user_module and user_ns, each properly initialised. | |
1108 | """ |
|
1109 | """ | |
1109 | if user_module is None and user_ns is not None: |
|
1110 | if user_module is None and user_ns is not None: | |
1110 | user_ns.setdefault("__name__", "__main__") |
|
1111 | user_ns.setdefault("__name__", "__main__") | |
1111 | user_module = DummyMod() |
|
1112 | user_module = DummyMod() | |
1112 | user_module.__dict__ = user_ns |
|
1113 | user_module.__dict__ = user_ns | |
1113 |
|
1114 | |||
1114 | if user_module is None: |
|
1115 | if user_module is None: | |
1115 | user_module = types.ModuleType("__main__", |
|
1116 | user_module = types.ModuleType("__main__", | |
1116 | doc="Automatically created module for IPython interactive environment") |
|
1117 | doc="Automatically created module for IPython interactive environment") | |
1117 |
|
1118 | |||
1118 | # We must ensure that __builtin__ (without the final 's') is always |
|
1119 | # We must ensure that __builtin__ (without the final 's') is always | |
1119 | # available and pointing to the __builtin__ *module*. For more details: |
|
1120 | # available and pointing to the __builtin__ *module*. For more details: | |
1120 | # http://mail.python.org/pipermail/python-dev/2001-April/014068.html |
|
1121 | # http://mail.python.org/pipermail/python-dev/2001-April/014068.html | |
1121 | user_module.__dict__.setdefault('__builtin__', builtin_mod) |
|
1122 | user_module.__dict__.setdefault('__builtin__', builtin_mod) | |
1122 | user_module.__dict__.setdefault('__builtins__', builtin_mod) |
|
1123 | user_module.__dict__.setdefault('__builtins__', builtin_mod) | |
1123 |
|
1124 | |||
1124 | if user_ns is None: |
|
1125 | if user_ns is None: | |
1125 | user_ns = user_module.__dict__ |
|
1126 | user_ns = user_module.__dict__ | |
1126 |
|
1127 | |||
1127 | return user_module, user_ns |
|
1128 | return user_module, user_ns | |
1128 |
|
1129 | |||
1129 | def init_sys_modules(self): |
|
1130 | def init_sys_modules(self): | |
1130 | # We need to insert into sys.modules something that looks like a |
|
1131 | # We need to insert into sys.modules something that looks like a | |
1131 | # module but which accesses the IPython namespace, for shelve and |
|
1132 | # module but which accesses the IPython namespace, for shelve and | |
1132 | # pickle to work interactively. Normally they rely on getting |
|
1133 | # pickle to work interactively. Normally they rely on getting | |
1133 | # everything out of __main__, but for embedding purposes each IPython |
|
1134 | # everything out of __main__, but for embedding purposes each IPython | |
1134 | # instance has its own private namespace, so we can't go shoving |
|
1135 | # instance has its own private namespace, so we can't go shoving | |
1135 | # everything into __main__. |
|
1136 | # everything into __main__. | |
1136 |
|
1137 | |||
1137 | # note, however, that we should only do this for non-embedded |
|
1138 | # note, however, that we should only do this for non-embedded | |
1138 | # ipythons, which really mimic the __main__.__dict__ with their own |
|
1139 | # ipythons, which really mimic the __main__.__dict__ with their own | |
1139 | # namespace. Embedded instances, on the other hand, should not do |
|
1140 | # namespace. Embedded instances, on the other hand, should not do | |
1140 | # this because they need to manage the user local/global namespaces |
|
1141 | # this because they need to manage the user local/global namespaces | |
1141 | # only, but they live within a 'normal' __main__ (meaning, they |
|
1142 | # only, but they live within a 'normal' __main__ (meaning, they | |
1142 | # shouldn't overtake the execution environment of the script they're |
|
1143 | # shouldn't overtake the execution environment of the script they're | |
1143 | # embedded in). |
|
1144 | # embedded in). | |
1144 |
|
1145 | |||
1145 | # This is overridden in the InteractiveShellEmbed subclass to a no-op. |
|
1146 | # This is overridden in the InteractiveShellEmbed subclass to a no-op. | |
1146 | main_name = self.user_module.__name__ |
|
1147 | main_name = self.user_module.__name__ | |
1147 | sys.modules[main_name] = self.user_module |
|
1148 | sys.modules[main_name] = self.user_module | |
1148 |
|
1149 | |||
1149 | def init_user_ns(self): |
|
1150 | def init_user_ns(self): | |
1150 | """Initialize all user-visible namespaces to their minimum defaults. |
|
1151 | """Initialize all user-visible namespaces to their minimum defaults. | |
1151 |
|
1152 | |||
1152 | Certain history lists are also initialized here, as they effectively |
|
1153 | Certain history lists are also initialized here, as they effectively | |
1153 | act as user namespaces. |
|
1154 | act as user namespaces. | |
1154 |
|
1155 | |||
1155 | Notes |
|
1156 | Notes | |
1156 | ----- |
|
1157 | ----- | |
1157 | All data structures here are only filled in, they are NOT reset by this |
|
1158 | All data structures here are only filled in, they are NOT reset by this | |
1158 | method. If they were not empty before, data will simply be added to |
|
1159 | method. If they were not empty before, data will simply be added to | |
1159 | therm. |
|
1160 | therm. | |
1160 | """ |
|
1161 | """ | |
1161 | # This function works in two parts: first we put a few things in |
|
1162 | # This function works in two parts: first we put a few things in | |
1162 | # user_ns, and we sync that contents into user_ns_hidden so that these |
|
1163 | # user_ns, and we sync that contents into user_ns_hidden so that these | |
1163 | # initial variables aren't shown by %who. After the sync, we add the |
|
1164 | # initial variables aren't shown by %who. After the sync, we add the | |
1164 | # rest of what we *do* want the user to see with %who even on a new |
|
1165 | # rest of what we *do* want the user to see with %who even on a new | |
1165 | # session (probably nothing, so they really only see their own stuff) |
|
1166 | # session (probably nothing, so they really only see their own stuff) | |
1166 |
|
1167 | |||
1167 | # The user dict must *always* have a __builtin__ reference to the |
|
1168 | # The user dict must *always* have a __builtin__ reference to the | |
1168 | # Python standard __builtin__ namespace, which must be imported. |
|
1169 | # Python standard __builtin__ namespace, which must be imported. | |
1169 | # This is so that certain operations in prompt evaluation can be |
|
1170 | # This is so that certain operations in prompt evaluation can be | |
1170 | # reliably executed with builtins. Note that we can NOT use |
|
1171 | # reliably executed with builtins. Note that we can NOT use | |
1171 | # __builtins__ (note the 's'), because that can either be a dict or a |
|
1172 | # __builtins__ (note the 's'), because that can either be a dict or a | |
1172 | # module, and can even mutate at runtime, depending on the context |
|
1173 | # module, and can even mutate at runtime, depending on the context | |
1173 | # (Python makes no guarantees on it). In contrast, __builtin__ is |
|
1174 | # (Python makes no guarantees on it). In contrast, __builtin__ is | |
1174 | # always a module object, though it must be explicitly imported. |
|
1175 | # always a module object, though it must be explicitly imported. | |
1175 |
|
1176 | |||
1176 | # For more details: |
|
1177 | # For more details: | |
1177 | # http://mail.python.org/pipermail/python-dev/2001-April/014068.html |
|
1178 | # http://mail.python.org/pipermail/python-dev/2001-April/014068.html | |
1178 | ns = dict() |
|
1179 | ns = dict() | |
1179 |
|
1180 | |||
1180 | # make global variables for user access to the histories |
|
1181 | # make global variables for user access to the histories | |
1181 | ns['_ih'] = self.history_manager.input_hist_parsed |
|
1182 | ns['_ih'] = self.history_manager.input_hist_parsed | |
1182 | ns['_oh'] = self.history_manager.output_hist |
|
1183 | ns['_oh'] = self.history_manager.output_hist | |
1183 | ns['_dh'] = self.history_manager.dir_hist |
|
1184 | ns['_dh'] = self.history_manager.dir_hist | |
1184 |
|
1185 | |||
1185 | ns['_sh'] = shadowns |
|
1186 | ns['_sh'] = shadowns | |
1186 |
|
1187 | |||
1187 | # user aliases to input and output histories. These shouldn't show up |
|
1188 | # user aliases to input and output histories. These shouldn't show up | |
1188 | # in %who, as they can have very large reprs. |
|
1189 | # in %who, as they can have very large reprs. | |
1189 | ns['In'] = self.history_manager.input_hist_parsed |
|
1190 | ns['In'] = self.history_manager.input_hist_parsed | |
1190 | ns['Out'] = self.history_manager.output_hist |
|
1191 | ns['Out'] = self.history_manager.output_hist | |
1191 |
|
1192 | |||
1192 | # Store myself as the public api!!! |
|
1193 | # Store myself as the public api!!! | |
1193 | ns['get_ipython'] = self.get_ipython |
|
1194 | ns['get_ipython'] = self.get_ipython | |
1194 |
|
1195 | |||
1195 | ns['exit'] = self.exiter |
|
1196 | ns['exit'] = self.exiter | |
1196 | ns['quit'] = self.exiter |
|
1197 | ns['quit'] = self.exiter | |
1197 |
|
1198 | |||
1198 | # Sync what we've added so far to user_ns_hidden so these aren't seen |
|
1199 | # Sync what we've added so far to user_ns_hidden so these aren't seen | |
1199 | # by %who |
|
1200 | # by %who | |
1200 | self.user_ns_hidden.update(ns) |
|
1201 | self.user_ns_hidden.update(ns) | |
1201 |
|
1202 | |||
1202 | # Anything put into ns now would show up in %who. Think twice before |
|
1203 | # Anything put into ns now would show up in %who. Think twice before | |
1203 | # putting anything here, as we really want %who to show the user their |
|
1204 | # putting anything here, as we really want %who to show the user their | |
1204 | # stuff, not our variables. |
|
1205 | # stuff, not our variables. | |
1205 |
|
1206 | |||
1206 | # Finally, update the real user's namespace |
|
1207 | # Finally, update the real user's namespace | |
1207 | self.user_ns.update(ns) |
|
1208 | self.user_ns.update(ns) | |
1208 |
|
1209 | |||
1209 | @property |
|
1210 | @property | |
1210 | def all_ns_refs(self): |
|
1211 | def all_ns_refs(self): | |
1211 | """Get a list of references to all the namespace dictionaries in which |
|
1212 | """Get a list of references to all the namespace dictionaries in which | |
1212 | IPython might store a user-created object. |
|
1213 | IPython might store a user-created object. | |
1213 |
|
1214 | |||
1214 | Note that this does not include the displayhook, which also caches |
|
1215 | Note that this does not include the displayhook, which also caches | |
1215 | objects from the output.""" |
|
1216 | objects from the output.""" | |
1216 | return [self.user_ns, self.user_global_ns, self.user_ns_hidden] + \ |
|
1217 | return [self.user_ns, self.user_global_ns, self.user_ns_hidden] + \ | |
1217 | [m.__dict__ for m in self._main_mod_cache.values()] |
|
1218 | [m.__dict__ for m in self._main_mod_cache.values()] | |
1218 |
|
1219 | |||
1219 | def reset(self, new_session=True): |
|
1220 | def reset(self, new_session=True): | |
1220 | """Clear all internal namespaces, and attempt to release references to |
|
1221 | """Clear all internal namespaces, and attempt to release references to | |
1221 | user objects. |
|
1222 | user objects. | |
1222 |
|
1223 | |||
1223 | If new_session is True, a new history session will be opened. |
|
1224 | If new_session is True, a new history session will be opened. | |
1224 | """ |
|
1225 | """ | |
1225 | # Clear histories |
|
1226 | # Clear histories | |
1226 | self.history_manager.reset(new_session) |
|
1227 | self.history_manager.reset(new_session) | |
1227 | # Reset counter used to index all histories |
|
1228 | # Reset counter used to index all histories | |
1228 | if new_session: |
|
1229 | if new_session: | |
1229 | self.execution_count = 1 |
|
1230 | self.execution_count = 1 | |
1230 |
|
1231 | |||
1231 | # Flush cached output items |
|
1232 | # Flush cached output items | |
1232 | if self.displayhook.do_full_cache: |
|
1233 | if self.displayhook.do_full_cache: | |
1233 | self.displayhook.flush() |
|
1234 | self.displayhook.flush() | |
1234 |
|
1235 | |||
1235 | # The main execution namespaces must be cleared very carefully, |
|
1236 | # The main execution namespaces must be cleared very carefully, | |
1236 | # skipping the deletion of the builtin-related keys, because doing so |
|
1237 | # skipping the deletion of the builtin-related keys, because doing so | |
1237 | # would cause errors in many object's __del__ methods. |
|
1238 | # would cause errors in many object's __del__ methods. | |
1238 | if self.user_ns is not self.user_global_ns: |
|
1239 | if self.user_ns is not self.user_global_ns: | |
1239 | self.user_ns.clear() |
|
1240 | self.user_ns.clear() | |
1240 | ns = self.user_global_ns |
|
1241 | ns = self.user_global_ns | |
1241 | drop_keys = set(ns.keys()) |
|
1242 | drop_keys = set(ns.keys()) | |
1242 | drop_keys.discard('__builtin__') |
|
1243 | drop_keys.discard('__builtin__') | |
1243 | drop_keys.discard('__builtins__') |
|
1244 | drop_keys.discard('__builtins__') | |
1244 | drop_keys.discard('__name__') |
|
1245 | drop_keys.discard('__name__') | |
1245 | for k in drop_keys: |
|
1246 | for k in drop_keys: | |
1246 | del ns[k] |
|
1247 | del ns[k] | |
1247 |
|
1248 | |||
1248 | self.user_ns_hidden.clear() |
|
1249 | self.user_ns_hidden.clear() | |
1249 |
|
1250 | |||
1250 | # Restore the user namespaces to minimal usability |
|
1251 | # Restore the user namespaces to minimal usability | |
1251 | self.init_user_ns() |
|
1252 | self.init_user_ns() | |
1252 |
|
1253 | |||
1253 | # Restore the default and user aliases |
|
1254 | # Restore the default and user aliases | |
1254 | self.alias_manager.clear_aliases() |
|
1255 | self.alias_manager.clear_aliases() | |
1255 | self.alias_manager.init_aliases() |
|
1256 | self.alias_manager.init_aliases() | |
1256 |
|
1257 | |||
1257 | # Flush the private list of module references kept for script |
|
1258 | # Flush the private list of module references kept for script | |
1258 | # execution protection |
|
1259 | # execution protection | |
1259 | self.clear_main_mod_cache() |
|
1260 | self.clear_main_mod_cache() | |
1260 |
|
1261 | |||
1261 | def del_var(self, varname, by_name=False): |
|
1262 | def del_var(self, varname, by_name=False): | |
1262 | """Delete a variable from the various namespaces, so that, as |
|
1263 | """Delete a variable from the various namespaces, so that, as | |
1263 | far as possible, we're not keeping any hidden references to it. |
|
1264 | far as possible, we're not keeping any hidden references to it. | |
1264 |
|
1265 | |||
1265 | Parameters |
|
1266 | Parameters | |
1266 | ---------- |
|
1267 | ---------- | |
1267 | varname : str |
|
1268 | varname : str | |
1268 | The name of the variable to delete. |
|
1269 | The name of the variable to delete. | |
1269 | by_name : bool |
|
1270 | by_name : bool | |
1270 | If True, delete variables with the given name in each |
|
1271 | If True, delete variables with the given name in each | |
1271 | namespace. If False (default), find the variable in the user |
|
1272 | namespace. If False (default), find the variable in the user | |
1272 | namespace, and delete references to it. |
|
1273 | namespace, and delete references to it. | |
1273 | """ |
|
1274 | """ | |
1274 | if varname in ('__builtin__', '__builtins__'): |
|
1275 | if varname in ('__builtin__', '__builtins__'): | |
1275 | raise ValueError("Refusing to delete %s" % varname) |
|
1276 | raise ValueError("Refusing to delete %s" % varname) | |
1276 |
|
1277 | |||
1277 | ns_refs = self.all_ns_refs |
|
1278 | ns_refs = self.all_ns_refs | |
1278 |
|
1279 | |||
1279 | if by_name: # Delete by name |
|
1280 | if by_name: # Delete by name | |
1280 | for ns in ns_refs: |
|
1281 | for ns in ns_refs: | |
1281 | try: |
|
1282 | try: | |
1282 | del ns[varname] |
|
1283 | del ns[varname] | |
1283 | except KeyError: |
|
1284 | except KeyError: | |
1284 | pass |
|
1285 | pass | |
1285 | else: # Delete by object |
|
1286 | else: # Delete by object | |
1286 | try: |
|
1287 | try: | |
1287 | obj = self.user_ns[varname] |
|
1288 | obj = self.user_ns[varname] | |
1288 | except KeyError: |
|
1289 | except KeyError: | |
1289 | raise NameError("name '%s' is not defined" % varname) |
|
1290 | raise NameError("name '%s' is not defined" % varname) | |
1290 | # Also check in output history |
|
1291 | # Also check in output history | |
1291 | ns_refs.append(self.history_manager.output_hist) |
|
1292 | ns_refs.append(self.history_manager.output_hist) | |
1292 | for ns in ns_refs: |
|
1293 | for ns in ns_refs: | |
1293 | to_delete = [n for n, o in iteritems(ns) if o is obj] |
|
1294 | to_delete = [n for n, o in iteritems(ns) if o is obj] | |
1294 | for name in to_delete: |
|
1295 | for name in to_delete: | |
1295 | del ns[name] |
|
1296 | del ns[name] | |
1296 |
|
1297 | |||
1297 | # displayhook keeps extra references, but not in a dictionary |
|
1298 | # displayhook keeps extra references, but not in a dictionary | |
1298 | for name in ('_', '__', '___'): |
|
1299 | for name in ('_', '__', '___'): | |
1299 | if getattr(self.displayhook, name) is obj: |
|
1300 | if getattr(self.displayhook, name) is obj: | |
1300 | setattr(self.displayhook, name, None) |
|
1301 | setattr(self.displayhook, name, None) | |
1301 |
|
1302 | |||
1302 | def reset_selective(self, regex=None): |
|
1303 | def reset_selective(self, regex=None): | |
1303 | """Clear selective variables from internal namespaces based on a |
|
1304 | """Clear selective variables from internal namespaces based on a | |
1304 | specified regular expression. |
|
1305 | specified regular expression. | |
1305 |
|
1306 | |||
1306 | Parameters |
|
1307 | Parameters | |
1307 | ---------- |
|
1308 | ---------- | |
1308 | regex : string or compiled pattern, optional |
|
1309 | regex : string or compiled pattern, optional | |
1309 | A regular expression pattern that will be used in searching |
|
1310 | A regular expression pattern that will be used in searching | |
1310 | variable names in the users namespaces. |
|
1311 | variable names in the users namespaces. | |
1311 | """ |
|
1312 | """ | |
1312 | if regex is not None: |
|
1313 | if regex is not None: | |
1313 | try: |
|
1314 | try: | |
1314 | m = re.compile(regex) |
|
1315 | m = re.compile(regex) | |
1315 | except TypeError: |
|
1316 | except TypeError: | |
1316 | raise TypeError('regex must be a string or compiled pattern') |
|
1317 | raise TypeError('regex must be a string or compiled pattern') | |
1317 | # Search for keys in each namespace that match the given regex |
|
1318 | # Search for keys in each namespace that match the given regex | |
1318 | # If a match is found, delete the key/value pair. |
|
1319 | # If a match is found, delete the key/value pair. | |
1319 | for ns in self.all_ns_refs: |
|
1320 | for ns in self.all_ns_refs: | |
1320 | for var in ns: |
|
1321 | for var in ns: | |
1321 | if m.search(var): |
|
1322 | if m.search(var): | |
1322 | del ns[var] |
|
1323 | del ns[var] | |
1323 |
|
1324 | |||
1324 | def push(self, variables, interactive=True): |
|
1325 | def push(self, variables, interactive=True): | |
1325 | """Inject a group of variables into the IPython user namespace. |
|
1326 | """Inject a group of variables into the IPython user namespace. | |
1326 |
|
1327 | |||
1327 | Parameters |
|
1328 | Parameters | |
1328 | ---------- |
|
1329 | ---------- | |
1329 | variables : dict, str or list/tuple of str |
|
1330 | variables : dict, str or list/tuple of str | |
1330 | The variables to inject into the user's namespace. If a dict, a |
|
1331 | The variables to inject into the user's namespace. If a dict, a | |
1331 | simple update is done. If a str, the string is assumed to have |
|
1332 | simple update is done. If a str, the string is assumed to have | |
1332 | variable names separated by spaces. A list/tuple of str can also |
|
1333 | variable names separated by spaces. A list/tuple of str can also | |
1333 | be used to give the variable names. If just the variable names are |
|
1334 | be used to give the variable names. If just the variable names are | |
1334 | give (list/tuple/str) then the variable values looked up in the |
|
1335 | give (list/tuple/str) then the variable values looked up in the | |
1335 | callers frame. |
|
1336 | callers frame. | |
1336 | interactive : bool |
|
1337 | interactive : bool | |
1337 | If True (default), the variables will be listed with the ``who`` |
|
1338 | If True (default), the variables will be listed with the ``who`` | |
1338 | magic. |
|
1339 | magic. | |
1339 | """ |
|
1340 | """ | |
1340 | vdict = None |
|
1341 | vdict = None | |
1341 |
|
1342 | |||
1342 | # We need a dict of name/value pairs to do namespace updates. |
|
1343 | # We need a dict of name/value pairs to do namespace updates. | |
1343 | if isinstance(variables, dict): |
|
1344 | if isinstance(variables, dict): | |
1344 | vdict = variables |
|
1345 | vdict = variables | |
1345 | elif isinstance(variables, string_types+(list, tuple)): |
|
1346 | elif isinstance(variables, string_types+(list, tuple)): | |
1346 | if isinstance(variables, string_types): |
|
1347 | if isinstance(variables, string_types): | |
1347 | vlist = variables.split() |
|
1348 | vlist = variables.split() | |
1348 | else: |
|
1349 | else: | |
1349 | vlist = variables |
|
1350 | vlist = variables | |
1350 | vdict = {} |
|
1351 | vdict = {} | |
1351 | cf = sys._getframe(1) |
|
1352 | cf = sys._getframe(1) | |
1352 | for name in vlist: |
|
1353 | for name in vlist: | |
1353 | try: |
|
1354 | try: | |
1354 | vdict[name] = eval(name, cf.f_globals, cf.f_locals) |
|
1355 | vdict[name] = eval(name, cf.f_globals, cf.f_locals) | |
1355 | except: |
|
1356 | except: | |
1356 | print('Could not get variable %s from %s' % |
|
1357 | print('Could not get variable %s from %s' % | |
1357 | (name,cf.f_code.co_name)) |
|
1358 | (name,cf.f_code.co_name)) | |
1358 | else: |
|
1359 | else: | |
1359 | raise ValueError('variables must be a dict/str/list/tuple') |
|
1360 | raise ValueError('variables must be a dict/str/list/tuple') | |
1360 |
|
1361 | |||
1361 | # Propagate variables to user namespace |
|
1362 | # Propagate variables to user namespace | |
1362 | self.user_ns.update(vdict) |
|
1363 | self.user_ns.update(vdict) | |
1363 |
|
1364 | |||
1364 | # And configure interactive visibility |
|
1365 | # And configure interactive visibility | |
1365 | user_ns_hidden = self.user_ns_hidden |
|
1366 | user_ns_hidden = self.user_ns_hidden | |
1366 | if interactive: |
|
1367 | if interactive: | |
1367 | for name in vdict: |
|
1368 | for name in vdict: | |
1368 | user_ns_hidden.pop(name, None) |
|
1369 | user_ns_hidden.pop(name, None) | |
1369 | else: |
|
1370 | else: | |
1370 | user_ns_hidden.update(vdict) |
|
1371 | user_ns_hidden.update(vdict) | |
1371 |
|
1372 | |||
1372 | def drop_by_id(self, variables): |
|
1373 | def drop_by_id(self, variables): | |
1373 | """Remove a dict of variables from the user namespace, if they are the |
|
1374 | """Remove a dict of variables from the user namespace, if they are the | |
1374 | same as the values in the dictionary. |
|
1375 | same as the values in the dictionary. | |
1375 |
|
1376 | |||
1376 | This is intended for use by extensions: variables that they've added can |
|
1377 | This is intended for use by extensions: variables that they've added can | |
1377 | be taken back out if they are unloaded, without removing any that the |
|
1378 | be taken back out if they are unloaded, without removing any that the | |
1378 | user has overwritten. |
|
1379 | user has overwritten. | |
1379 |
|
1380 | |||
1380 | Parameters |
|
1381 | Parameters | |
1381 | ---------- |
|
1382 | ---------- | |
1382 | variables : dict |
|
1383 | variables : dict | |
1383 | A dictionary mapping object names (as strings) to the objects. |
|
1384 | A dictionary mapping object names (as strings) to the objects. | |
1384 | """ |
|
1385 | """ | |
1385 | for name, obj in iteritems(variables): |
|
1386 | for name, obj in iteritems(variables): | |
1386 | if name in self.user_ns and self.user_ns[name] is obj: |
|
1387 | if name in self.user_ns and self.user_ns[name] is obj: | |
1387 | del self.user_ns[name] |
|
1388 | del self.user_ns[name] | |
1388 | self.user_ns_hidden.pop(name, None) |
|
1389 | self.user_ns_hidden.pop(name, None) | |
1389 |
|
1390 | |||
1390 | #------------------------------------------------------------------------- |
|
1391 | #------------------------------------------------------------------------- | |
1391 | # Things related to object introspection |
|
1392 | # Things related to object introspection | |
1392 | #------------------------------------------------------------------------- |
|
1393 | #------------------------------------------------------------------------- | |
1393 |
|
1394 | |||
1394 | def _ofind(self, oname, namespaces=None): |
|
1395 | def _ofind(self, oname, namespaces=None): | |
1395 | """Find an object in the available namespaces. |
|
1396 | """Find an object in the available namespaces. | |
1396 |
|
1397 | |||
1397 | self._ofind(oname) -> dict with keys: found,obj,ospace,ismagic |
|
1398 | self._ofind(oname) -> dict with keys: found,obj,ospace,ismagic | |
1398 |
|
1399 | |||
1399 | Has special code to detect magic functions. |
|
1400 | Has special code to detect magic functions. | |
1400 | """ |
|
1401 | """ | |
1401 | oname = oname.strip() |
|
1402 | oname = oname.strip() | |
1402 | #print '1- oname: <%r>' % oname # dbg |
|
1403 | #print '1- oname: <%r>' % oname # dbg | |
1403 | if not oname.startswith(ESC_MAGIC) and \ |
|
1404 | if not oname.startswith(ESC_MAGIC) and \ | |
1404 | not oname.startswith(ESC_MAGIC2) and \ |
|
1405 | not oname.startswith(ESC_MAGIC2) and \ | |
1405 | not py3compat.isidentifier(oname, dotted=True): |
|
1406 | not py3compat.isidentifier(oname, dotted=True): | |
1406 | return dict(found=False) |
|
1407 | return dict(found=False) | |
1407 |
|
1408 | |||
1408 | if namespaces is None: |
|
1409 | if namespaces is None: | |
1409 | # Namespaces to search in: |
|
1410 | # Namespaces to search in: | |
1410 | # Put them in a list. The order is important so that we |
|
1411 | # Put them in a list. The order is important so that we | |
1411 | # find things in the same order that Python finds them. |
|
1412 | # find things in the same order that Python finds them. | |
1412 | namespaces = [ ('Interactive', self.user_ns), |
|
1413 | namespaces = [ ('Interactive', self.user_ns), | |
1413 | ('Interactive (global)', self.user_global_ns), |
|
1414 | ('Interactive (global)', self.user_global_ns), | |
1414 | ('Python builtin', builtin_mod.__dict__), |
|
1415 | ('Python builtin', builtin_mod.__dict__), | |
1415 | ] |
|
1416 | ] | |
1416 |
|
1417 | |||
1417 | # initialize results to 'null' |
|
1418 | # initialize results to 'null' | |
1418 | found = False; obj = None; ospace = None; |
|
1419 | found = False; obj = None; ospace = None; | |
1419 | ismagic = False; isalias = False; parent = None |
|
1420 | ismagic = False; isalias = False; parent = None | |
1420 |
|
1421 | |||
1421 | # We need to special-case 'print', which as of python2.6 registers as a |
|
1422 | # We need to special-case 'print', which as of python2.6 registers as a | |
1422 | # function but should only be treated as one if print_function was |
|
1423 | # function but should only be treated as one if print_function was | |
1423 | # loaded with a future import. In this case, just bail. |
|
1424 | # loaded with a future import. In this case, just bail. | |
1424 | if (oname == 'print' and not py3compat.PY3 and not \ |
|
1425 | if (oname == 'print' and not py3compat.PY3 and not \ | |
1425 | (self.compile.compiler_flags & __future__.CO_FUTURE_PRINT_FUNCTION)): |
|
1426 | (self.compile.compiler_flags & __future__.CO_FUTURE_PRINT_FUNCTION)): | |
1426 | return {'found':found, 'obj':obj, 'namespace':ospace, |
|
1427 | return {'found':found, 'obj':obj, 'namespace':ospace, | |
1427 | 'ismagic':ismagic, 'isalias':isalias, 'parent':parent} |
|
1428 | 'ismagic':ismagic, 'isalias':isalias, 'parent':parent} | |
1428 |
|
1429 | |||
1429 | # Look for the given name by splitting it in parts. If the head is |
|
1430 | # Look for the given name by splitting it in parts. If the head is | |
1430 | # found, then we look for all the remaining parts as members, and only |
|
1431 | # found, then we look for all the remaining parts as members, and only | |
1431 | # declare success if we can find them all. |
|
1432 | # declare success if we can find them all. | |
1432 | oname_parts = oname.split('.') |
|
1433 | oname_parts = oname.split('.') | |
1433 | oname_head, oname_rest = oname_parts[0],oname_parts[1:] |
|
1434 | oname_head, oname_rest = oname_parts[0],oname_parts[1:] | |
1434 | for nsname,ns in namespaces: |
|
1435 | for nsname,ns in namespaces: | |
1435 | try: |
|
1436 | try: | |
1436 | obj = ns[oname_head] |
|
1437 | obj = ns[oname_head] | |
1437 | except KeyError: |
|
1438 | except KeyError: | |
1438 | continue |
|
1439 | continue | |
1439 | else: |
|
1440 | else: | |
1440 | #print 'oname_rest:', oname_rest # dbg |
|
1441 | #print 'oname_rest:', oname_rest # dbg | |
1441 | for idx, part in enumerate(oname_rest): |
|
1442 | for idx, part in enumerate(oname_rest): | |
1442 | try: |
|
1443 | try: | |
1443 | parent = obj |
|
1444 | parent = obj | |
1444 | # The last part is looked up in a special way to avoid |
|
1445 | # The last part is looked up in a special way to avoid | |
1445 | # descriptor invocation as it may raise or have side |
|
1446 | # descriptor invocation as it may raise or have side | |
1446 | # effects. |
|
1447 | # effects. | |
1447 | if idx == len(oname_rest) - 1: |
|
1448 | if idx == len(oname_rest) - 1: | |
1448 | obj = self._getattr_property(obj, part) |
|
1449 | obj = self._getattr_property(obj, part) | |
1449 | else: |
|
1450 | else: | |
1450 | obj = getattr(obj, part) |
|
1451 | obj = getattr(obj, part) | |
1451 | except: |
|
1452 | except: | |
1452 | # Blanket except b/c some badly implemented objects |
|
1453 | # Blanket except b/c some badly implemented objects | |
1453 | # allow __getattr__ to raise exceptions other than |
|
1454 | # allow __getattr__ to raise exceptions other than | |
1454 | # AttributeError, which then crashes IPython. |
|
1455 | # AttributeError, which then crashes IPython. | |
1455 | break |
|
1456 | break | |
1456 | else: |
|
1457 | else: | |
1457 | # If we finish the for loop (no break), we got all members |
|
1458 | # If we finish the for loop (no break), we got all members | |
1458 | found = True |
|
1459 | found = True | |
1459 | ospace = nsname |
|
1460 | ospace = nsname | |
1460 | break # namespace loop |
|
1461 | break # namespace loop | |
1461 |
|
1462 | |||
1462 | # Try to see if it's magic |
|
1463 | # Try to see if it's magic | |
1463 | if not found: |
|
1464 | if not found: | |
1464 | obj = None |
|
1465 | obj = None | |
1465 | if oname.startswith(ESC_MAGIC2): |
|
1466 | if oname.startswith(ESC_MAGIC2): | |
1466 | oname = oname.lstrip(ESC_MAGIC2) |
|
1467 | oname = oname.lstrip(ESC_MAGIC2) | |
1467 | obj = self.find_cell_magic(oname) |
|
1468 | obj = self.find_cell_magic(oname) | |
1468 | elif oname.startswith(ESC_MAGIC): |
|
1469 | elif oname.startswith(ESC_MAGIC): | |
1469 | oname = oname.lstrip(ESC_MAGIC) |
|
1470 | oname = oname.lstrip(ESC_MAGIC) | |
1470 | obj = self.find_line_magic(oname) |
|
1471 | obj = self.find_line_magic(oname) | |
1471 | else: |
|
1472 | else: | |
1472 | # search without prefix, so run? will find %run? |
|
1473 | # search without prefix, so run? will find %run? | |
1473 | obj = self.find_line_magic(oname) |
|
1474 | obj = self.find_line_magic(oname) | |
1474 | if obj is None: |
|
1475 | if obj is None: | |
1475 | obj = self.find_cell_magic(oname) |
|
1476 | obj = self.find_cell_magic(oname) | |
1476 | if obj is not None: |
|
1477 | if obj is not None: | |
1477 | found = True |
|
1478 | found = True | |
1478 | ospace = 'IPython internal' |
|
1479 | ospace = 'IPython internal' | |
1479 | ismagic = True |
|
1480 | ismagic = True | |
1480 | isalias = isinstance(obj, Alias) |
|
1481 | isalias = isinstance(obj, Alias) | |
1481 |
|
1482 | |||
1482 | # Last try: special-case some literals like '', [], {}, etc: |
|
1483 | # Last try: special-case some literals like '', [], {}, etc: | |
1483 | if not found and oname_head in ["''",'""','[]','{}','()']: |
|
1484 | if not found and oname_head in ["''",'""','[]','{}','()']: | |
1484 | obj = eval(oname_head) |
|
1485 | obj = eval(oname_head) | |
1485 | found = True |
|
1486 | found = True | |
1486 | ospace = 'Interactive' |
|
1487 | ospace = 'Interactive' | |
1487 |
|
1488 | |||
1488 | return {'found':found, 'obj':obj, 'namespace':ospace, |
|
1489 | return {'found':found, 'obj':obj, 'namespace':ospace, | |
1489 | 'ismagic':ismagic, 'isalias':isalias, 'parent':parent} |
|
1490 | 'ismagic':ismagic, 'isalias':isalias, 'parent':parent} | |
1490 |
|
1491 | |||
1491 | @staticmethod |
|
1492 | @staticmethod | |
1492 | def _getattr_property(obj, attrname): |
|
1493 | def _getattr_property(obj, attrname): | |
1493 | """Property-aware getattr to use in object finding. |
|
1494 | """Property-aware getattr to use in object finding. | |
1494 |
|
1495 | |||
1495 | If attrname represents a property, return it unevaluated (in case it has |
|
1496 | If attrname represents a property, return it unevaluated (in case it has | |
1496 | side effects or raises an error. |
|
1497 | side effects or raises an error. | |
1497 |
|
1498 | |||
1498 | """ |
|
1499 | """ | |
1499 | if not isinstance(obj, type): |
|
1500 | if not isinstance(obj, type): | |
1500 | try: |
|
1501 | try: | |
1501 | # `getattr(type(obj), attrname)` is not guaranteed to return |
|
1502 | # `getattr(type(obj), attrname)` is not guaranteed to return | |
1502 | # `obj`, but does so for property: |
|
1503 | # `obj`, but does so for property: | |
1503 | # |
|
1504 | # | |
1504 | # property.__get__(self, None, cls) -> self |
|
1505 | # property.__get__(self, None, cls) -> self | |
1505 | # |
|
1506 | # | |
1506 | # The universal alternative is to traverse the mro manually |
|
1507 | # The universal alternative is to traverse the mro manually | |
1507 | # searching for attrname in class dicts. |
|
1508 | # searching for attrname in class dicts. | |
1508 | attr = getattr(type(obj), attrname) |
|
1509 | attr = getattr(type(obj), attrname) | |
1509 | except AttributeError: |
|
1510 | except AttributeError: | |
1510 | pass |
|
1511 | pass | |
1511 | else: |
|
1512 | else: | |
1512 | # This relies on the fact that data descriptors (with both |
|
1513 | # This relies on the fact that data descriptors (with both | |
1513 | # __get__ & __set__ magic methods) take precedence over |
|
1514 | # __get__ & __set__ magic methods) take precedence over | |
1514 | # instance-level attributes: |
|
1515 | # instance-level attributes: | |
1515 | # |
|
1516 | # | |
1516 | # class A(object): |
|
1517 | # class A(object): | |
1517 | # @property |
|
1518 | # @property | |
1518 | # def foobar(self): return 123 |
|
1519 | # def foobar(self): return 123 | |
1519 | # a = A() |
|
1520 | # a = A() | |
1520 | # a.__dict__['foobar'] = 345 |
|
1521 | # a.__dict__['foobar'] = 345 | |
1521 | # a.foobar # == 123 |
|
1522 | # a.foobar # == 123 | |
1522 | # |
|
1523 | # | |
1523 | # So, a property may be returned right away. |
|
1524 | # So, a property may be returned right away. | |
1524 | if isinstance(attr, property): |
|
1525 | if isinstance(attr, property): | |
1525 | return attr |
|
1526 | return attr | |
1526 |
|
1527 | |||
1527 | # Nothing helped, fall back. |
|
1528 | # Nothing helped, fall back. | |
1528 | return getattr(obj, attrname) |
|
1529 | return getattr(obj, attrname) | |
1529 |
|
1530 | |||
1530 | def _object_find(self, oname, namespaces=None): |
|
1531 | def _object_find(self, oname, namespaces=None): | |
1531 | """Find an object and return a struct with info about it.""" |
|
1532 | """Find an object and return a struct with info about it.""" | |
1532 | return Struct(self._ofind(oname, namespaces)) |
|
1533 | return Struct(self._ofind(oname, namespaces)) | |
1533 |
|
1534 | |||
1534 | def _inspect(self, meth, oname, namespaces=None, **kw): |
|
1535 | def _inspect(self, meth, oname, namespaces=None, **kw): | |
1535 | """Generic interface to the inspector system. |
|
1536 | """Generic interface to the inspector system. | |
1536 |
|
1537 | |||
1537 | This function is meant to be called by pdef, pdoc & friends.""" |
|
1538 | This function is meant to be called by pdef, pdoc & friends.""" | |
1538 | info = self._object_find(oname, namespaces) |
|
1539 | info = self._object_find(oname, namespaces) | |
1539 | if info.found: |
|
1540 | if info.found: | |
1540 | pmethod = getattr(self.inspector, meth) |
|
1541 | pmethod = getattr(self.inspector, meth) | |
1541 | formatter = format_screen if info.ismagic else None |
|
1542 | formatter = format_screen if info.ismagic else None | |
1542 | if meth == 'pdoc': |
|
1543 | if meth == 'pdoc': | |
1543 | pmethod(info.obj, oname, formatter) |
|
1544 | pmethod(info.obj, oname, formatter) | |
1544 | elif meth == 'pinfo': |
|
1545 | elif meth == 'pinfo': | |
1545 | pmethod(info.obj, oname, formatter, info, **kw) |
|
1546 | pmethod(info.obj, oname, formatter, info, **kw) | |
1546 | else: |
|
1547 | else: | |
1547 | pmethod(info.obj, oname) |
|
1548 | pmethod(info.obj, oname) | |
1548 | else: |
|
1549 | else: | |
1549 | print('Object `%s` not found.' % oname) |
|
1550 | print('Object `%s` not found.' % oname) | |
1550 | return 'not found' # so callers can take other action |
|
1551 | return 'not found' # so callers can take other action | |
1551 |
|
1552 | |||
1552 | def object_inspect(self, oname, detail_level=0): |
|
1553 | def object_inspect(self, oname, detail_level=0): | |
1553 | """Get object info about oname""" |
|
1554 | """Get object info about oname""" | |
1554 | with self.builtin_trap: |
|
1555 | with self.builtin_trap: | |
1555 | info = self._object_find(oname) |
|
1556 | info = self._object_find(oname) | |
1556 | if info.found: |
|
1557 | if info.found: | |
1557 | return self.inspector.info(info.obj, oname, info=info, |
|
1558 | return self.inspector.info(info.obj, oname, info=info, | |
1558 | detail_level=detail_level |
|
1559 | detail_level=detail_level | |
1559 | ) |
|
1560 | ) | |
1560 | else: |
|
1561 | else: | |
1561 | return oinspect.object_info(name=oname, found=False) |
|
1562 | return oinspect.object_info(name=oname, found=False) | |
1562 |
|
1563 | |||
1563 | def object_inspect_text(self, oname, detail_level=0): |
|
1564 | def object_inspect_text(self, oname, detail_level=0): | |
1564 | """Get object info as formatted text""" |
|
1565 | """Get object info as formatted text""" | |
1565 | with self.builtin_trap: |
|
1566 | with self.builtin_trap: | |
1566 | info = self._object_find(oname) |
|
1567 | info = self._object_find(oname) | |
1567 | if info.found: |
|
1568 | if info.found: | |
1568 | return self.inspector._format_info(info.obj, oname, info=info, |
|
1569 | return self.inspector._format_info(info.obj, oname, info=info, | |
1569 | detail_level=detail_level |
|
1570 | detail_level=detail_level | |
1570 | ) |
|
1571 | ) | |
1571 | else: |
|
1572 | else: | |
1572 | raise KeyError(oname) |
|
1573 | raise KeyError(oname) | |
1573 |
|
1574 | |||
1574 | #------------------------------------------------------------------------- |
|
1575 | #------------------------------------------------------------------------- | |
1575 | # Things related to history management |
|
1576 | # Things related to history management | |
1576 | #------------------------------------------------------------------------- |
|
1577 | #------------------------------------------------------------------------- | |
1577 |
|
1578 | |||
1578 | def init_history(self): |
|
1579 | def init_history(self): | |
1579 | """Sets up the command history, and starts regular autosaves.""" |
|
1580 | """Sets up the command history, and starts regular autosaves.""" | |
1580 | self.history_manager = HistoryManager(shell=self, parent=self) |
|
1581 | self.history_manager = HistoryManager(shell=self, parent=self) | |
1581 | self.configurables.append(self.history_manager) |
|
1582 | self.configurables.append(self.history_manager) | |
1582 |
|
1583 | |||
1583 | #------------------------------------------------------------------------- |
|
1584 | #------------------------------------------------------------------------- | |
1584 | # Things related to exception handling and tracebacks (not debugging) |
|
1585 | # Things related to exception handling and tracebacks (not debugging) | |
1585 | #------------------------------------------------------------------------- |
|
1586 | #------------------------------------------------------------------------- | |
1586 |
|
1587 | |||
|
1588 | debugger_cls = Pdb | |||
|
1589 | ||||
1587 | def init_traceback_handlers(self, custom_exceptions): |
|
1590 | def init_traceback_handlers(self, custom_exceptions): | |
1588 | # Syntax error handler. |
|
1591 | # Syntax error handler. | |
1589 | self.SyntaxTB = ultratb.SyntaxTB(color_scheme='NoColor') |
|
1592 | self.SyntaxTB = ultratb.SyntaxTB(color_scheme='NoColor') | |
1590 |
|
1593 | |||
1591 | # The interactive one is initialized with an offset, meaning we always |
|
1594 | # The interactive one is initialized with an offset, meaning we always | |
1592 | # want to remove the topmost item in the traceback, which is our own |
|
1595 | # want to remove the topmost item in the traceback, which is our own | |
1593 | # internal code. Valid modes: ['Plain','Context','Verbose'] |
|
1596 | # internal code. Valid modes: ['Plain','Context','Verbose'] | |
1594 | self.InteractiveTB = ultratb.AutoFormattedTB(mode = 'Plain', |
|
1597 | self.InteractiveTB = ultratb.AutoFormattedTB(mode = 'Plain', | |
1595 | color_scheme='NoColor', |
|
1598 | color_scheme='NoColor', | |
1596 | tb_offset = 1, |
|
1599 | tb_offset = 1, | |
1597 |
check_cache=check_linecache_ipython |
|
1600 | check_cache=check_linecache_ipython, | |
|
1601 | debugger_cls=self.debugger_cls) | |||
1598 |
|
1602 | |||
1599 | # The instance will store a pointer to the system-wide exception hook, |
|
1603 | # The instance will store a pointer to the system-wide exception hook, | |
1600 | # so that runtime code (such as magics) can access it. This is because |
|
1604 | # so that runtime code (such as magics) can access it. This is because | |
1601 | # during the read-eval loop, it may get temporarily overwritten. |
|
1605 | # during the read-eval loop, it may get temporarily overwritten. | |
1602 | self.sys_excepthook = sys.excepthook |
|
1606 | self.sys_excepthook = sys.excepthook | |
1603 |
|
1607 | |||
1604 | # and add any custom exception handlers the user may have specified |
|
1608 | # and add any custom exception handlers the user may have specified | |
1605 | self.set_custom_exc(*custom_exceptions) |
|
1609 | self.set_custom_exc(*custom_exceptions) | |
1606 |
|
1610 | |||
1607 | # Set the exception mode |
|
1611 | # Set the exception mode | |
1608 | self.InteractiveTB.set_mode(mode=self.xmode) |
|
1612 | self.InteractiveTB.set_mode(mode=self.xmode) | |
1609 |
|
1613 | |||
1610 | def set_custom_exc(self, exc_tuple, handler): |
|
1614 | def set_custom_exc(self, exc_tuple, handler): | |
1611 | """set_custom_exc(exc_tuple,handler) |
|
1615 | """set_custom_exc(exc_tuple,handler) | |
1612 |
|
1616 | |||
1613 | Set a custom exception handler, which will be called if any of the |
|
1617 | Set a custom exception handler, which will be called if any of the | |
1614 | exceptions in exc_tuple occur in the mainloop (specifically, in the |
|
1618 | exceptions in exc_tuple occur in the mainloop (specifically, in the | |
1615 | run_code() method). |
|
1619 | run_code() method). | |
1616 |
|
1620 | |||
1617 | Parameters |
|
1621 | Parameters | |
1618 | ---------- |
|
1622 | ---------- | |
1619 |
|
1623 | |||
1620 | exc_tuple : tuple of exception classes |
|
1624 | exc_tuple : tuple of exception classes | |
1621 | A *tuple* of exception classes, for which to call the defined |
|
1625 | A *tuple* of exception classes, for which to call the defined | |
1622 | handler. It is very important that you use a tuple, and NOT A |
|
1626 | handler. It is very important that you use a tuple, and NOT A | |
1623 | LIST here, because of the way Python's except statement works. If |
|
1627 | LIST here, because of the way Python's except statement works. If | |
1624 | you only want to trap a single exception, use a singleton tuple:: |
|
1628 | you only want to trap a single exception, use a singleton tuple:: | |
1625 |
|
1629 | |||
1626 | exc_tuple == (MyCustomException,) |
|
1630 | exc_tuple == (MyCustomException,) | |
1627 |
|
1631 | |||
1628 | handler : callable |
|
1632 | handler : callable | |
1629 | handler must have the following signature:: |
|
1633 | handler must have the following signature:: | |
1630 |
|
1634 | |||
1631 | def my_handler(self, etype, value, tb, tb_offset=None): |
|
1635 | def my_handler(self, etype, value, tb, tb_offset=None): | |
1632 | ... |
|
1636 | ... | |
1633 | return structured_traceback |
|
1637 | return structured_traceback | |
1634 |
|
1638 | |||
1635 | Your handler must return a structured traceback (a list of strings), |
|
1639 | Your handler must return a structured traceback (a list of strings), | |
1636 | or None. |
|
1640 | or None. | |
1637 |
|
1641 | |||
1638 | This will be made into an instance method (via types.MethodType) |
|
1642 | This will be made into an instance method (via types.MethodType) | |
1639 | of IPython itself, and it will be called if any of the exceptions |
|
1643 | of IPython itself, and it will be called if any of the exceptions | |
1640 | listed in the exc_tuple are caught. If the handler is None, an |
|
1644 | listed in the exc_tuple are caught. If the handler is None, an | |
1641 | internal basic one is used, which just prints basic info. |
|
1645 | internal basic one is used, which just prints basic info. | |
1642 |
|
1646 | |||
1643 | To protect IPython from crashes, if your handler ever raises an |
|
1647 | To protect IPython from crashes, if your handler ever raises an | |
1644 | exception or returns an invalid result, it will be immediately |
|
1648 | exception or returns an invalid result, it will be immediately | |
1645 | disabled. |
|
1649 | disabled. | |
1646 |
|
1650 | |||
1647 | WARNING: by putting in your own exception handler into IPython's main |
|
1651 | WARNING: by putting in your own exception handler into IPython's main | |
1648 | execution loop, you run a very good chance of nasty crashes. This |
|
1652 | execution loop, you run a very good chance of nasty crashes. This | |
1649 | facility should only be used if you really know what you are doing.""" |
|
1653 | facility should only be used if you really know what you are doing.""" | |
1650 |
|
1654 | |||
1651 | assert type(exc_tuple)==type(()) , \ |
|
1655 | assert type(exc_tuple)==type(()) , \ | |
1652 | "The custom exceptions must be given AS A TUPLE." |
|
1656 | "The custom exceptions must be given AS A TUPLE." | |
1653 |
|
1657 | |||
1654 | def dummy_handler(self,etype,value,tb,tb_offset=None): |
|
1658 | def dummy_handler(self,etype,value,tb,tb_offset=None): | |
1655 | print('*** Simple custom exception handler ***') |
|
1659 | print('*** Simple custom exception handler ***') | |
1656 | print('Exception type :',etype) |
|
1660 | print('Exception type :',etype) | |
1657 | print('Exception value:',value) |
|
1661 | print('Exception value:',value) | |
1658 | print('Traceback :',tb) |
|
1662 | print('Traceback :',tb) | |
1659 | #print 'Source code :','\n'.join(self.buffer) |
|
1663 | #print 'Source code :','\n'.join(self.buffer) | |
1660 |
|
1664 | |||
1661 | def validate_stb(stb): |
|
1665 | def validate_stb(stb): | |
1662 | """validate structured traceback return type |
|
1666 | """validate structured traceback return type | |
1663 |
|
1667 | |||
1664 | return type of CustomTB *should* be a list of strings, but allow |
|
1668 | return type of CustomTB *should* be a list of strings, but allow | |
1665 | single strings or None, which are harmless. |
|
1669 | single strings or None, which are harmless. | |
1666 |
|
1670 | |||
1667 | This function will *always* return a list of strings, |
|
1671 | This function will *always* return a list of strings, | |
1668 | and will raise a TypeError if stb is inappropriate. |
|
1672 | and will raise a TypeError if stb is inappropriate. | |
1669 | """ |
|
1673 | """ | |
1670 | msg = "CustomTB must return list of strings, not %r" % stb |
|
1674 | msg = "CustomTB must return list of strings, not %r" % stb | |
1671 | if stb is None: |
|
1675 | if stb is None: | |
1672 | return [] |
|
1676 | return [] | |
1673 | elif isinstance(stb, string_types): |
|
1677 | elif isinstance(stb, string_types): | |
1674 | return [stb] |
|
1678 | return [stb] | |
1675 | elif not isinstance(stb, list): |
|
1679 | elif not isinstance(stb, list): | |
1676 | raise TypeError(msg) |
|
1680 | raise TypeError(msg) | |
1677 | # it's a list |
|
1681 | # it's a list | |
1678 | for line in stb: |
|
1682 | for line in stb: | |
1679 | # check every element |
|
1683 | # check every element | |
1680 | if not isinstance(line, string_types): |
|
1684 | if not isinstance(line, string_types): | |
1681 | raise TypeError(msg) |
|
1685 | raise TypeError(msg) | |
1682 | return stb |
|
1686 | return stb | |
1683 |
|
1687 | |||
1684 | if handler is None: |
|
1688 | if handler is None: | |
1685 | wrapped = dummy_handler |
|
1689 | wrapped = dummy_handler | |
1686 | else: |
|
1690 | else: | |
1687 | def wrapped(self,etype,value,tb,tb_offset=None): |
|
1691 | def wrapped(self,etype,value,tb,tb_offset=None): | |
1688 | """wrap CustomTB handler, to protect IPython from user code |
|
1692 | """wrap CustomTB handler, to protect IPython from user code | |
1689 |
|
1693 | |||
1690 | This makes it harder (but not impossible) for custom exception |
|
1694 | This makes it harder (but not impossible) for custom exception | |
1691 | handlers to crash IPython. |
|
1695 | handlers to crash IPython. | |
1692 | """ |
|
1696 | """ | |
1693 | try: |
|
1697 | try: | |
1694 | stb = handler(self,etype,value,tb,tb_offset=tb_offset) |
|
1698 | stb = handler(self,etype,value,tb,tb_offset=tb_offset) | |
1695 | return validate_stb(stb) |
|
1699 | return validate_stb(stb) | |
1696 | except: |
|
1700 | except: | |
1697 | # clear custom handler immediately |
|
1701 | # clear custom handler immediately | |
1698 | self.set_custom_exc((), None) |
|
1702 | self.set_custom_exc((), None) | |
1699 | print("Custom TB Handler failed, unregistering", file=sys.stderr) |
|
1703 | print("Custom TB Handler failed, unregistering", file=sys.stderr) | |
1700 | # show the exception in handler first |
|
1704 | # show the exception in handler first | |
1701 | stb = self.InteractiveTB.structured_traceback(*sys.exc_info()) |
|
1705 | stb = self.InteractiveTB.structured_traceback(*sys.exc_info()) | |
1702 | print(self.InteractiveTB.stb2text(stb)) |
|
1706 | print(self.InteractiveTB.stb2text(stb)) | |
1703 | print("The original exception:") |
|
1707 | print("The original exception:") | |
1704 | stb = self.InteractiveTB.structured_traceback( |
|
1708 | stb = self.InteractiveTB.structured_traceback( | |
1705 | (etype,value,tb), tb_offset=tb_offset |
|
1709 | (etype,value,tb), tb_offset=tb_offset | |
1706 | ) |
|
1710 | ) | |
1707 | return stb |
|
1711 | return stb | |
1708 |
|
1712 | |||
1709 | self.CustomTB = types.MethodType(wrapped,self) |
|
1713 | self.CustomTB = types.MethodType(wrapped,self) | |
1710 | self.custom_exceptions = exc_tuple |
|
1714 | self.custom_exceptions = exc_tuple | |
1711 |
|
1715 | |||
1712 | def excepthook(self, etype, value, tb): |
|
1716 | def excepthook(self, etype, value, tb): | |
1713 | """One more defense for GUI apps that call sys.excepthook. |
|
1717 | """One more defense for GUI apps that call sys.excepthook. | |
1714 |
|
1718 | |||
1715 | GUI frameworks like wxPython trap exceptions and call |
|
1719 | GUI frameworks like wxPython trap exceptions and call | |
1716 | sys.excepthook themselves. I guess this is a feature that |
|
1720 | sys.excepthook themselves. I guess this is a feature that | |
1717 | enables them to keep running after exceptions that would |
|
1721 | enables them to keep running after exceptions that would | |
1718 | otherwise kill their mainloop. This is a bother for IPython |
|
1722 | otherwise kill their mainloop. This is a bother for IPython | |
1719 | which excepts to catch all of the program exceptions with a try: |
|
1723 | which excepts to catch all of the program exceptions with a try: | |
1720 | except: statement. |
|
1724 | except: statement. | |
1721 |
|
1725 | |||
1722 | Normally, IPython sets sys.excepthook to a CrashHandler instance, so if |
|
1726 | Normally, IPython sets sys.excepthook to a CrashHandler instance, so if | |
1723 | any app directly invokes sys.excepthook, it will look to the user like |
|
1727 | any app directly invokes sys.excepthook, it will look to the user like | |
1724 | IPython crashed. In order to work around this, we can disable the |
|
1728 | IPython crashed. In order to work around this, we can disable the | |
1725 | CrashHandler and replace it with this excepthook instead, which prints a |
|
1729 | CrashHandler and replace it with this excepthook instead, which prints a | |
1726 | regular traceback using our InteractiveTB. In this fashion, apps which |
|
1730 | regular traceback using our InteractiveTB. In this fashion, apps which | |
1727 | call sys.excepthook will generate a regular-looking exception from |
|
1731 | call sys.excepthook will generate a regular-looking exception from | |
1728 | IPython, and the CrashHandler will only be triggered by real IPython |
|
1732 | IPython, and the CrashHandler will only be triggered by real IPython | |
1729 | crashes. |
|
1733 | crashes. | |
1730 |
|
1734 | |||
1731 | This hook should be used sparingly, only in places which are not likely |
|
1735 | This hook should be used sparingly, only in places which are not likely | |
1732 | to be true IPython errors. |
|
1736 | to be true IPython errors. | |
1733 | """ |
|
1737 | """ | |
1734 | self.showtraceback((etype, value, tb), tb_offset=0) |
|
1738 | self.showtraceback((etype, value, tb), tb_offset=0) | |
1735 |
|
1739 | |||
1736 | def _get_exc_info(self, exc_tuple=None): |
|
1740 | def _get_exc_info(self, exc_tuple=None): | |
1737 | """get exc_info from a given tuple, sys.exc_info() or sys.last_type etc. |
|
1741 | """get exc_info from a given tuple, sys.exc_info() or sys.last_type etc. | |
1738 |
|
1742 | |||
1739 | Ensures sys.last_type,value,traceback hold the exc_info we found, |
|
1743 | Ensures sys.last_type,value,traceback hold the exc_info we found, | |
1740 | from whichever source. |
|
1744 | from whichever source. | |
1741 |
|
1745 | |||
1742 | raises ValueError if none of these contain any information |
|
1746 | raises ValueError if none of these contain any information | |
1743 | """ |
|
1747 | """ | |
1744 | if exc_tuple is None: |
|
1748 | if exc_tuple is None: | |
1745 | etype, value, tb = sys.exc_info() |
|
1749 | etype, value, tb = sys.exc_info() | |
1746 | else: |
|
1750 | else: | |
1747 | etype, value, tb = exc_tuple |
|
1751 | etype, value, tb = exc_tuple | |
1748 |
|
1752 | |||
1749 | if etype is None: |
|
1753 | if etype is None: | |
1750 | if hasattr(sys, 'last_type'): |
|
1754 | if hasattr(sys, 'last_type'): | |
1751 | etype, value, tb = sys.last_type, sys.last_value, \ |
|
1755 | etype, value, tb = sys.last_type, sys.last_value, \ | |
1752 | sys.last_traceback |
|
1756 | sys.last_traceback | |
1753 |
|
1757 | |||
1754 | if etype is None: |
|
1758 | if etype is None: | |
1755 | raise ValueError("No exception to find") |
|
1759 | raise ValueError("No exception to find") | |
1756 |
|
1760 | |||
1757 | # Now store the exception info in sys.last_type etc. |
|
1761 | # Now store the exception info in sys.last_type etc. | |
1758 | # WARNING: these variables are somewhat deprecated and not |
|
1762 | # WARNING: these variables are somewhat deprecated and not | |
1759 | # necessarily safe to use in a threaded environment, but tools |
|
1763 | # necessarily safe to use in a threaded environment, but tools | |
1760 | # like pdb depend on their existence, so let's set them. If we |
|
1764 | # like pdb depend on their existence, so let's set them. If we | |
1761 | # find problems in the field, we'll need to revisit their use. |
|
1765 | # find problems in the field, we'll need to revisit their use. | |
1762 | sys.last_type = etype |
|
1766 | sys.last_type = etype | |
1763 | sys.last_value = value |
|
1767 | sys.last_value = value | |
1764 | sys.last_traceback = tb |
|
1768 | sys.last_traceback = tb | |
1765 |
|
1769 | |||
1766 | return etype, value, tb |
|
1770 | return etype, value, tb | |
1767 |
|
1771 | |||
1768 | def show_usage_error(self, exc): |
|
1772 | def show_usage_error(self, exc): | |
1769 | """Show a short message for UsageErrors |
|
1773 | """Show a short message for UsageErrors | |
1770 |
|
1774 | |||
1771 | These are special exceptions that shouldn't show a traceback. |
|
1775 | These are special exceptions that shouldn't show a traceback. | |
1772 | """ |
|
1776 | """ | |
1773 | print("UsageError: %s" % exc, file=sys.stderr) |
|
1777 | print("UsageError: %s" % exc, file=sys.stderr) | |
1774 |
|
1778 | |||
1775 | def get_exception_only(self, exc_tuple=None): |
|
1779 | def get_exception_only(self, exc_tuple=None): | |
1776 | """ |
|
1780 | """ | |
1777 | Return as a string (ending with a newline) the exception that |
|
1781 | Return as a string (ending with a newline) the exception that | |
1778 | just occurred, without any traceback. |
|
1782 | just occurred, without any traceback. | |
1779 | """ |
|
1783 | """ | |
1780 | etype, value, tb = self._get_exc_info(exc_tuple) |
|
1784 | etype, value, tb = self._get_exc_info(exc_tuple) | |
1781 | msg = traceback.format_exception_only(etype, value) |
|
1785 | msg = traceback.format_exception_only(etype, value) | |
1782 | return ''.join(msg) |
|
1786 | return ''.join(msg) | |
1783 |
|
1787 | |||
1784 | def showtraceback(self, exc_tuple=None, filename=None, tb_offset=None, |
|
1788 | def showtraceback(self, exc_tuple=None, filename=None, tb_offset=None, | |
1785 | exception_only=False): |
|
1789 | exception_only=False): | |
1786 | """Display the exception that just occurred. |
|
1790 | """Display the exception that just occurred. | |
1787 |
|
1791 | |||
1788 | If nothing is known about the exception, this is the method which |
|
1792 | If nothing is known about the exception, this is the method which | |
1789 | should be used throughout the code for presenting user tracebacks, |
|
1793 | should be used throughout the code for presenting user tracebacks, | |
1790 | rather than directly invoking the InteractiveTB object. |
|
1794 | rather than directly invoking the InteractiveTB object. | |
1791 |
|
1795 | |||
1792 | A specific showsyntaxerror() also exists, but this method can take |
|
1796 | A specific showsyntaxerror() also exists, but this method can take | |
1793 | care of calling it if needed, so unless you are explicitly catching a |
|
1797 | care of calling it if needed, so unless you are explicitly catching a | |
1794 | SyntaxError exception, don't try to analyze the stack manually and |
|
1798 | SyntaxError exception, don't try to analyze the stack manually and | |
1795 | simply call this method.""" |
|
1799 | simply call this method.""" | |
1796 |
|
1800 | |||
1797 | try: |
|
1801 | try: | |
1798 | try: |
|
1802 | try: | |
1799 | etype, value, tb = self._get_exc_info(exc_tuple) |
|
1803 | etype, value, tb = self._get_exc_info(exc_tuple) | |
1800 | except ValueError: |
|
1804 | except ValueError: | |
1801 | print('No traceback available to show.', file=sys.stderr) |
|
1805 | print('No traceback available to show.', file=sys.stderr) | |
1802 | return |
|
1806 | return | |
1803 |
|
1807 | |||
1804 | if issubclass(etype, SyntaxError): |
|
1808 | if issubclass(etype, SyntaxError): | |
1805 | # Though this won't be called by syntax errors in the input |
|
1809 | # Though this won't be called by syntax errors in the input | |
1806 | # line, there may be SyntaxError cases with imported code. |
|
1810 | # line, there may be SyntaxError cases with imported code. | |
1807 | self.showsyntaxerror(filename) |
|
1811 | self.showsyntaxerror(filename) | |
1808 | elif etype is UsageError: |
|
1812 | elif etype is UsageError: | |
1809 | self.show_usage_error(value) |
|
1813 | self.show_usage_error(value) | |
1810 | else: |
|
1814 | else: | |
1811 | if exception_only: |
|
1815 | if exception_only: | |
1812 | stb = ['An exception has occurred, use %tb to see ' |
|
1816 | stb = ['An exception has occurred, use %tb to see ' | |
1813 | 'the full traceback.\n'] |
|
1817 | 'the full traceback.\n'] | |
1814 | stb.extend(self.InteractiveTB.get_exception_only(etype, |
|
1818 | stb.extend(self.InteractiveTB.get_exception_only(etype, | |
1815 | value)) |
|
1819 | value)) | |
1816 | else: |
|
1820 | else: | |
1817 | try: |
|
1821 | try: | |
1818 | # Exception classes can customise their traceback - we |
|
1822 | # Exception classes can customise their traceback - we | |
1819 | # use this in IPython.parallel for exceptions occurring |
|
1823 | # use this in IPython.parallel for exceptions occurring | |
1820 | # in the engines. This should return a list of strings. |
|
1824 | # in the engines. This should return a list of strings. | |
1821 | stb = value._render_traceback_() |
|
1825 | stb = value._render_traceback_() | |
1822 | except Exception: |
|
1826 | except Exception: | |
1823 | stb = self.InteractiveTB.structured_traceback(etype, |
|
1827 | stb = self.InteractiveTB.structured_traceback(etype, | |
1824 | value, tb, tb_offset=tb_offset) |
|
1828 | value, tb, tb_offset=tb_offset) | |
1825 |
|
1829 | |||
1826 | self._showtraceback(etype, value, stb) |
|
1830 | self._showtraceback(etype, value, stb) | |
1827 | if self.call_pdb: |
|
1831 | if self.call_pdb: | |
1828 | # drop into debugger |
|
1832 | # drop into debugger | |
1829 | self.debugger(force=True) |
|
1833 | self.debugger(force=True) | |
1830 | return |
|
1834 | return | |
1831 |
|
1835 | |||
1832 | # Actually show the traceback |
|
1836 | # Actually show the traceback | |
1833 | self._showtraceback(etype, value, stb) |
|
1837 | self._showtraceback(etype, value, stb) | |
1834 |
|
1838 | |||
1835 | except KeyboardInterrupt: |
|
1839 | except KeyboardInterrupt: | |
1836 | print('\n' + self.get_exception_only(), file=sys.stderr) |
|
1840 | print('\n' + self.get_exception_only(), file=sys.stderr) | |
1837 |
|
1841 | |||
1838 | def _showtraceback(self, etype, evalue, stb): |
|
1842 | def _showtraceback(self, etype, evalue, stb): | |
1839 | """Actually show a traceback. |
|
1843 | """Actually show a traceback. | |
1840 |
|
1844 | |||
1841 | Subclasses may override this method to put the traceback on a different |
|
1845 | Subclasses may override this method to put the traceback on a different | |
1842 | place, like a side channel. |
|
1846 | place, like a side channel. | |
1843 | """ |
|
1847 | """ | |
1844 | print(self.InteractiveTB.stb2text(stb)) |
|
1848 | print(self.InteractiveTB.stb2text(stb)) | |
1845 |
|
1849 | |||
1846 | def showsyntaxerror(self, filename=None): |
|
1850 | def showsyntaxerror(self, filename=None): | |
1847 | """Display the syntax error that just occurred. |
|
1851 | """Display the syntax error that just occurred. | |
1848 |
|
1852 | |||
1849 | This doesn't display a stack trace because there isn't one. |
|
1853 | This doesn't display a stack trace because there isn't one. | |
1850 |
|
1854 | |||
1851 | If a filename is given, it is stuffed in the exception instead |
|
1855 | If a filename is given, it is stuffed in the exception instead | |
1852 | of what was there before (because Python's parser always uses |
|
1856 | of what was there before (because Python's parser always uses | |
1853 | "<string>" when reading from a string). |
|
1857 | "<string>" when reading from a string). | |
1854 | """ |
|
1858 | """ | |
1855 | etype, value, last_traceback = self._get_exc_info() |
|
1859 | etype, value, last_traceback = self._get_exc_info() | |
1856 |
|
1860 | |||
1857 | if filename and issubclass(etype, SyntaxError): |
|
1861 | if filename and issubclass(etype, SyntaxError): | |
1858 | try: |
|
1862 | try: | |
1859 | value.filename = filename |
|
1863 | value.filename = filename | |
1860 | except: |
|
1864 | except: | |
1861 | # Not the format we expect; leave it alone |
|
1865 | # Not the format we expect; leave it alone | |
1862 | pass |
|
1866 | pass | |
1863 |
|
1867 | |||
1864 | stb = self.SyntaxTB.structured_traceback(etype, value, []) |
|
1868 | stb = self.SyntaxTB.structured_traceback(etype, value, []) | |
1865 | self._showtraceback(etype, value, stb) |
|
1869 | self._showtraceback(etype, value, stb) | |
1866 |
|
1870 | |||
1867 | # This is overridden in TerminalInteractiveShell to show a message about |
|
1871 | # This is overridden in TerminalInteractiveShell to show a message about | |
1868 | # the %paste magic. |
|
1872 | # the %paste magic. | |
1869 | def showindentationerror(self): |
|
1873 | def showindentationerror(self): | |
1870 | """Called by run_cell when there's an IndentationError in code entered |
|
1874 | """Called by run_cell when there's an IndentationError in code entered | |
1871 | at the prompt. |
|
1875 | at the prompt. | |
1872 |
|
1876 | |||
1873 | This is overridden in TerminalInteractiveShell to show a message about |
|
1877 | This is overridden in TerminalInteractiveShell to show a message about | |
1874 | the %paste magic.""" |
|
1878 | the %paste magic.""" | |
1875 | self.showsyntaxerror() |
|
1879 | self.showsyntaxerror() | |
1876 |
|
1880 | |||
1877 | #------------------------------------------------------------------------- |
|
1881 | #------------------------------------------------------------------------- | |
1878 | # Things related to readline |
|
1882 | # Things related to readline | |
1879 | #------------------------------------------------------------------------- |
|
1883 | #------------------------------------------------------------------------- | |
1880 |
|
1884 | |||
1881 | def init_readline(self): |
|
1885 | def init_readline(self): | |
1882 | """Moved to terminal subclass, here only to simplify the init logic.""" |
|
1886 | """Moved to terminal subclass, here only to simplify the init logic.""" | |
1883 | self.readline = None |
|
1887 | self.readline = None | |
1884 | # Set a number of methods that depend on readline to be no-op |
|
1888 | # Set a number of methods that depend on readline to be no-op | |
1885 | self.readline_no_record = NoOpContext() |
|
1889 | self.readline_no_record = NoOpContext() | |
1886 | self.set_readline_completer = no_op |
|
1890 | self.set_readline_completer = no_op | |
1887 | self.set_custom_completer = no_op |
|
1891 | self.set_custom_completer = no_op | |
1888 |
|
1892 | |||
1889 | @skip_doctest |
|
1893 | @skip_doctest | |
1890 | def set_next_input(self, s, replace=False): |
|
1894 | def set_next_input(self, s, replace=False): | |
1891 | """ Sets the 'default' input string for the next command line. |
|
1895 | """ Sets the 'default' input string for the next command line. | |
1892 |
|
1896 | |||
1893 | Example:: |
|
1897 | Example:: | |
1894 |
|
1898 | |||
1895 | In [1]: _ip.set_next_input("Hello Word") |
|
1899 | In [1]: _ip.set_next_input("Hello Word") | |
1896 | In [2]: Hello Word_ # cursor is here |
|
1900 | In [2]: Hello Word_ # cursor is here | |
1897 | """ |
|
1901 | """ | |
1898 | self.rl_next_input = py3compat.cast_bytes_py2(s) |
|
1902 | self.rl_next_input = py3compat.cast_bytes_py2(s) | |
1899 |
|
1903 | |||
1900 | def _indent_current_str(self): |
|
1904 | def _indent_current_str(self): | |
1901 | """return the current level of indentation as a string""" |
|
1905 | """return the current level of indentation as a string""" | |
1902 | return self.input_splitter.indent_spaces * ' ' |
|
1906 | return self.input_splitter.indent_spaces * ' ' | |
1903 |
|
1907 | |||
1904 | #------------------------------------------------------------------------- |
|
1908 | #------------------------------------------------------------------------- | |
1905 | # Things related to text completion |
|
1909 | # Things related to text completion | |
1906 | #------------------------------------------------------------------------- |
|
1910 | #------------------------------------------------------------------------- | |
1907 |
|
1911 | |||
1908 | def init_completer(self): |
|
1912 | def init_completer(self): | |
1909 | """Initialize the completion machinery. |
|
1913 | """Initialize the completion machinery. | |
1910 |
|
1914 | |||
1911 | This creates completion machinery that can be used by client code, |
|
1915 | This creates completion machinery that can be used by client code, | |
1912 | either interactively in-process (typically triggered by the readline |
|
1916 | either interactively in-process (typically triggered by the readline | |
1913 | library), programmatically (such as in test suites) or out-of-process |
|
1917 | library), programmatically (such as in test suites) or out-of-process | |
1914 | (typically over the network by remote frontends). |
|
1918 | (typically over the network by remote frontends). | |
1915 | """ |
|
1919 | """ | |
1916 | from IPython.core.completer import IPCompleter |
|
1920 | from IPython.core.completer import IPCompleter | |
1917 | from IPython.core.completerlib import (module_completer, |
|
1921 | from IPython.core.completerlib import (module_completer, | |
1918 | magic_run_completer, cd_completer, reset_completer) |
|
1922 | magic_run_completer, cd_completer, reset_completer) | |
1919 |
|
1923 | |||
1920 | self.Completer = IPCompleter(shell=self, |
|
1924 | self.Completer = IPCompleter(shell=self, | |
1921 | namespace=self.user_ns, |
|
1925 | namespace=self.user_ns, | |
1922 | global_namespace=self.user_global_ns, |
|
1926 | global_namespace=self.user_global_ns, | |
1923 | use_readline=self.has_readline, |
|
1927 | use_readline=self.has_readline, | |
1924 | parent=self, |
|
1928 | parent=self, | |
1925 | ) |
|
1929 | ) | |
1926 | self.configurables.append(self.Completer) |
|
1930 | self.configurables.append(self.Completer) | |
1927 |
|
1931 | |||
1928 | # Add custom completers to the basic ones built into IPCompleter |
|
1932 | # Add custom completers to the basic ones built into IPCompleter | |
1929 | sdisp = self.strdispatchers.get('complete_command', StrDispatch()) |
|
1933 | sdisp = self.strdispatchers.get('complete_command', StrDispatch()) | |
1930 | self.strdispatchers['complete_command'] = sdisp |
|
1934 | self.strdispatchers['complete_command'] = sdisp | |
1931 | self.Completer.custom_completers = sdisp |
|
1935 | self.Completer.custom_completers = sdisp | |
1932 |
|
1936 | |||
1933 | self.set_hook('complete_command', module_completer, str_key = 'import') |
|
1937 | self.set_hook('complete_command', module_completer, str_key = 'import') | |
1934 | self.set_hook('complete_command', module_completer, str_key = 'from') |
|
1938 | self.set_hook('complete_command', module_completer, str_key = 'from') | |
1935 | self.set_hook('complete_command', module_completer, str_key = '%aimport') |
|
1939 | self.set_hook('complete_command', module_completer, str_key = '%aimport') | |
1936 | self.set_hook('complete_command', magic_run_completer, str_key = '%run') |
|
1940 | self.set_hook('complete_command', magic_run_completer, str_key = '%run') | |
1937 | self.set_hook('complete_command', cd_completer, str_key = '%cd') |
|
1941 | self.set_hook('complete_command', cd_completer, str_key = '%cd') | |
1938 | self.set_hook('complete_command', reset_completer, str_key = '%reset') |
|
1942 | self.set_hook('complete_command', reset_completer, str_key = '%reset') | |
1939 |
|
1943 | |||
1940 |
|
1944 | |||
1941 | @skip_doctest_py2 |
|
1945 | @skip_doctest_py2 | |
1942 | def complete(self, text, line=None, cursor_pos=None): |
|
1946 | def complete(self, text, line=None, cursor_pos=None): | |
1943 | """Return the completed text and a list of completions. |
|
1947 | """Return the completed text and a list of completions. | |
1944 |
|
1948 | |||
1945 | Parameters |
|
1949 | Parameters | |
1946 | ---------- |
|
1950 | ---------- | |
1947 |
|
1951 | |||
1948 | text : string |
|
1952 | text : string | |
1949 | A string of text to be completed on. It can be given as empty and |
|
1953 | A string of text to be completed on. It can be given as empty and | |
1950 | instead a line/position pair are given. In this case, the |
|
1954 | instead a line/position pair are given. In this case, the | |
1951 | completer itself will split the line like readline does. |
|
1955 | completer itself will split the line like readline does. | |
1952 |
|
1956 | |||
1953 | line : string, optional |
|
1957 | line : string, optional | |
1954 | The complete line that text is part of. |
|
1958 | The complete line that text is part of. | |
1955 |
|
1959 | |||
1956 | cursor_pos : int, optional |
|
1960 | cursor_pos : int, optional | |
1957 | The position of the cursor on the input line. |
|
1961 | The position of the cursor on the input line. | |
1958 |
|
1962 | |||
1959 | Returns |
|
1963 | Returns | |
1960 | ------- |
|
1964 | ------- | |
1961 | text : string |
|
1965 | text : string | |
1962 | The actual text that was completed. |
|
1966 | The actual text that was completed. | |
1963 |
|
1967 | |||
1964 | matches : list |
|
1968 | matches : list | |
1965 | A sorted list with all possible completions. |
|
1969 | A sorted list with all possible completions. | |
1966 |
|
1970 | |||
1967 | The optional arguments allow the completion to take more context into |
|
1971 | The optional arguments allow the completion to take more context into | |
1968 | account, and are part of the low-level completion API. |
|
1972 | account, and are part of the low-level completion API. | |
1969 |
|
1973 | |||
1970 | This is a wrapper around the completion mechanism, similar to what |
|
1974 | This is a wrapper around the completion mechanism, similar to what | |
1971 | readline does at the command line when the TAB key is hit. By |
|
1975 | readline does at the command line when the TAB key is hit. By | |
1972 | exposing it as a method, it can be used by other non-readline |
|
1976 | exposing it as a method, it can be used by other non-readline | |
1973 | environments (such as GUIs) for text completion. |
|
1977 | environments (such as GUIs) for text completion. | |
1974 |
|
1978 | |||
1975 | Simple usage example: |
|
1979 | Simple usage example: | |
1976 |
|
1980 | |||
1977 | In [1]: x = 'hello' |
|
1981 | In [1]: x = 'hello' | |
1978 |
|
1982 | |||
1979 | In [2]: _ip.complete('x.l') |
|
1983 | In [2]: _ip.complete('x.l') | |
1980 | Out[2]: ('x.l', ['x.ljust', 'x.lower', 'x.lstrip']) |
|
1984 | Out[2]: ('x.l', ['x.ljust', 'x.lower', 'x.lstrip']) | |
1981 | """ |
|
1985 | """ | |
1982 |
|
1986 | |||
1983 | # Inject names into __builtin__ so we can complete on the added names. |
|
1987 | # Inject names into __builtin__ so we can complete on the added names. | |
1984 | with self.builtin_trap: |
|
1988 | with self.builtin_trap: | |
1985 | return self.Completer.complete(text, line, cursor_pos) |
|
1989 | return self.Completer.complete(text, line, cursor_pos) | |
1986 |
|
1990 | |||
1987 | def set_custom_completer(self, completer, pos=0): |
|
1991 | def set_custom_completer(self, completer, pos=0): | |
1988 | """Adds a new custom completer function. |
|
1992 | """Adds a new custom completer function. | |
1989 |
|
1993 | |||
1990 | The position argument (defaults to 0) is the index in the completers |
|
1994 | The position argument (defaults to 0) is the index in the completers | |
1991 | list where you want the completer to be inserted.""" |
|
1995 | list where you want the completer to be inserted.""" | |
1992 |
|
1996 | |||
1993 | newcomp = types.MethodType(completer,self.Completer) |
|
1997 | newcomp = types.MethodType(completer,self.Completer) | |
1994 | self.Completer.matchers.insert(pos,newcomp) |
|
1998 | self.Completer.matchers.insert(pos,newcomp) | |
1995 |
|
1999 | |||
1996 | def set_completer_frame(self, frame=None): |
|
2000 | def set_completer_frame(self, frame=None): | |
1997 | """Set the frame of the completer.""" |
|
2001 | """Set the frame of the completer.""" | |
1998 | if frame: |
|
2002 | if frame: | |
1999 | self.Completer.namespace = frame.f_locals |
|
2003 | self.Completer.namespace = frame.f_locals | |
2000 | self.Completer.global_namespace = frame.f_globals |
|
2004 | self.Completer.global_namespace = frame.f_globals | |
2001 | else: |
|
2005 | else: | |
2002 | self.Completer.namespace = self.user_ns |
|
2006 | self.Completer.namespace = self.user_ns | |
2003 | self.Completer.global_namespace = self.user_global_ns |
|
2007 | self.Completer.global_namespace = self.user_global_ns | |
2004 |
|
2008 | |||
2005 | #------------------------------------------------------------------------- |
|
2009 | #------------------------------------------------------------------------- | |
2006 | # Things related to magics |
|
2010 | # Things related to magics | |
2007 | #------------------------------------------------------------------------- |
|
2011 | #------------------------------------------------------------------------- | |
2008 |
|
2012 | |||
2009 | def init_magics(self): |
|
2013 | def init_magics(self): | |
2010 | from IPython.core import magics as m |
|
2014 | from IPython.core import magics as m | |
2011 | self.magics_manager = magic.MagicsManager(shell=self, |
|
2015 | self.magics_manager = magic.MagicsManager(shell=self, | |
2012 | parent=self, |
|
2016 | parent=self, | |
2013 | user_magics=m.UserMagics(self)) |
|
2017 | user_magics=m.UserMagics(self)) | |
2014 | self.configurables.append(self.magics_manager) |
|
2018 | self.configurables.append(self.magics_manager) | |
2015 |
|
2019 | |||
2016 | # Expose as public API from the magics manager |
|
2020 | # Expose as public API from the magics manager | |
2017 | self.register_magics = self.magics_manager.register |
|
2021 | self.register_magics = self.magics_manager.register | |
2018 | self.define_magic = self.magics_manager.define_magic |
|
2022 | self.define_magic = self.magics_manager.define_magic | |
2019 |
|
2023 | |||
2020 | self.register_magics(m.AutoMagics, m.BasicMagics, m.CodeMagics, |
|
2024 | self.register_magics(m.AutoMagics, m.BasicMagics, m.CodeMagics, | |
2021 | m.ConfigMagics, m.DeprecatedMagics, m.DisplayMagics, m.ExecutionMagics, |
|
2025 | m.ConfigMagics, m.DeprecatedMagics, m.DisplayMagics, m.ExecutionMagics, | |
2022 | m.ExtensionMagics, m.HistoryMagics, m.LoggingMagics, |
|
2026 | m.ExtensionMagics, m.HistoryMagics, m.LoggingMagics, | |
2023 | m.NamespaceMagics, m.OSMagics, m.PylabMagics, m.ScriptMagics, |
|
2027 | m.NamespaceMagics, m.OSMagics, m.PylabMagics, m.ScriptMagics, | |
2024 | ) |
|
2028 | ) | |
2025 |
|
2029 | |||
2026 | # Register Magic Aliases |
|
2030 | # Register Magic Aliases | |
2027 | mman = self.magics_manager |
|
2031 | mman = self.magics_manager | |
2028 | # FIXME: magic aliases should be defined by the Magics classes |
|
2032 | # FIXME: magic aliases should be defined by the Magics classes | |
2029 | # or in MagicsManager, not here |
|
2033 | # or in MagicsManager, not here | |
2030 | mman.register_alias('ed', 'edit') |
|
2034 | mman.register_alias('ed', 'edit') | |
2031 | mman.register_alias('hist', 'history') |
|
2035 | mman.register_alias('hist', 'history') | |
2032 | mman.register_alias('rep', 'recall') |
|
2036 | mman.register_alias('rep', 'recall') | |
2033 | mman.register_alias('SVG', 'svg', 'cell') |
|
2037 | mman.register_alias('SVG', 'svg', 'cell') | |
2034 | mman.register_alias('HTML', 'html', 'cell') |
|
2038 | mman.register_alias('HTML', 'html', 'cell') | |
2035 | mman.register_alias('file', 'writefile', 'cell') |
|
2039 | mman.register_alias('file', 'writefile', 'cell') | |
2036 |
|
2040 | |||
2037 | # FIXME: Move the color initialization to the DisplayHook, which |
|
2041 | # FIXME: Move the color initialization to the DisplayHook, which | |
2038 | # should be split into a prompt manager and displayhook. We probably |
|
2042 | # should be split into a prompt manager and displayhook. We probably | |
2039 | # even need a centralize colors management object. |
|
2043 | # even need a centralize colors management object. | |
2040 | self.magic('colors %s' % self.colors) |
|
2044 | self.magic('colors %s' % self.colors) | |
2041 |
|
2045 | |||
2042 | # Defined here so that it's included in the documentation |
|
2046 | # Defined here so that it's included in the documentation | |
2043 | @functools.wraps(magic.MagicsManager.register_function) |
|
2047 | @functools.wraps(magic.MagicsManager.register_function) | |
2044 | def register_magic_function(self, func, magic_kind='line', magic_name=None): |
|
2048 | def register_magic_function(self, func, magic_kind='line', magic_name=None): | |
2045 | self.magics_manager.register_function(func, |
|
2049 | self.magics_manager.register_function(func, | |
2046 | magic_kind=magic_kind, magic_name=magic_name) |
|
2050 | magic_kind=magic_kind, magic_name=magic_name) | |
2047 |
|
2051 | |||
2048 | def run_line_magic(self, magic_name, line): |
|
2052 | def run_line_magic(self, magic_name, line): | |
2049 | """Execute the given line magic. |
|
2053 | """Execute the given line magic. | |
2050 |
|
2054 | |||
2051 | Parameters |
|
2055 | Parameters | |
2052 | ---------- |
|
2056 | ---------- | |
2053 | magic_name : str |
|
2057 | magic_name : str | |
2054 | Name of the desired magic function, without '%' prefix. |
|
2058 | Name of the desired magic function, without '%' prefix. | |
2055 |
|
2059 | |||
2056 | line : str |
|
2060 | line : str | |
2057 | The rest of the input line as a single string. |
|
2061 | The rest of the input line as a single string. | |
2058 | """ |
|
2062 | """ | |
2059 | fn = self.find_line_magic(magic_name) |
|
2063 | fn = self.find_line_magic(magic_name) | |
2060 | if fn is None: |
|
2064 | if fn is None: | |
2061 | cm = self.find_cell_magic(magic_name) |
|
2065 | cm = self.find_cell_magic(magic_name) | |
2062 | etpl = "Line magic function `%%%s` not found%s." |
|
2066 | etpl = "Line magic function `%%%s` not found%s." | |
2063 | extra = '' if cm is None else (' (But cell magic `%%%%%s` exists, ' |
|
2067 | extra = '' if cm is None else (' (But cell magic `%%%%%s` exists, ' | |
2064 | 'did you mean that instead?)' % magic_name ) |
|
2068 | 'did you mean that instead?)' % magic_name ) | |
2065 | error(etpl % (magic_name, extra)) |
|
2069 | error(etpl % (magic_name, extra)) | |
2066 | else: |
|
2070 | else: | |
2067 | # Note: this is the distance in the stack to the user's frame. |
|
2071 | # Note: this is the distance in the stack to the user's frame. | |
2068 | # This will need to be updated if the internal calling logic gets |
|
2072 | # This will need to be updated if the internal calling logic gets | |
2069 | # refactored, or else we'll be expanding the wrong variables. |
|
2073 | # refactored, or else we'll be expanding the wrong variables. | |
2070 | stack_depth = 2 |
|
2074 | stack_depth = 2 | |
2071 | magic_arg_s = self.var_expand(line, stack_depth) |
|
2075 | magic_arg_s = self.var_expand(line, stack_depth) | |
2072 | # Put magic args in a list so we can call with f(*a) syntax |
|
2076 | # Put magic args in a list so we can call with f(*a) syntax | |
2073 | args = [magic_arg_s] |
|
2077 | args = [magic_arg_s] | |
2074 | kwargs = {} |
|
2078 | kwargs = {} | |
2075 | # Grab local namespace if we need it: |
|
2079 | # Grab local namespace if we need it: | |
2076 | if getattr(fn, "needs_local_scope", False): |
|
2080 | if getattr(fn, "needs_local_scope", False): | |
2077 | kwargs['local_ns'] = sys._getframe(stack_depth).f_locals |
|
2081 | kwargs['local_ns'] = sys._getframe(stack_depth).f_locals | |
2078 | with self.builtin_trap: |
|
2082 | with self.builtin_trap: | |
2079 | result = fn(*args,**kwargs) |
|
2083 | result = fn(*args,**kwargs) | |
2080 | return result |
|
2084 | return result | |
2081 |
|
2085 | |||
2082 | def run_cell_magic(self, magic_name, line, cell): |
|
2086 | def run_cell_magic(self, magic_name, line, cell): | |
2083 | """Execute the given cell magic. |
|
2087 | """Execute the given cell magic. | |
2084 |
|
2088 | |||
2085 | Parameters |
|
2089 | Parameters | |
2086 | ---------- |
|
2090 | ---------- | |
2087 | magic_name : str |
|
2091 | magic_name : str | |
2088 | Name of the desired magic function, without '%' prefix. |
|
2092 | Name of the desired magic function, without '%' prefix. | |
2089 |
|
2093 | |||
2090 | line : str |
|
2094 | line : str | |
2091 | The rest of the first input line as a single string. |
|
2095 | The rest of the first input line as a single string. | |
2092 |
|
2096 | |||
2093 | cell : str |
|
2097 | cell : str | |
2094 | The body of the cell as a (possibly multiline) string. |
|
2098 | The body of the cell as a (possibly multiline) string. | |
2095 | """ |
|
2099 | """ | |
2096 | fn = self.find_cell_magic(magic_name) |
|
2100 | fn = self.find_cell_magic(magic_name) | |
2097 | if fn is None: |
|
2101 | if fn is None: | |
2098 | lm = self.find_line_magic(magic_name) |
|
2102 | lm = self.find_line_magic(magic_name) | |
2099 | etpl = "Cell magic `%%{0}` not found{1}." |
|
2103 | etpl = "Cell magic `%%{0}` not found{1}." | |
2100 | extra = '' if lm is None else (' (But line magic `%{0}` exists, ' |
|
2104 | extra = '' if lm is None else (' (But line magic `%{0}` exists, ' | |
2101 | 'did you mean that instead?)'.format(magic_name)) |
|
2105 | 'did you mean that instead?)'.format(magic_name)) | |
2102 | error(etpl.format(magic_name, extra)) |
|
2106 | error(etpl.format(magic_name, extra)) | |
2103 | elif cell == '': |
|
2107 | elif cell == '': | |
2104 | message = '%%{0} is a cell magic, but the cell body is empty.'.format(magic_name) |
|
2108 | message = '%%{0} is a cell magic, but the cell body is empty.'.format(magic_name) | |
2105 | if self.find_line_magic(magic_name) is not None: |
|
2109 | if self.find_line_magic(magic_name) is not None: | |
2106 | message += ' Did you mean the line magic %{0} (single %)?'.format(magic_name) |
|
2110 | message += ' Did you mean the line magic %{0} (single %)?'.format(magic_name) | |
2107 | raise UsageError(message) |
|
2111 | raise UsageError(message) | |
2108 | else: |
|
2112 | else: | |
2109 | # Note: this is the distance in the stack to the user's frame. |
|
2113 | # Note: this is the distance in the stack to the user's frame. | |
2110 | # This will need to be updated if the internal calling logic gets |
|
2114 | # This will need to be updated if the internal calling logic gets | |
2111 | # refactored, or else we'll be expanding the wrong variables. |
|
2115 | # refactored, or else we'll be expanding the wrong variables. | |
2112 | stack_depth = 2 |
|
2116 | stack_depth = 2 | |
2113 | magic_arg_s = self.var_expand(line, stack_depth) |
|
2117 | magic_arg_s = self.var_expand(line, stack_depth) | |
2114 | with self.builtin_trap: |
|
2118 | with self.builtin_trap: | |
2115 | result = fn(magic_arg_s, cell) |
|
2119 | result = fn(magic_arg_s, cell) | |
2116 | return result |
|
2120 | return result | |
2117 |
|
2121 | |||
2118 | def find_line_magic(self, magic_name): |
|
2122 | def find_line_magic(self, magic_name): | |
2119 | """Find and return a line magic by name. |
|
2123 | """Find and return a line magic by name. | |
2120 |
|
2124 | |||
2121 | Returns None if the magic isn't found.""" |
|
2125 | Returns None if the magic isn't found.""" | |
2122 | return self.magics_manager.magics['line'].get(magic_name) |
|
2126 | return self.magics_manager.magics['line'].get(magic_name) | |
2123 |
|
2127 | |||
2124 | def find_cell_magic(self, magic_name): |
|
2128 | def find_cell_magic(self, magic_name): | |
2125 | """Find and return a cell magic by name. |
|
2129 | """Find and return a cell magic by name. | |
2126 |
|
2130 | |||
2127 | Returns None if the magic isn't found.""" |
|
2131 | Returns None if the magic isn't found.""" | |
2128 | return self.magics_manager.magics['cell'].get(magic_name) |
|
2132 | return self.magics_manager.magics['cell'].get(magic_name) | |
2129 |
|
2133 | |||
2130 | def find_magic(self, magic_name, magic_kind='line'): |
|
2134 | def find_magic(self, magic_name, magic_kind='line'): | |
2131 | """Find and return a magic of the given type by name. |
|
2135 | """Find and return a magic of the given type by name. | |
2132 |
|
2136 | |||
2133 | Returns None if the magic isn't found.""" |
|
2137 | Returns None if the magic isn't found.""" | |
2134 | return self.magics_manager.magics[magic_kind].get(magic_name) |
|
2138 | return self.magics_manager.magics[magic_kind].get(magic_name) | |
2135 |
|
2139 | |||
2136 | def magic(self, arg_s): |
|
2140 | def magic(self, arg_s): | |
2137 | """DEPRECATED. Use run_line_magic() instead. |
|
2141 | """DEPRECATED. Use run_line_magic() instead. | |
2138 |
|
2142 | |||
2139 | Call a magic function by name. |
|
2143 | Call a magic function by name. | |
2140 |
|
2144 | |||
2141 | Input: a string containing the name of the magic function to call and |
|
2145 | Input: a string containing the name of the magic function to call and | |
2142 | any additional arguments to be passed to the magic. |
|
2146 | any additional arguments to be passed to the magic. | |
2143 |
|
2147 | |||
2144 | magic('name -opt foo bar') is equivalent to typing at the ipython |
|
2148 | magic('name -opt foo bar') is equivalent to typing at the ipython | |
2145 | prompt: |
|
2149 | prompt: | |
2146 |
|
2150 | |||
2147 | In[1]: %name -opt foo bar |
|
2151 | In[1]: %name -opt foo bar | |
2148 |
|
2152 | |||
2149 | To call a magic without arguments, simply use magic('name'). |
|
2153 | To call a magic without arguments, simply use magic('name'). | |
2150 |
|
2154 | |||
2151 | This provides a proper Python function to call IPython's magics in any |
|
2155 | This provides a proper Python function to call IPython's magics in any | |
2152 | valid Python code you can type at the interpreter, including loops and |
|
2156 | valid Python code you can type at the interpreter, including loops and | |
2153 | compound statements. |
|
2157 | compound statements. | |
2154 | """ |
|
2158 | """ | |
2155 | # TODO: should we issue a loud deprecation warning here? |
|
2159 | # TODO: should we issue a loud deprecation warning here? | |
2156 | magic_name, _, magic_arg_s = arg_s.partition(' ') |
|
2160 | magic_name, _, magic_arg_s = arg_s.partition(' ') | |
2157 | magic_name = magic_name.lstrip(prefilter.ESC_MAGIC) |
|
2161 | magic_name = magic_name.lstrip(prefilter.ESC_MAGIC) | |
2158 | return self.run_line_magic(magic_name, magic_arg_s) |
|
2162 | return self.run_line_magic(magic_name, magic_arg_s) | |
2159 |
|
2163 | |||
2160 | #------------------------------------------------------------------------- |
|
2164 | #------------------------------------------------------------------------- | |
2161 | # Things related to macros |
|
2165 | # Things related to macros | |
2162 | #------------------------------------------------------------------------- |
|
2166 | #------------------------------------------------------------------------- | |
2163 |
|
2167 | |||
2164 | def define_macro(self, name, themacro): |
|
2168 | def define_macro(self, name, themacro): | |
2165 | """Define a new macro |
|
2169 | """Define a new macro | |
2166 |
|
2170 | |||
2167 | Parameters |
|
2171 | Parameters | |
2168 | ---------- |
|
2172 | ---------- | |
2169 | name : str |
|
2173 | name : str | |
2170 | The name of the macro. |
|
2174 | The name of the macro. | |
2171 | themacro : str or Macro |
|
2175 | themacro : str or Macro | |
2172 | The action to do upon invoking the macro. If a string, a new |
|
2176 | The action to do upon invoking the macro. If a string, a new | |
2173 | Macro object is created by passing the string to it. |
|
2177 | Macro object is created by passing the string to it. | |
2174 | """ |
|
2178 | """ | |
2175 |
|
2179 | |||
2176 | from IPython.core import macro |
|
2180 | from IPython.core import macro | |
2177 |
|
2181 | |||
2178 | if isinstance(themacro, string_types): |
|
2182 | if isinstance(themacro, string_types): | |
2179 | themacro = macro.Macro(themacro) |
|
2183 | themacro = macro.Macro(themacro) | |
2180 | if not isinstance(themacro, macro.Macro): |
|
2184 | if not isinstance(themacro, macro.Macro): | |
2181 | raise ValueError('A macro must be a string or a Macro instance.') |
|
2185 | raise ValueError('A macro must be a string or a Macro instance.') | |
2182 | self.user_ns[name] = themacro |
|
2186 | self.user_ns[name] = themacro | |
2183 |
|
2187 | |||
2184 | #------------------------------------------------------------------------- |
|
2188 | #------------------------------------------------------------------------- | |
2185 | # Things related to the running of system commands |
|
2189 | # Things related to the running of system commands | |
2186 | #------------------------------------------------------------------------- |
|
2190 | #------------------------------------------------------------------------- | |
2187 |
|
2191 | |||
2188 | def system_piped(self, cmd): |
|
2192 | def system_piped(self, cmd): | |
2189 | """Call the given cmd in a subprocess, piping stdout/err |
|
2193 | """Call the given cmd in a subprocess, piping stdout/err | |
2190 |
|
2194 | |||
2191 | Parameters |
|
2195 | Parameters | |
2192 | ---------- |
|
2196 | ---------- | |
2193 | cmd : str |
|
2197 | cmd : str | |
2194 | Command to execute (can not end in '&', as background processes are |
|
2198 | Command to execute (can not end in '&', as background processes are | |
2195 | not supported. Should not be a command that expects input |
|
2199 | not supported. Should not be a command that expects input | |
2196 | other than simple text. |
|
2200 | other than simple text. | |
2197 | """ |
|
2201 | """ | |
2198 | if cmd.rstrip().endswith('&'): |
|
2202 | if cmd.rstrip().endswith('&'): | |
2199 | # this is *far* from a rigorous test |
|
2203 | # this is *far* from a rigorous test | |
2200 | # We do not support backgrounding processes because we either use |
|
2204 | # We do not support backgrounding processes because we either use | |
2201 | # pexpect or pipes to read from. Users can always just call |
|
2205 | # pexpect or pipes to read from. Users can always just call | |
2202 | # os.system() or use ip.system=ip.system_raw |
|
2206 | # os.system() or use ip.system=ip.system_raw | |
2203 | # if they really want a background process. |
|
2207 | # if they really want a background process. | |
2204 | raise OSError("Background processes not supported.") |
|
2208 | raise OSError("Background processes not supported.") | |
2205 |
|
2209 | |||
2206 | # we explicitly do NOT return the subprocess status code, because |
|
2210 | # we explicitly do NOT return the subprocess status code, because | |
2207 | # a non-None value would trigger :func:`sys.displayhook` calls. |
|
2211 | # a non-None value would trigger :func:`sys.displayhook` calls. | |
2208 | # Instead, we store the exit_code in user_ns. |
|
2212 | # Instead, we store the exit_code in user_ns. | |
2209 | self.user_ns['_exit_code'] = system(self.var_expand(cmd, depth=1)) |
|
2213 | self.user_ns['_exit_code'] = system(self.var_expand(cmd, depth=1)) | |
2210 |
|
2214 | |||
2211 | def system_raw(self, cmd): |
|
2215 | def system_raw(self, cmd): | |
2212 | """Call the given cmd in a subprocess using os.system on Windows or |
|
2216 | """Call the given cmd in a subprocess using os.system on Windows or | |
2213 | subprocess.call using the system shell on other platforms. |
|
2217 | subprocess.call using the system shell on other platforms. | |
2214 |
|
2218 | |||
2215 | Parameters |
|
2219 | Parameters | |
2216 | ---------- |
|
2220 | ---------- | |
2217 | cmd : str |
|
2221 | cmd : str | |
2218 | Command to execute. |
|
2222 | Command to execute. | |
2219 | """ |
|
2223 | """ | |
2220 | cmd = self.var_expand(cmd, depth=1) |
|
2224 | cmd = self.var_expand(cmd, depth=1) | |
2221 | # protect os.system from UNC paths on Windows, which it can't handle: |
|
2225 | # protect os.system from UNC paths on Windows, which it can't handle: | |
2222 | if sys.platform == 'win32': |
|
2226 | if sys.platform == 'win32': | |
2223 | from IPython.utils._process_win32 import AvoidUNCPath |
|
2227 | from IPython.utils._process_win32 import AvoidUNCPath | |
2224 | with AvoidUNCPath() as path: |
|
2228 | with AvoidUNCPath() as path: | |
2225 | if path is not None: |
|
2229 | if path is not None: | |
2226 | cmd = '"pushd %s &&"%s' % (path, cmd) |
|
2230 | cmd = '"pushd %s &&"%s' % (path, cmd) | |
2227 | cmd = py3compat.unicode_to_str(cmd) |
|
2231 | cmd = py3compat.unicode_to_str(cmd) | |
2228 | try: |
|
2232 | try: | |
2229 | ec = os.system(cmd) |
|
2233 | ec = os.system(cmd) | |
2230 | except KeyboardInterrupt: |
|
2234 | except KeyboardInterrupt: | |
2231 | print('\n' + self.get_exception_only(), file=sys.stderr) |
|
2235 | print('\n' + self.get_exception_only(), file=sys.stderr) | |
2232 | ec = -2 |
|
2236 | ec = -2 | |
2233 | else: |
|
2237 | else: | |
2234 | cmd = py3compat.unicode_to_str(cmd) |
|
2238 | cmd = py3compat.unicode_to_str(cmd) | |
2235 | # For posix the result of the subprocess.call() below is an exit |
|
2239 | # For posix the result of the subprocess.call() below is an exit | |
2236 | # code, which by convention is zero for success, positive for |
|
2240 | # code, which by convention is zero for success, positive for | |
2237 | # program failure. Exit codes above 128 are reserved for signals, |
|
2241 | # program failure. Exit codes above 128 are reserved for signals, | |
2238 | # and the formula for converting a signal to an exit code is usually |
|
2242 | # and the formula for converting a signal to an exit code is usually | |
2239 | # signal_number+128. To more easily differentiate between exit |
|
2243 | # signal_number+128. To more easily differentiate between exit | |
2240 | # codes and signals, ipython uses negative numbers. For instance |
|
2244 | # codes and signals, ipython uses negative numbers. For instance | |
2241 | # since control-c is signal 2 but exit code 130, ipython's |
|
2245 | # since control-c is signal 2 but exit code 130, ipython's | |
2242 | # _exit_code variable will read -2. Note that some shells like |
|
2246 | # _exit_code variable will read -2. Note that some shells like | |
2243 | # csh and fish don't follow sh/bash conventions for exit codes. |
|
2247 | # csh and fish don't follow sh/bash conventions for exit codes. | |
2244 | executable = os.environ.get('SHELL', None) |
|
2248 | executable = os.environ.get('SHELL', None) | |
2245 | try: |
|
2249 | try: | |
2246 | # Use env shell instead of default /bin/sh |
|
2250 | # Use env shell instead of default /bin/sh | |
2247 | ec = subprocess.call(cmd, shell=True, executable=executable) |
|
2251 | ec = subprocess.call(cmd, shell=True, executable=executable) | |
2248 | except KeyboardInterrupt: |
|
2252 | except KeyboardInterrupt: | |
2249 | # intercept control-C; a long traceback is not useful here |
|
2253 | # intercept control-C; a long traceback is not useful here | |
2250 | print('\n' + self.get_exception_only(), file=sys.stderr) |
|
2254 | print('\n' + self.get_exception_only(), file=sys.stderr) | |
2251 | ec = 130 |
|
2255 | ec = 130 | |
2252 | if ec > 128: |
|
2256 | if ec > 128: | |
2253 | ec = -(ec - 128) |
|
2257 | ec = -(ec - 128) | |
2254 |
|
2258 | |||
2255 | # We explicitly do NOT return the subprocess status code, because |
|
2259 | # We explicitly do NOT return the subprocess status code, because | |
2256 | # a non-None value would trigger :func:`sys.displayhook` calls. |
|
2260 | # a non-None value would trigger :func:`sys.displayhook` calls. | |
2257 | # Instead, we store the exit_code in user_ns. Note the semantics |
|
2261 | # Instead, we store the exit_code in user_ns. Note the semantics | |
2258 | # of _exit_code: for control-c, _exit_code == -signal.SIGNIT, |
|
2262 | # of _exit_code: for control-c, _exit_code == -signal.SIGNIT, | |
2259 | # but raising SystemExit(_exit_code) will give status 254! |
|
2263 | # but raising SystemExit(_exit_code) will give status 254! | |
2260 | self.user_ns['_exit_code'] = ec |
|
2264 | self.user_ns['_exit_code'] = ec | |
2261 |
|
2265 | |||
2262 | # use piped system by default, because it is better behaved |
|
2266 | # use piped system by default, because it is better behaved | |
2263 | system = system_piped |
|
2267 | system = system_piped | |
2264 |
|
2268 | |||
2265 | def getoutput(self, cmd, split=True, depth=0): |
|
2269 | def getoutput(self, cmd, split=True, depth=0): | |
2266 | """Get output (possibly including stderr) from a subprocess. |
|
2270 | """Get output (possibly including stderr) from a subprocess. | |
2267 |
|
2271 | |||
2268 | Parameters |
|
2272 | Parameters | |
2269 | ---------- |
|
2273 | ---------- | |
2270 | cmd : str |
|
2274 | cmd : str | |
2271 | Command to execute (can not end in '&', as background processes are |
|
2275 | Command to execute (can not end in '&', as background processes are | |
2272 | not supported. |
|
2276 | not supported. | |
2273 | split : bool, optional |
|
2277 | split : bool, optional | |
2274 | If True, split the output into an IPython SList. Otherwise, an |
|
2278 | If True, split the output into an IPython SList. Otherwise, an | |
2275 | IPython LSString is returned. These are objects similar to normal |
|
2279 | IPython LSString is returned. These are objects similar to normal | |
2276 | lists and strings, with a few convenience attributes for easier |
|
2280 | lists and strings, with a few convenience attributes for easier | |
2277 | manipulation of line-based output. You can use '?' on them for |
|
2281 | manipulation of line-based output. You can use '?' on them for | |
2278 | details. |
|
2282 | details. | |
2279 | depth : int, optional |
|
2283 | depth : int, optional | |
2280 | How many frames above the caller are the local variables which should |
|
2284 | How many frames above the caller are the local variables which should | |
2281 | be expanded in the command string? The default (0) assumes that the |
|
2285 | be expanded in the command string? The default (0) assumes that the | |
2282 | expansion variables are in the stack frame calling this function. |
|
2286 | expansion variables are in the stack frame calling this function. | |
2283 | """ |
|
2287 | """ | |
2284 | if cmd.rstrip().endswith('&'): |
|
2288 | if cmd.rstrip().endswith('&'): | |
2285 | # this is *far* from a rigorous test |
|
2289 | # this is *far* from a rigorous test | |
2286 | raise OSError("Background processes not supported.") |
|
2290 | raise OSError("Background processes not supported.") | |
2287 | out = getoutput(self.var_expand(cmd, depth=depth+1)) |
|
2291 | out = getoutput(self.var_expand(cmd, depth=depth+1)) | |
2288 | if split: |
|
2292 | if split: | |
2289 | out = SList(out.splitlines()) |
|
2293 | out = SList(out.splitlines()) | |
2290 | else: |
|
2294 | else: | |
2291 | out = LSString(out) |
|
2295 | out = LSString(out) | |
2292 | return out |
|
2296 | return out | |
2293 |
|
2297 | |||
2294 | #------------------------------------------------------------------------- |
|
2298 | #------------------------------------------------------------------------- | |
2295 | # Things related to aliases |
|
2299 | # Things related to aliases | |
2296 | #------------------------------------------------------------------------- |
|
2300 | #------------------------------------------------------------------------- | |
2297 |
|
2301 | |||
2298 | def init_alias(self): |
|
2302 | def init_alias(self): | |
2299 | self.alias_manager = AliasManager(shell=self, parent=self) |
|
2303 | self.alias_manager = AliasManager(shell=self, parent=self) | |
2300 | self.configurables.append(self.alias_manager) |
|
2304 | self.configurables.append(self.alias_manager) | |
2301 |
|
2305 | |||
2302 | #------------------------------------------------------------------------- |
|
2306 | #------------------------------------------------------------------------- | |
2303 | # Things related to extensions |
|
2307 | # Things related to extensions | |
2304 | #------------------------------------------------------------------------- |
|
2308 | #------------------------------------------------------------------------- | |
2305 |
|
2309 | |||
2306 | def init_extension_manager(self): |
|
2310 | def init_extension_manager(self): | |
2307 | self.extension_manager = ExtensionManager(shell=self, parent=self) |
|
2311 | self.extension_manager = ExtensionManager(shell=self, parent=self) | |
2308 | self.configurables.append(self.extension_manager) |
|
2312 | self.configurables.append(self.extension_manager) | |
2309 |
|
2313 | |||
2310 | #------------------------------------------------------------------------- |
|
2314 | #------------------------------------------------------------------------- | |
2311 | # Things related to payloads |
|
2315 | # Things related to payloads | |
2312 | #------------------------------------------------------------------------- |
|
2316 | #------------------------------------------------------------------------- | |
2313 |
|
2317 | |||
2314 | def init_payload(self): |
|
2318 | def init_payload(self): | |
2315 | self.payload_manager = PayloadManager(parent=self) |
|
2319 | self.payload_manager = PayloadManager(parent=self) | |
2316 | self.configurables.append(self.payload_manager) |
|
2320 | self.configurables.append(self.payload_manager) | |
2317 |
|
2321 | |||
2318 | #------------------------------------------------------------------------- |
|
2322 | #------------------------------------------------------------------------- | |
2319 | # Things related to the prefilter |
|
2323 | # Things related to the prefilter | |
2320 | #------------------------------------------------------------------------- |
|
2324 | #------------------------------------------------------------------------- | |
2321 |
|
2325 | |||
2322 | def init_prefilter(self): |
|
2326 | def init_prefilter(self): | |
2323 | self.prefilter_manager = PrefilterManager(shell=self, parent=self) |
|
2327 | self.prefilter_manager = PrefilterManager(shell=self, parent=self) | |
2324 | self.configurables.append(self.prefilter_manager) |
|
2328 | self.configurables.append(self.prefilter_manager) | |
2325 | # Ultimately this will be refactored in the new interpreter code, but |
|
2329 | # Ultimately this will be refactored in the new interpreter code, but | |
2326 | # for now, we should expose the main prefilter method (there's legacy |
|
2330 | # for now, we should expose the main prefilter method (there's legacy | |
2327 | # code out there that may rely on this). |
|
2331 | # code out there that may rely on this). | |
2328 | self.prefilter = self.prefilter_manager.prefilter_lines |
|
2332 | self.prefilter = self.prefilter_manager.prefilter_lines | |
2329 |
|
2333 | |||
2330 | def auto_rewrite_input(self, cmd): |
|
2334 | def auto_rewrite_input(self, cmd): | |
2331 | """Print to the screen the rewritten form of the user's command. |
|
2335 | """Print to the screen the rewritten form of the user's command. | |
2332 |
|
2336 | |||
2333 | This shows visual feedback by rewriting input lines that cause |
|
2337 | This shows visual feedback by rewriting input lines that cause | |
2334 | automatic calling to kick in, like:: |
|
2338 | automatic calling to kick in, like:: | |
2335 |
|
2339 | |||
2336 | /f x |
|
2340 | /f x | |
2337 |
|
2341 | |||
2338 | into:: |
|
2342 | into:: | |
2339 |
|
2343 | |||
2340 | ------> f(x) |
|
2344 | ------> f(x) | |
2341 |
|
2345 | |||
2342 | after the user's input prompt. This helps the user understand that the |
|
2346 | after the user's input prompt. This helps the user understand that the | |
2343 | input line was transformed automatically by IPython. |
|
2347 | input line was transformed automatically by IPython. | |
2344 | """ |
|
2348 | """ | |
2345 | if not self.show_rewritten_input: |
|
2349 | if not self.show_rewritten_input: | |
2346 | return |
|
2350 | return | |
2347 |
|
2351 | |||
2348 | rw = self.prompt_manager.render('rewrite') + cmd |
|
2352 | rw = self.prompt_manager.render('rewrite') + cmd | |
2349 |
|
2353 | |||
2350 | try: |
|
2354 | try: | |
2351 | # plain ascii works better w/ pyreadline, on some machines, so |
|
2355 | # plain ascii works better w/ pyreadline, on some machines, so | |
2352 | # we use it and only print uncolored rewrite if we have unicode |
|
2356 | # we use it and only print uncolored rewrite if we have unicode | |
2353 | rw = str(rw) |
|
2357 | rw = str(rw) | |
2354 | print(rw) |
|
2358 | print(rw) | |
2355 | except UnicodeEncodeError: |
|
2359 | except UnicodeEncodeError: | |
2356 | print("------> " + cmd) |
|
2360 | print("------> " + cmd) | |
2357 |
|
2361 | |||
2358 | #------------------------------------------------------------------------- |
|
2362 | #------------------------------------------------------------------------- | |
2359 | # Things related to extracting values/expressions from kernel and user_ns |
|
2363 | # Things related to extracting values/expressions from kernel and user_ns | |
2360 | #------------------------------------------------------------------------- |
|
2364 | #------------------------------------------------------------------------- | |
2361 |
|
2365 | |||
2362 | def _user_obj_error(self): |
|
2366 | def _user_obj_error(self): | |
2363 | """return simple exception dict |
|
2367 | """return simple exception dict | |
2364 |
|
2368 | |||
2365 | for use in user_expressions |
|
2369 | for use in user_expressions | |
2366 | """ |
|
2370 | """ | |
2367 |
|
2371 | |||
2368 | etype, evalue, tb = self._get_exc_info() |
|
2372 | etype, evalue, tb = self._get_exc_info() | |
2369 | stb = self.InteractiveTB.get_exception_only(etype, evalue) |
|
2373 | stb = self.InteractiveTB.get_exception_only(etype, evalue) | |
2370 |
|
2374 | |||
2371 | exc_info = { |
|
2375 | exc_info = { | |
2372 | u'status' : 'error', |
|
2376 | u'status' : 'error', | |
2373 | u'traceback' : stb, |
|
2377 | u'traceback' : stb, | |
2374 | u'ename' : unicode_type(etype.__name__), |
|
2378 | u'ename' : unicode_type(etype.__name__), | |
2375 | u'evalue' : py3compat.safe_unicode(evalue), |
|
2379 | u'evalue' : py3compat.safe_unicode(evalue), | |
2376 | } |
|
2380 | } | |
2377 |
|
2381 | |||
2378 | return exc_info |
|
2382 | return exc_info | |
2379 |
|
2383 | |||
2380 | def _format_user_obj(self, obj): |
|
2384 | def _format_user_obj(self, obj): | |
2381 | """format a user object to display dict |
|
2385 | """format a user object to display dict | |
2382 |
|
2386 | |||
2383 | for use in user_expressions |
|
2387 | for use in user_expressions | |
2384 | """ |
|
2388 | """ | |
2385 |
|
2389 | |||
2386 | data, md = self.display_formatter.format(obj) |
|
2390 | data, md = self.display_formatter.format(obj) | |
2387 | value = { |
|
2391 | value = { | |
2388 | 'status' : 'ok', |
|
2392 | 'status' : 'ok', | |
2389 | 'data' : data, |
|
2393 | 'data' : data, | |
2390 | 'metadata' : md, |
|
2394 | 'metadata' : md, | |
2391 | } |
|
2395 | } | |
2392 | return value |
|
2396 | return value | |
2393 |
|
2397 | |||
2394 | def user_expressions(self, expressions): |
|
2398 | def user_expressions(self, expressions): | |
2395 | """Evaluate a dict of expressions in the user's namespace. |
|
2399 | """Evaluate a dict of expressions in the user's namespace. | |
2396 |
|
2400 | |||
2397 | Parameters |
|
2401 | Parameters | |
2398 | ---------- |
|
2402 | ---------- | |
2399 | expressions : dict |
|
2403 | expressions : dict | |
2400 | A dict with string keys and string values. The expression values |
|
2404 | A dict with string keys and string values. The expression values | |
2401 | should be valid Python expressions, each of which will be evaluated |
|
2405 | should be valid Python expressions, each of which will be evaluated | |
2402 | in the user namespace. |
|
2406 | in the user namespace. | |
2403 |
|
2407 | |||
2404 | Returns |
|
2408 | Returns | |
2405 | ------- |
|
2409 | ------- | |
2406 | A dict, keyed like the input expressions dict, with the rich mime-typed |
|
2410 | A dict, keyed like the input expressions dict, with the rich mime-typed | |
2407 | display_data of each value. |
|
2411 | display_data of each value. | |
2408 | """ |
|
2412 | """ | |
2409 | out = {} |
|
2413 | out = {} | |
2410 | user_ns = self.user_ns |
|
2414 | user_ns = self.user_ns | |
2411 | global_ns = self.user_global_ns |
|
2415 | global_ns = self.user_global_ns | |
2412 |
|
2416 | |||
2413 | for key, expr in iteritems(expressions): |
|
2417 | for key, expr in iteritems(expressions): | |
2414 | try: |
|
2418 | try: | |
2415 | value = self._format_user_obj(eval(expr, global_ns, user_ns)) |
|
2419 | value = self._format_user_obj(eval(expr, global_ns, user_ns)) | |
2416 | except: |
|
2420 | except: | |
2417 | value = self._user_obj_error() |
|
2421 | value = self._user_obj_error() | |
2418 | out[key] = value |
|
2422 | out[key] = value | |
2419 | return out |
|
2423 | return out | |
2420 |
|
2424 | |||
2421 | #------------------------------------------------------------------------- |
|
2425 | #------------------------------------------------------------------------- | |
2422 | # Things related to the running of code |
|
2426 | # Things related to the running of code | |
2423 | #------------------------------------------------------------------------- |
|
2427 | #------------------------------------------------------------------------- | |
2424 |
|
2428 | |||
2425 | def ex(self, cmd): |
|
2429 | def ex(self, cmd): | |
2426 | """Execute a normal python statement in user namespace.""" |
|
2430 | """Execute a normal python statement in user namespace.""" | |
2427 | with self.builtin_trap: |
|
2431 | with self.builtin_trap: | |
2428 | exec(cmd, self.user_global_ns, self.user_ns) |
|
2432 | exec(cmd, self.user_global_ns, self.user_ns) | |
2429 |
|
2433 | |||
2430 | def ev(self, expr): |
|
2434 | def ev(self, expr): | |
2431 | """Evaluate python expression expr in user namespace. |
|
2435 | """Evaluate python expression expr in user namespace. | |
2432 |
|
2436 | |||
2433 | Returns the result of evaluation |
|
2437 | Returns the result of evaluation | |
2434 | """ |
|
2438 | """ | |
2435 | with self.builtin_trap: |
|
2439 | with self.builtin_trap: | |
2436 | return eval(expr, self.user_global_ns, self.user_ns) |
|
2440 | return eval(expr, self.user_global_ns, self.user_ns) | |
2437 |
|
2441 | |||
2438 | def safe_execfile(self, fname, *where, **kw): |
|
2442 | def safe_execfile(self, fname, *where, **kw): | |
2439 | """A safe version of the builtin execfile(). |
|
2443 | """A safe version of the builtin execfile(). | |
2440 |
|
2444 | |||
2441 | This version will never throw an exception, but instead print |
|
2445 | This version will never throw an exception, but instead print | |
2442 | helpful error messages to the screen. This only works on pure |
|
2446 | helpful error messages to the screen. This only works on pure | |
2443 | Python files with the .py extension. |
|
2447 | Python files with the .py extension. | |
2444 |
|
2448 | |||
2445 | Parameters |
|
2449 | Parameters | |
2446 | ---------- |
|
2450 | ---------- | |
2447 | fname : string |
|
2451 | fname : string | |
2448 | The name of the file to be executed. |
|
2452 | The name of the file to be executed. | |
2449 | where : tuple |
|
2453 | where : tuple | |
2450 | One or two namespaces, passed to execfile() as (globals,locals). |
|
2454 | One or two namespaces, passed to execfile() as (globals,locals). | |
2451 | If only one is given, it is passed as both. |
|
2455 | If only one is given, it is passed as both. | |
2452 | exit_ignore : bool (False) |
|
2456 | exit_ignore : bool (False) | |
2453 | If True, then silence SystemExit for non-zero status (it is always |
|
2457 | If True, then silence SystemExit for non-zero status (it is always | |
2454 | silenced for zero status, as it is so common). |
|
2458 | silenced for zero status, as it is so common). | |
2455 | raise_exceptions : bool (False) |
|
2459 | raise_exceptions : bool (False) | |
2456 | If True raise exceptions everywhere. Meant for testing. |
|
2460 | If True raise exceptions everywhere. Meant for testing. | |
2457 | shell_futures : bool (False) |
|
2461 | shell_futures : bool (False) | |
2458 | If True, the code will share future statements with the interactive |
|
2462 | If True, the code will share future statements with the interactive | |
2459 | shell. It will both be affected by previous __future__ imports, and |
|
2463 | shell. It will both be affected by previous __future__ imports, and | |
2460 | any __future__ imports in the code will affect the shell. If False, |
|
2464 | any __future__ imports in the code will affect the shell. If False, | |
2461 | __future__ imports are not shared in either direction. |
|
2465 | __future__ imports are not shared in either direction. | |
2462 |
|
2466 | |||
2463 | """ |
|
2467 | """ | |
2464 | kw.setdefault('exit_ignore', False) |
|
2468 | kw.setdefault('exit_ignore', False) | |
2465 | kw.setdefault('raise_exceptions', False) |
|
2469 | kw.setdefault('raise_exceptions', False) | |
2466 | kw.setdefault('shell_futures', False) |
|
2470 | kw.setdefault('shell_futures', False) | |
2467 |
|
2471 | |||
2468 | fname = os.path.abspath(os.path.expanduser(fname)) |
|
2472 | fname = os.path.abspath(os.path.expanduser(fname)) | |
2469 |
|
2473 | |||
2470 | # Make sure we can open the file |
|
2474 | # Make sure we can open the file | |
2471 | try: |
|
2475 | try: | |
2472 | with open(fname): |
|
2476 | with open(fname): | |
2473 | pass |
|
2477 | pass | |
2474 | except: |
|
2478 | except: | |
2475 | warn('Could not open file <%s> for safe execution.' % fname) |
|
2479 | warn('Could not open file <%s> for safe execution.' % fname) | |
2476 | return |
|
2480 | return | |
2477 |
|
2481 | |||
2478 | # Find things also in current directory. This is needed to mimic the |
|
2482 | # Find things also in current directory. This is needed to mimic the | |
2479 | # behavior of running a script from the system command line, where |
|
2483 | # behavior of running a script from the system command line, where | |
2480 | # Python inserts the script's directory into sys.path |
|
2484 | # Python inserts the script's directory into sys.path | |
2481 | dname = os.path.dirname(fname) |
|
2485 | dname = os.path.dirname(fname) | |
2482 |
|
2486 | |||
2483 | with prepended_to_syspath(dname): |
|
2487 | with prepended_to_syspath(dname): | |
2484 | try: |
|
2488 | try: | |
2485 | glob, loc = (where + (None, ))[:2] |
|
2489 | glob, loc = (where + (None, ))[:2] | |
2486 | py3compat.execfile( |
|
2490 | py3compat.execfile( | |
2487 | fname, glob, loc, |
|
2491 | fname, glob, loc, | |
2488 | self.compile if kw['shell_futures'] else None) |
|
2492 | self.compile if kw['shell_futures'] else None) | |
2489 | except SystemExit as status: |
|
2493 | except SystemExit as status: | |
2490 | # If the call was made with 0 or None exit status (sys.exit(0) |
|
2494 | # If the call was made with 0 or None exit status (sys.exit(0) | |
2491 | # or sys.exit() ), don't bother showing a traceback, as both of |
|
2495 | # or sys.exit() ), don't bother showing a traceback, as both of | |
2492 | # these are considered normal by the OS: |
|
2496 | # these are considered normal by the OS: | |
2493 | # > python -c'import sys;sys.exit(0)'; echo $? |
|
2497 | # > python -c'import sys;sys.exit(0)'; echo $? | |
2494 | # 0 |
|
2498 | # 0 | |
2495 | # > python -c'import sys;sys.exit()'; echo $? |
|
2499 | # > python -c'import sys;sys.exit()'; echo $? | |
2496 | # 0 |
|
2500 | # 0 | |
2497 | # For other exit status, we show the exception unless |
|
2501 | # For other exit status, we show the exception unless | |
2498 | # explicitly silenced, but only in short form. |
|
2502 | # explicitly silenced, but only in short form. | |
2499 | if status.code: |
|
2503 | if status.code: | |
2500 | if kw['raise_exceptions']: |
|
2504 | if kw['raise_exceptions']: | |
2501 | raise |
|
2505 | raise | |
2502 | if not kw['exit_ignore']: |
|
2506 | if not kw['exit_ignore']: | |
2503 | self.showtraceback(exception_only=True) |
|
2507 | self.showtraceback(exception_only=True) | |
2504 | except: |
|
2508 | except: | |
2505 | if kw['raise_exceptions']: |
|
2509 | if kw['raise_exceptions']: | |
2506 | raise |
|
2510 | raise | |
2507 | # tb offset is 2 because we wrap execfile |
|
2511 | # tb offset is 2 because we wrap execfile | |
2508 | self.showtraceback(tb_offset=2) |
|
2512 | self.showtraceback(tb_offset=2) | |
2509 |
|
2513 | |||
2510 | def safe_execfile_ipy(self, fname, shell_futures=False, raise_exceptions=False): |
|
2514 | def safe_execfile_ipy(self, fname, shell_futures=False, raise_exceptions=False): | |
2511 | """Like safe_execfile, but for .ipy or .ipynb files with IPython syntax. |
|
2515 | """Like safe_execfile, but for .ipy or .ipynb files with IPython syntax. | |
2512 |
|
2516 | |||
2513 | Parameters |
|
2517 | Parameters | |
2514 | ---------- |
|
2518 | ---------- | |
2515 | fname : str |
|
2519 | fname : str | |
2516 | The name of the file to execute. The filename must have a |
|
2520 | The name of the file to execute. The filename must have a | |
2517 | .ipy or .ipynb extension. |
|
2521 | .ipy or .ipynb extension. | |
2518 | shell_futures : bool (False) |
|
2522 | shell_futures : bool (False) | |
2519 | If True, the code will share future statements with the interactive |
|
2523 | If True, the code will share future statements with the interactive | |
2520 | shell. It will both be affected by previous __future__ imports, and |
|
2524 | shell. It will both be affected by previous __future__ imports, and | |
2521 | any __future__ imports in the code will affect the shell. If False, |
|
2525 | any __future__ imports in the code will affect the shell. If False, | |
2522 | __future__ imports are not shared in either direction. |
|
2526 | __future__ imports are not shared in either direction. | |
2523 | raise_exceptions : bool (False) |
|
2527 | raise_exceptions : bool (False) | |
2524 | If True raise exceptions everywhere. Meant for testing. |
|
2528 | If True raise exceptions everywhere. Meant for testing. | |
2525 | """ |
|
2529 | """ | |
2526 | fname = os.path.abspath(os.path.expanduser(fname)) |
|
2530 | fname = os.path.abspath(os.path.expanduser(fname)) | |
2527 |
|
2531 | |||
2528 | # Make sure we can open the file |
|
2532 | # Make sure we can open the file | |
2529 | try: |
|
2533 | try: | |
2530 | with open(fname): |
|
2534 | with open(fname): | |
2531 | pass |
|
2535 | pass | |
2532 | except: |
|
2536 | except: | |
2533 | warn('Could not open file <%s> for safe execution.' % fname) |
|
2537 | warn('Could not open file <%s> for safe execution.' % fname) | |
2534 | return |
|
2538 | return | |
2535 |
|
2539 | |||
2536 | # Find things also in current directory. This is needed to mimic the |
|
2540 | # Find things also in current directory. This is needed to mimic the | |
2537 | # behavior of running a script from the system command line, where |
|
2541 | # behavior of running a script from the system command line, where | |
2538 | # Python inserts the script's directory into sys.path |
|
2542 | # Python inserts the script's directory into sys.path | |
2539 | dname = os.path.dirname(fname) |
|
2543 | dname = os.path.dirname(fname) | |
2540 |
|
2544 | |||
2541 | def get_cells(): |
|
2545 | def get_cells(): | |
2542 | """generator for sequence of code blocks to run""" |
|
2546 | """generator for sequence of code blocks to run""" | |
2543 | if fname.endswith('.ipynb'): |
|
2547 | if fname.endswith('.ipynb'): | |
2544 | from nbformat import read |
|
2548 | from nbformat import read | |
2545 | with io_open(fname) as f: |
|
2549 | with io_open(fname) as f: | |
2546 | nb = read(f, as_version=4) |
|
2550 | nb = read(f, as_version=4) | |
2547 | if not nb.cells: |
|
2551 | if not nb.cells: | |
2548 | return |
|
2552 | return | |
2549 | for cell in nb.cells: |
|
2553 | for cell in nb.cells: | |
2550 | if cell.cell_type == 'code': |
|
2554 | if cell.cell_type == 'code': | |
2551 | yield cell.source |
|
2555 | yield cell.source | |
2552 | else: |
|
2556 | else: | |
2553 | with open(fname) as f: |
|
2557 | with open(fname) as f: | |
2554 | yield f.read() |
|
2558 | yield f.read() | |
2555 |
|
2559 | |||
2556 | with prepended_to_syspath(dname): |
|
2560 | with prepended_to_syspath(dname): | |
2557 | try: |
|
2561 | try: | |
2558 | for cell in get_cells(): |
|
2562 | for cell in get_cells(): | |
2559 | result = self.run_cell(cell, silent=True, shell_futures=shell_futures) |
|
2563 | result = self.run_cell(cell, silent=True, shell_futures=shell_futures) | |
2560 | if raise_exceptions: |
|
2564 | if raise_exceptions: | |
2561 | result.raise_error() |
|
2565 | result.raise_error() | |
2562 | elif not result.success: |
|
2566 | elif not result.success: | |
2563 | break |
|
2567 | break | |
2564 | except: |
|
2568 | except: | |
2565 | if raise_exceptions: |
|
2569 | if raise_exceptions: | |
2566 | raise |
|
2570 | raise | |
2567 | self.showtraceback() |
|
2571 | self.showtraceback() | |
2568 | warn('Unknown failure executing file: <%s>' % fname) |
|
2572 | warn('Unknown failure executing file: <%s>' % fname) | |
2569 |
|
2573 | |||
2570 | def safe_run_module(self, mod_name, where): |
|
2574 | def safe_run_module(self, mod_name, where): | |
2571 | """A safe version of runpy.run_module(). |
|
2575 | """A safe version of runpy.run_module(). | |
2572 |
|
2576 | |||
2573 | This version will never throw an exception, but instead print |
|
2577 | This version will never throw an exception, but instead print | |
2574 | helpful error messages to the screen. |
|
2578 | helpful error messages to the screen. | |
2575 |
|
2579 | |||
2576 | `SystemExit` exceptions with status code 0 or None are ignored. |
|
2580 | `SystemExit` exceptions with status code 0 or None are ignored. | |
2577 |
|
2581 | |||
2578 | Parameters |
|
2582 | Parameters | |
2579 | ---------- |
|
2583 | ---------- | |
2580 | mod_name : string |
|
2584 | mod_name : string | |
2581 | The name of the module to be executed. |
|
2585 | The name of the module to be executed. | |
2582 | where : dict |
|
2586 | where : dict | |
2583 | The globals namespace. |
|
2587 | The globals namespace. | |
2584 | """ |
|
2588 | """ | |
2585 | try: |
|
2589 | try: | |
2586 | try: |
|
2590 | try: | |
2587 | where.update( |
|
2591 | where.update( | |
2588 | runpy.run_module(str(mod_name), run_name="__main__", |
|
2592 | runpy.run_module(str(mod_name), run_name="__main__", | |
2589 | alter_sys=True) |
|
2593 | alter_sys=True) | |
2590 | ) |
|
2594 | ) | |
2591 | except SystemExit as status: |
|
2595 | except SystemExit as status: | |
2592 | if status.code: |
|
2596 | if status.code: | |
2593 | raise |
|
2597 | raise | |
2594 | except: |
|
2598 | except: | |
2595 | self.showtraceback() |
|
2599 | self.showtraceback() | |
2596 | warn('Unknown failure executing module: <%s>' % mod_name) |
|
2600 | warn('Unknown failure executing module: <%s>' % mod_name) | |
2597 |
|
2601 | |||
2598 | def run_cell(self, raw_cell, store_history=False, silent=False, shell_futures=True): |
|
2602 | def run_cell(self, raw_cell, store_history=False, silent=False, shell_futures=True): | |
2599 | """Run a complete IPython cell. |
|
2603 | """Run a complete IPython cell. | |
2600 |
|
2604 | |||
2601 | Parameters |
|
2605 | Parameters | |
2602 | ---------- |
|
2606 | ---------- | |
2603 | raw_cell : str |
|
2607 | raw_cell : str | |
2604 | The code (including IPython code such as %magic functions) to run. |
|
2608 | The code (including IPython code such as %magic functions) to run. | |
2605 | store_history : bool |
|
2609 | store_history : bool | |
2606 | If True, the raw and translated cell will be stored in IPython's |
|
2610 | If True, the raw and translated cell will be stored in IPython's | |
2607 | history. For user code calling back into IPython's machinery, this |
|
2611 | history. For user code calling back into IPython's machinery, this | |
2608 | should be set to False. |
|
2612 | should be set to False. | |
2609 | silent : bool |
|
2613 | silent : bool | |
2610 | If True, avoid side-effects, such as implicit displayhooks and |
|
2614 | If True, avoid side-effects, such as implicit displayhooks and | |
2611 | and logging. silent=True forces store_history=False. |
|
2615 | and logging. silent=True forces store_history=False. | |
2612 | shell_futures : bool |
|
2616 | shell_futures : bool | |
2613 | If True, the code will share future statements with the interactive |
|
2617 | If True, the code will share future statements with the interactive | |
2614 | shell. It will both be affected by previous __future__ imports, and |
|
2618 | shell. It will both be affected by previous __future__ imports, and | |
2615 | any __future__ imports in the code will affect the shell. If False, |
|
2619 | any __future__ imports in the code will affect the shell. If False, | |
2616 | __future__ imports are not shared in either direction. |
|
2620 | __future__ imports are not shared in either direction. | |
2617 |
|
2621 | |||
2618 | Returns |
|
2622 | Returns | |
2619 | ------- |
|
2623 | ------- | |
2620 | result : :class:`ExecutionResult` |
|
2624 | result : :class:`ExecutionResult` | |
2621 | """ |
|
2625 | """ | |
2622 | result = ExecutionResult() |
|
2626 | result = ExecutionResult() | |
2623 |
|
2627 | |||
2624 | if (not raw_cell) or raw_cell.isspace(): |
|
2628 | if (not raw_cell) or raw_cell.isspace(): | |
2625 | return result |
|
2629 | return result | |
2626 |
|
2630 | |||
2627 | if silent: |
|
2631 | if silent: | |
2628 | store_history = False |
|
2632 | store_history = False | |
2629 |
|
2633 | |||
2630 | if store_history: |
|
2634 | if store_history: | |
2631 | result.execution_count = self.execution_count |
|
2635 | result.execution_count = self.execution_count | |
2632 |
|
2636 | |||
2633 | def error_before_exec(value): |
|
2637 | def error_before_exec(value): | |
2634 | result.error_before_exec = value |
|
2638 | result.error_before_exec = value | |
2635 | return result |
|
2639 | return result | |
2636 |
|
2640 | |||
2637 | self.events.trigger('pre_execute') |
|
2641 | self.events.trigger('pre_execute') | |
2638 | if not silent: |
|
2642 | if not silent: | |
2639 | self.events.trigger('pre_run_cell') |
|
2643 | self.events.trigger('pre_run_cell') | |
2640 |
|
2644 | |||
2641 | # If any of our input transformation (input_transformer_manager or |
|
2645 | # If any of our input transformation (input_transformer_manager or | |
2642 | # prefilter_manager) raises an exception, we store it in this variable |
|
2646 | # prefilter_manager) raises an exception, we store it in this variable | |
2643 | # so that we can display the error after logging the input and storing |
|
2647 | # so that we can display the error after logging the input and storing | |
2644 | # it in the history. |
|
2648 | # it in the history. | |
2645 | preprocessing_exc_tuple = None |
|
2649 | preprocessing_exc_tuple = None | |
2646 | try: |
|
2650 | try: | |
2647 | # Static input transformations |
|
2651 | # Static input transformations | |
2648 | cell = self.input_transformer_manager.transform_cell(raw_cell) |
|
2652 | cell = self.input_transformer_manager.transform_cell(raw_cell) | |
2649 | except SyntaxError: |
|
2653 | except SyntaxError: | |
2650 | preprocessing_exc_tuple = sys.exc_info() |
|
2654 | preprocessing_exc_tuple = sys.exc_info() | |
2651 | cell = raw_cell # cell has to exist so it can be stored/logged |
|
2655 | cell = raw_cell # cell has to exist so it can be stored/logged | |
2652 | else: |
|
2656 | else: | |
2653 | if len(cell.splitlines()) == 1: |
|
2657 | if len(cell.splitlines()) == 1: | |
2654 | # Dynamic transformations - only applied for single line commands |
|
2658 | # Dynamic transformations - only applied for single line commands | |
2655 | with self.builtin_trap: |
|
2659 | with self.builtin_trap: | |
2656 | try: |
|
2660 | try: | |
2657 | # use prefilter_lines to handle trailing newlines |
|
2661 | # use prefilter_lines to handle trailing newlines | |
2658 | # restore trailing newline for ast.parse |
|
2662 | # restore trailing newline for ast.parse | |
2659 | cell = self.prefilter_manager.prefilter_lines(cell) + '\n' |
|
2663 | cell = self.prefilter_manager.prefilter_lines(cell) + '\n' | |
2660 | except Exception: |
|
2664 | except Exception: | |
2661 | # don't allow prefilter errors to crash IPython |
|
2665 | # don't allow prefilter errors to crash IPython | |
2662 | preprocessing_exc_tuple = sys.exc_info() |
|
2666 | preprocessing_exc_tuple = sys.exc_info() | |
2663 |
|
2667 | |||
2664 | # Store raw and processed history |
|
2668 | # Store raw and processed history | |
2665 | if store_history: |
|
2669 | if store_history: | |
2666 | self.history_manager.store_inputs(self.execution_count, |
|
2670 | self.history_manager.store_inputs(self.execution_count, | |
2667 | cell, raw_cell) |
|
2671 | cell, raw_cell) | |
2668 | if not silent: |
|
2672 | if not silent: | |
2669 | self.logger.log(cell, raw_cell) |
|
2673 | self.logger.log(cell, raw_cell) | |
2670 |
|
2674 | |||
2671 | # Display the exception if input processing failed. |
|
2675 | # Display the exception if input processing failed. | |
2672 | if preprocessing_exc_tuple is not None: |
|
2676 | if preprocessing_exc_tuple is not None: | |
2673 | self.showtraceback(preprocessing_exc_tuple) |
|
2677 | self.showtraceback(preprocessing_exc_tuple) | |
2674 | if store_history: |
|
2678 | if store_history: | |
2675 | self.execution_count += 1 |
|
2679 | self.execution_count += 1 | |
2676 | return error_before_exec(preprocessing_exc_tuple[2]) |
|
2680 | return error_before_exec(preprocessing_exc_tuple[2]) | |
2677 |
|
2681 | |||
2678 | # Our own compiler remembers the __future__ environment. If we want to |
|
2682 | # Our own compiler remembers the __future__ environment. If we want to | |
2679 | # run code with a separate __future__ environment, use the default |
|
2683 | # run code with a separate __future__ environment, use the default | |
2680 | # compiler |
|
2684 | # compiler | |
2681 | compiler = self.compile if shell_futures else CachingCompiler() |
|
2685 | compiler = self.compile if shell_futures else CachingCompiler() | |
2682 |
|
2686 | |||
2683 | with self.builtin_trap: |
|
2687 | with self.builtin_trap: | |
2684 | cell_name = self.compile.cache(cell, self.execution_count) |
|
2688 | cell_name = self.compile.cache(cell, self.execution_count) | |
2685 |
|
2689 | |||
2686 | with self.display_trap: |
|
2690 | with self.display_trap: | |
2687 | # Compile to bytecode |
|
2691 | # Compile to bytecode | |
2688 | try: |
|
2692 | try: | |
2689 | code_ast = compiler.ast_parse(cell, filename=cell_name) |
|
2693 | code_ast = compiler.ast_parse(cell, filename=cell_name) | |
2690 | except IndentationError as e: |
|
2694 | except IndentationError as e: | |
2691 | self.showindentationerror() |
|
2695 | self.showindentationerror() | |
2692 | if store_history: |
|
2696 | if store_history: | |
2693 | self.execution_count += 1 |
|
2697 | self.execution_count += 1 | |
2694 | return error_before_exec(e) |
|
2698 | return error_before_exec(e) | |
2695 | except (OverflowError, SyntaxError, ValueError, TypeError, |
|
2699 | except (OverflowError, SyntaxError, ValueError, TypeError, | |
2696 | MemoryError) as e: |
|
2700 | MemoryError) as e: | |
2697 | self.showsyntaxerror() |
|
2701 | self.showsyntaxerror() | |
2698 | if store_history: |
|
2702 | if store_history: | |
2699 | self.execution_count += 1 |
|
2703 | self.execution_count += 1 | |
2700 | return error_before_exec(e) |
|
2704 | return error_before_exec(e) | |
2701 |
|
2705 | |||
2702 | # Apply AST transformations |
|
2706 | # Apply AST transformations | |
2703 | try: |
|
2707 | try: | |
2704 | code_ast = self.transform_ast(code_ast) |
|
2708 | code_ast = self.transform_ast(code_ast) | |
2705 | except InputRejected as e: |
|
2709 | except InputRejected as e: | |
2706 | self.showtraceback() |
|
2710 | self.showtraceback() | |
2707 | if store_history: |
|
2711 | if store_history: | |
2708 | self.execution_count += 1 |
|
2712 | self.execution_count += 1 | |
2709 | return error_before_exec(e) |
|
2713 | return error_before_exec(e) | |
2710 |
|
2714 | |||
2711 | # Give the displayhook a reference to our ExecutionResult so it |
|
2715 | # Give the displayhook a reference to our ExecutionResult so it | |
2712 | # can fill in the output value. |
|
2716 | # can fill in the output value. | |
2713 | self.displayhook.exec_result = result |
|
2717 | self.displayhook.exec_result = result | |
2714 |
|
2718 | |||
2715 | # Execute the user code |
|
2719 | # Execute the user code | |
2716 | interactivity = "none" if silent else self.ast_node_interactivity |
|
2720 | interactivity = "none" if silent else self.ast_node_interactivity | |
2717 | self.run_ast_nodes(code_ast.body, cell_name, |
|
2721 | self.run_ast_nodes(code_ast.body, cell_name, | |
2718 | interactivity=interactivity, compiler=compiler, result=result) |
|
2722 | interactivity=interactivity, compiler=compiler, result=result) | |
2719 |
|
2723 | |||
2720 | # Reset this so later displayed values do not modify the |
|
2724 | # Reset this so later displayed values do not modify the | |
2721 | # ExecutionResult |
|
2725 | # ExecutionResult | |
2722 | self.displayhook.exec_result = None |
|
2726 | self.displayhook.exec_result = None | |
2723 |
|
2727 | |||
2724 | self.events.trigger('post_execute') |
|
2728 | self.events.trigger('post_execute') | |
2725 | if not silent: |
|
2729 | if not silent: | |
2726 | self.events.trigger('post_run_cell') |
|
2730 | self.events.trigger('post_run_cell') | |
2727 |
|
2731 | |||
2728 | if store_history: |
|
2732 | if store_history: | |
2729 | # Write output to the database. Does nothing unless |
|
2733 | # Write output to the database. Does nothing unless | |
2730 | # history output logging is enabled. |
|
2734 | # history output logging is enabled. | |
2731 | self.history_manager.store_output(self.execution_count) |
|
2735 | self.history_manager.store_output(self.execution_count) | |
2732 | # Each cell is a *single* input, regardless of how many lines it has |
|
2736 | # Each cell is a *single* input, regardless of how many lines it has | |
2733 | self.execution_count += 1 |
|
2737 | self.execution_count += 1 | |
2734 |
|
2738 | |||
2735 | return result |
|
2739 | return result | |
2736 |
|
2740 | |||
2737 | def transform_ast(self, node): |
|
2741 | def transform_ast(self, node): | |
2738 | """Apply the AST transformations from self.ast_transformers |
|
2742 | """Apply the AST transformations from self.ast_transformers | |
2739 |
|
2743 | |||
2740 | Parameters |
|
2744 | Parameters | |
2741 | ---------- |
|
2745 | ---------- | |
2742 | node : ast.Node |
|
2746 | node : ast.Node | |
2743 | The root node to be transformed. Typically called with the ast.Module |
|
2747 | The root node to be transformed. Typically called with the ast.Module | |
2744 | produced by parsing user input. |
|
2748 | produced by parsing user input. | |
2745 |
|
2749 | |||
2746 | Returns |
|
2750 | Returns | |
2747 | ------- |
|
2751 | ------- | |
2748 | An ast.Node corresponding to the node it was called with. Note that it |
|
2752 | An ast.Node corresponding to the node it was called with. Note that it | |
2749 | may also modify the passed object, so don't rely on references to the |
|
2753 | may also modify the passed object, so don't rely on references to the | |
2750 | original AST. |
|
2754 | original AST. | |
2751 | """ |
|
2755 | """ | |
2752 | for transformer in self.ast_transformers: |
|
2756 | for transformer in self.ast_transformers: | |
2753 | try: |
|
2757 | try: | |
2754 | node = transformer.visit(node) |
|
2758 | node = transformer.visit(node) | |
2755 | except InputRejected: |
|
2759 | except InputRejected: | |
2756 | # User-supplied AST transformers can reject an input by raising |
|
2760 | # User-supplied AST transformers can reject an input by raising | |
2757 | # an InputRejected. Short-circuit in this case so that we |
|
2761 | # an InputRejected. Short-circuit in this case so that we | |
2758 | # don't unregister the transform. |
|
2762 | # don't unregister the transform. | |
2759 | raise |
|
2763 | raise | |
2760 | except Exception: |
|
2764 | except Exception: | |
2761 | warn("AST transformer %r threw an error. It will be unregistered." % transformer) |
|
2765 | warn("AST transformer %r threw an error. It will be unregistered." % transformer) | |
2762 | self.ast_transformers.remove(transformer) |
|
2766 | self.ast_transformers.remove(transformer) | |
2763 |
|
2767 | |||
2764 | if self.ast_transformers: |
|
2768 | if self.ast_transformers: | |
2765 | ast.fix_missing_locations(node) |
|
2769 | ast.fix_missing_locations(node) | |
2766 | return node |
|
2770 | return node | |
2767 |
|
2771 | |||
2768 |
|
2772 | |||
2769 | def run_ast_nodes(self, nodelist, cell_name, interactivity='last_expr', |
|
2773 | def run_ast_nodes(self, nodelist, cell_name, interactivity='last_expr', | |
2770 | compiler=compile, result=None): |
|
2774 | compiler=compile, result=None): | |
2771 | """Run a sequence of AST nodes. The execution mode depends on the |
|
2775 | """Run a sequence of AST nodes. The execution mode depends on the | |
2772 | interactivity parameter. |
|
2776 | interactivity parameter. | |
2773 |
|
2777 | |||
2774 | Parameters |
|
2778 | Parameters | |
2775 | ---------- |
|
2779 | ---------- | |
2776 | nodelist : list |
|
2780 | nodelist : list | |
2777 | A sequence of AST nodes to run. |
|
2781 | A sequence of AST nodes to run. | |
2778 | cell_name : str |
|
2782 | cell_name : str | |
2779 | Will be passed to the compiler as the filename of the cell. Typically |
|
2783 | Will be passed to the compiler as the filename of the cell. Typically | |
2780 | the value returned by ip.compile.cache(cell). |
|
2784 | the value returned by ip.compile.cache(cell). | |
2781 | interactivity : str |
|
2785 | interactivity : str | |
2782 | 'all', 'last', 'last_expr' or 'none', specifying which nodes should be |
|
2786 | 'all', 'last', 'last_expr' or 'none', specifying which nodes should be | |
2783 | run interactively (displaying output from expressions). 'last_expr' |
|
2787 | run interactively (displaying output from expressions). 'last_expr' | |
2784 | will run the last node interactively only if it is an expression (i.e. |
|
2788 | will run the last node interactively only if it is an expression (i.e. | |
2785 | expressions in loops or other blocks are not displayed. Other values |
|
2789 | expressions in loops or other blocks are not displayed. Other values | |
2786 | for this parameter will raise a ValueError. |
|
2790 | for this parameter will raise a ValueError. | |
2787 | compiler : callable |
|
2791 | compiler : callable | |
2788 | A function with the same interface as the built-in compile(), to turn |
|
2792 | A function with the same interface as the built-in compile(), to turn | |
2789 | the AST nodes into code objects. Default is the built-in compile(). |
|
2793 | the AST nodes into code objects. Default is the built-in compile(). | |
2790 | result : ExecutionResult, optional |
|
2794 | result : ExecutionResult, optional | |
2791 | An object to store exceptions that occur during execution. |
|
2795 | An object to store exceptions that occur during execution. | |
2792 |
|
2796 | |||
2793 | Returns |
|
2797 | Returns | |
2794 | ------- |
|
2798 | ------- | |
2795 | True if an exception occurred while running code, False if it finished |
|
2799 | True if an exception occurred while running code, False if it finished | |
2796 | running. |
|
2800 | running. | |
2797 | """ |
|
2801 | """ | |
2798 | if not nodelist: |
|
2802 | if not nodelist: | |
2799 | return |
|
2803 | return | |
2800 |
|
2804 | |||
2801 | if interactivity == 'last_expr': |
|
2805 | if interactivity == 'last_expr': | |
2802 | if isinstance(nodelist[-1], ast.Expr): |
|
2806 | if isinstance(nodelist[-1], ast.Expr): | |
2803 | interactivity = "last" |
|
2807 | interactivity = "last" | |
2804 | else: |
|
2808 | else: | |
2805 | interactivity = "none" |
|
2809 | interactivity = "none" | |
2806 |
|
2810 | |||
2807 | if interactivity == 'none': |
|
2811 | if interactivity == 'none': | |
2808 | to_run_exec, to_run_interactive = nodelist, [] |
|
2812 | to_run_exec, to_run_interactive = nodelist, [] | |
2809 | elif interactivity == 'last': |
|
2813 | elif interactivity == 'last': | |
2810 | to_run_exec, to_run_interactive = nodelist[:-1], nodelist[-1:] |
|
2814 | to_run_exec, to_run_interactive = nodelist[:-1], nodelist[-1:] | |
2811 | elif interactivity == 'all': |
|
2815 | elif interactivity == 'all': | |
2812 | to_run_exec, to_run_interactive = [], nodelist |
|
2816 | to_run_exec, to_run_interactive = [], nodelist | |
2813 | else: |
|
2817 | else: | |
2814 | raise ValueError("Interactivity was %r" % interactivity) |
|
2818 | raise ValueError("Interactivity was %r" % interactivity) | |
2815 |
|
2819 | |||
2816 | try: |
|
2820 | try: | |
2817 | for i, node in enumerate(to_run_exec): |
|
2821 | for i, node in enumerate(to_run_exec): | |
2818 | mod = ast.Module([node]) |
|
2822 | mod = ast.Module([node]) | |
2819 | code = compiler(mod, cell_name, "exec") |
|
2823 | code = compiler(mod, cell_name, "exec") | |
2820 | if self.run_code(code, result): |
|
2824 | if self.run_code(code, result): | |
2821 | return True |
|
2825 | return True | |
2822 |
|
2826 | |||
2823 | for i, node in enumerate(to_run_interactive): |
|
2827 | for i, node in enumerate(to_run_interactive): | |
2824 | mod = ast.Interactive([node]) |
|
2828 | mod = ast.Interactive([node]) | |
2825 | code = compiler(mod, cell_name, "single") |
|
2829 | code = compiler(mod, cell_name, "single") | |
2826 | if self.run_code(code, result): |
|
2830 | if self.run_code(code, result): | |
2827 | return True |
|
2831 | return True | |
2828 |
|
2832 | |||
2829 | # Flush softspace |
|
2833 | # Flush softspace | |
2830 | if softspace(sys.stdout, 0): |
|
2834 | if softspace(sys.stdout, 0): | |
2831 | print() |
|
2835 | print() | |
2832 |
|
2836 | |||
2833 | except: |
|
2837 | except: | |
2834 | # It's possible to have exceptions raised here, typically by |
|
2838 | # It's possible to have exceptions raised here, typically by | |
2835 | # compilation of odd code (such as a naked 'return' outside a |
|
2839 | # compilation of odd code (such as a naked 'return' outside a | |
2836 | # function) that did parse but isn't valid. Typically the exception |
|
2840 | # function) that did parse but isn't valid. Typically the exception | |
2837 | # is a SyntaxError, but it's safest just to catch anything and show |
|
2841 | # is a SyntaxError, but it's safest just to catch anything and show | |
2838 | # the user a traceback. |
|
2842 | # the user a traceback. | |
2839 |
|
2843 | |||
2840 | # We do only one try/except outside the loop to minimize the impact |
|
2844 | # We do only one try/except outside the loop to minimize the impact | |
2841 | # on runtime, and also because if any node in the node list is |
|
2845 | # on runtime, and also because if any node in the node list is | |
2842 | # broken, we should stop execution completely. |
|
2846 | # broken, we should stop execution completely. | |
2843 | if result: |
|
2847 | if result: | |
2844 | result.error_before_exec = sys.exc_info()[1] |
|
2848 | result.error_before_exec = sys.exc_info()[1] | |
2845 | self.showtraceback() |
|
2849 | self.showtraceback() | |
2846 | return True |
|
2850 | return True | |
2847 |
|
2851 | |||
2848 | return False |
|
2852 | return False | |
2849 |
|
2853 | |||
2850 | def run_code(self, code_obj, result=None): |
|
2854 | def run_code(self, code_obj, result=None): | |
2851 | """Execute a code object. |
|
2855 | """Execute a code object. | |
2852 |
|
2856 | |||
2853 | When an exception occurs, self.showtraceback() is called to display a |
|
2857 | When an exception occurs, self.showtraceback() is called to display a | |
2854 | traceback. |
|
2858 | traceback. | |
2855 |
|
2859 | |||
2856 | Parameters |
|
2860 | Parameters | |
2857 | ---------- |
|
2861 | ---------- | |
2858 | code_obj : code object |
|
2862 | code_obj : code object | |
2859 | A compiled code object, to be executed |
|
2863 | A compiled code object, to be executed | |
2860 | result : ExecutionResult, optional |
|
2864 | result : ExecutionResult, optional | |
2861 | An object to store exceptions that occur during execution. |
|
2865 | An object to store exceptions that occur during execution. | |
2862 |
|
2866 | |||
2863 | Returns |
|
2867 | Returns | |
2864 | ------- |
|
2868 | ------- | |
2865 | False : successful execution. |
|
2869 | False : successful execution. | |
2866 | True : an error occurred. |
|
2870 | True : an error occurred. | |
2867 | """ |
|
2871 | """ | |
2868 | # Set our own excepthook in case the user code tries to call it |
|
2872 | # Set our own excepthook in case the user code tries to call it | |
2869 | # directly, so that the IPython crash handler doesn't get triggered |
|
2873 | # directly, so that the IPython crash handler doesn't get triggered | |
2870 | old_excepthook, sys.excepthook = sys.excepthook, self.excepthook |
|
2874 | old_excepthook, sys.excepthook = sys.excepthook, self.excepthook | |
2871 |
|
2875 | |||
2872 | # we save the original sys.excepthook in the instance, in case config |
|
2876 | # we save the original sys.excepthook in the instance, in case config | |
2873 | # code (such as magics) needs access to it. |
|
2877 | # code (such as magics) needs access to it. | |
2874 | self.sys_excepthook = old_excepthook |
|
2878 | self.sys_excepthook = old_excepthook | |
2875 | outflag = 1 # happens in more places, so it's easier as default |
|
2879 | outflag = 1 # happens in more places, so it's easier as default | |
2876 | try: |
|
2880 | try: | |
2877 | try: |
|
2881 | try: | |
2878 | self.hooks.pre_run_code_hook() |
|
2882 | self.hooks.pre_run_code_hook() | |
2879 | #rprint('Running code', repr(code_obj)) # dbg |
|
2883 | #rprint('Running code', repr(code_obj)) # dbg | |
2880 | exec(code_obj, self.user_global_ns, self.user_ns) |
|
2884 | exec(code_obj, self.user_global_ns, self.user_ns) | |
2881 | finally: |
|
2885 | finally: | |
2882 | # Reset our crash handler in place |
|
2886 | # Reset our crash handler in place | |
2883 | sys.excepthook = old_excepthook |
|
2887 | sys.excepthook = old_excepthook | |
2884 | except SystemExit as e: |
|
2888 | except SystemExit as e: | |
2885 | if result is not None: |
|
2889 | if result is not None: | |
2886 | result.error_in_exec = e |
|
2890 | result.error_in_exec = e | |
2887 | self.showtraceback(exception_only=True) |
|
2891 | self.showtraceback(exception_only=True) | |
2888 | warn("To exit: use 'exit', 'quit', or Ctrl-D.", level=1) |
|
2892 | warn("To exit: use 'exit', 'quit', or Ctrl-D.", level=1) | |
2889 | except self.custom_exceptions: |
|
2893 | except self.custom_exceptions: | |
2890 | etype, value, tb = sys.exc_info() |
|
2894 | etype, value, tb = sys.exc_info() | |
2891 | if result is not None: |
|
2895 | if result is not None: | |
2892 | result.error_in_exec = value |
|
2896 | result.error_in_exec = value | |
2893 | self.CustomTB(etype, value, tb) |
|
2897 | self.CustomTB(etype, value, tb) | |
2894 | except: |
|
2898 | except: | |
2895 | if result is not None: |
|
2899 | if result is not None: | |
2896 | result.error_in_exec = sys.exc_info()[1] |
|
2900 | result.error_in_exec = sys.exc_info()[1] | |
2897 | self.showtraceback() |
|
2901 | self.showtraceback() | |
2898 | else: |
|
2902 | else: | |
2899 | outflag = 0 |
|
2903 | outflag = 0 | |
2900 | return outflag |
|
2904 | return outflag | |
2901 |
|
2905 | |||
2902 | # For backwards compatibility |
|
2906 | # For backwards compatibility | |
2903 | runcode = run_code |
|
2907 | runcode = run_code | |
2904 |
|
2908 | |||
2905 | #------------------------------------------------------------------------- |
|
2909 | #------------------------------------------------------------------------- | |
2906 | # Things related to GUI support and pylab |
|
2910 | # Things related to GUI support and pylab | |
2907 | #------------------------------------------------------------------------- |
|
2911 | #------------------------------------------------------------------------- | |
2908 |
|
2912 | |||
2909 | def enable_gui(self, gui=None): |
|
2913 | def enable_gui(self, gui=None): | |
2910 | raise NotImplementedError('Implement enable_gui in a subclass') |
|
2914 | raise NotImplementedError('Implement enable_gui in a subclass') | |
2911 |
|
2915 | |||
2912 | def enable_matplotlib(self, gui=None): |
|
2916 | def enable_matplotlib(self, gui=None): | |
2913 | """Enable interactive matplotlib and inline figure support. |
|
2917 | """Enable interactive matplotlib and inline figure support. | |
2914 |
|
2918 | |||
2915 | This takes the following steps: |
|
2919 | This takes the following steps: | |
2916 |
|
2920 | |||
2917 | 1. select the appropriate eventloop and matplotlib backend |
|
2921 | 1. select the appropriate eventloop and matplotlib backend | |
2918 | 2. set up matplotlib for interactive use with that backend |
|
2922 | 2. set up matplotlib for interactive use with that backend | |
2919 | 3. configure formatters for inline figure display |
|
2923 | 3. configure formatters for inline figure display | |
2920 | 4. enable the selected gui eventloop |
|
2924 | 4. enable the selected gui eventloop | |
2921 |
|
2925 | |||
2922 | Parameters |
|
2926 | Parameters | |
2923 | ---------- |
|
2927 | ---------- | |
2924 | gui : optional, string |
|
2928 | gui : optional, string | |
2925 | If given, dictates the choice of matplotlib GUI backend to use |
|
2929 | If given, dictates the choice of matplotlib GUI backend to use | |
2926 | (should be one of IPython's supported backends, 'qt', 'osx', 'tk', |
|
2930 | (should be one of IPython's supported backends, 'qt', 'osx', 'tk', | |
2927 | 'gtk', 'wx' or 'inline'), otherwise we use the default chosen by |
|
2931 | 'gtk', 'wx' or 'inline'), otherwise we use the default chosen by | |
2928 | matplotlib (as dictated by the matplotlib build-time options plus the |
|
2932 | matplotlib (as dictated by the matplotlib build-time options plus the | |
2929 | user's matplotlibrc configuration file). Note that not all backends |
|
2933 | user's matplotlibrc configuration file). Note that not all backends | |
2930 | make sense in all contexts, for example a terminal ipython can't |
|
2934 | make sense in all contexts, for example a terminal ipython can't | |
2931 | display figures inline. |
|
2935 | display figures inline. | |
2932 | """ |
|
2936 | """ | |
2933 | from IPython.core import pylabtools as pt |
|
2937 | from IPython.core import pylabtools as pt | |
2934 | gui, backend = pt.find_gui_and_backend(gui, self.pylab_gui_select) |
|
2938 | gui, backend = pt.find_gui_and_backend(gui, self.pylab_gui_select) | |
2935 |
|
2939 | |||
2936 | if gui != 'inline': |
|
2940 | if gui != 'inline': | |
2937 | # If we have our first gui selection, store it |
|
2941 | # If we have our first gui selection, store it | |
2938 | if self.pylab_gui_select is None: |
|
2942 | if self.pylab_gui_select is None: | |
2939 | self.pylab_gui_select = gui |
|
2943 | self.pylab_gui_select = gui | |
2940 | # Otherwise if they are different |
|
2944 | # Otherwise if they are different | |
2941 | elif gui != self.pylab_gui_select: |
|
2945 | elif gui != self.pylab_gui_select: | |
2942 | print ('Warning: Cannot change to a different GUI toolkit: %s.' |
|
2946 | print ('Warning: Cannot change to a different GUI toolkit: %s.' | |
2943 | ' Using %s instead.' % (gui, self.pylab_gui_select)) |
|
2947 | ' Using %s instead.' % (gui, self.pylab_gui_select)) | |
2944 | gui, backend = pt.find_gui_and_backend(self.pylab_gui_select) |
|
2948 | gui, backend = pt.find_gui_and_backend(self.pylab_gui_select) | |
2945 |
|
2949 | |||
2946 | pt.activate_matplotlib(backend) |
|
2950 | pt.activate_matplotlib(backend) | |
2947 | pt.configure_inline_support(self, backend) |
|
2951 | pt.configure_inline_support(self, backend) | |
2948 |
|
2952 | |||
2949 | # Now we must activate the gui pylab wants to use, and fix %run to take |
|
2953 | # Now we must activate the gui pylab wants to use, and fix %run to take | |
2950 | # plot updates into account |
|
2954 | # plot updates into account | |
2951 | self.enable_gui(gui) |
|
2955 | self.enable_gui(gui) | |
2952 | self.magics_manager.registry['ExecutionMagics'].default_runner = \ |
|
2956 | self.magics_manager.registry['ExecutionMagics'].default_runner = \ | |
2953 | pt.mpl_runner(self.safe_execfile) |
|
2957 | pt.mpl_runner(self.safe_execfile) | |
2954 |
|
2958 | |||
2955 | return gui, backend |
|
2959 | return gui, backend | |
2956 |
|
2960 | |||
2957 | def enable_pylab(self, gui=None, import_all=True, welcome_message=False): |
|
2961 | def enable_pylab(self, gui=None, import_all=True, welcome_message=False): | |
2958 | """Activate pylab support at runtime. |
|
2962 | """Activate pylab support at runtime. | |
2959 |
|
2963 | |||
2960 | This turns on support for matplotlib, preloads into the interactive |
|
2964 | This turns on support for matplotlib, preloads into the interactive | |
2961 | namespace all of numpy and pylab, and configures IPython to correctly |
|
2965 | namespace all of numpy and pylab, and configures IPython to correctly | |
2962 | interact with the GUI event loop. The GUI backend to be used can be |
|
2966 | interact with the GUI event loop. The GUI backend to be used can be | |
2963 | optionally selected with the optional ``gui`` argument. |
|
2967 | optionally selected with the optional ``gui`` argument. | |
2964 |
|
2968 | |||
2965 | This method only adds preloading the namespace to InteractiveShell.enable_matplotlib. |
|
2969 | This method only adds preloading the namespace to InteractiveShell.enable_matplotlib. | |
2966 |
|
2970 | |||
2967 | Parameters |
|
2971 | Parameters | |
2968 | ---------- |
|
2972 | ---------- | |
2969 | gui : optional, string |
|
2973 | gui : optional, string | |
2970 | If given, dictates the choice of matplotlib GUI backend to use |
|
2974 | If given, dictates the choice of matplotlib GUI backend to use | |
2971 | (should be one of IPython's supported backends, 'qt', 'osx', 'tk', |
|
2975 | (should be one of IPython's supported backends, 'qt', 'osx', 'tk', | |
2972 | 'gtk', 'wx' or 'inline'), otherwise we use the default chosen by |
|
2976 | 'gtk', 'wx' or 'inline'), otherwise we use the default chosen by | |
2973 | matplotlib (as dictated by the matplotlib build-time options plus the |
|
2977 | matplotlib (as dictated by the matplotlib build-time options plus the | |
2974 | user's matplotlibrc configuration file). Note that not all backends |
|
2978 | user's matplotlibrc configuration file). Note that not all backends | |
2975 | make sense in all contexts, for example a terminal ipython can't |
|
2979 | make sense in all contexts, for example a terminal ipython can't | |
2976 | display figures inline. |
|
2980 | display figures inline. | |
2977 | import_all : optional, bool, default: True |
|
2981 | import_all : optional, bool, default: True | |
2978 | Whether to do `from numpy import *` and `from pylab import *` |
|
2982 | Whether to do `from numpy import *` and `from pylab import *` | |
2979 | in addition to module imports. |
|
2983 | in addition to module imports. | |
2980 | welcome_message : deprecated |
|
2984 | welcome_message : deprecated | |
2981 | This argument is ignored, no welcome message will be displayed. |
|
2985 | This argument is ignored, no welcome message will be displayed. | |
2982 | """ |
|
2986 | """ | |
2983 | from IPython.core.pylabtools import import_pylab |
|
2987 | from IPython.core.pylabtools import import_pylab | |
2984 |
|
2988 | |||
2985 | gui, backend = self.enable_matplotlib(gui) |
|
2989 | gui, backend = self.enable_matplotlib(gui) | |
2986 |
|
2990 | |||
2987 | # We want to prevent the loading of pylab to pollute the user's |
|
2991 | # We want to prevent the loading of pylab to pollute the user's | |
2988 | # namespace as shown by the %who* magics, so we execute the activation |
|
2992 | # namespace as shown by the %who* magics, so we execute the activation | |
2989 | # code in an empty namespace, and we update *both* user_ns and |
|
2993 | # code in an empty namespace, and we update *both* user_ns and | |
2990 | # user_ns_hidden with this information. |
|
2994 | # user_ns_hidden with this information. | |
2991 | ns = {} |
|
2995 | ns = {} | |
2992 | import_pylab(ns, import_all) |
|
2996 | import_pylab(ns, import_all) | |
2993 | # warn about clobbered names |
|
2997 | # warn about clobbered names | |
2994 | ignored = {"__builtins__"} |
|
2998 | ignored = {"__builtins__"} | |
2995 | both = set(ns).intersection(self.user_ns).difference(ignored) |
|
2999 | both = set(ns).intersection(self.user_ns).difference(ignored) | |
2996 | clobbered = [ name for name in both if self.user_ns[name] is not ns[name] ] |
|
3000 | clobbered = [ name for name in both if self.user_ns[name] is not ns[name] ] | |
2997 | self.user_ns.update(ns) |
|
3001 | self.user_ns.update(ns) | |
2998 | self.user_ns_hidden.update(ns) |
|
3002 | self.user_ns_hidden.update(ns) | |
2999 | return gui, backend, clobbered |
|
3003 | return gui, backend, clobbered | |
3000 |
|
3004 | |||
3001 | #------------------------------------------------------------------------- |
|
3005 | #------------------------------------------------------------------------- | |
3002 | # Utilities |
|
3006 | # Utilities | |
3003 | #------------------------------------------------------------------------- |
|
3007 | #------------------------------------------------------------------------- | |
3004 |
|
3008 | |||
3005 | def var_expand(self, cmd, depth=0, formatter=DollarFormatter()): |
|
3009 | def var_expand(self, cmd, depth=0, formatter=DollarFormatter()): | |
3006 | """Expand python variables in a string. |
|
3010 | """Expand python variables in a string. | |
3007 |
|
3011 | |||
3008 | The depth argument indicates how many frames above the caller should |
|
3012 | The depth argument indicates how many frames above the caller should | |
3009 | be walked to look for the local namespace where to expand variables. |
|
3013 | be walked to look for the local namespace where to expand variables. | |
3010 |
|
3014 | |||
3011 | The global namespace for expansion is always the user's interactive |
|
3015 | The global namespace for expansion is always the user's interactive | |
3012 | namespace. |
|
3016 | namespace. | |
3013 | """ |
|
3017 | """ | |
3014 | ns = self.user_ns.copy() |
|
3018 | ns = self.user_ns.copy() | |
3015 | try: |
|
3019 | try: | |
3016 | frame = sys._getframe(depth+1) |
|
3020 | frame = sys._getframe(depth+1) | |
3017 | except ValueError: |
|
3021 | except ValueError: | |
3018 | # This is thrown if there aren't that many frames on the stack, |
|
3022 | # This is thrown if there aren't that many frames on the stack, | |
3019 | # e.g. if a script called run_line_magic() directly. |
|
3023 | # e.g. if a script called run_line_magic() directly. | |
3020 | pass |
|
3024 | pass | |
3021 | else: |
|
3025 | else: | |
3022 | ns.update(frame.f_locals) |
|
3026 | ns.update(frame.f_locals) | |
3023 |
|
3027 | |||
3024 | try: |
|
3028 | try: | |
3025 | # We have to use .vformat() here, because 'self' is a valid and common |
|
3029 | # We have to use .vformat() here, because 'self' is a valid and common | |
3026 | # name, and expanding **ns for .format() would make it collide with |
|
3030 | # name, and expanding **ns for .format() would make it collide with | |
3027 | # the 'self' argument of the method. |
|
3031 | # the 'self' argument of the method. | |
3028 | cmd = formatter.vformat(cmd, args=[], kwargs=ns) |
|
3032 | cmd = formatter.vformat(cmd, args=[], kwargs=ns) | |
3029 | except Exception: |
|
3033 | except Exception: | |
3030 | # if formatter couldn't format, just let it go untransformed |
|
3034 | # if formatter couldn't format, just let it go untransformed | |
3031 | pass |
|
3035 | pass | |
3032 | return cmd |
|
3036 | return cmd | |
3033 |
|
3037 | |||
3034 | def mktempfile(self, data=None, prefix='ipython_edit_'): |
|
3038 | def mktempfile(self, data=None, prefix='ipython_edit_'): | |
3035 | """Make a new tempfile and return its filename. |
|
3039 | """Make a new tempfile and return its filename. | |
3036 |
|
3040 | |||
3037 | This makes a call to tempfile.mkstemp (created in a tempfile.mkdtemp), |
|
3041 | This makes a call to tempfile.mkstemp (created in a tempfile.mkdtemp), | |
3038 | but it registers the created filename internally so ipython cleans it up |
|
3042 | but it registers the created filename internally so ipython cleans it up | |
3039 | at exit time. |
|
3043 | at exit time. | |
3040 |
|
3044 | |||
3041 | Optional inputs: |
|
3045 | Optional inputs: | |
3042 |
|
3046 | |||
3043 | - data(None): if data is given, it gets written out to the temp file |
|
3047 | - data(None): if data is given, it gets written out to the temp file | |
3044 | immediately, and the file is closed again.""" |
|
3048 | immediately, and the file is closed again.""" | |
3045 |
|
3049 | |||
3046 | dirname = tempfile.mkdtemp(prefix=prefix) |
|
3050 | dirname = tempfile.mkdtemp(prefix=prefix) | |
3047 | self.tempdirs.append(dirname) |
|
3051 | self.tempdirs.append(dirname) | |
3048 |
|
3052 | |||
3049 | handle, filename = tempfile.mkstemp('.py', prefix, dir=dirname) |
|
3053 | handle, filename = tempfile.mkstemp('.py', prefix, dir=dirname) | |
3050 | os.close(handle) # On Windows, there can only be one open handle on a file |
|
3054 | os.close(handle) # On Windows, there can only be one open handle on a file | |
3051 | self.tempfiles.append(filename) |
|
3055 | self.tempfiles.append(filename) | |
3052 |
|
3056 | |||
3053 | if data: |
|
3057 | if data: | |
3054 | tmp_file = open(filename,'w') |
|
3058 | tmp_file = open(filename,'w') | |
3055 | tmp_file.write(data) |
|
3059 | tmp_file.write(data) | |
3056 | tmp_file.close() |
|
3060 | tmp_file.close() | |
3057 | return filename |
|
3061 | return filename | |
3058 |
|
3062 | |||
3059 | @undoc |
|
3063 | @undoc | |
3060 | def write(self,data): |
|
3064 | def write(self,data): | |
3061 | """DEPRECATED: Write a string to the default output""" |
|
3065 | """DEPRECATED: Write a string to the default output""" | |
3062 | warn('InteractiveShell.write() is deprecated, use sys.stdout instead', |
|
3066 | warn('InteractiveShell.write() is deprecated, use sys.stdout instead', | |
3063 | DeprecationWarning, stacklevel=2) |
|
3067 | DeprecationWarning, stacklevel=2) | |
3064 | sys.stdout.write(data) |
|
3068 | sys.stdout.write(data) | |
3065 |
|
3069 | |||
3066 | @undoc |
|
3070 | @undoc | |
3067 | def write_err(self,data): |
|
3071 | def write_err(self,data): | |
3068 | """DEPRECATED: Write a string to the default error output""" |
|
3072 | """DEPRECATED: Write a string to the default error output""" | |
3069 | warn('InteractiveShell.write_err() is deprecated, use sys.stderr instead', |
|
3073 | warn('InteractiveShell.write_err() is deprecated, use sys.stderr instead', | |
3070 | DeprecationWarning, stacklevel=2) |
|
3074 | DeprecationWarning, stacklevel=2) | |
3071 | sys.stderr.write(data) |
|
3075 | sys.stderr.write(data) | |
3072 |
|
3076 | |||
3073 | def ask_yes_no(self, prompt, default=None, interrupt=None): |
|
3077 | def ask_yes_no(self, prompt, default=None, interrupt=None): | |
3074 | if self.quiet: |
|
3078 | if self.quiet: | |
3075 | return True |
|
3079 | return True | |
3076 | return ask_yes_no(prompt,default,interrupt) |
|
3080 | return ask_yes_no(prompt,default,interrupt) | |
3077 |
|
3081 | |||
3078 | def show_usage(self): |
|
3082 | def show_usage(self): | |
3079 | """Show a usage message""" |
|
3083 | """Show a usage message""" | |
3080 | page.page(IPython.core.usage.interactive_usage) |
|
3084 | page.page(IPython.core.usage.interactive_usage) | |
3081 |
|
3085 | |||
3082 | def extract_input_lines(self, range_str, raw=False): |
|
3086 | def extract_input_lines(self, range_str, raw=False): | |
3083 | """Return as a string a set of input history slices. |
|
3087 | """Return as a string a set of input history slices. | |
3084 |
|
3088 | |||
3085 | Parameters |
|
3089 | Parameters | |
3086 | ---------- |
|
3090 | ---------- | |
3087 | range_str : string |
|
3091 | range_str : string | |
3088 | The set of slices is given as a string, like "~5/6-~4/2 4:8 9", |
|
3092 | The set of slices is given as a string, like "~5/6-~4/2 4:8 9", | |
3089 | since this function is for use by magic functions which get their |
|
3093 | since this function is for use by magic functions which get their | |
3090 | arguments as strings. The number before the / is the session |
|
3094 | arguments as strings. The number before the / is the session | |
3091 | number: ~n goes n back from the current session. |
|
3095 | number: ~n goes n back from the current session. | |
3092 |
|
3096 | |||
3093 | raw : bool, optional |
|
3097 | raw : bool, optional | |
3094 | By default, the processed input is used. If this is true, the raw |
|
3098 | By default, the processed input is used. If this is true, the raw | |
3095 | input history is used instead. |
|
3099 | input history is used instead. | |
3096 |
|
3100 | |||
3097 | Notes |
|
3101 | Notes | |
3098 | ----- |
|
3102 | ----- | |
3099 |
|
3103 | |||
3100 | Slices can be described with two notations: |
|
3104 | Slices can be described with two notations: | |
3101 |
|
3105 | |||
3102 | * ``N:M`` -> standard python form, means including items N...(M-1). |
|
3106 | * ``N:M`` -> standard python form, means including items N...(M-1). | |
3103 | * ``N-M`` -> include items N..M (closed endpoint). |
|
3107 | * ``N-M`` -> include items N..M (closed endpoint). | |
3104 | """ |
|
3108 | """ | |
3105 | lines = self.history_manager.get_range_by_str(range_str, raw=raw) |
|
3109 | lines = self.history_manager.get_range_by_str(range_str, raw=raw) | |
3106 | return "\n".join(x for _, _, x in lines) |
|
3110 | return "\n".join(x for _, _, x in lines) | |
3107 |
|
3111 | |||
3108 | def find_user_code(self, target, raw=True, py_only=False, skip_encoding_cookie=True, search_ns=False): |
|
3112 | def find_user_code(self, target, raw=True, py_only=False, skip_encoding_cookie=True, search_ns=False): | |
3109 | """Get a code string from history, file, url, or a string or macro. |
|
3113 | """Get a code string from history, file, url, or a string or macro. | |
3110 |
|
3114 | |||
3111 | This is mainly used by magic functions. |
|
3115 | This is mainly used by magic functions. | |
3112 |
|
3116 | |||
3113 | Parameters |
|
3117 | Parameters | |
3114 | ---------- |
|
3118 | ---------- | |
3115 |
|
3119 | |||
3116 | target : str |
|
3120 | target : str | |
3117 |
|
3121 | |||
3118 | A string specifying code to retrieve. This will be tried respectively |
|
3122 | A string specifying code to retrieve. This will be tried respectively | |
3119 | as: ranges of input history (see %history for syntax), url, |
|
3123 | as: ranges of input history (see %history for syntax), url, | |
3120 | corresponding .py file, filename, or an expression evaluating to a |
|
3124 | corresponding .py file, filename, or an expression evaluating to a | |
3121 | string or Macro in the user namespace. |
|
3125 | string or Macro in the user namespace. | |
3122 |
|
3126 | |||
3123 | raw : bool |
|
3127 | raw : bool | |
3124 | If true (default), retrieve raw history. Has no effect on the other |
|
3128 | If true (default), retrieve raw history. Has no effect on the other | |
3125 | retrieval mechanisms. |
|
3129 | retrieval mechanisms. | |
3126 |
|
3130 | |||
3127 | py_only : bool (default False) |
|
3131 | py_only : bool (default False) | |
3128 | Only try to fetch python code, do not try alternative methods to decode file |
|
3132 | Only try to fetch python code, do not try alternative methods to decode file | |
3129 | if unicode fails. |
|
3133 | if unicode fails. | |
3130 |
|
3134 | |||
3131 | Returns |
|
3135 | Returns | |
3132 | ------- |
|
3136 | ------- | |
3133 | A string of code. |
|
3137 | A string of code. | |
3134 |
|
3138 | |||
3135 | ValueError is raised if nothing is found, and TypeError if it evaluates |
|
3139 | ValueError is raised if nothing is found, and TypeError if it evaluates | |
3136 | to an object of another type. In each case, .args[0] is a printable |
|
3140 | to an object of another type. In each case, .args[0] is a printable | |
3137 | message. |
|
3141 | message. | |
3138 | """ |
|
3142 | """ | |
3139 | code = self.extract_input_lines(target, raw=raw) # Grab history |
|
3143 | code = self.extract_input_lines(target, raw=raw) # Grab history | |
3140 | if code: |
|
3144 | if code: | |
3141 | return code |
|
3145 | return code | |
3142 | utarget = unquote_filename(target) |
|
3146 | utarget = unquote_filename(target) | |
3143 | try: |
|
3147 | try: | |
3144 | if utarget.startswith(('http://', 'https://')): |
|
3148 | if utarget.startswith(('http://', 'https://')): | |
3145 | return openpy.read_py_url(utarget, skip_encoding_cookie=skip_encoding_cookie) |
|
3149 | return openpy.read_py_url(utarget, skip_encoding_cookie=skip_encoding_cookie) | |
3146 | except UnicodeDecodeError: |
|
3150 | except UnicodeDecodeError: | |
3147 | if not py_only : |
|
3151 | if not py_only : | |
3148 | # Deferred import |
|
3152 | # Deferred import | |
3149 | try: |
|
3153 | try: | |
3150 | from urllib.request import urlopen # Py3 |
|
3154 | from urllib.request import urlopen # Py3 | |
3151 | except ImportError: |
|
3155 | except ImportError: | |
3152 | from urllib import urlopen |
|
3156 | from urllib import urlopen | |
3153 | response = urlopen(target) |
|
3157 | response = urlopen(target) | |
3154 | return response.read().decode('latin1') |
|
3158 | return response.read().decode('latin1') | |
3155 | raise ValueError(("'%s' seem to be unreadable.") % utarget) |
|
3159 | raise ValueError(("'%s' seem to be unreadable.") % utarget) | |
3156 |
|
3160 | |||
3157 | potential_target = [target] |
|
3161 | potential_target = [target] | |
3158 | try : |
|
3162 | try : | |
3159 | potential_target.insert(0,get_py_filename(target)) |
|
3163 | potential_target.insert(0,get_py_filename(target)) | |
3160 | except IOError: |
|
3164 | except IOError: | |
3161 | pass |
|
3165 | pass | |
3162 |
|
3166 | |||
3163 | for tgt in potential_target : |
|
3167 | for tgt in potential_target : | |
3164 | if os.path.isfile(tgt): # Read file |
|
3168 | if os.path.isfile(tgt): # Read file | |
3165 | try : |
|
3169 | try : | |
3166 | return openpy.read_py_file(tgt, skip_encoding_cookie=skip_encoding_cookie) |
|
3170 | return openpy.read_py_file(tgt, skip_encoding_cookie=skip_encoding_cookie) | |
3167 | except UnicodeDecodeError : |
|
3171 | except UnicodeDecodeError : | |
3168 | if not py_only : |
|
3172 | if not py_only : | |
3169 | with io_open(tgt,'r', encoding='latin1') as f : |
|
3173 | with io_open(tgt,'r', encoding='latin1') as f : | |
3170 | return f.read() |
|
3174 | return f.read() | |
3171 | raise ValueError(("'%s' seem to be unreadable.") % target) |
|
3175 | raise ValueError(("'%s' seem to be unreadable.") % target) | |
3172 | elif os.path.isdir(os.path.expanduser(tgt)): |
|
3176 | elif os.path.isdir(os.path.expanduser(tgt)): | |
3173 | raise ValueError("'%s' is a directory, not a regular file." % target) |
|
3177 | raise ValueError("'%s' is a directory, not a regular file." % target) | |
3174 |
|
3178 | |||
3175 | if search_ns: |
|
3179 | if search_ns: | |
3176 | # Inspect namespace to load object source |
|
3180 | # Inspect namespace to load object source | |
3177 | object_info = self.object_inspect(target, detail_level=1) |
|
3181 | object_info = self.object_inspect(target, detail_level=1) | |
3178 | if object_info['found'] and object_info['source']: |
|
3182 | if object_info['found'] and object_info['source']: | |
3179 | return object_info['source'] |
|
3183 | return object_info['source'] | |
3180 |
|
3184 | |||
3181 | try: # User namespace |
|
3185 | try: # User namespace | |
3182 | codeobj = eval(target, self.user_ns) |
|
3186 | codeobj = eval(target, self.user_ns) | |
3183 | except Exception: |
|
3187 | except Exception: | |
3184 | raise ValueError(("'%s' was not found in history, as a file, url, " |
|
3188 | raise ValueError(("'%s' was not found in history, as a file, url, " | |
3185 | "nor in the user namespace.") % target) |
|
3189 | "nor in the user namespace.") % target) | |
3186 |
|
3190 | |||
3187 | if isinstance(codeobj, string_types): |
|
3191 | if isinstance(codeobj, string_types): | |
3188 | return codeobj |
|
3192 | return codeobj | |
3189 | elif isinstance(codeobj, Macro): |
|
3193 | elif isinstance(codeobj, Macro): | |
3190 | return codeobj.value |
|
3194 | return codeobj.value | |
3191 |
|
3195 | |||
3192 | raise TypeError("%s is neither a string nor a macro." % target, |
|
3196 | raise TypeError("%s is neither a string nor a macro." % target, | |
3193 | codeobj) |
|
3197 | codeobj) | |
3194 |
|
3198 | |||
3195 | #------------------------------------------------------------------------- |
|
3199 | #------------------------------------------------------------------------- | |
3196 | # Things related to IPython exiting |
|
3200 | # Things related to IPython exiting | |
3197 | #------------------------------------------------------------------------- |
|
3201 | #------------------------------------------------------------------------- | |
3198 | def atexit_operations(self): |
|
3202 | def atexit_operations(self): | |
3199 | """This will be executed at the time of exit. |
|
3203 | """This will be executed at the time of exit. | |
3200 |
|
3204 | |||
3201 | Cleanup operations and saving of persistent data that is done |
|
3205 | Cleanup operations and saving of persistent data that is done | |
3202 | unconditionally by IPython should be performed here. |
|
3206 | unconditionally by IPython should be performed here. | |
3203 |
|
3207 | |||
3204 | For things that may depend on startup flags or platform specifics (such |
|
3208 | For things that may depend on startup flags or platform specifics (such | |
3205 | as having readline or not), register a separate atexit function in the |
|
3209 | as having readline or not), register a separate atexit function in the | |
3206 | code that has the appropriate information, rather than trying to |
|
3210 | code that has the appropriate information, rather than trying to | |
3207 | clutter |
|
3211 | clutter | |
3208 | """ |
|
3212 | """ | |
3209 | # Close the history session (this stores the end time and line count) |
|
3213 | # Close the history session (this stores the end time and line count) | |
3210 | # this must be *before* the tempfile cleanup, in case of temporary |
|
3214 | # this must be *before* the tempfile cleanup, in case of temporary | |
3211 | # history db |
|
3215 | # history db | |
3212 | self.history_manager.end_session() |
|
3216 | self.history_manager.end_session() | |
3213 |
|
3217 | |||
3214 | # Cleanup all tempfiles and folders left around |
|
3218 | # Cleanup all tempfiles and folders left around | |
3215 | for tfile in self.tempfiles: |
|
3219 | for tfile in self.tempfiles: | |
3216 | try: |
|
3220 | try: | |
3217 | os.unlink(tfile) |
|
3221 | os.unlink(tfile) | |
3218 | except OSError: |
|
3222 | except OSError: | |
3219 | pass |
|
3223 | pass | |
3220 |
|
3224 | |||
3221 | for tdir in self.tempdirs: |
|
3225 | for tdir in self.tempdirs: | |
3222 | try: |
|
3226 | try: | |
3223 | os.rmdir(tdir) |
|
3227 | os.rmdir(tdir) | |
3224 | except OSError: |
|
3228 | except OSError: | |
3225 | pass |
|
3229 | pass | |
3226 |
|
3230 | |||
3227 | # Clear all user namespaces to release all references cleanly. |
|
3231 | # Clear all user namespaces to release all references cleanly. | |
3228 | self.reset(new_session=False) |
|
3232 | self.reset(new_session=False) | |
3229 |
|
3233 | |||
3230 | # Run user hooks |
|
3234 | # Run user hooks | |
3231 | self.hooks.shutdown_hook() |
|
3235 | self.hooks.shutdown_hook() | |
3232 |
|
3236 | |||
3233 | def cleanup(self): |
|
3237 | def cleanup(self): | |
3234 | self.restore_sys_module_state() |
|
3238 | self.restore_sys_module_state() | |
3235 |
|
3239 | |||
3236 |
|
3240 | |||
3237 | class InteractiveShellABC(with_metaclass(abc.ABCMeta, object)): |
|
3241 | class InteractiveShellABC(with_metaclass(abc.ABCMeta, object)): | |
3238 | """An abstract base class for InteractiveShell.""" |
|
3242 | """An abstract base class for InteractiveShell.""" | |
3239 |
|
3243 | |||
3240 | InteractiveShellABC.register(InteractiveShell) |
|
3244 | InteractiveShellABC.register(InteractiveShell) |
@@ -1,1490 +1,1492 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 | """ |
|
2 | """ | |
3 | Verbose and colourful traceback formatting. |
|
3 | Verbose and colourful traceback formatting. | |
4 |
|
4 | |||
5 | **ColorTB** |
|
5 | **ColorTB** | |
6 |
|
6 | |||
7 | I've always found it a bit hard to visually parse tracebacks in Python. The |
|
7 | I've always found it a bit hard to visually parse tracebacks in Python. The | |
8 | ColorTB class is a solution to that problem. It colors the different parts of a |
|
8 | ColorTB class is a solution to that problem. It colors the different parts of a | |
9 | traceback in a manner similar to what you would expect from a syntax-highlighting |
|
9 | traceback in a manner similar to what you would expect from a syntax-highlighting | |
10 | text editor. |
|
10 | text editor. | |
11 |
|
11 | |||
12 | Installation instructions for ColorTB:: |
|
12 | Installation instructions for ColorTB:: | |
13 |
|
13 | |||
14 | import sys,ultratb |
|
14 | import sys,ultratb | |
15 | sys.excepthook = ultratb.ColorTB() |
|
15 | sys.excepthook = ultratb.ColorTB() | |
16 |
|
16 | |||
17 | **VerboseTB** |
|
17 | **VerboseTB** | |
18 |
|
18 | |||
19 | I've also included a port of Ka-Ping Yee's "cgitb.py" that produces all kinds |
|
19 | I've also included a port of Ka-Ping Yee's "cgitb.py" that produces all kinds | |
20 | of useful info when a traceback occurs. Ping originally had it spit out HTML |
|
20 | of useful info when a traceback occurs. Ping originally had it spit out HTML | |
21 | and intended it for CGI programmers, but why should they have all the fun? I |
|
21 | and intended it for CGI programmers, but why should they have all the fun? I | |
22 | altered it to spit out colored text to the terminal. It's a bit overwhelming, |
|
22 | altered it to spit out colored text to the terminal. It's a bit overwhelming, | |
23 | but kind of neat, and maybe useful for long-running programs that you believe |
|
23 | but kind of neat, and maybe useful for long-running programs that you believe | |
24 | are bug-free. If a crash *does* occur in that type of program you want details. |
|
24 | are bug-free. If a crash *does* occur in that type of program you want details. | |
25 | Give it a shot--you'll love it or you'll hate it. |
|
25 | Give it a shot--you'll love it or you'll hate it. | |
26 |
|
26 | |||
27 | .. note:: |
|
27 | .. note:: | |
28 |
|
28 | |||
29 | The Verbose mode prints the variables currently visible where the exception |
|
29 | The Verbose mode prints the variables currently visible where the exception | |
30 | happened (shortening their strings if too long). This can potentially be |
|
30 | happened (shortening their strings if too long). This can potentially be | |
31 | very slow, if you happen to have a huge data structure whose string |
|
31 | very slow, if you happen to have a huge data structure whose string | |
32 | representation is complex to compute. Your computer may appear to freeze for |
|
32 | representation is complex to compute. Your computer may appear to freeze for | |
33 | a while with cpu usage at 100%. If this occurs, you can cancel the traceback |
|
33 | a while with cpu usage at 100%. If this occurs, you can cancel the traceback | |
34 | with Ctrl-C (maybe hitting it more than once). |
|
34 | with Ctrl-C (maybe hitting it more than once). | |
35 |
|
35 | |||
36 | If you encounter this kind of situation often, you may want to use the |
|
36 | If you encounter this kind of situation often, you may want to use the | |
37 | Verbose_novars mode instead of the regular Verbose, which avoids formatting |
|
37 | Verbose_novars mode instead of the regular Verbose, which avoids formatting | |
38 | variables (but otherwise includes the information and context given by |
|
38 | variables (but otherwise includes the information and context given by | |
39 | Verbose). |
|
39 | Verbose). | |
40 |
|
40 | |||
41 | .. note:: |
|
41 | .. note:: | |
42 |
|
42 | |||
43 | The verbose mode print all variables in the stack, which means it can |
|
43 | The verbose mode print all variables in the stack, which means it can | |
44 | potentially leak sensitive information like access keys, or unencryted |
|
44 | potentially leak sensitive information like access keys, or unencryted | |
45 | password. |
|
45 | password. | |
46 |
|
46 | |||
47 | Installation instructions for VerboseTB:: |
|
47 | Installation instructions for VerboseTB:: | |
48 |
|
48 | |||
49 | import sys,ultratb |
|
49 | import sys,ultratb | |
50 | sys.excepthook = ultratb.VerboseTB() |
|
50 | sys.excepthook = ultratb.VerboseTB() | |
51 |
|
51 | |||
52 | Note: Much of the code in this module was lifted verbatim from the standard |
|
52 | Note: Much of the code in this module was lifted verbatim from the standard | |
53 | library module 'traceback.py' and Ka-Ping Yee's 'cgitb.py'. |
|
53 | library module 'traceback.py' and Ka-Ping Yee's 'cgitb.py'. | |
54 |
|
54 | |||
55 | Color schemes |
|
55 | Color schemes | |
56 | ------------- |
|
56 | ------------- | |
57 |
|
57 | |||
58 | The colors are defined in the class TBTools through the use of the |
|
58 | The colors are defined in the class TBTools through the use of the | |
59 | ColorSchemeTable class. Currently the following exist: |
|
59 | ColorSchemeTable class. Currently the following exist: | |
60 |
|
60 | |||
61 | - NoColor: allows all of this module to be used in any terminal (the color |
|
61 | - NoColor: allows all of this module to be used in any terminal (the color | |
62 | escapes are just dummy blank strings). |
|
62 | escapes are just dummy blank strings). | |
63 |
|
63 | |||
64 | - Linux: is meant to look good in a terminal like the Linux console (black |
|
64 | - Linux: is meant to look good in a terminal like the Linux console (black | |
65 | or very dark background). |
|
65 | or very dark background). | |
66 |
|
66 | |||
67 | - LightBG: similar to Linux but swaps dark/light colors to be more readable |
|
67 | - LightBG: similar to Linux but swaps dark/light colors to be more readable | |
68 | in light background terminals. |
|
68 | in light background terminals. | |
69 |
|
69 | |||
70 | You can implement other color schemes easily, the syntax is fairly |
|
70 | You can implement other color schemes easily, the syntax is fairly | |
71 | self-explanatory. Please send back new schemes you develop to the author for |
|
71 | self-explanatory. Please send back new schemes you develop to the author for | |
72 | possible inclusion in future releases. |
|
72 | possible inclusion in future releases. | |
73 |
|
73 | |||
74 | Inheritance diagram: |
|
74 | Inheritance diagram: | |
75 |
|
75 | |||
76 | .. inheritance-diagram:: IPython.core.ultratb |
|
76 | .. inheritance-diagram:: IPython.core.ultratb | |
77 | :parts: 3 |
|
77 | :parts: 3 | |
78 | """ |
|
78 | """ | |
79 |
|
79 | |||
80 | #***************************************************************************** |
|
80 | #***************************************************************************** | |
81 | # Copyright (C) 2001 Nathaniel Gray <n8gray@caltech.edu> |
|
81 | # Copyright (C) 2001 Nathaniel Gray <n8gray@caltech.edu> | |
82 | # Copyright (C) 2001-2004 Fernando Perez <fperez@colorado.edu> |
|
82 | # Copyright (C) 2001-2004 Fernando Perez <fperez@colorado.edu> | |
83 | # |
|
83 | # | |
84 | # Distributed under the terms of the BSD License. The full license is in |
|
84 | # Distributed under the terms of the BSD License. The full license is in | |
85 | # the file COPYING, distributed as part of this software. |
|
85 | # the file COPYING, distributed as part of this software. | |
86 | #***************************************************************************** |
|
86 | #***************************************************************************** | |
87 |
|
87 | |||
88 | from __future__ import absolute_import |
|
88 | from __future__ import absolute_import | |
89 | from __future__ import unicode_literals |
|
89 | from __future__ import unicode_literals | |
90 | from __future__ import print_function |
|
90 | from __future__ import print_function | |
91 |
|
91 | |||
92 | import dis |
|
92 | import dis | |
93 | import inspect |
|
93 | import inspect | |
94 | import keyword |
|
94 | import keyword | |
95 | import linecache |
|
95 | import linecache | |
96 | import os |
|
96 | import os | |
97 | import pydoc |
|
97 | import pydoc | |
98 | import re |
|
98 | import re | |
99 | import sys |
|
99 | import sys | |
100 | import time |
|
100 | import time | |
101 | import tokenize |
|
101 | import tokenize | |
102 | import traceback |
|
102 | import traceback | |
103 | import types |
|
103 | import types | |
104 |
|
104 | |||
105 | try: # Python 2 |
|
105 | try: # Python 2 | |
106 | generate_tokens = tokenize.generate_tokens |
|
106 | generate_tokens = tokenize.generate_tokens | |
107 | except AttributeError: # Python 3 |
|
107 | except AttributeError: # Python 3 | |
108 | generate_tokens = tokenize.tokenize |
|
108 | generate_tokens = tokenize.tokenize | |
109 |
|
109 | |||
110 | # For purposes of monkeypatching inspect to fix a bug in it. |
|
110 | # For purposes of monkeypatching inspect to fix a bug in it. | |
111 | from inspect import getsourcefile, getfile, getmodule, \ |
|
111 | from inspect import getsourcefile, getfile, getmodule, \ | |
112 | ismodule, isclass, ismethod, isfunction, istraceback, isframe, iscode |
|
112 | ismodule, isclass, ismethod, isfunction, istraceback, isframe, iscode | |
113 |
|
113 | |||
114 | # IPython's own modules |
|
114 | # IPython's own modules | |
115 | # Modified pdb which doesn't damage IPython's readline handling |
|
115 | # Modified pdb which doesn't damage IPython's readline handling | |
116 | from IPython import get_ipython |
|
116 | from IPython import get_ipython | |
117 | from IPython.core import debugger |
|
117 | from IPython.core import debugger | |
118 | from IPython.core.display_trap import DisplayTrap |
|
118 | from IPython.core.display_trap import DisplayTrap | |
119 | from IPython.core.excolors import exception_colors |
|
119 | from IPython.core.excolors import exception_colors | |
120 | from IPython.utils import PyColorize |
|
120 | from IPython.utils import PyColorize | |
121 | from IPython.utils import openpy |
|
121 | from IPython.utils import openpy | |
122 | from IPython.utils import path as util_path |
|
122 | from IPython.utils import path as util_path | |
123 | from IPython.utils import py3compat |
|
123 | from IPython.utils import py3compat | |
124 | from IPython.utils import ulinecache |
|
124 | from IPython.utils import ulinecache | |
125 | from IPython.utils.data import uniq_stable |
|
125 | from IPython.utils.data import uniq_stable | |
126 | from IPython.utils.terminal import get_terminal_size |
|
126 | from IPython.utils.terminal import get_terminal_size | |
127 | from logging import info, error |
|
127 | from logging import info, error | |
128 |
|
128 | |||
129 | import IPython.utils.colorable as colorable |
|
129 | import IPython.utils.colorable as colorable | |
130 |
|
130 | |||
131 | # Globals |
|
131 | # Globals | |
132 | # amount of space to put line numbers before verbose tracebacks |
|
132 | # amount of space to put line numbers before verbose tracebacks | |
133 | INDENT_SIZE = 8 |
|
133 | INDENT_SIZE = 8 | |
134 |
|
134 | |||
135 | # Default color scheme. This is used, for example, by the traceback |
|
135 | # Default color scheme. This is used, for example, by the traceback | |
136 | # formatter. When running in an actual IPython instance, the user's rc.colors |
|
136 | # formatter. When running in an actual IPython instance, the user's rc.colors | |
137 | # value is used, but having a module global makes this functionality available |
|
137 | # value is used, but having a module global makes this functionality available | |
138 | # to users of ultratb who are NOT running inside ipython. |
|
138 | # to users of ultratb who are NOT running inside ipython. | |
139 | DEFAULT_SCHEME = 'NoColor' |
|
139 | DEFAULT_SCHEME = 'NoColor' | |
140 |
|
140 | |||
141 | # --------------------------------------------------------------------------- |
|
141 | # --------------------------------------------------------------------------- | |
142 | # Code begins |
|
142 | # Code begins | |
143 |
|
143 | |||
144 | # Utility functions |
|
144 | # Utility functions | |
145 | def inspect_error(): |
|
145 | def inspect_error(): | |
146 | """Print a message about internal inspect errors. |
|
146 | """Print a message about internal inspect errors. | |
147 |
|
147 | |||
148 | These are unfortunately quite common.""" |
|
148 | These are unfortunately quite common.""" | |
149 |
|
149 | |||
150 | error('Internal Python error in the inspect module.\n' |
|
150 | error('Internal Python error in the inspect module.\n' | |
151 | 'Below is the traceback from this internal error.\n') |
|
151 | 'Below is the traceback from this internal error.\n') | |
152 |
|
152 | |||
153 |
|
153 | |||
154 | # This function is a monkeypatch we apply to the Python inspect module. We have |
|
154 | # This function is a monkeypatch we apply to the Python inspect module. We have | |
155 | # now found when it's needed (see discussion on issue gh-1456), and we have a |
|
155 | # now found when it's needed (see discussion on issue gh-1456), and we have a | |
156 | # test case (IPython.core.tests.test_ultratb.ChangedPyFileTest) that fails if |
|
156 | # test case (IPython.core.tests.test_ultratb.ChangedPyFileTest) that fails if | |
157 | # the monkeypatch is not applied. TK, Aug 2012. |
|
157 | # the monkeypatch is not applied. TK, Aug 2012. | |
158 | def findsource(object): |
|
158 | def findsource(object): | |
159 | """Return the entire source file and starting line number for an object. |
|
159 | """Return the entire source file and starting line number for an object. | |
160 |
|
160 | |||
161 | The argument may be a module, class, method, function, traceback, frame, |
|
161 | The argument may be a module, class, method, function, traceback, frame, | |
162 | or code object. The source code is returned as a list of all the lines |
|
162 | or code object. The source code is returned as a list of all the lines | |
163 | in the file and the line number indexes a line in that list. An IOError |
|
163 | in the file and the line number indexes a line in that list. An IOError | |
164 | is raised if the source code cannot be retrieved. |
|
164 | is raised if the source code cannot be retrieved. | |
165 |
|
165 | |||
166 | FIXED version with which we monkeypatch the stdlib to work around a bug.""" |
|
166 | FIXED version with which we monkeypatch the stdlib to work around a bug.""" | |
167 |
|
167 | |||
168 | file = getsourcefile(object) or getfile(object) |
|
168 | file = getsourcefile(object) or getfile(object) | |
169 | # If the object is a frame, then trying to get the globals dict from its |
|
169 | # If the object is a frame, then trying to get the globals dict from its | |
170 | # module won't work. Instead, the frame object itself has the globals |
|
170 | # module won't work. Instead, the frame object itself has the globals | |
171 | # dictionary. |
|
171 | # dictionary. | |
172 | globals_dict = None |
|
172 | globals_dict = None | |
173 | if inspect.isframe(object): |
|
173 | if inspect.isframe(object): | |
174 | # XXX: can this ever be false? |
|
174 | # XXX: can this ever be false? | |
175 | globals_dict = object.f_globals |
|
175 | globals_dict = object.f_globals | |
176 | else: |
|
176 | else: | |
177 | module = getmodule(object, file) |
|
177 | module = getmodule(object, file) | |
178 | if module: |
|
178 | if module: | |
179 | globals_dict = module.__dict__ |
|
179 | globals_dict = module.__dict__ | |
180 | lines = linecache.getlines(file, globals_dict) |
|
180 | lines = linecache.getlines(file, globals_dict) | |
181 | if not lines: |
|
181 | if not lines: | |
182 | raise IOError('could not get source code') |
|
182 | raise IOError('could not get source code') | |
183 |
|
183 | |||
184 | if ismodule(object): |
|
184 | if ismodule(object): | |
185 | return lines, 0 |
|
185 | return lines, 0 | |
186 |
|
186 | |||
187 | if isclass(object): |
|
187 | if isclass(object): | |
188 | name = object.__name__ |
|
188 | name = object.__name__ | |
189 | pat = re.compile(r'^(\s*)class\s*' + name + r'\b') |
|
189 | pat = re.compile(r'^(\s*)class\s*' + name + r'\b') | |
190 | # make some effort to find the best matching class definition: |
|
190 | # make some effort to find the best matching class definition: | |
191 | # use the one with the least indentation, which is the one |
|
191 | # use the one with the least indentation, which is the one | |
192 | # that's most probably not inside a function definition. |
|
192 | # that's most probably not inside a function definition. | |
193 | candidates = [] |
|
193 | candidates = [] | |
194 | for i in range(len(lines)): |
|
194 | for i in range(len(lines)): | |
195 | match = pat.match(lines[i]) |
|
195 | match = pat.match(lines[i]) | |
196 | if match: |
|
196 | if match: | |
197 | # if it's at toplevel, it's already the best one |
|
197 | # if it's at toplevel, it's already the best one | |
198 | if lines[i][0] == 'c': |
|
198 | if lines[i][0] == 'c': | |
199 | return lines, i |
|
199 | return lines, i | |
200 | # else add whitespace to candidate list |
|
200 | # else add whitespace to candidate list | |
201 | candidates.append((match.group(1), i)) |
|
201 | candidates.append((match.group(1), i)) | |
202 | if candidates: |
|
202 | if candidates: | |
203 | # this will sort by whitespace, and by line number, |
|
203 | # this will sort by whitespace, and by line number, | |
204 | # less whitespace first |
|
204 | # less whitespace first | |
205 | candidates.sort() |
|
205 | candidates.sort() | |
206 | return lines, candidates[0][1] |
|
206 | return lines, candidates[0][1] | |
207 | else: |
|
207 | else: | |
208 | raise IOError('could not find class definition') |
|
208 | raise IOError('could not find class definition') | |
209 |
|
209 | |||
210 | if ismethod(object): |
|
210 | if ismethod(object): | |
211 | object = object.__func__ |
|
211 | object = object.__func__ | |
212 | if isfunction(object): |
|
212 | if isfunction(object): | |
213 | object = object.__code__ |
|
213 | object = object.__code__ | |
214 | if istraceback(object): |
|
214 | if istraceback(object): | |
215 | object = object.tb_frame |
|
215 | object = object.tb_frame | |
216 | if isframe(object): |
|
216 | if isframe(object): | |
217 | object = object.f_code |
|
217 | object = object.f_code | |
218 | if iscode(object): |
|
218 | if iscode(object): | |
219 | if not hasattr(object, 'co_firstlineno'): |
|
219 | if not hasattr(object, 'co_firstlineno'): | |
220 | raise IOError('could not find function definition') |
|
220 | raise IOError('could not find function definition') | |
221 | pat = re.compile(r'^(\s*def\s)|(.*(?<!\w)lambda(:|\s))|^(\s*@)') |
|
221 | pat = re.compile(r'^(\s*def\s)|(.*(?<!\w)lambda(:|\s))|^(\s*@)') | |
222 | pmatch = pat.match |
|
222 | pmatch = pat.match | |
223 | # fperez - fix: sometimes, co_firstlineno can give a number larger than |
|
223 | # fperez - fix: sometimes, co_firstlineno can give a number larger than | |
224 | # the length of lines, which causes an error. Safeguard against that. |
|
224 | # the length of lines, which causes an error. Safeguard against that. | |
225 | lnum = min(object.co_firstlineno, len(lines)) - 1 |
|
225 | lnum = min(object.co_firstlineno, len(lines)) - 1 | |
226 | while lnum > 0: |
|
226 | while lnum > 0: | |
227 | if pmatch(lines[lnum]): |
|
227 | if pmatch(lines[lnum]): | |
228 | break |
|
228 | break | |
229 | lnum -= 1 |
|
229 | lnum -= 1 | |
230 |
|
230 | |||
231 | return lines, lnum |
|
231 | return lines, lnum | |
232 | raise IOError('could not find code object') |
|
232 | raise IOError('could not find code object') | |
233 |
|
233 | |||
234 |
|
234 | |||
235 | # This is a patched version of inspect.getargs that applies the (unmerged) |
|
235 | # This is a patched version of inspect.getargs that applies the (unmerged) | |
236 | # patch for http://bugs.python.org/issue14611 by Stefano Taschini. This fixes |
|
236 | # patch for http://bugs.python.org/issue14611 by Stefano Taschini. This fixes | |
237 | # https://github.com/ipython/ipython/issues/8205 and |
|
237 | # https://github.com/ipython/ipython/issues/8205 and | |
238 | # https://github.com/ipython/ipython/issues/8293 |
|
238 | # https://github.com/ipython/ipython/issues/8293 | |
239 | def getargs(co): |
|
239 | def getargs(co): | |
240 | """Get information about the arguments accepted by a code object. |
|
240 | """Get information about the arguments accepted by a code object. | |
241 |
|
241 | |||
242 | Three things are returned: (args, varargs, varkw), where 'args' is |
|
242 | Three things are returned: (args, varargs, varkw), where 'args' is | |
243 | a list of argument names (possibly containing nested lists), and |
|
243 | a list of argument names (possibly containing nested lists), and | |
244 | 'varargs' and 'varkw' are the names of the * and ** arguments or None.""" |
|
244 | 'varargs' and 'varkw' are the names of the * and ** arguments or None.""" | |
245 | if not iscode(co): |
|
245 | if not iscode(co): | |
246 | raise TypeError('{!r} is not a code object'.format(co)) |
|
246 | raise TypeError('{!r} is not a code object'.format(co)) | |
247 |
|
247 | |||
248 | nargs = co.co_argcount |
|
248 | nargs = co.co_argcount | |
249 | names = co.co_varnames |
|
249 | names = co.co_varnames | |
250 | args = list(names[:nargs]) |
|
250 | args = list(names[:nargs]) | |
251 | step = 0 |
|
251 | step = 0 | |
252 |
|
252 | |||
253 | # The following acrobatics are for anonymous (tuple) arguments. |
|
253 | # The following acrobatics are for anonymous (tuple) arguments. | |
254 | for i in range(nargs): |
|
254 | for i in range(nargs): | |
255 | if args[i][:1] in ('', '.'): |
|
255 | if args[i][:1] in ('', '.'): | |
256 | stack, remain, count = [], [], [] |
|
256 | stack, remain, count = [], [], [] | |
257 | while step < len(co.co_code): |
|
257 | while step < len(co.co_code): | |
258 | op = ord(co.co_code[step]) |
|
258 | op = ord(co.co_code[step]) | |
259 | step = step + 1 |
|
259 | step = step + 1 | |
260 | if op >= dis.HAVE_ARGUMENT: |
|
260 | if op >= dis.HAVE_ARGUMENT: | |
261 | opname = dis.opname[op] |
|
261 | opname = dis.opname[op] | |
262 | value = ord(co.co_code[step]) + ord(co.co_code[step+1])*256 |
|
262 | value = ord(co.co_code[step]) + ord(co.co_code[step+1])*256 | |
263 | step = step + 2 |
|
263 | step = step + 2 | |
264 | if opname in ('UNPACK_TUPLE', 'UNPACK_SEQUENCE'): |
|
264 | if opname in ('UNPACK_TUPLE', 'UNPACK_SEQUENCE'): | |
265 | remain.append(value) |
|
265 | remain.append(value) | |
266 | count.append(value) |
|
266 | count.append(value) | |
267 | elif opname in ('STORE_FAST', 'STORE_DEREF'): |
|
267 | elif opname in ('STORE_FAST', 'STORE_DEREF'): | |
268 | if op in dis.haslocal: |
|
268 | if op in dis.haslocal: | |
269 | stack.append(co.co_varnames[value]) |
|
269 | stack.append(co.co_varnames[value]) | |
270 | elif op in dis.hasfree: |
|
270 | elif op in dis.hasfree: | |
271 | stack.append((co.co_cellvars + co.co_freevars)[value]) |
|
271 | stack.append((co.co_cellvars + co.co_freevars)[value]) | |
272 | # Special case for sublists of length 1: def foo((bar)) |
|
272 | # Special case for sublists of length 1: def foo((bar)) | |
273 | # doesn't generate the UNPACK_TUPLE bytecode, so if |
|
273 | # doesn't generate the UNPACK_TUPLE bytecode, so if | |
274 | # `remain` is empty here, we have such a sublist. |
|
274 | # `remain` is empty here, we have such a sublist. | |
275 | if not remain: |
|
275 | if not remain: | |
276 | stack[0] = [stack[0]] |
|
276 | stack[0] = [stack[0]] | |
277 | break |
|
277 | break | |
278 | else: |
|
278 | else: | |
279 | remain[-1] = remain[-1] - 1 |
|
279 | remain[-1] = remain[-1] - 1 | |
280 | while remain[-1] == 0: |
|
280 | while remain[-1] == 0: | |
281 | remain.pop() |
|
281 | remain.pop() | |
282 | size = count.pop() |
|
282 | size = count.pop() | |
283 | stack[-size:] = [stack[-size:]] |
|
283 | stack[-size:] = [stack[-size:]] | |
284 | if not remain: |
|
284 | if not remain: | |
285 | break |
|
285 | break | |
286 | remain[-1] = remain[-1] - 1 |
|
286 | remain[-1] = remain[-1] - 1 | |
287 | if not remain: |
|
287 | if not remain: | |
288 | break |
|
288 | break | |
289 | args[i] = stack[0] |
|
289 | args[i] = stack[0] | |
290 |
|
290 | |||
291 | varargs = None |
|
291 | varargs = None | |
292 | if co.co_flags & inspect.CO_VARARGS: |
|
292 | if co.co_flags & inspect.CO_VARARGS: | |
293 | varargs = co.co_varnames[nargs] |
|
293 | varargs = co.co_varnames[nargs] | |
294 | nargs = nargs + 1 |
|
294 | nargs = nargs + 1 | |
295 | varkw = None |
|
295 | varkw = None | |
296 | if co.co_flags & inspect.CO_VARKEYWORDS: |
|
296 | if co.co_flags & inspect.CO_VARKEYWORDS: | |
297 | varkw = co.co_varnames[nargs] |
|
297 | varkw = co.co_varnames[nargs] | |
298 | return inspect.Arguments(args, varargs, varkw) |
|
298 | return inspect.Arguments(args, varargs, varkw) | |
299 |
|
299 | |||
300 |
|
300 | |||
301 | # Monkeypatch inspect to apply our bugfix. |
|
301 | # Monkeypatch inspect to apply our bugfix. | |
302 | def with_patch_inspect(f): |
|
302 | def with_patch_inspect(f): | |
303 | """decorator for monkeypatching inspect.findsource""" |
|
303 | """decorator for monkeypatching inspect.findsource""" | |
304 |
|
304 | |||
305 | def wrapped(*args, **kwargs): |
|
305 | def wrapped(*args, **kwargs): | |
306 | save_findsource = inspect.findsource |
|
306 | save_findsource = inspect.findsource | |
307 | save_getargs = inspect.getargs |
|
307 | save_getargs = inspect.getargs | |
308 | inspect.findsource = findsource |
|
308 | inspect.findsource = findsource | |
309 | inspect.getargs = getargs |
|
309 | inspect.getargs = getargs | |
310 | try: |
|
310 | try: | |
311 | return f(*args, **kwargs) |
|
311 | return f(*args, **kwargs) | |
312 | finally: |
|
312 | finally: | |
313 | inspect.findsource = save_findsource |
|
313 | inspect.findsource = save_findsource | |
314 | inspect.getargs = save_getargs |
|
314 | inspect.getargs = save_getargs | |
315 |
|
315 | |||
316 | return wrapped |
|
316 | return wrapped | |
317 |
|
317 | |||
318 |
|
318 | |||
319 | if py3compat.PY3: |
|
319 | if py3compat.PY3: | |
320 | fixed_getargvalues = inspect.getargvalues |
|
320 | fixed_getargvalues = inspect.getargvalues | |
321 | else: |
|
321 | else: | |
322 | # Fixes for https://github.com/ipython/ipython/issues/8293 |
|
322 | # Fixes for https://github.com/ipython/ipython/issues/8293 | |
323 | # and https://github.com/ipython/ipython/issues/8205. |
|
323 | # and https://github.com/ipython/ipython/issues/8205. | |
324 | # The relevant bug is caused by failure to correctly handle anonymous tuple |
|
324 | # The relevant bug is caused by failure to correctly handle anonymous tuple | |
325 | # unpacking, which only exists in Python 2. |
|
325 | # unpacking, which only exists in Python 2. | |
326 | fixed_getargvalues = with_patch_inspect(inspect.getargvalues) |
|
326 | fixed_getargvalues = with_patch_inspect(inspect.getargvalues) | |
327 |
|
327 | |||
328 |
|
328 | |||
329 | def fix_frame_records_filenames(records): |
|
329 | def fix_frame_records_filenames(records): | |
330 | """Try to fix the filenames in each record from inspect.getinnerframes(). |
|
330 | """Try to fix the filenames in each record from inspect.getinnerframes(). | |
331 |
|
331 | |||
332 | Particularly, modules loaded from within zip files have useless filenames |
|
332 | Particularly, modules loaded from within zip files have useless filenames | |
333 | attached to their code object, and inspect.getinnerframes() just uses it. |
|
333 | attached to their code object, and inspect.getinnerframes() just uses it. | |
334 | """ |
|
334 | """ | |
335 | fixed_records = [] |
|
335 | fixed_records = [] | |
336 | for frame, filename, line_no, func_name, lines, index in records: |
|
336 | for frame, filename, line_no, func_name, lines, index in records: | |
337 | # Look inside the frame's globals dictionary for __file__, |
|
337 | # Look inside the frame's globals dictionary for __file__, | |
338 | # which should be better. However, keep Cython filenames since |
|
338 | # which should be better. However, keep Cython filenames since | |
339 | # we prefer the source filenames over the compiled .so file. |
|
339 | # we prefer the source filenames over the compiled .so file. | |
340 | filename = py3compat.cast_unicode_py2(filename, "utf-8") |
|
340 | filename = py3compat.cast_unicode_py2(filename, "utf-8") | |
341 | if not filename.endswith(('.pyx', '.pxd', '.pxi')): |
|
341 | if not filename.endswith(('.pyx', '.pxd', '.pxi')): | |
342 | better_fn = frame.f_globals.get('__file__', None) |
|
342 | better_fn = frame.f_globals.get('__file__', None) | |
343 | if isinstance(better_fn, str): |
|
343 | if isinstance(better_fn, str): | |
344 | # Check the type just in case someone did something weird with |
|
344 | # Check the type just in case someone did something weird with | |
345 | # __file__. It might also be None if the error occurred during |
|
345 | # __file__. It might also be None if the error occurred during | |
346 | # import. |
|
346 | # import. | |
347 | filename = better_fn |
|
347 | filename = better_fn | |
348 | fixed_records.append((frame, filename, line_no, func_name, lines, index)) |
|
348 | fixed_records.append((frame, filename, line_no, func_name, lines, index)) | |
349 | return fixed_records |
|
349 | return fixed_records | |
350 |
|
350 | |||
351 |
|
351 | |||
352 | @with_patch_inspect |
|
352 | @with_patch_inspect | |
353 | def _fixed_getinnerframes(etb, context=1, tb_offset=0): |
|
353 | def _fixed_getinnerframes(etb, context=1, tb_offset=0): | |
354 | LNUM_POS, LINES_POS, INDEX_POS = 2, 4, 5 |
|
354 | LNUM_POS, LINES_POS, INDEX_POS = 2, 4, 5 | |
355 |
|
355 | |||
356 | records = fix_frame_records_filenames(inspect.getinnerframes(etb, context)) |
|
356 | records = fix_frame_records_filenames(inspect.getinnerframes(etb, context)) | |
357 | # If the error is at the console, don't build any context, since it would |
|
357 | # If the error is at the console, don't build any context, since it would | |
358 | # otherwise produce 5 blank lines printed out (there is no file at the |
|
358 | # otherwise produce 5 blank lines printed out (there is no file at the | |
359 | # console) |
|
359 | # console) | |
360 | rec_check = records[tb_offset:] |
|
360 | rec_check = records[tb_offset:] | |
361 | try: |
|
361 | try: | |
362 | rname = rec_check[0][1] |
|
362 | rname = rec_check[0][1] | |
363 | if rname == '<ipython console>' or rname.endswith('<string>'): |
|
363 | if rname == '<ipython console>' or rname.endswith('<string>'): | |
364 | return rec_check |
|
364 | return rec_check | |
365 | except IndexError: |
|
365 | except IndexError: | |
366 | pass |
|
366 | pass | |
367 |
|
367 | |||
368 | aux = traceback.extract_tb(etb) |
|
368 | aux = traceback.extract_tb(etb) | |
369 | assert len(records) == len(aux) |
|
369 | assert len(records) == len(aux) | |
370 | for i, (file, lnum, _, _) in zip(range(len(records)), aux): |
|
370 | for i, (file, lnum, _, _) in zip(range(len(records)), aux): | |
371 | maybeStart = lnum - 1 - context // 2 |
|
371 | maybeStart = lnum - 1 - context // 2 | |
372 | start = max(maybeStart, 0) |
|
372 | start = max(maybeStart, 0) | |
373 | end = start + context |
|
373 | end = start + context | |
374 | lines = ulinecache.getlines(file)[start:end] |
|
374 | lines = ulinecache.getlines(file)[start:end] | |
375 | buf = list(records[i]) |
|
375 | buf = list(records[i]) | |
376 | buf[LNUM_POS] = lnum |
|
376 | buf[LNUM_POS] = lnum | |
377 | buf[INDEX_POS] = lnum - 1 - start |
|
377 | buf[INDEX_POS] = lnum - 1 - start | |
378 | buf[LINES_POS] = lines |
|
378 | buf[LINES_POS] = lines | |
379 | records[i] = tuple(buf) |
|
379 | records[i] = tuple(buf) | |
380 | return records[tb_offset:] |
|
380 | return records[tb_offset:] | |
381 |
|
381 | |||
382 | # Helper function -- largely belongs to VerboseTB, but we need the same |
|
382 | # Helper function -- largely belongs to VerboseTB, but we need the same | |
383 | # functionality to produce a pseudo verbose TB for SyntaxErrors, so that they |
|
383 | # functionality to produce a pseudo verbose TB for SyntaxErrors, so that they | |
384 | # can be recognized properly by ipython.el's py-traceback-line-re |
|
384 | # can be recognized properly by ipython.el's py-traceback-line-re | |
385 | # (SyntaxErrors have to be treated specially because they have no traceback) |
|
385 | # (SyntaxErrors have to be treated specially because they have no traceback) | |
386 |
|
386 | |||
387 | _parser = PyColorize.Parser() |
|
387 | _parser = PyColorize.Parser() | |
388 |
|
388 | |||
389 |
|
389 | |||
390 | def _format_traceback_lines(lnum, index, lines, Colors, lvals=None, scheme=None): |
|
390 | def _format_traceback_lines(lnum, index, lines, Colors, lvals=None, scheme=None): | |
391 | numbers_width = INDENT_SIZE - 1 |
|
391 | numbers_width = INDENT_SIZE - 1 | |
392 | res = [] |
|
392 | res = [] | |
393 | i = lnum - index |
|
393 | i = lnum - index | |
394 |
|
394 | |||
395 | # This lets us get fully syntax-highlighted tracebacks. |
|
395 | # This lets us get fully syntax-highlighted tracebacks. | |
396 | if scheme is None: |
|
396 | if scheme is None: | |
397 | ipinst = get_ipython() |
|
397 | ipinst = get_ipython() | |
398 | if ipinst is not None: |
|
398 | if ipinst is not None: | |
399 | scheme = ipinst.colors |
|
399 | scheme = ipinst.colors | |
400 | else: |
|
400 | else: | |
401 | scheme = DEFAULT_SCHEME |
|
401 | scheme = DEFAULT_SCHEME | |
402 |
|
402 | |||
403 | _line_format = _parser.format2 |
|
403 | _line_format = _parser.format2 | |
404 |
|
404 | |||
405 | for line in lines: |
|
405 | for line in lines: | |
406 | line = py3compat.cast_unicode(line) |
|
406 | line = py3compat.cast_unicode(line) | |
407 |
|
407 | |||
408 | new_line, err = _line_format(line, 'str', scheme) |
|
408 | new_line, err = _line_format(line, 'str', scheme) | |
409 | if not err: line = new_line |
|
409 | if not err: line = new_line | |
410 |
|
410 | |||
411 | if i == lnum: |
|
411 | if i == lnum: | |
412 | # This is the line with the error |
|
412 | # This is the line with the error | |
413 | pad = numbers_width - len(str(i)) |
|
413 | pad = numbers_width - len(str(i)) | |
414 | num = '%s%s' % (debugger.make_arrow(pad), str(lnum)) |
|
414 | num = '%s%s' % (debugger.make_arrow(pad), str(lnum)) | |
415 | line = '%s%s%s %s%s' % (Colors.linenoEm, num, |
|
415 | line = '%s%s%s %s%s' % (Colors.linenoEm, num, | |
416 | Colors.line, line, Colors.Normal) |
|
416 | Colors.line, line, Colors.Normal) | |
417 | else: |
|
417 | else: | |
418 | num = '%*s' % (numbers_width, i) |
|
418 | num = '%*s' % (numbers_width, i) | |
419 | line = '%s%s%s %s' % (Colors.lineno, num, |
|
419 | line = '%s%s%s %s' % (Colors.lineno, num, | |
420 | Colors.Normal, line) |
|
420 | Colors.Normal, line) | |
421 |
|
421 | |||
422 | res.append(line) |
|
422 | res.append(line) | |
423 | if lvals and i == lnum: |
|
423 | if lvals and i == lnum: | |
424 | res.append(lvals + '\n') |
|
424 | res.append(lvals + '\n') | |
425 | i = i + 1 |
|
425 | i = i + 1 | |
426 | return res |
|
426 | return res | |
427 |
|
427 | |||
428 | def is_recursion_error(etype, value, records): |
|
428 | def is_recursion_error(etype, value, records): | |
429 | try: |
|
429 | try: | |
430 | # RecursionError is new in Python 3.5 |
|
430 | # RecursionError is new in Python 3.5 | |
431 | recursion_error_type = RecursionError |
|
431 | recursion_error_type = RecursionError | |
432 | except NameError: |
|
432 | except NameError: | |
433 | recursion_error_type = RuntimeError |
|
433 | recursion_error_type = RuntimeError | |
434 |
|
434 | |||
435 | # The default recursion limit is 1000, but some of that will be taken up |
|
435 | # The default recursion limit is 1000, but some of that will be taken up | |
436 | # by stack frames in IPython itself. >500 frames probably indicates |
|
436 | # by stack frames in IPython itself. >500 frames probably indicates | |
437 | # a recursion error. |
|
437 | # a recursion error. | |
438 | return (etype is recursion_error_type) \ |
|
438 | return (etype is recursion_error_type) \ | |
439 | and "recursion" in str(value).lower() \ |
|
439 | and "recursion" in str(value).lower() \ | |
440 | and len(records) > 500 |
|
440 | and len(records) > 500 | |
441 |
|
441 | |||
442 | def find_recursion(etype, value, records): |
|
442 | def find_recursion(etype, value, records): | |
443 | """Identify the repeating stack frames from a RecursionError traceback |
|
443 | """Identify the repeating stack frames from a RecursionError traceback | |
444 |
|
444 | |||
445 | 'records' is a list as returned by VerboseTB.get_records() |
|
445 | 'records' is a list as returned by VerboseTB.get_records() | |
446 |
|
446 | |||
447 | Returns (last_unique, repeat_length) |
|
447 | Returns (last_unique, repeat_length) | |
448 | """ |
|
448 | """ | |
449 | # This involves a bit of guesswork - we want to show enough of the traceback |
|
449 | # This involves a bit of guesswork - we want to show enough of the traceback | |
450 | # to indicate where the recursion is occurring. We guess that the innermost |
|
450 | # to indicate where the recursion is occurring. We guess that the innermost | |
451 | # quarter of the traceback (250 frames by default) is repeats, and find the |
|
451 | # quarter of the traceback (250 frames by default) is repeats, and find the | |
452 | # first frame (from in to out) that looks different. |
|
452 | # first frame (from in to out) that looks different. | |
453 | if not is_recursion_error(etype, value, records): |
|
453 | if not is_recursion_error(etype, value, records): | |
454 | return len(records), 0 |
|
454 | return len(records), 0 | |
455 |
|
455 | |||
456 | # Select filename, lineno, func_name to track frames with |
|
456 | # Select filename, lineno, func_name to track frames with | |
457 | records = [r[1:4] for r in records] |
|
457 | records = [r[1:4] for r in records] | |
458 | inner_frames = records[-(len(records)//4):] |
|
458 | inner_frames = records[-(len(records)//4):] | |
459 | frames_repeated = set(inner_frames) |
|
459 | frames_repeated = set(inner_frames) | |
460 |
|
460 | |||
461 | last_seen_at = {} |
|
461 | last_seen_at = {} | |
462 | longest_repeat = 0 |
|
462 | longest_repeat = 0 | |
463 | i = len(records) |
|
463 | i = len(records) | |
464 | for frame in reversed(records): |
|
464 | for frame in reversed(records): | |
465 | i -= 1 |
|
465 | i -= 1 | |
466 | if frame not in frames_repeated: |
|
466 | if frame not in frames_repeated: | |
467 | last_unique = i |
|
467 | last_unique = i | |
468 | break |
|
468 | break | |
469 |
|
469 | |||
470 | if frame in last_seen_at: |
|
470 | if frame in last_seen_at: | |
471 | distance = last_seen_at[frame] - i |
|
471 | distance = last_seen_at[frame] - i | |
472 | longest_repeat = max(longest_repeat, distance) |
|
472 | longest_repeat = max(longest_repeat, distance) | |
473 |
|
473 | |||
474 | last_seen_at[frame] = i |
|
474 | last_seen_at[frame] = i | |
475 | else: |
|
475 | else: | |
476 | last_unique = 0 # The whole traceback was recursion |
|
476 | last_unique = 0 # The whole traceback was recursion | |
477 |
|
477 | |||
478 | return last_unique, longest_repeat |
|
478 | return last_unique, longest_repeat | |
479 |
|
479 | |||
480 | #--------------------------------------------------------------------------- |
|
480 | #--------------------------------------------------------------------------- | |
481 | # Module classes |
|
481 | # Module classes | |
482 | class TBTools(colorable.Colorable): |
|
482 | class TBTools(colorable.Colorable): | |
483 | """Basic tools used by all traceback printer classes.""" |
|
483 | """Basic tools used by all traceback printer classes.""" | |
484 |
|
484 | |||
485 | # Number of frames to skip when reporting tracebacks |
|
485 | # Number of frames to skip when reporting tracebacks | |
486 | tb_offset = 0 |
|
486 | tb_offset = 0 | |
487 |
|
487 | |||
488 | def __init__(self, color_scheme='NoColor', call_pdb=False, ostream=None, parent=None, config=None): |
|
488 | def __init__(self, color_scheme='NoColor', call_pdb=False, ostream=None, parent=None, config=None): | |
489 | # Whether to call the interactive pdb debugger after printing |
|
489 | # Whether to call the interactive pdb debugger after printing | |
490 | # tracebacks or not |
|
490 | # tracebacks or not | |
491 | super(TBTools, self).__init__(parent=parent, config=config) |
|
491 | super(TBTools, self).__init__(parent=parent, config=config) | |
492 | self.call_pdb = call_pdb |
|
492 | self.call_pdb = call_pdb | |
493 |
|
493 | |||
494 | # Output stream to write to. Note that we store the original value in |
|
494 | # Output stream to write to. Note that we store the original value in | |
495 | # a private attribute and then make the public ostream a property, so |
|
495 | # a private attribute and then make the public ostream a property, so | |
496 | # that we can delay accessing io.stdout until runtime. The way |
|
496 | # that we can delay accessing io.stdout until runtime. The way | |
497 | # things are written now, the io.stdout object is dynamically managed |
|
497 | # things are written now, the io.stdout object is dynamically managed | |
498 | # so a reference to it should NEVER be stored statically. This |
|
498 | # so a reference to it should NEVER be stored statically. This | |
499 | # property approach confines this detail to a single location, and all |
|
499 | # property approach confines this detail to a single location, and all | |
500 | # subclasses can simply access self.ostream for writing. |
|
500 | # subclasses can simply access self.ostream for writing. | |
501 | self._ostream = ostream |
|
501 | self._ostream = ostream | |
502 |
|
502 | |||
503 | # Create color table |
|
503 | # Create color table | |
504 | self.color_scheme_table = exception_colors() |
|
504 | self.color_scheme_table = exception_colors() | |
505 |
|
505 | |||
506 | self.set_colors(color_scheme) |
|
506 | self.set_colors(color_scheme) | |
507 | self.old_scheme = color_scheme # save initial value for toggles |
|
507 | self.old_scheme = color_scheme # save initial value for toggles | |
508 |
|
508 | |||
509 | if call_pdb: |
|
509 | if call_pdb: | |
510 | self.pdb = debugger.Pdb(self.color_scheme_table.active_scheme_name) |
|
510 | self.pdb = debugger.Pdb(self.color_scheme_table.active_scheme_name) | |
511 | else: |
|
511 | else: | |
512 | self.pdb = None |
|
512 | self.pdb = None | |
513 |
|
513 | |||
514 | def _get_ostream(self): |
|
514 | def _get_ostream(self): | |
515 | """Output stream that exceptions are written to. |
|
515 | """Output stream that exceptions are written to. | |
516 |
|
516 | |||
517 | Valid values are: |
|
517 | Valid values are: | |
518 |
|
518 | |||
519 | - None: the default, which means that IPython will dynamically resolve |
|
519 | - None: the default, which means that IPython will dynamically resolve | |
520 | to io.stdout. This ensures compatibility with most tools, including |
|
520 | to io.stdout. This ensures compatibility with most tools, including | |
521 | Windows (where plain stdout doesn't recognize ANSI escapes). |
|
521 | Windows (where plain stdout doesn't recognize ANSI escapes). | |
522 |
|
522 | |||
523 | - Any object with 'write' and 'flush' attributes. |
|
523 | - Any object with 'write' and 'flush' attributes. | |
524 | """ |
|
524 | """ | |
525 | return sys.stdout if self._ostream is None else self._ostream |
|
525 | return sys.stdout if self._ostream is None else self._ostream | |
526 |
|
526 | |||
527 | def _set_ostream(self, val): |
|
527 | def _set_ostream(self, val): | |
528 | assert val is None or (hasattr(val, 'write') and hasattr(val, 'flush')) |
|
528 | assert val is None or (hasattr(val, 'write') and hasattr(val, 'flush')) | |
529 | self._ostream = val |
|
529 | self._ostream = val | |
530 |
|
530 | |||
531 | ostream = property(_get_ostream, _set_ostream) |
|
531 | ostream = property(_get_ostream, _set_ostream) | |
532 |
|
532 | |||
533 | def set_colors(self, *args, **kw): |
|
533 | def set_colors(self, *args, **kw): | |
534 | """Shorthand access to the color table scheme selector method.""" |
|
534 | """Shorthand access to the color table scheme selector method.""" | |
535 |
|
535 | |||
536 | # Set own color table |
|
536 | # Set own color table | |
537 | self.color_scheme_table.set_active_scheme(*args, **kw) |
|
537 | self.color_scheme_table.set_active_scheme(*args, **kw) | |
538 | # for convenience, set Colors to the active scheme |
|
538 | # for convenience, set Colors to the active scheme | |
539 | self.Colors = self.color_scheme_table.active_colors |
|
539 | self.Colors = self.color_scheme_table.active_colors | |
540 | # Also set colors of debugger |
|
540 | # Also set colors of debugger | |
541 | if hasattr(self, 'pdb') and self.pdb is not None: |
|
541 | if hasattr(self, 'pdb') and self.pdb is not None: | |
542 | self.pdb.set_colors(*args, **kw) |
|
542 | self.pdb.set_colors(*args, **kw) | |
543 |
|
543 | |||
544 | def color_toggle(self): |
|
544 | def color_toggle(self): | |
545 | """Toggle between the currently active color scheme and NoColor.""" |
|
545 | """Toggle between the currently active color scheme and NoColor.""" | |
546 |
|
546 | |||
547 | if self.color_scheme_table.active_scheme_name == 'NoColor': |
|
547 | if self.color_scheme_table.active_scheme_name == 'NoColor': | |
548 | self.color_scheme_table.set_active_scheme(self.old_scheme) |
|
548 | self.color_scheme_table.set_active_scheme(self.old_scheme) | |
549 | self.Colors = self.color_scheme_table.active_colors |
|
549 | self.Colors = self.color_scheme_table.active_colors | |
550 | else: |
|
550 | else: | |
551 | self.old_scheme = self.color_scheme_table.active_scheme_name |
|
551 | self.old_scheme = self.color_scheme_table.active_scheme_name | |
552 | self.color_scheme_table.set_active_scheme('NoColor') |
|
552 | self.color_scheme_table.set_active_scheme('NoColor') | |
553 | self.Colors = self.color_scheme_table.active_colors |
|
553 | self.Colors = self.color_scheme_table.active_colors | |
554 |
|
554 | |||
555 | def stb2text(self, stb): |
|
555 | def stb2text(self, stb): | |
556 | """Convert a structured traceback (a list) to a string.""" |
|
556 | """Convert a structured traceback (a list) to a string.""" | |
557 | return '\n'.join(stb) |
|
557 | return '\n'.join(stb) | |
558 |
|
558 | |||
559 | def text(self, etype, value, tb, tb_offset=None, context=5): |
|
559 | def text(self, etype, value, tb, tb_offset=None, context=5): | |
560 | """Return formatted traceback. |
|
560 | """Return formatted traceback. | |
561 |
|
561 | |||
562 | Subclasses may override this if they add extra arguments. |
|
562 | Subclasses may override this if they add extra arguments. | |
563 | """ |
|
563 | """ | |
564 | tb_list = self.structured_traceback(etype, value, tb, |
|
564 | tb_list = self.structured_traceback(etype, value, tb, | |
565 | tb_offset, context) |
|
565 | tb_offset, context) | |
566 | return self.stb2text(tb_list) |
|
566 | return self.stb2text(tb_list) | |
567 |
|
567 | |||
568 | def structured_traceback(self, etype, evalue, tb, tb_offset=None, |
|
568 | def structured_traceback(self, etype, evalue, tb, tb_offset=None, | |
569 | context=5, mode=None): |
|
569 | context=5, mode=None): | |
570 | """Return a list of traceback frames. |
|
570 | """Return a list of traceback frames. | |
571 |
|
571 | |||
572 | Must be implemented by each class. |
|
572 | Must be implemented by each class. | |
573 | """ |
|
573 | """ | |
574 | raise NotImplementedError() |
|
574 | raise NotImplementedError() | |
575 |
|
575 | |||
576 |
|
576 | |||
577 | #--------------------------------------------------------------------------- |
|
577 | #--------------------------------------------------------------------------- | |
578 | class ListTB(TBTools): |
|
578 | class ListTB(TBTools): | |
579 | """Print traceback information from a traceback list, with optional color. |
|
579 | """Print traceback information from a traceback list, with optional color. | |
580 |
|
580 | |||
581 | Calling requires 3 arguments: (etype, evalue, elist) |
|
581 | Calling requires 3 arguments: (etype, evalue, elist) | |
582 | as would be obtained by:: |
|
582 | as would be obtained by:: | |
583 |
|
583 | |||
584 | etype, evalue, tb = sys.exc_info() |
|
584 | etype, evalue, tb = sys.exc_info() | |
585 | if tb: |
|
585 | if tb: | |
586 | elist = traceback.extract_tb(tb) |
|
586 | elist = traceback.extract_tb(tb) | |
587 | else: |
|
587 | else: | |
588 | elist = None |
|
588 | elist = None | |
589 |
|
589 | |||
590 | It can thus be used by programs which need to process the traceback before |
|
590 | It can thus be used by programs which need to process the traceback before | |
591 | printing (such as console replacements based on the code module from the |
|
591 | printing (such as console replacements based on the code module from the | |
592 | standard library). |
|
592 | standard library). | |
593 |
|
593 | |||
594 | Because they are meant to be called without a full traceback (only a |
|
594 | Because they are meant to be called without a full traceback (only a | |
595 | list), instances of this class can't call the interactive pdb debugger.""" |
|
595 | list), instances of this class can't call the interactive pdb debugger.""" | |
596 |
|
596 | |||
597 | def __init__(self, color_scheme='NoColor', call_pdb=False, ostream=None, parent=None): |
|
597 | def __init__(self, color_scheme='NoColor', call_pdb=False, ostream=None, parent=None): | |
598 | TBTools.__init__(self, color_scheme=color_scheme, call_pdb=call_pdb, |
|
598 | TBTools.__init__(self, color_scheme=color_scheme, call_pdb=call_pdb, | |
599 | ostream=ostream, parent=parent) |
|
599 | ostream=ostream, parent=parent) | |
600 |
|
600 | |||
601 | def __call__(self, etype, value, elist): |
|
601 | def __call__(self, etype, value, elist): | |
602 | self.ostream.flush() |
|
602 | self.ostream.flush() | |
603 | self.ostream.write(self.text(etype, value, elist)) |
|
603 | self.ostream.write(self.text(etype, value, elist)) | |
604 | self.ostream.write('\n') |
|
604 | self.ostream.write('\n') | |
605 |
|
605 | |||
606 | def structured_traceback(self, etype, value, elist, tb_offset=None, |
|
606 | def structured_traceback(self, etype, value, elist, tb_offset=None, | |
607 | context=5): |
|
607 | context=5): | |
608 | """Return a color formatted string with the traceback info. |
|
608 | """Return a color formatted string with the traceback info. | |
609 |
|
609 | |||
610 | Parameters |
|
610 | Parameters | |
611 | ---------- |
|
611 | ---------- | |
612 | etype : exception type |
|
612 | etype : exception type | |
613 | Type of the exception raised. |
|
613 | Type of the exception raised. | |
614 |
|
614 | |||
615 | value : object |
|
615 | value : object | |
616 | Data stored in the exception |
|
616 | Data stored in the exception | |
617 |
|
617 | |||
618 | elist : list |
|
618 | elist : list | |
619 | List of frames, see class docstring for details. |
|
619 | List of frames, see class docstring for details. | |
620 |
|
620 | |||
621 | tb_offset : int, optional |
|
621 | tb_offset : int, optional | |
622 | Number of frames in the traceback to skip. If not given, the |
|
622 | Number of frames in the traceback to skip. If not given, the | |
623 | instance value is used (set in constructor). |
|
623 | instance value is used (set in constructor). | |
624 |
|
624 | |||
625 | context : int, optional |
|
625 | context : int, optional | |
626 | Number of lines of context information to print. |
|
626 | Number of lines of context information to print. | |
627 |
|
627 | |||
628 | Returns |
|
628 | Returns | |
629 | ------- |
|
629 | ------- | |
630 | String with formatted exception. |
|
630 | String with formatted exception. | |
631 | """ |
|
631 | """ | |
632 | tb_offset = self.tb_offset if tb_offset is None else tb_offset |
|
632 | tb_offset = self.tb_offset if tb_offset is None else tb_offset | |
633 | Colors = self.Colors |
|
633 | Colors = self.Colors | |
634 | out_list = [] |
|
634 | out_list = [] | |
635 | if elist: |
|
635 | if elist: | |
636 |
|
636 | |||
637 | if tb_offset and len(elist) > tb_offset: |
|
637 | if tb_offset and len(elist) > tb_offset: | |
638 | elist = elist[tb_offset:] |
|
638 | elist = elist[tb_offset:] | |
639 |
|
639 | |||
640 | out_list.append('Traceback %s(most recent call last)%s:' % |
|
640 | out_list.append('Traceback %s(most recent call last)%s:' % | |
641 | (Colors.normalEm, Colors.Normal) + '\n') |
|
641 | (Colors.normalEm, Colors.Normal) + '\n') | |
642 | out_list.extend(self._format_list(elist)) |
|
642 | out_list.extend(self._format_list(elist)) | |
643 | # The exception info should be a single entry in the list. |
|
643 | # The exception info should be a single entry in the list. | |
644 | lines = ''.join(self._format_exception_only(etype, value)) |
|
644 | lines = ''.join(self._format_exception_only(etype, value)) | |
645 | out_list.append(lines) |
|
645 | out_list.append(lines) | |
646 |
|
646 | |||
647 | # Note: this code originally read: |
|
647 | # Note: this code originally read: | |
648 |
|
648 | |||
649 | ## for line in lines[:-1]: |
|
649 | ## for line in lines[:-1]: | |
650 | ## out_list.append(" "+line) |
|
650 | ## out_list.append(" "+line) | |
651 | ## out_list.append(lines[-1]) |
|
651 | ## out_list.append(lines[-1]) | |
652 |
|
652 | |||
653 | # This means it was indenting everything but the last line by a little |
|
653 | # This means it was indenting everything but the last line by a little | |
654 | # bit. I've disabled this for now, but if we see ugliness somewhere we |
|
654 | # bit. I've disabled this for now, but if we see ugliness somewhere we | |
655 | # can restore it. |
|
655 | # can restore it. | |
656 |
|
656 | |||
657 | return out_list |
|
657 | return out_list | |
658 |
|
658 | |||
659 | def _format_list(self, extracted_list): |
|
659 | def _format_list(self, extracted_list): | |
660 | """Format a list of traceback entry tuples for printing. |
|
660 | """Format a list of traceback entry tuples for printing. | |
661 |
|
661 | |||
662 | Given a list of tuples as returned by extract_tb() or |
|
662 | Given a list of tuples as returned by extract_tb() or | |
663 | extract_stack(), return a list of strings ready for printing. |
|
663 | extract_stack(), return a list of strings ready for printing. | |
664 | Each string in the resulting list corresponds to the item with the |
|
664 | Each string in the resulting list corresponds to the item with the | |
665 | same index in the argument list. Each string ends in a newline; |
|
665 | same index in the argument list. Each string ends in a newline; | |
666 | the strings may contain internal newlines as well, for those items |
|
666 | the strings may contain internal newlines as well, for those items | |
667 | whose source text line is not None. |
|
667 | whose source text line is not None. | |
668 |
|
668 | |||
669 | Lifted almost verbatim from traceback.py |
|
669 | Lifted almost verbatim from traceback.py | |
670 | """ |
|
670 | """ | |
671 |
|
671 | |||
672 | Colors = self.Colors |
|
672 | Colors = self.Colors | |
673 | list = [] |
|
673 | list = [] | |
674 | for filename, lineno, name, line in extracted_list[:-1]: |
|
674 | for filename, lineno, name, line in extracted_list[:-1]: | |
675 | item = ' File %s"%s"%s, line %s%d%s, in %s%s%s\n' % \ |
|
675 | item = ' File %s"%s"%s, line %s%d%s, in %s%s%s\n' % \ | |
676 | (Colors.filename, py3compat.cast_unicode_py2(filename, "utf-8"), Colors.Normal, |
|
676 | (Colors.filename, py3compat.cast_unicode_py2(filename, "utf-8"), Colors.Normal, | |
677 | Colors.lineno, lineno, Colors.Normal, |
|
677 | Colors.lineno, lineno, Colors.Normal, | |
678 | Colors.name, py3compat.cast_unicode_py2(name, "utf-8"), Colors.Normal) |
|
678 | Colors.name, py3compat.cast_unicode_py2(name, "utf-8"), Colors.Normal) | |
679 | if line: |
|
679 | if line: | |
680 | item += ' %s\n' % line.strip() |
|
680 | item += ' %s\n' % line.strip() | |
681 | list.append(item) |
|
681 | list.append(item) | |
682 | # Emphasize the last entry |
|
682 | # Emphasize the last entry | |
683 | filename, lineno, name, line = extracted_list[-1] |
|
683 | filename, lineno, name, line = extracted_list[-1] | |
684 | item = '%s File %s"%s"%s, line %s%d%s, in %s%s%s%s\n' % \ |
|
684 | item = '%s File %s"%s"%s, line %s%d%s, in %s%s%s%s\n' % \ | |
685 | (Colors.normalEm, |
|
685 | (Colors.normalEm, | |
686 | Colors.filenameEm, py3compat.cast_unicode_py2(filename, "utf-8"), Colors.normalEm, |
|
686 | Colors.filenameEm, py3compat.cast_unicode_py2(filename, "utf-8"), Colors.normalEm, | |
687 | Colors.linenoEm, lineno, Colors.normalEm, |
|
687 | Colors.linenoEm, lineno, Colors.normalEm, | |
688 | Colors.nameEm, py3compat.cast_unicode_py2(name, "utf-8"), Colors.normalEm, |
|
688 | Colors.nameEm, py3compat.cast_unicode_py2(name, "utf-8"), Colors.normalEm, | |
689 | Colors.Normal) |
|
689 | Colors.Normal) | |
690 | if line: |
|
690 | if line: | |
691 | item += '%s %s%s\n' % (Colors.line, line.strip(), |
|
691 | item += '%s %s%s\n' % (Colors.line, line.strip(), | |
692 | Colors.Normal) |
|
692 | Colors.Normal) | |
693 | list.append(item) |
|
693 | list.append(item) | |
694 | return list |
|
694 | return list | |
695 |
|
695 | |||
696 | def _format_exception_only(self, etype, value): |
|
696 | def _format_exception_only(self, etype, value): | |
697 | """Format the exception part of a traceback. |
|
697 | """Format the exception part of a traceback. | |
698 |
|
698 | |||
699 | The arguments are the exception type and value such as given by |
|
699 | The arguments are the exception type and value such as given by | |
700 | sys.exc_info()[:2]. The return value is a list of strings, each ending |
|
700 | sys.exc_info()[:2]. The return value is a list of strings, each ending | |
701 | in a newline. Normally, the list contains a single string; however, |
|
701 | in a newline. Normally, the list contains a single string; however, | |
702 | for SyntaxError exceptions, it contains several lines that (when |
|
702 | for SyntaxError exceptions, it contains several lines that (when | |
703 | printed) display detailed information about where the syntax error |
|
703 | printed) display detailed information about where the syntax error | |
704 | occurred. The message indicating which exception occurred is the |
|
704 | occurred. The message indicating which exception occurred is the | |
705 | always last string in the list. |
|
705 | always last string in the list. | |
706 |
|
706 | |||
707 | Also lifted nearly verbatim from traceback.py |
|
707 | Also lifted nearly verbatim from traceback.py | |
708 | """ |
|
708 | """ | |
709 | have_filedata = False |
|
709 | have_filedata = False | |
710 | Colors = self.Colors |
|
710 | Colors = self.Colors | |
711 | list = [] |
|
711 | list = [] | |
712 | stype = py3compat.cast_unicode(Colors.excName + etype.__name__ + Colors.Normal) |
|
712 | stype = py3compat.cast_unicode(Colors.excName + etype.__name__ + Colors.Normal) | |
713 | if value is None: |
|
713 | if value is None: | |
714 | # Not sure if this can still happen in Python 2.6 and above |
|
714 | # Not sure if this can still happen in Python 2.6 and above | |
715 | list.append(stype + '\n') |
|
715 | list.append(stype + '\n') | |
716 | else: |
|
716 | else: | |
717 | if issubclass(etype, SyntaxError): |
|
717 | if issubclass(etype, SyntaxError): | |
718 | have_filedata = True |
|
718 | have_filedata = True | |
719 | if not value.filename: value.filename = "<string>" |
|
719 | if not value.filename: value.filename = "<string>" | |
720 | if value.lineno: |
|
720 | if value.lineno: | |
721 | lineno = value.lineno |
|
721 | lineno = value.lineno | |
722 | textline = ulinecache.getline(value.filename, value.lineno) |
|
722 | textline = ulinecache.getline(value.filename, value.lineno) | |
723 | else: |
|
723 | else: | |
724 | lineno = 'unknown' |
|
724 | lineno = 'unknown' | |
725 | textline = '' |
|
725 | textline = '' | |
726 | list.append('%s File %s"%s"%s, line %s%s%s\n' % \ |
|
726 | list.append('%s File %s"%s"%s, line %s%s%s\n' % \ | |
727 | (Colors.normalEm, |
|
727 | (Colors.normalEm, | |
728 | Colors.filenameEm, py3compat.cast_unicode(value.filename), Colors.normalEm, |
|
728 | Colors.filenameEm, py3compat.cast_unicode(value.filename), Colors.normalEm, | |
729 | Colors.linenoEm, lineno, Colors.Normal )) |
|
729 | Colors.linenoEm, lineno, Colors.Normal )) | |
730 | if textline == '': |
|
730 | if textline == '': | |
731 | textline = py3compat.cast_unicode(value.text, "utf-8") |
|
731 | textline = py3compat.cast_unicode(value.text, "utf-8") | |
732 |
|
732 | |||
733 | if textline is not None: |
|
733 | if textline is not None: | |
734 | i = 0 |
|
734 | i = 0 | |
735 | while i < len(textline) and textline[i].isspace(): |
|
735 | while i < len(textline) and textline[i].isspace(): | |
736 | i += 1 |
|
736 | i += 1 | |
737 | list.append('%s %s%s\n' % (Colors.line, |
|
737 | list.append('%s %s%s\n' % (Colors.line, | |
738 | textline.strip(), |
|
738 | textline.strip(), | |
739 | Colors.Normal)) |
|
739 | Colors.Normal)) | |
740 | if value.offset is not None: |
|
740 | if value.offset is not None: | |
741 | s = ' ' |
|
741 | s = ' ' | |
742 | for c in textline[i:value.offset - 1]: |
|
742 | for c in textline[i:value.offset - 1]: | |
743 | if c.isspace(): |
|
743 | if c.isspace(): | |
744 | s += c |
|
744 | s += c | |
745 | else: |
|
745 | else: | |
746 | s += ' ' |
|
746 | s += ' ' | |
747 | list.append('%s%s^%s\n' % (Colors.caret, s, |
|
747 | list.append('%s%s^%s\n' % (Colors.caret, s, | |
748 | Colors.Normal)) |
|
748 | Colors.Normal)) | |
749 |
|
749 | |||
750 | try: |
|
750 | try: | |
751 | s = value.msg |
|
751 | s = value.msg | |
752 | except Exception: |
|
752 | except Exception: | |
753 | s = self._some_str(value) |
|
753 | s = self._some_str(value) | |
754 | if s: |
|
754 | if s: | |
755 | list.append('%s%s:%s %s\n' % (stype, Colors.excName, |
|
755 | list.append('%s%s:%s %s\n' % (stype, Colors.excName, | |
756 | Colors.Normal, s)) |
|
756 | Colors.Normal, s)) | |
757 | else: |
|
757 | else: | |
758 | list.append('%s\n' % stype) |
|
758 | list.append('%s\n' % stype) | |
759 |
|
759 | |||
760 | # sync with user hooks |
|
760 | # sync with user hooks | |
761 | if have_filedata: |
|
761 | if have_filedata: | |
762 | ipinst = get_ipython() |
|
762 | ipinst = get_ipython() | |
763 | if ipinst is not None: |
|
763 | if ipinst is not None: | |
764 | ipinst.hooks.synchronize_with_editor(value.filename, value.lineno, 0) |
|
764 | ipinst.hooks.synchronize_with_editor(value.filename, value.lineno, 0) | |
765 |
|
765 | |||
766 | return list |
|
766 | return list | |
767 |
|
767 | |||
768 | def get_exception_only(self, etype, value): |
|
768 | def get_exception_only(self, etype, value): | |
769 | """Only print the exception type and message, without a traceback. |
|
769 | """Only print the exception type and message, without a traceback. | |
770 |
|
770 | |||
771 | Parameters |
|
771 | Parameters | |
772 | ---------- |
|
772 | ---------- | |
773 | etype : exception type |
|
773 | etype : exception type | |
774 | value : exception value |
|
774 | value : exception value | |
775 | """ |
|
775 | """ | |
776 | return ListTB.structured_traceback(self, etype, value, []) |
|
776 | return ListTB.structured_traceback(self, etype, value, []) | |
777 |
|
777 | |||
778 | def show_exception_only(self, etype, evalue): |
|
778 | def show_exception_only(self, etype, evalue): | |
779 | """Only print the exception type and message, without a traceback. |
|
779 | """Only print the exception type and message, without a traceback. | |
780 |
|
780 | |||
781 | Parameters |
|
781 | Parameters | |
782 | ---------- |
|
782 | ---------- | |
783 | etype : exception type |
|
783 | etype : exception type | |
784 | value : exception value |
|
784 | value : exception value | |
785 | """ |
|
785 | """ | |
786 | # This method needs to use __call__ from *this* class, not the one from |
|
786 | # This method needs to use __call__ from *this* class, not the one from | |
787 | # a subclass whose signature or behavior may be different |
|
787 | # a subclass whose signature or behavior may be different | |
788 | ostream = self.ostream |
|
788 | ostream = self.ostream | |
789 | ostream.flush() |
|
789 | ostream.flush() | |
790 | ostream.write('\n'.join(self.get_exception_only(etype, evalue))) |
|
790 | ostream.write('\n'.join(self.get_exception_only(etype, evalue))) | |
791 | ostream.flush() |
|
791 | ostream.flush() | |
792 |
|
792 | |||
793 | def _some_str(self, value): |
|
793 | def _some_str(self, value): | |
794 | # Lifted from traceback.py |
|
794 | # Lifted from traceback.py | |
795 | try: |
|
795 | try: | |
796 | return py3compat.cast_unicode(str(value)) |
|
796 | return py3compat.cast_unicode(str(value)) | |
797 | except: |
|
797 | except: | |
798 | return u'<unprintable %s object>' % type(value).__name__ |
|
798 | return u'<unprintable %s object>' % type(value).__name__ | |
799 |
|
799 | |||
800 |
|
800 | |||
801 | #---------------------------------------------------------------------------- |
|
801 | #---------------------------------------------------------------------------- | |
802 | class VerboseTB(TBTools): |
|
802 | class VerboseTB(TBTools): | |
803 | """A port of Ka-Ping Yee's cgitb.py module that outputs color text instead |
|
803 | """A port of Ka-Ping Yee's cgitb.py module that outputs color text instead | |
804 | of HTML. Requires inspect and pydoc. Crazy, man. |
|
804 | of HTML. Requires inspect and pydoc. Crazy, man. | |
805 |
|
805 | |||
806 | Modified version which optionally strips the topmost entries from the |
|
806 | Modified version which optionally strips the topmost entries from the | |
807 | traceback, to be used with alternate interpreters (because their own code |
|
807 | traceback, to be used with alternate interpreters (because their own code | |
808 | would appear in the traceback).""" |
|
808 | would appear in the traceback).""" | |
809 |
|
809 | |||
810 | def __init__(self, color_scheme='Linux', call_pdb=False, ostream=None, |
|
810 | def __init__(self, color_scheme='Linux', call_pdb=False, ostream=None, | |
811 | tb_offset=0, long_header=False, include_vars=True, |
|
811 | tb_offset=0, long_header=False, include_vars=True, | |
812 | check_cache=None): |
|
812 | check_cache=None, debugger_cls = None): | |
813 | """Specify traceback offset, headers and color scheme. |
|
813 | """Specify traceback offset, headers and color scheme. | |
814 |
|
814 | |||
815 | Define how many frames to drop from the tracebacks. Calling it with |
|
815 | Define how many frames to drop from the tracebacks. Calling it with | |
816 | tb_offset=1 allows use of this handler in interpreters which will have |
|
816 | tb_offset=1 allows use of this handler in interpreters which will have | |
817 | their own code at the top of the traceback (VerboseTB will first |
|
817 | their own code at the top of the traceback (VerboseTB will first | |
818 | remove that frame before printing the traceback info).""" |
|
818 | remove that frame before printing the traceback info).""" | |
819 | TBTools.__init__(self, color_scheme=color_scheme, call_pdb=call_pdb, |
|
819 | TBTools.__init__(self, color_scheme=color_scheme, call_pdb=call_pdb, | |
820 | ostream=ostream) |
|
820 | ostream=ostream) | |
821 | self.tb_offset = tb_offset |
|
821 | self.tb_offset = tb_offset | |
822 | self.long_header = long_header |
|
822 | self.long_header = long_header | |
823 | self.include_vars = include_vars |
|
823 | self.include_vars = include_vars | |
824 | # By default we use linecache.checkcache, but the user can provide a |
|
824 | # By default we use linecache.checkcache, but the user can provide a | |
825 | # different check_cache implementation. This is used by the IPython |
|
825 | # different check_cache implementation. This is used by the IPython | |
826 | # kernel to provide tracebacks for interactive code that is cached, |
|
826 | # kernel to provide tracebacks for interactive code that is cached, | |
827 | # by a compiler instance that flushes the linecache but preserves its |
|
827 | # by a compiler instance that flushes the linecache but preserves its | |
828 | # own code cache. |
|
828 | # own code cache. | |
829 | if check_cache is None: |
|
829 | if check_cache is None: | |
830 | check_cache = linecache.checkcache |
|
830 | check_cache = linecache.checkcache | |
831 | self.check_cache = check_cache |
|
831 | self.check_cache = check_cache | |
832 |
|
832 | |||
|
833 | self.debugger_cls = debugger_cls or debugger.Pdb | |||
|
834 | ||||
833 | def format_records(self, records, last_unique, recursion_repeat): |
|
835 | def format_records(self, records, last_unique, recursion_repeat): | |
834 | """Format the stack frames of the traceback""" |
|
836 | """Format the stack frames of the traceback""" | |
835 | frames = [] |
|
837 | frames = [] | |
836 | for r in records[:last_unique+recursion_repeat+1]: |
|
838 | for r in records[:last_unique+recursion_repeat+1]: | |
837 | #print '*** record:',file,lnum,func,lines,index # dbg |
|
839 | #print '*** record:',file,lnum,func,lines,index # dbg | |
838 | frames.append(self.format_record(*r)) |
|
840 | frames.append(self.format_record(*r)) | |
839 |
|
841 | |||
840 | if recursion_repeat: |
|
842 | if recursion_repeat: | |
841 | frames.append('... last %d frames repeated, from the frame below ...\n' % recursion_repeat) |
|
843 | frames.append('... last %d frames repeated, from the frame below ...\n' % recursion_repeat) | |
842 | frames.append(self.format_record(*records[last_unique+recursion_repeat+1])) |
|
844 | frames.append(self.format_record(*records[last_unique+recursion_repeat+1])) | |
843 |
|
845 | |||
844 | return frames |
|
846 | return frames | |
845 |
|
847 | |||
846 | def format_record(self, frame, file, lnum, func, lines, index): |
|
848 | def format_record(self, frame, file, lnum, func, lines, index): | |
847 | """Format a single stack frame""" |
|
849 | """Format a single stack frame""" | |
848 | Colors = self.Colors # just a shorthand + quicker name lookup |
|
850 | Colors = self.Colors # just a shorthand + quicker name lookup | |
849 | ColorsNormal = Colors.Normal # used a lot |
|
851 | ColorsNormal = Colors.Normal # used a lot | |
850 | col_scheme = self.color_scheme_table.active_scheme_name |
|
852 | col_scheme = self.color_scheme_table.active_scheme_name | |
851 | indent = ' ' * INDENT_SIZE |
|
853 | indent = ' ' * INDENT_SIZE | |
852 | em_normal = '%s\n%s%s' % (Colors.valEm, indent, ColorsNormal) |
|
854 | em_normal = '%s\n%s%s' % (Colors.valEm, indent, ColorsNormal) | |
853 | undefined = '%sundefined%s' % (Colors.em, ColorsNormal) |
|
855 | undefined = '%sundefined%s' % (Colors.em, ColorsNormal) | |
854 | tpl_link = '%s%%s%s' % (Colors.filenameEm, ColorsNormal) |
|
856 | tpl_link = '%s%%s%s' % (Colors.filenameEm, ColorsNormal) | |
855 | tpl_call = 'in %s%%s%s%%s%s' % (Colors.vName, Colors.valEm, |
|
857 | tpl_call = 'in %s%%s%s%%s%s' % (Colors.vName, Colors.valEm, | |
856 | ColorsNormal) |
|
858 | ColorsNormal) | |
857 | tpl_call_fail = 'in %s%%s%s(***failed resolving arguments***)%s' % \ |
|
859 | tpl_call_fail = 'in %s%%s%s(***failed resolving arguments***)%s' % \ | |
858 | (Colors.vName, Colors.valEm, ColorsNormal) |
|
860 | (Colors.vName, Colors.valEm, ColorsNormal) | |
859 | tpl_local_var = '%s%%s%s' % (Colors.vName, ColorsNormal) |
|
861 | tpl_local_var = '%s%%s%s' % (Colors.vName, ColorsNormal) | |
860 | tpl_global_var = '%sglobal%s %s%%s%s' % (Colors.em, ColorsNormal, |
|
862 | tpl_global_var = '%sglobal%s %s%%s%s' % (Colors.em, ColorsNormal, | |
861 | Colors.vName, ColorsNormal) |
|
863 | Colors.vName, ColorsNormal) | |
862 | tpl_name_val = '%%s %s= %%s%s' % (Colors.valEm, ColorsNormal) |
|
864 | tpl_name_val = '%%s %s= %%s%s' % (Colors.valEm, ColorsNormal) | |
863 |
|
865 | |||
864 | tpl_line = '%s%%s%s %%s' % (Colors.lineno, ColorsNormal) |
|
866 | tpl_line = '%s%%s%s %%s' % (Colors.lineno, ColorsNormal) | |
865 | tpl_line_em = '%s%%s%s %%s%s' % (Colors.linenoEm, Colors.line, |
|
867 | tpl_line_em = '%s%%s%s %%s%s' % (Colors.linenoEm, Colors.line, | |
866 | ColorsNormal) |
|
868 | ColorsNormal) | |
867 |
|
869 | |||
868 | abspath = os.path.abspath |
|
870 | abspath = os.path.abspath | |
869 |
|
871 | |||
870 |
|
872 | |||
871 | if not file: |
|
873 | if not file: | |
872 | file = '?' |
|
874 | file = '?' | |
873 | elif file.startswith(str("<")) and file.endswith(str(">")): |
|
875 | elif file.startswith(str("<")) and file.endswith(str(">")): | |
874 | # Not a real filename, no problem... |
|
876 | # Not a real filename, no problem... | |
875 | pass |
|
877 | pass | |
876 | elif not os.path.isabs(file): |
|
878 | elif not os.path.isabs(file): | |
877 | # Try to make the filename absolute by trying all |
|
879 | # Try to make the filename absolute by trying all | |
878 | # sys.path entries (which is also what linecache does) |
|
880 | # sys.path entries (which is also what linecache does) | |
879 | for dirname in sys.path: |
|
881 | for dirname in sys.path: | |
880 | try: |
|
882 | try: | |
881 | fullname = os.path.join(dirname, file) |
|
883 | fullname = os.path.join(dirname, file) | |
882 | if os.path.isfile(fullname): |
|
884 | if os.path.isfile(fullname): | |
883 | file = os.path.abspath(fullname) |
|
885 | file = os.path.abspath(fullname) | |
884 | break |
|
886 | break | |
885 | except Exception: |
|
887 | except Exception: | |
886 | # Just in case that sys.path contains very |
|
888 | # Just in case that sys.path contains very | |
887 | # strange entries... |
|
889 | # strange entries... | |
888 | pass |
|
890 | pass | |
889 |
|
891 | |||
890 | file = py3compat.cast_unicode(file, util_path.fs_encoding) |
|
892 | file = py3compat.cast_unicode(file, util_path.fs_encoding) | |
891 | link = tpl_link % file |
|
893 | link = tpl_link % file | |
892 | args, varargs, varkw, locals = fixed_getargvalues(frame) |
|
894 | args, varargs, varkw, locals = fixed_getargvalues(frame) | |
893 |
|
895 | |||
894 | if func == '?': |
|
896 | if func == '?': | |
895 | call = '' |
|
897 | call = '' | |
896 | else: |
|
898 | else: | |
897 | # Decide whether to include variable details or not |
|
899 | # Decide whether to include variable details or not | |
898 | var_repr = self.include_vars and eqrepr or nullrepr |
|
900 | var_repr = self.include_vars and eqrepr or nullrepr | |
899 | try: |
|
901 | try: | |
900 | call = tpl_call % (func, inspect.formatargvalues(args, |
|
902 | call = tpl_call % (func, inspect.formatargvalues(args, | |
901 | varargs, varkw, |
|
903 | varargs, varkw, | |
902 | locals, formatvalue=var_repr)) |
|
904 | locals, formatvalue=var_repr)) | |
903 | except KeyError: |
|
905 | except KeyError: | |
904 | # This happens in situations like errors inside generator |
|
906 | # This happens in situations like errors inside generator | |
905 | # expressions, where local variables are listed in the |
|
907 | # expressions, where local variables are listed in the | |
906 | # line, but can't be extracted from the frame. I'm not |
|
908 | # line, but can't be extracted from the frame. I'm not | |
907 | # 100% sure this isn't actually a bug in inspect itself, |
|
909 | # 100% sure this isn't actually a bug in inspect itself, | |
908 | # but since there's no info for us to compute with, the |
|
910 | # but since there's no info for us to compute with, the | |
909 | # best we can do is report the failure and move on. Here |
|
911 | # best we can do is report the failure and move on. Here | |
910 | # we must *not* call any traceback construction again, |
|
912 | # we must *not* call any traceback construction again, | |
911 | # because that would mess up use of %debug later on. So we |
|
913 | # because that would mess up use of %debug later on. So we | |
912 | # simply report the failure and move on. The only |
|
914 | # simply report the failure and move on. The only | |
913 | # limitation will be that this frame won't have locals |
|
915 | # limitation will be that this frame won't have locals | |
914 | # listed in the call signature. Quite subtle problem... |
|
916 | # listed in the call signature. Quite subtle problem... | |
915 | # I can't think of a good way to validate this in a unit |
|
917 | # I can't think of a good way to validate this in a unit | |
916 | # test, but running a script consisting of: |
|
918 | # test, but running a script consisting of: | |
917 | # dict( (k,v.strip()) for (k,v) in range(10) ) |
|
919 | # dict( (k,v.strip()) for (k,v) in range(10) ) | |
918 | # will illustrate the error, if this exception catch is |
|
920 | # will illustrate the error, if this exception catch is | |
919 | # disabled. |
|
921 | # disabled. | |
920 | call = tpl_call_fail % func |
|
922 | call = tpl_call_fail % func | |
921 |
|
923 | |||
922 | # Don't attempt to tokenize binary files. |
|
924 | # Don't attempt to tokenize binary files. | |
923 | if file.endswith(('.so', '.pyd', '.dll')): |
|
925 | if file.endswith(('.so', '.pyd', '.dll')): | |
924 | return '%s %s\n' % (link, call) |
|
926 | return '%s %s\n' % (link, call) | |
925 |
|
927 | |||
926 | elif file.endswith(('.pyc', '.pyo')): |
|
928 | elif file.endswith(('.pyc', '.pyo')): | |
927 | # Look up the corresponding source file. |
|
929 | # Look up the corresponding source file. | |
928 | try: |
|
930 | try: | |
929 | file = openpy.source_from_cache(file) |
|
931 | file = openpy.source_from_cache(file) | |
930 | except ValueError: |
|
932 | except ValueError: | |
931 | # Failed to get the source file for some reason |
|
933 | # Failed to get the source file for some reason | |
932 | # E.g. https://github.com/ipython/ipython/issues/9486 |
|
934 | # E.g. https://github.com/ipython/ipython/issues/9486 | |
933 | return '%s %s\n' % (link, call) |
|
935 | return '%s %s\n' % (link, call) | |
934 |
|
936 | |||
935 | def linereader(file=file, lnum=[lnum], getline=ulinecache.getline): |
|
937 | def linereader(file=file, lnum=[lnum], getline=ulinecache.getline): | |
936 | line = getline(file, lnum[0]) |
|
938 | line = getline(file, lnum[0]) | |
937 | lnum[0] += 1 |
|
939 | lnum[0] += 1 | |
938 | return line |
|
940 | return line | |
939 |
|
941 | |||
940 | # Build the list of names on this line of code where the exception |
|
942 | # Build the list of names on this line of code where the exception | |
941 | # occurred. |
|
943 | # occurred. | |
942 | try: |
|
944 | try: | |
943 | names = [] |
|
945 | names = [] | |
944 | name_cont = False |
|
946 | name_cont = False | |
945 |
|
947 | |||
946 | for token_type, token, start, end, line in generate_tokens(linereader): |
|
948 | for token_type, token, start, end, line in generate_tokens(linereader): | |
947 | # build composite names |
|
949 | # build composite names | |
948 | if token_type == tokenize.NAME and token not in keyword.kwlist: |
|
950 | if token_type == tokenize.NAME and token not in keyword.kwlist: | |
949 | if name_cont: |
|
951 | if name_cont: | |
950 | # Continuation of a dotted name |
|
952 | # Continuation of a dotted name | |
951 | try: |
|
953 | try: | |
952 | names[-1].append(token) |
|
954 | names[-1].append(token) | |
953 | except IndexError: |
|
955 | except IndexError: | |
954 | names.append([token]) |
|
956 | names.append([token]) | |
955 | name_cont = False |
|
957 | name_cont = False | |
956 | else: |
|
958 | else: | |
957 | # Regular new names. We append everything, the caller |
|
959 | # Regular new names. We append everything, the caller | |
958 | # will be responsible for pruning the list later. It's |
|
960 | # will be responsible for pruning the list later. It's | |
959 | # very tricky to try to prune as we go, b/c composite |
|
961 | # very tricky to try to prune as we go, b/c composite | |
960 | # names can fool us. The pruning at the end is easy |
|
962 | # names can fool us. The pruning at the end is easy | |
961 | # to do (or the caller can print a list with repeated |
|
963 | # to do (or the caller can print a list with repeated | |
962 | # names if so desired. |
|
964 | # names if so desired. | |
963 | names.append([token]) |
|
965 | names.append([token]) | |
964 | elif token == '.': |
|
966 | elif token == '.': | |
965 | name_cont = True |
|
967 | name_cont = True | |
966 | elif token_type == tokenize.NEWLINE: |
|
968 | elif token_type == tokenize.NEWLINE: | |
967 | break |
|
969 | break | |
968 |
|
970 | |||
969 | except (IndexError, UnicodeDecodeError, SyntaxError): |
|
971 | except (IndexError, UnicodeDecodeError, SyntaxError): | |
970 | # signals exit of tokenizer |
|
972 | # signals exit of tokenizer | |
971 | # SyntaxError can occur if the file is not actually Python |
|
973 | # SyntaxError can occur if the file is not actually Python | |
972 | # - see gh-6300 |
|
974 | # - see gh-6300 | |
973 | pass |
|
975 | pass | |
974 | except tokenize.TokenError as msg: |
|
976 | except tokenize.TokenError as msg: | |
975 | _m = ("An unexpected error occurred while tokenizing input\n" |
|
977 | _m = ("An unexpected error occurred while tokenizing input\n" | |
976 | "The following traceback may be corrupted or invalid\n" |
|
978 | "The following traceback may be corrupted or invalid\n" | |
977 | "The error message is: %s\n" % msg) |
|
979 | "The error message is: %s\n" % msg) | |
978 | error(_m) |
|
980 | error(_m) | |
979 |
|
981 | |||
980 | # Join composite names (e.g. "dict.fromkeys") |
|
982 | # Join composite names (e.g. "dict.fromkeys") | |
981 | names = ['.'.join(n) for n in names] |
|
983 | names = ['.'.join(n) for n in names] | |
982 | # prune names list of duplicates, but keep the right order |
|
984 | # prune names list of duplicates, but keep the right order | |
983 | unique_names = uniq_stable(names) |
|
985 | unique_names = uniq_stable(names) | |
984 |
|
986 | |||
985 | # Start loop over vars |
|
987 | # Start loop over vars | |
986 | lvals = [] |
|
988 | lvals = [] | |
987 | if self.include_vars: |
|
989 | if self.include_vars: | |
988 | for name_full in unique_names: |
|
990 | for name_full in unique_names: | |
989 | name_base = name_full.split('.', 1)[0] |
|
991 | name_base = name_full.split('.', 1)[0] | |
990 | if name_base in frame.f_code.co_varnames: |
|
992 | if name_base in frame.f_code.co_varnames: | |
991 | if name_base in locals: |
|
993 | if name_base in locals: | |
992 | try: |
|
994 | try: | |
993 | value = repr(eval(name_full, locals)) |
|
995 | value = repr(eval(name_full, locals)) | |
994 | except: |
|
996 | except: | |
995 | value = undefined |
|
997 | value = undefined | |
996 | else: |
|
998 | else: | |
997 | value = undefined |
|
999 | value = undefined | |
998 | name = tpl_local_var % name_full |
|
1000 | name = tpl_local_var % name_full | |
999 | else: |
|
1001 | else: | |
1000 | if name_base in frame.f_globals: |
|
1002 | if name_base in frame.f_globals: | |
1001 | try: |
|
1003 | try: | |
1002 | value = repr(eval(name_full, frame.f_globals)) |
|
1004 | value = repr(eval(name_full, frame.f_globals)) | |
1003 | except: |
|
1005 | except: | |
1004 | value = undefined |
|
1006 | value = undefined | |
1005 | else: |
|
1007 | else: | |
1006 | value = undefined |
|
1008 | value = undefined | |
1007 | name = tpl_global_var % name_full |
|
1009 | name = tpl_global_var % name_full | |
1008 | lvals.append(tpl_name_val % (name, value)) |
|
1010 | lvals.append(tpl_name_val % (name, value)) | |
1009 | if lvals: |
|
1011 | if lvals: | |
1010 | lvals = '%s%s' % (indent, em_normal.join(lvals)) |
|
1012 | lvals = '%s%s' % (indent, em_normal.join(lvals)) | |
1011 | else: |
|
1013 | else: | |
1012 | lvals = '' |
|
1014 | lvals = '' | |
1013 |
|
1015 | |||
1014 | level = '%s %s\n' % (link, call) |
|
1016 | level = '%s %s\n' % (link, call) | |
1015 |
|
1017 | |||
1016 | if index is None: |
|
1018 | if index is None: | |
1017 | return level |
|
1019 | return level | |
1018 | else: |
|
1020 | else: | |
1019 | return '%s%s' % (level, ''.join( |
|
1021 | return '%s%s' % (level, ''.join( | |
1020 | _format_traceback_lines(lnum, index, lines, Colors, lvals, |
|
1022 | _format_traceback_lines(lnum, index, lines, Colors, lvals, | |
1021 | col_scheme))) |
|
1023 | col_scheme))) | |
1022 |
|
1024 | |||
1023 | def prepare_chained_exception_message(self, cause): |
|
1025 | def prepare_chained_exception_message(self, cause): | |
1024 | direct_cause = "\nThe above exception was the direct cause of the following exception:\n" |
|
1026 | direct_cause = "\nThe above exception was the direct cause of the following exception:\n" | |
1025 | exception_during_handling = "\nDuring handling of the above exception, another exception occurred:\n" |
|
1027 | exception_during_handling = "\nDuring handling of the above exception, another exception occurred:\n" | |
1026 |
|
1028 | |||
1027 | if cause: |
|
1029 | if cause: | |
1028 | message = [[direct_cause]] |
|
1030 | message = [[direct_cause]] | |
1029 | else: |
|
1031 | else: | |
1030 | message = [[exception_during_handling]] |
|
1032 | message = [[exception_during_handling]] | |
1031 | return message |
|
1033 | return message | |
1032 |
|
1034 | |||
1033 | def prepare_header(self, etype, long_version=False): |
|
1035 | def prepare_header(self, etype, long_version=False): | |
1034 | colors = self.Colors # just a shorthand + quicker name lookup |
|
1036 | colors = self.Colors # just a shorthand + quicker name lookup | |
1035 | colorsnormal = colors.Normal # used a lot |
|
1037 | colorsnormal = colors.Normal # used a lot | |
1036 | exc = '%s%s%s' % (colors.excName, etype, colorsnormal) |
|
1038 | exc = '%s%s%s' % (colors.excName, etype, colorsnormal) | |
1037 | width = min(75, get_terminal_size()[0]) |
|
1039 | width = min(75, get_terminal_size()[0]) | |
1038 | if long_version: |
|
1040 | if long_version: | |
1039 | # Header with the exception type, python version, and date |
|
1041 | # Header with the exception type, python version, and date | |
1040 | pyver = 'Python ' + sys.version.split()[0] + ': ' + sys.executable |
|
1042 | pyver = 'Python ' + sys.version.split()[0] + ': ' + sys.executable | |
1041 | date = time.ctime(time.time()) |
|
1043 | date = time.ctime(time.time()) | |
1042 |
|
1044 | |||
1043 | head = '%s%s%s\n%s%s%s\n%s' % (colors.topline, '-' * width, colorsnormal, |
|
1045 | head = '%s%s%s\n%s%s%s\n%s' % (colors.topline, '-' * width, colorsnormal, | |
1044 | exc, ' ' * (width - len(str(etype)) - len(pyver)), |
|
1046 | exc, ' ' * (width - len(str(etype)) - len(pyver)), | |
1045 | pyver, date.rjust(width) ) |
|
1047 | pyver, date.rjust(width) ) | |
1046 | head += "\nA problem occurred executing Python code. Here is the sequence of function" \ |
|
1048 | head += "\nA problem occurred executing Python code. Here is the sequence of function" \ | |
1047 | "\ncalls leading up to the error, with the most recent (innermost) call last." |
|
1049 | "\ncalls leading up to the error, with the most recent (innermost) call last." | |
1048 | else: |
|
1050 | else: | |
1049 | # Simplified header |
|
1051 | # Simplified header | |
1050 | head = '%s%s' % (exc, 'Traceback (most recent call last)'. \ |
|
1052 | head = '%s%s' % (exc, 'Traceback (most recent call last)'. \ | |
1051 | rjust(width - len(str(etype))) ) |
|
1053 | rjust(width - len(str(etype))) ) | |
1052 |
|
1054 | |||
1053 | return head |
|
1055 | return head | |
1054 |
|
1056 | |||
1055 | def format_exception(self, etype, evalue): |
|
1057 | def format_exception(self, etype, evalue): | |
1056 | colors = self.Colors # just a shorthand + quicker name lookup |
|
1058 | colors = self.Colors # just a shorthand + quicker name lookup | |
1057 | colorsnormal = colors.Normal # used a lot |
|
1059 | colorsnormal = colors.Normal # used a lot | |
1058 | indent = ' ' * INDENT_SIZE |
|
1060 | indent = ' ' * INDENT_SIZE | |
1059 | # Get (safely) a string form of the exception info |
|
1061 | # Get (safely) a string form of the exception info | |
1060 | try: |
|
1062 | try: | |
1061 | etype_str, evalue_str = map(str, (etype, evalue)) |
|
1063 | etype_str, evalue_str = map(str, (etype, evalue)) | |
1062 | except: |
|
1064 | except: | |
1063 | # User exception is improperly defined. |
|
1065 | # User exception is improperly defined. | |
1064 | etype, evalue = str, sys.exc_info()[:2] |
|
1066 | etype, evalue = str, sys.exc_info()[:2] | |
1065 | etype_str, evalue_str = map(str, (etype, evalue)) |
|
1067 | etype_str, evalue_str = map(str, (etype, evalue)) | |
1066 | # ... and format it |
|
1068 | # ... and format it | |
1067 | exception = ['%s%s%s: %s' % (colors.excName, etype_str, |
|
1069 | exception = ['%s%s%s: %s' % (colors.excName, etype_str, | |
1068 | colorsnormal, py3compat.cast_unicode(evalue_str))] |
|
1070 | colorsnormal, py3compat.cast_unicode(evalue_str))] | |
1069 |
|
1071 | |||
1070 | if (not py3compat.PY3) and type(evalue) is types.InstanceType: |
|
1072 | if (not py3compat.PY3) and type(evalue) is types.InstanceType: | |
1071 | try: |
|
1073 | try: | |
1072 | names = [w for w in dir(evalue) if isinstance(w, py3compat.string_types)] |
|
1074 | names = [w for w in dir(evalue) if isinstance(w, py3compat.string_types)] | |
1073 | except: |
|
1075 | except: | |
1074 | # Every now and then, an object with funny internals blows up |
|
1076 | # Every now and then, an object with funny internals blows up | |
1075 | # when dir() is called on it. We do the best we can to report |
|
1077 | # when dir() is called on it. We do the best we can to report | |
1076 | # the problem and continue |
|
1078 | # the problem and continue | |
1077 | _m = '%sException reporting error (object with broken dir())%s:' |
|
1079 | _m = '%sException reporting error (object with broken dir())%s:' | |
1078 | exception.append(_m % (colors.excName, colorsnormal)) |
|
1080 | exception.append(_m % (colors.excName, colorsnormal)) | |
1079 | etype_str, evalue_str = map(str, sys.exc_info()[:2]) |
|
1081 | etype_str, evalue_str = map(str, sys.exc_info()[:2]) | |
1080 | exception.append('%s%s%s: %s' % (colors.excName, etype_str, |
|
1082 | exception.append('%s%s%s: %s' % (colors.excName, etype_str, | |
1081 | colorsnormal, py3compat.cast_unicode(evalue_str))) |
|
1083 | colorsnormal, py3compat.cast_unicode(evalue_str))) | |
1082 | names = [] |
|
1084 | names = [] | |
1083 | for name in names: |
|
1085 | for name in names: | |
1084 | value = text_repr(getattr(evalue, name)) |
|
1086 | value = text_repr(getattr(evalue, name)) | |
1085 | exception.append('\n%s%s = %s' % (indent, name, value)) |
|
1087 | exception.append('\n%s%s = %s' % (indent, name, value)) | |
1086 |
|
1088 | |||
1087 | return exception |
|
1089 | return exception | |
1088 |
|
1090 | |||
1089 | def format_exception_as_a_whole(self, etype, evalue, etb, number_of_lines_of_context, tb_offset): |
|
1091 | def format_exception_as_a_whole(self, etype, evalue, etb, number_of_lines_of_context, tb_offset): | |
1090 | """Formats the header, traceback and exception message for a single exception. |
|
1092 | """Formats the header, traceback and exception message for a single exception. | |
1091 |
|
1093 | |||
1092 | This may be called multiple times by Python 3 exception chaining |
|
1094 | This may be called multiple times by Python 3 exception chaining | |
1093 | (PEP 3134). |
|
1095 | (PEP 3134). | |
1094 | """ |
|
1096 | """ | |
1095 | # some locals |
|
1097 | # some locals | |
1096 | orig_etype = etype |
|
1098 | orig_etype = etype | |
1097 | try: |
|
1099 | try: | |
1098 | etype = etype.__name__ |
|
1100 | etype = etype.__name__ | |
1099 | except AttributeError: |
|
1101 | except AttributeError: | |
1100 | pass |
|
1102 | pass | |
1101 |
|
1103 | |||
1102 | tb_offset = self.tb_offset if tb_offset is None else tb_offset |
|
1104 | tb_offset = self.tb_offset if tb_offset is None else tb_offset | |
1103 | head = self.prepare_header(etype, self.long_header) |
|
1105 | head = self.prepare_header(etype, self.long_header) | |
1104 | records = self.get_records(etb, number_of_lines_of_context, tb_offset) |
|
1106 | records = self.get_records(etb, number_of_lines_of_context, tb_offset) | |
1105 |
|
1107 | |||
1106 | if records is None: |
|
1108 | if records is None: | |
1107 | return "" |
|
1109 | return "" | |
1108 |
|
1110 | |||
1109 | last_unique, recursion_repeat = find_recursion(orig_etype, evalue, records) |
|
1111 | last_unique, recursion_repeat = find_recursion(orig_etype, evalue, records) | |
1110 |
|
1112 | |||
1111 | frames = self.format_records(records, last_unique, recursion_repeat) |
|
1113 | frames = self.format_records(records, last_unique, recursion_repeat) | |
1112 |
|
1114 | |||
1113 | formatted_exception = self.format_exception(etype, evalue) |
|
1115 | formatted_exception = self.format_exception(etype, evalue) | |
1114 | if records: |
|
1116 | if records: | |
1115 | filepath, lnum = records[-1][1:3] |
|
1117 | filepath, lnum = records[-1][1:3] | |
1116 | filepath = os.path.abspath(filepath) |
|
1118 | filepath = os.path.abspath(filepath) | |
1117 | ipinst = get_ipython() |
|
1119 | ipinst = get_ipython() | |
1118 | if ipinst is not None: |
|
1120 | if ipinst is not None: | |
1119 | ipinst.hooks.synchronize_with_editor(filepath, lnum, 0) |
|
1121 | ipinst.hooks.synchronize_with_editor(filepath, lnum, 0) | |
1120 |
|
1122 | |||
1121 | return [[head] + frames + [''.join(formatted_exception[0])]] |
|
1123 | return [[head] + frames + [''.join(formatted_exception[0])]] | |
1122 |
|
1124 | |||
1123 | def get_records(self, etb, number_of_lines_of_context, tb_offset): |
|
1125 | def get_records(self, etb, number_of_lines_of_context, tb_offset): | |
1124 | try: |
|
1126 | try: | |
1125 | # Try the default getinnerframes and Alex's: Alex's fixes some |
|
1127 | # Try the default getinnerframes and Alex's: Alex's fixes some | |
1126 | # problems, but it generates empty tracebacks for console errors |
|
1128 | # problems, but it generates empty tracebacks for console errors | |
1127 | # (5 blanks lines) where none should be returned. |
|
1129 | # (5 blanks lines) where none should be returned. | |
1128 | return _fixed_getinnerframes(etb, number_of_lines_of_context, tb_offset) |
|
1130 | return _fixed_getinnerframes(etb, number_of_lines_of_context, tb_offset) | |
1129 | except: |
|
1131 | except: | |
1130 | # FIXME: I've been getting many crash reports from python 2.3 |
|
1132 | # FIXME: I've been getting many crash reports from python 2.3 | |
1131 | # users, traceable to inspect.py. If I can find a small test-case |
|
1133 | # users, traceable to inspect.py. If I can find a small test-case | |
1132 | # to reproduce this, I should either write a better workaround or |
|
1134 | # to reproduce this, I should either write a better workaround or | |
1133 | # file a bug report against inspect (if that's the real problem). |
|
1135 | # file a bug report against inspect (if that's the real problem). | |
1134 | # So far, I haven't been able to find an isolated example to |
|
1136 | # So far, I haven't been able to find an isolated example to | |
1135 | # reproduce the problem. |
|
1137 | # reproduce the problem. | |
1136 | inspect_error() |
|
1138 | inspect_error() | |
1137 | traceback.print_exc(file=self.ostream) |
|
1139 | traceback.print_exc(file=self.ostream) | |
1138 | info('\nUnfortunately, your original traceback can not be constructed.\n') |
|
1140 | info('\nUnfortunately, your original traceback can not be constructed.\n') | |
1139 | return None |
|
1141 | return None | |
1140 |
|
1142 | |||
1141 | def get_parts_of_chained_exception(self, evalue): |
|
1143 | def get_parts_of_chained_exception(self, evalue): | |
1142 | def get_chained_exception(exception_value): |
|
1144 | def get_chained_exception(exception_value): | |
1143 | cause = getattr(exception_value, '__cause__', None) |
|
1145 | cause = getattr(exception_value, '__cause__', None) | |
1144 | if cause: |
|
1146 | if cause: | |
1145 | return cause |
|
1147 | return cause | |
1146 | if getattr(exception_value, '__suppress_context__', False): |
|
1148 | if getattr(exception_value, '__suppress_context__', False): | |
1147 | return None |
|
1149 | return None | |
1148 | return getattr(exception_value, '__context__', None) |
|
1150 | return getattr(exception_value, '__context__', None) | |
1149 |
|
1151 | |||
1150 | chained_evalue = get_chained_exception(evalue) |
|
1152 | chained_evalue = get_chained_exception(evalue) | |
1151 |
|
1153 | |||
1152 | if chained_evalue: |
|
1154 | if chained_evalue: | |
1153 | return chained_evalue.__class__, chained_evalue, chained_evalue.__traceback__ |
|
1155 | return chained_evalue.__class__, chained_evalue, chained_evalue.__traceback__ | |
1154 |
|
1156 | |||
1155 | def structured_traceback(self, etype, evalue, etb, tb_offset=None, |
|
1157 | def structured_traceback(self, etype, evalue, etb, tb_offset=None, | |
1156 | number_of_lines_of_context=5): |
|
1158 | number_of_lines_of_context=5): | |
1157 | """Return a nice text document describing the traceback.""" |
|
1159 | """Return a nice text document describing the traceback.""" | |
1158 |
|
1160 | |||
1159 | formatted_exception = self.format_exception_as_a_whole(etype, evalue, etb, number_of_lines_of_context, |
|
1161 | formatted_exception = self.format_exception_as_a_whole(etype, evalue, etb, number_of_lines_of_context, | |
1160 | tb_offset) |
|
1162 | tb_offset) | |
1161 |
|
1163 | |||
1162 | colors = self.Colors # just a shorthand + quicker name lookup |
|
1164 | colors = self.Colors # just a shorthand + quicker name lookup | |
1163 | colorsnormal = colors.Normal # used a lot |
|
1165 | colorsnormal = colors.Normal # used a lot | |
1164 | head = '%s%s%s' % (colors.topline, '-' * min(75, get_terminal_size()[0]), colorsnormal) |
|
1166 | head = '%s%s%s' % (colors.topline, '-' * min(75, get_terminal_size()[0]), colorsnormal) | |
1165 | structured_traceback_parts = [head] |
|
1167 | structured_traceback_parts = [head] | |
1166 | if py3compat.PY3: |
|
1168 | if py3compat.PY3: | |
1167 | chained_exceptions_tb_offset = 0 |
|
1169 | chained_exceptions_tb_offset = 0 | |
1168 | lines_of_context = 3 |
|
1170 | lines_of_context = 3 | |
1169 | formatted_exceptions = formatted_exception |
|
1171 | formatted_exceptions = formatted_exception | |
1170 | exception = self.get_parts_of_chained_exception(evalue) |
|
1172 | exception = self.get_parts_of_chained_exception(evalue) | |
1171 | if exception: |
|
1173 | if exception: | |
1172 | formatted_exceptions += self.prepare_chained_exception_message(evalue.__cause__) |
|
1174 | formatted_exceptions += self.prepare_chained_exception_message(evalue.__cause__) | |
1173 | etype, evalue, etb = exception |
|
1175 | etype, evalue, etb = exception | |
1174 | else: |
|
1176 | else: | |
1175 | evalue = None |
|
1177 | evalue = None | |
1176 | chained_exc_ids = set() |
|
1178 | chained_exc_ids = set() | |
1177 | while evalue: |
|
1179 | while evalue: | |
1178 | formatted_exceptions += self.format_exception_as_a_whole(etype, evalue, etb, lines_of_context, |
|
1180 | formatted_exceptions += self.format_exception_as_a_whole(etype, evalue, etb, lines_of_context, | |
1179 | chained_exceptions_tb_offset) |
|
1181 | chained_exceptions_tb_offset) | |
1180 | exception = self.get_parts_of_chained_exception(evalue) |
|
1182 | exception = self.get_parts_of_chained_exception(evalue) | |
1181 |
|
1183 | |||
1182 | if exception and not id(exception[1]) in chained_exc_ids: |
|
1184 | if exception and not id(exception[1]) in chained_exc_ids: | |
1183 | chained_exc_ids.add(id(exception[1])) # trace exception to avoid infinite 'cause' loop |
|
1185 | chained_exc_ids.add(id(exception[1])) # trace exception to avoid infinite 'cause' loop | |
1184 | formatted_exceptions += self.prepare_chained_exception_message(evalue.__cause__) |
|
1186 | formatted_exceptions += self.prepare_chained_exception_message(evalue.__cause__) | |
1185 | etype, evalue, etb = exception |
|
1187 | etype, evalue, etb = exception | |
1186 | else: |
|
1188 | else: | |
1187 | evalue = None |
|
1189 | evalue = None | |
1188 |
|
1190 | |||
1189 | # we want to see exceptions in a reversed order: |
|
1191 | # we want to see exceptions in a reversed order: | |
1190 | # the first exception should be on top |
|
1192 | # the first exception should be on top | |
1191 | for formatted_exception in reversed(formatted_exceptions): |
|
1193 | for formatted_exception in reversed(formatted_exceptions): | |
1192 | structured_traceback_parts += formatted_exception |
|
1194 | structured_traceback_parts += formatted_exception | |
1193 | else: |
|
1195 | else: | |
1194 | structured_traceback_parts += formatted_exception[0] |
|
1196 | structured_traceback_parts += formatted_exception[0] | |
1195 |
|
1197 | |||
1196 | return structured_traceback_parts |
|
1198 | return structured_traceback_parts | |
1197 |
|
1199 | |||
1198 | def debugger(self, force=False): |
|
1200 | def debugger(self, force=False): | |
1199 | """Call up the pdb debugger if desired, always clean up the tb |
|
1201 | """Call up the pdb debugger if desired, always clean up the tb | |
1200 | reference. |
|
1202 | reference. | |
1201 |
|
1203 | |||
1202 | Keywords: |
|
1204 | Keywords: | |
1203 |
|
1205 | |||
1204 | - force(False): by default, this routine checks the instance call_pdb |
|
1206 | - force(False): by default, this routine checks the instance call_pdb | |
1205 | flag and does not actually invoke the debugger if the flag is false. |
|
1207 | flag and does not actually invoke the debugger if the flag is false. | |
1206 | The 'force' option forces the debugger to activate even if the flag |
|
1208 | The 'force' option forces the debugger to activate even if the flag | |
1207 | is false. |
|
1209 | is false. | |
1208 |
|
1210 | |||
1209 | If the call_pdb flag is set, the pdb interactive debugger is |
|
1211 | If the call_pdb flag is set, the pdb interactive debugger is | |
1210 | invoked. In all cases, the self.tb reference to the current traceback |
|
1212 | invoked. In all cases, the self.tb reference to the current traceback | |
1211 | is deleted to prevent lingering references which hamper memory |
|
1213 | is deleted to prevent lingering references which hamper memory | |
1212 | management. |
|
1214 | management. | |
1213 |
|
1215 | |||
1214 | Note that each call to pdb() does an 'import readline', so if your app |
|
1216 | Note that each call to pdb() does an 'import readline', so if your app | |
1215 | requires a special setup for the readline completers, you'll have to |
|
1217 | requires a special setup for the readline completers, you'll have to | |
1216 | fix that by hand after invoking the exception handler.""" |
|
1218 | fix that by hand after invoking the exception handler.""" | |
1217 |
|
1219 | |||
1218 | if force or self.call_pdb: |
|
1220 | if force or self.call_pdb: | |
1219 | if self.pdb is None: |
|
1221 | if self.pdb is None: | |
1220 |
self.pdb = |
|
1222 | self.pdb = self.debugger_cls( | |
1221 | self.color_scheme_table.active_scheme_name) |
|
1223 | self.color_scheme_table.active_scheme_name) | |
1222 | # the system displayhook may have changed, restore the original |
|
1224 | # the system displayhook may have changed, restore the original | |
1223 | # for pdb |
|
1225 | # for pdb | |
1224 | display_trap = DisplayTrap(hook=sys.__displayhook__) |
|
1226 | display_trap = DisplayTrap(hook=sys.__displayhook__) | |
1225 | with display_trap: |
|
1227 | with display_trap: | |
1226 | self.pdb.reset() |
|
1228 | self.pdb.reset() | |
1227 | # Find the right frame so we don't pop up inside ipython itself |
|
1229 | # Find the right frame so we don't pop up inside ipython itself | |
1228 | if hasattr(self, 'tb') and self.tb is not None: |
|
1230 | if hasattr(self, 'tb') and self.tb is not None: | |
1229 | etb = self.tb |
|
1231 | etb = self.tb | |
1230 | else: |
|
1232 | else: | |
1231 | etb = self.tb = sys.last_traceback |
|
1233 | etb = self.tb = sys.last_traceback | |
1232 | while self.tb is not None and self.tb.tb_next is not None: |
|
1234 | while self.tb is not None and self.tb.tb_next is not None: | |
1233 | self.tb = self.tb.tb_next |
|
1235 | self.tb = self.tb.tb_next | |
1234 | if etb and etb.tb_next: |
|
1236 | if etb and etb.tb_next: | |
1235 | etb = etb.tb_next |
|
1237 | etb = etb.tb_next | |
1236 | self.pdb.botframe = etb.tb_frame |
|
1238 | self.pdb.botframe = etb.tb_frame | |
1237 | self.pdb.interaction(self.tb.tb_frame, self.tb) |
|
1239 | self.pdb.interaction(self.tb.tb_frame, self.tb) | |
1238 |
|
1240 | |||
1239 | if hasattr(self, 'tb'): |
|
1241 | if hasattr(self, 'tb'): | |
1240 | del self.tb |
|
1242 | del self.tb | |
1241 |
|
1243 | |||
1242 | def handler(self, info=None): |
|
1244 | def handler(self, info=None): | |
1243 | (etype, evalue, etb) = info or sys.exc_info() |
|
1245 | (etype, evalue, etb) = info or sys.exc_info() | |
1244 | self.tb = etb |
|
1246 | self.tb = etb | |
1245 | ostream = self.ostream |
|
1247 | ostream = self.ostream | |
1246 | ostream.flush() |
|
1248 | ostream.flush() | |
1247 | ostream.write(self.text(etype, evalue, etb)) |
|
1249 | ostream.write(self.text(etype, evalue, etb)) | |
1248 | ostream.write('\n') |
|
1250 | ostream.write('\n') | |
1249 | ostream.flush() |
|
1251 | ostream.flush() | |
1250 |
|
1252 | |||
1251 | # Changed so an instance can just be called as VerboseTB_inst() and print |
|
1253 | # Changed so an instance can just be called as VerboseTB_inst() and print | |
1252 | # out the right info on its own. |
|
1254 | # out the right info on its own. | |
1253 | def __call__(self, etype=None, evalue=None, etb=None): |
|
1255 | def __call__(self, etype=None, evalue=None, etb=None): | |
1254 | """This hook can replace sys.excepthook (for Python 2.1 or higher).""" |
|
1256 | """This hook can replace sys.excepthook (for Python 2.1 or higher).""" | |
1255 | if etb is None: |
|
1257 | if etb is None: | |
1256 | self.handler() |
|
1258 | self.handler() | |
1257 | else: |
|
1259 | else: | |
1258 | self.handler((etype, evalue, etb)) |
|
1260 | self.handler((etype, evalue, etb)) | |
1259 | try: |
|
1261 | try: | |
1260 | self.debugger() |
|
1262 | self.debugger() | |
1261 | except KeyboardInterrupt: |
|
1263 | except KeyboardInterrupt: | |
1262 | print("\nKeyboardInterrupt") |
|
1264 | print("\nKeyboardInterrupt") | |
1263 |
|
1265 | |||
1264 |
|
1266 | |||
1265 | #---------------------------------------------------------------------------- |
|
1267 | #---------------------------------------------------------------------------- | |
1266 | class FormattedTB(VerboseTB, ListTB): |
|
1268 | class FormattedTB(VerboseTB, ListTB): | |
1267 | """Subclass ListTB but allow calling with a traceback. |
|
1269 | """Subclass ListTB but allow calling with a traceback. | |
1268 |
|
1270 | |||
1269 | It can thus be used as a sys.excepthook for Python > 2.1. |
|
1271 | It can thus be used as a sys.excepthook for Python > 2.1. | |
1270 |
|
1272 | |||
1271 | Also adds 'Context' and 'Verbose' modes, not available in ListTB. |
|
1273 | Also adds 'Context' and 'Verbose' modes, not available in ListTB. | |
1272 |
|
1274 | |||
1273 | Allows a tb_offset to be specified. This is useful for situations where |
|
1275 | Allows a tb_offset to be specified. This is useful for situations where | |
1274 | one needs to remove a number of topmost frames from the traceback (such as |
|
1276 | one needs to remove a number of topmost frames from the traceback (such as | |
1275 | occurs with python programs that themselves execute other python code, |
|
1277 | occurs with python programs that themselves execute other python code, | |
1276 | like Python shells). """ |
|
1278 | like Python shells). """ | |
1277 |
|
1279 | |||
1278 | def __init__(self, mode='Plain', color_scheme='Linux', call_pdb=False, |
|
1280 | def __init__(self, mode='Plain', color_scheme='Linux', call_pdb=False, | |
1279 | ostream=None, |
|
1281 | ostream=None, | |
1280 | tb_offset=0, long_header=False, include_vars=False, |
|
1282 | tb_offset=0, long_header=False, include_vars=False, | |
1281 | check_cache=None): |
|
1283 | check_cache=None, debugger_cls=None): | |
1282 |
|
1284 | |||
1283 | # NEVER change the order of this list. Put new modes at the end: |
|
1285 | # NEVER change the order of this list. Put new modes at the end: | |
1284 | self.valid_modes = ['Plain', 'Context', 'Verbose'] |
|
1286 | self.valid_modes = ['Plain', 'Context', 'Verbose'] | |
1285 | self.verbose_modes = self.valid_modes[1:3] |
|
1287 | self.verbose_modes = self.valid_modes[1:3] | |
1286 |
|
1288 | |||
1287 | VerboseTB.__init__(self, color_scheme=color_scheme, call_pdb=call_pdb, |
|
1289 | VerboseTB.__init__(self, color_scheme=color_scheme, call_pdb=call_pdb, | |
1288 | ostream=ostream, tb_offset=tb_offset, |
|
1290 | ostream=ostream, tb_offset=tb_offset, | |
1289 | long_header=long_header, include_vars=include_vars, |
|
1291 | long_header=long_header, include_vars=include_vars, | |
1290 | check_cache=check_cache) |
|
1292 | check_cache=check_cache, debugger_cls=debugger_cls) | |
1291 |
|
1293 | |||
1292 | # Different types of tracebacks are joined with different separators to |
|
1294 | # Different types of tracebacks are joined with different separators to | |
1293 | # form a single string. They are taken from this dict |
|
1295 | # form a single string. They are taken from this dict | |
1294 | self._join_chars = dict(Plain='', Context='\n', Verbose='\n') |
|
1296 | self._join_chars = dict(Plain='', Context='\n', Verbose='\n') | |
1295 | # set_mode also sets the tb_join_char attribute |
|
1297 | # set_mode also sets the tb_join_char attribute | |
1296 | self.set_mode(mode) |
|
1298 | self.set_mode(mode) | |
1297 |
|
1299 | |||
1298 | def _extract_tb(self, tb): |
|
1300 | def _extract_tb(self, tb): | |
1299 | if tb: |
|
1301 | if tb: | |
1300 | return traceback.extract_tb(tb) |
|
1302 | return traceback.extract_tb(tb) | |
1301 | else: |
|
1303 | else: | |
1302 | return None |
|
1304 | return None | |
1303 |
|
1305 | |||
1304 | def structured_traceback(self, etype, value, tb, tb_offset=None, number_of_lines_of_context=5): |
|
1306 | def structured_traceback(self, etype, value, tb, tb_offset=None, number_of_lines_of_context=5): | |
1305 | tb_offset = self.tb_offset if tb_offset is None else tb_offset |
|
1307 | tb_offset = self.tb_offset if tb_offset is None else tb_offset | |
1306 | mode = self.mode |
|
1308 | mode = self.mode | |
1307 | if mode in self.verbose_modes: |
|
1309 | if mode in self.verbose_modes: | |
1308 | # Verbose modes need a full traceback |
|
1310 | # Verbose modes need a full traceback | |
1309 | return VerboseTB.structured_traceback( |
|
1311 | return VerboseTB.structured_traceback( | |
1310 | self, etype, value, tb, tb_offset, number_of_lines_of_context |
|
1312 | self, etype, value, tb, tb_offset, number_of_lines_of_context | |
1311 | ) |
|
1313 | ) | |
1312 | else: |
|
1314 | else: | |
1313 | # We must check the source cache because otherwise we can print |
|
1315 | # We must check the source cache because otherwise we can print | |
1314 | # out-of-date source code. |
|
1316 | # out-of-date source code. | |
1315 | self.check_cache() |
|
1317 | self.check_cache() | |
1316 | # Now we can extract and format the exception |
|
1318 | # Now we can extract and format the exception | |
1317 | elist = self._extract_tb(tb) |
|
1319 | elist = self._extract_tb(tb) | |
1318 | return ListTB.structured_traceback( |
|
1320 | return ListTB.structured_traceback( | |
1319 | self, etype, value, elist, tb_offset, number_of_lines_of_context |
|
1321 | self, etype, value, elist, tb_offset, number_of_lines_of_context | |
1320 | ) |
|
1322 | ) | |
1321 |
|
1323 | |||
1322 | def stb2text(self, stb): |
|
1324 | def stb2text(self, stb): | |
1323 | """Convert a structured traceback (a list) to a string.""" |
|
1325 | """Convert a structured traceback (a list) to a string.""" | |
1324 | return self.tb_join_char.join(stb) |
|
1326 | return self.tb_join_char.join(stb) | |
1325 |
|
1327 | |||
1326 |
|
1328 | |||
1327 | def set_mode(self, mode=None): |
|
1329 | def set_mode(self, mode=None): | |
1328 | """Switch to the desired mode. |
|
1330 | """Switch to the desired mode. | |
1329 |
|
1331 | |||
1330 | If mode is not specified, cycles through the available modes.""" |
|
1332 | If mode is not specified, cycles through the available modes.""" | |
1331 |
|
1333 | |||
1332 | if not mode: |
|
1334 | if not mode: | |
1333 | new_idx = (self.valid_modes.index(self.mode) + 1 ) % \ |
|
1335 | new_idx = (self.valid_modes.index(self.mode) + 1 ) % \ | |
1334 | len(self.valid_modes) |
|
1336 | len(self.valid_modes) | |
1335 | self.mode = self.valid_modes[new_idx] |
|
1337 | self.mode = self.valid_modes[new_idx] | |
1336 | elif mode not in self.valid_modes: |
|
1338 | elif mode not in self.valid_modes: | |
1337 | raise ValueError('Unrecognized mode in FormattedTB: <' + mode + '>\n' |
|
1339 | raise ValueError('Unrecognized mode in FormattedTB: <' + mode + '>\n' | |
1338 | 'Valid modes: ' + str(self.valid_modes)) |
|
1340 | 'Valid modes: ' + str(self.valid_modes)) | |
1339 | else: |
|
1341 | else: | |
1340 | self.mode = mode |
|
1342 | self.mode = mode | |
1341 | # include variable details only in 'Verbose' mode |
|
1343 | # include variable details only in 'Verbose' mode | |
1342 | self.include_vars = (self.mode == self.valid_modes[2]) |
|
1344 | self.include_vars = (self.mode == self.valid_modes[2]) | |
1343 | # Set the join character for generating text tracebacks |
|
1345 | # Set the join character for generating text tracebacks | |
1344 | self.tb_join_char = self._join_chars[self.mode] |
|
1346 | self.tb_join_char = self._join_chars[self.mode] | |
1345 |
|
1347 | |||
1346 | # some convenient shortcuts |
|
1348 | # some convenient shortcuts | |
1347 | def plain(self): |
|
1349 | def plain(self): | |
1348 | self.set_mode(self.valid_modes[0]) |
|
1350 | self.set_mode(self.valid_modes[0]) | |
1349 |
|
1351 | |||
1350 | def context(self): |
|
1352 | def context(self): | |
1351 | self.set_mode(self.valid_modes[1]) |
|
1353 | self.set_mode(self.valid_modes[1]) | |
1352 |
|
1354 | |||
1353 | def verbose(self): |
|
1355 | def verbose(self): | |
1354 | self.set_mode(self.valid_modes[2]) |
|
1356 | self.set_mode(self.valid_modes[2]) | |
1355 |
|
1357 | |||
1356 |
|
1358 | |||
1357 | #---------------------------------------------------------------------------- |
|
1359 | #---------------------------------------------------------------------------- | |
1358 | class AutoFormattedTB(FormattedTB): |
|
1360 | class AutoFormattedTB(FormattedTB): | |
1359 | """A traceback printer which can be called on the fly. |
|
1361 | """A traceback printer which can be called on the fly. | |
1360 |
|
1362 | |||
1361 | It will find out about exceptions by itself. |
|
1363 | It will find out about exceptions by itself. | |
1362 |
|
1364 | |||
1363 | A brief example:: |
|
1365 | A brief example:: | |
1364 |
|
1366 | |||
1365 | AutoTB = AutoFormattedTB(mode = 'Verbose',color_scheme='Linux') |
|
1367 | AutoTB = AutoFormattedTB(mode = 'Verbose',color_scheme='Linux') | |
1366 | try: |
|
1368 | try: | |
1367 | ... |
|
1369 | ... | |
1368 | except: |
|
1370 | except: | |
1369 | AutoTB() # or AutoTB(out=logfile) where logfile is an open file object |
|
1371 | AutoTB() # or AutoTB(out=logfile) where logfile is an open file object | |
1370 | """ |
|
1372 | """ | |
1371 |
|
1373 | |||
1372 | def __call__(self, etype=None, evalue=None, etb=None, |
|
1374 | def __call__(self, etype=None, evalue=None, etb=None, | |
1373 | out=None, tb_offset=None): |
|
1375 | out=None, tb_offset=None): | |
1374 | """Print out a formatted exception traceback. |
|
1376 | """Print out a formatted exception traceback. | |
1375 |
|
1377 | |||
1376 | Optional arguments: |
|
1378 | Optional arguments: | |
1377 | - out: an open file-like object to direct output to. |
|
1379 | - out: an open file-like object to direct output to. | |
1378 |
|
1380 | |||
1379 | - tb_offset: the number of frames to skip over in the stack, on a |
|
1381 | - tb_offset: the number of frames to skip over in the stack, on a | |
1380 | per-call basis (this overrides temporarily the instance's tb_offset |
|
1382 | per-call basis (this overrides temporarily the instance's tb_offset | |
1381 | given at initialization time. """ |
|
1383 | given at initialization time. """ | |
1382 |
|
1384 | |||
1383 | if out is None: |
|
1385 | if out is None: | |
1384 | out = self.ostream |
|
1386 | out = self.ostream | |
1385 | out.flush() |
|
1387 | out.flush() | |
1386 | out.write(self.text(etype, evalue, etb, tb_offset)) |
|
1388 | out.write(self.text(etype, evalue, etb, tb_offset)) | |
1387 | out.write('\n') |
|
1389 | out.write('\n') | |
1388 | out.flush() |
|
1390 | out.flush() | |
1389 | # FIXME: we should remove the auto pdb behavior from here and leave |
|
1391 | # FIXME: we should remove the auto pdb behavior from here and leave | |
1390 | # that to the clients. |
|
1392 | # that to the clients. | |
1391 | try: |
|
1393 | try: | |
1392 | self.debugger() |
|
1394 | self.debugger() | |
1393 | except KeyboardInterrupt: |
|
1395 | except KeyboardInterrupt: | |
1394 | print("\nKeyboardInterrupt") |
|
1396 | print("\nKeyboardInterrupt") | |
1395 |
|
1397 | |||
1396 | def structured_traceback(self, etype=None, value=None, tb=None, |
|
1398 | def structured_traceback(self, etype=None, value=None, tb=None, | |
1397 | tb_offset=None, number_of_lines_of_context=5): |
|
1399 | tb_offset=None, number_of_lines_of_context=5): | |
1398 | if etype is None: |
|
1400 | if etype is None: | |
1399 | etype, value, tb = sys.exc_info() |
|
1401 | etype, value, tb = sys.exc_info() | |
1400 | self.tb = tb |
|
1402 | self.tb = tb | |
1401 | return FormattedTB.structured_traceback( |
|
1403 | return FormattedTB.structured_traceback( | |
1402 | self, etype, value, tb, tb_offset, number_of_lines_of_context) |
|
1404 | self, etype, value, tb, tb_offset, number_of_lines_of_context) | |
1403 |
|
1405 | |||
1404 |
|
1406 | |||
1405 | #--------------------------------------------------------------------------- |
|
1407 | #--------------------------------------------------------------------------- | |
1406 |
|
1408 | |||
1407 | # A simple class to preserve Nathan's original functionality. |
|
1409 | # A simple class to preserve Nathan's original functionality. | |
1408 | class ColorTB(FormattedTB): |
|
1410 | class ColorTB(FormattedTB): | |
1409 | """Shorthand to initialize a FormattedTB in Linux colors mode.""" |
|
1411 | """Shorthand to initialize a FormattedTB in Linux colors mode.""" | |
1410 |
|
1412 | |||
1411 | def __init__(self, color_scheme='Linux', call_pdb=0, **kwargs): |
|
1413 | def __init__(self, color_scheme='Linux', call_pdb=0, **kwargs): | |
1412 | FormattedTB.__init__(self, color_scheme=color_scheme, |
|
1414 | FormattedTB.__init__(self, color_scheme=color_scheme, | |
1413 | call_pdb=call_pdb, **kwargs) |
|
1415 | call_pdb=call_pdb, **kwargs) | |
1414 |
|
1416 | |||
1415 |
|
1417 | |||
1416 | class SyntaxTB(ListTB): |
|
1418 | class SyntaxTB(ListTB): | |
1417 | """Extension which holds some state: the last exception value""" |
|
1419 | """Extension which holds some state: the last exception value""" | |
1418 |
|
1420 | |||
1419 | def __init__(self, color_scheme='NoColor'): |
|
1421 | def __init__(self, color_scheme='NoColor'): | |
1420 | ListTB.__init__(self, color_scheme) |
|
1422 | ListTB.__init__(self, color_scheme) | |
1421 | self.last_syntax_error = None |
|
1423 | self.last_syntax_error = None | |
1422 |
|
1424 | |||
1423 | def __call__(self, etype, value, elist): |
|
1425 | def __call__(self, etype, value, elist): | |
1424 | self.last_syntax_error = value |
|
1426 | self.last_syntax_error = value | |
1425 |
|
1427 | |||
1426 | ListTB.__call__(self, etype, value, elist) |
|
1428 | ListTB.__call__(self, etype, value, elist) | |
1427 |
|
1429 | |||
1428 | def structured_traceback(self, etype, value, elist, tb_offset=None, |
|
1430 | def structured_traceback(self, etype, value, elist, tb_offset=None, | |
1429 | context=5): |
|
1431 | context=5): | |
1430 | # If the source file has been edited, the line in the syntax error can |
|
1432 | # If the source file has been edited, the line in the syntax error can | |
1431 | # be wrong (retrieved from an outdated cache). This replaces it with |
|
1433 | # be wrong (retrieved from an outdated cache). This replaces it with | |
1432 | # the current value. |
|
1434 | # the current value. | |
1433 | if isinstance(value, SyntaxError) \ |
|
1435 | if isinstance(value, SyntaxError) \ | |
1434 | and isinstance(value.filename, py3compat.string_types) \ |
|
1436 | and isinstance(value.filename, py3compat.string_types) \ | |
1435 | and isinstance(value.lineno, int): |
|
1437 | and isinstance(value.lineno, int): | |
1436 | linecache.checkcache(value.filename) |
|
1438 | linecache.checkcache(value.filename) | |
1437 | newtext = ulinecache.getline(value.filename, value.lineno) |
|
1439 | newtext = ulinecache.getline(value.filename, value.lineno) | |
1438 | if newtext: |
|
1440 | if newtext: | |
1439 | value.text = newtext |
|
1441 | value.text = newtext | |
1440 | self.last_syntax_error = value |
|
1442 | self.last_syntax_error = value | |
1441 | return super(SyntaxTB, self).structured_traceback(etype, value, elist, |
|
1443 | return super(SyntaxTB, self).structured_traceback(etype, value, elist, | |
1442 | tb_offset=tb_offset, context=context) |
|
1444 | tb_offset=tb_offset, context=context) | |
1443 |
|
1445 | |||
1444 | def clear_err_state(self): |
|
1446 | def clear_err_state(self): | |
1445 | """Return the current error state and clear it""" |
|
1447 | """Return the current error state and clear it""" | |
1446 | e = self.last_syntax_error |
|
1448 | e = self.last_syntax_error | |
1447 | self.last_syntax_error = None |
|
1449 | self.last_syntax_error = None | |
1448 | return e |
|
1450 | return e | |
1449 |
|
1451 | |||
1450 | def stb2text(self, stb): |
|
1452 | def stb2text(self, stb): | |
1451 | """Convert a structured traceback (a list) to a string.""" |
|
1453 | """Convert a structured traceback (a list) to a string.""" | |
1452 | return ''.join(stb) |
|
1454 | return ''.join(stb) | |
1453 |
|
1455 | |||
1454 |
|
1456 | |||
1455 | # some internal-use functions |
|
1457 | # some internal-use functions | |
1456 | def text_repr(value): |
|
1458 | def text_repr(value): | |
1457 | """Hopefully pretty robust repr equivalent.""" |
|
1459 | """Hopefully pretty robust repr equivalent.""" | |
1458 | # this is pretty horrible but should always return *something* |
|
1460 | # this is pretty horrible but should always return *something* | |
1459 | try: |
|
1461 | try: | |
1460 | return pydoc.text.repr(value) |
|
1462 | return pydoc.text.repr(value) | |
1461 | except KeyboardInterrupt: |
|
1463 | except KeyboardInterrupt: | |
1462 | raise |
|
1464 | raise | |
1463 | except: |
|
1465 | except: | |
1464 | try: |
|
1466 | try: | |
1465 | return repr(value) |
|
1467 | return repr(value) | |
1466 | except KeyboardInterrupt: |
|
1468 | except KeyboardInterrupt: | |
1467 | raise |
|
1469 | raise | |
1468 | except: |
|
1470 | except: | |
1469 | try: |
|
1471 | try: | |
1470 | # all still in an except block so we catch |
|
1472 | # all still in an except block so we catch | |
1471 | # getattr raising |
|
1473 | # getattr raising | |
1472 | name = getattr(value, '__name__', None) |
|
1474 | name = getattr(value, '__name__', None) | |
1473 | if name: |
|
1475 | if name: | |
1474 | # ick, recursion |
|
1476 | # ick, recursion | |
1475 | return text_repr(name) |
|
1477 | return text_repr(name) | |
1476 | klass = getattr(value, '__class__', None) |
|
1478 | klass = getattr(value, '__class__', None) | |
1477 | if klass: |
|
1479 | if klass: | |
1478 | return '%s instance' % text_repr(klass) |
|
1480 | return '%s instance' % text_repr(klass) | |
1479 | except KeyboardInterrupt: |
|
1481 | except KeyboardInterrupt: | |
1480 | raise |
|
1482 | raise | |
1481 | except: |
|
1483 | except: | |
1482 | return 'UNRECOVERABLE REPR FAILURE' |
|
1484 | return 'UNRECOVERABLE REPR FAILURE' | |
1483 |
|
1485 | |||
1484 |
|
1486 | |||
1485 | def eqrepr(value, repr=text_repr): |
|
1487 | def eqrepr(value, repr=text_repr): | |
1486 | return '=%s' % repr(value) |
|
1488 | return '=%s' % repr(value) | |
1487 |
|
1489 | |||
1488 |
|
1490 | |||
1489 | def nullrepr(value, repr=text_repr): |
|
1491 | def nullrepr(value, repr=text_repr): | |
1490 | return '' |
|
1492 | return '' |
@@ -1,481 +1,428 b'' | |||||
1 | """IPython terminal interface using prompt_toolkit in place of readline""" |
|
1 | """IPython terminal interface using prompt_toolkit in place of readline""" | |
2 | from __future__ import print_function |
|
2 | from __future__ import print_function | |
3 |
|
3 | |||
4 | import os |
|
4 | import os | |
5 | import sys |
|
5 | import sys | |
6 | import signal |
|
6 | import signal | |
7 | import unicodedata |
|
|||
8 | from warnings import warn |
|
7 | from warnings import warn | |
9 | from wcwidth import wcwidth |
|
|||
10 |
|
8 | |||
11 | from IPython.core.error import TryNext |
|
9 | from IPython.core.error import TryNext | |
12 | from IPython.core.interactiveshell import InteractiveShell |
|
10 | from IPython.core.interactiveshell import InteractiveShell | |
13 |
from IPython.utils.py3compat import |
|
11 | from IPython.utils.py3compat import cast_unicode_py2, input | |
14 | from IPython.utils.terminal import toggle_set_term_title, set_term_title |
|
12 | from IPython.utils.terminal import toggle_set_term_title, set_term_title | |
15 | from IPython.utils.process import abbrev_cwd |
|
13 | from IPython.utils.process import abbrev_cwd | |
16 | from traitlets import Bool, Unicode, Dict, Integer, observe |
|
14 | from traitlets import Bool, Unicode, Dict, Integer, observe | |
17 |
|
15 | |||
18 | from prompt_toolkit.completion import Completer, Completion |
|
|||
19 | from prompt_toolkit.enums import DEFAULT_BUFFER, SEARCH_BUFFER, EditingMode |
|
16 | from prompt_toolkit.enums import DEFAULT_BUFFER, SEARCH_BUFFER, EditingMode | |
20 | from prompt_toolkit.filters import HasFocus, HasSelection, Condition, ViInsertMode, EmacsInsertMode, IsDone |
|
17 | from prompt_toolkit.filters import HasFocus, HasSelection, Condition, ViInsertMode, EmacsInsertMode, IsDone | |
21 | from prompt_toolkit.history import InMemoryHistory |
|
18 | from prompt_toolkit.history import InMemoryHistory | |
22 | from prompt_toolkit.shortcuts import create_prompt_application, create_eventloop, create_prompt_layout |
|
19 | from prompt_toolkit.shortcuts import create_prompt_application, create_eventloop, create_prompt_layout | |
23 | from prompt_toolkit.interface import CommandLineInterface |
|
20 | from prompt_toolkit.interface import CommandLineInterface | |
24 | from prompt_toolkit.key_binding.manager import KeyBindingManager |
|
21 | from prompt_toolkit.key_binding.manager import KeyBindingManager | |
25 | from prompt_toolkit.keys import Keys |
|
22 | from prompt_toolkit.keys import Keys | |
26 | from prompt_toolkit.layout.lexers import Lexer |
|
|||
27 | from prompt_toolkit.layout.lexers import PygmentsLexer |
|
|||
28 | from prompt_toolkit.layout.processors import ConditionalProcessor, HighlightMatchingBracketProcessor |
|
23 | from prompt_toolkit.layout.processors import ConditionalProcessor, HighlightMatchingBracketProcessor | |
29 | from prompt_toolkit.styles import PygmentsStyle, DynamicStyle |
|
24 | from prompt_toolkit.styles import PygmentsStyle, DynamicStyle | |
30 |
|
25 | |||
31 | from pygments.styles import get_style_by_name, get_all_styles |
|
26 | from pygments.styles import get_style_by_name, get_all_styles | |
32 | from pygments.lexers import Python3Lexer, BashLexer, PythonLexer |
|
|||
33 | from pygments.token import Token |
|
27 | from pygments.token import Token | |
34 |
|
28 | |||
|
29 | from .debugger import TerminalPdb | |||
35 | from .pt_inputhooks import get_inputhook_func |
|
30 | from .pt_inputhooks import get_inputhook_func | |
36 | from .interactiveshell import get_default_editor, TerminalMagics |
|
31 | from .interactiveshell import get_default_editor, TerminalMagics | |
37 |
|
32 | from .ptutils import IPythonPTCompleter, IPythonPTLexer | ||
38 |
|
||||
39 | class IPythonPTCompleter(Completer): |
|
|||
40 | """Adaptor to provide IPython completions to prompt_toolkit""" |
|
|||
41 | def __init__(self, ipy_completer): |
|
|||
42 | self.ipy_completer = ipy_completer |
|
|||
43 |
|
||||
44 | def get_completions(self, document, complete_event): |
|
|||
45 | if not document.current_line.strip(): |
|
|||
46 | return |
|
|||
47 |
|
||||
48 | used, matches = self.ipy_completer.complete( |
|
|||
49 | line_buffer=document.current_line, |
|
|||
50 | cursor_pos=document.cursor_position_col |
|
|||
51 | ) |
|
|||
52 | start_pos = -len(used) |
|
|||
53 | for m in matches: |
|
|||
54 | m = unicodedata.normalize('NFC', m) |
|
|||
55 |
|
||||
56 | # When the first character of the completion has a zero length, |
|
|||
57 | # then it's probably a decomposed unicode character. E.g. caused by |
|
|||
58 | # the "\dot" completion. Try to compose again with the previous |
|
|||
59 | # character. |
|
|||
60 | if wcwidth(m[0]) == 0: |
|
|||
61 | if document.cursor_position + start_pos > 0: |
|
|||
62 | char_before = document.text[document.cursor_position + start_pos - 1] |
|
|||
63 | m = unicodedata.normalize('NFC', char_before + m) |
|
|||
64 |
|
||||
65 | # Yield the modified completion instead, if this worked. |
|
|||
66 | if wcwidth(m[0:1]) == 1: |
|
|||
67 | yield Completion(m, start_position=start_pos - 1) |
|
|||
68 | continue |
|
|||
69 |
|
||||
70 | # TODO: Use Jedi to determine meta_text |
|
|||
71 | # (Jedi currently has a bug that results in incorrect information.) |
|
|||
72 | # meta_text = '' |
|
|||
73 | # yield Completion(m, start_position=start_pos, |
|
|||
74 | # display_meta=meta_text) |
|
|||
75 | yield Completion(m, start_position=start_pos) |
|
|||
76 |
|
||||
77 | class IPythonPTLexer(Lexer): |
|
|||
78 | """ |
|
|||
79 | Wrapper around PythonLexer and BashLexer. |
|
|||
80 | """ |
|
|||
81 | def __init__(self): |
|
|||
82 | self.python_lexer = PygmentsLexer(Python3Lexer if PY3 else PythonLexer) |
|
|||
83 | self.shell_lexer = PygmentsLexer(BashLexer) |
|
|||
84 |
|
||||
85 | def lex_document(self, cli, document): |
|
|||
86 | if document.text.startswith('!'): |
|
|||
87 | return self.shell_lexer.lex_document(cli, document) |
|
|||
88 | else: |
|
|||
89 | return self.python_lexer.lex_document(cli, document) |
|
|||
90 |
|
33 | |||
91 |
|
34 | |||
92 | class TerminalInteractiveShell(InteractiveShell): |
|
35 | class TerminalInteractiveShell(InteractiveShell): | |
93 | colors_force = True |
|
36 | colors_force = True | |
94 |
|
37 | |||
95 | space_for_menu = Integer(6, help='Number of line at the bottom of the screen ' |
|
38 | space_for_menu = Integer(6, help='Number of line at the bottom of the screen ' | |
96 | 'to reserve for the completion menu' |
|
39 | 'to reserve for the completion menu' | |
97 | ).tag(config=True) |
|
40 | ).tag(config=True) | |
98 |
|
41 | |||
99 | def _space_for_menu_changed(self, old, new): |
|
42 | def _space_for_menu_changed(self, old, new): | |
100 | self._update_layout() |
|
43 | self._update_layout() | |
101 |
|
44 | |||
102 | pt_cli = None |
|
45 | pt_cli = None | |
|
46 | debugger_history = None | |||
|
47 | debugger_cls = TerminalPdb | |||
103 |
|
48 | |||
104 | autoedit_syntax = Bool(False, |
|
49 | autoedit_syntax = Bool(False, | |
105 | help="auto editing of files with syntax errors.", |
|
50 | help="auto editing of files with syntax errors.", | |
106 | ).tag(config=True) |
|
51 | ).tag(config=True) | |
107 |
|
52 | |||
108 |
|
53 | |||
109 | confirm_exit = Bool(True, |
|
54 | confirm_exit = Bool(True, | |
110 | help=""" |
|
55 | help=""" | |
111 | Set to confirm when you try to exit IPython with an EOF (Control-D |
|
56 | Set to confirm when you try to exit IPython with an EOF (Control-D | |
112 | in Unix, Control-Z/Enter in Windows). By typing 'exit' or 'quit', |
|
57 | in Unix, Control-Z/Enter in Windows). By typing 'exit' or 'quit', | |
113 | you can force a direct exit without any confirmation.""", |
|
58 | you can force a direct exit without any confirmation.""", | |
114 | ).tag(config=True) |
|
59 | ).tag(config=True) | |
115 |
|
60 | |||
116 | editing_mode = Unicode('emacs', |
|
61 | editing_mode = Unicode('emacs', | |
117 | help="Shortcut style to use at the prompt. 'vi' or 'emacs'.", |
|
62 | help="Shortcut style to use at the prompt. 'vi' or 'emacs'.", | |
118 | ).tag(config=True) |
|
63 | ).tag(config=True) | |
119 |
|
64 | |||
120 | mouse_support = Bool(False, |
|
65 | mouse_support = Bool(False, | |
121 | help="Enable mouse support in the prompt" |
|
66 | help="Enable mouse support in the prompt" | |
122 | ).tag(config=True) |
|
67 | ).tag(config=True) | |
123 |
|
68 | |||
124 | highlighting_style = Unicode('default', |
|
69 | highlighting_style = Unicode('default', | |
125 | help="The name of a Pygments style to use for syntax highlighting: \n %s" % ', '.join(get_all_styles()) |
|
70 | help="The name of a Pygments style to use for syntax highlighting: \n %s" % ', '.join(get_all_styles()) | |
126 | ).tag(config=True) |
|
71 | ).tag(config=True) | |
127 |
|
72 | |||
128 |
|
73 | |||
129 | @observe('highlighting_style') |
|
74 | @observe('highlighting_style') | |
130 | def _highlighting_style_changed(self, change): |
|
75 | def _highlighting_style_changed(self, change): | |
131 | self._style = self._make_style_from_name(self.highlighting_style) |
|
76 | self._style = self._make_style_from_name(self.highlighting_style) | |
132 |
|
77 | |||
133 | highlighting_style_overrides = Dict( |
|
78 | highlighting_style_overrides = Dict( | |
134 | help="Override highlighting format for specific tokens" |
|
79 | help="Override highlighting format for specific tokens" | |
135 | ).tag(config=True) |
|
80 | ).tag(config=True) | |
136 |
|
81 | |||
137 | editor = Unicode(get_default_editor(), |
|
82 | editor = Unicode(get_default_editor(), | |
138 | help="Set the editor used by IPython (default to $EDITOR/vi/notepad)." |
|
83 | help="Set the editor used by IPython (default to $EDITOR/vi/notepad)." | |
139 | ).tag(config=True) |
|
84 | ).tag(config=True) | |
140 |
|
85 | |||
141 | term_title = Bool(True, |
|
86 | term_title = Bool(True, | |
142 | help="Automatically set the terminal title" |
|
87 | help="Automatically set the terminal title" | |
143 | ).tag(config=True) |
|
88 | ).tag(config=True) | |
144 |
|
89 | |||
145 | display_completions_in_columns = Bool(False, |
|
90 | display_completions_in_columns = Bool(False, | |
146 | help="Display a multi column completion menu.", |
|
91 | help="Display a multi column completion menu.", | |
147 | ).tag(config=True) |
|
92 | ).tag(config=True) | |
148 |
|
93 | |||
149 | highlight_matching_brackets = Bool(True, |
|
94 | highlight_matching_brackets = Bool(True, | |
150 | help="Highlight matching brackets .", |
|
95 | help="Highlight matching brackets .", | |
151 | ).tag(config=True) |
|
96 | ).tag(config=True) | |
152 |
|
97 | |||
153 | @observe('term_title') |
|
98 | @observe('term_title') | |
154 | def init_term_title(self, change=None): |
|
99 | def init_term_title(self, change=None): | |
155 | # Enable or disable the terminal title. |
|
100 | # Enable or disable the terminal title. | |
156 | if self.term_title: |
|
101 | if self.term_title: | |
157 | toggle_set_term_title(True) |
|
102 | toggle_set_term_title(True) | |
158 | set_term_title('IPython: ' + abbrev_cwd()) |
|
103 | set_term_title('IPython: ' + abbrev_cwd()) | |
159 | else: |
|
104 | else: | |
160 | toggle_set_term_title(False) |
|
105 | toggle_set_term_title(False) | |
161 |
|
106 | |||
162 | def get_prompt_tokens(self, cli): |
|
107 | def get_prompt_tokens(self, cli): | |
163 | return [ |
|
108 | return [ | |
164 | (Token.Prompt, 'In ['), |
|
109 | (Token.Prompt, 'In ['), | |
165 | (Token.PromptNum, str(self.execution_count)), |
|
110 | (Token.PromptNum, str(self.execution_count)), | |
166 | (Token.Prompt, ']: '), |
|
111 | (Token.Prompt, ']: '), | |
167 | ] |
|
112 | ] | |
168 |
|
113 | |||
169 | def get_continuation_tokens(self, cli, width): |
|
114 | def get_continuation_tokens(self, cli, width): | |
170 | return [ |
|
115 | return [ | |
171 | (Token.Prompt, (' ' * (width - 5)) + '...: '), |
|
116 | (Token.Prompt, (' ' * (width - 5)) + '...: '), | |
172 | ] |
|
117 | ] | |
173 |
|
118 | |||
174 | def init_prompt_toolkit_cli(self): |
|
119 | def init_prompt_toolkit_cli(self): | |
175 | if ('IPY_TEST_SIMPLE_PROMPT' in os.environ) or not sys.stdin.isatty(): |
|
120 | if ('IPY_TEST_SIMPLE_PROMPT' in os.environ) or not sys.stdin.isatty(): | |
176 | # Fall back to plain non-interactive output for tests. |
|
121 | # Fall back to plain non-interactive output for tests. | |
177 | # This is very limited, and only accepts a single line. |
|
122 | # This is very limited, and only accepts a single line. | |
178 | def prompt(): |
|
123 | def prompt(): | |
179 | return cast_unicode_py2(input('In [%d]: ' % self.execution_count)) |
|
124 | return cast_unicode_py2(input('In [%d]: ' % self.execution_count)) | |
180 | self.prompt_for_code = prompt |
|
125 | self.prompt_for_code = prompt | |
181 | return |
|
126 | return | |
182 |
|
127 | |||
183 | kbmanager = KeyBindingManager.for_prompt() |
|
128 | kbmanager = KeyBindingManager.for_prompt() | |
184 | insert_mode = ViInsertMode() | EmacsInsertMode() |
|
129 | insert_mode = ViInsertMode() | EmacsInsertMode() | |
185 | # Ctrl+J == Enter, seemingly |
|
130 | # Ctrl+J == Enter, seemingly | |
186 | @kbmanager.registry.add_binding(Keys.ControlJ, |
|
131 | @kbmanager.registry.add_binding(Keys.ControlJ, | |
187 | filter=(HasFocus(DEFAULT_BUFFER) |
|
132 | filter=(HasFocus(DEFAULT_BUFFER) | |
188 | & ~HasSelection() |
|
133 | & ~HasSelection() | |
189 | & insert_mode |
|
134 | & insert_mode | |
190 | )) |
|
135 | )) | |
191 | def _(event): |
|
136 | def _(event): | |
192 | b = event.current_buffer |
|
137 | b = event.current_buffer | |
193 | d = b.document |
|
138 | d = b.document | |
194 | if not (d.on_last_line or d.cursor_position_row >= d.line_count |
|
139 | if not (d.on_last_line or d.cursor_position_row >= d.line_count | |
195 | - d.empty_line_count_at_the_end()): |
|
140 | - d.empty_line_count_at_the_end()): | |
196 | b.newline() |
|
141 | b.newline() | |
197 | return |
|
142 | return | |
198 |
|
143 | |||
199 | status, indent = self.input_splitter.check_complete(d.text) |
|
144 | status, indent = self.input_splitter.check_complete(d.text) | |
200 |
|
145 | |||
201 | if (status != 'incomplete') and b.accept_action.is_returnable: |
|
146 | if (status != 'incomplete') and b.accept_action.is_returnable: | |
202 | b.accept_action.validate_and_handle(event.cli, b) |
|
147 | b.accept_action.validate_and_handle(event.cli, b) | |
203 | else: |
|
148 | else: | |
204 | b.insert_text('\n' + (' ' * (indent or 0))) |
|
149 | b.insert_text('\n' + (' ' * (indent or 0))) | |
205 |
|
150 | |||
206 | @kbmanager.registry.add_binding(Keys.ControlC, filter=HasFocus(DEFAULT_BUFFER)) |
|
151 | @kbmanager.registry.add_binding(Keys.ControlC, filter=HasFocus(DEFAULT_BUFFER)) | |
207 | def _reset_buffer(event): |
|
152 | def _reset_buffer(event): | |
208 | event.current_buffer.reset() |
|
153 | event.current_buffer.reset() | |
209 |
|
154 | |||
210 | @kbmanager.registry.add_binding(Keys.ControlC, filter=HasFocus(SEARCH_BUFFER)) |
|
155 | @kbmanager.registry.add_binding(Keys.ControlC, filter=HasFocus(SEARCH_BUFFER)) | |
211 | def _reset_search_buffer(event): |
|
156 | def _reset_search_buffer(event): | |
212 | if event.current_buffer.document.text: |
|
157 | if event.current_buffer.document.text: | |
213 | event.current_buffer.reset() |
|
158 | event.current_buffer.reset() | |
214 | else: |
|
159 | else: | |
215 | event.cli.push_focus(DEFAULT_BUFFER) |
|
160 | event.cli.push_focus(DEFAULT_BUFFER) | |
216 |
|
161 | |||
217 | supports_suspend = Condition(lambda cli: hasattr(signal, 'SIGTSTP')) |
|
162 | supports_suspend = Condition(lambda cli: hasattr(signal, 'SIGTSTP')) | |
218 |
|
163 | |||
219 | @kbmanager.registry.add_binding(Keys.ControlZ, filter=supports_suspend) |
|
164 | @kbmanager.registry.add_binding(Keys.ControlZ, filter=supports_suspend) | |
220 | def _suspend_to_bg(event): |
|
165 | def _suspend_to_bg(event): | |
221 | event.cli.suspend_to_background() |
|
166 | event.cli.suspend_to_background() | |
222 |
|
167 | |||
223 | @Condition |
|
168 | @Condition | |
224 | def cursor_in_leading_ws(cli): |
|
169 | def cursor_in_leading_ws(cli): | |
225 | before = cli.application.buffer.document.current_line_before_cursor |
|
170 | before = cli.application.buffer.document.current_line_before_cursor | |
226 | return (not before) or before.isspace() |
|
171 | return (not before) or before.isspace() | |
227 |
|
172 | |||
228 | # Ctrl+I == Tab |
|
173 | # Ctrl+I == Tab | |
229 | @kbmanager.registry.add_binding(Keys.ControlI, |
|
174 | @kbmanager.registry.add_binding(Keys.ControlI, | |
230 | filter=(HasFocus(DEFAULT_BUFFER) |
|
175 | filter=(HasFocus(DEFAULT_BUFFER) | |
231 | & ~HasSelection() |
|
176 | & ~HasSelection() | |
232 | & insert_mode |
|
177 | & insert_mode | |
233 | & cursor_in_leading_ws |
|
178 | & cursor_in_leading_ws | |
234 | )) |
|
179 | )) | |
235 | def _indent_buffer(event): |
|
180 | def _indent_buffer(event): | |
236 | event.current_buffer.insert_text(' ' * 4) |
|
181 | event.current_buffer.insert_text(' ' * 4) | |
237 |
|
182 | |||
238 | # Pre-populate history from IPython's history database |
|
183 | # Pre-populate history from IPython's history database | |
239 | history = InMemoryHistory() |
|
184 | history = InMemoryHistory() | |
240 | last_cell = u"" |
|
185 | last_cell = u"" | |
241 | for __, ___, cell in self.history_manager.get_tail(self.history_load_length, |
|
186 | for __, ___, cell in self.history_manager.get_tail(self.history_load_length, | |
242 | include_latest=True): |
|
187 | include_latest=True): | |
243 | # Ignore blank lines and consecutive duplicates |
|
188 | # Ignore blank lines and consecutive duplicates | |
244 | cell = cell.rstrip() |
|
189 | cell = cell.rstrip() | |
245 | if cell and (cell != last_cell): |
|
190 | if cell and (cell != last_cell): | |
246 | history.append(cell) |
|
191 | history.append(cell) | |
247 |
|
192 | |||
248 | self._style = self._make_style_from_name(self.highlighting_style) |
|
193 | self._style = self._make_style_from_name(self.highlighting_style) | |
249 | style = DynamicStyle(lambda: self._style) |
|
194 | style = DynamicStyle(lambda: self._style) | |
250 |
|
195 | |||
251 | editing_mode = getattr(EditingMode, self.editing_mode.upper()) |
|
196 | editing_mode = getattr(EditingMode, self.editing_mode.upper()) | |
252 |
|
197 | |||
253 | self._app = create_prompt_application( |
|
198 | self._app = create_prompt_application( | |
254 | editing_mode=editing_mode, |
|
199 | editing_mode=editing_mode, | |
255 | key_bindings_registry=kbmanager.registry, |
|
200 | key_bindings_registry=kbmanager.registry, | |
256 | history=history, |
|
201 | history=history, | |
257 | completer=IPythonPTCompleter(self.Completer), |
|
202 | completer=IPythonPTCompleter(self.Completer), | |
258 | enable_history_search=True, |
|
203 | enable_history_search=True, | |
259 | style=style, |
|
204 | style=style, | |
260 | mouse_support=self.mouse_support, |
|
205 | mouse_support=self.mouse_support, | |
261 | **self._layout_options() |
|
206 | **self._layout_options() | |
262 | ) |
|
207 | ) | |
263 | self._eventloop = create_eventloop(self.inputhook) |
|
208 | self._eventloop = create_eventloop(self.inputhook) | |
264 | self.pt_cli = CommandLineInterface(self._app, eventloop=self._eventloop) |
|
209 | self.pt_cli = CommandLineInterface(self._app, eventloop=self._eventloop) | |
265 |
|
210 | |||
266 | def _make_style_from_name(self, name): |
|
211 | def _make_style_from_name(self, name): | |
267 | """ |
|
212 | """ | |
268 | Small wrapper that make an IPython compatible style from a style name |
|
213 | Small wrapper that make an IPython compatible style from a style name | |
269 |
|
214 | |||
270 | We need that to add style for prompt ... etc. |
|
215 | We need that to add style for prompt ... etc. | |
271 | """ |
|
216 | """ | |
272 | style_cls = get_style_by_name(name) |
|
217 | style_cls = get_style_by_name(name) | |
273 | style_overrides = { |
|
218 | style_overrides = { | |
274 | Token.Prompt: '#009900', |
|
219 | Token.Prompt: '#009900', | |
275 | Token.PromptNum: '#00ff00 bold', |
|
220 | Token.PromptNum: '#00ff00 bold', | |
276 | } |
|
221 | } | |
277 | if name == 'default': |
|
222 | if name == 'default': | |
278 | style_cls = get_style_by_name('default') |
|
223 | style_cls = get_style_by_name('default') | |
279 | # The default theme needs to be visible on both a dark background |
|
224 | # The default theme needs to be visible on both a dark background | |
280 | # and a light background, because we can't tell what the terminal |
|
225 | # and a light background, because we can't tell what the terminal | |
281 | # looks like. These tweaks to the default theme help with that. |
|
226 | # looks like. These tweaks to the default theme help with that. | |
282 | style_overrides.update({ |
|
227 | style_overrides.update({ | |
283 | Token.Number: '#007700', |
|
228 | Token.Number: '#007700', | |
284 | Token.Operator: 'noinherit', |
|
229 | Token.Operator: 'noinherit', | |
285 | Token.String: '#BB6622', |
|
230 | Token.String: '#BB6622', | |
286 | Token.Name.Function: '#2080D0', |
|
231 | Token.Name.Function: '#2080D0', | |
287 | Token.Name.Class: 'bold #2080D0', |
|
232 | Token.Name.Class: 'bold #2080D0', | |
288 | Token.Name.Namespace: 'bold #2080D0', |
|
233 | Token.Name.Namespace: 'bold #2080D0', | |
289 | }) |
|
234 | }) | |
290 | style_overrides.update(self.highlighting_style_overrides) |
|
235 | style_overrides.update(self.highlighting_style_overrides) | |
291 | style = PygmentsStyle.from_defaults(pygments_style_cls=style_cls, |
|
236 | style = PygmentsStyle.from_defaults(pygments_style_cls=style_cls, | |
292 | style_dict=style_overrides) |
|
237 | style_dict=style_overrides) | |
293 |
|
238 | |||
294 | return style |
|
239 | return style | |
295 |
|
240 | |||
296 | def _layout_options(self): |
|
241 | def _layout_options(self): | |
297 | """ |
|
242 | """ | |
298 | Return the current layout option for the current Terminal InteractiveShell |
|
243 | Return the current layout option for the current Terminal InteractiveShell | |
299 | """ |
|
244 | """ | |
300 | return { |
|
245 | return { | |
301 | 'lexer':IPythonPTLexer(), |
|
246 | 'lexer':IPythonPTLexer(), | |
302 | 'reserve_space_for_menu':self.space_for_menu, |
|
247 | 'reserve_space_for_menu':self.space_for_menu, | |
303 | 'get_prompt_tokens':self.get_prompt_tokens, |
|
248 | 'get_prompt_tokens':self.get_prompt_tokens, | |
304 | 'get_continuation_tokens':self.get_continuation_tokens, |
|
249 | 'get_continuation_tokens':self.get_continuation_tokens, | |
305 | 'multiline':True, |
|
250 | 'multiline':True, | |
306 | 'display_completions_in_columns': self.display_completions_in_columns, |
|
251 | 'display_completions_in_columns': self.display_completions_in_columns, | |
307 |
|
252 | |||
308 | # Highlight matching brackets, but only when this setting is |
|
253 | # Highlight matching brackets, but only when this setting is | |
309 | # enabled, and only when the DEFAULT_BUFFER has the focus. |
|
254 | # enabled, and only when the DEFAULT_BUFFER has the focus. | |
310 | 'extra_input_processors': [ConditionalProcessor( |
|
255 | 'extra_input_processors': [ConditionalProcessor( | |
311 | processor=HighlightMatchingBracketProcessor(chars='[](){}'), |
|
256 | processor=HighlightMatchingBracketProcessor(chars='[](){}'), | |
312 | filter=HasFocus(DEFAULT_BUFFER) & ~IsDone() & |
|
257 | filter=HasFocus(DEFAULT_BUFFER) & ~IsDone() & | |
313 | Condition(lambda cli: self.highlight_matching_brackets))], |
|
258 | Condition(lambda cli: self.highlight_matching_brackets))], | |
314 | } |
|
259 | } | |
315 |
|
260 | |||
316 | def _update_layout(self): |
|
261 | def _update_layout(self): | |
317 | """ |
|
262 | """ | |
318 | Ask for a re computation of the application layout, if for example , |
|
263 | Ask for a re computation of the application layout, if for example , | |
319 | some configuration options have changed. |
|
264 | some configuration options have changed. | |
320 | """ |
|
265 | """ | |
321 | self._app.layout = create_prompt_layout(**self._layout_options()) |
|
266 | self._app.layout = create_prompt_layout(**self._layout_options()) | |
322 |
|
267 | |||
323 | def prompt_for_code(self): |
|
268 | def prompt_for_code(self): | |
324 | document = self.pt_cli.run( |
|
269 | document = self.pt_cli.run( | |
325 | pre_run=self.pre_prompt, reset_current_buffer=True) |
|
270 | pre_run=self.pre_prompt, reset_current_buffer=True) | |
326 | return document.text |
|
271 | return document.text | |
327 |
|
272 | |||
328 | def init_io(self): |
|
273 | def init_io(self): | |
329 | if sys.platform not in {'win32', 'cli'}: |
|
274 | if sys.platform not in {'win32', 'cli'}: | |
330 | return |
|
275 | return | |
331 |
|
276 | |||
332 | import colorama |
|
277 | import colorama | |
333 | colorama.init() |
|
278 | colorama.init() | |
334 |
|
279 | |||
335 | # For some reason we make these wrappers around stdout/stderr. |
|
280 | # For some reason we make these wrappers around stdout/stderr. | |
336 | # For now, we need to reset them so all output gets coloured. |
|
281 | # For now, we need to reset them so all output gets coloured. | |
337 | # https://github.com/ipython/ipython/issues/8669 |
|
282 | # https://github.com/ipython/ipython/issues/8669 | |
338 | from IPython.utils import io |
|
283 | from IPython.utils import io | |
339 | io.stdout = io.IOStream(sys.stdout) |
|
284 | io.stdout = io.IOStream(sys.stdout) | |
340 | io.stderr = io.IOStream(sys.stderr) |
|
285 | io.stderr = io.IOStream(sys.stderr) | |
341 |
|
286 | |||
342 | def init_magics(self): |
|
287 | def init_magics(self): | |
343 | super(TerminalInteractiveShell, self).init_magics() |
|
288 | super(TerminalInteractiveShell, self).init_magics() | |
344 | self.register_magics(TerminalMagics) |
|
289 | self.register_magics(TerminalMagics) | |
345 |
|
290 | |||
346 | def init_alias(self): |
|
291 | def init_alias(self): | |
347 | # The parent class defines aliases that can be safely used with any |
|
292 | # The parent class defines aliases that can be safely used with any | |
348 | # frontend. |
|
293 | # frontend. | |
349 | super(TerminalInteractiveShell, self).init_alias() |
|
294 | super(TerminalInteractiveShell, self).init_alias() | |
350 |
|
295 | |||
351 | # Now define aliases that only make sense on the terminal, because they |
|
296 | # Now define aliases that only make sense on the terminal, because they | |
352 | # need direct access to the console in a way that we can't emulate in |
|
297 | # need direct access to the console in a way that we can't emulate in | |
353 | # GUI or web frontend |
|
298 | # GUI or web frontend | |
354 | if os.name == 'posix': |
|
299 | if os.name == 'posix': | |
355 | for cmd in ['clear', 'more', 'less', 'man']: |
|
300 | for cmd in ['clear', 'more', 'less', 'man']: | |
356 | self.alias_manager.soft_define_alias(cmd, cmd) |
|
301 | self.alias_manager.soft_define_alias(cmd, cmd) | |
357 |
|
302 | |||
358 |
|
303 | |||
359 | def __init__(self, *args, **kwargs): |
|
304 | def __init__(self, *args, **kwargs): | |
360 | super(TerminalInteractiveShell, self).__init__(*args, **kwargs) |
|
305 | super(TerminalInteractiveShell, self).__init__(*args, **kwargs) | |
361 | self.init_prompt_toolkit_cli() |
|
306 | self.init_prompt_toolkit_cli() | |
362 | self.init_term_title() |
|
307 | self.init_term_title() | |
363 | self.keep_running = True |
|
308 | self.keep_running = True | |
364 |
|
309 | |||
|
310 | self.debugger_history = InMemoryHistory() | |||
|
311 | ||||
365 | def ask_exit(self): |
|
312 | def ask_exit(self): | |
366 | self.keep_running = False |
|
313 | self.keep_running = False | |
367 |
|
314 | |||
368 | rl_next_input = None |
|
315 | rl_next_input = None | |
369 |
|
316 | |||
370 | def pre_prompt(self): |
|
317 | def pre_prompt(self): | |
371 | if self.rl_next_input: |
|
318 | if self.rl_next_input: | |
372 | self.pt_cli.application.buffer.text = cast_unicode_py2(self.rl_next_input) |
|
319 | self.pt_cli.application.buffer.text = cast_unicode_py2(self.rl_next_input) | |
373 | self.rl_next_input = None |
|
320 | self.rl_next_input = None | |
374 |
|
321 | |||
375 | def interact(self): |
|
322 | def interact(self): | |
376 | while self.keep_running: |
|
323 | while self.keep_running: | |
377 | print(self.separate_in, end='') |
|
324 | print(self.separate_in, end='') | |
378 |
|
325 | |||
379 | try: |
|
326 | try: | |
380 | code = self.prompt_for_code() |
|
327 | code = self.prompt_for_code() | |
381 | except EOFError: |
|
328 | except EOFError: | |
382 | if (not self.confirm_exit) \ |
|
329 | if (not self.confirm_exit) \ | |
383 | or self.ask_yes_no('Do you really want to exit ([y]/n)?','y','n'): |
|
330 | or self.ask_yes_no('Do you really want to exit ([y]/n)?','y','n'): | |
384 | self.ask_exit() |
|
331 | self.ask_exit() | |
385 |
|
332 | |||
386 | else: |
|
333 | else: | |
387 | if code: |
|
334 | if code: | |
388 | self.run_cell(code, store_history=True) |
|
335 | self.run_cell(code, store_history=True) | |
389 | if self.autoedit_syntax and self.SyntaxTB.last_syntax_error: |
|
336 | if self.autoedit_syntax and self.SyntaxTB.last_syntax_error: | |
390 | self.edit_syntax_error() |
|
337 | self.edit_syntax_error() | |
391 |
|
338 | |||
392 | def mainloop(self): |
|
339 | def mainloop(self): | |
393 | # An extra layer of protection in case someone mashing Ctrl-C breaks |
|
340 | # An extra layer of protection in case someone mashing Ctrl-C breaks | |
394 | # out of our internal code. |
|
341 | # out of our internal code. | |
395 | while True: |
|
342 | while True: | |
396 | try: |
|
343 | try: | |
397 | self.interact() |
|
344 | self.interact() | |
398 | break |
|
345 | break | |
399 | except KeyboardInterrupt: |
|
346 | except KeyboardInterrupt: | |
400 | print("\nKeyboardInterrupt escaped interact()\n") |
|
347 | print("\nKeyboardInterrupt escaped interact()\n") | |
401 |
|
348 | |||
402 | if hasattr(self, '_eventloop'): |
|
349 | if hasattr(self, '_eventloop'): | |
403 | self._eventloop.close() |
|
350 | self._eventloop.close() | |
404 |
|
351 | |||
405 | _inputhook = None |
|
352 | _inputhook = None | |
406 | def inputhook(self, context): |
|
353 | def inputhook(self, context): | |
407 | if self._inputhook is not None: |
|
354 | if self._inputhook is not None: | |
408 | self._inputhook(context) |
|
355 | self._inputhook(context) | |
409 |
|
356 | |||
410 | def enable_gui(self, gui=None): |
|
357 | def enable_gui(self, gui=None): | |
411 | if gui: |
|
358 | if gui: | |
412 | self._inputhook = get_inputhook_func(gui) |
|
359 | self._inputhook = get_inputhook_func(gui) | |
413 | else: |
|
360 | else: | |
414 | self._inputhook = None |
|
361 | self._inputhook = None | |
415 |
|
362 | |||
416 | # Methods to support auto-editing of SyntaxErrors: |
|
363 | # Methods to support auto-editing of SyntaxErrors: | |
417 |
|
364 | |||
418 | def edit_syntax_error(self): |
|
365 | def edit_syntax_error(self): | |
419 | """The bottom half of the syntax error handler called in the main loop. |
|
366 | """The bottom half of the syntax error handler called in the main loop. | |
420 |
|
367 | |||
421 | Loop until syntax error is fixed or user cancels. |
|
368 | Loop until syntax error is fixed or user cancels. | |
422 | """ |
|
369 | """ | |
423 |
|
370 | |||
424 | while self.SyntaxTB.last_syntax_error: |
|
371 | while self.SyntaxTB.last_syntax_error: | |
425 | # copy and clear last_syntax_error |
|
372 | # copy and clear last_syntax_error | |
426 | err = self.SyntaxTB.clear_err_state() |
|
373 | err = self.SyntaxTB.clear_err_state() | |
427 | if not self._should_recompile(err): |
|
374 | if not self._should_recompile(err): | |
428 | return |
|
375 | return | |
429 | try: |
|
376 | try: | |
430 | # may set last_syntax_error again if a SyntaxError is raised |
|
377 | # may set last_syntax_error again if a SyntaxError is raised | |
431 | self.safe_execfile(err.filename, self.user_ns) |
|
378 | self.safe_execfile(err.filename, self.user_ns) | |
432 | except: |
|
379 | except: | |
433 | self.showtraceback() |
|
380 | self.showtraceback() | |
434 | else: |
|
381 | else: | |
435 | try: |
|
382 | try: | |
436 | with open(err.filename) as f: |
|
383 | with open(err.filename) as f: | |
437 | # This should be inside a display_trap block and I |
|
384 | # This should be inside a display_trap block and I | |
438 | # think it is. |
|
385 | # think it is. | |
439 | sys.displayhook(f.read()) |
|
386 | sys.displayhook(f.read()) | |
440 | except: |
|
387 | except: | |
441 | self.showtraceback() |
|
388 | self.showtraceback() | |
442 |
|
389 | |||
443 | def _should_recompile(self, e): |
|
390 | def _should_recompile(self, e): | |
444 | """Utility routine for edit_syntax_error""" |
|
391 | """Utility routine for edit_syntax_error""" | |
445 |
|
392 | |||
446 | if e.filename in ('<ipython console>', '<input>', '<string>', |
|
393 | if e.filename in ('<ipython console>', '<input>', '<string>', | |
447 | '<console>', '<BackgroundJob compilation>', |
|
394 | '<console>', '<BackgroundJob compilation>', | |
448 | None): |
|
395 | None): | |
449 | return False |
|
396 | return False | |
450 | try: |
|
397 | try: | |
451 | if (self.autoedit_syntax and |
|
398 | if (self.autoedit_syntax and | |
452 | not self.ask_yes_no( |
|
399 | not self.ask_yes_no( | |
453 | 'Return to editor to correct syntax error? ' |
|
400 | 'Return to editor to correct syntax error? ' | |
454 | '[Y/n] ', 'y')): |
|
401 | '[Y/n] ', 'y')): | |
455 | return False |
|
402 | return False | |
456 | except EOFError: |
|
403 | except EOFError: | |
457 | return False |
|
404 | return False | |
458 |
|
405 | |||
459 | def int0(x): |
|
406 | def int0(x): | |
460 | try: |
|
407 | try: | |
461 | return int(x) |
|
408 | return int(x) | |
462 | except TypeError: |
|
409 | except TypeError: | |
463 | return 0 |
|
410 | return 0 | |
464 |
|
411 | |||
465 | # always pass integer line and offset values to editor hook |
|
412 | # always pass integer line and offset values to editor hook | |
466 | try: |
|
413 | try: | |
467 | self.hooks.fix_error_editor(e.filename, |
|
414 | self.hooks.fix_error_editor(e.filename, | |
468 | int0(e.lineno), int0(e.offset), |
|
415 | int0(e.lineno), int0(e.offset), | |
469 | e.msg) |
|
416 | e.msg) | |
470 | except TryNext: |
|
417 | except TryNext: | |
471 | warn('Could not open editor') |
|
418 | warn('Could not open editor') | |
472 | return False |
|
419 | return False | |
473 | return True |
|
420 | return True | |
474 |
|
421 | |||
475 | # Run !system commands directly, not through pipes, so terminal programs |
|
422 | # Run !system commands directly, not through pipes, so terminal programs | |
476 | # work correctly. |
|
423 | # work correctly. | |
477 | system = InteractiveShell.system_raw |
|
424 | system = InteractiveShell.system_raw | |
478 |
|
425 | |||
479 |
|
426 | |||
480 | if __name__ == '__main__': |
|
427 | if __name__ == '__main__': | |
481 | TerminalInteractiveShell.instance().interact() |
|
428 | TerminalInteractiveShell.instance().interact() |
General Comments 0
You need to be logged in to leave comments.
Login now