Show More
@@ -1,1271 +1,1276 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 | if set(s) & set(PROTECTABLES): | |||
|
121 | if sys.platform == "win32": | |||
|
122 | return '"' + s + '"' | |||
|
123 | else: | |||
|
124 | return "".join(("\\" + c if c in PROTECTABLES else c) for c in s) | |||
|
125 | else: | |||
|
126 | return s | |||
120 |
|
127 | |||
121 | return "".join([(ch in PROTECTABLES and '\\' + ch or ch) |
|
|||
122 | for ch in s]) |
|
|||
123 |
|
128 | |||
124 | def expand_user(path): |
|
129 | def expand_user(path): | |
125 | """Expand '~'-style usernames in strings. |
|
130 | """Expand '~'-style usernames in strings. | |
126 |
|
131 | |||
127 | This is similar to :func:`os.path.expanduser`, but it computes and returns |
|
132 | 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 |
|
133 | 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 |
|
134 | computing completions, and you wish to return the completions with the | |
130 | original '~' instead of its expanded value. |
|
135 | original '~' instead of its expanded value. | |
131 |
|
136 | |||
132 | Parameters |
|
137 | Parameters | |
133 | ---------- |
|
138 | ---------- | |
134 | path : str |
|
139 | path : str | |
135 | String to be expanded. If no ~ is present, the output is the same as the |
|
140 | String to be expanded. If no ~ is present, the output is the same as the | |
136 | input. |
|
141 | input. | |
137 |
|
142 | |||
138 | Returns |
|
143 | Returns | |
139 | ------- |
|
144 | ------- | |
140 | newpath : str |
|
145 | newpath : str | |
141 | Result of ~ expansion in the input path. |
|
146 | Result of ~ expansion in the input path. | |
142 | tilde_expand : bool |
|
147 | tilde_expand : bool | |
143 | Whether any expansion was performed or not. |
|
148 | Whether any expansion was performed or not. | |
144 | tilde_val : str |
|
149 | tilde_val : str | |
145 | The value that ~ was replaced with. |
|
150 | The value that ~ was replaced with. | |
146 | """ |
|
151 | """ | |
147 | # Default values |
|
152 | # Default values | |
148 | tilde_expand = False |
|
153 | tilde_expand = False | |
149 | tilde_val = '' |
|
154 | tilde_val = '' | |
150 | newpath = path |
|
155 | newpath = path | |
151 |
|
156 | |||
152 | if path.startswith('~'): |
|
157 | if path.startswith('~'): | |
153 | tilde_expand = True |
|
158 | tilde_expand = True | |
154 | rest = len(path)-1 |
|
159 | rest = len(path)-1 | |
155 | newpath = os.path.expanduser(path) |
|
160 | newpath = os.path.expanduser(path) | |
156 | if rest: |
|
161 | if rest: | |
157 | tilde_val = newpath[:-rest] |
|
162 | tilde_val = newpath[:-rest] | |
158 | else: |
|
163 | else: | |
159 | tilde_val = newpath |
|
164 | tilde_val = newpath | |
160 |
|
165 | |||
161 | return newpath, tilde_expand, tilde_val |
|
166 | return newpath, tilde_expand, tilde_val | |
162 |
|
167 | |||
163 |
|
168 | |||
164 | def compress_user(path, tilde_expand, tilde_val): |
|
169 | def compress_user(path, tilde_expand, tilde_val): | |
165 | """Does the opposite of expand_user, with its outputs. |
|
170 | """Does the opposite of expand_user, with its outputs. | |
166 | """ |
|
171 | """ | |
167 | if tilde_expand: |
|
172 | if tilde_expand: | |
168 | return path.replace(tilde_val, '~') |
|
173 | return path.replace(tilde_val, '~') | |
169 | else: |
|
174 | else: | |
170 | return path |
|
175 | return path | |
171 |
|
176 | |||
172 |
|
177 | |||
173 | def completions_sorting_key(word): |
|
178 | def completions_sorting_key(word): | |
174 | """key for sorting completions |
|
179 | """key for sorting completions | |
175 |
|
180 | |||
176 | This does several things: |
|
181 | This does several things: | |
177 |
|
182 | |||
178 | - Lowercase all completions, so they are sorted alphabetically with |
|
183 | - Lowercase all completions, so they are sorted alphabetically with | |
179 | upper and lower case words mingled |
|
184 | upper and lower case words mingled | |
180 | - Demote any completions starting with underscores to the end |
|
185 | - Demote any completions starting with underscores to the end | |
181 | - Insert any %magic and %%cellmagic completions in the alphabetical order |
|
186 | - Insert any %magic and %%cellmagic completions in the alphabetical order | |
182 | by their name |
|
187 | by their name | |
183 | """ |
|
188 | """ | |
184 | # Case insensitive sort |
|
189 | # Case insensitive sort | |
185 | word = word.lower() |
|
190 | word = word.lower() | |
186 |
|
191 | |||
187 | prio1, prio2 = 0, 0 |
|
192 | prio1, prio2 = 0, 0 | |
188 |
|
193 | |||
189 | if word.startswith('__'): |
|
194 | if word.startswith('__'): | |
190 | prio1 = 2 |
|
195 | prio1 = 2 | |
191 | elif word.startswith('_'): |
|
196 | elif word.startswith('_'): | |
192 | prio1 = 1 |
|
197 | prio1 = 1 | |
193 |
|
198 | |||
194 | if word.endswith('='): |
|
199 | if word.endswith('='): | |
195 | prio1 = -1 |
|
200 | prio1 = -1 | |
196 |
|
201 | |||
197 | if word.startswith('%%'): |
|
202 | if word.startswith('%%'): | |
198 | # If there's another % in there, this is something else, so leave it alone |
|
203 | # If there's another % in there, this is something else, so leave it alone | |
199 | if not "%" in word[2:]: |
|
204 | if not "%" in word[2:]: | |
200 | word = word[2:] |
|
205 | word = word[2:] | |
201 | prio2 = 2 |
|
206 | prio2 = 2 | |
202 | elif word.startswith('%'): |
|
207 | elif word.startswith('%'): | |
203 | if not "%" in word[1:]: |
|
208 | if not "%" in word[1:]: | |
204 | word = word[1:] |
|
209 | word = word[1:] | |
205 | prio2 = 1 |
|
210 | prio2 = 1 | |
206 |
|
211 | |||
207 | return prio1, word, prio2 |
|
212 | return prio1, word, prio2 | |
208 |
|
213 | |||
209 |
|
214 | |||
210 | @undoc |
|
215 | @undoc | |
211 | class Bunch(object): pass |
|
216 | class Bunch(object): pass | |
212 |
|
217 | |||
213 |
|
218 | |||
214 | DELIMS = ' \t\n`!@#$^&*()=+[{]}\\|;:\'",<>?' |
|
219 | DELIMS = ' \t\n`!@#$^&*()=+[{]}\\|;:\'",<>?' | |
215 | GREEDY_DELIMS = ' =\r\n' |
|
220 | GREEDY_DELIMS = ' =\r\n' | |
216 |
|
221 | |||
217 |
|
222 | |||
218 | class CompletionSplitter(object): |
|
223 | class CompletionSplitter(object): | |
219 | """An object to split an input line in a manner similar to readline. |
|
224 | """An object to split an input line in a manner similar to readline. | |
220 |
|
225 | |||
221 | By having our own implementation, we can expose readline-like completion in |
|
226 | 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 |
|
227 | 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 |
|
228 | 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 |
|
229 | returns the 'word' to be completed on at the cursor after splitting the | |
225 | entire line. |
|
230 | entire line. | |
226 |
|
231 | |||
227 | What characters are used as splitting delimiters can be controlled by |
|
232 | What characters are used as splitting delimiters can be controlled by | |
228 | setting the `delims` attribute (this is a property that internally |
|
233 | setting the `delims` attribute (this is a property that internally | |
229 | automatically builds the necessary regular expression)""" |
|
234 | automatically builds the necessary regular expression)""" | |
230 |
|
235 | |||
231 | # Private interface |
|
236 | # Private interface | |
232 |
|
237 | |||
233 | # A string of delimiter characters. The default value makes sense for |
|
238 | # A string of delimiter characters. The default value makes sense for | |
234 | # IPython's most typical usage patterns. |
|
239 | # IPython's most typical usage patterns. | |
235 | _delims = DELIMS |
|
240 | _delims = DELIMS | |
236 |
|
241 | |||
237 | # The expression (a normal string) to be compiled into a regular expression |
|
242 | # 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 |
|
243 | # 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. |
|
244 | # debugging, since this type of code can be so tricky to debug. | |
240 | _delim_expr = None |
|
245 | _delim_expr = None | |
241 |
|
246 | |||
242 | # The regular expression that does the actual splitting |
|
247 | # The regular expression that does the actual splitting | |
243 | _delim_re = None |
|
248 | _delim_re = None | |
244 |
|
249 | |||
245 | def __init__(self, delims=None): |
|
250 | def __init__(self, delims=None): | |
246 | delims = CompletionSplitter._delims if delims is None else delims |
|
251 | delims = CompletionSplitter._delims if delims is None else delims | |
247 | self.delims = delims |
|
252 | self.delims = delims | |
248 |
|
253 | |||
249 | @property |
|
254 | @property | |
250 | def delims(self): |
|
255 | def delims(self): | |
251 | """Return the string of delimiter characters.""" |
|
256 | """Return the string of delimiter characters.""" | |
252 | return self._delims |
|
257 | return self._delims | |
253 |
|
258 | |||
254 | @delims.setter |
|
259 | @delims.setter | |
255 | def delims(self, delims): |
|
260 | def delims(self, delims): | |
256 | """Set the delimiters for line splitting.""" |
|
261 | """Set the delimiters for line splitting.""" | |
257 | expr = '[' + ''.join('\\'+ c for c in delims) + ']' |
|
262 | expr = '[' + ''.join('\\'+ c for c in delims) + ']' | |
258 | self._delim_re = re.compile(expr) |
|
263 | self._delim_re = re.compile(expr) | |
259 | self._delims = delims |
|
264 | self._delims = delims | |
260 | self._delim_expr = expr |
|
265 | self._delim_expr = expr | |
261 |
|
266 | |||
262 | def split_line(self, line, cursor_pos=None): |
|
267 | def split_line(self, line, cursor_pos=None): | |
263 | """Split a line of text with a cursor at the given position. |
|
268 | """Split a line of text with a cursor at the given position. | |
264 | """ |
|
269 | """ | |
265 | l = line if cursor_pos is None else line[:cursor_pos] |
|
270 | l = line if cursor_pos is None else line[:cursor_pos] | |
266 | return self._delim_re.split(l)[-1] |
|
271 | return self._delim_re.split(l)[-1] | |
267 |
|
272 | |||
268 |
|
273 | |||
269 | class Completer(Configurable): |
|
274 | class Completer(Configurable): | |
270 |
|
275 | |||
271 | greedy = Bool(False, |
|
276 | greedy = Bool(False, | |
272 | help="""Activate greedy completion |
|
277 | help="""Activate greedy completion | |
273 | PENDING DEPRECTION. this is now mostly taken care of with Jedi. |
|
278 | PENDING DEPRECTION. this is now mostly taken care of with Jedi. | |
274 |
|
279 | |||
275 | This will enable completion on elements of lists, results of function calls, etc., |
|
280 | 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. |
|
281 | but can be unsafe because the code is actually evaluated on TAB. | |
277 | """ |
|
282 | """ | |
278 | ).tag(config=True) |
|
283 | ).tag(config=True) | |
279 |
|
284 | |||
280 |
|
285 | |||
281 | def __init__(self, namespace=None, global_namespace=None, **kwargs): |
|
286 | def __init__(self, namespace=None, global_namespace=None, **kwargs): | |
282 | """Create a new completer for the command line. |
|
287 | """Create a new completer for the command line. | |
283 |
|
288 | |||
284 | Completer(namespace=ns, global_namespace=ns2) -> completer instance. |
|
289 | Completer(namespace=ns, global_namespace=ns2) -> completer instance. | |
285 |
|
290 | |||
286 | If unspecified, the default namespace where completions are performed |
|
291 | If unspecified, the default namespace where completions are performed | |
287 | is __main__ (technically, __main__.__dict__). Namespaces should be |
|
292 | is __main__ (technically, __main__.__dict__). Namespaces should be | |
288 | given as dictionaries. |
|
293 | given as dictionaries. | |
289 |
|
294 | |||
290 | An optional second namespace can be given. This allows the completer |
|
295 | 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 |
|
296 | to handle cases where both the local and global scopes need to be | |
292 | distinguished. |
|
297 | distinguished. | |
293 |
|
298 | |||
294 | Completer instances should be used as the completion mechanism of |
|
299 | Completer instances should be used as the completion mechanism of | |
295 | readline via the set_completer() call: |
|
300 | readline via the set_completer() call: | |
296 |
|
301 | |||
297 | readline.set_completer(Completer(my_namespace).complete) |
|
302 | readline.set_completer(Completer(my_namespace).complete) | |
298 | """ |
|
303 | """ | |
299 |
|
304 | |||
300 | # Don't bind to namespace quite yet, but flag whether the user wants a |
|
305 | # 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 |
|
306 | # specific namespace or to use __main__.__dict__. This will allow us | |
302 | # to bind to __main__.__dict__ at completion time, not now. |
|
307 | # to bind to __main__.__dict__ at completion time, not now. | |
303 | if namespace is None: |
|
308 | if namespace is None: | |
304 | self.use_main_ns = 1 |
|
309 | self.use_main_ns = 1 | |
305 | else: |
|
310 | else: | |
306 | self.use_main_ns = 0 |
|
311 | self.use_main_ns = 0 | |
307 | self.namespace = namespace |
|
312 | self.namespace = namespace | |
308 |
|
313 | |||
309 | # The global namespace, if given, can be bound directly |
|
314 | # The global namespace, if given, can be bound directly | |
310 | if global_namespace is None: |
|
315 | if global_namespace is None: | |
311 | self.global_namespace = {} |
|
316 | self.global_namespace = {} | |
312 | else: |
|
317 | else: | |
313 | self.global_namespace = global_namespace |
|
318 | self.global_namespace = global_namespace | |
314 |
|
319 | |||
315 | super(Completer, self).__init__(**kwargs) |
|
320 | super(Completer, self).__init__(**kwargs) | |
316 |
|
321 | |||
317 | def complete(self, text, state): |
|
322 | def complete(self, text, state): | |
318 | """Return the next possible completion for 'text'. |
|
323 | """Return the next possible completion for 'text'. | |
319 |
|
324 | |||
320 | This is called successively with state == 0, 1, 2, ... until it |
|
325 | This is called successively with state == 0, 1, 2, ... until it | |
321 | returns None. The completion should begin with 'text'. |
|
326 | returns None. The completion should begin with 'text'. | |
322 |
|
327 | |||
323 | """ |
|
328 | """ | |
324 | if self.use_main_ns: |
|
329 | if self.use_main_ns: | |
325 | self.namespace = __main__.__dict__ |
|
330 | self.namespace = __main__.__dict__ | |
326 |
|
331 | |||
327 | if state == 0: |
|
332 | if state == 0: | |
328 | if "." in text: |
|
333 | if "." in text: | |
329 | self.matches = self.attr_matches(text) |
|
334 | self.matches = self.attr_matches(text) | |
330 | else: |
|
335 | else: | |
331 | self.matches = self.global_matches(text) |
|
336 | self.matches = self.global_matches(text) | |
332 | try: |
|
337 | try: | |
333 | return self.matches[state] |
|
338 | return self.matches[state] | |
334 | except IndexError: |
|
339 | except IndexError: | |
335 | return None |
|
340 | return None | |
336 |
|
341 | |||
337 | def global_matches(self, text): |
|
342 | def global_matches(self, text): | |
338 | """Compute matches when text is a simple name. |
|
343 | """Compute matches when text is a simple name. | |
339 |
|
344 | |||
340 | Return a list of all keywords, built-in functions and names currently |
|
345 | Return a list of all keywords, built-in functions and names currently | |
341 | defined in self.namespace or self.global_namespace that match. |
|
346 | defined in self.namespace or self.global_namespace that match. | |
342 |
|
347 | |||
343 | """ |
|
348 | """ | |
344 | matches = [] |
|
349 | matches = [] | |
345 | match_append = matches.append |
|
350 | match_append = matches.append | |
346 | n = len(text) |
|
351 | n = len(text) | |
347 | for lst in [keyword.kwlist, |
|
352 | for lst in [keyword.kwlist, | |
348 | builtin_mod.__dict__.keys(), |
|
353 | builtin_mod.__dict__.keys(), | |
349 | self.namespace.keys(), |
|
354 | self.namespace.keys(), | |
350 | self.global_namespace.keys()]: |
|
355 | self.global_namespace.keys()]: | |
351 | for word in lst: |
|
356 | for word in lst: | |
352 | if word[:n] == text and word != "__builtins__": |
|
357 | if word[:n] == text and word != "__builtins__": | |
353 | match_append(word) |
|
358 | match_append(word) | |
354 | return [cast_unicode_py2(m) for m in matches] |
|
359 | return [cast_unicode_py2(m) for m in matches] | |
355 |
|
360 | |||
356 | def attr_matches(self, text): |
|
361 | def attr_matches(self, text): | |
357 | """Compute matches when text contains a dot. |
|
362 | """Compute matches when text contains a dot. | |
358 |
|
363 | |||
359 | Assuming the text is of the form NAME.NAME....[NAME], and is |
|
364 | Assuming the text is of the form NAME.NAME....[NAME], and is | |
360 | evaluatable in self.namespace or self.global_namespace, it will be |
|
365 | evaluatable in self.namespace or self.global_namespace, it will be | |
361 | evaluated and its attributes (as revealed by dir()) are used as |
|
366 | evaluated and its attributes (as revealed by dir()) are used as | |
362 | possible completions. (For class instances, class members are are |
|
367 | possible completions. (For class instances, class members are are | |
363 | also considered.) |
|
368 | also considered.) | |
364 |
|
369 | |||
365 | WARNING: this can still invoke arbitrary C code, if an object |
|
370 | WARNING: this can still invoke arbitrary C code, if an object | |
366 | with a __getattr__ hook is evaluated. |
|
371 | with a __getattr__ hook is evaluated. | |
367 |
|
372 | |||
368 | """ |
|
373 | """ | |
369 |
|
374 | |||
370 | # Another option, seems to work great. Catches things like ''.<tab> |
|
375 | # Another option, seems to work great. Catches things like ''.<tab> | |
371 | m = re.match(r"(\S+(\.\w+)*)\.(\w*)$", text) |
|
376 | m = re.match(r"(\S+(\.\w+)*)\.(\w*)$", text) | |
372 |
|
377 | |||
373 | if m: |
|
378 | if m: | |
374 | expr, attr = m.group(1, 3) |
|
379 | expr, attr = m.group(1, 3) | |
375 | elif self.greedy: |
|
380 | elif self.greedy: | |
376 | m2 = re.match(r"(.+)\.(\w*)$", self.line_buffer) |
|
381 | m2 = re.match(r"(.+)\.(\w*)$", self.line_buffer) | |
377 | if not m2: |
|
382 | if not m2: | |
378 | return [] |
|
383 | return [] | |
379 | expr, attr = m2.group(1,2) |
|
384 | expr, attr = m2.group(1,2) | |
380 | else: |
|
385 | else: | |
381 | return [] |
|
386 | return [] | |
382 |
|
387 | |||
383 | try: |
|
388 | try: | |
384 | obj = eval(expr, self.namespace) |
|
389 | obj = eval(expr, self.namespace) | |
385 | except: |
|
390 | except: | |
386 | try: |
|
391 | try: | |
387 | obj = eval(expr, self.global_namespace) |
|
392 | obj = eval(expr, self.global_namespace) | |
388 | except: |
|
393 | except: | |
389 | return [] |
|
394 | return [] | |
390 |
|
395 | |||
391 | if self.limit_to__all__ and hasattr(obj, '__all__'): |
|
396 | if self.limit_to__all__ and hasattr(obj, '__all__'): | |
392 | words = get__all__entries(obj) |
|
397 | words = get__all__entries(obj) | |
393 | else: |
|
398 | else: | |
394 | words = dir2(obj) |
|
399 | words = dir2(obj) | |
395 |
|
400 | |||
396 | try: |
|
401 | try: | |
397 | words = generics.complete_object(obj, words) |
|
402 | words = generics.complete_object(obj, words) | |
398 | except TryNext: |
|
403 | except TryNext: | |
399 | pass |
|
404 | pass | |
400 | except Exception: |
|
405 | except Exception: | |
401 | # Silence errors from completion function |
|
406 | # Silence errors from completion function | |
402 | #raise # dbg |
|
407 | #raise # dbg | |
403 | pass |
|
408 | pass | |
404 | # Build match list to return |
|
409 | # Build match list to return | |
405 | n = len(attr) |
|
410 | n = len(attr) | |
406 | return [u"%s.%s" % (expr, w) for w in words if w[:n] == attr ] |
|
411 | return [u"%s.%s" % (expr, w) for w in words if w[:n] == attr ] | |
407 |
|
412 | |||
408 |
|
413 | |||
409 | def get__all__entries(obj): |
|
414 | def get__all__entries(obj): | |
410 | """returns the strings in the __all__ attribute""" |
|
415 | """returns the strings in the __all__ attribute""" | |
411 | try: |
|
416 | try: | |
412 | words = getattr(obj, '__all__') |
|
417 | words = getattr(obj, '__all__') | |
413 | except: |
|
418 | except: | |
414 | return [] |
|
419 | return [] | |
415 |
|
420 | |||
416 | return [cast_unicode_py2(w) for w in words if isinstance(w, string_types)] |
|
421 | return [cast_unicode_py2(w) for w in words if isinstance(w, string_types)] | |
417 |
|
422 | |||
418 |
|
423 | |||
419 | def match_dict_keys(keys, prefix, delims): |
|
424 | def match_dict_keys(keys, prefix, delims): | |
420 | """Used by dict_key_matches, matching the prefix to a list of keys""" |
|
425 | """Used by dict_key_matches, matching the prefix to a list of keys""" | |
421 | if not prefix: |
|
426 | if not prefix: | |
422 | return None, 0, [repr(k) for k in keys |
|
427 | return None, 0, [repr(k) for k in keys | |
423 | if isinstance(k, (string_types, bytes))] |
|
428 | if isinstance(k, (string_types, bytes))] | |
424 | quote_match = re.search('["\']', prefix) |
|
429 | quote_match = re.search('["\']', prefix) | |
425 | quote = quote_match.group() |
|
430 | quote = quote_match.group() | |
426 | try: |
|
431 | try: | |
427 | prefix_str = eval(prefix + quote, {}) |
|
432 | prefix_str = eval(prefix + quote, {}) | |
428 | except Exception: |
|
433 | except Exception: | |
429 | return None, 0, [] |
|
434 | return None, 0, [] | |
430 |
|
435 | |||
431 | pattern = '[^' + ''.join('\\' + c for c in delims) + ']*$' |
|
436 | pattern = '[^' + ''.join('\\' + c for c in delims) + ']*$' | |
432 | token_match = re.search(pattern, prefix, re.UNICODE) |
|
437 | token_match = re.search(pattern, prefix, re.UNICODE) | |
433 | token_start = token_match.start() |
|
438 | token_start = token_match.start() | |
434 | token_prefix = token_match.group() |
|
439 | token_prefix = token_match.group() | |
435 |
|
440 | |||
436 | # TODO: support bytes in Py3k |
|
441 | # TODO: support bytes in Py3k | |
437 | matched = [] |
|
442 | matched = [] | |
438 | for key in keys: |
|
443 | for key in keys: | |
439 | try: |
|
444 | try: | |
440 | if not key.startswith(prefix_str): |
|
445 | if not key.startswith(prefix_str): | |
441 | continue |
|
446 | continue | |
442 | except (AttributeError, TypeError, UnicodeError): |
|
447 | except (AttributeError, TypeError, UnicodeError): | |
443 | # Python 3+ TypeError on b'a'.startswith('a') or vice-versa |
|
448 | # Python 3+ TypeError on b'a'.startswith('a') or vice-versa | |
444 | continue |
|
449 | continue | |
445 |
|
450 | |||
446 | # reformat remainder of key to begin with prefix |
|
451 | # reformat remainder of key to begin with prefix | |
447 | rem = key[len(prefix_str):] |
|
452 | rem = key[len(prefix_str):] | |
448 | # force repr wrapped in ' |
|
453 | # force repr wrapped in ' | |
449 | rem_repr = repr(rem + '"') |
|
454 | rem_repr = repr(rem + '"') | |
450 | if rem_repr.startswith('u') and prefix[0] not in 'uU': |
|
455 | if rem_repr.startswith('u') and prefix[0] not in 'uU': | |
451 | # Found key is unicode, but prefix is Py2 string. |
|
456 | # Found key is unicode, but prefix is Py2 string. | |
452 | # Therefore attempt to interpret key as string. |
|
457 | # Therefore attempt to interpret key as string. | |
453 | try: |
|
458 | try: | |
454 | rem_repr = repr(rem.encode('ascii') + '"') |
|
459 | rem_repr = repr(rem.encode('ascii') + '"') | |
455 | except UnicodeEncodeError: |
|
460 | except UnicodeEncodeError: | |
456 | continue |
|
461 | continue | |
457 |
|
462 | |||
458 | rem_repr = rem_repr[1 + rem_repr.index("'"):-2] |
|
463 | rem_repr = rem_repr[1 + rem_repr.index("'"):-2] | |
459 | if quote == '"': |
|
464 | if quote == '"': | |
460 | # The entered prefix is quoted with ", |
|
465 | # The entered prefix is quoted with ", | |
461 | # but the match is quoted with '. |
|
466 | # but the match is quoted with '. | |
462 | # A contained " hence needs escaping for comparison: |
|
467 | # A contained " hence needs escaping for comparison: | |
463 | rem_repr = rem_repr.replace('"', '\\"') |
|
468 | rem_repr = rem_repr.replace('"', '\\"') | |
464 |
|
469 | |||
465 | # then reinsert prefix from start of token |
|
470 | # then reinsert prefix from start of token | |
466 | matched.append('%s%s' % (token_prefix, rem_repr)) |
|
471 | matched.append('%s%s' % (token_prefix, rem_repr)) | |
467 | return quote, token_start, matched |
|
472 | return quote, token_start, matched | |
468 |
|
473 | |||
469 |
|
474 | |||
470 | def _safe_isinstance(obj, module, class_name): |
|
475 | def _safe_isinstance(obj, module, class_name): | |
471 | """Checks if obj is an instance of module.class_name if loaded |
|
476 | """Checks if obj is an instance of module.class_name if loaded | |
472 | """ |
|
477 | """ | |
473 | return (module in sys.modules and |
|
478 | return (module in sys.modules and | |
474 | isinstance(obj, getattr(__import__(module), class_name))) |
|
479 | isinstance(obj, getattr(__import__(module), class_name))) | |
475 |
|
480 | |||
476 |
|
481 | |||
477 | def back_unicode_name_matches(text): |
|
482 | def back_unicode_name_matches(text): | |
478 | u"""Match unicode characters back to unicode name |
|
483 | u"""Match unicode characters back to unicode name | |
479 |
|
484 | |||
480 | This does β -> \\snowman |
|
485 | This does β -> \\snowman | |
481 |
|
486 | |||
482 | Note that snowman is not a valid python3 combining character but will be expanded. |
|
487 | 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. |
|
488 | Though it will not recombine back to the snowman character by the completion machinery. | |
484 |
|
489 | |||
485 | This will not either back-complete standard sequences like \\n, \\b ... |
|
490 | This will not either back-complete standard sequences like \\n, \\b ... | |
486 |
|
491 | |||
487 | Used on Python 3 only. |
|
492 | Used on Python 3 only. | |
488 | """ |
|
493 | """ | |
489 | if len(text)<2: |
|
494 | if len(text)<2: | |
490 | return u'', () |
|
495 | return u'', () | |
491 | maybe_slash = text[-2] |
|
496 | maybe_slash = text[-2] | |
492 | if maybe_slash != '\\': |
|
497 | if maybe_slash != '\\': | |
493 | return u'', () |
|
498 | return u'', () | |
494 |
|
499 | |||
495 | char = text[-1] |
|
500 | char = text[-1] | |
496 | # no expand on quote for completion in strings. |
|
501 | # no expand on quote for completion in strings. | |
497 | # nor backcomplete standard ascii keys |
|
502 | # nor backcomplete standard ascii keys | |
498 | if char in string.ascii_letters or char in ['"',"'"]: |
|
503 | if char in string.ascii_letters or char in ['"',"'"]: | |
499 | return u'', () |
|
504 | return u'', () | |
500 | try : |
|
505 | try : | |
501 | unic = unicodedata.name(char) |
|
506 | unic = unicodedata.name(char) | |
502 | return '\\'+char,['\\'+unic] |
|
507 | return '\\'+char,['\\'+unic] | |
503 | except KeyError: |
|
508 | except KeyError: | |
504 | pass |
|
509 | pass | |
505 | return u'', () |
|
510 | return u'', () | |
506 |
|
511 | |||
507 | def back_latex_name_matches(text): |
|
512 | def back_latex_name_matches(text): | |
508 | u"""Match latex characters back to unicode name |
|
513 | u"""Match latex characters back to unicode name | |
509 |
|
514 | |||
510 | This does ->\\sqrt |
|
515 | This does ->\\sqrt | |
511 |
|
516 | |||
512 | Used on Python 3 only. |
|
517 | Used on Python 3 only. | |
513 | """ |
|
518 | """ | |
514 | if len(text)<2: |
|
519 | if len(text)<2: | |
515 | return u'', () |
|
520 | return u'', () | |
516 | maybe_slash = text[-2] |
|
521 | maybe_slash = text[-2] | |
517 | if maybe_slash != '\\': |
|
522 | if maybe_slash != '\\': | |
518 | return u'', () |
|
523 | return u'', () | |
519 |
|
524 | |||
520 |
|
525 | |||
521 | char = text[-1] |
|
526 | char = text[-1] | |
522 | # no expand on quote for completion in strings. |
|
527 | # no expand on quote for completion in strings. | |
523 | # nor backcomplete standard ascii keys |
|
528 | # nor backcomplete standard ascii keys | |
524 | if char in string.ascii_letters or char in ['"',"'"]: |
|
529 | if char in string.ascii_letters or char in ['"',"'"]: | |
525 | return u'', () |
|
530 | return u'', () | |
526 | try : |
|
531 | try : | |
527 | latex = reverse_latex_symbol[char] |
|
532 | latex = reverse_latex_symbol[char] | |
528 | # '\\' replace the \ as well |
|
533 | # '\\' replace the \ as well | |
529 | return '\\'+char,[latex] |
|
534 | return '\\'+char,[latex] | |
530 | except KeyError: |
|
535 | except KeyError: | |
531 | pass |
|
536 | pass | |
532 | return u'', () |
|
537 | return u'', () | |
533 |
|
538 | |||
534 |
|
539 | |||
535 | class IPCompleter(Completer): |
|
540 | class IPCompleter(Completer): | |
536 | """Extension of the completer class with IPython-specific features""" |
|
541 | """Extension of the completer class with IPython-specific features""" | |
537 |
|
542 | |||
538 | @observe('greedy') |
|
543 | @observe('greedy') | |
539 | def _greedy_changed(self, change): |
|
544 | def _greedy_changed(self, change): | |
540 | """update the splitter and readline delims when greedy is changed""" |
|
545 | """update the splitter and readline delims when greedy is changed""" | |
541 | if change['new']: |
|
546 | if change['new']: | |
542 | self.splitter.delims = GREEDY_DELIMS |
|
547 | self.splitter.delims = GREEDY_DELIMS | |
543 | else: |
|
548 | else: | |
544 | self.splitter.delims = DELIMS |
|
549 | self.splitter.delims = DELIMS | |
545 |
|
550 | |||
546 | if self.readline: |
|
551 | if self.readline: | |
547 | self.readline.set_completer_delims(self.splitter.delims) |
|
552 | self.readline.set_completer_delims(self.splitter.delims) | |
548 |
|
553 | |||
549 | merge_completions = Bool(True, |
|
554 | merge_completions = Bool(True, | |
550 | help="""Whether to merge completion results into a single list |
|
555 | help="""Whether to merge completion results into a single list | |
551 |
|
556 | |||
552 | If False, only the completion results from the first non-empty |
|
557 | If False, only the completion results from the first non-empty | |
553 | completer will be returned. |
|
558 | completer will be returned. | |
554 | """ |
|
559 | """ | |
555 | ).tag(config=True) |
|
560 | ).tag(config=True) | |
556 | omit__names = Enum((0,1,2), default_value=2, |
|
561 | omit__names = Enum((0,1,2), default_value=2, | |
557 | help="""Instruct the completer to omit private method names |
|
562 | help="""Instruct the completer to omit private method names | |
558 |
|
563 | |||
559 | Specifically, when completing on ``object.<tab>``. |
|
564 | Specifically, when completing on ``object.<tab>``. | |
560 |
|
565 | |||
561 | When 2 [default]: all names that start with '_' will be excluded. |
|
566 | When 2 [default]: all names that start with '_' will be excluded. | |
562 |
|
567 | |||
563 | When 1: all 'magic' names (``__foo__``) will be excluded. |
|
568 | When 1: all 'magic' names (``__foo__``) will be excluded. | |
564 |
|
569 | |||
565 | When 0: nothing will be excluded. |
|
570 | When 0: nothing will be excluded. | |
566 | """ |
|
571 | """ | |
567 | ).tag(config=True) |
|
572 | ).tag(config=True) | |
568 | limit_to__all__ = Bool(False, |
|
573 | limit_to__all__ = Bool(False, | |
569 | help=""" |
|
574 | help=""" | |
570 | DEPRECATED as of version 5.0. |
|
575 | DEPRECATED as of version 5.0. | |
571 |
|
576 | |||
572 | Instruct the completer to use __all__ for the completion |
|
577 | Instruct the completer to use __all__ for the completion | |
573 |
|
578 | |||
574 | Specifically, when completing on ``object.<tab>``. |
|
579 | Specifically, when completing on ``object.<tab>``. | |
575 |
|
580 | |||
576 | When True: only those names in obj.__all__ will be included. |
|
581 | When True: only those names in obj.__all__ will be included. | |
577 |
|
582 | |||
578 | When False [default]: the __all__ attribute is ignored |
|
583 | When False [default]: the __all__ attribute is ignored | |
579 | """, |
|
584 | """, | |
580 | ).tag(config=True) |
|
585 | ).tag(config=True) | |
581 |
|
586 | |||
582 | def __init__(self, shell=None, namespace=None, global_namespace=None, |
|
587 | def __init__(self, shell=None, namespace=None, global_namespace=None, | |
583 | use_readline=True, config=None, **kwargs): |
|
588 | use_readline=True, config=None, **kwargs): | |
584 | """IPCompleter() -> completer |
|
589 | """IPCompleter() -> completer | |
585 |
|
590 | |||
586 | Return a completer object suitable for use by the readline library |
|
591 | Return a completer object suitable for use by the readline library | |
587 | via readline.set_completer(). |
|
592 | via readline.set_completer(). | |
588 |
|
593 | |||
589 | Inputs: |
|
594 | Inputs: | |
590 |
|
595 | |||
591 | - shell: a pointer to the ipython shell itself. This is needed |
|
596 | - shell: a pointer to the ipython shell itself. This is needed | |
592 | because this completer knows about magic functions, and those can |
|
597 | because this completer knows about magic functions, and those can | |
593 | only be accessed via the ipython instance. |
|
598 | only be accessed via the ipython instance. | |
594 |
|
599 | |||
595 | - namespace: an optional dict where completions are performed. |
|
600 | - namespace: an optional dict where completions are performed. | |
596 |
|
601 | |||
597 | - global_namespace: secondary optional dict for completions, to |
|
602 | - global_namespace: secondary optional dict for completions, to | |
598 | handle cases (such as IPython embedded inside functions) where |
|
603 | handle cases (such as IPython embedded inside functions) where | |
599 | both Python scopes are visible. |
|
604 | both Python scopes are visible. | |
600 |
|
605 | |||
601 | use_readline : bool, optional |
|
606 | use_readline : bool, optional | |
602 | If true, use the readline library. This completer can still function |
|
607 | If true, use the readline library. This completer can still function | |
603 | without readline, though in that case callers must provide some extra |
|
608 | without readline, though in that case callers must provide some extra | |
604 | information on each call about the current line.""" |
|
609 | information on each call about the current line.""" | |
605 |
|
610 | |||
606 | self.magic_escape = ESC_MAGIC |
|
611 | self.magic_escape = ESC_MAGIC | |
607 | self.splitter = CompletionSplitter() |
|
612 | self.splitter = CompletionSplitter() | |
608 |
|
613 | |||
609 | # Readline configuration, only used by the rlcompleter method. |
|
614 | # Readline configuration, only used by the rlcompleter method. | |
610 | if use_readline: |
|
615 | if use_readline: | |
611 | # We store the right version of readline so that later code |
|
616 | # We store the right version of readline so that later code | |
612 | import IPython.utils.rlineimpl as readline |
|
617 | import IPython.utils.rlineimpl as readline | |
613 | self.readline = readline |
|
618 | self.readline = readline | |
614 | else: |
|
619 | else: | |
615 | self.readline = None |
|
620 | self.readline = None | |
616 |
|
621 | |||
617 | # _greedy_changed() depends on splitter and readline being defined: |
|
622 | # _greedy_changed() depends on splitter and readline being defined: | |
618 | Completer.__init__(self, namespace=namespace, global_namespace=global_namespace, |
|
623 | Completer.__init__(self, namespace=namespace, global_namespace=global_namespace, | |
619 | config=config, **kwargs) |
|
624 | config=config, **kwargs) | |
620 |
|
625 | |||
621 | # List where completion matches will be stored |
|
626 | # List where completion matches will be stored | |
622 | self.matches = [] |
|
627 | self.matches = [] | |
623 | self.shell = shell |
|
628 | self.shell = shell | |
624 | # Regexp to split filenames with spaces in them |
|
629 | # Regexp to split filenames with spaces in them | |
625 | self.space_name_re = re.compile(r'([^\\] )') |
|
630 | self.space_name_re = re.compile(r'([^\\] )') | |
626 | # Hold a local ref. to glob.glob for speed |
|
631 | # Hold a local ref. to glob.glob for speed | |
627 | self.glob = glob.glob |
|
632 | self.glob = glob.glob | |
628 |
|
633 | |||
629 | # Determine if we are running on 'dumb' terminals, like (X)Emacs |
|
634 | # Determine if we are running on 'dumb' terminals, like (X)Emacs | |
630 | # buffers, to avoid completion problems. |
|
635 | # buffers, to avoid completion problems. | |
631 | term = os.environ.get('TERM','xterm') |
|
636 | term = os.environ.get('TERM','xterm') | |
632 | self.dumb_terminal = term in ['dumb','emacs'] |
|
637 | self.dumb_terminal = term in ['dumb','emacs'] | |
633 |
|
638 | |||
634 | # Special handling of backslashes needed in win32 platforms |
|
639 | # Special handling of backslashes needed in win32 platforms | |
635 | if sys.platform == "win32": |
|
640 | if sys.platform == "win32": | |
636 | self.clean_glob = self._clean_glob_win32 |
|
641 | self.clean_glob = self._clean_glob_win32 | |
637 | else: |
|
642 | else: | |
638 | self.clean_glob = self._clean_glob |
|
643 | self.clean_glob = self._clean_glob | |
639 |
|
644 | |||
640 | #regexp to parse docstring for function signature |
|
645 | #regexp to parse docstring for function signature | |
641 | self.docstring_sig_re = re.compile(r'^[\w|\s.]+\(([^)]*)\).*') |
|
646 | self.docstring_sig_re = re.compile(r'^[\w|\s.]+\(([^)]*)\).*') | |
642 | self.docstring_kwd_re = re.compile(r'[\s|\[]*(\w+)(?:\s*=\s*.*)') |
|
647 | self.docstring_kwd_re = re.compile(r'[\s|\[]*(\w+)(?:\s*=\s*.*)') | |
643 | #use this if positional argument name is also needed |
|
648 | #use this if positional argument name is also needed | |
644 | #= re.compile(r'[\s|\[]*(\w+)(?:\s*=?\s*.*)') |
|
649 | #= re.compile(r'[\s|\[]*(\w+)(?:\s*=?\s*.*)') | |
645 |
|
650 | |||
646 | # All active matcher routines for completion |
|
651 | # All active matcher routines for completion | |
647 | self.matchers = [ |
|
652 | self.matchers = [ | |
648 | self.python_matches, |
|
653 | self.python_matches, | |
649 | self.file_matches, |
|
654 | self.file_matches, | |
650 | self.magic_matches, |
|
655 | self.magic_matches, | |
651 | self.python_func_kw_matches, |
|
656 | self.python_func_kw_matches, | |
652 | self.dict_key_matches, |
|
657 | self.dict_key_matches, | |
653 | ] |
|
658 | ] | |
654 |
|
659 | |||
655 | # This is set externally by InteractiveShell |
|
660 | # This is set externally by InteractiveShell | |
656 | self.custom_completers = None |
|
661 | self.custom_completers = None | |
657 |
|
662 | |||
658 | def all_completions(self, text): |
|
663 | def all_completions(self, text): | |
659 | """ |
|
664 | """ | |
660 | Wrapper around the complete method for the benefit of emacs. |
|
665 | Wrapper around the complete method for the benefit of emacs. | |
661 | """ |
|
666 | """ | |
662 | return self.complete(text)[1] |
|
667 | return self.complete(text)[1] | |
663 |
|
668 | |||
664 | def _clean_glob(self, text): |
|
669 | def _clean_glob(self, text): | |
665 | return self.glob("%s*" % text) |
|
670 | return self.glob("%s*" % text) | |
666 |
|
671 | |||
667 | def _clean_glob_win32(self,text): |
|
672 | def _clean_glob_win32(self,text): | |
668 | return [f.replace("\\","/") |
|
673 | return [f.replace("\\","/") | |
669 | for f in self.glob("%s*" % text)] |
|
674 | for f in self.glob("%s*" % text)] | |
670 |
|
675 | |||
671 | def file_matches(self, text): |
|
676 | def file_matches(self, text): | |
672 | """Match filenames, expanding ~USER type strings. |
|
677 | """Match filenames, expanding ~USER type strings. | |
673 |
|
678 | |||
674 | Most of the seemingly convoluted logic in this completer is an |
|
679 | Most of the seemingly convoluted logic in this completer is an | |
675 | attempt to handle filenames with spaces in them. And yet it's not |
|
680 | attempt to handle filenames with spaces in them. And yet it's not | |
676 | quite perfect, because Python's readline doesn't expose all of the |
|
681 | quite perfect, because Python's readline doesn't expose all of the | |
677 | GNU readline details needed for this to be done correctly. |
|
682 | GNU readline details needed for this to be done correctly. | |
678 |
|
683 | |||
679 | For a filename with a space in it, the printed completions will be |
|
684 | For a filename with a space in it, the printed completions will be | |
680 | only the parts after what's already been typed (instead of the |
|
685 | only the parts after what's already been typed (instead of the | |
681 | full completions, as is normally done). I don't think with the |
|
686 | full completions, as is normally done). I don't think with the | |
682 | current (as of Python 2.3) Python readline it's possible to do |
|
687 | current (as of Python 2.3) Python readline it's possible to do | |
683 | better.""" |
|
688 | better.""" | |
684 |
|
689 | |||
685 | # chars that require escaping with backslash - i.e. chars |
|
690 | # chars that require escaping with backslash - i.e. chars | |
686 | # that readline treats incorrectly as delimiters, but we |
|
691 | # that readline treats incorrectly as delimiters, but we | |
687 | # don't want to treat as delimiters in filename matching |
|
692 | # don't want to treat as delimiters in filename matching | |
688 | # when escaped with backslash |
|
693 | # when escaped with backslash | |
689 | if text.startswith('!'): |
|
694 | if text.startswith('!'): | |
690 | text = text[1:] |
|
695 | text = text[1:] | |
691 | text_prefix = u'!' |
|
696 | text_prefix = u'!' | |
692 | else: |
|
697 | else: | |
693 | text_prefix = u'' |
|
698 | text_prefix = u'' | |
694 |
|
699 | |||
695 | text_until_cursor = self.text_until_cursor |
|
700 | text_until_cursor = self.text_until_cursor | |
696 | # track strings with open quotes |
|
701 | # track strings with open quotes | |
697 | open_quotes = has_open_quotes(text_until_cursor) |
|
702 | open_quotes = has_open_quotes(text_until_cursor) | |
698 |
|
703 | |||
699 | if '(' in text_until_cursor or '[' in text_until_cursor: |
|
704 | if '(' in text_until_cursor or '[' in text_until_cursor: | |
700 | lsplit = text |
|
705 | lsplit = text | |
701 | else: |
|
706 | else: | |
702 | try: |
|
707 | try: | |
703 | # arg_split ~ shlex.split, but with unicode bugs fixed by us |
|
708 | # arg_split ~ shlex.split, but with unicode bugs fixed by us | |
704 | lsplit = arg_split(text_until_cursor)[-1] |
|
709 | lsplit = arg_split(text_until_cursor)[-1] | |
705 | except ValueError: |
|
710 | except ValueError: | |
706 | # typically an unmatched ", or backslash without escaped char. |
|
711 | # typically an unmatched ", or backslash without escaped char. | |
707 | if open_quotes: |
|
712 | if open_quotes: | |
708 | lsplit = text_until_cursor.split(open_quotes)[-1] |
|
713 | lsplit = text_until_cursor.split(open_quotes)[-1] | |
709 | else: |
|
714 | else: | |
710 | return [] |
|
715 | return [] | |
711 | except IndexError: |
|
716 | except IndexError: | |
712 | # tab pressed on empty line |
|
717 | # tab pressed on empty line | |
713 | lsplit = "" |
|
718 | lsplit = "" | |
714 |
|
719 | |||
715 | if not open_quotes and lsplit != protect_filename(lsplit): |
|
720 | if not open_quotes and lsplit != protect_filename(lsplit): | |
716 | # if protectables are found, do matching on the whole escaped name |
|
721 | # if protectables are found, do matching on the whole escaped name | |
717 | has_protectables = True |
|
722 | has_protectables = True | |
718 | text0,text = text,lsplit |
|
723 | text0,text = text,lsplit | |
719 | else: |
|
724 | else: | |
720 | has_protectables = False |
|
725 | has_protectables = False | |
721 | text = os.path.expanduser(text) |
|
726 | text = os.path.expanduser(text) | |
722 |
|
727 | |||
723 | if text == "": |
|
728 | if text == "": | |
724 | return [text_prefix + cast_unicode_py2(protect_filename(f)) for f in self.glob("*")] |
|
729 | return [text_prefix + cast_unicode_py2(protect_filename(f)) for f in self.glob("*")] | |
725 |
|
730 | |||
726 | # Compute the matches from the filesystem |
|
731 | # Compute the matches from the filesystem | |
727 | m0 = self.clean_glob(text.replace('\\','')) |
|
732 | m0 = self.clean_glob(text.replace('\\','')) | |
728 |
|
733 | |||
729 | if has_protectables: |
|
734 | if has_protectables: | |
730 | # If we had protectables, we need to revert our changes to the |
|
735 | # If we had protectables, we need to revert our changes to the | |
731 | # beginning of filename so that we don't double-write the part |
|
736 | # beginning of filename so that we don't double-write the part | |
732 | # of the filename we have so far |
|
737 | # of the filename we have so far | |
733 | len_lsplit = len(lsplit) |
|
738 | len_lsplit = len(lsplit) | |
734 | matches = [text_prefix + text0 + |
|
739 | matches = [text_prefix + text0 + | |
735 | protect_filename(f[len_lsplit:]) for f in m0] |
|
740 | protect_filename(f[len_lsplit:]) for f in m0] | |
736 | else: |
|
741 | else: | |
737 | if open_quotes: |
|
742 | if open_quotes: | |
738 | # if we have a string with an open quote, we don't need to |
|
743 | # if we have a string with an open quote, we don't need to | |
739 | # protect the names at all (and we _shouldn't_, as it |
|
744 | # protect the names at all (and we _shouldn't_, as it | |
740 | # would cause bugs when the filesystem call is made). |
|
745 | # would cause bugs when the filesystem call is made). | |
741 | matches = m0 |
|
746 | matches = m0 | |
742 | else: |
|
747 | else: | |
743 | matches = [text_prefix + |
|
748 | matches = [text_prefix + | |
744 | protect_filename(f) for f in m0] |
|
749 | protect_filename(f) for f in m0] | |
745 |
|
750 | |||
746 | # Mark directories in input list by appending '/' to their names. |
|
751 | # Mark directories in input list by appending '/' to their names. | |
747 | return [cast_unicode_py2(x+'/') if os.path.isdir(x) else x for x in matches] |
|
752 | return [cast_unicode_py2(x+'/') if os.path.isdir(x) else x for x in matches] | |
748 |
|
753 | |||
749 | def magic_matches(self, text): |
|
754 | def magic_matches(self, text): | |
750 | """Match magics""" |
|
755 | """Match magics""" | |
751 | # Get all shell magics now rather than statically, so magics loaded at |
|
756 | # Get all shell magics now rather than statically, so magics loaded at | |
752 | # runtime show up too. |
|
757 | # runtime show up too. | |
753 | lsm = self.shell.magics_manager.lsmagic() |
|
758 | lsm = self.shell.magics_manager.lsmagic() | |
754 | line_magics = lsm['line'] |
|
759 | line_magics = lsm['line'] | |
755 | cell_magics = lsm['cell'] |
|
760 | cell_magics = lsm['cell'] | |
756 | pre = self.magic_escape |
|
761 | pre = self.magic_escape | |
757 | pre2 = pre+pre |
|
762 | pre2 = pre+pre | |
758 |
|
763 | |||
759 | # Completion logic: |
|
764 | # Completion logic: | |
760 | # - user gives %%: only do cell magics |
|
765 | # - user gives %%: only do cell magics | |
761 | # - user gives %: do both line and cell magics |
|
766 | # - user gives %: do both line and cell magics | |
762 | # - no prefix: do both |
|
767 | # - no prefix: do both | |
763 | # In other words, line magics are skipped if the user gives %% explicitly |
|
768 | # In other words, line magics are skipped if the user gives %% explicitly | |
764 | bare_text = text.lstrip(pre) |
|
769 | bare_text = text.lstrip(pre) | |
765 | comp = [ pre2+m for m in cell_magics if m.startswith(bare_text)] |
|
770 | comp = [ pre2+m for m in cell_magics if m.startswith(bare_text)] | |
766 | if not text.startswith(pre2): |
|
771 | if not text.startswith(pre2): | |
767 | comp += [ pre+m for m in line_magics if m.startswith(bare_text)] |
|
772 | comp += [ pre+m for m in line_magics if m.startswith(bare_text)] | |
768 | return [cast_unicode_py2(c) for c in comp] |
|
773 | return [cast_unicode_py2(c) for c in comp] | |
769 |
|
774 | |||
770 |
|
775 | |||
771 | def python_matches(self, text): |
|
776 | def python_matches(self, text): | |
772 | """Match attributes or global python names""" |
|
777 | """Match attributes or global python names""" | |
773 | if "." in text: |
|
778 | if "." in text: | |
774 | try: |
|
779 | try: | |
775 | matches = self.attr_matches(text) |
|
780 | matches = self.attr_matches(text) | |
776 | if text.endswith('.') and self.omit__names: |
|
781 | if text.endswith('.') and self.omit__names: | |
777 | if self.omit__names == 1: |
|
782 | if self.omit__names == 1: | |
778 | # true if txt is _not_ a __ name, false otherwise: |
|
783 | # true if txt is _not_ a __ name, false otherwise: | |
779 | no__name = (lambda txt: |
|
784 | no__name = (lambda txt: | |
780 | re.match(r'.*\.__.*?__',txt) is None) |
|
785 | re.match(r'.*\.__.*?__',txt) is None) | |
781 | else: |
|
786 | else: | |
782 | # true if txt is _not_ a _ name, false otherwise: |
|
787 | # true if txt is _not_ a _ name, false otherwise: | |
783 | no__name = (lambda txt: |
|
788 | no__name = (lambda txt: | |
784 | re.match(r'\._.*?',txt[txt.rindex('.'):]) is None) |
|
789 | re.match(r'\._.*?',txt[txt.rindex('.'):]) is None) | |
785 | matches = filter(no__name, matches) |
|
790 | matches = filter(no__name, matches) | |
786 | except NameError: |
|
791 | except NameError: | |
787 | # catches <undefined attributes>.<tab> |
|
792 | # catches <undefined attributes>.<tab> | |
788 | matches = [] |
|
793 | matches = [] | |
789 | else: |
|
794 | else: | |
790 | matches = self.global_matches(text) |
|
795 | matches = self.global_matches(text) | |
791 | return matches |
|
796 | return matches | |
792 |
|
797 | |||
793 | def _default_arguments_from_docstring(self, doc): |
|
798 | def _default_arguments_from_docstring(self, doc): | |
794 | """Parse the first line of docstring for call signature. |
|
799 | """Parse the first line of docstring for call signature. | |
795 |
|
800 | |||
796 | Docstring should be of the form 'min(iterable[, key=func])\n'. |
|
801 | Docstring should be of the form 'min(iterable[, key=func])\n'. | |
797 | It can also parse cython docstring of the form |
|
802 | It can also parse cython docstring of the form | |
798 | 'Minuit.migrad(self, int ncall=10000, resume=True, int nsplit=1)'. |
|
803 | 'Minuit.migrad(self, int ncall=10000, resume=True, int nsplit=1)'. | |
799 | """ |
|
804 | """ | |
800 | if doc is None: |
|
805 | if doc is None: | |
801 | return [] |
|
806 | return [] | |
802 |
|
807 | |||
803 | #care only the firstline |
|
808 | #care only the firstline | |
804 | line = doc.lstrip().splitlines()[0] |
|
809 | line = doc.lstrip().splitlines()[0] | |
805 |
|
810 | |||
806 | #p = re.compile(r'^[\w|\s.]+\(([^)]*)\).*') |
|
811 | #p = re.compile(r'^[\w|\s.]+\(([^)]*)\).*') | |
807 | #'min(iterable[, key=func])\n' -> 'iterable[, key=func]' |
|
812 | #'min(iterable[, key=func])\n' -> 'iterable[, key=func]' | |
808 | sig = self.docstring_sig_re.search(line) |
|
813 | sig = self.docstring_sig_re.search(line) | |
809 | if sig is None: |
|
814 | if sig is None: | |
810 | return [] |
|
815 | return [] | |
811 | # iterable[, key=func]' -> ['iterable[' ,' key=func]'] |
|
816 | # iterable[, key=func]' -> ['iterable[' ,' key=func]'] | |
812 | sig = sig.groups()[0].split(',') |
|
817 | sig = sig.groups()[0].split(',') | |
813 | ret = [] |
|
818 | ret = [] | |
814 | for s in sig: |
|
819 | for s in sig: | |
815 | #re.compile(r'[\s|\[]*(\w+)(?:\s*=\s*.*)') |
|
820 | #re.compile(r'[\s|\[]*(\w+)(?:\s*=\s*.*)') | |
816 | ret += self.docstring_kwd_re.findall(s) |
|
821 | ret += self.docstring_kwd_re.findall(s) | |
817 | return ret |
|
822 | return ret | |
818 |
|
823 | |||
819 | def _default_arguments(self, obj): |
|
824 | def _default_arguments(self, obj): | |
820 | """Return the list of default arguments of obj if it is callable, |
|
825 | """Return the list of default arguments of obj if it is callable, | |
821 | or empty list otherwise.""" |
|
826 | or empty list otherwise.""" | |
822 | call_obj = obj |
|
827 | call_obj = obj | |
823 | ret = [] |
|
828 | ret = [] | |
824 | if inspect.isbuiltin(obj): |
|
829 | if inspect.isbuiltin(obj): | |
825 | pass |
|
830 | pass | |
826 | elif not (inspect.isfunction(obj) or inspect.ismethod(obj)): |
|
831 | elif not (inspect.isfunction(obj) or inspect.ismethod(obj)): | |
827 | if inspect.isclass(obj): |
|
832 | if inspect.isclass(obj): | |
828 | #for cython embededsignature=True the constructor docstring |
|
833 | #for cython embededsignature=True the constructor docstring | |
829 | #belongs to the object itself not __init__ |
|
834 | #belongs to the object itself not __init__ | |
830 | ret += self._default_arguments_from_docstring( |
|
835 | ret += self._default_arguments_from_docstring( | |
831 | getattr(obj, '__doc__', '')) |
|
836 | getattr(obj, '__doc__', '')) | |
832 | # for classes, check for __init__,__new__ |
|
837 | # for classes, check for __init__,__new__ | |
833 | call_obj = (getattr(obj, '__init__', None) or |
|
838 | call_obj = (getattr(obj, '__init__', None) or | |
834 | getattr(obj, '__new__', None)) |
|
839 | getattr(obj, '__new__', None)) | |
835 | # for all others, check if they are __call__able |
|
840 | # for all others, check if they are __call__able | |
836 | elif hasattr(obj, '__call__'): |
|
841 | elif hasattr(obj, '__call__'): | |
837 | call_obj = obj.__call__ |
|
842 | call_obj = obj.__call__ | |
838 | ret += self._default_arguments_from_docstring( |
|
843 | ret += self._default_arguments_from_docstring( | |
839 | getattr(call_obj, '__doc__', '')) |
|
844 | getattr(call_obj, '__doc__', '')) | |
840 |
|
845 | |||
841 | if PY3: |
|
846 | if PY3: | |
842 | _keeps = (inspect.Parameter.KEYWORD_ONLY, |
|
847 | _keeps = (inspect.Parameter.KEYWORD_ONLY, | |
843 | inspect.Parameter.POSITIONAL_OR_KEYWORD) |
|
848 | inspect.Parameter.POSITIONAL_OR_KEYWORD) | |
844 | signature = inspect.signature |
|
849 | signature = inspect.signature | |
845 | else: |
|
850 | else: | |
846 | import IPython.utils.signatures |
|
851 | import IPython.utils.signatures | |
847 | _keeps = (IPython.utils.signatures.Parameter.KEYWORD_ONLY, |
|
852 | _keeps = (IPython.utils.signatures.Parameter.KEYWORD_ONLY, | |
848 | IPython.utils.signatures.Parameter.POSITIONAL_OR_KEYWORD) |
|
853 | IPython.utils.signatures.Parameter.POSITIONAL_OR_KEYWORD) | |
849 | signature = IPython.utils.signatures.signature |
|
854 | signature = IPython.utils.signatures.signature | |
850 |
|
855 | |||
851 | try: |
|
856 | try: | |
852 | sig = signature(call_obj) |
|
857 | sig = signature(call_obj) | |
853 | ret.extend(k for k, v in sig.parameters.items() if |
|
858 | ret.extend(k for k, v in sig.parameters.items() if | |
854 | v.kind in _keeps) |
|
859 | v.kind in _keeps) | |
855 | except ValueError: |
|
860 | except ValueError: | |
856 | pass |
|
861 | pass | |
857 |
|
862 | |||
858 | return list(set(ret)) |
|
863 | return list(set(ret)) | |
859 |
|
864 | |||
860 | def python_func_kw_matches(self,text): |
|
865 | def python_func_kw_matches(self,text): | |
861 | """Match named parameters (kwargs) of the last open function""" |
|
866 | """Match named parameters (kwargs) of the last open function""" | |
862 |
|
867 | |||
863 | if "." in text: # a parameter cannot be dotted |
|
868 | if "." in text: # a parameter cannot be dotted | |
864 | return [] |
|
869 | return [] | |
865 | try: regexp = self.__funcParamsRegex |
|
870 | try: regexp = self.__funcParamsRegex | |
866 | except AttributeError: |
|
871 | except AttributeError: | |
867 | regexp = self.__funcParamsRegex = re.compile(r''' |
|
872 | regexp = self.__funcParamsRegex = re.compile(r''' | |
868 | '.*?(?<!\\)' | # single quoted strings or |
|
873 | '.*?(?<!\\)' | # single quoted strings or | |
869 | ".*?(?<!\\)" | # double quoted strings or |
|
874 | ".*?(?<!\\)" | # double quoted strings or | |
870 | \w+ | # identifier |
|
875 | \w+ | # identifier | |
871 | \S # other characters |
|
876 | \S # other characters | |
872 | ''', re.VERBOSE | re.DOTALL) |
|
877 | ''', re.VERBOSE | re.DOTALL) | |
873 | # 1. find the nearest identifier that comes before an unclosed |
|
878 | # 1. find the nearest identifier that comes before an unclosed | |
874 | # parenthesis before the cursor |
|
879 | # parenthesis before the cursor | |
875 | # e.g. for "foo (1+bar(x), pa<cursor>,a=1)", the candidate is "foo" |
|
880 | # e.g. for "foo (1+bar(x), pa<cursor>,a=1)", the candidate is "foo" | |
876 | tokens = regexp.findall(self.text_until_cursor) |
|
881 | tokens = regexp.findall(self.text_until_cursor) | |
877 | tokens.reverse() |
|
882 | tokens.reverse() | |
878 | iterTokens = iter(tokens); openPar = 0 |
|
883 | iterTokens = iter(tokens); openPar = 0 | |
879 |
|
884 | |||
880 | for token in iterTokens: |
|
885 | for token in iterTokens: | |
881 | if token == ')': |
|
886 | if token == ')': | |
882 | openPar -= 1 |
|
887 | openPar -= 1 | |
883 | elif token == '(': |
|
888 | elif token == '(': | |
884 | openPar += 1 |
|
889 | openPar += 1 | |
885 | if openPar > 0: |
|
890 | if openPar > 0: | |
886 | # found the last unclosed parenthesis |
|
891 | # found the last unclosed parenthesis | |
887 | break |
|
892 | break | |
888 | else: |
|
893 | else: | |
889 | return [] |
|
894 | return [] | |
890 | # 2. Concatenate dotted names ("foo.bar" for "foo.bar(x, pa" ) |
|
895 | # 2. Concatenate dotted names ("foo.bar" for "foo.bar(x, pa" ) | |
891 | ids = [] |
|
896 | ids = [] | |
892 | isId = re.compile(r'\w+$').match |
|
897 | isId = re.compile(r'\w+$').match | |
893 |
|
898 | |||
894 | while True: |
|
899 | while True: | |
895 | try: |
|
900 | try: | |
896 | ids.append(next(iterTokens)) |
|
901 | ids.append(next(iterTokens)) | |
897 | if not isId(ids[-1]): |
|
902 | if not isId(ids[-1]): | |
898 | ids.pop(); break |
|
903 | ids.pop(); break | |
899 | if not next(iterTokens) == '.': |
|
904 | if not next(iterTokens) == '.': | |
900 | break |
|
905 | break | |
901 | except StopIteration: |
|
906 | except StopIteration: | |
902 | break |
|
907 | break | |
903 | # lookup the candidate callable matches either using global_matches |
|
908 | # lookup the candidate callable matches either using global_matches | |
904 | # or attr_matches for dotted names |
|
909 | # or attr_matches for dotted names | |
905 | if len(ids) == 1: |
|
910 | if len(ids) == 1: | |
906 | callableMatches = self.global_matches(ids[0]) |
|
911 | callableMatches = self.global_matches(ids[0]) | |
907 | else: |
|
912 | else: | |
908 | callableMatches = self.attr_matches('.'.join(ids[::-1])) |
|
913 | callableMatches = self.attr_matches('.'.join(ids[::-1])) | |
909 | argMatches = [] |
|
914 | argMatches = [] | |
910 | for callableMatch in callableMatches: |
|
915 | for callableMatch in callableMatches: | |
911 | try: |
|
916 | try: | |
912 | namedArgs = self._default_arguments(eval(callableMatch, |
|
917 | namedArgs = self._default_arguments(eval(callableMatch, | |
913 | self.namespace)) |
|
918 | self.namespace)) | |
914 | except: |
|
919 | except: | |
915 | continue |
|
920 | continue | |
916 |
|
921 | |||
917 | for namedArg in namedArgs: |
|
922 | for namedArg in namedArgs: | |
918 | if namedArg.startswith(text): |
|
923 | if namedArg.startswith(text): | |
919 | argMatches.append(u"%s=" %namedArg) |
|
924 | argMatches.append(u"%s=" %namedArg) | |
920 | return argMatches |
|
925 | return argMatches | |
921 |
|
926 | |||
922 | def dict_key_matches(self, text): |
|
927 | def dict_key_matches(self, text): | |
923 | "Match string keys in a dictionary, after e.g. 'foo[' " |
|
928 | "Match string keys in a dictionary, after e.g. 'foo[' " | |
924 | def get_keys(obj): |
|
929 | def get_keys(obj): | |
925 | # Objects can define their own completions by defining an |
|
930 | # Objects can define their own completions by defining an | |
926 | # _ipy_key_completions_() method. |
|
931 | # _ipy_key_completions_() method. | |
927 | method = get_real_method(obj, '_ipython_key_completions_') |
|
932 | method = get_real_method(obj, '_ipython_key_completions_') | |
928 | if method is not None: |
|
933 | if method is not None: | |
929 | return method() |
|
934 | return method() | |
930 |
|
935 | |||
931 | # Special case some common in-memory dict-like types |
|
936 | # Special case some common in-memory dict-like types | |
932 | if isinstance(obj, dict) or\ |
|
937 | if isinstance(obj, dict) or\ | |
933 | _safe_isinstance(obj, 'pandas', 'DataFrame'): |
|
938 | _safe_isinstance(obj, 'pandas', 'DataFrame'): | |
934 | try: |
|
939 | try: | |
935 | return list(obj.keys()) |
|
940 | return list(obj.keys()) | |
936 | except Exception: |
|
941 | except Exception: | |
937 | return [] |
|
942 | return [] | |
938 | elif _safe_isinstance(obj, 'numpy', 'ndarray') or\ |
|
943 | elif _safe_isinstance(obj, 'numpy', 'ndarray') or\ | |
939 | _safe_isinstance(obj, 'numpy', 'void'): |
|
944 | _safe_isinstance(obj, 'numpy', 'void'): | |
940 | return obj.dtype.names or [] |
|
945 | return obj.dtype.names or [] | |
941 | return [] |
|
946 | return [] | |
942 |
|
947 | |||
943 | try: |
|
948 | try: | |
944 | regexps = self.__dict_key_regexps |
|
949 | regexps = self.__dict_key_regexps | |
945 | except AttributeError: |
|
950 | except AttributeError: | |
946 | dict_key_re_fmt = r'''(?x) |
|
951 | dict_key_re_fmt = r'''(?x) | |
947 | ( # match dict-referring expression wrt greedy setting |
|
952 | ( # match dict-referring expression wrt greedy setting | |
948 | %s |
|
953 | %s | |
949 | ) |
|
954 | ) | |
950 | \[ # open bracket |
|
955 | \[ # open bracket | |
951 | \s* # and optional whitespace |
|
956 | \s* # and optional whitespace | |
952 | ([uUbB]? # string prefix (r not handled) |
|
957 | ([uUbB]? # string prefix (r not handled) | |
953 | (?: # unclosed string |
|
958 | (?: # unclosed string | |
954 | '(?:[^']|(?<!\\)\\')* |
|
959 | '(?:[^']|(?<!\\)\\')* | |
955 | | |
|
960 | | | |
956 | "(?:[^"]|(?<!\\)\\")* |
|
961 | "(?:[^"]|(?<!\\)\\")* | |
957 | ) |
|
962 | ) | |
958 | )? |
|
963 | )? | |
959 | $ |
|
964 | $ | |
960 | ''' |
|
965 | ''' | |
961 | regexps = self.__dict_key_regexps = { |
|
966 | regexps = self.__dict_key_regexps = { | |
962 | False: re.compile(dict_key_re_fmt % ''' |
|
967 | False: re.compile(dict_key_re_fmt % ''' | |
963 | # identifiers separated by . |
|
968 | # identifiers separated by . | |
964 | (?!\d)\w+ |
|
969 | (?!\d)\w+ | |
965 | (?:\.(?!\d)\w+)* |
|
970 | (?:\.(?!\d)\w+)* | |
966 | '''), |
|
971 | '''), | |
967 | True: re.compile(dict_key_re_fmt % ''' |
|
972 | True: re.compile(dict_key_re_fmt % ''' | |
968 | .+ |
|
973 | .+ | |
969 | ''') |
|
974 | ''') | |
970 | } |
|
975 | } | |
971 |
|
976 | |||
972 | match = regexps[self.greedy].search(self.text_until_cursor) |
|
977 | match = regexps[self.greedy].search(self.text_until_cursor) | |
973 | if match is None: |
|
978 | if match is None: | |
974 | return [] |
|
979 | return [] | |
975 |
|
980 | |||
976 | expr, prefix = match.groups() |
|
981 | expr, prefix = match.groups() | |
977 | try: |
|
982 | try: | |
978 | obj = eval(expr, self.namespace) |
|
983 | obj = eval(expr, self.namespace) | |
979 | except Exception: |
|
984 | except Exception: | |
980 | try: |
|
985 | try: | |
981 | obj = eval(expr, self.global_namespace) |
|
986 | obj = eval(expr, self.global_namespace) | |
982 | except Exception: |
|
987 | except Exception: | |
983 | return [] |
|
988 | return [] | |
984 |
|
989 | |||
985 | keys = get_keys(obj) |
|
990 | keys = get_keys(obj) | |
986 | if not keys: |
|
991 | if not keys: | |
987 | return keys |
|
992 | return keys | |
988 | closing_quote, token_offset, matches = match_dict_keys(keys, prefix, self.splitter.delims) |
|
993 | closing_quote, token_offset, matches = match_dict_keys(keys, prefix, self.splitter.delims) | |
989 | if not matches: |
|
994 | if not matches: | |
990 | return matches |
|
995 | return matches | |
991 |
|
996 | |||
992 | # get the cursor position of |
|
997 | # get the cursor position of | |
993 | # - the text being completed |
|
998 | # - the text being completed | |
994 | # - the start of the key text |
|
999 | # - the start of the key text | |
995 | # - the start of the completion |
|
1000 | # - the start of the completion | |
996 | text_start = len(self.text_until_cursor) - len(text) |
|
1001 | text_start = len(self.text_until_cursor) - len(text) | |
997 | if prefix: |
|
1002 | if prefix: | |
998 | key_start = match.start(2) |
|
1003 | key_start = match.start(2) | |
999 | completion_start = key_start + token_offset |
|
1004 | completion_start = key_start + token_offset | |
1000 | else: |
|
1005 | else: | |
1001 | key_start = completion_start = match.end() |
|
1006 | key_start = completion_start = match.end() | |
1002 |
|
1007 | |||
1003 | # grab the leading prefix, to make sure all completions start with `text` |
|
1008 | # grab the leading prefix, to make sure all completions start with `text` | |
1004 | if text_start > key_start: |
|
1009 | if text_start > key_start: | |
1005 | leading = '' |
|
1010 | leading = '' | |
1006 | else: |
|
1011 | else: | |
1007 | leading = text[text_start:completion_start] |
|
1012 | leading = text[text_start:completion_start] | |
1008 |
|
1013 | |||
1009 | # the index of the `[` character |
|
1014 | # the index of the `[` character | |
1010 | bracket_idx = match.end(1) |
|
1015 | bracket_idx = match.end(1) | |
1011 |
|
1016 | |||
1012 | # append closing quote and bracket as appropriate |
|
1017 | # append closing quote and bracket as appropriate | |
1013 | # this is *not* appropriate if the opening quote or bracket is outside |
|
1018 | # this is *not* appropriate if the opening quote or bracket is outside | |
1014 | # the text given to this method |
|
1019 | # the text given to this method | |
1015 | suf = '' |
|
1020 | suf = '' | |
1016 | continuation = self.line_buffer[len(self.text_until_cursor):] |
|
1021 | continuation = self.line_buffer[len(self.text_until_cursor):] | |
1017 | if key_start > text_start and closing_quote: |
|
1022 | if key_start > text_start and closing_quote: | |
1018 | # quotes were opened inside text, maybe close them |
|
1023 | # quotes were opened inside text, maybe close them | |
1019 | if continuation.startswith(closing_quote): |
|
1024 | if continuation.startswith(closing_quote): | |
1020 | continuation = continuation[len(closing_quote):] |
|
1025 | continuation = continuation[len(closing_quote):] | |
1021 | else: |
|
1026 | else: | |
1022 | suf += closing_quote |
|
1027 | suf += closing_quote | |
1023 | if bracket_idx > text_start: |
|
1028 | if bracket_idx > text_start: | |
1024 | # brackets were opened inside text, maybe close them |
|
1029 | # brackets were opened inside text, maybe close them | |
1025 | if not continuation.startswith(']'): |
|
1030 | if not continuation.startswith(']'): | |
1026 | suf += ']' |
|
1031 | suf += ']' | |
1027 |
|
1032 | |||
1028 | return [leading + k + suf for k in matches] |
|
1033 | return [leading + k + suf for k in matches] | |
1029 |
|
1034 | |||
1030 | def unicode_name_matches(self, text): |
|
1035 | def unicode_name_matches(self, text): | |
1031 | u"""Match Latex-like syntax for unicode characters base |
|
1036 | u"""Match Latex-like syntax for unicode characters base | |
1032 | on the name of the character. |
|
1037 | on the name of the character. | |
1033 |
|
1038 | |||
1034 | This does \\GREEK SMALL LETTER ETA -> Ξ· |
|
1039 | This does \\GREEK SMALL LETTER ETA -> Ξ· | |
1035 |
|
1040 | |||
1036 | Works only on valid python 3 identifier, or on combining characters that |
|
1041 | Works only on valid python 3 identifier, or on combining characters that | |
1037 | will combine to form a valid identifier. |
|
1042 | will combine to form a valid identifier. | |
1038 |
|
1043 | |||
1039 | Used on Python 3 only. |
|
1044 | Used on Python 3 only. | |
1040 | """ |
|
1045 | """ | |
1041 | slashpos = text.rfind('\\') |
|
1046 | slashpos = text.rfind('\\') | |
1042 | if slashpos > -1: |
|
1047 | if slashpos > -1: | |
1043 | s = text[slashpos+1:] |
|
1048 | s = text[slashpos+1:] | |
1044 | try : |
|
1049 | try : | |
1045 | unic = unicodedata.lookup(s) |
|
1050 | unic = unicodedata.lookup(s) | |
1046 | # allow combining chars |
|
1051 | # allow combining chars | |
1047 | if ('a'+unic).isidentifier(): |
|
1052 | if ('a'+unic).isidentifier(): | |
1048 | return '\\'+s,[unic] |
|
1053 | return '\\'+s,[unic] | |
1049 | except KeyError: |
|
1054 | except KeyError: | |
1050 | pass |
|
1055 | pass | |
1051 | return u'', [] |
|
1056 | return u'', [] | |
1052 |
|
1057 | |||
1053 |
|
1058 | |||
1054 |
|
1059 | |||
1055 |
|
1060 | |||
1056 | def latex_matches(self, text): |
|
1061 | def latex_matches(self, text): | |
1057 | u"""Match Latex syntax for unicode characters. |
|
1062 | u"""Match Latex syntax for unicode characters. | |
1058 |
|
1063 | |||
1059 | This does both \\alp -> \\alpha and \\alpha -> Ξ± |
|
1064 | This does both \\alp -> \\alpha and \\alpha -> Ξ± | |
1060 |
|
1065 | |||
1061 | Used on Python 3 only. |
|
1066 | Used on Python 3 only. | |
1062 | """ |
|
1067 | """ | |
1063 | slashpos = text.rfind('\\') |
|
1068 | slashpos = text.rfind('\\') | |
1064 | if slashpos > -1: |
|
1069 | if slashpos > -1: | |
1065 | s = text[slashpos:] |
|
1070 | s = text[slashpos:] | |
1066 | if s in latex_symbols: |
|
1071 | if s in latex_symbols: | |
1067 | # Try to complete a full latex symbol to unicode |
|
1072 | # Try to complete a full latex symbol to unicode | |
1068 | # \\alpha -> Ξ± |
|
1073 | # \\alpha -> Ξ± | |
1069 | return s, [latex_symbols[s]] |
|
1074 | return s, [latex_symbols[s]] | |
1070 | else: |
|
1075 | else: | |
1071 | # If a user has partially typed a latex symbol, give them |
|
1076 | # If a user has partially typed a latex symbol, give them | |
1072 | # a full list of options \al -> [\aleph, \alpha] |
|
1077 | # a full list of options \al -> [\aleph, \alpha] | |
1073 | matches = [k for k in latex_symbols if k.startswith(s)] |
|
1078 | matches = [k for k in latex_symbols if k.startswith(s)] | |
1074 | return s, matches |
|
1079 | return s, matches | |
1075 | return u'', [] |
|
1080 | return u'', [] | |
1076 |
|
1081 | |||
1077 | def dispatch_custom_completer(self, text): |
|
1082 | def dispatch_custom_completer(self, text): | |
1078 | if not self.custom_completers: |
|
1083 | if not self.custom_completers: | |
1079 | return |
|
1084 | return | |
1080 |
|
1085 | |||
1081 | line = self.line_buffer |
|
1086 | line = self.line_buffer | |
1082 | if not line.strip(): |
|
1087 | if not line.strip(): | |
1083 | return None |
|
1088 | return None | |
1084 |
|
1089 | |||
1085 | # Create a little structure to pass all the relevant information about |
|
1090 | # Create a little structure to pass all the relevant information about | |
1086 | # the current completion to any custom completer. |
|
1091 | # the current completion to any custom completer. | |
1087 | event = Bunch() |
|
1092 | event = Bunch() | |
1088 | event.line = line |
|
1093 | event.line = line | |
1089 | event.symbol = text |
|
1094 | event.symbol = text | |
1090 | cmd = line.split(None,1)[0] |
|
1095 | cmd = line.split(None,1)[0] | |
1091 | event.command = cmd |
|
1096 | event.command = cmd | |
1092 | event.text_until_cursor = self.text_until_cursor |
|
1097 | event.text_until_cursor = self.text_until_cursor | |
1093 |
|
1098 | |||
1094 | # for foo etc, try also to find completer for %foo |
|
1099 | # for foo etc, try also to find completer for %foo | |
1095 | if not cmd.startswith(self.magic_escape): |
|
1100 | if not cmd.startswith(self.magic_escape): | |
1096 | try_magic = self.custom_completers.s_matches( |
|
1101 | try_magic = self.custom_completers.s_matches( | |
1097 | self.magic_escape + cmd) |
|
1102 | self.magic_escape + cmd) | |
1098 | else: |
|
1103 | else: | |
1099 | try_magic = [] |
|
1104 | try_magic = [] | |
1100 |
|
1105 | |||
1101 | for c in itertools.chain(self.custom_completers.s_matches(cmd), |
|
1106 | for c in itertools.chain(self.custom_completers.s_matches(cmd), | |
1102 | try_magic, |
|
1107 | try_magic, | |
1103 | self.custom_completers.flat_matches(self.text_until_cursor)): |
|
1108 | self.custom_completers.flat_matches(self.text_until_cursor)): | |
1104 | try: |
|
1109 | try: | |
1105 | res = c(event) |
|
1110 | res = c(event) | |
1106 | if res: |
|
1111 | if res: | |
1107 | # first, try case sensitive match |
|
1112 | # first, try case sensitive match | |
1108 | withcase = [cast_unicode_py2(r) for r in res if r.startswith(text)] |
|
1113 | withcase = [cast_unicode_py2(r) for r in res if r.startswith(text)] | |
1109 | if withcase: |
|
1114 | if withcase: | |
1110 | return withcase |
|
1115 | return withcase | |
1111 | # if none, then case insensitive ones are ok too |
|
1116 | # if none, then case insensitive ones are ok too | |
1112 | text_low = text.lower() |
|
1117 | text_low = text.lower() | |
1113 | return [cast_unicode_py2(r) for r in res if r.lower().startswith(text_low)] |
|
1118 | return [cast_unicode_py2(r) for r in res if r.lower().startswith(text_low)] | |
1114 | except TryNext: |
|
1119 | except TryNext: | |
1115 | pass |
|
1120 | pass | |
1116 |
|
1121 | |||
1117 | return None |
|
1122 | return None | |
1118 |
|
1123 | |||
1119 | def complete(self, text=None, line_buffer=None, cursor_pos=None): |
|
1124 | def complete(self, text=None, line_buffer=None, cursor_pos=None): | |
1120 | """Find completions for the given text and line context. |
|
1125 | """Find completions for the given text and line context. | |
1121 |
|
1126 | |||
1122 | Note that both the text and the line_buffer are optional, but at least |
|
1127 | Note that both the text and the line_buffer are optional, but at least | |
1123 | one of them must be given. |
|
1128 | one of them must be given. | |
1124 |
|
1129 | |||
1125 | Parameters |
|
1130 | Parameters | |
1126 | ---------- |
|
1131 | ---------- | |
1127 | text : string, optional |
|
1132 | text : string, optional | |
1128 | Text to perform the completion on. If not given, the line buffer |
|
1133 | Text to perform the completion on. If not given, the line buffer | |
1129 | is split using the instance's CompletionSplitter object. |
|
1134 | is split using the instance's CompletionSplitter object. | |
1130 |
|
1135 | |||
1131 | line_buffer : string, optional |
|
1136 | line_buffer : string, optional | |
1132 | If not given, the completer attempts to obtain the current line |
|
1137 | If not given, the completer attempts to obtain the current line | |
1133 | buffer via readline. This keyword allows clients which are |
|
1138 | buffer via readline. This keyword allows clients which are | |
1134 | requesting for text completions in non-readline contexts to inform |
|
1139 | requesting for text completions in non-readline contexts to inform | |
1135 | the completer of the entire text. |
|
1140 | the completer of the entire text. | |
1136 |
|
1141 | |||
1137 | cursor_pos : int, optional |
|
1142 | cursor_pos : int, optional | |
1138 | Index of the cursor in the full line buffer. Should be provided by |
|
1143 | Index of the cursor in the full line buffer. Should be provided by | |
1139 | remote frontends where kernel has no access to frontend state. |
|
1144 | remote frontends where kernel has no access to frontend state. | |
1140 |
|
1145 | |||
1141 | Returns |
|
1146 | Returns | |
1142 | ------- |
|
1147 | ------- | |
1143 | text : str |
|
1148 | text : str | |
1144 | Text that was actually used in the completion. |
|
1149 | Text that was actually used in the completion. | |
1145 |
|
1150 | |||
1146 | matches : list |
|
1151 | matches : list | |
1147 | A list of completion matches. |
|
1152 | A list of completion matches. | |
1148 | """ |
|
1153 | """ | |
1149 | # if the cursor position isn't given, the only sane assumption we can |
|
1154 | # if the cursor position isn't given, the only sane assumption we can | |
1150 | # make is that it's at the end of the line (the common case) |
|
1155 | # make is that it's at the end of the line (the common case) | |
1151 | if cursor_pos is None: |
|
1156 | if cursor_pos is None: | |
1152 | cursor_pos = len(line_buffer) if text is None else len(text) |
|
1157 | cursor_pos = len(line_buffer) if text is None else len(text) | |
1153 |
|
1158 | |||
1154 | if PY3: |
|
1159 | if PY3: | |
1155 |
|
1160 | |||
1156 | base_text = text if not line_buffer else line_buffer[:cursor_pos] |
|
1161 | base_text = text if not line_buffer else line_buffer[:cursor_pos] | |
1157 | latex_text, latex_matches = self.latex_matches(base_text) |
|
1162 | latex_text, latex_matches = self.latex_matches(base_text) | |
1158 | if latex_matches: |
|
1163 | if latex_matches: | |
1159 | return latex_text, latex_matches |
|
1164 | return latex_text, latex_matches | |
1160 | name_text = '' |
|
1165 | name_text = '' | |
1161 | name_matches = [] |
|
1166 | name_matches = [] | |
1162 | for meth in (self.unicode_name_matches, back_latex_name_matches, back_unicode_name_matches): |
|
1167 | for meth in (self.unicode_name_matches, back_latex_name_matches, back_unicode_name_matches): | |
1163 | name_text, name_matches = meth(base_text) |
|
1168 | name_text, name_matches = meth(base_text) | |
1164 | if name_text: |
|
1169 | if name_text: | |
1165 | return name_text, name_matches |
|
1170 | return name_text, name_matches | |
1166 |
|
1171 | |||
1167 | # if text is either None or an empty string, rely on the line buffer |
|
1172 | # if text is either None or an empty string, rely on the line buffer | |
1168 | if not text: |
|
1173 | if not text: | |
1169 | text = self.splitter.split_line(line_buffer, cursor_pos) |
|
1174 | text = self.splitter.split_line(line_buffer, cursor_pos) | |
1170 |
|
1175 | |||
1171 | # If no line buffer is given, assume the input text is all there was |
|
1176 | # If no line buffer is given, assume the input text is all there was | |
1172 | if line_buffer is None: |
|
1177 | if line_buffer is None: | |
1173 | line_buffer = text |
|
1178 | line_buffer = text | |
1174 |
|
1179 | |||
1175 | self.line_buffer = line_buffer |
|
1180 | self.line_buffer = line_buffer | |
1176 | self.text_until_cursor = self.line_buffer[:cursor_pos] |
|
1181 | self.text_until_cursor = self.line_buffer[:cursor_pos] | |
1177 |
|
1182 | |||
1178 | # Start with a clean slate of completions |
|
1183 | # Start with a clean slate of completions | |
1179 | self.matches[:] = [] |
|
1184 | self.matches[:] = [] | |
1180 | custom_res = self.dispatch_custom_completer(text) |
|
1185 | custom_res = self.dispatch_custom_completer(text) | |
1181 | if custom_res is not None: |
|
1186 | if custom_res is not None: | |
1182 | # did custom completers produce something? |
|
1187 | # did custom completers produce something? | |
1183 | self.matches = custom_res |
|
1188 | self.matches = custom_res | |
1184 | else: |
|
1189 | else: | |
1185 | # Extend the list of completions with the results of each |
|
1190 | # Extend the list of completions with the results of each | |
1186 | # matcher, so we return results to the user from all |
|
1191 | # matcher, so we return results to the user from all | |
1187 | # namespaces. |
|
1192 | # namespaces. | |
1188 | if self.merge_completions: |
|
1193 | if self.merge_completions: | |
1189 | self.matches = [] |
|
1194 | self.matches = [] | |
1190 | for matcher in self.matchers: |
|
1195 | for matcher in self.matchers: | |
1191 | try: |
|
1196 | try: | |
1192 | self.matches.extend(matcher(text)) |
|
1197 | self.matches.extend(matcher(text)) | |
1193 | except: |
|
1198 | except: | |
1194 | # Show the ugly traceback if the matcher causes an |
|
1199 | # Show the ugly traceback if the matcher causes an | |
1195 | # exception, but do NOT crash the kernel! |
|
1200 | # exception, but do NOT crash the kernel! | |
1196 | sys.excepthook(*sys.exc_info()) |
|
1201 | sys.excepthook(*sys.exc_info()) | |
1197 | else: |
|
1202 | else: | |
1198 | for matcher in self.matchers: |
|
1203 | for matcher in self.matchers: | |
1199 | self.matches = matcher(text) |
|
1204 | self.matches = matcher(text) | |
1200 | if self.matches: |
|
1205 | if self.matches: | |
1201 | break |
|
1206 | break | |
1202 | # FIXME: we should extend our api to return a dict with completions for |
|
1207 | # FIXME: we should extend our api to return a dict with completions for | |
1203 | # different types of objects. The rlcomplete() method could then |
|
1208 | # different types of objects. The rlcomplete() method could then | |
1204 | # simply collapse the dict into a list for readline, but we'd have |
|
1209 | # simply collapse the dict into a list for readline, but we'd have | |
1205 | # richer completion semantics in other evironments. |
|
1210 | # richer completion semantics in other evironments. | |
1206 | self.matches = sorted(set(self.matches), key=completions_sorting_key) |
|
1211 | self.matches = sorted(set(self.matches), key=completions_sorting_key) | |
1207 |
|
1212 | |||
1208 | return text, self.matches |
|
1213 | return text, self.matches | |
1209 |
|
1214 | |||
1210 | def rlcomplete(self, text, state): |
|
1215 | def rlcomplete(self, text, state): | |
1211 | """Return the state-th possible completion for 'text'. |
|
1216 | """Return the state-th possible completion for 'text'. | |
1212 |
|
1217 | |||
1213 | This is called successively with state == 0, 1, 2, ... until it |
|
1218 | This is called successively with state == 0, 1, 2, ... until it | |
1214 | returns None. The completion should begin with 'text'. |
|
1219 | returns None. The completion should begin with 'text'. | |
1215 |
|
1220 | |||
1216 | Parameters |
|
1221 | Parameters | |
1217 | ---------- |
|
1222 | ---------- | |
1218 | text : string |
|
1223 | text : string | |
1219 | Text to perform the completion on. |
|
1224 | Text to perform the completion on. | |
1220 |
|
1225 | |||
1221 | state : int |
|
1226 | state : int | |
1222 | Counter used by readline. |
|
1227 | Counter used by readline. | |
1223 | """ |
|
1228 | """ | |
1224 | if state==0: |
|
1229 | if state==0: | |
1225 |
|
1230 | |||
1226 | self.line_buffer = line_buffer = self.readline.get_line_buffer() |
|
1231 | self.line_buffer = line_buffer = self.readline.get_line_buffer() | |
1227 | cursor_pos = self.readline.get_endidx() |
|
1232 | cursor_pos = self.readline.get_endidx() | |
1228 |
|
1233 | |||
1229 | #io.rprint("\nRLCOMPLETE: %r %r %r" % |
|
1234 | #io.rprint("\nRLCOMPLETE: %r %r %r" % | |
1230 | # (text, line_buffer, cursor_pos) ) # dbg |
|
1235 | # (text, line_buffer, cursor_pos) ) # dbg | |
1231 |
|
1236 | |||
1232 | # if there is only a tab on a line with only whitespace, instead of |
|
1237 | # if there is only a tab on a line with only whitespace, instead of | |
1233 | # the mostly useless 'do you want to see all million completions' |
|
1238 | # the mostly useless 'do you want to see all million completions' | |
1234 | # message, just do the right thing and give the user his tab! |
|
1239 | # message, just do the right thing and give the user his tab! | |
1235 | # Incidentally, this enables pasting of tabbed text from an editor |
|
1240 | # Incidentally, this enables pasting of tabbed text from an editor | |
1236 | # (as long as autoindent is off). |
|
1241 | # (as long as autoindent is off). | |
1237 |
|
1242 | |||
1238 | # It should be noted that at least pyreadline still shows file |
|
1243 | # It should be noted that at least pyreadline still shows file | |
1239 | # completions - is there a way around it? |
|
1244 | # completions - is there a way around it? | |
1240 |
|
1245 | |||
1241 | # don't apply this on 'dumb' terminals, such as emacs buffers, so |
|
1246 | # don't apply this on 'dumb' terminals, such as emacs buffers, so | |
1242 | # we don't interfere with their own tab-completion mechanism. |
|
1247 | # we don't interfere with their own tab-completion mechanism. | |
1243 | if not (self.dumb_terminal or line_buffer.strip()): |
|
1248 | if not (self.dumb_terminal or line_buffer.strip()): | |
1244 | self.readline.insert_text('\t') |
|
1249 | self.readline.insert_text('\t') | |
1245 | sys.stdout.flush() |
|
1250 | sys.stdout.flush() | |
1246 | return None |
|
1251 | return None | |
1247 |
|
1252 | |||
1248 | # Note: debugging exceptions that may occur in completion is very |
|
1253 | # Note: debugging exceptions that may occur in completion is very | |
1249 | # tricky, because readline unconditionally silences them. So if |
|
1254 | # tricky, because readline unconditionally silences them. So if | |
1250 | # during development you suspect a bug in the completion code, turn |
|
1255 | # during development you suspect a bug in the completion code, turn | |
1251 | # this flag on temporarily by uncommenting the second form (don't |
|
1256 | # this flag on temporarily by uncommenting the second form (don't | |
1252 | # flip the value in the first line, as the '# dbg' marker can be |
|
1257 | # flip the value in the first line, as the '# dbg' marker can be | |
1253 | # automatically detected and is used elsewhere). |
|
1258 | # automatically detected and is used elsewhere). | |
1254 | DEBUG = False |
|
1259 | DEBUG = False | |
1255 | #DEBUG = True # dbg |
|
1260 | #DEBUG = True # dbg | |
1256 | if DEBUG: |
|
1261 | if DEBUG: | |
1257 | try: |
|
1262 | try: | |
1258 | self.complete(text, line_buffer, cursor_pos) |
|
1263 | self.complete(text, line_buffer, cursor_pos) | |
1259 | except: |
|
1264 | except: | |
1260 | import traceback; traceback.print_exc() |
|
1265 | import traceback; traceback.print_exc() | |
1261 | else: |
|
1266 | else: | |
1262 | # The normal production version is here |
|
1267 | # The normal production version is here | |
1263 |
|
1268 | |||
1264 | # This method computes the self.matches array |
|
1269 | # This method computes the self.matches array | |
1265 | self.complete(text, line_buffer, cursor_pos) |
|
1270 | self.complete(text, line_buffer, cursor_pos) | |
1266 |
|
1271 | |||
1267 | try: |
|
1272 | try: | |
1268 | return self.matches[state] |
|
1273 | return self.matches[state] | |
1269 | except IndexError: |
|
1274 | except IndexError: | |
1270 | return None |
|
1275 | return None | |
1271 |
|
1276 |
@@ -1,3234 +1,3233 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.debugger import Pdb | |
46 | from IPython.core.display_trap import DisplayTrap |
|
46 | from IPython.core.display_trap import DisplayTrap | |
47 | from IPython.core.displayhook import DisplayHook |
|
47 | from IPython.core.displayhook import DisplayHook | |
48 | from IPython.core.displaypub import DisplayPublisher |
|
48 | from IPython.core.displaypub import DisplayPublisher | |
49 | from IPython.core.error import InputRejected, UsageError |
|
49 | from IPython.core.error import InputRejected, UsageError | |
50 | from IPython.core.extensions import ExtensionManager |
|
50 | from IPython.core.extensions import ExtensionManager | |
51 | from IPython.core.formatters import DisplayFormatter |
|
51 | from IPython.core.formatters import DisplayFormatter | |
52 | from IPython.core.history import HistoryManager |
|
52 | from IPython.core.history import HistoryManager | |
53 | from IPython.core.inputsplitter import ESC_MAGIC, ESC_MAGIC2 |
|
53 | from IPython.core.inputsplitter import ESC_MAGIC, ESC_MAGIC2 | |
54 | from IPython.core.logger import Logger |
|
54 | from IPython.core.logger import Logger | |
55 | from IPython.core.macro import Macro |
|
55 | from IPython.core.macro import Macro | |
56 | from IPython.core.payload import PayloadManager |
|
56 | from IPython.core.payload import PayloadManager | |
57 | from IPython.core.prefilter import PrefilterManager |
|
57 | from IPython.core.prefilter import PrefilterManager | |
58 | from IPython.core.profiledir import ProfileDir |
|
58 | from IPython.core.profiledir import ProfileDir | |
59 | from IPython.core.usage import default_banner |
|
59 | from IPython.core.usage import default_banner | |
60 | from IPython.testing.skipdoctest import skip_doctest_py2, skip_doctest |
|
60 | from IPython.testing.skipdoctest import skip_doctest_py2, skip_doctest | |
61 | from IPython.utils import PyColorize |
|
61 | from IPython.utils import PyColorize | |
62 | from IPython.utils import io |
|
62 | from IPython.utils import io | |
63 | from IPython.utils import py3compat |
|
63 | from IPython.utils import py3compat | |
64 | from IPython.utils import openpy |
|
64 | from IPython.utils import openpy | |
65 | from IPython.utils.contexts import NoOpContext |
|
65 | from IPython.utils.contexts import NoOpContext | |
66 | from IPython.utils.decorators import undoc |
|
66 | from IPython.utils.decorators import undoc | |
67 | from IPython.utils.io import ask_yes_no |
|
67 | from IPython.utils.io import ask_yes_no | |
68 | from IPython.utils.ipstruct import Struct |
|
68 | from IPython.utils.ipstruct import Struct | |
69 | from IPython.paths import get_ipython_dir |
|
69 | from IPython.paths import get_ipython_dir | |
70 |
from IPython.utils.path import get_home_dir, get_py_filename, |
|
70 | from IPython.utils.path import get_home_dir, get_py_filename, ensure_dir_exists | |
71 | from IPython.utils.process import system, getoutput |
|
71 | from IPython.utils.process import system, getoutput | |
72 | from IPython.utils.py3compat import (builtin_mod, unicode_type, string_types, |
|
72 | from IPython.utils.py3compat import (builtin_mod, unicode_type, string_types, | |
73 | with_metaclass, iteritems) |
|
73 | with_metaclass, iteritems) | |
74 | from IPython.utils.strdispatch import StrDispatch |
|
74 | from IPython.utils.strdispatch import StrDispatch | |
75 | from IPython.utils.syspathcontext import prepended_to_syspath |
|
75 | from IPython.utils.syspathcontext import prepended_to_syspath | |
76 | from IPython.utils.text import (format_screen, LSString, SList, |
|
76 | from IPython.utils.text import (format_screen, LSString, SList, | |
77 | DollarFormatter) |
|
77 | DollarFormatter) | |
78 | from traitlets import ( |
|
78 | from traitlets import ( | |
79 | Integer, Bool, CaselessStrEnum, Enum, List, Dict, Unicode, Instance, Type, |
|
79 | Integer, Bool, CaselessStrEnum, Enum, List, Dict, Unicode, Instance, Type, | |
80 | observe, default, |
|
80 | observe, default, | |
81 | ) |
|
81 | ) | |
82 | from warnings import warn |
|
82 | from warnings import warn | |
83 | from logging import error |
|
83 | from logging import error | |
84 | import IPython.core.hooks |
|
84 | import IPython.core.hooks | |
85 |
|
85 | |||
86 | #----------------------------------------------------------------------------- |
|
86 | #----------------------------------------------------------------------------- | |
87 | # Globals |
|
87 | # Globals | |
88 | #----------------------------------------------------------------------------- |
|
88 | #----------------------------------------------------------------------------- | |
89 |
|
89 | |||
90 | # compiled regexps for autoindent management |
|
90 | # compiled regexps for autoindent management | |
91 | dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass') |
|
91 | dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass') | |
92 |
|
92 | |||
93 | #----------------------------------------------------------------------------- |
|
93 | #----------------------------------------------------------------------------- | |
94 | # Utilities |
|
94 | # Utilities | |
95 | #----------------------------------------------------------------------------- |
|
95 | #----------------------------------------------------------------------------- | |
96 |
|
96 | |||
97 | @undoc |
|
97 | @undoc | |
98 | def softspace(file, newvalue): |
|
98 | def softspace(file, newvalue): | |
99 | """Copied from code.py, to remove the dependency""" |
|
99 | """Copied from code.py, to remove the dependency""" | |
100 |
|
100 | |||
101 | oldvalue = 0 |
|
101 | oldvalue = 0 | |
102 | try: |
|
102 | try: | |
103 | oldvalue = file.softspace |
|
103 | oldvalue = file.softspace | |
104 | except AttributeError: |
|
104 | except AttributeError: | |
105 | pass |
|
105 | pass | |
106 | try: |
|
106 | try: | |
107 | file.softspace = newvalue |
|
107 | file.softspace = newvalue | |
108 | except (AttributeError, TypeError): |
|
108 | except (AttributeError, TypeError): | |
109 | # "attribute-less object" or "read-only attributes" |
|
109 | # "attribute-less object" or "read-only attributes" | |
110 | pass |
|
110 | pass | |
111 | return oldvalue |
|
111 | return oldvalue | |
112 |
|
112 | |||
113 | @undoc |
|
113 | @undoc | |
114 | def no_op(*a, **kw): pass |
|
114 | def no_op(*a, **kw): pass | |
115 |
|
115 | |||
116 |
|
116 | |||
117 | class SpaceInInput(Exception): pass |
|
117 | class SpaceInInput(Exception): pass | |
118 |
|
118 | |||
119 |
|
119 | |||
120 | def get_default_colors(): |
|
120 | def get_default_colors(): | |
121 | if sys.platform=='darwin': |
|
121 | if sys.platform=='darwin': | |
122 | return "LightBG" |
|
122 | return "LightBG" | |
123 | elif os.name=='nt': |
|
123 | elif os.name=='nt': | |
124 | return 'Linux' |
|
124 | return 'Linux' | |
125 | else: |
|
125 | else: | |
126 | return 'Linux' |
|
126 | return 'Linux' | |
127 |
|
127 | |||
128 |
|
128 | |||
129 | class SeparateUnicode(Unicode): |
|
129 | class SeparateUnicode(Unicode): | |
130 | r"""A Unicode subclass to validate separate_in, separate_out, etc. |
|
130 | r"""A Unicode subclass to validate separate_in, separate_out, etc. | |
131 |
|
131 | |||
132 | This is a Unicode based trait that converts '0'->'' and ``'\\n'->'\n'``. |
|
132 | This is a Unicode based trait that converts '0'->'' and ``'\\n'->'\n'``. | |
133 | """ |
|
133 | """ | |
134 |
|
134 | |||
135 | def validate(self, obj, value): |
|
135 | def validate(self, obj, value): | |
136 | if value == '0': value = '' |
|
136 | if value == '0': value = '' | |
137 | value = value.replace('\\n','\n') |
|
137 | value = value.replace('\\n','\n') | |
138 | return super(SeparateUnicode, self).validate(obj, value) |
|
138 | return super(SeparateUnicode, self).validate(obj, value) | |
139 |
|
139 | |||
140 |
|
140 | |||
141 | @undoc |
|
141 | @undoc | |
142 | class DummyMod(object): |
|
142 | class DummyMod(object): | |
143 | """A dummy module used for IPython's interactive module when |
|
143 | """A dummy module used for IPython's interactive module when | |
144 | a namespace must be assigned to the module's __dict__.""" |
|
144 | a namespace must be assigned to the module's __dict__.""" | |
145 | pass |
|
145 | pass | |
146 |
|
146 | |||
147 |
|
147 | |||
148 | class ExecutionResult(object): |
|
148 | class ExecutionResult(object): | |
149 | """The result of a call to :meth:`InteractiveShell.run_cell` |
|
149 | """The result of a call to :meth:`InteractiveShell.run_cell` | |
150 |
|
150 | |||
151 | Stores information about what took place. |
|
151 | Stores information about what took place. | |
152 | """ |
|
152 | """ | |
153 | execution_count = None |
|
153 | execution_count = None | |
154 | error_before_exec = None |
|
154 | error_before_exec = None | |
155 | error_in_exec = None |
|
155 | error_in_exec = None | |
156 | result = None |
|
156 | result = None | |
157 |
|
157 | |||
158 | @property |
|
158 | @property | |
159 | def success(self): |
|
159 | def success(self): | |
160 | return (self.error_before_exec is None) and (self.error_in_exec is None) |
|
160 | return (self.error_before_exec is None) and (self.error_in_exec is None) | |
161 |
|
161 | |||
162 | def raise_error(self): |
|
162 | def raise_error(self): | |
163 | """Reraises error if `success` is `False`, otherwise does nothing""" |
|
163 | """Reraises error if `success` is `False`, otherwise does nothing""" | |
164 | if self.error_before_exec is not None: |
|
164 | if self.error_before_exec is not None: | |
165 | raise self.error_before_exec |
|
165 | raise self.error_before_exec | |
166 | if self.error_in_exec is not None: |
|
166 | if self.error_in_exec is not None: | |
167 | raise self.error_in_exec |
|
167 | raise self.error_in_exec | |
168 |
|
168 | |||
169 |
|
169 | |||
170 | class InteractiveShell(SingletonConfigurable): |
|
170 | class InteractiveShell(SingletonConfigurable): | |
171 | """An enhanced, interactive shell for Python.""" |
|
171 | """An enhanced, interactive shell for Python.""" | |
172 |
|
172 | |||
173 | _instance = None |
|
173 | _instance = None | |
174 |
|
174 | |||
175 | ast_transformers = List([], help= |
|
175 | ast_transformers = List([], help= | |
176 | """ |
|
176 | """ | |
177 | A list of ast.NodeTransformer subclass instances, which will be applied |
|
177 | A list of ast.NodeTransformer subclass instances, which will be applied | |
178 | to user input before code is run. |
|
178 | to user input before code is run. | |
179 | """ |
|
179 | """ | |
180 | ).tag(config=True) |
|
180 | ).tag(config=True) | |
181 |
|
181 | |||
182 | autocall = Enum((0,1,2), default_value=0, help= |
|
182 | autocall = Enum((0,1,2), default_value=0, help= | |
183 | """ |
|
183 | """ | |
184 | Make IPython automatically call any callable object even if you didn't |
|
184 | Make IPython automatically call any callable object even if you didn't | |
185 | type explicit parentheses. For example, 'str 43' becomes 'str(43)' |
|
185 | type explicit parentheses. For example, 'str 43' becomes 'str(43)' | |
186 | automatically. The value can be '0' to disable the feature, '1' for |
|
186 | 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 |
|
187 | '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 |
|
188 | arguments on the line, and '2' for 'full' autocall, where all callable | |
189 | objects are automatically called (even if no arguments are present). |
|
189 | objects are automatically called (even if no arguments are present). | |
190 | """ |
|
190 | """ | |
191 | ).tag(config=True) |
|
191 | ).tag(config=True) | |
192 | # TODO: remove all autoindent logic and put into frontends. |
|
192 | # TODO: remove all autoindent logic and put into frontends. | |
193 | # We can't do this yet because even runlines uses the autoindent. |
|
193 | # We can't do this yet because even runlines uses the autoindent. | |
194 | autoindent = Bool(True, help= |
|
194 | autoindent = Bool(True, help= | |
195 | """ |
|
195 | """ | |
196 | Autoindent IPython code entered interactively. |
|
196 | Autoindent IPython code entered interactively. | |
197 | """ |
|
197 | """ | |
198 | ).tag(config=True) |
|
198 | ).tag(config=True) | |
199 | automagic = Bool(True, help= |
|
199 | automagic = Bool(True, help= | |
200 | """ |
|
200 | """ | |
201 | Enable magic commands to be called without the leading %. |
|
201 | Enable magic commands to be called without the leading %. | |
202 | """ |
|
202 | """ | |
203 | ).tag(config=True) |
|
203 | ).tag(config=True) | |
204 |
|
204 | |||
205 | banner1 = Unicode(default_banner, |
|
205 | banner1 = Unicode(default_banner, | |
206 | help="""The part of the banner to be printed before the profile""" |
|
206 | help="""The part of the banner to be printed before the profile""" | |
207 | ).tag(config=True) |
|
207 | ).tag(config=True) | |
208 | banner2 = Unicode('', |
|
208 | banner2 = Unicode('', | |
209 | help="""The part of the banner to be printed after the profile""" |
|
209 | help="""The part of the banner to be printed after the profile""" | |
210 | ).tag(config=True) |
|
210 | ).tag(config=True) | |
211 |
|
211 | |||
212 | cache_size = Integer(1000, help= |
|
212 | cache_size = Integer(1000, help= | |
213 | """ |
|
213 | """ | |
214 | Set the size of the output cache. The default is 1000, you can |
|
214 | 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 |
|
215 | 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 |
|
216 | 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 |
|
217 | 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 |
|
218 | issued). This limit is defined because otherwise you'll spend more | |
219 | time re-flushing a too small cache than working |
|
219 | time re-flushing a too small cache than working | |
220 | """ |
|
220 | """ | |
221 | ).tag(config=True) |
|
221 | ).tag(config=True) | |
222 | color_info = Bool(True, help= |
|
222 | color_info = Bool(True, help= | |
223 | """ |
|
223 | """ | |
224 | Use colors for displaying information about objects. Because this |
|
224 | Use colors for displaying information about objects. Because this | |
225 | information is passed through a pager (like 'less'), and some pagers |
|
225 | information is passed through a pager (like 'less'), and some pagers | |
226 | get confused with color codes, this capability can be turned off. |
|
226 | get confused with color codes, this capability can be turned off. | |
227 | """ |
|
227 | """ | |
228 | ).tag(config=True) |
|
228 | ).tag(config=True) | |
229 | colors = CaselessStrEnum(('NoColor','LightBG','Linux'), |
|
229 | colors = CaselessStrEnum(('NoColor','LightBG','Linux'), | |
230 | default_value=get_default_colors(), |
|
230 | default_value=get_default_colors(), | |
231 | help="Set the color scheme (NoColor, Linux, or LightBG)." |
|
231 | help="Set the color scheme (NoColor, Linux, or LightBG)." | |
232 | ).tag(config=True) |
|
232 | ).tag(config=True) | |
233 | colors_force = Bool(False, help= |
|
233 | colors_force = Bool(False, help= | |
234 | """ |
|
234 | """ | |
235 | Force use of ANSI color codes, regardless of OS and readline |
|
235 | Force use of ANSI color codes, regardless of OS and readline | |
236 | availability. |
|
236 | availability. | |
237 | """ |
|
237 | """ | |
238 | # FIXME: This is essentially a hack to allow ZMQShell to show colors |
|
238 | # FIXME: This is essentially a hack to allow ZMQShell to show colors | |
239 | # without readline on Win32. When the ZMQ formatting system is |
|
239 | # without readline on Win32. When the ZMQ formatting system is | |
240 | # refactored, this should be removed. |
|
240 | # refactored, this should be removed. | |
241 | ) |
|
241 | ) | |
242 | debug = Bool(False).tag(config=True) |
|
242 | debug = Bool(False).tag(config=True) | |
243 | deep_reload = Bool(False, help= |
|
243 | deep_reload = Bool(False, help= | |
244 | """ |
|
244 | """ | |
245 | **Deprecated** |
|
245 | **Deprecated** | |
246 |
|
246 | |||
247 | Will be removed in IPython 6.0 |
|
247 | Will be removed in IPython 6.0 | |
248 |
|
248 | |||
249 | Enable deep (recursive) reloading by default. IPython can use the |
|
249 | Enable deep (recursive) reloading by default. IPython can use the | |
250 | deep_reload module which reloads changes in modules recursively (it |
|
250 | deep_reload module which reloads changes in modules recursively (it | |
251 | replaces the reload() function, so you don't need to change anything to |
|
251 | 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 |
|
252 | use it). `deep_reload` forces a full reload of modules whose code may | |
253 | have changed, which the default reload() function does not. When |
|
253 | have changed, which the default reload() function does not. When | |
254 | deep_reload is off, IPython will use the normal reload(), but |
|
254 | deep_reload is off, IPython will use the normal reload(), but | |
255 | deep_reload will still be available as dreload(). |
|
255 | deep_reload will still be available as dreload(). | |
256 | """ |
|
256 | """ | |
257 | ).tag(config=True) |
|
257 | ).tag(config=True) | |
258 | disable_failing_post_execute = Bool(False, |
|
258 | disable_failing_post_execute = Bool(False, | |
259 | help="Don't call post-execute functions that have failed in the past." |
|
259 | help="Don't call post-execute functions that have failed in the past." | |
260 | ).tag(config=True) |
|
260 | ).tag(config=True) | |
261 | display_formatter = Instance(DisplayFormatter, allow_none=True) |
|
261 | display_formatter = Instance(DisplayFormatter, allow_none=True) | |
262 | displayhook_class = Type(DisplayHook) |
|
262 | displayhook_class = Type(DisplayHook) | |
263 | display_pub_class = Type(DisplayPublisher) |
|
263 | display_pub_class = Type(DisplayPublisher) | |
264 | data_pub_class = None |
|
264 | data_pub_class = None | |
265 |
|
265 | |||
266 | exit_now = Bool(False) |
|
266 | exit_now = Bool(False) | |
267 | exiter = Instance(ExitAutocall) |
|
267 | exiter = Instance(ExitAutocall) | |
268 | @default('exiter') |
|
268 | @default('exiter') | |
269 | def _exiter_default(self): |
|
269 | def _exiter_default(self): | |
270 | return ExitAutocall(self) |
|
270 | return ExitAutocall(self) | |
271 | # Monotonically increasing execution counter |
|
271 | # Monotonically increasing execution counter | |
272 | execution_count = Integer(1) |
|
272 | execution_count = Integer(1) | |
273 | filename = Unicode("<ipython console>") |
|
273 | filename = Unicode("<ipython console>") | |
274 | ipython_dir= Unicode('').tag(config=True) # Set to get_ipython_dir() in __init__ |
|
274 | ipython_dir= Unicode('').tag(config=True) # Set to get_ipython_dir() in __init__ | |
275 |
|
275 | |||
276 | # Input splitter, to transform input line by line and detect when a block |
|
276 | # Input splitter, to transform input line by line and detect when a block | |
277 | # is ready to be executed. |
|
277 | # is ready to be executed. | |
278 | input_splitter = Instance('IPython.core.inputsplitter.IPythonInputSplitter', |
|
278 | input_splitter = Instance('IPython.core.inputsplitter.IPythonInputSplitter', | |
279 | (), {'line_input_checker': True}) |
|
279 | (), {'line_input_checker': True}) | |
280 |
|
280 | |||
281 | # This InputSplitter instance is used to transform completed cells before |
|
281 | # This InputSplitter instance is used to transform completed cells before | |
282 | # running them. It allows cell magics to contain blank lines. |
|
282 | # running them. It allows cell magics to contain blank lines. | |
283 | input_transformer_manager = Instance('IPython.core.inputsplitter.IPythonInputSplitter', |
|
283 | input_transformer_manager = Instance('IPython.core.inputsplitter.IPythonInputSplitter', | |
284 | (), {'line_input_checker': False}) |
|
284 | (), {'line_input_checker': False}) | |
285 |
|
285 | |||
286 | logstart = Bool(False, help= |
|
286 | logstart = Bool(False, help= | |
287 | """ |
|
287 | """ | |
288 | Start logging to the default log file in overwrite mode. |
|
288 | Start logging to the default log file in overwrite mode. | |
289 | Use `logappend` to specify a log file to **append** logs to. |
|
289 | Use `logappend` to specify a log file to **append** logs to. | |
290 | """ |
|
290 | """ | |
291 | ).tag(config=True) |
|
291 | ).tag(config=True) | |
292 | logfile = Unicode('', help= |
|
292 | logfile = Unicode('', help= | |
293 | """ |
|
293 | """ | |
294 | The name of the logfile to use. |
|
294 | The name of the logfile to use. | |
295 | """ |
|
295 | """ | |
296 | ).tag(config=True) |
|
296 | ).tag(config=True) | |
297 | logappend = Unicode('', help= |
|
297 | logappend = Unicode('', help= | |
298 | """ |
|
298 | """ | |
299 | Start logging to the given file in append mode. |
|
299 | Start logging to the given file in append mode. | |
300 | Use `logfile` to specify a log file to **overwrite** logs to. |
|
300 | Use `logfile` to specify a log file to **overwrite** logs to. | |
301 | """ |
|
301 | """ | |
302 | ).tag(config=True) |
|
302 | ).tag(config=True) | |
303 | object_info_string_level = Enum((0,1,2), default_value=0, |
|
303 | object_info_string_level = Enum((0,1,2), default_value=0, | |
304 | ).tag(config=True) |
|
304 | ).tag(config=True) | |
305 | pdb = Bool(False, help= |
|
305 | pdb = Bool(False, help= | |
306 | """ |
|
306 | """ | |
307 | Automatically call the pdb debugger after every exception. |
|
307 | Automatically call the pdb debugger after every exception. | |
308 | """ |
|
308 | """ | |
309 | ).tag(config=True) |
|
309 | ).tag(config=True) | |
310 | multiline_history = Bool(sys.platform != 'win32', |
|
310 | multiline_history = Bool(sys.platform != 'win32', | |
311 | help="Save multi-line entries as one entry in readline history" |
|
311 | help="Save multi-line entries as one entry in readline history" | |
312 | ).tag(config=True) |
|
312 | ).tag(config=True) | |
313 | display_page = Bool(False, |
|
313 | display_page = Bool(False, | |
314 | help="""If True, anything that would be passed to the pager |
|
314 | help="""If True, anything that would be passed to the pager | |
315 | will be displayed as regular output instead.""" |
|
315 | will be displayed as regular output instead.""" | |
316 | ).tag(config=True) |
|
316 | ).tag(config=True) | |
317 |
|
317 | |||
318 | # deprecated prompt traits: |
|
318 | # deprecated prompt traits: | |
319 |
|
319 | |||
320 | prompt_in1 = Unicode('In [\\#]: ', |
|
320 | prompt_in1 = Unicode('In [\\#]: ', | |
321 | help="Deprecated, will be removed in IPython 5.0, use PromptManager.in_template" |
|
321 | help="Deprecated, will be removed in IPython 5.0, use PromptManager.in_template" | |
322 | ).tag(config=True) |
|
322 | ).tag(config=True) | |
323 | prompt_in2 = Unicode(' .\\D.: ', |
|
323 | prompt_in2 = Unicode(' .\\D.: ', | |
324 | help="Deprecated, will be removed in IPython 5.0, use PromptManager.in2_template" |
|
324 | help="Deprecated, will be removed in IPython 5.0, use PromptManager.in2_template" | |
325 | ).tag(config=True) |
|
325 | ).tag(config=True) | |
326 | prompt_out = Unicode('Out[\\#]: ', |
|
326 | prompt_out = Unicode('Out[\\#]: ', | |
327 | help="Deprecated, will be removed in IPython 5.0, use PromptManager.out_template" |
|
327 | help="Deprecated, will be removed in IPython 5.0, use PromptManager.out_template" | |
328 | ).tag(config=True) |
|
328 | ).tag(config=True) | |
329 | prompts_pad_left = Bool(True, |
|
329 | prompts_pad_left = Bool(True, | |
330 | help="Deprecated, will be removed in IPython 5.0, use PromptManager.justify" |
|
330 | help="Deprecated, will be removed in IPython 5.0, use PromptManager.justify" | |
331 | ).tag(config=True) |
|
331 | ).tag(config=True) | |
332 |
|
332 | |||
333 | @observe('prompt_in1', 'prompt_in2', 'prompt_out', 'prompt_pad_left') |
|
333 | @observe('prompt_in1', 'prompt_in2', 'prompt_out', 'prompt_pad_left') | |
334 | def _prompt_trait_changed(self, change): |
|
334 | def _prompt_trait_changed(self, change): | |
335 | table = { |
|
335 | table = { | |
336 | 'prompt_in1' : 'in_template', |
|
336 | 'prompt_in1' : 'in_template', | |
337 | 'prompt_in2' : 'in2_template', |
|
337 | 'prompt_in2' : 'in2_template', | |
338 | 'prompt_out' : 'out_template', |
|
338 | 'prompt_out' : 'out_template', | |
339 | 'prompts_pad_left' : 'justify', |
|
339 | 'prompts_pad_left' : 'justify', | |
340 | } |
|
340 | } | |
341 | name = change['name'] |
|
341 | name = change['name'] | |
342 | warn("InteractiveShell.{name} is deprecated, use PromptManager.{newname}".format( |
|
342 | warn("InteractiveShell.{name} is deprecated, use PromptManager.{newname}".format( | |
343 | name=name, newname=table[name]) |
|
343 | name=name, newname=table[name]) | |
344 | ) |
|
344 | ) | |
345 | # protect against weird cases where self.config may not exist: |
|
345 | # protect against weird cases where self.config may not exist: | |
346 | if self.config is not None: |
|
346 | if self.config is not None: | |
347 | # propagate to corresponding PromptManager trait |
|
347 | # propagate to corresponding PromptManager trait | |
348 | setattr(self.config.PromptManager, table[name], change['new']) |
|
348 | setattr(self.config.PromptManager, table[name], change['new']) | |
349 |
|
349 | |||
350 | show_rewritten_input = Bool(True, |
|
350 | show_rewritten_input = Bool(True, | |
351 | help="Show rewritten input, e.g. for autocall." |
|
351 | help="Show rewritten input, e.g. for autocall." | |
352 | ).tag(config=True) |
|
352 | ).tag(config=True) | |
353 |
|
353 | |||
354 | quiet = Bool(False).tag(config=True) |
|
354 | quiet = Bool(False).tag(config=True) | |
355 |
|
355 | |||
356 | history_length = Integer(10000, |
|
356 | history_length = Integer(10000, | |
357 | help='Total length of command history' |
|
357 | help='Total length of command history' | |
358 | ).tag(config=True) |
|
358 | ).tag(config=True) | |
359 |
|
359 | |||
360 | history_load_length = Integer(1000, help= |
|
360 | history_load_length = Integer(1000, help= | |
361 | """ |
|
361 | """ | |
362 | The number of saved history entries to be loaded |
|
362 | The number of saved history entries to be loaded | |
363 | into the readline buffer at startup. |
|
363 | into the readline buffer at startup. | |
364 | """ |
|
364 | """ | |
365 | ).tag(config=True) |
|
365 | ).tag(config=True) | |
366 |
|
366 | |||
367 | # The readline stuff will eventually be moved to the terminal subclass |
|
367 | # 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. |
|
368 | # but for now, we can't do that as readline is welded in everywhere. | |
369 | readline_use = Bool(True).tag(config=True) |
|
369 | readline_use = Bool(True).tag(config=True) | |
370 | readline_remove_delims = Unicode('-/~').tag(config=True) |
|
370 | readline_remove_delims = Unicode('-/~').tag(config=True) | |
371 | readline_delims = Unicode() # set by init_readline() |
|
371 | readline_delims = Unicode() # set by init_readline() | |
372 | # don't use \M- bindings by default, because they |
|
372 | # don't use \M- bindings by default, because they | |
373 | # conflict with 8-bit encodings. See gh-58,gh-88 |
|
373 | # conflict with 8-bit encodings. See gh-58,gh-88 | |
374 | readline_parse_and_bind = List([ |
|
374 | readline_parse_and_bind = List([ | |
375 | 'tab: complete', |
|
375 | 'tab: complete', | |
376 | '"\C-l": clear-screen', |
|
376 | '"\C-l": clear-screen', | |
377 | 'set show-all-if-ambiguous on', |
|
377 | 'set show-all-if-ambiguous on', | |
378 | '"\C-o": tab-insert', |
|
378 | '"\C-o": tab-insert', | |
379 | '"\C-r": reverse-search-history', |
|
379 | '"\C-r": reverse-search-history', | |
380 | '"\C-s": forward-search-history', |
|
380 | '"\C-s": forward-search-history', | |
381 | '"\C-p": history-search-backward', |
|
381 | '"\C-p": history-search-backward', | |
382 | '"\C-n": history-search-forward', |
|
382 | '"\C-n": history-search-forward', | |
383 | '"\e[A": history-search-backward', |
|
383 | '"\e[A": history-search-backward', | |
384 | '"\e[B": history-search-forward', |
|
384 | '"\e[B": history-search-forward', | |
385 | '"\C-k": kill-line', |
|
385 | '"\C-k": kill-line', | |
386 | '"\C-u": unix-line-discard', |
|
386 | '"\C-u": unix-line-discard', | |
387 | ]).tag(config=True) |
|
387 | ]).tag(config=True) | |
388 |
|
388 | |||
389 | _custom_readline_config = False |
|
389 | _custom_readline_config = False | |
390 |
|
390 | |||
391 | @observe('readline_parse_and_bind') |
|
391 | @observe('readline_parse_and_bind') | |
392 | def _readline_parse_and_bind_changed(self, change): |
|
392 | def _readline_parse_and_bind_changed(self, change): | |
393 | # notice that readline config is customized |
|
393 | # notice that readline config is customized | |
394 | # indicates that it should have higher priority than inputrc |
|
394 | # indicates that it should have higher priority than inputrc | |
395 | self._custom_readline_config = True |
|
395 | self._custom_readline_config = True | |
396 |
|
396 | |||
397 | ast_node_interactivity = Enum(['all', 'last', 'last_expr', 'none'], |
|
397 | ast_node_interactivity = Enum(['all', 'last', 'last_expr', 'none'], | |
398 | default_value='last_expr', |
|
398 | default_value='last_expr', | |
399 | help=""" |
|
399 | help=""" | |
400 | 'all', 'last', 'last_expr' or 'none', specifying which nodes should be |
|
400 | 'all', 'last', 'last_expr' or 'none', specifying which nodes should be | |
401 | run interactively (displaying output from expressions).""" |
|
401 | run interactively (displaying output from expressions).""" | |
402 | ).tag(config=True) |
|
402 | ).tag(config=True) | |
403 |
|
403 | |||
404 | # TODO: this part of prompt management should be moved to the frontends. |
|
404 | # TODO: this part of prompt management should be moved to the frontends. | |
405 | # Use custom TraitTypes that convert '0'->'' and '\\n'->'\n' |
|
405 | # Use custom TraitTypes that convert '0'->'' and '\\n'->'\n' | |
406 | separate_in = SeparateUnicode('\n').tag(config=True) |
|
406 | separate_in = SeparateUnicode('\n').tag(config=True) | |
407 | separate_out = SeparateUnicode('').tag(config=True) |
|
407 | separate_out = SeparateUnicode('').tag(config=True) | |
408 | separate_out2 = SeparateUnicode('').tag(config=True) |
|
408 | separate_out2 = SeparateUnicode('').tag(config=True) | |
409 | wildcards_case_sensitive = Bool(True).tag(config=True) |
|
409 | wildcards_case_sensitive = Bool(True).tag(config=True) | |
410 | xmode = CaselessStrEnum(('Context','Plain', 'Verbose'), |
|
410 | xmode = CaselessStrEnum(('Context','Plain', 'Verbose'), | |
411 | default_value='Context').tag(config=True) |
|
411 | default_value='Context').tag(config=True) | |
412 |
|
412 | |||
413 | # Subcomponents of InteractiveShell |
|
413 | # Subcomponents of InteractiveShell | |
414 | alias_manager = Instance('IPython.core.alias.AliasManager', allow_none=True) |
|
414 | alias_manager = Instance('IPython.core.alias.AliasManager', allow_none=True) | |
415 | prefilter_manager = Instance('IPython.core.prefilter.PrefilterManager', allow_none=True) |
|
415 | prefilter_manager = Instance('IPython.core.prefilter.PrefilterManager', allow_none=True) | |
416 | builtin_trap = Instance('IPython.core.builtin_trap.BuiltinTrap', allow_none=True) |
|
416 | builtin_trap = Instance('IPython.core.builtin_trap.BuiltinTrap', allow_none=True) | |
417 | display_trap = Instance('IPython.core.display_trap.DisplayTrap', allow_none=True) |
|
417 | display_trap = Instance('IPython.core.display_trap.DisplayTrap', allow_none=True) | |
418 | extension_manager = Instance('IPython.core.extensions.ExtensionManager', allow_none=True) |
|
418 | extension_manager = Instance('IPython.core.extensions.ExtensionManager', allow_none=True) | |
419 | payload_manager = Instance('IPython.core.payload.PayloadManager', allow_none=True) |
|
419 | payload_manager = Instance('IPython.core.payload.PayloadManager', allow_none=True) | |
420 | history_manager = Instance('IPython.core.history.HistoryAccessorBase', allow_none=True) |
|
420 | history_manager = Instance('IPython.core.history.HistoryAccessorBase', allow_none=True) | |
421 | magics_manager = Instance('IPython.core.magic.MagicsManager', allow_none=True) |
|
421 | magics_manager = Instance('IPython.core.magic.MagicsManager', allow_none=True) | |
422 |
|
422 | |||
423 | profile_dir = Instance('IPython.core.application.ProfileDir', allow_none=True) |
|
423 | profile_dir = Instance('IPython.core.application.ProfileDir', allow_none=True) | |
424 | @property |
|
424 | @property | |
425 | def profile(self): |
|
425 | def profile(self): | |
426 | if self.profile_dir is not None: |
|
426 | if self.profile_dir is not None: | |
427 | name = os.path.basename(self.profile_dir.location) |
|
427 | name = os.path.basename(self.profile_dir.location) | |
428 | return name.replace('profile_','') |
|
428 | return name.replace('profile_','') | |
429 |
|
429 | |||
430 |
|
430 | |||
431 | # Private interface |
|
431 | # Private interface | |
432 | _post_execute = Dict() |
|
432 | _post_execute = Dict() | |
433 |
|
433 | |||
434 | # Tracks any GUI loop loaded for pylab |
|
434 | # Tracks any GUI loop loaded for pylab | |
435 | pylab_gui_select = None |
|
435 | pylab_gui_select = None | |
436 |
|
436 | |||
437 | def __init__(self, ipython_dir=None, profile_dir=None, |
|
437 | def __init__(self, ipython_dir=None, profile_dir=None, | |
438 | user_module=None, user_ns=None, |
|
438 | user_module=None, user_ns=None, | |
439 | custom_exceptions=((), None), **kwargs): |
|
439 | custom_exceptions=((), None), **kwargs): | |
440 |
|
440 | |||
441 | # This is where traits with a config_key argument are updated |
|
441 | # This is where traits with a config_key argument are updated | |
442 | # from the values on config. |
|
442 | # from the values on config. | |
443 | super(InteractiveShell, self).__init__(**kwargs) |
|
443 | super(InteractiveShell, self).__init__(**kwargs) | |
444 | self.configurables = [self] |
|
444 | self.configurables = [self] | |
445 |
|
445 | |||
446 | # These are relatively independent and stateless |
|
446 | # These are relatively independent and stateless | |
447 | self.init_ipython_dir(ipython_dir) |
|
447 | self.init_ipython_dir(ipython_dir) | |
448 | self.init_profile_dir(profile_dir) |
|
448 | self.init_profile_dir(profile_dir) | |
449 | self.init_instance_attrs() |
|
449 | self.init_instance_attrs() | |
450 | self.init_environment() |
|
450 | self.init_environment() | |
451 |
|
451 | |||
452 | # Check if we're in a virtualenv, and set up sys.path. |
|
452 | # Check if we're in a virtualenv, and set up sys.path. | |
453 | self.init_virtualenv() |
|
453 | self.init_virtualenv() | |
454 |
|
454 | |||
455 | # Create namespaces (user_ns, user_global_ns, etc.) |
|
455 | # Create namespaces (user_ns, user_global_ns, etc.) | |
456 | self.init_create_namespaces(user_module, user_ns) |
|
456 | self.init_create_namespaces(user_module, user_ns) | |
457 | # This has to be done after init_create_namespaces because it uses |
|
457 | # This has to be done after init_create_namespaces because it uses | |
458 | # something in self.user_ns, but before init_sys_modules, which |
|
458 | # something in self.user_ns, but before init_sys_modules, which | |
459 | # is the first thing to modify sys. |
|
459 | # is the first thing to modify sys. | |
460 | # TODO: When we override sys.stdout and sys.stderr before this class |
|
460 | # 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 |
|
461 | # is created, we are saving the overridden ones here. Not sure if this | |
462 | # is what we want to do. |
|
462 | # is what we want to do. | |
463 | self.save_sys_module_state() |
|
463 | self.save_sys_module_state() | |
464 | self.init_sys_modules() |
|
464 | self.init_sys_modules() | |
465 |
|
465 | |||
466 | # While we're trying to have each part of the code directly access what |
|
466 | # 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 |
|
467 | # it needs without keeping redundant references to objects, we have too | |
468 | # much legacy code that expects ip.db to exist. |
|
468 | # much legacy code that expects ip.db to exist. | |
469 | self.db = PickleShareDB(os.path.join(self.profile_dir.location, 'db')) |
|
469 | self.db = PickleShareDB(os.path.join(self.profile_dir.location, 'db')) | |
470 |
|
470 | |||
471 | self.init_history() |
|
471 | self.init_history() | |
472 | self.init_encoding() |
|
472 | self.init_encoding() | |
473 | self.init_prefilter() |
|
473 | self.init_prefilter() | |
474 |
|
474 | |||
475 | self.init_syntax_highlighting() |
|
475 | self.init_syntax_highlighting() | |
476 | self.init_hooks() |
|
476 | self.init_hooks() | |
477 | self.init_events() |
|
477 | self.init_events() | |
478 | self.init_pushd_popd_magic() |
|
478 | self.init_pushd_popd_magic() | |
479 | # self.init_traceback_handlers use to be here, but we moved it below |
|
479 | # 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. |
|
480 | # because it and init_io have to come after init_readline. | |
481 | self.init_user_ns() |
|
481 | self.init_user_ns() | |
482 | self.init_logger() |
|
482 | self.init_logger() | |
483 | self.init_builtins() |
|
483 | self.init_builtins() | |
484 |
|
484 | |||
485 | # The following was in post_config_initialization |
|
485 | # The following was in post_config_initialization | |
486 | self.init_inspector() |
|
486 | self.init_inspector() | |
487 | # init_readline() must come before init_io(), because init_io uses |
|
487 | # init_readline() must come before init_io(), because init_io uses | |
488 | # readline related things. |
|
488 | # readline related things. | |
489 | self.init_readline() |
|
489 | self.init_readline() | |
490 | # We save this here in case user code replaces raw_input, but it needs |
|
490 | # 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 |
|
491 | # to be after init_readline(), because PyPy's readline works by replacing | |
492 | # raw_input. |
|
492 | # raw_input. | |
493 | if py3compat.PY3: |
|
493 | if py3compat.PY3: | |
494 | self.raw_input_original = input |
|
494 | self.raw_input_original = input | |
495 | else: |
|
495 | else: | |
496 | self.raw_input_original = raw_input |
|
496 | self.raw_input_original = raw_input | |
497 | # init_completer must come after init_readline, because it needs to |
|
497 | # init_completer must come after init_readline, because it needs to | |
498 | # know whether readline is present or not system-wide to configure the |
|
498 | # know whether readline is present or not system-wide to configure the | |
499 | # completers, since the completion machinery can now operate |
|
499 | # completers, since the completion machinery can now operate | |
500 | # independently of readline (e.g. over the network) |
|
500 | # independently of readline (e.g. over the network) | |
501 | self.init_completer() |
|
501 | self.init_completer() | |
502 | # TODO: init_io() needs to happen before init_traceback handlers |
|
502 | # TODO: init_io() needs to happen before init_traceback handlers | |
503 | # because the traceback handlers hardcode the stdout/stderr streams. |
|
503 | # because the traceback handlers hardcode the stdout/stderr streams. | |
504 | # This logic in in debugger.Pdb and should eventually be changed. |
|
504 | # This logic in in debugger.Pdb and should eventually be changed. | |
505 | self.init_io() |
|
505 | self.init_io() | |
506 | self.init_traceback_handlers(custom_exceptions) |
|
506 | self.init_traceback_handlers(custom_exceptions) | |
507 | self.init_prompts() |
|
507 | self.init_prompts() | |
508 | self.init_display_formatter() |
|
508 | self.init_display_formatter() | |
509 | self.init_display_pub() |
|
509 | self.init_display_pub() | |
510 | self.init_data_pub() |
|
510 | self.init_data_pub() | |
511 | self.init_displayhook() |
|
511 | self.init_displayhook() | |
512 | self.init_magics() |
|
512 | self.init_magics() | |
513 | self.init_alias() |
|
513 | self.init_alias() | |
514 | self.init_logstart() |
|
514 | self.init_logstart() | |
515 | self.init_pdb() |
|
515 | self.init_pdb() | |
516 | self.init_extension_manager() |
|
516 | self.init_extension_manager() | |
517 | self.init_payload() |
|
517 | self.init_payload() | |
518 | self.init_deprecation_warnings() |
|
518 | self.init_deprecation_warnings() | |
519 | self.hooks.late_startup_hook() |
|
519 | self.hooks.late_startup_hook() | |
520 | self.events.trigger('shell_initialized', self) |
|
520 | self.events.trigger('shell_initialized', self) | |
521 | atexit.register(self.atexit_operations) |
|
521 | atexit.register(self.atexit_operations) | |
522 |
|
522 | |||
523 | def get_ipython(self): |
|
523 | def get_ipython(self): | |
524 | """Return the currently running IPython instance.""" |
|
524 | """Return the currently running IPython instance.""" | |
525 | return self |
|
525 | return self | |
526 |
|
526 | |||
527 | #------------------------------------------------------------------------- |
|
527 | #------------------------------------------------------------------------- | |
528 | # Trait changed handlers |
|
528 | # Trait changed handlers | |
529 | #------------------------------------------------------------------------- |
|
529 | #------------------------------------------------------------------------- | |
530 | @observe('ipython_dir') |
|
530 | @observe('ipython_dir') | |
531 | def _ipython_dir_changed(self, change): |
|
531 | def _ipython_dir_changed(self, change): | |
532 | ensure_dir_exists(change['new']) |
|
532 | ensure_dir_exists(change['new']) | |
533 |
|
533 | |||
534 | def set_autoindent(self,value=None): |
|
534 | def set_autoindent(self,value=None): | |
535 | """Set the autoindent flag. |
|
535 | """Set the autoindent flag. | |
536 |
|
536 | |||
537 | If called with no arguments, it acts as a toggle.""" |
|
537 | If called with no arguments, it acts as a toggle.""" | |
538 | if value is None: |
|
538 | if value is None: | |
539 | self.autoindent = not self.autoindent |
|
539 | self.autoindent = not self.autoindent | |
540 | else: |
|
540 | else: | |
541 | self.autoindent = value |
|
541 | self.autoindent = value | |
542 |
|
542 | |||
543 | #------------------------------------------------------------------------- |
|
543 | #------------------------------------------------------------------------- | |
544 | # init_* methods called by __init__ |
|
544 | # init_* methods called by __init__ | |
545 | #------------------------------------------------------------------------- |
|
545 | #------------------------------------------------------------------------- | |
546 |
|
546 | |||
547 | def init_ipython_dir(self, ipython_dir): |
|
547 | def init_ipython_dir(self, ipython_dir): | |
548 | if ipython_dir is not None: |
|
548 | if ipython_dir is not None: | |
549 | self.ipython_dir = ipython_dir |
|
549 | self.ipython_dir = ipython_dir | |
550 | return |
|
550 | return | |
551 |
|
551 | |||
552 | self.ipython_dir = get_ipython_dir() |
|
552 | self.ipython_dir = get_ipython_dir() | |
553 |
|
553 | |||
554 | def init_profile_dir(self, profile_dir): |
|
554 | def init_profile_dir(self, profile_dir): | |
555 | if profile_dir is not None: |
|
555 | if profile_dir is not None: | |
556 | self.profile_dir = profile_dir |
|
556 | self.profile_dir = profile_dir | |
557 | return |
|
557 | return | |
558 | self.profile_dir =\ |
|
558 | self.profile_dir =\ | |
559 | ProfileDir.create_profile_dir_by_name(self.ipython_dir, 'default') |
|
559 | ProfileDir.create_profile_dir_by_name(self.ipython_dir, 'default') | |
560 |
|
560 | |||
561 | def init_instance_attrs(self): |
|
561 | def init_instance_attrs(self): | |
562 | self.more = False |
|
562 | self.more = False | |
563 |
|
563 | |||
564 | # command compiler |
|
564 | # command compiler | |
565 | self.compile = CachingCompiler() |
|
565 | self.compile = CachingCompiler() | |
566 |
|
566 | |||
567 | # Make an empty namespace, which extension writers can rely on both |
|
567 | # Make an empty namespace, which extension writers can rely on both | |
568 | # existing and NEVER being used by ipython itself. This gives them a |
|
568 | # existing and NEVER being used by ipython itself. This gives them a | |
569 | # convenient location for storing additional information and state |
|
569 | # convenient location for storing additional information and state | |
570 | # their extensions may require, without fear of collisions with other |
|
570 | # their extensions may require, without fear of collisions with other | |
571 | # ipython names that may develop later. |
|
571 | # ipython names that may develop later. | |
572 | self.meta = Struct() |
|
572 | self.meta = Struct() | |
573 |
|
573 | |||
574 | # Temporary files used for various purposes. Deleted at exit. |
|
574 | # Temporary files used for various purposes. Deleted at exit. | |
575 | self.tempfiles = [] |
|
575 | self.tempfiles = [] | |
576 | self.tempdirs = [] |
|
576 | self.tempdirs = [] | |
577 |
|
577 | |||
578 | # Keep track of readline usage (later set by init_readline) |
|
578 | # Keep track of readline usage (later set by init_readline) | |
579 | self.has_readline = False |
|
579 | self.has_readline = False | |
580 |
|
580 | |||
581 | # keep track of where we started running (mainly for crash post-mortem) |
|
581 | # keep track of where we started running (mainly for crash post-mortem) | |
582 | # This is not being used anywhere currently. |
|
582 | # This is not being used anywhere currently. | |
583 | self.starting_dir = py3compat.getcwd() |
|
583 | self.starting_dir = py3compat.getcwd() | |
584 |
|
584 | |||
585 | # Indentation management |
|
585 | # Indentation management | |
586 | self.indent_current_nsp = 0 |
|
586 | self.indent_current_nsp = 0 | |
587 |
|
587 | |||
588 | # Dict to track post-execution functions that have been registered |
|
588 | # Dict to track post-execution functions that have been registered | |
589 | self._post_execute = {} |
|
589 | self._post_execute = {} | |
590 |
|
590 | |||
591 | def init_environment(self): |
|
591 | def init_environment(self): | |
592 | """Any changes we need to make to the user's environment.""" |
|
592 | """Any changes we need to make to the user's environment.""" | |
593 | pass |
|
593 | pass | |
594 |
|
594 | |||
595 | def init_encoding(self): |
|
595 | def init_encoding(self): | |
596 | # Get system encoding at startup time. Certain terminals (like Emacs |
|
596 | # 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 |
|
597 | # under Win32 have it set to None, and we need to have a known valid | |
598 | # encoding to use in the raw_input() method |
|
598 | # encoding to use in the raw_input() method | |
599 | try: |
|
599 | try: | |
600 | self.stdin_encoding = sys.stdin.encoding or 'ascii' |
|
600 | self.stdin_encoding = sys.stdin.encoding or 'ascii' | |
601 | except AttributeError: |
|
601 | except AttributeError: | |
602 | self.stdin_encoding = 'ascii' |
|
602 | self.stdin_encoding = 'ascii' | |
603 |
|
603 | |||
604 | def init_syntax_highlighting(self): |
|
604 | def init_syntax_highlighting(self): | |
605 | # Python source parser/formatter for syntax highlighting |
|
605 | # Python source parser/formatter for syntax highlighting | |
606 | pyformat = PyColorize.Parser().format |
|
606 | pyformat = PyColorize.Parser().format | |
607 | self.pycolorize = lambda src: pyformat(src,'str',self.colors) |
|
607 | self.pycolorize = lambda src: pyformat(src,'str',self.colors) | |
608 |
|
608 | |||
609 | def init_pushd_popd_magic(self): |
|
609 | def init_pushd_popd_magic(self): | |
610 | # for pushd/popd management |
|
610 | # for pushd/popd management | |
611 | self.home_dir = get_home_dir() |
|
611 | self.home_dir = get_home_dir() | |
612 |
|
612 | |||
613 | self.dir_stack = [] |
|
613 | self.dir_stack = [] | |
614 |
|
614 | |||
615 | def init_logger(self): |
|
615 | def init_logger(self): | |
616 | self.logger = Logger(self.home_dir, logfname='ipython_log.py', |
|
616 | self.logger = Logger(self.home_dir, logfname='ipython_log.py', | |
617 | logmode='rotate') |
|
617 | logmode='rotate') | |
618 |
|
618 | |||
619 | def init_logstart(self): |
|
619 | def init_logstart(self): | |
620 | """Initialize logging in case it was requested at the command line. |
|
620 | """Initialize logging in case it was requested at the command line. | |
621 | """ |
|
621 | """ | |
622 | if self.logappend: |
|
622 | if self.logappend: | |
623 | self.magic('logstart %s append' % self.logappend) |
|
623 | self.magic('logstart %s append' % self.logappend) | |
624 | elif self.logfile: |
|
624 | elif self.logfile: | |
625 | self.magic('logstart %s' % self.logfile) |
|
625 | self.magic('logstart %s' % self.logfile) | |
626 | elif self.logstart: |
|
626 | elif self.logstart: | |
627 | self.magic('logstart') |
|
627 | self.magic('logstart') | |
628 |
|
628 | |||
629 | def init_deprecation_warnings(self): |
|
629 | def init_deprecation_warnings(self): | |
630 | """ |
|
630 | """ | |
631 | register default filter for deprecation warning. |
|
631 | register default filter for deprecation warning. | |
632 |
|
632 | |||
633 | This will allow deprecation warning of function used interactively to show |
|
633 | This will allow deprecation warning of function used interactively to show | |
634 | warning to users, and still hide deprecation warning from libraries import. |
|
634 | warning to users, and still hide deprecation warning from libraries import. | |
635 | """ |
|
635 | """ | |
636 | warnings.filterwarnings("default", category=DeprecationWarning, module=self.user_ns.get("__name__")) |
|
636 | warnings.filterwarnings("default", category=DeprecationWarning, module=self.user_ns.get("__name__")) | |
637 |
|
637 | |||
638 | def init_builtins(self): |
|
638 | def init_builtins(self): | |
639 | # A single, static flag that we set to True. Its presence indicates |
|
639 | # 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 |
|
640 | # 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 |
|
641 | # removing on exit or representing the existence of more than one | |
642 | # IPython at a time. |
|
642 | # IPython at a time. | |
643 | builtin_mod.__dict__['__IPYTHON__'] = True |
|
643 | builtin_mod.__dict__['__IPYTHON__'] = True | |
644 |
|
644 | |||
645 | self.builtin_trap = BuiltinTrap(shell=self) |
|
645 | self.builtin_trap = BuiltinTrap(shell=self) | |
646 |
|
646 | |||
647 | def init_inspector(self): |
|
647 | def init_inspector(self): | |
648 | # Object inspector |
|
648 | # Object inspector | |
649 | self.inspector = oinspect.Inspector(oinspect.InspectColors, |
|
649 | self.inspector = oinspect.Inspector(oinspect.InspectColors, | |
650 | PyColorize.ANSICodeColors, |
|
650 | PyColorize.ANSICodeColors, | |
651 | 'NoColor', |
|
651 | 'NoColor', | |
652 | self.object_info_string_level) |
|
652 | self.object_info_string_level) | |
653 |
|
653 | |||
654 | def init_io(self): |
|
654 | def init_io(self): | |
655 | # This will just use sys.stdout and sys.stderr. If you want to |
|
655 | # This will just use sys.stdout and sys.stderr. If you want to | |
656 | # override sys.stdout and sys.stderr themselves, you need to do that |
|
656 | # override sys.stdout and sys.stderr themselves, you need to do that | |
657 | # *before* instantiating this class, because io holds onto |
|
657 | # *before* instantiating this class, because io holds onto | |
658 | # references to the underlying streams. |
|
658 | # references to the underlying streams. | |
659 | if (sys.platform == 'win32' or sys.platform == 'cli') and self.has_readline: |
|
659 | if (sys.platform == 'win32' or sys.platform == 'cli') and self.has_readline: | |
660 | io.stdout = io.stderr = io.IOStream(self.readline._outputfile) |
|
660 | io.stdout = io.stderr = io.IOStream(self.readline._outputfile) | |
661 | else: |
|
661 | else: | |
662 | io.stdout = io.IOStream(sys.stdout) |
|
662 | io.stdout = io.IOStream(sys.stdout) | |
663 | io.stderr = io.IOStream(sys.stderr) |
|
663 | io.stderr = io.IOStream(sys.stderr) | |
664 |
|
664 | |||
665 | def init_prompts(self): |
|
665 | def init_prompts(self): | |
666 | # Set system prompts, so that scripts can decide if they are running |
|
666 | # Set system prompts, so that scripts can decide if they are running | |
667 | # interactively. |
|
667 | # interactively. | |
668 | sys.ps1 = 'In : ' |
|
668 | sys.ps1 = 'In : ' | |
669 | sys.ps2 = '...: ' |
|
669 | sys.ps2 = '...: ' | |
670 | sys.ps3 = 'Out: ' |
|
670 | sys.ps3 = 'Out: ' | |
671 |
|
671 | |||
672 | def init_display_formatter(self): |
|
672 | def init_display_formatter(self): | |
673 | self.display_formatter = DisplayFormatter(parent=self) |
|
673 | self.display_formatter = DisplayFormatter(parent=self) | |
674 | self.configurables.append(self.display_formatter) |
|
674 | self.configurables.append(self.display_formatter) | |
675 |
|
675 | |||
676 | def init_display_pub(self): |
|
676 | def init_display_pub(self): | |
677 | self.display_pub = self.display_pub_class(parent=self) |
|
677 | self.display_pub = self.display_pub_class(parent=self) | |
678 | self.configurables.append(self.display_pub) |
|
678 | self.configurables.append(self.display_pub) | |
679 |
|
679 | |||
680 | def init_data_pub(self): |
|
680 | def init_data_pub(self): | |
681 | if not self.data_pub_class: |
|
681 | if not self.data_pub_class: | |
682 | self.data_pub = None |
|
682 | self.data_pub = None | |
683 | return |
|
683 | return | |
684 | self.data_pub = self.data_pub_class(parent=self) |
|
684 | self.data_pub = self.data_pub_class(parent=self) | |
685 | self.configurables.append(self.data_pub) |
|
685 | self.configurables.append(self.data_pub) | |
686 |
|
686 | |||
687 | def init_displayhook(self): |
|
687 | def init_displayhook(self): | |
688 | # Initialize displayhook, set in/out prompts and printing system |
|
688 | # Initialize displayhook, set in/out prompts and printing system | |
689 | self.displayhook = self.displayhook_class( |
|
689 | self.displayhook = self.displayhook_class( | |
690 | parent=self, |
|
690 | parent=self, | |
691 | shell=self, |
|
691 | shell=self, | |
692 | cache_size=self.cache_size, |
|
692 | cache_size=self.cache_size, | |
693 | ) |
|
693 | ) | |
694 | self.configurables.append(self.displayhook) |
|
694 | self.configurables.append(self.displayhook) | |
695 | # This is a context manager that installs/revmoes the displayhook at |
|
695 | # This is a context manager that installs/revmoes the displayhook at | |
696 | # the appropriate time. |
|
696 | # the appropriate time. | |
697 | self.display_trap = DisplayTrap(hook=self.displayhook) |
|
697 | self.display_trap = DisplayTrap(hook=self.displayhook) | |
698 |
|
698 | |||
699 | def init_virtualenv(self): |
|
699 | def init_virtualenv(self): | |
700 | """Add a virtualenv to sys.path so the user can import modules from it. |
|
700 | """Add a virtualenv to sys.path so the user can import modules from it. | |
701 | This isn't perfect: it doesn't use the Python interpreter with which the |
|
701 | This isn't perfect: it doesn't use the Python interpreter with which the | |
702 | virtualenv was built, and it ignores the --no-site-packages option. A |
|
702 | virtualenv was built, and it ignores the --no-site-packages option. A | |
703 | warning will appear suggesting the user installs IPython in the |
|
703 | warning will appear suggesting the user installs IPython in the | |
704 | virtualenv, but for many cases, it probably works well enough. |
|
704 | virtualenv, but for many cases, it probably works well enough. | |
705 |
|
705 | |||
706 | Adapted from code snippets online. |
|
706 | Adapted from code snippets online. | |
707 |
|
707 | |||
708 | http://blog.ufsoft.org/2009/1/29/ipython-and-virtualenv |
|
708 | http://blog.ufsoft.org/2009/1/29/ipython-and-virtualenv | |
709 | """ |
|
709 | """ | |
710 | if 'VIRTUAL_ENV' not in os.environ: |
|
710 | if 'VIRTUAL_ENV' not in os.environ: | |
711 | # Not in a virtualenv |
|
711 | # Not in a virtualenv | |
712 | return |
|
712 | return | |
713 |
|
713 | |||
714 | # venv detection: |
|
714 | # venv detection: | |
715 | # stdlib venv may symlink sys.executable, so we can't use realpath. |
|
715 | # stdlib venv may symlink sys.executable, so we can't use realpath. | |
716 | # but others can symlink *to* the venv Python, so we can't just use sys.executable. |
|
716 | # but others can symlink *to* the venv Python, so we can't just use sys.executable. | |
717 | # So we just check every item in the symlink tree (generally <= 3) |
|
717 | # So we just check every item in the symlink tree (generally <= 3) | |
718 | p = os.path.normcase(sys.executable) |
|
718 | p = os.path.normcase(sys.executable) | |
719 | paths = [p] |
|
719 | paths = [p] | |
720 | while os.path.islink(p): |
|
720 | while os.path.islink(p): | |
721 | p = os.path.normcase(os.path.join(os.path.dirname(p), os.readlink(p))) |
|
721 | p = os.path.normcase(os.path.join(os.path.dirname(p), os.readlink(p))) | |
722 | paths.append(p) |
|
722 | paths.append(p) | |
723 | p_venv = os.path.normcase(os.environ['VIRTUAL_ENV']) |
|
723 | p_venv = os.path.normcase(os.environ['VIRTUAL_ENV']) | |
724 | if any(p.startswith(p_venv) for p in paths): |
|
724 | if any(p.startswith(p_venv) for p in paths): | |
725 | # Running properly in the virtualenv, don't need to do anything |
|
725 | # Running properly in the virtualenv, don't need to do anything | |
726 | return |
|
726 | return | |
727 |
|
727 | |||
728 | warn("Attempting to work in a virtualenv. If you encounter problems, please " |
|
728 | warn("Attempting to work in a virtualenv. If you encounter problems, please " | |
729 | "install IPython inside the virtualenv.") |
|
729 | "install IPython inside the virtualenv.") | |
730 | if sys.platform == "win32": |
|
730 | if sys.platform == "win32": | |
731 | virtual_env = os.path.join(os.environ['VIRTUAL_ENV'], 'Lib', 'site-packages') |
|
731 | virtual_env = os.path.join(os.environ['VIRTUAL_ENV'], 'Lib', 'site-packages') | |
732 | else: |
|
732 | else: | |
733 | virtual_env = os.path.join(os.environ['VIRTUAL_ENV'], 'lib', |
|
733 | virtual_env = os.path.join(os.environ['VIRTUAL_ENV'], 'lib', | |
734 | 'python%d.%d' % sys.version_info[:2], 'site-packages') |
|
734 | 'python%d.%d' % sys.version_info[:2], 'site-packages') | |
735 |
|
735 | |||
736 | import site |
|
736 | import site | |
737 | sys.path.insert(0, virtual_env) |
|
737 | sys.path.insert(0, virtual_env) | |
738 | site.addsitedir(virtual_env) |
|
738 | site.addsitedir(virtual_env) | |
739 |
|
739 | |||
740 | #------------------------------------------------------------------------- |
|
740 | #------------------------------------------------------------------------- | |
741 | # Things related to injections into the sys module |
|
741 | # Things related to injections into the sys module | |
742 | #------------------------------------------------------------------------- |
|
742 | #------------------------------------------------------------------------- | |
743 |
|
743 | |||
744 | def save_sys_module_state(self): |
|
744 | def save_sys_module_state(self): | |
745 | """Save the state of hooks in the sys module. |
|
745 | """Save the state of hooks in the sys module. | |
746 |
|
746 | |||
747 | This has to be called after self.user_module is created. |
|
747 | This has to be called after self.user_module is created. | |
748 | """ |
|
748 | """ | |
749 | self._orig_sys_module_state = {'stdin': sys.stdin, |
|
749 | self._orig_sys_module_state = {'stdin': sys.stdin, | |
750 | 'stdout': sys.stdout, |
|
750 | 'stdout': sys.stdout, | |
751 | 'stderr': sys.stderr, |
|
751 | 'stderr': sys.stderr, | |
752 | 'excepthook': sys.excepthook} |
|
752 | 'excepthook': sys.excepthook} | |
753 | self._orig_sys_modules_main_name = self.user_module.__name__ |
|
753 | self._orig_sys_modules_main_name = self.user_module.__name__ | |
754 | self._orig_sys_modules_main_mod = sys.modules.get(self.user_module.__name__) |
|
754 | self._orig_sys_modules_main_mod = sys.modules.get(self.user_module.__name__) | |
755 |
|
755 | |||
756 | def restore_sys_module_state(self): |
|
756 | def restore_sys_module_state(self): | |
757 | """Restore the state of the sys module.""" |
|
757 | """Restore the state of the sys module.""" | |
758 | try: |
|
758 | try: | |
759 | for k, v in iteritems(self._orig_sys_module_state): |
|
759 | for k, v in iteritems(self._orig_sys_module_state): | |
760 | setattr(sys, k, v) |
|
760 | setattr(sys, k, v) | |
761 | except AttributeError: |
|
761 | except AttributeError: | |
762 | pass |
|
762 | pass | |
763 | # Reset what what done in self.init_sys_modules |
|
763 | # Reset what what done in self.init_sys_modules | |
764 | if self._orig_sys_modules_main_mod is not None: |
|
764 | if self._orig_sys_modules_main_mod is not None: | |
765 | sys.modules[self._orig_sys_modules_main_name] = self._orig_sys_modules_main_mod |
|
765 | sys.modules[self._orig_sys_modules_main_name] = self._orig_sys_modules_main_mod | |
766 |
|
766 | |||
767 | #------------------------------------------------------------------------- |
|
767 | #------------------------------------------------------------------------- | |
768 | # Things related to the banner |
|
768 | # Things related to the banner | |
769 | #------------------------------------------------------------------------- |
|
769 | #------------------------------------------------------------------------- | |
770 |
|
770 | |||
771 | @property |
|
771 | @property | |
772 | def banner(self): |
|
772 | def banner(self): | |
773 | banner = self.banner1 |
|
773 | banner = self.banner1 | |
774 | if self.profile and self.profile != 'default': |
|
774 | if self.profile and self.profile != 'default': | |
775 | banner += '\nIPython profile: %s\n' % self.profile |
|
775 | banner += '\nIPython profile: %s\n' % self.profile | |
776 | if self.banner2: |
|
776 | if self.banner2: | |
777 | banner += '\n' + self.banner2 |
|
777 | banner += '\n' + self.banner2 | |
778 | return banner |
|
778 | return banner | |
779 |
|
779 | |||
780 | def show_banner(self, banner=None): |
|
780 | def show_banner(self, banner=None): | |
781 | if banner is None: |
|
781 | if banner is None: | |
782 | banner = self.banner |
|
782 | banner = self.banner | |
783 | sys.stdout.write(banner) |
|
783 | sys.stdout.write(banner) | |
784 |
|
784 | |||
785 | #------------------------------------------------------------------------- |
|
785 | #------------------------------------------------------------------------- | |
786 | # Things related to hooks |
|
786 | # Things related to hooks | |
787 | #------------------------------------------------------------------------- |
|
787 | #------------------------------------------------------------------------- | |
788 |
|
788 | |||
789 | def init_hooks(self): |
|
789 | def init_hooks(self): | |
790 | # hooks holds pointers used for user-side customizations |
|
790 | # hooks holds pointers used for user-side customizations | |
791 | self.hooks = Struct() |
|
791 | self.hooks = Struct() | |
792 |
|
792 | |||
793 | self.strdispatchers = {} |
|
793 | self.strdispatchers = {} | |
794 |
|
794 | |||
795 | # Set all default hooks, defined in the IPython.hooks module. |
|
795 | # Set all default hooks, defined in the IPython.hooks module. | |
796 | hooks = IPython.core.hooks |
|
796 | hooks = IPython.core.hooks | |
797 | for hook_name in hooks.__all__: |
|
797 | for hook_name in hooks.__all__: | |
798 | # default hooks have priority 100, i.e. low; user hooks should have |
|
798 | # default hooks have priority 100, i.e. low; user hooks should have | |
799 | # 0-100 priority |
|
799 | # 0-100 priority | |
800 | self.set_hook(hook_name,getattr(hooks,hook_name), 100, _warn_deprecated=False) |
|
800 | self.set_hook(hook_name,getattr(hooks,hook_name), 100, _warn_deprecated=False) | |
801 |
|
801 | |||
802 | if self.display_page: |
|
802 | if self.display_page: | |
803 | self.set_hook('show_in_pager', page.as_hook(page.display_page), 90) |
|
803 | self.set_hook('show_in_pager', page.as_hook(page.display_page), 90) | |
804 |
|
804 | |||
805 | def set_hook(self,name,hook, priority=50, str_key=None, re_key=None, |
|
805 | def set_hook(self,name,hook, priority=50, str_key=None, re_key=None, | |
806 | _warn_deprecated=True): |
|
806 | _warn_deprecated=True): | |
807 | """set_hook(name,hook) -> sets an internal IPython hook. |
|
807 | """set_hook(name,hook) -> sets an internal IPython hook. | |
808 |
|
808 | |||
809 | IPython exposes some of its internal API as user-modifiable hooks. By |
|
809 | IPython exposes some of its internal API as user-modifiable hooks. By | |
810 | adding your function to one of these hooks, you can modify IPython's |
|
810 | adding your function to one of these hooks, you can modify IPython's | |
811 | behavior to call at runtime your own routines.""" |
|
811 | behavior to call at runtime your own routines.""" | |
812 |
|
812 | |||
813 | # At some point in the future, this should validate the hook before it |
|
813 | # At some point in the future, this should validate the hook before it | |
814 | # accepts it. Probably at least check that the hook takes the number |
|
814 | # accepts it. Probably at least check that the hook takes the number | |
815 | # of args it's supposed to. |
|
815 | # of args it's supposed to. | |
816 |
|
816 | |||
817 | f = types.MethodType(hook,self) |
|
817 | f = types.MethodType(hook,self) | |
818 |
|
818 | |||
819 | # check if the hook is for strdispatcher first |
|
819 | # check if the hook is for strdispatcher first | |
820 | if str_key is not None: |
|
820 | if str_key is not None: | |
821 | sdp = self.strdispatchers.get(name, StrDispatch()) |
|
821 | sdp = self.strdispatchers.get(name, StrDispatch()) | |
822 | sdp.add_s(str_key, f, priority ) |
|
822 | sdp.add_s(str_key, f, priority ) | |
823 | self.strdispatchers[name] = sdp |
|
823 | self.strdispatchers[name] = sdp | |
824 | return |
|
824 | return | |
825 | if re_key is not None: |
|
825 | if re_key is not None: | |
826 | sdp = self.strdispatchers.get(name, StrDispatch()) |
|
826 | sdp = self.strdispatchers.get(name, StrDispatch()) | |
827 | sdp.add_re(re.compile(re_key), f, priority ) |
|
827 | sdp.add_re(re.compile(re_key), f, priority ) | |
828 | self.strdispatchers[name] = sdp |
|
828 | self.strdispatchers[name] = sdp | |
829 | return |
|
829 | return | |
830 |
|
830 | |||
831 | dp = getattr(self.hooks, name, None) |
|
831 | dp = getattr(self.hooks, name, None) | |
832 | if name not in IPython.core.hooks.__all__: |
|
832 | if name not in IPython.core.hooks.__all__: | |
833 | print("Warning! Hook '%s' is not one of %s" % \ |
|
833 | print("Warning! Hook '%s' is not one of %s" % \ | |
834 | (name, IPython.core.hooks.__all__ )) |
|
834 | (name, IPython.core.hooks.__all__ )) | |
835 |
|
835 | |||
836 | if _warn_deprecated and (name in IPython.core.hooks.deprecated): |
|
836 | if _warn_deprecated and (name in IPython.core.hooks.deprecated): | |
837 | alternative = IPython.core.hooks.deprecated[name] |
|
837 | alternative = IPython.core.hooks.deprecated[name] | |
838 | warn("Hook {} is deprecated. Use {} instead.".format(name, alternative)) |
|
838 | warn("Hook {} is deprecated. Use {} instead.".format(name, alternative)) | |
839 |
|
839 | |||
840 | if not dp: |
|
840 | if not dp: | |
841 | dp = IPython.core.hooks.CommandChainDispatcher() |
|
841 | dp = IPython.core.hooks.CommandChainDispatcher() | |
842 |
|
842 | |||
843 | try: |
|
843 | try: | |
844 | dp.add(f,priority) |
|
844 | dp.add(f,priority) | |
845 | except AttributeError: |
|
845 | except AttributeError: | |
846 | # it was not commandchain, plain old func - replace |
|
846 | # it was not commandchain, plain old func - replace | |
847 | dp = f |
|
847 | dp = f | |
848 |
|
848 | |||
849 | setattr(self.hooks,name, dp) |
|
849 | setattr(self.hooks,name, dp) | |
850 |
|
850 | |||
851 | #------------------------------------------------------------------------- |
|
851 | #------------------------------------------------------------------------- | |
852 | # Things related to events |
|
852 | # Things related to events | |
853 | #------------------------------------------------------------------------- |
|
853 | #------------------------------------------------------------------------- | |
854 |
|
854 | |||
855 | def init_events(self): |
|
855 | def init_events(self): | |
856 | self.events = EventManager(self, available_events) |
|
856 | self.events = EventManager(self, available_events) | |
857 |
|
857 | |||
858 | self.events.register("pre_execute", self._clear_warning_registry) |
|
858 | self.events.register("pre_execute", self._clear_warning_registry) | |
859 |
|
859 | |||
860 | def register_post_execute(self, func): |
|
860 | def register_post_execute(self, func): | |
861 | """DEPRECATED: Use ip.events.register('post_run_cell', func) |
|
861 | """DEPRECATED: Use ip.events.register('post_run_cell', func) | |
862 |
|
862 | |||
863 | Register a function for calling after code execution. |
|
863 | Register a function for calling after code execution. | |
864 | """ |
|
864 | """ | |
865 | warn("ip.register_post_execute is deprecated, use " |
|
865 | warn("ip.register_post_execute is deprecated, use " | |
866 | "ip.events.register('post_run_cell', func) instead.") |
|
866 | "ip.events.register('post_run_cell', func) instead.") | |
867 | self.events.register('post_run_cell', func) |
|
867 | self.events.register('post_run_cell', func) | |
868 |
|
868 | |||
869 | def _clear_warning_registry(self): |
|
869 | def _clear_warning_registry(self): | |
870 | # clear the warning registry, so that different code blocks with |
|
870 | # clear the warning registry, so that different code blocks with | |
871 | # overlapping line number ranges don't cause spurious suppression of |
|
871 | # overlapping line number ranges don't cause spurious suppression of | |
872 | # warnings (see gh-6611 for details) |
|
872 | # warnings (see gh-6611 for details) | |
873 | if "__warningregistry__" in self.user_global_ns: |
|
873 | if "__warningregistry__" in self.user_global_ns: | |
874 | del self.user_global_ns["__warningregistry__"] |
|
874 | del self.user_global_ns["__warningregistry__"] | |
875 |
|
875 | |||
876 | #------------------------------------------------------------------------- |
|
876 | #------------------------------------------------------------------------- | |
877 | # Things related to the "main" module |
|
877 | # Things related to the "main" module | |
878 | #------------------------------------------------------------------------- |
|
878 | #------------------------------------------------------------------------- | |
879 |
|
879 | |||
880 | def new_main_mod(self, filename, modname): |
|
880 | def new_main_mod(self, filename, modname): | |
881 | """Return a new 'main' module object for user code execution. |
|
881 | """Return a new 'main' module object for user code execution. | |
882 |
|
882 | |||
883 | ``filename`` should be the path of the script which will be run in the |
|
883 | ``filename`` should be the path of the script which will be run in the | |
884 | module. Requests with the same filename will get the same module, with |
|
884 | module. Requests with the same filename will get the same module, with | |
885 | its namespace cleared. |
|
885 | its namespace cleared. | |
886 |
|
886 | |||
887 | ``modname`` should be the module name - normally either '__main__' or |
|
887 | ``modname`` should be the module name - normally either '__main__' or | |
888 | the basename of the file without the extension. |
|
888 | the basename of the file without the extension. | |
889 |
|
889 | |||
890 | When scripts are executed via %run, we must keep a reference to their |
|
890 | When scripts are executed via %run, we must keep a reference to their | |
891 | __main__ module around so that Python doesn't |
|
891 | __main__ module around so that Python doesn't | |
892 | clear it, rendering references to module globals useless. |
|
892 | clear it, rendering references to module globals useless. | |
893 |
|
893 | |||
894 | This method keeps said reference in a private dict, keyed by the |
|
894 | This method keeps said reference in a private dict, keyed by the | |
895 | absolute path of the script. This way, for multiple executions of the |
|
895 | absolute path of the script. This way, for multiple executions of the | |
896 | same script we only keep one copy of the namespace (the last one), |
|
896 | same script we only keep one copy of the namespace (the last one), | |
897 | thus preventing memory leaks from old references while allowing the |
|
897 | thus preventing memory leaks from old references while allowing the | |
898 | objects from the last execution to be accessible. |
|
898 | objects from the last execution to be accessible. | |
899 | """ |
|
899 | """ | |
900 | filename = os.path.abspath(filename) |
|
900 | filename = os.path.abspath(filename) | |
901 | try: |
|
901 | try: | |
902 | main_mod = self._main_mod_cache[filename] |
|
902 | main_mod = self._main_mod_cache[filename] | |
903 | except KeyError: |
|
903 | except KeyError: | |
904 | main_mod = self._main_mod_cache[filename] = types.ModuleType( |
|
904 | main_mod = self._main_mod_cache[filename] = types.ModuleType( | |
905 | py3compat.cast_bytes_py2(modname), |
|
905 | py3compat.cast_bytes_py2(modname), | |
906 | doc="Module created for script run in IPython") |
|
906 | doc="Module created for script run in IPython") | |
907 | else: |
|
907 | else: | |
908 | main_mod.__dict__.clear() |
|
908 | main_mod.__dict__.clear() | |
909 | main_mod.__name__ = modname |
|
909 | main_mod.__name__ = modname | |
910 |
|
910 | |||
911 | main_mod.__file__ = filename |
|
911 | main_mod.__file__ = filename | |
912 | # It seems pydoc (and perhaps others) needs any module instance to |
|
912 | # It seems pydoc (and perhaps others) needs any module instance to | |
913 | # implement a __nonzero__ method |
|
913 | # implement a __nonzero__ method | |
914 | main_mod.__nonzero__ = lambda : True |
|
914 | main_mod.__nonzero__ = lambda : True | |
915 |
|
915 | |||
916 | return main_mod |
|
916 | return main_mod | |
917 |
|
917 | |||
918 | def clear_main_mod_cache(self): |
|
918 | def clear_main_mod_cache(self): | |
919 | """Clear the cache of main modules. |
|
919 | """Clear the cache of main modules. | |
920 |
|
920 | |||
921 | Mainly for use by utilities like %reset. |
|
921 | Mainly for use by utilities like %reset. | |
922 |
|
922 | |||
923 | Examples |
|
923 | Examples | |
924 | -------- |
|
924 | -------- | |
925 |
|
925 | |||
926 | In [15]: import IPython |
|
926 | In [15]: import IPython | |
927 |
|
927 | |||
928 | In [16]: m = _ip.new_main_mod(IPython.__file__, 'IPython') |
|
928 | In [16]: m = _ip.new_main_mod(IPython.__file__, 'IPython') | |
929 |
|
929 | |||
930 | In [17]: len(_ip._main_mod_cache) > 0 |
|
930 | In [17]: len(_ip._main_mod_cache) > 0 | |
931 | Out[17]: True |
|
931 | Out[17]: True | |
932 |
|
932 | |||
933 | In [18]: _ip.clear_main_mod_cache() |
|
933 | In [18]: _ip.clear_main_mod_cache() | |
934 |
|
934 | |||
935 | In [19]: len(_ip._main_mod_cache) == 0 |
|
935 | In [19]: len(_ip._main_mod_cache) == 0 | |
936 | Out[19]: True |
|
936 | Out[19]: True | |
937 | """ |
|
937 | """ | |
938 | self._main_mod_cache.clear() |
|
938 | self._main_mod_cache.clear() | |
939 |
|
939 | |||
940 | #------------------------------------------------------------------------- |
|
940 | #------------------------------------------------------------------------- | |
941 | # Things related to debugging |
|
941 | # Things related to debugging | |
942 | #------------------------------------------------------------------------- |
|
942 | #------------------------------------------------------------------------- | |
943 |
|
943 | |||
944 | def init_pdb(self): |
|
944 | def init_pdb(self): | |
945 | # Set calling of pdb on exceptions |
|
945 | # Set calling of pdb on exceptions | |
946 | # self.call_pdb is a property |
|
946 | # self.call_pdb is a property | |
947 | self.call_pdb = self.pdb |
|
947 | self.call_pdb = self.pdb | |
948 |
|
948 | |||
949 | def _get_call_pdb(self): |
|
949 | def _get_call_pdb(self): | |
950 | return self._call_pdb |
|
950 | return self._call_pdb | |
951 |
|
951 | |||
952 | def _set_call_pdb(self,val): |
|
952 | def _set_call_pdb(self,val): | |
953 |
|
953 | |||
954 | if val not in (0,1,False,True): |
|
954 | if val not in (0,1,False,True): | |
955 | raise ValueError('new call_pdb value must be boolean') |
|
955 | raise ValueError('new call_pdb value must be boolean') | |
956 |
|
956 | |||
957 | # store value in instance |
|
957 | # store value in instance | |
958 | self._call_pdb = val |
|
958 | self._call_pdb = val | |
959 |
|
959 | |||
960 | # notify the actual exception handlers |
|
960 | # notify the actual exception handlers | |
961 | self.InteractiveTB.call_pdb = val |
|
961 | self.InteractiveTB.call_pdb = val | |
962 |
|
962 | |||
963 | call_pdb = property(_get_call_pdb,_set_call_pdb,None, |
|
963 | call_pdb = property(_get_call_pdb,_set_call_pdb,None, | |
964 | 'Control auto-activation of pdb at exceptions') |
|
964 | 'Control auto-activation of pdb at exceptions') | |
965 |
|
965 | |||
966 | def debugger(self,force=False): |
|
966 | def debugger(self,force=False): | |
967 | """Call the pdb debugger. |
|
967 | """Call the pdb debugger. | |
968 |
|
968 | |||
969 | Keywords: |
|
969 | Keywords: | |
970 |
|
970 | |||
971 | - force(False): by default, this routine checks the instance call_pdb |
|
971 | - force(False): by default, this routine checks the instance call_pdb | |
972 | flag and does not actually invoke the debugger if the flag is false. |
|
972 | flag and does not actually invoke the debugger if the flag is false. | |
973 | The 'force' option forces the debugger to activate even if the flag |
|
973 | The 'force' option forces the debugger to activate even if the flag | |
974 | is false. |
|
974 | is false. | |
975 | """ |
|
975 | """ | |
976 |
|
976 | |||
977 | if not (force or self.call_pdb): |
|
977 | if not (force or self.call_pdb): | |
978 | return |
|
978 | return | |
979 |
|
979 | |||
980 | if not hasattr(sys,'last_traceback'): |
|
980 | if not hasattr(sys,'last_traceback'): | |
981 | error('No traceback has been produced, nothing to debug.') |
|
981 | error('No traceback has been produced, nothing to debug.') | |
982 | return |
|
982 | return | |
983 |
|
983 | |||
984 |
|
984 | |||
985 | with self.readline_no_record: |
|
985 | with self.readline_no_record: | |
986 | self.InteractiveTB.debugger(force=True) |
|
986 | self.InteractiveTB.debugger(force=True) | |
987 |
|
987 | |||
988 | #------------------------------------------------------------------------- |
|
988 | #------------------------------------------------------------------------- | |
989 | # Things related to IPython's various namespaces |
|
989 | # Things related to IPython's various namespaces | |
990 | #------------------------------------------------------------------------- |
|
990 | #------------------------------------------------------------------------- | |
991 | default_user_namespaces = True |
|
991 | default_user_namespaces = True | |
992 |
|
992 | |||
993 | def init_create_namespaces(self, user_module=None, user_ns=None): |
|
993 | def init_create_namespaces(self, user_module=None, user_ns=None): | |
994 | # Create the namespace where the user will operate. user_ns is |
|
994 | # Create the namespace where the user will operate. user_ns is | |
995 | # normally the only one used, and it is passed to the exec calls as |
|
995 | # normally the only one used, and it is passed to the exec calls as | |
996 | # the locals argument. But we do carry a user_global_ns namespace |
|
996 | # the locals argument. But we do carry a user_global_ns namespace | |
997 | # given as the exec 'globals' argument, This is useful in embedding |
|
997 | # given as the exec 'globals' argument, This is useful in embedding | |
998 | # situations where the ipython shell opens in a context where the |
|
998 | # situations where the ipython shell opens in a context where the | |
999 | # distinction between locals and globals is meaningful. For |
|
999 | # distinction between locals and globals is meaningful. For | |
1000 | # non-embedded contexts, it is just the same object as the user_ns dict. |
|
1000 | # non-embedded contexts, it is just the same object as the user_ns dict. | |
1001 |
|
1001 | |||
1002 | # FIXME. For some strange reason, __builtins__ is showing up at user |
|
1002 | # FIXME. For some strange reason, __builtins__ is showing up at user | |
1003 | # level as a dict instead of a module. This is a manual fix, but I |
|
1003 | # level as a dict instead of a module. This is a manual fix, but I | |
1004 | # should really track down where the problem is coming from. Alex |
|
1004 | # should really track down where the problem is coming from. Alex | |
1005 | # Schmolck reported this problem first. |
|
1005 | # Schmolck reported this problem first. | |
1006 |
|
1006 | |||
1007 | # A useful post by Alex Martelli on this topic: |
|
1007 | # A useful post by Alex Martelli on this topic: | |
1008 | # Re: inconsistent value from __builtins__ |
|
1008 | # Re: inconsistent value from __builtins__ | |
1009 | # Von: Alex Martelli <aleaxit@yahoo.com> |
|
1009 | # Von: Alex Martelli <aleaxit@yahoo.com> | |
1010 | # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends |
|
1010 | # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends | |
1011 | # Gruppen: comp.lang.python |
|
1011 | # Gruppen: comp.lang.python | |
1012 |
|
1012 | |||
1013 | # Michael Hohn <hohn@hooknose.lbl.gov> wrote: |
|
1013 | # Michael Hohn <hohn@hooknose.lbl.gov> wrote: | |
1014 | # > >>> print type(builtin_check.get_global_binding('__builtins__')) |
|
1014 | # > >>> print type(builtin_check.get_global_binding('__builtins__')) | |
1015 | # > <type 'dict'> |
|
1015 | # > <type 'dict'> | |
1016 | # > >>> print type(__builtins__) |
|
1016 | # > >>> print type(__builtins__) | |
1017 | # > <type 'module'> |
|
1017 | # > <type 'module'> | |
1018 | # > Is this difference in return value intentional? |
|
1018 | # > Is this difference in return value intentional? | |
1019 |
|
1019 | |||
1020 | # Well, it's documented that '__builtins__' can be either a dictionary |
|
1020 | # Well, it's documented that '__builtins__' can be either a dictionary | |
1021 | # or a module, and it's been that way for a long time. Whether it's |
|
1021 | # or a module, and it's been that way for a long time. Whether it's | |
1022 | # intentional (or sensible), I don't know. In any case, the idea is |
|
1022 | # intentional (or sensible), I don't know. In any case, the idea is | |
1023 | # that if you need to access the built-in namespace directly, you |
|
1023 | # that if you need to access the built-in namespace directly, you | |
1024 | # should start with "import __builtin__" (note, no 's') which will |
|
1024 | # should start with "import __builtin__" (note, no 's') which will | |
1025 | # definitely give you a module. Yeah, it's somewhat confusing:-(. |
|
1025 | # definitely give you a module. Yeah, it's somewhat confusing:-(. | |
1026 |
|
1026 | |||
1027 | # These routines return a properly built module and dict as needed by |
|
1027 | # These routines return a properly built module and dict as needed by | |
1028 | # the rest of the code, and can also be used by extension writers to |
|
1028 | # the rest of the code, and can also be used by extension writers to | |
1029 | # generate properly initialized namespaces. |
|
1029 | # generate properly initialized namespaces. | |
1030 | if (user_ns is not None) or (user_module is not None): |
|
1030 | if (user_ns is not None) or (user_module is not None): | |
1031 | self.default_user_namespaces = False |
|
1031 | self.default_user_namespaces = False | |
1032 | self.user_module, self.user_ns = self.prepare_user_module(user_module, user_ns) |
|
1032 | self.user_module, self.user_ns = self.prepare_user_module(user_module, user_ns) | |
1033 |
|
1033 | |||
1034 | # A record of hidden variables we have added to the user namespace, so |
|
1034 | # A record of hidden variables we have added to the user namespace, so | |
1035 | # we can list later only variables defined in actual interactive use. |
|
1035 | # we can list later only variables defined in actual interactive use. | |
1036 | self.user_ns_hidden = {} |
|
1036 | self.user_ns_hidden = {} | |
1037 |
|
1037 | |||
1038 | # Now that FakeModule produces a real module, we've run into a nasty |
|
1038 | # Now that FakeModule produces a real module, we've run into a nasty | |
1039 | # problem: after script execution (via %run), the module where the user |
|
1039 | # problem: after script execution (via %run), the module where the user | |
1040 | # code ran is deleted. Now that this object is a true module (needed |
|
1040 | # code ran is deleted. Now that this object is a true module (needed | |
1041 | # so doctest and other tools work correctly), the Python module |
|
1041 | # so doctest and other tools work correctly), the Python module | |
1042 | # teardown mechanism runs over it, and sets to None every variable |
|
1042 | # teardown mechanism runs over it, and sets to None every variable | |
1043 | # present in that module. Top-level references to objects from the |
|
1043 | # present in that module. Top-level references to objects from the | |
1044 | # script survive, because the user_ns is updated with them. However, |
|
1044 | # script survive, because the user_ns is updated with them. However, | |
1045 | # calling functions defined in the script that use other things from |
|
1045 | # calling functions defined in the script that use other things from | |
1046 | # the script will fail, because the function's closure had references |
|
1046 | # the script will fail, because the function's closure had references | |
1047 | # to the original objects, which are now all None. So we must protect |
|
1047 | # to the original objects, which are now all None. So we must protect | |
1048 | # these modules from deletion by keeping a cache. |
|
1048 | # these modules from deletion by keeping a cache. | |
1049 | # |
|
1049 | # | |
1050 | # To avoid keeping stale modules around (we only need the one from the |
|
1050 | # To avoid keeping stale modules around (we only need the one from the | |
1051 | # last run), we use a dict keyed with the full path to the script, so |
|
1051 | # last run), we use a dict keyed with the full path to the script, so | |
1052 | # only the last version of the module is held in the cache. Note, |
|
1052 | # only the last version of the module is held in the cache. Note, | |
1053 | # however, that we must cache the module *namespace contents* (their |
|
1053 | # however, that we must cache the module *namespace contents* (their | |
1054 | # __dict__). Because if we try to cache the actual modules, old ones |
|
1054 | # __dict__). Because if we try to cache the actual modules, old ones | |
1055 | # (uncached) could be destroyed while still holding references (such as |
|
1055 | # (uncached) could be destroyed while still holding references (such as | |
1056 | # those held by GUI objects that tend to be long-lived)> |
|
1056 | # those held by GUI objects that tend to be long-lived)> | |
1057 | # |
|
1057 | # | |
1058 | # The %reset command will flush this cache. See the cache_main_mod() |
|
1058 | # The %reset command will flush this cache. See the cache_main_mod() | |
1059 | # and clear_main_mod_cache() methods for details on use. |
|
1059 | # and clear_main_mod_cache() methods for details on use. | |
1060 |
|
1060 | |||
1061 | # This is the cache used for 'main' namespaces |
|
1061 | # This is the cache used for 'main' namespaces | |
1062 | self._main_mod_cache = {} |
|
1062 | self._main_mod_cache = {} | |
1063 |
|
1063 | |||
1064 | # A table holding all the namespaces IPython deals with, so that |
|
1064 | # A table holding all the namespaces IPython deals with, so that | |
1065 | # introspection facilities can search easily. |
|
1065 | # introspection facilities can search easily. | |
1066 | self.ns_table = {'user_global':self.user_module.__dict__, |
|
1066 | self.ns_table = {'user_global':self.user_module.__dict__, | |
1067 | 'user_local':self.user_ns, |
|
1067 | 'user_local':self.user_ns, | |
1068 | 'builtin':builtin_mod.__dict__ |
|
1068 | 'builtin':builtin_mod.__dict__ | |
1069 | } |
|
1069 | } | |
1070 |
|
1070 | |||
1071 | @property |
|
1071 | @property | |
1072 | def user_global_ns(self): |
|
1072 | def user_global_ns(self): | |
1073 | return self.user_module.__dict__ |
|
1073 | return self.user_module.__dict__ | |
1074 |
|
1074 | |||
1075 | def prepare_user_module(self, user_module=None, user_ns=None): |
|
1075 | def prepare_user_module(self, user_module=None, user_ns=None): | |
1076 | """Prepare the module and namespace in which user code will be run. |
|
1076 | """Prepare the module and namespace in which user code will be run. | |
1077 |
|
1077 | |||
1078 | When IPython is started normally, both parameters are None: a new module |
|
1078 | When IPython is started normally, both parameters are None: a new module | |
1079 | is created automatically, and its __dict__ used as the namespace. |
|
1079 | is created automatically, and its __dict__ used as the namespace. | |
1080 |
|
1080 | |||
1081 | If only user_module is provided, its __dict__ is used as the namespace. |
|
1081 | If only user_module is provided, its __dict__ is used as the namespace. | |
1082 | If only user_ns is provided, a dummy module is created, and user_ns |
|
1082 | If only user_ns is provided, a dummy module is created, and user_ns | |
1083 | becomes the global namespace. If both are provided (as they may be |
|
1083 | becomes the global namespace. If both are provided (as they may be | |
1084 | when embedding), user_ns is the local namespace, and user_module |
|
1084 | when embedding), user_ns is the local namespace, and user_module | |
1085 | provides the global namespace. |
|
1085 | provides the global namespace. | |
1086 |
|
1086 | |||
1087 | Parameters |
|
1087 | Parameters | |
1088 | ---------- |
|
1088 | ---------- | |
1089 | user_module : module, optional |
|
1089 | user_module : module, optional | |
1090 | The current user module in which IPython is being run. If None, |
|
1090 | The current user module in which IPython is being run. If None, | |
1091 | a clean module will be created. |
|
1091 | a clean module will be created. | |
1092 | user_ns : dict, optional |
|
1092 | user_ns : dict, optional | |
1093 | A namespace in which to run interactive commands. |
|
1093 | A namespace in which to run interactive commands. | |
1094 |
|
1094 | |||
1095 | Returns |
|
1095 | Returns | |
1096 | ------- |
|
1096 | ------- | |
1097 | A tuple of user_module and user_ns, each properly initialised. |
|
1097 | A tuple of user_module and user_ns, each properly initialised. | |
1098 | """ |
|
1098 | """ | |
1099 | if user_module is None and user_ns is not None: |
|
1099 | if user_module is None and user_ns is not None: | |
1100 | user_ns.setdefault("__name__", "__main__") |
|
1100 | user_ns.setdefault("__name__", "__main__") | |
1101 | user_module = DummyMod() |
|
1101 | user_module = DummyMod() | |
1102 | user_module.__dict__ = user_ns |
|
1102 | user_module.__dict__ = user_ns | |
1103 |
|
1103 | |||
1104 | if user_module is None: |
|
1104 | if user_module is None: | |
1105 | user_module = types.ModuleType("__main__", |
|
1105 | user_module = types.ModuleType("__main__", | |
1106 | doc="Automatically created module for IPython interactive environment") |
|
1106 | doc="Automatically created module for IPython interactive environment") | |
1107 |
|
1107 | |||
1108 | # We must ensure that __builtin__ (without the final 's') is always |
|
1108 | # We must ensure that __builtin__ (without the final 's') is always | |
1109 | # available and pointing to the __builtin__ *module*. For more details: |
|
1109 | # available and pointing to the __builtin__ *module*. For more details: | |
1110 | # http://mail.python.org/pipermail/python-dev/2001-April/014068.html |
|
1110 | # http://mail.python.org/pipermail/python-dev/2001-April/014068.html | |
1111 | user_module.__dict__.setdefault('__builtin__', builtin_mod) |
|
1111 | user_module.__dict__.setdefault('__builtin__', builtin_mod) | |
1112 | user_module.__dict__.setdefault('__builtins__', builtin_mod) |
|
1112 | user_module.__dict__.setdefault('__builtins__', builtin_mod) | |
1113 |
|
1113 | |||
1114 | if user_ns is None: |
|
1114 | if user_ns is None: | |
1115 | user_ns = user_module.__dict__ |
|
1115 | user_ns = user_module.__dict__ | |
1116 |
|
1116 | |||
1117 | return user_module, user_ns |
|
1117 | return user_module, user_ns | |
1118 |
|
1118 | |||
1119 | def init_sys_modules(self): |
|
1119 | def init_sys_modules(self): | |
1120 | # We need to insert into sys.modules something that looks like a |
|
1120 | # We need to insert into sys.modules something that looks like a | |
1121 | # module but which accesses the IPython namespace, for shelve and |
|
1121 | # module but which accesses the IPython namespace, for shelve and | |
1122 | # pickle to work interactively. Normally they rely on getting |
|
1122 | # pickle to work interactively. Normally they rely on getting | |
1123 | # everything out of __main__, but for embedding purposes each IPython |
|
1123 | # everything out of __main__, but for embedding purposes each IPython | |
1124 | # instance has its own private namespace, so we can't go shoving |
|
1124 | # instance has its own private namespace, so we can't go shoving | |
1125 | # everything into __main__. |
|
1125 | # everything into __main__. | |
1126 |
|
1126 | |||
1127 | # note, however, that we should only do this for non-embedded |
|
1127 | # note, however, that we should only do this for non-embedded | |
1128 | # ipythons, which really mimic the __main__.__dict__ with their own |
|
1128 | # ipythons, which really mimic the __main__.__dict__ with their own | |
1129 | # namespace. Embedded instances, on the other hand, should not do |
|
1129 | # namespace. Embedded instances, on the other hand, should not do | |
1130 | # this because they need to manage the user local/global namespaces |
|
1130 | # this because they need to manage the user local/global namespaces | |
1131 | # only, but they live within a 'normal' __main__ (meaning, they |
|
1131 | # only, but they live within a 'normal' __main__ (meaning, they | |
1132 | # shouldn't overtake the execution environment of the script they're |
|
1132 | # shouldn't overtake the execution environment of the script they're | |
1133 | # embedded in). |
|
1133 | # embedded in). | |
1134 |
|
1134 | |||
1135 | # This is overridden in the InteractiveShellEmbed subclass to a no-op. |
|
1135 | # This is overridden in the InteractiveShellEmbed subclass to a no-op. | |
1136 | main_name = self.user_module.__name__ |
|
1136 | main_name = self.user_module.__name__ | |
1137 | sys.modules[main_name] = self.user_module |
|
1137 | sys.modules[main_name] = self.user_module | |
1138 |
|
1138 | |||
1139 | def init_user_ns(self): |
|
1139 | def init_user_ns(self): | |
1140 | """Initialize all user-visible namespaces to their minimum defaults. |
|
1140 | """Initialize all user-visible namespaces to their minimum defaults. | |
1141 |
|
1141 | |||
1142 | Certain history lists are also initialized here, as they effectively |
|
1142 | Certain history lists are also initialized here, as they effectively | |
1143 | act as user namespaces. |
|
1143 | act as user namespaces. | |
1144 |
|
1144 | |||
1145 | Notes |
|
1145 | Notes | |
1146 | ----- |
|
1146 | ----- | |
1147 | All data structures here are only filled in, they are NOT reset by this |
|
1147 | All data structures here are only filled in, they are NOT reset by this | |
1148 | method. If they were not empty before, data will simply be added to |
|
1148 | method. If they were not empty before, data will simply be added to | |
1149 | therm. |
|
1149 | therm. | |
1150 | """ |
|
1150 | """ | |
1151 | # This function works in two parts: first we put a few things in |
|
1151 | # This function works in two parts: first we put a few things in | |
1152 | # user_ns, and we sync that contents into user_ns_hidden so that these |
|
1152 | # user_ns, and we sync that contents into user_ns_hidden so that these | |
1153 | # initial variables aren't shown by %who. After the sync, we add the |
|
1153 | # initial variables aren't shown by %who. After the sync, we add the | |
1154 | # rest of what we *do* want the user to see with %who even on a new |
|
1154 | # rest of what we *do* want the user to see with %who even on a new | |
1155 | # session (probably nothing, so they really only see their own stuff) |
|
1155 | # session (probably nothing, so they really only see their own stuff) | |
1156 |
|
1156 | |||
1157 | # The user dict must *always* have a __builtin__ reference to the |
|
1157 | # The user dict must *always* have a __builtin__ reference to the | |
1158 | # Python standard __builtin__ namespace, which must be imported. |
|
1158 | # Python standard __builtin__ namespace, which must be imported. | |
1159 | # This is so that certain operations in prompt evaluation can be |
|
1159 | # This is so that certain operations in prompt evaluation can be | |
1160 | # reliably executed with builtins. Note that we can NOT use |
|
1160 | # reliably executed with builtins. Note that we can NOT use | |
1161 | # __builtins__ (note the 's'), because that can either be a dict or a |
|
1161 | # __builtins__ (note the 's'), because that can either be a dict or a | |
1162 | # module, and can even mutate at runtime, depending on the context |
|
1162 | # module, and can even mutate at runtime, depending on the context | |
1163 | # (Python makes no guarantees on it). In contrast, __builtin__ is |
|
1163 | # (Python makes no guarantees on it). In contrast, __builtin__ is | |
1164 | # always a module object, though it must be explicitly imported. |
|
1164 | # always a module object, though it must be explicitly imported. | |
1165 |
|
1165 | |||
1166 | # For more details: |
|
1166 | # For more details: | |
1167 | # http://mail.python.org/pipermail/python-dev/2001-April/014068.html |
|
1167 | # http://mail.python.org/pipermail/python-dev/2001-April/014068.html | |
1168 | ns = dict() |
|
1168 | ns = dict() | |
1169 |
|
1169 | |||
1170 | # make global variables for user access to the histories |
|
1170 | # make global variables for user access to the histories | |
1171 | ns['_ih'] = self.history_manager.input_hist_parsed |
|
1171 | ns['_ih'] = self.history_manager.input_hist_parsed | |
1172 | ns['_oh'] = self.history_manager.output_hist |
|
1172 | ns['_oh'] = self.history_manager.output_hist | |
1173 | ns['_dh'] = self.history_manager.dir_hist |
|
1173 | ns['_dh'] = self.history_manager.dir_hist | |
1174 |
|
1174 | |||
1175 | ns['_sh'] = shadowns |
|
1175 | ns['_sh'] = shadowns | |
1176 |
|
1176 | |||
1177 | # user aliases to input and output histories. These shouldn't show up |
|
1177 | # user aliases to input and output histories. These shouldn't show up | |
1178 | # in %who, as they can have very large reprs. |
|
1178 | # in %who, as they can have very large reprs. | |
1179 | ns['In'] = self.history_manager.input_hist_parsed |
|
1179 | ns['In'] = self.history_manager.input_hist_parsed | |
1180 | ns['Out'] = self.history_manager.output_hist |
|
1180 | ns['Out'] = self.history_manager.output_hist | |
1181 |
|
1181 | |||
1182 | # Store myself as the public api!!! |
|
1182 | # Store myself as the public api!!! | |
1183 | ns['get_ipython'] = self.get_ipython |
|
1183 | ns['get_ipython'] = self.get_ipython | |
1184 |
|
1184 | |||
1185 | ns['exit'] = self.exiter |
|
1185 | ns['exit'] = self.exiter | |
1186 | ns['quit'] = self.exiter |
|
1186 | ns['quit'] = self.exiter | |
1187 |
|
1187 | |||
1188 | # Sync what we've added so far to user_ns_hidden so these aren't seen |
|
1188 | # Sync what we've added so far to user_ns_hidden so these aren't seen | |
1189 | # by %who |
|
1189 | # by %who | |
1190 | self.user_ns_hidden.update(ns) |
|
1190 | self.user_ns_hidden.update(ns) | |
1191 |
|
1191 | |||
1192 | # Anything put into ns now would show up in %who. Think twice before |
|
1192 | # Anything put into ns now would show up in %who. Think twice before | |
1193 | # putting anything here, as we really want %who to show the user their |
|
1193 | # putting anything here, as we really want %who to show the user their | |
1194 | # stuff, not our variables. |
|
1194 | # stuff, not our variables. | |
1195 |
|
1195 | |||
1196 | # Finally, update the real user's namespace |
|
1196 | # Finally, update the real user's namespace | |
1197 | self.user_ns.update(ns) |
|
1197 | self.user_ns.update(ns) | |
1198 |
|
1198 | |||
1199 | @property |
|
1199 | @property | |
1200 | def all_ns_refs(self): |
|
1200 | def all_ns_refs(self): | |
1201 | """Get a list of references to all the namespace dictionaries in which |
|
1201 | """Get a list of references to all the namespace dictionaries in which | |
1202 | IPython might store a user-created object. |
|
1202 | IPython might store a user-created object. | |
1203 |
|
1203 | |||
1204 | Note that this does not include the displayhook, which also caches |
|
1204 | Note that this does not include the displayhook, which also caches | |
1205 | objects from the output.""" |
|
1205 | objects from the output.""" | |
1206 | return [self.user_ns, self.user_global_ns, self.user_ns_hidden] + \ |
|
1206 | return [self.user_ns, self.user_global_ns, self.user_ns_hidden] + \ | |
1207 | [m.__dict__ for m in self._main_mod_cache.values()] |
|
1207 | [m.__dict__ for m in self._main_mod_cache.values()] | |
1208 |
|
1208 | |||
1209 | def reset(self, new_session=True): |
|
1209 | def reset(self, new_session=True): | |
1210 | """Clear all internal namespaces, and attempt to release references to |
|
1210 | """Clear all internal namespaces, and attempt to release references to | |
1211 | user objects. |
|
1211 | user objects. | |
1212 |
|
1212 | |||
1213 | If new_session is True, a new history session will be opened. |
|
1213 | If new_session is True, a new history session will be opened. | |
1214 | """ |
|
1214 | """ | |
1215 | # Clear histories |
|
1215 | # Clear histories | |
1216 | self.history_manager.reset(new_session) |
|
1216 | self.history_manager.reset(new_session) | |
1217 | # Reset counter used to index all histories |
|
1217 | # Reset counter used to index all histories | |
1218 | if new_session: |
|
1218 | if new_session: | |
1219 | self.execution_count = 1 |
|
1219 | self.execution_count = 1 | |
1220 |
|
1220 | |||
1221 | # Flush cached output items |
|
1221 | # Flush cached output items | |
1222 | if self.displayhook.do_full_cache: |
|
1222 | if self.displayhook.do_full_cache: | |
1223 | self.displayhook.flush() |
|
1223 | self.displayhook.flush() | |
1224 |
|
1224 | |||
1225 | # The main execution namespaces must be cleared very carefully, |
|
1225 | # The main execution namespaces must be cleared very carefully, | |
1226 | # skipping the deletion of the builtin-related keys, because doing so |
|
1226 | # skipping the deletion of the builtin-related keys, because doing so | |
1227 | # would cause errors in many object's __del__ methods. |
|
1227 | # would cause errors in many object's __del__ methods. | |
1228 | if self.user_ns is not self.user_global_ns: |
|
1228 | if self.user_ns is not self.user_global_ns: | |
1229 | self.user_ns.clear() |
|
1229 | self.user_ns.clear() | |
1230 | ns = self.user_global_ns |
|
1230 | ns = self.user_global_ns | |
1231 | drop_keys = set(ns.keys()) |
|
1231 | drop_keys = set(ns.keys()) | |
1232 | drop_keys.discard('__builtin__') |
|
1232 | drop_keys.discard('__builtin__') | |
1233 | drop_keys.discard('__builtins__') |
|
1233 | drop_keys.discard('__builtins__') | |
1234 | drop_keys.discard('__name__') |
|
1234 | drop_keys.discard('__name__') | |
1235 | for k in drop_keys: |
|
1235 | for k in drop_keys: | |
1236 | del ns[k] |
|
1236 | del ns[k] | |
1237 |
|
1237 | |||
1238 | self.user_ns_hidden.clear() |
|
1238 | self.user_ns_hidden.clear() | |
1239 |
|
1239 | |||
1240 | # Restore the user namespaces to minimal usability |
|
1240 | # Restore the user namespaces to minimal usability | |
1241 | self.init_user_ns() |
|
1241 | self.init_user_ns() | |
1242 |
|
1242 | |||
1243 | # Restore the default and user aliases |
|
1243 | # Restore the default and user aliases | |
1244 | self.alias_manager.clear_aliases() |
|
1244 | self.alias_manager.clear_aliases() | |
1245 | self.alias_manager.init_aliases() |
|
1245 | self.alias_manager.init_aliases() | |
1246 |
|
1246 | |||
1247 | # Flush the private list of module references kept for script |
|
1247 | # Flush the private list of module references kept for script | |
1248 | # execution protection |
|
1248 | # execution protection | |
1249 | self.clear_main_mod_cache() |
|
1249 | self.clear_main_mod_cache() | |
1250 |
|
1250 | |||
1251 | def del_var(self, varname, by_name=False): |
|
1251 | def del_var(self, varname, by_name=False): | |
1252 | """Delete a variable from the various namespaces, so that, as |
|
1252 | """Delete a variable from the various namespaces, so that, as | |
1253 | far as possible, we're not keeping any hidden references to it. |
|
1253 | far as possible, we're not keeping any hidden references to it. | |
1254 |
|
1254 | |||
1255 | Parameters |
|
1255 | Parameters | |
1256 | ---------- |
|
1256 | ---------- | |
1257 | varname : str |
|
1257 | varname : str | |
1258 | The name of the variable to delete. |
|
1258 | The name of the variable to delete. | |
1259 | by_name : bool |
|
1259 | by_name : bool | |
1260 | If True, delete variables with the given name in each |
|
1260 | If True, delete variables with the given name in each | |
1261 | namespace. If False (default), find the variable in the user |
|
1261 | namespace. If False (default), find the variable in the user | |
1262 | namespace, and delete references to it. |
|
1262 | namespace, and delete references to it. | |
1263 | """ |
|
1263 | """ | |
1264 | if varname in ('__builtin__', '__builtins__'): |
|
1264 | if varname in ('__builtin__', '__builtins__'): | |
1265 | raise ValueError("Refusing to delete %s" % varname) |
|
1265 | raise ValueError("Refusing to delete %s" % varname) | |
1266 |
|
1266 | |||
1267 | ns_refs = self.all_ns_refs |
|
1267 | ns_refs = self.all_ns_refs | |
1268 |
|
1268 | |||
1269 | if by_name: # Delete by name |
|
1269 | if by_name: # Delete by name | |
1270 | for ns in ns_refs: |
|
1270 | for ns in ns_refs: | |
1271 | try: |
|
1271 | try: | |
1272 | del ns[varname] |
|
1272 | del ns[varname] | |
1273 | except KeyError: |
|
1273 | except KeyError: | |
1274 | pass |
|
1274 | pass | |
1275 | else: # Delete by object |
|
1275 | else: # Delete by object | |
1276 | try: |
|
1276 | try: | |
1277 | obj = self.user_ns[varname] |
|
1277 | obj = self.user_ns[varname] | |
1278 | except KeyError: |
|
1278 | except KeyError: | |
1279 | raise NameError("name '%s' is not defined" % varname) |
|
1279 | raise NameError("name '%s' is not defined" % varname) | |
1280 | # Also check in output history |
|
1280 | # Also check in output history | |
1281 | ns_refs.append(self.history_manager.output_hist) |
|
1281 | ns_refs.append(self.history_manager.output_hist) | |
1282 | for ns in ns_refs: |
|
1282 | for ns in ns_refs: | |
1283 | to_delete = [n for n, o in iteritems(ns) if o is obj] |
|
1283 | to_delete = [n for n, o in iteritems(ns) if o is obj] | |
1284 | for name in to_delete: |
|
1284 | for name in to_delete: | |
1285 | del ns[name] |
|
1285 | del ns[name] | |
1286 |
|
1286 | |||
1287 | # displayhook keeps extra references, but not in a dictionary |
|
1287 | # displayhook keeps extra references, but not in a dictionary | |
1288 | for name in ('_', '__', '___'): |
|
1288 | for name in ('_', '__', '___'): | |
1289 | if getattr(self.displayhook, name) is obj: |
|
1289 | if getattr(self.displayhook, name) is obj: | |
1290 | setattr(self.displayhook, name, None) |
|
1290 | setattr(self.displayhook, name, None) | |
1291 |
|
1291 | |||
1292 | def reset_selective(self, regex=None): |
|
1292 | def reset_selective(self, regex=None): | |
1293 | """Clear selective variables from internal namespaces based on a |
|
1293 | """Clear selective variables from internal namespaces based on a | |
1294 | specified regular expression. |
|
1294 | specified regular expression. | |
1295 |
|
1295 | |||
1296 | Parameters |
|
1296 | Parameters | |
1297 | ---------- |
|
1297 | ---------- | |
1298 | regex : string or compiled pattern, optional |
|
1298 | regex : string or compiled pattern, optional | |
1299 | A regular expression pattern that will be used in searching |
|
1299 | A regular expression pattern that will be used in searching | |
1300 | variable names in the users namespaces. |
|
1300 | variable names in the users namespaces. | |
1301 | """ |
|
1301 | """ | |
1302 | if regex is not None: |
|
1302 | if regex is not None: | |
1303 | try: |
|
1303 | try: | |
1304 | m = re.compile(regex) |
|
1304 | m = re.compile(regex) | |
1305 | except TypeError: |
|
1305 | except TypeError: | |
1306 | raise TypeError('regex must be a string or compiled pattern') |
|
1306 | raise TypeError('regex must be a string or compiled pattern') | |
1307 | # Search for keys in each namespace that match the given regex |
|
1307 | # Search for keys in each namespace that match the given regex | |
1308 | # If a match is found, delete the key/value pair. |
|
1308 | # If a match is found, delete the key/value pair. | |
1309 | for ns in self.all_ns_refs: |
|
1309 | for ns in self.all_ns_refs: | |
1310 | for var in ns: |
|
1310 | for var in ns: | |
1311 | if m.search(var): |
|
1311 | if m.search(var): | |
1312 | del ns[var] |
|
1312 | del ns[var] | |
1313 |
|
1313 | |||
1314 | def push(self, variables, interactive=True): |
|
1314 | def push(self, variables, interactive=True): | |
1315 | """Inject a group of variables into the IPython user namespace. |
|
1315 | """Inject a group of variables into the IPython user namespace. | |
1316 |
|
1316 | |||
1317 | Parameters |
|
1317 | Parameters | |
1318 | ---------- |
|
1318 | ---------- | |
1319 | variables : dict, str or list/tuple of str |
|
1319 | variables : dict, str or list/tuple of str | |
1320 | The variables to inject into the user's namespace. If a dict, a |
|
1320 | The variables to inject into the user's namespace. If a dict, a | |
1321 | simple update is done. If a str, the string is assumed to have |
|
1321 | simple update is done. If a str, the string is assumed to have | |
1322 | variable names separated by spaces. A list/tuple of str can also |
|
1322 | variable names separated by spaces. A list/tuple of str can also | |
1323 | be used to give the variable names. If just the variable names are |
|
1323 | be used to give the variable names. If just the variable names are | |
1324 | give (list/tuple/str) then the variable values looked up in the |
|
1324 | give (list/tuple/str) then the variable values looked up in the | |
1325 | callers frame. |
|
1325 | callers frame. | |
1326 | interactive : bool |
|
1326 | interactive : bool | |
1327 | If True (default), the variables will be listed with the ``who`` |
|
1327 | If True (default), the variables will be listed with the ``who`` | |
1328 | magic. |
|
1328 | magic. | |
1329 | """ |
|
1329 | """ | |
1330 | vdict = None |
|
1330 | vdict = None | |
1331 |
|
1331 | |||
1332 | # We need a dict of name/value pairs to do namespace updates. |
|
1332 | # We need a dict of name/value pairs to do namespace updates. | |
1333 | if isinstance(variables, dict): |
|
1333 | if isinstance(variables, dict): | |
1334 | vdict = variables |
|
1334 | vdict = variables | |
1335 | elif isinstance(variables, string_types+(list, tuple)): |
|
1335 | elif isinstance(variables, string_types+(list, tuple)): | |
1336 | if isinstance(variables, string_types): |
|
1336 | if isinstance(variables, string_types): | |
1337 | vlist = variables.split() |
|
1337 | vlist = variables.split() | |
1338 | else: |
|
1338 | else: | |
1339 | vlist = variables |
|
1339 | vlist = variables | |
1340 | vdict = {} |
|
1340 | vdict = {} | |
1341 | cf = sys._getframe(1) |
|
1341 | cf = sys._getframe(1) | |
1342 | for name in vlist: |
|
1342 | for name in vlist: | |
1343 | try: |
|
1343 | try: | |
1344 | vdict[name] = eval(name, cf.f_globals, cf.f_locals) |
|
1344 | vdict[name] = eval(name, cf.f_globals, cf.f_locals) | |
1345 | except: |
|
1345 | except: | |
1346 | print('Could not get variable %s from %s' % |
|
1346 | print('Could not get variable %s from %s' % | |
1347 | (name,cf.f_code.co_name)) |
|
1347 | (name,cf.f_code.co_name)) | |
1348 | else: |
|
1348 | else: | |
1349 | raise ValueError('variables must be a dict/str/list/tuple') |
|
1349 | raise ValueError('variables must be a dict/str/list/tuple') | |
1350 |
|
1350 | |||
1351 | # Propagate variables to user namespace |
|
1351 | # Propagate variables to user namespace | |
1352 | self.user_ns.update(vdict) |
|
1352 | self.user_ns.update(vdict) | |
1353 |
|
1353 | |||
1354 | # And configure interactive visibility |
|
1354 | # And configure interactive visibility | |
1355 | user_ns_hidden = self.user_ns_hidden |
|
1355 | user_ns_hidden = self.user_ns_hidden | |
1356 | if interactive: |
|
1356 | if interactive: | |
1357 | for name in vdict: |
|
1357 | for name in vdict: | |
1358 | user_ns_hidden.pop(name, None) |
|
1358 | user_ns_hidden.pop(name, None) | |
1359 | else: |
|
1359 | else: | |
1360 | user_ns_hidden.update(vdict) |
|
1360 | user_ns_hidden.update(vdict) | |
1361 |
|
1361 | |||
1362 | def drop_by_id(self, variables): |
|
1362 | def drop_by_id(self, variables): | |
1363 | """Remove a dict of variables from the user namespace, if they are the |
|
1363 | """Remove a dict of variables from the user namespace, if they are the | |
1364 | same as the values in the dictionary. |
|
1364 | same as the values in the dictionary. | |
1365 |
|
1365 | |||
1366 | This is intended for use by extensions: variables that they've added can |
|
1366 | This is intended for use by extensions: variables that they've added can | |
1367 | be taken back out if they are unloaded, without removing any that the |
|
1367 | be taken back out if they are unloaded, without removing any that the | |
1368 | user has overwritten. |
|
1368 | user has overwritten. | |
1369 |
|
1369 | |||
1370 | Parameters |
|
1370 | Parameters | |
1371 | ---------- |
|
1371 | ---------- | |
1372 | variables : dict |
|
1372 | variables : dict | |
1373 | A dictionary mapping object names (as strings) to the objects. |
|
1373 | A dictionary mapping object names (as strings) to the objects. | |
1374 | """ |
|
1374 | """ | |
1375 | for name, obj in iteritems(variables): |
|
1375 | for name, obj in iteritems(variables): | |
1376 | if name in self.user_ns and self.user_ns[name] is obj: |
|
1376 | if name in self.user_ns and self.user_ns[name] is obj: | |
1377 | del self.user_ns[name] |
|
1377 | del self.user_ns[name] | |
1378 | self.user_ns_hidden.pop(name, None) |
|
1378 | self.user_ns_hidden.pop(name, None) | |
1379 |
|
1379 | |||
1380 | #------------------------------------------------------------------------- |
|
1380 | #------------------------------------------------------------------------- | |
1381 | # Things related to object introspection |
|
1381 | # Things related to object introspection | |
1382 | #------------------------------------------------------------------------- |
|
1382 | #------------------------------------------------------------------------- | |
1383 |
|
1383 | |||
1384 | def _ofind(self, oname, namespaces=None): |
|
1384 | def _ofind(self, oname, namespaces=None): | |
1385 | """Find an object in the available namespaces. |
|
1385 | """Find an object in the available namespaces. | |
1386 |
|
1386 | |||
1387 | self._ofind(oname) -> dict with keys: found,obj,ospace,ismagic |
|
1387 | self._ofind(oname) -> dict with keys: found,obj,ospace,ismagic | |
1388 |
|
1388 | |||
1389 | Has special code to detect magic functions. |
|
1389 | Has special code to detect magic functions. | |
1390 | """ |
|
1390 | """ | |
1391 | oname = oname.strip() |
|
1391 | oname = oname.strip() | |
1392 | #print '1- oname: <%r>' % oname # dbg |
|
1392 | #print '1- oname: <%r>' % oname # dbg | |
1393 | if not oname.startswith(ESC_MAGIC) and \ |
|
1393 | if not oname.startswith(ESC_MAGIC) and \ | |
1394 | not oname.startswith(ESC_MAGIC2) and \ |
|
1394 | not oname.startswith(ESC_MAGIC2) and \ | |
1395 | not py3compat.isidentifier(oname, dotted=True): |
|
1395 | not py3compat.isidentifier(oname, dotted=True): | |
1396 | return dict(found=False) |
|
1396 | return dict(found=False) | |
1397 |
|
1397 | |||
1398 | if namespaces is None: |
|
1398 | if namespaces is None: | |
1399 | # Namespaces to search in: |
|
1399 | # Namespaces to search in: | |
1400 | # Put them in a list. The order is important so that we |
|
1400 | # Put them in a list. The order is important so that we | |
1401 | # find things in the same order that Python finds them. |
|
1401 | # find things in the same order that Python finds them. | |
1402 | namespaces = [ ('Interactive', self.user_ns), |
|
1402 | namespaces = [ ('Interactive', self.user_ns), | |
1403 | ('Interactive (global)', self.user_global_ns), |
|
1403 | ('Interactive (global)', self.user_global_ns), | |
1404 | ('Python builtin', builtin_mod.__dict__), |
|
1404 | ('Python builtin', builtin_mod.__dict__), | |
1405 | ] |
|
1405 | ] | |
1406 |
|
1406 | |||
1407 | # initialize results to 'null' |
|
1407 | # initialize results to 'null' | |
1408 | found = False; obj = None; ospace = None; |
|
1408 | found = False; obj = None; ospace = None; | |
1409 | ismagic = False; isalias = False; parent = None |
|
1409 | ismagic = False; isalias = False; parent = None | |
1410 |
|
1410 | |||
1411 | # We need to special-case 'print', which as of python2.6 registers as a |
|
1411 | # We need to special-case 'print', which as of python2.6 registers as a | |
1412 | # function but should only be treated as one if print_function was |
|
1412 | # function but should only be treated as one if print_function was | |
1413 | # loaded with a future import. In this case, just bail. |
|
1413 | # loaded with a future import. In this case, just bail. | |
1414 | if (oname == 'print' and not py3compat.PY3 and not \ |
|
1414 | if (oname == 'print' and not py3compat.PY3 and not \ | |
1415 | (self.compile.compiler_flags & __future__.CO_FUTURE_PRINT_FUNCTION)): |
|
1415 | (self.compile.compiler_flags & __future__.CO_FUTURE_PRINT_FUNCTION)): | |
1416 | return {'found':found, 'obj':obj, 'namespace':ospace, |
|
1416 | return {'found':found, 'obj':obj, 'namespace':ospace, | |
1417 | 'ismagic':ismagic, 'isalias':isalias, 'parent':parent} |
|
1417 | 'ismagic':ismagic, 'isalias':isalias, 'parent':parent} | |
1418 |
|
1418 | |||
1419 | # Look for the given name by splitting it in parts. If the head is |
|
1419 | # Look for the given name by splitting it in parts. If the head is | |
1420 | # found, then we look for all the remaining parts as members, and only |
|
1420 | # found, then we look for all the remaining parts as members, and only | |
1421 | # declare success if we can find them all. |
|
1421 | # declare success if we can find them all. | |
1422 | oname_parts = oname.split('.') |
|
1422 | oname_parts = oname.split('.') | |
1423 | oname_head, oname_rest = oname_parts[0],oname_parts[1:] |
|
1423 | oname_head, oname_rest = oname_parts[0],oname_parts[1:] | |
1424 | for nsname,ns in namespaces: |
|
1424 | for nsname,ns in namespaces: | |
1425 | try: |
|
1425 | try: | |
1426 | obj = ns[oname_head] |
|
1426 | obj = ns[oname_head] | |
1427 | except KeyError: |
|
1427 | except KeyError: | |
1428 | continue |
|
1428 | continue | |
1429 | else: |
|
1429 | else: | |
1430 | #print 'oname_rest:', oname_rest # dbg |
|
1430 | #print 'oname_rest:', oname_rest # dbg | |
1431 | for idx, part in enumerate(oname_rest): |
|
1431 | for idx, part in enumerate(oname_rest): | |
1432 | try: |
|
1432 | try: | |
1433 | parent = obj |
|
1433 | parent = obj | |
1434 | # The last part is looked up in a special way to avoid |
|
1434 | # The last part is looked up in a special way to avoid | |
1435 | # descriptor invocation as it may raise or have side |
|
1435 | # descriptor invocation as it may raise or have side | |
1436 | # effects. |
|
1436 | # effects. | |
1437 | if idx == len(oname_rest) - 1: |
|
1437 | if idx == len(oname_rest) - 1: | |
1438 | obj = self._getattr_property(obj, part) |
|
1438 | obj = self._getattr_property(obj, part) | |
1439 | else: |
|
1439 | else: | |
1440 | obj = getattr(obj, part) |
|
1440 | obj = getattr(obj, part) | |
1441 | except: |
|
1441 | except: | |
1442 | # Blanket except b/c some badly implemented objects |
|
1442 | # Blanket except b/c some badly implemented objects | |
1443 | # allow __getattr__ to raise exceptions other than |
|
1443 | # allow __getattr__ to raise exceptions other than | |
1444 | # AttributeError, which then crashes IPython. |
|
1444 | # AttributeError, which then crashes IPython. | |
1445 | break |
|
1445 | break | |
1446 | else: |
|
1446 | else: | |
1447 | # If we finish the for loop (no break), we got all members |
|
1447 | # If we finish the for loop (no break), we got all members | |
1448 | found = True |
|
1448 | found = True | |
1449 | ospace = nsname |
|
1449 | ospace = nsname | |
1450 | break # namespace loop |
|
1450 | break # namespace loop | |
1451 |
|
1451 | |||
1452 | # Try to see if it's magic |
|
1452 | # Try to see if it's magic | |
1453 | if not found: |
|
1453 | if not found: | |
1454 | obj = None |
|
1454 | obj = None | |
1455 | if oname.startswith(ESC_MAGIC2): |
|
1455 | if oname.startswith(ESC_MAGIC2): | |
1456 | oname = oname.lstrip(ESC_MAGIC2) |
|
1456 | oname = oname.lstrip(ESC_MAGIC2) | |
1457 | obj = self.find_cell_magic(oname) |
|
1457 | obj = self.find_cell_magic(oname) | |
1458 | elif oname.startswith(ESC_MAGIC): |
|
1458 | elif oname.startswith(ESC_MAGIC): | |
1459 | oname = oname.lstrip(ESC_MAGIC) |
|
1459 | oname = oname.lstrip(ESC_MAGIC) | |
1460 | obj = self.find_line_magic(oname) |
|
1460 | obj = self.find_line_magic(oname) | |
1461 | else: |
|
1461 | else: | |
1462 | # search without prefix, so run? will find %run? |
|
1462 | # search without prefix, so run? will find %run? | |
1463 | obj = self.find_line_magic(oname) |
|
1463 | obj = self.find_line_magic(oname) | |
1464 | if obj is None: |
|
1464 | if obj is None: | |
1465 | obj = self.find_cell_magic(oname) |
|
1465 | obj = self.find_cell_magic(oname) | |
1466 | if obj is not None: |
|
1466 | if obj is not None: | |
1467 | found = True |
|
1467 | found = True | |
1468 | ospace = 'IPython internal' |
|
1468 | ospace = 'IPython internal' | |
1469 | ismagic = True |
|
1469 | ismagic = True | |
1470 | isalias = isinstance(obj, Alias) |
|
1470 | isalias = isinstance(obj, Alias) | |
1471 |
|
1471 | |||
1472 | # Last try: special-case some literals like '', [], {}, etc: |
|
1472 | # Last try: special-case some literals like '', [], {}, etc: | |
1473 | if not found and oname_head in ["''",'""','[]','{}','()']: |
|
1473 | if not found and oname_head in ["''",'""','[]','{}','()']: | |
1474 | obj = eval(oname_head) |
|
1474 | obj = eval(oname_head) | |
1475 | found = True |
|
1475 | found = True | |
1476 | ospace = 'Interactive' |
|
1476 | ospace = 'Interactive' | |
1477 |
|
1477 | |||
1478 | return {'found':found, 'obj':obj, 'namespace':ospace, |
|
1478 | return {'found':found, 'obj':obj, 'namespace':ospace, | |
1479 | 'ismagic':ismagic, 'isalias':isalias, 'parent':parent} |
|
1479 | 'ismagic':ismagic, 'isalias':isalias, 'parent':parent} | |
1480 |
|
1480 | |||
1481 | @staticmethod |
|
1481 | @staticmethod | |
1482 | def _getattr_property(obj, attrname): |
|
1482 | def _getattr_property(obj, attrname): | |
1483 | """Property-aware getattr to use in object finding. |
|
1483 | """Property-aware getattr to use in object finding. | |
1484 |
|
1484 | |||
1485 | If attrname represents a property, return it unevaluated (in case it has |
|
1485 | If attrname represents a property, return it unevaluated (in case it has | |
1486 | side effects or raises an error. |
|
1486 | side effects or raises an error. | |
1487 |
|
1487 | |||
1488 | """ |
|
1488 | """ | |
1489 | if not isinstance(obj, type): |
|
1489 | if not isinstance(obj, type): | |
1490 | try: |
|
1490 | try: | |
1491 | # `getattr(type(obj), attrname)` is not guaranteed to return |
|
1491 | # `getattr(type(obj), attrname)` is not guaranteed to return | |
1492 | # `obj`, but does so for property: |
|
1492 | # `obj`, but does so for property: | |
1493 | # |
|
1493 | # | |
1494 | # property.__get__(self, None, cls) -> self |
|
1494 | # property.__get__(self, None, cls) -> self | |
1495 | # |
|
1495 | # | |
1496 | # The universal alternative is to traverse the mro manually |
|
1496 | # The universal alternative is to traverse the mro manually | |
1497 | # searching for attrname in class dicts. |
|
1497 | # searching for attrname in class dicts. | |
1498 | attr = getattr(type(obj), attrname) |
|
1498 | attr = getattr(type(obj), attrname) | |
1499 | except AttributeError: |
|
1499 | except AttributeError: | |
1500 | pass |
|
1500 | pass | |
1501 | else: |
|
1501 | else: | |
1502 | # This relies on the fact that data descriptors (with both |
|
1502 | # This relies on the fact that data descriptors (with both | |
1503 | # __get__ & __set__ magic methods) take precedence over |
|
1503 | # __get__ & __set__ magic methods) take precedence over | |
1504 | # instance-level attributes: |
|
1504 | # instance-level attributes: | |
1505 | # |
|
1505 | # | |
1506 | # class A(object): |
|
1506 | # class A(object): | |
1507 | # @property |
|
1507 | # @property | |
1508 | # def foobar(self): return 123 |
|
1508 | # def foobar(self): return 123 | |
1509 | # a = A() |
|
1509 | # a = A() | |
1510 | # a.__dict__['foobar'] = 345 |
|
1510 | # a.__dict__['foobar'] = 345 | |
1511 | # a.foobar # == 123 |
|
1511 | # a.foobar # == 123 | |
1512 | # |
|
1512 | # | |
1513 | # So, a property may be returned right away. |
|
1513 | # So, a property may be returned right away. | |
1514 | if isinstance(attr, property): |
|
1514 | if isinstance(attr, property): | |
1515 | return attr |
|
1515 | return attr | |
1516 |
|
1516 | |||
1517 | # Nothing helped, fall back. |
|
1517 | # Nothing helped, fall back. | |
1518 | return getattr(obj, attrname) |
|
1518 | return getattr(obj, attrname) | |
1519 |
|
1519 | |||
1520 | def _object_find(self, oname, namespaces=None): |
|
1520 | def _object_find(self, oname, namespaces=None): | |
1521 | """Find an object and return a struct with info about it.""" |
|
1521 | """Find an object and return a struct with info about it.""" | |
1522 | return Struct(self._ofind(oname, namespaces)) |
|
1522 | return Struct(self._ofind(oname, namespaces)) | |
1523 |
|
1523 | |||
1524 | def _inspect(self, meth, oname, namespaces=None, **kw): |
|
1524 | def _inspect(self, meth, oname, namespaces=None, **kw): | |
1525 | """Generic interface to the inspector system. |
|
1525 | """Generic interface to the inspector system. | |
1526 |
|
1526 | |||
1527 | This function is meant to be called by pdef, pdoc & friends.""" |
|
1527 | This function is meant to be called by pdef, pdoc & friends.""" | |
1528 | info = self._object_find(oname, namespaces) |
|
1528 | info = self._object_find(oname, namespaces) | |
1529 | if info.found: |
|
1529 | if info.found: | |
1530 | pmethod = getattr(self.inspector, meth) |
|
1530 | pmethod = getattr(self.inspector, meth) | |
1531 | formatter = format_screen if info.ismagic else None |
|
1531 | formatter = format_screen if info.ismagic else None | |
1532 | if meth == 'pdoc': |
|
1532 | if meth == 'pdoc': | |
1533 | pmethod(info.obj, oname, formatter) |
|
1533 | pmethod(info.obj, oname, formatter) | |
1534 | elif meth == 'pinfo': |
|
1534 | elif meth == 'pinfo': | |
1535 | pmethod(info.obj, oname, formatter, info, **kw) |
|
1535 | pmethod(info.obj, oname, formatter, info, **kw) | |
1536 | else: |
|
1536 | else: | |
1537 | pmethod(info.obj, oname) |
|
1537 | pmethod(info.obj, oname) | |
1538 | else: |
|
1538 | else: | |
1539 | print('Object `%s` not found.' % oname) |
|
1539 | print('Object `%s` not found.' % oname) | |
1540 | return 'not found' # so callers can take other action |
|
1540 | return 'not found' # so callers can take other action | |
1541 |
|
1541 | |||
1542 | def object_inspect(self, oname, detail_level=0): |
|
1542 | def object_inspect(self, oname, detail_level=0): | |
1543 | """Get object info about oname""" |
|
1543 | """Get object info about oname""" | |
1544 | with self.builtin_trap: |
|
1544 | with self.builtin_trap: | |
1545 | info = self._object_find(oname) |
|
1545 | info = self._object_find(oname) | |
1546 | if info.found: |
|
1546 | if info.found: | |
1547 | return self.inspector.info(info.obj, oname, info=info, |
|
1547 | return self.inspector.info(info.obj, oname, info=info, | |
1548 | detail_level=detail_level |
|
1548 | detail_level=detail_level | |
1549 | ) |
|
1549 | ) | |
1550 | else: |
|
1550 | else: | |
1551 | return oinspect.object_info(name=oname, found=False) |
|
1551 | return oinspect.object_info(name=oname, found=False) | |
1552 |
|
1552 | |||
1553 | def object_inspect_text(self, oname, detail_level=0): |
|
1553 | def object_inspect_text(self, oname, detail_level=0): | |
1554 | """Get object info as formatted text""" |
|
1554 | """Get object info as formatted text""" | |
1555 | with self.builtin_trap: |
|
1555 | with self.builtin_trap: | |
1556 | info = self._object_find(oname) |
|
1556 | info = self._object_find(oname) | |
1557 | if info.found: |
|
1557 | if info.found: | |
1558 | return self.inspector._format_info(info.obj, oname, info=info, |
|
1558 | return self.inspector._format_info(info.obj, oname, info=info, | |
1559 | detail_level=detail_level |
|
1559 | detail_level=detail_level | |
1560 | ) |
|
1560 | ) | |
1561 | else: |
|
1561 | else: | |
1562 | raise KeyError(oname) |
|
1562 | raise KeyError(oname) | |
1563 |
|
1563 | |||
1564 | #------------------------------------------------------------------------- |
|
1564 | #------------------------------------------------------------------------- | |
1565 | # Things related to history management |
|
1565 | # Things related to history management | |
1566 | #------------------------------------------------------------------------- |
|
1566 | #------------------------------------------------------------------------- | |
1567 |
|
1567 | |||
1568 | def init_history(self): |
|
1568 | def init_history(self): | |
1569 | """Sets up the command history, and starts regular autosaves.""" |
|
1569 | """Sets up the command history, and starts regular autosaves.""" | |
1570 | self.history_manager = HistoryManager(shell=self, parent=self) |
|
1570 | self.history_manager = HistoryManager(shell=self, parent=self) | |
1571 | self.configurables.append(self.history_manager) |
|
1571 | self.configurables.append(self.history_manager) | |
1572 |
|
1572 | |||
1573 | #------------------------------------------------------------------------- |
|
1573 | #------------------------------------------------------------------------- | |
1574 | # Things related to exception handling and tracebacks (not debugging) |
|
1574 | # Things related to exception handling and tracebacks (not debugging) | |
1575 | #------------------------------------------------------------------------- |
|
1575 | #------------------------------------------------------------------------- | |
1576 |
|
1576 | |||
1577 | debugger_cls = Pdb |
|
1577 | debugger_cls = Pdb | |
1578 |
|
1578 | |||
1579 | def init_traceback_handlers(self, custom_exceptions): |
|
1579 | def init_traceback_handlers(self, custom_exceptions): | |
1580 | # Syntax error handler. |
|
1580 | # Syntax error handler. | |
1581 | self.SyntaxTB = ultratb.SyntaxTB(color_scheme='NoColor') |
|
1581 | self.SyntaxTB = ultratb.SyntaxTB(color_scheme='NoColor') | |
1582 |
|
1582 | |||
1583 | # The interactive one is initialized with an offset, meaning we always |
|
1583 | # The interactive one is initialized with an offset, meaning we always | |
1584 | # want to remove the topmost item in the traceback, which is our own |
|
1584 | # want to remove the topmost item in the traceback, which is our own | |
1585 | # internal code. Valid modes: ['Plain','Context','Verbose'] |
|
1585 | # internal code. Valid modes: ['Plain','Context','Verbose'] | |
1586 | self.InteractiveTB = ultratb.AutoFormattedTB(mode = 'Plain', |
|
1586 | self.InteractiveTB = ultratb.AutoFormattedTB(mode = 'Plain', | |
1587 | color_scheme='NoColor', |
|
1587 | color_scheme='NoColor', | |
1588 | tb_offset = 1, |
|
1588 | tb_offset = 1, | |
1589 | check_cache=check_linecache_ipython, |
|
1589 | check_cache=check_linecache_ipython, | |
1590 | debugger_cls=self.debugger_cls) |
|
1590 | debugger_cls=self.debugger_cls) | |
1591 |
|
1591 | |||
1592 | # The instance will store a pointer to the system-wide exception hook, |
|
1592 | # The instance will store a pointer to the system-wide exception hook, | |
1593 | # so that runtime code (such as magics) can access it. This is because |
|
1593 | # so that runtime code (such as magics) can access it. This is because | |
1594 | # during the read-eval loop, it may get temporarily overwritten. |
|
1594 | # during the read-eval loop, it may get temporarily overwritten. | |
1595 | self.sys_excepthook = sys.excepthook |
|
1595 | self.sys_excepthook = sys.excepthook | |
1596 |
|
1596 | |||
1597 | # and add any custom exception handlers the user may have specified |
|
1597 | # and add any custom exception handlers the user may have specified | |
1598 | self.set_custom_exc(*custom_exceptions) |
|
1598 | self.set_custom_exc(*custom_exceptions) | |
1599 |
|
1599 | |||
1600 | # Set the exception mode |
|
1600 | # Set the exception mode | |
1601 | self.InteractiveTB.set_mode(mode=self.xmode) |
|
1601 | self.InteractiveTB.set_mode(mode=self.xmode) | |
1602 |
|
1602 | |||
1603 | def set_custom_exc(self, exc_tuple, handler): |
|
1603 | def set_custom_exc(self, exc_tuple, handler): | |
1604 | """set_custom_exc(exc_tuple, handler) |
|
1604 | """set_custom_exc(exc_tuple, handler) | |
1605 |
|
1605 | |||
1606 | Set a custom exception handler, which will be called if any of the |
|
1606 | Set a custom exception handler, which will be called if any of the | |
1607 | exceptions in exc_tuple occur in the mainloop (specifically, in the |
|
1607 | exceptions in exc_tuple occur in the mainloop (specifically, in the | |
1608 | run_code() method). |
|
1608 | run_code() method). | |
1609 |
|
1609 | |||
1610 | Parameters |
|
1610 | Parameters | |
1611 | ---------- |
|
1611 | ---------- | |
1612 |
|
1612 | |||
1613 | exc_tuple : tuple of exception classes |
|
1613 | exc_tuple : tuple of exception classes | |
1614 | A *tuple* of exception classes, for which to call the defined |
|
1614 | A *tuple* of exception classes, for which to call the defined | |
1615 | handler. It is very important that you use a tuple, and NOT A |
|
1615 | handler. It is very important that you use a tuple, and NOT A | |
1616 | LIST here, because of the way Python's except statement works. If |
|
1616 | LIST here, because of the way Python's except statement works. If | |
1617 | you only want to trap a single exception, use a singleton tuple:: |
|
1617 | you only want to trap a single exception, use a singleton tuple:: | |
1618 |
|
1618 | |||
1619 | exc_tuple == (MyCustomException,) |
|
1619 | exc_tuple == (MyCustomException,) | |
1620 |
|
1620 | |||
1621 | handler : callable |
|
1621 | handler : callable | |
1622 | handler must have the following signature:: |
|
1622 | handler must have the following signature:: | |
1623 |
|
1623 | |||
1624 | def my_handler(self, etype, value, tb, tb_offset=None): |
|
1624 | def my_handler(self, etype, value, tb, tb_offset=None): | |
1625 | ... |
|
1625 | ... | |
1626 | return structured_traceback |
|
1626 | return structured_traceback | |
1627 |
|
1627 | |||
1628 | Your handler must return a structured traceback (a list of strings), |
|
1628 | Your handler must return a structured traceback (a list of strings), | |
1629 | or None. |
|
1629 | or None. | |
1630 |
|
1630 | |||
1631 | This will be made into an instance method (via types.MethodType) |
|
1631 | This will be made into an instance method (via types.MethodType) | |
1632 | of IPython itself, and it will be called if any of the exceptions |
|
1632 | of IPython itself, and it will be called if any of the exceptions | |
1633 | listed in the exc_tuple are caught. If the handler is None, an |
|
1633 | listed in the exc_tuple are caught. If the handler is None, an | |
1634 | internal basic one is used, which just prints basic info. |
|
1634 | internal basic one is used, which just prints basic info. | |
1635 |
|
1635 | |||
1636 | To protect IPython from crashes, if your handler ever raises an |
|
1636 | To protect IPython from crashes, if your handler ever raises an | |
1637 | exception or returns an invalid result, it will be immediately |
|
1637 | exception or returns an invalid result, it will be immediately | |
1638 | disabled. |
|
1638 | disabled. | |
1639 |
|
1639 | |||
1640 | WARNING: by putting in your own exception handler into IPython's main |
|
1640 | WARNING: by putting in your own exception handler into IPython's main | |
1641 | execution loop, you run a very good chance of nasty crashes. This |
|
1641 | execution loop, you run a very good chance of nasty crashes. This | |
1642 | facility should only be used if you really know what you are doing.""" |
|
1642 | facility should only be used if you really know what you are doing.""" | |
1643 |
|
1643 | |||
1644 | assert type(exc_tuple)==type(()) , \ |
|
1644 | assert type(exc_tuple)==type(()) , \ | |
1645 | "The custom exceptions must be given AS A TUPLE." |
|
1645 | "The custom exceptions must be given AS A TUPLE." | |
1646 |
|
1646 | |||
1647 | def dummy_handler(self, etype, value, tb, tb_offset=None): |
|
1647 | def dummy_handler(self, etype, value, tb, tb_offset=None): | |
1648 | print('*** Simple custom exception handler ***') |
|
1648 | print('*** Simple custom exception handler ***') | |
1649 | print('Exception type :',etype) |
|
1649 | print('Exception type :',etype) | |
1650 | print('Exception value:',value) |
|
1650 | print('Exception value:',value) | |
1651 | print('Traceback :',tb) |
|
1651 | print('Traceback :',tb) | |
1652 | #print 'Source code :','\n'.join(self.buffer) |
|
1652 | #print 'Source code :','\n'.join(self.buffer) | |
1653 |
|
1653 | |||
1654 | def validate_stb(stb): |
|
1654 | def validate_stb(stb): | |
1655 | """validate structured traceback return type |
|
1655 | """validate structured traceback return type | |
1656 |
|
1656 | |||
1657 | return type of CustomTB *should* be a list of strings, but allow |
|
1657 | return type of CustomTB *should* be a list of strings, but allow | |
1658 | single strings or None, which are harmless. |
|
1658 | single strings or None, which are harmless. | |
1659 |
|
1659 | |||
1660 | This function will *always* return a list of strings, |
|
1660 | This function will *always* return a list of strings, | |
1661 | and will raise a TypeError if stb is inappropriate. |
|
1661 | and will raise a TypeError if stb is inappropriate. | |
1662 | """ |
|
1662 | """ | |
1663 | msg = "CustomTB must return list of strings, not %r" % stb |
|
1663 | msg = "CustomTB must return list of strings, not %r" % stb | |
1664 | if stb is None: |
|
1664 | if stb is None: | |
1665 | return [] |
|
1665 | return [] | |
1666 | elif isinstance(stb, string_types): |
|
1666 | elif isinstance(stb, string_types): | |
1667 | return [stb] |
|
1667 | return [stb] | |
1668 | elif not isinstance(stb, list): |
|
1668 | elif not isinstance(stb, list): | |
1669 | raise TypeError(msg) |
|
1669 | raise TypeError(msg) | |
1670 | # it's a list |
|
1670 | # it's a list | |
1671 | for line in stb: |
|
1671 | for line in stb: | |
1672 | # check every element |
|
1672 | # check every element | |
1673 | if not isinstance(line, string_types): |
|
1673 | if not isinstance(line, string_types): | |
1674 | raise TypeError(msg) |
|
1674 | raise TypeError(msg) | |
1675 | return stb |
|
1675 | return stb | |
1676 |
|
1676 | |||
1677 | if handler is None: |
|
1677 | if handler is None: | |
1678 | wrapped = dummy_handler |
|
1678 | wrapped = dummy_handler | |
1679 | else: |
|
1679 | else: | |
1680 | def wrapped(self,etype,value,tb,tb_offset=None): |
|
1680 | def wrapped(self,etype,value,tb,tb_offset=None): | |
1681 | """wrap CustomTB handler, to protect IPython from user code |
|
1681 | """wrap CustomTB handler, to protect IPython from user code | |
1682 |
|
1682 | |||
1683 | This makes it harder (but not impossible) for custom exception |
|
1683 | This makes it harder (but not impossible) for custom exception | |
1684 | handlers to crash IPython. |
|
1684 | handlers to crash IPython. | |
1685 | """ |
|
1685 | """ | |
1686 | try: |
|
1686 | try: | |
1687 | stb = handler(self,etype,value,tb,tb_offset=tb_offset) |
|
1687 | stb = handler(self,etype,value,tb,tb_offset=tb_offset) | |
1688 | return validate_stb(stb) |
|
1688 | return validate_stb(stb) | |
1689 | except: |
|
1689 | except: | |
1690 | # clear custom handler immediately |
|
1690 | # clear custom handler immediately | |
1691 | self.set_custom_exc((), None) |
|
1691 | self.set_custom_exc((), None) | |
1692 | print("Custom TB Handler failed, unregistering", file=sys.stderr) |
|
1692 | print("Custom TB Handler failed, unregistering", file=sys.stderr) | |
1693 | # show the exception in handler first |
|
1693 | # show the exception in handler first | |
1694 | stb = self.InteractiveTB.structured_traceback(*sys.exc_info()) |
|
1694 | stb = self.InteractiveTB.structured_traceback(*sys.exc_info()) | |
1695 | print(self.InteractiveTB.stb2text(stb)) |
|
1695 | print(self.InteractiveTB.stb2text(stb)) | |
1696 | print("The original exception:") |
|
1696 | print("The original exception:") | |
1697 | stb = self.InteractiveTB.structured_traceback( |
|
1697 | stb = self.InteractiveTB.structured_traceback( | |
1698 | (etype,value,tb), tb_offset=tb_offset |
|
1698 | (etype,value,tb), tb_offset=tb_offset | |
1699 | ) |
|
1699 | ) | |
1700 | return stb |
|
1700 | return stb | |
1701 |
|
1701 | |||
1702 | self.CustomTB = types.MethodType(wrapped,self) |
|
1702 | self.CustomTB = types.MethodType(wrapped,self) | |
1703 | self.custom_exceptions = exc_tuple |
|
1703 | self.custom_exceptions = exc_tuple | |
1704 |
|
1704 | |||
1705 | def excepthook(self, etype, value, tb): |
|
1705 | def excepthook(self, etype, value, tb): | |
1706 | """One more defense for GUI apps that call sys.excepthook. |
|
1706 | """One more defense for GUI apps that call sys.excepthook. | |
1707 |
|
1707 | |||
1708 | GUI frameworks like wxPython trap exceptions and call |
|
1708 | GUI frameworks like wxPython trap exceptions and call | |
1709 | sys.excepthook themselves. I guess this is a feature that |
|
1709 | sys.excepthook themselves. I guess this is a feature that | |
1710 | enables them to keep running after exceptions that would |
|
1710 | enables them to keep running after exceptions that would | |
1711 | otherwise kill their mainloop. This is a bother for IPython |
|
1711 | otherwise kill their mainloop. This is a bother for IPython | |
1712 | which excepts to catch all of the program exceptions with a try: |
|
1712 | which excepts to catch all of the program exceptions with a try: | |
1713 | except: statement. |
|
1713 | except: statement. | |
1714 |
|
1714 | |||
1715 | Normally, IPython sets sys.excepthook to a CrashHandler instance, so if |
|
1715 | Normally, IPython sets sys.excepthook to a CrashHandler instance, so if | |
1716 | any app directly invokes sys.excepthook, it will look to the user like |
|
1716 | any app directly invokes sys.excepthook, it will look to the user like | |
1717 | IPython crashed. In order to work around this, we can disable the |
|
1717 | IPython crashed. In order to work around this, we can disable the | |
1718 | CrashHandler and replace it with this excepthook instead, which prints a |
|
1718 | CrashHandler and replace it with this excepthook instead, which prints a | |
1719 | regular traceback using our InteractiveTB. In this fashion, apps which |
|
1719 | regular traceback using our InteractiveTB. In this fashion, apps which | |
1720 | call sys.excepthook will generate a regular-looking exception from |
|
1720 | call sys.excepthook will generate a regular-looking exception from | |
1721 | IPython, and the CrashHandler will only be triggered by real IPython |
|
1721 | IPython, and the CrashHandler will only be triggered by real IPython | |
1722 | crashes. |
|
1722 | crashes. | |
1723 |
|
1723 | |||
1724 | This hook should be used sparingly, only in places which are not likely |
|
1724 | This hook should be used sparingly, only in places which are not likely | |
1725 | to be true IPython errors. |
|
1725 | to be true IPython errors. | |
1726 | """ |
|
1726 | """ | |
1727 | self.showtraceback((etype, value, tb), tb_offset=0) |
|
1727 | self.showtraceback((etype, value, tb), tb_offset=0) | |
1728 |
|
1728 | |||
1729 | def _get_exc_info(self, exc_tuple=None): |
|
1729 | def _get_exc_info(self, exc_tuple=None): | |
1730 | """get exc_info from a given tuple, sys.exc_info() or sys.last_type etc. |
|
1730 | """get exc_info from a given tuple, sys.exc_info() or sys.last_type etc. | |
1731 |
|
1731 | |||
1732 | Ensures sys.last_type,value,traceback hold the exc_info we found, |
|
1732 | Ensures sys.last_type,value,traceback hold the exc_info we found, | |
1733 | from whichever source. |
|
1733 | from whichever source. | |
1734 |
|
1734 | |||
1735 | raises ValueError if none of these contain any information |
|
1735 | raises ValueError if none of these contain any information | |
1736 | """ |
|
1736 | """ | |
1737 | if exc_tuple is None: |
|
1737 | if exc_tuple is None: | |
1738 | etype, value, tb = sys.exc_info() |
|
1738 | etype, value, tb = sys.exc_info() | |
1739 | else: |
|
1739 | else: | |
1740 | etype, value, tb = exc_tuple |
|
1740 | etype, value, tb = exc_tuple | |
1741 |
|
1741 | |||
1742 | if etype is None: |
|
1742 | if etype is None: | |
1743 | if hasattr(sys, 'last_type'): |
|
1743 | if hasattr(sys, 'last_type'): | |
1744 | etype, value, tb = sys.last_type, sys.last_value, \ |
|
1744 | etype, value, tb = sys.last_type, sys.last_value, \ | |
1745 | sys.last_traceback |
|
1745 | sys.last_traceback | |
1746 |
|
1746 | |||
1747 | if etype is None: |
|
1747 | if etype is None: | |
1748 | raise ValueError("No exception to find") |
|
1748 | raise ValueError("No exception to find") | |
1749 |
|
1749 | |||
1750 | # Now store the exception info in sys.last_type etc. |
|
1750 | # Now store the exception info in sys.last_type etc. | |
1751 | # WARNING: these variables are somewhat deprecated and not |
|
1751 | # WARNING: these variables are somewhat deprecated and not | |
1752 | # necessarily safe to use in a threaded environment, but tools |
|
1752 | # necessarily safe to use in a threaded environment, but tools | |
1753 | # like pdb depend on their existence, so let's set them. If we |
|
1753 | # like pdb depend on their existence, so let's set them. If we | |
1754 | # find problems in the field, we'll need to revisit their use. |
|
1754 | # find problems in the field, we'll need to revisit their use. | |
1755 | sys.last_type = etype |
|
1755 | sys.last_type = etype | |
1756 | sys.last_value = value |
|
1756 | sys.last_value = value | |
1757 | sys.last_traceback = tb |
|
1757 | sys.last_traceback = tb | |
1758 |
|
1758 | |||
1759 | return etype, value, tb |
|
1759 | return etype, value, tb | |
1760 |
|
1760 | |||
1761 | def show_usage_error(self, exc): |
|
1761 | def show_usage_error(self, exc): | |
1762 | """Show a short message for UsageErrors |
|
1762 | """Show a short message for UsageErrors | |
1763 |
|
1763 | |||
1764 | These are special exceptions that shouldn't show a traceback. |
|
1764 | These are special exceptions that shouldn't show a traceback. | |
1765 | """ |
|
1765 | """ | |
1766 | print("UsageError: %s" % exc, file=sys.stderr) |
|
1766 | print("UsageError: %s" % exc, file=sys.stderr) | |
1767 |
|
1767 | |||
1768 | def get_exception_only(self, exc_tuple=None): |
|
1768 | def get_exception_only(self, exc_tuple=None): | |
1769 | """ |
|
1769 | """ | |
1770 | Return as a string (ending with a newline) the exception that |
|
1770 | Return as a string (ending with a newline) the exception that | |
1771 | just occurred, without any traceback. |
|
1771 | just occurred, without any traceback. | |
1772 | """ |
|
1772 | """ | |
1773 | etype, value, tb = self._get_exc_info(exc_tuple) |
|
1773 | etype, value, tb = self._get_exc_info(exc_tuple) | |
1774 | msg = traceback.format_exception_only(etype, value) |
|
1774 | msg = traceback.format_exception_only(etype, value) | |
1775 | return ''.join(msg) |
|
1775 | return ''.join(msg) | |
1776 |
|
1776 | |||
1777 | def showtraceback(self, exc_tuple=None, filename=None, tb_offset=None, |
|
1777 | def showtraceback(self, exc_tuple=None, filename=None, tb_offset=None, | |
1778 | exception_only=False): |
|
1778 | exception_only=False): | |
1779 | """Display the exception that just occurred. |
|
1779 | """Display the exception that just occurred. | |
1780 |
|
1780 | |||
1781 | If nothing is known about the exception, this is the method which |
|
1781 | If nothing is known about the exception, this is the method which | |
1782 | should be used throughout the code for presenting user tracebacks, |
|
1782 | should be used throughout the code for presenting user tracebacks, | |
1783 | rather than directly invoking the InteractiveTB object. |
|
1783 | rather than directly invoking the InteractiveTB object. | |
1784 |
|
1784 | |||
1785 | A specific showsyntaxerror() also exists, but this method can take |
|
1785 | A specific showsyntaxerror() also exists, but this method can take | |
1786 | care of calling it if needed, so unless you are explicitly catching a |
|
1786 | care of calling it if needed, so unless you are explicitly catching a | |
1787 | SyntaxError exception, don't try to analyze the stack manually and |
|
1787 | SyntaxError exception, don't try to analyze the stack manually and | |
1788 | simply call this method.""" |
|
1788 | simply call this method.""" | |
1789 |
|
1789 | |||
1790 | try: |
|
1790 | try: | |
1791 | try: |
|
1791 | try: | |
1792 | etype, value, tb = self._get_exc_info(exc_tuple) |
|
1792 | etype, value, tb = self._get_exc_info(exc_tuple) | |
1793 | except ValueError: |
|
1793 | except ValueError: | |
1794 | print('No traceback available to show.', file=sys.stderr) |
|
1794 | print('No traceback available to show.', file=sys.stderr) | |
1795 | return |
|
1795 | return | |
1796 |
|
1796 | |||
1797 | if issubclass(etype, SyntaxError): |
|
1797 | if issubclass(etype, SyntaxError): | |
1798 | # Though this won't be called by syntax errors in the input |
|
1798 | # Though this won't be called by syntax errors in the input | |
1799 | # line, there may be SyntaxError cases with imported code. |
|
1799 | # line, there may be SyntaxError cases with imported code. | |
1800 | self.showsyntaxerror(filename) |
|
1800 | self.showsyntaxerror(filename) | |
1801 | elif etype is UsageError: |
|
1801 | elif etype is UsageError: | |
1802 | self.show_usage_error(value) |
|
1802 | self.show_usage_error(value) | |
1803 | else: |
|
1803 | else: | |
1804 | if exception_only: |
|
1804 | if exception_only: | |
1805 | stb = ['An exception has occurred, use %tb to see ' |
|
1805 | stb = ['An exception has occurred, use %tb to see ' | |
1806 | 'the full traceback.\n'] |
|
1806 | 'the full traceback.\n'] | |
1807 | stb.extend(self.InteractiveTB.get_exception_only(etype, |
|
1807 | stb.extend(self.InteractiveTB.get_exception_only(etype, | |
1808 | value)) |
|
1808 | value)) | |
1809 | else: |
|
1809 | else: | |
1810 | try: |
|
1810 | try: | |
1811 | # Exception classes can customise their traceback - we |
|
1811 | # Exception classes can customise their traceback - we | |
1812 | # use this in IPython.parallel for exceptions occurring |
|
1812 | # use this in IPython.parallel for exceptions occurring | |
1813 | # in the engines. This should return a list of strings. |
|
1813 | # in the engines. This should return a list of strings. | |
1814 | stb = value._render_traceback_() |
|
1814 | stb = value._render_traceback_() | |
1815 | except Exception: |
|
1815 | except Exception: | |
1816 | stb = self.InteractiveTB.structured_traceback(etype, |
|
1816 | stb = self.InteractiveTB.structured_traceback(etype, | |
1817 | value, tb, tb_offset=tb_offset) |
|
1817 | value, tb, tb_offset=tb_offset) | |
1818 |
|
1818 | |||
1819 | self._showtraceback(etype, value, stb) |
|
1819 | self._showtraceback(etype, value, stb) | |
1820 | if self.call_pdb: |
|
1820 | if self.call_pdb: | |
1821 | # drop into debugger |
|
1821 | # drop into debugger | |
1822 | self.debugger(force=True) |
|
1822 | self.debugger(force=True) | |
1823 | return |
|
1823 | return | |
1824 |
|
1824 | |||
1825 | # Actually show the traceback |
|
1825 | # Actually show the traceback | |
1826 | self._showtraceback(etype, value, stb) |
|
1826 | self._showtraceback(etype, value, stb) | |
1827 |
|
1827 | |||
1828 | except KeyboardInterrupt: |
|
1828 | except KeyboardInterrupt: | |
1829 | print('\n' + self.get_exception_only(), file=sys.stderr) |
|
1829 | print('\n' + self.get_exception_only(), file=sys.stderr) | |
1830 |
|
1830 | |||
1831 | def _showtraceback(self, etype, evalue, stb): |
|
1831 | def _showtraceback(self, etype, evalue, stb): | |
1832 | """Actually show a traceback. |
|
1832 | """Actually show a traceback. | |
1833 |
|
1833 | |||
1834 | Subclasses may override this method to put the traceback on a different |
|
1834 | Subclasses may override this method to put the traceback on a different | |
1835 | place, like a side channel. |
|
1835 | place, like a side channel. | |
1836 | """ |
|
1836 | """ | |
1837 | print(self.InteractiveTB.stb2text(stb)) |
|
1837 | print(self.InteractiveTB.stb2text(stb)) | |
1838 |
|
1838 | |||
1839 | def showsyntaxerror(self, filename=None): |
|
1839 | def showsyntaxerror(self, filename=None): | |
1840 | """Display the syntax error that just occurred. |
|
1840 | """Display the syntax error that just occurred. | |
1841 |
|
1841 | |||
1842 | This doesn't display a stack trace because there isn't one. |
|
1842 | This doesn't display a stack trace because there isn't one. | |
1843 |
|
1843 | |||
1844 | If a filename is given, it is stuffed in the exception instead |
|
1844 | If a filename is given, it is stuffed in the exception instead | |
1845 | of what was there before (because Python's parser always uses |
|
1845 | of what was there before (because Python's parser always uses | |
1846 | "<string>" when reading from a string). |
|
1846 | "<string>" when reading from a string). | |
1847 | """ |
|
1847 | """ | |
1848 | etype, value, last_traceback = self._get_exc_info() |
|
1848 | etype, value, last_traceback = self._get_exc_info() | |
1849 |
|
1849 | |||
1850 | if filename and issubclass(etype, SyntaxError): |
|
1850 | if filename and issubclass(etype, SyntaxError): | |
1851 | try: |
|
1851 | try: | |
1852 | value.filename = filename |
|
1852 | value.filename = filename | |
1853 | except: |
|
1853 | except: | |
1854 | # Not the format we expect; leave it alone |
|
1854 | # Not the format we expect; leave it alone | |
1855 | pass |
|
1855 | pass | |
1856 |
|
1856 | |||
1857 | stb = self.SyntaxTB.structured_traceback(etype, value, []) |
|
1857 | stb = self.SyntaxTB.structured_traceback(etype, value, []) | |
1858 | self._showtraceback(etype, value, stb) |
|
1858 | self._showtraceback(etype, value, stb) | |
1859 |
|
1859 | |||
1860 | # This is overridden in TerminalInteractiveShell to show a message about |
|
1860 | # This is overridden in TerminalInteractiveShell to show a message about | |
1861 | # the %paste magic. |
|
1861 | # the %paste magic. | |
1862 | def showindentationerror(self): |
|
1862 | def showindentationerror(self): | |
1863 | """Called by run_cell when there's an IndentationError in code entered |
|
1863 | """Called by run_cell when there's an IndentationError in code entered | |
1864 | at the prompt. |
|
1864 | at the prompt. | |
1865 |
|
1865 | |||
1866 | This is overridden in TerminalInteractiveShell to show a message about |
|
1866 | This is overridden in TerminalInteractiveShell to show a message about | |
1867 | the %paste magic.""" |
|
1867 | the %paste magic.""" | |
1868 | self.showsyntaxerror() |
|
1868 | self.showsyntaxerror() | |
1869 |
|
1869 | |||
1870 | #------------------------------------------------------------------------- |
|
1870 | #------------------------------------------------------------------------- | |
1871 | # Things related to readline |
|
1871 | # Things related to readline | |
1872 | #------------------------------------------------------------------------- |
|
1872 | #------------------------------------------------------------------------- | |
1873 |
|
1873 | |||
1874 | def init_readline(self): |
|
1874 | def init_readline(self): | |
1875 | """Moved to terminal subclass, here only to simplify the init logic.""" |
|
1875 | """Moved to terminal subclass, here only to simplify the init logic.""" | |
1876 | self.readline = None |
|
1876 | self.readline = None | |
1877 | # Set a number of methods that depend on readline to be no-op |
|
1877 | # Set a number of methods that depend on readline to be no-op | |
1878 | self.readline_no_record = NoOpContext() |
|
1878 | self.readline_no_record = NoOpContext() | |
1879 | self.set_readline_completer = no_op |
|
1879 | self.set_readline_completer = no_op | |
1880 | self.set_custom_completer = no_op |
|
1880 | self.set_custom_completer = no_op | |
1881 |
|
1881 | |||
1882 | @skip_doctest |
|
1882 | @skip_doctest | |
1883 | def set_next_input(self, s, replace=False): |
|
1883 | def set_next_input(self, s, replace=False): | |
1884 | """ Sets the 'default' input string for the next command line. |
|
1884 | """ Sets the 'default' input string for the next command line. | |
1885 |
|
1885 | |||
1886 | Example:: |
|
1886 | Example:: | |
1887 |
|
1887 | |||
1888 | In [1]: _ip.set_next_input("Hello Word") |
|
1888 | In [1]: _ip.set_next_input("Hello Word") | |
1889 | In [2]: Hello Word_ # cursor is here |
|
1889 | In [2]: Hello Word_ # cursor is here | |
1890 | """ |
|
1890 | """ | |
1891 | self.rl_next_input = py3compat.cast_bytes_py2(s) |
|
1891 | self.rl_next_input = py3compat.cast_bytes_py2(s) | |
1892 |
|
1892 | |||
1893 | def _indent_current_str(self): |
|
1893 | def _indent_current_str(self): | |
1894 | """return the current level of indentation as a string""" |
|
1894 | """return the current level of indentation as a string""" | |
1895 | return self.input_splitter.indent_spaces * ' ' |
|
1895 | return self.input_splitter.indent_spaces * ' ' | |
1896 |
|
1896 | |||
1897 | #------------------------------------------------------------------------- |
|
1897 | #------------------------------------------------------------------------- | |
1898 | # Things related to text completion |
|
1898 | # Things related to text completion | |
1899 | #------------------------------------------------------------------------- |
|
1899 | #------------------------------------------------------------------------- | |
1900 |
|
1900 | |||
1901 | def init_completer(self): |
|
1901 | def init_completer(self): | |
1902 | """Initialize the completion machinery. |
|
1902 | """Initialize the completion machinery. | |
1903 |
|
1903 | |||
1904 | This creates completion machinery that can be used by client code, |
|
1904 | This creates completion machinery that can be used by client code, | |
1905 | either interactively in-process (typically triggered by the readline |
|
1905 | either interactively in-process (typically triggered by the readline | |
1906 | library), programmatically (such as in test suites) or out-of-process |
|
1906 | library), programmatically (such as in test suites) or out-of-process | |
1907 | (typically over the network by remote frontends). |
|
1907 | (typically over the network by remote frontends). | |
1908 | """ |
|
1908 | """ | |
1909 | from IPython.core.completer import IPCompleter |
|
1909 | from IPython.core.completer import IPCompleter | |
1910 | from IPython.core.completerlib import (module_completer, |
|
1910 | from IPython.core.completerlib import (module_completer, | |
1911 | magic_run_completer, cd_completer, reset_completer) |
|
1911 | magic_run_completer, cd_completer, reset_completer) | |
1912 |
|
1912 | |||
1913 | self.Completer = IPCompleter(shell=self, |
|
1913 | self.Completer = IPCompleter(shell=self, | |
1914 | namespace=self.user_ns, |
|
1914 | namespace=self.user_ns, | |
1915 | global_namespace=self.user_global_ns, |
|
1915 | global_namespace=self.user_global_ns, | |
1916 | use_readline=self.has_readline, |
|
1916 | use_readline=self.has_readline, | |
1917 | parent=self, |
|
1917 | parent=self, | |
1918 | ) |
|
1918 | ) | |
1919 | self.configurables.append(self.Completer) |
|
1919 | self.configurables.append(self.Completer) | |
1920 |
|
1920 | |||
1921 | # Add custom completers to the basic ones built into IPCompleter |
|
1921 | # Add custom completers to the basic ones built into IPCompleter | |
1922 | sdisp = self.strdispatchers.get('complete_command', StrDispatch()) |
|
1922 | sdisp = self.strdispatchers.get('complete_command', StrDispatch()) | |
1923 | self.strdispatchers['complete_command'] = sdisp |
|
1923 | self.strdispatchers['complete_command'] = sdisp | |
1924 | self.Completer.custom_completers = sdisp |
|
1924 | self.Completer.custom_completers = sdisp | |
1925 |
|
1925 | |||
1926 | self.set_hook('complete_command', module_completer, str_key = 'import') |
|
1926 | self.set_hook('complete_command', module_completer, str_key = 'import') | |
1927 | self.set_hook('complete_command', module_completer, str_key = 'from') |
|
1927 | self.set_hook('complete_command', module_completer, str_key = 'from') | |
1928 | self.set_hook('complete_command', module_completer, str_key = '%aimport') |
|
1928 | self.set_hook('complete_command', module_completer, str_key = '%aimport') | |
1929 | self.set_hook('complete_command', magic_run_completer, str_key = '%run') |
|
1929 | self.set_hook('complete_command', magic_run_completer, str_key = '%run') | |
1930 | self.set_hook('complete_command', cd_completer, str_key = '%cd') |
|
1930 | self.set_hook('complete_command', cd_completer, str_key = '%cd') | |
1931 | self.set_hook('complete_command', reset_completer, str_key = '%reset') |
|
1931 | self.set_hook('complete_command', reset_completer, str_key = '%reset') | |
1932 |
|
1932 | |||
1933 |
|
1933 | |||
1934 | @skip_doctest_py2 |
|
1934 | @skip_doctest_py2 | |
1935 | def complete(self, text, line=None, cursor_pos=None): |
|
1935 | def complete(self, text, line=None, cursor_pos=None): | |
1936 | """Return the completed text and a list of completions. |
|
1936 | """Return the completed text and a list of completions. | |
1937 |
|
1937 | |||
1938 | Parameters |
|
1938 | Parameters | |
1939 | ---------- |
|
1939 | ---------- | |
1940 |
|
1940 | |||
1941 | text : string |
|
1941 | text : string | |
1942 | A string of text to be completed on. It can be given as empty and |
|
1942 | A string of text to be completed on. It can be given as empty and | |
1943 | instead a line/position pair are given. In this case, the |
|
1943 | instead a line/position pair are given. In this case, the | |
1944 | completer itself will split the line like readline does. |
|
1944 | completer itself will split the line like readline does. | |
1945 |
|
1945 | |||
1946 | line : string, optional |
|
1946 | line : string, optional | |
1947 | The complete line that text is part of. |
|
1947 | The complete line that text is part of. | |
1948 |
|
1948 | |||
1949 | cursor_pos : int, optional |
|
1949 | cursor_pos : int, optional | |
1950 | The position of the cursor on the input line. |
|
1950 | The position of the cursor on the input line. | |
1951 |
|
1951 | |||
1952 | Returns |
|
1952 | Returns | |
1953 | ------- |
|
1953 | ------- | |
1954 | text : string |
|
1954 | text : string | |
1955 | The actual text that was completed. |
|
1955 | The actual text that was completed. | |
1956 |
|
1956 | |||
1957 | matches : list |
|
1957 | matches : list | |
1958 | A sorted list with all possible completions. |
|
1958 | A sorted list with all possible completions. | |
1959 |
|
1959 | |||
1960 | The optional arguments allow the completion to take more context into |
|
1960 | The optional arguments allow the completion to take more context into | |
1961 | account, and are part of the low-level completion API. |
|
1961 | account, and are part of the low-level completion API. | |
1962 |
|
1962 | |||
1963 | This is a wrapper around the completion mechanism, similar to what |
|
1963 | This is a wrapper around the completion mechanism, similar to what | |
1964 | readline does at the command line when the TAB key is hit. By |
|
1964 | readline does at the command line when the TAB key is hit. By | |
1965 | exposing it as a method, it can be used by other non-readline |
|
1965 | exposing it as a method, it can be used by other non-readline | |
1966 | environments (such as GUIs) for text completion. |
|
1966 | environments (such as GUIs) for text completion. | |
1967 |
|
1967 | |||
1968 | Simple usage example: |
|
1968 | Simple usage example: | |
1969 |
|
1969 | |||
1970 | In [1]: x = 'hello' |
|
1970 | In [1]: x = 'hello' | |
1971 |
|
1971 | |||
1972 | In [2]: _ip.complete('x.l') |
|
1972 | In [2]: _ip.complete('x.l') | |
1973 | Out[2]: ('x.l', ['x.ljust', 'x.lower', 'x.lstrip']) |
|
1973 | Out[2]: ('x.l', ['x.ljust', 'x.lower', 'x.lstrip']) | |
1974 | """ |
|
1974 | """ | |
1975 |
|
1975 | |||
1976 | # Inject names into __builtin__ so we can complete on the added names. |
|
1976 | # Inject names into __builtin__ so we can complete on the added names. | |
1977 | with self.builtin_trap: |
|
1977 | with self.builtin_trap: | |
1978 | return self.Completer.complete(text, line, cursor_pos) |
|
1978 | return self.Completer.complete(text, line, cursor_pos) | |
1979 |
|
1979 | |||
1980 | def set_custom_completer(self, completer, pos=0): |
|
1980 | def set_custom_completer(self, completer, pos=0): | |
1981 | """Adds a new custom completer function. |
|
1981 | """Adds a new custom completer function. | |
1982 |
|
1982 | |||
1983 | The position argument (defaults to 0) is the index in the completers |
|
1983 | The position argument (defaults to 0) is the index in the completers | |
1984 | list where you want the completer to be inserted.""" |
|
1984 | list where you want the completer to be inserted.""" | |
1985 |
|
1985 | |||
1986 | newcomp = types.MethodType(completer,self.Completer) |
|
1986 | newcomp = types.MethodType(completer,self.Completer) | |
1987 | self.Completer.matchers.insert(pos,newcomp) |
|
1987 | self.Completer.matchers.insert(pos,newcomp) | |
1988 |
|
1988 | |||
1989 | def set_completer_frame(self, frame=None): |
|
1989 | def set_completer_frame(self, frame=None): | |
1990 | """Set the frame of the completer.""" |
|
1990 | """Set the frame of the completer.""" | |
1991 | if frame: |
|
1991 | if frame: | |
1992 | self.Completer.namespace = frame.f_locals |
|
1992 | self.Completer.namespace = frame.f_locals | |
1993 | self.Completer.global_namespace = frame.f_globals |
|
1993 | self.Completer.global_namespace = frame.f_globals | |
1994 | else: |
|
1994 | else: | |
1995 | self.Completer.namespace = self.user_ns |
|
1995 | self.Completer.namespace = self.user_ns | |
1996 | self.Completer.global_namespace = self.user_global_ns |
|
1996 | self.Completer.global_namespace = self.user_global_ns | |
1997 |
|
1997 | |||
1998 | #------------------------------------------------------------------------- |
|
1998 | #------------------------------------------------------------------------- | |
1999 | # Things related to magics |
|
1999 | # Things related to magics | |
2000 | #------------------------------------------------------------------------- |
|
2000 | #------------------------------------------------------------------------- | |
2001 |
|
2001 | |||
2002 | def init_magics(self): |
|
2002 | def init_magics(self): | |
2003 | from IPython.core import magics as m |
|
2003 | from IPython.core import magics as m | |
2004 | self.magics_manager = magic.MagicsManager(shell=self, |
|
2004 | self.magics_manager = magic.MagicsManager(shell=self, | |
2005 | parent=self, |
|
2005 | parent=self, | |
2006 | user_magics=m.UserMagics(self)) |
|
2006 | user_magics=m.UserMagics(self)) | |
2007 | self.configurables.append(self.magics_manager) |
|
2007 | self.configurables.append(self.magics_manager) | |
2008 |
|
2008 | |||
2009 | # Expose as public API from the magics manager |
|
2009 | # Expose as public API from the magics manager | |
2010 | self.register_magics = self.magics_manager.register |
|
2010 | self.register_magics = self.magics_manager.register | |
2011 |
|
2011 | |||
2012 | self.register_magics(m.AutoMagics, m.BasicMagics, m.CodeMagics, |
|
2012 | self.register_magics(m.AutoMagics, m.BasicMagics, m.CodeMagics, | |
2013 | m.ConfigMagics, m.DisplayMagics, m.ExecutionMagics, |
|
2013 | m.ConfigMagics, m.DisplayMagics, m.ExecutionMagics, | |
2014 | m.ExtensionMagics, m.HistoryMagics, m.LoggingMagics, |
|
2014 | m.ExtensionMagics, m.HistoryMagics, m.LoggingMagics, | |
2015 | m.NamespaceMagics, m.OSMagics, m.PylabMagics, m.ScriptMagics, |
|
2015 | m.NamespaceMagics, m.OSMagics, m.PylabMagics, m.ScriptMagics, | |
2016 | ) |
|
2016 | ) | |
2017 |
|
2017 | |||
2018 | # Register Magic Aliases |
|
2018 | # Register Magic Aliases | |
2019 | mman = self.magics_manager |
|
2019 | mman = self.magics_manager | |
2020 | # FIXME: magic aliases should be defined by the Magics classes |
|
2020 | # FIXME: magic aliases should be defined by the Magics classes | |
2021 | # or in MagicsManager, not here |
|
2021 | # or in MagicsManager, not here | |
2022 | mman.register_alias('ed', 'edit') |
|
2022 | mman.register_alias('ed', 'edit') | |
2023 | mman.register_alias('hist', 'history') |
|
2023 | mman.register_alias('hist', 'history') | |
2024 | mman.register_alias('rep', 'recall') |
|
2024 | mman.register_alias('rep', 'recall') | |
2025 | mman.register_alias('SVG', 'svg', 'cell') |
|
2025 | mman.register_alias('SVG', 'svg', 'cell') | |
2026 | mman.register_alias('HTML', 'html', 'cell') |
|
2026 | mman.register_alias('HTML', 'html', 'cell') | |
2027 | mman.register_alias('file', 'writefile', 'cell') |
|
2027 | mman.register_alias('file', 'writefile', 'cell') | |
2028 |
|
2028 | |||
2029 | # FIXME: Move the color initialization to the DisplayHook, which |
|
2029 | # FIXME: Move the color initialization to the DisplayHook, which | |
2030 | # should be split into a prompt manager and displayhook. We probably |
|
2030 | # should be split into a prompt manager and displayhook. We probably | |
2031 | # even need a centralize colors management object. |
|
2031 | # even need a centralize colors management object. | |
2032 | self.magic('colors %s' % self.colors) |
|
2032 | self.magic('colors %s' % self.colors) | |
2033 |
|
2033 | |||
2034 | # Defined here so that it's included in the documentation |
|
2034 | # Defined here so that it's included in the documentation | |
2035 | @functools.wraps(magic.MagicsManager.register_function) |
|
2035 | @functools.wraps(magic.MagicsManager.register_function) | |
2036 | def register_magic_function(self, func, magic_kind='line', magic_name=None): |
|
2036 | def register_magic_function(self, func, magic_kind='line', magic_name=None): | |
2037 | self.magics_manager.register_function(func, |
|
2037 | self.magics_manager.register_function(func, | |
2038 | magic_kind=magic_kind, magic_name=magic_name) |
|
2038 | magic_kind=magic_kind, magic_name=magic_name) | |
2039 |
|
2039 | |||
2040 | def run_line_magic(self, magic_name, line): |
|
2040 | def run_line_magic(self, magic_name, line): | |
2041 | """Execute the given line magic. |
|
2041 | """Execute the given line magic. | |
2042 |
|
2042 | |||
2043 | Parameters |
|
2043 | Parameters | |
2044 | ---------- |
|
2044 | ---------- | |
2045 | magic_name : str |
|
2045 | magic_name : str | |
2046 | Name of the desired magic function, without '%' prefix. |
|
2046 | Name of the desired magic function, without '%' prefix. | |
2047 |
|
2047 | |||
2048 | line : str |
|
2048 | line : str | |
2049 | The rest of the input line as a single string. |
|
2049 | The rest of the input line as a single string. | |
2050 | """ |
|
2050 | """ | |
2051 | fn = self.find_line_magic(magic_name) |
|
2051 | fn = self.find_line_magic(magic_name) | |
2052 | if fn is None: |
|
2052 | if fn is None: | |
2053 | cm = self.find_cell_magic(magic_name) |
|
2053 | cm = self.find_cell_magic(magic_name) | |
2054 | etpl = "Line magic function `%%%s` not found%s." |
|
2054 | etpl = "Line magic function `%%%s` not found%s." | |
2055 | extra = '' if cm is None else (' (But cell magic `%%%%%s` exists, ' |
|
2055 | extra = '' if cm is None else (' (But cell magic `%%%%%s` exists, ' | |
2056 | 'did you mean that instead?)' % magic_name ) |
|
2056 | 'did you mean that instead?)' % magic_name ) | |
2057 | error(etpl % (magic_name, extra)) |
|
2057 | error(etpl % (magic_name, extra)) | |
2058 | else: |
|
2058 | else: | |
2059 | # Note: this is the distance in the stack to the user's frame. |
|
2059 | # Note: this is the distance in the stack to the user's frame. | |
2060 | # This will need to be updated if the internal calling logic gets |
|
2060 | # This will need to be updated if the internal calling logic gets | |
2061 | # refactored, or else we'll be expanding the wrong variables. |
|
2061 | # refactored, or else we'll be expanding the wrong variables. | |
2062 | stack_depth = 2 |
|
2062 | stack_depth = 2 | |
2063 | magic_arg_s = self.var_expand(line, stack_depth) |
|
2063 | magic_arg_s = self.var_expand(line, stack_depth) | |
2064 | # Put magic args in a list so we can call with f(*a) syntax |
|
2064 | # Put magic args in a list so we can call with f(*a) syntax | |
2065 | args = [magic_arg_s] |
|
2065 | args = [magic_arg_s] | |
2066 | kwargs = {} |
|
2066 | kwargs = {} | |
2067 | # Grab local namespace if we need it: |
|
2067 | # Grab local namespace if we need it: | |
2068 | if getattr(fn, "needs_local_scope", False): |
|
2068 | if getattr(fn, "needs_local_scope", False): | |
2069 | kwargs['local_ns'] = sys._getframe(stack_depth).f_locals |
|
2069 | kwargs['local_ns'] = sys._getframe(stack_depth).f_locals | |
2070 | with self.builtin_trap: |
|
2070 | with self.builtin_trap: | |
2071 | result = fn(*args,**kwargs) |
|
2071 | result = fn(*args,**kwargs) | |
2072 | return result |
|
2072 | return result | |
2073 |
|
2073 | |||
2074 | def run_cell_magic(self, magic_name, line, cell): |
|
2074 | def run_cell_magic(self, magic_name, line, cell): | |
2075 | """Execute the given cell magic. |
|
2075 | """Execute the given cell magic. | |
2076 |
|
2076 | |||
2077 | Parameters |
|
2077 | Parameters | |
2078 | ---------- |
|
2078 | ---------- | |
2079 | magic_name : str |
|
2079 | magic_name : str | |
2080 | Name of the desired magic function, without '%' prefix. |
|
2080 | Name of the desired magic function, without '%' prefix. | |
2081 |
|
2081 | |||
2082 | line : str |
|
2082 | line : str | |
2083 | The rest of the first input line as a single string. |
|
2083 | The rest of the first input line as a single string. | |
2084 |
|
2084 | |||
2085 | cell : str |
|
2085 | cell : str | |
2086 | The body of the cell as a (possibly multiline) string. |
|
2086 | The body of the cell as a (possibly multiline) string. | |
2087 | """ |
|
2087 | """ | |
2088 | fn = self.find_cell_magic(magic_name) |
|
2088 | fn = self.find_cell_magic(magic_name) | |
2089 | if fn is None: |
|
2089 | if fn is None: | |
2090 | lm = self.find_line_magic(magic_name) |
|
2090 | lm = self.find_line_magic(magic_name) | |
2091 | etpl = "Cell magic `%%{0}` not found{1}." |
|
2091 | etpl = "Cell magic `%%{0}` not found{1}." | |
2092 | extra = '' if lm is None else (' (But line magic `%{0}` exists, ' |
|
2092 | extra = '' if lm is None else (' (But line magic `%{0}` exists, ' | |
2093 | 'did you mean that instead?)'.format(magic_name)) |
|
2093 | 'did you mean that instead?)'.format(magic_name)) | |
2094 | error(etpl.format(magic_name, extra)) |
|
2094 | error(etpl.format(magic_name, extra)) | |
2095 | elif cell == '': |
|
2095 | elif cell == '': | |
2096 | message = '%%{0} is a cell magic, but the cell body is empty.'.format(magic_name) |
|
2096 | message = '%%{0} is a cell magic, but the cell body is empty.'.format(magic_name) | |
2097 | if self.find_line_magic(magic_name) is not None: |
|
2097 | if self.find_line_magic(magic_name) is not None: | |
2098 | message += ' Did you mean the line magic %{0} (single %)?'.format(magic_name) |
|
2098 | message += ' Did you mean the line magic %{0} (single %)?'.format(magic_name) | |
2099 | raise UsageError(message) |
|
2099 | raise UsageError(message) | |
2100 | else: |
|
2100 | else: | |
2101 | # Note: this is the distance in the stack to the user's frame. |
|
2101 | # Note: this is the distance in the stack to the user's frame. | |
2102 | # This will need to be updated if the internal calling logic gets |
|
2102 | # This will need to be updated if the internal calling logic gets | |
2103 | # refactored, or else we'll be expanding the wrong variables. |
|
2103 | # refactored, or else we'll be expanding the wrong variables. | |
2104 | stack_depth = 2 |
|
2104 | stack_depth = 2 | |
2105 | magic_arg_s = self.var_expand(line, stack_depth) |
|
2105 | magic_arg_s = self.var_expand(line, stack_depth) | |
2106 | with self.builtin_trap: |
|
2106 | with self.builtin_trap: | |
2107 | result = fn(magic_arg_s, cell) |
|
2107 | result = fn(magic_arg_s, cell) | |
2108 | return result |
|
2108 | return result | |
2109 |
|
2109 | |||
2110 | def find_line_magic(self, magic_name): |
|
2110 | def find_line_magic(self, magic_name): | |
2111 | """Find and return a line magic by name. |
|
2111 | """Find and return a line magic by name. | |
2112 |
|
2112 | |||
2113 | Returns None if the magic isn't found.""" |
|
2113 | Returns None if the magic isn't found.""" | |
2114 | return self.magics_manager.magics['line'].get(magic_name) |
|
2114 | return self.magics_manager.magics['line'].get(magic_name) | |
2115 |
|
2115 | |||
2116 | def find_cell_magic(self, magic_name): |
|
2116 | def find_cell_magic(self, magic_name): | |
2117 | """Find and return a cell magic by name. |
|
2117 | """Find and return a cell magic by name. | |
2118 |
|
2118 | |||
2119 | Returns None if the magic isn't found.""" |
|
2119 | Returns None if the magic isn't found.""" | |
2120 | return self.magics_manager.magics['cell'].get(magic_name) |
|
2120 | return self.magics_manager.magics['cell'].get(magic_name) | |
2121 |
|
2121 | |||
2122 | def find_magic(self, magic_name, magic_kind='line'): |
|
2122 | def find_magic(self, magic_name, magic_kind='line'): | |
2123 | """Find and return a magic of the given type by name. |
|
2123 | """Find and return a magic of the given type by name. | |
2124 |
|
2124 | |||
2125 | Returns None if the magic isn't found.""" |
|
2125 | Returns None if the magic isn't found.""" | |
2126 | return self.magics_manager.magics[magic_kind].get(magic_name) |
|
2126 | return self.magics_manager.magics[magic_kind].get(magic_name) | |
2127 |
|
2127 | |||
2128 | def magic(self, arg_s): |
|
2128 | def magic(self, arg_s): | |
2129 | """DEPRECATED. Use run_line_magic() instead. |
|
2129 | """DEPRECATED. Use run_line_magic() instead. | |
2130 |
|
2130 | |||
2131 | Call a magic function by name. |
|
2131 | Call a magic function by name. | |
2132 |
|
2132 | |||
2133 | Input: a string containing the name of the magic function to call and |
|
2133 | Input: a string containing the name of the magic function to call and | |
2134 | any additional arguments to be passed to the magic. |
|
2134 | any additional arguments to be passed to the magic. | |
2135 |
|
2135 | |||
2136 | magic('name -opt foo bar') is equivalent to typing at the ipython |
|
2136 | magic('name -opt foo bar') is equivalent to typing at the ipython | |
2137 | prompt: |
|
2137 | prompt: | |
2138 |
|
2138 | |||
2139 | In[1]: %name -opt foo bar |
|
2139 | In[1]: %name -opt foo bar | |
2140 |
|
2140 | |||
2141 | To call a magic without arguments, simply use magic('name'). |
|
2141 | To call a magic without arguments, simply use magic('name'). | |
2142 |
|
2142 | |||
2143 | This provides a proper Python function to call IPython's magics in any |
|
2143 | This provides a proper Python function to call IPython's magics in any | |
2144 | valid Python code you can type at the interpreter, including loops and |
|
2144 | valid Python code you can type at the interpreter, including loops and | |
2145 | compound statements. |
|
2145 | compound statements. | |
2146 | """ |
|
2146 | """ | |
2147 | # TODO: should we issue a loud deprecation warning here? |
|
2147 | # TODO: should we issue a loud deprecation warning here? | |
2148 | magic_name, _, magic_arg_s = arg_s.partition(' ') |
|
2148 | magic_name, _, magic_arg_s = arg_s.partition(' ') | |
2149 | magic_name = magic_name.lstrip(prefilter.ESC_MAGIC) |
|
2149 | magic_name = magic_name.lstrip(prefilter.ESC_MAGIC) | |
2150 | return self.run_line_magic(magic_name, magic_arg_s) |
|
2150 | return self.run_line_magic(magic_name, magic_arg_s) | |
2151 |
|
2151 | |||
2152 | #------------------------------------------------------------------------- |
|
2152 | #------------------------------------------------------------------------- | |
2153 | # Things related to macros |
|
2153 | # Things related to macros | |
2154 | #------------------------------------------------------------------------- |
|
2154 | #------------------------------------------------------------------------- | |
2155 |
|
2155 | |||
2156 | def define_macro(self, name, themacro): |
|
2156 | def define_macro(self, name, themacro): | |
2157 | """Define a new macro |
|
2157 | """Define a new macro | |
2158 |
|
2158 | |||
2159 | Parameters |
|
2159 | Parameters | |
2160 | ---------- |
|
2160 | ---------- | |
2161 | name : str |
|
2161 | name : str | |
2162 | The name of the macro. |
|
2162 | The name of the macro. | |
2163 | themacro : str or Macro |
|
2163 | themacro : str or Macro | |
2164 | The action to do upon invoking the macro. If a string, a new |
|
2164 | The action to do upon invoking the macro. If a string, a new | |
2165 | Macro object is created by passing the string to it. |
|
2165 | Macro object is created by passing the string to it. | |
2166 | """ |
|
2166 | """ | |
2167 |
|
2167 | |||
2168 | from IPython.core import macro |
|
2168 | from IPython.core import macro | |
2169 |
|
2169 | |||
2170 | if isinstance(themacro, string_types): |
|
2170 | if isinstance(themacro, string_types): | |
2171 | themacro = macro.Macro(themacro) |
|
2171 | themacro = macro.Macro(themacro) | |
2172 | if not isinstance(themacro, macro.Macro): |
|
2172 | if not isinstance(themacro, macro.Macro): | |
2173 | raise ValueError('A macro must be a string or a Macro instance.') |
|
2173 | raise ValueError('A macro must be a string or a Macro instance.') | |
2174 | self.user_ns[name] = themacro |
|
2174 | self.user_ns[name] = themacro | |
2175 |
|
2175 | |||
2176 | #------------------------------------------------------------------------- |
|
2176 | #------------------------------------------------------------------------- | |
2177 | # Things related to the running of system commands |
|
2177 | # Things related to the running of system commands | |
2178 | #------------------------------------------------------------------------- |
|
2178 | #------------------------------------------------------------------------- | |
2179 |
|
2179 | |||
2180 | def system_piped(self, cmd): |
|
2180 | def system_piped(self, cmd): | |
2181 | """Call the given cmd in a subprocess, piping stdout/err |
|
2181 | """Call the given cmd in a subprocess, piping stdout/err | |
2182 |
|
2182 | |||
2183 | Parameters |
|
2183 | Parameters | |
2184 | ---------- |
|
2184 | ---------- | |
2185 | cmd : str |
|
2185 | cmd : str | |
2186 | Command to execute (can not end in '&', as background processes are |
|
2186 | Command to execute (can not end in '&', as background processes are | |
2187 | not supported. Should not be a command that expects input |
|
2187 | not supported. Should not be a command that expects input | |
2188 | other than simple text. |
|
2188 | other than simple text. | |
2189 | """ |
|
2189 | """ | |
2190 | if cmd.rstrip().endswith('&'): |
|
2190 | if cmd.rstrip().endswith('&'): | |
2191 | # this is *far* from a rigorous test |
|
2191 | # this is *far* from a rigorous test | |
2192 | # We do not support backgrounding processes because we either use |
|
2192 | # We do not support backgrounding processes because we either use | |
2193 | # pexpect or pipes to read from. Users can always just call |
|
2193 | # pexpect or pipes to read from. Users can always just call | |
2194 | # os.system() or use ip.system=ip.system_raw |
|
2194 | # os.system() or use ip.system=ip.system_raw | |
2195 | # if they really want a background process. |
|
2195 | # if they really want a background process. | |
2196 | raise OSError("Background processes not supported.") |
|
2196 | raise OSError("Background processes not supported.") | |
2197 |
|
2197 | |||
2198 | # we explicitly do NOT return the subprocess status code, because |
|
2198 | # we explicitly do NOT return the subprocess status code, because | |
2199 | # a non-None value would trigger :func:`sys.displayhook` calls. |
|
2199 | # a non-None value would trigger :func:`sys.displayhook` calls. | |
2200 | # Instead, we store the exit_code in user_ns. |
|
2200 | # Instead, we store the exit_code in user_ns. | |
2201 | self.user_ns['_exit_code'] = system(self.var_expand(cmd, depth=1)) |
|
2201 | self.user_ns['_exit_code'] = system(self.var_expand(cmd, depth=1)) | |
2202 |
|
2202 | |||
2203 | def system_raw(self, cmd): |
|
2203 | def system_raw(self, cmd): | |
2204 | """Call the given cmd in a subprocess using os.system on Windows or |
|
2204 | """Call the given cmd in a subprocess using os.system on Windows or | |
2205 | subprocess.call using the system shell on other platforms. |
|
2205 | subprocess.call using the system shell on other platforms. | |
2206 |
|
2206 | |||
2207 | Parameters |
|
2207 | Parameters | |
2208 | ---------- |
|
2208 | ---------- | |
2209 | cmd : str |
|
2209 | cmd : str | |
2210 | Command to execute. |
|
2210 | Command to execute. | |
2211 | """ |
|
2211 | """ | |
2212 | cmd = self.var_expand(cmd, depth=1) |
|
2212 | cmd = self.var_expand(cmd, depth=1) | |
2213 | # protect os.system from UNC paths on Windows, which it can't handle: |
|
2213 | # protect os.system from UNC paths on Windows, which it can't handle: | |
2214 | if sys.platform == 'win32': |
|
2214 | if sys.platform == 'win32': | |
2215 | from IPython.utils._process_win32 import AvoidUNCPath |
|
2215 | from IPython.utils._process_win32 import AvoidUNCPath | |
2216 | with AvoidUNCPath() as path: |
|
2216 | with AvoidUNCPath() as path: | |
2217 | if path is not None: |
|
2217 | if path is not None: | |
2218 | cmd = '"pushd %s &&"%s' % (path, cmd) |
|
2218 | cmd = '"pushd %s &&"%s' % (path, cmd) | |
2219 | cmd = py3compat.unicode_to_str(cmd) |
|
2219 | cmd = py3compat.unicode_to_str(cmd) | |
2220 | try: |
|
2220 | try: | |
2221 | ec = os.system(cmd) |
|
2221 | ec = os.system(cmd) | |
2222 | except KeyboardInterrupt: |
|
2222 | except KeyboardInterrupt: | |
2223 | print('\n' + self.get_exception_only(), file=sys.stderr) |
|
2223 | print('\n' + self.get_exception_only(), file=sys.stderr) | |
2224 | ec = -2 |
|
2224 | ec = -2 | |
2225 | else: |
|
2225 | else: | |
2226 | cmd = py3compat.unicode_to_str(cmd) |
|
2226 | cmd = py3compat.unicode_to_str(cmd) | |
2227 | # For posix the result of the subprocess.call() below is an exit |
|
2227 | # For posix the result of the subprocess.call() below is an exit | |
2228 | # code, which by convention is zero for success, positive for |
|
2228 | # code, which by convention is zero for success, positive for | |
2229 | # program failure. Exit codes above 128 are reserved for signals, |
|
2229 | # program failure. Exit codes above 128 are reserved for signals, | |
2230 | # and the formula for converting a signal to an exit code is usually |
|
2230 | # and the formula for converting a signal to an exit code is usually | |
2231 | # signal_number+128. To more easily differentiate between exit |
|
2231 | # signal_number+128. To more easily differentiate between exit | |
2232 | # codes and signals, ipython uses negative numbers. For instance |
|
2232 | # codes and signals, ipython uses negative numbers. For instance | |
2233 | # since control-c is signal 2 but exit code 130, ipython's |
|
2233 | # since control-c is signal 2 but exit code 130, ipython's | |
2234 | # _exit_code variable will read -2. Note that some shells like |
|
2234 | # _exit_code variable will read -2. Note that some shells like | |
2235 | # csh and fish don't follow sh/bash conventions for exit codes. |
|
2235 | # csh and fish don't follow sh/bash conventions for exit codes. | |
2236 | executable = os.environ.get('SHELL', None) |
|
2236 | executable = os.environ.get('SHELL', None) | |
2237 | try: |
|
2237 | try: | |
2238 | # Use env shell instead of default /bin/sh |
|
2238 | # Use env shell instead of default /bin/sh | |
2239 | ec = subprocess.call(cmd, shell=True, executable=executable) |
|
2239 | ec = subprocess.call(cmd, shell=True, executable=executable) | |
2240 | except KeyboardInterrupt: |
|
2240 | except KeyboardInterrupt: | |
2241 | # intercept control-C; a long traceback is not useful here |
|
2241 | # intercept control-C; a long traceback is not useful here | |
2242 | print('\n' + self.get_exception_only(), file=sys.stderr) |
|
2242 | print('\n' + self.get_exception_only(), file=sys.stderr) | |
2243 | ec = 130 |
|
2243 | ec = 130 | |
2244 | if ec > 128: |
|
2244 | if ec > 128: | |
2245 | ec = -(ec - 128) |
|
2245 | ec = -(ec - 128) | |
2246 |
|
2246 | |||
2247 | # We explicitly do NOT return the subprocess status code, because |
|
2247 | # We explicitly do NOT return the subprocess status code, because | |
2248 | # a non-None value would trigger :func:`sys.displayhook` calls. |
|
2248 | # a non-None value would trigger :func:`sys.displayhook` calls. | |
2249 | # Instead, we store the exit_code in user_ns. Note the semantics |
|
2249 | # Instead, we store the exit_code in user_ns. Note the semantics | |
2250 | # of _exit_code: for control-c, _exit_code == -signal.SIGNIT, |
|
2250 | # of _exit_code: for control-c, _exit_code == -signal.SIGNIT, | |
2251 | # but raising SystemExit(_exit_code) will give status 254! |
|
2251 | # but raising SystemExit(_exit_code) will give status 254! | |
2252 | self.user_ns['_exit_code'] = ec |
|
2252 | self.user_ns['_exit_code'] = ec | |
2253 |
|
2253 | |||
2254 | # use piped system by default, because it is better behaved |
|
2254 | # use piped system by default, because it is better behaved | |
2255 | system = system_piped |
|
2255 | system = system_piped | |
2256 |
|
2256 | |||
2257 | def getoutput(self, cmd, split=True, depth=0): |
|
2257 | def getoutput(self, cmd, split=True, depth=0): | |
2258 | """Get output (possibly including stderr) from a subprocess. |
|
2258 | """Get output (possibly including stderr) from a subprocess. | |
2259 |
|
2259 | |||
2260 | Parameters |
|
2260 | Parameters | |
2261 | ---------- |
|
2261 | ---------- | |
2262 | cmd : str |
|
2262 | cmd : str | |
2263 | Command to execute (can not end in '&', as background processes are |
|
2263 | Command to execute (can not end in '&', as background processes are | |
2264 | not supported. |
|
2264 | not supported. | |
2265 | split : bool, optional |
|
2265 | split : bool, optional | |
2266 | If True, split the output into an IPython SList. Otherwise, an |
|
2266 | If True, split the output into an IPython SList. Otherwise, an | |
2267 | IPython LSString is returned. These are objects similar to normal |
|
2267 | IPython LSString is returned. These are objects similar to normal | |
2268 | lists and strings, with a few convenience attributes for easier |
|
2268 | lists and strings, with a few convenience attributes for easier | |
2269 | manipulation of line-based output. You can use '?' on them for |
|
2269 | manipulation of line-based output. You can use '?' on them for | |
2270 | details. |
|
2270 | details. | |
2271 | depth : int, optional |
|
2271 | depth : int, optional | |
2272 | How many frames above the caller are the local variables which should |
|
2272 | How many frames above the caller are the local variables which should | |
2273 | be expanded in the command string? The default (0) assumes that the |
|
2273 | be expanded in the command string? The default (0) assumes that the | |
2274 | expansion variables are in the stack frame calling this function. |
|
2274 | expansion variables are in the stack frame calling this function. | |
2275 | """ |
|
2275 | """ | |
2276 | if cmd.rstrip().endswith('&'): |
|
2276 | if cmd.rstrip().endswith('&'): | |
2277 | # this is *far* from a rigorous test |
|
2277 | # this is *far* from a rigorous test | |
2278 | raise OSError("Background processes not supported.") |
|
2278 | raise OSError("Background processes not supported.") | |
2279 | out = getoutput(self.var_expand(cmd, depth=depth+1)) |
|
2279 | out = getoutput(self.var_expand(cmd, depth=depth+1)) | |
2280 | if split: |
|
2280 | if split: | |
2281 | out = SList(out.splitlines()) |
|
2281 | out = SList(out.splitlines()) | |
2282 | else: |
|
2282 | else: | |
2283 | out = LSString(out) |
|
2283 | out = LSString(out) | |
2284 | return out |
|
2284 | return out | |
2285 |
|
2285 | |||
2286 | #------------------------------------------------------------------------- |
|
2286 | #------------------------------------------------------------------------- | |
2287 | # Things related to aliases |
|
2287 | # Things related to aliases | |
2288 | #------------------------------------------------------------------------- |
|
2288 | #------------------------------------------------------------------------- | |
2289 |
|
2289 | |||
2290 | def init_alias(self): |
|
2290 | def init_alias(self): | |
2291 | self.alias_manager = AliasManager(shell=self, parent=self) |
|
2291 | self.alias_manager = AliasManager(shell=self, parent=self) | |
2292 | self.configurables.append(self.alias_manager) |
|
2292 | self.configurables.append(self.alias_manager) | |
2293 |
|
2293 | |||
2294 | #------------------------------------------------------------------------- |
|
2294 | #------------------------------------------------------------------------- | |
2295 | # Things related to extensions |
|
2295 | # Things related to extensions | |
2296 | #------------------------------------------------------------------------- |
|
2296 | #------------------------------------------------------------------------- | |
2297 |
|
2297 | |||
2298 | def init_extension_manager(self): |
|
2298 | def init_extension_manager(self): | |
2299 | self.extension_manager = ExtensionManager(shell=self, parent=self) |
|
2299 | self.extension_manager = ExtensionManager(shell=self, parent=self) | |
2300 | self.configurables.append(self.extension_manager) |
|
2300 | self.configurables.append(self.extension_manager) | |
2301 |
|
2301 | |||
2302 | #------------------------------------------------------------------------- |
|
2302 | #------------------------------------------------------------------------- | |
2303 | # Things related to payloads |
|
2303 | # Things related to payloads | |
2304 | #------------------------------------------------------------------------- |
|
2304 | #------------------------------------------------------------------------- | |
2305 |
|
2305 | |||
2306 | def init_payload(self): |
|
2306 | def init_payload(self): | |
2307 | self.payload_manager = PayloadManager(parent=self) |
|
2307 | self.payload_manager = PayloadManager(parent=self) | |
2308 | self.configurables.append(self.payload_manager) |
|
2308 | self.configurables.append(self.payload_manager) | |
2309 |
|
2309 | |||
2310 | #------------------------------------------------------------------------- |
|
2310 | #------------------------------------------------------------------------- | |
2311 | # Things related to the prefilter |
|
2311 | # Things related to the prefilter | |
2312 | #------------------------------------------------------------------------- |
|
2312 | #------------------------------------------------------------------------- | |
2313 |
|
2313 | |||
2314 | def init_prefilter(self): |
|
2314 | def init_prefilter(self): | |
2315 | self.prefilter_manager = PrefilterManager(shell=self, parent=self) |
|
2315 | self.prefilter_manager = PrefilterManager(shell=self, parent=self) | |
2316 | self.configurables.append(self.prefilter_manager) |
|
2316 | self.configurables.append(self.prefilter_manager) | |
2317 | # Ultimately this will be refactored in the new interpreter code, but |
|
2317 | # Ultimately this will be refactored in the new interpreter code, but | |
2318 | # for now, we should expose the main prefilter method (there's legacy |
|
2318 | # for now, we should expose the main prefilter method (there's legacy | |
2319 | # code out there that may rely on this). |
|
2319 | # code out there that may rely on this). | |
2320 | self.prefilter = self.prefilter_manager.prefilter_lines |
|
2320 | self.prefilter = self.prefilter_manager.prefilter_lines | |
2321 |
|
2321 | |||
2322 | def auto_rewrite_input(self, cmd): |
|
2322 | def auto_rewrite_input(self, cmd): | |
2323 | """Print to the screen the rewritten form of the user's command. |
|
2323 | """Print to the screen the rewritten form of the user's command. | |
2324 |
|
2324 | |||
2325 | This shows visual feedback by rewriting input lines that cause |
|
2325 | This shows visual feedback by rewriting input lines that cause | |
2326 | automatic calling to kick in, like:: |
|
2326 | automatic calling to kick in, like:: | |
2327 |
|
2327 | |||
2328 | /f x |
|
2328 | /f x | |
2329 |
|
2329 | |||
2330 | into:: |
|
2330 | into:: | |
2331 |
|
2331 | |||
2332 | ------> f(x) |
|
2332 | ------> f(x) | |
2333 |
|
2333 | |||
2334 | after the user's input prompt. This helps the user understand that the |
|
2334 | after the user's input prompt. This helps the user understand that the | |
2335 | input line was transformed automatically by IPython. |
|
2335 | input line was transformed automatically by IPython. | |
2336 | """ |
|
2336 | """ | |
2337 | if not self.show_rewritten_input: |
|
2337 | if not self.show_rewritten_input: | |
2338 | return |
|
2338 | return | |
2339 |
|
2339 | |||
2340 | # This is overridden in TerminalInteractiveShell to use fancy prompts |
|
2340 | # This is overridden in TerminalInteractiveShell to use fancy prompts | |
2341 | print("------> " + cmd) |
|
2341 | print("------> " + cmd) | |
2342 |
|
2342 | |||
2343 | #------------------------------------------------------------------------- |
|
2343 | #------------------------------------------------------------------------- | |
2344 | # Things related to extracting values/expressions from kernel and user_ns |
|
2344 | # Things related to extracting values/expressions from kernel and user_ns | |
2345 | #------------------------------------------------------------------------- |
|
2345 | #------------------------------------------------------------------------- | |
2346 |
|
2346 | |||
2347 | def _user_obj_error(self): |
|
2347 | def _user_obj_error(self): | |
2348 | """return simple exception dict |
|
2348 | """return simple exception dict | |
2349 |
|
2349 | |||
2350 | for use in user_expressions |
|
2350 | for use in user_expressions | |
2351 | """ |
|
2351 | """ | |
2352 |
|
2352 | |||
2353 | etype, evalue, tb = self._get_exc_info() |
|
2353 | etype, evalue, tb = self._get_exc_info() | |
2354 | stb = self.InteractiveTB.get_exception_only(etype, evalue) |
|
2354 | stb = self.InteractiveTB.get_exception_only(etype, evalue) | |
2355 |
|
2355 | |||
2356 | exc_info = { |
|
2356 | exc_info = { | |
2357 | u'status' : 'error', |
|
2357 | u'status' : 'error', | |
2358 | u'traceback' : stb, |
|
2358 | u'traceback' : stb, | |
2359 | u'ename' : unicode_type(etype.__name__), |
|
2359 | u'ename' : unicode_type(etype.__name__), | |
2360 | u'evalue' : py3compat.safe_unicode(evalue), |
|
2360 | u'evalue' : py3compat.safe_unicode(evalue), | |
2361 | } |
|
2361 | } | |
2362 |
|
2362 | |||
2363 | return exc_info |
|
2363 | return exc_info | |
2364 |
|
2364 | |||
2365 | def _format_user_obj(self, obj): |
|
2365 | def _format_user_obj(self, obj): | |
2366 | """format a user object to display dict |
|
2366 | """format a user object to display dict | |
2367 |
|
2367 | |||
2368 | for use in user_expressions |
|
2368 | for use in user_expressions | |
2369 | """ |
|
2369 | """ | |
2370 |
|
2370 | |||
2371 | data, md = self.display_formatter.format(obj) |
|
2371 | data, md = self.display_formatter.format(obj) | |
2372 | value = { |
|
2372 | value = { | |
2373 | 'status' : 'ok', |
|
2373 | 'status' : 'ok', | |
2374 | 'data' : data, |
|
2374 | 'data' : data, | |
2375 | 'metadata' : md, |
|
2375 | 'metadata' : md, | |
2376 | } |
|
2376 | } | |
2377 | return value |
|
2377 | return value | |
2378 |
|
2378 | |||
2379 | def user_expressions(self, expressions): |
|
2379 | def user_expressions(self, expressions): | |
2380 | """Evaluate a dict of expressions in the user's namespace. |
|
2380 | """Evaluate a dict of expressions in the user's namespace. | |
2381 |
|
2381 | |||
2382 | Parameters |
|
2382 | Parameters | |
2383 | ---------- |
|
2383 | ---------- | |
2384 | expressions : dict |
|
2384 | expressions : dict | |
2385 | A dict with string keys and string values. The expression values |
|
2385 | A dict with string keys and string values. The expression values | |
2386 | should be valid Python expressions, each of which will be evaluated |
|
2386 | should be valid Python expressions, each of which will be evaluated | |
2387 | in the user namespace. |
|
2387 | in the user namespace. | |
2388 |
|
2388 | |||
2389 | Returns |
|
2389 | Returns | |
2390 | ------- |
|
2390 | ------- | |
2391 | A dict, keyed like the input expressions dict, with the rich mime-typed |
|
2391 | A dict, keyed like the input expressions dict, with the rich mime-typed | |
2392 | display_data of each value. |
|
2392 | display_data of each value. | |
2393 | """ |
|
2393 | """ | |
2394 | out = {} |
|
2394 | out = {} | |
2395 | user_ns = self.user_ns |
|
2395 | user_ns = self.user_ns | |
2396 | global_ns = self.user_global_ns |
|
2396 | global_ns = self.user_global_ns | |
2397 |
|
2397 | |||
2398 | for key, expr in iteritems(expressions): |
|
2398 | for key, expr in iteritems(expressions): | |
2399 | try: |
|
2399 | try: | |
2400 | value = self._format_user_obj(eval(expr, global_ns, user_ns)) |
|
2400 | value = self._format_user_obj(eval(expr, global_ns, user_ns)) | |
2401 | except: |
|
2401 | except: | |
2402 | value = self._user_obj_error() |
|
2402 | value = self._user_obj_error() | |
2403 | out[key] = value |
|
2403 | out[key] = value | |
2404 | return out |
|
2404 | return out | |
2405 |
|
2405 | |||
2406 | #------------------------------------------------------------------------- |
|
2406 | #------------------------------------------------------------------------- | |
2407 | # Things related to the running of code |
|
2407 | # Things related to the running of code | |
2408 | #------------------------------------------------------------------------- |
|
2408 | #------------------------------------------------------------------------- | |
2409 |
|
2409 | |||
2410 | def ex(self, cmd): |
|
2410 | def ex(self, cmd): | |
2411 | """Execute a normal python statement in user namespace.""" |
|
2411 | """Execute a normal python statement in user namespace.""" | |
2412 | with self.builtin_trap: |
|
2412 | with self.builtin_trap: | |
2413 | exec(cmd, self.user_global_ns, self.user_ns) |
|
2413 | exec(cmd, self.user_global_ns, self.user_ns) | |
2414 |
|
2414 | |||
2415 | def ev(self, expr): |
|
2415 | def ev(self, expr): | |
2416 | """Evaluate python expression expr in user namespace. |
|
2416 | """Evaluate python expression expr in user namespace. | |
2417 |
|
2417 | |||
2418 | Returns the result of evaluation |
|
2418 | Returns the result of evaluation | |
2419 | """ |
|
2419 | """ | |
2420 | with self.builtin_trap: |
|
2420 | with self.builtin_trap: | |
2421 | return eval(expr, self.user_global_ns, self.user_ns) |
|
2421 | return eval(expr, self.user_global_ns, self.user_ns) | |
2422 |
|
2422 | |||
2423 | def safe_execfile(self, fname, *where, **kw): |
|
2423 | def safe_execfile(self, fname, *where, **kw): | |
2424 | """A safe version of the builtin execfile(). |
|
2424 | """A safe version of the builtin execfile(). | |
2425 |
|
2425 | |||
2426 | This version will never throw an exception, but instead print |
|
2426 | This version will never throw an exception, but instead print | |
2427 | helpful error messages to the screen. This only works on pure |
|
2427 | helpful error messages to the screen. This only works on pure | |
2428 | Python files with the .py extension. |
|
2428 | Python files with the .py extension. | |
2429 |
|
2429 | |||
2430 | Parameters |
|
2430 | Parameters | |
2431 | ---------- |
|
2431 | ---------- | |
2432 | fname : string |
|
2432 | fname : string | |
2433 | The name of the file to be executed. |
|
2433 | The name of the file to be executed. | |
2434 | where : tuple |
|
2434 | where : tuple | |
2435 | One or two namespaces, passed to execfile() as (globals,locals). |
|
2435 | One or two namespaces, passed to execfile() as (globals,locals). | |
2436 | If only one is given, it is passed as both. |
|
2436 | If only one is given, it is passed as both. | |
2437 | exit_ignore : bool (False) |
|
2437 | exit_ignore : bool (False) | |
2438 | If True, then silence SystemExit for non-zero status (it is always |
|
2438 | If True, then silence SystemExit for non-zero status (it is always | |
2439 | silenced for zero status, as it is so common). |
|
2439 | silenced for zero status, as it is so common). | |
2440 | raise_exceptions : bool (False) |
|
2440 | raise_exceptions : bool (False) | |
2441 | If True raise exceptions everywhere. Meant for testing. |
|
2441 | If True raise exceptions everywhere. Meant for testing. | |
2442 | shell_futures : bool (False) |
|
2442 | shell_futures : bool (False) | |
2443 | If True, the code will share future statements with the interactive |
|
2443 | If True, the code will share future statements with the interactive | |
2444 | shell. It will both be affected by previous __future__ imports, and |
|
2444 | shell. It will both be affected by previous __future__ imports, and | |
2445 | any __future__ imports in the code will affect the shell. If False, |
|
2445 | any __future__ imports in the code will affect the shell. If False, | |
2446 | __future__ imports are not shared in either direction. |
|
2446 | __future__ imports are not shared in either direction. | |
2447 |
|
2447 | |||
2448 | """ |
|
2448 | """ | |
2449 | kw.setdefault('exit_ignore', False) |
|
2449 | kw.setdefault('exit_ignore', False) | |
2450 | kw.setdefault('raise_exceptions', False) |
|
2450 | kw.setdefault('raise_exceptions', False) | |
2451 | kw.setdefault('shell_futures', False) |
|
2451 | kw.setdefault('shell_futures', False) | |
2452 |
|
2452 | |||
2453 | fname = os.path.abspath(os.path.expanduser(fname)) |
|
2453 | fname = os.path.abspath(os.path.expanduser(fname)) | |
2454 |
|
2454 | |||
2455 | # Make sure we can open the file |
|
2455 | # Make sure we can open the file | |
2456 | try: |
|
2456 | try: | |
2457 | with open(fname): |
|
2457 | with open(fname): | |
2458 | pass |
|
2458 | pass | |
2459 | except: |
|
2459 | except: | |
2460 | warn('Could not open file <%s> for safe execution.' % fname) |
|
2460 | warn('Could not open file <%s> for safe execution.' % fname) | |
2461 | return |
|
2461 | return | |
2462 |
|
2462 | |||
2463 | # Find things also in current directory. This is needed to mimic the |
|
2463 | # Find things also in current directory. This is needed to mimic the | |
2464 | # behavior of running a script from the system command line, where |
|
2464 | # behavior of running a script from the system command line, where | |
2465 | # Python inserts the script's directory into sys.path |
|
2465 | # Python inserts the script's directory into sys.path | |
2466 | dname = os.path.dirname(fname) |
|
2466 | dname = os.path.dirname(fname) | |
2467 |
|
2467 | |||
2468 | with prepended_to_syspath(dname): |
|
2468 | with prepended_to_syspath(dname): | |
2469 | try: |
|
2469 | try: | |
2470 | glob, loc = (where + (None, ))[:2] |
|
2470 | glob, loc = (where + (None, ))[:2] | |
2471 | py3compat.execfile( |
|
2471 | py3compat.execfile( | |
2472 | fname, glob, loc, |
|
2472 | fname, glob, loc, | |
2473 | self.compile if kw['shell_futures'] else None) |
|
2473 | self.compile if kw['shell_futures'] else None) | |
2474 | except SystemExit as status: |
|
2474 | except SystemExit as status: | |
2475 | # If the call was made with 0 or None exit status (sys.exit(0) |
|
2475 | # If the call was made with 0 or None exit status (sys.exit(0) | |
2476 | # or sys.exit() ), don't bother showing a traceback, as both of |
|
2476 | # or sys.exit() ), don't bother showing a traceback, as both of | |
2477 | # these are considered normal by the OS: |
|
2477 | # these are considered normal by the OS: | |
2478 | # > python -c'import sys;sys.exit(0)'; echo $? |
|
2478 | # > python -c'import sys;sys.exit(0)'; echo $? | |
2479 | # 0 |
|
2479 | # 0 | |
2480 | # > python -c'import sys;sys.exit()'; echo $? |
|
2480 | # > python -c'import sys;sys.exit()'; echo $? | |
2481 | # 0 |
|
2481 | # 0 | |
2482 | # For other exit status, we show the exception unless |
|
2482 | # For other exit status, we show the exception unless | |
2483 | # explicitly silenced, but only in short form. |
|
2483 | # explicitly silenced, but only in short form. | |
2484 | if status.code: |
|
2484 | if status.code: | |
2485 | if kw['raise_exceptions']: |
|
2485 | if kw['raise_exceptions']: | |
2486 | raise |
|
2486 | raise | |
2487 | if not kw['exit_ignore']: |
|
2487 | if not kw['exit_ignore']: | |
2488 | self.showtraceback(exception_only=True) |
|
2488 | self.showtraceback(exception_only=True) | |
2489 | except: |
|
2489 | except: | |
2490 | if kw['raise_exceptions']: |
|
2490 | if kw['raise_exceptions']: | |
2491 | raise |
|
2491 | raise | |
2492 | # tb offset is 2 because we wrap execfile |
|
2492 | # tb offset is 2 because we wrap execfile | |
2493 | self.showtraceback(tb_offset=2) |
|
2493 | self.showtraceback(tb_offset=2) | |
2494 |
|
2494 | |||
2495 | def safe_execfile_ipy(self, fname, shell_futures=False, raise_exceptions=False): |
|
2495 | def safe_execfile_ipy(self, fname, shell_futures=False, raise_exceptions=False): | |
2496 | """Like safe_execfile, but for .ipy or .ipynb files with IPython syntax. |
|
2496 | """Like safe_execfile, but for .ipy or .ipynb files with IPython syntax. | |
2497 |
|
2497 | |||
2498 | Parameters |
|
2498 | Parameters | |
2499 | ---------- |
|
2499 | ---------- | |
2500 | fname : str |
|
2500 | fname : str | |
2501 | The name of the file to execute. The filename must have a |
|
2501 | The name of the file to execute. The filename must have a | |
2502 | .ipy or .ipynb extension. |
|
2502 | .ipy or .ipynb extension. | |
2503 | shell_futures : bool (False) |
|
2503 | shell_futures : bool (False) | |
2504 | If True, the code will share future statements with the interactive |
|
2504 | If True, the code will share future statements with the interactive | |
2505 | shell. It will both be affected by previous __future__ imports, and |
|
2505 | shell. It will both be affected by previous __future__ imports, and | |
2506 | any __future__ imports in the code will affect the shell. If False, |
|
2506 | any __future__ imports in the code will affect the shell. If False, | |
2507 | __future__ imports are not shared in either direction. |
|
2507 | __future__ imports are not shared in either direction. | |
2508 | raise_exceptions : bool (False) |
|
2508 | raise_exceptions : bool (False) | |
2509 | If True raise exceptions everywhere. Meant for testing. |
|
2509 | If True raise exceptions everywhere. Meant for testing. | |
2510 | """ |
|
2510 | """ | |
2511 | fname = os.path.abspath(os.path.expanduser(fname)) |
|
2511 | fname = os.path.abspath(os.path.expanduser(fname)) | |
2512 |
|
2512 | |||
2513 | # Make sure we can open the file |
|
2513 | # Make sure we can open the file | |
2514 | try: |
|
2514 | try: | |
2515 | with open(fname): |
|
2515 | with open(fname): | |
2516 | pass |
|
2516 | pass | |
2517 | except: |
|
2517 | except: | |
2518 | warn('Could not open file <%s> for safe execution.' % fname) |
|
2518 | warn('Could not open file <%s> for safe execution.' % fname) | |
2519 | return |
|
2519 | return | |
2520 |
|
2520 | |||
2521 | # Find things also in current directory. This is needed to mimic the |
|
2521 | # Find things also in current directory. This is needed to mimic the | |
2522 | # behavior of running a script from the system command line, where |
|
2522 | # behavior of running a script from the system command line, where | |
2523 | # Python inserts the script's directory into sys.path |
|
2523 | # Python inserts the script's directory into sys.path | |
2524 | dname = os.path.dirname(fname) |
|
2524 | dname = os.path.dirname(fname) | |
2525 |
|
2525 | |||
2526 | def get_cells(): |
|
2526 | def get_cells(): | |
2527 | """generator for sequence of code blocks to run""" |
|
2527 | """generator for sequence of code blocks to run""" | |
2528 | if fname.endswith('.ipynb'): |
|
2528 | if fname.endswith('.ipynb'): | |
2529 | from nbformat import read |
|
2529 | from nbformat import read | |
2530 | with io_open(fname) as f: |
|
2530 | with io_open(fname) as f: | |
2531 | nb = read(f, as_version=4) |
|
2531 | nb = read(f, as_version=4) | |
2532 | if not nb.cells: |
|
2532 | if not nb.cells: | |
2533 | return |
|
2533 | return | |
2534 | for cell in nb.cells: |
|
2534 | for cell in nb.cells: | |
2535 | if cell.cell_type == 'code': |
|
2535 | if cell.cell_type == 'code': | |
2536 | yield cell.source |
|
2536 | yield cell.source | |
2537 | else: |
|
2537 | else: | |
2538 | with open(fname) as f: |
|
2538 | with open(fname) as f: | |
2539 | yield f.read() |
|
2539 | yield f.read() | |
2540 |
|
2540 | |||
2541 | with prepended_to_syspath(dname): |
|
2541 | with prepended_to_syspath(dname): | |
2542 | try: |
|
2542 | try: | |
2543 | for cell in get_cells(): |
|
2543 | for cell in get_cells(): | |
2544 | result = self.run_cell(cell, silent=True, shell_futures=shell_futures) |
|
2544 | result = self.run_cell(cell, silent=True, shell_futures=shell_futures) | |
2545 | if raise_exceptions: |
|
2545 | if raise_exceptions: | |
2546 | result.raise_error() |
|
2546 | result.raise_error() | |
2547 | elif not result.success: |
|
2547 | elif not result.success: | |
2548 | break |
|
2548 | break | |
2549 | except: |
|
2549 | except: | |
2550 | if raise_exceptions: |
|
2550 | if raise_exceptions: | |
2551 | raise |
|
2551 | raise | |
2552 | self.showtraceback() |
|
2552 | self.showtraceback() | |
2553 | warn('Unknown failure executing file: <%s>' % fname) |
|
2553 | warn('Unknown failure executing file: <%s>' % fname) | |
2554 |
|
2554 | |||
2555 | def safe_run_module(self, mod_name, where): |
|
2555 | def safe_run_module(self, mod_name, where): | |
2556 | """A safe version of runpy.run_module(). |
|
2556 | """A safe version of runpy.run_module(). | |
2557 |
|
2557 | |||
2558 | This version will never throw an exception, but instead print |
|
2558 | This version will never throw an exception, but instead print | |
2559 | helpful error messages to the screen. |
|
2559 | helpful error messages to the screen. | |
2560 |
|
2560 | |||
2561 | `SystemExit` exceptions with status code 0 or None are ignored. |
|
2561 | `SystemExit` exceptions with status code 0 or None are ignored. | |
2562 |
|
2562 | |||
2563 | Parameters |
|
2563 | Parameters | |
2564 | ---------- |
|
2564 | ---------- | |
2565 | mod_name : string |
|
2565 | mod_name : string | |
2566 | The name of the module to be executed. |
|
2566 | The name of the module to be executed. | |
2567 | where : dict |
|
2567 | where : dict | |
2568 | The globals namespace. |
|
2568 | The globals namespace. | |
2569 | """ |
|
2569 | """ | |
2570 | try: |
|
2570 | try: | |
2571 | try: |
|
2571 | try: | |
2572 | where.update( |
|
2572 | where.update( | |
2573 | runpy.run_module(str(mod_name), run_name="__main__", |
|
2573 | runpy.run_module(str(mod_name), run_name="__main__", | |
2574 | alter_sys=True) |
|
2574 | alter_sys=True) | |
2575 | ) |
|
2575 | ) | |
2576 | except SystemExit as status: |
|
2576 | except SystemExit as status: | |
2577 | if status.code: |
|
2577 | if status.code: | |
2578 | raise |
|
2578 | raise | |
2579 | except: |
|
2579 | except: | |
2580 | self.showtraceback() |
|
2580 | self.showtraceback() | |
2581 | warn('Unknown failure executing module: <%s>' % mod_name) |
|
2581 | warn('Unknown failure executing module: <%s>' % mod_name) | |
2582 |
|
2582 | |||
2583 | def run_cell(self, raw_cell, store_history=False, silent=False, shell_futures=True): |
|
2583 | def run_cell(self, raw_cell, store_history=False, silent=False, shell_futures=True): | |
2584 | """Run a complete IPython cell. |
|
2584 | """Run a complete IPython cell. | |
2585 |
|
2585 | |||
2586 | Parameters |
|
2586 | Parameters | |
2587 | ---------- |
|
2587 | ---------- | |
2588 | raw_cell : str |
|
2588 | raw_cell : str | |
2589 | The code (including IPython code such as %magic functions) to run. |
|
2589 | The code (including IPython code such as %magic functions) to run. | |
2590 | store_history : bool |
|
2590 | store_history : bool | |
2591 | If True, the raw and translated cell will be stored in IPython's |
|
2591 | If True, the raw and translated cell will be stored in IPython's | |
2592 | history. For user code calling back into IPython's machinery, this |
|
2592 | history. For user code calling back into IPython's machinery, this | |
2593 | should be set to False. |
|
2593 | should be set to False. | |
2594 | silent : bool |
|
2594 | silent : bool | |
2595 | If True, avoid side-effects, such as implicit displayhooks and |
|
2595 | If True, avoid side-effects, such as implicit displayhooks and | |
2596 | and logging. silent=True forces store_history=False. |
|
2596 | and logging. silent=True forces store_history=False. | |
2597 | shell_futures : bool |
|
2597 | shell_futures : bool | |
2598 | If True, the code will share future statements with the interactive |
|
2598 | If True, the code will share future statements with the interactive | |
2599 | shell. It will both be affected by previous __future__ imports, and |
|
2599 | shell. It will both be affected by previous __future__ imports, and | |
2600 | any __future__ imports in the code will affect the shell. If False, |
|
2600 | any __future__ imports in the code will affect the shell. If False, | |
2601 | __future__ imports are not shared in either direction. |
|
2601 | __future__ imports are not shared in either direction. | |
2602 |
|
2602 | |||
2603 | Returns |
|
2603 | Returns | |
2604 | ------- |
|
2604 | ------- | |
2605 | result : :class:`ExecutionResult` |
|
2605 | result : :class:`ExecutionResult` | |
2606 | """ |
|
2606 | """ | |
2607 | result = ExecutionResult() |
|
2607 | result = ExecutionResult() | |
2608 |
|
2608 | |||
2609 | if (not raw_cell) or raw_cell.isspace(): |
|
2609 | if (not raw_cell) or raw_cell.isspace(): | |
2610 | return result |
|
2610 | return result | |
2611 |
|
2611 | |||
2612 | if silent: |
|
2612 | if silent: | |
2613 | store_history = False |
|
2613 | store_history = False | |
2614 |
|
2614 | |||
2615 | if store_history: |
|
2615 | if store_history: | |
2616 | result.execution_count = self.execution_count |
|
2616 | result.execution_count = self.execution_count | |
2617 |
|
2617 | |||
2618 | def error_before_exec(value): |
|
2618 | def error_before_exec(value): | |
2619 | result.error_before_exec = value |
|
2619 | result.error_before_exec = value | |
2620 | return result |
|
2620 | return result | |
2621 |
|
2621 | |||
2622 | self.events.trigger('pre_execute') |
|
2622 | self.events.trigger('pre_execute') | |
2623 | if not silent: |
|
2623 | if not silent: | |
2624 | self.events.trigger('pre_run_cell') |
|
2624 | self.events.trigger('pre_run_cell') | |
2625 |
|
2625 | |||
2626 | # If any of our input transformation (input_transformer_manager or |
|
2626 | # If any of our input transformation (input_transformer_manager or | |
2627 | # prefilter_manager) raises an exception, we store it in this variable |
|
2627 | # prefilter_manager) raises an exception, we store it in this variable | |
2628 | # so that we can display the error after logging the input and storing |
|
2628 | # so that we can display the error after logging the input and storing | |
2629 | # it in the history. |
|
2629 | # it in the history. | |
2630 | preprocessing_exc_tuple = None |
|
2630 | preprocessing_exc_tuple = None | |
2631 | try: |
|
2631 | try: | |
2632 | # Static input transformations |
|
2632 | # Static input transformations | |
2633 | cell = self.input_transformer_manager.transform_cell(raw_cell) |
|
2633 | cell = self.input_transformer_manager.transform_cell(raw_cell) | |
2634 | except SyntaxError: |
|
2634 | except SyntaxError: | |
2635 | preprocessing_exc_tuple = sys.exc_info() |
|
2635 | preprocessing_exc_tuple = sys.exc_info() | |
2636 | cell = raw_cell # cell has to exist so it can be stored/logged |
|
2636 | cell = raw_cell # cell has to exist so it can be stored/logged | |
2637 | else: |
|
2637 | else: | |
2638 | if len(cell.splitlines()) == 1: |
|
2638 | if len(cell.splitlines()) == 1: | |
2639 | # Dynamic transformations - only applied for single line commands |
|
2639 | # Dynamic transformations - only applied for single line commands | |
2640 | with self.builtin_trap: |
|
2640 | with self.builtin_trap: | |
2641 | try: |
|
2641 | try: | |
2642 | # use prefilter_lines to handle trailing newlines |
|
2642 | # use prefilter_lines to handle trailing newlines | |
2643 | # restore trailing newline for ast.parse |
|
2643 | # restore trailing newline for ast.parse | |
2644 | cell = self.prefilter_manager.prefilter_lines(cell) + '\n' |
|
2644 | cell = self.prefilter_manager.prefilter_lines(cell) + '\n' | |
2645 | except Exception: |
|
2645 | except Exception: | |
2646 | # don't allow prefilter errors to crash IPython |
|
2646 | # don't allow prefilter errors to crash IPython | |
2647 | preprocessing_exc_tuple = sys.exc_info() |
|
2647 | preprocessing_exc_tuple = sys.exc_info() | |
2648 |
|
2648 | |||
2649 | # Store raw and processed history |
|
2649 | # Store raw and processed history | |
2650 | if store_history: |
|
2650 | if store_history: | |
2651 | self.history_manager.store_inputs(self.execution_count, |
|
2651 | self.history_manager.store_inputs(self.execution_count, | |
2652 | cell, raw_cell) |
|
2652 | cell, raw_cell) | |
2653 | if not silent: |
|
2653 | if not silent: | |
2654 | self.logger.log(cell, raw_cell) |
|
2654 | self.logger.log(cell, raw_cell) | |
2655 |
|
2655 | |||
2656 | # Display the exception if input processing failed. |
|
2656 | # Display the exception if input processing failed. | |
2657 | if preprocessing_exc_tuple is not None: |
|
2657 | if preprocessing_exc_tuple is not None: | |
2658 | self.showtraceback(preprocessing_exc_tuple) |
|
2658 | self.showtraceback(preprocessing_exc_tuple) | |
2659 | if store_history: |
|
2659 | if store_history: | |
2660 | self.execution_count += 1 |
|
2660 | self.execution_count += 1 | |
2661 | return error_before_exec(preprocessing_exc_tuple[2]) |
|
2661 | return error_before_exec(preprocessing_exc_tuple[2]) | |
2662 |
|
2662 | |||
2663 | # Our own compiler remembers the __future__ environment. If we want to |
|
2663 | # Our own compiler remembers the __future__ environment. If we want to | |
2664 | # run code with a separate __future__ environment, use the default |
|
2664 | # run code with a separate __future__ environment, use the default | |
2665 | # compiler |
|
2665 | # compiler | |
2666 | compiler = self.compile if shell_futures else CachingCompiler() |
|
2666 | compiler = self.compile if shell_futures else CachingCompiler() | |
2667 |
|
2667 | |||
2668 | with self.builtin_trap: |
|
2668 | with self.builtin_trap: | |
2669 | cell_name = self.compile.cache(cell, self.execution_count) |
|
2669 | cell_name = self.compile.cache(cell, self.execution_count) | |
2670 |
|
2670 | |||
2671 | with self.display_trap: |
|
2671 | with self.display_trap: | |
2672 | # Compile to bytecode |
|
2672 | # Compile to bytecode | |
2673 | try: |
|
2673 | try: | |
2674 | code_ast = compiler.ast_parse(cell, filename=cell_name) |
|
2674 | code_ast = compiler.ast_parse(cell, filename=cell_name) | |
2675 | except self.custom_exceptions as e: |
|
2675 | except self.custom_exceptions as e: | |
2676 | etype, value, tb = sys.exc_info() |
|
2676 | etype, value, tb = sys.exc_info() | |
2677 | self.CustomTB(etype, value, tb) |
|
2677 | self.CustomTB(etype, value, tb) | |
2678 | return error_before_exec(e) |
|
2678 | return error_before_exec(e) | |
2679 | except IndentationError as e: |
|
2679 | except IndentationError as e: | |
2680 | self.showindentationerror() |
|
2680 | self.showindentationerror() | |
2681 | if store_history: |
|
2681 | if store_history: | |
2682 | self.execution_count += 1 |
|
2682 | self.execution_count += 1 | |
2683 | return error_before_exec(e) |
|
2683 | return error_before_exec(e) | |
2684 | except (OverflowError, SyntaxError, ValueError, TypeError, |
|
2684 | except (OverflowError, SyntaxError, ValueError, TypeError, | |
2685 | MemoryError) as e: |
|
2685 | MemoryError) as e: | |
2686 | self.showsyntaxerror() |
|
2686 | self.showsyntaxerror() | |
2687 | if store_history: |
|
2687 | if store_history: | |
2688 | self.execution_count += 1 |
|
2688 | self.execution_count += 1 | |
2689 | return error_before_exec(e) |
|
2689 | return error_before_exec(e) | |
2690 |
|
2690 | |||
2691 | # Apply AST transformations |
|
2691 | # Apply AST transformations | |
2692 | try: |
|
2692 | try: | |
2693 | code_ast = self.transform_ast(code_ast) |
|
2693 | code_ast = self.transform_ast(code_ast) | |
2694 | except InputRejected as e: |
|
2694 | except InputRejected as e: | |
2695 | self.showtraceback() |
|
2695 | self.showtraceback() | |
2696 | if store_history: |
|
2696 | if store_history: | |
2697 | self.execution_count += 1 |
|
2697 | self.execution_count += 1 | |
2698 | return error_before_exec(e) |
|
2698 | return error_before_exec(e) | |
2699 |
|
2699 | |||
2700 | # Give the displayhook a reference to our ExecutionResult so it |
|
2700 | # Give the displayhook a reference to our ExecutionResult so it | |
2701 | # can fill in the output value. |
|
2701 | # can fill in the output value. | |
2702 | self.displayhook.exec_result = result |
|
2702 | self.displayhook.exec_result = result | |
2703 |
|
2703 | |||
2704 | # Execute the user code |
|
2704 | # Execute the user code | |
2705 | interactivity = "none" if silent else self.ast_node_interactivity |
|
2705 | interactivity = "none" if silent else self.ast_node_interactivity | |
2706 | self.run_ast_nodes(code_ast.body, cell_name, |
|
2706 | self.run_ast_nodes(code_ast.body, cell_name, | |
2707 | interactivity=interactivity, compiler=compiler, result=result) |
|
2707 | interactivity=interactivity, compiler=compiler, result=result) | |
2708 |
|
2708 | |||
2709 | # Reset this so later displayed values do not modify the |
|
2709 | # Reset this so later displayed values do not modify the | |
2710 | # ExecutionResult |
|
2710 | # ExecutionResult | |
2711 | self.displayhook.exec_result = None |
|
2711 | self.displayhook.exec_result = None | |
2712 |
|
2712 | |||
2713 | self.events.trigger('post_execute') |
|
2713 | self.events.trigger('post_execute') | |
2714 | if not silent: |
|
2714 | if not silent: | |
2715 | self.events.trigger('post_run_cell') |
|
2715 | self.events.trigger('post_run_cell') | |
2716 |
|
2716 | |||
2717 | if store_history: |
|
2717 | if store_history: | |
2718 | # Write output to the database. Does nothing unless |
|
2718 | # Write output to the database. Does nothing unless | |
2719 | # history output logging is enabled. |
|
2719 | # history output logging is enabled. | |
2720 | self.history_manager.store_output(self.execution_count) |
|
2720 | self.history_manager.store_output(self.execution_count) | |
2721 | # Each cell is a *single* input, regardless of how many lines it has |
|
2721 | # Each cell is a *single* input, regardless of how many lines it has | |
2722 | self.execution_count += 1 |
|
2722 | self.execution_count += 1 | |
2723 |
|
2723 | |||
2724 | return result |
|
2724 | return result | |
2725 |
|
2725 | |||
2726 | def transform_ast(self, node): |
|
2726 | def transform_ast(self, node): | |
2727 | """Apply the AST transformations from self.ast_transformers |
|
2727 | """Apply the AST transformations from self.ast_transformers | |
2728 |
|
2728 | |||
2729 | Parameters |
|
2729 | Parameters | |
2730 | ---------- |
|
2730 | ---------- | |
2731 | node : ast.Node |
|
2731 | node : ast.Node | |
2732 | The root node to be transformed. Typically called with the ast.Module |
|
2732 | The root node to be transformed. Typically called with the ast.Module | |
2733 | produced by parsing user input. |
|
2733 | produced by parsing user input. | |
2734 |
|
2734 | |||
2735 | Returns |
|
2735 | Returns | |
2736 | ------- |
|
2736 | ------- | |
2737 | An ast.Node corresponding to the node it was called with. Note that it |
|
2737 | An ast.Node corresponding to the node it was called with. Note that it | |
2738 | may also modify the passed object, so don't rely on references to the |
|
2738 | may also modify the passed object, so don't rely on references to the | |
2739 | original AST. |
|
2739 | original AST. | |
2740 | """ |
|
2740 | """ | |
2741 | for transformer in self.ast_transformers: |
|
2741 | for transformer in self.ast_transformers: | |
2742 | try: |
|
2742 | try: | |
2743 | node = transformer.visit(node) |
|
2743 | node = transformer.visit(node) | |
2744 | except InputRejected: |
|
2744 | except InputRejected: | |
2745 | # User-supplied AST transformers can reject an input by raising |
|
2745 | # User-supplied AST transformers can reject an input by raising | |
2746 | # an InputRejected. Short-circuit in this case so that we |
|
2746 | # an InputRejected. Short-circuit in this case so that we | |
2747 | # don't unregister the transform. |
|
2747 | # don't unregister the transform. | |
2748 | raise |
|
2748 | raise | |
2749 | except Exception: |
|
2749 | except Exception: | |
2750 | warn("AST transformer %r threw an error. It will be unregistered." % transformer) |
|
2750 | warn("AST transformer %r threw an error. It will be unregistered." % transformer) | |
2751 | self.ast_transformers.remove(transformer) |
|
2751 | self.ast_transformers.remove(transformer) | |
2752 |
|
2752 | |||
2753 | if self.ast_transformers: |
|
2753 | if self.ast_transformers: | |
2754 | ast.fix_missing_locations(node) |
|
2754 | ast.fix_missing_locations(node) | |
2755 | return node |
|
2755 | return node | |
2756 |
|
2756 | |||
2757 |
|
2757 | |||
2758 | def run_ast_nodes(self, nodelist, cell_name, interactivity='last_expr', |
|
2758 | def run_ast_nodes(self, nodelist, cell_name, interactivity='last_expr', | |
2759 | compiler=compile, result=None): |
|
2759 | compiler=compile, result=None): | |
2760 | """Run a sequence of AST nodes. The execution mode depends on the |
|
2760 | """Run a sequence of AST nodes. The execution mode depends on the | |
2761 | interactivity parameter. |
|
2761 | interactivity parameter. | |
2762 |
|
2762 | |||
2763 | Parameters |
|
2763 | Parameters | |
2764 | ---------- |
|
2764 | ---------- | |
2765 | nodelist : list |
|
2765 | nodelist : list | |
2766 | A sequence of AST nodes to run. |
|
2766 | A sequence of AST nodes to run. | |
2767 | cell_name : str |
|
2767 | cell_name : str | |
2768 | Will be passed to the compiler as the filename of the cell. Typically |
|
2768 | Will be passed to the compiler as the filename of the cell. Typically | |
2769 | the value returned by ip.compile.cache(cell). |
|
2769 | the value returned by ip.compile.cache(cell). | |
2770 | interactivity : str |
|
2770 | interactivity : str | |
2771 | 'all', 'last', 'last_expr' or 'none', specifying which nodes should be |
|
2771 | 'all', 'last', 'last_expr' or 'none', specifying which nodes should be | |
2772 | run interactively (displaying output from expressions). 'last_expr' |
|
2772 | run interactively (displaying output from expressions). 'last_expr' | |
2773 | will run the last node interactively only if it is an expression (i.e. |
|
2773 | will run the last node interactively only if it is an expression (i.e. | |
2774 | expressions in loops or other blocks are not displayed. Other values |
|
2774 | expressions in loops or other blocks are not displayed. Other values | |
2775 | for this parameter will raise a ValueError. |
|
2775 | for this parameter will raise a ValueError. | |
2776 | compiler : callable |
|
2776 | compiler : callable | |
2777 | A function with the same interface as the built-in compile(), to turn |
|
2777 | A function with the same interface as the built-in compile(), to turn | |
2778 | the AST nodes into code objects. Default is the built-in compile(). |
|
2778 | the AST nodes into code objects. Default is the built-in compile(). | |
2779 | result : ExecutionResult, optional |
|
2779 | result : ExecutionResult, optional | |
2780 | An object to store exceptions that occur during execution. |
|
2780 | An object to store exceptions that occur during execution. | |
2781 |
|
2781 | |||
2782 | Returns |
|
2782 | Returns | |
2783 | ------- |
|
2783 | ------- | |
2784 | True if an exception occurred while running code, False if it finished |
|
2784 | True if an exception occurred while running code, False if it finished | |
2785 | running. |
|
2785 | running. | |
2786 | """ |
|
2786 | """ | |
2787 | if not nodelist: |
|
2787 | if not nodelist: | |
2788 | return |
|
2788 | return | |
2789 |
|
2789 | |||
2790 | if interactivity == 'last_expr': |
|
2790 | if interactivity == 'last_expr': | |
2791 | if isinstance(nodelist[-1], ast.Expr): |
|
2791 | if isinstance(nodelist[-1], ast.Expr): | |
2792 | interactivity = "last" |
|
2792 | interactivity = "last" | |
2793 | else: |
|
2793 | else: | |
2794 | interactivity = "none" |
|
2794 | interactivity = "none" | |
2795 |
|
2795 | |||
2796 | if interactivity == 'none': |
|
2796 | if interactivity == 'none': | |
2797 | to_run_exec, to_run_interactive = nodelist, [] |
|
2797 | to_run_exec, to_run_interactive = nodelist, [] | |
2798 | elif interactivity == 'last': |
|
2798 | elif interactivity == 'last': | |
2799 | to_run_exec, to_run_interactive = nodelist[:-1], nodelist[-1:] |
|
2799 | to_run_exec, to_run_interactive = nodelist[:-1], nodelist[-1:] | |
2800 | elif interactivity == 'all': |
|
2800 | elif interactivity == 'all': | |
2801 | to_run_exec, to_run_interactive = [], nodelist |
|
2801 | to_run_exec, to_run_interactive = [], nodelist | |
2802 | else: |
|
2802 | else: | |
2803 | raise ValueError("Interactivity was %r" % interactivity) |
|
2803 | raise ValueError("Interactivity was %r" % interactivity) | |
2804 |
|
2804 | |||
2805 | try: |
|
2805 | try: | |
2806 | for i, node in enumerate(to_run_exec): |
|
2806 | for i, node in enumerate(to_run_exec): | |
2807 | mod = ast.Module([node]) |
|
2807 | mod = ast.Module([node]) | |
2808 | code = compiler(mod, cell_name, "exec") |
|
2808 | code = compiler(mod, cell_name, "exec") | |
2809 | if self.run_code(code, result): |
|
2809 | if self.run_code(code, result): | |
2810 | return True |
|
2810 | return True | |
2811 |
|
2811 | |||
2812 | for i, node in enumerate(to_run_interactive): |
|
2812 | for i, node in enumerate(to_run_interactive): | |
2813 | mod = ast.Interactive([node]) |
|
2813 | mod = ast.Interactive([node]) | |
2814 | code = compiler(mod, cell_name, "single") |
|
2814 | code = compiler(mod, cell_name, "single") | |
2815 | if self.run_code(code, result): |
|
2815 | if self.run_code(code, result): | |
2816 | return True |
|
2816 | return True | |
2817 |
|
2817 | |||
2818 | # Flush softspace |
|
2818 | # Flush softspace | |
2819 | if softspace(sys.stdout, 0): |
|
2819 | if softspace(sys.stdout, 0): | |
2820 | print() |
|
2820 | print() | |
2821 |
|
2821 | |||
2822 | except: |
|
2822 | except: | |
2823 | # It's possible to have exceptions raised here, typically by |
|
2823 | # It's possible to have exceptions raised here, typically by | |
2824 | # compilation of odd code (such as a naked 'return' outside a |
|
2824 | # compilation of odd code (such as a naked 'return' outside a | |
2825 | # function) that did parse but isn't valid. Typically the exception |
|
2825 | # function) that did parse but isn't valid. Typically the exception | |
2826 | # is a SyntaxError, but it's safest just to catch anything and show |
|
2826 | # is a SyntaxError, but it's safest just to catch anything and show | |
2827 | # the user a traceback. |
|
2827 | # the user a traceback. | |
2828 |
|
2828 | |||
2829 | # We do only one try/except outside the loop to minimize the impact |
|
2829 | # We do only one try/except outside the loop to minimize the impact | |
2830 | # on runtime, and also because if any node in the node list is |
|
2830 | # on runtime, and also because if any node in the node list is | |
2831 | # broken, we should stop execution completely. |
|
2831 | # broken, we should stop execution completely. | |
2832 | if result: |
|
2832 | if result: | |
2833 | result.error_before_exec = sys.exc_info()[1] |
|
2833 | result.error_before_exec = sys.exc_info()[1] | |
2834 | self.showtraceback() |
|
2834 | self.showtraceback() | |
2835 | return True |
|
2835 | return True | |
2836 |
|
2836 | |||
2837 | return False |
|
2837 | return False | |
2838 |
|
2838 | |||
2839 | def run_code(self, code_obj, result=None): |
|
2839 | def run_code(self, code_obj, result=None): | |
2840 | """Execute a code object. |
|
2840 | """Execute a code object. | |
2841 |
|
2841 | |||
2842 | When an exception occurs, self.showtraceback() is called to display a |
|
2842 | When an exception occurs, self.showtraceback() is called to display a | |
2843 | traceback. |
|
2843 | traceback. | |
2844 |
|
2844 | |||
2845 | Parameters |
|
2845 | Parameters | |
2846 | ---------- |
|
2846 | ---------- | |
2847 | code_obj : code object |
|
2847 | code_obj : code object | |
2848 | A compiled code object, to be executed |
|
2848 | A compiled code object, to be executed | |
2849 | result : ExecutionResult, optional |
|
2849 | result : ExecutionResult, optional | |
2850 | An object to store exceptions that occur during execution. |
|
2850 | An object to store exceptions that occur during execution. | |
2851 |
|
2851 | |||
2852 | Returns |
|
2852 | Returns | |
2853 | ------- |
|
2853 | ------- | |
2854 | False : successful execution. |
|
2854 | False : successful execution. | |
2855 | True : an error occurred. |
|
2855 | True : an error occurred. | |
2856 | """ |
|
2856 | """ | |
2857 | # Set our own excepthook in case the user code tries to call it |
|
2857 | # Set our own excepthook in case the user code tries to call it | |
2858 | # directly, so that the IPython crash handler doesn't get triggered |
|
2858 | # directly, so that the IPython crash handler doesn't get triggered | |
2859 | old_excepthook, sys.excepthook = sys.excepthook, self.excepthook |
|
2859 | old_excepthook, sys.excepthook = sys.excepthook, self.excepthook | |
2860 |
|
2860 | |||
2861 | # we save the original sys.excepthook in the instance, in case config |
|
2861 | # we save the original sys.excepthook in the instance, in case config | |
2862 | # code (such as magics) needs access to it. |
|
2862 | # code (such as magics) needs access to it. | |
2863 | self.sys_excepthook = old_excepthook |
|
2863 | self.sys_excepthook = old_excepthook | |
2864 | outflag = 1 # happens in more places, so it's easier as default |
|
2864 | outflag = 1 # happens in more places, so it's easier as default | |
2865 | try: |
|
2865 | try: | |
2866 | try: |
|
2866 | try: | |
2867 | self.hooks.pre_run_code_hook() |
|
2867 | self.hooks.pre_run_code_hook() | |
2868 | #rprint('Running code', repr(code_obj)) # dbg |
|
2868 | #rprint('Running code', repr(code_obj)) # dbg | |
2869 | exec(code_obj, self.user_global_ns, self.user_ns) |
|
2869 | exec(code_obj, self.user_global_ns, self.user_ns) | |
2870 | finally: |
|
2870 | finally: | |
2871 | # Reset our crash handler in place |
|
2871 | # Reset our crash handler in place | |
2872 | sys.excepthook = old_excepthook |
|
2872 | sys.excepthook = old_excepthook | |
2873 | except SystemExit as e: |
|
2873 | except SystemExit as e: | |
2874 | if result is not None: |
|
2874 | if result is not None: | |
2875 | result.error_in_exec = e |
|
2875 | result.error_in_exec = e | |
2876 | self.showtraceback(exception_only=True) |
|
2876 | self.showtraceback(exception_only=True) | |
2877 | warn("To exit: use 'exit', 'quit', or Ctrl-D.", level=1) |
|
2877 | warn("To exit: use 'exit', 'quit', or Ctrl-D.", level=1) | |
2878 | except self.custom_exceptions: |
|
2878 | except self.custom_exceptions: | |
2879 | etype, value, tb = sys.exc_info() |
|
2879 | etype, value, tb = sys.exc_info() | |
2880 | if result is not None: |
|
2880 | if result is not None: | |
2881 | result.error_in_exec = value |
|
2881 | result.error_in_exec = value | |
2882 | self.CustomTB(etype, value, tb) |
|
2882 | self.CustomTB(etype, value, tb) | |
2883 | except: |
|
2883 | except: | |
2884 | if result is not None: |
|
2884 | if result is not None: | |
2885 | result.error_in_exec = sys.exc_info()[1] |
|
2885 | result.error_in_exec = sys.exc_info()[1] | |
2886 | self.showtraceback() |
|
2886 | self.showtraceback() | |
2887 | else: |
|
2887 | else: | |
2888 | outflag = 0 |
|
2888 | outflag = 0 | |
2889 | return outflag |
|
2889 | return outflag | |
2890 |
|
2890 | |||
2891 | # For backwards compatibility |
|
2891 | # For backwards compatibility | |
2892 | runcode = run_code |
|
2892 | runcode = run_code | |
2893 |
|
2893 | |||
2894 | #------------------------------------------------------------------------- |
|
2894 | #------------------------------------------------------------------------- | |
2895 | # Things related to GUI support and pylab |
|
2895 | # Things related to GUI support and pylab | |
2896 | #------------------------------------------------------------------------- |
|
2896 | #------------------------------------------------------------------------- | |
2897 |
|
2897 | |||
2898 | def enable_gui(self, gui=None): |
|
2898 | def enable_gui(self, gui=None): | |
2899 | raise NotImplementedError('Implement enable_gui in a subclass') |
|
2899 | raise NotImplementedError('Implement enable_gui in a subclass') | |
2900 |
|
2900 | |||
2901 | def enable_matplotlib(self, gui=None): |
|
2901 | def enable_matplotlib(self, gui=None): | |
2902 | """Enable interactive matplotlib and inline figure support. |
|
2902 | """Enable interactive matplotlib and inline figure support. | |
2903 |
|
2903 | |||
2904 | This takes the following steps: |
|
2904 | This takes the following steps: | |
2905 |
|
2905 | |||
2906 | 1. select the appropriate eventloop and matplotlib backend |
|
2906 | 1. select the appropriate eventloop and matplotlib backend | |
2907 | 2. set up matplotlib for interactive use with that backend |
|
2907 | 2. set up matplotlib for interactive use with that backend | |
2908 | 3. configure formatters for inline figure display |
|
2908 | 3. configure formatters for inline figure display | |
2909 | 4. enable the selected gui eventloop |
|
2909 | 4. enable the selected gui eventloop | |
2910 |
|
2910 | |||
2911 | Parameters |
|
2911 | Parameters | |
2912 | ---------- |
|
2912 | ---------- | |
2913 | gui : optional, string |
|
2913 | gui : optional, string | |
2914 | If given, dictates the choice of matplotlib GUI backend to use |
|
2914 | If given, dictates the choice of matplotlib GUI backend to use | |
2915 | (should be one of IPython's supported backends, 'qt', 'osx', 'tk', |
|
2915 | (should be one of IPython's supported backends, 'qt', 'osx', 'tk', | |
2916 | 'gtk', 'wx' or 'inline'), otherwise we use the default chosen by |
|
2916 | 'gtk', 'wx' or 'inline'), otherwise we use the default chosen by | |
2917 | matplotlib (as dictated by the matplotlib build-time options plus the |
|
2917 | matplotlib (as dictated by the matplotlib build-time options plus the | |
2918 | user's matplotlibrc configuration file). Note that not all backends |
|
2918 | user's matplotlibrc configuration file). Note that not all backends | |
2919 | make sense in all contexts, for example a terminal ipython can't |
|
2919 | make sense in all contexts, for example a terminal ipython can't | |
2920 | display figures inline. |
|
2920 | display figures inline. | |
2921 | """ |
|
2921 | """ | |
2922 | from IPython.core import pylabtools as pt |
|
2922 | from IPython.core import pylabtools as pt | |
2923 | gui, backend = pt.find_gui_and_backend(gui, self.pylab_gui_select) |
|
2923 | gui, backend = pt.find_gui_and_backend(gui, self.pylab_gui_select) | |
2924 |
|
2924 | |||
2925 | if gui != 'inline': |
|
2925 | if gui != 'inline': | |
2926 | # If we have our first gui selection, store it |
|
2926 | # If we have our first gui selection, store it | |
2927 | if self.pylab_gui_select is None: |
|
2927 | if self.pylab_gui_select is None: | |
2928 | self.pylab_gui_select = gui |
|
2928 | self.pylab_gui_select = gui | |
2929 | # Otherwise if they are different |
|
2929 | # Otherwise if they are different | |
2930 | elif gui != self.pylab_gui_select: |
|
2930 | elif gui != self.pylab_gui_select: | |
2931 | print ('Warning: Cannot change to a different GUI toolkit: %s.' |
|
2931 | print ('Warning: Cannot change to a different GUI toolkit: %s.' | |
2932 | ' Using %s instead.' % (gui, self.pylab_gui_select)) |
|
2932 | ' Using %s instead.' % (gui, self.pylab_gui_select)) | |
2933 | gui, backend = pt.find_gui_and_backend(self.pylab_gui_select) |
|
2933 | gui, backend = pt.find_gui_and_backend(self.pylab_gui_select) | |
2934 |
|
2934 | |||
2935 | pt.activate_matplotlib(backend) |
|
2935 | pt.activate_matplotlib(backend) | |
2936 | pt.configure_inline_support(self, backend) |
|
2936 | pt.configure_inline_support(self, backend) | |
2937 |
|
2937 | |||
2938 | # Now we must activate the gui pylab wants to use, and fix %run to take |
|
2938 | # Now we must activate the gui pylab wants to use, and fix %run to take | |
2939 | # plot updates into account |
|
2939 | # plot updates into account | |
2940 | self.enable_gui(gui) |
|
2940 | self.enable_gui(gui) | |
2941 | self.magics_manager.registry['ExecutionMagics'].default_runner = \ |
|
2941 | self.magics_manager.registry['ExecutionMagics'].default_runner = \ | |
2942 | pt.mpl_runner(self.safe_execfile) |
|
2942 | pt.mpl_runner(self.safe_execfile) | |
2943 |
|
2943 | |||
2944 | return gui, backend |
|
2944 | return gui, backend | |
2945 |
|
2945 | |||
2946 | def enable_pylab(self, gui=None, import_all=True, welcome_message=False): |
|
2946 | def enable_pylab(self, gui=None, import_all=True, welcome_message=False): | |
2947 | """Activate pylab support at runtime. |
|
2947 | """Activate pylab support at runtime. | |
2948 |
|
2948 | |||
2949 | This turns on support for matplotlib, preloads into the interactive |
|
2949 | This turns on support for matplotlib, preloads into the interactive | |
2950 | namespace all of numpy and pylab, and configures IPython to correctly |
|
2950 | namespace all of numpy and pylab, and configures IPython to correctly | |
2951 | interact with the GUI event loop. The GUI backend to be used can be |
|
2951 | interact with the GUI event loop. The GUI backend to be used can be | |
2952 | optionally selected with the optional ``gui`` argument. |
|
2952 | optionally selected with the optional ``gui`` argument. | |
2953 |
|
2953 | |||
2954 | This method only adds preloading the namespace to InteractiveShell.enable_matplotlib. |
|
2954 | This method only adds preloading the namespace to InteractiveShell.enable_matplotlib. | |
2955 |
|
2955 | |||
2956 | Parameters |
|
2956 | Parameters | |
2957 | ---------- |
|
2957 | ---------- | |
2958 | gui : optional, string |
|
2958 | gui : optional, string | |
2959 | If given, dictates the choice of matplotlib GUI backend to use |
|
2959 | If given, dictates the choice of matplotlib GUI backend to use | |
2960 | (should be one of IPython's supported backends, 'qt', 'osx', 'tk', |
|
2960 | (should be one of IPython's supported backends, 'qt', 'osx', 'tk', | |
2961 | 'gtk', 'wx' or 'inline'), otherwise we use the default chosen by |
|
2961 | 'gtk', 'wx' or 'inline'), otherwise we use the default chosen by | |
2962 | matplotlib (as dictated by the matplotlib build-time options plus the |
|
2962 | matplotlib (as dictated by the matplotlib build-time options plus the | |
2963 | user's matplotlibrc configuration file). Note that not all backends |
|
2963 | user's matplotlibrc configuration file). Note that not all backends | |
2964 | make sense in all contexts, for example a terminal ipython can't |
|
2964 | make sense in all contexts, for example a terminal ipython can't | |
2965 | display figures inline. |
|
2965 | display figures inline. | |
2966 | import_all : optional, bool, default: True |
|
2966 | import_all : optional, bool, default: True | |
2967 | Whether to do `from numpy import *` and `from pylab import *` |
|
2967 | Whether to do `from numpy import *` and `from pylab import *` | |
2968 | in addition to module imports. |
|
2968 | in addition to module imports. | |
2969 | welcome_message : deprecated |
|
2969 | welcome_message : deprecated | |
2970 | This argument is ignored, no welcome message will be displayed. |
|
2970 | This argument is ignored, no welcome message will be displayed. | |
2971 | """ |
|
2971 | """ | |
2972 | from IPython.core.pylabtools import import_pylab |
|
2972 | from IPython.core.pylabtools import import_pylab | |
2973 |
|
2973 | |||
2974 | gui, backend = self.enable_matplotlib(gui) |
|
2974 | gui, backend = self.enable_matplotlib(gui) | |
2975 |
|
2975 | |||
2976 | # We want to prevent the loading of pylab to pollute the user's |
|
2976 | # We want to prevent the loading of pylab to pollute the user's | |
2977 | # namespace as shown by the %who* magics, so we execute the activation |
|
2977 | # namespace as shown by the %who* magics, so we execute the activation | |
2978 | # code in an empty namespace, and we update *both* user_ns and |
|
2978 | # code in an empty namespace, and we update *both* user_ns and | |
2979 | # user_ns_hidden with this information. |
|
2979 | # user_ns_hidden with this information. | |
2980 | ns = {} |
|
2980 | ns = {} | |
2981 | import_pylab(ns, import_all) |
|
2981 | import_pylab(ns, import_all) | |
2982 | # warn about clobbered names |
|
2982 | # warn about clobbered names | |
2983 | ignored = {"__builtins__"} |
|
2983 | ignored = {"__builtins__"} | |
2984 | both = set(ns).intersection(self.user_ns).difference(ignored) |
|
2984 | both = set(ns).intersection(self.user_ns).difference(ignored) | |
2985 | clobbered = [ name for name in both if self.user_ns[name] is not ns[name] ] |
|
2985 | clobbered = [ name for name in both if self.user_ns[name] is not ns[name] ] | |
2986 | self.user_ns.update(ns) |
|
2986 | self.user_ns.update(ns) | |
2987 | self.user_ns_hidden.update(ns) |
|
2987 | self.user_ns_hidden.update(ns) | |
2988 | return gui, backend, clobbered |
|
2988 | return gui, backend, clobbered | |
2989 |
|
2989 | |||
2990 | #------------------------------------------------------------------------- |
|
2990 | #------------------------------------------------------------------------- | |
2991 | # Utilities |
|
2991 | # Utilities | |
2992 | #------------------------------------------------------------------------- |
|
2992 | #------------------------------------------------------------------------- | |
2993 |
|
2993 | |||
2994 | def var_expand(self, cmd, depth=0, formatter=DollarFormatter()): |
|
2994 | def var_expand(self, cmd, depth=0, formatter=DollarFormatter()): | |
2995 | """Expand python variables in a string. |
|
2995 | """Expand python variables in a string. | |
2996 |
|
2996 | |||
2997 | The depth argument indicates how many frames above the caller should |
|
2997 | The depth argument indicates how many frames above the caller should | |
2998 | be walked to look for the local namespace where to expand variables. |
|
2998 | be walked to look for the local namespace where to expand variables. | |
2999 |
|
2999 | |||
3000 | The global namespace for expansion is always the user's interactive |
|
3000 | The global namespace for expansion is always the user's interactive | |
3001 | namespace. |
|
3001 | namespace. | |
3002 | """ |
|
3002 | """ | |
3003 | ns = self.user_ns.copy() |
|
3003 | ns = self.user_ns.copy() | |
3004 | try: |
|
3004 | try: | |
3005 | frame = sys._getframe(depth+1) |
|
3005 | frame = sys._getframe(depth+1) | |
3006 | except ValueError: |
|
3006 | except ValueError: | |
3007 | # This is thrown if there aren't that many frames on the stack, |
|
3007 | # This is thrown if there aren't that many frames on the stack, | |
3008 | # e.g. if a script called run_line_magic() directly. |
|
3008 | # e.g. if a script called run_line_magic() directly. | |
3009 | pass |
|
3009 | pass | |
3010 | else: |
|
3010 | else: | |
3011 | ns.update(frame.f_locals) |
|
3011 | ns.update(frame.f_locals) | |
3012 |
|
3012 | |||
3013 | try: |
|
3013 | try: | |
3014 | # We have to use .vformat() here, because 'self' is a valid and common |
|
3014 | # We have to use .vformat() here, because 'self' is a valid and common | |
3015 | # name, and expanding **ns for .format() would make it collide with |
|
3015 | # name, and expanding **ns for .format() would make it collide with | |
3016 | # the 'self' argument of the method. |
|
3016 | # the 'self' argument of the method. | |
3017 | cmd = formatter.vformat(cmd, args=[], kwargs=ns) |
|
3017 | cmd = formatter.vformat(cmd, args=[], kwargs=ns) | |
3018 | except Exception: |
|
3018 | except Exception: | |
3019 | # if formatter couldn't format, just let it go untransformed |
|
3019 | # if formatter couldn't format, just let it go untransformed | |
3020 | pass |
|
3020 | pass | |
3021 | return cmd |
|
3021 | return cmd | |
3022 |
|
3022 | |||
3023 | def mktempfile(self, data=None, prefix='ipython_edit_'): |
|
3023 | def mktempfile(self, data=None, prefix='ipython_edit_'): | |
3024 | """Make a new tempfile and return its filename. |
|
3024 | """Make a new tempfile and return its filename. | |
3025 |
|
3025 | |||
3026 | This makes a call to tempfile.mkstemp (created in a tempfile.mkdtemp), |
|
3026 | This makes a call to tempfile.mkstemp (created in a tempfile.mkdtemp), | |
3027 | but it registers the created filename internally so ipython cleans it up |
|
3027 | but it registers the created filename internally so ipython cleans it up | |
3028 | at exit time. |
|
3028 | at exit time. | |
3029 |
|
3029 | |||
3030 | Optional inputs: |
|
3030 | Optional inputs: | |
3031 |
|
3031 | |||
3032 | - data(None): if data is given, it gets written out to the temp file |
|
3032 | - data(None): if data is given, it gets written out to the temp file | |
3033 | immediately, and the file is closed again.""" |
|
3033 | immediately, and the file is closed again.""" | |
3034 |
|
3034 | |||
3035 | dirname = tempfile.mkdtemp(prefix=prefix) |
|
3035 | dirname = tempfile.mkdtemp(prefix=prefix) | |
3036 | self.tempdirs.append(dirname) |
|
3036 | self.tempdirs.append(dirname) | |
3037 |
|
3037 | |||
3038 | handle, filename = tempfile.mkstemp('.py', prefix, dir=dirname) |
|
3038 | handle, filename = tempfile.mkstemp('.py', prefix, dir=dirname) | |
3039 | os.close(handle) # On Windows, there can only be one open handle on a file |
|
3039 | os.close(handle) # On Windows, there can only be one open handle on a file | |
3040 | self.tempfiles.append(filename) |
|
3040 | self.tempfiles.append(filename) | |
3041 |
|
3041 | |||
3042 | if data: |
|
3042 | if data: | |
3043 | tmp_file = open(filename,'w') |
|
3043 | tmp_file = open(filename,'w') | |
3044 | tmp_file.write(data) |
|
3044 | tmp_file.write(data) | |
3045 | tmp_file.close() |
|
3045 | tmp_file.close() | |
3046 | return filename |
|
3046 | return filename | |
3047 |
|
3047 | |||
3048 | @undoc |
|
3048 | @undoc | |
3049 | def write(self,data): |
|
3049 | def write(self,data): | |
3050 | """DEPRECATED: Write a string to the default output""" |
|
3050 | """DEPRECATED: Write a string to the default output""" | |
3051 | warn('InteractiveShell.write() is deprecated, use sys.stdout instead', |
|
3051 | warn('InteractiveShell.write() is deprecated, use sys.stdout instead', | |
3052 | DeprecationWarning, stacklevel=2) |
|
3052 | DeprecationWarning, stacklevel=2) | |
3053 | sys.stdout.write(data) |
|
3053 | sys.stdout.write(data) | |
3054 |
|
3054 | |||
3055 | @undoc |
|
3055 | @undoc | |
3056 | def write_err(self,data): |
|
3056 | def write_err(self,data): | |
3057 | """DEPRECATED: Write a string to the default error output""" |
|
3057 | """DEPRECATED: Write a string to the default error output""" | |
3058 | warn('InteractiveShell.write_err() is deprecated, use sys.stderr instead', |
|
3058 | warn('InteractiveShell.write_err() is deprecated, use sys.stderr instead', | |
3059 | DeprecationWarning, stacklevel=2) |
|
3059 | DeprecationWarning, stacklevel=2) | |
3060 | sys.stderr.write(data) |
|
3060 | sys.stderr.write(data) | |
3061 |
|
3061 | |||
3062 | def ask_yes_no(self, prompt, default=None, interrupt=None): |
|
3062 | def ask_yes_no(self, prompt, default=None, interrupt=None): | |
3063 | if self.quiet: |
|
3063 | if self.quiet: | |
3064 | return True |
|
3064 | return True | |
3065 | return ask_yes_no(prompt,default,interrupt) |
|
3065 | return ask_yes_no(prompt,default,interrupt) | |
3066 |
|
3066 | |||
3067 | def show_usage(self): |
|
3067 | def show_usage(self): | |
3068 | """Show a usage message""" |
|
3068 | """Show a usage message""" | |
3069 | page.page(IPython.core.usage.interactive_usage) |
|
3069 | page.page(IPython.core.usage.interactive_usage) | |
3070 |
|
3070 | |||
3071 | def extract_input_lines(self, range_str, raw=False): |
|
3071 | def extract_input_lines(self, range_str, raw=False): | |
3072 | """Return as a string a set of input history slices. |
|
3072 | """Return as a string a set of input history slices. | |
3073 |
|
3073 | |||
3074 | Parameters |
|
3074 | Parameters | |
3075 | ---------- |
|
3075 | ---------- | |
3076 | range_str : string |
|
3076 | range_str : string | |
3077 | The set of slices is given as a string, like "~5/6-~4/2 4:8 9", |
|
3077 | The set of slices is given as a string, like "~5/6-~4/2 4:8 9", | |
3078 | since this function is for use by magic functions which get their |
|
3078 | since this function is for use by magic functions which get their | |
3079 | arguments as strings. The number before the / is the session |
|
3079 | arguments as strings. The number before the / is the session | |
3080 | number: ~n goes n back from the current session. |
|
3080 | number: ~n goes n back from the current session. | |
3081 |
|
3081 | |||
3082 | raw : bool, optional |
|
3082 | raw : bool, optional | |
3083 | By default, the processed input is used. If this is true, the raw |
|
3083 | By default, the processed input is used. If this is true, the raw | |
3084 | input history is used instead. |
|
3084 | input history is used instead. | |
3085 |
|
3085 | |||
3086 | Notes |
|
3086 | Notes | |
3087 | ----- |
|
3087 | ----- | |
3088 |
|
3088 | |||
3089 | Slices can be described with two notations: |
|
3089 | Slices can be described with two notations: | |
3090 |
|
3090 | |||
3091 | * ``N:M`` -> standard python form, means including items N...(M-1). |
|
3091 | * ``N:M`` -> standard python form, means including items N...(M-1). | |
3092 | * ``N-M`` -> include items N..M (closed endpoint). |
|
3092 | * ``N-M`` -> include items N..M (closed endpoint). | |
3093 | """ |
|
3093 | """ | |
3094 | lines = self.history_manager.get_range_by_str(range_str, raw=raw) |
|
3094 | lines = self.history_manager.get_range_by_str(range_str, raw=raw) | |
3095 | return "\n".join(x for _, _, x in lines) |
|
3095 | return "\n".join(x for _, _, x in lines) | |
3096 |
|
3096 | |||
3097 | def find_user_code(self, target, raw=True, py_only=False, skip_encoding_cookie=True, search_ns=False): |
|
3097 | def find_user_code(self, target, raw=True, py_only=False, skip_encoding_cookie=True, search_ns=False): | |
3098 | """Get a code string from history, file, url, or a string or macro. |
|
3098 | """Get a code string from history, file, url, or a string or macro. | |
3099 |
|
3099 | |||
3100 | This is mainly used by magic functions. |
|
3100 | This is mainly used by magic functions. | |
3101 |
|
3101 | |||
3102 | Parameters |
|
3102 | Parameters | |
3103 | ---------- |
|
3103 | ---------- | |
3104 |
|
3104 | |||
3105 | target : str |
|
3105 | target : str | |
3106 |
|
3106 | |||
3107 | A string specifying code to retrieve. This will be tried respectively |
|
3107 | A string specifying code to retrieve. This will be tried respectively | |
3108 | as: ranges of input history (see %history for syntax), url, |
|
3108 | as: ranges of input history (see %history for syntax), url, | |
3109 | corresponding .py file, filename, or an expression evaluating to a |
|
3109 | corresponding .py file, filename, or an expression evaluating to a | |
3110 | string or Macro in the user namespace. |
|
3110 | string or Macro in the user namespace. | |
3111 |
|
3111 | |||
3112 | raw : bool |
|
3112 | raw : bool | |
3113 | If true (default), retrieve raw history. Has no effect on the other |
|
3113 | If true (default), retrieve raw history. Has no effect on the other | |
3114 | retrieval mechanisms. |
|
3114 | retrieval mechanisms. | |
3115 |
|
3115 | |||
3116 | py_only : bool (default False) |
|
3116 | py_only : bool (default False) | |
3117 | Only try to fetch python code, do not try alternative methods to decode file |
|
3117 | Only try to fetch python code, do not try alternative methods to decode file | |
3118 | if unicode fails. |
|
3118 | if unicode fails. | |
3119 |
|
3119 | |||
3120 | Returns |
|
3120 | Returns | |
3121 | ------- |
|
3121 | ------- | |
3122 | A string of code. |
|
3122 | A string of code. | |
3123 |
|
3123 | |||
3124 | ValueError is raised if nothing is found, and TypeError if it evaluates |
|
3124 | ValueError is raised if nothing is found, and TypeError if it evaluates | |
3125 | to an object of another type. In each case, .args[0] is a printable |
|
3125 | to an object of another type. In each case, .args[0] is a printable | |
3126 | message. |
|
3126 | message. | |
3127 | """ |
|
3127 | """ | |
3128 | code = self.extract_input_lines(target, raw=raw) # Grab history |
|
3128 | code = self.extract_input_lines(target, raw=raw) # Grab history | |
3129 | if code: |
|
3129 | if code: | |
3130 | return code |
|
3130 | return code | |
3131 | utarget = unquote_filename(target) |
|
|||
3132 | try: |
|
3131 | try: | |
3133 |
if |
|
3132 | if target.startswith(('http://', 'https://')): | |
3134 |
return openpy.read_py_url( |
|
3133 | return openpy.read_py_url(target, skip_encoding_cookie=skip_encoding_cookie) | |
3135 | except UnicodeDecodeError: |
|
3134 | except UnicodeDecodeError: | |
3136 | if not py_only : |
|
3135 | if not py_only : | |
3137 | # Deferred import |
|
3136 | # Deferred import | |
3138 | try: |
|
3137 | try: | |
3139 | from urllib.request import urlopen # Py3 |
|
3138 | from urllib.request import urlopen # Py3 | |
3140 | except ImportError: |
|
3139 | except ImportError: | |
3141 | from urllib import urlopen |
|
3140 | from urllib import urlopen | |
3142 | response = urlopen(target) |
|
3141 | response = urlopen(target) | |
3143 | return response.read().decode('latin1') |
|
3142 | return response.read().decode('latin1') | |
3144 |
raise ValueError(("'%s' seem to be unreadable.") % |
|
3143 | raise ValueError(("'%s' seem to be unreadable.") % target) | |
3145 |
|
3144 | |||
3146 | potential_target = [target] |
|
3145 | potential_target = [target] | |
3147 | try : |
|
3146 | try : | |
3148 | potential_target.insert(0,get_py_filename(target)) |
|
3147 | potential_target.insert(0,get_py_filename(target)) | |
3149 | except IOError: |
|
3148 | except IOError: | |
3150 | pass |
|
3149 | pass | |
3151 |
|
3150 | |||
3152 | for tgt in potential_target : |
|
3151 | for tgt in potential_target : | |
3153 | if os.path.isfile(tgt): # Read file |
|
3152 | if os.path.isfile(tgt): # Read file | |
3154 | try : |
|
3153 | try : | |
3155 | return openpy.read_py_file(tgt, skip_encoding_cookie=skip_encoding_cookie) |
|
3154 | return openpy.read_py_file(tgt, skip_encoding_cookie=skip_encoding_cookie) | |
3156 | except UnicodeDecodeError : |
|
3155 | except UnicodeDecodeError : | |
3157 | if not py_only : |
|
3156 | if not py_only : | |
3158 | with io_open(tgt,'r', encoding='latin1') as f : |
|
3157 | with io_open(tgt,'r', encoding='latin1') as f : | |
3159 | return f.read() |
|
3158 | return f.read() | |
3160 | raise ValueError(("'%s' seem to be unreadable.") % target) |
|
3159 | raise ValueError(("'%s' seem to be unreadable.") % target) | |
3161 | elif os.path.isdir(os.path.expanduser(tgt)): |
|
3160 | elif os.path.isdir(os.path.expanduser(tgt)): | |
3162 | raise ValueError("'%s' is a directory, not a regular file." % target) |
|
3161 | raise ValueError("'%s' is a directory, not a regular file." % target) | |
3163 |
|
3162 | |||
3164 | if search_ns: |
|
3163 | if search_ns: | |
3165 | # Inspect namespace to load object source |
|
3164 | # Inspect namespace to load object source | |
3166 | object_info = self.object_inspect(target, detail_level=1) |
|
3165 | object_info = self.object_inspect(target, detail_level=1) | |
3167 | if object_info['found'] and object_info['source']: |
|
3166 | if object_info['found'] and object_info['source']: | |
3168 | return object_info['source'] |
|
3167 | return object_info['source'] | |
3169 |
|
3168 | |||
3170 | try: # User namespace |
|
3169 | try: # User namespace | |
3171 | codeobj = eval(target, self.user_ns) |
|
3170 | codeobj = eval(target, self.user_ns) | |
3172 | except Exception: |
|
3171 | except Exception: | |
3173 | raise ValueError(("'%s' was not found in history, as a file, url, " |
|
3172 | raise ValueError(("'%s' was not found in history, as a file, url, " | |
3174 | "nor in the user namespace.") % target) |
|
3173 | "nor in the user namespace.") % target) | |
3175 |
|
3174 | |||
3176 | if isinstance(codeobj, string_types): |
|
3175 | if isinstance(codeobj, string_types): | |
3177 | return codeobj |
|
3176 | return codeobj | |
3178 | elif isinstance(codeobj, Macro): |
|
3177 | elif isinstance(codeobj, Macro): | |
3179 | return codeobj.value |
|
3178 | return codeobj.value | |
3180 |
|
3179 | |||
3181 | raise TypeError("%s is neither a string nor a macro." % target, |
|
3180 | raise TypeError("%s is neither a string nor a macro." % target, | |
3182 | codeobj) |
|
3181 | codeobj) | |
3183 |
|
3182 | |||
3184 | #------------------------------------------------------------------------- |
|
3183 | #------------------------------------------------------------------------- | |
3185 | # Things related to IPython exiting |
|
3184 | # Things related to IPython exiting | |
3186 | #------------------------------------------------------------------------- |
|
3185 | #------------------------------------------------------------------------- | |
3187 | def atexit_operations(self): |
|
3186 | def atexit_operations(self): | |
3188 | """This will be executed at the time of exit. |
|
3187 | """This will be executed at the time of exit. | |
3189 |
|
3188 | |||
3190 | Cleanup operations and saving of persistent data that is done |
|
3189 | Cleanup operations and saving of persistent data that is done | |
3191 | unconditionally by IPython should be performed here. |
|
3190 | unconditionally by IPython should be performed here. | |
3192 |
|
3191 | |||
3193 | For things that may depend on startup flags or platform specifics (such |
|
3192 | For things that may depend on startup flags or platform specifics (such | |
3194 | as having readline or not), register a separate atexit function in the |
|
3193 | as having readline or not), register a separate atexit function in the | |
3195 | code that has the appropriate information, rather than trying to |
|
3194 | code that has the appropriate information, rather than trying to | |
3196 | clutter |
|
3195 | clutter | |
3197 | """ |
|
3196 | """ | |
3198 | # Close the history session (this stores the end time and line count) |
|
3197 | # Close the history session (this stores the end time and line count) | |
3199 | # this must be *before* the tempfile cleanup, in case of temporary |
|
3198 | # this must be *before* the tempfile cleanup, in case of temporary | |
3200 | # history db |
|
3199 | # history db | |
3201 | self.history_manager.end_session() |
|
3200 | self.history_manager.end_session() | |
3202 |
|
3201 | |||
3203 | # Cleanup all tempfiles and folders left around |
|
3202 | # Cleanup all tempfiles and folders left around | |
3204 | for tfile in self.tempfiles: |
|
3203 | for tfile in self.tempfiles: | |
3205 | try: |
|
3204 | try: | |
3206 | os.unlink(tfile) |
|
3205 | os.unlink(tfile) | |
3207 | except OSError: |
|
3206 | except OSError: | |
3208 | pass |
|
3207 | pass | |
3209 |
|
3208 | |||
3210 | for tdir in self.tempdirs: |
|
3209 | for tdir in self.tempdirs: | |
3211 | try: |
|
3210 | try: | |
3212 | os.rmdir(tdir) |
|
3211 | os.rmdir(tdir) | |
3213 | except OSError: |
|
3212 | except OSError: | |
3214 | pass |
|
3213 | pass | |
3215 |
|
3214 | |||
3216 | # Clear all user namespaces to release all references cleanly. |
|
3215 | # Clear all user namespaces to release all references cleanly. | |
3217 | self.reset(new_session=False) |
|
3216 | self.reset(new_session=False) | |
3218 |
|
3217 | |||
3219 | # Run user hooks |
|
3218 | # Run user hooks | |
3220 | self.hooks.shutdown_hook() |
|
3219 | self.hooks.shutdown_hook() | |
3221 |
|
3220 | |||
3222 | def cleanup(self): |
|
3221 | def cleanup(self): | |
3223 | self.restore_sys_module_state() |
|
3222 | self.restore_sys_module_state() | |
3224 |
|
3223 | |||
3225 |
|
3224 | |||
3226 | # Overridden in terminal subclass to change prompts |
|
3225 | # Overridden in terminal subclass to change prompts | |
3227 | def switch_doctest_mode(self, mode): |
|
3226 | def switch_doctest_mode(self, mode): | |
3228 | pass |
|
3227 | pass | |
3229 |
|
3228 | |||
3230 |
|
3229 | |||
3231 | class InteractiveShellABC(with_metaclass(abc.ABCMeta, object)): |
|
3230 | class InteractiveShellABC(with_metaclass(abc.ABCMeta, object)): | |
3232 | """An abstract base class for InteractiveShell.""" |
|
3231 | """An abstract base class for InteractiveShell.""" | |
3233 |
|
3232 | |||
3234 | InteractiveShellABC.register(InteractiveShell) |
|
3233 | InteractiveShellABC.register(InteractiveShell) |
@@ -1,599 +1,597 b'' | |||||
1 | """Implementation of basic magic functions.""" |
|
1 | """Implementation of basic magic functions.""" | |
2 |
|
2 | |||
3 | from __future__ import print_function |
|
3 | from __future__ import print_function | |
4 | from __future__ import absolute_import |
|
4 | from __future__ import absolute_import | |
5 |
|
5 | |||
6 | import io |
|
6 | import io | |
7 | import sys |
|
7 | import sys | |
8 | from pprint import pformat |
|
8 | from pprint import pformat | |
9 |
|
9 | |||
10 | from IPython.core import magic_arguments, page |
|
10 | from IPython.core import magic_arguments, page | |
11 | from IPython.core.error import UsageError |
|
11 | from IPython.core.error import UsageError | |
12 | from IPython.core.magic import Magics, magics_class, line_magic, magic_escapes |
|
12 | from IPython.core.magic import Magics, magics_class, line_magic, magic_escapes | |
13 | from IPython.utils.text import format_screen, dedent, indent |
|
13 | from IPython.utils.text import format_screen, dedent, indent | |
14 | from IPython.testing.skipdoctest import skip_doctest |
|
14 | from IPython.testing.skipdoctest import skip_doctest | |
15 | from IPython.utils.ipstruct import Struct |
|
15 | from IPython.utils.ipstruct import Struct | |
16 | from IPython.utils.path import unquote_filename |
|
|||
17 | from IPython.utils.py3compat import unicode_type |
|
16 | from IPython.utils.py3compat import unicode_type | |
18 | from warnings import warn |
|
17 | from warnings import warn | |
19 | from logging import error |
|
18 | from logging import error | |
20 |
|
19 | |||
21 |
|
20 | |||
22 | class MagicsDisplay(object): |
|
21 | class MagicsDisplay(object): | |
23 | def __init__(self, magics_manager): |
|
22 | def __init__(self, magics_manager): | |
24 | self.magics_manager = magics_manager |
|
23 | self.magics_manager = magics_manager | |
25 |
|
24 | |||
26 | def _lsmagic(self): |
|
25 | def _lsmagic(self): | |
27 | """The main implementation of the %lsmagic""" |
|
26 | """The main implementation of the %lsmagic""" | |
28 | mesc = magic_escapes['line'] |
|
27 | mesc = magic_escapes['line'] | |
29 | cesc = magic_escapes['cell'] |
|
28 | cesc = magic_escapes['cell'] | |
30 | mman = self.magics_manager |
|
29 | mman = self.magics_manager | |
31 | magics = mman.lsmagic() |
|
30 | magics = mman.lsmagic() | |
32 | out = ['Available line magics:', |
|
31 | out = ['Available line magics:', | |
33 | mesc + (' '+mesc).join(sorted(magics['line'])), |
|
32 | mesc + (' '+mesc).join(sorted(magics['line'])), | |
34 | '', |
|
33 | '', | |
35 | 'Available cell magics:', |
|
34 | 'Available cell magics:', | |
36 | cesc + (' '+cesc).join(sorted(magics['cell'])), |
|
35 | cesc + (' '+cesc).join(sorted(magics['cell'])), | |
37 | '', |
|
36 | '', | |
38 | mman.auto_status()] |
|
37 | mman.auto_status()] | |
39 | return '\n'.join(out) |
|
38 | return '\n'.join(out) | |
40 |
|
39 | |||
41 | def _repr_pretty_(self, p, cycle): |
|
40 | def _repr_pretty_(self, p, cycle): | |
42 | p.text(self._lsmagic()) |
|
41 | p.text(self._lsmagic()) | |
43 |
|
42 | |||
44 | def __str__(self): |
|
43 | def __str__(self): | |
45 | return self._lsmagic() |
|
44 | return self._lsmagic() | |
46 |
|
45 | |||
47 | def _jsonable(self): |
|
46 | def _jsonable(self): | |
48 | """turn magics dict into jsonable dict of the same structure |
|
47 | """turn magics dict into jsonable dict of the same structure | |
49 |
|
48 | |||
50 | replaces object instances with their class names as strings |
|
49 | replaces object instances with their class names as strings | |
51 | """ |
|
50 | """ | |
52 | magic_dict = {} |
|
51 | magic_dict = {} | |
53 | mman = self.magics_manager |
|
52 | mman = self.magics_manager | |
54 | magics = mman.lsmagic() |
|
53 | magics = mman.lsmagic() | |
55 | for key, subdict in magics.items(): |
|
54 | for key, subdict in magics.items(): | |
56 | d = {} |
|
55 | d = {} | |
57 | magic_dict[key] = d |
|
56 | magic_dict[key] = d | |
58 | for name, obj in subdict.items(): |
|
57 | for name, obj in subdict.items(): | |
59 | try: |
|
58 | try: | |
60 | classname = obj.__self__.__class__.__name__ |
|
59 | classname = obj.__self__.__class__.__name__ | |
61 | except AttributeError: |
|
60 | except AttributeError: | |
62 | classname = 'Other' |
|
61 | classname = 'Other' | |
63 |
|
62 | |||
64 | d[name] = classname |
|
63 | d[name] = classname | |
65 | return magic_dict |
|
64 | return magic_dict | |
66 |
|
65 | |||
67 | def _repr_json_(self): |
|
66 | def _repr_json_(self): | |
68 | return self._jsonable() |
|
67 | return self._jsonable() | |
69 |
|
68 | |||
70 |
|
69 | |||
71 | @magics_class |
|
70 | @magics_class | |
72 | class BasicMagics(Magics): |
|
71 | class BasicMagics(Magics): | |
73 | """Magics that provide central IPython functionality. |
|
72 | """Magics that provide central IPython functionality. | |
74 |
|
73 | |||
75 | These are various magics that don't fit into specific categories but that |
|
74 | These are various magics that don't fit into specific categories but that | |
76 | are all part of the base 'IPython experience'.""" |
|
75 | are all part of the base 'IPython experience'.""" | |
77 |
|
76 | |||
78 | @magic_arguments.magic_arguments() |
|
77 | @magic_arguments.magic_arguments() | |
79 | @magic_arguments.argument( |
|
78 | @magic_arguments.argument( | |
80 | '-l', '--line', action='store_true', |
|
79 | '-l', '--line', action='store_true', | |
81 | help="""Create a line magic alias.""" |
|
80 | help="""Create a line magic alias.""" | |
82 | ) |
|
81 | ) | |
83 | @magic_arguments.argument( |
|
82 | @magic_arguments.argument( | |
84 | '-c', '--cell', action='store_true', |
|
83 | '-c', '--cell', action='store_true', | |
85 | help="""Create a cell magic alias.""" |
|
84 | help="""Create a cell magic alias.""" | |
86 | ) |
|
85 | ) | |
87 | @magic_arguments.argument( |
|
86 | @magic_arguments.argument( | |
88 | 'name', |
|
87 | 'name', | |
89 | help="""Name of the magic to be created.""" |
|
88 | help="""Name of the magic to be created.""" | |
90 | ) |
|
89 | ) | |
91 | @magic_arguments.argument( |
|
90 | @magic_arguments.argument( | |
92 | 'target', |
|
91 | 'target', | |
93 | help="""Name of the existing line or cell magic.""" |
|
92 | help="""Name of the existing line or cell magic.""" | |
94 | ) |
|
93 | ) | |
95 | @line_magic |
|
94 | @line_magic | |
96 | def alias_magic(self, line=''): |
|
95 | def alias_magic(self, line=''): | |
97 | """Create an alias for an existing line or cell magic. |
|
96 | """Create an alias for an existing line or cell magic. | |
98 |
|
97 | |||
99 | Examples |
|
98 | Examples | |
100 | -------- |
|
99 | -------- | |
101 | :: |
|
100 | :: | |
102 |
|
101 | |||
103 | In [1]: %alias_magic t timeit |
|
102 | In [1]: %alias_magic t timeit | |
104 | Created `%t` as an alias for `%timeit`. |
|
103 | Created `%t` as an alias for `%timeit`. | |
105 | Created `%%t` as an alias for `%%timeit`. |
|
104 | Created `%%t` as an alias for `%%timeit`. | |
106 |
|
105 | |||
107 | In [2]: %t -n1 pass |
|
106 | In [2]: %t -n1 pass | |
108 | 1 loops, best of 3: 954 ns per loop |
|
107 | 1 loops, best of 3: 954 ns per loop | |
109 |
|
108 | |||
110 | In [3]: %%t -n1 |
|
109 | In [3]: %%t -n1 | |
111 | ...: pass |
|
110 | ...: pass | |
112 | ...: |
|
111 | ...: | |
113 | 1 loops, best of 3: 954 ns per loop |
|
112 | 1 loops, best of 3: 954 ns per loop | |
114 |
|
113 | |||
115 | In [4]: %alias_magic --cell whereami pwd |
|
114 | In [4]: %alias_magic --cell whereami pwd | |
116 | UsageError: Cell magic function `%%pwd` not found. |
|
115 | UsageError: Cell magic function `%%pwd` not found. | |
117 | In [5]: %alias_magic --line whereami pwd |
|
116 | In [5]: %alias_magic --line whereami pwd | |
118 | Created `%whereami` as an alias for `%pwd`. |
|
117 | Created `%whereami` as an alias for `%pwd`. | |
119 |
|
118 | |||
120 | In [6]: %whereami |
|
119 | In [6]: %whereami | |
121 | Out[6]: u'/home/testuser' |
|
120 | Out[6]: u'/home/testuser' | |
122 | """ |
|
121 | """ | |
123 | args = magic_arguments.parse_argstring(self.alias_magic, line) |
|
122 | args = magic_arguments.parse_argstring(self.alias_magic, line) | |
124 | shell = self.shell |
|
123 | shell = self.shell | |
125 | mman = self.shell.magics_manager |
|
124 | mman = self.shell.magics_manager | |
126 | escs = ''.join(magic_escapes.values()) |
|
125 | escs = ''.join(magic_escapes.values()) | |
127 |
|
126 | |||
128 | target = args.target.lstrip(escs) |
|
127 | target = args.target.lstrip(escs) | |
129 | name = args.name.lstrip(escs) |
|
128 | name = args.name.lstrip(escs) | |
130 |
|
129 | |||
131 | # Find the requested magics. |
|
130 | # Find the requested magics. | |
132 | m_line = shell.find_magic(target, 'line') |
|
131 | m_line = shell.find_magic(target, 'line') | |
133 | m_cell = shell.find_magic(target, 'cell') |
|
132 | m_cell = shell.find_magic(target, 'cell') | |
134 | if args.line and m_line is None: |
|
133 | if args.line and m_line is None: | |
135 | raise UsageError('Line magic function `%s%s` not found.' % |
|
134 | raise UsageError('Line magic function `%s%s` not found.' % | |
136 | (magic_escapes['line'], target)) |
|
135 | (magic_escapes['line'], target)) | |
137 | if args.cell and m_cell is None: |
|
136 | if args.cell and m_cell is None: | |
138 | raise UsageError('Cell magic function `%s%s` not found.' % |
|
137 | raise UsageError('Cell magic function `%s%s` not found.' % | |
139 | (magic_escapes['cell'], target)) |
|
138 | (magic_escapes['cell'], target)) | |
140 |
|
139 | |||
141 | # If --line and --cell are not specified, default to the ones |
|
140 | # If --line and --cell are not specified, default to the ones | |
142 | # that are available. |
|
141 | # that are available. | |
143 | if not args.line and not args.cell: |
|
142 | if not args.line and not args.cell: | |
144 | if not m_line and not m_cell: |
|
143 | if not m_line and not m_cell: | |
145 | raise UsageError( |
|
144 | raise UsageError( | |
146 | 'No line or cell magic with name `%s` found.' % target |
|
145 | 'No line or cell magic with name `%s` found.' % target | |
147 | ) |
|
146 | ) | |
148 | args.line = bool(m_line) |
|
147 | args.line = bool(m_line) | |
149 | args.cell = bool(m_cell) |
|
148 | args.cell = bool(m_cell) | |
150 |
|
149 | |||
151 | if args.line: |
|
150 | if args.line: | |
152 | mman.register_alias(name, target, 'line') |
|
151 | mman.register_alias(name, target, 'line') | |
153 | print('Created `%s%s` as an alias for `%s%s`.' % ( |
|
152 | print('Created `%s%s` as an alias for `%s%s`.' % ( | |
154 | magic_escapes['line'], name, |
|
153 | magic_escapes['line'], name, | |
155 | magic_escapes['line'], target)) |
|
154 | magic_escapes['line'], target)) | |
156 |
|
155 | |||
157 | if args.cell: |
|
156 | if args.cell: | |
158 | mman.register_alias(name, target, 'cell') |
|
157 | mman.register_alias(name, target, 'cell') | |
159 | print('Created `%s%s` as an alias for `%s%s`.' % ( |
|
158 | print('Created `%s%s` as an alias for `%s%s`.' % ( | |
160 | magic_escapes['cell'], name, |
|
159 | magic_escapes['cell'], name, | |
161 | magic_escapes['cell'], target)) |
|
160 | magic_escapes['cell'], target)) | |
162 |
|
161 | |||
163 | @line_magic |
|
162 | @line_magic | |
164 | def lsmagic(self, parameter_s=''): |
|
163 | def lsmagic(self, parameter_s=''): | |
165 | """List currently available magic functions.""" |
|
164 | """List currently available magic functions.""" | |
166 | return MagicsDisplay(self.shell.magics_manager) |
|
165 | return MagicsDisplay(self.shell.magics_manager) | |
167 |
|
166 | |||
168 | def _magic_docs(self, brief=False, rest=False): |
|
167 | def _magic_docs(self, brief=False, rest=False): | |
169 | """Return docstrings from magic functions.""" |
|
168 | """Return docstrings from magic functions.""" | |
170 | mman = self.shell.magics_manager |
|
169 | mman = self.shell.magics_manager | |
171 | docs = mman.lsmagic_docs(brief, missing='No documentation') |
|
170 | docs = mman.lsmagic_docs(brief, missing='No documentation') | |
172 |
|
171 | |||
173 | if rest: |
|
172 | if rest: | |
174 | format_string = '**%s%s**::\n\n%s\n\n' |
|
173 | format_string = '**%s%s**::\n\n%s\n\n' | |
175 | else: |
|
174 | else: | |
176 | format_string = '%s%s:\n%s\n' |
|
175 | format_string = '%s%s:\n%s\n' | |
177 |
|
176 | |||
178 | return ''.join( |
|
177 | return ''.join( | |
179 | [format_string % (magic_escapes['line'], fname, |
|
178 | [format_string % (magic_escapes['line'], fname, | |
180 | indent(dedent(fndoc))) |
|
179 | indent(dedent(fndoc))) | |
181 | for fname, fndoc in sorted(docs['line'].items())] |
|
180 | for fname, fndoc in sorted(docs['line'].items())] | |
182 | + |
|
181 | + | |
183 | [format_string % (magic_escapes['cell'], fname, |
|
182 | [format_string % (magic_escapes['cell'], fname, | |
184 | indent(dedent(fndoc))) |
|
183 | indent(dedent(fndoc))) | |
185 | for fname, fndoc in sorted(docs['cell'].items())] |
|
184 | for fname, fndoc in sorted(docs['cell'].items())] | |
186 | ) |
|
185 | ) | |
187 |
|
186 | |||
188 | @line_magic |
|
187 | @line_magic | |
189 | def magic(self, parameter_s=''): |
|
188 | def magic(self, parameter_s=''): | |
190 | """Print information about the magic function system. |
|
189 | """Print information about the magic function system. | |
191 |
|
190 | |||
192 | Supported formats: -latex, -brief, -rest |
|
191 | Supported formats: -latex, -brief, -rest | |
193 | """ |
|
192 | """ | |
194 |
|
193 | |||
195 | mode = '' |
|
194 | mode = '' | |
196 | try: |
|
195 | try: | |
197 | mode = parameter_s.split()[0][1:] |
|
196 | mode = parameter_s.split()[0][1:] | |
198 | except IndexError: |
|
197 | except IndexError: | |
199 | pass |
|
198 | pass | |
200 |
|
199 | |||
201 | brief = (mode == 'brief') |
|
200 | brief = (mode == 'brief') | |
202 | rest = (mode == 'rest') |
|
201 | rest = (mode == 'rest') | |
203 | magic_docs = self._magic_docs(brief, rest) |
|
202 | magic_docs = self._magic_docs(brief, rest) | |
204 |
|
203 | |||
205 | if mode == 'latex': |
|
204 | if mode == 'latex': | |
206 | print(self.format_latex(magic_docs)) |
|
205 | print(self.format_latex(magic_docs)) | |
207 | return |
|
206 | return | |
208 | else: |
|
207 | else: | |
209 | magic_docs = format_screen(magic_docs) |
|
208 | magic_docs = format_screen(magic_docs) | |
210 |
|
209 | |||
211 | out = [""" |
|
210 | out = [""" | |
212 | IPython's 'magic' functions |
|
211 | IPython's 'magic' functions | |
213 | =========================== |
|
212 | =========================== | |
214 |
|
213 | |||
215 | The magic function system provides a series of functions which allow you to |
|
214 | The magic function system provides a series of functions which allow you to | |
216 | control the behavior of IPython itself, plus a lot of system-type |
|
215 | control the behavior of IPython itself, plus a lot of system-type | |
217 | features. There are two kinds of magics, line-oriented and cell-oriented. |
|
216 | features. There are two kinds of magics, line-oriented and cell-oriented. | |
218 |
|
217 | |||
219 | Line magics are prefixed with the % character and work much like OS |
|
218 | Line magics are prefixed with the % character and work much like OS | |
220 | command-line calls: they get as an argument the rest of the line, where |
|
219 | command-line calls: they get as an argument the rest of the line, where | |
221 | arguments are passed without parentheses or quotes. For example, this will |
|
220 | arguments are passed without parentheses or quotes. For example, this will | |
222 | time the given statement:: |
|
221 | time the given statement:: | |
223 |
|
222 | |||
224 | %timeit range(1000) |
|
223 | %timeit range(1000) | |
225 |
|
224 | |||
226 | Cell magics are prefixed with a double %%, and they are functions that get as |
|
225 | Cell magics are prefixed with a double %%, and they are functions that get as | |
227 | an argument not only the rest of the line, but also the lines below it in a |
|
226 | an argument not only the rest of the line, but also the lines below it in a | |
228 | separate argument. These magics are called with two arguments: the rest of the |
|
227 | separate argument. These magics are called with two arguments: the rest of the | |
229 | call line and the body of the cell, consisting of the lines below the first. |
|
228 | call line and the body of the cell, consisting of the lines below the first. | |
230 | For example:: |
|
229 | For example:: | |
231 |
|
230 | |||
232 | %%timeit x = numpy.random.randn((100, 100)) |
|
231 | %%timeit x = numpy.random.randn((100, 100)) | |
233 | numpy.linalg.svd(x) |
|
232 | numpy.linalg.svd(x) | |
234 |
|
233 | |||
235 | will time the execution of the numpy svd routine, running the assignment of x |
|
234 | will time the execution of the numpy svd routine, running the assignment of x | |
236 | as part of the setup phase, which is not timed. |
|
235 | as part of the setup phase, which is not timed. | |
237 |
|
236 | |||
238 | In a line-oriented client (the terminal or Qt console IPython), starting a new |
|
237 | In a line-oriented client (the terminal or Qt console IPython), starting a new | |
239 | input with %% will automatically enter cell mode, and IPython will continue |
|
238 | input with %% will automatically enter cell mode, and IPython will continue | |
240 | reading input until a blank line is given. In the notebook, simply type the |
|
239 | reading input until a blank line is given. In the notebook, simply type the | |
241 | whole cell as one entity, but keep in mind that the %% escape can only be at |
|
240 | whole cell as one entity, but keep in mind that the %% escape can only be at | |
242 | the very start of the cell. |
|
241 | the very start of the cell. | |
243 |
|
242 | |||
244 | NOTE: If you have 'automagic' enabled (via the command line option or with the |
|
243 | NOTE: If you have 'automagic' enabled (via the command line option or with the | |
245 | %automagic function), you don't need to type in the % explicitly for line |
|
244 | %automagic function), you don't need to type in the % explicitly for line | |
246 | magics; cell magics always require an explicit '%%' escape. By default, |
|
245 | magics; cell magics always require an explicit '%%' escape. By default, | |
247 | IPython ships with automagic on, so you should only rarely need the % escape. |
|
246 | IPython ships with automagic on, so you should only rarely need the % escape. | |
248 |
|
247 | |||
249 | Example: typing '%cd mydir' (without the quotes) changes your working directory |
|
248 | Example: typing '%cd mydir' (without the quotes) changes your working directory | |
250 | to 'mydir', if it exists. |
|
249 | to 'mydir', if it exists. | |
251 |
|
250 | |||
252 | For a list of the available magic functions, use %lsmagic. For a description |
|
251 | For a list of the available magic functions, use %lsmagic. For a description | |
253 | of any of them, type %magic_name?, e.g. '%cd?'. |
|
252 | of any of them, type %magic_name?, e.g. '%cd?'. | |
254 |
|
253 | |||
255 | Currently the magic system has the following functions:""", |
|
254 | Currently the magic system has the following functions:""", | |
256 | magic_docs, |
|
255 | magic_docs, | |
257 | "Summary of magic functions (from %slsmagic):" % magic_escapes['line'], |
|
256 | "Summary of magic functions (from %slsmagic):" % magic_escapes['line'], | |
258 | str(self.lsmagic()), |
|
257 | str(self.lsmagic()), | |
259 | ] |
|
258 | ] | |
260 | page.page('\n'.join(out)) |
|
259 | page.page('\n'.join(out)) | |
261 |
|
260 | |||
262 |
|
261 | |||
263 | @line_magic |
|
262 | @line_magic | |
264 | def page(self, parameter_s=''): |
|
263 | def page(self, parameter_s=''): | |
265 | """Pretty print the object and display it through a pager. |
|
264 | """Pretty print the object and display it through a pager. | |
266 |
|
265 | |||
267 | %page [options] OBJECT |
|
266 | %page [options] OBJECT | |
268 |
|
267 | |||
269 | If no object is given, use _ (last output). |
|
268 | If no object is given, use _ (last output). | |
270 |
|
269 | |||
271 | Options: |
|
270 | Options: | |
272 |
|
271 | |||
273 | -r: page str(object), don't pretty-print it.""" |
|
272 | -r: page str(object), don't pretty-print it.""" | |
274 |
|
273 | |||
275 | # After a function contributed by Olivier Aubert, slightly modified. |
|
274 | # After a function contributed by Olivier Aubert, slightly modified. | |
276 |
|
275 | |||
277 | # Process options/args |
|
276 | # Process options/args | |
278 | opts, args = self.parse_options(parameter_s, 'r') |
|
277 | opts, args = self.parse_options(parameter_s, 'r') | |
279 | raw = 'r' in opts |
|
278 | raw = 'r' in opts | |
280 |
|
279 | |||
281 | oname = args and args or '_' |
|
280 | oname = args and args or '_' | |
282 | info = self.shell._ofind(oname) |
|
281 | info = self.shell._ofind(oname) | |
283 | if info['found']: |
|
282 | if info['found']: | |
284 | txt = (raw and str or pformat)( info['obj'] ) |
|
283 | txt = (raw and str or pformat)( info['obj'] ) | |
285 | page.page(txt) |
|
284 | page.page(txt) | |
286 | else: |
|
285 | else: | |
287 | print('Object `%s` not found' % oname) |
|
286 | print('Object `%s` not found' % oname) | |
288 |
|
287 | |||
289 | @line_magic |
|
288 | @line_magic | |
290 | def profile(self, parameter_s=''): |
|
289 | def profile(self, parameter_s=''): | |
291 | """Print your currently active IPython profile. |
|
290 | """Print your currently active IPython profile. | |
292 |
|
291 | |||
293 | See Also |
|
292 | See Also | |
294 | -------- |
|
293 | -------- | |
295 | prun : run code using the Python profiler |
|
294 | prun : run code using the Python profiler | |
296 | (:meth:`~IPython.core.magics.execution.ExecutionMagics.prun`) |
|
295 | (:meth:`~IPython.core.magics.execution.ExecutionMagics.prun`) | |
297 | """ |
|
296 | """ | |
298 | warn("%profile is now deprecated. Please use get_ipython().profile instead.") |
|
297 | warn("%profile is now deprecated. Please use get_ipython().profile instead.") | |
299 | from IPython.core.application import BaseIPythonApplication |
|
298 | from IPython.core.application import BaseIPythonApplication | |
300 | if BaseIPythonApplication.initialized(): |
|
299 | if BaseIPythonApplication.initialized(): | |
301 | print(BaseIPythonApplication.instance().profile) |
|
300 | print(BaseIPythonApplication.instance().profile) | |
302 | else: |
|
301 | else: | |
303 | error("profile is an application-level value, but you don't appear to be in an IPython application") |
|
302 | error("profile is an application-level value, but you don't appear to be in an IPython application") | |
304 |
|
303 | |||
305 | @line_magic |
|
304 | @line_magic | |
306 | def pprint(self, parameter_s=''): |
|
305 | def pprint(self, parameter_s=''): | |
307 | """Toggle pretty printing on/off.""" |
|
306 | """Toggle pretty printing on/off.""" | |
308 | ptformatter = self.shell.display_formatter.formatters['text/plain'] |
|
307 | ptformatter = self.shell.display_formatter.formatters['text/plain'] | |
309 | ptformatter.pprint = bool(1 - ptformatter.pprint) |
|
308 | ptformatter.pprint = bool(1 - ptformatter.pprint) | |
310 | print('Pretty printing has been turned', |
|
309 | print('Pretty printing has been turned', | |
311 | ['OFF','ON'][ptformatter.pprint]) |
|
310 | ['OFF','ON'][ptformatter.pprint]) | |
312 |
|
311 | |||
313 | @line_magic |
|
312 | @line_magic | |
314 | def colors(self, parameter_s=''): |
|
313 | def colors(self, parameter_s=''): | |
315 | """Switch color scheme for prompts, info system and exception handlers. |
|
314 | """Switch color scheme for prompts, info system and exception handlers. | |
316 |
|
315 | |||
317 | Currently implemented schemes: NoColor, Linux, LightBG. |
|
316 | Currently implemented schemes: NoColor, Linux, LightBG. | |
318 |
|
317 | |||
319 | Color scheme names are not case-sensitive. |
|
318 | Color scheme names are not case-sensitive. | |
320 |
|
319 | |||
321 | Examples |
|
320 | Examples | |
322 | -------- |
|
321 | -------- | |
323 | To get a plain black and white terminal:: |
|
322 | To get a plain black and white terminal:: | |
324 |
|
323 | |||
325 | %colors nocolor |
|
324 | %colors nocolor | |
326 | """ |
|
325 | """ | |
327 | def color_switch_err(name): |
|
326 | def color_switch_err(name): | |
328 | warn('Error changing %s color schemes.\n%s' % |
|
327 | warn('Error changing %s color schemes.\n%s' % | |
329 | (name, sys.exc_info()[1])) |
|
328 | (name, sys.exc_info()[1])) | |
330 |
|
329 | |||
331 |
|
330 | |||
332 | new_scheme = parameter_s.strip() |
|
331 | new_scheme = parameter_s.strip() | |
333 | if not new_scheme: |
|
332 | if not new_scheme: | |
334 | raise UsageError( |
|
333 | raise UsageError( | |
335 | "%colors: you must specify a color scheme. See '%colors?'") |
|
334 | "%colors: you must specify a color scheme. See '%colors?'") | |
336 | # local shortcut |
|
335 | # local shortcut | |
337 | shell = self.shell |
|
336 | shell = self.shell | |
338 |
|
337 | |||
339 |
|
338 | |||
340 |
|
339 | |||
341 | if not shell.colors_force: |
|
340 | if not shell.colors_force: | |
342 | if sys.platform in {'win32', 'cli'}: |
|
341 | if sys.platform in {'win32', 'cli'}: | |
343 | import IPython.utils.rlineimpl as readline |
|
342 | import IPython.utils.rlineimpl as readline | |
344 | if not readline.have_readline: |
|
343 | if not readline.have_readline: | |
345 | msg = """\ |
|
344 | msg = """\ | |
346 | Proper color support under MS Windows requires the pyreadline library. |
|
345 | Proper color support under MS Windows requires the pyreadline library. | |
347 | You can find it at: |
|
346 | You can find it at: | |
348 | http://ipython.org/pyreadline.html |
|
347 | http://ipython.org/pyreadline.html | |
349 |
|
348 | |||
350 | Defaulting color scheme to 'NoColor'""" |
|
349 | Defaulting color scheme to 'NoColor'""" | |
351 | new_scheme = 'NoColor' |
|
350 | new_scheme = 'NoColor' | |
352 | warn(msg) |
|
351 | warn(msg) | |
353 |
|
352 | |||
354 | elif not shell.has_readline: |
|
353 | elif not shell.has_readline: | |
355 | # Coloured prompts get messed up without readline |
|
354 | # Coloured prompts get messed up without readline | |
356 | # Will remove this check after switching to prompt_toolkit |
|
355 | # Will remove this check after switching to prompt_toolkit | |
357 | new_scheme = 'NoColor' |
|
356 | new_scheme = 'NoColor' | |
358 |
|
357 | |||
359 | # Set exception colors |
|
358 | # Set exception colors | |
360 | try: |
|
359 | try: | |
361 | shell.InteractiveTB.set_colors(scheme = new_scheme) |
|
360 | shell.InteractiveTB.set_colors(scheme = new_scheme) | |
362 | shell.SyntaxTB.set_colors(scheme = new_scheme) |
|
361 | shell.SyntaxTB.set_colors(scheme = new_scheme) | |
363 | except: |
|
362 | except: | |
364 | color_switch_err('exception') |
|
363 | color_switch_err('exception') | |
365 |
|
364 | |||
366 | # Set info (for 'object?') colors |
|
365 | # Set info (for 'object?') colors | |
367 | if shell.color_info: |
|
366 | if shell.color_info: | |
368 | try: |
|
367 | try: | |
369 | shell.inspector.set_active_scheme(new_scheme) |
|
368 | shell.inspector.set_active_scheme(new_scheme) | |
370 | except: |
|
369 | except: | |
371 | color_switch_err('object inspector') |
|
370 | color_switch_err('object inspector') | |
372 | else: |
|
371 | else: | |
373 | shell.inspector.set_active_scheme('NoColor') |
|
372 | shell.inspector.set_active_scheme('NoColor') | |
374 |
|
373 | |||
375 | @line_magic |
|
374 | @line_magic | |
376 | def xmode(self, parameter_s=''): |
|
375 | def xmode(self, parameter_s=''): | |
377 | """Switch modes for the exception handlers. |
|
376 | """Switch modes for the exception handlers. | |
378 |
|
377 | |||
379 | Valid modes: Plain, Context and Verbose. |
|
378 | Valid modes: Plain, Context and Verbose. | |
380 |
|
379 | |||
381 | If called without arguments, acts as a toggle.""" |
|
380 | If called without arguments, acts as a toggle.""" | |
382 |
|
381 | |||
383 | def xmode_switch_err(name): |
|
382 | def xmode_switch_err(name): | |
384 | warn('Error changing %s exception modes.\n%s' % |
|
383 | warn('Error changing %s exception modes.\n%s' % | |
385 | (name,sys.exc_info()[1])) |
|
384 | (name,sys.exc_info()[1])) | |
386 |
|
385 | |||
387 | shell = self.shell |
|
386 | shell = self.shell | |
388 | new_mode = parameter_s.strip().capitalize() |
|
387 | new_mode = parameter_s.strip().capitalize() | |
389 | try: |
|
388 | try: | |
390 | shell.InteractiveTB.set_mode(mode=new_mode) |
|
389 | shell.InteractiveTB.set_mode(mode=new_mode) | |
391 | print('Exception reporting mode:',shell.InteractiveTB.mode) |
|
390 | print('Exception reporting mode:',shell.InteractiveTB.mode) | |
392 | except: |
|
391 | except: | |
393 | xmode_switch_err('user') |
|
392 | xmode_switch_err('user') | |
394 |
|
393 | |||
395 | @line_magic |
|
394 | @line_magic | |
396 | def quickref(self,arg): |
|
395 | def quickref(self,arg): | |
397 | """ Show a quick reference sheet """ |
|
396 | """ Show a quick reference sheet """ | |
398 | from IPython.core.usage import quick_reference |
|
397 | from IPython.core.usage import quick_reference | |
399 | qr = quick_reference + self._magic_docs(brief=True) |
|
398 | qr = quick_reference + self._magic_docs(brief=True) | |
400 | page.page(qr) |
|
399 | page.page(qr) | |
401 |
|
400 | |||
402 | @line_magic |
|
401 | @line_magic | |
403 | def doctest_mode(self, parameter_s=''): |
|
402 | def doctest_mode(self, parameter_s=''): | |
404 | """Toggle doctest mode on and off. |
|
403 | """Toggle doctest mode on and off. | |
405 |
|
404 | |||
406 | This mode is intended to make IPython behave as much as possible like a |
|
405 | This mode is intended to make IPython behave as much as possible like a | |
407 | plain Python shell, from the perspective of how its prompts, exceptions |
|
406 | plain Python shell, from the perspective of how its prompts, exceptions | |
408 | and output look. This makes it easy to copy and paste parts of a |
|
407 | and output look. This makes it easy to copy and paste parts of a | |
409 | session into doctests. It does so by: |
|
408 | session into doctests. It does so by: | |
410 |
|
409 | |||
411 | - Changing the prompts to the classic ``>>>`` ones. |
|
410 | - Changing the prompts to the classic ``>>>`` ones. | |
412 | - Changing the exception reporting mode to 'Plain'. |
|
411 | - Changing the exception reporting mode to 'Plain'. | |
413 | - Disabling pretty-printing of output. |
|
412 | - Disabling pretty-printing of output. | |
414 |
|
413 | |||
415 | Note that IPython also supports the pasting of code snippets that have |
|
414 | Note that IPython also supports the pasting of code snippets that have | |
416 | leading '>>>' and '...' prompts in them. This means that you can paste |
|
415 | leading '>>>' and '...' prompts in them. This means that you can paste | |
417 | doctests from files or docstrings (even if they have leading |
|
416 | doctests from files or docstrings (even if they have leading | |
418 | whitespace), and the code will execute correctly. You can then use |
|
417 | whitespace), and the code will execute correctly. You can then use | |
419 | '%history -t' to see the translated history; this will give you the |
|
418 | '%history -t' to see the translated history; this will give you the | |
420 | input after removal of all the leading prompts and whitespace, which |
|
419 | input after removal of all the leading prompts and whitespace, which | |
421 | can be pasted back into an editor. |
|
420 | can be pasted back into an editor. | |
422 |
|
421 | |||
423 | With these features, you can switch into this mode easily whenever you |
|
422 | With these features, you can switch into this mode easily whenever you | |
424 | need to do testing and changes to doctests, without having to leave |
|
423 | need to do testing and changes to doctests, without having to leave | |
425 | your existing IPython session. |
|
424 | your existing IPython session. | |
426 | """ |
|
425 | """ | |
427 |
|
426 | |||
428 | # Shorthands |
|
427 | # Shorthands | |
429 | shell = self.shell |
|
428 | shell = self.shell | |
430 | meta = shell.meta |
|
429 | meta = shell.meta | |
431 | disp_formatter = self.shell.display_formatter |
|
430 | disp_formatter = self.shell.display_formatter | |
432 | ptformatter = disp_formatter.formatters['text/plain'] |
|
431 | ptformatter = disp_formatter.formatters['text/plain'] | |
433 | # dstore is a data store kept in the instance metadata bag to track any |
|
432 | # dstore is a data store kept in the instance metadata bag to track any | |
434 | # changes we make, so we can undo them later. |
|
433 | # changes we make, so we can undo them later. | |
435 | dstore = meta.setdefault('doctest_mode',Struct()) |
|
434 | dstore = meta.setdefault('doctest_mode',Struct()) | |
436 | save_dstore = dstore.setdefault |
|
435 | save_dstore = dstore.setdefault | |
437 |
|
436 | |||
438 | # save a few values we'll need to recover later |
|
437 | # save a few values we'll need to recover later | |
439 | mode = save_dstore('mode',False) |
|
438 | mode = save_dstore('mode',False) | |
440 | save_dstore('rc_pprint',ptformatter.pprint) |
|
439 | save_dstore('rc_pprint',ptformatter.pprint) | |
441 | save_dstore('xmode',shell.InteractiveTB.mode) |
|
440 | save_dstore('xmode',shell.InteractiveTB.mode) | |
442 | save_dstore('rc_separate_out',shell.separate_out) |
|
441 | save_dstore('rc_separate_out',shell.separate_out) | |
443 | save_dstore('rc_separate_out2',shell.separate_out2) |
|
442 | save_dstore('rc_separate_out2',shell.separate_out2) | |
444 | save_dstore('rc_separate_in',shell.separate_in) |
|
443 | save_dstore('rc_separate_in',shell.separate_in) | |
445 | save_dstore('rc_active_types',disp_formatter.active_types) |
|
444 | save_dstore('rc_active_types',disp_formatter.active_types) | |
446 |
|
445 | |||
447 | if not mode: |
|
446 | if not mode: | |
448 | # turn on |
|
447 | # turn on | |
449 |
|
448 | |||
450 | # Prompt separators like plain python |
|
449 | # Prompt separators like plain python | |
451 | shell.separate_in = '' |
|
450 | shell.separate_in = '' | |
452 | shell.separate_out = '' |
|
451 | shell.separate_out = '' | |
453 | shell.separate_out2 = '' |
|
452 | shell.separate_out2 = '' | |
454 |
|
453 | |||
455 |
|
454 | |||
456 | ptformatter.pprint = False |
|
455 | ptformatter.pprint = False | |
457 | disp_formatter.active_types = ['text/plain'] |
|
456 | disp_formatter.active_types = ['text/plain'] | |
458 |
|
457 | |||
459 | shell.magic('xmode Plain') |
|
458 | shell.magic('xmode Plain') | |
460 | else: |
|
459 | else: | |
461 | # turn off |
|
460 | # turn off | |
462 | shell.separate_in = dstore.rc_separate_in |
|
461 | shell.separate_in = dstore.rc_separate_in | |
463 |
|
462 | |||
464 | shell.separate_out = dstore.rc_separate_out |
|
463 | shell.separate_out = dstore.rc_separate_out | |
465 | shell.separate_out2 = dstore.rc_separate_out2 |
|
464 | shell.separate_out2 = dstore.rc_separate_out2 | |
466 |
|
465 | |||
467 | ptformatter.pprint = dstore.rc_pprint |
|
466 | ptformatter.pprint = dstore.rc_pprint | |
468 | disp_formatter.active_types = dstore.rc_active_types |
|
467 | disp_formatter.active_types = dstore.rc_active_types | |
469 |
|
468 | |||
470 | shell.magic('xmode ' + dstore.xmode) |
|
469 | shell.magic('xmode ' + dstore.xmode) | |
471 |
|
470 | |||
472 | # mode here is the state before we switch; switch_doctest_mode takes |
|
471 | # mode here is the state before we switch; switch_doctest_mode takes | |
473 | # the mode we're switching to. |
|
472 | # the mode we're switching to. | |
474 | shell.switch_doctest_mode(not mode) |
|
473 | shell.switch_doctest_mode(not mode) | |
475 |
|
474 | |||
476 | # Store new mode and inform |
|
475 | # Store new mode and inform | |
477 | dstore.mode = bool(not mode) |
|
476 | dstore.mode = bool(not mode) | |
478 | mode_label = ['OFF','ON'][dstore.mode] |
|
477 | mode_label = ['OFF','ON'][dstore.mode] | |
479 | print('Doctest mode is:', mode_label) |
|
478 | print('Doctest mode is:', mode_label) | |
480 |
|
479 | |||
481 | @line_magic |
|
480 | @line_magic | |
482 | def gui(self, parameter_s=''): |
|
481 | def gui(self, parameter_s=''): | |
483 | """Enable or disable IPython GUI event loop integration. |
|
482 | """Enable or disable IPython GUI event loop integration. | |
484 |
|
483 | |||
485 | %gui [GUINAME] |
|
484 | %gui [GUINAME] | |
486 |
|
485 | |||
487 | This magic replaces IPython's threaded shells that were activated |
|
486 | This magic replaces IPython's threaded shells that were activated | |
488 | using the (pylab/wthread/etc.) command line flags. GUI toolkits |
|
487 | using the (pylab/wthread/etc.) command line flags. GUI toolkits | |
489 | can now be enabled at runtime and keyboard |
|
488 | can now be enabled at runtime and keyboard | |
490 | interrupts should work without any problems. The following toolkits |
|
489 | interrupts should work without any problems. The following toolkits | |
491 | are supported: wxPython, PyQt4, PyGTK, Tk and Cocoa (OSX):: |
|
490 | are supported: wxPython, PyQt4, PyGTK, Tk and Cocoa (OSX):: | |
492 |
|
491 | |||
493 | %gui wx # enable wxPython event loop integration |
|
492 | %gui wx # enable wxPython event loop integration | |
494 | %gui qt4|qt # enable PyQt4 event loop integration |
|
493 | %gui qt4|qt # enable PyQt4 event loop integration | |
495 | %gui qt5 # enable PyQt5 event loop integration |
|
494 | %gui qt5 # enable PyQt5 event loop integration | |
496 | %gui gtk # enable PyGTK event loop integration |
|
495 | %gui gtk # enable PyGTK event loop integration | |
497 | %gui gtk3 # enable Gtk3 event loop integration |
|
496 | %gui gtk3 # enable Gtk3 event loop integration | |
498 | %gui tk # enable Tk event loop integration |
|
497 | %gui tk # enable Tk event loop integration | |
499 | %gui osx # enable Cocoa event loop integration |
|
498 | %gui osx # enable Cocoa event loop integration | |
500 | # (requires %matplotlib 1.1) |
|
499 | # (requires %matplotlib 1.1) | |
501 | %gui # disable all event loop integration |
|
500 | %gui # disable all event loop integration | |
502 |
|
501 | |||
503 | WARNING: after any of these has been called you can simply create |
|
502 | WARNING: after any of these has been called you can simply create | |
504 | an application object, but DO NOT start the event loop yourself, as |
|
503 | an application object, but DO NOT start the event loop yourself, as | |
505 | we have already handled that. |
|
504 | we have already handled that. | |
506 | """ |
|
505 | """ | |
507 | opts, arg = self.parse_options(parameter_s, '') |
|
506 | opts, arg = self.parse_options(parameter_s, '') | |
508 | if arg=='': arg = None |
|
507 | if arg=='': arg = None | |
509 | try: |
|
508 | try: | |
510 | return self.shell.enable_gui(arg) |
|
509 | return self.shell.enable_gui(arg) | |
511 | except Exception as e: |
|
510 | except Exception as e: | |
512 | # print simple error message, rather than traceback if we can't |
|
511 | # print simple error message, rather than traceback if we can't | |
513 | # hook up the GUI |
|
512 | # hook up the GUI | |
514 | error(str(e)) |
|
513 | error(str(e)) | |
515 |
|
514 | |||
516 | @skip_doctest |
|
515 | @skip_doctest | |
517 | @line_magic |
|
516 | @line_magic | |
518 | def precision(self, s=''): |
|
517 | def precision(self, s=''): | |
519 | """Set floating point precision for pretty printing. |
|
518 | """Set floating point precision for pretty printing. | |
520 |
|
519 | |||
521 | Can set either integer precision or a format string. |
|
520 | Can set either integer precision or a format string. | |
522 |
|
521 | |||
523 | If numpy has been imported and precision is an int, |
|
522 | If numpy has been imported and precision is an int, | |
524 | numpy display precision will also be set, via ``numpy.set_printoptions``. |
|
523 | numpy display precision will also be set, via ``numpy.set_printoptions``. | |
525 |
|
524 | |||
526 | If no argument is given, defaults will be restored. |
|
525 | If no argument is given, defaults will be restored. | |
527 |
|
526 | |||
528 | Examples |
|
527 | Examples | |
529 | -------- |
|
528 | -------- | |
530 | :: |
|
529 | :: | |
531 |
|
530 | |||
532 | In [1]: from math import pi |
|
531 | In [1]: from math import pi | |
533 |
|
532 | |||
534 | In [2]: %precision 3 |
|
533 | In [2]: %precision 3 | |
535 | Out[2]: u'%.3f' |
|
534 | Out[2]: u'%.3f' | |
536 |
|
535 | |||
537 | In [3]: pi |
|
536 | In [3]: pi | |
538 | Out[3]: 3.142 |
|
537 | Out[3]: 3.142 | |
539 |
|
538 | |||
540 | In [4]: %precision %i |
|
539 | In [4]: %precision %i | |
541 | Out[4]: u'%i' |
|
540 | Out[4]: u'%i' | |
542 |
|
541 | |||
543 | In [5]: pi |
|
542 | In [5]: pi | |
544 | Out[5]: 3 |
|
543 | Out[5]: 3 | |
545 |
|
544 | |||
546 | In [6]: %precision %e |
|
545 | In [6]: %precision %e | |
547 | Out[6]: u'%e' |
|
546 | Out[6]: u'%e' | |
548 |
|
547 | |||
549 | In [7]: pi**10 |
|
548 | In [7]: pi**10 | |
550 | Out[7]: 9.364805e+04 |
|
549 | Out[7]: 9.364805e+04 | |
551 |
|
550 | |||
552 | In [8]: %precision |
|
551 | In [8]: %precision | |
553 | Out[8]: u'%r' |
|
552 | Out[8]: u'%r' | |
554 |
|
553 | |||
555 | In [9]: pi**10 |
|
554 | In [9]: pi**10 | |
556 | Out[9]: 93648.047476082982 |
|
555 | Out[9]: 93648.047476082982 | |
557 | """ |
|
556 | """ | |
558 | ptformatter = self.shell.display_formatter.formatters['text/plain'] |
|
557 | ptformatter = self.shell.display_formatter.formatters['text/plain'] | |
559 | ptformatter.float_precision = s |
|
558 | ptformatter.float_precision = s | |
560 | return ptformatter.float_format |
|
559 | return ptformatter.float_format | |
561 |
|
560 | |||
562 | @magic_arguments.magic_arguments() |
|
561 | @magic_arguments.magic_arguments() | |
563 | @magic_arguments.argument( |
|
562 | @magic_arguments.argument( | |
564 | '-e', '--export', action='store_true', default=False, |
|
563 | '-e', '--export', action='store_true', default=False, | |
565 | help='Export IPython history as a notebook. The filename argument ' |
|
564 | help='Export IPython history as a notebook. The filename argument ' | |
566 | 'is used to specify the notebook name and format. For example ' |
|
565 | 'is used to specify the notebook name and format. For example ' | |
567 | 'a filename of notebook.ipynb will result in a notebook name ' |
|
566 | 'a filename of notebook.ipynb will result in a notebook name ' | |
568 | 'of "notebook" and a format of "json". Likewise using a ".py" ' |
|
567 | 'of "notebook" and a format of "json". Likewise using a ".py" ' | |
569 | 'file extension will write the notebook as a Python script' |
|
568 | 'file extension will write the notebook as a Python script' | |
570 | ) |
|
569 | ) | |
571 | @magic_arguments.argument( |
|
570 | @magic_arguments.argument( | |
572 | 'filename', type=unicode_type, |
|
571 | 'filename', type=unicode_type, | |
573 | help='Notebook name or filename' |
|
572 | help='Notebook name or filename' | |
574 | ) |
|
573 | ) | |
575 | @line_magic |
|
574 | @line_magic | |
576 | def notebook(self, s): |
|
575 | def notebook(self, s): | |
577 | """Export and convert IPython notebooks. |
|
576 | """Export and convert IPython notebooks. | |
578 |
|
577 | |||
579 | This function can export the current IPython history to a notebook file. |
|
578 | This function can export the current IPython history to a notebook file. | |
580 | For example, to export the history to "foo.ipynb" do "%notebook -e foo.ipynb". |
|
579 | For example, to export the history to "foo.ipynb" do "%notebook -e foo.ipynb". | |
581 | To export the history to "foo.py" do "%notebook -e foo.py". |
|
580 | To export the history to "foo.py" do "%notebook -e foo.py". | |
582 | """ |
|
581 | """ | |
583 | args = magic_arguments.parse_argstring(self.notebook, s) |
|
582 | args = magic_arguments.parse_argstring(self.notebook, s) | |
584 |
|
583 | |||
585 | from nbformat import write, v4 |
|
584 | from nbformat import write, v4 | |
586 | args.filename = unquote_filename(args.filename) |
|
|||
587 | if args.export: |
|
585 | if args.export: | |
588 | cells = [] |
|
586 | cells = [] | |
589 | hist = list(self.shell.history_manager.get_range()) |
|
587 | hist = list(self.shell.history_manager.get_range()) | |
590 | if(len(hist)<=1): |
|
588 | if(len(hist)<=1): | |
591 | raise ValueError('History is empty, cannot export') |
|
589 | raise ValueError('History is empty, cannot export') | |
592 | for session, execution_count, source in hist[:-1]: |
|
590 | for session, execution_count, source in hist[:-1]: | |
593 | cells.append(v4.new_code_cell( |
|
591 | cells.append(v4.new_code_cell( | |
594 | execution_count=execution_count, |
|
592 | execution_count=execution_count, | |
595 | source=source |
|
593 | source=source | |
596 | )) |
|
594 | )) | |
597 | nb = v4.new_notebook(cells=cells) |
|
595 | nb = v4.new_notebook(cells=cells) | |
598 | with io.open(args.filename, 'w', encoding='utf-8') as f: |
|
596 | with io.open(args.filename, 'w', encoding='utf-8') as f: | |
599 | write(nb, f, version=4) |
|
597 | write(nb, f, version=4) |
@@ -1,717 +1,716 b'' | |||||
1 | """Implementation of code management magic functions. |
|
1 | """Implementation of code management magic functions. | |
2 | """ |
|
2 | """ | |
3 | from __future__ import print_function |
|
3 | from __future__ import print_function | |
4 | from __future__ import absolute_import |
|
4 | from __future__ import absolute_import | |
5 | #----------------------------------------------------------------------------- |
|
5 | #----------------------------------------------------------------------------- | |
6 | # Copyright (c) 2012 The IPython Development Team. |
|
6 | # Copyright (c) 2012 The IPython Development Team. | |
7 | # |
|
7 | # | |
8 | # Distributed under the terms of the Modified BSD License. |
|
8 | # Distributed under the terms of the Modified BSD License. | |
9 | # |
|
9 | # | |
10 | # The full license is in the file COPYING.txt, distributed with this software. |
|
10 | # The full license is in the file COPYING.txt, distributed with this software. | |
11 | #----------------------------------------------------------------------------- |
|
11 | #----------------------------------------------------------------------------- | |
12 |
|
12 | |||
13 | #----------------------------------------------------------------------------- |
|
13 | #----------------------------------------------------------------------------- | |
14 | # Imports |
|
14 | # Imports | |
15 | #----------------------------------------------------------------------------- |
|
15 | #----------------------------------------------------------------------------- | |
16 |
|
16 | |||
17 | # Stdlib |
|
17 | # Stdlib | |
18 | import inspect |
|
18 | import inspect | |
19 | import io |
|
19 | import io | |
20 | import os |
|
20 | import os | |
21 | import re |
|
21 | import re | |
22 | import sys |
|
22 | import sys | |
23 | import ast |
|
23 | import ast | |
24 | from itertools import chain |
|
24 | from itertools import chain | |
25 |
|
25 | |||
26 | # Our own packages |
|
26 | # Our own packages | |
27 | from IPython.core.error import TryNext, StdinNotImplementedError, UsageError |
|
27 | from IPython.core.error import TryNext, StdinNotImplementedError, UsageError | |
28 | from IPython.core.macro import Macro |
|
28 | from IPython.core.macro import Macro | |
29 | from IPython.core.magic import Magics, magics_class, line_magic |
|
29 | from IPython.core.magic import Magics, magics_class, line_magic | |
30 | from IPython.core.oinspect import find_file, find_source_lines |
|
30 | from IPython.core.oinspect import find_file, find_source_lines | |
31 | from IPython.testing.skipdoctest import skip_doctest |
|
31 | from IPython.testing.skipdoctest import skip_doctest | |
32 | from IPython.utils import py3compat |
|
32 | from IPython.utils import py3compat | |
33 | from IPython.utils.py3compat import string_types |
|
33 | from IPython.utils.py3compat import string_types | |
34 | from IPython.utils.contexts import preserve_keys |
|
34 | from IPython.utils.contexts import preserve_keys | |
35 |
from IPython.utils.path import get_py_filename |
|
35 | from IPython.utils.path import get_py_filename | |
36 | from warnings import warn |
|
36 | from warnings import warn | |
37 | from logging import error |
|
37 | from logging import error | |
38 | from IPython.utils.text import get_text_list |
|
38 | from IPython.utils.text import get_text_list | |
39 |
|
39 | |||
40 | #----------------------------------------------------------------------------- |
|
40 | #----------------------------------------------------------------------------- | |
41 | # Magic implementation classes |
|
41 | # Magic implementation classes | |
42 | #----------------------------------------------------------------------------- |
|
42 | #----------------------------------------------------------------------------- | |
43 |
|
43 | |||
44 | # Used for exception handling in magic_edit |
|
44 | # Used for exception handling in magic_edit | |
45 | class MacroToEdit(ValueError): pass |
|
45 | class MacroToEdit(ValueError): pass | |
46 |
|
46 | |||
47 | ipython_input_pat = re.compile(r"<ipython\-input\-(\d+)-[a-z\d]+>$") |
|
47 | ipython_input_pat = re.compile(r"<ipython\-input\-(\d+)-[a-z\d]+>$") | |
48 |
|
48 | |||
49 | # To match, e.g. 8-10 1:5 :10 3- |
|
49 | # To match, e.g. 8-10 1:5 :10 3- | |
50 | range_re = re.compile(r""" |
|
50 | range_re = re.compile(r""" | |
51 | (?P<start>\d+)? |
|
51 | (?P<start>\d+)? | |
52 | ((?P<sep>[\-:]) |
|
52 | ((?P<sep>[\-:]) | |
53 | (?P<end>\d+)?)? |
|
53 | (?P<end>\d+)?)? | |
54 | $""", re.VERBOSE) |
|
54 | $""", re.VERBOSE) | |
55 |
|
55 | |||
56 |
|
56 | |||
57 | def extract_code_ranges(ranges_str): |
|
57 | def extract_code_ranges(ranges_str): | |
58 | """Turn a string of range for %%load into 2-tuples of (start, stop) |
|
58 | """Turn a string of range for %%load into 2-tuples of (start, stop) | |
59 | ready to use as a slice of the content splitted by lines. |
|
59 | ready to use as a slice of the content splitted by lines. | |
60 |
|
60 | |||
61 | Examples |
|
61 | Examples | |
62 | -------- |
|
62 | -------- | |
63 | list(extract_input_ranges("5-10 2")) |
|
63 | list(extract_input_ranges("5-10 2")) | |
64 | [(4, 10), (1, 2)] |
|
64 | [(4, 10), (1, 2)] | |
65 | """ |
|
65 | """ | |
66 | for range_str in ranges_str.split(): |
|
66 | for range_str in ranges_str.split(): | |
67 | rmatch = range_re.match(range_str) |
|
67 | rmatch = range_re.match(range_str) | |
68 | if not rmatch: |
|
68 | if not rmatch: | |
69 | continue |
|
69 | continue | |
70 | sep = rmatch.group("sep") |
|
70 | sep = rmatch.group("sep") | |
71 | start = rmatch.group("start") |
|
71 | start = rmatch.group("start") | |
72 | end = rmatch.group("end") |
|
72 | end = rmatch.group("end") | |
73 |
|
73 | |||
74 | if sep == '-': |
|
74 | if sep == '-': | |
75 | start = int(start) - 1 if start else None |
|
75 | start = int(start) - 1 if start else None | |
76 | end = int(end) if end else None |
|
76 | end = int(end) if end else None | |
77 | elif sep == ':': |
|
77 | elif sep == ':': | |
78 | start = int(start) - 1 if start else None |
|
78 | start = int(start) - 1 if start else None | |
79 | end = int(end) - 1 if end else None |
|
79 | end = int(end) - 1 if end else None | |
80 | else: |
|
80 | else: | |
81 | end = int(start) |
|
81 | end = int(start) | |
82 | start = int(start) - 1 |
|
82 | start = int(start) - 1 | |
83 | yield (start, end) |
|
83 | yield (start, end) | |
84 |
|
84 | |||
85 |
|
85 | |||
86 | @skip_doctest |
|
86 | @skip_doctest | |
87 | def extract_symbols(code, symbols): |
|
87 | def extract_symbols(code, symbols): | |
88 | """ |
|
88 | """ | |
89 | Return a tuple (blocks, not_found) |
|
89 | Return a tuple (blocks, not_found) | |
90 | where ``blocks`` is a list of code fragments |
|
90 | where ``blocks`` is a list of code fragments | |
91 | for each symbol parsed from code, and ``not_found`` are |
|
91 | for each symbol parsed from code, and ``not_found`` are | |
92 | symbols not found in the code. |
|
92 | symbols not found in the code. | |
93 |
|
93 | |||
94 | For example:: |
|
94 | For example:: | |
95 |
|
95 | |||
96 | >>> code = '''a = 10 |
|
96 | >>> code = '''a = 10 | |
97 |
|
97 | |||
98 | def b(): return 42 |
|
98 | def b(): return 42 | |
99 |
|
99 | |||
100 | class A: pass''' |
|
100 | class A: pass''' | |
101 |
|
101 | |||
102 | >>> extract_symbols(code, 'A,b,z') |
|
102 | >>> extract_symbols(code, 'A,b,z') | |
103 | (["class A: pass", "def b(): return 42"], ['z']) |
|
103 | (["class A: pass", "def b(): return 42"], ['z']) | |
104 | """ |
|
104 | """ | |
105 | symbols = symbols.split(',') |
|
105 | symbols = symbols.split(',') | |
106 |
|
106 | |||
107 | # this will raise SyntaxError if code isn't valid Python |
|
107 | # this will raise SyntaxError if code isn't valid Python | |
108 | py_code = ast.parse(code) |
|
108 | py_code = ast.parse(code) | |
109 |
|
109 | |||
110 | marks = [(getattr(s, 'name', None), s.lineno) for s in py_code.body] |
|
110 | marks = [(getattr(s, 'name', None), s.lineno) for s in py_code.body] | |
111 | code = code.split('\n') |
|
111 | code = code.split('\n') | |
112 |
|
112 | |||
113 | symbols_lines = {} |
|
113 | symbols_lines = {} | |
114 |
|
114 | |||
115 | # we already know the start_lineno of each symbol (marks). |
|
115 | # we already know the start_lineno of each symbol (marks). | |
116 | # To find each end_lineno, we traverse in reverse order until each |
|
116 | # To find each end_lineno, we traverse in reverse order until each | |
117 | # non-blank line |
|
117 | # non-blank line | |
118 | end = len(code) |
|
118 | end = len(code) | |
119 | for name, start in reversed(marks): |
|
119 | for name, start in reversed(marks): | |
120 | while not code[end - 1].strip(): |
|
120 | while not code[end - 1].strip(): | |
121 | end -= 1 |
|
121 | end -= 1 | |
122 | if name: |
|
122 | if name: | |
123 | symbols_lines[name] = (start - 1, end) |
|
123 | symbols_lines[name] = (start - 1, end) | |
124 | end = start - 1 |
|
124 | end = start - 1 | |
125 |
|
125 | |||
126 | # Now symbols_lines is a map |
|
126 | # Now symbols_lines is a map | |
127 | # {'symbol_name': (start_lineno, end_lineno), ...} |
|
127 | # {'symbol_name': (start_lineno, end_lineno), ...} | |
128 |
|
128 | |||
129 | # fill a list with chunks of codes for each requested symbol |
|
129 | # fill a list with chunks of codes for each requested symbol | |
130 | blocks = [] |
|
130 | blocks = [] | |
131 | not_found = [] |
|
131 | not_found = [] | |
132 | for symbol in symbols: |
|
132 | for symbol in symbols: | |
133 | if symbol in symbols_lines: |
|
133 | if symbol in symbols_lines: | |
134 | start, end = symbols_lines[symbol] |
|
134 | start, end = symbols_lines[symbol] | |
135 | blocks.append('\n'.join(code[start:end]) + '\n') |
|
135 | blocks.append('\n'.join(code[start:end]) + '\n') | |
136 | else: |
|
136 | else: | |
137 | not_found.append(symbol) |
|
137 | not_found.append(symbol) | |
138 |
|
138 | |||
139 | return blocks, not_found |
|
139 | return blocks, not_found | |
140 |
|
140 | |||
141 |
|
141 | |||
142 | class InteractivelyDefined(Exception): |
|
142 | class InteractivelyDefined(Exception): | |
143 | """Exception for interactively defined variable in magic_edit""" |
|
143 | """Exception for interactively defined variable in magic_edit""" | |
144 | def __init__(self, index): |
|
144 | def __init__(self, index): | |
145 | self.index = index |
|
145 | self.index = index | |
146 |
|
146 | |||
147 |
|
147 | |||
148 | @magics_class |
|
148 | @magics_class | |
149 | class CodeMagics(Magics): |
|
149 | class CodeMagics(Magics): | |
150 | """Magics related to code management (loading, saving, editing, ...).""" |
|
150 | """Magics related to code management (loading, saving, editing, ...).""" | |
151 |
|
151 | |||
152 | def __init__(self, *args, **kwargs): |
|
152 | def __init__(self, *args, **kwargs): | |
153 | self._knowntemps = set() |
|
153 | self._knowntemps = set() | |
154 | super(CodeMagics, self).__init__(*args, **kwargs) |
|
154 | super(CodeMagics, self).__init__(*args, **kwargs) | |
155 |
|
155 | |||
156 | @line_magic |
|
156 | @line_magic | |
157 | def save(self, parameter_s=''): |
|
157 | def save(self, parameter_s=''): | |
158 | """Save a set of lines or a macro to a given filename. |
|
158 | """Save a set of lines or a macro to a given filename. | |
159 |
|
159 | |||
160 | Usage:\\ |
|
160 | Usage:\\ | |
161 | %save [options] filename n1-n2 n3-n4 ... n5 .. n6 ... |
|
161 | %save [options] filename n1-n2 n3-n4 ... n5 .. n6 ... | |
162 |
|
162 | |||
163 | Options: |
|
163 | Options: | |
164 |
|
164 | |||
165 | -r: use 'raw' input. By default, the 'processed' history is used, |
|
165 | -r: use 'raw' input. By default, the 'processed' history is used, | |
166 | so that magics are loaded in their transformed version to valid |
|
166 | so that magics are loaded in their transformed version to valid | |
167 | Python. If this option is given, the raw input as typed as the |
|
167 | Python. If this option is given, the raw input as typed as the | |
168 | command line is used instead. |
|
168 | command line is used instead. | |
169 |
|
169 | |||
170 | -f: force overwrite. If file exists, %save will prompt for overwrite |
|
170 | -f: force overwrite. If file exists, %save will prompt for overwrite | |
171 | unless -f is given. |
|
171 | unless -f is given. | |
172 |
|
172 | |||
173 | -a: append to the file instead of overwriting it. |
|
173 | -a: append to the file instead of overwriting it. | |
174 |
|
174 | |||
175 | This function uses the same syntax as %history for input ranges, |
|
175 | This function uses the same syntax as %history for input ranges, | |
176 | then saves the lines to the filename you specify. |
|
176 | then saves the lines to the filename you specify. | |
177 |
|
177 | |||
178 | It adds a '.py' extension to the file if you don't do so yourself, and |
|
178 | It adds a '.py' extension to the file if you don't do so yourself, and | |
179 | it asks for confirmation before overwriting existing files. |
|
179 | it asks for confirmation before overwriting existing files. | |
180 |
|
180 | |||
181 | If `-r` option is used, the default extension is `.ipy`. |
|
181 | If `-r` option is used, the default extension is `.ipy`. | |
182 | """ |
|
182 | """ | |
183 |
|
183 | |||
184 | opts,args = self.parse_options(parameter_s,'fra',mode='list') |
|
184 | opts,args = self.parse_options(parameter_s,'fra',mode='list') | |
185 | if not args: |
|
185 | if not args: | |
186 | raise UsageError('Missing filename.') |
|
186 | raise UsageError('Missing filename.') | |
187 | raw = 'r' in opts |
|
187 | raw = 'r' in opts | |
188 | force = 'f' in opts |
|
188 | force = 'f' in opts | |
189 | append = 'a' in opts |
|
189 | append = 'a' in opts | |
190 | mode = 'a' if append else 'w' |
|
190 | mode = 'a' if append else 'w' | |
191 | ext = u'.ipy' if raw else u'.py' |
|
191 | ext = u'.ipy' if raw else u'.py' | |
192 |
fname, codefrom = |
|
192 | fname, codefrom = args[0], " ".join(args[1:]) | |
193 | if not fname.endswith((u'.py',u'.ipy')): |
|
193 | if not fname.endswith((u'.py',u'.ipy')): | |
194 | fname += ext |
|
194 | fname += ext | |
195 | file_exists = os.path.isfile(fname) |
|
195 | file_exists = os.path.isfile(fname) | |
196 | if file_exists and not force and not append: |
|
196 | if file_exists and not force and not append: | |
197 | try: |
|
197 | try: | |
198 | overwrite = self.shell.ask_yes_no('File `%s` exists. Overwrite (y/[N])? ' % fname, default='n') |
|
198 | overwrite = self.shell.ask_yes_no('File `%s` exists. Overwrite (y/[N])? ' % fname, default='n') | |
199 | except StdinNotImplementedError: |
|
199 | except StdinNotImplementedError: | |
200 | print("File `%s` exists. Use `%%save -f %s` to force overwrite" % (fname, parameter_s)) |
|
200 | print("File `%s` exists. Use `%%save -f %s` to force overwrite" % (fname, parameter_s)) | |
201 | return |
|
201 | return | |
202 | if not overwrite : |
|
202 | if not overwrite : | |
203 | print('Operation cancelled.') |
|
203 | print('Operation cancelled.') | |
204 | return |
|
204 | return | |
205 | try: |
|
205 | try: | |
206 | cmds = self.shell.find_user_code(codefrom,raw) |
|
206 | cmds = self.shell.find_user_code(codefrom,raw) | |
207 | except (TypeError, ValueError) as e: |
|
207 | except (TypeError, ValueError) as e: | |
208 | print(e.args[0]) |
|
208 | print(e.args[0]) | |
209 | return |
|
209 | return | |
210 | out = py3compat.cast_unicode(cmds) |
|
210 | out = py3compat.cast_unicode(cmds) | |
211 | with io.open(fname, mode, encoding="utf-8") as f: |
|
211 | with io.open(fname, mode, encoding="utf-8") as f: | |
212 | if not file_exists or not append: |
|
212 | if not file_exists or not append: | |
213 | f.write(u"# coding: utf-8\n") |
|
213 | f.write(u"# coding: utf-8\n") | |
214 | f.write(out) |
|
214 | f.write(out) | |
215 | # make sure we end on a newline |
|
215 | # make sure we end on a newline | |
216 | if not out.endswith(u'\n'): |
|
216 | if not out.endswith(u'\n'): | |
217 | f.write(u'\n') |
|
217 | f.write(u'\n') | |
218 | print('The following commands were written to file `%s`:' % fname) |
|
218 | print('The following commands were written to file `%s`:' % fname) | |
219 | print(cmds) |
|
219 | print(cmds) | |
220 |
|
220 | |||
221 | @line_magic |
|
221 | @line_magic | |
222 | def pastebin(self, parameter_s=''): |
|
222 | def pastebin(self, parameter_s=''): | |
223 | """Upload code to Github's Gist paste bin, returning the URL. |
|
223 | """Upload code to Github's Gist paste bin, returning the URL. | |
224 |
|
224 | |||
225 | Usage:\\ |
|
225 | Usage:\\ | |
226 | %pastebin [-d "Custom description"] 1-7 |
|
226 | %pastebin [-d "Custom description"] 1-7 | |
227 |
|
227 | |||
228 | The argument can be an input history range, a filename, or the name of a |
|
228 | The argument can be an input history range, a filename, or the name of a | |
229 | string or macro. |
|
229 | string or macro. | |
230 |
|
230 | |||
231 | Options: |
|
231 | Options: | |
232 |
|
232 | |||
233 | -d: Pass a custom description for the gist. The default will say |
|
233 | -d: Pass a custom description for the gist. The default will say | |
234 | "Pasted from IPython". |
|
234 | "Pasted from IPython". | |
235 | """ |
|
235 | """ | |
236 | opts, args = self.parse_options(parameter_s, 'd:') |
|
236 | opts, args = self.parse_options(parameter_s, 'd:') | |
237 |
|
237 | |||
238 | try: |
|
238 | try: | |
239 | code = self.shell.find_user_code(args) |
|
239 | code = self.shell.find_user_code(args) | |
240 | except (ValueError, TypeError) as e: |
|
240 | except (ValueError, TypeError) as e: | |
241 | print(e.args[0]) |
|
241 | print(e.args[0]) | |
242 | return |
|
242 | return | |
243 |
|
243 | |||
244 | # Deferred import |
|
244 | # Deferred import | |
245 | try: |
|
245 | try: | |
246 | from urllib.request import urlopen # Py 3 |
|
246 | from urllib.request import urlopen # Py 3 | |
247 | except ImportError: |
|
247 | except ImportError: | |
248 | from urllib2 import urlopen |
|
248 | from urllib2 import urlopen | |
249 | import json |
|
249 | import json | |
250 | post_data = json.dumps({ |
|
250 | post_data = json.dumps({ | |
251 | "description": opts.get('d', "Pasted from IPython"), |
|
251 | "description": opts.get('d', "Pasted from IPython"), | |
252 | "public": True, |
|
252 | "public": True, | |
253 | "files": { |
|
253 | "files": { | |
254 | "file1.py": { |
|
254 | "file1.py": { | |
255 | "content": code |
|
255 | "content": code | |
256 | } |
|
256 | } | |
257 | } |
|
257 | } | |
258 | }).encode('utf-8') |
|
258 | }).encode('utf-8') | |
259 |
|
259 | |||
260 | response = urlopen("https://api.github.com/gists", post_data) |
|
260 | response = urlopen("https://api.github.com/gists", post_data) | |
261 | response_data = json.loads(response.read().decode('utf-8')) |
|
261 | response_data = json.loads(response.read().decode('utf-8')) | |
262 | return response_data['html_url'] |
|
262 | return response_data['html_url'] | |
263 |
|
263 | |||
264 | @line_magic |
|
264 | @line_magic | |
265 | def loadpy(self, arg_s): |
|
265 | def loadpy(self, arg_s): | |
266 | """Alias of `%load` |
|
266 | """Alias of `%load` | |
267 |
|
267 | |||
268 | `%loadpy` has gained some flexibility and dropped the requirement of a `.py` |
|
268 | `%loadpy` has gained some flexibility and dropped the requirement of a `.py` | |
269 | extension. So it has been renamed simply into %load. You can look at |
|
269 | extension. So it has been renamed simply into %load. You can look at | |
270 | `%load`'s docstring for more info. |
|
270 | `%load`'s docstring for more info. | |
271 | """ |
|
271 | """ | |
272 | self.load(arg_s) |
|
272 | self.load(arg_s) | |
273 |
|
273 | |||
274 | @line_magic |
|
274 | @line_magic | |
275 | def load(self, arg_s): |
|
275 | def load(self, arg_s): | |
276 | """Load code into the current frontend. |
|
276 | """Load code into the current frontend. | |
277 |
|
277 | |||
278 | Usage:\\ |
|
278 | Usage:\\ | |
279 | %load [options] source |
|
279 | %load [options] source | |
280 |
|
280 | |||
281 | where source can be a filename, URL, input history range, macro, or |
|
281 | where source can be a filename, URL, input history range, macro, or | |
282 | element in the user namespace |
|
282 | element in the user namespace | |
283 |
|
283 | |||
284 | Options: |
|
284 | Options: | |
285 |
|
285 | |||
286 | -r <lines>: Specify lines or ranges of lines to load from the source. |
|
286 | -r <lines>: Specify lines or ranges of lines to load from the source. | |
287 | Ranges could be specified as x-y (x..y) or in python-style x:y |
|
287 | Ranges could be specified as x-y (x..y) or in python-style x:y | |
288 | (x..(y-1)). Both limits x and y can be left blank (meaning the |
|
288 | (x..(y-1)). Both limits x and y can be left blank (meaning the | |
289 | beginning and end of the file, respectively). |
|
289 | beginning and end of the file, respectively). | |
290 |
|
290 | |||
291 | -s <symbols>: Specify function or classes to load from python source. |
|
291 | -s <symbols>: Specify function or classes to load from python source. | |
292 |
|
292 | |||
293 | -y : Don't ask confirmation for loading source above 200 000 characters. |
|
293 | -y : Don't ask confirmation for loading source above 200 000 characters. | |
294 |
|
294 | |||
295 | -n : Include the user's namespace when searching for source code. |
|
295 | -n : Include the user's namespace when searching for source code. | |
296 |
|
296 | |||
297 | This magic command can either take a local filename, a URL, an history |
|
297 | This magic command can either take a local filename, a URL, an history | |
298 | range (see %history) or a macro as argument, it will prompt for |
|
298 | range (see %history) or a macro as argument, it will prompt for | |
299 | confirmation before loading source with more than 200 000 characters, unless |
|
299 | confirmation before loading source with more than 200 000 characters, unless | |
300 | -y flag is passed or if the frontend does not support raw_input:: |
|
300 | -y flag is passed or if the frontend does not support raw_input:: | |
301 |
|
301 | |||
302 | %load myscript.py |
|
302 | %load myscript.py | |
303 | %load 7-27 |
|
303 | %load 7-27 | |
304 | %load myMacro |
|
304 | %load myMacro | |
305 | %load http://www.example.com/myscript.py |
|
305 | %load http://www.example.com/myscript.py | |
306 | %load -r 5-10 myscript.py |
|
306 | %load -r 5-10 myscript.py | |
307 | %load -r 10-20,30,40: foo.py |
|
307 | %load -r 10-20,30,40: foo.py | |
308 | %load -s MyClass,wonder_function myscript.py |
|
308 | %load -s MyClass,wonder_function myscript.py | |
309 | %load -n MyClass |
|
309 | %load -n MyClass | |
310 | %load -n my_module.wonder_function |
|
310 | %load -n my_module.wonder_function | |
311 | """ |
|
311 | """ | |
312 | opts,args = self.parse_options(arg_s,'yns:r:') |
|
312 | opts,args = self.parse_options(arg_s,'yns:r:') | |
313 |
|
313 | |||
314 | if not args: |
|
314 | if not args: | |
315 | raise UsageError('Missing filename, URL, input history range, ' |
|
315 | raise UsageError('Missing filename, URL, input history range, ' | |
316 | 'macro, or element in the user namespace.') |
|
316 | 'macro, or element in the user namespace.') | |
317 |
|
317 | |||
318 | search_ns = 'n' in opts |
|
318 | search_ns = 'n' in opts | |
319 |
|
319 | |||
320 | contents = self.shell.find_user_code(args, search_ns=search_ns) |
|
320 | contents = self.shell.find_user_code(args, search_ns=search_ns) | |
321 |
|
321 | |||
322 | if 's' in opts: |
|
322 | if 's' in opts: | |
323 | try: |
|
323 | try: | |
324 | blocks, not_found = extract_symbols(contents, opts['s']) |
|
324 | blocks, not_found = extract_symbols(contents, opts['s']) | |
325 | except SyntaxError: |
|
325 | except SyntaxError: | |
326 | # non python code |
|
326 | # non python code | |
327 | error("Unable to parse the input as valid Python code") |
|
327 | error("Unable to parse the input as valid Python code") | |
328 | return |
|
328 | return | |
329 |
|
329 | |||
330 | if len(not_found) == 1: |
|
330 | if len(not_found) == 1: | |
331 | warn('The symbol `%s` was not found' % not_found[0]) |
|
331 | warn('The symbol `%s` was not found' % not_found[0]) | |
332 | elif len(not_found) > 1: |
|
332 | elif len(not_found) > 1: | |
333 | warn('The symbols %s were not found' % get_text_list(not_found, |
|
333 | warn('The symbols %s were not found' % get_text_list(not_found, | |
334 | wrap_item_with='`') |
|
334 | wrap_item_with='`') | |
335 | ) |
|
335 | ) | |
336 |
|
336 | |||
337 | contents = '\n'.join(blocks) |
|
337 | contents = '\n'.join(blocks) | |
338 |
|
338 | |||
339 | if 'r' in opts: |
|
339 | if 'r' in opts: | |
340 | ranges = opts['r'].replace(',', ' ') |
|
340 | ranges = opts['r'].replace(',', ' ') | |
341 | lines = contents.split('\n') |
|
341 | lines = contents.split('\n') | |
342 | slices = extract_code_ranges(ranges) |
|
342 | slices = extract_code_ranges(ranges) | |
343 | contents = [lines[slice(*slc)] for slc in slices] |
|
343 | contents = [lines[slice(*slc)] for slc in slices] | |
344 | contents = '\n'.join(chain.from_iterable(contents)) |
|
344 | contents = '\n'.join(chain.from_iterable(contents)) | |
345 |
|
345 | |||
346 | l = len(contents) |
|
346 | l = len(contents) | |
347 |
|
347 | |||
348 | # 200 000 is ~ 2500 full 80 caracter lines |
|
348 | # 200 000 is ~ 2500 full 80 caracter lines | |
349 | # so in average, more than 5000 lines |
|
349 | # so in average, more than 5000 lines | |
350 | if l > 200000 and 'y' not in opts: |
|
350 | if l > 200000 and 'y' not in opts: | |
351 | try: |
|
351 | try: | |
352 | ans = self.shell.ask_yes_no(("The text you're trying to load seems pretty big"\ |
|
352 | ans = self.shell.ask_yes_no(("The text you're trying to load seems pretty big"\ | |
353 | " (%d characters). Continue (y/[N]) ?" % l), default='n' ) |
|
353 | " (%d characters). Continue (y/[N]) ?" % l), default='n' ) | |
354 | except StdinNotImplementedError: |
|
354 | except StdinNotImplementedError: | |
355 | #asume yes if raw input not implemented |
|
355 | #asume yes if raw input not implemented | |
356 | ans = True |
|
356 | ans = True | |
357 |
|
357 | |||
358 | if ans is False : |
|
358 | if ans is False : | |
359 | print('Operation cancelled.') |
|
359 | print('Operation cancelled.') | |
360 | return |
|
360 | return | |
361 |
|
361 | |||
362 | contents = "# %load {}\n".format(arg_s) + contents |
|
362 | contents = "# %load {}\n".format(arg_s) + contents | |
363 |
|
363 | |||
364 | self.shell.set_next_input(contents, replace=True) |
|
364 | self.shell.set_next_input(contents, replace=True) | |
365 |
|
365 | |||
366 | @staticmethod |
|
366 | @staticmethod | |
367 | def _find_edit_target(shell, args, opts, last_call): |
|
367 | def _find_edit_target(shell, args, opts, last_call): | |
368 | """Utility method used by magic_edit to find what to edit.""" |
|
368 | """Utility method used by magic_edit to find what to edit.""" | |
369 |
|
369 | |||
370 | def make_filename(arg): |
|
370 | def make_filename(arg): | |
371 | "Make a filename from the given args" |
|
371 | "Make a filename from the given args" | |
372 | arg = unquote_filename(arg) |
|
|||
373 | try: |
|
372 | try: | |
374 | filename = get_py_filename(arg) |
|
373 | filename = get_py_filename(arg) | |
375 | except IOError: |
|
374 | except IOError: | |
376 | # If it ends with .py but doesn't already exist, assume we want |
|
375 | # If it ends with .py but doesn't already exist, assume we want | |
377 | # a new file. |
|
376 | # a new file. | |
378 | if arg.endswith('.py'): |
|
377 | if arg.endswith('.py'): | |
379 | filename = arg |
|
378 | filename = arg | |
380 | else: |
|
379 | else: | |
381 | filename = None |
|
380 | filename = None | |
382 | return filename |
|
381 | return filename | |
383 |
|
382 | |||
384 | # Set a few locals from the options for convenience: |
|
383 | # Set a few locals from the options for convenience: | |
385 | opts_prev = 'p' in opts |
|
384 | opts_prev = 'p' in opts | |
386 | opts_raw = 'r' in opts |
|
385 | opts_raw = 'r' in opts | |
387 |
|
386 | |||
388 | # custom exceptions |
|
387 | # custom exceptions | |
389 | class DataIsObject(Exception): pass |
|
388 | class DataIsObject(Exception): pass | |
390 |
|
389 | |||
391 | # Default line number value |
|
390 | # Default line number value | |
392 | lineno = opts.get('n',None) |
|
391 | lineno = opts.get('n',None) | |
393 |
|
392 | |||
394 | if opts_prev: |
|
393 | if opts_prev: | |
395 | args = '_%s' % last_call[0] |
|
394 | args = '_%s' % last_call[0] | |
396 | if args not in shell.user_ns: |
|
395 | if args not in shell.user_ns: | |
397 | args = last_call[1] |
|
396 | args = last_call[1] | |
398 |
|
397 | |||
399 | # by default this is done with temp files, except when the given |
|
398 | # by default this is done with temp files, except when the given | |
400 | # arg is a filename |
|
399 | # arg is a filename | |
401 | use_temp = True |
|
400 | use_temp = True | |
402 |
|
401 | |||
403 | data = '' |
|
402 | data = '' | |
404 |
|
403 | |||
405 | # First, see if the arguments should be a filename. |
|
404 | # First, see if the arguments should be a filename. | |
406 | filename = make_filename(args) |
|
405 | filename = make_filename(args) | |
407 | if filename: |
|
406 | if filename: | |
408 | use_temp = False |
|
407 | use_temp = False | |
409 | elif args: |
|
408 | elif args: | |
410 | # Mode where user specifies ranges of lines, like in %macro. |
|
409 | # Mode where user specifies ranges of lines, like in %macro. | |
411 | data = shell.extract_input_lines(args, opts_raw) |
|
410 | data = shell.extract_input_lines(args, opts_raw) | |
412 | if not data: |
|
411 | if not data: | |
413 | try: |
|
412 | try: | |
414 | # Load the parameter given as a variable. If not a string, |
|
413 | # Load the parameter given as a variable. If not a string, | |
415 | # process it as an object instead (below) |
|
414 | # process it as an object instead (below) | |
416 |
|
415 | |||
417 | #print '*** args',args,'type',type(args) # dbg |
|
416 | #print '*** args',args,'type',type(args) # dbg | |
418 | data = eval(args, shell.user_ns) |
|
417 | data = eval(args, shell.user_ns) | |
419 | if not isinstance(data, string_types): |
|
418 | if not isinstance(data, string_types): | |
420 | raise DataIsObject |
|
419 | raise DataIsObject | |
421 |
|
420 | |||
422 | except (NameError,SyntaxError): |
|
421 | except (NameError,SyntaxError): | |
423 | # given argument is not a variable, try as a filename |
|
422 | # given argument is not a variable, try as a filename | |
424 | filename = make_filename(args) |
|
423 | filename = make_filename(args) | |
425 | if filename is None: |
|
424 | if filename is None: | |
426 | warn("Argument given (%s) can't be found as a variable " |
|
425 | warn("Argument given (%s) can't be found as a variable " | |
427 | "or as a filename." % args) |
|
426 | "or as a filename." % args) | |
428 | return (None, None, None) |
|
427 | return (None, None, None) | |
429 | use_temp = False |
|
428 | use_temp = False | |
430 |
|
429 | |||
431 | except DataIsObject: |
|
430 | except DataIsObject: | |
432 | # macros have a special edit function |
|
431 | # macros have a special edit function | |
433 | if isinstance(data, Macro): |
|
432 | if isinstance(data, Macro): | |
434 | raise MacroToEdit(data) |
|
433 | raise MacroToEdit(data) | |
435 |
|
434 | |||
436 | # For objects, try to edit the file where they are defined |
|
435 | # For objects, try to edit the file where they are defined | |
437 | filename = find_file(data) |
|
436 | filename = find_file(data) | |
438 | if filename: |
|
437 | if filename: | |
439 | if 'fakemodule' in filename.lower() and \ |
|
438 | if 'fakemodule' in filename.lower() and \ | |
440 | inspect.isclass(data): |
|
439 | inspect.isclass(data): | |
441 | # class created by %edit? Try to find source |
|
440 | # class created by %edit? Try to find source | |
442 | # by looking for method definitions instead, the |
|
441 | # by looking for method definitions instead, the | |
443 | # __module__ in those classes is FakeModule. |
|
442 | # __module__ in those classes is FakeModule. | |
444 | attrs = [getattr(data, aname) for aname in dir(data)] |
|
443 | attrs = [getattr(data, aname) for aname in dir(data)] | |
445 | for attr in attrs: |
|
444 | for attr in attrs: | |
446 | if not inspect.ismethod(attr): |
|
445 | if not inspect.ismethod(attr): | |
447 | continue |
|
446 | continue | |
448 | filename = find_file(attr) |
|
447 | filename = find_file(attr) | |
449 | if filename and \ |
|
448 | if filename and \ | |
450 | 'fakemodule' not in filename.lower(): |
|
449 | 'fakemodule' not in filename.lower(): | |
451 | # change the attribute to be the edit |
|
450 | # change the attribute to be the edit | |
452 | # target instead |
|
451 | # target instead | |
453 | data = attr |
|
452 | data = attr | |
454 | break |
|
453 | break | |
455 |
|
454 | |||
456 | m = ipython_input_pat.match(os.path.basename(filename)) |
|
455 | m = ipython_input_pat.match(os.path.basename(filename)) | |
457 | if m: |
|
456 | if m: | |
458 | raise InteractivelyDefined(int(m.groups()[0])) |
|
457 | raise InteractivelyDefined(int(m.groups()[0])) | |
459 |
|
458 | |||
460 | datafile = 1 |
|
459 | datafile = 1 | |
461 | if filename is None: |
|
460 | if filename is None: | |
462 | filename = make_filename(args) |
|
461 | filename = make_filename(args) | |
463 | datafile = 1 |
|
462 | datafile = 1 | |
464 | if filename is not None: |
|
463 | if filename is not None: | |
465 | # only warn about this if we get a real name |
|
464 | # only warn about this if we get a real name | |
466 | warn('Could not find file where `%s` is defined.\n' |
|
465 | warn('Could not find file where `%s` is defined.\n' | |
467 | 'Opening a file named `%s`' % (args, filename)) |
|
466 | 'Opening a file named `%s`' % (args, filename)) | |
468 | # Now, make sure we can actually read the source (if it was |
|
467 | # Now, make sure we can actually read the source (if it was | |
469 | # in a temp file it's gone by now). |
|
468 | # in a temp file it's gone by now). | |
470 | if datafile: |
|
469 | if datafile: | |
471 | if lineno is None: |
|
470 | if lineno is None: | |
472 | lineno = find_source_lines(data) |
|
471 | lineno = find_source_lines(data) | |
473 | if lineno is None: |
|
472 | if lineno is None: | |
474 | filename = make_filename(args) |
|
473 | filename = make_filename(args) | |
475 | if filename is None: |
|
474 | if filename is None: | |
476 | warn('The file where `%s` was defined ' |
|
475 | warn('The file where `%s` was defined ' | |
477 | 'cannot be read or found.' % data) |
|
476 | 'cannot be read or found.' % data) | |
478 | return (None, None, None) |
|
477 | return (None, None, None) | |
479 | use_temp = False |
|
478 | use_temp = False | |
480 |
|
479 | |||
481 | if use_temp: |
|
480 | if use_temp: | |
482 | filename = shell.mktempfile(data) |
|
481 | filename = shell.mktempfile(data) | |
483 | print('IPython will make a temporary file named:',filename) |
|
482 | print('IPython will make a temporary file named:',filename) | |
484 |
|
483 | |||
485 | # use last_call to remember the state of the previous call, but don't |
|
484 | # use last_call to remember the state of the previous call, but don't | |
486 | # let it be clobbered by successive '-p' calls. |
|
485 | # let it be clobbered by successive '-p' calls. | |
487 | try: |
|
486 | try: | |
488 | last_call[0] = shell.displayhook.prompt_count |
|
487 | last_call[0] = shell.displayhook.prompt_count | |
489 | if not opts_prev: |
|
488 | if not opts_prev: | |
490 | last_call[1] = args |
|
489 | last_call[1] = args | |
491 | except: |
|
490 | except: | |
492 | pass |
|
491 | pass | |
493 |
|
492 | |||
494 |
|
493 | |||
495 | return filename, lineno, use_temp |
|
494 | return filename, lineno, use_temp | |
496 |
|
495 | |||
497 | def _edit_macro(self,mname,macro): |
|
496 | def _edit_macro(self,mname,macro): | |
498 | """open an editor with the macro data in a file""" |
|
497 | """open an editor with the macro data in a file""" | |
499 | filename = self.shell.mktempfile(macro.value) |
|
498 | filename = self.shell.mktempfile(macro.value) | |
500 | self.shell.hooks.editor(filename) |
|
499 | self.shell.hooks.editor(filename) | |
501 |
|
500 | |||
502 | # and make a new macro object, to replace the old one |
|
501 | # and make a new macro object, to replace the old one | |
503 | with open(filename) as mfile: |
|
502 | with open(filename) as mfile: | |
504 | mvalue = mfile.read() |
|
503 | mvalue = mfile.read() | |
505 | self.shell.user_ns[mname] = Macro(mvalue) |
|
504 | self.shell.user_ns[mname] = Macro(mvalue) | |
506 |
|
505 | |||
507 | @skip_doctest |
|
506 | @skip_doctest | |
508 | @line_magic |
|
507 | @line_magic | |
509 | def edit(self, parameter_s='',last_call=['','']): |
|
508 | def edit(self, parameter_s='',last_call=['','']): | |
510 | """Bring up an editor and execute the resulting code. |
|
509 | """Bring up an editor and execute the resulting code. | |
511 |
|
510 | |||
512 | Usage: |
|
511 | Usage: | |
513 | %edit [options] [args] |
|
512 | %edit [options] [args] | |
514 |
|
513 | |||
515 | %edit runs IPython's editor hook. The default version of this hook is |
|
514 | %edit runs IPython's editor hook. The default version of this hook is | |
516 | set to call the editor specified by your $EDITOR environment variable. |
|
515 | set to call the editor specified by your $EDITOR environment variable. | |
517 | If this isn't found, it will default to vi under Linux/Unix and to |
|
516 | If this isn't found, it will default to vi under Linux/Unix and to | |
518 | notepad under Windows. See the end of this docstring for how to change |
|
517 | notepad under Windows. See the end of this docstring for how to change | |
519 | the editor hook. |
|
518 | the editor hook. | |
520 |
|
519 | |||
521 | You can also set the value of this editor via the |
|
520 | You can also set the value of this editor via the | |
522 | ``TerminalInteractiveShell.editor`` option in your configuration file. |
|
521 | ``TerminalInteractiveShell.editor`` option in your configuration file. | |
523 | This is useful if you wish to use a different editor from your typical |
|
522 | This is useful if you wish to use a different editor from your typical | |
524 | default with IPython (and for Windows users who typically don't set |
|
523 | default with IPython (and for Windows users who typically don't set | |
525 | environment variables). |
|
524 | environment variables). | |
526 |
|
525 | |||
527 | This command allows you to conveniently edit multi-line code right in |
|
526 | This command allows you to conveniently edit multi-line code right in | |
528 | your IPython session. |
|
527 | your IPython session. | |
529 |
|
528 | |||
530 | If called without arguments, %edit opens up an empty editor with a |
|
529 | If called without arguments, %edit opens up an empty editor with a | |
531 | temporary file and will execute the contents of this file when you |
|
530 | temporary file and will execute the contents of this file when you | |
532 | close it (don't forget to save it!). |
|
531 | close it (don't forget to save it!). | |
533 |
|
532 | |||
534 |
|
533 | |||
535 | Options: |
|
534 | Options: | |
536 |
|
535 | |||
537 | -n <number>: open the editor at a specified line number. By default, |
|
536 | -n <number>: open the editor at a specified line number. By default, | |
538 | the IPython editor hook uses the unix syntax 'editor +N filename', but |
|
537 | the IPython editor hook uses the unix syntax 'editor +N filename', but | |
539 | you can configure this by providing your own modified hook if your |
|
538 | you can configure this by providing your own modified hook if your | |
540 | favorite editor supports line-number specifications with a different |
|
539 | favorite editor supports line-number specifications with a different | |
541 | syntax. |
|
540 | syntax. | |
542 |
|
541 | |||
543 | -p: this will call the editor with the same data as the previous time |
|
542 | -p: this will call the editor with the same data as the previous time | |
544 | it was used, regardless of how long ago (in your current session) it |
|
543 | it was used, regardless of how long ago (in your current session) it | |
545 | was. |
|
544 | was. | |
546 |
|
545 | |||
547 | -r: use 'raw' input. This option only applies to input taken from the |
|
546 | -r: use 'raw' input. This option only applies to input taken from the | |
548 | user's history. By default, the 'processed' history is used, so that |
|
547 | user's history. By default, the 'processed' history is used, so that | |
549 | magics are loaded in their transformed version to valid Python. If |
|
548 | magics are loaded in their transformed version to valid Python. If | |
550 | this option is given, the raw input as typed as the command line is |
|
549 | this option is given, the raw input as typed as the command line is | |
551 | used instead. When you exit the editor, it will be executed by |
|
550 | used instead. When you exit the editor, it will be executed by | |
552 | IPython's own processor. |
|
551 | IPython's own processor. | |
553 |
|
552 | |||
554 | -x: do not execute the edited code immediately upon exit. This is |
|
553 | -x: do not execute the edited code immediately upon exit. This is | |
555 | mainly useful if you are editing programs which need to be called with |
|
554 | mainly useful if you are editing programs which need to be called with | |
556 | command line arguments, which you can then do using %run. |
|
555 | command line arguments, which you can then do using %run. | |
557 |
|
556 | |||
558 |
|
557 | |||
559 | Arguments: |
|
558 | Arguments: | |
560 |
|
559 | |||
561 | If arguments are given, the following possibilities exist: |
|
560 | If arguments are given, the following possibilities exist: | |
562 |
|
561 | |||
563 | - If the argument is a filename, IPython will load that into the |
|
562 | - If the argument is a filename, IPython will load that into the | |
564 | editor. It will execute its contents with execfile() when you exit, |
|
563 | editor. It will execute its contents with execfile() when you exit, | |
565 | loading any code in the file into your interactive namespace. |
|
564 | loading any code in the file into your interactive namespace. | |
566 |
|
565 | |||
567 | - The arguments are ranges of input history, e.g. "7 ~1/4-6". |
|
566 | - The arguments are ranges of input history, e.g. "7 ~1/4-6". | |
568 | The syntax is the same as in the %history magic. |
|
567 | The syntax is the same as in the %history magic. | |
569 |
|
568 | |||
570 | - If the argument is a string variable, its contents are loaded |
|
569 | - If the argument is a string variable, its contents are loaded | |
571 | into the editor. You can thus edit any string which contains |
|
570 | into the editor. You can thus edit any string which contains | |
572 | python code (including the result of previous edits). |
|
571 | python code (including the result of previous edits). | |
573 |
|
572 | |||
574 | - If the argument is the name of an object (other than a string), |
|
573 | - If the argument is the name of an object (other than a string), | |
575 | IPython will try to locate the file where it was defined and open the |
|
574 | IPython will try to locate the file where it was defined and open the | |
576 | editor at the point where it is defined. You can use `%edit function` |
|
575 | editor at the point where it is defined. You can use `%edit function` | |
577 | to load an editor exactly at the point where 'function' is defined, |
|
576 | to load an editor exactly at the point where 'function' is defined, | |
578 | edit it and have the file be executed automatically. |
|
577 | edit it and have the file be executed automatically. | |
579 |
|
578 | |||
580 | - If the object is a macro (see %macro for details), this opens up your |
|
579 | - If the object is a macro (see %macro for details), this opens up your | |
581 | specified editor with a temporary file containing the macro's data. |
|
580 | specified editor with a temporary file containing the macro's data. | |
582 | Upon exit, the macro is reloaded with the contents of the file. |
|
581 | Upon exit, the macro is reloaded with the contents of the file. | |
583 |
|
582 | |||
584 | Note: opening at an exact line is only supported under Unix, and some |
|
583 | Note: opening at an exact line is only supported under Unix, and some | |
585 | editors (like kedit and gedit up to Gnome 2.8) do not understand the |
|
584 | editors (like kedit and gedit up to Gnome 2.8) do not understand the | |
586 | '+NUMBER' parameter necessary for this feature. Good editors like |
|
585 | '+NUMBER' parameter necessary for this feature. Good editors like | |
587 | (X)Emacs, vi, jed, pico and joe all do. |
|
586 | (X)Emacs, vi, jed, pico and joe all do. | |
588 |
|
587 | |||
589 | After executing your code, %edit will return as output the code you |
|
588 | After executing your code, %edit will return as output the code you | |
590 | typed in the editor (except when it was an existing file). This way |
|
589 | typed in the editor (except when it was an existing file). This way | |
591 | you can reload the code in further invocations of %edit as a variable, |
|
590 | you can reload the code in further invocations of %edit as a variable, | |
592 | via _<NUMBER> or Out[<NUMBER>], where <NUMBER> is the prompt number of |
|
591 | via _<NUMBER> or Out[<NUMBER>], where <NUMBER> is the prompt number of | |
593 | the output. |
|
592 | the output. | |
594 |
|
593 | |||
595 | Note that %edit is also available through the alias %ed. |
|
594 | Note that %edit is also available through the alias %ed. | |
596 |
|
595 | |||
597 | This is an example of creating a simple function inside the editor and |
|
596 | This is an example of creating a simple function inside the editor and | |
598 | then modifying it. First, start up the editor:: |
|
597 | then modifying it. First, start up the editor:: | |
599 |
|
598 | |||
600 | In [1]: edit |
|
599 | In [1]: edit | |
601 | Editing... done. Executing edited code... |
|
600 | Editing... done. Executing edited code... | |
602 | Out[1]: 'def foo():\\n print "foo() was defined in an editing |
|
601 | Out[1]: 'def foo():\\n print "foo() was defined in an editing | |
603 | session"\\n' |
|
602 | session"\\n' | |
604 |
|
603 | |||
605 | We can then call the function foo():: |
|
604 | We can then call the function foo():: | |
606 |
|
605 | |||
607 | In [2]: foo() |
|
606 | In [2]: foo() | |
608 | foo() was defined in an editing session |
|
607 | foo() was defined in an editing session | |
609 |
|
608 | |||
610 | Now we edit foo. IPython automatically loads the editor with the |
|
609 | Now we edit foo. IPython automatically loads the editor with the | |
611 | (temporary) file where foo() was previously defined:: |
|
610 | (temporary) file where foo() was previously defined:: | |
612 |
|
611 | |||
613 | In [3]: edit foo |
|
612 | In [3]: edit foo | |
614 | Editing... done. Executing edited code... |
|
613 | Editing... done. Executing edited code... | |
615 |
|
614 | |||
616 | And if we call foo() again we get the modified version:: |
|
615 | And if we call foo() again we get the modified version:: | |
617 |
|
616 | |||
618 | In [4]: foo() |
|
617 | In [4]: foo() | |
619 | foo() has now been changed! |
|
618 | foo() has now been changed! | |
620 |
|
619 | |||
621 | Here is an example of how to edit a code snippet successive |
|
620 | Here is an example of how to edit a code snippet successive | |
622 | times. First we call the editor:: |
|
621 | times. First we call the editor:: | |
623 |
|
622 | |||
624 | In [5]: edit |
|
623 | In [5]: edit | |
625 | Editing... done. Executing edited code... |
|
624 | Editing... done. Executing edited code... | |
626 | hello |
|
625 | hello | |
627 | Out[5]: "print 'hello'\\n" |
|
626 | Out[5]: "print 'hello'\\n" | |
628 |
|
627 | |||
629 | Now we call it again with the previous output (stored in _):: |
|
628 | Now we call it again with the previous output (stored in _):: | |
630 |
|
629 | |||
631 | In [6]: edit _ |
|
630 | In [6]: edit _ | |
632 | Editing... done. Executing edited code... |
|
631 | Editing... done. Executing edited code... | |
633 | hello world |
|
632 | hello world | |
634 | Out[6]: "print 'hello world'\\n" |
|
633 | Out[6]: "print 'hello world'\\n" | |
635 |
|
634 | |||
636 | Now we call it with the output #8 (stored in _8, also as Out[8]):: |
|
635 | Now we call it with the output #8 (stored in _8, also as Out[8]):: | |
637 |
|
636 | |||
638 | In [7]: edit _8 |
|
637 | In [7]: edit _8 | |
639 | Editing... done. Executing edited code... |
|
638 | Editing... done. Executing edited code... | |
640 | hello again |
|
639 | hello again | |
641 | Out[7]: "print 'hello again'\\n" |
|
640 | Out[7]: "print 'hello again'\\n" | |
642 |
|
641 | |||
643 |
|
642 | |||
644 | Changing the default editor hook: |
|
643 | Changing the default editor hook: | |
645 |
|
644 | |||
646 | If you wish to write your own editor hook, you can put it in a |
|
645 | If you wish to write your own editor hook, you can put it in a | |
647 | configuration file which you load at startup time. The default hook |
|
646 | configuration file which you load at startup time. The default hook | |
648 | is defined in the IPython.core.hooks module, and you can use that as a |
|
647 | is defined in the IPython.core.hooks module, and you can use that as a | |
649 | starting example for further modifications. That file also has |
|
648 | starting example for further modifications. That file also has | |
650 | general instructions on how to set a new hook for use once you've |
|
649 | general instructions on how to set a new hook for use once you've | |
651 | defined it.""" |
|
650 | defined it.""" | |
652 | opts,args = self.parse_options(parameter_s,'prxn:') |
|
651 | opts,args = self.parse_options(parameter_s,'prxn:') | |
653 |
|
652 | |||
654 | try: |
|
653 | try: | |
655 | filename, lineno, is_temp = self._find_edit_target(self.shell, |
|
654 | filename, lineno, is_temp = self._find_edit_target(self.shell, | |
656 | args, opts, last_call) |
|
655 | args, opts, last_call) | |
657 | except MacroToEdit as e: |
|
656 | except MacroToEdit as e: | |
658 | self._edit_macro(args, e.args[0]) |
|
657 | self._edit_macro(args, e.args[0]) | |
659 | return |
|
658 | return | |
660 | except InteractivelyDefined as e: |
|
659 | except InteractivelyDefined as e: | |
661 | print("Editing In[%i]" % e.index) |
|
660 | print("Editing In[%i]" % e.index) | |
662 | args = str(e.index) |
|
661 | args = str(e.index) | |
663 | filename, lineno, is_temp = self._find_edit_target(self.shell, |
|
662 | filename, lineno, is_temp = self._find_edit_target(self.shell, | |
664 | args, opts, last_call) |
|
663 | args, opts, last_call) | |
665 | if filename is None: |
|
664 | if filename is None: | |
666 | # nothing was found, warnings have already been issued, |
|
665 | # nothing was found, warnings have already been issued, | |
667 | # just give up. |
|
666 | # just give up. | |
668 | return |
|
667 | return | |
669 |
|
668 | |||
670 | if is_temp: |
|
669 | if is_temp: | |
671 | self._knowntemps.add(filename) |
|
670 | self._knowntemps.add(filename) | |
672 | elif (filename in self._knowntemps): |
|
671 | elif (filename in self._knowntemps): | |
673 | is_temp = True |
|
672 | is_temp = True | |
674 |
|
673 | |||
675 |
|
674 | |||
676 | # do actual editing here |
|
675 | # do actual editing here | |
677 | print('Editing...', end=' ') |
|
676 | print('Editing...', end=' ') | |
678 | sys.stdout.flush() |
|
677 | sys.stdout.flush() | |
679 | try: |
|
678 | try: | |
680 | # Quote filenames that may have spaces in them |
|
679 | # Quote filenames that may have spaces in them | |
681 | if ' ' in filename: |
|
680 | if ' ' in filename: | |
682 | filename = "'%s'" % filename |
|
681 | filename = "'%s'" % filename | |
683 | self.shell.hooks.editor(filename,lineno) |
|
682 | self.shell.hooks.editor(filename,lineno) | |
684 | except TryNext: |
|
683 | except TryNext: | |
685 | warn('Could not open editor') |
|
684 | warn('Could not open editor') | |
686 | return |
|
685 | return | |
687 |
|
686 | |||
688 | # XXX TODO: should this be generalized for all string vars? |
|
687 | # XXX TODO: should this be generalized for all string vars? | |
689 | # For now, this is special-cased to blocks created by cpaste |
|
688 | # For now, this is special-cased to blocks created by cpaste | |
690 | if args.strip() == 'pasted_block': |
|
689 | if args.strip() == 'pasted_block': | |
691 | with open(filename, 'r') as f: |
|
690 | with open(filename, 'r') as f: | |
692 | self.shell.user_ns['pasted_block'] = f.read() |
|
691 | self.shell.user_ns['pasted_block'] = f.read() | |
693 |
|
692 | |||
694 | if 'x' in opts: # -x prevents actual execution |
|
693 | if 'x' in opts: # -x prevents actual execution | |
695 | print() |
|
694 | print() | |
696 | else: |
|
695 | else: | |
697 | print('done. Executing edited code...') |
|
696 | print('done. Executing edited code...') | |
698 | with preserve_keys(self.shell.user_ns, '__file__'): |
|
697 | with preserve_keys(self.shell.user_ns, '__file__'): | |
699 | if not is_temp: |
|
698 | if not is_temp: | |
700 | self.shell.user_ns['__file__'] = filename |
|
699 | self.shell.user_ns['__file__'] = filename | |
701 | if 'r' in opts: # Untranslated IPython code |
|
700 | if 'r' in opts: # Untranslated IPython code | |
702 | with open(filename, 'r') as f: |
|
701 | with open(filename, 'r') as f: | |
703 | source = f.read() |
|
702 | source = f.read() | |
704 | self.shell.run_cell(source, store_history=False) |
|
703 | self.shell.run_cell(source, store_history=False) | |
705 | else: |
|
704 | else: | |
706 | self.shell.safe_execfile(filename, self.shell.user_ns, |
|
705 | self.shell.safe_execfile(filename, self.shell.user_ns, | |
707 | self.shell.user_ns) |
|
706 | self.shell.user_ns) | |
708 |
|
707 | |||
709 | if is_temp: |
|
708 | if is_temp: | |
710 | try: |
|
709 | try: | |
711 | return open(filename).read() |
|
710 | return open(filename).read() | |
712 | except IOError as msg: |
|
711 | except IOError as msg: | |
713 | if msg.filename == filename: |
|
712 | if msg.filename == filename: | |
714 | warn('File not found. Did you forget to save?') |
|
713 | warn('File not found. Did you forget to save?') | |
715 | return |
|
714 | return | |
716 | else: |
|
715 | else: | |
717 | self.shell.showtraceback() |
|
716 | self.shell.showtraceback() |
@@ -1,1364 +1,1362 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 | """Implementation of execution-related magic functions.""" |
|
2 | """Implementation of execution-related magic functions.""" | |
3 |
|
3 | |||
4 | # Copyright (c) IPython Development Team. |
|
4 | # Copyright (c) IPython Development Team. | |
5 | # Distributed under the terms of the Modified BSD License. |
|
5 | # Distributed under the terms of the Modified BSD License. | |
6 |
|
6 | |||
7 | from __future__ import print_function |
|
7 | from __future__ import print_function | |
8 | from __future__ import absolute_import |
|
8 | from __future__ import absolute_import | |
9 |
|
9 | |||
10 | import ast |
|
10 | import ast | |
11 | import bdb |
|
11 | import bdb | |
12 | import gc |
|
12 | import gc | |
13 | import itertools |
|
13 | import itertools | |
14 | import os |
|
14 | import os | |
15 | import sys |
|
15 | import sys | |
16 | import time |
|
16 | import time | |
17 | import timeit |
|
17 | import timeit | |
18 | from pdb import Restart |
|
18 | from pdb import Restart | |
19 |
|
19 | |||
20 | # cProfile was added in Python2.5 |
|
20 | # cProfile was added in Python2.5 | |
21 | try: |
|
21 | try: | |
22 | import cProfile as profile |
|
22 | import cProfile as profile | |
23 | import pstats |
|
23 | import pstats | |
24 | except ImportError: |
|
24 | except ImportError: | |
25 | # profile isn't bundled by default in Debian for license reasons |
|
25 | # profile isn't bundled by default in Debian for license reasons | |
26 | try: |
|
26 | try: | |
27 | import profile, pstats |
|
27 | import profile, pstats | |
28 | except ImportError: |
|
28 | except ImportError: | |
29 | profile = pstats = None |
|
29 | profile = pstats = None | |
30 |
|
30 | |||
31 | from IPython.core import debugger, oinspect |
|
31 | from IPython.core import debugger, oinspect | |
32 | from IPython.core import magic_arguments |
|
32 | from IPython.core import magic_arguments | |
33 | from IPython.core import page |
|
33 | from IPython.core import page | |
34 | from IPython.core.error import UsageError |
|
34 | from IPython.core.error import UsageError | |
35 | from IPython.core.macro import Macro |
|
35 | from IPython.core.macro import Macro | |
36 | from IPython.core.magic import (Magics, magics_class, line_magic, cell_magic, |
|
36 | from IPython.core.magic import (Magics, magics_class, line_magic, cell_magic, | |
37 | line_cell_magic, on_off, needs_local_scope) |
|
37 | line_cell_magic, on_off, needs_local_scope) | |
38 | from IPython.testing.skipdoctest import skip_doctest |
|
38 | from IPython.testing.skipdoctest import skip_doctest | |
39 | from IPython.utils import py3compat |
|
39 | from IPython.utils import py3compat | |
40 | from IPython.utils.py3compat import builtin_mod, iteritems, PY3 |
|
40 | from IPython.utils.py3compat import builtin_mod, iteritems, PY3 | |
41 | from IPython.utils.contexts import preserve_keys |
|
41 | from IPython.utils.contexts import preserve_keys | |
42 | from IPython.utils.capture import capture_output |
|
42 | from IPython.utils.capture import capture_output | |
43 | from IPython.utils.ipstruct import Struct |
|
43 | from IPython.utils.ipstruct import Struct | |
44 | from IPython.utils.module_paths import find_mod |
|
44 | from IPython.utils.module_paths import find_mod | |
45 |
from IPython.utils.path import get_py_filename, |
|
45 | from IPython.utils.path import get_py_filename, shellglob | |
46 | from IPython.utils.timing import clock, clock2 |
|
46 | from IPython.utils.timing import clock, clock2 | |
47 | from warnings import warn |
|
47 | from warnings import warn | |
48 | from logging import error |
|
48 | from logging import error | |
49 |
|
49 | |||
50 | if PY3: |
|
50 | if PY3: | |
51 | from io import StringIO |
|
51 | from io import StringIO | |
52 | else: |
|
52 | else: | |
53 | from StringIO import StringIO |
|
53 | from StringIO import StringIO | |
54 |
|
54 | |||
55 | #----------------------------------------------------------------------------- |
|
55 | #----------------------------------------------------------------------------- | |
56 | # Magic implementation classes |
|
56 | # Magic implementation classes | |
57 | #----------------------------------------------------------------------------- |
|
57 | #----------------------------------------------------------------------------- | |
58 |
|
58 | |||
59 |
|
59 | |||
60 | class TimeitResult(object): |
|
60 | class TimeitResult(object): | |
61 | """ |
|
61 | """ | |
62 | Object returned by the timeit magic with info about the run. |
|
62 | Object returned by the timeit magic with info about the run. | |
63 |
|
63 | |||
64 | Contains the following attributes : |
|
64 | Contains the following attributes : | |
65 |
|
65 | |||
66 | loops: (int) number of loops done per measurement |
|
66 | loops: (int) number of loops done per measurement | |
67 | repeat: (int) number of times the measurement has been repeated |
|
67 | repeat: (int) number of times the measurement has been repeated | |
68 | best: (float) best execution time / number |
|
68 | best: (float) best execution time / number | |
69 | all_runs: (list of float) execution time of each run (in s) |
|
69 | all_runs: (list of float) execution time of each run (in s) | |
70 | compile_time: (float) time of statement compilation (s) |
|
70 | compile_time: (float) time of statement compilation (s) | |
71 |
|
71 | |||
72 | """ |
|
72 | """ | |
73 |
|
73 | |||
74 | def __init__(self, loops, repeat, best, worst, all_runs, compile_time, precision): |
|
74 | def __init__(self, loops, repeat, best, worst, all_runs, compile_time, precision): | |
75 | self.loops = loops |
|
75 | self.loops = loops | |
76 | self.repeat = repeat |
|
76 | self.repeat = repeat | |
77 | self.best = best |
|
77 | self.best = best | |
78 | self.worst = worst |
|
78 | self.worst = worst | |
79 | self.all_runs = all_runs |
|
79 | self.all_runs = all_runs | |
80 | self.compile_time = compile_time |
|
80 | self.compile_time = compile_time | |
81 | self._precision = precision |
|
81 | self._precision = precision | |
82 |
|
82 | |||
83 | def _repr_pretty_(self, p , cycle): |
|
83 | def _repr_pretty_(self, p , cycle): | |
84 | if self.loops == 1: # No s at "loops" if only one loop |
|
84 | if self.loops == 1: # No s at "loops" if only one loop | |
85 | unic = u"%d loop, best of %d: %s per loop" % (self.loops, self.repeat, |
|
85 | unic = u"%d loop, best of %d: %s per loop" % (self.loops, self.repeat, | |
86 | _format_time(self.best, self._precision)) |
|
86 | _format_time(self.best, self._precision)) | |
87 | else: |
|
87 | else: | |
88 | unic = u"%d loops, best of %d: %s per loop" % (self.loops, self.repeat, |
|
88 | unic = u"%d loops, best of %d: %s per loop" % (self.loops, self.repeat, | |
89 | _format_time(self.best, self._precision)) |
|
89 | _format_time(self.best, self._precision)) | |
90 | p.text(u'<TimeitResult : '+unic+u'>') |
|
90 | p.text(u'<TimeitResult : '+unic+u'>') | |
91 |
|
91 | |||
92 |
|
92 | |||
93 | class TimeitTemplateFiller(ast.NodeTransformer): |
|
93 | class TimeitTemplateFiller(ast.NodeTransformer): | |
94 | """Fill in the AST template for timing execution. |
|
94 | """Fill in the AST template for timing execution. | |
95 |
|
95 | |||
96 | This is quite closely tied to the template definition, which is in |
|
96 | This is quite closely tied to the template definition, which is in | |
97 | :meth:`ExecutionMagics.timeit`. |
|
97 | :meth:`ExecutionMagics.timeit`. | |
98 | """ |
|
98 | """ | |
99 | def __init__(self, ast_setup, ast_stmt): |
|
99 | def __init__(self, ast_setup, ast_stmt): | |
100 | self.ast_setup = ast_setup |
|
100 | self.ast_setup = ast_setup | |
101 | self.ast_stmt = ast_stmt |
|
101 | self.ast_stmt = ast_stmt | |
102 |
|
102 | |||
103 | def visit_FunctionDef(self, node): |
|
103 | def visit_FunctionDef(self, node): | |
104 | "Fill in the setup statement" |
|
104 | "Fill in the setup statement" | |
105 | self.generic_visit(node) |
|
105 | self.generic_visit(node) | |
106 | if node.name == "inner": |
|
106 | if node.name == "inner": | |
107 | node.body[:1] = self.ast_setup.body |
|
107 | node.body[:1] = self.ast_setup.body | |
108 |
|
108 | |||
109 | return node |
|
109 | return node | |
110 |
|
110 | |||
111 | def visit_For(self, node): |
|
111 | def visit_For(self, node): | |
112 | "Fill in the statement to be timed" |
|
112 | "Fill in the statement to be timed" | |
113 | if getattr(getattr(node.body[0], 'value', None), 'id', None) == 'stmt': |
|
113 | if getattr(getattr(node.body[0], 'value', None), 'id', None) == 'stmt': | |
114 | node.body = self.ast_stmt.body |
|
114 | node.body = self.ast_stmt.body | |
115 | return node |
|
115 | return node | |
116 |
|
116 | |||
117 |
|
117 | |||
118 | class Timer(timeit.Timer): |
|
118 | class Timer(timeit.Timer): | |
119 | """Timer class that explicitly uses self.inner |
|
119 | """Timer class that explicitly uses self.inner | |
120 |
|
120 | |||
121 | which is an undocumented implementation detail of CPython, |
|
121 | which is an undocumented implementation detail of CPython, | |
122 | not shared by PyPy. |
|
122 | not shared by PyPy. | |
123 | """ |
|
123 | """ | |
124 | # Timer.timeit copied from CPython 3.4.2 |
|
124 | # Timer.timeit copied from CPython 3.4.2 | |
125 | def timeit(self, number=timeit.default_number): |
|
125 | def timeit(self, number=timeit.default_number): | |
126 | """Time 'number' executions of the main statement. |
|
126 | """Time 'number' executions of the main statement. | |
127 |
|
127 | |||
128 | To be precise, this executes the setup statement once, and |
|
128 | To be precise, this executes the setup statement once, and | |
129 | then returns the time it takes to execute the main statement |
|
129 | then returns the time it takes to execute the main statement | |
130 | a number of times, as a float measured in seconds. The |
|
130 | a number of times, as a float measured in seconds. The | |
131 | argument is the number of times through the loop, defaulting |
|
131 | argument is the number of times through the loop, defaulting | |
132 | to one million. The main statement, the setup statement and |
|
132 | to one million. The main statement, the setup statement and | |
133 | the timer function to be used are passed to the constructor. |
|
133 | the timer function to be used are passed to the constructor. | |
134 | """ |
|
134 | """ | |
135 | it = itertools.repeat(None, number) |
|
135 | it = itertools.repeat(None, number) | |
136 | gcold = gc.isenabled() |
|
136 | gcold = gc.isenabled() | |
137 | gc.disable() |
|
137 | gc.disable() | |
138 | try: |
|
138 | try: | |
139 | timing = self.inner(it, self.timer) |
|
139 | timing = self.inner(it, self.timer) | |
140 | finally: |
|
140 | finally: | |
141 | if gcold: |
|
141 | if gcold: | |
142 | gc.enable() |
|
142 | gc.enable() | |
143 | return timing |
|
143 | return timing | |
144 |
|
144 | |||
145 |
|
145 | |||
146 | @magics_class |
|
146 | @magics_class | |
147 | class ExecutionMagics(Magics): |
|
147 | class ExecutionMagics(Magics): | |
148 | """Magics related to code execution, debugging, profiling, etc. |
|
148 | """Magics related to code execution, debugging, profiling, etc. | |
149 |
|
149 | |||
150 | """ |
|
150 | """ | |
151 |
|
151 | |||
152 | def __init__(self, shell): |
|
152 | def __init__(self, shell): | |
153 | super(ExecutionMagics, self).__init__(shell) |
|
153 | super(ExecutionMagics, self).__init__(shell) | |
154 | if profile is None: |
|
154 | if profile is None: | |
155 | self.prun = self.profile_missing_notice |
|
155 | self.prun = self.profile_missing_notice | |
156 | # Default execution function used to actually run user code. |
|
156 | # Default execution function used to actually run user code. | |
157 | self.default_runner = None |
|
157 | self.default_runner = None | |
158 |
|
158 | |||
159 | def profile_missing_notice(self, *args, **kwargs): |
|
159 | def profile_missing_notice(self, *args, **kwargs): | |
160 | error("""\ |
|
160 | error("""\ | |
161 | The profile module could not be found. It has been removed from the standard |
|
161 | The profile module could not be found. It has been removed from the standard | |
162 | python packages because of its non-free license. To use profiling, install the |
|
162 | python packages because of its non-free license. To use profiling, install the | |
163 | python-profiler package from non-free.""") |
|
163 | python-profiler package from non-free.""") | |
164 |
|
164 | |||
165 | @skip_doctest |
|
165 | @skip_doctest | |
166 | @line_cell_magic |
|
166 | @line_cell_magic | |
167 | def prun(self, parameter_s='', cell=None): |
|
167 | def prun(self, parameter_s='', cell=None): | |
168 |
|
168 | |||
169 | """Run a statement through the python code profiler. |
|
169 | """Run a statement through the python code profiler. | |
170 |
|
170 | |||
171 | Usage, in line mode: |
|
171 | Usage, in line mode: | |
172 | %prun [options] statement |
|
172 | %prun [options] statement | |
173 |
|
173 | |||
174 | Usage, in cell mode: |
|
174 | Usage, in cell mode: | |
175 | %%prun [options] [statement] |
|
175 | %%prun [options] [statement] | |
176 | code... |
|
176 | code... | |
177 | code... |
|
177 | code... | |
178 |
|
178 | |||
179 | In cell mode, the additional code lines are appended to the (possibly |
|
179 | In cell mode, the additional code lines are appended to the (possibly | |
180 | empty) statement in the first line. Cell mode allows you to easily |
|
180 | empty) statement in the first line. Cell mode allows you to easily | |
181 | profile multiline blocks without having to put them in a separate |
|
181 | profile multiline blocks without having to put them in a separate | |
182 | function. |
|
182 | function. | |
183 |
|
183 | |||
184 | The given statement (which doesn't require quote marks) is run via the |
|
184 | The given statement (which doesn't require quote marks) is run via the | |
185 | python profiler in a manner similar to the profile.run() function. |
|
185 | python profiler in a manner similar to the profile.run() function. | |
186 | Namespaces are internally managed to work correctly; profile.run |
|
186 | Namespaces are internally managed to work correctly; profile.run | |
187 | cannot be used in IPython because it makes certain assumptions about |
|
187 | cannot be used in IPython because it makes certain assumptions about | |
188 | namespaces which do not hold under IPython. |
|
188 | namespaces which do not hold under IPython. | |
189 |
|
189 | |||
190 | Options: |
|
190 | Options: | |
191 |
|
191 | |||
192 | -l <limit> |
|
192 | -l <limit> | |
193 | you can place restrictions on what or how much of the |
|
193 | you can place restrictions on what or how much of the | |
194 | profile gets printed. The limit value can be: |
|
194 | profile gets printed. The limit value can be: | |
195 |
|
195 | |||
196 | * A string: only information for function names containing this string |
|
196 | * A string: only information for function names containing this string | |
197 | is printed. |
|
197 | is printed. | |
198 |
|
198 | |||
199 | * An integer: only these many lines are printed. |
|
199 | * An integer: only these many lines are printed. | |
200 |
|
200 | |||
201 | * A float (between 0 and 1): this fraction of the report is printed |
|
201 | * A float (between 0 and 1): this fraction of the report is printed | |
202 | (for example, use a limit of 0.4 to see the topmost 40% only). |
|
202 | (for example, use a limit of 0.4 to see the topmost 40% only). | |
203 |
|
203 | |||
204 | You can combine several limits with repeated use of the option. For |
|
204 | You can combine several limits with repeated use of the option. For | |
205 | example, ``-l __init__ -l 5`` will print only the topmost 5 lines of |
|
205 | example, ``-l __init__ -l 5`` will print only the topmost 5 lines of | |
206 | information about class constructors. |
|
206 | information about class constructors. | |
207 |
|
207 | |||
208 | -r |
|
208 | -r | |
209 | return the pstats.Stats object generated by the profiling. This |
|
209 | return the pstats.Stats object generated by the profiling. This | |
210 | object has all the information about the profile in it, and you can |
|
210 | object has all the information about the profile in it, and you can | |
211 | later use it for further analysis or in other functions. |
|
211 | later use it for further analysis or in other functions. | |
212 |
|
212 | |||
213 | -s <key> |
|
213 | -s <key> | |
214 | sort profile by given key. You can provide more than one key |
|
214 | sort profile by given key. You can provide more than one key | |
215 | by using the option several times: '-s key1 -s key2 -s key3...'. The |
|
215 | by using the option several times: '-s key1 -s key2 -s key3...'. The | |
216 | default sorting key is 'time'. |
|
216 | default sorting key is 'time'. | |
217 |
|
217 | |||
218 | The following is copied verbatim from the profile documentation |
|
218 | The following is copied verbatim from the profile documentation | |
219 | referenced below: |
|
219 | referenced below: | |
220 |
|
220 | |||
221 | When more than one key is provided, additional keys are used as |
|
221 | When more than one key is provided, additional keys are used as | |
222 | secondary criteria when the there is equality in all keys selected |
|
222 | secondary criteria when the there is equality in all keys selected | |
223 | before them. |
|
223 | before them. | |
224 |
|
224 | |||
225 | Abbreviations can be used for any key names, as long as the |
|
225 | Abbreviations can be used for any key names, as long as the | |
226 | abbreviation is unambiguous. The following are the keys currently |
|
226 | abbreviation is unambiguous. The following are the keys currently | |
227 | defined: |
|
227 | defined: | |
228 |
|
228 | |||
229 | ============ ===================== |
|
229 | ============ ===================== | |
230 | Valid Arg Meaning |
|
230 | Valid Arg Meaning | |
231 | ============ ===================== |
|
231 | ============ ===================== | |
232 | "calls" call count |
|
232 | "calls" call count | |
233 | "cumulative" cumulative time |
|
233 | "cumulative" cumulative time | |
234 | "file" file name |
|
234 | "file" file name | |
235 | "module" file name |
|
235 | "module" file name | |
236 | "pcalls" primitive call count |
|
236 | "pcalls" primitive call count | |
237 | "line" line number |
|
237 | "line" line number | |
238 | "name" function name |
|
238 | "name" function name | |
239 | "nfl" name/file/line |
|
239 | "nfl" name/file/line | |
240 | "stdname" standard name |
|
240 | "stdname" standard name | |
241 | "time" internal time |
|
241 | "time" internal time | |
242 | ============ ===================== |
|
242 | ============ ===================== | |
243 |
|
243 | |||
244 | Note that all sorts on statistics are in descending order (placing |
|
244 | Note that all sorts on statistics are in descending order (placing | |
245 | most time consuming items first), where as name, file, and line number |
|
245 | most time consuming items first), where as name, file, and line number | |
246 | searches are in ascending order (i.e., alphabetical). The subtle |
|
246 | searches are in ascending order (i.e., alphabetical). The subtle | |
247 | distinction between "nfl" and "stdname" is that the standard name is a |
|
247 | distinction between "nfl" and "stdname" is that the standard name is a | |
248 | sort of the name as printed, which means that the embedded line |
|
248 | sort of the name as printed, which means that the embedded line | |
249 | numbers get compared in an odd way. For example, lines 3, 20, and 40 |
|
249 | numbers get compared in an odd way. For example, lines 3, 20, and 40 | |
250 | would (if the file names were the same) appear in the string order |
|
250 | would (if the file names were the same) appear in the string order | |
251 | "20" "3" and "40". In contrast, "nfl" does a numeric compare of the |
|
251 | "20" "3" and "40". In contrast, "nfl" does a numeric compare of the | |
252 | line numbers. In fact, sort_stats("nfl") is the same as |
|
252 | line numbers. In fact, sort_stats("nfl") is the same as | |
253 | sort_stats("name", "file", "line"). |
|
253 | sort_stats("name", "file", "line"). | |
254 |
|
254 | |||
255 | -T <filename> |
|
255 | -T <filename> | |
256 | save profile results as shown on screen to a text |
|
256 | save profile results as shown on screen to a text | |
257 | file. The profile is still shown on screen. |
|
257 | file. The profile is still shown on screen. | |
258 |
|
258 | |||
259 | -D <filename> |
|
259 | -D <filename> | |
260 | save (via dump_stats) profile statistics to given |
|
260 | save (via dump_stats) profile statistics to given | |
261 | filename. This data is in a format understood by the pstats module, and |
|
261 | filename. This data is in a format understood by the pstats module, and | |
262 | is generated by a call to the dump_stats() method of profile |
|
262 | is generated by a call to the dump_stats() method of profile | |
263 | objects. The profile is still shown on screen. |
|
263 | objects. The profile is still shown on screen. | |
264 |
|
264 | |||
265 | -q |
|
265 | -q | |
266 | suppress output to the pager. Best used with -T and/or -D above. |
|
266 | suppress output to the pager. Best used with -T and/or -D above. | |
267 |
|
267 | |||
268 | If you want to run complete programs under the profiler's control, use |
|
268 | If you want to run complete programs under the profiler's control, use | |
269 | ``%run -p [prof_opts] filename.py [args to program]`` where prof_opts |
|
269 | ``%run -p [prof_opts] filename.py [args to program]`` where prof_opts | |
270 | contains profiler specific options as described here. |
|
270 | contains profiler specific options as described here. | |
271 |
|
271 | |||
272 | You can read the complete documentation for the profile module with:: |
|
272 | You can read the complete documentation for the profile module with:: | |
273 |
|
273 | |||
274 | In [1]: import profile; profile.help() |
|
274 | In [1]: import profile; profile.help() | |
275 | """ |
|
275 | """ | |
276 | opts, arg_str = self.parse_options(parameter_s, 'D:l:rs:T:q', |
|
276 | opts, arg_str = self.parse_options(parameter_s, 'D:l:rs:T:q', | |
277 | list_all=True, posix=False) |
|
277 | list_all=True, posix=False) | |
278 | if cell is not None: |
|
278 | if cell is not None: | |
279 | arg_str += '\n' + cell |
|
279 | arg_str += '\n' + cell | |
280 | arg_str = self.shell.input_splitter.transform_cell(arg_str) |
|
280 | arg_str = self.shell.input_splitter.transform_cell(arg_str) | |
281 | return self._run_with_profiler(arg_str, opts, self.shell.user_ns) |
|
281 | return self._run_with_profiler(arg_str, opts, self.shell.user_ns) | |
282 |
|
282 | |||
283 | def _run_with_profiler(self, code, opts, namespace): |
|
283 | def _run_with_profiler(self, code, opts, namespace): | |
284 | """ |
|
284 | """ | |
285 | Run `code` with profiler. Used by ``%prun`` and ``%run -p``. |
|
285 | Run `code` with profiler. Used by ``%prun`` and ``%run -p``. | |
286 |
|
286 | |||
287 | Parameters |
|
287 | Parameters | |
288 | ---------- |
|
288 | ---------- | |
289 | code : str |
|
289 | code : str | |
290 | Code to be executed. |
|
290 | Code to be executed. | |
291 | opts : Struct |
|
291 | opts : Struct | |
292 | Options parsed by `self.parse_options`. |
|
292 | Options parsed by `self.parse_options`. | |
293 | namespace : dict |
|
293 | namespace : dict | |
294 | A dictionary for Python namespace (e.g., `self.shell.user_ns`). |
|
294 | A dictionary for Python namespace (e.g., `self.shell.user_ns`). | |
295 |
|
295 | |||
296 | """ |
|
296 | """ | |
297 |
|
297 | |||
298 | # Fill default values for unspecified options: |
|
298 | # Fill default values for unspecified options: | |
299 | opts.merge(Struct(D=[''], l=[], s=['time'], T=[''])) |
|
299 | opts.merge(Struct(D=[''], l=[], s=['time'], T=[''])) | |
300 |
|
300 | |||
301 | prof = profile.Profile() |
|
301 | prof = profile.Profile() | |
302 | try: |
|
302 | try: | |
303 | prof = prof.runctx(code, namespace, namespace) |
|
303 | prof = prof.runctx(code, namespace, namespace) | |
304 | sys_exit = '' |
|
304 | sys_exit = '' | |
305 | except SystemExit: |
|
305 | except SystemExit: | |
306 | sys_exit = """*** SystemExit exception caught in code being profiled.""" |
|
306 | sys_exit = """*** SystemExit exception caught in code being profiled.""" | |
307 |
|
307 | |||
308 | stats = pstats.Stats(prof).strip_dirs().sort_stats(*opts.s) |
|
308 | stats = pstats.Stats(prof).strip_dirs().sort_stats(*opts.s) | |
309 |
|
309 | |||
310 | lims = opts.l |
|
310 | lims = opts.l | |
311 | if lims: |
|
311 | if lims: | |
312 | lims = [] # rebuild lims with ints/floats/strings |
|
312 | lims = [] # rebuild lims with ints/floats/strings | |
313 | for lim in opts.l: |
|
313 | for lim in opts.l: | |
314 | try: |
|
314 | try: | |
315 | lims.append(int(lim)) |
|
315 | lims.append(int(lim)) | |
316 | except ValueError: |
|
316 | except ValueError: | |
317 | try: |
|
317 | try: | |
318 | lims.append(float(lim)) |
|
318 | lims.append(float(lim)) | |
319 | except ValueError: |
|
319 | except ValueError: | |
320 | lims.append(lim) |
|
320 | lims.append(lim) | |
321 |
|
321 | |||
322 | # Trap output. |
|
322 | # Trap output. | |
323 | stdout_trap = StringIO() |
|
323 | stdout_trap = StringIO() | |
324 | stats_stream = stats.stream |
|
324 | stats_stream = stats.stream | |
325 | try: |
|
325 | try: | |
326 | stats.stream = stdout_trap |
|
326 | stats.stream = stdout_trap | |
327 | stats.print_stats(*lims) |
|
327 | stats.print_stats(*lims) | |
328 | finally: |
|
328 | finally: | |
329 | stats.stream = stats_stream |
|
329 | stats.stream = stats_stream | |
330 |
|
330 | |||
331 | output = stdout_trap.getvalue() |
|
331 | output = stdout_trap.getvalue() | |
332 | output = output.rstrip() |
|
332 | output = output.rstrip() | |
333 |
|
333 | |||
334 | if 'q' not in opts: |
|
334 | if 'q' not in opts: | |
335 | page.page(output) |
|
335 | page.page(output) | |
336 | print(sys_exit, end=' ') |
|
336 | print(sys_exit, end=' ') | |
337 |
|
337 | |||
338 | dump_file = opts.D[0] |
|
338 | dump_file = opts.D[0] | |
339 | text_file = opts.T[0] |
|
339 | text_file = opts.T[0] | |
340 | if dump_file: |
|
340 | if dump_file: | |
341 | dump_file = unquote_filename(dump_file) |
|
|||
342 | prof.dump_stats(dump_file) |
|
341 | prof.dump_stats(dump_file) | |
343 | print('\n*** Profile stats marshalled to file',\ |
|
342 | print('\n*** Profile stats marshalled to file',\ | |
344 | repr(dump_file)+'.',sys_exit) |
|
343 | repr(dump_file)+'.',sys_exit) | |
345 | if text_file: |
|
344 | if text_file: | |
346 | text_file = unquote_filename(text_file) |
|
|||
347 | pfile = open(text_file,'w') |
|
345 | pfile = open(text_file,'w') | |
348 | pfile.write(output) |
|
346 | pfile.write(output) | |
349 | pfile.close() |
|
347 | pfile.close() | |
350 | print('\n*** Profile printout saved to text file',\ |
|
348 | print('\n*** Profile printout saved to text file',\ | |
351 | repr(text_file)+'.',sys_exit) |
|
349 | repr(text_file)+'.',sys_exit) | |
352 |
|
350 | |||
353 | if 'r' in opts: |
|
351 | if 'r' in opts: | |
354 | return stats |
|
352 | return stats | |
355 | else: |
|
353 | else: | |
356 | return None |
|
354 | return None | |
357 |
|
355 | |||
358 | @line_magic |
|
356 | @line_magic | |
359 | def pdb(self, parameter_s=''): |
|
357 | def pdb(self, parameter_s=''): | |
360 | """Control the automatic calling of the pdb interactive debugger. |
|
358 | """Control the automatic calling of the pdb interactive debugger. | |
361 |
|
359 | |||
362 | Call as '%pdb on', '%pdb 1', '%pdb off' or '%pdb 0'. If called without |
|
360 | Call as '%pdb on', '%pdb 1', '%pdb off' or '%pdb 0'. If called without | |
363 | argument it works as a toggle. |
|
361 | argument it works as a toggle. | |
364 |
|
362 | |||
365 | When an exception is triggered, IPython can optionally call the |
|
363 | When an exception is triggered, IPython can optionally call the | |
366 | interactive pdb debugger after the traceback printout. %pdb toggles |
|
364 | interactive pdb debugger after the traceback printout. %pdb toggles | |
367 | this feature on and off. |
|
365 | this feature on and off. | |
368 |
|
366 | |||
369 | The initial state of this feature is set in your configuration |
|
367 | The initial state of this feature is set in your configuration | |
370 | file (the option is ``InteractiveShell.pdb``). |
|
368 | file (the option is ``InteractiveShell.pdb``). | |
371 |
|
369 | |||
372 | If you want to just activate the debugger AFTER an exception has fired, |
|
370 | If you want to just activate the debugger AFTER an exception has fired, | |
373 | without having to type '%pdb on' and rerunning your code, you can use |
|
371 | without having to type '%pdb on' and rerunning your code, you can use | |
374 | the %debug magic.""" |
|
372 | the %debug magic.""" | |
375 |
|
373 | |||
376 | par = parameter_s.strip().lower() |
|
374 | par = parameter_s.strip().lower() | |
377 |
|
375 | |||
378 | if par: |
|
376 | if par: | |
379 | try: |
|
377 | try: | |
380 | new_pdb = {'off':0,'0':0,'on':1,'1':1}[par] |
|
378 | new_pdb = {'off':0,'0':0,'on':1,'1':1}[par] | |
381 | except KeyError: |
|
379 | except KeyError: | |
382 | print ('Incorrect argument. Use on/1, off/0, ' |
|
380 | print ('Incorrect argument. Use on/1, off/0, ' | |
383 | 'or nothing for a toggle.') |
|
381 | 'or nothing for a toggle.') | |
384 | return |
|
382 | return | |
385 | else: |
|
383 | else: | |
386 | # toggle |
|
384 | # toggle | |
387 | new_pdb = not self.shell.call_pdb |
|
385 | new_pdb = not self.shell.call_pdb | |
388 |
|
386 | |||
389 | # set on the shell |
|
387 | # set on the shell | |
390 | self.shell.call_pdb = new_pdb |
|
388 | self.shell.call_pdb = new_pdb | |
391 | print('Automatic pdb calling has been turned',on_off(new_pdb)) |
|
389 | print('Automatic pdb calling has been turned',on_off(new_pdb)) | |
392 |
|
390 | |||
393 | @skip_doctest |
|
391 | @skip_doctest | |
394 | @magic_arguments.magic_arguments() |
|
392 | @magic_arguments.magic_arguments() | |
395 | @magic_arguments.argument('--breakpoint', '-b', metavar='FILE:LINE', |
|
393 | @magic_arguments.argument('--breakpoint', '-b', metavar='FILE:LINE', | |
396 | help=""" |
|
394 | help=""" | |
397 | Set break point at LINE in FILE. |
|
395 | Set break point at LINE in FILE. | |
398 | """ |
|
396 | """ | |
399 | ) |
|
397 | ) | |
400 | @magic_arguments.argument('statement', nargs='*', |
|
398 | @magic_arguments.argument('statement', nargs='*', | |
401 | help=""" |
|
399 | help=""" | |
402 | Code to run in debugger. |
|
400 | Code to run in debugger. | |
403 | You can omit this in cell magic mode. |
|
401 | You can omit this in cell magic mode. | |
404 | """ |
|
402 | """ | |
405 | ) |
|
403 | ) | |
406 | @line_cell_magic |
|
404 | @line_cell_magic | |
407 | def debug(self, line='', cell=None): |
|
405 | def debug(self, line='', cell=None): | |
408 | """Activate the interactive debugger. |
|
406 | """Activate the interactive debugger. | |
409 |
|
407 | |||
410 | This magic command support two ways of activating debugger. |
|
408 | This magic command support two ways of activating debugger. | |
411 | One is to activate debugger before executing code. This way, you |
|
409 | One is to activate debugger before executing code. This way, you | |
412 | can set a break point, to step through the code from the point. |
|
410 | can set a break point, to step through the code from the point. | |
413 | You can use this mode by giving statements to execute and optionally |
|
411 | You can use this mode by giving statements to execute and optionally | |
414 | a breakpoint. |
|
412 | a breakpoint. | |
415 |
|
413 | |||
416 | The other one is to activate debugger in post-mortem mode. You can |
|
414 | The other one is to activate debugger in post-mortem mode. You can | |
417 | activate this mode simply running %debug without any argument. |
|
415 | activate this mode simply running %debug without any argument. | |
418 | If an exception has just occurred, this lets you inspect its stack |
|
416 | If an exception has just occurred, this lets you inspect its stack | |
419 | frames interactively. Note that this will always work only on the last |
|
417 | frames interactively. Note that this will always work only on the last | |
420 | traceback that occurred, so you must call this quickly after an |
|
418 | traceback that occurred, so you must call this quickly after an | |
421 | exception that you wish to inspect has fired, because if another one |
|
419 | exception that you wish to inspect has fired, because if another one | |
422 | occurs, it clobbers the previous one. |
|
420 | occurs, it clobbers the previous one. | |
423 |
|
421 | |||
424 | If you want IPython to automatically do this on every exception, see |
|
422 | If you want IPython to automatically do this on every exception, see | |
425 | the %pdb magic for more details. |
|
423 | the %pdb magic for more details. | |
426 | """ |
|
424 | """ | |
427 | args = magic_arguments.parse_argstring(self.debug, line) |
|
425 | args = magic_arguments.parse_argstring(self.debug, line) | |
428 |
|
426 | |||
429 | if not (args.breakpoint or args.statement or cell): |
|
427 | if not (args.breakpoint or args.statement or cell): | |
430 | self._debug_post_mortem() |
|
428 | self._debug_post_mortem() | |
431 | else: |
|
429 | else: | |
432 | code = "\n".join(args.statement) |
|
430 | code = "\n".join(args.statement) | |
433 | if cell: |
|
431 | if cell: | |
434 | code += "\n" + cell |
|
432 | code += "\n" + cell | |
435 | self._debug_exec(code, args.breakpoint) |
|
433 | self._debug_exec(code, args.breakpoint) | |
436 |
|
434 | |||
437 | def _debug_post_mortem(self): |
|
435 | def _debug_post_mortem(self): | |
438 | self.shell.debugger(force=True) |
|
436 | self.shell.debugger(force=True) | |
439 |
|
437 | |||
440 | def _debug_exec(self, code, breakpoint): |
|
438 | def _debug_exec(self, code, breakpoint): | |
441 | if breakpoint: |
|
439 | if breakpoint: | |
442 | (filename, bp_line) = breakpoint.split(':', 1) |
|
440 | (filename, bp_line) = breakpoint.split(':', 1) | |
443 | bp_line = int(bp_line) |
|
441 | bp_line = int(bp_line) | |
444 | else: |
|
442 | else: | |
445 | (filename, bp_line) = (None, None) |
|
443 | (filename, bp_line) = (None, None) | |
446 | self._run_with_debugger(code, self.shell.user_ns, filename, bp_line) |
|
444 | self._run_with_debugger(code, self.shell.user_ns, filename, bp_line) | |
447 |
|
445 | |||
448 | @line_magic |
|
446 | @line_magic | |
449 | def tb(self, s): |
|
447 | def tb(self, s): | |
450 | """Print the last traceback with the currently active exception mode. |
|
448 | """Print the last traceback with the currently active exception mode. | |
451 |
|
449 | |||
452 | See %xmode for changing exception reporting modes.""" |
|
450 | See %xmode for changing exception reporting modes.""" | |
453 | self.shell.showtraceback() |
|
451 | self.shell.showtraceback() | |
454 |
|
452 | |||
455 | @skip_doctest |
|
453 | @skip_doctest | |
456 | @line_magic |
|
454 | @line_magic | |
457 | def run(self, parameter_s='', runner=None, |
|
455 | def run(self, parameter_s='', runner=None, | |
458 | file_finder=get_py_filename): |
|
456 | file_finder=get_py_filename): | |
459 | """Run the named file inside IPython as a program. |
|
457 | """Run the named file inside IPython as a program. | |
460 |
|
458 | |||
461 | Usage:: |
|
459 | Usage:: | |
462 |
|
460 | |||
463 | %run [-n -i -e -G] |
|
461 | %run [-n -i -e -G] | |
464 | [( -t [-N<N>] | -d [-b<N>] | -p [profile options] )] |
|
462 | [( -t [-N<N>] | -d [-b<N>] | -p [profile options] )] | |
465 | ( -m mod | file ) [args] |
|
463 | ( -m mod | file ) [args] | |
466 |
|
464 | |||
467 | Parameters after the filename are passed as command-line arguments to |
|
465 | Parameters after the filename are passed as command-line arguments to | |
468 | the program (put in sys.argv). Then, control returns to IPython's |
|
466 | the program (put in sys.argv). Then, control returns to IPython's | |
469 | prompt. |
|
467 | prompt. | |
470 |
|
468 | |||
471 | This is similar to running at a system prompt ``python file args``, |
|
469 | This is similar to running at a system prompt ``python file args``, | |
472 | but with the advantage of giving you IPython's tracebacks, and of |
|
470 | but with the advantage of giving you IPython's tracebacks, and of | |
473 | loading all variables into your interactive namespace for further use |
|
471 | loading all variables into your interactive namespace for further use | |
474 | (unless -p is used, see below). |
|
472 | (unless -p is used, see below). | |
475 |
|
473 | |||
476 | The file is executed in a namespace initially consisting only of |
|
474 | The file is executed in a namespace initially consisting only of | |
477 | ``__name__=='__main__'`` and sys.argv constructed as indicated. It thus |
|
475 | ``__name__=='__main__'`` and sys.argv constructed as indicated. It thus | |
478 | sees its environment as if it were being run as a stand-alone program |
|
476 | sees its environment as if it were being run as a stand-alone program | |
479 | (except for sharing global objects such as previously imported |
|
477 | (except for sharing global objects such as previously imported | |
480 | modules). But after execution, the IPython interactive namespace gets |
|
478 | modules). But after execution, the IPython interactive namespace gets | |
481 | updated with all variables defined in the program (except for __name__ |
|
479 | updated with all variables defined in the program (except for __name__ | |
482 | and sys.argv). This allows for very convenient loading of code for |
|
480 | and sys.argv). This allows for very convenient loading of code for | |
483 | interactive work, while giving each program a 'clean sheet' to run in. |
|
481 | interactive work, while giving each program a 'clean sheet' to run in. | |
484 |
|
482 | |||
485 | Arguments are expanded using shell-like glob match. Patterns |
|
483 | Arguments are expanded using shell-like glob match. Patterns | |
486 | '*', '?', '[seq]' and '[!seq]' can be used. Additionally, |
|
484 | '*', '?', '[seq]' and '[!seq]' can be used. Additionally, | |
487 | tilde '~' will be expanded into user's home directory. Unlike |
|
485 | tilde '~' will be expanded into user's home directory. Unlike | |
488 | real shells, quotation does not suppress expansions. Use |
|
486 | real shells, quotation does not suppress expansions. Use | |
489 | *two* back slashes (e.g. ``\\\\*``) to suppress expansions. |
|
487 | *two* back slashes (e.g. ``\\\\*``) to suppress expansions. | |
490 | To completely disable these expansions, you can use -G flag. |
|
488 | To completely disable these expansions, you can use -G flag. | |
491 |
|
489 | |||
492 | Options: |
|
490 | Options: | |
493 |
|
491 | |||
494 | -n |
|
492 | -n | |
495 | __name__ is NOT set to '__main__', but to the running file's name |
|
493 | __name__ is NOT set to '__main__', but to the running file's name | |
496 | without extension (as python does under import). This allows running |
|
494 | without extension (as python does under import). This allows running | |
497 | scripts and reloading the definitions in them without calling code |
|
495 | scripts and reloading the definitions in them without calling code | |
498 | protected by an ``if __name__ == "__main__"`` clause. |
|
496 | protected by an ``if __name__ == "__main__"`` clause. | |
499 |
|
497 | |||
500 | -i |
|
498 | -i | |
501 | run the file in IPython's namespace instead of an empty one. This |
|
499 | run the file in IPython's namespace instead of an empty one. This | |
502 | is useful if you are experimenting with code written in a text editor |
|
500 | is useful if you are experimenting with code written in a text editor | |
503 | which depends on variables defined interactively. |
|
501 | which depends on variables defined interactively. | |
504 |
|
502 | |||
505 | -e |
|
503 | -e | |
506 | ignore sys.exit() calls or SystemExit exceptions in the script |
|
504 | ignore sys.exit() calls or SystemExit exceptions in the script | |
507 | being run. This is particularly useful if IPython is being used to |
|
505 | being run. This is particularly useful if IPython is being used to | |
508 | run unittests, which always exit with a sys.exit() call. In such |
|
506 | run unittests, which always exit with a sys.exit() call. In such | |
509 | cases you are interested in the output of the test results, not in |
|
507 | cases you are interested in the output of the test results, not in | |
510 | seeing a traceback of the unittest module. |
|
508 | seeing a traceback of the unittest module. | |
511 |
|
509 | |||
512 | -t |
|
510 | -t | |
513 | print timing information at the end of the run. IPython will give |
|
511 | print timing information at the end of the run. IPython will give | |
514 | you an estimated CPU time consumption for your script, which under |
|
512 | you an estimated CPU time consumption for your script, which under | |
515 | Unix uses the resource module to avoid the wraparound problems of |
|
513 | Unix uses the resource module to avoid the wraparound problems of | |
516 | time.clock(). Under Unix, an estimate of time spent on system tasks |
|
514 | time.clock(). Under Unix, an estimate of time spent on system tasks | |
517 | is also given (for Windows platforms this is reported as 0.0). |
|
515 | is also given (for Windows platforms this is reported as 0.0). | |
518 |
|
516 | |||
519 | If -t is given, an additional ``-N<N>`` option can be given, where <N> |
|
517 | If -t is given, an additional ``-N<N>`` option can be given, where <N> | |
520 | must be an integer indicating how many times you want the script to |
|
518 | must be an integer indicating how many times you want the script to | |
521 | run. The final timing report will include total and per run results. |
|
519 | run. The final timing report will include total and per run results. | |
522 |
|
520 | |||
523 | For example (testing the script uniq_stable.py):: |
|
521 | For example (testing the script uniq_stable.py):: | |
524 |
|
522 | |||
525 | In [1]: run -t uniq_stable |
|
523 | In [1]: run -t uniq_stable | |
526 |
|
524 | |||
527 | IPython CPU timings (estimated): |
|
525 | IPython CPU timings (estimated): | |
528 | User : 0.19597 s. |
|
526 | User : 0.19597 s. | |
529 | System: 0.0 s. |
|
527 | System: 0.0 s. | |
530 |
|
528 | |||
531 | In [2]: run -t -N5 uniq_stable |
|
529 | In [2]: run -t -N5 uniq_stable | |
532 |
|
530 | |||
533 | IPython CPU timings (estimated): |
|
531 | IPython CPU timings (estimated): | |
534 | Total runs performed: 5 |
|
532 | Total runs performed: 5 | |
535 | Times : Total Per run |
|
533 | Times : Total Per run | |
536 | User : 0.910862 s, 0.1821724 s. |
|
534 | User : 0.910862 s, 0.1821724 s. | |
537 | System: 0.0 s, 0.0 s. |
|
535 | System: 0.0 s, 0.0 s. | |
538 |
|
536 | |||
539 | -d |
|
537 | -d | |
540 | run your program under the control of pdb, the Python debugger. |
|
538 | run your program under the control of pdb, the Python debugger. | |
541 | This allows you to execute your program step by step, watch variables, |
|
539 | This allows you to execute your program step by step, watch variables, | |
542 | etc. Internally, what IPython does is similar to calling:: |
|
540 | etc. Internally, what IPython does is similar to calling:: | |
543 |
|
541 | |||
544 | pdb.run('execfile("YOURFILENAME")') |
|
542 | pdb.run('execfile("YOURFILENAME")') | |
545 |
|
543 | |||
546 | with a breakpoint set on line 1 of your file. You can change the line |
|
544 | with a breakpoint set on line 1 of your file. You can change the line | |
547 | number for this automatic breakpoint to be <N> by using the -bN option |
|
545 | number for this automatic breakpoint to be <N> by using the -bN option | |
548 | (where N must be an integer). For example:: |
|
546 | (where N must be an integer). For example:: | |
549 |
|
547 | |||
550 | %run -d -b40 myscript |
|
548 | %run -d -b40 myscript | |
551 |
|
549 | |||
552 | will set the first breakpoint at line 40 in myscript.py. Note that |
|
550 | will set the first breakpoint at line 40 in myscript.py. Note that | |
553 | the first breakpoint must be set on a line which actually does |
|
551 | the first breakpoint must be set on a line which actually does | |
554 | something (not a comment or docstring) for it to stop execution. |
|
552 | something (not a comment or docstring) for it to stop execution. | |
555 |
|
553 | |||
556 | Or you can specify a breakpoint in a different file:: |
|
554 | Or you can specify a breakpoint in a different file:: | |
557 |
|
555 | |||
558 | %run -d -b myotherfile.py:20 myscript |
|
556 | %run -d -b myotherfile.py:20 myscript | |
559 |
|
557 | |||
560 | When the pdb debugger starts, you will see a (Pdb) prompt. You must |
|
558 | When the pdb debugger starts, you will see a (Pdb) prompt. You must | |
561 | first enter 'c' (without quotes) to start execution up to the first |
|
559 | first enter 'c' (without quotes) to start execution up to the first | |
562 | breakpoint. |
|
560 | breakpoint. | |
563 |
|
561 | |||
564 | Entering 'help' gives information about the use of the debugger. You |
|
562 | Entering 'help' gives information about the use of the debugger. You | |
565 | can easily see pdb's full documentation with "import pdb;pdb.help()" |
|
563 | can easily see pdb's full documentation with "import pdb;pdb.help()" | |
566 | at a prompt. |
|
564 | at a prompt. | |
567 |
|
565 | |||
568 | -p |
|
566 | -p | |
569 | run program under the control of the Python profiler module (which |
|
567 | run program under the control of the Python profiler module (which | |
570 | prints a detailed report of execution times, function calls, etc). |
|
568 | prints a detailed report of execution times, function calls, etc). | |
571 |
|
569 | |||
572 | You can pass other options after -p which affect the behavior of the |
|
570 | You can pass other options after -p which affect the behavior of the | |
573 | profiler itself. See the docs for %prun for details. |
|
571 | profiler itself. See the docs for %prun for details. | |
574 |
|
572 | |||
575 | In this mode, the program's variables do NOT propagate back to the |
|
573 | In this mode, the program's variables do NOT propagate back to the | |
576 | IPython interactive namespace (because they remain in the namespace |
|
574 | IPython interactive namespace (because they remain in the namespace | |
577 | where the profiler executes them). |
|
575 | where the profiler executes them). | |
578 |
|
576 | |||
579 | Internally this triggers a call to %prun, see its documentation for |
|
577 | Internally this triggers a call to %prun, see its documentation for | |
580 | details on the options available specifically for profiling. |
|
578 | details on the options available specifically for profiling. | |
581 |
|
579 | |||
582 | There is one special usage for which the text above doesn't apply: |
|
580 | There is one special usage for which the text above doesn't apply: | |
583 | if the filename ends with .ipy[nb], the file is run as ipython script, |
|
581 | if the filename ends with .ipy[nb], the file is run as ipython script, | |
584 | just as if the commands were written on IPython prompt. |
|
582 | just as if the commands were written on IPython prompt. | |
585 |
|
583 | |||
586 | -m |
|
584 | -m | |
587 | specify module name to load instead of script path. Similar to |
|
585 | specify module name to load instead of script path. Similar to | |
588 | the -m option for the python interpreter. Use this option last if you |
|
586 | the -m option for the python interpreter. Use this option last if you | |
589 | want to combine with other %run options. Unlike the python interpreter |
|
587 | want to combine with other %run options. Unlike the python interpreter | |
590 | only source modules are allowed no .pyc or .pyo files. |
|
588 | only source modules are allowed no .pyc or .pyo files. | |
591 | For example:: |
|
589 | For example:: | |
592 |
|
590 | |||
593 | %run -m example |
|
591 | %run -m example | |
594 |
|
592 | |||
595 | will run the example module. |
|
593 | will run the example module. | |
596 |
|
594 | |||
597 | -G |
|
595 | -G | |
598 | disable shell-like glob expansion of arguments. |
|
596 | disable shell-like glob expansion of arguments. | |
599 |
|
597 | |||
600 | """ |
|
598 | """ | |
601 |
|
599 | |||
602 | # get arguments and set sys.argv for program to be run. |
|
600 | # get arguments and set sys.argv for program to be run. | |
603 | opts, arg_lst = self.parse_options(parameter_s, |
|
601 | opts, arg_lst = self.parse_options(parameter_s, | |
604 | 'nidtN:b:pD:l:rs:T:em:G', |
|
602 | 'nidtN:b:pD:l:rs:T:em:G', | |
605 | mode='list', list_all=1) |
|
603 | mode='list', list_all=1) | |
606 | if "m" in opts: |
|
604 | if "m" in opts: | |
607 | modulename = opts["m"][0] |
|
605 | modulename = opts["m"][0] | |
608 | modpath = find_mod(modulename) |
|
606 | modpath = find_mod(modulename) | |
609 | if modpath is None: |
|
607 | if modpath is None: | |
610 | warn('%r is not a valid modulename on sys.path'%modulename) |
|
608 | warn('%r is not a valid modulename on sys.path'%modulename) | |
611 | return |
|
609 | return | |
612 | arg_lst = [modpath] + arg_lst |
|
610 | arg_lst = [modpath] + arg_lst | |
613 | try: |
|
611 | try: | |
614 | filename = file_finder(arg_lst[0]) |
|
612 | filename = file_finder(arg_lst[0]) | |
615 | except IndexError: |
|
613 | except IndexError: | |
616 | warn('you must provide at least a filename.') |
|
614 | warn('you must provide at least a filename.') | |
617 | print('\n%run:\n', oinspect.getdoc(self.run)) |
|
615 | print('\n%run:\n', oinspect.getdoc(self.run)) | |
618 | return |
|
616 | return | |
619 | except IOError as e: |
|
617 | except IOError as e: | |
620 | try: |
|
618 | try: | |
621 | msg = str(e) |
|
619 | msg = str(e) | |
622 | except UnicodeError: |
|
620 | except UnicodeError: | |
623 | msg = e.message |
|
621 | msg = e.message | |
624 | error(msg) |
|
622 | error(msg) | |
625 | return |
|
623 | return | |
626 |
|
624 | |||
627 | if filename.lower().endswith(('.ipy', '.ipynb')): |
|
625 | if filename.lower().endswith(('.ipy', '.ipynb')): | |
628 | with preserve_keys(self.shell.user_ns, '__file__'): |
|
626 | with preserve_keys(self.shell.user_ns, '__file__'): | |
629 | self.shell.user_ns['__file__'] = filename |
|
627 | self.shell.user_ns['__file__'] = filename | |
630 | self.shell.safe_execfile_ipy(filename) |
|
628 | self.shell.safe_execfile_ipy(filename) | |
631 | return |
|
629 | return | |
632 |
|
630 | |||
633 | # Control the response to exit() calls made by the script being run |
|
631 | # Control the response to exit() calls made by the script being run | |
634 | exit_ignore = 'e' in opts |
|
632 | exit_ignore = 'e' in opts | |
635 |
|
633 | |||
636 | # Make sure that the running script gets a proper sys.argv as if it |
|
634 | # Make sure that the running script gets a proper sys.argv as if it | |
637 | # were run from a system shell. |
|
635 | # were run from a system shell. | |
638 | save_argv = sys.argv # save it for later restoring |
|
636 | save_argv = sys.argv # save it for later restoring | |
639 |
|
637 | |||
640 | if 'G' in opts: |
|
638 | if 'G' in opts: | |
641 | args = arg_lst[1:] |
|
639 | args = arg_lst[1:] | |
642 | else: |
|
640 | else: | |
643 | # tilde and glob expansion |
|
641 | # tilde and glob expansion | |
644 | args = shellglob(map(os.path.expanduser, arg_lst[1:])) |
|
642 | args = shellglob(map(os.path.expanduser, arg_lst[1:])) | |
645 |
|
643 | |||
646 | sys.argv = [filename] + args # put in the proper filename |
|
644 | sys.argv = [filename] + args # put in the proper filename | |
647 | # protect sys.argv from potential unicode strings on Python 2: |
|
645 | # protect sys.argv from potential unicode strings on Python 2: | |
648 | if not py3compat.PY3: |
|
646 | if not py3compat.PY3: | |
649 | sys.argv = [ py3compat.cast_bytes(a) for a in sys.argv ] |
|
647 | sys.argv = [ py3compat.cast_bytes(a) for a in sys.argv ] | |
650 |
|
648 | |||
651 | if 'i' in opts: |
|
649 | if 'i' in opts: | |
652 | # Run in user's interactive namespace |
|
650 | # Run in user's interactive namespace | |
653 | prog_ns = self.shell.user_ns |
|
651 | prog_ns = self.shell.user_ns | |
654 | __name__save = self.shell.user_ns['__name__'] |
|
652 | __name__save = self.shell.user_ns['__name__'] | |
655 | prog_ns['__name__'] = '__main__' |
|
653 | prog_ns['__name__'] = '__main__' | |
656 | main_mod = self.shell.user_module |
|
654 | main_mod = self.shell.user_module | |
657 |
|
655 | |||
658 | # Since '%run foo' emulates 'python foo.py' at the cmd line, we must |
|
656 | # Since '%run foo' emulates 'python foo.py' at the cmd line, we must | |
659 | # set the __file__ global in the script's namespace |
|
657 | # set the __file__ global in the script's namespace | |
660 | # TK: Is this necessary in interactive mode? |
|
658 | # TK: Is this necessary in interactive mode? | |
661 | prog_ns['__file__'] = filename |
|
659 | prog_ns['__file__'] = filename | |
662 | else: |
|
660 | else: | |
663 | # Run in a fresh, empty namespace |
|
661 | # Run in a fresh, empty namespace | |
664 | if 'n' in opts: |
|
662 | if 'n' in opts: | |
665 | name = os.path.splitext(os.path.basename(filename))[0] |
|
663 | name = os.path.splitext(os.path.basename(filename))[0] | |
666 | else: |
|
664 | else: | |
667 | name = '__main__' |
|
665 | name = '__main__' | |
668 |
|
666 | |||
669 | # The shell MUST hold a reference to prog_ns so after %run |
|
667 | # The shell MUST hold a reference to prog_ns so after %run | |
670 | # exits, the python deletion mechanism doesn't zero it out |
|
668 | # exits, the python deletion mechanism doesn't zero it out | |
671 | # (leaving dangling references). See interactiveshell for details |
|
669 | # (leaving dangling references). See interactiveshell for details | |
672 | main_mod = self.shell.new_main_mod(filename, name) |
|
670 | main_mod = self.shell.new_main_mod(filename, name) | |
673 | prog_ns = main_mod.__dict__ |
|
671 | prog_ns = main_mod.__dict__ | |
674 |
|
672 | |||
675 | # pickle fix. See interactiveshell for an explanation. But we need to |
|
673 | # pickle fix. See interactiveshell for an explanation. But we need to | |
676 | # make sure that, if we overwrite __main__, we replace it at the end |
|
674 | # make sure that, if we overwrite __main__, we replace it at the end | |
677 | main_mod_name = prog_ns['__name__'] |
|
675 | main_mod_name = prog_ns['__name__'] | |
678 |
|
676 | |||
679 | if main_mod_name == '__main__': |
|
677 | if main_mod_name == '__main__': | |
680 | restore_main = sys.modules['__main__'] |
|
678 | restore_main = sys.modules['__main__'] | |
681 | else: |
|
679 | else: | |
682 | restore_main = False |
|
680 | restore_main = False | |
683 |
|
681 | |||
684 | # This needs to be undone at the end to prevent holding references to |
|
682 | # This needs to be undone at the end to prevent holding references to | |
685 | # every single object ever created. |
|
683 | # every single object ever created. | |
686 | sys.modules[main_mod_name] = main_mod |
|
684 | sys.modules[main_mod_name] = main_mod | |
687 |
|
685 | |||
688 | if 'p' in opts or 'd' in opts: |
|
686 | if 'p' in opts or 'd' in opts: | |
689 | if 'm' in opts: |
|
687 | if 'm' in opts: | |
690 | code = 'run_module(modulename, prog_ns)' |
|
688 | code = 'run_module(modulename, prog_ns)' | |
691 | code_ns = { |
|
689 | code_ns = { | |
692 | 'run_module': self.shell.safe_run_module, |
|
690 | 'run_module': self.shell.safe_run_module, | |
693 | 'prog_ns': prog_ns, |
|
691 | 'prog_ns': prog_ns, | |
694 | 'modulename': modulename, |
|
692 | 'modulename': modulename, | |
695 | } |
|
693 | } | |
696 | else: |
|
694 | else: | |
697 | if 'd' in opts: |
|
695 | if 'd' in opts: | |
698 | # allow exceptions to raise in debug mode |
|
696 | # allow exceptions to raise in debug mode | |
699 | code = 'execfile(filename, prog_ns, raise_exceptions=True)' |
|
697 | code = 'execfile(filename, prog_ns, raise_exceptions=True)' | |
700 | else: |
|
698 | else: | |
701 | code = 'execfile(filename, prog_ns)' |
|
699 | code = 'execfile(filename, prog_ns)' | |
702 | code_ns = { |
|
700 | code_ns = { | |
703 | 'execfile': self.shell.safe_execfile, |
|
701 | 'execfile': self.shell.safe_execfile, | |
704 | 'prog_ns': prog_ns, |
|
702 | 'prog_ns': prog_ns, | |
705 | 'filename': get_py_filename(filename), |
|
703 | 'filename': get_py_filename(filename), | |
706 | } |
|
704 | } | |
707 |
|
705 | |||
708 | try: |
|
706 | try: | |
709 | stats = None |
|
707 | stats = None | |
710 | with self.shell.readline_no_record: |
|
708 | with self.shell.readline_no_record: | |
711 | if 'p' in opts: |
|
709 | if 'p' in opts: | |
712 | stats = self._run_with_profiler(code, opts, code_ns) |
|
710 | stats = self._run_with_profiler(code, opts, code_ns) | |
713 | else: |
|
711 | else: | |
714 | if 'd' in opts: |
|
712 | if 'd' in opts: | |
715 | bp_file, bp_line = parse_breakpoint( |
|
713 | bp_file, bp_line = parse_breakpoint( | |
716 | opts.get('b', ['1'])[0], filename) |
|
714 | opts.get('b', ['1'])[0], filename) | |
717 | self._run_with_debugger( |
|
715 | self._run_with_debugger( | |
718 | code, code_ns, filename, bp_line, bp_file) |
|
716 | code, code_ns, filename, bp_line, bp_file) | |
719 | else: |
|
717 | else: | |
720 | if 'm' in opts: |
|
718 | if 'm' in opts: | |
721 | def run(): |
|
719 | def run(): | |
722 | self.shell.safe_run_module(modulename, prog_ns) |
|
720 | self.shell.safe_run_module(modulename, prog_ns) | |
723 | else: |
|
721 | else: | |
724 | if runner is None: |
|
722 | if runner is None: | |
725 | runner = self.default_runner |
|
723 | runner = self.default_runner | |
726 | if runner is None: |
|
724 | if runner is None: | |
727 | runner = self.shell.safe_execfile |
|
725 | runner = self.shell.safe_execfile | |
728 |
|
726 | |||
729 | def run(): |
|
727 | def run(): | |
730 | runner(filename, prog_ns, prog_ns, |
|
728 | runner(filename, prog_ns, prog_ns, | |
731 | exit_ignore=exit_ignore) |
|
729 | exit_ignore=exit_ignore) | |
732 |
|
730 | |||
733 | if 't' in opts: |
|
731 | if 't' in opts: | |
734 | # timed execution |
|
732 | # timed execution | |
735 | try: |
|
733 | try: | |
736 | nruns = int(opts['N'][0]) |
|
734 | nruns = int(opts['N'][0]) | |
737 | if nruns < 1: |
|
735 | if nruns < 1: | |
738 | error('Number of runs must be >=1') |
|
736 | error('Number of runs must be >=1') | |
739 | return |
|
737 | return | |
740 | except (KeyError): |
|
738 | except (KeyError): | |
741 | nruns = 1 |
|
739 | nruns = 1 | |
742 | self._run_with_timing(run, nruns) |
|
740 | self._run_with_timing(run, nruns) | |
743 | else: |
|
741 | else: | |
744 | # regular execution |
|
742 | # regular execution | |
745 | run() |
|
743 | run() | |
746 |
|
744 | |||
747 | if 'i' in opts: |
|
745 | if 'i' in opts: | |
748 | self.shell.user_ns['__name__'] = __name__save |
|
746 | self.shell.user_ns['__name__'] = __name__save | |
749 | else: |
|
747 | else: | |
750 | # update IPython interactive namespace |
|
748 | # update IPython interactive namespace | |
751 |
|
749 | |||
752 | # Some forms of read errors on the file may mean the |
|
750 | # Some forms of read errors on the file may mean the | |
753 | # __name__ key was never set; using pop we don't have to |
|
751 | # __name__ key was never set; using pop we don't have to | |
754 | # worry about a possible KeyError. |
|
752 | # worry about a possible KeyError. | |
755 | prog_ns.pop('__name__', None) |
|
753 | prog_ns.pop('__name__', None) | |
756 |
|
754 | |||
757 | with preserve_keys(self.shell.user_ns, '__file__'): |
|
755 | with preserve_keys(self.shell.user_ns, '__file__'): | |
758 | self.shell.user_ns.update(prog_ns) |
|
756 | self.shell.user_ns.update(prog_ns) | |
759 | finally: |
|
757 | finally: | |
760 | # It's a bit of a mystery why, but __builtins__ can change from |
|
758 | # It's a bit of a mystery why, but __builtins__ can change from | |
761 | # being a module to becoming a dict missing some key data after |
|
759 | # being a module to becoming a dict missing some key data after | |
762 | # %run. As best I can see, this is NOT something IPython is doing |
|
760 | # %run. As best I can see, this is NOT something IPython is doing | |
763 | # at all, and similar problems have been reported before: |
|
761 | # at all, and similar problems have been reported before: | |
764 | # http://coding.derkeiler.com/Archive/Python/comp.lang.python/2004-10/0188.html |
|
762 | # http://coding.derkeiler.com/Archive/Python/comp.lang.python/2004-10/0188.html | |
765 | # Since this seems to be done by the interpreter itself, the best |
|
763 | # Since this seems to be done by the interpreter itself, the best | |
766 | # we can do is to at least restore __builtins__ for the user on |
|
764 | # we can do is to at least restore __builtins__ for the user on | |
767 | # exit. |
|
765 | # exit. | |
768 | self.shell.user_ns['__builtins__'] = builtin_mod |
|
766 | self.shell.user_ns['__builtins__'] = builtin_mod | |
769 |
|
767 | |||
770 | # Ensure key global structures are restored |
|
768 | # Ensure key global structures are restored | |
771 | sys.argv = save_argv |
|
769 | sys.argv = save_argv | |
772 | if restore_main: |
|
770 | if restore_main: | |
773 | sys.modules['__main__'] = restore_main |
|
771 | sys.modules['__main__'] = restore_main | |
774 | else: |
|
772 | else: | |
775 | # Remove from sys.modules the reference to main_mod we'd |
|
773 | # Remove from sys.modules the reference to main_mod we'd | |
776 | # added. Otherwise it will trap references to objects |
|
774 | # added. Otherwise it will trap references to objects | |
777 | # contained therein. |
|
775 | # contained therein. | |
778 | del sys.modules[main_mod_name] |
|
776 | del sys.modules[main_mod_name] | |
779 |
|
777 | |||
780 | return stats |
|
778 | return stats | |
781 |
|
779 | |||
782 | def _run_with_debugger(self, code, code_ns, filename=None, |
|
780 | def _run_with_debugger(self, code, code_ns, filename=None, | |
783 | bp_line=None, bp_file=None): |
|
781 | bp_line=None, bp_file=None): | |
784 | """ |
|
782 | """ | |
785 | Run `code` in debugger with a break point. |
|
783 | Run `code` in debugger with a break point. | |
786 |
|
784 | |||
787 | Parameters |
|
785 | Parameters | |
788 | ---------- |
|
786 | ---------- | |
789 | code : str |
|
787 | code : str | |
790 | Code to execute. |
|
788 | Code to execute. | |
791 | code_ns : dict |
|
789 | code_ns : dict | |
792 | A namespace in which `code` is executed. |
|
790 | A namespace in which `code` is executed. | |
793 | filename : str |
|
791 | filename : str | |
794 | `code` is ran as if it is in `filename`. |
|
792 | `code` is ran as if it is in `filename`. | |
795 | bp_line : int, optional |
|
793 | bp_line : int, optional | |
796 | Line number of the break point. |
|
794 | Line number of the break point. | |
797 | bp_file : str, optional |
|
795 | bp_file : str, optional | |
798 | Path to the file in which break point is specified. |
|
796 | Path to the file in which break point is specified. | |
799 | `filename` is used if not given. |
|
797 | `filename` is used if not given. | |
800 |
|
798 | |||
801 | Raises |
|
799 | Raises | |
802 | ------ |
|
800 | ------ | |
803 | UsageError |
|
801 | UsageError | |
804 | If the break point given by `bp_line` is not valid. |
|
802 | If the break point given by `bp_line` is not valid. | |
805 |
|
803 | |||
806 | """ |
|
804 | """ | |
807 | deb = debugger.Pdb(self.shell.colors) |
|
805 | deb = debugger.Pdb(self.shell.colors) | |
808 | # reset Breakpoint state, which is moronically kept |
|
806 | # reset Breakpoint state, which is moronically kept | |
809 | # in a class |
|
807 | # in a class | |
810 | bdb.Breakpoint.next = 1 |
|
808 | bdb.Breakpoint.next = 1 | |
811 | bdb.Breakpoint.bplist = {} |
|
809 | bdb.Breakpoint.bplist = {} | |
812 | bdb.Breakpoint.bpbynumber = [None] |
|
810 | bdb.Breakpoint.bpbynumber = [None] | |
813 | if bp_line is not None: |
|
811 | if bp_line is not None: | |
814 | # Set an initial breakpoint to stop execution |
|
812 | # Set an initial breakpoint to stop execution | |
815 | maxtries = 10 |
|
813 | maxtries = 10 | |
816 | bp_file = bp_file or filename |
|
814 | bp_file = bp_file or filename | |
817 | checkline = deb.checkline(bp_file, bp_line) |
|
815 | checkline = deb.checkline(bp_file, bp_line) | |
818 | if not checkline: |
|
816 | if not checkline: | |
819 | for bp in range(bp_line + 1, bp_line + maxtries + 1): |
|
817 | for bp in range(bp_line + 1, bp_line + maxtries + 1): | |
820 | if deb.checkline(bp_file, bp): |
|
818 | if deb.checkline(bp_file, bp): | |
821 | break |
|
819 | break | |
822 | else: |
|
820 | else: | |
823 | msg = ("\nI failed to find a valid line to set " |
|
821 | msg = ("\nI failed to find a valid line to set " | |
824 | "a breakpoint\n" |
|
822 | "a breakpoint\n" | |
825 | "after trying up to line: %s.\n" |
|
823 | "after trying up to line: %s.\n" | |
826 | "Please set a valid breakpoint manually " |
|
824 | "Please set a valid breakpoint manually " | |
827 | "with the -b option." % bp) |
|
825 | "with the -b option." % bp) | |
828 | raise UsageError(msg) |
|
826 | raise UsageError(msg) | |
829 | # if we find a good linenumber, set the breakpoint |
|
827 | # if we find a good linenumber, set the breakpoint | |
830 | deb.do_break('%s:%s' % (bp_file, bp_line)) |
|
828 | deb.do_break('%s:%s' % (bp_file, bp_line)) | |
831 |
|
829 | |||
832 | if filename: |
|
830 | if filename: | |
833 | # Mimic Pdb._runscript(...) |
|
831 | # Mimic Pdb._runscript(...) | |
834 | deb._wait_for_mainpyfile = True |
|
832 | deb._wait_for_mainpyfile = True | |
835 | deb.mainpyfile = deb.canonic(filename) |
|
833 | deb.mainpyfile = deb.canonic(filename) | |
836 |
|
834 | |||
837 | # Start file run |
|
835 | # Start file run | |
838 | print("NOTE: Enter 'c' at the %s prompt to continue execution." % deb.prompt) |
|
836 | print("NOTE: Enter 'c' at the %s prompt to continue execution." % deb.prompt) | |
839 | try: |
|
837 | try: | |
840 | if filename: |
|
838 | if filename: | |
841 | # save filename so it can be used by methods on the deb object |
|
839 | # save filename so it can be used by methods on the deb object | |
842 | deb._exec_filename = filename |
|
840 | deb._exec_filename = filename | |
843 | while True: |
|
841 | while True: | |
844 | try: |
|
842 | try: | |
845 | deb.run(code, code_ns) |
|
843 | deb.run(code, code_ns) | |
846 | except Restart: |
|
844 | except Restart: | |
847 | print("Restarting") |
|
845 | print("Restarting") | |
848 | if filename: |
|
846 | if filename: | |
849 | deb._wait_for_mainpyfile = True |
|
847 | deb._wait_for_mainpyfile = True | |
850 | deb.mainpyfile = deb.canonic(filename) |
|
848 | deb.mainpyfile = deb.canonic(filename) | |
851 | continue |
|
849 | continue | |
852 | else: |
|
850 | else: | |
853 | break |
|
851 | break | |
854 |
|
852 | |||
855 |
|
853 | |||
856 | except: |
|
854 | except: | |
857 | etype, value, tb = sys.exc_info() |
|
855 | etype, value, tb = sys.exc_info() | |
858 | # Skip three frames in the traceback: the %run one, |
|
856 | # Skip three frames in the traceback: the %run one, | |
859 | # one inside bdb.py, and the command-line typed by the |
|
857 | # one inside bdb.py, and the command-line typed by the | |
860 | # user (run by exec in pdb itself). |
|
858 | # user (run by exec in pdb itself). | |
861 | self.shell.InteractiveTB(etype, value, tb, tb_offset=3) |
|
859 | self.shell.InteractiveTB(etype, value, tb, tb_offset=3) | |
862 |
|
860 | |||
863 | @staticmethod |
|
861 | @staticmethod | |
864 | def _run_with_timing(run, nruns): |
|
862 | def _run_with_timing(run, nruns): | |
865 | """ |
|
863 | """ | |
866 | Run function `run` and print timing information. |
|
864 | Run function `run` and print timing information. | |
867 |
|
865 | |||
868 | Parameters |
|
866 | Parameters | |
869 | ---------- |
|
867 | ---------- | |
870 | run : callable |
|
868 | run : callable | |
871 | Any callable object which takes no argument. |
|
869 | Any callable object which takes no argument. | |
872 | nruns : int |
|
870 | nruns : int | |
873 | Number of times to execute `run`. |
|
871 | Number of times to execute `run`. | |
874 |
|
872 | |||
875 | """ |
|
873 | """ | |
876 | twall0 = time.time() |
|
874 | twall0 = time.time() | |
877 | if nruns == 1: |
|
875 | if nruns == 1: | |
878 | t0 = clock2() |
|
876 | t0 = clock2() | |
879 | run() |
|
877 | run() | |
880 | t1 = clock2() |
|
878 | t1 = clock2() | |
881 | t_usr = t1[0] - t0[0] |
|
879 | t_usr = t1[0] - t0[0] | |
882 | t_sys = t1[1] - t0[1] |
|
880 | t_sys = t1[1] - t0[1] | |
883 | print("\nIPython CPU timings (estimated):") |
|
881 | print("\nIPython CPU timings (estimated):") | |
884 | print(" User : %10.2f s." % t_usr) |
|
882 | print(" User : %10.2f s." % t_usr) | |
885 | print(" System : %10.2f s." % t_sys) |
|
883 | print(" System : %10.2f s." % t_sys) | |
886 | else: |
|
884 | else: | |
887 | runs = range(nruns) |
|
885 | runs = range(nruns) | |
888 | t0 = clock2() |
|
886 | t0 = clock2() | |
889 | for nr in runs: |
|
887 | for nr in runs: | |
890 | run() |
|
888 | run() | |
891 | t1 = clock2() |
|
889 | t1 = clock2() | |
892 | t_usr = t1[0] - t0[0] |
|
890 | t_usr = t1[0] - t0[0] | |
893 | t_sys = t1[1] - t0[1] |
|
891 | t_sys = t1[1] - t0[1] | |
894 | print("\nIPython CPU timings (estimated):") |
|
892 | print("\nIPython CPU timings (estimated):") | |
895 | print("Total runs performed:", nruns) |
|
893 | print("Total runs performed:", nruns) | |
896 | print(" Times : %10s %10s" % ('Total', 'Per run')) |
|
894 | print(" Times : %10s %10s" % ('Total', 'Per run')) | |
897 | print(" User : %10.2f s, %10.2f s." % (t_usr, t_usr / nruns)) |
|
895 | print(" User : %10.2f s, %10.2f s." % (t_usr, t_usr / nruns)) | |
898 | print(" System : %10.2f s, %10.2f s." % (t_sys, t_sys / nruns)) |
|
896 | print(" System : %10.2f s, %10.2f s." % (t_sys, t_sys / nruns)) | |
899 | twall1 = time.time() |
|
897 | twall1 = time.time() | |
900 | print("Wall time: %10.2f s." % (twall1 - twall0)) |
|
898 | print("Wall time: %10.2f s." % (twall1 - twall0)) | |
901 |
|
899 | |||
902 | @skip_doctest |
|
900 | @skip_doctest | |
903 | @line_cell_magic |
|
901 | @line_cell_magic | |
904 | def timeit(self, line='', cell=None): |
|
902 | def timeit(self, line='', cell=None): | |
905 | """Time execution of a Python statement or expression |
|
903 | """Time execution of a Python statement or expression | |
906 |
|
904 | |||
907 | Usage, in line mode: |
|
905 | Usage, in line mode: | |
908 | %timeit [-n<N> -r<R> [-t|-c] -q -p<P> -o] statement |
|
906 | %timeit [-n<N> -r<R> [-t|-c] -q -p<P> -o] statement | |
909 | or in cell mode: |
|
907 | or in cell mode: | |
910 | %%timeit [-n<N> -r<R> [-t|-c] -q -p<P> -o] setup_code |
|
908 | %%timeit [-n<N> -r<R> [-t|-c] -q -p<P> -o] setup_code | |
911 | code |
|
909 | code | |
912 | code... |
|
910 | code... | |
913 |
|
911 | |||
914 | Time execution of a Python statement or expression using the timeit |
|
912 | Time execution of a Python statement or expression using the timeit | |
915 | module. This function can be used both as a line and cell magic: |
|
913 | module. This function can be used both as a line and cell magic: | |
916 |
|
914 | |||
917 | - In line mode you can time a single-line statement (though multiple |
|
915 | - In line mode you can time a single-line statement (though multiple | |
918 | ones can be chained with using semicolons). |
|
916 | ones can be chained with using semicolons). | |
919 |
|
917 | |||
920 | - In cell mode, the statement in the first line is used as setup code |
|
918 | - In cell mode, the statement in the first line is used as setup code | |
921 | (executed but not timed) and the body of the cell is timed. The cell |
|
919 | (executed but not timed) and the body of the cell is timed. The cell | |
922 | body has access to any variables created in the setup code. |
|
920 | body has access to any variables created in the setup code. | |
923 |
|
921 | |||
924 | Options: |
|
922 | Options: | |
925 | -n<N>: execute the given statement <N> times in a loop. If this value |
|
923 | -n<N>: execute the given statement <N> times in a loop. If this value | |
926 | is not given, a fitting value is chosen. |
|
924 | is not given, a fitting value is chosen. | |
927 |
|
925 | |||
928 | -r<R>: repeat the loop iteration <R> times and take the best result. |
|
926 | -r<R>: repeat the loop iteration <R> times and take the best result. | |
929 | Default: 3 |
|
927 | Default: 3 | |
930 |
|
928 | |||
931 | -t: use time.time to measure the time, which is the default on Unix. |
|
929 | -t: use time.time to measure the time, which is the default on Unix. | |
932 | This function measures wall time. |
|
930 | This function measures wall time. | |
933 |
|
931 | |||
934 | -c: use time.clock to measure the time, which is the default on |
|
932 | -c: use time.clock to measure the time, which is the default on | |
935 | Windows and measures wall time. On Unix, resource.getrusage is used |
|
933 | Windows and measures wall time. On Unix, resource.getrusage is used | |
936 | instead and returns the CPU user time. |
|
934 | instead and returns the CPU user time. | |
937 |
|
935 | |||
938 | -p<P>: use a precision of <P> digits to display the timing result. |
|
936 | -p<P>: use a precision of <P> digits to display the timing result. | |
939 | Default: 3 |
|
937 | Default: 3 | |
940 |
|
938 | |||
941 | -q: Quiet, do not print result. |
|
939 | -q: Quiet, do not print result. | |
942 |
|
940 | |||
943 | -o: return a TimeitResult that can be stored in a variable to inspect |
|
941 | -o: return a TimeitResult that can be stored in a variable to inspect | |
944 | the result in more details. |
|
942 | the result in more details. | |
945 |
|
943 | |||
946 |
|
944 | |||
947 | Examples |
|
945 | Examples | |
948 | -------- |
|
946 | -------- | |
949 | :: |
|
947 | :: | |
950 |
|
948 | |||
951 | In [1]: %timeit pass |
|
949 | In [1]: %timeit pass | |
952 | 10000000 loops, best of 3: 53.3 ns per loop |
|
950 | 10000000 loops, best of 3: 53.3 ns per loop | |
953 |
|
951 | |||
954 | In [2]: u = None |
|
952 | In [2]: u = None | |
955 |
|
953 | |||
956 | In [3]: %timeit u is None |
|
954 | In [3]: %timeit u is None | |
957 | 10000000 loops, best of 3: 184 ns per loop |
|
955 | 10000000 loops, best of 3: 184 ns per loop | |
958 |
|
956 | |||
959 | In [4]: %timeit -r 4 u == None |
|
957 | In [4]: %timeit -r 4 u == None | |
960 | 1000000 loops, best of 4: 242 ns per loop |
|
958 | 1000000 loops, best of 4: 242 ns per loop | |
961 |
|
959 | |||
962 | In [5]: import time |
|
960 | In [5]: import time | |
963 |
|
961 | |||
964 | In [6]: %timeit -n1 time.sleep(2) |
|
962 | In [6]: %timeit -n1 time.sleep(2) | |
965 | 1 loop, best of 3: 2 s per loop |
|
963 | 1 loop, best of 3: 2 s per loop | |
966 |
|
964 | |||
967 |
|
965 | |||
968 | The times reported by %timeit will be slightly higher than those |
|
966 | The times reported by %timeit will be slightly higher than those | |
969 | reported by the timeit.py script when variables are accessed. This is |
|
967 | reported by the timeit.py script when variables are accessed. This is | |
970 | due to the fact that %timeit executes the statement in the namespace |
|
968 | due to the fact that %timeit executes the statement in the namespace | |
971 | of the shell, compared with timeit.py, which uses a single setup |
|
969 | of the shell, compared with timeit.py, which uses a single setup | |
972 | statement to import function or create variables. Generally, the bias |
|
970 | statement to import function or create variables. Generally, the bias | |
973 | does not matter as long as results from timeit.py are not mixed with |
|
971 | does not matter as long as results from timeit.py are not mixed with | |
974 | those from %timeit.""" |
|
972 | those from %timeit.""" | |
975 |
|
973 | |||
976 | opts, stmt = self.parse_options(line,'n:r:tcp:qo', |
|
974 | opts, stmt = self.parse_options(line,'n:r:tcp:qo', | |
977 | posix=False, strict=False) |
|
975 | posix=False, strict=False) | |
978 | if stmt == "" and cell is None: |
|
976 | if stmt == "" and cell is None: | |
979 | return |
|
977 | return | |
980 |
|
978 | |||
981 | timefunc = timeit.default_timer |
|
979 | timefunc = timeit.default_timer | |
982 | number = int(getattr(opts, "n", 0)) |
|
980 | number = int(getattr(opts, "n", 0)) | |
983 | repeat = int(getattr(opts, "r", timeit.default_repeat)) |
|
981 | repeat = int(getattr(opts, "r", timeit.default_repeat)) | |
984 | precision = int(getattr(opts, "p", 3)) |
|
982 | precision = int(getattr(opts, "p", 3)) | |
985 | quiet = 'q' in opts |
|
983 | quiet = 'q' in opts | |
986 | return_result = 'o' in opts |
|
984 | return_result = 'o' in opts | |
987 | if hasattr(opts, "t"): |
|
985 | if hasattr(opts, "t"): | |
988 | timefunc = time.time |
|
986 | timefunc = time.time | |
989 | if hasattr(opts, "c"): |
|
987 | if hasattr(opts, "c"): | |
990 | timefunc = clock |
|
988 | timefunc = clock | |
991 |
|
989 | |||
992 | timer = Timer(timer=timefunc) |
|
990 | timer = Timer(timer=timefunc) | |
993 | # this code has tight coupling to the inner workings of timeit.Timer, |
|
991 | # this code has tight coupling to the inner workings of timeit.Timer, | |
994 | # but is there a better way to achieve that the code stmt has access |
|
992 | # but is there a better way to achieve that the code stmt has access | |
995 | # to the shell namespace? |
|
993 | # to the shell namespace? | |
996 | transform = self.shell.input_splitter.transform_cell |
|
994 | transform = self.shell.input_splitter.transform_cell | |
997 |
|
995 | |||
998 | if cell is None: |
|
996 | if cell is None: | |
999 | # called as line magic |
|
997 | # called as line magic | |
1000 | ast_setup = self.shell.compile.ast_parse("pass") |
|
998 | ast_setup = self.shell.compile.ast_parse("pass") | |
1001 | ast_stmt = self.shell.compile.ast_parse(transform(stmt)) |
|
999 | ast_stmt = self.shell.compile.ast_parse(transform(stmt)) | |
1002 | else: |
|
1000 | else: | |
1003 | ast_setup = self.shell.compile.ast_parse(transform(stmt)) |
|
1001 | ast_setup = self.shell.compile.ast_parse(transform(stmt)) | |
1004 | ast_stmt = self.shell.compile.ast_parse(transform(cell)) |
|
1002 | ast_stmt = self.shell.compile.ast_parse(transform(cell)) | |
1005 |
|
1003 | |||
1006 | ast_setup = self.shell.transform_ast(ast_setup) |
|
1004 | ast_setup = self.shell.transform_ast(ast_setup) | |
1007 | ast_stmt = self.shell.transform_ast(ast_stmt) |
|
1005 | ast_stmt = self.shell.transform_ast(ast_stmt) | |
1008 |
|
1006 | |||
1009 | # This codestring is taken from timeit.template - we fill it in as an |
|
1007 | # This codestring is taken from timeit.template - we fill it in as an | |
1010 | # AST, so that we can apply our AST transformations to the user code |
|
1008 | # AST, so that we can apply our AST transformations to the user code | |
1011 | # without affecting the timing code. |
|
1009 | # without affecting the timing code. | |
1012 | timeit_ast_template = ast.parse('def inner(_it, _timer):\n' |
|
1010 | timeit_ast_template = ast.parse('def inner(_it, _timer):\n' | |
1013 | ' setup\n' |
|
1011 | ' setup\n' | |
1014 | ' _t0 = _timer()\n' |
|
1012 | ' _t0 = _timer()\n' | |
1015 | ' for _i in _it:\n' |
|
1013 | ' for _i in _it:\n' | |
1016 | ' stmt\n' |
|
1014 | ' stmt\n' | |
1017 | ' _t1 = _timer()\n' |
|
1015 | ' _t1 = _timer()\n' | |
1018 | ' return _t1 - _t0\n') |
|
1016 | ' return _t1 - _t0\n') | |
1019 |
|
1017 | |||
1020 | timeit_ast = TimeitTemplateFiller(ast_setup, ast_stmt).visit(timeit_ast_template) |
|
1018 | timeit_ast = TimeitTemplateFiller(ast_setup, ast_stmt).visit(timeit_ast_template) | |
1021 | timeit_ast = ast.fix_missing_locations(timeit_ast) |
|
1019 | timeit_ast = ast.fix_missing_locations(timeit_ast) | |
1022 |
|
1020 | |||
1023 | # Track compilation time so it can be reported if too long |
|
1021 | # Track compilation time so it can be reported if too long | |
1024 | # Minimum time above which compilation time will be reported |
|
1022 | # Minimum time above which compilation time will be reported | |
1025 | tc_min = 0.1 |
|
1023 | tc_min = 0.1 | |
1026 |
|
1024 | |||
1027 | t0 = clock() |
|
1025 | t0 = clock() | |
1028 | code = self.shell.compile(timeit_ast, "<magic-timeit>", "exec") |
|
1026 | code = self.shell.compile(timeit_ast, "<magic-timeit>", "exec") | |
1029 | tc = clock()-t0 |
|
1027 | tc = clock()-t0 | |
1030 |
|
1028 | |||
1031 | ns = {} |
|
1029 | ns = {} | |
1032 | exec(code, self.shell.user_ns, ns) |
|
1030 | exec(code, self.shell.user_ns, ns) | |
1033 | timer.inner = ns["inner"] |
|
1031 | timer.inner = ns["inner"] | |
1034 |
|
1032 | |||
1035 | # This is used to check if there is a huge difference between the |
|
1033 | # This is used to check if there is a huge difference between the | |
1036 | # best and worst timings. |
|
1034 | # best and worst timings. | |
1037 | # Issue: https://github.com/ipython/ipython/issues/6471 |
|
1035 | # Issue: https://github.com/ipython/ipython/issues/6471 | |
1038 | worst_tuning = 0 |
|
1036 | worst_tuning = 0 | |
1039 | if number == 0: |
|
1037 | if number == 0: | |
1040 | # determine number so that 0.2 <= total time < 2.0 |
|
1038 | # determine number so that 0.2 <= total time < 2.0 | |
1041 | number = 1 |
|
1039 | number = 1 | |
1042 | for _ in range(1, 10): |
|
1040 | for _ in range(1, 10): | |
1043 | time_number = timer.timeit(number) |
|
1041 | time_number = timer.timeit(number) | |
1044 | worst_tuning = max(worst_tuning, time_number / number) |
|
1042 | worst_tuning = max(worst_tuning, time_number / number) | |
1045 | if time_number >= 0.2: |
|
1043 | if time_number >= 0.2: | |
1046 | break |
|
1044 | break | |
1047 | number *= 10 |
|
1045 | number *= 10 | |
1048 | all_runs = timer.repeat(repeat, number) |
|
1046 | all_runs = timer.repeat(repeat, number) | |
1049 | best = min(all_runs) / number |
|
1047 | best = min(all_runs) / number | |
1050 |
|
1048 | |||
1051 | worst = max(all_runs) / number |
|
1049 | worst = max(all_runs) / number | |
1052 | if worst_tuning: |
|
1050 | if worst_tuning: | |
1053 | worst = max(worst, worst_tuning) |
|
1051 | worst = max(worst, worst_tuning) | |
1054 |
|
1052 | |||
1055 | if not quiet : |
|
1053 | if not quiet : | |
1056 | # Check best timing is greater than zero to avoid a |
|
1054 | # Check best timing is greater than zero to avoid a | |
1057 | # ZeroDivisionError. |
|
1055 | # ZeroDivisionError. | |
1058 | # In cases where the slowest timing is lesser than a micosecond |
|
1056 | # In cases where the slowest timing is lesser than a micosecond | |
1059 | # we assume that it does not really matter if the fastest |
|
1057 | # we assume that it does not really matter if the fastest | |
1060 | # timing is 4 times faster than the slowest timing or not. |
|
1058 | # timing is 4 times faster than the slowest timing or not. | |
1061 | if worst > 4 * best and best > 0 and worst > 1e-6: |
|
1059 | if worst > 4 * best and best > 0 and worst > 1e-6: | |
1062 | print("The slowest run took %0.2f times longer than the " |
|
1060 | print("The slowest run took %0.2f times longer than the " | |
1063 | "fastest. This could mean that an intermediate result " |
|
1061 | "fastest. This could mean that an intermediate result " | |
1064 | "is being cached." % (worst / best)) |
|
1062 | "is being cached." % (worst / best)) | |
1065 | if number == 1: # No s at "loops" if only one loop |
|
1063 | if number == 1: # No s at "loops" if only one loop | |
1066 | print(u"%d loop, best of %d: %s per loop" % (number, repeat, |
|
1064 | print(u"%d loop, best of %d: %s per loop" % (number, repeat, | |
1067 | _format_time(best, precision))) |
|
1065 | _format_time(best, precision))) | |
1068 | else: |
|
1066 | else: | |
1069 | print(u"%d loops, best of %d: %s per loop" % (number, repeat, |
|
1067 | print(u"%d loops, best of %d: %s per loop" % (number, repeat, | |
1070 | _format_time(best, precision))) |
|
1068 | _format_time(best, precision))) | |
1071 | if tc > tc_min: |
|
1069 | if tc > tc_min: | |
1072 | print("Compiler time: %.2f s" % tc) |
|
1070 | print("Compiler time: %.2f s" % tc) | |
1073 | if return_result: |
|
1071 | if return_result: | |
1074 | return TimeitResult(number, repeat, best, worst, all_runs, tc, precision) |
|
1072 | return TimeitResult(number, repeat, best, worst, all_runs, tc, precision) | |
1075 |
|
1073 | |||
1076 | @skip_doctest |
|
1074 | @skip_doctest | |
1077 | @needs_local_scope |
|
1075 | @needs_local_scope | |
1078 | @line_cell_magic |
|
1076 | @line_cell_magic | |
1079 | def time(self,line='', cell=None, local_ns=None): |
|
1077 | def time(self,line='', cell=None, local_ns=None): | |
1080 | """Time execution of a Python statement or expression. |
|
1078 | """Time execution of a Python statement or expression. | |
1081 |
|
1079 | |||
1082 | The CPU and wall clock times are printed, and the value of the |
|
1080 | The CPU and wall clock times are printed, and the value of the | |
1083 | expression (if any) is returned. Note that under Win32, system time |
|
1081 | expression (if any) is returned. Note that under Win32, system time | |
1084 | is always reported as 0, since it can not be measured. |
|
1082 | is always reported as 0, since it can not be measured. | |
1085 |
|
1083 | |||
1086 | This function can be used both as a line and cell magic: |
|
1084 | This function can be used both as a line and cell magic: | |
1087 |
|
1085 | |||
1088 | - In line mode you can time a single-line statement (though multiple |
|
1086 | - In line mode you can time a single-line statement (though multiple | |
1089 | ones can be chained with using semicolons). |
|
1087 | ones can be chained with using semicolons). | |
1090 |
|
1088 | |||
1091 | - In cell mode, you can time the cell body (a directly |
|
1089 | - In cell mode, you can time the cell body (a directly | |
1092 | following statement raises an error). |
|
1090 | following statement raises an error). | |
1093 |
|
1091 | |||
1094 | This function provides very basic timing functionality. Use the timeit |
|
1092 | This function provides very basic timing functionality. Use the timeit | |
1095 | magic for more control over the measurement. |
|
1093 | magic for more control over the measurement. | |
1096 |
|
1094 | |||
1097 | Examples |
|
1095 | Examples | |
1098 | -------- |
|
1096 | -------- | |
1099 | :: |
|
1097 | :: | |
1100 |
|
1098 | |||
1101 | In [1]: %time 2**128 |
|
1099 | In [1]: %time 2**128 | |
1102 | CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s |
|
1100 | CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s | |
1103 | Wall time: 0.00 |
|
1101 | Wall time: 0.00 | |
1104 | Out[1]: 340282366920938463463374607431768211456L |
|
1102 | Out[1]: 340282366920938463463374607431768211456L | |
1105 |
|
1103 | |||
1106 | In [2]: n = 1000000 |
|
1104 | In [2]: n = 1000000 | |
1107 |
|
1105 | |||
1108 | In [3]: %time sum(range(n)) |
|
1106 | In [3]: %time sum(range(n)) | |
1109 | CPU times: user 1.20 s, sys: 0.05 s, total: 1.25 s |
|
1107 | CPU times: user 1.20 s, sys: 0.05 s, total: 1.25 s | |
1110 | Wall time: 1.37 |
|
1108 | Wall time: 1.37 | |
1111 | Out[3]: 499999500000L |
|
1109 | Out[3]: 499999500000L | |
1112 |
|
1110 | |||
1113 | In [4]: %time print 'hello world' |
|
1111 | In [4]: %time print 'hello world' | |
1114 | hello world |
|
1112 | hello world | |
1115 | CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s |
|
1113 | CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s | |
1116 | Wall time: 0.00 |
|
1114 | Wall time: 0.00 | |
1117 |
|
1115 | |||
1118 | Note that the time needed by Python to compile the given expression |
|
1116 | Note that the time needed by Python to compile the given expression | |
1119 | will be reported if it is more than 0.1s. In this example, the |
|
1117 | will be reported if it is more than 0.1s. In this example, the | |
1120 | actual exponentiation is done by Python at compilation time, so while |
|
1118 | actual exponentiation is done by Python at compilation time, so while | |
1121 | the expression can take a noticeable amount of time to compute, that |
|
1119 | the expression can take a noticeable amount of time to compute, that | |
1122 | time is purely due to the compilation: |
|
1120 | time is purely due to the compilation: | |
1123 |
|
1121 | |||
1124 | In [5]: %time 3**9999; |
|
1122 | In [5]: %time 3**9999; | |
1125 | CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s |
|
1123 | CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s | |
1126 | Wall time: 0.00 s |
|
1124 | Wall time: 0.00 s | |
1127 |
|
1125 | |||
1128 | In [6]: %time 3**999999; |
|
1126 | In [6]: %time 3**999999; | |
1129 | CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s |
|
1127 | CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s | |
1130 | Wall time: 0.00 s |
|
1128 | Wall time: 0.00 s | |
1131 | Compiler : 0.78 s |
|
1129 | Compiler : 0.78 s | |
1132 | """ |
|
1130 | """ | |
1133 |
|
1131 | |||
1134 | # fail immediately if the given expression can't be compiled |
|
1132 | # fail immediately if the given expression can't be compiled | |
1135 |
|
1133 | |||
1136 | if line and cell: |
|
1134 | if line and cell: | |
1137 | raise UsageError("Can't use statement directly after '%%time'!") |
|
1135 | raise UsageError("Can't use statement directly after '%%time'!") | |
1138 |
|
1136 | |||
1139 | if cell: |
|
1137 | if cell: | |
1140 | expr = self.shell.input_transformer_manager.transform_cell(cell) |
|
1138 | expr = self.shell.input_transformer_manager.transform_cell(cell) | |
1141 | else: |
|
1139 | else: | |
1142 | expr = self.shell.input_transformer_manager.transform_cell(line) |
|
1140 | expr = self.shell.input_transformer_manager.transform_cell(line) | |
1143 |
|
1141 | |||
1144 | # Minimum time above which parse time will be reported |
|
1142 | # Minimum time above which parse time will be reported | |
1145 | tp_min = 0.1 |
|
1143 | tp_min = 0.1 | |
1146 |
|
1144 | |||
1147 | t0 = clock() |
|
1145 | t0 = clock() | |
1148 | expr_ast = self.shell.compile.ast_parse(expr) |
|
1146 | expr_ast = self.shell.compile.ast_parse(expr) | |
1149 | tp = clock()-t0 |
|
1147 | tp = clock()-t0 | |
1150 |
|
1148 | |||
1151 | # Apply AST transformations |
|
1149 | # Apply AST transformations | |
1152 | expr_ast = self.shell.transform_ast(expr_ast) |
|
1150 | expr_ast = self.shell.transform_ast(expr_ast) | |
1153 |
|
1151 | |||
1154 | # Minimum time above which compilation time will be reported |
|
1152 | # Minimum time above which compilation time will be reported | |
1155 | tc_min = 0.1 |
|
1153 | tc_min = 0.1 | |
1156 |
|
1154 | |||
1157 | if len(expr_ast.body)==1 and isinstance(expr_ast.body[0], ast.Expr): |
|
1155 | if len(expr_ast.body)==1 and isinstance(expr_ast.body[0], ast.Expr): | |
1158 | mode = 'eval' |
|
1156 | mode = 'eval' | |
1159 | source = '<timed eval>' |
|
1157 | source = '<timed eval>' | |
1160 | expr_ast = ast.Expression(expr_ast.body[0].value) |
|
1158 | expr_ast = ast.Expression(expr_ast.body[0].value) | |
1161 | else: |
|
1159 | else: | |
1162 | mode = 'exec' |
|
1160 | mode = 'exec' | |
1163 | source = '<timed exec>' |
|
1161 | source = '<timed exec>' | |
1164 | t0 = clock() |
|
1162 | t0 = clock() | |
1165 | code = self.shell.compile(expr_ast, source, mode) |
|
1163 | code = self.shell.compile(expr_ast, source, mode) | |
1166 | tc = clock()-t0 |
|
1164 | tc = clock()-t0 | |
1167 |
|
1165 | |||
1168 | # skew measurement as little as possible |
|
1166 | # skew measurement as little as possible | |
1169 | glob = self.shell.user_ns |
|
1167 | glob = self.shell.user_ns | |
1170 | wtime = time.time |
|
1168 | wtime = time.time | |
1171 | # time execution |
|
1169 | # time execution | |
1172 | wall_st = wtime() |
|
1170 | wall_st = wtime() | |
1173 | if mode=='eval': |
|
1171 | if mode=='eval': | |
1174 | st = clock2() |
|
1172 | st = clock2() | |
1175 | out = eval(code, glob, local_ns) |
|
1173 | out = eval(code, glob, local_ns) | |
1176 | end = clock2() |
|
1174 | end = clock2() | |
1177 | else: |
|
1175 | else: | |
1178 | st = clock2() |
|
1176 | st = clock2() | |
1179 | exec(code, glob, local_ns) |
|
1177 | exec(code, glob, local_ns) | |
1180 | end = clock2() |
|
1178 | end = clock2() | |
1181 | out = None |
|
1179 | out = None | |
1182 | wall_end = wtime() |
|
1180 | wall_end = wtime() | |
1183 | # Compute actual times and report |
|
1181 | # Compute actual times and report | |
1184 | wall_time = wall_end-wall_st |
|
1182 | wall_time = wall_end-wall_st | |
1185 | cpu_user = end[0]-st[0] |
|
1183 | cpu_user = end[0]-st[0] | |
1186 | cpu_sys = end[1]-st[1] |
|
1184 | cpu_sys = end[1]-st[1] | |
1187 | cpu_tot = cpu_user+cpu_sys |
|
1185 | cpu_tot = cpu_user+cpu_sys | |
1188 | # On windows cpu_sys is always zero, so no new information to the next print |
|
1186 | # On windows cpu_sys is always zero, so no new information to the next print | |
1189 | if sys.platform != 'win32': |
|
1187 | if sys.platform != 'win32': | |
1190 | print("CPU times: user %s, sys: %s, total: %s" % \ |
|
1188 | print("CPU times: user %s, sys: %s, total: %s" % \ | |
1191 | (_format_time(cpu_user),_format_time(cpu_sys),_format_time(cpu_tot))) |
|
1189 | (_format_time(cpu_user),_format_time(cpu_sys),_format_time(cpu_tot))) | |
1192 | print("Wall time: %s" % _format_time(wall_time)) |
|
1190 | print("Wall time: %s" % _format_time(wall_time)) | |
1193 | if tc > tc_min: |
|
1191 | if tc > tc_min: | |
1194 | print("Compiler : %s" % _format_time(tc)) |
|
1192 | print("Compiler : %s" % _format_time(tc)) | |
1195 | if tp > tp_min: |
|
1193 | if tp > tp_min: | |
1196 | print("Parser : %s" % _format_time(tp)) |
|
1194 | print("Parser : %s" % _format_time(tp)) | |
1197 | return out |
|
1195 | return out | |
1198 |
|
1196 | |||
1199 | @skip_doctest |
|
1197 | @skip_doctest | |
1200 | @line_magic |
|
1198 | @line_magic | |
1201 | def macro(self, parameter_s=''): |
|
1199 | def macro(self, parameter_s=''): | |
1202 | """Define a macro for future re-execution. It accepts ranges of history, |
|
1200 | """Define a macro for future re-execution. It accepts ranges of history, | |
1203 | filenames or string objects. |
|
1201 | filenames or string objects. | |
1204 |
|
1202 | |||
1205 | Usage:\\ |
|
1203 | Usage:\\ | |
1206 | %macro [options] name n1-n2 n3-n4 ... n5 .. n6 ... |
|
1204 | %macro [options] name n1-n2 n3-n4 ... n5 .. n6 ... | |
1207 |
|
1205 | |||
1208 | Options: |
|
1206 | Options: | |
1209 |
|
1207 | |||
1210 | -r: use 'raw' input. By default, the 'processed' history is used, |
|
1208 | -r: use 'raw' input. By default, the 'processed' history is used, | |
1211 | so that magics are loaded in their transformed version to valid |
|
1209 | so that magics are loaded in their transformed version to valid | |
1212 | Python. If this option is given, the raw input as typed at the |
|
1210 | Python. If this option is given, the raw input as typed at the | |
1213 | command line is used instead. |
|
1211 | command line is used instead. | |
1214 |
|
1212 | |||
1215 | -q: quiet macro definition. By default, a tag line is printed |
|
1213 | -q: quiet macro definition. By default, a tag line is printed | |
1216 | to indicate the macro has been created, and then the contents of |
|
1214 | to indicate the macro has been created, and then the contents of | |
1217 | the macro are printed. If this option is given, then no printout |
|
1215 | the macro are printed. If this option is given, then no printout | |
1218 | is produced once the macro is created. |
|
1216 | is produced once the macro is created. | |
1219 |
|
1217 | |||
1220 | This will define a global variable called `name` which is a string |
|
1218 | This will define a global variable called `name` which is a string | |
1221 | made of joining the slices and lines you specify (n1,n2,... numbers |
|
1219 | made of joining the slices and lines you specify (n1,n2,... numbers | |
1222 | above) from your input history into a single string. This variable |
|
1220 | above) from your input history into a single string. This variable | |
1223 | acts like an automatic function which re-executes those lines as if |
|
1221 | acts like an automatic function which re-executes those lines as if | |
1224 | you had typed them. You just type 'name' at the prompt and the code |
|
1222 | you had typed them. You just type 'name' at the prompt and the code | |
1225 | executes. |
|
1223 | executes. | |
1226 |
|
1224 | |||
1227 | The syntax for indicating input ranges is described in %history. |
|
1225 | The syntax for indicating input ranges is described in %history. | |
1228 |
|
1226 | |||
1229 | Note: as a 'hidden' feature, you can also use traditional python slice |
|
1227 | Note: as a 'hidden' feature, you can also use traditional python slice | |
1230 | notation, where N:M means numbers N through M-1. |
|
1228 | notation, where N:M means numbers N through M-1. | |
1231 |
|
1229 | |||
1232 | For example, if your history contains (print using %hist -n ):: |
|
1230 | For example, if your history contains (print using %hist -n ):: | |
1233 |
|
1231 | |||
1234 | 44: x=1 |
|
1232 | 44: x=1 | |
1235 | 45: y=3 |
|
1233 | 45: y=3 | |
1236 | 46: z=x+y |
|
1234 | 46: z=x+y | |
1237 | 47: print x |
|
1235 | 47: print x | |
1238 | 48: a=5 |
|
1236 | 48: a=5 | |
1239 | 49: print 'x',x,'y',y |
|
1237 | 49: print 'x',x,'y',y | |
1240 |
|
1238 | |||
1241 | you can create a macro with lines 44 through 47 (included) and line 49 |
|
1239 | you can create a macro with lines 44 through 47 (included) and line 49 | |
1242 | called my_macro with:: |
|
1240 | called my_macro with:: | |
1243 |
|
1241 | |||
1244 | In [55]: %macro my_macro 44-47 49 |
|
1242 | In [55]: %macro my_macro 44-47 49 | |
1245 |
|
1243 | |||
1246 | Now, typing `my_macro` (without quotes) will re-execute all this code |
|
1244 | Now, typing `my_macro` (without quotes) will re-execute all this code | |
1247 | in one pass. |
|
1245 | in one pass. | |
1248 |
|
1246 | |||
1249 | You don't need to give the line-numbers in order, and any given line |
|
1247 | You don't need to give the line-numbers in order, and any given line | |
1250 | number can appear multiple times. You can assemble macros with any |
|
1248 | number can appear multiple times. You can assemble macros with any | |
1251 | lines from your input history in any order. |
|
1249 | lines from your input history in any order. | |
1252 |
|
1250 | |||
1253 | The macro is a simple object which holds its value in an attribute, |
|
1251 | The macro is a simple object which holds its value in an attribute, | |
1254 | but IPython's display system checks for macros and executes them as |
|
1252 | but IPython's display system checks for macros and executes them as | |
1255 | code instead of printing them when you type their name. |
|
1253 | code instead of printing them when you type their name. | |
1256 |
|
1254 | |||
1257 | You can view a macro's contents by explicitly printing it with:: |
|
1255 | You can view a macro's contents by explicitly printing it with:: | |
1258 |
|
1256 | |||
1259 | print macro_name |
|
1257 | print macro_name | |
1260 |
|
1258 | |||
1261 | """ |
|
1259 | """ | |
1262 | opts,args = self.parse_options(parameter_s,'rq',mode='list') |
|
1260 | opts,args = self.parse_options(parameter_s,'rq',mode='list') | |
1263 | if not args: # List existing macros |
|
1261 | if not args: # List existing macros | |
1264 | return sorted(k for k,v in iteritems(self.shell.user_ns) if\ |
|
1262 | return sorted(k for k,v in iteritems(self.shell.user_ns) if\ | |
1265 | isinstance(v, Macro)) |
|
1263 | isinstance(v, Macro)) | |
1266 | if len(args) == 1: |
|
1264 | if len(args) == 1: | |
1267 | raise UsageError( |
|
1265 | raise UsageError( | |
1268 | "%macro insufficient args; usage '%macro name n1-n2 n3-4...") |
|
1266 | "%macro insufficient args; usage '%macro name n1-n2 n3-4...") | |
1269 | name, codefrom = args[0], " ".join(args[1:]) |
|
1267 | name, codefrom = args[0], " ".join(args[1:]) | |
1270 |
|
1268 | |||
1271 | #print 'rng',ranges # dbg |
|
1269 | #print 'rng',ranges # dbg | |
1272 | try: |
|
1270 | try: | |
1273 | lines = self.shell.find_user_code(codefrom, 'r' in opts) |
|
1271 | lines = self.shell.find_user_code(codefrom, 'r' in opts) | |
1274 | except (ValueError, TypeError) as e: |
|
1272 | except (ValueError, TypeError) as e: | |
1275 | print(e.args[0]) |
|
1273 | print(e.args[0]) | |
1276 | return |
|
1274 | return | |
1277 | macro = Macro(lines) |
|
1275 | macro = Macro(lines) | |
1278 | self.shell.define_macro(name, macro) |
|
1276 | self.shell.define_macro(name, macro) | |
1279 | if not ( 'q' in opts) : |
|
1277 | if not ( 'q' in opts) : | |
1280 | print('Macro `%s` created. To execute, type its name (without quotes).' % name) |
|
1278 | print('Macro `%s` created. To execute, type its name (without quotes).' % name) | |
1281 | print('=== Macro contents: ===') |
|
1279 | print('=== Macro contents: ===') | |
1282 | print(macro, end=' ') |
|
1280 | print(macro, end=' ') | |
1283 |
|
1281 | |||
1284 | @magic_arguments.magic_arguments() |
|
1282 | @magic_arguments.magic_arguments() | |
1285 | @magic_arguments.argument('output', type=str, default='', nargs='?', |
|
1283 | @magic_arguments.argument('output', type=str, default='', nargs='?', | |
1286 | help="""The name of the variable in which to store output. |
|
1284 | help="""The name of the variable in which to store output. | |
1287 | This is a utils.io.CapturedIO object with stdout/err attributes |
|
1285 | This is a utils.io.CapturedIO object with stdout/err attributes | |
1288 | for the text of the captured output. |
|
1286 | for the text of the captured output. | |
1289 |
|
1287 | |||
1290 | CapturedOutput also has a show() method for displaying the output, |
|
1288 | CapturedOutput also has a show() method for displaying the output, | |
1291 | and __call__ as well, so you can use that to quickly display the |
|
1289 | and __call__ as well, so you can use that to quickly display the | |
1292 | output. |
|
1290 | output. | |
1293 |
|
1291 | |||
1294 | If unspecified, captured output is discarded. |
|
1292 | If unspecified, captured output is discarded. | |
1295 | """ |
|
1293 | """ | |
1296 | ) |
|
1294 | ) | |
1297 | @magic_arguments.argument('--no-stderr', action="store_true", |
|
1295 | @magic_arguments.argument('--no-stderr', action="store_true", | |
1298 | help="""Don't capture stderr.""" |
|
1296 | help="""Don't capture stderr.""" | |
1299 | ) |
|
1297 | ) | |
1300 | @magic_arguments.argument('--no-stdout', action="store_true", |
|
1298 | @magic_arguments.argument('--no-stdout', action="store_true", | |
1301 | help="""Don't capture stdout.""" |
|
1299 | help="""Don't capture stdout.""" | |
1302 | ) |
|
1300 | ) | |
1303 | @magic_arguments.argument('--no-display', action="store_true", |
|
1301 | @magic_arguments.argument('--no-display', action="store_true", | |
1304 | help="""Don't capture IPython's rich display.""" |
|
1302 | help="""Don't capture IPython's rich display.""" | |
1305 | ) |
|
1303 | ) | |
1306 | @cell_magic |
|
1304 | @cell_magic | |
1307 | def capture(self, line, cell): |
|
1305 | def capture(self, line, cell): | |
1308 | """run the cell, capturing stdout, stderr, and IPython's rich display() calls.""" |
|
1306 | """run the cell, capturing stdout, stderr, and IPython's rich display() calls.""" | |
1309 | args = magic_arguments.parse_argstring(self.capture, line) |
|
1307 | args = magic_arguments.parse_argstring(self.capture, line) | |
1310 | out = not args.no_stdout |
|
1308 | out = not args.no_stdout | |
1311 | err = not args.no_stderr |
|
1309 | err = not args.no_stderr | |
1312 | disp = not args.no_display |
|
1310 | disp = not args.no_display | |
1313 | with capture_output(out, err, disp) as io: |
|
1311 | with capture_output(out, err, disp) as io: | |
1314 | self.shell.run_cell(cell) |
|
1312 | self.shell.run_cell(cell) | |
1315 | if args.output: |
|
1313 | if args.output: | |
1316 | self.shell.user_ns[args.output] = io |
|
1314 | self.shell.user_ns[args.output] = io | |
1317 |
|
1315 | |||
1318 | def parse_breakpoint(text, current_file): |
|
1316 | def parse_breakpoint(text, current_file): | |
1319 | '''Returns (file, line) for file:line and (current_file, line) for line''' |
|
1317 | '''Returns (file, line) for file:line and (current_file, line) for line''' | |
1320 | colon = text.find(':') |
|
1318 | colon = text.find(':') | |
1321 | if colon == -1: |
|
1319 | if colon == -1: | |
1322 | return current_file, int(text) |
|
1320 | return current_file, int(text) | |
1323 | else: |
|
1321 | else: | |
1324 | return text[:colon], int(text[colon+1:]) |
|
1322 | return text[:colon], int(text[colon+1:]) | |
1325 |
|
1323 | |||
1326 | def _format_time(timespan, precision=3): |
|
1324 | def _format_time(timespan, precision=3): | |
1327 | """Formats the timespan in a human readable form""" |
|
1325 | """Formats the timespan in a human readable form""" | |
1328 | import math |
|
1326 | import math | |
1329 |
|
1327 | |||
1330 | if timespan >= 60.0: |
|
1328 | if timespan >= 60.0: | |
1331 | # we have more than a minute, format that in a human readable form |
|
1329 | # we have more than a minute, format that in a human readable form | |
1332 | # Idea from http://snipplr.com/view/5713/ |
|
1330 | # Idea from http://snipplr.com/view/5713/ | |
1333 | parts = [("d", 60*60*24),("h", 60*60),("min", 60), ("s", 1)] |
|
1331 | parts = [("d", 60*60*24),("h", 60*60),("min", 60), ("s", 1)] | |
1334 | time = [] |
|
1332 | time = [] | |
1335 | leftover = timespan |
|
1333 | leftover = timespan | |
1336 | for suffix, length in parts: |
|
1334 | for suffix, length in parts: | |
1337 | value = int(leftover / length) |
|
1335 | value = int(leftover / length) | |
1338 | if value > 0: |
|
1336 | if value > 0: | |
1339 | leftover = leftover % length |
|
1337 | leftover = leftover % length | |
1340 | time.append(u'%s%s' % (str(value), suffix)) |
|
1338 | time.append(u'%s%s' % (str(value), suffix)) | |
1341 | if leftover < 1: |
|
1339 | if leftover < 1: | |
1342 | break |
|
1340 | break | |
1343 | return " ".join(time) |
|
1341 | return " ".join(time) | |
1344 |
|
1342 | |||
1345 |
|
1343 | |||
1346 | # Unfortunately the unicode 'micro' symbol can cause problems in |
|
1344 | # Unfortunately the unicode 'micro' symbol can cause problems in | |
1347 | # certain terminals. |
|
1345 | # certain terminals. | |
1348 | # See bug: https://bugs.launchpad.net/ipython/+bug/348466 |
|
1346 | # See bug: https://bugs.launchpad.net/ipython/+bug/348466 | |
1349 | # Try to prevent crashes by being more secure than it needs to |
|
1347 | # Try to prevent crashes by being more secure than it needs to | |
1350 | # E.g. eclipse is able to print a Β΅, but has no sys.stdout.encoding set. |
|
1348 | # E.g. eclipse is able to print a Β΅, but has no sys.stdout.encoding set. | |
1351 | units = [u"s", u"ms",u'us',"ns"] # the save value |
|
1349 | units = [u"s", u"ms",u'us',"ns"] # the save value | |
1352 | if hasattr(sys.stdout, 'encoding') and sys.stdout.encoding: |
|
1350 | if hasattr(sys.stdout, 'encoding') and sys.stdout.encoding: | |
1353 | try: |
|
1351 | try: | |
1354 | u'\xb5'.encode(sys.stdout.encoding) |
|
1352 | u'\xb5'.encode(sys.stdout.encoding) | |
1355 | units = [u"s", u"ms",u'\xb5s',"ns"] |
|
1353 | units = [u"s", u"ms",u'\xb5s',"ns"] | |
1356 | except: |
|
1354 | except: | |
1357 | pass |
|
1355 | pass | |
1358 | scaling = [1, 1e3, 1e6, 1e9] |
|
1356 | scaling = [1, 1e3, 1e6, 1e9] | |
1359 |
|
1357 | |||
1360 | if timespan > 0.0: |
|
1358 | if timespan > 0.0: | |
1361 | order = min(-int(math.floor(math.log10(timespan)) // 3), 3) |
|
1359 | order = min(-int(math.floor(math.log10(timespan)) // 3), 3) | |
1362 | else: |
|
1360 | else: | |
1363 | order = 3 |
|
1361 | order = 3 | |
1364 | return u"%.*g %s" % (precision, timespan * scaling[order], units[order]) |
|
1362 | return u"%.*g %s" % (precision, timespan * scaling[order], units[order]) |
@@ -1,796 +1,790 b'' | |||||
1 | """Implementation of magic functions for interaction with the OS. |
|
1 | """Implementation of magic functions for interaction with the OS. | |
2 |
|
2 | |||
3 | Note: this module is named 'osm' instead of 'os' to avoid a collision with the |
|
3 | Note: this module is named 'osm' instead of 'os' to avoid a collision with the | |
4 | builtin. |
|
4 | builtin. | |
5 | """ |
|
5 | """ | |
6 | from __future__ import print_function |
|
6 | from __future__ import print_function | |
7 | #----------------------------------------------------------------------------- |
|
7 | #----------------------------------------------------------------------------- | |
8 | # Copyright (c) 2012 The IPython Development Team. |
|
8 | # Copyright (c) 2012 The IPython Development Team. | |
9 | # |
|
9 | # | |
10 | # Distributed under the terms of the Modified BSD License. |
|
10 | # Distributed under the terms of the Modified BSD License. | |
11 | # |
|
11 | # | |
12 | # The full license is in the file COPYING.txt, distributed with this software. |
|
12 | # The full license is in the file COPYING.txt, distributed with this software. | |
13 | #----------------------------------------------------------------------------- |
|
13 | #----------------------------------------------------------------------------- | |
14 |
|
14 | |||
15 | #----------------------------------------------------------------------------- |
|
15 | #----------------------------------------------------------------------------- | |
16 | # Imports |
|
16 | # Imports | |
17 | #----------------------------------------------------------------------------- |
|
17 | #----------------------------------------------------------------------------- | |
18 |
|
18 | |||
19 | # Stdlib |
|
19 | # Stdlib | |
20 | import io |
|
20 | import io | |
21 | import os |
|
21 | import os | |
22 | import re |
|
22 | import re | |
23 | import sys |
|
23 | import sys | |
24 | from pprint import pformat |
|
24 | from pprint import pformat | |
25 |
|
25 | |||
26 | # Our own packages |
|
26 | # Our own packages | |
27 | from IPython.core import magic_arguments |
|
27 | from IPython.core import magic_arguments | |
28 | from IPython.core import oinspect |
|
28 | from IPython.core import oinspect | |
29 | from IPython.core import page |
|
29 | from IPython.core import page | |
30 | from IPython.core.alias import AliasError, Alias |
|
30 | from IPython.core.alias import AliasError, Alias | |
31 | from IPython.core.error import UsageError |
|
31 | from IPython.core.error import UsageError | |
32 | from IPython.core.magic import ( |
|
32 | from IPython.core.magic import ( | |
33 | Magics, compress_dhist, magics_class, line_magic, cell_magic, line_cell_magic |
|
33 | Magics, compress_dhist, magics_class, line_magic, cell_magic, line_cell_magic | |
34 | ) |
|
34 | ) | |
35 | from IPython.testing.skipdoctest import skip_doctest |
|
35 | from IPython.testing.skipdoctest import skip_doctest | |
36 | from IPython.utils.openpy import source_to_unicode |
|
36 | from IPython.utils.openpy import source_to_unicode | |
37 | from IPython.utils.path import unquote_filename |
|
|||
38 | from IPython.utils.process import abbrev_cwd |
|
37 | from IPython.utils.process import abbrev_cwd | |
39 | from IPython.utils import py3compat |
|
38 | from IPython.utils import py3compat | |
40 | from IPython.utils.py3compat import unicode_type |
|
39 | from IPython.utils.py3compat import unicode_type | |
41 | from IPython.utils.terminal import set_term_title |
|
40 | from IPython.utils.terminal import set_term_title | |
42 |
|
41 | |||
43 | #----------------------------------------------------------------------------- |
|
42 | #----------------------------------------------------------------------------- | |
44 | # Magic implementation classes |
|
43 | # Magic implementation classes | |
45 | #----------------------------------------------------------------------------- |
|
44 | #----------------------------------------------------------------------------- | |
46 | @magics_class |
|
45 | @magics_class | |
47 | class OSMagics(Magics): |
|
46 | class OSMagics(Magics): | |
48 | """Magics to interact with the underlying OS (shell-type functionality). |
|
47 | """Magics to interact with the underlying OS (shell-type functionality). | |
49 | """ |
|
48 | """ | |
50 |
|
49 | |||
51 | @skip_doctest |
|
50 | @skip_doctest | |
52 | @line_magic |
|
51 | @line_magic | |
53 | def alias(self, parameter_s=''): |
|
52 | def alias(self, parameter_s=''): | |
54 | """Define an alias for a system command. |
|
53 | """Define an alias for a system command. | |
55 |
|
54 | |||
56 | '%alias alias_name cmd' defines 'alias_name' as an alias for 'cmd' |
|
55 | '%alias alias_name cmd' defines 'alias_name' as an alias for 'cmd' | |
57 |
|
56 | |||
58 | Then, typing 'alias_name params' will execute the system command 'cmd |
|
57 | Then, typing 'alias_name params' will execute the system command 'cmd | |
59 | params' (from your underlying operating system). |
|
58 | params' (from your underlying operating system). | |
60 |
|
59 | |||
61 | Aliases have lower precedence than magic functions and Python normal |
|
60 | Aliases have lower precedence than magic functions and Python normal | |
62 | variables, so if 'foo' is both a Python variable and an alias, the |
|
61 | variables, so if 'foo' is both a Python variable and an alias, the | |
63 | alias can not be executed until 'del foo' removes the Python variable. |
|
62 | alias can not be executed until 'del foo' removes the Python variable. | |
64 |
|
63 | |||
65 | You can use the %l specifier in an alias definition to represent the |
|
64 | You can use the %l specifier in an alias definition to represent the | |
66 | whole line when the alias is called. For example:: |
|
65 | whole line when the alias is called. For example:: | |
67 |
|
66 | |||
68 | In [2]: alias bracket echo "Input in brackets: <%l>" |
|
67 | In [2]: alias bracket echo "Input in brackets: <%l>" | |
69 | In [3]: bracket hello world |
|
68 | In [3]: bracket hello world | |
70 | Input in brackets: <hello world> |
|
69 | Input in brackets: <hello world> | |
71 |
|
70 | |||
72 | You can also define aliases with parameters using %s specifiers (one |
|
71 | You can also define aliases with parameters using %s specifiers (one | |
73 | per parameter):: |
|
72 | per parameter):: | |
74 |
|
73 | |||
75 | In [1]: alias parts echo first %s second %s |
|
74 | In [1]: alias parts echo first %s second %s | |
76 | In [2]: %parts A B |
|
75 | In [2]: %parts A B | |
77 | first A second B |
|
76 | first A second B | |
78 | In [3]: %parts A |
|
77 | In [3]: %parts A | |
79 | Incorrect number of arguments: 2 expected. |
|
78 | Incorrect number of arguments: 2 expected. | |
80 | parts is an alias to: 'echo first %s second %s' |
|
79 | parts is an alias to: 'echo first %s second %s' | |
81 |
|
80 | |||
82 | Note that %l and %s are mutually exclusive. You can only use one or |
|
81 | Note that %l and %s are mutually exclusive. You can only use one or | |
83 | the other in your aliases. |
|
82 | the other in your aliases. | |
84 |
|
83 | |||
85 | Aliases expand Python variables just like system calls using ! or !! |
|
84 | Aliases expand Python variables just like system calls using ! or !! | |
86 | do: all expressions prefixed with '$' get expanded. For details of |
|
85 | do: all expressions prefixed with '$' get expanded. For details of | |
87 | the semantic rules, see PEP-215: |
|
86 | the semantic rules, see PEP-215: | |
88 | http://www.python.org/peps/pep-0215.html. This is the library used by |
|
87 | http://www.python.org/peps/pep-0215.html. This is the library used by | |
89 | IPython for variable expansion. If you want to access a true shell |
|
88 | IPython for variable expansion. If you want to access a true shell | |
90 | variable, an extra $ is necessary to prevent its expansion by |
|
89 | variable, an extra $ is necessary to prevent its expansion by | |
91 | IPython:: |
|
90 | IPython:: | |
92 |
|
91 | |||
93 | In [6]: alias show echo |
|
92 | In [6]: alias show echo | |
94 | In [7]: PATH='A Python string' |
|
93 | In [7]: PATH='A Python string' | |
95 | In [8]: show $PATH |
|
94 | In [8]: show $PATH | |
96 | A Python string |
|
95 | A Python string | |
97 | In [9]: show $$PATH |
|
96 | In [9]: show $$PATH | |
98 | /usr/local/lf9560/bin:/usr/local/intel/compiler70/ia32/bin:... |
|
97 | /usr/local/lf9560/bin:/usr/local/intel/compiler70/ia32/bin:... | |
99 |
|
98 | |||
100 | You can use the alias facility to acess all of $PATH. See the %rehashx |
|
99 | You can use the alias facility to acess all of $PATH. See the %rehashx | |
101 | function, which automatically creates aliases for the contents of your |
|
100 | function, which automatically creates aliases for the contents of your | |
102 | $PATH. |
|
101 | $PATH. | |
103 |
|
102 | |||
104 | If called with no parameters, %alias prints the current alias table.""" |
|
103 | If called with no parameters, %alias prints the current alias table.""" | |
105 |
|
104 | |||
106 | par = parameter_s.strip() |
|
105 | par = parameter_s.strip() | |
107 | if not par: |
|
106 | if not par: | |
108 | aliases = sorted(self.shell.alias_manager.aliases) |
|
107 | aliases = sorted(self.shell.alias_manager.aliases) | |
109 | # stored = self.shell.db.get('stored_aliases', {} ) |
|
108 | # stored = self.shell.db.get('stored_aliases', {} ) | |
110 | # for k, v in stored: |
|
109 | # for k, v in stored: | |
111 | # atab.append(k, v[0]) |
|
110 | # atab.append(k, v[0]) | |
112 |
|
111 | |||
113 | print("Total number of aliases:", len(aliases)) |
|
112 | print("Total number of aliases:", len(aliases)) | |
114 | sys.stdout.flush() |
|
113 | sys.stdout.flush() | |
115 | return aliases |
|
114 | return aliases | |
116 |
|
115 | |||
117 | # Now try to define a new one |
|
116 | # Now try to define a new one | |
118 | try: |
|
117 | try: | |
119 | alias,cmd = par.split(None, 1) |
|
118 | alias,cmd = par.split(None, 1) | |
120 | except TypeError: |
|
119 | except TypeError: | |
121 | print(oinspect.getdoc(self.alias)) |
|
120 | print(oinspect.getdoc(self.alias)) | |
122 | return |
|
121 | return | |
123 |
|
122 | |||
124 | try: |
|
123 | try: | |
125 | self.shell.alias_manager.define_alias(alias, cmd) |
|
124 | self.shell.alias_manager.define_alias(alias, cmd) | |
126 | except AliasError as e: |
|
125 | except AliasError as e: | |
127 | print(e) |
|
126 | print(e) | |
128 | # end magic_alias |
|
127 | # end magic_alias | |
129 |
|
128 | |||
130 | @line_magic |
|
129 | @line_magic | |
131 | def unalias(self, parameter_s=''): |
|
130 | def unalias(self, parameter_s=''): | |
132 | """Remove an alias""" |
|
131 | """Remove an alias""" | |
133 |
|
132 | |||
134 | aname = parameter_s.strip() |
|
133 | aname = parameter_s.strip() | |
135 | try: |
|
134 | try: | |
136 | self.shell.alias_manager.undefine_alias(aname) |
|
135 | self.shell.alias_manager.undefine_alias(aname) | |
137 | except ValueError as e: |
|
136 | except ValueError as e: | |
138 | print(e) |
|
137 | print(e) | |
139 | return |
|
138 | return | |
140 |
|
139 | |||
141 | stored = self.shell.db.get('stored_aliases', {} ) |
|
140 | stored = self.shell.db.get('stored_aliases', {} ) | |
142 | if aname in stored: |
|
141 | if aname in stored: | |
143 | print("Removing %stored alias",aname) |
|
142 | print("Removing %stored alias",aname) | |
144 | del stored[aname] |
|
143 | del stored[aname] | |
145 | self.shell.db['stored_aliases'] = stored |
|
144 | self.shell.db['stored_aliases'] = stored | |
146 |
|
145 | |||
147 | @line_magic |
|
146 | @line_magic | |
148 | def rehashx(self, parameter_s=''): |
|
147 | def rehashx(self, parameter_s=''): | |
149 | """Update the alias table with all executable files in $PATH. |
|
148 | """Update the alias table with all executable files in $PATH. | |
150 |
|
149 | |||
151 | rehashx explicitly checks that every entry in $PATH is a file |
|
150 | rehashx explicitly checks that every entry in $PATH is a file | |
152 | with execute access (os.X_OK). |
|
151 | with execute access (os.X_OK). | |
153 |
|
152 | |||
154 | Under Windows, it checks executability as a match against a |
|
153 | Under Windows, it checks executability as a match against a | |
155 | '|'-separated string of extensions, stored in the IPython config |
|
154 | '|'-separated string of extensions, stored in the IPython config | |
156 | variable win_exec_ext. This defaults to 'exe|com|bat'. |
|
155 | variable win_exec_ext. This defaults to 'exe|com|bat'. | |
157 |
|
156 | |||
158 | This function also resets the root module cache of module completer, |
|
157 | This function also resets the root module cache of module completer, | |
159 | used on slow filesystems. |
|
158 | used on slow filesystems. | |
160 | """ |
|
159 | """ | |
161 | from IPython.core.alias import InvalidAliasError |
|
160 | from IPython.core.alias import InvalidAliasError | |
162 |
|
161 | |||
163 | # for the benefit of module completer in ipy_completers.py |
|
162 | # for the benefit of module completer in ipy_completers.py | |
164 | del self.shell.db['rootmodules_cache'] |
|
163 | del self.shell.db['rootmodules_cache'] | |
165 |
|
164 | |||
166 | path = [os.path.abspath(os.path.expanduser(p)) for p in |
|
165 | path = [os.path.abspath(os.path.expanduser(p)) for p in | |
167 | os.environ.get('PATH','').split(os.pathsep)] |
|
166 | os.environ.get('PATH','').split(os.pathsep)] | |
168 |
|
167 | |||
169 | syscmdlist = [] |
|
168 | syscmdlist = [] | |
170 | # Now define isexec in a cross platform manner. |
|
169 | # Now define isexec in a cross platform manner. | |
171 | if os.name == 'posix': |
|
170 | if os.name == 'posix': | |
172 | isexec = lambda fname:os.path.isfile(fname) and \ |
|
171 | isexec = lambda fname:os.path.isfile(fname) and \ | |
173 | os.access(fname,os.X_OK) |
|
172 | os.access(fname,os.X_OK) | |
174 | else: |
|
173 | else: | |
175 | try: |
|
174 | try: | |
176 | winext = os.environ['pathext'].replace(';','|').replace('.','') |
|
175 | winext = os.environ['pathext'].replace(';','|').replace('.','') | |
177 | except KeyError: |
|
176 | except KeyError: | |
178 | winext = 'exe|com|bat|py' |
|
177 | winext = 'exe|com|bat|py' | |
179 | if 'py' not in winext: |
|
178 | if 'py' not in winext: | |
180 | winext += '|py' |
|
179 | winext += '|py' | |
181 | execre = re.compile(r'(.*)\.(%s)$' % winext,re.IGNORECASE) |
|
180 | execre = re.compile(r'(.*)\.(%s)$' % winext,re.IGNORECASE) | |
182 | isexec = lambda fname:os.path.isfile(fname) and execre.match(fname) |
|
181 | isexec = lambda fname:os.path.isfile(fname) and execre.match(fname) | |
183 | savedir = py3compat.getcwd() |
|
182 | savedir = py3compat.getcwd() | |
184 |
|
183 | |||
185 | # Now walk the paths looking for executables to alias. |
|
184 | # Now walk the paths looking for executables to alias. | |
186 | try: |
|
185 | try: | |
187 | # write the whole loop for posix/Windows so we don't have an if in |
|
186 | # write the whole loop for posix/Windows so we don't have an if in | |
188 | # the innermost part |
|
187 | # the innermost part | |
189 | if os.name == 'posix': |
|
188 | if os.name == 'posix': | |
190 | for pdir in path: |
|
189 | for pdir in path: | |
191 | try: |
|
190 | try: | |
192 | os.chdir(pdir) |
|
191 | os.chdir(pdir) | |
193 | dirlist = os.listdir(pdir) |
|
192 | dirlist = os.listdir(pdir) | |
194 | except OSError: |
|
193 | except OSError: | |
195 | continue |
|
194 | continue | |
196 | for ff in dirlist: |
|
195 | for ff in dirlist: | |
197 | if isexec(ff): |
|
196 | if isexec(ff): | |
198 | try: |
|
197 | try: | |
199 | # Removes dots from the name since ipython |
|
198 | # Removes dots from the name since ipython | |
200 | # will assume names with dots to be python. |
|
199 | # will assume names with dots to be python. | |
201 | if not self.shell.alias_manager.is_alias(ff): |
|
200 | if not self.shell.alias_manager.is_alias(ff): | |
202 | self.shell.alias_manager.define_alias( |
|
201 | self.shell.alias_manager.define_alias( | |
203 | ff.replace('.',''), ff) |
|
202 | ff.replace('.',''), ff) | |
204 | except InvalidAliasError: |
|
203 | except InvalidAliasError: | |
205 | pass |
|
204 | pass | |
206 | else: |
|
205 | else: | |
207 | syscmdlist.append(ff) |
|
206 | syscmdlist.append(ff) | |
208 | else: |
|
207 | else: | |
209 | no_alias = Alias.blacklist |
|
208 | no_alias = Alias.blacklist | |
210 | for pdir in path: |
|
209 | for pdir in path: | |
211 | try: |
|
210 | try: | |
212 | os.chdir(pdir) |
|
211 | os.chdir(pdir) | |
213 | dirlist = os.listdir(pdir) |
|
212 | dirlist = os.listdir(pdir) | |
214 | except OSError: |
|
213 | except OSError: | |
215 | continue |
|
214 | continue | |
216 | for ff in dirlist: |
|
215 | for ff in dirlist: | |
217 | base, ext = os.path.splitext(ff) |
|
216 | base, ext = os.path.splitext(ff) | |
218 | if isexec(ff) and base.lower() not in no_alias: |
|
217 | if isexec(ff) and base.lower() not in no_alias: | |
219 | if ext.lower() == '.exe': |
|
218 | if ext.lower() == '.exe': | |
220 | ff = base |
|
219 | ff = base | |
221 | try: |
|
220 | try: | |
222 | # Removes dots from the name since ipython |
|
221 | # Removes dots from the name since ipython | |
223 | # will assume names with dots to be python. |
|
222 | # will assume names with dots to be python. | |
224 | self.shell.alias_manager.define_alias( |
|
223 | self.shell.alias_manager.define_alias( | |
225 | base.lower().replace('.',''), ff) |
|
224 | base.lower().replace('.',''), ff) | |
226 | except InvalidAliasError: |
|
225 | except InvalidAliasError: | |
227 | pass |
|
226 | pass | |
228 | syscmdlist.append(ff) |
|
227 | syscmdlist.append(ff) | |
229 | self.shell.db['syscmdlist'] = syscmdlist |
|
228 | self.shell.db['syscmdlist'] = syscmdlist | |
230 | finally: |
|
229 | finally: | |
231 | os.chdir(savedir) |
|
230 | os.chdir(savedir) | |
232 |
|
231 | |||
233 | @skip_doctest |
|
232 | @skip_doctest | |
234 | @line_magic |
|
233 | @line_magic | |
235 | def pwd(self, parameter_s=''): |
|
234 | def pwd(self, parameter_s=''): | |
236 | """Return the current working directory path. |
|
235 | """Return the current working directory path. | |
237 |
|
236 | |||
238 | Examples |
|
237 | Examples | |
239 | -------- |
|
238 | -------- | |
240 | :: |
|
239 | :: | |
241 |
|
240 | |||
242 | In [9]: pwd |
|
241 | In [9]: pwd | |
243 | Out[9]: '/home/tsuser/sprint/ipython' |
|
242 | Out[9]: '/home/tsuser/sprint/ipython' | |
244 | """ |
|
243 | """ | |
245 | return py3compat.getcwd() |
|
244 | return py3compat.getcwd() | |
246 |
|
245 | |||
247 | @skip_doctest |
|
246 | @skip_doctest | |
248 | @line_magic |
|
247 | @line_magic | |
249 | def cd(self, parameter_s=''): |
|
248 | def cd(self, parameter_s=''): | |
250 | """Change the current working directory. |
|
249 | """Change the current working directory. | |
251 |
|
250 | |||
252 | This command automatically maintains an internal list of directories |
|
251 | This command automatically maintains an internal list of directories | |
253 | you visit during your IPython session, in the variable _dh. The |
|
252 | you visit during your IPython session, in the variable _dh. The | |
254 | command %dhist shows this history nicely formatted. You can also |
|
253 | command %dhist shows this history nicely formatted. You can also | |
255 | do 'cd -<tab>' to see directory history conveniently. |
|
254 | do 'cd -<tab>' to see directory history conveniently. | |
256 |
|
255 | |||
257 | Usage: |
|
256 | Usage: | |
258 |
|
257 | |||
259 | cd 'dir': changes to directory 'dir'. |
|
258 | cd 'dir': changes to directory 'dir'. | |
260 |
|
259 | |||
261 | cd -: changes to the last visited directory. |
|
260 | cd -: changes to the last visited directory. | |
262 |
|
261 | |||
263 | cd -<n>: changes to the n-th directory in the directory history. |
|
262 | cd -<n>: changes to the n-th directory in the directory history. | |
264 |
|
263 | |||
265 | cd --foo: change to directory that matches 'foo' in history |
|
264 | cd --foo: change to directory that matches 'foo' in history | |
266 |
|
265 | |||
267 | cd -b <bookmark_name>: jump to a bookmark set by %bookmark |
|
266 | cd -b <bookmark_name>: jump to a bookmark set by %bookmark | |
268 | (note: cd <bookmark_name> is enough if there is no |
|
267 | (note: cd <bookmark_name> is enough if there is no | |
269 | directory <bookmark_name>, but a bookmark with the name exists.) |
|
268 | directory <bookmark_name>, but a bookmark with the name exists.) | |
270 | 'cd -b <tab>' allows you to tab-complete bookmark names. |
|
269 | 'cd -b <tab>' allows you to tab-complete bookmark names. | |
271 |
|
270 | |||
272 | Options: |
|
271 | Options: | |
273 |
|
272 | |||
274 | -q: quiet. Do not print the working directory after the cd command is |
|
273 | -q: quiet. Do not print the working directory after the cd command is | |
275 | executed. By default IPython's cd command does print this directory, |
|
274 | executed. By default IPython's cd command does print this directory, | |
276 | since the default prompts do not display path information. |
|
275 | since the default prompts do not display path information. | |
277 |
|
276 | |||
278 | Note that !cd doesn't work for this purpose because the shell where |
|
277 | Note that !cd doesn't work for this purpose because the shell where | |
279 | !command runs is immediately discarded after executing 'command'. |
|
278 | !command runs is immediately discarded after executing 'command'. | |
280 |
|
279 | |||
281 | Examples |
|
280 | Examples | |
282 | -------- |
|
281 | -------- | |
283 | :: |
|
282 | :: | |
284 |
|
283 | |||
285 | In [10]: cd parent/child |
|
284 | In [10]: cd parent/child | |
286 | /home/tsuser/parent/child |
|
285 | /home/tsuser/parent/child | |
287 | """ |
|
286 | """ | |
288 |
|
287 | |||
289 | oldcwd = py3compat.getcwd() |
|
288 | oldcwd = py3compat.getcwd() | |
290 | numcd = re.match(r'(-)(\d+)$',parameter_s) |
|
289 | numcd = re.match(r'(-)(\d+)$',parameter_s) | |
291 | # jump in directory history by number |
|
290 | # jump in directory history by number | |
292 | if numcd: |
|
291 | if numcd: | |
293 | nn = int(numcd.group(2)) |
|
292 | nn = int(numcd.group(2)) | |
294 | try: |
|
293 | try: | |
295 | ps = self.shell.user_ns['_dh'][nn] |
|
294 | ps = self.shell.user_ns['_dh'][nn] | |
296 | except IndexError: |
|
295 | except IndexError: | |
297 | print('The requested directory does not exist in history.') |
|
296 | print('The requested directory does not exist in history.') | |
298 | return |
|
297 | return | |
299 | else: |
|
298 | else: | |
300 | opts = {} |
|
299 | opts = {} | |
301 | elif parameter_s.startswith('--'): |
|
300 | elif parameter_s.startswith('--'): | |
302 | ps = None |
|
301 | ps = None | |
303 | fallback = None |
|
302 | fallback = None | |
304 | pat = parameter_s[2:] |
|
303 | pat = parameter_s[2:] | |
305 | dh = self.shell.user_ns['_dh'] |
|
304 | dh = self.shell.user_ns['_dh'] | |
306 | # first search only by basename (last component) |
|
305 | # first search only by basename (last component) | |
307 | for ent in reversed(dh): |
|
306 | for ent in reversed(dh): | |
308 | if pat in os.path.basename(ent) and os.path.isdir(ent): |
|
307 | if pat in os.path.basename(ent) and os.path.isdir(ent): | |
309 | ps = ent |
|
308 | ps = ent | |
310 | break |
|
309 | break | |
311 |
|
310 | |||
312 | if fallback is None and pat in ent and os.path.isdir(ent): |
|
311 | if fallback is None and pat in ent and os.path.isdir(ent): | |
313 | fallback = ent |
|
312 | fallback = ent | |
314 |
|
313 | |||
315 | # if we have no last part match, pick the first full path match |
|
314 | # if we have no last part match, pick the first full path match | |
316 | if ps is None: |
|
315 | if ps is None: | |
317 | ps = fallback |
|
316 | ps = fallback | |
318 |
|
317 | |||
319 | if ps is None: |
|
318 | if ps is None: | |
320 | print("No matching entry in directory history") |
|
319 | print("No matching entry in directory history") | |
321 | return |
|
320 | return | |
322 | else: |
|
321 | else: | |
323 | opts = {} |
|
322 | opts = {} | |
324 |
|
323 | |||
325 |
|
324 | |||
326 | else: |
|
325 | else: | |
327 | #turn all non-space-escaping backslashes to slashes, |
|
326 | opts, ps = self.parse_options(parameter_s, 'qb', mode='string') | |
328 | # for c:\windows\directory\names\ |
|
|||
329 | parameter_s = re.sub(r'\\(?! )','/', parameter_s) |
|
|||
330 | opts,ps = self.parse_options(parameter_s,'qb',mode='string') |
|
|||
331 | # jump to previous |
|
327 | # jump to previous | |
332 | if ps == '-': |
|
328 | if ps == '-': | |
333 | try: |
|
329 | try: | |
334 | ps = self.shell.user_ns['_dh'][-2] |
|
330 | ps = self.shell.user_ns['_dh'][-2] | |
335 | except IndexError: |
|
331 | except IndexError: | |
336 | raise UsageError('%cd -: No previous directory to change to.') |
|
332 | raise UsageError('%cd -: No previous directory to change to.') | |
337 | # jump to bookmark if needed |
|
333 | # jump to bookmark if needed | |
338 | else: |
|
334 | else: | |
339 | if not os.path.isdir(ps) or 'b' in opts: |
|
335 | if not os.path.isdir(ps) or 'b' in opts: | |
340 | bkms = self.shell.db.get('bookmarks', {}) |
|
336 | bkms = self.shell.db.get('bookmarks', {}) | |
341 |
|
337 | |||
342 | if ps in bkms: |
|
338 | if ps in bkms: | |
343 | target = bkms[ps] |
|
339 | target = bkms[ps] | |
344 | print('(bookmark:%s) -> %s' % (ps, target)) |
|
340 | print('(bookmark:%s) -> %s' % (ps, target)) | |
345 | ps = target |
|
341 | ps = target | |
346 | else: |
|
342 | else: | |
347 | if 'b' in opts: |
|
343 | if 'b' in opts: | |
348 | raise UsageError("Bookmark '%s' not found. " |
|
344 | raise UsageError("Bookmark '%s' not found. " | |
349 | "Use '%%bookmark -l' to see your bookmarks." % ps) |
|
345 | "Use '%%bookmark -l' to see your bookmarks." % ps) | |
350 |
|
346 | |||
351 | # strip extra quotes on Windows, because os.chdir doesn't like them |
|
|||
352 | ps = unquote_filename(ps) |
|
|||
353 | # at this point ps should point to the target dir |
|
347 | # at this point ps should point to the target dir | |
354 | if ps: |
|
348 | if ps: | |
355 | try: |
|
349 | try: | |
356 | os.chdir(os.path.expanduser(ps)) |
|
350 | os.chdir(os.path.expanduser(ps)) | |
357 | if hasattr(self.shell, 'term_title') and self.shell.term_title: |
|
351 | if hasattr(self.shell, 'term_title') and self.shell.term_title: | |
358 | set_term_title('IPython: ' + abbrev_cwd()) |
|
352 | set_term_title('IPython: ' + abbrev_cwd()) | |
359 | except OSError: |
|
353 | except OSError: | |
360 | print(sys.exc_info()[1]) |
|
354 | print(sys.exc_info()[1]) | |
361 | else: |
|
355 | else: | |
362 | cwd = py3compat.getcwd() |
|
356 | cwd = py3compat.getcwd() | |
363 | dhist = self.shell.user_ns['_dh'] |
|
357 | dhist = self.shell.user_ns['_dh'] | |
364 | if oldcwd != cwd: |
|
358 | if oldcwd != cwd: | |
365 | dhist.append(cwd) |
|
359 | dhist.append(cwd) | |
366 | self.shell.db['dhist'] = compress_dhist(dhist)[-100:] |
|
360 | self.shell.db['dhist'] = compress_dhist(dhist)[-100:] | |
367 |
|
361 | |||
368 | else: |
|
362 | else: | |
369 | os.chdir(self.shell.home_dir) |
|
363 | os.chdir(self.shell.home_dir) | |
370 | if hasattr(self.shell, 'term_title') and self.shell.term_title: |
|
364 | if hasattr(self.shell, 'term_title') and self.shell.term_title: | |
371 | set_term_title('IPython: ' + '~') |
|
365 | set_term_title('IPython: ' + '~') | |
372 | cwd = py3compat.getcwd() |
|
366 | cwd = py3compat.getcwd() | |
373 | dhist = self.shell.user_ns['_dh'] |
|
367 | dhist = self.shell.user_ns['_dh'] | |
374 |
|
368 | |||
375 | if oldcwd != cwd: |
|
369 | if oldcwd != cwd: | |
376 | dhist.append(cwd) |
|
370 | dhist.append(cwd) | |
377 | self.shell.db['dhist'] = compress_dhist(dhist)[-100:] |
|
371 | self.shell.db['dhist'] = compress_dhist(dhist)[-100:] | |
378 | if not 'q' in opts and self.shell.user_ns['_dh']: |
|
372 | if not 'q' in opts and self.shell.user_ns['_dh']: | |
379 | print(self.shell.user_ns['_dh'][-1]) |
|
373 | print(self.shell.user_ns['_dh'][-1]) | |
380 |
|
374 | |||
381 | @line_magic |
|
375 | @line_magic | |
382 | def env(self, parameter_s=''): |
|
376 | def env(self, parameter_s=''): | |
383 | """Get, set, or list environment variables. |
|
377 | """Get, set, or list environment variables. | |
384 |
|
378 | |||
385 | Usage:\\ |
|
379 | Usage:\\ | |
386 |
|
380 | |||
387 | %env: lists all environment variables/values |
|
381 | %env: lists all environment variables/values | |
388 | %env var: get value for var |
|
382 | %env var: get value for var | |
389 | %env var val: set value for var |
|
383 | %env var val: set value for var | |
390 | %env var=val: set value for var |
|
384 | %env var=val: set value for var | |
391 | %env var=$val: set value for var, using python expansion if possible |
|
385 | %env var=$val: set value for var, using python expansion if possible | |
392 | """ |
|
386 | """ | |
393 | if parameter_s.strip(): |
|
387 | if parameter_s.strip(): | |
394 | split = '=' if '=' in parameter_s else ' ' |
|
388 | split = '=' if '=' in parameter_s else ' ' | |
395 | bits = parameter_s.split(split) |
|
389 | bits = parameter_s.split(split) | |
396 | if len(bits) == 1: |
|
390 | if len(bits) == 1: | |
397 | key = parameter_s.strip() |
|
391 | key = parameter_s.strip() | |
398 | if key in os.environ: |
|
392 | if key in os.environ: | |
399 | return os.environ[key] |
|
393 | return os.environ[key] | |
400 | else: |
|
394 | else: | |
401 | err = "Environment does not have key: {0}".format(key) |
|
395 | err = "Environment does not have key: {0}".format(key) | |
402 | raise UsageError(err) |
|
396 | raise UsageError(err) | |
403 | if len(bits) > 1: |
|
397 | if len(bits) > 1: | |
404 | return self.set_env(parameter_s) |
|
398 | return self.set_env(parameter_s) | |
405 | return dict(os.environ) |
|
399 | return dict(os.environ) | |
406 |
|
400 | |||
407 | @line_magic |
|
401 | @line_magic | |
408 | def set_env(self, parameter_s): |
|
402 | def set_env(self, parameter_s): | |
409 | """Set environment variables. Assumptions are that either "val" is a |
|
403 | """Set environment variables. Assumptions are that either "val" is a | |
410 | name in the user namespace, or val is something that evaluates to a |
|
404 | name in the user namespace, or val is something that evaluates to a | |
411 | string. |
|
405 | string. | |
412 |
|
406 | |||
413 | Usage:\\ |
|
407 | Usage:\\ | |
414 | %set_env var val: set value for var |
|
408 | %set_env var val: set value for var | |
415 | %set_env var=val: set value for var |
|
409 | %set_env var=val: set value for var | |
416 | %set_env var=$val: set value for var, using python expansion if possible |
|
410 | %set_env var=$val: set value for var, using python expansion if possible | |
417 | """ |
|
411 | """ | |
418 | split = '=' if '=' in parameter_s else ' ' |
|
412 | split = '=' if '=' in parameter_s else ' ' | |
419 | bits = parameter_s.split(split, 1) |
|
413 | bits = parameter_s.split(split, 1) | |
420 | if not parameter_s.strip() or len(bits)<2: |
|
414 | if not parameter_s.strip() or len(bits)<2: | |
421 | raise UsageError("usage is 'set_env var=val'") |
|
415 | raise UsageError("usage is 'set_env var=val'") | |
422 | var = bits[0].strip() |
|
416 | var = bits[0].strip() | |
423 | val = bits[1].strip() |
|
417 | val = bits[1].strip() | |
424 | if re.match(r'.*\s.*', var): |
|
418 | if re.match(r'.*\s.*', var): | |
425 | # an environment variable with whitespace is almost certainly |
|
419 | # an environment variable with whitespace is almost certainly | |
426 | # not what the user intended. what's more likely is the wrong |
|
420 | # not what the user intended. what's more likely is the wrong | |
427 | # split was chosen, ie for "set_env cmd_args A=B", we chose |
|
421 | # split was chosen, ie for "set_env cmd_args A=B", we chose | |
428 | # '=' for the split and should have chosen ' '. to get around |
|
422 | # '=' for the split and should have chosen ' '. to get around | |
429 | # this, users should just assign directly to os.environ or use |
|
423 | # this, users should just assign directly to os.environ or use | |
430 | # standard magic {var} expansion. |
|
424 | # standard magic {var} expansion. | |
431 | err = "refusing to set env var with whitespace: '{0}'" |
|
425 | err = "refusing to set env var with whitespace: '{0}'" | |
432 | err = err.format(val) |
|
426 | err = err.format(val) | |
433 | raise UsageError(err) |
|
427 | raise UsageError(err) | |
434 | os.environ[py3compat.cast_bytes_py2(var)] = py3compat.cast_bytes_py2(val) |
|
428 | os.environ[py3compat.cast_bytes_py2(var)] = py3compat.cast_bytes_py2(val) | |
435 | print('env: {0}={1}'.format(var,val)) |
|
429 | print('env: {0}={1}'.format(var,val)) | |
436 |
|
430 | |||
437 | @line_magic |
|
431 | @line_magic | |
438 | def pushd(self, parameter_s=''): |
|
432 | def pushd(self, parameter_s=''): | |
439 | """Place the current dir on stack and change directory. |
|
433 | """Place the current dir on stack and change directory. | |
440 |
|
434 | |||
441 | Usage:\\ |
|
435 | Usage:\\ | |
442 | %pushd ['dirname'] |
|
436 | %pushd ['dirname'] | |
443 | """ |
|
437 | """ | |
444 |
|
438 | |||
445 | dir_s = self.shell.dir_stack |
|
439 | dir_s = self.shell.dir_stack | |
446 |
tgt = os.path.expanduser( |
|
440 | tgt = os.path.expanduser(parameter_s) | |
447 | cwd = py3compat.getcwd().replace(self.shell.home_dir,'~') |
|
441 | cwd = py3compat.getcwd().replace(self.shell.home_dir,'~') | |
448 | if tgt: |
|
442 | if tgt: | |
449 | self.cd(parameter_s) |
|
443 | self.cd(parameter_s) | |
450 | dir_s.insert(0,cwd) |
|
444 | dir_s.insert(0,cwd) | |
451 | return self.shell.magic('dirs') |
|
445 | return self.shell.magic('dirs') | |
452 |
|
446 | |||
453 | @line_magic |
|
447 | @line_magic | |
454 | def popd(self, parameter_s=''): |
|
448 | def popd(self, parameter_s=''): | |
455 | """Change to directory popped off the top of the stack. |
|
449 | """Change to directory popped off the top of the stack. | |
456 | """ |
|
450 | """ | |
457 | if not self.shell.dir_stack: |
|
451 | if not self.shell.dir_stack: | |
458 | raise UsageError("%popd on empty stack") |
|
452 | raise UsageError("%popd on empty stack") | |
459 | top = self.shell.dir_stack.pop(0) |
|
453 | top = self.shell.dir_stack.pop(0) | |
460 | self.cd(top) |
|
454 | self.cd(top) | |
461 | print("popd ->",top) |
|
455 | print("popd ->",top) | |
462 |
|
456 | |||
463 | @line_magic |
|
457 | @line_magic | |
464 | def dirs(self, parameter_s=''): |
|
458 | def dirs(self, parameter_s=''): | |
465 | """Return the current directory stack.""" |
|
459 | """Return the current directory stack.""" | |
466 |
|
460 | |||
467 | return self.shell.dir_stack |
|
461 | return self.shell.dir_stack | |
468 |
|
462 | |||
469 | @line_magic |
|
463 | @line_magic | |
470 | def dhist(self, parameter_s=''): |
|
464 | def dhist(self, parameter_s=''): | |
471 | """Print your history of visited directories. |
|
465 | """Print your history of visited directories. | |
472 |
|
466 | |||
473 | %dhist -> print full history\\ |
|
467 | %dhist -> print full history\\ | |
474 | %dhist n -> print last n entries only\\ |
|
468 | %dhist n -> print last n entries only\\ | |
475 | %dhist n1 n2 -> print entries between n1 and n2 (n2 not included)\\ |
|
469 | %dhist n1 n2 -> print entries between n1 and n2 (n2 not included)\\ | |
476 |
|
470 | |||
477 | This history is automatically maintained by the %cd command, and |
|
471 | This history is automatically maintained by the %cd command, and | |
478 | always available as the global list variable _dh. You can use %cd -<n> |
|
472 | always available as the global list variable _dh. You can use %cd -<n> | |
479 | to go to directory number <n>. |
|
473 | to go to directory number <n>. | |
480 |
|
474 | |||
481 | Note that most of time, you should view directory history by entering |
|
475 | Note that most of time, you should view directory history by entering | |
482 | cd -<TAB>. |
|
476 | cd -<TAB>. | |
483 |
|
477 | |||
484 | """ |
|
478 | """ | |
485 |
|
479 | |||
486 | dh = self.shell.user_ns['_dh'] |
|
480 | dh = self.shell.user_ns['_dh'] | |
487 | if parameter_s: |
|
481 | if parameter_s: | |
488 | try: |
|
482 | try: | |
489 | args = map(int,parameter_s.split()) |
|
483 | args = map(int,parameter_s.split()) | |
490 | except: |
|
484 | except: | |
491 | self.arg_err(self.dhist) |
|
485 | self.arg_err(self.dhist) | |
492 | return |
|
486 | return | |
493 | if len(args) == 1: |
|
487 | if len(args) == 1: | |
494 | ini,fin = max(len(dh)-(args[0]),0),len(dh) |
|
488 | ini,fin = max(len(dh)-(args[0]),0),len(dh) | |
495 | elif len(args) == 2: |
|
489 | elif len(args) == 2: | |
496 | ini,fin = args |
|
490 | ini,fin = args | |
497 | fin = min(fin, len(dh)) |
|
491 | fin = min(fin, len(dh)) | |
498 | else: |
|
492 | else: | |
499 | self.arg_err(self.dhist) |
|
493 | self.arg_err(self.dhist) | |
500 | return |
|
494 | return | |
501 | else: |
|
495 | else: | |
502 | ini,fin = 0,len(dh) |
|
496 | ini,fin = 0,len(dh) | |
503 | print('Directory history (kept in _dh)') |
|
497 | print('Directory history (kept in _dh)') | |
504 | for i in range(ini, fin): |
|
498 | for i in range(ini, fin): | |
505 | print("%d: %s" % (i, dh[i])) |
|
499 | print("%d: %s" % (i, dh[i])) | |
506 |
|
500 | |||
507 | @skip_doctest |
|
501 | @skip_doctest | |
508 | @line_magic |
|
502 | @line_magic | |
509 | def sc(self, parameter_s=''): |
|
503 | def sc(self, parameter_s=''): | |
510 | """Shell capture - run shell command and capture output (DEPRECATED use !). |
|
504 | """Shell capture - run shell command and capture output (DEPRECATED use !). | |
511 |
|
505 | |||
512 | DEPRECATED. Suboptimal, retained for backwards compatibility. |
|
506 | DEPRECATED. Suboptimal, retained for backwards compatibility. | |
513 |
|
507 | |||
514 | You should use the form 'var = !command' instead. Example: |
|
508 | You should use the form 'var = !command' instead. Example: | |
515 |
|
509 | |||
516 | "%sc -l myfiles = ls ~" should now be written as |
|
510 | "%sc -l myfiles = ls ~" should now be written as | |
517 |
|
511 | |||
518 | "myfiles = !ls ~" |
|
512 | "myfiles = !ls ~" | |
519 |
|
513 | |||
520 | myfiles.s, myfiles.l and myfiles.n still apply as documented |
|
514 | myfiles.s, myfiles.l and myfiles.n still apply as documented | |
521 | below. |
|
515 | below. | |
522 |
|
516 | |||
523 | -- |
|
517 | -- | |
524 | %sc [options] varname=command |
|
518 | %sc [options] varname=command | |
525 |
|
519 | |||
526 | IPython will run the given command using commands.getoutput(), and |
|
520 | IPython will run the given command using commands.getoutput(), and | |
527 | will then update the user's interactive namespace with a variable |
|
521 | will then update the user's interactive namespace with a variable | |
528 | called varname, containing the value of the call. Your command can |
|
522 | called varname, containing the value of the call. Your command can | |
529 | contain shell wildcards, pipes, etc. |
|
523 | contain shell wildcards, pipes, etc. | |
530 |
|
524 | |||
531 | The '=' sign in the syntax is mandatory, and the variable name you |
|
525 | The '=' sign in the syntax is mandatory, and the variable name you | |
532 | supply must follow Python's standard conventions for valid names. |
|
526 | supply must follow Python's standard conventions for valid names. | |
533 |
|
527 | |||
534 | (A special format without variable name exists for internal use) |
|
528 | (A special format without variable name exists for internal use) | |
535 |
|
529 | |||
536 | Options: |
|
530 | Options: | |
537 |
|
531 | |||
538 | -l: list output. Split the output on newlines into a list before |
|
532 | -l: list output. Split the output on newlines into a list before | |
539 | assigning it to the given variable. By default the output is stored |
|
533 | assigning it to the given variable. By default the output is stored | |
540 | as a single string. |
|
534 | as a single string. | |
541 |
|
535 | |||
542 | -v: verbose. Print the contents of the variable. |
|
536 | -v: verbose. Print the contents of the variable. | |
543 |
|
537 | |||
544 | In most cases you should not need to split as a list, because the |
|
538 | In most cases you should not need to split as a list, because the | |
545 | returned value is a special type of string which can automatically |
|
539 | returned value is a special type of string which can automatically | |
546 | provide its contents either as a list (split on newlines) or as a |
|
540 | provide its contents either as a list (split on newlines) or as a | |
547 | space-separated string. These are convenient, respectively, either |
|
541 | space-separated string. These are convenient, respectively, either | |
548 | for sequential processing or to be passed to a shell command. |
|
542 | for sequential processing or to be passed to a shell command. | |
549 |
|
543 | |||
550 | For example:: |
|
544 | For example:: | |
551 |
|
545 | |||
552 | # Capture into variable a |
|
546 | # Capture into variable a | |
553 | In [1]: sc a=ls *py |
|
547 | In [1]: sc a=ls *py | |
554 |
|
548 | |||
555 | # a is a string with embedded newlines |
|
549 | # a is a string with embedded newlines | |
556 | In [2]: a |
|
550 | In [2]: a | |
557 | Out[2]: 'setup.py\\nwin32_manual_post_install.py' |
|
551 | Out[2]: 'setup.py\\nwin32_manual_post_install.py' | |
558 |
|
552 | |||
559 | # which can be seen as a list: |
|
553 | # which can be seen as a list: | |
560 | In [3]: a.l |
|
554 | In [3]: a.l | |
561 | Out[3]: ['setup.py', 'win32_manual_post_install.py'] |
|
555 | Out[3]: ['setup.py', 'win32_manual_post_install.py'] | |
562 |
|
556 | |||
563 | # or as a whitespace-separated string: |
|
557 | # or as a whitespace-separated string: | |
564 | In [4]: a.s |
|
558 | In [4]: a.s | |
565 | Out[4]: 'setup.py win32_manual_post_install.py' |
|
559 | Out[4]: 'setup.py win32_manual_post_install.py' | |
566 |
|
560 | |||
567 | # a.s is useful to pass as a single command line: |
|
561 | # a.s is useful to pass as a single command line: | |
568 | In [5]: !wc -l $a.s |
|
562 | In [5]: !wc -l $a.s | |
569 | 146 setup.py |
|
563 | 146 setup.py | |
570 | 130 win32_manual_post_install.py |
|
564 | 130 win32_manual_post_install.py | |
571 | 276 total |
|
565 | 276 total | |
572 |
|
566 | |||
573 | # while the list form is useful to loop over: |
|
567 | # while the list form is useful to loop over: | |
574 | In [6]: for f in a.l: |
|
568 | In [6]: for f in a.l: | |
575 | ...: !wc -l $f |
|
569 | ...: !wc -l $f | |
576 | ...: |
|
570 | ...: | |
577 | 146 setup.py |
|
571 | 146 setup.py | |
578 | 130 win32_manual_post_install.py |
|
572 | 130 win32_manual_post_install.py | |
579 |
|
573 | |||
580 | Similarly, the lists returned by the -l option are also special, in |
|
574 | Similarly, the lists returned by the -l option are also special, in | |
581 | the sense that you can equally invoke the .s attribute on them to |
|
575 | the sense that you can equally invoke the .s attribute on them to | |
582 | automatically get a whitespace-separated string from their contents:: |
|
576 | automatically get a whitespace-separated string from their contents:: | |
583 |
|
577 | |||
584 | In [7]: sc -l b=ls *py |
|
578 | In [7]: sc -l b=ls *py | |
585 |
|
579 | |||
586 | In [8]: b |
|
580 | In [8]: b | |
587 | Out[8]: ['setup.py', 'win32_manual_post_install.py'] |
|
581 | Out[8]: ['setup.py', 'win32_manual_post_install.py'] | |
588 |
|
582 | |||
589 | In [9]: b.s |
|
583 | In [9]: b.s | |
590 | Out[9]: 'setup.py win32_manual_post_install.py' |
|
584 | Out[9]: 'setup.py win32_manual_post_install.py' | |
591 |
|
585 | |||
592 | In summary, both the lists and strings used for output capture have |
|
586 | In summary, both the lists and strings used for output capture have | |
593 | the following special attributes:: |
|
587 | the following special attributes:: | |
594 |
|
588 | |||
595 | .l (or .list) : value as list. |
|
589 | .l (or .list) : value as list. | |
596 | .n (or .nlstr): value as newline-separated string. |
|
590 | .n (or .nlstr): value as newline-separated string. | |
597 | .s (or .spstr): value as space-separated string. |
|
591 | .s (or .spstr): value as space-separated string. | |
598 | """ |
|
592 | """ | |
599 |
|
593 | |||
600 | opts,args = self.parse_options(parameter_s, 'lv') |
|
594 | opts,args = self.parse_options(parameter_s, 'lv') | |
601 | # Try to get a variable name and command to run |
|
595 | # Try to get a variable name and command to run | |
602 | try: |
|
596 | try: | |
603 | # the variable name must be obtained from the parse_options |
|
597 | # the variable name must be obtained from the parse_options | |
604 | # output, which uses shlex.split to strip options out. |
|
598 | # output, which uses shlex.split to strip options out. | |
605 | var,_ = args.split('=', 1) |
|
599 | var,_ = args.split('=', 1) | |
606 | var = var.strip() |
|
600 | var = var.strip() | |
607 | # But the command has to be extracted from the original input |
|
601 | # But the command has to be extracted from the original input | |
608 | # parameter_s, not on what parse_options returns, to avoid the |
|
602 | # parameter_s, not on what parse_options returns, to avoid the | |
609 | # quote stripping which shlex.split performs on it. |
|
603 | # quote stripping which shlex.split performs on it. | |
610 | _,cmd = parameter_s.split('=', 1) |
|
604 | _,cmd = parameter_s.split('=', 1) | |
611 | except ValueError: |
|
605 | except ValueError: | |
612 | var,cmd = '','' |
|
606 | var,cmd = '','' | |
613 | # If all looks ok, proceed |
|
607 | # If all looks ok, proceed | |
614 | split = 'l' in opts |
|
608 | split = 'l' in opts | |
615 | out = self.shell.getoutput(cmd, split=split) |
|
609 | out = self.shell.getoutput(cmd, split=split) | |
616 | if 'v' in opts: |
|
610 | if 'v' in opts: | |
617 | print('%s ==\n%s' % (var, pformat(out))) |
|
611 | print('%s ==\n%s' % (var, pformat(out))) | |
618 | if var: |
|
612 | if var: | |
619 | self.shell.user_ns.update({var:out}) |
|
613 | self.shell.user_ns.update({var:out}) | |
620 | else: |
|
614 | else: | |
621 | return out |
|
615 | return out | |
622 |
|
616 | |||
623 | @line_cell_magic |
|
617 | @line_cell_magic | |
624 | def sx(self, line='', cell=None): |
|
618 | def sx(self, line='', cell=None): | |
625 | """Shell execute - run shell command and capture output (!! is short-hand). |
|
619 | """Shell execute - run shell command and capture output (!! is short-hand). | |
626 |
|
620 | |||
627 | %sx command |
|
621 | %sx command | |
628 |
|
622 | |||
629 | IPython will run the given command using commands.getoutput(), and |
|
623 | IPython will run the given command using commands.getoutput(), and | |
630 | return the result formatted as a list (split on '\\n'). Since the |
|
624 | return the result formatted as a list (split on '\\n'). Since the | |
631 | output is _returned_, it will be stored in ipython's regular output |
|
625 | output is _returned_, it will be stored in ipython's regular output | |
632 | cache Out[N] and in the '_N' automatic variables. |
|
626 | cache Out[N] and in the '_N' automatic variables. | |
633 |
|
627 | |||
634 | Notes: |
|
628 | Notes: | |
635 |
|
629 | |||
636 | 1) If an input line begins with '!!', then %sx is automatically |
|
630 | 1) If an input line begins with '!!', then %sx is automatically | |
637 | invoked. That is, while:: |
|
631 | invoked. That is, while:: | |
638 |
|
632 | |||
639 | !ls |
|
633 | !ls | |
640 |
|
634 | |||
641 | causes ipython to simply issue system('ls'), typing:: |
|
635 | causes ipython to simply issue system('ls'), typing:: | |
642 |
|
636 | |||
643 | !!ls |
|
637 | !!ls | |
644 |
|
638 | |||
645 | is a shorthand equivalent to:: |
|
639 | is a shorthand equivalent to:: | |
646 |
|
640 | |||
647 | %sx ls |
|
641 | %sx ls | |
648 |
|
642 | |||
649 | 2) %sx differs from %sc in that %sx automatically splits into a list, |
|
643 | 2) %sx differs from %sc in that %sx automatically splits into a list, | |
650 | like '%sc -l'. The reason for this is to make it as easy as possible |
|
644 | like '%sc -l'. The reason for this is to make it as easy as possible | |
651 | to process line-oriented shell output via further python commands. |
|
645 | to process line-oriented shell output via further python commands. | |
652 | %sc is meant to provide much finer control, but requires more |
|
646 | %sc is meant to provide much finer control, but requires more | |
653 | typing. |
|
647 | typing. | |
654 |
|
648 | |||
655 | 3) Just like %sc -l, this is a list with special attributes: |
|
649 | 3) Just like %sc -l, this is a list with special attributes: | |
656 | :: |
|
650 | :: | |
657 |
|
651 | |||
658 | .l (or .list) : value as list. |
|
652 | .l (or .list) : value as list. | |
659 | .n (or .nlstr): value as newline-separated string. |
|
653 | .n (or .nlstr): value as newline-separated string. | |
660 | .s (or .spstr): value as whitespace-separated string. |
|
654 | .s (or .spstr): value as whitespace-separated string. | |
661 |
|
655 | |||
662 | This is very useful when trying to use such lists as arguments to |
|
656 | This is very useful when trying to use such lists as arguments to | |
663 | system commands.""" |
|
657 | system commands.""" | |
664 |
|
658 | |||
665 | if cell is None: |
|
659 | if cell is None: | |
666 | # line magic |
|
660 | # line magic | |
667 | return self.shell.getoutput(line) |
|
661 | return self.shell.getoutput(line) | |
668 | else: |
|
662 | else: | |
669 | opts,args = self.parse_options(line, '', 'out=') |
|
663 | opts,args = self.parse_options(line, '', 'out=') | |
670 | output = self.shell.getoutput(cell) |
|
664 | output = self.shell.getoutput(cell) | |
671 | out_name = opts.get('out', opts.get('o')) |
|
665 | out_name = opts.get('out', opts.get('o')) | |
672 | if out_name: |
|
666 | if out_name: | |
673 | self.shell.user_ns[out_name] = output |
|
667 | self.shell.user_ns[out_name] = output | |
674 | else: |
|
668 | else: | |
675 | return output |
|
669 | return output | |
676 |
|
670 | |||
677 | system = line_cell_magic('system')(sx) |
|
671 | system = line_cell_magic('system')(sx) | |
678 | bang = cell_magic('!')(sx) |
|
672 | bang = cell_magic('!')(sx) | |
679 |
|
673 | |||
680 | @line_magic |
|
674 | @line_magic | |
681 | def bookmark(self, parameter_s=''): |
|
675 | def bookmark(self, parameter_s=''): | |
682 | """Manage IPython's bookmark system. |
|
676 | """Manage IPython's bookmark system. | |
683 |
|
677 | |||
684 | %bookmark <name> - set bookmark to current dir |
|
678 | %bookmark <name> - set bookmark to current dir | |
685 | %bookmark <name> <dir> - set bookmark to <dir> |
|
679 | %bookmark <name> <dir> - set bookmark to <dir> | |
686 | %bookmark -l - list all bookmarks |
|
680 | %bookmark -l - list all bookmarks | |
687 | %bookmark -d <name> - remove bookmark |
|
681 | %bookmark -d <name> - remove bookmark | |
688 | %bookmark -r - remove all bookmarks |
|
682 | %bookmark -r - remove all bookmarks | |
689 |
|
683 | |||
690 | You can later on access a bookmarked folder with:: |
|
684 | You can later on access a bookmarked folder with:: | |
691 |
|
685 | |||
692 | %cd -b <name> |
|
686 | %cd -b <name> | |
693 |
|
687 | |||
694 | or simply '%cd <name>' if there is no directory called <name> AND |
|
688 | or simply '%cd <name>' if there is no directory called <name> AND | |
695 | there is such a bookmark defined. |
|
689 | there is such a bookmark defined. | |
696 |
|
690 | |||
697 | Your bookmarks persist through IPython sessions, but they are |
|
691 | Your bookmarks persist through IPython sessions, but they are | |
698 | associated with each profile.""" |
|
692 | associated with each profile.""" | |
699 |
|
693 | |||
700 | opts,args = self.parse_options(parameter_s,'drl',mode='list') |
|
694 | opts,args = self.parse_options(parameter_s,'drl',mode='list') | |
701 | if len(args) > 2: |
|
695 | if len(args) > 2: | |
702 | raise UsageError("%bookmark: too many arguments") |
|
696 | raise UsageError("%bookmark: too many arguments") | |
703 |
|
697 | |||
704 | bkms = self.shell.db.get('bookmarks',{}) |
|
698 | bkms = self.shell.db.get('bookmarks',{}) | |
705 |
|
699 | |||
706 | if 'd' in opts: |
|
700 | if 'd' in opts: | |
707 | try: |
|
701 | try: | |
708 | todel = args[0] |
|
702 | todel = args[0] | |
709 | except IndexError: |
|
703 | except IndexError: | |
710 | raise UsageError( |
|
704 | raise UsageError( | |
711 | "%bookmark -d: must provide a bookmark to delete") |
|
705 | "%bookmark -d: must provide a bookmark to delete") | |
712 | else: |
|
706 | else: | |
713 | try: |
|
707 | try: | |
714 | del bkms[todel] |
|
708 | del bkms[todel] | |
715 | except KeyError: |
|
709 | except KeyError: | |
716 | raise UsageError( |
|
710 | raise UsageError( | |
717 | "%%bookmark -d: Can't delete bookmark '%s'" % todel) |
|
711 | "%%bookmark -d: Can't delete bookmark '%s'" % todel) | |
718 |
|
712 | |||
719 | elif 'r' in opts: |
|
713 | elif 'r' in opts: | |
720 | bkms = {} |
|
714 | bkms = {} | |
721 | elif 'l' in opts: |
|
715 | elif 'l' in opts: | |
722 | bks = sorted(bkms) |
|
716 | bks = sorted(bkms) | |
723 | if bks: |
|
717 | if bks: | |
724 | size = max(map(len, bks)) |
|
718 | size = max(map(len, bks)) | |
725 | else: |
|
719 | else: | |
726 | size = 0 |
|
720 | size = 0 | |
727 | fmt = '%-'+str(size)+'s -> %s' |
|
721 | fmt = '%-'+str(size)+'s -> %s' | |
728 | print('Current bookmarks:') |
|
722 | print('Current bookmarks:') | |
729 | for bk in bks: |
|
723 | for bk in bks: | |
730 | print(fmt % (bk, bkms[bk])) |
|
724 | print(fmt % (bk, bkms[bk])) | |
731 | else: |
|
725 | else: | |
732 | if not args: |
|
726 | if not args: | |
733 | raise UsageError("%bookmark: You must specify the bookmark name") |
|
727 | raise UsageError("%bookmark: You must specify the bookmark name") | |
734 | elif len(args)==1: |
|
728 | elif len(args)==1: | |
735 | bkms[args[0]] = py3compat.getcwd() |
|
729 | bkms[args[0]] = py3compat.getcwd() | |
736 | elif len(args)==2: |
|
730 | elif len(args)==2: | |
737 | bkms[args[0]] = args[1] |
|
731 | bkms[args[0]] = args[1] | |
738 | self.shell.db['bookmarks'] = bkms |
|
732 | self.shell.db['bookmarks'] = bkms | |
739 |
|
733 | |||
740 | @line_magic |
|
734 | @line_magic | |
741 | def pycat(self, parameter_s=''): |
|
735 | def pycat(self, parameter_s=''): | |
742 | """Show a syntax-highlighted file through a pager. |
|
736 | """Show a syntax-highlighted file through a pager. | |
743 |
|
737 | |||
744 | This magic is similar to the cat utility, but it will assume the file |
|
738 | This magic is similar to the cat utility, but it will assume the file | |
745 | to be Python source and will show it with syntax highlighting. |
|
739 | to be Python source and will show it with syntax highlighting. | |
746 |
|
740 | |||
747 | This magic command can either take a local filename, an url, |
|
741 | This magic command can either take a local filename, an url, | |
748 | an history range (see %history) or a macro as argument :: |
|
742 | an history range (see %history) or a macro as argument :: | |
749 |
|
743 | |||
750 | %pycat myscript.py |
|
744 | %pycat myscript.py | |
751 | %pycat 7-27 |
|
745 | %pycat 7-27 | |
752 | %pycat myMacro |
|
746 | %pycat myMacro | |
753 | %pycat http://www.example.com/myscript.py |
|
747 | %pycat http://www.example.com/myscript.py | |
754 | """ |
|
748 | """ | |
755 | if not parameter_s: |
|
749 | if not parameter_s: | |
756 | raise UsageError('Missing filename, URL, input history range, ' |
|
750 | raise UsageError('Missing filename, URL, input history range, ' | |
757 | 'or macro.') |
|
751 | 'or macro.') | |
758 |
|
752 | |||
759 | try : |
|
753 | try : | |
760 | cont = self.shell.find_user_code(parameter_s, skip_encoding_cookie=False) |
|
754 | cont = self.shell.find_user_code(parameter_s, skip_encoding_cookie=False) | |
761 | except (ValueError, IOError): |
|
755 | except (ValueError, IOError): | |
762 | print("Error: no such file, variable, URL, history range or macro") |
|
756 | print("Error: no such file, variable, URL, history range or macro") | |
763 | return |
|
757 | return | |
764 |
|
758 | |||
765 | page.page(self.shell.pycolorize(source_to_unicode(cont))) |
|
759 | page.page(self.shell.pycolorize(source_to_unicode(cont))) | |
766 |
|
760 | |||
767 | @magic_arguments.magic_arguments() |
|
761 | @magic_arguments.magic_arguments() | |
768 | @magic_arguments.argument( |
|
762 | @magic_arguments.argument( | |
769 | '-a', '--append', action='store_true', default=False, |
|
763 | '-a', '--append', action='store_true', default=False, | |
770 | help='Append contents of the cell to an existing file. ' |
|
764 | help='Append contents of the cell to an existing file. ' | |
771 | 'The file will be created if it does not exist.' |
|
765 | 'The file will be created if it does not exist.' | |
772 | ) |
|
766 | ) | |
773 | @magic_arguments.argument( |
|
767 | @magic_arguments.argument( | |
774 | 'filename', type=unicode_type, |
|
768 | 'filename', type=unicode_type, | |
775 | help='file to write' |
|
769 | help='file to write' | |
776 | ) |
|
770 | ) | |
777 | @cell_magic |
|
771 | @cell_magic | |
778 | def writefile(self, line, cell): |
|
772 | def writefile(self, line, cell): | |
779 | """Write the contents of the cell to a file. |
|
773 | """Write the contents of the cell to a file. | |
780 |
|
774 | |||
781 | The file will be overwritten unless the -a (--append) flag is specified. |
|
775 | The file will be overwritten unless the -a (--append) flag is specified. | |
782 | """ |
|
776 | """ | |
783 | args = magic_arguments.parse_argstring(self.writefile, line) |
|
777 | args = magic_arguments.parse_argstring(self.writefile, line) | |
784 |
filename = os.path.expanduser( |
|
778 | filename = os.path.expanduser(args.filename) | |
785 |
|
779 | |||
786 | if os.path.exists(filename): |
|
780 | if os.path.exists(filename): | |
787 | if args.append: |
|
781 | if args.append: | |
788 | print("Appending to %s" % filename) |
|
782 | print("Appending to %s" % filename) | |
789 | else: |
|
783 | else: | |
790 | print("Overwriting %s" % filename) |
|
784 | print("Overwriting %s" % filename) | |
791 | else: |
|
785 | else: | |
792 | print("Writing %s" % filename) |
|
786 | print("Writing %s" % filename) | |
793 |
|
787 | |||
794 | mode = 'a' if args.append else 'w' |
|
788 | mode = 'a' if args.append else 'w' | |
795 | with io.open(filename, mode, encoding='utf-8') as f: |
|
789 | with io.open(filename, mode, encoding='utf-8') as f: | |
796 | f.write(cell) |
|
790 | f.write(cell) |
@@ -1,796 +1,802 b'' | |||||
1 | # encoding: utf-8 |
|
1 | # encoding: utf-8 | |
2 | """Tests for the IPython tab-completion machinery.""" |
|
2 | """Tests for the IPython tab-completion machinery.""" | |
3 |
|
3 | |||
4 | # Copyright (c) IPython Development Team. |
|
4 | # Copyright (c) IPython Development Team. | |
5 | # Distributed under the terms of the Modified BSD License. |
|
5 | # Distributed under the terms of the Modified BSD License. | |
6 |
|
6 | |||
7 | import os |
|
7 | import os | |
8 | import sys |
|
8 | import sys | |
9 | import unittest |
|
9 | import unittest | |
10 |
|
10 | |||
11 | from contextlib import contextmanager |
|
11 | from contextlib import contextmanager | |
12 |
|
12 | |||
13 | import nose.tools as nt |
|
13 | import nose.tools as nt | |
14 |
|
14 | |||
15 | from traitlets.config.loader import Config |
|
15 | from traitlets.config.loader import Config | |
16 | from IPython import get_ipython |
|
16 | from IPython import get_ipython | |
17 | from IPython.core import completer |
|
17 | from IPython.core import completer | |
18 | from IPython.external.decorators import knownfailureif |
|
18 | from IPython.external.decorators import knownfailureif | |
19 | from IPython.utils.tempdir import TemporaryDirectory, TemporaryWorkingDirectory |
|
19 | from IPython.utils.tempdir import TemporaryDirectory, TemporaryWorkingDirectory | |
20 | from IPython.utils.generics import complete_object |
|
20 | from IPython.utils.generics import complete_object | |
21 | from IPython.utils.py3compat import string_types, unicode_type |
|
21 | from IPython.utils.py3compat import string_types, unicode_type | |
22 | from IPython.testing import decorators as dec |
|
22 | from IPython.testing import decorators as dec | |
23 |
|
23 | |||
24 | #----------------------------------------------------------------------------- |
|
24 | #----------------------------------------------------------------------------- | |
25 | # Test functions |
|
25 | # Test functions | |
26 | #----------------------------------------------------------------------------- |
|
26 | #----------------------------------------------------------------------------- | |
27 |
|
27 | |||
28 | @contextmanager |
|
28 | @contextmanager | |
29 | def greedy_completion(): |
|
29 | def greedy_completion(): | |
30 | ip = get_ipython() |
|
30 | ip = get_ipython() | |
31 | greedy_original = ip.Completer.greedy |
|
31 | greedy_original = ip.Completer.greedy | |
32 | try: |
|
32 | try: | |
33 | ip.Completer.greedy = True |
|
33 | ip.Completer.greedy = True | |
34 | yield |
|
34 | yield | |
35 | finally: |
|
35 | finally: | |
36 | ip.Completer.greedy = greedy_original |
|
36 | ip.Completer.greedy = greedy_original | |
37 |
|
37 | |||
38 | def test_protect_filename(): |
|
38 | def test_protect_filename(): | |
39 | pairs = [ ('abc','abc'), |
|
39 | if sys.platform == 'win32': | |
40 |
|
|
40 | pairs = [ ('abc','abc'), | |
41 |
(' |
|
41 | (' abc',"' abc'"), | |
42 |
('a |
|
42 | ('a bc',"'a bc'"), | |
43 |
(' bc', |
|
43 | ('a bc',"'a bc'"), | |
44 | ] |
|
44 | (' bc',"' bc'"), | |
45 | # On posix, we also protect parens and other special characters |
|
45 | ] | |
46 | if sys.platform != 'win32': |
|
46 | else: | |
47 |
pairs |
|
47 | pairs = [ ('abc','abc'), | |
48 |
|
|
48 | (' abc',r'\ abc'), | |
49 |
|
|
49 | ('a bc',r'a\ bc'), | |
50 |
|
|
50 | ('a bc',r'a\ \ bc'), | |
51 |
|
|
51 | (' bc',r'\ \ bc'), | |
52 | ('a#bc', r'a\#bc'), |
|
52 | # On posix, we also protect parens and other special characters | |
53 |
|
|
53 | ('a(bc',r'a\(bc'), | |
54 |
|
|
54 | ('a)bc',r'a\)bc'), | |
55 |
|
|
55 | ('a( )bc',r'a\(\ \)bc'), | |
56 |
|
|
56 | ('a[1]bc', r'a\[1\]bc'), | |
57 |
|
|
57 | ('a{1}bc', r'a\{1\}bc'), | |
58 |
|
|
58 | ('a#bc', r'a\#bc'), | |
59 |
|
|
59 | ('a?bc', r'a\?bc'), | |
60 |
|
|
60 | ('a=bc', r'a\=bc'), | |
61 |
|
|
61 | ('a\\bc', r'a\\bc'), | |
62 |
|
|
62 | ('a|bc', r'a\|bc'), | |
63 |
|
|
63 | ('a;bc', r'a\;bc'), | |
64 | ] ) |
|
64 | ('a:bc', r'a\:bc'), | |
|
65 | ("a'bc", r"a\'bc"), | |||
|
66 | ('a*bc', r'a\*bc'), | |||
|
67 | ('a"bc', r'a\"bc'), | |||
|
68 | ('a^bc', r'a\^bc'), | |||
|
69 | ('a&bc', r'a\&bc'), | |||
|
70 | ] | |||
65 | # run the actual tests |
|
71 | # run the actual tests | |
66 | for s1, s2 in pairs: |
|
72 | for s1, s2 in pairs: | |
67 | s1p = completer.protect_filename(s1) |
|
73 | s1p = completer.protect_filename(s1) | |
68 | nt.assert_equal(s1p, s2) |
|
74 | nt.assert_equal(s1p, s2) | |
69 |
|
75 | |||
70 |
|
76 | |||
71 | def check_line_split(splitter, test_specs): |
|
77 | def check_line_split(splitter, test_specs): | |
72 | for part1, part2, split in test_specs: |
|
78 | for part1, part2, split in test_specs: | |
73 | cursor_pos = len(part1) |
|
79 | cursor_pos = len(part1) | |
74 | line = part1+part2 |
|
80 | line = part1+part2 | |
75 | out = splitter.split_line(line, cursor_pos) |
|
81 | out = splitter.split_line(line, cursor_pos) | |
76 | nt.assert_equal(out, split) |
|
82 | nt.assert_equal(out, split) | |
77 |
|
83 | |||
78 |
|
84 | |||
79 | def test_line_split(): |
|
85 | def test_line_split(): | |
80 | """Basic line splitter test with default specs.""" |
|
86 | """Basic line splitter test with default specs.""" | |
81 | sp = completer.CompletionSplitter() |
|
87 | sp = completer.CompletionSplitter() | |
82 | # The format of the test specs is: part1, part2, expected answer. Parts 1 |
|
88 | # The format of the test specs is: part1, part2, expected answer. Parts 1 | |
83 | # and 2 are joined into the 'line' sent to the splitter, as if the cursor |
|
89 | # and 2 are joined into the 'line' sent to the splitter, as if the cursor | |
84 | # was at the end of part1. So an empty part2 represents someone hitting |
|
90 | # was at the end of part1. So an empty part2 represents someone hitting | |
85 | # tab at the end of the line, the most common case. |
|
91 | # tab at the end of the line, the most common case. | |
86 | t = [('run some/scrip', '', 'some/scrip'), |
|
92 | t = [('run some/scrip', '', 'some/scrip'), | |
87 | ('run scripts/er', 'ror.py foo', 'scripts/er'), |
|
93 | ('run scripts/er', 'ror.py foo', 'scripts/er'), | |
88 | ('echo $HOM', '', 'HOM'), |
|
94 | ('echo $HOM', '', 'HOM'), | |
89 | ('print sys.pa', '', 'sys.pa'), |
|
95 | ('print sys.pa', '', 'sys.pa'), | |
90 | ('print(sys.pa', '', 'sys.pa'), |
|
96 | ('print(sys.pa', '', 'sys.pa'), | |
91 | ("execfile('scripts/er", '', 'scripts/er'), |
|
97 | ("execfile('scripts/er", '', 'scripts/er'), | |
92 | ('a[x.', '', 'x.'), |
|
98 | ('a[x.', '', 'x.'), | |
93 | ('a[x.', 'y', 'x.'), |
|
99 | ('a[x.', 'y', 'x.'), | |
94 | ('cd "some_file/', '', 'some_file/'), |
|
100 | ('cd "some_file/', '', 'some_file/'), | |
95 | ] |
|
101 | ] | |
96 | check_line_split(sp, t) |
|
102 | check_line_split(sp, t) | |
97 | # Ensure splitting works OK with unicode by re-running the tests with |
|
103 | # Ensure splitting works OK with unicode by re-running the tests with | |
98 | # all inputs turned into unicode |
|
104 | # all inputs turned into unicode | |
99 | check_line_split(sp, [ map(unicode_type, p) for p in t] ) |
|
105 | check_line_split(sp, [ map(unicode_type, p) for p in t] ) | |
100 |
|
106 | |||
101 |
|
107 | |||
102 | def test_custom_completion_error(): |
|
108 | def test_custom_completion_error(): | |
103 | """Test that errors from custom attribute completers are silenced.""" |
|
109 | """Test that errors from custom attribute completers are silenced.""" | |
104 | ip = get_ipython() |
|
110 | ip = get_ipython() | |
105 | class A(object): pass |
|
111 | class A(object): pass | |
106 | ip.user_ns['a'] = A() |
|
112 | ip.user_ns['a'] = A() | |
107 |
|
113 | |||
108 | @complete_object.when_type(A) |
|
114 | @complete_object.when_type(A) | |
109 | def complete_A(a, existing_completions): |
|
115 | def complete_A(a, existing_completions): | |
110 | raise TypeError("this should be silenced") |
|
116 | raise TypeError("this should be silenced") | |
111 |
|
117 | |||
112 | ip.complete("a.") |
|
118 | ip.complete("a.") | |
113 |
|
119 | |||
114 |
|
120 | |||
115 | def test_unicode_completions(): |
|
121 | def test_unicode_completions(): | |
116 | ip = get_ipython() |
|
122 | ip = get_ipython() | |
117 | # Some strings that trigger different types of completion. Check them both |
|
123 | # Some strings that trigger different types of completion. Check them both | |
118 | # in str and unicode forms |
|
124 | # in str and unicode forms | |
119 | s = ['ru', '%ru', 'cd /', 'floa', 'float(x)/'] |
|
125 | s = ['ru', '%ru', 'cd /', 'floa', 'float(x)/'] | |
120 | for t in s + list(map(unicode_type, s)): |
|
126 | for t in s + list(map(unicode_type, s)): | |
121 | # We don't need to check exact completion values (they may change |
|
127 | # We don't need to check exact completion values (they may change | |
122 | # depending on the state of the namespace, but at least no exceptions |
|
128 | # depending on the state of the namespace, but at least no exceptions | |
123 | # should be thrown and the return value should be a pair of text, list |
|
129 | # should be thrown and the return value should be a pair of text, list | |
124 | # values. |
|
130 | # values. | |
125 | text, matches = ip.complete(t) |
|
131 | text, matches = ip.complete(t) | |
126 | nt.assert_true(isinstance(text, string_types)) |
|
132 | nt.assert_true(isinstance(text, string_types)) | |
127 | nt.assert_true(isinstance(matches, list)) |
|
133 | nt.assert_true(isinstance(matches, list)) | |
128 |
|
134 | |||
129 | @dec.onlyif(sys.version_info[0] >= 3, 'This test only applies in Py>=3') |
|
135 | @dec.onlyif(sys.version_info[0] >= 3, 'This test only applies in Py>=3') | |
130 | def test_latex_completions(): |
|
136 | def test_latex_completions(): | |
131 | from IPython.core.latex_symbols import latex_symbols |
|
137 | from IPython.core.latex_symbols import latex_symbols | |
132 | import random |
|
138 | import random | |
133 | ip = get_ipython() |
|
139 | ip = get_ipython() | |
134 | # Test some random unicode symbols |
|
140 | # Test some random unicode symbols | |
135 | keys = random.sample(latex_symbols.keys(), 10) |
|
141 | keys = random.sample(latex_symbols.keys(), 10) | |
136 | for k in keys: |
|
142 | for k in keys: | |
137 | text, matches = ip.complete(k) |
|
143 | text, matches = ip.complete(k) | |
138 | nt.assert_equal(len(matches),1) |
|
144 | nt.assert_equal(len(matches),1) | |
139 | nt.assert_equal(text, k) |
|
145 | nt.assert_equal(text, k) | |
140 | nt.assert_equal(matches[0], latex_symbols[k]) |
|
146 | nt.assert_equal(matches[0], latex_symbols[k]) | |
141 | # Test a more complex line |
|
147 | # Test a more complex line | |
142 | text, matches = ip.complete(u'print(\\alpha') |
|
148 | text, matches = ip.complete(u'print(\\alpha') | |
143 | nt.assert_equals(text, u'\\alpha') |
|
149 | nt.assert_equals(text, u'\\alpha') | |
144 | nt.assert_equals(matches[0], latex_symbols['\\alpha']) |
|
150 | nt.assert_equals(matches[0], latex_symbols['\\alpha']) | |
145 | # Test multiple matching latex symbols |
|
151 | # Test multiple matching latex symbols | |
146 | text, matches = ip.complete(u'\\al') |
|
152 | text, matches = ip.complete(u'\\al') | |
147 | nt.assert_in('\\alpha', matches) |
|
153 | nt.assert_in('\\alpha', matches) | |
148 | nt.assert_in('\\aleph', matches) |
|
154 | nt.assert_in('\\aleph', matches) | |
149 |
|
155 | |||
150 |
|
156 | |||
151 |
|
157 | |||
152 |
|
158 | |||
153 | @dec.onlyif(sys.version_info[0] >= 3, 'This test only apply on python3') |
|
159 | @dec.onlyif(sys.version_info[0] >= 3, 'This test only apply on python3') | |
154 | def test_back_latex_completion(): |
|
160 | def test_back_latex_completion(): | |
155 | ip = get_ipython() |
|
161 | ip = get_ipython() | |
156 |
|
162 | |||
157 | # do not return more than 1 matches fro \beta, only the latex one. |
|
163 | # do not return more than 1 matches fro \beta, only the latex one. | |
158 | name, matches = ip.complete('\\Ξ²') |
|
164 | name, matches = ip.complete('\\Ξ²') | |
159 | nt.assert_equal(len(matches), 1) |
|
165 | nt.assert_equal(len(matches), 1) | |
160 | nt.assert_equal(matches[0], '\\beta') |
|
166 | nt.assert_equal(matches[0], '\\beta') | |
161 |
|
167 | |||
162 | @dec.onlyif(sys.version_info[0] >= 3, 'This test only apply on python3') |
|
168 | @dec.onlyif(sys.version_info[0] >= 3, 'This test only apply on python3') | |
163 | def test_back_unicode_completion(): |
|
169 | def test_back_unicode_completion(): | |
164 | ip = get_ipython() |
|
170 | ip = get_ipython() | |
165 |
|
171 | |||
166 | name, matches = ip.complete('\\β €') |
|
172 | name, matches = ip.complete('\\β €') | |
167 | nt.assert_equal(len(matches), 1) |
|
173 | nt.assert_equal(len(matches), 1) | |
168 | nt.assert_equal(matches[0], '\\ROMAN NUMERAL FIVE') |
|
174 | nt.assert_equal(matches[0], '\\ROMAN NUMERAL FIVE') | |
169 |
|
175 | |||
170 |
|
176 | |||
171 | @dec.onlyif(sys.version_info[0] >= 3, 'This test only apply on python3') |
|
177 | @dec.onlyif(sys.version_info[0] >= 3, 'This test only apply on python3') | |
172 | def test_forward_unicode_completion(): |
|
178 | def test_forward_unicode_completion(): | |
173 | ip = get_ipython() |
|
179 | ip = get_ipython() | |
174 |
|
180 | |||
175 | name, matches = ip.complete('\\ROMAN NUMERAL FIVE') |
|
181 | name, matches = ip.complete('\\ROMAN NUMERAL FIVE') | |
176 | nt.assert_equal(len(matches), 1) |
|
182 | nt.assert_equal(len(matches), 1) | |
177 | nt.assert_equal(matches[0], 'β €') |
|
183 | nt.assert_equal(matches[0], 'β €') | |
178 |
|
184 | |||
179 | @dec.onlyif(sys.version_info[0] >= 3, 'This test only apply on python3') |
|
185 | @dec.onlyif(sys.version_info[0] >= 3, 'This test only apply on python3') | |
180 | def test_no_ascii_back_completion(): |
|
186 | def test_no_ascii_back_completion(): | |
181 | ip = get_ipython() |
|
187 | ip = get_ipython() | |
182 | with TemporaryWorkingDirectory(): # Avoid any filename completions |
|
188 | with TemporaryWorkingDirectory(): # Avoid any filename completions | |
183 | # single ascii letter that don't have yet completions |
|
189 | # single ascii letter that don't have yet completions | |
184 | for letter in 'jJ' : |
|
190 | for letter in 'jJ' : | |
185 | name, matches = ip.complete('\\'+letter) |
|
191 | name, matches = ip.complete('\\'+letter) | |
186 | nt.assert_equal(matches, []) |
|
192 | nt.assert_equal(matches, []) | |
187 |
|
193 | |||
188 |
|
194 | |||
189 |
|
195 | |||
190 |
|
196 | |||
191 | class CompletionSplitterTestCase(unittest.TestCase): |
|
197 | class CompletionSplitterTestCase(unittest.TestCase): | |
192 | def setUp(self): |
|
198 | def setUp(self): | |
193 | self.sp = completer.CompletionSplitter() |
|
199 | self.sp = completer.CompletionSplitter() | |
194 |
|
200 | |||
195 | def test_delim_setting(self): |
|
201 | def test_delim_setting(self): | |
196 | self.sp.delims = ' ' |
|
202 | self.sp.delims = ' ' | |
197 | nt.assert_equal(self.sp.delims, ' ') |
|
203 | nt.assert_equal(self.sp.delims, ' ') | |
198 | nt.assert_equal(self.sp._delim_expr, '[\ ]') |
|
204 | nt.assert_equal(self.sp._delim_expr, '[\ ]') | |
199 |
|
205 | |||
200 | def test_spaces(self): |
|
206 | def test_spaces(self): | |
201 | """Test with only spaces as split chars.""" |
|
207 | """Test with only spaces as split chars.""" | |
202 | self.sp.delims = ' ' |
|
208 | self.sp.delims = ' ' | |
203 | t = [('foo', '', 'foo'), |
|
209 | t = [('foo', '', 'foo'), | |
204 | ('run foo', '', 'foo'), |
|
210 | ('run foo', '', 'foo'), | |
205 | ('run foo', 'bar', 'foo'), |
|
211 | ('run foo', 'bar', 'foo'), | |
206 | ] |
|
212 | ] | |
207 | check_line_split(self.sp, t) |
|
213 | check_line_split(self.sp, t) | |
208 |
|
214 | |||
209 |
|
215 | |||
210 | def test_has_open_quotes1(): |
|
216 | def test_has_open_quotes1(): | |
211 | for s in ["'", "'''", "'hi' '"]: |
|
217 | for s in ["'", "'''", "'hi' '"]: | |
212 | nt.assert_equal(completer.has_open_quotes(s), "'") |
|
218 | nt.assert_equal(completer.has_open_quotes(s), "'") | |
213 |
|
219 | |||
214 |
|
220 | |||
215 | def test_has_open_quotes2(): |
|
221 | def test_has_open_quotes2(): | |
216 | for s in ['"', '"""', '"hi" "']: |
|
222 | for s in ['"', '"""', '"hi" "']: | |
217 | nt.assert_equal(completer.has_open_quotes(s), '"') |
|
223 | nt.assert_equal(completer.has_open_quotes(s), '"') | |
218 |
|
224 | |||
219 |
|
225 | |||
220 | def test_has_open_quotes3(): |
|
226 | def test_has_open_quotes3(): | |
221 | for s in ["''", "''' '''", "'hi' 'ipython'"]: |
|
227 | for s in ["''", "''' '''", "'hi' 'ipython'"]: | |
222 | nt.assert_false(completer.has_open_quotes(s)) |
|
228 | nt.assert_false(completer.has_open_quotes(s)) | |
223 |
|
229 | |||
224 |
|
230 | |||
225 | def test_has_open_quotes4(): |
|
231 | def test_has_open_quotes4(): | |
226 | for s in ['""', '""" """', '"hi" "ipython"']: |
|
232 | for s in ['""', '""" """', '"hi" "ipython"']: | |
227 | nt.assert_false(completer.has_open_quotes(s)) |
|
233 | nt.assert_false(completer.has_open_quotes(s)) | |
228 |
|
234 | |||
229 |
|
235 | |||
230 | @knownfailureif(sys.platform == 'win32', "abspath completions fail on Windows") |
|
236 | @knownfailureif(sys.platform == 'win32', "abspath completions fail on Windows") | |
231 | def test_abspath_file_completions(): |
|
237 | def test_abspath_file_completions(): | |
232 | ip = get_ipython() |
|
238 | ip = get_ipython() | |
233 | with TemporaryDirectory() as tmpdir: |
|
239 | with TemporaryDirectory() as tmpdir: | |
234 | prefix = os.path.join(tmpdir, 'foo') |
|
240 | prefix = os.path.join(tmpdir, 'foo') | |
235 | suffixes = ['1', '2'] |
|
241 | suffixes = ['1', '2'] | |
236 | names = [prefix+s for s in suffixes] |
|
242 | names = [prefix+s for s in suffixes] | |
237 | for n in names: |
|
243 | for n in names: | |
238 | open(n, 'w').close() |
|
244 | open(n, 'w').close() | |
239 |
|
245 | |||
240 | # Check simple completion |
|
246 | # Check simple completion | |
241 | c = ip.complete(prefix)[1] |
|
247 | c = ip.complete(prefix)[1] | |
242 | nt.assert_equal(c, names) |
|
248 | nt.assert_equal(c, names) | |
243 |
|
249 | |||
244 | # Now check with a function call |
|
250 | # Now check with a function call | |
245 | cmd = 'a = f("%s' % prefix |
|
251 | cmd = 'a = f("%s' % prefix | |
246 | c = ip.complete(prefix, cmd)[1] |
|
252 | c = ip.complete(prefix, cmd)[1] | |
247 | comp = [prefix+s for s in suffixes] |
|
253 | comp = [prefix+s for s in suffixes] | |
248 | nt.assert_equal(c, comp) |
|
254 | nt.assert_equal(c, comp) | |
249 |
|
255 | |||
250 |
|
256 | |||
251 | def test_local_file_completions(): |
|
257 | def test_local_file_completions(): | |
252 | ip = get_ipython() |
|
258 | ip = get_ipython() | |
253 | with TemporaryWorkingDirectory(): |
|
259 | with TemporaryWorkingDirectory(): | |
254 | prefix = './foo' |
|
260 | prefix = './foo' | |
255 | suffixes = ['1', '2'] |
|
261 | suffixes = ['1', '2'] | |
256 | names = [prefix+s for s in suffixes] |
|
262 | names = [prefix+s for s in suffixes] | |
257 | for n in names: |
|
263 | for n in names: | |
258 | open(n, 'w').close() |
|
264 | open(n, 'w').close() | |
259 |
|
265 | |||
260 | # Check simple completion |
|
266 | # Check simple completion | |
261 | c = ip.complete(prefix)[1] |
|
267 | c = ip.complete(prefix)[1] | |
262 | nt.assert_equal(c, names) |
|
268 | nt.assert_equal(c, names) | |
263 |
|
269 | |||
264 | # Now check with a function call |
|
270 | # Now check with a function call | |
265 | cmd = 'a = f("%s' % prefix |
|
271 | cmd = 'a = f("%s' % prefix | |
266 | c = ip.complete(prefix, cmd)[1] |
|
272 | c = ip.complete(prefix, cmd)[1] | |
267 | comp = set(prefix+s for s in suffixes) |
|
273 | comp = set(prefix+s for s in suffixes) | |
268 | nt.assert_true(comp.issubset(set(c))) |
|
274 | nt.assert_true(comp.issubset(set(c))) | |
269 |
|
275 | |||
270 |
|
276 | |||
271 | def test_greedy_completions(): |
|
277 | def test_greedy_completions(): | |
272 | ip = get_ipython() |
|
278 | ip = get_ipython() | |
273 | ip.ex('a=list(range(5))') |
|
279 | ip.ex('a=list(range(5))') | |
274 | _,c = ip.complete('.',line='a[0].') |
|
280 | _,c = ip.complete('.',line='a[0].') | |
275 | nt.assert_false('.real' in c, |
|
281 | nt.assert_false('.real' in c, | |
276 | "Shouldn't have completed on a[0]: %s"%c) |
|
282 | "Shouldn't have completed on a[0]: %s"%c) | |
277 | with greedy_completion(): |
|
283 | with greedy_completion(): | |
278 | def _(line, cursor_pos, expect, message): |
|
284 | def _(line, cursor_pos, expect, message): | |
279 | _,c = ip.complete('.', line=line, cursor_pos=cursor_pos) |
|
285 | _,c = ip.complete('.', line=line, cursor_pos=cursor_pos) | |
280 | nt.assert_in(expect, c, message%c) |
|
286 | nt.assert_in(expect, c, message%c) | |
281 |
|
287 | |||
282 | yield _, 'a[0].', 5, 'a[0].real', "Should have completed on a[0].: %s" |
|
288 | yield _, 'a[0].', 5, 'a[0].real', "Should have completed on a[0].: %s" | |
283 | yield _, 'a[0].r', 6, 'a[0].real', "Should have completed on a[0].r: %s" |
|
289 | yield _, 'a[0].r', 6, 'a[0].real', "Should have completed on a[0].r: %s" | |
284 |
|
290 | |||
285 | if sys.version_info > (3,4): |
|
291 | if sys.version_info > (3,4): | |
286 | yield _, 'a[0].from_', 10, 'a[0].from_bytes', "Should have completed on a[0].from_: %s" |
|
292 | yield _, 'a[0].from_', 10, 'a[0].from_bytes', "Should have completed on a[0].from_: %s" | |
287 |
|
293 | |||
288 |
|
294 | |||
289 |
|
295 | |||
290 | def test_omit__names(): |
|
296 | def test_omit__names(): | |
291 | # also happens to test IPCompleter as a configurable |
|
297 | # also happens to test IPCompleter as a configurable | |
292 | ip = get_ipython() |
|
298 | ip = get_ipython() | |
293 | ip._hidden_attr = 1 |
|
299 | ip._hidden_attr = 1 | |
294 | ip._x = {} |
|
300 | ip._x = {} | |
295 | c = ip.Completer |
|
301 | c = ip.Completer | |
296 | ip.ex('ip=get_ipython()') |
|
302 | ip.ex('ip=get_ipython()') | |
297 | cfg = Config() |
|
303 | cfg = Config() | |
298 | cfg.IPCompleter.omit__names = 0 |
|
304 | cfg.IPCompleter.omit__names = 0 | |
299 | c.update_config(cfg) |
|
305 | c.update_config(cfg) | |
300 | s,matches = c.complete('ip.') |
|
306 | s,matches = c.complete('ip.') | |
301 | nt.assert_in('ip.__str__', matches) |
|
307 | nt.assert_in('ip.__str__', matches) | |
302 | nt.assert_in('ip._hidden_attr', matches) |
|
308 | nt.assert_in('ip._hidden_attr', matches) | |
303 | cfg = Config() |
|
309 | cfg = Config() | |
304 | cfg.IPCompleter.omit__names = 1 |
|
310 | cfg.IPCompleter.omit__names = 1 | |
305 | c.update_config(cfg) |
|
311 | c.update_config(cfg) | |
306 | s,matches = c.complete('ip.') |
|
312 | s,matches = c.complete('ip.') | |
307 | nt.assert_not_in('ip.__str__', matches) |
|
313 | nt.assert_not_in('ip.__str__', matches) | |
308 | nt.assert_in('ip._hidden_attr', matches) |
|
314 | nt.assert_in('ip._hidden_attr', matches) | |
309 | cfg = Config() |
|
315 | cfg = Config() | |
310 | cfg.IPCompleter.omit__names = 2 |
|
316 | cfg.IPCompleter.omit__names = 2 | |
311 | c.update_config(cfg) |
|
317 | c.update_config(cfg) | |
312 | s,matches = c.complete('ip.') |
|
318 | s,matches = c.complete('ip.') | |
313 | nt.assert_not_in('ip.__str__', matches) |
|
319 | nt.assert_not_in('ip.__str__', matches) | |
314 | nt.assert_not_in('ip._hidden_attr', matches) |
|
320 | nt.assert_not_in('ip._hidden_attr', matches) | |
315 | s,matches = c.complete('ip._x.') |
|
321 | s,matches = c.complete('ip._x.') | |
316 | nt.assert_in('ip._x.keys', matches) |
|
322 | nt.assert_in('ip._x.keys', matches) | |
317 | del ip._hidden_attr |
|
323 | del ip._hidden_attr | |
318 |
|
324 | |||
319 |
|
325 | |||
320 | def test_limit_to__all__False_ok(): |
|
326 | def test_limit_to__all__False_ok(): | |
321 | ip = get_ipython() |
|
327 | ip = get_ipython() | |
322 | c = ip.Completer |
|
328 | c = ip.Completer | |
323 | ip.ex('class D: x=24') |
|
329 | ip.ex('class D: x=24') | |
324 | ip.ex('d=D()') |
|
330 | ip.ex('d=D()') | |
325 | cfg = Config() |
|
331 | cfg = Config() | |
326 | cfg.IPCompleter.limit_to__all__ = False |
|
332 | cfg.IPCompleter.limit_to__all__ = False | |
327 | c.update_config(cfg) |
|
333 | c.update_config(cfg) | |
328 | s, matches = c.complete('d.') |
|
334 | s, matches = c.complete('d.') | |
329 | nt.assert_in('d.x', matches) |
|
335 | nt.assert_in('d.x', matches) | |
330 |
|
336 | |||
331 |
|
337 | |||
332 | def test_get__all__entries_ok(): |
|
338 | def test_get__all__entries_ok(): | |
333 | class A(object): |
|
339 | class A(object): | |
334 | __all__ = ['x', 1] |
|
340 | __all__ = ['x', 1] | |
335 | words = completer.get__all__entries(A()) |
|
341 | words = completer.get__all__entries(A()) | |
336 | nt.assert_equal(words, ['x']) |
|
342 | nt.assert_equal(words, ['x']) | |
337 |
|
343 | |||
338 |
|
344 | |||
339 | def test_get__all__entries_no__all__ok(): |
|
345 | def test_get__all__entries_no__all__ok(): | |
340 | class A(object): |
|
346 | class A(object): | |
341 | pass |
|
347 | pass | |
342 | words = completer.get__all__entries(A()) |
|
348 | words = completer.get__all__entries(A()) | |
343 | nt.assert_equal(words, []) |
|
349 | nt.assert_equal(words, []) | |
344 |
|
350 | |||
345 |
|
351 | |||
346 | def test_func_kw_completions(): |
|
352 | def test_func_kw_completions(): | |
347 | ip = get_ipython() |
|
353 | ip = get_ipython() | |
348 | c = ip.Completer |
|
354 | c = ip.Completer | |
349 | ip.ex('def myfunc(a=1,b=2): return a+b') |
|
355 | ip.ex('def myfunc(a=1,b=2): return a+b') | |
350 | s, matches = c.complete(None, 'myfunc(1,b') |
|
356 | s, matches = c.complete(None, 'myfunc(1,b') | |
351 | nt.assert_in('b=', matches) |
|
357 | nt.assert_in('b=', matches) | |
352 | # Simulate completing with cursor right after b (pos==10): |
|
358 | # Simulate completing with cursor right after b (pos==10): | |
353 | s, matches = c.complete(None, 'myfunc(1,b)', 10) |
|
359 | s, matches = c.complete(None, 'myfunc(1,b)', 10) | |
354 | nt.assert_in('b=', matches) |
|
360 | nt.assert_in('b=', matches) | |
355 | s, matches = c.complete(None, 'myfunc(a="escaped\\")string",b') |
|
361 | s, matches = c.complete(None, 'myfunc(a="escaped\\")string",b') | |
356 | nt.assert_in('b=', matches) |
|
362 | nt.assert_in('b=', matches) | |
357 | #builtin function |
|
363 | #builtin function | |
358 | s, matches = c.complete(None, 'min(k, k') |
|
364 | s, matches = c.complete(None, 'min(k, k') | |
359 | nt.assert_in('key=', matches) |
|
365 | nt.assert_in('key=', matches) | |
360 |
|
366 | |||
361 |
|
367 | |||
362 | def test_default_arguments_from_docstring(): |
|
368 | def test_default_arguments_from_docstring(): | |
363 | ip = get_ipython() |
|
369 | ip = get_ipython() | |
364 | c = ip.Completer |
|
370 | c = ip.Completer | |
365 | kwd = c._default_arguments_from_docstring( |
|
371 | kwd = c._default_arguments_from_docstring( | |
366 | 'min(iterable[, key=func]) -> value') |
|
372 | 'min(iterable[, key=func]) -> value') | |
367 | nt.assert_equal(kwd, ['key']) |
|
373 | nt.assert_equal(kwd, ['key']) | |
368 | #with cython type etc |
|
374 | #with cython type etc | |
369 | kwd = c._default_arguments_from_docstring( |
|
375 | kwd = c._default_arguments_from_docstring( | |
370 | 'Minuit.migrad(self, int ncall=10000, resume=True, int nsplit=1)\n') |
|
376 | 'Minuit.migrad(self, int ncall=10000, resume=True, int nsplit=1)\n') | |
371 | nt.assert_equal(kwd, ['ncall', 'resume', 'nsplit']) |
|
377 | nt.assert_equal(kwd, ['ncall', 'resume', 'nsplit']) | |
372 | #white spaces |
|
378 | #white spaces | |
373 | kwd = c._default_arguments_from_docstring( |
|
379 | kwd = c._default_arguments_from_docstring( | |
374 | '\n Minuit.migrad(self, int ncall=10000, resume=True, int nsplit=1)\n') |
|
380 | '\n Minuit.migrad(self, int ncall=10000, resume=True, int nsplit=1)\n') | |
375 | nt.assert_equal(kwd, ['ncall', 'resume', 'nsplit']) |
|
381 | nt.assert_equal(kwd, ['ncall', 'resume', 'nsplit']) | |
376 |
|
382 | |||
377 | def test_line_magics(): |
|
383 | def test_line_magics(): | |
378 | ip = get_ipython() |
|
384 | ip = get_ipython() | |
379 | c = ip.Completer |
|
385 | c = ip.Completer | |
380 | s, matches = c.complete(None, 'lsmag') |
|
386 | s, matches = c.complete(None, 'lsmag') | |
381 | nt.assert_in('%lsmagic', matches) |
|
387 | nt.assert_in('%lsmagic', matches) | |
382 | s, matches = c.complete(None, '%lsmag') |
|
388 | s, matches = c.complete(None, '%lsmag') | |
383 | nt.assert_in('%lsmagic', matches) |
|
389 | nt.assert_in('%lsmagic', matches) | |
384 |
|
390 | |||
385 |
|
391 | |||
386 | def test_cell_magics(): |
|
392 | def test_cell_magics(): | |
387 | from IPython.core.magic import register_cell_magic |
|
393 | from IPython.core.magic import register_cell_magic | |
388 |
|
394 | |||
389 | @register_cell_magic |
|
395 | @register_cell_magic | |
390 | def _foo_cellm(line, cell): |
|
396 | def _foo_cellm(line, cell): | |
391 | pass |
|
397 | pass | |
392 |
|
398 | |||
393 | ip = get_ipython() |
|
399 | ip = get_ipython() | |
394 | c = ip.Completer |
|
400 | c = ip.Completer | |
395 |
|
401 | |||
396 | s, matches = c.complete(None, '_foo_ce') |
|
402 | s, matches = c.complete(None, '_foo_ce') | |
397 | nt.assert_in('%%_foo_cellm', matches) |
|
403 | nt.assert_in('%%_foo_cellm', matches) | |
398 | s, matches = c.complete(None, '%%_foo_ce') |
|
404 | s, matches = c.complete(None, '%%_foo_ce') | |
399 | nt.assert_in('%%_foo_cellm', matches) |
|
405 | nt.assert_in('%%_foo_cellm', matches) | |
400 |
|
406 | |||
401 |
|
407 | |||
402 | def test_line_cell_magics(): |
|
408 | def test_line_cell_magics(): | |
403 | from IPython.core.magic import register_line_cell_magic |
|
409 | from IPython.core.magic import register_line_cell_magic | |
404 |
|
410 | |||
405 | @register_line_cell_magic |
|
411 | @register_line_cell_magic | |
406 | def _bar_cellm(line, cell): |
|
412 | def _bar_cellm(line, cell): | |
407 | pass |
|
413 | pass | |
408 |
|
414 | |||
409 | ip = get_ipython() |
|
415 | ip = get_ipython() | |
410 | c = ip.Completer |
|
416 | c = ip.Completer | |
411 |
|
417 | |||
412 | # The policy here is trickier, see comments in completion code. The |
|
418 | # The policy here is trickier, see comments in completion code. The | |
413 | # returned values depend on whether the user passes %% or not explicitly, |
|
419 | # returned values depend on whether the user passes %% or not explicitly, | |
414 | # and this will show a difference if the same name is both a line and cell |
|
420 | # and this will show a difference if the same name is both a line and cell | |
415 | # magic. |
|
421 | # magic. | |
416 | s, matches = c.complete(None, '_bar_ce') |
|
422 | s, matches = c.complete(None, '_bar_ce') | |
417 | nt.assert_in('%_bar_cellm', matches) |
|
423 | nt.assert_in('%_bar_cellm', matches) | |
418 | nt.assert_in('%%_bar_cellm', matches) |
|
424 | nt.assert_in('%%_bar_cellm', matches) | |
419 | s, matches = c.complete(None, '%_bar_ce') |
|
425 | s, matches = c.complete(None, '%_bar_ce') | |
420 | nt.assert_in('%_bar_cellm', matches) |
|
426 | nt.assert_in('%_bar_cellm', matches) | |
421 | nt.assert_in('%%_bar_cellm', matches) |
|
427 | nt.assert_in('%%_bar_cellm', matches) | |
422 | s, matches = c.complete(None, '%%_bar_ce') |
|
428 | s, matches = c.complete(None, '%%_bar_ce') | |
423 | nt.assert_not_in('%_bar_cellm', matches) |
|
429 | nt.assert_not_in('%_bar_cellm', matches) | |
424 | nt.assert_in('%%_bar_cellm', matches) |
|
430 | nt.assert_in('%%_bar_cellm', matches) | |
425 |
|
431 | |||
426 |
|
432 | |||
427 | def test_magic_completion_order(): |
|
433 | def test_magic_completion_order(): | |
428 |
|
434 | |||
429 | ip = get_ipython() |
|
435 | ip = get_ipython() | |
430 | c = ip.Completer |
|
436 | c = ip.Completer | |
431 |
|
437 | |||
432 | # Test ordering of magics and non-magics with the same name |
|
438 | # Test ordering of magics and non-magics with the same name | |
433 | # We want the non-magic first |
|
439 | # We want the non-magic first | |
434 |
|
440 | |||
435 | # Before importing matplotlib, there should only be one option: |
|
441 | # Before importing matplotlib, there should only be one option: | |
436 |
|
442 | |||
437 | text, matches = c.complete('mat') |
|
443 | text, matches = c.complete('mat') | |
438 | nt.assert_equal(matches, ["%matplotlib"]) |
|
444 | nt.assert_equal(matches, ["%matplotlib"]) | |
439 |
|
445 | |||
440 |
|
446 | |||
441 | ip.run_cell("matplotlib = 1") # introduce name into namespace |
|
447 | ip.run_cell("matplotlib = 1") # introduce name into namespace | |
442 |
|
448 | |||
443 | # After the import, there should be two options, ordered like this: |
|
449 | # After the import, there should be two options, ordered like this: | |
444 | text, matches = c.complete('mat') |
|
450 | text, matches = c.complete('mat') | |
445 | nt.assert_equal(matches, ["matplotlib", "%matplotlib"]) |
|
451 | nt.assert_equal(matches, ["matplotlib", "%matplotlib"]) | |
446 |
|
452 | |||
447 |
|
453 | |||
448 | ip.run_cell("timeit = 1") # define a user variable called 'timeit' |
|
454 | ip.run_cell("timeit = 1") # define a user variable called 'timeit' | |
449 |
|
455 | |||
450 | # Order of user variable and line and cell magics with same name: |
|
456 | # Order of user variable and line and cell magics with same name: | |
451 | text, matches = c.complete('timeit') |
|
457 | text, matches = c.complete('timeit') | |
452 | nt.assert_equal(matches, ["timeit", "%timeit","%%timeit"]) |
|
458 | nt.assert_equal(matches, ["timeit", "%timeit","%%timeit"]) | |
453 |
|
459 | |||
454 |
|
460 | |||
455 | def test_dict_key_completion_string(): |
|
461 | def test_dict_key_completion_string(): | |
456 | """Test dictionary key completion for string keys""" |
|
462 | """Test dictionary key completion for string keys""" | |
457 | ip = get_ipython() |
|
463 | ip = get_ipython() | |
458 | complete = ip.Completer.complete |
|
464 | complete = ip.Completer.complete | |
459 |
|
465 | |||
460 | ip.user_ns['d'] = {'abc': None} |
|
466 | ip.user_ns['d'] = {'abc': None} | |
461 |
|
467 | |||
462 | # check completion at different stages |
|
468 | # check completion at different stages | |
463 | _, matches = complete(line_buffer="d[") |
|
469 | _, matches = complete(line_buffer="d[") | |
464 | nt.assert_in("'abc'", matches) |
|
470 | nt.assert_in("'abc'", matches) | |
465 | nt.assert_not_in("'abc']", matches) |
|
471 | nt.assert_not_in("'abc']", matches) | |
466 |
|
472 | |||
467 | _, matches = complete(line_buffer="d['") |
|
473 | _, matches = complete(line_buffer="d['") | |
468 | nt.assert_in("abc", matches) |
|
474 | nt.assert_in("abc", matches) | |
469 | nt.assert_not_in("abc']", matches) |
|
475 | nt.assert_not_in("abc']", matches) | |
470 |
|
476 | |||
471 | _, matches = complete(line_buffer="d['a") |
|
477 | _, matches = complete(line_buffer="d['a") | |
472 | nt.assert_in("abc", matches) |
|
478 | nt.assert_in("abc", matches) | |
473 | nt.assert_not_in("abc']", matches) |
|
479 | nt.assert_not_in("abc']", matches) | |
474 |
|
480 | |||
475 | # check use of different quoting |
|
481 | # check use of different quoting | |
476 | _, matches = complete(line_buffer="d[\"") |
|
482 | _, matches = complete(line_buffer="d[\"") | |
477 | nt.assert_in("abc", matches) |
|
483 | nt.assert_in("abc", matches) | |
478 | nt.assert_not_in('abc\"]', matches) |
|
484 | nt.assert_not_in('abc\"]', matches) | |
479 |
|
485 | |||
480 | _, matches = complete(line_buffer="d[\"a") |
|
486 | _, matches = complete(line_buffer="d[\"a") | |
481 | nt.assert_in("abc", matches) |
|
487 | nt.assert_in("abc", matches) | |
482 | nt.assert_not_in('abc\"]', matches) |
|
488 | nt.assert_not_in('abc\"]', matches) | |
483 |
|
489 | |||
484 | # check sensitivity to following context |
|
490 | # check sensitivity to following context | |
485 | _, matches = complete(line_buffer="d[]", cursor_pos=2) |
|
491 | _, matches = complete(line_buffer="d[]", cursor_pos=2) | |
486 | nt.assert_in("'abc'", matches) |
|
492 | nt.assert_in("'abc'", matches) | |
487 |
|
493 | |||
488 | _, matches = complete(line_buffer="d['']", cursor_pos=3) |
|
494 | _, matches = complete(line_buffer="d['']", cursor_pos=3) | |
489 | nt.assert_in("abc", matches) |
|
495 | nt.assert_in("abc", matches) | |
490 | nt.assert_not_in("abc'", matches) |
|
496 | nt.assert_not_in("abc'", matches) | |
491 | nt.assert_not_in("abc']", matches) |
|
497 | nt.assert_not_in("abc']", matches) | |
492 |
|
498 | |||
493 | # check multiple solutions are correctly returned and that noise is not |
|
499 | # check multiple solutions are correctly returned and that noise is not | |
494 | ip.user_ns['d'] = {'abc': None, 'abd': None, 'bad': None, object(): None, |
|
500 | ip.user_ns['d'] = {'abc': None, 'abd': None, 'bad': None, object(): None, | |
495 | 5: None} |
|
501 | 5: None} | |
496 |
|
502 | |||
497 | _, matches = complete(line_buffer="d['a") |
|
503 | _, matches = complete(line_buffer="d['a") | |
498 | nt.assert_in("abc", matches) |
|
504 | nt.assert_in("abc", matches) | |
499 | nt.assert_in("abd", matches) |
|
505 | nt.assert_in("abd", matches) | |
500 | nt.assert_not_in("bad", matches) |
|
506 | nt.assert_not_in("bad", matches) | |
501 | assert not any(m.endswith((']', '"', "'")) for m in matches), matches |
|
507 | assert not any(m.endswith((']', '"', "'")) for m in matches), matches | |
502 |
|
508 | |||
503 | # check escaping and whitespace |
|
509 | # check escaping and whitespace | |
504 | ip.user_ns['d'] = {'a\nb': None, 'a\'b': None, 'a"b': None, 'a word': None} |
|
510 | ip.user_ns['d'] = {'a\nb': None, 'a\'b': None, 'a"b': None, 'a word': None} | |
505 | _, matches = complete(line_buffer="d['a") |
|
511 | _, matches = complete(line_buffer="d['a") | |
506 | nt.assert_in("a\\nb", matches) |
|
512 | nt.assert_in("a\\nb", matches) | |
507 | nt.assert_in("a\\'b", matches) |
|
513 | nt.assert_in("a\\'b", matches) | |
508 | nt.assert_in("a\"b", matches) |
|
514 | nt.assert_in("a\"b", matches) | |
509 | nt.assert_in("a word", matches) |
|
515 | nt.assert_in("a word", matches) | |
510 | assert not any(m.endswith((']', '"', "'")) for m in matches), matches |
|
516 | assert not any(m.endswith((']', '"', "'")) for m in matches), matches | |
511 |
|
517 | |||
512 | # - can complete on non-initial word of the string |
|
518 | # - can complete on non-initial word of the string | |
513 | _, matches = complete(line_buffer="d['a w") |
|
519 | _, matches = complete(line_buffer="d['a w") | |
514 | nt.assert_in("word", matches) |
|
520 | nt.assert_in("word", matches) | |
515 |
|
521 | |||
516 | # - understands quote escaping |
|
522 | # - understands quote escaping | |
517 | _, matches = complete(line_buffer="d['a\\'") |
|
523 | _, matches = complete(line_buffer="d['a\\'") | |
518 | nt.assert_in("b", matches) |
|
524 | nt.assert_in("b", matches) | |
519 |
|
525 | |||
520 | # - default quoting should work like repr |
|
526 | # - default quoting should work like repr | |
521 | _, matches = complete(line_buffer="d[") |
|
527 | _, matches = complete(line_buffer="d[") | |
522 | nt.assert_in("\"a'b\"", matches) |
|
528 | nt.assert_in("\"a'b\"", matches) | |
523 |
|
529 | |||
524 | # - when opening quote with ", possible to match with unescaped apostrophe |
|
530 | # - when opening quote with ", possible to match with unescaped apostrophe | |
525 | _, matches = complete(line_buffer="d[\"a'") |
|
531 | _, matches = complete(line_buffer="d[\"a'") | |
526 | nt.assert_in("b", matches) |
|
532 | nt.assert_in("b", matches) | |
527 |
|
533 | |||
528 | # need to not split at delims that readline won't split at |
|
534 | # need to not split at delims that readline won't split at | |
529 | if '-' not in ip.Completer.splitter.delims: |
|
535 | if '-' not in ip.Completer.splitter.delims: | |
530 | ip.user_ns['d'] = {'before-after': None} |
|
536 | ip.user_ns['d'] = {'before-after': None} | |
531 | _, matches = complete(line_buffer="d['before-af") |
|
537 | _, matches = complete(line_buffer="d['before-af") | |
532 | nt.assert_in('before-after', matches) |
|
538 | nt.assert_in('before-after', matches) | |
533 |
|
539 | |||
534 | def test_dict_key_completion_contexts(): |
|
540 | def test_dict_key_completion_contexts(): | |
535 | """Test expression contexts in which dict key completion occurs""" |
|
541 | """Test expression contexts in which dict key completion occurs""" | |
536 | ip = get_ipython() |
|
542 | ip = get_ipython() | |
537 | complete = ip.Completer.complete |
|
543 | complete = ip.Completer.complete | |
538 | d = {'abc': None} |
|
544 | d = {'abc': None} | |
539 | ip.user_ns['d'] = d |
|
545 | ip.user_ns['d'] = d | |
540 |
|
546 | |||
541 | class C: |
|
547 | class C: | |
542 | data = d |
|
548 | data = d | |
543 | ip.user_ns['C'] = C |
|
549 | ip.user_ns['C'] = C | |
544 | ip.user_ns['get'] = lambda: d |
|
550 | ip.user_ns['get'] = lambda: d | |
545 |
|
551 | |||
546 | def assert_no_completion(**kwargs): |
|
552 | def assert_no_completion(**kwargs): | |
547 | _, matches = complete(**kwargs) |
|
553 | _, matches = complete(**kwargs) | |
548 | nt.assert_not_in('abc', matches) |
|
554 | nt.assert_not_in('abc', matches) | |
549 | nt.assert_not_in('abc\'', matches) |
|
555 | nt.assert_not_in('abc\'', matches) | |
550 | nt.assert_not_in('abc\']', matches) |
|
556 | nt.assert_not_in('abc\']', matches) | |
551 | nt.assert_not_in('\'abc\'', matches) |
|
557 | nt.assert_not_in('\'abc\'', matches) | |
552 | nt.assert_not_in('\'abc\']', matches) |
|
558 | nt.assert_not_in('\'abc\']', matches) | |
553 |
|
559 | |||
554 | def assert_completion(**kwargs): |
|
560 | def assert_completion(**kwargs): | |
555 | _, matches = complete(**kwargs) |
|
561 | _, matches = complete(**kwargs) | |
556 | nt.assert_in("'abc'", matches) |
|
562 | nt.assert_in("'abc'", matches) | |
557 | nt.assert_not_in("'abc']", matches) |
|
563 | nt.assert_not_in("'abc']", matches) | |
558 |
|
564 | |||
559 | # no completion after string closed, even if reopened |
|
565 | # no completion after string closed, even if reopened | |
560 | assert_no_completion(line_buffer="d['a'") |
|
566 | assert_no_completion(line_buffer="d['a'") | |
561 | assert_no_completion(line_buffer="d[\"a\"") |
|
567 | assert_no_completion(line_buffer="d[\"a\"") | |
562 | assert_no_completion(line_buffer="d['a' + ") |
|
568 | assert_no_completion(line_buffer="d['a' + ") | |
563 | assert_no_completion(line_buffer="d['a' + '") |
|
569 | assert_no_completion(line_buffer="d['a' + '") | |
564 |
|
570 | |||
565 | # completion in non-trivial expressions |
|
571 | # completion in non-trivial expressions | |
566 | assert_completion(line_buffer="+ d[") |
|
572 | assert_completion(line_buffer="+ d[") | |
567 | assert_completion(line_buffer="(d[") |
|
573 | assert_completion(line_buffer="(d[") | |
568 | assert_completion(line_buffer="C.data[") |
|
574 | assert_completion(line_buffer="C.data[") | |
569 |
|
575 | |||
570 | # greedy flag |
|
576 | # greedy flag | |
571 | def assert_completion(**kwargs): |
|
577 | def assert_completion(**kwargs): | |
572 | _, matches = complete(**kwargs) |
|
578 | _, matches = complete(**kwargs) | |
573 | nt.assert_in("get()['abc']", matches) |
|
579 | nt.assert_in("get()['abc']", matches) | |
574 |
|
580 | |||
575 | assert_no_completion(line_buffer="get()[") |
|
581 | assert_no_completion(line_buffer="get()[") | |
576 | with greedy_completion(): |
|
582 | with greedy_completion(): | |
577 | assert_completion(line_buffer="get()[") |
|
583 | assert_completion(line_buffer="get()[") | |
578 | assert_completion(line_buffer="get()['") |
|
584 | assert_completion(line_buffer="get()['") | |
579 | assert_completion(line_buffer="get()['a") |
|
585 | assert_completion(line_buffer="get()['a") | |
580 | assert_completion(line_buffer="get()['ab") |
|
586 | assert_completion(line_buffer="get()['ab") | |
581 | assert_completion(line_buffer="get()['abc") |
|
587 | assert_completion(line_buffer="get()['abc") | |
582 |
|
588 | |||
583 |
|
589 | |||
584 |
|
590 | |||
585 | @dec.onlyif(sys.version_info[0] >= 3, 'This test only applies in Py>=3') |
|
591 | @dec.onlyif(sys.version_info[0] >= 3, 'This test only applies in Py>=3') | |
586 | def test_dict_key_completion_bytes(): |
|
592 | def test_dict_key_completion_bytes(): | |
587 | """Test handling of bytes in dict key completion""" |
|
593 | """Test handling of bytes in dict key completion""" | |
588 | ip = get_ipython() |
|
594 | ip = get_ipython() | |
589 | complete = ip.Completer.complete |
|
595 | complete = ip.Completer.complete | |
590 |
|
596 | |||
591 | ip.user_ns['d'] = {'abc': None, b'abd': None} |
|
597 | ip.user_ns['d'] = {'abc': None, b'abd': None} | |
592 |
|
598 | |||
593 | _, matches = complete(line_buffer="d[") |
|
599 | _, matches = complete(line_buffer="d[") | |
594 | nt.assert_in("'abc'", matches) |
|
600 | nt.assert_in("'abc'", matches) | |
595 | nt.assert_in("b'abd'", matches) |
|
601 | nt.assert_in("b'abd'", matches) | |
596 |
|
602 | |||
597 | if False: # not currently implemented |
|
603 | if False: # not currently implemented | |
598 | _, matches = complete(line_buffer="d[b") |
|
604 | _, matches = complete(line_buffer="d[b") | |
599 | nt.assert_in("b'abd'", matches) |
|
605 | nt.assert_in("b'abd'", matches) | |
600 | nt.assert_not_in("b'abc'", matches) |
|
606 | nt.assert_not_in("b'abc'", matches) | |
601 |
|
607 | |||
602 | _, matches = complete(line_buffer="d[b'") |
|
608 | _, matches = complete(line_buffer="d[b'") | |
603 | nt.assert_in("abd", matches) |
|
609 | nt.assert_in("abd", matches) | |
604 | nt.assert_not_in("abc", matches) |
|
610 | nt.assert_not_in("abc", matches) | |
605 |
|
611 | |||
606 | _, matches = complete(line_buffer="d[B'") |
|
612 | _, matches = complete(line_buffer="d[B'") | |
607 | nt.assert_in("abd", matches) |
|
613 | nt.assert_in("abd", matches) | |
608 | nt.assert_not_in("abc", matches) |
|
614 | nt.assert_not_in("abc", matches) | |
609 |
|
615 | |||
610 | _, matches = complete(line_buffer="d['") |
|
616 | _, matches = complete(line_buffer="d['") | |
611 | nt.assert_in("abc", matches) |
|
617 | nt.assert_in("abc", matches) | |
612 | nt.assert_not_in("abd", matches) |
|
618 | nt.assert_not_in("abd", matches) | |
613 |
|
619 | |||
614 |
|
620 | |||
615 | @dec.onlyif(sys.version_info[0] < 3, 'This test only applies in Py<3') |
|
621 | @dec.onlyif(sys.version_info[0] < 3, 'This test only applies in Py<3') | |
616 | def test_dict_key_completion_unicode_py2(): |
|
622 | def test_dict_key_completion_unicode_py2(): | |
617 | """Test handling of unicode in dict key completion""" |
|
623 | """Test handling of unicode in dict key completion""" | |
618 | ip = get_ipython() |
|
624 | ip = get_ipython() | |
619 | complete = ip.Completer.complete |
|
625 | complete = ip.Completer.complete | |
620 |
|
626 | |||
621 | ip.user_ns['d'] = {u'abc': None, |
|
627 | ip.user_ns['d'] = {u'abc': None, | |
622 | u'a\u05d0b': None} |
|
628 | u'a\u05d0b': None} | |
623 |
|
629 | |||
624 | _, matches = complete(line_buffer="d[") |
|
630 | _, matches = complete(line_buffer="d[") | |
625 | nt.assert_in("u'abc'", matches) |
|
631 | nt.assert_in("u'abc'", matches) | |
626 | nt.assert_in("u'a\\u05d0b'", matches) |
|
632 | nt.assert_in("u'a\\u05d0b'", matches) | |
627 |
|
633 | |||
628 | _, matches = complete(line_buffer="d['a") |
|
634 | _, matches = complete(line_buffer="d['a") | |
629 | nt.assert_in("abc", matches) |
|
635 | nt.assert_in("abc", matches) | |
630 | nt.assert_not_in("a\\u05d0b", matches) |
|
636 | nt.assert_not_in("a\\u05d0b", matches) | |
631 |
|
637 | |||
632 | _, matches = complete(line_buffer="d[u'a") |
|
638 | _, matches = complete(line_buffer="d[u'a") | |
633 | nt.assert_in("abc", matches) |
|
639 | nt.assert_in("abc", matches) | |
634 | nt.assert_in("a\\u05d0b", matches) |
|
640 | nt.assert_in("a\\u05d0b", matches) | |
635 |
|
641 | |||
636 | _, matches = complete(line_buffer="d[U'a") |
|
642 | _, matches = complete(line_buffer="d[U'a") | |
637 | nt.assert_in("abc", matches) |
|
643 | nt.assert_in("abc", matches) | |
638 | nt.assert_in("a\\u05d0b", matches) |
|
644 | nt.assert_in("a\\u05d0b", matches) | |
639 |
|
645 | |||
640 | # query using escape |
|
646 | # query using escape | |
641 | _, matches = complete(line_buffer=u"d[u'a\\u05d0") |
|
647 | _, matches = complete(line_buffer=u"d[u'a\\u05d0") | |
642 | nt.assert_in("u05d0b", matches) # tokenized after \\ |
|
648 | nt.assert_in("u05d0b", matches) # tokenized after \\ | |
643 |
|
649 | |||
644 | # query using character |
|
650 | # query using character | |
645 | _, matches = complete(line_buffer=u"d[u'a\u05d0") |
|
651 | _, matches = complete(line_buffer=u"d[u'a\u05d0") | |
646 | nt.assert_in(u"a\u05d0b", matches) |
|
652 | nt.assert_in(u"a\u05d0b", matches) | |
647 |
|
653 | |||
648 | with greedy_completion(): |
|
654 | with greedy_completion(): | |
649 | _, matches = complete(line_buffer="d[") |
|
655 | _, matches = complete(line_buffer="d[") | |
650 | nt.assert_in("d[u'abc']", matches) |
|
656 | nt.assert_in("d[u'abc']", matches) | |
651 | nt.assert_in("d[u'a\\u05d0b']", matches) |
|
657 | nt.assert_in("d[u'a\\u05d0b']", matches) | |
652 |
|
658 | |||
653 | _, matches = complete(line_buffer="d['a") |
|
659 | _, matches = complete(line_buffer="d['a") | |
654 | nt.assert_in("d['abc']", matches) |
|
660 | nt.assert_in("d['abc']", matches) | |
655 | nt.assert_not_in("d[u'a\\u05d0b']", matches) |
|
661 | nt.assert_not_in("d[u'a\\u05d0b']", matches) | |
656 |
|
662 | |||
657 | _, matches = complete(line_buffer="d[u'a") |
|
663 | _, matches = complete(line_buffer="d[u'a") | |
658 | nt.assert_in("d[u'abc']", matches) |
|
664 | nt.assert_in("d[u'abc']", matches) | |
659 | nt.assert_in("d[u'a\\u05d0b']", matches) |
|
665 | nt.assert_in("d[u'a\\u05d0b']", matches) | |
660 |
|
666 | |||
661 | _, matches = complete(line_buffer="d[U'a") |
|
667 | _, matches = complete(line_buffer="d[U'a") | |
662 | nt.assert_in("d[U'abc']", matches) |
|
668 | nt.assert_in("d[U'abc']", matches) | |
663 | nt.assert_in("d[U'a\\u05d0b']", matches) |
|
669 | nt.assert_in("d[U'a\\u05d0b']", matches) | |
664 |
|
670 | |||
665 | # query using escape |
|
671 | # query using escape | |
666 | _, matches = complete(line_buffer=u"d[u'a\\u05d0") |
|
672 | _, matches = complete(line_buffer=u"d[u'a\\u05d0") | |
667 | nt.assert_in("d[u'a\\u05d0b']", matches) # tokenized after \\ |
|
673 | nt.assert_in("d[u'a\\u05d0b']", matches) # tokenized after \\ | |
668 |
|
674 | |||
669 | # query using character |
|
675 | # query using character | |
670 | _, matches = complete(line_buffer=u"d[u'a\u05d0") |
|
676 | _, matches = complete(line_buffer=u"d[u'a\u05d0") | |
671 | nt.assert_in(u"d[u'a\u05d0b']", matches) |
|
677 | nt.assert_in(u"d[u'a\u05d0b']", matches) | |
672 |
|
678 | |||
673 |
|
679 | |||
674 | @dec.onlyif(sys.version_info[0] >= 3, 'This test only applies in Py>=3') |
|
680 | @dec.onlyif(sys.version_info[0] >= 3, 'This test only applies in Py>=3') | |
675 | def test_dict_key_completion_unicode_py3(): |
|
681 | def test_dict_key_completion_unicode_py3(): | |
676 | """Test handling of unicode in dict key completion""" |
|
682 | """Test handling of unicode in dict key completion""" | |
677 | ip = get_ipython() |
|
683 | ip = get_ipython() | |
678 | complete = ip.Completer.complete |
|
684 | complete = ip.Completer.complete | |
679 |
|
685 | |||
680 | ip.user_ns['d'] = {u'a\u05d0': None} |
|
686 | ip.user_ns['d'] = {u'a\u05d0': None} | |
681 |
|
687 | |||
682 | # query using escape |
|
688 | # query using escape | |
683 | _, matches = complete(line_buffer="d['a\\u05d0") |
|
689 | _, matches = complete(line_buffer="d['a\\u05d0") | |
684 | nt.assert_in("u05d0", matches) # tokenized after \\ |
|
690 | nt.assert_in("u05d0", matches) # tokenized after \\ | |
685 |
|
691 | |||
686 | # query using character |
|
692 | # query using character | |
687 | _, matches = complete(line_buffer="d['a\u05d0") |
|
693 | _, matches = complete(line_buffer="d['a\u05d0") | |
688 | nt.assert_in(u"a\u05d0", matches) |
|
694 | nt.assert_in(u"a\u05d0", matches) | |
689 |
|
695 | |||
690 | with greedy_completion(): |
|
696 | with greedy_completion(): | |
691 | # query using escape |
|
697 | # query using escape | |
692 | _, matches = complete(line_buffer="d['a\\u05d0") |
|
698 | _, matches = complete(line_buffer="d['a\\u05d0") | |
693 | nt.assert_in("d['a\\u05d0']", matches) # tokenized after \\ |
|
699 | nt.assert_in("d['a\\u05d0']", matches) # tokenized after \\ | |
694 |
|
700 | |||
695 | # query using character |
|
701 | # query using character | |
696 | _, matches = complete(line_buffer="d['a\u05d0") |
|
702 | _, matches = complete(line_buffer="d['a\u05d0") | |
697 | nt.assert_in(u"d['a\u05d0']", matches) |
|
703 | nt.assert_in(u"d['a\u05d0']", matches) | |
698 |
|
704 | |||
699 |
|
705 | |||
700 |
|
706 | |||
701 | @dec.skip_without('numpy') |
|
707 | @dec.skip_without('numpy') | |
702 | def test_struct_array_key_completion(): |
|
708 | def test_struct_array_key_completion(): | |
703 | """Test dict key completion applies to numpy struct arrays""" |
|
709 | """Test dict key completion applies to numpy struct arrays""" | |
704 | import numpy |
|
710 | import numpy | |
705 | ip = get_ipython() |
|
711 | ip = get_ipython() | |
706 | complete = ip.Completer.complete |
|
712 | complete = ip.Completer.complete | |
707 | ip.user_ns['d'] = numpy.array([], dtype=[('hello', 'f'), ('world', 'f')]) |
|
713 | ip.user_ns['d'] = numpy.array([], dtype=[('hello', 'f'), ('world', 'f')]) | |
708 | _, matches = complete(line_buffer="d['") |
|
714 | _, matches = complete(line_buffer="d['") | |
709 | nt.assert_in("hello", matches) |
|
715 | nt.assert_in("hello", matches) | |
710 | nt.assert_in("world", matches) |
|
716 | nt.assert_in("world", matches) | |
711 | # complete on the numpy struct itself |
|
717 | # complete on the numpy struct itself | |
712 | dt = numpy.dtype([('my_head', [('my_dt', '>u4'), ('my_df', '>u4')]), |
|
718 | dt = numpy.dtype([('my_head', [('my_dt', '>u4'), ('my_df', '>u4')]), | |
713 | ('my_data', '>f4', 5)]) |
|
719 | ('my_data', '>f4', 5)]) | |
714 | x = numpy.zeros(2, dtype=dt) |
|
720 | x = numpy.zeros(2, dtype=dt) | |
715 | ip.user_ns['d'] = x[1] |
|
721 | ip.user_ns['d'] = x[1] | |
716 | _, matches = complete(line_buffer="d['") |
|
722 | _, matches = complete(line_buffer="d['") | |
717 | nt.assert_in("my_head", matches) |
|
723 | nt.assert_in("my_head", matches) | |
718 | nt.assert_in("my_data", matches) |
|
724 | nt.assert_in("my_data", matches) | |
719 | # complete on a nested level |
|
725 | # complete on a nested level | |
720 | with greedy_completion(): |
|
726 | with greedy_completion(): | |
721 | ip.user_ns['d'] = numpy.zeros(2, dtype=dt) |
|
727 | ip.user_ns['d'] = numpy.zeros(2, dtype=dt) | |
722 | _, matches = complete(line_buffer="d[1]['my_head']['") |
|
728 | _, matches = complete(line_buffer="d[1]['my_head']['") | |
723 | nt.assert_true(any(["my_dt" in m for m in matches])) |
|
729 | nt.assert_true(any(["my_dt" in m for m in matches])) | |
724 | nt.assert_true(any(["my_df" in m for m in matches])) |
|
730 | nt.assert_true(any(["my_df" in m for m in matches])) | |
725 |
|
731 | |||
726 |
|
732 | |||
727 | @dec.skip_without('pandas') |
|
733 | @dec.skip_without('pandas') | |
728 | def test_dataframe_key_completion(): |
|
734 | def test_dataframe_key_completion(): | |
729 | """Test dict key completion applies to pandas DataFrames""" |
|
735 | """Test dict key completion applies to pandas DataFrames""" | |
730 | import pandas |
|
736 | import pandas | |
731 | ip = get_ipython() |
|
737 | ip = get_ipython() | |
732 | complete = ip.Completer.complete |
|
738 | complete = ip.Completer.complete | |
733 | ip.user_ns['d'] = pandas.DataFrame({'hello': [1], 'world': [2]}) |
|
739 | ip.user_ns['d'] = pandas.DataFrame({'hello': [1], 'world': [2]}) | |
734 | _, matches = complete(line_buffer="d['") |
|
740 | _, matches = complete(line_buffer="d['") | |
735 | nt.assert_in("hello", matches) |
|
741 | nt.assert_in("hello", matches) | |
736 | nt.assert_in("world", matches) |
|
742 | nt.assert_in("world", matches) | |
737 |
|
743 | |||
738 |
|
744 | |||
739 | def test_dict_key_completion_invalids(): |
|
745 | def test_dict_key_completion_invalids(): | |
740 | """Smoke test cases dict key completion can't handle""" |
|
746 | """Smoke test cases dict key completion can't handle""" | |
741 | ip = get_ipython() |
|
747 | ip = get_ipython() | |
742 | complete = ip.Completer.complete |
|
748 | complete = ip.Completer.complete | |
743 |
|
749 | |||
744 | ip.user_ns['no_getitem'] = None |
|
750 | ip.user_ns['no_getitem'] = None | |
745 | ip.user_ns['no_keys'] = [] |
|
751 | ip.user_ns['no_keys'] = [] | |
746 | ip.user_ns['cant_call_keys'] = dict |
|
752 | ip.user_ns['cant_call_keys'] = dict | |
747 | ip.user_ns['empty'] = {} |
|
753 | ip.user_ns['empty'] = {} | |
748 | ip.user_ns['d'] = {'abc': 5} |
|
754 | ip.user_ns['d'] = {'abc': 5} | |
749 |
|
755 | |||
750 | _, matches = complete(line_buffer="no_getitem['") |
|
756 | _, matches = complete(line_buffer="no_getitem['") | |
751 | _, matches = complete(line_buffer="no_keys['") |
|
757 | _, matches = complete(line_buffer="no_keys['") | |
752 | _, matches = complete(line_buffer="cant_call_keys['") |
|
758 | _, matches = complete(line_buffer="cant_call_keys['") | |
753 | _, matches = complete(line_buffer="empty['") |
|
759 | _, matches = complete(line_buffer="empty['") | |
754 | _, matches = complete(line_buffer="name_error['") |
|
760 | _, matches = complete(line_buffer="name_error['") | |
755 | _, matches = complete(line_buffer="d['\\") # incomplete escape |
|
761 | _, matches = complete(line_buffer="d['\\") # incomplete escape | |
756 |
|
762 | |||
757 | class KeyCompletable(object): |
|
763 | class KeyCompletable(object): | |
758 | def __init__(self, things=()): |
|
764 | def __init__(self, things=()): | |
759 | self.things = things |
|
765 | self.things = things | |
760 |
|
766 | |||
761 | def _ipython_key_completions_(self): |
|
767 | def _ipython_key_completions_(self): | |
762 | return list(self.things) |
|
768 | return list(self.things) | |
763 |
|
769 | |||
764 | def test_object_key_completion(): |
|
770 | def test_object_key_completion(): | |
765 | ip = get_ipython() |
|
771 | ip = get_ipython() | |
766 | ip.user_ns['key_completable'] = KeyCompletable(['qwerty', 'qwick']) |
|
772 | ip.user_ns['key_completable'] = KeyCompletable(['qwerty', 'qwick']) | |
767 |
|
773 | |||
768 | _, matches = ip.Completer.complete(line_buffer="key_completable['qw") |
|
774 | _, matches = ip.Completer.complete(line_buffer="key_completable['qw") | |
769 | nt.assert_in('qwerty', matches) |
|
775 | nt.assert_in('qwerty', matches) | |
770 | nt.assert_in('qwick', matches) |
|
776 | nt.assert_in('qwick', matches) | |
771 |
|
777 | |||
772 |
|
778 | |||
773 | def test_aimport_module_completer(): |
|
779 | def test_aimport_module_completer(): | |
774 | ip = get_ipython() |
|
780 | ip = get_ipython() | |
775 | _, matches = ip.complete('i', '%aimport i') |
|
781 | _, matches = ip.complete('i', '%aimport i') | |
776 | nt.assert_in('io', matches) |
|
782 | nt.assert_in('io', matches) | |
777 | nt.assert_not_in('int', matches) |
|
783 | nt.assert_not_in('int', matches) | |
778 |
|
784 | |||
779 | def test_nested_import_module_completer(): |
|
785 | def test_nested_import_module_completer(): | |
780 | ip = get_ipython() |
|
786 | ip = get_ipython() | |
781 | _, matches = ip.complete(None, 'import IPython.co', 17) |
|
787 | _, matches = ip.complete(None, 'import IPython.co', 17) | |
782 | nt.assert_in('IPython.core', matches) |
|
788 | nt.assert_in('IPython.core', matches) | |
783 | nt.assert_not_in('import IPython.core', matches) |
|
789 | nt.assert_not_in('import IPython.core', matches) | |
784 | nt.assert_not_in('IPython.display', matches) |
|
790 | nt.assert_not_in('IPython.display', matches) | |
785 |
|
791 | |||
786 | def test_import_module_completer(): |
|
792 | def test_import_module_completer(): | |
787 | ip = get_ipython() |
|
793 | ip = get_ipython() | |
788 | _, matches = ip.complete('i', 'import i') |
|
794 | _, matches = ip.complete('i', 'import i') | |
789 | nt.assert_in('io', matches) |
|
795 | nt.assert_in('io', matches) | |
790 | nt.assert_not_in('int', matches) |
|
796 | nt.assert_not_in('int', matches) | |
791 |
|
797 | |||
792 | def test_from_module_completer(): |
|
798 | def test_from_module_completer(): | |
793 | ip = get_ipython() |
|
799 | ip = get_ipython() | |
794 | _, matches = ip.complete('B', 'from io import B', 16) |
|
800 | _, matches = ip.complete('B', 'from io import B', 16) | |
795 | nt.assert_in('BytesIO', matches) |
|
801 | nt.assert_in('BytesIO', matches) | |
796 | nt.assert_not_in('BaseException', matches) |
|
802 | nt.assert_not_in('BaseException', matches) |
@@ -1,447 +1,452 b'' | |||||
1 | # encoding: utf-8 |
|
1 | # encoding: utf-8 | |
2 | """ |
|
2 | """ | |
3 | Utilities for path handling. |
|
3 | Utilities for path handling. | |
4 | """ |
|
4 | """ | |
5 |
|
5 | |||
6 | # Copyright (c) IPython Development Team. |
|
6 | # Copyright (c) IPython Development Team. | |
7 | # Distributed under the terms of the Modified BSD License. |
|
7 | # Distributed under the terms of the Modified BSD License. | |
8 |
|
8 | |||
9 | import os |
|
9 | import os | |
10 | import sys |
|
10 | import sys | |
11 | import errno |
|
11 | import errno | |
12 | import shutil |
|
12 | import shutil | |
13 | import random |
|
13 | import random | |
14 | import tempfile |
|
14 | import tempfile | |
15 | import glob |
|
15 | import glob | |
16 | from warnings import warn |
|
16 | from warnings import warn | |
17 | from hashlib import md5 |
|
17 | from hashlib import md5 | |
18 |
|
18 | |||
19 | from IPython.utils.process import system |
|
19 | from IPython.utils.process import system | |
20 | from IPython.utils import py3compat |
|
20 | from IPython.utils import py3compat | |
21 | from IPython.utils.decorators import undoc |
|
21 | from IPython.utils.decorators import undoc | |
22 |
|
22 | |||
23 | #----------------------------------------------------------------------------- |
|
23 | #----------------------------------------------------------------------------- | |
24 | # Code |
|
24 | # Code | |
25 | #----------------------------------------------------------------------------- |
|
25 | #----------------------------------------------------------------------------- | |
26 |
|
26 | |||
27 | fs_encoding = sys.getfilesystemencoding() |
|
27 | fs_encoding = sys.getfilesystemencoding() | |
28 |
|
28 | |||
29 | def _writable_dir(path): |
|
29 | def _writable_dir(path): | |
30 | """Whether `path` is a directory, to which the user has write access.""" |
|
30 | """Whether `path` is a directory, to which the user has write access.""" | |
31 | return os.path.isdir(path) and os.access(path, os.W_OK) |
|
31 | return os.path.isdir(path) and os.access(path, os.W_OK) | |
32 |
|
32 | |||
33 | if sys.platform == 'win32': |
|
33 | if sys.platform == 'win32': | |
34 | def _get_long_path_name(path): |
|
34 | def _get_long_path_name(path): | |
35 | """Get a long path name (expand ~) on Windows using ctypes. |
|
35 | """Get a long path name (expand ~) on Windows using ctypes. | |
36 |
|
36 | |||
37 | Examples |
|
37 | Examples | |
38 | -------- |
|
38 | -------- | |
39 |
|
39 | |||
40 | >>> get_long_path_name('c:\\docume~1') |
|
40 | >>> get_long_path_name('c:\\docume~1') | |
41 | u'c:\\\\Documents and Settings' |
|
41 | u'c:\\\\Documents and Settings' | |
42 |
|
42 | |||
43 | """ |
|
43 | """ | |
44 | try: |
|
44 | try: | |
45 | import ctypes |
|
45 | import ctypes | |
46 | except ImportError: |
|
46 | except ImportError: | |
47 | raise ImportError('you need to have ctypes installed for this to work') |
|
47 | raise ImportError('you need to have ctypes installed for this to work') | |
48 | _GetLongPathName = ctypes.windll.kernel32.GetLongPathNameW |
|
48 | _GetLongPathName = ctypes.windll.kernel32.GetLongPathNameW | |
49 | _GetLongPathName.argtypes = [ctypes.c_wchar_p, ctypes.c_wchar_p, |
|
49 | _GetLongPathName.argtypes = [ctypes.c_wchar_p, ctypes.c_wchar_p, | |
50 | ctypes.c_uint ] |
|
50 | ctypes.c_uint ] | |
51 |
|
51 | |||
52 | buf = ctypes.create_unicode_buffer(260) |
|
52 | buf = ctypes.create_unicode_buffer(260) | |
53 | rv = _GetLongPathName(path, buf, 260) |
|
53 | rv = _GetLongPathName(path, buf, 260) | |
54 | if rv == 0 or rv > 260: |
|
54 | if rv == 0 or rv > 260: | |
55 | return path |
|
55 | return path | |
56 | else: |
|
56 | else: | |
57 | return buf.value |
|
57 | return buf.value | |
58 | else: |
|
58 | else: | |
59 | def _get_long_path_name(path): |
|
59 | def _get_long_path_name(path): | |
60 | """Dummy no-op.""" |
|
60 | """Dummy no-op.""" | |
61 | return path |
|
61 | return path | |
62 |
|
62 | |||
63 |
|
63 | |||
64 |
|
64 | |||
65 | def get_long_path_name(path): |
|
65 | def get_long_path_name(path): | |
66 | """Expand a path into its long form. |
|
66 | """Expand a path into its long form. | |
67 |
|
67 | |||
68 | On Windows this expands any ~ in the paths. On other platforms, it is |
|
68 | On Windows this expands any ~ in the paths. On other platforms, it is | |
69 | a null operation. |
|
69 | a null operation. | |
70 | """ |
|
70 | """ | |
71 | return _get_long_path_name(path) |
|
71 | return _get_long_path_name(path) | |
72 |
|
72 | |||
73 |
|
73 | |||
74 | def unquote_filename(name, win32=(sys.platform=='win32')): |
|
74 | def unquote_filename(name, win32=(sys.platform=='win32')): | |
75 | """ On Windows, remove leading and trailing quotes from filenames. |
|
75 | """ On Windows, remove leading and trailing quotes from filenames. | |
|
76 | ||||
|
77 | This function has been deprecated and should not be used any more: | |||
|
78 | unquoting is now taken care of by :func:`IPython.utils.process.arg_split`. | |||
76 | """ |
|
79 | """ | |
|
80 | warn("'unquote_filename' is deprecated", DeprecationWarning) | |||
77 | if win32: |
|
81 | if win32: | |
78 | if name.startswith(("'", '"')) and name.endswith(("'", '"')): |
|
82 | if name.startswith(("'", '"')) and name.endswith(("'", '"')): | |
79 | name = name[1:-1] |
|
83 | name = name[1:-1] | |
80 | return name |
|
84 | return name | |
81 |
|
85 | |||
|
86 | ||||
82 | def compress_user(path): |
|
87 | def compress_user(path): | |
83 | """Reverse of :func:`os.path.expanduser` |
|
88 | """Reverse of :func:`os.path.expanduser` | |
84 |
""" |
|
89 | """ | |
85 | path = py3compat.unicode_to_str(path, sys.getfilesystemencoding()) |
|
90 | path = py3compat.unicode_to_str(path, sys.getfilesystemencoding()) | |
86 | home = os.path.expanduser('~') |
|
91 | home = os.path.expanduser('~') | |
87 | if path.startswith(home): |
|
92 | if path.startswith(home): | |
88 | path = "~" + path[len(home):] |
|
93 | path = "~" + path[len(home):] | |
89 | return path |
|
94 | return path | |
90 |
|
95 | |||
91 | def get_py_filename(name, force_win32=None): |
|
96 | def get_py_filename(name, force_win32=None): | |
92 | """Return a valid python filename in the current directory. |
|
97 | """Return a valid python filename in the current directory. | |
93 |
|
98 | |||
94 | If the given name is not a file, it adds '.py' and searches again. |
|
99 | If the given name is not a file, it adds '.py' and searches again. | |
95 | Raises IOError with an informative message if the file isn't found. |
|
100 | Raises IOError with an informative message if the file isn't found. | |
96 |
|
101 | |||
97 | On Windows, apply Windows semantics to the filename. In particular, remove |
|
102 | On Windows, apply Windows semantics to the filename. In particular, remove | |
98 | any quoting that has been applied to it. This option can be forced for |
|
103 | any quoting that has been applied to it. This option can be forced for | |
99 | testing purposes. |
|
104 | testing purposes. | |
100 | """ |
|
105 | """ | |
101 |
|
106 | |||
102 | name = os.path.expanduser(name) |
|
107 | name = os.path.expanduser(name) | |
103 | if force_win32 is None: |
|
108 | if force_win32 is None: | |
104 | win32 = (sys.platform == 'win32') |
|
109 | win32 = (sys.platform == 'win32') | |
105 | else: |
|
110 | else: | |
106 | win32 = force_win32 |
|
111 | win32 = force_win32 | |
107 | name = unquote_filename(name, win32=win32) |
|
112 | name = unquote_filename(name, win32=win32) | |
108 | if not os.path.isfile(name) and not name.endswith('.py'): |
|
113 | if not os.path.isfile(name) and not name.endswith('.py'): | |
109 | name += '.py' |
|
114 | name += '.py' | |
110 | if os.path.isfile(name): |
|
115 | if os.path.isfile(name): | |
111 | return name |
|
116 | return name | |
112 | else: |
|
117 | else: | |
113 | raise IOError('File `%r` not found.' % name) |
|
118 | raise IOError('File `%r` not found.' % name) | |
114 |
|
119 | |||
115 |
|
120 | |||
116 | def filefind(filename, path_dirs=None): |
|
121 | def filefind(filename, path_dirs=None): | |
117 | """Find a file by looking through a sequence of paths. |
|
122 | """Find a file by looking through a sequence of paths. | |
118 |
|
123 | |||
119 | This iterates through a sequence of paths looking for a file and returns |
|
124 | This iterates through a sequence of paths looking for a file and returns | |
120 | the full, absolute path of the first occurence of the file. If no set of |
|
125 | the full, absolute path of the first occurence of the file. If no set of | |
121 | path dirs is given, the filename is tested as is, after running through |
|
126 | path dirs is given, the filename is tested as is, after running through | |
122 | :func:`expandvars` and :func:`expanduser`. Thus a simple call:: |
|
127 | :func:`expandvars` and :func:`expanduser`. Thus a simple call:: | |
123 |
|
128 | |||
124 | filefind('myfile.txt') |
|
129 | filefind('myfile.txt') | |
125 |
|
130 | |||
126 | will find the file in the current working dir, but:: |
|
131 | will find the file in the current working dir, but:: | |
127 |
|
132 | |||
128 | filefind('~/myfile.txt') |
|
133 | filefind('~/myfile.txt') | |
129 |
|
134 | |||
130 | Will find the file in the users home directory. This function does not |
|
135 | Will find the file in the users home directory. This function does not | |
131 | automatically try any paths, such as the cwd or the user's home directory. |
|
136 | automatically try any paths, such as the cwd or the user's home directory. | |
132 |
|
137 | |||
133 | Parameters |
|
138 | Parameters | |
134 | ---------- |
|
139 | ---------- | |
135 | filename : str |
|
140 | filename : str | |
136 | The filename to look for. |
|
141 | The filename to look for. | |
137 | path_dirs : str, None or sequence of str |
|
142 | path_dirs : str, None or sequence of str | |
138 | The sequence of paths to look for the file in. If None, the filename |
|
143 | The sequence of paths to look for the file in. If None, the filename | |
139 | need to be absolute or be in the cwd. If a string, the string is |
|
144 | need to be absolute or be in the cwd. If a string, the string is | |
140 | put into a sequence and the searched. If a sequence, walk through |
|
145 | put into a sequence and the searched. If a sequence, walk through | |
141 | each element and join with ``filename``, calling :func:`expandvars` |
|
146 | each element and join with ``filename``, calling :func:`expandvars` | |
142 | and :func:`expanduser` before testing for existence. |
|
147 | and :func:`expanduser` before testing for existence. | |
143 |
|
148 | |||
144 | Returns |
|
149 | Returns | |
145 | ------- |
|
150 | ------- | |
146 | Raises :exc:`IOError` or returns absolute path to file. |
|
151 | Raises :exc:`IOError` or returns absolute path to file. | |
147 | """ |
|
152 | """ | |
148 |
|
153 | |||
149 | # If paths are quoted, abspath gets confused, strip them... |
|
154 | # If paths are quoted, abspath gets confused, strip them... | |
150 | filename = filename.strip('"').strip("'") |
|
155 | filename = filename.strip('"').strip("'") | |
151 | # If the input is an absolute path, just check it exists |
|
156 | # If the input is an absolute path, just check it exists | |
152 | if os.path.isabs(filename) and os.path.isfile(filename): |
|
157 | if os.path.isabs(filename) and os.path.isfile(filename): | |
153 | return filename |
|
158 | return filename | |
154 |
|
159 | |||
155 | if path_dirs is None: |
|
160 | if path_dirs is None: | |
156 | path_dirs = ("",) |
|
161 | path_dirs = ("",) | |
157 | elif isinstance(path_dirs, py3compat.string_types): |
|
162 | elif isinstance(path_dirs, py3compat.string_types): | |
158 | path_dirs = (path_dirs,) |
|
163 | path_dirs = (path_dirs,) | |
159 |
|
164 | |||
160 | for path in path_dirs: |
|
165 | for path in path_dirs: | |
161 | if path == '.': path = py3compat.getcwd() |
|
166 | if path == '.': path = py3compat.getcwd() | |
162 | testname = expand_path(os.path.join(path, filename)) |
|
167 | testname = expand_path(os.path.join(path, filename)) | |
163 | if os.path.isfile(testname): |
|
168 | if os.path.isfile(testname): | |
164 | return os.path.abspath(testname) |
|
169 | return os.path.abspath(testname) | |
165 |
|
170 | |||
166 | raise IOError("File %r does not exist in any of the search paths: %r" % |
|
171 | raise IOError("File %r does not exist in any of the search paths: %r" % | |
167 | (filename, path_dirs) ) |
|
172 | (filename, path_dirs) ) | |
168 |
|
173 | |||
169 |
|
174 | |||
170 | class HomeDirError(Exception): |
|
175 | class HomeDirError(Exception): | |
171 | pass |
|
176 | pass | |
172 |
|
177 | |||
173 |
|
178 | |||
174 | def get_home_dir(require_writable=False): |
|
179 | def get_home_dir(require_writable=False): | |
175 | """Return the 'home' directory, as a unicode string. |
|
180 | """Return the 'home' directory, as a unicode string. | |
176 |
|
181 | |||
177 | Uses os.path.expanduser('~'), and checks for writability. |
|
182 | Uses os.path.expanduser('~'), and checks for writability. | |
178 |
|
183 | |||
179 | See stdlib docs for how this is determined. |
|
184 | See stdlib docs for how this is determined. | |
180 | $HOME is first priority on *ALL* platforms. |
|
185 | $HOME is first priority on *ALL* platforms. | |
181 |
|
186 | |||
182 | Parameters |
|
187 | Parameters | |
183 | ---------- |
|
188 | ---------- | |
184 |
|
189 | |||
185 | require_writable : bool [default: False] |
|
190 | require_writable : bool [default: False] | |
186 | if True: |
|
191 | if True: | |
187 | guarantees the return value is a writable directory, otherwise |
|
192 | guarantees the return value is a writable directory, otherwise | |
188 | raises HomeDirError |
|
193 | raises HomeDirError | |
189 | if False: |
|
194 | if False: | |
190 | The path is resolved, but it is not guaranteed to exist or be writable. |
|
195 | The path is resolved, but it is not guaranteed to exist or be writable. | |
191 | """ |
|
196 | """ | |
192 |
|
197 | |||
193 | homedir = os.path.expanduser('~') |
|
198 | homedir = os.path.expanduser('~') | |
194 | # Next line will make things work even when /home/ is a symlink to |
|
199 | # Next line will make things work even when /home/ is a symlink to | |
195 | # /usr/home as it is on FreeBSD, for example |
|
200 | # /usr/home as it is on FreeBSD, for example | |
196 | homedir = os.path.realpath(homedir) |
|
201 | homedir = os.path.realpath(homedir) | |
197 |
|
202 | |||
198 | if not _writable_dir(homedir) and os.name == 'nt': |
|
203 | if not _writable_dir(homedir) and os.name == 'nt': | |
199 | # expanduser failed, use the registry to get the 'My Documents' folder. |
|
204 | # expanduser failed, use the registry to get the 'My Documents' folder. | |
200 | try: |
|
205 | try: | |
201 | try: |
|
206 | try: | |
202 | import winreg as wreg # Py 3 |
|
207 | import winreg as wreg # Py 3 | |
203 | except ImportError: |
|
208 | except ImportError: | |
204 | import _winreg as wreg # Py 2 |
|
209 | import _winreg as wreg # Py 2 | |
205 | key = wreg.OpenKey( |
|
210 | key = wreg.OpenKey( | |
206 | wreg.HKEY_CURRENT_USER, |
|
211 | wreg.HKEY_CURRENT_USER, | |
207 | "Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" |
|
212 | "Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" | |
208 | ) |
|
213 | ) | |
209 | homedir = wreg.QueryValueEx(key,'Personal')[0] |
|
214 | homedir = wreg.QueryValueEx(key,'Personal')[0] | |
210 | key.Close() |
|
215 | key.Close() | |
211 | except: |
|
216 | except: | |
212 | pass |
|
217 | pass | |
213 |
|
218 | |||
214 | if (not require_writable) or _writable_dir(homedir): |
|
219 | if (not require_writable) or _writable_dir(homedir): | |
215 | return py3compat.cast_unicode(homedir, fs_encoding) |
|
220 | return py3compat.cast_unicode(homedir, fs_encoding) | |
216 | else: |
|
221 | else: | |
217 | raise HomeDirError('%s is not a writable dir, ' |
|
222 | raise HomeDirError('%s is not a writable dir, ' | |
218 | 'set $HOME environment variable to override' % homedir) |
|
223 | 'set $HOME environment variable to override' % homedir) | |
219 |
|
224 | |||
220 | def get_xdg_dir(): |
|
225 | def get_xdg_dir(): | |
221 | """Return the XDG_CONFIG_HOME, if it is defined and exists, else None. |
|
226 | """Return the XDG_CONFIG_HOME, if it is defined and exists, else None. | |
222 |
|
227 | |||
223 | This is only for non-OS X posix (Linux,Unix,etc.) systems. |
|
228 | This is only for non-OS X posix (Linux,Unix,etc.) systems. | |
224 | """ |
|
229 | """ | |
225 |
|
230 | |||
226 | env = os.environ |
|
231 | env = os.environ | |
227 |
|
232 | |||
228 | if os.name == 'posix' and sys.platform != 'darwin': |
|
233 | if os.name == 'posix' and sys.platform != 'darwin': | |
229 | # Linux, Unix, AIX, etc. |
|
234 | # Linux, Unix, AIX, etc. | |
230 | # use ~/.config if empty OR not set |
|
235 | # use ~/.config if empty OR not set | |
231 | xdg = env.get("XDG_CONFIG_HOME", None) or os.path.join(get_home_dir(), '.config') |
|
236 | xdg = env.get("XDG_CONFIG_HOME", None) or os.path.join(get_home_dir(), '.config') | |
232 | if xdg and _writable_dir(xdg): |
|
237 | if xdg and _writable_dir(xdg): | |
233 | return py3compat.cast_unicode(xdg, fs_encoding) |
|
238 | return py3compat.cast_unicode(xdg, fs_encoding) | |
234 |
|
239 | |||
235 | return None |
|
240 | return None | |
236 |
|
241 | |||
237 |
|
242 | |||
238 | def get_xdg_cache_dir(): |
|
243 | def get_xdg_cache_dir(): | |
239 | """Return the XDG_CACHE_HOME, if it is defined and exists, else None. |
|
244 | """Return the XDG_CACHE_HOME, if it is defined and exists, else None. | |
240 |
|
245 | |||
241 | This is only for non-OS X posix (Linux,Unix,etc.) systems. |
|
246 | This is only for non-OS X posix (Linux,Unix,etc.) systems. | |
242 | """ |
|
247 | """ | |
243 |
|
248 | |||
244 | env = os.environ |
|
249 | env = os.environ | |
245 |
|
250 | |||
246 | if os.name == 'posix' and sys.platform != 'darwin': |
|
251 | if os.name == 'posix' and sys.platform != 'darwin': | |
247 | # Linux, Unix, AIX, etc. |
|
252 | # Linux, Unix, AIX, etc. | |
248 | # use ~/.cache if empty OR not set |
|
253 | # use ~/.cache if empty OR not set | |
249 | xdg = env.get("XDG_CACHE_HOME", None) or os.path.join(get_home_dir(), '.cache') |
|
254 | xdg = env.get("XDG_CACHE_HOME", None) or os.path.join(get_home_dir(), '.cache') | |
250 | if xdg and _writable_dir(xdg): |
|
255 | if xdg and _writable_dir(xdg): | |
251 | return py3compat.cast_unicode(xdg, fs_encoding) |
|
256 | return py3compat.cast_unicode(xdg, fs_encoding) | |
252 |
|
257 | |||
253 | return None |
|
258 | return None | |
254 |
|
259 | |||
255 |
|
260 | |||
256 | @undoc |
|
261 | @undoc | |
257 | def get_ipython_dir(): |
|
262 | def get_ipython_dir(): | |
258 | warn("get_ipython_dir has moved to the IPython.paths module") |
|
263 | warn("get_ipython_dir has moved to the IPython.paths module") | |
259 | from IPython.paths import get_ipython_dir |
|
264 | from IPython.paths import get_ipython_dir | |
260 | return get_ipython_dir() |
|
265 | return get_ipython_dir() | |
261 |
|
266 | |||
262 | @undoc |
|
267 | @undoc | |
263 | def get_ipython_cache_dir(): |
|
268 | def get_ipython_cache_dir(): | |
264 | warn("get_ipython_cache_dir has moved to the IPython.paths module") |
|
269 | warn("get_ipython_cache_dir has moved to the IPython.paths module") | |
265 | from IPython.paths import get_ipython_cache_dir |
|
270 | from IPython.paths import get_ipython_cache_dir | |
266 | return get_ipython_cache_dir() |
|
271 | return get_ipython_cache_dir() | |
267 |
|
272 | |||
268 | @undoc |
|
273 | @undoc | |
269 | def get_ipython_package_dir(): |
|
274 | def get_ipython_package_dir(): | |
270 | warn("get_ipython_package_dir has moved to the IPython.paths module") |
|
275 | warn("get_ipython_package_dir has moved to the IPython.paths module") | |
271 | from IPython.paths import get_ipython_package_dir |
|
276 | from IPython.paths import get_ipython_package_dir | |
272 | return get_ipython_package_dir() |
|
277 | return get_ipython_package_dir() | |
273 |
|
278 | |||
274 | @undoc |
|
279 | @undoc | |
275 | def get_ipython_module_path(module_str): |
|
280 | def get_ipython_module_path(module_str): | |
276 | warn("get_ipython_module_path has moved to the IPython.paths module") |
|
281 | warn("get_ipython_module_path has moved to the IPython.paths module") | |
277 | from IPython.paths import get_ipython_module_path |
|
282 | from IPython.paths import get_ipython_module_path | |
278 | return get_ipython_module_path(module_str) |
|
283 | return get_ipython_module_path(module_str) | |
279 |
|
284 | |||
280 | @undoc |
|
285 | @undoc | |
281 | def locate_profile(profile='default'): |
|
286 | def locate_profile(profile='default'): | |
282 | warn("locate_profile has moved to the IPython.paths module") |
|
287 | warn("locate_profile has moved to the IPython.paths module") | |
283 | from IPython.paths import locate_profile |
|
288 | from IPython.paths import locate_profile | |
284 | return locate_profile(profile=profile) |
|
289 | return locate_profile(profile=profile) | |
285 |
|
290 | |||
286 | def expand_path(s): |
|
291 | def expand_path(s): | |
287 | """Expand $VARS and ~names in a string, like a shell |
|
292 | """Expand $VARS and ~names in a string, like a shell | |
288 |
|
293 | |||
289 | :Examples: |
|
294 | :Examples: | |
290 |
|
295 | |||
291 | In [2]: os.environ['FOO']='test' |
|
296 | In [2]: os.environ['FOO']='test' | |
292 |
|
297 | |||
293 | In [3]: expand_path('variable FOO is $FOO') |
|
298 | In [3]: expand_path('variable FOO is $FOO') | |
294 | Out[3]: 'variable FOO is test' |
|
299 | Out[3]: 'variable FOO is test' | |
295 | """ |
|
300 | """ | |
296 | # This is a pretty subtle hack. When expand user is given a UNC path |
|
301 | # This is a pretty subtle hack. When expand user is given a UNC path | |
297 | # on Windows (\\server\share$\%username%), os.path.expandvars, removes |
|
302 | # on Windows (\\server\share$\%username%), os.path.expandvars, removes | |
298 | # the $ to get (\\server\share\%username%). I think it considered $ |
|
303 | # the $ to get (\\server\share\%username%). I think it considered $ | |
299 | # alone an empty var. But, we need the $ to remains there (it indicates |
|
304 | # alone an empty var. But, we need the $ to remains there (it indicates | |
300 | # a hidden share). |
|
305 | # a hidden share). | |
301 | if os.name=='nt': |
|
306 | if os.name=='nt': | |
302 | s = s.replace('$\\', 'IPYTHON_TEMP') |
|
307 | s = s.replace('$\\', 'IPYTHON_TEMP') | |
303 | s = os.path.expandvars(os.path.expanduser(s)) |
|
308 | s = os.path.expandvars(os.path.expanduser(s)) | |
304 | if os.name=='nt': |
|
309 | if os.name=='nt': | |
305 | s = s.replace('IPYTHON_TEMP', '$\\') |
|
310 | s = s.replace('IPYTHON_TEMP', '$\\') | |
306 | return s |
|
311 | return s | |
307 |
|
312 | |||
308 |
|
313 | |||
309 | def unescape_glob(string): |
|
314 | def unescape_glob(string): | |
310 | """Unescape glob pattern in `string`.""" |
|
315 | """Unescape glob pattern in `string`.""" | |
311 | def unescape(s): |
|
316 | def unescape(s): | |
312 | for pattern in '*[]!?': |
|
317 | for pattern in '*[]!?': | |
313 | s = s.replace(r'\{0}'.format(pattern), pattern) |
|
318 | s = s.replace(r'\{0}'.format(pattern), pattern) | |
314 | return s |
|
319 | return s | |
315 | return '\\'.join(map(unescape, string.split('\\\\'))) |
|
320 | return '\\'.join(map(unescape, string.split('\\\\'))) | |
316 |
|
321 | |||
317 |
|
322 | |||
318 | def shellglob(args): |
|
323 | def shellglob(args): | |
319 | """ |
|
324 | """ | |
320 | Do glob expansion for each element in `args` and return a flattened list. |
|
325 | Do glob expansion for each element in `args` and return a flattened list. | |
321 |
|
326 | |||
322 | Unmatched glob pattern will remain as-is in the returned list. |
|
327 | Unmatched glob pattern will remain as-is in the returned list. | |
323 |
|
328 | |||
324 | """ |
|
329 | """ | |
325 | expanded = [] |
|
330 | expanded = [] | |
326 | # Do not unescape backslash in Windows as it is interpreted as |
|
331 | # Do not unescape backslash in Windows as it is interpreted as | |
327 | # path separator: |
|
332 | # path separator: | |
328 | unescape = unescape_glob if sys.platform != 'win32' else lambda x: x |
|
333 | unescape = unescape_glob if sys.platform != 'win32' else lambda x: x | |
329 | for a in args: |
|
334 | for a in args: | |
330 | expanded.extend(glob.glob(a) or [unescape(a)]) |
|
335 | expanded.extend(glob.glob(a) or [unescape(a)]) | |
331 | return expanded |
|
336 | return expanded | |
332 |
|
337 | |||
333 |
|
338 | |||
334 | def target_outdated(target,deps): |
|
339 | def target_outdated(target,deps): | |
335 | """Determine whether a target is out of date. |
|
340 | """Determine whether a target is out of date. | |
336 |
|
341 | |||
337 | target_outdated(target,deps) -> 1/0 |
|
342 | target_outdated(target,deps) -> 1/0 | |
338 |
|
343 | |||
339 | deps: list of filenames which MUST exist. |
|
344 | deps: list of filenames which MUST exist. | |
340 | target: single filename which may or may not exist. |
|
345 | target: single filename which may or may not exist. | |
341 |
|
346 | |||
342 | If target doesn't exist or is older than any file listed in deps, return |
|
347 | If target doesn't exist or is older than any file listed in deps, return | |
343 | true, otherwise return false. |
|
348 | true, otherwise return false. | |
344 | """ |
|
349 | """ | |
345 | try: |
|
350 | try: | |
346 | target_time = os.path.getmtime(target) |
|
351 | target_time = os.path.getmtime(target) | |
347 | except os.error: |
|
352 | except os.error: | |
348 | return 1 |
|
353 | return 1 | |
349 | for dep in deps: |
|
354 | for dep in deps: | |
350 | dep_time = os.path.getmtime(dep) |
|
355 | dep_time = os.path.getmtime(dep) | |
351 | if dep_time > target_time: |
|
356 | if dep_time > target_time: | |
352 | #print "For target",target,"Dep failed:",dep # dbg |
|
357 | #print "For target",target,"Dep failed:",dep # dbg | |
353 | #print "times (dep,tar):",dep_time,target_time # dbg |
|
358 | #print "times (dep,tar):",dep_time,target_time # dbg | |
354 | return 1 |
|
359 | return 1 | |
355 | return 0 |
|
360 | return 0 | |
356 |
|
361 | |||
357 |
|
362 | |||
358 | def target_update(target,deps,cmd): |
|
363 | def target_update(target,deps,cmd): | |
359 | """Update a target with a given command given a list of dependencies. |
|
364 | """Update a target with a given command given a list of dependencies. | |
360 |
|
365 | |||
361 | target_update(target,deps,cmd) -> runs cmd if target is outdated. |
|
366 | target_update(target,deps,cmd) -> runs cmd if target is outdated. | |
362 |
|
367 | |||
363 | This is just a wrapper around target_outdated() which calls the given |
|
368 | This is just a wrapper around target_outdated() which calls the given | |
364 | command if target is outdated.""" |
|
369 | command if target is outdated.""" | |
365 |
|
370 | |||
366 | if target_outdated(target,deps): |
|
371 | if target_outdated(target,deps): | |
367 | system(cmd) |
|
372 | system(cmd) | |
368 |
|
373 | |||
369 | @undoc |
|
374 | @undoc | |
370 | def filehash(path): |
|
375 | def filehash(path): | |
371 | """Make an MD5 hash of a file, ignoring any differences in line |
|
376 | """Make an MD5 hash of a file, ignoring any differences in line | |
372 | ending characters.""" |
|
377 | ending characters.""" | |
373 | warn("filehash() is deprecated") |
|
378 | warn("filehash() is deprecated") | |
374 | with open(path, "rU") as f: |
|
379 | with open(path, "rU") as f: | |
375 | return md5(py3compat.str_to_bytes(f.read())).hexdigest() |
|
380 | return md5(py3compat.str_to_bytes(f.read())).hexdigest() | |
376 |
|
381 | |||
377 | ENOLINK = 1998 |
|
382 | ENOLINK = 1998 | |
378 |
|
383 | |||
379 | def link(src, dst): |
|
384 | def link(src, dst): | |
380 | """Hard links ``src`` to ``dst``, returning 0 or errno. |
|
385 | """Hard links ``src`` to ``dst``, returning 0 or errno. | |
381 |
|
386 | |||
382 | Note that the special errno ``ENOLINK`` will be returned if ``os.link`` isn't |
|
387 | Note that the special errno ``ENOLINK`` will be returned if ``os.link`` isn't | |
383 | supported by the operating system. |
|
388 | supported by the operating system. | |
384 | """ |
|
389 | """ | |
385 |
|
390 | |||
386 | if not hasattr(os, "link"): |
|
391 | if not hasattr(os, "link"): | |
387 | return ENOLINK |
|
392 | return ENOLINK | |
388 | link_errno = 0 |
|
393 | link_errno = 0 | |
389 | try: |
|
394 | try: | |
390 | os.link(src, dst) |
|
395 | os.link(src, dst) | |
391 | except OSError as e: |
|
396 | except OSError as e: | |
392 | link_errno = e.errno |
|
397 | link_errno = e.errno | |
393 | return link_errno |
|
398 | return link_errno | |
394 |
|
399 | |||
395 |
|
400 | |||
396 | def link_or_copy(src, dst): |
|
401 | def link_or_copy(src, dst): | |
397 | """Attempts to hardlink ``src`` to ``dst``, copying if the link fails. |
|
402 | """Attempts to hardlink ``src`` to ``dst``, copying if the link fails. | |
398 |
|
403 | |||
399 | Attempts to maintain the semantics of ``shutil.copy``. |
|
404 | Attempts to maintain the semantics of ``shutil.copy``. | |
400 |
|
405 | |||
401 | Because ``os.link`` does not overwrite files, a unique temporary file |
|
406 | Because ``os.link`` does not overwrite files, a unique temporary file | |
402 | will be used if the target already exists, then that file will be moved |
|
407 | will be used if the target already exists, then that file will be moved | |
403 | into place. |
|
408 | into place. | |
404 | """ |
|
409 | """ | |
405 |
|
410 | |||
406 | if os.path.isdir(dst): |
|
411 | if os.path.isdir(dst): | |
407 | dst = os.path.join(dst, os.path.basename(src)) |
|
412 | dst = os.path.join(dst, os.path.basename(src)) | |
408 |
|
413 | |||
409 | link_errno = link(src, dst) |
|
414 | link_errno = link(src, dst) | |
410 | if link_errno == errno.EEXIST: |
|
415 | if link_errno == errno.EEXIST: | |
411 | if os.stat(src).st_ino == os.stat(dst).st_ino: |
|
416 | if os.stat(src).st_ino == os.stat(dst).st_ino: | |
412 | # dst is already a hard link to the correct file, so we don't need |
|
417 | # dst is already a hard link to the correct file, so we don't need | |
413 | # to do anything else. If we try to link and rename the file |
|
418 | # to do anything else. If we try to link and rename the file | |
414 | # anyway, we get duplicate files - see http://bugs.python.org/issue21876 |
|
419 | # anyway, we get duplicate files - see http://bugs.python.org/issue21876 | |
415 | return |
|
420 | return | |
416 |
|
421 | |||
417 | new_dst = dst + "-temp-%04X" %(random.randint(1, 16**4), ) |
|
422 | new_dst = dst + "-temp-%04X" %(random.randint(1, 16**4), ) | |
418 | try: |
|
423 | try: | |
419 | link_or_copy(src, new_dst) |
|
424 | link_or_copy(src, new_dst) | |
420 | except: |
|
425 | except: | |
421 | try: |
|
426 | try: | |
422 | os.remove(new_dst) |
|
427 | os.remove(new_dst) | |
423 | except OSError: |
|
428 | except OSError: | |
424 | pass |
|
429 | pass | |
425 | raise |
|
430 | raise | |
426 | os.rename(new_dst, dst) |
|
431 | os.rename(new_dst, dst) | |
427 | elif link_errno != 0: |
|
432 | elif link_errno != 0: | |
428 | # Either link isn't supported, or the filesystem doesn't support |
|
433 | # Either link isn't supported, or the filesystem doesn't support | |
429 | # linking, or 'src' and 'dst' are on different filesystems. |
|
434 | # linking, or 'src' and 'dst' are on different filesystems. | |
430 | shutil.copy(src, dst) |
|
435 | shutil.copy(src, dst) | |
431 |
|
436 | |||
432 | def ensure_dir_exists(path, mode=0o755): |
|
437 | def ensure_dir_exists(path, mode=0o755): | |
433 | """ensure that a directory exists |
|
438 | """ensure that a directory exists | |
434 |
|
439 | |||
435 | If it doesn't exist, try to create it and protect against a race condition |
|
440 | If it doesn't exist, try to create it and protect against a race condition | |
436 | if another process is doing the same. |
|
441 | if another process is doing the same. | |
437 |
|
442 | |||
438 | The default permissions are 755, which differ from os.makedirs default of 777. |
|
443 | The default permissions are 755, which differ from os.makedirs default of 777. | |
439 | """ |
|
444 | """ | |
440 | if not os.path.exists(path): |
|
445 | if not os.path.exists(path): | |
441 | try: |
|
446 | try: | |
442 | os.makedirs(path, mode=mode) |
|
447 | os.makedirs(path, mode=mode) | |
443 | except OSError as e: |
|
448 | except OSError as e: | |
444 | if e.errno != errno.EEXIST: |
|
449 | if e.errno != errno.EEXIST: | |
445 | raise |
|
450 | raise | |
446 | elif not os.path.isdir(path): |
|
451 | elif not os.path.isdir(path): | |
447 | raise IOError("%r exists but is not a directory" % path) |
|
452 | raise IOError("%r exists but is not a directory" % path) |
@@ -1,514 +1,501 b'' | |||||
1 | # encoding: utf-8 |
|
1 | # encoding: utf-8 | |
2 | """Tests for IPython.utils.path.py""" |
|
2 | """Tests for IPython.utils.path.py""" | |
3 |
|
3 | |||
4 | # Copyright (c) IPython Development Team. |
|
4 | # Copyright (c) IPython Development Team. | |
5 | # Distributed under the terms of the Modified BSD License. |
|
5 | # Distributed under the terms of the Modified BSD License. | |
6 |
|
6 | |||
7 | import errno |
|
7 | import errno | |
8 | import os |
|
8 | import os | |
9 | import shutil |
|
9 | import shutil | |
10 | import sys |
|
10 | import sys | |
11 | import tempfile |
|
11 | import tempfile | |
12 | import warnings |
|
12 | import warnings | |
13 | from contextlib import contextmanager |
|
13 | from contextlib import contextmanager | |
14 |
|
14 | |||
15 | try: # Python 3.3+ |
|
15 | try: # Python 3.3+ | |
16 | from unittest.mock import patch |
|
16 | from unittest.mock import patch | |
17 | except ImportError: |
|
17 | except ImportError: | |
18 | from mock import patch |
|
18 | from mock import patch | |
19 |
|
19 | |||
20 | from os.path import join, abspath, split |
|
20 | from os.path import join, abspath, split | |
21 |
|
21 | |||
22 | from nose import SkipTest |
|
22 | from nose import SkipTest | |
23 | import nose.tools as nt |
|
23 | import nose.tools as nt | |
24 |
|
24 | |||
25 | from nose import with_setup |
|
25 | from nose import with_setup | |
26 |
|
26 | |||
27 | import IPython |
|
27 | import IPython | |
28 | from IPython import paths |
|
28 | from IPython import paths | |
29 | from IPython.testing import decorators as dec |
|
29 | from IPython.testing import decorators as dec | |
30 | from IPython.testing.decorators import (skip_if_not_win32, skip_win32, |
|
30 | from IPython.testing.decorators import (skip_if_not_win32, skip_win32, | |
31 | onlyif_unicode_paths,) |
|
31 | onlyif_unicode_paths,) | |
32 | from IPython.testing.tools import make_tempfile, AssertPrints |
|
32 | from IPython.testing.tools import make_tempfile, AssertPrints | |
33 | from IPython.utils import path |
|
33 | from IPython.utils import path | |
34 | from IPython.utils import py3compat |
|
34 | from IPython.utils import py3compat | |
35 | from IPython.utils.tempdir import TemporaryDirectory |
|
35 | from IPython.utils.tempdir import TemporaryDirectory | |
36 |
|
36 | |||
37 | # Platform-dependent imports |
|
37 | # Platform-dependent imports | |
38 | try: |
|
38 | try: | |
39 | import winreg as wreg # Py 3 |
|
39 | import winreg as wreg # Py 3 | |
40 | except ImportError: |
|
40 | except ImportError: | |
41 | try: |
|
41 | try: | |
42 | import _winreg as wreg # Py 2 |
|
42 | import _winreg as wreg # Py 2 | |
43 | except ImportError: |
|
43 | except ImportError: | |
44 | #Fake _winreg module on none windows platforms |
|
44 | #Fake _winreg module on none windows platforms | |
45 | import types |
|
45 | import types | |
46 | wr_name = "winreg" if py3compat.PY3 else "_winreg" |
|
46 | wr_name = "winreg" if py3compat.PY3 else "_winreg" | |
47 | sys.modules[wr_name] = types.ModuleType(wr_name) |
|
47 | sys.modules[wr_name] = types.ModuleType(wr_name) | |
48 | try: |
|
48 | try: | |
49 | import winreg as wreg |
|
49 | import winreg as wreg | |
50 | except ImportError: |
|
50 | except ImportError: | |
51 | import _winreg as wreg |
|
51 | import _winreg as wreg | |
52 | #Add entries that needs to be stubbed by the testing code |
|
52 | #Add entries that needs to be stubbed by the testing code | |
53 | (wreg.OpenKey, wreg.QueryValueEx,) = (None, None) |
|
53 | (wreg.OpenKey, wreg.QueryValueEx,) = (None, None) | |
54 |
|
54 | |||
55 | try: |
|
55 | try: | |
56 | reload |
|
56 | reload | |
57 | except NameError: # Python 3 |
|
57 | except NameError: # Python 3 | |
58 | from imp import reload |
|
58 | from imp import reload | |
59 |
|
59 | |||
60 | #----------------------------------------------------------------------------- |
|
60 | #----------------------------------------------------------------------------- | |
61 | # Globals |
|
61 | # Globals | |
62 | #----------------------------------------------------------------------------- |
|
62 | #----------------------------------------------------------------------------- | |
63 | env = os.environ |
|
63 | env = os.environ | |
64 | TMP_TEST_DIR = tempfile.mkdtemp() |
|
64 | TMP_TEST_DIR = tempfile.mkdtemp() | |
65 | HOME_TEST_DIR = join(TMP_TEST_DIR, "home_test_dir") |
|
65 | HOME_TEST_DIR = join(TMP_TEST_DIR, "home_test_dir") | |
66 | # |
|
66 | # | |
67 | # Setup/teardown functions/decorators |
|
67 | # Setup/teardown functions/decorators | |
68 | # |
|
68 | # | |
69 |
|
69 | |||
70 | def setup(): |
|
70 | def setup(): | |
71 | """Setup testenvironment for the module: |
|
71 | """Setup testenvironment for the module: | |
72 |
|
72 | |||
73 | - Adds dummy home dir tree |
|
73 | - Adds dummy home dir tree | |
74 | """ |
|
74 | """ | |
75 | # Do not mask exceptions here. In particular, catching WindowsError is a |
|
75 | # Do not mask exceptions here. In particular, catching WindowsError is a | |
76 | # problem because that exception is only defined on Windows... |
|
76 | # problem because that exception is only defined on Windows... | |
77 | os.makedirs(os.path.join(HOME_TEST_DIR, 'ipython')) |
|
77 | os.makedirs(os.path.join(HOME_TEST_DIR, 'ipython')) | |
78 |
|
78 | |||
79 |
|
79 | |||
80 | def teardown(): |
|
80 | def teardown(): | |
81 | """Teardown testenvironment for the module: |
|
81 | """Teardown testenvironment for the module: | |
82 |
|
82 | |||
83 | - Remove dummy home dir tree |
|
83 | - Remove dummy home dir tree | |
84 | """ |
|
84 | """ | |
85 | # Note: we remove the parent test dir, which is the root of all test |
|
85 | # Note: we remove the parent test dir, which is the root of all test | |
86 | # subdirs we may have created. Use shutil instead of os.removedirs, so |
|
86 | # subdirs we may have created. Use shutil instead of os.removedirs, so | |
87 | # that non-empty directories are all recursively removed. |
|
87 | # that non-empty directories are all recursively removed. | |
88 | shutil.rmtree(TMP_TEST_DIR) |
|
88 | shutil.rmtree(TMP_TEST_DIR) | |
89 |
|
89 | |||
90 |
|
90 | |||
91 | def setup_environment(): |
|
91 | def setup_environment(): | |
92 | """Setup testenvironment for some functions that are tested |
|
92 | """Setup testenvironment for some functions that are tested | |
93 | in this module. In particular this functions stores attributes |
|
93 | in this module. In particular this functions stores attributes | |
94 | and other things that we need to stub in some test functions. |
|
94 | and other things that we need to stub in some test functions. | |
95 | This needs to be done on a function level and not module level because |
|
95 | This needs to be done on a function level and not module level because | |
96 | each testfunction needs a pristine environment. |
|
96 | each testfunction needs a pristine environment. | |
97 | """ |
|
97 | """ | |
98 | global oldstuff, platformstuff |
|
98 | global oldstuff, platformstuff | |
99 | oldstuff = (env.copy(), os.name, sys.platform, path.get_home_dir, IPython.__file__, os.getcwd()) |
|
99 | oldstuff = (env.copy(), os.name, sys.platform, path.get_home_dir, IPython.__file__, os.getcwd()) | |
100 |
|
100 | |||
101 | def teardown_environment(): |
|
101 | def teardown_environment(): | |
102 | """Restore things that were remembered by the setup_environment function |
|
102 | """Restore things that were remembered by the setup_environment function | |
103 | """ |
|
103 | """ | |
104 | (oldenv, os.name, sys.platform, path.get_home_dir, IPython.__file__, old_wd) = oldstuff |
|
104 | (oldenv, os.name, sys.platform, path.get_home_dir, IPython.__file__, old_wd) = oldstuff | |
105 | os.chdir(old_wd) |
|
105 | os.chdir(old_wd) | |
106 | reload(path) |
|
106 | reload(path) | |
107 |
|
107 | |||
108 | for key in list(env): |
|
108 | for key in list(env): | |
109 | if key not in oldenv: |
|
109 | if key not in oldenv: | |
110 | del env[key] |
|
110 | del env[key] | |
111 | env.update(oldenv) |
|
111 | env.update(oldenv) | |
112 | if hasattr(sys, 'frozen'): |
|
112 | if hasattr(sys, 'frozen'): | |
113 | del sys.frozen |
|
113 | del sys.frozen | |
114 |
|
114 | |||
115 | # Build decorator that uses the setup_environment/setup_environment |
|
115 | # Build decorator that uses the setup_environment/setup_environment | |
116 | with_environment = with_setup(setup_environment, teardown_environment) |
|
116 | with_environment = with_setup(setup_environment, teardown_environment) | |
117 |
|
117 | |||
118 | @skip_if_not_win32 |
|
118 | @skip_if_not_win32 | |
119 | @with_environment |
|
119 | @with_environment | |
120 | def test_get_home_dir_1(): |
|
120 | def test_get_home_dir_1(): | |
121 | """Testcase for py2exe logic, un-compressed lib |
|
121 | """Testcase for py2exe logic, un-compressed lib | |
122 | """ |
|
122 | """ | |
123 | unfrozen = path.get_home_dir() |
|
123 | unfrozen = path.get_home_dir() | |
124 | sys.frozen = True |
|
124 | sys.frozen = True | |
125 |
|
125 | |||
126 | #fake filename for IPython.__init__ |
|
126 | #fake filename for IPython.__init__ | |
127 | IPython.__file__ = abspath(join(HOME_TEST_DIR, "Lib/IPython/__init__.py")) |
|
127 | IPython.__file__ = abspath(join(HOME_TEST_DIR, "Lib/IPython/__init__.py")) | |
128 |
|
128 | |||
129 | home_dir = path.get_home_dir() |
|
129 | home_dir = path.get_home_dir() | |
130 | nt.assert_equal(home_dir, unfrozen) |
|
130 | nt.assert_equal(home_dir, unfrozen) | |
131 |
|
131 | |||
132 |
|
132 | |||
133 | @skip_if_not_win32 |
|
133 | @skip_if_not_win32 | |
134 | @with_environment |
|
134 | @with_environment | |
135 | def test_get_home_dir_2(): |
|
135 | def test_get_home_dir_2(): | |
136 | """Testcase for py2exe logic, compressed lib |
|
136 | """Testcase for py2exe logic, compressed lib | |
137 | """ |
|
137 | """ | |
138 | unfrozen = path.get_home_dir() |
|
138 | unfrozen = path.get_home_dir() | |
139 | sys.frozen = True |
|
139 | sys.frozen = True | |
140 | #fake filename for IPython.__init__ |
|
140 | #fake filename for IPython.__init__ | |
141 | IPython.__file__ = abspath(join(HOME_TEST_DIR, "Library.zip/IPython/__init__.py")).lower() |
|
141 | IPython.__file__ = abspath(join(HOME_TEST_DIR, "Library.zip/IPython/__init__.py")).lower() | |
142 |
|
142 | |||
143 | home_dir = path.get_home_dir(True) |
|
143 | home_dir = path.get_home_dir(True) | |
144 | nt.assert_equal(home_dir, unfrozen) |
|
144 | nt.assert_equal(home_dir, unfrozen) | |
145 |
|
145 | |||
146 |
|
146 | |||
147 | @with_environment |
|
147 | @with_environment | |
148 | def test_get_home_dir_3(): |
|
148 | def test_get_home_dir_3(): | |
149 | """get_home_dir() uses $HOME if set""" |
|
149 | """get_home_dir() uses $HOME if set""" | |
150 | env["HOME"] = HOME_TEST_DIR |
|
150 | env["HOME"] = HOME_TEST_DIR | |
151 | home_dir = path.get_home_dir(True) |
|
151 | home_dir = path.get_home_dir(True) | |
152 | # get_home_dir expands symlinks |
|
152 | # get_home_dir expands symlinks | |
153 | nt.assert_equal(home_dir, os.path.realpath(env["HOME"])) |
|
153 | nt.assert_equal(home_dir, os.path.realpath(env["HOME"])) | |
154 |
|
154 | |||
155 |
|
155 | |||
156 | @with_environment |
|
156 | @with_environment | |
157 | def test_get_home_dir_4(): |
|
157 | def test_get_home_dir_4(): | |
158 | """get_home_dir() still works if $HOME is not set""" |
|
158 | """get_home_dir() still works if $HOME is not set""" | |
159 |
|
159 | |||
160 | if 'HOME' in env: del env['HOME'] |
|
160 | if 'HOME' in env: del env['HOME'] | |
161 | # this should still succeed, but we don't care what the answer is |
|
161 | # this should still succeed, but we don't care what the answer is | |
162 | home = path.get_home_dir(False) |
|
162 | home = path.get_home_dir(False) | |
163 |
|
163 | |||
164 | @with_environment |
|
164 | @with_environment | |
165 | def test_get_home_dir_5(): |
|
165 | def test_get_home_dir_5(): | |
166 | """raise HomeDirError if $HOME is specified, but not a writable dir""" |
|
166 | """raise HomeDirError if $HOME is specified, but not a writable dir""" | |
167 | env['HOME'] = abspath(HOME_TEST_DIR+'garbage') |
|
167 | env['HOME'] = abspath(HOME_TEST_DIR+'garbage') | |
168 | # set os.name = posix, to prevent My Documents fallback on Windows |
|
168 | # set os.name = posix, to prevent My Documents fallback on Windows | |
169 | os.name = 'posix' |
|
169 | os.name = 'posix' | |
170 | nt.assert_raises(path.HomeDirError, path.get_home_dir, True) |
|
170 | nt.assert_raises(path.HomeDirError, path.get_home_dir, True) | |
171 |
|
171 | |||
172 | # Should we stub wreg fully so we can run the test on all platforms? |
|
172 | # Should we stub wreg fully so we can run the test on all platforms? | |
173 | @skip_if_not_win32 |
|
173 | @skip_if_not_win32 | |
174 | @with_environment |
|
174 | @with_environment | |
175 | def test_get_home_dir_8(): |
|
175 | def test_get_home_dir_8(): | |
176 | """Using registry hack for 'My Documents', os=='nt' |
|
176 | """Using registry hack for 'My Documents', os=='nt' | |
177 |
|
177 | |||
178 | HOMESHARE, HOMEDRIVE, HOMEPATH, USERPROFILE and others are missing. |
|
178 | HOMESHARE, HOMEDRIVE, HOMEPATH, USERPROFILE and others are missing. | |
179 | """ |
|
179 | """ | |
180 | os.name = 'nt' |
|
180 | os.name = 'nt' | |
181 | # Remove from stub environment all keys that may be set |
|
181 | # Remove from stub environment all keys that may be set | |
182 | for key in ['HOME', 'HOMESHARE', 'HOMEDRIVE', 'HOMEPATH', 'USERPROFILE']: |
|
182 | for key in ['HOME', 'HOMESHARE', 'HOMEDRIVE', 'HOMEPATH', 'USERPROFILE']: | |
183 | env.pop(key, None) |
|
183 | env.pop(key, None) | |
184 |
|
184 | |||
185 | class key: |
|
185 | class key: | |
186 | def Close(self): |
|
186 | def Close(self): | |
187 | pass |
|
187 | pass | |
188 |
|
188 | |||
189 | with patch.object(wreg, 'OpenKey', return_value=key()), \ |
|
189 | with patch.object(wreg, 'OpenKey', return_value=key()), \ | |
190 | patch.object(wreg, 'QueryValueEx', return_value=[abspath(HOME_TEST_DIR)]): |
|
190 | patch.object(wreg, 'QueryValueEx', return_value=[abspath(HOME_TEST_DIR)]): | |
191 | home_dir = path.get_home_dir() |
|
191 | home_dir = path.get_home_dir() | |
192 | nt.assert_equal(home_dir, abspath(HOME_TEST_DIR)) |
|
192 | nt.assert_equal(home_dir, abspath(HOME_TEST_DIR)) | |
193 |
|
193 | |||
194 | @with_environment |
|
194 | @with_environment | |
195 | def test_get_xdg_dir_0(): |
|
195 | def test_get_xdg_dir_0(): | |
196 | """test_get_xdg_dir_0, check xdg_dir""" |
|
196 | """test_get_xdg_dir_0, check xdg_dir""" | |
197 | reload(path) |
|
197 | reload(path) | |
198 | path._writable_dir = lambda path: True |
|
198 | path._writable_dir = lambda path: True | |
199 | path.get_home_dir = lambda : 'somewhere' |
|
199 | path.get_home_dir = lambda : 'somewhere' | |
200 | os.name = "posix" |
|
200 | os.name = "posix" | |
201 | sys.platform = "linux2" |
|
201 | sys.platform = "linux2" | |
202 | env.pop('IPYTHON_DIR', None) |
|
202 | env.pop('IPYTHON_DIR', None) | |
203 | env.pop('IPYTHONDIR', None) |
|
203 | env.pop('IPYTHONDIR', None) | |
204 | env.pop('XDG_CONFIG_HOME', None) |
|
204 | env.pop('XDG_CONFIG_HOME', None) | |
205 |
|
205 | |||
206 | nt.assert_equal(path.get_xdg_dir(), os.path.join('somewhere', '.config')) |
|
206 | nt.assert_equal(path.get_xdg_dir(), os.path.join('somewhere', '.config')) | |
207 |
|
207 | |||
208 |
|
208 | |||
209 | @with_environment |
|
209 | @with_environment | |
210 | def test_get_xdg_dir_1(): |
|
210 | def test_get_xdg_dir_1(): | |
211 | """test_get_xdg_dir_1, check nonexistant xdg_dir""" |
|
211 | """test_get_xdg_dir_1, check nonexistant xdg_dir""" | |
212 | reload(path) |
|
212 | reload(path) | |
213 | path.get_home_dir = lambda : HOME_TEST_DIR |
|
213 | path.get_home_dir = lambda : HOME_TEST_DIR | |
214 | os.name = "posix" |
|
214 | os.name = "posix" | |
215 | sys.platform = "linux2" |
|
215 | sys.platform = "linux2" | |
216 | env.pop('IPYTHON_DIR', None) |
|
216 | env.pop('IPYTHON_DIR', None) | |
217 | env.pop('IPYTHONDIR', None) |
|
217 | env.pop('IPYTHONDIR', None) | |
218 | env.pop('XDG_CONFIG_HOME', None) |
|
218 | env.pop('XDG_CONFIG_HOME', None) | |
219 | nt.assert_equal(path.get_xdg_dir(), None) |
|
219 | nt.assert_equal(path.get_xdg_dir(), None) | |
220 |
|
220 | |||
221 | @with_environment |
|
221 | @with_environment | |
222 | def test_get_xdg_dir_2(): |
|
222 | def test_get_xdg_dir_2(): | |
223 | """test_get_xdg_dir_2, check xdg_dir default to ~/.config""" |
|
223 | """test_get_xdg_dir_2, check xdg_dir default to ~/.config""" | |
224 | reload(path) |
|
224 | reload(path) | |
225 | path.get_home_dir = lambda : HOME_TEST_DIR |
|
225 | path.get_home_dir = lambda : HOME_TEST_DIR | |
226 | os.name = "posix" |
|
226 | os.name = "posix" | |
227 | sys.platform = "linux2" |
|
227 | sys.platform = "linux2" | |
228 | env.pop('IPYTHON_DIR', None) |
|
228 | env.pop('IPYTHON_DIR', None) | |
229 | env.pop('IPYTHONDIR', None) |
|
229 | env.pop('IPYTHONDIR', None) | |
230 | env.pop('XDG_CONFIG_HOME', None) |
|
230 | env.pop('XDG_CONFIG_HOME', None) | |
231 | cfgdir=os.path.join(path.get_home_dir(), '.config') |
|
231 | cfgdir=os.path.join(path.get_home_dir(), '.config') | |
232 | if not os.path.exists(cfgdir): |
|
232 | if not os.path.exists(cfgdir): | |
233 | os.makedirs(cfgdir) |
|
233 | os.makedirs(cfgdir) | |
234 |
|
234 | |||
235 | nt.assert_equal(path.get_xdg_dir(), cfgdir) |
|
235 | nt.assert_equal(path.get_xdg_dir(), cfgdir) | |
236 |
|
236 | |||
237 | @with_environment |
|
237 | @with_environment | |
238 | def test_get_xdg_dir_3(): |
|
238 | def test_get_xdg_dir_3(): | |
239 | """test_get_xdg_dir_3, check xdg_dir not used on OS X""" |
|
239 | """test_get_xdg_dir_3, check xdg_dir not used on OS X""" | |
240 | reload(path) |
|
240 | reload(path) | |
241 | path.get_home_dir = lambda : HOME_TEST_DIR |
|
241 | path.get_home_dir = lambda : HOME_TEST_DIR | |
242 | os.name = "posix" |
|
242 | os.name = "posix" | |
243 | sys.platform = "darwin" |
|
243 | sys.platform = "darwin" | |
244 | env.pop('IPYTHON_DIR', None) |
|
244 | env.pop('IPYTHON_DIR', None) | |
245 | env.pop('IPYTHONDIR', None) |
|
245 | env.pop('IPYTHONDIR', None) | |
246 | env.pop('XDG_CONFIG_HOME', None) |
|
246 | env.pop('XDG_CONFIG_HOME', None) | |
247 | cfgdir=os.path.join(path.get_home_dir(), '.config') |
|
247 | cfgdir=os.path.join(path.get_home_dir(), '.config') | |
248 | if not os.path.exists(cfgdir): |
|
248 | if not os.path.exists(cfgdir): | |
249 | os.makedirs(cfgdir) |
|
249 | os.makedirs(cfgdir) | |
250 |
|
250 | |||
251 | nt.assert_equal(path.get_xdg_dir(), None) |
|
251 | nt.assert_equal(path.get_xdg_dir(), None) | |
252 |
|
252 | |||
253 | def test_filefind(): |
|
253 | def test_filefind(): | |
254 | """Various tests for filefind""" |
|
254 | """Various tests for filefind""" | |
255 | f = tempfile.NamedTemporaryFile() |
|
255 | f = tempfile.NamedTemporaryFile() | |
256 | # print 'fname:',f.name |
|
256 | # print 'fname:',f.name | |
257 | alt_dirs = paths.get_ipython_dir() |
|
257 | alt_dirs = paths.get_ipython_dir() | |
258 | t = path.filefind(f.name, alt_dirs) |
|
258 | t = path.filefind(f.name, alt_dirs) | |
259 | # print 'found:',t |
|
259 | # print 'found:',t | |
260 |
|
260 | |||
261 |
|
261 | |||
262 | @dec.skip_if_not_win32 |
|
262 | @dec.skip_if_not_win32 | |
263 | def test_get_long_path_name_win32(): |
|
263 | def test_get_long_path_name_win32(): | |
264 | with TemporaryDirectory() as tmpdir: |
|
264 | with TemporaryDirectory() as tmpdir: | |
265 |
|
265 | |||
266 | # Make a long path. Expands the path of tmpdir prematurely as it may already have a long |
|
266 | # Make a long path. Expands the path of tmpdir prematurely as it may already have a long | |
267 | # path component, so ensure we include the long form of it |
|
267 | # path component, so ensure we include the long form of it | |
268 | long_path = os.path.join(path.get_long_path_name(tmpdir), u'this is my long path name') |
|
268 | long_path = os.path.join(path.get_long_path_name(tmpdir), u'this is my long path name') | |
269 | os.makedirs(long_path) |
|
269 | os.makedirs(long_path) | |
270 |
|
270 | |||
271 | # Test to see if the short path evaluates correctly. |
|
271 | # Test to see if the short path evaluates correctly. | |
272 | short_path = os.path.join(tmpdir, u'THISIS~1') |
|
272 | short_path = os.path.join(tmpdir, u'THISIS~1') | |
273 | evaluated_path = path.get_long_path_name(short_path) |
|
273 | evaluated_path = path.get_long_path_name(short_path) | |
274 | nt.assert_equal(evaluated_path.lower(), long_path.lower()) |
|
274 | nt.assert_equal(evaluated_path.lower(), long_path.lower()) | |
275 |
|
275 | |||
276 |
|
276 | |||
277 | @dec.skip_win32 |
|
277 | @dec.skip_win32 | |
278 | def test_get_long_path_name(): |
|
278 | def test_get_long_path_name(): | |
279 | p = path.get_long_path_name('/usr/local') |
|
279 | p = path.get_long_path_name('/usr/local') | |
280 | nt.assert_equal(p,'/usr/local') |
|
280 | nt.assert_equal(p,'/usr/local') | |
281 |
|
281 | |||
282 | @dec.skip_win32 # can't create not-user-writable dir on win |
|
282 | @dec.skip_win32 # can't create not-user-writable dir on win | |
283 | @with_environment |
|
283 | @with_environment | |
284 | def test_not_writable_ipdir(): |
|
284 | def test_not_writable_ipdir(): | |
285 | tmpdir = tempfile.mkdtemp() |
|
285 | tmpdir = tempfile.mkdtemp() | |
286 | os.name = "posix" |
|
286 | os.name = "posix" | |
287 | env.pop('IPYTHON_DIR', None) |
|
287 | env.pop('IPYTHON_DIR', None) | |
288 | env.pop('IPYTHONDIR', None) |
|
288 | env.pop('IPYTHONDIR', None) | |
289 | env.pop('XDG_CONFIG_HOME', None) |
|
289 | env.pop('XDG_CONFIG_HOME', None) | |
290 | env['HOME'] = tmpdir |
|
290 | env['HOME'] = tmpdir | |
291 | ipdir = os.path.join(tmpdir, '.ipython') |
|
291 | ipdir = os.path.join(tmpdir, '.ipython') | |
292 | os.mkdir(ipdir, 0o555) |
|
292 | os.mkdir(ipdir, 0o555) | |
293 | try: |
|
293 | try: | |
294 | open(os.path.join(ipdir, "_foo_"), 'w').close() |
|
294 | open(os.path.join(ipdir, "_foo_"), 'w').close() | |
295 | except IOError: |
|
295 | except IOError: | |
296 | pass |
|
296 | pass | |
297 | else: |
|
297 | else: | |
298 | # I can still write to an unwritable dir, |
|
298 | # I can still write to an unwritable dir, | |
299 | # assume I'm root and skip the test |
|
299 | # assume I'm root and skip the test | |
300 | raise SkipTest("I can't create directories that I can't write to") |
|
300 | raise SkipTest("I can't create directories that I can't write to") | |
301 | with AssertPrints('is not a writable location', channel='stderr'): |
|
301 | with AssertPrints('is not a writable location', channel='stderr'): | |
302 | ipdir = paths.get_ipython_dir() |
|
302 | ipdir = paths.get_ipython_dir() | |
303 | env.pop('IPYTHON_DIR', None) |
|
303 | env.pop('IPYTHON_DIR', None) | |
304 |
|
304 | |||
305 | def test_unquote_filename(): |
|
|||
306 | for win32 in (True, False): |
|
|||
307 | nt.assert_equal(path.unquote_filename('foo.py', win32=win32), 'foo.py') |
|
|||
308 | nt.assert_equal(path.unquote_filename('foo bar.py', win32=win32), 'foo bar.py') |
|
|||
309 | nt.assert_equal(path.unquote_filename('"foo.py"', win32=True), 'foo.py') |
|
|||
310 | nt.assert_equal(path.unquote_filename('"foo bar.py"', win32=True), 'foo bar.py') |
|
|||
311 | nt.assert_equal(path.unquote_filename("'foo.py'", win32=True), 'foo.py') |
|
|||
312 | nt.assert_equal(path.unquote_filename("'foo bar.py'", win32=True), 'foo bar.py') |
|
|||
313 | nt.assert_equal(path.unquote_filename('"foo.py"', win32=False), '"foo.py"') |
|
|||
314 | nt.assert_equal(path.unquote_filename('"foo bar.py"', win32=False), '"foo bar.py"') |
|
|||
315 | nt.assert_equal(path.unquote_filename("'foo.py'", win32=False), "'foo.py'") |
|
|||
316 | nt.assert_equal(path.unquote_filename("'foo bar.py'", win32=False), "'foo bar.py'") |
|
|||
317 |
|
||||
318 | @with_environment |
|
305 | @with_environment | |
319 | def test_get_py_filename(): |
|
306 | def test_get_py_filename(): | |
320 | os.chdir(TMP_TEST_DIR) |
|
307 | os.chdir(TMP_TEST_DIR) | |
321 | for win32 in (True, False): |
|
308 | for win32 in (True, False): | |
322 | with make_tempfile('foo.py'): |
|
309 | with make_tempfile('foo.py'): | |
323 | nt.assert_equal(path.get_py_filename('foo.py', force_win32=win32), 'foo.py') |
|
310 | nt.assert_equal(path.get_py_filename('foo.py', force_win32=win32), 'foo.py') | |
324 | nt.assert_equal(path.get_py_filename('foo', force_win32=win32), 'foo.py') |
|
311 | nt.assert_equal(path.get_py_filename('foo', force_win32=win32), 'foo.py') | |
325 | with make_tempfile('foo'): |
|
312 | with make_tempfile('foo'): | |
326 | nt.assert_equal(path.get_py_filename('foo', force_win32=win32), 'foo') |
|
313 | nt.assert_equal(path.get_py_filename('foo', force_win32=win32), 'foo') | |
327 | nt.assert_raises(IOError, path.get_py_filename, 'foo.py', force_win32=win32) |
|
314 | nt.assert_raises(IOError, path.get_py_filename, 'foo.py', force_win32=win32) | |
328 | nt.assert_raises(IOError, path.get_py_filename, 'foo', force_win32=win32) |
|
315 | nt.assert_raises(IOError, path.get_py_filename, 'foo', force_win32=win32) | |
329 | nt.assert_raises(IOError, path.get_py_filename, 'foo.py', force_win32=win32) |
|
316 | nt.assert_raises(IOError, path.get_py_filename, 'foo.py', force_win32=win32) | |
330 | true_fn = 'foo with spaces.py' |
|
317 | true_fn = 'foo with spaces.py' | |
331 | with make_tempfile(true_fn): |
|
318 | with make_tempfile(true_fn): | |
332 | nt.assert_equal(path.get_py_filename('foo with spaces', force_win32=win32), true_fn) |
|
319 | nt.assert_equal(path.get_py_filename('foo with spaces', force_win32=win32), true_fn) | |
333 | nt.assert_equal(path.get_py_filename('foo with spaces.py', force_win32=win32), true_fn) |
|
320 | nt.assert_equal(path.get_py_filename('foo with spaces.py', force_win32=win32), true_fn) | |
334 | if win32: |
|
321 | if win32: | |
335 | nt.assert_equal(path.get_py_filename('"foo with spaces.py"', force_win32=True), true_fn) |
|
322 | nt.assert_equal(path.get_py_filename('"foo with spaces.py"', force_win32=True), true_fn) | |
336 | nt.assert_equal(path.get_py_filename("'foo with spaces.py'", force_win32=True), true_fn) |
|
323 | nt.assert_equal(path.get_py_filename("'foo with spaces.py'", force_win32=True), true_fn) | |
337 | else: |
|
324 | else: | |
338 | nt.assert_raises(IOError, path.get_py_filename, '"foo with spaces.py"', force_win32=False) |
|
325 | nt.assert_raises(IOError, path.get_py_filename, '"foo with spaces.py"', force_win32=False) | |
339 | nt.assert_raises(IOError, path.get_py_filename, "'foo with spaces.py'", force_win32=False) |
|
326 | nt.assert_raises(IOError, path.get_py_filename, "'foo with spaces.py'", force_win32=False) | |
340 |
|
327 | |||
341 | @onlyif_unicode_paths |
|
328 | @onlyif_unicode_paths | |
342 | def test_unicode_in_filename(): |
|
329 | def test_unicode_in_filename(): | |
343 | """When a file doesn't exist, the exception raised should be safe to call |
|
330 | """When a file doesn't exist, the exception raised should be safe to call | |
344 | str() on - i.e. in Python 2 it must only have ASCII characters. |
|
331 | str() on - i.e. in Python 2 it must only have ASCII characters. | |
345 |
|
332 | |||
346 | https://github.com/ipython/ipython/issues/875 |
|
333 | https://github.com/ipython/ipython/issues/875 | |
347 | """ |
|
334 | """ | |
348 | try: |
|
335 | try: | |
349 | # these calls should not throw unicode encode exceptions |
|
336 | # these calls should not throw unicode encode exceptions | |
350 | path.get_py_filename(u'fooéè.py', force_win32=False) |
|
337 | path.get_py_filename(u'fooéè.py', force_win32=False) | |
351 | except IOError as ex: |
|
338 | except IOError as ex: | |
352 | str(ex) |
|
339 | str(ex) | |
353 |
|
340 | |||
354 |
|
341 | |||
355 | class TestShellGlob(object): |
|
342 | class TestShellGlob(object): | |
356 |
|
343 | |||
357 | @classmethod |
|
344 | @classmethod | |
358 | def setUpClass(cls): |
|
345 | def setUpClass(cls): | |
359 | cls.filenames_start_with_a = ['a0', 'a1', 'a2'] |
|
346 | cls.filenames_start_with_a = ['a0', 'a1', 'a2'] | |
360 | cls.filenames_end_with_b = ['0b', '1b', '2b'] |
|
347 | cls.filenames_end_with_b = ['0b', '1b', '2b'] | |
361 | cls.filenames = cls.filenames_start_with_a + cls.filenames_end_with_b |
|
348 | cls.filenames = cls.filenames_start_with_a + cls.filenames_end_with_b | |
362 | cls.tempdir = TemporaryDirectory() |
|
349 | cls.tempdir = TemporaryDirectory() | |
363 | td = cls.tempdir.name |
|
350 | td = cls.tempdir.name | |
364 |
|
351 | |||
365 | with cls.in_tempdir(): |
|
352 | with cls.in_tempdir(): | |
366 | # Create empty files |
|
353 | # Create empty files | |
367 | for fname in cls.filenames: |
|
354 | for fname in cls.filenames: | |
368 | open(os.path.join(td, fname), 'w').close() |
|
355 | open(os.path.join(td, fname), 'w').close() | |
369 |
|
356 | |||
370 | @classmethod |
|
357 | @classmethod | |
371 | def tearDownClass(cls): |
|
358 | def tearDownClass(cls): | |
372 | cls.tempdir.cleanup() |
|
359 | cls.tempdir.cleanup() | |
373 |
|
360 | |||
374 | @classmethod |
|
361 | @classmethod | |
375 | @contextmanager |
|
362 | @contextmanager | |
376 | def in_tempdir(cls): |
|
363 | def in_tempdir(cls): | |
377 | save = py3compat.getcwd() |
|
364 | save = py3compat.getcwd() | |
378 | try: |
|
365 | try: | |
379 | os.chdir(cls.tempdir.name) |
|
366 | os.chdir(cls.tempdir.name) | |
380 | yield |
|
367 | yield | |
381 | finally: |
|
368 | finally: | |
382 | os.chdir(save) |
|
369 | os.chdir(save) | |
383 |
|
370 | |||
384 | def check_match(self, patterns, matches): |
|
371 | def check_match(self, patterns, matches): | |
385 | with self.in_tempdir(): |
|
372 | with self.in_tempdir(): | |
386 | # glob returns unordered list. that's why sorted is required. |
|
373 | # glob returns unordered list. that's why sorted is required. | |
387 | nt.assert_equals(sorted(path.shellglob(patterns)), |
|
374 | nt.assert_equals(sorted(path.shellglob(patterns)), | |
388 | sorted(matches)) |
|
375 | sorted(matches)) | |
389 |
|
376 | |||
390 | def common_cases(self): |
|
377 | def common_cases(self): | |
391 | return [ |
|
378 | return [ | |
392 | (['*'], self.filenames), |
|
379 | (['*'], self.filenames), | |
393 | (['a*'], self.filenames_start_with_a), |
|
380 | (['a*'], self.filenames_start_with_a), | |
394 | (['*c'], ['*c']), |
|
381 | (['*c'], ['*c']), | |
395 | (['*', 'a*', '*b', '*c'], self.filenames |
|
382 | (['*', 'a*', '*b', '*c'], self.filenames | |
396 | + self.filenames_start_with_a |
|
383 | + self.filenames_start_with_a | |
397 | + self.filenames_end_with_b |
|
384 | + self.filenames_end_with_b | |
398 | + ['*c']), |
|
385 | + ['*c']), | |
399 | (['a[012]'], self.filenames_start_with_a), |
|
386 | (['a[012]'], self.filenames_start_with_a), | |
400 | ] |
|
387 | ] | |
401 |
|
388 | |||
402 | @skip_win32 |
|
389 | @skip_win32 | |
403 | def test_match_posix(self): |
|
390 | def test_match_posix(self): | |
404 | for (patterns, matches) in self.common_cases() + [ |
|
391 | for (patterns, matches) in self.common_cases() + [ | |
405 | ([r'\*'], ['*']), |
|
392 | ([r'\*'], ['*']), | |
406 | ([r'a\*', 'a*'], ['a*'] + self.filenames_start_with_a), |
|
393 | ([r'a\*', 'a*'], ['a*'] + self.filenames_start_with_a), | |
407 | ([r'a\[012]'], ['a[012]']), |
|
394 | ([r'a\[012]'], ['a[012]']), | |
408 | ]: |
|
395 | ]: | |
409 | yield (self.check_match, patterns, matches) |
|
396 | yield (self.check_match, patterns, matches) | |
410 |
|
397 | |||
411 | @skip_if_not_win32 |
|
398 | @skip_if_not_win32 | |
412 | def test_match_windows(self): |
|
399 | def test_match_windows(self): | |
413 | for (patterns, matches) in self.common_cases() + [ |
|
400 | for (patterns, matches) in self.common_cases() + [ | |
414 | # In windows, backslash is interpreted as path |
|
401 | # In windows, backslash is interpreted as path | |
415 | # separator. Therefore, you can't escape glob |
|
402 | # separator. Therefore, you can't escape glob | |
416 | # using it. |
|
403 | # using it. | |
417 | ([r'a\*', 'a*'], [r'a\*'] + self.filenames_start_with_a), |
|
404 | ([r'a\*', 'a*'], [r'a\*'] + self.filenames_start_with_a), | |
418 | ([r'a\[012]'], [r'a\[012]']), |
|
405 | ([r'a\[012]'], [r'a\[012]']), | |
419 | ]: |
|
406 | ]: | |
420 | yield (self.check_match, patterns, matches) |
|
407 | yield (self.check_match, patterns, matches) | |
421 |
|
408 | |||
422 |
|
409 | |||
423 | def test_unescape_glob(): |
|
410 | def test_unescape_glob(): | |
424 | nt.assert_equals(path.unescape_glob(r'\*\[\!\]\?'), '*[!]?') |
|
411 | nt.assert_equals(path.unescape_glob(r'\*\[\!\]\?'), '*[!]?') | |
425 | nt.assert_equals(path.unescape_glob(r'\\*'), r'\*') |
|
412 | nt.assert_equals(path.unescape_glob(r'\\*'), r'\*') | |
426 | nt.assert_equals(path.unescape_glob(r'\\\*'), r'\*') |
|
413 | nt.assert_equals(path.unescape_glob(r'\\\*'), r'\*') | |
427 | nt.assert_equals(path.unescape_glob(r'\\a'), r'\a') |
|
414 | nt.assert_equals(path.unescape_glob(r'\\a'), r'\a') | |
428 | nt.assert_equals(path.unescape_glob(r'\a'), r'\a') |
|
415 | nt.assert_equals(path.unescape_glob(r'\a'), r'\a') | |
429 |
|
416 | |||
430 |
|
417 | |||
431 | def test_ensure_dir_exists(): |
|
418 | def test_ensure_dir_exists(): | |
432 | with TemporaryDirectory() as td: |
|
419 | with TemporaryDirectory() as td: | |
433 | d = os.path.join(td, u'βir') |
|
420 | d = os.path.join(td, u'βir') | |
434 | path.ensure_dir_exists(d) # create it |
|
421 | path.ensure_dir_exists(d) # create it | |
435 | assert os.path.isdir(d) |
|
422 | assert os.path.isdir(d) | |
436 | path.ensure_dir_exists(d) # no-op |
|
423 | path.ensure_dir_exists(d) # no-op | |
437 | f = os.path.join(td, u'Ζile') |
|
424 | f = os.path.join(td, u'Ζile') | |
438 | open(f, 'w').close() # touch |
|
425 | open(f, 'w').close() # touch | |
439 | with nt.assert_raises(IOError): |
|
426 | with nt.assert_raises(IOError): | |
440 | path.ensure_dir_exists(f) |
|
427 | path.ensure_dir_exists(f) | |
441 |
|
428 | |||
442 | class TestLinkOrCopy(object): |
|
429 | class TestLinkOrCopy(object): | |
443 | def setUp(self): |
|
430 | def setUp(self): | |
444 | self.tempdir = TemporaryDirectory() |
|
431 | self.tempdir = TemporaryDirectory() | |
445 | self.src = self.dst("src") |
|
432 | self.src = self.dst("src") | |
446 | with open(self.src, "w") as f: |
|
433 | with open(self.src, "w") as f: | |
447 | f.write("Hello, world!") |
|
434 | f.write("Hello, world!") | |
448 |
|
435 | |||
449 | def tearDown(self): |
|
436 | def tearDown(self): | |
450 | self.tempdir.cleanup() |
|
437 | self.tempdir.cleanup() | |
451 |
|
438 | |||
452 | def dst(self, *args): |
|
439 | def dst(self, *args): | |
453 | return os.path.join(self.tempdir.name, *args) |
|
440 | return os.path.join(self.tempdir.name, *args) | |
454 |
|
441 | |||
455 | def assert_inode_not_equal(self, a, b): |
|
442 | def assert_inode_not_equal(self, a, b): | |
456 | nt.assert_not_equals(os.stat(a).st_ino, os.stat(b).st_ino, |
|
443 | nt.assert_not_equals(os.stat(a).st_ino, os.stat(b).st_ino, | |
457 | "%r and %r do reference the same indoes" %(a, b)) |
|
444 | "%r and %r do reference the same indoes" %(a, b)) | |
458 |
|
445 | |||
459 | def assert_inode_equal(self, a, b): |
|
446 | def assert_inode_equal(self, a, b): | |
460 | nt.assert_equals(os.stat(a).st_ino, os.stat(b).st_ino, |
|
447 | nt.assert_equals(os.stat(a).st_ino, os.stat(b).st_ino, | |
461 | "%r and %r do not reference the same indoes" %(a, b)) |
|
448 | "%r and %r do not reference the same indoes" %(a, b)) | |
462 |
|
449 | |||
463 | def assert_content_equal(self, a, b): |
|
450 | def assert_content_equal(self, a, b): | |
464 | with open(a) as a_f: |
|
451 | with open(a) as a_f: | |
465 | with open(b) as b_f: |
|
452 | with open(b) as b_f: | |
466 | nt.assert_equals(a_f.read(), b_f.read()) |
|
453 | nt.assert_equals(a_f.read(), b_f.read()) | |
467 |
|
454 | |||
468 | @skip_win32 |
|
455 | @skip_win32 | |
469 | def test_link_successful(self): |
|
456 | def test_link_successful(self): | |
470 | dst = self.dst("target") |
|
457 | dst = self.dst("target") | |
471 | path.link_or_copy(self.src, dst) |
|
458 | path.link_or_copy(self.src, dst) | |
472 | self.assert_inode_equal(self.src, dst) |
|
459 | self.assert_inode_equal(self.src, dst) | |
473 |
|
460 | |||
474 | @skip_win32 |
|
461 | @skip_win32 | |
475 | def test_link_into_dir(self): |
|
462 | def test_link_into_dir(self): | |
476 | dst = self.dst("some_dir") |
|
463 | dst = self.dst("some_dir") | |
477 | os.mkdir(dst) |
|
464 | os.mkdir(dst) | |
478 | path.link_or_copy(self.src, dst) |
|
465 | path.link_or_copy(self.src, dst) | |
479 | expected_dst = self.dst("some_dir", os.path.basename(self.src)) |
|
466 | expected_dst = self.dst("some_dir", os.path.basename(self.src)) | |
480 | self.assert_inode_equal(self.src, expected_dst) |
|
467 | self.assert_inode_equal(self.src, expected_dst) | |
481 |
|
468 | |||
482 | @skip_win32 |
|
469 | @skip_win32 | |
483 | def test_target_exists(self): |
|
470 | def test_target_exists(self): | |
484 | dst = self.dst("target") |
|
471 | dst = self.dst("target") | |
485 | open(dst, "w").close() |
|
472 | open(dst, "w").close() | |
486 | path.link_or_copy(self.src, dst) |
|
473 | path.link_or_copy(self.src, dst) | |
487 | self.assert_inode_equal(self.src, dst) |
|
474 | self.assert_inode_equal(self.src, dst) | |
488 |
|
475 | |||
489 | @skip_win32 |
|
476 | @skip_win32 | |
490 | def test_no_link(self): |
|
477 | def test_no_link(self): | |
491 | real_link = os.link |
|
478 | real_link = os.link | |
492 | try: |
|
479 | try: | |
493 | del os.link |
|
480 | del os.link | |
494 | dst = self.dst("target") |
|
481 | dst = self.dst("target") | |
495 | path.link_or_copy(self.src, dst) |
|
482 | path.link_or_copy(self.src, dst) | |
496 | self.assert_content_equal(self.src, dst) |
|
483 | self.assert_content_equal(self.src, dst) | |
497 | self.assert_inode_not_equal(self.src, dst) |
|
484 | self.assert_inode_not_equal(self.src, dst) | |
498 | finally: |
|
485 | finally: | |
499 | os.link = real_link |
|
486 | os.link = real_link | |
500 |
|
487 | |||
501 | @skip_if_not_win32 |
|
488 | @skip_if_not_win32 | |
502 | def test_windows(self): |
|
489 | def test_windows(self): | |
503 | dst = self.dst("target") |
|
490 | dst = self.dst("target") | |
504 | path.link_or_copy(self.src, dst) |
|
491 | path.link_or_copy(self.src, dst) | |
505 | self.assert_content_equal(self.src, dst) |
|
492 | self.assert_content_equal(self.src, dst) | |
506 |
|
493 | |||
507 | def test_link_twice(self): |
|
494 | def test_link_twice(self): | |
508 | # Linking the same file twice shouldn't leave duplicates around. |
|
495 | # Linking the same file twice shouldn't leave duplicates around. | |
509 | # See https://github.com/ipython/ipython/issues/6450 |
|
496 | # See https://github.com/ipython/ipython/issues/6450 | |
510 | dst = self.dst('target') |
|
497 | dst = self.dst('target') | |
511 | path.link_or_copy(self.src, dst) |
|
498 | path.link_or_copy(self.src, dst) | |
512 | path.link_or_copy(self.src, dst) |
|
499 | path.link_or_copy(self.src, dst) | |
513 | self.assert_inode_equal(self.src, dst) |
|
500 | self.assert_inode_equal(self.src, dst) | |
514 | nt.assert_equal(sorted(os.listdir(self.tempdir.name)), ['src', 'target']) |
|
501 | nt.assert_equal(sorted(os.listdir(self.tempdir.name)), ['src', 'target']) |
General Comments 0
You need to be logged in to leave comments.
Login now