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