##// END OF EJS Templates
Remove runlines method and calls to it.
Thomas Kluyver -
Show More
@@ -1,2608 +1,2562 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
19
20 import __builtin__
20 import __builtin__
21 import __future__
21 import __future__
22 import abc
22 import abc
23 import ast
23 import ast
24 import atexit
24 import atexit
25 import codeop
25 import codeop
26 import inspect
26 import inspect
27 import os
27 import os
28 import re
28 import re
29 import sys
29 import sys
30 import tempfile
30 import tempfile
31 import types
31 import types
32 from contextlib import nested
32 from contextlib import nested
33
33
34 from IPython.config.configurable import Configurable
34 from IPython.config.configurable import Configurable
35 from IPython.core import debugger, oinspect
35 from IPython.core import debugger, oinspect
36 from IPython.core import history as ipcorehist
36 from IPython.core import history as ipcorehist
37 from IPython.core import page
37 from IPython.core import page
38 from IPython.core import prefilter
38 from IPython.core import prefilter
39 from IPython.core import shadowns
39 from IPython.core import shadowns
40 from IPython.core import ultratb
40 from IPython.core import ultratb
41 from IPython.core.alias import AliasManager
41 from IPython.core.alias import AliasManager
42 from IPython.core.autocall import ExitAutocall
42 from IPython.core.autocall import ExitAutocall
43 from IPython.core.builtin_trap import BuiltinTrap
43 from IPython.core.builtin_trap import BuiltinTrap
44 from IPython.core.compilerop import CachingCompiler
44 from IPython.core.compilerop import CachingCompiler
45 from IPython.core.display_trap import DisplayTrap
45 from IPython.core.display_trap import DisplayTrap
46 from IPython.core.displayhook import DisplayHook
46 from IPython.core.displayhook import DisplayHook
47 from IPython.core.displaypub import DisplayPublisher
47 from IPython.core.displaypub import DisplayPublisher
48 from IPython.core.error import TryNext, UsageError
48 from IPython.core.error import TryNext, UsageError
49 from IPython.core.extensions import ExtensionManager
49 from IPython.core.extensions import ExtensionManager
50 from IPython.core.fakemodule import FakeModule, init_fakemod_dict
50 from IPython.core.fakemodule import FakeModule, init_fakemod_dict
51 from IPython.core.formatters import DisplayFormatter
51 from IPython.core.formatters import DisplayFormatter
52 from IPython.core.history import HistoryManager
52 from IPython.core.history import HistoryManager
53 from IPython.core.inputsplitter import IPythonInputSplitter
53 from IPython.core.inputsplitter import IPythonInputSplitter
54 from IPython.core.logger import Logger
54 from IPython.core.logger import Logger
55 from IPython.core.macro import Macro
55 from IPython.core.macro import Macro
56 from IPython.core.magic import Magic
56 from IPython.core.magic import Magic
57 from IPython.core.payload import PayloadManager
57 from IPython.core.payload import PayloadManager
58 from IPython.core.plugin import PluginManager
58 from IPython.core.plugin import PluginManager
59 from IPython.core.prefilter import PrefilterManager, ESC_MAGIC
59 from IPython.core.prefilter import PrefilterManager, ESC_MAGIC
60 from IPython.external.Itpl import ItplNS
60 from IPython.external.Itpl import ItplNS
61 from IPython.utils import PyColorize
61 from IPython.utils import PyColorize
62 from IPython.utils import io
62 from IPython.utils import io
63 from IPython.utils.doctestreload import doctest_reload
63 from IPython.utils.doctestreload import doctest_reload
64 from IPython.utils.io import ask_yes_no, rprint
64 from IPython.utils.io import ask_yes_no, rprint
65 from IPython.utils.ipstruct import Struct
65 from IPython.utils.ipstruct import Struct
66 from IPython.utils.path import get_home_dir, get_ipython_dir, HomeDirError
66 from IPython.utils.path import get_home_dir, get_ipython_dir, HomeDirError
67 from IPython.utils.pickleshare import PickleShareDB
67 from IPython.utils.pickleshare import PickleShareDB
68 from IPython.utils.process import system, getoutput
68 from IPython.utils.process import system, getoutput
69 from IPython.utils.strdispatch import StrDispatch
69 from IPython.utils.strdispatch import StrDispatch
70 from IPython.utils.syspathcontext import prepended_to_syspath
70 from IPython.utils.syspathcontext import prepended_to_syspath
71 from IPython.utils.text import num_ini_spaces, format_screen, LSString, SList
71 from IPython.utils.text import num_ini_spaces, format_screen, LSString, SList
72 from IPython.utils.traitlets import (Int, Str, CBool, CaselessStrEnum, Enum,
72 from IPython.utils.traitlets import (Int, Str, CBool, CaselessStrEnum, Enum,
73 List, Unicode, Instance, Type)
73 List, Unicode, Instance, Type)
74 from IPython.utils.warn import warn, error, fatal
74 from IPython.utils.warn import warn, error, fatal
75 import IPython.core.hooks
75 import IPython.core.hooks
76
76
77 #-----------------------------------------------------------------------------
77 #-----------------------------------------------------------------------------
78 # Globals
78 # Globals
79 #-----------------------------------------------------------------------------
79 #-----------------------------------------------------------------------------
80
80
81 # compiled regexps for autoindent management
81 # compiled regexps for autoindent management
82 dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass')
82 dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass')
83
83
84 #-----------------------------------------------------------------------------
84 #-----------------------------------------------------------------------------
85 # Utilities
85 # Utilities
86 #-----------------------------------------------------------------------------
86 #-----------------------------------------------------------------------------
87
87
88 # store the builtin raw_input globally, and use this always, in case user code
88 # store the builtin raw_input globally, and use this always, in case user code
89 # overwrites it (like wx.py.PyShell does)
89 # overwrites it (like wx.py.PyShell does)
90 raw_input_original = raw_input
90 raw_input_original = raw_input
91
91
92 def softspace(file, newvalue):
92 def softspace(file, newvalue):
93 """Copied from code.py, to remove the dependency"""
93 """Copied from code.py, to remove the dependency"""
94
94
95 oldvalue = 0
95 oldvalue = 0
96 try:
96 try:
97 oldvalue = file.softspace
97 oldvalue = file.softspace
98 except AttributeError:
98 except AttributeError:
99 pass
99 pass
100 try:
100 try:
101 file.softspace = newvalue
101 file.softspace = newvalue
102 except (AttributeError, TypeError):
102 except (AttributeError, TypeError):
103 # "attribute-less object" or "read-only attributes"
103 # "attribute-less object" or "read-only attributes"
104 pass
104 pass
105 return oldvalue
105 return oldvalue
106
106
107
107
108 def no_op(*a, **kw): pass
108 def no_op(*a, **kw): pass
109
109
110 class SpaceInInput(Exception): pass
110 class SpaceInInput(Exception): pass
111
111
112 class Bunch: pass
112 class Bunch: pass
113
113
114
114
115 def get_default_colors():
115 def get_default_colors():
116 if sys.platform=='darwin':
116 if sys.platform=='darwin':
117 return "LightBG"
117 return "LightBG"
118 elif os.name=='nt':
118 elif os.name=='nt':
119 return 'Linux'
119 return 'Linux'
120 else:
120 else:
121 return 'Linux'
121 return 'Linux'
122
122
123
123
124 class SeparateStr(Str):
124 class SeparateStr(Str):
125 """A Str subclass to validate separate_in, separate_out, etc.
125 """A Str subclass to validate separate_in, separate_out, etc.
126
126
127 This is a Str based trait that converts '0'->'' and '\\n'->'\n'.
127 This is a Str based trait that converts '0'->'' and '\\n'->'\n'.
128 """
128 """
129
129
130 def validate(self, obj, value):
130 def validate(self, obj, value):
131 if value == '0': value = ''
131 if value == '0': value = ''
132 value = value.replace('\\n','\n')
132 value = value.replace('\\n','\n')
133 return super(SeparateStr, self).validate(obj, value)
133 return super(SeparateStr, self).validate(obj, value)
134
134
135 class MultipleInstanceError(Exception):
135 class MultipleInstanceError(Exception):
136 pass
136 pass
137
137
138 class ReadlineNoRecord(object):
138 class ReadlineNoRecord(object):
139 """Context manager to execute some code, then reload readline history
139 """Context manager to execute some code, then reload readline history
140 so that interactive input to the code doesn't appear when pressing up."""
140 so that interactive input to the code doesn't appear when pressing up."""
141 def __init__(self, shell):
141 def __init__(self, shell):
142 self.shell = shell
142 self.shell = shell
143 self._nested_level = 0
143 self._nested_level = 0
144
144
145 def __enter__(self):
145 def __enter__(self):
146 if self._nested_level == 0:
146 if self._nested_level == 0:
147 self.orig_length = self.current_length()
147 self.orig_length = self.current_length()
148 self.readline_tail = self.get_readline_tail()
148 self.readline_tail = self.get_readline_tail()
149 self._nested_level += 1
149 self._nested_level += 1
150
150
151 def __exit__(self, type, value, traceback):
151 def __exit__(self, type, value, traceback):
152 self._nested_level -= 1
152 self._nested_level -= 1
153 if self._nested_level == 0:
153 if self._nested_level == 0:
154 # Try clipping the end if it's got longer
154 # Try clipping the end if it's got longer
155 e = self.current_length() - self.orig_length
155 e = self.current_length() - self.orig_length
156 if e > 0:
156 if e > 0:
157 for _ in range(e):
157 for _ in range(e):
158 self.shell.readline.remove_history_item(self.orig_length)
158 self.shell.readline.remove_history_item(self.orig_length)
159
159
160 # If it still doesn't match, just reload readline history.
160 # If it still doesn't match, just reload readline history.
161 if self.current_length() != self.orig_length \
161 if self.current_length() != self.orig_length \
162 or self.get_readline_tail() != self.readline_tail:
162 or self.get_readline_tail() != self.readline_tail:
163 self.shell.refill_readline_hist()
163 self.shell.refill_readline_hist()
164 # Returning False will cause exceptions to propagate
164 # Returning False will cause exceptions to propagate
165 return False
165 return False
166
166
167 def current_length(self):
167 def current_length(self):
168 return self.shell.readline.get_current_history_length()
168 return self.shell.readline.get_current_history_length()
169
169
170 def get_readline_tail(self, n=10):
170 def get_readline_tail(self, n=10):
171 """Get the last n items in readline history."""
171 """Get the last n items in readline history."""
172 end = self.shell.readline.get_current_history_length() + 1
172 end = self.shell.readline.get_current_history_length() + 1
173 start = max(end-n, 1)
173 start = max(end-n, 1)
174 ghi = self.shell.readline.get_history_item
174 ghi = self.shell.readline.get_history_item
175 return [ghi(x) for x in range(start, end)]
175 return [ghi(x) for x in range(start, end)]
176
176
177
177
178 #-----------------------------------------------------------------------------
178 #-----------------------------------------------------------------------------
179 # Main IPython class
179 # Main IPython class
180 #-----------------------------------------------------------------------------
180 #-----------------------------------------------------------------------------
181
181
182 class InteractiveShell(Configurable, Magic):
182 class InteractiveShell(Configurable, Magic):
183 """An enhanced, interactive shell for Python."""
183 """An enhanced, interactive shell for Python."""
184
184
185 _instance = None
185 _instance = None
186 autocall = Enum((0,1,2), default_value=1, config=True)
186 autocall = Enum((0,1,2), default_value=1, config=True)
187 # TODO: remove all autoindent logic and put into frontends.
187 # TODO: remove all autoindent logic and put into frontends.
188 # We can't do this yet because even runlines uses the autoindent.
188 # We can't do this yet because even runlines uses the autoindent.
189 autoindent = CBool(True, config=True)
189 autoindent = CBool(True, config=True)
190 automagic = CBool(True, config=True)
190 automagic = CBool(True, config=True)
191 cache_size = Int(1000, config=True)
191 cache_size = Int(1000, config=True)
192 color_info = CBool(True, config=True)
192 color_info = CBool(True, config=True)
193 colors = CaselessStrEnum(('NoColor','LightBG','Linux'),
193 colors = CaselessStrEnum(('NoColor','LightBG','Linux'),
194 default_value=get_default_colors(), config=True)
194 default_value=get_default_colors(), config=True)
195 debug = CBool(False, config=True)
195 debug = CBool(False, config=True)
196 deep_reload = CBool(False, config=True)
196 deep_reload = CBool(False, config=True)
197 display_formatter = Instance(DisplayFormatter)
197 display_formatter = Instance(DisplayFormatter)
198 displayhook_class = Type(DisplayHook)
198 displayhook_class = Type(DisplayHook)
199 display_pub_class = Type(DisplayPublisher)
199 display_pub_class = Type(DisplayPublisher)
200
200
201 exit_now = CBool(False)
201 exit_now = CBool(False)
202 exiter = Instance(ExitAutocall)
202 exiter = Instance(ExitAutocall)
203 def _exiter_default(self):
203 def _exiter_default(self):
204 return ExitAutocall(self)
204 return ExitAutocall(self)
205 # Monotonically increasing execution counter
205 # Monotonically increasing execution counter
206 execution_count = Int(1)
206 execution_count = Int(1)
207 filename = Unicode("<ipython console>")
207 filename = Unicode("<ipython console>")
208 ipython_dir= Unicode('', config=True) # Set to get_ipython_dir() in __init__
208 ipython_dir= Unicode('', config=True) # Set to get_ipython_dir() in __init__
209
209
210 # Input splitter, to split entire cells of input into either individual
210 # Input splitter, to split entire cells of input into either individual
211 # interactive statements or whole blocks.
211 # interactive statements or whole blocks.
212 input_splitter = Instance('IPython.core.inputsplitter.IPythonInputSplitter',
212 input_splitter = Instance('IPython.core.inputsplitter.IPythonInputSplitter',
213 (), {})
213 (), {})
214 logstart = CBool(False, config=True)
214 logstart = CBool(False, config=True)
215 logfile = Unicode('', config=True)
215 logfile = Unicode('', config=True)
216 logappend = Unicode('', config=True)
216 logappend = Unicode('', config=True)
217 object_info_string_level = Enum((0,1,2), default_value=0,
217 object_info_string_level = Enum((0,1,2), default_value=0,
218 config=True)
218 config=True)
219 pdb = CBool(False, config=True)
219 pdb = CBool(False, config=True)
220
220
221 profile = Unicode('', config=True)
221 profile = Unicode('', config=True)
222 prompt_in1 = Str('In [\\#]: ', config=True)
222 prompt_in1 = Str('In [\\#]: ', config=True)
223 prompt_in2 = Str(' .\\D.: ', config=True)
223 prompt_in2 = Str(' .\\D.: ', config=True)
224 prompt_out = Str('Out[\\#]: ', config=True)
224 prompt_out = Str('Out[\\#]: ', config=True)
225 prompts_pad_left = CBool(True, config=True)
225 prompts_pad_left = CBool(True, config=True)
226 quiet = CBool(False, config=True)
226 quiet = CBool(False, config=True)
227
227
228 history_length = Int(10000, config=True)
228 history_length = Int(10000, config=True)
229
229
230 # The readline stuff will eventually be moved to the terminal subclass
230 # The readline stuff will eventually be moved to the terminal subclass
231 # but for now, we can't do that as readline is welded in everywhere.
231 # but for now, we can't do that as readline is welded in everywhere.
232 readline_use = CBool(True, config=True)
232 readline_use = CBool(True, config=True)
233 readline_merge_completions = CBool(True, config=True)
233 readline_merge_completions = CBool(True, config=True)
234 readline_omit__names = Enum((0,1,2), default_value=2, config=True)
234 readline_omit__names = Enum((0,1,2), default_value=2, config=True)
235 readline_remove_delims = Str('-/~', config=True)
235 readline_remove_delims = Str('-/~', config=True)
236 readline_parse_and_bind = List([
236 readline_parse_and_bind = List([
237 'tab: complete',
237 'tab: complete',
238 '"\C-l": clear-screen',
238 '"\C-l": clear-screen',
239 'set show-all-if-ambiguous on',
239 'set show-all-if-ambiguous on',
240 '"\C-o": tab-insert',
240 '"\C-o": tab-insert',
241 # See bug gh-58 - with \M-i enabled, chars 0x9000-0x9fff
241 # See bug gh-58 - with \M-i enabled, chars 0x9000-0x9fff
242 # crash IPython.
242 # crash IPython.
243 '"\M-o": "\d\d\d\d"',
243 '"\M-o": "\d\d\d\d"',
244 '"\M-I": "\d\d\d\d"',
244 '"\M-I": "\d\d\d\d"',
245 '"\C-r": reverse-search-history',
245 '"\C-r": reverse-search-history',
246 '"\C-s": forward-search-history',
246 '"\C-s": forward-search-history',
247 '"\C-p": history-search-backward',
247 '"\C-p": history-search-backward',
248 '"\C-n": history-search-forward',
248 '"\C-n": history-search-forward',
249 '"\e[A": history-search-backward',
249 '"\e[A": history-search-backward',
250 '"\e[B": history-search-forward',
250 '"\e[B": history-search-forward',
251 '"\C-k": kill-line',
251 '"\C-k": kill-line',
252 '"\C-u": unix-line-discard',
252 '"\C-u": unix-line-discard',
253 ], allow_none=False, config=True)
253 ], allow_none=False, config=True)
254
254
255 # TODO: this part of prompt management should be moved to the frontends.
255 # TODO: this part of prompt management should be moved to the frontends.
256 # Use custom TraitTypes that convert '0'->'' and '\\n'->'\n'
256 # Use custom TraitTypes that convert '0'->'' and '\\n'->'\n'
257 separate_in = SeparateStr('\n', config=True)
257 separate_in = SeparateStr('\n', config=True)
258 separate_out = SeparateStr('', config=True)
258 separate_out = SeparateStr('', config=True)
259 separate_out2 = SeparateStr('', config=True)
259 separate_out2 = SeparateStr('', config=True)
260 wildcards_case_sensitive = CBool(True, config=True)
260 wildcards_case_sensitive = CBool(True, config=True)
261 xmode = CaselessStrEnum(('Context','Plain', 'Verbose'),
261 xmode = CaselessStrEnum(('Context','Plain', 'Verbose'),
262 default_value='Context', config=True)
262 default_value='Context', config=True)
263
263
264 # Subcomponents of InteractiveShell
264 # Subcomponents of InteractiveShell
265 alias_manager = Instance('IPython.core.alias.AliasManager')
265 alias_manager = Instance('IPython.core.alias.AliasManager')
266 prefilter_manager = Instance('IPython.core.prefilter.PrefilterManager')
266 prefilter_manager = Instance('IPython.core.prefilter.PrefilterManager')
267 builtin_trap = Instance('IPython.core.builtin_trap.BuiltinTrap')
267 builtin_trap = Instance('IPython.core.builtin_trap.BuiltinTrap')
268 display_trap = Instance('IPython.core.display_trap.DisplayTrap')
268 display_trap = Instance('IPython.core.display_trap.DisplayTrap')
269 extension_manager = Instance('IPython.core.extensions.ExtensionManager')
269 extension_manager = Instance('IPython.core.extensions.ExtensionManager')
270 plugin_manager = Instance('IPython.core.plugin.PluginManager')
270 plugin_manager = Instance('IPython.core.plugin.PluginManager')
271 payload_manager = Instance('IPython.core.payload.PayloadManager')
271 payload_manager = Instance('IPython.core.payload.PayloadManager')
272 history_manager = Instance('IPython.core.history.HistoryManager')
272 history_manager = Instance('IPython.core.history.HistoryManager')
273
273
274 # Private interface
274 # Private interface
275 _post_execute = Instance(dict)
275 _post_execute = Instance(dict)
276
276
277 def __init__(self, config=None, ipython_dir=None,
277 def __init__(self, config=None, ipython_dir=None,
278 user_ns=None, user_global_ns=None,
278 user_ns=None, user_global_ns=None,
279 custom_exceptions=((), None)):
279 custom_exceptions=((), None)):
280
280
281 # This is where traits with a config_key argument are updated
281 # This is where traits with a config_key argument are updated
282 # from the values on config.
282 # from the values on config.
283 super(InteractiveShell, self).__init__(config=config)
283 super(InteractiveShell, self).__init__(config=config)
284
284
285 # These are relatively independent and stateless
285 # These are relatively independent and stateless
286 self.init_ipython_dir(ipython_dir)
286 self.init_ipython_dir(ipython_dir)
287 self.init_instance_attrs()
287 self.init_instance_attrs()
288 self.init_environment()
288 self.init_environment()
289
289
290 # Create namespaces (user_ns, user_global_ns, etc.)
290 # Create namespaces (user_ns, user_global_ns, etc.)
291 self.init_create_namespaces(user_ns, user_global_ns)
291 self.init_create_namespaces(user_ns, user_global_ns)
292 # This has to be done after init_create_namespaces because it uses
292 # This has to be done after init_create_namespaces because it uses
293 # something in self.user_ns, but before init_sys_modules, which
293 # something in self.user_ns, but before init_sys_modules, which
294 # is the first thing to modify sys.
294 # is the first thing to modify sys.
295 # TODO: When we override sys.stdout and sys.stderr before this class
295 # TODO: When we override sys.stdout and sys.stderr before this class
296 # is created, we are saving the overridden ones here. Not sure if this
296 # is created, we are saving the overridden ones here. Not sure if this
297 # is what we want to do.
297 # is what we want to do.
298 self.save_sys_module_state()
298 self.save_sys_module_state()
299 self.init_sys_modules()
299 self.init_sys_modules()
300
300
301 # While we're trying to have each part of the code directly access what
301 # While we're trying to have each part of the code directly access what
302 # it needs without keeping redundant references to objects, we have too
302 # it needs without keeping redundant references to objects, we have too
303 # much legacy code that expects ip.db to exist.
303 # much legacy code that expects ip.db to exist.
304 self.db = PickleShareDB(os.path.join(self.ipython_dir, 'db'))
304 self.db = PickleShareDB(os.path.join(self.ipython_dir, 'db'))
305
305
306 self.init_history()
306 self.init_history()
307 self.init_encoding()
307 self.init_encoding()
308 self.init_prefilter()
308 self.init_prefilter()
309
309
310 Magic.__init__(self, self)
310 Magic.__init__(self, self)
311
311
312 self.init_syntax_highlighting()
312 self.init_syntax_highlighting()
313 self.init_hooks()
313 self.init_hooks()
314 self.init_pushd_popd_magic()
314 self.init_pushd_popd_magic()
315 # self.init_traceback_handlers use to be here, but we moved it below
315 # self.init_traceback_handlers use to be here, but we moved it below
316 # because it and init_io have to come after init_readline.
316 # because it and init_io have to come after init_readline.
317 self.init_user_ns()
317 self.init_user_ns()
318 self.init_logger()
318 self.init_logger()
319 self.init_alias()
319 self.init_alias()
320 self.init_builtins()
320 self.init_builtins()
321
321
322 # pre_config_initialization
322 # pre_config_initialization
323
323
324 # The next section should contain everything that was in ipmaker.
324 # The next section should contain everything that was in ipmaker.
325 self.init_logstart()
325 self.init_logstart()
326
326
327 # The following was in post_config_initialization
327 # The following was in post_config_initialization
328 self.init_inspector()
328 self.init_inspector()
329 # init_readline() must come before init_io(), because init_io uses
329 # init_readline() must come before init_io(), because init_io uses
330 # readline related things.
330 # readline related things.
331 self.init_readline()
331 self.init_readline()
332 # init_completer must come after init_readline, because it needs to
332 # init_completer must come after init_readline, because it needs to
333 # know whether readline is present or not system-wide to configure the
333 # know whether readline is present or not system-wide to configure the
334 # completers, since the completion machinery can now operate
334 # completers, since the completion machinery can now operate
335 # independently of readline (e.g. over the network)
335 # independently of readline (e.g. over the network)
336 self.init_completer()
336 self.init_completer()
337 # TODO: init_io() needs to happen before init_traceback handlers
337 # TODO: init_io() needs to happen before init_traceback handlers
338 # because the traceback handlers hardcode the stdout/stderr streams.
338 # because the traceback handlers hardcode the stdout/stderr streams.
339 # This logic in in debugger.Pdb and should eventually be changed.
339 # This logic in in debugger.Pdb and should eventually be changed.
340 self.init_io()
340 self.init_io()
341 self.init_traceback_handlers(custom_exceptions)
341 self.init_traceback_handlers(custom_exceptions)
342 self.init_prompts()
342 self.init_prompts()
343 self.init_display_formatter()
343 self.init_display_formatter()
344 self.init_display_pub()
344 self.init_display_pub()
345 self.init_displayhook()
345 self.init_displayhook()
346 self.init_reload_doctest()
346 self.init_reload_doctest()
347 self.init_magics()
347 self.init_magics()
348 self.init_pdb()
348 self.init_pdb()
349 self.init_extension_manager()
349 self.init_extension_manager()
350 self.init_plugin_manager()
350 self.init_plugin_manager()
351 self.init_payload()
351 self.init_payload()
352 self.hooks.late_startup_hook()
352 self.hooks.late_startup_hook()
353 atexit.register(self.atexit_operations)
353 atexit.register(self.atexit_operations)
354
354
355 @classmethod
355 @classmethod
356 def instance(cls, *args, **kwargs):
356 def instance(cls, *args, **kwargs):
357 """Returns a global InteractiveShell instance."""
357 """Returns a global InteractiveShell instance."""
358 if cls._instance is None:
358 if cls._instance is None:
359 inst = cls(*args, **kwargs)
359 inst = cls(*args, **kwargs)
360 # Now make sure that the instance will also be returned by
360 # Now make sure that the instance will also be returned by
361 # the subclasses instance attribute.
361 # the subclasses instance attribute.
362 for subclass in cls.mro():
362 for subclass in cls.mro():
363 if issubclass(cls, subclass) and \
363 if issubclass(cls, subclass) and \
364 issubclass(subclass, InteractiveShell):
364 issubclass(subclass, InteractiveShell):
365 subclass._instance = inst
365 subclass._instance = inst
366 else:
366 else:
367 break
367 break
368 if isinstance(cls._instance, cls):
368 if isinstance(cls._instance, cls):
369 return cls._instance
369 return cls._instance
370 else:
370 else:
371 raise MultipleInstanceError(
371 raise MultipleInstanceError(
372 'Multiple incompatible subclass instances of '
372 'Multiple incompatible subclass instances of '
373 'InteractiveShell are being created.'
373 'InteractiveShell are being created.'
374 )
374 )
375
375
376 @classmethod
376 @classmethod
377 def initialized(cls):
377 def initialized(cls):
378 return hasattr(cls, "_instance")
378 return hasattr(cls, "_instance")
379
379
380 def get_ipython(self):
380 def get_ipython(self):
381 """Return the currently running IPython instance."""
381 """Return the currently running IPython instance."""
382 return self
382 return self
383
383
384 #-------------------------------------------------------------------------
384 #-------------------------------------------------------------------------
385 # Trait changed handlers
385 # Trait changed handlers
386 #-------------------------------------------------------------------------
386 #-------------------------------------------------------------------------
387
387
388 def _ipython_dir_changed(self, name, new):
388 def _ipython_dir_changed(self, name, new):
389 if not os.path.isdir(new):
389 if not os.path.isdir(new):
390 os.makedirs(new, mode = 0777)
390 os.makedirs(new, mode = 0777)
391
391
392 def set_autoindent(self,value=None):
392 def set_autoindent(self,value=None):
393 """Set the autoindent flag, checking for readline support.
393 """Set the autoindent flag, checking for readline support.
394
394
395 If called with no arguments, it acts as a toggle."""
395 If called with no arguments, it acts as a toggle."""
396
396
397 if not self.has_readline:
397 if not self.has_readline:
398 if os.name == 'posix':
398 if os.name == 'posix':
399 warn("The auto-indent feature requires the readline library")
399 warn("The auto-indent feature requires the readline library")
400 self.autoindent = 0
400 self.autoindent = 0
401 return
401 return
402 if value is None:
402 if value is None:
403 self.autoindent = not self.autoindent
403 self.autoindent = not self.autoindent
404 else:
404 else:
405 self.autoindent = value
405 self.autoindent = value
406
406
407 #-------------------------------------------------------------------------
407 #-------------------------------------------------------------------------
408 # init_* methods called by __init__
408 # init_* methods called by __init__
409 #-------------------------------------------------------------------------
409 #-------------------------------------------------------------------------
410
410
411 def init_ipython_dir(self, ipython_dir):
411 def init_ipython_dir(self, ipython_dir):
412 if ipython_dir is not None:
412 if ipython_dir is not None:
413 self.ipython_dir = ipython_dir
413 self.ipython_dir = ipython_dir
414 self.config.Global.ipython_dir = self.ipython_dir
414 self.config.Global.ipython_dir = self.ipython_dir
415 return
415 return
416
416
417 if hasattr(self.config.Global, 'ipython_dir'):
417 if hasattr(self.config.Global, 'ipython_dir'):
418 self.ipython_dir = self.config.Global.ipython_dir
418 self.ipython_dir = self.config.Global.ipython_dir
419 else:
419 else:
420 self.ipython_dir = get_ipython_dir()
420 self.ipython_dir = get_ipython_dir()
421
421
422 # All children can just read this
422 # All children can just read this
423 self.config.Global.ipython_dir = self.ipython_dir
423 self.config.Global.ipython_dir = self.ipython_dir
424
424
425 def init_instance_attrs(self):
425 def init_instance_attrs(self):
426 self.more = False
426 self.more = False
427
427
428 # command compiler
428 # command compiler
429 self.compile = CachingCompiler()
429 self.compile = CachingCompiler()
430
430
431 # User input buffers
431 # User input buffers
432 # NOTE: these variables are slated for full removal, once we are 100%
432 # NOTE: these variables are slated for full removal, once we are 100%
433 # sure that the new execution logic is solid. We will delte runlines,
433 # sure that the new execution logic is solid. We will delte runlines,
434 # push_line and these buffers, as all input will be managed by the
434 # push_line and these buffers, as all input will be managed by the
435 # frontends via an inputsplitter instance.
435 # frontends via an inputsplitter instance.
436 self.buffer = []
436 self.buffer = []
437 self.buffer_raw = []
437 self.buffer_raw = []
438
438
439 # Make an empty namespace, which extension writers can rely on both
439 # Make an empty namespace, which extension writers can rely on both
440 # existing and NEVER being used by ipython itself. This gives them a
440 # existing and NEVER being used by ipython itself. This gives them a
441 # convenient location for storing additional information and state
441 # convenient location for storing additional information and state
442 # their extensions may require, without fear of collisions with other
442 # their extensions may require, without fear of collisions with other
443 # ipython names that may develop later.
443 # ipython names that may develop later.
444 self.meta = Struct()
444 self.meta = Struct()
445
445
446 # Temporary files used for various purposes. Deleted at exit.
446 # Temporary files used for various purposes. Deleted at exit.
447 self.tempfiles = []
447 self.tempfiles = []
448
448
449 # Keep track of readline usage (later set by init_readline)
449 # Keep track of readline usage (later set by init_readline)
450 self.has_readline = False
450 self.has_readline = False
451
451
452 # keep track of where we started running (mainly for crash post-mortem)
452 # keep track of where we started running (mainly for crash post-mortem)
453 # This is not being used anywhere currently.
453 # This is not being used anywhere currently.
454 self.starting_dir = os.getcwd()
454 self.starting_dir = os.getcwd()
455
455
456 # Indentation management
456 # Indentation management
457 self.indent_current_nsp = 0
457 self.indent_current_nsp = 0
458
458
459 # Dict to track post-execution functions that have been registered
459 # Dict to track post-execution functions that have been registered
460 self._post_execute = {}
460 self._post_execute = {}
461
461
462 def init_environment(self):
462 def init_environment(self):
463 """Any changes we need to make to the user's environment."""
463 """Any changes we need to make to the user's environment."""
464 pass
464 pass
465
465
466 def init_encoding(self):
466 def init_encoding(self):
467 # Get system encoding at startup time. Certain terminals (like Emacs
467 # Get system encoding at startup time. Certain terminals (like Emacs
468 # under Win32 have it set to None, and we need to have a known valid
468 # under Win32 have it set to None, and we need to have a known valid
469 # encoding to use in the raw_input() method
469 # encoding to use in the raw_input() method
470 try:
470 try:
471 self.stdin_encoding = sys.stdin.encoding or 'ascii'
471 self.stdin_encoding = sys.stdin.encoding or 'ascii'
472 except AttributeError:
472 except AttributeError:
473 self.stdin_encoding = 'ascii'
473 self.stdin_encoding = 'ascii'
474
474
475 def init_syntax_highlighting(self):
475 def init_syntax_highlighting(self):
476 # Python source parser/formatter for syntax highlighting
476 # Python source parser/formatter for syntax highlighting
477 pyformat = PyColorize.Parser().format
477 pyformat = PyColorize.Parser().format
478 self.pycolorize = lambda src: pyformat(src,'str',self.colors)
478 self.pycolorize = lambda src: pyformat(src,'str',self.colors)
479
479
480 def init_pushd_popd_magic(self):
480 def init_pushd_popd_magic(self):
481 # for pushd/popd management
481 # for pushd/popd management
482 try:
482 try:
483 self.home_dir = get_home_dir()
483 self.home_dir = get_home_dir()
484 except HomeDirError, msg:
484 except HomeDirError, msg:
485 fatal(msg)
485 fatal(msg)
486
486
487 self.dir_stack = []
487 self.dir_stack = []
488
488
489 def init_logger(self):
489 def init_logger(self):
490 self.logger = Logger(self.home_dir, logfname='ipython_log.py',
490 self.logger = Logger(self.home_dir, logfname='ipython_log.py',
491 logmode='rotate')
491 logmode='rotate')
492
492
493 def init_logstart(self):
493 def init_logstart(self):
494 """Initialize logging in case it was requested at the command line.
494 """Initialize logging in case it was requested at the command line.
495 """
495 """
496 if self.logappend:
496 if self.logappend:
497 self.magic_logstart(self.logappend + ' append')
497 self.magic_logstart(self.logappend + ' append')
498 elif self.logfile:
498 elif self.logfile:
499 self.magic_logstart(self.logfile)
499 self.magic_logstart(self.logfile)
500 elif self.logstart:
500 elif self.logstart:
501 self.magic_logstart()
501 self.magic_logstart()
502
502
503 def init_builtins(self):
503 def init_builtins(self):
504 self.builtin_trap = BuiltinTrap(shell=self)
504 self.builtin_trap = BuiltinTrap(shell=self)
505
505
506 def init_inspector(self):
506 def init_inspector(self):
507 # Object inspector
507 # Object inspector
508 self.inspector = oinspect.Inspector(oinspect.InspectColors,
508 self.inspector = oinspect.Inspector(oinspect.InspectColors,
509 PyColorize.ANSICodeColors,
509 PyColorize.ANSICodeColors,
510 'NoColor',
510 'NoColor',
511 self.object_info_string_level)
511 self.object_info_string_level)
512
512
513 def init_io(self):
513 def init_io(self):
514 # This will just use sys.stdout and sys.stderr. If you want to
514 # This will just use sys.stdout and sys.stderr. If you want to
515 # override sys.stdout and sys.stderr themselves, you need to do that
515 # override sys.stdout and sys.stderr themselves, you need to do that
516 # *before* instantiating this class, because Term holds onto
516 # *before* instantiating this class, because Term holds onto
517 # references to the underlying streams.
517 # references to the underlying streams.
518 if sys.platform == 'win32' and self.has_readline:
518 if sys.platform == 'win32' and self.has_readline:
519 Term = io.IOTerm(cout=self.readline._outputfile,
519 Term = io.IOTerm(cout=self.readline._outputfile,
520 cerr=self.readline._outputfile)
520 cerr=self.readline._outputfile)
521 else:
521 else:
522 Term = io.IOTerm()
522 Term = io.IOTerm()
523 io.Term = Term
523 io.Term = Term
524
524
525 def init_prompts(self):
525 def init_prompts(self):
526 # TODO: This is a pass for now because the prompts are managed inside
526 # TODO: This is a pass for now because the prompts are managed inside
527 # the DisplayHook. Once there is a separate prompt manager, this
527 # the DisplayHook. Once there is a separate prompt manager, this
528 # will initialize that object and all prompt related information.
528 # will initialize that object and all prompt related information.
529 pass
529 pass
530
530
531 def init_display_formatter(self):
531 def init_display_formatter(self):
532 self.display_formatter = DisplayFormatter(config=self.config)
532 self.display_formatter = DisplayFormatter(config=self.config)
533
533
534 def init_display_pub(self):
534 def init_display_pub(self):
535 self.display_pub = self.display_pub_class(config=self.config)
535 self.display_pub = self.display_pub_class(config=self.config)
536
536
537 def init_displayhook(self):
537 def init_displayhook(self):
538 # Initialize displayhook, set in/out prompts and printing system
538 # Initialize displayhook, set in/out prompts and printing system
539 self.displayhook = self.displayhook_class(
539 self.displayhook = self.displayhook_class(
540 config=self.config,
540 config=self.config,
541 shell=self,
541 shell=self,
542 cache_size=self.cache_size,
542 cache_size=self.cache_size,
543 input_sep = self.separate_in,
543 input_sep = self.separate_in,
544 output_sep = self.separate_out,
544 output_sep = self.separate_out,
545 output_sep2 = self.separate_out2,
545 output_sep2 = self.separate_out2,
546 ps1 = self.prompt_in1,
546 ps1 = self.prompt_in1,
547 ps2 = self.prompt_in2,
547 ps2 = self.prompt_in2,
548 ps_out = self.prompt_out,
548 ps_out = self.prompt_out,
549 pad_left = self.prompts_pad_left
549 pad_left = self.prompts_pad_left
550 )
550 )
551 # This is a context manager that installs/revmoes the displayhook at
551 # This is a context manager that installs/revmoes the displayhook at
552 # the appropriate time.
552 # the appropriate time.
553 self.display_trap = DisplayTrap(hook=self.displayhook)
553 self.display_trap = DisplayTrap(hook=self.displayhook)
554
554
555 def init_reload_doctest(self):
555 def init_reload_doctest(self):
556 # Do a proper resetting of doctest, including the necessary displayhook
556 # Do a proper resetting of doctest, including the necessary displayhook
557 # monkeypatching
557 # monkeypatching
558 try:
558 try:
559 doctest_reload()
559 doctest_reload()
560 except ImportError:
560 except ImportError:
561 warn("doctest module does not exist.")
561 warn("doctest module does not exist.")
562
562
563 #-------------------------------------------------------------------------
563 #-------------------------------------------------------------------------
564 # Things related to injections into the sys module
564 # Things related to injections into the sys module
565 #-------------------------------------------------------------------------
565 #-------------------------------------------------------------------------
566
566
567 def save_sys_module_state(self):
567 def save_sys_module_state(self):
568 """Save the state of hooks in the sys module.
568 """Save the state of hooks in the sys module.
569
569
570 This has to be called after self.user_ns is created.
570 This has to be called after self.user_ns is created.
571 """
571 """
572 self._orig_sys_module_state = {}
572 self._orig_sys_module_state = {}
573 self._orig_sys_module_state['stdin'] = sys.stdin
573 self._orig_sys_module_state['stdin'] = sys.stdin
574 self._orig_sys_module_state['stdout'] = sys.stdout
574 self._orig_sys_module_state['stdout'] = sys.stdout
575 self._orig_sys_module_state['stderr'] = sys.stderr
575 self._orig_sys_module_state['stderr'] = sys.stderr
576 self._orig_sys_module_state['excepthook'] = sys.excepthook
576 self._orig_sys_module_state['excepthook'] = sys.excepthook
577 try:
577 try:
578 self._orig_sys_modules_main_name = self.user_ns['__name__']
578 self._orig_sys_modules_main_name = self.user_ns['__name__']
579 except KeyError:
579 except KeyError:
580 pass
580 pass
581
581
582 def restore_sys_module_state(self):
582 def restore_sys_module_state(self):
583 """Restore the state of the sys module."""
583 """Restore the state of the sys module."""
584 try:
584 try:
585 for k, v in self._orig_sys_module_state.iteritems():
585 for k, v in self._orig_sys_module_state.iteritems():
586 setattr(sys, k, v)
586 setattr(sys, k, v)
587 except AttributeError:
587 except AttributeError:
588 pass
588 pass
589 # Reset what what done in self.init_sys_modules
589 # Reset what what done in self.init_sys_modules
590 try:
590 try:
591 sys.modules[self.user_ns['__name__']] = self._orig_sys_modules_main_name
591 sys.modules[self.user_ns['__name__']] = self._orig_sys_modules_main_name
592 except (AttributeError, KeyError):
592 except (AttributeError, KeyError):
593 pass
593 pass
594
594
595 #-------------------------------------------------------------------------
595 #-------------------------------------------------------------------------
596 # Things related to hooks
596 # Things related to hooks
597 #-------------------------------------------------------------------------
597 #-------------------------------------------------------------------------
598
598
599 def init_hooks(self):
599 def init_hooks(self):
600 # hooks holds pointers used for user-side customizations
600 # hooks holds pointers used for user-side customizations
601 self.hooks = Struct()
601 self.hooks = Struct()
602
602
603 self.strdispatchers = {}
603 self.strdispatchers = {}
604
604
605 # Set all default hooks, defined in the IPython.hooks module.
605 # Set all default hooks, defined in the IPython.hooks module.
606 hooks = IPython.core.hooks
606 hooks = IPython.core.hooks
607 for hook_name in hooks.__all__:
607 for hook_name in hooks.__all__:
608 # default hooks have priority 100, i.e. low; user hooks should have
608 # default hooks have priority 100, i.e. low; user hooks should have
609 # 0-100 priority
609 # 0-100 priority
610 self.set_hook(hook_name,getattr(hooks,hook_name), 100)
610 self.set_hook(hook_name,getattr(hooks,hook_name), 100)
611
611
612 def set_hook(self,name,hook, priority = 50, str_key = None, re_key = None):
612 def set_hook(self,name,hook, priority = 50, str_key = None, re_key = None):
613 """set_hook(name,hook) -> sets an internal IPython hook.
613 """set_hook(name,hook) -> sets an internal IPython hook.
614
614
615 IPython exposes some of its internal API as user-modifiable hooks. By
615 IPython exposes some of its internal API as user-modifiable hooks. By
616 adding your function to one of these hooks, you can modify IPython's
616 adding your function to one of these hooks, you can modify IPython's
617 behavior to call at runtime your own routines."""
617 behavior to call at runtime your own routines."""
618
618
619 # At some point in the future, this should validate the hook before it
619 # At some point in the future, this should validate the hook before it
620 # accepts it. Probably at least check that the hook takes the number
620 # accepts it. Probably at least check that the hook takes the number
621 # of args it's supposed to.
621 # of args it's supposed to.
622
622
623 f = types.MethodType(hook,self)
623 f = types.MethodType(hook,self)
624
624
625 # check if the hook is for strdispatcher first
625 # check if the hook is for strdispatcher first
626 if str_key is not None:
626 if str_key is not None:
627 sdp = self.strdispatchers.get(name, StrDispatch())
627 sdp = self.strdispatchers.get(name, StrDispatch())
628 sdp.add_s(str_key, f, priority )
628 sdp.add_s(str_key, f, priority )
629 self.strdispatchers[name] = sdp
629 self.strdispatchers[name] = sdp
630 return
630 return
631 if re_key is not None:
631 if re_key is not None:
632 sdp = self.strdispatchers.get(name, StrDispatch())
632 sdp = self.strdispatchers.get(name, StrDispatch())
633 sdp.add_re(re.compile(re_key), f, priority )
633 sdp.add_re(re.compile(re_key), f, priority )
634 self.strdispatchers[name] = sdp
634 self.strdispatchers[name] = sdp
635 return
635 return
636
636
637 dp = getattr(self.hooks, name, None)
637 dp = getattr(self.hooks, name, None)
638 if name not in IPython.core.hooks.__all__:
638 if name not in IPython.core.hooks.__all__:
639 print "Warning! Hook '%s' is not one of %s" % \
639 print "Warning! Hook '%s' is not one of %s" % \
640 (name, IPython.core.hooks.__all__ )
640 (name, IPython.core.hooks.__all__ )
641 if not dp:
641 if not dp:
642 dp = IPython.core.hooks.CommandChainDispatcher()
642 dp = IPython.core.hooks.CommandChainDispatcher()
643
643
644 try:
644 try:
645 dp.add(f,priority)
645 dp.add(f,priority)
646 except AttributeError:
646 except AttributeError:
647 # it was not commandchain, plain old func - replace
647 # it was not commandchain, plain old func - replace
648 dp = f
648 dp = f
649
649
650 setattr(self.hooks,name, dp)
650 setattr(self.hooks,name, dp)
651
651
652 def register_post_execute(self, func):
652 def register_post_execute(self, func):
653 """Register a function for calling after code execution.
653 """Register a function for calling after code execution.
654 """
654 """
655 if not callable(func):
655 if not callable(func):
656 raise ValueError('argument %s must be callable' % func)
656 raise ValueError('argument %s must be callable' % func)
657 self._post_execute[func] = True
657 self._post_execute[func] = True
658
658
659 #-------------------------------------------------------------------------
659 #-------------------------------------------------------------------------
660 # Things related to the "main" module
660 # Things related to the "main" module
661 #-------------------------------------------------------------------------
661 #-------------------------------------------------------------------------
662
662
663 def new_main_mod(self,ns=None):
663 def new_main_mod(self,ns=None):
664 """Return a new 'main' module object for user code execution.
664 """Return a new 'main' module object for user code execution.
665 """
665 """
666 main_mod = self._user_main_module
666 main_mod = self._user_main_module
667 init_fakemod_dict(main_mod,ns)
667 init_fakemod_dict(main_mod,ns)
668 return main_mod
668 return main_mod
669
669
670 def cache_main_mod(self,ns,fname):
670 def cache_main_mod(self,ns,fname):
671 """Cache a main module's namespace.
671 """Cache a main module's namespace.
672
672
673 When scripts are executed via %run, we must keep a reference to the
673 When scripts are executed via %run, we must keep a reference to the
674 namespace of their __main__ module (a FakeModule instance) around so
674 namespace of their __main__ module (a FakeModule instance) around so
675 that Python doesn't clear it, rendering objects defined therein
675 that Python doesn't clear it, rendering objects defined therein
676 useless.
676 useless.
677
677
678 This method keeps said reference in a private dict, keyed by the
678 This method keeps said reference in a private dict, keyed by the
679 absolute path of the module object (which corresponds to the script
679 absolute path of the module object (which corresponds to the script
680 path). This way, for multiple executions of the same script we only
680 path). This way, for multiple executions of the same script we only
681 keep one copy of the namespace (the last one), thus preventing memory
681 keep one copy of the namespace (the last one), thus preventing memory
682 leaks from old references while allowing the objects from the last
682 leaks from old references while allowing the objects from the last
683 execution to be accessible.
683 execution to be accessible.
684
684
685 Note: we can not allow the actual FakeModule instances to be deleted,
685 Note: we can not allow the actual FakeModule instances to be deleted,
686 because of how Python tears down modules (it hard-sets all their
686 because of how Python tears down modules (it hard-sets all their
687 references to None without regard for reference counts). This method
687 references to None without regard for reference counts). This method
688 must therefore make a *copy* of the given namespace, to allow the
688 must therefore make a *copy* of the given namespace, to allow the
689 original module's __dict__ to be cleared and reused.
689 original module's __dict__ to be cleared and reused.
690
690
691
691
692 Parameters
692 Parameters
693 ----------
693 ----------
694 ns : a namespace (a dict, typically)
694 ns : a namespace (a dict, typically)
695
695
696 fname : str
696 fname : str
697 Filename associated with the namespace.
697 Filename associated with the namespace.
698
698
699 Examples
699 Examples
700 --------
700 --------
701
701
702 In [10]: import IPython
702 In [10]: import IPython
703
703
704 In [11]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__)
704 In [11]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__)
705
705
706 In [12]: IPython.__file__ in _ip._main_ns_cache
706 In [12]: IPython.__file__ in _ip._main_ns_cache
707 Out[12]: True
707 Out[12]: True
708 """
708 """
709 self._main_ns_cache[os.path.abspath(fname)] = ns.copy()
709 self._main_ns_cache[os.path.abspath(fname)] = ns.copy()
710
710
711 def clear_main_mod_cache(self):
711 def clear_main_mod_cache(self):
712 """Clear the cache of main modules.
712 """Clear the cache of main modules.
713
713
714 Mainly for use by utilities like %reset.
714 Mainly for use by utilities like %reset.
715
715
716 Examples
716 Examples
717 --------
717 --------
718
718
719 In [15]: import IPython
719 In [15]: import IPython
720
720
721 In [16]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__)
721 In [16]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__)
722
722
723 In [17]: len(_ip._main_ns_cache) > 0
723 In [17]: len(_ip._main_ns_cache) > 0
724 Out[17]: True
724 Out[17]: True
725
725
726 In [18]: _ip.clear_main_mod_cache()
726 In [18]: _ip.clear_main_mod_cache()
727
727
728 In [19]: len(_ip._main_ns_cache) == 0
728 In [19]: len(_ip._main_ns_cache) == 0
729 Out[19]: True
729 Out[19]: True
730 """
730 """
731 self._main_ns_cache.clear()
731 self._main_ns_cache.clear()
732
732
733 #-------------------------------------------------------------------------
733 #-------------------------------------------------------------------------
734 # Things related to debugging
734 # Things related to debugging
735 #-------------------------------------------------------------------------
735 #-------------------------------------------------------------------------
736
736
737 def init_pdb(self):
737 def init_pdb(self):
738 # Set calling of pdb on exceptions
738 # Set calling of pdb on exceptions
739 # self.call_pdb is a property
739 # self.call_pdb is a property
740 self.call_pdb = self.pdb
740 self.call_pdb = self.pdb
741
741
742 def _get_call_pdb(self):
742 def _get_call_pdb(self):
743 return self._call_pdb
743 return self._call_pdb
744
744
745 def _set_call_pdb(self,val):
745 def _set_call_pdb(self,val):
746
746
747 if val not in (0,1,False,True):
747 if val not in (0,1,False,True):
748 raise ValueError,'new call_pdb value must be boolean'
748 raise ValueError,'new call_pdb value must be boolean'
749
749
750 # store value in instance
750 # store value in instance
751 self._call_pdb = val
751 self._call_pdb = val
752
752
753 # notify the actual exception handlers
753 # notify the actual exception handlers
754 self.InteractiveTB.call_pdb = val
754 self.InteractiveTB.call_pdb = val
755
755
756 call_pdb = property(_get_call_pdb,_set_call_pdb,None,
756 call_pdb = property(_get_call_pdb,_set_call_pdb,None,
757 'Control auto-activation of pdb at exceptions')
757 'Control auto-activation of pdb at exceptions')
758
758
759 def debugger(self,force=False):
759 def debugger(self,force=False):
760 """Call the pydb/pdb debugger.
760 """Call the pydb/pdb debugger.
761
761
762 Keywords:
762 Keywords:
763
763
764 - force(False): by default, this routine checks the instance call_pdb
764 - force(False): by default, this routine checks the instance call_pdb
765 flag and does not actually invoke the debugger if the flag is false.
765 flag and does not actually invoke the debugger if the flag is false.
766 The 'force' option forces the debugger to activate even if the flag
766 The 'force' option forces the debugger to activate even if the flag
767 is false.
767 is false.
768 """
768 """
769
769
770 if not (force or self.call_pdb):
770 if not (force or self.call_pdb):
771 return
771 return
772
772
773 if not hasattr(sys,'last_traceback'):
773 if not hasattr(sys,'last_traceback'):
774 error('No traceback has been produced, nothing to debug.')
774 error('No traceback has been produced, nothing to debug.')
775 return
775 return
776
776
777 # use pydb if available
777 # use pydb if available
778 if debugger.has_pydb:
778 if debugger.has_pydb:
779 from pydb import pm
779 from pydb import pm
780 else:
780 else:
781 # fallback to our internal debugger
781 # fallback to our internal debugger
782 pm = lambda : self.InteractiveTB.debugger(force=True)
782 pm = lambda : self.InteractiveTB.debugger(force=True)
783
783
784 with self.readline_no_record:
784 with self.readline_no_record:
785 pm()
785 pm()
786
786
787 #-------------------------------------------------------------------------
787 #-------------------------------------------------------------------------
788 # Things related to IPython's various namespaces
788 # Things related to IPython's various namespaces
789 #-------------------------------------------------------------------------
789 #-------------------------------------------------------------------------
790
790
791 def init_create_namespaces(self, user_ns=None, user_global_ns=None):
791 def init_create_namespaces(self, user_ns=None, user_global_ns=None):
792 # Create the namespace where the user will operate. user_ns is
792 # Create the namespace where the user will operate. user_ns is
793 # normally the only one used, and it is passed to the exec calls as
793 # normally the only one used, and it is passed to the exec calls as
794 # the locals argument. But we do carry a user_global_ns namespace
794 # the locals argument. But we do carry a user_global_ns namespace
795 # given as the exec 'globals' argument, This is useful in embedding
795 # given as the exec 'globals' argument, This is useful in embedding
796 # situations where the ipython shell opens in a context where the
796 # situations where the ipython shell opens in a context where the
797 # distinction between locals and globals is meaningful. For
797 # distinction between locals and globals is meaningful. For
798 # non-embedded contexts, it is just the same object as the user_ns dict.
798 # non-embedded contexts, it is just the same object as the user_ns dict.
799
799
800 # FIXME. For some strange reason, __builtins__ is showing up at user
800 # FIXME. For some strange reason, __builtins__ is showing up at user
801 # level as a dict instead of a module. This is a manual fix, but I
801 # level as a dict instead of a module. This is a manual fix, but I
802 # should really track down where the problem is coming from. Alex
802 # should really track down where the problem is coming from. Alex
803 # Schmolck reported this problem first.
803 # Schmolck reported this problem first.
804
804
805 # A useful post by Alex Martelli on this topic:
805 # A useful post by Alex Martelli on this topic:
806 # Re: inconsistent value from __builtins__
806 # Re: inconsistent value from __builtins__
807 # Von: Alex Martelli <aleaxit@yahoo.com>
807 # Von: Alex Martelli <aleaxit@yahoo.com>
808 # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends
808 # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends
809 # Gruppen: comp.lang.python
809 # Gruppen: comp.lang.python
810
810
811 # Michael Hohn <hohn@hooknose.lbl.gov> wrote:
811 # Michael Hohn <hohn@hooknose.lbl.gov> wrote:
812 # > >>> print type(builtin_check.get_global_binding('__builtins__'))
812 # > >>> print type(builtin_check.get_global_binding('__builtins__'))
813 # > <type 'dict'>
813 # > <type 'dict'>
814 # > >>> print type(__builtins__)
814 # > >>> print type(__builtins__)
815 # > <type 'module'>
815 # > <type 'module'>
816 # > Is this difference in return value intentional?
816 # > Is this difference in return value intentional?
817
817
818 # Well, it's documented that '__builtins__' can be either a dictionary
818 # Well, it's documented that '__builtins__' can be either a dictionary
819 # or a module, and it's been that way for a long time. Whether it's
819 # or a module, and it's been that way for a long time. Whether it's
820 # intentional (or sensible), I don't know. In any case, the idea is
820 # intentional (or sensible), I don't know. In any case, the idea is
821 # that if you need to access the built-in namespace directly, you
821 # that if you need to access the built-in namespace directly, you
822 # should start with "import __builtin__" (note, no 's') which will
822 # should start with "import __builtin__" (note, no 's') which will
823 # definitely give you a module. Yeah, it's somewhat confusing:-(.
823 # definitely give you a module. Yeah, it's somewhat confusing:-(.
824
824
825 # These routines return properly built dicts as needed by the rest of
825 # These routines return properly built dicts as needed by the rest of
826 # the code, and can also be used by extension writers to generate
826 # the code, and can also be used by extension writers to generate
827 # properly initialized namespaces.
827 # properly initialized namespaces.
828 user_ns, user_global_ns = self.make_user_namespaces(user_ns,
828 user_ns, user_global_ns = self.make_user_namespaces(user_ns,
829 user_global_ns)
829 user_global_ns)
830
830
831 # Assign namespaces
831 # Assign namespaces
832 # This is the namespace where all normal user variables live
832 # This is the namespace where all normal user variables live
833 self.user_ns = user_ns
833 self.user_ns = user_ns
834 self.user_global_ns = user_global_ns
834 self.user_global_ns = user_global_ns
835
835
836 # An auxiliary namespace that checks what parts of the user_ns were
836 # An auxiliary namespace that checks what parts of the user_ns were
837 # loaded at startup, so we can list later only variables defined in
837 # loaded at startup, so we can list later only variables defined in
838 # actual interactive use. Since it is always a subset of user_ns, it
838 # actual interactive use. Since it is always a subset of user_ns, it
839 # doesn't need to be separately tracked in the ns_table.
839 # doesn't need to be separately tracked in the ns_table.
840 self.user_ns_hidden = {}
840 self.user_ns_hidden = {}
841
841
842 # A namespace to keep track of internal data structures to prevent
842 # A namespace to keep track of internal data structures to prevent
843 # them from cluttering user-visible stuff. Will be updated later
843 # them from cluttering user-visible stuff. Will be updated later
844 self.internal_ns = {}
844 self.internal_ns = {}
845
845
846 # Now that FakeModule produces a real module, we've run into a nasty
846 # Now that FakeModule produces a real module, we've run into a nasty
847 # problem: after script execution (via %run), the module where the user
847 # problem: after script execution (via %run), the module where the user
848 # code ran is deleted. Now that this object is a true module (needed
848 # code ran is deleted. Now that this object is a true module (needed
849 # so docetst and other tools work correctly), the Python module
849 # so docetst and other tools work correctly), the Python module
850 # teardown mechanism runs over it, and sets to None every variable
850 # teardown mechanism runs over it, and sets to None every variable
851 # present in that module. Top-level references to objects from the
851 # present in that module. Top-level references to objects from the
852 # script survive, because the user_ns is updated with them. However,
852 # script survive, because the user_ns is updated with them. However,
853 # calling functions defined in the script that use other things from
853 # calling functions defined in the script that use other things from
854 # the script will fail, because the function's closure had references
854 # the script will fail, because the function's closure had references
855 # to the original objects, which are now all None. So we must protect
855 # to the original objects, which are now all None. So we must protect
856 # these modules from deletion by keeping a cache.
856 # these modules from deletion by keeping a cache.
857 #
857 #
858 # To avoid keeping stale modules around (we only need the one from the
858 # To avoid keeping stale modules around (we only need the one from the
859 # last run), we use a dict keyed with the full path to the script, so
859 # last run), we use a dict keyed with the full path to the script, so
860 # only the last version of the module is held in the cache. Note,
860 # only the last version of the module is held in the cache. Note,
861 # however, that we must cache the module *namespace contents* (their
861 # however, that we must cache the module *namespace contents* (their
862 # __dict__). Because if we try to cache the actual modules, old ones
862 # __dict__). Because if we try to cache the actual modules, old ones
863 # (uncached) could be destroyed while still holding references (such as
863 # (uncached) could be destroyed while still holding references (such as
864 # those held by GUI objects that tend to be long-lived)>
864 # those held by GUI objects that tend to be long-lived)>
865 #
865 #
866 # The %reset command will flush this cache. See the cache_main_mod()
866 # The %reset command will flush this cache. See the cache_main_mod()
867 # and clear_main_mod_cache() methods for details on use.
867 # and clear_main_mod_cache() methods for details on use.
868
868
869 # This is the cache used for 'main' namespaces
869 # This is the cache used for 'main' namespaces
870 self._main_ns_cache = {}
870 self._main_ns_cache = {}
871 # And this is the single instance of FakeModule whose __dict__ we keep
871 # And this is the single instance of FakeModule whose __dict__ we keep
872 # copying and clearing for reuse on each %run
872 # copying and clearing for reuse on each %run
873 self._user_main_module = FakeModule()
873 self._user_main_module = FakeModule()
874
874
875 # A table holding all the namespaces IPython deals with, so that
875 # A table holding all the namespaces IPython deals with, so that
876 # introspection facilities can search easily.
876 # introspection facilities can search easily.
877 self.ns_table = {'user':user_ns,
877 self.ns_table = {'user':user_ns,
878 'user_global':user_global_ns,
878 'user_global':user_global_ns,
879 'internal':self.internal_ns,
879 'internal':self.internal_ns,
880 'builtin':__builtin__.__dict__
880 'builtin':__builtin__.__dict__
881 }
881 }
882
882
883 # Similarly, track all namespaces where references can be held and that
883 # Similarly, track all namespaces where references can be held and that
884 # we can safely clear (so it can NOT include builtin). This one can be
884 # we can safely clear (so it can NOT include builtin). This one can be
885 # a simple list. Note that the main execution namespaces, user_ns and
885 # a simple list. Note that the main execution namespaces, user_ns and
886 # user_global_ns, can NOT be listed here, as clearing them blindly
886 # user_global_ns, can NOT be listed here, as clearing them blindly
887 # causes errors in object __del__ methods. Instead, the reset() method
887 # causes errors in object __del__ methods. Instead, the reset() method
888 # clears them manually and carefully.
888 # clears them manually and carefully.
889 self.ns_refs_table = [ self.user_ns_hidden,
889 self.ns_refs_table = [ self.user_ns_hidden,
890 self.internal_ns, self._main_ns_cache ]
890 self.internal_ns, self._main_ns_cache ]
891
891
892 def make_user_namespaces(self, user_ns=None, user_global_ns=None):
892 def make_user_namespaces(self, user_ns=None, user_global_ns=None):
893 """Return a valid local and global user interactive namespaces.
893 """Return a valid local and global user interactive namespaces.
894
894
895 This builds a dict with the minimal information needed to operate as a
895 This builds a dict with the minimal information needed to operate as a
896 valid IPython user namespace, which you can pass to the various
896 valid IPython user namespace, which you can pass to the various
897 embedding classes in ipython. The default implementation returns the
897 embedding classes in ipython. The default implementation returns the
898 same dict for both the locals and the globals to allow functions to
898 same dict for both the locals and the globals to allow functions to
899 refer to variables in the namespace. Customized implementations can
899 refer to variables in the namespace. Customized implementations can
900 return different dicts. The locals dictionary can actually be anything
900 return different dicts. The locals dictionary can actually be anything
901 following the basic mapping protocol of a dict, but the globals dict
901 following the basic mapping protocol of a dict, but the globals dict
902 must be a true dict, not even a subclass. It is recommended that any
902 must be a true dict, not even a subclass. It is recommended that any
903 custom object for the locals namespace synchronize with the globals
903 custom object for the locals namespace synchronize with the globals
904 dict somehow.
904 dict somehow.
905
905
906 Raises TypeError if the provided globals namespace is not a true dict.
906 Raises TypeError if the provided globals namespace is not a true dict.
907
907
908 Parameters
908 Parameters
909 ----------
909 ----------
910 user_ns : dict-like, optional
910 user_ns : dict-like, optional
911 The current user namespace. The items in this namespace should
911 The current user namespace. The items in this namespace should
912 be included in the output. If None, an appropriate blank
912 be included in the output. If None, an appropriate blank
913 namespace should be created.
913 namespace should be created.
914 user_global_ns : dict, optional
914 user_global_ns : dict, optional
915 The current user global namespace. The items in this namespace
915 The current user global namespace. The items in this namespace
916 should be included in the output. If None, an appropriate
916 should be included in the output. If None, an appropriate
917 blank namespace should be created.
917 blank namespace should be created.
918
918
919 Returns
919 Returns
920 -------
920 -------
921 A pair of dictionary-like object to be used as the local namespace
921 A pair of dictionary-like object to be used as the local namespace
922 of the interpreter and a dict to be used as the global namespace.
922 of the interpreter and a dict to be used as the global namespace.
923 """
923 """
924
924
925
925
926 # We must ensure that __builtin__ (without the final 's') is always
926 # We must ensure that __builtin__ (without the final 's') is always
927 # available and pointing to the __builtin__ *module*. For more details:
927 # available and pointing to the __builtin__ *module*. For more details:
928 # http://mail.python.org/pipermail/python-dev/2001-April/014068.html
928 # http://mail.python.org/pipermail/python-dev/2001-April/014068.html
929
929
930 if user_ns is None:
930 if user_ns is None:
931 # Set __name__ to __main__ to better match the behavior of the
931 # Set __name__ to __main__ to better match the behavior of the
932 # normal interpreter.
932 # normal interpreter.
933 user_ns = {'__name__' :'__main__',
933 user_ns = {'__name__' :'__main__',
934 '__builtin__' : __builtin__,
934 '__builtin__' : __builtin__,
935 '__builtins__' : __builtin__,
935 '__builtins__' : __builtin__,
936 }
936 }
937 else:
937 else:
938 user_ns.setdefault('__name__','__main__')
938 user_ns.setdefault('__name__','__main__')
939 user_ns.setdefault('__builtin__',__builtin__)
939 user_ns.setdefault('__builtin__',__builtin__)
940 user_ns.setdefault('__builtins__',__builtin__)
940 user_ns.setdefault('__builtins__',__builtin__)
941
941
942 if user_global_ns is None:
942 if user_global_ns is None:
943 user_global_ns = user_ns
943 user_global_ns = user_ns
944 if type(user_global_ns) is not dict:
944 if type(user_global_ns) is not dict:
945 raise TypeError("user_global_ns must be a true dict; got %r"
945 raise TypeError("user_global_ns must be a true dict; got %r"
946 % type(user_global_ns))
946 % type(user_global_ns))
947
947
948 return user_ns, user_global_ns
948 return user_ns, user_global_ns
949
949
950 def init_sys_modules(self):
950 def init_sys_modules(self):
951 # We need to insert into sys.modules something that looks like a
951 # We need to insert into sys.modules something that looks like a
952 # module but which accesses the IPython namespace, for shelve and
952 # module but which accesses the IPython namespace, for shelve and
953 # pickle to work interactively. Normally they rely on getting
953 # pickle to work interactively. Normally they rely on getting
954 # everything out of __main__, but for embedding purposes each IPython
954 # everything out of __main__, but for embedding purposes each IPython
955 # instance has its own private namespace, so we can't go shoving
955 # instance has its own private namespace, so we can't go shoving
956 # everything into __main__.
956 # everything into __main__.
957
957
958 # note, however, that we should only do this for non-embedded
958 # note, however, that we should only do this for non-embedded
959 # ipythons, which really mimic the __main__.__dict__ with their own
959 # ipythons, which really mimic the __main__.__dict__ with their own
960 # namespace. Embedded instances, on the other hand, should not do
960 # namespace. Embedded instances, on the other hand, should not do
961 # this because they need to manage the user local/global namespaces
961 # this because they need to manage the user local/global namespaces
962 # only, but they live within a 'normal' __main__ (meaning, they
962 # only, but they live within a 'normal' __main__ (meaning, they
963 # shouldn't overtake the execution environment of the script they're
963 # shouldn't overtake the execution environment of the script they're
964 # embedded in).
964 # embedded in).
965
965
966 # This is overridden in the InteractiveShellEmbed subclass to a no-op.
966 # This is overridden in the InteractiveShellEmbed subclass to a no-op.
967
967
968 try:
968 try:
969 main_name = self.user_ns['__name__']
969 main_name = self.user_ns['__name__']
970 except KeyError:
970 except KeyError:
971 raise KeyError('user_ns dictionary MUST have a "__name__" key')
971 raise KeyError('user_ns dictionary MUST have a "__name__" key')
972 else:
972 else:
973 sys.modules[main_name] = FakeModule(self.user_ns)
973 sys.modules[main_name] = FakeModule(self.user_ns)
974
974
975 def init_user_ns(self):
975 def init_user_ns(self):
976 """Initialize all user-visible namespaces to their minimum defaults.
976 """Initialize all user-visible namespaces to their minimum defaults.
977
977
978 Certain history lists are also initialized here, as they effectively
978 Certain history lists are also initialized here, as they effectively
979 act as user namespaces.
979 act as user namespaces.
980
980
981 Notes
981 Notes
982 -----
982 -----
983 All data structures here are only filled in, they are NOT reset by this
983 All data structures here are only filled in, they are NOT reset by this
984 method. If they were not empty before, data will simply be added to
984 method. If they were not empty before, data will simply be added to
985 therm.
985 therm.
986 """
986 """
987 # This function works in two parts: first we put a few things in
987 # This function works in two parts: first we put a few things in
988 # user_ns, and we sync that contents into user_ns_hidden so that these
988 # user_ns, and we sync that contents into user_ns_hidden so that these
989 # initial variables aren't shown by %who. After the sync, we add the
989 # initial variables aren't shown by %who. After the sync, we add the
990 # rest of what we *do* want the user to see with %who even on a new
990 # rest of what we *do* want the user to see with %who even on a new
991 # session (probably nothing, so theye really only see their own stuff)
991 # session (probably nothing, so theye really only see their own stuff)
992
992
993 # The user dict must *always* have a __builtin__ reference to the
993 # The user dict must *always* have a __builtin__ reference to the
994 # Python standard __builtin__ namespace, which must be imported.
994 # Python standard __builtin__ namespace, which must be imported.
995 # This is so that certain operations in prompt evaluation can be
995 # This is so that certain operations in prompt evaluation can be
996 # reliably executed with builtins. Note that we can NOT use
996 # reliably executed with builtins. Note that we can NOT use
997 # __builtins__ (note the 's'), because that can either be a dict or a
997 # __builtins__ (note the 's'), because that can either be a dict or a
998 # module, and can even mutate at runtime, depending on the context
998 # module, and can even mutate at runtime, depending on the context
999 # (Python makes no guarantees on it). In contrast, __builtin__ is
999 # (Python makes no guarantees on it). In contrast, __builtin__ is
1000 # always a module object, though it must be explicitly imported.
1000 # always a module object, though it must be explicitly imported.
1001
1001
1002 # For more details:
1002 # For more details:
1003 # http://mail.python.org/pipermail/python-dev/2001-April/014068.html
1003 # http://mail.python.org/pipermail/python-dev/2001-April/014068.html
1004 ns = dict(__builtin__ = __builtin__)
1004 ns = dict(__builtin__ = __builtin__)
1005
1005
1006 # Put 'help' in the user namespace
1006 # Put 'help' in the user namespace
1007 try:
1007 try:
1008 from site import _Helper
1008 from site import _Helper
1009 ns['help'] = _Helper()
1009 ns['help'] = _Helper()
1010 except ImportError:
1010 except ImportError:
1011 warn('help() not available - check site.py')
1011 warn('help() not available - check site.py')
1012
1012
1013 # make global variables for user access to the histories
1013 # make global variables for user access to the histories
1014 ns['_ih'] = self.history_manager.input_hist_parsed
1014 ns['_ih'] = self.history_manager.input_hist_parsed
1015 ns['_oh'] = self.history_manager.output_hist
1015 ns['_oh'] = self.history_manager.output_hist
1016 ns['_dh'] = self.history_manager.dir_hist
1016 ns['_dh'] = self.history_manager.dir_hist
1017
1017
1018 ns['_sh'] = shadowns
1018 ns['_sh'] = shadowns
1019
1019
1020 # user aliases to input and output histories. These shouldn't show up
1020 # user aliases to input and output histories. These shouldn't show up
1021 # in %who, as they can have very large reprs.
1021 # in %who, as they can have very large reprs.
1022 ns['In'] = self.history_manager.input_hist_parsed
1022 ns['In'] = self.history_manager.input_hist_parsed
1023 ns['Out'] = self.history_manager.output_hist
1023 ns['Out'] = self.history_manager.output_hist
1024
1024
1025 # Store myself as the public api!!!
1025 # Store myself as the public api!!!
1026 ns['get_ipython'] = self.get_ipython
1026 ns['get_ipython'] = self.get_ipython
1027
1027
1028 ns['exit'] = self.exiter
1028 ns['exit'] = self.exiter
1029 ns['quit'] = self.exiter
1029 ns['quit'] = self.exiter
1030
1030
1031 # Sync what we've added so far to user_ns_hidden so these aren't seen
1031 # Sync what we've added so far to user_ns_hidden so these aren't seen
1032 # by %who
1032 # by %who
1033 self.user_ns_hidden.update(ns)
1033 self.user_ns_hidden.update(ns)
1034
1034
1035 # Anything put into ns now would show up in %who. Think twice before
1035 # Anything put into ns now would show up in %who. Think twice before
1036 # putting anything here, as we really want %who to show the user their
1036 # putting anything here, as we really want %who to show the user their
1037 # stuff, not our variables.
1037 # stuff, not our variables.
1038
1038
1039 # Finally, update the real user's namespace
1039 # Finally, update the real user's namespace
1040 self.user_ns.update(ns)
1040 self.user_ns.update(ns)
1041
1041
1042 def reset(self, new_session=True):
1042 def reset(self, new_session=True):
1043 """Clear all internal namespaces.
1043 """Clear all internal namespaces.
1044
1044
1045 Note that this is much more aggressive than %reset, since it clears
1045 Note that this is much more aggressive than %reset, since it clears
1046 fully all namespaces, as well as all input/output lists.
1046 fully all namespaces, as well as all input/output lists.
1047
1047
1048 If new_session is True, a new history session will be opened.
1048 If new_session is True, a new history session will be opened.
1049 """
1049 """
1050 # Clear histories
1050 # Clear histories
1051 self.history_manager.reset(new_session)
1051 self.history_manager.reset(new_session)
1052 # Reset counter used to index all histories
1052 # Reset counter used to index all histories
1053 if new_session:
1053 if new_session:
1054 self.execution_count = 1
1054 self.execution_count = 1
1055
1055
1056 # Flush cached output items
1056 # Flush cached output items
1057 self.displayhook.flush()
1057 self.displayhook.flush()
1058
1058
1059 # Restore the user namespaces to minimal usability
1059 # Restore the user namespaces to minimal usability
1060 for ns in self.ns_refs_table:
1060 for ns in self.ns_refs_table:
1061 ns.clear()
1061 ns.clear()
1062
1062
1063 # The main execution namespaces must be cleared very carefully,
1063 # The main execution namespaces must be cleared very carefully,
1064 # skipping the deletion of the builtin-related keys, because doing so
1064 # skipping the deletion of the builtin-related keys, because doing so
1065 # would cause errors in many object's __del__ methods.
1065 # would cause errors in many object's __del__ methods.
1066 for ns in [self.user_ns, self.user_global_ns]:
1066 for ns in [self.user_ns, self.user_global_ns]:
1067 drop_keys = set(ns.keys())
1067 drop_keys = set(ns.keys())
1068 drop_keys.discard('__builtin__')
1068 drop_keys.discard('__builtin__')
1069 drop_keys.discard('__builtins__')
1069 drop_keys.discard('__builtins__')
1070 for k in drop_keys:
1070 for k in drop_keys:
1071 del ns[k]
1071 del ns[k]
1072
1072
1073 # Restore the user namespaces to minimal usability
1073 # Restore the user namespaces to minimal usability
1074 self.init_user_ns()
1074 self.init_user_ns()
1075
1075
1076 # Restore the default and user aliases
1076 # Restore the default and user aliases
1077 self.alias_manager.clear_aliases()
1077 self.alias_manager.clear_aliases()
1078 self.alias_manager.init_aliases()
1078 self.alias_manager.init_aliases()
1079
1079
1080 # Flush the private list of module references kept for script
1080 # Flush the private list of module references kept for script
1081 # execution protection
1081 # execution protection
1082 self.clear_main_mod_cache()
1082 self.clear_main_mod_cache()
1083
1083
1084 def reset_selective(self, regex=None):
1084 def reset_selective(self, regex=None):
1085 """Clear selective variables from internal namespaces based on a
1085 """Clear selective variables from internal namespaces based on a
1086 specified regular expression.
1086 specified regular expression.
1087
1087
1088 Parameters
1088 Parameters
1089 ----------
1089 ----------
1090 regex : string or compiled pattern, optional
1090 regex : string or compiled pattern, optional
1091 A regular expression pattern that will be used in searching
1091 A regular expression pattern that will be used in searching
1092 variable names in the users namespaces.
1092 variable names in the users namespaces.
1093 """
1093 """
1094 if regex is not None:
1094 if regex is not None:
1095 try:
1095 try:
1096 m = re.compile(regex)
1096 m = re.compile(regex)
1097 except TypeError:
1097 except TypeError:
1098 raise TypeError('regex must be a string or compiled pattern')
1098 raise TypeError('regex must be a string or compiled pattern')
1099 # Search for keys in each namespace that match the given regex
1099 # Search for keys in each namespace that match the given regex
1100 # If a match is found, delete the key/value pair.
1100 # If a match is found, delete the key/value pair.
1101 for ns in self.ns_refs_table:
1101 for ns in self.ns_refs_table:
1102 for var in ns:
1102 for var in ns:
1103 if m.search(var):
1103 if m.search(var):
1104 del ns[var]
1104 del ns[var]
1105
1105
1106 def push(self, variables, interactive=True):
1106 def push(self, variables, interactive=True):
1107 """Inject a group of variables into the IPython user namespace.
1107 """Inject a group of variables into the IPython user namespace.
1108
1108
1109 Parameters
1109 Parameters
1110 ----------
1110 ----------
1111 variables : dict, str or list/tuple of str
1111 variables : dict, str or list/tuple of str
1112 The variables to inject into the user's namespace. If a dict, a
1112 The variables to inject into the user's namespace. If a dict, a
1113 simple update is done. If a str, the string is assumed to have
1113 simple update is done. If a str, the string is assumed to have
1114 variable names separated by spaces. A list/tuple of str can also
1114 variable names separated by spaces. A list/tuple of str can also
1115 be used to give the variable names. If just the variable names are
1115 be used to give the variable names. If just the variable names are
1116 give (list/tuple/str) then the variable values looked up in the
1116 give (list/tuple/str) then the variable values looked up in the
1117 callers frame.
1117 callers frame.
1118 interactive : bool
1118 interactive : bool
1119 If True (default), the variables will be listed with the ``who``
1119 If True (default), the variables will be listed with the ``who``
1120 magic.
1120 magic.
1121 """
1121 """
1122 vdict = None
1122 vdict = None
1123
1123
1124 # We need a dict of name/value pairs to do namespace updates.
1124 # We need a dict of name/value pairs to do namespace updates.
1125 if isinstance(variables, dict):
1125 if isinstance(variables, dict):
1126 vdict = variables
1126 vdict = variables
1127 elif isinstance(variables, (basestring, list, tuple)):
1127 elif isinstance(variables, (basestring, list, tuple)):
1128 if isinstance(variables, basestring):
1128 if isinstance(variables, basestring):
1129 vlist = variables.split()
1129 vlist = variables.split()
1130 else:
1130 else:
1131 vlist = variables
1131 vlist = variables
1132 vdict = {}
1132 vdict = {}
1133 cf = sys._getframe(1)
1133 cf = sys._getframe(1)
1134 for name in vlist:
1134 for name in vlist:
1135 try:
1135 try:
1136 vdict[name] = eval(name, cf.f_globals, cf.f_locals)
1136 vdict[name] = eval(name, cf.f_globals, cf.f_locals)
1137 except:
1137 except:
1138 print ('Could not get variable %s from %s' %
1138 print ('Could not get variable %s from %s' %
1139 (name,cf.f_code.co_name))
1139 (name,cf.f_code.co_name))
1140 else:
1140 else:
1141 raise ValueError('variables must be a dict/str/list/tuple')
1141 raise ValueError('variables must be a dict/str/list/tuple')
1142
1142
1143 # Propagate variables to user namespace
1143 # Propagate variables to user namespace
1144 self.user_ns.update(vdict)
1144 self.user_ns.update(vdict)
1145
1145
1146 # And configure interactive visibility
1146 # And configure interactive visibility
1147 config_ns = self.user_ns_hidden
1147 config_ns = self.user_ns_hidden
1148 if interactive:
1148 if interactive:
1149 for name, val in vdict.iteritems():
1149 for name, val in vdict.iteritems():
1150 config_ns.pop(name, None)
1150 config_ns.pop(name, None)
1151 else:
1151 else:
1152 for name,val in vdict.iteritems():
1152 for name,val in vdict.iteritems():
1153 config_ns[name] = val
1153 config_ns[name] = val
1154
1154
1155 #-------------------------------------------------------------------------
1155 #-------------------------------------------------------------------------
1156 # Things related to object introspection
1156 # Things related to object introspection
1157 #-------------------------------------------------------------------------
1157 #-------------------------------------------------------------------------
1158
1158
1159 def _ofind(self, oname, namespaces=None):
1159 def _ofind(self, oname, namespaces=None):
1160 """Find an object in the available namespaces.
1160 """Find an object in the available namespaces.
1161
1161
1162 self._ofind(oname) -> dict with keys: found,obj,ospace,ismagic
1162 self._ofind(oname) -> dict with keys: found,obj,ospace,ismagic
1163
1163
1164 Has special code to detect magic functions.
1164 Has special code to detect magic functions.
1165 """
1165 """
1166 #oname = oname.strip()
1166 #oname = oname.strip()
1167 #print '1- oname: <%r>' % oname # dbg
1167 #print '1- oname: <%r>' % oname # dbg
1168 try:
1168 try:
1169 oname = oname.strip().encode('ascii')
1169 oname = oname.strip().encode('ascii')
1170 #print '2- oname: <%r>' % oname # dbg
1170 #print '2- oname: <%r>' % oname # dbg
1171 except UnicodeEncodeError:
1171 except UnicodeEncodeError:
1172 print 'Python identifiers can only contain ascii characters.'
1172 print 'Python identifiers can only contain ascii characters.'
1173 return dict(found=False)
1173 return dict(found=False)
1174
1174
1175 alias_ns = None
1175 alias_ns = None
1176 if namespaces is None:
1176 if namespaces is None:
1177 # Namespaces to search in:
1177 # Namespaces to search in:
1178 # Put them in a list. The order is important so that we
1178 # Put them in a list. The order is important so that we
1179 # find things in the same order that Python finds them.
1179 # find things in the same order that Python finds them.
1180 namespaces = [ ('Interactive', self.user_ns),
1180 namespaces = [ ('Interactive', self.user_ns),
1181 ('IPython internal', self.internal_ns),
1181 ('IPython internal', self.internal_ns),
1182 ('Python builtin', __builtin__.__dict__),
1182 ('Python builtin', __builtin__.__dict__),
1183 ('Alias', self.alias_manager.alias_table),
1183 ('Alias', self.alias_manager.alias_table),
1184 ]
1184 ]
1185 alias_ns = self.alias_manager.alias_table
1185 alias_ns = self.alias_manager.alias_table
1186
1186
1187 # initialize results to 'null'
1187 # initialize results to 'null'
1188 found = False; obj = None; ospace = None; ds = None;
1188 found = False; obj = None; ospace = None; ds = None;
1189 ismagic = False; isalias = False; parent = None
1189 ismagic = False; isalias = False; parent = None
1190
1190
1191 # We need to special-case 'print', which as of python2.6 registers as a
1191 # We need to special-case 'print', which as of python2.6 registers as a
1192 # function but should only be treated as one if print_function was
1192 # function but should only be treated as one if print_function was
1193 # loaded with a future import. In this case, just bail.
1193 # loaded with a future import. In this case, just bail.
1194 if (oname == 'print' and not (self.compile.compiler_flags &
1194 if (oname == 'print' and not (self.compile.compiler_flags &
1195 __future__.CO_FUTURE_PRINT_FUNCTION)):
1195 __future__.CO_FUTURE_PRINT_FUNCTION)):
1196 return {'found':found, 'obj':obj, 'namespace':ospace,
1196 return {'found':found, 'obj':obj, 'namespace':ospace,
1197 'ismagic':ismagic, 'isalias':isalias, 'parent':parent}
1197 'ismagic':ismagic, 'isalias':isalias, 'parent':parent}
1198
1198
1199 # Look for the given name by splitting it in parts. If the head is
1199 # Look for the given name by splitting it in parts. If the head is
1200 # found, then we look for all the remaining parts as members, and only
1200 # found, then we look for all the remaining parts as members, and only
1201 # declare success if we can find them all.
1201 # declare success if we can find them all.
1202 oname_parts = oname.split('.')
1202 oname_parts = oname.split('.')
1203 oname_head, oname_rest = oname_parts[0],oname_parts[1:]
1203 oname_head, oname_rest = oname_parts[0],oname_parts[1:]
1204 for nsname,ns in namespaces:
1204 for nsname,ns in namespaces:
1205 try:
1205 try:
1206 obj = ns[oname_head]
1206 obj = ns[oname_head]
1207 except KeyError:
1207 except KeyError:
1208 continue
1208 continue
1209 else:
1209 else:
1210 #print 'oname_rest:', oname_rest # dbg
1210 #print 'oname_rest:', oname_rest # dbg
1211 for part in oname_rest:
1211 for part in oname_rest:
1212 try:
1212 try:
1213 parent = obj
1213 parent = obj
1214 obj = getattr(obj,part)
1214 obj = getattr(obj,part)
1215 except:
1215 except:
1216 # Blanket except b/c some badly implemented objects
1216 # Blanket except b/c some badly implemented objects
1217 # allow __getattr__ to raise exceptions other than
1217 # allow __getattr__ to raise exceptions other than
1218 # AttributeError, which then crashes IPython.
1218 # AttributeError, which then crashes IPython.
1219 break
1219 break
1220 else:
1220 else:
1221 # If we finish the for loop (no break), we got all members
1221 # If we finish the for loop (no break), we got all members
1222 found = True
1222 found = True
1223 ospace = nsname
1223 ospace = nsname
1224 if ns == alias_ns:
1224 if ns == alias_ns:
1225 isalias = True
1225 isalias = True
1226 break # namespace loop
1226 break # namespace loop
1227
1227
1228 # Try to see if it's magic
1228 # Try to see if it's magic
1229 if not found:
1229 if not found:
1230 if oname.startswith(ESC_MAGIC):
1230 if oname.startswith(ESC_MAGIC):
1231 oname = oname[1:]
1231 oname = oname[1:]
1232 obj = getattr(self,'magic_'+oname,None)
1232 obj = getattr(self,'magic_'+oname,None)
1233 if obj is not None:
1233 if obj is not None:
1234 found = True
1234 found = True
1235 ospace = 'IPython internal'
1235 ospace = 'IPython internal'
1236 ismagic = True
1236 ismagic = True
1237
1237
1238 # Last try: special-case some literals like '', [], {}, etc:
1238 # Last try: special-case some literals like '', [], {}, etc:
1239 if not found and oname_head in ["''",'""','[]','{}','()']:
1239 if not found and oname_head in ["''",'""','[]','{}','()']:
1240 obj = eval(oname_head)
1240 obj = eval(oname_head)
1241 found = True
1241 found = True
1242 ospace = 'Interactive'
1242 ospace = 'Interactive'
1243
1243
1244 return {'found':found, 'obj':obj, 'namespace':ospace,
1244 return {'found':found, 'obj':obj, 'namespace':ospace,
1245 'ismagic':ismagic, 'isalias':isalias, 'parent':parent}
1245 'ismagic':ismagic, 'isalias':isalias, 'parent':parent}
1246
1246
1247 def _ofind_property(self, oname, info):
1247 def _ofind_property(self, oname, info):
1248 """Second part of object finding, to look for property details."""
1248 """Second part of object finding, to look for property details."""
1249 if info.found:
1249 if info.found:
1250 # Get the docstring of the class property if it exists.
1250 # Get the docstring of the class property if it exists.
1251 path = oname.split('.')
1251 path = oname.split('.')
1252 root = '.'.join(path[:-1])
1252 root = '.'.join(path[:-1])
1253 if info.parent is not None:
1253 if info.parent is not None:
1254 try:
1254 try:
1255 target = getattr(info.parent, '__class__')
1255 target = getattr(info.parent, '__class__')
1256 # The object belongs to a class instance.
1256 # The object belongs to a class instance.
1257 try:
1257 try:
1258 target = getattr(target, path[-1])
1258 target = getattr(target, path[-1])
1259 # The class defines the object.
1259 # The class defines the object.
1260 if isinstance(target, property):
1260 if isinstance(target, property):
1261 oname = root + '.__class__.' + path[-1]
1261 oname = root + '.__class__.' + path[-1]
1262 info = Struct(self._ofind(oname))
1262 info = Struct(self._ofind(oname))
1263 except AttributeError: pass
1263 except AttributeError: pass
1264 except AttributeError: pass
1264 except AttributeError: pass
1265
1265
1266 # We return either the new info or the unmodified input if the object
1266 # We return either the new info or the unmodified input if the object
1267 # hadn't been found
1267 # hadn't been found
1268 return info
1268 return info
1269
1269
1270 def _object_find(self, oname, namespaces=None):
1270 def _object_find(self, oname, namespaces=None):
1271 """Find an object and return a struct with info about it."""
1271 """Find an object and return a struct with info about it."""
1272 inf = Struct(self._ofind(oname, namespaces))
1272 inf = Struct(self._ofind(oname, namespaces))
1273 return Struct(self._ofind_property(oname, inf))
1273 return Struct(self._ofind_property(oname, inf))
1274
1274
1275 def _inspect(self, meth, oname, namespaces=None, **kw):
1275 def _inspect(self, meth, oname, namespaces=None, **kw):
1276 """Generic interface to the inspector system.
1276 """Generic interface to the inspector system.
1277
1277
1278 This function is meant to be called by pdef, pdoc & friends."""
1278 This function is meant to be called by pdef, pdoc & friends."""
1279 info = self._object_find(oname)
1279 info = self._object_find(oname)
1280 if info.found:
1280 if info.found:
1281 pmethod = getattr(self.inspector, meth)
1281 pmethod = getattr(self.inspector, meth)
1282 formatter = format_screen if info.ismagic else None
1282 formatter = format_screen if info.ismagic else None
1283 if meth == 'pdoc':
1283 if meth == 'pdoc':
1284 pmethod(info.obj, oname, formatter)
1284 pmethod(info.obj, oname, formatter)
1285 elif meth == 'pinfo':
1285 elif meth == 'pinfo':
1286 pmethod(info.obj, oname, formatter, info, **kw)
1286 pmethod(info.obj, oname, formatter, info, **kw)
1287 else:
1287 else:
1288 pmethod(info.obj, oname)
1288 pmethod(info.obj, oname)
1289 else:
1289 else:
1290 print 'Object `%s` not found.' % oname
1290 print 'Object `%s` not found.' % oname
1291 return 'not found' # so callers can take other action
1291 return 'not found' # so callers can take other action
1292
1292
1293 def object_inspect(self, oname):
1293 def object_inspect(self, oname):
1294 with self.builtin_trap:
1294 with self.builtin_trap:
1295 info = self._object_find(oname)
1295 info = self._object_find(oname)
1296 if info.found:
1296 if info.found:
1297 return self.inspector.info(info.obj, oname, info=info)
1297 return self.inspector.info(info.obj, oname, info=info)
1298 else:
1298 else:
1299 return oinspect.object_info(name=oname, found=False)
1299 return oinspect.object_info(name=oname, found=False)
1300
1300
1301 #-------------------------------------------------------------------------
1301 #-------------------------------------------------------------------------
1302 # Things related to history management
1302 # Things related to history management
1303 #-------------------------------------------------------------------------
1303 #-------------------------------------------------------------------------
1304
1304
1305 def init_history(self):
1305 def init_history(self):
1306 """Sets up the command history, and starts regular autosaves."""
1306 """Sets up the command history, and starts regular autosaves."""
1307 self.history_manager = HistoryManager(shell=self, config=self.config)
1307 self.history_manager = HistoryManager(shell=self, config=self.config)
1308
1308
1309 #-------------------------------------------------------------------------
1309 #-------------------------------------------------------------------------
1310 # Things related to exception handling and tracebacks (not debugging)
1310 # Things related to exception handling and tracebacks (not debugging)
1311 #-------------------------------------------------------------------------
1311 #-------------------------------------------------------------------------
1312
1312
1313 def init_traceback_handlers(self, custom_exceptions):
1313 def init_traceback_handlers(self, custom_exceptions):
1314 # Syntax error handler.
1314 # Syntax error handler.
1315 self.SyntaxTB = ultratb.SyntaxTB(color_scheme='NoColor')
1315 self.SyntaxTB = ultratb.SyntaxTB(color_scheme='NoColor')
1316
1316
1317 # The interactive one is initialized with an offset, meaning we always
1317 # The interactive one is initialized with an offset, meaning we always
1318 # want to remove the topmost item in the traceback, which is our own
1318 # want to remove the topmost item in the traceback, which is our own
1319 # internal code. Valid modes: ['Plain','Context','Verbose']
1319 # internal code. Valid modes: ['Plain','Context','Verbose']
1320 self.InteractiveTB = ultratb.AutoFormattedTB(mode = 'Plain',
1320 self.InteractiveTB = ultratb.AutoFormattedTB(mode = 'Plain',
1321 color_scheme='NoColor',
1321 color_scheme='NoColor',
1322 tb_offset = 1,
1322 tb_offset = 1,
1323 check_cache=self.compile.check_cache)
1323 check_cache=self.compile.check_cache)
1324
1324
1325 # The instance will store a pointer to the system-wide exception hook,
1325 # The instance will store a pointer to the system-wide exception hook,
1326 # so that runtime code (such as magics) can access it. This is because
1326 # so that runtime code (such as magics) can access it. This is because
1327 # during the read-eval loop, it may get temporarily overwritten.
1327 # during the read-eval loop, it may get temporarily overwritten.
1328 self.sys_excepthook = sys.excepthook
1328 self.sys_excepthook = sys.excepthook
1329
1329
1330 # and add any custom exception handlers the user may have specified
1330 # and add any custom exception handlers the user may have specified
1331 self.set_custom_exc(*custom_exceptions)
1331 self.set_custom_exc(*custom_exceptions)
1332
1332
1333 # Set the exception mode
1333 # Set the exception mode
1334 self.InteractiveTB.set_mode(mode=self.xmode)
1334 self.InteractiveTB.set_mode(mode=self.xmode)
1335
1335
1336 def set_custom_exc(self, exc_tuple, handler):
1336 def set_custom_exc(self, exc_tuple, handler):
1337 """set_custom_exc(exc_tuple,handler)
1337 """set_custom_exc(exc_tuple,handler)
1338
1338
1339 Set a custom exception handler, which will be called if any of the
1339 Set a custom exception handler, which will be called if any of the
1340 exceptions in exc_tuple occur in the mainloop (specifically, in the
1340 exceptions in exc_tuple occur in the mainloop (specifically, in the
1341 run_code() method.
1341 run_code() method.
1342
1342
1343 Inputs:
1343 Inputs:
1344
1344
1345 - exc_tuple: a *tuple* of valid exceptions to call the defined
1345 - exc_tuple: a *tuple* of valid exceptions to call the defined
1346 handler for. It is very important that you use a tuple, and NOT A
1346 handler for. It is very important that you use a tuple, and NOT A
1347 LIST here, because of the way Python's except statement works. If
1347 LIST here, because of the way Python's except statement works. If
1348 you only want to trap a single exception, use a singleton tuple:
1348 you only want to trap a single exception, use a singleton tuple:
1349
1349
1350 exc_tuple == (MyCustomException,)
1350 exc_tuple == (MyCustomException,)
1351
1351
1352 - handler: this must be defined as a function with the following
1352 - handler: this must be defined as a function with the following
1353 basic interface::
1353 basic interface::
1354
1354
1355 def my_handler(self, etype, value, tb, tb_offset=None)
1355 def my_handler(self, etype, value, tb, tb_offset=None)
1356 ...
1356 ...
1357 # The return value must be
1357 # The return value must be
1358 return structured_traceback
1358 return structured_traceback
1359
1359
1360 This will be made into an instance method (via types.MethodType)
1360 This will be made into an instance method (via types.MethodType)
1361 of IPython itself, and it will be called if any of the exceptions
1361 of IPython itself, and it will be called if any of the exceptions
1362 listed in the exc_tuple are caught. If the handler is None, an
1362 listed in the exc_tuple are caught. If the handler is None, an
1363 internal basic one is used, which just prints basic info.
1363 internal basic one is used, which just prints basic info.
1364
1364
1365 WARNING: by putting in your own exception handler into IPython's main
1365 WARNING: by putting in your own exception handler into IPython's main
1366 execution loop, you run a very good chance of nasty crashes. This
1366 execution loop, you run a very good chance of nasty crashes. This
1367 facility should only be used if you really know what you are doing."""
1367 facility should only be used if you really know what you are doing."""
1368
1368
1369 assert type(exc_tuple)==type(()) , \
1369 assert type(exc_tuple)==type(()) , \
1370 "The custom exceptions must be given AS A TUPLE."
1370 "The custom exceptions must be given AS A TUPLE."
1371
1371
1372 def dummy_handler(self,etype,value,tb):
1372 def dummy_handler(self,etype,value,tb):
1373 print '*** Simple custom exception handler ***'
1373 print '*** Simple custom exception handler ***'
1374 print 'Exception type :',etype
1374 print 'Exception type :',etype
1375 print 'Exception value:',value
1375 print 'Exception value:',value
1376 print 'Traceback :',tb
1376 print 'Traceback :',tb
1377 print 'Source code :','\n'.join(self.buffer)
1377 print 'Source code :','\n'.join(self.buffer)
1378
1378
1379 if handler is None: handler = dummy_handler
1379 if handler is None: handler = dummy_handler
1380
1380
1381 self.CustomTB = types.MethodType(handler,self)
1381 self.CustomTB = types.MethodType(handler,self)
1382 self.custom_exceptions = exc_tuple
1382 self.custom_exceptions = exc_tuple
1383
1383
1384 def excepthook(self, etype, value, tb):
1384 def excepthook(self, etype, value, tb):
1385 """One more defense for GUI apps that call sys.excepthook.
1385 """One more defense for GUI apps that call sys.excepthook.
1386
1386
1387 GUI frameworks like wxPython trap exceptions and call
1387 GUI frameworks like wxPython trap exceptions and call
1388 sys.excepthook themselves. I guess this is a feature that
1388 sys.excepthook themselves. I guess this is a feature that
1389 enables them to keep running after exceptions that would
1389 enables them to keep running after exceptions that would
1390 otherwise kill their mainloop. This is a bother for IPython
1390 otherwise kill their mainloop. This is a bother for IPython
1391 which excepts to catch all of the program exceptions with a try:
1391 which excepts to catch all of the program exceptions with a try:
1392 except: statement.
1392 except: statement.
1393
1393
1394 Normally, IPython sets sys.excepthook to a CrashHandler instance, so if
1394 Normally, IPython sets sys.excepthook to a CrashHandler instance, so if
1395 any app directly invokes sys.excepthook, it will look to the user like
1395 any app directly invokes sys.excepthook, it will look to the user like
1396 IPython crashed. In order to work around this, we can disable the
1396 IPython crashed. In order to work around this, we can disable the
1397 CrashHandler and replace it with this excepthook instead, which prints a
1397 CrashHandler and replace it with this excepthook instead, which prints a
1398 regular traceback using our InteractiveTB. In this fashion, apps which
1398 regular traceback using our InteractiveTB. In this fashion, apps which
1399 call sys.excepthook will generate a regular-looking exception from
1399 call sys.excepthook will generate a regular-looking exception from
1400 IPython, and the CrashHandler will only be triggered by real IPython
1400 IPython, and the CrashHandler will only be triggered by real IPython
1401 crashes.
1401 crashes.
1402
1402
1403 This hook should be used sparingly, only in places which are not likely
1403 This hook should be used sparingly, only in places which are not likely
1404 to be true IPython errors.
1404 to be true IPython errors.
1405 """
1405 """
1406 self.showtraceback((etype,value,tb),tb_offset=0)
1406 self.showtraceback((etype,value,tb),tb_offset=0)
1407
1407
1408 def showtraceback(self,exc_tuple = None,filename=None,tb_offset=None,
1408 def showtraceback(self,exc_tuple = None,filename=None,tb_offset=None,
1409 exception_only=False):
1409 exception_only=False):
1410 """Display the exception that just occurred.
1410 """Display the exception that just occurred.
1411
1411
1412 If nothing is known about the exception, this is the method which
1412 If nothing is known about the exception, this is the method which
1413 should be used throughout the code for presenting user tracebacks,
1413 should be used throughout the code for presenting user tracebacks,
1414 rather than directly invoking the InteractiveTB object.
1414 rather than directly invoking the InteractiveTB object.
1415
1415
1416 A specific showsyntaxerror() also exists, but this method can take
1416 A specific showsyntaxerror() also exists, but this method can take
1417 care of calling it if needed, so unless you are explicitly catching a
1417 care of calling it if needed, so unless you are explicitly catching a
1418 SyntaxError exception, don't try to analyze the stack manually and
1418 SyntaxError exception, don't try to analyze the stack manually and
1419 simply call this method."""
1419 simply call this method."""
1420
1420
1421 try:
1421 try:
1422 if exc_tuple is None:
1422 if exc_tuple is None:
1423 etype, value, tb = sys.exc_info()
1423 etype, value, tb = sys.exc_info()
1424 else:
1424 else:
1425 etype, value, tb = exc_tuple
1425 etype, value, tb = exc_tuple
1426
1426
1427 if etype is None:
1427 if etype is None:
1428 if hasattr(sys, 'last_type'):
1428 if hasattr(sys, 'last_type'):
1429 etype, value, tb = sys.last_type, sys.last_value, \
1429 etype, value, tb = sys.last_type, sys.last_value, \
1430 sys.last_traceback
1430 sys.last_traceback
1431 else:
1431 else:
1432 self.write_err('No traceback available to show.\n')
1432 self.write_err('No traceback available to show.\n')
1433 return
1433 return
1434
1434
1435 if etype is SyntaxError:
1435 if etype is SyntaxError:
1436 # Though this won't be called by syntax errors in the input
1436 # Though this won't be called by syntax errors in the input
1437 # line, there may be SyntaxError cases whith imported code.
1437 # line, there may be SyntaxError cases whith imported code.
1438 self.showsyntaxerror(filename)
1438 self.showsyntaxerror(filename)
1439 elif etype is UsageError:
1439 elif etype is UsageError:
1440 print "UsageError:", value
1440 print "UsageError:", value
1441 else:
1441 else:
1442 # WARNING: these variables are somewhat deprecated and not
1442 # WARNING: these variables are somewhat deprecated and not
1443 # necessarily safe to use in a threaded environment, but tools
1443 # necessarily safe to use in a threaded environment, but tools
1444 # like pdb depend on their existence, so let's set them. If we
1444 # like pdb depend on their existence, so let's set them. If we
1445 # find problems in the field, we'll need to revisit their use.
1445 # find problems in the field, we'll need to revisit their use.
1446 sys.last_type = etype
1446 sys.last_type = etype
1447 sys.last_value = value
1447 sys.last_value = value
1448 sys.last_traceback = tb
1448 sys.last_traceback = tb
1449 if etype in self.custom_exceptions:
1449 if etype in self.custom_exceptions:
1450 # FIXME: Old custom traceback objects may just return a
1450 # FIXME: Old custom traceback objects may just return a
1451 # string, in that case we just put it into a list
1451 # string, in that case we just put it into a list
1452 stb = self.CustomTB(etype, value, tb, tb_offset)
1452 stb = self.CustomTB(etype, value, tb, tb_offset)
1453 if isinstance(ctb, basestring):
1453 if isinstance(ctb, basestring):
1454 stb = [stb]
1454 stb = [stb]
1455 else:
1455 else:
1456 if exception_only:
1456 if exception_only:
1457 stb = ['An exception has occurred, use %tb to see '
1457 stb = ['An exception has occurred, use %tb to see '
1458 'the full traceback.\n']
1458 'the full traceback.\n']
1459 stb.extend(self.InteractiveTB.get_exception_only(etype,
1459 stb.extend(self.InteractiveTB.get_exception_only(etype,
1460 value))
1460 value))
1461 else:
1461 else:
1462 stb = self.InteractiveTB.structured_traceback(etype,
1462 stb = self.InteractiveTB.structured_traceback(etype,
1463 value, tb, tb_offset=tb_offset)
1463 value, tb, tb_offset=tb_offset)
1464
1464
1465 if self.call_pdb:
1465 if self.call_pdb:
1466 # drop into debugger
1466 # drop into debugger
1467 self.debugger(force=True)
1467 self.debugger(force=True)
1468
1468
1469 # Actually show the traceback
1469 # Actually show the traceback
1470 self._showtraceback(etype, value, stb)
1470 self._showtraceback(etype, value, stb)
1471
1471
1472 except KeyboardInterrupt:
1472 except KeyboardInterrupt:
1473 self.write_err("\nKeyboardInterrupt\n")
1473 self.write_err("\nKeyboardInterrupt\n")
1474
1474
1475 def _showtraceback(self, etype, evalue, stb):
1475 def _showtraceback(self, etype, evalue, stb):
1476 """Actually show a traceback.
1476 """Actually show a traceback.
1477
1477
1478 Subclasses may override this method to put the traceback on a different
1478 Subclasses may override this method to put the traceback on a different
1479 place, like a side channel.
1479 place, like a side channel.
1480 """
1480 """
1481 print >> io.Term.cout, self.InteractiveTB.stb2text(stb)
1481 print >> io.Term.cout, self.InteractiveTB.stb2text(stb)
1482
1482
1483 def showsyntaxerror(self, filename=None):
1483 def showsyntaxerror(self, filename=None):
1484 """Display the syntax error that just occurred.
1484 """Display the syntax error that just occurred.
1485
1485
1486 This doesn't display a stack trace because there isn't one.
1486 This doesn't display a stack trace because there isn't one.
1487
1487
1488 If a filename is given, it is stuffed in the exception instead
1488 If a filename is given, it is stuffed in the exception instead
1489 of what was there before (because Python's parser always uses
1489 of what was there before (because Python's parser always uses
1490 "<string>" when reading from a string).
1490 "<string>" when reading from a string).
1491 """
1491 """
1492 etype, value, last_traceback = sys.exc_info()
1492 etype, value, last_traceback = sys.exc_info()
1493
1493
1494 # See note about these variables in showtraceback() above
1494 # See note about these variables in showtraceback() above
1495 sys.last_type = etype
1495 sys.last_type = etype
1496 sys.last_value = value
1496 sys.last_value = value
1497 sys.last_traceback = last_traceback
1497 sys.last_traceback = last_traceback
1498
1498
1499 if filename and etype is SyntaxError:
1499 if filename and etype is SyntaxError:
1500 # Work hard to stuff the correct filename in the exception
1500 # Work hard to stuff the correct filename in the exception
1501 try:
1501 try:
1502 msg, (dummy_filename, lineno, offset, line) = value
1502 msg, (dummy_filename, lineno, offset, line) = value
1503 except:
1503 except:
1504 # Not the format we expect; leave it alone
1504 # Not the format we expect; leave it alone
1505 pass
1505 pass
1506 else:
1506 else:
1507 # Stuff in the right filename
1507 # Stuff in the right filename
1508 try:
1508 try:
1509 # Assume SyntaxError is a class exception
1509 # Assume SyntaxError is a class exception
1510 value = SyntaxError(msg, (filename, lineno, offset, line))
1510 value = SyntaxError(msg, (filename, lineno, offset, line))
1511 except:
1511 except:
1512 # If that failed, assume SyntaxError is a string
1512 # If that failed, assume SyntaxError is a string
1513 value = msg, (filename, lineno, offset, line)
1513 value = msg, (filename, lineno, offset, line)
1514 stb = self.SyntaxTB.structured_traceback(etype, value, [])
1514 stb = self.SyntaxTB.structured_traceback(etype, value, [])
1515 self._showtraceback(etype, value, stb)
1515 self._showtraceback(etype, value, stb)
1516
1516
1517 #-------------------------------------------------------------------------
1517 #-------------------------------------------------------------------------
1518 # Things related to readline
1518 # Things related to readline
1519 #-------------------------------------------------------------------------
1519 #-------------------------------------------------------------------------
1520
1520
1521 def init_readline(self):
1521 def init_readline(self):
1522 """Command history completion/saving/reloading."""
1522 """Command history completion/saving/reloading."""
1523
1523
1524 if self.readline_use:
1524 if self.readline_use:
1525 import IPython.utils.rlineimpl as readline
1525 import IPython.utils.rlineimpl as readline
1526
1526
1527 self.rl_next_input = None
1527 self.rl_next_input = None
1528 self.rl_do_indent = False
1528 self.rl_do_indent = False
1529
1529
1530 if not self.readline_use or not readline.have_readline:
1530 if not self.readline_use or not readline.have_readline:
1531 self.has_readline = False
1531 self.has_readline = False
1532 self.readline = None
1532 self.readline = None
1533 # Set a number of methods that depend on readline to be no-op
1533 # Set a number of methods that depend on readline to be no-op
1534 self.set_readline_completer = no_op
1534 self.set_readline_completer = no_op
1535 self.set_custom_completer = no_op
1535 self.set_custom_completer = no_op
1536 self.set_completer_frame = no_op
1536 self.set_completer_frame = no_op
1537 warn('Readline services not available or not loaded.')
1537 warn('Readline services not available or not loaded.')
1538 else:
1538 else:
1539 self.has_readline = True
1539 self.has_readline = True
1540 self.readline = readline
1540 self.readline = readline
1541 sys.modules['readline'] = readline
1541 sys.modules['readline'] = readline
1542
1542
1543 # Platform-specific configuration
1543 # Platform-specific configuration
1544 if os.name == 'nt':
1544 if os.name == 'nt':
1545 # FIXME - check with Frederick to see if we can harmonize
1545 # FIXME - check with Frederick to see if we can harmonize
1546 # naming conventions with pyreadline to avoid this
1546 # naming conventions with pyreadline to avoid this
1547 # platform-dependent check
1547 # platform-dependent check
1548 self.readline_startup_hook = readline.set_pre_input_hook
1548 self.readline_startup_hook = readline.set_pre_input_hook
1549 else:
1549 else:
1550 self.readline_startup_hook = readline.set_startup_hook
1550 self.readline_startup_hook = readline.set_startup_hook
1551
1551
1552 # Load user's initrc file (readline config)
1552 # Load user's initrc file (readline config)
1553 # Or if libedit is used, load editrc.
1553 # Or if libedit is used, load editrc.
1554 inputrc_name = os.environ.get('INPUTRC')
1554 inputrc_name = os.environ.get('INPUTRC')
1555 if inputrc_name is None:
1555 if inputrc_name is None:
1556 home_dir = get_home_dir()
1556 home_dir = get_home_dir()
1557 if home_dir is not None:
1557 if home_dir is not None:
1558 inputrc_name = '.inputrc'
1558 inputrc_name = '.inputrc'
1559 if readline.uses_libedit:
1559 if readline.uses_libedit:
1560 inputrc_name = '.editrc'
1560 inputrc_name = '.editrc'
1561 inputrc_name = os.path.join(home_dir, inputrc_name)
1561 inputrc_name = os.path.join(home_dir, inputrc_name)
1562 if os.path.isfile(inputrc_name):
1562 if os.path.isfile(inputrc_name):
1563 try:
1563 try:
1564 readline.read_init_file(inputrc_name)
1564 readline.read_init_file(inputrc_name)
1565 except:
1565 except:
1566 warn('Problems reading readline initialization file <%s>'
1566 warn('Problems reading readline initialization file <%s>'
1567 % inputrc_name)
1567 % inputrc_name)
1568
1568
1569 # Configure readline according to user's prefs
1569 # Configure readline according to user's prefs
1570 # This is only done if GNU readline is being used. If libedit
1570 # This is only done if GNU readline is being used. If libedit
1571 # is being used (as on Leopard) the readline config is
1571 # is being used (as on Leopard) the readline config is
1572 # not run as the syntax for libedit is different.
1572 # not run as the syntax for libedit is different.
1573 if not readline.uses_libedit:
1573 if not readline.uses_libedit:
1574 for rlcommand in self.readline_parse_and_bind:
1574 for rlcommand in self.readline_parse_and_bind:
1575 #print "loading rl:",rlcommand # dbg
1575 #print "loading rl:",rlcommand # dbg
1576 readline.parse_and_bind(rlcommand)
1576 readline.parse_and_bind(rlcommand)
1577
1577
1578 # Remove some chars from the delimiters list. If we encounter
1578 # Remove some chars from the delimiters list. If we encounter
1579 # unicode chars, discard them.
1579 # unicode chars, discard them.
1580 delims = readline.get_completer_delims().encode("ascii", "ignore")
1580 delims = readline.get_completer_delims().encode("ascii", "ignore")
1581 delims = delims.translate(None, self.readline_remove_delims)
1581 delims = delims.translate(None, self.readline_remove_delims)
1582 delims = delims.replace(ESC_MAGIC, '')
1582 delims = delims.replace(ESC_MAGIC, '')
1583 readline.set_completer_delims(delims)
1583 readline.set_completer_delims(delims)
1584 # otherwise we end up with a monster history after a while:
1584 # otherwise we end up with a monster history after a while:
1585 readline.set_history_length(self.history_length)
1585 readline.set_history_length(self.history_length)
1586
1586
1587 self.refill_readline_hist()
1587 self.refill_readline_hist()
1588 self.readline_no_record = ReadlineNoRecord(self)
1588 self.readline_no_record = ReadlineNoRecord(self)
1589
1589
1590 # Configure auto-indent for all platforms
1590 # Configure auto-indent for all platforms
1591 self.set_autoindent(self.autoindent)
1591 self.set_autoindent(self.autoindent)
1592
1592
1593 def refill_readline_hist(self):
1593 def refill_readline_hist(self):
1594 # Load the last 1000 lines from history
1594 # Load the last 1000 lines from history
1595 self.readline.clear_history()
1595 self.readline.clear_history()
1596 stdin_encoding = sys.stdin.encoding or "utf-8"
1596 stdin_encoding = sys.stdin.encoding or "utf-8"
1597 for _, _, cell in self.history_manager.get_tail(1000,
1597 for _, _, cell in self.history_manager.get_tail(1000,
1598 include_latest=True):
1598 include_latest=True):
1599 if cell.strip(): # Ignore blank lines
1599 if cell.strip(): # Ignore blank lines
1600 for line in cell.splitlines():
1600 for line in cell.splitlines():
1601 self.readline.add_history(line.encode(stdin_encoding))
1601 self.readline.add_history(line.encode(stdin_encoding))
1602
1602
1603 def set_next_input(self, s):
1603 def set_next_input(self, s):
1604 """ Sets the 'default' input string for the next command line.
1604 """ Sets the 'default' input string for the next command line.
1605
1605
1606 Requires readline.
1606 Requires readline.
1607
1607
1608 Example:
1608 Example:
1609
1609
1610 [D:\ipython]|1> _ip.set_next_input("Hello Word")
1610 [D:\ipython]|1> _ip.set_next_input("Hello Word")
1611 [D:\ipython]|2> Hello Word_ # cursor is here
1611 [D:\ipython]|2> Hello Word_ # cursor is here
1612 """
1612 """
1613
1613
1614 self.rl_next_input = s
1614 self.rl_next_input = s
1615
1615
1616 # Maybe move this to the terminal subclass?
1616 # Maybe move this to the terminal subclass?
1617 def pre_readline(self):
1617 def pre_readline(self):
1618 """readline hook to be used at the start of each line.
1618 """readline hook to be used at the start of each line.
1619
1619
1620 Currently it handles auto-indent only."""
1620 Currently it handles auto-indent only."""
1621
1621
1622 if self.rl_do_indent:
1622 if self.rl_do_indent:
1623 self.readline.insert_text(self._indent_current_str())
1623 self.readline.insert_text(self._indent_current_str())
1624 if self.rl_next_input is not None:
1624 if self.rl_next_input is not None:
1625 self.readline.insert_text(self.rl_next_input)
1625 self.readline.insert_text(self.rl_next_input)
1626 self.rl_next_input = None
1626 self.rl_next_input = None
1627
1627
1628 def _indent_current_str(self):
1628 def _indent_current_str(self):
1629 """return the current level of indentation as a string"""
1629 """return the current level of indentation as a string"""
1630 return self.input_splitter.indent_spaces * ' '
1630 return self.input_splitter.indent_spaces * ' '
1631
1631
1632 #-------------------------------------------------------------------------
1632 #-------------------------------------------------------------------------
1633 # Things related to text completion
1633 # Things related to text completion
1634 #-------------------------------------------------------------------------
1634 #-------------------------------------------------------------------------
1635
1635
1636 def init_completer(self):
1636 def init_completer(self):
1637 """Initialize the completion machinery.
1637 """Initialize the completion machinery.
1638
1638
1639 This creates completion machinery that can be used by client code,
1639 This creates completion machinery that can be used by client code,
1640 either interactively in-process (typically triggered by the readline
1640 either interactively in-process (typically triggered by the readline
1641 library), programatically (such as in test suites) or out-of-prcess
1641 library), programatically (such as in test suites) or out-of-prcess
1642 (typically over the network by remote frontends).
1642 (typically over the network by remote frontends).
1643 """
1643 """
1644 from IPython.core.completer import IPCompleter
1644 from IPython.core.completer import IPCompleter
1645 from IPython.core.completerlib import (module_completer,
1645 from IPython.core.completerlib import (module_completer,
1646 magic_run_completer, cd_completer)
1646 magic_run_completer, cd_completer)
1647
1647
1648 self.Completer = IPCompleter(self,
1648 self.Completer = IPCompleter(self,
1649 self.user_ns,
1649 self.user_ns,
1650 self.user_global_ns,
1650 self.user_global_ns,
1651 self.readline_omit__names,
1651 self.readline_omit__names,
1652 self.alias_manager.alias_table,
1652 self.alias_manager.alias_table,
1653 self.has_readline)
1653 self.has_readline)
1654
1654
1655 # Add custom completers to the basic ones built into IPCompleter
1655 # Add custom completers to the basic ones built into IPCompleter
1656 sdisp = self.strdispatchers.get('complete_command', StrDispatch())
1656 sdisp = self.strdispatchers.get('complete_command', StrDispatch())
1657 self.strdispatchers['complete_command'] = sdisp
1657 self.strdispatchers['complete_command'] = sdisp
1658 self.Completer.custom_completers = sdisp
1658 self.Completer.custom_completers = sdisp
1659
1659
1660 self.set_hook('complete_command', module_completer, str_key = 'import')
1660 self.set_hook('complete_command', module_completer, str_key = 'import')
1661 self.set_hook('complete_command', module_completer, str_key = 'from')
1661 self.set_hook('complete_command', module_completer, str_key = 'from')
1662 self.set_hook('complete_command', magic_run_completer, str_key = '%run')
1662 self.set_hook('complete_command', magic_run_completer, str_key = '%run')
1663 self.set_hook('complete_command', cd_completer, str_key = '%cd')
1663 self.set_hook('complete_command', cd_completer, str_key = '%cd')
1664
1664
1665 # Only configure readline if we truly are using readline. IPython can
1665 # Only configure readline if we truly are using readline. IPython can
1666 # do tab-completion over the network, in GUIs, etc, where readline
1666 # do tab-completion over the network, in GUIs, etc, where readline
1667 # itself may be absent
1667 # itself may be absent
1668 if self.has_readline:
1668 if self.has_readline:
1669 self.set_readline_completer()
1669 self.set_readline_completer()
1670
1670
1671 def complete(self, text, line=None, cursor_pos=None):
1671 def complete(self, text, line=None, cursor_pos=None):
1672 """Return the completed text and a list of completions.
1672 """Return the completed text and a list of completions.
1673
1673
1674 Parameters
1674 Parameters
1675 ----------
1675 ----------
1676
1676
1677 text : string
1677 text : string
1678 A string of text to be completed on. It can be given as empty and
1678 A string of text to be completed on. It can be given as empty and
1679 instead a line/position pair are given. In this case, the
1679 instead a line/position pair are given. In this case, the
1680 completer itself will split the line like readline does.
1680 completer itself will split the line like readline does.
1681
1681
1682 line : string, optional
1682 line : string, optional
1683 The complete line that text is part of.
1683 The complete line that text is part of.
1684
1684
1685 cursor_pos : int, optional
1685 cursor_pos : int, optional
1686 The position of the cursor on the input line.
1686 The position of the cursor on the input line.
1687
1687
1688 Returns
1688 Returns
1689 -------
1689 -------
1690 text : string
1690 text : string
1691 The actual text that was completed.
1691 The actual text that was completed.
1692
1692
1693 matches : list
1693 matches : list
1694 A sorted list with all possible completions.
1694 A sorted list with all possible completions.
1695
1695
1696 The optional arguments allow the completion to take more context into
1696 The optional arguments allow the completion to take more context into
1697 account, and are part of the low-level completion API.
1697 account, and are part of the low-level completion API.
1698
1698
1699 This is a wrapper around the completion mechanism, similar to what
1699 This is a wrapper around the completion mechanism, similar to what
1700 readline does at the command line when the TAB key is hit. By
1700 readline does at the command line when the TAB key is hit. By
1701 exposing it as a method, it can be used by other non-readline
1701 exposing it as a method, it can be used by other non-readline
1702 environments (such as GUIs) for text completion.
1702 environments (such as GUIs) for text completion.
1703
1703
1704 Simple usage example:
1704 Simple usage example:
1705
1705
1706 In [1]: x = 'hello'
1706 In [1]: x = 'hello'
1707
1707
1708 In [2]: _ip.complete('x.l')
1708 In [2]: _ip.complete('x.l')
1709 Out[2]: ('x.l', ['x.ljust', 'x.lower', 'x.lstrip'])
1709 Out[2]: ('x.l', ['x.ljust', 'x.lower', 'x.lstrip'])
1710 """
1710 """
1711
1711
1712 # Inject names into __builtin__ so we can complete on the added names.
1712 # Inject names into __builtin__ so we can complete on the added names.
1713 with self.builtin_trap:
1713 with self.builtin_trap:
1714 return self.Completer.complete(text, line, cursor_pos)
1714 return self.Completer.complete(text, line, cursor_pos)
1715
1715
1716 def set_custom_completer(self, completer, pos=0):
1716 def set_custom_completer(self, completer, pos=0):
1717 """Adds a new custom completer function.
1717 """Adds a new custom completer function.
1718
1718
1719 The position argument (defaults to 0) is the index in the completers
1719 The position argument (defaults to 0) is the index in the completers
1720 list where you want the completer to be inserted."""
1720 list where you want the completer to be inserted."""
1721
1721
1722 newcomp = types.MethodType(completer,self.Completer)
1722 newcomp = types.MethodType(completer,self.Completer)
1723 self.Completer.matchers.insert(pos,newcomp)
1723 self.Completer.matchers.insert(pos,newcomp)
1724
1724
1725 def set_readline_completer(self):
1725 def set_readline_completer(self):
1726 """Reset readline's completer to be our own."""
1726 """Reset readline's completer to be our own."""
1727 self.readline.set_completer(self.Completer.rlcomplete)
1727 self.readline.set_completer(self.Completer.rlcomplete)
1728
1728
1729 def set_completer_frame(self, frame=None):
1729 def set_completer_frame(self, frame=None):
1730 """Set the frame of the completer."""
1730 """Set the frame of the completer."""
1731 if frame:
1731 if frame:
1732 self.Completer.namespace = frame.f_locals
1732 self.Completer.namespace = frame.f_locals
1733 self.Completer.global_namespace = frame.f_globals
1733 self.Completer.global_namespace = frame.f_globals
1734 else:
1734 else:
1735 self.Completer.namespace = self.user_ns
1735 self.Completer.namespace = self.user_ns
1736 self.Completer.global_namespace = self.user_global_ns
1736 self.Completer.global_namespace = self.user_global_ns
1737
1737
1738 #-------------------------------------------------------------------------
1738 #-------------------------------------------------------------------------
1739 # Things related to magics
1739 # Things related to magics
1740 #-------------------------------------------------------------------------
1740 #-------------------------------------------------------------------------
1741
1741
1742 def init_magics(self):
1742 def init_magics(self):
1743 # FIXME: Move the color initialization to the DisplayHook, which
1743 # FIXME: Move the color initialization to the DisplayHook, which
1744 # should be split into a prompt manager and displayhook. We probably
1744 # should be split into a prompt manager and displayhook. We probably
1745 # even need a centralize colors management object.
1745 # even need a centralize colors management object.
1746 self.magic_colors(self.colors)
1746 self.magic_colors(self.colors)
1747 # History was moved to a separate module
1747 # History was moved to a separate module
1748 from . import history
1748 from . import history
1749 history.init_ipython(self)
1749 history.init_ipython(self)
1750
1750
1751 def magic(self,arg_s):
1751 def magic(self,arg_s):
1752 """Call a magic function by name.
1752 """Call a magic function by name.
1753
1753
1754 Input: a string containing the name of the magic function to call and
1754 Input: a string containing the name of the magic function to call and
1755 any additional arguments to be passed to the magic.
1755 any additional arguments to be passed to the magic.
1756
1756
1757 magic('name -opt foo bar') is equivalent to typing at the ipython
1757 magic('name -opt foo bar') is equivalent to typing at the ipython
1758 prompt:
1758 prompt:
1759
1759
1760 In[1]: %name -opt foo bar
1760 In[1]: %name -opt foo bar
1761
1761
1762 To call a magic without arguments, simply use magic('name').
1762 To call a magic without arguments, simply use magic('name').
1763
1763
1764 This provides a proper Python function to call IPython's magics in any
1764 This provides a proper Python function to call IPython's magics in any
1765 valid Python code you can type at the interpreter, including loops and
1765 valid Python code you can type at the interpreter, including loops and
1766 compound statements.
1766 compound statements.
1767 """
1767 """
1768 args = arg_s.split(' ',1)
1768 args = arg_s.split(' ',1)
1769 magic_name = args[0]
1769 magic_name = args[0]
1770 magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
1770 magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
1771
1771
1772 try:
1772 try:
1773 magic_args = args[1]
1773 magic_args = args[1]
1774 except IndexError:
1774 except IndexError:
1775 magic_args = ''
1775 magic_args = ''
1776 fn = getattr(self,'magic_'+magic_name,None)
1776 fn = getattr(self,'magic_'+magic_name,None)
1777 if fn is None:
1777 if fn is None:
1778 error("Magic function `%s` not found." % magic_name)
1778 error("Magic function `%s` not found." % magic_name)
1779 else:
1779 else:
1780 magic_args = self.var_expand(magic_args,1)
1780 magic_args = self.var_expand(magic_args,1)
1781 # Grab local namespace if we need it:
1781 # Grab local namespace if we need it:
1782 if getattr(fn, "needs_local_scope", False):
1782 if getattr(fn, "needs_local_scope", False):
1783 self._magic_locals = sys._getframe(1).f_locals
1783 self._magic_locals = sys._getframe(1).f_locals
1784 with self.builtin_trap:
1784 with self.builtin_trap:
1785 result = fn(magic_args)
1785 result = fn(magic_args)
1786 # Ensure we're not keeping object references around:
1786 # Ensure we're not keeping object references around:
1787 self._magic_locals = {}
1787 self._magic_locals = {}
1788 return result
1788 return result
1789
1789
1790 def define_magic(self, magicname, func):
1790 def define_magic(self, magicname, func):
1791 """Expose own function as magic function for ipython
1791 """Expose own function as magic function for ipython
1792
1792
1793 def foo_impl(self,parameter_s=''):
1793 def foo_impl(self,parameter_s=''):
1794 'My very own magic!. (Use docstrings, IPython reads them).'
1794 'My very own magic!. (Use docstrings, IPython reads them).'
1795 print 'Magic function. Passed parameter is between < >:'
1795 print 'Magic function. Passed parameter is between < >:'
1796 print '<%s>' % parameter_s
1796 print '<%s>' % parameter_s
1797 print 'The self object is:',self
1797 print 'The self object is:',self
1798
1798
1799 self.define_magic('foo',foo_impl)
1799 self.define_magic('foo',foo_impl)
1800 """
1800 """
1801
1801
1802 import new
1802 import new
1803 im = types.MethodType(func,self)
1803 im = types.MethodType(func,self)
1804 old = getattr(self, "magic_" + magicname, None)
1804 old = getattr(self, "magic_" + magicname, None)
1805 setattr(self, "magic_" + magicname, im)
1805 setattr(self, "magic_" + magicname, im)
1806 return old
1806 return old
1807
1807
1808 #-------------------------------------------------------------------------
1808 #-------------------------------------------------------------------------
1809 # Things related to macros
1809 # Things related to macros
1810 #-------------------------------------------------------------------------
1810 #-------------------------------------------------------------------------
1811
1811
1812 def define_macro(self, name, themacro):
1812 def define_macro(self, name, themacro):
1813 """Define a new macro
1813 """Define a new macro
1814
1814
1815 Parameters
1815 Parameters
1816 ----------
1816 ----------
1817 name : str
1817 name : str
1818 The name of the macro.
1818 The name of the macro.
1819 themacro : str or Macro
1819 themacro : str or Macro
1820 The action to do upon invoking the macro. If a string, a new
1820 The action to do upon invoking the macro. If a string, a new
1821 Macro object is created by passing the string to it.
1821 Macro object is created by passing the string to it.
1822 """
1822 """
1823
1823
1824 from IPython.core import macro
1824 from IPython.core import macro
1825
1825
1826 if isinstance(themacro, basestring):
1826 if isinstance(themacro, basestring):
1827 themacro = macro.Macro(themacro)
1827 themacro = macro.Macro(themacro)
1828 if not isinstance(themacro, macro.Macro):
1828 if not isinstance(themacro, macro.Macro):
1829 raise ValueError('A macro must be a string or a Macro instance.')
1829 raise ValueError('A macro must be a string or a Macro instance.')
1830 self.user_ns[name] = themacro
1830 self.user_ns[name] = themacro
1831
1831
1832 #-------------------------------------------------------------------------
1832 #-------------------------------------------------------------------------
1833 # Things related to the running of system commands
1833 # Things related to the running of system commands
1834 #-------------------------------------------------------------------------
1834 #-------------------------------------------------------------------------
1835
1835
1836 def system(self, cmd):
1836 def system(self, cmd):
1837 """Call the given cmd in a subprocess.
1837 """Call the given cmd in a subprocess.
1838
1838
1839 Parameters
1839 Parameters
1840 ----------
1840 ----------
1841 cmd : str
1841 cmd : str
1842 Command to execute (can not end in '&', as bacground processes are
1842 Command to execute (can not end in '&', as bacground processes are
1843 not supported.
1843 not supported.
1844 """
1844 """
1845 # We do not support backgrounding processes because we either use
1845 # We do not support backgrounding processes because we either use
1846 # pexpect or pipes to read from. Users can always just call
1846 # pexpect or pipes to read from. Users can always just call
1847 # os.system() if they really want a background process.
1847 # os.system() if they really want a background process.
1848 if cmd.endswith('&'):
1848 if cmd.endswith('&'):
1849 raise OSError("Background processes not supported.")
1849 raise OSError("Background processes not supported.")
1850
1850
1851 return system(self.var_expand(cmd, depth=2))
1851 return system(self.var_expand(cmd, depth=2))
1852
1852
1853 def getoutput(self, cmd, split=True):
1853 def getoutput(self, cmd, split=True):
1854 """Get output (possibly including stderr) from a subprocess.
1854 """Get output (possibly including stderr) from a subprocess.
1855
1855
1856 Parameters
1856 Parameters
1857 ----------
1857 ----------
1858 cmd : str
1858 cmd : str
1859 Command to execute (can not end in '&', as background processes are
1859 Command to execute (can not end in '&', as background processes are
1860 not supported.
1860 not supported.
1861 split : bool, optional
1861 split : bool, optional
1862
1862
1863 If True, split the output into an IPython SList. Otherwise, an
1863 If True, split the output into an IPython SList. Otherwise, an
1864 IPython LSString is returned. These are objects similar to normal
1864 IPython LSString is returned. These are objects similar to normal
1865 lists and strings, with a few convenience attributes for easier
1865 lists and strings, with a few convenience attributes for easier
1866 manipulation of line-based output. You can use '?' on them for
1866 manipulation of line-based output. You can use '?' on them for
1867 details.
1867 details.
1868 """
1868 """
1869 if cmd.endswith('&'):
1869 if cmd.endswith('&'):
1870 raise OSError("Background processes not supported.")
1870 raise OSError("Background processes not supported.")
1871 out = getoutput(self.var_expand(cmd, depth=2))
1871 out = getoutput(self.var_expand(cmd, depth=2))
1872 if split:
1872 if split:
1873 out = SList(out.splitlines())
1873 out = SList(out.splitlines())
1874 else:
1874 else:
1875 out = LSString(out)
1875 out = LSString(out)
1876 return out
1876 return out
1877
1877
1878 #-------------------------------------------------------------------------
1878 #-------------------------------------------------------------------------
1879 # Things related to aliases
1879 # Things related to aliases
1880 #-------------------------------------------------------------------------
1880 #-------------------------------------------------------------------------
1881
1881
1882 def init_alias(self):
1882 def init_alias(self):
1883 self.alias_manager = AliasManager(shell=self, config=self.config)
1883 self.alias_manager = AliasManager(shell=self, config=self.config)
1884 self.ns_table['alias'] = self.alias_manager.alias_table,
1884 self.ns_table['alias'] = self.alias_manager.alias_table,
1885
1885
1886 #-------------------------------------------------------------------------
1886 #-------------------------------------------------------------------------
1887 # Things related to extensions and plugins
1887 # Things related to extensions and plugins
1888 #-------------------------------------------------------------------------
1888 #-------------------------------------------------------------------------
1889
1889
1890 def init_extension_manager(self):
1890 def init_extension_manager(self):
1891 self.extension_manager = ExtensionManager(shell=self, config=self.config)
1891 self.extension_manager = ExtensionManager(shell=self, config=self.config)
1892
1892
1893 def init_plugin_manager(self):
1893 def init_plugin_manager(self):
1894 self.plugin_manager = PluginManager(config=self.config)
1894 self.plugin_manager = PluginManager(config=self.config)
1895
1895
1896 #-------------------------------------------------------------------------
1896 #-------------------------------------------------------------------------
1897 # Things related to payloads
1897 # Things related to payloads
1898 #-------------------------------------------------------------------------
1898 #-------------------------------------------------------------------------
1899
1899
1900 def init_payload(self):
1900 def init_payload(self):
1901 self.payload_manager = PayloadManager(config=self.config)
1901 self.payload_manager = PayloadManager(config=self.config)
1902
1902
1903 #-------------------------------------------------------------------------
1903 #-------------------------------------------------------------------------
1904 # Things related to the prefilter
1904 # Things related to the prefilter
1905 #-------------------------------------------------------------------------
1905 #-------------------------------------------------------------------------
1906
1906
1907 def init_prefilter(self):
1907 def init_prefilter(self):
1908 self.prefilter_manager = PrefilterManager(shell=self, config=self.config)
1908 self.prefilter_manager = PrefilterManager(shell=self, config=self.config)
1909 # Ultimately this will be refactored in the new interpreter code, but
1909 # Ultimately this will be refactored in the new interpreter code, but
1910 # for now, we should expose the main prefilter method (there's legacy
1910 # for now, we should expose the main prefilter method (there's legacy
1911 # code out there that may rely on this).
1911 # code out there that may rely on this).
1912 self.prefilter = self.prefilter_manager.prefilter_lines
1912 self.prefilter = self.prefilter_manager.prefilter_lines
1913
1913
1914 def auto_rewrite_input(self, cmd):
1914 def auto_rewrite_input(self, cmd):
1915 """Print to the screen the rewritten form of the user's command.
1915 """Print to the screen the rewritten form of the user's command.
1916
1916
1917 This shows visual feedback by rewriting input lines that cause
1917 This shows visual feedback by rewriting input lines that cause
1918 automatic calling to kick in, like::
1918 automatic calling to kick in, like::
1919
1919
1920 /f x
1920 /f x
1921
1921
1922 into::
1922 into::
1923
1923
1924 ------> f(x)
1924 ------> f(x)
1925
1925
1926 after the user's input prompt. This helps the user understand that the
1926 after the user's input prompt. This helps the user understand that the
1927 input line was transformed automatically by IPython.
1927 input line was transformed automatically by IPython.
1928 """
1928 """
1929 rw = self.displayhook.prompt1.auto_rewrite() + cmd
1929 rw = self.displayhook.prompt1.auto_rewrite() + cmd
1930
1930
1931 try:
1931 try:
1932 # plain ascii works better w/ pyreadline, on some machines, so
1932 # plain ascii works better w/ pyreadline, on some machines, so
1933 # we use it and only print uncolored rewrite if we have unicode
1933 # we use it and only print uncolored rewrite if we have unicode
1934 rw = str(rw)
1934 rw = str(rw)
1935 print >> IPython.utils.io.Term.cout, rw
1935 print >> IPython.utils.io.Term.cout, rw
1936 except UnicodeEncodeError:
1936 except UnicodeEncodeError:
1937 print "------> " + cmd
1937 print "------> " + cmd
1938
1938
1939 #-------------------------------------------------------------------------
1939 #-------------------------------------------------------------------------
1940 # Things related to extracting values/expressions from kernel and user_ns
1940 # Things related to extracting values/expressions from kernel and user_ns
1941 #-------------------------------------------------------------------------
1941 #-------------------------------------------------------------------------
1942
1942
1943 def _simple_error(self):
1943 def _simple_error(self):
1944 etype, value = sys.exc_info()[:2]
1944 etype, value = sys.exc_info()[:2]
1945 return u'[ERROR] {e.__name__}: {v}'.format(e=etype, v=value)
1945 return u'[ERROR] {e.__name__}: {v}'.format(e=etype, v=value)
1946
1946
1947 def user_variables(self, names):
1947 def user_variables(self, names):
1948 """Get a list of variable names from the user's namespace.
1948 """Get a list of variable names from the user's namespace.
1949
1949
1950 Parameters
1950 Parameters
1951 ----------
1951 ----------
1952 names : list of strings
1952 names : list of strings
1953 A list of names of variables to be read from the user namespace.
1953 A list of names of variables to be read from the user namespace.
1954
1954
1955 Returns
1955 Returns
1956 -------
1956 -------
1957 A dict, keyed by the input names and with the repr() of each value.
1957 A dict, keyed by the input names and with the repr() of each value.
1958 """
1958 """
1959 out = {}
1959 out = {}
1960 user_ns = self.user_ns
1960 user_ns = self.user_ns
1961 for varname in names:
1961 for varname in names:
1962 try:
1962 try:
1963 value = repr(user_ns[varname])
1963 value = repr(user_ns[varname])
1964 except:
1964 except:
1965 value = self._simple_error()
1965 value = self._simple_error()
1966 out[varname] = value
1966 out[varname] = value
1967 return out
1967 return out
1968
1968
1969 def user_expressions(self, expressions):
1969 def user_expressions(self, expressions):
1970 """Evaluate a dict of expressions in the user's namespace.
1970 """Evaluate a dict of expressions in the user's namespace.
1971
1971
1972 Parameters
1972 Parameters
1973 ----------
1973 ----------
1974 expressions : dict
1974 expressions : dict
1975 A dict with string keys and string values. The expression values
1975 A dict with string keys and string values. The expression values
1976 should be valid Python expressions, each of which will be evaluated
1976 should be valid Python expressions, each of which will be evaluated
1977 in the user namespace.
1977 in the user namespace.
1978
1978
1979 Returns
1979 Returns
1980 -------
1980 -------
1981 A dict, keyed like the input expressions dict, with the repr() of each
1981 A dict, keyed like the input expressions dict, with the repr() of each
1982 value.
1982 value.
1983 """
1983 """
1984 out = {}
1984 out = {}
1985 user_ns = self.user_ns
1985 user_ns = self.user_ns
1986 global_ns = self.user_global_ns
1986 global_ns = self.user_global_ns
1987 for key, expr in expressions.iteritems():
1987 for key, expr in expressions.iteritems():
1988 try:
1988 try:
1989 value = repr(eval(expr, global_ns, user_ns))
1989 value = repr(eval(expr, global_ns, user_ns))
1990 except:
1990 except:
1991 value = self._simple_error()
1991 value = self._simple_error()
1992 out[key] = value
1992 out[key] = value
1993 return out
1993 return out
1994
1994
1995 #-------------------------------------------------------------------------
1995 #-------------------------------------------------------------------------
1996 # Things related to the running of code
1996 # Things related to the running of code
1997 #-------------------------------------------------------------------------
1997 #-------------------------------------------------------------------------
1998
1998
1999 def ex(self, cmd):
1999 def ex(self, cmd):
2000 """Execute a normal python statement in user namespace."""
2000 """Execute a normal python statement in user namespace."""
2001 with self.builtin_trap:
2001 with self.builtin_trap:
2002 exec cmd in self.user_global_ns, self.user_ns
2002 exec cmd in self.user_global_ns, self.user_ns
2003
2003
2004 def ev(self, expr):
2004 def ev(self, expr):
2005 """Evaluate python expression expr in user namespace.
2005 """Evaluate python expression expr in user namespace.
2006
2006
2007 Returns the result of evaluation
2007 Returns the result of evaluation
2008 """
2008 """
2009 with self.builtin_trap:
2009 with self.builtin_trap:
2010 return eval(expr, self.user_global_ns, self.user_ns)
2010 return eval(expr, self.user_global_ns, self.user_ns)
2011
2011
2012 def safe_execfile(self, fname, *where, **kw):
2012 def safe_execfile(self, fname, *where, **kw):
2013 """A safe version of the builtin execfile().
2013 """A safe version of the builtin execfile().
2014
2014
2015 This version will never throw an exception, but instead print
2015 This version will never throw an exception, but instead print
2016 helpful error messages to the screen. This only works on pure
2016 helpful error messages to the screen. This only works on pure
2017 Python files with the .py extension.
2017 Python files with the .py extension.
2018
2018
2019 Parameters
2019 Parameters
2020 ----------
2020 ----------
2021 fname : string
2021 fname : string
2022 The name of the file to be executed.
2022 The name of the file to be executed.
2023 where : tuple
2023 where : tuple
2024 One or two namespaces, passed to execfile() as (globals,locals).
2024 One or two namespaces, passed to execfile() as (globals,locals).
2025 If only one is given, it is passed as both.
2025 If only one is given, it is passed as both.
2026 exit_ignore : bool (False)
2026 exit_ignore : bool (False)
2027 If True, then silence SystemExit for non-zero status (it is always
2027 If True, then silence SystemExit for non-zero status (it is always
2028 silenced for zero status, as it is so common).
2028 silenced for zero status, as it is so common).
2029 """
2029 """
2030 kw.setdefault('exit_ignore', False)
2030 kw.setdefault('exit_ignore', False)
2031
2031
2032 fname = os.path.abspath(os.path.expanduser(fname))
2032 fname = os.path.abspath(os.path.expanduser(fname))
2033 # Make sure we have a .py file
2033 # Make sure we have a .py file
2034 if not fname.endswith('.py'):
2034 if not fname.endswith('.py'):
2035 warn('File must end with .py to be run using execfile: <%s>' % fname)
2035 warn('File must end with .py to be run using execfile: <%s>' % fname)
2036
2036
2037 # Make sure we can open the file
2037 # Make sure we can open the file
2038 try:
2038 try:
2039 with open(fname) as thefile:
2039 with open(fname) as thefile:
2040 pass
2040 pass
2041 except:
2041 except:
2042 warn('Could not open file <%s> for safe execution.' % fname)
2042 warn('Could not open file <%s> for safe execution.' % fname)
2043 return
2043 return
2044
2044
2045 # Find things also in current directory. This is needed to mimic the
2045 # Find things also in current directory. This is needed to mimic the
2046 # behavior of running a script from the system command line, where
2046 # behavior of running a script from the system command line, where
2047 # Python inserts the script's directory into sys.path
2047 # Python inserts the script's directory into sys.path
2048 dname = os.path.dirname(fname)
2048 dname = os.path.dirname(fname)
2049
2049
2050 if isinstance(fname, unicode):
2050 if isinstance(fname, unicode):
2051 # execfile uses default encoding instead of filesystem encoding
2051 # execfile uses default encoding instead of filesystem encoding
2052 # so unicode filenames will fail
2052 # so unicode filenames will fail
2053 fname = fname.encode(sys.getfilesystemencoding() or sys.getdefaultencoding())
2053 fname = fname.encode(sys.getfilesystemencoding() or sys.getdefaultencoding())
2054
2054
2055 with prepended_to_syspath(dname):
2055 with prepended_to_syspath(dname):
2056 try:
2056 try:
2057 execfile(fname,*where)
2057 execfile(fname,*where)
2058 except SystemExit, status:
2058 except SystemExit, status:
2059 # If the call was made with 0 or None exit status (sys.exit(0)
2059 # If the call was made with 0 or None exit status (sys.exit(0)
2060 # or sys.exit() ), don't bother showing a traceback, as both of
2060 # or sys.exit() ), don't bother showing a traceback, as both of
2061 # these are considered normal by the OS:
2061 # these are considered normal by the OS:
2062 # > python -c'import sys;sys.exit(0)'; echo $?
2062 # > python -c'import sys;sys.exit(0)'; echo $?
2063 # 0
2063 # 0
2064 # > python -c'import sys;sys.exit()'; echo $?
2064 # > python -c'import sys;sys.exit()'; echo $?
2065 # 0
2065 # 0
2066 # For other exit status, we show the exception unless
2066 # For other exit status, we show the exception unless
2067 # explicitly silenced, but only in short form.
2067 # explicitly silenced, but only in short form.
2068 if status.code not in (0, None) and not kw['exit_ignore']:
2068 if status.code not in (0, None) and not kw['exit_ignore']:
2069 self.showtraceback(exception_only=True)
2069 self.showtraceback(exception_only=True)
2070 except:
2070 except:
2071 self.showtraceback()
2071 self.showtraceback()
2072
2072
2073 def safe_execfile_ipy(self, fname):
2073 def safe_execfile_ipy(self, fname):
2074 """Like safe_execfile, but for .ipy files with IPython syntax.
2074 """Like safe_execfile, but for .ipy files with IPython syntax.
2075
2075
2076 Parameters
2076 Parameters
2077 ----------
2077 ----------
2078 fname : str
2078 fname : str
2079 The name of the file to execute. The filename must have a
2079 The name of the file to execute. The filename must have a
2080 .ipy extension.
2080 .ipy extension.
2081 """
2081 """
2082 fname = os.path.abspath(os.path.expanduser(fname))
2082 fname = os.path.abspath(os.path.expanduser(fname))
2083
2083
2084 # Make sure we have a .py file
2084 # Make sure we have a .py file
2085 if not fname.endswith('.ipy'):
2085 if not fname.endswith('.ipy'):
2086 warn('File must end with .py to be run using execfile: <%s>' % fname)
2086 warn('File must end with .py to be run using execfile: <%s>' % fname)
2087
2087
2088 # Make sure we can open the file
2088 # Make sure we can open the file
2089 try:
2089 try:
2090 with open(fname) as thefile:
2090 with open(fname) as thefile:
2091 pass
2091 pass
2092 except:
2092 except:
2093 warn('Could not open file <%s> for safe execution.' % fname)
2093 warn('Could not open file <%s> for safe execution.' % fname)
2094 return
2094 return
2095
2095
2096 # Find things also in current directory. This is needed to mimic the
2096 # Find things also in current directory. This is needed to mimic the
2097 # behavior of running a script from the system command line, where
2097 # behavior of running a script from the system command line, where
2098 # Python inserts the script's directory into sys.path
2098 # Python inserts the script's directory into sys.path
2099 dname = os.path.dirname(fname)
2099 dname = os.path.dirname(fname)
2100
2100
2101 with prepended_to_syspath(dname):
2101 with prepended_to_syspath(dname):
2102 try:
2102 try:
2103 with open(fname) as thefile:
2103 with open(fname) as thefile:
2104 # self.run_cell currently captures all exceptions
2104 # self.run_cell currently captures all exceptions
2105 # raised in user code. It would be nice if there were
2105 # raised in user code. It would be nice if there were
2106 # versions of runlines, execfile that did raise, so
2106 # versions of runlines, execfile that did raise, so
2107 # we could catch the errors.
2107 # we could catch the errors.
2108 self.run_cell(thefile.read(), store_history=False)
2108 self.run_cell(thefile.read(), store_history=False)
2109 except:
2109 except:
2110 self.showtraceback()
2110 self.showtraceback()
2111 warn('Unknown failure executing file: <%s>' % fname)
2111 warn('Unknown failure executing file: <%s>' % fname)
2112
2112
2113 def run_cell(self, raw_cell, store_history=True):
2113 def run_cell(self, raw_cell, store_history=True):
2114 """Run a complete IPython cell.
2114 """Run a complete IPython cell.
2115
2115
2116 Parameters
2116 Parameters
2117 ----------
2117 ----------
2118 raw_cell : str
2118 raw_cell : str
2119 The code (including IPython code such as %magic functions) to run.
2119 The code (including IPython code such as %magic functions) to run.
2120 store_history : bool
2120 store_history : bool
2121 If True, the raw and translated cell will be stored in IPython's
2121 If True, the raw and translated cell will be stored in IPython's
2122 history. For user code calling back into IPython's machinery, this
2122 history. For user code calling back into IPython's machinery, this
2123 should be set to False.
2123 should be set to False.
2124 """
2124 """
2125 if (not raw_cell) or raw_cell.isspace():
2125 if (not raw_cell) or raw_cell.isspace():
2126 return
2126 return
2127
2127
2128 for line in raw_cell.splitlines():
2128 for line in raw_cell.splitlines():
2129 self.input_splitter.push(line)
2129 self.input_splitter.push(line)
2130 cell = self.input_splitter.source_reset()
2130 cell = self.input_splitter.source_reset()
2131
2131
2132 with self.builtin_trap:
2132 with self.builtin_trap:
2133 if len(cell.splitlines()) == 1:
2133 if len(cell.splitlines()) == 1:
2134 cell = self.prefilter_manager.prefilter_lines(cell)
2134 cell = self.prefilter_manager.prefilter_lines(cell)
2135
2135
2136 # Store raw and processed history
2136 # Store raw and processed history
2137 if store_history:
2137 if store_history:
2138 self.history_manager.store_inputs(self.execution_count,
2138 self.history_manager.store_inputs(self.execution_count,
2139 cell, raw_cell)
2139 cell, raw_cell)
2140
2140
2141 self.logger.log(cell, raw_cell)
2141 self.logger.log(cell, raw_cell)
2142
2142
2143 cell_name = self.compile.cache(cell, self.execution_count)
2143 cell_name = self.compile.cache(cell, self.execution_count)
2144
2144
2145 with self.display_trap:
2145 with self.display_trap:
2146 try:
2146 try:
2147 code_ast = ast.parse(cell, filename=cell_name)
2147 code_ast = ast.parse(cell, filename=cell_name)
2148 except (OverflowError, SyntaxError, ValueError, TypeError,
2148 except (OverflowError, SyntaxError, ValueError, TypeError,
2149 MemoryError):
2149 MemoryError):
2150 self.showsyntaxerror()
2150 self.showsyntaxerror()
2151 self.execution_count += 1
2151 self.execution_count += 1
2152 return None
2152 return None
2153
2153
2154 self.run_ast_nodes(code_ast.body, cell_name,
2154 self.run_ast_nodes(code_ast.body, cell_name,
2155 interactivity="last_expr")
2155 interactivity="last_expr")
2156
2156
2157 # Execute any registered post-execution functions.
2157 # Execute any registered post-execution functions.
2158 for func, status in self._post_execute.iteritems():
2158 for func, status in self._post_execute.iteritems():
2159 if not status:
2159 if not status:
2160 continue
2160 continue
2161 try:
2161 try:
2162 func()
2162 func()
2163 except:
2163 except:
2164 self.showtraceback()
2164 self.showtraceback()
2165 # Deactivate failing function
2165 # Deactivate failing function
2166 self._post_execute[func] = False
2166 self._post_execute[func] = False
2167
2167
2168 if store_history:
2168 if store_history:
2169 # Write output to the database. Does nothing unless
2169 # Write output to the database. Does nothing unless
2170 # history output logging is enabled.
2170 # history output logging is enabled.
2171 self.history_manager.store_output(self.execution_count)
2171 self.history_manager.store_output(self.execution_count)
2172 # Each cell is a *single* input, regardless of how many lines it has
2172 # Each cell is a *single* input, regardless of how many lines it has
2173 self.execution_count += 1
2173 self.execution_count += 1
2174
2174
2175 def run_ast_nodes(self, nodelist, cell_name, interactivity='last_expr'):
2175 def run_ast_nodes(self, nodelist, cell_name, interactivity='last_expr'):
2176 """Run a sequence of AST nodes. The execution mode depends on the
2176 """Run a sequence of AST nodes. The execution mode depends on the
2177 interactivity parameter.
2177 interactivity parameter.
2178
2178
2179 Parameters
2179 Parameters
2180 ----------
2180 ----------
2181 nodelist : list
2181 nodelist : list
2182 A sequence of AST nodes to run.
2182 A sequence of AST nodes to run.
2183 cell_name : str
2183 cell_name : str
2184 Will be passed to the compiler as the filename of the cell. Typically
2184 Will be passed to the compiler as the filename of the cell. Typically
2185 the value returned by ip.compile.cache(cell).
2185 the value returned by ip.compile.cache(cell).
2186 interactivity : str
2186 interactivity : str
2187 'all', 'last', 'last_expr' or 'none', specifying which nodes should be
2187 'all', 'last', 'last_expr' or 'none', specifying which nodes should be
2188 run interactively (displaying output from expressions). 'last_expr'
2188 run interactively (displaying output from expressions). 'last_expr'
2189 will run the last node interactively only if it is an expression (i.e.
2189 will run the last node interactively only if it is an expression (i.e.
2190 expressions in loops or other blocks are not displayed. Other values
2190 expressions in loops or other blocks are not displayed. Other values
2191 for this parameter will raise a ValueError.
2191 for this parameter will raise a ValueError.
2192 """
2192 """
2193 if not nodelist:
2193 if not nodelist:
2194 return
2194 return
2195
2195
2196 if interactivity == 'last_expr':
2196 if interactivity == 'last_expr':
2197 if isinstance(nodelist[-1], ast.Expr):
2197 if isinstance(nodelist[-1], ast.Expr):
2198 interactivity = "last"
2198 interactivity = "last"
2199 else:
2199 else:
2200 interactivity = "none"
2200 interactivity = "none"
2201
2201
2202 if interactivity == 'none':
2202 if interactivity == 'none':
2203 to_run_exec, to_run_interactive = nodelist, []
2203 to_run_exec, to_run_interactive = nodelist, []
2204 elif interactivity == 'last':
2204 elif interactivity == 'last':
2205 to_run_exec, to_run_interactive = nodelist[:-1], nodelist[-1:]
2205 to_run_exec, to_run_interactive = nodelist[:-1], nodelist[-1:]
2206 elif interactivity == 'all':
2206 elif interactivity == 'all':
2207 to_run_exec, to_run_interactive = [], nodelist
2207 to_run_exec, to_run_interactive = [], nodelist
2208 else:
2208 else:
2209 raise ValueError("Interactivity was %r" % interactivity)
2209 raise ValueError("Interactivity was %r" % interactivity)
2210
2210
2211 exec_count = self.execution_count
2211 exec_count = self.execution_count
2212
2212
2213 for i, node in enumerate(to_run_exec):
2213 for i, node in enumerate(to_run_exec):
2214 mod = ast.Module([node])
2214 mod = ast.Module([node])
2215 code = self.compile(mod, cell_name, "exec")
2215 code = self.compile(mod, cell_name, "exec")
2216 if self.run_code(code):
2216 if self.run_code(code):
2217 return True
2217 return True
2218
2218
2219 for i, node in enumerate(to_run_interactive):
2219 for i, node in enumerate(to_run_interactive):
2220 mod = ast.Interactive([node])
2220 mod = ast.Interactive([node])
2221 code = self.compile(mod, cell_name, "single")
2221 code = self.compile(mod, cell_name, "single")
2222 if self.run_code(code):
2222 if self.run_code(code):
2223 return True
2223 return True
2224
2224
2225 return False
2225 return False
2226
2226
2227
2228 # PENDING REMOVAL: this method is slated for deletion, once our new
2229 # input logic has been 100% moved to frontends and is stable.
2230 def runlines(self, lines, clean=False):
2231 """Run a string of one or more lines of source.
2232
2233 This method is capable of running a string containing multiple source
2234 lines, as if they had been entered at the IPython prompt. Since it
2235 exposes IPython's processing machinery, the given strings can contain
2236 magic calls (%magic), special shell access (!cmd), etc.
2237 """
2238
2239 if not isinstance(lines, (list, tuple)):
2240 lines = lines.splitlines()
2241
2242 if clean:
2243 lines = self._cleanup_ipy_script(lines)
2244
2245 # We must start with a clean buffer, in case this is run from an
2246 # interactive IPython session (via a magic, for example).
2247 self.reset_buffer()
2248
2249 # Since we will prefilter all lines, store the user's raw input too
2250 # before we apply any transformations
2251 self.buffer_raw[:] = [ l+'\n' for l in lines]
2252
2253 more = False
2254 prefilter_lines = self.prefilter_manager.prefilter_lines
2255 with nested(self.builtin_trap, self.display_trap):
2256 for line in lines:
2257 # skip blank lines so we don't mess up the prompt counter, but
2258 # do NOT skip even a blank line if we are in a code block (more
2259 # is true)
2260
2261 if line or more:
2262 more = self.push_line(prefilter_lines(line, more))
2263 # IPython's run_source returns None if there was an error
2264 # compiling the code. This allows us to stop processing
2265 # right away, so the user gets the error message at the
2266 # right place.
2267 if more is None:
2268 break
2269 # final newline in case the input didn't have it, so that the code
2270 # actually does get executed
2271 if more:
2272 self.push_line('\n')
2273
2227
2274 def run_source(self, source, filename=None, symbol='single'):
2228 def run_source(self, source, filename=None, symbol='single'):
2275 """Compile and run some source in the interpreter.
2229 """Compile and run some source in the interpreter.
2276
2230
2277 Arguments are as for compile_command().
2231 Arguments are as for compile_command().
2278
2232
2279 One several things can happen:
2233 One several things can happen:
2280
2234
2281 1) The input is incorrect; compile_command() raised an
2235 1) The input is incorrect; compile_command() raised an
2282 exception (SyntaxError or OverflowError). A syntax traceback
2236 exception (SyntaxError or OverflowError). A syntax traceback
2283 will be printed by calling the showsyntaxerror() method.
2237 will be printed by calling the showsyntaxerror() method.
2284
2238
2285 2) The input is incomplete, and more input is required;
2239 2) The input is incomplete, and more input is required;
2286 compile_command() returned None. Nothing happens.
2240 compile_command() returned None. Nothing happens.
2287
2241
2288 3) The input is complete; compile_command() returned a code
2242 3) The input is complete; compile_command() returned a code
2289 object. The code is executed by calling self.run_code() (which
2243 object. The code is executed by calling self.run_code() (which
2290 also handles run-time exceptions, except for SystemExit).
2244 also handles run-time exceptions, except for SystemExit).
2291
2245
2292 The return value is:
2246 The return value is:
2293
2247
2294 - True in case 2
2248 - True in case 2
2295
2249
2296 - False in the other cases, unless an exception is raised, where
2250 - False in the other cases, unless an exception is raised, where
2297 None is returned instead. This can be used by external callers to
2251 None is returned instead. This can be used by external callers to
2298 know whether to continue feeding input or not.
2252 know whether to continue feeding input or not.
2299
2253
2300 The return value can be used to decide whether to use sys.ps1 or
2254 The return value can be used to decide whether to use sys.ps1 or
2301 sys.ps2 to prompt the next line."""
2255 sys.ps2 to prompt the next line."""
2302
2256
2303 # We need to ensure that the source is unicode from here on.
2257 # We need to ensure that the source is unicode from here on.
2304 if type(source)==str:
2258 if type(source)==str:
2305 usource = source.decode(self.stdin_encoding)
2259 usource = source.decode(self.stdin_encoding)
2306 else:
2260 else:
2307 usource = source
2261 usource = source
2308
2262
2309 try:
2263 try:
2310 code_name = self.compile.cache(usource, self.execution_count)
2264 code_name = self.compile.cache(usource, self.execution_count)
2311 code = self.compile(usource, code_name, symbol)
2265 code = self.compile(usource, code_name, symbol)
2312 except (OverflowError, SyntaxError, ValueError, TypeError, MemoryError):
2266 except (OverflowError, SyntaxError, ValueError, TypeError, MemoryError):
2313 # Case 1
2267 # Case 1
2314 self.showsyntaxerror(filename)
2268 self.showsyntaxerror(filename)
2315 return None
2269 return None
2316
2270
2317 if code is None:
2271 if code is None:
2318 # Case 2
2272 # Case 2
2319 return True
2273 return True
2320
2274
2321 # Case 3
2275 # Case 3
2322 # now actually execute the code object
2276 # now actually execute the code object
2323 if not self.run_code(code):
2277 if not self.run_code(code):
2324 return False
2278 return False
2325 else:
2279 else:
2326 return None
2280 return None
2327
2281
2328 # For backwards compatibility
2282 # For backwards compatibility
2329 runsource = run_source
2283 runsource = run_source
2330
2284
2331 def run_code(self, code_obj):
2285 def run_code(self, code_obj):
2332 """Execute a code object.
2286 """Execute a code object.
2333
2287
2334 When an exception occurs, self.showtraceback() is called to display a
2288 When an exception occurs, self.showtraceback() is called to display a
2335 traceback.
2289 traceback.
2336
2290
2337 Parameters
2291 Parameters
2338 ----------
2292 ----------
2339 code_obj : code object
2293 code_obj : code object
2340 A compiled code object, to be executed
2294 A compiled code object, to be executed
2341 post_execute : bool [default: True]
2295 post_execute : bool [default: True]
2342 whether to call post_execute hooks after this particular execution.
2296 whether to call post_execute hooks after this particular execution.
2343
2297
2344 Returns
2298 Returns
2345 -------
2299 -------
2346 False : successful execution.
2300 False : successful execution.
2347 True : an error occurred.
2301 True : an error occurred.
2348 """
2302 """
2349
2303
2350 # Set our own excepthook in case the user code tries to call it
2304 # Set our own excepthook in case the user code tries to call it
2351 # directly, so that the IPython crash handler doesn't get triggered
2305 # directly, so that the IPython crash handler doesn't get triggered
2352 old_excepthook,sys.excepthook = sys.excepthook, self.excepthook
2306 old_excepthook,sys.excepthook = sys.excepthook, self.excepthook
2353
2307
2354 # we save the original sys.excepthook in the instance, in case config
2308 # we save the original sys.excepthook in the instance, in case config
2355 # code (such as magics) needs access to it.
2309 # code (such as magics) needs access to it.
2356 self.sys_excepthook = old_excepthook
2310 self.sys_excepthook = old_excepthook
2357 outflag = 1 # happens in more places, so it's easier as default
2311 outflag = 1 # happens in more places, so it's easier as default
2358 try:
2312 try:
2359 try:
2313 try:
2360 self.hooks.pre_run_code_hook()
2314 self.hooks.pre_run_code_hook()
2361 #rprint('Running code', repr(code_obj)) # dbg
2315 #rprint('Running code', repr(code_obj)) # dbg
2362 exec code_obj in self.user_global_ns, self.user_ns
2316 exec code_obj in self.user_global_ns, self.user_ns
2363 finally:
2317 finally:
2364 # Reset our crash handler in place
2318 # Reset our crash handler in place
2365 sys.excepthook = old_excepthook
2319 sys.excepthook = old_excepthook
2366 except SystemExit:
2320 except SystemExit:
2367 self.reset_buffer()
2321 self.reset_buffer()
2368 self.showtraceback(exception_only=True)
2322 self.showtraceback(exception_only=True)
2369 warn("To exit: use any of 'exit', 'quit', %Exit or Ctrl-D.", level=1)
2323 warn("To exit: use any of 'exit', 'quit', %Exit or Ctrl-D.", level=1)
2370 except self.custom_exceptions:
2324 except self.custom_exceptions:
2371 etype,value,tb = sys.exc_info()
2325 etype,value,tb = sys.exc_info()
2372 self.CustomTB(etype,value,tb)
2326 self.CustomTB(etype,value,tb)
2373 except:
2327 except:
2374 self.showtraceback()
2328 self.showtraceback()
2375 else:
2329 else:
2376 outflag = 0
2330 outflag = 0
2377 if softspace(sys.stdout, 0):
2331 if softspace(sys.stdout, 0):
2378 print
2332 print
2379
2333
2380 return outflag
2334 return outflag
2381
2335
2382 # For backwards compatibility
2336 # For backwards compatibility
2383 runcode = run_code
2337 runcode = run_code
2384
2338
2385 # PENDING REMOVAL: this method is slated for deletion, once our new
2339 # PENDING REMOVAL: this method is slated for deletion, once our new
2386 # input logic has been 100% moved to frontends and is stable.
2340 # input logic has been 100% moved to frontends and is stable.
2387 def push_line(self, line):
2341 def push_line(self, line):
2388 """Push a line to the interpreter.
2342 """Push a line to the interpreter.
2389
2343
2390 The line should not have a trailing newline; it may have
2344 The line should not have a trailing newline; it may have
2391 internal newlines. The line is appended to a buffer and the
2345 internal newlines. The line is appended to a buffer and the
2392 interpreter's run_source() method is called with the
2346 interpreter's run_source() method is called with the
2393 concatenated contents of the buffer as source. If this
2347 concatenated contents of the buffer as source. If this
2394 indicates that the command was executed or invalid, the buffer
2348 indicates that the command was executed or invalid, the buffer
2395 is reset; otherwise, the command is incomplete, and the buffer
2349 is reset; otherwise, the command is incomplete, and the buffer
2396 is left as it was after the line was appended. The return
2350 is left as it was after the line was appended. The return
2397 value is 1 if more input is required, 0 if the line was dealt
2351 value is 1 if more input is required, 0 if the line was dealt
2398 with in some way (this is the same as run_source()).
2352 with in some way (this is the same as run_source()).
2399 """
2353 """
2400
2354
2401 # autoindent management should be done here, and not in the
2355 # autoindent management should be done here, and not in the
2402 # interactive loop, since that one is only seen by keyboard input. We
2356 # interactive loop, since that one is only seen by keyboard input. We
2403 # need this done correctly even for code run via runlines (which uses
2357 # need this done correctly even for code run via runlines (which uses
2404 # push).
2358 # push).
2405
2359
2406 #print 'push line: <%s>' % line # dbg
2360 #print 'push line: <%s>' % line # dbg
2407 self.buffer.append(line)
2361 self.buffer.append(line)
2408 full_source = '\n'.join(self.buffer)
2362 full_source = '\n'.join(self.buffer)
2409 more = self.run_source(full_source, self.filename)
2363 more = self.run_source(full_source, self.filename)
2410 if not more:
2364 if not more:
2411 self.history_manager.store_inputs(self.execution_count,
2365 self.history_manager.store_inputs(self.execution_count,
2412 '\n'.join(self.buffer_raw), full_source)
2366 '\n'.join(self.buffer_raw), full_source)
2413 self.reset_buffer()
2367 self.reset_buffer()
2414 self.execution_count += 1
2368 self.execution_count += 1
2415 return more
2369 return more
2416
2370
2417 def reset_buffer(self):
2371 def reset_buffer(self):
2418 """Reset the input buffer."""
2372 """Reset the input buffer."""
2419 self.buffer[:] = []
2373 self.buffer[:] = []
2420 self.buffer_raw[:] = []
2374 self.buffer_raw[:] = []
2421 self.input_splitter.reset()
2375 self.input_splitter.reset()
2422
2376
2423 # For backwards compatibility
2377 # For backwards compatibility
2424 resetbuffer = reset_buffer
2378 resetbuffer = reset_buffer
2425
2379
2426 def _is_secondary_block_start(self, s):
2380 def _is_secondary_block_start(self, s):
2427 if not s.endswith(':'):
2381 if not s.endswith(':'):
2428 return False
2382 return False
2429 if (s.startswith('elif') or
2383 if (s.startswith('elif') or
2430 s.startswith('else') or
2384 s.startswith('else') or
2431 s.startswith('except') or
2385 s.startswith('except') or
2432 s.startswith('finally')):
2386 s.startswith('finally')):
2433 return True
2387 return True
2434
2388
2435 def _cleanup_ipy_script(self, script):
2389 def _cleanup_ipy_script(self, script):
2436 """Make a script safe for self.runlines()
2390 """Make a script safe for self.runlines()
2437
2391
2438 Currently, IPython is lines based, with blocks being detected by
2392 Currently, IPython is lines based, with blocks being detected by
2439 empty lines. This is a problem for block based scripts that may
2393 empty lines. This is a problem for block based scripts that may
2440 not have empty lines after blocks. This script adds those empty
2394 not have empty lines after blocks. This script adds those empty
2441 lines to make scripts safe for running in the current line based
2395 lines to make scripts safe for running in the current line based
2442 IPython.
2396 IPython.
2443 """
2397 """
2444 res = []
2398 res = []
2445 lines = script.splitlines()
2399 lines = script.splitlines()
2446 level = 0
2400 level = 0
2447
2401
2448 for l in lines:
2402 for l in lines:
2449 lstripped = l.lstrip()
2403 lstripped = l.lstrip()
2450 stripped = l.strip()
2404 stripped = l.strip()
2451 if not stripped:
2405 if not stripped:
2452 continue
2406 continue
2453 newlevel = len(l) - len(lstripped)
2407 newlevel = len(l) - len(lstripped)
2454 if level > 0 and newlevel == 0 and \
2408 if level > 0 and newlevel == 0 and \
2455 not self._is_secondary_block_start(stripped):
2409 not self._is_secondary_block_start(stripped):
2456 # add empty line
2410 # add empty line
2457 res.append('')
2411 res.append('')
2458 res.append(l)
2412 res.append(l)
2459 level = newlevel
2413 level = newlevel
2460
2414
2461 return '\n'.join(res) + '\n'
2415 return '\n'.join(res) + '\n'
2462
2416
2463 #-------------------------------------------------------------------------
2417 #-------------------------------------------------------------------------
2464 # Things related to GUI support and pylab
2418 # Things related to GUI support and pylab
2465 #-------------------------------------------------------------------------
2419 #-------------------------------------------------------------------------
2466
2420
2467 def enable_pylab(self, gui=None):
2421 def enable_pylab(self, gui=None):
2468 raise NotImplementedError('Implement enable_pylab in a subclass')
2422 raise NotImplementedError('Implement enable_pylab in a subclass')
2469
2423
2470 #-------------------------------------------------------------------------
2424 #-------------------------------------------------------------------------
2471 # Utilities
2425 # Utilities
2472 #-------------------------------------------------------------------------
2426 #-------------------------------------------------------------------------
2473
2427
2474 def var_expand(self,cmd,depth=0):
2428 def var_expand(self,cmd,depth=0):
2475 """Expand python variables in a string.
2429 """Expand python variables in a string.
2476
2430
2477 The depth argument indicates how many frames above the caller should
2431 The depth argument indicates how many frames above the caller should
2478 be walked to look for the local namespace where to expand variables.
2432 be walked to look for the local namespace where to expand variables.
2479
2433
2480 The global namespace for expansion is always the user's interactive
2434 The global namespace for expansion is always the user's interactive
2481 namespace.
2435 namespace.
2482 """
2436 """
2483 res = ItplNS(cmd, self.user_ns, # globals
2437 res = ItplNS(cmd, self.user_ns, # globals
2484 # Skip our own frame in searching for locals:
2438 # Skip our own frame in searching for locals:
2485 sys._getframe(depth+1).f_locals # locals
2439 sys._getframe(depth+1).f_locals # locals
2486 )
2440 )
2487 return str(res).decode(res.codec)
2441 return str(res).decode(res.codec)
2488
2442
2489 def mktempfile(self, data=None, prefix='ipython_edit_'):
2443 def mktempfile(self, data=None, prefix='ipython_edit_'):
2490 """Make a new tempfile and return its filename.
2444 """Make a new tempfile and return its filename.
2491
2445
2492 This makes a call to tempfile.mktemp, but it registers the created
2446 This makes a call to tempfile.mktemp, but it registers the created
2493 filename internally so ipython cleans it up at exit time.
2447 filename internally so ipython cleans it up at exit time.
2494
2448
2495 Optional inputs:
2449 Optional inputs:
2496
2450
2497 - data(None): if data is given, it gets written out to the temp file
2451 - data(None): if data is given, it gets written out to the temp file
2498 immediately, and the file is closed again."""
2452 immediately, and the file is closed again."""
2499
2453
2500 filename = tempfile.mktemp('.py', prefix)
2454 filename = tempfile.mktemp('.py', prefix)
2501 self.tempfiles.append(filename)
2455 self.tempfiles.append(filename)
2502
2456
2503 if data:
2457 if data:
2504 tmp_file = open(filename,'w')
2458 tmp_file = open(filename,'w')
2505 tmp_file.write(data)
2459 tmp_file.write(data)
2506 tmp_file.close()
2460 tmp_file.close()
2507 return filename
2461 return filename
2508
2462
2509 # TODO: This should be removed when Term is refactored.
2463 # TODO: This should be removed when Term is refactored.
2510 def write(self,data):
2464 def write(self,data):
2511 """Write a string to the default output"""
2465 """Write a string to the default output"""
2512 io.Term.cout.write(data)
2466 io.Term.cout.write(data)
2513
2467
2514 # TODO: This should be removed when Term is refactored.
2468 # TODO: This should be removed when Term is refactored.
2515 def write_err(self,data):
2469 def write_err(self,data):
2516 """Write a string to the default error output"""
2470 """Write a string to the default error output"""
2517 io.Term.cerr.write(data)
2471 io.Term.cerr.write(data)
2518
2472
2519 def ask_yes_no(self,prompt,default=True):
2473 def ask_yes_no(self,prompt,default=True):
2520 if self.quiet:
2474 if self.quiet:
2521 return True
2475 return True
2522 return ask_yes_no(prompt,default)
2476 return ask_yes_no(prompt,default)
2523
2477
2524 def show_usage(self):
2478 def show_usage(self):
2525 """Show a usage message"""
2479 """Show a usage message"""
2526 page.page(IPython.core.usage.interactive_usage)
2480 page.page(IPython.core.usage.interactive_usage)
2527
2481
2528 def find_user_code(self, target, raw=True):
2482 def find_user_code(self, target, raw=True):
2529 """Get a code string from history, file, or a string or macro.
2483 """Get a code string from history, file, or a string or macro.
2530
2484
2531 This is mainly used by magic functions.
2485 This is mainly used by magic functions.
2532
2486
2533 Parameters
2487 Parameters
2534 ----------
2488 ----------
2535 target : str
2489 target : str
2536 A string specifying code to retrieve. This will be tried respectively
2490 A string specifying code to retrieve. This will be tried respectively
2537 as: ranges of input history (see %history for syntax), a filename, or
2491 as: ranges of input history (see %history for syntax), a filename, or
2538 an expression evaluating to a string or Macro in the user namespace.
2492 an expression evaluating to a string or Macro in the user namespace.
2539 raw : bool
2493 raw : bool
2540 If true (default), retrieve raw history. Has no effect on the other
2494 If true (default), retrieve raw history. Has no effect on the other
2541 retrieval mechanisms.
2495 retrieval mechanisms.
2542
2496
2543 Returns
2497 Returns
2544 -------
2498 -------
2545 A string of code.
2499 A string of code.
2546
2500
2547 ValueError is raised if nothing is found, and TypeError if it evaluates
2501 ValueError is raised if nothing is found, and TypeError if it evaluates
2548 to an object of another type. In each case, .args[0] is a printable
2502 to an object of another type. In each case, .args[0] is a printable
2549 message.
2503 message.
2550 """
2504 """
2551 code = self.extract_input_lines(target, raw=raw) # Grab history
2505 code = self.extract_input_lines(target, raw=raw) # Grab history
2552 if code:
2506 if code:
2553 return code
2507 return code
2554 if os.path.isfile(target): # Read file
2508 if os.path.isfile(target): # Read file
2555 return open(target, "r").read()
2509 return open(target, "r").read()
2556
2510
2557 try: # User namespace
2511 try: # User namespace
2558 codeobj = eval(target, self.user_ns)
2512 codeobj = eval(target, self.user_ns)
2559 except Exception:
2513 except Exception:
2560 raise ValueError(("'%s' was not found in history, as a file, nor in"
2514 raise ValueError(("'%s' was not found in history, as a file, nor in"
2561 " the user namespace.") % target)
2515 " the user namespace.") % target)
2562 if isinstance(codeobj, basestring):
2516 if isinstance(codeobj, basestring):
2563 return codeobj
2517 return codeobj
2564 elif isinstance(codeobj, Macro):
2518 elif isinstance(codeobj, Macro):
2565 return codeobj.value
2519 return codeobj.value
2566
2520
2567 raise TypeError("%s is neither a string nor a macro." % target,
2521 raise TypeError("%s is neither a string nor a macro." % target,
2568 codeobj)
2522 codeobj)
2569
2523
2570 #-------------------------------------------------------------------------
2524 #-------------------------------------------------------------------------
2571 # Things related to IPython exiting
2525 # Things related to IPython exiting
2572 #-------------------------------------------------------------------------
2526 #-------------------------------------------------------------------------
2573 def atexit_operations(self):
2527 def atexit_operations(self):
2574 """This will be executed at the time of exit.
2528 """This will be executed at the time of exit.
2575
2529
2576 Cleanup operations and saving of persistent data that is done
2530 Cleanup operations and saving of persistent data that is done
2577 unconditionally by IPython should be performed here.
2531 unconditionally by IPython should be performed here.
2578
2532
2579 For things that may depend on startup flags or platform specifics (such
2533 For things that may depend on startup flags or platform specifics (such
2580 as having readline or not), register a separate atexit function in the
2534 as having readline or not), register a separate atexit function in the
2581 code that has the appropriate information, rather than trying to
2535 code that has the appropriate information, rather than trying to
2582 clutter
2536 clutter
2583 """
2537 """
2584 # Cleanup all tempfiles left around
2538 # Cleanup all tempfiles left around
2585 for tfile in self.tempfiles:
2539 for tfile in self.tempfiles:
2586 try:
2540 try:
2587 os.unlink(tfile)
2541 os.unlink(tfile)
2588 except OSError:
2542 except OSError:
2589 pass
2543 pass
2590
2544
2591 # Close the history session (this stores the end time and line count)
2545 # Close the history session (this stores the end time and line count)
2592 self.history_manager.end_session()
2546 self.history_manager.end_session()
2593
2547
2594 # Clear all user namespaces to release all references cleanly.
2548 # Clear all user namespaces to release all references cleanly.
2595 self.reset(new_session=False)
2549 self.reset(new_session=False)
2596
2550
2597 # Run user hooks
2551 # Run user hooks
2598 self.hooks.shutdown_hook()
2552 self.hooks.shutdown_hook()
2599
2553
2600 def cleanup(self):
2554 def cleanup(self):
2601 self.restore_sys_module_state()
2555 self.restore_sys_module_state()
2602
2556
2603
2557
2604 class InteractiveShellABC(object):
2558 class InteractiveShellABC(object):
2605 """An abstract base class for InteractiveShell."""
2559 """An abstract base class for InteractiveShell."""
2606 __metaclass__ = abc.ABCMeta
2560 __metaclass__ = abc.ABCMeta
2607
2561
2608 InteractiveShellABC.register(InteractiveShell)
2562 InteractiveShellABC.register(InteractiveShell)
@@ -1,271 +1,271 b''
1 """Tests for the key interactiveshell module, where the main ipython class is defined.
1 """Tests for the key interactiveshell module, where the main ipython class is defined.
2 """
2 """
3 #-----------------------------------------------------------------------------
3 #-----------------------------------------------------------------------------
4 # Module imports
4 # Module imports
5 #-----------------------------------------------------------------------------
5 #-----------------------------------------------------------------------------
6
6
7 # stdlib
7 # stdlib
8 import os
8 import os
9 import shutil
9 import shutil
10 import tempfile
10 import tempfile
11
11
12 # third party
12 # third party
13 import nose.tools as nt
13 import nose.tools as nt
14
14
15 # our own packages
15 # our own packages
16 from IPython.testing import decorators as dec
16 from IPython.testing import decorators as dec
17 from IPython.testing.globalipapp import get_ipython
17 from IPython.testing.globalipapp import get_ipython
18
18
19 #-----------------------------------------------------------------------------
19 #-----------------------------------------------------------------------------
20 # Globals
20 # Globals
21 #-----------------------------------------------------------------------------
21 #-----------------------------------------------------------------------------
22
22
23 # Get the public instance of IPython
23 # Get the public instance of IPython
24 ip = get_ipython()
24 ip = get_ipython()
25
25
26 #-----------------------------------------------------------------------------
26 #-----------------------------------------------------------------------------
27 # Test functions
27 # Test functions
28 #-----------------------------------------------------------------------------
28 #-----------------------------------------------------------------------------
29
29
30 @dec.parametric
30 @dec.parametric
31 def test_reset():
31 def test_reset():
32 """reset must clear most namespaces."""
32 """reset must clear most namespaces."""
33 # The number of variables in the private user_ns_hidden is not zero, but it
33 # The number of variables in the private user_ns_hidden is not zero, but it
34 # should be constant regardless of what we do
34 # should be constant regardless of what we do
35 nvars_config_ns = len(ip.user_ns_hidden)
35 nvars_config_ns = len(ip.user_ns_hidden)
36
36
37 # Check that reset runs without error
37 # Check that reset runs without error
38 ip.reset()
38 ip.reset()
39
39
40 # Once we've reset it (to clear of any junk that might have been there from
40 # Once we've reset it (to clear of any junk that might have been there from
41 # other tests, we can count how many variables are in the user's namespace
41 # other tests, we can count how many variables are in the user's namespace
42 nvars_user_ns = len(ip.user_ns)
42 nvars_user_ns = len(ip.user_ns)
43
43
44 # Now add a few variables to user_ns, and check that reset clears them
44 # Now add a few variables to user_ns, and check that reset clears them
45 ip.user_ns['x'] = 1
45 ip.user_ns['x'] = 1
46 ip.user_ns['y'] = 1
46 ip.user_ns['y'] = 1
47 ip.reset()
47 ip.reset()
48
48
49 # Finally, check that all namespaces have only as many variables as we
49 # Finally, check that all namespaces have only as many variables as we
50 # expect to find in them:
50 # expect to find in them:
51 for ns in ip.ns_refs_table:
51 for ns in ip.ns_refs_table:
52 if ns is ip.user_ns:
52 if ns is ip.user_ns:
53 nvars_expected = nvars_user_ns
53 nvars_expected = nvars_user_ns
54 elif ns is ip.user_ns_hidden:
54 elif ns is ip.user_ns_hidden:
55 nvars_expected = nvars_config_ns
55 nvars_expected = nvars_config_ns
56 else:
56 else:
57 nvars_expected = 0
57 nvars_expected = 0
58
58
59 yield nt.assert_equals(len(ns), nvars_expected)
59 yield nt.assert_equals(len(ns), nvars_expected)
60
60
61
61
62 # Tests for reporting of exceptions in various modes, handling of SystemExit,
62 # Tests for reporting of exceptions in various modes, handling of SystemExit,
63 # and %tb functionality. This is really a mix of testing ultraTB and interactiveshell.
63 # and %tb functionality. This is really a mix of testing ultraTB and interactiveshell.
64
64
65 def doctest_tb_plain():
65 def doctest_tb_plain():
66 """
66 """
67 In [18]: xmode plain
67 In [18]: xmode plain
68 Exception reporting mode: Plain
68 Exception reporting mode: Plain
69
69
70 In [19]: run simpleerr.py
70 In [19]: run simpleerr.py
71 Traceback (most recent call last):
71 Traceback (most recent call last):
72 ...line 32, in <module>
72 ...line 32, in <module>
73 bar(mode)
73 bar(mode)
74 ...line 16, in bar
74 ...line 16, in bar
75 div0()
75 div0()
76 ...line 8, in div0
76 ...line 8, in div0
77 x/y
77 x/y
78 ZeroDivisionError: integer division or modulo by zero
78 ZeroDivisionError: integer division or modulo by zero
79 """
79 """
80
80
81
81
82 def doctest_tb_context():
82 def doctest_tb_context():
83 """
83 """
84 In [3]: xmode context
84 In [3]: xmode context
85 Exception reporting mode: Context
85 Exception reporting mode: Context
86
86
87 In [4]: run simpleerr.py
87 In [4]: run simpleerr.py
88 ---------------------------------------------------------------------------
88 ---------------------------------------------------------------------------
89 ZeroDivisionError Traceback (most recent call last)
89 ZeroDivisionError Traceback (most recent call last)
90 <BLANKLINE>
90 <BLANKLINE>
91 ... in <module>()
91 ... in <module>()
92 30 mode = 'div'
92 30 mode = 'div'
93 31
93 31
94 ---> 32 bar(mode)
94 ---> 32 bar(mode)
95 <BLANKLINE>
95 <BLANKLINE>
96 ... in bar(mode)
96 ... in bar(mode)
97 14 "bar"
97 14 "bar"
98 15 if mode=='div':
98 15 if mode=='div':
99 ---> 16 div0()
99 ---> 16 div0()
100 17 elif mode=='exit':
100 17 elif mode=='exit':
101 18 try:
101 18 try:
102 <BLANKLINE>
102 <BLANKLINE>
103 ... in div0()
103 ... in div0()
104 6 x = 1
104 6 x = 1
105 7 y = 0
105 7 y = 0
106 ----> 8 x/y
106 ----> 8 x/y
107 9
107 9
108 10 def sysexit(stat, mode):
108 10 def sysexit(stat, mode):
109 <BLANKLINE>
109 <BLANKLINE>
110 ZeroDivisionError: integer division or modulo by zero
110 ZeroDivisionError: integer division or modulo by zero
111 """
111 """
112
112
113
113
114 def doctest_tb_verbose():
114 def doctest_tb_verbose():
115 """
115 """
116 In [5]: xmode verbose
116 In [5]: xmode verbose
117 Exception reporting mode: Verbose
117 Exception reporting mode: Verbose
118
118
119 In [6]: run simpleerr.py
119 In [6]: run simpleerr.py
120 ---------------------------------------------------------------------------
120 ---------------------------------------------------------------------------
121 ZeroDivisionError Traceback (most recent call last)
121 ZeroDivisionError Traceback (most recent call last)
122 <BLANKLINE>
122 <BLANKLINE>
123 ... in <module>()
123 ... in <module>()
124 30 mode = 'div'
124 30 mode = 'div'
125 31
125 31
126 ---> 32 bar(mode)
126 ---> 32 bar(mode)
127 global bar = <function bar at ...>
127 global bar = <function bar at ...>
128 global mode = 'div'
128 global mode = 'div'
129 <BLANKLINE>
129 <BLANKLINE>
130 ... in bar(mode='div')
130 ... in bar(mode='div')
131 14 "bar"
131 14 "bar"
132 15 if mode=='div':
132 15 if mode=='div':
133 ---> 16 div0()
133 ---> 16 div0()
134 global div0 = <function div0 at ...>
134 global div0 = <function div0 at ...>
135 17 elif mode=='exit':
135 17 elif mode=='exit':
136 18 try:
136 18 try:
137 <BLANKLINE>
137 <BLANKLINE>
138 ... in div0()
138 ... in div0()
139 6 x = 1
139 6 x = 1
140 7 y = 0
140 7 y = 0
141 ----> 8 x/y
141 ----> 8 x/y
142 x = 1
142 x = 1
143 y = 0
143 y = 0
144 9
144 9
145 10 def sysexit(stat, mode):
145 10 def sysexit(stat, mode):
146 <BLANKLINE>
146 <BLANKLINE>
147 ZeroDivisionError: integer division or modulo by zero
147 ZeroDivisionError: integer division or modulo by zero
148 """
148 """
149
149
150
150
151 def doctest_tb_sysexit():
151 def doctest_tb_sysexit():
152 """
152 """
153 In [17]: %xmode plain
153 In [17]: %xmode plain
154 Exception reporting mode: Plain
154 Exception reporting mode: Plain
155
155
156 In [18]: %run simpleerr.py exit
156 In [18]: %run simpleerr.py exit
157 An exception has occurred, use %tb to see the full traceback.
157 An exception has occurred, use %tb to see the full traceback.
158 SystemExit: (1, u'Mode = exit')
158 SystemExit: (1, u'Mode = exit')
159
159
160 In [19]: %run simpleerr.py exit 2
160 In [19]: %run simpleerr.py exit 2
161 An exception has occurred, use %tb to see the full traceback.
161 An exception has occurred, use %tb to see the full traceback.
162 SystemExit: (2, u'Mode = exit')
162 SystemExit: (2, u'Mode = exit')
163
163
164 In [20]: %tb
164 In [20]: %tb
165 Traceback (most recent call last):
165 Traceback (most recent call last):
166 File ... in <module>
166 File ... in <module>
167 bar(mode)
167 bar(mode)
168 File ... line 22, in bar
168 File ... line 22, in bar
169 sysexit(stat, mode)
169 sysexit(stat, mode)
170 File ... line 11, in sysexit
170 File ... line 11, in sysexit
171 raise SystemExit(stat, 'Mode = %s' % mode)
171 raise SystemExit(stat, 'Mode = %s' % mode)
172 SystemExit: (2, u'Mode = exit')
172 SystemExit: (2, u'Mode = exit')
173
173
174 In [21]: %xmode context
174 In [21]: %xmode context
175 Exception reporting mode: Context
175 Exception reporting mode: Context
176
176
177 In [22]: %tb
177 In [22]: %tb
178 ---------------------------------------------------------------------------
178 ---------------------------------------------------------------------------
179 SystemExit Traceback (most recent call last)
179 SystemExit Traceback (most recent call last)
180 <BLANKLINE>
180 <BLANKLINE>
181 ...<module>()
181 ...<module>()
182 30 mode = 'div'
182 30 mode = 'div'
183 31
183 31
184 ---> 32 bar(mode)
184 ---> 32 bar(mode)
185 <BLANKLINE>
185 <BLANKLINE>
186 ...bar(mode)
186 ...bar(mode)
187 20 except:
187 20 except:
188 21 stat = 1
188 21 stat = 1
189 ---> 22 sysexit(stat, mode)
189 ---> 22 sysexit(stat, mode)
190 23 else:
190 23 else:
191 24 raise ValueError('Unknown mode')
191 24 raise ValueError('Unknown mode')
192 <BLANKLINE>
192 <BLANKLINE>
193 ...sysexit(stat, mode)
193 ...sysexit(stat, mode)
194 9
194 9
195 10 def sysexit(stat, mode):
195 10 def sysexit(stat, mode):
196 ---> 11 raise SystemExit(stat, 'Mode = %s' % mode)
196 ---> 11 raise SystemExit(stat, 'Mode = %s' % mode)
197 12
197 12
198 13 def bar(mode):
198 13 def bar(mode):
199 <BLANKLINE>
199 <BLANKLINE>
200 SystemExit: (2, u'Mode = exit')
200 SystemExit: (2, u'Mode = exit')
201
201
202 In [23]: %xmode verbose
202 In [23]: %xmode verbose
203 Exception reporting mode: Verbose
203 Exception reporting mode: Verbose
204
204
205 In [24]: %tb
205 In [24]: %tb
206 ---------------------------------------------------------------------------
206 ---------------------------------------------------------------------------
207 SystemExit Traceback (most recent call last)
207 SystemExit Traceback (most recent call last)
208 <BLANKLINE>
208 <BLANKLINE>
209 ... in <module>()
209 ... in <module>()
210 30 mode = 'div'
210 30 mode = 'div'
211 31
211 31
212 ---> 32 bar(mode)
212 ---> 32 bar(mode)
213 global bar = <function bar at ...>
213 global bar = <function bar at ...>
214 global mode = u'exit'
214 global mode = u'exit'
215 <BLANKLINE>
215 <BLANKLINE>
216 ... in bar(mode=u'exit')
216 ... in bar(mode=u'exit')
217 20 except:
217 20 except:
218 21 stat = 1
218 21 stat = 1
219 ---> 22 sysexit(stat, mode)
219 ---> 22 sysexit(stat, mode)
220 global sysexit = <function sysexit at ...>
220 global sysexit = <function sysexit at ...>
221 stat = 2
221 stat = 2
222 mode = u'exit'
222 mode = u'exit'
223 23 else:
223 23 else:
224 24 raise ValueError('Unknown mode')
224 24 raise ValueError('Unknown mode')
225 <BLANKLINE>
225 <BLANKLINE>
226 ... in sysexit(stat=2, mode=u'exit')
226 ... in sysexit(stat=2, mode=u'exit')
227 9
227 9
228 10 def sysexit(stat, mode):
228 10 def sysexit(stat, mode):
229 ---> 11 raise SystemExit(stat, 'Mode = %s' % mode)
229 ---> 11 raise SystemExit(stat, 'Mode = %s' % mode)
230 global SystemExit = undefined
230 global SystemExit = undefined
231 stat = 2
231 stat = 2
232 mode = u'exit'
232 mode = u'exit'
233 12
233 12
234 13 def bar(mode):
234 13 def bar(mode):
235 <BLANKLINE>
235 <BLANKLINE>
236 SystemExit: (2, u'Mode = exit')
236 SystemExit: (2, u'Mode = exit')
237 """
237 """
238
238
239
239
240 def test_runlines():
240 def test_run_cell():
241 import textwrap
241 import textwrap
242 ip.runlines(['a = 10', 'a+=1'])
242 ip.run_cell('a = 10\na+=1')
243 ip.runlines('assert a == 11\nassert 1')
243 ip.run_cell('assert a == 11\nassert 1')
244
244
245 nt.assert_equals(ip.user_ns['a'], 11)
245 nt.assert_equals(ip.user_ns['a'], 11)
246 complex = textwrap.dedent("""
246 complex = textwrap.dedent("""
247 if 1:
247 if 1:
248 print "hello"
248 print "hello"
249 if 1:
249 if 1:
250 print "world"
250 print "world"
251
251
252 if 2:
252 if 2:
253 print "foo"
253 print "foo"
254
254
255 if 3:
255 if 3:
256 print "bar"
256 print "bar"
257
257
258 if 4:
258 if 4:
259 print "bar"
259 print "bar"
260
260
261 """)
261 """)
262 # Simply verifies that this kind of input is run
262 # Simply verifies that this kind of input is run
263 ip.runlines(complex)
263 ip.run_cell(complex)
264
264
265
265
266 def test_db():
266 def test_db():
267 """Test the internal database used for variable persistence."""
267 """Test the internal database used for variable persistence."""
268 ip.db['__unittest_'] = 12
268 ip.db['__unittest_'] = 12
269 nt.assert_equals(ip.db['__unittest_'], 12)
269 nt.assert_equals(ip.db['__unittest_'], 12)
270 del ip.db['__unittest_']
270 del ip.db['__unittest_']
271 assert '__unittest_' not in ip.db
271 assert '__unittest_' not in ip.db
@@ -1,206 +1,206 b''
1 """Tests for code execution (%run and related), which is particularly tricky.
1 """Tests for code execution (%run and related), which is particularly tricky.
2
2
3 Because of how %run manages namespaces, and the fact that we are trying here to
3 Because of how %run manages namespaces, and the fact that we are trying here to
4 verify subtle object deletion and reference counting issues, the %run tests
4 verify subtle object deletion and reference counting issues, the %run tests
5 will be kept in this separate file. This makes it easier to aggregate in one
5 will be kept in this separate file. This makes it easier to aggregate in one
6 place the tricks needed to handle it; most other magics are much easier to test
6 place the tricks needed to handle it; most other magics are much easier to test
7 and we do so in a common test_magic file.
7 and we do so in a common test_magic file.
8 """
8 """
9 from __future__ import absolute_import
9 from __future__ import absolute_import
10
10
11 #-----------------------------------------------------------------------------
11 #-----------------------------------------------------------------------------
12 # Imports
12 # Imports
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14
14
15 import os
15 import os
16 import sys
16 import sys
17 import tempfile
17 import tempfile
18
18
19 import nose.tools as nt
19 import nose.tools as nt
20
20
21 from IPython.testing import decorators as dec
21 from IPython.testing import decorators as dec
22 from IPython.testing import tools as tt
22 from IPython.testing import tools as tt
23
23
24 #-----------------------------------------------------------------------------
24 #-----------------------------------------------------------------------------
25 # Test functions begin
25 # Test functions begin
26 #-----------------------------------------------------------------------------
26 #-----------------------------------------------------------------------------
27
27
28 def doctest_refbug():
28 def doctest_refbug():
29 """Very nasty problem with references held by multiple runs of a script.
29 """Very nasty problem with references held by multiple runs of a script.
30 See: https://bugs.launchpad.net/ipython/+bug/269966
30 See: https://bugs.launchpad.net/ipython/+bug/269966
31
31
32 In [1]: _ip.clear_main_mod_cache()
32 In [1]: _ip.clear_main_mod_cache()
33 # random
33 # random
34
34
35 In [2]: %run refbug
35 In [2]: %run refbug
36
36
37 In [3]: call_f()
37 In [3]: call_f()
38 lowercased: hello
38 lowercased: hello
39
39
40 In [4]: %run refbug
40 In [4]: %run refbug
41
41
42 In [5]: call_f()
42 In [5]: call_f()
43 lowercased: hello
43 lowercased: hello
44 lowercased: hello
44 lowercased: hello
45 """
45 """
46
46
47
47
48 def doctest_run_builtins():
48 def doctest_run_builtins():
49 r"""Check that %run doesn't damage __builtins__.
49 r"""Check that %run doesn't damage __builtins__.
50
50
51 In [1]: import tempfile
51 In [1]: import tempfile
52
52
53 In [2]: bid1 = id(__builtins__)
53 In [2]: bid1 = id(__builtins__)
54
54
55 In [3]: fname = tempfile.mkstemp('.py')[1]
55 In [3]: fname = tempfile.mkstemp('.py')[1]
56
56
57 In [3]: f = open(fname,'w')
57 In [3]: f = open(fname,'w')
58
58
59 In [4]: f.write('pass\n')
59 In [4]: f.write('pass\n')
60
60
61 In [5]: f.flush()
61 In [5]: f.flush()
62
62
63 In [6]: t1 = type(__builtins__)
63 In [6]: t1 = type(__builtins__)
64
64
65 In [7]: %run $fname
65 In [7]: %run $fname
66
66
67 In [7]: f.close()
67 In [7]: f.close()
68
68
69 In [8]: bid2 = id(__builtins__)
69 In [8]: bid2 = id(__builtins__)
70
70
71 In [9]: t2 = type(__builtins__)
71 In [9]: t2 = type(__builtins__)
72
72
73 In [10]: t1 == t2
73 In [10]: t1 == t2
74 Out[10]: True
74 Out[10]: True
75
75
76 In [10]: bid1 == bid2
76 In [10]: bid1 == bid2
77 Out[10]: True
77 Out[10]: True
78
78
79 In [12]: try:
79 In [12]: try:
80 ....: os.unlink(fname)
80 ....: os.unlink(fname)
81 ....: except:
81 ....: except:
82 ....: pass
82 ....: pass
83 ....:
83 ....:
84 """
84 """
85
85
86 def doctest_reset_del():
86 def doctest_reset_del():
87 """Test that resetting doesn't cause errors in __del__ methods.
87 """Test that resetting doesn't cause errors in __del__ methods.
88
88
89 In [2]: class A(object):
89 In [2]: class A(object):
90 ...: def __del__(self):
90 ...: def __del__(self):
91 ...: print str("Hi")
91 ...: print str("Hi")
92 ...:
92 ...:
93
93
94 In [3]: a = A()
94 In [3]: a = A()
95
95
96 In [4]: get_ipython().reset()
96 In [4]: get_ipython().reset()
97 Hi
97 Hi
98
98
99 In [5]: 1+1
99 In [5]: 1+1
100 Out[5]: 2
100 Out[5]: 2
101 """
101 """
102
102
103 # For some tests, it will be handy to organize them in a class with a common
103 # For some tests, it will be handy to organize them in a class with a common
104 # setup that makes a temp file
104 # setup that makes a temp file
105
105
106 class TestMagicRunPass(tt.TempFileMixin):
106 class TestMagicRunPass(tt.TempFileMixin):
107
107
108 def setup(self):
108 def setup(self):
109 """Make a valid python temp file."""
109 """Make a valid python temp file."""
110 self.mktmp('pass\n')
110 self.mktmp('pass\n')
111
111
112 def run_tmpfile(self):
112 def run_tmpfile(self):
113 _ip = get_ipython()
113 _ip = get_ipython()
114 # This fails on Windows if self.tmpfile.name has spaces or "~" in it.
114 # This fails on Windows if self.tmpfile.name has spaces or "~" in it.
115 # See below and ticket https://bugs.launchpad.net/bugs/366353
115 # See below and ticket https://bugs.launchpad.net/bugs/366353
116 _ip.magic('run %s' % self.fname)
116 _ip.magic('run %s' % self.fname)
117
117
118 def test_builtins_id(self):
118 def test_builtins_id(self):
119 """Check that %run doesn't damage __builtins__ """
119 """Check that %run doesn't damage __builtins__ """
120 _ip = get_ipython()
120 _ip = get_ipython()
121 # Test that the id of __builtins__ is not modified by %run
121 # Test that the id of __builtins__ is not modified by %run
122 bid1 = id(_ip.user_ns['__builtins__'])
122 bid1 = id(_ip.user_ns['__builtins__'])
123 self.run_tmpfile()
123 self.run_tmpfile()
124 bid2 = id(_ip.user_ns['__builtins__'])
124 bid2 = id(_ip.user_ns['__builtins__'])
125 tt.assert_equals(bid1, bid2)
125 tt.assert_equals(bid1, bid2)
126
126
127 def test_builtins_type(self):
127 def test_builtins_type(self):
128 """Check that the type of __builtins__ doesn't change with %run.
128 """Check that the type of __builtins__ doesn't change with %run.
129
129
130 However, the above could pass if __builtins__ was already modified to
130 However, the above could pass if __builtins__ was already modified to
131 be a dict (it should be a module) by a previous use of %run. So we
131 be a dict (it should be a module) by a previous use of %run. So we
132 also check explicitly that it really is a module:
132 also check explicitly that it really is a module:
133 """
133 """
134 _ip = get_ipython()
134 _ip = get_ipython()
135 self.run_tmpfile()
135 self.run_tmpfile()
136 tt.assert_equals(type(_ip.user_ns['__builtins__']),type(sys))
136 tt.assert_equals(type(_ip.user_ns['__builtins__']),type(sys))
137
137
138 def test_prompts(self):
138 def test_prompts(self):
139 """Test that prompts correctly generate after %run"""
139 """Test that prompts correctly generate after %run"""
140 self.run_tmpfile()
140 self.run_tmpfile()
141 _ip = get_ipython()
141 _ip = get_ipython()
142 p2 = str(_ip.displayhook.prompt2).strip()
142 p2 = str(_ip.displayhook.prompt2).strip()
143 nt.assert_equals(p2[:3], '...')
143 nt.assert_equals(p2[:3], '...')
144
144
145
145
146 class TestMagicRunSimple(tt.TempFileMixin):
146 class TestMagicRunSimple(tt.TempFileMixin):
147
147
148 def test_simpledef(self):
148 def test_simpledef(self):
149 """Test that simple class definitions work."""
149 """Test that simple class definitions work."""
150 src = ("class foo: pass\n"
150 src = ("class foo: pass\n"
151 "def f(): return foo()")
151 "def f(): return foo()")
152 self.mktmp(src)
152 self.mktmp(src)
153 _ip.magic('run %s' % self.fname)
153 _ip.magic('run %s' % self.fname)
154 _ip.runlines('t = isinstance(f(), foo)')
154 _ip.run_cell('t = isinstance(f(), foo)')
155 nt.assert_true(_ip.user_ns['t'])
155 nt.assert_true(_ip.user_ns['t'])
156
156
157 # We have to skip these in win32 because getoutputerr() crashes,
157 # We have to skip these in win32 because getoutputerr() crashes,
158 # due to the fact that subprocess does not support close_fds when
158 # due to the fact that subprocess does not support close_fds when
159 # redirecting stdout/err. So unless someone who knows more tells us how to
159 # redirecting stdout/err. So unless someone who knows more tells us how to
160 # implement getoutputerr() in win32, we're stuck avoiding these.
160 # implement getoutputerr() in win32, we're stuck avoiding these.
161 @dec.skip_win32
161 @dec.skip_win32
162 def test_obj_del(self):
162 def test_obj_del(self):
163 """Test that object's __del__ methods are called on exit."""
163 """Test that object's __del__ methods are called on exit."""
164
164
165 # This test is known to fail on win32.
165 # This test is known to fail on win32.
166 # See ticket https://bugs.launchpad.net/bugs/366334
166 # See ticket https://bugs.launchpad.net/bugs/366334
167 src = ("class A(object):\n"
167 src = ("class A(object):\n"
168 " def __del__(self):\n"
168 " def __del__(self):\n"
169 " print 'object A deleted'\n"
169 " print 'object A deleted'\n"
170 "a = A()\n")
170 "a = A()\n")
171 self.mktmp(src)
171 self.mktmp(src)
172 tt.ipexec_validate(self.fname, 'object A deleted')
172 tt.ipexec_validate(self.fname, 'object A deleted')
173
173
174 @dec.skip_known_failure
174 @dec.skip_known_failure
175 def test_aggressive_namespace_cleanup(self):
175 def test_aggressive_namespace_cleanup(self):
176 """Test that namespace cleanup is not too aggressive GH-238
176 """Test that namespace cleanup is not too aggressive GH-238
177
177
178 Returning from another run magic deletes the namespace"""
178 Returning from another run magic deletes the namespace"""
179 # see ticket https://github.com/ipython/ipython/issues/238
179 # see ticket https://github.com/ipython/ipython/issues/238
180 class secondtmp(tt.TempFileMixin): pass
180 class secondtmp(tt.TempFileMixin): pass
181 empty = secondtmp()
181 empty = secondtmp()
182 empty.mktmp('')
182 empty.mktmp('')
183 src = ("ip = get_ipython()\n"
183 src = ("ip = get_ipython()\n"
184 "for i in range(5):\n"
184 "for i in range(5):\n"
185 " try:\n"
185 " try:\n"
186 " ip.magic('run %s')\n"
186 " ip.magic('run %s')\n"
187 " except NameError, e:\n"
187 " except NameError, e:\n"
188 " print i;break\n" % empty.fname)
188 " print i;break\n" % empty.fname)
189 self.mktmp(src)
189 self.mktmp(src)
190 _ip.magic('run %s' % self.fname)
190 _ip.magic('run %s' % self.fname)
191 _ip.runlines('ip == get_ipython()')
191 _ip.run_cell('ip == get_ipython()')
192 tt.assert_equals(_ip.user_ns['i'], 5)
192 tt.assert_equals(_ip.user_ns['i'], 5)
193
193
194 @dec.skip_win32
194 @dec.skip_win32
195 def test_tclass(self):
195 def test_tclass(self):
196 mydir = os.path.dirname(__file__)
196 mydir = os.path.dirname(__file__)
197 tc = os.path.join(mydir, 'tclass')
197 tc = os.path.join(mydir, 'tclass')
198 src = ("%%run '%s' C-first\n"
198 src = ("%%run '%s' C-first\n"
199 "%%run '%s' C-second\n") % (tc, tc)
199 "%%run '%s' C-second\n") % (tc, tc)
200 self.mktmp(src, '.ipy')
200 self.mktmp(src, '.ipy')
201 out = """\
201 out = """\
202 ARGV 1-: [u'C-first']
202 ARGV 1-: [u'C-first']
203 ARGV 1-: [u'C-second']
203 ARGV 1-: [u'C-second']
204 tclass.py: deleting object: C-first
204 tclass.py: deleting object: C-first
205 """
205 """
206 tt.ipexec_validate(self.fname, out)
206 tt.ipexec_validate(self.fname, out)
@@ -1,658 +1,658 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 # System library imports.
25 # System library imports.
26 import zmq
26 import zmq
27
27
28 # Local imports.
28 # Local imports.
29 from IPython.config.configurable import Configurable
29 from IPython.config.configurable import Configurable
30 from IPython.utils import io
30 from IPython.utils import io
31 from IPython.utils.jsonutil import json_clean
31 from IPython.utils.jsonutil import json_clean
32 from IPython.lib import pylabtools
32 from IPython.lib import pylabtools
33 from IPython.utils.traitlets import Instance, Float
33 from IPython.utils.traitlets import Instance, Float
34 from entry_point import (base_launch_kernel, make_argument_parser, make_kernel,
34 from entry_point import (base_launch_kernel, make_argument_parser, make_kernel,
35 start_kernel)
35 start_kernel)
36 from iostream import OutStream
36 from iostream import OutStream
37 from session import Session, Message
37 from session import Session, Message
38 from zmqshell import ZMQInteractiveShell
38 from zmqshell import ZMQInteractiveShell
39
39
40 #-----------------------------------------------------------------------------
40 #-----------------------------------------------------------------------------
41 # Globals
41 # Globals
42 #-----------------------------------------------------------------------------
42 #-----------------------------------------------------------------------------
43
43
44 # Module-level logger
44 # Module-level logger
45 logger = logging.getLogger(__name__)
45 logger = logging.getLogger(__name__)
46
46
47 # FIXME: this needs to be done more cleanly later, once we have proper
47 # FIXME: this needs to be done more cleanly later, once we have proper
48 # configuration support. This is a library, so it shouldn't set a stream
48 # configuration support. This is a library, so it shouldn't set a stream
49 # handler, see:
49 # handler, see:
50 # http://docs.python.org/library/logging.html#configuring-logging-for-a-library
50 # http://docs.python.org/library/logging.html#configuring-logging-for-a-library
51 # But this lets us at least do developer debugging for now by manually turning
51 # But this lets us at least do developer debugging for now by manually turning
52 # it on/off. And once we have full config support, the client entry points
52 # it on/off. And once we have full config support, the client entry points
53 # will select their logging handlers, as well as passing to this library the
53 # will select their logging handlers, as well as passing to this library the
54 # logging level.
54 # logging level.
55
55
56 if 0: # dbg - set to 1 to actually see the messages.
56 if 0: # dbg - set to 1 to actually see the messages.
57 logger.addHandler(logging.StreamHandler())
57 logger.addHandler(logging.StreamHandler())
58 logger.setLevel(logging.DEBUG)
58 logger.setLevel(logging.DEBUG)
59
59
60 # /FIXME
60 # /FIXME
61
61
62 #-----------------------------------------------------------------------------
62 #-----------------------------------------------------------------------------
63 # Main kernel class
63 # Main kernel class
64 #-----------------------------------------------------------------------------
64 #-----------------------------------------------------------------------------
65
65
66 class Kernel(Configurable):
66 class Kernel(Configurable):
67
67
68 #---------------------------------------------------------------------------
68 #---------------------------------------------------------------------------
69 # Kernel interface
69 # Kernel interface
70 #---------------------------------------------------------------------------
70 #---------------------------------------------------------------------------
71
71
72 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC')
72 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC')
73 session = Instance(Session)
73 session = Instance(Session)
74 reply_socket = Instance('zmq.Socket')
74 reply_socket = Instance('zmq.Socket')
75 pub_socket = Instance('zmq.Socket')
75 pub_socket = Instance('zmq.Socket')
76 req_socket = Instance('zmq.Socket')
76 req_socket = Instance('zmq.Socket')
77
77
78 # Private interface
78 # Private interface
79
79
80 # Time to sleep after flushing the stdout/err buffers in each execute
80 # Time to sleep after flushing the stdout/err buffers in each execute
81 # cycle. While this introduces a hard limit on the minimal latency of the
81 # cycle. While this introduces a hard limit on the minimal latency of the
82 # execute cycle, it helps prevent output synchronization problems for
82 # execute cycle, it helps prevent output synchronization problems for
83 # clients.
83 # clients.
84 # Units are in seconds. The minimum zmq latency on local host is probably
84 # Units are in seconds. The minimum zmq latency on local host is probably
85 # ~150 microseconds, set this to 500us for now. We may need to increase it
85 # ~150 microseconds, set this to 500us for now. We may need to increase it
86 # a little if it's not enough after more interactive testing.
86 # a little if it's not enough after more interactive testing.
87 _execute_sleep = Float(0.0005, config=True)
87 _execute_sleep = Float(0.0005, config=True)
88
88
89 # Frequency of the kernel's event loop.
89 # Frequency of the kernel's event loop.
90 # Units are in seconds, kernel subclasses for GUI toolkits may need to
90 # Units are in seconds, kernel subclasses for GUI toolkits may need to
91 # adapt to milliseconds.
91 # adapt to milliseconds.
92 _poll_interval = Float(0.05, config=True)
92 _poll_interval = Float(0.05, config=True)
93
93
94 # If the shutdown was requested over the network, we leave here the
94 # If the shutdown was requested over the network, we leave here the
95 # necessary reply message so it can be sent by our registered atexit
95 # necessary reply message so it can be sent by our registered atexit
96 # handler. This ensures that the reply is only sent to clients truly at
96 # handler. This ensures that the reply is only sent to clients truly at
97 # the end of our shutdown process (which happens after the underlying
97 # the end of our shutdown process (which happens after the underlying
98 # IPython shell's own shutdown).
98 # IPython shell's own shutdown).
99 _shutdown_message = None
99 _shutdown_message = None
100
100
101 # This is a dict of port number that the kernel is listening on. It is set
101 # This is a dict of port number that the kernel is listening on. It is set
102 # by record_ports and used by connect_request.
102 # by record_ports and used by connect_request.
103 _recorded_ports = None
103 _recorded_ports = None
104
104
105
105
106 def __init__(self, **kwargs):
106 def __init__(self, **kwargs):
107 super(Kernel, self).__init__(**kwargs)
107 super(Kernel, self).__init__(**kwargs)
108
108
109 # Before we even start up the shell, register *first* our exit handlers
109 # Before we even start up the shell, register *first* our exit handlers
110 # so they come before the shell's
110 # so they come before the shell's
111 atexit.register(self._at_shutdown)
111 atexit.register(self._at_shutdown)
112
112
113 # Initialize the InteractiveShell subclass
113 # Initialize the InteractiveShell subclass
114 self.shell = ZMQInteractiveShell.instance()
114 self.shell = ZMQInteractiveShell.instance()
115 self.shell.displayhook.session = self.session
115 self.shell.displayhook.session = self.session
116 self.shell.displayhook.pub_socket = self.pub_socket
116 self.shell.displayhook.pub_socket = self.pub_socket
117 self.shell.display_pub.session = self.session
117 self.shell.display_pub.session = self.session
118 self.shell.display_pub.pub_socket = self.pub_socket
118 self.shell.display_pub.pub_socket = self.pub_socket
119
119
120 # TMP - hack while developing
120 # TMP - hack while developing
121 self.shell._reply_content = None
121 self.shell._reply_content = None
122
122
123 # Build dict of handlers for message types
123 # Build dict of handlers for message types
124 msg_types = [ 'execute_request', 'complete_request',
124 msg_types = [ 'execute_request', 'complete_request',
125 'object_info_request', 'history_tail_request',
125 'object_info_request', 'history_tail_request',
126 'connect_request', 'shutdown_request']
126 'connect_request', 'shutdown_request']
127 self.handlers = {}
127 self.handlers = {}
128 for msg_type in msg_types:
128 for msg_type in msg_types:
129 self.handlers[msg_type] = getattr(self, msg_type)
129 self.handlers[msg_type] = getattr(self, msg_type)
130
130
131 def do_one_iteration(self):
131 def do_one_iteration(self):
132 """Do one iteration of the kernel's evaluation loop.
132 """Do one iteration of the kernel's evaluation loop.
133 """
133 """
134 ident,msg = self.session.recv(self.reply_socket, zmq.NOBLOCK)
134 ident,msg = self.session.recv(self.reply_socket, zmq.NOBLOCK)
135 if msg is None:
135 if msg is None:
136 return
136 return
137
137
138 # This assert will raise in versions of zeromq 2.0.7 and lesser.
138 # This assert will raise in versions of zeromq 2.0.7 and lesser.
139 # We now require 2.0.8 or above, so we can uncomment for safety.
139 # We now require 2.0.8 or above, so we can uncomment for safety.
140 # print(ident,msg, file=sys.__stdout__)
140 # print(ident,msg, file=sys.__stdout__)
141 assert ident is not None, "Missing message part."
141 assert ident is not None, "Missing message part."
142
142
143 # Print some info about this message and leave a '--->' marker, so it's
143 # Print some info about this message and leave a '--->' marker, so it's
144 # easier to trace visually the message chain when debugging. Each
144 # easier to trace visually the message chain when debugging. Each
145 # handler prints its message at the end.
145 # handler prints its message at the end.
146 # Eventually we'll move these from stdout to a logger.
146 # Eventually we'll move these from stdout to a logger.
147 logger.debug('\n*** MESSAGE TYPE:'+str(msg['msg_type'])+'***')
147 logger.debug('\n*** MESSAGE TYPE:'+str(msg['msg_type'])+'***')
148 logger.debug(' Content: '+str(msg['content'])+'\n --->\n ')
148 logger.debug(' Content: '+str(msg['content'])+'\n --->\n ')
149
149
150 # Find and call actual handler for message
150 # Find and call actual handler for message
151 handler = self.handlers.get(msg['msg_type'], None)
151 handler = self.handlers.get(msg['msg_type'], None)
152 if handler is None:
152 if handler is None:
153 logger.error("UNKNOWN MESSAGE TYPE:" +str(msg))
153 logger.error("UNKNOWN MESSAGE TYPE:" +str(msg))
154 else:
154 else:
155 handler(ident, msg)
155 handler(ident, msg)
156
156
157 # Check whether we should exit, in case the incoming message set the
157 # Check whether we should exit, in case the incoming message set the
158 # exit flag on
158 # exit flag on
159 if self.shell.exit_now:
159 if self.shell.exit_now:
160 logger.debug('\nExiting IPython kernel...')
160 logger.debug('\nExiting IPython kernel...')
161 # We do a normal, clean exit, which allows any actions registered
161 # We do a normal, clean exit, which allows any actions registered
162 # via atexit (such as history saving) to take place.
162 # via atexit (such as history saving) to take place.
163 sys.exit(0)
163 sys.exit(0)
164
164
165
165
166 def start(self):
166 def start(self):
167 """ Start the kernel main loop.
167 """ Start the kernel main loop.
168 """
168 """
169 while True:
169 while True:
170 time.sleep(self._poll_interval)
170 time.sleep(self._poll_interval)
171 self.do_one_iteration()
171 self.do_one_iteration()
172
172
173 def record_ports(self, xrep_port, pub_port, req_port, hb_port):
173 def record_ports(self, xrep_port, pub_port, req_port, hb_port):
174 """Record the ports that this kernel is using.
174 """Record the ports that this kernel is using.
175
175
176 The creator of the Kernel instance must call this methods if they
176 The creator of the Kernel instance must call this methods if they
177 want the :meth:`connect_request` method to return the port numbers.
177 want the :meth:`connect_request` method to return the port numbers.
178 """
178 """
179 self._recorded_ports = {
179 self._recorded_ports = {
180 'xrep_port' : xrep_port,
180 'xrep_port' : xrep_port,
181 'pub_port' : pub_port,
181 'pub_port' : pub_port,
182 'req_port' : req_port,
182 'req_port' : req_port,
183 'hb_port' : hb_port
183 'hb_port' : hb_port
184 }
184 }
185
185
186 #---------------------------------------------------------------------------
186 #---------------------------------------------------------------------------
187 # Kernel request handlers
187 # Kernel request handlers
188 #---------------------------------------------------------------------------
188 #---------------------------------------------------------------------------
189
189
190 def _publish_pyin(self, code, parent):
190 def _publish_pyin(self, code, parent):
191 """Publish the code request on the pyin stream."""
191 """Publish the code request on the pyin stream."""
192
192
193 pyin_msg = self.session.send(self.pub_socket, u'pyin',{u'code':code}, parent=parent)
193 pyin_msg = self.session.send(self.pub_socket, u'pyin',{u'code':code}, parent=parent)
194
194
195 def execute_request(self, ident, parent):
195 def execute_request(self, ident, parent):
196
196
197 status_msg = self.session.send(self.pub_socket,
197 status_msg = self.session.send(self.pub_socket,
198 u'status',
198 u'status',
199 {u'execution_state':u'busy'},
199 {u'execution_state':u'busy'},
200 parent=parent
200 parent=parent
201 )
201 )
202
202
203 try:
203 try:
204 content = parent[u'content']
204 content = parent[u'content']
205 code = content[u'code']
205 code = content[u'code']
206 silent = content[u'silent']
206 silent = content[u'silent']
207 except:
207 except:
208 logger.error("Got bad msg: ")
208 logger.error("Got bad msg: ")
209 logger.error(str(Message(parent)))
209 logger.error(str(Message(parent)))
210 return
210 return
211
211
212 shell = self.shell # we'll need this a lot here
212 shell = self.shell # we'll need this a lot here
213
213
214 # Replace raw_input. Note that is not sufficient to replace
214 # Replace raw_input. Note that is not sufficient to replace
215 # raw_input in the user namespace.
215 # raw_input in the user namespace.
216 raw_input = lambda prompt='': self._raw_input(prompt, ident, parent)
216 raw_input = lambda prompt='': self._raw_input(prompt, ident, parent)
217 __builtin__.raw_input = raw_input
217 __builtin__.raw_input = raw_input
218
218
219 # Set the parent message of the display hook and out streams.
219 # Set the parent message of the display hook and out streams.
220 shell.displayhook.set_parent(parent)
220 shell.displayhook.set_parent(parent)
221 shell.display_pub.set_parent(parent)
221 shell.display_pub.set_parent(parent)
222 sys.stdout.set_parent(parent)
222 sys.stdout.set_parent(parent)
223 sys.stderr.set_parent(parent)
223 sys.stderr.set_parent(parent)
224
224
225 # Re-broadcast our input for the benefit of listening clients, and
225 # Re-broadcast our input for the benefit of listening clients, and
226 # start computing output
226 # start computing output
227 if not silent:
227 if not silent:
228 self._publish_pyin(code, parent)
228 self._publish_pyin(code, parent)
229
229
230 reply_content = {}
230 reply_content = {}
231 try:
231 try:
232 if silent:
232 if silent:
233 # run_code uses 'exec' mode, so no displayhook will fire, and it
233 # run_code uses 'exec' mode, so no displayhook will fire, and it
234 # doesn't call logging or history manipulations. Print
234 # doesn't call logging or history manipulations. Print
235 # statements in that code will obviously still execute.
235 # statements in that code will obviously still execute.
236 shell.run_code(code)
236 shell.run_code(code)
237 else:
237 else:
238 # FIXME: the shell calls the exception handler itself.
238 # FIXME: the shell calls the exception handler itself.
239 shell._reply_content = None
239 shell._reply_content = None
240 shell.run_cell(code)
240 shell.run_cell(code)
241 except:
241 except:
242 status = u'error'
242 status = u'error'
243 # FIXME: this code right now isn't being used yet by default,
243 # FIXME: this code right now isn't being used yet by default,
244 # because the runlines() call above directly fires off exception
244 # because the run_cell() call above directly fires off exception
245 # reporting. This code, therefore, is only active in the scenario
245 # reporting. This code, therefore, is only active in the scenario
246 # where runlines itself has an unhandled exception. We need to
246 # where runlines itself has an unhandled exception. We need to
247 # uniformize this, for all exception construction to come from a
247 # uniformize this, for all exception construction to come from a
248 # single location in the codbase.
248 # single location in the codbase.
249 etype, evalue, tb = sys.exc_info()
249 etype, evalue, tb = sys.exc_info()
250 tb_list = traceback.format_exception(etype, evalue, tb)
250 tb_list = traceback.format_exception(etype, evalue, tb)
251 reply_content.update(shell._showtraceback(etype, evalue, tb_list))
251 reply_content.update(shell._showtraceback(etype, evalue, tb_list))
252 else:
252 else:
253 status = u'ok'
253 status = u'ok'
254
254
255 reply_content[u'status'] = status
255 reply_content[u'status'] = status
256
256
257 # Return the execution counter so clients can display prompts
257 # Return the execution counter so clients can display prompts
258 reply_content['execution_count'] = shell.execution_count -1
258 reply_content['execution_count'] = shell.execution_count -1
259
259
260 # FIXME - fish exception info out of shell, possibly left there by
260 # FIXME - fish exception info out of shell, possibly left there by
261 # runlines. We'll need to clean up this logic later.
261 # runlines. We'll need to clean up this logic later.
262 if shell._reply_content is not None:
262 if shell._reply_content is not None:
263 reply_content.update(shell._reply_content)
263 reply_content.update(shell._reply_content)
264
264
265 # At this point, we can tell whether the main code execution succeeded
265 # At this point, we can tell whether the main code execution succeeded
266 # or not. If it did, we proceed to evaluate user_variables/expressions
266 # or not. If it did, we proceed to evaluate user_variables/expressions
267 if reply_content['status'] == 'ok':
267 if reply_content['status'] == 'ok':
268 reply_content[u'user_variables'] = \
268 reply_content[u'user_variables'] = \
269 shell.user_variables(content[u'user_variables'])
269 shell.user_variables(content[u'user_variables'])
270 reply_content[u'user_expressions'] = \
270 reply_content[u'user_expressions'] = \
271 shell.user_expressions(content[u'user_expressions'])
271 shell.user_expressions(content[u'user_expressions'])
272 else:
272 else:
273 # If there was an error, don't even try to compute variables or
273 # If there was an error, don't even try to compute variables or
274 # expressions
274 # expressions
275 reply_content[u'user_variables'] = {}
275 reply_content[u'user_variables'] = {}
276 reply_content[u'user_expressions'] = {}
276 reply_content[u'user_expressions'] = {}
277
277
278 # Payloads should be retrieved regardless of outcome, so we can both
278 # Payloads should be retrieved regardless of outcome, so we can both
279 # recover partial output (that could have been generated early in a
279 # recover partial output (that could have been generated early in a
280 # block, before an error) and clear the payload system always.
280 # block, before an error) and clear the payload system always.
281 reply_content[u'payload'] = shell.payload_manager.read_payload()
281 reply_content[u'payload'] = shell.payload_manager.read_payload()
282 # Be agressive about clearing the payload because we don't want
282 # Be agressive about clearing the payload because we don't want
283 # it to sit in memory until the next execute_request comes in.
283 # it to sit in memory until the next execute_request comes in.
284 shell.payload_manager.clear_payload()
284 shell.payload_manager.clear_payload()
285
285
286 # Flush output before sending the reply.
286 # Flush output before sending the reply.
287 sys.stdout.flush()
287 sys.stdout.flush()
288 sys.stderr.flush()
288 sys.stderr.flush()
289 # FIXME: on rare occasions, the flush doesn't seem to make it to the
289 # FIXME: on rare occasions, the flush doesn't seem to make it to the
290 # clients... This seems to mitigate the problem, but we definitely need
290 # clients... This seems to mitigate the problem, but we definitely need
291 # to better understand what's going on.
291 # to better understand what's going on.
292 if self._execute_sleep:
292 if self._execute_sleep:
293 time.sleep(self._execute_sleep)
293 time.sleep(self._execute_sleep)
294
294
295 # Send the reply.
295 # Send the reply.
296 reply_msg = self.session.send(self.reply_socket, u'execute_reply',
296 reply_msg = self.session.send(self.reply_socket, u'execute_reply',
297 reply_content, parent, ident=ident)
297 reply_content, parent, ident=ident)
298 logger.debug(str(reply_msg))
298 logger.debug(str(reply_msg))
299
299
300 if reply_msg['content']['status'] == u'error':
300 if reply_msg['content']['status'] == u'error':
301 self._abort_queue()
301 self._abort_queue()
302
302
303 status_msg = self.session.send(self.pub_socket,
303 status_msg = self.session.send(self.pub_socket,
304 u'status',
304 u'status',
305 {u'execution_state':u'idle'},
305 {u'execution_state':u'idle'},
306 parent=parent
306 parent=parent
307 )
307 )
308
308
309 def complete_request(self, ident, parent):
309 def complete_request(self, ident, parent):
310 txt, matches = self._complete(parent)
310 txt, matches = self._complete(parent)
311 matches = {'matches' : matches,
311 matches = {'matches' : matches,
312 'matched_text' : txt,
312 'matched_text' : txt,
313 'status' : 'ok'}
313 'status' : 'ok'}
314 completion_msg = self.session.send(self.reply_socket, 'complete_reply',
314 completion_msg = self.session.send(self.reply_socket, 'complete_reply',
315 matches, parent, ident)
315 matches, parent, ident)
316 logger.debug(str(completion_msg))
316 logger.debug(str(completion_msg))
317
317
318 def object_info_request(self, ident, parent):
318 def object_info_request(self, ident, parent):
319 object_info = self.shell.object_inspect(parent['content']['oname'])
319 object_info = self.shell.object_inspect(parent['content']['oname'])
320 # Before we send this object over, we scrub it for JSON usage
320 # Before we send this object over, we scrub it for JSON usage
321 oinfo = json_clean(object_info)
321 oinfo = json_clean(object_info)
322 msg = self.session.send(self.reply_socket, 'object_info_reply',
322 msg = self.session.send(self.reply_socket, 'object_info_reply',
323 oinfo, parent, ident)
323 oinfo, parent, ident)
324 logger.debug(msg)
324 logger.debug(msg)
325
325
326 def history_tail_request(self, ident, parent):
326 def history_tail_request(self, ident, parent):
327 # We need to pull these out, as passing **kwargs doesn't work with
327 # We need to pull these out, as passing **kwargs doesn't work with
328 # unicode keys before Python 2.6.5.
328 # unicode keys before Python 2.6.5.
329 n = parent['content']['n']
329 n = parent['content']['n']
330 raw = parent['content']['raw']
330 raw = parent['content']['raw']
331 output = parent['content']['output']
331 output = parent['content']['output']
332 hist = self.shell.history_manager.get_tail(n, raw=raw, output=output)
332 hist = self.shell.history_manager.get_tail(n, raw=raw, output=output)
333 content = {'history' : list(hist)}
333 content = {'history' : list(hist)}
334 msg = self.session.send(self.reply_socket, 'history_tail_reply',
334 msg = self.session.send(self.reply_socket, 'history_tail_reply',
335 content, parent, ident)
335 content, parent, ident)
336 logger.debug(str(msg))
336 logger.debug(str(msg))
337
337
338 def connect_request(self, ident, parent):
338 def connect_request(self, ident, parent):
339 if self._recorded_ports is not None:
339 if self._recorded_ports is not None:
340 content = self._recorded_ports.copy()
340 content = self._recorded_ports.copy()
341 else:
341 else:
342 content = {}
342 content = {}
343 msg = self.session.send(self.reply_socket, 'connect_reply',
343 msg = self.session.send(self.reply_socket, 'connect_reply',
344 content, parent, ident)
344 content, parent, ident)
345 logger.debug(msg)
345 logger.debug(msg)
346
346
347 def shutdown_request(self, ident, parent):
347 def shutdown_request(self, ident, parent):
348 self.shell.exit_now = True
348 self.shell.exit_now = True
349 self._shutdown_message = self.session.msg(u'shutdown_reply', parent['content'], parent)
349 self._shutdown_message = self.session.msg(u'shutdown_reply', parent['content'], parent)
350 sys.exit(0)
350 sys.exit(0)
351
351
352 #---------------------------------------------------------------------------
352 #---------------------------------------------------------------------------
353 # Protected interface
353 # Protected interface
354 #---------------------------------------------------------------------------
354 #---------------------------------------------------------------------------
355
355
356 def _abort_queue(self):
356 def _abort_queue(self):
357 while True:
357 while True:
358 ident,msg = self.session.recv(self.reply_socket, zmq.NOBLOCK)
358 ident,msg = self.session.recv(self.reply_socket, zmq.NOBLOCK)
359 if msg is None:
359 if msg is None:
360 break
360 break
361 else:
361 else:
362 assert ident is not None, \
362 assert ident is not None, \
363 "Unexpected missing message part."
363 "Unexpected missing message part."
364
364
365 logger.debug("Aborting:\n"+str(Message(msg)))
365 logger.debug("Aborting:\n"+str(Message(msg)))
366 msg_type = msg['msg_type']
366 msg_type = msg['msg_type']
367 reply_type = msg_type.split('_')[0] + '_reply'
367 reply_type = msg_type.split('_')[0] + '_reply'
368 reply_msg = self.session.send(self.reply_socket, reply_type,
368 reply_msg = self.session.send(self.reply_socket, reply_type,
369 {'status' : 'aborted'}, msg, ident=ident)
369 {'status' : 'aborted'}, msg, ident=ident)
370 logger.debug(reply_msg)
370 logger.debug(reply_msg)
371 # We need to wait a bit for requests to come in. This can probably
371 # We need to wait a bit for requests to come in. This can probably
372 # be set shorter for true asynchronous clients.
372 # be set shorter for true asynchronous clients.
373 time.sleep(0.1)
373 time.sleep(0.1)
374
374
375 def _raw_input(self, prompt, ident, parent):
375 def _raw_input(self, prompt, ident, parent):
376 # Flush output before making the request.
376 # Flush output before making the request.
377 sys.stderr.flush()
377 sys.stderr.flush()
378 sys.stdout.flush()
378 sys.stdout.flush()
379
379
380 # Send the input request.
380 # Send the input request.
381 content = dict(prompt=prompt)
381 content = dict(prompt=prompt)
382 msg = self.session.send(self.req_socket, u'input_request', content, parent)
382 msg = self.session.send(self.req_socket, u'input_request', content, parent)
383
383
384 # Await a response.
384 # Await a response.
385 ident, reply = self.session.recv(self.req_socket, 0)
385 ident, reply = self.session.recv(self.req_socket, 0)
386 try:
386 try:
387 value = reply['content']['value']
387 value = reply['content']['value']
388 except:
388 except:
389 logger.error("Got bad raw_input reply: ")
389 logger.error("Got bad raw_input reply: ")
390 logger.error(str(Message(parent)))
390 logger.error(str(Message(parent)))
391 value = ''
391 value = ''
392 return value
392 return value
393
393
394 def _complete(self, msg):
394 def _complete(self, msg):
395 c = msg['content']
395 c = msg['content']
396 try:
396 try:
397 cpos = int(c['cursor_pos'])
397 cpos = int(c['cursor_pos'])
398 except:
398 except:
399 # If we don't get something that we can convert to an integer, at
399 # If we don't get something that we can convert to an integer, at
400 # least attempt the completion guessing the cursor is at the end of
400 # least attempt the completion guessing the cursor is at the end of
401 # the text, if there's any, and otherwise of the line
401 # the text, if there's any, and otherwise of the line
402 cpos = len(c['text'])
402 cpos = len(c['text'])
403 if cpos==0:
403 if cpos==0:
404 cpos = len(c['line'])
404 cpos = len(c['line'])
405 return self.shell.complete(c['text'], c['line'], cpos)
405 return self.shell.complete(c['text'], c['line'], cpos)
406
406
407 def _object_info(self, context):
407 def _object_info(self, context):
408 symbol, leftover = self._symbol_from_context(context)
408 symbol, leftover = self._symbol_from_context(context)
409 if symbol is not None and not leftover:
409 if symbol is not None and not leftover:
410 doc = getattr(symbol, '__doc__', '')
410 doc = getattr(symbol, '__doc__', '')
411 else:
411 else:
412 doc = ''
412 doc = ''
413 object_info = dict(docstring = doc)
413 object_info = dict(docstring = doc)
414 return object_info
414 return object_info
415
415
416 def _symbol_from_context(self, context):
416 def _symbol_from_context(self, context):
417 if not context:
417 if not context:
418 return None, context
418 return None, context
419
419
420 base_symbol_string = context[0]
420 base_symbol_string = context[0]
421 symbol = self.shell.user_ns.get(base_symbol_string, None)
421 symbol = self.shell.user_ns.get(base_symbol_string, None)
422 if symbol is None:
422 if symbol is None:
423 symbol = __builtin__.__dict__.get(base_symbol_string, None)
423 symbol = __builtin__.__dict__.get(base_symbol_string, None)
424 if symbol is None:
424 if symbol is None:
425 return None, context
425 return None, context
426
426
427 context = context[1:]
427 context = context[1:]
428 for i, name in enumerate(context):
428 for i, name in enumerate(context):
429 new_symbol = getattr(symbol, name, None)
429 new_symbol = getattr(symbol, name, None)
430 if new_symbol is None:
430 if new_symbol is None:
431 return symbol, context[i:]
431 return symbol, context[i:]
432 else:
432 else:
433 symbol = new_symbol
433 symbol = new_symbol
434
434
435 return symbol, []
435 return symbol, []
436
436
437 def _at_shutdown(self):
437 def _at_shutdown(self):
438 """Actions taken at shutdown by the kernel, called by python's atexit.
438 """Actions taken at shutdown by the kernel, called by python's atexit.
439 """
439 """
440 # io.rprint("Kernel at_shutdown") # dbg
440 # io.rprint("Kernel at_shutdown") # dbg
441 if self._shutdown_message is not None:
441 if self._shutdown_message is not None:
442 self.session.send(self.reply_socket, self._shutdown_message)
442 self.session.send(self.reply_socket, self._shutdown_message)
443 self.session.send(self.pub_socket, self._shutdown_message)
443 self.session.send(self.pub_socket, self._shutdown_message)
444 logger.debug(str(self._shutdown_message))
444 logger.debug(str(self._shutdown_message))
445 # A very short sleep to give zmq time to flush its message buffers
445 # A very short sleep to give zmq time to flush its message buffers
446 # before Python truly shuts down.
446 # before Python truly shuts down.
447 time.sleep(0.01)
447 time.sleep(0.01)
448
448
449
449
450 class QtKernel(Kernel):
450 class QtKernel(Kernel):
451 """A Kernel subclass with Qt support."""
451 """A Kernel subclass with Qt support."""
452
452
453 def start(self):
453 def start(self):
454 """Start a kernel with QtPy4 event loop integration."""
454 """Start a kernel with QtPy4 event loop integration."""
455
455
456 from PyQt4 import QtCore
456 from PyQt4 import QtCore
457 from IPython.lib.guisupport import get_app_qt4, start_event_loop_qt4
457 from IPython.lib.guisupport import get_app_qt4, start_event_loop_qt4
458
458
459 self.app = get_app_qt4([" "])
459 self.app = get_app_qt4([" "])
460 self.app.setQuitOnLastWindowClosed(False)
460 self.app.setQuitOnLastWindowClosed(False)
461 self.timer = QtCore.QTimer()
461 self.timer = QtCore.QTimer()
462 self.timer.timeout.connect(self.do_one_iteration)
462 self.timer.timeout.connect(self.do_one_iteration)
463 # Units for the timer are in milliseconds
463 # Units for the timer are in milliseconds
464 self.timer.start(1000*self._poll_interval)
464 self.timer.start(1000*self._poll_interval)
465 start_event_loop_qt4(self.app)
465 start_event_loop_qt4(self.app)
466
466
467
467
468 class WxKernel(Kernel):
468 class WxKernel(Kernel):
469 """A Kernel subclass with Wx support."""
469 """A Kernel subclass with Wx support."""
470
470
471 def start(self):
471 def start(self):
472 """Start a kernel with wx event loop support."""
472 """Start a kernel with wx event loop support."""
473
473
474 import wx
474 import wx
475 from IPython.lib.guisupport import start_event_loop_wx
475 from IPython.lib.guisupport import start_event_loop_wx
476
476
477 doi = self.do_one_iteration
477 doi = self.do_one_iteration
478 # Wx uses milliseconds
478 # Wx uses milliseconds
479 poll_interval = int(1000*self._poll_interval)
479 poll_interval = int(1000*self._poll_interval)
480
480
481 # We have to put the wx.Timer in a wx.Frame for it to fire properly.
481 # We have to put the wx.Timer in a wx.Frame for it to fire properly.
482 # We make the Frame hidden when we create it in the main app below.
482 # We make the Frame hidden when we create it in the main app below.
483 class TimerFrame(wx.Frame):
483 class TimerFrame(wx.Frame):
484 def __init__(self, func):
484 def __init__(self, func):
485 wx.Frame.__init__(self, None, -1)
485 wx.Frame.__init__(self, None, -1)
486 self.timer = wx.Timer(self)
486 self.timer = wx.Timer(self)
487 # Units for the timer are in milliseconds
487 # Units for the timer are in milliseconds
488 self.timer.Start(poll_interval)
488 self.timer.Start(poll_interval)
489 self.Bind(wx.EVT_TIMER, self.on_timer)
489 self.Bind(wx.EVT_TIMER, self.on_timer)
490 self.func = func
490 self.func = func
491
491
492 def on_timer(self, event):
492 def on_timer(self, event):
493 self.func()
493 self.func()
494
494
495 # We need a custom wx.App to create our Frame subclass that has the
495 # We need a custom wx.App to create our Frame subclass that has the
496 # wx.Timer to drive the ZMQ event loop.
496 # wx.Timer to drive the ZMQ event loop.
497 class IPWxApp(wx.App):
497 class IPWxApp(wx.App):
498 def OnInit(self):
498 def OnInit(self):
499 self.frame = TimerFrame(doi)
499 self.frame = TimerFrame(doi)
500 self.frame.Show(False)
500 self.frame.Show(False)
501 return True
501 return True
502
502
503 # The redirect=False here makes sure that wx doesn't replace
503 # The redirect=False here makes sure that wx doesn't replace
504 # sys.stdout/stderr with its own classes.
504 # sys.stdout/stderr with its own classes.
505 self.app = IPWxApp(redirect=False)
505 self.app = IPWxApp(redirect=False)
506 start_event_loop_wx(self.app)
506 start_event_loop_wx(self.app)
507
507
508
508
509 class TkKernel(Kernel):
509 class TkKernel(Kernel):
510 """A Kernel subclass with Tk support."""
510 """A Kernel subclass with Tk support."""
511
511
512 def start(self):
512 def start(self):
513 """Start a Tk enabled event loop."""
513 """Start a Tk enabled event loop."""
514
514
515 import Tkinter
515 import Tkinter
516 doi = self.do_one_iteration
516 doi = self.do_one_iteration
517 # Tk uses milliseconds
517 # Tk uses milliseconds
518 poll_interval = int(1000*self._poll_interval)
518 poll_interval = int(1000*self._poll_interval)
519 # For Tkinter, we create a Tk object and call its withdraw method.
519 # For Tkinter, we create a Tk object and call its withdraw method.
520 class Timer(object):
520 class Timer(object):
521 def __init__(self, func):
521 def __init__(self, func):
522 self.app = Tkinter.Tk()
522 self.app = Tkinter.Tk()
523 self.app.withdraw()
523 self.app.withdraw()
524 self.func = func
524 self.func = func
525
525
526 def on_timer(self):
526 def on_timer(self):
527 self.func()
527 self.func()
528 self.app.after(poll_interval, self.on_timer)
528 self.app.after(poll_interval, self.on_timer)
529
529
530 def start(self):
530 def start(self):
531 self.on_timer() # Call it once to get things going.
531 self.on_timer() # Call it once to get things going.
532 self.app.mainloop()
532 self.app.mainloop()
533
533
534 self.timer = Timer(doi)
534 self.timer = Timer(doi)
535 self.timer.start()
535 self.timer.start()
536
536
537
537
538 class GTKKernel(Kernel):
538 class GTKKernel(Kernel):
539 """A Kernel subclass with GTK support."""
539 """A Kernel subclass with GTK support."""
540
540
541 def start(self):
541 def start(self):
542 """Start the kernel, coordinating with the GTK event loop"""
542 """Start the kernel, coordinating with the GTK event loop"""
543 from .gui.gtkembed import GTKEmbed
543 from .gui.gtkembed import GTKEmbed
544
544
545 gtk_kernel = GTKEmbed(self)
545 gtk_kernel = GTKEmbed(self)
546 gtk_kernel.start()
546 gtk_kernel.start()
547
547
548
548
549 #-----------------------------------------------------------------------------
549 #-----------------------------------------------------------------------------
550 # Kernel main and launch functions
550 # Kernel main and launch functions
551 #-----------------------------------------------------------------------------
551 #-----------------------------------------------------------------------------
552
552
553 def launch_kernel(ip=None, xrep_port=0, pub_port=0, req_port=0, hb_port=0,
553 def launch_kernel(ip=None, xrep_port=0, pub_port=0, req_port=0, hb_port=0,
554 independent=False, pylab=False, colors=None):
554 independent=False, pylab=False, colors=None):
555 """Launches a localhost kernel, binding to the specified ports.
555 """Launches a localhost kernel, binding to the specified ports.
556
556
557 Parameters
557 Parameters
558 ----------
558 ----------
559 ip : str, optional
559 ip : str, optional
560 The ip address the kernel will bind to.
560 The ip address the kernel will bind to.
561
561
562 xrep_port : int, optional
562 xrep_port : int, optional
563 The port to use for XREP channel.
563 The port to use for XREP channel.
564
564
565 pub_port : int, optional
565 pub_port : int, optional
566 The port to use for the SUB channel.
566 The port to use for the SUB channel.
567
567
568 req_port : int, optional
568 req_port : int, optional
569 The port to use for the REQ (raw input) channel.
569 The port to use for the REQ (raw input) channel.
570
570
571 hb_port : int, optional
571 hb_port : int, optional
572 The port to use for the hearbeat REP channel.
572 The port to use for the hearbeat REP channel.
573
573
574 independent : bool, optional (default False)
574 independent : bool, optional (default False)
575 If set, the kernel process is guaranteed to survive if this process
575 If set, the kernel process is guaranteed to survive if this process
576 dies. If not set, an effort is made to ensure that the kernel is killed
576 dies. If not set, an effort is made to ensure that the kernel is killed
577 when this process dies. Note that in this case it is still good practice
577 when this process dies. Note that in this case it is still good practice
578 to kill kernels manually before exiting.
578 to kill kernels manually before exiting.
579
579
580 pylab : bool or string, optional (default False)
580 pylab : bool or string, optional (default False)
581 If not False, the kernel will be launched with pylab enabled. If a
581 If not False, the kernel will be launched with pylab enabled. If a
582 string is passed, matplotlib will use the specified backend. Otherwise,
582 string is passed, matplotlib will use the specified backend. Otherwise,
583 matplotlib's default backend will be used.
583 matplotlib's default backend will be used.
584
584
585 colors : None or string, optional (default None)
585 colors : None or string, optional (default None)
586 If not None, specify the color scheme. One of (NoColor, LightBG, Linux)
586 If not None, specify the color scheme. One of (NoColor, LightBG, Linux)
587
587
588 Returns
588 Returns
589 -------
589 -------
590 A tuple of form:
590 A tuple of form:
591 (kernel_process, xrep_port, pub_port, req_port)
591 (kernel_process, xrep_port, pub_port, req_port)
592 where kernel_process is a Popen object and the ports are integers.
592 where kernel_process is a Popen object and the ports are integers.
593 """
593 """
594 extra_arguments = []
594 extra_arguments = []
595 if pylab:
595 if pylab:
596 extra_arguments.append('--pylab')
596 extra_arguments.append('--pylab')
597 if isinstance(pylab, basestring):
597 if isinstance(pylab, basestring):
598 extra_arguments.append(pylab)
598 extra_arguments.append(pylab)
599 if ip is not None:
599 if ip is not None:
600 extra_arguments.append('--ip')
600 extra_arguments.append('--ip')
601 if isinstance(ip, basestring):
601 if isinstance(ip, basestring):
602 extra_arguments.append(ip)
602 extra_arguments.append(ip)
603 if colors is not None:
603 if colors is not None:
604 extra_arguments.append('--colors')
604 extra_arguments.append('--colors')
605 extra_arguments.append(colors)
605 extra_arguments.append(colors)
606 return base_launch_kernel('from IPython.zmq.ipkernel import main; main()',
606 return base_launch_kernel('from IPython.zmq.ipkernel import main; main()',
607 xrep_port, pub_port, req_port, hb_port,
607 xrep_port, pub_port, req_port, hb_port,
608 independent, extra_arguments)
608 independent, extra_arguments)
609
609
610
610
611 def main():
611 def main():
612 """ The IPython kernel main entry point.
612 """ The IPython kernel main entry point.
613 """
613 """
614 parser = make_argument_parser()
614 parser = make_argument_parser()
615 parser.add_argument('--pylab', type=str, metavar='GUI', nargs='?',
615 parser.add_argument('--pylab', type=str, metavar='GUI', nargs='?',
616 const='auto', help = \
616 const='auto', help = \
617 "Pre-load matplotlib and numpy for interactive use. If GUI is not \
617 "Pre-load matplotlib and numpy for interactive use. If GUI is not \
618 given, the GUI backend is matplotlib's, otherwise use one of: \
618 given, the GUI backend is matplotlib's, otherwise use one of: \
619 ['tk', 'gtk', 'qt', 'wx', 'inline'].")
619 ['tk', 'gtk', 'qt', 'wx', 'inline'].")
620 parser.add_argument('--colors',
620 parser.add_argument('--colors',
621 type=str, dest='colors',
621 type=str, dest='colors',
622 help="Set the color scheme (NoColor, Linux, and LightBG).",
622 help="Set the color scheme (NoColor, Linux, and LightBG).",
623 metavar='ZMQInteractiveShell.colors')
623 metavar='ZMQInteractiveShell.colors')
624 namespace = parser.parse_args()
624 namespace = parser.parse_args()
625
625
626 kernel_class = Kernel
626 kernel_class = Kernel
627
627
628 kernel_classes = {
628 kernel_classes = {
629 'qt' : QtKernel,
629 'qt' : QtKernel,
630 'qt4': QtKernel,
630 'qt4': QtKernel,
631 'inline': Kernel,
631 'inline': Kernel,
632 'wx' : WxKernel,
632 'wx' : WxKernel,
633 'tk' : TkKernel,
633 'tk' : TkKernel,
634 'gtk': GTKKernel,
634 'gtk': GTKKernel,
635 }
635 }
636 if namespace.pylab:
636 if namespace.pylab:
637 if namespace.pylab == 'auto':
637 if namespace.pylab == 'auto':
638 gui, backend = pylabtools.find_gui_and_backend()
638 gui, backend = pylabtools.find_gui_and_backend()
639 else:
639 else:
640 gui, backend = pylabtools.find_gui_and_backend(namespace.pylab)
640 gui, backend = pylabtools.find_gui_and_backend(namespace.pylab)
641 kernel_class = kernel_classes.get(gui)
641 kernel_class = kernel_classes.get(gui)
642 if kernel_class is None:
642 if kernel_class is None:
643 raise ValueError('GUI is not supported: %r' % gui)
643 raise ValueError('GUI is not supported: %r' % gui)
644 pylabtools.activate_matplotlib(backend)
644 pylabtools.activate_matplotlib(backend)
645 if namespace.colors:
645 if namespace.colors:
646 ZMQInteractiveShell.colors=namespace.colors
646 ZMQInteractiveShell.colors=namespace.colors
647
647
648 kernel = make_kernel(namespace, kernel_class, OutStream)
648 kernel = make_kernel(namespace, kernel_class, OutStream)
649
649
650 if namespace.pylab:
650 if namespace.pylab:
651 pylabtools.import_pylab(kernel.shell.user_ns, backend,
651 pylabtools.import_pylab(kernel.shell.user_ns, backend,
652 shell=kernel.shell)
652 shell=kernel.shell)
653
653
654 start_kernel(namespace, kernel)
654 start_kernel(namespace, kernel)
655
655
656
656
657 if __name__ == '__main__':
657 if __name__ == '__main__':
658 main()
658 main()
General Comments 0
You need to be logged in to leave comments. Login now