##// END OF EJS Templates
convert string_types to str
Srinivas Reddy Thatiparthy -
Show More

The requested changes are too big and content was truncated. Show full diff

@@ -1,257 +1,256 b''
1 1 # encoding: utf-8
2 2 """
3 3 System command aliases.
4 4
5 5 Authors:
6 6
7 7 * Fernando Perez
8 8 * Brian Granger
9 9 """
10 10
11 11 #-----------------------------------------------------------------------------
12 12 # Copyright (C) 2008-2011 The IPython Development Team
13 13 #
14 14 # Distributed under the terms of the BSD License.
15 15 #
16 16 # The full license is in the file COPYING.txt, distributed with this software.
17 17 #-----------------------------------------------------------------------------
18 18
19 19 #-----------------------------------------------------------------------------
20 20 # Imports
21 21 #-----------------------------------------------------------------------------
22 22
23 23 import os
24 24 import re
25 25 import sys
26 26
27 27 from traitlets.config.configurable import Configurable
28 28 from IPython.core.error import UsageError
29 29
30 from IPython.utils.py3compat import string_types
31 30 from traitlets import List, Instance
32 31 from logging import error
33 32
34 33 #-----------------------------------------------------------------------------
35 34 # Utilities
36 35 #-----------------------------------------------------------------------------
37 36
38 37 # This is used as the pattern for calls to split_user_input.
39 38 shell_line_split = re.compile(r'^(\s*)()(\S+)(.*$)')
40 39
41 40 def default_aliases():
42 41 """Return list of shell aliases to auto-define.
43 42 """
44 43 # Note: the aliases defined here should be safe to use on a kernel
45 44 # regardless of what frontend it is attached to. Frontends that use a
46 45 # kernel in-process can define additional aliases that will only work in
47 46 # their case. For example, things like 'less' or 'clear' that manipulate
48 47 # the terminal should NOT be declared here, as they will only work if the
49 48 # kernel is running inside a true terminal, and not over the network.
50 49
51 50 if os.name == 'posix':
52 51 default_aliases = [('mkdir', 'mkdir'), ('rmdir', 'rmdir'),
53 52 ('mv', 'mv'), ('rm', 'rm'), ('cp', 'cp'),
54 53 ('cat', 'cat'),
55 54 ]
56 55 # Useful set of ls aliases. The GNU and BSD options are a little
57 56 # different, so we make aliases that provide as similar as possible
58 57 # behavior in ipython, by passing the right flags for each platform
59 58 if sys.platform.startswith('linux'):
60 59 ls_aliases = [('ls', 'ls -F --color'),
61 60 # long ls
62 61 ('ll', 'ls -F -o --color'),
63 62 # ls normal files only
64 63 ('lf', 'ls -F -o --color %l | grep ^-'),
65 64 # ls symbolic links
66 65 ('lk', 'ls -F -o --color %l | grep ^l'),
67 66 # directories or links to directories,
68 67 ('ldir', 'ls -F -o --color %l | grep /$'),
69 68 # things which are executable
70 69 ('lx', 'ls -F -o --color %l | grep ^-..x'),
71 70 ]
72 71 elif sys.platform.startswith('openbsd') or sys.platform.startswith('netbsd'):
73 72 # OpenBSD, NetBSD. The ls implementation on these platforms do not support
74 73 # the -G switch and lack the ability to use colorized output.
75 74 ls_aliases = [('ls', 'ls -F'),
76 75 # long ls
77 76 ('ll', 'ls -F -l'),
78 77 # ls normal files only
79 78 ('lf', 'ls -F -l %l | grep ^-'),
80 79 # ls symbolic links
81 80 ('lk', 'ls -F -l %l | grep ^l'),
82 81 # directories or links to directories,
83 82 ('ldir', 'ls -F -l %l | grep /$'),
84 83 # things which are executable
85 84 ('lx', 'ls -F -l %l | grep ^-..x'),
86 85 ]
87 86 else:
88 87 # BSD, OSX, etc.
89 88 ls_aliases = [('ls', 'ls -F -G'),
90 89 # long ls
91 90 ('ll', 'ls -F -l -G'),
92 91 # ls normal files only
93 92 ('lf', 'ls -F -l -G %l | grep ^-'),
94 93 # ls symbolic links
95 94 ('lk', 'ls -F -l -G %l | grep ^l'),
96 95 # directories or links to directories,
97 96 ('ldir', 'ls -F -G -l %l | grep /$'),
98 97 # things which are executable
99 98 ('lx', 'ls -F -l -G %l | grep ^-..x'),
100 99 ]
101 100 default_aliases = default_aliases + ls_aliases
102 101 elif os.name in ['nt', 'dos']:
103 102 default_aliases = [('ls', 'dir /on'),
104 103 ('ddir', 'dir /ad /on'), ('ldir', 'dir /ad /on'),
105 104 ('mkdir', 'mkdir'), ('rmdir', 'rmdir'),
106 105 ('echo', 'echo'), ('ren', 'ren'), ('copy', 'copy'),
107 106 ]
108 107 else:
109 108 default_aliases = []
110 109
111 110 return default_aliases
112 111
113 112
114 113 class AliasError(Exception):
115 114 pass
116 115
117 116
118 117 class InvalidAliasError(AliasError):
119 118 pass
120 119
121 120 class Alias(object):
122 121 """Callable object storing the details of one alias.
123 122
124 123 Instances are registered as magic functions to allow use of aliases.
125 124 """
126 125
127 126 # Prepare blacklist
128 127 blacklist = {'cd','popd','pushd','dhist','alias','unalias'}
129 128
130 129 def __init__(self, shell, name, cmd):
131 130 self.shell = shell
132 131 self.name = name
133 132 self.cmd = cmd
134 133 self.__doc__ = "Alias for `!{}`".format(cmd)
135 134 self.nargs = self.validate()
136 135
137 136 def validate(self):
138 137 """Validate the alias, and return the number of arguments."""
139 138 if self.name in self.blacklist:
140 139 raise InvalidAliasError("The name %s can't be aliased "
141 140 "because it is a keyword or builtin." % self.name)
142 141 try:
143 142 caller = self.shell.magics_manager.magics['line'][self.name]
144 143 except KeyError:
145 144 pass
146 145 else:
147 146 if not isinstance(caller, Alias):
148 147 raise InvalidAliasError("The name %s can't be aliased "
149 148 "because it is another magic command." % self.name)
150 149
151 if not (isinstance(self.cmd, string_types)):
150 if not (isinstance(self.cmd, str)):
152 151 raise InvalidAliasError("An alias command must be a string, "
153 152 "got: %r" % self.cmd)
154 153
155 154 nargs = self.cmd.count('%s') - self.cmd.count('%%s')
156 155
157 156 if (nargs > 0) and (self.cmd.find('%l') >= 0):
158 157 raise InvalidAliasError('The %s and %l specifiers are mutually '
159 158 'exclusive in alias definitions.')
160 159
161 160 return nargs
162 161
163 162 def __repr__(self):
164 163 return "<alias {} for {!r}>".format(self.name, self.cmd)
165 164
166 165 def __call__(self, rest=''):
167 166 cmd = self.cmd
168 167 nargs = self.nargs
169 168 # Expand the %l special to be the user's input line
170 169 if cmd.find('%l') >= 0:
171 170 cmd = cmd.replace('%l', rest)
172 171 rest = ''
173 172
174 173 if nargs==0:
175 174 if cmd.find('%%s') >= 1:
176 175 cmd = cmd.replace('%%s', '%s')
177 176 # Simple, argument-less aliases
178 177 cmd = '%s %s' % (cmd, rest)
179 178 else:
180 179 # Handle aliases with positional arguments
181 180 args = rest.split(None, nargs)
182 181 if len(args) < nargs:
183 182 raise UsageError('Alias <%s> requires %s arguments, %s given.' %
184 183 (self.name, nargs, len(args)))
185 184 cmd = '%s %s' % (cmd % tuple(args[:nargs]),' '.join(args[nargs:]))
186 185
187 186 self.shell.system(cmd)
188 187
189 188 #-----------------------------------------------------------------------------
190 189 # Main AliasManager class
191 190 #-----------------------------------------------------------------------------
192 191
193 192 class AliasManager(Configurable):
194 193
195 194 default_aliases = List(default_aliases()).tag(config=True)
196 195 user_aliases = List(default_value=[]).tag(config=True)
197 196 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC', allow_none=True)
198 197
199 198 def __init__(self, shell=None, **kwargs):
200 199 super(AliasManager, self).__init__(shell=shell, **kwargs)
201 200 # For convenient access
202 201 self.linemagics = self.shell.magics_manager.magics['line']
203 202 self.init_aliases()
204 203
205 204 def init_aliases(self):
206 205 # Load default & user aliases
207 206 for name, cmd in self.default_aliases + self.user_aliases:
208 207 self.soft_define_alias(name, cmd)
209 208
210 209 @property
211 210 def aliases(self):
212 211 return [(n, func.cmd) for (n, func) in self.linemagics.items()
213 212 if isinstance(func, Alias)]
214 213
215 214 def soft_define_alias(self, name, cmd):
216 215 """Define an alias, but don't raise on an AliasError."""
217 216 try:
218 217 self.define_alias(name, cmd)
219 218 except AliasError as e:
220 219 error("Invalid alias: %s" % e)
221 220
222 221 def define_alias(self, name, cmd):
223 222 """Define a new alias after validating it.
224 223
225 224 This will raise an :exc:`AliasError` if there are validation
226 225 problems.
227 226 """
228 227 caller = Alias(shell=self.shell, name=name, cmd=cmd)
229 228 self.shell.magics_manager.register_function(caller, magic_kind='line',
230 229 magic_name=name)
231 230
232 231 def get_alias(self, name):
233 232 """Return an alias, or None if no alias by that name exists."""
234 233 aname = self.linemagics.get(name, None)
235 234 return aname if isinstance(aname, Alias) else None
236 235
237 236 def is_alias(self, name):
238 237 """Return whether or not a given name has been defined as an alias"""
239 238 return self.get_alias(name) is not None
240 239
241 240 def undefine_alias(self, name):
242 241 if self.is_alias(name):
243 242 del self.linemagics[name]
244 243 else:
245 244 raise ValueError('%s is not an alias' % name)
246 245
247 246 def clear_aliases(self):
248 247 for name, cmd in self.aliases:
249 248 self.undefine_alias(name)
250 249
251 250 def retrieve_alias(self, name):
252 251 """Retrieve the command to which an alias expands."""
253 252 caller = self.get_alias(name)
254 253 if caller:
255 254 return caller.cmd
256 255 else:
257 256 raise ValueError('%s is not an alias' % name)
@@ -1,1229 +1,1229 b''
1 1 # encoding: utf-8
2 2 """Word completion for IPython.
3 3
4 4 This module started as fork of the rlcompleter module in the Python standard
5 5 library. The original enhancements made to rlcompleter have been sent
6 6 upstream and were accepted as of Python 2.3,
7 7
8 8 """
9 9
10 10 # Copyright (c) IPython Development Team.
11 11 # Distributed under the terms of the Modified BSD License.
12 12 #
13 13 # Some of this code originated from rlcompleter in the Python standard library
14 14 # Copyright (C) 2001 Python Software Foundation, www.python.org
15 15
16 16
17 17 import __main__
18 18 import glob
19 19 import inspect
20 20 import itertools
21 21 import keyword
22 22 import os
23 23 import re
24 24 import sys
25 25 import unicodedata
26 26 import string
27 27 import warnings
28 28 from importlib import import_module
29 29
30 30 from traitlets.config.configurable import Configurable
31 31 from IPython.core.error import TryNext
32 32 from IPython.core.inputsplitter import ESC_MAGIC
33 33 from IPython.core.latex_symbols import latex_symbols, reverse_latex_symbol
34 34 from IPython.utils import generics
35 35 from IPython.utils.decorators import undoc
36 36 from IPython.utils.dir2 import dir2, get_real_method
37 37 from IPython.utils.process import arg_split
38 from IPython.utils.py3compat import builtin_mod, string_types, PY3, cast_unicode_py2
38 from IPython.utils.py3compat import builtin_mod, PY3, cast_unicode_py2
39 39 from traitlets import Bool, Enum, observe
40 40
41 41 from functools import wraps
42 42
43 43 #-----------------------------------------------------------------------------
44 44 # Globals
45 45 #-----------------------------------------------------------------------------
46 46
47 47 # Public API
48 48 __all__ = ['Completer','IPCompleter']
49 49
50 50 if sys.platform == 'win32':
51 51 PROTECTABLES = ' '
52 52 else:
53 53 PROTECTABLES = ' ()[]{}?=\\|;:\'#*"^&'
54 54
55 55
56 56 #-----------------------------------------------------------------------------
57 57 # Work around BUG decorators.
58 58 #-----------------------------------------------------------------------------
59 59
60 60 def _strip_single_trailing_space(complete):
61 61 """
62 62 This is a workaround for a weird IPython/Prompt_toolkit behavior,
63 63 that can be removed once we rely on a slightly more recent prompt_toolkit
64 64 version (likely > 1.0.3). So this can likely be removed in IPython 6.0
65 65
66 66 cf https://github.com/ipython/ipython/issues/9658
67 67 and https://github.com/jonathanslenders/python-prompt-toolkit/pull/328
68 68
69 69 The bug is due to the fact that in PTK the completer will reinvoke itself
70 70 after trying to completer to the longuest common prefix of all the
71 71 completions, unless only one completion is available.
72 72
73 73 This logic is faulty if the completion ends with space, which can happen in
74 74 case like::
75 75
76 76 from foo import im<ta>
77 77
78 78 which only matching completion is `import `. Note the leading space at the
79 79 end. So leaving a space at the end is a reasonable request, but for now
80 80 we'll strip it.
81 81 """
82 82
83 83 @wraps(complete)
84 84 def comp(*args, **kwargs):
85 85 text, matches = complete(*args, **kwargs)
86 86 if len(matches) == 1:
87 87 return text, [matches[0].rstrip()]
88 88 return text, matches
89 89
90 90 return comp
91 91
92 92
93 93
94 94 #-----------------------------------------------------------------------------
95 95 # Main functions and classes
96 96 #-----------------------------------------------------------------------------
97 97
98 98 def has_open_quotes(s):
99 99 """Return whether a string has open quotes.
100 100
101 101 This simply counts whether the number of quote characters of either type in
102 102 the string is odd.
103 103
104 104 Returns
105 105 -------
106 106 If there is an open quote, the quote character is returned. Else, return
107 107 False.
108 108 """
109 109 # We check " first, then ', so complex cases with nested quotes will get
110 110 # the " to take precedence.
111 111 if s.count('"') % 2:
112 112 return '"'
113 113 elif s.count("'") % 2:
114 114 return "'"
115 115 else:
116 116 return False
117 117
118 118
119 119 def protect_filename(s):
120 120 """Escape a string to protect certain characters."""
121 121 if set(s) & set(PROTECTABLES):
122 122 if sys.platform == "win32":
123 123 return '"' + s + '"'
124 124 else:
125 125 return "".join(("\\" + c if c in PROTECTABLES else c) for c in s)
126 126 else:
127 127 return s
128 128
129 129
130 130 def expand_user(path):
131 131 """Expand '~'-style usernames in strings.
132 132
133 133 This is similar to :func:`os.path.expanduser`, but it computes and returns
134 134 extra information that will be useful if the input was being used in
135 135 computing completions, and you wish to return the completions with the
136 136 original '~' instead of its expanded value.
137 137
138 138 Parameters
139 139 ----------
140 140 path : str
141 141 String to be expanded. If no ~ is present, the output is the same as the
142 142 input.
143 143
144 144 Returns
145 145 -------
146 146 newpath : str
147 147 Result of ~ expansion in the input path.
148 148 tilde_expand : bool
149 149 Whether any expansion was performed or not.
150 150 tilde_val : str
151 151 The value that ~ was replaced with.
152 152 """
153 153 # Default values
154 154 tilde_expand = False
155 155 tilde_val = ''
156 156 newpath = path
157 157
158 158 if path.startswith('~'):
159 159 tilde_expand = True
160 160 rest = len(path)-1
161 161 newpath = os.path.expanduser(path)
162 162 if rest:
163 163 tilde_val = newpath[:-rest]
164 164 else:
165 165 tilde_val = newpath
166 166
167 167 return newpath, tilde_expand, tilde_val
168 168
169 169
170 170 def compress_user(path, tilde_expand, tilde_val):
171 171 """Does the opposite of expand_user, with its outputs.
172 172 """
173 173 if tilde_expand:
174 174 return path.replace(tilde_val, '~')
175 175 else:
176 176 return path
177 177
178 178
179 179 def completions_sorting_key(word):
180 180 """key for sorting completions
181 181
182 182 This does several things:
183 183
184 184 - Lowercase all completions, so they are sorted alphabetically with
185 185 upper and lower case words mingled
186 186 - Demote any completions starting with underscores to the end
187 187 - Insert any %magic and %%cellmagic completions in the alphabetical order
188 188 by their name
189 189 """
190 190 # Case insensitive sort
191 191 word = word.lower()
192 192
193 193 prio1, prio2 = 0, 0
194 194
195 195 if word.startswith('__'):
196 196 prio1 = 2
197 197 elif word.startswith('_'):
198 198 prio1 = 1
199 199
200 200 if word.endswith('='):
201 201 prio1 = -1
202 202
203 203 if word.startswith('%%'):
204 204 # If there's another % in there, this is something else, so leave it alone
205 205 if not "%" in word[2:]:
206 206 word = word[2:]
207 207 prio2 = 2
208 208 elif word.startswith('%'):
209 209 if not "%" in word[1:]:
210 210 word = word[1:]
211 211 prio2 = 1
212 212
213 213 return prio1, word, prio2
214 214
215 215
216 216 @undoc
217 217 class Bunch(object): pass
218 218
219 219
220 220 if sys.platform == 'win32':
221 221 DELIMS = ' \t\n`!@#$^&*()=+[{]}|;\'",<>?'
222 222 else:
223 223 DELIMS = ' \t\n`!@#$^&*()=+[{]}\\|;:\'",<>?'
224 224
225 225 GREEDY_DELIMS = ' =\r\n'
226 226
227 227
228 228 class CompletionSplitter(object):
229 229 """An object to split an input line in a manner similar to readline.
230 230
231 231 By having our own implementation, we can expose readline-like completion in
232 232 a uniform manner to all frontends. This object only needs to be given the
233 233 line of text to be split and the cursor position on said line, and it
234 234 returns the 'word' to be completed on at the cursor after splitting the
235 235 entire line.
236 236
237 237 What characters are used as splitting delimiters can be controlled by
238 238 setting the `delims` attribute (this is a property that internally
239 239 automatically builds the necessary regular expression)"""
240 240
241 241 # Private interface
242 242
243 243 # A string of delimiter characters. The default value makes sense for
244 244 # IPython's most typical usage patterns.
245 245 _delims = DELIMS
246 246
247 247 # The expression (a normal string) to be compiled into a regular expression
248 248 # for actual splitting. We store it as an attribute mostly for ease of
249 249 # debugging, since this type of code can be so tricky to debug.
250 250 _delim_expr = None
251 251
252 252 # The regular expression that does the actual splitting
253 253 _delim_re = None
254 254
255 255 def __init__(self, delims=None):
256 256 delims = CompletionSplitter._delims if delims is None else delims
257 257 self.delims = delims
258 258
259 259 @property
260 260 def delims(self):
261 261 """Return the string of delimiter characters."""
262 262 return self._delims
263 263
264 264 @delims.setter
265 265 def delims(self, delims):
266 266 """Set the delimiters for line splitting."""
267 267 expr = '[' + ''.join('\\'+ c for c in delims) + ']'
268 268 self._delim_re = re.compile(expr)
269 269 self._delims = delims
270 270 self._delim_expr = expr
271 271
272 272 def split_line(self, line, cursor_pos=None):
273 273 """Split a line of text with a cursor at the given position.
274 274 """
275 275 l = line if cursor_pos is None else line[:cursor_pos]
276 276 return self._delim_re.split(l)[-1]
277 277
278 278
279 279 class Completer(Configurable):
280 280
281 281 greedy = Bool(False,
282 282 help="""Activate greedy completion
283 283 PENDING DEPRECTION. this is now mostly taken care of with Jedi.
284 284
285 285 This will enable completion on elements of lists, results of function calls, etc.,
286 286 but can be unsafe because the code is actually evaluated on TAB.
287 287 """
288 288 ).tag(config=True)
289 289
290 290
291 291 def __init__(self, namespace=None, global_namespace=None, **kwargs):
292 292 """Create a new completer for the command line.
293 293
294 294 Completer(namespace=ns, global_namespace=ns2) -> completer instance.
295 295
296 296 If unspecified, the default namespace where completions are performed
297 297 is __main__ (technically, __main__.__dict__). Namespaces should be
298 298 given as dictionaries.
299 299
300 300 An optional second namespace can be given. This allows the completer
301 301 to handle cases where both the local and global scopes need to be
302 302 distinguished.
303 303
304 304 Completer instances should be used as the completion mechanism of
305 305 readline via the set_completer() call:
306 306
307 307 readline.set_completer(Completer(my_namespace).complete)
308 308 """
309 309
310 310 # Don't bind to namespace quite yet, but flag whether the user wants a
311 311 # specific namespace or to use __main__.__dict__. This will allow us
312 312 # to bind to __main__.__dict__ at completion time, not now.
313 313 if namespace is None:
314 314 self.use_main_ns = 1
315 315 else:
316 316 self.use_main_ns = 0
317 317 self.namespace = namespace
318 318
319 319 # The global namespace, if given, can be bound directly
320 320 if global_namespace is None:
321 321 self.global_namespace = {}
322 322 else:
323 323 self.global_namespace = global_namespace
324 324
325 325 super(Completer, self).__init__(**kwargs)
326 326
327 327 def complete(self, text, state):
328 328 """Return the next possible completion for 'text'.
329 329
330 330 This is called successively with state == 0, 1, 2, ... until it
331 331 returns None. The completion should begin with 'text'.
332 332
333 333 """
334 334 if self.use_main_ns:
335 335 self.namespace = __main__.__dict__
336 336
337 337 if state == 0:
338 338 if "." in text:
339 339 self.matches = self.attr_matches(text)
340 340 else:
341 341 self.matches = self.global_matches(text)
342 342 try:
343 343 return self.matches[state]
344 344 except IndexError:
345 345 return None
346 346
347 347 def global_matches(self, text):
348 348 """Compute matches when text is a simple name.
349 349
350 350 Return a list of all keywords, built-in functions and names currently
351 351 defined in self.namespace or self.global_namespace that match.
352 352
353 353 """
354 354 matches = []
355 355 match_append = matches.append
356 356 n = len(text)
357 357 for lst in [keyword.kwlist,
358 358 builtin_mod.__dict__.keys(),
359 359 self.namespace.keys(),
360 360 self.global_namespace.keys()]:
361 361 for word in lst:
362 362 if word[:n] == text and word != "__builtins__":
363 363 match_append(word)
364 364 return [cast_unicode_py2(m) for m in matches]
365 365
366 366 def attr_matches(self, text):
367 367 """Compute matches when text contains a dot.
368 368
369 369 Assuming the text is of the form NAME.NAME....[NAME], and is
370 370 evaluatable in self.namespace or self.global_namespace, it will be
371 371 evaluated and its attributes (as revealed by dir()) are used as
372 372 possible completions. (For class instances, class members are are
373 373 also considered.)
374 374
375 375 WARNING: this can still invoke arbitrary C code, if an object
376 376 with a __getattr__ hook is evaluated.
377 377
378 378 """
379 379
380 380 # Another option, seems to work great. Catches things like ''.<tab>
381 381 m = re.match(r"(\S+(\.\w+)*)\.(\w*)$", text)
382 382
383 383 if m:
384 384 expr, attr = m.group(1, 3)
385 385 elif self.greedy:
386 386 m2 = re.match(r"(.+)\.(\w*)$", self.line_buffer)
387 387 if not m2:
388 388 return []
389 389 expr, attr = m2.group(1,2)
390 390 else:
391 391 return []
392 392
393 393 try:
394 394 obj = eval(expr, self.namespace)
395 395 except:
396 396 try:
397 397 obj = eval(expr, self.global_namespace)
398 398 except:
399 399 return []
400 400
401 401 if self.limit_to__all__ and hasattr(obj, '__all__'):
402 402 words = get__all__entries(obj)
403 403 else:
404 404 words = dir2(obj)
405 405
406 406 try:
407 407 words = generics.complete_object(obj, words)
408 408 except TryNext:
409 409 pass
410 410 except Exception:
411 411 # Silence errors from completion function
412 412 #raise # dbg
413 413 pass
414 414 # Build match list to return
415 415 n = len(attr)
416 416 return [u"%s.%s" % (expr, w) for w in words if w[:n] == attr ]
417 417
418 418
419 419 def get__all__entries(obj):
420 420 """returns the strings in the __all__ attribute"""
421 421 try:
422 422 words = getattr(obj, '__all__')
423 423 except:
424 424 return []
425 425
426 return [cast_unicode_py2(w) for w in words if isinstance(w, string_types)]
426 return [cast_unicode_py2(w) for w in words if isinstance(w, str)]
427 427
428 428
429 429 def match_dict_keys(keys, prefix, delims):
430 430 """Used by dict_key_matches, matching the prefix to a list of keys"""
431 431 if not prefix:
432 432 return None, 0, [repr(k) for k in keys
433 if isinstance(k, (string_types, bytes))]
433 if isinstance(k, (str, bytes))]
434 434 quote_match = re.search('["\']', prefix)
435 435 quote = quote_match.group()
436 436 try:
437 437 prefix_str = eval(prefix + quote, {})
438 438 except Exception:
439 439 return None, 0, []
440 440
441 441 pattern = '[^' + ''.join('\\' + c for c in delims) + ']*$'
442 442 token_match = re.search(pattern, prefix, re.UNICODE)
443 443 token_start = token_match.start()
444 444 token_prefix = token_match.group()
445 445
446 446 # TODO: support bytes in Py3k
447 447 matched = []
448 448 for key in keys:
449 449 try:
450 450 if not key.startswith(prefix_str):
451 451 continue
452 452 except (AttributeError, TypeError, UnicodeError):
453 453 # Python 3+ TypeError on b'a'.startswith('a') or vice-versa
454 454 continue
455 455
456 456 # reformat remainder of key to begin with prefix
457 457 rem = key[len(prefix_str):]
458 458 # force repr wrapped in '
459 459 rem_repr = repr(rem + '"')
460 460 if rem_repr.startswith('u') and prefix[0] not in 'uU':
461 461 # Found key is unicode, but prefix is Py2 string.
462 462 # Therefore attempt to interpret key as string.
463 463 try:
464 464 rem_repr = repr(rem.encode('ascii') + '"')
465 465 except UnicodeEncodeError:
466 466 continue
467 467
468 468 rem_repr = rem_repr[1 + rem_repr.index("'"):-2]
469 469 if quote == '"':
470 470 # The entered prefix is quoted with ",
471 471 # but the match is quoted with '.
472 472 # A contained " hence needs escaping for comparison:
473 473 rem_repr = rem_repr.replace('"', '\\"')
474 474
475 475 # then reinsert prefix from start of token
476 476 matched.append('%s%s' % (token_prefix, rem_repr))
477 477 return quote, token_start, matched
478 478
479 479
480 480 def _safe_isinstance(obj, module, class_name):
481 481 """Checks if obj is an instance of module.class_name if loaded
482 482 """
483 483 return (module in sys.modules and
484 484 isinstance(obj, getattr(import_module(module), class_name)))
485 485
486 486
487 487 def back_unicode_name_matches(text):
488 488 u"""Match unicode characters back to unicode name
489 489
490 490 This does ☃ -> \\snowman
491 491
492 492 Note that snowman is not a valid python3 combining character but will be expanded.
493 493 Though it will not recombine back to the snowman character by the completion machinery.
494 494
495 495 This will not either back-complete standard sequences like \\n, \\b ...
496 496
497 497 Used on Python 3 only.
498 498 """
499 499 if len(text)<2:
500 500 return u'', ()
501 501 maybe_slash = text[-2]
502 502 if maybe_slash != '\\':
503 503 return u'', ()
504 504
505 505 char = text[-1]
506 506 # no expand on quote for completion in strings.
507 507 # nor backcomplete standard ascii keys
508 508 if char in string.ascii_letters or char in ['"',"'"]:
509 509 return u'', ()
510 510 try :
511 511 unic = unicodedata.name(char)
512 512 return '\\'+char,['\\'+unic]
513 513 except KeyError:
514 514 pass
515 515 return u'', ()
516 516
517 517 def back_latex_name_matches(text):
518 518 u"""Match latex characters back to unicode name
519 519
520 520 This does ->\\sqrt
521 521
522 522 Used on Python 3 only.
523 523 """
524 524 if len(text)<2:
525 525 return u'', ()
526 526 maybe_slash = text[-2]
527 527 if maybe_slash != '\\':
528 528 return u'', ()
529 529
530 530
531 531 char = text[-1]
532 532 # no expand on quote for completion in strings.
533 533 # nor backcomplete standard ascii keys
534 534 if char in string.ascii_letters or char in ['"',"'"]:
535 535 return u'', ()
536 536 try :
537 537 latex = reverse_latex_symbol[char]
538 538 # '\\' replace the \ as well
539 539 return '\\'+char,[latex]
540 540 except KeyError:
541 541 pass
542 542 return u'', ()
543 543
544 544
545 545 class IPCompleter(Completer):
546 546 """Extension of the completer class with IPython-specific features"""
547 547
548 548 @observe('greedy')
549 549 def _greedy_changed(self, change):
550 550 """update the splitter and readline delims when greedy is changed"""
551 551 if change['new']:
552 552 self.splitter.delims = GREEDY_DELIMS
553 553 else:
554 554 self.splitter.delims = DELIMS
555 555
556 556 merge_completions = Bool(True,
557 557 help="""Whether to merge completion results into a single list
558 558
559 559 If False, only the completion results from the first non-empty
560 560 completer will be returned.
561 561 """
562 562 ).tag(config=True)
563 563 omit__names = Enum((0,1,2), default_value=2,
564 564 help="""Instruct the completer to omit private method names
565 565
566 566 Specifically, when completing on ``object.<tab>``.
567 567
568 568 When 2 [default]: all names that start with '_' will be excluded.
569 569
570 570 When 1: all 'magic' names (``__foo__``) will be excluded.
571 571
572 572 When 0: nothing will be excluded.
573 573 """
574 574 ).tag(config=True)
575 575 limit_to__all__ = Bool(False,
576 576 help="""
577 577 DEPRECATED as of version 5.0.
578 578
579 579 Instruct the completer to use __all__ for the completion
580 580
581 581 Specifically, when completing on ``object.<tab>``.
582 582
583 583 When True: only those names in obj.__all__ will be included.
584 584
585 585 When False [default]: the __all__ attribute is ignored
586 586 """,
587 587 ).tag(config=True)
588 588
589 589 def __init__(self, shell=None, namespace=None, global_namespace=None,
590 590 use_readline=False, config=None, **kwargs):
591 591 """IPCompleter() -> completer
592 592
593 593 Return a completer object suitable for use by the readline library
594 594 via readline.set_completer().
595 595
596 596 Inputs:
597 597
598 598 - shell: a pointer to the ipython shell itself. This is needed
599 599 because this completer knows about magic functions, and those can
600 600 only be accessed via the ipython instance.
601 601
602 602 - namespace: an optional dict where completions are performed.
603 603
604 604 - global_namespace: secondary optional dict for completions, to
605 605 handle cases (such as IPython embedded inside functions) where
606 606 both Python scopes are visible.
607 607
608 608 use_readline : bool, optional
609 609 DEPRECATED, ignored.
610 610 """
611 611
612 612 self.magic_escape = ESC_MAGIC
613 613 self.splitter = CompletionSplitter()
614 614
615 615 if use_readline:
616 616 warnings.warn('The use_readline parameter is deprecated and ignored since IPython 6.0.',
617 617 DeprecationWarning, stacklevel=2)
618 618
619 619 # _greedy_changed() depends on splitter and readline being defined:
620 620 Completer.__init__(self, namespace=namespace, global_namespace=global_namespace,
621 621 config=config, **kwargs)
622 622
623 623 # List where completion matches will be stored
624 624 self.matches = []
625 625 self.shell = shell
626 626 # Regexp to split filenames with spaces in them
627 627 self.space_name_re = re.compile(r'([^\\] )')
628 628 # Hold a local ref. to glob.glob for speed
629 629 self.glob = glob.glob
630 630
631 631 # Determine if we are running on 'dumb' terminals, like (X)Emacs
632 632 # buffers, to avoid completion problems.
633 633 term = os.environ.get('TERM','xterm')
634 634 self.dumb_terminal = term in ['dumb','emacs']
635 635
636 636 # Special handling of backslashes needed in win32 platforms
637 637 if sys.platform == "win32":
638 638 self.clean_glob = self._clean_glob_win32
639 639 else:
640 640 self.clean_glob = self._clean_glob
641 641
642 642 #regexp to parse docstring for function signature
643 643 self.docstring_sig_re = re.compile(r'^[\w|\s.]+\(([^)]*)\).*')
644 644 self.docstring_kwd_re = re.compile(r'[\s|\[]*(\w+)(?:\s*=\s*.*)')
645 645 #use this if positional argument name is also needed
646 646 #= re.compile(r'[\s|\[]*(\w+)(?:\s*=?\s*.*)')
647 647
648 648 # All active matcher routines for completion
649 649 self.matchers = [
650 650 self.python_matches,
651 651 self.file_matches,
652 652 self.magic_matches,
653 653 self.python_func_kw_matches,
654 654 self.dict_key_matches,
655 655 ]
656 656
657 657 # This is set externally by InteractiveShell
658 658 self.custom_completers = None
659 659
660 660 def all_completions(self, text):
661 661 """
662 662 Wrapper around the complete method for the benefit of emacs.
663 663 """
664 664 return self.complete(text)[1]
665 665
666 666 def _clean_glob(self, text):
667 667 return self.glob("%s*" % text)
668 668
669 669 def _clean_glob_win32(self,text):
670 670 return [f.replace("\\","/")
671 671 for f in self.glob("%s*" % text)]
672 672
673 673 def file_matches(self, text):
674 674 """Match filenames, expanding ~USER type strings.
675 675
676 676 Most of the seemingly convoluted logic in this completer is an
677 677 attempt to handle filenames with spaces in them. And yet it's not
678 678 quite perfect, because Python's readline doesn't expose all of the
679 679 GNU readline details needed for this to be done correctly.
680 680
681 681 For a filename with a space in it, the printed completions will be
682 682 only the parts after what's already been typed (instead of the
683 683 full completions, as is normally done). I don't think with the
684 684 current (as of Python 2.3) Python readline it's possible to do
685 685 better."""
686 686
687 687 # chars that require escaping with backslash - i.e. chars
688 688 # that readline treats incorrectly as delimiters, but we
689 689 # don't want to treat as delimiters in filename matching
690 690 # when escaped with backslash
691 691 if text.startswith('!'):
692 692 text = text[1:]
693 693 text_prefix = u'!'
694 694 else:
695 695 text_prefix = u''
696 696
697 697 text_until_cursor = self.text_until_cursor
698 698 # track strings with open quotes
699 699 open_quotes = has_open_quotes(text_until_cursor)
700 700
701 701 if '(' in text_until_cursor or '[' in text_until_cursor:
702 702 lsplit = text
703 703 else:
704 704 try:
705 705 # arg_split ~ shlex.split, but with unicode bugs fixed by us
706 706 lsplit = arg_split(text_until_cursor)[-1]
707 707 except ValueError:
708 708 # typically an unmatched ", or backslash without escaped char.
709 709 if open_quotes:
710 710 lsplit = text_until_cursor.split(open_quotes)[-1]
711 711 else:
712 712 return []
713 713 except IndexError:
714 714 # tab pressed on empty line
715 715 lsplit = ""
716 716
717 717 if not open_quotes and lsplit != protect_filename(lsplit):
718 718 # if protectables are found, do matching on the whole escaped name
719 719 has_protectables = True
720 720 text0,text = text,lsplit
721 721 else:
722 722 has_protectables = False
723 723 text = os.path.expanduser(text)
724 724
725 725 if text == "":
726 726 return [text_prefix + cast_unicode_py2(protect_filename(f)) for f in self.glob("*")]
727 727
728 728 # Compute the matches from the filesystem
729 729 if sys.platform == 'win32':
730 730 m0 = self.clean_glob(text)
731 731 else:
732 732 m0 = self.clean_glob(text.replace('\\', ''))
733 733
734 734 if has_protectables:
735 735 # If we had protectables, we need to revert our changes to the
736 736 # beginning of filename so that we don't double-write the part
737 737 # of the filename we have so far
738 738 len_lsplit = len(lsplit)
739 739 matches = [text_prefix + text0 +
740 740 protect_filename(f[len_lsplit:]) for f in m0]
741 741 else:
742 742 if open_quotes:
743 743 # if we have a string with an open quote, we don't need to
744 744 # protect the names at all (and we _shouldn't_, as it
745 745 # would cause bugs when the filesystem call is made).
746 746 matches = m0
747 747 else:
748 748 matches = [text_prefix +
749 749 protect_filename(f) for f in m0]
750 750
751 751 # Mark directories in input list by appending '/' to their names.
752 752 return [cast_unicode_py2(x+'/') if os.path.isdir(x) else x for x in matches]
753 753
754 754 def magic_matches(self, text):
755 755 """Match magics"""
756 756 # Get all shell magics now rather than statically, so magics loaded at
757 757 # runtime show up too.
758 758 lsm = self.shell.magics_manager.lsmagic()
759 759 line_magics = lsm['line']
760 760 cell_magics = lsm['cell']
761 761 pre = self.magic_escape
762 762 pre2 = pre+pre
763 763
764 764 # Completion logic:
765 765 # - user gives %%: only do cell magics
766 766 # - user gives %: do both line and cell magics
767 767 # - no prefix: do both
768 768 # In other words, line magics are skipped if the user gives %% explicitly
769 769 bare_text = text.lstrip(pre)
770 770 comp = [ pre2+m for m in cell_magics if m.startswith(bare_text)]
771 771 if not text.startswith(pre2):
772 772 comp += [ pre+m for m in line_magics if m.startswith(bare_text)]
773 773 return [cast_unicode_py2(c) for c in comp]
774 774
775 775
776 776 def python_matches(self, text):
777 777 """Match attributes or global python names"""
778 778 if "." in text:
779 779 try:
780 780 matches = self.attr_matches(text)
781 781 if text.endswith('.') and self.omit__names:
782 782 if self.omit__names == 1:
783 783 # true if txt is _not_ a __ name, false otherwise:
784 784 no__name = (lambda txt:
785 785 re.match(r'.*\.__.*?__',txt) is None)
786 786 else:
787 787 # true if txt is _not_ a _ name, false otherwise:
788 788 no__name = (lambda txt:
789 789 re.match(r'\._.*?',txt[txt.rindex('.'):]) is None)
790 790 matches = filter(no__name, matches)
791 791 except NameError:
792 792 # catches <undefined attributes>.<tab>
793 793 matches = []
794 794 else:
795 795 matches = self.global_matches(text)
796 796 return matches
797 797
798 798 def _default_arguments_from_docstring(self, doc):
799 799 """Parse the first line of docstring for call signature.
800 800
801 801 Docstring should be of the form 'min(iterable[, key=func])\n'.
802 802 It can also parse cython docstring of the form
803 803 'Minuit.migrad(self, int ncall=10000, resume=True, int nsplit=1)'.
804 804 """
805 805 if doc is None:
806 806 return []
807 807
808 808 #care only the firstline
809 809 line = doc.lstrip().splitlines()[0]
810 810
811 811 #p = re.compile(r'^[\w|\s.]+\(([^)]*)\).*')
812 812 #'min(iterable[, key=func])\n' -> 'iterable[, key=func]'
813 813 sig = self.docstring_sig_re.search(line)
814 814 if sig is None:
815 815 return []
816 816 # iterable[, key=func]' -> ['iterable[' ,' key=func]']
817 817 sig = sig.groups()[0].split(',')
818 818 ret = []
819 819 for s in sig:
820 820 #re.compile(r'[\s|\[]*(\w+)(?:\s*=\s*.*)')
821 821 ret += self.docstring_kwd_re.findall(s)
822 822 return ret
823 823
824 824 def _default_arguments(self, obj):
825 825 """Return the list of default arguments of obj if it is callable,
826 826 or empty list otherwise."""
827 827 call_obj = obj
828 828 ret = []
829 829 if inspect.isbuiltin(obj):
830 830 pass
831 831 elif not (inspect.isfunction(obj) or inspect.ismethod(obj)):
832 832 if inspect.isclass(obj):
833 833 #for cython embededsignature=True the constructor docstring
834 834 #belongs to the object itself not __init__
835 835 ret += self._default_arguments_from_docstring(
836 836 getattr(obj, '__doc__', ''))
837 837 # for classes, check for __init__,__new__
838 838 call_obj = (getattr(obj, '__init__', None) or
839 839 getattr(obj, '__new__', None))
840 840 # for all others, check if they are __call__able
841 841 elif hasattr(obj, '__call__'):
842 842 call_obj = obj.__call__
843 843 ret += self._default_arguments_from_docstring(
844 844 getattr(call_obj, '__doc__', ''))
845 845
846 846 _keeps = (inspect.Parameter.KEYWORD_ONLY,
847 847 inspect.Parameter.POSITIONAL_OR_KEYWORD)
848 848
849 849 try:
850 850 sig = inspect.signature(call_obj)
851 851 ret.extend(k for k, v in sig.parameters.items() if
852 852 v.kind in _keeps)
853 853 except ValueError:
854 854 pass
855 855
856 856 return list(set(ret))
857 857
858 858 def python_func_kw_matches(self,text):
859 859 """Match named parameters (kwargs) of the last open function"""
860 860
861 861 if "." in text: # a parameter cannot be dotted
862 862 return []
863 863 try: regexp = self.__funcParamsRegex
864 864 except AttributeError:
865 865 regexp = self.__funcParamsRegex = re.compile(r'''
866 866 '.*?(?<!\\)' | # single quoted strings or
867 867 ".*?(?<!\\)" | # double quoted strings or
868 868 \w+ | # identifier
869 869 \S # other characters
870 870 ''', re.VERBOSE | re.DOTALL)
871 871 # 1. find the nearest identifier that comes before an unclosed
872 872 # parenthesis before the cursor
873 873 # e.g. for "foo (1+bar(x), pa<cursor>,a=1)", the candidate is "foo"
874 874 tokens = regexp.findall(self.text_until_cursor)
875 875 iterTokens = reversed(tokens); openPar = 0
876 876
877 877 for token in iterTokens:
878 878 if token == ')':
879 879 openPar -= 1
880 880 elif token == '(':
881 881 openPar += 1
882 882 if openPar > 0:
883 883 # found the last unclosed parenthesis
884 884 break
885 885 else:
886 886 return []
887 887 # 2. Concatenate dotted names ("foo.bar" for "foo.bar(x, pa" )
888 888 ids = []
889 889 isId = re.compile(r'\w+$').match
890 890
891 891 while True:
892 892 try:
893 893 ids.append(next(iterTokens))
894 894 if not isId(ids[-1]):
895 895 ids.pop(); break
896 896 if not next(iterTokens) == '.':
897 897 break
898 898 except StopIteration:
899 899 break
900 900
901 901 # Find all named arguments already assigned to, as to avoid suggesting
902 902 # them again
903 903 usedNamedArgs = set()
904 904 par_level = -1
905 905 for token, next_token in zip(tokens, tokens[1:]):
906 906 if token == '(':
907 907 par_level += 1
908 908 elif token == ')':
909 909 par_level -= 1
910 910
911 911 if par_level != 0:
912 912 continue
913 913
914 914 if next_token != '=':
915 915 continue
916 916
917 917 usedNamedArgs.add(token)
918 918
919 919 # lookup the candidate callable matches either using global_matches
920 920 # or attr_matches for dotted names
921 921 if len(ids) == 1:
922 922 callableMatches = self.global_matches(ids[0])
923 923 else:
924 924 callableMatches = self.attr_matches('.'.join(ids[::-1]))
925 925 argMatches = []
926 926 for callableMatch in callableMatches:
927 927 try:
928 928 namedArgs = self._default_arguments(eval(callableMatch,
929 929 self.namespace))
930 930 except:
931 931 continue
932 932
933 933 # Remove used named arguments from the list, no need to show twice
934 934 for namedArg in set(namedArgs) - usedNamedArgs:
935 935 if namedArg.startswith(text):
936 936 argMatches.append(u"%s=" %namedArg)
937 937 return argMatches
938 938
939 939 def dict_key_matches(self, text):
940 940 "Match string keys in a dictionary, after e.g. 'foo[' "
941 941 def get_keys(obj):
942 942 # Objects can define their own completions by defining an
943 943 # _ipy_key_completions_() method.
944 944 method = get_real_method(obj, '_ipython_key_completions_')
945 945 if method is not None:
946 946 return method()
947 947
948 948 # Special case some common in-memory dict-like types
949 949 if isinstance(obj, dict) or\
950 950 _safe_isinstance(obj, 'pandas', 'DataFrame'):
951 951 try:
952 952 return list(obj.keys())
953 953 except Exception:
954 954 return []
955 955 elif _safe_isinstance(obj, 'numpy', 'ndarray') or\
956 956 _safe_isinstance(obj, 'numpy', 'void'):
957 957 return obj.dtype.names or []
958 958 return []
959 959
960 960 try:
961 961 regexps = self.__dict_key_regexps
962 962 except AttributeError:
963 963 dict_key_re_fmt = r'''(?x)
964 964 ( # match dict-referring expression wrt greedy setting
965 965 %s
966 966 )
967 967 \[ # open bracket
968 968 \s* # and optional whitespace
969 969 ([uUbB]? # string prefix (r not handled)
970 970 (?: # unclosed string
971 971 '(?:[^']|(?<!\\)\\')*
972 972 |
973 973 "(?:[^"]|(?<!\\)\\")*
974 974 )
975 975 )?
976 976 $
977 977 '''
978 978 regexps = self.__dict_key_regexps = {
979 979 False: re.compile(dict_key_re_fmt % '''
980 980 # identifiers separated by .
981 981 (?!\d)\w+
982 982 (?:\.(?!\d)\w+)*
983 983 '''),
984 984 True: re.compile(dict_key_re_fmt % '''
985 985 .+
986 986 ''')
987 987 }
988 988
989 989 match = regexps[self.greedy].search(self.text_until_cursor)
990 990 if match is None:
991 991 return []
992 992
993 993 expr, prefix = match.groups()
994 994 try:
995 995 obj = eval(expr, self.namespace)
996 996 except Exception:
997 997 try:
998 998 obj = eval(expr, self.global_namespace)
999 999 except Exception:
1000 1000 return []
1001 1001
1002 1002 keys = get_keys(obj)
1003 1003 if not keys:
1004 1004 return keys
1005 1005 closing_quote, token_offset, matches = match_dict_keys(keys, prefix, self.splitter.delims)
1006 1006 if not matches:
1007 1007 return matches
1008 1008
1009 1009 # get the cursor position of
1010 1010 # - the text being completed
1011 1011 # - the start of the key text
1012 1012 # - the start of the completion
1013 1013 text_start = len(self.text_until_cursor) - len(text)
1014 1014 if prefix:
1015 1015 key_start = match.start(2)
1016 1016 completion_start = key_start + token_offset
1017 1017 else:
1018 1018 key_start = completion_start = match.end()
1019 1019
1020 1020 # grab the leading prefix, to make sure all completions start with `text`
1021 1021 if text_start > key_start:
1022 1022 leading = ''
1023 1023 else:
1024 1024 leading = text[text_start:completion_start]
1025 1025
1026 1026 # the index of the `[` character
1027 1027 bracket_idx = match.end(1)
1028 1028
1029 1029 # append closing quote and bracket as appropriate
1030 1030 # this is *not* appropriate if the opening quote or bracket is outside
1031 1031 # the text given to this method
1032 1032 suf = ''
1033 1033 continuation = self.line_buffer[len(self.text_until_cursor):]
1034 1034 if key_start > text_start and closing_quote:
1035 1035 # quotes were opened inside text, maybe close them
1036 1036 if continuation.startswith(closing_quote):
1037 1037 continuation = continuation[len(closing_quote):]
1038 1038 else:
1039 1039 suf += closing_quote
1040 1040 if bracket_idx > text_start:
1041 1041 # brackets were opened inside text, maybe close them
1042 1042 if not continuation.startswith(']'):
1043 1043 suf += ']'
1044 1044
1045 1045 return [leading + k + suf for k in matches]
1046 1046
1047 1047 def unicode_name_matches(self, text):
1048 1048 u"""Match Latex-like syntax for unicode characters base
1049 1049 on the name of the character.
1050 1050
1051 1051 This does \\GREEK SMALL LETTER ETA -> η
1052 1052
1053 1053 Works only on valid python 3 identifier, or on combining characters that
1054 1054 will combine to form a valid identifier.
1055 1055
1056 1056 Used on Python 3 only.
1057 1057 """
1058 1058 slashpos = text.rfind('\\')
1059 1059 if slashpos > -1:
1060 1060 s = text[slashpos+1:]
1061 1061 try :
1062 1062 unic = unicodedata.lookup(s)
1063 1063 # allow combining chars
1064 1064 if ('a'+unic).isidentifier():
1065 1065 return '\\'+s,[unic]
1066 1066 except KeyError:
1067 1067 pass
1068 1068 return u'', []
1069 1069
1070 1070
1071 1071
1072 1072
1073 1073 def latex_matches(self, text):
1074 1074 u"""Match Latex syntax for unicode characters.
1075 1075
1076 1076 This does both \\alp -> \\alpha and \\alpha -> α
1077 1077
1078 1078 Used on Python 3 only.
1079 1079 """
1080 1080 slashpos = text.rfind('\\')
1081 1081 if slashpos > -1:
1082 1082 s = text[slashpos:]
1083 1083 if s in latex_symbols:
1084 1084 # Try to complete a full latex symbol to unicode
1085 1085 # \\alpha -> α
1086 1086 return s, [latex_symbols[s]]
1087 1087 else:
1088 1088 # If a user has partially typed a latex symbol, give them
1089 1089 # a full list of options \al -> [\aleph, \alpha]
1090 1090 matches = [k for k in latex_symbols if k.startswith(s)]
1091 1091 return s, matches
1092 1092 return u'', []
1093 1093
1094 1094 def dispatch_custom_completer(self, text):
1095 1095 if not self.custom_completers:
1096 1096 return
1097 1097
1098 1098 line = self.line_buffer
1099 1099 if not line.strip():
1100 1100 return None
1101 1101
1102 1102 # Create a little structure to pass all the relevant information about
1103 1103 # the current completion to any custom completer.
1104 1104 event = Bunch()
1105 1105 event.line = line
1106 1106 event.symbol = text
1107 1107 cmd = line.split(None,1)[0]
1108 1108 event.command = cmd
1109 1109 event.text_until_cursor = self.text_until_cursor
1110 1110
1111 1111 # for foo etc, try also to find completer for %foo
1112 1112 if not cmd.startswith(self.magic_escape):
1113 1113 try_magic = self.custom_completers.s_matches(
1114 1114 self.magic_escape + cmd)
1115 1115 else:
1116 1116 try_magic = []
1117 1117
1118 1118 for c in itertools.chain(self.custom_completers.s_matches(cmd),
1119 1119 try_magic,
1120 1120 self.custom_completers.flat_matches(self.text_until_cursor)):
1121 1121 try:
1122 1122 res = c(event)
1123 1123 if res:
1124 1124 # first, try case sensitive match
1125 1125 withcase = [cast_unicode_py2(r) for r in res if r.startswith(text)]
1126 1126 if withcase:
1127 1127 return withcase
1128 1128 # if none, then case insensitive ones are ok too
1129 1129 text_low = text.lower()
1130 1130 return [cast_unicode_py2(r) for r in res if r.lower().startswith(text_low)]
1131 1131 except TryNext:
1132 1132 pass
1133 1133
1134 1134 return None
1135 1135
1136 1136 @_strip_single_trailing_space
1137 1137 def complete(self, text=None, line_buffer=None, cursor_pos=None):
1138 1138 """Find completions for the given text and line context.
1139 1139
1140 1140 Note that both the text and the line_buffer are optional, but at least
1141 1141 one of them must be given.
1142 1142
1143 1143 Parameters
1144 1144 ----------
1145 1145 text : string, optional
1146 1146 Text to perform the completion on. If not given, the line buffer
1147 1147 is split using the instance's CompletionSplitter object.
1148 1148
1149 1149 line_buffer : string, optional
1150 1150 If not given, the completer attempts to obtain the current line
1151 1151 buffer via readline. This keyword allows clients which are
1152 1152 requesting for text completions in non-readline contexts to inform
1153 1153 the completer of the entire text.
1154 1154
1155 1155 cursor_pos : int, optional
1156 1156 Index of the cursor in the full line buffer. Should be provided by
1157 1157 remote frontends where kernel has no access to frontend state.
1158 1158
1159 1159 Returns
1160 1160 -------
1161 1161 text : str
1162 1162 Text that was actually used in the completion.
1163 1163
1164 1164 matches : list
1165 1165 A list of completion matches.
1166 1166 """
1167 1167 # if the cursor position isn't given, the only sane assumption we can
1168 1168 # make is that it's at the end of the line (the common case)
1169 1169 if cursor_pos is None:
1170 1170 cursor_pos = len(line_buffer) if text is None else len(text)
1171 1171
1172 1172 if self.use_main_ns:
1173 1173 self.namespace = __main__.__dict__
1174 1174
1175 1175 if PY3:
1176 1176
1177 1177 base_text = text if not line_buffer else line_buffer[:cursor_pos]
1178 1178 latex_text, latex_matches = self.latex_matches(base_text)
1179 1179 if latex_matches:
1180 1180 return latex_text, latex_matches
1181 1181 name_text = ''
1182 1182 name_matches = []
1183 1183 for meth in (self.unicode_name_matches, back_latex_name_matches, back_unicode_name_matches):
1184 1184 name_text, name_matches = meth(base_text)
1185 1185 if name_text:
1186 1186 return name_text, name_matches
1187 1187
1188 1188 # if text is either None or an empty string, rely on the line buffer
1189 1189 if not text:
1190 1190 text = self.splitter.split_line(line_buffer, cursor_pos)
1191 1191
1192 1192 # If no line buffer is given, assume the input text is all there was
1193 1193 if line_buffer is None:
1194 1194 line_buffer = text
1195 1195
1196 1196 self.line_buffer = line_buffer
1197 1197 self.text_until_cursor = self.line_buffer[:cursor_pos]
1198 1198
1199 1199 # Start with a clean slate of completions
1200 1200 self.matches[:] = []
1201 1201 custom_res = self.dispatch_custom_completer(text)
1202 1202 if custom_res is not None:
1203 1203 # did custom completers produce something?
1204 1204 self.matches = custom_res
1205 1205 else:
1206 1206 # Extend the list of completions with the results of each
1207 1207 # matcher, so we return results to the user from all
1208 1208 # namespaces.
1209 1209 if self.merge_completions:
1210 1210 self.matches = []
1211 1211 for matcher in self.matchers:
1212 1212 try:
1213 1213 self.matches.extend(matcher(text))
1214 1214 except:
1215 1215 # Show the ugly traceback if the matcher causes an
1216 1216 # exception, but do NOT crash the kernel!
1217 1217 sys.excepthook(*sys.exc_info())
1218 1218 else:
1219 1219 for matcher in self.matchers:
1220 1220 self.matches = matcher(text)
1221 1221 if self.matches:
1222 1222 break
1223 1223 # FIXME: we should extend our api to return a dict with completions for
1224 1224 # different types of objects. The rlcomplete() method could then
1225 1225 # simply collapse the dict into a list for readline, but we'd have
1226 1226 # richer completion semantics in other evironments.
1227 1227 self.matches = sorted(set(self.matches), key=completions_sorting_key)
1228 1228
1229 1229 return text, self.matches
@@ -1,348 +1,347 b''
1 1 # encoding: utf-8
2 2 """Implementations for various useful completers.
3 3
4 4 These are all loaded by default by IPython.
5 5 """
6 6 #-----------------------------------------------------------------------------
7 7 # Copyright (C) 2010-2011 The IPython Development Team.
8 8 #
9 9 # Distributed under the terms of the BSD License.
10 10 #
11 11 # The full license is in the file COPYING.txt, distributed with this software.
12 12 #-----------------------------------------------------------------------------
13 13
14 14 #-----------------------------------------------------------------------------
15 15 # Imports
16 16 #-----------------------------------------------------------------------------
17 17
18 18 # Stdlib imports
19 19 import glob
20 20 import inspect
21 21 import os
22 22 import re
23 23 import sys
24 24 from importlib import import_module
25 25
26 26 try:
27 27 # Python >= 3.3
28 28 from importlib.machinery import all_suffixes
29 29 _suffixes = all_suffixes()
30 30 except ImportError:
31 31 from imp import get_suffixes
32 32 _suffixes = [ s[0] for s in get_suffixes() ]
33 33
34 34 # Third-party imports
35 35 from time import time
36 36 from zipimport import zipimporter
37 37
38 38 # Our own imports
39 39 from IPython.core.completer import expand_user, compress_user
40 40 from IPython.core.error import TryNext
41 41 from IPython.utils._process_common import arg_split
42 from IPython.utils.py3compat import string_types
43 42
44 43 # FIXME: this should be pulled in with the right call via the component system
45 44 from IPython import get_ipython
46 45
47 46 #-----------------------------------------------------------------------------
48 47 # Globals and constants
49 48 #-----------------------------------------------------------------------------
50 49
51 50 # Time in seconds after which the rootmodules will be stored permanently in the
52 51 # ipython ip.db database (kept in the user's .ipython dir).
53 52 TIMEOUT_STORAGE = 2
54 53
55 54 # Time in seconds after which we give up
56 55 TIMEOUT_GIVEUP = 20
57 56
58 57 # Regular expression for the python import statement
59 58 import_re = re.compile(r'(?P<name>[a-zA-Z_][a-zA-Z0-9_]*?)'
60 59 r'(?P<package>[/\\]__init__)?'
61 60 r'(?P<suffix>%s)$' %
62 61 r'|'.join(re.escape(s) for s in _suffixes))
63 62
64 63 # RE for the ipython %run command (python + ipython scripts)
65 64 magic_run_re = re.compile(r'.*(\.ipy|\.ipynb|\.py[w]?)$')
66 65
67 66 #-----------------------------------------------------------------------------
68 67 # Local utilities
69 68 #-----------------------------------------------------------------------------
70 69
71 70 def module_list(path):
72 71 """
73 72 Return the list containing the names of the modules available in the given
74 73 folder.
75 74 """
76 75 # sys.path has the cwd as an empty string, but isdir/listdir need it as '.'
77 76 if path == '':
78 77 path = '.'
79 78
80 79 # A few local constants to be used in loops below
81 80 pjoin = os.path.join
82 81
83 82 if os.path.isdir(path):
84 83 # Build a list of all files in the directory and all files
85 84 # in its subdirectories. For performance reasons, do not
86 85 # recurse more than one level into subdirectories.
87 86 files = []
88 87 for root, dirs, nondirs in os.walk(path, followlinks=True):
89 88 subdir = root[len(path)+1:]
90 89 if subdir:
91 90 files.extend(pjoin(subdir, f) for f in nondirs)
92 91 dirs[:] = [] # Do not recurse into additional subdirectories.
93 92 else:
94 93 files.extend(nondirs)
95 94
96 95 else:
97 96 try:
98 97 files = list(zipimporter(path)._files.keys())
99 98 except:
100 99 files = []
101 100
102 101 # Build a list of modules which match the import_re regex.
103 102 modules = []
104 103 for f in files:
105 104 m = import_re.match(f)
106 105 if m:
107 106 modules.append(m.group('name'))
108 107 return list(set(modules))
109 108
110 109
111 110 def get_root_modules():
112 111 """
113 112 Returns a list containing the names of all the modules available in the
114 113 folders of the pythonpath.
115 114
116 115 ip.db['rootmodules_cache'] maps sys.path entries to list of modules.
117 116 """
118 117 ip = get_ipython()
119 118 rootmodules_cache = ip.db.get('rootmodules_cache', {})
120 119 rootmodules = list(sys.builtin_module_names)
121 120 start_time = time()
122 121 store = False
123 122 for path in sys.path:
124 123 try:
125 124 modules = rootmodules_cache[path]
126 125 except KeyError:
127 126 modules = module_list(path)
128 127 try:
129 128 modules.remove('__init__')
130 129 except ValueError:
131 130 pass
132 131 if path not in ('', '.'): # cwd modules should not be cached
133 132 rootmodules_cache[path] = modules
134 133 if time() - start_time > TIMEOUT_STORAGE and not store:
135 134 store = True
136 135 print("\nCaching the list of root modules, please wait!")
137 136 print("(This will only be done once - type '%rehashx' to "
138 137 "reset cache!)\n")
139 138 sys.stdout.flush()
140 139 if time() - start_time > TIMEOUT_GIVEUP:
141 140 print("This is taking too long, we give up.\n")
142 141 return []
143 142 rootmodules.extend(modules)
144 143 if store:
145 144 ip.db['rootmodules_cache'] = rootmodules_cache
146 145 rootmodules = list(set(rootmodules))
147 146 return rootmodules
148 147
149 148
150 149 def is_importable(module, attr, only_modules):
151 150 if only_modules:
152 151 return inspect.ismodule(getattr(module, attr))
153 152 else:
154 153 return not(attr[:2] == '__' and attr[-2:] == '__')
155 154
156 155 def try_import(mod, only_modules=False):
157 156 try:
158 157 m = import_module(mod)
159 158 except:
160 159 return []
161 160
162 161 m_is_init = hasattr(m, '__file__') and '__init__' in m.__file__
163 162
164 163 completions = []
165 164 if (not hasattr(m, '__file__')) or (not only_modules) or m_is_init:
166 165 completions.extend( [attr for attr in dir(m) if
167 166 is_importable(m, attr, only_modules)])
168 167
169 168 completions.extend(getattr(m, '__all__', []))
170 169 if m_is_init:
171 170 completions.extend(module_list(os.path.dirname(m.__file__)))
172 completions = {c for c in completions if isinstance(c, string_types)}
171 completions = {c for c in completions if isinstance(c, str)}
173 172 completions.discard('__init__')
174 173 return list(completions)
175 174
176 175
177 176 #-----------------------------------------------------------------------------
178 177 # Completion-related functions.
179 178 #-----------------------------------------------------------------------------
180 179
181 180 def quick_completer(cmd, completions):
182 181 """ Easily create a trivial completer for a command.
183 182
184 183 Takes either a list of completions, or all completions in string (that will
185 184 be split on whitespace).
186 185
187 186 Example::
188 187
189 188 [d:\ipython]|1> import ipy_completers
190 189 [d:\ipython]|2> ipy_completers.quick_completer('foo', ['bar','baz'])
191 190 [d:\ipython]|3> foo b<TAB>
192 191 bar baz
193 192 [d:\ipython]|3> foo ba
194 193 """
195 194
196 if isinstance(completions, string_types):
195 if isinstance(completions, str):
197 196 completions = completions.split()
198 197
199 198 def do_complete(self, event):
200 199 return completions
201 200
202 201 get_ipython().set_hook('complete_command',do_complete, str_key = cmd)
203 202
204 203 def module_completion(line):
205 204 """
206 205 Returns a list containing the completion possibilities for an import line.
207 206
208 207 The line looks like this :
209 208 'import xml.d'
210 209 'from xml.dom import'
211 210 """
212 211
213 212 words = line.split(' ')
214 213 nwords = len(words)
215 214
216 215 # from whatever <tab> -> 'import '
217 216 if nwords == 3 and words[0] == 'from':
218 217 return ['import ']
219 218
220 219 # 'from xy<tab>' or 'import xy<tab>'
221 220 if nwords < 3 and (words[0] in {'%aimport', 'import', 'from'}) :
222 221 if nwords == 1:
223 222 return get_root_modules()
224 223 mod = words[1].split('.')
225 224 if len(mod) < 2:
226 225 return get_root_modules()
227 226 completion_list = try_import('.'.join(mod[:-1]), True)
228 227 return ['.'.join(mod[:-1] + [el]) for el in completion_list]
229 228
230 229 # 'from xyz import abc<tab>'
231 230 if nwords >= 3 and words[0] == 'from':
232 231 mod = words[1]
233 232 return try_import(mod)
234 233
235 234 #-----------------------------------------------------------------------------
236 235 # Completers
237 236 #-----------------------------------------------------------------------------
238 237 # These all have the func(self, event) signature to be used as custom
239 238 # completers
240 239
241 240 def module_completer(self,event):
242 241 """Give completions after user has typed 'import ...' or 'from ...'"""
243 242
244 243 # This works in all versions of python. While 2.5 has
245 244 # pkgutil.walk_packages(), that particular routine is fairly dangerous,
246 245 # since it imports *EVERYTHING* on sys.path. That is: a) very slow b) full
247 246 # of possibly problematic side effects.
248 247 # This search the folders in the sys.path for available modules.
249 248
250 249 return module_completion(event.line)
251 250
252 251 # FIXME: there's a lot of logic common to the run, cd and builtin file
253 252 # completers, that is currently reimplemented in each.
254 253
255 254 def magic_run_completer(self, event):
256 255 """Complete files that end in .py or .ipy or .ipynb for the %run command.
257 256 """
258 257 comps = arg_split(event.line, strict=False)
259 258 # relpath should be the current token that we need to complete.
260 259 if (len(comps) > 1) and (not event.line.endswith(' ')):
261 260 relpath = comps[-1].strip("'\"")
262 261 else:
263 262 relpath = ''
264 263
265 264 #print("\nev=", event) # dbg
266 265 #print("rp=", relpath) # dbg
267 266 #print('comps=', comps) # dbg
268 267
269 268 lglob = glob.glob
270 269 isdir = os.path.isdir
271 270 relpath, tilde_expand, tilde_val = expand_user(relpath)
272 271
273 272 # Find if the user has already typed the first filename, after which we
274 273 # should complete on all files, since after the first one other files may
275 274 # be arguments to the input script.
276 275
277 276 if any(magic_run_re.match(c) for c in comps):
278 277 matches = [f.replace('\\','/') + ('/' if isdir(f) else '')
279 278 for f in lglob(relpath+'*')]
280 279 else:
281 280 dirs = [f.replace('\\','/') + "/" for f in lglob(relpath+'*') if isdir(f)]
282 281 pys = [f.replace('\\','/')
283 282 for f in lglob(relpath+'*.py') + lglob(relpath+'*.ipy') +
284 283 lglob(relpath+'*.ipynb') + lglob(relpath + '*.pyw')]
285 284
286 285 matches = dirs + pys
287 286
288 287 #print('run comp:', dirs+pys) # dbg
289 288 return [compress_user(p, tilde_expand, tilde_val) for p in matches]
290 289
291 290
292 291 def cd_completer(self, event):
293 292 """Completer function for cd, which only returns directories."""
294 293 ip = get_ipython()
295 294 relpath = event.symbol
296 295
297 296 #print(event) # dbg
298 297 if event.line.endswith('-b') or ' -b ' in event.line:
299 298 # return only bookmark completions
300 299 bkms = self.db.get('bookmarks', None)
301 300 if bkms:
302 301 return bkms.keys()
303 302 else:
304 303 return []
305 304
306 305 if event.symbol == '-':
307 306 width_dh = str(len(str(len(ip.user_ns['_dh']) + 1)))
308 307 # jump in directory history by number
309 308 fmt = '-%0' + width_dh +'d [%s]'
310 309 ents = [ fmt % (i,s) for i,s in enumerate(ip.user_ns['_dh'])]
311 310 if len(ents) > 1:
312 311 return ents
313 312 return []
314 313
315 314 if event.symbol.startswith('--'):
316 315 return ["--" + os.path.basename(d) for d in ip.user_ns['_dh']]
317 316
318 317 # Expand ~ in path and normalize directory separators.
319 318 relpath, tilde_expand, tilde_val = expand_user(relpath)
320 319 relpath = relpath.replace('\\','/')
321 320
322 321 found = []
323 322 for d in [f.replace('\\','/') + '/' for f in glob.glob(relpath+'*')
324 323 if os.path.isdir(f)]:
325 324 if ' ' in d:
326 325 # we don't want to deal with any of that, complex code
327 326 # for this is elsewhere
328 327 raise TryNext
329 328
330 329 found.append(d)
331 330
332 331 if not found:
333 332 if os.path.isdir(relpath):
334 333 return [compress_user(relpath, tilde_expand, tilde_val)]
335 334
336 335 # if no completions so far, try bookmarks
337 336 bks = self.db.get('bookmarks',{})
338 337 bkmatches = [s for s in bks if s.startswith(event.symbol)]
339 338 if bkmatches:
340 339 return bkmatches
341 340
342 341 raise TryNext
343 342
344 343 return [compress_user(p, tilde_expand, tilde_val) for p in found]
345 344
346 345 def reset_completer(self, event):
347 346 "A completer for %reset magic"
348 347 return '-f -s in out array dhist'.split()
@@ -1,1136 +1,1135 b''
1 1 # -*- coding: utf-8 -*-
2 2 """Top-level display functions for displaying object in different formats."""
3 3
4 4 # Copyright (c) IPython Development Team.
5 5 # Distributed under the terms of the Modified BSD License.
6 6
7 7
8 8 try:
9 9 from base64 import encodebytes as base64_encode
10 10 except ImportError:
11 11 from base64 import encodestring as base64_encode
12 12
13 13 from binascii import b2a_hex
14 14 import json
15 15 import mimetypes
16 16 import os
17 17 import struct
18 18 import sys
19 19 import warnings
20 20
21 from IPython.utils.py3compat import (string_types, cast_bytes_py2, cast_unicode,
22 unicode_type)
21 from IPython.utils.py3compat import cast_bytes_py2, cast_unicode, unicode_type
23 22 from IPython.testing.skipdoctest import skip_doctest
24 23
25 24 __all__ = ['display', 'display_pretty', 'display_html', 'display_markdown',
26 25 'display_svg', 'display_png', 'display_jpeg', 'display_latex', 'display_json',
27 26 'display_javascript', 'display_pdf', 'DisplayObject', 'TextDisplayObject',
28 27 'Pretty', 'HTML', 'Markdown', 'Math', 'Latex', 'SVG', 'JSON', 'Javascript',
29 28 'Image', 'clear_output', 'set_matplotlib_formats', 'set_matplotlib_close',
30 29 'publish_display_data', 'update_display', 'DisplayHandle']
31 30
32 31 #-----------------------------------------------------------------------------
33 32 # utility functions
34 33 #-----------------------------------------------------------------------------
35 34
36 35 def _safe_exists(path):
37 36 """Check path, but don't let exceptions raise"""
38 37 try:
39 38 return os.path.exists(path)
40 39 except Exception:
41 40 return False
42 41
43 42 def _merge(d1, d2):
44 43 """Like update, but merges sub-dicts instead of clobbering at the top level.
45 44
46 45 Updates d1 in-place
47 46 """
48 47
49 48 if not isinstance(d2, dict) or not isinstance(d1, dict):
50 49 return d2
51 50 for key, value in d2.items():
52 51 d1[key] = _merge(d1.get(key), value)
53 52 return d1
54 53
55 54 def _display_mimetype(mimetype, objs, raw=False, metadata=None):
56 55 """internal implementation of all display_foo methods
57 56
58 57 Parameters
59 58 ----------
60 59 mimetype : str
61 60 The mimetype to be published (e.g. 'image/png')
62 61 objs : tuple of objects
63 62 The Python objects to display, or if raw=True raw text data to
64 63 display.
65 64 raw : bool
66 65 Are the data objects raw data or Python objects that need to be
67 66 formatted before display? [default: False]
68 67 metadata : dict (optional)
69 68 Metadata to be associated with the specific mimetype output.
70 69 """
71 70 if metadata:
72 71 metadata = {mimetype: metadata}
73 72 if raw:
74 73 # turn list of pngdata into list of { 'image/png': pngdata }
75 74 objs = [ {mimetype: obj} for obj in objs ]
76 75 display(*objs, raw=raw, metadata=metadata, include=[mimetype])
77 76
78 77 #-----------------------------------------------------------------------------
79 78 # Main functions
80 79 #-----------------------------------------------------------------------------
81 80
82 81 # use * to indicate transient is keyword-only
83 82 def publish_display_data(data, metadata=None, source=None, *, transient=None, **kwargs):
84 83 """Publish data and metadata to all frontends.
85 84
86 85 See the ``display_data`` message in the messaging documentation for
87 86 more details about this message type.
88 87
89 88 The following MIME types are currently implemented:
90 89
91 90 * text/plain
92 91 * text/html
93 92 * text/markdown
94 93 * text/latex
95 94 * application/json
96 95 * application/javascript
97 96 * image/png
98 97 * image/jpeg
99 98 * image/svg+xml
100 99
101 100 Parameters
102 101 ----------
103 102 data : dict
104 103 A dictionary having keys that are valid MIME types (like
105 104 'text/plain' or 'image/svg+xml') and values that are the data for
106 105 that MIME type. The data itself must be a JSON'able data
107 106 structure. Minimally all data should have the 'text/plain' data,
108 107 which can be displayed by all frontends. If more than the plain
109 108 text is given, it is up to the frontend to decide which
110 109 representation to use.
111 110 metadata : dict
112 111 A dictionary for metadata related to the data. This can contain
113 112 arbitrary key, value pairs that frontends can use to interpret
114 113 the data. mime-type keys matching those in data can be used
115 114 to specify metadata about particular representations.
116 115 source : str, deprecated
117 116 Unused.
118 117 transient : dict, keyword-only
119 118 A dictionary of transient data, such as display_id.
120 119 """
121 120 from IPython.core.interactiveshell import InteractiveShell
122 121
123 122 display_pub = InteractiveShell.instance().display_pub
124 123
125 124 # only pass transient if supplied,
126 125 # to avoid errors with older ipykernel.
127 126 # TODO: We could check for ipykernel version and provide a detailed upgrade message.
128 127 if transient:
129 128 kwargs['transient'] = transient
130 129
131 130 display_pub.publish(
132 131 data=data,
133 132 metadata=metadata,
134 133 **kwargs
135 134 )
136 135
137 136
138 137 def _new_id():
139 138 """Generate a new random text id with urandom"""
140 139 return b2a_hex(os.urandom(16)).decode('ascii')
141 140
142 141
143 142 def display(*objs, include=None, exclude=None, metadata=None, transient=None, display_id=None, **kwargs):
144 143 """Display a Python object in all frontends.
145 144
146 145 By default all representations will be computed and sent to the frontends.
147 146 Frontends can decide which representation is used and how.
148 147
149 148 Parameters
150 149 ----------
151 150 objs : tuple of objects
152 151 The Python objects to display.
153 152 raw : bool, optional
154 153 Are the objects to be displayed already mimetype-keyed dicts of raw display data,
155 154 or Python objects that need to be formatted before display? [default: False]
156 155 include : list or tuple, optional
157 156 A list of format type strings (MIME types) to include in the
158 157 format data dict. If this is set *only* the format types included
159 158 in this list will be computed.
160 159 exclude : list or tuple, optional
161 160 A list of format type strings (MIME types) to exclude in the format
162 161 data dict. If this is set all format types will be computed,
163 162 except for those included in this argument.
164 163 metadata : dict, optional
165 164 A dictionary of metadata to associate with the output.
166 165 mime-type keys in this dictionary will be associated with the individual
167 166 representation formats, if they exist.
168 167 transient : dict, optional
169 168 A dictionary of transient data to associate with the output.
170 169 Data in this dict should not be persisted to files (e.g. notebooks).
171 170 display_id : str, optional
172 171 Set an id for the display.
173 172 This id can be used for updating this display area later via update_display.
174 173 If given as True, generate a new display_id
175 174 kwargs: additional keyword-args, optional
176 175 Additional keyword-arguments are passed through to the display publisher.
177 176
178 177 Returns
179 178 -------
180 179
181 180 handle: DisplayHandle
182 181 Returns a handle on updatable displays, if display_id is given.
183 182 Returns None if no display_id is given (default).
184 183 """
185 184 raw = kwargs.pop('raw', False)
186 185 if transient is None:
187 186 transient = {}
188 187 if display_id:
189 188 if display_id == True:
190 189 display_id = _new_id()
191 190 transient['display_id'] = display_id
192 191 if kwargs.get('update') and 'display_id' not in transient:
193 192 raise TypeError('display_id required for update_display')
194 193 if transient:
195 194 kwargs['transient'] = transient
196 195
197 196 from IPython.core.interactiveshell import InteractiveShell
198 197
199 198 if not raw:
200 199 format = InteractiveShell.instance().display_formatter.format
201 200
202 201 for obj in objs:
203 202 if raw:
204 203 publish_display_data(data=obj, metadata=metadata, **kwargs)
205 204 else:
206 205 format_dict, md_dict = format(obj, include=include, exclude=exclude)
207 206 if not format_dict:
208 207 # nothing to display (e.g. _ipython_display_ took over)
209 208 continue
210 209 if metadata:
211 210 # kwarg-specified metadata gets precedence
212 211 _merge(md_dict, metadata)
213 212 publish_display_data(data=format_dict, metadata=md_dict, **kwargs)
214 213 if display_id:
215 214 return DisplayHandle(display_id)
216 215
217 216
218 217 # use * for keyword-only display_id arg
219 218 def update_display(obj, *, display_id, **kwargs):
220 219 """Update an existing display by id
221 220
222 221 Parameters
223 222 ----------
224 223
225 224 obj:
226 225 The object with which to update the display
227 226 display_id: keyword-only
228 227 The id of the display to update
229 228 """
230 229 kwargs['update'] = True
231 230 display(obj, display_id=display_id, **kwargs)
232 231
233 232
234 233 class DisplayHandle(object):
235 234 """A handle on an updatable display
236 235
237 236 Call .update(obj) to display a new object.
238 237
239 238 Call .display(obj) to add a new instance of this display,
240 239 and update existing instances.
241 240 """
242 241
243 242 def __init__(self, display_id=None):
244 243 if display_id is None:
245 244 display_id = _new_id()
246 245 self.display_id = display_id
247 246
248 247 def __repr__(self):
249 248 return "<%s display_id=%s>" % (self.__class__.__name__, self.display_id)
250 249
251 250 def display(self, obj, **kwargs):
252 251 """Make a new display with my id, updating existing instances.
253 252
254 253 Parameters
255 254 ----------
256 255
257 256 obj:
258 257 object to display
259 258 **kwargs:
260 259 additional keyword arguments passed to display
261 260 """
262 261 display(obj, display_id=self.display_id, **kwargs)
263 262
264 263 def update(self, obj, **kwargs):
265 264 """Update existing displays with my id
266 265
267 266 Parameters
268 267 ----------
269 268
270 269 obj:
271 270 object to display
272 271 **kwargs:
273 272 additional keyword arguments passed to update_display
274 273 """
275 274 update_display(obj, display_id=self.display_id, **kwargs)
276 275
277 276
278 277 def display_pretty(*objs, **kwargs):
279 278 """Display the pretty (default) representation of an object.
280 279
281 280 Parameters
282 281 ----------
283 282 objs : tuple of objects
284 283 The Python objects to display, or if raw=True raw text data to
285 284 display.
286 285 raw : bool
287 286 Are the data objects raw data or Python objects that need to be
288 287 formatted before display? [default: False]
289 288 metadata : dict (optional)
290 289 Metadata to be associated with the specific mimetype output.
291 290 """
292 291 _display_mimetype('text/plain', objs, **kwargs)
293 292
294 293
295 294 def display_html(*objs, **kwargs):
296 295 """Display the HTML representation of an object.
297 296
298 297 Note: If raw=False and the object does not have a HTML
299 298 representation, no HTML will be shown.
300 299
301 300 Parameters
302 301 ----------
303 302 objs : tuple of objects
304 303 The Python objects to display, or if raw=True raw HTML data to
305 304 display.
306 305 raw : bool
307 306 Are the data objects raw data or Python objects that need to be
308 307 formatted before display? [default: False]
309 308 metadata : dict (optional)
310 309 Metadata to be associated with the specific mimetype output.
311 310 """
312 311 _display_mimetype('text/html', objs, **kwargs)
313 312
314 313
315 314 def display_markdown(*objs, **kwargs):
316 315 """Displays the Markdown representation of an object.
317 316
318 317 Parameters
319 318 ----------
320 319 objs : tuple of objects
321 320 The Python objects to display, or if raw=True raw markdown data to
322 321 display.
323 322 raw : bool
324 323 Are the data objects raw data or Python objects that need to be
325 324 formatted before display? [default: False]
326 325 metadata : dict (optional)
327 326 Metadata to be associated with the specific mimetype output.
328 327 """
329 328
330 329 _display_mimetype('text/markdown', objs, **kwargs)
331 330
332 331
333 332 def display_svg(*objs, **kwargs):
334 333 """Display the SVG representation of an object.
335 334
336 335 Parameters
337 336 ----------
338 337 objs : tuple of objects
339 338 The Python objects to display, or if raw=True raw svg data to
340 339 display.
341 340 raw : bool
342 341 Are the data objects raw data or Python objects that need to be
343 342 formatted before display? [default: False]
344 343 metadata : dict (optional)
345 344 Metadata to be associated with the specific mimetype output.
346 345 """
347 346 _display_mimetype('image/svg+xml', objs, **kwargs)
348 347
349 348
350 349 def display_png(*objs, **kwargs):
351 350 """Display the PNG representation of an object.
352 351
353 352 Parameters
354 353 ----------
355 354 objs : tuple of objects
356 355 The Python objects to display, or if raw=True raw png data to
357 356 display.
358 357 raw : bool
359 358 Are the data objects raw data or Python objects that need to be
360 359 formatted before display? [default: False]
361 360 metadata : dict (optional)
362 361 Metadata to be associated with the specific mimetype output.
363 362 """
364 363 _display_mimetype('image/png', objs, **kwargs)
365 364
366 365
367 366 def display_jpeg(*objs, **kwargs):
368 367 """Display the JPEG representation of an object.
369 368
370 369 Parameters
371 370 ----------
372 371 objs : tuple of objects
373 372 The Python objects to display, or if raw=True raw JPEG data to
374 373 display.
375 374 raw : bool
376 375 Are the data objects raw data or Python objects that need to be
377 376 formatted before display? [default: False]
378 377 metadata : dict (optional)
379 378 Metadata to be associated with the specific mimetype output.
380 379 """
381 380 _display_mimetype('image/jpeg', objs, **kwargs)
382 381
383 382
384 383 def display_latex(*objs, **kwargs):
385 384 """Display the LaTeX representation of an object.
386 385
387 386 Parameters
388 387 ----------
389 388 objs : tuple of objects
390 389 The Python objects to display, or if raw=True raw latex data to
391 390 display.
392 391 raw : bool
393 392 Are the data objects raw data or Python objects that need to be
394 393 formatted before display? [default: False]
395 394 metadata : dict (optional)
396 395 Metadata to be associated with the specific mimetype output.
397 396 """
398 397 _display_mimetype('text/latex', objs, **kwargs)
399 398
400 399
401 400 def display_json(*objs, **kwargs):
402 401 """Display the JSON representation of an object.
403 402
404 403 Note that not many frontends support displaying JSON.
405 404
406 405 Parameters
407 406 ----------
408 407 objs : tuple of objects
409 408 The Python objects to display, or if raw=True raw json data to
410 409 display.
411 410 raw : bool
412 411 Are the data objects raw data or Python objects that need to be
413 412 formatted before display? [default: False]
414 413 metadata : dict (optional)
415 414 Metadata to be associated with the specific mimetype output.
416 415 """
417 416 _display_mimetype('application/json', objs, **kwargs)
418 417
419 418
420 419 def display_javascript(*objs, **kwargs):
421 420 """Display the Javascript representation of an object.
422 421
423 422 Parameters
424 423 ----------
425 424 objs : tuple of objects
426 425 The Python objects to display, or if raw=True raw javascript data to
427 426 display.
428 427 raw : bool
429 428 Are the data objects raw data or Python objects that need to be
430 429 formatted before display? [default: False]
431 430 metadata : dict (optional)
432 431 Metadata to be associated with the specific mimetype output.
433 432 """
434 433 _display_mimetype('application/javascript', objs, **kwargs)
435 434
436 435
437 436 def display_pdf(*objs, **kwargs):
438 437 """Display the PDF representation of an object.
439 438
440 439 Parameters
441 440 ----------
442 441 objs : tuple of objects
443 442 The Python objects to display, or if raw=True raw javascript data to
444 443 display.
445 444 raw : bool
446 445 Are the data objects raw data or Python objects that need to be
447 446 formatted before display? [default: False]
448 447 metadata : dict (optional)
449 448 Metadata to be associated with the specific mimetype output.
450 449 """
451 450 _display_mimetype('application/pdf', objs, **kwargs)
452 451
453 452
454 453 #-----------------------------------------------------------------------------
455 454 # Smart classes
456 455 #-----------------------------------------------------------------------------
457 456
458 457
459 458 class DisplayObject(object):
460 459 """An object that wraps data to be displayed."""
461 460
462 461 _read_flags = 'r'
463 462 _show_mem_addr = False
464 463
465 464 def __init__(self, data=None, url=None, filename=None):
466 465 """Create a display object given raw data.
467 466
468 467 When this object is returned by an expression or passed to the
469 468 display function, it will result in the data being displayed
470 469 in the frontend. The MIME type of the data should match the
471 470 subclasses used, so the Png subclass should be used for 'image/png'
472 471 data. If the data is a URL, the data will first be downloaded
473 472 and then displayed. If
474 473
475 474 Parameters
476 475 ----------
477 476 data : unicode, str or bytes
478 477 The raw data or a URL or file to load the data from
479 478 url : unicode
480 479 A URL to download the data from.
481 480 filename : unicode
482 481 Path to a local file to load the data from.
483 482 """
484 if data is not None and isinstance(data, string_types):
483 if data is not None and isinstance(data, str):
485 484 if data.startswith('http') and url is None:
486 485 url = data
487 486 filename = None
488 487 data = None
489 488 elif _safe_exists(data) and filename is None:
490 489 url = None
491 490 filename = data
492 491 data = None
493 492
494 493 self.data = data
495 494 self.url = url
496 495 self.filename = None if filename is None else unicode_type(filename)
497 496
498 497 self.reload()
499 498 self._check_data()
500 499
501 500 def __repr__(self):
502 501 if not self._show_mem_addr:
503 502 cls = self.__class__
504 503 r = "<%s.%s object>" % (cls.__module__, cls.__name__)
505 504 else:
506 505 r = super(DisplayObject, self).__repr__()
507 506 return r
508 507
509 508 def _check_data(self):
510 509 """Override in subclasses if there's something to check."""
511 510 pass
512 511
513 512 def reload(self):
514 513 """Reload the raw data from file or URL."""
515 514 if self.filename is not None:
516 515 with open(self.filename, self._read_flags) as f:
517 516 self.data = f.read()
518 517 elif self.url is not None:
519 518 try:
520 519 try:
521 520 from urllib.request import urlopen # Py3
522 521 except ImportError:
523 522 from urllib2 import urlopen
524 523 response = urlopen(self.url)
525 524 self.data = response.read()
526 525 # extract encoding from header, if there is one:
527 526 encoding = None
528 527 for sub in response.headers['content-type'].split(';'):
529 528 sub = sub.strip()
530 529 if sub.startswith('charset'):
531 530 encoding = sub.split('=')[-1].strip()
532 531 break
533 532 # decode data, if an encoding was specified
534 533 if encoding:
535 534 self.data = self.data.decode(encoding, 'replace')
536 535 except:
537 536 self.data = None
538 537
539 538 class TextDisplayObject(DisplayObject):
540 539 """Validate that display data is text"""
541 540 def _check_data(self):
542 if self.data is not None and not isinstance(self.data, string_types):
541 if self.data is not None and not isinstance(self.data, str):
543 542 raise TypeError("%s expects text, not %r" % (self.__class__.__name__, self.data))
544 543
545 544 class Pretty(TextDisplayObject):
546 545
547 546 def _repr_pretty_(self):
548 547 return self.data
549 548
550 549
551 550 class HTML(TextDisplayObject):
552 551
553 552 def _repr_html_(self):
554 553 return self.data
555 554
556 555 def __html__(self):
557 556 """
558 557 This method exists to inform other HTML-using modules (e.g. Markupsafe,
559 558 htmltag, etc) that this object is HTML and does not need things like
560 559 special characters (<>&) escaped.
561 560 """
562 561 return self._repr_html_()
563 562
564 563
565 564 class Markdown(TextDisplayObject):
566 565
567 566 def _repr_markdown_(self):
568 567 return self.data
569 568
570 569
571 570 class Math(TextDisplayObject):
572 571
573 572 def _repr_latex_(self):
574 573 s = self.data.strip('$')
575 574 return "$$%s$$" % s
576 575
577 576
578 577 class Latex(TextDisplayObject):
579 578
580 579 def _repr_latex_(self):
581 580 return self.data
582 581
583 582
584 583 class SVG(DisplayObject):
585 584
586 585 _read_flags = 'rb'
587 586 # wrap data in a property, which extracts the <svg> tag, discarding
588 587 # document headers
589 588 _data = None
590 589
591 590 @property
592 591 def data(self):
593 592 return self._data
594 593
595 594 @data.setter
596 595 def data(self, svg):
597 596 if svg is None:
598 597 self._data = None
599 598 return
600 599 # parse into dom object
601 600 from xml.dom import minidom
602 601 svg = cast_bytes_py2(svg)
603 602 x = minidom.parseString(svg)
604 603 # get svg tag (should be 1)
605 604 found_svg = x.getElementsByTagName('svg')
606 605 if found_svg:
607 606 svg = found_svg[0].toxml()
608 607 else:
609 608 # fallback on the input, trust the user
610 609 # but this is probably an error.
611 610 pass
612 611 svg = cast_unicode(svg)
613 612 self._data = svg
614 613
615 614 def _repr_svg_(self):
616 615 return self.data
617 616
618 617
619 618 class JSON(DisplayObject):
620 619 """JSON expects a JSON-able dict or list
621 620
622 621 not an already-serialized JSON string.
623 622
624 623 Scalar types (None, number, string) are not allowed, only dict or list containers.
625 624 """
626 625 # wrap data in a property, which warns about passing already-serialized JSON
627 626 _data = None
628 627 def __init__(self, data=None, url=None, filename=None, expanded=False, metadata=None):
629 628 """Create a JSON display object given raw data.
630 629
631 630 Parameters
632 631 ----------
633 632 data : dict or list
634 633 JSON data to display. Not an already-serialized JSON string.
635 634 Scalar types (None, number, string) are not allowed, only dict
636 635 or list containers.
637 636 url : unicode
638 637 A URL to download the data from.
639 638 filename : unicode
640 639 Path to a local file to load the data from.
641 640 expanded : boolean
642 641 Metadata to control whether a JSON display component is expanded.
643 642 metadata: dict
644 643 Specify extra metadata to attach to the json display object.
645 644 """
646 645 self.expanded = expanded
647 646 self.metadata = metadata
648 647 super(JSON, self).__init__(data=data, url=url, filename=filename)
649 648
650 649 def _check_data(self):
651 650 if self.data is not None and not isinstance(self.data, (dict, list)):
652 651 raise TypeError("%s expects JSONable dict or list, not %r" % (self.__class__.__name__, self.data))
653 652
654 653 @property
655 654 def data(self):
656 655 return self._data
657 656
658 657 @data.setter
659 658 def data(self, data):
660 if isinstance(data, string_types):
659 if isinstance(data, str):
661 660 warnings.warn("JSON expects JSONable dict or list, not JSON strings")
662 661 data = json.loads(data)
663 662 self._data = data
664 663
665 664 def _data_and_metadata(self):
666 665 md = {'expanded': self.expanded}
667 666 if self.metadata:
668 667 md.update(self.metadata)
669 668 return self.data, md
670 669
671 670 def _repr_json_(self):
672 671 return self._data_and_metadata()
673 672
674 673 css_t = """$("head").append($("<link/>").attr({
675 674 rel: "stylesheet",
676 675 type: "text/css",
677 676 href: "%s"
678 677 }));
679 678 """
680 679
681 680 lib_t1 = """$.getScript("%s", function () {
682 681 """
683 682 lib_t2 = """});
684 683 """
685 684
686 685 class Javascript(TextDisplayObject):
687 686
688 687 def __init__(self, data=None, url=None, filename=None, lib=None, css=None):
689 688 """Create a Javascript display object given raw data.
690 689
691 690 When this object is returned by an expression or passed to the
692 691 display function, it will result in the data being displayed
693 692 in the frontend. If the data is a URL, the data will first be
694 693 downloaded and then displayed.
695 694
696 695 In the Notebook, the containing element will be available as `element`,
697 696 and jQuery will be available. Content appended to `element` will be
698 697 visible in the output area.
699 698
700 699 Parameters
701 700 ----------
702 701 data : unicode, str or bytes
703 702 The Javascript source code or a URL to download it from.
704 703 url : unicode
705 704 A URL to download the data from.
706 705 filename : unicode
707 706 Path to a local file to load the data from.
708 707 lib : list or str
709 708 A sequence of Javascript library URLs to load asynchronously before
710 709 running the source code. The full URLs of the libraries should
711 710 be given. A single Javascript library URL can also be given as a
712 711 string.
713 712 css: : list or str
714 713 A sequence of css files to load before running the source code.
715 714 The full URLs of the css files should be given. A single css URL
716 715 can also be given as a string.
717 716 """
718 if isinstance(lib, string_types):
717 if isinstance(lib, str):
719 718 lib = [lib]
720 719 elif lib is None:
721 720 lib = []
722 if isinstance(css, string_types):
721 if isinstance(css, str):
723 722 css = [css]
724 723 elif css is None:
725 724 css = []
726 725 if not isinstance(lib, (list,tuple)):
727 726 raise TypeError('expected sequence, got: %r' % lib)
728 727 if not isinstance(css, (list,tuple)):
729 728 raise TypeError('expected sequence, got: %r' % css)
730 729 self.lib = lib
731 730 self.css = css
732 731 super(Javascript, self).__init__(data=data, url=url, filename=filename)
733 732
734 733 def _repr_javascript_(self):
735 734 r = ''
736 735 for c in self.css:
737 736 r += css_t % c
738 737 for l in self.lib:
739 738 r += lib_t1 % l
740 739 r += self.data
741 740 r += lib_t2*len(self.lib)
742 741 return r
743 742
744 743 # constants for identifying png/jpeg data
745 744 _PNG = b'\x89PNG\r\n\x1a\n'
746 745 _JPEG = b'\xff\xd8'
747 746
748 747 def _pngxy(data):
749 748 """read the (width, height) from a PNG header"""
750 749 ihdr = data.index(b'IHDR')
751 750 # next 8 bytes are width/height
752 751 w4h4 = data[ihdr+4:ihdr+12]
753 752 return struct.unpack('>ii', w4h4)
754 753
755 754 def _jpegxy(data):
756 755 """read the (width, height) from a JPEG header"""
757 756 # adapted from http://www.64lines.com/jpeg-width-height
758 757
759 758 idx = 4
760 759 while True:
761 760 block_size = struct.unpack('>H', data[idx:idx+2])[0]
762 761 idx = idx + block_size
763 762 if data[idx:idx+2] == b'\xFF\xC0':
764 763 # found Start of Frame
765 764 iSOF = idx
766 765 break
767 766 else:
768 767 # read another block
769 768 idx += 2
770 769
771 770 h, w = struct.unpack('>HH', data[iSOF+5:iSOF+9])
772 771 return w, h
773 772
774 773 class Image(DisplayObject):
775 774
776 775 _read_flags = 'rb'
777 776 _FMT_JPEG = u'jpeg'
778 777 _FMT_PNG = u'png'
779 778 _ACCEPTABLE_EMBEDDINGS = [_FMT_JPEG, _FMT_PNG]
780 779
781 780 def __init__(self, data=None, url=None, filename=None, format=None,
782 781 embed=None, width=None, height=None, retina=False,
783 782 unconfined=False, metadata=None):
784 783 """Create a PNG/JPEG image object given raw data.
785 784
786 785 When this object is returned by an input cell or passed to the
787 786 display function, it will result in the image being displayed
788 787 in the frontend.
789 788
790 789 Parameters
791 790 ----------
792 791 data : unicode, str or bytes
793 792 The raw image data or a URL or filename to load the data from.
794 793 This always results in embedded image data.
795 794 url : unicode
796 795 A URL to download the data from. If you specify `url=`,
797 796 the image data will not be embedded unless you also specify `embed=True`.
798 797 filename : unicode
799 798 Path to a local file to load the data from.
800 799 Images from a file are always embedded.
801 800 format : unicode
802 801 The format of the image data (png/jpeg/jpg). If a filename or URL is given
803 802 for format will be inferred from the filename extension.
804 803 embed : bool
805 804 Should the image data be embedded using a data URI (True) or be
806 805 loaded using an <img> tag. Set this to True if you want the image
807 806 to be viewable later with no internet connection in the notebook.
808 807
809 808 Default is `True`, unless the keyword argument `url` is set, then
810 809 default value is `False`.
811 810
812 811 Note that QtConsole is not able to display images if `embed` is set to `False`
813 812 width : int
814 813 Width in pixels to which to constrain the image in html
815 814 height : int
816 815 Height in pixels to which to constrain the image in html
817 816 retina : bool
818 817 Automatically set the width and height to half of the measured
819 818 width and height.
820 819 This only works for embedded images because it reads the width/height
821 820 from image data.
822 821 For non-embedded images, you can just set the desired display width
823 822 and height directly.
824 823 unconfined: bool
825 824 Set unconfined=True to disable max-width confinement of the image.
826 825 metadata: dict
827 826 Specify extra metadata to attach to the image.
828 827
829 828 Examples
830 829 --------
831 830 # embedded image data, works in qtconsole and notebook
832 831 # when passed positionally, the first arg can be any of raw image data,
833 832 # a URL, or a filename from which to load image data.
834 833 # The result is always embedding image data for inline images.
835 834 Image('http://www.google.fr/images/srpr/logo3w.png')
836 835 Image('/path/to/image.jpg')
837 836 Image(b'RAW_PNG_DATA...')
838 837
839 838 # Specifying Image(url=...) does not embed the image data,
840 839 # it only generates `<img>` tag with a link to the source.
841 840 # This will not work in the qtconsole or offline.
842 841 Image(url='http://www.google.fr/images/srpr/logo3w.png')
843 842
844 843 """
845 844 if filename is not None:
846 845 ext = self._find_ext(filename)
847 846 elif url is not None:
848 847 ext = self._find_ext(url)
849 848 elif data is None:
850 849 raise ValueError("No image data found. Expecting filename, url, or data.")
851 elif isinstance(data, string_types) and (
850 elif isinstance(data, str) and (
852 851 data.startswith('http') or _safe_exists(data)
853 852 ):
854 853 ext = self._find_ext(data)
855 854 else:
856 855 ext = None
857 856
858 857 if format is None:
859 858 if ext is not None:
860 859 if ext == u'jpg' or ext == u'jpeg':
861 860 format = self._FMT_JPEG
862 861 if ext == u'png':
863 862 format = self._FMT_PNG
864 863 else:
865 864 format = ext.lower()
866 865 elif isinstance(data, bytes):
867 866 # infer image type from image data header,
868 867 # only if format has not been specified.
869 868 if data[:2] == _JPEG:
870 869 format = self._FMT_JPEG
871 870
872 871 # failed to detect format, default png
873 872 if format is None:
874 873 format = 'png'
875 874
876 875 if format.lower() == 'jpg':
877 876 # jpg->jpeg
878 877 format = self._FMT_JPEG
879 878
880 879 self.format = unicode_type(format).lower()
881 880 self.embed = embed if embed is not None else (url is None)
882 881
883 882 if self.embed and self.format not in self._ACCEPTABLE_EMBEDDINGS:
884 883 raise ValueError("Cannot embed the '%s' image format" % (self.format))
885 884 self.width = width
886 885 self.height = height
887 886 self.retina = retina
888 887 self.unconfined = unconfined
889 888 self.metadata = metadata
890 889 super(Image, self).__init__(data=data, url=url, filename=filename)
891 890
892 891 if retina:
893 892 self._retina_shape()
894 893
895 894 def _retina_shape(self):
896 895 """load pixel-doubled width and height from image data"""
897 896 if not self.embed:
898 897 return
899 898 if self.format == 'png':
900 899 w, h = _pngxy(self.data)
901 900 elif self.format == 'jpeg':
902 901 w, h = _jpegxy(self.data)
903 902 else:
904 903 # retina only supports png
905 904 return
906 905 self.width = w // 2
907 906 self.height = h // 2
908 907
909 908 def reload(self):
910 909 """Reload the raw data from file or URL."""
911 910 if self.embed:
912 911 super(Image,self).reload()
913 912 if self.retina:
914 913 self._retina_shape()
915 914
916 915 def _repr_html_(self):
917 916 if not self.embed:
918 917 width = height = klass = ''
919 918 if self.width:
920 919 width = ' width="%d"' % self.width
921 920 if self.height:
922 921 height = ' height="%d"' % self.height
923 922 if self.unconfined:
924 923 klass = ' class="unconfined"'
925 924 return u'<img src="{url}"{width}{height}{klass}/>'.format(
926 925 url=self.url,
927 926 width=width,
928 927 height=height,
929 928 klass=klass,
930 929 )
931 930
932 931 def _data_and_metadata(self):
933 932 """shortcut for returning metadata with shape information, if defined"""
934 933 md = {}
935 934 if self.width:
936 935 md['width'] = self.width
937 936 if self.height:
938 937 md['height'] = self.height
939 938 if self.unconfined:
940 939 md['unconfined'] = self.unconfined
941 940 if self.metadata:
942 941 md.update(self.metadata)
943 942 if md:
944 943 return self.data, md
945 944 else:
946 945 return self.data
947 946
948 947 def _repr_png_(self):
949 948 if self.embed and self.format == u'png':
950 949 return self._data_and_metadata()
951 950
952 951 def _repr_jpeg_(self):
953 952 if self.embed and (self.format == u'jpeg' or self.format == u'jpg'):
954 953 return self._data_and_metadata()
955 954
956 955 def _find_ext(self, s):
957 956 return unicode_type(s.split('.')[-1].lower())
958 957
959 958 class Video(DisplayObject):
960 959
961 960 def __init__(self, data=None, url=None, filename=None, embed=False, mimetype=None):
962 961 """Create a video object given raw data or an URL.
963 962
964 963 When this object is returned by an input cell or passed to the
965 964 display function, it will result in the video being displayed
966 965 in the frontend.
967 966
968 967 Parameters
969 968 ----------
970 969 data : unicode, str or bytes
971 970 The raw video data or a URL or filename to load the data from.
972 971 Raw data will require passing `embed=True`.
973 972 url : unicode
974 973 A URL for the video. If you specify `url=`,
975 974 the image data will not be embedded.
976 975 filename : unicode
977 976 Path to a local file containing the video.
978 977 Will be interpreted as a local URL unless `embed=True`.
979 978 embed : bool
980 979 Should the video be embedded using a data URI (True) or be
981 980 loaded using a <video> tag (False).
982 981
983 982 Since videos are large, embedding them should be avoided, if possible.
984 983 You must confirm embedding as your intention by passing `embed=True`.
985 984
986 985 Local files can be displayed with URLs without embedding the content, via::
987 986
988 987 Video('./video.mp4')
989 988
990 989 mimetype: unicode
991 990 Specify the mimetype for embedded videos.
992 991 Default will be guessed from file extension, if available.
993 992
994 993 Examples
995 994 --------
996 995
997 996 Video('https://archive.org/download/Sita_Sings_the_Blues/Sita_Sings_the_Blues_small.mp4')
998 997 Video('path/to/video.mp4')
999 998 Video('path/to/video.mp4', embed=True)
1000 999 Video(b'raw-videodata', embed=True)
1001 1000 """
1002 if url is None and isinstance(data, string_types) and data.startswith(('http:', 'https:')):
1001 if url is None and isinstance(data, str) and data.startswith(('http:', 'https:')):
1003 1002 url = data
1004 1003 data = None
1005 1004 elif os.path.exists(data):
1006 1005 filename = data
1007 1006 data = None
1008 1007
1009 1008 if data and not embed:
1010 1009 msg = ''.join([
1011 1010 "To embed videos, you must pass embed=True ",
1012 1011 "(this may make your notebook files huge)\n",
1013 1012 "Consider passing Video(url='...')",
1014 1013 ])
1015 1014 raise ValueError(msg)
1016 1015
1017 1016 self.mimetype = mimetype
1018 1017 self.embed = embed
1019 1018 super(Video, self).__init__(data=data, url=url, filename=filename)
1020 1019
1021 1020 def _repr_html_(self):
1022 1021 # External URLs and potentially local files are not embedded into the
1023 1022 # notebook output.
1024 1023 if not self.embed:
1025 1024 url = self.url if self.url is not None else self.filename
1026 1025 output = """<video src="{0}" controls>
1027 1026 Your browser does not support the <code>video</code> element.
1028 1027 </video>""".format(url)
1029 1028 return output
1030 1029
1031 1030 # Embedded videos are base64-encoded.
1032 1031 mimetype = self.mimetype
1033 1032 if self.filename is not None:
1034 1033 if not mimetype:
1035 1034 mimetype, _ = mimetypes.guess_type(self.filename)
1036 1035
1037 1036 with open(self.filename, 'rb') as f:
1038 1037 video = f.read()
1039 1038 else:
1040 1039 video = self.data
1041 1040 if isinstance(video, unicode_type):
1042 1041 # unicode input is already b64-encoded
1043 1042 b64_video = video
1044 1043 else:
1045 1044 b64_video = base64_encode(video).decode('ascii').rstrip()
1046 1045
1047 1046 output = """<video controls>
1048 1047 <source src="data:{0};base64,{1}" type="{0}">
1049 1048 Your browser does not support the video tag.
1050 1049 </video>""".format(mimetype, b64_video)
1051 1050 return output
1052 1051
1053 1052 def reload(self):
1054 1053 # TODO
1055 1054 pass
1056 1055
1057 1056 def _repr_png_(self):
1058 1057 # TODO
1059 1058 pass
1060 1059 def _repr_jpeg_(self):
1061 1060 # TODO
1062 1061 pass
1063 1062
1064 1063 def clear_output(wait=False):
1065 1064 """Clear the output of the current cell receiving output.
1066 1065
1067 1066 Parameters
1068 1067 ----------
1069 1068 wait : bool [default: false]
1070 1069 Wait to clear the output until new output is available to replace it."""
1071 1070 from IPython.core.interactiveshell import InteractiveShell
1072 1071 if InteractiveShell.initialized():
1073 1072 InteractiveShell.instance().display_pub.clear_output(wait)
1074 1073 else:
1075 1074 print('\033[2K\r', end='')
1076 1075 sys.stdout.flush()
1077 1076 print('\033[2K\r', end='')
1078 1077 sys.stderr.flush()
1079 1078
1080 1079
1081 1080 @skip_doctest
1082 1081 def set_matplotlib_formats(*formats, **kwargs):
1083 1082 """Select figure formats for the inline backend. Optionally pass quality for JPEG.
1084 1083
1085 1084 For example, this enables PNG and JPEG output with a JPEG quality of 90%::
1086 1085
1087 1086 In [1]: set_matplotlib_formats('png', 'jpeg', quality=90)
1088 1087
1089 1088 To set this in your config files use the following::
1090 1089
1091 1090 c.InlineBackend.figure_formats = {'png', 'jpeg'}
1092 1091 c.InlineBackend.print_figure_kwargs.update({'quality' : 90})
1093 1092
1094 1093 Parameters
1095 1094 ----------
1096 1095 *formats : strs
1097 1096 One or more figure formats to enable: 'png', 'retina', 'jpeg', 'svg', 'pdf'.
1098 1097 **kwargs :
1099 1098 Keyword args will be relayed to ``figure.canvas.print_figure``.
1100 1099 """
1101 1100 from IPython.core.interactiveshell import InteractiveShell
1102 1101 from IPython.core.pylabtools import select_figure_formats
1103 1102 # build kwargs, starting with InlineBackend config
1104 1103 kw = {}
1105 1104 from ipykernel.pylab.config import InlineBackend
1106 1105 cfg = InlineBackend.instance()
1107 1106 kw.update(cfg.print_figure_kwargs)
1108 1107 kw.update(**kwargs)
1109 1108 shell = InteractiveShell.instance()
1110 1109 select_figure_formats(shell, formats, **kw)
1111 1110
1112 1111 @skip_doctest
1113 1112 def set_matplotlib_close(close=True):
1114 1113 """Set whether the inline backend closes all figures automatically or not.
1115 1114
1116 1115 By default, the inline backend used in the IPython Notebook will close all
1117 1116 matplotlib figures automatically after each cell is run. This means that
1118 1117 plots in different cells won't interfere. Sometimes, you may want to make
1119 1118 a plot in one cell and then refine it in later cells. This can be accomplished
1120 1119 by::
1121 1120
1122 1121 In [1]: set_matplotlib_close(False)
1123 1122
1124 1123 To set this in your config files use the following::
1125 1124
1126 1125 c.InlineBackend.close_figures = False
1127 1126
1128 1127 Parameters
1129 1128 ----------
1130 1129 close : bool
1131 1130 Should all matplotlib figures be automatically closed after each cell is
1132 1131 run?
1133 1132 """
1134 1133 from ipykernel.pylab.config import InlineBackend
1135 1134 cfg = InlineBackend.instance()
1136 1135 cfg.close_figures = close
@@ -1,947 +1,947 b''
1 1 # -*- coding: utf-8 -*-
2 2 """Display formatters.
3 3
4 4 Inheritance diagram:
5 5
6 6 .. inheritance-diagram:: IPython.core.formatters
7 7 :parts: 3
8 8 """
9 9
10 10 # Copyright (c) IPython Development Team.
11 11 # Distributed under the terms of the Modified BSD License.
12 12
13 13 import abc
14 14 import json
15 15 import sys
16 16 import traceback
17 17 import warnings
18 18
19 19 from decorator import decorator
20 20
21 21 from traitlets.config.configurable import Configurable
22 22 from IPython.core.getipython import get_ipython
23 23 from IPython.utils.sentinel import Sentinel
24 24 from IPython.utils.dir2 import get_real_method
25 25 from IPython.lib import pretty
26 26 from traitlets import (
27 27 Bool, Dict, Integer, Unicode, CUnicode, ObjectName, List,
28 28 ForwardDeclaredInstance,
29 29 default, observe,
30 30 )
31 31 from IPython.utils.py3compat import (
32 with_metaclass, string_types, unicode_type,
32 with_metaclass, unicode_type,
33 33 )
34 34
35 35
36 36 class DisplayFormatter(Configurable):
37 37
38 38 active_types = List(Unicode(),
39 39 help="""List of currently active mime-types to display.
40 40 You can use this to set a white-list for formats to display.
41 41
42 42 Most users will not need to change this value.
43 43 """).tag(config=True)
44 44
45 45 @default('active_types')
46 46 def _active_types_default(self):
47 47 return self.format_types
48 48
49 49 @observe('active_types')
50 50 def _active_types_changed(self, change):
51 51 for key, formatter in self.formatters.items():
52 52 if key in change['new']:
53 53 formatter.enabled = True
54 54 else:
55 55 formatter.enabled = False
56 56
57 57 ipython_display_formatter = ForwardDeclaredInstance('FormatterABC')
58 58 @default('ipython_display_formatter')
59 59 def _default_formatter(self):
60 60 return IPythonDisplayFormatter(parent=self)
61 61
62 62 # A dict of formatter whose keys are format types (MIME types) and whose
63 63 # values are subclasses of BaseFormatter.
64 64 formatters = Dict()
65 65 @default('formatters')
66 66 def _formatters_default(self):
67 67 """Activate the default formatters."""
68 68 formatter_classes = [
69 69 PlainTextFormatter,
70 70 HTMLFormatter,
71 71 MarkdownFormatter,
72 72 SVGFormatter,
73 73 PNGFormatter,
74 74 PDFFormatter,
75 75 JPEGFormatter,
76 76 LatexFormatter,
77 77 JSONFormatter,
78 78 JavascriptFormatter
79 79 ]
80 80 d = {}
81 81 for cls in formatter_classes:
82 82 f = cls(parent=self)
83 83 d[f.format_type] = f
84 84 return d
85 85
86 86 def format(self, obj, include=None, exclude=None):
87 87 """Return a format data dict for an object.
88 88
89 89 By default all format types will be computed.
90 90
91 91 The following MIME types are currently implemented:
92 92
93 93 * text/plain
94 94 * text/html
95 95 * text/markdown
96 96 * text/latex
97 97 * application/json
98 98 * application/javascript
99 99 * application/pdf
100 100 * image/png
101 101 * image/jpeg
102 102 * image/svg+xml
103 103
104 104 Parameters
105 105 ----------
106 106 obj : object
107 107 The Python object whose format data will be computed.
108 108 include : list or tuple, optional
109 109 A list of format type strings (MIME types) to include in the
110 110 format data dict. If this is set *only* the format types included
111 111 in this list will be computed.
112 112 exclude : list or tuple, optional
113 113 A list of format type string (MIME types) to exclude in the format
114 114 data dict. If this is set all format types will be computed,
115 115 except for those included in this argument.
116 116
117 117 Returns
118 118 -------
119 119 (format_dict, metadata_dict) : tuple of two dicts
120 120
121 121 format_dict is a dictionary of key/value pairs, one of each format that was
122 122 generated for the object. The keys are the format types, which
123 123 will usually be MIME type strings and the values and JSON'able
124 124 data structure containing the raw data for the representation in
125 125 that format.
126 126
127 127 metadata_dict is a dictionary of metadata about each mime-type output.
128 128 Its keys will be a strict subset of the keys in format_dict.
129 129 """
130 130 format_dict = {}
131 131 md_dict = {}
132 132
133 133 if self.ipython_display_formatter(obj):
134 134 # object handled itself, don't proceed
135 135 return {}, {}
136 136
137 137 for format_type, formatter in self.formatters.items():
138 138 if include and format_type not in include:
139 139 continue
140 140 if exclude and format_type in exclude:
141 141 continue
142 142
143 143 md = None
144 144 try:
145 145 data = formatter(obj)
146 146 except:
147 147 # FIXME: log the exception
148 148 raise
149 149
150 150 # formatters can return raw data or (data, metadata)
151 151 if isinstance(data, tuple) and len(data) == 2:
152 152 data, md = data
153 153
154 154 if data is not None:
155 155 format_dict[format_type] = data
156 156 if md is not None:
157 157 md_dict[format_type] = md
158 158
159 159 return format_dict, md_dict
160 160
161 161 @property
162 162 def format_types(self):
163 163 """Return the format types (MIME types) of the active formatters."""
164 164 return list(self.formatters.keys())
165 165
166 166
167 167 #-----------------------------------------------------------------------------
168 168 # Formatters for specific format types (text, html, svg, etc.)
169 169 #-----------------------------------------------------------------------------
170 170
171 171
172 172 def _safe_repr(obj):
173 173 """Try to return a repr of an object
174 174
175 175 always returns a string, at least.
176 176 """
177 177 try:
178 178 return repr(obj)
179 179 except Exception as e:
180 180 return "un-repr-able object (%r)" % e
181 181
182 182
183 183 class FormatterWarning(UserWarning):
184 184 """Warning class for errors in formatters"""
185 185
186 186 @decorator
187 187 def catch_format_error(method, self, *args, **kwargs):
188 188 """show traceback on failed format call"""
189 189 try:
190 190 r = method(self, *args, **kwargs)
191 191 except NotImplementedError:
192 192 # don't warn on NotImplementedErrors
193 193 return None
194 194 except Exception:
195 195 exc_info = sys.exc_info()
196 196 ip = get_ipython()
197 197 if ip is not None:
198 198 ip.showtraceback(exc_info)
199 199 else:
200 200 traceback.print_exception(*exc_info)
201 201 return None
202 202 return self._check_return(r, args[0])
203 203
204 204
205 205 class FormatterABC(with_metaclass(abc.ABCMeta, object)):
206 206 """ Abstract base class for Formatters.
207 207
208 208 A formatter is a callable class that is responsible for computing the
209 209 raw format data for a particular format type (MIME type). For example,
210 210 an HTML formatter would have a format type of `text/html` and would return
211 211 the HTML representation of the object when called.
212 212 """
213 213
214 214 # The format type of the data returned, usually a MIME type.
215 215 format_type = 'text/plain'
216 216
217 217 # Is the formatter enabled...
218 218 enabled = True
219 219
220 220 @abc.abstractmethod
221 221 def __call__(self, obj):
222 222 """Return a JSON'able representation of the object.
223 223
224 224 If the object cannot be formatted by this formatter,
225 225 warn and return None.
226 226 """
227 227 return repr(obj)
228 228
229 229
230 230 def _mod_name_key(typ):
231 231 """Return a (__module__, __name__) tuple for a type.
232 232
233 233 Used as key in Formatter.deferred_printers.
234 234 """
235 235 module = getattr(typ, '__module__', None)
236 236 name = getattr(typ, '__name__', None)
237 237 return (module, name)
238 238
239 239
240 240 def _get_type(obj):
241 241 """Return the type of an instance (old and new-style)"""
242 242 return getattr(obj, '__class__', None) or type(obj)
243 243
244 244
245 245 _raise_key_error = Sentinel('_raise_key_error', __name__,
246 246 """
247 247 Special value to raise a KeyError
248 248
249 249 Raise KeyError in `BaseFormatter.pop` if passed as the default value to `pop`
250 250 """)
251 251
252 252
253 253 class BaseFormatter(Configurable):
254 254 """A base formatter class that is configurable.
255 255
256 256 This formatter should usually be used as the base class of all formatters.
257 257 It is a traited :class:`Configurable` class and includes an extensible
258 258 API for users to determine how their objects are formatted. The following
259 259 logic is used to find a function to format an given object.
260 260
261 261 1. The object is introspected to see if it has a method with the name
262 262 :attr:`print_method`. If is does, that object is passed to that method
263 263 for formatting.
264 264 2. If no print method is found, three internal dictionaries are consulted
265 265 to find print method: :attr:`singleton_printers`, :attr:`type_printers`
266 266 and :attr:`deferred_printers`.
267 267
268 268 Users should use these dictionaries to register functions that will be
269 269 used to compute the format data for their objects (if those objects don't
270 270 have the special print methods). The easiest way of using these
271 271 dictionaries is through the :meth:`for_type` and :meth:`for_type_by_name`
272 272 methods.
273 273
274 274 If no function/callable is found to compute the format data, ``None`` is
275 275 returned and this format type is not used.
276 276 """
277 277
278 278 format_type = Unicode('text/plain')
279 _return_type = string_types
279 _return_type = str
280 280
281 281 enabled = Bool(True).tag(config=True)
282 282
283 283 print_method = ObjectName('__repr__')
284 284
285 285 # The singleton printers.
286 286 # Maps the IDs of the builtin singleton objects to the format functions.
287 287 singleton_printers = Dict().tag(config=True)
288 288
289 289 # The type-specific printers.
290 290 # Map type objects to the format functions.
291 291 type_printers = Dict().tag(config=True)
292 292
293 293 # The deferred-import type-specific printers.
294 294 # Map (modulename, classname) pairs to the format functions.
295 295 deferred_printers = Dict().tag(config=True)
296 296
297 297 @catch_format_error
298 298 def __call__(self, obj):
299 299 """Compute the format for an object."""
300 300 if self.enabled:
301 301 # lookup registered printer
302 302 try:
303 303 printer = self.lookup(obj)
304 304 except KeyError:
305 305 pass
306 306 else:
307 307 return printer(obj)
308 308 # Finally look for special method names
309 309 method = get_real_method(obj, self.print_method)
310 310 if method is not None:
311 311 return method()
312 312 return None
313 313 else:
314 314 return None
315 315
316 316 def __contains__(self, typ):
317 317 """map in to lookup_by_type"""
318 318 try:
319 319 self.lookup_by_type(typ)
320 320 except KeyError:
321 321 return False
322 322 else:
323 323 return True
324 324
325 325 def _check_return(self, r, obj):
326 326 """Check that a return value is appropriate
327 327
328 328 Return the value if so, None otherwise, warning if invalid.
329 329 """
330 330 if r is None or isinstance(r, self._return_type) or \
331 331 (isinstance(r, tuple) and r and isinstance(r[0], self._return_type)):
332 332 return r
333 333 else:
334 334 warnings.warn(
335 335 "%s formatter returned invalid type %s (expected %s) for object: %s" % \
336 336 (self.format_type, type(r), self._return_type, _safe_repr(obj)),
337 337 FormatterWarning
338 338 )
339 339
340 340 def lookup(self, obj):
341 341 """Look up the formatter for a given instance.
342 342
343 343 Parameters
344 344 ----------
345 345 obj : object instance
346 346
347 347 Returns
348 348 -------
349 349 f : callable
350 350 The registered formatting callable for the type.
351 351
352 352 Raises
353 353 ------
354 354 KeyError if the type has not been registered.
355 355 """
356 356 # look for singleton first
357 357 obj_id = id(obj)
358 358 if obj_id in self.singleton_printers:
359 359 return self.singleton_printers[obj_id]
360 360 # then lookup by type
361 361 return self.lookup_by_type(_get_type(obj))
362 362
363 363 def lookup_by_type(self, typ):
364 364 """Look up the registered formatter for a type.
365 365
366 366 Parameters
367 367 ----------
368 368 typ : type or '__module__.__name__' string for a type
369 369
370 370 Returns
371 371 -------
372 372 f : callable
373 373 The registered formatting callable for the type.
374 374
375 375 Raises
376 376 ------
377 377 KeyError if the type has not been registered.
378 378 """
379 if isinstance(typ, string_types):
379 if isinstance(typ, str):
380 380 typ_key = tuple(typ.rsplit('.',1))
381 381 if typ_key not in self.deferred_printers:
382 382 # We may have it cached in the type map. We will have to
383 383 # iterate over all of the types to check.
384 384 for cls in self.type_printers:
385 385 if _mod_name_key(cls) == typ_key:
386 386 return self.type_printers[cls]
387 387 else:
388 388 return self.deferred_printers[typ_key]
389 389 else:
390 390 for cls in pretty._get_mro(typ):
391 391 if cls in self.type_printers or self._in_deferred_types(cls):
392 392 return self.type_printers[cls]
393 393
394 394 # If we have reached here, the lookup failed.
395 395 raise KeyError("No registered printer for {0!r}".format(typ))
396 396
397 397 def for_type(self, typ, func=None):
398 398 """Add a format function for a given type.
399 399
400 400 Parameters
401 401 -----------
402 402 typ : type or '__module__.__name__' string for a type
403 403 The class of the object that will be formatted using `func`.
404 404 func : callable
405 405 A callable for computing the format data.
406 406 `func` will be called with the object to be formatted,
407 407 and will return the raw data in this formatter's format.
408 408 Subclasses may use a different call signature for the
409 409 `func` argument.
410 410
411 411 If `func` is None or not specified, there will be no change,
412 412 only returning the current value.
413 413
414 414 Returns
415 415 -------
416 416 oldfunc : callable
417 417 The currently registered callable.
418 418 If you are registering a new formatter,
419 419 this will be the previous value (to enable restoring later).
420 420 """
421 421 # if string given, interpret as 'pkg.module.class_name'
422 if isinstance(typ, string_types):
422 if isinstance(typ, str):
423 423 type_module, type_name = typ.rsplit('.', 1)
424 424 return self.for_type_by_name(type_module, type_name, func)
425 425
426 426 try:
427 427 oldfunc = self.lookup_by_type(typ)
428 428 except KeyError:
429 429 oldfunc = None
430 430
431 431 if func is not None:
432 432 self.type_printers[typ] = func
433 433
434 434 return oldfunc
435 435
436 436 def for_type_by_name(self, type_module, type_name, func=None):
437 437 """Add a format function for a type specified by the full dotted
438 438 module and name of the type, rather than the type of the object.
439 439
440 440 Parameters
441 441 ----------
442 442 type_module : str
443 443 The full dotted name of the module the type is defined in, like
444 444 ``numpy``.
445 445 type_name : str
446 446 The name of the type (the class name), like ``dtype``
447 447 func : callable
448 448 A callable for computing the format data.
449 449 `func` will be called with the object to be formatted,
450 450 and will return the raw data in this formatter's format.
451 451 Subclasses may use a different call signature for the
452 452 `func` argument.
453 453
454 454 If `func` is None or unspecified, there will be no change,
455 455 only returning the current value.
456 456
457 457 Returns
458 458 -------
459 459 oldfunc : callable
460 460 The currently registered callable.
461 461 If you are registering a new formatter,
462 462 this will be the previous value (to enable restoring later).
463 463 """
464 464 key = (type_module, type_name)
465 465
466 466 try:
467 467 oldfunc = self.lookup_by_type("%s.%s" % key)
468 468 except KeyError:
469 469 oldfunc = None
470 470
471 471 if func is not None:
472 472 self.deferred_printers[key] = func
473 473 return oldfunc
474 474
475 475 def pop(self, typ, default=_raise_key_error):
476 476 """Pop a formatter for the given type.
477 477
478 478 Parameters
479 479 ----------
480 480 typ : type or '__module__.__name__' string for a type
481 481 default : object
482 482 value to be returned if no formatter is registered for typ.
483 483
484 484 Returns
485 485 -------
486 486 obj : object
487 487 The last registered object for the type.
488 488
489 489 Raises
490 490 ------
491 491 KeyError if the type is not registered and default is not specified.
492 492 """
493 493
494 if isinstance(typ, string_types):
494 if isinstance(typ, str):
495 495 typ_key = tuple(typ.rsplit('.',1))
496 496 if typ_key not in self.deferred_printers:
497 497 # We may have it cached in the type map. We will have to
498 498 # iterate over all of the types to check.
499 499 for cls in self.type_printers:
500 500 if _mod_name_key(cls) == typ_key:
501 501 old = self.type_printers.pop(cls)
502 502 break
503 503 else:
504 504 old = default
505 505 else:
506 506 old = self.deferred_printers.pop(typ_key)
507 507 else:
508 508 if typ in self.type_printers:
509 509 old = self.type_printers.pop(typ)
510 510 else:
511 511 old = self.deferred_printers.pop(_mod_name_key(typ), default)
512 512 if old is _raise_key_error:
513 513 raise KeyError("No registered value for {0!r}".format(typ))
514 514 return old
515 515
516 516 def _in_deferred_types(self, cls):
517 517 """
518 518 Check if the given class is specified in the deferred type registry.
519 519
520 520 Successful matches will be moved to the regular type registry for future use.
521 521 """
522 522 mod = getattr(cls, '__module__', None)
523 523 name = getattr(cls, '__name__', None)
524 524 key = (mod, name)
525 525 if key in self.deferred_printers:
526 526 # Move the printer over to the regular registry.
527 527 printer = self.deferred_printers.pop(key)
528 528 self.type_printers[cls] = printer
529 529 return True
530 530 return False
531 531
532 532
533 533 class PlainTextFormatter(BaseFormatter):
534 534 """The default pretty-printer.
535 535
536 536 This uses :mod:`IPython.lib.pretty` to compute the format data of
537 537 the object. If the object cannot be pretty printed, :func:`repr` is used.
538 538 See the documentation of :mod:`IPython.lib.pretty` for details on
539 539 how to write pretty printers. Here is a simple example::
540 540
541 541 def dtype_pprinter(obj, p, cycle):
542 542 if cycle:
543 543 return p.text('dtype(...)')
544 544 if hasattr(obj, 'fields'):
545 545 if obj.fields is None:
546 546 p.text(repr(obj))
547 547 else:
548 548 p.begin_group(7, 'dtype([')
549 549 for i, field in enumerate(obj.descr):
550 550 if i > 0:
551 551 p.text(',')
552 552 p.breakable()
553 553 p.pretty(field)
554 554 p.end_group(7, '])')
555 555 """
556 556
557 557 # The format type of data returned.
558 558 format_type = Unicode('text/plain')
559 559
560 560 # This subclass ignores this attribute as it always need to return
561 561 # something.
562 562 enabled = Bool(True).tag(config=False)
563 563
564 564 max_seq_length = Integer(pretty.MAX_SEQ_LENGTH,
565 565 help="""Truncate large collections (lists, dicts, tuples, sets) to this size.
566 566
567 567 Set to 0 to disable truncation.
568 568 """
569 569 ).tag(config=True)
570 570
571 571 # Look for a _repr_pretty_ methods to use for pretty printing.
572 572 print_method = ObjectName('_repr_pretty_')
573 573
574 574 # Whether to pretty-print or not.
575 575 pprint = Bool(True).tag(config=True)
576 576
577 577 # Whether to be verbose or not.
578 578 verbose = Bool(False).tag(config=True)
579 579
580 580 # The maximum width.
581 581 max_width = Integer(79).tag(config=True)
582 582
583 583 # The newline character.
584 584 newline = Unicode('\n').tag(config=True)
585 585
586 586 # format-string for pprinting floats
587 587 float_format = Unicode('%r')
588 588 # setter for float precision, either int or direct format-string
589 589 float_precision = CUnicode('').tag(config=True)
590 590
591 591 @observe('float_precision')
592 592 def _float_precision_changed(self, change):
593 593 """float_precision changed, set float_format accordingly.
594 594
595 595 float_precision can be set by int or str.
596 596 This will set float_format, after interpreting input.
597 597 If numpy has been imported, numpy print precision will also be set.
598 598
599 599 integer `n` sets format to '%.nf', otherwise, format set directly.
600 600
601 601 An empty string returns to defaults (repr for float, 8 for numpy).
602 602
603 603 This parameter can be set via the '%precision' magic.
604 604 """
605 605
606 606 new = change['new']
607 607 if '%' in new:
608 608 # got explicit format string
609 609 fmt = new
610 610 try:
611 611 fmt%3.14159
612 612 except Exception:
613 613 raise ValueError("Precision must be int or format string, not %r"%new)
614 614 elif new:
615 615 # otherwise, should be an int
616 616 try:
617 617 i = int(new)
618 618 assert i >= 0
619 619 except ValueError:
620 620 raise ValueError("Precision must be int or format string, not %r"%new)
621 621 except AssertionError:
622 622 raise ValueError("int precision must be non-negative, not %r"%i)
623 623
624 624 fmt = '%%.%if'%i
625 625 if 'numpy' in sys.modules:
626 626 # set numpy precision if it has been imported
627 627 import numpy
628 628 numpy.set_printoptions(precision=i)
629 629 else:
630 630 # default back to repr
631 631 fmt = '%r'
632 632 if 'numpy' in sys.modules:
633 633 import numpy
634 634 # numpy default is 8
635 635 numpy.set_printoptions(precision=8)
636 636 self.float_format = fmt
637 637
638 638 # Use the default pretty printers from IPython.lib.pretty.
639 639 @default('singleton_printers')
640 640 def _singleton_printers_default(self):
641 641 return pretty._singleton_pprinters.copy()
642 642
643 643 @default('type_printers')
644 644 def _type_printers_default(self):
645 645 d = pretty._type_pprinters.copy()
646 646 d[float] = lambda obj,p,cycle: p.text(self.float_format%obj)
647 647 return d
648 648
649 649 @default('deferred_printers')
650 650 def _deferred_printers_default(self):
651 651 return pretty._deferred_type_pprinters.copy()
652 652
653 653 #### FormatterABC interface ####
654 654
655 655 @catch_format_error
656 656 def __call__(self, obj):
657 657 """Compute the pretty representation of the object."""
658 658 if not self.pprint:
659 659 return repr(obj)
660 660 else:
661 661 # handle str and unicode on Python 2
662 662 # io.StringIO only accepts unicode,
663 663 # cStringIO doesn't handle unicode on py2,
664 664 # StringIO allows str, unicode but only ascii str
665 665 stream = pretty.CUnicodeIO()
666 666 printer = pretty.RepresentationPrinter(stream, self.verbose,
667 667 self.max_width, self.newline,
668 668 max_seq_length=self.max_seq_length,
669 669 singleton_pprinters=self.singleton_printers,
670 670 type_pprinters=self.type_printers,
671 671 deferred_pprinters=self.deferred_printers)
672 672 printer.pretty(obj)
673 673 printer.flush()
674 674 return stream.getvalue()
675 675
676 676
677 677 class HTMLFormatter(BaseFormatter):
678 678 """An HTML formatter.
679 679
680 680 To define the callables that compute the HTML representation of your
681 681 objects, define a :meth:`_repr_html_` method or use the :meth:`for_type`
682 682 or :meth:`for_type_by_name` methods to register functions that handle
683 683 this.
684 684
685 685 The return value of this formatter should be a valid HTML snippet that
686 686 could be injected into an existing DOM. It should *not* include the
687 687 ```<html>`` or ```<body>`` tags.
688 688 """
689 689 format_type = Unicode('text/html')
690 690
691 691 print_method = ObjectName('_repr_html_')
692 692
693 693
694 694 class MarkdownFormatter(BaseFormatter):
695 695 """A Markdown formatter.
696 696
697 697 To define the callables that compute the Markdown representation of your
698 698 objects, define a :meth:`_repr_markdown_` method or use the :meth:`for_type`
699 699 or :meth:`for_type_by_name` methods to register functions that handle
700 700 this.
701 701
702 702 The return value of this formatter should be a valid Markdown.
703 703 """
704 704 format_type = Unicode('text/markdown')
705 705
706 706 print_method = ObjectName('_repr_markdown_')
707 707
708 708 class SVGFormatter(BaseFormatter):
709 709 """An SVG formatter.
710 710
711 711 To define the callables that compute the SVG representation of your
712 712 objects, define a :meth:`_repr_svg_` method or use the :meth:`for_type`
713 713 or :meth:`for_type_by_name` methods to register functions that handle
714 714 this.
715 715
716 716 The return value of this formatter should be valid SVG enclosed in
717 717 ```<svg>``` tags, that could be injected into an existing DOM. It should
718 718 *not* include the ```<html>`` or ```<body>`` tags.
719 719 """
720 720 format_type = Unicode('image/svg+xml')
721 721
722 722 print_method = ObjectName('_repr_svg_')
723 723
724 724
725 725 class PNGFormatter(BaseFormatter):
726 726 """A PNG formatter.
727 727
728 728 To define the callables that compute the PNG representation of your
729 729 objects, define a :meth:`_repr_png_` method or use the :meth:`for_type`
730 730 or :meth:`for_type_by_name` methods to register functions that handle
731 731 this.
732 732
733 733 The return value of this formatter should be raw PNG data, *not*
734 734 base64 encoded.
735 735 """
736 736 format_type = Unicode('image/png')
737 737
738 738 print_method = ObjectName('_repr_png_')
739 739
740 740 _return_type = (bytes, unicode_type)
741 741
742 742
743 743 class JPEGFormatter(BaseFormatter):
744 744 """A JPEG formatter.
745 745
746 746 To define the callables that compute the JPEG representation of your
747 747 objects, define a :meth:`_repr_jpeg_` method or use the :meth:`for_type`
748 748 or :meth:`for_type_by_name` methods to register functions that handle
749 749 this.
750 750
751 751 The return value of this formatter should be raw JPEG data, *not*
752 752 base64 encoded.
753 753 """
754 754 format_type = Unicode('image/jpeg')
755 755
756 756 print_method = ObjectName('_repr_jpeg_')
757 757
758 758 _return_type = (bytes, unicode_type)
759 759
760 760
761 761 class LatexFormatter(BaseFormatter):
762 762 """A LaTeX formatter.
763 763
764 764 To define the callables that compute the LaTeX representation of your
765 765 objects, define a :meth:`_repr_latex_` method or use the :meth:`for_type`
766 766 or :meth:`for_type_by_name` methods to register functions that handle
767 767 this.
768 768
769 769 The return value of this formatter should be a valid LaTeX equation,
770 770 enclosed in either ```$```, ```$$``` or another LaTeX equation
771 771 environment.
772 772 """
773 773 format_type = Unicode('text/latex')
774 774
775 775 print_method = ObjectName('_repr_latex_')
776 776
777 777
778 778 class JSONFormatter(BaseFormatter):
779 779 """A JSON string formatter.
780 780
781 781 To define the callables that compute the JSONable representation of
782 782 your objects, define a :meth:`_repr_json_` method or use the :meth:`for_type`
783 783 or :meth:`for_type_by_name` methods to register functions that handle
784 784 this.
785 785
786 786 The return value of this formatter should be a JSONable list or dict.
787 787 JSON scalars (None, number, string) are not allowed, only dict or list containers.
788 788 """
789 789 format_type = Unicode('application/json')
790 790 _return_type = (list, dict)
791 791
792 792 print_method = ObjectName('_repr_json_')
793 793
794 794 def _check_return(self, r, obj):
795 795 """Check that a return value is appropriate
796 796
797 797 Return the value if so, None otherwise, warning if invalid.
798 798 """
799 799 if r is None:
800 800 return
801 801 md = None
802 802 if isinstance(r, tuple):
803 803 # unpack data, metadata tuple for type checking on first element
804 804 r, md = r
805 805
806 806 # handle deprecated JSON-as-string form from IPython < 3
807 if isinstance(r, string_types):
807 if isinstance(r, str):
808 808 warnings.warn("JSON expects JSONable list/dict containers, not JSON strings",
809 809 FormatterWarning)
810 810 r = json.loads(r)
811 811
812 812 if md is not None:
813 813 # put the tuple back together
814 814 r = (r, md)
815 815 return super(JSONFormatter, self)._check_return(r, obj)
816 816
817 817
818 818 class JavascriptFormatter(BaseFormatter):
819 819 """A Javascript formatter.
820 820
821 821 To define the callables that compute the Javascript representation of
822 822 your objects, define a :meth:`_repr_javascript_` method or use the
823 823 :meth:`for_type` or :meth:`for_type_by_name` methods to register functions
824 824 that handle this.
825 825
826 826 The return value of this formatter should be valid Javascript code and
827 827 should *not* be enclosed in ```<script>``` tags.
828 828 """
829 829 format_type = Unicode('application/javascript')
830 830
831 831 print_method = ObjectName('_repr_javascript_')
832 832
833 833
834 834 class PDFFormatter(BaseFormatter):
835 835 """A PDF formatter.
836 836
837 837 To define the callables that compute the PDF representation of your
838 838 objects, define a :meth:`_repr_pdf_` method or use the :meth:`for_type`
839 839 or :meth:`for_type_by_name` methods to register functions that handle
840 840 this.
841 841
842 842 The return value of this formatter should be raw PDF data, *not*
843 843 base64 encoded.
844 844 """
845 845 format_type = Unicode('application/pdf')
846 846
847 847 print_method = ObjectName('_repr_pdf_')
848 848
849 849 _return_type = (bytes, unicode_type)
850 850
851 851 class IPythonDisplayFormatter(BaseFormatter):
852 852 """A Formatter for objects that know how to display themselves.
853 853
854 854 To define the callables that compute the representation of your
855 855 objects, define a :meth:`_ipython_display_` method or use the :meth:`for_type`
856 856 or :meth:`for_type_by_name` methods to register functions that handle
857 857 this. Unlike mime-type displays, this method should not return anything,
858 858 instead calling any appropriate display methods itself.
859 859
860 860 This display formatter has highest priority.
861 861 If it fires, no other display formatter will be called.
862 862 """
863 863 print_method = ObjectName('_ipython_display_')
864 864 _return_type = (type(None), bool)
865 865
866 866
867 867 @catch_format_error
868 868 def __call__(self, obj):
869 869 """Compute the format for an object."""
870 870 if self.enabled:
871 871 # lookup registered printer
872 872 try:
873 873 printer = self.lookup(obj)
874 874 except KeyError:
875 875 pass
876 876 else:
877 877 printer(obj)
878 878 return True
879 879 # Finally look for special method names
880 880 method = get_real_method(obj, self.print_method)
881 881 if method is not None:
882 882 method()
883 883 return True
884 884
885 885
886 886 FormatterABC.register(BaseFormatter)
887 887 FormatterABC.register(PlainTextFormatter)
888 888 FormatterABC.register(HTMLFormatter)
889 889 FormatterABC.register(MarkdownFormatter)
890 890 FormatterABC.register(SVGFormatter)
891 891 FormatterABC.register(PNGFormatter)
892 892 FormatterABC.register(PDFFormatter)
893 893 FormatterABC.register(JPEGFormatter)
894 894 FormatterABC.register(LatexFormatter)
895 895 FormatterABC.register(JSONFormatter)
896 896 FormatterABC.register(JavascriptFormatter)
897 897 FormatterABC.register(IPythonDisplayFormatter)
898 898
899 899
900 900 def format_display_data(obj, include=None, exclude=None):
901 901 """Return a format data dict for an object.
902 902
903 903 By default all format types will be computed.
904 904
905 905 The following MIME types are currently implemented:
906 906
907 907 * text/plain
908 908 * text/html
909 909 * text/markdown
910 910 * text/latex
911 911 * application/json
912 912 * application/javascript
913 913 * application/pdf
914 914 * image/png
915 915 * image/jpeg
916 916 * image/svg+xml
917 917
918 918 Parameters
919 919 ----------
920 920 obj : object
921 921 The Python object whose format data will be computed.
922 922
923 923 Returns
924 924 -------
925 925 format_dict : dict
926 926 A dictionary of key/value pairs, one or each format that was
927 927 generated for the object. The keys are the format types, which
928 928 will usually be MIME type strings and the values and JSON'able
929 929 data structure containing the raw data for the representation in
930 930 that format.
931 931 include : list or tuple, optional
932 932 A list of format type strings (MIME types) to include in the
933 933 format data dict. If this is set *only* the format types included
934 934 in this list will be computed.
935 935 exclude : list or tuple, optional
936 936 A list of format type string (MIME types) to exclue in the format
937 937 data dict. If this is set all format types will be computed,
938 938 except for those included in this argument.
939 939 """
940 940 from IPython.core.interactiveshell import InteractiveShell
941 941
942 942 return InteractiveShell.instance().display_formatter.format(
943 943 obj,
944 944 include,
945 945 exclude
946 946 )
947 947
@@ -1,3226 +1,3225 b''
1 1 # -*- coding: utf-8 -*-
2 2 """Main IPython class."""
3 3
4 4 #-----------------------------------------------------------------------------
5 5 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de>
6 6 # Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu>
7 7 # Copyright (C) 2008-2011 The IPython Development Team
8 8 #
9 9 # Distributed under the terms of the BSD License. The full license is in
10 10 # the file COPYING, distributed as part of this software.
11 11 #-----------------------------------------------------------------------------
12 12
13 13
14 14 import __future__
15 15 import abc
16 16 import ast
17 17 import atexit
18 18 import functools
19 19 import os
20 20 import re
21 21 import runpy
22 22 import sys
23 23 import tempfile
24 24 import traceback
25 25 import types
26 26 import subprocess
27 27 import warnings
28 28 from io import open as io_open
29 29
30 30 from pickleshare import PickleShareDB
31 31
32 32 from traitlets.config.configurable import SingletonConfigurable
33 33 from IPython.core import oinspect
34 34 from IPython.core import magic
35 35 from IPython.core import page
36 36 from IPython.core import prefilter
37 37 from IPython.core import shadowns
38 38 from IPython.core import ultratb
39 39 from IPython.core.alias import Alias, AliasManager
40 40 from IPython.core.autocall import ExitAutocall
41 41 from IPython.core.builtin_trap import BuiltinTrap
42 42 from IPython.core.events import EventManager, available_events
43 43 from IPython.core.compilerop import CachingCompiler, check_linecache_ipython
44 44 from IPython.core.debugger import Pdb
45 45 from IPython.core.display_trap import DisplayTrap
46 46 from IPython.core.displayhook import DisplayHook
47 47 from IPython.core.displaypub import DisplayPublisher
48 48 from IPython.core.error import InputRejected, UsageError
49 49 from IPython.core.extensions import ExtensionManager
50 50 from IPython.core.formatters import DisplayFormatter
51 51 from IPython.core.history import HistoryManager
52 52 from IPython.core.inputsplitter import ESC_MAGIC, ESC_MAGIC2
53 53 from IPython.core.logger import Logger
54 54 from IPython.core.macro import Macro
55 55 from IPython.core.payload import PayloadManager
56 56 from IPython.core.prefilter import PrefilterManager
57 57 from IPython.core.profiledir import ProfileDir
58 58 from IPython.core.usage import default_banner
59 59 from IPython.testing.skipdoctest import skip_doctest
60 60 from IPython.utils import PyColorize
61 61 from IPython.utils import io
62 62 from IPython.utils import py3compat
63 63 from IPython.utils import openpy
64 64 from IPython.utils.decorators import undoc
65 65 from IPython.utils.io import ask_yes_no
66 66 from IPython.utils.ipstruct import Struct
67 67 from IPython.paths import get_ipython_dir
68 68 from IPython.utils.path import get_home_dir, get_py_filename, ensure_dir_exists
69 69 from IPython.utils.process import system, getoutput
70 from IPython.utils.py3compat import (builtin_mod, unicode_type, string_types,
71 with_metaclass)
70 from IPython.utils.py3compat import builtin_mod, unicode_type, with_metaclass
72 71 from IPython.utils.strdispatch import StrDispatch
73 72 from IPython.utils.syspathcontext import prepended_to_syspath
74 73 from IPython.utils.text import format_screen, LSString, SList, DollarFormatter
75 74 from IPython.utils.tempdir import TemporaryDirectory
76 75 from traitlets import (
77 76 Integer, Bool, CaselessStrEnum, Enum, List, Dict, Unicode, Instance, Type,
78 77 observe, default,
79 78 )
80 79 from warnings import warn
81 80 from logging import error
82 81 import IPython.core.hooks
83 82
84 83 # NoOpContext is deprecated, but ipykernel imports it from here.
85 84 # See https://github.com/ipython/ipykernel/issues/157
86 85 from IPython.utils.contexts import NoOpContext
87 86
88 87 try:
89 88 import docrepr.sphinxify as sphx
90 89
91 90 def sphinxify(doc):
92 91 with TemporaryDirectory() as dirname:
93 92 return {
94 93 'text/html': sphx.sphinxify(doc, dirname),
95 94 'text/plain': doc
96 95 }
97 96 except ImportError:
98 97 sphinxify = None
99 98
100 99
101 100 class ProvisionalWarning(DeprecationWarning):
102 101 """
103 102 Warning class for unstable features
104 103 """
105 104 pass
106 105
107 106 #-----------------------------------------------------------------------------
108 107 # Globals
109 108 #-----------------------------------------------------------------------------
110 109
111 110 # compiled regexps for autoindent management
112 111 dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass')
113 112
114 113 #-----------------------------------------------------------------------------
115 114 # Utilities
116 115 #-----------------------------------------------------------------------------
117 116
118 117 @undoc
119 118 def softspace(file, newvalue):
120 119 """Copied from code.py, to remove the dependency"""
121 120
122 121 oldvalue = 0
123 122 try:
124 123 oldvalue = file.softspace
125 124 except AttributeError:
126 125 pass
127 126 try:
128 127 file.softspace = newvalue
129 128 except (AttributeError, TypeError):
130 129 # "attribute-less object" or "read-only attributes"
131 130 pass
132 131 return oldvalue
133 132
134 133 @undoc
135 134 def no_op(*a, **kw): pass
136 135
137 136
138 137 class SpaceInInput(Exception): pass
139 138
140 139
141 140 def get_default_colors():
142 141 "DEPRECATED"
143 142 warn('get_default_color is Deprecated, and is `Neutral` on all platforms.',
144 143 DeprecationWarning, stacklevel=2)
145 144 return 'Neutral'
146 145
147 146
148 147 class SeparateUnicode(Unicode):
149 148 r"""A Unicode subclass to validate separate_in, separate_out, etc.
150 149
151 150 This is a Unicode based trait that converts '0'->'' and ``'\\n'->'\n'``.
152 151 """
153 152
154 153 def validate(self, obj, value):
155 154 if value == '0': value = ''
156 155 value = value.replace('\\n','\n')
157 156 return super(SeparateUnicode, self).validate(obj, value)
158 157
159 158
160 159 @undoc
161 160 class DummyMod(object):
162 161 """A dummy module used for IPython's interactive module when
163 162 a namespace must be assigned to the module's __dict__."""
164 163 pass
165 164
166 165
167 166 class ExecutionResult(object):
168 167 """The result of a call to :meth:`InteractiveShell.run_cell`
169 168
170 169 Stores information about what took place.
171 170 """
172 171 execution_count = None
173 172 error_before_exec = None
174 173 error_in_exec = None
175 174 result = None
176 175
177 176 @property
178 177 def success(self):
179 178 return (self.error_before_exec is None) and (self.error_in_exec is None)
180 179
181 180 def raise_error(self):
182 181 """Reraises error if `success` is `False`, otherwise does nothing"""
183 182 if self.error_before_exec is not None:
184 183 raise self.error_before_exec
185 184 if self.error_in_exec is not None:
186 185 raise self.error_in_exec
187 186
188 187 def __repr__(self):
189 188 name = self.__class__.__qualname__
190 189 return '<%s object at %x, execution_count=%s error_before_exec=%s error_in_exec=%s result=%s>' %\
191 190 (name, id(self), self.execution_count, self.error_before_exec, self.error_in_exec, repr(self.result))
192 191
193 192
194 193 class InteractiveShell(SingletonConfigurable):
195 194 """An enhanced, interactive shell for Python."""
196 195
197 196 _instance = None
198 197
199 198 ast_transformers = List([], help=
200 199 """
201 200 A list of ast.NodeTransformer subclass instances, which will be applied
202 201 to user input before code is run.
203 202 """
204 203 ).tag(config=True)
205 204
206 205 autocall = Enum((0,1,2), default_value=0, help=
207 206 """
208 207 Make IPython automatically call any callable object even if you didn't
209 208 type explicit parentheses. For example, 'str 43' becomes 'str(43)'
210 209 automatically. The value can be '0' to disable the feature, '1' for
211 210 'smart' autocall, where it is not applied if there are no more
212 211 arguments on the line, and '2' for 'full' autocall, where all callable
213 212 objects are automatically called (even if no arguments are present).
214 213 """
215 214 ).tag(config=True)
216 215 # TODO: remove all autoindent logic and put into frontends.
217 216 # We can't do this yet because even runlines uses the autoindent.
218 217 autoindent = Bool(True, help=
219 218 """
220 219 Autoindent IPython code entered interactively.
221 220 """
222 221 ).tag(config=True)
223 222
224 223 automagic = Bool(True, help=
225 224 """
226 225 Enable magic commands to be called without the leading %.
227 226 """
228 227 ).tag(config=True)
229 228
230 229 banner1 = Unicode(default_banner,
231 230 help="""The part of the banner to be printed before the profile"""
232 231 ).tag(config=True)
233 232 banner2 = Unicode('',
234 233 help="""The part of the banner to be printed after the profile"""
235 234 ).tag(config=True)
236 235
237 236 cache_size = Integer(1000, help=
238 237 """
239 238 Set the size of the output cache. The default is 1000, you can
240 239 change it permanently in your config file. Setting it to 0 completely
241 240 disables the caching system, and the minimum value accepted is 20 (if
242 241 you provide a value less than 20, it is reset to 0 and a warning is
243 242 issued). This limit is defined because otherwise you'll spend more
244 243 time re-flushing a too small cache than working
245 244 """
246 245 ).tag(config=True)
247 246 color_info = Bool(True, help=
248 247 """
249 248 Use colors for displaying information about objects. Because this
250 249 information is passed through a pager (like 'less'), and some pagers
251 250 get confused with color codes, this capability can be turned off.
252 251 """
253 252 ).tag(config=True)
254 253 colors = CaselessStrEnum(('Neutral', 'NoColor','LightBG','Linux'),
255 254 default_value='Neutral',
256 255 help="Set the color scheme (NoColor, Neutral, Linux, or LightBG)."
257 256 ).tag(config=True)
258 257 debug = Bool(False).tag(config=True)
259 258 disable_failing_post_execute = Bool(False,
260 259 help="Don't call post-execute functions that have failed in the past."
261 260 ).tag(config=True)
262 261 display_formatter = Instance(DisplayFormatter, allow_none=True)
263 262 displayhook_class = Type(DisplayHook)
264 263 display_pub_class = Type(DisplayPublisher)
265 264
266 265 sphinxify_docstring = Bool(False, help=
267 266 """
268 267 Enables rich html representation of docstrings. (This requires the
269 268 docrepr module).
270 269 """).tag(config=True)
271 270
272 271 @observe("sphinxify_docstring")
273 272 def _sphinxify_docstring_changed(self, change):
274 273 if change['new']:
275 274 warn("`sphinxify_docstring` is provisional since IPython 5.0 and might change in future versions." , ProvisionalWarning)
276 275
277 276 enable_html_pager = Bool(False, help=
278 277 """
279 278 (Provisional API) enables html representation in mime bundles sent
280 279 to pagers.
281 280 """).tag(config=True)
282 281
283 282 @observe("enable_html_pager")
284 283 def _enable_html_pager_changed(self, change):
285 284 if change['new']:
286 285 warn("`enable_html_pager` is provisional since IPython 5.0 and might change in future versions.", ProvisionalWarning)
287 286
288 287 data_pub_class = None
289 288
290 289 exit_now = Bool(False)
291 290 exiter = Instance(ExitAutocall)
292 291 @default('exiter')
293 292 def _exiter_default(self):
294 293 return ExitAutocall(self)
295 294 # Monotonically increasing execution counter
296 295 execution_count = Integer(1)
297 296 filename = Unicode("<ipython console>")
298 297 ipython_dir= Unicode('').tag(config=True) # Set to get_ipython_dir() in __init__
299 298
300 299 # Input splitter, to transform input line by line and detect when a block
301 300 # is ready to be executed.
302 301 input_splitter = Instance('IPython.core.inputsplitter.IPythonInputSplitter',
303 302 (), {'line_input_checker': True})
304 303
305 304 # This InputSplitter instance is used to transform completed cells before
306 305 # running them. It allows cell magics to contain blank lines.
307 306 input_transformer_manager = Instance('IPython.core.inputsplitter.IPythonInputSplitter',
308 307 (), {'line_input_checker': False})
309 308
310 309 logstart = Bool(False, help=
311 310 """
312 311 Start logging to the default log file in overwrite mode.
313 312 Use `logappend` to specify a log file to **append** logs to.
314 313 """
315 314 ).tag(config=True)
316 315 logfile = Unicode('', help=
317 316 """
318 317 The name of the logfile to use.
319 318 """
320 319 ).tag(config=True)
321 320 logappend = Unicode('', help=
322 321 """
323 322 Start logging to the given file in append mode.
324 323 Use `logfile` to specify a log file to **overwrite** logs to.
325 324 """
326 325 ).tag(config=True)
327 326 object_info_string_level = Enum((0,1,2), default_value=0,
328 327 ).tag(config=True)
329 328 pdb = Bool(False, help=
330 329 """
331 330 Automatically call the pdb debugger after every exception.
332 331 """
333 332 ).tag(config=True)
334 333 display_page = Bool(False,
335 334 help="""If True, anything that would be passed to the pager
336 335 will be displayed as regular output instead."""
337 336 ).tag(config=True)
338 337
339 338 # deprecated prompt traits:
340 339
341 340 prompt_in1 = Unicode('In [\\#]: ',
342 341 help="Deprecated since IPython 4.0 and ignored since 5.0, set TerminalInteractiveShell.prompts object directly."
343 342 ).tag(config=True)
344 343 prompt_in2 = Unicode(' .\\D.: ',
345 344 help="Deprecated since IPython 4.0 and ignored since 5.0, set TerminalInteractiveShell.prompts object directly."
346 345 ).tag(config=True)
347 346 prompt_out = Unicode('Out[\\#]: ',
348 347 help="Deprecated since IPython 4.0 and ignored since 5.0, set TerminalInteractiveShell.prompts object directly."
349 348 ).tag(config=True)
350 349 prompts_pad_left = Bool(True,
351 350 help="Deprecated since IPython 4.0 and ignored since 5.0, set TerminalInteractiveShell.prompts object directly."
352 351 ).tag(config=True)
353 352
354 353 @observe('prompt_in1', 'prompt_in2', 'prompt_out', 'prompt_pad_left')
355 354 def _prompt_trait_changed(self, change):
356 355 name = change['name']
357 356 warn("InteractiveShell.{name} is deprecated since IPython 4.0"
358 357 " and ignored since 5.0, set TerminalInteractiveShell.prompts"
359 358 " object directly.".format(name=name))
360 359
361 360 # protect against weird cases where self.config may not exist:
362 361
363 362 show_rewritten_input = Bool(True,
364 363 help="Show rewritten input, e.g. for autocall."
365 364 ).tag(config=True)
366 365
367 366 quiet = Bool(False).tag(config=True)
368 367
369 368 history_length = Integer(10000,
370 369 help='Total length of command history'
371 370 ).tag(config=True)
372 371
373 372 history_load_length = Integer(1000, help=
374 373 """
375 374 The number of saved history entries to be loaded
376 375 into the history buffer at startup.
377 376 """
378 377 ).tag(config=True)
379 378
380 379 ast_node_interactivity = Enum(['all', 'last', 'last_expr', 'none'],
381 380 default_value='last_expr',
382 381 help="""
383 382 'all', 'last', 'last_expr' or 'none', specifying which nodes should be
384 383 run interactively (displaying output from expressions)."""
385 384 ).tag(config=True)
386 385
387 386 # TODO: this part of prompt management should be moved to the frontends.
388 387 # Use custom TraitTypes that convert '0'->'' and '\\n'->'\n'
389 388 separate_in = SeparateUnicode('\n').tag(config=True)
390 389 separate_out = SeparateUnicode('').tag(config=True)
391 390 separate_out2 = SeparateUnicode('').tag(config=True)
392 391 wildcards_case_sensitive = Bool(True).tag(config=True)
393 392 xmode = CaselessStrEnum(('Context','Plain', 'Verbose'),
394 393 default_value='Context').tag(config=True)
395 394
396 395 # Subcomponents of InteractiveShell
397 396 alias_manager = Instance('IPython.core.alias.AliasManager', allow_none=True)
398 397 prefilter_manager = Instance('IPython.core.prefilter.PrefilterManager', allow_none=True)
399 398 builtin_trap = Instance('IPython.core.builtin_trap.BuiltinTrap', allow_none=True)
400 399 display_trap = Instance('IPython.core.display_trap.DisplayTrap', allow_none=True)
401 400 extension_manager = Instance('IPython.core.extensions.ExtensionManager', allow_none=True)
402 401 payload_manager = Instance('IPython.core.payload.PayloadManager', allow_none=True)
403 402 history_manager = Instance('IPython.core.history.HistoryAccessorBase', allow_none=True)
404 403 magics_manager = Instance('IPython.core.magic.MagicsManager', allow_none=True)
405 404
406 405 profile_dir = Instance('IPython.core.application.ProfileDir', allow_none=True)
407 406 @property
408 407 def profile(self):
409 408 if self.profile_dir is not None:
410 409 name = os.path.basename(self.profile_dir.location)
411 410 return name.replace('profile_','')
412 411
413 412
414 413 # Private interface
415 414 _post_execute = Dict()
416 415
417 416 # Tracks any GUI loop loaded for pylab
418 417 pylab_gui_select = None
419 418
420 419 last_execution_succeeded = Bool(True, help='Did last executed command succeeded')
421 420
422 421 def __init__(self, ipython_dir=None, profile_dir=None,
423 422 user_module=None, user_ns=None,
424 423 custom_exceptions=((), None), **kwargs):
425 424
426 425 # This is where traits with a config_key argument are updated
427 426 # from the values on config.
428 427 super(InteractiveShell, self).__init__(**kwargs)
429 428 if 'PromptManager' in self.config:
430 429 warn('As of IPython 5.0 `PromptManager` config will have no effect'
431 430 ' and has been replaced by TerminalInteractiveShell.prompts_class')
432 431 self.configurables = [self]
433 432
434 433 # These are relatively independent and stateless
435 434 self.init_ipython_dir(ipython_dir)
436 435 self.init_profile_dir(profile_dir)
437 436 self.init_instance_attrs()
438 437 self.init_environment()
439 438
440 439 # Check if we're in a virtualenv, and set up sys.path.
441 440 self.init_virtualenv()
442 441
443 442 # Create namespaces (user_ns, user_global_ns, etc.)
444 443 self.init_create_namespaces(user_module, user_ns)
445 444 # This has to be done after init_create_namespaces because it uses
446 445 # something in self.user_ns, but before init_sys_modules, which
447 446 # is the first thing to modify sys.
448 447 # TODO: When we override sys.stdout and sys.stderr before this class
449 448 # is created, we are saving the overridden ones here. Not sure if this
450 449 # is what we want to do.
451 450 self.save_sys_module_state()
452 451 self.init_sys_modules()
453 452
454 453 # While we're trying to have each part of the code directly access what
455 454 # it needs without keeping redundant references to objects, we have too
456 455 # much legacy code that expects ip.db to exist.
457 456 self.db = PickleShareDB(os.path.join(self.profile_dir.location, 'db'))
458 457
459 458 self.init_history()
460 459 self.init_encoding()
461 460 self.init_prefilter()
462 461
463 462 self.init_syntax_highlighting()
464 463 self.init_hooks()
465 464 self.init_events()
466 465 self.init_pushd_popd_magic()
467 466 self.init_user_ns()
468 467 self.init_logger()
469 468 self.init_builtins()
470 469
471 470 # The following was in post_config_initialization
472 471 self.init_inspector()
473 472 self.raw_input_original = input
474 473 self.init_completer()
475 474 # TODO: init_io() needs to happen before init_traceback handlers
476 475 # because the traceback handlers hardcode the stdout/stderr streams.
477 476 # This logic in in debugger.Pdb and should eventually be changed.
478 477 self.init_io()
479 478 self.init_traceback_handlers(custom_exceptions)
480 479 self.init_prompts()
481 480 self.init_display_formatter()
482 481 self.init_display_pub()
483 482 self.init_data_pub()
484 483 self.init_displayhook()
485 484 self.init_magics()
486 485 self.init_alias()
487 486 self.init_logstart()
488 487 self.init_pdb()
489 488 self.init_extension_manager()
490 489 self.init_payload()
491 490 self.init_deprecation_warnings()
492 491 self.hooks.late_startup_hook()
493 492 self.events.trigger('shell_initialized', self)
494 493 atexit.register(self.atexit_operations)
495 494
496 495 def get_ipython(self):
497 496 """Return the currently running IPython instance."""
498 497 return self
499 498
500 499 #-------------------------------------------------------------------------
501 500 # Trait changed handlers
502 501 #-------------------------------------------------------------------------
503 502 @observe('ipython_dir')
504 503 def _ipython_dir_changed(self, change):
505 504 ensure_dir_exists(change['new'])
506 505
507 506 def set_autoindent(self,value=None):
508 507 """Set the autoindent flag.
509 508
510 509 If called with no arguments, it acts as a toggle."""
511 510 if value is None:
512 511 self.autoindent = not self.autoindent
513 512 else:
514 513 self.autoindent = value
515 514
516 515 #-------------------------------------------------------------------------
517 516 # init_* methods called by __init__
518 517 #-------------------------------------------------------------------------
519 518
520 519 def init_ipython_dir(self, ipython_dir):
521 520 if ipython_dir is not None:
522 521 self.ipython_dir = ipython_dir
523 522 return
524 523
525 524 self.ipython_dir = get_ipython_dir()
526 525
527 526 def init_profile_dir(self, profile_dir):
528 527 if profile_dir is not None:
529 528 self.profile_dir = profile_dir
530 529 return
531 530 self.profile_dir =\
532 531 ProfileDir.create_profile_dir_by_name(self.ipython_dir, 'default')
533 532
534 533 def init_instance_attrs(self):
535 534 self.more = False
536 535
537 536 # command compiler
538 537 self.compile = CachingCompiler()
539 538
540 539 # Make an empty namespace, which extension writers can rely on both
541 540 # existing and NEVER being used by ipython itself. This gives them a
542 541 # convenient location for storing additional information and state
543 542 # their extensions may require, without fear of collisions with other
544 543 # ipython names that may develop later.
545 544 self.meta = Struct()
546 545
547 546 # Temporary files used for various purposes. Deleted at exit.
548 547 self.tempfiles = []
549 548 self.tempdirs = []
550 549
551 550 # keep track of where we started running (mainly for crash post-mortem)
552 551 # This is not being used anywhere currently.
553 552 self.starting_dir = py3compat.getcwd()
554 553
555 554 # Indentation management
556 555 self.indent_current_nsp = 0
557 556
558 557 # Dict to track post-execution functions that have been registered
559 558 self._post_execute = {}
560 559
561 560 def init_environment(self):
562 561 """Any changes we need to make to the user's environment."""
563 562 pass
564 563
565 564 def init_encoding(self):
566 565 # Get system encoding at startup time. Certain terminals (like Emacs
567 566 # under Win32 have it set to None, and we need to have a known valid
568 567 # encoding to use in the raw_input() method
569 568 try:
570 569 self.stdin_encoding = sys.stdin.encoding or 'ascii'
571 570 except AttributeError:
572 571 self.stdin_encoding = 'ascii'
573 572
574 573
575 574 @observe('colors')
576 575 def init_syntax_highlighting(self, changes=None):
577 576 # Python source parser/formatter for syntax highlighting
578 577 pyformat = PyColorize.Parser(style=self.colors, parent=self).format
579 578 self.pycolorize = lambda src: pyformat(src,'str')
580 579
581 580 def refresh_style(self):
582 581 # No-op here, used in subclass
583 582 pass
584 583
585 584 def init_pushd_popd_magic(self):
586 585 # for pushd/popd management
587 586 self.home_dir = get_home_dir()
588 587
589 588 self.dir_stack = []
590 589
591 590 def init_logger(self):
592 591 self.logger = Logger(self.home_dir, logfname='ipython_log.py',
593 592 logmode='rotate')
594 593
595 594 def init_logstart(self):
596 595 """Initialize logging in case it was requested at the command line.
597 596 """
598 597 if self.logappend:
599 598 self.magic('logstart %s append' % self.logappend)
600 599 elif self.logfile:
601 600 self.magic('logstart %s' % self.logfile)
602 601 elif self.logstart:
603 602 self.magic('logstart')
604 603
605 604 def init_deprecation_warnings(self):
606 605 """
607 606 register default filter for deprecation warning.
608 607
609 608 This will allow deprecation warning of function used interactively to show
610 609 warning to users, and still hide deprecation warning from libraries import.
611 610 """
612 611 warnings.filterwarnings("default", category=DeprecationWarning, module=self.user_ns.get("__name__"))
613 612
614 613 def init_builtins(self):
615 614 # A single, static flag that we set to True. Its presence indicates
616 615 # that an IPython shell has been created, and we make no attempts at
617 616 # removing on exit or representing the existence of more than one
618 617 # IPython at a time.
619 618 builtin_mod.__dict__['__IPYTHON__'] = True
620 619
621 620 self.builtin_trap = BuiltinTrap(shell=self)
622 621
623 622 def init_inspector(self):
624 623 # Object inspector
625 624 self.inspector = oinspect.Inspector(oinspect.InspectColors,
626 625 PyColorize.ANSICodeColors,
627 626 'NoColor',
628 627 self.object_info_string_level)
629 628
630 629 def init_io(self):
631 630 # This will just use sys.stdout and sys.stderr. If you want to
632 631 # override sys.stdout and sys.stderr themselves, you need to do that
633 632 # *before* instantiating this class, because io holds onto
634 633 # references to the underlying streams.
635 634 # io.std* are deprecated, but don't show our own deprecation warnings
636 635 # during initialization of the deprecated API.
637 636 with warnings.catch_warnings():
638 637 warnings.simplefilter('ignore', DeprecationWarning)
639 638 io.stdout = io.IOStream(sys.stdout)
640 639 io.stderr = io.IOStream(sys.stderr)
641 640
642 641 def init_prompts(self):
643 642 # Set system prompts, so that scripts can decide if they are running
644 643 # interactively.
645 644 sys.ps1 = 'In : '
646 645 sys.ps2 = '...: '
647 646 sys.ps3 = 'Out: '
648 647
649 648 def init_display_formatter(self):
650 649 self.display_formatter = DisplayFormatter(parent=self)
651 650 self.configurables.append(self.display_formatter)
652 651
653 652 def init_display_pub(self):
654 653 self.display_pub = self.display_pub_class(parent=self)
655 654 self.configurables.append(self.display_pub)
656 655
657 656 def init_data_pub(self):
658 657 if not self.data_pub_class:
659 658 self.data_pub = None
660 659 return
661 660 self.data_pub = self.data_pub_class(parent=self)
662 661 self.configurables.append(self.data_pub)
663 662
664 663 def init_displayhook(self):
665 664 # Initialize displayhook, set in/out prompts and printing system
666 665 self.displayhook = self.displayhook_class(
667 666 parent=self,
668 667 shell=self,
669 668 cache_size=self.cache_size,
670 669 )
671 670 self.configurables.append(self.displayhook)
672 671 # This is a context manager that installs/revmoes the displayhook at
673 672 # the appropriate time.
674 673 self.display_trap = DisplayTrap(hook=self.displayhook)
675 674
676 675 def init_virtualenv(self):
677 676 """Add a virtualenv to sys.path so the user can import modules from it.
678 677 This isn't perfect: it doesn't use the Python interpreter with which the
679 678 virtualenv was built, and it ignores the --no-site-packages option. A
680 679 warning will appear suggesting the user installs IPython in the
681 680 virtualenv, but for many cases, it probably works well enough.
682 681
683 682 Adapted from code snippets online.
684 683
685 684 http://blog.ufsoft.org/2009/1/29/ipython-and-virtualenv
686 685 """
687 686 if 'VIRTUAL_ENV' not in os.environ:
688 687 # Not in a virtualenv
689 688 return
690 689
691 690 # venv detection:
692 691 # stdlib venv may symlink sys.executable, so we can't use realpath.
693 692 # but others can symlink *to* the venv Python, so we can't just use sys.executable.
694 693 # So we just check every item in the symlink tree (generally <= 3)
695 694 p = os.path.normcase(sys.executable)
696 695 paths = [p]
697 696 while os.path.islink(p):
698 697 p = os.path.normcase(os.path.join(os.path.dirname(p), os.readlink(p)))
699 698 paths.append(p)
700 699 p_venv = os.path.normcase(os.environ['VIRTUAL_ENV'])
701 700 if any(p.startswith(p_venv) for p in paths):
702 701 # Running properly in the virtualenv, don't need to do anything
703 702 return
704 703
705 704 warn("Attempting to work in a virtualenv. If you encounter problems, please "
706 705 "install IPython inside the virtualenv.")
707 706 if sys.platform == "win32":
708 707 virtual_env = os.path.join(os.environ['VIRTUAL_ENV'], 'Lib', 'site-packages')
709 708 else:
710 709 virtual_env = os.path.join(os.environ['VIRTUAL_ENV'], 'lib',
711 710 'python%d.%d' % sys.version_info[:2], 'site-packages')
712 711
713 712 import site
714 713 sys.path.insert(0, virtual_env)
715 714 site.addsitedir(virtual_env)
716 715
717 716 #-------------------------------------------------------------------------
718 717 # Things related to injections into the sys module
719 718 #-------------------------------------------------------------------------
720 719
721 720 def save_sys_module_state(self):
722 721 """Save the state of hooks in the sys module.
723 722
724 723 This has to be called after self.user_module is created.
725 724 """
726 725 self._orig_sys_module_state = {'stdin': sys.stdin,
727 726 'stdout': sys.stdout,
728 727 'stderr': sys.stderr,
729 728 'excepthook': sys.excepthook}
730 729 self._orig_sys_modules_main_name = self.user_module.__name__
731 730 self._orig_sys_modules_main_mod = sys.modules.get(self.user_module.__name__)
732 731
733 732 def restore_sys_module_state(self):
734 733 """Restore the state of the sys module."""
735 734 try:
736 735 for k, v in self._orig_sys_module_state.items():
737 736 setattr(sys, k, v)
738 737 except AttributeError:
739 738 pass
740 739 # Reset what what done in self.init_sys_modules
741 740 if self._orig_sys_modules_main_mod is not None:
742 741 sys.modules[self._orig_sys_modules_main_name] = self._orig_sys_modules_main_mod
743 742
744 743 #-------------------------------------------------------------------------
745 744 # Things related to the banner
746 745 #-------------------------------------------------------------------------
747 746
748 747 @property
749 748 def banner(self):
750 749 banner = self.banner1
751 750 if self.profile and self.profile != 'default':
752 751 banner += '\nIPython profile: %s\n' % self.profile
753 752 if self.banner2:
754 753 banner += '\n' + self.banner2
755 754 return banner
756 755
757 756 def show_banner(self, banner=None):
758 757 if banner is None:
759 758 banner = self.banner
760 759 sys.stdout.write(banner)
761 760
762 761 #-------------------------------------------------------------------------
763 762 # Things related to hooks
764 763 #-------------------------------------------------------------------------
765 764
766 765 def init_hooks(self):
767 766 # hooks holds pointers used for user-side customizations
768 767 self.hooks = Struct()
769 768
770 769 self.strdispatchers = {}
771 770
772 771 # Set all default hooks, defined in the IPython.hooks module.
773 772 hooks = IPython.core.hooks
774 773 for hook_name in hooks.__all__:
775 774 # default hooks have priority 100, i.e. low; user hooks should have
776 775 # 0-100 priority
777 776 self.set_hook(hook_name,getattr(hooks,hook_name), 100, _warn_deprecated=False)
778 777
779 778 if self.display_page:
780 779 self.set_hook('show_in_pager', page.as_hook(page.display_page), 90)
781 780
782 781 def set_hook(self,name,hook, priority=50, str_key=None, re_key=None,
783 782 _warn_deprecated=True):
784 783 """set_hook(name,hook) -> sets an internal IPython hook.
785 784
786 785 IPython exposes some of its internal API as user-modifiable hooks. By
787 786 adding your function to one of these hooks, you can modify IPython's
788 787 behavior to call at runtime your own routines."""
789 788
790 789 # At some point in the future, this should validate the hook before it
791 790 # accepts it. Probably at least check that the hook takes the number
792 791 # of args it's supposed to.
793 792
794 793 f = types.MethodType(hook,self)
795 794
796 795 # check if the hook is for strdispatcher first
797 796 if str_key is not None:
798 797 sdp = self.strdispatchers.get(name, StrDispatch())
799 798 sdp.add_s(str_key, f, priority )
800 799 self.strdispatchers[name] = sdp
801 800 return
802 801 if re_key is not None:
803 802 sdp = self.strdispatchers.get(name, StrDispatch())
804 803 sdp.add_re(re.compile(re_key), f, priority )
805 804 self.strdispatchers[name] = sdp
806 805 return
807 806
808 807 dp = getattr(self.hooks, name, None)
809 808 if name not in IPython.core.hooks.__all__:
810 809 print("Warning! Hook '%s' is not one of %s" % \
811 810 (name, IPython.core.hooks.__all__ ))
812 811
813 812 if _warn_deprecated and (name in IPython.core.hooks.deprecated):
814 813 alternative = IPython.core.hooks.deprecated[name]
815 814 warn("Hook {} is deprecated. Use {} instead.".format(name, alternative), stacklevel=2)
816 815
817 816 if not dp:
818 817 dp = IPython.core.hooks.CommandChainDispatcher()
819 818
820 819 try:
821 820 dp.add(f,priority)
822 821 except AttributeError:
823 822 # it was not commandchain, plain old func - replace
824 823 dp = f
825 824
826 825 setattr(self.hooks,name, dp)
827 826
828 827 #-------------------------------------------------------------------------
829 828 # Things related to events
830 829 #-------------------------------------------------------------------------
831 830
832 831 def init_events(self):
833 832 self.events = EventManager(self, available_events)
834 833
835 834 self.events.register("pre_execute", self._clear_warning_registry)
836 835
837 836 def register_post_execute(self, func):
838 837 """DEPRECATED: Use ip.events.register('post_run_cell', func)
839 838
840 839 Register a function for calling after code execution.
841 840 """
842 841 warn("ip.register_post_execute is deprecated, use "
843 842 "ip.events.register('post_run_cell', func) instead.", stacklevel=2)
844 843 self.events.register('post_run_cell', func)
845 844
846 845 def _clear_warning_registry(self):
847 846 # clear the warning registry, so that different code blocks with
848 847 # overlapping line number ranges don't cause spurious suppression of
849 848 # warnings (see gh-6611 for details)
850 849 if "__warningregistry__" in self.user_global_ns:
851 850 del self.user_global_ns["__warningregistry__"]
852 851
853 852 #-------------------------------------------------------------------------
854 853 # Things related to the "main" module
855 854 #-------------------------------------------------------------------------
856 855
857 856 def new_main_mod(self, filename, modname):
858 857 """Return a new 'main' module object for user code execution.
859 858
860 859 ``filename`` should be the path of the script which will be run in the
861 860 module. Requests with the same filename will get the same module, with
862 861 its namespace cleared.
863 862
864 863 ``modname`` should be the module name - normally either '__main__' or
865 864 the basename of the file without the extension.
866 865
867 866 When scripts are executed via %run, we must keep a reference to their
868 867 __main__ module around so that Python doesn't
869 868 clear it, rendering references to module globals useless.
870 869
871 870 This method keeps said reference in a private dict, keyed by the
872 871 absolute path of the script. This way, for multiple executions of the
873 872 same script we only keep one copy of the namespace (the last one),
874 873 thus preventing memory leaks from old references while allowing the
875 874 objects from the last execution to be accessible.
876 875 """
877 876 filename = os.path.abspath(filename)
878 877 try:
879 878 main_mod = self._main_mod_cache[filename]
880 879 except KeyError:
881 880 main_mod = self._main_mod_cache[filename] = types.ModuleType(
882 881 py3compat.cast_bytes_py2(modname),
883 882 doc="Module created for script run in IPython")
884 883 else:
885 884 main_mod.__dict__.clear()
886 885 main_mod.__name__ = modname
887 886
888 887 main_mod.__file__ = filename
889 888 # It seems pydoc (and perhaps others) needs any module instance to
890 889 # implement a __nonzero__ method
891 890 main_mod.__nonzero__ = lambda : True
892 891
893 892 return main_mod
894 893
895 894 def clear_main_mod_cache(self):
896 895 """Clear the cache of main modules.
897 896
898 897 Mainly for use by utilities like %reset.
899 898
900 899 Examples
901 900 --------
902 901
903 902 In [15]: import IPython
904 903
905 904 In [16]: m = _ip.new_main_mod(IPython.__file__, 'IPython')
906 905
907 906 In [17]: len(_ip._main_mod_cache) > 0
908 907 Out[17]: True
909 908
910 909 In [18]: _ip.clear_main_mod_cache()
911 910
912 911 In [19]: len(_ip._main_mod_cache) == 0
913 912 Out[19]: True
914 913 """
915 914 self._main_mod_cache.clear()
916 915
917 916 #-------------------------------------------------------------------------
918 917 # Things related to debugging
919 918 #-------------------------------------------------------------------------
920 919
921 920 def init_pdb(self):
922 921 # Set calling of pdb on exceptions
923 922 # self.call_pdb is a property
924 923 self.call_pdb = self.pdb
925 924
926 925 def _get_call_pdb(self):
927 926 return self._call_pdb
928 927
929 928 def _set_call_pdb(self,val):
930 929
931 930 if val not in (0,1,False,True):
932 931 raise ValueError('new call_pdb value must be boolean')
933 932
934 933 # store value in instance
935 934 self._call_pdb = val
936 935
937 936 # notify the actual exception handlers
938 937 self.InteractiveTB.call_pdb = val
939 938
940 939 call_pdb = property(_get_call_pdb,_set_call_pdb,None,
941 940 'Control auto-activation of pdb at exceptions')
942 941
943 942 def debugger(self,force=False):
944 943 """Call the pdb debugger.
945 944
946 945 Keywords:
947 946
948 947 - force(False): by default, this routine checks the instance call_pdb
949 948 flag and does not actually invoke the debugger if the flag is false.
950 949 The 'force' option forces the debugger to activate even if the flag
951 950 is false.
952 951 """
953 952
954 953 if not (force or self.call_pdb):
955 954 return
956 955
957 956 if not hasattr(sys,'last_traceback'):
958 957 error('No traceback has been produced, nothing to debug.')
959 958 return
960 959
961 960 self.InteractiveTB.debugger(force=True)
962 961
963 962 #-------------------------------------------------------------------------
964 963 # Things related to IPython's various namespaces
965 964 #-------------------------------------------------------------------------
966 965 default_user_namespaces = True
967 966
968 967 def init_create_namespaces(self, user_module=None, user_ns=None):
969 968 # Create the namespace where the user will operate. user_ns is
970 969 # normally the only one used, and it is passed to the exec calls as
971 970 # the locals argument. But we do carry a user_global_ns namespace
972 971 # given as the exec 'globals' argument, This is useful in embedding
973 972 # situations where the ipython shell opens in a context where the
974 973 # distinction between locals and globals is meaningful. For
975 974 # non-embedded contexts, it is just the same object as the user_ns dict.
976 975
977 976 # FIXME. For some strange reason, __builtins__ is showing up at user
978 977 # level as a dict instead of a module. This is a manual fix, but I
979 978 # should really track down where the problem is coming from. Alex
980 979 # Schmolck reported this problem first.
981 980
982 981 # A useful post by Alex Martelli on this topic:
983 982 # Re: inconsistent value from __builtins__
984 983 # Von: Alex Martelli <aleaxit@yahoo.com>
985 984 # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends
986 985 # Gruppen: comp.lang.python
987 986
988 987 # Michael Hohn <hohn@hooknose.lbl.gov> wrote:
989 988 # > >>> print type(builtin_check.get_global_binding('__builtins__'))
990 989 # > <type 'dict'>
991 990 # > >>> print type(__builtins__)
992 991 # > <type 'module'>
993 992 # > Is this difference in return value intentional?
994 993
995 994 # Well, it's documented that '__builtins__' can be either a dictionary
996 995 # or a module, and it's been that way for a long time. Whether it's
997 996 # intentional (or sensible), I don't know. In any case, the idea is
998 997 # that if you need to access the built-in namespace directly, you
999 998 # should start with "import __builtin__" (note, no 's') which will
1000 999 # definitely give you a module. Yeah, it's somewhat confusing:-(.
1001 1000
1002 1001 # These routines return a properly built module and dict as needed by
1003 1002 # the rest of the code, and can also be used by extension writers to
1004 1003 # generate properly initialized namespaces.
1005 1004 if (user_ns is not None) or (user_module is not None):
1006 1005 self.default_user_namespaces = False
1007 1006 self.user_module, self.user_ns = self.prepare_user_module(user_module, user_ns)
1008 1007
1009 1008 # A record of hidden variables we have added to the user namespace, so
1010 1009 # we can list later only variables defined in actual interactive use.
1011 1010 self.user_ns_hidden = {}
1012 1011
1013 1012 # Now that FakeModule produces a real module, we've run into a nasty
1014 1013 # problem: after script execution (via %run), the module where the user
1015 1014 # code ran is deleted. Now that this object is a true module (needed
1016 1015 # so doctest and other tools work correctly), the Python module
1017 1016 # teardown mechanism runs over it, and sets to None every variable
1018 1017 # present in that module. Top-level references to objects from the
1019 1018 # script survive, because the user_ns is updated with them. However,
1020 1019 # calling functions defined in the script that use other things from
1021 1020 # the script will fail, because the function's closure had references
1022 1021 # to the original objects, which are now all None. So we must protect
1023 1022 # these modules from deletion by keeping a cache.
1024 1023 #
1025 1024 # To avoid keeping stale modules around (we only need the one from the
1026 1025 # last run), we use a dict keyed with the full path to the script, so
1027 1026 # only the last version of the module is held in the cache. Note,
1028 1027 # however, that we must cache the module *namespace contents* (their
1029 1028 # __dict__). Because if we try to cache the actual modules, old ones
1030 1029 # (uncached) could be destroyed while still holding references (such as
1031 1030 # those held by GUI objects that tend to be long-lived)>
1032 1031 #
1033 1032 # The %reset command will flush this cache. See the cache_main_mod()
1034 1033 # and clear_main_mod_cache() methods for details on use.
1035 1034
1036 1035 # This is the cache used for 'main' namespaces
1037 1036 self._main_mod_cache = {}
1038 1037
1039 1038 # A table holding all the namespaces IPython deals with, so that
1040 1039 # introspection facilities can search easily.
1041 1040 self.ns_table = {'user_global':self.user_module.__dict__,
1042 1041 'user_local':self.user_ns,
1043 1042 'builtin':builtin_mod.__dict__
1044 1043 }
1045 1044
1046 1045 @property
1047 1046 def user_global_ns(self):
1048 1047 return self.user_module.__dict__
1049 1048
1050 1049 def prepare_user_module(self, user_module=None, user_ns=None):
1051 1050 """Prepare the module and namespace in which user code will be run.
1052 1051
1053 1052 When IPython is started normally, both parameters are None: a new module
1054 1053 is created automatically, and its __dict__ used as the namespace.
1055 1054
1056 1055 If only user_module is provided, its __dict__ is used as the namespace.
1057 1056 If only user_ns is provided, a dummy module is created, and user_ns
1058 1057 becomes the global namespace. If both are provided (as they may be
1059 1058 when embedding), user_ns is the local namespace, and user_module
1060 1059 provides the global namespace.
1061 1060
1062 1061 Parameters
1063 1062 ----------
1064 1063 user_module : module, optional
1065 1064 The current user module in which IPython is being run. If None,
1066 1065 a clean module will be created.
1067 1066 user_ns : dict, optional
1068 1067 A namespace in which to run interactive commands.
1069 1068
1070 1069 Returns
1071 1070 -------
1072 1071 A tuple of user_module and user_ns, each properly initialised.
1073 1072 """
1074 1073 if user_module is None and user_ns is not None:
1075 1074 user_ns.setdefault("__name__", "__main__")
1076 1075 user_module = DummyMod()
1077 1076 user_module.__dict__ = user_ns
1078 1077
1079 1078 if user_module is None:
1080 1079 user_module = types.ModuleType("__main__",
1081 1080 doc="Automatically created module for IPython interactive environment")
1082 1081
1083 1082 # We must ensure that __builtin__ (without the final 's') is always
1084 1083 # available and pointing to the __builtin__ *module*. For more details:
1085 1084 # http://mail.python.org/pipermail/python-dev/2001-April/014068.html
1086 1085 user_module.__dict__.setdefault('__builtin__', builtin_mod)
1087 1086 user_module.__dict__.setdefault('__builtins__', builtin_mod)
1088 1087
1089 1088 if user_ns is None:
1090 1089 user_ns = user_module.__dict__
1091 1090
1092 1091 return user_module, user_ns
1093 1092
1094 1093 def init_sys_modules(self):
1095 1094 # We need to insert into sys.modules something that looks like a
1096 1095 # module but which accesses the IPython namespace, for shelve and
1097 1096 # pickle to work interactively. Normally they rely on getting
1098 1097 # everything out of __main__, but for embedding purposes each IPython
1099 1098 # instance has its own private namespace, so we can't go shoving
1100 1099 # everything into __main__.
1101 1100
1102 1101 # note, however, that we should only do this for non-embedded
1103 1102 # ipythons, which really mimic the __main__.__dict__ with their own
1104 1103 # namespace. Embedded instances, on the other hand, should not do
1105 1104 # this because they need to manage the user local/global namespaces
1106 1105 # only, but they live within a 'normal' __main__ (meaning, they
1107 1106 # shouldn't overtake the execution environment of the script they're
1108 1107 # embedded in).
1109 1108
1110 1109 # This is overridden in the InteractiveShellEmbed subclass to a no-op.
1111 1110 main_name = self.user_module.__name__
1112 1111 sys.modules[main_name] = self.user_module
1113 1112
1114 1113 def init_user_ns(self):
1115 1114 """Initialize all user-visible namespaces to their minimum defaults.
1116 1115
1117 1116 Certain history lists are also initialized here, as they effectively
1118 1117 act as user namespaces.
1119 1118
1120 1119 Notes
1121 1120 -----
1122 1121 All data structures here are only filled in, they are NOT reset by this
1123 1122 method. If they were not empty before, data will simply be added to
1124 1123 therm.
1125 1124 """
1126 1125 # This function works in two parts: first we put a few things in
1127 1126 # user_ns, and we sync that contents into user_ns_hidden so that these
1128 1127 # initial variables aren't shown by %who. After the sync, we add the
1129 1128 # rest of what we *do* want the user to see with %who even on a new
1130 1129 # session (probably nothing, so they really only see their own stuff)
1131 1130
1132 1131 # The user dict must *always* have a __builtin__ reference to the
1133 1132 # Python standard __builtin__ namespace, which must be imported.
1134 1133 # This is so that certain operations in prompt evaluation can be
1135 1134 # reliably executed with builtins. Note that we can NOT use
1136 1135 # __builtins__ (note the 's'), because that can either be a dict or a
1137 1136 # module, and can even mutate at runtime, depending on the context
1138 1137 # (Python makes no guarantees on it). In contrast, __builtin__ is
1139 1138 # always a module object, though it must be explicitly imported.
1140 1139
1141 1140 # For more details:
1142 1141 # http://mail.python.org/pipermail/python-dev/2001-April/014068.html
1143 1142 ns = dict()
1144 1143
1145 1144 # make global variables for user access to the histories
1146 1145 ns['_ih'] = self.history_manager.input_hist_parsed
1147 1146 ns['_oh'] = self.history_manager.output_hist
1148 1147 ns['_dh'] = self.history_manager.dir_hist
1149 1148
1150 1149 ns['_sh'] = shadowns
1151 1150
1152 1151 # user aliases to input and output histories. These shouldn't show up
1153 1152 # in %who, as they can have very large reprs.
1154 1153 ns['In'] = self.history_manager.input_hist_parsed
1155 1154 ns['Out'] = self.history_manager.output_hist
1156 1155
1157 1156 # Store myself as the public api!!!
1158 1157 ns['get_ipython'] = self.get_ipython
1159 1158
1160 1159 ns['exit'] = self.exiter
1161 1160 ns['quit'] = self.exiter
1162 1161
1163 1162 # Sync what we've added so far to user_ns_hidden so these aren't seen
1164 1163 # by %who
1165 1164 self.user_ns_hidden.update(ns)
1166 1165
1167 1166 # Anything put into ns now would show up in %who. Think twice before
1168 1167 # putting anything here, as we really want %who to show the user their
1169 1168 # stuff, not our variables.
1170 1169
1171 1170 # Finally, update the real user's namespace
1172 1171 self.user_ns.update(ns)
1173 1172
1174 1173 @property
1175 1174 def all_ns_refs(self):
1176 1175 """Get a list of references to all the namespace dictionaries in which
1177 1176 IPython might store a user-created object.
1178 1177
1179 1178 Note that this does not include the displayhook, which also caches
1180 1179 objects from the output."""
1181 1180 return [self.user_ns, self.user_global_ns, self.user_ns_hidden] + \
1182 1181 [m.__dict__ for m in self._main_mod_cache.values()]
1183 1182
1184 1183 def reset(self, new_session=True):
1185 1184 """Clear all internal namespaces, and attempt to release references to
1186 1185 user objects.
1187 1186
1188 1187 If new_session is True, a new history session will be opened.
1189 1188 """
1190 1189 # Clear histories
1191 1190 self.history_manager.reset(new_session)
1192 1191 # Reset counter used to index all histories
1193 1192 if new_session:
1194 1193 self.execution_count = 1
1195 1194
1196 1195 # Flush cached output items
1197 1196 if self.displayhook.do_full_cache:
1198 1197 self.displayhook.flush()
1199 1198
1200 1199 # The main execution namespaces must be cleared very carefully,
1201 1200 # skipping the deletion of the builtin-related keys, because doing so
1202 1201 # would cause errors in many object's __del__ methods.
1203 1202 if self.user_ns is not self.user_global_ns:
1204 1203 self.user_ns.clear()
1205 1204 ns = self.user_global_ns
1206 1205 drop_keys = set(ns.keys())
1207 1206 drop_keys.discard('__builtin__')
1208 1207 drop_keys.discard('__builtins__')
1209 1208 drop_keys.discard('__name__')
1210 1209 for k in drop_keys:
1211 1210 del ns[k]
1212 1211
1213 1212 self.user_ns_hidden.clear()
1214 1213
1215 1214 # Restore the user namespaces to minimal usability
1216 1215 self.init_user_ns()
1217 1216
1218 1217 # Restore the default and user aliases
1219 1218 self.alias_manager.clear_aliases()
1220 1219 self.alias_manager.init_aliases()
1221 1220
1222 1221 # Flush the private list of module references kept for script
1223 1222 # execution protection
1224 1223 self.clear_main_mod_cache()
1225 1224
1226 1225 def del_var(self, varname, by_name=False):
1227 1226 """Delete a variable from the various namespaces, so that, as
1228 1227 far as possible, we're not keeping any hidden references to it.
1229 1228
1230 1229 Parameters
1231 1230 ----------
1232 1231 varname : str
1233 1232 The name of the variable to delete.
1234 1233 by_name : bool
1235 1234 If True, delete variables with the given name in each
1236 1235 namespace. If False (default), find the variable in the user
1237 1236 namespace, and delete references to it.
1238 1237 """
1239 1238 if varname in ('__builtin__', '__builtins__'):
1240 1239 raise ValueError("Refusing to delete %s" % varname)
1241 1240
1242 1241 ns_refs = self.all_ns_refs
1243 1242
1244 1243 if by_name: # Delete by name
1245 1244 for ns in ns_refs:
1246 1245 try:
1247 1246 del ns[varname]
1248 1247 except KeyError:
1249 1248 pass
1250 1249 else: # Delete by object
1251 1250 try:
1252 1251 obj = self.user_ns[varname]
1253 1252 except KeyError:
1254 1253 raise NameError("name '%s' is not defined" % varname)
1255 1254 # Also check in output history
1256 1255 ns_refs.append(self.history_manager.output_hist)
1257 1256 for ns in ns_refs:
1258 1257 to_delete = [n for n, o in ns.items() if o is obj]
1259 1258 for name in to_delete:
1260 1259 del ns[name]
1261 1260
1262 1261 # displayhook keeps extra references, but not in a dictionary
1263 1262 for name in ('_', '__', '___'):
1264 1263 if getattr(self.displayhook, name) is obj:
1265 1264 setattr(self.displayhook, name, None)
1266 1265
1267 1266 def reset_selective(self, regex=None):
1268 1267 """Clear selective variables from internal namespaces based on a
1269 1268 specified regular expression.
1270 1269
1271 1270 Parameters
1272 1271 ----------
1273 1272 regex : string or compiled pattern, optional
1274 1273 A regular expression pattern that will be used in searching
1275 1274 variable names in the users namespaces.
1276 1275 """
1277 1276 if regex is not None:
1278 1277 try:
1279 1278 m = re.compile(regex)
1280 1279 except TypeError:
1281 1280 raise TypeError('regex must be a string or compiled pattern')
1282 1281 # Search for keys in each namespace that match the given regex
1283 1282 # If a match is found, delete the key/value pair.
1284 1283 for ns in self.all_ns_refs:
1285 1284 for var in ns:
1286 1285 if m.search(var):
1287 1286 del ns[var]
1288 1287
1289 1288 def push(self, variables, interactive=True):
1290 1289 """Inject a group of variables into the IPython user namespace.
1291 1290
1292 1291 Parameters
1293 1292 ----------
1294 1293 variables : dict, str or list/tuple of str
1295 1294 The variables to inject into the user's namespace. If a dict, a
1296 1295 simple update is done. If a str, the string is assumed to have
1297 1296 variable names separated by spaces. A list/tuple of str can also
1298 1297 be used to give the variable names. If just the variable names are
1299 1298 give (list/tuple/str) then the variable values looked up in the
1300 1299 callers frame.
1301 1300 interactive : bool
1302 1301 If True (default), the variables will be listed with the ``who``
1303 1302 magic.
1304 1303 """
1305 1304 vdict = None
1306 1305
1307 1306 # We need a dict of name/value pairs to do namespace updates.
1308 1307 if isinstance(variables, dict):
1309 1308 vdict = variables
1310 elif isinstance(variables, string_types+(list, tuple)):
1311 if isinstance(variables, string_types):
1309 elif isinstance(variables, (str, list, tuple)):
1310 if isinstance(variables, str):
1312 1311 vlist = variables.split()
1313 1312 else:
1314 1313 vlist = variables
1315 1314 vdict = {}
1316 1315 cf = sys._getframe(1)
1317 1316 for name in vlist:
1318 1317 try:
1319 1318 vdict[name] = eval(name, cf.f_globals, cf.f_locals)
1320 1319 except:
1321 1320 print('Could not get variable %s from %s' %
1322 1321 (name,cf.f_code.co_name))
1323 1322 else:
1324 1323 raise ValueError('variables must be a dict/str/list/tuple')
1325 1324
1326 1325 # Propagate variables to user namespace
1327 1326 self.user_ns.update(vdict)
1328 1327
1329 1328 # And configure interactive visibility
1330 1329 user_ns_hidden = self.user_ns_hidden
1331 1330 if interactive:
1332 1331 for name in vdict:
1333 1332 user_ns_hidden.pop(name, None)
1334 1333 else:
1335 1334 user_ns_hidden.update(vdict)
1336 1335
1337 1336 def drop_by_id(self, variables):
1338 1337 """Remove a dict of variables from the user namespace, if they are the
1339 1338 same as the values in the dictionary.
1340 1339
1341 1340 This is intended for use by extensions: variables that they've added can
1342 1341 be taken back out if they are unloaded, without removing any that the
1343 1342 user has overwritten.
1344 1343
1345 1344 Parameters
1346 1345 ----------
1347 1346 variables : dict
1348 1347 A dictionary mapping object names (as strings) to the objects.
1349 1348 """
1350 1349 for name, obj in variables.items():
1351 1350 if name in self.user_ns and self.user_ns[name] is obj:
1352 1351 del self.user_ns[name]
1353 1352 self.user_ns_hidden.pop(name, None)
1354 1353
1355 1354 #-------------------------------------------------------------------------
1356 1355 # Things related to object introspection
1357 1356 #-------------------------------------------------------------------------
1358 1357
1359 1358 def _ofind(self, oname, namespaces=None):
1360 1359 """Find an object in the available namespaces.
1361 1360
1362 1361 self._ofind(oname) -> dict with keys: found,obj,ospace,ismagic
1363 1362
1364 1363 Has special code to detect magic functions.
1365 1364 """
1366 1365 oname = oname.strip()
1367 1366 #print '1- oname: <%r>' % oname # dbg
1368 1367 if not oname.startswith(ESC_MAGIC) and \
1369 1368 not oname.startswith(ESC_MAGIC2) and \
1370 1369 not py3compat.isidentifier(oname, dotted=True):
1371 1370 return dict(found=False)
1372 1371
1373 1372 if namespaces is None:
1374 1373 # Namespaces to search in:
1375 1374 # Put them in a list. The order is important so that we
1376 1375 # find things in the same order that Python finds them.
1377 1376 namespaces = [ ('Interactive', self.user_ns),
1378 1377 ('Interactive (global)', self.user_global_ns),
1379 1378 ('Python builtin', builtin_mod.__dict__),
1380 1379 ]
1381 1380
1382 1381 # initialize results to 'null'
1383 1382 found = False; obj = None; ospace = None;
1384 1383 ismagic = False; isalias = False; parent = None
1385 1384
1386 1385 # We need to special-case 'print', which as of python2.6 registers as a
1387 1386 # function but should only be treated as one if print_function was
1388 1387 # loaded with a future import. In this case, just bail.
1389 1388 if (oname == 'print' and not py3compat.PY3 and not \
1390 1389 (self.compile.compiler_flags & __future__.CO_FUTURE_PRINT_FUNCTION)):
1391 1390 return {'found':found, 'obj':obj, 'namespace':ospace,
1392 1391 'ismagic':ismagic, 'isalias':isalias, 'parent':parent}
1393 1392
1394 1393 # Look for the given name by splitting it in parts. If the head is
1395 1394 # found, then we look for all the remaining parts as members, and only
1396 1395 # declare success if we can find them all.
1397 1396 oname_parts = oname.split('.')
1398 1397 oname_head, oname_rest = oname_parts[0],oname_parts[1:]
1399 1398 for nsname,ns in namespaces:
1400 1399 try:
1401 1400 obj = ns[oname_head]
1402 1401 except KeyError:
1403 1402 continue
1404 1403 else:
1405 1404 #print 'oname_rest:', oname_rest # dbg
1406 1405 for idx, part in enumerate(oname_rest):
1407 1406 try:
1408 1407 parent = obj
1409 1408 # The last part is looked up in a special way to avoid
1410 1409 # descriptor invocation as it may raise or have side
1411 1410 # effects.
1412 1411 if idx == len(oname_rest) - 1:
1413 1412 obj = self._getattr_property(obj, part)
1414 1413 else:
1415 1414 obj = getattr(obj, part)
1416 1415 except:
1417 1416 # Blanket except b/c some badly implemented objects
1418 1417 # allow __getattr__ to raise exceptions other than
1419 1418 # AttributeError, which then crashes IPython.
1420 1419 break
1421 1420 else:
1422 1421 # If we finish the for loop (no break), we got all members
1423 1422 found = True
1424 1423 ospace = nsname
1425 1424 break # namespace loop
1426 1425
1427 1426 # Try to see if it's magic
1428 1427 if not found:
1429 1428 obj = None
1430 1429 if oname.startswith(ESC_MAGIC2):
1431 1430 oname = oname.lstrip(ESC_MAGIC2)
1432 1431 obj = self.find_cell_magic(oname)
1433 1432 elif oname.startswith(ESC_MAGIC):
1434 1433 oname = oname.lstrip(ESC_MAGIC)
1435 1434 obj = self.find_line_magic(oname)
1436 1435 else:
1437 1436 # search without prefix, so run? will find %run?
1438 1437 obj = self.find_line_magic(oname)
1439 1438 if obj is None:
1440 1439 obj = self.find_cell_magic(oname)
1441 1440 if obj is not None:
1442 1441 found = True
1443 1442 ospace = 'IPython internal'
1444 1443 ismagic = True
1445 1444 isalias = isinstance(obj, Alias)
1446 1445
1447 1446 # Last try: special-case some literals like '', [], {}, etc:
1448 1447 if not found and oname_head in ["''",'""','[]','{}','()']:
1449 1448 obj = eval(oname_head)
1450 1449 found = True
1451 1450 ospace = 'Interactive'
1452 1451
1453 1452 return {'found':found, 'obj':obj, 'namespace':ospace,
1454 1453 'ismagic':ismagic, 'isalias':isalias, 'parent':parent}
1455 1454
1456 1455 @staticmethod
1457 1456 def _getattr_property(obj, attrname):
1458 1457 """Property-aware getattr to use in object finding.
1459 1458
1460 1459 If attrname represents a property, return it unevaluated (in case it has
1461 1460 side effects or raises an error.
1462 1461
1463 1462 """
1464 1463 if not isinstance(obj, type):
1465 1464 try:
1466 1465 # `getattr(type(obj), attrname)` is not guaranteed to return
1467 1466 # `obj`, but does so for property:
1468 1467 #
1469 1468 # property.__get__(self, None, cls) -> self
1470 1469 #
1471 1470 # The universal alternative is to traverse the mro manually
1472 1471 # searching for attrname in class dicts.
1473 1472 attr = getattr(type(obj), attrname)
1474 1473 except AttributeError:
1475 1474 pass
1476 1475 else:
1477 1476 # This relies on the fact that data descriptors (with both
1478 1477 # __get__ & __set__ magic methods) take precedence over
1479 1478 # instance-level attributes:
1480 1479 #
1481 1480 # class A(object):
1482 1481 # @property
1483 1482 # def foobar(self): return 123
1484 1483 # a = A()
1485 1484 # a.__dict__['foobar'] = 345
1486 1485 # a.foobar # == 123
1487 1486 #
1488 1487 # So, a property may be returned right away.
1489 1488 if isinstance(attr, property):
1490 1489 return attr
1491 1490
1492 1491 # Nothing helped, fall back.
1493 1492 return getattr(obj, attrname)
1494 1493
1495 1494 def _object_find(self, oname, namespaces=None):
1496 1495 """Find an object and return a struct with info about it."""
1497 1496 return Struct(self._ofind(oname, namespaces))
1498 1497
1499 1498 def _inspect(self, meth, oname, namespaces=None, **kw):
1500 1499 """Generic interface to the inspector system.
1501 1500
1502 1501 This function is meant to be called by pdef, pdoc & friends.
1503 1502 """
1504 1503 info = self._object_find(oname, namespaces)
1505 1504 docformat = sphinxify if self.sphinxify_docstring else None
1506 1505 if info.found:
1507 1506 pmethod = getattr(self.inspector, meth)
1508 1507 # TODO: only apply format_screen to the plain/text repr of the mime
1509 1508 # bundle.
1510 1509 formatter = format_screen if info.ismagic else docformat
1511 1510 if meth == 'pdoc':
1512 1511 pmethod(info.obj, oname, formatter)
1513 1512 elif meth == 'pinfo':
1514 1513 pmethod(info.obj, oname, formatter, info,
1515 1514 enable_html_pager=self.enable_html_pager, **kw)
1516 1515 else:
1517 1516 pmethod(info.obj, oname)
1518 1517 else:
1519 1518 print('Object `%s` not found.' % oname)
1520 1519 return 'not found' # so callers can take other action
1521 1520
1522 1521 def object_inspect(self, oname, detail_level=0):
1523 1522 """Get object info about oname"""
1524 1523 with self.builtin_trap:
1525 1524 info = self._object_find(oname)
1526 1525 if info.found:
1527 1526 return self.inspector.info(info.obj, oname, info=info,
1528 1527 detail_level=detail_level
1529 1528 )
1530 1529 else:
1531 1530 return oinspect.object_info(name=oname, found=False)
1532 1531
1533 1532 def object_inspect_text(self, oname, detail_level=0):
1534 1533 """Get object info as formatted text"""
1535 1534 return self.object_inspect_mime(oname, detail_level)['text/plain']
1536 1535
1537 1536 def object_inspect_mime(self, oname, detail_level=0):
1538 1537 """Get object info as a mimebundle of formatted representations.
1539 1538
1540 1539 A mimebundle is a dictionary, keyed by mime-type.
1541 1540 It must always have the key `'text/plain'`.
1542 1541 """
1543 1542 with self.builtin_trap:
1544 1543 info = self._object_find(oname)
1545 1544 if info.found:
1546 1545 return self.inspector._get_info(info.obj, oname, info=info,
1547 1546 detail_level=detail_level
1548 1547 )
1549 1548 else:
1550 1549 raise KeyError(oname)
1551 1550
1552 1551 #-------------------------------------------------------------------------
1553 1552 # Things related to history management
1554 1553 #-------------------------------------------------------------------------
1555 1554
1556 1555 def init_history(self):
1557 1556 """Sets up the command history, and starts regular autosaves."""
1558 1557 self.history_manager = HistoryManager(shell=self, parent=self)
1559 1558 self.configurables.append(self.history_manager)
1560 1559
1561 1560 #-------------------------------------------------------------------------
1562 1561 # Things related to exception handling and tracebacks (not debugging)
1563 1562 #-------------------------------------------------------------------------
1564 1563
1565 1564 debugger_cls = Pdb
1566 1565
1567 1566 def init_traceback_handlers(self, custom_exceptions):
1568 1567 # Syntax error handler.
1569 1568 self.SyntaxTB = ultratb.SyntaxTB(color_scheme='NoColor', parent=self)
1570 1569
1571 1570 # The interactive one is initialized with an offset, meaning we always
1572 1571 # want to remove the topmost item in the traceback, which is our own
1573 1572 # internal code. Valid modes: ['Plain','Context','Verbose']
1574 1573 self.InteractiveTB = ultratb.AutoFormattedTB(mode = 'Plain',
1575 1574 color_scheme='NoColor',
1576 1575 tb_offset = 1,
1577 1576 check_cache=check_linecache_ipython,
1578 1577 debugger_cls=self.debugger_cls, parent=self)
1579 1578
1580 1579 # The instance will store a pointer to the system-wide exception hook,
1581 1580 # so that runtime code (such as magics) can access it. This is because
1582 1581 # during the read-eval loop, it may get temporarily overwritten.
1583 1582 self.sys_excepthook = sys.excepthook
1584 1583
1585 1584 # and add any custom exception handlers the user may have specified
1586 1585 self.set_custom_exc(*custom_exceptions)
1587 1586
1588 1587 # Set the exception mode
1589 1588 self.InteractiveTB.set_mode(mode=self.xmode)
1590 1589
1591 1590 def set_custom_exc(self, exc_tuple, handler):
1592 1591 """set_custom_exc(exc_tuple, handler)
1593 1592
1594 1593 Set a custom exception handler, which will be called if any of the
1595 1594 exceptions in exc_tuple occur in the mainloop (specifically, in the
1596 1595 run_code() method).
1597 1596
1598 1597 Parameters
1599 1598 ----------
1600 1599
1601 1600 exc_tuple : tuple of exception classes
1602 1601 A *tuple* of exception classes, for which to call the defined
1603 1602 handler. It is very important that you use a tuple, and NOT A
1604 1603 LIST here, because of the way Python's except statement works. If
1605 1604 you only want to trap a single exception, use a singleton tuple::
1606 1605
1607 1606 exc_tuple == (MyCustomException,)
1608 1607
1609 1608 handler : callable
1610 1609 handler must have the following signature::
1611 1610
1612 1611 def my_handler(self, etype, value, tb, tb_offset=None):
1613 1612 ...
1614 1613 return structured_traceback
1615 1614
1616 1615 Your handler must return a structured traceback (a list of strings),
1617 1616 or None.
1618 1617
1619 1618 This will be made into an instance method (via types.MethodType)
1620 1619 of IPython itself, and it will be called if any of the exceptions
1621 1620 listed in the exc_tuple are caught. If the handler is None, an
1622 1621 internal basic one is used, which just prints basic info.
1623 1622
1624 1623 To protect IPython from crashes, if your handler ever raises an
1625 1624 exception or returns an invalid result, it will be immediately
1626 1625 disabled.
1627 1626
1628 1627 WARNING: by putting in your own exception handler into IPython's main
1629 1628 execution loop, you run a very good chance of nasty crashes. This
1630 1629 facility should only be used if you really know what you are doing."""
1631 1630
1632 1631 assert type(exc_tuple)==type(()) , \
1633 1632 "The custom exceptions must be given AS A TUPLE."
1634 1633
1635 1634 def dummy_handler(self, etype, value, tb, tb_offset=None):
1636 1635 print('*** Simple custom exception handler ***')
1637 1636 print('Exception type :',etype)
1638 1637 print('Exception value:',value)
1639 1638 print('Traceback :',tb)
1640 1639 #print 'Source code :','\n'.join(self.buffer)
1641 1640
1642 1641 def validate_stb(stb):
1643 1642 """validate structured traceback return type
1644 1643
1645 1644 return type of CustomTB *should* be a list of strings, but allow
1646 1645 single strings or None, which are harmless.
1647 1646
1648 1647 This function will *always* return a list of strings,
1649 1648 and will raise a TypeError if stb is inappropriate.
1650 1649 """
1651 1650 msg = "CustomTB must return list of strings, not %r" % stb
1652 1651 if stb is None:
1653 1652 return []
1654 elif isinstance(stb, string_types):
1653 elif isinstance(stb, str):
1655 1654 return [stb]
1656 1655 elif not isinstance(stb, list):
1657 1656 raise TypeError(msg)
1658 1657 # it's a list
1659 1658 for line in stb:
1660 1659 # check every element
1661 if not isinstance(line, string_types):
1660 if not isinstance(line, str):
1662 1661 raise TypeError(msg)
1663 1662 return stb
1664 1663
1665 1664 if handler is None:
1666 1665 wrapped = dummy_handler
1667 1666 else:
1668 1667 def wrapped(self,etype,value,tb,tb_offset=None):
1669 1668 """wrap CustomTB handler, to protect IPython from user code
1670 1669
1671 1670 This makes it harder (but not impossible) for custom exception
1672 1671 handlers to crash IPython.
1673 1672 """
1674 1673 try:
1675 1674 stb = handler(self,etype,value,tb,tb_offset=tb_offset)
1676 1675 return validate_stb(stb)
1677 1676 except:
1678 1677 # clear custom handler immediately
1679 1678 self.set_custom_exc((), None)
1680 1679 print("Custom TB Handler failed, unregistering", file=sys.stderr)
1681 1680 # show the exception in handler first
1682 1681 stb = self.InteractiveTB.structured_traceback(*sys.exc_info())
1683 1682 print(self.InteractiveTB.stb2text(stb))
1684 1683 print("The original exception:")
1685 1684 stb = self.InteractiveTB.structured_traceback(
1686 1685 (etype,value,tb), tb_offset=tb_offset
1687 1686 )
1688 1687 return stb
1689 1688
1690 1689 self.CustomTB = types.MethodType(wrapped,self)
1691 1690 self.custom_exceptions = exc_tuple
1692 1691
1693 1692 def excepthook(self, etype, value, tb):
1694 1693 """One more defense for GUI apps that call sys.excepthook.
1695 1694
1696 1695 GUI frameworks like wxPython trap exceptions and call
1697 1696 sys.excepthook themselves. I guess this is a feature that
1698 1697 enables them to keep running after exceptions that would
1699 1698 otherwise kill their mainloop. This is a bother for IPython
1700 1699 which excepts to catch all of the program exceptions with a try:
1701 1700 except: statement.
1702 1701
1703 1702 Normally, IPython sets sys.excepthook to a CrashHandler instance, so if
1704 1703 any app directly invokes sys.excepthook, it will look to the user like
1705 1704 IPython crashed. In order to work around this, we can disable the
1706 1705 CrashHandler and replace it with this excepthook instead, which prints a
1707 1706 regular traceback using our InteractiveTB. In this fashion, apps which
1708 1707 call sys.excepthook will generate a regular-looking exception from
1709 1708 IPython, and the CrashHandler will only be triggered by real IPython
1710 1709 crashes.
1711 1710
1712 1711 This hook should be used sparingly, only in places which are not likely
1713 1712 to be true IPython errors.
1714 1713 """
1715 1714 self.showtraceback((etype, value, tb), tb_offset=0)
1716 1715
1717 1716 def _get_exc_info(self, exc_tuple=None):
1718 1717 """get exc_info from a given tuple, sys.exc_info() or sys.last_type etc.
1719 1718
1720 1719 Ensures sys.last_type,value,traceback hold the exc_info we found,
1721 1720 from whichever source.
1722 1721
1723 1722 raises ValueError if none of these contain any information
1724 1723 """
1725 1724 if exc_tuple is None:
1726 1725 etype, value, tb = sys.exc_info()
1727 1726 else:
1728 1727 etype, value, tb = exc_tuple
1729 1728
1730 1729 if etype is None:
1731 1730 if hasattr(sys, 'last_type'):
1732 1731 etype, value, tb = sys.last_type, sys.last_value, \
1733 1732 sys.last_traceback
1734 1733
1735 1734 if etype is None:
1736 1735 raise ValueError("No exception to find")
1737 1736
1738 1737 # Now store the exception info in sys.last_type etc.
1739 1738 # WARNING: these variables are somewhat deprecated and not
1740 1739 # necessarily safe to use in a threaded environment, but tools
1741 1740 # like pdb depend on their existence, so let's set them. If we
1742 1741 # find problems in the field, we'll need to revisit their use.
1743 1742 sys.last_type = etype
1744 1743 sys.last_value = value
1745 1744 sys.last_traceback = tb
1746 1745
1747 1746 return etype, value, tb
1748 1747
1749 1748 def show_usage_error(self, exc):
1750 1749 """Show a short message for UsageErrors
1751 1750
1752 1751 These are special exceptions that shouldn't show a traceback.
1753 1752 """
1754 1753 print("UsageError: %s" % exc, file=sys.stderr)
1755 1754
1756 1755 def get_exception_only(self, exc_tuple=None):
1757 1756 """
1758 1757 Return as a string (ending with a newline) the exception that
1759 1758 just occurred, without any traceback.
1760 1759 """
1761 1760 etype, value, tb = self._get_exc_info(exc_tuple)
1762 1761 msg = traceback.format_exception_only(etype, value)
1763 1762 return ''.join(msg)
1764 1763
1765 1764 def showtraceback(self, exc_tuple=None, filename=None, tb_offset=None,
1766 1765 exception_only=False):
1767 1766 """Display the exception that just occurred.
1768 1767
1769 1768 If nothing is known about the exception, this is the method which
1770 1769 should be used throughout the code for presenting user tracebacks,
1771 1770 rather than directly invoking the InteractiveTB object.
1772 1771
1773 1772 A specific showsyntaxerror() also exists, but this method can take
1774 1773 care of calling it if needed, so unless you are explicitly catching a
1775 1774 SyntaxError exception, don't try to analyze the stack manually and
1776 1775 simply call this method."""
1777 1776
1778 1777 try:
1779 1778 try:
1780 1779 etype, value, tb = self._get_exc_info(exc_tuple)
1781 1780 except ValueError:
1782 1781 print('No traceback available to show.', file=sys.stderr)
1783 1782 return
1784 1783
1785 1784 if issubclass(etype, SyntaxError):
1786 1785 # Though this won't be called by syntax errors in the input
1787 1786 # line, there may be SyntaxError cases with imported code.
1788 1787 self.showsyntaxerror(filename)
1789 1788 elif etype is UsageError:
1790 1789 self.show_usage_error(value)
1791 1790 else:
1792 1791 if exception_only:
1793 1792 stb = ['An exception has occurred, use %tb to see '
1794 1793 'the full traceback.\n']
1795 1794 stb.extend(self.InteractiveTB.get_exception_only(etype,
1796 1795 value))
1797 1796 else:
1798 1797 try:
1799 1798 # Exception classes can customise their traceback - we
1800 1799 # use this in IPython.parallel for exceptions occurring
1801 1800 # in the engines. This should return a list of strings.
1802 1801 stb = value._render_traceback_()
1803 1802 except Exception:
1804 1803 stb = self.InteractiveTB.structured_traceback(etype,
1805 1804 value, tb, tb_offset=tb_offset)
1806 1805
1807 1806 self._showtraceback(etype, value, stb)
1808 1807 if self.call_pdb:
1809 1808 # drop into debugger
1810 1809 self.debugger(force=True)
1811 1810 return
1812 1811
1813 1812 # Actually show the traceback
1814 1813 self._showtraceback(etype, value, stb)
1815 1814
1816 1815 except KeyboardInterrupt:
1817 1816 print('\n' + self.get_exception_only(), file=sys.stderr)
1818 1817
1819 1818 def _showtraceback(self, etype, evalue, stb):
1820 1819 """Actually show a traceback.
1821 1820
1822 1821 Subclasses may override this method to put the traceback on a different
1823 1822 place, like a side channel.
1824 1823 """
1825 1824 print(self.InteractiveTB.stb2text(stb))
1826 1825
1827 1826 def showsyntaxerror(self, filename=None):
1828 1827 """Display the syntax error that just occurred.
1829 1828
1830 1829 This doesn't display a stack trace because there isn't one.
1831 1830
1832 1831 If a filename is given, it is stuffed in the exception instead
1833 1832 of what was there before (because Python's parser always uses
1834 1833 "<string>" when reading from a string).
1835 1834 """
1836 1835 etype, value, last_traceback = self._get_exc_info()
1837 1836
1838 1837 if filename and issubclass(etype, SyntaxError):
1839 1838 try:
1840 1839 value.filename = filename
1841 1840 except:
1842 1841 # Not the format we expect; leave it alone
1843 1842 pass
1844 1843
1845 1844 stb = self.SyntaxTB.structured_traceback(etype, value, [])
1846 1845 self._showtraceback(etype, value, stb)
1847 1846
1848 1847 # This is overridden in TerminalInteractiveShell to show a message about
1849 1848 # the %paste magic.
1850 1849 def showindentationerror(self):
1851 1850 """Called by run_cell when there's an IndentationError in code entered
1852 1851 at the prompt.
1853 1852
1854 1853 This is overridden in TerminalInteractiveShell to show a message about
1855 1854 the %paste magic."""
1856 1855 self.showsyntaxerror()
1857 1856
1858 1857 #-------------------------------------------------------------------------
1859 1858 # Things related to readline
1860 1859 #-------------------------------------------------------------------------
1861 1860
1862 1861 def init_readline(self):
1863 1862 """DEPRECATED
1864 1863
1865 1864 Moved to terminal subclass, here only to simplify the init logic."""
1866 1865 # Set a number of methods that depend on readline to be no-op
1867 1866 warnings.warn('`init_readline` is no-op since IPython 5.0 and is Deprecated',
1868 1867 DeprecationWarning, stacklevel=2)
1869 1868 self.set_custom_completer = no_op
1870 1869
1871 1870 @skip_doctest
1872 1871 def set_next_input(self, s, replace=False):
1873 1872 """ Sets the 'default' input string for the next command line.
1874 1873
1875 1874 Example::
1876 1875
1877 1876 In [1]: _ip.set_next_input("Hello Word")
1878 1877 In [2]: Hello Word_ # cursor is here
1879 1878 """
1880 1879 self.rl_next_input = py3compat.cast_bytes_py2(s)
1881 1880
1882 1881 def _indent_current_str(self):
1883 1882 """return the current level of indentation as a string"""
1884 1883 return self.input_splitter.indent_spaces * ' '
1885 1884
1886 1885 #-------------------------------------------------------------------------
1887 1886 # Things related to text completion
1888 1887 #-------------------------------------------------------------------------
1889 1888
1890 1889 def init_completer(self):
1891 1890 """Initialize the completion machinery.
1892 1891
1893 1892 This creates completion machinery that can be used by client code,
1894 1893 either interactively in-process (typically triggered by the readline
1895 1894 library), programmatically (such as in test suites) or out-of-process
1896 1895 (typically over the network by remote frontends).
1897 1896 """
1898 1897 from IPython.core.completer import IPCompleter
1899 1898 from IPython.core.completerlib import (module_completer,
1900 1899 magic_run_completer, cd_completer, reset_completer)
1901 1900
1902 1901 self.Completer = IPCompleter(shell=self,
1903 1902 namespace=self.user_ns,
1904 1903 global_namespace=self.user_global_ns,
1905 1904 parent=self,
1906 1905 )
1907 1906 self.configurables.append(self.Completer)
1908 1907
1909 1908 # Add custom completers to the basic ones built into IPCompleter
1910 1909 sdisp = self.strdispatchers.get('complete_command', StrDispatch())
1911 1910 self.strdispatchers['complete_command'] = sdisp
1912 1911 self.Completer.custom_completers = sdisp
1913 1912
1914 1913 self.set_hook('complete_command', module_completer, str_key = 'import')
1915 1914 self.set_hook('complete_command', module_completer, str_key = 'from')
1916 1915 self.set_hook('complete_command', module_completer, str_key = '%aimport')
1917 1916 self.set_hook('complete_command', magic_run_completer, str_key = '%run')
1918 1917 self.set_hook('complete_command', cd_completer, str_key = '%cd')
1919 1918 self.set_hook('complete_command', reset_completer, str_key = '%reset')
1920 1919
1921 1920
1922 1921 def complete(self, text, line=None, cursor_pos=None):
1923 1922 """Return the completed text and a list of completions.
1924 1923
1925 1924 Parameters
1926 1925 ----------
1927 1926
1928 1927 text : string
1929 1928 A string of text to be completed on. It can be given as empty and
1930 1929 instead a line/position pair are given. In this case, the
1931 1930 completer itself will split the line like readline does.
1932 1931
1933 1932 line : string, optional
1934 1933 The complete line that text is part of.
1935 1934
1936 1935 cursor_pos : int, optional
1937 1936 The position of the cursor on the input line.
1938 1937
1939 1938 Returns
1940 1939 -------
1941 1940 text : string
1942 1941 The actual text that was completed.
1943 1942
1944 1943 matches : list
1945 1944 A sorted list with all possible completions.
1946 1945
1947 1946 The optional arguments allow the completion to take more context into
1948 1947 account, and are part of the low-level completion API.
1949 1948
1950 1949 This is a wrapper around the completion mechanism, similar to what
1951 1950 readline does at the command line when the TAB key is hit. By
1952 1951 exposing it as a method, it can be used by other non-readline
1953 1952 environments (such as GUIs) for text completion.
1954 1953
1955 1954 Simple usage example:
1956 1955
1957 1956 In [1]: x = 'hello'
1958 1957
1959 1958 In [2]: _ip.complete('x.l')
1960 1959 Out[2]: ('x.l', ['x.ljust', 'x.lower', 'x.lstrip'])
1961 1960 """
1962 1961
1963 1962 # Inject names into __builtin__ so we can complete on the added names.
1964 1963 with self.builtin_trap:
1965 1964 return self.Completer.complete(text, line, cursor_pos)
1966 1965
1967 1966 def set_custom_completer(self, completer, pos=0):
1968 1967 """Adds a new custom completer function.
1969 1968
1970 1969 The position argument (defaults to 0) is the index in the completers
1971 1970 list where you want the completer to be inserted."""
1972 1971
1973 1972 newcomp = types.MethodType(completer,self.Completer)
1974 1973 self.Completer.matchers.insert(pos,newcomp)
1975 1974
1976 1975 def set_completer_frame(self, frame=None):
1977 1976 """Set the frame of the completer."""
1978 1977 if frame:
1979 1978 self.Completer.namespace = frame.f_locals
1980 1979 self.Completer.global_namespace = frame.f_globals
1981 1980 else:
1982 1981 self.Completer.namespace = self.user_ns
1983 1982 self.Completer.global_namespace = self.user_global_ns
1984 1983
1985 1984 #-------------------------------------------------------------------------
1986 1985 # Things related to magics
1987 1986 #-------------------------------------------------------------------------
1988 1987
1989 1988 def init_magics(self):
1990 1989 from IPython.core import magics as m
1991 1990 self.magics_manager = magic.MagicsManager(shell=self,
1992 1991 parent=self,
1993 1992 user_magics=m.UserMagics(self))
1994 1993 self.configurables.append(self.magics_manager)
1995 1994
1996 1995 # Expose as public API from the magics manager
1997 1996 self.register_magics = self.magics_manager.register
1998 1997
1999 1998 self.register_magics(m.AutoMagics, m.BasicMagics, m.CodeMagics,
2000 1999 m.ConfigMagics, m.DisplayMagics, m.ExecutionMagics,
2001 2000 m.ExtensionMagics, m.HistoryMagics, m.LoggingMagics,
2002 2001 m.NamespaceMagics, m.OSMagics, m.PylabMagics, m.ScriptMagics,
2003 2002 )
2004 2003
2005 2004 # Register Magic Aliases
2006 2005 mman = self.magics_manager
2007 2006 # FIXME: magic aliases should be defined by the Magics classes
2008 2007 # or in MagicsManager, not here
2009 2008 mman.register_alias('ed', 'edit')
2010 2009 mman.register_alias('hist', 'history')
2011 2010 mman.register_alias('rep', 'recall')
2012 2011 mman.register_alias('SVG', 'svg', 'cell')
2013 2012 mman.register_alias('HTML', 'html', 'cell')
2014 2013 mman.register_alias('file', 'writefile', 'cell')
2015 2014
2016 2015 # FIXME: Move the color initialization to the DisplayHook, which
2017 2016 # should be split into a prompt manager and displayhook. We probably
2018 2017 # even need a centralize colors management object.
2019 2018 self.magic('colors %s' % self.colors)
2020 2019
2021 2020 # Defined here so that it's included in the documentation
2022 2021 @functools.wraps(magic.MagicsManager.register_function)
2023 2022 def register_magic_function(self, func, magic_kind='line', magic_name=None):
2024 2023 self.magics_manager.register_function(func,
2025 2024 magic_kind=magic_kind, magic_name=magic_name)
2026 2025
2027 2026 def run_line_magic(self, magic_name, line):
2028 2027 """Execute the given line magic.
2029 2028
2030 2029 Parameters
2031 2030 ----------
2032 2031 magic_name : str
2033 2032 Name of the desired magic function, without '%' prefix.
2034 2033
2035 2034 line : str
2036 2035 The rest of the input line as a single string.
2037 2036 """
2038 2037 fn = self.find_line_magic(magic_name)
2039 2038 if fn is None:
2040 2039 cm = self.find_cell_magic(magic_name)
2041 2040 etpl = "Line magic function `%%%s` not found%s."
2042 2041 extra = '' if cm is None else (' (But cell magic `%%%%%s` exists, '
2043 2042 'did you mean that instead?)' % magic_name )
2044 2043 error(etpl % (magic_name, extra))
2045 2044 else:
2046 2045 # Note: this is the distance in the stack to the user's frame.
2047 2046 # This will need to be updated if the internal calling logic gets
2048 2047 # refactored, or else we'll be expanding the wrong variables.
2049 2048 stack_depth = 2
2050 2049 magic_arg_s = self.var_expand(line, stack_depth)
2051 2050 # Put magic args in a list so we can call with f(*a) syntax
2052 2051 args = [magic_arg_s]
2053 2052 kwargs = {}
2054 2053 # Grab local namespace if we need it:
2055 2054 if getattr(fn, "needs_local_scope", False):
2056 2055 kwargs['local_ns'] = sys._getframe(stack_depth).f_locals
2057 2056 with self.builtin_trap:
2058 2057 result = fn(*args,**kwargs)
2059 2058 return result
2060 2059
2061 2060 def run_cell_magic(self, magic_name, line, cell):
2062 2061 """Execute the given cell magic.
2063 2062
2064 2063 Parameters
2065 2064 ----------
2066 2065 magic_name : str
2067 2066 Name of the desired magic function, without '%' prefix.
2068 2067
2069 2068 line : str
2070 2069 The rest of the first input line as a single string.
2071 2070
2072 2071 cell : str
2073 2072 The body of the cell as a (possibly multiline) string.
2074 2073 """
2075 2074 fn = self.find_cell_magic(magic_name)
2076 2075 if fn is None:
2077 2076 lm = self.find_line_magic(magic_name)
2078 2077 etpl = "Cell magic `%%{0}` not found{1}."
2079 2078 extra = '' if lm is None else (' (But line magic `%{0}` exists, '
2080 2079 'did you mean that instead?)'.format(magic_name))
2081 2080 error(etpl.format(magic_name, extra))
2082 2081 elif cell == '':
2083 2082 message = '%%{0} is a cell magic, but the cell body is empty.'.format(magic_name)
2084 2083 if self.find_line_magic(magic_name) is not None:
2085 2084 message += ' Did you mean the line magic %{0} (single %)?'.format(magic_name)
2086 2085 raise UsageError(message)
2087 2086 else:
2088 2087 # Note: this is the distance in the stack to the user's frame.
2089 2088 # This will need to be updated if the internal calling logic gets
2090 2089 # refactored, or else we'll be expanding the wrong variables.
2091 2090 stack_depth = 2
2092 2091 magic_arg_s = self.var_expand(line, stack_depth)
2093 2092 with self.builtin_trap:
2094 2093 result = fn(magic_arg_s, cell)
2095 2094 return result
2096 2095
2097 2096 def find_line_magic(self, magic_name):
2098 2097 """Find and return a line magic by name.
2099 2098
2100 2099 Returns None if the magic isn't found."""
2101 2100 return self.magics_manager.magics['line'].get(magic_name)
2102 2101
2103 2102 def find_cell_magic(self, magic_name):
2104 2103 """Find and return a cell magic by name.
2105 2104
2106 2105 Returns None if the magic isn't found."""
2107 2106 return self.magics_manager.magics['cell'].get(magic_name)
2108 2107
2109 2108 def find_magic(self, magic_name, magic_kind='line'):
2110 2109 """Find and return a magic of the given type by name.
2111 2110
2112 2111 Returns None if the magic isn't found."""
2113 2112 return self.magics_manager.magics[magic_kind].get(magic_name)
2114 2113
2115 2114 def magic(self, arg_s):
2116 2115 """DEPRECATED. Use run_line_magic() instead.
2117 2116
2118 2117 Call a magic function by name.
2119 2118
2120 2119 Input: a string containing the name of the magic function to call and
2121 2120 any additional arguments to be passed to the magic.
2122 2121
2123 2122 magic('name -opt foo bar') is equivalent to typing at the ipython
2124 2123 prompt:
2125 2124
2126 2125 In[1]: %name -opt foo bar
2127 2126
2128 2127 To call a magic without arguments, simply use magic('name').
2129 2128
2130 2129 This provides a proper Python function to call IPython's magics in any
2131 2130 valid Python code you can type at the interpreter, including loops and
2132 2131 compound statements.
2133 2132 """
2134 2133 # TODO: should we issue a loud deprecation warning here?
2135 2134 magic_name, _, magic_arg_s = arg_s.partition(' ')
2136 2135 magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
2137 2136 return self.run_line_magic(magic_name, magic_arg_s)
2138 2137
2139 2138 #-------------------------------------------------------------------------
2140 2139 # Things related to macros
2141 2140 #-------------------------------------------------------------------------
2142 2141
2143 2142 def define_macro(self, name, themacro):
2144 2143 """Define a new macro
2145 2144
2146 2145 Parameters
2147 2146 ----------
2148 2147 name : str
2149 2148 The name of the macro.
2150 2149 themacro : str or Macro
2151 2150 The action to do upon invoking the macro. If a string, a new
2152 2151 Macro object is created by passing the string to it.
2153 2152 """
2154 2153
2155 2154 from IPython.core import macro
2156 2155
2157 if isinstance(themacro, string_types):
2156 if isinstance(themacro, str):
2158 2157 themacro = macro.Macro(themacro)
2159 2158 if not isinstance(themacro, macro.Macro):
2160 2159 raise ValueError('A macro must be a string or a Macro instance.')
2161 2160 self.user_ns[name] = themacro
2162 2161
2163 2162 #-------------------------------------------------------------------------
2164 2163 # Things related to the running of system commands
2165 2164 #-------------------------------------------------------------------------
2166 2165
2167 2166 def system_piped(self, cmd):
2168 2167 """Call the given cmd in a subprocess, piping stdout/err
2169 2168
2170 2169 Parameters
2171 2170 ----------
2172 2171 cmd : str
2173 2172 Command to execute (can not end in '&', as background processes are
2174 2173 not supported. Should not be a command that expects input
2175 2174 other than simple text.
2176 2175 """
2177 2176 if cmd.rstrip().endswith('&'):
2178 2177 # this is *far* from a rigorous test
2179 2178 # We do not support backgrounding processes because we either use
2180 2179 # pexpect or pipes to read from. Users can always just call
2181 2180 # os.system() or use ip.system=ip.system_raw
2182 2181 # if they really want a background process.
2183 2182 raise OSError("Background processes not supported.")
2184 2183
2185 2184 # we explicitly do NOT return the subprocess status code, because
2186 2185 # a non-None value would trigger :func:`sys.displayhook` calls.
2187 2186 # Instead, we store the exit_code in user_ns.
2188 2187 self.user_ns['_exit_code'] = system(self.var_expand(cmd, depth=1))
2189 2188
2190 2189 def system_raw(self, cmd):
2191 2190 """Call the given cmd in a subprocess using os.system on Windows or
2192 2191 subprocess.call using the system shell on other platforms.
2193 2192
2194 2193 Parameters
2195 2194 ----------
2196 2195 cmd : str
2197 2196 Command to execute.
2198 2197 """
2199 2198 cmd = self.var_expand(cmd, depth=1)
2200 2199 # protect os.system from UNC paths on Windows, which it can't handle:
2201 2200 if sys.platform == 'win32':
2202 2201 from IPython.utils._process_win32 import AvoidUNCPath
2203 2202 with AvoidUNCPath() as path:
2204 2203 if path is not None:
2205 2204 cmd = '"pushd %s &&"%s' % (path, cmd)
2206 2205 cmd = py3compat.unicode_to_str(cmd)
2207 2206 try:
2208 2207 ec = os.system(cmd)
2209 2208 except KeyboardInterrupt:
2210 2209 print('\n' + self.get_exception_only(), file=sys.stderr)
2211 2210 ec = -2
2212 2211 else:
2213 2212 cmd = py3compat.unicode_to_str(cmd)
2214 2213 # For posix the result of the subprocess.call() below is an exit
2215 2214 # code, which by convention is zero for success, positive for
2216 2215 # program failure. Exit codes above 128 are reserved for signals,
2217 2216 # and the formula for converting a signal to an exit code is usually
2218 2217 # signal_number+128. To more easily differentiate between exit
2219 2218 # codes and signals, ipython uses negative numbers. For instance
2220 2219 # since control-c is signal 2 but exit code 130, ipython's
2221 2220 # _exit_code variable will read -2. Note that some shells like
2222 2221 # csh and fish don't follow sh/bash conventions for exit codes.
2223 2222 executable = os.environ.get('SHELL', None)
2224 2223 try:
2225 2224 # Use env shell instead of default /bin/sh
2226 2225 ec = subprocess.call(cmd, shell=True, executable=executable)
2227 2226 except KeyboardInterrupt:
2228 2227 # intercept control-C; a long traceback is not useful here
2229 2228 print('\n' + self.get_exception_only(), file=sys.stderr)
2230 2229 ec = 130
2231 2230 if ec > 128:
2232 2231 ec = -(ec - 128)
2233 2232
2234 2233 # We explicitly do NOT return the subprocess status code, because
2235 2234 # a non-None value would trigger :func:`sys.displayhook` calls.
2236 2235 # Instead, we store the exit_code in user_ns. Note the semantics
2237 2236 # of _exit_code: for control-c, _exit_code == -signal.SIGNIT,
2238 2237 # but raising SystemExit(_exit_code) will give status 254!
2239 2238 self.user_ns['_exit_code'] = ec
2240 2239
2241 2240 # use piped system by default, because it is better behaved
2242 2241 system = system_piped
2243 2242
2244 2243 def getoutput(self, cmd, split=True, depth=0):
2245 2244 """Get output (possibly including stderr) from a subprocess.
2246 2245
2247 2246 Parameters
2248 2247 ----------
2249 2248 cmd : str
2250 2249 Command to execute (can not end in '&', as background processes are
2251 2250 not supported.
2252 2251 split : bool, optional
2253 2252 If True, split the output into an IPython SList. Otherwise, an
2254 2253 IPython LSString is returned. These are objects similar to normal
2255 2254 lists and strings, with a few convenience attributes for easier
2256 2255 manipulation of line-based output. You can use '?' on them for
2257 2256 details.
2258 2257 depth : int, optional
2259 2258 How many frames above the caller are the local variables which should
2260 2259 be expanded in the command string? The default (0) assumes that the
2261 2260 expansion variables are in the stack frame calling this function.
2262 2261 """
2263 2262 if cmd.rstrip().endswith('&'):
2264 2263 # this is *far* from a rigorous test
2265 2264 raise OSError("Background processes not supported.")
2266 2265 out = getoutput(self.var_expand(cmd, depth=depth+1))
2267 2266 if split:
2268 2267 out = SList(out.splitlines())
2269 2268 else:
2270 2269 out = LSString(out)
2271 2270 return out
2272 2271
2273 2272 #-------------------------------------------------------------------------
2274 2273 # Things related to aliases
2275 2274 #-------------------------------------------------------------------------
2276 2275
2277 2276 def init_alias(self):
2278 2277 self.alias_manager = AliasManager(shell=self, parent=self)
2279 2278 self.configurables.append(self.alias_manager)
2280 2279
2281 2280 #-------------------------------------------------------------------------
2282 2281 # Things related to extensions
2283 2282 #-------------------------------------------------------------------------
2284 2283
2285 2284 def init_extension_manager(self):
2286 2285 self.extension_manager = ExtensionManager(shell=self, parent=self)
2287 2286 self.configurables.append(self.extension_manager)
2288 2287
2289 2288 #-------------------------------------------------------------------------
2290 2289 # Things related to payloads
2291 2290 #-------------------------------------------------------------------------
2292 2291
2293 2292 def init_payload(self):
2294 2293 self.payload_manager = PayloadManager(parent=self)
2295 2294 self.configurables.append(self.payload_manager)
2296 2295
2297 2296 #-------------------------------------------------------------------------
2298 2297 # Things related to the prefilter
2299 2298 #-------------------------------------------------------------------------
2300 2299
2301 2300 def init_prefilter(self):
2302 2301 self.prefilter_manager = PrefilterManager(shell=self, parent=self)
2303 2302 self.configurables.append(self.prefilter_manager)
2304 2303 # Ultimately this will be refactored in the new interpreter code, but
2305 2304 # for now, we should expose the main prefilter method (there's legacy
2306 2305 # code out there that may rely on this).
2307 2306 self.prefilter = self.prefilter_manager.prefilter_lines
2308 2307
2309 2308 def auto_rewrite_input(self, cmd):
2310 2309 """Print to the screen the rewritten form of the user's command.
2311 2310
2312 2311 This shows visual feedback by rewriting input lines that cause
2313 2312 automatic calling to kick in, like::
2314 2313
2315 2314 /f x
2316 2315
2317 2316 into::
2318 2317
2319 2318 ------> f(x)
2320 2319
2321 2320 after the user's input prompt. This helps the user understand that the
2322 2321 input line was transformed automatically by IPython.
2323 2322 """
2324 2323 if not self.show_rewritten_input:
2325 2324 return
2326 2325
2327 2326 # This is overridden in TerminalInteractiveShell to use fancy prompts
2328 2327 print("------> " + cmd)
2329 2328
2330 2329 #-------------------------------------------------------------------------
2331 2330 # Things related to extracting values/expressions from kernel and user_ns
2332 2331 #-------------------------------------------------------------------------
2333 2332
2334 2333 def _user_obj_error(self):
2335 2334 """return simple exception dict
2336 2335
2337 2336 for use in user_expressions
2338 2337 """
2339 2338
2340 2339 etype, evalue, tb = self._get_exc_info()
2341 2340 stb = self.InteractiveTB.get_exception_only(etype, evalue)
2342 2341
2343 2342 exc_info = {
2344 2343 u'status' : 'error',
2345 2344 u'traceback' : stb,
2346 2345 u'ename' : unicode_type(etype.__name__),
2347 2346 u'evalue' : py3compat.safe_unicode(evalue),
2348 2347 }
2349 2348
2350 2349 return exc_info
2351 2350
2352 2351 def _format_user_obj(self, obj):
2353 2352 """format a user object to display dict
2354 2353
2355 2354 for use in user_expressions
2356 2355 """
2357 2356
2358 2357 data, md = self.display_formatter.format(obj)
2359 2358 value = {
2360 2359 'status' : 'ok',
2361 2360 'data' : data,
2362 2361 'metadata' : md,
2363 2362 }
2364 2363 return value
2365 2364
2366 2365 def user_expressions(self, expressions):
2367 2366 """Evaluate a dict of expressions in the user's namespace.
2368 2367
2369 2368 Parameters
2370 2369 ----------
2371 2370 expressions : dict
2372 2371 A dict with string keys and string values. The expression values
2373 2372 should be valid Python expressions, each of which will be evaluated
2374 2373 in the user namespace.
2375 2374
2376 2375 Returns
2377 2376 -------
2378 2377 A dict, keyed like the input expressions dict, with the rich mime-typed
2379 2378 display_data of each value.
2380 2379 """
2381 2380 out = {}
2382 2381 user_ns = self.user_ns
2383 2382 global_ns = self.user_global_ns
2384 2383
2385 2384 for key, expr in expressions.items():
2386 2385 try:
2387 2386 value = self._format_user_obj(eval(expr, global_ns, user_ns))
2388 2387 except:
2389 2388 value = self._user_obj_error()
2390 2389 out[key] = value
2391 2390 return out
2392 2391
2393 2392 #-------------------------------------------------------------------------
2394 2393 # Things related to the running of code
2395 2394 #-------------------------------------------------------------------------
2396 2395
2397 2396 def ex(self, cmd):
2398 2397 """Execute a normal python statement in user namespace."""
2399 2398 with self.builtin_trap:
2400 2399 exec(cmd, self.user_global_ns, self.user_ns)
2401 2400
2402 2401 def ev(self, expr):
2403 2402 """Evaluate python expression expr in user namespace.
2404 2403
2405 2404 Returns the result of evaluation
2406 2405 """
2407 2406 with self.builtin_trap:
2408 2407 return eval(expr, self.user_global_ns, self.user_ns)
2409 2408
2410 2409 def safe_execfile(self, fname, *where, **kw):
2411 2410 """A safe version of the builtin execfile().
2412 2411
2413 2412 This version will never throw an exception, but instead print
2414 2413 helpful error messages to the screen. This only works on pure
2415 2414 Python files with the .py extension.
2416 2415
2417 2416 Parameters
2418 2417 ----------
2419 2418 fname : string
2420 2419 The name of the file to be executed.
2421 2420 where : tuple
2422 2421 One or two namespaces, passed to execfile() as (globals,locals).
2423 2422 If only one is given, it is passed as both.
2424 2423 exit_ignore : bool (False)
2425 2424 If True, then silence SystemExit for non-zero status (it is always
2426 2425 silenced for zero status, as it is so common).
2427 2426 raise_exceptions : bool (False)
2428 2427 If True raise exceptions everywhere. Meant for testing.
2429 2428 shell_futures : bool (False)
2430 2429 If True, the code will share future statements with the interactive
2431 2430 shell. It will both be affected by previous __future__ imports, and
2432 2431 any __future__ imports in the code will affect the shell. If False,
2433 2432 __future__ imports are not shared in either direction.
2434 2433
2435 2434 """
2436 2435 kw.setdefault('exit_ignore', False)
2437 2436 kw.setdefault('raise_exceptions', False)
2438 2437 kw.setdefault('shell_futures', False)
2439 2438
2440 2439 fname = os.path.abspath(os.path.expanduser(fname))
2441 2440
2442 2441 # Make sure we can open the file
2443 2442 try:
2444 2443 with open(fname):
2445 2444 pass
2446 2445 except:
2447 2446 warn('Could not open file <%s> for safe execution.' % fname)
2448 2447 return
2449 2448
2450 2449 # Find things also in current directory. This is needed to mimic the
2451 2450 # behavior of running a script from the system command line, where
2452 2451 # Python inserts the script's directory into sys.path
2453 2452 dname = os.path.dirname(fname)
2454 2453
2455 2454 with prepended_to_syspath(dname), self.builtin_trap:
2456 2455 try:
2457 2456 glob, loc = (where + (None, ))[:2]
2458 2457 py3compat.execfile(
2459 2458 fname, glob, loc,
2460 2459 self.compile if kw['shell_futures'] else None)
2461 2460 except SystemExit as status:
2462 2461 # If the call was made with 0 or None exit status (sys.exit(0)
2463 2462 # or sys.exit() ), don't bother showing a traceback, as both of
2464 2463 # these are considered normal by the OS:
2465 2464 # > python -c'import sys;sys.exit(0)'; echo $?
2466 2465 # 0
2467 2466 # > python -c'import sys;sys.exit()'; echo $?
2468 2467 # 0
2469 2468 # For other exit status, we show the exception unless
2470 2469 # explicitly silenced, but only in short form.
2471 2470 if status.code:
2472 2471 if kw['raise_exceptions']:
2473 2472 raise
2474 2473 if not kw['exit_ignore']:
2475 2474 self.showtraceback(exception_only=True)
2476 2475 except:
2477 2476 if kw['raise_exceptions']:
2478 2477 raise
2479 2478 # tb offset is 2 because we wrap execfile
2480 2479 self.showtraceback(tb_offset=2)
2481 2480
2482 2481 def safe_execfile_ipy(self, fname, shell_futures=False, raise_exceptions=False):
2483 2482 """Like safe_execfile, but for .ipy or .ipynb files with IPython syntax.
2484 2483
2485 2484 Parameters
2486 2485 ----------
2487 2486 fname : str
2488 2487 The name of the file to execute. The filename must have a
2489 2488 .ipy or .ipynb extension.
2490 2489 shell_futures : bool (False)
2491 2490 If True, the code will share future statements with the interactive
2492 2491 shell. It will both be affected by previous __future__ imports, and
2493 2492 any __future__ imports in the code will affect the shell. If False,
2494 2493 __future__ imports are not shared in either direction.
2495 2494 raise_exceptions : bool (False)
2496 2495 If True raise exceptions everywhere. Meant for testing.
2497 2496 """
2498 2497 fname = os.path.abspath(os.path.expanduser(fname))
2499 2498
2500 2499 # Make sure we can open the file
2501 2500 try:
2502 2501 with open(fname):
2503 2502 pass
2504 2503 except:
2505 2504 warn('Could not open file <%s> for safe execution.' % fname)
2506 2505 return
2507 2506
2508 2507 # Find things also in current directory. This is needed to mimic the
2509 2508 # behavior of running a script from the system command line, where
2510 2509 # Python inserts the script's directory into sys.path
2511 2510 dname = os.path.dirname(fname)
2512 2511
2513 2512 def get_cells():
2514 2513 """generator for sequence of code blocks to run"""
2515 2514 if fname.endswith('.ipynb'):
2516 2515 from nbformat import read
2517 2516 with io_open(fname) as f:
2518 2517 nb = read(f, as_version=4)
2519 2518 if not nb.cells:
2520 2519 return
2521 2520 for cell in nb.cells:
2522 2521 if cell.cell_type == 'code':
2523 2522 yield cell.source
2524 2523 else:
2525 2524 with open(fname) as f:
2526 2525 yield f.read()
2527 2526
2528 2527 with prepended_to_syspath(dname):
2529 2528 try:
2530 2529 for cell in get_cells():
2531 2530 result = self.run_cell(cell, silent=True, shell_futures=shell_futures)
2532 2531 if raise_exceptions:
2533 2532 result.raise_error()
2534 2533 elif not result.success:
2535 2534 break
2536 2535 except:
2537 2536 if raise_exceptions:
2538 2537 raise
2539 2538 self.showtraceback()
2540 2539 warn('Unknown failure executing file: <%s>' % fname)
2541 2540
2542 2541 def safe_run_module(self, mod_name, where):
2543 2542 """A safe version of runpy.run_module().
2544 2543
2545 2544 This version will never throw an exception, but instead print
2546 2545 helpful error messages to the screen.
2547 2546
2548 2547 `SystemExit` exceptions with status code 0 or None are ignored.
2549 2548
2550 2549 Parameters
2551 2550 ----------
2552 2551 mod_name : string
2553 2552 The name of the module to be executed.
2554 2553 where : dict
2555 2554 The globals namespace.
2556 2555 """
2557 2556 try:
2558 2557 try:
2559 2558 where.update(
2560 2559 runpy.run_module(str(mod_name), run_name="__main__",
2561 2560 alter_sys=True)
2562 2561 )
2563 2562 except SystemExit as status:
2564 2563 if status.code:
2565 2564 raise
2566 2565 except:
2567 2566 self.showtraceback()
2568 2567 warn('Unknown failure executing module: <%s>' % mod_name)
2569 2568
2570 2569 def run_cell(self, raw_cell, store_history=False, silent=False, shell_futures=True):
2571 2570 """Run a complete IPython cell.
2572 2571
2573 2572 Parameters
2574 2573 ----------
2575 2574 raw_cell : str
2576 2575 The code (including IPython code such as %magic functions) to run.
2577 2576 store_history : bool
2578 2577 If True, the raw and translated cell will be stored in IPython's
2579 2578 history. For user code calling back into IPython's machinery, this
2580 2579 should be set to False.
2581 2580 silent : bool
2582 2581 If True, avoid side-effects, such as implicit displayhooks and
2583 2582 and logging. silent=True forces store_history=False.
2584 2583 shell_futures : bool
2585 2584 If True, the code will share future statements with the interactive
2586 2585 shell. It will both be affected by previous __future__ imports, and
2587 2586 any __future__ imports in the code will affect the shell. If False,
2588 2587 __future__ imports are not shared in either direction.
2589 2588
2590 2589 Returns
2591 2590 -------
2592 2591 result : :class:`ExecutionResult`
2593 2592 """
2594 2593 result = ExecutionResult()
2595 2594
2596 2595 if (not raw_cell) or raw_cell.isspace():
2597 2596 self.last_execution_succeeded = True
2598 2597 return result
2599 2598
2600 2599 if silent:
2601 2600 store_history = False
2602 2601
2603 2602 if store_history:
2604 2603 result.execution_count = self.execution_count
2605 2604
2606 2605 def error_before_exec(value):
2607 2606 result.error_before_exec = value
2608 2607 self.last_execution_succeeded = False
2609 2608 return result
2610 2609
2611 2610 self.events.trigger('pre_execute')
2612 2611 if not silent:
2613 2612 self.events.trigger('pre_run_cell')
2614 2613
2615 2614 # If any of our input transformation (input_transformer_manager or
2616 2615 # prefilter_manager) raises an exception, we store it in this variable
2617 2616 # so that we can display the error after logging the input and storing
2618 2617 # it in the history.
2619 2618 preprocessing_exc_tuple = None
2620 2619 try:
2621 2620 # Static input transformations
2622 2621 cell = self.input_transformer_manager.transform_cell(raw_cell)
2623 2622 except SyntaxError:
2624 2623 preprocessing_exc_tuple = sys.exc_info()
2625 2624 cell = raw_cell # cell has to exist so it can be stored/logged
2626 2625 else:
2627 2626 if len(cell.splitlines()) == 1:
2628 2627 # Dynamic transformations - only applied for single line commands
2629 2628 with self.builtin_trap:
2630 2629 try:
2631 2630 # use prefilter_lines to handle trailing newlines
2632 2631 # restore trailing newline for ast.parse
2633 2632 cell = self.prefilter_manager.prefilter_lines(cell) + '\n'
2634 2633 except Exception:
2635 2634 # don't allow prefilter errors to crash IPython
2636 2635 preprocessing_exc_tuple = sys.exc_info()
2637 2636
2638 2637 # Store raw and processed history
2639 2638 if store_history:
2640 2639 self.history_manager.store_inputs(self.execution_count,
2641 2640 cell, raw_cell)
2642 2641 if not silent:
2643 2642 self.logger.log(cell, raw_cell)
2644 2643
2645 2644 # Display the exception if input processing failed.
2646 2645 if preprocessing_exc_tuple is not None:
2647 2646 self.showtraceback(preprocessing_exc_tuple)
2648 2647 if store_history:
2649 2648 self.execution_count += 1
2650 2649 return error_before_exec(preprocessing_exc_tuple[2])
2651 2650
2652 2651 # Our own compiler remembers the __future__ environment. If we want to
2653 2652 # run code with a separate __future__ environment, use the default
2654 2653 # compiler
2655 2654 compiler = self.compile if shell_futures else CachingCompiler()
2656 2655
2657 2656 with self.builtin_trap:
2658 2657 cell_name = self.compile.cache(cell, self.execution_count)
2659 2658
2660 2659 with self.display_trap:
2661 2660 # Compile to bytecode
2662 2661 try:
2663 2662 code_ast = compiler.ast_parse(cell, filename=cell_name)
2664 2663 except self.custom_exceptions as e:
2665 2664 etype, value, tb = sys.exc_info()
2666 2665 self.CustomTB(etype, value, tb)
2667 2666 return error_before_exec(e)
2668 2667 except IndentationError as e:
2669 2668 self.showindentationerror()
2670 2669 if store_history:
2671 2670 self.execution_count += 1
2672 2671 return error_before_exec(e)
2673 2672 except (OverflowError, SyntaxError, ValueError, TypeError,
2674 2673 MemoryError) as e:
2675 2674 self.showsyntaxerror()
2676 2675 if store_history:
2677 2676 self.execution_count += 1
2678 2677 return error_before_exec(e)
2679 2678
2680 2679 # Apply AST transformations
2681 2680 try:
2682 2681 code_ast = self.transform_ast(code_ast)
2683 2682 except InputRejected as e:
2684 2683 self.showtraceback()
2685 2684 if store_history:
2686 2685 self.execution_count += 1
2687 2686 return error_before_exec(e)
2688 2687
2689 2688 # Give the displayhook a reference to our ExecutionResult so it
2690 2689 # can fill in the output value.
2691 2690 self.displayhook.exec_result = result
2692 2691
2693 2692 # Execute the user code
2694 2693 interactivity = "none" if silent else self.ast_node_interactivity
2695 2694 has_raised = self.run_ast_nodes(code_ast.body, cell_name,
2696 2695 interactivity=interactivity, compiler=compiler, result=result)
2697 2696
2698 2697 self.last_execution_succeeded = not has_raised
2699 2698
2700 2699 # Reset this so later displayed values do not modify the
2701 2700 # ExecutionResult
2702 2701 self.displayhook.exec_result = None
2703 2702
2704 2703 self.events.trigger('post_execute')
2705 2704 if not silent:
2706 2705 self.events.trigger('post_run_cell')
2707 2706
2708 2707 if store_history:
2709 2708 # Write output to the database. Does nothing unless
2710 2709 # history output logging is enabled.
2711 2710 self.history_manager.store_output(self.execution_count)
2712 2711 # Each cell is a *single* input, regardless of how many lines it has
2713 2712 self.execution_count += 1
2714 2713
2715 2714 return result
2716 2715
2717 2716 def transform_ast(self, node):
2718 2717 """Apply the AST transformations from self.ast_transformers
2719 2718
2720 2719 Parameters
2721 2720 ----------
2722 2721 node : ast.Node
2723 2722 The root node to be transformed. Typically called with the ast.Module
2724 2723 produced by parsing user input.
2725 2724
2726 2725 Returns
2727 2726 -------
2728 2727 An ast.Node corresponding to the node it was called with. Note that it
2729 2728 may also modify the passed object, so don't rely on references to the
2730 2729 original AST.
2731 2730 """
2732 2731 for transformer in self.ast_transformers:
2733 2732 try:
2734 2733 node = transformer.visit(node)
2735 2734 except InputRejected:
2736 2735 # User-supplied AST transformers can reject an input by raising
2737 2736 # an InputRejected. Short-circuit in this case so that we
2738 2737 # don't unregister the transform.
2739 2738 raise
2740 2739 except Exception:
2741 2740 warn("AST transformer %r threw an error. It will be unregistered." % transformer)
2742 2741 self.ast_transformers.remove(transformer)
2743 2742
2744 2743 if self.ast_transformers:
2745 2744 ast.fix_missing_locations(node)
2746 2745 return node
2747 2746
2748 2747
2749 2748 def run_ast_nodes(self, nodelist, cell_name, interactivity='last_expr',
2750 2749 compiler=compile, result=None):
2751 2750 """Run a sequence of AST nodes. The execution mode depends on the
2752 2751 interactivity parameter.
2753 2752
2754 2753 Parameters
2755 2754 ----------
2756 2755 nodelist : list
2757 2756 A sequence of AST nodes to run.
2758 2757 cell_name : str
2759 2758 Will be passed to the compiler as the filename of the cell. Typically
2760 2759 the value returned by ip.compile.cache(cell).
2761 2760 interactivity : str
2762 2761 'all', 'last', 'last_expr' or 'none', specifying which nodes should be
2763 2762 run interactively (displaying output from expressions). 'last_expr'
2764 2763 will run the last node interactively only if it is an expression (i.e.
2765 2764 expressions in loops or other blocks are not displayed. Other values
2766 2765 for this parameter will raise a ValueError.
2767 2766 compiler : callable
2768 2767 A function with the same interface as the built-in compile(), to turn
2769 2768 the AST nodes into code objects. Default is the built-in compile().
2770 2769 result : ExecutionResult, optional
2771 2770 An object to store exceptions that occur during execution.
2772 2771
2773 2772 Returns
2774 2773 -------
2775 2774 True if an exception occurred while running code, False if it finished
2776 2775 running.
2777 2776 """
2778 2777 if not nodelist:
2779 2778 return
2780 2779
2781 2780 if interactivity == 'last_expr':
2782 2781 if isinstance(nodelist[-1], ast.Expr):
2783 2782 interactivity = "last"
2784 2783 else:
2785 2784 interactivity = "none"
2786 2785
2787 2786 if interactivity == 'none':
2788 2787 to_run_exec, to_run_interactive = nodelist, []
2789 2788 elif interactivity == 'last':
2790 2789 to_run_exec, to_run_interactive = nodelist[:-1], nodelist[-1:]
2791 2790 elif interactivity == 'all':
2792 2791 to_run_exec, to_run_interactive = [], nodelist
2793 2792 else:
2794 2793 raise ValueError("Interactivity was %r" % interactivity)
2795 2794
2796 2795 try:
2797 2796 for i, node in enumerate(to_run_exec):
2798 2797 mod = ast.Module([node])
2799 2798 code = compiler(mod, cell_name, "exec")
2800 2799 if self.run_code(code, result):
2801 2800 return True
2802 2801
2803 2802 for i, node in enumerate(to_run_interactive):
2804 2803 mod = ast.Interactive([node])
2805 2804 code = compiler(mod, cell_name, "single")
2806 2805 if self.run_code(code, result):
2807 2806 return True
2808 2807
2809 2808 # Flush softspace
2810 2809 if softspace(sys.stdout, 0):
2811 2810 print()
2812 2811
2813 2812 except:
2814 2813 # It's possible to have exceptions raised here, typically by
2815 2814 # compilation of odd code (such as a naked 'return' outside a
2816 2815 # function) that did parse but isn't valid. Typically the exception
2817 2816 # is a SyntaxError, but it's safest just to catch anything and show
2818 2817 # the user a traceback.
2819 2818
2820 2819 # We do only one try/except outside the loop to minimize the impact
2821 2820 # on runtime, and also because if any node in the node list is
2822 2821 # broken, we should stop execution completely.
2823 2822 if result:
2824 2823 result.error_before_exec = sys.exc_info()[1]
2825 2824 self.showtraceback()
2826 2825 return True
2827 2826
2828 2827 return False
2829 2828
2830 2829 def run_code(self, code_obj, result=None):
2831 2830 """Execute a code object.
2832 2831
2833 2832 When an exception occurs, self.showtraceback() is called to display a
2834 2833 traceback.
2835 2834
2836 2835 Parameters
2837 2836 ----------
2838 2837 code_obj : code object
2839 2838 A compiled code object, to be executed
2840 2839 result : ExecutionResult, optional
2841 2840 An object to store exceptions that occur during execution.
2842 2841
2843 2842 Returns
2844 2843 -------
2845 2844 False : successful execution.
2846 2845 True : an error occurred.
2847 2846 """
2848 2847 # Set our own excepthook in case the user code tries to call it
2849 2848 # directly, so that the IPython crash handler doesn't get triggered
2850 2849 old_excepthook, sys.excepthook = sys.excepthook, self.excepthook
2851 2850
2852 2851 # we save the original sys.excepthook in the instance, in case config
2853 2852 # code (such as magics) needs access to it.
2854 2853 self.sys_excepthook = old_excepthook
2855 2854 outflag = 1 # happens in more places, so it's easier as default
2856 2855 try:
2857 2856 try:
2858 2857 self.hooks.pre_run_code_hook()
2859 2858 #rprint('Running code', repr(code_obj)) # dbg
2860 2859 exec(code_obj, self.user_global_ns, self.user_ns)
2861 2860 finally:
2862 2861 # Reset our crash handler in place
2863 2862 sys.excepthook = old_excepthook
2864 2863 except SystemExit as e:
2865 2864 if result is not None:
2866 2865 result.error_in_exec = e
2867 2866 self.showtraceback(exception_only=True)
2868 2867 warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1)
2869 2868 except self.custom_exceptions:
2870 2869 etype, value, tb = sys.exc_info()
2871 2870 if result is not None:
2872 2871 result.error_in_exec = value
2873 2872 self.CustomTB(etype, value, tb)
2874 2873 except:
2875 2874 if result is not None:
2876 2875 result.error_in_exec = sys.exc_info()[1]
2877 2876 self.showtraceback()
2878 2877 else:
2879 2878 outflag = 0
2880 2879 return outflag
2881 2880
2882 2881 # For backwards compatibility
2883 2882 runcode = run_code
2884 2883
2885 2884 #-------------------------------------------------------------------------
2886 2885 # Things related to GUI support and pylab
2887 2886 #-------------------------------------------------------------------------
2888 2887
2889 2888 active_eventloop = None
2890 2889
2891 2890 def enable_gui(self, gui=None):
2892 2891 raise NotImplementedError('Implement enable_gui in a subclass')
2893 2892
2894 2893 def enable_matplotlib(self, gui=None):
2895 2894 """Enable interactive matplotlib and inline figure support.
2896 2895
2897 2896 This takes the following steps:
2898 2897
2899 2898 1. select the appropriate eventloop and matplotlib backend
2900 2899 2. set up matplotlib for interactive use with that backend
2901 2900 3. configure formatters for inline figure display
2902 2901 4. enable the selected gui eventloop
2903 2902
2904 2903 Parameters
2905 2904 ----------
2906 2905 gui : optional, string
2907 2906 If given, dictates the choice of matplotlib GUI backend to use
2908 2907 (should be one of IPython's supported backends, 'qt', 'osx', 'tk',
2909 2908 'gtk', 'wx' or 'inline'), otherwise we use the default chosen by
2910 2909 matplotlib (as dictated by the matplotlib build-time options plus the
2911 2910 user's matplotlibrc configuration file). Note that not all backends
2912 2911 make sense in all contexts, for example a terminal ipython can't
2913 2912 display figures inline.
2914 2913 """
2915 2914 from IPython.core import pylabtools as pt
2916 2915 gui, backend = pt.find_gui_and_backend(gui, self.pylab_gui_select)
2917 2916
2918 2917 if gui != 'inline':
2919 2918 # If we have our first gui selection, store it
2920 2919 if self.pylab_gui_select is None:
2921 2920 self.pylab_gui_select = gui
2922 2921 # Otherwise if they are different
2923 2922 elif gui != self.pylab_gui_select:
2924 2923 print ('Warning: Cannot change to a different GUI toolkit: %s.'
2925 2924 ' Using %s instead.' % (gui, self.pylab_gui_select))
2926 2925 gui, backend = pt.find_gui_and_backend(self.pylab_gui_select)
2927 2926
2928 2927 pt.activate_matplotlib(backend)
2929 2928 pt.configure_inline_support(self, backend)
2930 2929
2931 2930 # Now we must activate the gui pylab wants to use, and fix %run to take
2932 2931 # plot updates into account
2933 2932 self.enable_gui(gui)
2934 2933 self.magics_manager.registry['ExecutionMagics'].default_runner = \
2935 2934 pt.mpl_runner(self.safe_execfile)
2936 2935
2937 2936 return gui, backend
2938 2937
2939 2938 def enable_pylab(self, gui=None, import_all=True, welcome_message=False):
2940 2939 """Activate pylab support at runtime.
2941 2940
2942 2941 This turns on support for matplotlib, preloads into the interactive
2943 2942 namespace all of numpy and pylab, and configures IPython to correctly
2944 2943 interact with the GUI event loop. The GUI backend to be used can be
2945 2944 optionally selected with the optional ``gui`` argument.
2946 2945
2947 2946 This method only adds preloading the namespace to InteractiveShell.enable_matplotlib.
2948 2947
2949 2948 Parameters
2950 2949 ----------
2951 2950 gui : optional, string
2952 2951 If given, dictates the choice of matplotlib GUI backend to use
2953 2952 (should be one of IPython's supported backends, 'qt', 'osx', 'tk',
2954 2953 'gtk', 'wx' or 'inline'), otherwise we use the default chosen by
2955 2954 matplotlib (as dictated by the matplotlib build-time options plus the
2956 2955 user's matplotlibrc configuration file). Note that not all backends
2957 2956 make sense in all contexts, for example a terminal ipython can't
2958 2957 display figures inline.
2959 2958 import_all : optional, bool, default: True
2960 2959 Whether to do `from numpy import *` and `from pylab import *`
2961 2960 in addition to module imports.
2962 2961 welcome_message : deprecated
2963 2962 This argument is ignored, no welcome message will be displayed.
2964 2963 """
2965 2964 from IPython.core.pylabtools import import_pylab
2966 2965
2967 2966 gui, backend = self.enable_matplotlib(gui)
2968 2967
2969 2968 # We want to prevent the loading of pylab to pollute the user's
2970 2969 # namespace as shown by the %who* magics, so we execute the activation
2971 2970 # code in an empty namespace, and we update *both* user_ns and
2972 2971 # user_ns_hidden with this information.
2973 2972 ns = {}
2974 2973 import_pylab(ns, import_all)
2975 2974 # warn about clobbered names
2976 2975 ignored = {"__builtins__"}
2977 2976 both = set(ns).intersection(self.user_ns).difference(ignored)
2978 2977 clobbered = [ name for name in both if self.user_ns[name] is not ns[name] ]
2979 2978 self.user_ns.update(ns)
2980 2979 self.user_ns_hidden.update(ns)
2981 2980 return gui, backend, clobbered
2982 2981
2983 2982 #-------------------------------------------------------------------------
2984 2983 # Utilities
2985 2984 #-------------------------------------------------------------------------
2986 2985
2987 2986 def var_expand(self, cmd, depth=0, formatter=DollarFormatter()):
2988 2987 """Expand python variables in a string.
2989 2988
2990 2989 The depth argument indicates how many frames above the caller should
2991 2990 be walked to look for the local namespace where to expand variables.
2992 2991
2993 2992 The global namespace for expansion is always the user's interactive
2994 2993 namespace.
2995 2994 """
2996 2995 ns = self.user_ns.copy()
2997 2996 try:
2998 2997 frame = sys._getframe(depth+1)
2999 2998 except ValueError:
3000 2999 # This is thrown if there aren't that many frames on the stack,
3001 3000 # e.g. if a script called run_line_magic() directly.
3002 3001 pass
3003 3002 else:
3004 3003 ns.update(frame.f_locals)
3005 3004
3006 3005 try:
3007 3006 # We have to use .vformat() here, because 'self' is a valid and common
3008 3007 # name, and expanding **ns for .format() would make it collide with
3009 3008 # the 'self' argument of the method.
3010 3009 cmd = formatter.vformat(cmd, args=[], kwargs=ns)
3011 3010 except Exception:
3012 3011 # if formatter couldn't format, just let it go untransformed
3013 3012 pass
3014 3013 return cmd
3015 3014
3016 3015 def mktempfile(self, data=None, prefix='ipython_edit_'):
3017 3016 """Make a new tempfile and return its filename.
3018 3017
3019 3018 This makes a call to tempfile.mkstemp (created in a tempfile.mkdtemp),
3020 3019 but it registers the created filename internally so ipython cleans it up
3021 3020 at exit time.
3022 3021
3023 3022 Optional inputs:
3024 3023
3025 3024 - data(None): if data is given, it gets written out to the temp file
3026 3025 immediately, and the file is closed again."""
3027 3026
3028 3027 dirname = tempfile.mkdtemp(prefix=prefix)
3029 3028 self.tempdirs.append(dirname)
3030 3029
3031 3030 handle, filename = tempfile.mkstemp('.py', prefix, dir=dirname)
3032 3031 os.close(handle) # On Windows, there can only be one open handle on a file
3033 3032 self.tempfiles.append(filename)
3034 3033
3035 3034 if data:
3036 3035 tmp_file = open(filename,'w')
3037 3036 tmp_file.write(data)
3038 3037 tmp_file.close()
3039 3038 return filename
3040 3039
3041 3040 @undoc
3042 3041 def write(self,data):
3043 3042 """DEPRECATED: Write a string to the default output"""
3044 3043 warn('InteractiveShell.write() is deprecated, use sys.stdout instead',
3045 3044 DeprecationWarning, stacklevel=2)
3046 3045 sys.stdout.write(data)
3047 3046
3048 3047 @undoc
3049 3048 def write_err(self,data):
3050 3049 """DEPRECATED: Write a string to the default error output"""
3051 3050 warn('InteractiveShell.write_err() is deprecated, use sys.stderr instead',
3052 3051 DeprecationWarning, stacklevel=2)
3053 3052 sys.stderr.write(data)
3054 3053
3055 3054 def ask_yes_no(self, prompt, default=None, interrupt=None):
3056 3055 if self.quiet:
3057 3056 return True
3058 3057 return ask_yes_no(prompt,default,interrupt)
3059 3058
3060 3059 def show_usage(self):
3061 3060 """Show a usage message"""
3062 3061 page.page(IPython.core.usage.interactive_usage)
3063 3062
3064 3063 def extract_input_lines(self, range_str, raw=False):
3065 3064 """Return as a string a set of input history slices.
3066 3065
3067 3066 Parameters
3068 3067 ----------
3069 3068 range_str : string
3070 3069 The set of slices is given as a string, like "~5/6-~4/2 4:8 9",
3071 3070 since this function is for use by magic functions which get their
3072 3071 arguments as strings. The number before the / is the session
3073 3072 number: ~n goes n back from the current session.
3074 3073
3075 3074 raw : bool, optional
3076 3075 By default, the processed input is used. If this is true, the raw
3077 3076 input history is used instead.
3078 3077
3079 3078 Notes
3080 3079 -----
3081 3080
3082 3081 Slices can be described with two notations:
3083 3082
3084 3083 * ``N:M`` -> standard python form, means including items N...(M-1).
3085 3084 * ``N-M`` -> include items N..M (closed endpoint).
3086 3085 """
3087 3086 lines = self.history_manager.get_range_by_str(range_str, raw=raw)
3088 3087 return "\n".join(x for _, _, x in lines)
3089 3088
3090 3089 def find_user_code(self, target, raw=True, py_only=False, skip_encoding_cookie=True, search_ns=False):
3091 3090 """Get a code string from history, file, url, or a string or macro.
3092 3091
3093 3092 This is mainly used by magic functions.
3094 3093
3095 3094 Parameters
3096 3095 ----------
3097 3096
3098 3097 target : str
3099 3098
3100 3099 A string specifying code to retrieve. This will be tried respectively
3101 3100 as: ranges of input history (see %history for syntax), url,
3102 3101 corresponding .py file, filename, or an expression evaluating to a
3103 3102 string or Macro in the user namespace.
3104 3103
3105 3104 raw : bool
3106 3105 If true (default), retrieve raw history. Has no effect on the other
3107 3106 retrieval mechanisms.
3108 3107
3109 3108 py_only : bool (default False)
3110 3109 Only try to fetch python code, do not try alternative methods to decode file
3111 3110 if unicode fails.
3112 3111
3113 3112 Returns
3114 3113 -------
3115 3114 A string of code.
3116 3115
3117 3116 ValueError is raised if nothing is found, and TypeError if it evaluates
3118 3117 to an object of another type. In each case, .args[0] is a printable
3119 3118 message.
3120 3119 """
3121 3120 code = self.extract_input_lines(target, raw=raw) # Grab history
3122 3121 if code:
3123 3122 return code
3124 3123 try:
3125 3124 if target.startswith(('http://', 'https://')):
3126 3125 return openpy.read_py_url(target, skip_encoding_cookie=skip_encoding_cookie)
3127 3126 except UnicodeDecodeError:
3128 3127 if not py_only :
3129 3128 # Deferred import
3130 3129 try:
3131 3130 from urllib.request import urlopen # Py3
3132 3131 except ImportError:
3133 3132 from urllib import urlopen
3134 3133 response = urlopen(target)
3135 3134 return response.read().decode('latin1')
3136 3135 raise ValueError(("'%s' seem to be unreadable.") % target)
3137 3136
3138 3137 potential_target = [target]
3139 3138 try :
3140 3139 potential_target.insert(0,get_py_filename(target))
3141 3140 except IOError:
3142 3141 pass
3143 3142
3144 3143 for tgt in potential_target :
3145 3144 if os.path.isfile(tgt): # Read file
3146 3145 try :
3147 3146 return openpy.read_py_file(tgt, skip_encoding_cookie=skip_encoding_cookie)
3148 3147 except UnicodeDecodeError :
3149 3148 if not py_only :
3150 3149 with io_open(tgt,'r', encoding='latin1') as f :
3151 3150 return f.read()
3152 3151 raise ValueError(("'%s' seem to be unreadable.") % target)
3153 3152 elif os.path.isdir(os.path.expanduser(tgt)):
3154 3153 raise ValueError("'%s' is a directory, not a regular file." % target)
3155 3154
3156 3155 if search_ns:
3157 3156 # Inspect namespace to load object source
3158 3157 object_info = self.object_inspect(target, detail_level=1)
3159 3158 if object_info['found'] and object_info['source']:
3160 3159 return object_info['source']
3161 3160
3162 3161 try: # User namespace
3163 3162 codeobj = eval(target, self.user_ns)
3164 3163 except Exception:
3165 3164 raise ValueError(("'%s' was not found in history, as a file, url, "
3166 3165 "nor in the user namespace.") % target)
3167 3166
3168 if isinstance(codeobj, string_types):
3167 if isinstance(codeobj, str):
3169 3168 return codeobj
3170 3169 elif isinstance(codeobj, Macro):
3171 3170 return codeobj.value
3172 3171
3173 3172 raise TypeError("%s is neither a string nor a macro." % target,
3174 3173 codeobj)
3175 3174
3176 3175 #-------------------------------------------------------------------------
3177 3176 # Things related to IPython exiting
3178 3177 #-------------------------------------------------------------------------
3179 3178 def atexit_operations(self):
3180 3179 """This will be executed at the time of exit.
3181 3180
3182 3181 Cleanup operations and saving of persistent data that is done
3183 3182 unconditionally by IPython should be performed here.
3184 3183
3185 3184 For things that may depend on startup flags or platform specifics (such
3186 3185 as having readline or not), register a separate atexit function in the
3187 3186 code that has the appropriate information, rather than trying to
3188 3187 clutter
3189 3188 """
3190 3189 # Close the history session (this stores the end time and line count)
3191 3190 # this must be *before* the tempfile cleanup, in case of temporary
3192 3191 # history db
3193 3192 self.history_manager.end_session()
3194 3193
3195 3194 # Cleanup all tempfiles and folders left around
3196 3195 for tfile in self.tempfiles:
3197 3196 try:
3198 3197 os.unlink(tfile)
3199 3198 except OSError:
3200 3199 pass
3201 3200
3202 3201 for tdir in self.tempdirs:
3203 3202 try:
3204 3203 os.rmdir(tdir)
3205 3204 except OSError:
3206 3205 pass
3207 3206
3208 3207 # Clear all user namespaces to release all references cleanly.
3209 3208 self.reset(new_session=False)
3210 3209
3211 3210 # Run user hooks
3212 3211 self.hooks.shutdown_hook()
3213 3212
3214 3213 def cleanup(self):
3215 3214 self.restore_sys_module_state()
3216 3215
3217 3216
3218 3217 # Overridden in terminal subclass to change prompts
3219 3218 def switch_doctest_mode(self, mode):
3220 3219 pass
3221 3220
3222 3221
3223 3222 class InteractiveShellABC(with_metaclass(abc.ABCMeta, object)):
3224 3223 """An abstract base class for InteractiveShell."""
3225 3224
3226 3225 InteractiveShellABC.register(InteractiveShell)
@@ -1,57 +1,57 b''
1 1 """Support for interactive macros in IPython"""
2 2
3 3 #*****************************************************************************
4 4 # Copyright (C) 2001-2005 Fernando Perez <fperez@colorado.edu>
5 5 #
6 6 # Distributed under the terms of the BSD License. The full license is in
7 7 # the file COPYING, distributed as part of this software.
8 8 #*****************************************************************************
9 9
10 10 import re
11 11
12 12 from IPython.utils import py3compat
13 13 from IPython.utils.encoding import DEFAULT_ENCODING
14 14
15 15 coding_declaration = re.compile(r"#\s*coding[:=]\s*([-\w.]+)")
16 16
17 17 class Macro(object):
18 18 """Simple class to store the value of macros as strings.
19 19
20 20 Macro is just a callable that executes a string of IPython
21 21 input when called.
22 22 """
23 23
24 24 def __init__(self,code):
25 25 """store the macro value, as a single string which can be executed"""
26 26 lines = []
27 27 enc = None
28 28 for line in code.splitlines():
29 29 coding_match = coding_declaration.match(line)
30 30 if coding_match:
31 31 enc = coding_match.group(1)
32 32 else:
33 33 lines.append(line)
34 34 code = "\n".join(lines)
35 35 if isinstance(code, bytes):
36 36 code = code.decode(enc or DEFAULT_ENCODING)
37 37 self.value = code + '\n'
38 38
39 39 def __str__(self):
40 40 return py3compat.unicode_to_str(self.value)
41 41
42 42 def __unicode__(self):
43 43 return self.value
44 44
45 45 def __repr__(self):
46 46 return 'IPython.macro.Macro(%s)' % repr(self.value)
47 47
48 48 def __getstate__(self):
49 49 """ needed for safe pickling via %store """
50 50 return {'value': self.value}
51 51
52 52 def __add__(self, other):
53 53 if isinstance(other, Macro):
54 54 return Macro(self.value + other.value)
55 elif isinstance(other, py3compat.string_types):
55 elif isinstance(other, str):
56 56 return Macro(self.value + other)
57 57 raise TypeError
@@ -1,679 +1,678 b''
1 1 # encoding: utf-8
2 2 """Magic functions for InteractiveShell.
3 3 """
4 4
5 5 #-----------------------------------------------------------------------------
6 6 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and
7 7 # Copyright (C) 2001 Fernando Perez <fperez@colorado.edu>
8 8 # Copyright (C) 2008 The IPython Development Team
9 9
10 10 # Distributed under the terms of the BSD License. The full license is in
11 11 # the file COPYING, distributed as part of this software.
12 12 #-----------------------------------------------------------------------------
13 13
14 14 import os
15 15 import re
16 16 import sys
17 17 import types
18 18 from getopt import getopt, GetoptError
19 19
20 20 from traitlets.config.configurable import Configurable
21 21 from IPython.core import oinspect
22 22 from IPython.core.error import UsageError
23 23 from IPython.core.inputsplitter import ESC_MAGIC, ESC_MAGIC2
24 24 from decorator import decorator
25 25 from IPython.utils.ipstruct import Struct
26 26 from IPython.utils.process import arg_split
27 from IPython.utils.py3compat import string_types
28 27 from IPython.utils.text import dedent
29 28 from traitlets import Bool, Dict, Instance, observe
30 29 from logging import error
31 30
32 31 #-----------------------------------------------------------------------------
33 32 # Globals
34 33 #-----------------------------------------------------------------------------
35 34
36 35 # A dict we'll use for each class that has magics, used as temporary storage to
37 36 # pass information between the @line/cell_magic method decorators and the
38 37 # @magics_class class decorator, because the method decorators have no
39 38 # access to the class when they run. See for more details:
40 39 # http://stackoverflow.com/questions/2366713/can-a-python-decorator-of-an-instance-method-access-the-class
41 40
42 41 magics = dict(line={}, cell={})
43 42
44 43 magic_kinds = ('line', 'cell')
45 44 magic_spec = ('line', 'cell', 'line_cell')
46 45 magic_escapes = dict(line=ESC_MAGIC, cell=ESC_MAGIC2)
47 46
48 47 #-----------------------------------------------------------------------------
49 48 # Utility classes and functions
50 49 #-----------------------------------------------------------------------------
51 50
52 51 class Bunch: pass
53 52
54 53
55 54 def on_off(tag):
56 55 """Return an ON/OFF string for a 1/0 input. Simple utility function."""
57 56 return ['OFF','ON'][tag]
58 57
59 58
60 59 def compress_dhist(dh):
61 60 """Compress a directory history into a new one with at most 20 entries.
62 61
63 62 Return a new list made from the first and last 10 elements of dhist after
64 63 removal of duplicates.
65 64 """
66 65 head, tail = dh[:-10], dh[-10:]
67 66
68 67 newhead = []
69 68 done = set()
70 69 for h in head:
71 70 if h in done:
72 71 continue
73 72 newhead.append(h)
74 73 done.add(h)
75 74
76 75 return newhead + tail
77 76
78 77
79 78 def needs_local_scope(func):
80 79 """Decorator to mark magic functions which need to local scope to run."""
81 80 func.needs_local_scope = True
82 81 return func
83 82
84 83 #-----------------------------------------------------------------------------
85 84 # Class and method decorators for registering magics
86 85 #-----------------------------------------------------------------------------
87 86
88 87 def magics_class(cls):
89 88 """Class decorator for all subclasses of the main Magics class.
90 89
91 90 Any class that subclasses Magics *must* also apply this decorator, to
92 91 ensure that all the methods that have been decorated as line/cell magics
93 92 get correctly registered in the class instance. This is necessary because
94 93 when method decorators run, the class does not exist yet, so they
95 94 temporarily store their information into a module global. Application of
96 95 this class decorator copies that global data to the class instance and
97 96 clears the global.
98 97
99 98 Obviously, this mechanism is not thread-safe, which means that the
100 99 *creation* of subclasses of Magic should only be done in a single-thread
101 100 context. Instantiation of the classes has no restrictions. Given that
102 101 these classes are typically created at IPython startup time and before user
103 102 application code becomes active, in practice this should not pose any
104 103 problems.
105 104 """
106 105 cls.registered = True
107 106 cls.magics = dict(line = magics['line'],
108 107 cell = magics['cell'])
109 108 magics['line'] = {}
110 109 magics['cell'] = {}
111 110 return cls
112 111
113 112
114 113 def record_magic(dct, magic_kind, magic_name, func):
115 114 """Utility function to store a function as a magic of a specific kind.
116 115
117 116 Parameters
118 117 ----------
119 118 dct : dict
120 119 A dictionary with 'line' and 'cell' subdicts.
121 120
122 121 magic_kind : str
123 122 Kind of magic to be stored.
124 123
125 124 magic_name : str
126 125 Key to store the magic as.
127 126
128 127 func : function
129 128 Callable object to store.
130 129 """
131 130 if magic_kind == 'line_cell':
132 131 dct['line'][magic_name] = dct['cell'][magic_name] = func
133 132 else:
134 133 dct[magic_kind][magic_name] = func
135 134
136 135
137 136 def validate_type(magic_kind):
138 137 """Ensure that the given magic_kind is valid.
139 138
140 139 Check that the given magic_kind is one of the accepted spec types (stored
141 140 in the global `magic_spec`), raise ValueError otherwise.
142 141 """
143 142 if magic_kind not in magic_spec:
144 143 raise ValueError('magic_kind must be one of %s, %s given' %
145 144 magic_kinds, magic_kind)
146 145
147 146
148 147 # The docstrings for the decorator below will be fairly similar for the two
149 148 # types (method and function), so we generate them here once and reuse the
150 149 # templates below.
151 150 _docstring_template = \
152 151 """Decorate the given {0} as {1} magic.
153 152
154 153 The decorator can be used with or without arguments, as follows.
155 154
156 155 i) without arguments: it will create a {1} magic named as the {0} being
157 156 decorated::
158 157
159 158 @deco
160 159 def foo(...)
161 160
162 161 will create a {1} magic named `foo`.
163 162
164 163 ii) with one string argument: which will be used as the actual name of the
165 164 resulting magic::
166 165
167 166 @deco('bar')
168 167 def foo(...)
169 168
170 169 will create a {1} magic named `bar`.
171 170 """
172 171
173 172 # These two are decorator factories. While they are conceptually very similar,
174 173 # there are enough differences in the details that it's simpler to have them
175 174 # written as completely standalone functions rather than trying to share code
176 175 # and make a single one with convoluted logic.
177 176
178 177 def _method_magic_marker(magic_kind):
179 178 """Decorator factory for methods in Magics subclasses.
180 179 """
181 180
182 181 validate_type(magic_kind)
183 182
184 183 # This is a closure to capture the magic_kind. We could also use a class,
185 184 # but it's overkill for just that one bit of state.
186 185 def magic_deco(arg):
187 186 call = lambda f, *a, **k: f(*a, **k)
188 187
189 188 if callable(arg):
190 189 # "Naked" decorator call (just @foo, no args)
191 190 func = arg
192 191 name = func.__name__
193 192 retval = decorator(call, func)
194 193 record_magic(magics, magic_kind, name, name)
195 elif isinstance(arg, string_types):
194 elif isinstance(arg, str):
196 195 # Decorator called with arguments (@foo('bar'))
197 196 name = arg
198 197 def mark(func, *a, **kw):
199 198 record_magic(magics, magic_kind, name, func.__name__)
200 199 return decorator(call, func)
201 200 retval = mark
202 201 else:
203 202 raise TypeError("Decorator can only be called with "
204 203 "string or function")
205 204 return retval
206 205
207 206 # Ensure the resulting decorator has a usable docstring
208 207 magic_deco.__doc__ = _docstring_template.format('method', magic_kind)
209 208 return magic_deco
210 209
211 210
212 211 def _function_magic_marker(magic_kind):
213 212 """Decorator factory for standalone functions.
214 213 """
215 214 validate_type(magic_kind)
216 215
217 216 # This is a closure to capture the magic_kind. We could also use a class,
218 217 # but it's overkill for just that one bit of state.
219 218 def magic_deco(arg):
220 219 call = lambda f, *a, **k: f(*a, **k)
221 220
222 221 # Find get_ipython() in the caller's namespace
223 222 caller = sys._getframe(1)
224 223 for ns in ['f_locals', 'f_globals', 'f_builtins']:
225 224 get_ipython = getattr(caller, ns).get('get_ipython')
226 225 if get_ipython is not None:
227 226 break
228 227 else:
229 228 raise NameError('Decorator can only run in context where '
230 229 '`get_ipython` exists')
231 230
232 231 ip = get_ipython()
233 232
234 233 if callable(arg):
235 234 # "Naked" decorator call (just @foo, no args)
236 235 func = arg
237 236 name = func.__name__
238 237 ip.register_magic_function(func, magic_kind, name)
239 238 retval = decorator(call, func)
240 elif isinstance(arg, string_types):
239 elif isinstance(arg, str):
241 240 # Decorator called with arguments (@foo('bar'))
242 241 name = arg
243 242 def mark(func, *a, **kw):
244 243 ip.register_magic_function(func, magic_kind, name)
245 244 return decorator(call, func)
246 245 retval = mark
247 246 else:
248 247 raise TypeError("Decorator can only be called with "
249 248 "string or function")
250 249 return retval
251 250
252 251 # Ensure the resulting decorator has a usable docstring
253 252 ds = _docstring_template.format('function', magic_kind)
254 253
255 254 ds += dedent("""
256 255 Note: this decorator can only be used in a context where IPython is already
257 256 active, so that the `get_ipython()` call succeeds. You can therefore use
258 257 it in your startup files loaded after IPython initializes, but *not* in the
259 258 IPython configuration file itself, which is executed before IPython is
260 259 fully up and running. Any file located in the `startup` subdirectory of
261 260 your configuration profile will be OK in this sense.
262 261 """)
263 262
264 263 magic_deco.__doc__ = ds
265 264 return magic_deco
266 265
267 266
268 267 # Create the actual decorators for public use
269 268
270 269 # These three are used to decorate methods in class definitions
271 270 line_magic = _method_magic_marker('line')
272 271 cell_magic = _method_magic_marker('cell')
273 272 line_cell_magic = _method_magic_marker('line_cell')
274 273
275 274 # These three decorate standalone functions and perform the decoration
276 275 # immediately. They can only run where get_ipython() works
277 276 register_line_magic = _function_magic_marker('line')
278 277 register_cell_magic = _function_magic_marker('cell')
279 278 register_line_cell_magic = _function_magic_marker('line_cell')
280 279
281 280 #-----------------------------------------------------------------------------
282 281 # Core Magic classes
283 282 #-----------------------------------------------------------------------------
284 283
285 284 class MagicsManager(Configurable):
286 285 """Object that handles all magic-related functionality for IPython.
287 286 """
288 287 # Non-configurable class attributes
289 288
290 289 # A two-level dict, first keyed by magic type, then by magic function, and
291 290 # holding the actual callable object as value. This is the dict used for
292 291 # magic function dispatch
293 292 magics = Dict()
294 293
295 294 # A registry of the original objects that we've been given holding magics.
296 295 registry = Dict()
297 296
298 297 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC', allow_none=True)
299 298
300 299 auto_magic = Bool(True, help=
301 300 "Automatically call line magics without requiring explicit % prefix"
302 301 ).tag(config=True)
303 302 @observe('auto_magic')
304 303 def _auto_magic_changed(self, change):
305 304 self.shell.automagic = change['new']
306 305
307 306 _auto_status = [
308 307 'Automagic is OFF, % prefix IS needed for line magics.',
309 308 'Automagic is ON, % prefix IS NOT needed for line magics.']
310 309
311 310 user_magics = Instance('IPython.core.magics.UserMagics', allow_none=True)
312 311
313 312 def __init__(self, shell=None, config=None, user_magics=None, **traits):
314 313
315 314 super(MagicsManager, self).__init__(shell=shell, config=config,
316 315 user_magics=user_magics, **traits)
317 316 self.magics = dict(line={}, cell={})
318 317 # Let's add the user_magics to the registry for uniformity, so *all*
319 318 # registered magic containers can be found there.
320 319 self.registry[user_magics.__class__.__name__] = user_magics
321 320
322 321 def auto_status(self):
323 322 """Return descriptive string with automagic status."""
324 323 return self._auto_status[self.auto_magic]
325 324
326 325 def lsmagic(self):
327 326 """Return a dict of currently available magic functions.
328 327
329 328 The return dict has the keys 'line' and 'cell', corresponding to the
330 329 two types of magics we support. Each value is a list of names.
331 330 """
332 331 return self.magics
333 332
334 333 def lsmagic_docs(self, brief=False, missing=''):
335 334 """Return dict of documentation of magic functions.
336 335
337 336 The return dict has the keys 'line' and 'cell', corresponding to the
338 337 two types of magics we support. Each value is a dict keyed by magic
339 338 name whose value is the function docstring. If a docstring is
340 339 unavailable, the value of `missing` is used instead.
341 340
342 341 If brief is True, only the first line of each docstring will be returned.
343 342 """
344 343 docs = {}
345 344 for m_type in self.magics:
346 345 m_docs = {}
347 346 for m_name, m_func in self.magics[m_type].items():
348 347 if m_func.__doc__:
349 348 if brief:
350 349 m_docs[m_name] = m_func.__doc__.split('\n', 1)[0]
351 350 else:
352 351 m_docs[m_name] = m_func.__doc__.rstrip()
353 352 else:
354 353 m_docs[m_name] = missing
355 354 docs[m_type] = m_docs
356 355 return docs
357 356
358 357 def register(self, *magic_objects):
359 358 """Register one or more instances of Magics.
360 359
361 360 Take one or more classes or instances of classes that subclass the main
362 361 `core.Magic` class, and register them with IPython to use the magic
363 362 functions they provide. The registration process will then ensure that
364 363 any methods that have decorated to provide line and/or cell magics will
365 364 be recognized with the `%x`/`%%x` syntax as a line/cell magic
366 365 respectively.
367 366
368 367 If classes are given, they will be instantiated with the default
369 368 constructor. If your classes need a custom constructor, you should
370 369 instanitate them first and pass the instance.
371 370
372 371 The provided arguments can be an arbitrary mix of classes and instances.
373 372
374 373 Parameters
375 374 ----------
376 375 magic_objects : one or more classes or instances
377 376 """
378 377 # Start by validating them to ensure they have all had their magic
379 378 # methods registered at the instance level
380 379 for m in magic_objects:
381 380 if not m.registered:
382 381 raise ValueError("Class of magics %r was constructed without "
383 382 "the @register_magics class decorator")
384 383 if isinstance(m, type):
385 384 # If we're given an uninstantiated class
386 385 m = m(shell=self.shell)
387 386
388 387 # Now that we have an instance, we can register it and update the
389 388 # table of callables
390 389 self.registry[m.__class__.__name__] = m
391 390 for mtype in magic_kinds:
392 391 self.magics[mtype].update(m.magics[mtype])
393 392
394 393 def register_function(self, func, magic_kind='line', magic_name=None):
395 394 """Expose a standalone function as magic function for IPython.
396 395
397 396 This will create an IPython magic (line, cell or both) from a
398 397 standalone function. The functions should have the following
399 398 signatures:
400 399
401 400 * For line magics: `def f(line)`
402 401 * For cell magics: `def f(line, cell)`
403 402 * For a function that does both: `def f(line, cell=None)`
404 403
405 404 In the latter case, the function will be called with `cell==None` when
406 405 invoked as `%f`, and with cell as a string when invoked as `%%f`.
407 406
408 407 Parameters
409 408 ----------
410 409 func : callable
411 410 Function to be registered as a magic.
412 411
413 412 magic_kind : str
414 413 Kind of magic, one of 'line', 'cell' or 'line_cell'
415 414
416 415 magic_name : optional str
417 416 If given, the name the magic will have in the IPython namespace. By
418 417 default, the name of the function itself is used.
419 418 """
420 419
421 420 # Create the new method in the user_magics and register it in the
422 421 # global table
423 422 validate_type(magic_kind)
424 423 magic_name = func.__name__ if magic_name is None else magic_name
425 424 setattr(self.user_magics, magic_name, func)
426 425 record_magic(self.magics, magic_kind, magic_name, func)
427 426
428 427 def register_alias(self, alias_name, magic_name, magic_kind='line'):
429 428 """Register an alias to a magic function.
430 429
431 430 The alias is an instance of :class:`MagicAlias`, which holds the
432 431 name and kind of the magic it should call. Binding is done at
433 432 call time, so if the underlying magic function is changed the alias
434 433 will call the new function.
435 434
436 435 Parameters
437 436 ----------
438 437 alias_name : str
439 438 The name of the magic to be registered.
440 439
441 440 magic_name : str
442 441 The name of an existing magic.
443 442
444 443 magic_kind : str
445 444 Kind of magic, one of 'line' or 'cell'
446 445 """
447 446
448 447 # `validate_type` is too permissive, as it allows 'line_cell'
449 448 # which we do not handle.
450 449 if magic_kind not in magic_kinds:
451 450 raise ValueError('magic_kind must be one of %s, %s given' %
452 451 magic_kinds, magic_kind)
453 452
454 453 alias = MagicAlias(self.shell, magic_name, magic_kind)
455 454 setattr(self.user_magics, alias_name, alias)
456 455 record_magic(self.magics, magic_kind, alias_name, alias)
457 456
458 457 # Key base class that provides the central functionality for magics.
459 458
460 459
461 460 class Magics(Configurable):
462 461 """Base class for implementing magic functions.
463 462
464 463 Shell functions which can be reached as %function_name. All magic
465 464 functions should accept a string, which they can parse for their own
466 465 needs. This can make some functions easier to type, eg `%cd ../`
467 466 vs. `%cd("../")`
468 467
469 468 Classes providing magic functions need to subclass this class, and they
470 469 MUST:
471 470
472 471 - Use the method decorators `@line_magic` and `@cell_magic` to decorate
473 472 individual methods as magic functions, AND
474 473
475 474 - Use the class decorator `@magics_class` to ensure that the magic
476 475 methods are properly registered at the instance level upon instance
477 476 initialization.
478 477
479 478 See :mod:`magic_functions` for examples of actual implementation classes.
480 479 """
481 480 # Dict holding all command-line options for each magic.
482 481 options_table = None
483 482 # Dict for the mapping of magic names to methods, set by class decorator
484 483 magics = None
485 484 # Flag to check that the class decorator was properly applied
486 485 registered = False
487 486 # Instance of IPython shell
488 487 shell = None
489 488
490 489 def __init__(self, shell=None, **kwargs):
491 490 if not(self.__class__.registered):
492 491 raise ValueError('Magics subclass without registration - '
493 492 'did you forget to apply @magics_class?')
494 493 if shell is not None:
495 494 if hasattr(shell, 'configurables'):
496 495 shell.configurables.append(self)
497 496 if hasattr(shell, 'config'):
498 497 kwargs.setdefault('parent', shell)
499 498
500 499 self.shell = shell
501 500 self.options_table = {}
502 501 # The method decorators are run when the instance doesn't exist yet, so
503 502 # they can only record the names of the methods they are supposed to
504 503 # grab. Only now, that the instance exists, can we create the proper
505 504 # mapping to bound methods. So we read the info off the original names
506 505 # table and replace each method name by the actual bound method.
507 506 # But we mustn't clobber the *class* mapping, in case of multiple instances.
508 507 class_magics = self.magics
509 508 self.magics = {}
510 509 for mtype in magic_kinds:
511 510 tab = self.magics[mtype] = {}
512 511 cls_tab = class_magics[mtype]
513 512 for magic_name, meth_name in cls_tab.items():
514 if isinstance(meth_name, string_types):
513 if isinstance(meth_name, str):
515 514 # it's a method name, grab it
516 515 tab[magic_name] = getattr(self, meth_name)
517 516 else:
518 517 # it's the real thing
519 518 tab[magic_name] = meth_name
520 519 # Configurable **needs** to be initiated at the end or the config
521 520 # magics get screwed up.
522 521 super(Magics, self).__init__(**kwargs)
523 522
524 523 def arg_err(self,func):
525 524 """Print docstring if incorrect arguments were passed"""
526 525 print('Error in arguments:')
527 526 print(oinspect.getdoc(func))
528 527
529 528 def format_latex(self, strng):
530 529 """Format a string for latex inclusion."""
531 530
532 531 # Characters that need to be escaped for latex:
533 532 escape_re = re.compile(r'(%|_|\$|#|&)',re.MULTILINE)
534 533 # Magic command names as headers:
535 534 cmd_name_re = re.compile(r'^(%s.*?):' % ESC_MAGIC,
536 535 re.MULTILINE)
537 536 # Magic commands
538 537 cmd_re = re.compile(r'(?P<cmd>%s.+?\b)(?!\}\}:)' % ESC_MAGIC,
539 538 re.MULTILINE)
540 539 # Paragraph continue
541 540 par_re = re.compile(r'\\$',re.MULTILINE)
542 541
543 542 # The "\n" symbol
544 543 newline_re = re.compile(r'\\n')
545 544
546 545 # Now build the string for output:
547 546 #strng = cmd_name_re.sub(r'\n\\texttt{\\textsl{\\large \1}}:',strng)
548 547 strng = cmd_name_re.sub(r'\n\\bigskip\n\\texttt{\\textbf{ \1}}:',
549 548 strng)
550 549 strng = cmd_re.sub(r'\\texttt{\g<cmd>}',strng)
551 550 strng = par_re.sub(r'\\\\',strng)
552 551 strng = escape_re.sub(r'\\\1',strng)
553 552 strng = newline_re.sub(r'\\textbackslash{}n',strng)
554 553 return strng
555 554
556 555 def parse_options(self, arg_str, opt_str, *long_opts, **kw):
557 556 """Parse options passed to an argument string.
558 557
559 558 The interface is similar to that of :func:`getopt.getopt`, but it
560 559 returns a :class:`~IPython.utils.struct.Struct` with the options as keys
561 560 and the stripped argument string still as a string.
562 561
563 562 arg_str is quoted as a true sys.argv vector by using shlex.split.
564 563 This allows us to easily expand variables, glob files, quote
565 564 arguments, etc.
566 565
567 566 Parameters
568 567 ----------
569 568
570 569 arg_str : str
571 570 The arguments to parse.
572 571
573 572 opt_str : str
574 573 The options specification.
575 574
576 575 mode : str, default 'string'
577 576 If given as 'list', the argument string is returned as a list (split
578 577 on whitespace) instead of a string.
579 578
580 579 list_all : bool, default False
581 580 Put all option values in lists. Normally only options
582 581 appearing more than once are put in a list.
583 582
584 583 posix : bool, default True
585 584 Whether to split the input line in POSIX mode or not, as per the
586 585 conventions outlined in the :mod:`shlex` module from the standard
587 586 library.
588 587 """
589 588
590 589 # inject default options at the beginning of the input line
591 590 caller = sys._getframe(1).f_code.co_name
592 591 arg_str = '%s %s' % (self.options_table.get(caller,''),arg_str)
593 592
594 593 mode = kw.get('mode','string')
595 594 if mode not in ['string','list']:
596 595 raise ValueError('incorrect mode given: %s' % mode)
597 596 # Get options
598 597 list_all = kw.get('list_all',0)
599 598 posix = kw.get('posix', os.name == 'posix')
600 599 strict = kw.get('strict', True)
601 600
602 601 # Check if we have more than one argument to warrant extra processing:
603 602 odict = {} # Dictionary with options
604 603 args = arg_str.split()
605 604 if len(args) >= 1:
606 605 # If the list of inputs only has 0 or 1 thing in it, there's no
607 606 # need to look for options
608 607 argv = arg_split(arg_str, posix, strict)
609 608 # Do regular option processing
610 609 try:
611 610 opts,args = getopt(argv, opt_str, long_opts)
612 611 except GetoptError as e:
613 612 raise UsageError('%s ( allowed: "%s" %s)' % (e.msg,opt_str,
614 613 " ".join(long_opts)))
615 614 for o,a in opts:
616 615 if o.startswith('--'):
617 616 o = o[2:]
618 617 else:
619 618 o = o[1:]
620 619 try:
621 620 odict[o].append(a)
622 621 except AttributeError:
623 622 odict[o] = [odict[o],a]
624 623 except KeyError:
625 624 if list_all:
626 625 odict[o] = [a]
627 626 else:
628 627 odict[o] = a
629 628
630 629 # Prepare opts,args for return
631 630 opts = Struct(odict)
632 631 if mode == 'string':
633 632 args = ' '.join(args)
634 633
635 634 return opts,args
636 635
637 636 def default_option(self, fn, optstr):
638 637 """Make an entry in the options_table for fn, with value optstr"""
639 638
640 639 if fn not in self.lsmagic():
641 640 error("%s is not a magic function" % fn)
642 641 self.options_table[fn] = optstr
643 642
644 643
645 644 class MagicAlias(object):
646 645 """An alias to another magic function.
647 646
648 647 An alias is determined by its magic name and magic kind. Lookup
649 648 is done at call time, so if the underlying magic changes the alias
650 649 will call the new function.
651 650
652 651 Use the :meth:`MagicsManager.register_alias` method or the
653 652 `%alias_magic` magic function to create and register a new alias.
654 653 """
655 654 def __init__(self, shell, magic_name, magic_kind):
656 655 self.shell = shell
657 656 self.magic_name = magic_name
658 657 self.magic_kind = magic_kind
659 658
660 659 self.pretty_target = '%s%s' % (magic_escapes[self.magic_kind], self.magic_name)
661 660 self.__doc__ = "Alias for `%s`." % self.pretty_target
662 661
663 662 self._in_call = False
664 663
665 664 def __call__(self, *args, **kwargs):
666 665 """Call the magic alias."""
667 666 fn = self.shell.find_magic(self.magic_name, self.magic_kind)
668 667 if fn is None:
669 668 raise UsageError("Magic `%s` not found." % self.pretty_target)
670 669
671 670 # Protect against infinite recursion.
672 671 if self._in_call:
673 672 raise UsageError("Infinite recursion detected; "
674 673 "magic aliases cannot call themselves.")
675 674 self._in_call = True
676 675 try:
677 676 return fn(*args, **kwargs)
678 677 finally:
679 678 self._in_call = False
@@ -1,744 +1,743 b''
1 1 """Implementation of code management magic functions.
2 2 """
3 3 #-----------------------------------------------------------------------------
4 4 # Copyright (c) 2012 The IPython Development Team.
5 5 #
6 6 # Distributed under the terms of the Modified BSD License.
7 7 #
8 8 # The full license is in the file COPYING.txt, distributed with this software.
9 9 #-----------------------------------------------------------------------------
10 10
11 11 #-----------------------------------------------------------------------------
12 12 # Imports
13 13 #-----------------------------------------------------------------------------
14 14
15 15 # Stdlib
16 16 import inspect
17 17 import io
18 18 import os
19 19 import re
20 20 import sys
21 21 import ast
22 22 from itertools import chain
23 23
24 24 # Our own packages
25 25 from IPython.core.error import TryNext, StdinNotImplementedError, UsageError
26 26 from IPython.core.macro import Macro
27 27 from IPython.core.magic import Magics, magics_class, line_magic
28 28 from IPython.core.oinspect import find_file, find_source_lines
29 29 from IPython.testing.skipdoctest import skip_doctest
30 30 from IPython.utils import py3compat
31 from IPython.utils.py3compat import string_types
32 31 from IPython.utils.contexts import preserve_keys
33 32 from IPython.utils.path import get_py_filename
34 33 from warnings import warn
35 34 from logging import error
36 35 from IPython.utils.text import get_text_list
37 36
38 37 #-----------------------------------------------------------------------------
39 38 # Magic implementation classes
40 39 #-----------------------------------------------------------------------------
41 40
42 41 # Used for exception handling in magic_edit
43 42 class MacroToEdit(ValueError): pass
44 43
45 44 ipython_input_pat = re.compile(r"<ipython\-input\-(\d+)-[a-z\d]+>$")
46 45
47 46 # To match, e.g. 8-10 1:5 :10 3-
48 47 range_re = re.compile(r"""
49 48 (?P<start>\d+)?
50 49 ((?P<sep>[\-:])
51 50 (?P<end>\d+)?)?
52 51 $""", re.VERBOSE)
53 52
54 53
55 54 def extract_code_ranges(ranges_str):
56 55 """Turn a string of range for %%load into 2-tuples of (start, stop)
57 56 ready to use as a slice of the content splitted by lines.
58 57
59 58 Examples
60 59 --------
61 60 list(extract_input_ranges("5-10 2"))
62 61 [(4, 10), (1, 2)]
63 62 """
64 63 for range_str in ranges_str.split():
65 64 rmatch = range_re.match(range_str)
66 65 if not rmatch:
67 66 continue
68 67 sep = rmatch.group("sep")
69 68 start = rmatch.group("start")
70 69 end = rmatch.group("end")
71 70
72 71 if sep == '-':
73 72 start = int(start) - 1 if start else None
74 73 end = int(end) if end else None
75 74 elif sep == ':':
76 75 start = int(start) - 1 if start else None
77 76 end = int(end) - 1 if end else None
78 77 else:
79 78 end = int(start)
80 79 start = int(start) - 1
81 80 yield (start, end)
82 81
83 82
84 83 @skip_doctest
85 84 def extract_symbols(code, symbols):
86 85 """
87 86 Return a tuple (blocks, not_found)
88 87 where ``blocks`` is a list of code fragments
89 88 for each symbol parsed from code, and ``not_found`` are
90 89 symbols not found in the code.
91 90
92 91 For example::
93 92
94 93 >>> code = '''a = 10
95 94
96 95 def b(): return 42
97 96
98 97 class A: pass'''
99 98
100 99 >>> extract_symbols(code, 'A,b,z')
101 100 (["class A: pass", "def b(): return 42"], ['z'])
102 101 """
103 102 symbols = symbols.split(',')
104 103
105 104 # this will raise SyntaxError if code isn't valid Python
106 105 py_code = ast.parse(code)
107 106
108 107 marks = [(getattr(s, 'name', None), s.lineno) for s in py_code.body]
109 108 code = code.split('\n')
110 109
111 110 symbols_lines = {}
112 111
113 112 # we already know the start_lineno of each symbol (marks).
114 113 # To find each end_lineno, we traverse in reverse order until each
115 114 # non-blank line
116 115 end = len(code)
117 116 for name, start in reversed(marks):
118 117 while not code[end - 1].strip():
119 118 end -= 1
120 119 if name:
121 120 symbols_lines[name] = (start - 1, end)
122 121 end = start - 1
123 122
124 123 # Now symbols_lines is a map
125 124 # {'symbol_name': (start_lineno, end_lineno), ...}
126 125
127 126 # fill a list with chunks of codes for each requested symbol
128 127 blocks = []
129 128 not_found = []
130 129 for symbol in symbols:
131 130 if symbol in symbols_lines:
132 131 start, end = symbols_lines[symbol]
133 132 blocks.append('\n'.join(code[start:end]) + '\n')
134 133 else:
135 134 not_found.append(symbol)
136 135
137 136 return blocks, not_found
138 137
139 138 def strip_initial_indent(lines):
140 139 """For %load, strip indent from lines until finding an unindented line.
141 140
142 141 https://github.com/ipython/ipython/issues/9775
143 142 """
144 143 indent_re = re.compile(r'\s+')
145 144
146 145 it = iter(lines)
147 146 first_line = next(it)
148 147 indent_match = indent_re.match(first_line)
149 148
150 149 if indent_match:
151 150 # First line was indented
152 151 indent = indent_match.group()
153 152 yield first_line[len(indent):]
154 153
155 154 for line in it:
156 155 if line.startswith(indent):
157 156 yield line[len(indent):]
158 157 else:
159 158 # Less indented than the first line - stop dedenting
160 159 yield line
161 160 break
162 161 else:
163 162 yield first_line
164 163
165 164 # Pass the remaining lines through without dedenting
166 165 for line in it:
167 166 yield line
168 167
169 168
170 169 class InteractivelyDefined(Exception):
171 170 """Exception for interactively defined variable in magic_edit"""
172 171 def __init__(self, index):
173 172 self.index = index
174 173
175 174
176 175 @magics_class
177 176 class CodeMagics(Magics):
178 177 """Magics related to code management (loading, saving, editing, ...)."""
179 178
180 179 def __init__(self, *args, **kwargs):
181 180 self._knowntemps = set()
182 181 super(CodeMagics, self).__init__(*args, **kwargs)
183 182
184 183 @line_magic
185 184 def save(self, parameter_s=''):
186 185 """Save a set of lines or a macro to a given filename.
187 186
188 187 Usage:\\
189 188 %save [options] filename n1-n2 n3-n4 ... n5 .. n6 ...
190 189
191 190 Options:
192 191
193 192 -r: use 'raw' input. By default, the 'processed' history is used,
194 193 so that magics are loaded in their transformed version to valid
195 194 Python. If this option is given, the raw input as typed as the
196 195 command line is used instead.
197 196
198 197 -f: force overwrite. If file exists, %save will prompt for overwrite
199 198 unless -f is given.
200 199
201 200 -a: append to the file instead of overwriting it.
202 201
203 202 This function uses the same syntax as %history for input ranges,
204 203 then saves the lines to the filename you specify.
205 204
206 205 It adds a '.py' extension to the file if you don't do so yourself, and
207 206 it asks for confirmation before overwriting existing files.
208 207
209 208 If `-r` option is used, the default extension is `.ipy`.
210 209 """
211 210
212 211 opts,args = self.parse_options(parameter_s,'fra',mode='list')
213 212 if not args:
214 213 raise UsageError('Missing filename.')
215 214 raw = 'r' in opts
216 215 force = 'f' in opts
217 216 append = 'a' in opts
218 217 mode = 'a' if append else 'w'
219 218 ext = u'.ipy' if raw else u'.py'
220 219 fname, codefrom = args[0], " ".join(args[1:])
221 220 if not fname.endswith((u'.py',u'.ipy')):
222 221 fname += ext
223 222 file_exists = os.path.isfile(fname)
224 223 if file_exists and not force and not append:
225 224 try:
226 225 overwrite = self.shell.ask_yes_no('File `%s` exists. Overwrite (y/[N])? ' % fname, default='n')
227 226 except StdinNotImplementedError:
228 227 print("File `%s` exists. Use `%%save -f %s` to force overwrite" % (fname, parameter_s))
229 228 return
230 229 if not overwrite :
231 230 print('Operation cancelled.')
232 231 return
233 232 try:
234 233 cmds = self.shell.find_user_code(codefrom,raw)
235 234 except (TypeError, ValueError) as e:
236 235 print(e.args[0])
237 236 return
238 237 out = py3compat.cast_unicode(cmds)
239 238 with io.open(fname, mode, encoding="utf-8") as f:
240 239 if not file_exists or not append:
241 240 f.write(u"# coding: utf-8\n")
242 241 f.write(out)
243 242 # make sure we end on a newline
244 243 if not out.endswith(u'\n'):
245 244 f.write(u'\n')
246 245 print('The following commands were written to file `%s`:' % fname)
247 246 print(cmds)
248 247
249 248 @line_magic
250 249 def pastebin(self, parameter_s=''):
251 250 """Upload code to Github's Gist paste bin, returning the URL.
252 251
253 252 Usage:\\
254 253 %pastebin [-d "Custom description"] 1-7
255 254
256 255 The argument can be an input history range, a filename, or the name of a
257 256 string or macro.
258 257
259 258 Options:
260 259
261 260 -d: Pass a custom description for the gist. The default will say
262 261 "Pasted from IPython".
263 262 """
264 263 opts, args = self.parse_options(parameter_s, 'd:')
265 264
266 265 try:
267 266 code = self.shell.find_user_code(args)
268 267 except (ValueError, TypeError) as e:
269 268 print(e.args[0])
270 269 return
271 270
272 271 # Deferred import
273 272 try:
274 273 from urllib.request import urlopen # Py 3
275 274 except ImportError:
276 275 from urllib2 import urlopen
277 276 import json
278 277 post_data = json.dumps({
279 278 "description": opts.get('d', "Pasted from IPython"),
280 279 "public": True,
281 280 "files": {
282 281 "file1.py": {
283 282 "content": code
284 283 }
285 284 }
286 285 }).encode('utf-8')
287 286
288 287 response = urlopen("https://api.github.com/gists", post_data)
289 288 response_data = json.loads(response.read().decode('utf-8'))
290 289 return response_data['html_url']
291 290
292 291 @line_magic
293 292 def loadpy(self, arg_s):
294 293 """Alias of `%load`
295 294
296 295 `%loadpy` has gained some flexibility and dropped the requirement of a `.py`
297 296 extension. So it has been renamed simply into %load. You can look at
298 297 `%load`'s docstring for more info.
299 298 """
300 299 self.load(arg_s)
301 300
302 301 @line_magic
303 302 def load(self, arg_s):
304 303 """Load code into the current frontend.
305 304
306 305 Usage:\\
307 306 %load [options] source
308 307
309 308 where source can be a filename, URL, input history range, macro, or
310 309 element in the user namespace
311 310
312 311 Options:
313 312
314 313 -r <lines>: Specify lines or ranges of lines to load from the source.
315 314 Ranges could be specified as x-y (x..y) or in python-style x:y
316 315 (x..(y-1)). Both limits x and y can be left blank (meaning the
317 316 beginning and end of the file, respectively).
318 317
319 318 -s <symbols>: Specify function or classes to load from python source.
320 319
321 320 -y : Don't ask confirmation for loading source above 200 000 characters.
322 321
323 322 -n : Include the user's namespace when searching for source code.
324 323
325 324 This magic command can either take a local filename, a URL, an history
326 325 range (see %history) or a macro as argument, it will prompt for
327 326 confirmation before loading source with more than 200 000 characters, unless
328 327 -y flag is passed or if the frontend does not support raw_input::
329 328
330 329 %load myscript.py
331 330 %load 7-27
332 331 %load myMacro
333 332 %load http://www.example.com/myscript.py
334 333 %load -r 5-10 myscript.py
335 334 %load -r 10-20,30,40: foo.py
336 335 %load -s MyClass,wonder_function myscript.py
337 336 %load -n MyClass
338 337 %load -n my_module.wonder_function
339 338 """
340 339 opts,args = self.parse_options(arg_s,'yns:r:')
341 340
342 341 if not args:
343 342 raise UsageError('Missing filename, URL, input history range, '
344 343 'macro, or element in the user namespace.')
345 344
346 345 search_ns = 'n' in opts
347 346
348 347 contents = self.shell.find_user_code(args, search_ns=search_ns)
349 348
350 349 if 's' in opts:
351 350 try:
352 351 blocks, not_found = extract_symbols(contents, opts['s'])
353 352 except SyntaxError:
354 353 # non python code
355 354 error("Unable to parse the input as valid Python code")
356 355 return
357 356
358 357 if len(not_found) == 1:
359 358 warn('The symbol `%s` was not found' % not_found[0])
360 359 elif len(not_found) > 1:
361 360 warn('The symbols %s were not found' % get_text_list(not_found,
362 361 wrap_item_with='`')
363 362 )
364 363
365 364 contents = '\n'.join(blocks)
366 365
367 366 if 'r' in opts:
368 367 ranges = opts['r'].replace(',', ' ')
369 368 lines = contents.split('\n')
370 369 slices = extract_code_ranges(ranges)
371 370 contents = [lines[slice(*slc)] for slc in slices]
372 371 contents = '\n'.join(strip_initial_indent(chain.from_iterable(contents)))
373 372
374 373 l = len(contents)
375 374
376 375 # 200 000 is ~ 2500 full 80 caracter lines
377 376 # so in average, more than 5000 lines
378 377 if l > 200000 and 'y' not in opts:
379 378 try:
380 379 ans = self.shell.ask_yes_no(("The text you're trying to load seems pretty big"\
381 380 " (%d characters). Continue (y/[N]) ?" % l), default='n' )
382 381 except StdinNotImplementedError:
383 382 #asume yes if raw input not implemented
384 383 ans = True
385 384
386 385 if ans is False :
387 386 print('Operation cancelled.')
388 387 return
389 388
390 389 contents = "# %load {}\n".format(arg_s) + contents
391 390
392 391 self.shell.set_next_input(contents, replace=True)
393 392
394 393 @staticmethod
395 394 def _find_edit_target(shell, args, opts, last_call):
396 395 """Utility method used by magic_edit to find what to edit."""
397 396
398 397 def make_filename(arg):
399 398 "Make a filename from the given args"
400 399 try:
401 400 filename = get_py_filename(arg)
402 401 except IOError:
403 402 # If it ends with .py but doesn't already exist, assume we want
404 403 # a new file.
405 404 if arg.endswith('.py'):
406 405 filename = arg
407 406 else:
408 407 filename = None
409 408 return filename
410 409
411 410 # Set a few locals from the options for convenience:
412 411 opts_prev = 'p' in opts
413 412 opts_raw = 'r' in opts
414 413
415 414 # custom exceptions
416 415 class DataIsObject(Exception): pass
417 416
418 417 # Default line number value
419 418 lineno = opts.get('n',None)
420 419
421 420 if opts_prev:
422 421 args = '_%s' % last_call[0]
423 422 if args not in shell.user_ns:
424 423 args = last_call[1]
425 424
426 425 # by default this is done with temp files, except when the given
427 426 # arg is a filename
428 427 use_temp = True
429 428
430 429 data = ''
431 430
432 431 # First, see if the arguments should be a filename.
433 432 filename = make_filename(args)
434 433 if filename:
435 434 use_temp = False
436 435 elif args:
437 436 # Mode where user specifies ranges of lines, like in %macro.
438 437 data = shell.extract_input_lines(args, opts_raw)
439 438 if not data:
440 439 try:
441 440 # Load the parameter given as a variable. If not a string,
442 441 # process it as an object instead (below)
443 442
444 443 #print '*** args',args,'type',type(args) # dbg
445 444 data = eval(args, shell.user_ns)
446 if not isinstance(data, string_types):
445 if not isinstance(data, str):
447 446 raise DataIsObject
448 447
449 448 except (NameError,SyntaxError):
450 449 # given argument is not a variable, try as a filename
451 450 filename = make_filename(args)
452 451 if filename is None:
453 452 warn("Argument given (%s) can't be found as a variable "
454 453 "or as a filename." % args)
455 454 return (None, None, None)
456 455 use_temp = False
457 456
458 457 except DataIsObject:
459 458 # macros have a special edit function
460 459 if isinstance(data, Macro):
461 460 raise MacroToEdit(data)
462 461
463 462 # For objects, try to edit the file where they are defined
464 463 filename = find_file(data)
465 464 if filename:
466 465 if 'fakemodule' in filename.lower() and \
467 466 inspect.isclass(data):
468 467 # class created by %edit? Try to find source
469 468 # by looking for method definitions instead, the
470 469 # __module__ in those classes is FakeModule.
471 470 attrs = [getattr(data, aname) for aname in dir(data)]
472 471 for attr in attrs:
473 472 if not inspect.ismethod(attr):
474 473 continue
475 474 filename = find_file(attr)
476 475 if filename and \
477 476 'fakemodule' not in filename.lower():
478 477 # change the attribute to be the edit
479 478 # target instead
480 479 data = attr
481 480 break
482 481
483 482 m = ipython_input_pat.match(os.path.basename(filename))
484 483 if m:
485 484 raise InteractivelyDefined(int(m.groups()[0]))
486 485
487 486 datafile = 1
488 487 if filename is None:
489 488 filename = make_filename(args)
490 489 datafile = 1
491 490 if filename is not None:
492 491 # only warn about this if we get a real name
493 492 warn('Could not find file where `%s` is defined.\n'
494 493 'Opening a file named `%s`' % (args, filename))
495 494 # Now, make sure we can actually read the source (if it was
496 495 # in a temp file it's gone by now).
497 496 if datafile:
498 497 if lineno is None:
499 498 lineno = find_source_lines(data)
500 499 if lineno is None:
501 500 filename = make_filename(args)
502 501 if filename is None:
503 502 warn('The file where `%s` was defined '
504 503 'cannot be read or found.' % data)
505 504 return (None, None, None)
506 505 use_temp = False
507 506
508 507 if use_temp:
509 508 filename = shell.mktempfile(data)
510 509 print('IPython will make a temporary file named:',filename)
511 510
512 511 # use last_call to remember the state of the previous call, but don't
513 512 # let it be clobbered by successive '-p' calls.
514 513 try:
515 514 last_call[0] = shell.displayhook.prompt_count
516 515 if not opts_prev:
517 516 last_call[1] = args
518 517 except:
519 518 pass
520 519
521 520
522 521 return filename, lineno, use_temp
523 522
524 523 def _edit_macro(self,mname,macro):
525 524 """open an editor with the macro data in a file"""
526 525 filename = self.shell.mktempfile(macro.value)
527 526 self.shell.hooks.editor(filename)
528 527
529 528 # and make a new macro object, to replace the old one
530 529 with open(filename) as mfile:
531 530 mvalue = mfile.read()
532 531 self.shell.user_ns[mname] = Macro(mvalue)
533 532
534 533 @skip_doctest
535 534 @line_magic
536 535 def edit(self, parameter_s='',last_call=['','']):
537 536 """Bring up an editor and execute the resulting code.
538 537
539 538 Usage:
540 539 %edit [options] [args]
541 540
542 541 %edit runs IPython's editor hook. The default version of this hook is
543 542 set to call the editor specified by your $EDITOR environment variable.
544 543 If this isn't found, it will default to vi under Linux/Unix and to
545 544 notepad under Windows. See the end of this docstring for how to change
546 545 the editor hook.
547 546
548 547 You can also set the value of this editor via the
549 548 ``TerminalInteractiveShell.editor`` option in your configuration file.
550 549 This is useful if you wish to use a different editor from your typical
551 550 default with IPython (and for Windows users who typically don't set
552 551 environment variables).
553 552
554 553 This command allows you to conveniently edit multi-line code right in
555 554 your IPython session.
556 555
557 556 If called without arguments, %edit opens up an empty editor with a
558 557 temporary file and will execute the contents of this file when you
559 558 close it (don't forget to save it!).
560 559
561 560
562 561 Options:
563 562
564 563 -n <number>: open the editor at a specified line number. By default,
565 564 the IPython editor hook uses the unix syntax 'editor +N filename', but
566 565 you can configure this by providing your own modified hook if your
567 566 favorite editor supports line-number specifications with a different
568 567 syntax.
569 568
570 569 -p: this will call the editor with the same data as the previous time
571 570 it was used, regardless of how long ago (in your current session) it
572 571 was.
573 572
574 573 -r: use 'raw' input. This option only applies to input taken from the
575 574 user's history. By default, the 'processed' history is used, so that
576 575 magics are loaded in their transformed version to valid Python. If
577 576 this option is given, the raw input as typed as the command line is
578 577 used instead. When you exit the editor, it will be executed by
579 578 IPython's own processor.
580 579
581 580 -x: do not execute the edited code immediately upon exit. This is
582 581 mainly useful if you are editing programs which need to be called with
583 582 command line arguments, which you can then do using %run.
584 583
585 584
586 585 Arguments:
587 586
588 587 If arguments are given, the following possibilities exist:
589 588
590 589 - If the argument is a filename, IPython will load that into the
591 590 editor. It will execute its contents with execfile() when you exit,
592 591 loading any code in the file into your interactive namespace.
593 592
594 593 - The arguments are ranges of input history, e.g. "7 ~1/4-6".
595 594 The syntax is the same as in the %history magic.
596 595
597 596 - If the argument is a string variable, its contents are loaded
598 597 into the editor. You can thus edit any string which contains
599 598 python code (including the result of previous edits).
600 599
601 600 - If the argument is the name of an object (other than a string),
602 601 IPython will try to locate the file where it was defined and open the
603 602 editor at the point where it is defined. You can use `%edit function`
604 603 to load an editor exactly at the point where 'function' is defined,
605 604 edit it and have the file be executed automatically.
606 605
607 606 - If the object is a macro (see %macro for details), this opens up your
608 607 specified editor with a temporary file containing the macro's data.
609 608 Upon exit, the macro is reloaded with the contents of the file.
610 609
611 610 Note: opening at an exact line is only supported under Unix, and some
612 611 editors (like kedit and gedit up to Gnome 2.8) do not understand the
613 612 '+NUMBER' parameter necessary for this feature. Good editors like
614 613 (X)Emacs, vi, jed, pico and joe all do.
615 614
616 615 After executing your code, %edit will return as output the code you
617 616 typed in the editor (except when it was an existing file). This way
618 617 you can reload the code in further invocations of %edit as a variable,
619 618 via _<NUMBER> or Out[<NUMBER>], where <NUMBER> is the prompt number of
620 619 the output.
621 620
622 621 Note that %edit is also available through the alias %ed.
623 622
624 623 This is an example of creating a simple function inside the editor and
625 624 then modifying it. First, start up the editor::
626 625
627 626 In [1]: edit
628 627 Editing... done. Executing edited code...
629 628 Out[1]: 'def foo():\\n print "foo() was defined in an editing
630 629 session"\\n'
631 630
632 631 We can then call the function foo()::
633 632
634 633 In [2]: foo()
635 634 foo() was defined in an editing session
636 635
637 636 Now we edit foo. IPython automatically loads the editor with the
638 637 (temporary) file where foo() was previously defined::
639 638
640 639 In [3]: edit foo
641 640 Editing... done. Executing edited code...
642 641
643 642 And if we call foo() again we get the modified version::
644 643
645 644 In [4]: foo()
646 645 foo() has now been changed!
647 646
648 647 Here is an example of how to edit a code snippet successive
649 648 times. First we call the editor::
650 649
651 650 In [5]: edit
652 651 Editing... done. Executing edited code...
653 652 hello
654 653 Out[5]: "print 'hello'\\n"
655 654
656 655 Now we call it again with the previous output (stored in _)::
657 656
658 657 In [6]: edit _
659 658 Editing... done. Executing edited code...
660 659 hello world
661 660 Out[6]: "print 'hello world'\\n"
662 661
663 662 Now we call it with the output #8 (stored in _8, also as Out[8])::
664 663
665 664 In [7]: edit _8
666 665 Editing... done. Executing edited code...
667 666 hello again
668 667 Out[7]: "print 'hello again'\\n"
669 668
670 669
671 670 Changing the default editor hook:
672 671
673 672 If you wish to write your own editor hook, you can put it in a
674 673 configuration file which you load at startup time. The default hook
675 674 is defined in the IPython.core.hooks module, and you can use that as a
676 675 starting example for further modifications. That file also has
677 676 general instructions on how to set a new hook for use once you've
678 677 defined it."""
679 678 opts,args = self.parse_options(parameter_s,'prxn:')
680 679
681 680 try:
682 681 filename, lineno, is_temp = self._find_edit_target(self.shell,
683 682 args, opts, last_call)
684 683 except MacroToEdit as e:
685 684 self._edit_macro(args, e.args[0])
686 685 return
687 686 except InteractivelyDefined as e:
688 687 print("Editing In[%i]" % e.index)
689 688 args = str(e.index)
690 689 filename, lineno, is_temp = self._find_edit_target(self.shell,
691 690 args, opts, last_call)
692 691 if filename is None:
693 692 # nothing was found, warnings have already been issued,
694 693 # just give up.
695 694 return
696 695
697 696 if is_temp:
698 697 self._knowntemps.add(filename)
699 698 elif (filename in self._knowntemps):
700 699 is_temp = True
701 700
702 701
703 702 # do actual editing here
704 703 print('Editing...', end=' ')
705 704 sys.stdout.flush()
706 705 try:
707 706 # Quote filenames that may have spaces in them
708 707 if ' ' in filename:
709 708 filename = "'%s'" % filename
710 709 self.shell.hooks.editor(filename,lineno)
711 710 except TryNext:
712 711 warn('Could not open editor')
713 712 return
714 713
715 714 # XXX TODO: should this be generalized for all string vars?
716 715 # For now, this is special-cased to blocks created by cpaste
717 716 if args.strip() == 'pasted_block':
718 717 with open(filename, 'r') as f:
719 718 self.shell.user_ns['pasted_block'] = f.read()
720 719
721 720 if 'x' in opts: # -x prevents actual execution
722 721 print()
723 722 else:
724 723 print('done. Executing edited code...')
725 724 with preserve_keys(self.shell.user_ns, '__file__'):
726 725 if not is_temp:
727 726 self.shell.user_ns['__file__'] = filename
728 727 if 'r' in opts: # Untranslated IPython code
729 728 with open(filename, 'r') as f:
730 729 source = f.read()
731 730 self.shell.run_cell(source, store_history=False)
732 731 else:
733 732 self.shell.safe_execfile(filename, self.shell.user_ns,
734 733 self.shell.user_ns)
735 734
736 735 if is_temp:
737 736 try:
738 737 return open(filename).read()
739 738 except IOError as msg:
740 739 if msg.filename == filename:
741 740 warn('File not found. Did you forget to save?')
742 741 return
743 742 else:
744 743 self.shell.showtraceback()
@@ -1,1009 +1,1009 b''
1 1 # -*- coding: utf-8 -*-
2 2 """Tools for inspecting Python objects.
3 3
4 4 Uses syntax highlighting for presenting the various information elements.
5 5
6 6 Similar in spirit to the inspect module, but all calls take a name argument to
7 7 reference the name under which an object is being read.
8 8 """
9 9
10 10 # Copyright (c) IPython Development Team.
11 11 # Distributed under the terms of the Modified BSD License.
12 12
13 13 __all__ = ['Inspector','InspectColors']
14 14
15 15 # stdlib modules
16 16 import inspect
17 17 from inspect import signature
18 18 import linecache
19 19 import warnings
20 20 import os
21 21 from textwrap import dedent
22 22 import types
23 23 import io as stdlib_io
24 24
25 25 try:
26 26 from itertools import izip_longest
27 27 except ImportError:
28 28 from itertools import zip_longest as izip_longest
29 29
30 30 # IPython's own
31 31 from IPython.core import page
32 32 from IPython.lib.pretty import pretty
33 33 from IPython.testing.skipdoctest import skip_doctest
34 34 from IPython.utils import PyColorize
35 35 from IPython.utils import openpy
36 36 from IPython.utils import py3compat
37 37 from IPython.utils.dir2 import safe_hasattr
38 38 from IPython.utils.path import compress_user
39 39 from IPython.utils.text import indent
40 40 from IPython.utils.wildcard import list_namespace
41 41 from IPython.utils.coloransi import TermColors, ColorScheme, ColorSchemeTable
42 from IPython.utils.py3compat import cast_unicode, string_types, PY3
42 from IPython.utils.py3compat import cast_unicode, PY3
43 43 from IPython.utils.colorable import Colorable
44 44
45 45 from pygments import highlight
46 46 from pygments.lexers import PythonLexer
47 47 from pygments.formatters import HtmlFormatter
48 48
49 49 def pylight(code):
50 50 return highlight(code, PythonLexer(), HtmlFormatter(noclasses=True))
51 51
52 52 # builtin docstrings to ignore
53 53 _func_call_docstring = types.FunctionType.__call__.__doc__
54 54 _object_init_docstring = object.__init__.__doc__
55 55 _builtin_type_docstrings = {
56 56 inspect.getdoc(t) for t in (types.ModuleType, types.MethodType,
57 57 types.FunctionType, property)
58 58 }
59 59
60 60 _builtin_func_type = type(all)
61 61 _builtin_meth_type = type(str.upper) # Bound methods have the same type as builtin functions
62 62 #****************************************************************************
63 63 # Builtin color schemes
64 64
65 65 Colors = TermColors # just a shorthand
66 66
67 67 InspectColors = PyColorize.ANSICodeColors
68 68
69 69 #****************************************************************************
70 70 # Auxiliary functions and objects
71 71
72 72 # See the messaging spec for the definition of all these fields. This list
73 73 # effectively defines the order of display
74 74 info_fields = ['type_name', 'base_class', 'string_form', 'namespace',
75 75 'length', 'file', 'definition', 'docstring', 'source',
76 76 'init_definition', 'class_docstring', 'init_docstring',
77 77 'call_def', 'call_docstring',
78 78 # These won't be printed but will be used to determine how to
79 79 # format the object
80 80 'ismagic', 'isalias', 'isclass', 'argspec', 'found', 'name'
81 81 ]
82 82
83 83
84 84 def object_info(**kw):
85 85 """Make an object info dict with all fields present."""
86 86 infodict = dict(izip_longest(info_fields, [None]))
87 87 infodict.update(kw)
88 88 return infodict
89 89
90 90
91 91 def get_encoding(obj):
92 92 """Get encoding for python source file defining obj
93 93
94 94 Returns None if obj is not defined in a sourcefile.
95 95 """
96 96 ofile = find_file(obj)
97 97 # run contents of file through pager starting at line where the object
98 98 # is defined, as long as the file isn't binary and is actually on the
99 99 # filesystem.
100 100 if ofile is None:
101 101 return None
102 102 elif ofile.endswith(('.so', '.dll', '.pyd')):
103 103 return None
104 104 elif not os.path.isfile(ofile):
105 105 return None
106 106 else:
107 107 # Print only text files, not extension binaries. Note that
108 108 # getsourcelines returns lineno with 1-offset and page() uses
109 109 # 0-offset, so we must adjust.
110 110 with stdlib_io.open(ofile, 'rb') as buffer: # Tweaked to use io.open for Python 2
111 111 encoding, lines = openpy.detect_encoding(buffer.readline)
112 112 return encoding
113 113
114 114 def getdoc(obj):
115 115 """Stable wrapper around inspect.getdoc.
116 116
117 117 This can't crash because of attribute problems.
118 118
119 119 It also attempts to call a getdoc() method on the given object. This
120 120 allows objects which provide their docstrings via non-standard mechanisms
121 121 (like Pyro proxies) to still be inspected by ipython's ? system.
122 122 """
123 123 # Allow objects to offer customized documentation via a getdoc method:
124 124 try:
125 125 ds = obj.getdoc()
126 126 except Exception:
127 127 pass
128 128 else:
129 129 # if we get extra info, we add it to the normal docstring.
130 if isinstance(ds, string_types):
130 if isinstance(ds, str):
131 131 return inspect.cleandoc(ds)
132 132 try:
133 133 docstr = inspect.getdoc(obj)
134 134 encoding = get_encoding(obj)
135 135 return py3compat.cast_unicode(docstr, encoding=encoding)
136 136 except Exception:
137 137 # Harden against an inspect failure, which can occur with
138 138 # extensions modules.
139 139 raise
140 140 return None
141 141
142 142
143 143 def getsource(obj, oname=''):
144 144 """Wrapper around inspect.getsource.
145 145
146 146 This can be modified by other projects to provide customized source
147 147 extraction.
148 148
149 149 Parameters
150 150 ----------
151 151 obj : object
152 152 an object whose source code we will attempt to extract
153 153 oname : str
154 154 (optional) a name under which the object is known
155 155
156 156 Returns
157 157 -------
158 158 src : unicode or None
159 159
160 160 """
161 161
162 162 if isinstance(obj, property):
163 163 sources = []
164 164 for attrname in ['fget', 'fset', 'fdel']:
165 165 fn = getattr(obj, attrname)
166 166 if fn is not None:
167 167 encoding = get_encoding(fn)
168 168 oname_prefix = ('%s.' % oname) if oname else ''
169 169 sources.append(cast_unicode(
170 170 ''.join(('# ', oname_prefix, attrname)),
171 171 encoding=encoding))
172 172 if inspect.isfunction(fn):
173 173 sources.append(dedent(getsource(fn)))
174 174 else:
175 175 # Default str/repr only prints function name,
176 176 # pretty.pretty prints module name too.
177 177 sources.append(cast_unicode(
178 178 '%s%s = %s\n' % (
179 179 oname_prefix, attrname, pretty(fn)),
180 180 encoding=encoding))
181 181 if sources:
182 182 return '\n'.join(sources)
183 183 else:
184 184 return None
185 185
186 186 else:
187 187 # Get source for non-property objects.
188 188
189 189 obj = _get_wrapped(obj)
190 190
191 191 try:
192 192 src = inspect.getsource(obj)
193 193 except TypeError:
194 194 # The object itself provided no meaningful source, try looking for
195 195 # its class definition instead.
196 196 if hasattr(obj, '__class__'):
197 197 try:
198 198 src = inspect.getsource(obj.__class__)
199 199 except TypeError:
200 200 return None
201 201
202 202 encoding = get_encoding(obj)
203 203 return cast_unicode(src, encoding=encoding)
204 204
205 205
206 206 def is_simple_callable(obj):
207 207 """True if obj is a function ()"""
208 208 return (inspect.isfunction(obj) or inspect.ismethod(obj) or \
209 209 isinstance(obj, _builtin_func_type) or isinstance(obj, _builtin_meth_type))
210 210
211 211
212 212 def getargspec(obj):
213 213 """Wrapper around :func:`inspect.getfullargspec` on Python 3, and
214 214 :func:inspect.getargspec` on Python 2.
215 215
216 216 In addition to functions and methods, this can also handle objects with a
217 217 ``__call__`` attribute.
218 218 """
219 219 if safe_hasattr(obj, '__call__') and not is_simple_callable(obj):
220 220 obj = obj.__call__
221 221
222 222 return inspect.getfullargspec(obj) if PY3 else inspect.getargspec(obj)
223 223
224 224
225 225 def format_argspec(argspec):
226 226 """Format argspect, convenience wrapper around inspect's.
227 227
228 228 This takes a dict instead of ordered arguments and calls
229 229 inspect.format_argspec with the arguments in the necessary order.
230 230 """
231 231 return inspect.formatargspec(argspec['args'], argspec['varargs'],
232 232 argspec['varkw'], argspec['defaults'])
233 233
234 234
235 235 def call_tip(oinfo, format_call=True):
236 236 """Extract call tip data from an oinfo dict.
237 237
238 238 Parameters
239 239 ----------
240 240 oinfo : dict
241 241
242 242 format_call : bool, optional
243 243 If True, the call line is formatted and returned as a string. If not, a
244 244 tuple of (name, argspec) is returned.
245 245
246 246 Returns
247 247 -------
248 248 call_info : None, str or (str, dict) tuple.
249 249 When format_call is True, the whole call information is formattted as a
250 250 single string. Otherwise, the object's name and its argspec dict are
251 251 returned. If no call information is available, None is returned.
252 252
253 253 docstring : str or None
254 254 The most relevant docstring for calling purposes is returned, if
255 255 available. The priority is: call docstring for callable instances, then
256 256 constructor docstring for classes, then main object's docstring otherwise
257 257 (regular functions).
258 258 """
259 259 # Get call definition
260 260 argspec = oinfo.get('argspec')
261 261 if argspec is None:
262 262 call_line = None
263 263 else:
264 264 # Callable objects will have 'self' as their first argument, prune
265 265 # it out if it's there for clarity (since users do *not* pass an
266 266 # extra first argument explicitly).
267 267 try:
268 268 has_self = argspec['args'][0] == 'self'
269 269 except (KeyError, IndexError):
270 270 pass
271 271 else:
272 272 if has_self:
273 273 argspec['args'] = argspec['args'][1:]
274 274
275 275 call_line = oinfo['name']+format_argspec(argspec)
276 276
277 277 # Now get docstring.
278 278 # The priority is: call docstring, constructor docstring, main one.
279 279 doc = oinfo.get('call_docstring')
280 280 if doc is None:
281 281 doc = oinfo.get('init_docstring')
282 282 if doc is None:
283 283 doc = oinfo.get('docstring','')
284 284
285 285 return call_line, doc
286 286
287 287
288 288 def _get_wrapped(obj):
289 289 """Get the original object if wrapped in one or more @decorators
290 290
291 291 Some objects automatically construct similar objects on any unrecognised
292 292 attribute access (e.g. unittest.mock.call). To protect against infinite loops,
293 293 this will arbitrarily cut off after 100 levels of obj.__wrapped__
294 294 attribute access. --TK, Jan 2016
295 295 """
296 296 orig_obj = obj
297 297 i = 0
298 298 while safe_hasattr(obj, '__wrapped__'):
299 299 obj = obj.__wrapped__
300 300 i += 1
301 301 if i > 100:
302 302 # __wrapped__ is probably a lie, so return the thing we started with
303 303 return orig_obj
304 304 return obj
305 305
306 306 def find_file(obj):
307 307 """Find the absolute path to the file where an object was defined.
308 308
309 309 This is essentially a robust wrapper around `inspect.getabsfile`.
310 310
311 311 Returns None if no file can be found.
312 312
313 313 Parameters
314 314 ----------
315 315 obj : any Python object
316 316
317 317 Returns
318 318 -------
319 319 fname : str
320 320 The absolute path to the file where the object was defined.
321 321 """
322 322 obj = _get_wrapped(obj)
323 323
324 324 fname = None
325 325 try:
326 326 fname = inspect.getabsfile(obj)
327 327 except TypeError:
328 328 # For an instance, the file that matters is where its class was
329 329 # declared.
330 330 if hasattr(obj, '__class__'):
331 331 try:
332 332 fname = inspect.getabsfile(obj.__class__)
333 333 except TypeError:
334 334 # Can happen for builtins
335 335 pass
336 336 except:
337 337 pass
338 338 return cast_unicode(fname)
339 339
340 340
341 341 def find_source_lines(obj):
342 342 """Find the line number in a file where an object was defined.
343 343
344 344 This is essentially a robust wrapper around `inspect.getsourcelines`.
345 345
346 346 Returns None if no file can be found.
347 347
348 348 Parameters
349 349 ----------
350 350 obj : any Python object
351 351
352 352 Returns
353 353 -------
354 354 lineno : int
355 355 The line number where the object definition starts.
356 356 """
357 357 obj = _get_wrapped(obj)
358 358
359 359 try:
360 360 try:
361 361 lineno = inspect.getsourcelines(obj)[1]
362 362 except TypeError:
363 363 # For instances, try the class object like getsource() does
364 364 if hasattr(obj, '__class__'):
365 365 lineno = inspect.getsourcelines(obj.__class__)[1]
366 366 else:
367 367 lineno = None
368 368 except:
369 369 return None
370 370
371 371 return lineno
372 372
373 373 class Inspector(Colorable):
374 374
375 375 def __init__(self, color_table=InspectColors,
376 376 code_color_table=PyColorize.ANSICodeColors,
377 377 scheme='NoColor',
378 378 str_detail_level=0,
379 379 parent=None, config=None):
380 380 super(Inspector, self).__init__(parent=parent, config=config)
381 381 self.color_table = color_table
382 382 self.parser = PyColorize.Parser(out='str', parent=self, style=scheme)
383 383 self.format = self.parser.format
384 384 self.str_detail_level = str_detail_level
385 385 self.set_active_scheme(scheme)
386 386
387 387 def _getdef(self,obj,oname=''):
388 388 """Return the call signature for any callable object.
389 389
390 390 If any exception is generated, None is returned instead and the
391 391 exception is suppressed."""
392 392 try:
393 393 hdef = oname + str(signature(obj))
394 394 return cast_unicode(hdef)
395 395 except:
396 396 return None
397 397
398 398 def __head(self,h):
399 399 """Return a header string with proper colors."""
400 400 return '%s%s%s' % (self.color_table.active_colors.header,h,
401 401 self.color_table.active_colors.normal)
402 402
403 403 def set_active_scheme(self, scheme):
404 404 self.color_table.set_active_scheme(scheme)
405 405 self.parser.color_table.set_active_scheme(scheme)
406 406
407 407 def noinfo(self, msg, oname):
408 408 """Generic message when no information is found."""
409 409 print('No %s found' % msg, end=' ')
410 410 if oname:
411 411 print('for %s' % oname)
412 412 else:
413 413 print()
414 414
415 415 def pdef(self, obj, oname=''):
416 416 """Print the call signature for any callable object.
417 417
418 418 If the object is a class, print the constructor information."""
419 419
420 420 if not callable(obj):
421 421 print('Object is not callable.')
422 422 return
423 423
424 424 header = ''
425 425
426 426 if inspect.isclass(obj):
427 427 header = self.__head('Class constructor information:\n')
428 428 elif (not py3compat.PY3) and type(obj) is types.InstanceType:
429 429 obj = obj.__call__
430 430
431 431 output = self._getdef(obj,oname)
432 432 if output is None:
433 433 self.noinfo('definition header',oname)
434 434 else:
435 435 print(header,self.format(output), end=' ')
436 436
437 437 # In Python 3, all classes are new-style, so they all have __init__.
438 438 @skip_doctest
439 439 def pdoc(self, obj, oname='', formatter=None):
440 440 """Print the docstring for any object.
441 441
442 442 Optional:
443 443 -formatter: a function to run the docstring through for specially
444 444 formatted docstrings.
445 445
446 446 Examples
447 447 --------
448 448
449 449 In [1]: class NoInit:
450 450 ...: pass
451 451
452 452 In [2]: class NoDoc:
453 453 ...: def __init__(self):
454 454 ...: pass
455 455
456 456 In [3]: %pdoc NoDoc
457 457 No documentation found for NoDoc
458 458
459 459 In [4]: %pdoc NoInit
460 460 No documentation found for NoInit
461 461
462 462 In [5]: obj = NoInit()
463 463
464 464 In [6]: %pdoc obj
465 465 No documentation found for obj
466 466
467 467 In [5]: obj2 = NoDoc()
468 468
469 469 In [6]: %pdoc obj2
470 470 No documentation found for obj2
471 471 """
472 472
473 473 head = self.__head # For convenience
474 474 lines = []
475 475 ds = getdoc(obj)
476 476 if formatter:
477 477 ds = formatter(ds).get('plain/text', ds)
478 478 if ds:
479 479 lines.append(head("Class docstring:"))
480 480 lines.append(indent(ds))
481 481 if inspect.isclass(obj) and hasattr(obj, '__init__'):
482 482 init_ds = getdoc(obj.__init__)
483 483 if init_ds is not None:
484 484 lines.append(head("Init docstring:"))
485 485 lines.append(indent(init_ds))
486 486 elif hasattr(obj,'__call__'):
487 487 call_ds = getdoc(obj.__call__)
488 488 if call_ds:
489 489 lines.append(head("Call docstring:"))
490 490 lines.append(indent(call_ds))
491 491
492 492 if not lines:
493 493 self.noinfo('documentation',oname)
494 494 else:
495 495 page.page('\n'.join(lines))
496 496
497 497 def psource(self, obj, oname=''):
498 498 """Print the source code for an object."""
499 499
500 500 # Flush the source cache because inspect can return out-of-date source
501 501 linecache.checkcache()
502 502 try:
503 503 src = getsource(obj, oname=oname)
504 504 except Exception:
505 505 src = None
506 506
507 507 if src is None:
508 508 self.noinfo('source', oname)
509 509 else:
510 510 page.page(self.format(src))
511 511
512 512 def pfile(self, obj, oname=''):
513 513 """Show the whole file where an object was defined."""
514 514
515 515 lineno = find_source_lines(obj)
516 516 if lineno is None:
517 517 self.noinfo('file', oname)
518 518 return
519 519
520 520 ofile = find_file(obj)
521 521 # run contents of file through pager starting at line where the object
522 522 # is defined, as long as the file isn't binary and is actually on the
523 523 # filesystem.
524 524 if ofile.endswith(('.so', '.dll', '.pyd')):
525 525 print('File %r is binary, not printing.' % ofile)
526 526 elif not os.path.isfile(ofile):
527 527 print('File %r does not exist, not printing.' % ofile)
528 528 else:
529 529 # Print only text files, not extension binaries. Note that
530 530 # getsourcelines returns lineno with 1-offset and page() uses
531 531 # 0-offset, so we must adjust.
532 532 page.page(self.format(openpy.read_py_file(ofile, skip_encoding_cookie=False)), lineno - 1)
533 533
534 534 def _format_fields(self, fields, title_width=0):
535 535 """Formats a list of fields for display.
536 536
537 537 Parameters
538 538 ----------
539 539 fields : list
540 540 A list of 2-tuples: (field_title, field_content)
541 541 title_width : int
542 542 How many characters to pad titles to. Default to longest title.
543 543 """
544 544 out = []
545 545 header = self.__head
546 546 if title_width == 0:
547 547 title_width = max(len(title) + 2 for title, _ in fields)
548 548 for title, content in fields:
549 549 if len(content.splitlines()) > 1:
550 550 title = header(title + ':') + '\n'
551 551 else:
552 552 title = header((title + ':').ljust(title_width))
553 553 out.append(cast_unicode(title) + cast_unicode(content))
554 554 return "\n".join(out)
555 555
556 556 def _mime_format(self, text, formatter=None):
557 557 """Return a mime bundle representation of the input text.
558 558
559 559 - if `formatter` is None, the returned mime bundle has
560 560 a `text/plain` field, with the input text.
561 561 a `text/html` field with a `<pre>` tag containing the input text.
562 562
563 563 - if `formatter` is not None, it must be a callable transforming the
564 564 input text into a mime bundle. Default values for `text/plain` and
565 565 `text/html` representations are the ones described above.
566 566
567 567 Note:
568 568
569 569 Formatters returning strings are supported but this behavior is deprecated.
570 570
571 571 """
572 572 text = cast_unicode(text)
573 573 defaults = {
574 574 'text/plain': text,
575 575 'text/html': '<pre>' + text + '</pre>'
576 576 }
577 577
578 578 if formatter is None:
579 579 return defaults
580 580 else:
581 581 formatted = formatter(text)
582 582
583 583 if not isinstance(formatted, dict):
584 584 # Handle the deprecated behavior of a formatter returning
585 585 # a string instead of a mime bundle.
586 586 return {
587 587 'text/plain': formatted,
588 588 'text/html': '<pre>' + formatted + '</pre>'
589 589 }
590 590
591 591 else:
592 592 return dict(defaults, **formatted)
593 593
594 594
595 595 def format_mime(self, bundle):
596 596
597 597 text_plain = bundle['text/plain']
598 598
599 599 text = ''
600 600 heads, bodies = list(zip(*text_plain))
601 601 _len = max(len(h) for h in heads)
602 602
603 603 for head, body in zip(heads, bodies):
604 604 body = body.strip('\n')
605 605 delim = '\n' if '\n' in body else ' '
606 606 text += self.__head(head+':') + (_len - len(head))*' ' +delim + body +'\n'
607 607
608 608 bundle['text/plain'] = text
609 609 return bundle
610 610
611 611 def _get_info(self, obj, oname='', formatter=None, info=None, detail_level=0):
612 612 """Retrieve an info dict and format it."""
613 613
614 614 info = self._info(obj, oname=oname, info=info, detail_level=detail_level)
615 615
616 616 _mime = {
617 617 'text/plain': [],
618 618 'text/html': '',
619 619 }
620 620
621 621 def append_field(bundle, title, key, formatter=None):
622 622 field = info[key]
623 623 if field is not None:
624 624 formatted_field = self._mime_format(field, formatter)
625 625 bundle['text/plain'].append((title, formatted_field['text/plain']))
626 626 bundle['text/html'] += '<h1>' + title + '</h1>\n' + formatted_field['text/html'] + '\n'
627 627
628 628 def code_formatter(text):
629 629 return {
630 630 'text/plain': self.format(text),
631 631 'text/html': pylight(text)
632 632 }
633 633
634 634 if info['isalias']:
635 635 append_field(_mime, 'Repr', 'string_form')
636 636
637 637 elif info['ismagic']:
638 638 if detail_level > 0:
639 639 append_field(_mime, 'Source', 'source', code_formatter)
640 640 else:
641 641 append_field(_mime, 'Docstring', 'docstring', formatter)
642 642 append_field(_mime, 'File', 'file')
643 643
644 644 elif info['isclass'] or is_simple_callable(obj):
645 645 # Functions, methods, classes
646 646 append_field(_mime, 'Signature', 'definition', code_formatter)
647 647 append_field(_mime, 'Init signature', 'init_definition', code_formatter)
648 648 if detail_level > 0 and info['source']:
649 649 append_field(_mime, 'Source', 'source', code_formatter)
650 650 else:
651 651 append_field(_mime, 'Docstring', 'docstring', formatter)
652 652 append_field(_mime, 'Init docstring', 'init_docstring', formatter)
653 653
654 654 append_field(_mime, 'File', 'file')
655 655 append_field(_mime, 'Type', 'type_name')
656 656
657 657 else:
658 658 # General Python objects
659 659 append_field(_mime, 'Signature', 'definition', code_formatter)
660 660 append_field(_mime, 'Call signature', 'call_def', code_formatter)
661 661
662 662 append_field(_mime, 'Type', 'type_name')
663 663
664 664 # Base class for old-style instances
665 665 if (not py3compat.PY3) and isinstance(obj, types.InstanceType) and info['base_class']:
666 666 append_field(_mime, 'Base Class', 'base_class')
667 667
668 668 append_field(_mime, 'String form', 'string_form')
669 669
670 670 # Namespace
671 671 if info['namespace'] != 'Interactive':
672 672 append_field(_mime, 'Namespace', 'namespace')
673 673
674 674 append_field(_mime, 'Length', 'length')
675 675 append_field(_mime, 'File', 'file')
676 676
677 677 # Source or docstring, depending on detail level and whether
678 678 # source found.
679 679 if detail_level > 0:
680 680 append_field(_mime, 'Source', 'source', code_formatter)
681 681 else:
682 682 append_field(_mime, 'Docstring', 'docstring', formatter)
683 683
684 684 append_field(_mime, 'Class docstring', 'class_docstring', formatter)
685 685 append_field(_mime, 'Init docstring', 'init_docstring', formatter)
686 686 append_field(_mime, 'Call docstring', 'call_docstring', formatter)
687 687
688 688
689 689 return self.format_mime(_mime)
690 690
691 691 def pinfo(self, obj, oname='', formatter=None, info=None, detail_level=0, enable_html_pager=True):
692 692 """Show detailed information about an object.
693 693
694 694 Optional arguments:
695 695
696 696 - oname: name of the variable pointing to the object.
697 697
698 698 - formatter: callable (optional)
699 699 A special formatter for docstrings.
700 700
701 701 The formatter is a callable that takes a string as an input
702 702 and returns either a formatted string or a mime type bundle
703 703 in the form of a dictionnary.
704 704
705 705 Although the support of custom formatter returning a string
706 706 instead of a mime type bundle is deprecated.
707 707
708 708 - info: a structure with some information fields which may have been
709 709 precomputed already.
710 710
711 711 - detail_level: if set to 1, more information is given.
712 712 """
713 713 info = self._get_info(obj, oname, formatter, info, detail_level)
714 714 if not enable_html_pager:
715 715 del info['text/html']
716 716 page.page(info)
717 717
718 718 def info(self, obj, oname='', formatter=None, info=None, detail_level=0):
719 719 """DEPRECATED. Compute a dict with detailed information about an object.
720 720 """
721 721 if formatter is not None:
722 722 warnings.warn('The `formatter` keyword argument to `Inspector.info`'
723 723 'is deprecated as of IPython 5.0 and will have no effects.',
724 724 DeprecationWarning, stacklevel=2)
725 725 return self._info(obj, oname=oname, info=info, detail_level=detail_level)
726 726
727 727 def _info(self, obj, oname='', info=None, detail_level=0):
728 728 """Compute a dict with detailed information about an object.
729 729
730 730 Optional arguments:
731 731
732 732 - oname: name of the variable pointing to the object.
733 733
734 734 - info: a structure with some information fields which may have been
735 735 precomputed already.
736 736
737 737 - detail_level: if set to 1, more information is given.
738 738 """
739 739
740 740 obj_type = type(obj)
741 741
742 742 if info is None:
743 743 ismagic = 0
744 744 isalias = 0
745 745 ospace = ''
746 746 else:
747 747 ismagic = info.ismagic
748 748 isalias = info.isalias
749 749 ospace = info.namespace
750 750
751 751 # Get docstring, special-casing aliases:
752 752 if isalias:
753 753 if not callable(obj):
754 754 try:
755 755 ds = "Alias to the system command:\n %s" % obj[1]
756 756 except:
757 757 ds = "Alias: " + str(obj)
758 758 else:
759 759 ds = "Alias to " + str(obj)
760 760 if obj.__doc__:
761 761 ds += "\nDocstring:\n" + obj.__doc__
762 762 else:
763 763 ds = getdoc(obj)
764 764 if ds is None:
765 765 ds = '<no docstring>'
766 766
767 767 # store output in a dict, we initialize it here and fill it as we go
768 768 out = dict(name=oname, found=True, isalias=isalias, ismagic=ismagic)
769 769
770 770 string_max = 200 # max size of strings to show (snipped if longer)
771 771 shalf = int((string_max - 5) / 2)
772 772
773 773 if ismagic:
774 774 obj_type_name = 'Magic function'
775 775 elif isalias:
776 776 obj_type_name = 'System alias'
777 777 else:
778 778 obj_type_name = obj_type.__name__
779 779 out['type_name'] = obj_type_name
780 780
781 781 try:
782 782 bclass = obj.__class__
783 783 out['base_class'] = str(bclass)
784 784 except: pass
785 785
786 786 # String form, but snip if too long in ? form (full in ??)
787 787 if detail_level >= self.str_detail_level:
788 788 try:
789 789 ostr = str(obj)
790 790 str_head = 'string_form'
791 791 if not detail_level and len(ostr)>string_max:
792 792 ostr = ostr[:shalf] + ' <...> ' + ostr[-shalf:]
793 793 ostr = ("\n" + " " * len(str_head.expandtabs())).\
794 794 join(q.strip() for q in ostr.split("\n"))
795 795 out[str_head] = ostr
796 796 except:
797 797 pass
798 798
799 799 if ospace:
800 800 out['namespace'] = ospace
801 801
802 802 # Length (for strings and lists)
803 803 try:
804 804 out['length'] = str(len(obj))
805 805 except: pass
806 806
807 807 # Filename where object was defined
808 808 binary_file = False
809 809 fname = find_file(obj)
810 810 if fname is None:
811 811 # if anything goes wrong, we don't want to show source, so it's as
812 812 # if the file was binary
813 813 binary_file = True
814 814 else:
815 815 if fname.endswith(('.so', '.dll', '.pyd')):
816 816 binary_file = True
817 817 elif fname.endswith('<string>'):
818 818 fname = 'Dynamically generated function. No source code available.'
819 819 out['file'] = compress_user(fname)
820 820
821 821 # Original source code for a callable, class or property.
822 822 if detail_level:
823 823 # Flush the source cache because inspect can return out-of-date
824 824 # source
825 825 linecache.checkcache()
826 826 try:
827 827 if isinstance(obj, property) or not binary_file:
828 828 src = getsource(obj, oname)
829 829 if src is not None:
830 830 src = src.rstrip()
831 831 out['source'] = src
832 832
833 833 except Exception:
834 834 pass
835 835
836 836 # Add docstring only if no source is to be shown (avoid repetitions).
837 837 if ds and out.get('source', None) is None:
838 838 out['docstring'] = ds
839 839
840 840 # Constructor docstring for classes
841 841 if inspect.isclass(obj):
842 842 out['isclass'] = True
843 843
844 844 # get the init signature:
845 845 try:
846 846 init_def = self._getdef(obj, oname)
847 847 except AttributeError:
848 848 init_def = None
849 849
850 850 # get the __init__ docstring
851 851 try:
852 852 obj_init = obj.__init__
853 853 except AttributeError:
854 854 init_ds = None
855 855 else:
856 856 if init_def is None:
857 857 # Get signature from init if top-level sig failed.
858 858 # Can happen for built-in types (list, etc.).
859 859 try:
860 860 init_def = self._getdef(obj_init, oname)
861 861 except AttributeError:
862 862 pass
863 863 init_ds = getdoc(obj_init)
864 864 # Skip Python's auto-generated docstrings
865 865 if init_ds == _object_init_docstring:
866 866 init_ds = None
867 867
868 868 if init_def:
869 869 out['init_definition'] = init_def
870 870
871 871 if init_ds:
872 872 out['init_docstring'] = init_ds
873 873
874 874 # and class docstring for instances:
875 875 else:
876 876 # reconstruct the function definition and print it:
877 877 defln = self._getdef(obj, oname)
878 878 if defln:
879 879 out['definition'] = defln
880 880
881 881 # First, check whether the instance docstring is identical to the
882 882 # class one, and print it separately if they don't coincide. In
883 883 # most cases they will, but it's nice to print all the info for
884 884 # objects which use instance-customized docstrings.
885 885 if ds:
886 886 try:
887 887 cls = getattr(obj,'__class__')
888 888 except:
889 889 class_ds = None
890 890 else:
891 891 class_ds = getdoc(cls)
892 892 # Skip Python's auto-generated docstrings
893 893 if class_ds in _builtin_type_docstrings:
894 894 class_ds = None
895 895 if class_ds and ds != class_ds:
896 896 out['class_docstring'] = class_ds
897 897
898 898 # Next, try to show constructor docstrings
899 899 try:
900 900 init_ds = getdoc(obj.__init__)
901 901 # Skip Python's auto-generated docstrings
902 902 if init_ds == _object_init_docstring:
903 903 init_ds = None
904 904 except AttributeError:
905 905 init_ds = None
906 906 if init_ds:
907 907 out['init_docstring'] = init_ds
908 908
909 909 # Call form docstring for callable instances
910 910 if safe_hasattr(obj, '__call__') and not is_simple_callable(obj):
911 911 call_def = self._getdef(obj.__call__, oname)
912 912 if call_def and (call_def != out.get('definition')):
913 913 # it may never be the case that call def and definition differ,
914 914 # but don't include the same signature twice
915 915 out['call_def'] = call_def
916 916 call_ds = getdoc(obj.__call__)
917 917 # Skip Python's auto-generated docstrings
918 918 if call_ds == _func_call_docstring:
919 919 call_ds = None
920 920 if call_ds:
921 921 out['call_docstring'] = call_ds
922 922
923 923 # Compute the object's argspec as a callable. The key is to decide
924 924 # whether to pull it from the object itself, from its __init__ or
925 925 # from its __call__ method.
926 926
927 927 if inspect.isclass(obj):
928 928 # Old-style classes need not have an __init__
929 929 callable_obj = getattr(obj, "__init__", None)
930 930 elif callable(obj):
931 931 callable_obj = obj
932 932 else:
933 933 callable_obj = None
934 934
935 935 if callable_obj is not None:
936 936 try:
937 937 argspec = getargspec(callable_obj)
938 938 except (TypeError, AttributeError):
939 939 # For extensions/builtins we can't retrieve the argspec
940 940 pass
941 941 else:
942 942 # named tuples' _asdict() method returns an OrderedDict, but we
943 943 # we want a normal
944 944 out['argspec'] = argspec_dict = dict(argspec._asdict())
945 945 # We called this varkw before argspec became a named tuple.
946 946 # With getfullargspec it's also called varkw.
947 947 if 'varkw' not in argspec_dict:
948 948 argspec_dict['varkw'] = argspec_dict.pop('keywords')
949 949
950 950 return object_info(**out)
951 951
952 952 def psearch(self,pattern,ns_table,ns_search=[],
953 953 ignore_case=False,show_all=False):
954 954 """Search namespaces with wildcards for objects.
955 955
956 956 Arguments:
957 957
958 958 - pattern: string containing shell-like wildcards to use in namespace
959 959 searches and optionally a type specification to narrow the search to
960 960 objects of that type.
961 961
962 962 - ns_table: dict of name->namespaces for search.
963 963
964 964 Optional arguments:
965 965
966 966 - ns_search: list of namespace names to include in search.
967 967
968 968 - ignore_case(False): make the search case-insensitive.
969 969
970 970 - show_all(False): show all names, including those starting with
971 971 underscores.
972 972 """
973 973 #print 'ps pattern:<%r>' % pattern # dbg
974 974
975 975 # defaults
976 976 type_pattern = 'all'
977 977 filter = ''
978 978
979 979 cmds = pattern.split()
980 980 len_cmds = len(cmds)
981 981 if len_cmds == 1:
982 982 # Only filter pattern given
983 983 filter = cmds[0]
984 984 elif len_cmds == 2:
985 985 # Both filter and type specified
986 986 filter,type_pattern = cmds
987 987 else:
988 988 raise ValueError('invalid argument string for psearch: <%s>' %
989 989 pattern)
990 990
991 991 # filter search namespaces
992 992 for name in ns_search:
993 993 if name not in ns_table:
994 994 raise ValueError('invalid namespace <%s>. Valid names: %s' %
995 995 (name,ns_table.keys()))
996 996
997 997 #print 'type_pattern:',type_pattern # dbg
998 998 search_result, namespaces_seen = set(), set()
999 999 for ns_name in ns_search:
1000 1000 ns = ns_table[ns_name]
1001 1001 # Normally, locals and globals are the same, so we just check one.
1002 1002 if id(ns) in namespaces_seen:
1003 1003 continue
1004 1004 namespaces_seen.add(id(ns))
1005 1005 tmp_res = list_namespace(ns, type_pattern, filter,
1006 1006 ignore_case=ignore_case, show_all=show_all)
1007 1007 search_result.update(tmp_res)
1008 1008
1009 1009 page.page('\n'.join(sorted(search_result)))
@@ -1,410 +1,410 b''
1 1 # -*- coding: utf-8 -*-
2 2 """Pylab (matplotlib) support utilities."""
3 3
4 4 # Copyright (c) IPython Development Team.
5 5 # Distributed under the terms of the Modified BSD License.
6 6
7 7 from io import BytesIO
8 8
9 9 from IPython.core.display import _pngxy
10 10 from IPython.utils.decorators import flag_calls
11 11 from IPython.utils import py3compat
12 12
13 13 # If user specifies a GUI, that dictates the backend, otherwise we read the
14 14 # user's mpl default from the mpl rc structure
15 15 backends = {'tk': 'TkAgg',
16 16 'gtk': 'GTKAgg',
17 17 'gtk3': 'GTK3Agg',
18 18 'wx': 'WXAgg',
19 19 'qt': 'Qt4Agg', # qt3 not supported
20 20 'qt4': 'Qt4Agg',
21 21 'qt5': 'Qt5Agg',
22 22 'osx': 'MacOSX',
23 23 'nbagg': 'nbAgg',
24 24 'notebook': 'nbAgg',
25 25 'agg': 'agg',
26 26 'inline': 'module://ipykernel.pylab.backend_inline',
27 27 'ipympl': 'module://ipympl.backend_nbagg',
28 28 }
29 29
30 30 # We also need a reverse backends2guis mapping that will properly choose which
31 31 # GUI support to activate based on the desired matplotlib backend. For the
32 32 # most part it's just a reverse of the above dict, but we also need to add a
33 33 # few others that map to the same GUI manually:
34 34 backend2gui = dict(zip(backends.values(), backends.keys()))
35 35 # Our tests expect backend2gui to just return 'qt'
36 36 backend2gui['Qt4Agg'] = 'qt'
37 37 # In the reverse mapping, there are a few extra valid matplotlib backends that
38 38 # map to the same GUI support
39 39 backend2gui['GTK'] = backend2gui['GTKCairo'] = 'gtk'
40 40 backend2gui['GTK3Cairo'] = 'gtk3'
41 41 backend2gui['WX'] = 'wx'
42 42 backend2gui['CocoaAgg'] = 'osx'
43 43 # And some backends that don't need GUI integration
44 44 del backend2gui['nbAgg']
45 45 del backend2gui['agg']
46 46 del backend2gui['module://ipykernel.pylab.backend_inline']
47 47
48 48 #-----------------------------------------------------------------------------
49 49 # Matplotlib utilities
50 50 #-----------------------------------------------------------------------------
51 51
52 52
53 53 def getfigs(*fig_nums):
54 54 """Get a list of matplotlib figures by figure numbers.
55 55
56 56 If no arguments are given, all available figures are returned. If the
57 57 argument list contains references to invalid figures, a warning is printed
58 58 but the function continues pasting further figures.
59 59
60 60 Parameters
61 61 ----------
62 62 figs : tuple
63 63 A tuple of ints giving the figure numbers of the figures to return.
64 64 """
65 65 from matplotlib._pylab_helpers import Gcf
66 66 if not fig_nums:
67 67 fig_managers = Gcf.get_all_fig_managers()
68 68 return [fm.canvas.figure for fm in fig_managers]
69 69 else:
70 70 figs = []
71 71 for num in fig_nums:
72 72 f = Gcf.figs.get(num)
73 73 if f is None:
74 74 print('Warning: figure %s not available.' % num)
75 75 else:
76 76 figs.append(f.canvas.figure)
77 77 return figs
78 78
79 79
80 80 def figsize(sizex, sizey):
81 81 """Set the default figure size to be [sizex, sizey].
82 82
83 83 This is just an easy to remember, convenience wrapper that sets::
84 84
85 85 matplotlib.rcParams['figure.figsize'] = [sizex, sizey]
86 86 """
87 87 import matplotlib
88 88 matplotlib.rcParams['figure.figsize'] = [sizex, sizey]
89 89
90 90
91 91 def print_figure(fig, fmt='png', bbox_inches='tight', **kwargs):
92 92 """Print a figure to an image, and return the resulting file data
93 93
94 94 Returned data will be bytes unless ``fmt='svg'``,
95 95 in which case it will be unicode.
96 96
97 97 Any keyword args are passed to fig.canvas.print_figure,
98 98 such as ``quality`` or ``bbox_inches``.
99 99 """
100 100 from matplotlib import rcParams
101 101 # When there's an empty figure, we shouldn't return anything, otherwise we
102 102 # get big blank areas in the qt console.
103 103 if not fig.axes and not fig.lines:
104 104 return
105 105
106 106 dpi = fig.dpi
107 107 if fmt == 'retina':
108 108 dpi = dpi * 2
109 109 fmt = 'png'
110 110
111 111 # build keyword args
112 112 kw = dict(
113 113 format=fmt,
114 114 facecolor=fig.get_facecolor(),
115 115 edgecolor=fig.get_edgecolor(),
116 116 dpi=dpi,
117 117 bbox_inches=bbox_inches,
118 118 )
119 119 # **kwargs get higher priority
120 120 kw.update(kwargs)
121 121
122 122 bytes_io = BytesIO()
123 123 fig.canvas.print_figure(bytes_io, **kw)
124 124 data = bytes_io.getvalue()
125 125 if fmt == 'svg':
126 126 data = data.decode('utf-8')
127 127 return data
128 128
129 129 def retina_figure(fig, **kwargs):
130 130 """format a figure as a pixel-doubled (retina) PNG"""
131 131 pngdata = print_figure(fig, fmt='retina', **kwargs)
132 132 # Make sure that retina_figure acts just like print_figure and returns
133 133 # None when the figure is empty.
134 134 if pngdata is None:
135 135 return
136 136 w, h = _pngxy(pngdata)
137 137 metadata = dict(width=w//2, height=h//2)
138 138 return pngdata, metadata
139 139
140 140 # We need a little factory function here to create the closure where
141 141 # safe_execfile can live.
142 142 def mpl_runner(safe_execfile):
143 143 """Factory to return a matplotlib-enabled runner for %run.
144 144
145 145 Parameters
146 146 ----------
147 147 safe_execfile : function
148 148 This must be a function with the same interface as the
149 149 :meth:`safe_execfile` method of IPython.
150 150
151 151 Returns
152 152 -------
153 153 A function suitable for use as the ``runner`` argument of the %run magic
154 154 function.
155 155 """
156 156
157 157 def mpl_execfile(fname,*where,**kw):
158 158 """matplotlib-aware wrapper around safe_execfile.
159 159
160 160 Its interface is identical to that of the :func:`execfile` builtin.
161 161
162 162 This is ultimately a call to execfile(), but wrapped in safeties to
163 163 properly handle interactive rendering."""
164 164
165 165 import matplotlib
166 166 import matplotlib.pyplot as plt
167 167
168 168 #print '*** Matplotlib runner ***' # dbg
169 169 # turn off rendering until end of script
170 170 is_interactive = matplotlib.rcParams['interactive']
171 171 matplotlib.interactive(False)
172 172 safe_execfile(fname,*where,**kw)
173 173 matplotlib.interactive(is_interactive)
174 174 # make rendering call now, if the user tried to do it
175 175 if plt.draw_if_interactive.called:
176 176 plt.draw()
177 177 plt.draw_if_interactive.called = False
178 178
179 179 # re-draw everything that is stale
180 180 try:
181 181 da = plt.draw_all
182 182 except AttributeError:
183 183 pass
184 184 else:
185 185 da()
186 186
187 187 return mpl_execfile
188 188
189 189
190 190 def _reshow_nbagg_figure(fig):
191 191 """reshow an nbagg figure"""
192 192 try:
193 193 reshow = fig.canvas.manager.reshow
194 194 except AttributeError:
195 195 raise NotImplementedError()
196 196 else:
197 197 reshow()
198 198
199 199
200 200 def select_figure_formats(shell, formats, **kwargs):
201 201 """Select figure formats for the inline backend.
202 202
203 203 Parameters
204 204 ==========
205 205 shell : InteractiveShell
206 206 The main IPython instance.
207 207 formats : str or set
208 208 One or a set of figure formats to enable: 'png', 'retina', 'jpeg', 'svg', 'pdf'.
209 209 **kwargs : any
210 210 Extra keyword arguments to be passed to fig.canvas.print_figure.
211 211 """
212 212 import matplotlib
213 213 from matplotlib.figure import Figure
214 214
215 215 svg_formatter = shell.display_formatter.formatters['image/svg+xml']
216 216 png_formatter = shell.display_formatter.formatters['image/png']
217 217 jpg_formatter = shell.display_formatter.formatters['image/jpeg']
218 218 pdf_formatter = shell.display_formatter.formatters['application/pdf']
219 219
220 if isinstance(formats, py3compat.string_types):
220 if isinstance(formats, str):
221 221 formats = {formats}
222 222 # cast in case of list / tuple
223 223 formats = set(formats)
224 224
225 225 [ f.pop(Figure, None) for f in shell.display_formatter.formatters.values() ]
226 226 mplbackend = matplotlib.get_backend().lower()
227 227 if mplbackend == 'nbagg' or mplbackend == 'module://ipympl.backend_nbagg':
228 228 formatter = shell.display_formatter.ipython_display_formatter
229 229 formatter.for_type(Figure, _reshow_nbagg_figure)
230 230
231 231 supported = {'png', 'png2x', 'retina', 'jpg', 'jpeg', 'svg', 'pdf'}
232 232 bad = formats.difference(supported)
233 233 if bad:
234 234 bs = "%s" % ','.join([repr(f) for f in bad])
235 235 gs = "%s" % ','.join([repr(f) for f in supported])
236 236 raise ValueError("supported formats are: %s not %s" % (gs, bs))
237 237
238 238 if 'png' in formats:
239 239 png_formatter.for_type(Figure, lambda fig: print_figure(fig, 'png', **kwargs))
240 240 if 'retina' in formats or 'png2x' in formats:
241 241 png_formatter.for_type(Figure, lambda fig: retina_figure(fig, **kwargs))
242 242 if 'jpg' in formats or 'jpeg' in formats:
243 243 jpg_formatter.for_type(Figure, lambda fig: print_figure(fig, 'jpg', **kwargs))
244 244 if 'svg' in formats:
245 245 svg_formatter.for_type(Figure, lambda fig: print_figure(fig, 'svg', **kwargs))
246 246 if 'pdf' in formats:
247 247 pdf_formatter.for_type(Figure, lambda fig: print_figure(fig, 'pdf', **kwargs))
248 248
249 249 #-----------------------------------------------------------------------------
250 250 # Code for initializing matplotlib and importing pylab
251 251 #-----------------------------------------------------------------------------
252 252
253 253
254 254 def find_gui_and_backend(gui=None, gui_select=None):
255 255 """Given a gui string return the gui and mpl backend.
256 256
257 257 Parameters
258 258 ----------
259 259 gui : str
260 260 Can be one of ('tk','gtk','wx','qt','qt4','inline','agg').
261 261 gui_select : str
262 262 Can be one of ('tk','gtk','wx','qt','qt4','inline').
263 263 This is any gui already selected by the shell.
264 264
265 265 Returns
266 266 -------
267 267 A tuple of (gui, backend) where backend is one of ('TkAgg','GTKAgg',
268 268 'WXAgg','Qt4Agg','module://ipykernel.pylab.backend_inline','agg').
269 269 """
270 270
271 271 import matplotlib
272 272
273 273 if gui and gui != 'auto':
274 274 # select backend based on requested gui
275 275 backend = backends[gui]
276 276 if gui == 'agg':
277 277 gui = None
278 278 else:
279 279 # We need to read the backend from the original data structure, *not*
280 280 # from mpl.rcParams, since a prior invocation of %matplotlib may have
281 281 # overwritten that.
282 282 # WARNING: this assumes matplotlib 1.1 or newer!!
283 283 backend = matplotlib.rcParamsOrig['backend']
284 284 # In this case, we need to find what the appropriate gui selection call
285 285 # should be for IPython, so we can activate inputhook accordingly
286 286 gui = backend2gui.get(backend, None)
287 287
288 288 # If we have already had a gui active, we need it and inline are the
289 289 # ones allowed.
290 290 if gui_select and gui != gui_select:
291 291 gui = gui_select
292 292 backend = backends[gui]
293 293
294 294 return gui, backend
295 295
296 296
297 297 def activate_matplotlib(backend):
298 298 """Activate the given backend and set interactive to True."""
299 299
300 300 import matplotlib
301 301 matplotlib.interactive(True)
302 302
303 303 # Matplotlib had a bug where even switch_backend could not force
304 304 # the rcParam to update. This needs to be set *before* the module
305 305 # magic of switch_backend().
306 306 matplotlib.rcParams['backend'] = backend
307 307
308 308 import matplotlib.pyplot
309 309 matplotlib.pyplot.switch_backend(backend)
310 310
311 311 # This must be imported last in the matplotlib series, after
312 312 # backend/interactivity choices have been made
313 313 import matplotlib.pyplot as plt
314 314
315 315 plt.show._needmain = False
316 316 # We need to detect at runtime whether show() is called by the user.
317 317 # For this, we wrap it into a decorator which adds a 'called' flag.
318 318 plt.draw_if_interactive = flag_calls(plt.draw_if_interactive)
319 319
320 320
321 321 def import_pylab(user_ns, import_all=True):
322 322 """Populate the namespace with pylab-related values.
323 323
324 324 Imports matplotlib, pylab, numpy, and everything from pylab and numpy.
325 325
326 326 Also imports a few names from IPython (figsize, display, getfigs)
327 327
328 328 """
329 329
330 330 # Import numpy as np/pyplot as plt are conventions we're trying to
331 331 # somewhat standardize on. Making them available to users by default
332 332 # will greatly help this.
333 333 s = ("import numpy\n"
334 334 "import matplotlib\n"
335 335 "from matplotlib import pylab, mlab, pyplot\n"
336 336 "np = numpy\n"
337 337 "plt = pyplot\n"
338 338 )
339 339 exec(s, user_ns)
340 340
341 341 if import_all:
342 342 s = ("from matplotlib.pylab import *\n"
343 343 "from numpy import *\n")
344 344 exec(s, user_ns)
345 345
346 346 # IPython symbols to add
347 347 user_ns['figsize'] = figsize
348 348 from IPython.core.display import display
349 349 # Add display and getfigs to the user's namespace
350 350 user_ns['display'] = display
351 351 user_ns['getfigs'] = getfigs
352 352
353 353
354 354 def configure_inline_support(shell, backend):
355 355 """Configure an IPython shell object for matplotlib use.
356 356
357 357 Parameters
358 358 ----------
359 359 shell : InteractiveShell instance
360 360
361 361 backend : matplotlib backend
362 362 """
363 363 # If using our svg payload backend, register the post-execution
364 364 # function that will pick up the results for display. This can only be
365 365 # done with access to the real shell object.
366 366
367 367 # Note: if we can't load the inline backend, then there's no point
368 368 # continuing (such as in terminal-only shells in environments without
369 369 # zeromq available).
370 370 try:
371 371 from ipykernel.pylab.backend_inline import InlineBackend
372 372 except ImportError:
373 373 return
374 374 import matplotlib
375 375
376 376 cfg = InlineBackend.instance(parent=shell)
377 377 cfg.shell = shell
378 378 if cfg not in shell.configurables:
379 379 shell.configurables.append(cfg)
380 380
381 381 if backend == backends['inline']:
382 382 from ipykernel.pylab.backend_inline import flush_figures
383 383 shell.events.register('post_execute', flush_figures)
384 384
385 385 # Save rcParams that will be overwrittern
386 386 shell._saved_rcParams = dict()
387 387 for k in cfg.rc:
388 388 shell._saved_rcParams[k] = matplotlib.rcParams[k]
389 389 # load inline_rc
390 390 matplotlib.rcParams.update(cfg.rc)
391 391 new_backend_name = "inline"
392 392 else:
393 393 from ipykernel.pylab.backend_inline import flush_figures
394 394 try:
395 395 shell.events.unregister('post_execute', flush_figures)
396 396 except ValueError:
397 397 pass
398 398 if hasattr(shell, '_saved_rcParams'):
399 399 matplotlib.rcParams.update(shell._saved_rcParams)
400 400 del shell._saved_rcParams
401 401 new_backend_name = "other"
402 402
403 403 # only enable the formats once -> don't change the enabled formats (which the user may
404 404 # has changed) when getting another "%matplotlib inline" call.
405 405 # See https://github.com/ipython/ipykernel/issues/29
406 406 cur_backend = getattr(configure_inline_support, "current_backend", "unset")
407 407 if new_backend_name != cur_backend:
408 408 # Setup the default figure format
409 409 select_figure_formats(shell, cfg.figure_formats, **cfg.print_figure_kwargs)
410 410 configure_inline_support.current_backend = new_backend_name
@@ -1,739 +1,739 b''
1 1 # encoding: utf-8
2 2 """Tests for the IPython tab-completion machinery."""
3 3
4 4 # Copyright (c) IPython Development Team.
5 5 # Distributed under the terms of the Modified BSD License.
6 6
7 7 import os
8 8 import sys
9 9 import unittest
10 10
11 11 from contextlib import contextmanager
12 12
13 13 import nose.tools as nt
14 14
15 15 from traitlets.config.loader import Config
16 16 from IPython import get_ipython
17 17 from IPython.core import completer
18 18 from IPython.external.decorators import knownfailureif
19 19 from IPython.utils.tempdir import TemporaryDirectory, TemporaryWorkingDirectory
20 20 from IPython.utils.generics import complete_object
21 from IPython.utils.py3compat import string_types, unicode_type
21 from IPython.utils.py3compat import unicode_type
22 22 from IPython.testing import decorators as dec
23 23
24 24 #-----------------------------------------------------------------------------
25 25 # Test functions
26 26 #-----------------------------------------------------------------------------
27 27
28 28 @contextmanager
29 29 def greedy_completion():
30 30 ip = get_ipython()
31 31 greedy_original = ip.Completer.greedy
32 32 try:
33 33 ip.Completer.greedy = True
34 34 yield
35 35 finally:
36 36 ip.Completer.greedy = greedy_original
37 37
38 38 def test_protect_filename():
39 39 if sys.platform == 'win32':
40 40 pairs = [('abc','abc'),
41 41 (' abc','" abc"'),
42 42 ('a bc','"a bc"'),
43 43 ('a bc','"a bc"'),
44 44 (' bc','" bc"'),
45 45 ]
46 46 else:
47 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 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 65 ("a'bc", r"a\'bc"),
66 66 ('a*bc', r'a\*bc'),
67 67 ('a"bc', r'a\"bc'),
68 68 ('a^bc', r'a\^bc'),
69 69 ('a&bc', r'a\&bc'),
70 70 ]
71 71 # run the actual tests
72 72 for s1, s2 in pairs:
73 73 s1p = completer.protect_filename(s1)
74 74 nt.assert_equal(s1p, s2)
75 75
76 76
77 77 def check_line_split(splitter, test_specs):
78 78 for part1, part2, split in test_specs:
79 79 cursor_pos = len(part1)
80 80 line = part1+part2
81 81 out = splitter.split_line(line, cursor_pos)
82 82 nt.assert_equal(out, split)
83 83
84 84
85 85 def test_line_split():
86 86 """Basic line splitter test with default specs."""
87 87 sp = completer.CompletionSplitter()
88 88 # The format of the test specs is: part1, part2, expected answer. Parts 1
89 89 # and 2 are joined into the 'line' sent to the splitter, as if the cursor
90 90 # was at the end of part1. So an empty part2 represents someone hitting
91 91 # tab at the end of the line, the most common case.
92 92 t = [('run some/scrip', '', 'some/scrip'),
93 93 ('run scripts/er', 'ror.py foo', 'scripts/er'),
94 94 ('echo $HOM', '', 'HOM'),
95 95 ('print sys.pa', '', 'sys.pa'),
96 96 ('print(sys.pa', '', 'sys.pa'),
97 97 ("execfile('scripts/er", '', 'scripts/er'),
98 98 ('a[x.', '', 'x.'),
99 99 ('a[x.', 'y', 'x.'),
100 100 ('cd "some_file/', '', 'some_file/'),
101 101 ]
102 102 check_line_split(sp, t)
103 103 # Ensure splitting works OK with unicode by re-running the tests with
104 104 # all inputs turned into unicode
105 105 check_line_split(sp, [ map(unicode_type, p) for p in t] )
106 106
107 107
108 108 def test_custom_completion_error():
109 109 """Test that errors from custom attribute completers are silenced."""
110 110 ip = get_ipython()
111 111 class A(object): pass
112 112 ip.user_ns['a'] = A()
113 113
114 114 @complete_object.when_type(A)
115 115 def complete_A(a, existing_completions):
116 116 raise TypeError("this should be silenced")
117 117
118 118 ip.complete("a.")
119 119
120 120
121 121 def test_unicode_completions():
122 122 ip = get_ipython()
123 123 # Some strings that trigger different types of completion. Check them both
124 124 # in str and unicode forms
125 125 s = ['ru', '%ru', 'cd /', 'floa', 'float(x)/']
126 126 for t in s + list(map(unicode_type, s)):
127 127 # We don't need to check exact completion values (they may change
128 128 # depending on the state of the namespace, but at least no exceptions
129 129 # should be thrown and the return value should be a pair of text, list
130 130 # values.
131 131 text, matches = ip.complete(t)
132 nt.assert_true(isinstance(text, string_types))
132 nt.assert_true(isinstance(text, str))
133 133 nt.assert_true(isinstance(matches, list))
134 134
135 135 def test_latex_completions():
136 136 from IPython.core.latex_symbols import latex_symbols
137 137 import random
138 138 ip = get_ipython()
139 139 # Test some random unicode symbols
140 140 keys = random.sample(latex_symbols.keys(), 10)
141 141 for k in keys:
142 142 text, matches = ip.complete(k)
143 143 nt.assert_equal(len(matches),1)
144 144 nt.assert_equal(text, k)
145 145 nt.assert_equal(matches[0], latex_symbols[k])
146 146 # Test a more complex line
147 147 text, matches = ip.complete(u'print(\\alpha')
148 148 nt.assert_equals(text, u'\\alpha')
149 149 nt.assert_equals(matches[0], latex_symbols['\\alpha'])
150 150 # Test multiple matching latex symbols
151 151 text, matches = ip.complete(u'\\al')
152 152 nt.assert_in('\\alpha', matches)
153 153 nt.assert_in('\\aleph', matches)
154 154
155 155
156 156
157 157
158 158 def test_back_latex_completion():
159 159 ip = get_ipython()
160 160
161 161 # do not return more than 1 matches fro \beta, only the latex one.
162 162 name, matches = ip.complete('\\β')
163 163 nt.assert_equal(len(matches), 1)
164 164 nt.assert_equal(matches[0], '\\beta')
165 165
166 166 def test_back_unicode_completion():
167 167 ip = get_ipython()
168 168
169 169 name, matches = ip.complete('\\Ⅴ')
170 170 nt.assert_equal(len(matches), 1)
171 171 nt.assert_equal(matches[0], '\\ROMAN NUMERAL FIVE')
172 172
173 173
174 174 def test_forward_unicode_completion():
175 175 ip = get_ipython()
176 176
177 177 name, matches = ip.complete('\\ROMAN NUMERAL FIVE')
178 178 nt.assert_equal(len(matches), 1)
179 179 nt.assert_equal(matches[0], 'Ⅴ')
180 180
181 181 @dec.knownfailureif(sys.platform == 'win32', 'Fails if there is a C:\\j... path')
182 182 def test_no_ascii_back_completion():
183 183 ip = get_ipython()
184 184 with TemporaryWorkingDirectory(): # Avoid any filename completions
185 185 # single ascii letter that don't have yet completions
186 186 for letter in 'jJ' :
187 187 name, matches = ip.complete('\\'+letter)
188 188 nt.assert_equal(matches, [])
189 189
190 190
191 191
192 192
193 193 class CompletionSplitterTestCase(unittest.TestCase):
194 194 def setUp(self):
195 195 self.sp = completer.CompletionSplitter()
196 196
197 197 def test_delim_setting(self):
198 198 self.sp.delims = ' '
199 199 nt.assert_equal(self.sp.delims, ' ')
200 200 nt.assert_equal(self.sp._delim_expr, '[\ ]')
201 201
202 202 def test_spaces(self):
203 203 """Test with only spaces as split chars."""
204 204 self.sp.delims = ' '
205 205 t = [('foo', '', 'foo'),
206 206 ('run foo', '', 'foo'),
207 207 ('run foo', 'bar', 'foo'),
208 208 ]
209 209 check_line_split(self.sp, t)
210 210
211 211
212 212 def test_has_open_quotes1():
213 213 for s in ["'", "'''", "'hi' '"]:
214 214 nt.assert_equal(completer.has_open_quotes(s), "'")
215 215
216 216
217 217 def test_has_open_quotes2():
218 218 for s in ['"', '"""', '"hi" "']:
219 219 nt.assert_equal(completer.has_open_quotes(s), '"')
220 220
221 221
222 222 def test_has_open_quotes3():
223 223 for s in ["''", "''' '''", "'hi' 'ipython'"]:
224 224 nt.assert_false(completer.has_open_quotes(s))
225 225
226 226
227 227 def test_has_open_quotes4():
228 228 for s in ['""', '""" """', '"hi" "ipython"']:
229 229 nt.assert_false(completer.has_open_quotes(s))
230 230
231 231
232 232 @knownfailureif(sys.platform == 'win32', "abspath completions fail on Windows")
233 233 def test_abspath_file_completions():
234 234 ip = get_ipython()
235 235 with TemporaryDirectory() as tmpdir:
236 236 prefix = os.path.join(tmpdir, 'foo')
237 237 suffixes = ['1', '2']
238 238 names = [prefix+s for s in suffixes]
239 239 for n in names:
240 240 open(n, 'w').close()
241 241
242 242 # Check simple completion
243 243 c = ip.complete(prefix)[1]
244 244 nt.assert_equal(c, names)
245 245
246 246 # Now check with a function call
247 247 cmd = 'a = f("%s' % prefix
248 248 c = ip.complete(prefix, cmd)[1]
249 249 comp = [prefix+s for s in suffixes]
250 250 nt.assert_equal(c, comp)
251 251
252 252
253 253 def test_local_file_completions():
254 254 ip = get_ipython()
255 255 with TemporaryWorkingDirectory():
256 256 prefix = './foo'
257 257 suffixes = ['1', '2']
258 258 names = [prefix+s for s in suffixes]
259 259 for n in names:
260 260 open(n, 'w').close()
261 261
262 262 # Check simple completion
263 263 c = ip.complete(prefix)[1]
264 264 nt.assert_equal(c, names)
265 265
266 266 # Now check with a function call
267 267 cmd = 'a = f("%s' % prefix
268 268 c = ip.complete(prefix, cmd)[1]
269 269 comp = set(prefix+s for s in suffixes)
270 270 nt.assert_true(comp.issubset(set(c)))
271 271
272 272
273 273 def test_greedy_completions():
274 274 ip = get_ipython()
275 275 ip.ex('a=list(range(5))')
276 276 _,c = ip.complete('.',line='a[0].')
277 277 nt.assert_false('.real' in c,
278 278 "Shouldn't have completed on a[0]: %s"%c)
279 279 with greedy_completion():
280 280 def _(line, cursor_pos, expect, message):
281 281 _,c = ip.complete('.', line=line, cursor_pos=cursor_pos)
282 282 nt.assert_in(expect, c, message%c)
283 283
284 284 yield _, 'a[0].', 5, 'a[0].real', "Should have completed on a[0].: %s"
285 285 yield _, 'a[0].r', 6, 'a[0].real', "Should have completed on a[0].r: %s"
286 286
287 287 if sys.version_info > (3,4):
288 288 yield _, 'a[0].from_', 10, 'a[0].from_bytes', "Should have completed on a[0].from_: %s"
289 289
290 290
291 291
292 292 def test_omit__names():
293 293 # also happens to test IPCompleter as a configurable
294 294 ip = get_ipython()
295 295 ip._hidden_attr = 1
296 296 ip._x = {}
297 297 c = ip.Completer
298 298 ip.ex('ip=get_ipython()')
299 299 cfg = Config()
300 300 cfg.IPCompleter.omit__names = 0
301 301 c.update_config(cfg)
302 302 s,matches = c.complete('ip.')
303 303 nt.assert_in('ip.__str__', matches)
304 304 nt.assert_in('ip._hidden_attr', matches)
305 305 cfg = Config()
306 306 cfg.IPCompleter.omit__names = 1
307 307 c.update_config(cfg)
308 308 s,matches = c.complete('ip.')
309 309 nt.assert_not_in('ip.__str__', matches)
310 310 nt.assert_in('ip._hidden_attr', matches)
311 311 cfg = Config()
312 312 cfg.IPCompleter.omit__names = 2
313 313 c.update_config(cfg)
314 314 s,matches = c.complete('ip.')
315 315 nt.assert_not_in('ip.__str__', matches)
316 316 nt.assert_not_in('ip._hidden_attr', matches)
317 317 s,matches = c.complete('ip._x.')
318 318 nt.assert_in('ip._x.keys', matches)
319 319 del ip._hidden_attr
320 320
321 321
322 322 def test_limit_to__all__False_ok():
323 323 ip = get_ipython()
324 324 c = ip.Completer
325 325 ip.ex('class D: x=24')
326 326 ip.ex('d=D()')
327 327 cfg = Config()
328 328 cfg.IPCompleter.limit_to__all__ = False
329 329 c.update_config(cfg)
330 330 s, matches = c.complete('d.')
331 331 nt.assert_in('d.x', matches)
332 332
333 333
334 334 def test_get__all__entries_ok():
335 335 class A(object):
336 336 __all__ = ['x', 1]
337 337 words = completer.get__all__entries(A())
338 338 nt.assert_equal(words, ['x'])
339 339
340 340
341 341 def test_get__all__entries_no__all__ok():
342 342 class A(object):
343 343 pass
344 344 words = completer.get__all__entries(A())
345 345 nt.assert_equal(words, [])
346 346
347 347
348 348 def test_func_kw_completions():
349 349 ip = get_ipython()
350 350 c = ip.Completer
351 351 ip.ex('def myfunc(a=1,b=2): return a+b')
352 352 s, matches = c.complete(None, 'myfunc(1,b')
353 353 nt.assert_in('b=', matches)
354 354 # Simulate completing with cursor right after b (pos==10):
355 355 s, matches = c.complete(None, 'myfunc(1,b)', 10)
356 356 nt.assert_in('b=', matches)
357 357 s, matches = c.complete(None, 'myfunc(a="escaped\\")string",b')
358 358 nt.assert_in('b=', matches)
359 359 #builtin function
360 360 s, matches = c.complete(None, 'min(k, k')
361 361 nt.assert_in('key=', matches)
362 362
363 363
364 364 def test_default_arguments_from_docstring():
365 365 ip = get_ipython()
366 366 c = ip.Completer
367 367 kwd = c._default_arguments_from_docstring(
368 368 'min(iterable[, key=func]) -> value')
369 369 nt.assert_equal(kwd, ['key'])
370 370 #with cython type etc
371 371 kwd = c._default_arguments_from_docstring(
372 372 'Minuit.migrad(self, int ncall=10000, resume=True, int nsplit=1)\n')
373 373 nt.assert_equal(kwd, ['ncall', 'resume', 'nsplit'])
374 374 #white spaces
375 375 kwd = c._default_arguments_from_docstring(
376 376 '\n Minuit.migrad(self, int ncall=10000, resume=True, int nsplit=1)\n')
377 377 nt.assert_equal(kwd, ['ncall', 'resume', 'nsplit'])
378 378
379 379 def test_line_magics():
380 380 ip = get_ipython()
381 381 c = ip.Completer
382 382 s, matches = c.complete(None, 'lsmag')
383 383 nt.assert_in('%lsmagic', matches)
384 384 s, matches = c.complete(None, '%lsmag')
385 385 nt.assert_in('%lsmagic', matches)
386 386
387 387
388 388 def test_cell_magics():
389 389 from IPython.core.magic import register_cell_magic
390 390
391 391 @register_cell_magic
392 392 def _foo_cellm(line, cell):
393 393 pass
394 394
395 395 ip = get_ipython()
396 396 c = ip.Completer
397 397
398 398 s, matches = c.complete(None, '_foo_ce')
399 399 nt.assert_in('%%_foo_cellm', matches)
400 400 s, matches = c.complete(None, '%%_foo_ce')
401 401 nt.assert_in('%%_foo_cellm', matches)
402 402
403 403
404 404 def test_line_cell_magics():
405 405 from IPython.core.magic import register_line_cell_magic
406 406
407 407 @register_line_cell_magic
408 408 def _bar_cellm(line, cell):
409 409 pass
410 410
411 411 ip = get_ipython()
412 412 c = ip.Completer
413 413
414 414 # The policy here is trickier, see comments in completion code. The
415 415 # returned values depend on whether the user passes %% or not explicitly,
416 416 # and this will show a difference if the same name is both a line and cell
417 417 # magic.
418 418 s, matches = c.complete(None, '_bar_ce')
419 419 nt.assert_in('%_bar_cellm', matches)
420 420 nt.assert_in('%%_bar_cellm', matches)
421 421 s, matches = c.complete(None, '%_bar_ce')
422 422 nt.assert_in('%_bar_cellm', matches)
423 423 nt.assert_in('%%_bar_cellm', matches)
424 424 s, matches = c.complete(None, '%%_bar_ce')
425 425 nt.assert_not_in('%_bar_cellm', matches)
426 426 nt.assert_in('%%_bar_cellm', matches)
427 427
428 428
429 429 def test_magic_completion_order():
430 430
431 431 ip = get_ipython()
432 432 c = ip.Completer
433 433
434 434 # Test ordering of magics and non-magics with the same name
435 435 # We want the non-magic first
436 436
437 437 # Before importing matplotlib, there should only be one option:
438 438
439 439 text, matches = c.complete('mat')
440 440 nt.assert_equal(matches, ["%matplotlib"])
441 441
442 442
443 443 ip.run_cell("matplotlib = 1") # introduce name into namespace
444 444
445 445 # After the import, there should be two options, ordered like this:
446 446 text, matches = c.complete('mat')
447 447 nt.assert_equal(matches, ["matplotlib", "%matplotlib"])
448 448
449 449
450 450 ip.run_cell("timeit = 1") # define a user variable called 'timeit'
451 451
452 452 # Order of user variable and line and cell magics with same name:
453 453 text, matches = c.complete('timeit')
454 454 nt.assert_equal(matches, ["timeit", "%timeit","%%timeit"])
455 455
456 456
457 457 def test_dict_key_completion_string():
458 458 """Test dictionary key completion for string keys"""
459 459 ip = get_ipython()
460 460 complete = ip.Completer.complete
461 461
462 462 ip.user_ns['d'] = {'abc': None}
463 463
464 464 # check completion at different stages
465 465 _, matches = complete(line_buffer="d[")
466 466 nt.assert_in("'abc'", matches)
467 467 nt.assert_not_in("'abc']", matches)
468 468
469 469 _, matches = complete(line_buffer="d['")
470 470 nt.assert_in("abc", matches)
471 471 nt.assert_not_in("abc']", matches)
472 472
473 473 _, matches = complete(line_buffer="d['a")
474 474 nt.assert_in("abc", matches)
475 475 nt.assert_not_in("abc']", matches)
476 476
477 477 # check use of different quoting
478 478 _, matches = complete(line_buffer="d[\"")
479 479 nt.assert_in("abc", matches)
480 480 nt.assert_not_in('abc\"]', matches)
481 481
482 482 _, matches = complete(line_buffer="d[\"a")
483 483 nt.assert_in("abc", matches)
484 484 nt.assert_not_in('abc\"]', matches)
485 485
486 486 # check sensitivity to following context
487 487 _, matches = complete(line_buffer="d[]", cursor_pos=2)
488 488 nt.assert_in("'abc'", matches)
489 489
490 490 _, matches = complete(line_buffer="d['']", cursor_pos=3)
491 491 nt.assert_in("abc", matches)
492 492 nt.assert_not_in("abc'", matches)
493 493 nt.assert_not_in("abc']", matches)
494 494
495 495 # check multiple solutions are correctly returned and that noise is not
496 496 ip.user_ns['d'] = {'abc': None, 'abd': None, 'bad': None, object(): None,
497 497 5: None}
498 498
499 499 _, matches = complete(line_buffer="d['a")
500 500 nt.assert_in("abc", matches)
501 501 nt.assert_in("abd", matches)
502 502 nt.assert_not_in("bad", matches)
503 503 assert not any(m.endswith((']', '"', "'")) for m in matches), matches
504 504
505 505 # check escaping and whitespace
506 506 ip.user_ns['d'] = {'a\nb': None, 'a\'b': None, 'a"b': None, 'a word': None}
507 507 _, matches = complete(line_buffer="d['a")
508 508 nt.assert_in("a\\nb", matches)
509 509 nt.assert_in("a\\'b", matches)
510 510 nt.assert_in("a\"b", matches)
511 511 nt.assert_in("a word", matches)
512 512 assert not any(m.endswith((']', '"', "'")) for m in matches), matches
513 513
514 514 # - can complete on non-initial word of the string
515 515 _, matches = complete(line_buffer="d['a w")
516 516 nt.assert_in("word", matches)
517 517
518 518 # - understands quote escaping
519 519 _, matches = complete(line_buffer="d['a\\'")
520 520 nt.assert_in("b", matches)
521 521
522 522 # - default quoting should work like repr
523 523 _, matches = complete(line_buffer="d[")
524 524 nt.assert_in("\"a'b\"", matches)
525 525
526 526 # - when opening quote with ", possible to match with unescaped apostrophe
527 527 _, matches = complete(line_buffer="d[\"a'")
528 528 nt.assert_in("b", matches)
529 529
530 530 # need to not split at delims that readline won't split at
531 531 if '-' not in ip.Completer.splitter.delims:
532 532 ip.user_ns['d'] = {'before-after': None}
533 533 _, matches = complete(line_buffer="d['before-af")
534 534 nt.assert_in('before-after', matches)
535 535
536 536 def test_dict_key_completion_contexts():
537 537 """Test expression contexts in which dict key completion occurs"""
538 538 ip = get_ipython()
539 539 complete = ip.Completer.complete
540 540 d = {'abc': None}
541 541 ip.user_ns['d'] = d
542 542
543 543 class C:
544 544 data = d
545 545 ip.user_ns['C'] = C
546 546 ip.user_ns['get'] = lambda: d
547 547
548 548 def assert_no_completion(**kwargs):
549 549 _, matches = complete(**kwargs)
550 550 nt.assert_not_in('abc', matches)
551 551 nt.assert_not_in('abc\'', matches)
552 552 nt.assert_not_in('abc\']', matches)
553 553 nt.assert_not_in('\'abc\'', matches)
554 554 nt.assert_not_in('\'abc\']', matches)
555 555
556 556 def assert_completion(**kwargs):
557 557 _, matches = complete(**kwargs)
558 558 nt.assert_in("'abc'", matches)
559 559 nt.assert_not_in("'abc']", matches)
560 560
561 561 # no completion after string closed, even if reopened
562 562 assert_no_completion(line_buffer="d['a'")
563 563 assert_no_completion(line_buffer="d[\"a\"")
564 564 assert_no_completion(line_buffer="d['a' + ")
565 565 assert_no_completion(line_buffer="d['a' + '")
566 566
567 567 # completion in non-trivial expressions
568 568 assert_completion(line_buffer="+ d[")
569 569 assert_completion(line_buffer="(d[")
570 570 assert_completion(line_buffer="C.data[")
571 571
572 572 # greedy flag
573 573 def assert_completion(**kwargs):
574 574 _, matches = complete(**kwargs)
575 575 nt.assert_in("get()['abc']", matches)
576 576
577 577 assert_no_completion(line_buffer="get()[")
578 578 with greedy_completion():
579 579 assert_completion(line_buffer="get()[")
580 580 assert_completion(line_buffer="get()['")
581 581 assert_completion(line_buffer="get()['a")
582 582 assert_completion(line_buffer="get()['ab")
583 583 assert_completion(line_buffer="get()['abc")
584 584
585 585
586 586
587 587 def test_dict_key_completion_bytes():
588 588 """Test handling of bytes in dict key completion"""
589 589 ip = get_ipython()
590 590 complete = ip.Completer.complete
591 591
592 592 ip.user_ns['d'] = {'abc': None, b'abd': None}
593 593
594 594 _, matches = complete(line_buffer="d[")
595 595 nt.assert_in("'abc'", matches)
596 596 nt.assert_in("b'abd'", matches)
597 597
598 598 if False: # not currently implemented
599 599 _, matches = complete(line_buffer="d[b")
600 600 nt.assert_in("b'abd'", matches)
601 601 nt.assert_not_in("b'abc'", matches)
602 602
603 603 _, matches = complete(line_buffer="d[b'")
604 604 nt.assert_in("abd", matches)
605 605 nt.assert_not_in("abc", matches)
606 606
607 607 _, matches = complete(line_buffer="d[B'")
608 608 nt.assert_in("abd", matches)
609 609 nt.assert_not_in("abc", matches)
610 610
611 611 _, matches = complete(line_buffer="d['")
612 612 nt.assert_in("abc", matches)
613 613 nt.assert_not_in("abd", matches)
614 614
615 615
616 616 def test_dict_key_completion_unicode_py3():
617 617 """Test handling of unicode in dict key completion"""
618 618 ip = get_ipython()
619 619 complete = ip.Completer.complete
620 620
621 621 ip.user_ns['d'] = {u'a\u05d0': None}
622 622
623 623 # query using escape
624 624 if sys.platform != 'win32':
625 625 # Known failure on Windows
626 626 _, matches = complete(line_buffer="d['a\\u05d0")
627 627 nt.assert_in("u05d0", matches) # tokenized after \\
628 628
629 629 # query using character
630 630 _, matches = complete(line_buffer="d['a\u05d0")
631 631 nt.assert_in(u"a\u05d0", matches)
632 632
633 633 with greedy_completion():
634 634 # query using escape
635 635 _, matches = complete(line_buffer="d['a\\u05d0")
636 636 nt.assert_in("d['a\\u05d0']", matches) # tokenized after \\
637 637
638 638 # query using character
639 639 _, matches = complete(line_buffer="d['a\u05d0")
640 640 nt.assert_in(u"d['a\u05d0']", matches)
641 641
642 642
643 643
644 644 @dec.skip_without('numpy')
645 645 def test_struct_array_key_completion():
646 646 """Test dict key completion applies to numpy struct arrays"""
647 647 import numpy
648 648 ip = get_ipython()
649 649 complete = ip.Completer.complete
650 650 ip.user_ns['d'] = numpy.array([], dtype=[('hello', 'f'), ('world', 'f')])
651 651 _, matches = complete(line_buffer="d['")
652 652 nt.assert_in("hello", matches)
653 653 nt.assert_in("world", matches)
654 654 # complete on the numpy struct itself
655 655 dt = numpy.dtype([('my_head', [('my_dt', '>u4'), ('my_df', '>u4')]),
656 656 ('my_data', '>f4', 5)])
657 657 x = numpy.zeros(2, dtype=dt)
658 658 ip.user_ns['d'] = x[1]
659 659 _, matches = complete(line_buffer="d['")
660 660 nt.assert_in("my_head", matches)
661 661 nt.assert_in("my_data", matches)
662 662 # complete on a nested level
663 663 with greedy_completion():
664 664 ip.user_ns['d'] = numpy.zeros(2, dtype=dt)
665 665 _, matches = complete(line_buffer="d[1]['my_head']['")
666 666 nt.assert_true(any(["my_dt" in m for m in matches]))
667 667 nt.assert_true(any(["my_df" in m for m in matches]))
668 668
669 669
670 670 @dec.skip_without('pandas')
671 671 def test_dataframe_key_completion():
672 672 """Test dict key completion applies to pandas DataFrames"""
673 673 import pandas
674 674 ip = get_ipython()
675 675 complete = ip.Completer.complete
676 676 ip.user_ns['d'] = pandas.DataFrame({'hello': [1], 'world': [2]})
677 677 _, matches = complete(line_buffer="d['")
678 678 nt.assert_in("hello", matches)
679 679 nt.assert_in("world", matches)
680 680
681 681
682 682 def test_dict_key_completion_invalids():
683 683 """Smoke test cases dict key completion can't handle"""
684 684 ip = get_ipython()
685 685 complete = ip.Completer.complete
686 686
687 687 ip.user_ns['no_getitem'] = None
688 688 ip.user_ns['no_keys'] = []
689 689 ip.user_ns['cant_call_keys'] = dict
690 690 ip.user_ns['empty'] = {}
691 691 ip.user_ns['d'] = {'abc': 5}
692 692
693 693 _, matches = complete(line_buffer="no_getitem['")
694 694 _, matches = complete(line_buffer="no_keys['")
695 695 _, matches = complete(line_buffer="cant_call_keys['")
696 696 _, matches = complete(line_buffer="empty['")
697 697 _, matches = complete(line_buffer="name_error['")
698 698 _, matches = complete(line_buffer="d['\\") # incomplete escape
699 699
700 700 class KeyCompletable(object):
701 701 def __init__(self, things=()):
702 702 self.things = things
703 703
704 704 def _ipython_key_completions_(self):
705 705 return list(self.things)
706 706
707 707 def test_object_key_completion():
708 708 ip = get_ipython()
709 709 ip.user_ns['key_completable'] = KeyCompletable(['qwerty', 'qwick'])
710 710
711 711 _, matches = ip.Completer.complete(line_buffer="key_completable['qw")
712 712 nt.assert_in('qwerty', matches)
713 713 nt.assert_in('qwick', matches)
714 714
715 715
716 716 def test_aimport_module_completer():
717 717 ip = get_ipython()
718 718 _, matches = ip.complete('i', '%aimport i')
719 719 nt.assert_in('io', matches)
720 720 nt.assert_not_in('int', matches)
721 721
722 722 def test_nested_import_module_completer():
723 723 ip = get_ipython()
724 724 _, matches = ip.complete(None, 'import IPython.co', 17)
725 725 nt.assert_in('IPython.core', matches)
726 726 nt.assert_not_in('import IPython.core', matches)
727 727 nt.assert_not_in('IPython.display', matches)
728 728
729 729 def test_import_module_completer():
730 730 ip = get_ipython()
731 731 _, matches = ip.complete('i', 'import i')
732 732 nt.assert_in('io', matches)
733 733 nt.assert_not_in('int', matches)
734 734
735 735 def test_from_module_completer():
736 736 ip = get_ipython()
737 737 _, matches = ip.complete('B', 'from io import B', 16)
738 738 nt.assert_in('BytesIO', matches)
739 739 nt.assert_not_in('BaseException', matches)
@@ -1,162 +1,162 b''
1 1 # -*- coding: utf-8 -*-
2 2 """Tests for completerlib.
3 3
4 4 """
5 5
6 6 #-----------------------------------------------------------------------------
7 7 # Imports
8 8 #-----------------------------------------------------------------------------
9 9
10 10 import os
11 11 import shutil
12 12 import sys
13 13 import tempfile
14 14 import unittest
15 15 from os.path import join
16 16
17 17 import nose.tools as nt
18 18
19 19 from IPython.core.completerlib import magic_run_completer, module_completion
20 20 from IPython.utils import py3compat
21 21 from IPython.utils.tempdir import TemporaryDirectory
22 22 from IPython.testing.decorators import onlyif_unicode_paths
23 23
24 24
25 25 class MockEvent(object):
26 26 def __init__(self, line):
27 27 self.line = line
28 28
29 29 #-----------------------------------------------------------------------------
30 30 # Test functions begin
31 31 #-----------------------------------------------------------------------------
32 32 class Test_magic_run_completer(unittest.TestCase):
33 33 files = [u"aao.py", u"a.py", u"b.py", u"aao.txt"]
34 34 dirs = [u"adir/", "bdir/"]
35 35
36 36 def setUp(self):
37 37 self.BASETESTDIR = tempfile.mkdtemp()
38 38 for fil in self.files:
39 39 with open(join(self.BASETESTDIR, fil), "w") as sfile:
40 40 sfile.write("pass\n")
41 41 for d in self.dirs:
42 42 os.mkdir(join(self.BASETESTDIR, d))
43 43
44 44 self.oldpath = py3compat.getcwd()
45 45 os.chdir(self.BASETESTDIR)
46 46
47 47 def tearDown(self):
48 48 os.chdir(self.oldpath)
49 49 shutil.rmtree(self.BASETESTDIR)
50 50
51 51 def test_1(self):
52 52 """Test magic_run_completer, should match two alterntives
53 53 """
54 54 event = MockEvent(u"%run a")
55 55 mockself = None
56 56 match = set(magic_run_completer(mockself, event))
57 57 self.assertEqual(match, {u"a.py", u"aao.py", u"adir/"})
58 58
59 59 def test_2(self):
60 60 """Test magic_run_completer, should match one alterntive
61 61 """
62 62 event = MockEvent(u"%run aa")
63 63 mockself = None
64 64 match = set(magic_run_completer(mockself, event))
65 65 self.assertEqual(match, {u"aao.py"})
66 66
67 67 def test_3(self):
68 68 """Test magic_run_completer with unterminated " """
69 69 event = MockEvent(u'%run "a')
70 70 mockself = None
71 71 match = set(magic_run_completer(mockself, event))
72 72 self.assertEqual(match, {u"a.py", u"aao.py", u"adir/"})
73 73
74 74 def test_completion_more_args(self):
75 75 event = MockEvent(u'%run a.py ')
76 76 match = set(magic_run_completer(None, event))
77 77 self.assertEqual(match, set(self.files + self.dirs))
78 78
79 79 def test_completion_in_dir(self):
80 80 # Github issue #3459
81 81 event = MockEvent(u'%run a.py {}'.format(join(self.BASETESTDIR, 'a')))
82 82 print(repr(event.line))
83 83 match = set(magic_run_completer(None, event))
84 84 # We specifically use replace here rather than normpath, because
85 85 # at one point there were duplicates 'adir' and 'adir/', and normpath
86 86 # would hide the failure for that.
87 87 self.assertEqual(match, {join(self.BASETESTDIR, f).replace('\\','/')
88 88 for f in (u'a.py', u'aao.py', u'aao.txt', u'adir/')})
89 89
90 90 class Test_magic_run_completer_nonascii(unittest.TestCase):
91 91 @onlyif_unicode_paths
92 92 def setUp(self):
93 93 self.BASETESTDIR = tempfile.mkdtemp()
94 94 for fil in [u"aaø.py", u"a.py", u"b.py"]:
95 95 with open(join(self.BASETESTDIR, fil), "w") as sfile:
96 96 sfile.write("pass\n")
97 97 self.oldpath = py3compat.getcwd()
98 98 os.chdir(self.BASETESTDIR)
99 99
100 100 def tearDown(self):
101 101 os.chdir(self.oldpath)
102 102 shutil.rmtree(self.BASETESTDIR)
103 103
104 104 @onlyif_unicode_paths
105 105 def test_1(self):
106 106 """Test magic_run_completer, should match two alterntives
107 107 """
108 108 event = MockEvent(u"%run a")
109 109 mockself = None
110 110 match = set(magic_run_completer(mockself, event))
111 111 self.assertEqual(match, {u"a.py", u"aaø.py"})
112 112
113 113 @onlyif_unicode_paths
114 114 def test_2(self):
115 115 """Test magic_run_completer, should match one alterntive
116 116 """
117 117 event = MockEvent(u"%run aa")
118 118 mockself = None
119 119 match = set(magic_run_completer(mockself, event))
120 120 self.assertEqual(match, {u"aaø.py"})
121 121
122 122 @onlyif_unicode_paths
123 123 def test_3(self):
124 124 """Test magic_run_completer with unterminated " """
125 125 event = MockEvent(u'%run "a')
126 126 mockself = None
127 127 match = set(magic_run_completer(mockself, event))
128 128 self.assertEqual(match, {u"a.py", u"aaø.py"})
129 129
130 130 # module_completer:
131 131
132 132 def test_import_invalid_module():
133 133 """Testing of issue https://github.com/ipython/ipython/issues/1107"""
134 134 invalid_module_names = {'foo-bar', 'foo:bar', '10foo'}
135 135 valid_module_names = {'foobar'}
136 136 with TemporaryDirectory() as tmpdir:
137 137 sys.path.insert( 0, tmpdir )
138 138 for name in invalid_module_names | valid_module_names:
139 139 filename = os.path.join(tmpdir, name + '.py')
140 140 open(filename, 'w').close()
141 141
142 142 s = set( module_completion('import foo') )
143 143 intersection = s.intersection(invalid_module_names)
144 144 nt.assert_equal(intersection, set())
145 145
146 146 assert valid_module_names.issubset(s), valid_module_names.intersection(s)
147 147
148 148
149 149 def test_bad_module_all():
150 150 """Test module with invalid __all__
151 151
152 152 https://github.com/ipython/ipython/issues/9678
153 153 """
154 154 testsdir = os.path.dirname(__file__)
155 155 sys.path.insert(0, testsdir)
156 156 try:
157 157 results = module_completion('from bad_all import ')
158 158 nt.assert_in('puppies', results)
159 159 for r in results:
160 nt.assert_is_instance(r, py3compat.string_types)
160 nt.assert_is_instance(r, str)
161 161 finally:
162 162 sys.path.remove(testsdir)
@@ -1,614 +1,614 b''
1 1 # -*- coding: utf-8 -*-
2 2 """Tests for the inputsplitter module."""
3 3
4 4
5 5 # Copyright (c) IPython Development Team.
6 6 # Distributed under the terms of the Modified BSD License.
7 7
8 8 import unittest
9 9 import sys
10 10
11 11 import nose.tools as nt
12 12
13 13 from IPython.core import inputsplitter as isp
14 14 from IPython.core.inputtransformer import InputTransformer
15 15 from IPython.core.tests.test_inputtransformer import syntax, syntax_ml
16 16 from IPython.testing import tools as tt
17 17 from IPython.utils import py3compat
18 from IPython.utils.py3compat import string_types, input
18 from IPython.utils.py3compat import input
19 19
20 20 #-----------------------------------------------------------------------------
21 21 # Semi-complete examples (also used as tests)
22 22 #-----------------------------------------------------------------------------
23 23
24 24 # Note: at the bottom, there's a slightly more complete version of this that
25 25 # can be useful during development of code here.
26 26
27 27 def mini_interactive_loop(input_func):
28 28 """Minimal example of the logic of an interactive interpreter loop.
29 29
30 30 This serves as an example, and it is used by the test system with a fake
31 31 raw_input that simulates interactive input."""
32 32
33 33 from IPython.core.inputsplitter import InputSplitter
34 34
35 35 isp = InputSplitter()
36 36 # In practice, this input loop would be wrapped in an outside loop to read
37 37 # input indefinitely, until some exit/quit command was issued. Here we
38 38 # only illustrate the basic inner loop.
39 39 while isp.push_accepts_more():
40 40 indent = ' '*isp.indent_spaces
41 41 prompt = '>>> ' + indent
42 42 line = indent + input_func(prompt)
43 43 isp.push(line)
44 44
45 45 # Here we just return input so we can use it in a test suite, but a real
46 46 # interpreter would instead send it for execution somewhere.
47 47 src = isp.source_reset()
48 48 #print 'Input source was:\n', src # dbg
49 49 return src
50 50
51 51 #-----------------------------------------------------------------------------
52 52 # Test utilities, just for local use
53 53 #-----------------------------------------------------------------------------
54 54
55 55 def assemble(block):
56 56 """Assemble a block into multi-line sub-blocks."""
57 57 return ['\n'.join(sub_block)+'\n' for sub_block in block]
58 58
59 59
60 60 def pseudo_input(lines):
61 61 """Return a function that acts like raw_input but feeds the input list."""
62 62 ilines = iter(lines)
63 63 def raw_in(prompt):
64 64 try:
65 65 return next(ilines)
66 66 except StopIteration:
67 67 return ''
68 68 return raw_in
69 69
70 70 #-----------------------------------------------------------------------------
71 71 # Tests
72 72 #-----------------------------------------------------------------------------
73 73 def test_spaces():
74 74 tests = [('', 0),
75 75 (' ', 1),
76 76 ('\n', 0),
77 77 (' \n', 1),
78 78 ('x', 0),
79 79 (' x', 1),
80 80 (' x',2),
81 81 (' x',4),
82 82 # Note: tabs are counted as a single whitespace!
83 83 ('\tx', 1),
84 84 ('\t x', 2),
85 85 ]
86 86 tt.check_pairs(isp.num_ini_spaces, tests)
87 87
88 88
89 89 def test_remove_comments():
90 90 tests = [('text', 'text'),
91 91 ('text # comment', 'text '),
92 92 ('text # comment\n', 'text \n'),
93 93 ('text # comment \n', 'text \n'),
94 94 ('line # c \nline\n','line \nline\n'),
95 95 ('line # c \nline#c2 \nline\nline #c\n\n',
96 96 'line \nline\nline\nline \n\n'),
97 97 ]
98 98 tt.check_pairs(isp.remove_comments, tests)
99 99
100 100
101 101 def test_get_input_encoding():
102 102 encoding = isp.get_input_encoding()
103 nt.assert_true(isinstance(encoding, string_types))
103 nt.assert_true(isinstance(encoding, str))
104 104 # simple-minded check that at least encoding a simple string works with the
105 105 # encoding we got.
106 106 nt.assert_equal(u'test'.encode(encoding), b'test')
107 107
108 108
109 109 class NoInputEncodingTestCase(unittest.TestCase):
110 110 def setUp(self):
111 111 self.old_stdin = sys.stdin
112 112 class X: pass
113 113 fake_stdin = X()
114 114 sys.stdin = fake_stdin
115 115
116 116 def test(self):
117 117 # Verify that if sys.stdin has no 'encoding' attribute we do the right
118 118 # thing
119 119 enc = isp.get_input_encoding()
120 120 self.assertEqual(enc, 'ascii')
121 121
122 122 def tearDown(self):
123 123 sys.stdin = self.old_stdin
124 124
125 125
126 126 class InputSplitterTestCase(unittest.TestCase):
127 127 def setUp(self):
128 128 self.isp = isp.InputSplitter()
129 129
130 130 def test_reset(self):
131 131 isp = self.isp
132 132 isp.push('x=1')
133 133 isp.reset()
134 134 self.assertEqual(isp._buffer, [])
135 135 self.assertEqual(isp.indent_spaces, 0)
136 136 self.assertEqual(isp.source, '')
137 137 self.assertEqual(isp.code, None)
138 138 self.assertEqual(isp._is_complete, False)
139 139
140 140 def test_source(self):
141 141 self.isp._store('1')
142 142 self.isp._store('2')
143 143 self.assertEqual(self.isp.source, '1\n2\n')
144 144 self.assertEqual(len(self.isp._buffer)>0, True)
145 145 self.assertEqual(self.isp.source_reset(), '1\n2\n')
146 146 self.assertEqual(self.isp._buffer, [])
147 147 self.assertEqual(self.isp.source, '')
148 148
149 149 def test_indent(self):
150 150 isp = self.isp # shorthand
151 151 isp.push('x=1')
152 152 self.assertEqual(isp.indent_spaces, 0)
153 153 isp.push('if 1:\n x=1')
154 154 self.assertEqual(isp.indent_spaces, 4)
155 155 isp.push('y=2\n')
156 156 self.assertEqual(isp.indent_spaces, 0)
157 157
158 158 def test_indent2(self):
159 159 isp = self.isp
160 160 isp.push('if 1:')
161 161 self.assertEqual(isp.indent_spaces, 4)
162 162 isp.push(' x=1')
163 163 self.assertEqual(isp.indent_spaces, 4)
164 164 # Blank lines shouldn't change the indent level
165 165 isp.push(' '*2)
166 166 self.assertEqual(isp.indent_spaces, 4)
167 167
168 168 def test_indent3(self):
169 169 isp = self.isp
170 170 # When a multiline statement contains parens or multiline strings, we
171 171 # shouldn't get confused.
172 172 isp.push("if 1:")
173 173 isp.push(" x = (1+\n 2)")
174 174 self.assertEqual(isp.indent_spaces, 4)
175 175
176 176 def test_indent4(self):
177 177 isp = self.isp
178 178 # whitespace after ':' should not screw up indent level
179 179 isp.push('if 1: \n x=1')
180 180 self.assertEqual(isp.indent_spaces, 4)
181 181 isp.push('y=2\n')
182 182 self.assertEqual(isp.indent_spaces, 0)
183 183 isp.push('if 1:\t\n x=1')
184 184 self.assertEqual(isp.indent_spaces, 4)
185 185 isp.push('y=2\n')
186 186 self.assertEqual(isp.indent_spaces, 0)
187 187
188 188 def test_dedent_pass(self):
189 189 isp = self.isp # shorthand
190 190 # should NOT cause dedent
191 191 isp.push('if 1:\n passes = 5')
192 192 self.assertEqual(isp.indent_spaces, 4)
193 193 isp.push('if 1:\n pass')
194 194 self.assertEqual(isp.indent_spaces, 0)
195 195 isp.push('if 1:\n pass ')
196 196 self.assertEqual(isp.indent_spaces, 0)
197 197
198 198 def test_dedent_break(self):
199 199 isp = self.isp # shorthand
200 200 # should NOT cause dedent
201 201 isp.push('while 1:\n breaks = 5')
202 202 self.assertEqual(isp.indent_spaces, 4)
203 203 isp.push('while 1:\n break')
204 204 self.assertEqual(isp.indent_spaces, 0)
205 205 isp.push('while 1:\n break ')
206 206 self.assertEqual(isp.indent_spaces, 0)
207 207
208 208 def test_dedent_continue(self):
209 209 isp = self.isp # shorthand
210 210 # should NOT cause dedent
211 211 isp.push('while 1:\n continues = 5')
212 212 self.assertEqual(isp.indent_spaces, 4)
213 213 isp.push('while 1:\n continue')
214 214 self.assertEqual(isp.indent_spaces, 0)
215 215 isp.push('while 1:\n continue ')
216 216 self.assertEqual(isp.indent_spaces, 0)
217 217
218 218 def test_dedent_raise(self):
219 219 isp = self.isp # shorthand
220 220 # should NOT cause dedent
221 221 isp.push('if 1:\n raised = 4')
222 222 self.assertEqual(isp.indent_spaces, 4)
223 223 isp.push('if 1:\n raise TypeError()')
224 224 self.assertEqual(isp.indent_spaces, 0)
225 225 isp.push('if 1:\n raise')
226 226 self.assertEqual(isp.indent_spaces, 0)
227 227 isp.push('if 1:\n raise ')
228 228 self.assertEqual(isp.indent_spaces, 0)
229 229
230 230 def test_dedent_return(self):
231 231 isp = self.isp # shorthand
232 232 # should NOT cause dedent
233 233 isp.push('if 1:\n returning = 4')
234 234 self.assertEqual(isp.indent_spaces, 4)
235 235 isp.push('if 1:\n return 5 + 493')
236 236 self.assertEqual(isp.indent_spaces, 0)
237 237 isp.push('if 1:\n return')
238 238 self.assertEqual(isp.indent_spaces, 0)
239 239 isp.push('if 1:\n return ')
240 240 self.assertEqual(isp.indent_spaces, 0)
241 241 isp.push('if 1:\n return(0)')
242 242 self.assertEqual(isp.indent_spaces, 0)
243 243
244 244 def test_push(self):
245 245 isp = self.isp
246 246 self.assertEqual(isp.push('x=1'), True)
247 247
248 248 def test_push2(self):
249 249 isp = self.isp
250 250 self.assertEqual(isp.push('if 1:'), False)
251 251 for line in [' x=1', '# a comment', ' y=2']:
252 252 print(line)
253 253 self.assertEqual(isp.push(line), True)
254 254
255 255 def test_push3(self):
256 256 isp = self.isp
257 257 isp.push('if True:')
258 258 isp.push(' a = 1')
259 259 self.assertEqual(isp.push('b = [1,'), False)
260 260
261 261 def test_push_accepts_more(self):
262 262 isp = self.isp
263 263 isp.push('x=1')
264 264 self.assertEqual(isp.push_accepts_more(), False)
265 265
266 266 def test_push_accepts_more2(self):
267 267 isp = self.isp
268 268 isp.push('if 1:')
269 269 self.assertEqual(isp.push_accepts_more(), True)
270 270 isp.push(' x=1')
271 271 self.assertEqual(isp.push_accepts_more(), True)
272 272 isp.push('')
273 273 self.assertEqual(isp.push_accepts_more(), False)
274 274
275 275 def test_push_accepts_more3(self):
276 276 isp = self.isp
277 277 isp.push("x = (2+\n3)")
278 278 self.assertEqual(isp.push_accepts_more(), False)
279 279
280 280 def test_push_accepts_more4(self):
281 281 isp = self.isp
282 282 # When a multiline statement contains parens or multiline strings, we
283 283 # shouldn't get confused.
284 284 # FIXME: we should be able to better handle de-dents in statements like
285 285 # multiline strings and multiline expressions (continued with \ or
286 286 # parens). Right now we aren't handling the indentation tracking quite
287 287 # correctly with this, though in practice it may not be too much of a
288 288 # problem. We'll need to see.
289 289 isp.push("if 1:")
290 290 isp.push(" x = (2+")
291 291 isp.push(" 3)")
292 292 self.assertEqual(isp.push_accepts_more(), True)
293 293 isp.push(" y = 3")
294 294 self.assertEqual(isp.push_accepts_more(), True)
295 295 isp.push('')
296 296 self.assertEqual(isp.push_accepts_more(), False)
297 297
298 298 def test_push_accepts_more5(self):
299 299 isp = self.isp
300 300 isp.push('try:')
301 301 isp.push(' a = 5')
302 302 isp.push('except:')
303 303 isp.push(' raise')
304 304 # We want to be able to add an else: block at this point, so it should
305 305 # wait for a blank line.
306 306 self.assertEqual(isp.push_accepts_more(), True)
307 307
308 308 def test_continuation(self):
309 309 isp = self.isp
310 310 isp.push("import os, \\")
311 311 self.assertEqual(isp.push_accepts_more(), True)
312 312 isp.push("sys")
313 313 self.assertEqual(isp.push_accepts_more(), False)
314 314
315 315 def test_syntax_error(self):
316 316 isp = self.isp
317 317 # Syntax errors immediately produce a 'ready' block, so the invalid
318 318 # Python can be sent to the kernel for evaluation with possible ipython
319 319 # special-syntax conversion.
320 320 isp.push('run foo')
321 321 self.assertEqual(isp.push_accepts_more(), False)
322 322
323 323 def test_unicode(self):
324 324 self.isp.push(u"Pérez")
325 325 self.isp.push(u'\xc3\xa9')
326 326 self.isp.push(u"u'\xc3\xa9'")
327 327
328 328 def test_line_continuation(self):
329 329 """ Test issue #2108."""
330 330 isp = self.isp
331 331 # A blank line after a line continuation should not accept more
332 332 isp.push("1 \\\n\n")
333 333 self.assertEqual(isp.push_accepts_more(), False)
334 334 # Whitespace after a \ is a SyntaxError. The only way to test that
335 335 # here is to test that push doesn't accept more (as with
336 336 # test_syntax_error() above).
337 337 isp.push(r"1 \ ")
338 338 self.assertEqual(isp.push_accepts_more(), False)
339 339 # Even if the line is continuable (c.f. the regular Python
340 340 # interpreter)
341 341 isp.push(r"(1 \ ")
342 342 self.assertEqual(isp.push_accepts_more(), False)
343 343
344 344 def test_check_complete(self):
345 345 isp = self.isp
346 346 self.assertEqual(isp.check_complete("a = 1"), ('complete', None))
347 347 self.assertEqual(isp.check_complete("for a in range(5):"), ('incomplete', 4))
348 348 self.assertEqual(isp.check_complete("raise = 2"), ('invalid', None))
349 349 self.assertEqual(isp.check_complete("a = [1,\n2,"), ('incomplete', 0))
350 350 self.assertEqual(isp.check_complete("def a():\n x=1\n global x"), ('invalid', None))
351 351
352 352 class InteractiveLoopTestCase(unittest.TestCase):
353 353 """Tests for an interactive loop like a python shell.
354 354 """
355 355 def check_ns(self, lines, ns):
356 356 """Validate that the given input lines produce the resulting namespace.
357 357
358 358 Note: the input lines are given exactly as they would be typed in an
359 359 auto-indenting environment, as mini_interactive_loop above already does
360 360 auto-indenting and prepends spaces to the input.
361 361 """
362 362 src = mini_interactive_loop(pseudo_input(lines))
363 363 test_ns = {}
364 364 exec(src, test_ns)
365 365 # We can't check that the provided ns is identical to the test_ns,
366 366 # because Python fills test_ns with extra keys (copyright, etc). But
367 367 # we can check that the given dict is *contained* in test_ns
368 368 for k,v in ns.items():
369 369 self.assertEqual(test_ns[k], v)
370 370
371 371 def test_simple(self):
372 372 self.check_ns(['x=1'], dict(x=1))
373 373
374 374 def test_simple2(self):
375 375 self.check_ns(['if 1:', 'x=2'], dict(x=2))
376 376
377 377 def test_xy(self):
378 378 self.check_ns(['x=1; y=2'], dict(x=1, y=2))
379 379
380 380 def test_abc(self):
381 381 self.check_ns(['if 1:','a=1','b=2','c=3'], dict(a=1, b=2, c=3))
382 382
383 383 def test_multi(self):
384 384 self.check_ns(['x =(1+','1+','2)'], dict(x=4))
385 385
386 386
387 387 class IPythonInputTestCase(InputSplitterTestCase):
388 388 """By just creating a new class whose .isp is a different instance, we
389 389 re-run the same test battery on the new input splitter.
390 390
391 391 In addition, this runs the tests over the syntax and syntax_ml dicts that
392 392 were tested by individual functions, as part of the OO interface.
393 393
394 394 It also makes some checks on the raw buffer storage.
395 395 """
396 396
397 397 def setUp(self):
398 398 self.isp = isp.IPythonInputSplitter()
399 399
400 400 def test_syntax(self):
401 401 """Call all single-line syntax tests from the main object"""
402 402 isp = self.isp
403 403 for example in syntax.values():
404 404 for raw, out_t in example:
405 405 if raw.startswith(' '):
406 406 continue
407 407
408 408 isp.push(raw+'\n')
409 409 out_raw = isp.source_raw
410 410 out = isp.source_reset()
411 411 self.assertEqual(out.rstrip(), out_t,
412 412 tt.pair_fail_msg.format("inputsplitter",raw, out_t, out))
413 413 self.assertEqual(out_raw.rstrip(), raw.rstrip())
414 414
415 415 def test_syntax_multiline(self):
416 416 isp = self.isp
417 417 for example in syntax_ml.values():
418 418 for line_pairs in example:
419 419 out_t_parts = []
420 420 raw_parts = []
421 421 for lraw, out_t_part in line_pairs:
422 422 if out_t_part is not None:
423 423 out_t_parts.append(out_t_part)
424 424
425 425 if lraw is not None:
426 426 isp.push(lraw)
427 427 raw_parts.append(lraw)
428 428
429 429 out_raw = isp.source_raw
430 430 out = isp.source_reset()
431 431 out_t = '\n'.join(out_t_parts).rstrip()
432 432 raw = '\n'.join(raw_parts).rstrip()
433 433 self.assertEqual(out.rstrip(), out_t)
434 434 self.assertEqual(out_raw.rstrip(), raw)
435 435
436 436 def test_syntax_multiline_cell(self):
437 437 isp = self.isp
438 438 for example in syntax_ml.values():
439 439
440 440 out_t_parts = []
441 441 for line_pairs in example:
442 442 raw = '\n'.join(r for r, _ in line_pairs if r is not None)
443 443 out_t = '\n'.join(t for _,t in line_pairs if t is not None)
444 444 out = isp.transform_cell(raw)
445 445 # Match ignoring trailing whitespace
446 446 self.assertEqual(out.rstrip(), out_t.rstrip())
447 447
448 448 def test_cellmagic_preempt(self):
449 449 isp = self.isp
450 450 for raw, name, line, cell in [
451 451 ("%%cellm a\nIn[1]:", u'cellm', u'a', u'In[1]:'),
452 452 ("%%cellm \nline\n>>> hi", u'cellm', u'', u'line\n>>> hi'),
453 453 (">>> %%cellm \nline\n>>> hi", u'cellm', u'', u'line\nhi'),
454 454 ("%%cellm \n>>> hi", u'cellm', u'', u'>>> hi'),
455 455 ("%%cellm \nline1\nline2", u'cellm', u'', u'line1\nline2'),
456 456 ("%%cellm \nline1\\\\\nline2", u'cellm', u'', u'line1\\\\\nline2'),
457 457 ]:
458 458 expected = "get_ipython().run_cell_magic(%r, %r, %r)" % (
459 459 name, line, cell
460 460 )
461 461 out = isp.transform_cell(raw)
462 462 self.assertEqual(out.rstrip(), expected.rstrip())
463 463
464 464 def test_multiline_passthrough(self):
465 465 isp = self.isp
466 466 class CommentTransformer(InputTransformer):
467 467 def __init__(self):
468 468 self._lines = []
469 469
470 470 def push(self, line):
471 471 self._lines.append(line + '#')
472 472
473 473 def reset(self):
474 474 text = '\n'.join(self._lines)
475 475 self._lines = []
476 476 return text
477 477
478 478 isp.physical_line_transforms.insert(0, CommentTransformer())
479 479
480 480 for raw, expected in [
481 481 ("a=5", "a=5#"),
482 482 ("%ls foo", "get_ipython().magic(%r)" % u'ls foo#'),
483 483 ("!ls foo\n%ls bar", "get_ipython().system(%r)\nget_ipython().magic(%r)" % (
484 484 u'ls foo#', u'ls bar#'
485 485 )),
486 486 ("1\n2\n3\n%ls foo\n4\n5", "1#\n2#\n3#\nget_ipython().magic(%r)\n4#\n5#" % u'ls foo#'),
487 487 ]:
488 488 out = isp.transform_cell(raw)
489 489 self.assertEqual(out.rstrip(), expected.rstrip())
490 490
491 491 #-----------------------------------------------------------------------------
492 492 # Main - use as a script, mostly for developer experiments
493 493 #-----------------------------------------------------------------------------
494 494
495 495 if __name__ == '__main__':
496 496 # A simple demo for interactive experimentation. This code will not get
497 497 # picked up by any test suite.
498 498 from IPython.core.inputsplitter import IPythonInputSplitter
499 499
500 500 # configure here the syntax to use, prompt and whether to autoindent
501 501 #isp, start_prompt = InputSplitter(), '>>> '
502 502 isp, start_prompt = IPythonInputSplitter(), 'In> '
503 503
504 504 autoindent = True
505 505 #autoindent = False
506 506
507 507 try:
508 508 while True:
509 509 prompt = start_prompt
510 510 while isp.push_accepts_more():
511 511 indent = ' '*isp.indent_spaces
512 512 if autoindent:
513 513 line = indent + input(prompt+indent)
514 514 else:
515 515 line = input(prompt)
516 516 isp.push(line)
517 517 prompt = '... '
518 518
519 519 # Here we just return input so we can use it in a test suite, but a
520 520 # real interpreter would instead send it for execution somewhere.
521 521 #src = isp.source; raise EOFError # dbg
522 522 raw = isp.source_raw
523 523 src = isp.source_reset()
524 524 print('Input source was:\n', src)
525 525 print('Raw source was:\n', raw)
526 526 except EOFError:
527 527 print('Bye')
528 528
529 529 # Tests for cell magics support
530 530
531 531 def test_last_blank():
532 532 nt.assert_false(isp.last_blank(''))
533 533 nt.assert_false(isp.last_blank('abc'))
534 534 nt.assert_false(isp.last_blank('abc\n'))
535 535 nt.assert_false(isp.last_blank('abc\na'))
536 536
537 537 nt.assert_true(isp.last_blank('\n'))
538 538 nt.assert_true(isp.last_blank('\n '))
539 539 nt.assert_true(isp.last_blank('abc\n '))
540 540 nt.assert_true(isp.last_blank('abc\n\n'))
541 541 nt.assert_true(isp.last_blank('abc\nd\n\n'))
542 542 nt.assert_true(isp.last_blank('abc\nd\ne\n\n'))
543 543 nt.assert_true(isp.last_blank('abc \n \n \n\n'))
544 544
545 545
546 546 def test_last_two_blanks():
547 547 nt.assert_false(isp.last_two_blanks(''))
548 548 nt.assert_false(isp.last_two_blanks('abc'))
549 549 nt.assert_false(isp.last_two_blanks('abc\n'))
550 550 nt.assert_false(isp.last_two_blanks('abc\n\na'))
551 551 nt.assert_false(isp.last_two_blanks('abc\n \n'))
552 552 nt.assert_false(isp.last_two_blanks('abc\n\n'))
553 553
554 554 nt.assert_true(isp.last_two_blanks('\n\n'))
555 555 nt.assert_true(isp.last_two_blanks('\n\n '))
556 556 nt.assert_true(isp.last_two_blanks('\n \n'))
557 557 nt.assert_true(isp.last_two_blanks('abc\n\n '))
558 558 nt.assert_true(isp.last_two_blanks('abc\n\n\n'))
559 559 nt.assert_true(isp.last_two_blanks('abc\n\n \n'))
560 560 nt.assert_true(isp.last_two_blanks('abc\n\n \n '))
561 561 nt.assert_true(isp.last_two_blanks('abc\n\n \n \n'))
562 562 nt.assert_true(isp.last_two_blanks('abc\nd\n\n\n'))
563 563 nt.assert_true(isp.last_two_blanks('abc\nd\ne\nf\n\n\n'))
564 564
565 565
566 566 class CellMagicsCommon(object):
567 567
568 568 def test_whole_cell(self):
569 569 src = "%%cellm line\nbody\n"
570 570 out = self.sp.transform_cell(src)
571 571 ref = u"get_ipython().run_cell_magic({u}'cellm', {u}'line', {u}'body')\n"
572 572 nt.assert_equal(out, py3compat.u_format(ref))
573 573
574 574 def test_cellmagic_help(self):
575 575 self.sp.push('%%cellm?')
576 576 nt.assert_false(self.sp.push_accepts_more())
577 577
578 578 def tearDown(self):
579 579 self.sp.reset()
580 580
581 581
582 582 class CellModeCellMagics(CellMagicsCommon, unittest.TestCase):
583 583 sp = isp.IPythonInputSplitter(line_input_checker=False)
584 584
585 585 def test_incremental(self):
586 586 sp = self.sp
587 587 sp.push('%%cellm firstline\n')
588 588 nt.assert_true(sp.push_accepts_more()) #1
589 589 sp.push('line2\n')
590 590 nt.assert_true(sp.push_accepts_more()) #2
591 591 sp.push('\n')
592 592 # This should accept a blank line and carry on until the cell is reset
593 593 nt.assert_true(sp.push_accepts_more()) #3
594 594
595 595 def test_no_strip_coding(self):
596 596 src = '\n'.join([
597 597 '%%writefile foo.py',
598 598 '# coding: utf-8',
599 599 'print(u"üñîçø∂é")',
600 600 ])
601 601 out = self.sp.transform_cell(src)
602 602 nt.assert_in('# coding: utf-8', out)
603 603
604 604
605 605 class LineModeCellMagics(CellMagicsCommon, unittest.TestCase):
606 606 sp = isp.IPythonInputSplitter(line_input_checker=True)
607 607
608 608 def test_incremental(self):
609 609 sp = self.sp
610 610 sp.push('%%cellm line2\n')
611 611 nt.assert_true(sp.push_accepts_more()) #1
612 612 sp.push('\n')
613 613 # In this case, a blank line should end the cell magic
614 614 nt.assert_false(sp.push_accepts_more()) #2
@@ -1,1488 +1,1488 b''
1 1 # -*- coding: utf-8 -*-
2 2 """
3 3 Verbose and colourful traceback formatting.
4 4
5 5 **ColorTB**
6 6
7 7 I've always found it a bit hard to visually parse tracebacks in Python. The
8 8 ColorTB class is a solution to that problem. It colors the different parts of a
9 9 traceback in a manner similar to what you would expect from a syntax-highlighting
10 10 text editor.
11 11
12 12 Installation instructions for ColorTB::
13 13
14 14 import sys,ultratb
15 15 sys.excepthook = ultratb.ColorTB()
16 16
17 17 **VerboseTB**
18 18
19 19 I've also included a port of Ka-Ping Yee's "cgitb.py" that produces all kinds
20 20 of useful info when a traceback occurs. Ping originally had it spit out HTML
21 21 and intended it for CGI programmers, but why should they have all the fun? I
22 22 altered it to spit out colored text to the terminal. It's a bit overwhelming,
23 23 but kind of neat, and maybe useful for long-running programs that you believe
24 24 are bug-free. If a crash *does* occur in that type of program you want details.
25 25 Give it a shot--you'll love it or you'll hate it.
26 26
27 27 .. note::
28 28
29 29 The Verbose mode prints the variables currently visible where the exception
30 30 happened (shortening their strings if too long). This can potentially be
31 31 very slow, if you happen to have a huge data structure whose string
32 32 representation is complex to compute. Your computer may appear to freeze for
33 33 a while with cpu usage at 100%. If this occurs, you can cancel the traceback
34 34 with Ctrl-C (maybe hitting it more than once).
35 35
36 36 If you encounter this kind of situation often, you may want to use the
37 37 Verbose_novars mode instead of the regular Verbose, which avoids formatting
38 38 variables (but otherwise includes the information and context given by
39 39 Verbose).
40 40
41 41 .. note::
42 42
43 43 The verbose mode print all variables in the stack, which means it can
44 44 potentially leak sensitive information like access keys, or unencryted
45 45 password.
46 46
47 47 Installation instructions for VerboseTB::
48 48
49 49 import sys,ultratb
50 50 sys.excepthook = ultratb.VerboseTB()
51 51
52 52 Note: Much of the code in this module was lifted verbatim from the standard
53 53 library module 'traceback.py' and Ka-Ping Yee's 'cgitb.py'.
54 54
55 55 Color schemes
56 56 -------------
57 57
58 58 The colors are defined in the class TBTools through the use of the
59 59 ColorSchemeTable class. Currently the following exist:
60 60
61 61 - NoColor: allows all of this module to be used in any terminal (the color
62 62 escapes are just dummy blank strings).
63 63
64 64 - Linux: is meant to look good in a terminal like the Linux console (black
65 65 or very dark background).
66 66
67 67 - LightBG: similar to Linux but swaps dark/light colors to be more readable
68 68 in light background terminals.
69 69
70 70 - Neutral: a neutral color scheme that should be readable on both light and
71 71 dark background
72 72
73 73 You can implement other color schemes easily, the syntax is fairly
74 74 self-explanatory. Please send back new schemes you develop to the author for
75 75 possible inclusion in future releases.
76 76
77 77 Inheritance diagram:
78 78
79 79 .. inheritance-diagram:: IPython.core.ultratb
80 80 :parts: 3
81 81 """
82 82
83 83 #*****************************************************************************
84 84 # Copyright (C) 2001 Nathaniel Gray <n8gray@caltech.edu>
85 85 # Copyright (C) 2001-2004 Fernando Perez <fperez@colorado.edu>
86 86 #
87 87 # Distributed under the terms of the BSD License. The full license is in
88 88 # the file COPYING, distributed as part of this software.
89 89 #*****************************************************************************
90 90
91 91
92 92 import dis
93 93 import inspect
94 94 import keyword
95 95 import linecache
96 96 import os
97 97 import pydoc
98 98 import re
99 99 import sys
100 100 import time
101 101 import tokenize
102 102 import traceback
103 103 import types
104 104
105 105 try: # Python 2
106 106 generate_tokens = tokenize.generate_tokens
107 107 except AttributeError: # Python 3
108 108 generate_tokens = tokenize.tokenize
109 109
110 110 # For purposes of monkeypatching inspect to fix a bug in it.
111 111 from inspect import getsourcefile, getfile, getmodule, \
112 112 ismodule, isclass, ismethod, isfunction, istraceback, isframe, iscode
113 113
114 114 # IPython's own modules
115 115 from IPython import get_ipython
116 116 from IPython.core import debugger
117 117 from IPython.core.display_trap import DisplayTrap
118 118 from IPython.core.excolors import exception_colors
119 119 from IPython.utils import PyColorize
120 120 from IPython.utils import openpy
121 121 from IPython.utils import path as util_path
122 122 from IPython.utils import py3compat
123 123 from IPython.utils import ulinecache
124 124 from IPython.utils.data import uniq_stable
125 125 from IPython.utils.terminal import get_terminal_size
126 126 from logging import info, error
127 127
128 128 import IPython.utils.colorable as colorable
129 129
130 130 # Globals
131 131 # amount of space to put line numbers before verbose tracebacks
132 132 INDENT_SIZE = 8
133 133
134 134 # Default color scheme. This is used, for example, by the traceback
135 135 # formatter. When running in an actual IPython instance, the user's rc.colors
136 136 # value is used, but having a module global makes this functionality available
137 137 # to users of ultratb who are NOT running inside ipython.
138 138 DEFAULT_SCHEME = 'NoColor'
139 139
140 140 # ---------------------------------------------------------------------------
141 141 # Code begins
142 142
143 143 # Utility functions
144 144 def inspect_error():
145 145 """Print a message about internal inspect errors.
146 146
147 147 These are unfortunately quite common."""
148 148
149 149 error('Internal Python error in the inspect module.\n'
150 150 'Below is the traceback from this internal error.\n')
151 151
152 152
153 153 # This function is a monkeypatch we apply to the Python inspect module. We have
154 154 # now found when it's needed (see discussion on issue gh-1456), and we have a
155 155 # test case (IPython.core.tests.test_ultratb.ChangedPyFileTest) that fails if
156 156 # the monkeypatch is not applied. TK, Aug 2012.
157 157 def findsource(object):
158 158 """Return the entire source file and starting line number for an object.
159 159
160 160 The argument may be a module, class, method, function, traceback, frame,
161 161 or code object. The source code is returned as a list of all the lines
162 162 in the file and the line number indexes a line in that list. An IOError
163 163 is raised if the source code cannot be retrieved.
164 164
165 165 FIXED version with which we monkeypatch the stdlib to work around a bug."""
166 166
167 167 file = getsourcefile(object) or getfile(object)
168 168 # If the object is a frame, then trying to get the globals dict from its
169 169 # module won't work. Instead, the frame object itself has the globals
170 170 # dictionary.
171 171 globals_dict = None
172 172 if inspect.isframe(object):
173 173 # XXX: can this ever be false?
174 174 globals_dict = object.f_globals
175 175 else:
176 176 module = getmodule(object, file)
177 177 if module:
178 178 globals_dict = module.__dict__
179 179 lines = linecache.getlines(file, globals_dict)
180 180 if not lines:
181 181 raise IOError('could not get source code')
182 182
183 183 if ismodule(object):
184 184 return lines, 0
185 185
186 186 if isclass(object):
187 187 name = object.__name__
188 188 pat = re.compile(r'^(\s*)class\s*' + name + r'\b')
189 189 # make some effort to find the best matching class definition:
190 190 # use the one with the least indentation, which is the one
191 191 # that's most probably not inside a function definition.
192 192 candidates = []
193 193 for i in range(len(lines)):
194 194 match = pat.match(lines[i])
195 195 if match:
196 196 # if it's at toplevel, it's already the best one
197 197 if lines[i][0] == 'c':
198 198 return lines, i
199 199 # else add whitespace to candidate list
200 200 candidates.append((match.group(1), i))
201 201 if candidates:
202 202 # this will sort by whitespace, and by line number,
203 203 # less whitespace first
204 204 candidates.sort()
205 205 return lines, candidates[0][1]
206 206 else:
207 207 raise IOError('could not find class definition')
208 208
209 209 if ismethod(object):
210 210 object = object.__func__
211 211 if isfunction(object):
212 212 object = object.__code__
213 213 if istraceback(object):
214 214 object = object.tb_frame
215 215 if isframe(object):
216 216 object = object.f_code
217 217 if iscode(object):
218 218 if not hasattr(object, 'co_firstlineno'):
219 219 raise IOError('could not find function definition')
220 220 pat = re.compile(r'^(\s*def\s)|(.*(?<!\w)lambda(:|\s))|^(\s*@)')
221 221 pmatch = pat.match
222 222 # fperez - fix: sometimes, co_firstlineno can give a number larger than
223 223 # the length of lines, which causes an error. Safeguard against that.
224 224 lnum = min(object.co_firstlineno, len(lines)) - 1
225 225 while lnum > 0:
226 226 if pmatch(lines[lnum]):
227 227 break
228 228 lnum -= 1
229 229
230 230 return lines, lnum
231 231 raise IOError('could not find code object')
232 232
233 233
234 234 # This is a patched version of inspect.getargs that applies the (unmerged)
235 235 # patch for http://bugs.python.org/issue14611 by Stefano Taschini. This fixes
236 236 # https://github.com/ipython/ipython/issues/8205 and
237 237 # https://github.com/ipython/ipython/issues/8293
238 238 def getargs(co):
239 239 """Get information about the arguments accepted by a code object.
240 240
241 241 Three things are returned: (args, varargs, varkw), where 'args' is
242 242 a list of argument names (possibly containing nested lists), and
243 243 'varargs' and 'varkw' are the names of the * and ** arguments or None."""
244 244 if not iscode(co):
245 245 raise TypeError('{!r} is not a code object'.format(co))
246 246
247 247 nargs = co.co_argcount
248 248 names = co.co_varnames
249 249 args = list(names[:nargs])
250 250 step = 0
251 251
252 252 # The following acrobatics are for anonymous (tuple) arguments.
253 253 for i in range(nargs):
254 254 if args[i][:1] in ('', '.'):
255 255 stack, remain, count = [], [], []
256 256 while step < len(co.co_code):
257 257 op = ord(co.co_code[step])
258 258 step = step + 1
259 259 if op >= dis.HAVE_ARGUMENT:
260 260 opname = dis.opname[op]
261 261 value = ord(co.co_code[step]) + ord(co.co_code[step+1])*256
262 262 step = step + 2
263 263 if opname in ('UNPACK_TUPLE', 'UNPACK_SEQUENCE'):
264 264 remain.append(value)
265 265 count.append(value)
266 266 elif opname in ('STORE_FAST', 'STORE_DEREF'):
267 267 if op in dis.haslocal:
268 268 stack.append(co.co_varnames[value])
269 269 elif op in dis.hasfree:
270 270 stack.append((co.co_cellvars + co.co_freevars)[value])
271 271 # Special case for sublists of length 1: def foo((bar))
272 272 # doesn't generate the UNPACK_TUPLE bytecode, so if
273 273 # `remain` is empty here, we have such a sublist.
274 274 if not remain:
275 275 stack[0] = [stack[0]]
276 276 break
277 277 else:
278 278 remain[-1] = remain[-1] - 1
279 279 while remain[-1] == 0:
280 280 remain.pop()
281 281 size = count.pop()
282 282 stack[-size:] = [stack[-size:]]
283 283 if not remain:
284 284 break
285 285 remain[-1] = remain[-1] - 1
286 286 if not remain:
287 287 break
288 288 args[i] = stack[0]
289 289
290 290 varargs = None
291 291 if co.co_flags & inspect.CO_VARARGS:
292 292 varargs = co.co_varnames[nargs]
293 293 nargs = nargs + 1
294 294 varkw = None
295 295 if co.co_flags & inspect.CO_VARKEYWORDS:
296 296 varkw = co.co_varnames[nargs]
297 297 return inspect.Arguments(args, varargs, varkw)
298 298
299 299
300 300 # Monkeypatch inspect to apply our bugfix.
301 301 def with_patch_inspect(f):
302 302 """decorator for monkeypatching inspect.findsource"""
303 303
304 304 def wrapped(*args, **kwargs):
305 305 save_findsource = inspect.findsource
306 306 save_getargs = inspect.getargs
307 307 inspect.findsource = findsource
308 308 inspect.getargs = getargs
309 309 try:
310 310 return f(*args, **kwargs)
311 311 finally:
312 312 inspect.findsource = save_findsource
313 313 inspect.getargs = save_getargs
314 314
315 315 return wrapped
316 316
317 317
318 318 if py3compat.PY3:
319 319 fixed_getargvalues = inspect.getargvalues
320 320 else:
321 321 # Fixes for https://github.com/ipython/ipython/issues/8293
322 322 # and https://github.com/ipython/ipython/issues/8205.
323 323 # The relevant bug is caused by failure to correctly handle anonymous tuple
324 324 # unpacking, which only exists in Python 2.
325 325 fixed_getargvalues = with_patch_inspect(inspect.getargvalues)
326 326
327 327
328 328 def fix_frame_records_filenames(records):
329 329 """Try to fix the filenames in each record from inspect.getinnerframes().
330 330
331 331 Particularly, modules loaded from within zip files have useless filenames
332 332 attached to their code object, and inspect.getinnerframes() just uses it.
333 333 """
334 334 fixed_records = []
335 335 for frame, filename, line_no, func_name, lines, index in records:
336 336 # Look inside the frame's globals dictionary for __file__,
337 337 # which should be better. However, keep Cython filenames since
338 338 # we prefer the source filenames over the compiled .so file.
339 339 filename = py3compat.cast_unicode_py2(filename, "utf-8")
340 340 if not filename.endswith(('.pyx', '.pxd', '.pxi')):
341 341 better_fn = frame.f_globals.get('__file__', None)
342 342 if isinstance(better_fn, str):
343 343 # Check the type just in case someone did something weird with
344 344 # __file__. It might also be None if the error occurred during
345 345 # import.
346 346 filename = better_fn
347 347 fixed_records.append((frame, filename, line_no, func_name, lines, index))
348 348 return fixed_records
349 349
350 350
351 351 @with_patch_inspect
352 352 def _fixed_getinnerframes(etb, context=1, tb_offset=0):
353 353 LNUM_POS, LINES_POS, INDEX_POS = 2, 4, 5
354 354
355 355 records = fix_frame_records_filenames(inspect.getinnerframes(etb, context))
356 356 # If the error is at the console, don't build any context, since it would
357 357 # otherwise produce 5 blank lines printed out (there is no file at the
358 358 # console)
359 359 rec_check = records[tb_offset:]
360 360 try:
361 361 rname = rec_check[0][1]
362 362 if rname == '<ipython console>' or rname.endswith('<string>'):
363 363 return rec_check
364 364 except IndexError:
365 365 pass
366 366
367 367 aux = traceback.extract_tb(etb)
368 368 assert len(records) == len(aux)
369 369 for i, (file, lnum, _, _) in zip(range(len(records)), aux):
370 370 maybeStart = lnum - 1 - context // 2
371 371 start = max(maybeStart, 0)
372 372 end = start + context
373 373 lines = ulinecache.getlines(file)[start:end]
374 374 buf = list(records[i])
375 375 buf[LNUM_POS] = lnum
376 376 buf[INDEX_POS] = lnum - 1 - start
377 377 buf[LINES_POS] = lines
378 378 records[i] = tuple(buf)
379 379 return records[tb_offset:]
380 380
381 381 # Helper function -- largely belongs to VerboseTB, but we need the same
382 382 # functionality to produce a pseudo verbose TB for SyntaxErrors, so that they
383 383 # can be recognized properly by ipython.el's py-traceback-line-re
384 384 # (SyntaxErrors have to be treated specially because they have no traceback)
385 385
386 386
387 387 def _format_traceback_lines(lnum, index, lines, Colors, lvals=None, _line_format=(lambda x,_:x,None)):
388 388 numbers_width = INDENT_SIZE - 1
389 389 res = []
390 390 i = lnum - index
391 391
392 392 for line in lines:
393 393 line = py3compat.cast_unicode(line)
394 394
395 395 new_line, err = _line_format(line, 'str')
396 396 if not err: line = new_line
397 397
398 398 if i == lnum:
399 399 # This is the line with the error
400 400 pad = numbers_width - len(str(i))
401 401 num = '%s%s' % (debugger.make_arrow(pad), str(lnum))
402 402 line = '%s%s%s %s%s' % (Colors.linenoEm, num,
403 403 Colors.line, line, Colors.Normal)
404 404 else:
405 405 num = '%*s' % (numbers_width, i)
406 406 line = '%s%s%s %s' % (Colors.lineno, num,
407 407 Colors.Normal, line)
408 408
409 409 res.append(line)
410 410 if lvals and i == lnum:
411 411 res.append(lvals + '\n')
412 412 i = i + 1
413 413 return res
414 414
415 415 def is_recursion_error(etype, value, records):
416 416 try:
417 417 # RecursionError is new in Python 3.5
418 418 recursion_error_type = RecursionError
419 419 except NameError:
420 420 recursion_error_type = RuntimeError
421 421
422 422 # The default recursion limit is 1000, but some of that will be taken up
423 423 # by stack frames in IPython itself. >500 frames probably indicates
424 424 # a recursion error.
425 425 return (etype is recursion_error_type) \
426 426 and "recursion" in str(value).lower() \
427 427 and len(records) > 500
428 428
429 429 def find_recursion(etype, value, records):
430 430 """Identify the repeating stack frames from a RecursionError traceback
431 431
432 432 'records' is a list as returned by VerboseTB.get_records()
433 433
434 434 Returns (last_unique, repeat_length)
435 435 """
436 436 # This involves a bit of guesswork - we want to show enough of the traceback
437 437 # to indicate where the recursion is occurring. We guess that the innermost
438 438 # quarter of the traceback (250 frames by default) is repeats, and find the
439 439 # first frame (from in to out) that looks different.
440 440 if not is_recursion_error(etype, value, records):
441 441 return len(records), 0
442 442
443 443 # Select filename, lineno, func_name to track frames with
444 444 records = [r[1:4] for r in records]
445 445 inner_frames = records[-(len(records)//4):]
446 446 frames_repeated = set(inner_frames)
447 447
448 448 last_seen_at = {}
449 449 longest_repeat = 0
450 450 i = len(records)
451 451 for frame in reversed(records):
452 452 i -= 1
453 453 if frame not in frames_repeated:
454 454 last_unique = i
455 455 break
456 456
457 457 if frame in last_seen_at:
458 458 distance = last_seen_at[frame] - i
459 459 longest_repeat = max(longest_repeat, distance)
460 460
461 461 last_seen_at[frame] = i
462 462 else:
463 463 last_unique = 0 # The whole traceback was recursion
464 464
465 465 return last_unique, longest_repeat
466 466
467 467 #---------------------------------------------------------------------------
468 468 # Module classes
469 469 class TBTools(colorable.Colorable):
470 470 """Basic tools used by all traceback printer classes."""
471 471
472 472 # Number of frames to skip when reporting tracebacks
473 473 tb_offset = 0
474 474
475 475 def __init__(self, color_scheme='NoColor', call_pdb=False, ostream=None, parent=None, config=None):
476 476 # Whether to call the interactive pdb debugger after printing
477 477 # tracebacks or not
478 478 super(TBTools, self).__init__(parent=parent, config=config)
479 479 self.call_pdb = call_pdb
480 480
481 481 # Output stream to write to. Note that we store the original value in
482 482 # a private attribute and then make the public ostream a property, so
483 483 # that we can delay accessing sys.stdout until runtime. The way
484 484 # things are written now, the sys.stdout object is dynamically managed
485 485 # so a reference to it should NEVER be stored statically. This
486 486 # property approach confines this detail to a single location, and all
487 487 # subclasses can simply access self.ostream for writing.
488 488 self._ostream = ostream
489 489
490 490 # Create color table
491 491 self.color_scheme_table = exception_colors()
492 492
493 493 self.set_colors(color_scheme)
494 494 self.old_scheme = color_scheme # save initial value for toggles
495 495
496 496 if call_pdb:
497 497 self.pdb = debugger.Pdb()
498 498 else:
499 499 self.pdb = None
500 500
501 501 def _get_ostream(self):
502 502 """Output stream that exceptions are written to.
503 503
504 504 Valid values are:
505 505
506 506 - None: the default, which means that IPython will dynamically resolve
507 507 to sys.stdout. This ensures compatibility with most tools, including
508 508 Windows (where plain stdout doesn't recognize ANSI escapes).
509 509
510 510 - Any object with 'write' and 'flush' attributes.
511 511 """
512 512 return sys.stdout if self._ostream is None else self._ostream
513 513
514 514 def _set_ostream(self, val):
515 515 assert val is None or (hasattr(val, 'write') and hasattr(val, 'flush'))
516 516 self._ostream = val
517 517
518 518 ostream = property(_get_ostream, _set_ostream)
519 519
520 520 def set_colors(self, *args, **kw):
521 521 """Shorthand access to the color table scheme selector method."""
522 522
523 523 # Set own color table
524 524 self.color_scheme_table.set_active_scheme(*args, **kw)
525 525 # for convenience, set Colors to the active scheme
526 526 self.Colors = self.color_scheme_table.active_colors
527 527 # Also set colors of debugger
528 528 if hasattr(self, 'pdb') and self.pdb is not None:
529 529 self.pdb.set_colors(*args, **kw)
530 530
531 531 def color_toggle(self):
532 532 """Toggle between the currently active color scheme and NoColor."""
533 533
534 534 if self.color_scheme_table.active_scheme_name == 'NoColor':
535 535 self.color_scheme_table.set_active_scheme(self.old_scheme)
536 536 self.Colors = self.color_scheme_table.active_colors
537 537 else:
538 538 self.old_scheme = self.color_scheme_table.active_scheme_name
539 539 self.color_scheme_table.set_active_scheme('NoColor')
540 540 self.Colors = self.color_scheme_table.active_colors
541 541
542 542 def stb2text(self, stb):
543 543 """Convert a structured traceback (a list) to a string."""
544 544 return '\n'.join(stb)
545 545
546 546 def text(self, etype, value, tb, tb_offset=None, context=5):
547 547 """Return formatted traceback.
548 548
549 549 Subclasses may override this if they add extra arguments.
550 550 """
551 551 tb_list = self.structured_traceback(etype, value, tb,
552 552 tb_offset, context)
553 553 return self.stb2text(tb_list)
554 554
555 555 def structured_traceback(self, etype, evalue, tb, tb_offset=None,
556 556 context=5, mode=None):
557 557 """Return a list of traceback frames.
558 558
559 559 Must be implemented by each class.
560 560 """
561 561 raise NotImplementedError()
562 562
563 563
564 564 #---------------------------------------------------------------------------
565 565 class ListTB(TBTools):
566 566 """Print traceback information from a traceback list, with optional color.
567 567
568 568 Calling requires 3 arguments: (etype, evalue, elist)
569 569 as would be obtained by::
570 570
571 571 etype, evalue, tb = sys.exc_info()
572 572 if tb:
573 573 elist = traceback.extract_tb(tb)
574 574 else:
575 575 elist = None
576 576
577 577 It can thus be used by programs which need to process the traceback before
578 578 printing (such as console replacements based on the code module from the
579 579 standard library).
580 580
581 581 Because they are meant to be called without a full traceback (only a
582 582 list), instances of this class can't call the interactive pdb debugger."""
583 583
584 584 def __init__(self, color_scheme='NoColor', call_pdb=False, ostream=None, parent=None, config=None):
585 585 TBTools.__init__(self, color_scheme=color_scheme, call_pdb=call_pdb,
586 586 ostream=ostream, parent=parent,config=config)
587 587
588 588 def __call__(self, etype, value, elist):
589 589 self.ostream.flush()
590 590 self.ostream.write(self.text(etype, value, elist))
591 591 self.ostream.write('\n')
592 592
593 593 def structured_traceback(self, etype, value, elist, tb_offset=None,
594 594 context=5):
595 595 """Return a color formatted string with the traceback info.
596 596
597 597 Parameters
598 598 ----------
599 599 etype : exception type
600 600 Type of the exception raised.
601 601
602 602 value : object
603 603 Data stored in the exception
604 604
605 605 elist : list
606 606 List of frames, see class docstring for details.
607 607
608 608 tb_offset : int, optional
609 609 Number of frames in the traceback to skip. If not given, the
610 610 instance value is used (set in constructor).
611 611
612 612 context : int, optional
613 613 Number of lines of context information to print.
614 614
615 615 Returns
616 616 -------
617 617 String with formatted exception.
618 618 """
619 619 tb_offset = self.tb_offset if tb_offset is None else tb_offset
620 620 Colors = self.Colors
621 621 out_list = []
622 622 if elist:
623 623
624 624 if tb_offset and len(elist) > tb_offset:
625 625 elist = elist[tb_offset:]
626 626
627 627 out_list.append('Traceback %s(most recent call last)%s:' %
628 628 (Colors.normalEm, Colors.Normal) + '\n')
629 629 out_list.extend(self._format_list(elist))
630 630 # The exception info should be a single entry in the list.
631 631 lines = ''.join(self._format_exception_only(etype, value))
632 632 out_list.append(lines)
633 633
634 634 # Note: this code originally read:
635 635
636 636 ## for line in lines[:-1]:
637 637 ## out_list.append(" "+line)
638 638 ## out_list.append(lines[-1])
639 639
640 640 # This means it was indenting everything but the last line by a little
641 641 # bit. I've disabled this for now, but if we see ugliness somewhere we
642 642 # can restore it.
643 643
644 644 return out_list
645 645
646 646 def _format_list(self, extracted_list):
647 647 """Format a list of traceback entry tuples for printing.
648 648
649 649 Given a list of tuples as returned by extract_tb() or
650 650 extract_stack(), return a list of strings ready for printing.
651 651 Each string in the resulting list corresponds to the item with the
652 652 same index in the argument list. Each string ends in a newline;
653 653 the strings may contain internal newlines as well, for those items
654 654 whose source text line is not None.
655 655
656 656 Lifted almost verbatim from traceback.py
657 657 """
658 658
659 659 Colors = self.Colors
660 660 list = []
661 661 for filename, lineno, name, line in extracted_list[:-1]:
662 662 item = ' File %s"%s"%s, line %s%d%s, in %s%s%s\n' % \
663 663 (Colors.filename, py3compat.cast_unicode_py2(filename, "utf-8"), Colors.Normal,
664 664 Colors.lineno, lineno, Colors.Normal,
665 665 Colors.name, py3compat.cast_unicode_py2(name, "utf-8"), Colors.Normal)
666 666 if line:
667 667 item += ' %s\n' % line.strip()
668 668 list.append(item)
669 669 # Emphasize the last entry
670 670 filename, lineno, name, line = extracted_list[-1]
671 671 item = '%s File %s"%s"%s, line %s%d%s, in %s%s%s%s\n' % \
672 672 (Colors.normalEm,
673 673 Colors.filenameEm, py3compat.cast_unicode_py2(filename, "utf-8"), Colors.normalEm,
674 674 Colors.linenoEm, lineno, Colors.normalEm,
675 675 Colors.nameEm, py3compat.cast_unicode_py2(name, "utf-8"), Colors.normalEm,
676 676 Colors.Normal)
677 677 if line:
678 678 item += '%s %s%s\n' % (Colors.line, line.strip(),
679 679 Colors.Normal)
680 680 list.append(item)
681 681 return list
682 682
683 683 def _format_exception_only(self, etype, value):
684 684 """Format the exception part of a traceback.
685 685
686 686 The arguments are the exception type and value such as given by
687 687 sys.exc_info()[:2]. The return value is a list of strings, each ending
688 688 in a newline. Normally, the list contains a single string; however,
689 689 for SyntaxError exceptions, it contains several lines that (when
690 690 printed) display detailed information about where the syntax error
691 691 occurred. The message indicating which exception occurred is the
692 692 always last string in the list.
693 693
694 694 Also lifted nearly verbatim from traceback.py
695 695 """
696 696 have_filedata = False
697 697 Colors = self.Colors
698 698 list = []
699 699 stype = py3compat.cast_unicode(Colors.excName + etype.__name__ + Colors.Normal)
700 700 if value is None:
701 701 # Not sure if this can still happen in Python 2.6 and above
702 702 list.append(stype + '\n')
703 703 else:
704 704 if issubclass(etype, SyntaxError):
705 705 have_filedata = True
706 706 if not value.filename: value.filename = "<string>"
707 707 if value.lineno:
708 708 lineno = value.lineno
709 709 textline = ulinecache.getline(value.filename, value.lineno)
710 710 else:
711 711 lineno = 'unknown'
712 712 textline = ''
713 713 list.append('%s File %s"%s"%s, line %s%s%s\n' % \
714 714 (Colors.normalEm,
715 715 Colors.filenameEm, py3compat.cast_unicode(value.filename), Colors.normalEm,
716 716 Colors.linenoEm, lineno, Colors.Normal ))
717 717 if textline == '':
718 718 textline = py3compat.cast_unicode(value.text, "utf-8")
719 719
720 720 if textline is not None:
721 721 i = 0
722 722 while i < len(textline) and textline[i].isspace():
723 723 i += 1
724 724 list.append('%s %s%s\n' % (Colors.line,
725 725 textline.strip(),
726 726 Colors.Normal))
727 727 if value.offset is not None:
728 728 s = ' '
729 729 for c in textline[i:value.offset - 1]:
730 730 if c.isspace():
731 731 s += c
732 732 else:
733 733 s += ' '
734 734 list.append('%s%s^%s\n' % (Colors.caret, s,
735 735 Colors.Normal))
736 736
737 737 try:
738 738 s = value.msg
739 739 except Exception:
740 740 s = self._some_str(value)
741 741 if s:
742 742 list.append('%s%s:%s %s\n' % (stype, Colors.excName,
743 743 Colors.Normal, s))
744 744 else:
745 745 list.append('%s\n' % stype)
746 746
747 747 # sync with user hooks
748 748 if have_filedata:
749 749 ipinst = get_ipython()
750 750 if ipinst is not None:
751 751 ipinst.hooks.synchronize_with_editor(value.filename, value.lineno, 0)
752 752
753 753 return list
754 754
755 755 def get_exception_only(self, etype, value):
756 756 """Only print the exception type and message, without a traceback.
757 757
758 758 Parameters
759 759 ----------
760 760 etype : exception type
761 761 value : exception value
762 762 """
763 763 return ListTB.structured_traceback(self, etype, value, [])
764 764
765 765 def show_exception_only(self, etype, evalue):
766 766 """Only print the exception type and message, without a traceback.
767 767
768 768 Parameters
769 769 ----------
770 770 etype : exception type
771 771 value : exception value
772 772 """
773 773 # This method needs to use __call__ from *this* class, not the one from
774 774 # a subclass whose signature or behavior may be different
775 775 ostream = self.ostream
776 776 ostream.flush()
777 777 ostream.write('\n'.join(self.get_exception_only(etype, evalue)))
778 778 ostream.flush()
779 779
780 780 def _some_str(self, value):
781 781 # Lifted from traceback.py
782 782 try:
783 783 return py3compat.cast_unicode(str(value))
784 784 except:
785 785 return u'<unprintable %s object>' % type(value).__name__
786 786
787 787
788 788 #----------------------------------------------------------------------------
789 789 class VerboseTB(TBTools):
790 790 """A port of Ka-Ping Yee's cgitb.py module that outputs color text instead
791 791 of HTML. Requires inspect and pydoc. Crazy, man.
792 792
793 793 Modified version which optionally strips the topmost entries from the
794 794 traceback, to be used with alternate interpreters (because their own code
795 795 would appear in the traceback)."""
796 796
797 797 def __init__(self, color_scheme='Linux', call_pdb=False, ostream=None,
798 798 tb_offset=0, long_header=False, include_vars=True,
799 799 check_cache=None, debugger_cls = None,
800 800 parent=None, config=None):
801 801 """Specify traceback offset, headers and color scheme.
802 802
803 803 Define how many frames to drop from the tracebacks. Calling it with
804 804 tb_offset=1 allows use of this handler in interpreters which will have
805 805 their own code at the top of the traceback (VerboseTB will first
806 806 remove that frame before printing the traceback info)."""
807 807 TBTools.__init__(self, color_scheme=color_scheme, call_pdb=call_pdb,
808 808 ostream=ostream, parent=parent, config=config)
809 809 self.tb_offset = tb_offset
810 810 self.long_header = long_header
811 811 self.include_vars = include_vars
812 812 # By default we use linecache.checkcache, but the user can provide a
813 813 # different check_cache implementation. This is used by the IPython
814 814 # kernel to provide tracebacks for interactive code that is cached,
815 815 # by a compiler instance that flushes the linecache but preserves its
816 816 # own code cache.
817 817 if check_cache is None:
818 818 check_cache = linecache.checkcache
819 819 self.check_cache = check_cache
820 820
821 821 self.debugger_cls = debugger_cls or debugger.Pdb
822 822
823 823 def format_records(self, records, last_unique, recursion_repeat):
824 824 """Format the stack frames of the traceback"""
825 825 frames = []
826 826 for r in records[:last_unique+recursion_repeat+1]:
827 827 #print '*** record:',file,lnum,func,lines,index # dbg
828 828 frames.append(self.format_record(*r))
829 829
830 830 if recursion_repeat:
831 831 frames.append('... last %d frames repeated, from the frame below ...\n' % recursion_repeat)
832 832 frames.append(self.format_record(*records[last_unique+recursion_repeat+1]))
833 833
834 834 return frames
835 835
836 836 def format_record(self, frame, file, lnum, func, lines, index):
837 837 """Format a single stack frame"""
838 838 Colors = self.Colors # just a shorthand + quicker name lookup
839 839 ColorsNormal = Colors.Normal # used a lot
840 840 col_scheme = self.color_scheme_table.active_scheme_name
841 841 indent = ' ' * INDENT_SIZE
842 842 em_normal = '%s\n%s%s' % (Colors.valEm, indent, ColorsNormal)
843 843 undefined = '%sundefined%s' % (Colors.em, ColorsNormal)
844 844 tpl_link = '%s%%s%s' % (Colors.filenameEm, ColorsNormal)
845 845 tpl_call = 'in %s%%s%s%%s%s' % (Colors.vName, Colors.valEm,
846 846 ColorsNormal)
847 847 tpl_call_fail = 'in %s%%s%s(***failed resolving arguments***)%s' % \
848 848 (Colors.vName, Colors.valEm, ColorsNormal)
849 849 tpl_local_var = '%s%%s%s' % (Colors.vName, ColorsNormal)
850 850 tpl_global_var = '%sglobal%s %s%%s%s' % (Colors.em, ColorsNormal,
851 851 Colors.vName, ColorsNormal)
852 852 tpl_name_val = '%%s %s= %%s%s' % (Colors.valEm, ColorsNormal)
853 853
854 854 tpl_line = '%s%%s%s %%s' % (Colors.lineno, ColorsNormal)
855 855 tpl_line_em = '%s%%s%s %%s%s' % (Colors.linenoEm, Colors.line,
856 856 ColorsNormal)
857 857
858 858 abspath = os.path.abspath
859 859
860 860
861 861 if not file:
862 862 file = '?'
863 863 elif file.startswith(str("<")) and file.endswith(str(">")):
864 864 # Not a real filename, no problem...
865 865 pass
866 866 elif not os.path.isabs(file):
867 867 # Try to make the filename absolute by trying all
868 868 # sys.path entries (which is also what linecache does)
869 869 for dirname in sys.path:
870 870 try:
871 871 fullname = os.path.join(dirname, file)
872 872 if os.path.isfile(fullname):
873 873 file = os.path.abspath(fullname)
874 874 break
875 875 except Exception:
876 876 # Just in case that sys.path contains very
877 877 # strange entries...
878 878 pass
879 879
880 880 file = py3compat.cast_unicode(file, util_path.fs_encoding)
881 881 link = tpl_link % file
882 882 args, varargs, varkw, locals = fixed_getargvalues(frame)
883 883
884 884 if func == '?':
885 885 call = ''
886 886 else:
887 887 # Decide whether to include variable details or not
888 888 var_repr = self.include_vars and eqrepr or nullrepr
889 889 try:
890 890 call = tpl_call % (func, inspect.formatargvalues(args,
891 891 varargs, varkw,
892 892 locals, formatvalue=var_repr))
893 893 except KeyError:
894 894 # This happens in situations like errors inside generator
895 895 # expressions, where local variables are listed in the
896 896 # line, but can't be extracted from the frame. I'm not
897 897 # 100% sure this isn't actually a bug in inspect itself,
898 898 # but since there's no info for us to compute with, the
899 899 # best we can do is report the failure and move on. Here
900 900 # we must *not* call any traceback construction again,
901 901 # because that would mess up use of %debug later on. So we
902 902 # simply report the failure and move on. The only
903 903 # limitation will be that this frame won't have locals
904 904 # listed in the call signature. Quite subtle problem...
905 905 # I can't think of a good way to validate this in a unit
906 906 # test, but running a script consisting of:
907 907 # dict( (k,v.strip()) for (k,v) in range(10) )
908 908 # will illustrate the error, if this exception catch is
909 909 # disabled.
910 910 call = tpl_call_fail % func
911 911
912 912 # Don't attempt to tokenize binary files.
913 913 if file.endswith(('.so', '.pyd', '.dll')):
914 914 return '%s %s\n' % (link, call)
915 915
916 916 elif file.endswith(('.pyc', '.pyo')):
917 917 # Look up the corresponding source file.
918 918 try:
919 919 file = openpy.source_from_cache(file)
920 920 except ValueError:
921 921 # Failed to get the source file for some reason
922 922 # E.g. https://github.com/ipython/ipython/issues/9486
923 923 return '%s %s\n' % (link, call)
924 924
925 925 def linereader(file=file, lnum=[lnum], getline=ulinecache.getline):
926 926 line = getline(file, lnum[0])
927 927 lnum[0] += 1
928 928 return line
929 929
930 930 # Build the list of names on this line of code where the exception
931 931 # occurred.
932 932 try:
933 933 names = []
934 934 name_cont = False
935 935
936 936 for token_type, token, start, end, line in generate_tokens(linereader):
937 937 # build composite names
938 938 if token_type == tokenize.NAME and token not in keyword.kwlist:
939 939 if name_cont:
940 940 # Continuation of a dotted name
941 941 try:
942 942 names[-1].append(token)
943 943 except IndexError:
944 944 names.append([token])
945 945 name_cont = False
946 946 else:
947 947 # Regular new names. We append everything, the caller
948 948 # will be responsible for pruning the list later. It's
949 949 # very tricky to try to prune as we go, b/c composite
950 950 # names can fool us. The pruning at the end is easy
951 951 # to do (or the caller can print a list with repeated
952 952 # names if so desired.
953 953 names.append([token])
954 954 elif token == '.':
955 955 name_cont = True
956 956 elif token_type == tokenize.NEWLINE:
957 957 break
958 958
959 959 except (IndexError, UnicodeDecodeError, SyntaxError):
960 960 # signals exit of tokenizer
961 961 # SyntaxError can occur if the file is not actually Python
962 962 # - see gh-6300
963 963 pass
964 964 except tokenize.TokenError as msg:
965 965 _m = ("An unexpected error occurred while tokenizing input\n"
966 966 "The following traceback may be corrupted or invalid\n"
967 967 "The error message is: %s\n" % msg)
968 968 error(_m)
969 969
970 970 # Join composite names (e.g. "dict.fromkeys")
971 971 names = ['.'.join(n) for n in names]
972 972 # prune names list of duplicates, but keep the right order
973 973 unique_names = uniq_stable(names)
974 974
975 975 # Start loop over vars
976 976 lvals = []
977 977 if self.include_vars:
978 978 for name_full in unique_names:
979 979 name_base = name_full.split('.', 1)[0]
980 980 if name_base in frame.f_code.co_varnames:
981 981 if name_base in locals:
982 982 try:
983 983 value = repr(eval(name_full, locals))
984 984 except:
985 985 value = undefined
986 986 else:
987 987 value = undefined
988 988 name = tpl_local_var % name_full
989 989 else:
990 990 if name_base in frame.f_globals:
991 991 try:
992 992 value = repr(eval(name_full, frame.f_globals))
993 993 except:
994 994 value = undefined
995 995 else:
996 996 value = undefined
997 997 name = tpl_global_var % name_full
998 998 lvals.append(tpl_name_val % (name, value))
999 999 if lvals:
1000 1000 lvals = '%s%s' % (indent, em_normal.join(lvals))
1001 1001 else:
1002 1002 lvals = ''
1003 1003
1004 1004 level = '%s %s\n' % (link, call)
1005 1005
1006 1006 if index is None:
1007 1007 return level
1008 1008 else:
1009 1009 _line_format = PyColorize.Parser(style=col_scheme, parent=self).format2
1010 1010 return '%s%s' % (level, ''.join(
1011 1011 _format_traceback_lines(lnum, index, lines, Colors, lvals,
1012 1012 _line_format)))
1013 1013
1014 1014 def prepare_chained_exception_message(self, cause):
1015 1015 direct_cause = "\nThe above exception was the direct cause of the following exception:\n"
1016 1016 exception_during_handling = "\nDuring handling of the above exception, another exception occurred:\n"
1017 1017
1018 1018 if cause:
1019 1019 message = [[direct_cause]]
1020 1020 else:
1021 1021 message = [[exception_during_handling]]
1022 1022 return message
1023 1023
1024 1024 def prepare_header(self, etype, long_version=False):
1025 1025 colors = self.Colors # just a shorthand + quicker name lookup
1026 1026 colorsnormal = colors.Normal # used a lot
1027 1027 exc = '%s%s%s' % (colors.excName, etype, colorsnormal)
1028 1028 width = min(75, get_terminal_size()[0])
1029 1029 if long_version:
1030 1030 # Header with the exception type, python version, and date
1031 1031 pyver = 'Python ' + sys.version.split()[0] + ': ' + sys.executable
1032 1032 date = time.ctime(time.time())
1033 1033
1034 1034 head = '%s%s%s\n%s%s%s\n%s' % (colors.topline, '-' * width, colorsnormal,
1035 1035 exc, ' ' * (width - len(str(etype)) - len(pyver)),
1036 1036 pyver, date.rjust(width) )
1037 1037 head += "\nA problem occurred executing Python code. Here is the sequence of function" \
1038 1038 "\ncalls leading up to the error, with the most recent (innermost) call last."
1039 1039 else:
1040 1040 # Simplified header
1041 1041 head = '%s%s' % (exc, 'Traceback (most recent call last)'. \
1042 1042 rjust(width - len(str(etype))) )
1043 1043
1044 1044 return head
1045 1045
1046 1046 def format_exception(self, etype, evalue):
1047 1047 colors = self.Colors # just a shorthand + quicker name lookup
1048 1048 colorsnormal = colors.Normal # used a lot
1049 1049 indent = ' ' * INDENT_SIZE
1050 1050 # Get (safely) a string form of the exception info
1051 1051 try:
1052 1052 etype_str, evalue_str = map(str, (etype, evalue))
1053 1053 except:
1054 1054 # User exception is improperly defined.
1055 1055 etype, evalue = str, sys.exc_info()[:2]
1056 1056 etype_str, evalue_str = map(str, (etype, evalue))
1057 1057 # ... and format it
1058 1058 exception = ['%s%s%s: %s' % (colors.excName, etype_str,
1059 1059 colorsnormal, py3compat.cast_unicode(evalue_str))]
1060 1060
1061 1061 if (not py3compat.PY3) and type(evalue) is types.InstanceType:
1062 1062 try:
1063 names = [w for w in dir(evalue) if isinstance(w, py3compat.string_types)]
1063 names = [w for w in dir(evalue) if isinstance(w, str)]
1064 1064 except:
1065 1065 # Every now and then, an object with funny internals blows up
1066 1066 # when dir() is called on it. We do the best we can to report
1067 1067 # the problem and continue
1068 1068 _m = '%sException reporting error (object with broken dir())%s:'
1069 1069 exception.append(_m % (colors.excName, colorsnormal))
1070 1070 etype_str, evalue_str = map(str, sys.exc_info()[:2])
1071 1071 exception.append('%s%s%s: %s' % (colors.excName, etype_str,
1072 1072 colorsnormal, py3compat.cast_unicode(evalue_str)))
1073 1073 names = []
1074 1074 for name in names:
1075 1075 value = text_repr(getattr(evalue, name))
1076 1076 exception.append('\n%s%s = %s' % (indent, name, value))
1077 1077
1078 1078 return exception
1079 1079
1080 1080 def format_exception_as_a_whole(self, etype, evalue, etb, number_of_lines_of_context, tb_offset):
1081 1081 """Formats the header, traceback and exception message for a single exception.
1082 1082
1083 1083 This may be called multiple times by Python 3 exception chaining
1084 1084 (PEP 3134).
1085 1085 """
1086 1086 # some locals
1087 1087 orig_etype = etype
1088 1088 try:
1089 1089 etype = etype.__name__
1090 1090 except AttributeError:
1091 1091 pass
1092 1092
1093 1093 tb_offset = self.tb_offset if tb_offset is None else tb_offset
1094 1094 head = self.prepare_header(etype, self.long_header)
1095 1095 records = self.get_records(etb, number_of_lines_of_context, tb_offset)
1096 1096
1097 1097 if records is None:
1098 1098 return ""
1099 1099
1100 1100 last_unique, recursion_repeat = find_recursion(orig_etype, evalue, records)
1101 1101
1102 1102 frames = self.format_records(records, last_unique, recursion_repeat)
1103 1103
1104 1104 formatted_exception = self.format_exception(etype, evalue)
1105 1105 if records:
1106 1106 filepath, lnum = records[-1][1:3]
1107 1107 filepath = os.path.abspath(filepath)
1108 1108 ipinst = get_ipython()
1109 1109 if ipinst is not None:
1110 1110 ipinst.hooks.synchronize_with_editor(filepath, lnum, 0)
1111 1111
1112 1112 return [[head] + frames + [''.join(formatted_exception[0])]]
1113 1113
1114 1114 def get_records(self, etb, number_of_lines_of_context, tb_offset):
1115 1115 try:
1116 1116 # Try the default getinnerframes and Alex's: Alex's fixes some
1117 1117 # problems, but it generates empty tracebacks for console errors
1118 1118 # (5 blanks lines) where none should be returned.
1119 1119 return _fixed_getinnerframes(etb, number_of_lines_of_context, tb_offset)
1120 1120 except UnicodeDecodeError:
1121 1121 # This can occur if a file's encoding magic comment is wrong.
1122 1122 # I can't see a way to recover without duplicating a bunch of code
1123 1123 # from the stdlib traceback module. --TK
1124 1124 error('\nUnicodeDecodeError while processing traceback.\n')
1125 1125 return None
1126 1126 except:
1127 1127 # FIXME: I've been getting many crash reports from python 2.3
1128 1128 # users, traceable to inspect.py. If I can find a small test-case
1129 1129 # to reproduce this, I should either write a better workaround or
1130 1130 # file a bug report against inspect (if that's the real problem).
1131 1131 # So far, I haven't been able to find an isolated example to
1132 1132 # reproduce the problem.
1133 1133 inspect_error()
1134 1134 traceback.print_exc(file=self.ostream)
1135 1135 info('\nUnfortunately, your original traceback can not be constructed.\n')
1136 1136 return None
1137 1137
1138 1138 def get_parts_of_chained_exception(self, evalue):
1139 1139 def get_chained_exception(exception_value):
1140 1140 cause = getattr(exception_value, '__cause__', None)
1141 1141 if cause:
1142 1142 return cause
1143 1143 if getattr(exception_value, '__suppress_context__', False):
1144 1144 return None
1145 1145 return getattr(exception_value, '__context__', None)
1146 1146
1147 1147 chained_evalue = get_chained_exception(evalue)
1148 1148
1149 1149 if chained_evalue:
1150 1150 return chained_evalue.__class__, chained_evalue, chained_evalue.__traceback__
1151 1151
1152 1152 def structured_traceback(self, etype, evalue, etb, tb_offset=None,
1153 1153 number_of_lines_of_context=5):
1154 1154 """Return a nice text document describing the traceback."""
1155 1155
1156 1156 formatted_exception = self.format_exception_as_a_whole(etype, evalue, etb, number_of_lines_of_context,
1157 1157 tb_offset)
1158 1158
1159 1159 colors = self.Colors # just a shorthand + quicker name lookup
1160 1160 colorsnormal = colors.Normal # used a lot
1161 1161 head = '%s%s%s' % (colors.topline, '-' * min(75, get_terminal_size()[0]), colorsnormal)
1162 1162 structured_traceback_parts = [head]
1163 1163 if py3compat.PY3:
1164 1164 chained_exceptions_tb_offset = 0
1165 1165 lines_of_context = 3
1166 1166 formatted_exceptions = formatted_exception
1167 1167 exception = self.get_parts_of_chained_exception(evalue)
1168 1168 if exception:
1169 1169 formatted_exceptions += self.prepare_chained_exception_message(evalue.__cause__)
1170 1170 etype, evalue, etb = exception
1171 1171 else:
1172 1172 evalue = None
1173 1173 chained_exc_ids = set()
1174 1174 while evalue:
1175 1175 formatted_exceptions += self.format_exception_as_a_whole(etype, evalue, etb, lines_of_context,
1176 1176 chained_exceptions_tb_offset)
1177 1177 exception = self.get_parts_of_chained_exception(evalue)
1178 1178
1179 1179 if exception and not id(exception[1]) in chained_exc_ids:
1180 1180 chained_exc_ids.add(id(exception[1])) # trace exception to avoid infinite 'cause' loop
1181 1181 formatted_exceptions += self.prepare_chained_exception_message(evalue.__cause__)
1182 1182 etype, evalue, etb = exception
1183 1183 else:
1184 1184 evalue = None
1185 1185
1186 1186 # we want to see exceptions in a reversed order:
1187 1187 # the first exception should be on top
1188 1188 for formatted_exception in reversed(formatted_exceptions):
1189 1189 structured_traceback_parts += formatted_exception
1190 1190 else:
1191 1191 structured_traceback_parts += formatted_exception[0]
1192 1192
1193 1193 return structured_traceback_parts
1194 1194
1195 1195 def debugger(self, force=False):
1196 1196 """Call up the pdb debugger if desired, always clean up the tb
1197 1197 reference.
1198 1198
1199 1199 Keywords:
1200 1200
1201 1201 - force(False): by default, this routine checks the instance call_pdb
1202 1202 flag and does not actually invoke the debugger if the flag is false.
1203 1203 The 'force' option forces the debugger to activate even if the flag
1204 1204 is false.
1205 1205
1206 1206 If the call_pdb flag is set, the pdb interactive debugger is
1207 1207 invoked. In all cases, the self.tb reference to the current traceback
1208 1208 is deleted to prevent lingering references which hamper memory
1209 1209 management.
1210 1210
1211 1211 Note that each call to pdb() does an 'import readline', so if your app
1212 1212 requires a special setup for the readline completers, you'll have to
1213 1213 fix that by hand after invoking the exception handler."""
1214 1214
1215 1215 if force or self.call_pdb:
1216 1216 if self.pdb is None:
1217 1217 self.pdb = self.debugger_cls()
1218 1218 # the system displayhook may have changed, restore the original
1219 1219 # for pdb
1220 1220 display_trap = DisplayTrap(hook=sys.__displayhook__)
1221 1221 with display_trap:
1222 1222 self.pdb.reset()
1223 1223 # Find the right frame so we don't pop up inside ipython itself
1224 1224 if hasattr(self, 'tb') and self.tb is not None:
1225 1225 etb = self.tb
1226 1226 else:
1227 1227 etb = self.tb = sys.last_traceback
1228 1228 while self.tb is not None and self.tb.tb_next is not None:
1229 1229 self.tb = self.tb.tb_next
1230 1230 if etb and etb.tb_next:
1231 1231 etb = etb.tb_next
1232 1232 self.pdb.botframe = etb.tb_frame
1233 1233 self.pdb.interaction(self.tb.tb_frame, self.tb)
1234 1234
1235 1235 if hasattr(self, 'tb'):
1236 1236 del self.tb
1237 1237
1238 1238 def handler(self, info=None):
1239 1239 (etype, evalue, etb) = info or sys.exc_info()
1240 1240 self.tb = etb
1241 1241 ostream = self.ostream
1242 1242 ostream.flush()
1243 1243 ostream.write(self.text(etype, evalue, etb))
1244 1244 ostream.write('\n')
1245 1245 ostream.flush()
1246 1246
1247 1247 # Changed so an instance can just be called as VerboseTB_inst() and print
1248 1248 # out the right info on its own.
1249 1249 def __call__(self, etype=None, evalue=None, etb=None):
1250 1250 """This hook can replace sys.excepthook (for Python 2.1 or higher)."""
1251 1251 if etb is None:
1252 1252 self.handler()
1253 1253 else:
1254 1254 self.handler((etype, evalue, etb))
1255 1255 try:
1256 1256 self.debugger()
1257 1257 except KeyboardInterrupt:
1258 1258 print("\nKeyboardInterrupt")
1259 1259
1260 1260
1261 1261 #----------------------------------------------------------------------------
1262 1262 class FormattedTB(VerboseTB, ListTB):
1263 1263 """Subclass ListTB but allow calling with a traceback.
1264 1264
1265 1265 It can thus be used as a sys.excepthook for Python > 2.1.
1266 1266
1267 1267 Also adds 'Context' and 'Verbose' modes, not available in ListTB.
1268 1268
1269 1269 Allows a tb_offset to be specified. This is useful for situations where
1270 1270 one needs to remove a number of topmost frames from the traceback (such as
1271 1271 occurs with python programs that themselves execute other python code,
1272 1272 like Python shells). """
1273 1273
1274 1274 def __init__(self, mode='Plain', color_scheme='Linux', call_pdb=False,
1275 1275 ostream=None,
1276 1276 tb_offset=0, long_header=False, include_vars=False,
1277 1277 check_cache=None, debugger_cls=None,
1278 1278 parent=None, config=None):
1279 1279
1280 1280 # NEVER change the order of this list. Put new modes at the end:
1281 1281 self.valid_modes = ['Plain', 'Context', 'Verbose']
1282 1282 self.verbose_modes = self.valid_modes[1:3]
1283 1283
1284 1284 VerboseTB.__init__(self, color_scheme=color_scheme, call_pdb=call_pdb,
1285 1285 ostream=ostream, tb_offset=tb_offset,
1286 1286 long_header=long_header, include_vars=include_vars,
1287 1287 check_cache=check_cache, debugger_cls=debugger_cls,
1288 1288 parent=parent, config=config)
1289 1289
1290 1290 # Different types of tracebacks are joined with different separators to
1291 1291 # form a single string. They are taken from this dict
1292 1292 self._join_chars = dict(Plain='', Context='\n', Verbose='\n')
1293 1293 # set_mode also sets the tb_join_char attribute
1294 1294 self.set_mode(mode)
1295 1295
1296 1296 def _extract_tb(self, tb):
1297 1297 if tb:
1298 1298 return traceback.extract_tb(tb)
1299 1299 else:
1300 1300 return None
1301 1301
1302 1302 def structured_traceback(self, etype, value, tb, tb_offset=None, number_of_lines_of_context=5):
1303 1303 tb_offset = self.tb_offset if tb_offset is None else tb_offset
1304 1304 mode = self.mode
1305 1305 if mode in self.verbose_modes:
1306 1306 # Verbose modes need a full traceback
1307 1307 return VerboseTB.structured_traceback(
1308 1308 self, etype, value, tb, tb_offset, number_of_lines_of_context
1309 1309 )
1310 1310 else:
1311 1311 # We must check the source cache because otherwise we can print
1312 1312 # out-of-date source code.
1313 1313 self.check_cache()
1314 1314 # Now we can extract and format the exception
1315 1315 elist = self._extract_tb(tb)
1316 1316 return ListTB.structured_traceback(
1317 1317 self, etype, value, elist, tb_offset, number_of_lines_of_context
1318 1318 )
1319 1319
1320 1320 def stb2text(self, stb):
1321 1321 """Convert a structured traceback (a list) to a string."""
1322 1322 return self.tb_join_char.join(stb)
1323 1323
1324 1324
1325 1325 def set_mode(self, mode=None):
1326 1326 """Switch to the desired mode.
1327 1327
1328 1328 If mode is not specified, cycles through the available modes."""
1329 1329
1330 1330 if not mode:
1331 1331 new_idx = (self.valid_modes.index(self.mode) + 1 ) % \
1332 1332 len(self.valid_modes)
1333 1333 self.mode = self.valid_modes[new_idx]
1334 1334 elif mode not in self.valid_modes:
1335 1335 raise ValueError('Unrecognized mode in FormattedTB: <' + mode + '>\n'
1336 1336 'Valid modes: ' + str(self.valid_modes))
1337 1337 else:
1338 1338 self.mode = mode
1339 1339 # include variable details only in 'Verbose' mode
1340 1340 self.include_vars = (self.mode == self.valid_modes[2])
1341 1341 # Set the join character for generating text tracebacks
1342 1342 self.tb_join_char = self._join_chars[self.mode]
1343 1343
1344 1344 # some convenient shortcuts
1345 1345 def plain(self):
1346 1346 self.set_mode(self.valid_modes[0])
1347 1347
1348 1348 def context(self):
1349 1349 self.set_mode(self.valid_modes[1])
1350 1350
1351 1351 def verbose(self):
1352 1352 self.set_mode(self.valid_modes[2])
1353 1353
1354 1354
1355 1355 #----------------------------------------------------------------------------
1356 1356 class AutoFormattedTB(FormattedTB):
1357 1357 """A traceback printer which can be called on the fly.
1358 1358
1359 1359 It will find out about exceptions by itself.
1360 1360
1361 1361 A brief example::
1362 1362
1363 1363 AutoTB = AutoFormattedTB(mode = 'Verbose',color_scheme='Linux')
1364 1364 try:
1365 1365 ...
1366 1366 except:
1367 1367 AutoTB() # or AutoTB(out=logfile) where logfile is an open file object
1368 1368 """
1369 1369
1370 1370 def __call__(self, etype=None, evalue=None, etb=None,
1371 1371 out=None, tb_offset=None):
1372 1372 """Print out a formatted exception traceback.
1373 1373
1374 1374 Optional arguments:
1375 1375 - out: an open file-like object to direct output to.
1376 1376
1377 1377 - tb_offset: the number of frames to skip over in the stack, on a
1378 1378 per-call basis (this overrides temporarily the instance's tb_offset
1379 1379 given at initialization time. """
1380 1380
1381 1381 if out is None:
1382 1382 out = self.ostream
1383 1383 out.flush()
1384 1384 out.write(self.text(etype, evalue, etb, tb_offset))
1385 1385 out.write('\n')
1386 1386 out.flush()
1387 1387 # FIXME: we should remove the auto pdb behavior from here and leave
1388 1388 # that to the clients.
1389 1389 try:
1390 1390 self.debugger()
1391 1391 except KeyboardInterrupt:
1392 1392 print("\nKeyboardInterrupt")
1393 1393
1394 1394 def structured_traceback(self, etype=None, value=None, tb=None,
1395 1395 tb_offset=None, number_of_lines_of_context=5):
1396 1396 if etype is None:
1397 1397 etype, value, tb = sys.exc_info()
1398 1398 self.tb = tb
1399 1399 return FormattedTB.structured_traceback(
1400 1400 self, etype, value, tb, tb_offset, number_of_lines_of_context)
1401 1401
1402 1402
1403 1403 #---------------------------------------------------------------------------
1404 1404
1405 1405 # A simple class to preserve Nathan's original functionality.
1406 1406 class ColorTB(FormattedTB):
1407 1407 """Shorthand to initialize a FormattedTB in Linux colors mode."""
1408 1408
1409 1409 def __init__(self, color_scheme='Linux', call_pdb=0, **kwargs):
1410 1410 FormattedTB.__init__(self, color_scheme=color_scheme,
1411 1411 call_pdb=call_pdb, **kwargs)
1412 1412
1413 1413
1414 1414 class SyntaxTB(ListTB):
1415 1415 """Extension which holds some state: the last exception value"""
1416 1416
1417 1417 def __init__(self, color_scheme='NoColor', parent=None, config=None):
1418 1418 ListTB.__init__(self, color_scheme, parent=parent, config=config)
1419 1419 self.last_syntax_error = None
1420 1420
1421 1421 def __call__(self, etype, value, elist):
1422 1422 self.last_syntax_error = value
1423 1423
1424 1424 ListTB.__call__(self, etype, value, elist)
1425 1425
1426 1426 def structured_traceback(self, etype, value, elist, tb_offset=None,
1427 1427 context=5):
1428 1428 # If the source file has been edited, the line in the syntax error can
1429 1429 # be wrong (retrieved from an outdated cache). This replaces it with
1430 1430 # the current value.
1431 1431 if isinstance(value, SyntaxError) \
1432 and isinstance(value.filename, py3compat.string_types) \
1432 and isinstance(value.filename, str) \
1433 1433 and isinstance(value.lineno, int):
1434 1434 linecache.checkcache(value.filename)
1435 1435 newtext = ulinecache.getline(value.filename, value.lineno)
1436 1436 if newtext:
1437 1437 value.text = newtext
1438 1438 self.last_syntax_error = value
1439 1439 return super(SyntaxTB, self).structured_traceback(etype, value, elist,
1440 1440 tb_offset=tb_offset, context=context)
1441 1441
1442 1442 def clear_err_state(self):
1443 1443 """Return the current error state and clear it"""
1444 1444 e = self.last_syntax_error
1445 1445 self.last_syntax_error = None
1446 1446 return e
1447 1447
1448 1448 def stb2text(self, stb):
1449 1449 """Convert a structured traceback (a list) to a string."""
1450 1450 return ''.join(stb)
1451 1451
1452 1452
1453 1453 # some internal-use functions
1454 1454 def text_repr(value):
1455 1455 """Hopefully pretty robust repr equivalent."""
1456 1456 # this is pretty horrible but should always return *something*
1457 1457 try:
1458 1458 return pydoc.text.repr(value)
1459 1459 except KeyboardInterrupt:
1460 1460 raise
1461 1461 except:
1462 1462 try:
1463 1463 return repr(value)
1464 1464 except KeyboardInterrupt:
1465 1465 raise
1466 1466 except:
1467 1467 try:
1468 1468 # all still in an except block so we catch
1469 1469 # getattr raising
1470 1470 name = getattr(value, '__name__', None)
1471 1471 if name:
1472 1472 # ick, recursion
1473 1473 return text_repr(name)
1474 1474 klass = getattr(value, '__class__', None)
1475 1475 if klass:
1476 1476 return '%s instance' % text_repr(klass)
1477 1477 except KeyboardInterrupt:
1478 1478 raise
1479 1479 except:
1480 1480 return 'UNRECOVERABLE REPR FAILURE'
1481 1481
1482 1482
1483 1483 def eqrepr(value, repr=text_repr):
1484 1484 return '=%s' % repr(value)
1485 1485
1486 1486
1487 1487 def nullrepr(value, repr=text_repr):
1488 1488 return ''
@@ -1,227 +1,226 b''
1 1 # -*- coding: utf-8 -*-
2 2 """
3 3 %store magic for lightweight persistence.
4 4
5 5 Stores variables, aliases and macros in IPython's database.
6 6
7 7 To automatically restore stored variables at startup, add this to your
8 8 :file:`ipython_config.py` file::
9 9
10 10 c.StoreMagics.autorestore = True
11 11 """
12 12
13 13 # Copyright (c) IPython Development Team.
14 14 # Distributed under the terms of the Modified BSD License.
15 15
16 16 import inspect, os, sys, textwrap
17 17
18 18 from IPython.core.error import UsageError
19 19 from IPython.core.magic import Magics, magics_class, line_magic
20 20 from traitlets import Bool
21 from IPython.utils.py3compat import string_types
22 21
23 22
24 23 def restore_aliases(ip):
25 24 staliases = ip.db.get('stored_aliases', {})
26 25 for k,v in staliases.items():
27 26 #print "restore alias",k,v # dbg
28 27 #self.alias_table[k] = v
29 28 ip.alias_manager.define_alias(k,v)
30 29
31 30
32 31 def refresh_variables(ip):
33 32 db = ip.db
34 33 for key in db.keys('autorestore/*'):
35 34 # strip autorestore
36 35 justkey = os.path.basename(key)
37 36 try:
38 37 obj = db[key]
39 38 except KeyError:
40 39 print("Unable to restore variable '%s', ignoring (use %%store -d to forget!)" % justkey)
41 40 print("The error was:", sys.exc_info()[0])
42 41 else:
43 42 #print "restored",justkey,"=",obj #dbg
44 43 ip.user_ns[justkey] = obj
45 44
46 45
47 46 def restore_dhist(ip):
48 47 ip.user_ns['_dh'] = ip.db.get('dhist',[])
49 48
50 49
51 50 def restore_data(ip):
52 51 refresh_variables(ip)
53 52 restore_aliases(ip)
54 53 restore_dhist(ip)
55 54
56 55
57 56 @magics_class
58 57 class StoreMagics(Magics):
59 58 """Lightweight persistence for python variables.
60 59
61 60 Provides the %store magic."""
62 61
63 62 autorestore = Bool(False, help=
64 63 """If True, any %store-d variables will be automatically restored
65 64 when IPython starts.
66 65 """
67 66 ).tag(config=True)
68 67
69 68 def __init__(self, shell):
70 69 super(StoreMagics, self).__init__(shell=shell)
71 70 self.shell.configurables.append(self)
72 71 if self.autorestore:
73 72 restore_data(self.shell)
74 73
75 74 @line_magic
76 75 def store(self, parameter_s=''):
77 76 """Lightweight persistence for python variables.
78 77
79 78 Example::
80 79
81 80 In [1]: l = ['hello',10,'world']
82 81 In [2]: %store l
83 82 In [3]: exit
84 83
85 84 (IPython session is closed and started again...)
86 85
87 86 ville@badger:~$ ipython
88 87 In [1]: l
89 88 NameError: name 'l' is not defined
90 89 In [2]: %store -r
91 90 In [3]: l
92 91 Out[3]: ['hello', 10, 'world']
93 92
94 93 Usage:
95 94
96 95 * ``%store`` - Show list of all variables and their current
97 96 values
98 97 * ``%store spam`` - Store the *current* value of the variable spam
99 98 to disk
100 99 * ``%store -d spam`` - Remove the variable and its value from storage
101 100 * ``%store -z`` - Remove all variables from storage
102 101 * ``%store -r`` - Refresh all variables from store (overwrite
103 102 current vals)
104 103 * ``%store -r spam bar`` - Refresh specified variables from store
105 104 (delete current val)
106 105 * ``%store foo >a.txt`` - Store value of foo to new file a.txt
107 106 * ``%store foo >>a.txt`` - Append value of foo to file a.txt
108 107
109 108 It should be noted that if you change the value of a variable, you
110 109 need to %store it again if you want to persist the new value.
111 110
112 111 Note also that the variables will need to be pickleable; most basic
113 112 python types can be safely %store'd.
114 113
115 114 Also aliases can be %store'd across sessions.
116 115 """
117 116
118 117 opts,argsl = self.parse_options(parameter_s,'drz',mode='string')
119 118 args = argsl.split(None,1)
120 119 ip = self.shell
121 120 db = ip.db
122 121 # delete
123 122 if 'd' in opts:
124 123 try:
125 124 todel = args[0]
126 125 except IndexError:
127 126 raise UsageError('You must provide the variable to forget')
128 127 else:
129 128 try:
130 129 del db['autorestore/' + todel]
131 130 except:
132 131 raise UsageError("Can't delete variable '%s'" % todel)
133 132 # reset
134 133 elif 'z' in opts:
135 134 for k in db.keys('autorestore/*'):
136 135 del db[k]
137 136
138 137 elif 'r' in opts:
139 138 if args:
140 139 for arg in args:
141 140 try:
142 141 obj = db['autorestore/' + arg]
143 142 except KeyError:
144 143 print("no stored variable %s" % arg)
145 144 else:
146 145 ip.user_ns[arg] = obj
147 146 else:
148 147 restore_data(ip)
149 148
150 149 # run without arguments -> list variables & values
151 150 elif not args:
152 151 vars = db.keys('autorestore/*')
153 152 vars.sort()
154 153 if vars:
155 154 size = max(map(len, vars))
156 155 else:
157 156 size = 0
158 157
159 158 print('Stored variables and their in-db values:')
160 159 fmt = '%-'+str(size)+'s -> %s'
161 160 get = db.get
162 161 for var in vars:
163 162 justkey = os.path.basename(var)
164 163 # print 30 first characters from every var
165 164 print(fmt % (justkey, repr(get(var, '<unavailable>'))[:50]))
166 165
167 166 # default action - store the variable
168 167 else:
169 168 # %store foo >file.txt or >>file.txt
170 169 if len(args) > 1 and args[1].startswith('>'):
171 170 fnam = os.path.expanduser(args[1].lstrip('>').lstrip())
172 171 if args[1].startswith('>>'):
173 172 fil = open(fnam, 'a')
174 173 else:
175 174 fil = open(fnam, 'w')
176 175 obj = ip.ev(args[0])
177 176 print("Writing '%s' (%s) to file '%s'." % (args[0],
178 177 obj.__class__.__name__, fnam))
179 178
180 179
181 if not isinstance (obj, string_types):
180 if not isinstance (obj, str):
182 181 from pprint import pprint
183 182 pprint(obj, fil)
184 183 else:
185 184 fil.write(obj)
186 185 if not obj.endswith('\n'):
187 186 fil.write('\n')
188 187
189 188 fil.close()
190 189 return
191 190
192 191 # %store foo
193 192 try:
194 193 obj = ip.user_ns[args[0]]
195 194 except KeyError:
196 195 # it might be an alias
197 196 name = args[0]
198 197 try:
199 198 cmd = ip.alias_manager.retrieve_alias(name)
200 199 except ValueError:
201 200 raise UsageError("Unknown variable '%s'" % name)
202 201
203 202 staliases = db.get('stored_aliases',{})
204 203 staliases[name] = cmd
205 204 db['stored_aliases'] = staliases
206 205 print("Alias stored: %s (%s)" % (name, cmd))
207 206 return
208 207
209 208 else:
210 209 modname = getattr(inspect.getmodule(obj), '__name__', '')
211 210 if modname == '__main__':
212 211 print(textwrap.dedent("""\
213 212 Warning:%s is %s
214 213 Proper storage of interactively declared classes (or instances
215 214 of those classes) is not possible! Only instances
216 215 of classes in real modules on file system can be %%store'd.
217 216 """ % (args[0], obj) ))
218 217 return
219 218 #pickled = pickle.dumps(obj)
220 219 db[ 'autorestore/' + args[0] ] = obj
221 220 print("Stored '%s' (%s)" % (args[0], obj.__class__.__name__))
222 221
223 222
224 223 def load_ipython_extension(ip):
225 224 """Load the extension in IPython."""
226 225 ip.register_magics(StoreMagics)
227 226
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
General Comments 0
You need to be logged in to leave comments. Login now