##// END OF EJS Templates
Pass parent to child for configuration to propagate
Matthias Bussonnier -
Show More
@@ -1,3229 +1,3229 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """Main IPython class."""
2 """Main IPython class."""
3
3
4 #-----------------------------------------------------------------------------
4 #-----------------------------------------------------------------------------
5 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de>
5 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de>
6 # Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu>
6 # Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu>
7 # Copyright (C) 2008-2011 The IPython Development Team
7 # Copyright (C) 2008-2011 The IPython Development Team
8 #
8 #
9 # Distributed under the terms of the BSD License. The full license is in
9 # Distributed under the terms of the BSD License. The full license is in
10 # the file COPYING, distributed as part of this software.
10 # the file COPYING, distributed as part of this software.
11 #-----------------------------------------------------------------------------
11 #-----------------------------------------------------------------------------
12
12
13 from __future__ import absolute_import, print_function
13 from __future__ import absolute_import, print_function
14
14
15 import __future__
15 import __future__
16 import abc
16 import abc
17 import ast
17 import ast
18 import atexit
18 import atexit
19 import functools
19 import functools
20 import os
20 import os
21 import re
21 import re
22 import runpy
22 import runpy
23 import sys
23 import sys
24 import tempfile
24 import tempfile
25 import traceback
25 import traceback
26 import types
26 import types
27 import subprocess
27 import subprocess
28 import warnings
28 import warnings
29 from io import open as io_open
29 from io import open as io_open
30
30
31 from pickleshare import PickleShareDB
31 from pickleshare import PickleShareDB
32
32
33 from traitlets.config.configurable import SingletonConfigurable
33 from traitlets.config.configurable import SingletonConfigurable
34 from IPython.core import oinspect
34 from IPython.core import oinspect
35 from IPython.core import magic
35 from IPython.core import magic
36 from IPython.core import page
36 from IPython.core import page
37 from IPython.core import prefilter
37 from IPython.core import prefilter
38 from IPython.core import shadowns
38 from IPython.core import shadowns
39 from IPython.core import ultratb
39 from IPython.core import ultratb
40 from IPython.core.alias import Alias, AliasManager
40 from IPython.core.alias import Alias, AliasManager
41 from IPython.core.autocall import ExitAutocall
41 from IPython.core.autocall import ExitAutocall
42 from IPython.core.builtin_trap import BuiltinTrap
42 from IPython.core.builtin_trap import BuiltinTrap
43 from IPython.core.events import EventManager, available_events
43 from IPython.core.events import EventManager, available_events
44 from IPython.core.compilerop import CachingCompiler, check_linecache_ipython
44 from IPython.core.compilerop import CachingCompiler, check_linecache_ipython
45 from IPython.core.debugger import Pdb
45 from IPython.core.debugger import Pdb
46 from IPython.core.display_trap import DisplayTrap
46 from IPython.core.display_trap import DisplayTrap
47 from IPython.core.displayhook import DisplayHook
47 from IPython.core.displayhook import DisplayHook
48 from IPython.core.displaypub import DisplayPublisher
48 from IPython.core.displaypub import DisplayPublisher
49 from IPython.core.error import InputRejected, UsageError
49 from IPython.core.error import InputRejected, UsageError
50 from IPython.core.extensions import ExtensionManager
50 from IPython.core.extensions import ExtensionManager
51 from IPython.core.formatters import DisplayFormatter
51 from IPython.core.formatters import DisplayFormatter
52 from IPython.core.history import HistoryManager
52 from IPython.core.history import HistoryManager
53 from IPython.core.inputsplitter import ESC_MAGIC, ESC_MAGIC2
53 from IPython.core.inputsplitter import ESC_MAGIC, ESC_MAGIC2
54 from IPython.core.logger import Logger
54 from IPython.core.logger import Logger
55 from IPython.core.macro import Macro
55 from IPython.core.macro import Macro
56 from IPython.core.payload import PayloadManager
56 from IPython.core.payload import PayloadManager
57 from IPython.core.prefilter import PrefilterManager
57 from IPython.core.prefilter import PrefilterManager
58 from IPython.core.profiledir import ProfileDir
58 from IPython.core.profiledir import ProfileDir
59 from IPython.core.usage import default_banner
59 from IPython.core.usage import default_banner
60 from IPython.testing.skipdoctest import skip_doctest_py2, skip_doctest
60 from IPython.testing.skipdoctest import skip_doctest_py2, skip_doctest
61 from IPython.utils import PyColorize
61 from IPython.utils import PyColorize
62 from IPython.utils import io
62 from IPython.utils import io
63 from IPython.utils import py3compat
63 from IPython.utils import py3compat
64 from IPython.utils import openpy
64 from IPython.utils import openpy
65 from IPython.utils.decorators import undoc
65 from IPython.utils.decorators import undoc
66 from IPython.utils.io import ask_yes_no
66 from IPython.utils.io import ask_yes_no
67 from IPython.utils.ipstruct import Struct
67 from IPython.utils.ipstruct import Struct
68 from IPython.paths import get_ipython_dir
68 from IPython.paths import get_ipython_dir
69 from IPython.utils.path import get_home_dir, get_py_filename, ensure_dir_exists
69 from IPython.utils.path import get_home_dir, get_py_filename, ensure_dir_exists
70 from IPython.utils.process import system, getoutput
70 from IPython.utils.process import system, getoutput
71 from IPython.utils.py3compat import (builtin_mod, unicode_type, string_types,
71 from IPython.utils.py3compat import (builtin_mod, unicode_type, string_types,
72 with_metaclass, iteritems)
72 with_metaclass, iteritems)
73 from IPython.utils.strdispatch import StrDispatch
73 from IPython.utils.strdispatch import StrDispatch
74 from IPython.utils.syspathcontext import prepended_to_syspath
74 from IPython.utils.syspathcontext import prepended_to_syspath
75 from IPython.utils.text import format_screen, LSString, SList, DollarFormatter
75 from IPython.utils.text import format_screen, LSString, SList, DollarFormatter
76 from IPython.utils.tempdir import TemporaryDirectory
76 from IPython.utils.tempdir import TemporaryDirectory
77 from traitlets import (
77 from traitlets import (
78 Integer, Bool, CaselessStrEnum, Enum, List, Dict, Unicode, Instance, Type,
78 Integer, Bool, CaselessStrEnum, Enum, List, Dict, Unicode, Instance, Type,
79 observe, default,
79 observe, default,
80 )
80 )
81 from warnings import warn
81 from warnings import warn
82 from logging import error
82 from logging import error
83 import IPython.core.hooks
83 import IPython.core.hooks
84
84
85 # NoOpContext is deprecated, but ipykernel imports it from here.
85 # NoOpContext is deprecated, but ipykernel imports it from here.
86 # See https://github.com/ipython/ipykernel/issues/157
86 # See https://github.com/ipython/ipykernel/issues/157
87 from IPython.utils.contexts import NoOpContext
87 from IPython.utils.contexts import NoOpContext
88
88
89 try:
89 try:
90 import docrepr.sphinxify as sphx
90 import docrepr.sphinxify as sphx
91
91
92 def sphinxify(doc):
92 def sphinxify(doc):
93 with TemporaryDirectory() as dirname:
93 with TemporaryDirectory() as dirname:
94 return {
94 return {
95 'text/html': sphx.sphinxify(doc, dirname),
95 'text/html': sphx.sphinxify(doc, dirname),
96 'text/plain': doc
96 'text/plain': doc
97 }
97 }
98 except ImportError:
98 except ImportError:
99 sphinxify = None
99 sphinxify = None
100
100
101
101
102 class ProvisionalWarning(DeprecationWarning):
102 class ProvisionalWarning(DeprecationWarning):
103 """
103 """
104 Warning class for unstable features
104 Warning class for unstable features
105 """
105 """
106 pass
106 pass
107
107
108 #-----------------------------------------------------------------------------
108 #-----------------------------------------------------------------------------
109 # Globals
109 # Globals
110 #-----------------------------------------------------------------------------
110 #-----------------------------------------------------------------------------
111
111
112 # compiled regexps for autoindent management
112 # compiled regexps for autoindent management
113 dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass')
113 dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass')
114
114
115 #-----------------------------------------------------------------------------
115 #-----------------------------------------------------------------------------
116 # Utilities
116 # Utilities
117 #-----------------------------------------------------------------------------
117 #-----------------------------------------------------------------------------
118
118
119 @undoc
119 @undoc
120 def softspace(file, newvalue):
120 def softspace(file, newvalue):
121 """Copied from code.py, to remove the dependency"""
121 """Copied from code.py, to remove the dependency"""
122
122
123 oldvalue = 0
123 oldvalue = 0
124 try:
124 try:
125 oldvalue = file.softspace
125 oldvalue = file.softspace
126 except AttributeError:
126 except AttributeError:
127 pass
127 pass
128 try:
128 try:
129 file.softspace = newvalue
129 file.softspace = newvalue
130 except (AttributeError, TypeError):
130 except (AttributeError, TypeError):
131 # "attribute-less object" or "read-only attributes"
131 # "attribute-less object" or "read-only attributes"
132 pass
132 pass
133 return oldvalue
133 return oldvalue
134
134
135 @undoc
135 @undoc
136 def no_op(*a, **kw): pass
136 def no_op(*a, **kw): pass
137
137
138
138
139 class SpaceInInput(Exception): pass
139 class SpaceInInput(Exception): pass
140
140
141
141
142 def get_default_colors():
142 def get_default_colors():
143 "DEPRECATED"
143 "DEPRECATED"
144 warn('get_default_color is Deprecated, and is `Neutral` on all platforms.',
144 warn('get_default_color is Deprecated, and is `Neutral` on all platforms.',
145 DeprecationWarning, stacklevel=2)
145 DeprecationWarning, stacklevel=2)
146 return 'Neutral'
146 return 'Neutral'
147
147
148
148
149 class SeparateUnicode(Unicode):
149 class SeparateUnicode(Unicode):
150 r"""A Unicode subclass to validate separate_in, separate_out, etc.
150 r"""A Unicode subclass to validate separate_in, separate_out, etc.
151
151
152 This is a Unicode based trait that converts '0'->'' and ``'\\n'->'\n'``.
152 This is a Unicode based trait that converts '0'->'' and ``'\\n'->'\n'``.
153 """
153 """
154
154
155 def validate(self, obj, value):
155 def validate(self, obj, value):
156 if value == '0': value = ''
156 if value == '0': value = ''
157 value = value.replace('\\n','\n')
157 value = value.replace('\\n','\n')
158 return super(SeparateUnicode, self).validate(obj, value)
158 return super(SeparateUnicode, self).validate(obj, value)
159
159
160
160
161 @undoc
161 @undoc
162 class DummyMod(object):
162 class DummyMod(object):
163 """A dummy module used for IPython's interactive module when
163 """A dummy module used for IPython's interactive module when
164 a namespace must be assigned to the module's __dict__."""
164 a namespace must be assigned to the module's __dict__."""
165 pass
165 pass
166
166
167
167
168 class ExecutionResult(object):
168 class ExecutionResult(object):
169 """The result of a call to :meth:`InteractiveShell.run_cell`
169 """The result of a call to :meth:`InteractiveShell.run_cell`
170
170
171 Stores information about what took place.
171 Stores information about what took place.
172 """
172 """
173 execution_count = None
173 execution_count = None
174 error_before_exec = None
174 error_before_exec = None
175 error_in_exec = None
175 error_in_exec = None
176 result = None
176 result = None
177
177
178 @property
178 @property
179 def success(self):
179 def success(self):
180 return (self.error_before_exec is None) and (self.error_in_exec is None)
180 return (self.error_before_exec is None) and (self.error_in_exec is None)
181
181
182 def raise_error(self):
182 def raise_error(self):
183 """Reraises error if `success` is `False`, otherwise does nothing"""
183 """Reraises error if `success` is `False`, otherwise does nothing"""
184 if self.error_before_exec is not None:
184 if self.error_before_exec is not None:
185 raise self.error_before_exec
185 raise self.error_before_exec
186 if self.error_in_exec is not None:
186 if self.error_in_exec is not None:
187 raise self.error_in_exec
187 raise self.error_in_exec
188
188
189 def __repr__(self):
189 def __repr__(self):
190 if sys.version_info > (3,):
190 if sys.version_info > (3,):
191 name = self.__class__.__qualname__
191 name = self.__class__.__qualname__
192 else:
192 else:
193 name = self.__class__.__name__
193 name = self.__class__.__name__
194 return '<%s object at %x, execution_count=%s error_before_exec=%s error_in_exec=%s result=%s>' %\
194 return '<%s object at %x, execution_count=%s error_before_exec=%s error_in_exec=%s result=%s>' %\
195 (name, id(self), self.execution_count, self.error_before_exec, self.error_in_exec, repr(self.result))
195 (name, id(self), self.execution_count, self.error_before_exec, self.error_in_exec, repr(self.result))
196
196
197
197
198 class InteractiveShell(SingletonConfigurable):
198 class InteractiveShell(SingletonConfigurable):
199 """An enhanced, interactive shell for Python."""
199 """An enhanced, interactive shell for Python."""
200
200
201 _instance = None
201 _instance = None
202
202
203 ast_transformers = List([], help=
203 ast_transformers = List([], help=
204 """
204 """
205 A list of ast.NodeTransformer subclass instances, which will be applied
205 A list of ast.NodeTransformer subclass instances, which will be applied
206 to user input before code is run.
206 to user input before code is run.
207 """
207 """
208 ).tag(config=True)
208 ).tag(config=True)
209
209
210 autocall = Enum((0,1,2), default_value=0, help=
210 autocall = Enum((0,1,2), default_value=0, help=
211 """
211 """
212 Make IPython automatically call any callable object even if you didn't
212 Make IPython automatically call any callable object even if you didn't
213 type explicit parentheses. For example, 'str 43' becomes 'str(43)'
213 type explicit parentheses. For example, 'str 43' becomes 'str(43)'
214 automatically. The value can be '0' to disable the feature, '1' for
214 automatically. The value can be '0' to disable the feature, '1' for
215 'smart' autocall, where it is not applied if there are no more
215 'smart' autocall, where it is not applied if there are no more
216 arguments on the line, and '2' for 'full' autocall, where all callable
216 arguments on the line, and '2' for 'full' autocall, where all callable
217 objects are automatically called (even if no arguments are present).
217 objects are automatically called (even if no arguments are present).
218 """
218 """
219 ).tag(config=True)
219 ).tag(config=True)
220 # TODO: remove all autoindent logic and put into frontends.
220 # TODO: remove all autoindent logic and put into frontends.
221 # We can't do this yet because even runlines uses the autoindent.
221 # We can't do this yet because even runlines uses the autoindent.
222 autoindent = Bool(True, help=
222 autoindent = Bool(True, help=
223 """
223 """
224 Autoindent IPython code entered interactively.
224 Autoindent IPython code entered interactively.
225 """
225 """
226 ).tag(config=True)
226 ).tag(config=True)
227
227
228 automagic = Bool(True, help=
228 automagic = Bool(True, help=
229 """
229 """
230 Enable magic commands to be called without the leading %.
230 Enable magic commands to be called without the leading %.
231 """
231 """
232 ).tag(config=True)
232 ).tag(config=True)
233
233
234 banner1 = Unicode(default_banner,
234 banner1 = Unicode(default_banner,
235 help="""The part of the banner to be printed before the profile"""
235 help="""The part of the banner to be printed before the profile"""
236 ).tag(config=True)
236 ).tag(config=True)
237 banner2 = Unicode('',
237 banner2 = Unicode('',
238 help="""The part of the banner to be printed after the profile"""
238 help="""The part of the banner to be printed after the profile"""
239 ).tag(config=True)
239 ).tag(config=True)
240
240
241 cache_size = Integer(1000, help=
241 cache_size = Integer(1000, help=
242 """
242 """
243 Set the size of the output cache. The default is 1000, you can
243 Set the size of the output cache. The default is 1000, you can
244 change it permanently in your config file. Setting it to 0 completely
244 change it permanently in your config file. Setting it to 0 completely
245 disables the caching system, and the minimum value accepted is 20 (if
245 disables the caching system, and the minimum value accepted is 20 (if
246 you provide a value less than 20, it is reset to 0 and a warning is
246 you provide a value less than 20, it is reset to 0 and a warning is
247 issued). This limit is defined because otherwise you'll spend more
247 issued). This limit is defined because otherwise you'll spend more
248 time re-flushing a too small cache than working
248 time re-flushing a too small cache than working
249 """
249 """
250 ).tag(config=True)
250 ).tag(config=True)
251 color_info = Bool(True, help=
251 color_info = Bool(True, help=
252 """
252 """
253 Use colors for displaying information about objects. Because this
253 Use colors for displaying information about objects. Because this
254 information is passed through a pager (like 'less'), and some pagers
254 information is passed through a pager (like 'less'), and some pagers
255 get confused with color codes, this capability can be turned off.
255 get confused with color codes, this capability can be turned off.
256 """
256 """
257 ).tag(config=True)
257 ).tag(config=True)
258 colors = CaselessStrEnum(('Neutral', 'NoColor','LightBG','Linux'),
258 colors = CaselessStrEnum(('Neutral', 'NoColor','LightBG','Linux'),
259 default_value='Neutral',
259 default_value='Neutral',
260 help="Set the color scheme (NoColor, Neutral, Linux, or LightBG)."
260 help="Set the color scheme (NoColor, Neutral, Linux, or LightBG)."
261 ).tag(config=True)
261 ).tag(config=True)
262 debug = Bool(False).tag(config=True)
262 debug = Bool(False).tag(config=True)
263 disable_failing_post_execute = Bool(False,
263 disable_failing_post_execute = Bool(False,
264 help="Don't call post-execute functions that have failed in the past."
264 help="Don't call post-execute functions that have failed in the past."
265 ).tag(config=True)
265 ).tag(config=True)
266 display_formatter = Instance(DisplayFormatter, allow_none=True)
266 display_formatter = Instance(DisplayFormatter, allow_none=True)
267 displayhook_class = Type(DisplayHook)
267 displayhook_class = Type(DisplayHook)
268 display_pub_class = Type(DisplayPublisher)
268 display_pub_class = Type(DisplayPublisher)
269
269
270 sphinxify_docstring = Bool(False, help=
270 sphinxify_docstring = Bool(False, help=
271 """
271 """
272 Enables rich html representation of docstrings. (This requires the
272 Enables rich html representation of docstrings. (This requires the
273 docrepr module).
273 docrepr module).
274 """).tag(config=True)
274 """).tag(config=True)
275
275
276 @observe("sphinxify_docstring")
276 @observe("sphinxify_docstring")
277 def _sphinxify_docstring_changed(self, change):
277 def _sphinxify_docstring_changed(self, change):
278 if change['new']:
278 if change['new']:
279 warn("`sphinxify_docstring` is provisional since IPython 5.0 and might change in future versions." , ProvisionalWarning)
279 warn("`sphinxify_docstring` is provisional since IPython 5.0 and might change in future versions." , ProvisionalWarning)
280
280
281 enable_html_pager = Bool(False, help=
281 enable_html_pager = Bool(False, help=
282 """
282 """
283 (Provisional API) enables html representation in mime bundles sent
283 (Provisional API) enables html representation in mime bundles sent
284 to pagers.
284 to pagers.
285 """).tag(config=True)
285 """).tag(config=True)
286
286
287 @observe("enable_html_pager")
287 @observe("enable_html_pager")
288 def _enable_html_pager_changed(self, change):
288 def _enable_html_pager_changed(self, change):
289 if change['new']:
289 if change['new']:
290 warn("`enable_html_pager` is provisional since IPython 5.0 and might change in future versions.", ProvisionalWarning)
290 warn("`enable_html_pager` is provisional since IPython 5.0 and might change in future versions.", ProvisionalWarning)
291
291
292 data_pub_class = None
292 data_pub_class = None
293
293
294 exit_now = Bool(False)
294 exit_now = Bool(False)
295 exiter = Instance(ExitAutocall)
295 exiter = Instance(ExitAutocall)
296 @default('exiter')
296 @default('exiter')
297 def _exiter_default(self):
297 def _exiter_default(self):
298 return ExitAutocall(self)
298 return ExitAutocall(self)
299 # Monotonically increasing execution counter
299 # Monotonically increasing execution counter
300 execution_count = Integer(1)
300 execution_count = Integer(1)
301 filename = Unicode("<ipython console>")
301 filename = Unicode("<ipython console>")
302 ipython_dir= Unicode('').tag(config=True) # Set to get_ipython_dir() in __init__
302 ipython_dir= Unicode('').tag(config=True) # Set to get_ipython_dir() in __init__
303
303
304 # Input splitter, to transform input line by line and detect when a block
304 # Input splitter, to transform input line by line and detect when a block
305 # is ready to be executed.
305 # is ready to be executed.
306 input_splitter = Instance('IPython.core.inputsplitter.IPythonInputSplitter',
306 input_splitter = Instance('IPython.core.inputsplitter.IPythonInputSplitter',
307 (), {'line_input_checker': True})
307 (), {'line_input_checker': True})
308
308
309 # This InputSplitter instance is used to transform completed cells before
309 # This InputSplitter instance is used to transform completed cells before
310 # running them. It allows cell magics to contain blank lines.
310 # running them. It allows cell magics to contain blank lines.
311 input_transformer_manager = Instance('IPython.core.inputsplitter.IPythonInputSplitter',
311 input_transformer_manager = Instance('IPython.core.inputsplitter.IPythonInputSplitter',
312 (), {'line_input_checker': False})
312 (), {'line_input_checker': False})
313
313
314 logstart = Bool(False, help=
314 logstart = Bool(False, help=
315 """
315 """
316 Start logging to the default log file in overwrite mode.
316 Start logging to the default log file in overwrite mode.
317 Use `logappend` to specify a log file to **append** logs to.
317 Use `logappend` to specify a log file to **append** logs to.
318 """
318 """
319 ).tag(config=True)
319 ).tag(config=True)
320 logfile = Unicode('', help=
320 logfile = Unicode('', help=
321 """
321 """
322 The name of the logfile to use.
322 The name of the logfile to use.
323 """
323 """
324 ).tag(config=True)
324 ).tag(config=True)
325 logappend = Unicode('', help=
325 logappend = Unicode('', help=
326 """
326 """
327 Start logging to the given file in append mode.
327 Start logging to the given file in append mode.
328 Use `logfile` to specify a log file to **overwrite** logs to.
328 Use `logfile` to specify a log file to **overwrite** logs to.
329 """
329 """
330 ).tag(config=True)
330 ).tag(config=True)
331 object_info_string_level = Enum((0,1,2), default_value=0,
331 object_info_string_level = Enum((0,1,2), default_value=0,
332 ).tag(config=True)
332 ).tag(config=True)
333 pdb = Bool(False, help=
333 pdb = Bool(False, help=
334 """
334 """
335 Automatically call the pdb debugger after every exception.
335 Automatically call the pdb debugger after every exception.
336 """
336 """
337 ).tag(config=True)
337 ).tag(config=True)
338 display_page = Bool(False,
338 display_page = Bool(False,
339 help="""If True, anything that would be passed to the pager
339 help="""If True, anything that would be passed to the pager
340 will be displayed as regular output instead."""
340 will be displayed as regular output instead."""
341 ).tag(config=True)
341 ).tag(config=True)
342
342
343 # deprecated prompt traits:
343 # deprecated prompt traits:
344
344
345 prompt_in1 = Unicode('In [\\#]: ',
345 prompt_in1 = Unicode('In [\\#]: ',
346 help="Deprecated since IPython 4.0 and ignored since 5.0, set TerminalInteractiveShell.prompts object directly."
346 help="Deprecated since IPython 4.0 and ignored since 5.0, set TerminalInteractiveShell.prompts object directly."
347 ).tag(config=True)
347 ).tag(config=True)
348 prompt_in2 = Unicode(' .\\D.: ',
348 prompt_in2 = Unicode(' .\\D.: ',
349 help="Deprecated since IPython 4.0 and ignored since 5.0, set TerminalInteractiveShell.prompts object directly."
349 help="Deprecated since IPython 4.0 and ignored since 5.0, set TerminalInteractiveShell.prompts object directly."
350 ).tag(config=True)
350 ).tag(config=True)
351 prompt_out = Unicode('Out[\\#]: ',
351 prompt_out = Unicode('Out[\\#]: ',
352 help="Deprecated since IPython 4.0 and ignored since 5.0, set TerminalInteractiveShell.prompts object directly."
352 help="Deprecated since IPython 4.0 and ignored since 5.0, set TerminalInteractiveShell.prompts object directly."
353 ).tag(config=True)
353 ).tag(config=True)
354 prompts_pad_left = Bool(True,
354 prompts_pad_left = Bool(True,
355 help="Deprecated since IPython 4.0 and ignored since 5.0, set TerminalInteractiveShell.prompts object directly."
355 help="Deprecated since IPython 4.0 and ignored since 5.0, set TerminalInteractiveShell.prompts object directly."
356 ).tag(config=True)
356 ).tag(config=True)
357
357
358 @observe('prompt_in1', 'prompt_in2', 'prompt_out', 'prompt_pad_left')
358 @observe('prompt_in1', 'prompt_in2', 'prompt_out', 'prompt_pad_left')
359 def _prompt_trait_changed(self, change):
359 def _prompt_trait_changed(self, change):
360 name = change['name']
360 name = change['name']
361 warn("InteractiveShell.{name} is deprecated since IPython 4.0 and ignored since 5.0, set TerminalInteractiveShell.prompts object directly.".format(
361 warn("InteractiveShell.{name} is deprecated since IPython 4.0 and ignored since 5.0, set TerminalInteractiveShell.prompts object directly.".format(
362 name=name)
362 name=name)
363 )
363 )
364 # protect against weird cases where self.config may not exist:
364 # protect against weird cases where self.config may not exist:
365
365
366 show_rewritten_input = Bool(True,
366 show_rewritten_input = Bool(True,
367 help="Show rewritten input, e.g. for autocall."
367 help="Show rewritten input, e.g. for autocall."
368 ).tag(config=True)
368 ).tag(config=True)
369
369
370 quiet = Bool(False).tag(config=True)
370 quiet = Bool(False).tag(config=True)
371
371
372 history_length = Integer(10000,
372 history_length = Integer(10000,
373 help='Total length of command history'
373 help='Total length of command history'
374 ).tag(config=True)
374 ).tag(config=True)
375
375
376 history_load_length = Integer(1000, help=
376 history_load_length = Integer(1000, help=
377 """
377 """
378 The number of saved history entries to be loaded
378 The number of saved history entries to be loaded
379 into the history buffer at startup.
379 into the history buffer at startup.
380 """
380 """
381 ).tag(config=True)
381 ).tag(config=True)
382
382
383 ast_node_interactivity = Enum(['all', 'last', 'last_expr', 'none'],
383 ast_node_interactivity = Enum(['all', 'last', 'last_expr', 'none'],
384 default_value='last_expr',
384 default_value='last_expr',
385 help="""
385 help="""
386 'all', 'last', 'last_expr' or 'none', specifying which nodes should be
386 'all', 'last', 'last_expr' or 'none', specifying which nodes should be
387 run interactively (displaying output from expressions)."""
387 run interactively (displaying output from expressions)."""
388 ).tag(config=True)
388 ).tag(config=True)
389
389
390 # TODO: this part of prompt management should be moved to the frontends.
390 # TODO: this part of prompt management should be moved to the frontends.
391 # Use custom TraitTypes that convert '0'->'' and '\\n'->'\n'
391 # Use custom TraitTypes that convert '0'->'' and '\\n'->'\n'
392 separate_in = SeparateUnicode('\n').tag(config=True)
392 separate_in = SeparateUnicode('\n').tag(config=True)
393 separate_out = SeparateUnicode('').tag(config=True)
393 separate_out = SeparateUnicode('').tag(config=True)
394 separate_out2 = SeparateUnicode('').tag(config=True)
394 separate_out2 = SeparateUnicode('').tag(config=True)
395 wildcards_case_sensitive = Bool(True).tag(config=True)
395 wildcards_case_sensitive = Bool(True).tag(config=True)
396 xmode = CaselessStrEnum(('Context','Plain', 'Verbose'),
396 xmode = CaselessStrEnum(('Context','Plain', 'Verbose'),
397 default_value='Context').tag(config=True)
397 default_value='Context').tag(config=True)
398
398
399 # Subcomponents of InteractiveShell
399 # Subcomponents of InteractiveShell
400 alias_manager = Instance('IPython.core.alias.AliasManager', allow_none=True)
400 alias_manager = Instance('IPython.core.alias.AliasManager', allow_none=True)
401 prefilter_manager = Instance('IPython.core.prefilter.PrefilterManager', allow_none=True)
401 prefilter_manager = Instance('IPython.core.prefilter.PrefilterManager', allow_none=True)
402 builtin_trap = Instance('IPython.core.builtin_trap.BuiltinTrap', allow_none=True)
402 builtin_trap = Instance('IPython.core.builtin_trap.BuiltinTrap', allow_none=True)
403 display_trap = Instance('IPython.core.display_trap.DisplayTrap', allow_none=True)
403 display_trap = Instance('IPython.core.display_trap.DisplayTrap', allow_none=True)
404 extension_manager = Instance('IPython.core.extensions.ExtensionManager', allow_none=True)
404 extension_manager = Instance('IPython.core.extensions.ExtensionManager', allow_none=True)
405 payload_manager = Instance('IPython.core.payload.PayloadManager', allow_none=True)
405 payload_manager = Instance('IPython.core.payload.PayloadManager', allow_none=True)
406 history_manager = Instance('IPython.core.history.HistoryAccessorBase', allow_none=True)
406 history_manager = Instance('IPython.core.history.HistoryAccessorBase', allow_none=True)
407 magics_manager = Instance('IPython.core.magic.MagicsManager', allow_none=True)
407 magics_manager = Instance('IPython.core.magic.MagicsManager', allow_none=True)
408
408
409 profile_dir = Instance('IPython.core.application.ProfileDir', allow_none=True)
409 profile_dir = Instance('IPython.core.application.ProfileDir', allow_none=True)
410 @property
410 @property
411 def profile(self):
411 def profile(self):
412 if self.profile_dir is not None:
412 if self.profile_dir is not None:
413 name = os.path.basename(self.profile_dir.location)
413 name = os.path.basename(self.profile_dir.location)
414 return name.replace('profile_','')
414 return name.replace('profile_','')
415
415
416
416
417 # Private interface
417 # Private interface
418 _post_execute = Dict()
418 _post_execute = Dict()
419
419
420 # Tracks any GUI loop loaded for pylab
420 # Tracks any GUI loop loaded for pylab
421 pylab_gui_select = None
421 pylab_gui_select = None
422
422
423 last_execution_succeeded = Bool(True, help='Did last executed command succeeded')
423 last_execution_succeeded = Bool(True, help='Did last executed command succeeded')
424
424
425 def __init__(self, ipython_dir=None, profile_dir=None,
425 def __init__(self, ipython_dir=None, profile_dir=None,
426 user_module=None, user_ns=None,
426 user_module=None, user_ns=None,
427 custom_exceptions=((), None), **kwargs):
427 custom_exceptions=((), None), **kwargs):
428
428
429 # This is where traits with a config_key argument are updated
429 # This is where traits with a config_key argument are updated
430 # from the values on config.
430 # from the values on config.
431 super(InteractiveShell, self).__init__(**kwargs)
431 super(InteractiveShell, self).__init__(**kwargs)
432 if 'PromptManager' in self.config:
432 if 'PromptManager' in self.config:
433 warn('As of IPython 5.0 `PromptManager` config will have no effect'
433 warn('As of IPython 5.0 `PromptManager` config will have no effect'
434 ' and has been replaced by TerminalInteractiveShell.prompts_class')
434 ' and has been replaced by TerminalInteractiveShell.prompts_class')
435 self.configurables = [self]
435 self.configurables = [self]
436
436
437 # These are relatively independent and stateless
437 # These are relatively independent and stateless
438 self.init_ipython_dir(ipython_dir)
438 self.init_ipython_dir(ipython_dir)
439 self.init_profile_dir(profile_dir)
439 self.init_profile_dir(profile_dir)
440 self.init_instance_attrs()
440 self.init_instance_attrs()
441 self.init_environment()
441 self.init_environment()
442
442
443 # Check if we're in a virtualenv, and set up sys.path.
443 # Check if we're in a virtualenv, and set up sys.path.
444 self.init_virtualenv()
444 self.init_virtualenv()
445
445
446 # Create namespaces (user_ns, user_global_ns, etc.)
446 # Create namespaces (user_ns, user_global_ns, etc.)
447 self.init_create_namespaces(user_module, user_ns)
447 self.init_create_namespaces(user_module, user_ns)
448 # This has to be done after init_create_namespaces because it uses
448 # This has to be done after init_create_namespaces because it uses
449 # something in self.user_ns, but before init_sys_modules, which
449 # something in self.user_ns, but before init_sys_modules, which
450 # is the first thing to modify sys.
450 # is the first thing to modify sys.
451 # TODO: When we override sys.stdout and sys.stderr before this class
451 # TODO: When we override sys.stdout and sys.stderr before this class
452 # is created, we are saving the overridden ones here. Not sure if this
452 # is created, we are saving the overridden ones here. Not sure if this
453 # is what we want to do.
453 # is what we want to do.
454 self.save_sys_module_state()
454 self.save_sys_module_state()
455 self.init_sys_modules()
455 self.init_sys_modules()
456
456
457 # While we're trying to have each part of the code directly access what
457 # While we're trying to have each part of the code directly access what
458 # it needs without keeping redundant references to objects, we have too
458 # it needs without keeping redundant references to objects, we have too
459 # much legacy code that expects ip.db to exist.
459 # much legacy code that expects ip.db to exist.
460 self.db = PickleShareDB(os.path.join(self.profile_dir.location, 'db'))
460 self.db = PickleShareDB(os.path.join(self.profile_dir.location, 'db'))
461
461
462 self.init_history()
462 self.init_history()
463 self.init_encoding()
463 self.init_encoding()
464 self.init_prefilter()
464 self.init_prefilter()
465
465
466 self.init_syntax_highlighting()
466 self.init_syntax_highlighting()
467 self.init_hooks()
467 self.init_hooks()
468 self.init_events()
468 self.init_events()
469 self.init_pushd_popd_magic()
469 self.init_pushd_popd_magic()
470 self.init_user_ns()
470 self.init_user_ns()
471 self.init_logger()
471 self.init_logger()
472 self.init_builtins()
472 self.init_builtins()
473
473
474 # The following was in post_config_initialization
474 # The following was in post_config_initialization
475 self.init_inspector()
475 self.init_inspector()
476 self.raw_input_original = input
476 self.raw_input_original = input
477 self.init_completer()
477 self.init_completer()
478 # TODO: init_io() needs to happen before init_traceback handlers
478 # TODO: init_io() needs to happen before init_traceback handlers
479 # because the traceback handlers hardcode the stdout/stderr streams.
479 # because the traceback handlers hardcode the stdout/stderr streams.
480 # This logic in in debugger.Pdb and should eventually be changed.
480 # This logic in in debugger.Pdb and should eventually be changed.
481 self.init_io()
481 self.init_io()
482 self.init_traceback_handlers(custom_exceptions)
482 self.init_traceback_handlers(custom_exceptions)
483 self.init_prompts()
483 self.init_prompts()
484 self.init_display_formatter()
484 self.init_display_formatter()
485 self.init_display_pub()
485 self.init_display_pub()
486 self.init_data_pub()
486 self.init_data_pub()
487 self.init_displayhook()
487 self.init_displayhook()
488 self.init_magics()
488 self.init_magics()
489 self.init_alias()
489 self.init_alias()
490 self.init_logstart()
490 self.init_logstart()
491 self.init_pdb()
491 self.init_pdb()
492 self.init_extension_manager()
492 self.init_extension_manager()
493 self.init_payload()
493 self.init_payload()
494 self.init_deprecation_warnings()
494 self.init_deprecation_warnings()
495 self.hooks.late_startup_hook()
495 self.hooks.late_startup_hook()
496 self.events.trigger('shell_initialized', self)
496 self.events.trigger('shell_initialized', self)
497 atexit.register(self.atexit_operations)
497 atexit.register(self.atexit_operations)
498
498
499 def get_ipython(self):
499 def get_ipython(self):
500 """Return the currently running IPython instance."""
500 """Return the currently running IPython instance."""
501 return self
501 return self
502
502
503 #-------------------------------------------------------------------------
503 #-------------------------------------------------------------------------
504 # Trait changed handlers
504 # Trait changed handlers
505 #-------------------------------------------------------------------------
505 #-------------------------------------------------------------------------
506 @observe('ipython_dir')
506 @observe('ipython_dir')
507 def _ipython_dir_changed(self, change):
507 def _ipython_dir_changed(self, change):
508 ensure_dir_exists(change['new'])
508 ensure_dir_exists(change['new'])
509
509
510 def set_autoindent(self,value=None):
510 def set_autoindent(self,value=None):
511 """Set the autoindent flag.
511 """Set the autoindent flag.
512
512
513 If called with no arguments, it acts as a toggle."""
513 If called with no arguments, it acts as a toggle."""
514 if value is None:
514 if value is None:
515 self.autoindent = not self.autoindent
515 self.autoindent = not self.autoindent
516 else:
516 else:
517 self.autoindent = value
517 self.autoindent = value
518
518
519 #-------------------------------------------------------------------------
519 #-------------------------------------------------------------------------
520 # init_* methods called by __init__
520 # init_* methods called by __init__
521 #-------------------------------------------------------------------------
521 #-------------------------------------------------------------------------
522
522
523 def init_ipython_dir(self, ipython_dir):
523 def init_ipython_dir(self, ipython_dir):
524 if ipython_dir is not None:
524 if ipython_dir is not None:
525 self.ipython_dir = ipython_dir
525 self.ipython_dir = ipython_dir
526 return
526 return
527
527
528 self.ipython_dir = get_ipython_dir()
528 self.ipython_dir = get_ipython_dir()
529
529
530 def init_profile_dir(self, profile_dir):
530 def init_profile_dir(self, profile_dir):
531 if profile_dir is not None:
531 if profile_dir is not None:
532 self.profile_dir = profile_dir
532 self.profile_dir = profile_dir
533 return
533 return
534 self.profile_dir =\
534 self.profile_dir =\
535 ProfileDir.create_profile_dir_by_name(self.ipython_dir, 'default')
535 ProfileDir.create_profile_dir_by_name(self.ipython_dir, 'default')
536
536
537 def init_instance_attrs(self):
537 def init_instance_attrs(self):
538 self.more = False
538 self.more = False
539
539
540 # command compiler
540 # command compiler
541 self.compile = CachingCompiler()
541 self.compile = CachingCompiler()
542
542
543 # Make an empty namespace, which extension writers can rely on both
543 # Make an empty namespace, which extension writers can rely on both
544 # existing and NEVER being used by ipython itself. This gives them a
544 # existing and NEVER being used by ipython itself. This gives them a
545 # convenient location for storing additional information and state
545 # convenient location for storing additional information and state
546 # their extensions may require, without fear of collisions with other
546 # their extensions may require, without fear of collisions with other
547 # ipython names that may develop later.
547 # ipython names that may develop later.
548 self.meta = Struct()
548 self.meta = Struct()
549
549
550 # Temporary files used for various purposes. Deleted at exit.
550 # Temporary files used for various purposes. Deleted at exit.
551 self.tempfiles = []
551 self.tempfiles = []
552 self.tempdirs = []
552 self.tempdirs = []
553
553
554 # keep track of where we started running (mainly for crash post-mortem)
554 # keep track of where we started running (mainly for crash post-mortem)
555 # This is not being used anywhere currently.
555 # This is not being used anywhere currently.
556 self.starting_dir = py3compat.getcwd()
556 self.starting_dir = py3compat.getcwd()
557
557
558 # Indentation management
558 # Indentation management
559 self.indent_current_nsp = 0
559 self.indent_current_nsp = 0
560
560
561 # Dict to track post-execution functions that have been registered
561 # Dict to track post-execution functions that have been registered
562 self._post_execute = {}
562 self._post_execute = {}
563
563
564 def init_environment(self):
564 def init_environment(self):
565 """Any changes we need to make to the user's environment."""
565 """Any changes we need to make to the user's environment."""
566 pass
566 pass
567
567
568 def init_encoding(self):
568 def init_encoding(self):
569 # Get system encoding at startup time. Certain terminals (like Emacs
569 # Get system encoding at startup time. Certain terminals (like Emacs
570 # under Win32 have it set to None, and we need to have a known valid
570 # under Win32 have it set to None, and we need to have a known valid
571 # encoding to use in the raw_input() method
571 # encoding to use in the raw_input() method
572 try:
572 try:
573 self.stdin_encoding = sys.stdin.encoding or 'ascii'
573 self.stdin_encoding = sys.stdin.encoding or 'ascii'
574 except AttributeError:
574 except AttributeError:
575 self.stdin_encoding = 'ascii'
575 self.stdin_encoding = 'ascii'
576
576
577
577
578 @observe('colors')
578 @observe('colors')
579 def init_syntax_highlighting(self, changes=None):
579 def init_syntax_highlighting(self, changes=None):
580 # Python source parser/formatter for syntax highlighting
580 # Python source parser/formatter for syntax highlighting
581 pyformat = PyColorize.Parser(style=self.colors).format
581 pyformat = PyColorize.Parser(style=self.colors, parent=self).format
582 self.pycolorize = lambda src: pyformat(src,'str')
582 self.pycolorize = lambda src: pyformat(src,'str')
583
583
584 def refresh_style(self):
584 def refresh_style(self):
585 # No-op here, used in subclass
585 # No-op here, used in subclass
586 pass
586 pass
587
587
588 def init_pushd_popd_magic(self):
588 def init_pushd_popd_magic(self):
589 # for pushd/popd management
589 # for pushd/popd management
590 self.home_dir = get_home_dir()
590 self.home_dir = get_home_dir()
591
591
592 self.dir_stack = []
592 self.dir_stack = []
593
593
594 def init_logger(self):
594 def init_logger(self):
595 self.logger = Logger(self.home_dir, logfname='ipython_log.py',
595 self.logger = Logger(self.home_dir, logfname='ipython_log.py',
596 logmode='rotate')
596 logmode='rotate')
597
597
598 def init_logstart(self):
598 def init_logstart(self):
599 """Initialize logging in case it was requested at the command line.
599 """Initialize logging in case it was requested at the command line.
600 """
600 """
601 if self.logappend:
601 if self.logappend:
602 self.magic('logstart %s append' % self.logappend)
602 self.magic('logstart %s append' % self.logappend)
603 elif self.logfile:
603 elif self.logfile:
604 self.magic('logstart %s' % self.logfile)
604 self.magic('logstart %s' % self.logfile)
605 elif self.logstart:
605 elif self.logstart:
606 self.magic('logstart')
606 self.magic('logstart')
607
607
608 def init_deprecation_warnings(self):
608 def init_deprecation_warnings(self):
609 """
609 """
610 register default filter for deprecation warning.
610 register default filter for deprecation warning.
611
611
612 This will allow deprecation warning of function used interactively to show
612 This will allow deprecation warning of function used interactively to show
613 warning to users, and still hide deprecation warning from libraries import.
613 warning to users, and still hide deprecation warning from libraries import.
614 """
614 """
615 warnings.filterwarnings("default", category=DeprecationWarning, module=self.user_ns.get("__name__"))
615 warnings.filterwarnings("default", category=DeprecationWarning, module=self.user_ns.get("__name__"))
616
616
617 def init_builtins(self):
617 def init_builtins(self):
618 # A single, static flag that we set to True. Its presence indicates
618 # A single, static flag that we set to True. Its presence indicates
619 # that an IPython shell has been created, and we make no attempts at
619 # that an IPython shell has been created, and we make no attempts at
620 # removing on exit or representing the existence of more than one
620 # removing on exit or representing the existence of more than one
621 # IPython at a time.
621 # IPython at a time.
622 builtin_mod.__dict__['__IPYTHON__'] = True
622 builtin_mod.__dict__['__IPYTHON__'] = True
623
623
624 self.builtin_trap = BuiltinTrap(shell=self)
624 self.builtin_trap = BuiltinTrap(shell=self)
625
625
626 def init_inspector(self):
626 def init_inspector(self):
627 # Object inspector
627 # Object inspector
628 self.inspector = oinspect.Inspector(oinspect.InspectColors,
628 self.inspector = oinspect.Inspector(oinspect.InspectColors,
629 PyColorize.ANSICodeColors,
629 PyColorize.ANSICodeColors,
630 'NoColor',
630 'NoColor',
631 self.object_info_string_level)
631 self.object_info_string_level)
632
632
633 def init_io(self):
633 def init_io(self):
634 # This will just use sys.stdout and sys.stderr. If you want to
634 # This will just use sys.stdout and sys.stderr. If you want to
635 # override sys.stdout and sys.stderr themselves, you need to do that
635 # override sys.stdout and sys.stderr themselves, you need to do that
636 # *before* instantiating this class, because io holds onto
636 # *before* instantiating this class, because io holds onto
637 # references to the underlying streams.
637 # references to the underlying streams.
638 # io.std* are deprecated, but don't show our own deprecation warnings
638 # io.std* are deprecated, but don't show our own deprecation warnings
639 # during initialization of the deprecated API.
639 # during initialization of the deprecated API.
640 with warnings.catch_warnings():
640 with warnings.catch_warnings():
641 warnings.simplefilter('ignore', DeprecationWarning)
641 warnings.simplefilter('ignore', DeprecationWarning)
642 io.stdout = io.IOStream(sys.stdout)
642 io.stdout = io.IOStream(sys.stdout)
643 io.stderr = io.IOStream(sys.stderr)
643 io.stderr = io.IOStream(sys.stderr)
644
644
645 def init_prompts(self):
645 def init_prompts(self):
646 # Set system prompts, so that scripts can decide if they are running
646 # Set system prompts, so that scripts can decide if they are running
647 # interactively.
647 # interactively.
648 sys.ps1 = 'In : '
648 sys.ps1 = 'In : '
649 sys.ps2 = '...: '
649 sys.ps2 = '...: '
650 sys.ps3 = 'Out: '
650 sys.ps3 = 'Out: '
651
651
652 def init_display_formatter(self):
652 def init_display_formatter(self):
653 self.display_formatter = DisplayFormatter(parent=self)
653 self.display_formatter = DisplayFormatter(parent=self)
654 self.configurables.append(self.display_formatter)
654 self.configurables.append(self.display_formatter)
655
655
656 def init_display_pub(self):
656 def init_display_pub(self):
657 self.display_pub = self.display_pub_class(parent=self)
657 self.display_pub = self.display_pub_class(parent=self)
658 self.configurables.append(self.display_pub)
658 self.configurables.append(self.display_pub)
659
659
660 def init_data_pub(self):
660 def init_data_pub(self):
661 if not self.data_pub_class:
661 if not self.data_pub_class:
662 self.data_pub = None
662 self.data_pub = None
663 return
663 return
664 self.data_pub = self.data_pub_class(parent=self)
664 self.data_pub = self.data_pub_class(parent=self)
665 self.configurables.append(self.data_pub)
665 self.configurables.append(self.data_pub)
666
666
667 def init_displayhook(self):
667 def init_displayhook(self):
668 # Initialize displayhook, set in/out prompts and printing system
668 # Initialize displayhook, set in/out prompts and printing system
669 self.displayhook = self.displayhook_class(
669 self.displayhook = self.displayhook_class(
670 parent=self,
670 parent=self,
671 shell=self,
671 shell=self,
672 cache_size=self.cache_size,
672 cache_size=self.cache_size,
673 )
673 )
674 self.configurables.append(self.displayhook)
674 self.configurables.append(self.displayhook)
675 # This is a context manager that installs/revmoes the displayhook at
675 # This is a context manager that installs/revmoes the displayhook at
676 # the appropriate time.
676 # the appropriate time.
677 self.display_trap = DisplayTrap(hook=self.displayhook)
677 self.display_trap = DisplayTrap(hook=self.displayhook)
678
678
679 def init_virtualenv(self):
679 def init_virtualenv(self):
680 """Add a virtualenv to sys.path so the user can import modules from it.
680 """Add a virtualenv to sys.path so the user can import modules from it.
681 This isn't perfect: it doesn't use the Python interpreter with which the
681 This isn't perfect: it doesn't use the Python interpreter with which the
682 virtualenv was built, and it ignores the --no-site-packages option. A
682 virtualenv was built, and it ignores the --no-site-packages option. A
683 warning will appear suggesting the user installs IPython in the
683 warning will appear suggesting the user installs IPython in the
684 virtualenv, but for many cases, it probably works well enough.
684 virtualenv, but for many cases, it probably works well enough.
685
685
686 Adapted from code snippets online.
686 Adapted from code snippets online.
687
687
688 http://blog.ufsoft.org/2009/1/29/ipython-and-virtualenv
688 http://blog.ufsoft.org/2009/1/29/ipython-and-virtualenv
689 """
689 """
690 if 'VIRTUAL_ENV' not in os.environ:
690 if 'VIRTUAL_ENV' not in os.environ:
691 # Not in a virtualenv
691 # Not in a virtualenv
692 return
692 return
693
693
694 # venv detection:
694 # venv detection:
695 # stdlib venv may symlink sys.executable, so we can't use realpath.
695 # stdlib venv may symlink sys.executable, so we can't use realpath.
696 # but others can symlink *to* the venv Python, so we can't just use sys.executable.
696 # but others can symlink *to* the venv Python, so we can't just use sys.executable.
697 # So we just check every item in the symlink tree (generally <= 3)
697 # So we just check every item in the symlink tree (generally <= 3)
698 p = os.path.normcase(sys.executable)
698 p = os.path.normcase(sys.executable)
699 paths = [p]
699 paths = [p]
700 while os.path.islink(p):
700 while os.path.islink(p):
701 p = os.path.normcase(os.path.join(os.path.dirname(p), os.readlink(p)))
701 p = os.path.normcase(os.path.join(os.path.dirname(p), os.readlink(p)))
702 paths.append(p)
702 paths.append(p)
703 p_venv = os.path.normcase(os.environ['VIRTUAL_ENV'])
703 p_venv = os.path.normcase(os.environ['VIRTUAL_ENV'])
704 if any(p.startswith(p_venv) for p in paths):
704 if any(p.startswith(p_venv) for p in paths):
705 # Running properly in the virtualenv, don't need to do anything
705 # Running properly in the virtualenv, don't need to do anything
706 return
706 return
707
707
708 warn("Attempting to work in a virtualenv. If you encounter problems, please "
708 warn("Attempting to work in a virtualenv. If you encounter problems, please "
709 "install IPython inside the virtualenv.")
709 "install IPython inside the virtualenv.")
710 if sys.platform == "win32":
710 if sys.platform == "win32":
711 virtual_env = os.path.join(os.environ['VIRTUAL_ENV'], 'Lib', 'site-packages')
711 virtual_env = os.path.join(os.environ['VIRTUAL_ENV'], 'Lib', 'site-packages')
712 else:
712 else:
713 virtual_env = os.path.join(os.environ['VIRTUAL_ENV'], 'lib',
713 virtual_env = os.path.join(os.environ['VIRTUAL_ENV'], 'lib',
714 'python%d.%d' % sys.version_info[:2], 'site-packages')
714 'python%d.%d' % sys.version_info[:2], 'site-packages')
715
715
716 import site
716 import site
717 sys.path.insert(0, virtual_env)
717 sys.path.insert(0, virtual_env)
718 site.addsitedir(virtual_env)
718 site.addsitedir(virtual_env)
719
719
720 #-------------------------------------------------------------------------
720 #-------------------------------------------------------------------------
721 # Things related to injections into the sys module
721 # Things related to injections into the sys module
722 #-------------------------------------------------------------------------
722 #-------------------------------------------------------------------------
723
723
724 def save_sys_module_state(self):
724 def save_sys_module_state(self):
725 """Save the state of hooks in the sys module.
725 """Save the state of hooks in the sys module.
726
726
727 This has to be called after self.user_module is created.
727 This has to be called after self.user_module is created.
728 """
728 """
729 self._orig_sys_module_state = {'stdin': sys.stdin,
729 self._orig_sys_module_state = {'stdin': sys.stdin,
730 'stdout': sys.stdout,
730 'stdout': sys.stdout,
731 'stderr': sys.stderr,
731 'stderr': sys.stderr,
732 'excepthook': sys.excepthook}
732 'excepthook': sys.excepthook}
733 self._orig_sys_modules_main_name = self.user_module.__name__
733 self._orig_sys_modules_main_name = self.user_module.__name__
734 self._orig_sys_modules_main_mod = sys.modules.get(self.user_module.__name__)
734 self._orig_sys_modules_main_mod = sys.modules.get(self.user_module.__name__)
735
735
736 def restore_sys_module_state(self):
736 def restore_sys_module_state(self):
737 """Restore the state of the sys module."""
737 """Restore the state of the sys module."""
738 try:
738 try:
739 for k, v in iteritems(self._orig_sys_module_state):
739 for k, v in iteritems(self._orig_sys_module_state):
740 setattr(sys, k, v)
740 setattr(sys, k, v)
741 except AttributeError:
741 except AttributeError:
742 pass
742 pass
743 # Reset what what done in self.init_sys_modules
743 # Reset what what done in self.init_sys_modules
744 if self._orig_sys_modules_main_mod is not None:
744 if self._orig_sys_modules_main_mod is not None:
745 sys.modules[self._orig_sys_modules_main_name] = self._orig_sys_modules_main_mod
745 sys.modules[self._orig_sys_modules_main_name] = self._orig_sys_modules_main_mod
746
746
747 #-------------------------------------------------------------------------
747 #-------------------------------------------------------------------------
748 # Things related to the banner
748 # Things related to the banner
749 #-------------------------------------------------------------------------
749 #-------------------------------------------------------------------------
750
750
751 @property
751 @property
752 def banner(self):
752 def banner(self):
753 banner = self.banner1
753 banner = self.banner1
754 if self.profile and self.profile != 'default':
754 if self.profile and self.profile != 'default':
755 banner += '\nIPython profile: %s\n' % self.profile
755 banner += '\nIPython profile: %s\n' % self.profile
756 if self.banner2:
756 if self.banner2:
757 banner += '\n' + self.banner2
757 banner += '\n' + self.banner2
758 return banner
758 return banner
759
759
760 def show_banner(self, banner=None):
760 def show_banner(self, banner=None):
761 if banner is None:
761 if banner is None:
762 banner = self.banner
762 banner = self.banner
763 sys.stdout.write(banner)
763 sys.stdout.write(banner)
764
764
765 #-------------------------------------------------------------------------
765 #-------------------------------------------------------------------------
766 # Things related to hooks
766 # Things related to hooks
767 #-------------------------------------------------------------------------
767 #-------------------------------------------------------------------------
768
768
769 def init_hooks(self):
769 def init_hooks(self):
770 # hooks holds pointers used for user-side customizations
770 # hooks holds pointers used for user-side customizations
771 self.hooks = Struct()
771 self.hooks = Struct()
772
772
773 self.strdispatchers = {}
773 self.strdispatchers = {}
774
774
775 # Set all default hooks, defined in the IPython.hooks module.
775 # Set all default hooks, defined in the IPython.hooks module.
776 hooks = IPython.core.hooks
776 hooks = IPython.core.hooks
777 for hook_name in hooks.__all__:
777 for hook_name in hooks.__all__:
778 # default hooks have priority 100, i.e. low; user hooks should have
778 # default hooks have priority 100, i.e. low; user hooks should have
779 # 0-100 priority
779 # 0-100 priority
780 self.set_hook(hook_name,getattr(hooks,hook_name), 100, _warn_deprecated=False)
780 self.set_hook(hook_name,getattr(hooks,hook_name), 100, _warn_deprecated=False)
781
781
782 if self.display_page:
782 if self.display_page:
783 self.set_hook('show_in_pager', page.as_hook(page.display_page), 90)
783 self.set_hook('show_in_pager', page.as_hook(page.display_page), 90)
784
784
785 def set_hook(self,name,hook, priority=50, str_key=None, re_key=None,
785 def set_hook(self,name,hook, priority=50, str_key=None, re_key=None,
786 _warn_deprecated=True):
786 _warn_deprecated=True):
787 """set_hook(name,hook) -> sets an internal IPython hook.
787 """set_hook(name,hook) -> sets an internal IPython hook.
788
788
789 IPython exposes some of its internal API as user-modifiable hooks. By
789 IPython exposes some of its internal API as user-modifiable hooks. By
790 adding your function to one of these hooks, you can modify IPython's
790 adding your function to one of these hooks, you can modify IPython's
791 behavior to call at runtime your own routines."""
791 behavior to call at runtime your own routines."""
792
792
793 # At some point in the future, this should validate the hook before it
793 # At some point in the future, this should validate the hook before it
794 # accepts it. Probably at least check that the hook takes the number
794 # accepts it. Probably at least check that the hook takes the number
795 # of args it's supposed to.
795 # of args it's supposed to.
796
796
797 f = types.MethodType(hook,self)
797 f = types.MethodType(hook,self)
798
798
799 # check if the hook is for strdispatcher first
799 # check if the hook is for strdispatcher first
800 if str_key is not None:
800 if str_key is not None:
801 sdp = self.strdispatchers.get(name, StrDispatch())
801 sdp = self.strdispatchers.get(name, StrDispatch())
802 sdp.add_s(str_key, f, priority )
802 sdp.add_s(str_key, f, priority )
803 self.strdispatchers[name] = sdp
803 self.strdispatchers[name] = sdp
804 return
804 return
805 if re_key is not None:
805 if re_key is not None:
806 sdp = self.strdispatchers.get(name, StrDispatch())
806 sdp = self.strdispatchers.get(name, StrDispatch())
807 sdp.add_re(re.compile(re_key), f, priority )
807 sdp.add_re(re.compile(re_key), f, priority )
808 self.strdispatchers[name] = sdp
808 self.strdispatchers[name] = sdp
809 return
809 return
810
810
811 dp = getattr(self.hooks, name, None)
811 dp = getattr(self.hooks, name, None)
812 if name not in IPython.core.hooks.__all__:
812 if name not in IPython.core.hooks.__all__:
813 print("Warning! Hook '%s' is not one of %s" % \
813 print("Warning! Hook '%s' is not one of %s" % \
814 (name, IPython.core.hooks.__all__ ))
814 (name, IPython.core.hooks.__all__ ))
815
815
816 if _warn_deprecated and (name in IPython.core.hooks.deprecated):
816 if _warn_deprecated and (name in IPython.core.hooks.deprecated):
817 alternative = IPython.core.hooks.deprecated[name]
817 alternative = IPython.core.hooks.deprecated[name]
818 warn("Hook {} is deprecated. Use {} instead.".format(name, alternative))
818 warn("Hook {} is deprecated. Use {} instead.".format(name, alternative))
819
819
820 if not dp:
820 if not dp:
821 dp = IPython.core.hooks.CommandChainDispatcher()
821 dp = IPython.core.hooks.CommandChainDispatcher()
822
822
823 try:
823 try:
824 dp.add(f,priority)
824 dp.add(f,priority)
825 except AttributeError:
825 except AttributeError:
826 # it was not commandchain, plain old func - replace
826 # it was not commandchain, plain old func - replace
827 dp = f
827 dp = f
828
828
829 setattr(self.hooks,name, dp)
829 setattr(self.hooks,name, dp)
830
830
831 #-------------------------------------------------------------------------
831 #-------------------------------------------------------------------------
832 # Things related to events
832 # Things related to events
833 #-------------------------------------------------------------------------
833 #-------------------------------------------------------------------------
834
834
835 def init_events(self):
835 def init_events(self):
836 self.events = EventManager(self, available_events)
836 self.events = EventManager(self, available_events)
837
837
838 self.events.register("pre_execute", self._clear_warning_registry)
838 self.events.register("pre_execute", self._clear_warning_registry)
839
839
840 def register_post_execute(self, func):
840 def register_post_execute(self, func):
841 """DEPRECATED: Use ip.events.register('post_run_cell', func)
841 """DEPRECATED: Use ip.events.register('post_run_cell', func)
842
842
843 Register a function for calling after code execution.
843 Register a function for calling after code execution.
844 """
844 """
845 warn("ip.register_post_execute is deprecated, use "
845 warn("ip.register_post_execute is deprecated, use "
846 "ip.events.register('post_run_cell', func) instead.")
846 "ip.events.register('post_run_cell', func) instead.")
847 self.events.register('post_run_cell', func)
847 self.events.register('post_run_cell', func)
848
848
849 def _clear_warning_registry(self):
849 def _clear_warning_registry(self):
850 # clear the warning registry, so that different code blocks with
850 # clear the warning registry, so that different code blocks with
851 # overlapping line number ranges don't cause spurious suppression of
851 # overlapping line number ranges don't cause spurious suppression of
852 # warnings (see gh-6611 for details)
852 # warnings (see gh-6611 for details)
853 if "__warningregistry__" in self.user_global_ns:
853 if "__warningregistry__" in self.user_global_ns:
854 del self.user_global_ns["__warningregistry__"]
854 del self.user_global_ns["__warningregistry__"]
855
855
856 #-------------------------------------------------------------------------
856 #-------------------------------------------------------------------------
857 # Things related to the "main" module
857 # Things related to the "main" module
858 #-------------------------------------------------------------------------
858 #-------------------------------------------------------------------------
859
859
860 def new_main_mod(self, filename, modname):
860 def new_main_mod(self, filename, modname):
861 """Return a new 'main' module object for user code execution.
861 """Return a new 'main' module object for user code execution.
862
862
863 ``filename`` should be the path of the script which will be run in the
863 ``filename`` should be the path of the script which will be run in the
864 module. Requests with the same filename will get the same module, with
864 module. Requests with the same filename will get the same module, with
865 its namespace cleared.
865 its namespace cleared.
866
866
867 ``modname`` should be the module name - normally either '__main__' or
867 ``modname`` should be the module name - normally either '__main__' or
868 the basename of the file without the extension.
868 the basename of the file without the extension.
869
869
870 When scripts are executed via %run, we must keep a reference to their
870 When scripts are executed via %run, we must keep a reference to their
871 __main__ module around so that Python doesn't
871 __main__ module around so that Python doesn't
872 clear it, rendering references to module globals useless.
872 clear it, rendering references to module globals useless.
873
873
874 This method keeps said reference in a private dict, keyed by the
874 This method keeps said reference in a private dict, keyed by the
875 absolute path of the script. This way, for multiple executions of the
875 absolute path of the script. This way, for multiple executions of the
876 same script we only keep one copy of the namespace (the last one),
876 same script we only keep one copy of the namespace (the last one),
877 thus preventing memory leaks from old references while allowing the
877 thus preventing memory leaks from old references while allowing the
878 objects from the last execution to be accessible.
878 objects from the last execution to be accessible.
879 """
879 """
880 filename = os.path.abspath(filename)
880 filename = os.path.abspath(filename)
881 try:
881 try:
882 main_mod = self._main_mod_cache[filename]
882 main_mod = self._main_mod_cache[filename]
883 except KeyError:
883 except KeyError:
884 main_mod = self._main_mod_cache[filename] = types.ModuleType(
884 main_mod = self._main_mod_cache[filename] = types.ModuleType(
885 py3compat.cast_bytes_py2(modname),
885 py3compat.cast_bytes_py2(modname),
886 doc="Module created for script run in IPython")
886 doc="Module created for script run in IPython")
887 else:
887 else:
888 main_mod.__dict__.clear()
888 main_mod.__dict__.clear()
889 main_mod.__name__ = modname
889 main_mod.__name__ = modname
890
890
891 main_mod.__file__ = filename
891 main_mod.__file__ = filename
892 # It seems pydoc (and perhaps others) needs any module instance to
892 # It seems pydoc (and perhaps others) needs any module instance to
893 # implement a __nonzero__ method
893 # implement a __nonzero__ method
894 main_mod.__nonzero__ = lambda : True
894 main_mod.__nonzero__ = lambda : True
895
895
896 return main_mod
896 return main_mod
897
897
898 def clear_main_mod_cache(self):
898 def clear_main_mod_cache(self):
899 """Clear the cache of main modules.
899 """Clear the cache of main modules.
900
900
901 Mainly for use by utilities like %reset.
901 Mainly for use by utilities like %reset.
902
902
903 Examples
903 Examples
904 --------
904 --------
905
905
906 In [15]: import IPython
906 In [15]: import IPython
907
907
908 In [16]: m = _ip.new_main_mod(IPython.__file__, 'IPython')
908 In [16]: m = _ip.new_main_mod(IPython.__file__, 'IPython')
909
909
910 In [17]: len(_ip._main_mod_cache) > 0
910 In [17]: len(_ip._main_mod_cache) > 0
911 Out[17]: True
911 Out[17]: True
912
912
913 In [18]: _ip.clear_main_mod_cache()
913 In [18]: _ip.clear_main_mod_cache()
914
914
915 In [19]: len(_ip._main_mod_cache) == 0
915 In [19]: len(_ip._main_mod_cache) == 0
916 Out[19]: True
916 Out[19]: True
917 """
917 """
918 self._main_mod_cache.clear()
918 self._main_mod_cache.clear()
919
919
920 #-------------------------------------------------------------------------
920 #-------------------------------------------------------------------------
921 # Things related to debugging
921 # Things related to debugging
922 #-------------------------------------------------------------------------
922 #-------------------------------------------------------------------------
923
923
924 def init_pdb(self):
924 def init_pdb(self):
925 # Set calling of pdb on exceptions
925 # Set calling of pdb on exceptions
926 # self.call_pdb is a property
926 # self.call_pdb is a property
927 self.call_pdb = self.pdb
927 self.call_pdb = self.pdb
928
928
929 def _get_call_pdb(self):
929 def _get_call_pdb(self):
930 return self._call_pdb
930 return self._call_pdb
931
931
932 def _set_call_pdb(self,val):
932 def _set_call_pdb(self,val):
933
933
934 if val not in (0,1,False,True):
934 if val not in (0,1,False,True):
935 raise ValueError('new call_pdb value must be boolean')
935 raise ValueError('new call_pdb value must be boolean')
936
936
937 # store value in instance
937 # store value in instance
938 self._call_pdb = val
938 self._call_pdb = val
939
939
940 # notify the actual exception handlers
940 # notify the actual exception handlers
941 self.InteractiveTB.call_pdb = val
941 self.InteractiveTB.call_pdb = val
942
942
943 call_pdb = property(_get_call_pdb,_set_call_pdb,None,
943 call_pdb = property(_get_call_pdb,_set_call_pdb,None,
944 'Control auto-activation of pdb at exceptions')
944 'Control auto-activation of pdb at exceptions')
945
945
946 def debugger(self,force=False):
946 def debugger(self,force=False):
947 """Call the pdb debugger.
947 """Call the pdb debugger.
948
948
949 Keywords:
949 Keywords:
950
950
951 - force(False): by default, this routine checks the instance call_pdb
951 - force(False): by default, this routine checks the instance call_pdb
952 flag and does not actually invoke the debugger if the flag is false.
952 flag and does not actually invoke the debugger if the flag is false.
953 The 'force' option forces the debugger to activate even if the flag
953 The 'force' option forces the debugger to activate even if the flag
954 is false.
954 is false.
955 """
955 """
956
956
957 if not (force or self.call_pdb):
957 if not (force or self.call_pdb):
958 return
958 return
959
959
960 if not hasattr(sys,'last_traceback'):
960 if not hasattr(sys,'last_traceback'):
961 error('No traceback has been produced, nothing to debug.')
961 error('No traceback has been produced, nothing to debug.')
962 return
962 return
963
963
964 self.InteractiveTB.debugger(force=True)
964 self.InteractiveTB.debugger(force=True)
965
965
966 #-------------------------------------------------------------------------
966 #-------------------------------------------------------------------------
967 # Things related to IPython's various namespaces
967 # Things related to IPython's various namespaces
968 #-------------------------------------------------------------------------
968 #-------------------------------------------------------------------------
969 default_user_namespaces = True
969 default_user_namespaces = True
970
970
971 def init_create_namespaces(self, user_module=None, user_ns=None):
971 def init_create_namespaces(self, user_module=None, user_ns=None):
972 # Create the namespace where the user will operate. user_ns is
972 # Create the namespace where the user will operate. user_ns is
973 # normally the only one used, and it is passed to the exec calls as
973 # normally the only one used, and it is passed to the exec calls as
974 # the locals argument. But we do carry a user_global_ns namespace
974 # the locals argument. But we do carry a user_global_ns namespace
975 # given as the exec 'globals' argument, This is useful in embedding
975 # given as the exec 'globals' argument, This is useful in embedding
976 # situations where the ipython shell opens in a context where the
976 # situations where the ipython shell opens in a context where the
977 # distinction between locals and globals is meaningful. For
977 # distinction between locals and globals is meaningful. For
978 # non-embedded contexts, it is just the same object as the user_ns dict.
978 # non-embedded contexts, it is just the same object as the user_ns dict.
979
979
980 # FIXME. For some strange reason, __builtins__ is showing up at user
980 # FIXME. For some strange reason, __builtins__ is showing up at user
981 # level as a dict instead of a module. This is a manual fix, but I
981 # level as a dict instead of a module. This is a manual fix, but I
982 # should really track down where the problem is coming from. Alex
982 # should really track down where the problem is coming from. Alex
983 # Schmolck reported this problem first.
983 # Schmolck reported this problem first.
984
984
985 # A useful post by Alex Martelli on this topic:
985 # A useful post by Alex Martelli on this topic:
986 # Re: inconsistent value from __builtins__
986 # Re: inconsistent value from __builtins__
987 # Von: Alex Martelli <aleaxit@yahoo.com>
987 # Von: Alex Martelli <aleaxit@yahoo.com>
988 # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends
988 # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends
989 # Gruppen: comp.lang.python
989 # Gruppen: comp.lang.python
990
990
991 # Michael Hohn <hohn@hooknose.lbl.gov> wrote:
991 # Michael Hohn <hohn@hooknose.lbl.gov> wrote:
992 # > >>> print type(builtin_check.get_global_binding('__builtins__'))
992 # > >>> print type(builtin_check.get_global_binding('__builtins__'))
993 # > <type 'dict'>
993 # > <type 'dict'>
994 # > >>> print type(__builtins__)
994 # > >>> print type(__builtins__)
995 # > <type 'module'>
995 # > <type 'module'>
996 # > Is this difference in return value intentional?
996 # > Is this difference in return value intentional?
997
997
998 # Well, it's documented that '__builtins__' can be either a dictionary
998 # Well, it's documented that '__builtins__' can be either a dictionary
999 # or a module, and it's been that way for a long time. Whether it's
999 # or a module, and it's been that way for a long time. Whether it's
1000 # intentional (or sensible), I don't know. In any case, the idea is
1000 # intentional (or sensible), I don't know. In any case, the idea is
1001 # that if you need to access the built-in namespace directly, you
1001 # that if you need to access the built-in namespace directly, you
1002 # should start with "import __builtin__" (note, no 's') which will
1002 # should start with "import __builtin__" (note, no 's') which will
1003 # definitely give you a module. Yeah, it's somewhat confusing:-(.
1003 # definitely give you a module. Yeah, it's somewhat confusing:-(.
1004
1004
1005 # These routines return a properly built module and dict as needed by
1005 # These routines return a properly built module and dict as needed by
1006 # the rest of the code, and can also be used by extension writers to
1006 # the rest of the code, and can also be used by extension writers to
1007 # generate properly initialized namespaces.
1007 # generate properly initialized namespaces.
1008 if (user_ns is not None) or (user_module is not None):
1008 if (user_ns is not None) or (user_module is not None):
1009 self.default_user_namespaces = False
1009 self.default_user_namespaces = False
1010 self.user_module, self.user_ns = self.prepare_user_module(user_module, user_ns)
1010 self.user_module, self.user_ns = self.prepare_user_module(user_module, user_ns)
1011
1011
1012 # A record of hidden variables we have added to the user namespace, so
1012 # A record of hidden variables we have added to the user namespace, so
1013 # we can list later only variables defined in actual interactive use.
1013 # we can list later only variables defined in actual interactive use.
1014 self.user_ns_hidden = {}
1014 self.user_ns_hidden = {}
1015
1015
1016 # Now that FakeModule produces a real module, we've run into a nasty
1016 # Now that FakeModule produces a real module, we've run into a nasty
1017 # problem: after script execution (via %run), the module where the user
1017 # problem: after script execution (via %run), the module where the user
1018 # code ran is deleted. Now that this object is a true module (needed
1018 # code ran is deleted. Now that this object is a true module (needed
1019 # so doctest and other tools work correctly), the Python module
1019 # so doctest and other tools work correctly), the Python module
1020 # teardown mechanism runs over it, and sets to None every variable
1020 # teardown mechanism runs over it, and sets to None every variable
1021 # present in that module. Top-level references to objects from the
1021 # present in that module. Top-level references to objects from the
1022 # script survive, because the user_ns is updated with them. However,
1022 # script survive, because the user_ns is updated with them. However,
1023 # calling functions defined in the script that use other things from
1023 # calling functions defined in the script that use other things from
1024 # the script will fail, because the function's closure had references
1024 # the script will fail, because the function's closure had references
1025 # to the original objects, which are now all None. So we must protect
1025 # to the original objects, which are now all None. So we must protect
1026 # these modules from deletion by keeping a cache.
1026 # these modules from deletion by keeping a cache.
1027 #
1027 #
1028 # To avoid keeping stale modules around (we only need the one from the
1028 # To avoid keeping stale modules around (we only need the one from the
1029 # last run), we use a dict keyed with the full path to the script, so
1029 # last run), we use a dict keyed with the full path to the script, so
1030 # only the last version of the module is held in the cache. Note,
1030 # only the last version of the module is held in the cache. Note,
1031 # however, that we must cache the module *namespace contents* (their
1031 # however, that we must cache the module *namespace contents* (their
1032 # __dict__). Because if we try to cache the actual modules, old ones
1032 # __dict__). Because if we try to cache the actual modules, old ones
1033 # (uncached) could be destroyed while still holding references (such as
1033 # (uncached) could be destroyed while still holding references (such as
1034 # those held by GUI objects that tend to be long-lived)>
1034 # those held by GUI objects that tend to be long-lived)>
1035 #
1035 #
1036 # The %reset command will flush this cache. See the cache_main_mod()
1036 # The %reset command will flush this cache. See the cache_main_mod()
1037 # and clear_main_mod_cache() methods for details on use.
1037 # and clear_main_mod_cache() methods for details on use.
1038
1038
1039 # This is the cache used for 'main' namespaces
1039 # This is the cache used for 'main' namespaces
1040 self._main_mod_cache = {}
1040 self._main_mod_cache = {}
1041
1041
1042 # A table holding all the namespaces IPython deals with, so that
1042 # A table holding all the namespaces IPython deals with, so that
1043 # introspection facilities can search easily.
1043 # introspection facilities can search easily.
1044 self.ns_table = {'user_global':self.user_module.__dict__,
1044 self.ns_table = {'user_global':self.user_module.__dict__,
1045 'user_local':self.user_ns,
1045 'user_local':self.user_ns,
1046 'builtin':builtin_mod.__dict__
1046 'builtin':builtin_mod.__dict__
1047 }
1047 }
1048
1048
1049 @property
1049 @property
1050 def user_global_ns(self):
1050 def user_global_ns(self):
1051 return self.user_module.__dict__
1051 return self.user_module.__dict__
1052
1052
1053 def prepare_user_module(self, user_module=None, user_ns=None):
1053 def prepare_user_module(self, user_module=None, user_ns=None):
1054 """Prepare the module and namespace in which user code will be run.
1054 """Prepare the module and namespace in which user code will be run.
1055
1055
1056 When IPython is started normally, both parameters are None: a new module
1056 When IPython is started normally, both parameters are None: a new module
1057 is created automatically, and its __dict__ used as the namespace.
1057 is created automatically, and its __dict__ used as the namespace.
1058
1058
1059 If only user_module is provided, its __dict__ is used as the namespace.
1059 If only user_module is provided, its __dict__ is used as the namespace.
1060 If only user_ns is provided, a dummy module is created, and user_ns
1060 If only user_ns is provided, a dummy module is created, and user_ns
1061 becomes the global namespace. If both are provided (as they may be
1061 becomes the global namespace. If both are provided (as they may be
1062 when embedding), user_ns is the local namespace, and user_module
1062 when embedding), user_ns is the local namespace, and user_module
1063 provides the global namespace.
1063 provides the global namespace.
1064
1064
1065 Parameters
1065 Parameters
1066 ----------
1066 ----------
1067 user_module : module, optional
1067 user_module : module, optional
1068 The current user module in which IPython is being run. If None,
1068 The current user module in which IPython is being run. If None,
1069 a clean module will be created.
1069 a clean module will be created.
1070 user_ns : dict, optional
1070 user_ns : dict, optional
1071 A namespace in which to run interactive commands.
1071 A namespace in which to run interactive commands.
1072
1072
1073 Returns
1073 Returns
1074 -------
1074 -------
1075 A tuple of user_module and user_ns, each properly initialised.
1075 A tuple of user_module and user_ns, each properly initialised.
1076 """
1076 """
1077 if user_module is None and user_ns is not None:
1077 if user_module is None and user_ns is not None:
1078 user_ns.setdefault("__name__", "__main__")
1078 user_ns.setdefault("__name__", "__main__")
1079 user_module = DummyMod()
1079 user_module = DummyMod()
1080 user_module.__dict__ = user_ns
1080 user_module.__dict__ = user_ns
1081
1081
1082 if user_module is None:
1082 if user_module is None:
1083 user_module = types.ModuleType("__main__",
1083 user_module = types.ModuleType("__main__",
1084 doc="Automatically created module for IPython interactive environment")
1084 doc="Automatically created module for IPython interactive environment")
1085
1085
1086 # We must ensure that __builtin__ (without the final 's') is always
1086 # We must ensure that __builtin__ (without the final 's') is always
1087 # available and pointing to the __builtin__ *module*. For more details:
1087 # available and pointing to the __builtin__ *module*. For more details:
1088 # http://mail.python.org/pipermail/python-dev/2001-April/014068.html
1088 # http://mail.python.org/pipermail/python-dev/2001-April/014068.html
1089 user_module.__dict__.setdefault('__builtin__', builtin_mod)
1089 user_module.__dict__.setdefault('__builtin__', builtin_mod)
1090 user_module.__dict__.setdefault('__builtins__', builtin_mod)
1090 user_module.__dict__.setdefault('__builtins__', builtin_mod)
1091
1091
1092 if user_ns is None:
1092 if user_ns is None:
1093 user_ns = user_module.__dict__
1093 user_ns = user_module.__dict__
1094
1094
1095 return user_module, user_ns
1095 return user_module, user_ns
1096
1096
1097 def init_sys_modules(self):
1097 def init_sys_modules(self):
1098 # We need to insert into sys.modules something that looks like a
1098 # We need to insert into sys.modules something that looks like a
1099 # module but which accesses the IPython namespace, for shelve and
1099 # module but which accesses the IPython namespace, for shelve and
1100 # pickle to work interactively. Normally they rely on getting
1100 # pickle to work interactively. Normally they rely on getting
1101 # everything out of __main__, but for embedding purposes each IPython
1101 # everything out of __main__, but for embedding purposes each IPython
1102 # instance has its own private namespace, so we can't go shoving
1102 # instance has its own private namespace, so we can't go shoving
1103 # everything into __main__.
1103 # everything into __main__.
1104
1104
1105 # note, however, that we should only do this for non-embedded
1105 # note, however, that we should only do this for non-embedded
1106 # ipythons, which really mimic the __main__.__dict__ with their own
1106 # ipythons, which really mimic the __main__.__dict__ with their own
1107 # namespace. Embedded instances, on the other hand, should not do
1107 # namespace. Embedded instances, on the other hand, should not do
1108 # this because they need to manage the user local/global namespaces
1108 # this because they need to manage the user local/global namespaces
1109 # only, but they live within a 'normal' __main__ (meaning, they
1109 # only, but they live within a 'normal' __main__ (meaning, they
1110 # shouldn't overtake the execution environment of the script they're
1110 # shouldn't overtake the execution environment of the script they're
1111 # embedded in).
1111 # embedded in).
1112
1112
1113 # This is overridden in the InteractiveShellEmbed subclass to a no-op.
1113 # This is overridden in the InteractiveShellEmbed subclass to a no-op.
1114 main_name = self.user_module.__name__
1114 main_name = self.user_module.__name__
1115 sys.modules[main_name] = self.user_module
1115 sys.modules[main_name] = self.user_module
1116
1116
1117 def init_user_ns(self):
1117 def init_user_ns(self):
1118 """Initialize all user-visible namespaces to their minimum defaults.
1118 """Initialize all user-visible namespaces to their minimum defaults.
1119
1119
1120 Certain history lists are also initialized here, as they effectively
1120 Certain history lists are also initialized here, as they effectively
1121 act as user namespaces.
1121 act as user namespaces.
1122
1122
1123 Notes
1123 Notes
1124 -----
1124 -----
1125 All data structures here are only filled in, they are NOT reset by this
1125 All data structures here are only filled in, they are NOT reset by this
1126 method. If they were not empty before, data will simply be added to
1126 method. If they were not empty before, data will simply be added to
1127 therm.
1127 therm.
1128 """
1128 """
1129 # This function works in two parts: first we put a few things in
1129 # This function works in two parts: first we put a few things in
1130 # user_ns, and we sync that contents into user_ns_hidden so that these
1130 # user_ns, and we sync that contents into user_ns_hidden so that these
1131 # initial variables aren't shown by %who. After the sync, we add the
1131 # initial variables aren't shown by %who. After the sync, we add the
1132 # rest of what we *do* want the user to see with %who even on a new
1132 # rest of what we *do* want the user to see with %who even on a new
1133 # session (probably nothing, so they really only see their own stuff)
1133 # session (probably nothing, so they really only see their own stuff)
1134
1134
1135 # The user dict must *always* have a __builtin__ reference to the
1135 # The user dict must *always* have a __builtin__ reference to the
1136 # Python standard __builtin__ namespace, which must be imported.
1136 # Python standard __builtin__ namespace, which must be imported.
1137 # This is so that certain operations in prompt evaluation can be
1137 # This is so that certain operations in prompt evaluation can be
1138 # reliably executed with builtins. Note that we can NOT use
1138 # reliably executed with builtins. Note that we can NOT use
1139 # __builtins__ (note the 's'), because that can either be a dict or a
1139 # __builtins__ (note the 's'), because that can either be a dict or a
1140 # module, and can even mutate at runtime, depending on the context
1140 # module, and can even mutate at runtime, depending on the context
1141 # (Python makes no guarantees on it). In contrast, __builtin__ is
1141 # (Python makes no guarantees on it). In contrast, __builtin__ is
1142 # always a module object, though it must be explicitly imported.
1142 # always a module object, though it must be explicitly imported.
1143
1143
1144 # For more details:
1144 # For more details:
1145 # http://mail.python.org/pipermail/python-dev/2001-April/014068.html
1145 # http://mail.python.org/pipermail/python-dev/2001-April/014068.html
1146 ns = dict()
1146 ns = dict()
1147
1147
1148 # make global variables for user access to the histories
1148 # make global variables for user access to the histories
1149 ns['_ih'] = self.history_manager.input_hist_parsed
1149 ns['_ih'] = self.history_manager.input_hist_parsed
1150 ns['_oh'] = self.history_manager.output_hist
1150 ns['_oh'] = self.history_manager.output_hist
1151 ns['_dh'] = self.history_manager.dir_hist
1151 ns['_dh'] = self.history_manager.dir_hist
1152
1152
1153 ns['_sh'] = shadowns
1153 ns['_sh'] = shadowns
1154
1154
1155 # user aliases to input and output histories. These shouldn't show up
1155 # user aliases to input and output histories. These shouldn't show up
1156 # in %who, as they can have very large reprs.
1156 # in %who, as they can have very large reprs.
1157 ns['In'] = self.history_manager.input_hist_parsed
1157 ns['In'] = self.history_manager.input_hist_parsed
1158 ns['Out'] = self.history_manager.output_hist
1158 ns['Out'] = self.history_manager.output_hist
1159
1159
1160 # Store myself as the public api!!!
1160 # Store myself as the public api!!!
1161 ns['get_ipython'] = self.get_ipython
1161 ns['get_ipython'] = self.get_ipython
1162
1162
1163 ns['exit'] = self.exiter
1163 ns['exit'] = self.exiter
1164 ns['quit'] = self.exiter
1164 ns['quit'] = self.exiter
1165
1165
1166 # Sync what we've added so far to user_ns_hidden so these aren't seen
1166 # Sync what we've added so far to user_ns_hidden so these aren't seen
1167 # by %who
1167 # by %who
1168 self.user_ns_hidden.update(ns)
1168 self.user_ns_hidden.update(ns)
1169
1169
1170 # Anything put into ns now would show up in %who. Think twice before
1170 # Anything put into ns now would show up in %who. Think twice before
1171 # putting anything here, as we really want %who to show the user their
1171 # putting anything here, as we really want %who to show the user their
1172 # stuff, not our variables.
1172 # stuff, not our variables.
1173
1173
1174 # Finally, update the real user's namespace
1174 # Finally, update the real user's namespace
1175 self.user_ns.update(ns)
1175 self.user_ns.update(ns)
1176
1176
1177 @property
1177 @property
1178 def all_ns_refs(self):
1178 def all_ns_refs(self):
1179 """Get a list of references to all the namespace dictionaries in which
1179 """Get a list of references to all the namespace dictionaries in which
1180 IPython might store a user-created object.
1180 IPython might store a user-created object.
1181
1181
1182 Note that this does not include the displayhook, which also caches
1182 Note that this does not include the displayhook, which also caches
1183 objects from the output."""
1183 objects from the output."""
1184 return [self.user_ns, self.user_global_ns, self.user_ns_hidden] + \
1184 return [self.user_ns, self.user_global_ns, self.user_ns_hidden] + \
1185 [m.__dict__ for m in self._main_mod_cache.values()]
1185 [m.__dict__ for m in self._main_mod_cache.values()]
1186
1186
1187 def reset(self, new_session=True):
1187 def reset(self, new_session=True):
1188 """Clear all internal namespaces, and attempt to release references to
1188 """Clear all internal namespaces, and attempt to release references to
1189 user objects.
1189 user objects.
1190
1190
1191 If new_session is True, a new history session will be opened.
1191 If new_session is True, a new history session will be opened.
1192 """
1192 """
1193 # Clear histories
1193 # Clear histories
1194 self.history_manager.reset(new_session)
1194 self.history_manager.reset(new_session)
1195 # Reset counter used to index all histories
1195 # Reset counter used to index all histories
1196 if new_session:
1196 if new_session:
1197 self.execution_count = 1
1197 self.execution_count = 1
1198
1198
1199 # Flush cached output items
1199 # Flush cached output items
1200 if self.displayhook.do_full_cache:
1200 if self.displayhook.do_full_cache:
1201 self.displayhook.flush()
1201 self.displayhook.flush()
1202
1202
1203 # The main execution namespaces must be cleared very carefully,
1203 # The main execution namespaces must be cleared very carefully,
1204 # skipping the deletion of the builtin-related keys, because doing so
1204 # skipping the deletion of the builtin-related keys, because doing so
1205 # would cause errors in many object's __del__ methods.
1205 # would cause errors in many object's __del__ methods.
1206 if self.user_ns is not self.user_global_ns:
1206 if self.user_ns is not self.user_global_ns:
1207 self.user_ns.clear()
1207 self.user_ns.clear()
1208 ns = self.user_global_ns
1208 ns = self.user_global_ns
1209 drop_keys = set(ns.keys())
1209 drop_keys = set(ns.keys())
1210 drop_keys.discard('__builtin__')
1210 drop_keys.discard('__builtin__')
1211 drop_keys.discard('__builtins__')
1211 drop_keys.discard('__builtins__')
1212 drop_keys.discard('__name__')
1212 drop_keys.discard('__name__')
1213 for k in drop_keys:
1213 for k in drop_keys:
1214 del ns[k]
1214 del ns[k]
1215
1215
1216 self.user_ns_hidden.clear()
1216 self.user_ns_hidden.clear()
1217
1217
1218 # Restore the user namespaces to minimal usability
1218 # Restore the user namespaces to minimal usability
1219 self.init_user_ns()
1219 self.init_user_ns()
1220
1220
1221 # Restore the default and user aliases
1221 # Restore the default and user aliases
1222 self.alias_manager.clear_aliases()
1222 self.alias_manager.clear_aliases()
1223 self.alias_manager.init_aliases()
1223 self.alias_manager.init_aliases()
1224
1224
1225 # Flush the private list of module references kept for script
1225 # Flush the private list of module references kept for script
1226 # execution protection
1226 # execution protection
1227 self.clear_main_mod_cache()
1227 self.clear_main_mod_cache()
1228
1228
1229 def del_var(self, varname, by_name=False):
1229 def del_var(self, varname, by_name=False):
1230 """Delete a variable from the various namespaces, so that, as
1230 """Delete a variable from the various namespaces, so that, as
1231 far as possible, we're not keeping any hidden references to it.
1231 far as possible, we're not keeping any hidden references to it.
1232
1232
1233 Parameters
1233 Parameters
1234 ----------
1234 ----------
1235 varname : str
1235 varname : str
1236 The name of the variable to delete.
1236 The name of the variable to delete.
1237 by_name : bool
1237 by_name : bool
1238 If True, delete variables with the given name in each
1238 If True, delete variables with the given name in each
1239 namespace. If False (default), find the variable in the user
1239 namespace. If False (default), find the variable in the user
1240 namespace, and delete references to it.
1240 namespace, and delete references to it.
1241 """
1241 """
1242 if varname in ('__builtin__', '__builtins__'):
1242 if varname in ('__builtin__', '__builtins__'):
1243 raise ValueError("Refusing to delete %s" % varname)
1243 raise ValueError("Refusing to delete %s" % varname)
1244
1244
1245 ns_refs = self.all_ns_refs
1245 ns_refs = self.all_ns_refs
1246
1246
1247 if by_name: # Delete by name
1247 if by_name: # Delete by name
1248 for ns in ns_refs:
1248 for ns in ns_refs:
1249 try:
1249 try:
1250 del ns[varname]
1250 del ns[varname]
1251 except KeyError:
1251 except KeyError:
1252 pass
1252 pass
1253 else: # Delete by object
1253 else: # Delete by object
1254 try:
1254 try:
1255 obj = self.user_ns[varname]
1255 obj = self.user_ns[varname]
1256 except KeyError:
1256 except KeyError:
1257 raise NameError("name '%s' is not defined" % varname)
1257 raise NameError("name '%s' is not defined" % varname)
1258 # Also check in output history
1258 # Also check in output history
1259 ns_refs.append(self.history_manager.output_hist)
1259 ns_refs.append(self.history_manager.output_hist)
1260 for ns in ns_refs:
1260 for ns in ns_refs:
1261 to_delete = [n for n, o in iteritems(ns) if o is obj]
1261 to_delete = [n for n, o in iteritems(ns) if o is obj]
1262 for name in to_delete:
1262 for name in to_delete:
1263 del ns[name]
1263 del ns[name]
1264
1264
1265 # displayhook keeps extra references, but not in a dictionary
1265 # displayhook keeps extra references, but not in a dictionary
1266 for name in ('_', '__', '___'):
1266 for name in ('_', '__', '___'):
1267 if getattr(self.displayhook, name) is obj:
1267 if getattr(self.displayhook, name) is obj:
1268 setattr(self.displayhook, name, None)
1268 setattr(self.displayhook, name, None)
1269
1269
1270 def reset_selective(self, regex=None):
1270 def reset_selective(self, regex=None):
1271 """Clear selective variables from internal namespaces based on a
1271 """Clear selective variables from internal namespaces based on a
1272 specified regular expression.
1272 specified regular expression.
1273
1273
1274 Parameters
1274 Parameters
1275 ----------
1275 ----------
1276 regex : string or compiled pattern, optional
1276 regex : string or compiled pattern, optional
1277 A regular expression pattern that will be used in searching
1277 A regular expression pattern that will be used in searching
1278 variable names in the users namespaces.
1278 variable names in the users namespaces.
1279 """
1279 """
1280 if regex is not None:
1280 if regex is not None:
1281 try:
1281 try:
1282 m = re.compile(regex)
1282 m = re.compile(regex)
1283 except TypeError:
1283 except TypeError:
1284 raise TypeError('regex must be a string or compiled pattern')
1284 raise TypeError('regex must be a string or compiled pattern')
1285 # Search for keys in each namespace that match the given regex
1285 # Search for keys in each namespace that match the given regex
1286 # If a match is found, delete the key/value pair.
1286 # If a match is found, delete the key/value pair.
1287 for ns in self.all_ns_refs:
1287 for ns in self.all_ns_refs:
1288 for var in ns:
1288 for var in ns:
1289 if m.search(var):
1289 if m.search(var):
1290 del ns[var]
1290 del ns[var]
1291
1291
1292 def push(self, variables, interactive=True):
1292 def push(self, variables, interactive=True):
1293 """Inject a group of variables into the IPython user namespace.
1293 """Inject a group of variables into the IPython user namespace.
1294
1294
1295 Parameters
1295 Parameters
1296 ----------
1296 ----------
1297 variables : dict, str or list/tuple of str
1297 variables : dict, str or list/tuple of str
1298 The variables to inject into the user's namespace. If a dict, a
1298 The variables to inject into the user's namespace. If a dict, a
1299 simple update is done. If a str, the string is assumed to have
1299 simple update is done. If a str, the string is assumed to have
1300 variable names separated by spaces. A list/tuple of str can also
1300 variable names separated by spaces. A list/tuple of str can also
1301 be used to give the variable names. If just the variable names are
1301 be used to give the variable names. If just the variable names are
1302 give (list/tuple/str) then the variable values looked up in the
1302 give (list/tuple/str) then the variable values looked up in the
1303 callers frame.
1303 callers frame.
1304 interactive : bool
1304 interactive : bool
1305 If True (default), the variables will be listed with the ``who``
1305 If True (default), the variables will be listed with the ``who``
1306 magic.
1306 magic.
1307 """
1307 """
1308 vdict = None
1308 vdict = None
1309
1309
1310 # We need a dict of name/value pairs to do namespace updates.
1310 # We need a dict of name/value pairs to do namespace updates.
1311 if isinstance(variables, dict):
1311 if isinstance(variables, dict):
1312 vdict = variables
1312 vdict = variables
1313 elif isinstance(variables, string_types+(list, tuple)):
1313 elif isinstance(variables, string_types+(list, tuple)):
1314 if isinstance(variables, string_types):
1314 if isinstance(variables, string_types):
1315 vlist = variables.split()
1315 vlist = variables.split()
1316 else:
1316 else:
1317 vlist = variables
1317 vlist = variables
1318 vdict = {}
1318 vdict = {}
1319 cf = sys._getframe(1)
1319 cf = sys._getframe(1)
1320 for name in vlist:
1320 for name in vlist:
1321 try:
1321 try:
1322 vdict[name] = eval(name, cf.f_globals, cf.f_locals)
1322 vdict[name] = eval(name, cf.f_globals, cf.f_locals)
1323 except:
1323 except:
1324 print('Could not get variable %s from %s' %
1324 print('Could not get variable %s from %s' %
1325 (name,cf.f_code.co_name))
1325 (name,cf.f_code.co_name))
1326 else:
1326 else:
1327 raise ValueError('variables must be a dict/str/list/tuple')
1327 raise ValueError('variables must be a dict/str/list/tuple')
1328
1328
1329 # Propagate variables to user namespace
1329 # Propagate variables to user namespace
1330 self.user_ns.update(vdict)
1330 self.user_ns.update(vdict)
1331
1331
1332 # And configure interactive visibility
1332 # And configure interactive visibility
1333 user_ns_hidden = self.user_ns_hidden
1333 user_ns_hidden = self.user_ns_hidden
1334 if interactive:
1334 if interactive:
1335 for name in vdict:
1335 for name in vdict:
1336 user_ns_hidden.pop(name, None)
1336 user_ns_hidden.pop(name, None)
1337 else:
1337 else:
1338 user_ns_hidden.update(vdict)
1338 user_ns_hidden.update(vdict)
1339
1339
1340 def drop_by_id(self, variables):
1340 def drop_by_id(self, variables):
1341 """Remove a dict of variables from the user namespace, if they are the
1341 """Remove a dict of variables from the user namespace, if they are the
1342 same as the values in the dictionary.
1342 same as the values in the dictionary.
1343
1343
1344 This is intended for use by extensions: variables that they've added can
1344 This is intended for use by extensions: variables that they've added can
1345 be taken back out if they are unloaded, without removing any that the
1345 be taken back out if they are unloaded, without removing any that the
1346 user has overwritten.
1346 user has overwritten.
1347
1347
1348 Parameters
1348 Parameters
1349 ----------
1349 ----------
1350 variables : dict
1350 variables : dict
1351 A dictionary mapping object names (as strings) to the objects.
1351 A dictionary mapping object names (as strings) to the objects.
1352 """
1352 """
1353 for name, obj in iteritems(variables):
1353 for name, obj in iteritems(variables):
1354 if name in self.user_ns and self.user_ns[name] is obj:
1354 if name in self.user_ns and self.user_ns[name] is obj:
1355 del self.user_ns[name]
1355 del self.user_ns[name]
1356 self.user_ns_hidden.pop(name, None)
1356 self.user_ns_hidden.pop(name, None)
1357
1357
1358 #-------------------------------------------------------------------------
1358 #-------------------------------------------------------------------------
1359 # Things related to object introspection
1359 # Things related to object introspection
1360 #-------------------------------------------------------------------------
1360 #-------------------------------------------------------------------------
1361
1361
1362 def _ofind(self, oname, namespaces=None):
1362 def _ofind(self, oname, namespaces=None):
1363 """Find an object in the available namespaces.
1363 """Find an object in the available namespaces.
1364
1364
1365 self._ofind(oname) -> dict with keys: found,obj,ospace,ismagic
1365 self._ofind(oname) -> dict with keys: found,obj,ospace,ismagic
1366
1366
1367 Has special code to detect magic functions.
1367 Has special code to detect magic functions.
1368 """
1368 """
1369 oname = oname.strip()
1369 oname = oname.strip()
1370 #print '1- oname: <%r>' % oname # dbg
1370 #print '1- oname: <%r>' % oname # dbg
1371 if not oname.startswith(ESC_MAGIC) and \
1371 if not oname.startswith(ESC_MAGIC) and \
1372 not oname.startswith(ESC_MAGIC2) and \
1372 not oname.startswith(ESC_MAGIC2) and \
1373 not py3compat.isidentifier(oname, dotted=True):
1373 not py3compat.isidentifier(oname, dotted=True):
1374 return dict(found=False)
1374 return dict(found=False)
1375
1375
1376 if namespaces is None:
1376 if namespaces is None:
1377 # Namespaces to search in:
1377 # Namespaces to search in:
1378 # Put them in a list. The order is important so that we
1378 # Put them in a list. The order is important so that we
1379 # find things in the same order that Python finds them.
1379 # find things in the same order that Python finds them.
1380 namespaces = [ ('Interactive', self.user_ns),
1380 namespaces = [ ('Interactive', self.user_ns),
1381 ('Interactive (global)', self.user_global_ns),
1381 ('Interactive (global)', self.user_global_ns),
1382 ('Python builtin', builtin_mod.__dict__),
1382 ('Python builtin', builtin_mod.__dict__),
1383 ]
1383 ]
1384
1384
1385 # initialize results to 'null'
1385 # initialize results to 'null'
1386 found = False; obj = None; ospace = None;
1386 found = False; obj = None; ospace = None;
1387 ismagic = False; isalias = False; parent = None
1387 ismagic = False; isalias = False; parent = None
1388
1388
1389 # We need to special-case 'print', which as of python2.6 registers as a
1389 # We need to special-case 'print', which as of python2.6 registers as a
1390 # function but should only be treated as one if print_function was
1390 # function but should only be treated as one if print_function was
1391 # loaded with a future import. In this case, just bail.
1391 # loaded with a future import. In this case, just bail.
1392 if (oname == 'print' and not py3compat.PY3 and not \
1392 if (oname == 'print' and not py3compat.PY3 and not \
1393 (self.compile.compiler_flags & __future__.CO_FUTURE_PRINT_FUNCTION)):
1393 (self.compile.compiler_flags & __future__.CO_FUTURE_PRINT_FUNCTION)):
1394 return {'found':found, 'obj':obj, 'namespace':ospace,
1394 return {'found':found, 'obj':obj, 'namespace':ospace,
1395 'ismagic':ismagic, 'isalias':isalias, 'parent':parent}
1395 'ismagic':ismagic, 'isalias':isalias, 'parent':parent}
1396
1396
1397 # Look for the given name by splitting it in parts. If the head is
1397 # Look for the given name by splitting it in parts. If the head is
1398 # found, then we look for all the remaining parts as members, and only
1398 # found, then we look for all the remaining parts as members, and only
1399 # declare success if we can find them all.
1399 # declare success if we can find them all.
1400 oname_parts = oname.split('.')
1400 oname_parts = oname.split('.')
1401 oname_head, oname_rest = oname_parts[0],oname_parts[1:]
1401 oname_head, oname_rest = oname_parts[0],oname_parts[1:]
1402 for nsname,ns in namespaces:
1402 for nsname,ns in namespaces:
1403 try:
1403 try:
1404 obj = ns[oname_head]
1404 obj = ns[oname_head]
1405 except KeyError:
1405 except KeyError:
1406 continue
1406 continue
1407 else:
1407 else:
1408 #print 'oname_rest:', oname_rest # dbg
1408 #print 'oname_rest:', oname_rest # dbg
1409 for idx, part in enumerate(oname_rest):
1409 for idx, part in enumerate(oname_rest):
1410 try:
1410 try:
1411 parent = obj
1411 parent = obj
1412 # The last part is looked up in a special way to avoid
1412 # The last part is looked up in a special way to avoid
1413 # descriptor invocation as it may raise or have side
1413 # descriptor invocation as it may raise or have side
1414 # effects.
1414 # effects.
1415 if idx == len(oname_rest) - 1:
1415 if idx == len(oname_rest) - 1:
1416 obj = self._getattr_property(obj, part)
1416 obj = self._getattr_property(obj, part)
1417 else:
1417 else:
1418 obj = getattr(obj, part)
1418 obj = getattr(obj, part)
1419 except:
1419 except:
1420 # Blanket except b/c some badly implemented objects
1420 # Blanket except b/c some badly implemented objects
1421 # allow __getattr__ to raise exceptions other than
1421 # allow __getattr__ to raise exceptions other than
1422 # AttributeError, which then crashes IPython.
1422 # AttributeError, which then crashes IPython.
1423 break
1423 break
1424 else:
1424 else:
1425 # If we finish the for loop (no break), we got all members
1425 # If we finish the for loop (no break), we got all members
1426 found = True
1426 found = True
1427 ospace = nsname
1427 ospace = nsname
1428 break # namespace loop
1428 break # namespace loop
1429
1429
1430 # Try to see if it's magic
1430 # Try to see if it's magic
1431 if not found:
1431 if not found:
1432 obj = None
1432 obj = None
1433 if oname.startswith(ESC_MAGIC2):
1433 if oname.startswith(ESC_MAGIC2):
1434 oname = oname.lstrip(ESC_MAGIC2)
1434 oname = oname.lstrip(ESC_MAGIC2)
1435 obj = self.find_cell_magic(oname)
1435 obj = self.find_cell_magic(oname)
1436 elif oname.startswith(ESC_MAGIC):
1436 elif oname.startswith(ESC_MAGIC):
1437 oname = oname.lstrip(ESC_MAGIC)
1437 oname = oname.lstrip(ESC_MAGIC)
1438 obj = self.find_line_magic(oname)
1438 obj = self.find_line_magic(oname)
1439 else:
1439 else:
1440 # search without prefix, so run? will find %run?
1440 # search without prefix, so run? will find %run?
1441 obj = self.find_line_magic(oname)
1441 obj = self.find_line_magic(oname)
1442 if obj is None:
1442 if obj is None:
1443 obj = self.find_cell_magic(oname)
1443 obj = self.find_cell_magic(oname)
1444 if obj is not None:
1444 if obj is not None:
1445 found = True
1445 found = True
1446 ospace = 'IPython internal'
1446 ospace = 'IPython internal'
1447 ismagic = True
1447 ismagic = True
1448 isalias = isinstance(obj, Alias)
1448 isalias = isinstance(obj, Alias)
1449
1449
1450 # Last try: special-case some literals like '', [], {}, etc:
1450 # Last try: special-case some literals like '', [], {}, etc:
1451 if not found and oname_head in ["''",'""','[]','{}','()']:
1451 if not found and oname_head in ["''",'""','[]','{}','()']:
1452 obj = eval(oname_head)
1452 obj = eval(oname_head)
1453 found = True
1453 found = True
1454 ospace = 'Interactive'
1454 ospace = 'Interactive'
1455
1455
1456 return {'found':found, 'obj':obj, 'namespace':ospace,
1456 return {'found':found, 'obj':obj, 'namespace':ospace,
1457 'ismagic':ismagic, 'isalias':isalias, 'parent':parent}
1457 'ismagic':ismagic, 'isalias':isalias, 'parent':parent}
1458
1458
1459 @staticmethod
1459 @staticmethod
1460 def _getattr_property(obj, attrname):
1460 def _getattr_property(obj, attrname):
1461 """Property-aware getattr to use in object finding.
1461 """Property-aware getattr to use in object finding.
1462
1462
1463 If attrname represents a property, return it unevaluated (in case it has
1463 If attrname represents a property, return it unevaluated (in case it has
1464 side effects or raises an error.
1464 side effects or raises an error.
1465
1465
1466 """
1466 """
1467 if not isinstance(obj, type):
1467 if not isinstance(obj, type):
1468 try:
1468 try:
1469 # `getattr(type(obj), attrname)` is not guaranteed to return
1469 # `getattr(type(obj), attrname)` is not guaranteed to return
1470 # `obj`, but does so for property:
1470 # `obj`, but does so for property:
1471 #
1471 #
1472 # property.__get__(self, None, cls) -> self
1472 # property.__get__(self, None, cls) -> self
1473 #
1473 #
1474 # The universal alternative is to traverse the mro manually
1474 # The universal alternative is to traverse the mro manually
1475 # searching for attrname in class dicts.
1475 # searching for attrname in class dicts.
1476 attr = getattr(type(obj), attrname)
1476 attr = getattr(type(obj), attrname)
1477 except AttributeError:
1477 except AttributeError:
1478 pass
1478 pass
1479 else:
1479 else:
1480 # This relies on the fact that data descriptors (with both
1480 # This relies on the fact that data descriptors (with both
1481 # __get__ & __set__ magic methods) take precedence over
1481 # __get__ & __set__ magic methods) take precedence over
1482 # instance-level attributes:
1482 # instance-level attributes:
1483 #
1483 #
1484 # class A(object):
1484 # class A(object):
1485 # @property
1485 # @property
1486 # def foobar(self): return 123
1486 # def foobar(self): return 123
1487 # a = A()
1487 # a = A()
1488 # a.__dict__['foobar'] = 345
1488 # a.__dict__['foobar'] = 345
1489 # a.foobar # == 123
1489 # a.foobar # == 123
1490 #
1490 #
1491 # So, a property may be returned right away.
1491 # So, a property may be returned right away.
1492 if isinstance(attr, property):
1492 if isinstance(attr, property):
1493 return attr
1493 return attr
1494
1494
1495 # Nothing helped, fall back.
1495 # Nothing helped, fall back.
1496 return getattr(obj, attrname)
1496 return getattr(obj, attrname)
1497
1497
1498 def _object_find(self, oname, namespaces=None):
1498 def _object_find(self, oname, namespaces=None):
1499 """Find an object and return a struct with info about it."""
1499 """Find an object and return a struct with info about it."""
1500 return Struct(self._ofind(oname, namespaces))
1500 return Struct(self._ofind(oname, namespaces))
1501
1501
1502 def _inspect(self, meth, oname, namespaces=None, **kw):
1502 def _inspect(self, meth, oname, namespaces=None, **kw):
1503 """Generic interface to the inspector system.
1503 """Generic interface to the inspector system.
1504
1504
1505 This function is meant to be called by pdef, pdoc & friends.
1505 This function is meant to be called by pdef, pdoc & friends.
1506 """
1506 """
1507 info = self._object_find(oname, namespaces)
1507 info = self._object_find(oname, namespaces)
1508 docformat = sphinxify if self.sphinxify_docstring else None
1508 docformat = sphinxify if self.sphinxify_docstring else None
1509 if info.found:
1509 if info.found:
1510 pmethod = getattr(self.inspector, meth)
1510 pmethod = getattr(self.inspector, meth)
1511 # TODO: only apply format_screen to the plain/text repr of the mime
1511 # TODO: only apply format_screen to the plain/text repr of the mime
1512 # bundle.
1512 # bundle.
1513 formatter = format_screen if info.ismagic else docformat
1513 formatter = format_screen if info.ismagic else docformat
1514 if meth == 'pdoc':
1514 if meth == 'pdoc':
1515 pmethod(info.obj, oname, formatter)
1515 pmethod(info.obj, oname, formatter)
1516 elif meth == 'pinfo':
1516 elif meth == 'pinfo':
1517 pmethod(info.obj, oname, formatter, info,
1517 pmethod(info.obj, oname, formatter, info,
1518 enable_html_pager=self.enable_html_pager, **kw)
1518 enable_html_pager=self.enable_html_pager, **kw)
1519 else:
1519 else:
1520 pmethod(info.obj, oname)
1520 pmethod(info.obj, oname)
1521 else:
1521 else:
1522 print('Object `%s` not found.' % oname)
1522 print('Object `%s` not found.' % oname)
1523 return 'not found' # so callers can take other action
1523 return 'not found' # so callers can take other action
1524
1524
1525 def object_inspect(self, oname, detail_level=0):
1525 def object_inspect(self, oname, detail_level=0):
1526 """Get object info about oname"""
1526 """Get object info about oname"""
1527 with self.builtin_trap:
1527 with self.builtin_trap:
1528 info = self._object_find(oname)
1528 info = self._object_find(oname)
1529 if info.found:
1529 if info.found:
1530 return self.inspector.info(info.obj, oname, info=info,
1530 return self.inspector.info(info.obj, oname, info=info,
1531 detail_level=detail_level
1531 detail_level=detail_level
1532 )
1532 )
1533 else:
1533 else:
1534 return oinspect.object_info(name=oname, found=False)
1534 return oinspect.object_info(name=oname, found=False)
1535
1535
1536 def object_inspect_text(self, oname, detail_level=0):
1536 def object_inspect_text(self, oname, detail_level=0):
1537 """Get object info as formatted text"""
1537 """Get object info as formatted text"""
1538 return self.object_inspect_mime(oname, detail_level)['text/plain']
1538 return self.object_inspect_mime(oname, detail_level)['text/plain']
1539
1539
1540 def object_inspect_mime(self, oname, detail_level=0):
1540 def object_inspect_mime(self, oname, detail_level=0):
1541 """Get object info as a mimebundle of formatted representations.
1541 """Get object info as a mimebundle of formatted representations.
1542
1542
1543 A mimebundle is a dictionary, keyed by mime-type.
1543 A mimebundle is a dictionary, keyed by mime-type.
1544 It must always have the key `'text/plain'`.
1544 It must always have the key `'text/plain'`.
1545 """
1545 """
1546 with self.builtin_trap:
1546 with self.builtin_trap:
1547 info = self._object_find(oname)
1547 info = self._object_find(oname)
1548 if info.found:
1548 if info.found:
1549 return self.inspector._get_info(info.obj, oname, info=info,
1549 return self.inspector._get_info(info.obj, oname, info=info,
1550 detail_level=detail_level
1550 detail_level=detail_level
1551 )
1551 )
1552 else:
1552 else:
1553 raise KeyError(oname)
1553 raise KeyError(oname)
1554
1554
1555 #-------------------------------------------------------------------------
1555 #-------------------------------------------------------------------------
1556 # Things related to history management
1556 # Things related to history management
1557 #-------------------------------------------------------------------------
1557 #-------------------------------------------------------------------------
1558
1558
1559 def init_history(self):
1559 def init_history(self):
1560 """Sets up the command history, and starts regular autosaves."""
1560 """Sets up the command history, and starts regular autosaves."""
1561 self.history_manager = HistoryManager(shell=self, parent=self)
1561 self.history_manager = HistoryManager(shell=self, parent=self)
1562 self.configurables.append(self.history_manager)
1562 self.configurables.append(self.history_manager)
1563
1563
1564 #-------------------------------------------------------------------------
1564 #-------------------------------------------------------------------------
1565 # Things related to exception handling and tracebacks (not debugging)
1565 # Things related to exception handling and tracebacks (not debugging)
1566 #-------------------------------------------------------------------------
1566 #-------------------------------------------------------------------------
1567
1567
1568 debugger_cls = Pdb
1568 debugger_cls = Pdb
1569
1569
1570 def init_traceback_handlers(self, custom_exceptions):
1570 def init_traceback_handlers(self, custom_exceptions):
1571 # Syntax error handler.
1571 # Syntax error handler.
1572 self.SyntaxTB = ultratb.SyntaxTB(color_scheme='NoColor')
1572 self.SyntaxTB = ultratb.SyntaxTB(color_scheme='NoColor', parent=self)
1573
1573
1574 # The interactive one is initialized with an offset, meaning we always
1574 # The interactive one is initialized with an offset, meaning we always
1575 # want to remove the topmost item in the traceback, which is our own
1575 # want to remove the topmost item in the traceback, which is our own
1576 # internal code. Valid modes: ['Plain','Context','Verbose']
1576 # internal code. Valid modes: ['Plain','Context','Verbose']
1577 self.InteractiveTB = ultratb.AutoFormattedTB(mode = 'Plain',
1577 self.InteractiveTB = ultratb.AutoFormattedTB(mode = 'Plain',
1578 color_scheme='NoColor',
1578 color_scheme='NoColor',
1579 tb_offset = 1,
1579 tb_offset = 1,
1580 check_cache=check_linecache_ipython,
1580 check_cache=check_linecache_ipython,
1581 debugger_cls=self.debugger_cls)
1581 debugger_cls=self.debugger_cls, parent=self)
1582
1582
1583 # The instance will store a pointer to the system-wide exception hook,
1583 # The instance will store a pointer to the system-wide exception hook,
1584 # so that runtime code (such as magics) can access it. This is because
1584 # so that runtime code (such as magics) can access it. This is because
1585 # during the read-eval loop, it may get temporarily overwritten.
1585 # during the read-eval loop, it may get temporarily overwritten.
1586 self.sys_excepthook = sys.excepthook
1586 self.sys_excepthook = sys.excepthook
1587
1587
1588 # and add any custom exception handlers the user may have specified
1588 # and add any custom exception handlers the user may have specified
1589 self.set_custom_exc(*custom_exceptions)
1589 self.set_custom_exc(*custom_exceptions)
1590
1590
1591 # Set the exception mode
1591 # Set the exception mode
1592 self.InteractiveTB.set_mode(mode=self.xmode)
1592 self.InteractiveTB.set_mode(mode=self.xmode)
1593
1593
1594 def set_custom_exc(self, exc_tuple, handler):
1594 def set_custom_exc(self, exc_tuple, handler):
1595 """set_custom_exc(exc_tuple, handler)
1595 """set_custom_exc(exc_tuple, handler)
1596
1596
1597 Set a custom exception handler, which will be called if any of the
1597 Set a custom exception handler, which will be called if any of the
1598 exceptions in exc_tuple occur in the mainloop (specifically, in the
1598 exceptions in exc_tuple occur in the mainloop (specifically, in the
1599 run_code() method).
1599 run_code() method).
1600
1600
1601 Parameters
1601 Parameters
1602 ----------
1602 ----------
1603
1603
1604 exc_tuple : tuple of exception classes
1604 exc_tuple : tuple of exception classes
1605 A *tuple* of exception classes, for which to call the defined
1605 A *tuple* of exception classes, for which to call the defined
1606 handler. It is very important that you use a tuple, and NOT A
1606 handler. It is very important that you use a tuple, and NOT A
1607 LIST here, because of the way Python's except statement works. If
1607 LIST here, because of the way Python's except statement works. If
1608 you only want to trap a single exception, use a singleton tuple::
1608 you only want to trap a single exception, use a singleton tuple::
1609
1609
1610 exc_tuple == (MyCustomException,)
1610 exc_tuple == (MyCustomException,)
1611
1611
1612 handler : callable
1612 handler : callable
1613 handler must have the following signature::
1613 handler must have the following signature::
1614
1614
1615 def my_handler(self, etype, value, tb, tb_offset=None):
1615 def my_handler(self, etype, value, tb, tb_offset=None):
1616 ...
1616 ...
1617 return structured_traceback
1617 return structured_traceback
1618
1618
1619 Your handler must return a structured traceback (a list of strings),
1619 Your handler must return a structured traceback (a list of strings),
1620 or None.
1620 or None.
1621
1621
1622 This will be made into an instance method (via types.MethodType)
1622 This will be made into an instance method (via types.MethodType)
1623 of IPython itself, and it will be called if any of the exceptions
1623 of IPython itself, and it will be called if any of the exceptions
1624 listed in the exc_tuple are caught. If the handler is None, an
1624 listed in the exc_tuple are caught. If the handler is None, an
1625 internal basic one is used, which just prints basic info.
1625 internal basic one is used, which just prints basic info.
1626
1626
1627 To protect IPython from crashes, if your handler ever raises an
1627 To protect IPython from crashes, if your handler ever raises an
1628 exception or returns an invalid result, it will be immediately
1628 exception or returns an invalid result, it will be immediately
1629 disabled.
1629 disabled.
1630
1630
1631 WARNING: by putting in your own exception handler into IPython's main
1631 WARNING: by putting in your own exception handler into IPython's main
1632 execution loop, you run a very good chance of nasty crashes. This
1632 execution loop, you run a very good chance of nasty crashes. This
1633 facility should only be used if you really know what you are doing."""
1633 facility should only be used if you really know what you are doing."""
1634
1634
1635 assert type(exc_tuple)==type(()) , \
1635 assert type(exc_tuple)==type(()) , \
1636 "The custom exceptions must be given AS A TUPLE."
1636 "The custom exceptions must be given AS A TUPLE."
1637
1637
1638 def dummy_handler(self, etype, value, tb, tb_offset=None):
1638 def dummy_handler(self, etype, value, tb, tb_offset=None):
1639 print('*** Simple custom exception handler ***')
1639 print('*** Simple custom exception handler ***')
1640 print('Exception type :',etype)
1640 print('Exception type :',etype)
1641 print('Exception value:',value)
1641 print('Exception value:',value)
1642 print('Traceback :',tb)
1642 print('Traceback :',tb)
1643 #print 'Source code :','\n'.join(self.buffer)
1643 #print 'Source code :','\n'.join(self.buffer)
1644
1644
1645 def validate_stb(stb):
1645 def validate_stb(stb):
1646 """validate structured traceback return type
1646 """validate structured traceback return type
1647
1647
1648 return type of CustomTB *should* be a list of strings, but allow
1648 return type of CustomTB *should* be a list of strings, but allow
1649 single strings or None, which are harmless.
1649 single strings or None, which are harmless.
1650
1650
1651 This function will *always* return a list of strings,
1651 This function will *always* return a list of strings,
1652 and will raise a TypeError if stb is inappropriate.
1652 and will raise a TypeError if stb is inappropriate.
1653 """
1653 """
1654 msg = "CustomTB must return list of strings, not %r" % stb
1654 msg = "CustomTB must return list of strings, not %r" % stb
1655 if stb is None:
1655 if stb is None:
1656 return []
1656 return []
1657 elif isinstance(stb, string_types):
1657 elif isinstance(stb, string_types):
1658 return [stb]
1658 return [stb]
1659 elif not isinstance(stb, list):
1659 elif not isinstance(stb, list):
1660 raise TypeError(msg)
1660 raise TypeError(msg)
1661 # it's a list
1661 # it's a list
1662 for line in stb:
1662 for line in stb:
1663 # check every element
1663 # check every element
1664 if not isinstance(line, string_types):
1664 if not isinstance(line, string_types):
1665 raise TypeError(msg)
1665 raise TypeError(msg)
1666 return stb
1666 return stb
1667
1667
1668 if handler is None:
1668 if handler is None:
1669 wrapped = dummy_handler
1669 wrapped = dummy_handler
1670 else:
1670 else:
1671 def wrapped(self,etype,value,tb,tb_offset=None):
1671 def wrapped(self,etype,value,tb,tb_offset=None):
1672 """wrap CustomTB handler, to protect IPython from user code
1672 """wrap CustomTB handler, to protect IPython from user code
1673
1673
1674 This makes it harder (but not impossible) for custom exception
1674 This makes it harder (but not impossible) for custom exception
1675 handlers to crash IPython.
1675 handlers to crash IPython.
1676 """
1676 """
1677 try:
1677 try:
1678 stb = handler(self,etype,value,tb,tb_offset=tb_offset)
1678 stb = handler(self,etype,value,tb,tb_offset=tb_offset)
1679 return validate_stb(stb)
1679 return validate_stb(stb)
1680 except:
1680 except:
1681 # clear custom handler immediately
1681 # clear custom handler immediately
1682 self.set_custom_exc((), None)
1682 self.set_custom_exc((), None)
1683 print("Custom TB Handler failed, unregistering", file=sys.stderr)
1683 print("Custom TB Handler failed, unregistering", file=sys.stderr)
1684 # show the exception in handler first
1684 # show the exception in handler first
1685 stb = self.InteractiveTB.structured_traceback(*sys.exc_info())
1685 stb = self.InteractiveTB.structured_traceback(*sys.exc_info())
1686 print(self.InteractiveTB.stb2text(stb))
1686 print(self.InteractiveTB.stb2text(stb))
1687 print("The original exception:")
1687 print("The original exception:")
1688 stb = self.InteractiveTB.structured_traceback(
1688 stb = self.InteractiveTB.structured_traceback(
1689 (etype,value,tb), tb_offset=tb_offset
1689 (etype,value,tb), tb_offset=tb_offset
1690 )
1690 )
1691 return stb
1691 return stb
1692
1692
1693 self.CustomTB = types.MethodType(wrapped,self)
1693 self.CustomTB = types.MethodType(wrapped,self)
1694 self.custom_exceptions = exc_tuple
1694 self.custom_exceptions = exc_tuple
1695
1695
1696 def excepthook(self, etype, value, tb):
1696 def excepthook(self, etype, value, tb):
1697 """One more defense for GUI apps that call sys.excepthook.
1697 """One more defense for GUI apps that call sys.excepthook.
1698
1698
1699 GUI frameworks like wxPython trap exceptions and call
1699 GUI frameworks like wxPython trap exceptions and call
1700 sys.excepthook themselves. I guess this is a feature that
1700 sys.excepthook themselves. I guess this is a feature that
1701 enables them to keep running after exceptions that would
1701 enables them to keep running after exceptions that would
1702 otherwise kill their mainloop. This is a bother for IPython
1702 otherwise kill their mainloop. This is a bother for IPython
1703 which excepts to catch all of the program exceptions with a try:
1703 which excepts to catch all of the program exceptions with a try:
1704 except: statement.
1704 except: statement.
1705
1705
1706 Normally, IPython sets sys.excepthook to a CrashHandler instance, so if
1706 Normally, IPython sets sys.excepthook to a CrashHandler instance, so if
1707 any app directly invokes sys.excepthook, it will look to the user like
1707 any app directly invokes sys.excepthook, it will look to the user like
1708 IPython crashed. In order to work around this, we can disable the
1708 IPython crashed. In order to work around this, we can disable the
1709 CrashHandler and replace it with this excepthook instead, which prints a
1709 CrashHandler and replace it with this excepthook instead, which prints a
1710 regular traceback using our InteractiveTB. In this fashion, apps which
1710 regular traceback using our InteractiveTB. In this fashion, apps which
1711 call sys.excepthook will generate a regular-looking exception from
1711 call sys.excepthook will generate a regular-looking exception from
1712 IPython, and the CrashHandler will only be triggered by real IPython
1712 IPython, and the CrashHandler will only be triggered by real IPython
1713 crashes.
1713 crashes.
1714
1714
1715 This hook should be used sparingly, only in places which are not likely
1715 This hook should be used sparingly, only in places which are not likely
1716 to be true IPython errors.
1716 to be true IPython errors.
1717 """
1717 """
1718 self.showtraceback((etype, value, tb), tb_offset=0)
1718 self.showtraceback((etype, value, tb), tb_offset=0)
1719
1719
1720 def _get_exc_info(self, exc_tuple=None):
1720 def _get_exc_info(self, exc_tuple=None):
1721 """get exc_info from a given tuple, sys.exc_info() or sys.last_type etc.
1721 """get exc_info from a given tuple, sys.exc_info() or sys.last_type etc.
1722
1722
1723 Ensures sys.last_type,value,traceback hold the exc_info we found,
1723 Ensures sys.last_type,value,traceback hold the exc_info we found,
1724 from whichever source.
1724 from whichever source.
1725
1725
1726 raises ValueError if none of these contain any information
1726 raises ValueError if none of these contain any information
1727 """
1727 """
1728 if exc_tuple is None:
1728 if exc_tuple is None:
1729 etype, value, tb = sys.exc_info()
1729 etype, value, tb = sys.exc_info()
1730 else:
1730 else:
1731 etype, value, tb = exc_tuple
1731 etype, value, tb = exc_tuple
1732
1732
1733 if etype is None:
1733 if etype is None:
1734 if hasattr(sys, 'last_type'):
1734 if hasattr(sys, 'last_type'):
1735 etype, value, tb = sys.last_type, sys.last_value, \
1735 etype, value, tb = sys.last_type, sys.last_value, \
1736 sys.last_traceback
1736 sys.last_traceback
1737
1737
1738 if etype is None:
1738 if etype is None:
1739 raise ValueError("No exception to find")
1739 raise ValueError("No exception to find")
1740
1740
1741 # Now store the exception info in sys.last_type etc.
1741 # Now store the exception info in sys.last_type etc.
1742 # WARNING: these variables are somewhat deprecated and not
1742 # WARNING: these variables are somewhat deprecated and not
1743 # necessarily safe to use in a threaded environment, but tools
1743 # necessarily safe to use in a threaded environment, but tools
1744 # like pdb depend on their existence, so let's set them. If we
1744 # like pdb depend on their existence, so let's set them. If we
1745 # find problems in the field, we'll need to revisit their use.
1745 # find problems in the field, we'll need to revisit their use.
1746 sys.last_type = etype
1746 sys.last_type = etype
1747 sys.last_value = value
1747 sys.last_value = value
1748 sys.last_traceback = tb
1748 sys.last_traceback = tb
1749
1749
1750 return etype, value, tb
1750 return etype, value, tb
1751
1751
1752 def show_usage_error(self, exc):
1752 def show_usage_error(self, exc):
1753 """Show a short message for UsageErrors
1753 """Show a short message for UsageErrors
1754
1754
1755 These are special exceptions that shouldn't show a traceback.
1755 These are special exceptions that shouldn't show a traceback.
1756 """
1756 """
1757 print("UsageError: %s" % exc, file=sys.stderr)
1757 print("UsageError: %s" % exc, file=sys.stderr)
1758
1758
1759 def get_exception_only(self, exc_tuple=None):
1759 def get_exception_only(self, exc_tuple=None):
1760 """
1760 """
1761 Return as a string (ending with a newline) the exception that
1761 Return as a string (ending with a newline) the exception that
1762 just occurred, without any traceback.
1762 just occurred, without any traceback.
1763 """
1763 """
1764 etype, value, tb = self._get_exc_info(exc_tuple)
1764 etype, value, tb = self._get_exc_info(exc_tuple)
1765 msg = traceback.format_exception_only(etype, value)
1765 msg = traceback.format_exception_only(etype, value)
1766 return ''.join(msg)
1766 return ''.join(msg)
1767
1767
1768 def showtraceback(self, exc_tuple=None, filename=None, tb_offset=None,
1768 def showtraceback(self, exc_tuple=None, filename=None, tb_offset=None,
1769 exception_only=False):
1769 exception_only=False):
1770 """Display the exception that just occurred.
1770 """Display the exception that just occurred.
1771
1771
1772 If nothing is known about the exception, this is the method which
1772 If nothing is known about the exception, this is the method which
1773 should be used throughout the code for presenting user tracebacks,
1773 should be used throughout the code for presenting user tracebacks,
1774 rather than directly invoking the InteractiveTB object.
1774 rather than directly invoking the InteractiveTB object.
1775
1775
1776 A specific showsyntaxerror() also exists, but this method can take
1776 A specific showsyntaxerror() also exists, but this method can take
1777 care of calling it if needed, so unless you are explicitly catching a
1777 care of calling it if needed, so unless you are explicitly catching a
1778 SyntaxError exception, don't try to analyze the stack manually and
1778 SyntaxError exception, don't try to analyze the stack manually and
1779 simply call this method."""
1779 simply call this method."""
1780
1780
1781 try:
1781 try:
1782 try:
1782 try:
1783 etype, value, tb = self._get_exc_info(exc_tuple)
1783 etype, value, tb = self._get_exc_info(exc_tuple)
1784 except ValueError:
1784 except ValueError:
1785 print('No traceback available to show.', file=sys.stderr)
1785 print('No traceback available to show.', file=sys.stderr)
1786 return
1786 return
1787
1787
1788 if issubclass(etype, SyntaxError):
1788 if issubclass(etype, SyntaxError):
1789 # Though this won't be called by syntax errors in the input
1789 # Though this won't be called by syntax errors in the input
1790 # line, there may be SyntaxError cases with imported code.
1790 # line, there may be SyntaxError cases with imported code.
1791 self.showsyntaxerror(filename)
1791 self.showsyntaxerror(filename)
1792 elif etype is UsageError:
1792 elif etype is UsageError:
1793 self.show_usage_error(value)
1793 self.show_usage_error(value)
1794 else:
1794 else:
1795 if exception_only:
1795 if exception_only:
1796 stb = ['An exception has occurred, use %tb to see '
1796 stb = ['An exception has occurred, use %tb to see '
1797 'the full traceback.\n']
1797 'the full traceback.\n']
1798 stb.extend(self.InteractiveTB.get_exception_only(etype,
1798 stb.extend(self.InteractiveTB.get_exception_only(etype,
1799 value))
1799 value))
1800 else:
1800 else:
1801 try:
1801 try:
1802 # Exception classes can customise their traceback - we
1802 # Exception classes can customise their traceback - we
1803 # use this in IPython.parallel for exceptions occurring
1803 # use this in IPython.parallel for exceptions occurring
1804 # in the engines. This should return a list of strings.
1804 # in the engines. This should return a list of strings.
1805 stb = value._render_traceback_()
1805 stb = value._render_traceback_()
1806 except Exception:
1806 except Exception:
1807 stb = self.InteractiveTB.structured_traceback(etype,
1807 stb = self.InteractiveTB.structured_traceback(etype,
1808 value, tb, tb_offset=tb_offset)
1808 value, tb, tb_offset=tb_offset)
1809
1809
1810 self._showtraceback(etype, value, stb)
1810 self._showtraceback(etype, value, stb)
1811 if self.call_pdb:
1811 if self.call_pdb:
1812 # drop into debugger
1812 # drop into debugger
1813 self.debugger(force=True)
1813 self.debugger(force=True)
1814 return
1814 return
1815
1815
1816 # Actually show the traceback
1816 # Actually show the traceback
1817 self._showtraceback(etype, value, stb)
1817 self._showtraceback(etype, value, stb)
1818
1818
1819 except KeyboardInterrupt:
1819 except KeyboardInterrupt:
1820 print('\n' + self.get_exception_only(), file=sys.stderr)
1820 print('\n' + self.get_exception_only(), file=sys.stderr)
1821
1821
1822 def _showtraceback(self, etype, evalue, stb):
1822 def _showtraceback(self, etype, evalue, stb):
1823 """Actually show a traceback.
1823 """Actually show a traceback.
1824
1824
1825 Subclasses may override this method to put the traceback on a different
1825 Subclasses may override this method to put the traceback on a different
1826 place, like a side channel.
1826 place, like a side channel.
1827 """
1827 """
1828 print(self.InteractiveTB.stb2text(stb))
1828 print(self.InteractiveTB.stb2text(stb))
1829
1829
1830 def showsyntaxerror(self, filename=None):
1830 def showsyntaxerror(self, filename=None):
1831 """Display the syntax error that just occurred.
1831 """Display the syntax error that just occurred.
1832
1832
1833 This doesn't display a stack trace because there isn't one.
1833 This doesn't display a stack trace because there isn't one.
1834
1834
1835 If a filename is given, it is stuffed in the exception instead
1835 If a filename is given, it is stuffed in the exception instead
1836 of what was there before (because Python's parser always uses
1836 of what was there before (because Python's parser always uses
1837 "<string>" when reading from a string).
1837 "<string>" when reading from a string).
1838 """
1838 """
1839 etype, value, last_traceback = self._get_exc_info()
1839 etype, value, last_traceback = self._get_exc_info()
1840
1840
1841 if filename and issubclass(etype, SyntaxError):
1841 if filename and issubclass(etype, SyntaxError):
1842 try:
1842 try:
1843 value.filename = filename
1843 value.filename = filename
1844 except:
1844 except:
1845 # Not the format we expect; leave it alone
1845 # Not the format we expect; leave it alone
1846 pass
1846 pass
1847
1847
1848 stb = self.SyntaxTB.structured_traceback(etype, value, [])
1848 stb = self.SyntaxTB.structured_traceback(etype, value, [])
1849 self._showtraceback(etype, value, stb)
1849 self._showtraceback(etype, value, stb)
1850
1850
1851 # This is overridden in TerminalInteractiveShell to show a message about
1851 # This is overridden in TerminalInteractiveShell to show a message about
1852 # the %paste magic.
1852 # the %paste magic.
1853 def showindentationerror(self):
1853 def showindentationerror(self):
1854 """Called by run_cell when there's an IndentationError in code entered
1854 """Called by run_cell when there's an IndentationError in code entered
1855 at the prompt.
1855 at the prompt.
1856
1856
1857 This is overridden in TerminalInteractiveShell to show a message about
1857 This is overridden in TerminalInteractiveShell to show a message about
1858 the %paste magic."""
1858 the %paste magic."""
1859 self.showsyntaxerror()
1859 self.showsyntaxerror()
1860
1860
1861 #-------------------------------------------------------------------------
1861 #-------------------------------------------------------------------------
1862 # Things related to readline
1862 # Things related to readline
1863 #-------------------------------------------------------------------------
1863 #-------------------------------------------------------------------------
1864
1864
1865 def init_readline(self):
1865 def init_readline(self):
1866 """DEPRECATED
1866 """DEPRECATED
1867
1867
1868 Moved to terminal subclass, here only to simplify the init logic."""
1868 Moved to terminal subclass, here only to simplify the init logic."""
1869 # Set a number of methods that depend on readline to be no-op
1869 # Set a number of methods that depend on readline to be no-op
1870 warnings.warn('`init_readline` is no-op since IPython 5.0 and is Deprecated',
1870 warnings.warn('`init_readline` is no-op since IPython 5.0 and is Deprecated',
1871 DeprecationWarning, stacklevel=2)
1871 DeprecationWarning, stacklevel=2)
1872 self.set_custom_completer = no_op
1872 self.set_custom_completer = no_op
1873
1873
1874 @skip_doctest
1874 @skip_doctest
1875 def set_next_input(self, s, replace=False):
1875 def set_next_input(self, s, replace=False):
1876 """ Sets the 'default' input string for the next command line.
1876 """ Sets the 'default' input string for the next command line.
1877
1877
1878 Example::
1878 Example::
1879
1879
1880 In [1]: _ip.set_next_input("Hello Word")
1880 In [1]: _ip.set_next_input("Hello Word")
1881 In [2]: Hello Word_ # cursor is here
1881 In [2]: Hello Word_ # cursor is here
1882 """
1882 """
1883 self.rl_next_input = py3compat.cast_bytes_py2(s)
1883 self.rl_next_input = py3compat.cast_bytes_py2(s)
1884
1884
1885 def _indent_current_str(self):
1885 def _indent_current_str(self):
1886 """return the current level of indentation as a string"""
1886 """return the current level of indentation as a string"""
1887 return self.input_splitter.indent_spaces * ' '
1887 return self.input_splitter.indent_spaces * ' '
1888
1888
1889 #-------------------------------------------------------------------------
1889 #-------------------------------------------------------------------------
1890 # Things related to text completion
1890 # Things related to text completion
1891 #-------------------------------------------------------------------------
1891 #-------------------------------------------------------------------------
1892
1892
1893 def init_completer(self):
1893 def init_completer(self):
1894 """Initialize the completion machinery.
1894 """Initialize the completion machinery.
1895
1895
1896 This creates completion machinery that can be used by client code,
1896 This creates completion machinery that can be used by client code,
1897 either interactively in-process (typically triggered by the readline
1897 either interactively in-process (typically triggered by the readline
1898 library), programmatically (such as in test suites) or out-of-process
1898 library), programmatically (such as in test suites) or out-of-process
1899 (typically over the network by remote frontends).
1899 (typically over the network by remote frontends).
1900 """
1900 """
1901 from IPython.core.completer import IPCompleter
1901 from IPython.core.completer import IPCompleter
1902 from IPython.core.completerlib import (module_completer,
1902 from IPython.core.completerlib import (module_completer,
1903 magic_run_completer, cd_completer, reset_completer)
1903 magic_run_completer, cd_completer, reset_completer)
1904
1904
1905 self.Completer = IPCompleter(shell=self,
1905 self.Completer = IPCompleter(shell=self,
1906 namespace=self.user_ns,
1906 namespace=self.user_ns,
1907 global_namespace=self.user_global_ns,
1907 global_namespace=self.user_global_ns,
1908 use_readline=False,
1908 use_readline=False,
1909 parent=self,
1909 parent=self,
1910 )
1910 )
1911 self.configurables.append(self.Completer)
1911 self.configurables.append(self.Completer)
1912
1912
1913 # Add custom completers to the basic ones built into IPCompleter
1913 # Add custom completers to the basic ones built into IPCompleter
1914 sdisp = self.strdispatchers.get('complete_command', StrDispatch())
1914 sdisp = self.strdispatchers.get('complete_command', StrDispatch())
1915 self.strdispatchers['complete_command'] = sdisp
1915 self.strdispatchers['complete_command'] = sdisp
1916 self.Completer.custom_completers = sdisp
1916 self.Completer.custom_completers = sdisp
1917
1917
1918 self.set_hook('complete_command', module_completer, str_key = 'import')
1918 self.set_hook('complete_command', module_completer, str_key = 'import')
1919 self.set_hook('complete_command', module_completer, str_key = 'from')
1919 self.set_hook('complete_command', module_completer, str_key = 'from')
1920 self.set_hook('complete_command', module_completer, str_key = '%aimport')
1920 self.set_hook('complete_command', module_completer, str_key = '%aimport')
1921 self.set_hook('complete_command', magic_run_completer, str_key = '%run')
1921 self.set_hook('complete_command', magic_run_completer, str_key = '%run')
1922 self.set_hook('complete_command', cd_completer, str_key = '%cd')
1922 self.set_hook('complete_command', cd_completer, str_key = '%cd')
1923 self.set_hook('complete_command', reset_completer, str_key = '%reset')
1923 self.set_hook('complete_command', reset_completer, str_key = '%reset')
1924
1924
1925
1925
1926 @skip_doctest_py2
1926 @skip_doctest_py2
1927 def complete(self, text, line=None, cursor_pos=None):
1927 def complete(self, text, line=None, cursor_pos=None):
1928 """Return the completed text and a list of completions.
1928 """Return the completed text and a list of completions.
1929
1929
1930 Parameters
1930 Parameters
1931 ----------
1931 ----------
1932
1932
1933 text : string
1933 text : string
1934 A string of text to be completed on. It can be given as empty and
1934 A string of text to be completed on. It can be given as empty and
1935 instead a line/position pair are given. In this case, the
1935 instead a line/position pair are given. In this case, the
1936 completer itself will split the line like readline does.
1936 completer itself will split the line like readline does.
1937
1937
1938 line : string, optional
1938 line : string, optional
1939 The complete line that text is part of.
1939 The complete line that text is part of.
1940
1940
1941 cursor_pos : int, optional
1941 cursor_pos : int, optional
1942 The position of the cursor on the input line.
1942 The position of the cursor on the input line.
1943
1943
1944 Returns
1944 Returns
1945 -------
1945 -------
1946 text : string
1946 text : string
1947 The actual text that was completed.
1947 The actual text that was completed.
1948
1948
1949 matches : list
1949 matches : list
1950 A sorted list with all possible completions.
1950 A sorted list with all possible completions.
1951
1951
1952 The optional arguments allow the completion to take more context into
1952 The optional arguments allow the completion to take more context into
1953 account, and are part of the low-level completion API.
1953 account, and are part of the low-level completion API.
1954
1954
1955 This is a wrapper around the completion mechanism, similar to what
1955 This is a wrapper around the completion mechanism, similar to what
1956 readline does at the command line when the TAB key is hit. By
1956 readline does at the command line when the TAB key is hit. By
1957 exposing it as a method, it can be used by other non-readline
1957 exposing it as a method, it can be used by other non-readline
1958 environments (such as GUIs) for text completion.
1958 environments (such as GUIs) for text completion.
1959
1959
1960 Simple usage example:
1960 Simple usage example:
1961
1961
1962 In [1]: x = 'hello'
1962 In [1]: x = 'hello'
1963
1963
1964 In [2]: _ip.complete('x.l')
1964 In [2]: _ip.complete('x.l')
1965 Out[2]: ('x.l', ['x.ljust', 'x.lower', 'x.lstrip'])
1965 Out[2]: ('x.l', ['x.ljust', 'x.lower', 'x.lstrip'])
1966 """
1966 """
1967
1967
1968 # Inject names into __builtin__ so we can complete on the added names.
1968 # Inject names into __builtin__ so we can complete on the added names.
1969 with self.builtin_trap:
1969 with self.builtin_trap:
1970 return self.Completer.complete(text, line, cursor_pos)
1970 return self.Completer.complete(text, line, cursor_pos)
1971
1971
1972 def set_custom_completer(self, completer, pos=0):
1972 def set_custom_completer(self, completer, pos=0):
1973 """Adds a new custom completer function.
1973 """Adds a new custom completer function.
1974
1974
1975 The position argument (defaults to 0) is the index in the completers
1975 The position argument (defaults to 0) is the index in the completers
1976 list where you want the completer to be inserted."""
1976 list where you want the completer to be inserted."""
1977
1977
1978 newcomp = types.MethodType(completer,self.Completer)
1978 newcomp = types.MethodType(completer,self.Completer)
1979 self.Completer.matchers.insert(pos,newcomp)
1979 self.Completer.matchers.insert(pos,newcomp)
1980
1980
1981 def set_completer_frame(self, frame=None):
1981 def set_completer_frame(self, frame=None):
1982 """Set the frame of the completer."""
1982 """Set the frame of the completer."""
1983 if frame:
1983 if frame:
1984 self.Completer.namespace = frame.f_locals
1984 self.Completer.namespace = frame.f_locals
1985 self.Completer.global_namespace = frame.f_globals
1985 self.Completer.global_namespace = frame.f_globals
1986 else:
1986 else:
1987 self.Completer.namespace = self.user_ns
1987 self.Completer.namespace = self.user_ns
1988 self.Completer.global_namespace = self.user_global_ns
1988 self.Completer.global_namespace = self.user_global_ns
1989
1989
1990 #-------------------------------------------------------------------------
1990 #-------------------------------------------------------------------------
1991 # Things related to magics
1991 # Things related to magics
1992 #-------------------------------------------------------------------------
1992 #-------------------------------------------------------------------------
1993
1993
1994 def init_magics(self):
1994 def init_magics(self):
1995 from IPython.core import magics as m
1995 from IPython.core import magics as m
1996 self.magics_manager = magic.MagicsManager(shell=self,
1996 self.magics_manager = magic.MagicsManager(shell=self,
1997 parent=self,
1997 parent=self,
1998 user_magics=m.UserMagics(self))
1998 user_magics=m.UserMagics(self))
1999 self.configurables.append(self.magics_manager)
1999 self.configurables.append(self.magics_manager)
2000
2000
2001 # Expose as public API from the magics manager
2001 # Expose as public API from the magics manager
2002 self.register_magics = self.magics_manager.register
2002 self.register_magics = self.magics_manager.register
2003
2003
2004 self.register_magics(m.AutoMagics, m.BasicMagics, m.CodeMagics,
2004 self.register_magics(m.AutoMagics, m.BasicMagics, m.CodeMagics,
2005 m.ConfigMagics, m.DisplayMagics, m.ExecutionMagics,
2005 m.ConfigMagics, m.DisplayMagics, m.ExecutionMagics,
2006 m.ExtensionMagics, m.HistoryMagics, m.LoggingMagics,
2006 m.ExtensionMagics, m.HistoryMagics, m.LoggingMagics,
2007 m.NamespaceMagics, m.OSMagics, m.PylabMagics, m.ScriptMagics,
2007 m.NamespaceMagics, m.OSMagics, m.PylabMagics, m.ScriptMagics,
2008 )
2008 )
2009
2009
2010 # Register Magic Aliases
2010 # Register Magic Aliases
2011 mman = self.magics_manager
2011 mman = self.magics_manager
2012 # FIXME: magic aliases should be defined by the Magics classes
2012 # FIXME: magic aliases should be defined by the Magics classes
2013 # or in MagicsManager, not here
2013 # or in MagicsManager, not here
2014 mman.register_alias('ed', 'edit')
2014 mman.register_alias('ed', 'edit')
2015 mman.register_alias('hist', 'history')
2015 mman.register_alias('hist', 'history')
2016 mman.register_alias('rep', 'recall')
2016 mman.register_alias('rep', 'recall')
2017 mman.register_alias('SVG', 'svg', 'cell')
2017 mman.register_alias('SVG', 'svg', 'cell')
2018 mman.register_alias('HTML', 'html', 'cell')
2018 mman.register_alias('HTML', 'html', 'cell')
2019 mman.register_alias('file', 'writefile', 'cell')
2019 mman.register_alias('file', 'writefile', 'cell')
2020
2020
2021 # FIXME: Move the color initialization to the DisplayHook, which
2021 # FIXME: Move the color initialization to the DisplayHook, which
2022 # should be split into a prompt manager and displayhook. We probably
2022 # should be split into a prompt manager and displayhook. We probably
2023 # even need a centralize colors management object.
2023 # even need a centralize colors management object.
2024 self.magic('colors %s' % self.colors)
2024 self.magic('colors %s' % self.colors)
2025
2025
2026 # Defined here so that it's included in the documentation
2026 # Defined here so that it's included in the documentation
2027 @functools.wraps(magic.MagicsManager.register_function)
2027 @functools.wraps(magic.MagicsManager.register_function)
2028 def register_magic_function(self, func, magic_kind='line', magic_name=None):
2028 def register_magic_function(self, func, magic_kind='line', magic_name=None):
2029 self.magics_manager.register_function(func,
2029 self.magics_manager.register_function(func,
2030 magic_kind=magic_kind, magic_name=magic_name)
2030 magic_kind=magic_kind, magic_name=magic_name)
2031
2031
2032 def run_line_magic(self, magic_name, line):
2032 def run_line_magic(self, magic_name, line):
2033 """Execute the given line magic.
2033 """Execute the given line magic.
2034
2034
2035 Parameters
2035 Parameters
2036 ----------
2036 ----------
2037 magic_name : str
2037 magic_name : str
2038 Name of the desired magic function, without '%' prefix.
2038 Name of the desired magic function, without '%' prefix.
2039
2039
2040 line : str
2040 line : str
2041 The rest of the input line as a single string.
2041 The rest of the input line as a single string.
2042 """
2042 """
2043 fn = self.find_line_magic(magic_name)
2043 fn = self.find_line_magic(magic_name)
2044 if fn is None:
2044 if fn is None:
2045 cm = self.find_cell_magic(magic_name)
2045 cm = self.find_cell_magic(magic_name)
2046 etpl = "Line magic function `%%%s` not found%s."
2046 etpl = "Line magic function `%%%s` not found%s."
2047 extra = '' if cm is None else (' (But cell magic `%%%%%s` exists, '
2047 extra = '' if cm is None else (' (But cell magic `%%%%%s` exists, '
2048 'did you mean that instead?)' % magic_name )
2048 'did you mean that instead?)' % magic_name )
2049 error(etpl % (magic_name, extra))
2049 error(etpl % (magic_name, extra))
2050 else:
2050 else:
2051 # Note: this is the distance in the stack to the user's frame.
2051 # Note: this is the distance in the stack to the user's frame.
2052 # This will need to be updated if the internal calling logic gets
2052 # This will need to be updated if the internal calling logic gets
2053 # refactored, or else we'll be expanding the wrong variables.
2053 # refactored, or else we'll be expanding the wrong variables.
2054 stack_depth = 2
2054 stack_depth = 2
2055 magic_arg_s = self.var_expand(line, stack_depth)
2055 magic_arg_s = self.var_expand(line, stack_depth)
2056 # Put magic args in a list so we can call with f(*a) syntax
2056 # Put magic args in a list so we can call with f(*a) syntax
2057 args = [magic_arg_s]
2057 args = [magic_arg_s]
2058 kwargs = {}
2058 kwargs = {}
2059 # Grab local namespace if we need it:
2059 # Grab local namespace if we need it:
2060 if getattr(fn, "needs_local_scope", False):
2060 if getattr(fn, "needs_local_scope", False):
2061 kwargs['local_ns'] = sys._getframe(stack_depth).f_locals
2061 kwargs['local_ns'] = sys._getframe(stack_depth).f_locals
2062 with self.builtin_trap:
2062 with self.builtin_trap:
2063 result = fn(*args,**kwargs)
2063 result = fn(*args,**kwargs)
2064 return result
2064 return result
2065
2065
2066 def run_cell_magic(self, magic_name, line, cell):
2066 def run_cell_magic(self, magic_name, line, cell):
2067 """Execute the given cell magic.
2067 """Execute the given cell magic.
2068
2068
2069 Parameters
2069 Parameters
2070 ----------
2070 ----------
2071 magic_name : str
2071 magic_name : str
2072 Name of the desired magic function, without '%' prefix.
2072 Name of the desired magic function, without '%' prefix.
2073
2073
2074 line : str
2074 line : str
2075 The rest of the first input line as a single string.
2075 The rest of the first input line as a single string.
2076
2076
2077 cell : str
2077 cell : str
2078 The body of the cell as a (possibly multiline) string.
2078 The body of the cell as a (possibly multiline) string.
2079 """
2079 """
2080 fn = self.find_cell_magic(magic_name)
2080 fn = self.find_cell_magic(magic_name)
2081 if fn is None:
2081 if fn is None:
2082 lm = self.find_line_magic(magic_name)
2082 lm = self.find_line_magic(magic_name)
2083 etpl = "Cell magic `%%{0}` not found{1}."
2083 etpl = "Cell magic `%%{0}` not found{1}."
2084 extra = '' if lm is None else (' (But line magic `%{0}` exists, '
2084 extra = '' if lm is None else (' (But line magic `%{0}` exists, '
2085 'did you mean that instead?)'.format(magic_name))
2085 'did you mean that instead?)'.format(magic_name))
2086 error(etpl.format(magic_name, extra))
2086 error(etpl.format(magic_name, extra))
2087 elif cell == '':
2087 elif cell == '':
2088 message = '%%{0} is a cell magic, but the cell body is empty.'.format(magic_name)
2088 message = '%%{0} is a cell magic, but the cell body is empty.'.format(magic_name)
2089 if self.find_line_magic(magic_name) is not None:
2089 if self.find_line_magic(magic_name) is not None:
2090 message += ' Did you mean the line magic %{0} (single %)?'.format(magic_name)
2090 message += ' Did you mean the line magic %{0} (single %)?'.format(magic_name)
2091 raise UsageError(message)
2091 raise UsageError(message)
2092 else:
2092 else:
2093 # Note: this is the distance in the stack to the user's frame.
2093 # Note: this is the distance in the stack to the user's frame.
2094 # This will need to be updated if the internal calling logic gets
2094 # This will need to be updated if the internal calling logic gets
2095 # refactored, or else we'll be expanding the wrong variables.
2095 # refactored, or else we'll be expanding the wrong variables.
2096 stack_depth = 2
2096 stack_depth = 2
2097 magic_arg_s = self.var_expand(line, stack_depth)
2097 magic_arg_s = self.var_expand(line, stack_depth)
2098 with self.builtin_trap:
2098 with self.builtin_trap:
2099 result = fn(magic_arg_s, cell)
2099 result = fn(magic_arg_s, cell)
2100 return result
2100 return result
2101
2101
2102 def find_line_magic(self, magic_name):
2102 def find_line_magic(self, magic_name):
2103 """Find and return a line magic by name.
2103 """Find and return a line magic by name.
2104
2104
2105 Returns None if the magic isn't found."""
2105 Returns None if the magic isn't found."""
2106 return self.magics_manager.magics['line'].get(magic_name)
2106 return self.magics_manager.magics['line'].get(magic_name)
2107
2107
2108 def find_cell_magic(self, magic_name):
2108 def find_cell_magic(self, magic_name):
2109 """Find and return a cell magic by name.
2109 """Find and return a cell magic by name.
2110
2110
2111 Returns None if the magic isn't found."""
2111 Returns None if the magic isn't found."""
2112 return self.magics_manager.magics['cell'].get(magic_name)
2112 return self.magics_manager.magics['cell'].get(magic_name)
2113
2113
2114 def find_magic(self, magic_name, magic_kind='line'):
2114 def find_magic(self, magic_name, magic_kind='line'):
2115 """Find and return a magic of the given type by name.
2115 """Find and return a magic of the given type by name.
2116
2116
2117 Returns None if the magic isn't found."""
2117 Returns None if the magic isn't found."""
2118 return self.magics_manager.magics[magic_kind].get(magic_name)
2118 return self.magics_manager.magics[magic_kind].get(magic_name)
2119
2119
2120 def magic(self, arg_s):
2120 def magic(self, arg_s):
2121 """DEPRECATED. Use run_line_magic() instead.
2121 """DEPRECATED. Use run_line_magic() instead.
2122
2122
2123 Call a magic function by name.
2123 Call a magic function by name.
2124
2124
2125 Input: a string containing the name of the magic function to call and
2125 Input: a string containing the name of the magic function to call and
2126 any additional arguments to be passed to the magic.
2126 any additional arguments to be passed to the magic.
2127
2127
2128 magic('name -opt foo bar') is equivalent to typing at the ipython
2128 magic('name -opt foo bar') is equivalent to typing at the ipython
2129 prompt:
2129 prompt:
2130
2130
2131 In[1]: %name -opt foo bar
2131 In[1]: %name -opt foo bar
2132
2132
2133 To call a magic without arguments, simply use magic('name').
2133 To call a magic without arguments, simply use magic('name').
2134
2134
2135 This provides a proper Python function to call IPython's magics in any
2135 This provides a proper Python function to call IPython's magics in any
2136 valid Python code you can type at the interpreter, including loops and
2136 valid Python code you can type at the interpreter, including loops and
2137 compound statements.
2137 compound statements.
2138 """
2138 """
2139 # TODO: should we issue a loud deprecation warning here?
2139 # TODO: should we issue a loud deprecation warning here?
2140 magic_name, _, magic_arg_s = arg_s.partition(' ')
2140 magic_name, _, magic_arg_s = arg_s.partition(' ')
2141 magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
2141 magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
2142 return self.run_line_magic(magic_name, magic_arg_s)
2142 return self.run_line_magic(magic_name, magic_arg_s)
2143
2143
2144 #-------------------------------------------------------------------------
2144 #-------------------------------------------------------------------------
2145 # Things related to macros
2145 # Things related to macros
2146 #-------------------------------------------------------------------------
2146 #-------------------------------------------------------------------------
2147
2147
2148 def define_macro(self, name, themacro):
2148 def define_macro(self, name, themacro):
2149 """Define a new macro
2149 """Define a new macro
2150
2150
2151 Parameters
2151 Parameters
2152 ----------
2152 ----------
2153 name : str
2153 name : str
2154 The name of the macro.
2154 The name of the macro.
2155 themacro : str or Macro
2155 themacro : str or Macro
2156 The action to do upon invoking the macro. If a string, a new
2156 The action to do upon invoking the macro. If a string, a new
2157 Macro object is created by passing the string to it.
2157 Macro object is created by passing the string to it.
2158 """
2158 """
2159
2159
2160 from IPython.core import macro
2160 from IPython.core import macro
2161
2161
2162 if isinstance(themacro, string_types):
2162 if isinstance(themacro, string_types):
2163 themacro = macro.Macro(themacro)
2163 themacro = macro.Macro(themacro)
2164 if not isinstance(themacro, macro.Macro):
2164 if not isinstance(themacro, macro.Macro):
2165 raise ValueError('A macro must be a string or a Macro instance.')
2165 raise ValueError('A macro must be a string or a Macro instance.')
2166 self.user_ns[name] = themacro
2166 self.user_ns[name] = themacro
2167
2167
2168 #-------------------------------------------------------------------------
2168 #-------------------------------------------------------------------------
2169 # Things related to the running of system commands
2169 # Things related to the running of system commands
2170 #-------------------------------------------------------------------------
2170 #-------------------------------------------------------------------------
2171
2171
2172 def system_piped(self, cmd):
2172 def system_piped(self, cmd):
2173 """Call the given cmd in a subprocess, piping stdout/err
2173 """Call the given cmd in a subprocess, piping stdout/err
2174
2174
2175 Parameters
2175 Parameters
2176 ----------
2176 ----------
2177 cmd : str
2177 cmd : str
2178 Command to execute (can not end in '&', as background processes are
2178 Command to execute (can not end in '&', as background processes are
2179 not supported. Should not be a command that expects input
2179 not supported. Should not be a command that expects input
2180 other than simple text.
2180 other than simple text.
2181 """
2181 """
2182 if cmd.rstrip().endswith('&'):
2182 if cmd.rstrip().endswith('&'):
2183 # this is *far* from a rigorous test
2183 # this is *far* from a rigorous test
2184 # We do not support backgrounding processes because we either use
2184 # We do not support backgrounding processes because we either use
2185 # pexpect or pipes to read from. Users can always just call
2185 # pexpect or pipes to read from. Users can always just call
2186 # os.system() or use ip.system=ip.system_raw
2186 # os.system() or use ip.system=ip.system_raw
2187 # if they really want a background process.
2187 # if they really want a background process.
2188 raise OSError("Background processes not supported.")
2188 raise OSError("Background processes not supported.")
2189
2189
2190 # we explicitly do NOT return the subprocess status code, because
2190 # we explicitly do NOT return the subprocess status code, because
2191 # a non-None value would trigger :func:`sys.displayhook` calls.
2191 # a non-None value would trigger :func:`sys.displayhook` calls.
2192 # Instead, we store the exit_code in user_ns.
2192 # Instead, we store the exit_code in user_ns.
2193 self.user_ns['_exit_code'] = system(self.var_expand(cmd, depth=1))
2193 self.user_ns['_exit_code'] = system(self.var_expand(cmd, depth=1))
2194
2194
2195 def system_raw(self, cmd):
2195 def system_raw(self, cmd):
2196 """Call the given cmd in a subprocess using os.system on Windows or
2196 """Call the given cmd in a subprocess using os.system on Windows or
2197 subprocess.call using the system shell on other platforms.
2197 subprocess.call using the system shell on other platforms.
2198
2198
2199 Parameters
2199 Parameters
2200 ----------
2200 ----------
2201 cmd : str
2201 cmd : str
2202 Command to execute.
2202 Command to execute.
2203 """
2203 """
2204 cmd = self.var_expand(cmd, depth=1)
2204 cmd = self.var_expand(cmd, depth=1)
2205 # protect os.system from UNC paths on Windows, which it can't handle:
2205 # protect os.system from UNC paths on Windows, which it can't handle:
2206 if sys.platform == 'win32':
2206 if sys.platform == 'win32':
2207 from IPython.utils._process_win32 import AvoidUNCPath
2207 from IPython.utils._process_win32 import AvoidUNCPath
2208 with AvoidUNCPath() as path:
2208 with AvoidUNCPath() as path:
2209 if path is not None:
2209 if path is not None:
2210 cmd = '"pushd %s &&"%s' % (path, cmd)
2210 cmd = '"pushd %s &&"%s' % (path, cmd)
2211 cmd = py3compat.unicode_to_str(cmd)
2211 cmd = py3compat.unicode_to_str(cmd)
2212 try:
2212 try:
2213 ec = os.system(cmd)
2213 ec = os.system(cmd)
2214 except KeyboardInterrupt:
2214 except KeyboardInterrupt:
2215 print('\n' + self.get_exception_only(), file=sys.stderr)
2215 print('\n' + self.get_exception_only(), file=sys.stderr)
2216 ec = -2
2216 ec = -2
2217 else:
2217 else:
2218 cmd = py3compat.unicode_to_str(cmd)
2218 cmd = py3compat.unicode_to_str(cmd)
2219 # For posix the result of the subprocess.call() below is an exit
2219 # For posix the result of the subprocess.call() below is an exit
2220 # code, which by convention is zero for success, positive for
2220 # code, which by convention is zero for success, positive for
2221 # program failure. Exit codes above 128 are reserved for signals,
2221 # program failure. Exit codes above 128 are reserved for signals,
2222 # and the formula for converting a signal to an exit code is usually
2222 # and the formula for converting a signal to an exit code is usually
2223 # signal_number+128. To more easily differentiate between exit
2223 # signal_number+128. To more easily differentiate between exit
2224 # codes and signals, ipython uses negative numbers. For instance
2224 # codes and signals, ipython uses negative numbers. For instance
2225 # since control-c is signal 2 but exit code 130, ipython's
2225 # since control-c is signal 2 but exit code 130, ipython's
2226 # _exit_code variable will read -2. Note that some shells like
2226 # _exit_code variable will read -2. Note that some shells like
2227 # csh and fish don't follow sh/bash conventions for exit codes.
2227 # csh and fish don't follow sh/bash conventions for exit codes.
2228 executable = os.environ.get('SHELL', None)
2228 executable = os.environ.get('SHELL', None)
2229 try:
2229 try:
2230 # Use env shell instead of default /bin/sh
2230 # Use env shell instead of default /bin/sh
2231 ec = subprocess.call(cmd, shell=True, executable=executable)
2231 ec = subprocess.call(cmd, shell=True, executable=executable)
2232 except KeyboardInterrupt:
2232 except KeyboardInterrupt:
2233 # intercept control-C; a long traceback is not useful here
2233 # intercept control-C; a long traceback is not useful here
2234 print('\n' + self.get_exception_only(), file=sys.stderr)
2234 print('\n' + self.get_exception_only(), file=sys.stderr)
2235 ec = 130
2235 ec = 130
2236 if ec > 128:
2236 if ec > 128:
2237 ec = -(ec - 128)
2237 ec = -(ec - 128)
2238
2238
2239 # We explicitly do NOT return the subprocess status code, because
2239 # We explicitly do NOT return the subprocess status code, because
2240 # a non-None value would trigger :func:`sys.displayhook` calls.
2240 # a non-None value would trigger :func:`sys.displayhook` calls.
2241 # Instead, we store the exit_code in user_ns. Note the semantics
2241 # Instead, we store the exit_code in user_ns. Note the semantics
2242 # of _exit_code: for control-c, _exit_code == -signal.SIGNIT,
2242 # of _exit_code: for control-c, _exit_code == -signal.SIGNIT,
2243 # but raising SystemExit(_exit_code) will give status 254!
2243 # but raising SystemExit(_exit_code) will give status 254!
2244 self.user_ns['_exit_code'] = ec
2244 self.user_ns['_exit_code'] = ec
2245
2245
2246 # use piped system by default, because it is better behaved
2246 # use piped system by default, because it is better behaved
2247 system = system_piped
2247 system = system_piped
2248
2248
2249 def getoutput(self, cmd, split=True, depth=0):
2249 def getoutput(self, cmd, split=True, depth=0):
2250 """Get output (possibly including stderr) from a subprocess.
2250 """Get output (possibly including stderr) from a subprocess.
2251
2251
2252 Parameters
2252 Parameters
2253 ----------
2253 ----------
2254 cmd : str
2254 cmd : str
2255 Command to execute (can not end in '&', as background processes are
2255 Command to execute (can not end in '&', as background processes are
2256 not supported.
2256 not supported.
2257 split : bool, optional
2257 split : bool, optional
2258 If True, split the output into an IPython SList. Otherwise, an
2258 If True, split the output into an IPython SList. Otherwise, an
2259 IPython LSString is returned. These are objects similar to normal
2259 IPython LSString is returned. These are objects similar to normal
2260 lists and strings, with a few convenience attributes for easier
2260 lists and strings, with a few convenience attributes for easier
2261 manipulation of line-based output. You can use '?' on them for
2261 manipulation of line-based output. You can use '?' on them for
2262 details.
2262 details.
2263 depth : int, optional
2263 depth : int, optional
2264 How many frames above the caller are the local variables which should
2264 How many frames above the caller are the local variables which should
2265 be expanded in the command string? The default (0) assumes that the
2265 be expanded in the command string? The default (0) assumes that the
2266 expansion variables are in the stack frame calling this function.
2266 expansion variables are in the stack frame calling this function.
2267 """
2267 """
2268 if cmd.rstrip().endswith('&'):
2268 if cmd.rstrip().endswith('&'):
2269 # this is *far* from a rigorous test
2269 # this is *far* from a rigorous test
2270 raise OSError("Background processes not supported.")
2270 raise OSError("Background processes not supported.")
2271 out = getoutput(self.var_expand(cmd, depth=depth+1))
2271 out = getoutput(self.var_expand(cmd, depth=depth+1))
2272 if split:
2272 if split:
2273 out = SList(out.splitlines())
2273 out = SList(out.splitlines())
2274 else:
2274 else:
2275 out = LSString(out)
2275 out = LSString(out)
2276 return out
2276 return out
2277
2277
2278 #-------------------------------------------------------------------------
2278 #-------------------------------------------------------------------------
2279 # Things related to aliases
2279 # Things related to aliases
2280 #-------------------------------------------------------------------------
2280 #-------------------------------------------------------------------------
2281
2281
2282 def init_alias(self):
2282 def init_alias(self):
2283 self.alias_manager = AliasManager(shell=self, parent=self)
2283 self.alias_manager = AliasManager(shell=self, parent=self)
2284 self.configurables.append(self.alias_manager)
2284 self.configurables.append(self.alias_manager)
2285
2285
2286 #-------------------------------------------------------------------------
2286 #-------------------------------------------------------------------------
2287 # Things related to extensions
2287 # Things related to extensions
2288 #-------------------------------------------------------------------------
2288 #-------------------------------------------------------------------------
2289
2289
2290 def init_extension_manager(self):
2290 def init_extension_manager(self):
2291 self.extension_manager = ExtensionManager(shell=self, parent=self)
2291 self.extension_manager = ExtensionManager(shell=self, parent=self)
2292 self.configurables.append(self.extension_manager)
2292 self.configurables.append(self.extension_manager)
2293
2293
2294 #-------------------------------------------------------------------------
2294 #-------------------------------------------------------------------------
2295 # Things related to payloads
2295 # Things related to payloads
2296 #-------------------------------------------------------------------------
2296 #-------------------------------------------------------------------------
2297
2297
2298 def init_payload(self):
2298 def init_payload(self):
2299 self.payload_manager = PayloadManager(parent=self)
2299 self.payload_manager = PayloadManager(parent=self)
2300 self.configurables.append(self.payload_manager)
2300 self.configurables.append(self.payload_manager)
2301
2301
2302 #-------------------------------------------------------------------------
2302 #-------------------------------------------------------------------------
2303 # Things related to the prefilter
2303 # Things related to the prefilter
2304 #-------------------------------------------------------------------------
2304 #-------------------------------------------------------------------------
2305
2305
2306 def init_prefilter(self):
2306 def init_prefilter(self):
2307 self.prefilter_manager = PrefilterManager(shell=self, parent=self)
2307 self.prefilter_manager = PrefilterManager(shell=self, parent=self)
2308 self.configurables.append(self.prefilter_manager)
2308 self.configurables.append(self.prefilter_manager)
2309 # Ultimately this will be refactored in the new interpreter code, but
2309 # Ultimately this will be refactored in the new interpreter code, but
2310 # for now, we should expose the main prefilter method (there's legacy
2310 # for now, we should expose the main prefilter method (there's legacy
2311 # code out there that may rely on this).
2311 # code out there that may rely on this).
2312 self.prefilter = self.prefilter_manager.prefilter_lines
2312 self.prefilter = self.prefilter_manager.prefilter_lines
2313
2313
2314 def auto_rewrite_input(self, cmd):
2314 def auto_rewrite_input(self, cmd):
2315 """Print to the screen the rewritten form of the user's command.
2315 """Print to the screen the rewritten form of the user's command.
2316
2316
2317 This shows visual feedback by rewriting input lines that cause
2317 This shows visual feedback by rewriting input lines that cause
2318 automatic calling to kick in, like::
2318 automatic calling to kick in, like::
2319
2319
2320 /f x
2320 /f x
2321
2321
2322 into::
2322 into::
2323
2323
2324 ------> f(x)
2324 ------> f(x)
2325
2325
2326 after the user's input prompt. This helps the user understand that the
2326 after the user's input prompt. This helps the user understand that the
2327 input line was transformed automatically by IPython.
2327 input line was transformed automatically by IPython.
2328 """
2328 """
2329 if not self.show_rewritten_input:
2329 if not self.show_rewritten_input:
2330 return
2330 return
2331
2331
2332 # This is overridden in TerminalInteractiveShell to use fancy prompts
2332 # This is overridden in TerminalInteractiveShell to use fancy prompts
2333 print("------> " + cmd)
2333 print("------> " + cmd)
2334
2334
2335 #-------------------------------------------------------------------------
2335 #-------------------------------------------------------------------------
2336 # Things related to extracting values/expressions from kernel and user_ns
2336 # Things related to extracting values/expressions from kernel and user_ns
2337 #-------------------------------------------------------------------------
2337 #-------------------------------------------------------------------------
2338
2338
2339 def _user_obj_error(self):
2339 def _user_obj_error(self):
2340 """return simple exception dict
2340 """return simple exception dict
2341
2341
2342 for use in user_expressions
2342 for use in user_expressions
2343 """
2343 """
2344
2344
2345 etype, evalue, tb = self._get_exc_info()
2345 etype, evalue, tb = self._get_exc_info()
2346 stb = self.InteractiveTB.get_exception_only(etype, evalue)
2346 stb = self.InteractiveTB.get_exception_only(etype, evalue)
2347
2347
2348 exc_info = {
2348 exc_info = {
2349 u'status' : 'error',
2349 u'status' : 'error',
2350 u'traceback' : stb,
2350 u'traceback' : stb,
2351 u'ename' : unicode_type(etype.__name__),
2351 u'ename' : unicode_type(etype.__name__),
2352 u'evalue' : py3compat.safe_unicode(evalue),
2352 u'evalue' : py3compat.safe_unicode(evalue),
2353 }
2353 }
2354
2354
2355 return exc_info
2355 return exc_info
2356
2356
2357 def _format_user_obj(self, obj):
2357 def _format_user_obj(self, obj):
2358 """format a user object to display dict
2358 """format a user object to display dict
2359
2359
2360 for use in user_expressions
2360 for use in user_expressions
2361 """
2361 """
2362
2362
2363 data, md = self.display_formatter.format(obj)
2363 data, md = self.display_formatter.format(obj)
2364 value = {
2364 value = {
2365 'status' : 'ok',
2365 'status' : 'ok',
2366 'data' : data,
2366 'data' : data,
2367 'metadata' : md,
2367 'metadata' : md,
2368 }
2368 }
2369 return value
2369 return value
2370
2370
2371 def user_expressions(self, expressions):
2371 def user_expressions(self, expressions):
2372 """Evaluate a dict of expressions in the user's namespace.
2372 """Evaluate a dict of expressions in the user's namespace.
2373
2373
2374 Parameters
2374 Parameters
2375 ----------
2375 ----------
2376 expressions : dict
2376 expressions : dict
2377 A dict with string keys and string values. The expression values
2377 A dict with string keys and string values. The expression values
2378 should be valid Python expressions, each of which will be evaluated
2378 should be valid Python expressions, each of which will be evaluated
2379 in the user namespace.
2379 in the user namespace.
2380
2380
2381 Returns
2381 Returns
2382 -------
2382 -------
2383 A dict, keyed like the input expressions dict, with the rich mime-typed
2383 A dict, keyed like the input expressions dict, with the rich mime-typed
2384 display_data of each value.
2384 display_data of each value.
2385 """
2385 """
2386 out = {}
2386 out = {}
2387 user_ns = self.user_ns
2387 user_ns = self.user_ns
2388 global_ns = self.user_global_ns
2388 global_ns = self.user_global_ns
2389
2389
2390 for key, expr in iteritems(expressions):
2390 for key, expr in iteritems(expressions):
2391 try:
2391 try:
2392 value = self._format_user_obj(eval(expr, global_ns, user_ns))
2392 value = self._format_user_obj(eval(expr, global_ns, user_ns))
2393 except:
2393 except:
2394 value = self._user_obj_error()
2394 value = self._user_obj_error()
2395 out[key] = value
2395 out[key] = value
2396 return out
2396 return out
2397
2397
2398 #-------------------------------------------------------------------------
2398 #-------------------------------------------------------------------------
2399 # Things related to the running of code
2399 # Things related to the running of code
2400 #-------------------------------------------------------------------------
2400 #-------------------------------------------------------------------------
2401
2401
2402 def ex(self, cmd):
2402 def ex(self, cmd):
2403 """Execute a normal python statement in user namespace."""
2403 """Execute a normal python statement in user namespace."""
2404 with self.builtin_trap:
2404 with self.builtin_trap:
2405 exec(cmd, self.user_global_ns, self.user_ns)
2405 exec(cmd, self.user_global_ns, self.user_ns)
2406
2406
2407 def ev(self, expr):
2407 def ev(self, expr):
2408 """Evaluate python expression expr in user namespace.
2408 """Evaluate python expression expr in user namespace.
2409
2409
2410 Returns the result of evaluation
2410 Returns the result of evaluation
2411 """
2411 """
2412 with self.builtin_trap:
2412 with self.builtin_trap:
2413 return eval(expr, self.user_global_ns, self.user_ns)
2413 return eval(expr, self.user_global_ns, self.user_ns)
2414
2414
2415 def safe_execfile(self, fname, *where, **kw):
2415 def safe_execfile(self, fname, *where, **kw):
2416 """A safe version of the builtin execfile().
2416 """A safe version of the builtin execfile().
2417
2417
2418 This version will never throw an exception, but instead print
2418 This version will never throw an exception, but instead print
2419 helpful error messages to the screen. This only works on pure
2419 helpful error messages to the screen. This only works on pure
2420 Python files with the .py extension.
2420 Python files with the .py extension.
2421
2421
2422 Parameters
2422 Parameters
2423 ----------
2423 ----------
2424 fname : string
2424 fname : string
2425 The name of the file to be executed.
2425 The name of the file to be executed.
2426 where : tuple
2426 where : tuple
2427 One or two namespaces, passed to execfile() as (globals,locals).
2427 One or two namespaces, passed to execfile() as (globals,locals).
2428 If only one is given, it is passed as both.
2428 If only one is given, it is passed as both.
2429 exit_ignore : bool (False)
2429 exit_ignore : bool (False)
2430 If True, then silence SystemExit for non-zero status (it is always
2430 If True, then silence SystemExit for non-zero status (it is always
2431 silenced for zero status, as it is so common).
2431 silenced for zero status, as it is so common).
2432 raise_exceptions : bool (False)
2432 raise_exceptions : bool (False)
2433 If True raise exceptions everywhere. Meant for testing.
2433 If True raise exceptions everywhere. Meant for testing.
2434 shell_futures : bool (False)
2434 shell_futures : bool (False)
2435 If True, the code will share future statements with the interactive
2435 If True, the code will share future statements with the interactive
2436 shell. It will both be affected by previous __future__ imports, and
2436 shell. It will both be affected by previous __future__ imports, and
2437 any __future__ imports in the code will affect the shell. If False,
2437 any __future__ imports in the code will affect the shell. If False,
2438 __future__ imports are not shared in either direction.
2438 __future__ imports are not shared in either direction.
2439
2439
2440 """
2440 """
2441 kw.setdefault('exit_ignore', False)
2441 kw.setdefault('exit_ignore', False)
2442 kw.setdefault('raise_exceptions', False)
2442 kw.setdefault('raise_exceptions', False)
2443 kw.setdefault('shell_futures', False)
2443 kw.setdefault('shell_futures', False)
2444
2444
2445 fname = os.path.abspath(os.path.expanduser(fname))
2445 fname = os.path.abspath(os.path.expanduser(fname))
2446
2446
2447 # Make sure we can open the file
2447 # Make sure we can open the file
2448 try:
2448 try:
2449 with open(fname):
2449 with open(fname):
2450 pass
2450 pass
2451 except:
2451 except:
2452 warn('Could not open file <%s> for safe execution.' % fname)
2452 warn('Could not open file <%s> for safe execution.' % fname)
2453 return
2453 return
2454
2454
2455 # Find things also in current directory. This is needed to mimic the
2455 # Find things also in current directory. This is needed to mimic the
2456 # behavior of running a script from the system command line, where
2456 # behavior of running a script from the system command line, where
2457 # Python inserts the script's directory into sys.path
2457 # Python inserts the script's directory into sys.path
2458 dname = os.path.dirname(fname)
2458 dname = os.path.dirname(fname)
2459
2459
2460 with prepended_to_syspath(dname), self.builtin_trap:
2460 with prepended_to_syspath(dname), self.builtin_trap:
2461 try:
2461 try:
2462 glob, loc = (where + (None, ))[:2]
2462 glob, loc = (where + (None, ))[:2]
2463 py3compat.execfile(
2463 py3compat.execfile(
2464 fname, glob, loc,
2464 fname, glob, loc,
2465 self.compile if kw['shell_futures'] else None)
2465 self.compile if kw['shell_futures'] else None)
2466 except SystemExit as status:
2466 except SystemExit as status:
2467 # If the call was made with 0 or None exit status (sys.exit(0)
2467 # If the call was made with 0 or None exit status (sys.exit(0)
2468 # or sys.exit() ), don't bother showing a traceback, as both of
2468 # or sys.exit() ), don't bother showing a traceback, as both of
2469 # these are considered normal by the OS:
2469 # these are considered normal by the OS:
2470 # > python -c'import sys;sys.exit(0)'; echo $?
2470 # > python -c'import sys;sys.exit(0)'; echo $?
2471 # 0
2471 # 0
2472 # > python -c'import sys;sys.exit()'; echo $?
2472 # > python -c'import sys;sys.exit()'; echo $?
2473 # 0
2473 # 0
2474 # For other exit status, we show the exception unless
2474 # For other exit status, we show the exception unless
2475 # explicitly silenced, but only in short form.
2475 # explicitly silenced, but only in short form.
2476 if status.code:
2476 if status.code:
2477 if kw['raise_exceptions']:
2477 if kw['raise_exceptions']:
2478 raise
2478 raise
2479 if not kw['exit_ignore']:
2479 if not kw['exit_ignore']:
2480 self.showtraceback(exception_only=True)
2480 self.showtraceback(exception_only=True)
2481 except:
2481 except:
2482 if kw['raise_exceptions']:
2482 if kw['raise_exceptions']:
2483 raise
2483 raise
2484 # tb offset is 2 because we wrap execfile
2484 # tb offset is 2 because we wrap execfile
2485 self.showtraceback(tb_offset=2)
2485 self.showtraceback(tb_offset=2)
2486
2486
2487 def safe_execfile_ipy(self, fname, shell_futures=False, raise_exceptions=False):
2487 def safe_execfile_ipy(self, fname, shell_futures=False, raise_exceptions=False):
2488 """Like safe_execfile, but for .ipy or .ipynb files with IPython syntax.
2488 """Like safe_execfile, but for .ipy or .ipynb files with IPython syntax.
2489
2489
2490 Parameters
2490 Parameters
2491 ----------
2491 ----------
2492 fname : str
2492 fname : str
2493 The name of the file to execute. The filename must have a
2493 The name of the file to execute. The filename must have a
2494 .ipy or .ipynb extension.
2494 .ipy or .ipynb extension.
2495 shell_futures : bool (False)
2495 shell_futures : bool (False)
2496 If True, the code will share future statements with the interactive
2496 If True, the code will share future statements with the interactive
2497 shell. It will both be affected by previous __future__ imports, and
2497 shell. It will both be affected by previous __future__ imports, and
2498 any __future__ imports in the code will affect the shell. If False,
2498 any __future__ imports in the code will affect the shell. If False,
2499 __future__ imports are not shared in either direction.
2499 __future__ imports are not shared in either direction.
2500 raise_exceptions : bool (False)
2500 raise_exceptions : bool (False)
2501 If True raise exceptions everywhere. Meant for testing.
2501 If True raise exceptions everywhere. Meant for testing.
2502 """
2502 """
2503 fname = os.path.abspath(os.path.expanduser(fname))
2503 fname = os.path.abspath(os.path.expanduser(fname))
2504
2504
2505 # Make sure we can open the file
2505 # Make sure we can open the file
2506 try:
2506 try:
2507 with open(fname):
2507 with open(fname):
2508 pass
2508 pass
2509 except:
2509 except:
2510 warn('Could not open file <%s> for safe execution.' % fname)
2510 warn('Could not open file <%s> for safe execution.' % fname)
2511 return
2511 return
2512
2512
2513 # Find things also in current directory. This is needed to mimic the
2513 # Find things also in current directory. This is needed to mimic the
2514 # behavior of running a script from the system command line, where
2514 # behavior of running a script from the system command line, where
2515 # Python inserts the script's directory into sys.path
2515 # Python inserts the script's directory into sys.path
2516 dname = os.path.dirname(fname)
2516 dname = os.path.dirname(fname)
2517
2517
2518 def get_cells():
2518 def get_cells():
2519 """generator for sequence of code blocks to run"""
2519 """generator for sequence of code blocks to run"""
2520 if fname.endswith('.ipynb'):
2520 if fname.endswith('.ipynb'):
2521 from nbformat import read
2521 from nbformat import read
2522 with io_open(fname) as f:
2522 with io_open(fname) as f:
2523 nb = read(f, as_version=4)
2523 nb = read(f, as_version=4)
2524 if not nb.cells:
2524 if not nb.cells:
2525 return
2525 return
2526 for cell in nb.cells:
2526 for cell in nb.cells:
2527 if cell.cell_type == 'code':
2527 if cell.cell_type == 'code':
2528 yield cell.source
2528 yield cell.source
2529 else:
2529 else:
2530 with open(fname) as f:
2530 with open(fname) as f:
2531 yield f.read()
2531 yield f.read()
2532
2532
2533 with prepended_to_syspath(dname):
2533 with prepended_to_syspath(dname):
2534 try:
2534 try:
2535 for cell in get_cells():
2535 for cell in get_cells():
2536 result = self.run_cell(cell, silent=True, shell_futures=shell_futures)
2536 result = self.run_cell(cell, silent=True, shell_futures=shell_futures)
2537 if raise_exceptions:
2537 if raise_exceptions:
2538 result.raise_error()
2538 result.raise_error()
2539 elif not result.success:
2539 elif not result.success:
2540 break
2540 break
2541 except:
2541 except:
2542 if raise_exceptions:
2542 if raise_exceptions:
2543 raise
2543 raise
2544 self.showtraceback()
2544 self.showtraceback()
2545 warn('Unknown failure executing file: <%s>' % fname)
2545 warn('Unknown failure executing file: <%s>' % fname)
2546
2546
2547 def safe_run_module(self, mod_name, where):
2547 def safe_run_module(self, mod_name, where):
2548 """A safe version of runpy.run_module().
2548 """A safe version of runpy.run_module().
2549
2549
2550 This version will never throw an exception, but instead print
2550 This version will never throw an exception, but instead print
2551 helpful error messages to the screen.
2551 helpful error messages to the screen.
2552
2552
2553 `SystemExit` exceptions with status code 0 or None are ignored.
2553 `SystemExit` exceptions with status code 0 or None are ignored.
2554
2554
2555 Parameters
2555 Parameters
2556 ----------
2556 ----------
2557 mod_name : string
2557 mod_name : string
2558 The name of the module to be executed.
2558 The name of the module to be executed.
2559 where : dict
2559 where : dict
2560 The globals namespace.
2560 The globals namespace.
2561 """
2561 """
2562 try:
2562 try:
2563 try:
2563 try:
2564 where.update(
2564 where.update(
2565 runpy.run_module(str(mod_name), run_name="__main__",
2565 runpy.run_module(str(mod_name), run_name="__main__",
2566 alter_sys=True)
2566 alter_sys=True)
2567 )
2567 )
2568 except SystemExit as status:
2568 except SystemExit as status:
2569 if status.code:
2569 if status.code:
2570 raise
2570 raise
2571 except:
2571 except:
2572 self.showtraceback()
2572 self.showtraceback()
2573 warn('Unknown failure executing module: <%s>' % mod_name)
2573 warn('Unknown failure executing module: <%s>' % mod_name)
2574
2574
2575 def run_cell(self, raw_cell, store_history=False, silent=False, shell_futures=True):
2575 def run_cell(self, raw_cell, store_history=False, silent=False, shell_futures=True):
2576 """Run a complete IPython cell.
2576 """Run a complete IPython cell.
2577
2577
2578 Parameters
2578 Parameters
2579 ----------
2579 ----------
2580 raw_cell : str
2580 raw_cell : str
2581 The code (including IPython code such as %magic functions) to run.
2581 The code (including IPython code such as %magic functions) to run.
2582 store_history : bool
2582 store_history : bool
2583 If True, the raw and translated cell will be stored in IPython's
2583 If True, the raw and translated cell will be stored in IPython's
2584 history. For user code calling back into IPython's machinery, this
2584 history. For user code calling back into IPython's machinery, this
2585 should be set to False.
2585 should be set to False.
2586 silent : bool
2586 silent : bool
2587 If True, avoid side-effects, such as implicit displayhooks and
2587 If True, avoid side-effects, such as implicit displayhooks and
2588 and logging. silent=True forces store_history=False.
2588 and logging. silent=True forces store_history=False.
2589 shell_futures : bool
2589 shell_futures : bool
2590 If True, the code will share future statements with the interactive
2590 If True, the code will share future statements with the interactive
2591 shell. It will both be affected by previous __future__ imports, and
2591 shell. It will both be affected by previous __future__ imports, and
2592 any __future__ imports in the code will affect the shell. If False,
2592 any __future__ imports in the code will affect the shell. If False,
2593 __future__ imports are not shared in either direction.
2593 __future__ imports are not shared in either direction.
2594
2594
2595 Returns
2595 Returns
2596 -------
2596 -------
2597 result : :class:`ExecutionResult`
2597 result : :class:`ExecutionResult`
2598 """
2598 """
2599 result = ExecutionResult()
2599 result = ExecutionResult()
2600
2600
2601 if (not raw_cell) or raw_cell.isspace():
2601 if (not raw_cell) or raw_cell.isspace():
2602 self.last_execution_succeeded = True
2602 self.last_execution_succeeded = True
2603 return result
2603 return result
2604
2604
2605 if silent:
2605 if silent:
2606 store_history = False
2606 store_history = False
2607
2607
2608 if store_history:
2608 if store_history:
2609 result.execution_count = self.execution_count
2609 result.execution_count = self.execution_count
2610
2610
2611 def error_before_exec(value):
2611 def error_before_exec(value):
2612 result.error_before_exec = value
2612 result.error_before_exec = value
2613 self.last_execution_succeeded = False
2613 self.last_execution_succeeded = False
2614 return result
2614 return result
2615
2615
2616 self.events.trigger('pre_execute')
2616 self.events.trigger('pre_execute')
2617 if not silent:
2617 if not silent:
2618 self.events.trigger('pre_run_cell')
2618 self.events.trigger('pre_run_cell')
2619
2619
2620 # If any of our input transformation (input_transformer_manager or
2620 # If any of our input transformation (input_transformer_manager or
2621 # prefilter_manager) raises an exception, we store it in this variable
2621 # prefilter_manager) raises an exception, we store it in this variable
2622 # so that we can display the error after logging the input and storing
2622 # so that we can display the error after logging the input and storing
2623 # it in the history.
2623 # it in the history.
2624 preprocessing_exc_tuple = None
2624 preprocessing_exc_tuple = None
2625 try:
2625 try:
2626 # Static input transformations
2626 # Static input transformations
2627 cell = self.input_transformer_manager.transform_cell(raw_cell)
2627 cell = self.input_transformer_manager.transform_cell(raw_cell)
2628 except SyntaxError:
2628 except SyntaxError:
2629 preprocessing_exc_tuple = sys.exc_info()
2629 preprocessing_exc_tuple = sys.exc_info()
2630 cell = raw_cell # cell has to exist so it can be stored/logged
2630 cell = raw_cell # cell has to exist so it can be stored/logged
2631 else:
2631 else:
2632 if len(cell.splitlines()) == 1:
2632 if len(cell.splitlines()) == 1:
2633 # Dynamic transformations - only applied for single line commands
2633 # Dynamic transformations - only applied for single line commands
2634 with self.builtin_trap:
2634 with self.builtin_trap:
2635 try:
2635 try:
2636 # use prefilter_lines to handle trailing newlines
2636 # use prefilter_lines to handle trailing newlines
2637 # restore trailing newline for ast.parse
2637 # restore trailing newline for ast.parse
2638 cell = self.prefilter_manager.prefilter_lines(cell) + '\n'
2638 cell = self.prefilter_manager.prefilter_lines(cell) + '\n'
2639 except Exception:
2639 except Exception:
2640 # don't allow prefilter errors to crash IPython
2640 # don't allow prefilter errors to crash IPython
2641 preprocessing_exc_tuple = sys.exc_info()
2641 preprocessing_exc_tuple = sys.exc_info()
2642
2642
2643 # Store raw and processed history
2643 # Store raw and processed history
2644 if store_history:
2644 if store_history:
2645 self.history_manager.store_inputs(self.execution_count,
2645 self.history_manager.store_inputs(self.execution_count,
2646 cell, raw_cell)
2646 cell, raw_cell)
2647 if not silent:
2647 if not silent:
2648 self.logger.log(cell, raw_cell)
2648 self.logger.log(cell, raw_cell)
2649
2649
2650 # Display the exception if input processing failed.
2650 # Display the exception if input processing failed.
2651 if preprocessing_exc_tuple is not None:
2651 if preprocessing_exc_tuple is not None:
2652 self.showtraceback(preprocessing_exc_tuple)
2652 self.showtraceback(preprocessing_exc_tuple)
2653 if store_history:
2653 if store_history:
2654 self.execution_count += 1
2654 self.execution_count += 1
2655 return error_before_exec(preprocessing_exc_tuple[2])
2655 return error_before_exec(preprocessing_exc_tuple[2])
2656
2656
2657 # Our own compiler remembers the __future__ environment. If we want to
2657 # Our own compiler remembers the __future__ environment. If we want to
2658 # run code with a separate __future__ environment, use the default
2658 # run code with a separate __future__ environment, use the default
2659 # compiler
2659 # compiler
2660 compiler = self.compile if shell_futures else CachingCompiler()
2660 compiler = self.compile if shell_futures else CachingCompiler()
2661
2661
2662 with self.builtin_trap:
2662 with self.builtin_trap:
2663 cell_name = self.compile.cache(cell, self.execution_count)
2663 cell_name = self.compile.cache(cell, self.execution_count)
2664
2664
2665 with self.display_trap:
2665 with self.display_trap:
2666 # Compile to bytecode
2666 # Compile to bytecode
2667 try:
2667 try:
2668 code_ast = compiler.ast_parse(cell, filename=cell_name)
2668 code_ast = compiler.ast_parse(cell, filename=cell_name)
2669 except self.custom_exceptions as e:
2669 except self.custom_exceptions as e:
2670 etype, value, tb = sys.exc_info()
2670 etype, value, tb = sys.exc_info()
2671 self.CustomTB(etype, value, tb)
2671 self.CustomTB(etype, value, tb)
2672 return error_before_exec(e)
2672 return error_before_exec(e)
2673 except IndentationError as e:
2673 except IndentationError as e:
2674 self.showindentationerror()
2674 self.showindentationerror()
2675 if store_history:
2675 if store_history:
2676 self.execution_count += 1
2676 self.execution_count += 1
2677 return error_before_exec(e)
2677 return error_before_exec(e)
2678 except (OverflowError, SyntaxError, ValueError, TypeError,
2678 except (OverflowError, SyntaxError, ValueError, TypeError,
2679 MemoryError) as e:
2679 MemoryError) as e:
2680 self.showsyntaxerror()
2680 self.showsyntaxerror()
2681 if store_history:
2681 if store_history:
2682 self.execution_count += 1
2682 self.execution_count += 1
2683 return error_before_exec(e)
2683 return error_before_exec(e)
2684
2684
2685 # Apply AST transformations
2685 # Apply AST transformations
2686 try:
2686 try:
2687 code_ast = self.transform_ast(code_ast)
2687 code_ast = self.transform_ast(code_ast)
2688 except InputRejected as e:
2688 except InputRejected as e:
2689 self.showtraceback()
2689 self.showtraceback()
2690 if store_history:
2690 if store_history:
2691 self.execution_count += 1
2691 self.execution_count += 1
2692 return error_before_exec(e)
2692 return error_before_exec(e)
2693
2693
2694 # Give the displayhook a reference to our ExecutionResult so it
2694 # Give the displayhook a reference to our ExecutionResult so it
2695 # can fill in the output value.
2695 # can fill in the output value.
2696 self.displayhook.exec_result = result
2696 self.displayhook.exec_result = result
2697
2697
2698 # Execute the user code
2698 # Execute the user code
2699 interactivity = "none" if silent else self.ast_node_interactivity
2699 interactivity = "none" if silent else self.ast_node_interactivity
2700 has_raised = self.run_ast_nodes(code_ast.body, cell_name,
2700 has_raised = self.run_ast_nodes(code_ast.body, cell_name,
2701 interactivity=interactivity, compiler=compiler, result=result)
2701 interactivity=interactivity, compiler=compiler, result=result)
2702
2702
2703 self.last_execution_succeeded = not has_raised
2703 self.last_execution_succeeded = not has_raised
2704
2704
2705 # Reset this so later displayed values do not modify the
2705 # Reset this so later displayed values do not modify the
2706 # ExecutionResult
2706 # ExecutionResult
2707 self.displayhook.exec_result = None
2707 self.displayhook.exec_result = None
2708
2708
2709 self.events.trigger('post_execute')
2709 self.events.trigger('post_execute')
2710 if not silent:
2710 if not silent:
2711 self.events.trigger('post_run_cell')
2711 self.events.trigger('post_run_cell')
2712
2712
2713 if store_history:
2713 if store_history:
2714 # Write output to the database. Does nothing unless
2714 # Write output to the database. Does nothing unless
2715 # history output logging is enabled.
2715 # history output logging is enabled.
2716 self.history_manager.store_output(self.execution_count)
2716 self.history_manager.store_output(self.execution_count)
2717 # Each cell is a *single* input, regardless of how many lines it has
2717 # Each cell is a *single* input, regardless of how many lines it has
2718 self.execution_count += 1
2718 self.execution_count += 1
2719
2719
2720 return result
2720 return result
2721
2721
2722 def transform_ast(self, node):
2722 def transform_ast(self, node):
2723 """Apply the AST transformations from self.ast_transformers
2723 """Apply the AST transformations from self.ast_transformers
2724
2724
2725 Parameters
2725 Parameters
2726 ----------
2726 ----------
2727 node : ast.Node
2727 node : ast.Node
2728 The root node to be transformed. Typically called with the ast.Module
2728 The root node to be transformed. Typically called with the ast.Module
2729 produced by parsing user input.
2729 produced by parsing user input.
2730
2730
2731 Returns
2731 Returns
2732 -------
2732 -------
2733 An ast.Node corresponding to the node it was called with. Note that it
2733 An ast.Node corresponding to the node it was called with. Note that it
2734 may also modify the passed object, so don't rely on references to the
2734 may also modify the passed object, so don't rely on references to the
2735 original AST.
2735 original AST.
2736 """
2736 """
2737 for transformer in self.ast_transformers:
2737 for transformer in self.ast_transformers:
2738 try:
2738 try:
2739 node = transformer.visit(node)
2739 node = transformer.visit(node)
2740 except InputRejected:
2740 except InputRejected:
2741 # User-supplied AST transformers can reject an input by raising
2741 # User-supplied AST transformers can reject an input by raising
2742 # an InputRejected. Short-circuit in this case so that we
2742 # an InputRejected. Short-circuit in this case so that we
2743 # don't unregister the transform.
2743 # don't unregister the transform.
2744 raise
2744 raise
2745 except Exception:
2745 except Exception:
2746 warn("AST transformer %r threw an error. It will be unregistered." % transformer)
2746 warn("AST transformer %r threw an error. It will be unregistered." % transformer)
2747 self.ast_transformers.remove(transformer)
2747 self.ast_transformers.remove(transformer)
2748
2748
2749 if self.ast_transformers:
2749 if self.ast_transformers:
2750 ast.fix_missing_locations(node)
2750 ast.fix_missing_locations(node)
2751 return node
2751 return node
2752
2752
2753
2753
2754 def run_ast_nodes(self, nodelist, cell_name, interactivity='last_expr',
2754 def run_ast_nodes(self, nodelist, cell_name, interactivity='last_expr',
2755 compiler=compile, result=None):
2755 compiler=compile, result=None):
2756 """Run a sequence of AST nodes. The execution mode depends on the
2756 """Run a sequence of AST nodes. The execution mode depends on the
2757 interactivity parameter.
2757 interactivity parameter.
2758
2758
2759 Parameters
2759 Parameters
2760 ----------
2760 ----------
2761 nodelist : list
2761 nodelist : list
2762 A sequence of AST nodes to run.
2762 A sequence of AST nodes to run.
2763 cell_name : str
2763 cell_name : str
2764 Will be passed to the compiler as the filename of the cell. Typically
2764 Will be passed to the compiler as the filename of the cell. Typically
2765 the value returned by ip.compile.cache(cell).
2765 the value returned by ip.compile.cache(cell).
2766 interactivity : str
2766 interactivity : str
2767 'all', 'last', 'last_expr' or 'none', specifying which nodes should be
2767 'all', 'last', 'last_expr' or 'none', specifying which nodes should be
2768 run interactively (displaying output from expressions). 'last_expr'
2768 run interactively (displaying output from expressions). 'last_expr'
2769 will run the last node interactively only if it is an expression (i.e.
2769 will run the last node interactively only if it is an expression (i.e.
2770 expressions in loops or other blocks are not displayed. Other values
2770 expressions in loops or other blocks are not displayed. Other values
2771 for this parameter will raise a ValueError.
2771 for this parameter will raise a ValueError.
2772 compiler : callable
2772 compiler : callable
2773 A function with the same interface as the built-in compile(), to turn
2773 A function with the same interface as the built-in compile(), to turn
2774 the AST nodes into code objects. Default is the built-in compile().
2774 the AST nodes into code objects. Default is the built-in compile().
2775 result : ExecutionResult, optional
2775 result : ExecutionResult, optional
2776 An object to store exceptions that occur during execution.
2776 An object to store exceptions that occur during execution.
2777
2777
2778 Returns
2778 Returns
2779 -------
2779 -------
2780 True if an exception occurred while running code, False if it finished
2780 True if an exception occurred while running code, False if it finished
2781 running.
2781 running.
2782 """
2782 """
2783 if not nodelist:
2783 if not nodelist:
2784 return
2784 return
2785
2785
2786 if interactivity == 'last_expr':
2786 if interactivity == 'last_expr':
2787 if isinstance(nodelist[-1], ast.Expr):
2787 if isinstance(nodelist[-1], ast.Expr):
2788 interactivity = "last"
2788 interactivity = "last"
2789 else:
2789 else:
2790 interactivity = "none"
2790 interactivity = "none"
2791
2791
2792 if interactivity == 'none':
2792 if interactivity == 'none':
2793 to_run_exec, to_run_interactive = nodelist, []
2793 to_run_exec, to_run_interactive = nodelist, []
2794 elif interactivity == 'last':
2794 elif interactivity == 'last':
2795 to_run_exec, to_run_interactive = nodelist[:-1], nodelist[-1:]
2795 to_run_exec, to_run_interactive = nodelist[:-1], nodelist[-1:]
2796 elif interactivity == 'all':
2796 elif interactivity == 'all':
2797 to_run_exec, to_run_interactive = [], nodelist
2797 to_run_exec, to_run_interactive = [], nodelist
2798 else:
2798 else:
2799 raise ValueError("Interactivity was %r" % interactivity)
2799 raise ValueError("Interactivity was %r" % interactivity)
2800
2800
2801 try:
2801 try:
2802 for i, node in enumerate(to_run_exec):
2802 for i, node in enumerate(to_run_exec):
2803 mod = ast.Module([node])
2803 mod = ast.Module([node])
2804 code = compiler(mod, cell_name, "exec")
2804 code = compiler(mod, cell_name, "exec")
2805 if self.run_code(code, result):
2805 if self.run_code(code, result):
2806 return True
2806 return True
2807
2807
2808 for i, node in enumerate(to_run_interactive):
2808 for i, node in enumerate(to_run_interactive):
2809 mod = ast.Interactive([node])
2809 mod = ast.Interactive([node])
2810 code = compiler(mod, cell_name, "single")
2810 code = compiler(mod, cell_name, "single")
2811 if self.run_code(code, result):
2811 if self.run_code(code, result):
2812 return True
2812 return True
2813
2813
2814 # Flush softspace
2814 # Flush softspace
2815 if softspace(sys.stdout, 0):
2815 if softspace(sys.stdout, 0):
2816 print()
2816 print()
2817
2817
2818 except:
2818 except:
2819 # It's possible to have exceptions raised here, typically by
2819 # It's possible to have exceptions raised here, typically by
2820 # compilation of odd code (such as a naked 'return' outside a
2820 # compilation of odd code (such as a naked 'return' outside a
2821 # function) that did parse but isn't valid. Typically the exception
2821 # function) that did parse but isn't valid. Typically the exception
2822 # is a SyntaxError, but it's safest just to catch anything and show
2822 # is a SyntaxError, but it's safest just to catch anything and show
2823 # the user a traceback.
2823 # the user a traceback.
2824
2824
2825 # We do only one try/except outside the loop to minimize the impact
2825 # We do only one try/except outside the loop to minimize the impact
2826 # on runtime, and also because if any node in the node list is
2826 # on runtime, and also because if any node in the node list is
2827 # broken, we should stop execution completely.
2827 # broken, we should stop execution completely.
2828 if result:
2828 if result:
2829 result.error_before_exec = sys.exc_info()[1]
2829 result.error_before_exec = sys.exc_info()[1]
2830 self.showtraceback()
2830 self.showtraceback()
2831 return True
2831 return True
2832
2832
2833 return False
2833 return False
2834
2834
2835 def run_code(self, code_obj, result=None):
2835 def run_code(self, code_obj, result=None):
2836 """Execute a code object.
2836 """Execute a code object.
2837
2837
2838 When an exception occurs, self.showtraceback() is called to display a
2838 When an exception occurs, self.showtraceback() is called to display a
2839 traceback.
2839 traceback.
2840
2840
2841 Parameters
2841 Parameters
2842 ----------
2842 ----------
2843 code_obj : code object
2843 code_obj : code object
2844 A compiled code object, to be executed
2844 A compiled code object, to be executed
2845 result : ExecutionResult, optional
2845 result : ExecutionResult, optional
2846 An object to store exceptions that occur during execution.
2846 An object to store exceptions that occur during execution.
2847
2847
2848 Returns
2848 Returns
2849 -------
2849 -------
2850 False : successful execution.
2850 False : successful execution.
2851 True : an error occurred.
2851 True : an error occurred.
2852 """
2852 """
2853 # Set our own excepthook in case the user code tries to call it
2853 # Set our own excepthook in case the user code tries to call it
2854 # directly, so that the IPython crash handler doesn't get triggered
2854 # directly, so that the IPython crash handler doesn't get triggered
2855 old_excepthook, sys.excepthook = sys.excepthook, self.excepthook
2855 old_excepthook, sys.excepthook = sys.excepthook, self.excepthook
2856
2856
2857 # we save the original sys.excepthook in the instance, in case config
2857 # we save the original sys.excepthook in the instance, in case config
2858 # code (such as magics) needs access to it.
2858 # code (such as magics) needs access to it.
2859 self.sys_excepthook = old_excepthook
2859 self.sys_excepthook = old_excepthook
2860 outflag = 1 # happens in more places, so it's easier as default
2860 outflag = 1 # happens in more places, so it's easier as default
2861 try:
2861 try:
2862 try:
2862 try:
2863 self.hooks.pre_run_code_hook()
2863 self.hooks.pre_run_code_hook()
2864 #rprint('Running code', repr(code_obj)) # dbg
2864 #rprint('Running code', repr(code_obj)) # dbg
2865 exec(code_obj, self.user_global_ns, self.user_ns)
2865 exec(code_obj, self.user_global_ns, self.user_ns)
2866 finally:
2866 finally:
2867 # Reset our crash handler in place
2867 # Reset our crash handler in place
2868 sys.excepthook = old_excepthook
2868 sys.excepthook = old_excepthook
2869 except SystemExit as e:
2869 except SystemExit as e:
2870 if result is not None:
2870 if result is not None:
2871 result.error_in_exec = e
2871 result.error_in_exec = e
2872 self.showtraceback(exception_only=True)
2872 self.showtraceback(exception_only=True)
2873 warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1)
2873 warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1)
2874 except self.custom_exceptions:
2874 except self.custom_exceptions:
2875 etype, value, tb = sys.exc_info()
2875 etype, value, tb = sys.exc_info()
2876 if result is not None:
2876 if result is not None:
2877 result.error_in_exec = value
2877 result.error_in_exec = value
2878 self.CustomTB(etype, value, tb)
2878 self.CustomTB(etype, value, tb)
2879 except:
2879 except:
2880 if result is not None:
2880 if result is not None:
2881 result.error_in_exec = sys.exc_info()[1]
2881 result.error_in_exec = sys.exc_info()[1]
2882 self.showtraceback()
2882 self.showtraceback()
2883 else:
2883 else:
2884 outflag = 0
2884 outflag = 0
2885 return outflag
2885 return outflag
2886
2886
2887 # For backwards compatibility
2887 # For backwards compatibility
2888 runcode = run_code
2888 runcode = run_code
2889
2889
2890 #-------------------------------------------------------------------------
2890 #-------------------------------------------------------------------------
2891 # Things related to GUI support and pylab
2891 # Things related to GUI support and pylab
2892 #-------------------------------------------------------------------------
2892 #-------------------------------------------------------------------------
2893
2893
2894 def enable_gui(self, gui=None):
2894 def enable_gui(self, gui=None):
2895 raise NotImplementedError('Implement enable_gui in a subclass')
2895 raise NotImplementedError('Implement enable_gui in a subclass')
2896
2896
2897 def enable_matplotlib(self, gui=None):
2897 def enable_matplotlib(self, gui=None):
2898 """Enable interactive matplotlib and inline figure support.
2898 """Enable interactive matplotlib and inline figure support.
2899
2899
2900 This takes the following steps:
2900 This takes the following steps:
2901
2901
2902 1. select the appropriate eventloop and matplotlib backend
2902 1. select the appropriate eventloop and matplotlib backend
2903 2. set up matplotlib for interactive use with that backend
2903 2. set up matplotlib for interactive use with that backend
2904 3. configure formatters for inline figure display
2904 3. configure formatters for inline figure display
2905 4. enable the selected gui eventloop
2905 4. enable the selected gui eventloop
2906
2906
2907 Parameters
2907 Parameters
2908 ----------
2908 ----------
2909 gui : optional, string
2909 gui : optional, string
2910 If given, dictates the choice of matplotlib GUI backend to use
2910 If given, dictates the choice of matplotlib GUI backend to use
2911 (should be one of IPython's supported backends, 'qt', 'osx', 'tk',
2911 (should be one of IPython's supported backends, 'qt', 'osx', 'tk',
2912 'gtk', 'wx' or 'inline'), otherwise we use the default chosen by
2912 'gtk', 'wx' or 'inline'), otherwise we use the default chosen by
2913 matplotlib (as dictated by the matplotlib build-time options plus the
2913 matplotlib (as dictated by the matplotlib build-time options plus the
2914 user's matplotlibrc configuration file). Note that not all backends
2914 user's matplotlibrc configuration file). Note that not all backends
2915 make sense in all contexts, for example a terminal ipython can't
2915 make sense in all contexts, for example a terminal ipython can't
2916 display figures inline.
2916 display figures inline.
2917 """
2917 """
2918 from IPython.core import pylabtools as pt
2918 from IPython.core import pylabtools as pt
2919 gui, backend = pt.find_gui_and_backend(gui, self.pylab_gui_select)
2919 gui, backend = pt.find_gui_and_backend(gui, self.pylab_gui_select)
2920
2920
2921 if gui != 'inline':
2921 if gui != 'inline':
2922 # If we have our first gui selection, store it
2922 # If we have our first gui selection, store it
2923 if self.pylab_gui_select is None:
2923 if self.pylab_gui_select is None:
2924 self.pylab_gui_select = gui
2924 self.pylab_gui_select = gui
2925 # Otherwise if they are different
2925 # Otherwise if they are different
2926 elif gui != self.pylab_gui_select:
2926 elif gui != self.pylab_gui_select:
2927 print ('Warning: Cannot change to a different GUI toolkit: %s.'
2927 print ('Warning: Cannot change to a different GUI toolkit: %s.'
2928 ' Using %s instead.' % (gui, self.pylab_gui_select))
2928 ' Using %s instead.' % (gui, self.pylab_gui_select))
2929 gui, backend = pt.find_gui_and_backend(self.pylab_gui_select)
2929 gui, backend = pt.find_gui_and_backend(self.pylab_gui_select)
2930
2930
2931 pt.activate_matplotlib(backend)
2931 pt.activate_matplotlib(backend)
2932 pt.configure_inline_support(self, backend)
2932 pt.configure_inline_support(self, backend)
2933
2933
2934 # Now we must activate the gui pylab wants to use, and fix %run to take
2934 # Now we must activate the gui pylab wants to use, and fix %run to take
2935 # plot updates into account
2935 # plot updates into account
2936 self.enable_gui(gui)
2936 self.enable_gui(gui)
2937 self.magics_manager.registry['ExecutionMagics'].default_runner = \
2937 self.magics_manager.registry['ExecutionMagics'].default_runner = \
2938 pt.mpl_runner(self.safe_execfile)
2938 pt.mpl_runner(self.safe_execfile)
2939
2939
2940 return gui, backend
2940 return gui, backend
2941
2941
2942 def enable_pylab(self, gui=None, import_all=True, welcome_message=False):
2942 def enable_pylab(self, gui=None, import_all=True, welcome_message=False):
2943 """Activate pylab support at runtime.
2943 """Activate pylab support at runtime.
2944
2944
2945 This turns on support for matplotlib, preloads into the interactive
2945 This turns on support for matplotlib, preloads into the interactive
2946 namespace all of numpy and pylab, and configures IPython to correctly
2946 namespace all of numpy and pylab, and configures IPython to correctly
2947 interact with the GUI event loop. The GUI backend to be used can be
2947 interact with the GUI event loop. The GUI backend to be used can be
2948 optionally selected with the optional ``gui`` argument.
2948 optionally selected with the optional ``gui`` argument.
2949
2949
2950 This method only adds preloading the namespace to InteractiveShell.enable_matplotlib.
2950 This method only adds preloading the namespace to InteractiveShell.enable_matplotlib.
2951
2951
2952 Parameters
2952 Parameters
2953 ----------
2953 ----------
2954 gui : optional, string
2954 gui : optional, string
2955 If given, dictates the choice of matplotlib GUI backend to use
2955 If given, dictates the choice of matplotlib GUI backend to use
2956 (should be one of IPython's supported backends, 'qt', 'osx', 'tk',
2956 (should be one of IPython's supported backends, 'qt', 'osx', 'tk',
2957 'gtk', 'wx' or 'inline'), otherwise we use the default chosen by
2957 'gtk', 'wx' or 'inline'), otherwise we use the default chosen by
2958 matplotlib (as dictated by the matplotlib build-time options plus the
2958 matplotlib (as dictated by the matplotlib build-time options plus the
2959 user's matplotlibrc configuration file). Note that not all backends
2959 user's matplotlibrc configuration file). Note that not all backends
2960 make sense in all contexts, for example a terminal ipython can't
2960 make sense in all contexts, for example a terminal ipython can't
2961 display figures inline.
2961 display figures inline.
2962 import_all : optional, bool, default: True
2962 import_all : optional, bool, default: True
2963 Whether to do `from numpy import *` and `from pylab import *`
2963 Whether to do `from numpy import *` and `from pylab import *`
2964 in addition to module imports.
2964 in addition to module imports.
2965 welcome_message : deprecated
2965 welcome_message : deprecated
2966 This argument is ignored, no welcome message will be displayed.
2966 This argument is ignored, no welcome message will be displayed.
2967 """
2967 """
2968 from IPython.core.pylabtools import import_pylab
2968 from IPython.core.pylabtools import import_pylab
2969
2969
2970 gui, backend = self.enable_matplotlib(gui)
2970 gui, backend = self.enable_matplotlib(gui)
2971
2971
2972 # We want to prevent the loading of pylab to pollute the user's
2972 # We want to prevent the loading of pylab to pollute the user's
2973 # namespace as shown by the %who* magics, so we execute the activation
2973 # namespace as shown by the %who* magics, so we execute the activation
2974 # code in an empty namespace, and we update *both* user_ns and
2974 # code in an empty namespace, and we update *both* user_ns and
2975 # user_ns_hidden with this information.
2975 # user_ns_hidden with this information.
2976 ns = {}
2976 ns = {}
2977 import_pylab(ns, import_all)
2977 import_pylab(ns, import_all)
2978 # warn about clobbered names
2978 # warn about clobbered names
2979 ignored = {"__builtins__"}
2979 ignored = {"__builtins__"}
2980 both = set(ns).intersection(self.user_ns).difference(ignored)
2980 both = set(ns).intersection(self.user_ns).difference(ignored)
2981 clobbered = [ name for name in both if self.user_ns[name] is not ns[name] ]
2981 clobbered = [ name for name in both if self.user_ns[name] is not ns[name] ]
2982 self.user_ns.update(ns)
2982 self.user_ns.update(ns)
2983 self.user_ns_hidden.update(ns)
2983 self.user_ns_hidden.update(ns)
2984 return gui, backend, clobbered
2984 return gui, backend, clobbered
2985
2985
2986 #-------------------------------------------------------------------------
2986 #-------------------------------------------------------------------------
2987 # Utilities
2987 # Utilities
2988 #-------------------------------------------------------------------------
2988 #-------------------------------------------------------------------------
2989
2989
2990 def var_expand(self, cmd, depth=0, formatter=DollarFormatter()):
2990 def var_expand(self, cmd, depth=0, formatter=DollarFormatter()):
2991 """Expand python variables in a string.
2991 """Expand python variables in a string.
2992
2992
2993 The depth argument indicates how many frames above the caller should
2993 The depth argument indicates how many frames above the caller should
2994 be walked to look for the local namespace where to expand variables.
2994 be walked to look for the local namespace where to expand variables.
2995
2995
2996 The global namespace for expansion is always the user's interactive
2996 The global namespace for expansion is always the user's interactive
2997 namespace.
2997 namespace.
2998 """
2998 """
2999 ns = self.user_ns.copy()
2999 ns = self.user_ns.copy()
3000 try:
3000 try:
3001 frame = sys._getframe(depth+1)
3001 frame = sys._getframe(depth+1)
3002 except ValueError:
3002 except ValueError:
3003 # This is thrown if there aren't that many frames on the stack,
3003 # This is thrown if there aren't that many frames on the stack,
3004 # e.g. if a script called run_line_magic() directly.
3004 # e.g. if a script called run_line_magic() directly.
3005 pass
3005 pass
3006 else:
3006 else:
3007 ns.update(frame.f_locals)
3007 ns.update(frame.f_locals)
3008
3008
3009 try:
3009 try:
3010 # We have to use .vformat() here, because 'self' is a valid and common
3010 # We have to use .vformat() here, because 'self' is a valid and common
3011 # name, and expanding **ns for .format() would make it collide with
3011 # name, and expanding **ns for .format() would make it collide with
3012 # the 'self' argument of the method.
3012 # the 'self' argument of the method.
3013 cmd = formatter.vformat(cmd, args=[], kwargs=ns)
3013 cmd = formatter.vformat(cmd, args=[], kwargs=ns)
3014 except Exception:
3014 except Exception:
3015 # if formatter couldn't format, just let it go untransformed
3015 # if formatter couldn't format, just let it go untransformed
3016 pass
3016 pass
3017 return cmd
3017 return cmd
3018
3018
3019 def mktempfile(self, data=None, prefix='ipython_edit_'):
3019 def mktempfile(self, data=None, prefix='ipython_edit_'):
3020 """Make a new tempfile and return its filename.
3020 """Make a new tempfile and return its filename.
3021
3021
3022 This makes a call to tempfile.mkstemp (created in a tempfile.mkdtemp),
3022 This makes a call to tempfile.mkstemp (created in a tempfile.mkdtemp),
3023 but it registers the created filename internally so ipython cleans it up
3023 but it registers the created filename internally so ipython cleans it up
3024 at exit time.
3024 at exit time.
3025
3025
3026 Optional inputs:
3026 Optional inputs:
3027
3027
3028 - data(None): if data is given, it gets written out to the temp file
3028 - data(None): if data is given, it gets written out to the temp file
3029 immediately, and the file is closed again."""
3029 immediately, and the file is closed again."""
3030
3030
3031 dirname = tempfile.mkdtemp(prefix=prefix)
3031 dirname = tempfile.mkdtemp(prefix=prefix)
3032 self.tempdirs.append(dirname)
3032 self.tempdirs.append(dirname)
3033
3033
3034 handle, filename = tempfile.mkstemp('.py', prefix, dir=dirname)
3034 handle, filename = tempfile.mkstemp('.py', prefix, dir=dirname)
3035 os.close(handle) # On Windows, there can only be one open handle on a file
3035 os.close(handle) # On Windows, there can only be one open handle on a file
3036 self.tempfiles.append(filename)
3036 self.tempfiles.append(filename)
3037
3037
3038 if data:
3038 if data:
3039 tmp_file = open(filename,'w')
3039 tmp_file = open(filename,'w')
3040 tmp_file.write(data)
3040 tmp_file.write(data)
3041 tmp_file.close()
3041 tmp_file.close()
3042 return filename
3042 return filename
3043
3043
3044 @undoc
3044 @undoc
3045 def write(self,data):
3045 def write(self,data):
3046 """DEPRECATED: Write a string to the default output"""
3046 """DEPRECATED: Write a string to the default output"""
3047 warn('InteractiveShell.write() is deprecated, use sys.stdout instead',
3047 warn('InteractiveShell.write() is deprecated, use sys.stdout instead',
3048 DeprecationWarning, stacklevel=2)
3048 DeprecationWarning, stacklevel=2)
3049 sys.stdout.write(data)
3049 sys.stdout.write(data)
3050
3050
3051 @undoc
3051 @undoc
3052 def write_err(self,data):
3052 def write_err(self,data):
3053 """DEPRECATED: Write a string to the default error output"""
3053 """DEPRECATED: Write a string to the default error output"""
3054 warn('InteractiveShell.write_err() is deprecated, use sys.stderr instead',
3054 warn('InteractiveShell.write_err() is deprecated, use sys.stderr instead',
3055 DeprecationWarning, stacklevel=2)
3055 DeprecationWarning, stacklevel=2)
3056 sys.stderr.write(data)
3056 sys.stderr.write(data)
3057
3057
3058 def ask_yes_no(self, prompt, default=None, interrupt=None):
3058 def ask_yes_no(self, prompt, default=None, interrupt=None):
3059 if self.quiet:
3059 if self.quiet:
3060 return True
3060 return True
3061 return ask_yes_no(prompt,default,interrupt)
3061 return ask_yes_no(prompt,default,interrupt)
3062
3062
3063 def show_usage(self):
3063 def show_usage(self):
3064 """Show a usage message"""
3064 """Show a usage message"""
3065 page.page(IPython.core.usage.interactive_usage)
3065 page.page(IPython.core.usage.interactive_usage)
3066
3066
3067 def extract_input_lines(self, range_str, raw=False):
3067 def extract_input_lines(self, range_str, raw=False):
3068 """Return as a string a set of input history slices.
3068 """Return as a string a set of input history slices.
3069
3069
3070 Parameters
3070 Parameters
3071 ----------
3071 ----------
3072 range_str : string
3072 range_str : string
3073 The set of slices is given as a string, like "~5/6-~4/2 4:8 9",
3073 The set of slices is given as a string, like "~5/6-~4/2 4:8 9",
3074 since this function is for use by magic functions which get their
3074 since this function is for use by magic functions which get their
3075 arguments as strings. The number before the / is the session
3075 arguments as strings. The number before the / is the session
3076 number: ~n goes n back from the current session.
3076 number: ~n goes n back from the current session.
3077
3077
3078 raw : bool, optional
3078 raw : bool, optional
3079 By default, the processed input is used. If this is true, the raw
3079 By default, the processed input is used. If this is true, the raw
3080 input history is used instead.
3080 input history is used instead.
3081
3081
3082 Notes
3082 Notes
3083 -----
3083 -----
3084
3084
3085 Slices can be described with two notations:
3085 Slices can be described with two notations:
3086
3086
3087 * ``N:M`` -> standard python form, means including items N...(M-1).
3087 * ``N:M`` -> standard python form, means including items N...(M-1).
3088 * ``N-M`` -> include items N..M (closed endpoint).
3088 * ``N-M`` -> include items N..M (closed endpoint).
3089 """
3089 """
3090 lines = self.history_manager.get_range_by_str(range_str, raw=raw)
3090 lines = self.history_manager.get_range_by_str(range_str, raw=raw)
3091 return "\n".join(x for _, _, x in lines)
3091 return "\n".join(x for _, _, x in lines)
3092
3092
3093 def find_user_code(self, target, raw=True, py_only=False, skip_encoding_cookie=True, search_ns=False):
3093 def find_user_code(self, target, raw=True, py_only=False, skip_encoding_cookie=True, search_ns=False):
3094 """Get a code string from history, file, url, or a string or macro.
3094 """Get a code string from history, file, url, or a string or macro.
3095
3095
3096 This is mainly used by magic functions.
3096 This is mainly used by magic functions.
3097
3097
3098 Parameters
3098 Parameters
3099 ----------
3099 ----------
3100
3100
3101 target : str
3101 target : str
3102
3102
3103 A string specifying code to retrieve. This will be tried respectively
3103 A string specifying code to retrieve. This will be tried respectively
3104 as: ranges of input history (see %history for syntax), url,
3104 as: ranges of input history (see %history for syntax), url,
3105 corresponding .py file, filename, or an expression evaluating to a
3105 corresponding .py file, filename, or an expression evaluating to a
3106 string or Macro in the user namespace.
3106 string or Macro in the user namespace.
3107
3107
3108 raw : bool
3108 raw : bool
3109 If true (default), retrieve raw history. Has no effect on the other
3109 If true (default), retrieve raw history. Has no effect on the other
3110 retrieval mechanisms.
3110 retrieval mechanisms.
3111
3111
3112 py_only : bool (default False)
3112 py_only : bool (default False)
3113 Only try to fetch python code, do not try alternative methods to decode file
3113 Only try to fetch python code, do not try alternative methods to decode file
3114 if unicode fails.
3114 if unicode fails.
3115
3115
3116 Returns
3116 Returns
3117 -------
3117 -------
3118 A string of code.
3118 A string of code.
3119
3119
3120 ValueError is raised if nothing is found, and TypeError if it evaluates
3120 ValueError is raised if nothing is found, and TypeError if it evaluates
3121 to an object of another type. In each case, .args[0] is a printable
3121 to an object of another type. In each case, .args[0] is a printable
3122 message.
3122 message.
3123 """
3123 """
3124 code = self.extract_input_lines(target, raw=raw) # Grab history
3124 code = self.extract_input_lines(target, raw=raw) # Grab history
3125 if code:
3125 if code:
3126 return code
3126 return code
3127 try:
3127 try:
3128 if target.startswith(('http://', 'https://')):
3128 if target.startswith(('http://', 'https://')):
3129 return openpy.read_py_url(target, skip_encoding_cookie=skip_encoding_cookie)
3129 return openpy.read_py_url(target, skip_encoding_cookie=skip_encoding_cookie)
3130 except UnicodeDecodeError:
3130 except UnicodeDecodeError:
3131 if not py_only :
3131 if not py_only :
3132 # Deferred import
3132 # Deferred import
3133 try:
3133 try:
3134 from urllib.request import urlopen # Py3
3134 from urllib.request import urlopen # Py3
3135 except ImportError:
3135 except ImportError:
3136 from urllib import urlopen
3136 from urllib import urlopen
3137 response = urlopen(target)
3137 response = urlopen(target)
3138 return response.read().decode('latin1')
3138 return response.read().decode('latin1')
3139 raise ValueError(("'%s' seem to be unreadable.") % target)
3139 raise ValueError(("'%s' seem to be unreadable.") % target)
3140
3140
3141 potential_target = [target]
3141 potential_target = [target]
3142 try :
3142 try :
3143 potential_target.insert(0,get_py_filename(target))
3143 potential_target.insert(0,get_py_filename(target))
3144 except IOError:
3144 except IOError:
3145 pass
3145 pass
3146
3146
3147 for tgt in potential_target :
3147 for tgt in potential_target :
3148 if os.path.isfile(tgt): # Read file
3148 if os.path.isfile(tgt): # Read file
3149 try :
3149 try :
3150 return openpy.read_py_file(tgt, skip_encoding_cookie=skip_encoding_cookie)
3150 return openpy.read_py_file(tgt, skip_encoding_cookie=skip_encoding_cookie)
3151 except UnicodeDecodeError :
3151 except UnicodeDecodeError :
3152 if not py_only :
3152 if not py_only :
3153 with io_open(tgt,'r', encoding='latin1') as f :
3153 with io_open(tgt,'r', encoding='latin1') as f :
3154 return f.read()
3154 return f.read()
3155 raise ValueError(("'%s' seem to be unreadable.") % target)
3155 raise ValueError(("'%s' seem to be unreadable.") % target)
3156 elif os.path.isdir(os.path.expanduser(tgt)):
3156 elif os.path.isdir(os.path.expanduser(tgt)):
3157 raise ValueError("'%s' is a directory, not a regular file." % target)
3157 raise ValueError("'%s' is a directory, not a regular file." % target)
3158
3158
3159 if search_ns:
3159 if search_ns:
3160 # Inspect namespace to load object source
3160 # Inspect namespace to load object source
3161 object_info = self.object_inspect(target, detail_level=1)
3161 object_info = self.object_inspect(target, detail_level=1)
3162 if object_info['found'] and object_info['source']:
3162 if object_info['found'] and object_info['source']:
3163 return object_info['source']
3163 return object_info['source']
3164
3164
3165 try: # User namespace
3165 try: # User namespace
3166 codeobj = eval(target, self.user_ns)
3166 codeobj = eval(target, self.user_ns)
3167 except Exception:
3167 except Exception:
3168 raise ValueError(("'%s' was not found in history, as a file, url, "
3168 raise ValueError(("'%s' was not found in history, as a file, url, "
3169 "nor in the user namespace.") % target)
3169 "nor in the user namespace.") % target)
3170
3170
3171 if isinstance(codeobj, string_types):
3171 if isinstance(codeobj, string_types):
3172 return codeobj
3172 return codeobj
3173 elif isinstance(codeobj, Macro):
3173 elif isinstance(codeobj, Macro):
3174 return codeobj.value
3174 return codeobj.value
3175
3175
3176 raise TypeError("%s is neither a string nor a macro." % target,
3176 raise TypeError("%s is neither a string nor a macro." % target,
3177 codeobj)
3177 codeobj)
3178
3178
3179 #-------------------------------------------------------------------------
3179 #-------------------------------------------------------------------------
3180 # Things related to IPython exiting
3180 # Things related to IPython exiting
3181 #-------------------------------------------------------------------------
3181 #-------------------------------------------------------------------------
3182 def atexit_operations(self):
3182 def atexit_operations(self):
3183 """This will be executed at the time of exit.
3183 """This will be executed at the time of exit.
3184
3184
3185 Cleanup operations and saving of persistent data that is done
3185 Cleanup operations and saving of persistent data that is done
3186 unconditionally by IPython should be performed here.
3186 unconditionally by IPython should be performed here.
3187
3187
3188 For things that may depend on startup flags or platform specifics (such
3188 For things that may depend on startup flags or platform specifics (such
3189 as having readline or not), register a separate atexit function in the
3189 as having readline or not), register a separate atexit function in the
3190 code that has the appropriate information, rather than trying to
3190 code that has the appropriate information, rather than trying to
3191 clutter
3191 clutter
3192 """
3192 """
3193 # Close the history session (this stores the end time and line count)
3193 # Close the history session (this stores the end time and line count)
3194 # this must be *before* the tempfile cleanup, in case of temporary
3194 # this must be *before* the tempfile cleanup, in case of temporary
3195 # history db
3195 # history db
3196 self.history_manager.end_session()
3196 self.history_manager.end_session()
3197
3197
3198 # Cleanup all tempfiles and folders left around
3198 # Cleanup all tempfiles and folders left around
3199 for tfile in self.tempfiles:
3199 for tfile in self.tempfiles:
3200 try:
3200 try:
3201 os.unlink(tfile)
3201 os.unlink(tfile)
3202 except OSError:
3202 except OSError:
3203 pass
3203 pass
3204
3204
3205 for tdir in self.tempdirs:
3205 for tdir in self.tempdirs:
3206 try:
3206 try:
3207 os.rmdir(tdir)
3207 os.rmdir(tdir)
3208 except OSError:
3208 except OSError:
3209 pass
3209 pass
3210
3210
3211 # Clear all user namespaces to release all references cleanly.
3211 # Clear all user namespaces to release all references cleanly.
3212 self.reset(new_session=False)
3212 self.reset(new_session=False)
3213
3213
3214 # Run user hooks
3214 # Run user hooks
3215 self.hooks.shutdown_hook()
3215 self.hooks.shutdown_hook()
3216
3216
3217 def cleanup(self):
3217 def cleanup(self):
3218 self.restore_sys_module_state()
3218 self.restore_sys_module_state()
3219
3219
3220
3220
3221 # Overridden in terminal subclass to change prompts
3221 # Overridden in terminal subclass to change prompts
3222 def switch_doctest_mode(self, mode):
3222 def switch_doctest_mode(self, mode):
3223 pass
3223 pass
3224
3224
3225
3225
3226 class InteractiveShellABC(with_metaclass(abc.ABCMeta, object)):
3226 class InteractiveShellABC(with_metaclass(abc.ABCMeta, object)):
3227 """An abstract base class for InteractiveShell."""
3227 """An abstract base class for InteractiveShell."""
3228
3228
3229 InteractiveShellABC.register(InteractiveShell)
3229 InteractiveShellABC.register(InteractiveShell)
@@ -1,1489 +1,1491 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """
2 """
3 Verbose and colourful traceback formatting.
3 Verbose and colourful traceback formatting.
4
4
5 **ColorTB**
5 **ColorTB**
6
6
7 I've always found it a bit hard to visually parse tracebacks in Python. The
7 I've always found it a bit hard to visually parse tracebacks in Python. The
8 ColorTB class is a solution to that problem. It colors the different parts of a
8 ColorTB class is a solution to that problem. It colors the different parts of a
9 traceback in a manner similar to what you would expect from a syntax-highlighting
9 traceback in a manner similar to what you would expect from a syntax-highlighting
10 text editor.
10 text editor.
11
11
12 Installation instructions for ColorTB::
12 Installation instructions for ColorTB::
13
13
14 import sys,ultratb
14 import sys,ultratb
15 sys.excepthook = ultratb.ColorTB()
15 sys.excepthook = ultratb.ColorTB()
16
16
17 **VerboseTB**
17 **VerboseTB**
18
18
19 I've also included a port of Ka-Ping Yee's "cgitb.py" that produces all kinds
19 I've also included a port of Ka-Ping Yee's "cgitb.py" that produces all kinds
20 of useful info when a traceback occurs. Ping originally had it spit out HTML
20 of useful info when a traceback occurs. Ping originally had it spit out HTML
21 and intended it for CGI programmers, but why should they have all the fun? I
21 and intended it for CGI programmers, but why should they have all the fun? I
22 altered it to spit out colored text to the terminal. It's a bit overwhelming,
22 altered it to spit out colored text to the terminal. It's a bit overwhelming,
23 but kind of neat, and maybe useful for long-running programs that you believe
23 but kind of neat, and maybe useful for long-running programs that you believe
24 are bug-free. If a crash *does* occur in that type of program you want details.
24 are bug-free. If a crash *does* occur in that type of program you want details.
25 Give it a shot--you'll love it or you'll hate it.
25 Give it a shot--you'll love it or you'll hate it.
26
26
27 .. note::
27 .. note::
28
28
29 The Verbose mode prints the variables currently visible where the exception
29 The Verbose mode prints the variables currently visible where the exception
30 happened (shortening their strings if too long). This can potentially be
30 happened (shortening their strings if too long). This can potentially be
31 very slow, if you happen to have a huge data structure whose string
31 very slow, if you happen to have a huge data structure whose string
32 representation is complex to compute. Your computer may appear to freeze for
32 representation is complex to compute. Your computer may appear to freeze for
33 a while with cpu usage at 100%. If this occurs, you can cancel the traceback
33 a while with cpu usage at 100%. If this occurs, you can cancel the traceback
34 with Ctrl-C (maybe hitting it more than once).
34 with Ctrl-C (maybe hitting it more than once).
35
35
36 If you encounter this kind of situation often, you may want to use the
36 If you encounter this kind of situation often, you may want to use the
37 Verbose_novars mode instead of the regular Verbose, which avoids formatting
37 Verbose_novars mode instead of the regular Verbose, which avoids formatting
38 variables (but otherwise includes the information and context given by
38 variables (but otherwise includes the information and context given by
39 Verbose).
39 Verbose).
40
40
41 .. note::
41 .. note::
42
42
43 The verbose mode print all variables in the stack, which means it can
43 The verbose mode print all variables in the stack, which means it can
44 potentially leak sensitive information like access keys, or unencryted
44 potentially leak sensitive information like access keys, or unencryted
45 password.
45 password.
46
46
47 Installation instructions for VerboseTB::
47 Installation instructions for VerboseTB::
48
48
49 import sys,ultratb
49 import sys,ultratb
50 sys.excepthook = ultratb.VerboseTB()
50 sys.excepthook = ultratb.VerboseTB()
51
51
52 Note: Much of the code in this module was lifted verbatim from the standard
52 Note: Much of the code in this module was lifted verbatim from the standard
53 library module 'traceback.py' and Ka-Ping Yee's 'cgitb.py'.
53 library module 'traceback.py' and Ka-Ping Yee's 'cgitb.py'.
54
54
55 Color schemes
55 Color schemes
56 -------------
56 -------------
57
57
58 The colors are defined in the class TBTools through the use of the
58 The colors are defined in the class TBTools through the use of the
59 ColorSchemeTable class. Currently the following exist:
59 ColorSchemeTable class. Currently the following exist:
60
60
61 - NoColor: allows all of this module to be used in any terminal (the color
61 - NoColor: allows all of this module to be used in any terminal (the color
62 escapes are just dummy blank strings).
62 escapes are just dummy blank strings).
63
63
64 - Linux: is meant to look good in a terminal like the Linux console (black
64 - Linux: is meant to look good in a terminal like the Linux console (black
65 or very dark background).
65 or very dark background).
66
66
67 - LightBG: similar to Linux but swaps dark/light colors to be more readable
67 - LightBG: similar to Linux but swaps dark/light colors to be more readable
68 in light background terminals.
68 in light background terminals.
69
69
70 - Neutral: a neutral color scheme that should be readable on both light and
70 - Neutral: a neutral color scheme that should be readable on both light and
71 dark background
71 dark background
72
72
73 You can implement other color schemes easily, the syntax is fairly
73 You can implement other color schemes easily, the syntax is fairly
74 self-explanatory. Please send back new schemes you develop to the author for
74 self-explanatory. Please send back new schemes you develop to the author for
75 possible inclusion in future releases.
75 possible inclusion in future releases.
76
76
77 Inheritance diagram:
77 Inheritance diagram:
78
78
79 .. inheritance-diagram:: IPython.core.ultratb
79 .. inheritance-diagram:: IPython.core.ultratb
80 :parts: 3
80 :parts: 3
81 """
81 """
82
82
83 #*****************************************************************************
83 #*****************************************************************************
84 # Copyright (C) 2001 Nathaniel Gray <n8gray@caltech.edu>
84 # Copyright (C) 2001 Nathaniel Gray <n8gray@caltech.edu>
85 # Copyright (C) 2001-2004 Fernando Perez <fperez@colorado.edu>
85 # Copyright (C) 2001-2004 Fernando Perez <fperez@colorado.edu>
86 #
86 #
87 # Distributed under the terms of the BSD License. The full license is in
87 # Distributed under the terms of the BSD License. The full license is in
88 # the file COPYING, distributed as part of this software.
88 # the file COPYING, distributed as part of this software.
89 #*****************************************************************************
89 #*****************************************************************************
90
90
91 from __future__ import absolute_import
91 from __future__ import absolute_import
92 from __future__ import unicode_literals
92 from __future__ import unicode_literals
93 from __future__ import print_function
93 from __future__ import print_function
94
94
95 import dis
95 import dis
96 import inspect
96 import inspect
97 import keyword
97 import keyword
98 import linecache
98 import linecache
99 import os
99 import os
100 import pydoc
100 import pydoc
101 import re
101 import re
102 import sys
102 import sys
103 import time
103 import time
104 import tokenize
104 import tokenize
105 import traceback
105 import traceback
106 import types
106 import types
107
107
108 try: # Python 2
108 try: # Python 2
109 generate_tokens = tokenize.generate_tokens
109 generate_tokens = tokenize.generate_tokens
110 except AttributeError: # Python 3
110 except AttributeError: # Python 3
111 generate_tokens = tokenize.tokenize
111 generate_tokens = tokenize.tokenize
112
112
113 # For purposes of monkeypatching inspect to fix a bug in it.
113 # For purposes of monkeypatching inspect to fix a bug in it.
114 from inspect import getsourcefile, getfile, getmodule, \
114 from inspect import getsourcefile, getfile, getmodule, \
115 ismodule, isclass, ismethod, isfunction, istraceback, isframe, iscode
115 ismodule, isclass, ismethod, isfunction, istraceback, isframe, iscode
116
116
117 # IPython's own modules
117 # IPython's own modules
118 from IPython import get_ipython
118 from IPython import get_ipython
119 from IPython.core import debugger
119 from IPython.core import debugger
120 from IPython.core.display_trap import DisplayTrap
120 from IPython.core.display_trap import DisplayTrap
121 from IPython.core.excolors import exception_colors
121 from IPython.core.excolors import exception_colors
122 from IPython.utils import PyColorize
122 from IPython.utils import PyColorize
123 from IPython.utils import openpy
123 from IPython.utils import openpy
124 from IPython.utils import path as util_path
124 from IPython.utils import path as util_path
125 from IPython.utils import py3compat
125 from IPython.utils import py3compat
126 from IPython.utils import ulinecache
126 from IPython.utils import ulinecache
127 from IPython.utils.data import uniq_stable
127 from IPython.utils.data import uniq_stable
128 from IPython.utils.terminal import get_terminal_size
128 from IPython.utils.terminal import get_terminal_size
129 from logging import info, error
129 from logging import info, error
130
130
131 import IPython.utils.colorable as colorable
131 import IPython.utils.colorable as colorable
132
132
133 # Globals
133 # Globals
134 # amount of space to put line numbers before verbose tracebacks
134 # amount of space to put line numbers before verbose tracebacks
135 INDENT_SIZE = 8
135 INDENT_SIZE = 8
136
136
137 # Default color scheme. This is used, for example, by the traceback
137 # Default color scheme. This is used, for example, by the traceback
138 # formatter. When running in an actual IPython instance, the user's rc.colors
138 # formatter. When running in an actual IPython instance, the user's rc.colors
139 # value is used, but having a module global makes this functionality available
139 # value is used, but having a module global makes this functionality available
140 # to users of ultratb who are NOT running inside ipython.
140 # to users of ultratb who are NOT running inside ipython.
141 DEFAULT_SCHEME = 'NoColor'
141 DEFAULT_SCHEME = 'NoColor'
142
142
143 # ---------------------------------------------------------------------------
143 # ---------------------------------------------------------------------------
144 # Code begins
144 # Code begins
145
145
146 # Utility functions
146 # Utility functions
147 def inspect_error():
147 def inspect_error():
148 """Print a message about internal inspect errors.
148 """Print a message about internal inspect errors.
149
149
150 These are unfortunately quite common."""
150 These are unfortunately quite common."""
151
151
152 error('Internal Python error in the inspect module.\n'
152 error('Internal Python error in the inspect module.\n'
153 'Below is the traceback from this internal error.\n')
153 'Below is the traceback from this internal error.\n')
154
154
155
155
156 # This function is a monkeypatch we apply to the Python inspect module. We have
156 # This function is a monkeypatch we apply to the Python inspect module. We have
157 # now found when it's needed (see discussion on issue gh-1456), and we have a
157 # now found when it's needed (see discussion on issue gh-1456), and we have a
158 # test case (IPython.core.tests.test_ultratb.ChangedPyFileTest) that fails if
158 # test case (IPython.core.tests.test_ultratb.ChangedPyFileTest) that fails if
159 # the monkeypatch is not applied. TK, Aug 2012.
159 # the monkeypatch is not applied. TK, Aug 2012.
160 def findsource(object):
160 def findsource(object):
161 """Return the entire source file and starting line number for an object.
161 """Return the entire source file and starting line number for an object.
162
162
163 The argument may be a module, class, method, function, traceback, frame,
163 The argument may be a module, class, method, function, traceback, frame,
164 or code object. The source code is returned as a list of all the lines
164 or code object. The source code is returned as a list of all the lines
165 in the file and the line number indexes a line in that list. An IOError
165 in the file and the line number indexes a line in that list. An IOError
166 is raised if the source code cannot be retrieved.
166 is raised if the source code cannot be retrieved.
167
167
168 FIXED version with which we monkeypatch the stdlib to work around a bug."""
168 FIXED version with which we monkeypatch the stdlib to work around a bug."""
169
169
170 file = getsourcefile(object) or getfile(object)
170 file = getsourcefile(object) or getfile(object)
171 # If the object is a frame, then trying to get the globals dict from its
171 # If the object is a frame, then trying to get the globals dict from its
172 # module won't work. Instead, the frame object itself has the globals
172 # module won't work. Instead, the frame object itself has the globals
173 # dictionary.
173 # dictionary.
174 globals_dict = None
174 globals_dict = None
175 if inspect.isframe(object):
175 if inspect.isframe(object):
176 # XXX: can this ever be false?
176 # XXX: can this ever be false?
177 globals_dict = object.f_globals
177 globals_dict = object.f_globals
178 else:
178 else:
179 module = getmodule(object, file)
179 module = getmodule(object, file)
180 if module:
180 if module:
181 globals_dict = module.__dict__
181 globals_dict = module.__dict__
182 lines = linecache.getlines(file, globals_dict)
182 lines = linecache.getlines(file, globals_dict)
183 if not lines:
183 if not lines:
184 raise IOError('could not get source code')
184 raise IOError('could not get source code')
185
185
186 if ismodule(object):
186 if ismodule(object):
187 return lines, 0
187 return lines, 0
188
188
189 if isclass(object):
189 if isclass(object):
190 name = object.__name__
190 name = object.__name__
191 pat = re.compile(r'^(\s*)class\s*' + name + r'\b')
191 pat = re.compile(r'^(\s*)class\s*' + name + r'\b')
192 # make some effort to find the best matching class definition:
192 # make some effort to find the best matching class definition:
193 # use the one with the least indentation, which is the one
193 # use the one with the least indentation, which is the one
194 # that's most probably not inside a function definition.
194 # that's most probably not inside a function definition.
195 candidates = []
195 candidates = []
196 for i in range(len(lines)):
196 for i in range(len(lines)):
197 match = pat.match(lines[i])
197 match = pat.match(lines[i])
198 if match:
198 if match:
199 # if it's at toplevel, it's already the best one
199 # if it's at toplevel, it's already the best one
200 if lines[i][0] == 'c':
200 if lines[i][0] == 'c':
201 return lines, i
201 return lines, i
202 # else add whitespace to candidate list
202 # else add whitespace to candidate list
203 candidates.append((match.group(1), i))
203 candidates.append((match.group(1), i))
204 if candidates:
204 if candidates:
205 # this will sort by whitespace, and by line number,
205 # this will sort by whitespace, and by line number,
206 # less whitespace first
206 # less whitespace first
207 candidates.sort()
207 candidates.sort()
208 return lines, candidates[0][1]
208 return lines, candidates[0][1]
209 else:
209 else:
210 raise IOError('could not find class definition')
210 raise IOError('could not find class definition')
211
211
212 if ismethod(object):
212 if ismethod(object):
213 object = object.__func__
213 object = object.__func__
214 if isfunction(object):
214 if isfunction(object):
215 object = object.__code__
215 object = object.__code__
216 if istraceback(object):
216 if istraceback(object):
217 object = object.tb_frame
217 object = object.tb_frame
218 if isframe(object):
218 if isframe(object):
219 object = object.f_code
219 object = object.f_code
220 if iscode(object):
220 if iscode(object):
221 if not hasattr(object, 'co_firstlineno'):
221 if not hasattr(object, 'co_firstlineno'):
222 raise IOError('could not find function definition')
222 raise IOError('could not find function definition')
223 pat = re.compile(r'^(\s*def\s)|(.*(?<!\w)lambda(:|\s))|^(\s*@)')
223 pat = re.compile(r'^(\s*def\s)|(.*(?<!\w)lambda(:|\s))|^(\s*@)')
224 pmatch = pat.match
224 pmatch = pat.match
225 # fperez - fix: sometimes, co_firstlineno can give a number larger than
225 # fperez - fix: sometimes, co_firstlineno can give a number larger than
226 # the length of lines, which causes an error. Safeguard against that.
226 # the length of lines, which causes an error. Safeguard against that.
227 lnum = min(object.co_firstlineno, len(lines)) - 1
227 lnum = min(object.co_firstlineno, len(lines)) - 1
228 while lnum > 0:
228 while lnum > 0:
229 if pmatch(lines[lnum]):
229 if pmatch(lines[lnum]):
230 break
230 break
231 lnum -= 1
231 lnum -= 1
232
232
233 return lines, lnum
233 return lines, lnum
234 raise IOError('could not find code object')
234 raise IOError('could not find code object')
235
235
236
236
237 # This is a patched version of inspect.getargs that applies the (unmerged)
237 # This is a patched version of inspect.getargs that applies the (unmerged)
238 # patch for http://bugs.python.org/issue14611 by Stefano Taschini. This fixes
238 # patch for http://bugs.python.org/issue14611 by Stefano Taschini. This fixes
239 # https://github.com/ipython/ipython/issues/8205 and
239 # https://github.com/ipython/ipython/issues/8205 and
240 # https://github.com/ipython/ipython/issues/8293
240 # https://github.com/ipython/ipython/issues/8293
241 def getargs(co):
241 def getargs(co):
242 """Get information about the arguments accepted by a code object.
242 """Get information about the arguments accepted by a code object.
243
243
244 Three things are returned: (args, varargs, varkw), where 'args' is
244 Three things are returned: (args, varargs, varkw), where 'args' is
245 a list of argument names (possibly containing nested lists), and
245 a list of argument names (possibly containing nested lists), and
246 'varargs' and 'varkw' are the names of the * and ** arguments or None."""
246 'varargs' and 'varkw' are the names of the * and ** arguments or None."""
247 if not iscode(co):
247 if not iscode(co):
248 raise TypeError('{!r} is not a code object'.format(co))
248 raise TypeError('{!r} is not a code object'.format(co))
249
249
250 nargs = co.co_argcount
250 nargs = co.co_argcount
251 names = co.co_varnames
251 names = co.co_varnames
252 args = list(names[:nargs])
252 args = list(names[:nargs])
253 step = 0
253 step = 0
254
254
255 # The following acrobatics are for anonymous (tuple) arguments.
255 # The following acrobatics are for anonymous (tuple) arguments.
256 for i in range(nargs):
256 for i in range(nargs):
257 if args[i][:1] in ('', '.'):
257 if args[i][:1] in ('', '.'):
258 stack, remain, count = [], [], []
258 stack, remain, count = [], [], []
259 while step < len(co.co_code):
259 while step < len(co.co_code):
260 op = ord(co.co_code[step])
260 op = ord(co.co_code[step])
261 step = step + 1
261 step = step + 1
262 if op >= dis.HAVE_ARGUMENT:
262 if op >= dis.HAVE_ARGUMENT:
263 opname = dis.opname[op]
263 opname = dis.opname[op]
264 value = ord(co.co_code[step]) + ord(co.co_code[step+1])*256
264 value = ord(co.co_code[step]) + ord(co.co_code[step+1])*256
265 step = step + 2
265 step = step + 2
266 if opname in ('UNPACK_TUPLE', 'UNPACK_SEQUENCE'):
266 if opname in ('UNPACK_TUPLE', 'UNPACK_SEQUENCE'):
267 remain.append(value)
267 remain.append(value)
268 count.append(value)
268 count.append(value)
269 elif opname in ('STORE_FAST', 'STORE_DEREF'):
269 elif opname in ('STORE_FAST', 'STORE_DEREF'):
270 if op in dis.haslocal:
270 if op in dis.haslocal:
271 stack.append(co.co_varnames[value])
271 stack.append(co.co_varnames[value])
272 elif op in dis.hasfree:
272 elif op in dis.hasfree:
273 stack.append((co.co_cellvars + co.co_freevars)[value])
273 stack.append((co.co_cellvars + co.co_freevars)[value])
274 # Special case for sublists of length 1: def foo((bar))
274 # Special case for sublists of length 1: def foo((bar))
275 # doesn't generate the UNPACK_TUPLE bytecode, so if
275 # doesn't generate the UNPACK_TUPLE bytecode, so if
276 # `remain` is empty here, we have such a sublist.
276 # `remain` is empty here, we have such a sublist.
277 if not remain:
277 if not remain:
278 stack[0] = [stack[0]]
278 stack[0] = [stack[0]]
279 break
279 break
280 else:
280 else:
281 remain[-1] = remain[-1] - 1
281 remain[-1] = remain[-1] - 1
282 while remain[-1] == 0:
282 while remain[-1] == 0:
283 remain.pop()
283 remain.pop()
284 size = count.pop()
284 size = count.pop()
285 stack[-size:] = [stack[-size:]]
285 stack[-size:] = [stack[-size:]]
286 if not remain:
286 if not remain:
287 break
287 break
288 remain[-1] = remain[-1] - 1
288 remain[-1] = remain[-1] - 1
289 if not remain:
289 if not remain:
290 break
290 break
291 args[i] = stack[0]
291 args[i] = stack[0]
292
292
293 varargs = None
293 varargs = None
294 if co.co_flags & inspect.CO_VARARGS:
294 if co.co_flags & inspect.CO_VARARGS:
295 varargs = co.co_varnames[nargs]
295 varargs = co.co_varnames[nargs]
296 nargs = nargs + 1
296 nargs = nargs + 1
297 varkw = None
297 varkw = None
298 if co.co_flags & inspect.CO_VARKEYWORDS:
298 if co.co_flags & inspect.CO_VARKEYWORDS:
299 varkw = co.co_varnames[nargs]
299 varkw = co.co_varnames[nargs]
300 return inspect.Arguments(args, varargs, varkw)
300 return inspect.Arguments(args, varargs, varkw)
301
301
302
302
303 # Monkeypatch inspect to apply our bugfix.
303 # Monkeypatch inspect to apply our bugfix.
304 def with_patch_inspect(f):
304 def with_patch_inspect(f):
305 """decorator for monkeypatching inspect.findsource"""
305 """decorator for monkeypatching inspect.findsource"""
306
306
307 def wrapped(*args, **kwargs):
307 def wrapped(*args, **kwargs):
308 save_findsource = inspect.findsource
308 save_findsource = inspect.findsource
309 save_getargs = inspect.getargs
309 save_getargs = inspect.getargs
310 inspect.findsource = findsource
310 inspect.findsource = findsource
311 inspect.getargs = getargs
311 inspect.getargs = getargs
312 try:
312 try:
313 return f(*args, **kwargs)
313 return f(*args, **kwargs)
314 finally:
314 finally:
315 inspect.findsource = save_findsource
315 inspect.findsource = save_findsource
316 inspect.getargs = save_getargs
316 inspect.getargs = save_getargs
317
317
318 return wrapped
318 return wrapped
319
319
320
320
321 if py3compat.PY3:
321 if py3compat.PY3:
322 fixed_getargvalues = inspect.getargvalues
322 fixed_getargvalues = inspect.getargvalues
323 else:
323 else:
324 # Fixes for https://github.com/ipython/ipython/issues/8293
324 # Fixes for https://github.com/ipython/ipython/issues/8293
325 # and https://github.com/ipython/ipython/issues/8205.
325 # and https://github.com/ipython/ipython/issues/8205.
326 # The relevant bug is caused by failure to correctly handle anonymous tuple
326 # The relevant bug is caused by failure to correctly handle anonymous tuple
327 # unpacking, which only exists in Python 2.
327 # unpacking, which only exists in Python 2.
328 fixed_getargvalues = with_patch_inspect(inspect.getargvalues)
328 fixed_getargvalues = with_patch_inspect(inspect.getargvalues)
329
329
330
330
331 def fix_frame_records_filenames(records):
331 def fix_frame_records_filenames(records):
332 """Try to fix the filenames in each record from inspect.getinnerframes().
332 """Try to fix the filenames in each record from inspect.getinnerframes().
333
333
334 Particularly, modules loaded from within zip files have useless filenames
334 Particularly, modules loaded from within zip files have useless filenames
335 attached to their code object, and inspect.getinnerframes() just uses it.
335 attached to their code object, and inspect.getinnerframes() just uses it.
336 """
336 """
337 fixed_records = []
337 fixed_records = []
338 for frame, filename, line_no, func_name, lines, index in records:
338 for frame, filename, line_no, func_name, lines, index in records:
339 # Look inside the frame's globals dictionary for __file__,
339 # Look inside the frame's globals dictionary for __file__,
340 # which should be better. However, keep Cython filenames since
340 # which should be better. However, keep Cython filenames since
341 # we prefer the source filenames over the compiled .so file.
341 # we prefer the source filenames over the compiled .so file.
342 filename = py3compat.cast_unicode_py2(filename, "utf-8")
342 filename = py3compat.cast_unicode_py2(filename, "utf-8")
343 if not filename.endswith(('.pyx', '.pxd', '.pxi')):
343 if not filename.endswith(('.pyx', '.pxd', '.pxi')):
344 better_fn = frame.f_globals.get('__file__', None)
344 better_fn = frame.f_globals.get('__file__', None)
345 if isinstance(better_fn, str):
345 if isinstance(better_fn, str):
346 # Check the type just in case someone did something weird with
346 # Check the type just in case someone did something weird with
347 # __file__. It might also be None if the error occurred during
347 # __file__. It might also be None if the error occurred during
348 # import.
348 # import.
349 filename = better_fn
349 filename = better_fn
350 fixed_records.append((frame, filename, line_no, func_name, lines, index))
350 fixed_records.append((frame, filename, line_no, func_name, lines, index))
351 return fixed_records
351 return fixed_records
352
352
353
353
354 @with_patch_inspect
354 @with_patch_inspect
355 def _fixed_getinnerframes(etb, context=1, tb_offset=0):
355 def _fixed_getinnerframes(etb, context=1, tb_offset=0):
356 LNUM_POS, LINES_POS, INDEX_POS = 2, 4, 5
356 LNUM_POS, LINES_POS, INDEX_POS = 2, 4, 5
357
357
358 records = fix_frame_records_filenames(inspect.getinnerframes(etb, context))
358 records = fix_frame_records_filenames(inspect.getinnerframes(etb, context))
359 # If the error is at the console, don't build any context, since it would
359 # If the error is at the console, don't build any context, since it would
360 # otherwise produce 5 blank lines printed out (there is no file at the
360 # otherwise produce 5 blank lines printed out (there is no file at the
361 # console)
361 # console)
362 rec_check = records[tb_offset:]
362 rec_check = records[tb_offset:]
363 try:
363 try:
364 rname = rec_check[0][1]
364 rname = rec_check[0][1]
365 if rname == '<ipython console>' or rname.endswith('<string>'):
365 if rname == '<ipython console>' or rname.endswith('<string>'):
366 return rec_check
366 return rec_check
367 except IndexError:
367 except IndexError:
368 pass
368 pass
369
369
370 aux = traceback.extract_tb(etb)
370 aux = traceback.extract_tb(etb)
371 assert len(records) == len(aux)
371 assert len(records) == len(aux)
372 for i, (file, lnum, _, _) in zip(range(len(records)), aux):
372 for i, (file, lnum, _, _) in zip(range(len(records)), aux):
373 maybeStart = lnum - 1 - context // 2
373 maybeStart = lnum - 1 - context // 2
374 start = max(maybeStart, 0)
374 start = max(maybeStart, 0)
375 end = start + context
375 end = start + context
376 lines = ulinecache.getlines(file)[start:end]
376 lines = ulinecache.getlines(file)[start:end]
377 buf = list(records[i])
377 buf = list(records[i])
378 buf[LNUM_POS] = lnum
378 buf[LNUM_POS] = lnum
379 buf[INDEX_POS] = lnum - 1 - start
379 buf[INDEX_POS] = lnum - 1 - start
380 buf[LINES_POS] = lines
380 buf[LINES_POS] = lines
381 records[i] = tuple(buf)
381 records[i] = tuple(buf)
382 return records[tb_offset:]
382 return records[tb_offset:]
383
383
384 # Helper function -- largely belongs to VerboseTB, but we need the same
384 # Helper function -- largely belongs to VerboseTB, but we need the same
385 # functionality to produce a pseudo verbose TB for SyntaxErrors, so that they
385 # functionality to produce a pseudo verbose TB for SyntaxErrors, so that they
386 # can be recognized properly by ipython.el's py-traceback-line-re
386 # can be recognized properly by ipython.el's py-traceback-line-re
387 # (SyntaxErrors have to be treated specially because they have no traceback)
387 # (SyntaxErrors have to be treated specially because they have no traceback)
388
388
389
389
390 def _format_traceback_lines(lnum, index, lines, Colors, lvals=None, scheme=None):
390 def _format_traceback_lines(lnum, index, lines, Colors, lvals=None, _line_format=(lambda x,_:x,None)):
391 numbers_width = INDENT_SIZE - 1
391 numbers_width = INDENT_SIZE - 1
392 res = []
392 res = []
393 i = lnum - index
393 i = lnum - index
394
394
395 _line_format = PyColorize.Parser(style=scheme).format2
396
397 for line in lines:
395 for line in lines:
398 line = py3compat.cast_unicode(line)
396 line = py3compat.cast_unicode(line)
399
397
400 new_line, err = _line_format(line, 'str')
398 new_line, err = _line_format(line, 'str')
401 if not err: line = new_line
399 if not err: line = new_line
402
400
403 if i == lnum:
401 if i == lnum:
404 # This is the line with the error
402 # This is the line with the error
405 pad = numbers_width - len(str(i))
403 pad = numbers_width - len(str(i))
406 num = '%s%s' % (debugger.make_arrow(pad), str(lnum))
404 num = '%s%s' % (debugger.make_arrow(pad), str(lnum))
407 line = '%s%s%s %s%s' % (Colors.linenoEm, num,
405 line = '%s%s%s %s%s' % (Colors.linenoEm, num,
408 Colors.line, line, Colors.Normal)
406 Colors.line, line, Colors.Normal)
409 else:
407 else:
410 num = '%*s' % (numbers_width, i)
408 num = '%*s' % (numbers_width, i)
411 line = '%s%s%s %s' % (Colors.lineno, num,
409 line = '%s%s%s %s' % (Colors.lineno, num,
412 Colors.Normal, line)
410 Colors.Normal, line)
413
411
414 res.append(line)
412 res.append(line)
415 if lvals and i == lnum:
413 if lvals and i == lnum:
416 res.append(lvals + '\n')
414 res.append(lvals + '\n')
417 i = i + 1
415 i = i + 1
418 return res
416 return res
419
417
420 def is_recursion_error(etype, value, records):
418 def is_recursion_error(etype, value, records):
421 try:
419 try:
422 # RecursionError is new in Python 3.5
420 # RecursionError is new in Python 3.5
423 recursion_error_type = RecursionError
421 recursion_error_type = RecursionError
424 except NameError:
422 except NameError:
425 recursion_error_type = RuntimeError
423 recursion_error_type = RuntimeError
426
424
427 # The default recursion limit is 1000, but some of that will be taken up
425 # The default recursion limit is 1000, but some of that will be taken up
428 # by stack frames in IPython itself. >500 frames probably indicates
426 # by stack frames in IPython itself. >500 frames probably indicates
429 # a recursion error.
427 # a recursion error.
430 return (etype is recursion_error_type) \
428 return (etype is recursion_error_type) \
431 and "recursion" in str(value).lower() \
429 and "recursion" in str(value).lower() \
432 and len(records) > 500
430 and len(records) > 500
433
431
434 def find_recursion(etype, value, records):
432 def find_recursion(etype, value, records):
435 """Identify the repeating stack frames from a RecursionError traceback
433 """Identify the repeating stack frames from a RecursionError traceback
436
434
437 'records' is a list as returned by VerboseTB.get_records()
435 'records' is a list as returned by VerboseTB.get_records()
438
436
439 Returns (last_unique, repeat_length)
437 Returns (last_unique, repeat_length)
440 """
438 """
441 # This involves a bit of guesswork - we want to show enough of the traceback
439 # This involves a bit of guesswork - we want to show enough of the traceback
442 # to indicate where the recursion is occurring. We guess that the innermost
440 # to indicate where the recursion is occurring. We guess that the innermost
443 # quarter of the traceback (250 frames by default) is repeats, and find the
441 # quarter of the traceback (250 frames by default) is repeats, and find the
444 # first frame (from in to out) that looks different.
442 # first frame (from in to out) that looks different.
445 if not is_recursion_error(etype, value, records):
443 if not is_recursion_error(etype, value, records):
446 return len(records), 0
444 return len(records), 0
447
445
448 # Select filename, lineno, func_name to track frames with
446 # Select filename, lineno, func_name to track frames with
449 records = [r[1:4] for r in records]
447 records = [r[1:4] for r in records]
450 inner_frames = records[-(len(records)//4):]
448 inner_frames = records[-(len(records)//4):]
451 frames_repeated = set(inner_frames)
449 frames_repeated = set(inner_frames)
452
450
453 last_seen_at = {}
451 last_seen_at = {}
454 longest_repeat = 0
452 longest_repeat = 0
455 i = len(records)
453 i = len(records)
456 for frame in reversed(records):
454 for frame in reversed(records):
457 i -= 1
455 i -= 1
458 if frame not in frames_repeated:
456 if frame not in frames_repeated:
459 last_unique = i
457 last_unique = i
460 break
458 break
461
459
462 if frame in last_seen_at:
460 if frame in last_seen_at:
463 distance = last_seen_at[frame] - i
461 distance = last_seen_at[frame] - i
464 longest_repeat = max(longest_repeat, distance)
462 longest_repeat = max(longest_repeat, distance)
465
463
466 last_seen_at[frame] = i
464 last_seen_at[frame] = i
467 else:
465 else:
468 last_unique = 0 # The whole traceback was recursion
466 last_unique = 0 # The whole traceback was recursion
469
467
470 return last_unique, longest_repeat
468 return last_unique, longest_repeat
471
469
472 #---------------------------------------------------------------------------
470 #---------------------------------------------------------------------------
473 # Module classes
471 # Module classes
474 class TBTools(colorable.Colorable):
472 class TBTools(colorable.Colorable):
475 """Basic tools used by all traceback printer classes."""
473 """Basic tools used by all traceback printer classes."""
476
474
477 # Number of frames to skip when reporting tracebacks
475 # Number of frames to skip when reporting tracebacks
478 tb_offset = 0
476 tb_offset = 0
479
477
480 def __init__(self, color_scheme='NoColor', call_pdb=False, ostream=None, parent=None, config=None):
478 def __init__(self, color_scheme='NoColor', call_pdb=False, ostream=None, parent=None, config=None):
481 # Whether to call the interactive pdb debugger after printing
479 # Whether to call the interactive pdb debugger after printing
482 # tracebacks or not
480 # tracebacks or not
483 super(TBTools, self).__init__(parent=parent, config=config)
481 super(TBTools, self).__init__(parent=parent, config=config)
484 self.call_pdb = call_pdb
482 self.call_pdb = call_pdb
485
483
486 # Output stream to write to. Note that we store the original value in
484 # Output stream to write to. Note that we store the original value in
487 # a private attribute and then make the public ostream a property, so
485 # a private attribute and then make the public ostream a property, so
488 # that we can delay accessing sys.stdout until runtime. The way
486 # that we can delay accessing sys.stdout until runtime. The way
489 # things are written now, the sys.stdout object is dynamically managed
487 # things are written now, the sys.stdout object is dynamically managed
490 # so a reference to it should NEVER be stored statically. This
488 # so a reference to it should NEVER be stored statically. This
491 # property approach confines this detail to a single location, and all
489 # property approach confines this detail to a single location, and all
492 # subclasses can simply access self.ostream for writing.
490 # subclasses can simply access self.ostream for writing.
493 self._ostream = ostream
491 self._ostream = ostream
494
492
495 # Create color table
493 # Create color table
496 self.color_scheme_table = exception_colors()
494 self.color_scheme_table = exception_colors()
497
495
498 self.set_colors(color_scheme)
496 self.set_colors(color_scheme)
499 self.old_scheme = color_scheme # save initial value for toggles
497 self.old_scheme = color_scheme # save initial value for toggles
500
498
501 if call_pdb:
499 if call_pdb:
502 self.pdb = debugger.Pdb()
500 self.pdb = debugger.Pdb()
503 else:
501 else:
504 self.pdb = None
502 self.pdb = None
505
503
506 def _get_ostream(self):
504 def _get_ostream(self):
507 """Output stream that exceptions are written to.
505 """Output stream that exceptions are written to.
508
506
509 Valid values are:
507 Valid values are:
510
508
511 - None: the default, which means that IPython will dynamically resolve
509 - None: the default, which means that IPython will dynamically resolve
512 to sys.stdout. This ensures compatibility with most tools, including
510 to sys.stdout. This ensures compatibility with most tools, including
513 Windows (where plain stdout doesn't recognize ANSI escapes).
511 Windows (where plain stdout doesn't recognize ANSI escapes).
514
512
515 - Any object with 'write' and 'flush' attributes.
513 - Any object with 'write' and 'flush' attributes.
516 """
514 """
517 return sys.stdout if self._ostream is None else self._ostream
515 return sys.stdout if self._ostream is None else self._ostream
518
516
519 def _set_ostream(self, val):
517 def _set_ostream(self, val):
520 assert val is None or (hasattr(val, 'write') and hasattr(val, 'flush'))
518 assert val is None or (hasattr(val, 'write') and hasattr(val, 'flush'))
521 self._ostream = val
519 self._ostream = val
522
520
523 ostream = property(_get_ostream, _set_ostream)
521 ostream = property(_get_ostream, _set_ostream)
524
522
525 def set_colors(self, *args, **kw):
523 def set_colors(self, *args, **kw):
526 """Shorthand access to the color table scheme selector method."""
524 """Shorthand access to the color table scheme selector method."""
527
525
528 # Set own color table
526 # Set own color table
529 self.color_scheme_table.set_active_scheme(*args, **kw)
527 self.color_scheme_table.set_active_scheme(*args, **kw)
530 # for convenience, set Colors to the active scheme
528 # for convenience, set Colors to the active scheme
531 self.Colors = self.color_scheme_table.active_colors
529 self.Colors = self.color_scheme_table.active_colors
532 # Also set colors of debugger
530 # Also set colors of debugger
533 if hasattr(self, 'pdb') and self.pdb is not None:
531 if hasattr(self, 'pdb') and self.pdb is not None:
534 self.pdb.set_colors(*args, **kw)
532 self.pdb.set_colors(*args, **kw)
535
533
536 def color_toggle(self):
534 def color_toggle(self):
537 """Toggle between the currently active color scheme and NoColor."""
535 """Toggle between the currently active color scheme and NoColor."""
538
536
539 if self.color_scheme_table.active_scheme_name == 'NoColor':
537 if self.color_scheme_table.active_scheme_name == 'NoColor':
540 self.color_scheme_table.set_active_scheme(self.old_scheme)
538 self.color_scheme_table.set_active_scheme(self.old_scheme)
541 self.Colors = self.color_scheme_table.active_colors
539 self.Colors = self.color_scheme_table.active_colors
542 else:
540 else:
543 self.old_scheme = self.color_scheme_table.active_scheme_name
541 self.old_scheme = self.color_scheme_table.active_scheme_name
544 self.color_scheme_table.set_active_scheme('NoColor')
542 self.color_scheme_table.set_active_scheme('NoColor')
545 self.Colors = self.color_scheme_table.active_colors
543 self.Colors = self.color_scheme_table.active_colors
546
544
547 def stb2text(self, stb):
545 def stb2text(self, stb):
548 """Convert a structured traceback (a list) to a string."""
546 """Convert a structured traceback (a list) to a string."""
549 return '\n'.join(stb)
547 return '\n'.join(stb)
550
548
551 def text(self, etype, value, tb, tb_offset=None, context=5):
549 def text(self, etype, value, tb, tb_offset=None, context=5):
552 """Return formatted traceback.
550 """Return formatted traceback.
553
551
554 Subclasses may override this if they add extra arguments.
552 Subclasses may override this if they add extra arguments.
555 """
553 """
556 tb_list = self.structured_traceback(etype, value, tb,
554 tb_list = self.structured_traceback(etype, value, tb,
557 tb_offset, context)
555 tb_offset, context)
558 return self.stb2text(tb_list)
556 return self.stb2text(tb_list)
559
557
560 def structured_traceback(self, etype, evalue, tb, tb_offset=None,
558 def structured_traceback(self, etype, evalue, tb, tb_offset=None,
561 context=5, mode=None):
559 context=5, mode=None):
562 """Return a list of traceback frames.
560 """Return a list of traceback frames.
563
561
564 Must be implemented by each class.
562 Must be implemented by each class.
565 """
563 """
566 raise NotImplementedError()
564 raise NotImplementedError()
567
565
568
566
569 #---------------------------------------------------------------------------
567 #---------------------------------------------------------------------------
570 class ListTB(TBTools):
568 class ListTB(TBTools):
571 """Print traceback information from a traceback list, with optional color.
569 """Print traceback information from a traceback list, with optional color.
572
570
573 Calling requires 3 arguments: (etype, evalue, elist)
571 Calling requires 3 arguments: (etype, evalue, elist)
574 as would be obtained by::
572 as would be obtained by::
575
573
576 etype, evalue, tb = sys.exc_info()
574 etype, evalue, tb = sys.exc_info()
577 if tb:
575 if tb:
578 elist = traceback.extract_tb(tb)
576 elist = traceback.extract_tb(tb)
579 else:
577 else:
580 elist = None
578 elist = None
581
579
582 It can thus be used by programs which need to process the traceback before
580 It can thus be used by programs which need to process the traceback before
583 printing (such as console replacements based on the code module from the
581 printing (such as console replacements based on the code module from the
584 standard library).
582 standard library).
585
583
586 Because they are meant to be called without a full traceback (only a
584 Because they are meant to be called without a full traceback (only a
587 list), instances of this class can't call the interactive pdb debugger."""
585 list), instances of this class can't call the interactive pdb debugger."""
588
586
589 def __init__(self, color_scheme='NoColor', call_pdb=False, ostream=None, parent=None):
587 def __init__(self, color_scheme='NoColor', call_pdb=False, ostream=None, parent=None, config=None):
590 TBTools.__init__(self, color_scheme=color_scheme, call_pdb=call_pdb,
588 TBTools.__init__(self, color_scheme=color_scheme, call_pdb=call_pdb,
591 ostream=ostream, parent=parent)
589 ostream=ostream, parent=parent,config=config)
592
590
593 def __call__(self, etype, value, elist):
591 def __call__(self, etype, value, elist):
594 self.ostream.flush()
592 self.ostream.flush()
595 self.ostream.write(self.text(etype, value, elist))
593 self.ostream.write(self.text(etype, value, elist))
596 self.ostream.write('\n')
594 self.ostream.write('\n')
597
595
598 def structured_traceback(self, etype, value, elist, tb_offset=None,
596 def structured_traceback(self, etype, value, elist, tb_offset=None,
599 context=5):
597 context=5):
600 """Return a color formatted string with the traceback info.
598 """Return a color formatted string with the traceback info.
601
599
602 Parameters
600 Parameters
603 ----------
601 ----------
604 etype : exception type
602 etype : exception type
605 Type of the exception raised.
603 Type of the exception raised.
606
604
607 value : object
605 value : object
608 Data stored in the exception
606 Data stored in the exception
609
607
610 elist : list
608 elist : list
611 List of frames, see class docstring for details.
609 List of frames, see class docstring for details.
612
610
613 tb_offset : int, optional
611 tb_offset : int, optional
614 Number of frames in the traceback to skip. If not given, the
612 Number of frames in the traceback to skip. If not given, the
615 instance value is used (set in constructor).
613 instance value is used (set in constructor).
616
614
617 context : int, optional
615 context : int, optional
618 Number of lines of context information to print.
616 Number of lines of context information to print.
619
617
620 Returns
618 Returns
621 -------
619 -------
622 String with formatted exception.
620 String with formatted exception.
623 """
621 """
624 tb_offset = self.tb_offset if tb_offset is None else tb_offset
622 tb_offset = self.tb_offset if tb_offset is None else tb_offset
625 Colors = self.Colors
623 Colors = self.Colors
626 out_list = []
624 out_list = []
627 if elist:
625 if elist:
628
626
629 if tb_offset and len(elist) > tb_offset:
627 if tb_offset and len(elist) > tb_offset:
630 elist = elist[tb_offset:]
628 elist = elist[tb_offset:]
631
629
632 out_list.append('Traceback %s(most recent call last)%s:' %
630 out_list.append('Traceback %s(most recent call last)%s:' %
633 (Colors.normalEm, Colors.Normal) + '\n')
631 (Colors.normalEm, Colors.Normal) + '\n')
634 out_list.extend(self._format_list(elist))
632 out_list.extend(self._format_list(elist))
635 # The exception info should be a single entry in the list.
633 # The exception info should be a single entry in the list.
636 lines = ''.join(self._format_exception_only(etype, value))
634 lines = ''.join(self._format_exception_only(etype, value))
637 out_list.append(lines)
635 out_list.append(lines)
638
636
639 # Note: this code originally read:
637 # Note: this code originally read:
640
638
641 ## for line in lines[:-1]:
639 ## for line in lines[:-1]:
642 ## out_list.append(" "+line)
640 ## out_list.append(" "+line)
643 ## out_list.append(lines[-1])
641 ## out_list.append(lines[-1])
644
642
645 # This means it was indenting everything but the last line by a little
643 # This means it was indenting everything but the last line by a little
646 # bit. I've disabled this for now, but if we see ugliness somewhere we
644 # bit. I've disabled this for now, but if we see ugliness somewhere we
647 # can restore it.
645 # can restore it.
648
646
649 return out_list
647 return out_list
650
648
651 def _format_list(self, extracted_list):
649 def _format_list(self, extracted_list):
652 """Format a list of traceback entry tuples for printing.
650 """Format a list of traceback entry tuples for printing.
653
651
654 Given a list of tuples as returned by extract_tb() or
652 Given a list of tuples as returned by extract_tb() or
655 extract_stack(), return a list of strings ready for printing.
653 extract_stack(), return a list of strings ready for printing.
656 Each string in the resulting list corresponds to the item with the
654 Each string in the resulting list corresponds to the item with the
657 same index in the argument list. Each string ends in a newline;
655 same index in the argument list. Each string ends in a newline;
658 the strings may contain internal newlines as well, for those items
656 the strings may contain internal newlines as well, for those items
659 whose source text line is not None.
657 whose source text line is not None.
660
658
661 Lifted almost verbatim from traceback.py
659 Lifted almost verbatim from traceback.py
662 """
660 """
663
661
664 Colors = self.Colors
662 Colors = self.Colors
665 list = []
663 list = []
666 for filename, lineno, name, line in extracted_list[:-1]:
664 for filename, lineno, name, line in extracted_list[:-1]:
667 item = ' File %s"%s"%s, line %s%d%s, in %s%s%s\n' % \
665 item = ' File %s"%s"%s, line %s%d%s, in %s%s%s\n' % \
668 (Colors.filename, py3compat.cast_unicode_py2(filename, "utf-8"), Colors.Normal,
666 (Colors.filename, py3compat.cast_unicode_py2(filename, "utf-8"), Colors.Normal,
669 Colors.lineno, lineno, Colors.Normal,
667 Colors.lineno, lineno, Colors.Normal,
670 Colors.name, py3compat.cast_unicode_py2(name, "utf-8"), Colors.Normal)
668 Colors.name, py3compat.cast_unicode_py2(name, "utf-8"), Colors.Normal)
671 if line:
669 if line:
672 item += ' %s\n' % line.strip()
670 item += ' %s\n' % line.strip()
673 list.append(item)
671 list.append(item)
674 # Emphasize the last entry
672 # Emphasize the last entry
675 filename, lineno, name, line = extracted_list[-1]
673 filename, lineno, name, line = extracted_list[-1]
676 item = '%s File %s"%s"%s, line %s%d%s, in %s%s%s%s\n' % \
674 item = '%s File %s"%s"%s, line %s%d%s, in %s%s%s%s\n' % \
677 (Colors.normalEm,
675 (Colors.normalEm,
678 Colors.filenameEm, py3compat.cast_unicode_py2(filename, "utf-8"), Colors.normalEm,
676 Colors.filenameEm, py3compat.cast_unicode_py2(filename, "utf-8"), Colors.normalEm,
679 Colors.linenoEm, lineno, Colors.normalEm,
677 Colors.linenoEm, lineno, Colors.normalEm,
680 Colors.nameEm, py3compat.cast_unicode_py2(name, "utf-8"), Colors.normalEm,
678 Colors.nameEm, py3compat.cast_unicode_py2(name, "utf-8"), Colors.normalEm,
681 Colors.Normal)
679 Colors.Normal)
682 if line:
680 if line:
683 item += '%s %s%s\n' % (Colors.line, line.strip(),
681 item += '%s %s%s\n' % (Colors.line, line.strip(),
684 Colors.Normal)
682 Colors.Normal)
685 list.append(item)
683 list.append(item)
686 return list
684 return list
687
685
688 def _format_exception_only(self, etype, value):
686 def _format_exception_only(self, etype, value):
689 """Format the exception part of a traceback.
687 """Format the exception part of a traceback.
690
688
691 The arguments are the exception type and value such as given by
689 The arguments are the exception type and value such as given by
692 sys.exc_info()[:2]. The return value is a list of strings, each ending
690 sys.exc_info()[:2]. The return value is a list of strings, each ending
693 in a newline. Normally, the list contains a single string; however,
691 in a newline. Normally, the list contains a single string; however,
694 for SyntaxError exceptions, it contains several lines that (when
692 for SyntaxError exceptions, it contains several lines that (when
695 printed) display detailed information about where the syntax error
693 printed) display detailed information about where the syntax error
696 occurred. The message indicating which exception occurred is the
694 occurred. The message indicating which exception occurred is the
697 always last string in the list.
695 always last string in the list.
698
696
699 Also lifted nearly verbatim from traceback.py
697 Also lifted nearly verbatim from traceback.py
700 """
698 """
701 have_filedata = False
699 have_filedata = False
702 Colors = self.Colors
700 Colors = self.Colors
703 list = []
701 list = []
704 stype = py3compat.cast_unicode(Colors.excName + etype.__name__ + Colors.Normal)
702 stype = py3compat.cast_unicode(Colors.excName + etype.__name__ + Colors.Normal)
705 if value is None:
703 if value is None:
706 # Not sure if this can still happen in Python 2.6 and above
704 # Not sure if this can still happen in Python 2.6 and above
707 list.append(stype + '\n')
705 list.append(stype + '\n')
708 else:
706 else:
709 if issubclass(etype, SyntaxError):
707 if issubclass(etype, SyntaxError):
710 have_filedata = True
708 have_filedata = True
711 if not value.filename: value.filename = "<string>"
709 if not value.filename: value.filename = "<string>"
712 if value.lineno:
710 if value.lineno:
713 lineno = value.lineno
711 lineno = value.lineno
714 textline = ulinecache.getline(value.filename, value.lineno)
712 textline = ulinecache.getline(value.filename, value.lineno)
715 else:
713 else:
716 lineno = 'unknown'
714 lineno = 'unknown'
717 textline = ''
715 textline = ''
718 list.append('%s File %s"%s"%s, line %s%s%s\n' % \
716 list.append('%s File %s"%s"%s, line %s%s%s\n' % \
719 (Colors.normalEm,
717 (Colors.normalEm,
720 Colors.filenameEm, py3compat.cast_unicode(value.filename), Colors.normalEm,
718 Colors.filenameEm, py3compat.cast_unicode(value.filename), Colors.normalEm,
721 Colors.linenoEm, lineno, Colors.Normal ))
719 Colors.linenoEm, lineno, Colors.Normal ))
722 if textline == '':
720 if textline == '':
723 textline = py3compat.cast_unicode(value.text, "utf-8")
721 textline = py3compat.cast_unicode(value.text, "utf-8")
724
722
725 if textline is not None:
723 if textline is not None:
726 i = 0
724 i = 0
727 while i < len(textline) and textline[i].isspace():
725 while i < len(textline) and textline[i].isspace():
728 i += 1
726 i += 1
729 list.append('%s %s%s\n' % (Colors.line,
727 list.append('%s %s%s\n' % (Colors.line,
730 textline.strip(),
728 textline.strip(),
731 Colors.Normal))
729 Colors.Normal))
732 if value.offset is not None:
730 if value.offset is not None:
733 s = ' '
731 s = ' '
734 for c in textline[i:value.offset - 1]:
732 for c in textline[i:value.offset - 1]:
735 if c.isspace():
733 if c.isspace():
736 s += c
734 s += c
737 else:
735 else:
738 s += ' '
736 s += ' '
739 list.append('%s%s^%s\n' % (Colors.caret, s,
737 list.append('%s%s^%s\n' % (Colors.caret, s,
740 Colors.Normal))
738 Colors.Normal))
741
739
742 try:
740 try:
743 s = value.msg
741 s = value.msg
744 except Exception:
742 except Exception:
745 s = self._some_str(value)
743 s = self._some_str(value)
746 if s:
744 if s:
747 list.append('%s%s:%s %s\n' % (stype, Colors.excName,
745 list.append('%s%s:%s %s\n' % (stype, Colors.excName,
748 Colors.Normal, s))
746 Colors.Normal, s))
749 else:
747 else:
750 list.append('%s\n' % stype)
748 list.append('%s\n' % stype)
751
749
752 # sync with user hooks
750 # sync with user hooks
753 if have_filedata:
751 if have_filedata:
754 ipinst = get_ipython()
752 ipinst = get_ipython()
755 if ipinst is not None:
753 if ipinst is not None:
756 ipinst.hooks.synchronize_with_editor(value.filename, value.lineno, 0)
754 ipinst.hooks.synchronize_with_editor(value.filename, value.lineno, 0)
757
755
758 return list
756 return list
759
757
760 def get_exception_only(self, etype, value):
758 def get_exception_only(self, etype, value):
761 """Only print the exception type and message, without a traceback.
759 """Only print the exception type and message, without a traceback.
762
760
763 Parameters
761 Parameters
764 ----------
762 ----------
765 etype : exception type
763 etype : exception type
766 value : exception value
764 value : exception value
767 """
765 """
768 return ListTB.structured_traceback(self, etype, value, [])
766 return ListTB.structured_traceback(self, etype, value, [])
769
767
770 def show_exception_only(self, etype, evalue):
768 def show_exception_only(self, etype, evalue):
771 """Only print the exception type and message, without a traceback.
769 """Only print the exception type and message, without a traceback.
772
770
773 Parameters
771 Parameters
774 ----------
772 ----------
775 etype : exception type
773 etype : exception type
776 value : exception value
774 value : exception value
777 """
775 """
778 # This method needs to use __call__ from *this* class, not the one from
776 # This method needs to use __call__ from *this* class, not the one from
779 # a subclass whose signature or behavior may be different
777 # a subclass whose signature or behavior may be different
780 ostream = self.ostream
778 ostream = self.ostream
781 ostream.flush()
779 ostream.flush()
782 ostream.write('\n'.join(self.get_exception_only(etype, evalue)))
780 ostream.write('\n'.join(self.get_exception_only(etype, evalue)))
783 ostream.flush()
781 ostream.flush()
784
782
785 def _some_str(self, value):
783 def _some_str(self, value):
786 # Lifted from traceback.py
784 # Lifted from traceback.py
787 try:
785 try:
788 return py3compat.cast_unicode(str(value))
786 return py3compat.cast_unicode(str(value))
789 except:
787 except:
790 return u'<unprintable %s object>' % type(value).__name__
788 return u'<unprintable %s object>' % type(value).__name__
791
789
792
790
793 #----------------------------------------------------------------------------
791 #----------------------------------------------------------------------------
794 class VerboseTB(TBTools):
792 class VerboseTB(TBTools):
795 """A port of Ka-Ping Yee's cgitb.py module that outputs color text instead
793 """A port of Ka-Ping Yee's cgitb.py module that outputs color text instead
796 of HTML. Requires inspect and pydoc. Crazy, man.
794 of HTML. Requires inspect and pydoc. Crazy, man.
797
795
798 Modified version which optionally strips the topmost entries from the
796 Modified version which optionally strips the topmost entries from the
799 traceback, to be used with alternate interpreters (because their own code
797 traceback, to be used with alternate interpreters (because their own code
800 would appear in the traceback)."""
798 would appear in the traceback)."""
801
799
802 def __init__(self, color_scheme='Linux', call_pdb=False, ostream=None,
800 def __init__(self, color_scheme='Linux', call_pdb=False, ostream=None,
803 tb_offset=0, long_header=False, include_vars=True,
801 tb_offset=0, long_header=False, include_vars=True,
804 check_cache=None, debugger_cls = None):
802 check_cache=None, debugger_cls = None,
803 parent=None, config=None):
805 """Specify traceback offset, headers and color scheme.
804 """Specify traceback offset, headers and color scheme.
806
805
807 Define how many frames to drop from the tracebacks. Calling it with
806 Define how many frames to drop from the tracebacks. Calling it with
808 tb_offset=1 allows use of this handler in interpreters which will have
807 tb_offset=1 allows use of this handler in interpreters which will have
809 their own code at the top of the traceback (VerboseTB will first
808 their own code at the top of the traceback (VerboseTB will first
810 remove that frame before printing the traceback info)."""
809 remove that frame before printing the traceback info)."""
811 TBTools.__init__(self, color_scheme=color_scheme, call_pdb=call_pdb,
810 TBTools.__init__(self, color_scheme=color_scheme, call_pdb=call_pdb,
812 ostream=ostream)
811 ostream=ostream, parent=parent, config=config)
813 self.tb_offset = tb_offset
812 self.tb_offset = tb_offset
814 self.long_header = long_header
813 self.long_header = long_header
815 self.include_vars = include_vars
814 self.include_vars = include_vars
816 # By default we use linecache.checkcache, but the user can provide a
815 # By default we use linecache.checkcache, but the user can provide a
817 # different check_cache implementation. This is used by the IPython
816 # different check_cache implementation. This is used by the IPython
818 # kernel to provide tracebacks for interactive code that is cached,
817 # kernel to provide tracebacks for interactive code that is cached,
819 # by a compiler instance that flushes the linecache but preserves its
818 # by a compiler instance that flushes the linecache but preserves its
820 # own code cache.
819 # own code cache.
821 if check_cache is None:
820 if check_cache is None:
822 check_cache = linecache.checkcache
821 check_cache = linecache.checkcache
823 self.check_cache = check_cache
822 self.check_cache = check_cache
824
823
825 self.debugger_cls = debugger_cls or debugger.Pdb
824 self.debugger_cls = debugger_cls or debugger.Pdb
826
825
827 def format_records(self, records, last_unique, recursion_repeat):
826 def format_records(self, records, last_unique, recursion_repeat):
828 """Format the stack frames of the traceback"""
827 """Format the stack frames of the traceback"""
829 frames = []
828 frames = []
830 for r in records[:last_unique+recursion_repeat+1]:
829 for r in records[:last_unique+recursion_repeat+1]:
831 #print '*** record:',file,lnum,func,lines,index # dbg
830 #print '*** record:',file,lnum,func,lines,index # dbg
832 frames.append(self.format_record(*r))
831 frames.append(self.format_record(*r))
833
832
834 if recursion_repeat:
833 if recursion_repeat:
835 frames.append('... last %d frames repeated, from the frame below ...\n' % recursion_repeat)
834 frames.append('... last %d frames repeated, from the frame below ...\n' % recursion_repeat)
836 frames.append(self.format_record(*records[last_unique+recursion_repeat+1]))
835 frames.append(self.format_record(*records[last_unique+recursion_repeat+1]))
837
836
838 return frames
837 return frames
839
838
840 def format_record(self, frame, file, lnum, func, lines, index):
839 def format_record(self, frame, file, lnum, func, lines, index):
841 """Format a single stack frame"""
840 """Format a single stack frame"""
842 Colors = self.Colors # just a shorthand + quicker name lookup
841 Colors = self.Colors # just a shorthand + quicker name lookup
843 ColorsNormal = Colors.Normal # used a lot
842 ColorsNormal = Colors.Normal # used a lot
844 col_scheme = self.color_scheme_table.active_scheme_name
843 col_scheme = self.color_scheme_table.active_scheme_name
845 indent = ' ' * INDENT_SIZE
844 indent = ' ' * INDENT_SIZE
846 em_normal = '%s\n%s%s' % (Colors.valEm, indent, ColorsNormal)
845 em_normal = '%s\n%s%s' % (Colors.valEm, indent, ColorsNormal)
847 undefined = '%sundefined%s' % (Colors.em, ColorsNormal)
846 undefined = '%sundefined%s' % (Colors.em, ColorsNormal)
848 tpl_link = '%s%%s%s' % (Colors.filenameEm, ColorsNormal)
847 tpl_link = '%s%%s%s' % (Colors.filenameEm, ColorsNormal)
849 tpl_call = 'in %s%%s%s%%s%s' % (Colors.vName, Colors.valEm,
848 tpl_call = 'in %s%%s%s%%s%s' % (Colors.vName, Colors.valEm,
850 ColorsNormal)
849 ColorsNormal)
851 tpl_call_fail = 'in %s%%s%s(***failed resolving arguments***)%s' % \
850 tpl_call_fail = 'in %s%%s%s(***failed resolving arguments***)%s' % \
852 (Colors.vName, Colors.valEm, ColorsNormal)
851 (Colors.vName, Colors.valEm, ColorsNormal)
853 tpl_local_var = '%s%%s%s' % (Colors.vName, ColorsNormal)
852 tpl_local_var = '%s%%s%s' % (Colors.vName, ColorsNormal)
854 tpl_global_var = '%sglobal%s %s%%s%s' % (Colors.em, ColorsNormal,
853 tpl_global_var = '%sglobal%s %s%%s%s' % (Colors.em, ColorsNormal,
855 Colors.vName, ColorsNormal)
854 Colors.vName, ColorsNormal)
856 tpl_name_val = '%%s %s= %%s%s' % (Colors.valEm, ColorsNormal)
855 tpl_name_val = '%%s %s= %%s%s' % (Colors.valEm, ColorsNormal)
857
856
858 tpl_line = '%s%%s%s %%s' % (Colors.lineno, ColorsNormal)
857 tpl_line = '%s%%s%s %%s' % (Colors.lineno, ColorsNormal)
859 tpl_line_em = '%s%%s%s %%s%s' % (Colors.linenoEm, Colors.line,
858 tpl_line_em = '%s%%s%s %%s%s' % (Colors.linenoEm, Colors.line,
860 ColorsNormal)
859 ColorsNormal)
861
860
862 abspath = os.path.abspath
861 abspath = os.path.abspath
863
862
864
863
865 if not file:
864 if not file:
866 file = '?'
865 file = '?'
867 elif file.startswith(str("<")) and file.endswith(str(">")):
866 elif file.startswith(str("<")) and file.endswith(str(">")):
868 # Not a real filename, no problem...
867 # Not a real filename, no problem...
869 pass
868 pass
870 elif not os.path.isabs(file):
869 elif not os.path.isabs(file):
871 # Try to make the filename absolute by trying all
870 # Try to make the filename absolute by trying all
872 # sys.path entries (which is also what linecache does)
871 # sys.path entries (which is also what linecache does)
873 for dirname in sys.path:
872 for dirname in sys.path:
874 try:
873 try:
875 fullname = os.path.join(dirname, file)
874 fullname = os.path.join(dirname, file)
876 if os.path.isfile(fullname):
875 if os.path.isfile(fullname):
877 file = os.path.abspath(fullname)
876 file = os.path.abspath(fullname)
878 break
877 break
879 except Exception:
878 except Exception:
880 # Just in case that sys.path contains very
879 # Just in case that sys.path contains very
881 # strange entries...
880 # strange entries...
882 pass
881 pass
883
882
884 file = py3compat.cast_unicode(file, util_path.fs_encoding)
883 file = py3compat.cast_unicode(file, util_path.fs_encoding)
885 link = tpl_link % file
884 link = tpl_link % file
886 args, varargs, varkw, locals = fixed_getargvalues(frame)
885 args, varargs, varkw, locals = fixed_getargvalues(frame)
887
886
888 if func == '?':
887 if func == '?':
889 call = ''
888 call = ''
890 else:
889 else:
891 # Decide whether to include variable details or not
890 # Decide whether to include variable details or not
892 var_repr = self.include_vars and eqrepr or nullrepr
891 var_repr = self.include_vars and eqrepr or nullrepr
893 try:
892 try:
894 call = tpl_call % (func, inspect.formatargvalues(args,
893 call = tpl_call % (func, inspect.formatargvalues(args,
895 varargs, varkw,
894 varargs, varkw,
896 locals, formatvalue=var_repr))
895 locals, formatvalue=var_repr))
897 except KeyError:
896 except KeyError:
898 # This happens in situations like errors inside generator
897 # This happens in situations like errors inside generator
899 # expressions, where local variables are listed in the
898 # expressions, where local variables are listed in the
900 # line, but can't be extracted from the frame. I'm not
899 # line, but can't be extracted from the frame. I'm not
901 # 100% sure this isn't actually a bug in inspect itself,
900 # 100% sure this isn't actually a bug in inspect itself,
902 # but since there's no info for us to compute with, the
901 # but since there's no info for us to compute with, the
903 # best we can do is report the failure and move on. Here
902 # best we can do is report the failure and move on. Here
904 # we must *not* call any traceback construction again,
903 # we must *not* call any traceback construction again,
905 # because that would mess up use of %debug later on. So we
904 # because that would mess up use of %debug later on. So we
906 # simply report the failure and move on. The only
905 # simply report the failure and move on. The only
907 # limitation will be that this frame won't have locals
906 # limitation will be that this frame won't have locals
908 # listed in the call signature. Quite subtle problem...
907 # listed in the call signature. Quite subtle problem...
909 # I can't think of a good way to validate this in a unit
908 # I can't think of a good way to validate this in a unit
910 # test, but running a script consisting of:
909 # test, but running a script consisting of:
911 # dict( (k,v.strip()) for (k,v) in range(10) )
910 # dict( (k,v.strip()) for (k,v) in range(10) )
912 # will illustrate the error, if this exception catch is
911 # will illustrate the error, if this exception catch is
913 # disabled.
912 # disabled.
914 call = tpl_call_fail % func
913 call = tpl_call_fail % func
915
914
916 # Don't attempt to tokenize binary files.
915 # Don't attempt to tokenize binary files.
917 if file.endswith(('.so', '.pyd', '.dll')):
916 if file.endswith(('.so', '.pyd', '.dll')):
918 return '%s %s\n' % (link, call)
917 return '%s %s\n' % (link, call)
919
918
920 elif file.endswith(('.pyc', '.pyo')):
919 elif file.endswith(('.pyc', '.pyo')):
921 # Look up the corresponding source file.
920 # Look up the corresponding source file.
922 try:
921 try:
923 file = openpy.source_from_cache(file)
922 file = openpy.source_from_cache(file)
924 except ValueError:
923 except ValueError:
925 # Failed to get the source file for some reason
924 # Failed to get the source file for some reason
926 # E.g. https://github.com/ipython/ipython/issues/9486
925 # E.g. https://github.com/ipython/ipython/issues/9486
927 return '%s %s\n' % (link, call)
926 return '%s %s\n' % (link, call)
928
927
929 def linereader(file=file, lnum=[lnum], getline=ulinecache.getline):
928 def linereader(file=file, lnum=[lnum], getline=ulinecache.getline):
930 line = getline(file, lnum[0])
929 line = getline(file, lnum[0])
931 lnum[0] += 1
930 lnum[0] += 1
932 return line
931 return line
933
932
934 # Build the list of names on this line of code where the exception
933 # Build the list of names on this line of code where the exception
935 # occurred.
934 # occurred.
936 try:
935 try:
937 names = []
936 names = []
938 name_cont = False
937 name_cont = False
939
938
940 for token_type, token, start, end, line in generate_tokens(linereader):
939 for token_type, token, start, end, line in generate_tokens(linereader):
941 # build composite names
940 # build composite names
942 if token_type == tokenize.NAME and token not in keyword.kwlist:
941 if token_type == tokenize.NAME and token not in keyword.kwlist:
943 if name_cont:
942 if name_cont:
944 # Continuation of a dotted name
943 # Continuation of a dotted name
945 try:
944 try:
946 names[-1].append(token)
945 names[-1].append(token)
947 except IndexError:
946 except IndexError:
948 names.append([token])
947 names.append([token])
949 name_cont = False
948 name_cont = False
950 else:
949 else:
951 # Regular new names. We append everything, the caller
950 # Regular new names. We append everything, the caller
952 # will be responsible for pruning the list later. It's
951 # will be responsible for pruning the list later. It's
953 # very tricky to try to prune as we go, b/c composite
952 # very tricky to try to prune as we go, b/c composite
954 # names can fool us. The pruning at the end is easy
953 # names can fool us. The pruning at the end is easy
955 # to do (or the caller can print a list with repeated
954 # to do (or the caller can print a list with repeated
956 # names if so desired.
955 # names if so desired.
957 names.append([token])
956 names.append([token])
958 elif token == '.':
957 elif token == '.':
959 name_cont = True
958 name_cont = True
960 elif token_type == tokenize.NEWLINE:
959 elif token_type == tokenize.NEWLINE:
961 break
960 break
962
961
963 except (IndexError, UnicodeDecodeError, SyntaxError):
962 except (IndexError, UnicodeDecodeError, SyntaxError):
964 # signals exit of tokenizer
963 # signals exit of tokenizer
965 # SyntaxError can occur if the file is not actually Python
964 # SyntaxError can occur if the file is not actually Python
966 # - see gh-6300
965 # - see gh-6300
967 pass
966 pass
968 except tokenize.TokenError as msg:
967 except tokenize.TokenError as msg:
969 _m = ("An unexpected error occurred while tokenizing input\n"
968 _m = ("An unexpected error occurred while tokenizing input\n"
970 "The following traceback may be corrupted or invalid\n"
969 "The following traceback may be corrupted or invalid\n"
971 "The error message is: %s\n" % msg)
970 "The error message is: %s\n" % msg)
972 error(_m)
971 error(_m)
973
972
974 # Join composite names (e.g. "dict.fromkeys")
973 # Join composite names (e.g. "dict.fromkeys")
975 names = ['.'.join(n) for n in names]
974 names = ['.'.join(n) for n in names]
976 # prune names list of duplicates, but keep the right order
975 # prune names list of duplicates, but keep the right order
977 unique_names = uniq_stable(names)
976 unique_names = uniq_stable(names)
978
977
979 # Start loop over vars
978 # Start loop over vars
980 lvals = []
979 lvals = []
981 if self.include_vars:
980 if self.include_vars:
982 for name_full in unique_names:
981 for name_full in unique_names:
983 name_base = name_full.split('.', 1)[0]
982 name_base = name_full.split('.', 1)[0]
984 if name_base in frame.f_code.co_varnames:
983 if name_base in frame.f_code.co_varnames:
985 if name_base in locals:
984 if name_base in locals:
986 try:
985 try:
987 value = repr(eval(name_full, locals))
986 value = repr(eval(name_full, locals))
988 except:
987 except:
989 value = undefined
988 value = undefined
990 else:
989 else:
991 value = undefined
990 value = undefined
992 name = tpl_local_var % name_full
991 name = tpl_local_var % name_full
993 else:
992 else:
994 if name_base in frame.f_globals:
993 if name_base in frame.f_globals:
995 try:
994 try:
996 value = repr(eval(name_full, frame.f_globals))
995 value = repr(eval(name_full, frame.f_globals))
997 except:
996 except:
998 value = undefined
997 value = undefined
999 else:
998 else:
1000 value = undefined
999 value = undefined
1001 name = tpl_global_var % name_full
1000 name = tpl_global_var % name_full
1002 lvals.append(tpl_name_val % (name, value))
1001 lvals.append(tpl_name_val % (name, value))
1003 if lvals:
1002 if lvals:
1004 lvals = '%s%s' % (indent, em_normal.join(lvals))
1003 lvals = '%s%s' % (indent, em_normal.join(lvals))
1005 else:
1004 else:
1006 lvals = ''
1005 lvals = ''
1007
1006
1008 level = '%s %s\n' % (link, call)
1007 level = '%s %s\n' % (link, call)
1009
1008
1010 if index is None:
1009 if index is None:
1011 return level
1010 return level
1012 else:
1011 else:
1012 _line_format = PyColorize.Parser(style=col_scheme, parent=self).format2
1013 return '%s%s' % (level, ''.join(
1013 return '%s%s' % (level, ''.join(
1014 _format_traceback_lines(lnum, index, lines, Colors, lvals,
1014 _format_traceback_lines(lnum, index, lines, Colors, lvals,
1015 col_scheme)))
1015 _line_format)))
1016
1016
1017 def prepare_chained_exception_message(self, cause):
1017 def prepare_chained_exception_message(self, cause):
1018 direct_cause = "\nThe above exception was the direct cause of the following exception:\n"
1018 direct_cause = "\nThe above exception was the direct cause of the following exception:\n"
1019 exception_during_handling = "\nDuring handling of the above exception, another exception occurred:\n"
1019 exception_during_handling = "\nDuring handling of the above exception, another exception occurred:\n"
1020
1020
1021 if cause:
1021 if cause:
1022 message = [[direct_cause]]
1022 message = [[direct_cause]]
1023 else:
1023 else:
1024 message = [[exception_during_handling]]
1024 message = [[exception_during_handling]]
1025 return message
1025 return message
1026
1026
1027 def prepare_header(self, etype, long_version=False):
1027 def prepare_header(self, etype, long_version=False):
1028 colors = self.Colors # just a shorthand + quicker name lookup
1028 colors = self.Colors # just a shorthand + quicker name lookup
1029 colorsnormal = colors.Normal # used a lot
1029 colorsnormal = colors.Normal # used a lot
1030 exc = '%s%s%s' % (colors.excName, etype, colorsnormal)
1030 exc = '%s%s%s' % (colors.excName, etype, colorsnormal)
1031 width = min(75, get_terminal_size()[0])
1031 width = min(75, get_terminal_size()[0])
1032 if long_version:
1032 if long_version:
1033 # Header with the exception type, python version, and date
1033 # Header with the exception type, python version, and date
1034 pyver = 'Python ' + sys.version.split()[0] + ': ' + sys.executable
1034 pyver = 'Python ' + sys.version.split()[0] + ': ' + sys.executable
1035 date = time.ctime(time.time())
1035 date = time.ctime(time.time())
1036
1036
1037 head = '%s%s%s\n%s%s%s\n%s' % (colors.topline, '-' * width, colorsnormal,
1037 head = '%s%s%s\n%s%s%s\n%s' % (colors.topline, '-' * width, colorsnormal,
1038 exc, ' ' * (width - len(str(etype)) - len(pyver)),
1038 exc, ' ' * (width - len(str(etype)) - len(pyver)),
1039 pyver, date.rjust(width) )
1039 pyver, date.rjust(width) )
1040 head += "\nA problem occurred executing Python code. Here is the sequence of function" \
1040 head += "\nA problem occurred executing Python code. Here is the sequence of function" \
1041 "\ncalls leading up to the error, with the most recent (innermost) call last."
1041 "\ncalls leading up to the error, with the most recent (innermost) call last."
1042 else:
1042 else:
1043 # Simplified header
1043 # Simplified header
1044 head = '%s%s' % (exc, 'Traceback (most recent call last)'. \
1044 head = '%s%s' % (exc, 'Traceback (most recent call last)'. \
1045 rjust(width - len(str(etype))) )
1045 rjust(width - len(str(etype))) )
1046
1046
1047 return head
1047 return head
1048
1048
1049 def format_exception(self, etype, evalue):
1049 def format_exception(self, etype, evalue):
1050 colors = self.Colors # just a shorthand + quicker name lookup
1050 colors = self.Colors # just a shorthand + quicker name lookup
1051 colorsnormal = colors.Normal # used a lot
1051 colorsnormal = colors.Normal # used a lot
1052 indent = ' ' * INDENT_SIZE
1052 indent = ' ' * INDENT_SIZE
1053 # Get (safely) a string form of the exception info
1053 # Get (safely) a string form of the exception info
1054 try:
1054 try:
1055 etype_str, evalue_str = map(str, (etype, evalue))
1055 etype_str, evalue_str = map(str, (etype, evalue))
1056 except:
1056 except:
1057 # User exception is improperly defined.
1057 # User exception is improperly defined.
1058 etype, evalue = str, sys.exc_info()[:2]
1058 etype, evalue = str, sys.exc_info()[:2]
1059 etype_str, evalue_str = map(str, (etype, evalue))
1059 etype_str, evalue_str = map(str, (etype, evalue))
1060 # ... and format it
1060 # ... and format it
1061 exception = ['%s%s%s: %s' % (colors.excName, etype_str,
1061 exception = ['%s%s%s: %s' % (colors.excName, etype_str,
1062 colorsnormal, py3compat.cast_unicode(evalue_str))]
1062 colorsnormal, py3compat.cast_unicode(evalue_str))]
1063
1063
1064 if (not py3compat.PY3) and type(evalue) is types.InstanceType:
1064 if (not py3compat.PY3) and type(evalue) is types.InstanceType:
1065 try:
1065 try:
1066 names = [w for w in dir(evalue) if isinstance(w, py3compat.string_types)]
1066 names = [w for w in dir(evalue) if isinstance(w, py3compat.string_types)]
1067 except:
1067 except:
1068 # Every now and then, an object with funny internals blows up
1068 # Every now and then, an object with funny internals blows up
1069 # when dir() is called on it. We do the best we can to report
1069 # when dir() is called on it. We do the best we can to report
1070 # the problem and continue
1070 # the problem and continue
1071 _m = '%sException reporting error (object with broken dir())%s:'
1071 _m = '%sException reporting error (object with broken dir())%s:'
1072 exception.append(_m % (colors.excName, colorsnormal))
1072 exception.append(_m % (colors.excName, colorsnormal))
1073 etype_str, evalue_str = map(str, sys.exc_info()[:2])
1073 etype_str, evalue_str = map(str, sys.exc_info()[:2])
1074 exception.append('%s%s%s: %s' % (colors.excName, etype_str,
1074 exception.append('%s%s%s: %s' % (colors.excName, etype_str,
1075 colorsnormal, py3compat.cast_unicode(evalue_str)))
1075 colorsnormal, py3compat.cast_unicode(evalue_str)))
1076 names = []
1076 names = []
1077 for name in names:
1077 for name in names:
1078 value = text_repr(getattr(evalue, name))
1078 value = text_repr(getattr(evalue, name))
1079 exception.append('\n%s%s = %s' % (indent, name, value))
1079 exception.append('\n%s%s = %s' % (indent, name, value))
1080
1080
1081 return exception
1081 return exception
1082
1082
1083 def format_exception_as_a_whole(self, etype, evalue, etb, number_of_lines_of_context, tb_offset):
1083 def format_exception_as_a_whole(self, etype, evalue, etb, number_of_lines_of_context, tb_offset):
1084 """Formats the header, traceback and exception message for a single exception.
1084 """Formats the header, traceback and exception message for a single exception.
1085
1085
1086 This may be called multiple times by Python 3 exception chaining
1086 This may be called multiple times by Python 3 exception chaining
1087 (PEP 3134).
1087 (PEP 3134).
1088 """
1088 """
1089 # some locals
1089 # some locals
1090 orig_etype = etype
1090 orig_etype = etype
1091 try:
1091 try:
1092 etype = etype.__name__
1092 etype = etype.__name__
1093 except AttributeError:
1093 except AttributeError:
1094 pass
1094 pass
1095
1095
1096 tb_offset = self.tb_offset if tb_offset is None else tb_offset
1096 tb_offset = self.tb_offset if tb_offset is None else tb_offset
1097 head = self.prepare_header(etype, self.long_header)
1097 head = self.prepare_header(etype, self.long_header)
1098 records = self.get_records(etb, number_of_lines_of_context, tb_offset)
1098 records = self.get_records(etb, number_of_lines_of_context, tb_offset)
1099
1099
1100 if records is None:
1100 if records is None:
1101 return ""
1101 return ""
1102
1102
1103 last_unique, recursion_repeat = find_recursion(orig_etype, evalue, records)
1103 last_unique, recursion_repeat = find_recursion(orig_etype, evalue, records)
1104
1104
1105 frames = self.format_records(records, last_unique, recursion_repeat)
1105 frames = self.format_records(records, last_unique, recursion_repeat)
1106
1106
1107 formatted_exception = self.format_exception(etype, evalue)
1107 formatted_exception = self.format_exception(etype, evalue)
1108 if records:
1108 if records:
1109 filepath, lnum = records[-1][1:3]
1109 filepath, lnum = records[-1][1:3]
1110 filepath = os.path.abspath(filepath)
1110 filepath = os.path.abspath(filepath)
1111 ipinst = get_ipython()
1111 ipinst = get_ipython()
1112 if ipinst is not None:
1112 if ipinst is not None:
1113 ipinst.hooks.synchronize_with_editor(filepath, lnum, 0)
1113 ipinst.hooks.synchronize_with_editor(filepath, lnum, 0)
1114
1114
1115 return [[head] + frames + [''.join(formatted_exception[0])]]
1115 return [[head] + frames + [''.join(formatted_exception[0])]]
1116
1116
1117 def get_records(self, etb, number_of_lines_of_context, tb_offset):
1117 def get_records(self, etb, number_of_lines_of_context, tb_offset):
1118 try:
1118 try:
1119 # Try the default getinnerframes and Alex's: Alex's fixes some
1119 # Try the default getinnerframes and Alex's: Alex's fixes some
1120 # problems, but it generates empty tracebacks for console errors
1120 # problems, but it generates empty tracebacks for console errors
1121 # (5 blanks lines) where none should be returned.
1121 # (5 blanks lines) where none should be returned.
1122 return _fixed_getinnerframes(etb, number_of_lines_of_context, tb_offset)
1122 return _fixed_getinnerframes(etb, number_of_lines_of_context, tb_offset)
1123 except UnicodeDecodeError:
1123 except UnicodeDecodeError:
1124 # This can occur if a file's encoding magic comment is wrong.
1124 # This can occur if a file's encoding magic comment is wrong.
1125 # I can't see a way to recover without duplicating a bunch of code
1125 # I can't see a way to recover without duplicating a bunch of code
1126 # from the stdlib traceback module. --TK
1126 # from the stdlib traceback module. --TK
1127 error('\nUnicodeDecodeError while processing traceback.\n')
1127 error('\nUnicodeDecodeError while processing traceback.\n')
1128 return None
1128 return None
1129 except:
1129 except:
1130 # FIXME: I've been getting many crash reports from python 2.3
1130 # FIXME: I've been getting many crash reports from python 2.3
1131 # users, traceable to inspect.py. If I can find a small test-case
1131 # users, traceable to inspect.py. If I can find a small test-case
1132 # to reproduce this, I should either write a better workaround or
1132 # to reproduce this, I should either write a better workaround or
1133 # file a bug report against inspect (if that's the real problem).
1133 # file a bug report against inspect (if that's the real problem).
1134 # So far, I haven't been able to find an isolated example to
1134 # So far, I haven't been able to find an isolated example to
1135 # reproduce the problem.
1135 # reproduce the problem.
1136 inspect_error()
1136 inspect_error()
1137 traceback.print_exc(file=self.ostream)
1137 traceback.print_exc(file=self.ostream)
1138 info('\nUnfortunately, your original traceback can not be constructed.\n')
1138 info('\nUnfortunately, your original traceback can not be constructed.\n')
1139 return None
1139 return None
1140
1140
1141 def get_parts_of_chained_exception(self, evalue):
1141 def get_parts_of_chained_exception(self, evalue):
1142 def get_chained_exception(exception_value):
1142 def get_chained_exception(exception_value):
1143 cause = getattr(exception_value, '__cause__', None)
1143 cause = getattr(exception_value, '__cause__', None)
1144 if cause:
1144 if cause:
1145 return cause
1145 return cause
1146 if getattr(exception_value, '__suppress_context__', False):
1146 if getattr(exception_value, '__suppress_context__', False):
1147 return None
1147 return None
1148 return getattr(exception_value, '__context__', None)
1148 return getattr(exception_value, '__context__', None)
1149
1149
1150 chained_evalue = get_chained_exception(evalue)
1150 chained_evalue = get_chained_exception(evalue)
1151
1151
1152 if chained_evalue:
1152 if chained_evalue:
1153 return chained_evalue.__class__, chained_evalue, chained_evalue.__traceback__
1153 return chained_evalue.__class__, chained_evalue, chained_evalue.__traceback__
1154
1154
1155 def structured_traceback(self, etype, evalue, etb, tb_offset=None,
1155 def structured_traceback(self, etype, evalue, etb, tb_offset=None,
1156 number_of_lines_of_context=5):
1156 number_of_lines_of_context=5):
1157 """Return a nice text document describing the traceback."""
1157 """Return a nice text document describing the traceback."""
1158
1158
1159 formatted_exception = self.format_exception_as_a_whole(etype, evalue, etb, number_of_lines_of_context,
1159 formatted_exception = self.format_exception_as_a_whole(etype, evalue, etb, number_of_lines_of_context,
1160 tb_offset)
1160 tb_offset)
1161
1161
1162 colors = self.Colors # just a shorthand + quicker name lookup
1162 colors = self.Colors # just a shorthand + quicker name lookup
1163 colorsnormal = colors.Normal # used a lot
1163 colorsnormal = colors.Normal # used a lot
1164 head = '%s%s%s' % (colors.topline, '-' * min(75, get_terminal_size()[0]), colorsnormal)
1164 head = '%s%s%s' % (colors.topline, '-' * min(75, get_terminal_size()[0]), colorsnormal)
1165 structured_traceback_parts = [head]
1165 structured_traceback_parts = [head]
1166 if py3compat.PY3:
1166 if py3compat.PY3:
1167 chained_exceptions_tb_offset = 0
1167 chained_exceptions_tb_offset = 0
1168 lines_of_context = 3
1168 lines_of_context = 3
1169 formatted_exceptions = formatted_exception
1169 formatted_exceptions = formatted_exception
1170 exception = self.get_parts_of_chained_exception(evalue)
1170 exception = self.get_parts_of_chained_exception(evalue)
1171 if exception:
1171 if exception:
1172 formatted_exceptions += self.prepare_chained_exception_message(evalue.__cause__)
1172 formatted_exceptions += self.prepare_chained_exception_message(evalue.__cause__)
1173 etype, evalue, etb = exception
1173 etype, evalue, etb = exception
1174 else:
1174 else:
1175 evalue = None
1175 evalue = None
1176 chained_exc_ids = set()
1176 chained_exc_ids = set()
1177 while evalue:
1177 while evalue:
1178 formatted_exceptions += self.format_exception_as_a_whole(etype, evalue, etb, lines_of_context,
1178 formatted_exceptions += self.format_exception_as_a_whole(etype, evalue, etb, lines_of_context,
1179 chained_exceptions_tb_offset)
1179 chained_exceptions_tb_offset)
1180 exception = self.get_parts_of_chained_exception(evalue)
1180 exception = self.get_parts_of_chained_exception(evalue)
1181
1181
1182 if exception and not id(exception[1]) in chained_exc_ids:
1182 if exception and not id(exception[1]) in chained_exc_ids:
1183 chained_exc_ids.add(id(exception[1])) # trace exception to avoid infinite 'cause' loop
1183 chained_exc_ids.add(id(exception[1])) # trace exception to avoid infinite 'cause' loop
1184 formatted_exceptions += self.prepare_chained_exception_message(evalue.__cause__)
1184 formatted_exceptions += self.prepare_chained_exception_message(evalue.__cause__)
1185 etype, evalue, etb = exception
1185 etype, evalue, etb = exception
1186 else:
1186 else:
1187 evalue = None
1187 evalue = None
1188
1188
1189 # we want to see exceptions in a reversed order:
1189 # we want to see exceptions in a reversed order:
1190 # the first exception should be on top
1190 # the first exception should be on top
1191 for formatted_exception in reversed(formatted_exceptions):
1191 for formatted_exception in reversed(formatted_exceptions):
1192 structured_traceback_parts += formatted_exception
1192 structured_traceback_parts += formatted_exception
1193 else:
1193 else:
1194 structured_traceback_parts += formatted_exception[0]
1194 structured_traceback_parts += formatted_exception[0]
1195
1195
1196 return structured_traceback_parts
1196 return structured_traceback_parts
1197
1197
1198 def debugger(self, force=False):
1198 def debugger(self, force=False):
1199 """Call up the pdb debugger if desired, always clean up the tb
1199 """Call up the pdb debugger if desired, always clean up the tb
1200 reference.
1200 reference.
1201
1201
1202 Keywords:
1202 Keywords:
1203
1203
1204 - force(False): by default, this routine checks the instance call_pdb
1204 - force(False): by default, this routine checks the instance call_pdb
1205 flag and does not actually invoke the debugger if the flag is false.
1205 flag and does not actually invoke the debugger if the flag is false.
1206 The 'force' option forces the debugger to activate even if the flag
1206 The 'force' option forces the debugger to activate even if the flag
1207 is false.
1207 is false.
1208
1208
1209 If the call_pdb flag is set, the pdb interactive debugger is
1209 If the call_pdb flag is set, the pdb interactive debugger is
1210 invoked. In all cases, the self.tb reference to the current traceback
1210 invoked. In all cases, the self.tb reference to the current traceback
1211 is deleted to prevent lingering references which hamper memory
1211 is deleted to prevent lingering references which hamper memory
1212 management.
1212 management.
1213
1213
1214 Note that each call to pdb() does an 'import readline', so if your app
1214 Note that each call to pdb() does an 'import readline', so if your app
1215 requires a special setup for the readline completers, you'll have to
1215 requires a special setup for the readline completers, you'll have to
1216 fix that by hand after invoking the exception handler."""
1216 fix that by hand after invoking the exception handler."""
1217
1217
1218 if force or self.call_pdb:
1218 if force or self.call_pdb:
1219 if self.pdb is None:
1219 if self.pdb is None:
1220 self.pdb = self.debugger_cls()
1220 self.pdb = self.debugger_cls()
1221 # the system displayhook may have changed, restore the original
1221 # the system displayhook may have changed, restore the original
1222 # for pdb
1222 # for pdb
1223 display_trap = DisplayTrap(hook=sys.__displayhook__)
1223 display_trap = DisplayTrap(hook=sys.__displayhook__)
1224 with display_trap:
1224 with display_trap:
1225 self.pdb.reset()
1225 self.pdb.reset()
1226 # Find the right frame so we don't pop up inside ipython itself
1226 # Find the right frame so we don't pop up inside ipython itself
1227 if hasattr(self, 'tb') and self.tb is not None:
1227 if hasattr(self, 'tb') and self.tb is not None:
1228 etb = self.tb
1228 etb = self.tb
1229 else:
1229 else:
1230 etb = self.tb = sys.last_traceback
1230 etb = self.tb = sys.last_traceback
1231 while self.tb is not None and self.tb.tb_next is not None:
1231 while self.tb is not None and self.tb.tb_next is not None:
1232 self.tb = self.tb.tb_next
1232 self.tb = self.tb.tb_next
1233 if etb and etb.tb_next:
1233 if etb and etb.tb_next:
1234 etb = etb.tb_next
1234 etb = etb.tb_next
1235 self.pdb.botframe = etb.tb_frame
1235 self.pdb.botframe = etb.tb_frame
1236 self.pdb.interaction(self.tb.tb_frame, self.tb)
1236 self.pdb.interaction(self.tb.tb_frame, self.tb)
1237
1237
1238 if hasattr(self, 'tb'):
1238 if hasattr(self, 'tb'):
1239 del self.tb
1239 del self.tb
1240
1240
1241 def handler(self, info=None):
1241 def handler(self, info=None):
1242 (etype, evalue, etb) = info or sys.exc_info()
1242 (etype, evalue, etb) = info or sys.exc_info()
1243 self.tb = etb
1243 self.tb = etb
1244 ostream = self.ostream
1244 ostream = self.ostream
1245 ostream.flush()
1245 ostream.flush()
1246 ostream.write(self.text(etype, evalue, etb))
1246 ostream.write(self.text(etype, evalue, etb))
1247 ostream.write('\n')
1247 ostream.write('\n')
1248 ostream.flush()
1248 ostream.flush()
1249
1249
1250 # Changed so an instance can just be called as VerboseTB_inst() and print
1250 # Changed so an instance can just be called as VerboseTB_inst() and print
1251 # out the right info on its own.
1251 # out the right info on its own.
1252 def __call__(self, etype=None, evalue=None, etb=None):
1252 def __call__(self, etype=None, evalue=None, etb=None):
1253 """This hook can replace sys.excepthook (for Python 2.1 or higher)."""
1253 """This hook can replace sys.excepthook (for Python 2.1 or higher)."""
1254 if etb is None:
1254 if etb is None:
1255 self.handler()
1255 self.handler()
1256 else:
1256 else:
1257 self.handler((etype, evalue, etb))
1257 self.handler((etype, evalue, etb))
1258 try:
1258 try:
1259 self.debugger()
1259 self.debugger()
1260 except KeyboardInterrupt:
1260 except KeyboardInterrupt:
1261 print("\nKeyboardInterrupt")
1261 print("\nKeyboardInterrupt")
1262
1262
1263
1263
1264 #----------------------------------------------------------------------------
1264 #----------------------------------------------------------------------------
1265 class FormattedTB(VerboseTB, ListTB):
1265 class FormattedTB(VerboseTB, ListTB):
1266 """Subclass ListTB but allow calling with a traceback.
1266 """Subclass ListTB but allow calling with a traceback.
1267
1267
1268 It can thus be used as a sys.excepthook for Python > 2.1.
1268 It can thus be used as a sys.excepthook for Python > 2.1.
1269
1269
1270 Also adds 'Context' and 'Verbose' modes, not available in ListTB.
1270 Also adds 'Context' and 'Verbose' modes, not available in ListTB.
1271
1271
1272 Allows a tb_offset to be specified. This is useful for situations where
1272 Allows a tb_offset to be specified. This is useful for situations where
1273 one needs to remove a number of topmost frames from the traceback (such as
1273 one needs to remove a number of topmost frames from the traceback (such as
1274 occurs with python programs that themselves execute other python code,
1274 occurs with python programs that themselves execute other python code,
1275 like Python shells). """
1275 like Python shells). """
1276
1276
1277 def __init__(self, mode='Plain', color_scheme='Linux', call_pdb=False,
1277 def __init__(self, mode='Plain', color_scheme='Linux', call_pdb=False,
1278 ostream=None,
1278 ostream=None,
1279 tb_offset=0, long_header=False, include_vars=False,
1279 tb_offset=0, long_header=False, include_vars=False,
1280 check_cache=None, debugger_cls=None):
1280 check_cache=None, debugger_cls=None,
1281 parent=None, config=None):
1281
1282
1282 # NEVER change the order of this list. Put new modes at the end:
1283 # NEVER change the order of this list. Put new modes at the end:
1283 self.valid_modes = ['Plain', 'Context', 'Verbose']
1284 self.valid_modes = ['Plain', 'Context', 'Verbose']
1284 self.verbose_modes = self.valid_modes[1:3]
1285 self.verbose_modes = self.valid_modes[1:3]
1285
1286
1286 VerboseTB.__init__(self, color_scheme=color_scheme, call_pdb=call_pdb,
1287 VerboseTB.__init__(self, color_scheme=color_scheme, call_pdb=call_pdb,
1287 ostream=ostream, tb_offset=tb_offset,
1288 ostream=ostream, tb_offset=tb_offset,
1288 long_header=long_header, include_vars=include_vars,
1289 long_header=long_header, include_vars=include_vars,
1289 check_cache=check_cache, debugger_cls=debugger_cls)
1290 check_cache=check_cache, debugger_cls=debugger_cls,
1291 parent=parent, config=config)
1290
1292
1291 # Different types of tracebacks are joined with different separators to
1293 # Different types of tracebacks are joined with different separators to
1292 # form a single string. They are taken from this dict
1294 # form a single string. They are taken from this dict
1293 self._join_chars = dict(Plain='', Context='\n', Verbose='\n')
1295 self._join_chars = dict(Plain='', Context='\n', Verbose='\n')
1294 # set_mode also sets the tb_join_char attribute
1296 # set_mode also sets the tb_join_char attribute
1295 self.set_mode(mode)
1297 self.set_mode(mode)
1296
1298
1297 def _extract_tb(self, tb):
1299 def _extract_tb(self, tb):
1298 if tb:
1300 if tb:
1299 return traceback.extract_tb(tb)
1301 return traceback.extract_tb(tb)
1300 else:
1302 else:
1301 return None
1303 return None
1302
1304
1303 def structured_traceback(self, etype, value, tb, tb_offset=None, number_of_lines_of_context=5):
1305 def structured_traceback(self, etype, value, tb, tb_offset=None, number_of_lines_of_context=5):
1304 tb_offset = self.tb_offset if tb_offset is None else tb_offset
1306 tb_offset = self.tb_offset if tb_offset is None else tb_offset
1305 mode = self.mode
1307 mode = self.mode
1306 if mode in self.verbose_modes:
1308 if mode in self.verbose_modes:
1307 # Verbose modes need a full traceback
1309 # Verbose modes need a full traceback
1308 return VerboseTB.structured_traceback(
1310 return VerboseTB.structured_traceback(
1309 self, etype, value, tb, tb_offset, number_of_lines_of_context
1311 self, etype, value, tb, tb_offset, number_of_lines_of_context
1310 )
1312 )
1311 else:
1313 else:
1312 # We must check the source cache because otherwise we can print
1314 # We must check the source cache because otherwise we can print
1313 # out-of-date source code.
1315 # out-of-date source code.
1314 self.check_cache()
1316 self.check_cache()
1315 # Now we can extract and format the exception
1317 # Now we can extract and format the exception
1316 elist = self._extract_tb(tb)
1318 elist = self._extract_tb(tb)
1317 return ListTB.structured_traceback(
1319 return ListTB.structured_traceback(
1318 self, etype, value, elist, tb_offset, number_of_lines_of_context
1320 self, etype, value, elist, tb_offset, number_of_lines_of_context
1319 )
1321 )
1320
1322
1321 def stb2text(self, stb):
1323 def stb2text(self, stb):
1322 """Convert a structured traceback (a list) to a string."""
1324 """Convert a structured traceback (a list) to a string."""
1323 return self.tb_join_char.join(stb)
1325 return self.tb_join_char.join(stb)
1324
1326
1325
1327
1326 def set_mode(self, mode=None):
1328 def set_mode(self, mode=None):
1327 """Switch to the desired mode.
1329 """Switch to the desired mode.
1328
1330
1329 If mode is not specified, cycles through the available modes."""
1331 If mode is not specified, cycles through the available modes."""
1330
1332
1331 if not mode:
1333 if not mode:
1332 new_idx = (self.valid_modes.index(self.mode) + 1 ) % \
1334 new_idx = (self.valid_modes.index(self.mode) + 1 ) % \
1333 len(self.valid_modes)
1335 len(self.valid_modes)
1334 self.mode = self.valid_modes[new_idx]
1336 self.mode = self.valid_modes[new_idx]
1335 elif mode not in self.valid_modes:
1337 elif mode not in self.valid_modes:
1336 raise ValueError('Unrecognized mode in FormattedTB: <' + mode + '>\n'
1338 raise ValueError('Unrecognized mode in FormattedTB: <' + mode + '>\n'
1337 'Valid modes: ' + str(self.valid_modes))
1339 'Valid modes: ' + str(self.valid_modes))
1338 else:
1340 else:
1339 self.mode = mode
1341 self.mode = mode
1340 # include variable details only in 'Verbose' mode
1342 # include variable details only in 'Verbose' mode
1341 self.include_vars = (self.mode == self.valid_modes[2])
1343 self.include_vars = (self.mode == self.valid_modes[2])
1342 # Set the join character for generating text tracebacks
1344 # Set the join character for generating text tracebacks
1343 self.tb_join_char = self._join_chars[self.mode]
1345 self.tb_join_char = self._join_chars[self.mode]
1344
1346
1345 # some convenient shortcuts
1347 # some convenient shortcuts
1346 def plain(self):
1348 def plain(self):
1347 self.set_mode(self.valid_modes[0])
1349 self.set_mode(self.valid_modes[0])
1348
1350
1349 def context(self):
1351 def context(self):
1350 self.set_mode(self.valid_modes[1])
1352 self.set_mode(self.valid_modes[1])
1351
1353
1352 def verbose(self):
1354 def verbose(self):
1353 self.set_mode(self.valid_modes[2])
1355 self.set_mode(self.valid_modes[2])
1354
1356
1355
1357
1356 #----------------------------------------------------------------------------
1358 #----------------------------------------------------------------------------
1357 class AutoFormattedTB(FormattedTB):
1359 class AutoFormattedTB(FormattedTB):
1358 """A traceback printer which can be called on the fly.
1360 """A traceback printer which can be called on the fly.
1359
1361
1360 It will find out about exceptions by itself.
1362 It will find out about exceptions by itself.
1361
1363
1362 A brief example::
1364 A brief example::
1363
1365
1364 AutoTB = AutoFormattedTB(mode = 'Verbose',color_scheme='Linux')
1366 AutoTB = AutoFormattedTB(mode = 'Verbose',color_scheme='Linux')
1365 try:
1367 try:
1366 ...
1368 ...
1367 except:
1369 except:
1368 AutoTB() # or AutoTB(out=logfile) where logfile is an open file object
1370 AutoTB() # or AutoTB(out=logfile) where logfile is an open file object
1369 """
1371 """
1370
1372
1371 def __call__(self, etype=None, evalue=None, etb=None,
1373 def __call__(self, etype=None, evalue=None, etb=None,
1372 out=None, tb_offset=None):
1374 out=None, tb_offset=None):
1373 """Print out a formatted exception traceback.
1375 """Print out a formatted exception traceback.
1374
1376
1375 Optional arguments:
1377 Optional arguments:
1376 - out: an open file-like object to direct output to.
1378 - out: an open file-like object to direct output to.
1377
1379
1378 - tb_offset: the number of frames to skip over in the stack, on a
1380 - tb_offset: the number of frames to skip over in the stack, on a
1379 per-call basis (this overrides temporarily the instance's tb_offset
1381 per-call basis (this overrides temporarily the instance's tb_offset
1380 given at initialization time. """
1382 given at initialization time. """
1381
1383
1382 if out is None:
1384 if out is None:
1383 out = self.ostream
1385 out = self.ostream
1384 out.flush()
1386 out.flush()
1385 out.write(self.text(etype, evalue, etb, tb_offset))
1387 out.write(self.text(etype, evalue, etb, tb_offset))
1386 out.write('\n')
1388 out.write('\n')
1387 out.flush()
1389 out.flush()
1388 # FIXME: we should remove the auto pdb behavior from here and leave
1390 # FIXME: we should remove the auto pdb behavior from here and leave
1389 # that to the clients.
1391 # that to the clients.
1390 try:
1392 try:
1391 self.debugger()
1393 self.debugger()
1392 except KeyboardInterrupt:
1394 except KeyboardInterrupt:
1393 print("\nKeyboardInterrupt")
1395 print("\nKeyboardInterrupt")
1394
1396
1395 def structured_traceback(self, etype=None, value=None, tb=None,
1397 def structured_traceback(self, etype=None, value=None, tb=None,
1396 tb_offset=None, number_of_lines_of_context=5):
1398 tb_offset=None, number_of_lines_of_context=5):
1397 if etype is None:
1399 if etype is None:
1398 etype, value, tb = sys.exc_info()
1400 etype, value, tb = sys.exc_info()
1399 self.tb = tb
1401 self.tb = tb
1400 return FormattedTB.structured_traceback(
1402 return FormattedTB.structured_traceback(
1401 self, etype, value, tb, tb_offset, number_of_lines_of_context)
1403 self, etype, value, tb, tb_offset, number_of_lines_of_context)
1402
1404
1403
1405
1404 #---------------------------------------------------------------------------
1406 #---------------------------------------------------------------------------
1405
1407
1406 # A simple class to preserve Nathan's original functionality.
1408 # A simple class to preserve Nathan's original functionality.
1407 class ColorTB(FormattedTB):
1409 class ColorTB(FormattedTB):
1408 """Shorthand to initialize a FormattedTB in Linux colors mode."""
1410 """Shorthand to initialize a FormattedTB in Linux colors mode."""
1409
1411
1410 def __init__(self, color_scheme='Linux', call_pdb=0, **kwargs):
1412 def __init__(self, color_scheme='Linux', call_pdb=0, **kwargs):
1411 FormattedTB.__init__(self, color_scheme=color_scheme,
1413 FormattedTB.__init__(self, color_scheme=color_scheme,
1412 call_pdb=call_pdb, **kwargs)
1414 call_pdb=call_pdb, **kwargs)
1413
1415
1414
1416
1415 class SyntaxTB(ListTB):
1417 class SyntaxTB(ListTB):
1416 """Extension which holds some state: the last exception value"""
1418 """Extension which holds some state: the last exception value"""
1417
1419
1418 def __init__(self, color_scheme='NoColor'):
1420 def __init__(self, color_scheme='NoColor', parent=None, config=None):
1419 ListTB.__init__(self, color_scheme)
1421 ListTB.__init__(self, color_scheme, parent=parent, config=config)
1420 self.last_syntax_error = None
1422 self.last_syntax_error = None
1421
1423
1422 def __call__(self, etype, value, elist):
1424 def __call__(self, etype, value, elist):
1423 self.last_syntax_error = value
1425 self.last_syntax_error = value
1424
1426
1425 ListTB.__call__(self, etype, value, elist)
1427 ListTB.__call__(self, etype, value, elist)
1426
1428
1427 def structured_traceback(self, etype, value, elist, tb_offset=None,
1429 def structured_traceback(self, etype, value, elist, tb_offset=None,
1428 context=5):
1430 context=5):
1429 # If the source file has been edited, the line in the syntax error can
1431 # If the source file has been edited, the line in the syntax error can
1430 # be wrong (retrieved from an outdated cache). This replaces it with
1432 # be wrong (retrieved from an outdated cache). This replaces it with
1431 # the current value.
1433 # the current value.
1432 if isinstance(value, SyntaxError) \
1434 if isinstance(value, SyntaxError) \
1433 and isinstance(value.filename, py3compat.string_types) \
1435 and isinstance(value.filename, py3compat.string_types) \
1434 and isinstance(value.lineno, int):
1436 and isinstance(value.lineno, int):
1435 linecache.checkcache(value.filename)
1437 linecache.checkcache(value.filename)
1436 newtext = ulinecache.getline(value.filename, value.lineno)
1438 newtext = ulinecache.getline(value.filename, value.lineno)
1437 if newtext:
1439 if newtext:
1438 value.text = newtext
1440 value.text = newtext
1439 self.last_syntax_error = value
1441 self.last_syntax_error = value
1440 return super(SyntaxTB, self).structured_traceback(etype, value, elist,
1442 return super(SyntaxTB, self).structured_traceback(etype, value, elist,
1441 tb_offset=tb_offset, context=context)
1443 tb_offset=tb_offset, context=context)
1442
1444
1443 def clear_err_state(self):
1445 def clear_err_state(self):
1444 """Return the current error state and clear it"""
1446 """Return the current error state and clear it"""
1445 e = self.last_syntax_error
1447 e = self.last_syntax_error
1446 self.last_syntax_error = None
1448 self.last_syntax_error = None
1447 return e
1449 return e
1448
1450
1449 def stb2text(self, stb):
1451 def stb2text(self, stb):
1450 """Convert a structured traceback (a list) to a string."""
1452 """Convert a structured traceback (a list) to a string."""
1451 return ''.join(stb)
1453 return ''.join(stb)
1452
1454
1453
1455
1454 # some internal-use functions
1456 # some internal-use functions
1455 def text_repr(value):
1457 def text_repr(value):
1456 """Hopefully pretty robust repr equivalent."""
1458 """Hopefully pretty robust repr equivalent."""
1457 # this is pretty horrible but should always return *something*
1459 # this is pretty horrible but should always return *something*
1458 try:
1460 try:
1459 return pydoc.text.repr(value)
1461 return pydoc.text.repr(value)
1460 except KeyboardInterrupt:
1462 except KeyboardInterrupt:
1461 raise
1463 raise
1462 except:
1464 except:
1463 try:
1465 try:
1464 return repr(value)
1466 return repr(value)
1465 except KeyboardInterrupt:
1467 except KeyboardInterrupt:
1466 raise
1468 raise
1467 except:
1469 except:
1468 try:
1470 try:
1469 # all still in an except block so we catch
1471 # all still in an except block so we catch
1470 # getattr raising
1472 # getattr raising
1471 name = getattr(value, '__name__', None)
1473 name = getattr(value, '__name__', None)
1472 if name:
1474 if name:
1473 # ick, recursion
1475 # ick, recursion
1474 return text_repr(name)
1476 return text_repr(name)
1475 klass = getattr(value, '__class__', None)
1477 klass = getattr(value, '__class__', None)
1476 if klass:
1478 if klass:
1477 return '%s instance' % text_repr(klass)
1479 return '%s instance' % text_repr(klass)
1478 except KeyboardInterrupt:
1480 except KeyboardInterrupt:
1479 raise
1481 raise
1480 except:
1482 except:
1481 return 'UNRECOVERABLE REPR FAILURE'
1483 return 'UNRECOVERABLE REPR FAILURE'
1482
1484
1483
1485
1484 def eqrepr(value, repr=text_repr):
1486 def eqrepr(value, repr=text_repr):
1485 return '=%s' % repr(value)
1487 return '=%s' % repr(value)
1486
1488
1487
1489
1488 def nullrepr(value, repr=text_repr):
1490 def nullrepr(value, repr=text_repr):
1489 return ''
1491 return ''
@@ -1,332 +1,327 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """
2 """
3 Class and program to colorize python source code for ANSI terminals.
3 Class and program to colorize python source code for ANSI terminals.
4
4
5 Based on an HTML code highlighter by Jurgen Hermann found at:
5 Based on an HTML code highlighter by Jurgen Hermann found at:
6 http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52298
6 http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52298
7
7
8 Modifications by Fernando Perez (fperez@colorado.edu).
8 Modifications by Fernando Perez (fperez@colorado.edu).
9
9
10 Information on the original HTML highlighter follows:
10 Information on the original HTML highlighter follows:
11
11
12 MoinMoin - Python Source Parser
12 MoinMoin - Python Source Parser
13
13
14 Title: Colorize Python source using the built-in tokenizer
14 Title: Colorize Python source using the built-in tokenizer
15
15
16 Submitter: Jurgen Hermann
16 Submitter: Jurgen Hermann
17 Last Updated:2001/04/06
17 Last Updated:2001/04/06
18
18
19 Version no:1.2
19 Version no:1.2
20
20
21 Description:
21 Description:
22
22
23 This code is part of MoinMoin (http://moin.sourceforge.net/) and converts
23 This code is part of MoinMoin (http://moin.sourceforge.net/) and converts
24 Python source code to HTML markup, rendering comments, keywords,
24 Python source code to HTML markup, rendering comments, keywords,
25 operators, numeric and string literals in different colors.
25 operators, numeric and string literals in different colors.
26
26
27 It shows how to use the built-in keyword, token and tokenize modules to
27 It shows how to use the built-in keyword, token and tokenize modules to
28 scan Python source code and re-emit it with no changes to its original
28 scan Python source code and re-emit it with no changes to its original
29 formatting (which is the hard part).
29 formatting (which is the hard part).
30 """
30 """
31 from __future__ import print_function
31 from __future__ import print_function
32 from __future__ import absolute_import
32 from __future__ import absolute_import
33 from __future__ import unicode_literals
33 from __future__ import unicode_literals
34
34
35 __all__ = ['ANSICodeColors','Parser']
35 __all__ = ['ANSICodeColors','Parser']
36
36
37 _scheme_default = 'Linux'
37 _scheme_default = 'Linux'
38
38
39
39
40 # Imports
40 # Imports
41 import keyword
41 import keyword
42 import os
42 import os
43 import sys
43 import sys
44 import token
44 import token
45 import tokenize
45 import tokenize
46
46
47 try:
47 generate_tokens = tokenize.generate_tokens
48 generate_tokens = tokenize.generate_tokens
49 except AttributeError:
50 # Python 3. Note that we use the undocumented _tokenize because it expects
51 # strings, not bytes. See also Python issue #9969.
52 generate_tokens = tokenize._tokenize
53
48
54 from IPython.utils.coloransi import TermColors, InputTermColors ,ColorScheme, ColorSchemeTable
49 from IPython.utils.coloransi import TermColors, InputTermColors ,ColorScheme, ColorSchemeTable
55 from IPython.utils.py3compat import PY3
50 from IPython.utils.py3compat import PY3
56
51
57 from .colorable import Colorable
52 from .colorable import Colorable
58
53
59 if PY3:
54 if PY3:
60 from io import StringIO
55 from io import StringIO
61 else:
56 else:
62 from StringIO import StringIO
57 from StringIO import StringIO
63
58
64 #############################################################################
59 #############################################################################
65 ### Python Source Parser (does Hilighting)
60 ### Python Source Parser (does Hilighting)
66 #############################################################################
61 #############################################################################
67
62
68 _KEYWORD = token.NT_OFFSET + 1
63 _KEYWORD = token.NT_OFFSET + 1
69 _TEXT = token.NT_OFFSET + 2
64 _TEXT = token.NT_OFFSET + 2
70
65
71 #****************************************************************************
66 #****************************************************************************
72 # Builtin color schemes
67 # Builtin color schemes
73
68
74 Colors = TermColors # just a shorthand
69 Colors = TermColors # just a shorthand
75
70
76 # Build a few color schemes
71 # Build a few color schemes
77 NoColor = ColorScheme(
72 NoColor = ColorScheme(
78 'NoColor',{
73 'NoColor',{
79 'header' : Colors.NoColor,
74 'header' : Colors.NoColor,
80 token.NUMBER : Colors.NoColor,
75 token.NUMBER : Colors.NoColor,
81 token.OP : Colors.NoColor,
76 token.OP : Colors.NoColor,
82 token.STRING : Colors.NoColor,
77 token.STRING : Colors.NoColor,
83 tokenize.COMMENT : Colors.NoColor,
78 tokenize.COMMENT : Colors.NoColor,
84 token.NAME : Colors.NoColor,
79 token.NAME : Colors.NoColor,
85 token.ERRORTOKEN : Colors.NoColor,
80 token.ERRORTOKEN : Colors.NoColor,
86
81
87 _KEYWORD : Colors.NoColor,
82 _KEYWORD : Colors.NoColor,
88 _TEXT : Colors.NoColor,
83 _TEXT : Colors.NoColor,
89
84
90 'in_prompt' : InputTermColors.NoColor, # Input prompt
85 'in_prompt' : InputTermColors.NoColor, # Input prompt
91 'in_number' : InputTermColors.NoColor, # Input prompt number
86 'in_number' : InputTermColors.NoColor, # Input prompt number
92 'in_prompt2' : InputTermColors.NoColor, # Continuation prompt
87 'in_prompt2' : InputTermColors.NoColor, # Continuation prompt
93 'in_normal' : InputTermColors.NoColor, # color off (usu. Colors.Normal)
88 'in_normal' : InputTermColors.NoColor, # color off (usu. Colors.Normal)
94
89
95 'out_prompt' : Colors.NoColor, # Output prompt
90 'out_prompt' : Colors.NoColor, # Output prompt
96 'out_number' : Colors.NoColor, # Output prompt number
91 'out_number' : Colors.NoColor, # Output prompt number
97
92
98 'normal' : Colors.NoColor # color off (usu. Colors.Normal)
93 'normal' : Colors.NoColor # color off (usu. Colors.Normal)
99 } )
94 } )
100
95
101 LinuxColors = ColorScheme(
96 LinuxColors = ColorScheme(
102 'Linux',{
97 'Linux',{
103 'header' : Colors.LightRed,
98 'header' : Colors.LightRed,
104 token.NUMBER : Colors.LightCyan,
99 token.NUMBER : Colors.LightCyan,
105 token.OP : Colors.Yellow,
100 token.OP : Colors.Yellow,
106 token.STRING : Colors.LightBlue,
101 token.STRING : Colors.LightBlue,
107 tokenize.COMMENT : Colors.LightRed,
102 tokenize.COMMENT : Colors.LightRed,
108 token.NAME : Colors.Normal,
103 token.NAME : Colors.Normal,
109 token.ERRORTOKEN : Colors.Red,
104 token.ERRORTOKEN : Colors.Red,
110
105
111 _KEYWORD : Colors.LightGreen,
106 _KEYWORD : Colors.LightGreen,
112 _TEXT : Colors.Yellow,
107 _TEXT : Colors.Yellow,
113
108
114 'in_prompt' : InputTermColors.Green,
109 'in_prompt' : InputTermColors.Green,
115 'in_number' : InputTermColors.LightGreen,
110 'in_number' : InputTermColors.LightGreen,
116 'in_prompt2' : InputTermColors.Green,
111 'in_prompt2' : InputTermColors.Green,
117 'in_normal' : InputTermColors.Normal, # color off (usu. Colors.Normal)
112 'in_normal' : InputTermColors.Normal, # color off (usu. Colors.Normal)
118
113
119 'out_prompt' : Colors.Red,
114 'out_prompt' : Colors.Red,
120 'out_number' : Colors.LightRed,
115 'out_number' : Colors.LightRed,
121
116
122 'normal' : Colors.Normal # color off (usu. Colors.Normal)
117 'normal' : Colors.Normal # color off (usu. Colors.Normal)
123 } )
118 } )
124
119
125 NeutralColors = ColorScheme(
120 NeutralColors = ColorScheme(
126 'Neutral',{
121 'Neutral',{
127 'header' : Colors.Red,
122 'header' : Colors.Red,
128 token.NUMBER : Colors.Cyan,
123 token.NUMBER : Colors.Cyan,
129 token.OP : Colors.Blue,
124 token.OP : Colors.Blue,
130 token.STRING : Colors.Blue,
125 token.STRING : Colors.Blue,
131 tokenize.COMMENT : Colors.Red,
126 tokenize.COMMENT : Colors.Red,
132 token.NAME : Colors.Normal,
127 token.NAME : Colors.Normal,
133 token.ERRORTOKEN : Colors.Red,
128 token.ERRORTOKEN : Colors.Red,
134
129
135 _KEYWORD : Colors.Green,
130 _KEYWORD : Colors.Green,
136 _TEXT : Colors.Blue,
131 _TEXT : Colors.Blue,
137
132
138 'in_prompt' : InputTermColors.Blue,
133 'in_prompt' : InputTermColors.Blue,
139 'in_number' : InputTermColors.LightBlue,
134 'in_number' : InputTermColors.LightBlue,
140 'in_prompt2' : InputTermColors.Blue,
135 'in_prompt2' : InputTermColors.Blue,
141 'in_normal' : InputTermColors.Normal, # color off (usu. Colors.Normal)
136 'in_normal' : InputTermColors.Normal, # color off (usu. Colors.Normal)
142
137
143 'out_prompt' : Colors.Red,
138 'out_prompt' : Colors.Red,
144 'out_number' : Colors.LightRed,
139 'out_number' : Colors.LightRed,
145
140
146 'normal' : Colors.Normal # color off (usu. Colors.Normal)
141 'normal' : Colors.Normal # color off (usu. Colors.Normal)
147 } )
142 } )
148
143
149 # Hack: the 'neutral' colours are not very visible on a dark background on
144 # Hack: the 'neutral' colours are not very visible on a dark background on
150 # Windows. Since Windows command prompts have a dark background by default, and
145 # Windows. Since Windows command prompts have a dark background by default, and
151 # relatively few users are likely to alter that, we will use the 'Linux' colours,
146 # relatively few users are likely to alter that, we will use the 'Linux' colours,
152 # designed for a dark background, as the default on Windows. Changing it here
147 # designed for a dark background, as the default on Windows. Changing it here
153 # avoids affecting the prompt colours rendered by prompt_toolkit, where the
148 # avoids affecting the prompt colours rendered by prompt_toolkit, where the
154 # neutral defaults do work OK.
149 # neutral defaults do work OK.
155
150
156 if os.name == 'nt':
151 if os.name == 'nt':
157 NeutralColors = LinuxColors.copy(name='Neutral')
152 NeutralColors = LinuxColors.copy(name='Neutral')
158
153
159 LightBGColors = ColorScheme(
154 LightBGColors = ColorScheme(
160 'LightBG',{
155 'LightBG',{
161 'header' : Colors.Red,
156 'header' : Colors.Red,
162 token.NUMBER : Colors.Cyan,
157 token.NUMBER : Colors.Cyan,
163 token.OP : Colors.Blue,
158 token.OP : Colors.Blue,
164 token.STRING : Colors.Blue,
159 token.STRING : Colors.Blue,
165 tokenize.COMMENT : Colors.Red,
160 tokenize.COMMENT : Colors.Red,
166 token.NAME : Colors.Normal,
161 token.NAME : Colors.Normal,
167 token.ERRORTOKEN : Colors.Red,
162 token.ERRORTOKEN : Colors.Red,
168
163
169
164
170 _KEYWORD : Colors.Green,
165 _KEYWORD : Colors.Green,
171 _TEXT : Colors.Blue,
166 _TEXT : Colors.Blue,
172
167
173 'in_prompt' : InputTermColors.Blue,
168 'in_prompt' : InputTermColors.Blue,
174 'in_number' : InputTermColors.LightBlue,
169 'in_number' : InputTermColors.LightBlue,
175 'in_prompt2' : InputTermColors.Blue,
170 'in_prompt2' : InputTermColors.Blue,
176 'in_normal' : InputTermColors.Normal, # color off (usu. Colors.Normal)
171 'in_normal' : InputTermColors.Normal, # color off (usu. Colors.Normal)
177
172
178 'out_prompt' : Colors.Red,
173 'out_prompt' : Colors.Red,
179 'out_number' : Colors.LightRed,
174 'out_number' : Colors.LightRed,
180
175
181 'normal' : Colors.Normal # color off (usu. Colors.Normal)
176 'normal' : Colors.Normal # color off (usu. Colors.Normal)
182 } )
177 } )
183
178
184 # Build table of color schemes (needed by the parser)
179 # Build table of color schemes (needed by the parser)
185 ANSICodeColors = ColorSchemeTable([NoColor,LinuxColors,LightBGColors, NeutralColors],
180 ANSICodeColors = ColorSchemeTable([NoColor,LinuxColors,LightBGColors, NeutralColors],
186 _scheme_default)
181 _scheme_default)
187
182
188 Undefined = object()
183 Undefined = object()
189
184
190 class Parser(Colorable):
185 class Parser(Colorable):
191 """ Format colored Python source.
186 """ Format colored Python source.
192 """
187 """
193
188
194 def __init__(self, color_table=None, out = sys.stdout, parent=None, style=None):
189 def __init__(self, color_table=None, out = sys.stdout, parent=None, style=None):
195 """ Create a parser with a specified color table and output channel.
190 """ Create a parser with a specified color table and output channel.
196
191
197 Call format() to process code.
192 Call format() to process code.
198 """
193 """
199
194
200 super(Parser, self).__init__(parent=parent)
195 super(Parser, self).__init__(parent=parent)
201
196
202 self.color_table = color_table and color_table or ANSICodeColors
197 self.color_table = color_table and color_table or ANSICodeColors
203 self.out = out
198 self.out = out
204 if not style:
199 if not style:
205 self.style = self.default_style
200 self.style = self.default_style
206 else:
201 else:
207 self.style = style
202 self.style = style
208
203
209
204
210 def format(self, raw, out=None, scheme=Undefined):
205 def format(self, raw, out=None, scheme=Undefined):
211 import warnings
206 import warnings
212 if scheme is not Undefined:
207 if scheme is not Undefined:
213 warnings.warn('The `scheme` argument of IPython.utils.PyColorize:Parser.format is deprecated since IPython 6.0.'
208 warnings.warn('The `scheme` argument of IPython.utils.PyColorize:Parser.format is deprecated since IPython 6.0.'
214 'It will have no effect. Set the parser `style` directly.',
209 'It will have no effect. Set the parser `style` directly.',
215 stacklevel=2)
210 stacklevel=2)
216 return self.format2(raw, out)[0]
211 return self.format2(raw, out)[0]
217
212
218 def format2(self, raw, out = None):
213 def format2(self, raw, out = None):
219 """ Parse and send the colored source.
214 """ Parse and send the colored source.
220
215
221 If out and scheme are not specified, the defaults (given to
216 If out and scheme are not specified, the defaults (given to
222 constructor) are used.
217 constructor) are used.
223
218
224 out should be a file-type object. Optionally, out can be given as the
219 out should be a file-type object. Optionally, out can be given as the
225 string 'str' and the parser will automatically return the output in a
220 string 'str' and the parser will automatically return the output in a
226 string."""
221 string."""
227
222
228 string_output = 0
223 string_output = 0
229 if out == 'str' or self.out == 'str' or \
224 if out == 'str' or self.out == 'str' or \
230 isinstance(self.out,StringIO):
225 isinstance(self.out,StringIO):
231 # XXX - I don't really like this state handling logic, but at this
226 # XXX - I don't really like this state handling logic, but at this
232 # point I don't want to make major changes, so adding the
227 # point I don't want to make major changes, so adding the
233 # isinstance() check is the simplest I can do to ensure correct
228 # isinstance() check is the simplest I can do to ensure correct
234 # behavior.
229 # behavior.
235 out_old = self.out
230 out_old = self.out
236 self.out = StringIO()
231 self.out = StringIO()
237 string_output = 1
232 string_output = 1
238 elif out is not None:
233 elif out is not None:
239 self.out = out
234 self.out = out
240
235
241 # Fast return of the unmodified input for NoColor scheme
236 # Fast return of the unmodified input for NoColor scheme
242 if self.style == 'NoColor':
237 if self.style == 'NoColor':
243 error = False
238 error = False
244 self.out.write(raw)
239 self.out.write(raw)
245 if string_output:
240 if string_output:
246 return raw,error
241 return raw,error
247 else:
242 else:
248 return None,error
243 return None,error
249
244
250 # local shorthands
245 # local shorthands
251 colors = self.color_table[self.style].colors
246 colors = self.color_table[self.style].colors
252 self.colors = colors # put in object so __call__ sees it
247 self.colors = colors # put in object so __call__ sees it
253
248
254 # Remove trailing whitespace and normalize tabs
249 # Remove trailing whitespace and normalize tabs
255 self.raw = raw.expandtabs().rstrip()
250 self.raw = raw.expandtabs().rstrip()
256
251
257 # store line offsets in self.lines
252 # store line offsets in self.lines
258 self.lines = [0, 0]
253 self.lines = [0, 0]
259 pos = 0
254 pos = 0
260 raw_find = self.raw.find
255 raw_find = self.raw.find
261 lines_append = self.lines.append
256 lines_append = self.lines.append
262 while 1:
257 while 1:
263 pos = raw_find('\n', pos) + 1
258 pos = raw_find('\n', pos) + 1
264 if not pos: break
259 if not pos: break
265 lines_append(pos)
260 lines_append(pos)
266 lines_append(len(self.raw))
261 lines_append(len(self.raw))
267
262
268 # parse the source and write it
263 # parse the source and write it
269 self.pos = 0
264 self.pos = 0
270 text = StringIO(self.raw)
265 text = StringIO(self.raw)
271
266
272 error = False
267 error = False
273 try:
268 try:
274 for atoken in generate_tokens(text.readline):
269 for atoken in generate_tokens(text.readline):
275 self(*atoken)
270 self(*atoken)
276 except tokenize.TokenError as ex:
271 except tokenize.TokenError as ex:
277 msg = ex.args[0]
272 msg = ex.args[0]
278 line = ex.args[1][0]
273 line = ex.args[1][0]
279 self.out.write("%s\n\n*** ERROR: %s%s%s\n" %
274 self.out.write("%s\n\n*** ERROR: %s%s%s\n" %
280 (colors[token.ERRORTOKEN],
275 (colors[token.ERRORTOKEN],
281 msg, self.raw[self.lines[line]:],
276 msg, self.raw[self.lines[line]:],
282 colors.normal)
277 colors.normal)
283 )
278 )
284 error = True
279 error = True
285 self.out.write(colors.normal+'\n')
280 self.out.write(colors.normal+'\n')
286 if string_output:
281 if string_output:
287 output = self.out.getvalue()
282 output = self.out.getvalue()
288 self.out = out_old
283 self.out = out_old
289 return (output, error)
284 return (output, error)
290 return (None, error)
285 return (None, error)
291
286
292 def __call__(self, toktype, toktext, start_pos, end_pos, line):
287 def __call__(self, toktype, toktext, start_pos, end_pos, line):
293 """ Token handler, with syntax highlighting."""
288 """ Token handler, with syntax highlighting."""
294 (srow,scol) = start_pos
289 (srow,scol) = start_pos
295 (erow,ecol) = end_pos
290 (erow,ecol) = end_pos
296 colors = self.colors
291 colors = self.colors
297 owrite = self.out.write
292 owrite = self.out.write
298
293
299 # line separator, so this works across platforms
294 # line separator, so this works across platforms
300 linesep = os.linesep
295 linesep = os.linesep
301
296
302 # calculate new positions
297 # calculate new positions
303 oldpos = self.pos
298 oldpos = self.pos
304 newpos = self.lines[srow] + scol
299 newpos = self.lines[srow] + scol
305 self.pos = newpos + len(toktext)
300 self.pos = newpos + len(toktext)
306
301
307 # send the original whitespace, if needed
302 # send the original whitespace, if needed
308 if newpos > oldpos:
303 if newpos > oldpos:
309 owrite(self.raw[oldpos:newpos])
304 owrite(self.raw[oldpos:newpos])
310
305
311 # skip indenting tokens
306 # skip indenting tokens
312 if toktype in [token.INDENT, token.DEDENT]:
307 if toktype in [token.INDENT, token.DEDENT]:
313 self.pos = newpos
308 self.pos = newpos
314 return
309 return
315
310
316 # map token type to a color group
311 # map token type to a color group
317 if token.LPAR <= toktype <= token.OP:
312 if token.LPAR <= toktype <= token.OP:
318 toktype = token.OP
313 toktype = token.OP
319 elif toktype == token.NAME and keyword.iskeyword(toktext):
314 elif toktype == token.NAME and keyword.iskeyword(toktext):
320 toktype = _KEYWORD
315 toktype = _KEYWORD
321 color = colors.get(toktype, colors[_TEXT])
316 color = colors.get(toktype, colors[_TEXT])
322
317
323 #print '<%s>' % toktext, # dbg
318 #print '<%s>' % toktext, # dbg
324
319
325 # Triple quoted strings must be handled carefully so that backtracking
320 # Triple quoted strings must be handled carefully so that backtracking
326 # in pagers works correctly. We need color terminators on _each_ line.
321 # in pagers works correctly. We need color terminators on _each_ line.
327 if linesep in toktext:
322 if linesep in toktext:
328 toktext = toktext.replace(linesep, '%s%s%s' %
323 toktext = toktext.replace(linesep, '%s%s%s' %
329 (colors.normal,linesep,color))
324 (colors.normal,linesep,color))
330
325
331 # send text
326 # send text
332 owrite('%s%s%s' % (color,toktext,colors.normal))
327 owrite('%s%s%s' % (color,toktext,colors.normal))
General Comments 0
You need to be logged in to leave comments. Login now