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