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