##// END OF EJS Templates
Replace all import of IPython.utils.warn module
Pierre Gerold -
Show More
@@ -1,257 +1,257 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 30 from IPython.utils.py3compat import string_types
31 31 from traitlets import List, Instance
32 from IPython.utils.warn import error
32 from logging import error
33 33
34 34 #-----------------------------------------------------------------------------
35 35 # Utilities
36 36 #-----------------------------------------------------------------------------
37 37
38 38 # This is used as the pattern for calls to split_user_input.
39 39 shell_line_split = re.compile(r'^(\s*)()(\S+)(.*$)')
40 40
41 41 def default_aliases():
42 42 """Return list of shell aliases to auto-define.
43 43 """
44 44 # Note: the aliases defined here should be safe to use on a kernel
45 45 # regardless of what frontend it is attached to. Frontends that use a
46 46 # kernel in-process can define additional aliases that will only work in
47 47 # their case. For example, things like 'less' or 'clear' that manipulate
48 48 # the terminal should NOT be declared here, as they will only work if the
49 49 # kernel is running inside a true terminal, and not over the network.
50 50
51 51 if os.name == 'posix':
52 52 default_aliases = [('mkdir', 'mkdir'), ('rmdir', 'rmdir'),
53 53 ('mv', 'mv'), ('rm', 'rm'), ('cp', 'cp'),
54 54 ('cat', 'cat'),
55 55 ]
56 56 # Useful set of ls aliases. The GNU and BSD options are a little
57 57 # different, so we make aliases that provide as similar as possible
58 58 # behavior in ipython, by passing the right flags for each platform
59 59 if sys.platform.startswith('linux'):
60 60 ls_aliases = [('ls', 'ls -F --color'),
61 61 # long ls
62 62 ('ll', 'ls -F -o --color'),
63 63 # ls normal files only
64 64 ('lf', 'ls -F -o --color %l | grep ^-'),
65 65 # ls symbolic links
66 66 ('lk', 'ls -F -o --color %l | grep ^l'),
67 67 # directories or links to directories,
68 68 ('ldir', 'ls -F -o --color %l | grep /$'),
69 69 # things which are executable
70 70 ('lx', 'ls -F -o --color %l | grep ^-..x'),
71 71 ]
72 72 elif sys.platform.startswith('openbsd') or sys.platform.startswith('netbsd'):
73 73 # OpenBSD, NetBSD. The ls implementation on these platforms do not support
74 74 # the -G switch and lack the ability to use colorized output.
75 75 ls_aliases = [('ls', 'ls -F'),
76 76 # long ls
77 77 ('ll', 'ls -F -l'),
78 78 # ls normal files only
79 79 ('lf', 'ls -F -l %l | grep ^-'),
80 80 # ls symbolic links
81 81 ('lk', 'ls -F -l %l | grep ^l'),
82 82 # directories or links to directories,
83 83 ('ldir', 'ls -F -l %l | grep /$'),
84 84 # things which are executable
85 85 ('lx', 'ls -F -l %l | grep ^-..x'),
86 86 ]
87 87 else:
88 88 # BSD, OSX, etc.
89 89 ls_aliases = [('ls', 'ls -F -G'),
90 90 # long ls
91 91 ('ll', 'ls -F -l -G'),
92 92 # ls normal files only
93 93 ('lf', 'ls -F -l -G %l | grep ^-'),
94 94 # ls symbolic links
95 95 ('lk', 'ls -F -l -G %l | grep ^l'),
96 96 # directories or links to directories,
97 97 ('ldir', 'ls -F -G -l %l | grep /$'),
98 98 # things which are executable
99 99 ('lx', 'ls -F -l -G %l | grep ^-..x'),
100 100 ]
101 101 default_aliases = default_aliases + ls_aliases
102 102 elif os.name in ['nt', 'dos']:
103 103 default_aliases = [('ls', 'dir /on'),
104 104 ('ddir', 'dir /ad /on'), ('ldir', 'dir /ad /on'),
105 105 ('mkdir', 'mkdir'), ('rmdir', 'rmdir'),
106 106 ('echo', 'echo'), ('ren', 'ren'), ('copy', 'copy'),
107 107 ]
108 108 else:
109 109 default_aliases = []
110 110
111 111 return default_aliases
112 112
113 113
114 114 class AliasError(Exception):
115 115 pass
116 116
117 117
118 118 class InvalidAliasError(AliasError):
119 119 pass
120 120
121 121 class Alias(object):
122 122 """Callable object storing the details of one alias.
123 123
124 124 Instances are registered as magic functions to allow use of aliases.
125 125 """
126 126
127 127 # Prepare blacklist
128 128 blacklist = {'cd','popd','pushd','dhist','alias','unalias'}
129 129
130 130 def __init__(self, shell, name, cmd):
131 131 self.shell = shell
132 132 self.name = name
133 133 self.cmd = cmd
134 134 self.__doc__ = "Alias for `!{}`".format(cmd)
135 135 self.nargs = self.validate()
136 136
137 137 def validate(self):
138 138 """Validate the alias, and return the number of arguments."""
139 139 if self.name in self.blacklist:
140 140 raise InvalidAliasError("The name %s can't be aliased "
141 141 "because it is a keyword or builtin." % self.name)
142 142 try:
143 143 caller = self.shell.magics_manager.magics['line'][self.name]
144 144 except KeyError:
145 145 pass
146 146 else:
147 147 if not isinstance(caller, Alias):
148 148 raise InvalidAliasError("The name %s can't be aliased "
149 149 "because it is another magic command." % self.name)
150 150
151 151 if not (isinstance(self.cmd, string_types)):
152 152 raise InvalidAliasError("An alias command must be a string, "
153 153 "got: %r" % self.cmd)
154 154
155 155 nargs = self.cmd.count('%s') - self.cmd.count('%%s')
156 156
157 157 if (nargs > 0) and (self.cmd.find('%l') >= 0):
158 158 raise InvalidAliasError('The %s and %l specifiers are mutually '
159 159 'exclusive in alias definitions.')
160 160
161 161 return nargs
162 162
163 163 def __repr__(self):
164 164 return "<alias {} for {!r}>".format(self.name, self.cmd)
165 165
166 166 def __call__(self, rest=''):
167 167 cmd = self.cmd
168 168 nargs = self.nargs
169 169 # Expand the %l special to be the user's input line
170 170 if cmd.find('%l') >= 0:
171 171 cmd = cmd.replace('%l', rest)
172 172 rest = ''
173 173
174 174 if nargs==0:
175 175 if cmd.find('%%s') >= 1:
176 176 cmd = cmd.replace('%%s', '%s')
177 177 # Simple, argument-less aliases
178 178 cmd = '%s %s' % (cmd, rest)
179 179 else:
180 180 # Handle aliases with positional arguments
181 181 args = rest.split(None, nargs)
182 182 if len(args) < nargs:
183 183 raise UsageError('Alias <%s> requires %s arguments, %s given.' %
184 184 (self.name, nargs, len(args)))
185 185 cmd = '%s %s' % (cmd % tuple(args[:nargs]),' '.join(args[nargs:]))
186 186
187 187 self.shell.system(cmd)
188 188
189 189 #-----------------------------------------------------------------------------
190 190 # Main AliasManager class
191 191 #-----------------------------------------------------------------------------
192 192
193 193 class AliasManager(Configurable):
194 194
195 195 default_aliases = List(default_aliases(), config=True)
196 196 user_aliases = List(default_value=[], config=True)
197 197 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC', allow_none=True)
198 198
199 199 def __init__(self, shell=None, **kwargs):
200 200 super(AliasManager, self).__init__(shell=shell, **kwargs)
201 201 # For convenient access
202 202 self.linemagics = self.shell.magics_manager.magics['line']
203 203 self.init_aliases()
204 204
205 205 def init_aliases(self):
206 206 # Load default & user aliases
207 207 for name, cmd in self.default_aliases + self.user_aliases:
208 208 self.soft_define_alias(name, cmd)
209 209
210 210 @property
211 211 def aliases(self):
212 212 return [(n, func.cmd) for (n, func) in self.linemagics.items()
213 213 if isinstance(func, Alias)]
214 214
215 215 def soft_define_alias(self, name, cmd):
216 216 """Define an alias, but don't raise on an AliasError."""
217 217 try:
218 218 self.define_alias(name, cmd)
219 219 except AliasError as e:
220 220 error("Invalid alias: %s" % e)
221 221
222 222 def define_alias(self, name, cmd):
223 223 """Define a new alias after validating it.
224 224
225 225 This will raise an :exc:`AliasError` if there are validation
226 226 problems.
227 227 """
228 228 caller = Alias(shell=self.shell, name=name, cmd=cmd)
229 229 self.shell.magics_manager.register_function(caller, magic_kind='line',
230 230 magic_name=name)
231 231
232 232 def get_alias(self, name):
233 233 """Return an alias, or None if no alias by that name exists."""
234 234 aname = self.linemagics.get(name, None)
235 235 return aname if isinstance(aname, Alias) else None
236 236
237 237 def is_alias(self, name):
238 238 """Return whether or not a given name has been defined as an alias"""
239 239 return self.get_alias(name) is not None
240 240
241 241 def undefine_alias(self, name):
242 242 if self.is_alias(name):
243 243 del self.linemagics[name]
244 244 else:
245 245 raise ValueError('%s is not an alias' % name)
246 246
247 247 def clear_aliases(self):
248 248 for name, cmd in self.aliases:
249 249 self.undefine_alias(name)
250 250
251 251 def retrieve_alias(self, name):
252 252 """Retrieve the command to which an alias expands."""
253 253 caller = self.get_alias(name)
254 254 if caller:
255 255 return caller.cmd
256 256 else:
257 257 raise ValueError('%s is not an alias' % name)
@@ -1,296 +1,295 b''
1 1 # -*- coding: utf-8 -*-
2 2 """Displayhook for IPython.
3 3
4 4 This defines a callable class that IPython uses for `sys.displayhook`.
5 5 """
6 6
7 7 # Copyright (c) IPython Development Team.
8 8 # Distributed under the terms of the Modified BSD License.
9 9
10 10 from __future__ import print_function
11 11
12 12 import sys
13 13 import io as _io
14 14 import tokenize
15 15
16 16 from IPython.core.formatters import _safe_get_formatter_method
17 17 from traitlets.config.configurable import Configurable
18 18 from IPython.utils import io
19 19 from IPython.utils.py3compat import builtin_mod, cast_unicode_py2
20 20 from traitlets import Instance, Float
21 from IPython.utils.warn import warn
21 from warnings import warn
22 22
23 23 # TODO: Move the various attributes (cache_size, [others now moved]). Some
24 24 # of these are also attributes of InteractiveShell. They should be on ONE object
25 25 # only and the other objects should ask that one object for their values.
26 26
27 27 class DisplayHook(Configurable):
28 28 """The custom IPython displayhook to replace sys.displayhook.
29 29
30 30 This class does many things, but the basic idea is that it is a callable
31 31 that gets called anytime user code returns a value.
32 32 """
33 33
34 34 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC',
35 35 allow_none=True)
36 36 exec_result = Instance('IPython.core.interactiveshell.ExecutionResult',
37 37 allow_none=True)
38 38 cull_fraction = Float(0.2)
39 39
40 40 def __init__(self, shell=None, cache_size=1000, **kwargs):
41 41 super(DisplayHook, self).__init__(shell=shell, **kwargs)
42 42 cache_size_min = 3
43 43 if cache_size <= 0:
44 44 self.do_full_cache = 0
45 45 cache_size = 0
46 46 elif cache_size < cache_size_min:
47 47 self.do_full_cache = 0
48 48 cache_size = 0
49 49 warn('caching was disabled (min value for cache size is %s).' %
50 50 cache_size_min,level=3)
51 51 else:
52 52 self.do_full_cache = 1
53 53
54 54 self.cache_size = cache_size
55 55
56 56 # we need a reference to the user-level namespace
57 57 self.shell = shell
58 58
59 59 self._,self.__,self.___ = '','',''
60 60
61 61 # these are deliberately global:
62 62 to_user_ns = {'_':self._,'__':self.__,'___':self.___}
63 63 self.shell.user_ns.update(to_user_ns)
64 64
65 65 @property
66 66 def prompt_count(self):
67 67 return self.shell.execution_count
68 68
69 69 #-------------------------------------------------------------------------
70 70 # Methods used in __call__. Override these methods to modify the behavior
71 71 # of the displayhook.
72 72 #-------------------------------------------------------------------------
73 73
74 74 def check_for_underscore(self):
75 75 """Check if the user has set the '_' variable by hand."""
76 76 # If something injected a '_' variable in __builtin__, delete
77 77 # ipython's automatic one so we don't clobber that. gettext() in
78 78 # particular uses _, so we need to stay away from it.
79 79 if '_' in builtin_mod.__dict__:
80 80 try:
81 81 del self.shell.user_ns['_']
82 82 except KeyError:
83 83 pass
84 84
85 85 def quiet(self):
86 86 """Should we silence the display hook because of ';'?"""
87 87 # do not print output if input ends in ';'
88 88
89 89 try:
90 90 cell = cast_unicode_py2(self.shell.history_manager.input_hist_parsed[-1])
91 91 except IndexError:
92 92 # some uses of ipshellembed may fail here
93 93 return False
94 94
95 95 sio = _io.StringIO(cell)
96 96 tokens = list(tokenize.generate_tokens(sio.readline))
97 97
98 98 for token in reversed(tokens):
99 99 if token[0] in (tokenize.ENDMARKER, tokenize.NL, tokenize.NEWLINE, tokenize.COMMENT):
100 100 continue
101 101 if (token[0] == tokenize.OP) and (token[1] == ';'):
102 102 return True
103 103 else:
104 104 return False
105 105
106 106 def start_displayhook(self):
107 107 """Start the displayhook, initializing resources."""
108 108 pass
109 109
110 110 def write_output_prompt(self):
111 111 """Write the output prompt.
112 112
113 113 The default implementation simply writes the prompt to
114 114 ``io.stdout``.
115 115 """
116 116 # Use write, not print which adds an extra space.
117 117 io.stdout.write(self.shell.separate_out)
118 118 outprompt = self.shell.prompt_manager.render('out')
119 119 if self.do_full_cache:
120 120 io.stdout.write(outprompt)
121 121
122 122 def compute_format_data(self, result):
123 123 """Compute format data of the object to be displayed.
124 124
125 125 The format data is a generalization of the :func:`repr` of an object.
126 126 In the default implementation the format data is a :class:`dict` of
127 127 key value pair where the keys are valid MIME types and the values
128 128 are JSON'able data structure containing the raw data for that MIME
129 129 type. It is up to frontends to determine pick a MIME to to use and
130 130 display that data in an appropriate manner.
131 131
132 132 This method only computes the format data for the object and should
133 133 NOT actually print or write that to a stream.
134 134
135 135 Parameters
136 136 ----------
137 137 result : object
138 138 The Python object passed to the display hook, whose format will be
139 139 computed.
140 140
141 141 Returns
142 142 -------
143 143 (format_dict, md_dict) : dict
144 144 format_dict is a :class:`dict` whose keys are valid MIME types and values are
145 145 JSON'able raw data for that MIME type. It is recommended that
146 146 all return values of this should always include the "text/plain"
147 147 MIME type representation of the object.
148 148 md_dict is a :class:`dict` with the same MIME type keys
149 149 of metadata associated with each output.
150 150
151 151 """
152 152 return self.shell.display_formatter.format(result)
153 153
154 154 def write_format_data(self, format_dict, md_dict=None):
155 155 """Write the format data dict to the frontend.
156 156
157 157 This default version of this method simply writes the plain text
158 158 representation of the object to ``io.stdout``. Subclasses should
159 159 override this method to send the entire `format_dict` to the
160 160 frontends.
161 161
162 162 Parameters
163 163 ----------
164 164 format_dict : dict
165 165 The format dict for the object passed to `sys.displayhook`.
166 166 md_dict : dict (optional)
167 167 The metadata dict to be associated with the display data.
168 168 """
169 169 if 'text/plain' not in format_dict:
170 170 # nothing to do
171 171 return
172 172 # We want to print because we want to always make sure we have a
173 173 # newline, even if all the prompt separators are ''. This is the
174 174 # standard IPython behavior.
175 175 result_repr = format_dict['text/plain']
176 176 if '\n' in result_repr:
177 177 # So that multi-line strings line up with the left column of
178 178 # the screen, instead of having the output prompt mess up
179 179 # their first line.
180 180 # We use the prompt template instead of the expanded prompt
181 181 # because the expansion may add ANSI escapes that will interfere
182 182 # with our ability to determine whether or not we should add
183 183 # a newline.
184 184 prompt_template = self.shell.prompt_manager.out_template
185 185 if prompt_template and not prompt_template.endswith('\n'):
186 186 # But avoid extraneous empty lines.
187 187 result_repr = '\n' + result_repr
188 188
189 189 print(result_repr, file=io.stdout)
190 190
191 191 def update_user_ns(self, result):
192 192 """Update user_ns with various things like _, __, _1, etc."""
193 193
194 194 # Avoid recursive reference when displaying _oh/Out
195 195 if result is not self.shell.user_ns['_oh']:
196 196 if len(self.shell.user_ns['_oh']) >= self.cache_size and self.do_full_cache:
197 197 self.cull_cache()
198 198 # Don't overwrite '_' and friends if '_' is in __builtin__ (otherwise
199 199 # we cause buggy behavior for things like gettext).
200 200
201 201 if '_' not in builtin_mod.__dict__:
202 202 self.___ = self.__
203 203 self.__ = self._
204 204 self._ = result
205 205 self.shell.push({'_':self._,
206 206 '__':self.__,
207 207 '___':self.___}, interactive=False)
208 208
209 209 # hackish access to top-level namespace to create _1,_2... dynamically
210 210 to_main = {}
211 211 if self.do_full_cache:
212 212 new_result = '_'+repr(self.prompt_count)
213 213 to_main[new_result] = result
214 214 self.shell.push(to_main, interactive=False)
215 215 self.shell.user_ns['_oh'][self.prompt_count] = result
216 216
217 217 def fill_exec_result(self, result):
218 218 if self.exec_result is not None:
219 219 self.exec_result.result = result
220 220
221 221 def log_output(self, format_dict):
222 222 """Log the output."""
223 223 if 'text/plain' not in format_dict:
224 224 # nothing to do
225 225 return
226 226 if self.shell.logger.log_output:
227 227 self.shell.logger.log_write(format_dict['text/plain'], 'output')
228 228 self.shell.history_manager.output_hist_reprs[self.prompt_count] = \
229 229 format_dict['text/plain']
230 230
231 231 def finish_displayhook(self):
232 232 """Finish up all displayhook activities."""
233 233 io.stdout.write(self.shell.separate_out2)
234 234 io.stdout.flush()
235 235
236 236 def __call__(self, result=None):
237 237 """Printing with history cache management.
238 238
239 239 This is invoked everytime the interpreter needs to print, and is
240 240 activated by setting the variable sys.displayhook to it.
241 241 """
242 242 self.check_for_underscore()
243 243 if result is not None and not self.quiet():
244 244 self.start_displayhook()
245 245 self.write_output_prompt()
246 246 format_dict, md_dict = self.compute_format_data(result)
247 247 self.update_user_ns(result)
248 248 self.fill_exec_result(result)
249 249 if format_dict:
250 250 self.write_format_data(format_dict, md_dict)
251 251 self.log_output(format_dict)
252 252 self.finish_displayhook()
253 253
254 254 def cull_cache(self):
255 255 """Output cache is full, cull the oldest entries"""
256 256 oh = self.shell.user_ns.get('_oh', {})
257 257 sz = len(oh)
258 258 cull_count = max(int(sz * self.cull_fraction), 2)
259 259 warn('Output cache limit (currently {sz} entries) hit.\n'
260 260 'Flushing oldest {cull_count} entries.'.format(sz=sz, cull_count=cull_count))
261 261
262 262 for i, n in enumerate(sorted(oh)):
263 263 if i >= cull_count:
264 264 break
265 265 self.shell.user_ns.pop('_%i' % n, None)
266 266 oh.pop(n, None)
267 267
268 268
269 269 def flush(self):
270 270 if not self.do_full_cache:
271 271 raise ValueError("You shouldn't have reached the cache flush "
272 272 "if full caching is not enabled!")
273 273 # delete auto-generated vars from global namespace
274 274
275 275 for n in range(1,self.prompt_count + 1):
276 276 key = '_'+repr(n)
277 277 try:
278 278 del self.shell.user_ns[key]
279 279 except: pass
280 280 # In some embedded circumstances, the user_ns doesn't have the
281 281 # '_oh' key set up.
282 282 oh = self.shell.user_ns.get('_oh', None)
283 283 if oh is not None:
284 284 oh.clear()
285 285
286 286 # Release our own references to objects:
287 287 self._, self.__, self.___ = '', '', ''
288 288
289 289 if '_' not in builtin_mod.__dict__:
290 290 self.shell.user_ns.update({'_':None,'__':None, '___':None})
291 291 import gc
292 292 # TODO: Is this really needed?
293 293 # IronPython blocks here forever
294 294 if sys.platform != "cli":
295 295 gc.collect()
296
@@ -1,883 +1,881 b''
1 1 """ History related magics and functionality """
2 2 #-----------------------------------------------------------------------------
3 3 # Copyright (C) 2010-2011 The IPython Development Team.
4 4 #
5 5 # Distributed under the terms of the BSD License.
6 6 #
7 7 # The full license is in the file COPYING.txt, distributed with this software.
8 8 #-----------------------------------------------------------------------------
9 9
10 10 #-----------------------------------------------------------------------------
11 11 # Imports
12 12 #-----------------------------------------------------------------------------
13 13 from __future__ import print_function
14 14
15 15 # Stdlib imports
16 16 import atexit
17 17 import datetime
18 18 import os
19 19 import re
20 20 try:
21 21 import sqlite3
22 22 except ImportError:
23 23 try:
24 24 from pysqlite2 import dbapi2 as sqlite3
25 25 except ImportError:
26 26 sqlite3 = None
27 27 import threading
28 28
29 29 # Our own packages
30 30 from traitlets.config.configurable import Configurable
31 31 from decorator import decorator
32 32 from IPython.utils.decorators import undoc
33 33 from IPython.utils.path import locate_profile
34 34 from IPython.utils import py3compat
35 35 from traitlets import (
36 36 Any, Bool, Dict, Instance, Integer, List, Unicode, TraitError,
37 37 )
38 from IPython.utils.warn import warn
38 from warnings import warn
39 39
40 40 #-----------------------------------------------------------------------------
41 41 # Classes and functions
42 42 #-----------------------------------------------------------------------------
43 43
44 44 @undoc
45 45 class DummyDB(object):
46 46 """Dummy DB that will act as a black hole for history.
47 47
48 48 Only used in the absence of sqlite"""
49 49 def execute(*args, **kwargs):
50 50 return []
51 51
52 52 def commit(self, *args, **kwargs):
53 53 pass
54 54
55 55 def __enter__(self, *args, **kwargs):
56 56 pass
57 57
58 58 def __exit__(self, *args, **kwargs):
59 59 pass
60 60
61 61
62 62 @decorator
63 63 def needs_sqlite(f, self, *a, **kw):
64 64 """Decorator: return an empty list in the absence of sqlite."""
65 65 if sqlite3 is None or not self.enabled:
66 66 return []
67 67 else:
68 68 return f(self, *a, **kw)
69 69
70 70
71 71 if sqlite3 is not None:
72 72 DatabaseError = sqlite3.DatabaseError
73 73 OperationalError = sqlite3.OperationalError
74 74 else:
75 75 @undoc
76 76 class DatabaseError(Exception):
77 77 "Dummy exception when sqlite could not be imported. Should never occur."
78 78
79 79 @undoc
80 80 class OperationalError(Exception):
81 81 "Dummy exception when sqlite could not be imported. Should never occur."
82 82
83 83 @decorator
84 84 def catch_corrupt_db(f, self, *a, **kw):
85 85 """A decorator which wraps HistoryAccessor method calls to catch errors from
86 86 a corrupt SQLite database, move the old database out of the way, and create
87 87 a new one.
88 88 """
89 89 try:
90 90 return f(self, *a, **kw)
91 91 except (DatabaseError, OperationalError):
92 92 if os.path.isfile(self.hist_file):
93 93 # Try to move the file out of the way
94 94 base,ext = os.path.splitext(self.hist_file)
95 95 newpath = base + '-corrupt' + ext
96 96 os.rename(self.hist_file, newpath)
97 97 self.init_db()
98 98 print("ERROR! History file wasn't a valid SQLite database.",
99 99 "It was moved to %s" % newpath, "and a new file created.")
100 100 return []
101 101
102 102 else:
103 103 # The hist_file is probably :memory: or something else.
104 104 raise
105 105
106 106 class HistoryAccessorBase(Configurable):
107 107 """An abstract class for History Accessors """
108 108
109 109 def get_tail(self, n=10, raw=True, output=False, include_latest=False):
110 110 raise NotImplementedError
111 111
112 112 def search(self, pattern="*", raw=True, search_raw=True,
113 113 output=False, n=None, unique=False):
114 114 raise NotImplementedError
115 115
116 116 def get_range(self, session, start=1, stop=None, raw=True,output=False):
117 117 raise NotImplementedError
118 118
119 119 def get_range_by_str(self, rangestr, raw=True, output=False):
120 120 raise NotImplementedError
121 121
122 122
123 123 class HistoryAccessor(HistoryAccessorBase):
124 124 """Access the history database without adding to it.
125 125
126 126 This is intended for use by standalone history tools. IPython shells use
127 127 HistoryManager, below, which is a subclass of this."""
128 128
129 129 # String holding the path to the history file
130 130 hist_file = Unicode(config=True,
131 131 help="""Path to file to use for SQLite history database.
132 132
133 133 By default, IPython will put the history database in the IPython
134 134 profile directory. If you would rather share one history among
135 135 profiles, you can set this value in each, so that they are consistent.
136 136
137 137 Due to an issue with fcntl, SQLite is known to misbehave on some NFS
138 138 mounts. If you see IPython hanging, try setting this to something on a
139 139 local disk, e.g::
140 140
141 141 ipython --HistoryManager.hist_file=/tmp/ipython_hist.sqlite
142 142
143 143 """)
144 144
145 145 enabled = Bool(True, config=True,
146 146 help="""enable the SQLite history
147 147
148 148 set enabled=False to disable the SQLite history,
149 149 in which case there will be no stored history, no SQLite connection,
150 150 and no background saving thread. This may be necessary in some
151 151 threaded environments where IPython is embedded.
152 152 """
153 153 )
154 154
155 155 connection_options = Dict(config=True,
156 156 help="""Options for configuring the SQLite connection
157 157
158 158 These options are passed as keyword args to sqlite3.connect
159 159 when establishing database conenctions.
160 160 """
161 161 )
162 162
163 163 # The SQLite database
164 164 db = Any()
165 165 def _db_changed(self, name, old, new):
166 166 """validate the db, since it can be an Instance of two different types"""
167 167 connection_types = (DummyDB,)
168 168 if sqlite3 is not None:
169 169 connection_types = (DummyDB, sqlite3.Connection)
170 170 if not isinstance(new, connection_types):
171 171 msg = "%s.db must be sqlite3 Connection or DummyDB, not %r" % \
172 172 (self.__class__.__name__, new)
173 173 raise TraitError(msg)
174 174
175 175 def __init__(self, profile='default', hist_file=u'', **traits):
176 176 """Create a new history accessor.
177 177
178 178 Parameters
179 179 ----------
180 180 profile : str
181 181 The name of the profile from which to open history.
182 182 hist_file : str
183 183 Path to an SQLite history database stored by IPython. If specified,
184 184 hist_file overrides profile.
185 185 config : :class:`~traitlets.config.loader.Config`
186 186 Config object. hist_file can also be set through this.
187 187 """
188 188 # We need a pointer back to the shell for various tasks.
189 189 super(HistoryAccessor, self).__init__(**traits)
190 190 # defer setting hist_file from kwarg until after init,
191 191 # otherwise the default kwarg value would clobber any value
192 192 # set by config
193 193 if hist_file:
194 194 self.hist_file = hist_file
195 195
196 196 if self.hist_file == u'':
197 197 # No one has set the hist_file, yet.
198 198 self.hist_file = self._get_hist_file_name(profile)
199 199
200 200 if sqlite3 is None and self.enabled:
201 201 warn("IPython History requires SQLite, your history will not be saved")
202 202 self.enabled = False
203 203
204 204 self.init_db()
205 205
206 206 def _get_hist_file_name(self, profile='default'):
207 207 """Find the history file for the given profile name.
208 208
209 209 This is overridden by the HistoryManager subclass, to use the shell's
210 210 active profile.
211 211
212 212 Parameters
213 213 ----------
214 214 profile : str
215 215 The name of a profile which has a history file.
216 216 """
217 217 return os.path.join(locate_profile(profile), 'history.sqlite')
218 218
219 219 @catch_corrupt_db
220 220 def init_db(self):
221 221 """Connect to the database, and create tables if necessary."""
222 222 if not self.enabled:
223 223 self.db = DummyDB()
224 224 return
225 225
226 226 # use detect_types so that timestamps return datetime objects
227 227 kwargs = dict(detect_types=sqlite3.PARSE_DECLTYPES|sqlite3.PARSE_COLNAMES)
228 228 kwargs.update(self.connection_options)
229 229 self.db = sqlite3.connect(self.hist_file, **kwargs)
230 230 self.db.execute("""CREATE TABLE IF NOT EXISTS sessions (session integer
231 231 primary key autoincrement, start timestamp,
232 232 end timestamp, num_cmds integer, remark text)""")
233 233 self.db.execute("""CREATE TABLE IF NOT EXISTS history
234 234 (session integer, line integer, source text, source_raw text,
235 235 PRIMARY KEY (session, line))""")
236 236 # Output history is optional, but ensure the table's there so it can be
237 237 # enabled later.
238 238 self.db.execute("""CREATE TABLE IF NOT EXISTS output_history
239 239 (session integer, line integer, output text,
240 240 PRIMARY KEY (session, line))""")
241 241 self.db.commit()
242 242
243 243 def writeout_cache(self):
244 244 """Overridden by HistoryManager to dump the cache before certain
245 245 database lookups."""
246 246 pass
247 247
248 248 ## -------------------------------
249 249 ## Methods for retrieving history:
250 250 ## -------------------------------
251 251 def _run_sql(self, sql, params, raw=True, output=False):
252 252 """Prepares and runs an SQL query for the history database.
253 253
254 254 Parameters
255 255 ----------
256 256 sql : str
257 257 Any filtering expressions to go after SELECT ... FROM ...
258 258 params : tuple
259 259 Parameters passed to the SQL query (to replace "?")
260 260 raw, output : bool
261 261 See :meth:`get_range`
262 262
263 263 Returns
264 264 -------
265 265 Tuples as :meth:`get_range`
266 266 """
267 267 toget = 'source_raw' if raw else 'source'
268 268 sqlfrom = "history"
269 269 if output:
270 270 sqlfrom = "history LEFT JOIN output_history USING (session, line)"
271 271 toget = "history.%s, output_history.output" % toget
272 272 cur = self.db.execute("SELECT session, line, %s FROM %s " %\
273 273 (toget, sqlfrom) + sql, params)
274 274 if output: # Regroup into 3-tuples, and parse JSON
275 275 return ((ses, lin, (inp, out)) for ses, lin, inp, out in cur)
276 276 return cur
277 277
278 278 @needs_sqlite
279 279 @catch_corrupt_db
280 280 def get_session_info(self, session):
281 281 """Get info about a session.
282 282
283 283 Parameters
284 284 ----------
285 285
286 286 session : int
287 287 Session number to retrieve.
288 288
289 289 Returns
290 290 -------
291 291
292 292 session_id : int
293 293 Session ID number
294 294 start : datetime
295 295 Timestamp for the start of the session.
296 296 end : datetime
297 297 Timestamp for the end of the session, or None if IPython crashed.
298 298 num_cmds : int
299 299 Number of commands run, or None if IPython crashed.
300 300 remark : unicode
301 301 A manually set description.
302 302 """
303 303 query = "SELECT * from sessions where session == ?"
304 304 return self.db.execute(query, (session,)).fetchone()
305 305
306 306 @catch_corrupt_db
307 307 def get_last_session_id(self):
308 308 """Get the last session ID currently in the database.
309 309
310 310 Within IPython, this should be the same as the value stored in
311 311 :attr:`HistoryManager.session_number`.
312 312 """
313 313 for record in self.get_tail(n=1, include_latest=True):
314 314 return record[0]
315 315
316 316 @catch_corrupt_db
317 317 def get_tail(self, n=10, raw=True, output=False, include_latest=False):
318 318 """Get the last n lines from the history database.
319 319
320 320 Parameters
321 321 ----------
322 322 n : int
323 323 The number of lines to get
324 324 raw, output : bool
325 325 See :meth:`get_range`
326 326 include_latest : bool
327 327 If False (default), n+1 lines are fetched, and the latest one
328 328 is discarded. This is intended to be used where the function
329 329 is called by a user command, which it should not return.
330 330
331 331 Returns
332 332 -------
333 333 Tuples as :meth:`get_range`
334 334 """
335 335 self.writeout_cache()
336 336 if not include_latest:
337 337 n += 1
338 338 cur = self._run_sql("ORDER BY session DESC, line DESC LIMIT ?",
339 339 (n,), raw=raw, output=output)
340 340 if not include_latest:
341 341 return reversed(list(cur)[1:])
342 342 return reversed(list(cur))
343 343
344 344 @catch_corrupt_db
345 345 def search(self, pattern="*", raw=True, search_raw=True,
346 346 output=False, n=None, unique=False):
347 347 """Search the database using unix glob-style matching (wildcards
348 348 * and ?).
349 349
350 350 Parameters
351 351 ----------
352 352 pattern : str
353 353 The wildcarded pattern to match when searching
354 354 search_raw : bool
355 355 If True, search the raw input, otherwise, the parsed input
356 356 raw, output : bool
357 357 See :meth:`get_range`
358 358 n : None or int
359 359 If an integer is given, it defines the limit of
360 360 returned entries.
361 361 unique : bool
362 362 When it is true, return only unique entries.
363 363
364 364 Returns
365 365 -------
366 366 Tuples as :meth:`get_range`
367 367 """
368 368 tosearch = "source_raw" if search_raw else "source"
369 369 if output:
370 370 tosearch = "history." + tosearch
371 371 self.writeout_cache()
372 372 sqlform = "WHERE %s GLOB ?" % tosearch
373 373 params = (pattern,)
374 374 if unique:
375 375 sqlform += ' GROUP BY {0}'.format(tosearch)
376 376 if n is not None:
377 377 sqlform += " ORDER BY session DESC, line DESC LIMIT ?"
378 378 params += (n,)
379 379 elif unique:
380 380 sqlform += " ORDER BY session, line"
381 381 cur = self._run_sql(sqlform, params, raw=raw, output=output)
382 382 if n is not None:
383 383 return reversed(list(cur))
384 384 return cur
385 385
386 386 @catch_corrupt_db
387 387 def get_range(self, session, start=1, stop=None, raw=True,output=False):
388 388 """Retrieve input by session.
389 389
390 390 Parameters
391 391 ----------
392 392 session : int
393 393 Session number to retrieve.
394 394 start : int
395 395 First line to retrieve.
396 396 stop : int
397 397 End of line range (excluded from output itself). If None, retrieve
398 398 to the end of the session.
399 399 raw : bool
400 400 If True, return untranslated input
401 401 output : bool
402 402 If True, attempt to include output. This will be 'real' Python
403 403 objects for the current session, or text reprs from previous
404 404 sessions if db_log_output was enabled at the time. Where no output
405 405 is found, None is used.
406 406
407 407 Returns
408 408 -------
409 409 entries
410 410 An iterator over the desired lines. Each line is a 3-tuple, either
411 411 (session, line, input) if output is False, or
412 412 (session, line, (input, output)) if output is True.
413 413 """
414 414 if stop:
415 415 lineclause = "line >= ? AND line < ?"
416 416 params = (session, start, stop)
417 417 else:
418 418 lineclause = "line>=?"
419 419 params = (session, start)
420 420
421 421 return self._run_sql("WHERE session==? AND %s" % lineclause,
422 422 params, raw=raw, output=output)
423 423
424 424 def get_range_by_str(self, rangestr, raw=True, output=False):
425 425 """Get lines of history from a string of ranges, as used by magic
426 426 commands %hist, %save, %macro, etc.
427 427
428 428 Parameters
429 429 ----------
430 430 rangestr : str
431 431 A string specifying ranges, e.g. "5 ~2/1-4". See
432 432 :func:`magic_history` for full details.
433 433 raw, output : bool
434 434 As :meth:`get_range`
435 435
436 436 Returns
437 437 -------
438 438 Tuples as :meth:`get_range`
439 439 """
440 440 for sess, s, e in extract_hist_ranges(rangestr):
441 441 for line in self.get_range(sess, s, e, raw=raw, output=output):
442 442 yield line
443 443
444 444
445 445 class HistoryManager(HistoryAccessor):
446 446 """A class to organize all history-related functionality in one place.
447 447 """
448 448 # Public interface
449 449
450 450 # An instance of the IPython shell we are attached to
451 451 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC',
452 452 allow_none=True)
453 453 # Lists to hold processed and raw history. These start with a blank entry
454 454 # so that we can index them starting from 1
455 455 input_hist_parsed = List([""])
456 456 input_hist_raw = List([""])
457 457 # A list of directories visited during session
458 458 dir_hist = List()
459 459 def _dir_hist_default(self):
460 460 try:
461 461 return [py3compat.getcwd()]
462 462 except OSError:
463 463 return []
464 464
465 465 # A dict of output history, keyed with ints from the shell's
466 466 # execution count.
467 467 output_hist = Dict()
468 468 # The text/plain repr of outputs.
469 469 output_hist_reprs = Dict()
470 470
471 471 # The number of the current session in the history database
472 472 session_number = Integer()
473 473
474 474 db_log_output = Bool(False, config=True,
475 475 help="Should the history database include output? (default: no)"
476 476 )
477 477 db_cache_size = Integer(0, config=True,
478 478 help="Write to database every x commands (higher values save disk access & power).\n"
479 479 "Values of 1 or less effectively disable caching."
480 480 )
481 481 # The input and output caches
482 482 db_input_cache = List()
483 483 db_output_cache = List()
484 484
485 485 # History saving in separate thread
486 486 save_thread = Instance('IPython.core.history.HistorySavingThread',
487 487 allow_none=True)
488 488 try: # Event is a function returning an instance of _Event...
489 489 save_flag = Instance(threading._Event, allow_none=True)
490 490 except AttributeError: # ...until Python 3.3, when it's a class.
491 491 save_flag = Instance(threading.Event, allow_none=True)
492 492
493 493 # Private interface
494 494 # Variables used to store the three last inputs from the user. On each new
495 495 # history update, we populate the user's namespace with these, shifted as
496 496 # necessary.
497 497 _i00 = Unicode(u'')
498 498 _i = Unicode(u'')
499 499 _ii = Unicode(u'')
500 500 _iii = Unicode(u'')
501 501
502 502 # A regex matching all forms of the exit command, so that we don't store
503 503 # them in the history (it's annoying to rewind the first entry and land on
504 504 # an exit call).
505 505 _exit_re = re.compile(r"(exit|quit)(\s*\(.*\))?$")
506 506
507 507 def __init__(self, shell=None, config=None, **traits):
508 508 """Create a new history manager associated with a shell instance.
509 509 """
510 510 # We need a pointer back to the shell for various tasks.
511 511 super(HistoryManager, self).__init__(shell=shell, config=config,
512 512 **traits)
513 513 self.save_flag = threading.Event()
514 514 self.db_input_cache_lock = threading.Lock()
515 515 self.db_output_cache_lock = threading.Lock()
516 516
517 517 try:
518 518 self.new_session()
519 519 except OperationalError:
520 520 self.log.error("Failed to create history session in %s. History will not be saved.",
521 521 self.hist_file, exc_info=True)
522 522 self.hist_file = ':memory:'
523 523
524 524 if self.enabled and self.hist_file != ':memory:':
525 525 self.save_thread = HistorySavingThread(self)
526 526 self.save_thread.start()
527 527
528 528 def _get_hist_file_name(self, profile=None):
529 529 """Get default history file name based on the Shell's profile.
530 530
531 531 The profile parameter is ignored, but must exist for compatibility with
532 532 the parent class."""
533 533 profile_dir = self.shell.profile_dir.location
534 534 return os.path.join(profile_dir, 'history.sqlite')
535 535
536 536 @needs_sqlite
537 537 def new_session(self, conn=None):
538 538 """Get a new session number."""
539 539 if conn is None:
540 540 conn = self.db
541 541
542 542 with conn:
543 543 cur = conn.execute("""INSERT INTO sessions VALUES (NULL, ?, NULL,
544 544 NULL, "") """, (datetime.datetime.now(),))
545 545 self.session_number = cur.lastrowid
546 546
547 547 def end_session(self):
548 548 """Close the database session, filling in the end time and line count."""
549 549 self.writeout_cache()
550 550 with self.db:
551 551 self.db.execute("""UPDATE sessions SET end=?, num_cmds=? WHERE
552 552 session==?""", (datetime.datetime.now(),
553 553 len(self.input_hist_parsed)-1, self.session_number))
554 554 self.session_number = 0
555 555
556 556 def name_session(self, name):
557 557 """Give the current session a name in the history database."""
558 558 with self.db:
559 559 self.db.execute("UPDATE sessions SET remark=? WHERE session==?",
560 560 (name, self.session_number))
561 561
562 562 def reset(self, new_session=True):
563 563 """Clear the session history, releasing all object references, and
564 564 optionally open a new session."""
565 565 self.output_hist.clear()
566 566 # The directory history can't be completely empty
567 567 self.dir_hist[:] = [py3compat.getcwd()]
568 568
569 569 if new_session:
570 570 if self.session_number:
571 571 self.end_session()
572 572 self.input_hist_parsed[:] = [""]
573 573 self.input_hist_raw[:] = [""]
574 574 self.new_session()
575 575
576 576 # ------------------------------
577 577 # Methods for retrieving history
578 578 # ------------------------------
579 579 def get_session_info(self, session=0):
580 580 """Get info about a session.
581 581
582 582 Parameters
583 583 ----------
584 584
585 585 session : int
586 586 Session number to retrieve. The current session is 0, and negative
587 587 numbers count back from current session, so -1 is the previous session.
588 588
589 589 Returns
590 590 -------
591 591
592 592 session_id : int
593 593 Session ID number
594 594 start : datetime
595 595 Timestamp for the start of the session.
596 596 end : datetime
597 597 Timestamp for the end of the session, or None if IPython crashed.
598 598 num_cmds : int
599 599 Number of commands run, or None if IPython crashed.
600 600 remark : unicode
601 601 A manually set description.
602 602 """
603 603 if session <= 0:
604 604 session += self.session_number
605 605
606 606 return super(HistoryManager, self).get_session_info(session=session)
607 607
608 608 def _get_range_session(self, start=1, stop=None, raw=True, output=False):
609 609 """Get input and output history from the current session. Called by
610 610 get_range, and takes similar parameters."""
611 611 input_hist = self.input_hist_raw if raw else self.input_hist_parsed
612 612
613 613 n = len(input_hist)
614 614 if start < 0:
615 615 start += n
616 616 if not stop or (stop > n):
617 617 stop = n
618 618 elif stop < 0:
619 619 stop += n
620 620
621 621 for i in range(start, stop):
622 622 if output:
623 623 line = (input_hist[i], self.output_hist_reprs.get(i))
624 624 else:
625 625 line = input_hist[i]
626 626 yield (0, i, line)
627 627
628 628 def get_range(self, session=0, start=1, stop=None, raw=True,output=False):
629 629 """Retrieve input by session.
630 630
631 631 Parameters
632 632 ----------
633 633 session : int
634 634 Session number to retrieve. The current session is 0, and negative
635 635 numbers count back from current session, so -1 is previous session.
636 636 start : int
637 637 First line to retrieve.
638 638 stop : int
639 639 End of line range (excluded from output itself). If None, retrieve
640 640 to the end of the session.
641 641 raw : bool
642 642 If True, return untranslated input
643 643 output : bool
644 644 If True, attempt to include output. This will be 'real' Python
645 645 objects for the current session, or text reprs from previous
646 646 sessions if db_log_output was enabled at the time. Where no output
647 647 is found, None is used.
648 648
649 649 Returns
650 650 -------
651 651 entries
652 652 An iterator over the desired lines. Each line is a 3-tuple, either
653 653 (session, line, input) if output is False, or
654 654 (session, line, (input, output)) if output is True.
655 655 """
656 656 if session <= 0:
657 657 session += self.session_number
658 658 if session==self.session_number: # Current session
659 659 return self._get_range_session(start, stop, raw, output)
660 660 return super(HistoryManager, self).get_range(session, start, stop, raw,
661 661 output)
662 662
663 663 ## ----------------------------
664 664 ## Methods for storing history:
665 665 ## ----------------------------
666 666 def store_inputs(self, line_num, source, source_raw=None):
667 667 """Store source and raw input in history and create input cache
668 668 variables ``_i*``.
669 669
670 670 Parameters
671 671 ----------
672 672 line_num : int
673 673 The prompt number of this input.
674 674
675 675 source : str
676 676 Python input.
677 677
678 678 source_raw : str, optional
679 679 If given, this is the raw input without any IPython transformations
680 680 applied to it. If not given, ``source`` is used.
681 681 """
682 682 if source_raw is None:
683 683 source_raw = source
684 684 source = source.rstrip('\n')
685 685 source_raw = source_raw.rstrip('\n')
686 686
687 687 # do not store exit/quit commands
688 688 if self._exit_re.match(source_raw.strip()):
689 689 return
690 690
691 691 self.input_hist_parsed.append(source)
692 692 self.input_hist_raw.append(source_raw)
693 693
694 694 with self.db_input_cache_lock:
695 695 self.db_input_cache.append((line_num, source, source_raw))
696 696 # Trigger to flush cache and write to DB.
697 697 if len(self.db_input_cache) >= self.db_cache_size:
698 698 self.save_flag.set()
699 699
700 700 # update the auto _i variables
701 701 self._iii = self._ii
702 702 self._ii = self._i
703 703 self._i = self._i00
704 704 self._i00 = source_raw
705 705
706 706 # hackish access to user namespace to create _i1,_i2... dynamically
707 707 new_i = '_i%s' % line_num
708 708 to_main = {'_i': self._i,
709 709 '_ii': self._ii,
710 710 '_iii': self._iii,
711 711 new_i : self._i00 }
712 712
713 713 if self.shell is not None:
714 714 self.shell.push(to_main, interactive=False)
715 715
716 716 def store_output(self, line_num):
717 717 """If database output logging is enabled, this saves all the
718 718 outputs from the indicated prompt number to the database. It's
719 719 called by run_cell after code has been executed.
720 720
721 721 Parameters
722 722 ----------
723 723 line_num : int
724 724 The line number from which to save outputs
725 725 """
726 726 if (not self.db_log_output) or (line_num not in self.output_hist_reprs):
727 727 return
728 728 output = self.output_hist_reprs[line_num]
729 729
730 730 with self.db_output_cache_lock:
731 731 self.db_output_cache.append((line_num, output))
732 732 if self.db_cache_size <= 1:
733 733 self.save_flag.set()
734 734
735 735 def _writeout_input_cache(self, conn):
736 736 with conn:
737 737 for line in self.db_input_cache:
738 738 conn.execute("INSERT INTO history VALUES (?, ?, ?, ?)",
739 739 (self.session_number,)+line)
740 740
741 741 def _writeout_output_cache(self, conn):
742 742 with conn:
743 743 for line in self.db_output_cache:
744 744 conn.execute("INSERT INTO output_history VALUES (?, ?, ?)",
745 745 (self.session_number,)+line)
746 746
747 747 @needs_sqlite
748 748 def writeout_cache(self, conn=None):
749 749 """Write any entries in the cache to the database."""
750 750 if conn is None:
751 751 conn = self.db
752 752
753 753 with self.db_input_cache_lock:
754 754 try:
755 755 self._writeout_input_cache(conn)
756 756 except sqlite3.IntegrityError:
757 757 self.new_session(conn)
758 758 print("ERROR! Session/line number was not unique in",
759 759 "database. History logging moved to new session",
760 760 self.session_number)
761 761 try:
762 762 # Try writing to the new session. If this fails, don't
763 763 # recurse
764 764 self._writeout_input_cache(conn)
765 765 except sqlite3.IntegrityError:
766 766 pass
767 767 finally:
768 768 self.db_input_cache = []
769 769
770 770 with self.db_output_cache_lock:
771 771 try:
772 772 self._writeout_output_cache(conn)
773 773 except sqlite3.IntegrityError:
774 774 print("!! Session/line number for output was not unique",
775 775 "in database. Output will not be stored.")
776 776 finally:
777 777 self.db_output_cache = []
778 778
779 779
780 780 class HistorySavingThread(threading.Thread):
781 781 """This thread takes care of writing history to the database, so that
782 782 the UI isn't held up while that happens.
783 783
784 784 It waits for the HistoryManager's save_flag to be set, then writes out
785 785 the history cache. The main thread is responsible for setting the flag when
786 786 the cache size reaches a defined threshold."""
787 787 daemon = True
788 788 stop_now = False
789 789 enabled = True
790 790 def __init__(self, history_manager):
791 791 super(HistorySavingThread, self).__init__(name="IPythonHistorySavingThread")
792 792 self.history_manager = history_manager
793 793 self.enabled = history_manager.enabled
794 794 atexit.register(self.stop)
795 795
796 796 @needs_sqlite
797 797 def run(self):
798 798 # We need a separate db connection per thread:
799 799 try:
800 800 self.db = sqlite3.connect(self.history_manager.hist_file,
801 801 **self.history_manager.connection_options
802 802 )
803 803 while True:
804 804 self.history_manager.save_flag.wait()
805 805 if self.stop_now:
806 806 self.db.close()
807 807 return
808 808 self.history_manager.save_flag.clear()
809 809 self.history_manager.writeout_cache(self.db)
810 810 except Exception as e:
811 811 print(("The history saving thread hit an unexpected error (%s)."
812 812 "History will not be written to the database.") % repr(e))
813 813
814 814 def stop(self):
815 815 """This can be called from the main thread to safely stop this thread.
816 816
817 817 Note that it does not attempt to write out remaining history before
818 818 exiting. That should be done by calling the HistoryManager's
819 819 end_session method."""
820 820 self.stop_now = True
821 821 self.history_manager.save_flag.set()
822 822 self.join()
823 823
824 824
825 825 # To match, e.g. ~5/8-~2/3
826 826 range_re = re.compile(r"""
827 827 ((?P<startsess>~?\d+)/)?
828 828 (?P<start>\d+)?
829 829 ((?P<sep>[\-:])
830 830 ((?P<endsess>~?\d+)/)?
831 831 (?P<end>\d+))?
832 832 $""", re.VERBOSE)
833 833
834 834
835 835 def extract_hist_ranges(ranges_str):
836 836 """Turn a string of history ranges into 3-tuples of (session, start, stop).
837 837
838 838 Examples
839 839 --------
840 840 >>> list(extract_hist_ranges("~8/5-~7/4 2"))
841 841 [(-8, 5, None), (-7, 1, 5), (0, 2, 3)]
842 842 """
843 843 for range_str in ranges_str.split():
844 844 rmatch = range_re.match(range_str)
845 845 if not rmatch:
846 846 continue
847 847 start = rmatch.group("start")
848 848 if start:
849 849 start = int(start)
850 850 end = rmatch.group("end")
851 851 # If no end specified, get (a, a + 1)
852 852 end = int(end) if end else start + 1
853 853 else: # start not specified
854 854 if not rmatch.group('startsess'): # no startsess
855 855 continue
856 856 start = 1
857 857 end = None # provide the entire session hist
858 858
859 859 if rmatch.group("sep") == "-": # 1-3 == 1:4 --> [1, 2, 3]
860 860 end += 1
861 861 startsess = rmatch.group("startsess") or "0"
862 862 endsess = rmatch.group("endsess") or startsess
863 863 startsess = int(startsess.replace("~","-"))
864 864 endsess = int(endsess.replace("~","-"))
865 865 assert endsess >= startsess, "start session must be earlier than end session"
866 866
867 867 if endsess == startsess:
868 868 yield (startsess, start, end)
869 869 continue
870 870 # Multiple sessions in one range:
871 871 yield (startsess, start, None)
872 872 for sess in range(startsess+1, endsess):
873 873 yield (sess, 1, None)
874 874 yield (endsess, 1, end)
875 875
876 876
877 877 def _format_lineno(session, line):
878 878 """Helper function to format line numbers properly."""
879 879 if session == 0:
880 880 return str(line)
881 881 return "%s#%s" % (session, line)
882
883
@@ -1,3241 +1,3242 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 from __future__ import absolute_import, print_function
14 14
15 15 import __future__
16 16 import abc
17 17 import ast
18 18 import atexit
19 19 import functools
20 20 import os
21 21 import re
22 22 import runpy
23 23 import sys
24 24 import tempfile
25 25 import traceback
26 26 import types
27 27 import subprocess
28 28 import warnings
29 29 from io import open as io_open
30 30
31 31 from pickleshare import PickleShareDB
32 32
33 33 from traitlets.config.configurable import SingletonConfigurable
34 34 from IPython.core import debugger, oinspect
35 35 from IPython.core import magic
36 36 from IPython.core import page
37 37 from IPython.core import prefilter
38 38 from IPython.core import shadowns
39 39 from IPython.core import ultratb
40 40 from IPython.core.alias import Alias, AliasManager
41 41 from IPython.core.autocall import ExitAutocall
42 42 from IPython.core.builtin_trap import BuiltinTrap
43 43 from IPython.core.events import EventManager, available_events
44 44 from IPython.core.compilerop import CachingCompiler, check_linecache_ipython
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 IPythonInputSplitter, 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.prompts import PromptManager
59 59 from IPython.core.usage import default_banner
60 60 from IPython.testing.skipdoctest import skip_doctest
61 61 from IPython.utils import PyColorize
62 62 from IPython.utils import io
63 63 from IPython.utils import py3compat
64 64 from IPython.utils import openpy
65 65 from IPython.utils.contexts import NoOpContext
66 66 from IPython.utils.decorators import undoc
67 67 from IPython.utils.io import ask_yes_no
68 68 from IPython.utils.ipstruct import Struct
69 69 from IPython.paths import get_ipython_dir
70 70 from IPython.utils.path import get_home_dir, get_py_filename, unquote_filename, ensure_dir_exists
71 71 from IPython.utils.process import system, getoutput
72 72 from IPython.utils.py3compat import (builtin_mod, unicode_type, string_types,
73 73 with_metaclass, iteritems)
74 74 from IPython.utils.strdispatch import StrDispatch
75 75 from IPython.utils.syspathcontext import prepended_to_syspath
76 76 from IPython.utils.text import (format_screen, LSString, SList,
77 77 DollarFormatter)
78 78 from traitlets import (Integer, Bool, CBool, CaselessStrEnum, Enum,
79 79 List, Dict, Unicode, Instance, Type)
80 from IPython.utils.warn import warn, error
80 from warnings import warn
81 from logging import error
81 82 import IPython.core.hooks
82 83
83 84 #-----------------------------------------------------------------------------
84 85 # Globals
85 86 #-----------------------------------------------------------------------------
86 87
87 88 # compiled regexps for autoindent management
88 89 dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass')
89 90
90 91 #-----------------------------------------------------------------------------
91 92 # Utilities
92 93 #-----------------------------------------------------------------------------
93 94
94 95 @undoc
95 96 def softspace(file, newvalue):
96 97 """Copied from code.py, to remove the dependency"""
97 98
98 99 oldvalue = 0
99 100 try:
100 101 oldvalue = file.softspace
101 102 except AttributeError:
102 103 pass
103 104 try:
104 105 file.softspace = newvalue
105 106 except (AttributeError, TypeError):
106 107 # "attribute-less object" or "read-only attributes"
107 108 pass
108 109 return oldvalue
109 110
110 111 @undoc
111 112 def no_op(*a, **kw): pass
112 113
113 114
114 115 class SpaceInInput(Exception): pass
115 116
116 117 @undoc
117 118 class Bunch: pass
118 119
119 120
120 121 def get_default_colors():
121 122 if sys.platform=='darwin':
122 123 return "LightBG"
123 124 elif os.name=='nt':
124 125 return 'Linux'
125 126 else:
126 127 return 'Linux'
127 128
128 129
129 130 class SeparateUnicode(Unicode):
130 131 r"""A Unicode subclass to validate separate_in, separate_out, etc.
131 132
132 133 This is a Unicode based trait that converts '0'->'' and ``'\\n'->'\n'``.
133 134 """
134 135
135 136 def validate(self, obj, value):
136 137 if value == '0': value = ''
137 138 value = value.replace('\\n','\n')
138 139 return super(SeparateUnicode, self).validate(obj, value)
139 140
140 141
141 142 @undoc
142 143 class DummyMod(object):
143 144 """A dummy module used for IPython's interactive module when
144 145 a namespace must be assigned to the module's __dict__."""
145 146 pass
146 147
147 148
148 149 class ExecutionResult(object):
149 150 """The result of a call to :meth:`InteractiveShell.run_cell`
150 151
151 152 Stores information about what took place.
152 153 """
153 154 execution_count = None
154 155 error_before_exec = None
155 156 error_in_exec = None
156 157 result = None
157 158
158 159 @property
159 160 def success(self):
160 161 return (self.error_before_exec is None) and (self.error_in_exec is None)
161 162
162 163 def raise_error(self):
163 164 """Reraises error if `success` is `False`, otherwise does nothing"""
164 165 if self.error_before_exec is not None:
165 166 raise self.error_before_exec
166 167 if self.error_in_exec is not None:
167 168 raise self.error_in_exec
168 169
169 170
170 171 class InteractiveShell(SingletonConfigurable):
171 172 """An enhanced, interactive shell for Python."""
172 173
173 174 _instance = None
174 175
175 176 ast_transformers = List([], config=True, help=
176 177 """
177 178 A list of ast.NodeTransformer subclass instances, which will be applied
178 179 to user input before code is run.
179 180 """
180 181 )
181 182
182 183 autocall = Enum((0,1,2), default_value=0, config=True, help=
183 184 """
184 185 Make IPython automatically call any callable object even if you didn't
185 186 type explicit parentheses. For example, 'str 43' becomes 'str(43)'
186 187 automatically. The value can be '0' to disable the feature, '1' for
187 188 'smart' autocall, where it is not applied if there are no more
188 189 arguments on the line, and '2' for 'full' autocall, where all callable
189 190 objects are automatically called (even if no arguments are present).
190 191 """
191 192 )
192 193 # TODO: remove all autoindent logic and put into frontends.
193 194 # We can't do this yet because even runlines uses the autoindent.
194 195 autoindent = CBool(True, config=True, help=
195 196 """
196 197 Autoindent IPython code entered interactively.
197 198 """
198 199 )
199 200 automagic = CBool(True, config=True, help=
200 201 """
201 202 Enable magic commands to be called without the leading %.
202 203 """
203 204 )
204 205
205 206 banner1 = Unicode(default_banner, config=True,
206 207 help="""The part of the banner to be printed before the profile"""
207 208 )
208 209 banner2 = Unicode('', config=True,
209 210 help="""The part of the banner to be printed after the profile"""
210 211 )
211 212
212 213 cache_size = Integer(1000, config=True, help=
213 214 """
214 215 Set the size of the output cache. The default is 1000, you can
215 216 change it permanently in your config file. Setting it to 0 completely
216 217 disables the caching system, and the minimum value accepted is 20 (if
217 218 you provide a value less than 20, it is reset to 0 and a warning is
218 219 issued). This limit is defined because otherwise you'll spend more
219 220 time re-flushing a too small cache than working
220 221 """
221 222 )
222 223 color_info = CBool(True, config=True, help=
223 224 """
224 225 Use colors for displaying information about objects. Because this
225 226 information is passed through a pager (like 'less'), and some pagers
226 227 get confused with color codes, this capability can be turned off.
227 228 """
228 229 )
229 230 colors = CaselessStrEnum(('NoColor','LightBG','Linux'),
230 231 default_value=get_default_colors(), config=True,
231 232 help="Set the color scheme (NoColor, Linux, or LightBG)."
232 233 )
233 234 colors_force = CBool(False, help=
234 235 """
235 236 Force use of ANSI color codes, regardless of OS and readline
236 237 availability.
237 238 """
238 239 # FIXME: This is essentially a hack to allow ZMQShell to show colors
239 240 # without readline on Win32. When the ZMQ formatting system is
240 241 # refactored, this should be removed.
241 242 )
242 243 debug = CBool(False, config=True)
243 244 deep_reload = CBool(False, config=True, help=
244 245 """
245 246 **Deprecated**
246 247
247 248 Will be removed in IPython 6.0
248 249
249 250 Enable deep (recursive) reloading by default. IPython can use the
250 251 deep_reload module which reloads changes in modules recursively (it
251 252 replaces the reload() function, so you don't need to change anything to
252 253 use it). `deep_reload` forces a full reload of modules whose code may
253 254 have changed, which the default reload() function does not. When
254 255 deep_reload is off, IPython will use the normal reload(), but
255 256 deep_reload will still be available as dreload().
256 257 """
257 258 )
258 259 disable_failing_post_execute = CBool(False, config=True,
259 260 help="Don't call post-execute functions that have failed in the past."
260 261 )
261 262 display_formatter = Instance(DisplayFormatter, allow_none=True)
262 263 displayhook_class = Type(DisplayHook)
263 264 display_pub_class = Type(DisplayPublisher)
264 265 data_pub_class = None
265 266
266 267 exit_now = CBool(False)
267 268 exiter = Instance(ExitAutocall)
268 269 def _exiter_default(self):
269 270 return ExitAutocall(self)
270 271 # Monotonically increasing execution counter
271 272 execution_count = Integer(1)
272 273 filename = Unicode("<ipython console>")
273 274 ipython_dir= Unicode('', config=True) # Set to get_ipython_dir() in __init__
274 275
275 276 # Input splitter, to transform input line by line and detect when a block
276 277 # is ready to be executed.
277 278 input_splitter = Instance('IPython.core.inputsplitter.IPythonInputSplitter',
278 279 (), {'line_input_checker': True})
279 280
280 281 # This InputSplitter instance is used to transform completed cells before
281 282 # running them. It allows cell magics to contain blank lines.
282 283 input_transformer_manager = Instance('IPython.core.inputsplitter.IPythonInputSplitter',
283 284 (), {'line_input_checker': False})
284 285
285 286 logstart = CBool(False, config=True, help=
286 287 """
287 288 Start logging to the default log file in overwrite mode.
288 289 Use `logappend` to specify a log file to **append** logs to.
289 290 """
290 291 )
291 292 logfile = Unicode('', config=True, help=
292 293 """
293 294 The name of the logfile to use.
294 295 """
295 296 )
296 297 logappend = Unicode('', config=True, help=
297 298 """
298 299 Start logging to the given file in append mode.
299 300 Use `logfile` to specify a log file to **overwrite** logs to.
300 301 """
301 302 )
302 303 object_info_string_level = Enum((0,1,2), default_value=0,
303 304 config=True)
304 305 pdb = CBool(False, config=True, help=
305 306 """
306 307 Automatically call the pdb debugger after every exception.
307 308 """
308 309 )
309 310 multiline_history = CBool(sys.platform != 'win32', config=True,
310 311 help="Save multi-line entries as one entry in readline history"
311 312 )
312 313 display_page = Bool(False, config=True,
313 314 help="""If True, anything that would be passed to the pager
314 315 will be displayed as regular output instead."""
315 316 )
316 317
317 318 # deprecated prompt traits:
318 319
319 320 prompt_in1 = Unicode('In [\\#]: ', config=True,
320 321 help="Deprecated, will be removed in IPython 5.0, use PromptManager.in_template")
321 322 prompt_in2 = Unicode(' .\\D.: ', config=True,
322 323 help="Deprecated, will be removed in IPython 5.0, use PromptManager.in2_template")
323 324 prompt_out = Unicode('Out[\\#]: ', config=True,
324 325 help="Deprecated, will be removed in IPython 5.0, use PromptManager.out_template")
325 326 prompts_pad_left = CBool(True, config=True,
326 327 help="Deprecated, will be removed in IPython 5.0, use PromptManager.justify")
327 328
328 329 def _prompt_trait_changed(self, name, old, new):
329 330 table = {
330 331 'prompt_in1' : 'in_template',
331 332 'prompt_in2' : 'in2_template',
332 333 'prompt_out' : 'out_template',
333 334 'prompts_pad_left' : 'justify',
334 335 }
335 336 warn("InteractiveShell.{name} is deprecated, use PromptManager.{newname}".format(
336 337 name=name, newname=table[name])
337 338 )
338 339 # protect against weird cases where self.config may not exist:
339 340 if self.config is not None:
340 341 # propagate to corresponding PromptManager trait
341 342 setattr(self.config.PromptManager, table[name], new)
342 343
343 344 _prompt_in1_changed = _prompt_trait_changed
344 345 _prompt_in2_changed = _prompt_trait_changed
345 346 _prompt_out_changed = _prompt_trait_changed
346 347 _prompt_pad_left_changed = _prompt_trait_changed
347 348
348 349 show_rewritten_input = CBool(True, config=True,
349 350 help="Show rewritten input, e.g. for autocall."
350 351 )
351 352
352 353 quiet = CBool(False, config=True)
353 354
354 355 history_length = Integer(10000, config=True)
355 356
356 357 history_load_length = Integer(1000, config=True, help=
357 358 """
358 359 The number of saved history entries to be loaded
359 360 into the readline buffer at startup.
360 361 """
361 362 )
362 363
363 364 # The readline stuff will eventually be moved to the terminal subclass
364 365 # but for now, we can't do that as readline is welded in everywhere.
365 366 readline_use = CBool(True, config=True)
366 367 readline_remove_delims = Unicode('-/~', config=True)
367 368 readline_delims = Unicode() # set by init_readline()
368 369 # don't use \M- bindings by default, because they
369 370 # conflict with 8-bit encodings. See gh-58,gh-88
370 371 readline_parse_and_bind = List([
371 372 'tab: complete',
372 373 '"\C-l": clear-screen',
373 374 'set show-all-if-ambiguous on',
374 375 '"\C-o": tab-insert',
375 376 '"\C-r": reverse-search-history',
376 377 '"\C-s": forward-search-history',
377 378 '"\C-p": history-search-backward',
378 379 '"\C-n": history-search-forward',
379 380 '"\e[A": history-search-backward',
380 381 '"\e[B": history-search-forward',
381 382 '"\C-k": kill-line',
382 383 '"\C-u": unix-line-discard',
383 384 ], config=True)
384 385
385 386 _custom_readline_config = False
386 387
387 388 def _readline_parse_and_bind_changed(self, name, old, new):
388 389 # notice that readline config is customized
389 390 # indicates that it should have higher priority than inputrc
390 391 self._custom_readline_config = True
391 392
392 393 ast_node_interactivity = Enum(['all', 'last', 'last_expr', 'none'],
393 394 default_value='last_expr', config=True,
394 395 help="""
395 396 'all', 'last', 'last_expr' or 'none', specifying which nodes should be
396 397 run interactively (displaying output from expressions).""")
397 398
398 399 # TODO: this part of prompt management should be moved to the frontends.
399 400 # Use custom TraitTypes that convert '0'->'' and '\\n'->'\n'
400 401 separate_in = SeparateUnicode('\n', config=True)
401 402 separate_out = SeparateUnicode('', config=True)
402 403 separate_out2 = SeparateUnicode('', config=True)
403 404 wildcards_case_sensitive = CBool(True, config=True)
404 405 xmode = CaselessStrEnum(('Context','Plain', 'Verbose'),
405 406 default_value='Context', config=True)
406 407
407 408 # Subcomponents of InteractiveShell
408 409 alias_manager = Instance('IPython.core.alias.AliasManager', allow_none=True)
409 410 prefilter_manager = Instance('IPython.core.prefilter.PrefilterManager', allow_none=True)
410 411 builtin_trap = Instance('IPython.core.builtin_trap.BuiltinTrap', allow_none=True)
411 412 display_trap = Instance('IPython.core.display_trap.DisplayTrap', allow_none=True)
412 413 extension_manager = Instance('IPython.core.extensions.ExtensionManager', allow_none=True)
413 414 payload_manager = Instance('IPython.core.payload.PayloadManager', allow_none=True)
414 415 history_manager = Instance('IPython.core.history.HistoryAccessorBase', allow_none=True)
415 416 magics_manager = Instance('IPython.core.magic.MagicsManager', allow_none=True)
416 417
417 418 profile_dir = Instance('IPython.core.application.ProfileDir', allow_none=True)
418 419 @property
419 420 def profile(self):
420 421 if self.profile_dir is not None:
421 422 name = os.path.basename(self.profile_dir.location)
422 423 return name.replace('profile_','')
423 424
424 425
425 426 # Private interface
426 427 _post_execute = Dict()
427 428
428 429 # Tracks any GUI loop loaded for pylab
429 430 pylab_gui_select = None
430 431
431 432 def __init__(self, ipython_dir=None, profile_dir=None,
432 433 user_module=None, user_ns=None,
433 434 custom_exceptions=((), None), **kwargs):
434 435
435 436 # This is where traits with a config_key argument are updated
436 437 # from the values on config.
437 438 super(InteractiveShell, self).__init__(**kwargs)
438 439 self.configurables = [self]
439 440
440 441 # These are relatively independent and stateless
441 442 self.init_ipython_dir(ipython_dir)
442 443 self.init_profile_dir(profile_dir)
443 444 self.init_instance_attrs()
444 445 self.init_environment()
445 446
446 447 # Check if we're in a virtualenv, and set up sys.path.
447 448 self.init_virtualenv()
448 449
449 450 # Create namespaces (user_ns, user_global_ns, etc.)
450 451 self.init_create_namespaces(user_module, user_ns)
451 452 # This has to be done after init_create_namespaces because it uses
452 453 # something in self.user_ns, but before init_sys_modules, which
453 454 # is the first thing to modify sys.
454 455 # TODO: When we override sys.stdout and sys.stderr before this class
455 456 # is created, we are saving the overridden ones here. Not sure if this
456 457 # is what we want to do.
457 458 self.save_sys_module_state()
458 459 self.init_sys_modules()
459 460
460 461 # While we're trying to have each part of the code directly access what
461 462 # it needs without keeping redundant references to objects, we have too
462 463 # much legacy code that expects ip.db to exist.
463 464 self.db = PickleShareDB(os.path.join(self.profile_dir.location, 'db'))
464 465
465 466 self.init_history()
466 467 self.init_encoding()
467 468 self.init_prefilter()
468 469
469 470 self.init_syntax_highlighting()
470 471 self.init_hooks()
471 472 self.init_events()
472 473 self.init_pushd_popd_magic()
473 474 # self.init_traceback_handlers use to be here, but we moved it below
474 475 # because it and init_io have to come after init_readline.
475 476 self.init_user_ns()
476 477 self.init_logger()
477 478 self.init_builtins()
478 479
479 480 # The following was in post_config_initialization
480 481 self.init_inspector()
481 482 # init_readline() must come before init_io(), because init_io uses
482 483 # readline related things.
483 484 self.init_readline()
484 485 # We save this here in case user code replaces raw_input, but it needs
485 486 # to be after init_readline(), because PyPy's readline works by replacing
486 487 # raw_input.
487 488 if py3compat.PY3:
488 489 self.raw_input_original = input
489 490 else:
490 491 self.raw_input_original = raw_input
491 492 # init_completer must come after init_readline, because it needs to
492 493 # know whether readline is present or not system-wide to configure the
493 494 # completers, since the completion machinery can now operate
494 495 # independently of readline (e.g. over the network)
495 496 self.init_completer()
496 497 # TODO: init_io() needs to happen before init_traceback handlers
497 498 # because the traceback handlers hardcode the stdout/stderr streams.
498 499 # This logic in in debugger.Pdb and should eventually be changed.
499 500 self.init_io()
500 501 self.init_traceback_handlers(custom_exceptions)
501 502 self.init_prompts()
502 503 self.init_display_formatter()
503 504 self.init_display_pub()
504 505 self.init_data_pub()
505 506 self.init_displayhook()
506 507 self.init_magics()
507 508 self.init_alias()
508 509 self.init_logstart()
509 510 self.init_pdb()
510 511 self.init_extension_manager()
511 512 self.init_payload()
512 513 self.init_deprecation_warnings()
513 514 self.hooks.late_startup_hook()
514 515 self.events.trigger('shell_initialized', self)
515 516 atexit.register(self.atexit_operations)
516 517
517 518 def get_ipython(self):
518 519 """Return the currently running IPython instance."""
519 520 return self
520 521
521 522 #-------------------------------------------------------------------------
522 523 # Trait changed handlers
523 524 #-------------------------------------------------------------------------
524 525
525 526 def _ipython_dir_changed(self, name, new):
526 527 ensure_dir_exists(new)
527 528
528 529 def set_autoindent(self,value=None):
529 530 """Set the autoindent flag, checking for readline support.
530 531
531 532 If called with no arguments, it acts as a toggle."""
532 533
533 534 if value != 0 and not self.has_readline:
534 535 if os.name == 'posix':
535 536 warn("The auto-indent feature requires the readline library")
536 537 self.autoindent = 0
537 538 return
538 539 if value is None:
539 540 self.autoindent = not self.autoindent
540 541 else:
541 542 self.autoindent = value
542 543
543 544 #-------------------------------------------------------------------------
544 545 # init_* methods called by __init__
545 546 #-------------------------------------------------------------------------
546 547
547 548 def init_ipython_dir(self, ipython_dir):
548 549 if ipython_dir is not None:
549 550 self.ipython_dir = ipython_dir
550 551 return
551 552
552 553 self.ipython_dir = get_ipython_dir()
553 554
554 555 def init_profile_dir(self, profile_dir):
555 556 if profile_dir is not None:
556 557 self.profile_dir = profile_dir
557 558 return
558 559 self.profile_dir =\
559 560 ProfileDir.create_profile_dir_by_name(self.ipython_dir, 'default')
560 561
561 562 def init_instance_attrs(self):
562 563 self.more = False
563 564
564 565 # command compiler
565 566 self.compile = CachingCompiler()
566 567
567 568 # Make an empty namespace, which extension writers can rely on both
568 569 # existing and NEVER being used by ipython itself. This gives them a
569 570 # convenient location for storing additional information and state
570 571 # their extensions may require, without fear of collisions with other
571 572 # ipython names that may develop later.
572 573 self.meta = Struct()
573 574
574 575 # Temporary files used for various purposes. Deleted at exit.
575 576 self.tempfiles = []
576 577 self.tempdirs = []
577 578
578 579 # Keep track of readline usage (later set by init_readline)
579 580 self.has_readline = False
580 581
581 582 # keep track of where we started running (mainly for crash post-mortem)
582 583 # This is not being used anywhere currently.
583 584 self.starting_dir = py3compat.getcwd()
584 585
585 586 # Indentation management
586 587 self.indent_current_nsp = 0
587 588
588 589 # Dict to track post-execution functions that have been registered
589 590 self._post_execute = {}
590 591
591 592 def init_environment(self):
592 593 """Any changes we need to make to the user's environment."""
593 594 pass
594 595
595 596 def init_encoding(self):
596 597 # Get system encoding at startup time. Certain terminals (like Emacs
597 598 # under Win32 have it set to None, and we need to have a known valid
598 599 # encoding to use in the raw_input() method
599 600 try:
600 601 self.stdin_encoding = sys.stdin.encoding or 'ascii'
601 602 except AttributeError:
602 603 self.stdin_encoding = 'ascii'
603 604
604 605 def init_syntax_highlighting(self):
605 606 # Python source parser/formatter for syntax highlighting
606 607 pyformat = PyColorize.Parser().format
607 608 self.pycolorize = lambda src: pyformat(src,'str',self.colors)
608 609
609 610 def init_pushd_popd_magic(self):
610 611 # for pushd/popd management
611 612 self.home_dir = get_home_dir()
612 613
613 614 self.dir_stack = []
614 615
615 616 def init_logger(self):
616 617 self.logger = Logger(self.home_dir, logfname='ipython_log.py',
617 618 logmode='rotate')
618 619
619 620 def init_logstart(self):
620 621 """Initialize logging in case it was requested at the command line.
621 622 """
622 623 if self.logappend:
623 624 self.magic('logstart %s append' % self.logappend)
624 625 elif self.logfile:
625 626 self.magic('logstart %s' % self.logfile)
626 627 elif self.logstart:
627 628 self.magic('logstart')
628 629
629 630 def init_deprecation_warnings(self):
630 631 """
631 632 register default filter for deprecation warning.
632 633
633 634 This will allow deprecation warning of function used interactively to show
634 635 warning to users, and still hide deprecation warning from libraries import.
635 636 """
636 637 warnings.filterwarnings("default", category=DeprecationWarning, module=self.user_ns.get("__name__"))
637 638
638 639 def init_builtins(self):
639 640 # A single, static flag that we set to True. Its presence indicates
640 641 # that an IPython shell has been created, and we make no attempts at
641 642 # removing on exit or representing the existence of more than one
642 643 # IPython at a time.
643 644 builtin_mod.__dict__['__IPYTHON__'] = True
644 645
645 646 # In 0.11 we introduced '__IPYTHON__active' as an integer we'd try to
646 647 # manage on enter/exit, but with all our shells it's virtually
647 648 # impossible to get all the cases right. We're leaving the name in for
648 649 # those who adapted their codes to check for this flag, but will
649 650 # eventually remove it after a few more releases.
650 651 builtin_mod.__dict__['__IPYTHON__active'] = \
651 652 'Deprecated, check for __IPYTHON__'
652 653
653 654 self.builtin_trap = BuiltinTrap(shell=self)
654 655
655 656 def init_inspector(self):
656 657 # Object inspector
657 658 self.inspector = oinspect.Inspector(oinspect.InspectColors,
658 659 PyColorize.ANSICodeColors,
659 660 'NoColor',
660 661 self.object_info_string_level)
661 662
662 663 def init_io(self):
663 664 # This will just use sys.stdout and sys.stderr. If you want to
664 665 # override sys.stdout and sys.stderr themselves, you need to do that
665 666 # *before* instantiating this class, because io holds onto
666 667 # references to the underlying streams.
667 668 if (sys.platform == 'win32' or sys.platform == 'cli') and self.has_readline:
668 669 io.stdout = io.stderr = io.IOStream(self.readline._outputfile)
669 670 else:
670 671 io.stdout = io.IOStream(sys.stdout)
671 672 io.stderr = io.IOStream(sys.stderr)
672 673
673 674 def init_prompts(self):
674 675 self.prompt_manager = PromptManager(shell=self, parent=self)
675 676 self.configurables.append(self.prompt_manager)
676 677 # Set system prompts, so that scripts can decide if they are running
677 678 # interactively.
678 679 sys.ps1 = 'In : '
679 680 sys.ps2 = '...: '
680 681 sys.ps3 = 'Out: '
681 682
682 683 def init_display_formatter(self):
683 684 self.display_formatter = DisplayFormatter(parent=self)
684 685 self.configurables.append(self.display_formatter)
685 686
686 687 def init_display_pub(self):
687 688 self.display_pub = self.display_pub_class(parent=self)
688 689 self.configurables.append(self.display_pub)
689 690
690 691 def init_data_pub(self):
691 692 if not self.data_pub_class:
692 693 self.data_pub = None
693 694 return
694 695 self.data_pub = self.data_pub_class(parent=self)
695 696 self.configurables.append(self.data_pub)
696 697
697 698 def init_displayhook(self):
698 699 # Initialize displayhook, set in/out prompts and printing system
699 700 self.displayhook = self.displayhook_class(
700 701 parent=self,
701 702 shell=self,
702 703 cache_size=self.cache_size,
703 704 )
704 705 self.configurables.append(self.displayhook)
705 706 # This is a context manager that installs/revmoes the displayhook at
706 707 # the appropriate time.
707 708 self.display_trap = DisplayTrap(hook=self.displayhook)
708 709
709 710 def init_virtualenv(self):
710 711 """Add a virtualenv to sys.path so the user can import modules from it.
711 712 This isn't perfect: it doesn't use the Python interpreter with which the
712 713 virtualenv was built, and it ignores the --no-site-packages option. A
713 714 warning will appear suggesting the user installs IPython in the
714 715 virtualenv, but for many cases, it probably works well enough.
715 716
716 717 Adapted from code snippets online.
717 718
718 719 http://blog.ufsoft.org/2009/1/29/ipython-and-virtualenv
719 720 """
720 721 if 'VIRTUAL_ENV' not in os.environ:
721 722 # Not in a virtualenv
722 723 return
723 724
724 725 # venv detection:
725 726 # stdlib venv may symlink sys.executable, so we can't use realpath.
726 727 # but others can symlink *to* the venv Python, so we can't just use sys.executable.
727 728 # So we just check every item in the symlink tree (generally <= 3)
728 729 p = os.path.normcase(sys.executable)
729 730 paths = [p]
730 731 while os.path.islink(p):
731 732 p = os.path.normcase(os.path.join(os.path.dirname(p), os.readlink(p)))
732 733 paths.append(p)
733 734 p_venv = os.path.normcase(os.environ['VIRTUAL_ENV'])
734 735 if any(p.startswith(p_venv) for p in paths):
735 736 # Running properly in the virtualenv, don't need to do anything
736 737 return
737 738
738 739 warn("Attempting to work in a virtualenv. If you encounter problems, please "
739 740 "install IPython inside the virtualenv.")
740 741 if sys.platform == "win32":
741 742 virtual_env = os.path.join(os.environ['VIRTUAL_ENV'], 'Lib', 'site-packages')
742 743 else:
743 744 virtual_env = os.path.join(os.environ['VIRTUAL_ENV'], 'lib',
744 745 'python%d.%d' % sys.version_info[:2], 'site-packages')
745 746
746 747 import site
747 748 sys.path.insert(0, virtual_env)
748 749 site.addsitedir(virtual_env)
749 750
750 751 #-------------------------------------------------------------------------
751 752 # Things related to injections into the sys module
752 753 #-------------------------------------------------------------------------
753 754
754 755 def save_sys_module_state(self):
755 756 """Save the state of hooks in the sys module.
756 757
757 758 This has to be called after self.user_module is created.
758 759 """
759 760 self._orig_sys_module_state = {'stdin': sys.stdin,
760 761 'stdout': sys.stdout,
761 762 'stderr': sys.stderr,
762 763 'excepthook': sys.excepthook}
763 764 self._orig_sys_modules_main_name = self.user_module.__name__
764 765 self._orig_sys_modules_main_mod = sys.modules.get(self.user_module.__name__)
765 766
766 767 def restore_sys_module_state(self):
767 768 """Restore the state of the sys module."""
768 769 try:
769 770 for k, v in iteritems(self._orig_sys_module_state):
770 771 setattr(sys, k, v)
771 772 except AttributeError:
772 773 pass
773 774 # Reset what what done in self.init_sys_modules
774 775 if self._orig_sys_modules_main_mod is not None:
775 776 sys.modules[self._orig_sys_modules_main_name] = self._orig_sys_modules_main_mod
776 777
777 778 #-------------------------------------------------------------------------
778 779 # Things related to the banner
779 780 #-------------------------------------------------------------------------
780 781
781 782 @property
782 783 def banner(self):
783 784 banner = self.banner1
784 785 if self.profile and self.profile != 'default':
785 786 banner += '\nIPython profile: %s\n' % self.profile
786 787 if self.banner2:
787 788 banner += '\n' + self.banner2
788 789 return banner
789 790
790 791 def show_banner(self, banner=None):
791 792 if banner is None:
792 793 banner = self.banner
793 794 self.write(banner)
794 795
795 796 #-------------------------------------------------------------------------
796 797 # Things related to hooks
797 798 #-------------------------------------------------------------------------
798 799
799 800 def init_hooks(self):
800 801 # hooks holds pointers used for user-side customizations
801 802 self.hooks = Struct()
802 803
803 804 self.strdispatchers = {}
804 805
805 806 # Set all default hooks, defined in the IPython.hooks module.
806 807 hooks = IPython.core.hooks
807 808 for hook_name in hooks.__all__:
808 809 # default hooks have priority 100, i.e. low; user hooks should have
809 810 # 0-100 priority
810 811 self.set_hook(hook_name,getattr(hooks,hook_name), 100, _warn_deprecated=False)
811 812
812 813 if self.display_page:
813 814 self.set_hook('show_in_pager', page.as_hook(page.display_page), 90)
814 815
815 816 def set_hook(self,name,hook, priority=50, str_key=None, re_key=None,
816 817 _warn_deprecated=True):
817 818 """set_hook(name,hook) -> sets an internal IPython hook.
818 819
819 820 IPython exposes some of its internal API as user-modifiable hooks. By
820 821 adding your function to one of these hooks, you can modify IPython's
821 822 behavior to call at runtime your own routines."""
822 823
823 824 # At some point in the future, this should validate the hook before it
824 825 # accepts it. Probably at least check that the hook takes the number
825 826 # of args it's supposed to.
826 827
827 828 f = types.MethodType(hook,self)
828 829
829 830 # check if the hook is for strdispatcher first
830 831 if str_key is not None:
831 832 sdp = self.strdispatchers.get(name, StrDispatch())
832 833 sdp.add_s(str_key, f, priority )
833 834 self.strdispatchers[name] = sdp
834 835 return
835 836 if re_key is not None:
836 837 sdp = self.strdispatchers.get(name, StrDispatch())
837 838 sdp.add_re(re.compile(re_key), f, priority )
838 839 self.strdispatchers[name] = sdp
839 840 return
840 841
841 842 dp = getattr(self.hooks, name, None)
842 843 if name not in IPython.core.hooks.__all__:
843 844 print("Warning! Hook '%s' is not one of %s" % \
844 845 (name, IPython.core.hooks.__all__ ))
845 846
846 847 if _warn_deprecated and (name in IPython.core.hooks.deprecated):
847 848 alternative = IPython.core.hooks.deprecated[name]
848 849 warn("Hook {} is deprecated. Use {} instead.".format(name, alternative))
849 850
850 851 if not dp:
851 852 dp = IPython.core.hooks.CommandChainDispatcher()
852 853
853 854 try:
854 855 dp.add(f,priority)
855 856 except AttributeError:
856 857 # it was not commandchain, plain old func - replace
857 858 dp = f
858 859
859 860 setattr(self.hooks,name, dp)
860 861
861 862 #-------------------------------------------------------------------------
862 863 # Things related to events
863 864 #-------------------------------------------------------------------------
864 865
865 866 def init_events(self):
866 867 self.events = EventManager(self, available_events)
867 868
868 869 self.events.register("pre_execute", self._clear_warning_registry)
869 870
870 871 def register_post_execute(self, func):
871 872 """DEPRECATED: Use ip.events.register('post_run_cell', func)
872 873
873 874 Register a function for calling after code execution.
874 875 """
875 876 warn("ip.register_post_execute is deprecated, use "
876 877 "ip.events.register('post_run_cell', func) instead.")
877 878 self.events.register('post_run_cell', func)
878 879
879 880 def _clear_warning_registry(self):
880 881 # clear the warning registry, so that different code blocks with
881 882 # overlapping line number ranges don't cause spurious suppression of
882 883 # warnings (see gh-6611 for details)
883 884 if "__warningregistry__" in self.user_global_ns:
884 885 del self.user_global_ns["__warningregistry__"]
885 886
886 887 #-------------------------------------------------------------------------
887 888 # Things related to the "main" module
888 889 #-------------------------------------------------------------------------
889 890
890 891 def new_main_mod(self, filename, modname):
891 892 """Return a new 'main' module object for user code execution.
892 893
893 894 ``filename`` should be the path of the script which will be run in the
894 895 module. Requests with the same filename will get the same module, with
895 896 its namespace cleared.
896 897
897 898 ``modname`` should be the module name - normally either '__main__' or
898 899 the basename of the file without the extension.
899 900
900 901 When scripts are executed via %run, we must keep a reference to their
901 902 __main__ module around so that Python doesn't
902 903 clear it, rendering references to module globals useless.
903 904
904 905 This method keeps said reference in a private dict, keyed by the
905 906 absolute path of the script. This way, for multiple executions of the
906 907 same script we only keep one copy of the namespace (the last one),
907 908 thus preventing memory leaks from old references while allowing the
908 909 objects from the last execution to be accessible.
909 910 """
910 911 filename = os.path.abspath(filename)
911 912 try:
912 913 main_mod = self._main_mod_cache[filename]
913 914 except KeyError:
914 915 main_mod = self._main_mod_cache[filename] = types.ModuleType(
915 916 py3compat.cast_bytes_py2(modname),
916 917 doc="Module created for script run in IPython")
917 918 else:
918 919 main_mod.__dict__.clear()
919 920 main_mod.__name__ = modname
920 921
921 922 main_mod.__file__ = filename
922 923 # It seems pydoc (and perhaps others) needs any module instance to
923 924 # implement a __nonzero__ method
924 925 main_mod.__nonzero__ = lambda : True
925 926
926 927 return main_mod
927 928
928 929 def clear_main_mod_cache(self):
929 930 """Clear the cache of main modules.
930 931
931 932 Mainly for use by utilities like %reset.
932 933
933 934 Examples
934 935 --------
935 936
936 937 In [15]: import IPython
937 938
938 939 In [16]: m = _ip.new_main_mod(IPython.__file__, 'IPython')
939 940
940 941 In [17]: len(_ip._main_mod_cache) > 0
941 942 Out[17]: True
942 943
943 944 In [18]: _ip.clear_main_mod_cache()
944 945
945 946 In [19]: len(_ip._main_mod_cache) == 0
946 947 Out[19]: True
947 948 """
948 949 self._main_mod_cache.clear()
949 950
950 951 #-------------------------------------------------------------------------
951 952 # Things related to debugging
952 953 #-------------------------------------------------------------------------
953 954
954 955 def init_pdb(self):
955 956 # Set calling of pdb on exceptions
956 957 # self.call_pdb is a property
957 958 self.call_pdb = self.pdb
958 959
959 960 def _get_call_pdb(self):
960 961 return self._call_pdb
961 962
962 963 def _set_call_pdb(self,val):
963 964
964 965 if val not in (0,1,False,True):
965 966 raise ValueError('new call_pdb value must be boolean')
966 967
967 968 # store value in instance
968 969 self._call_pdb = val
969 970
970 971 # notify the actual exception handlers
971 972 self.InteractiveTB.call_pdb = val
972 973
973 974 call_pdb = property(_get_call_pdb,_set_call_pdb,None,
974 975 'Control auto-activation of pdb at exceptions')
975 976
976 977 def debugger(self,force=False):
977 978 """Call the pydb/pdb debugger.
978 979
979 980 Keywords:
980 981
981 982 - force(False): by default, this routine checks the instance call_pdb
982 983 flag and does not actually invoke the debugger if the flag is false.
983 984 The 'force' option forces the debugger to activate even if the flag
984 985 is false.
985 986 """
986 987
987 988 if not (force or self.call_pdb):
988 989 return
989 990
990 991 if not hasattr(sys,'last_traceback'):
991 992 error('No traceback has been produced, nothing to debug.')
992 993 return
993 994
994 995 # use pydb if available
995 996 if debugger.has_pydb:
996 997 from pydb import pm
997 998 else:
998 999 # fallback to our internal debugger
999 1000 pm = lambda : self.InteractiveTB.debugger(force=True)
1000 1001
1001 1002 with self.readline_no_record:
1002 1003 pm()
1003 1004
1004 1005 #-------------------------------------------------------------------------
1005 1006 # Things related to IPython's various namespaces
1006 1007 #-------------------------------------------------------------------------
1007 1008 default_user_namespaces = True
1008 1009
1009 1010 def init_create_namespaces(self, user_module=None, user_ns=None):
1010 1011 # Create the namespace where the user will operate. user_ns is
1011 1012 # normally the only one used, and it is passed to the exec calls as
1012 1013 # the locals argument. But we do carry a user_global_ns namespace
1013 1014 # given as the exec 'globals' argument, This is useful in embedding
1014 1015 # situations where the ipython shell opens in a context where the
1015 1016 # distinction between locals and globals is meaningful. For
1016 1017 # non-embedded contexts, it is just the same object as the user_ns dict.
1017 1018
1018 1019 # FIXME. For some strange reason, __builtins__ is showing up at user
1019 1020 # level as a dict instead of a module. This is a manual fix, but I
1020 1021 # should really track down where the problem is coming from. Alex
1021 1022 # Schmolck reported this problem first.
1022 1023
1023 1024 # A useful post by Alex Martelli on this topic:
1024 1025 # Re: inconsistent value from __builtins__
1025 1026 # Von: Alex Martelli <aleaxit@yahoo.com>
1026 1027 # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends
1027 1028 # Gruppen: comp.lang.python
1028 1029
1029 1030 # Michael Hohn <hohn@hooknose.lbl.gov> wrote:
1030 1031 # > >>> print type(builtin_check.get_global_binding('__builtins__'))
1031 1032 # > <type 'dict'>
1032 1033 # > >>> print type(__builtins__)
1033 1034 # > <type 'module'>
1034 1035 # > Is this difference in return value intentional?
1035 1036
1036 1037 # Well, it's documented that '__builtins__' can be either a dictionary
1037 1038 # or a module, and it's been that way for a long time. Whether it's
1038 1039 # intentional (or sensible), I don't know. In any case, the idea is
1039 1040 # that if you need to access the built-in namespace directly, you
1040 1041 # should start with "import __builtin__" (note, no 's') which will
1041 1042 # definitely give you a module. Yeah, it's somewhat confusing:-(.
1042 1043
1043 1044 # These routines return a properly built module and dict as needed by
1044 1045 # the rest of the code, and can also be used by extension writers to
1045 1046 # generate properly initialized namespaces.
1046 1047 if (user_ns is not None) or (user_module is not None):
1047 1048 self.default_user_namespaces = False
1048 1049 self.user_module, self.user_ns = self.prepare_user_module(user_module, user_ns)
1049 1050
1050 1051 # A record of hidden variables we have added to the user namespace, so
1051 1052 # we can list later only variables defined in actual interactive use.
1052 1053 self.user_ns_hidden = {}
1053 1054
1054 1055 # Now that FakeModule produces a real module, we've run into a nasty
1055 1056 # problem: after script execution (via %run), the module where the user
1056 1057 # code ran is deleted. Now that this object is a true module (needed
1057 1058 # so doctest and other tools work correctly), the Python module
1058 1059 # teardown mechanism runs over it, and sets to None every variable
1059 1060 # present in that module. Top-level references to objects from the
1060 1061 # script survive, because the user_ns is updated with them. However,
1061 1062 # calling functions defined in the script that use other things from
1062 1063 # the script will fail, because the function's closure had references
1063 1064 # to the original objects, which are now all None. So we must protect
1064 1065 # these modules from deletion by keeping a cache.
1065 1066 #
1066 1067 # To avoid keeping stale modules around (we only need the one from the
1067 1068 # last run), we use a dict keyed with the full path to the script, so
1068 1069 # only the last version of the module is held in the cache. Note,
1069 1070 # however, that we must cache the module *namespace contents* (their
1070 1071 # __dict__). Because if we try to cache the actual modules, old ones
1071 1072 # (uncached) could be destroyed while still holding references (such as
1072 1073 # those held by GUI objects that tend to be long-lived)>
1073 1074 #
1074 1075 # The %reset command will flush this cache. See the cache_main_mod()
1075 1076 # and clear_main_mod_cache() methods for details on use.
1076 1077
1077 1078 # This is the cache used for 'main' namespaces
1078 1079 self._main_mod_cache = {}
1079 1080
1080 1081 # A table holding all the namespaces IPython deals with, so that
1081 1082 # introspection facilities can search easily.
1082 1083 self.ns_table = {'user_global':self.user_module.__dict__,
1083 1084 'user_local':self.user_ns,
1084 1085 'builtin':builtin_mod.__dict__
1085 1086 }
1086 1087
1087 1088 @property
1088 1089 def user_global_ns(self):
1089 1090 return self.user_module.__dict__
1090 1091
1091 1092 def prepare_user_module(self, user_module=None, user_ns=None):
1092 1093 """Prepare the module and namespace in which user code will be run.
1093 1094
1094 1095 When IPython is started normally, both parameters are None: a new module
1095 1096 is created automatically, and its __dict__ used as the namespace.
1096 1097
1097 1098 If only user_module is provided, its __dict__ is used as the namespace.
1098 1099 If only user_ns is provided, a dummy module is created, and user_ns
1099 1100 becomes the global namespace. If both are provided (as they may be
1100 1101 when embedding), user_ns is the local namespace, and user_module
1101 1102 provides the global namespace.
1102 1103
1103 1104 Parameters
1104 1105 ----------
1105 1106 user_module : module, optional
1106 1107 The current user module in which IPython is being run. If None,
1107 1108 a clean module will be created.
1108 1109 user_ns : dict, optional
1109 1110 A namespace in which to run interactive commands.
1110 1111
1111 1112 Returns
1112 1113 -------
1113 1114 A tuple of user_module and user_ns, each properly initialised.
1114 1115 """
1115 1116 if user_module is None and user_ns is not None:
1116 1117 user_ns.setdefault("__name__", "__main__")
1117 1118 user_module = DummyMod()
1118 1119 user_module.__dict__ = user_ns
1119 1120
1120 1121 if user_module is None:
1121 1122 user_module = types.ModuleType("__main__",
1122 1123 doc="Automatically created module for IPython interactive environment")
1123 1124
1124 1125 # We must ensure that __builtin__ (without the final 's') is always
1125 1126 # available and pointing to the __builtin__ *module*. For more details:
1126 1127 # http://mail.python.org/pipermail/python-dev/2001-April/014068.html
1127 1128 user_module.__dict__.setdefault('__builtin__', builtin_mod)
1128 1129 user_module.__dict__.setdefault('__builtins__', builtin_mod)
1129 1130
1130 1131 if user_ns is None:
1131 1132 user_ns = user_module.__dict__
1132 1133
1133 1134 return user_module, user_ns
1134 1135
1135 1136 def init_sys_modules(self):
1136 1137 # We need to insert into sys.modules something that looks like a
1137 1138 # module but which accesses the IPython namespace, for shelve and
1138 1139 # pickle to work interactively. Normally they rely on getting
1139 1140 # everything out of __main__, but for embedding purposes each IPython
1140 1141 # instance has its own private namespace, so we can't go shoving
1141 1142 # everything into __main__.
1142 1143
1143 1144 # note, however, that we should only do this for non-embedded
1144 1145 # ipythons, which really mimic the __main__.__dict__ with their own
1145 1146 # namespace. Embedded instances, on the other hand, should not do
1146 1147 # this because they need to manage the user local/global namespaces
1147 1148 # only, but they live within a 'normal' __main__ (meaning, they
1148 1149 # shouldn't overtake the execution environment of the script they're
1149 1150 # embedded in).
1150 1151
1151 1152 # This is overridden in the InteractiveShellEmbed subclass to a no-op.
1152 1153 main_name = self.user_module.__name__
1153 1154 sys.modules[main_name] = self.user_module
1154 1155
1155 1156 def init_user_ns(self):
1156 1157 """Initialize all user-visible namespaces to their minimum defaults.
1157 1158
1158 1159 Certain history lists are also initialized here, as they effectively
1159 1160 act as user namespaces.
1160 1161
1161 1162 Notes
1162 1163 -----
1163 1164 All data structures here are only filled in, they are NOT reset by this
1164 1165 method. If they were not empty before, data will simply be added to
1165 1166 therm.
1166 1167 """
1167 1168 # This function works in two parts: first we put a few things in
1168 1169 # user_ns, and we sync that contents into user_ns_hidden so that these
1169 1170 # initial variables aren't shown by %who. After the sync, we add the
1170 1171 # rest of what we *do* want the user to see with %who even on a new
1171 1172 # session (probably nothing, so they really only see their own stuff)
1172 1173
1173 1174 # The user dict must *always* have a __builtin__ reference to the
1174 1175 # Python standard __builtin__ namespace, which must be imported.
1175 1176 # This is so that certain operations in prompt evaluation can be
1176 1177 # reliably executed with builtins. Note that we can NOT use
1177 1178 # __builtins__ (note the 's'), because that can either be a dict or a
1178 1179 # module, and can even mutate at runtime, depending on the context
1179 1180 # (Python makes no guarantees on it). In contrast, __builtin__ is
1180 1181 # always a module object, though it must be explicitly imported.
1181 1182
1182 1183 # For more details:
1183 1184 # http://mail.python.org/pipermail/python-dev/2001-April/014068.html
1184 1185 ns = dict()
1185 1186
1186 1187 # make global variables for user access to the histories
1187 1188 ns['_ih'] = self.history_manager.input_hist_parsed
1188 1189 ns['_oh'] = self.history_manager.output_hist
1189 1190 ns['_dh'] = self.history_manager.dir_hist
1190 1191
1191 1192 ns['_sh'] = shadowns
1192 1193
1193 1194 # user aliases to input and output histories. These shouldn't show up
1194 1195 # in %who, as they can have very large reprs.
1195 1196 ns['In'] = self.history_manager.input_hist_parsed
1196 1197 ns['Out'] = self.history_manager.output_hist
1197 1198
1198 1199 # Store myself as the public api!!!
1199 1200 ns['get_ipython'] = self.get_ipython
1200 1201
1201 1202 ns['exit'] = self.exiter
1202 1203 ns['quit'] = self.exiter
1203 1204
1204 1205 # Sync what we've added so far to user_ns_hidden so these aren't seen
1205 1206 # by %who
1206 1207 self.user_ns_hidden.update(ns)
1207 1208
1208 1209 # Anything put into ns now would show up in %who. Think twice before
1209 1210 # putting anything here, as we really want %who to show the user their
1210 1211 # stuff, not our variables.
1211 1212
1212 1213 # Finally, update the real user's namespace
1213 1214 self.user_ns.update(ns)
1214 1215
1215 1216 @property
1216 1217 def all_ns_refs(self):
1217 1218 """Get a list of references to all the namespace dictionaries in which
1218 1219 IPython might store a user-created object.
1219 1220
1220 1221 Note that this does not include the displayhook, which also caches
1221 1222 objects from the output."""
1222 1223 return [self.user_ns, self.user_global_ns, self.user_ns_hidden] + \
1223 1224 [m.__dict__ for m in self._main_mod_cache.values()]
1224 1225
1225 1226 def reset(self, new_session=True):
1226 1227 """Clear all internal namespaces, and attempt to release references to
1227 1228 user objects.
1228 1229
1229 1230 If new_session is True, a new history session will be opened.
1230 1231 """
1231 1232 # Clear histories
1232 1233 self.history_manager.reset(new_session)
1233 1234 # Reset counter used to index all histories
1234 1235 if new_session:
1235 1236 self.execution_count = 1
1236 1237
1237 1238 # Flush cached output items
1238 1239 if self.displayhook.do_full_cache:
1239 1240 self.displayhook.flush()
1240 1241
1241 1242 # The main execution namespaces must be cleared very carefully,
1242 1243 # skipping the deletion of the builtin-related keys, because doing so
1243 1244 # would cause errors in many object's __del__ methods.
1244 1245 if self.user_ns is not self.user_global_ns:
1245 1246 self.user_ns.clear()
1246 1247 ns = self.user_global_ns
1247 1248 drop_keys = set(ns.keys())
1248 1249 drop_keys.discard('__builtin__')
1249 1250 drop_keys.discard('__builtins__')
1250 1251 drop_keys.discard('__name__')
1251 1252 for k in drop_keys:
1252 1253 del ns[k]
1253 1254
1254 1255 self.user_ns_hidden.clear()
1255 1256
1256 1257 # Restore the user namespaces to minimal usability
1257 1258 self.init_user_ns()
1258 1259
1259 1260 # Restore the default and user aliases
1260 1261 self.alias_manager.clear_aliases()
1261 1262 self.alias_manager.init_aliases()
1262 1263
1263 1264 # Flush the private list of module references kept for script
1264 1265 # execution protection
1265 1266 self.clear_main_mod_cache()
1266 1267
1267 1268 def del_var(self, varname, by_name=False):
1268 1269 """Delete a variable from the various namespaces, so that, as
1269 1270 far as possible, we're not keeping any hidden references to it.
1270 1271
1271 1272 Parameters
1272 1273 ----------
1273 1274 varname : str
1274 1275 The name of the variable to delete.
1275 1276 by_name : bool
1276 1277 If True, delete variables with the given name in each
1277 1278 namespace. If False (default), find the variable in the user
1278 1279 namespace, and delete references to it.
1279 1280 """
1280 1281 if varname in ('__builtin__', '__builtins__'):
1281 1282 raise ValueError("Refusing to delete %s" % varname)
1282 1283
1283 1284 ns_refs = self.all_ns_refs
1284 1285
1285 1286 if by_name: # Delete by name
1286 1287 for ns in ns_refs:
1287 1288 try:
1288 1289 del ns[varname]
1289 1290 except KeyError:
1290 1291 pass
1291 1292 else: # Delete by object
1292 1293 try:
1293 1294 obj = self.user_ns[varname]
1294 1295 except KeyError:
1295 1296 raise NameError("name '%s' is not defined" % varname)
1296 1297 # Also check in output history
1297 1298 ns_refs.append(self.history_manager.output_hist)
1298 1299 for ns in ns_refs:
1299 1300 to_delete = [n for n, o in iteritems(ns) if o is obj]
1300 1301 for name in to_delete:
1301 1302 del ns[name]
1302 1303
1303 1304 # displayhook keeps extra references, but not in a dictionary
1304 1305 for name in ('_', '__', '___'):
1305 1306 if getattr(self.displayhook, name) is obj:
1306 1307 setattr(self.displayhook, name, None)
1307 1308
1308 1309 def reset_selective(self, regex=None):
1309 1310 """Clear selective variables from internal namespaces based on a
1310 1311 specified regular expression.
1311 1312
1312 1313 Parameters
1313 1314 ----------
1314 1315 regex : string or compiled pattern, optional
1315 1316 A regular expression pattern that will be used in searching
1316 1317 variable names in the users namespaces.
1317 1318 """
1318 1319 if regex is not None:
1319 1320 try:
1320 1321 m = re.compile(regex)
1321 1322 except TypeError:
1322 1323 raise TypeError('regex must be a string or compiled pattern')
1323 1324 # Search for keys in each namespace that match the given regex
1324 1325 # If a match is found, delete the key/value pair.
1325 1326 for ns in self.all_ns_refs:
1326 1327 for var in ns:
1327 1328 if m.search(var):
1328 1329 del ns[var]
1329 1330
1330 1331 def push(self, variables, interactive=True):
1331 1332 """Inject a group of variables into the IPython user namespace.
1332 1333
1333 1334 Parameters
1334 1335 ----------
1335 1336 variables : dict, str or list/tuple of str
1336 1337 The variables to inject into the user's namespace. If a dict, a
1337 1338 simple update is done. If a str, the string is assumed to have
1338 1339 variable names separated by spaces. A list/tuple of str can also
1339 1340 be used to give the variable names. If just the variable names are
1340 1341 give (list/tuple/str) then the variable values looked up in the
1341 1342 callers frame.
1342 1343 interactive : bool
1343 1344 If True (default), the variables will be listed with the ``who``
1344 1345 magic.
1345 1346 """
1346 1347 vdict = None
1347 1348
1348 1349 # We need a dict of name/value pairs to do namespace updates.
1349 1350 if isinstance(variables, dict):
1350 1351 vdict = variables
1351 1352 elif isinstance(variables, string_types+(list, tuple)):
1352 1353 if isinstance(variables, string_types):
1353 1354 vlist = variables.split()
1354 1355 else:
1355 1356 vlist = variables
1356 1357 vdict = {}
1357 1358 cf = sys._getframe(1)
1358 1359 for name in vlist:
1359 1360 try:
1360 1361 vdict[name] = eval(name, cf.f_globals, cf.f_locals)
1361 1362 except:
1362 1363 print('Could not get variable %s from %s' %
1363 1364 (name,cf.f_code.co_name))
1364 1365 else:
1365 1366 raise ValueError('variables must be a dict/str/list/tuple')
1366 1367
1367 1368 # Propagate variables to user namespace
1368 1369 self.user_ns.update(vdict)
1369 1370
1370 1371 # And configure interactive visibility
1371 1372 user_ns_hidden = self.user_ns_hidden
1372 1373 if interactive:
1373 1374 for name in vdict:
1374 1375 user_ns_hidden.pop(name, None)
1375 1376 else:
1376 1377 user_ns_hidden.update(vdict)
1377 1378
1378 1379 def drop_by_id(self, variables):
1379 1380 """Remove a dict of variables from the user namespace, if they are the
1380 1381 same as the values in the dictionary.
1381 1382
1382 1383 This is intended for use by extensions: variables that they've added can
1383 1384 be taken back out if they are unloaded, without removing any that the
1384 1385 user has overwritten.
1385 1386
1386 1387 Parameters
1387 1388 ----------
1388 1389 variables : dict
1389 1390 A dictionary mapping object names (as strings) to the objects.
1390 1391 """
1391 1392 for name, obj in iteritems(variables):
1392 1393 if name in self.user_ns and self.user_ns[name] is obj:
1393 1394 del self.user_ns[name]
1394 1395 self.user_ns_hidden.pop(name, None)
1395 1396
1396 1397 #-------------------------------------------------------------------------
1397 1398 # Things related to object introspection
1398 1399 #-------------------------------------------------------------------------
1399 1400
1400 1401 def _ofind(self, oname, namespaces=None):
1401 1402 """Find an object in the available namespaces.
1402 1403
1403 1404 self._ofind(oname) -> dict with keys: found,obj,ospace,ismagic
1404 1405
1405 1406 Has special code to detect magic functions.
1406 1407 """
1407 1408 oname = oname.strip()
1408 1409 #print '1- oname: <%r>' % oname # dbg
1409 1410 if not oname.startswith(ESC_MAGIC) and \
1410 1411 not oname.startswith(ESC_MAGIC2) and \
1411 1412 not py3compat.isidentifier(oname, dotted=True):
1412 1413 return dict(found=False)
1413 1414
1414 1415 if namespaces is None:
1415 1416 # Namespaces to search in:
1416 1417 # Put them in a list. The order is important so that we
1417 1418 # find things in the same order that Python finds them.
1418 1419 namespaces = [ ('Interactive', self.user_ns),
1419 1420 ('Interactive (global)', self.user_global_ns),
1420 1421 ('Python builtin', builtin_mod.__dict__),
1421 1422 ]
1422 1423
1423 1424 # initialize results to 'null'
1424 1425 found = False; obj = None; ospace = None;
1425 1426 ismagic = False; isalias = False; parent = None
1426 1427
1427 1428 # We need to special-case 'print', which as of python2.6 registers as a
1428 1429 # function but should only be treated as one if print_function was
1429 1430 # loaded with a future import. In this case, just bail.
1430 1431 if (oname == 'print' and not py3compat.PY3 and not \
1431 1432 (self.compile.compiler_flags & __future__.CO_FUTURE_PRINT_FUNCTION)):
1432 1433 return {'found':found, 'obj':obj, 'namespace':ospace,
1433 1434 'ismagic':ismagic, 'isalias':isalias, 'parent':parent}
1434 1435
1435 1436 # Look for the given name by splitting it in parts. If the head is
1436 1437 # found, then we look for all the remaining parts as members, and only
1437 1438 # declare success if we can find them all.
1438 1439 oname_parts = oname.split('.')
1439 1440 oname_head, oname_rest = oname_parts[0],oname_parts[1:]
1440 1441 for nsname,ns in namespaces:
1441 1442 try:
1442 1443 obj = ns[oname_head]
1443 1444 except KeyError:
1444 1445 continue
1445 1446 else:
1446 1447 #print 'oname_rest:', oname_rest # dbg
1447 1448 for idx, part in enumerate(oname_rest):
1448 1449 try:
1449 1450 parent = obj
1450 1451 # The last part is looked up in a special way to avoid
1451 1452 # descriptor invocation as it may raise or have side
1452 1453 # effects.
1453 1454 if idx == len(oname_rest) - 1:
1454 1455 obj = self._getattr_property(obj, part)
1455 1456 else:
1456 1457 obj = getattr(obj, part)
1457 1458 except:
1458 1459 # Blanket except b/c some badly implemented objects
1459 1460 # allow __getattr__ to raise exceptions other than
1460 1461 # AttributeError, which then crashes IPython.
1461 1462 break
1462 1463 else:
1463 1464 # If we finish the for loop (no break), we got all members
1464 1465 found = True
1465 1466 ospace = nsname
1466 1467 break # namespace loop
1467 1468
1468 1469 # Try to see if it's magic
1469 1470 if not found:
1470 1471 obj = None
1471 1472 if oname.startswith(ESC_MAGIC2):
1472 1473 oname = oname.lstrip(ESC_MAGIC2)
1473 1474 obj = self.find_cell_magic(oname)
1474 1475 elif oname.startswith(ESC_MAGIC):
1475 1476 oname = oname.lstrip(ESC_MAGIC)
1476 1477 obj = self.find_line_magic(oname)
1477 1478 else:
1478 1479 # search without prefix, so run? will find %run?
1479 1480 obj = self.find_line_magic(oname)
1480 1481 if obj is None:
1481 1482 obj = self.find_cell_magic(oname)
1482 1483 if obj is not None:
1483 1484 found = True
1484 1485 ospace = 'IPython internal'
1485 1486 ismagic = True
1486 1487 isalias = isinstance(obj, Alias)
1487 1488
1488 1489 # Last try: special-case some literals like '', [], {}, etc:
1489 1490 if not found and oname_head in ["''",'""','[]','{}','()']:
1490 1491 obj = eval(oname_head)
1491 1492 found = True
1492 1493 ospace = 'Interactive'
1493 1494
1494 1495 return {'found':found, 'obj':obj, 'namespace':ospace,
1495 1496 'ismagic':ismagic, 'isalias':isalias, 'parent':parent}
1496 1497
1497 1498 @staticmethod
1498 1499 def _getattr_property(obj, attrname):
1499 1500 """Property-aware getattr to use in object finding.
1500 1501
1501 1502 If attrname represents a property, return it unevaluated (in case it has
1502 1503 side effects or raises an error.
1503 1504
1504 1505 """
1505 1506 if not isinstance(obj, type):
1506 1507 try:
1507 1508 # `getattr(type(obj), attrname)` is not guaranteed to return
1508 1509 # `obj`, but does so for property:
1509 1510 #
1510 1511 # property.__get__(self, None, cls) -> self
1511 1512 #
1512 1513 # The universal alternative is to traverse the mro manually
1513 1514 # searching for attrname in class dicts.
1514 1515 attr = getattr(type(obj), attrname)
1515 1516 except AttributeError:
1516 1517 pass
1517 1518 else:
1518 1519 # This relies on the fact that data descriptors (with both
1519 1520 # __get__ & __set__ magic methods) take precedence over
1520 1521 # instance-level attributes:
1521 1522 #
1522 1523 # class A(object):
1523 1524 # @property
1524 1525 # def foobar(self): return 123
1525 1526 # a = A()
1526 1527 # a.__dict__['foobar'] = 345
1527 1528 # a.foobar # == 123
1528 1529 #
1529 1530 # So, a property may be returned right away.
1530 1531 if isinstance(attr, property):
1531 1532 return attr
1532 1533
1533 1534 # Nothing helped, fall back.
1534 1535 return getattr(obj, attrname)
1535 1536
1536 1537 def _object_find(self, oname, namespaces=None):
1537 1538 """Find an object and return a struct with info about it."""
1538 1539 return Struct(self._ofind(oname, namespaces))
1539 1540
1540 1541 def _inspect(self, meth, oname, namespaces=None, **kw):
1541 1542 """Generic interface to the inspector system.
1542 1543
1543 1544 This function is meant to be called by pdef, pdoc & friends."""
1544 1545 info = self._object_find(oname, namespaces)
1545 1546 if info.found:
1546 1547 pmethod = getattr(self.inspector, meth)
1547 1548 formatter = format_screen if info.ismagic else None
1548 1549 if meth == 'pdoc':
1549 1550 pmethod(info.obj, oname, formatter)
1550 1551 elif meth == 'pinfo':
1551 1552 pmethod(info.obj, oname, formatter, info, **kw)
1552 1553 else:
1553 1554 pmethod(info.obj, oname)
1554 1555 else:
1555 1556 print('Object `%s` not found.' % oname)
1556 1557 return 'not found' # so callers can take other action
1557 1558
1558 1559 def object_inspect(self, oname, detail_level=0):
1559 1560 """Get object info about oname"""
1560 1561 with self.builtin_trap:
1561 1562 info = self._object_find(oname)
1562 1563 if info.found:
1563 1564 return self.inspector.info(info.obj, oname, info=info,
1564 1565 detail_level=detail_level
1565 1566 )
1566 1567 else:
1567 1568 return oinspect.object_info(name=oname, found=False)
1568 1569
1569 1570 def object_inspect_text(self, oname, detail_level=0):
1570 1571 """Get object info as formatted text"""
1571 1572 with self.builtin_trap:
1572 1573 info = self._object_find(oname)
1573 1574 if info.found:
1574 1575 return self.inspector._format_info(info.obj, oname, info=info,
1575 1576 detail_level=detail_level
1576 1577 )
1577 1578 else:
1578 1579 raise KeyError(oname)
1579 1580
1580 1581 #-------------------------------------------------------------------------
1581 1582 # Things related to history management
1582 1583 #-------------------------------------------------------------------------
1583 1584
1584 1585 def init_history(self):
1585 1586 """Sets up the command history, and starts regular autosaves."""
1586 1587 self.history_manager = HistoryManager(shell=self, parent=self)
1587 1588 self.configurables.append(self.history_manager)
1588 1589
1589 1590 #-------------------------------------------------------------------------
1590 1591 # Things related to exception handling and tracebacks (not debugging)
1591 1592 #-------------------------------------------------------------------------
1592 1593
1593 1594 def init_traceback_handlers(self, custom_exceptions):
1594 1595 # Syntax error handler.
1595 1596 self.SyntaxTB = ultratb.SyntaxTB(color_scheme='NoColor')
1596 1597
1597 1598 # The interactive one is initialized with an offset, meaning we always
1598 1599 # want to remove the topmost item in the traceback, which is our own
1599 1600 # internal code. Valid modes: ['Plain','Context','Verbose']
1600 1601 self.InteractiveTB = ultratb.AutoFormattedTB(mode = 'Plain',
1601 1602 color_scheme='NoColor',
1602 1603 tb_offset = 1,
1603 1604 check_cache=check_linecache_ipython)
1604 1605
1605 1606 # The instance will store a pointer to the system-wide exception hook,
1606 1607 # so that runtime code (such as magics) can access it. This is because
1607 1608 # during the read-eval loop, it may get temporarily overwritten.
1608 1609 self.sys_excepthook = sys.excepthook
1609 1610
1610 1611 # and add any custom exception handlers the user may have specified
1611 1612 self.set_custom_exc(*custom_exceptions)
1612 1613
1613 1614 # Set the exception mode
1614 1615 self.InteractiveTB.set_mode(mode=self.xmode)
1615 1616
1616 1617 def set_custom_exc(self, exc_tuple, handler):
1617 1618 """set_custom_exc(exc_tuple,handler)
1618 1619
1619 1620 Set a custom exception handler, which will be called if any of the
1620 1621 exceptions in exc_tuple occur in the mainloop (specifically, in the
1621 1622 run_code() method).
1622 1623
1623 1624 Parameters
1624 1625 ----------
1625 1626
1626 1627 exc_tuple : tuple of exception classes
1627 1628 A *tuple* of exception classes, for which to call the defined
1628 1629 handler. It is very important that you use a tuple, and NOT A
1629 1630 LIST here, because of the way Python's except statement works. If
1630 1631 you only want to trap a single exception, use a singleton tuple::
1631 1632
1632 1633 exc_tuple == (MyCustomException,)
1633 1634
1634 1635 handler : callable
1635 1636 handler must have the following signature::
1636 1637
1637 1638 def my_handler(self, etype, value, tb, tb_offset=None):
1638 1639 ...
1639 1640 return structured_traceback
1640 1641
1641 1642 Your handler must return a structured traceback (a list of strings),
1642 1643 or None.
1643 1644
1644 1645 This will be made into an instance method (via types.MethodType)
1645 1646 of IPython itself, and it will be called if any of the exceptions
1646 1647 listed in the exc_tuple are caught. If the handler is None, an
1647 1648 internal basic one is used, which just prints basic info.
1648 1649
1649 1650 To protect IPython from crashes, if your handler ever raises an
1650 1651 exception or returns an invalid result, it will be immediately
1651 1652 disabled.
1652 1653
1653 1654 WARNING: by putting in your own exception handler into IPython's main
1654 1655 execution loop, you run a very good chance of nasty crashes. This
1655 1656 facility should only be used if you really know what you are doing."""
1656 1657
1657 1658 assert type(exc_tuple)==type(()) , \
1658 1659 "The custom exceptions must be given AS A TUPLE."
1659 1660
1660 1661 def dummy_handler(self,etype,value,tb,tb_offset=None):
1661 1662 print('*** Simple custom exception handler ***')
1662 1663 print('Exception type :',etype)
1663 1664 print('Exception value:',value)
1664 1665 print('Traceback :',tb)
1665 1666 #print 'Source code :','\n'.join(self.buffer)
1666 1667
1667 1668 def validate_stb(stb):
1668 1669 """validate structured traceback return type
1669 1670
1670 1671 return type of CustomTB *should* be a list of strings, but allow
1671 1672 single strings or None, which are harmless.
1672 1673
1673 1674 This function will *always* return a list of strings,
1674 1675 and will raise a TypeError if stb is inappropriate.
1675 1676 """
1676 1677 msg = "CustomTB must return list of strings, not %r" % stb
1677 1678 if stb is None:
1678 1679 return []
1679 1680 elif isinstance(stb, string_types):
1680 1681 return [stb]
1681 1682 elif not isinstance(stb, list):
1682 1683 raise TypeError(msg)
1683 1684 # it's a list
1684 1685 for line in stb:
1685 1686 # check every element
1686 1687 if not isinstance(line, string_types):
1687 1688 raise TypeError(msg)
1688 1689 return stb
1689 1690
1690 1691 if handler is None:
1691 1692 wrapped = dummy_handler
1692 1693 else:
1693 1694 def wrapped(self,etype,value,tb,tb_offset=None):
1694 1695 """wrap CustomTB handler, to protect IPython from user code
1695 1696
1696 1697 This makes it harder (but not impossible) for custom exception
1697 1698 handlers to crash IPython.
1698 1699 """
1699 1700 try:
1700 1701 stb = handler(self,etype,value,tb,tb_offset=tb_offset)
1701 1702 return validate_stb(stb)
1702 1703 except:
1703 1704 # clear custom handler immediately
1704 1705 self.set_custom_exc((), None)
1705 1706 print("Custom TB Handler failed, unregistering", file=io.stderr)
1706 1707 # show the exception in handler first
1707 1708 stb = self.InteractiveTB.structured_traceback(*sys.exc_info())
1708 1709 print(self.InteractiveTB.stb2text(stb), file=io.stdout)
1709 1710 print("The original exception:", file=io.stdout)
1710 1711 stb = self.InteractiveTB.structured_traceback(
1711 1712 (etype,value,tb), tb_offset=tb_offset
1712 1713 )
1713 1714 return stb
1714 1715
1715 1716 self.CustomTB = types.MethodType(wrapped,self)
1716 1717 self.custom_exceptions = exc_tuple
1717 1718
1718 1719 def excepthook(self, etype, value, tb):
1719 1720 """One more defense for GUI apps that call sys.excepthook.
1720 1721
1721 1722 GUI frameworks like wxPython trap exceptions and call
1722 1723 sys.excepthook themselves. I guess this is a feature that
1723 1724 enables them to keep running after exceptions that would
1724 1725 otherwise kill their mainloop. This is a bother for IPython
1725 1726 which excepts to catch all of the program exceptions with a try:
1726 1727 except: statement.
1727 1728
1728 1729 Normally, IPython sets sys.excepthook to a CrashHandler instance, so if
1729 1730 any app directly invokes sys.excepthook, it will look to the user like
1730 1731 IPython crashed. In order to work around this, we can disable the
1731 1732 CrashHandler and replace it with this excepthook instead, which prints a
1732 1733 regular traceback using our InteractiveTB. In this fashion, apps which
1733 1734 call sys.excepthook will generate a regular-looking exception from
1734 1735 IPython, and the CrashHandler will only be triggered by real IPython
1735 1736 crashes.
1736 1737
1737 1738 This hook should be used sparingly, only in places which are not likely
1738 1739 to be true IPython errors.
1739 1740 """
1740 1741 self.showtraceback((etype, value, tb), tb_offset=0)
1741 1742
1742 1743 def _get_exc_info(self, exc_tuple=None):
1743 1744 """get exc_info from a given tuple, sys.exc_info() or sys.last_type etc.
1744 1745
1745 1746 Ensures sys.last_type,value,traceback hold the exc_info we found,
1746 1747 from whichever source.
1747 1748
1748 1749 raises ValueError if none of these contain any information
1749 1750 """
1750 1751 if exc_tuple is None:
1751 1752 etype, value, tb = sys.exc_info()
1752 1753 else:
1753 1754 etype, value, tb = exc_tuple
1754 1755
1755 1756 if etype is None:
1756 1757 if hasattr(sys, 'last_type'):
1757 1758 etype, value, tb = sys.last_type, sys.last_value, \
1758 1759 sys.last_traceback
1759 1760
1760 1761 if etype is None:
1761 1762 raise ValueError("No exception to find")
1762 1763
1763 1764 # Now store the exception info in sys.last_type etc.
1764 1765 # WARNING: these variables are somewhat deprecated and not
1765 1766 # necessarily safe to use in a threaded environment, but tools
1766 1767 # like pdb depend on their existence, so let's set them. If we
1767 1768 # find problems in the field, we'll need to revisit their use.
1768 1769 sys.last_type = etype
1769 1770 sys.last_value = value
1770 1771 sys.last_traceback = tb
1771 1772
1772 1773 return etype, value, tb
1773 1774
1774 1775 def show_usage_error(self, exc):
1775 1776 """Show a short message for UsageErrors
1776 1777
1777 1778 These are special exceptions that shouldn't show a traceback.
1778 1779 """
1779 1780 self.write_err("UsageError: %s" % exc)
1780 1781
1781 1782 def get_exception_only(self, exc_tuple=None):
1782 1783 """
1783 1784 Return as a string (ending with a newline) the exception that
1784 1785 just occurred, without any traceback.
1785 1786 """
1786 1787 etype, value, tb = self._get_exc_info(exc_tuple)
1787 1788 msg = traceback.format_exception_only(etype, value)
1788 1789 return ''.join(msg)
1789 1790
1790 1791 def showtraceback(self, exc_tuple=None, filename=None, tb_offset=None,
1791 1792 exception_only=False):
1792 1793 """Display the exception that just occurred.
1793 1794
1794 1795 If nothing is known about the exception, this is the method which
1795 1796 should be used throughout the code for presenting user tracebacks,
1796 1797 rather than directly invoking the InteractiveTB object.
1797 1798
1798 1799 A specific showsyntaxerror() also exists, but this method can take
1799 1800 care of calling it if needed, so unless you are explicitly catching a
1800 1801 SyntaxError exception, don't try to analyze the stack manually and
1801 1802 simply call this method."""
1802 1803
1803 1804 try:
1804 1805 try:
1805 1806 etype, value, tb = self._get_exc_info(exc_tuple)
1806 1807 except ValueError:
1807 1808 self.write_err('No traceback available to show.\n')
1808 1809 return
1809 1810
1810 1811 if issubclass(etype, SyntaxError):
1811 1812 # Though this won't be called by syntax errors in the input
1812 1813 # line, there may be SyntaxError cases with imported code.
1813 1814 self.showsyntaxerror(filename)
1814 1815 elif etype is UsageError:
1815 1816 self.show_usage_error(value)
1816 1817 else:
1817 1818 if exception_only:
1818 1819 stb = ['An exception has occurred, use %tb to see '
1819 1820 'the full traceback.\n']
1820 1821 stb.extend(self.InteractiveTB.get_exception_only(etype,
1821 1822 value))
1822 1823 else:
1823 1824 try:
1824 1825 # Exception classes can customise their traceback - we
1825 1826 # use this in IPython.parallel for exceptions occurring
1826 1827 # in the engines. This should return a list of strings.
1827 1828 stb = value._render_traceback_()
1828 1829 except Exception:
1829 1830 stb = self.InteractiveTB.structured_traceback(etype,
1830 1831 value, tb, tb_offset=tb_offset)
1831 1832
1832 1833 self._showtraceback(etype, value, stb)
1833 1834 if self.call_pdb:
1834 1835 # drop into debugger
1835 1836 self.debugger(force=True)
1836 1837 return
1837 1838
1838 1839 # Actually show the traceback
1839 1840 self._showtraceback(etype, value, stb)
1840 1841
1841 1842 except KeyboardInterrupt:
1842 1843 self.write_err('\n' + self.get_exception_only())
1843 1844
1844 1845 def _showtraceback(self, etype, evalue, stb):
1845 1846 """Actually show a traceback.
1846 1847
1847 1848 Subclasses may override this method to put the traceback on a different
1848 1849 place, like a side channel.
1849 1850 """
1850 1851 print(self.InteractiveTB.stb2text(stb), file=io.stdout)
1851 1852
1852 1853 def showsyntaxerror(self, filename=None):
1853 1854 """Display the syntax error that just occurred.
1854 1855
1855 1856 This doesn't display a stack trace because there isn't one.
1856 1857
1857 1858 If a filename is given, it is stuffed in the exception instead
1858 1859 of what was there before (because Python's parser always uses
1859 1860 "<string>" when reading from a string).
1860 1861 """
1861 1862 etype, value, last_traceback = self._get_exc_info()
1862 1863
1863 1864 if filename and issubclass(etype, SyntaxError):
1864 1865 try:
1865 1866 value.filename = filename
1866 1867 except:
1867 1868 # Not the format we expect; leave it alone
1868 1869 pass
1869 1870
1870 1871 stb = self.SyntaxTB.structured_traceback(etype, value, [])
1871 1872 self._showtraceback(etype, value, stb)
1872 1873
1873 1874 # This is overridden in TerminalInteractiveShell to show a message about
1874 1875 # the %paste magic.
1875 1876 def showindentationerror(self):
1876 1877 """Called by run_cell when there's an IndentationError in code entered
1877 1878 at the prompt.
1878 1879
1879 1880 This is overridden in TerminalInteractiveShell to show a message about
1880 1881 the %paste magic."""
1881 1882 self.showsyntaxerror()
1882 1883
1883 1884 #-------------------------------------------------------------------------
1884 1885 # Things related to readline
1885 1886 #-------------------------------------------------------------------------
1886 1887
1887 1888 def init_readline(self):
1888 1889 """Moved to terminal subclass, here only to simplify the init logic."""
1889 1890 self.readline = None
1890 1891 # Set a number of methods that depend on readline to be no-op
1891 1892 self.readline_no_record = NoOpContext()
1892 1893 self.set_readline_completer = no_op
1893 1894 self.set_custom_completer = no_op
1894 1895
1895 1896 @skip_doctest
1896 1897 def set_next_input(self, s, replace=False):
1897 1898 """ Sets the 'default' input string for the next command line.
1898 1899
1899 1900 Example::
1900 1901
1901 1902 In [1]: _ip.set_next_input("Hello Word")
1902 1903 In [2]: Hello Word_ # cursor is here
1903 1904 """
1904 1905 self.rl_next_input = py3compat.cast_bytes_py2(s)
1905 1906
1906 1907 def _indent_current_str(self):
1907 1908 """return the current level of indentation as a string"""
1908 1909 return self.input_splitter.indent_spaces * ' '
1909 1910
1910 1911 #-------------------------------------------------------------------------
1911 1912 # Things related to text completion
1912 1913 #-------------------------------------------------------------------------
1913 1914
1914 1915 def init_completer(self):
1915 1916 """Initialize the completion machinery.
1916 1917
1917 1918 This creates completion machinery that can be used by client code,
1918 1919 either interactively in-process (typically triggered by the readline
1919 1920 library), programmatically (such as in test suites) or out-of-process
1920 1921 (typically over the network by remote frontends).
1921 1922 """
1922 1923 from IPython.core.completer import IPCompleter
1923 1924 from IPython.core.completerlib import (module_completer,
1924 1925 magic_run_completer, cd_completer, reset_completer)
1925 1926
1926 1927 self.Completer = IPCompleter(shell=self,
1927 1928 namespace=self.user_ns,
1928 1929 global_namespace=self.user_global_ns,
1929 1930 use_readline=self.has_readline,
1930 1931 parent=self,
1931 1932 )
1932 1933 self.configurables.append(self.Completer)
1933 1934
1934 1935 # Add custom completers to the basic ones built into IPCompleter
1935 1936 sdisp = self.strdispatchers.get('complete_command', StrDispatch())
1936 1937 self.strdispatchers['complete_command'] = sdisp
1937 1938 self.Completer.custom_completers = sdisp
1938 1939
1939 1940 self.set_hook('complete_command', module_completer, str_key = 'import')
1940 1941 self.set_hook('complete_command', module_completer, str_key = 'from')
1941 1942 self.set_hook('complete_command', module_completer, str_key = '%aimport')
1942 1943 self.set_hook('complete_command', magic_run_completer, str_key = '%run')
1943 1944 self.set_hook('complete_command', cd_completer, str_key = '%cd')
1944 1945 self.set_hook('complete_command', reset_completer, str_key = '%reset')
1945 1946
1946 1947
1947 1948 def complete(self, text, line=None, cursor_pos=None):
1948 1949 """Return the completed text and a list of completions.
1949 1950
1950 1951 Parameters
1951 1952 ----------
1952 1953
1953 1954 text : string
1954 1955 A string of text to be completed on. It can be given as empty and
1955 1956 instead a line/position pair are given. In this case, the
1956 1957 completer itself will split the line like readline does.
1957 1958
1958 1959 line : string, optional
1959 1960 The complete line that text is part of.
1960 1961
1961 1962 cursor_pos : int, optional
1962 1963 The position of the cursor on the input line.
1963 1964
1964 1965 Returns
1965 1966 -------
1966 1967 text : string
1967 1968 The actual text that was completed.
1968 1969
1969 1970 matches : list
1970 1971 A sorted list with all possible completions.
1971 1972
1972 1973 The optional arguments allow the completion to take more context into
1973 1974 account, and are part of the low-level completion API.
1974 1975
1975 1976 This is a wrapper around the completion mechanism, similar to what
1976 1977 readline does at the command line when the TAB key is hit. By
1977 1978 exposing it as a method, it can be used by other non-readline
1978 1979 environments (such as GUIs) for text completion.
1979 1980
1980 1981 Simple usage example:
1981 1982
1982 1983 In [1]: x = 'hello'
1983 1984
1984 1985 In [2]: _ip.complete('x.l')
1985 1986 Out[2]: ('x.l', ['x.ljust', 'x.lower', 'x.lstrip'])
1986 1987 """
1987 1988
1988 1989 # Inject names into __builtin__ so we can complete on the added names.
1989 1990 with self.builtin_trap:
1990 1991 return self.Completer.complete(text, line, cursor_pos)
1991 1992
1992 1993 def set_custom_completer(self, completer, pos=0):
1993 1994 """Adds a new custom completer function.
1994 1995
1995 1996 The position argument (defaults to 0) is the index in the completers
1996 1997 list where you want the completer to be inserted."""
1997 1998
1998 1999 newcomp = types.MethodType(completer,self.Completer)
1999 2000 self.Completer.matchers.insert(pos,newcomp)
2000 2001
2001 2002 def set_completer_frame(self, frame=None):
2002 2003 """Set the frame of the completer."""
2003 2004 if frame:
2004 2005 self.Completer.namespace = frame.f_locals
2005 2006 self.Completer.global_namespace = frame.f_globals
2006 2007 else:
2007 2008 self.Completer.namespace = self.user_ns
2008 2009 self.Completer.global_namespace = self.user_global_ns
2009 2010
2010 2011 #-------------------------------------------------------------------------
2011 2012 # Things related to magics
2012 2013 #-------------------------------------------------------------------------
2013 2014
2014 2015 def init_magics(self):
2015 2016 from IPython.core import magics as m
2016 2017 self.magics_manager = magic.MagicsManager(shell=self,
2017 2018 parent=self,
2018 2019 user_magics=m.UserMagics(self))
2019 2020 self.configurables.append(self.magics_manager)
2020 2021
2021 2022 # Expose as public API from the magics manager
2022 2023 self.register_magics = self.magics_manager.register
2023 2024 self.define_magic = self.magics_manager.define_magic
2024 2025
2025 2026 self.register_magics(m.AutoMagics, m.BasicMagics, m.CodeMagics,
2026 2027 m.ConfigMagics, m.DeprecatedMagics, m.DisplayMagics, m.ExecutionMagics,
2027 2028 m.ExtensionMagics, m.HistoryMagics, m.LoggingMagics,
2028 2029 m.NamespaceMagics, m.OSMagics, m.PylabMagics, m.ScriptMagics,
2029 2030 )
2030 2031
2031 2032 # Register Magic Aliases
2032 2033 mman = self.magics_manager
2033 2034 # FIXME: magic aliases should be defined by the Magics classes
2034 2035 # or in MagicsManager, not here
2035 2036 mman.register_alias('ed', 'edit')
2036 2037 mman.register_alias('hist', 'history')
2037 2038 mman.register_alias('rep', 'recall')
2038 2039 mman.register_alias('SVG', 'svg', 'cell')
2039 2040 mman.register_alias('HTML', 'html', 'cell')
2040 2041 mman.register_alias('file', 'writefile', 'cell')
2041 2042
2042 2043 # FIXME: Move the color initialization to the DisplayHook, which
2043 2044 # should be split into a prompt manager and displayhook. We probably
2044 2045 # even need a centralize colors management object.
2045 2046 self.magic('colors %s' % self.colors)
2046 2047
2047 2048 # Defined here so that it's included in the documentation
2048 2049 @functools.wraps(magic.MagicsManager.register_function)
2049 2050 def register_magic_function(self, func, magic_kind='line', magic_name=None):
2050 2051 self.magics_manager.register_function(func,
2051 2052 magic_kind=magic_kind, magic_name=magic_name)
2052 2053
2053 2054 def run_line_magic(self, magic_name, line):
2054 2055 """Execute the given line magic.
2055 2056
2056 2057 Parameters
2057 2058 ----------
2058 2059 magic_name : str
2059 2060 Name of the desired magic function, without '%' prefix.
2060 2061
2061 2062 line : str
2062 2063 The rest of the input line as a single string.
2063 2064 """
2064 2065 fn = self.find_line_magic(magic_name)
2065 2066 if fn is None:
2066 2067 cm = self.find_cell_magic(magic_name)
2067 2068 etpl = "Line magic function `%%%s` not found%s."
2068 2069 extra = '' if cm is None else (' (But cell magic `%%%%%s` exists, '
2069 2070 'did you mean that instead?)' % magic_name )
2070 2071 error(etpl % (magic_name, extra))
2071 2072 else:
2072 2073 # Note: this is the distance in the stack to the user's frame.
2073 2074 # This will need to be updated if the internal calling logic gets
2074 2075 # refactored, or else we'll be expanding the wrong variables.
2075 2076 stack_depth = 2
2076 2077 magic_arg_s = self.var_expand(line, stack_depth)
2077 2078 # Put magic args in a list so we can call with f(*a) syntax
2078 2079 args = [magic_arg_s]
2079 2080 kwargs = {}
2080 2081 # Grab local namespace if we need it:
2081 2082 if getattr(fn, "needs_local_scope", False):
2082 2083 kwargs['local_ns'] = sys._getframe(stack_depth).f_locals
2083 2084 with self.builtin_trap:
2084 2085 result = fn(*args,**kwargs)
2085 2086 return result
2086 2087
2087 2088 def run_cell_magic(self, magic_name, line, cell):
2088 2089 """Execute the given cell magic.
2089 2090
2090 2091 Parameters
2091 2092 ----------
2092 2093 magic_name : str
2093 2094 Name of the desired magic function, without '%' prefix.
2094 2095
2095 2096 line : str
2096 2097 The rest of the first input line as a single string.
2097 2098
2098 2099 cell : str
2099 2100 The body of the cell as a (possibly multiline) string.
2100 2101 """
2101 2102 fn = self.find_cell_magic(magic_name)
2102 2103 if fn is None:
2103 2104 lm = self.find_line_magic(magic_name)
2104 2105 etpl = "Cell magic `%%{0}` not found{1}."
2105 2106 extra = '' if lm is None else (' (But line magic `%{0}` exists, '
2106 2107 'did you mean that instead?)'.format(magic_name))
2107 2108 error(etpl.format(magic_name, extra))
2108 2109 elif cell == '':
2109 2110 message = '%%{0} is a cell magic, but the cell body is empty.'.format(magic_name)
2110 2111 if self.find_line_magic(magic_name) is not None:
2111 2112 message += ' Did you mean the line magic %{0} (single %)?'.format(magic_name)
2112 2113 raise UsageError(message)
2113 2114 else:
2114 2115 # Note: this is the distance in the stack to the user's frame.
2115 2116 # This will need to be updated if the internal calling logic gets
2116 2117 # refactored, or else we'll be expanding the wrong variables.
2117 2118 stack_depth = 2
2118 2119 magic_arg_s = self.var_expand(line, stack_depth)
2119 2120 with self.builtin_trap:
2120 2121 result = fn(magic_arg_s, cell)
2121 2122 return result
2122 2123
2123 2124 def find_line_magic(self, magic_name):
2124 2125 """Find and return a line magic by name.
2125 2126
2126 2127 Returns None if the magic isn't found."""
2127 2128 return self.magics_manager.magics['line'].get(magic_name)
2128 2129
2129 2130 def find_cell_magic(self, magic_name):
2130 2131 """Find and return a cell magic by name.
2131 2132
2132 2133 Returns None if the magic isn't found."""
2133 2134 return self.magics_manager.magics['cell'].get(magic_name)
2134 2135
2135 2136 def find_magic(self, magic_name, magic_kind='line'):
2136 2137 """Find and return a magic of the given type by name.
2137 2138
2138 2139 Returns None if the magic isn't found."""
2139 2140 return self.magics_manager.magics[magic_kind].get(magic_name)
2140 2141
2141 2142 def magic(self, arg_s):
2142 2143 """DEPRECATED. Use run_line_magic() instead.
2143 2144
2144 2145 Call a magic function by name.
2145 2146
2146 2147 Input: a string containing the name of the magic function to call and
2147 2148 any additional arguments to be passed to the magic.
2148 2149
2149 2150 magic('name -opt foo bar') is equivalent to typing at the ipython
2150 2151 prompt:
2151 2152
2152 2153 In[1]: %name -opt foo bar
2153 2154
2154 2155 To call a magic without arguments, simply use magic('name').
2155 2156
2156 2157 This provides a proper Python function to call IPython's magics in any
2157 2158 valid Python code you can type at the interpreter, including loops and
2158 2159 compound statements.
2159 2160 """
2160 2161 # TODO: should we issue a loud deprecation warning here?
2161 2162 magic_name, _, magic_arg_s = arg_s.partition(' ')
2162 2163 magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
2163 2164 return self.run_line_magic(magic_name, magic_arg_s)
2164 2165
2165 2166 #-------------------------------------------------------------------------
2166 2167 # Things related to macros
2167 2168 #-------------------------------------------------------------------------
2168 2169
2169 2170 def define_macro(self, name, themacro):
2170 2171 """Define a new macro
2171 2172
2172 2173 Parameters
2173 2174 ----------
2174 2175 name : str
2175 2176 The name of the macro.
2176 2177 themacro : str or Macro
2177 2178 The action to do upon invoking the macro. If a string, a new
2178 2179 Macro object is created by passing the string to it.
2179 2180 """
2180 2181
2181 2182 from IPython.core import macro
2182 2183
2183 2184 if isinstance(themacro, string_types):
2184 2185 themacro = macro.Macro(themacro)
2185 2186 if not isinstance(themacro, macro.Macro):
2186 2187 raise ValueError('A macro must be a string or a Macro instance.')
2187 2188 self.user_ns[name] = themacro
2188 2189
2189 2190 #-------------------------------------------------------------------------
2190 2191 # Things related to the running of system commands
2191 2192 #-------------------------------------------------------------------------
2192 2193
2193 2194 def system_piped(self, cmd):
2194 2195 """Call the given cmd in a subprocess, piping stdout/err
2195 2196
2196 2197 Parameters
2197 2198 ----------
2198 2199 cmd : str
2199 2200 Command to execute (can not end in '&', as background processes are
2200 2201 not supported. Should not be a command that expects input
2201 2202 other than simple text.
2202 2203 """
2203 2204 if cmd.rstrip().endswith('&'):
2204 2205 # this is *far* from a rigorous test
2205 2206 # We do not support backgrounding processes because we either use
2206 2207 # pexpect or pipes to read from. Users can always just call
2207 2208 # os.system() or use ip.system=ip.system_raw
2208 2209 # if they really want a background process.
2209 2210 raise OSError("Background processes not supported.")
2210 2211
2211 2212 # we explicitly do NOT return the subprocess status code, because
2212 2213 # a non-None value would trigger :func:`sys.displayhook` calls.
2213 2214 # Instead, we store the exit_code in user_ns.
2214 2215 self.user_ns['_exit_code'] = system(self.var_expand(cmd, depth=1))
2215 2216
2216 2217 def system_raw(self, cmd):
2217 2218 """Call the given cmd in a subprocess using os.system on Windows or
2218 2219 subprocess.call using the system shell on other platforms.
2219 2220
2220 2221 Parameters
2221 2222 ----------
2222 2223 cmd : str
2223 2224 Command to execute.
2224 2225 """
2225 2226 cmd = self.var_expand(cmd, depth=1)
2226 2227 # protect os.system from UNC paths on Windows, which it can't handle:
2227 2228 if sys.platform == 'win32':
2228 2229 from IPython.utils._process_win32 import AvoidUNCPath
2229 2230 with AvoidUNCPath() as path:
2230 2231 if path is not None:
2231 2232 cmd = '"pushd %s &&"%s' % (path, cmd)
2232 2233 cmd = py3compat.unicode_to_str(cmd)
2233 2234 try:
2234 2235 ec = os.system(cmd)
2235 2236 except KeyboardInterrupt:
2236 2237 self.write_err('\n' + self.get_exception_only())
2237 2238 ec = -2
2238 2239 else:
2239 2240 cmd = py3compat.unicode_to_str(cmd)
2240 2241 # For posix the result of the subprocess.call() below is an exit
2241 2242 # code, which by convention is zero for success, positive for
2242 2243 # program failure. Exit codes above 128 are reserved for signals,
2243 2244 # and the formula for converting a signal to an exit code is usually
2244 2245 # signal_number+128. To more easily differentiate between exit
2245 2246 # codes and signals, ipython uses negative numbers. For instance
2246 2247 # since control-c is signal 2 but exit code 130, ipython's
2247 2248 # _exit_code variable will read -2. Note that some shells like
2248 2249 # csh and fish don't follow sh/bash conventions for exit codes.
2249 2250 executable = os.environ.get('SHELL', None)
2250 2251 try:
2251 2252 # Use env shell instead of default /bin/sh
2252 2253 ec = subprocess.call(cmd, shell=True, executable=executable)
2253 2254 except KeyboardInterrupt:
2254 2255 # intercept control-C; a long traceback is not useful here
2255 2256 self.write_err('\n' + self.get_exception_only())
2256 2257 ec = 130
2257 2258 if ec > 128:
2258 2259 ec = -(ec - 128)
2259 2260
2260 2261 # We explicitly do NOT return the subprocess status code, because
2261 2262 # a non-None value would trigger :func:`sys.displayhook` calls.
2262 2263 # Instead, we store the exit_code in user_ns. Note the semantics
2263 2264 # of _exit_code: for control-c, _exit_code == -signal.SIGNIT,
2264 2265 # but raising SystemExit(_exit_code) will give status 254!
2265 2266 self.user_ns['_exit_code'] = ec
2266 2267
2267 2268 # use piped system by default, because it is better behaved
2268 2269 system = system_piped
2269 2270
2270 2271 def getoutput(self, cmd, split=True, depth=0):
2271 2272 """Get output (possibly including stderr) from a subprocess.
2272 2273
2273 2274 Parameters
2274 2275 ----------
2275 2276 cmd : str
2276 2277 Command to execute (can not end in '&', as background processes are
2277 2278 not supported.
2278 2279 split : bool, optional
2279 2280 If True, split the output into an IPython SList. Otherwise, an
2280 2281 IPython LSString is returned. These are objects similar to normal
2281 2282 lists and strings, with a few convenience attributes for easier
2282 2283 manipulation of line-based output. You can use '?' on them for
2283 2284 details.
2284 2285 depth : int, optional
2285 2286 How many frames above the caller are the local variables which should
2286 2287 be expanded in the command string? The default (0) assumes that the
2287 2288 expansion variables are in the stack frame calling this function.
2288 2289 """
2289 2290 if cmd.rstrip().endswith('&'):
2290 2291 # this is *far* from a rigorous test
2291 2292 raise OSError("Background processes not supported.")
2292 2293 out = getoutput(self.var_expand(cmd, depth=depth+1))
2293 2294 if split:
2294 2295 out = SList(out.splitlines())
2295 2296 else:
2296 2297 out = LSString(out)
2297 2298 return out
2298 2299
2299 2300 #-------------------------------------------------------------------------
2300 2301 # Things related to aliases
2301 2302 #-------------------------------------------------------------------------
2302 2303
2303 2304 def init_alias(self):
2304 2305 self.alias_manager = AliasManager(shell=self, parent=self)
2305 2306 self.configurables.append(self.alias_manager)
2306 2307
2307 2308 #-------------------------------------------------------------------------
2308 2309 # Things related to extensions
2309 2310 #-------------------------------------------------------------------------
2310 2311
2311 2312 def init_extension_manager(self):
2312 2313 self.extension_manager = ExtensionManager(shell=self, parent=self)
2313 2314 self.configurables.append(self.extension_manager)
2314 2315
2315 2316 #-------------------------------------------------------------------------
2316 2317 # Things related to payloads
2317 2318 #-------------------------------------------------------------------------
2318 2319
2319 2320 def init_payload(self):
2320 2321 self.payload_manager = PayloadManager(parent=self)
2321 2322 self.configurables.append(self.payload_manager)
2322 2323
2323 2324 #-------------------------------------------------------------------------
2324 2325 # Things related to the prefilter
2325 2326 #-------------------------------------------------------------------------
2326 2327
2327 2328 def init_prefilter(self):
2328 2329 self.prefilter_manager = PrefilterManager(shell=self, parent=self)
2329 2330 self.configurables.append(self.prefilter_manager)
2330 2331 # Ultimately this will be refactored in the new interpreter code, but
2331 2332 # for now, we should expose the main prefilter method (there's legacy
2332 2333 # code out there that may rely on this).
2333 2334 self.prefilter = self.prefilter_manager.prefilter_lines
2334 2335
2335 2336 def auto_rewrite_input(self, cmd):
2336 2337 """Print to the screen the rewritten form of the user's command.
2337 2338
2338 2339 This shows visual feedback by rewriting input lines that cause
2339 2340 automatic calling to kick in, like::
2340 2341
2341 2342 /f x
2342 2343
2343 2344 into::
2344 2345
2345 2346 ------> f(x)
2346 2347
2347 2348 after the user's input prompt. This helps the user understand that the
2348 2349 input line was transformed automatically by IPython.
2349 2350 """
2350 2351 if not self.show_rewritten_input:
2351 2352 return
2352 2353
2353 2354 rw = self.prompt_manager.render('rewrite') + cmd
2354 2355
2355 2356 try:
2356 2357 # plain ascii works better w/ pyreadline, on some machines, so
2357 2358 # we use it and only print uncolored rewrite if we have unicode
2358 2359 rw = str(rw)
2359 2360 print(rw, file=io.stdout)
2360 2361 except UnicodeEncodeError:
2361 2362 print("------> " + cmd)
2362 2363
2363 2364 #-------------------------------------------------------------------------
2364 2365 # Things related to extracting values/expressions from kernel and user_ns
2365 2366 #-------------------------------------------------------------------------
2366 2367
2367 2368 def _user_obj_error(self):
2368 2369 """return simple exception dict
2369 2370
2370 2371 for use in user_expressions
2371 2372 """
2372 2373
2373 2374 etype, evalue, tb = self._get_exc_info()
2374 2375 stb = self.InteractiveTB.get_exception_only(etype, evalue)
2375 2376
2376 2377 exc_info = {
2377 2378 u'status' : 'error',
2378 2379 u'traceback' : stb,
2379 2380 u'ename' : unicode_type(etype.__name__),
2380 2381 u'evalue' : py3compat.safe_unicode(evalue),
2381 2382 }
2382 2383
2383 2384 return exc_info
2384 2385
2385 2386 def _format_user_obj(self, obj):
2386 2387 """format a user object to display dict
2387 2388
2388 2389 for use in user_expressions
2389 2390 """
2390 2391
2391 2392 data, md = self.display_formatter.format(obj)
2392 2393 value = {
2393 2394 'status' : 'ok',
2394 2395 'data' : data,
2395 2396 'metadata' : md,
2396 2397 }
2397 2398 return value
2398 2399
2399 2400 def user_expressions(self, expressions):
2400 2401 """Evaluate a dict of expressions in the user's namespace.
2401 2402
2402 2403 Parameters
2403 2404 ----------
2404 2405 expressions : dict
2405 2406 A dict with string keys and string values. The expression values
2406 2407 should be valid Python expressions, each of which will be evaluated
2407 2408 in the user namespace.
2408 2409
2409 2410 Returns
2410 2411 -------
2411 2412 A dict, keyed like the input expressions dict, with the rich mime-typed
2412 2413 display_data of each value.
2413 2414 """
2414 2415 out = {}
2415 2416 user_ns = self.user_ns
2416 2417 global_ns = self.user_global_ns
2417 2418
2418 2419 for key, expr in iteritems(expressions):
2419 2420 try:
2420 2421 value = self._format_user_obj(eval(expr, global_ns, user_ns))
2421 2422 except:
2422 2423 value = self._user_obj_error()
2423 2424 out[key] = value
2424 2425 return out
2425 2426
2426 2427 #-------------------------------------------------------------------------
2427 2428 # Things related to the running of code
2428 2429 #-------------------------------------------------------------------------
2429 2430
2430 2431 def ex(self, cmd):
2431 2432 """Execute a normal python statement in user namespace."""
2432 2433 with self.builtin_trap:
2433 2434 exec(cmd, self.user_global_ns, self.user_ns)
2434 2435
2435 2436 def ev(self, expr):
2436 2437 """Evaluate python expression expr in user namespace.
2437 2438
2438 2439 Returns the result of evaluation
2439 2440 """
2440 2441 with self.builtin_trap:
2441 2442 return eval(expr, self.user_global_ns, self.user_ns)
2442 2443
2443 2444 def safe_execfile(self, fname, *where, **kw):
2444 2445 """A safe version of the builtin execfile().
2445 2446
2446 2447 This version will never throw an exception, but instead print
2447 2448 helpful error messages to the screen. This only works on pure
2448 2449 Python files with the .py extension.
2449 2450
2450 2451 Parameters
2451 2452 ----------
2452 2453 fname : string
2453 2454 The name of the file to be executed.
2454 2455 where : tuple
2455 2456 One or two namespaces, passed to execfile() as (globals,locals).
2456 2457 If only one is given, it is passed as both.
2457 2458 exit_ignore : bool (False)
2458 2459 If True, then silence SystemExit for non-zero status (it is always
2459 2460 silenced for zero status, as it is so common).
2460 2461 raise_exceptions : bool (False)
2461 2462 If True raise exceptions everywhere. Meant for testing.
2462 2463 shell_futures : bool (False)
2463 2464 If True, the code will share future statements with the interactive
2464 2465 shell. It will both be affected by previous __future__ imports, and
2465 2466 any __future__ imports in the code will affect the shell. If False,
2466 2467 __future__ imports are not shared in either direction.
2467 2468
2468 2469 """
2469 2470 kw.setdefault('exit_ignore', False)
2470 2471 kw.setdefault('raise_exceptions', False)
2471 2472 kw.setdefault('shell_futures', False)
2472 2473
2473 2474 fname = os.path.abspath(os.path.expanduser(fname))
2474 2475
2475 2476 # Make sure we can open the file
2476 2477 try:
2477 2478 with open(fname):
2478 2479 pass
2479 2480 except:
2480 2481 warn('Could not open file <%s> for safe execution.' % fname)
2481 2482 return
2482 2483
2483 2484 # Find things also in current directory. This is needed to mimic the
2484 2485 # behavior of running a script from the system command line, where
2485 2486 # Python inserts the script's directory into sys.path
2486 2487 dname = os.path.dirname(fname)
2487 2488
2488 2489 with prepended_to_syspath(dname):
2489 2490 try:
2490 2491 glob, loc = (where + (None, ))[:2]
2491 2492 py3compat.execfile(
2492 2493 fname, glob, loc,
2493 2494 self.compile if kw['shell_futures'] else None)
2494 2495 except SystemExit as status:
2495 2496 # If the call was made with 0 or None exit status (sys.exit(0)
2496 2497 # or sys.exit() ), don't bother showing a traceback, as both of
2497 2498 # these are considered normal by the OS:
2498 2499 # > python -c'import sys;sys.exit(0)'; echo $?
2499 2500 # 0
2500 2501 # > python -c'import sys;sys.exit()'; echo $?
2501 2502 # 0
2502 2503 # For other exit status, we show the exception unless
2503 2504 # explicitly silenced, but only in short form.
2504 2505 if status.code:
2505 2506 if kw['raise_exceptions']:
2506 2507 raise
2507 2508 if not kw['exit_ignore']:
2508 2509 self.showtraceback(exception_only=True)
2509 2510 except:
2510 2511 if kw['raise_exceptions']:
2511 2512 raise
2512 2513 # tb offset is 2 because we wrap execfile
2513 2514 self.showtraceback(tb_offset=2)
2514 2515
2515 2516 def safe_execfile_ipy(self, fname, shell_futures=False, raise_exceptions=False):
2516 2517 """Like safe_execfile, but for .ipy or .ipynb files with IPython syntax.
2517 2518
2518 2519 Parameters
2519 2520 ----------
2520 2521 fname : str
2521 2522 The name of the file to execute. The filename must have a
2522 2523 .ipy or .ipynb extension.
2523 2524 shell_futures : bool (False)
2524 2525 If True, the code will share future statements with the interactive
2525 2526 shell. It will both be affected by previous __future__ imports, and
2526 2527 any __future__ imports in the code will affect the shell. If False,
2527 2528 __future__ imports are not shared in either direction.
2528 2529 raise_exceptions : bool (False)
2529 2530 If True raise exceptions everywhere. Meant for testing.
2530 2531 """
2531 2532 fname = os.path.abspath(os.path.expanduser(fname))
2532 2533
2533 2534 # Make sure we can open the file
2534 2535 try:
2535 2536 with open(fname):
2536 2537 pass
2537 2538 except:
2538 2539 warn('Could not open file <%s> for safe execution.' % fname)
2539 2540 return
2540 2541
2541 2542 # Find things also in current directory. This is needed to mimic the
2542 2543 # behavior of running a script from the system command line, where
2543 2544 # Python inserts the script's directory into sys.path
2544 2545 dname = os.path.dirname(fname)
2545 2546
2546 2547 def get_cells():
2547 2548 """generator for sequence of code blocks to run"""
2548 2549 if fname.endswith('.ipynb'):
2549 2550 from nbformat import read
2550 2551 with io_open(fname) as f:
2551 2552 nb = read(f, as_version=4)
2552 2553 if not nb.cells:
2553 2554 return
2554 2555 for cell in nb.cells:
2555 2556 if cell.cell_type == 'code':
2556 2557 yield cell.source
2557 2558 else:
2558 2559 with open(fname) as f:
2559 2560 yield f.read()
2560 2561
2561 2562 with prepended_to_syspath(dname):
2562 2563 try:
2563 2564 for cell in get_cells():
2564 2565 result = self.run_cell(cell, silent=True, shell_futures=shell_futures)
2565 2566 if raise_exceptions:
2566 2567 result.raise_error()
2567 2568 elif not result.success:
2568 2569 break
2569 2570 except:
2570 2571 if raise_exceptions:
2571 2572 raise
2572 2573 self.showtraceback()
2573 2574 warn('Unknown failure executing file: <%s>' % fname)
2574 2575
2575 2576 def safe_run_module(self, mod_name, where):
2576 2577 """A safe version of runpy.run_module().
2577 2578
2578 2579 This version will never throw an exception, but instead print
2579 2580 helpful error messages to the screen.
2580 2581
2581 2582 `SystemExit` exceptions with status code 0 or None are ignored.
2582 2583
2583 2584 Parameters
2584 2585 ----------
2585 2586 mod_name : string
2586 2587 The name of the module to be executed.
2587 2588 where : dict
2588 2589 The globals namespace.
2589 2590 """
2590 2591 try:
2591 2592 try:
2592 2593 where.update(
2593 2594 runpy.run_module(str(mod_name), run_name="__main__",
2594 2595 alter_sys=True)
2595 2596 )
2596 2597 except SystemExit as status:
2597 2598 if status.code:
2598 2599 raise
2599 2600 except:
2600 2601 self.showtraceback()
2601 2602 warn('Unknown failure executing module: <%s>' % mod_name)
2602 2603
2603 2604 def run_cell(self, raw_cell, store_history=False, silent=False, shell_futures=True):
2604 2605 """Run a complete IPython cell.
2605 2606
2606 2607 Parameters
2607 2608 ----------
2608 2609 raw_cell : str
2609 2610 The code (including IPython code such as %magic functions) to run.
2610 2611 store_history : bool
2611 2612 If True, the raw and translated cell will be stored in IPython's
2612 2613 history. For user code calling back into IPython's machinery, this
2613 2614 should be set to False.
2614 2615 silent : bool
2615 2616 If True, avoid side-effects, such as implicit displayhooks and
2616 2617 and logging. silent=True forces store_history=False.
2617 2618 shell_futures : bool
2618 2619 If True, the code will share future statements with the interactive
2619 2620 shell. It will both be affected by previous __future__ imports, and
2620 2621 any __future__ imports in the code will affect the shell. If False,
2621 2622 __future__ imports are not shared in either direction.
2622 2623
2623 2624 Returns
2624 2625 -------
2625 2626 result : :class:`ExecutionResult`
2626 2627 """
2627 2628 result = ExecutionResult()
2628 2629
2629 2630 if (not raw_cell) or raw_cell.isspace():
2630 2631 return result
2631 2632
2632 2633 if silent:
2633 2634 store_history = False
2634 2635
2635 2636 if store_history:
2636 2637 result.execution_count = self.execution_count
2637 2638
2638 2639 def error_before_exec(value):
2639 2640 result.error_before_exec = value
2640 2641 return result
2641 2642
2642 2643 self.events.trigger('pre_execute')
2643 2644 if not silent:
2644 2645 self.events.trigger('pre_run_cell')
2645 2646
2646 2647 # If any of our input transformation (input_transformer_manager or
2647 2648 # prefilter_manager) raises an exception, we store it in this variable
2648 2649 # so that we can display the error after logging the input and storing
2649 2650 # it in the history.
2650 2651 preprocessing_exc_tuple = None
2651 2652 try:
2652 2653 # Static input transformations
2653 2654 cell = self.input_transformer_manager.transform_cell(raw_cell)
2654 2655 except SyntaxError:
2655 2656 preprocessing_exc_tuple = sys.exc_info()
2656 2657 cell = raw_cell # cell has to exist so it can be stored/logged
2657 2658 else:
2658 2659 if len(cell.splitlines()) == 1:
2659 2660 # Dynamic transformations - only applied for single line commands
2660 2661 with self.builtin_trap:
2661 2662 try:
2662 2663 # use prefilter_lines to handle trailing newlines
2663 2664 # restore trailing newline for ast.parse
2664 2665 cell = self.prefilter_manager.prefilter_lines(cell) + '\n'
2665 2666 except Exception:
2666 2667 # don't allow prefilter errors to crash IPython
2667 2668 preprocessing_exc_tuple = sys.exc_info()
2668 2669
2669 2670 # Store raw and processed history
2670 2671 if store_history:
2671 2672 self.history_manager.store_inputs(self.execution_count,
2672 2673 cell, raw_cell)
2673 2674 if not silent:
2674 2675 self.logger.log(cell, raw_cell)
2675 2676
2676 2677 # Display the exception if input processing failed.
2677 2678 if preprocessing_exc_tuple is not None:
2678 2679 self.showtraceback(preprocessing_exc_tuple)
2679 2680 if store_history:
2680 2681 self.execution_count += 1
2681 2682 return error_before_exec(preprocessing_exc_tuple[2])
2682 2683
2683 2684 # Our own compiler remembers the __future__ environment. If we want to
2684 2685 # run code with a separate __future__ environment, use the default
2685 2686 # compiler
2686 2687 compiler = self.compile if shell_futures else CachingCompiler()
2687 2688
2688 2689 with self.builtin_trap:
2689 2690 cell_name = self.compile.cache(cell, self.execution_count)
2690 2691
2691 2692 with self.display_trap:
2692 2693 # Compile to bytecode
2693 2694 try:
2694 2695 code_ast = compiler.ast_parse(cell, filename=cell_name)
2695 2696 except IndentationError as e:
2696 2697 self.showindentationerror()
2697 2698 if store_history:
2698 2699 self.execution_count += 1
2699 2700 return error_before_exec(e)
2700 2701 except (OverflowError, SyntaxError, ValueError, TypeError,
2701 2702 MemoryError) as e:
2702 2703 self.showsyntaxerror()
2703 2704 if store_history:
2704 2705 self.execution_count += 1
2705 2706 return error_before_exec(e)
2706 2707
2707 2708 # Apply AST transformations
2708 2709 try:
2709 2710 code_ast = self.transform_ast(code_ast)
2710 2711 except InputRejected as e:
2711 2712 self.showtraceback()
2712 2713 if store_history:
2713 2714 self.execution_count += 1
2714 2715 return error_before_exec(e)
2715 2716
2716 2717 # Give the displayhook a reference to our ExecutionResult so it
2717 2718 # can fill in the output value.
2718 2719 self.displayhook.exec_result = result
2719 2720
2720 2721 # Execute the user code
2721 2722 interactivity = "none" if silent else self.ast_node_interactivity
2722 2723 self.run_ast_nodes(code_ast.body, cell_name,
2723 2724 interactivity=interactivity, compiler=compiler, result=result)
2724 2725
2725 2726 # Reset this so later displayed values do not modify the
2726 2727 # ExecutionResult
2727 2728 self.displayhook.exec_result = None
2728 2729
2729 2730 self.events.trigger('post_execute')
2730 2731 if not silent:
2731 2732 self.events.trigger('post_run_cell')
2732 2733
2733 2734 if store_history:
2734 2735 # Write output to the database. Does nothing unless
2735 2736 # history output logging is enabled.
2736 2737 self.history_manager.store_output(self.execution_count)
2737 2738 # Each cell is a *single* input, regardless of how many lines it has
2738 2739 self.execution_count += 1
2739 2740
2740 2741 return result
2741 2742
2742 2743 def transform_ast(self, node):
2743 2744 """Apply the AST transformations from self.ast_transformers
2744 2745
2745 2746 Parameters
2746 2747 ----------
2747 2748 node : ast.Node
2748 2749 The root node to be transformed. Typically called with the ast.Module
2749 2750 produced by parsing user input.
2750 2751
2751 2752 Returns
2752 2753 -------
2753 2754 An ast.Node corresponding to the node it was called with. Note that it
2754 2755 may also modify the passed object, so don't rely on references to the
2755 2756 original AST.
2756 2757 """
2757 2758 for transformer in self.ast_transformers:
2758 2759 try:
2759 2760 node = transformer.visit(node)
2760 2761 except InputRejected:
2761 2762 # User-supplied AST transformers can reject an input by raising
2762 2763 # an InputRejected. Short-circuit in this case so that we
2763 2764 # don't unregister the transform.
2764 2765 raise
2765 2766 except Exception:
2766 2767 warn("AST transformer %r threw an error. It will be unregistered." % transformer)
2767 2768 self.ast_transformers.remove(transformer)
2768 2769
2769 2770 if self.ast_transformers:
2770 2771 ast.fix_missing_locations(node)
2771 2772 return node
2772 2773
2773 2774
2774 2775 def run_ast_nodes(self, nodelist, cell_name, interactivity='last_expr',
2775 2776 compiler=compile, result=None):
2776 2777 """Run a sequence of AST nodes. The execution mode depends on the
2777 2778 interactivity parameter.
2778 2779
2779 2780 Parameters
2780 2781 ----------
2781 2782 nodelist : list
2782 2783 A sequence of AST nodes to run.
2783 2784 cell_name : str
2784 2785 Will be passed to the compiler as the filename of the cell. Typically
2785 2786 the value returned by ip.compile.cache(cell).
2786 2787 interactivity : str
2787 2788 'all', 'last', 'last_expr' or 'none', specifying which nodes should be
2788 2789 run interactively (displaying output from expressions). 'last_expr'
2789 2790 will run the last node interactively only if it is an expression (i.e.
2790 2791 expressions in loops or other blocks are not displayed. Other values
2791 2792 for this parameter will raise a ValueError.
2792 2793 compiler : callable
2793 2794 A function with the same interface as the built-in compile(), to turn
2794 2795 the AST nodes into code objects. Default is the built-in compile().
2795 2796 result : ExecutionResult, optional
2796 2797 An object to store exceptions that occur during execution.
2797 2798
2798 2799 Returns
2799 2800 -------
2800 2801 True if an exception occurred while running code, False if it finished
2801 2802 running.
2802 2803 """
2803 2804 if not nodelist:
2804 2805 return
2805 2806
2806 2807 if interactivity == 'last_expr':
2807 2808 if isinstance(nodelist[-1], ast.Expr):
2808 2809 interactivity = "last"
2809 2810 else:
2810 2811 interactivity = "none"
2811 2812
2812 2813 if interactivity == 'none':
2813 2814 to_run_exec, to_run_interactive = nodelist, []
2814 2815 elif interactivity == 'last':
2815 2816 to_run_exec, to_run_interactive = nodelist[:-1], nodelist[-1:]
2816 2817 elif interactivity == 'all':
2817 2818 to_run_exec, to_run_interactive = [], nodelist
2818 2819 else:
2819 2820 raise ValueError("Interactivity was %r" % interactivity)
2820 2821
2821 2822 try:
2822 2823 for i, node in enumerate(to_run_exec):
2823 2824 mod = ast.Module([node])
2824 2825 code = compiler(mod, cell_name, "exec")
2825 2826 if self.run_code(code, result):
2826 2827 return True
2827 2828
2828 2829 for i, node in enumerate(to_run_interactive):
2829 2830 mod = ast.Interactive([node])
2830 2831 code = compiler(mod, cell_name, "single")
2831 2832 if self.run_code(code, result):
2832 2833 return True
2833 2834
2834 2835 # Flush softspace
2835 2836 if softspace(sys.stdout, 0):
2836 2837 print()
2837 2838
2838 2839 except:
2839 2840 # It's possible to have exceptions raised here, typically by
2840 2841 # compilation of odd code (such as a naked 'return' outside a
2841 2842 # function) that did parse but isn't valid. Typically the exception
2842 2843 # is a SyntaxError, but it's safest just to catch anything and show
2843 2844 # the user a traceback.
2844 2845
2845 2846 # We do only one try/except outside the loop to minimize the impact
2846 2847 # on runtime, and also because if any node in the node list is
2847 2848 # broken, we should stop execution completely.
2848 2849 if result:
2849 2850 result.error_before_exec = sys.exc_info()[1]
2850 2851 self.showtraceback()
2851 2852 return True
2852 2853
2853 2854 return False
2854 2855
2855 2856 def run_code(self, code_obj, result=None):
2856 2857 """Execute a code object.
2857 2858
2858 2859 When an exception occurs, self.showtraceback() is called to display a
2859 2860 traceback.
2860 2861
2861 2862 Parameters
2862 2863 ----------
2863 2864 code_obj : code object
2864 2865 A compiled code object, to be executed
2865 2866 result : ExecutionResult, optional
2866 2867 An object to store exceptions that occur during execution.
2867 2868
2868 2869 Returns
2869 2870 -------
2870 2871 False : successful execution.
2871 2872 True : an error occurred.
2872 2873 """
2873 2874 # Set our own excepthook in case the user code tries to call it
2874 2875 # directly, so that the IPython crash handler doesn't get triggered
2875 2876 old_excepthook, sys.excepthook = sys.excepthook, self.excepthook
2876 2877
2877 2878 # we save the original sys.excepthook in the instance, in case config
2878 2879 # code (such as magics) needs access to it.
2879 2880 self.sys_excepthook = old_excepthook
2880 2881 outflag = 1 # happens in more places, so it's easier as default
2881 2882 try:
2882 2883 try:
2883 2884 self.hooks.pre_run_code_hook()
2884 2885 #rprint('Running code', repr(code_obj)) # dbg
2885 2886 exec(code_obj, self.user_global_ns, self.user_ns)
2886 2887 finally:
2887 2888 # Reset our crash handler in place
2888 2889 sys.excepthook = old_excepthook
2889 2890 except SystemExit as e:
2890 2891 if result is not None:
2891 2892 result.error_in_exec = e
2892 2893 self.showtraceback(exception_only=True)
2893 2894 warn("To exit: use 'exit', 'quit', or Ctrl-D.", level=1)
2894 2895 except self.custom_exceptions:
2895 2896 etype, value, tb = sys.exc_info()
2896 2897 if result is not None:
2897 2898 result.error_in_exec = value
2898 2899 self.CustomTB(etype, value, tb)
2899 2900 except:
2900 2901 if result is not None:
2901 2902 result.error_in_exec = sys.exc_info()[1]
2902 2903 self.showtraceback()
2903 2904 else:
2904 2905 outflag = 0
2905 2906 return outflag
2906 2907
2907 2908 # For backwards compatibility
2908 2909 runcode = run_code
2909 2910
2910 2911 #-------------------------------------------------------------------------
2911 2912 # Things related to GUI support and pylab
2912 2913 #-------------------------------------------------------------------------
2913 2914
2914 2915 def enable_gui(self, gui=None):
2915 2916 raise NotImplementedError('Implement enable_gui in a subclass')
2916 2917
2917 2918 def enable_matplotlib(self, gui=None):
2918 2919 """Enable interactive matplotlib and inline figure support.
2919 2920
2920 2921 This takes the following steps:
2921 2922
2922 2923 1. select the appropriate eventloop and matplotlib backend
2923 2924 2. set up matplotlib for interactive use with that backend
2924 2925 3. configure formatters for inline figure display
2925 2926 4. enable the selected gui eventloop
2926 2927
2927 2928 Parameters
2928 2929 ----------
2929 2930 gui : optional, string
2930 2931 If given, dictates the choice of matplotlib GUI backend to use
2931 2932 (should be one of IPython's supported backends, 'qt', 'osx', 'tk',
2932 2933 'gtk', 'wx' or 'inline'), otherwise we use the default chosen by
2933 2934 matplotlib (as dictated by the matplotlib build-time options plus the
2934 2935 user's matplotlibrc configuration file). Note that not all backends
2935 2936 make sense in all contexts, for example a terminal ipython can't
2936 2937 display figures inline.
2937 2938 """
2938 2939 from IPython.core import pylabtools as pt
2939 2940 gui, backend = pt.find_gui_and_backend(gui, self.pylab_gui_select)
2940 2941
2941 2942 if gui != 'inline':
2942 2943 # If we have our first gui selection, store it
2943 2944 if self.pylab_gui_select is None:
2944 2945 self.pylab_gui_select = gui
2945 2946 # Otherwise if they are different
2946 2947 elif gui != self.pylab_gui_select:
2947 2948 print ('Warning: Cannot change to a different GUI toolkit: %s.'
2948 2949 ' Using %s instead.' % (gui, self.pylab_gui_select))
2949 2950 gui, backend = pt.find_gui_and_backend(self.pylab_gui_select)
2950 2951
2951 2952 pt.activate_matplotlib(backend)
2952 2953 pt.configure_inline_support(self, backend)
2953 2954
2954 2955 # Now we must activate the gui pylab wants to use, and fix %run to take
2955 2956 # plot updates into account
2956 2957 self.enable_gui(gui)
2957 2958 self.magics_manager.registry['ExecutionMagics'].default_runner = \
2958 2959 pt.mpl_runner(self.safe_execfile)
2959 2960
2960 2961 return gui, backend
2961 2962
2962 2963 def enable_pylab(self, gui=None, import_all=True, welcome_message=False):
2963 2964 """Activate pylab support at runtime.
2964 2965
2965 2966 This turns on support for matplotlib, preloads into the interactive
2966 2967 namespace all of numpy and pylab, and configures IPython to correctly
2967 2968 interact with the GUI event loop. The GUI backend to be used can be
2968 2969 optionally selected with the optional ``gui`` argument.
2969 2970
2970 2971 This method only adds preloading the namespace to InteractiveShell.enable_matplotlib.
2971 2972
2972 2973 Parameters
2973 2974 ----------
2974 2975 gui : optional, string
2975 2976 If given, dictates the choice of matplotlib GUI backend to use
2976 2977 (should be one of IPython's supported backends, 'qt', 'osx', 'tk',
2977 2978 'gtk', 'wx' or 'inline'), otherwise we use the default chosen by
2978 2979 matplotlib (as dictated by the matplotlib build-time options plus the
2979 2980 user's matplotlibrc configuration file). Note that not all backends
2980 2981 make sense in all contexts, for example a terminal ipython can't
2981 2982 display figures inline.
2982 2983 import_all : optional, bool, default: True
2983 2984 Whether to do `from numpy import *` and `from pylab import *`
2984 2985 in addition to module imports.
2985 2986 welcome_message : deprecated
2986 2987 This argument is ignored, no welcome message will be displayed.
2987 2988 """
2988 2989 from IPython.core.pylabtools import import_pylab
2989 2990
2990 2991 gui, backend = self.enable_matplotlib(gui)
2991 2992
2992 2993 # We want to prevent the loading of pylab to pollute the user's
2993 2994 # namespace as shown by the %who* magics, so we execute the activation
2994 2995 # code in an empty namespace, and we update *both* user_ns and
2995 2996 # user_ns_hidden with this information.
2996 2997 ns = {}
2997 2998 import_pylab(ns, import_all)
2998 2999 # warn about clobbered names
2999 3000 ignored = {"__builtins__"}
3000 3001 both = set(ns).intersection(self.user_ns).difference(ignored)
3001 3002 clobbered = [ name for name in both if self.user_ns[name] is not ns[name] ]
3002 3003 self.user_ns.update(ns)
3003 3004 self.user_ns_hidden.update(ns)
3004 3005 return gui, backend, clobbered
3005 3006
3006 3007 #-------------------------------------------------------------------------
3007 3008 # Utilities
3008 3009 #-------------------------------------------------------------------------
3009 3010
3010 3011 def var_expand(self, cmd, depth=0, formatter=DollarFormatter()):
3011 3012 """Expand python variables in a string.
3012 3013
3013 3014 The depth argument indicates how many frames above the caller should
3014 3015 be walked to look for the local namespace where to expand variables.
3015 3016
3016 3017 The global namespace for expansion is always the user's interactive
3017 3018 namespace.
3018 3019 """
3019 3020 ns = self.user_ns.copy()
3020 3021 try:
3021 3022 frame = sys._getframe(depth+1)
3022 3023 except ValueError:
3023 3024 # This is thrown if there aren't that many frames on the stack,
3024 3025 # e.g. if a script called run_line_magic() directly.
3025 3026 pass
3026 3027 else:
3027 3028 ns.update(frame.f_locals)
3028 3029
3029 3030 try:
3030 3031 # We have to use .vformat() here, because 'self' is a valid and common
3031 3032 # name, and expanding **ns for .format() would make it collide with
3032 3033 # the 'self' argument of the method.
3033 3034 cmd = formatter.vformat(cmd, args=[], kwargs=ns)
3034 3035 except Exception:
3035 3036 # if formatter couldn't format, just let it go untransformed
3036 3037 pass
3037 3038 return cmd
3038 3039
3039 3040 def mktempfile(self, data=None, prefix='ipython_edit_'):
3040 3041 """Make a new tempfile and return its filename.
3041 3042
3042 3043 This makes a call to tempfile.mkstemp (created in a tempfile.mkdtemp),
3043 3044 but it registers the created filename internally so ipython cleans it up
3044 3045 at exit time.
3045 3046
3046 3047 Optional inputs:
3047 3048
3048 3049 - data(None): if data is given, it gets written out to the temp file
3049 3050 immediately, and the file is closed again."""
3050 3051
3051 3052 dirname = tempfile.mkdtemp(prefix=prefix)
3052 3053 self.tempdirs.append(dirname)
3053 3054
3054 3055 handle, filename = tempfile.mkstemp('.py', prefix, dir=dirname)
3055 3056 os.close(handle) # On Windows, there can only be one open handle on a file
3056 3057 self.tempfiles.append(filename)
3057 3058
3058 3059 if data:
3059 3060 tmp_file = open(filename,'w')
3060 3061 tmp_file.write(data)
3061 3062 tmp_file.close()
3062 3063 return filename
3063 3064
3064 3065 # TODO: This should be removed when Term is refactored.
3065 3066 def write(self,data):
3066 3067 """Write a string to the default output"""
3067 3068 io.stdout.write(data)
3068 3069
3069 3070 # TODO: This should be removed when Term is refactored.
3070 3071 def write_err(self,data):
3071 3072 """Write a string to the default error output"""
3072 3073 io.stderr.write(data)
3073 3074
3074 3075 def ask_yes_no(self, prompt, default=None, interrupt=None):
3075 3076 if self.quiet:
3076 3077 return True
3077 3078 return ask_yes_no(prompt,default,interrupt)
3078 3079
3079 3080 def show_usage(self):
3080 3081 """Show a usage message"""
3081 3082 page.page(IPython.core.usage.interactive_usage)
3082 3083
3083 3084 def extract_input_lines(self, range_str, raw=False):
3084 3085 """Return as a string a set of input history slices.
3085 3086
3086 3087 Parameters
3087 3088 ----------
3088 3089 range_str : string
3089 3090 The set of slices is given as a string, like "~5/6-~4/2 4:8 9",
3090 3091 since this function is for use by magic functions which get their
3091 3092 arguments as strings. The number before the / is the session
3092 3093 number: ~n goes n back from the current session.
3093 3094
3094 3095 raw : bool, optional
3095 3096 By default, the processed input is used. If this is true, the raw
3096 3097 input history is used instead.
3097 3098
3098 3099 Notes
3099 3100 -----
3100 3101
3101 3102 Slices can be described with two notations:
3102 3103
3103 3104 * ``N:M`` -> standard python form, means including items N...(M-1).
3104 3105 * ``N-M`` -> include items N..M (closed endpoint).
3105 3106 """
3106 3107 lines = self.history_manager.get_range_by_str(range_str, raw=raw)
3107 3108 return "\n".join(x for _, _, x in lines)
3108 3109
3109 3110 def find_user_code(self, target, raw=True, py_only=False, skip_encoding_cookie=True, search_ns=False):
3110 3111 """Get a code string from history, file, url, or a string or macro.
3111 3112
3112 3113 This is mainly used by magic functions.
3113 3114
3114 3115 Parameters
3115 3116 ----------
3116 3117
3117 3118 target : str
3118 3119
3119 3120 A string specifying code to retrieve. This will be tried respectively
3120 3121 as: ranges of input history (see %history for syntax), url,
3121 3122 corresponding .py file, filename, or an expression evaluating to a
3122 3123 string or Macro in the user namespace.
3123 3124
3124 3125 raw : bool
3125 3126 If true (default), retrieve raw history. Has no effect on the other
3126 3127 retrieval mechanisms.
3127 3128
3128 3129 py_only : bool (default False)
3129 3130 Only try to fetch python code, do not try alternative methods to decode file
3130 3131 if unicode fails.
3131 3132
3132 3133 Returns
3133 3134 -------
3134 3135 A string of code.
3135 3136
3136 3137 ValueError is raised if nothing is found, and TypeError if it evaluates
3137 3138 to an object of another type. In each case, .args[0] is a printable
3138 3139 message.
3139 3140 """
3140 3141 code = self.extract_input_lines(target, raw=raw) # Grab history
3141 3142 if code:
3142 3143 return code
3143 3144 utarget = unquote_filename(target)
3144 3145 try:
3145 3146 if utarget.startswith(('http://', 'https://')):
3146 3147 return openpy.read_py_url(utarget, skip_encoding_cookie=skip_encoding_cookie)
3147 3148 except UnicodeDecodeError:
3148 3149 if not py_only :
3149 3150 # Deferred import
3150 3151 try:
3151 3152 from urllib.request import urlopen # Py3
3152 3153 except ImportError:
3153 3154 from urllib import urlopen
3154 3155 response = urlopen(target)
3155 3156 return response.read().decode('latin1')
3156 3157 raise ValueError(("'%s' seem to be unreadable.") % utarget)
3157 3158
3158 3159 potential_target = [target]
3159 3160 try :
3160 3161 potential_target.insert(0,get_py_filename(target))
3161 3162 except IOError:
3162 3163 pass
3163 3164
3164 3165 for tgt in potential_target :
3165 3166 if os.path.isfile(tgt): # Read file
3166 3167 try :
3167 3168 return openpy.read_py_file(tgt, skip_encoding_cookie=skip_encoding_cookie)
3168 3169 except UnicodeDecodeError :
3169 3170 if not py_only :
3170 3171 with io_open(tgt,'r', encoding='latin1') as f :
3171 3172 return f.read()
3172 3173 raise ValueError(("'%s' seem to be unreadable.") % target)
3173 3174 elif os.path.isdir(os.path.expanduser(tgt)):
3174 3175 raise ValueError("'%s' is a directory, not a regular file." % target)
3175 3176
3176 3177 if search_ns:
3177 3178 # Inspect namespace to load object source
3178 3179 object_info = self.object_inspect(target, detail_level=1)
3179 3180 if object_info['found'] and object_info['source']:
3180 3181 return object_info['source']
3181 3182
3182 3183 try: # User namespace
3183 3184 codeobj = eval(target, self.user_ns)
3184 3185 except Exception:
3185 3186 raise ValueError(("'%s' was not found in history, as a file, url, "
3186 3187 "nor in the user namespace.") % target)
3187 3188
3188 3189 if isinstance(codeobj, string_types):
3189 3190 return codeobj
3190 3191 elif isinstance(codeobj, Macro):
3191 3192 return codeobj.value
3192 3193
3193 3194 raise TypeError("%s is neither a string nor a macro." % target,
3194 3195 codeobj)
3195 3196
3196 3197 #-------------------------------------------------------------------------
3197 3198 # Things related to IPython exiting
3198 3199 #-------------------------------------------------------------------------
3199 3200 def atexit_operations(self):
3200 3201 """This will be executed at the time of exit.
3201 3202
3202 3203 Cleanup operations and saving of persistent data that is done
3203 3204 unconditionally by IPython should be performed here.
3204 3205
3205 3206 For things that may depend on startup flags or platform specifics (such
3206 3207 as having readline or not), register a separate atexit function in the
3207 3208 code that has the appropriate information, rather than trying to
3208 3209 clutter
3209 3210 """
3210 3211 # Close the history session (this stores the end time and line count)
3211 3212 # this must be *before* the tempfile cleanup, in case of temporary
3212 3213 # history db
3213 3214 self.history_manager.end_session()
3214 3215
3215 3216 # Cleanup all tempfiles and folders left around
3216 3217 for tfile in self.tempfiles:
3217 3218 try:
3218 3219 os.unlink(tfile)
3219 3220 except OSError:
3220 3221 pass
3221 3222
3222 3223 for tdir in self.tempdirs:
3223 3224 try:
3224 3225 os.rmdir(tdir)
3225 3226 except OSError:
3226 3227 pass
3227 3228
3228 3229 # Clear all user namespaces to release all references cleanly.
3229 3230 self.reset(new_session=False)
3230 3231
3231 3232 # Run user hooks
3232 3233 self.hooks.shutdown_hook()
3233 3234
3234 3235 def cleanup(self):
3235 3236 self.restore_sys_module_state()
3236 3237
3237 3238
3238 3239 class InteractiveShellABC(with_metaclass(abc.ABCMeta, object)):
3239 3240 """An abstract base class for InteractiveShell."""
3240 3241
3241 3242 InteractiveShellABC.register(InteractiveShell)
@@ -1,703 +1,703 b''
1 1 # encoding: utf-8
2 2 """Magic functions for InteractiveShell.
3 3 """
4 4 from __future__ import print_function
5 5
6 6 #-----------------------------------------------------------------------------
7 7 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and
8 8 # Copyright (C) 2001 Fernando Perez <fperez@colorado.edu>
9 9 # Copyright (C) 2008 The IPython Development Team
10 10
11 11 # Distributed under the terms of the BSD License. The full license is in
12 12 # the file COPYING, distributed as part of this software.
13 13 #-----------------------------------------------------------------------------
14 14
15 15 #-----------------------------------------------------------------------------
16 16 # Imports
17 17 #-----------------------------------------------------------------------------
18 18 # Stdlib
19 19 import os
20 20 import re
21 21 import sys
22 22 import types
23 23 from getopt import getopt, GetoptError
24 24
25 25 # Our own
26 26 from traitlets.config.configurable import Configurable
27 27 from IPython.core import oinspect
28 28 from IPython.core.error import UsageError
29 29 from IPython.core.inputsplitter import ESC_MAGIC, ESC_MAGIC2
30 30 from decorator import decorator
31 31 from IPython.utils.ipstruct import Struct
32 32 from IPython.utils.process import arg_split
33 33 from IPython.utils.py3compat import string_types, iteritems
34 34 from IPython.utils.text import dedent
35 35 from traitlets import Bool, Dict, Instance
36 from IPython.utils.warn import error
36 from logging import error
37 37
38 38 #-----------------------------------------------------------------------------
39 39 # Globals
40 40 #-----------------------------------------------------------------------------
41 41
42 42 # A dict we'll use for each class that has magics, used as temporary storage to
43 43 # pass information between the @line/cell_magic method decorators and the
44 44 # @magics_class class decorator, because the method decorators have no
45 45 # access to the class when they run. See for more details:
46 46 # http://stackoverflow.com/questions/2366713/can-a-python-decorator-of-an-instance-method-access-the-class
47 47
48 48 magics = dict(line={}, cell={})
49 49
50 50 magic_kinds = ('line', 'cell')
51 51 magic_spec = ('line', 'cell', 'line_cell')
52 52 magic_escapes = dict(line=ESC_MAGIC, cell=ESC_MAGIC2)
53 53
54 54 #-----------------------------------------------------------------------------
55 55 # Utility classes and functions
56 56 #-----------------------------------------------------------------------------
57 57
58 58 class Bunch: pass
59 59
60 60
61 61 def on_off(tag):
62 62 """Return an ON/OFF string for a 1/0 input. Simple utility function."""
63 63 return ['OFF','ON'][tag]
64 64
65 65
66 66 def compress_dhist(dh):
67 67 """Compress a directory history into a new one with at most 20 entries.
68 68
69 69 Return a new list made from the first and last 10 elements of dhist after
70 70 removal of duplicates.
71 71 """
72 72 head, tail = dh[:-10], dh[-10:]
73 73
74 74 newhead = []
75 75 done = set()
76 76 for h in head:
77 77 if h in done:
78 78 continue
79 79 newhead.append(h)
80 80 done.add(h)
81 81
82 82 return newhead + tail
83 83
84 84
85 85 def needs_local_scope(func):
86 86 """Decorator to mark magic functions which need to local scope to run."""
87 87 func.needs_local_scope = True
88 88 return func
89 89
90 90 #-----------------------------------------------------------------------------
91 91 # Class and method decorators for registering magics
92 92 #-----------------------------------------------------------------------------
93 93
94 94 def magics_class(cls):
95 95 """Class decorator for all subclasses of the main Magics class.
96 96
97 97 Any class that subclasses Magics *must* also apply this decorator, to
98 98 ensure that all the methods that have been decorated as line/cell magics
99 99 get correctly registered in the class instance. This is necessary because
100 100 when method decorators run, the class does not exist yet, so they
101 101 temporarily store their information into a module global. Application of
102 102 this class decorator copies that global data to the class instance and
103 103 clears the global.
104 104
105 105 Obviously, this mechanism is not thread-safe, which means that the
106 106 *creation* of subclasses of Magic should only be done in a single-thread
107 107 context. Instantiation of the classes has no restrictions. Given that
108 108 these classes are typically created at IPython startup time and before user
109 109 application code becomes active, in practice this should not pose any
110 110 problems.
111 111 """
112 112 cls.registered = True
113 113 cls.magics = dict(line = magics['line'],
114 114 cell = magics['cell'])
115 115 magics['line'] = {}
116 116 magics['cell'] = {}
117 117 return cls
118 118
119 119
120 120 def record_magic(dct, magic_kind, magic_name, func):
121 121 """Utility function to store a function as a magic of a specific kind.
122 122
123 123 Parameters
124 124 ----------
125 125 dct : dict
126 126 A dictionary with 'line' and 'cell' subdicts.
127 127
128 128 magic_kind : str
129 129 Kind of magic to be stored.
130 130
131 131 magic_name : str
132 132 Key to store the magic as.
133 133
134 134 func : function
135 135 Callable object to store.
136 136 """
137 137 if magic_kind == 'line_cell':
138 138 dct['line'][magic_name] = dct['cell'][magic_name] = func
139 139 else:
140 140 dct[magic_kind][magic_name] = func
141 141
142 142
143 143 def validate_type(magic_kind):
144 144 """Ensure that the given magic_kind is valid.
145 145
146 146 Check that the given magic_kind is one of the accepted spec types (stored
147 147 in the global `magic_spec`), raise ValueError otherwise.
148 148 """
149 149 if magic_kind not in magic_spec:
150 150 raise ValueError('magic_kind must be one of %s, %s given' %
151 151 magic_kinds, magic_kind)
152 152
153 153
154 154 # The docstrings for the decorator below will be fairly similar for the two
155 155 # types (method and function), so we generate them here once and reuse the
156 156 # templates below.
157 157 _docstring_template = \
158 158 """Decorate the given {0} as {1} magic.
159 159
160 160 The decorator can be used with or without arguments, as follows.
161 161
162 162 i) without arguments: it will create a {1} magic named as the {0} being
163 163 decorated::
164 164
165 165 @deco
166 166 def foo(...)
167 167
168 168 will create a {1} magic named `foo`.
169 169
170 170 ii) with one string argument: which will be used as the actual name of the
171 171 resulting magic::
172 172
173 173 @deco('bar')
174 174 def foo(...)
175 175
176 176 will create a {1} magic named `bar`.
177 177 """
178 178
179 179 # These two are decorator factories. While they are conceptually very similar,
180 180 # there are enough differences in the details that it's simpler to have them
181 181 # written as completely standalone functions rather than trying to share code
182 182 # and make a single one with convoluted logic.
183 183
184 184 def _method_magic_marker(magic_kind):
185 185 """Decorator factory for methods in Magics subclasses.
186 186 """
187 187
188 188 validate_type(magic_kind)
189 189
190 190 # This is a closure to capture the magic_kind. We could also use a class,
191 191 # but it's overkill for just that one bit of state.
192 192 def magic_deco(arg):
193 193 call = lambda f, *a, **k: f(*a, **k)
194 194
195 195 if callable(arg):
196 196 # "Naked" decorator call (just @foo, no args)
197 197 func = arg
198 198 name = func.__name__
199 199 retval = decorator(call, func)
200 200 record_magic(magics, magic_kind, name, name)
201 201 elif isinstance(arg, string_types):
202 202 # Decorator called with arguments (@foo('bar'))
203 203 name = arg
204 204 def mark(func, *a, **kw):
205 205 record_magic(magics, magic_kind, name, func.__name__)
206 206 return decorator(call, func)
207 207 retval = mark
208 208 else:
209 209 raise TypeError("Decorator can only be called with "
210 210 "string or function")
211 211 return retval
212 212
213 213 # Ensure the resulting decorator has a usable docstring
214 214 magic_deco.__doc__ = _docstring_template.format('method', magic_kind)
215 215 return magic_deco
216 216
217 217
218 218 def _function_magic_marker(magic_kind):
219 219 """Decorator factory for standalone functions.
220 220 """
221 221 validate_type(magic_kind)
222 222
223 223 # This is a closure to capture the magic_kind. We could also use a class,
224 224 # but it's overkill for just that one bit of state.
225 225 def magic_deco(arg):
226 226 call = lambda f, *a, **k: f(*a, **k)
227 227
228 228 # Find get_ipython() in the caller's namespace
229 229 caller = sys._getframe(1)
230 230 for ns in ['f_locals', 'f_globals', 'f_builtins']:
231 231 get_ipython = getattr(caller, ns).get('get_ipython')
232 232 if get_ipython is not None:
233 233 break
234 234 else:
235 235 raise NameError('Decorator can only run in context where '
236 236 '`get_ipython` exists')
237 237
238 238 ip = get_ipython()
239 239
240 240 if callable(arg):
241 241 # "Naked" decorator call (just @foo, no args)
242 242 func = arg
243 243 name = func.__name__
244 244 ip.register_magic_function(func, magic_kind, name)
245 245 retval = decorator(call, func)
246 246 elif isinstance(arg, string_types):
247 247 # Decorator called with arguments (@foo('bar'))
248 248 name = arg
249 249 def mark(func, *a, **kw):
250 250 ip.register_magic_function(func, magic_kind, name)
251 251 return decorator(call, func)
252 252 retval = mark
253 253 else:
254 254 raise TypeError("Decorator can only be called with "
255 255 "string or function")
256 256 return retval
257 257
258 258 # Ensure the resulting decorator has a usable docstring
259 259 ds = _docstring_template.format('function', magic_kind)
260 260
261 261 ds += dedent("""
262 262 Note: this decorator can only be used in a context where IPython is already
263 263 active, so that the `get_ipython()` call succeeds. You can therefore use
264 264 it in your startup files loaded after IPython initializes, but *not* in the
265 265 IPython configuration file itself, which is executed before IPython is
266 266 fully up and running. Any file located in the `startup` subdirectory of
267 267 your configuration profile will be OK in this sense.
268 268 """)
269 269
270 270 magic_deco.__doc__ = ds
271 271 return magic_deco
272 272
273 273
274 274 # Create the actual decorators for public use
275 275
276 276 # These three are used to decorate methods in class definitions
277 277 line_magic = _method_magic_marker('line')
278 278 cell_magic = _method_magic_marker('cell')
279 279 line_cell_magic = _method_magic_marker('line_cell')
280 280
281 281 # These three decorate standalone functions and perform the decoration
282 282 # immediately. They can only run where get_ipython() works
283 283 register_line_magic = _function_magic_marker('line')
284 284 register_cell_magic = _function_magic_marker('cell')
285 285 register_line_cell_magic = _function_magic_marker('line_cell')
286 286
287 287 #-----------------------------------------------------------------------------
288 288 # Core Magic classes
289 289 #-----------------------------------------------------------------------------
290 290
291 291 class MagicsManager(Configurable):
292 292 """Object that handles all magic-related functionality for IPython.
293 293 """
294 294 # Non-configurable class attributes
295 295
296 296 # A two-level dict, first keyed by magic type, then by magic function, and
297 297 # holding the actual callable object as value. This is the dict used for
298 298 # magic function dispatch
299 299 magics = Dict()
300 300
301 301 # A registry of the original objects that we've been given holding magics.
302 302 registry = Dict()
303 303
304 304 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC', allow_none=True)
305 305
306 306 auto_magic = Bool(True, config=True, help=
307 307 "Automatically call line magics without requiring explicit % prefix")
308 308
309 309 def _auto_magic_changed(self, name, value):
310 310 self.shell.automagic = value
311 311
312 312 _auto_status = [
313 313 'Automagic is OFF, % prefix IS needed for line magics.',
314 314 'Automagic is ON, % prefix IS NOT needed for line magics.']
315 315
316 316 user_magics = Instance('IPython.core.magics.UserMagics', allow_none=True)
317 317
318 318 def __init__(self, shell=None, config=None, user_magics=None, **traits):
319 319
320 320 super(MagicsManager, self).__init__(shell=shell, config=config,
321 321 user_magics=user_magics, **traits)
322 322 self.magics = dict(line={}, cell={})
323 323 # Let's add the user_magics to the registry for uniformity, so *all*
324 324 # registered magic containers can be found there.
325 325 self.registry[user_magics.__class__.__name__] = user_magics
326 326
327 327 def auto_status(self):
328 328 """Return descriptive string with automagic status."""
329 329 return self._auto_status[self.auto_magic]
330 330
331 331 def lsmagic(self):
332 332 """Return a dict of currently available magic functions.
333 333
334 334 The return dict has the keys 'line' and 'cell', corresponding to the
335 335 two types of magics we support. Each value is a list of names.
336 336 """
337 337 return self.magics
338 338
339 339 def lsmagic_docs(self, brief=False, missing=''):
340 340 """Return dict of documentation of magic functions.
341 341
342 342 The return dict has the keys 'line' and 'cell', corresponding to the
343 343 two types of magics we support. Each value is a dict keyed by magic
344 344 name whose value is the function docstring. If a docstring is
345 345 unavailable, the value of `missing` is used instead.
346 346
347 347 If brief is True, only the first line of each docstring will be returned.
348 348 """
349 349 docs = {}
350 350 for m_type in self.magics:
351 351 m_docs = {}
352 352 for m_name, m_func in iteritems(self.magics[m_type]):
353 353 if m_func.__doc__:
354 354 if brief:
355 355 m_docs[m_name] = m_func.__doc__.split('\n', 1)[0]
356 356 else:
357 357 m_docs[m_name] = m_func.__doc__.rstrip()
358 358 else:
359 359 m_docs[m_name] = missing
360 360 docs[m_type] = m_docs
361 361 return docs
362 362
363 363 def register(self, *magic_objects):
364 364 """Register one or more instances of Magics.
365 365
366 366 Take one or more classes or instances of classes that subclass the main
367 367 `core.Magic` class, and register them with IPython to use the magic
368 368 functions they provide. The registration process will then ensure that
369 369 any methods that have decorated to provide line and/or cell magics will
370 370 be recognized with the `%x`/`%%x` syntax as a line/cell magic
371 371 respectively.
372 372
373 373 If classes are given, they will be instantiated with the default
374 374 constructor. If your classes need a custom constructor, you should
375 375 instanitate them first and pass the instance.
376 376
377 377 The provided arguments can be an arbitrary mix of classes and instances.
378 378
379 379 Parameters
380 380 ----------
381 381 magic_objects : one or more classes or instances
382 382 """
383 383 # Start by validating them to ensure they have all had their magic
384 384 # methods registered at the instance level
385 385 for m in magic_objects:
386 386 if not m.registered:
387 387 raise ValueError("Class of magics %r was constructed without "
388 388 "the @register_magics class decorator")
389 389 if isinstance(m, type):
390 390 # If we're given an uninstantiated class
391 391 m = m(shell=self.shell)
392 392
393 393 # Now that we have an instance, we can register it and update the
394 394 # table of callables
395 395 self.registry[m.__class__.__name__] = m
396 396 for mtype in magic_kinds:
397 397 self.magics[mtype].update(m.magics[mtype])
398 398
399 399 def register_function(self, func, magic_kind='line', magic_name=None):
400 400 """Expose a standalone function as magic function for IPython.
401 401
402 402 This will create an IPython magic (line, cell or both) from a
403 403 standalone function. The functions should have the following
404 404 signatures:
405 405
406 406 * For line magics: `def f(line)`
407 407 * For cell magics: `def f(line, cell)`
408 408 * For a function that does both: `def f(line, cell=None)`
409 409
410 410 In the latter case, the function will be called with `cell==None` when
411 411 invoked as `%f`, and with cell as a string when invoked as `%%f`.
412 412
413 413 Parameters
414 414 ----------
415 415 func : callable
416 416 Function to be registered as a magic.
417 417
418 418 magic_kind : str
419 419 Kind of magic, one of 'line', 'cell' or 'line_cell'
420 420
421 421 magic_name : optional str
422 422 If given, the name the magic will have in the IPython namespace. By
423 423 default, the name of the function itself is used.
424 424 """
425 425
426 426 # Create the new method in the user_magics and register it in the
427 427 # global table
428 428 validate_type(magic_kind)
429 429 magic_name = func.__name__ if magic_name is None else magic_name
430 430 setattr(self.user_magics, magic_name, func)
431 431 record_magic(self.magics, magic_kind, magic_name, func)
432 432
433 433 def define_magic(self, name, func):
434 434 """[Deprecated] Expose own function as magic function for IPython.
435 435
436 436 Will be removed in IPython 5.0
437 437
438 438 Example::
439 439
440 440 def foo_impl(self, parameter_s=''):
441 441 'My very own magic!. (Use docstrings, IPython reads them).'
442 442 print 'Magic function. Passed parameter is between < >:'
443 443 print '<%s>' % parameter_s
444 444 print 'The self object is:', self
445 445
446 446 ip.define_magic('foo',foo_impl)
447 447 """
448 448 meth = types.MethodType(func, self.user_magics)
449 449 setattr(self.user_magics, name, meth)
450 450 record_magic(self.magics, 'line', name, meth)
451 451
452 452 def register_alias(self, alias_name, magic_name, magic_kind='line'):
453 453 """Register an alias to a magic function.
454 454
455 455 The alias is an instance of :class:`MagicAlias`, which holds the
456 456 name and kind of the magic it should call. Binding is done at
457 457 call time, so if the underlying magic function is changed the alias
458 458 will call the new function.
459 459
460 460 Parameters
461 461 ----------
462 462 alias_name : str
463 463 The name of the magic to be registered.
464 464
465 465 magic_name : str
466 466 The name of an existing magic.
467 467
468 468 magic_kind : str
469 469 Kind of magic, one of 'line' or 'cell'
470 470 """
471 471
472 472 # `validate_type` is too permissive, as it allows 'line_cell'
473 473 # which we do not handle.
474 474 if magic_kind not in magic_kinds:
475 475 raise ValueError('magic_kind must be one of %s, %s given' %
476 476 magic_kinds, magic_kind)
477 477
478 478 alias = MagicAlias(self.shell, magic_name, magic_kind)
479 479 setattr(self.user_magics, alias_name, alias)
480 480 record_magic(self.magics, magic_kind, alias_name, alias)
481 481
482 482 # Key base class that provides the central functionality for magics.
483 483
484 484
485 485 class Magics(Configurable):
486 486 """Base class for implementing magic functions.
487 487
488 488 Shell functions which can be reached as %function_name. All magic
489 489 functions should accept a string, which they can parse for their own
490 490 needs. This can make some functions easier to type, eg `%cd ../`
491 491 vs. `%cd("../")`
492 492
493 493 Classes providing magic functions need to subclass this class, and they
494 494 MUST:
495 495
496 496 - Use the method decorators `@line_magic` and `@cell_magic` to decorate
497 497 individual methods as magic functions, AND
498 498
499 499 - Use the class decorator `@magics_class` to ensure that the magic
500 500 methods are properly registered at the instance level upon instance
501 501 initialization.
502 502
503 503 See :mod:`magic_functions` for examples of actual implementation classes.
504 504 """
505 505 # Dict holding all command-line options for each magic.
506 506 options_table = None
507 507 # Dict for the mapping of magic names to methods, set by class decorator
508 508 magics = None
509 509 # Flag to check that the class decorator was properly applied
510 510 registered = False
511 511 # Instance of IPython shell
512 512 shell = None
513 513
514 514 def __init__(self, shell=None, **kwargs):
515 515 if not(self.__class__.registered):
516 516 raise ValueError('Magics subclass without registration - '
517 517 'did you forget to apply @magics_class?')
518 518 if shell is not None:
519 519 if hasattr(shell, 'configurables'):
520 520 shell.configurables.append(self)
521 521 if hasattr(shell, 'config'):
522 522 kwargs.setdefault('parent', shell)
523 523
524 524 self.shell = shell
525 525 self.options_table = {}
526 526 # The method decorators are run when the instance doesn't exist yet, so
527 527 # they can only record the names of the methods they are supposed to
528 528 # grab. Only now, that the instance exists, can we create the proper
529 529 # mapping to bound methods. So we read the info off the original names
530 530 # table and replace each method name by the actual bound method.
531 531 # But we mustn't clobber the *class* mapping, in case of multiple instances.
532 532 class_magics = self.magics
533 533 self.magics = {}
534 534 for mtype in magic_kinds:
535 535 tab = self.magics[mtype] = {}
536 536 cls_tab = class_magics[mtype]
537 537 for magic_name, meth_name in iteritems(cls_tab):
538 538 if isinstance(meth_name, string_types):
539 539 # it's a method name, grab it
540 540 tab[magic_name] = getattr(self, meth_name)
541 541 else:
542 542 # it's the real thing
543 543 tab[magic_name] = meth_name
544 544 # Configurable **needs** to be initiated at the end or the config
545 545 # magics get screwed up.
546 546 super(Magics, self).__init__(**kwargs)
547 547
548 548 def arg_err(self,func):
549 549 """Print docstring if incorrect arguments were passed"""
550 550 print('Error in arguments:')
551 551 print(oinspect.getdoc(func))
552 552
553 553 def format_latex(self, strng):
554 554 """Format a string for latex inclusion."""
555 555
556 556 # Characters that need to be escaped for latex:
557 557 escape_re = re.compile(r'(%|_|\$|#|&)',re.MULTILINE)
558 558 # Magic command names as headers:
559 559 cmd_name_re = re.compile(r'^(%s.*?):' % ESC_MAGIC,
560 560 re.MULTILINE)
561 561 # Magic commands
562 562 cmd_re = re.compile(r'(?P<cmd>%s.+?\b)(?!\}\}:)' % ESC_MAGIC,
563 563 re.MULTILINE)
564 564 # Paragraph continue
565 565 par_re = re.compile(r'\\$',re.MULTILINE)
566 566
567 567 # The "\n" symbol
568 568 newline_re = re.compile(r'\\n')
569 569
570 570 # Now build the string for output:
571 571 #strng = cmd_name_re.sub(r'\n\\texttt{\\textsl{\\large \1}}:',strng)
572 572 strng = cmd_name_re.sub(r'\n\\bigskip\n\\texttt{\\textbf{ \1}}:',
573 573 strng)
574 574 strng = cmd_re.sub(r'\\texttt{\g<cmd>}',strng)
575 575 strng = par_re.sub(r'\\\\',strng)
576 576 strng = escape_re.sub(r'\\\1',strng)
577 577 strng = newline_re.sub(r'\\textbackslash{}n',strng)
578 578 return strng
579 579
580 580 def parse_options(self, arg_str, opt_str, *long_opts, **kw):
581 581 """Parse options passed to an argument string.
582 582
583 583 The interface is similar to that of :func:`getopt.getopt`, but it
584 584 returns a :class:`~IPython.utils.struct.Struct` with the options as keys
585 585 and the stripped argument string still as a string.
586 586
587 587 arg_str is quoted as a true sys.argv vector by using shlex.split.
588 588 This allows us to easily expand variables, glob files, quote
589 589 arguments, etc.
590 590
591 591 Parameters
592 592 ----------
593 593
594 594 arg_str : str
595 595 The arguments to parse.
596 596
597 597 opt_str : str
598 598 The options specification.
599 599
600 600 mode : str, default 'string'
601 601 If given as 'list', the argument string is returned as a list (split
602 602 on whitespace) instead of a string.
603 603
604 604 list_all : bool, default False
605 605 Put all option values in lists. Normally only options
606 606 appearing more than once are put in a list.
607 607
608 608 posix : bool, default True
609 609 Whether to split the input line in POSIX mode or not, as per the
610 610 conventions outlined in the :mod:`shlex` module from the standard
611 611 library.
612 612 """
613 613
614 614 # inject default options at the beginning of the input line
615 615 caller = sys._getframe(1).f_code.co_name
616 616 arg_str = '%s %s' % (self.options_table.get(caller,''),arg_str)
617 617
618 618 mode = kw.get('mode','string')
619 619 if mode not in ['string','list']:
620 620 raise ValueError('incorrect mode given: %s' % mode)
621 621 # Get options
622 622 list_all = kw.get('list_all',0)
623 623 posix = kw.get('posix', os.name == 'posix')
624 624 strict = kw.get('strict', True)
625 625
626 626 # Check if we have more than one argument to warrant extra processing:
627 627 odict = {} # Dictionary with options
628 628 args = arg_str.split()
629 629 if len(args) >= 1:
630 630 # If the list of inputs only has 0 or 1 thing in it, there's no
631 631 # need to look for options
632 632 argv = arg_split(arg_str, posix, strict)
633 633 # Do regular option processing
634 634 try:
635 635 opts,args = getopt(argv, opt_str, long_opts)
636 636 except GetoptError as e:
637 637 raise UsageError('%s ( allowed: "%s" %s)' % (e.msg,opt_str,
638 638 " ".join(long_opts)))
639 639 for o,a in opts:
640 640 if o.startswith('--'):
641 641 o = o[2:]
642 642 else:
643 643 o = o[1:]
644 644 try:
645 645 odict[o].append(a)
646 646 except AttributeError:
647 647 odict[o] = [odict[o],a]
648 648 except KeyError:
649 649 if list_all:
650 650 odict[o] = [a]
651 651 else:
652 652 odict[o] = a
653 653
654 654 # Prepare opts,args for return
655 655 opts = Struct(odict)
656 656 if mode == 'string':
657 657 args = ' '.join(args)
658 658
659 659 return opts,args
660 660
661 661 def default_option(self, fn, optstr):
662 662 """Make an entry in the options_table for fn, with value optstr"""
663 663
664 664 if fn not in self.lsmagic():
665 665 error("%s is not a magic function" % fn)
666 666 self.options_table[fn] = optstr
667 667
668 668
669 669 class MagicAlias(object):
670 670 """An alias to another magic function.
671 671
672 672 An alias is determined by its magic name and magic kind. Lookup
673 673 is done at call time, so if the underlying magic changes the alias
674 674 will call the new function.
675 675
676 676 Use the :meth:`MagicsManager.register_alias` method or the
677 677 `%alias_magic` magic function to create and register a new alias.
678 678 """
679 679 def __init__(self, shell, magic_name, magic_kind):
680 680 self.shell = shell
681 681 self.magic_name = magic_name
682 682 self.magic_kind = magic_kind
683 683
684 684 self.pretty_target = '%s%s' % (magic_escapes[self.magic_kind], self.magic_name)
685 685 self.__doc__ = "Alias for `%s`." % self.pretty_target
686 686
687 687 self._in_call = False
688 688
689 689 def __call__(self, *args, **kwargs):
690 690 """Call the magic alias."""
691 691 fn = self.shell.find_magic(self.magic_name, self.magic_kind)
692 692 if fn is None:
693 693 raise UsageError("Magic `%s` not found." % self.pretty_target)
694 694
695 695 # Protect against infinite recursion.
696 696 if self._in_call:
697 697 raise UsageError("Infinite recursion detected; "
698 698 "magic aliases cannot call themselves.")
699 699 self._in_call = True
700 700 try:
701 701 return fn(*args, **kwargs)
702 702 finally:
703 703 self._in_call = False
@@ -1,129 +1,129 b''
1 1 """Implementation of magic functions that control various automatic behaviors.
2 2 """
3 3 from __future__ import print_function
4 4 #-----------------------------------------------------------------------------
5 5 # Copyright (c) 2012 The IPython Development Team.
6 6 #
7 7 # Distributed under the terms of the Modified BSD License.
8 8 #
9 9 # The full license is in the file COPYING.txt, distributed with this software.
10 10 #-----------------------------------------------------------------------------
11 11
12 12 #-----------------------------------------------------------------------------
13 13 # Imports
14 14 #-----------------------------------------------------------------------------
15 15
16 16 # Our own packages
17 17 from IPython.core.magic import Bunch, Magics, magics_class, line_magic
18 18 from IPython.testing.skipdoctest import skip_doctest
19 from IPython.utils.warn import error
19 from logging import error
20 20
21 21 #-----------------------------------------------------------------------------
22 22 # Magic implementation classes
23 23 #-----------------------------------------------------------------------------
24 24
25 25 @magics_class
26 26 class AutoMagics(Magics):
27 27 """Magics that control various autoX behaviors."""
28 28
29 29 def __init__(self, shell):
30 30 super(AutoMagics, self).__init__(shell)
31 31 # namespace for holding state we may need
32 32 self._magic_state = Bunch()
33 33
34 34 @line_magic
35 35 def automagic(self, parameter_s=''):
36 36 """Make magic functions callable without having to type the initial %.
37 37
38 38 Without argumentsl toggles on/off (when off, you must call it as
39 39 %automagic, of course). With arguments it sets the value, and you can
40 40 use any of (case insensitive):
41 41
42 42 - on, 1, True: to activate
43 43
44 44 - off, 0, False: to deactivate.
45 45
46 46 Note that magic functions have lowest priority, so if there's a
47 47 variable whose name collides with that of a magic fn, automagic won't
48 48 work for that function (you get the variable instead). However, if you
49 49 delete the variable (del var), the previously shadowed magic function
50 50 becomes visible to automagic again."""
51 51
52 52 arg = parameter_s.lower()
53 53 mman = self.shell.magics_manager
54 54 if arg in ('on', '1', 'true'):
55 55 val = True
56 56 elif arg in ('off', '0', 'false'):
57 57 val = False
58 58 else:
59 59 val = not mman.auto_magic
60 60 mman.auto_magic = val
61 61 print('\n' + self.shell.magics_manager.auto_status())
62 62
63 63 @skip_doctest
64 64 @line_magic
65 65 def autocall(self, parameter_s=''):
66 66 """Make functions callable without having to type parentheses.
67 67
68 68 Usage:
69 69
70 70 %autocall [mode]
71 71
72 72 The mode can be one of: 0->Off, 1->Smart, 2->Full. If not given, the
73 73 value is toggled on and off (remembering the previous state).
74 74
75 75 In more detail, these values mean:
76 76
77 77 0 -> fully disabled
78 78
79 79 1 -> active, but do not apply if there are no arguments on the line.
80 80
81 81 In this mode, you get::
82 82
83 83 In [1]: callable
84 84 Out[1]: <built-in function callable>
85 85
86 86 In [2]: callable 'hello'
87 87 ------> callable('hello')
88 88 Out[2]: False
89 89
90 90 2 -> Active always. Even if no arguments are present, the callable
91 91 object is called::
92 92
93 93 In [2]: float
94 94 ------> float()
95 95 Out[2]: 0.0
96 96
97 97 Note that even with autocall off, you can still use '/' at the start of
98 98 a line to treat the first argument on the command line as a function
99 99 and add parentheses to it::
100 100
101 101 In [8]: /str 43
102 102 ------> str(43)
103 103 Out[8]: '43'
104 104
105 105 # all-random (note for auto-testing)
106 106 """
107 107
108 108 if parameter_s:
109 109 arg = int(parameter_s)
110 110 else:
111 111 arg = 'toggle'
112 112
113 113 if not arg in (0, 1, 2, 'toggle'):
114 114 error('Valid modes: (0->Off, 1->Smart, 2->Full')
115 115 return
116 116
117 117 if arg in (0, 1, 2):
118 118 self.shell.autocall = arg
119 119 else: # toggle
120 120 if self.shell.autocall:
121 121 self._magic_state.autocall_save = self.shell.autocall
122 122 self.shell.autocall = 0
123 123 else:
124 124 try:
125 125 self.shell.autocall = self._magic_state.autocall_save
126 126 except AttributeError:
127 127 self.shell.autocall = self._magic_state.autocall_save = 1
128 128
129 129 print("Automatic calling is:",['OFF','Smart','Full'][self.shell.autocall])
@@ -1,612 +1,613 b''
1 1 """Implementation of basic magic functions."""
2 2
3 3 from __future__ import print_function
4 4
5 5 import io
6 6 import sys
7 7 from pprint import pformat
8 8
9 9 from IPython.core import magic_arguments, page
10 10 from IPython.core.error import UsageError
11 11 from IPython.core.magic import Magics, magics_class, line_magic, magic_escapes
12 12 from IPython.utils.text import format_screen, dedent, indent
13 13 from IPython.testing.skipdoctest import skip_doctest
14 14 from IPython.utils.ipstruct import Struct
15 15 from IPython.utils.path import unquote_filename
16 16 from IPython.utils.py3compat import unicode_type
17 from IPython.utils.warn import warn, error
17 from warnings import warn
18 from logging import error
18 19
19 20
20 21 class MagicsDisplay(object):
21 22 def __init__(self, magics_manager):
22 23 self.magics_manager = magics_manager
23 24
24 25 def _lsmagic(self):
25 26 """The main implementation of the %lsmagic"""
26 27 mesc = magic_escapes['line']
27 28 cesc = magic_escapes['cell']
28 29 mman = self.magics_manager
29 30 magics = mman.lsmagic()
30 31 out = ['Available line magics:',
31 32 mesc + (' '+mesc).join(sorted(magics['line'])),
32 33 '',
33 34 'Available cell magics:',
34 35 cesc + (' '+cesc).join(sorted(magics['cell'])),
35 36 '',
36 37 mman.auto_status()]
37 38 return '\n'.join(out)
38 39
39 40 def _repr_pretty_(self, p, cycle):
40 41 p.text(self._lsmagic())
41 42
42 43 def __str__(self):
43 44 return self._lsmagic()
44 45
45 46 def _jsonable(self):
46 47 """turn magics dict into jsonable dict of the same structure
47 48
48 49 replaces object instances with their class names as strings
49 50 """
50 51 magic_dict = {}
51 52 mman = self.magics_manager
52 53 magics = mman.lsmagic()
53 54 for key, subdict in magics.items():
54 55 d = {}
55 56 magic_dict[key] = d
56 57 for name, obj in subdict.items():
57 58 try:
58 59 classname = obj.__self__.__class__.__name__
59 60 except AttributeError:
60 61 classname = 'Other'
61 62
62 63 d[name] = classname
63 64 return magic_dict
64 65
65 66 def _repr_json_(self):
66 67 return self._jsonable()
67 68
68 69
69 70 @magics_class
70 71 class BasicMagics(Magics):
71 72 """Magics that provide central IPython functionality.
72 73
73 74 These are various magics that don't fit into specific categories but that
74 75 are all part of the base 'IPython experience'."""
75 76
76 77 @magic_arguments.magic_arguments()
77 78 @magic_arguments.argument(
78 79 '-l', '--line', action='store_true',
79 80 help="""Create a line magic alias."""
80 81 )
81 82 @magic_arguments.argument(
82 83 '-c', '--cell', action='store_true',
83 84 help="""Create a cell magic alias."""
84 85 )
85 86 @magic_arguments.argument(
86 87 'name',
87 88 help="""Name of the magic to be created."""
88 89 )
89 90 @magic_arguments.argument(
90 91 'target',
91 92 help="""Name of the existing line or cell magic."""
92 93 )
93 94 @line_magic
94 95 def alias_magic(self, line=''):
95 96 """Create an alias for an existing line or cell magic.
96 97
97 98 Examples
98 99 --------
99 100 ::
100 101
101 102 In [1]: %alias_magic t timeit
102 103 Created `%t` as an alias for `%timeit`.
103 104 Created `%%t` as an alias for `%%timeit`.
104 105
105 106 In [2]: %t -n1 pass
106 107 1 loops, best of 3: 954 ns per loop
107 108
108 109 In [3]: %%t -n1
109 110 ...: pass
110 111 ...:
111 112 1 loops, best of 3: 954 ns per loop
112 113
113 114 In [4]: %alias_magic --cell whereami pwd
114 115 UsageError: Cell magic function `%%pwd` not found.
115 116 In [5]: %alias_magic --line whereami pwd
116 117 Created `%whereami` as an alias for `%pwd`.
117 118
118 119 In [6]: %whereami
119 120 Out[6]: u'/home/testuser'
120 121 """
121 122 args = magic_arguments.parse_argstring(self.alias_magic, line)
122 123 shell = self.shell
123 124 mman = self.shell.magics_manager
124 125 escs = ''.join(magic_escapes.values())
125 126
126 127 target = args.target.lstrip(escs)
127 128 name = args.name.lstrip(escs)
128 129
129 130 # Find the requested magics.
130 131 m_line = shell.find_magic(target, 'line')
131 132 m_cell = shell.find_magic(target, 'cell')
132 133 if args.line and m_line is None:
133 134 raise UsageError('Line magic function `%s%s` not found.' %
134 135 (magic_escapes['line'], target))
135 136 if args.cell and m_cell is None:
136 137 raise UsageError('Cell magic function `%s%s` not found.' %
137 138 (magic_escapes['cell'], target))
138 139
139 140 # If --line and --cell are not specified, default to the ones
140 141 # that are available.
141 142 if not args.line and not args.cell:
142 143 if not m_line and not m_cell:
143 144 raise UsageError(
144 145 'No line or cell magic with name `%s` found.' % target
145 146 )
146 147 args.line = bool(m_line)
147 148 args.cell = bool(m_cell)
148 149
149 150 if args.line:
150 151 mman.register_alias(name, target, 'line')
151 152 print('Created `%s%s` as an alias for `%s%s`.' % (
152 153 magic_escapes['line'], name,
153 154 magic_escapes['line'], target))
154 155
155 156 if args.cell:
156 157 mman.register_alias(name, target, 'cell')
157 158 print('Created `%s%s` as an alias for `%s%s`.' % (
158 159 magic_escapes['cell'], name,
159 160 magic_escapes['cell'], target))
160 161
161 162 @line_magic
162 163 def lsmagic(self, parameter_s=''):
163 164 """List currently available magic functions."""
164 165 return MagicsDisplay(self.shell.magics_manager)
165 166
166 167 def _magic_docs(self, brief=False, rest=False):
167 168 """Return docstrings from magic functions."""
168 169 mman = self.shell.magics_manager
169 170 docs = mman.lsmagic_docs(brief, missing='No documentation')
170 171
171 172 if rest:
172 173 format_string = '**%s%s**::\n\n%s\n\n'
173 174 else:
174 175 format_string = '%s%s:\n%s\n'
175 176
176 177 return ''.join(
177 178 [format_string % (magic_escapes['line'], fname,
178 179 indent(dedent(fndoc)))
179 180 for fname, fndoc in sorted(docs['line'].items())]
180 181 +
181 182 [format_string % (magic_escapes['cell'], fname,
182 183 indent(dedent(fndoc)))
183 184 for fname, fndoc in sorted(docs['cell'].items())]
184 185 )
185 186
186 187 @line_magic
187 188 def magic(self, parameter_s=''):
188 189 """Print information about the magic function system.
189 190
190 191 Supported formats: -latex, -brief, -rest
191 192 """
192 193
193 194 mode = ''
194 195 try:
195 196 mode = parameter_s.split()[0][1:]
196 197 except IndexError:
197 198 pass
198 199
199 200 brief = (mode == 'brief')
200 201 rest = (mode == 'rest')
201 202 magic_docs = self._magic_docs(brief, rest)
202 203
203 204 if mode == 'latex':
204 205 print(self.format_latex(magic_docs))
205 206 return
206 207 else:
207 208 magic_docs = format_screen(magic_docs)
208 209
209 210 out = ["""
210 211 IPython's 'magic' functions
211 212 ===========================
212 213
213 214 The magic function system provides a series of functions which allow you to
214 215 control the behavior of IPython itself, plus a lot of system-type
215 216 features. There are two kinds of magics, line-oriented and cell-oriented.
216 217
217 218 Line magics are prefixed with the % character and work much like OS
218 219 command-line calls: they get as an argument the rest of the line, where
219 220 arguments are passed without parentheses or quotes. For example, this will
220 221 time the given statement::
221 222
222 223 %timeit range(1000)
223 224
224 225 Cell magics are prefixed with a double %%, and they are functions that get as
225 226 an argument not only the rest of the line, but also the lines below it in a
226 227 separate argument. These magics are called with two arguments: the rest of the
227 228 call line and the body of the cell, consisting of the lines below the first.
228 229 For example::
229 230
230 231 %%timeit x = numpy.random.randn((100, 100))
231 232 numpy.linalg.svd(x)
232 233
233 234 will time the execution of the numpy svd routine, running the assignment of x
234 235 as part of the setup phase, which is not timed.
235 236
236 237 In a line-oriented client (the terminal or Qt console IPython), starting a new
237 238 input with %% will automatically enter cell mode, and IPython will continue
238 239 reading input until a blank line is given. In the notebook, simply type the
239 240 whole cell as one entity, but keep in mind that the %% escape can only be at
240 241 the very start of the cell.
241 242
242 243 NOTE: If you have 'automagic' enabled (via the command line option or with the
243 244 %automagic function), you don't need to type in the % explicitly for line
244 245 magics; cell magics always require an explicit '%%' escape. By default,
245 246 IPython ships with automagic on, so you should only rarely need the % escape.
246 247
247 248 Example: typing '%cd mydir' (without the quotes) changes your working directory
248 249 to 'mydir', if it exists.
249 250
250 251 For a list of the available magic functions, use %lsmagic. For a description
251 252 of any of them, type %magic_name?, e.g. '%cd?'.
252 253
253 254 Currently the magic system has the following functions:""",
254 255 magic_docs,
255 256 "Summary of magic functions (from %slsmagic):" % magic_escapes['line'],
256 257 str(self.lsmagic()),
257 258 ]
258 259 page.page('\n'.join(out))
259 260
260 261
261 262 @line_magic
262 263 def page(self, parameter_s=''):
263 264 """Pretty print the object and display it through a pager.
264 265
265 266 %page [options] OBJECT
266 267
267 268 If no object is given, use _ (last output).
268 269
269 270 Options:
270 271
271 272 -r: page str(object), don't pretty-print it."""
272 273
273 274 # After a function contributed by Olivier Aubert, slightly modified.
274 275
275 276 # Process options/args
276 277 opts, args = self.parse_options(parameter_s, 'r')
277 278 raw = 'r' in opts
278 279
279 280 oname = args and args or '_'
280 281 info = self.shell._ofind(oname)
281 282 if info['found']:
282 283 txt = (raw and str or pformat)( info['obj'] )
283 284 page.page(txt)
284 285 else:
285 286 print('Object `%s` not found' % oname)
286 287
287 288 @line_magic
288 289 def profile(self, parameter_s=''):
289 290 """Print your currently active IPython profile.
290 291
291 292 See Also
292 293 --------
293 294 prun : run code using the Python profiler
294 295 (:meth:`~IPython.core.magics.execution.ExecutionMagics.prun`)
295 296 """
296 297 warn("%profile is now deprecated. Please use get_ipython().profile instead.")
297 298 from IPython.core.application import BaseIPythonApplication
298 299 if BaseIPythonApplication.initialized():
299 300 print(BaseIPythonApplication.instance().profile)
300 301 else:
301 302 error("profile is an application-level value, but you don't appear to be in an IPython application")
302 303
303 304 @line_magic
304 305 def pprint(self, parameter_s=''):
305 306 """Toggle pretty printing on/off."""
306 307 ptformatter = self.shell.display_formatter.formatters['text/plain']
307 308 ptformatter.pprint = bool(1 - ptformatter.pprint)
308 309 print('Pretty printing has been turned',
309 310 ['OFF','ON'][ptformatter.pprint])
310 311
311 312 @line_magic
312 313 def colors(self, parameter_s=''):
313 314 """Switch color scheme for prompts, info system and exception handlers.
314 315
315 316 Currently implemented schemes: NoColor, Linux, LightBG.
316 317
317 318 Color scheme names are not case-sensitive.
318 319
319 320 Examples
320 321 --------
321 322 To get a plain black and white terminal::
322 323
323 324 %colors nocolor
324 325 """
325 326 def color_switch_err(name):
326 327 warn('Error changing %s color schemes.\n%s' %
327 328 (name, sys.exc_info()[1]))
328 329
329 330
330 331 new_scheme = parameter_s.strip()
331 332 if not new_scheme:
332 333 raise UsageError(
333 334 "%colors: you must specify a color scheme. See '%colors?'")
334 335 # local shortcut
335 336 shell = self.shell
336 337
337 338
338 339
339 340 if not shell.colors_force:
340 341 if sys.platform in {'win32', 'cli'}:
341 342 import IPython.utils.rlineimpl as readline
342 343 if not readline.have_readline:
343 344 msg = """\
344 345 Proper color support under MS Windows requires the pyreadline library.
345 346 You can find it at:
346 347 http://ipython.org/pyreadline.html
347 348
348 349 Defaulting color scheme to 'NoColor'"""
349 350 new_scheme = 'NoColor'
350 351 warn(msg)
351 352
352 353 elif not shell.has_readline:
353 354 # Coloured prompts get messed up without readline
354 355 # Will remove this check after switching to prompt_toolkit
355 356 new_scheme = 'NoColor'
356 357
357 358 # Set prompt colors
358 359 try:
359 360 shell.prompt_manager.color_scheme = new_scheme
360 361 except:
361 362 color_switch_err('prompt')
362 363 else:
363 364 shell.colors = \
364 365 shell.prompt_manager.color_scheme_table.active_scheme_name
365 366 # Set exception colors
366 367 try:
367 368 shell.InteractiveTB.set_colors(scheme = new_scheme)
368 369 shell.SyntaxTB.set_colors(scheme = new_scheme)
369 370 except:
370 371 color_switch_err('exception')
371 372
372 373 # Set info (for 'object?') colors
373 374 if shell.color_info:
374 375 try:
375 376 shell.inspector.set_active_scheme(new_scheme)
376 377 except:
377 378 color_switch_err('object inspector')
378 379 else:
379 380 shell.inspector.set_active_scheme('NoColor')
380 381
381 382 @line_magic
382 383 def xmode(self, parameter_s=''):
383 384 """Switch modes for the exception handlers.
384 385
385 386 Valid modes: Plain, Context and Verbose.
386 387
387 388 If called without arguments, acts as a toggle."""
388 389
389 390 def xmode_switch_err(name):
390 391 warn('Error changing %s exception modes.\n%s' %
391 392 (name,sys.exc_info()[1]))
392 393
393 394 shell = self.shell
394 395 new_mode = parameter_s.strip().capitalize()
395 396 try:
396 397 shell.InteractiveTB.set_mode(mode=new_mode)
397 398 print('Exception reporting mode:',shell.InteractiveTB.mode)
398 399 except:
399 400 xmode_switch_err('user')
400 401
401 402 @line_magic
402 403 def quickref(self,arg):
403 404 """ Show a quick reference sheet """
404 405 from IPython.core.usage import quick_reference
405 406 qr = quick_reference + self._magic_docs(brief=True)
406 407 page.page(qr)
407 408
408 409 @line_magic
409 410 def doctest_mode(self, parameter_s=''):
410 411 """Toggle doctest mode on and off.
411 412
412 413 This mode is intended to make IPython behave as much as possible like a
413 414 plain Python shell, from the perspective of how its prompts, exceptions
414 415 and output look. This makes it easy to copy and paste parts of a
415 416 session into doctests. It does so by:
416 417
417 418 - Changing the prompts to the classic ``>>>`` ones.
418 419 - Changing the exception reporting mode to 'Plain'.
419 420 - Disabling pretty-printing of output.
420 421
421 422 Note that IPython also supports the pasting of code snippets that have
422 423 leading '>>>' and '...' prompts in them. This means that you can paste
423 424 doctests from files or docstrings (even if they have leading
424 425 whitespace), and the code will execute correctly. You can then use
425 426 '%history -t' to see the translated history; this will give you the
426 427 input after removal of all the leading prompts and whitespace, which
427 428 can be pasted back into an editor.
428 429
429 430 With these features, you can switch into this mode easily whenever you
430 431 need to do testing and changes to doctests, without having to leave
431 432 your existing IPython session.
432 433 """
433 434
434 435 # Shorthands
435 436 shell = self.shell
436 437 pm = shell.prompt_manager
437 438 meta = shell.meta
438 439 disp_formatter = self.shell.display_formatter
439 440 ptformatter = disp_formatter.formatters['text/plain']
440 441 # dstore is a data store kept in the instance metadata bag to track any
441 442 # changes we make, so we can undo them later.
442 443 dstore = meta.setdefault('doctest_mode',Struct())
443 444 save_dstore = dstore.setdefault
444 445
445 446 # save a few values we'll need to recover later
446 447 mode = save_dstore('mode',False)
447 448 save_dstore('rc_pprint',ptformatter.pprint)
448 449 save_dstore('xmode',shell.InteractiveTB.mode)
449 450 save_dstore('rc_separate_out',shell.separate_out)
450 451 save_dstore('rc_separate_out2',shell.separate_out2)
451 452 save_dstore('rc_prompts_pad_left',pm.justify)
452 453 save_dstore('rc_separate_in',shell.separate_in)
453 454 save_dstore('rc_active_types',disp_formatter.active_types)
454 455 save_dstore('prompt_templates',(pm.in_template, pm.in2_template, pm.out_template))
455 456
456 457 if not mode:
457 458 # turn on
458 459 pm.in_template = '>>> '
459 460 pm.in2_template = '... '
460 461 pm.out_template = ''
461 462
462 463 # Prompt separators like plain python
463 464 shell.separate_in = ''
464 465 shell.separate_out = ''
465 466 shell.separate_out2 = ''
466 467
467 468 pm.justify = False
468 469
469 470 ptformatter.pprint = False
470 471 disp_formatter.active_types = ['text/plain']
471 472
472 473 shell.magic('xmode Plain')
473 474 else:
474 475 # turn off
475 476 pm.in_template, pm.in2_template, pm.out_template = dstore.prompt_templates
476 477
477 478 shell.separate_in = dstore.rc_separate_in
478 479
479 480 shell.separate_out = dstore.rc_separate_out
480 481 shell.separate_out2 = dstore.rc_separate_out2
481 482
482 483 pm.justify = dstore.rc_prompts_pad_left
483 484
484 485 ptformatter.pprint = dstore.rc_pprint
485 486 disp_formatter.active_types = dstore.rc_active_types
486 487
487 488 shell.magic('xmode ' + dstore.xmode)
488 489
489 490 # Store new mode and inform
490 491 dstore.mode = bool(1-int(mode))
491 492 mode_label = ['OFF','ON'][dstore.mode]
492 493 print('Doctest mode is:', mode_label)
493 494
494 495 @line_magic
495 496 def gui(self, parameter_s=''):
496 497 """Enable or disable IPython GUI event loop integration.
497 498
498 499 %gui [GUINAME]
499 500
500 501 This magic replaces IPython's threaded shells that were activated
501 502 using the (pylab/wthread/etc.) command line flags. GUI toolkits
502 503 can now be enabled at runtime and keyboard
503 504 interrupts should work without any problems. The following toolkits
504 505 are supported: wxPython, PyQt4, PyGTK, Tk and Cocoa (OSX)::
505 506
506 507 %gui wx # enable wxPython event loop integration
507 508 %gui qt4|qt # enable PyQt4 event loop integration
508 509 %gui qt5 # enable PyQt5 event loop integration
509 510 %gui gtk # enable PyGTK event loop integration
510 511 %gui gtk3 # enable Gtk3 event loop integration
511 512 %gui tk # enable Tk event loop integration
512 513 %gui osx # enable Cocoa event loop integration
513 514 # (requires %matplotlib 1.1)
514 515 %gui # disable all event loop integration
515 516
516 517 WARNING: after any of these has been called you can simply create
517 518 an application object, but DO NOT start the event loop yourself, as
518 519 we have already handled that.
519 520 """
520 521 opts, arg = self.parse_options(parameter_s, '')
521 522 if arg=='': arg = None
522 523 try:
523 524 return self.shell.enable_gui(arg)
524 525 except Exception as e:
525 526 # print simple error message, rather than traceback if we can't
526 527 # hook up the GUI
527 528 error(str(e))
528 529
529 530 @skip_doctest
530 531 @line_magic
531 532 def precision(self, s=''):
532 533 """Set floating point precision for pretty printing.
533 534
534 535 Can set either integer precision or a format string.
535 536
536 537 If numpy has been imported and precision is an int,
537 538 numpy display precision will also be set, via ``numpy.set_printoptions``.
538 539
539 540 If no argument is given, defaults will be restored.
540 541
541 542 Examples
542 543 --------
543 544 ::
544 545
545 546 In [1]: from math import pi
546 547
547 548 In [2]: %precision 3
548 549 Out[2]: u'%.3f'
549 550
550 551 In [3]: pi
551 552 Out[3]: 3.142
552 553
553 554 In [4]: %precision %i
554 555 Out[4]: u'%i'
555 556
556 557 In [5]: pi
557 558 Out[5]: 3
558 559
559 560 In [6]: %precision %e
560 561 Out[6]: u'%e'
561 562
562 563 In [7]: pi**10
563 564 Out[7]: 9.364805e+04
564 565
565 566 In [8]: %precision
566 567 Out[8]: u'%r'
567 568
568 569 In [9]: pi**10
569 570 Out[9]: 93648.047476082982
570 571 """
571 572 ptformatter = self.shell.display_formatter.formatters['text/plain']
572 573 ptformatter.float_precision = s
573 574 return ptformatter.float_format
574 575
575 576 @magic_arguments.magic_arguments()
576 577 @magic_arguments.argument(
577 578 '-e', '--export', action='store_true', default=False,
578 579 help='Export IPython history as a notebook. The filename argument '
579 580 'is used to specify the notebook name and format. For example '
580 581 'a filename of notebook.ipynb will result in a notebook name '
581 582 'of "notebook" and a format of "json". Likewise using a ".py" '
582 583 'file extension will write the notebook as a Python script'
583 584 )
584 585 @magic_arguments.argument(
585 586 'filename', type=unicode_type,
586 587 help='Notebook name or filename'
587 588 )
588 589 @line_magic
589 590 def notebook(self, s):
590 591 """Export and convert IPython notebooks.
591 592
592 593 This function can export the current IPython history to a notebook file.
593 594 For example, to export the history to "foo.ipynb" do "%notebook -e foo.ipynb".
594 595 To export the history to "foo.py" do "%notebook -e foo.py".
595 596 """
596 597 args = magic_arguments.parse_argstring(self.notebook, s)
597 598
598 599 from nbformat import write, v4
599 600 args.filename = unquote_filename(args.filename)
600 601 if args.export:
601 602 cells = []
602 603 hist = list(self.shell.history_manager.get_range())
603 604 if(len(hist)<=1):
604 605 raise ValueError('History is empty, cannot export')
605 606 for session, execution_count, source in hist[:-1]:
606 607 cells.append(v4.new_code_cell(
607 608 execution_count=execution_count,
608 609 source=source
609 610 ))
610 611 nb = v4.new_notebook(cells=cells)
611 612 with io.open(args.filename, 'w', encoding='utf-8') as f:
612 613 write(nb, f, version=4)
@@ -1,715 +1,716 b''
1 1 """Implementation of code management magic functions.
2 2 """
3 3 from __future__ import print_function
4 4 #-----------------------------------------------------------------------------
5 5 # Copyright (c) 2012 The IPython Development Team.
6 6 #
7 7 # Distributed under the terms of the Modified BSD License.
8 8 #
9 9 # The full license is in the file COPYING.txt, distributed with this software.
10 10 #-----------------------------------------------------------------------------
11 11
12 12 #-----------------------------------------------------------------------------
13 13 # Imports
14 14 #-----------------------------------------------------------------------------
15 15
16 16 # Stdlib
17 17 import inspect
18 18 import io
19 19 import os
20 20 import re
21 21 import sys
22 22 import ast
23 23 from itertools import chain
24 24
25 25 # Our own packages
26 26 from IPython.core.error import TryNext, StdinNotImplementedError, UsageError
27 27 from IPython.core.macro import Macro
28 28 from IPython.core.magic import Magics, magics_class, line_magic
29 29 from IPython.core.oinspect import find_file, find_source_lines
30 30 from IPython.testing.skipdoctest import skip_doctest
31 31 from IPython.utils import py3compat
32 32 from IPython.utils.py3compat import string_types
33 33 from IPython.utils.contexts import preserve_keys
34 34 from IPython.utils.path import get_py_filename, unquote_filename
35 from IPython.utils.warn import warn, error
35 from warnings import warn
36 from logging import error
36 37 from IPython.utils.text import get_text_list
37 38
38 39 #-----------------------------------------------------------------------------
39 40 # Magic implementation classes
40 41 #-----------------------------------------------------------------------------
41 42
42 43 # Used for exception handling in magic_edit
43 44 class MacroToEdit(ValueError): pass
44 45
45 46 ipython_input_pat = re.compile(r"<ipython\-input\-(\d+)-[a-z\d]+>$")
46 47
47 48 # To match, e.g. 8-10 1:5 :10 3-
48 49 range_re = re.compile(r"""
49 50 (?P<start>\d+)?
50 51 ((?P<sep>[\-:])
51 52 (?P<end>\d+)?)?
52 53 $""", re.VERBOSE)
53 54
54 55
55 56 def extract_code_ranges(ranges_str):
56 57 """Turn a string of range for %%load into 2-tuples of (start, stop)
57 58 ready to use as a slice of the content splitted by lines.
58 59
59 60 Examples
60 61 --------
61 62 list(extract_input_ranges("5-10 2"))
62 63 [(4, 10), (1, 2)]
63 64 """
64 65 for range_str in ranges_str.split():
65 66 rmatch = range_re.match(range_str)
66 67 if not rmatch:
67 68 continue
68 69 sep = rmatch.group("sep")
69 70 start = rmatch.group("start")
70 71 end = rmatch.group("end")
71 72
72 73 if sep == '-':
73 74 start = int(start) - 1 if start else None
74 75 end = int(end) if end else None
75 76 elif sep == ':':
76 77 start = int(start) - 1 if start else None
77 78 end = int(end) - 1 if end else None
78 79 else:
79 80 end = int(start)
80 81 start = int(start) - 1
81 82 yield (start, end)
82 83
83 84
84 85 @skip_doctest
85 86 def extract_symbols(code, symbols):
86 87 """
87 88 Return a tuple (blocks, not_found)
88 89 where ``blocks`` is a list of code fragments
89 90 for each symbol parsed from code, and ``not_found`` are
90 91 symbols not found in the code.
91 92
92 93 For example::
93 94
94 95 >>> code = '''a = 10
95 96
96 97 def b(): return 42
97 98
98 99 class A: pass'''
99 100
100 101 >>> extract_symbols(code, 'A,b,z')
101 102 (["class A: pass", "def b(): return 42"], ['z'])
102 103 """
103 104 symbols = symbols.split(',')
104 105
105 106 # this will raise SyntaxError if code isn't valid Python
106 107 py_code = ast.parse(code)
107 108
108 109 marks = [(getattr(s, 'name', None), s.lineno) for s in py_code.body]
109 110 code = code.split('\n')
110 111
111 112 symbols_lines = {}
112 113
113 114 # we already know the start_lineno of each symbol (marks).
114 115 # To find each end_lineno, we traverse in reverse order until each
115 116 # non-blank line
116 117 end = len(code)
117 118 for name, start in reversed(marks):
118 119 while not code[end - 1].strip():
119 120 end -= 1
120 121 if name:
121 122 symbols_lines[name] = (start - 1, end)
122 123 end = start - 1
123 124
124 125 # Now symbols_lines is a map
125 126 # {'symbol_name': (start_lineno, end_lineno), ...}
126 127
127 128 # fill a list with chunks of codes for each requested symbol
128 129 blocks = []
129 130 not_found = []
130 131 for symbol in symbols:
131 132 if symbol in symbols_lines:
132 133 start, end = symbols_lines[symbol]
133 134 blocks.append('\n'.join(code[start:end]) + '\n')
134 135 else:
135 136 not_found.append(symbol)
136 137
137 138 return blocks, not_found
138 139
139 140
140 141 class InteractivelyDefined(Exception):
141 142 """Exception for interactively defined variable in magic_edit"""
142 143 def __init__(self, index):
143 144 self.index = index
144 145
145 146
146 147 @magics_class
147 148 class CodeMagics(Magics):
148 149 """Magics related to code management (loading, saving, editing, ...)."""
149 150
150 151 def __init__(self, *args, **kwargs):
151 152 self._knowntemps = set()
152 153 super(CodeMagics, self).__init__(*args, **kwargs)
153 154
154 155 @line_magic
155 156 def save(self, parameter_s=''):
156 157 """Save a set of lines or a macro to a given filename.
157 158
158 159 Usage:\\
159 160 %save [options] filename n1-n2 n3-n4 ... n5 .. n6 ...
160 161
161 162 Options:
162 163
163 164 -r: use 'raw' input. By default, the 'processed' history is used,
164 165 so that magics are loaded in their transformed version to valid
165 166 Python. If this option is given, the raw input as typed as the
166 167 command line is used instead.
167 168
168 169 -f: force overwrite. If file exists, %save will prompt for overwrite
169 170 unless -f is given.
170 171
171 172 -a: append to the file instead of overwriting it.
172 173
173 174 This function uses the same syntax as %history for input ranges,
174 175 then saves the lines to the filename you specify.
175 176
176 177 It adds a '.py' extension to the file if you don't do so yourself, and
177 178 it asks for confirmation before overwriting existing files.
178 179
179 180 If `-r` option is used, the default extension is `.ipy`.
180 181 """
181 182
182 183 opts,args = self.parse_options(parameter_s,'fra',mode='list')
183 184 if not args:
184 185 raise UsageError('Missing filename.')
185 186 raw = 'r' in opts
186 187 force = 'f' in opts
187 188 append = 'a' in opts
188 189 mode = 'a' if append else 'w'
189 190 ext = u'.ipy' if raw else u'.py'
190 191 fname, codefrom = unquote_filename(args[0]), " ".join(args[1:])
191 192 if not fname.endswith((u'.py',u'.ipy')):
192 193 fname += ext
193 194 file_exists = os.path.isfile(fname)
194 195 if file_exists and not force and not append:
195 196 try:
196 197 overwrite = self.shell.ask_yes_no('File `%s` exists. Overwrite (y/[N])? ' % fname, default='n')
197 198 except StdinNotImplementedError:
198 199 print("File `%s` exists. Use `%%save -f %s` to force overwrite" % (fname, parameter_s))
199 200 return
200 201 if not overwrite :
201 202 print('Operation cancelled.')
202 203 return
203 204 try:
204 205 cmds = self.shell.find_user_code(codefrom,raw)
205 206 except (TypeError, ValueError) as e:
206 207 print(e.args[0])
207 208 return
208 209 out = py3compat.cast_unicode(cmds)
209 210 with io.open(fname, mode, encoding="utf-8") as f:
210 211 if not file_exists or not append:
211 212 f.write(u"# coding: utf-8\n")
212 213 f.write(out)
213 214 # make sure we end on a newline
214 215 if not out.endswith(u'\n'):
215 216 f.write(u'\n')
216 217 print('The following commands were written to file `%s`:' % fname)
217 218 print(cmds)
218 219
219 220 @line_magic
220 221 def pastebin(self, parameter_s=''):
221 222 """Upload code to Github's Gist paste bin, returning the URL.
222 223
223 224 Usage:\\
224 225 %pastebin [-d "Custom description"] 1-7
225 226
226 227 The argument can be an input history range, a filename, or the name of a
227 228 string or macro.
228 229
229 230 Options:
230 231
231 232 -d: Pass a custom description for the gist. The default will say
232 233 "Pasted from IPython".
233 234 """
234 235 opts, args = self.parse_options(parameter_s, 'd:')
235 236
236 237 try:
237 238 code = self.shell.find_user_code(args)
238 239 except (ValueError, TypeError) as e:
239 240 print(e.args[0])
240 241 return
241 242
242 243 # Deferred import
243 244 try:
244 245 from urllib.request import urlopen # Py 3
245 246 except ImportError:
246 247 from urllib2 import urlopen
247 248 import json
248 249 post_data = json.dumps({
249 250 "description": opts.get('d', "Pasted from IPython"),
250 251 "public": True,
251 252 "files": {
252 253 "file1.py": {
253 254 "content": code
254 255 }
255 256 }
256 257 }).encode('utf-8')
257 258
258 259 response = urlopen("https://api.github.com/gists", post_data)
259 260 response_data = json.loads(response.read().decode('utf-8'))
260 261 return response_data['html_url']
261 262
262 263 @line_magic
263 264 def loadpy(self, arg_s):
264 265 """Alias of `%load`
265 266
266 267 `%loadpy` has gained some flexibility and dropped the requirement of a `.py`
267 268 extension. So it has been renamed simply into %load. You can look at
268 269 `%load`'s docstring for more info.
269 270 """
270 271 self.load(arg_s)
271 272
272 273 @line_magic
273 274 def load(self, arg_s):
274 275 """Load code into the current frontend.
275 276
276 277 Usage:\\
277 278 %load [options] source
278 279
279 280 where source can be a filename, URL, input history range, macro, or
280 281 element in the user namespace
281 282
282 283 Options:
283 284
284 285 -r <lines>: Specify lines or ranges of lines to load from the source.
285 286 Ranges could be specified as x-y (x..y) or in python-style x:y
286 287 (x..(y-1)). Both limits x and y can be left blank (meaning the
287 288 beginning and end of the file, respectively).
288 289
289 290 -s <symbols>: Specify function or classes to load from python source.
290 291
291 292 -y : Don't ask confirmation for loading source above 200 000 characters.
292 293
293 294 -n : Include the user's namespace when searching for source code.
294 295
295 296 This magic command can either take a local filename, a URL, an history
296 297 range (see %history) or a macro as argument, it will prompt for
297 298 confirmation before loading source with more than 200 000 characters, unless
298 299 -y flag is passed or if the frontend does not support raw_input::
299 300
300 301 %load myscript.py
301 302 %load 7-27
302 303 %load myMacro
303 304 %load http://www.example.com/myscript.py
304 305 %load -r 5-10 myscript.py
305 306 %load -r 10-20,30,40: foo.py
306 307 %load -s MyClass,wonder_function myscript.py
307 308 %load -n MyClass
308 309 %load -n my_module.wonder_function
309 310 """
310 311 opts,args = self.parse_options(arg_s,'yns:r:')
311 312
312 313 if not args:
313 314 raise UsageError('Missing filename, URL, input history range, '
314 315 'macro, or element in the user namespace.')
315 316
316 317 search_ns = 'n' in opts
317 318
318 319 contents = self.shell.find_user_code(args, search_ns=search_ns)
319 320
320 321 if 's' in opts:
321 322 try:
322 323 blocks, not_found = extract_symbols(contents, opts['s'])
323 324 except SyntaxError:
324 325 # non python code
325 326 error("Unable to parse the input as valid Python code")
326 327 return
327 328
328 329 if len(not_found) == 1:
329 330 warn('The symbol `%s` was not found' % not_found[0])
330 331 elif len(not_found) > 1:
331 332 warn('The symbols %s were not found' % get_text_list(not_found,
332 333 wrap_item_with='`')
333 334 )
334 335
335 336 contents = '\n'.join(blocks)
336 337
337 338 if 'r' in opts:
338 339 ranges = opts['r'].replace(',', ' ')
339 340 lines = contents.split('\n')
340 341 slices = extract_code_ranges(ranges)
341 342 contents = [lines[slice(*slc)] for slc in slices]
342 343 contents = '\n'.join(chain.from_iterable(contents))
343 344
344 345 l = len(contents)
345 346
346 347 # 200 000 is ~ 2500 full 80 caracter lines
347 348 # so in average, more than 5000 lines
348 349 if l > 200000 and 'y' not in opts:
349 350 try:
350 351 ans = self.shell.ask_yes_no(("The text you're trying to load seems pretty big"\
351 352 " (%d characters). Continue (y/[N]) ?" % l), default='n' )
352 353 except StdinNotImplementedError:
353 354 #asume yes if raw input not implemented
354 355 ans = True
355 356
356 357 if ans is False :
357 358 print('Operation cancelled.')
358 359 return
359 360
360 361 contents = "# %load {}\n".format(arg_s) + contents
361 362
362 363 self.shell.set_next_input(contents, replace=True)
363 364
364 365 @staticmethod
365 366 def _find_edit_target(shell, args, opts, last_call):
366 367 """Utility method used by magic_edit to find what to edit."""
367 368
368 369 def make_filename(arg):
369 370 "Make a filename from the given args"
370 371 arg = unquote_filename(arg)
371 372 try:
372 373 filename = get_py_filename(arg)
373 374 except IOError:
374 375 # If it ends with .py but doesn't already exist, assume we want
375 376 # a new file.
376 377 if arg.endswith('.py'):
377 378 filename = arg
378 379 else:
379 380 filename = None
380 381 return filename
381 382
382 383 # Set a few locals from the options for convenience:
383 384 opts_prev = 'p' in opts
384 385 opts_raw = 'r' in opts
385 386
386 387 # custom exceptions
387 388 class DataIsObject(Exception): pass
388 389
389 390 # Default line number value
390 391 lineno = opts.get('n',None)
391 392
392 393 if opts_prev:
393 394 args = '_%s' % last_call[0]
394 395 if args not in shell.user_ns:
395 396 args = last_call[1]
396 397
397 398 # by default this is done with temp files, except when the given
398 399 # arg is a filename
399 400 use_temp = True
400 401
401 402 data = ''
402 403
403 404 # First, see if the arguments should be a filename.
404 405 filename = make_filename(args)
405 406 if filename:
406 407 use_temp = False
407 408 elif args:
408 409 # Mode where user specifies ranges of lines, like in %macro.
409 410 data = shell.extract_input_lines(args, opts_raw)
410 411 if not data:
411 412 try:
412 413 # Load the parameter given as a variable. If not a string,
413 414 # process it as an object instead (below)
414 415
415 416 #print '*** args',args,'type',type(args) # dbg
416 417 data = eval(args, shell.user_ns)
417 418 if not isinstance(data, string_types):
418 419 raise DataIsObject
419 420
420 421 except (NameError,SyntaxError):
421 422 # given argument is not a variable, try as a filename
422 423 filename = make_filename(args)
423 424 if filename is None:
424 425 warn("Argument given (%s) can't be found as a variable "
425 426 "or as a filename." % args)
426 427 return (None, None, None)
427 428 use_temp = False
428 429
429 430 except DataIsObject:
430 431 # macros have a special edit function
431 432 if isinstance(data, Macro):
432 433 raise MacroToEdit(data)
433 434
434 435 # For objects, try to edit the file where they are defined
435 436 filename = find_file(data)
436 437 if filename:
437 438 if 'fakemodule' in filename.lower() and \
438 439 inspect.isclass(data):
439 440 # class created by %edit? Try to find source
440 441 # by looking for method definitions instead, the
441 442 # __module__ in those classes is FakeModule.
442 443 attrs = [getattr(data, aname) for aname in dir(data)]
443 444 for attr in attrs:
444 445 if not inspect.ismethod(attr):
445 446 continue
446 447 filename = find_file(attr)
447 448 if filename and \
448 449 'fakemodule' not in filename.lower():
449 450 # change the attribute to be the edit
450 451 # target instead
451 452 data = attr
452 453 break
453 454
454 455 m = ipython_input_pat.match(os.path.basename(filename))
455 456 if m:
456 457 raise InteractivelyDefined(int(m.groups()[0]))
457 458
458 459 datafile = 1
459 460 if filename is None:
460 461 filename = make_filename(args)
461 462 datafile = 1
462 463 if filename is not None:
463 464 # only warn about this if we get a real name
464 465 warn('Could not find file where `%s` is defined.\n'
465 466 'Opening a file named `%s`' % (args, filename))
466 467 # Now, make sure we can actually read the source (if it was
467 468 # in a temp file it's gone by now).
468 469 if datafile:
469 470 if lineno is None:
470 471 lineno = find_source_lines(data)
471 472 if lineno is None:
472 473 filename = make_filename(args)
473 474 if filename is None:
474 475 warn('The file where `%s` was defined '
475 476 'cannot be read or found.' % data)
476 477 return (None, None, None)
477 478 use_temp = False
478 479
479 480 if use_temp:
480 481 filename = shell.mktempfile(data)
481 482 print('IPython will make a temporary file named:',filename)
482 483
483 484 # use last_call to remember the state of the previous call, but don't
484 485 # let it be clobbered by successive '-p' calls.
485 486 try:
486 487 last_call[0] = shell.displayhook.prompt_count
487 488 if not opts_prev:
488 489 last_call[1] = args
489 490 except:
490 491 pass
491 492
492 493
493 494 return filename, lineno, use_temp
494 495
495 496 def _edit_macro(self,mname,macro):
496 497 """open an editor with the macro data in a file"""
497 498 filename = self.shell.mktempfile(macro.value)
498 499 self.shell.hooks.editor(filename)
499 500
500 501 # and make a new macro object, to replace the old one
501 502 with open(filename) as mfile:
502 503 mvalue = mfile.read()
503 504 self.shell.user_ns[mname] = Macro(mvalue)
504 505
505 506 @skip_doctest
506 507 @line_magic
507 508 def edit(self, parameter_s='',last_call=['','']):
508 509 """Bring up an editor and execute the resulting code.
509 510
510 511 Usage:
511 512 %edit [options] [args]
512 513
513 514 %edit runs IPython's editor hook. The default version of this hook is
514 515 set to call the editor specified by your $EDITOR environment variable.
515 516 If this isn't found, it will default to vi under Linux/Unix and to
516 517 notepad under Windows. See the end of this docstring for how to change
517 518 the editor hook.
518 519
519 520 You can also set the value of this editor via the
520 521 ``TerminalInteractiveShell.editor`` option in your configuration file.
521 522 This is useful if you wish to use a different editor from your typical
522 523 default with IPython (and for Windows users who typically don't set
523 524 environment variables).
524 525
525 526 This command allows you to conveniently edit multi-line code right in
526 527 your IPython session.
527 528
528 529 If called without arguments, %edit opens up an empty editor with a
529 530 temporary file and will execute the contents of this file when you
530 531 close it (don't forget to save it!).
531 532
532 533
533 534 Options:
534 535
535 536 -n <number>: open the editor at a specified line number. By default,
536 537 the IPython editor hook uses the unix syntax 'editor +N filename', but
537 538 you can configure this by providing your own modified hook if your
538 539 favorite editor supports line-number specifications with a different
539 540 syntax.
540 541
541 542 -p: this will call the editor with the same data as the previous time
542 543 it was used, regardless of how long ago (in your current session) it
543 544 was.
544 545
545 546 -r: use 'raw' input. This option only applies to input taken from the
546 547 user's history. By default, the 'processed' history is used, so that
547 548 magics are loaded in their transformed version to valid Python. If
548 549 this option is given, the raw input as typed as the command line is
549 550 used instead. When you exit the editor, it will be executed by
550 551 IPython's own processor.
551 552
552 553 -x: do not execute the edited code immediately upon exit. This is
553 554 mainly useful if you are editing programs which need to be called with
554 555 command line arguments, which you can then do using %run.
555 556
556 557
557 558 Arguments:
558 559
559 560 If arguments are given, the following possibilities exist:
560 561
561 562 - If the argument is a filename, IPython will load that into the
562 563 editor. It will execute its contents with execfile() when you exit,
563 564 loading any code in the file into your interactive namespace.
564 565
565 566 - The arguments are ranges of input history, e.g. "7 ~1/4-6".
566 567 The syntax is the same as in the %history magic.
567 568
568 569 - If the argument is a string variable, its contents are loaded
569 570 into the editor. You can thus edit any string which contains
570 571 python code (including the result of previous edits).
571 572
572 573 - If the argument is the name of an object (other than a string),
573 574 IPython will try to locate the file where it was defined and open the
574 575 editor at the point where it is defined. You can use `%edit function`
575 576 to load an editor exactly at the point where 'function' is defined,
576 577 edit it and have the file be executed automatically.
577 578
578 579 - If the object is a macro (see %macro for details), this opens up your
579 580 specified editor with a temporary file containing the macro's data.
580 581 Upon exit, the macro is reloaded with the contents of the file.
581 582
582 583 Note: opening at an exact line is only supported under Unix, and some
583 584 editors (like kedit and gedit up to Gnome 2.8) do not understand the
584 585 '+NUMBER' parameter necessary for this feature. Good editors like
585 586 (X)Emacs, vi, jed, pico and joe all do.
586 587
587 588 After executing your code, %edit will return as output the code you
588 589 typed in the editor (except when it was an existing file). This way
589 590 you can reload the code in further invocations of %edit as a variable,
590 591 via _<NUMBER> or Out[<NUMBER>], where <NUMBER> is the prompt number of
591 592 the output.
592 593
593 594 Note that %edit is also available through the alias %ed.
594 595
595 596 This is an example of creating a simple function inside the editor and
596 597 then modifying it. First, start up the editor::
597 598
598 599 In [1]: edit
599 600 Editing... done. Executing edited code...
600 601 Out[1]: 'def foo():\\n print "foo() was defined in an editing
601 602 session"\\n'
602 603
603 604 We can then call the function foo()::
604 605
605 606 In [2]: foo()
606 607 foo() was defined in an editing session
607 608
608 609 Now we edit foo. IPython automatically loads the editor with the
609 610 (temporary) file where foo() was previously defined::
610 611
611 612 In [3]: edit foo
612 613 Editing... done. Executing edited code...
613 614
614 615 And if we call foo() again we get the modified version::
615 616
616 617 In [4]: foo()
617 618 foo() has now been changed!
618 619
619 620 Here is an example of how to edit a code snippet successive
620 621 times. First we call the editor::
621 622
622 623 In [5]: edit
623 624 Editing... done. Executing edited code...
624 625 hello
625 626 Out[5]: "print 'hello'\\n"
626 627
627 628 Now we call it again with the previous output (stored in _)::
628 629
629 630 In [6]: edit _
630 631 Editing... done. Executing edited code...
631 632 hello world
632 633 Out[6]: "print 'hello world'\\n"
633 634
634 635 Now we call it with the output #8 (stored in _8, also as Out[8])::
635 636
636 637 In [7]: edit _8
637 638 Editing... done. Executing edited code...
638 639 hello again
639 640 Out[7]: "print 'hello again'\\n"
640 641
641 642
642 643 Changing the default editor hook:
643 644
644 645 If you wish to write your own editor hook, you can put it in a
645 646 configuration file which you load at startup time. The default hook
646 647 is defined in the IPython.core.hooks module, and you can use that as a
647 648 starting example for further modifications. That file also has
648 649 general instructions on how to set a new hook for use once you've
649 650 defined it."""
650 651 opts,args = self.parse_options(parameter_s,'prxn:')
651 652
652 653 try:
653 654 filename, lineno, is_temp = self._find_edit_target(self.shell,
654 655 args, opts, last_call)
655 656 except MacroToEdit as e:
656 657 self._edit_macro(args, e.args[0])
657 658 return
658 659 except InteractivelyDefined as e:
659 660 print("Editing In[%i]" % e.index)
660 661 args = str(e.index)
661 662 filename, lineno, is_temp = self._find_edit_target(self.shell,
662 663 args, opts, last_call)
663 664 if filename is None:
664 665 # nothing was found, warnings have already been issued,
665 666 # just give up.
666 667 return
667 668
668 669 if is_temp:
669 670 self._knowntemps.add(filename)
670 671 elif (filename in self._knowntemps):
671 672 is_temp = True
672 673
673 674
674 675 # do actual editing here
675 676 print('Editing...', end=' ')
676 677 sys.stdout.flush()
677 678 try:
678 679 # Quote filenames that may have spaces in them
679 680 if ' ' in filename:
680 681 filename = "'%s'" % filename
681 682 self.shell.hooks.editor(filename,lineno)
682 683 except TryNext:
683 684 warn('Could not open editor')
684 685 return
685 686
686 687 # XXX TODO: should this be generalized for all string vars?
687 688 # For now, this is special-cased to blocks created by cpaste
688 689 if args.strip() == 'pasted_block':
689 690 with open(filename, 'r') as f:
690 691 self.shell.user_ns['pasted_block'] = f.read()
691 692
692 693 if 'x' in opts: # -x prevents actual execution
693 694 print()
694 695 else:
695 696 print('done. Executing edited code...')
696 697 with preserve_keys(self.shell.user_ns, '__file__'):
697 698 if not is_temp:
698 699 self.shell.user_ns['__file__'] = filename
699 700 if 'r' in opts: # Untranslated IPython code
700 701 with open(filename, 'r') as f:
701 702 source = f.read()
702 703 self.shell.run_cell(source, store_history=False)
703 704 else:
704 705 self.shell.safe_execfile(filename, self.shell.user_ns,
705 706 self.shell.user_ns)
706 707
707 708 if is_temp:
708 709 try:
709 710 return open(filename).read()
710 711 except IOError as msg:
711 712 if msg.filename == filename:
712 713 warn('File not found. Did you forget to save?')
713 714 return
714 715 else:
715 716 self.shell.showtraceback()
@@ -1,159 +1,159 b''
1 1 """Implementation of configuration-related magic functions.
2 2 """
3 3 from __future__ import print_function
4 4 #-----------------------------------------------------------------------------
5 5 # Copyright (c) 2012 The IPython Development Team.
6 6 #
7 7 # Distributed under the terms of the Modified BSD License.
8 8 #
9 9 # The full license is in the file COPYING.txt, distributed with this software.
10 10 #-----------------------------------------------------------------------------
11 11
12 12 #-----------------------------------------------------------------------------
13 13 # Imports
14 14 #-----------------------------------------------------------------------------
15 15
16 16 # Stdlib
17 17 import re
18 18
19 19 # Our own packages
20 20 from IPython.core.error import UsageError
21 21 from IPython.core.magic import Magics, magics_class, line_magic
22 from IPython.utils.warn import error
22 from logging import error
23 23
24 24 #-----------------------------------------------------------------------------
25 25 # Magic implementation classes
26 26 #-----------------------------------------------------------------------------
27 27
28 28 reg = re.compile('^\w+\.\w+$')
29 29 @magics_class
30 30 class ConfigMagics(Magics):
31 31
32 32 def __init__(self, shell):
33 33 super(ConfigMagics, self).__init__(shell)
34 34 self.configurables = []
35 35
36 36 @line_magic
37 37 def config(self, s):
38 38 """configure IPython
39 39
40 40 %config Class[.trait=value]
41 41
42 42 This magic exposes most of the IPython config system. Any
43 43 Configurable class should be able to be configured with the simple
44 44 line::
45 45
46 46 %config Class.trait=value
47 47
48 48 Where `value` will be resolved in the user's namespace, if it is an
49 49 expression or variable name.
50 50
51 51 Examples
52 52 --------
53 53
54 54 To see what classes are available for config, pass no arguments::
55 55
56 56 In [1]: %config
57 57 Available objects for config:
58 58 TerminalInteractiveShell
59 59 HistoryManager
60 60 PrefilterManager
61 61 AliasManager
62 62 IPCompleter
63 63 PromptManager
64 64 DisplayFormatter
65 65
66 66 To view what is configurable on a given class, just pass the class
67 67 name::
68 68
69 69 In [2]: %config IPCompleter
70 70 IPCompleter options
71 71 -----------------
72 72 IPCompleter.omit__names=<Enum>
73 73 Current: 2
74 74 Choices: (0, 1, 2)
75 75 Instruct the completer to omit private method names
76 76 Specifically, when completing on ``object.<tab>``.
77 77 When 2 [default]: all names that start with '_' will be excluded.
78 78 When 1: all 'magic' names (``__foo__``) will be excluded.
79 79 When 0: nothing will be excluded.
80 80 IPCompleter.merge_completions=<CBool>
81 81 Current: True
82 82 Whether to merge completion results into a single list
83 83 If False, only the completion results from the first non-empty
84 84 completer will be returned.
85 85 IPCompleter.limit_to__all__=<CBool>
86 86 Current: False
87 87 Instruct the completer to use __all__ for the completion
88 88 Specifically, when completing on ``object.<tab>``.
89 89 When True: only those names in obj.__all__ will be included.
90 90 When False [default]: the __all__ attribute is ignored
91 91 IPCompleter.greedy=<CBool>
92 92 Current: False
93 93 Activate greedy completion
94 94 This will enable completion on elements of lists, results of
95 95 function calls, etc., but can be unsafe because the code is
96 96 actually evaluated on TAB.
97 97
98 98 but the real use is in setting values::
99 99
100 100 In [3]: %config IPCompleter.greedy = True
101 101
102 102 and these values are read from the user_ns if they are variables::
103 103
104 104 In [4]: feeling_greedy=False
105 105
106 106 In [5]: %config IPCompleter.greedy = feeling_greedy
107 107
108 108 """
109 109 from traitlets.config.loader import Config
110 110 # some IPython objects are Configurable, but do not yet have
111 111 # any configurable traits. Exclude them from the effects of
112 112 # this magic, as their presence is just noise:
113 113 configurables = [ c for c in self.shell.configurables
114 114 if c.__class__.class_traits(config=True) ]
115 115 classnames = [ c.__class__.__name__ for c in configurables ]
116 116
117 117 line = s.strip()
118 118 if not line:
119 119 # print available configurable names
120 120 print("Available objects for config:")
121 121 for name in classnames:
122 122 print(" ", name)
123 123 return
124 124 elif line in classnames:
125 125 # `%config TerminalInteractiveShell` will print trait info for
126 126 # TerminalInteractiveShell
127 127 c = configurables[classnames.index(line)]
128 128 cls = c.__class__
129 129 help = cls.class_get_help(c)
130 130 # strip leading '--' from cl-args:
131 131 help = re.sub(re.compile(r'^--', re.MULTILINE), '', help)
132 132 print(help)
133 133 return
134 134 elif reg.match(line):
135 135 cls, attr = line.split('.')
136 136 return getattr(configurables[classnames.index(cls)],attr)
137 137 elif '=' not in line:
138 138 msg = "Invalid config statement: %r, "\
139 139 "should be `Class.trait = value`."
140 140
141 141 ll = line.lower()
142 142 for classname in classnames:
143 143 if ll == classname.lower():
144 144 msg = msg + '\nDid you mean %s (note the case)?' % classname
145 145 break
146 146
147 147 raise UsageError( msg % line)
148 148
149 149 # otherwise, assume we are setting configurables.
150 150 # leave quotes on args when splitting, because we want
151 151 # unquoted args to eval in user_ns
152 152 cfg = Config()
153 153 exec("cfg."+line, locals(), self.shell.user_ns)
154 154
155 155 for configurable in configurables:
156 156 try:
157 157 configurable.update_config(cfg)
158 158 except Exception as e:
159 159 error(e)
@@ -1,1362 +1,1363 b''
1 1 # -*- coding: utf-8 -*-
2 2 """Implementation of execution-related magic functions."""
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 __future__ import print_function
8 8
9 9 import ast
10 10 import bdb
11 11 import gc
12 12 import itertools
13 13 import os
14 14 import sys
15 15 import time
16 16 import timeit
17 17 from pdb import Restart
18 18
19 19 # cProfile was added in Python2.5
20 20 try:
21 21 import cProfile as profile
22 22 import pstats
23 23 except ImportError:
24 24 # profile isn't bundled by default in Debian for license reasons
25 25 try:
26 26 import profile, pstats
27 27 except ImportError:
28 28 profile = pstats = None
29 29
30 30 from IPython.core import debugger, oinspect
31 31 from IPython.core import magic_arguments
32 32 from IPython.core import page
33 33 from IPython.core.error import UsageError
34 34 from IPython.core.macro import Macro
35 35 from IPython.core.magic import (Magics, magics_class, line_magic, cell_magic,
36 36 line_cell_magic, on_off, needs_local_scope)
37 37 from IPython.testing.skipdoctest import skip_doctest
38 38 from IPython.utils import py3compat
39 39 from IPython.utils.py3compat import builtin_mod, iteritems, PY3
40 40 from IPython.utils.contexts import preserve_keys
41 41 from IPython.utils.capture import capture_output
42 42 from IPython.utils.ipstruct import Struct
43 43 from IPython.utils.module_paths import find_mod
44 44 from IPython.utils.path import get_py_filename, unquote_filename, shellglob
45 45 from IPython.utils.timing import clock, clock2
46 from IPython.utils.warn import warn, error
46 from warnings import warn
47 from logging import error
47 48
48 49 if PY3:
49 50 from io import StringIO
50 51 else:
51 52 from StringIO import StringIO
52 53
53 54 #-----------------------------------------------------------------------------
54 55 # Magic implementation classes
55 56 #-----------------------------------------------------------------------------
56 57
57 58
58 59 class TimeitResult(object):
59 60 """
60 61 Object returned by the timeit magic with info about the run.
61 62
62 63 Contain the following attributes :
63 64
64 65 loops: (int) number of loop done per measurement
65 66 repeat: (int) number of time the mesurement has been repeated
66 67 best: (float) best execusion time / number
67 68 all_runs: (list of float) execusion time of each run (in s)
68 69 compile_time: (float) time of statement compilation (s)
69 70
70 71 """
71 72
72 73 def __init__(self, loops, repeat, best, worst, all_runs, compile_time, precision):
73 74 self.loops = loops
74 75 self.repeat = repeat
75 76 self.best = best
76 77 self.worst = worst
77 78 self.all_runs = all_runs
78 79 self.compile_time = compile_time
79 80 self._precision = precision
80 81
81 82 def _repr_pretty_(self, p , cycle):
82 83 if self.loops == 1: # No s at "loops" if only one loop
83 84 unic = u"%d loop, best of %d: %s per loop" % (self.loops, self.repeat,
84 85 _format_time(self.best, self._precision))
85 86 else:
86 87 unic = u"%d loops, best of %d: %s per loop" % (self.loops, self.repeat,
87 88 _format_time(self.best, self._precision))
88 89 p.text(u'<TimeitResult : '+unic+u'>')
89 90
90 91
91 92 class TimeitTemplateFiller(ast.NodeTransformer):
92 93 """Fill in the AST template for timing execution.
93 94
94 95 This is quite closely tied to the template definition, which is in
95 96 :meth:`ExecutionMagics.timeit`.
96 97 """
97 98 def __init__(self, ast_setup, ast_stmt):
98 99 self.ast_setup = ast_setup
99 100 self.ast_stmt = ast_stmt
100 101
101 102 def visit_FunctionDef(self, node):
102 103 "Fill in the setup statement"
103 104 self.generic_visit(node)
104 105 if node.name == "inner":
105 106 node.body[:1] = self.ast_setup.body
106 107
107 108 return node
108 109
109 110 def visit_For(self, node):
110 111 "Fill in the statement to be timed"
111 112 if getattr(getattr(node.body[0], 'value', None), 'id', None) == 'stmt':
112 113 node.body = self.ast_stmt.body
113 114 return node
114 115
115 116
116 117 class Timer(timeit.Timer):
117 118 """Timer class that explicitly uses self.inner
118 119
119 120 which is an undocumented implementation detail of CPython,
120 121 not shared by PyPy.
121 122 """
122 123 # Timer.timeit copied from CPython 3.4.2
123 124 def timeit(self, number=timeit.default_number):
124 125 """Time 'number' executions of the main statement.
125 126
126 127 To be precise, this executes the setup statement once, and
127 128 then returns the time it takes to execute the main statement
128 129 a number of times, as a float measured in seconds. The
129 130 argument is the number of times through the loop, defaulting
130 131 to one million. The main statement, the setup statement and
131 132 the timer function to be used are passed to the constructor.
132 133 """
133 134 it = itertools.repeat(None, number)
134 135 gcold = gc.isenabled()
135 136 gc.disable()
136 137 try:
137 138 timing = self.inner(it, self.timer)
138 139 finally:
139 140 if gcold:
140 141 gc.enable()
141 142 return timing
142 143
143 144
144 145 @magics_class
145 146 class ExecutionMagics(Magics):
146 147 """Magics related to code execution, debugging, profiling, etc.
147 148
148 149 """
149 150
150 151 def __init__(self, shell):
151 152 super(ExecutionMagics, self).__init__(shell)
152 153 if profile is None:
153 154 self.prun = self.profile_missing_notice
154 155 # Default execution function used to actually run user code.
155 156 self.default_runner = None
156 157
157 158 def profile_missing_notice(self, *args, **kwargs):
158 159 error("""\
159 160 The profile module could not be found. It has been removed from the standard
160 161 python packages because of its non-free license. To use profiling, install the
161 162 python-profiler package from non-free.""")
162 163
163 164 @skip_doctest
164 165 @line_cell_magic
165 166 def prun(self, parameter_s='', cell=None):
166 167
167 168 """Run a statement through the python code profiler.
168 169
169 170 Usage, in line mode:
170 171 %prun [options] statement
171 172
172 173 Usage, in cell mode:
173 174 %%prun [options] [statement]
174 175 code...
175 176 code...
176 177
177 178 In cell mode, the additional code lines are appended to the (possibly
178 179 empty) statement in the first line. Cell mode allows you to easily
179 180 profile multiline blocks without having to put them in a separate
180 181 function.
181 182
182 183 The given statement (which doesn't require quote marks) is run via the
183 184 python profiler in a manner similar to the profile.run() function.
184 185 Namespaces are internally managed to work correctly; profile.run
185 186 cannot be used in IPython because it makes certain assumptions about
186 187 namespaces which do not hold under IPython.
187 188
188 189 Options:
189 190
190 191 -l <limit>
191 192 you can place restrictions on what or how much of the
192 193 profile gets printed. The limit value can be:
193 194
194 195 * A string: only information for function names containing this string
195 196 is printed.
196 197
197 198 * An integer: only these many lines are printed.
198 199
199 200 * A float (between 0 and 1): this fraction of the report is printed
200 201 (for example, use a limit of 0.4 to see the topmost 40% only).
201 202
202 203 You can combine several limits with repeated use of the option. For
203 204 example, ``-l __init__ -l 5`` will print only the topmost 5 lines of
204 205 information about class constructors.
205 206
206 207 -r
207 208 return the pstats.Stats object generated by the profiling. This
208 209 object has all the information about the profile in it, and you can
209 210 later use it for further analysis or in other functions.
210 211
211 212 -s <key>
212 213 sort profile by given key. You can provide more than one key
213 214 by using the option several times: '-s key1 -s key2 -s key3...'. The
214 215 default sorting key is 'time'.
215 216
216 217 The following is copied verbatim from the profile documentation
217 218 referenced below:
218 219
219 220 When more than one key is provided, additional keys are used as
220 221 secondary criteria when the there is equality in all keys selected
221 222 before them.
222 223
223 224 Abbreviations can be used for any key names, as long as the
224 225 abbreviation is unambiguous. The following are the keys currently
225 226 defined:
226 227
227 228 ============ =====================
228 229 Valid Arg Meaning
229 230 ============ =====================
230 231 "calls" call count
231 232 "cumulative" cumulative time
232 233 "file" file name
233 234 "module" file name
234 235 "pcalls" primitive call count
235 236 "line" line number
236 237 "name" function name
237 238 "nfl" name/file/line
238 239 "stdname" standard name
239 240 "time" internal time
240 241 ============ =====================
241 242
242 243 Note that all sorts on statistics are in descending order (placing
243 244 most time consuming items first), where as name, file, and line number
244 245 searches are in ascending order (i.e., alphabetical). The subtle
245 246 distinction between "nfl" and "stdname" is that the standard name is a
246 247 sort of the name as printed, which means that the embedded line
247 248 numbers get compared in an odd way. For example, lines 3, 20, and 40
248 249 would (if the file names were the same) appear in the string order
249 250 "20" "3" and "40". In contrast, "nfl" does a numeric compare of the
250 251 line numbers. In fact, sort_stats("nfl") is the same as
251 252 sort_stats("name", "file", "line").
252 253
253 254 -T <filename>
254 255 save profile results as shown on screen to a text
255 256 file. The profile is still shown on screen.
256 257
257 258 -D <filename>
258 259 save (via dump_stats) profile statistics to given
259 260 filename. This data is in a format understood by the pstats module, and
260 261 is generated by a call to the dump_stats() method of profile
261 262 objects. The profile is still shown on screen.
262 263
263 264 -q
264 265 suppress output to the pager. Best used with -T and/or -D above.
265 266
266 267 If you want to run complete programs under the profiler's control, use
267 268 ``%run -p [prof_opts] filename.py [args to program]`` where prof_opts
268 269 contains profiler specific options as described here.
269 270
270 271 You can read the complete documentation for the profile module with::
271 272
272 273 In [1]: import profile; profile.help()
273 274 """
274 275 opts, arg_str = self.parse_options(parameter_s, 'D:l:rs:T:q',
275 276 list_all=True, posix=False)
276 277 if cell is not None:
277 278 arg_str += '\n' + cell
278 279 arg_str = self.shell.input_splitter.transform_cell(arg_str)
279 280 return self._run_with_profiler(arg_str, opts, self.shell.user_ns)
280 281
281 282 def _run_with_profiler(self, code, opts, namespace):
282 283 """
283 284 Run `code` with profiler. Used by ``%prun`` and ``%run -p``.
284 285
285 286 Parameters
286 287 ----------
287 288 code : str
288 289 Code to be executed.
289 290 opts : Struct
290 291 Options parsed by `self.parse_options`.
291 292 namespace : dict
292 293 A dictionary for Python namespace (e.g., `self.shell.user_ns`).
293 294
294 295 """
295 296
296 297 # Fill default values for unspecified options:
297 298 opts.merge(Struct(D=[''], l=[], s=['time'], T=['']))
298 299
299 300 prof = profile.Profile()
300 301 try:
301 302 prof = prof.runctx(code, namespace, namespace)
302 303 sys_exit = ''
303 304 except SystemExit:
304 305 sys_exit = """*** SystemExit exception caught in code being profiled."""
305 306
306 307 stats = pstats.Stats(prof).strip_dirs().sort_stats(*opts.s)
307 308
308 309 lims = opts.l
309 310 if lims:
310 311 lims = [] # rebuild lims with ints/floats/strings
311 312 for lim in opts.l:
312 313 try:
313 314 lims.append(int(lim))
314 315 except ValueError:
315 316 try:
316 317 lims.append(float(lim))
317 318 except ValueError:
318 319 lims.append(lim)
319 320
320 321 # Trap output.
321 322 stdout_trap = StringIO()
322 323 stats_stream = stats.stream
323 324 try:
324 325 stats.stream = stdout_trap
325 326 stats.print_stats(*lims)
326 327 finally:
327 328 stats.stream = stats_stream
328 329
329 330 output = stdout_trap.getvalue()
330 331 output = output.rstrip()
331 332
332 333 if 'q' not in opts:
333 334 page.page(output)
334 335 print(sys_exit, end=' ')
335 336
336 337 dump_file = opts.D[0]
337 338 text_file = opts.T[0]
338 339 if dump_file:
339 340 dump_file = unquote_filename(dump_file)
340 341 prof.dump_stats(dump_file)
341 342 print('\n*** Profile stats marshalled to file',\
342 343 repr(dump_file)+'.',sys_exit)
343 344 if text_file:
344 345 text_file = unquote_filename(text_file)
345 346 pfile = open(text_file,'w')
346 347 pfile.write(output)
347 348 pfile.close()
348 349 print('\n*** Profile printout saved to text file',\
349 350 repr(text_file)+'.',sys_exit)
350 351
351 352 if 'r' in opts:
352 353 return stats
353 354 else:
354 355 return None
355 356
356 357 @line_magic
357 358 def pdb(self, parameter_s=''):
358 359 """Control the automatic calling of the pdb interactive debugger.
359 360
360 361 Call as '%pdb on', '%pdb 1', '%pdb off' or '%pdb 0'. If called without
361 362 argument it works as a toggle.
362 363
363 364 When an exception is triggered, IPython can optionally call the
364 365 interactive pdb debugger after the traceback printout. %pdb toggles
365 366 this feature on and off.
366 367
367 368 The initial state of this feature is set in your configuration
368 369 file (the option is ``InteractiveShell.pdb``).
369 370
370 371 If you want to just activate the debugger AFTER an exception has fired,
371 372 without having to type '%pdb on' and rerunning your code, you can use
372 373 the %debug magic."""
373 374
374 375 par = parameter_s.strip().lower()
375 376
376 377 if par:
377 378 try:
378 379 new_pdb = {'off':0,'0':0,'on':1,'1':1}[par]
379 380 except KeyError:
380 381 print ('Incorrect argument. Use on/1, off/0, '
381 382 'or nothing for a toggle.')
382 383 return
383 384 else:
384 385 # toggle
385 386 new_pdb = not self.shell.call_pdb
386 387
387 388 # set on the shell
388 389 self.shell.call_pdb = new_pdb
389 390 print('Automatic pdb calling has been turned',on_off(new_pdb))
390 391
391 392 @skip_doctest
392 393 @magic_arguments.magic_arguments()
393 394 @magic_arguments.argument('--breakpoint', '-b', metavar='FILE:LINE',
394 395 help="""
395 396 Set break point at LINE in FILE.
396 397 """
397 398 )
398 399 @magic_arguments.argument('statement', nargs='*',
399 400 help="""
400 401 Code to run in debugger.
401 402 You can omit this in cell magic mode.
402 403 """
403 404 )
404 405 @line_cell_magic
405 406 def debug(self, line='', cell=None):
406 407 """Activate the interactive debugger.
407 408
408 409 This magic command support two ways of activating debugger.
409 410 One is to activate debugger before executing code. This way, you
410 411 can set a break point, to step through the code from the point.
411 412 You can use this mode by giving statements to execute and optionally
412 413 a breakpoint.
413 414
414 415 The other one is to activate debugger in post-mortem mode. You can
415 416 activate this mode simply running %debug without any argument.
416 417 If an exception has just occurred, this lets you inspect its stack
417 418 frames interactively. Note that this will always work only on the last
418 419 traceback that occurred, so you must call this quickly after an
419 420 exception that you wish to inspect has fired, because if another one
420 421 occurs, it clobbers the previous one.
421 422
422 423 If you want IPython to automatically do this on every exception, see
423 424 the %pdb magic for more details.
424 425 """
425 426 args = magic_arguments.parse_argstring(self.debug, line)
426 427
427 428 if not (args.breakpoint or args.statement or cell):
428 429 self._debug_post_mortem()
429 430 else:
430 431 code = "\n".join(args.statement)
431 432 if cell:
432 433 code += "\n" + cell
433 434 self._debug_exec(code, args.breakpoint)
434 435
435 436 def _debug_post_mortem(self):
436 437 self.shell.debugger(force=True)
437 438
438 439 def _debug_exec(self, code, breakpoint):
439 440 if breakpoint:
440 441 (filename, bp_line) = breakpoint.split(':', 1)
441 442 bp_line = int(bp_line)
442 443 else:
443 444 (filename, bp_line) = (None, None)
444 445 self._run_with_debugger(code, self.shell.user_ns, filename, bp_line)
445 446
446 447 @line_magic
447 448 def tb(self, s):
448 449 """Print the last traceback with the currently active exception mode.
449 450
450 451 See %xmode for changing exception reporting modes."""
451 452 self.shell.showtraceback()
452 453
453 454 @skip_doctest
454 455 @line_magic
455 456 def run(self, parameter_s='', runner=None,
456 457 file_finder=get_py_filename):
457 458 """Run the named file inside IPython as a program.
458 459
459 460 Usage::
460 461
461 462 %run [-n -i -e -G]
462 463 [( -t [-N<N>] | -d [-b<N>] | -p [profile options] )]
463 464 ( -m mod | file ) [args]
464 465
465 466 Parameters after the filename are passed as command-line arguments to
466 467 the program (put in sys.argv). Then, control returns to IPython's
467 468 prompt.
468 469
469 470 This is similar to running at a system prompt ``python file args``,
470 471 but with the advantage of giving you IPython's tracebacks, and of
471 472 loading all variables into your interactive namespace for further use
472 473 (unless -p is used, see below).
473 474
474 475 The file is executed in a namespace initially consisting only of
475 476 ``__name__=='__main__'`` and sys.argv constructed as indicated. It thus
476 477 sees its environment as if it were being run as a stand-alone program
477 478 (except for sharing global objects such as previously imported
478 479 modules). But after execution, the IPython interactive namespace gets
479 480 updated with all variables defined in the program (except for __name__
480 481 and sys.argv). This allows for very convenient loading of code for
481 482 interactive work, while giving each program a 'clean sheet' to run in.
482 483
483 484 Arguments are expanded using shell-like glob match. Patterns
484 485 '*', '?', '[seq]' and '[!seq]' can be used. Additionally,
485 486 tilde '~' will be expanded into user's home directory. Unlike
486 487 real shells, quotation does not suppress expansions. Use
487 488 *two* back slashes (e.g. ``\\\\*``) to suppress expansions.
488 489 To completely disable these expansions, you can use -G flag.
489 490
490 491 Options:
491 492
492 493 -n
493 494 __name__ is NOT set to '__main__', but to the running file's name
494 495 without extension (as python does under import). This allows running
495 496 scripts and reloading the definitions in them without calling code
496 497 protected by an ``if __name__ == "__main__"`` clause.
497 498
498 499 -i
499 500 run the file in IPython's namespace instead of an empty one. This
500 501 is useful if you are experimenting with code written in a text editor
501 502 which depends on variables defined interactively.
502 503
503 504 -e
504 505 ignore sys.exit() calls or SystemExit exceptions in the script
505 506 being run. This is particularly useful if IPython is being used to
506 507 run unittests, which always exit with a sys.exit() call. In such
507 508 cases you are interested in the output of the test results, not in
508 509 seeing a traceback of the unittest module.
509 510
510 511 -t
511 512 print timing information at the end of the run. IPython will give
512 513 you an estimated CPU time consumption for your script, which under
513 514 Unix uses the resource module to avoid the wraparound problems of
514 515 time.clock(). Under Unix, an estimate of time spent on system tasks
515 516 is also given (for Windows platforms this is reported as 0.0).
516 517
517 518 If -t is given, an additional ``-N<N>`` option can be given, where <N>
518 519 must be an integer indicating how many times you want the script to
519 520 run. The final timing report will include total and per run results.
520 521
521 522 For example (testing the script uniq_stable.py)::
522 523
523 524 In [1]: run -t uniq_stable
524 525
525 526 IPython CPU timings (estimated):
526 527 User : 0.19597 s.
527 528 System: 0.0 s.
528 529
529 530 In [2]: run -t -N5 uniq_stable
530 531
531 532 IPython CPU timings (estimated):
532 533 Total runs performed: 5
533 534 Times : Total Per run
534 535 User : 0.910862 s, 0.1821724 s.
535 536 System: 0.0 s, 0.0 s.
536 537
537 538 -d
538 539 run your program under the control of pdb, the Python debugger.
539 540 This allows you to execute your program step by step, watch variables,
540 541 etc. Internally, what IPython does is similar to calling::
541 542
542 543 pdb.run('execfile("YOURFILENAME")')
543 544
544 545 with a breakpoint set on line 1 of your file. You can change the line
545 546 number for this automatic breakpoint to be <N> by using the -bN option
546 547 (where N must be an integer). For example::
547 548
548 549 %run -d -b40 myscript
549 550
550 551 will set the first breakpoint at line 40 in myscript.py. Note that
551 552 the first breakpoint must be set on a line which actually does
552 553 something (not a comment or docstring) for it to stop execution.
553 554
554 555 Or you can specify a breakpoint in a different file::
555 556
556 557 %run -d -b myotherfile.py:20 myscript
557 558
558 559 When the pdb debugger starts, you will see a (Pdb) prompt. You must
559 560 first enter 'c' (without quotes) to start execution up to the first
560 561 breakpoint.
561 562
562 563 Entering 'help' gives information about the use of the debugger. You
563 564 can easily see pdb's full documentation with "import pdb;pdb.help()"
564 565 at a prompt.
565 566
566 567 -p
567 568 run program under the control of the Python profiler module (which
568 569 prints a detailed report of execution times, function calls, etc).
569 570
570 571 You can pass other options after -p which affect the behavior of the
571 572 profiler itself. See the docs for %prun for details.
572 573
573 574 In this mode, the program's variables do NOT propagate back to the
574 575 IPython interactive namespace (because they remain in the namespace
575 576 where the profiler executes them).
576 577
577 578 Internally this triggers a call to %prun, see its documentation for
578 579 details on the options available specifically for profiling.
579 580
580 581 There is one special usage for which the text above doesn't apply:
581 582 if the filename ends with .ipy[nb], the file is run as ipython script,
582 583 just as if the commands were written on IPython prompt.
583 584
584 585 -m
585 586 specify module name to load instead of script path. Similar to
586 587 the -m option for the python interpreter. Use this option last if you
587 588 want to combine with other %run options. Unlike the python interpreter
588 589 only source modules are allowed no .pyc or .pyo files.
589 590 For example::
590 591
591 592 %run -m example
592 593
593 594 will run the example module.
594 595
595 596 -G
596 597 disable shell-like glob expansion of arguments.
597 598
598 599 """
599 600
600 601 # get arguments and set sys.argv for program to be run.
601 602 opts, arg_lst = self.parse_options(parameter_s,
602 603 'nidtN:b:pD:l:rs:T:em:G',
603 604 mode='list', list_all=1)
604 605 if "m" in opts:
605 606 modulename = opts["m"][0]
606 607 modpath = find_mod(modulename)
607 608 if modpath is None:
608 609 warn('%r is not a valid modulename on sys.path'%modulename)
609 610 return
610 611 arg_lst = [modpath] + arg_lst
611 612 try:
612 613 filename = file_finder(arg_lst[0])
613 614 except IndexError:
614 615 warn('you must provide at least a filename.')
615 616 print('\n%run:\n', oinspect.getdoc(self.run))
616 617 return
617 618 except IOError as e:
618 619 try:
619 620 msg = str(e)
620 621 except UnicodeError:
621 622 msg = e.message
622 623 error(msg)
623 624 return
624 625
625 626 if filename.lower().endswith(('.ipy', '.ipynb')):
626 627 with preserve_keys(self.shell.user_ns, '__file__'):
627 628 self.shell.user_ns['__file__'] = filename
628 629 self.shell.safe_execfile_ipy(filename)
629 630 return
630 631
631 632 # Control the response to exit() calls made by the script being run
632 633 exit_ignore = 'e' in opts
633 634
634 635 # Make sure that the running script gets a proper sys.argv as if it
635 636 # were run from a system shell.
636 637 save_argv = sys.argv # save it for later restoring
637 638
638 639 if 'G' in opts:
639 640 args = arg_lst[1:]
640 641 else:
641 642 # tilde and glob expansion
642 643 args = shellglob(map(os.path.expanduser, arg_lst[1:]))
643 644
644 645 sys.argv = [filename] + args # put in the proper filename
645 646 # protect sys.argv from potential unicode strings on Python 2:
646 647 if not py3compat.PY3:
647 648 sys.argv = [ py3compat.cast_bytes(a) for a in sys.argv ]
648 649
649 650 if 'i' in opts:
650 651 # Run in user's interactive namespace
651 652 prog_ns = self.shell.user_ns
652 653 __name__save = self.shell.user_ns['__name__']
653 654 prog_ns['__name__'] = '__main__'
654 655 main_mod = self.shell.user_module
655 656
656 657 # Since '%run foo' emulates 'python foo.py' at the cmd line, we must
657 658 # set the __file__ global in the script's namespace
658 659 # TK: Is this necessary in interactive mode?
659 660 prog_ns['__file__'] = filename
660 661 else:
661 662 # Run in a fresh, empty namespace
662 663 if 'n' in opts:
663 664 name = os.path.splitext(os.path.basename(filename))[0]
664 665 else:
665 666 name = '__main__'
666 667
667 668 # The shell MUST hold a reference to prog_ns so after %run
668 669 # exits, the python deletion mechanism doesn't zero it out
669 670 # (leaving dangling references). See interactiveshell for details
670 671 main_mod = self.shell.new_main_mod(filename, name)
671 672 prog_ns = main_mod.__dict__
672 673
673 674 # pickle fix. See interactiveshell for an explanation. But we need to
674 675 # make sure that, if we overwrite __main__, we replace it at the end
675 676 main_mod_name = prog_ns['__name__']
676 677
677 678 if main_mod_name == '__main__':
678 679 restore_main = sys.modules['__main__']
679 680 else:
680 681 restore_main = False
681 682
682 683 # This needs to be undone at the end to prevent holding references to
683 684 # every single object ever created.
684 685 sys.modules[main_mod_name] = main_mod
685 686
686 687 if 'p' in opts or 'd' in opts:
687 688 if 'm' in opts:
688 689 code = 'run_module(modulename, prog_ns)'
689 690 code_ns = {
690 691 'run_module': self.shell.safe_run_module,
691 692 'prog_ns': prog_ns,
692 693 'modulename': modulename,
693 694 }
694 695 else:
695 696 if 'd' in opts:
696 697 # allow exceptions to raise in debug mode
697 698 code = 'execfile(filename, prog_ns, raise_exceptions=True)'
698 699 else:
699 700 code = 'execfile(filename, prog_ns)'
700 701 code_ns = {
701 702 'execfile': self.shell.safe_execfile,
702 703 'prog_ns': prog_ns,
703 704 'filename': get_py_filename(filename),
704 705 }
705 706
706 707 try:
707 708 stats = None
708 709 with self.shell.readline_no_record:
709 710 if 'p' in opts:
710 711 stats = self._run_with_profiler(code, opts, code_ns)
711 712 else:
712 713 if 'd' in opts:
713 714 bp_file, bp_line = parse_breakpoint(
714 715 opts.get('b', ['1'])[0], filename)
715 716 self._run_with_debugger(
716 717 code, code_ns, filename, bp_line, bp_file)
717 718 else:
718 719 if 'm' in opts:
719 720 def run():
720 721 self.shell.safe_run_module(modulename, prog_ns)
721 722 else:
722 723 if runner is None:
723 724 runner = self.default_runner
724 725 if runner is None:
725 726 runner = self.shell.safe_execfile
726 727
727 728 def run():
728 729 runner(filename, prog_ns, prog_ns,
729 730 exit_ignore=exit_ignore)
730 731
731 732 if 't' in opts:
732 733 # timed execution
733 734 try:
734 735 nruns = int(opts['N'][0])
735 736 if nruns < 1:
736 737 error('Number of runs must be >=1')
737 738 return
738 739 except (KeyError):
739 740 nruns = 1
740 741 self._run_with_timing(run, nruns)
741 742 else:
742 743 # regular execution
743 744 run()
744 745
745 746 if 'i' in opts:
746 747 self.shell.user_ns['__name__'] = __name__save
747 748 else:
748 749 # update IPython interactive namespace
749 750
750 751 # Some forms of read errors on the file may mean the
751 752 # __name__ key was never set; using pop we don't have to
752 753 # worry about a possible KeyError.
753 754 prog_ns.pop('__name__', None)
754 755
755 756 with preserve_keys(self.shell.user_ns, '__file__'):
756 757 self.shell.user_ns.update(prog_ns)
757 758 finally:
758 759 # It's a bit of a mystery why, but __builtins__ can change from
759 760 # being a module to becoming a dict missing some key data after
760 761 # %run. As best I can see, this is NOT something IPython is doing
761 762 # at all, and similar problems have been reported before:
762 763 # http://coding.derkeiler.com/Archive/Python/comp.lang.python/2004-10/0188.html
763 764 # Since this seems to be done by the interpreter itself, the best
764 765 # we can do is to at least restore __builtins__ for the user on
765 766 # exit.
766 767 self.shell.user_ns['__builtins__'] = builtin_mod
767 768
768 769 # Ensure key global structures are restored
769 770 sys.argv = save_argv
770 771 if restore_main:
771 772 sys.modules['__main__'] = restore_main
772 773 else:
773 774 # Remove from sys.modules the reference to main_mod we'd
774 775 # added. Otherwise it will trap references to objects
775 776 # contained therein.
776 777 del sys.modules[main_mod_name]
777 778
778 779 return stats
779 780
780 781 def _run_with_debugger(self, code, code_ns, filename=None,
781 782 bp_line=None, bp_file=None):
782 783 """
783 784 Run `code` in debugger with a break point.
784 785
785 786 Parameters
786 787 ----------
787 788 code : str
788 789 Code to execute.
789 790 code_ns : dict
790 791 A namespace in which `code` is executed.
791 792 filename : str
792 793 `code` is ran as if it is in `filename`.
793 794 bp_line : int, optional
794 795 Line number of the break point.
795 796 bp_file : str, optional
796 797 Path to the file in which break point is specified.
797 798 `filename` is used if not given.
798 799
799 800 Raises
800 801 ------
801 802 UsageError
802 803 If the break point given by `bp_line` is not valid.
803 804
804 805 """
805 806 deb = debugger.Pdb(self.shell.colors)
806 807 # reset Breakpoint state, which is moronically kept
807 808 # in a class
808 809 bdb.Breakpoint.next = 1
809 810 bdb.Breakpoint.bplist = {}
810 811 bdb.Breakpoint.bpbynumber = [None]
811 812 if bp_line is not None:
812 813 # Set an initial breakpoint to stop execution
813 814 maxtries = 10
814 815 bp_file = bp_file or filename
815 816 checkline = deb.checkline(bp_file, bp_line)
816 817 if not checkline:
817 818 for bp in range(bp_line + 1, bp_line + maxtries + 1):
818 819 if deb.checkline(bp_file, bp):
819 820 break
820 821 else:
821 822 msg = ("\nI failed to find a valid line to set "
822 823 "a breakpoint\n"
823 824 "after trying up to line: %s.\n"
824 825 "Please set a valid breakpoint manually "
825 826 "with the -b option." % bp)
826 827 raise UsageError(msg)
827 828 # if we find a good linenumber, set the breakpoint
828 829 deb.do_break('%s:%s' % (bp_file, bp_line))
829 830
830 831 if filename:
831 832 # Mimic Pdb._runscript(...)
832 833 deb._wait_for_mainpyfile = True
833 834 deb.mainpyfile = deb.canonic(filename)
834 835
835 836 # Start file run
836 837 print("NOTE: Enter 'c' at the %s prompt to continue execution." % deb.prompt)
837 838 try:
838 839 if filename:
839 840 # save filename so it can be used by methods on the deb object
840 841 deb._exec_filename = filename
841 842 while True:
842 843 try:
843 844 deb.run(code, code_ns)
844 845 except Restart:
845 846 print("Restarting")
846 847 if filename:
847 848 deb._wait_for_mainpyfile = True
848 849 deb.mainpyfile = deb.canonic(filename)
849 850 continue
850 851 else:
851 852 break
852 853
853 854
854 855 except:
855 856 etype, value, tb = sys.exc_info()
856 857 # Skip three frames in the traceback: the %run one,
857 858 # one inside bdb.py, and the command-line typed by the
858 859 # user (run by exec in pdb itself).
859 860 self.shell.InteractiveTB(etype, value, tb, tb_offset=3)
860 861
861 862 @staticmethod
862 863 def _run_with_timing(run, nruns):
863 864 """
864 865 Run function `run` and print timing information.
865 866
866 867 Parameters
867 868 ----------
868 869 run : callable
869 870 Any callable object which takes no argument.
870 871 nruns : int
871 872 Number of times to execute `run`.
872 873
873 874 """
874 875 twall0 = time.time()
875 876 if nruns == 1:
876 877 t0 = clock2()
877 878 run()
878 879 t1 = clock2()
879 880 t_usr = t1[0] - t0[0]
880 881 t_sys = t1[1] - t0[1]
881 882 print("\nIPython CPU timings (estimated):")
882 883 print(" User : %10.2f s." % t_usr)
883 884 print(" System : %10.2f s." % t_sys)
884 885 else:
885 886 runs = range(nruns)
886 887 t0 = clock2()
887 888 for nr in runs:
888 889 run()
889 890 t1 = clock2()
890 891 t_usr = t1[0] - t0[0]
891 892 t_sys = t1[1] - t0[1]
892 893 print("\nIPython CPU timings (estimated):")
893 894 print("Total runs performed:", nruns)
894 895 print(" Times : %10s %10s" % ('Total', 'Per run'))
895 896 print(" User : %10.2f s, %10.2f s." % (t_usr, t_usr / nruns))
896 897 print(" System : %10.2f s, %10.2f s." % (t_sys, t_sys / nruns))
897 898 twall1 = time.time()
898 899 print("Wall time: %10.2f s." % (twall1 - twall0))
899 900
900 901 @skip_doctest
901 902 @line_cell_magic
902 903 def timeit(self, line='', cell=None):
903 904 """Time execution of a Python statement or expression
904 905
905 906 Usage, in line mode:
906 907 %timeit [-n<N> -r<R> [-t|-c] -q -p<P> -o] statement
907 908 or in cell mode:
908 909 %%timeit [-n<N> -r<R> [-t|-c] -q -p<P> -o] setup_code
909 910 code
910 911 code...
911 912
912 913 Time execution of a Python statement or expression using the timeit
913 914 module. This function can be used both as a line and cell magic:
914 915
915 916 - In line mode you can time a single-line statement (though multiple
916 917 ones can be chained with using semicolons).
917 918
918 919 - In cell mode, the statement in the first line is used as setup code
919 920 (executed but not timed) and the body of the cell is timed. The cell
920 921 body has access to any variables created in the setup code.
921 922
922 923 Options:
923 924 -n<N>: execute the given statement <N> times in a loop. If this value
924 925 is not given, a fitting value is chosen.
925 926
926 927 -r<R>: repeat the loop iteration <R> times and take the best result.
927 928 Default: 3
928 929
929 930 -t: use time.time to measure the time, which is the default on Unix.
930 931 This function measures wall time.
931 932
932 933 -c: use time.clock to measure the time, which is the default on
933 934 Windows and measures wall time. On Unix, resource.getrusage is used
934 935 instead and returns the CPU user time.
935 936
936 937 -p<P>: use a precision of <P> digits to display the timing result.
937 938 Default: 3
938 939
939 940 -q: Quiet, do not print result.
940 941
941 942 -o: return a TimeitResult that can be stored in a variable to inspect
942 943 the result in more details.
943 944
944 945
945 946 Examples
946 947 --------
947 948 ::
948 949
949 950 In [1]: %timeit pass
950 951 10000000 loops, best of 3: 53.3 ns per loop
951 952
952 953 In [2]: u = None
953 954
954 955 In [3]: %timeit u is None
955 956 10000000 loops, best of 3: 184 ns per loop
956 957
957 958 In [4]: %timeit -r 4 u == None
958 959 1000000 loops, best of 4: 242 ns per loop
959 960
960 961 In [5]: import time
961 962
962 963 In [6]: %timeit -n1 time.sleep(2)
963 964 1 loop, best of 3: 2 s per loop
964 965
965 966
966 967 The times reported by %timeit will be slightly higher than those
967 968 reported by the timeit.py script when variables are accessed. This is
968 969 due to the fact that %timeit executes the statement in the namespace
969 970 of the shell, compared with timeit.py, which uses a single setup
970 971 statement to import function or create variables. Generally, the bias
971 972 does not matter as long as results from timeit.py are not mixed with
972 973 those from %timeit."""
973 974
974 975 opts, stmt = self.parse_options(line,'n:r:tcp:qo',
975 976 posix=False, strict=False)
976 977 if stmt == "" and cell is None:
977 978 return
978 979
979 980 timefunc = timeit.default_timer
980 981 number = int(getattr(opts, "n", 0))
981 982 repeat = int(getattr(opts, "r", timeit.default_repeat))
982 983 precision = int(getattr(opts, "p", 3))
983 984 quiet = 'q' in opts
984 985 return_result = 'o' in opts
985 986 if hasattr(opts, "t"):
986 987 timefunc = time.time
987 988 if hasattr(opts, "c"):
988 989 timefunc = clock
989 990
990 991 timer = Timer(timer=timefunc)
991 992 # this code has tight coupling to the inner workings of timeit.Timer,
992 993 # but is there a better way to achieve that the code stmt has access
993 994 # to the shell namespace?
994 995 transform = self.shell.input_splitter.transform_cell
995 996
996 997 if cell is None:
997 998 # called as line magic
998 999 ast_setup = self.shell.compile.ast_parse("pass")
999 1000 ast_stmt = self.shell.compile.ast_parse(transform(stmt))
1000 1001 else:
1001 1002 ast_setup = self.shell.compile.ast_parse(transform(stmt))
1002 1003 ast_stmt = self.shell.compile.ast_parse(transform(cell))
1003 1004
1004 1005 ast_setup = self.shell.transform_ast(ast_setup)
1005 1006 ast_stmt = self.shell.transform_ast(ast_stmt)
1006 1007
1007 1008 # This codestring is taken from timeit.template - we fill it in as an
1008 1009 # AST, so that we can apply our AST transformations to the user code
1009 1010 # without affecting the timing code.
1010 1011 timeit_ast_template = ast.parse('def inner(_it, _timer):\n'
1011 1012 ' setup\n'
1012 1013 ' _t0 = _timer()\n'
1013 1014 ' for _i in _it:\n'
1014 1015 ' stmt\n'
1015 1016 ' _t1 = _timer()\n'
1016 1017 ' return _t1 - _t0\n')
1017 1018
1018 1019 timeit_ast = TimeitTemplateFiller(ast_setup, ast_stmt).visit(timeit_ast_template)
1019 1020 timeit_ast = ast.fix_missing_locations(timeit_ast)
1020 1021
1021 1022 # Track compilation time so it can be reported if too long
1022 1023 # Minimum time above which compilation time will be reported
1023 1024 tc_min = 0.1
1024 1025
1025 1026 t0 = clock()
1026 1027 code = self.shell.compile(timeit_ast, "<magic-timeit>", "exec")
1027 1028 tc = clock()-t0
1028 1029
1029 1030 ns = {}
1030 1031 exec(code, self.shell.user_ns, ns)
1031 1032 timer.inner = ns["inner"]
1032 1033
1033 1034 # This is used to check if there is a huge difference between the
1034 1035 # best and worst timings.
1035 1036 # Issue: https://github.com/ipython/ipython/issues/6471
1036 1037 worst_tuning = 0
1037 1038 if number == 0:
1038 1039 # determine number so that 0.2 <= total time < 2.0
1039 1040 number = 1
1040 1041 for _ in range(1, 10):
1041 1042 time_number = timer.timeit(number)
1042 1043 worst_tuning = max(worst_tuning, time_number / number)
1043 1044 if time_number >= 0.2:
1044 1045 break
1045 1046 number *= 10
1046 1047 all_runs = timer.repeat(repeat, number)
1047 1048 best = min(all_runs) / number
1048 1049
1049 1050 worst = max(all_runs) / number
1050 1051 if worst_tuning:
1051 1052 worst = max(worst, worst_tuning)
1052 1053
1053 1054 if not quiet :
1054 1055 # Check best timing is greater than zero to avoid a
1055 1056 # ZeroDivisionError.
1056 1057 # In cases where the slowest timing is lesser than a micosecond
1057 1058 # we assume that it does not really matter if the fastest
1058 1059 # timing is 4 times faster than the slowest timing or not.
1059 1060 if worst > 4 * best and best > 0 and worst > 1e-6:
1060 1061 print("The slowest run took %0.2f times longer than the "
1061 1062 "fastest. This could mean that an intermediate result "
1062 1063 "is being cached." % (worst / best))
1063 1064 if number == 1: # No s at "loops" if only one loop
1064 1065 print(u"%d loop, best of %d: %s per loop" % (number, repeat,
1065 1066 _format_time(best, precision)))
1066 1067 else:
1067 1068 print(u"%d loops, best of %d: %s per loop" % (number, repeat,
1068 1069 _format_time(best, precision)))
1069 1070 if tc > tc_min:
1070 1071 print("Compiler time: %.2f s" % tc)
1071 1072 if return_result:
1072 1073 return TimeitResult(number, repeat, best, worst, all_runs, tc, precision)
1073 1074
1074 1075 @skip_doctest
1075 1076 @needs_local_scope
1076 1077 @line_cell_magic
1077 1078 def time(self,line='', cell=None, local_ns=None):
1078 1079 """Time execution of a Python statement or expression.
1079 1080
1080 1081 The CPU and wall clock times are printed, and the value of the
1081 1082 expression (if any) is returned. Note that under Win32, system time
1082 1083 is always reported as 0, since it can not be measured.
1083 1084
1084 1085 This function can be used both as a line and cell magic:
1085 1086
1086 1087 - In line mode you can time a single-line statement (though multiple
1087 1088 ones can be chained with using semicolons).
1088 1089
1089 1090 - In cell mode, you can time the cell body (a directly
1090 1091 following statement raises an error).
1091 1092
1092 1093 This function provides very basic timing functionality. Use the timeit
1093 1094 magic for more control over the measurement.
1094 1095
1095 1096 Examples
1096 1097 --------
1097 1098 ::
1098 1099
1099 1100 In [1]: %time 2**128
1100 1101 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1101 1102 Wall time: 0.00
1102 1103 Out[1]: 340282366920938463463374607431768211456L
1103 1104
1104 1105 In [2]: n = 1000000
1105 1106
1106 1107 In [3]: %time sum(range(n))
1107 1108 CPU times: user 1.20 s, sys: 0.05 s, total: 1.25 s
1108 1109 Wall time: 1.37
1109 1110 Out[3]: 499999500000L
1110 1111
1111 1112 In [4]: %time print 'hello world'
1112 1113 hello world
1113 1114 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1114 1115 Wall time: 0.00
1115 1116
1116 1117 Note that the time needed by Python to compile the given expression
1117 1118 will be reported if it is more than 0.1s. In this example, the
1118 1119 actual exponentiation is done by Python at compilation time, so while
1119 1120 the expression can take a noticeable amount of time to compute, that
1120 1121 time is purely due to the compilation:
1121 1122
1122 1123 In [5]: %time 3**9999;
1123 1124 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1124 1125 Wall time: 0.00 s
1125 1126
1126 1127 In [6]: %time 3**999999;
1127 1128 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1128 1129 Wall time: 0.00 s
1129 1130 Compiler : 0.78 s
1130 1131 """
1131 1132
1132 1133 # fail immediately if the given expression can't be compiled
1133 1134
1134 1135 if line and cell:
1135 1136 raise UsageError("Can't use statement directly after '%%time'!")
1136 1137
1137 1138 if cell:
1138 1139 expr = self.shell.input_transformer_manager.transform_cell(cell)
1139 1140 else:
1140 1141 expr = self.shell.input_transformer_manager.transform_cell(line)
1141 1142
1142 1143 # Minimum time above which parse time will be reported
1143 1144 tp_min = 0.1
1144 1145
1145 1146 t0 = clock()
1146 1147 expr_ast = self.shell.compile.ast_parse(expr)
1147 1148 tp = clock()-t0
1148 1149
1149 1150 # Apply AST transformations
1150 1151 expr_ast = self.shell.transform_ast(expr_ast)
1151 1152
1152 1153 # Minimum time above which compilation time will be reported
1153 1154 tc_min = 0.1
1154 1155
1155 1156 if len(expr_ast.body)==1 and isinstance(expr_ast.body[0], ast.Expr):
1156 1157 mode = 'eval'
1157 1158 source = '<timed eval>'
1158 1159 expr_ast = ast.Expression(expr_ast.body[0].value)
1159 1160 else:
1160 1161 mode = 'exec'
1161 1162 source = '<timed exec>'
1162 1163 t0 = clock()
1163 1164 code = self.shell.compile(expr_ast, source, mode)
1164 1165 tc = clock()-t0
1165 1166
1166 1167 # skew measurement as little as possible
1167 1168 glob = self.shell.user_ns
1168 1169 wtime = time.time
1169 1170 # time execution
1170 1171 wall_st = wtime()
1171 1172 if mode=='eval':
1172 1173 st = clock2()
1173 1174 out = eval(code, glob, local_ns)
1174 1175 end = clock2()
1175 1176 else:
1176 1177 st = clock2()
1177 1178 exec(code, glob, local_ns)
1178 1179 end = clock2()
1179 1180 out = None
1180 1181 wall_end = wtime()
1181 1182 # Compute actual times and report
1182 1183 wall_time = wall_end-wall_st
1183 1184 cpu_user = end[0]-st[0]
1184 1185 cpu_sys = end[1]-st[1]
1185 1186 cpu_tot = cpu_user+cpu_sys
1186 1187 # On windows cpu_sys is always zero, so no new information to the next print
1187 1188 if sys.platform != 'win32':
1188 1189 print("CPU times: user %s, sys: %s, total: %s" % \
1189 1190 (_format_time(cpu_user),_format_time(cpu_sys),_format_time(cpu_tot)))
1190 1191 print("Wall time: %s" % _format_time(wall_time))
1191 1192 if tc > tc_min:
1192 1193 print("Compiler : %s" % _format_time(tc))
1193 1194 if tp > tp_min:
1194 1195 print("Parser : %s" % _format_time(tp))
1195 1196 return out
1196 1197
1197 1198 @skip_doctest
1198 1199 @line_magic
1199 1200 def macro(self, parameter_s=''):
1200 1201 """Define a macro for future re-execution. It accepts ranges of history,
1201 1202 filenames or string objects.
1202 1203
1203 1204 Usage:\\
1204 1205 %macro [options] name n1-n2 n3-n4 ... n5 .. n6 ...
1205 1206
1206 1207 Options:
1207 1208
1208 1209 -r: use 'raw' input. By default, the 'processed' history is used,
1209 1210 so that magics are loaded in their transformed version to valid
1210 1211 Python. If this option is given, the raw input as typed at the
1211 1212 command line is used instead.
1212 1213
1213 1214 -q: quiet macro definition. By default, a tag line is printed
1214 1215 to indicate the macro has been created, and then the contents of
1215 1216 the macro are printed. If this option is given, then no printout
1216 1217 is produced once the macro is created.
1217 1218
1218 1219 This will define a global variable called `name` which is a string
1219 1220 made of joining the slices and lines you specify (n1,n2,... numbers
1220 1221 above) from your input history into a single string. This variable
1221 1222 acts like an automatic function which re-executes those lines as if
1222 1223 you had typed them. You just type 'name' at the prompt and the code
1223 1224 executes.
1224 1225
1225 1226 The syntax for indicating input ranges is described in %history.
1226 1227
1227 1228 Note: as a 'hidden' feature, you can also use traditional python slice
1228 1229 notation, where N:M means numbers N through M-1.
1229 1230
1230 1231 For example, if your history contains (print using %hist -n )::
1231 1232
1232 1233 44: x=1
1233 1234 45: y=3
1234 1235 46: z=x+y
1235 1236 47: print x
1236 1237 48: a=5
1237 1238 49: print 'x',x,'y',y
1238 1239
1239 1240 you can create a macro with lines 44 through 47 (included) and line 49
1240 1241 called my_macro with::
1241 1242
1242 1243 In [55]: %macro my_macro 44-47 49
1243 1244
1244 1245 Now, typing `my_macro` (without quotes) will re-execute all this code
1245 1246 in one pass.
1246 1247
1247 1248 You don't need to give the line-numbers in order, and any given line
1248 1249 number can appear multiple times. You can assemble macros with any
1249 1250 lines from your input history in any order.
1250 1251
1251 1252 The macro is a simple object which holds its value in an attribute,
1252 1253 but IPython's display system checks for macros and executes them as
1253 1254 code instead of printing them when you type their name.
1254 1255
1255 1256 You can view a macro's contents by explicitly printing it with::
1256 1257
1257 1258 print macro_name
1258 1259
1259 1260 """
1260 1261 opts,args = self.parse_options(parameter_s,'rq',mode='list')
1261 1262 if not args: # List existing macros
1262 1263 return sorted(k for k,v in iteritems(self.shell.user_ns) if\
1263 1264 isinstance(v, Macro))
1264 1265 if len(args) == 1:
1265 1266 raise UsageError(
1266 1267 "%macro insufficient args; usage '%macro name n1-n2 n3-4...")
1267 1268 name, codefrom = args[0], " ".join(args[1:])
1268 1269
1269 1270 #print 'rng',ranges # dbg
1270 1271 try:
1271 1272 lines = self.shell.find_user_code(codefrom, 'r' in opts)
1272 1273 except (ValueError, TypeError) as e:
1273 1274 print(e.args[0])
1274 1275 return
1275 1276 macro = Macro(lines)
1276 1277 self.shell.define_macro(name, macro)
1277 1278 if not ( 'q' in opts) :
1278 1279 print('Macro `%s` created. To execute, type its name (without quotes).' % name)
1279 1280 print('=== Macro contents: ===')
1280 1281 print(macro, end=' ')
1281 1282
1282 1283 @magic_arguments.magic_arguments()
1283 1284 @magic_arguments.argument('output', type=str, default='', nargs='?',
1284 1285 help="""The name of the variable in which to store output.
1285 1286 This is a utils.io.CapturedIO object with stdout/err attributes
1286 1287 for the text of the captured output.
1287 1288
1288 1289 CapturedOutput also has a show() method for displaying the output,
1289 1290 and __call__ as well, so you can use that to quickly display the
1290 1291 output.
1291 1292
1292 1293 If unspecified, captured output is discarded.
1293 1294 """
1294 1295 )
1295 1296 @magic_arguments.argument('--no-stderr', action="store_true",
1296 1297 help="""Don't capture stderr."""
1297 1298 )
1298 1299 @magic_arguments.argument('--no-stdout', action="store_true",
1299 1300 help="""Don't capture stdout."""
1300 1301 )
1301 1302 @magic_arguments.argument('--no-display', action="store_true",
1302 1303 help="""Don't capture IPython's rich display."""
1303 1304 )
1304 1305 @cell_magic
1305 1306 def capture(self, line, cell):
1306 1307 """run the cell, capturing stdout, stderr, and IPython's rich display() calls."""
1307 1308 args = magic_arguments.parse_argstring(self.capture, line)
1308 1309 out = not args.no_stdout
1309 1310 err = not args.no_stderr
1310 1311 disp = not args.no_display
1311 1312 with capture_output(out, err, disp) as io:
1312 1313 self.shell.run_cell(cell)
1313 1314 if args.output:
1314 1315 self.shell.user_ns[args.output] = io
1315 1316
1316 1317 def parse_breakpoint(text, current_file):
1317 1318 '''Returns (file, line) for file:line and (current_file, line) for line'''
1318 1319 colon = text.find(':')
1319 1320 if colon == -1:
1320 1321 return current_file, int(text)
1321 1322 else:
1322 1323 return text[:colon], int(text[colon+1:])
1323 1324
1324 1325 def _format_time(timespan, precision=3):
1325 1326 """Formats the timespan in a human readable form"""
1326 1327 import math
1327 1328
1328 1329 if timespan >= 60.0:
1329 1330 # we have more than a minute, format that in a human readable form
1330 1331 # Idea from http://snipplr.com/view/5713/
1331 1332 parts = [("d", 60*60*24),("h", 60*60),("min", 60), ("s", 1)]
1332 1333 time = []
1333 1334 leftover = timespan
1334 1335 for suffix, length in parts:
1335 1336 value = int(leftover / length)
1336 1337 if value > 0:
1337 1338 leftover = leftover % length
1338 1339 time.append(u'%s%s' % (str(value), suffix))
1339 1340 if leftover < 1:
1340 1341 break
1341 1342 return " ".join(time)
1342 1343
1343 1344
1344 1345 # Unfortunately the unicode 'micro' symbol can cause problems in
1345 1346 # certain terminals.
1346 1347 # See bug: https://bugs.launchpad.net/ipython/+bug/348466
1347 1348 # Try to prevent crashes by being more secure than it needs to
1348 1349 # E.g. eclipse is able to print a Β΅, but has no sys.stdout.encoding set.
1349 1350 units = [u"s", u"ms",u'us',"ns"] # the save value
1350 1351 if hasattr(sys.stdout, 'encoding') and sys.stdout.encoding:
1351 1352 try:
1352 1353 u'\xb5'.encode(sys.stdout.encoding)
1353 1354 units = [u"s", u"ms",u'\xb5s',"ns"]
1354 1355 except:
1355 1356 pass
1356 1357 scaling = [1, 1e3, 1e6, 1e9]
1357 1358
1358 1359 if timespan > 0.0:
1359 1360 order = min(-int(math.floor(math.log10(timespan)) // 3), 3)
1360 1361 else:
1361 1362 order = 3
1362 1363 return u"%.*g %s" % (precision, timespan * scaling[order], units[order])
@@ -1,184 +1,184 b''
1 1 """Implementation of magic functions for IPython's own logging.
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 os
17 17 import sys
18 18
19 19 # Our own packages
20 20 from IPython.core.magic import Magics, magics_class, line_magic
21 from IPython.utils.warn import warn
21 from warnings import warn
22 22 from IPython.utils.py3compat import str_to_unicode
23 23
24 24 #-----------------------------------------------------------------------------
25 25 # Magic implementation classes
26 26 #-----------------------------------------------------------------------------
27 27
28 28 @magics_class
29 29 class LoggingMagics(Magics):
30 30 """Magics related to all logging machinery."""
31 31
32 32 @line_magic
33 33 def logstart(self, parameter_s=''):
34 34 """Start logging anywhere in a session.
35 35
36 36 %logstart [-o|-r|-t] [log_name [log_mode]]
37 37
38 38 If no name is given, it defaults to a file named 'ipython_log.py' in your
39 39 current directory, in 'rotate' mode (see below).
40 40
41 41 '%logstart name' saves to file 'name' in 'backup' mode. It saves your
42 42 history up to that point and then continues logging.
43 43
44 44 %logstart takes a second optional parameter: logging mode. This can be one
45 45 of (note that the modes are given unquoted):
46 46
47 47 append
48 48 Keep logging at the end of any existing file.
49 49
50 50 backup
51 51 Rename any existing file to name~ and start name.
52 52
53 53 global
54 54 Append to a single logfile in your home directory.
55 55
56 56 over
57 57 Overwrite any existing log.
58 58
59 59 rotate
60 60 Create rotating logs: name.1~, name.2~, etc.
61 61
62 62 Options:
63 63
64 64 -o
65 65 log also IPython's output. In this mode, all commands which
66 66 generate an Out[NN] prompt are recorded to the logfile, right after
67 67 their corresponding input line. The output lines are always
68 68 prepended with a '#[Out]# ' marker, so that the log remains valid
69 69 Python code.
70 70
71 71 Since this marker is always the same, filtering only the output from
72 72 a log is very easy, using for example a simple awk call::
73 73
74 74 awk -F'#\\[Out\\]# ' '{if($2) {print $2}}' ipython_log.py
75 75
76 76 -r
77 77 log 'raw' input. Normally, IPython's logs contain the processed
78 78 input, so that user lines are logged in their final form, converted
79 79 into valid Python. For example, %Exit is logged as
80 80 _ip.magic("Exit"). If the -r flag is given, all input is logged
81 81 exactly as typed, with no transformations applied.
82 82
83 83 -t
84 84 put timestamps before each input line logged (these are put in
85 85 comments).
86 86 """
87 87
88 88 opts,par = self.parse_options(parameter_s,'ort')
89 89 log_output = 'o' in opts
90 90 log_raw_input = 'r' in opts
91 91 timestamp = 't' in opts
92 92
93 93 logger = self.shell.logger
94 94
95 95 # if no args are given, the defaults set in the logger constructor by
96 96 # ipython remain valid
97 97 if par:
98 98 try:
99 99 logfname,logmode = par.split()
100 100 except:
101 101 logfname = par
102 102 logmode = 'backup'
103 103 else:
104 104 logfname = logger.logfname
105 105 logmode = logger.logmode
106 106 # put logfname into rc struct as if it had been called on the command
107 107 # line, so it ends up saved in the log header Save it in case we need
108 108 # to restore it...
109 109 old_logfile = self.shell.logfile
110 110 if logfname:
111 111 logfname = os.path.expanduser(logfname)
112 112 self.shell.logfile = logfname
113 113
114 114 loghead = u'# IPython log file\n\n'
115 115 try:
116 116 logger.logstart(logfname, loghead, logmode, log_output, timestamp,
117 117 log_raw_input)
118 118 except:
119 119 self.shell.logfile = old_logfile
120 120 warn("Couldn't start log: %s" % sys.exc_info()[1])
121 121 else:
122 122 # log input history up to this point, optionally interleaving
123 123 # output if requested
124 124
125 125 if timestamp:
126 126 # disable timestamping for the previous history, since we've
127 127 # lost those already (no time machine here).
128 128 logger.timestamp = False
129 129
130 130 if log_raw_input:
131 131 input_hist = self.shell.history_manager.input_hist_raw
132 132 else:
133 133 input_hist = self.shell.history_manager.input_hist_parsed
134 134
135 135 if log_output:
136 136 log_write = logger.log_write
137 137 output_hist = self.shell.history_manager.output_hist
138 138 for n in range(1,len(input_hist)-1):
139 139 log_write(input_hist[n].rstrip() + u'\n')
140 140 if n in output_hist:
141 141 log_write(str_to_unicode(repr(output_hist[n])),'output')
142 142 else:
143 143 logger.log_write(u'\n'.join(input_hist[1:]))
144 144 logger.log_write(u'\n')
145 145 if timestamp:
146 146 # re-enable timestamping
147 147 logger.timestamp = True
148 148
149 149 print ('Activating auto-logging. '
150 150 'Current session state plus future input saved.')
151 151 logger.logstate()
152 152
153 153 @line_magic
154 154 def logstop(self, parameter_s=''):
155 155 """Fully stop logging and close log file.
156 156
157 157 In order to start logging again, a new %logstart call needs to be made,
158 158 possibly (though not necessarily) with a new filename, mode and other
159 159 options."""
160 160 self.shell.logger.logstop()
161 161
162 162 @line_magic
163 163 def logoff(self, parameter_s=''):
164 164 """Temporarily stop logging.
165 165
166 166 You must have previously started logging."""
167 167 self.shell.logger.switch_log(0)
168 168
169 169 @line_magic
170 170 def logon(self, parameter_s=''):
171 171 """Restart logging.
172 172
173 173 This function is for restarting logging which you've temporarily
174 174 stopped with %logoff. For starting logging for the first time, you
175 175 must use the %logstart function, which allows you to specify an
176 176 optional log filename."""
177 177
178 178 self.shell.logger.switch_log(1)
179 179
180 180 @line_magic
181 181 def logstate(self, parameter_s=''):
182 182 """Print the status of the logging system."""
183 183
184 184 self.shell.logger.logstate()
@@ -1,167 +1,167 b''
1 1 """Implementation of magic functions for matplotlib/pylab support.
2 2 """
3 3 from __future__ import print_function
4 4 #-----------------------------------------------------------------------------
5 5 # Copyright (c) 2012 The IPython Development Team.
6 6 #
7 7 # Distributed under the terms of the Modified BSD License.
8 8 #
9 9 # The full license is in the file COPYING.txt, distributed with this software.
10 10 #-----------------------------------------------------------------------------
11 11
12 12 #-----------------------------------------------------------------------------
13 13 # Imports
14 14 #-----------------------------------------------------------------------------
15 15
16 16 # Our own packages
17 17 from traitlets.config.application import Application
18 18 from IPython.core import magic_arguments
19 19 from IPython.core.magic import Magics, magics_class, line_magic
20 20 from IPython.testing.skipdoctest import skip_doctest
21 from IPython.utils.warn import warn
21 from warnings import warn
22 22 from IPython.core.pylabtools import backends
23 23
24 24 #-----------------------------------------------------------------------------
25 25 # Magic implementation classes
26 26 #-----------------------------------------------------------------------------
27 27
28 28 magic_gui_arg = magic_arguments.argument(
29 29 'gui', nargs='?',
30 30 help="""Name of the matplotlib backend to use %s.
31 31 If given, the corresponding matplotlib backend is used,
32 32 otherwise it will be matplotlib's default
33 33 (which you can set in your matplotlib config file).
34 34 """ % str(tuple(sorted(backends.keys())))
35 35 )
36 36
37 37
38 38 @magics_class
39 39 class PylabMagics(Magics):
40 40 """Magics related to matplotlib's pylab support"""
41 41
42 42 @skip_doctest
43 43 @line_magic
44 44 @magic_arguments.magic_arguments()
45 45 @magic_arguments.argument('-l', '--list', action='store_true',
46 46 help='Show available matplotlib backends')
47 47 @magic_gui_arg
48 48 def matplotlib(self, line=''):
49 49 """Set up matplotlib to work interactively.
50 50
51 51 This function lets you activate matplotlib interactive support
52 52 at any point during an IPython session. It does not import anything
53 53 into the interactive namespace.
54 54
55 55 If you are using the inline matplotlib backend in the IPython Notebook
56 56 you can set which figure formats are enabled using the following::
57 57
58 58 In [1]: from IPython.display import set_matplotlib_formats
59 59
60 60 In [2]: set_matplotlib_formats('pdf', 'svg')
61 61
62 62 The default for inline figures sets `bbox_inches` to 'tight'. This can
63 63 cause discrepancies between the displayed image and the identical
64 64 image created using `savefig`. This behavior can be disabled using the
65 65 `%config` magic::
66 66
67 67 In [3]: %config InlineBackend.print_figure_kwargs = {'bbox_inches':None}
68 68
69 69 In addition, see the docstring of
70 70 `IPython.display.set_matplotlib_formats` and
71 71 `IPython.display.set_matplotlib_close` for more information on
72 72 changing additional behaviors of the inline backend.
73 73
74 74 Examples
75 75 --------
76 76 To enable the inline backend for usage with the IPython Notebook::
77 77
78 78 In [1]: %matplotlib inline
79 79
80 80 In this case, where the matplotlib default is TkAgg::
81 81
82 82 In [2]: %matplotlib
83 83 Using matplotlib backend: TkAgg
84 84
85 85 But you can explicitly request a different GUI backend::
86 86
87 87 In [3]: %matplotlib qt
88 88
89 89 You can list the available backends using the -l/--list option::
90 90
91 91 In [4]: %matplotlib --list
92 92 Available matplotlib backends: ['osx', 'qt4', 'qt5', 'gtk3', 'notebook', 'wx', 'qt', 'nbagg',
93 93 'gtk', 'tk', 'inline']
94 94 """
95 95 args = magic_arguments.parse_argstring(self.matplotlib, line)
96 96 if args.list:
97 97 backends_list = list(backends.keys())
98 98 print("Available matplotlib backends: %s" % backends_list)
99 99 else:
100 100 gui, backend = self.shell.enable_matplotlib(args.gui)
101 101 self._show_matplotlib_backend(args.gui, backend)
102 102
103 103 @skip_doctest
104 104 @line_magic
105 105 @magic_arguments.magic_arguments()
106 106 @magic_arguments.argument(
107 107 '--no-import-all', action='store_true', default=None,
108 108 help="""Prevent IPython from performing ``import *`` into the interactive namespace.
109 109
110 110 You can govern the default behavior of this flag with the
111 111 InteractiveShellApp.pylab_import_all configurable.
112 112 """
113 113 )
114 114 @magic_gui_arg
115 115 def pylab(self, line=''):
116 116 """Load numpy and matplotlib to work interactively.
117 117
118 118 This function lets you activate pylab (matplotlib, numpy and
119 119 interactive support) at any point during an IPython session.
120 120
121 121 %pylab makes the following imports::
122 122
123 123 import numpy
124 124 import matplotlib
125 125 from matplotlib import pylab, mlab, pyplot
126 126 np = numpy
127 127 plt = pyplot
128 128
129 129 from IPython.display import display
130 130 from IPython.core.pylabtools import figsize, getfigs
131 131
132 132 from pylab import *
133 133 from numpy import *
134 134
135 135 If you pass `--no-import-all`, the last two `*` imports will be excluded.
136 136
137 137 See the %matplotlib magic for more details about activating matplotlib
138 138 without affecting the interactive namespace.
139 139 """
140 140 args = magic_arguments.parse_argstring(self.pylab, line)
141 141 if args.no_import_all is None:
142 142 # get default from Application
143 143 if Application.initialized():
144 144 app = Application.instance()
145 145 try:
146 146 import_all = app.pylab_import_all
147 147 except AttributeError:
148 148 import_all = True
149 149 else:
150 150 # nothing specified, no app - default True
151 151 import_all = True
152 152 else:
153 153 # invert no-import flag
154 154 import_all = not args.no_import_all
155 155
156 156 gui, backend, clobbered = self.shell.enable_pylab(args.gui, import_all=import_all)
157 157 self._show_matplotlib_backend(args.gui, backend)
158 158 print ("Populating the interactive namespace from numpy and matplotlib")
159 159 if clobbered:
160 160 warn("pylab import has clobbered these variables: %s" % clobbered +
161 161 "\n`%matplotlib` prevents importing * from pylab and numpy"
162 162 )
163 163
164 164 def _show_matplotlib_backend(self, gui, backend):
165 165 """show matplotlib message backend message"""
166 166 if not gui or gui == 'auto':
167 167 print("Using matplotlib backend: %s" % backend)
@@ -1,1471 +1,1471 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
42 42 Installation instructions for VerboseTB::
43 43
44 44 import sys,ultratb
45 45 sys.excepthook = ultratb.VerboseTB()
46 46
47 47 Note: Much of the code in this module was lifted verbatim from the standard
48 48 library module 'traceback.py' and Ka-Ping Yee's 'cgitb.py'.
49 49
50 50 Color schemes
51 51 -------------
52 52
53 53 The colors are defined in the class TBTools through the use of the
54 54 ColorSchemeTable class. Currently the following exist:
55 55
56 56 - NoColor: allows all of this module to be used in any terminal (the color
57 57 escapes are just dummy blank strings).
58 58
59 59 - Linux: is meant to look good in a terminal like the Linux console (black
60 60 or very dark background).
61 61
62 62 - LightBG: similar to Linux but swaps dark/light colors to be more readable
63 63 in light background terminals.
64 64
65 65 You can implement other color schemes easily, the syntax is fairly
66 66 self-explanatory. Please send back new schemes you develop to the author for
67 67 possible inclusion in future releases.
68 68
69 69 Inheritance diagram:
70 70
71 71 .. inheritance-diagram:: IPython.core.ultratb
72 72 :parts: 3
73 73 """
74 74
75 75 #*****************************************************************************
76 76 # Copyright (C) 2001 Nathaniel Gray <n8gray@caltech.edu>
77 77 # Copyright (C) 2001-2004 Fernando Perez <fperez@colorado.edu>
78 78 #
79 79 # Distributed under the terms of the BSD License. The full license is in
80 80 # the file COPYING, distributed as part of this software.
81 81 #*****************************************************************************
82 82
83 83 from __future__ import unicode_literals
84 84 from __future__ import print_function
85 85
86 86 import dis
87 87 import inspect
88 88 import keyword
89 89 import linecache
90 90 import os
91 91 import pydoc
92 92 import re
93 93 import sys
94 94 import time
95 95 import tokenize
96 96 import traceback
97 97 import types
98 98
99 99 try: # Python 2
100 100 generate_tokens = tokenize.generate_tokens
101 101 except AttributeError: # Python 3
102 102 generate_tokens = tokenize.tokenize
103 103
104 104 # For purposes of monkeypatching inspect to fix a bug in it.
105 105 from inspect import getsourcefile, getfile, getmodule, \
106 106 ismodule, isclass, ismethod, isfunction, istraceback, isframe, iscode
107 107
108 108 # IPython's own modules
109 109 # Modified pdb which doesn't damage IPython's readline handling
110 110 from IPython import get_ipython
111 111 from IPython.core import debugger
112 112 from IPython.core.display_trap import DisplayTrap
113 113 from IPython.core.excolors import exception_colors
114 114 from IPython.utils import PyColorize
115 115 from IPython.utils import io
116 116 from IPython.utils import openpy
117 117 from IPython.utils import path as util_path
118 118 from IPython.utils import py3compat
119 119 from IPython.utils import ulinecache
120 120 from IPython.utils.data import uniq_stable
121 from IPython.utils.warn import info, error
121 from logging import info, error
122 122
123 123 # Globals
124 124 # amount of space to put line numbers before verbose tracebacks
125 125 INDENT_SIZE = 8
126 126
127 127 # Default color scheme. This is used, for example, by the traceback
128 128 # formatter. When running in an actual IPython instance, the user's rc.colors
129 129 # value is used, but having a module global makes this functionality available
130 130 # to users of ultratb who are NOT running inside ipython.
131 131 DEFAULT_SCHEME = 'NoColor'
132 132
133 133 # ---------------------------------------------------------------------------
134 134 # Code begins
135 135
136 136 # Utility functions
137 137 def inspect_error():
138 138 """Print a message about internal inspect errors.
139 139
140 140 These are unfortunately quite common."""
141 141
142 142 error('Internal Python error in the inspect module.\n'
143 143 'Below is the traceback from this internal error.\n')
144 144
145 145
146 146 # This function is a monkeypatch we apply to the Python inspect module. We have
147 147 # now found when it's needed (see discussion on issue gh-1456), and we have a
148 148 # test case (IPython.core.tests.test_ultratb.ChangedPyFileTest) that fails if
149 149 # the monkeypatch is not applied. TK, Aug 2012.
150 150 def findsource(object):
151 151 """Return the entire source file and starting line number for an object.
152 152
153 153 The argument may be a module, class, method, function, traceback, frame,
154 154 or code object. The source code is returned as a list of all the lines
155 155 in the file and the line number indexes a line in that list. An IOError
156 156 is raised if the source code cannot be retrieved.
157 157
158 158 FIXED version with which we monkeypatch the stdlib to work around a bug."""
159 159
160 160 file = getsourcefile(object) or getfile(object)
161 161 # If the object is a frame, then trying to get the globals dict from its
162 162 # module won't work. Instead, the frame object itself has the globals
163 163 # dictionary.
164 164 globals_dict = None
165 165 if inspect.isframe(object):
166 166 # XXX: can this ever be false?
167 167 globals_dict = object.f_globals
168 168 else:
169 169 module = getmodule(object, file)
170 170 if module:
171 171 globals_dict = module.__dict__
172 172 lines = linecache.getlines(file, globals_dict)
173 173 if not lines:
174 174 raise IOError('could not get source code')
175 175
176 176 if ismodule(object):
177 177 return lines, 0
178 178
179 179 if isclass(object):
180 180 name = object.__name__
181 181 pat = re.compile(r'^(\s*)class\s*' + name + r'\b')
182 182 # make some effort to find the best matching class definition:
183 183 # use the one with the least indentation, which is the one
184 184 # that's most probably not inside a function definition.
185 185 candidates = []
186 186 for i in range(len(lines)):
187 187 match = pat.match(lines[i])
188 188 if match:
189 189 # if it's at toplevel, it's already the best one
190 190 if lines[i][0] == 'c':
191 191 return lines, i
192 192 # else add whitespace to candidate list
193 193 candidates.append((match.group(1), i))
194 194 if candidates:
195 195 # this will sort by whitespace, and by line number,
196 196 # less whitespace first
197 197 candidates.sort()
198 198 return lines, candidates[0][1]
199 199 else:
200 200 raise IOError('could not find class definition')
201 201
202 202 if ismethod(object):
203 203 object = object.__func__
204 204 if isfunction(object):
205 205 object = object.__code__
206 206 if istraceback(object):
207 207 object = object.tb_frame
208 208 if isframe(object):
209 209 object = object.f_code
210 210 if iscode(object):
211 211 if not hasattr(object, 'co_firstlineno'):
212 212 raise IOError('could not find function definition')
213 213 pat = re.compile(r'^(\s*def\s)|(.*(?<!\w)lambda(:|\s))|^(\s*@)')
214 214 pmatch = pat.match
215 215 # fperez - fix: sometimes, co_firstlineno can give a number larger than
216 216 # the length of lines, which causes an error. Safeguard against that.
217 217 lnum = min(object.co_firstlineno, len(lines)) - 1
218 218 while lnum > 0:
219 219 if pmatch(lines[lnum]): break
220 220 lnum -= 1
221 221
222 222 return lines, lnum
223 223 raise IOError('could not find code object')
224 224
225 225
226 226 # This is a patched version of inspect.getargs that applies the (unmerged)
227 227 # patch for http://bugs.python.org/issue14611 by Stefano Taschini. This fixes
228 228 # https://github.com/ipython/ipython/issues/8205 and
229 229 # https://github.com/ipython/ipython/issues/8293
230 230 def getargs(co):
231 231 """Get information about the arguments accepted by a code object.
232 232
233 233 Three things are returned: (args, varargs, varkw), where 'args' is
234 234 a list of argument names (possibly containing nested lists), and
235 235 'varargs' and 'varkw' are the names of the * and ** arguments or None."""
236 236 if not iscode(co):
237 237 raise TypeError('{!r} is not a code object'.format(co))
238 238
239 239 nargs = co.co_argcount
240 240 names = co.co_varnames
241 241 args = list(names[:nargs])
242 242 step = 0
243 243
244 244 # The following acrobatics are for anonymous (tuple) arguments.
245 245 for i in range(nargs):
246 246 if args[i][:1] in ('', '.'):
247 247 stack, remain, count = [], [], []
248 248 while step < len(co.co_code):
249 249 op = ord(co.co_code[step])
250 250 step = step + 1
251 251 if op >= dis.HAVE_ARGUMENT:
252 252 opname = dis.opname[op]
253 253 value = ord(co.co_code[step]) + ord(co.co_code[step+1])*256
254 254 step = step + 2
255 255 if opname in ('UNPACK_TUPLE', 'UNPACK_SEQUENCE'):
256 256 remain.append(value)
257 257 count.append(value)
258 258 elif opname in ('STORE_FAST', 'STORE_DEREF'):
259 259 if op in dis.haslocal:
260 260 stack.append(co.co_varnames[value])
261 261 elif op in dis.hasfree:
262 262 stack.append((co.co_cellvars + co.co_freevars)[value])
263 263 # Special case for sublists of length 1: def foo((bar))
264 264 # doesn't generate the UNPACK_TUPLE bytecode, so if
265 265 # `remain` is empty here, we have such a sublist.
266 266 if not remain:
267 267 stack[0] = [stack[0]]
268 268 break
269 269 else:
270 270 remain[-1] = remain[-1] - 1
271 271 while remain[-1] == 0:
272 272 remain.pop()
273 273 size = count.pop()
274 274 stack[-size:] = [stack[-size:]]
275 275 if not remain: break
276 276 remain[-1] = remain[-1] - 1
277 277 if not remain: break
278 278 args[i] = stack[0]
279 279
280 280 varargs = None
281 281 if co.co_flags & inspect.CO_VARARGS:
282 282 varargs = co.co_varnames[nargs]
283 283 nargs = nargs + 1
284 284 varkw = None
285 285 if co.co_flags & inspect.CO_VARKEYWORDS:
286 286 varkw = co.co_varnames[nargs]
287 287 return inspect.Arguments(args, varargs, varkw)
288 288
289 289
290 290 # Monkeypatch inspect to apply our bugfix.
291 291 def with_patch_inspect(f):
292 292 """decorator for monkeypatching inspect.findsource"""
293 293
294 294 def wrapped(*args, **kwargs):
295 295 save_findsource = inspect.findsource
296 296 save_getargs = inspect.getargs
297 297 inspect.findsource = findsource
298 298 inspect.getargs = getargs
299 299 try:
300 300 return f(*args, **kwargs)
301 301 finally:
302 302 inspect.findsource = save_findsource
303 303 inspect.getargs = save_getargs
304 304
305 305 return wrapped
306 306
307 307
308 308 if py3compat.PY3:
309 309 fixed_getargvalues = inspect.getargvalues
310 310 else:
311 311 # Fixes for https://github.com/ipython/ipython/issues/8293
312 312 # and https://github.com/ipython/ipython/issues/8205.
313 313 # The relevant bug is caused by failure to correctly handle anonymous tuple
314 314 # unpacking, which only exists in Python 2.
315 315 fixed_getargvalues = with_patch_inspect(inspect.getargvalues)
316 316
317 317
318 318 def fix_frame_records_filenames(records):
319 319 """Try to fix the filenames in each record from inspect.getinnerframes().
320 320
321 321 Particularly, modules loaded from within zip files have useless filenames
322 322 attached to their code object, and inspect.getinnerframes() just uses it.
323 323 """
324 324 fixed_records = []
325 325 for frame, filename, line_no, func_name, lines, index in records:
326 326 # Look inside the frame's globals dictionary for __file__,
327 327 # which should be better. However, keep Cython filenames since
328 328 # we prefer the source filenames over the compiled .so file.
329 329 filename = py3compat.cast_unicode_py2(filename, "utf-8")
330 330 if not filename.endswith(('.pyx', '.pxd', '.pxi')):
331 331 better_fn = frame.f_globals.get('__file__', None)
332 332 if isinstance(better_fn, str):
333 333 # Check the type just in case someone did something weird with
334 334 # __file__. It might also be None if the error occurred during
335 335 # import.
336 336 filename = better_fn
337 337 fixed_records.append((frame, filename, line_no, func_name, lines, index))
338 338 return fixed_records
339 339
340 340
341 341 @with_patch_inspect
342 342 def _fixed_getinnerframes(etb, context=1, tb_offset=0):
343 343 LNUM_POS, LINES_POS, INDEX_POS = 2, 4, 5
344 344
345 345 records = fix_frame_records_filenames(inspect.getinnerframes(etb, context))
346 346 # If the error is at the console, don't build any context, since it would
347 347 # otherwise produce 5 blank lines printed out (there is no file at the
348 348 # console)
349 349 rec_check = records[tb_offset:]
350 350 try:
351 351 rname = rec_check[0][1]
352 352 if rname == '<ipython console>' or rname.endswith('<string>'):
353 353 return rec_check
354 354 except IndexError:
355 355 pass
356 356
357 357 aux = traceback.extract_tb(etb)
358 358 assert len(records) == len(aux)
359 359 for i, (file, lnum, _, _) in zip(range(len(records)), aux):
360 360 maybeStart = lnum - 1 - context // 2
361 361 start = max(maybeStart, 0)
362 362 end = start + context
363 363 lines = ulinecache.getlines(file)[start:end]
364 364 buf = list(records[i])
365 365 buf[LNUM_POS] = lnum
366 366 buf[INDEX_POS] = lnum - 1 - start
367 367 buf[LINES_POS] = lines
368 368 records[i] = tuple(buf)
369 369 return records[tb_offset:]
370 370
371 371 # Helper function -- largely belongs to VerboseTB, but we need the same
372 372 # functionality to produce a pseudo verbose TB for SyntaxErrors, so that they
373 373 # can be recognized properly by ipython.el's py-traceback-line-re
374 374 # (SyntaxErrors have to be treated specially because they have no traceback)
375 375
376 376 _parser = PyColorize.Parser()
377 377
378 378
379 379 def _format_traceback_lines(lnum, index, lines, Colors, lvals=None, scheme=None):
380 380 numbers_width = INDENT_SIZE - 1
381 381 res = []
382 382 i = lnum - index
383 383
384 384 # This lets us get fully syntax-highlighted tracebacks.
385 385 if scheme is None:
386 386 ipinst = get_ipython()
387 387 if ipinst is not None:
388 388 scheme = ipinst.colors
389 389 else:
390 390 scheme = DEFAULT_SCHEME
391 391
392 392 _line_format = _parser.format2
393 393
394 394 for line in lines:
395 395 line = py3compat.cast_unicode(line)
396 396
397 397 new_line, err = _line_format(line, 'str', scheme)
398 398 if not err: line = new_line
399 399
400 400 if i == lnum:
401 401 # This is the line with the error
402 402 pad = numbers_width - len(str(i))
403 403 num = '%s%s' % (debugger.make_arrow(pad), str(lnum))
404 404 line = '%s%s%s %s%s' % (Colors.linenoEm, num,
405 405 Colors.line, line, Colors.Normal)
406 406 else:
407 407 num = '%*s' % (numbers_width, i)
408 408 line = '%s%s%s %s' % (Colors.lineno, num,
409 409 Colors.Normal, line)
410 410
411 411 res.append(line)
412 412 if lvals and i == lnum:
413 413 res.append(lvals + '\n')
414 414 i = i + 1
415 415 return res
416 416
417 417 def is_recursion_error(etype, value, records):
418 418 try:
419 419 # RecursionError is new in Python 3.5
420 420 recursion_error_type = RecursionError
421 421 except NameError:
422 422 recursion_error_type = RuntimeError
423 423
424 424 # The default recursion limit is 1000, but some of that will be taken up
425 425 # by stack frames in IPython itself. >500 frames probably indicates
426 426 # a recursion error.
427 427 return (etype is recursion_error_type) \
428 428 and "recursion" in str(value).lower() \
429 429 and len(records) > 500
430 430
431 431 def find_recursion(etype, value, records):
432 432 """Identify the repeating stack frames from a RecursionError traceback
433 433
434 434 'records' is a list as returned by VerboseTB.get_records()
435 435
436 436 Returns (last_unique, repeat_length)
437 437 """
438 438 # This involves a bit of guesswork - we want to show enough of the traceback
439 439 # to indicate where the recursion is occurring. We guess that the innermost
440 440 # quarter of the traceback (250 frames by default) is repeats, and find the
441 441 # first frame (from in to out) that looks different.
442 442 if not is_recursion_error(etype, value, records):
443 443 return len(records), 0
444 444
445 445 # Select filename, lineno, func_name to track frames with
446 446 records = [r[1:4] for r in records]
447 447 inner_frames = records[-(len(records)//4):]
448 448 frames_repeated = set(inner_frames)
449 449
450 450 last_seen_at = {}
451 451 longest_repeat = 0
452 452 i = len(records)
453 453 for frame in reversed(records):
454 454 i -= 1
455 455 if frame not in frames_repeated:
456 456 last_unique = i
457 457 break
458 458
459 459 if frame in last_seen_at:
460 460 distance = last_seen_at[frame] - i
461 461 longest_repeat = max(longest_repeat, distance)
462 462
463 463 last_seen_at[frame] = i
464 464 else:
465 465 last_unique = 0 # The whole traceback was recursion
466 466
467 467 return last_unique, longest_repeat
468 468
469 469 #---------------------------------------------------------------------------
470 470 # Module classes
471 471 class TBTools(object):
472 472 """Basic tools used by all traceback printer classes."""
473 473
474 474 # Number of frames to skip when reporting tracebacks
475 475 tb_offset = 0
476 476
477 477 def __init__(self, color_scheme='NoColor', call_pdb=False, ostream=None):
478 478 # Whether to call the interactive pdb debugger after printing
479 479 # tracebacks or not
480 480 self.call_pdb = call_pdb
481 481
482 482 # Output stream to write to. Note that we store the original value in
483 483 # a private attribute and then make the public ostream a property, so
484 484 # that we can delay accessing io.stdout until runtime. The way
485 485 # things are written now, the io.stdout object is dynamically managed
486 486 # so a reference to it should NEVER be stored statically. This
487 487 # property approach confines this detail to a single location, and all
488 488 # subclasses can simply access self.ostream for writing.
489 489 self._ostream = ostream
490 490
491 491 # Create color table
492 492 self.color_scheme_table = exception_colors()
493 493
494 494 self.set_colors(color_scheme)
495 495 self.old_scheme = color_scheme # save initial value for toggles
496 496
497 497 if call_pdb:
498 498 self.pdb = debugger.Pdb(self.color_scheme_table.active_scheme_name)
499 499 else:
500 500 self.pdb = None
501 501
502 502 def _get_ostream(self):
503 503 """Output stream that exceptions are written to.
504 504
505 505 Valid values are:
506 506
507 507 - None: the default, which means that IPython will dynamically resolve
508 508 to io.stdout. This ensures compatibility with most tools, including
509 509 Windows (where plain stdout doesn't recognize ANSI escapes).
510 510
511 511 - Any object with 'write' and 'flush' attributes.
512 512 """
513 513 return io.stdout if self._ostream is None else self._ostream
514 514
515 515 def _set_ostream(self, val):
516 516 assert val is None or (hasattr(val, 'write') and hasattr(val, 'flush'))
517 517 self._ostream = val
518 518
519 519 ostream = property(_get_ostream, _set_ostream)
520 520
521 521 def set_colors(self, *args, **kw):
522 522 """Shorthand access to the color table scheme selector method."""
523 523
524 524 # Set own color table
525 525 self.color_scheme_table.set_active_scheme(*args, **kw)
526 526 # for convenience, set Colors to the active scheme
527 527 self.Colors = self.color_scheme_table.active_colors
528 528 # Also set colors of debugger
529 529 if hasattr(self, 'pdb') and self.pdb is not None:
530 530 self.pdb.set_colors(*args, **kw)
531 531
532 532 def color_toggle(self):
533 533 """Toggle between the currently active color scheme and NoColor."""
534 534
535 535 if self.color_scheme_table.active_scheme_name == 'NoColor':
536 536 self.color_scheme_table.set_active_scheme(self.old_scheme)
537 537 self.Colors = self.color_scheme_table.active_colors
538 538 else:
539 539 self.old_scheme = self.color_scheme_table.active_scheme_name
540 540 self.color_scheme_table.set_active_scheme('NoColor')
541 541 self.Colors = self.color_scheme_table.active_colors
542 542
543 543 def stb2text(self, stb):
544 544 """Convert a structured traceback (a list) to a string."""
545 545 return '\n'.join(stb)
546 546
547 547 def text(self, etype, value, tb, tb_offset=None, context=5):
548 548 """Return formatted traceback.
549 549
550 550 Subclasses may override this if they add extra arguments.
551 551 """
552 552 tb_list = self.structured_traceback(etype, value, tb,
553 553 tb_offset, context)
554 554 return self.stb2text(tb_list)
555 555
556 556 def structured_traceback(self, etype, evalue, tb, tb_offset=None,
557 557 context=5, mode=None):
558 558 """Return a list of traceback frames.
559 559
560 560 Must be implemented by each class.
561 561 """
562 562 raise NotImplementedError()
563 563
564 564
565 565 #---------------------------------------------------------------------------
566 566 class ListTB(TBTools):
567 567 """Print traceback information from a traceback list, with optional color.
568 568
569 569 Calling requires 3 arguments: (etype, evalue, elist)
570 570 as would be obtained by::
571 571
572 572 etype, evalue, tb = sys.exc_info()
573 573 if tb:
574 574 elist = traceback.extract_tb(tb)
575 575 else:
576 576 elist = None
577 577
578 578 It can thus be used by programs which need to process the traceback before
579 579 printing (such as console replacements based on the code module from the
580 580 standard library).
581 581
582 582 Because they are meant to be called without a full traceback (only a
583 583 list), instances of this class can't call the interactive pdb debugger."""
584 584
585 585 def __init__(self, color_scheme='NoColor', call_pdb=False, ostream=None):
586 586 TBTools.__init__(self, color_scheme=color_scheme, call_pdb=call_pdb,
587 587 ostream=ostream)
588 588
589 589 def __call__(self, etype, value, elist):
590 590 self.ostream.flush()
591 591 self.ostream.write(self.text(etype, value, elist))
592 592 self.ostream.write('\n')
593 593
594 594 def structured_traceback(self, etype, value, elist, tb_offset=None,
595 595 context=5):
596 596 """Return a color formatted string with the traceback info.
597 597
598 598 Parameters
599 599 ----------
600 600 etype : exception type
601 601 Type of the exception raised.
602 602
603 603 value : object
604 604 Data stored in the exception
605 605
606 606 elist : list
607 607 List of frames, see class docstring for details.
608 608
609 609 tb_offset : int, optional
610 610 Number of frames in the traceback to skip. If not given, the
611 611 instance value is used (set in constructor).
612 612
613 613 context : int, optional
614 614 Number of lines of context information to print.
615 615
616 616 Returns
617 617 -------
618 618 String with formatted exception.
619 619 """
620 620 tb_offset = self.tb_offset if tb_offset is None else tb_offset
621 621 Colors = self.Colors
622 622 out_list = []
623 623 if elist:
624 624
625 625 if tb_offset and len(elist) > tb_offset:
626 626 elist = elist[tb_offset:]
627 627
628 628 out_list.append('Traceback %s(most recent call last)%s:' %
629 629 (Colors.normalEm, Colors.Normal) + '\n')
630 630 out_list.extend(self._format_list(elist))
631 631 # The exception info should be a single entry in the list.
632 632 lines = ''.join(self._format_exception_only(etype, value))
633 633 out_list.append(lines)
634 634
635 635 # Note: this code originally read:
636 636
637 637 ## for line in lines[:-1]:
638 638 ## out_list.append(" "+line)
639 639 ## out_list.append(lines[-1])
640 640
641 641 # This means it was indenting everything but the last line by a little
642 642 # bit. I've disabled this for now, but if we see ugliness somewhere we
643 643 # can restore it.
644 644
645 645 return out_list
646 646
647 647 def _format_list(self, extracted_list):
648 648 """Format a list of traceback entry tuples for printing.
649 649
650 650 Given a list of tuples as returned by extract_tb() or
651 651 extract_stack(), return a list of strings ready for printing.
652 652 Each string in the resulting list corresponds to the item with the
653 653 same index in the argument list. Each string ends in a newline;
654 654 the strings may contain internal newlines as well, for those items
655 655 whose source text line is not None.
656 656
657 657 Lifted almost verbatim from traceback.py
658 658 """
659 659
660 660 Colors = self.Colors
661 661 list = []
662 662 for filename, lineno, name, line in extracted_list[:-1]:
663 663 item = ' File %s"%s"%s, line %s%d%s, in %s%s%s\n' % \
664 664 (Colors.filename, py3compat.cast_unicode_py2(filename, "utf-8"), Colors.Normal,
665 665 Colors.lineno, lineno, Colors.Normal,
666 666 Colors.name, py3compat.cast_unicode_py2(name, "utf-8"), Colors.Normal)
667 667 if line:
668 668 item += ' %s\n' % line.strip()
669 669 list.append(item)
670 670 # Emphasize the last entry
671 671 filename, lineno, name, line = extracted_list[-1]
672 672 item = '%s File %s"%s"%s, line %s%d%s, in %s%s%s%s\n' % \
673 673 (Colors.normalEm,
674 674 Colors.filenameEm, py3compat.cast_unicode_py2(filename, "utf-8"), Colors.normalEm,
675 675 Colors.linenoEm, lineno, Colors.normalEm,
676 676 Colors.nameEm, py3compat.cast_unicode_py2(name, "utf-8"), Colors.normalEm,
677 677 Colors.Normal)
678 678 if line:
679 679 item += '%s %s%s\n' % (Colors.line, line.strip(),
680 680 Colors.Normal)
681 681 list.append(item)
682 682 return list
683 683
684 684 def _format_exception_only(self, etype, value):
685 685 """Format the exception part of a traceback.
686 686
687 687 The arguments are the exception type and value such as given by
688 688 sys.exc_info()[:2]. The return value is a list of strings, each ending
689 689 in a newline. Normally, the list contains a single string; however,
690 690 for SyntaxError exceptions, it contains several lines that (when
691 691 printed) display detailed information about where the syntax error
692 692 occurred. The message indicating which exception occurred is the
693 693 always last string in the list.
694 694
695 695 Also lifted nearly verbatim from traceback.py
696 696 """
697 697 have_filedata = False
698 698 Colors = self.Colors
699 699 list = []
700 700 stype = Colors.excName + etype.__name__ + Colors.Normal
701 701 if value is None:
702 702 # Not sure if this can still happen in Python 2.6 and above
703 703 list.append(py3compat.cast_unicode(stype) + '\n')
704 704 else:
705 705 if issubclass(etype, SyntaxError):
706 706 have_filedata = True
707 707 if not value.filename: value.filename = "<string>"
708 708 if value.lineno:
709 709 lineno = value.lineno
710 710 textline = ulinecache.getline(value.filename, value.lineno)
711 711 else:
712 712 lineno = 'unknown'
713 713 textline = ''
714 714 list.append('%s File %s"%s"%s, line %s%s%s\n' % \
715 715 (Colors.normalEm,
716 716 Colors.filenameEm, py3compat.cast_unicode(value.filename), Colors.normalEm,
717 717 Colors.linenoEm, lineno, Colors.Normal ))
718 718 if textline == '':
719 719 textline = py3compat.cast_unicode(value.text, "utf-8")
720 720
721 721 if textline is not None:
722 722 i = 0
723 723 while i < len(textline) and textline[i].isspace():
724 724 i += 1
725 725 list.append('%s %s%s\n' % (Colors.line,
726 726 textline.strip(),
727 727 Colors.Normal))
728 728 if value.offset is not None:
729 729 s = ' '
730 730 for c in textline[i:value.offset - 1]:
731 731 if c.isspace():
732 732 s += c
733 733 else:
734 734 s += ' '
735 735 list.append('%s%s^%s\n' % (Colors.caret, s,
736 736 Colors.Normal))
737 737
738 738 try:
739 739 s = value.msg
740 740 except Exception:
741 741 s = self._some_str(value)
742 742 if s:
743 743 list.append('%s%s:%s %s\n' % (str(stype), Colors.excName,
744 744 Colors.Normal, s))
745 745 else:
746 746 list.append('%s\n' % str(stype))
747 747
748 748 # sync with user hooks
749 749 if have_filedata:
750 750 ipinst = get_ipython()
751 751 if ipinst is not None:
752 752 ipinst.hooks.synchronize_with_editor(value.filename, value.lineno, 0)
753 753
754 754 return list
755 755
756 756 def get_exception_only(self, etype, value):
757 757 """Only print the exception type and message, without a traceback.
758 758
759 759 Parameters
760 760 ----------
761 761 etype : exception type
762 762 value : exception value
763 763 """
764 764 return ListTB.structured_traceback(self, etype, value, [])
765 765
766 766 def show_exception_only(self, etype, evalue):
767 767 """Only print the exception type and message, without a traceback.
768 768
769 769 Parameters
770 770 ----------
771 771 etype : exception type
772 772 value : exception value
773 773 """
774 774 # This method needs to use __call__ from *this* class, not the one from
775 775 # a subclass whose signature or behavior may be different
776 776 ostream = self.ostream
777 777 ostream.flush()
778 778 ostream.write('\n'.join(self.get_exception_only(etype, evalue)))
779 779 ostream.flush()
780 780
781 781 def _some_str(self, value):
782 782 # Lifted from traceback.py
783 783 try:
784 784 return str(value)
785 785 except:
786 786 return '<unprintable %s object>' % type(value).__name__
787 787
788 788
789 789 #----------------------------------------------------------------------------
790 790 class VerboseTB(TBTools):
791 791 """A port of Ka-Ping Yee's cgitb.py module that outputs color text instead
792 792 of HTML. Requires inspect and pydoc. Crazy, man.
793 793
794 794 Modified version which optionally strips the topmost entries from the
795 795 traceback, to be used with alternate interpreters (because their own code
796 796 would appear in the traceback)."""
797 797
798 798 def __init__(self, color_scheme='Linux', call_pdb=False, ostream=None,
799 799 tb_offset=0, long_header=False, include_vars=True,
800 800 check_cache=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)
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 def format_records(self, records, last_unique, recursion_repeat):
822 822 """Format the stack frames of the traceback"""
823 823 frames = []
824 824 for r in records[:last_unique+recursion_repeat+1]:
825 825 #print '*** record:',file,lnum,func,lines,index # dbg
826 826 frames.append(self.format_record(*r))
827 827
828 828 if recursion_repeat:
829 829 frames.append('... last %d frames repeated, from the frame below ...\n' % recursion_repeat)
830 830 frames.append(self.format_record(*records[last_unique+recursion_repeat+1]))
831 831
832 832 return frames
833 833
834 834 def format_record(self, frame, file, lnum, func, lines, index):
835 835 """Format a single stack frame"""
836 836 Colors = self.Colors # just a shorthand + quicker name lookup
837 837 ColorsNormal = Colors.Normal # used a lot
838 838 col_scheme = self.color_scheme_table.active_scheme_name
839 839 indent = ' ' * INDENT_SIZE
840 840 em_normal = '%s\n%s%s' % (Colors.valEm, indent, ColorsNormal)
841 841 undefined = '%sundefined%s' % (Colors.em, ColorsNormal)
842 842 tpl_link = '%s%%s%s' % (Colors.filenameEm, ColorsNormal)
843 843 tpl_call = 'in %s%%s%s%%s%s' % (Colors.vName, Colors.valEm,
844 844 ColorsNormal)
845 845 tpl_call_fail = 'in %s%%s%s(***failed resolving arguments***)%s' % \
846 846 (Colors.vName, Colors.valEm, ColorsNormal)
847 847 tpl_local_var = '%s%%s%s' % (Colors.vName, ColorsNormal)
848 848 tpl_global_var = '%sglobal%s %s%%s%s' % (Colors.em, ColorsNormal,
849 849 Colors.vName, ColorsNormal)
850 850 tpl_name_val = '%%s %s= %%s%s' % (Colors.valEm, ColorsNormal)
851 851
852 852 tpl_line = '%s%%s%s %%s' % (Colors.lineno, ColorsNormal)
853 853 tpl_line_em = '%s%%s%s %%s%s' % (Colors.linenoEm, Colors.line,
854 854 ColorsNormal)
855 855
856 856 abspath = os.path.abspath
857 857
858 858
859 859 if not file:
860 860 file = '?'
861 861 elif file.startswith(str("<")) and file.endswith(str(">")):
862 862 # Not a real filename, no problem...
863 863 pass
864 864 elif not os.path.isabs(file):
865 865 # Try to make the filename absolute by trying all
866 866 # sys.path entries (which is also what linecache does)
867 867 for dirname in sys.path:
868 868 try:
869 869 fullname = os.path.join(dirname, file)
870 870 if os.path.isfile(fullname):
871 871 file = os.path.abspath(fullname)
872 872 break
873 873 except Exception:
874 874 # Just in case that sys.path contains very
875 875 # strange entries...
876 876 pass
877 877
878 878 file = py3compat.cast_unicode(file, util_path.fs_encoding)
879 879 link = tpl_link % file
880 880 args, varargs, varkw, locals = fixed_getargvalues(frame)
881 881
882 882 if func == '?':
883 883 call = ''
884 884 else:
885 885 # Decide whether to include variable details or not
886 886 var_repr = self.include_vars and eqrepr or nullrepr
887 887 try:
888 888 call = tpl_call % (func, inspect.formatargvalues(args,
889 889 varargs, varkw,
890 890 locals, formatvalue=var_repr))
891 891 except KeyError:
892 892 # This happens in situations like errors inside generator
893 893 # expressions, where local variables are listed in the
894 894 # line, but can't be extracted from the frame. I'm not
895 895 # 100% sure this isn't actually a bug in inspect itself,
896 896 # but since there's no info for us to compute with, the
897 897 # best we can do is report the failure and move on. Here
898 898 # we must *not* call any traceback construction again,
899 899 # because that would mess up use of %debug later on. So we
900 900 # simply report the failure and move on. The only
901 901 # limitation will be that this frame won't have locals
902 902 # listed in the call signature. Quite subtle problem...
903 903 # I can't think of a good way to validate this in a unit
904 904 # test, but running a script consisting of:
905 905 # dict( (k,v.strip()) for (k,v) in range(10) )
906 906 # will illustrate the error, if this exception catch is
907 907 # disabled.
908 908 call = tpl_call_fail % func
909 909
910 910 # Don't attempt to tokenize binary files.
911 911 if file.endswith(('.so', '.pyd', '.dll')):
912 912 return '%s %s\n' % (link, call)
913 913
914 914 elif file.endswith(('.pyc', '.pyo')):
915 915 # Look up the corresponding source file.
916 916 file = openpy.source_from_cache(file)
917 917
918 918 def linereader(file=file, lnum=[lnum], getline=ulinecache.getline):
919 919 line = getline(file, lnum[0])
920 920 lnum[0] += 1
921 921 return line
922 922
923 923 # Build the list of names on this line of code where the exception
924 924 # occurred.
925 925 try:
926 926 names = []
927 927 name_cont = False
928 928
929 929 for token_type, token, start, end, line in generate_tokens(linereader):
930 930 # build composite names
931 931 if token_type == tokenize.NAME and token not in keyword.kwlist:
932 932 if name_cont:
933 933 # Continuation of a dotted name
934 934 try:
935 935 names[-1].append(token)
936 936 except IndexError:
937 937 names.append([token])
938 938 name_cont = False
939 939 else:
940 940 # Regular new names. We append everything, the caller
941 941 # will be responsible for pruning the list later. It's
942 942 # very tricky to try to prune as we go, b/c composite
943 943 # names can fool us. The pruning at the end is easy
944 944 # to do (or the caller can print a list with repeated
945 945 # names if so desired.
946 946 names.append([token])
947 947 elif token == '.':
948 948 name_cont = True
949 949 elif token_type == tokenize.NEWLINE:
950 950 break
951 951
952 952 except (IndexError, UnicodeDecodeError, SyntaxError):
953 953 # signals exit of tokenizer
954 954 # SyntaxError can occur if the file is not actually Python
955 955 # - see gh-6300
956 956 pass
957 957 except tokenize.TokenError as msg:
958 958 _m = ("An unexpected error occurred while tokenizing input\n"
959 959 "The following traceback may be corrupted or invalid\n"
960 960 "The error message is: %s\n" % msg)
961 961 error(_m)
962 962
963 963 # Join composite names (e.g. "dict.fromkeys")
964 964 names = ['.'.join(n) for n in names]
965 965 # prune names list of duplicates, but keep the right order
966 966 unique_names = uniq_stable(names)
967 967
968 968 # Start loop over vars
969 969 lvals = []
970 970 if self.include_vars:
971 971 for name_full in unique_names:
972 972 name_base = name_full.split('.', 1)[0]
973 973 if name_base in frame.f_code.co_varnames:
974 974 if name_base in locals:
975 975 try:
976 976 value = repr(eval(name_full, locals))
977 977 except:
978 978 value = undefined
979 979 else:
980 980 value = undefined
981 981 name = tpl_local_var % name_full
982 982 else:
983 983 if name_base in frame.f_globals:
984 984 try:
985 985 value = repr(eval(name_full, frame.f_globals))
986 986 except:
987 987 value = undefined
988 988 else:
989 989 value = undefined
990 990 name = tpl_global_var % name_full
991 991 lvals.append(tpl_name_val % (name, value))
992 992 if lvals:
993 993 lvals = '%s%s' % (indent, em_normal.join(lvals))
994 994 else:
995 995 lvals = ''
996 996
997 997 level = '%s %s\n' % (link, call)
998 998
999 999 if index is None:
1000 1000 return level
1001 1001 else:
1002 1002 return '%s%s' % (level, ''.join(
1003 1003 _format_traceback_lines(lnum, index, lines, Colors, lvals,
1004 1004 col_scheme)))
1005 1005
1006 1006 def prepare_chained_exception_message(self, cause):
1007 1007 direct_cause = "\nThe above exception was the direct cause of the following exception:\n"
1008 1008 exception_during_handling = "\nDuring handling of the above exception, another exception occurred:\n"
1009 1009
1010 1010 if cause:
1011 1011 message = [[direct_cause]]
1012 1012 else:
1013 1013 message = [[exception_during_handling]]
1014 1014 return message
1015 1015
1016 1016 def prepare_header(self, etype, long_version=False):
1017 1017 colors = self.Colors # just a shorthand + quicker name lookup
1018 1018 colorsnormal = colors.Normal # used a lot
1019 1019 exc = '%s%s%s' % (colors.excName, etype, colorsnormal)
1020 1020 if long_version:
1021 1021 # Header with the exception type, python version, and date
1022 1022 pyver = 'Python ' + sys.version.split()[0] + ': ' + sys.executable
1023 1023 date = time.ctime(time.time())
1024 1024
1025 1025 head = '%s%s%s\n%s%s%s\n%s' % (colors.topline, '-' * 75, colorsnormal,
1026 1026 exc, ' ' * (75 - len(str(etype)) - len(pyver)),
1027 1027 pyver, date.rjust(75) )
1028 1028 head += "\nA problem occurred executing Python code. Here is the sequence of function" \
1029 1029 "\ncalls leading up to the error, with the most recent (innermost) call last."
1030 1030 else:
1031 1031 # Simplified header
1032 1032 head = '%s%s' % (exc, 'Traceback (most recent call last)'. \
1033 1033 rjust(75 - len(str(etype))) )
1034 1034
1035 1035 return head
1036 1036
1037 1037 def format_exception(self, etype, evalue):
1038 1038 colors = self.Colors # just a shorthand + quicker name lookup
1039 1039 colorsnormal = colors.Normal # used a lot
1040 1040 indent = ' ' * INDENT_SIZE
1041 1041 # Get (safely) a string form of the exception info
1042 1042 try:
1043 1043 etype_str, evalue_str = map(str, (etype, evalue))
1044 1044 except:
1045 1045 # User exception is improperly defined.
1046 1046 etype, evalue = str, sys.exc_info()[:2]
1047 1047 etype_str, evalue_str = map(str, (etype, evalue))
1048 1048 # ... and format it
1049 1049 exception = ['%s%s%s: %s' % (colors.excName, etype_str,
1050 1050 colorsnormal, py3compat.cast_unicode(evalue_str))]
1051 1051
1052 1052 if (not py3compat.PY3) and type(evalue) is types.InstanceType:
1053 1053 try:
1054 1054 names = [w for w in dir(evalue) if isinstance(w, py3compat.string_types)]
1055 1055 except:
1056 1056 # Every now and then, an object with funny internals blows up
1057 1057 # when dir() is called on it. We do the best we can to report
1058 1058 # the problem and continue
1059 1059 _m = '%sException reporting error (object with broken dir())%s:'
1060 1060 exception.append(_m % (colors.excName, colorsnormal))
1061 1061 etype_str, evalue_str = map(str, sys.exc_info()[:2])
1062 1062 exception.append('%s%s%s: %s' % (colors.excName, etype_str,
1063 1063 colorsnormal, py3compat.cast_unicode(evalue_str)))
1064 1064 names = []
1065 1065 for name in names:
1066 1066 value = text_repr(getattr(evalue, name))
1067 1067 exception.append('\n%s%s = %s' % (indent, name, value))
1068 1068
1069 1069 return exception
1070 1070
1071 1071 def format_exception_as_a_whole(self, etype, evalue, etb, number_of_lines_of_context, tb_offset):
1072 1072 """Formats the header, traceback and exception message for a single exception.
1073 1073
1074 1074 This may be called multiple times by Python 3 exception chaining
1075 1075 (PEP 3134).
1076 1076 """
1077 1077 # some locals
1078 1078 orig_etype = etype
1079 1079 try:
1080 1080 etype = etype.__name__
1081 1081 except AttributeError:
1082 1082 pass
1083 1083
1084 1084 tb_offset = self.tb_offset if tb_offset is None else tb_offset
1085 1085 head = self.prepare_header(etype, self.long_header)
1086 1086 records = self.get_records(etb, number_of_lines_of_context, tb_offset)
1087 1087
1088 1088 if records is None:
1089 1089 return ""
1090 1090
1091 1091 last_unique, recursion_repeat = find_recursion(orig_etype, evalue, records)
1092 1092
1093 1093 frames = self.format_records(records, last_unique, recursion_repeat)
1094 1094
1095 1095 formatted_exception = self.format_exception(etype, evalue)
1096 1096 if records:
1097 1097 filepath, lnum = records[-1][1:3]
1098 1098 filepath = os.path.abspath(filepath)
1099 1099 ipinst = get_ipython()
1100 1100 if ipinst is not None:
1101 1101 ipinst.hooks.synchronize_with_editor(filepath, lnum, 0)
1102 1102
1103 1103 return [[head] + frames + [''.join(formatted_exception[0])]]
1104 1104
1105 1105 def get_records(self, etb, number_of_lines_of_context, tb_offset):
1106 1106 try:
1107 1107 # Try the default getinnerframes and Alex's: Alex's fixes some
1108 1108 # problems, but it generates empty tracebacks for console errors
1109 1109 # (5 blanks lines) where none should be returned.
1110 1110 return _fixed_getinnerframes(etb, number_of_lines_of_context, tb_offset)
1111 1111 except:
1112 1112 # FIXME: I've been getting many crash reports from python 2.3
1113 1113 # users, traceable to inspect.py. If I can find a small test-case
1114 1114 # to reproduce this, I should either write a better workaround or
1115 1115 # file a bug report against inspect (if that's the real problem).
1116 1116 # So far, I haven't been able to find an isolated example to
1117 1117 # reproduce the problem.
1118 1118 inspect_error()
1119 1119 traceback.print_exc(file=self.ostream)
1120 1120 info('\nUnfortunately, your original traceback can not be constructed.\n')
1121 1121 return None
1122 1122
1123 1123 def get_parts_of_chained_exception(self, evalue):
1124 1124 def get_chained_exception(exception_value):
1125 1125 cause = getattr(exception_value, '__cause__', None)
1126 1126 if cause:
1127 1127 return cause
1128 1128 if getattr(exception_value, '__suppress_context__', False):
1129 1129 return None
1130 1130 return getattr(exception_value, '__context__', None)
1131 1131
1132 1132 chained_evalue = get_chained_exception(evalue)
1133 1133
1134 1134 if chained_evalue:
1135 1135 return chained_evalue.__class__, chained_evalue, chained_evalue.__traceback__
1136 1136
1137 1137 def structured_traceback(self, etype, evalue, etb, tb_offset=None,
1138 1138 number_of_lines_of_context=5):
1139 1139 """Return a nice text document describing the traceback."""
1140 1140
1141 1141 formatted_exception = self.format_exception_as_a_whole(etype, evalue, etb, number_of_lines_of_context,
1142 1142 tb_offset)
1143 1143
1144 1144 colors = self.Colors # just a shorthand + quicker name lookup
1145 1145 colorsnormal = colors.Normal # used a lot
1146 1146 head = '%s%s%s' % (colors.topline, '-' * 75, colorsnormal)
1147 1147 structured_traceback_parts = [head]
1148 1148 if py3compat.PY3:
1149 1149 chained_exceptions_tb_offset = 0
1150 1150 lines_of_context = 3
1151 1151 formatted_exceptions = formatted_exception
1152 1152 exception = self.get_parts_of_chained_exception(evalue)
1153 1153 if exception:
1154 1154 formatted_exceptions += self.prepare_chained_exception_message(evalue.__cause__)
1155 1155 etype, evalue, etb = exception
1156 1156 else:
1157 1157 evalue = None
1158 1158 chained_exc_ids = set()
1159 1159 while evalue:
1160 1160 formatted_exceptions += self.format_exception_as_a_whole(etype, evalue, etb, lines_of_context,
1161 1161 chained_exceptions_tb_offset)
1162 1162 exception = self.get_parts_of_chained_exception(evalue)
1163 1163
1164 1164 if exception and not id(exception[1]) in chained_exc_ids:
1165 1165 chained_exc_ids.add(id(exception[1])) # trace exception to avoid infinite 'cause' loop
1166 1166 formatted_exceptions += self.prepare_chained_exception_message(evalue.__cause__)
1167 1167 etype, evalue, etb = exception
1168 1168 else:
1169 1169 evalue = None
1170 1170
1171 1171 # we want to see exceptions in a reversed order:
1172 1172 # the first exception should be on top
1173 1173 for formatted_exception in reversed(formatted_exceptions):
1174 1174 structured_traceback_parts += formatted_exception
1175 1175 else:
1176 1176 structured_traceback_parts += formatted_exception[0]
1177 1177
1178 1178 return structured_traceback_parts
1179 1179
1180 1180 def debugger(self, force=False):
1181 1181 """Call up the pdb debugger if desired, always clean up the tb
1182 1182 reference.
1183 1183
1184 1184 Keywords:
1185 1185
1186 1186 - force(False): by default, this routine checks the instance call_pdb
1187 1187 flag and does not actually invoke the debugger if the flag is false.
1188 1188 The 'force' option forces the debugger to activate even if the flag
1189 1189 is false.
1190 1190
1191 1191 If the call_pdb flag is set, the pdb interactive debugger is
1192 1192 invoked. In all cases, the self.tb reference to the current traceback
1193 1193 is deleted to prevent lingering references which hamper memory
1194 1194 management.
1195 1195
1196 1196 Note that each call to pdb() does an 'import readline', so if your app
1197 1197 requires a special setup for the readline completers, you'll have to
1198 1198 fix that by hand after invoking the exception handler."""
1199 1199
1200 1200 if force or self.call_pdb:
1201 1201 if self.pdb is None:
1202 1202 self.pdb = debugger.Pdb(
1203 1203 self.color_scheme_table.active_scheme_name)
1204 1204 # the system displayhook may have changed, restore the original
1205 1205 # for pdb
1206 1206 display_trap = DisplayTrap(hook=sys.__displayhook__)
1207 1207 with display_trap:
1208 1208 self.pdb.reset()
1209 1209 # Find the right frame so we don't pop up inside ipython itself
1210 1210 if hasattr(self, 'tb') and self.tb is not None:
1211 1211 etb = self.tb
1212 1212 else:
1213 1213 etb = self.tb = sys.last_traceback
1214 1214 while self.tb is not None and self.tb.tb_next is not None:
1215 1215 self.tb = self.tb.tb_next
1216 1216 if etb and etb.tb_next:
1217 1217 etb = etb.tb_next
1218 1218 self.pdb.botframe = etb.tb_frame
1219 1219 self.pdb.interaction(self.tb.tb_frame, self.tb)
1220 1220
1221 1221 if hasattr(self, 'tb'):
1222 1222 del self.tb
1223 1223
1224 1224 def handler(self, info=None):
1225 1225 (etype, evalue, etb) = info or sys.exc_info()
1226 1226 self.tb = etb
1227 1227 ostream = self.ostream
1228 1228 ostream.flush()
1229 1229 ostream.write(self.text(etype, evalue, etb))
1230 1230 ostream.write('\n')
1231 1231 ostream.flush()
1232 1232
1233 1233 # Changed so an instance can just be called as VerboseTB_inst() and print
1234 1234 # out the right info on its own.
1235 1235 def __call__(self, etype=None, evalue=None, etb=None):
1236 1236 """This hook can replace sys.excepthook (for Python 2.1 or higher)."""
1237 1237 if etb is None:
1238 1238 self.handler()
1239 1239 else:
1240 1240 self.handler((etype, evalue, etb))
1241 1241 try:
1242 1242 self.debugger()
1243 1243 except KeyboardInterrupt:
1244 1244 print("\nKeyboardInterrupt")
1245 1245
1246 1246
1247 1247 #----------------------------------------------------------------------------
1248 1248 class FormattedTB(VerboseTB, ListTB):
1249 1249 """Subclass ListTB but allow calling with a traceback.
1250 1250
1251 1251 It can thus be used as a sys.excepthook for Python > 2.1.
1252 1252
1253 1253 Also adds 'Context' and 'Verbose' modes, not available in ListTB.
1254 1254
1255 1255 Allows a tb_offset to be specified. This is useful for situations where
1256 1256 one needs to remove a number of topmost frames from the traceback (such as
1257 1257 occurs with python programs that themselves execute other python code,
1258 1258 like Python shells). """
1259 1259
1260 1260 def __init__(self, mode='Plain', color_scheme='Linux', call_pdb=False,
1261 1261 ostream=None,
1262 1262 tb_offset=0, long_header=False, include_vars=False,
1263 1263 check_cache=None):
1264 1264
1265 1265 # NEVER change the order of this list. Put new modes at the end:
1266 1266 self.valid_modes = ['Plain', 'Context', 'Verbose']
1267 1267 self.verbose_modes = self.valid_modes[1:3]
1268 1268
1269 1269 VerboseTB.__init__(self, color_scheme=color_scheme, call_pdb=call_pdb,
1270 1270 ostream=ostream, tb_offset=tb_offset,
1271 1271 long_header=long_header, include_vars=include_vars,
1272 1272 check_cache=check_cache)
1273 1273
1274 1274 # Different types of tracebacks are joined with different separators to
1275 1275 # form a single string. They are taken from this dict
1276 1276 self._join_chars = dict(Plain='', Context='\n', Verbose='\n')
1277 1277 # set_mode also sets the tb_join_char attribute
1278 1278 self.set_mode(mode)
1279 1279
1280 1280 def _extract_tb(self, tb):
1281 1281 if tb:
1282 1282 return traceback.extract_tb(tb)
1283 1283 else:
1284 1284 return None
1285 1285
1286 1286 def structured_traceback(self, etype, value, tb, tb_offset=None, number_of_lines_of_context=5):
1287 1287 tb_offset = self.tb_offset if tb_offset is None else tb_offset
1288 1288 mode = self.mode
1289 1289 if mode in self.verbose_modes:
1290 1290 # Verbose modes need a full traceback
1291 1291 return VerboseTB.structured_traceback(
1292 1292 self, etype, value, tb, tb_offset, number_of_lines_of_context
1293 1293 )
1294 1294 else:
1295 1295 # We must check the source cache because otherwise we can print
1296 1296 # out-of-date source code.
1297 1297 self.check_cache()
1298 1298 # Now we can extract and format the exception
1299 1299 elist = self._extract_tb(tb)
1300 1300 return ListTB.structured_traceback(
1301 1301 self, etype, value, elist, tb_offset, number_of_lines_of_context
1302 1302 )
1303 1303
1304 1304 def stb2text(self, stb):
1305 1305 """Convert a structured traceback (a list) to a string."""
1306 1306 return self.tb_join_char.join(stb)
1307 1307
1308 1308
1309 1309 def set_mode(self, mode=None):
1310 1310 """Switch to the desired mode.
1311 1311
1312 1312 If mode is not specified, cycles through the available modes."""
1313 1313
1314 1314 if not mode:
1315 1315 new_idx = (self.valid_modes.index(self.mode) + 1 ) % \
1316 1316 len(self.valid_modes)
1317 1317 self.mode = self.valid_modes[new_idx]
1318 1318 elif mode not in self.valid_modes:
1319 1319 raise ValueError('Unrecognized mode in FormattedTB: <' + mode + '>\n'
1320 1320 'Valid modes: ' + str(self.valid_modes))
1321 1321 else:
1322 1322 self.mode = mode
1323 1323 # include variable details only in 'Verbose' mode
1324 1324 self.include_vars = (self.mode == self.valid_modes[2])
1325 1325 # Set the join character for generating text tracebacks
1326 1326 self.tb_join_char = self._join_chars[self.mode]
1327 1327
1328 1328 # some convenient shortcuts
1329 1329 def plain(self):
1330 1330 self.set_mode(self.valid_modes[0])
1331 1331
1332 1332 def context(self):
1333 1333 self.set_mode(self.valid_modes[1])
1334 1334
1335 1335 def verbose(self):
1336 1336 self.set_mode(self.valid_modes[2])
1337 1337
1338 1338
1339 1339 #----------------------------------------------------------------------------
1340 1340 class AutoFormattedTB(FormattedTB):
1341 1341 """A traceback printer which can be called on the fly.
1342 1342
1343 1343 It will find out about exceptions by itself.
1344 1344
1345 1345 A brief example::
1346 1346
1347 1347 AutoTB = AutoFormattedTB(mode = 'Verbose',color_scheme='Linux')
1348 1348 try:
1349 1349 ...
1350 1350 except:
1351 1351 AutoTB() # or AutoTB(out=logfile) where logfile is an open file object
1352 1352 """
1353 1353
1354 1354 def __call__(self, etype=None, evalue=None, etb=None,
1355 1355 out=None, tb_offset=None):
1356 1356 """Print out a formatted exception traceback.
1357 1357
1358 1358 Optional arguments:
1359 1359 - out: an open file-like object to direct output to.
1360 1360
1361 1361 - tb_offset: the number of frames to skip over in the stack, on a
1362 1362 per-call basis (this overrides temporarily the instance's tb_offset
1363 1363 given at initialization time. """
1364 1364
1365 1365 if out is None:
1366 1366 out = self.ostream
1367 1367 out.flush()
1368 1368 out.write(self.text(etype, evalue, etb, tb_offset))
1369 1369 out.write('\n')
1370 1370 out.flush()
1371 1371 # FIXME: we should remove the auto pdb behavior from here and leave
1372 1372 # that to the clients.
1373 1373 try:
1374 1374 self.debugger()
1375 1375 except KeyboardInterrupt:
1376 1376 print("\nKeyboardInterrupt")
1377 1377
1378 1378 def structured_traceback(self, etype=None, value=None, tb=None,
1379 1379 tb_offset=None, number_of_lines_of_context=5):
1380 1380 if etype is None:
1381 1381 etype, value, tb = sys.exc_info()
1382 1382 self.tb = tb
1383 1383 return FormattedTB.structured_traceback(
1384 1384 self, etype, value, tb, tb_offset, number_of_lines_of_context)
1385 1385
1386 1386
1387 1387 #---------------------------------------------------------------------------
1388 1388
1389 1389 # A simple class to preserve Nathan's original functionality.
1390 1390 class ColorTB(FormattedTB):
1391 1391 """Shorthand to initialize a FormattedTB in Linux colors mode."""
1392 1392
1393 1393 def __init__(self, color_scheme='Linux', call_pdb=0, **kwargs):
1394 1394 FormattedTB.__init__(self, color_scheme=color_scheme,
1395 1395 call_pdb=call_pdb, **kwargs)
1396 1396
1397 1397
1398 1398 class SyntaxTB(ListTB):
1399 1399 """Extension which holds some state: the last exception value"""
1400 1400
1401 1401 def __init__(self, color_scheme='NoColor'):
1402 1402 ListTB.__init__(self, color_scheme)
1403 1403 self.last_syntax_error = None
1404 1404
1405 1405 def __call__(self, etype, value, elist):
1406 1406 self.last_syntax_error = value
1407 1407
1408 1408 ListTB.__call__(self, etype, value, elist)
1409 1409
1410 1410 def structured_traceback(self, etype, value, elist, tb_offset=None,
1411 1411 context=5):
1412 1412 # If the source file has been edited, the line in the syntax error can
1413 1413 # be wrong (retrieved from an outdated cache). This replaces it with
1414 1414 # the current value.
1415 1415 if isinstance(value, SyntaxError) \
1416 1416 and isinstance(value.filename, py3compat.string_types) \
1417 1417 and isinstance(value.lineno, int):
1418 1418 linecache.checkcache(value.filename)
1419 1419 newtext = ulinecache.getline(value.filename, value.lineno)
1420 1420 if newtext:
1421 1421 value.text = newtext
1422 1422 return super(SyntaxTB, self).structured_traceback(etype, value, elist,
1423 1423 tb_offset=tb_offset, context=context)
1424 1424
1425 1425 def clear_err_state(self):
1426 1426 """Return the current error state and clear it"""
1427 1427 e = self.last_syntax_error
1428 1428 self.last_syntax_error = None
1429 1429 return e
1430 1430
1431 1431 def stb2text(self, stb):
1432 1432 """Convert a structured traceback (a list) to a string."""
1433 1433 return ''.join(stb)
1434 1434
1435 1435
1436 1436 # some internal-use functions
1437 1437 def text_repr(value):
1438 1438 """Hopefully pretty robust repr equivalent."""
1439 1439 # this is pretty horrible but should always return *something*
1440 1440 try:
1441 1441 return pydoc.text.repr(value)
1442 1442 except KeyboardInterrupt:
1443 1443 raise
1444 1444 except:
1445 1445 try:
1446 1446 return repr(value)
1447 1447 except KeyboardInterrupt:
1448 1448 raise
1449 1449 except:
1450 1450 try:
1451 1451 # all still in an except block so we catch
1452 1452 # getattr raising
1453 1453 name = getattr(value, '__name__', None)
1454 1454 if name:
1455 1455 # ick, recursion
1456 1456 return text_repr(name)
1457 1457 klass = getattr(value, '__class__', None)
1458 1458 if klass:
1459 1459 return '%s instance' % text_repr(klass)
1460 1460 except KeyboardInterrupt:
1461 1461 raise
1462 1462 except:
1463 1463 return 'UNRECOVERABLE REPR FAILURE'
1464 1464
1465 1465
1466 1466 def eqrepr(value, repr=text_repr):
1467 1467 return '=%s' % repr(value)
1468 1468
1469 1469
1470 1470 def nullrepr(value, repr=text_repr):
1471 1471 return ''
@@ -1,491 +1,491 b''
1 1 # -*- coding: utf-8 -*-
2 2 """Manage background (threaded) jobs conveniently from an interactive shell.
3 3
4 4 This module provides a BackgroundJobManager class. This is the main class
5 5 meant for public usage, it implements an object which can create and manage
6 6 new background jobs.
7 7
8 8 It also provides the actual job classes managed by these BackgroundJobManager
9 9 objects, see their docstrings below.
10 10
11 11
12 12 This system was inspired by discussions with B. Granger and the
13 13 BackgroundCommand class described in the book Python Scripting for
14 14 Computational Science, by H. P. Langtangen:
15 15
16 16 http://folk.uio.no/hpl/scripting
17 17
18 18 (although ultimately no code from this text was used, as IPython's system is a
19 19 separate implementation).
20 20
21 21 An example notebook is provided in our documentation illustrating interactive
22 22 use of the system.
23 23 """
24 24 from __future__ import print_function
25 25
26 26 #*****************************************************************************
27 27 # Copyright (C) 2005-2006 Fernando Perez <fperez@colorado.edu>
28 28 #
29 29 # Distributed under the terms of the BSD License. The full license is in
30 30 # the file COPYING, distributed as part of this software.
31 31 #*****************************************************************************
32 32
33 33 # Code begins
34 34 import sys
35 35 import threading
36 36
37 37 from IPython import get_ipython
38 38 from IPython.core.ultratb import AutoFormattedTB
39 from IPython.utils.warn import error
39 from logging import error
40 40 from IPython.utils.py3compat import string_types
41 41
42 42
43 43 class BackgroundJobManager(object):
44 44 """Class to manage a pool of backgrounded threaded jobs.
45 45
46 46 Below, we assume that 'jobs' is a BackgroundJobManager instance.
47 47
48 48 Usage summary (see the method docstrings for details):
49 49
50 50 jobs.new(...) -> start a new job
51 51
52 52 jobs() or jobs.status() -> print status summary of all jobs
53 53
54 54 jobs[N] -> returns job number N.
55 55
56 56 foo = jobs[N].result -> assign to variable foo the result of job N
57 57
58 58 jobs[N].traceback() -> print the traceback of dead job N
59 59
60 60 jobs.remove(N) -> remove (finished) job N
61 61
62 62 jobs.flush() -> remove all finished jobs
63 63
64 64 As a convenience feature, BackgroundJobManager instances provide the
65 65 utility result and traceback methods which retrieve the corresponding
66 66 information from the jobs list:
67 67
68 68 jobs.result(N) <--> jobs[N].result
69 69 jobs.traceback(N) <--> jobs[N].traceback()
70 70
71 71 While this appears minor, it allows you to use tab completion
72 72 interactively on the job manager instance.
73 73 """
74 74
75 75 def __init__(self):
76 76 # Lists for job management, accessed via a property to ensure they're
77 77 # up to date.x
78 78 self._running = []
79 79 self._completed = []
80 80 self._dead = []
81 81 # A dict of all jobs, so users can easily access any of them
82 82 self.all = {}
83 83 # For reporting
84 84 self._comp_report = []
85 85 self._dead_report = []
86 86 # Store status codes locally for fast lookups
87 87 self._s_created = BackgroundJobBase.stat_created_c
88 88 self._s_running = BackgroundJobBase.stat_running_c
89 89 self._s_completed = BackgroundJobBase.stat_completed_c
90 90 self._s_dead = BackgroundJobBase.stat_dead_c
91 91
92 92 @property
93 93 def running(self):
94 94 self._update_status()
95 95 return self._running
96 96
97 97 @property
98 98 def dead(self):
99 99 self._update_status()
100 100 return self._dead
101 101
102 102 @property
103 103 def completed(self):
104 104 self._update_status()
105 105 return self._completed
106 106
107 107 def new(self, func_or_exp, *args, **kwargs):
108 108 """Add a new background job and start it in a separate thread.
109 109
110 110 There are two types of jobs which can be created:
111 111
112 112 1. Jobs based on expressions which can be passed to an eval() call.
113 113 The expression must be given as a string. For example:
114 114
115 115 job_manager.new('myfunc(x,y,z=1)'[,glob[,loc]])
116 116
117 117 The given expression is passed to eval(), along with the optional
118 118 global/local dicts provided. If no dicts are given, they are
119 119 extracted automatically from the caller's frame.
120 120
121 121 A Python statement is NOT a valid eval() expression. Basically, you
122 122 can only use as an eval() argument something which can go on the right
123 123 of an '=' sign and be assigned to a variable.
124 124
125 125 For example,"print 'hello'" is not valid, but '2+3' is.
126 126
127 127 2. Jobs given a function object, optionally passing additional
128 128 positional arguments:
129 129
130 130 job_manager.new(myfunc, x, y)
131 131
132 132 The function is called with the given arguments.
133 133
134 134 If you need to pass keyword arguments to your function, you must
135 135 supply them as a dict named kw:
136 136
137 137 job_manager.new(myfunc, x, y, kw=dict(z=1))
138 138
139 139 The reason for this assymmetry is that the new() method needs to
140 140 maintain access to its own keywords, and this prevents name collisions
141 141 between arguments to new() and arguments to your own functions.
142 142
143 143 In both cases, the result is stored in the job.result field of the
144 144 background job object.
145 145
146 146 You can set `daemon` attribute of the thread by giving the keyword
147 147 argument `daemon`.
148 148
149 149 Notes and caveats:
150 150
151 151 1. All threads running share the same standard output. Thus, if your
152 152 background jobs generate output, it will come out on top of whatever
153 153 you are currently writing. For this reason, background jobs are best
154 154 used with silent functions which simply return their output.
155 155
156 156 2. Threads also all work within the same global namespace, and this
157 157 system does not lock interactive variables. So if you send job to the
158 158 background which operates on a mutable object for a long time, and
159 159 start modifying that same mutable object interactively (or in another
160 160 backgrounded job), all sorts of bizarre behaviour will occur.
161 161
162 162 3. If a background job is spending a lot of time inside a C extension
163 163 module which does not release the Python Global Interpreter Lock
164 164 (GIL), this will block the IPython prompt. This is simply because the
165 165 Python interpreter can only switch between threads at Python
166 166 bytecodes. While the execution is inside C code, the interpreter must
167 167 simply wait unless the extension module releases the GIL.
168 168
169 169 4. There is no way, due to limitations in the Python threads library,
170 170 to kill a thread once it has started."""
171 171
172 172 if callable(func_or_exp):
173 173 kw = kwargs.get('kw',{})
174 174 job = BackgroundJobFunc(func_or_exp,*args,**kw)
175 175 elif isinstance(func_or_exp, string_types):
176 176 if not args:
177 177 frame = sys._getframe(1)
178 178 glob, loc = frame.f_globals, frame.f_locals
179 179 elif len(args)==1:
180 180 glob = loc = args[0]
181 181 elif len(args)==2:
182 182 glob,loc = args
183 183 else:
184 184 raise ValueError(
185 185 'Expression jobs take at most 2 args (globals,locals)')
186 186 job = BackgroundJobExpr(func_or_exp, glob, loc)
187 187 else:
188 188 raise TypeError('invalid args for new job')
189 189
190 190 if kwargs.get('daemon', False):
191 191 job.daemon = True
192 192 job.num = len(self.all)+1 if self.all else 0
193 193 self.running.append(job)
194 194 self.all[job.num] = job
195 195 print('Starting job # %s in a separate thread.' % job.num)
196 196 job.start()
197 197 return job
198 198
199 199 def __getitem__(self, job_key):
200 200 num = job_key if isinstance(job_key, int) else job_key.num
201 201 return self.all[num]
202 202
203 203 def __call__(self):
204 204 """An alias to self.status(),
205 205
206 206 This allows you to simply call a job manager instance much like the
207 207 Unix `jobs` shell command."""
208 208
209 209 return self.status()
210 210
211 211 def _update_status(self):
212 212 """Update the status of the job lists.
213 213
214 214 This method moves finished jobs to one of two lists:
215 215 - self.completed: jobs which completed successfully
216 216 - self.dead: jobs which finished but died.
217 217
218 218 It also copies those jobs to corresponding _report lists. These lists
219 219 are used to report jobs completed/dead since the last update, and are
220 220 then cleared by the reporting function after each call."""
221 221
222 222 # Status codes
223 223 srun, scomp, sdead = self._s_running, self._s_completed, self._s_dead
224 224 # State lists, use the actual lists b/c the public names are properties
225 225 # that call this very function on access
226 226 running, completed, dead = self._running, self._completed, self._dead
227 227
228 228 # Now, update all state lists
229 229 for num, job in enumerate(running):
230 230 stat = job.stat_code
231 231 if stat == srun:
232 232 continue
233 233 elif stat == scomp:
234 234 completed.append(job)
235 235 self._comp_report.append(job)
236 236 running[num] = False
237 237 elif stat == sdead:
238 238 dead.append(job)
239 239 self._dead_report.append(job)
240 240 running[num] = False
241 241 # Remove dead/completed jobs from running list
242 242 running[:] = filter(None, running)
243 243
244 244 def _group_report(self,group,name):
245 245 """Report summary for a given job group.
246 246
247 247 Return True if the group had any elements."""
248 248
249 249 if group:
250 250 print('%s jobs:' % name)
251 251 for job in group:
252 252 print('%s : %s' % (job.num,job))
253 253 print()
254 254 return True
255 255
256 256 def _group_flush(self,group,name):
257 257 """Flush a given job group
258 258
259 259 Return True if the group had any elements."""
260 260
261 261 njobs = len(group)
262 262 if njobs:
263 263 plural = {1:''}.setdefault(njobs,'s')
264 264 print('Flushing %s %s job%s.' % (njobs,name,plural))
265 265 group[:] = []
266 266 return True
267 267
268 268 def _status_new(self):
269 269 """Print the status of newly finished jobs.
270 270
271 271 Return True if any new jobs are reported.
272 272
273 273 This call resets its own state every time, so it only reports jobs
274 274 which have finished since the last time it was called."""
275 275
276 276 self._update_status()
277 277 new_comp = self._group_report(self._comp_report, 'Completed')
278 278 new_dead = self._group_report(self._dead_report,
279 279 'Dead, call jobs.traceback() for details')
280 280 self._comp_report[:] = []
281 281 self._dead_report[:] = []
282 282 return new_comp or new_dead
283 283
284 284 def status(self,verbose=0):
285 285 """Print a status of all jobs currently being managed."""
286 286
287 287 self._update_status()
288 288 self._group_report(self.running,'Running')
289 289 self._group_report(self.completed,'Completed')
290 290 self._group_report(self.dead,'Dead')
291 291 # Also flush the report queues
292 292 self._comp_report[:] = []
293 293 self._dead_report[:] = []
294 294
295 295 def remove(self,num):
296 296 """Remove a finished (completed or dead) job."""
297 297
298 298 try:
299 299 job = self.all[num]
300 300 except KeyError:
301 301 error('Job #%s not found' % num)
302 302 else:
303 303 stat_code = job.stat_code
304 304 if stat_code == self._s_running:
305 305 error('Job #%s is still running, it can not be removed.' % num)
306 306 return
307 307 elif stat_code == self._s_completed:
308 308 self.completed.remove(job)
309 309 elif stat_code == self._s_dead:
310 310 self.dead.remove(job)
311 311
312 312 def flush(self):
313 313 """Flush all finished jobs (completed and dead) from lists.
314 314
315 315 Running jobs are never flushed.
316 316
317 317 It first calls _status_new(), to update info. If any jobs have
318 318 completed since the last _status_new() call, the flush operation
319 319 aborts."""
320 320
321 321 # Remove the finished jobs from the master dict
322 322 alljobs = self.all
323 323 for job in self.completed+self.dead:
324 324 del(alljobs[job.num])
325 325
326 326 # Now flush these lists completely
327 327 fl_comp = self._group_flush(self.completed, 'Completed')
328 328 fl_dead = self._group_flush(self.dead, 'Dead')
329 329 if not (fl_comp or fl_dead):
330 330 print('No jobs to flush.')
331 331
332 332 def result(self,num):
333 333 """result(N) -> return the result of job N."""
334 334 try:
335 335 return self.all[num].result
336 336 except KeyError:
337 337 error('Job #%s not found' % num)
338 338
339 339 def _traceback(self, job):
340 340 num = job if isinstance(job, int) else job.num
341 341 try:
342 342 self.all[num].traceback()
343 343 except KeyError:
344 344 error('Job #%s not found' % num)
345 345
346 346 def traceback(self, job=None):
347 347 if job is None:
348 348 self._update_status()
349 349 for deadjob in self.dead:
350 350 print("Traceback for: %r" % deadjob)
351 351 self._traceback(deadjob)
352 352 print()
353 353 else:
354 354 self._traceback(job)
355 355
356 356
357 357 class BackgroundJobBase(threading.Thread):
358 358 """Base class to build BackgroundJob classes.
359 359
360 360 The derived classes must implement:
361 361
362 362 - Their own __init__, since the one here raises NotImplementedError. The
363 363 derived constructor must call self._init() at the end, to provide common
364 364 initialization.
365 365
366 366 - A strform attribute used in calls to __str__.
367 367
368 368 - A call() method, which will make the actual execution call and must
369 369 return a value to be held in the 'result' field of the job object.
370 370 """
371 371
372 372 # Class constants for status, in string and as numerical codes (when
373 373 # updating jobs lists, we don't want to do string comparisons). This will
374 374 # be done at every user prompt, so it has to be as fast as possible
375 375 stat_created = 'Created'; stat_created_c = 0
376 376 stat_running = 'Running'; stat_running_c = 1
377 377 stat_completed = 'Completed'; stat_completed_c = 2
378 378 stat_dead = 'Dead (Exception), call jobs.traceback() for details'
379 379 stat_dead_c = -1
380 380
381 381 def __init__(self):
382 382 """Must be implemented in subclasses.
383 383
384 384 Subclasses must call :meth:`_init` for standard initialisation.
385 385 """
386 386 raise NotImplementedError("This class can not be instantiated directly.")
387 387
388 388 def _init(self):
389 389 """Common initialization for all BackgroundJob objects"""
390 390
391 391 for attr in ['call','strform']:
392 392 assert hasattr(self,attr), "Missing attribute <%s>" % attr
393 393
394 394 # The num tag can be set by an external job manager
395 395 self.num = None
396 396
397 397 self.status = BackgroundJobBase.stat_created
398 398 self.stat_code = BackgroundJobBase.stat_created_c
399 399 self.finished = False
400 400 self.result = '<BackgroundJob has not completed>'
401 401
402 402 # reuse the ipython traceback handler if we can get to it, otherwise
403 403 # make a new one
404 404 try:
405 405 make_tb = get_ipython().InteractiveTB.text
406 406 except:
407 407 make_tb = AutoFormattedTB(mode = 'Context',
408 408 color_scheme='NoColor',
409 409 tb_offset = 1).text
410 410 # Note that the actual API for text() requires the three args to be
411 411 # passed in, so we wrap it in a simple lambda.
412 412 self._make_tb = lambda : make_tb(None, None, None)
413 413
414 414 # Hold a formatted traceback if one is generated.
415 415 self._tb = None
416 416
417 417 threading.Thread.__init__(self)
418 418
419 419 def __str__(self):
420 420 return self.strform
421 421
422 422 def __repr__(self):
423 423 return '<BackgroundJob #%d: %s>' % (self.num, self.strform)
424 424
425 425 def traceback(self):
426 426 print(self._tb)
427 427
428 428 def run(self):
429 429 try:
430 430 self.status = BackgroundJobBase.stat_running
431 431 self.stat_code = BackgroundJobBase.stat_running_c
432 432 self.result = self.call()
433 433 except:
434 434 self.status = BackgroundJobBase.stat_dead
435 435 self.stat_code = BackgroundJobBase.stat_dead_c
436 436 self.finished = None
437 437 self.result = ('<BackgroundJob died, call jobs.traceback() for details>')
438 438 self._tb = self._make_tb()
439 439 else:
440 440 self.status = BackgroundJobBase.stat_completed
441 441 self.stat_code = BackgroundJobBase.stat_completed_c
442 442 self.finished = True
443 443
444 444
445 445 class BackgroundJobExpr(BackgroundJobBase):
446 446 """Evaluate an expression as a background job (uses a separate thread)."""
447 447
448 448 def __init__(self, expression, glob=None, loc=None):
449 449 """Create a new job from a string which can be fed to eval().
450 450
451 451 global/locals dicts can be provided, which will be passed to the eval
452 452 call."""
453 453
454 454 # fail immediately if the given expression can't be compiled
455 455 self.code = compile(expression,'<BackgroundJob compilation>','eval')
456 456
457 457 glob = {} if glob is None else glob
458 458 loc = {} if loc is None else loc
459 459 self.expression = self.strform = expression
460 460 self.glob = glob
461 461 self.loc = loc
462 462 self._init()
463 463
464 464 def call(self):
465 465 return eval(self.code,self.glob,self.loc)
466 466
467 467
468 468 class BackgroundJobFunc(BackgroundJobBase):
469 469 """Run a function call as a background job (uses a separate thread)."""
470 470
471 471 def __init__(self, func, *args, **kwargs):
472 472 """Create a new job from a callable object.
473 473
474 474 Any positional arguments and keyword args given to this constructor
475 475 after the initial callable are passed directly to it."""
476 476
477 477 if not callable(func):
478 478 raise TypeError(
479 479 'first argument to BackgroundJobFunc must be callable')
480 480
481 481 self.func = func
482 482 self.args = args
483 483 self.kwargs = kwargs
484 484 # The string form will only include the function passed, because
485 485 # generating string representations of the arguments is a potentially
486 486 # _very_ expensive operation (e.g. with large arrays).
487 487 self.strform = str(func)
488 488 self._init()
489 489
490 490 def call(self):
491 491 return self.func(*self.args, **self.kwargs)
@@ -1,574 +1,574 b''
1 1 # coding: utf-8
2 2 """
3 3 Inputhook management for GUI event loop integration.
4 4 """
5 5
6 6 # Copyright (c) IPython Development Team.
7 7 # Distributed under the terms of the Modified BSD License.
8 8
9 9 try:
10 10 import ctypes
11 11 except ImportError:
12 12 ctypes = None
13 13 except SystemError: # IronPython issue, 2/8/2014
14 14 ctypes = None
15 15 import os
16 16 import platform
17 17 import sys
18 18 from distutils.version import LooseVersion as V
19 19
20 from IPython.utils.warn import warn
20 from warnings import warn
21 21
22 22 #-----------------------------------------------------------------------------
23 23 # Constants
24 24 #-----------------------------------------------------------------------------
25 25
26 26 # Constants for identifying the GUI toolkits.
27 27 GUI_WX = 'wx'
28 28 GUI_QT = 'qt'
29 29 GUI_QT4 = 'qt4'
30 30 GUI_GTK = 'gtk'
31 31 GUI_TK = 'tk'
32 32 GUI_OSX = 'osx'
33 33 GUI_GLUT = 'glut'
34 34 GUI_PYGLET = 'pyglet'
35 35 GUI_GTK3 = 'gtk3'
36 36 GUI_NONE = 'none' # i.e. disable
37 37
38 38 #-----------------------------------------------------------------------------
39 39 # Utilities
40 40 #-----------------------------------------------------------------------------
41 41
42 42 def _stdin_ready_posix():
43 43 """Return True if there's something to read on stdin (posix version)."""
44 44 infds, outfds, erfds = select.select([sys.stdin],[],[],0)
45 45 return bool(infds)
46 46
47 47 def _stdin_ready_nt():
48 48 """Return True if there's something to read on stdin (nt version)."""
49 49 return msvcrt.kbhit()
50 50
51 51 def _stdin_ready_other():
52 52 """Return True, assuming there's something to read on stdin."""
53 53 return True
54 54
55 55 def _use_appnope():
56 56 """Should we use appnope for dealing with OS X app nap?
57 57
58 58 Checks if we are on OS X 10.9 or greater.
59 59 """
60 60 return sys.platform == 'darwin' and V(platform.mac_ver()[0]) >= V('10.9')
61 61
62 62 def _ignore_CTRL_C_posix():
63 63 """Ignore CTRL+C (SIGINT)."""
64 64 signal.signal(signal.SIGINT, signal.SIG_IGN)
65 65
66 66 def _allow_CTRL_C_posix():
67 67 """Take CTRL+C into account (SIGINT)."""
68 68 signal.signal(signal.SIGINT, signal.default_int_handler)
69 69
70 70 def _ignore_CTRL_C_other():
71 71 """Ignore CTRL+C (not implemented)."""
72 72 pass
73 73
74 74 def _allow_CTRL_C_other():
75 75 """Take CTRL+C into account (not implemented)."""
76 76 pass
77 77
78 78 if os.name == 'posix':
79 79 import select
80 80 import signal
81 81 stdin_ready = _stdin_ready_posix
82 82 ignore_CTRL_C = _ignore_CTRL_C_posix
83 83 allow_CTRL_C = _allow_CTRL_C_posix
84 84 elif os.name == 'nt':
85 85 import msvcrt
86 86 stdin_ready = _stdin_ready_nt
87 87 ignore_CTRL_C = _ignore_CTRL_C_other
88 88 allow_CTRL_C = _allow_CTRL_C_other
89 89 else:
90 90 stdin_ready = _stdin_ready_other
91 91 ignore_CTRL_C = _ignore_CTRL_C_other
92 92 allow_CTRL_C = _allow_CTRL_C_other
93 93
94 94
95 95 #-----------------------------------------------------------------------------
96 96 # Main InputHookManager class
97 97 #-----------------------------------------------------------------------------
98 98
99 99
100 100 class InputHookManager(object):
101 101 """Manage PyOS_InputHook for different GUI toolkits.
102 102
103 103 This class installs various hooks under ``PyOSInputHook`` to handle
104 104 GUI event loop integration.
105 105 """
106 106
107 107 def __init__(self):
108 108 if ctypes is None:
109 109 warn("IPython GUI event loop requires ctypes, %gui will not be available")
110 110 else:
111 111 self.PYFUNC = ctypes.PYFUNCTYPE(ctypes.c_int)
112 112 self.guihooks = {}
113 113 self.aliases = {}
114 114 self.apps = {}
115 115 self._reset()
116 116
117 117 def _reset(self):
118 118 self._callback_pyfunctype = None
119 119 self._callback = None
120 120 self._installed = False
121 121 self._current_gui = None
122 122
123 123 def get_pyos_inputhook(self):
124 124 """Return the current PyOS_InputHook as a ctypes.c_void_p."""
125 125 return ctypes.c_void_p.in_dll(ctypes.pythonapi,"PyOS_InputHook")
126 126
127 127 def get_pyos_inputhook_as_func(self):
128 128 """Return the current PyOS_InputHook as a ctypes.PYFUNCYPE."""
129 129 return self.PYFUNC.in_dll(ctypes.pythonapi,"PyOS_InputHook")
130 130
131 131 def set_inputhook(self, callback):
132 132 """Set PyOS_InputHook to callback and return the previous one."""
133 133 # On platforms with 'readline' support, it's all too likely to
134 134 # have a KeyboardInterrupt signal delivered *even before* an
135 135 # initial ``try:`` clause in the callback can be executed, so
136 136 # we need to disable CTRL+C in this situation.
137 137 ignore_CTRL_C()
138 138 self._callback = callback
139 139 self._callback_pyfunctype = self.PYFUNC(callback)
140 140 pyos_inputhook_ptr = self.get_pyos_inputhook()
141 141 original = self.get_pyos_inputhook_as_func()
142 142 pyos_inputhook_ptr.value = \
143 143 ctypes.cast(self._callback_pyfunctype, ctypes.c_void_p).value
144 144 self._installed = True
145 145 return original
146 146
147 147 def clear_inputhook(self, app=None):
148 148 """Set PyOS_InputHook to NULL and return the previous one.
149 149
150 150 Parameters
151 151 ----------
152 152 app : optional, ignored
153 153 This parameter is allowed only so that clear_inputhook() can be
154 154 called with a similar interface as all the ``enable_*`` methods. But
155 155 the actual value of the parameter is ignored. This uniform interface
156 156 makes it easier to have user-level entry points in the main IPython
157 157 app like :meth:`enable_gui`."""
158 158 pyos_inputhook_ptr = self.get_pyos_inputhook()
159 159 original = self.get_pyos_inputhook_as_func()
160 160 pyos_inputhook_ptr.value = ctypes.c_void_p(None).value
161 161 allow_CTRL_C()
162 162 self._reset()
163 163 return original
164 164
165 165 def clear_app_refs(self, gui=None):
166 166 """Clear IPython's internal reference to an application instance.
167 167
168 168 Whenever we create an app for a user on qt4 or wx, we hold a
169 169 reference to the app. This is needed because in some cases bad things
170 170 can happen if a user doesn't hold a reference themselves. This
171 171 method is provided to clear the references we are holding.
172 172
173 173 Parameters
174 174 ----------
175 175 gui : None or str
176 176 If None, clear all app references. If ('wx', 'qt4') clear
177 177 the app for that toolkit. References are not held for gtk or tk
178 178 as those toolkits don't have the notion of an app.
179 179 """
180 180 if gui is None:
181 181 self.apps = {}
182 182 elif gui in self.apps:
183 183 del self.apps[gui]
184 184
185 185 def register(self, toolkitname, *aliases):
186 186 """Register a class to provide the event loop for a given GUI.
187 187
188 188 This is intended to be used as a class decorator. It should be passed
189 189 the names with which to register this GUI integration. The classes
190 190 themselves should subclass :class:`InputHookBase`.
191 191
192 192 ::
193 193
194 194 @inputhook_manager.register('qt')
195 195 class QtInputHook(InputHookBase):
196 196 def enable(self, app=None):
197 197 ...
198 198 """
199 199 def decorator(cls):
200 200 if ctypes is not None:
201 201 inst = cls(self)
202 202 self.guihooks[toolkitname] = inst
203 203 for a in aliases:
204 204 self.aliases[a] = toolkitname
205 205 return cls
206 206 return decorator
207 207
208 208 def current_gui(self):
209 209 """Return a string indicating the currently active GUI or None."""
210 210 return self._current_gui
211 211
212 212 def enable_gui(self, gui=None, app=None):
213 213 """Switch amongst GUI input hooks by name.
214 214
215 215 This is a higher level method than :meth:`set_inputhook` - it uses the
216 216 GUI name to look up a registered object which enables the input hook
217 217 for that GUI.
218 218
219 219 Parameters
220 220 ----------
221 221 gui : optional, string or None
222 222 If None (or 'none'), clears input hook, otherwise it must be one
223 223 of the recognized GUI names (see ``GUI_*`` constants in module).
224 224
225 225 app : optional, existing application object.
226 226 For toolkits that have the concept of a global app, you can supply an
227 227 existing one. If not given, the toolkit will be probed for one, and if
228 228 none is found, a new one will be created. Note that GTK does not have
229 229 this concept, and passing an app if ``gui=="GTK"`` will raise an error.
230 230
231 231 Returns
232 232 -------
233 233 The output of the underlying gui switch routine, typically the actual
234 234 PyOS_InputHook wrapper object or the GUI toolkit app created, if there was
235 235 one.
236 236 """
237 237 if gui in (None, GUI_NONE):
238 238 return self.disable_gui()
239 239
240 240 if gui in self.aliases:
241 241 return self.enable_gui(self.aliases[gui], app)
242 242
243 243 try:
244 244 gui_hook = self.guihooks[gui]
245 245 except KeyError:
246 246 e = "Invalid GUI request {!r}, valid ones are: {}"
247 247 raise ValueError(e.format(gui, ', '.join(self.guihooks)))
248 248 self._current_gui = gui
249 249
250 250 app = gui_hook.enable(app)
251 251 if app is not None:
252 252 app._in_event_loop = True
253 253 self.apps[gui] = app
254 254 return app
255 255
256 256 def disable_gui(self):
257 257 """Disable GUI event loop integration.
258 258
259 259 If an application was registered, this sets its ``_in_event_loop``
260 260 attribute to False. It then calls :meth:`clear_inputhook`.
261 261 """
262 262 gui = self._current_gui
263 263 if gui in self.apps:
264 264 self.apps[gui]._in_event_loop = False
265 265 return self.clear_inputhook()
266 266
267 267 class InputHookBase(object):
268 268 """Base class for input hooks for specific toolkits.
269 269
270 270 Subclasses should define an :meth:`enable` method with one argument, ``app``,
271 271 which will either be an instance of the toolkit's application class, or None.
272 272 They may also define a :meth:`disable` method with no arguments.
273 273 """
274 274 def __init__(self, manager):
275 275 self.manager = manager
276 276
277 277 def disable(self):
278 278 pass
279 279
280 280 inputhook_manager = InputHookManager()
281 281
282 282 @inputhook_manager.register('osx')
283 283 class NullInputHook(InputHookBase):
284 284 """A null inputhook that doesn't need to do anything"""
285 285 def enable(self, app=None):
286 286 pass
287 287
288 288 @inputhook_manager.register('wx')
289 289 class WxInputHook(InputHookBase):
290 290 def enable(self, app=None):
291 291 """Enable event loop integration with wxPython.
292 292
293 293 Parameters
294 294 ----------
295 295 app : WX Application, optional.
296 296 Running application to use. If not given, we probe WX for an
297 297 existing application object, and create a new one if none is found.
298 298
299 299 Notes
300 300 -----
301 301 This methods sets the ``PyOS_InputHook`` for wxPython, which allows
302 302 the wxPython to integrate with terminal based applications like
303 303 IPython.
304 304
305 305 If ``app`` is not given we probe for an existing one, and return it if
306 306 found. If no existing app is found, we create an :class:`wx.App` as
307 307 follows::
308 308
309 309 import wx
310 310 app = wx.App(redirect=False, clearSigInt=False)
311 311 """
312 312 import wx
313 313
314 314 wx_version = V(wx.__version__).version
315 315
316 316 if wx_version < [2, 8]:
317 317 raise ValueError("requires wxPython >= 2.8, but you have %s" % wx.__version__)
318 318
319 319 from IPython.lib.inputhookwx import inputhook_wx
320 320 self.manager.set_inputhook(inputhook_wx)
321 321 if _use_appnope():
322 322 from appnope import nope
323 323 nope()
324 324
325 325 import wx
326 326 if app is None:
327 327 app = wx.GetApp()
328 328 if app is None:
329 329 app = wx.App(redirect=False, clearSigInt=False)
330 330
331 331 return app
332 332
333 333 def disable(self):
334 334 """Disable event loop integration with wxPython.
335 335
336 336 This restores appnapp on OS X
337 337 """
338 338 if _use_appnope():
339 339 from appnope import nap
340 340 nap()
341 341
342 342 @inputhook_manager.register('qt', 'qt4')
343 343 class Qt4InputHook(InputHookBase):
344 344 def enable(self, app=None):
345 345 """Enable event loop integration with PyQt4.
346 346
347 347 Parameters
348 348 ----------
349 349 app : Qt Application, optional.
350 350 Running application to use. If not given, we probe Qt for an
351 351 existing application object, and create a new one if none is found.
352 352
353 353 Notes
354 354 -----
355 355 This methods sets the PyOS_InputHook for PyQt4, which allows
356 356 the PyQt4 to integrate with terminal based applications like
357 357 IPython.
358 358
359 359 If ``app`` is not given we probe for an existing one, and return it if
360 360 found. If no existing app is found, we create an :class:`QApplication`
361 361 as follows::
362 362
363 363 from PyQt4 import QtCore
364 364 app = QtGui.QApplication(sys.argv)
365 365 """
366 366 from IPython.lib.inputhookqt4 import create_inputhook_qt4
367 367 app, inputhook_qt4 = create_inputhook_qt4(self.manager, app)
368 368 self.manager.set_inputhook(inputhook_qt4)
369 369 if _use_appnope():
370 370 from appnope import nope
371 371 nope()
372 372
373 373 return app
374 374
375 375 def disable_qt4(self):
376 376 """Disable event loop integration with PyQt4.
377 377
378 378 This restores appnapp on OS X
379 379 """
380 380 if _use_appnope():
381 381 from appnope import nap
382 382 nap()
383 383
384 384
385 385 @inputhook_manager.register('qt5')
386 386 class Qt5InputHook(Qt4InputHook):
387 387 def enable(self, app=None):
388 388 os.environ['QT_API'] = 'pyqt5'
389 389 return Qt4InputHook.enable(self, app)
390 390
391 391
392 392 @inputhook_manager.register('gtk')
393 393 class GtkInputHook(InputHookBase):
394 394 def enable(self, app=None):
395 395 """Enable event loop integration with PyGTK.
396 396
397 397 Parameters
398 398 ----------
399 399 app : ignored
400 400 Ignored, it's only a placeholder to keep the call signature of all
401 401 gui activation methods consistent, which simplifies the logic of
402 402 supporting magics.
403 403
404 404 Notes
405 405 -----
406 406 This methods sets the PyOS_InputHook for PyGTK, which allows
407 407 the PyGTK to integrate with terminal based applications like
408 408 IPython.
409 409 """
410 410 import gtk
411 411 try:
412 412 gtk.set_interactive(True)
413 413 except AttributeError:
414 414 # For older versions of gtk, use our own ctypes version
415 415 from IPython.lib.inputhookgtk import inputhook_gtk
416 416 self.manager.set_inputhook(inputhook_gtk)
417 417
418 418
419 419 @inputhook_manager.register('tk')
420 420 class TkInputHook(InputHookBase):
421 421 def enable(self, app=None):
422 422 """Enable event loop integration with Tk.
423 423
424 424 Parameters
425 425 ----------
426 426 app : toplevel :class:`Tkinter.Tk` widget, optional.
427 427 Running toplevel widget to use. If not given, we probe Tk for an
428 428 existing one, and create a new one if none is found.
429 429
430 430 Notes
431 431 -----
432 432 If you have already created a :class:`Tkinter.Tk` object, the only
433 433 thing done by this method is to register with the
434 434 :class:`InputHookManager`, since creating that object automatically
435 435 sets ``PyOS_InputHook``.
436 436 """
437 437 if app is None:
438 438 try:
439 439 from tkinter import Tk # Py 3
440 440 except ImportError:
441 441 from Tkinter import Tk # Py 2
442 442 app = Tk()
443 443 app.withdraw()
444 444 self.manager.apps[GUI_TK] = app
445 445 return app
446 446
447 447
448 448 @inputhook_manager.register('glut')
449 449 class GlutInputHook(InputHookBase):
450 450 def enable(self, app=None):
451 451 """Enable event loop integration with GLUT.
452 452
453 453 Parameters
454 454 ----------
455 455
456 456 app : ignored
457 457 Ignored, it's only a placeholder to keep the call signature of all
458 458 gui activation methods consistent, which simplifies the logic of
459 459 supporting magics.
460 460
461 461 Notes
462 462 -----
463 463
464 464 This methods sets the PyOS_InputHook for GLUT, which allows the GLUT to
465 465 integrate with terminal based applications like IPython. Due to GLUT
466 466 limitations, it is currently not possible to start the event loop
467 467 without first creating a window. You should thus not create another
468 468 window but use instead the created one. See 'gui-glut.py' in the
469 469 docs/examples/lib directory.
470 470
471 471 The default screen mode is set to:
472 472 glut.GLUT_DOUBLE | glut.GLUT_RGBA | glut.GLUT_DEPTH
473 473 """
474 474
475 475 import OpenGL.GLUT as glut
476 476 from IPython.lib.inputhookglut import glut_display_mode, \
477 477 glut_close, glut_display, \
478 478 glut_idle, inputhook_glut
479 479
480 480 if GUI_GLUT not in self.manager.apps:
481 481 glut.glutInit( sys.argv )
482 482 glut.glutInitDisplayMode( glut_display_mode )
483 483 # This is specific to freeglut
484 484 if bool(glut.glutSetOption):
485 485 glut.glutSetOption( glut.GLUT_ACTION_ON_WINDOW_CLOSE,
486 486 glut.GLUT_ACTION_GLUTMAINLOOP_RETURNS )
487 487 glut.glutCreateWindow( sys.argv[0] )
488 488 glut.glutReshapeWindow( 1, 1 )
489 489 glut.glutHideWindow( )
490 490 glut.glutWMCloseFunc( glut_close )
491 491 glut.glutDisplayFunc( glut_display )
492 492 glut.glutIdleFunc( glut_idle )
493 493 else:
494 494 glut.glutWMCloseFunc( glut_close )
495 495 glut.glutDisplayFunc( glut_display )
496 496 glut.glutIdleFunc( glut_idle)
497 497 self.manager.set_inputhook( inputhook_glut )
498 498
499 499
500 500 def disable(self):
501 501 """Disable event loop integration with glut.
502 502
503 503 This sets PyOS_InputHook to NULL and set the display function to a
504 504 dummy one and set the timer to a dummy timer that will be triggered
505 505 very far in the future.
506 506 """
507 507 import OpenGL.GLUT as glut
508 508 from glut_support import glutMainLoopEvent
509 509
510 510 glut.glutHideWindow() # This is an event to be processed below
511 511 glutMainLoopEvent()
512 512 super(GlutInputHook, self).disable()
513 513
514 514 @inputhook_manager.register('pyglet')
515 515 class PygletInputHook(InputHookBase):
516 516 def enable(self, app=None):
517 517 """Enable event loop integration with pyglet.
518 518
519 519 Parameters
520 520 ----------
521 521 app : ignored
522 522 Ignored, it's only a placeholder to keep the call signature of all
523 523 gui activation methods consistent, which simplifies the logic of
524 524 supporting magics.
525 525
526 526 Notes
527 527 -----
528 528 This methods sets the ``PyOS_InputHook`` for pyglet, which allows
529 529 pyglet to integrate with terminal based applications like
530 530 IPython.
531 531
532 532 """
533 533 from IPython.lib.inputhookpyglet import inputhook_pyglet
534 534 self.manager.set_inputhook(inputhook_pyglet)
535 535 return app
536 536
537 537
538 538 @inputhook_manager.register('gtk3')
539 539 class Gtk3InputHook(InputHookBase):
540 540 def enable(self, app=None):
541 541 """Enable event loop integration with Gtk3 (gir bindings).
542 542
543 543 Parameters
544 544 ----------
545 545 app : ignored
546 546 Ignored, it's only a placeholder to keep the call signature of all
547 547 gui activation methods consistent, which simplifies the logic of
548 548 supporting magics.
549 549
550 550 Notes
551 551 -----
552 552 This methods sets the PyOS_InputHook for Gtk3, which allows
553 553 the Gtk3 to integrate with terminal based applications like
554 554 IPython.
555 555 """
556 556 from IPython.lib.inputhookgtk3 import inputhook_gtk3
557 557 self.manager.set_inputhook(inputhook_gtk3)
558 558
559 559
560 560 clear_inputhook = inputhook_manager.clear_inputhook
561 561 set_inputhook = inputhook_manager.set_inputhook
562 562 current_gui = inputhook_manager.current_gui
563 563 clear_app_refs = inputhook_manager.clear_app_refs
564 564 enable_gui = inputhook_manager.enable_gui
565 565 disable_gui = inputhook_manager.disable_gui
566 566 register = inputhook_manager.register
567 567 guis = inputhook_manager.guihooks
568 568
569 569
570 570 def _deprecated_disable():
571 571 warn("This function is deprecated: use disable_gui() instead")
572 572 inputhook_manager.disable_gui()
573 573 disable_wx = disable_qt4 = disable_gtk = disable_gtk3 = disable_glut = \
574 574 disable_pyglet = disable_osx = _deprecated_disable
@@ -1,807 +1,808 b''
1 1 # -*- coding: utf-8 -*-
2 2 """Subclass of InteractiveShell for terminal based frontends."""
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 __future__ import print_function
8 8
9 9 import bdb
10 10 import os
11 11 import sys
12 12
13 13 from IPython.core.error import TryNext, UsageError
14 14 from IPython.core.usage import interactive_usage
15 15 from IPython.core.inputsplitter import IPythonInputSplitter, ESC_MAGIC
16 16 from IPython.core.interactiveshell import InteractiveShell, InteractiveShellABC
17 17 from IPython.core.magic import Magics, magics_class, line_magic
18 18 from IPython.lib.clipboard import ClipboardEmpty
19 19 from IPython.utils.contexts import NoOpContext
20 20 from IPython.utils.decorators import undoc
21 21 from IPython.utils.encoding import get_stream_enc
22 22 from IPython.utils import py3compat
23 23 from IPython.utils.terminal import toggle_set_term_title, set_term_title
24 24 from IPython.utils.process import abbrev_cwd
25 from IPython.utils.warn import warn, error
25 from warnings import warn
26 from logging import error
26 27 from IPython.utils.text import num_ini_spaces, SList, strip_email_quotes
27 28 from traitlets import Integer, CBool, Unicode
28 29
29 30
30 31 def get_default_editor():
31 32 try:
32 33 ed = os.environ['EDITOR']
33 34 if not py3compat.PY3:
34 35 ed = ed.decode()
35 36 return ed
36 37 except KeyError:
37 38 pass
38 39 except UnicodeError:
39 40 warn("$EDITOR environment variable is not pure ASCII. Using platform "
40 41 "default editor.")
41 42
42 43 if os.name == 'posix':
43 44 return 'vi' # the only one guaranteed to be there!
44 45 else:
45 46 return 'notepad' # same in Windows!
46 47
47 48 def get_pasted_lines(sentinel, l_input=py3compat.input, quiet=False):
48 49 """ Yield pasted lines until the user enters the given sentinel value.
49 50 """
50 51 if not quiet:
51 52 print("Pasting code; enter '%s' alone on the line to stop or use Ctrl-D." \
52 53 % sentinel)
53 54 prompt = ":"
54 55 else:
55 56 prompt = ""
56 57 while True:
57 58 try:
58 59 l = py3compat.str_to_unicode(l_input(prompt))
59 60 if l == sentinel:
60 61 return
61 62 else:
62 63 yield l
63 64 except EOFError:
64 65 print('<EOF>')
65 66 return
66 67
67 68 @undoc
68 69 def no_op(*a, **kw): pass
69 70
70 71
71 72 class ReadlineNoRecord(object):
72 73 """Context manager to execute some code, then reload readline history
73 74 so that interactive input to the code doesn't appear when pressing up."""
74 75 def __init__(self, shell):
75 76 self.shell = shell
76 77 self._nested_level = 0
77 78
78 79 def __enter__(self):
79 80 if self._nested_level == 0:
80 81 try:
81 82 self.orig_length = self.current_length()
82 83 self.readline_tail = self.get_readline_tail()
83 84 except (AttributeError, IndexError): # Can fail with pyreadline
84 85 self.orig_length, self.readline_tail = 999999, []
85 86 self._nested_level += 1
86 87
87 88 def __exit__(self, type, value, traceback):
88 89 self._nested_level -= 1
89 90 if self._nested_level == 0:
90 91 # Try clipping the end if it's got longer
91 92 try:
92 93 e = self.current_length() - self.orig_length
93 94 if e > 0:
94 95 for _ in range(e):
95 96 self.shell.readline.remove_history_item(self.orig_length)
96 97
97 98 # If it still doesn't match, just reload readline history.
98 99 if self.current_length() != self.orig_length \
99 100 or self.get_readline_tail() != self.readline_tail:
100 101 self.shell.refill_readline_hist()
101 102 except (AttributeError, IndexError):
102 103 pass
103 104 # Returning False will cause exceptions to propagate
104 105 return False
105 106
106 107 def current_length(self):
107 108 return self.shell.readline.get_current_history_length()
108 109
109 110 def get_readline_tail(self, n=10):
110 111 """Get the last n items in readline history."""
111 112 end = self.shell.readline.get_current_history_length() + 1
112 113 start = max(end-n, 1)
113 114 ghi = self.shell.readline.get_history_item
114 115 return [ghi(x) for x in range(start, end)]
115 116
116 117
117 118 @magics_class
118 119 class TerminalMagics(Magics):
119 120 def __init__(self, shell):
120 121 super(TerminalMagics, self).__init__(shell)
121 122 self.input_splitter = IPythonInputSplitter()
122 123
123 124 def store_or_execute(self, block, name):
124 125 """ Execute a block, or store it in a variable, per the user's request.
125 126 """
126 127 if name:
127 128 # If storing it for further editing
128 129 self.shell.user_ns[name] = SList(block.splitlines())
129 130 print("Block assigned to '%s'" % name)
130 131 else:
131 132 b = self.preclean_input(block)
132 133 self.shell.user_ns['pasted_block'] = b
133 134 self.shell.using_paste_magics = True
134 135 try:
135 136 self.shell.run_cell(b)
136 137 finally:
137 138 self.shell.using_paste_magics = False
138 139
139 140 def preclean_input(self, block):
140 141 lines = block.splitlines()
141 142 while lines and not lines[0].strip():
142 143 lines = lines[1:]
143 144 return strip_email_quotes('\n'.join(lines))
144 145
145 146 def rerun_pasted(self, name='pasted_block'):
146 147 """ Rerun a previously pasted command.
147 148 """
148 149 b = self.shell.user_ns.get(name)
149 150
150 151 # Sanity checks
151 152 if b is None:
152 153 raise UsageError('No previous pasted block available')
153 154 if not isinstance(b, py3compat.string_types):
154 155 raise UsageError(
155 156 "Variable 'pasted_block' is not a string, can't execute")
156 157
157 158 print("Re-executing '%s...' (%d chars)"% (b.split('\n',1)[0], len(b)))
158 159 self.shell.run_cell(b)
159 160
160 161 @line_magic
161 162 def autoindent(self, parameter_s = ''):
162 163 """Toggle autoindent on/off (if available)."""
163 164
164 165 self.shell.set_autoindent()
165 166 print("Automatic indentation is:",['OFF','ON'][self.shell.autoindent])
166 167
167 168 @line_magic
168 169 def cpaste(self, parameter_s=''):
169 170 """Paste & execute a pre-formatted code block from clipboard.
170 171
171 172 You must terminate the block with '--' (two minus-signs) or Ctrl-D
172 173 alone on the line. You can also provide your own sentinel with '%paste
173 174 -s %%' ('%%' is the new sentinel for this operation).
174 175
175 176 The block is dedented prior to execution to enable execution of method
176 177 definitions. '>' and '+' characters at the beginning of a line are
177 178 ignored, to allow pasting directly from e-mails, diff files and
178 179 doctests (the '...' continuation prompt is also stripped). The
179 180 executed block is also assigned to variable named 'pasted_block' for
180 181 later editing with '%edit pasted_block'.
181 182
182 183 You can also pass a variable name as an argument, e.g. '%cpaste foo'.
183 184 This assigns the pasted block to variable 'foo' as string, without
184 185 dedenting or executing it (preceding >>> and + is still stripped)
185 186
186 187 '%cpaste -r' re-executes the block previously entered by cpaste.
187 188 '%cpaste -q' suppresses any additional output messages.
188 189
189 190 Do not be alarmed by garbled output on Windows (it's a readline bug).
190 191 Just press enter and type -- (and press enter again) and the block
191 192 will be what was just pasted.
192 193
193 194 IPython statements (magics, shell escapes) are not supported (yet).
194 195
195 196 See also
196 197 --------
197 198 paste: automatically pull code from clipboard.
198 199
199 200 Examples
200 201 --------
201 202 ::
202 203
203 204 In [8]: %cpaste
204 205 Pasting code; enter '--' alone on the line to stop.
205 206 :>>> a = ["world!", "Hello"]
206 207 :>>> print " ".join(sorted(a))
207 208 :--
208 209 Hello world!
209 210 """
210 211 opts, name = self.parse_options(parameter_s, 'rqs:', mode='string')
211 212 if 'r' in opts:
212 213 self.rerun_pasted()
213 214 return
214 215
215 216 quiet = ('q' in opts)
216 217
217 218 sentinel = opts.get('s', u'--')
218 219 block = '\n'.join(get_pasted_lines(sentinel, quiet=quiet))
219 220 self.store_or_execute(block, name)
220 221
221 222 @line_magic
222 223 def paste(self, parameter_s=''):
223 224 """Paste & execute a pre-formatted code block from clipboard.
224 225
225 226 The text is pulled directly from the clipboard without user
226 227 intervention and printed back on the screen before execution (unless
227 228 the -q flag is given to force quiet mode).
228 229
229 230 The block is dedented prior to execution to enable execution of method
230 231 definitions. '>' and '+' characters at the beginning of a line are
231 232 ignored, to allow pasting directly from e-mails, diff files and
232 233 doctests (the '...' continuation prompt is also stripped). The
233 234 executed block is also assigned to variable named 'pasted_block' for
234 235 later editing with '%edit pasted_block'.
235 236
236 237 You can also pass a variable name as an argument, e.g. '%paste foo'.
237 238 This assigns the pasted block to variable 'foo' as string, without
238 239 executing it (preceding >>> and + is still stripped).
239 240
240 241 Options:
241 242
242 243 -r: re-executes the block previously entered by cpaste.
243 244
244 245 -q: quiet mode: do not echo the pasted text back to the terminal.
245 246
246 247 IPython statements (magics, shell escapes) are not supported (yet).
247 248
248 249 See also
249 250 --------
250 251 cpaste: manually paste code into terminal until you mark its end.
251 252 """
252 253 opts, name = self.parse_options(parameter_s, 'rq', mode='string')
253 254 if 'r' in opts:
254 255 self.rerun_pasted()
255 256 return
256 257 try:
257 258 block = self.shell.hooks.clipboard_get()
258 259 except TryNext as clipboard_exc:
259 260 message = getattr(clipboard_exc, 'args')
260 261 if message:
261 262 error(message[0])
262 263 else:
263 264 error('Could not get text from the clipboard.')
264 265 return
265 266 except ClipboardEmpty:
266 267 raise UsageError("The clipboard appears to be empty")
267 268
268 269 # By default, echo back to terminal unless quiet mode is requested
269 270 if 'q' not in opts:
270 271 write = self.shell.write
271 272 write(self.shell.pycolorize(block))
272 273 if not block.endswith('\n'):
273 274 write('\n')
274 275 write("## -- End pasted text --\n")
275 276
276 277 self.store_or_execute(block, name)
277 278
278 279 # Class-level: add a '%cls' magic only on Windows
279 280 if sys.platform == 'win32':
280 281 @line_magic
281 282 def cls(self, s):
282 283 """Clear screen.
283 284 """
284 285 os.system("cls")
285 286
286 287
287 288 class TerminalInteractiveShell(InteractiveShell):
288 289
289 290 autoedit_syntax = CBool(False, config=True,
290 291 help="auto editing of files with syntax errors.")
291 292 confirm_exit = CBool(True, config=True,
292 293 help="""
293 294 Set to confirm when you try to exit IPython with an EOF (Control-D
294 295 in Unix, Control-Z/Enter in Windows). By typing 'exit' or 'quit',
295 296 you can force a direct exit without any confirmation.""",
296 297 )
297 298 # This display_banner only controls whether or not self.show_banner()
298 299 # is called when mainloop/interact are called. The default is False
299 300 # because for the terminal based application, the banner behavior
300 301 # is controlled by the application.
301 302 display_banner = CBool(False) # This isn't configurable!
302 303 embedded = CBool(False)
303 304 embedded_active = CBool(False)
304 305 editor = Unicode(get_default_editor(), config=True,
305 306 help="Set the editor used by IPython (default to $EDITOR/vi/notepad)."
306 307 )
307 308 pager = Unicode('less', config=True,
308 309 help="The shell program to be used for paging.")
309 310
310 311 screen_length = Integer(0, config=True,
311 312 help=
312 313 """Number of lines of your screen, used to control printing of very
313 314 long strings. Strings longer than this number of lines will be sent
314 315 through a pager instead of directly printed. The default value for
315 316 this is 0, which means IPython will auto-detect your screen size every
316 317 time it needs to print certain potentially long strings (this doesn't
317 318 change the behavior of the 'print' keyword, it's only triggered
318 319 internally). If for some reason this isn't working well (it needs
319 320 curses support), specify it yourself. Otherwise don't change the
320 321 default.""",
321 322 )
322 323 term_title = CBool(False, config=True,
323 324 help="Enable auto setting the terminal title."
324 325 )
325 326 usage = Unicode(interactive_usage)
326 327
327 328 # This `using_paste_magics` is used to detect whether the code is being
328 329 # executed via paste magics functions
329 330 using_paste_magics = CBool(False)
330 331
331 332 # In the terminal, GUI control is done via PyOS_InputHook
332 333 @staticmethod
333 334 def enable_gui(gui=None, app=None):
334 335 """Switch amongst GUI input hooks by name.
335 336 """
336 337 # Deferred import
337 338 from IPython.lib.inputhook import enable_gui as real_enable_gui
338 339 try:
339 340 return real_enable_gui(gui, app)
340 341 except ValueError as e:
341 342 raise UsageError("%s" % e)
342 343
343 344 system = InteractiveShell.system_raw
344 345
345 346 #-------------------------------------------------------------------------
346 347 # Overrides of init stages
347 348 #-------------------------------------------------------------------------
348 349
349 350 def init_display_formatter(self):
350 351 super(TerminalInteractiveShell, self).init_display_formatter()
351 352 # terminal only supports plaintext
352 353 self.display_formatter.active_types = ['text/plain']
353 354
354 355 #-------------------------------------------------------------------------
355 356 # Things related to readline
356 357 #-------------------------------------------------------------------------
357 358
358 359 def init_readline(self):
359 360 """Command history completion/saving/reloading."""
360 361
361 362 if self.readline_use:
362 363 import IPython.utils.rlineimpl as readline
363 364
364 365 self.rl_next_input = None
365 366 self.rl_do_indent = False
366 367
367 368 if not self.readline_use or not readline.have_readline:
368 369 self.readline = None
369 370 # Set a number of methods that depend on readline to be no-op
370 371 self.readline_no_record = NoOpContext()
371 372 self.set_readline_completer = no_op
372 373 self.set_custom_completer = no_op
373 374 if self.readline_use:
374 375 warn('Readline services not available or not loaded.')
375 376 else:
376 377 self.has_readline = True
377 378 self.readline = readline
378 379 sys.modules['readline'] = readline
379 380
380 381 # Platform-specific configuration
381 382 if os.name == 'nt':
382 383 # FIXME - check with Frederick to see if we can harmonize
383 384 # naming conventions with pyreadline to avoid this
384 385 # platform-dependent check
385 386 self.readline_startup_hook = readline.set_pre_input_hook
386 387 else:
387 388 self.readline_startup_hook = readline.set_startup_hook
388 389
389 390 # Readline config order:
390 391 # - IPython config (default value)
391 392 # - custom inputrc
392 393 # - IPython config (user customized)
393 394
394 395 # load IPython config before inputrc if default
395 396 # skip if libedit because parse_and_bind syntax is different
396 397 if not self._custom_readline_config and not readline.uses_libedit:
397 398 for rlcommand in self.readline_parse_and_bind:
398 399 readline.parse_and_bind(rlcommand)
399 400
400 401 # Load user's initrc file (readline config)
401 402 # Or if libedit is used, load editrc.
402 403 inputrc_name = os.environ.get('INPUTRC')
403 404 if inputrc_name is None:
404 405 inputrc_name = '.inputrc'
405 406 if readline.uses_libedit:
406 407 inputrc_name = '.editrc'
407 408 inputrc_name = os.path.join(self.home_dir, inputrc_name)
408 409 if os.path.isfile(inputrc_name):
409 410 try:
410 411 readline.read_init_file(inputrc_name)
411 412 except:
412 413 warn('Problems reading readline initialization file <%s>'
413 414 % inputrc_name)
414 415
415 416 # load IPython config after inputrc if user has customized
416 417 if self._custom_readline_config:
417 418 for rlcommand in self.readline_parse_and_bind:
418 419 readline.parse_and_bind(rlcommand)
419 420
420 421 # Remove some chars from the delimiters list. If we encounter
421 422 # unicode chars, discard them.
422 423 delims = readline.get_completer_delims()
423 424 if not py3compat.PY3:
424 425 delims = delims.encode("ascii", "ignore")
425 426 for d in self.readline_remove_delims:
426 427 delims = delims.replace(d, "")
427 428 delims = delims.replace(ESC_MAGIC, '')
428 429 readline.set_completer_delims(delims)
429 430 # Store these so we can restore them if something like rpy2 modifies
430 431 # them.
431 432 self.readline_delims = delims
432 433 # otherwise we end up with a monster history after a while:
433 434 readline.set_history_length(self.history_length)
434 435
435 436 self.refill_readline_hist()
436 437 self.readline_no_record = ReadlineNoRecord(self)
437 438
438 439 # Configure auto-indent for all platforms
439 440 self.set_autoindent(self.autoindent)
440 441
441 442 def init_completer(self):
442 443 super(TerminalInteractiveShell, self).init_completer()
443 444
444 445 # Only configure readline if we truly are using readline.
445 446 if self.has_readline:
446 447 self.set_readline_completer()
447 448
448 449 def set_readline_completer(self):
449 450 """Reset readline's completer to be our own."""
450 451 self.readline.set_completer(self.Completer.rlcomplete)
451 452
452 453
453 454 def pre_readline(self):
454 455 """readline hook to be used at the start of each line.
455 456
456 457 It handles auto-indent and text from set_next_input."""
457 458
458 459 if self.rl_do_indent:
459 460 self.readline.insert_text(self._indent_current_str())
460 461 if self.rl_next_input is not None:
461 462 self.readline.insert_text(self.rl_next_input)
462 463 self.rl_next_input = None
463 464
464 465 def refill_readline_hist(self):
465 466 # Load the last 1000 lines from history
466 467 self.readline.clear_history()
467 468 stdin_encoding = sys.stdin.encoding or "utf-8"
468 469 last_cell = u""
469 470 for _, _, cell in self.history_manager.get_tail(self.history_load_length,
470 471 include_latest=True):
471 472 # Ignore blank lines and consecutive duplicates
472 473 cell = cell.rstrip()
473 474 if cell and (cell != last_cell):
474 475 try:
475 476 if self.multiline_history:
476 477 self.readline.add_history(py3compat.unicode_to_str(cell,
477 478 stdin_encoding))
478 479 else:
479 480 for line in cell.splitlines():
480 481 self.readline.add_history(py3compat.unicode_to_str(line,
481 482 stdin_encoding))
482 483 last_cell = cell
483 484
484 485 except TypeError:
485 486 # The history DB can get corrupted so it returns strings
486 487 # containing null bytes, which readline objects to.
487 488 continue
488 489
489 490 #-------------------------------------------------------------------------
490 491 # Things related to the terminal
491 492 #-------------------------------------------------------------------------
492 493
493 494 @property
494 495 def usable_screen_length(self):
495 496 if self.screen_length == 0:
496 497 return 0
497 498 else:
498 499 num_lines_bot = self.separate_in.count('\n')+1
499 500 return self.screen_length - num_lines_bot
500 501
501 502 def _term_title_changed(self, name, new_value):
502 503 self.init_term_title()
503 504
504 505 def init_term_title(self):
505 506 # Enable or disable the terminal title.
506 507 if self.term_title:
507 508 toggle_set_term_title(True)
508 509 set_term_title('IPython: ' + abbrev_cwd())
509 510 else:
510 511 toggle_set_term_title(False)
511 512
512 513 #-------------------------------------------------------------------------
513 514 # Things related to aliases
514 515 #-------------------------------------------------------------------------
515 516
516 517 def init_alias(self):
517 518 # The parent class defines aliases that can be safely used with any
518 519 # frontend.
519 520 super(TerminalInteractiveShell, self).init_alias()
520 521
521 522 # Now define aliases that only make sense on the terminal, because they
522 523 # need direct access to the console in a way that we can't emulate in
523 524 # GUI or web frontend
524 525 if os.name == 'posix':
525 526 aliases = [('clear', 'clear'), ('more', 'more'), ('less', 'less'),
526 527 ('man', 'man')]
527 528 else :
528 529 aliases = []
529 530
530 531 for name, cmd in aliases:
531 532 self.alias_manager.soft_define_alias(name, cmd)
532 533
533 534 #-------------------------------------------------------------------------
534 535 # Mainloop and code execution logic
535 536 #-------------------------------------------------------------------------
536 537
537 538 def mainloop(self, display_banner=None):
538 539 """Start the mainloop.
539 540
540 541 If an optional banner argument is given, it will override the
541 542 internally created default banner.
542 543 """
543 544
544 545 with self.builtin_trap, self.display_trap:
545 546
546 547 while 1:
547 548 try:
548 549 self.interact(display_banner=display_banner)
549 550 #self.interact_with_readline()
550 551 # XXX for testing of a readline-decoupled repl loop, call
551 552 # interact_with_readline above
552 553 break
553 554 except KeyboardInterrupt:
554 555 # this should not be necessary, but KeyboardInterrupt
555 556 # handling seems rather unpredictable...
556 557 self.write("\nKeyboardInterrupt in interact()\n")
557 558
558 559 def _replace_rlhist_multiline(self, source_raw, hlen_before_cell):
559 560 """Store multiple lines as a single entry in history"""
560 561
561 562 # do nothing without readline or disabled multiline
562 563 if not self.has_readline or not self.multiline_history:
563 564 return hlen_before_cell
564 565
565 566 # windows rl has no remove_history_item
566 567 if not hasattr(self.readline, "remove_history_item"):
567 568 return hlen_before_cell
568 569
569 570 # skip empty cells
570 571 if not source_raw.rstrip():
571 572 return hlen_before_cell
572 573
573 574 # nothing changed do nothing, e.g. when rl removes consecutive dups
574 575 hlen = self.readline.get_current_history_length()
575 576 if hlen == hlen_before_cell:
576 577 return hlen_before_cell
577 578
578 579 for i in range(hlen - hlen_before_cell):
579 580 self.readline.remove_history_item(hlen - i - 1)
580 581 stdin_encoding = get_stream_enc(sys.stdin, 'utf-8')
581 582 self.readline.add_history(py3compat.unicode_to_str(source_raw.rstrip(),
582 583 stdin_encoding))
583 584 return self.readline.get_current_history_length()
584 585
585 586 def interact(self, display_banner=None):
586 587 """Closely emulate the interactive Python console."""
587 588
588 589 # batch run -> do not interact
589 590 if self.exit_now:
590 591 return
591 592
592 593 if display_banner is None:
593 594 display_banner = self.display_banner
594 595
595 596 if isinstance(display_banner, py3compat.string_types):
596 597 self.show_banner(display_banner)
597 598 elif display_banner:
598 599 self.show_banner()
599 600
600 601 more = False
601 602
602 603 if self.has_readline:
603 604 self.readline_startup_hook(self.pre_readline)
604 605 hlen_b4_cell = self.readline.get_current_history_length()
605 606 else:
606 607 hlen_b4_cell = 0
607 608 # exit_now is set by a call to %Exit or %Quit, through the
608 609 # ask_exit callback.
609 610
610 611 while not self.exit_now:
611 612 self.hooks.pre_prompt_hook()
612 613 if more:
613 614 try:
614 615 prompt = self.prompt_manager.render('in2')
615 616 except:
616 617 self.showtraceback()
617 618 if self.autoindent:
618 619 self.rl_do_indent = True
619 620
620 621 else:
621 622 try:
622 623 prompt = self.separate_in + self.prompt_manager.render('in')
623 624 except:
624 625 self.showtraceback()
625 626 try:
626 627 line = self.raw_input(prompt)
627 628 if self.exit_now:
628 629 # quick exit on sys.std[in|out] close
629 630 break
630 631 if self.autoindent:
631 632 self.rl_do_indent = False
632 633
633 634 except KeyboardInterrupt:
634 635 #double-guard against keyboardinterrupts during kbdint handling
635 636 try:
636 637 self.write('\n' + self.get_exception_only())
637 638 source_raw = self.input_splitter.raw_reset()
638 639 hlen_b4_cell = \
639 640 self._replace_rlhist_multiline(source_raw, hlen_b4_cell)
640 641 more = False
641 642 except KeyboardInterrupt:
642 643 pass
643 644 except EOFError:
644 645 if self.autoindent:
645 646 self.rl_do_indent = False
646 647 if self.has_readline:
647 648 self.readline_startup_hook(None)
648 649 self.write('\n')
649 650 self.exit()
650 651 except bdb.BdbQuit:
651 652 warn('The Python debugger has exited with a BdbQuit exception.\n'
652 653 'Because of how pdb handles the stack, it is impossible\n'
653 654 'for IPython to properly format this particular exception.\n'
654 655 'IPython will resume normal operation.')
655 656 except:
656 657 # exceptions here are VERY RARE, but they can be triggered
657 658 # asynchronously by signal handlers, for example.
658 659 self.showtraceback()
659 660 else:
660 661 try:
661 662 self.input_splitter.push(line)
662 663 more = self.input_splitter.push_accepts_more()
663 664 except SyntaxError:
664 665 # Run the code directly - run_cell takes care of displaying
665 666 # the exception.
666 667 more = False
667 668 if (self.SyntaxTB.last_syntax_error and
668 669 self.autoedit_syntax):
669 670 self.edit_syntax_error()
670 671 if not more:
671 672 source_raw = self.input_splitter.raw_reset()
672 673 self.run_cell(source_raw, store_history=True)
673 674 hlen_b4_cell = \
674 675 self._replace_rlhist_multiline(source_raw, hlen_b4_cell)
675 676
676 677 # Turn off the exit flag, so the mainloop can be restarted if desired
677 678 self.exit_now = False
678 679
679 680 def raw_input(self, prompt=''):
680 681 """Write a prompt and read a line.
681 682
682 683 The returned line does not include the trailing newline.
683 684 When the user enters the EOF key sequence, EOFError is raised.
684 685
685 686 Parameters
686 687 ----------
687 688
688 689 prompt : str, optional
689 690 A string to be printed to prompt the user.
690 691 """
691 692 # raw_input expects str, but we pass it unicode sometimes
692 693 prompt = py3compat.cast_bytes_py2(prompt)
693 694
694 695 try:
695 696 line = py3compat.cast_unicode_py2(self.raw_input_original(prompt))
696 697 except ValueError:
697 698 warn("\n********\nYou or a %run:ed script called sys.stdin.close()"
698 699 " or sys.stdout.close()!\nExiting IPython!\n")
699 700 self.ask_exit()
700 701 return ""
701 702
702 703 # Try to be reasonably smart about not re-indenting pasted input more
703 704 # than necessary. We do this by trimming out the auto-indent initial
704 705 # spaces, if the user's actual input started itself with whitespace.
705 706 if self.autoindent:
706 707 if num_ini_spaces(line) > self.indent_current_nsp:
707 708 line = line[self.indent_current_nsp:]
708 709 self.indent_current_nsp = 0
709 710
710 711 return line
711 712
712 713 #-------------------------------------------------------------------------
713 714 # Methods to support auto-editing of SyntaxErrors.
714 715 #-------------------------------------------------------------------------
715 716
716 717 def edit_syntax_error(self):
717 718 """The bottom half of the syntax error handler called in the main loop.
718 719
719 720 Loop until syntax error is fixed or user cancels.
720 721 """
721 722
722 723 while self.SyntaxTB.last_syntax_error:
723 724 # copy and clear last_syntax_error
724 725 err = self.SyntaxTB.clear_err_state()
725 726 if not self._should_recompile(err):
726 727 return
727 728 try:
728 729 # may set last_syntax_error again if a SyntaxError is raised
729 730 self.safe_execfile(err.filename,self.user_ns)
730 731 except:
731 732 self.showtraceback()
732 733 else:
733 734 try:
734 735 f = open(err.filename)
735 736 try:
736 737 # This should be inside a display_trap block and I
737 738 # think it is.
738 739 sys.displayhook(f.read())
739 740 finally:
740 741 f.close()
741 742 except:
742 743 self.showtraceback()
743 744
744 745 def _should_recompile(self,e):
745 746 """Utility routine for edit_syntax_error"""
746 747
747 748 if e.filename in ('<ipython console>','<input>','<string>',
748 749 '<console>','<BackgroundJob compilation>',
749 750 None):
750 751
751 752 return False
752 753 try:
753 754 if (self.autoedit_syntax and
754 755 not self.ask_yes_no('Return to editor to correct syntax error? '
755 756 '[Y/n] ','y')):
756 757 return False
757 758 except EOFError:
758 759 return False
759 760
760 761 def int0(x):
761 762 try:
762 763 return int(x)
763 764 except TypeError:
764 765 return 0
765 766 # always pass integer line and offset values to editor hook
766 767 try:
767 768 self.hooks.fix_error_editor(e.filename,
768 769 int0(e.lineno),int0(e.offset),e.msg)
769 770 except TryNext:
770 771 warn('Could not open editor')
771 772 return False
772 773 return True
773 774
774 775 #-------------------------------------------------------------------------
775 776 # Things related to exiting
776 777 #-------------------------------------------------------------------------
777 778
778 779 def ask_exit(self):
779 780 """ Ask the shell to exit. Can be overiden and used as a callback. """
780 781 self.exit_now = True
781 782
782 783 def exit(self):
783 784 """Handle interactive exit.
784 785
785 786 This method calls the ask_exit callback."""
786 787 if self.confirm_exit:
787 788 if self.ask_yes_no('Do you really want to exit ([y]/n)?','y','n'):
788 789 self.ask_exit()
789 790 else:
790 791 self.ask_exit()
791 792
792 793 #-------------------------------------------------------------------------
793 794 # Things related to magics
794 795 #-------------------------------------------------------------------------
795 796
796 797 def init_magics(self):
797 798 super(TerminalInteractiveShell, self).init_magics()
798 799 self.register_magics(TerminalMagics)
799 800
800 801 def showindentationerror(self):
801 802 super(TerminalInteractiveShell, self).showindentationerror()
802 803 if not self.using_paste_magics:
803 804 print("If you want to paste code into IPython, try the "
804 805 "%paste and %cpaste magic functions.")
805 806
806 807
807 808 InteractiveShellABC.register(TerminalInteractiveShell)
@@ -1,442 +1,441 b''
1 1 # -*- coding: utf-8 -*-
2 2 """IPython Test Suite Runner.
3 3
4 4 This module provides a main entry point to a user script to test IPython
5 5 itself from the command line. There are two ways of running this script:
6 6
7 7 1. With the syntax `iptest all`. This runs our entire test suite by
8 8 calling this script (with different arguments) recursively. This
9 9 causes modules and package to be tested in different processes, using nose
10 10 or trial where appropriate.
11 11 2. With the regular nose syntax, like `iptest -vvs IPython`. In this form
12 12 the script simply calls nose, but with special command line flags and
13 13 plugins loaded.
14 14
15 15 """
16 16
17 17 # Copyright (c) IPython Development Team.
18 18 # Distributed under the terms of the Modified BSD License.
19 19
20 20 from __future__ import print_function
21 21
22 22 import glob
23 23 from io import BytesIO
24 24 import os
25 25 import os.path as path
26 26 import sys
27 27 from threading import Thread, Lock, Event
28 28 import warnings
29 29
30 30 import nose.plugins.builtin
31 31 from nose.plugins.xunit import Xunit
32 32 from nose import SkipTest
33 33 from nose.core import TestProgram
34 34 from nose.plugins import Plugin
35 35 from nose.util import safe_str
36 36
37 37 from IPython import version_info
38 38 from IPython.utils.py3compat import bytes_to_str
39 39 from IPython.utils.importstring import import_item
40 40 from IPython.testing.plugin.ipdoctest import IPythonDoctest
41 41 from IPython.external.decorators import KnownFailure, knownfailureif
42 42
43 43 pjoin = path.join
44 44
45 45
46 46 # Enable printing all warnings raise by IPython's modules
47 47 warnings.filterwarnings('default', message='.*', category=Warning, module='IPy.*')
48 48
49 49
50 50 if version_info < (4,2):
51 51 # ignore some warnings from traitlets until 6.0
52 52 warnings.filterwarnings('ignore', message='.*on_trait_change is deprecated: use observe instead.*')
53 53 warnings.filterwarnings('ignore', message='.*was set from the constructor.*', category=Warning, module='IPython.*')
54 54 warnings.filterwarnings('ignore', message='.*use the instance .help string directly, like x.help.*', category=DeprecationWarning, module='IPython.*')
55 55 else :
56 56 warnings.warn('iptest has been filtering out for Traitlets warnings messages, for 2 minor versions (since 4.x), please consider updating to use new API')
57 57
58 58 if version_info < (6,):
59 59 # nose.tools renames all things from `camelCase` to `snake_case` which raise an
60 60 # warning with the runner they also import from standard import library. (as of Dec 2015)
61 61 # Ignore, let's revisit that in a couple of years for IPython 6.
62 62 warnings.filterwarnings('ignore', message='.*Please use assertEqual instead', category=Warning, module='IPython.*')
63 63
64 64
65 65 # ------------------------------------------------------------------------------
66 66 # Monkeypatch Xunit to count known failures as skipped.
67 67 # ------------------------------------------------------------------------------
68 68 def monkeypatch_xunit():
69 69 try:
70 70 knownfailureif(True)(lambda: None)()
71 71 except Exception as e:
72 72 KnownFailureTest = type(e)
73 73
74 74 def addError(self, test, err, capt=None):
75 75 if issubclass(err[0], KnownFailureTest):
76 76 err = (SkipTest,) + err[1:]
77 77 return self.orig_addError(test, err, capt)
78 78
79 79 Xunit.orig_addError = Xunit.addError
80 80 Xunit.addError = addError
81 81
82 82 #-----------------------------------------------------------------------------
83 83 # Check which dependencies are installed and greater than minimum version.
84 84 #-----------------------------------------------------------------------------
85 85 def extract_version(mod):
86 86 return mod.__version__
87 87
88 88 def test_for(item, min_version=None, callback=extract_version):
89 89 """Test to see if item is importable, and optionally check against a minimum
90 90 version.
91 91
92 92 If min_version is given, the default behavior is to check against the
93 93 `__version__` attribute of the item, but specifying `callback` allows you to
94 94 extract the value you are interested in. e.g::
95 95
96 96 In [1]: import sys
97 97
98 98 In [2]: from IPython.testing.iptest import test_for
99 99
100 100 In [3]: test_for('sys', (2,6), callback=lambda sys: sys.version_info)
101 101 Out[3]: True
102 102
103 103 """
104 104 try:
105 105 check = import_item(item)
106 106 except (ImportError, RuntimeError):
107 107 # GTK reports Runtime error if it can't be initialized even if it's
108 108 # importable.
109 109 return False
110 110 else:
111 111 if min_version:
112 112 if callback:
113 113 # extra processing step to get version to compare
114 114 check = callback(check)
115 115
116 116 return check >= min_version
117 117 else:
118 118 return True
119 119
120 120 # Global dict where we can store information on what we have and what we don't
121 121 # have available at test run time
122 122 have = {'matplotlib': test_for('matplotlib'),
123 123 'pygments': test_for('pygments'),
124 124 'sqlite3': test_for('sqlite3')}
125 125
126 126 #-----------------------------------------------------------------------------
127 127 # Test suite definitions
128 128 #-----------------------------------------------------------------------------
129 129
130 130 test_group_names = ['core',
131 131 'extensions', 'lib', 'terminal', 'testing', 'utils',
132 132 ]
133 133
134 134 class TestSection(object):
135 135 def __init__(self, name, includes):
136 136 self.name = name
137 137 self.includes = includes
138 138 self.excludes = []
139 139 self.dependencies = []
140 140 self.enabled = True
141 141
142 142 def exclude(self, module):
143 143 if not module.startswith('IPython'):
144 144 module = self.includes[0] + "." + module
145 145 self.excludes.append(module.replace('.', os.sep))
146 146
147 147 def requires(self, *packages):
148 148 self.dependencies.extend(packages)
149 149
150 150 @property
151 151 def will_run(self):
152 152 return self.enabled and all(have[p] for p in self.dependencies)
153 153
154 154 # Name -> (include, exclude, dependencies_met)
155 155 test_sections = {n:TestSection(n, ['IPython.%s' % n]) for n in test_group_names}
156 156
157 157
158 158 # Exclusions and dependencies
159 159 # ---------------------------
160 160
161 161 # core:
162 162 sec = test_sections['core']
163 163 if not have['sqlite3']:
164 164 sec.exclude('tests.test_history')
165 165 sec.exclude('history')
166 166 if not have['matplotlib']:
167 167 sec.exclude('pylabtools'),
168 168 sec.exclude('tests.test_pylabtools')
169 169
170 170 # lib:
171 171 sec = test_sections['lib']
172 172 sec.exclude('kernel')
173 173 if not have['pygments']:
174 174 sec.exclude('tests.test_lexers')
175 175 # We do this unconditionally, so that the test suite doesn't import
176 176 # gtk, changing the default encoding and masking some unicode bugs.
177 177 sec.exclude('inputhookgtk')
178 178 # We also do this unconditionally, because wx can interfere with Unix signals.
179 179 # There are currently no tests for it anyway.
180 180 sec.exclude('inputhookwx')
181 181 # Testing inputhook will need a lot of thought, to figure out
182 182 # how to have tests that don't lock up with the gui event
183 183 # loops in the picture
184 184 sec.exclude('inputhook')
185 185
186 186 # testing:
187 187 sec = test_sections['testing']
188 188 # These have to be skipped on win32 because they use echo, rm, cd, etc.
189 189 # See ticket https://github.com/ipython/ipython/issues/87
190 190 if sys.platform == 'win32':
191 191 sec.exclude('plugin.test_exampleip')
192 192 sec.exclude('plugin.dtexample')
193 193
194 194 # don't run jupyter_console tests found via shim
195 195 test_sections['terminal'].exclude('console')
196 196
197 197 # extensions:
198 198 sec = test_sections['extensions']
199 199 # This is deprecated in favour of rpy2
200 200 sec.exclude('rmagic')
201 201 # autoreload does some strange stuff, so move it to its own test section
202 202 sec.exclude('autoreload')
203 203 sec.exclude('tests.test_autoreload')
204 204 test_sections['autoreload'] = TestSection('autoreload',
205 205 ['IPython.extensions.autoreload', 'IPython.extensions.tests.test_autoreload'])
206 206 test_group_names.append('autoreload')
207 207
208 208
209 209 #-----------------------------------------------------------------------------
210 210 # Functions and classes
211 211 #-----------------------------------------------------------------------------
212 212
213 213 def check_exclusions_exist():
214 214 from IPython.paths import get_ipython_package_dir
215 from IPython.utils.warn import warn
215 from warnings import warn
216 216 parent = os.path.dirname(get_ipython_package_dir())
217 217 for sec in test_sections:
218 218 for pattern in sec.exclusions:
219 219 fullpath = pjoin(parent, pattern)
220 220 if not os.path.exists(fullpath) and not glob.glob(fullpath + '.*'):
221 221 warn("Excluding nonexistent file: %r" % pattern)
222 222
223 223
224 224 class ExclusionPlugin(Plugin):
225 225 """A nose plugin to effect our exclusions of files and directories.
226 226 """
227 227 name = 'exclusions'
228 228 score = 3000 # Should come before any other plugins
229 229
230 230 def __init__(self, exclude_patterns=None):
231 231 """
232 232 Parameters
233 233 ----------
234 234
235 235 exclude_patterns : sequence of strings, optional
236 236 Filenames containing these patterns (as raw strings, not as regular
237 237 expressions) are excluded from the tests.
238 238 """
239 239 self.exclude_patterns = exclude_patterns or []
240 240 super(ExclusionPlugin, self).__init__()
241 241
242 242 def options(self, parser, env=os.environ):
243 243 Plugin.options(self, parser, env)
244 244
245 245 def configure(self, options, config):
246 246 Plugin.configure(self, options, config)
247 247 # Override nose trying to disable plugin.
248 248 self.enabled = True
249 249
250 250 def wantFile(self, filename):
251 251 """Return whether the given filename should be scanned for tests.
252 252 """
253 253 if any(pat in filename for pat in self.exclude_patterns):
254 254 return False
255 255 return None
256 256
257 257 def wantDirectory(self, directory):
258 258 """Return whether the given directory should be scanned for tests.
259 259 """
260 260 if any(pat in directory for pat in self.exclude_patterns):
261 261 return False
262 262 return None
263 263
264 264
265 265 class StreamCapturer(Thread):
266 266 daemon = True # Don't hang if main thread crashes
267 267 started = False
268 268 def __init__(self, echo=False):
269 269 super(StreamCapturer, self).__init__()
270 270 self.echo = echo
271 271 self.streams = []
272 272 self.buffer = BytesIO()
273 273 self.readfd, self.writefd = os.pipe()
274 274 self.buffer_lock = Lock()
275 275 self.stop = Event()
276 276
277 277 def run(self):
278 278 self.started = True
279 279
280 280 while not self.stop.is_set():
281 281 chunk = os.read(self.readfd, 1024)
282 282
283 283 with self.buffer_lock:
284 284 self.buffer.write(chunk)
285 285 if self.echo:
286 286 sys.stdout.write(bytes_to_str(chunk))
287 287
288 288 os.close(self.readfd)
289 289 os.close(self.writefd)
290 290
291 291 def reset_buffer(self):
292 292 with self.buffer_lock:
293 293 self.buffer.truncate(0)
294 294 self.buffer.seek(0)
295 295
296 296 def get_buffer(self):
297 297 with self.buffer_lock:
298 298 return self.buffer.getvalue()
299 299
300 300 def ensure_started(self):
301 301 if not self.started:
302 302 self.start()
303 303
304 304 def halt(self):
305 305 """Safely stop the thread."""
306 306 if not self.started:
307 307 return
308 308
309 309 self.stop.set()
310 310 os.write(self.writefd, b'\0') # Ensure we're not locked in a read()
311 311 self.join()
312 312
313 313 class SubprocessStreamCapturePlugin(Plugin):
314 314 name='subprocstreams'
315 315 def __init__(self):
316 316 Plugin.__init__(self)
317 317 self.stream_capturer = StreamCapturer()
318 318 self.destination = os.environ.get('IPTEST_SUBPROC_STREAMS', 'capture')
319 319 # This is ugly, but distant parts of the test machinery need to be able
320 320 # to redirect streams, so we make the object globally accessible.
321 321 nose.iptest_stdstreams_fileno = self.get_write_fileno
322 322
323 323 def get_write_fileno(self):
324 324 if self.destination == 'capture':
325 325 self.stream_capturer.ensure_started()
326 326 return self.stream_capturer.writefd
327 327 elif self.destination == 'discard':
328 328 return os.open(os.devnull, os.O_WRONLY)
329 329 else:
330 330 return sys.__stdout__.fileno()
331 331
332 332 def configure(self, options, config):
333 333 Plugin.configure(self, options, config)
334 334 # Override nose trying to disable plugin.
335 335 if self.destination == 'capture':
336 336 self.enabled = True
337 337
338 338 def startTest(self, test):
339 339 # Reset log capture
340 340 self.stream_capturer.reset_buffer()
341 341
342 342 def formatFailure(self, test, err):
343 343 # Show output
344 344 ec, ev, tb = err
345 345 captured = self.stream_capturer.get_buffer().decode('utf-8', 'replace')
346 346 if captured.strip():
347 347 ev = safe_str(ev)
348 348 out = [ev, '>> begin captured subprocess output <<',
349 349 captured,
350 350 '>> end captured subprocess output <<']
351 351 return ec, '\n'.join(out), tb
352 352
353 353 return err
354 354
355 355 formatError = formatFailure
356 356
357 357 def finalize(self, result):
358 358 self.stream_capturer.halt()
359 359
360 360
361 361 def run_iptest():
362 362 """Run the IPython test suite using nose.
363 363
364 364 This function is called when this script is **not** called with the form
365 365 `iptest all`. It simply calls nose with appropriate command line flags
366 366 and accepts all of the standard nose arguments.
367 367 """
368 368 # Apply our monkeypatch to Xunit
369 369 if '--with-xunit' in sys.argv and not hasattr(Xunit, 'orig_addError'):
370 370 monkeypatch_xunit()
371 371
372 372 warnings.filterwarnings('ignore',
373 373 'This will be removed soon. Use IPython.testing.util instead')
374 374
375 375 arg1 = sys.argv[1]
376 376 if arg1 in test_sections:
377 377 section = test_sections[arg1]
378 378 sys.argv[1:2] = section.includes
379 379 elif arg1.startswith('IPython.') and arg1[8:] in test_sections:
380 380 section = test_sections[arg1[8:]]
381 381 sys.argv[1:2] = section.includes
382 382 else:
383 383 section = TestSection(arg1, includes=[arg1])
384 384
385 385
386 386 argv = sys.argv + [ '--detailed-errors', # extra info in tracebacks
387 387 # We add --exe because of setuptools' imbecility (it
388 388 # blindly does chmod +x on ALL files). Nose does the
389 389 # right thing and it tries to avoid executables,
390 390 # setuptools unfortunately forces our hand here. This
391 391 # has been discussed on the distutils list and the
392 392 # setuptools devs refuse to fix this problem!
393 393 '--exe',
394 394 ]
395 395 if '-a' not in argv and '-A' not in argv:
396 396 argv = argv + ['-a', '!crash']
397 397
398 398 if nose.__version__ >= '0.11':
399 399 # I don't fully understand why we need this one, but depending on what
400 400 # directory the test suite is run from, if we don't give it, 0 tests
401 401 # get run. Specifically, if the test suite is run from the source dir
402 402 # with an argument (like 'iptest.py IPython.core', 0 tests are run,
403 403 # even if the same call done in this directory works fine). It appears
404 404 # that if the requested package is in the current dir, nose bails early
405 405 # by default. Since it's otherwise harmless, leave it in by default
406 406 # for nose >= 0.11, though unfortunately nose 0.10 doesn't support it.
407 407 argv.append('--traverse-namespace')
408 408
409 409 plugins = [ ExclusionPlugin(section.excludes), KnownFailure(),
410 410 SubprocessStreamCapturePlugin() ]
411 411
412 412 # we still have some vestigial doctests in core
413 413 if (section.name.startswith(('core', 'IPython.core'))):
414 414 plugins.append(IPythonDoctest())
415 415 argv.extend([
416 416 '--with-ipdoctest',
417 417 '--ipdoctest-tests',
418 418 '--ipdoctest-extension=txt',
419 419 ])
420 420
421 421
422 422 # Use working directory set by parent process (see iptestcontroller)
423 423 if 'IPTEST_WORKING_DIR' in os.environ:
424 424 os.chdir(os.environ['IPTEST_WORKING_DIR'])
425 425
426 426 # We need a global ipython running in this process, but the special
427 427 # in-process group spawns its own IPython kernels, so for *that* group we
428 428 # must avoid also opening the global one (otherwise there's a conflict of
429 429 # singletons). Ultimately the solution to this problem is to refactor our
430 430 # assumptions about what needs to be a singleton and what doesn't (app
431 431 # objects should, individual shells shouldn't). But for now, this
432 432 # workaround allows the test suite for the inprocess module to complete.
433 433 if 'kernel.inprocess' not in section.name:
434 434 from IPython.testing import globalipapp
435 435 globalipapp.start_ipython()
436 436
437 437 # Now nose can run
438 438 TestProgram(argv=argv, addplugins=plugins)
439 439
440 440 if __name__ == '__main__':
441 441 run_iptest()
442
@@ -1,60 +1,60 b''
1 1 # encoding: utf-8
2 2 """
3 3 Utilities for warnings. Shoudn't we just use the built in warnings module.
4 4 """
5 5
6 6 # Copyright (c) IPython Development Team.
7 7 # Distributed under the terms of the Modified BSD License.
8 8
9 9 from __future__ import print_function
10 10
11 11 import sys
12 12
13 13 from IPython.utils import io
14 14
15 from warning import warn
15 from warnings import warn
16 16
17 17 warn("The module IPython.utils.warn is deprecated, use the standard warnings module instead", DeprecationWarning)
18 18
19 19 def warn(msg,level=2,exit_val=1):
20 20 """Standard warning printer. Gives formatting consistency.
21 21
22 22 Output is sent to io.stderr (sys.stderr by default).
23 23
24 24 Options:
25 25
26 26 -level(2): allows finer control:
27 27 0 -> Do nothing, dummy function.
28 28 1 -> Print message.
29 29 2 -> Print 'WARNING:' + message. (Default level).
30 30 3 -> Print 'ERROR:' + message.
31 31 4 -> Print 'FATAL ERROR:' + message and trigger a sys.exit(exit_val).
32 32
33 33 -exit_val (1): exit value returned by sys.exit() for a level 4
34 34 warning. Ignored for all other levels."""
35 35
36 36 warn("The module IPython.utils.warn is deprecated, use the standard warnings module instead", DeprecationWarning)
37 37 if level>0:
38 38 header = ['','','WARNING: ','ERROR: ','FATAL ERROR: ']
39 39 print(header[level], msg, sep='', file=io.stderr)
40 40 if level == 4:
41 41 print('Exiting.\n', file=io.stderr)
42 42 sys.exit(exit_val)
43 43
44 44
45 45 def info(msg):
46 46 """Equivalent to warn(msg,level=1)."""
47 47
48 48 warn(msg,level=1)
49 49
50 50
51 51 def error(msg):
52 52 """Equivalent to warn(msg,level=3)."""
53 53
54 54 warn(msg,level=3)
55 55
56 56
57 57 def fatal(msg,exit_val=1):
58 58 """Equivalent to warn(msg,exit_val=exit_val,level=4)."""
59 59
60 60 warn(msg,exit_val=exit_val,level=4)
General Comments 0
You need to be logged in to leave comments. Login now