##// END OF EJS Templates
Initial GUI support in kernel.
Brian Granger -
Show More
@@ -1,2147 +1,2147
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-2010 The IPython Development Team
7 # Copyright (C) 2008-2010 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 abc
21 import abc
22 import codeop
22 import codeop
23 import exceptions
23 import exceptions
24 import new
24 import new
25 import os
25 import os
26 import re
26 import re
27 import string
27 import string
28 import sys
28 import sys
29 import tempfile
29 import tempfile
30 from contextlib import nested
30 from contextlib import nested
31
31
32 from IPython.config.configurable import Configurable
32 from IPython.config.configurable import Configurable
33 from IPython.core import debugger, oinspect
33 from IPython.core import debugger, oinspect
34 from IPython.core import history as ipcorehist
34 from IPython.core import history as ipcorehist
35 from IPython.core import prefilter
35 from IPython.core import prefilter
36 from IPython.core import shadowns
36 from IPython.core import shadowns
37 from IPython.core import ultratb
37 from IPython.core import ultratb
38 from IPython.core.alias import AliasManager
38 from IPython.core.alias import AliasManager
39 from IPython.core.builtin_trap import BuiltinTrap
39 from IPython.core.builtin_trap import BuiltinTrap
40 from IPython.core.display_trap import DisplayTrap
40 from IPython.core.display_trap import DisplayTrap
41 from IPython.core.displayhook import DisplayHook
41 from IPython.core.displayhook import DisplayHook
42 from IPython.core.error import UsageError
42 from IPython.core.error import UsageError
43 from IPython.core.extensions import ExtensionManager
43 from IPython.core.extensions import ExtensionManager
44 from IPython.core.fakemodule import FakeModule, init_fakemod_dict
44 from IPython.core.fakemodule import FakeModule, init_fakemod_dict
45 from IPython.core.inputlist import InputList
45 from IPython.core.inputlist import InputList
46 from IPython.core.logger import Logger
46 from IPython.core.logger import Logger
47 from IPython.core.magic import Magic
47 from IPython.core.magic import Magic
48 from IPython.core.payload import PayloadManager
48 from IPython.core.payload import PayloadManager
49 from IPython.core.plugin import PluginManager
49 from IPython.core.plugin import PluginManager
50 from IPython.core.prefilter import PrefilterManager
50 from IPython.core.prefilter import PrefilterManager
51 from IPython.external.Itpl import ItplNS
51 from IPython.external.Itpl import ItplNS
52 from IPython.utils import PyColorize
52 from IPython.utils import PyColorize
53 from IPython.utils import io
53 from IPython.utils import io
54 from IPython.utils import pickleshare
54 from IPython.utils import pickleshare
55 from IPython.utils.doctestreload import doctest_reload
55 from IPython.utils.doctestreload import doctest_reload
56 from IPython.utils.io import ask_yes_no, rprint
56 from IPython.utils.io import ask_yes_no, rprint
57 from IPython.utils.ipstruct import Struct
57 from IPython.utils.ipstruct import Struct
58 from IPython.utils.path import get_home_dir, get_ipython_dir, HomeDirError
58 from IPython.utils.path import get_home_dir, get_ipython_dir, HomeDirError
59 from IPython.utils.process import getoutput, getoutputerror
59 from IPython.utils.process import getoutput, getoutputerror
60 from IPython.utils.strdispatch import StrDispatch
60 from IPython.utils.strdispatch import StrDispatch
61 from IPython.utils.syspathcontext import prepended_to_syspath
61 from IPython.utils.syspathcontext import prepended_to_syspath
62 from IPython.utils.text import num_ini_spaces
62 from IPython.utils.text import num_ini_spaces
63 from IPython.utils.traitlets import (Int, Str, CBool, CaselessStrEnum, Enum,
63 from IPython.utils.traitlets import (Int, Str, CBool, CaselessStrEnum, Enum,
64 List, Unicode, Instance, Type)
64 List, Unicode, Instance, Type)
65 from IPython.utils.warn import warn, error, fatal
65 from IPython.utils.warn import warn, error, fatal
66 import IPython.core.hooks
66 import IPython.core.hooks
67
67
68 # from IPython.utils import growl
68 # from IPython.utils import growl
69 # growl.start("IPython")
69 # growl.start("IPython")
70
70
71 #-----------------------------------------------------------------------------
71 #-----------------------------------------------------------------------------
72 # Globals
72 # Globals
73 #-----------------------------------------------------------------------------
73 #-----------------------------------------------------------------------------
74
74
75 # compiled regexps for autoindent management
75 # compiled regexps for autoindent management
76 dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass')
76 dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass')
77
77
78 #-----------------------------------------------------------------------------
78 #-----------------------------------------------------------------------------
79 # Utilities
79 # Utilities
80 #-----------------------------------------------------------------------------
80 #-----------------------------------------------------------------------------
81
81
82 # store the builtin raw_input globally, and use this always, in case user code
82 # store the builtin raw_input globally, and use this always, in case user code
83 # overwrites it (like wx.py.PyShell does)
83 # overwrites it (like wx.py.PyShell does)
84 raw_input_original = raw_input
84 raw_input_original = raw_input
85
85
86 def softspace(file, newvalue):
86 def softspace(file, newvalue):
87 """Copied from code.py, to remove the dependency"""
87 """Copied from code.py, to remove the dependency"""
88
88
89 oldvalue = 0
89 oldvalue = 0
90 try:
90 try:
91 oldvalue = file.softspace
91 oldvalue = file.softspace
92 except AttributeError:
92 except AttributeError:
93 pass
93 pass
94 try:
94 try:
95 file.softspace = newvalue
95 file.softspace = newvalue
96 except (AttributeError, TypeError):
96 except (AttributeError, TypeError):
97 # "attribute-less object" or "read-only attributes"
97 # "attribute-less object" or "read-only attributes"
98 pass
98 pass
99 return oldvalue
99 return oldvalue
100
100
101
101
102 def no_op(*a, **kw): pass
102 def no_op(*a, **kw): pass
103
103
104 class SpaceInInput(exceptions.Exception): pass
104 class SpaceInInput(exceptions.Exception): pass
105
105
106 class Bunch: pass
106 class Bunch: pass
107
107
108
108
109 def get_default_colors():
109 def get_default_colors():
110 if sys.platform=='darwin':
110 if sys.platform=='darwin':
111 return "LightBG"
111 return "LightBG"
112 elif os.name=='nt':
112 elif os.name=='nt':
113 return 'Linux'
113 return 'Linux'
114 else:
114 else:
115 return 'Linux'
115 return 'Linux'
116
116
117
117
118 class SeparateStr(Str):
118 class SeparateStr(Str):
119 """A Str subclass to validate separate_in, separate_out, etc.
119 """A Str subclass to validate separate_in, separate_out, etc.
120
120
121 This is a Str based trait that converts '0'->'' and '\\n'->'\n'.
121 This is a Str based trait that converts '0'->'' and '\\n'->'\n'.
122 """
122 """
123
123
124 def validate(self, obj, value):
124 def validate(self, obj, value):
125 if value == '0': value = ''
125 if value == '0': value = ''
126 value = value.replace('\\n','\n')
126 value = value.replace('\\n','\n')
127 return super(SeparateStr, self).validate(obj, value)
127 return super(SeparateStr, self).validate(obj, value)
128
128
129 class MultipleInstanceError(Exception):
129 class MultipleInstanceError(Exception):
130 pass
130 pass
131
131
132
132
133 #-----------------------------------------------------------------------------
133 #-----------------------------------------------------------------------------
134 # Main IPython class
134 # Main IPython class
135 #-----------------------------------------------------------------------------
135 #-----------------------------------------------------------------------------
136
136
137
137
138 class InteractiveShell(Configurable, Magic):
138 class InteractiveShell(Configurable, Magic):
139 """An enhanced, interactive shell for Python."""
139 """An enhanced, interactive shell for Python."""
140
140
141 _instance = None
141 _instance = None
142 autocall = Enum((0,1,2), default_value=1, config=True)
142 autocall = Enum((0,1,2), default_value=1, config=True)
143 # TODO: remove all autoindent logic and put into frontends.
143 # TODO: remove all autoindent logic and put into frontends.
144 # We can't do this yet because even runlines uses the autoindent.
144 # We can't do this yet because even runlines uses the autoindent.
145 autoindent = CBool(True, config=True)
145 autoindent = CBool(True, config=True)
146 automagic = CBool(True, config=True)
146 automagic = CBool(True, config=True)
147 cache_size = Int(1000, config=True)
147 cache_size = Int(1000, config=True)
148 color_info = CBool(True, config=True)
148 color_info = CBool(True, config=True)
149 colors = CaselessStrEnum(('NoColor','LightBG','Linux'),
149 colors = CaselessStrEnum(('NoColor','LightBG','Linux'),
150 default_value=get_default_colors(), config=True)
150 default_value=get_default_colors(), config=True)
151 debug = CBool(False, config=True)
151 debug = CBool(False, config=True)
152 deep_reload = CBool(False, config=True)
152 deep_reload = CBool(False, config=True)
153 displayhook_class = Type(DisplayHook)
153 displayhook_class = Type(DisplayHook)
154 filename = Str("<ipython console>")
154 filename = Str("<ipython console>")
155 ipython_dir= Unicode('', config=True) # Set to get_ipython_dir() in __init__
155 ipython_dir= Unicode('', config=True) # Set to get_ipython_dir() in __init__
156 logstart = CBool(False, config=True)
156 logstart = CBool(False, config=True)
157 logfile = Str('', config=True)
157 logfile = Str('', config=True)
158 logappend = Str('', config=True)
158 logappend = Str('', config=True)
159 object_info_string_level = Enum((0,1,2), default_value=0,
159 object_info_string_level = Enum((0,1,2), default_value=0,
160 config=True)
160 config=True)
161 pdb = CBool(False, config=True)
161 pdb = CBool(False, config=True)
162 pprint = CBool(True, config=True)
162 pprint = CBool(True, config=True)
163 profile = Str('', config=True)
163 profile = Str('', config=True)
164 prompt_in1 = Str('In [\\#]: ', config=True)
164 prompt_in1 = Str('In [\\#]: ', config=True)
165 prompt_in2 = Str(' .\\D.: ', config=True)
165 prompt_in2 = Str(' .\\D.: ', config=True)
166 prompt_out = Str('Out[\\#]: ', config=True)
166 prompt_out = Str('Out[\\#]: ', config=True)
167 prompts_pad_left = CBool(True, config=True)
167 prompts_pad_left = CBool(True, config=True)
168 quiet = CBool(False, config=True)
168 quiet = CBool(False, config=True)
169
169
170 # The readline stuff will eventually be moved to the terminal subclass
170 # The readline stuff will eventually be moved to the terminal subclass
171 # but for now, we can't do that as readline is welded in everywhere.
171 # but for now, we can't do that as readline is welded in everywhere.
172 readline_use = CBool(True, config=True)
172 readline_use = CBool(True, config=True)
173 readline_merge_completions = CBool(True, config=True)
173 readline_merge_completions = CBool(True, config=True)
174 readline_omit__names = Enum((0,1,2), default_value=0, config=True)
174 readline_omit__names = Enum((0,1,2), default_value=0, config=True)
175 readline_remove_delims = Str('-/~', config=True)
175 readline_remove_delims = Str('-/~', config=True)
176 readline_parse_and_bind = List([
176 readline_parse_and_bind = List([
177 'tab: complete',
177 'tab: complete',
178 '"\C-l": clear-screen',
178 '"\C-l": clear-screen',
179 'set show-all-if-ambiguous on',
179 'set show-all-if-ambiguous on',
180 '"\C-o": tab-insert',
180 '"\C-o": tab-insert',
181 '"\M-i": " "',
181 '"\M-i": " "',
182 '"\M-o": "\d\d\d\d"',
182 '"\M-o": "\d\d\d\d"',
183 '"\M-I": "\d\d\d\d"',
183 '"\M-I": "\d\d\d\d"',
184 '"\C-r": reverse-search-history',
184 '"\C-r": reverse-search-history',
185 '"\C-s": forward-search-history',
185 '"\C-s": forward-search-history',
186 '"\C-p": history-search-backward',
186 '"\C-p": history-search-backward',
187 '"\C-n": history-search-forward',
187 '"\C-n": history-search-forward',
188 '"\e[A": history-search-backward',
188 '"\e[A": history-search-backward',
189 '"\e[B": history-search-forward',
189 '"\e[B": history-search-forward',
190 '"\C-k": kill-line',
190 '"\C-k": kill-line',
191 '"\C-u": unix-line-discard',
191 '"\C-u": unix-line-discard',
192 ], allow_none=False, config=True)
192 ], allow_none=False, config=True)
193
193
194 # TODO: this part of prompt management should be moved to the frontends.
194 # TODO: this part of prompt management should be moved to the frontends.
195 # Use custom TraitTypes that convert '0'->'' and '\\n'->'\n'
195 # Use custom TraitTypes that convert '0'->'' and '\\n'->'\n'
196 separate_in = SeparateStr('\n', config=True)
196 separate_in = SeparateStr('\n', config=True)
197 separate_out = SeparateStr('\n', config=True)
197 separate_out = SeparateStr('', config=True)
198 separate_out2 = SeparateStr('\n', config=True)
198 separate_out2 = SeparateStr('', config=True)
199 system_header = Str('IPython system call: ', config=True)
199 system_header = Str('IPython system call: ', config=True)
200 system_verbose = CBool(False, config=True)
200 system_verbose = CBool(False, config=True)
201 wildcards_case_sensitive = CBool(True, config=True)
201 wildcards_case_sensitive = CBool(True, config=True)
202 xmode = CaselessStrEnum(('Context','Plain', 'Verbose'),
202 xmode = CaselessStrEnum(('Context','Plain', 'Verbose'),
203 default_value='Context', config=True)
203 default_value='Context', config=True)
204
204
205 # Subcomponents of InteractiveShell
205 # Subcomponents of InteractiveShell
206 alias_manager = Instance('IPython.core.alias.AliasManager')
206 alias_manager = Instance('IPython.core.alias.AliasManager')
207 prefilter_manager = Instance('IPython.core.prefilter.PrefilterManager')
207 prefilter_manager = Instance('IPython.core.prefilter.PrefilterManager')
208 builtin_trap = Instance('IPython.core.builtin_trap.BuiltinTrap')
208 builtin_trap = Instance('IPython.core.builtin_trap.BuiltinTrap')
209 display_trap = Instance('IPython.core.display_trap.DisplayTrap')
209 display_trap = Instance('IPython.core.display_trap.DisplayTrap')
210 extension_manager = Instance('IPython.core.extensions.ExtensionManager')
210 extension_manager = Instance('IPython.core.extensions.ExtensionManager')
211 plugin_manager = Instance('IPython.core.plugin.PluginManager')
211 plugin_manager = Instance('IPython.core.plugin.PluginManager')
212 payload_manager = Instance('IPython.core.payload.PayloadManager')
212 payload_manager = Instance('IPython.core.payload.PayloadManager')
213
213
214 def __init__(self, config=None, ipython_dir=None,
214 def __init__(self, config=None, ipython_dir=None,
215 user_ns=None, user_global_ns=None,
215 user_ns=None, user_global_ns=None,
216 custom_exceptions=((),None)):
216 custom_exceptions=((),None)):
217
217
218 # This is where traits with a config_key argument are updated
218 # This is where traits with a config_key argument are updated
219 # from the values on config.
219 # from the values on config.
220 super(InteractiveShell, self).__init__(config=config)
220 super(InteractiveShell, self).__init__(config=config)
221
221
222 # These are relatively independent and stateless
222 # These are relatively independent and stateless
223 self.init_ipython_dir(ipython_dir)
223 self.init_ipython_dir(ipython_dir)
224 self.init_instance_attrs()
224 self.init_instance_attrs()
225
225
226 # Create namespaces (user_ns, user_global_ns, etc.)
226 # Create namespaces (user_ns, user_global_ns, etc.)
227 self.init_create_namespaces(user_ns, user_global_ns)
227 self.init_create_namespaces(user_ns, user_global_ns)
228 # This has to be done after init_create_namespaces because it uses
228 # This has to be done after init_create_namespaces because it uses
229 # something in self.user_ns, but before init_sys_modules, which
229 # something in self.user_ns, but before init_sys_modules, which
230 # is the first thing to modify sys.
230 # is the first thing to modify sys.
231 # TODO: When we override sys.stdout and sys.stderr before this class
231 # TODO: When we override sys.stdout and sys.stderr before this class
232 # is created, we are saving the overridden ones here. Not sure if this
232 # is created, we are saving the overridden ones here. Not sure if this
233 # is what we want to do.
233 # is what we want to do.
234 self.save_sys_module_state()
234 self.save_sys_module_state()
235 self.init_sys_modules()
235 self.init_sys_modules()
236
236
237 self.init_history()
237 self.init_history()
238 self.init_encoding()
238 self.init_encoding()
239 self.init_prefilter()
239 self.init_prefilter()
240
240
241 Magic.__init__(self, self)
241 Magic.__init__(self, self)
242
242
243 self.init_syntax_highlighting()
243 self.init_syntax_highlighting()
244 self.init_hooks()
244 self.init_hooks()
245 self.init_pushd_popd_magic()
245 self.init_pushd_popd_magic()
246 # self.init_traceback_handlers use to be here, but we moved it below
246 # self.init_traceback_handlers use to be here, but we moved it below
247 # because it and init_io have to come after init_readline.
247 # because it and init_io have to come after init_readline.
248 self.init_user_ns()
248 self.init_user_ns()
249 self.init_logger()
249 self.init_logger()
250 self.init_alias()
250 self.init_alias()
251 self.init_builtins()
251 self.init_builtins()
252
252
253 # pre_config_initialization
253 # pre_config_initialization
254 self.init_shadow_hist()
254 self.init_shadow_hist()
255
255
256 # The next section should contain averything that was in ipmaker.
256 # The next section should contain averything that was in ipmaker.
257 self.init_logstart()
257 self.init_logstart()
258
258
259 # The following was in post_config_initialization
259 # The following was in post_config_initialization
260 self.init_inspector()
260 self.init_inspector()
261 # init_readline() must come before init_io(), because init_io uses
261 # init_readline() must come before init_io(), because init_io uses
262 # readline related things.
262 # readline related things.
263 self.init_readline()
263 self.init_readline()
264 # TODO: init_io() needs to happen before init_traceback handlers
264 # TODO: init_io() needs to happen before init_traceback handlers
265 # because the traceback handlers hardcode the stdout/stderr streams.
265 # because the traceback handlers hardcode the stdout/stderr streams.
266 # This logic in in debugger.Pdb and should eventually be changed.
266 # This logic in in debugger.Pdb and should eventually be changed.
267 self.init_io()
267 self.init_io()
268 self.init_traceback_handlers(custom_exceptions)
268 self.init_traceback_handlers(custom_exceptions)
269 self.init_prompts()
269 self.init_prompts()
270 self.init_displayhook()
270 self.init_displayhook()
271 self.init_reload_doctest()
271 self.init_reload_doctest()
272 self.init_magics()
272 self.init_magics()
273 self.init_pdb()
273 self.init_pdb()
274 self.init_extension_manager()
274 self.init_extension_manager()
275 self.init_plugin_manager()
275 self.init_plugin_manager()
276 self.init_payload()
276 self.init_payload()
277 self.hooks.late_startup_hook()
277 self.hooks.late_startup_hook()
278
278
279 @classmethod
279 @classmethod
280 def instance(cls, *args, **kwargs):
280 def instance(cls, *args, **kwargs):
281 """Returns a global InteractiveShell instance."""
281 """Returns a global InteractiveShell instance."""
282 if cls._instance is None:
282 if cls._instance is None:
283 inst = cls(*args, **kwargs)
283 inst = cls(*args, **kwargs)
284 # Now make sure that the instance will also be returned by
284 # Now make sure that the instance will also be returned by
285 # the subclasses instance attribute.
285 # the subclasses instance attribute.
286 for subclass in cls.mro():
286 for subclass in cls.mro():
287 if issubclass(cls, subclass) and issubclass(subclass, InteractiveShell):
287 if issubclass(cls, subclass) and issubclass(subclass, InteractiveShell):
288 subclass._instance = inst
288 subclass._instance = inst
289 else:
289 else:
290 break
290 break
291 if isinstance(cls._instance, cls):
291 if isinstance(cls._instance, cls):
292 return cls._instance
292 return cls._instance
293 else:
293 else:
294 raise MultipleInstanceError(
294 raise MultipleInstanceError(
295 'Multiple incompatible subclass instances of '
295 'Multiple incompatible subclass instances of '
296 'InteractiveShell are being created.'
296 'InteractiveShell are being created.'
297 )
297 )
298
298
299 @classmethod
299 @classmethod
300 def initialized(cls):
300 def initialized(cls):
301 return hasattr(cls, "_instance")
301 return hasattr(cls, "_instance")
302
302
303 def get_ipython(self):
303 def get_ipython(self):
304 """Return the currently running IPython instance."""
304 """Return the currently running IPython instance."""
305 return self
305 return self
306
306
307 #-------------------------------------------------------------------------
307 #-------------------------------------------------------------------------
308 # Trait changed handlers
308 # Trait changed handlers
309 #-------------------------------------------------------------------------
309 #-------------------------------------------------------------------------
310
310
311 def _ipython_dir_changed(self, name, new):
311 def _ipython_dir_changed(self, name, new):
312 if not os.path.isdir(new):
312 if not os.path.isdir(new):
313 os.makedirs(new, mode = 0777)
313 os.makedirs(new, mode = 0777)
314
314
315 def set_autoindent(self,value=None):
315 def set_autoindent(self,value=None):
316 """Set the autoindent flag, checking for readline support.
316 """Set the autoindent flag, checking for readline support.
317
317
318 If called with no arguments, it acts as a toggle."""
318 If called with no arguments, it acts as a toggle."""
319
319
320 if not self.has_readline:
320 if not self.has_readline:
321 if os.name == 'posix':
321 if os.name == 'posix':
322 warn("The auto-indent feature requires the readline library")
322 warn("The auto-indent feature requires the readline library")
323 self.autoindent = 0
323 self.autoindent = 0
324 return
324 return
325 if value is None:
325 if value is None:
326 self.autoindent = not self.autoindent
326 self.autoindent = not self.autoindent
327 else:
327 else:
328 self.autoindent = value
328 self.autoindent = value
329
329
330 #-------------------------------------------------------------------------
330 #-------------------------------------------------------------------------
331 # init_* methods called by __init__
331 # init_* methods called by __init__
332 #-------------------------------------------------------------------------
332 #-------------------------------------------------------------------------
333
333
334 def init_ipython_dir(self, ipython_dir):
334 def init_ipython_dir(self, ipython_dir):
335 if ipython_dir is not None:
335 if ipython_dir is not None:
336 self.ipython_dir = ipython_dir
336 self.ipython_dir = ipython_dir
337 self.config.Global.ipython_dir = self.ipython_dir
337 self.config.Global.ipython_dir = self.ipython_dir
338 return
338 return
339
339
340 if hasattr(self.config.Global, 'ipython_dir'):
340 if hasattr(self.config.Global, 'ipython_dir'):
341 self.ipython_dir = self.config.Global.ipython_dir
341 self.ipython_dir = self.config.Global.ipython_dir
342 else:
342 else:
343 self.ipython_dir = get_ipython_dir()
343 self.ipython_dir = get_ipython_dir()
344
344
345 # All children can just read this
345 # All children can just read this
346 self.config.Global.ipython_dir = self.ipython_dir
346 self.config.Global.ipython_dir = self.ipython_dir
347
347
348 def init_instance_attrs(self):
348 def init_instance_attrs(self):
349 self.more = False
349 self.more = False
350
350
351 # command compiler
351 # command compiler
352 self.compile = codeop.CommandCompiler()
352 self.compile = codeop.CommandCompiler()
353
353
354 # User input buffer
354 # User input buffer
355 self.buffer = []
355 self.buffer = []
356
356
357 # Make an empty namespace, which extension writers can rely on both
357 # Make an empty namespace, which extension writers can rely on both
358 # existing and NEVER being used by ipython itself. This gives them a
358 # existing and NEVER being used by ipython itself. This gives them a
359 # convenient location for storing additional information and state
359 # convenient location for storing additional information and state
360 # their extensions may require, without fear of collisions with other
360 # their extensions may require, without fear of collisions with other
361 # ipython names that may develop later.
361 # ipython names that may develop later.
362 self.meta = Struct()
362 self.meta = Struct()
363
363
364 # Object variable to store code object waiting execution. This is
364 # Object variable to store code object waiting execution. This is
365 # used mainly by the multithreaded shells, but it can come in handy in
365 # used mainly by the multithreaded shells, but it can come in handy in
366 # other situations. No need to use a Queue here, since it's a single
366 # other situations. No need to use a Queue here, since it's a single
367 # item which gets cleared once run.
367 # item which gets cleared once run.
368 self.code_to_run = None
368 self.code_to_run = None
369
369
370 # Temporary files used for various purposes. Deleted at exit.
370 # Temporary files used for various purposes. Deleted at exit.
371 self.tempfiles = []
371 self.tempfiles = []
372
372
373 # Keep track of readline usage (later set by init_readline)
373 # Keep track of readline usage (later set by init_readline)
374 self.has_readline = False
374 self.has_readline = False
375
375
376 # keep track of where we started running (mainly for crash post-mortem)
376 # keep track of where we started running (mainly for crash post-mortem)
377 # This is not being used anywhere currently.
377 # This is not being used anywhere currently.
378 self.starting_dir = os.getcwd()
378 self.starting_dir = os.getcwd()
379
379
380 # Indentation management
380 # Indentation management
381 self.indent_current_nsp = 0
381 self.indent_current_nsp = 0
382
382
383 def init_encoding(self):
383 def init_encoding(self):
384 # Get system encoding at startup time. Certain terminals (like Emacs
384 # Get system encoding at startup time. Certain terminals (like Emacs
385 # under Win32 have it set to None, and we need to have a known valid
385 # under Win32 have it set to None, and we need to have a known valid
386 # encoding to use in the raw_input() method
386 # encoding to use in the raw_input() method
387 try:
387 try:
388 self.stdin_encoding = sys.stdin.encoding or 'ascii'
388 self.stdin_encoding = sys.stdin.encoding or 'ascii'
389 except AttributeError:
389 except AttributeError:
390 self.stdin_encoding = 'ascii'
390 self.stdin_encoding = 'ascii'
391
391
392 def init_syntax_highlighting(self):
392 def init_syntax_highlighting(self):
393 # Python source parser/formatter for syntax highlighting
393 # Python source parser/formatter for syntax highlighting
394 pyformat = PyColorize.Parser().format
394 pyformat = PyColorize.Parser().format
395 self.pycolorize = lambda src: pyformat(src,'str',self.colors)
395 self.pycolorize = lambda src: pyformat(src,'str',self.colors)
396
396
397 def init_pushd_popd_magic(self):
397 def init_pushd_popd_magic(self):
398 # for pushd/popd management
398 # for pushd/popd management
399 try:
399 try:
400 self.home_dir = get_home_dir()
400 self.home_dir = get_home_dir()
401 except HomeDirError, msg:
401 except HomeDirError, msg:
402 fatal(msg)
402 fatal(msg)
403
403
404 self.dir_stack = []
404 self.dir_stack = []
405
405
406 def init_logger(self):
406 def init_logger(self):
407 self.logger = Logger(self, logfname='ipython_log.py', logmode='rotate')
407 self.logger = Logger(self, logfname='ipython_log.py', logmode='rotate')
408 # local shortcut, this is used a LOT
408 # local shortcut, this is used a LOT
409 self.log = self.logger.log
409 self.log = self.logger.log
410
410
411 def init_logstart(self):
411 def init_logstart(self):
412 if self.logappend:
412 if self.logappend:
413 self.magic_logstart(self.logappend + ' append')
413 self.magic_logstart(self.logappend + ' append')
414 elif self.logfile:
414 elif self.logfile:
415 self.magic_logstart(self.logfile)
415 self.magic_logstart(self.logfile)
416 elif self.logstart:
416 elif self.logstart:
417 self.magic_logstart()
417 self.magic_logstart()
418
418
419 def init_builtins(self):
419 def init_builtins(self):
420 self.builtin_trap = BuiltinTrap(shell=self)
420 self.builtin_trap = BuiltinTrap(shell=self)
421
421
422 def init_inspector(self):
422 def init_inspector(self):
423 # Object inspector
423 # Object inspector
424 self.inspector = oinspect.Inspector(oinspect.InspectColors,
424 self.inspector = oinspect.Inspector(oinspect.InspectColors,
425 PyColorize.ANSICodeColors,
425 PyColorize.ANSICodeColors,
426 'NoColor',
426 'NoColor',
427 self.object_info_string_level)
427 self.object_info_string_level)
428
428
429 def init_io(self):
429 def init_io(self):
430 import IPython.utils.io
430 import IPython.utils.io
431 if sys.platform == 'win32' and self.has_readline:
431 if sys.platform == 'win32' and self.has_readline:
432 Term = io.IOTerm(
432 Term = io.IOTerm(
433 cout=self.readline._outputfile,cerr=self.readline._outputfile
433 cout=self.readline._outputfile,cerr=self.readline._outputfile
434 )
434 )
435 else:
435 else:
436 Term = io.IOTerm()
436 Term = io.IOTerm()
437 io.Term = Term
437 io.Term = Term
438
438
439 def init_prompts(self):
439 def init_prompts(self):
440 # TODO: This is a pass for now because the prompts are managed inside
440 # TODO: This is a pass for now because the prompts are managed inside
441 # the DisplayHook. Once there is a separate prompt manager, this
441 # the DisplayHook. Once there is a separate prompt manager, this
442 # will initialize that object and all prompt related information.
442 # will initialize that object and all prompt related information.
443 pass
443 pass
444
444
445 def init_displayhook(self):
445 def init_displayhook(self):
446 # Initialize displayhook, set in/out prompts and printing system
446 # Initialize displayhook, set in/out prompts and printing system
447 self.displayhook = self.displayhook_class(
447 self.displayhook = self.displayhook_class(
448 shell=self,
448 shell=self,
449 cache_size=self.cache_size,
449 cache_size=self.cache_size,
450 input_sep = self.separate_in,
450 input_sep = self.separate_in,
451 output_sep = self.separate_out,
451 output_sep = self.separate_out,
452 output_sep2 = self.separate_out2,
452 output_sep2 = self.separate_out2,
453 ps1 = self.prompt_in1,
453 ps1 = self.prompt_in1,
454 ps2 = self.prompt_in2,
454 ps2 = self.prompt_in2,
455 ps_out = self.prompt_out,
455 ps_out = self.prompt_out,
456 pad_left = self.prompts_pad_left
456 pad_left = self.prompts_pad_left
457 )
457 )
458 # This is a context manager that installs/revmoes the displayhook at
458 # This is a context manager that installs/revmoes the displayhook at
459 # the appropriate time.
459 # the appropriate time.
460 self.display_trap = DisplayTrap(hook=self.displayhook)
460 self.display_trap = DisplayTrap(hook=self.displayhook)
461
461
462 def init_reload_doctest(self):
462 def init_reload_doctest(self):
463 # Do a proper resetting of doctest, including the necessary displayhook
463 # Do a proper resetting of doctest, including the necessary displayhook
464 # monkeypatching
464 # monkeypatching
465 try:
465 try:
466 doctest_reload()
466 doctest_reload()
467 except ImportError:
467 except ImportError:
468 warn("doctest module does not exist.")
468 warn("doctest module does not exist.")
469
469
470 #-------------------------------------------------------------------------
470 #-------------------------------------------------------------------------
471 # Things related to injections into the sys module
471 # Things related to injections into the sys module
472 #-------------------------------------------------------------------------
472 #-------------------------------------------------------------------------
473
473
474 def save_sys_module_state(self):
474 def save_sys_module_state(self):
475 """Save the state of hooks in the sys module.
475 """Save the state of hooks in the sys module.
476
476
477 This has to be called after self.user_ns is created.
477 This has to be called after self.user_ns is created.
478 """
478 """
479 self._orig_sys_module_state = {}
479 self._orig_sys_module_state = {}
480 self._orig_sys_module_state['stdin'] = sys.stdin
480 self._orig_sys_module_state['stdin'] = sys.stdin
481 self._orig_sys_module_state['stdout'] = sys.stdout
481 self._orig_sys_module_state['stdout'] = sys.stdout
482 self._orig_sys_module_state['stderr'] = sys.stderr
482 self._orig_sys_module_state['stderr'] = sys.stderr
483 self._orig_sys_module_state['excepthook'] = sys.excepthook
483 self._orig_sys_module_state['excepthook'] = sys.excepthook
484 try:
484 try:
485 self._orig_sys_modules_main_name = self.user_ns['__name__']
485 self._orig_sys_modules_main_name = self.user_ns['__name__']
486 except KeyError:
486 except KeyError:
487 pass
487 pass
488
488
489 def restore_sys_module_state(self):
489 def restore_sys_module_state(self):
490 """Restore the state of the sys module."""
490 """Restore the state of the sys module."""
491 try:
491 try:
492 for k, v in self._orig_sys_module_state.items():
492 for k, v in self._orig_sys_module_state.items():
493 setattr(sys, k, v)
493 setattr(sys, k, v)
494 except AttributeError:
494 except AttributeError:
495 pass
495 pass
496 try:
496 try:
497 delattr(sys, 'ipcompleter')
497 delattr(sys, 'ipcompleter')
498 except AttributeError:
498 except AttributeError:
499 pass
499 pass
500 # Reset what what done in self.init_sys_modules
500 # Reset what what done in self.init_sys_modules
501 try:
501 try:
502 sys.modules[self.user_ns['__name__']] = self._orig_sys_modules_main_name
502 sys.modules[self.user_ns['__name__']] = self._orig_sys_modules_main_name
503 except (AttributeError, KeyError):
503 except (AttributeError, KeyError):
504 pass
504 pass
505
505
506 #-------------------------------------------------------------------------
506 #-------------------------------------------------------------------------
507 # Things related to hooks
507 # Things related to hooks
508 #-------------------------------------------------------------------------
508 #-------------------------------------------------------------------------
509
509
510 def init_hooks(self):
510 def init_hooks(self):
511 # hooks holds pointers used for user-side customizations
511 # hooks holds pointers used for user-side customizations
512 self.hooks = Struct()
512 self.hooks = Struct()
513
513
514 self.strdispatchers = {}
514 self.strdispatchers = {}
515
515
516 # Set all default hooks, defined in the IPython.hooks module.
516 # Set all default hooks, defined in the IPython.hooks module.
517 hooks = IPython.core.hooks
517 hooks = IPython.core.hooks
518 for hook_name in hooks.__all__:
518 for hook_name in hooks.__all__:
519 # default hooks have priority 100, i.e. low; user hooks should have
519 # default hooks have priority 100, i.e. low; user hooks should have
520 # 0-100 priority
520 # 0-100 priority
521 self.set_hook(hook_name,getattr(hooks,hook_name), 100)
521 self.set_hook(hook_name,getattr(hooks,hook_name), 100)
522
522
523 def set_hook(self,name,hook, priority = 50, str_key = None, re_key = None):
523 def set_hook(self,name,hook, priority = 50, str_key = None, re_key = None):
524 """set_hook(name,hook) -> sets an internal IPython hook.
524 """set_hook(name,hook) -> sets an internal IPython hook.
525
525
526 IPython exposes some of its internal API as user-modifiable hooks. By
526 IPython exposes some of its internal API as user-modifiable hooks. By
527 adding your function to one of these hooks, you can modify IPython's
527 adding your function to one of these hooks, you can modify IPython's
528 behavior to call at runtime your own routines."""
528 behavior to call at runtime your own routines."""
529
529
530 # At some point in the future, this should validate the hook before it
530 # At some point in the future, this should validate the hook before it
531 # accepts it. Probably at least check that the hook takes the number
531 # accepts it. Probably at least check that the hook takes the number
532 # of args it's supposed to.
532 # of args it's supposed to.
533
533
534 f = new.instancemethod(hook,self,self.__class__)
534 f = new.instancemethod(hook,self,self.__class__)
535
535
536 # check if the hook is for strdispatcher first
536 # check if the hook is for strdispatcher first
537 if str_key is not None:
537 if str_key is not None:
538 sdp = self.strdispatchers.get(name, StrDispatch())
538 sdp = self.strdispatchers.get(name, StrDispatch())
539 sdp.add_s(str_key, f, priority )
539 sdp.add_s(str_key, f, priority )
540 self.strdispatchers[name] = sdp
540 self.strdispatchers[name] = sdp
541 return
541 return
542 if re_key is not None:
542 if re_key is not None:
543 sdp = self.strdispatchers.get(name, StrDispatch())
543 sdp = self.strdispatchers.get(name, StrDispatch())
544 sdp.add_re(re.compile(re_key), f, priority )
544 sdp.add_re(re.compile(re_key), f, priority )
545 self.strdispatchers[name] = sdp
545 self.strdispatchers[name] = sdp
546 return
546 return
547
547
548 dp = getattr(self.hooks, name, None)
548 dp = getattr(self.hooks, name, None)
549 if name not in IPython.core.hooks.__all__:
549 if name not in IPython.core.hooks.__all__:
550 print "Warning! Hook '%s' is not one of %s" % (name, IPython.core.hooks.__all__ )
550 print "Warning! Hook '%s' is not one of %s" % (name, IPython.core.hooks.__all__ )
551 if not dp:
551 if not dp:
552 dp = IPython.core.hooks.CommandChainDispatcher()
552 dp = IPython.core.hooks.CommandChainDispatcher()
553
553
554 try:
554 try:
555 dp.add(f,priority)
555 dp.add(f,priority)
556 except AttributeError:
556 except AttributeError:
557 # it was not commandchain, plain old func - replace
557 # it was not commandchain, plain old func - replace
558 dp = f
558 dp = f
559
559
560 setattr(self.hooks,name, dp)
560 setattr(self.hooks,name, dp)
561
561
562 #-------------------------------------------------------------------------
562 #-------------------------------------------------------------------------
563 # Things related to the "main" module
563 # Things related to the "main" module
564 #-------------------------------------------------------------------------
564 #-------------------------------------------------------------------------
565
565
566 def new_main_mod(self,ns=None):
566 def new_main_mod(self,ns=None):
567 """Return a new 'main' module object for user code execution.
567 """Return a new 'main' module object for user code execution.
568 """
568 """
569 main_mod = self._user_main_module
569 main_mod = self._user_main_module
570 init_fakemod_dict(main_mod,ns)
570 init_fakemod_dict(main_mod,ns)
571 return main_mod
571 return main_mod
572
572
573 def cache_main_mod(self,ns,fname):
573 def cache_main_mod(self,ns,fname):
574 """Cache a main module's namespace.
574 """Cache a main module's namespace.
575
575
576 When scripts are executed via %run, we must keep a reference to the
576 When scripts are executed via %run, we must keep a reference to the
577 namespace of their __main__ module (a FakeModule instance) around so
577 namespace of their __main__ module (a FakeModule instance) around so
578 that Python doesn't clear it, rendering objects defined therein
578 that Python doesn't clear it, rendering objects defined therein
579 useless.
579 useless.
580
580
581 This method keeps said reference in a private dict, keyed by the
581 This method keeps said reference in a private dict, keyed by the
582 absolute path of the module object (which corresponds to the script
582 absolute path of the module object (which corresponds to the script
583 path). This way, for multiple executions of the same script we only
583 path). This way, for multiple executions of the same script we only
584 keep one copy of the namespace (the last one), thus preventing memory
584 keep one copy of the namespace (the last one), thus preventing memory
585 leaks from old references while allowing the objects from the last
585 leaks from old references while allowing the objects from the last
586 execution to be accessible.
586 execution to be accessible.
587
587
588 Note: we can not allow the actual FakeModule instances to be deleted,
588 Note: we can not allow the actual FakeModule instances to be deleted,
589 because of how Python tears down modules (it hard-sets all their
589 because of how Python tears down modules (it hard-sets all their
590 references to None without regard for reference counts). This method
590 references to None without regard for reference counts). This method
591 must therefore make a *copy* of the given namespace, to allow the
591 must therefore make a *copy* of the given namespace, to allow the
592 original module's __dict__ to be cleared and reused.
592 original module's __dict__ to be cleared and reused.
593
593
594
594
595 Parameters
595 Parameters
596 ----------
596 ----------
597 ns : a namespace (a dict, typically)
597 ns : a namespace (a dict, typically)
598
598
599 fname : str
599 fname : str
600 Filename associated with the namespace.
600 Filename associated with the namespace.
601
601
602 Examples
602 Examples
603 --------
603 --------
604
604
605 In [10]: import IPython
605 In [10]: import IPython
606
606
607 In [11]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__)
607 In [11]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__)
608
608
609 In [12]: IPython.__file__ in _ip._main_ns_cache
609 In [12]: IPython.__file__ in _ip._main_ns_cache
610 Out[12]: True
610 Out[12]: True
611 """
611 """
612 self._main_ns_cache[os.path.abspath(fname)] = ns.copy()
612 self._main_ns_cache[os.path.abspath(fname)] = ns.copy()
613
613
614 def clear_main_mod_cache(self):
614 def clear_main_mod_cache(self):
615 """Clear the cache of main modules.
615 """Clear the cache of main modules.
616
616
617 Mainly for use by utilities like %reset.
617 Mainly for use by utilities like %reset.
618
618
619 Examples
619 Examples
620 --------
620 --------
621
621
622 In [15]: import IPython
622 In [15]: import IPython
623
623
624 In [16]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__)
624 In [16]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__)
625
625
626 In [17]: len(_ip._main_ns_cache) > 0
626 In [17]: len(_ip._main_ns_cache) > 0
627 Out[17]: True
627 Out[17]: True
628
628
629 In [18]: _ip.clear_main_mod_cache()
629 In [18]: _ip.clear_main_mod_cache()
630
630
631 In [19]: len(_ip._main_ns_cache) == 0
631 In [19]: len(_ip._main_ns_cache) == 0
632 Out[19]: True
632 Out[19]: True
633 """
633 """
634 self._main_ns_cache.clear()
634 self._main_ns_cache.clear()
635
635
636 #-------------------------------------------------------------------------
636 #-------------------------------------------------------------------------
637 # Things related to debugging
637 # Things related to debugging
638 #-------------------------------------------------------------------------
638 #-------------------------------------------------------------------------
639
639
640 def init_pdb(self):
640 def init_pdb(self):
641 # Set calling of pdb on exceptions
641 # Set calling of pdb on exceptions
642 # self.call_pdb is a property
642 # self.call_pdb is a property
643 self.call_pdb = self.pdb
643 self.call_pdb = self.pdb
644
644
645 def _get_call_pdb(self):
645 def _get_call_pdb(self):
646 return self._call_pdb
646 return self._call_pdb
647
647
648 def _set_call_pdb(self,val):
648 def _set_call_pdb(self,val):
649
649
650 if val not in (0,1,False,True):
650 if val not in (0,1,False,True):
651 raise ValueError,'new call_pdb value must be boolean'
651 raise ValueError,'new call_pdb value must be boolean'
652
652
653 # store value in instance
653 # store value in instance
654 self._call_pdb = val
654 self._call_pdb = val
655
655
656 # notify the actual exception handlers
656 # notify the actual exception handlers
657 self.InteractiveTB.call_pdb = val
657 self.InteractiveTB.call_pdb = val
658
658
659 call_pdb = property(_get_call_pdb,_set_call_pdb,None,
659 call_pdb = property(_get_call_pdb,_set_call_pdb,None,
660 'Control auto-activation of pdb at exceptions')
660 'Control auto-activation of pdb at exceptions')
661
661
662 def debugger(self,force=False):
662 def debugger(self,force=False):
663 """Call the pydb/pdb debugger.
663 """Call the pydb/pdb debugger.
664
664
665 Keywords:
665 Keywords:
666
666
667 - force(False): by default, this routine checks the instance call_pdb
667 - force(False): by default, this routine checks the instance call_pdb
668 flag and does not actually invoke the debugger if the flag is false.
668 flag and does not actually invoke the debugger if the flag is false.
669 The 'force' option forces the debugger to activate even if the flag
669 The 'force' option forces the debugger to activate even if the flag
670 is false.
670 is false.
671 """
671 """
672
672
673 if not (force or self.call_pdb):
673 if not (force or self.call_pdb):
674 return
674 return
675
675
676 if not hasattr(sys,'last_traceback'):
676 if not hasattr(sys,'last_traceback'):
677 error('No traceback has been produced, nothing to debug.')
677 error('No traceback has been produced, nothing to debug.')
678 return
678 return
679
679
680 # use pydb if available
680 # use pydb if available
681 if debugger.has_pydb:
681 if debugger.has_pydb:
682 from pydb import pm
682 from pydb import pm
683 else:
683 else:
684 # fallback to our internal debugger
684 # fallback to our internal debugger
685 pm = lambda : self.InteractiveTB.debugger(force=True)
685 pm = lambda : self.InteractiveTB.debugger(force=True)
686 self.history_saving_wrapper(pm)()
686 self.history_saving_wrapper(pm)()
687
687
688 #-------------------------------------------------------------------------
688 #-------------------------------------------------------------------------
689 # Things related to IPython's various namespaces
689 # Things related to IPython's various namespaces
690 #-------------------------------------------------------------------------
690 #-------------------------------------------------------------------------
691
691
692 def init_create_namespaces(self, user_ns=None, user_global_ns=None):
692 def init_create_namespaces(self, user_ns=None, user_global_ns=None):
693 # Create the namespace where the user will operate. user_ns is
693 # Create the namespace where the user will operate. user_ns is
694 # normally the only one used, and it is passed to the exec calls as
694 # normally the only one used, and it is passed to the exec calls as
695 # the locals argument. But we do carry a user_global_ns namespace
695 # the locals argument. But we do carry a user_global_ns namespace
696 # given as the exec 'globals' argument, This is useful in embedding
696 # given as the exec 'globals' argument, This is useful in embedding
697 # situations where the ipython shell opens in a context where the
697 # situations where the ipython shell opens in a context where the
698 # distinction between locals and globals is meaningful. For
698 # distinction between locals and globals is meaningful. For
699 # non-embedded contexts, it is just the same object as the user_ns dict.
699 # non-embedded contexts, it is just the same object as the user_ns dict.
700
700
701 # FIXME. For some strange reason, __builtins__ is showing up at user
701 # FIXME. For some strange reason, __builtins__ is showing up at user
702 # level as a dict instead of a module. This is a manual fix, but I
702 # level as a dict instead of a module. This is a manual fix, but I
703 # should really track down where the problem is coming from. Alex
703 # should really track down where the problem is coming from. Alex
704 # Schmolck reported this problem first.
704 # Schmolck reported this problem first.
705
705
706 # A useful post by Alex Martelli on this topic:
706 # A useful post by Alex Martelli on this topic:
707 # Re: inconsistent value from __builtins__
707 # Re: inconsistent value from __builtins__
708 # Von: Alex Martelli <aleaxit@yahoo.com>
708 # Von: Alex Martelli <aleaxit@yahoo.com>
709 # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends
709 # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends
710 # Gruppen: comp.lang.python
710 # Gruppen: comp.lang.python
711
711
712 # Michael Hohn <hohn@hooknose.lbl.gov> wrote:
712 # Michael Hohn <hohn@hooknose.lbl.gov> wrote:
713 # > >>> print type(builtin_check.get_global_binding('__builtins__'))
713 # > >>> print type(builtin_check.get_global_binding('__builtins__'))
714 # > <type 'dict'>
714 # > <type 'dict'>
715 # > >>> print type(__builtins__)
715 # > >>> print type(__builtins__)
716 # > <type 'module'>
716 # > <type 'module'>
717 # > Is this difference in return value intentional?
717 # > Is this difference in return value intentional?
718
718
719 # Well, it's documented that '__builtins__' can be either a dictionary
719 # Well, it's documented that '__builtins__' can be either a dictionary
720 # or a module, and it's been that way for a long time. Whether it's
720 # or a module, and it's been that way for a long time. Whether it's
721 # intentional (or sensible), I don't know. In any case, the idea is
721 # intentional (or sensible), I don't know. In any case, the idea is
722 # that if you need to access the built-in namespace directly, you
722 # that if you need to access the built-in namespace directly, you
723 # should start with "import __builtin__" (note, no 's') which will
723 # should start with "import __builtin__" (note, no 's') which will
724 # definitely give you a module. Yeah, it's somewhat confusing:-(.
724 # definitely give you a module. Yeah, it's somewhat confusing:-(.
725
725
726 # These routines return properly built dicts as needed by the rest of
726 # These routines return properly built dicts as needed by the rest of
727 # the code, and can also be used by extension writers to generate
727 # the code, and can also be used by extension writers to generate
728 # properly initialized namespaces.
728 # properly initialized namespaces.
729 user_ns, user_global_ns = self.make_user_namespaces(user_ns, user_global_ns)
729 user_ns, user_global_ns = self.make_user_namespaces(user_ns, user_global_ns)
730
730
731 # Assign namespaces
731 # Assign namespaces
732 # This is the namespace where all normal user variables live
732 # This is the namespace where all normal user variables live
733 self.user_ns = user_ns
733 self.user_ns = user_ns
734 self.user_global_ns = user_global_ns
734 self.user_global_ns = user_global_ns
735
735
736 # An auxiliary namespace that checks what parts of the user_ns were
736 # An auxiliary namespace that checks what parts of the user_ns were
737 # loaded at startup, so we can list later only variables defined in
737 # loaded at startup, so we can list later only variables defined in
738 # actual interactive use. Since it is always a subset of user_ns, it
738 # actual interactive use. Since it is always a subset of user_ns, it
739 # doesn't need to be separately tracked in the ns_table.
739 # doesn't need to be separately tracked in the ns_table.
740 self.user_ns_hidden = {}
740 self.user_ns_hidden = {}
741
741
742 # A namespace to keep track of internal data structures to prevent
742 # A namespace to keep track of internal data structures to prevent
743 # them from cluttering user-visible stuff. Will be updated later
743 # them from cluttering user-visible stuff. Will be updated later
744 self.internal_ns = {}
744 self.internal_ns = {}
745
745
746 # Now that FakeModule produces a real module, we've run into a nasty
746 # Now that FakeModule produces a real module, we've run into a nasty
747 # problem: after script execution (via %run), the module where the user
747 # problem: after script execution (via %run), the module where the user
748 # code ran is deleted. Now that this object is a true module (needed
748 # code ran is deleted. Now that this object is a true module (needed
749 # so docetst and other tools work correctly), the Python module
749 # so docetst and other tools work correctly), the Python module
750 # teardown mechanism runs over it, and sets to None every variable
750 # teardown mechanism runs over it, and sets to None every variable
751 # present in that module. Top-level references to objects from the
751 # present in that module. Top-level references to objects from the
752 # script survive, because the user_ns is updated with them. However,
752 # script survive, because the user_ns is updated with them. However,
753 # calling functions defined in the script that use other things from
753 # calling functions defined in the script that use other things from
754 # the script will fail, because the function's closure had references
754 # the script will fail, because the function's closure had references
755 # to the original objects, which are now all None. So we must protect
755 # to the original objects, which are now all None. So we must protect
756 # these modules from deletion by keeping a cache.
756 # these modules from deletion by keeping a cache.
757 #
757 #
758 # To avoid keeping stale modules around (we only need the one from the
758 # To avoid keeping stale modules around (we only need the one from the
759 # last run), we use a dict keyed with the full path to the script, so
759 # last run), we use a dict keyed with the full path to the script, so
760 # only the last version of the module is held in the cache. Note,
760 # only the last version of the module is held in the cache. Note,
761 # however, that we must cache the module *namespace contents* (their
761 # however, that we must cache the module *namespace contents* (their
762 # __dict__). Because if we try to cache the actual modules, old ones
762 # __dict__). Because if we try to cache the actual modules, old ones
763 # (uncached) could be destroyed while still holding references (such as
763 # (uncached) could be destroyed while still holding references (such as
764 # those held by GUI objects that tend to be long-lived)>
764 # those held by GUI objects that tend to be long-lived)>
765 #
765 #
766 # The %reset command will flush this cache. See the cache_main_mod()
766 # The %reset command will flush this cache. See the cache_main_mod()
767 # and clear_main_mod_cache() methods for details on use.
767 # and clear_main_mod_cache() methods for details on use.
768
768
769 # This is the cache used for 'main' namespaces
769 # This is the cache used for 'main' namespaces
770 self._main_ns_cache = {}
770 self._main_ns_cache = {}
771 # And this is the single instance of FakeModule whose __dict__ we keep
771 # And this is the single instance of FakeModule whose __dict__ we keep
772 # copying and clearing for reuse on each %run
772 # copying and clearing for reuse on each %run
773 self._user_main_module = FakeModule()
773 self._user_main_module = FakeModule()
774
774
775 # A table holding all the namespaces IPython deals with, so that
775 # A table holding all the namespaces IPython deals with, so that
776 # introspection facilities can search easily.
776 # introspection facilities can search easily.
777 self.ns_table = {'user':user_ns,
777 self.ns_table = {'user':user_ns,
778 'user_global':user_global_ns,
778 'user_global':user_global_ns,
779 'internal':self.internal_ns,
779 'internal':self.internal_ns,
780 'builtin':__builtin__.__dict__
780 'builtin':__builtin__.__dict__
781 }
781 }
782
782
783 # Similarly, track all namespaces where references can be held and that
783 # Similarly, track all namespaces where references can be held and that
784 # we can safely clear (so it can NOT include builtin). This one can be
784 # we can safely clear (so it can NOT include builtin). This one can be
785 # a simple list.
785 # a simple list.
786 self.ns_refs_table = [ user_ns, user_global_ns, self.user_ns_hidden,
786 self.ns_refs_table = [ user_ns, user_global_ns, self.user_ns_hidden,
787 self.internal_ns, self._main_ns_cache ]
787 self.internal_ns, self._main_ns_cache ]
788
788
789 def make_user_namespaces(self, user_ns=None, user_global_ns=None):
789 def make_user_namespaces(self, user_ns=None, user_global_ns=None):
790 """Return a valid local and global user interactive namespaces.
790 """Return a valid local and global user interactive namespaces.
791
791
792 This builds a dict with the minimal information needed to operate as a
792 This builds a dict with the minimal information needed to operate as a
793 valid IPython user namespace, which you can pass to the various
793 valid IPython user namespace, which you can pass to the various
794 embedding classes in ipython. The default implementation returns the
794 embedding classes in ipython. The default implementation returns the
795 same dict for both the locals and the globals to allow functions to
795 same dict for both the locals and the globals to allow functions to
796 refer to variables in the namespace. Customized implementations can
796 refer to variables in the namespace. Customized implementations can
797 return different dicts. The locals dictionary can actually be anything
797 return different dicts. The locals dictionary can actually be anything
798 following the basic mapping protocol of a dict, but the globals dict
798 following the basic mapping protocol of a dict, but the globals dict
799 must be a true dict, not even a subclass. It is recommended that any
799 must be a true dict, not even a subclass. It is recommended that any
800 custom object for the locals namespace synchronize with the globals
800 custom object for the locals namespace synchronize with the globals
801 dict somehow.
801 dict somehow.
802
802
803 Raises TypeError if the provided globals namespace is not a true dict.
803 Raises TypeError if the provided globals namespace is not a true dict.
804
804
805 Parameters
805 Parameters
806 ----------
806 ----------
807 user_ns : dict-like, optional
807 user_ns : dict-like, optional
808 The current user namespace. The items in this namespace should
808 The current user namespace. The items in this namespace should
809 be included in the output. If None, an appropriate blank
809 be included in the output. If None, an appropriate blank
810 namespace should be created.
810 namespace should be created.
811 user_global_ns : dict, optional
811 user_global_ns : dict, optional
812 The current user global namespace. The items in this namespace
812 The current user global namespace. The items in this namespace
813 should be included in the output. If None, an appropriate
813 should be included in the output. If None, an appropriate
814 blank namespace should be created.
814 blank namespace should be created.
815
815
816 Returns
816 Returns
817 -------
817 -------
818 A pair of dictionary-like object to be used as the local namespace
818 A pair of dictionary-like object to be used as the local namespace
819 of the interpreter and a dict to be used as the global namespace.
819 of the interpreter and a dict to be used as the global namespace.
820 """
820 """
821
821
822
822
823 # We must ensure that __builtin__ (without the final 's') is always
823 # We must ensure that __builtin__ (without the final 's') is always
824 # available and pointing to the __builtin__ *module*. For more details:
824 # available and pointing to the __builtin__ *module*. For more details:
825 # http://mail.python.org/pipermail/python-dev/2001-April/014068.html
825 # http://mail.python.org/pipermail/python-dev/2001-April/014068.html
826
826
827 if user_ns is None:
827 if user_ns is None:
828 # Set __name__ to __main__ to better match the behavior of the
828 # Set __name__ to __main__ to better match the behavior of the
829 # normal interpreter.
829 # normal interpreter.
830 user_ns = {'__name__' :'__main__',
830 user_ns = {'__name__' :'__main__',
831 '__builtin__' : __builtin__,
831 '__builtin__' : __builtin__,
832 '__builtins__' : __builtin__,
832 '__builtins__' : __builtin__,
833 }
833 }
834 else:
834 else:
835 user_ns.setdefault('__name__','__main__')
835 user_ns.setdefault('__name__','__main__')
836 user_ns.setdefault('__builtin__',__builtin__)
836 user_ns.setdefault('__builtin__',__builtin__)
837 user_ns.setdefault('__builtins__',__builtin__)
837 user_ns.setdefault('__builtins__',__builtin__)
838
838
839 if user_global_ns is None:
839 if user_global_ns is None:
840 user_global_ns = user_ns
840 user_global_ns = user_ns
841 if type(user_global_ns) is not dict:
841 if type(user_global_ns) is not dict:
842 raise TypeError("user_global_ns must be a true dict; got %r"
842 raise TypeError("user_global_ns must be a true dict; got %r"
843 % type(user_global_ns))
843 % type(user_global_ns))
844
844
845 return user_ns, user_global_ns
845 return user_ns, user_global_ns
846
846
847 def init_sys_modules(self):
847 def init_sys_modules(self):
848 # We need to insert into sys.modules something that looks like a
848 # We need to insert into sys.modules something that looks like a
849 # module but which accesses the IPython namespace, for shelve and
849 # module but which accesses the IPython namespace, for shelve and
850 # pickle to work interactively. Normally they rely on getting
850 # pickle to work interactively. Normally they rely on getting
851 # everything out of __main__, but for embedding purposes each IPython
851 # everything out of __main__, but for embedding purposes each IPython
852 # instance has its own private namespace, so we can't go shoving
852 # instance has its own private namespace, so we can't go shoving
853 # everything into __main__.
853 # everything into __main__.
854
854
855 # note, however, that we should only do this for non-embedded
855 # note, however, that we should only do this for non-embedded
856 # ipythons, which really mimic the __main__.__dict__ with their own
856 # ipythons, which really mimic the __main__.__dict__ with their own
857 # namespace. Embedded instances, on the other hand, should not do
857 # namespace. Embedded instances, on the other hand, should not do
858 # this because they need to manage the user local/global namespaces
858 # this because they need to manage the user local/global namespaces
859 # only, but they live within a 'normal' __main__ (meaning, they
859 # only, but they live within a 'normal' __main__ (meaning, they
860 # shouldn't overtake the execution environment of the script they're
860 # shouldn't overtake the execution environment of the script they're
861 # embedded in).
861 # embedded in).
862
862
863 # This is overridden in the InteractiveShellEmbed subclass to a no-op.
863 # This is overridden in the InteractiveShellEmbed subclass to a no-op.
864
864
865 try:
865 try:
866 main_name = self.user_ns['__name__']
866 main_name = self.user_ns['__name__']
867 except KeyError:
867 except KeyError:
868 raise KeyError('user_ns dictionary MUST have a "__name__" key')
868 raise KeyError('user_ns dictionary MUST have a "__name__" key')
869 else:
869 else:
870 sys.modules[main_name] = FakeModule(self.user_ns)
870 sys.modules[main_name] = FakeModule(self.user_ns)
871
871
872 def init_user_ns(self):
872 def init_user_ns(self):
873 """Initialize all user-visible namespaces to their minimum defaults.
873 """Initialize all user-visible namespaces to their minimum defaults.
874
874
875 Certain history lists are also initialized here, as they effectively
875 Certain history lists are also initialized here, as they effectively
876 act as user namespaces.
876 act as user namespaces.
877
877
878 Notes
878 Notes
879 -----
879 -----
880 All data structures here are only filled in, they are NOT reset by this
880 All data structures here are only filled in, they are NOT reset by this
881 method. If they were not empty before, data will simply be added to
881 method. If they were not empty before, data will simply be added to
882 therm.
882 therm.
883 """
883 """
884 # This function works in two parts: first we put a few things in
884 # This function works in two parts: first we put a few things in
885 # user_ns, and we sync that contents into user_ns_hidden so that these
885 # user_ns, and we sync that contents into user_ns_hidden so that these
886 # initial variables aren't shown by %who. After the sync, we add the
886 # initial variables aren't shown by %who. After the sync, we add the
887 # rest of what we *do* want the user to see with %who even on a new
887 # rest of what we *do* want the user to see with %who even on a new
888 # session (probably nothing, so theye really only see their own stuff)
888 # session (probably nothing, so theye really only see their own stuff)
889
889
890 # The user dict must *always* have a __builtin__ reference to the
890 # The user dict must *always* have a __builtin__ reference to the
891 # Python standard __builtin__ namespace, which must be imported.
891 # Python standard __builtin__ namespace, which must be imported.
892 # This is so that certain operations in prompt evaluation can be
892 # This is so that certain operations in prompt evaluation can be
893 # reliably executed with builtins. Note that we can NOT use
893 # reliably executed with builtins. Note that we can NOT use
894 # __builtins__ (note the 's'), because that can either be a dict or a
894 # __builtins__ (note the 's'), because that can either be a dict or a
895 # module, and can even mutate at runtime, depending on the context
895 # module, and can even mutate at runtime, depending on the context
896 # (Python makes no guarantees on it). In contrast, __builtin__ is
896 # (Python makes no guarantees on it). In contrast, __builtin__ is
897 # always a module object, though it must be explicitly imported.
897 # always a module object, though it must be explicitly imported.
898
898
899 # For more details:
899 # For more details:
900 # http://mail.python.org/pipermail/python-dev/2001-April/014068.html
900 # http://mail.python.org/pipermail/python-dev/2001-April/014068.html
901 ns = dict(__builtin__ = __builtin__)
901 ns = dict(__builtin__ = __builtin__)
902
902
903 # Put 'help' in the user namespace
903 # Put 'help' in the user namespace
904 try:
904 try:
905 from site import _Helper
905 from site import _Helper
906 ns['help'] = _Helper()
906 ns['help'] = _Helper()
907 except ImportError:
907 except ImportError:
908 warn('help() not available - check site.py')
908 warn('help() not available - check site.py')
909
909
910 # make global variables for user access to the histories
910 # make global variables for user access to the histories
911 ns['_ih'] = self.input_hist
911 ns['_ih'] = self.input_hist
912 ns['_oh'] = self.output_hist
912 ns['_oh'] = self.output_hist
913 ns['_dh'] = self.dir_hist
913 ns['_dh'] = self.dir_hist
914
914
915 ns['_sh'] = shadowns
915 ns['_sh'] = shadowns
916
916
917 # user aliases to input and output histories. These shouldn't show up
917 # user aliases to input and output histories. These shouldn't show up
918 # in %who, as they can have very large reprs.
918 # in %who, as they can have very large reprs.
919 ns['In'] = self.input_hist
919 ns['In'] = self.input_hist
920 ns['Out'] = self.output_hist
920 ns['Out'] = self.output_hist
921
921
922 # Store myself as the public api!!!
922 # Store myself as the public api!!!
923 ns['get_ipython'] = self.get_ipython
923 ns['get_ipython'] = self.get_ipython
924
924
925 # Sync what we've added so far to user_ns_hidden so these aren't seen
925 # Sync what we've added so far to user_ns_hidden so these aren't seen
926 # by %who
926 # by %who
927 self.user_ns_hidden.update(ns)
927 self.user_ns_hidden.update(ns)
928
928
929 # Anything put into ns now would show up in %who. Think twice before
929 # Anything put into ns now would show up in %who. Think twice before
930 # putting anything here, as we really want %who to show the user their
930 # putting anything here, as we really want %who to show the user their
931 # stuff, not our variables.
931 # stuff, not our variables.
932
932
933 # Finally, update the real user's namespace
933 # Finally, update the real user's namespace
934 self.user_ns.update(ns)
934 self.user_ns.update(ns)
935
935
936
936
937 def reset(self):
937 def reset(self):
938 """Clear all internal namespaces.
938 """Clear all internal namespaces.
939
939
940 Note that this is much more aggressive than %reset, since it clears
940 Note that this is much more aggressive than %reset, since it clears
941 fully all namespaces, as well as all input/output lists.
941 fully all namespaces, as well as all input/output lists.
942 """
942 """
943 for ns in self.ns_refs_table:
943 for ns in self.ns_refs_table:
944 ns.clear()
944 ns.clear()
945
945
946 self.alias_manager.clear_aliases()
946 self.alias_manager.clear_aliases()
947
947
948 # Clear input and output histories
948 # Clear input and output histories
949 self.input_hist[:] = []
949 self.input_hist[:] = []
950 self.input_hist_raw[:] = []
950 self.input_hist_raw[:] = []
951 self.output_hist.clear()
951 self.output_hist.clear()
952
952
953 # Restore the user namespaces to minimal usability
953 # Restore the user namespaces to minimal usability
954 self.init_user_ns()
954 self.init_user_ns()
955
955
956 # Restore the default and user aliases
956 # Restore the default and user aliases
957 self.alias_manager.init_aliases()
957 self.alias_manager.init_aliases()
958
958
959 def reset_selective(self, regex=None):
959 def reset_selective(self, regex=None):
960 """Clear selective variables from internal namespaces based on a specified regular expression.
960 """Clear selective variables from internal namespaces based on a specified regular expression.
961
961
962 Parameters
962 Parameters
963 ----------
963 ----------
964 regex : string or compiled pattern, optional
964 regex : string or compiled pattern, optional
965 A regular expression pattern that will be used in searching variable names in the users
965 A regular expression pattern that will be used in searching variable names in the users
966 namespaces.
966 namespaces.
967 """
967 """
968 if regex is not None:
968 if regex is not None:
969 try:
969 try:
970 m = re.compile(regex)
970 m = re.compile(regex)
971 except TypeError:
971 except TypeError:
972 raise TypeError('regex must be a string or compiled pattern')
972 raise TypeError('regex must be a string or compiled pattern')
973 # Search for keys in each namespace that match the given regex
973 # Search for keys in each namespace that match the given regex
974 # If a match is found, delete the key/value pair.
974 # If a match is found, delete the key/value pair.
975 for ns in self.ns_refs_table:
975 for ns in self.ns_refs_table:
976 for var in ns:
976 for var in ns:
977 if m.search(var):
977 if m.search(var):
978 del ns[var]
978 del ns[var]
979
979
980 def push(self, variables, interactive=True):
980 def push(self, variables, interactive=True):
981 """Inject a group of variables into the IPython user namespace.
981 """Inject a group of variables into the IPython user namespace.
982
982
983 Parameters
983 Parameters
984 ----------
984 ----------
985 variables : dict, str or list/tuple of str
985 variables : dict, str or list/tuple of str
986 The variables to inject into the user's namespace. If a dict,
986 The variables to inject into the user's namespace. If a dict,
987 a simple update is done. If a str, the string is assumed to
987 a simple update is done. If a str, the string is assumed to
988 have variable names separated by spaces. A list/tuple of str
988 have variable names separated by spaces. A list/tuple of str
989 can also be used to give the variable names. If just the variable
989 can also be used to give the variable names. If just the variable
990 names are give (list/tuple/str) then the variable values looked
990 names are give (list/tuple/str) then the variable values looked
991 up in the callers frame.
991 up in the callers frame.
992 interactive : bool
992 interactive : bool
993 If True (default), the variables will be listed with the ``who``
993 If True (default), the variables will be listed with the ``who``
994 magic.
994 magic.
995 """
995 """
996 vdict = None
996 vdict = None
997
997
998 # We need a dict of name/value pairs to do namespace updates.
998 # We need a dict of name/value pairs to do namespace updates.
999 if isinstance(variables, dict):
999 if isinstance(variables, dict):
1000 vdict = variables
1000 vdict = variables
1001 elif isinstance(variables, (basestring, list, tuple)):
1001 elif isinstance(variables, (basestring, list, tuple)):
1002 if isinstance(variables, basestring):
1002 if isinstance(variables, basestring):
1003 vlist = variables.split()
1003 vlist = variables.split()
1004 else:
1004 else:
1005 vlist = variables
1005 vlist = variables
1006 vdict = {}
1006 vdict = {}
1007 cf = sys._getframe(1)
1007 cf = sys._getframe(1)
1008 for name in vlist:
1008 for name in vlist:
1009 try:
1009 try:
1010 vdict[name] = eval(name, cf.f_globals, cf.f_locals)
1010 vdict[name] = eval(name, cf.f_globals, cf.f_locals)
1011 except:
1011 except:
1012 print ('Could not get variable %s from %s' %
1012 print ('Could not get variable %s from %s' %
1013 (name,cf.f_code.co_name))
1013 (name,cf.f_code.co_name))
1014 else:
1014 else:
1015 raise ValueError('variables must be a dict/str/list/tuple')
1015 raise ValueError('variables must be a dict/str/list/tuple')
1016
1016
1017 # Propagate variables to user namespace
1017 # Propagate variables to user namespace
1018 self.user_ns.update(vdict)
1018 self.user_ns.update(vdict)
1019
1019
1020 # And configure interactive visibility
1020 # And configure interactive visibility
1021 config_ns = self.user_ns_hidden
1021 config_ns = self.user_ns_hidden
1022 if interactive:
1022 if interactive:
1023 for name, val in vdict.iteritems():
1023 for name, val in vdict.iteritems():
1024 config_ns.pop(name, None)
1024 config_ns.pop(name, None)
1025 else:
1025 else:
1026 for name,val in vdict.iteritems():
1026 for name,val in vdict.iteritems():
1027 config_ns[name] = val
1027 config_ns[name] = val
1028
1028
1029 #-------------------------------------------------------------------------
1029 #-------------------------------------------------------------------------
1030 # Things related to history management
1030 # Things related to history management
1031 #-------------------------------------------------------------------------
1031 #-------------------------------------------------------------------------
1032
1032
1033 def init_history(self):
1033 def init_history(self):
1034 # List of input with multi-line handling.
1034 # List of input with multi-line handling.
1035 self.input_hist = InputList()
1035 self.input_hist = InputList()
1036 # This one will hold the 'raw' input history, without any
1036 # This one will hold the 'raw' input history, without any
1037 # pre-processing. This will allow users to retrieve the input just as
1037 # pre-processing. This will allow users to retrieve the input just as
1038 # it was exactly typed in by the user, with %hist -r.
1038 # it was exactly typed in by the user, with %hist -r.
1039 self.input_hist_raw = InputList()
1039 self.input_hist_raw = InputList()
1040
1040
1041 # list of visited directories
1041 # list of visited directories
1042 try:
1042 try:
1043 self.dir_hist = [os.getcwd()]
1043 self.dir_hist = [os.getcwd()]
1044 except OSError:
1044 except OSError:
1045 self.dir_hist = []
1045 self.dir_hist = []
1046
1046
1047 # dict of output history
1047 # dict of output history
1048 self.output_hist = {}
1048 self.output_hist = {}
1049
1049
1050 # Now the history file
1050 # Now the history file
1051 if self.profile:
1051 if self.profile:
1052 histfname = 'history-%s' % self.profile
1052 histfname = 'history-%s' % self.profile
1053 else:
1053 else:
1054 histfname = 'history'
1054 histfname = 'history'
1055 self.histfile = os.path.join(self.ipython_dir, histfname)
1055 self.histfile = os.path.join(self.ipython_dir, histfname)
1056
1056
1057 # Fill the history zero entry, user counter starts at 1
1057 # Fill the history zero entry, user counter starts at 1
1058 self.input_hist.append('\n')
1058 self.input_hist.append('\n')
1059 self.input_hist_raw.append('\n')
1059 self.input_hist_raw.append('\n')
1060
1060
1061 def init_shadow_hist(self):
1061 def init_shadow_hist(self):
1062 try:
1062 try:
1063 self.db = pickleshare.PickleShareDB(self.ipython_dir + "/db")
1063 self.db = pickleshare.PickleShareDB(self.ipython_dir + "/db")
1064 except exceptions.UnicodeDecodeError:
1064 except exceptions.UnicodeDecodeError:
1065 print "Your ipython_dir can't be decoded to unicode!"
1065 print "Your ipython_dir can't be decoded to unicode!"
1066 print "Please set HOME environment variable to something that"
1066 print "Please set HOME environment variable to something that"
1067 print r"only has ASCII characters, e.g. c:\home"
1067 print r"only has ASCII characters, e.g. c:\home"
1068 print "Now it is", self.ipython_dir
1068 print "Now it is", self.ipython_dir
1069 sys.exit()
1069 sys.exit()
1070 self.shadowhist = ipcorehist.ShadowHist(self.db)
1070 self.shadowhist = ipcorehist.ShadowHist(self.db)
1071
1071
1072 def savehist(self):
1072 def savehist(self):
1073 """Save input history to a file (via readline library)."""
1073 """Save input history to a file (via readline library)."""
1074
1074
1075 try:
1075 try:
1076 self.readline.write_history_file(self.histfile)
1076 self.readline.write_history_file(self.histfile)
1077 except:
1077 except:
1078 print 'Unable to save IPython command history to file: ' + \
1078 print 'Unable to save IPython command history to file: ' + \
1079 `self.histfile`
1079 `self.histfile`
1080
1080
1081 def reloadhist(self):
1081 def reloadhist(self):
1082 """Reload the input history from disk file."""
1082 """Reload the input history from disk file."""
1083
1083
1084 try:
1084 try:
1085 self.readline.clear_history()
1085 self.readline.clear_history()
1086 self.readline.read_history_file(self.shell.histfile)
1086 self.readline.read_history_file(self.shell.histfile)
1087 except AttributeError:
1087 except AttributeError:
1088 pass
1088 pass
1089
1089
1090 def history_saving_wrapper(self, func):
1090 def history_saving_wrapper(self, func):
1091 """ Wrap func for readline history saving
1091 """ Wrap func for readline history saving
1092
1092
1093 Convert func into callable that saves & restores
1093 Convert func into callable that saves & restores
1094 history around the call """
1094 history around the call """
1095
1095
1096 if self.has_readline:
1096 if self.has_readline:
1097 from IPython.utils import rlineimpl as readline
1097 from IPython.utils import rlineimpl as readline
1098 else:
1098 else:
1099 return func
1099 return func
1100
1100
1101 def wrapper():
1101 def wrapper():
1102 self.savehist()
1102 self.savehist()
1103 try:
1103 try:
1104 func()
1104 func()
1105 finally:
1105 finally:
1106 readline.read_history_file(self.histfile)
1106 readline.read_history_file(self.histfile)
1107 return wrapper
1107 return wrapper
1108
1108
1109 def get_history(self, index=None, raw=False, output=True):
1109 def get_history(self, index=None, raw=False, output=True):
1110 """Get the history list.
1110 """Get the history list.
1111
1111
1112 Get the input and output history.
1112 Get the input and output history.
1113
1113
1114 Parameters
1114 Parameters
1115 ----------
1115 ----------
1116 index : n or (n1, n2) or None
1116 index : n or (n1, n2) or None
1117 If n, then the last entries. If a tuple, then all in
1117 If n, then the last entries. If a tuple, then all in
1118 range(n1, n2). If None, then all entries. Raises IndexError if
1118 range(n1, n2). If None, then all entries. Raises IndexError if
1119 the format of index is incorrect.
1119 the format of index is incorrect.
1120 raw : bool
1120 raw : bool
1121 If True, return the raw input.
1121 If True, return the raw input.
1122 output : bool
1122 output : bool
1123 If True, then return the output as well.
1123 If True, then return the output as well.
1124
1124
1125 Returns
1125 Returns
1126 -------
1126 -------
1127 If output is True, then return a dict of tuples, keyed by the prompt
1127 If output is True, then return a dict of tuples, keyed by the prompt
1128 numbers and with values of (input, output). If output is False, then
1128 numbers and with values of (input, output). If output is False, then
1129 a dict, keyed by the prompt number with the values of input. Raises
1129 a dict, keyed by the prompt number with the values of input. Raises
1130 IndexError if no history is found.
1130 IndexError if no history is found.
1131 """
1131 """
1132 if raw:
1132 if raw:
1133 input_hist = self.input_hist_raw
1133 input_hist = self.input_hist_raw
1134 else:
1134 else:
1135 input_hist = self.input_hist
1135 input_hist = self.input_hist
1136 if output:
1136 if output:
1137 output_hist = self.user_ns['Out']
1137 output_hist = self.user_ns['Out']
1138 n = len(input_hist)
1138 n = len(input_hist)
1139 if index is None:
1139 if index is None:
1140 start=0; stop=n
1140 start=0; stop=n
1141 elif isinstance(index, int):
1141 elif isinstance(index, int):
1142 start=n-index; stop=n
1142 start=n-index; stop=n
1143 elif isinstance(index, tuple) and len(index) == 2:
1143 elif isinstance(index, tuple) and len(index) == 2:
1144 start=index[0]; stop=index[1]
1144 start=index[0]; stop=index[1]
1145 else:
1145 else:
1146 raise IndexError('Not a valid index for the input history: %r' % index)
1146 raise IndexError('Not a valid index for the input history: %r' % index)
1147 hist = {}
1147 hist = {}
1148 for i in range(start, stop):
1148 for i in range(start, stop):
1149 if output:
1149 if output:
1150 hist[i] = (input_hist[i], output_hist.get(i))
1150 hist[i] = (input_hist[i], output_hist.get(i))
1151 else:
1151 else:
1152 hist[i] = input_hist[i]
1152 hist[i] = input_hist[i]
1153 if len(hist)==0:
1153 if len(hist)==0:
1154 raise IndexError('No history for range of indices: %r' % index)
1154 raise IndexError('No history for range of indices: %r' % index)
1155 return hist
1155 return hist
1156
1156
1157 #-------------------------------------------------------------------------
1157 #-------------------------------------------------------------------------
1158 # Things related to exception handling and tracebacks (not debugging)
1158 # Things related to exception handling and tracebacks (not debugging)
1159 #-------------------------------------------------------------------------
1159 #-------------------------------------------------------------------------
1160
1160
1161 def init_traceback_handlers(self, custom_exceptions):
1161 def init_traceback_handlers(self, custom_exceptions):
1162 # Syntax error handler.
1162 # Syntax error handler.
1163 self.SyntaxTB = ultratb.SyntaxTB(color_scheme='NoColor')
1163 self.SyntaxTB = ultratb.SyntaxTB(color_scheme='NoColor')
1164
1164
1165 # The interactive one is initialized with an offset, meaning we always
1165 # The interactive one is initialized with an offset, meaning we always
1166 # want to remove the topmost item in the traceback, which is our own
1166 # want to remove the topmost item in the traceback, which is our own
1167 # internal code. Valid modes: ['Plain','Context','Verbose']
1167 # internal code. Valid modes: ['Plain','Context','Verbose']
1168 self.InteractiveTB = ultratb.AutoFormattedTB(mode = 'Plain',
1168 self.InteractiveTB = ultratb.AutoFormattedTB(mode = 'Plain',
1169 color_scheme='NoColor',
1169 color_scheme='NoColor',
1170 tb_offset = 1)
1170 tb_offset = 1)
1171
1171
1172 # The instance will store a pointer to the system-wide exception hook,
1172 # The instance will store a pointer to the system-wide exception hook,
1173 # so that runtime code (such as magics) can access it. This is because
1173 # so that runtime code (such as magics) can access it. This is because
1174 # during the read-eval loop, it may get temporarily overwritten.
1174 # during the read-eval loop, it may get temporarily overwritten.
1175 self.sys_excepthook = sys.excepthook
1175 self.sys_excepthook = sys.excepthook
1176
1176
1177 # and add any custom exception handlers the user may have specified
1177 # and add any custom exception handlers the user may have specified
1178 self.set_custom_exc(*custom_exceptions)
1178 self.set_custom_exc(*custom_exceptions)
1179
1179
1180 # Set the exception mode
1180 # Set the exception mode
1181 self.InteractiveTB.set_mode(mode=self.xmode)
1181 self.InteractiveTB.set_mode(mode=self.xmode)
1182
1182
1183 def set_custom_exc(self, exc_tuple, handler):
1183 def set_custom_exc(self, exc_tuple, handler):
1184 """set_custom_exc(exc_tuple,handler)
1184 """set_custom_exc(exc_tuple,handler)
1185
1185
1186 Set a custom exception handler, which will be called if any of the
1186 Set a custom exception handler, which will be called if any of the
1187 exceptions in exc_tuple occur in the mainloop (specifically, in the
1187 exceptions in exc_tuple occur in the mainloop (specifically, in the
1188 runcode() method.
1188 runcode() method.
1189
1189
1190 Inputs:
1190 Inputs:
1191
1191
1192 - exc_tuple: a *tuple* of valid exceptions to call the defined
1192 - exc_tuple: a *tuple* of valid exceptions to call the defined
1193 handler for. It is very important that you use a tuple, and NOT A
1193 handler for. It is very important that you use a tuple, and NOT A
1194 LIST here, because of the way Python's except statement works. If
1194 LIST here, because of the way Python's except statement works. If
1195 you only want to trap a single exception, use a singleton tuple:
1195 you only want to trap a single exception, use a singleton tuple:
1196
1196
1197 exc_tuple == (MyCustomException,)
1197 exc_tuple == (MyCustomException,)
1198
1198
1199 - handler: this must be defined as a function with the following
1199 - handler: this must be defined as a function with the following
1200 basic interface::
1200 basic interface::
1201
1201
1202 def my_handler(self, etype, value, tb, tb_offset=None)
1202 def my_handler(self, etype, value, tb, tb_offset=None)
1203 ...
1203 ...
1204 # The return value must be
1204 # The return value must be
1205 return structured_traceback
1205 return structured_traceback
1206
1206
1207 This will be made into an instance method (via new.instancemethod)
1207 This will be made into an instance method (via new.instancemethod)
1208 of IPython itself, and it will be called if any of the exceptions
1208 of IPython itself, and it will be called if any of the exceptions
1209 listed in the exc_tuple are caught. If the handler is None, an
1209 listed in the exc_tuple are caught. If the handler is None, an
1210 internal basic one is used, which just prints basic info.
1210 internal basic one is used, which just prints basic info.
1211
1211
1212 WARNING: by putting in your own exception handler into IPython's main
1212 WARNING: by putting in your own exception handler into IPython's main
1213 execution loop, you run a very good chance of nasty crashes. This
1213 execution loop, you run a very good chance of nasty crashes. This
1214 facility should only be used if you really know what you are doing."""
1214 facility should only be used if you really know what you are doing."""
1215
1215
1216 assert type(exc_tuple)==type(()) , \
1216 assert type(exc_tuple)==type(()) , \
1217 "The custom exceptions must be given AS A TUPLE."
1217 "The custom exceptions must be given AS A TUPLE."
1218
1218
1219 def dummy_handler(self,etype,value,tb):
1219 def dummy_handler(self,etype,value,tb):
1220 print '*** Simple custom exception handler ***'
1220 print '*** Simple custom exception handler ***'
1221 print 'Exception type :',etype
1221 print 'Exception type :',etype
1222 print 'Exception value:',value
1222 print 'Exception value:',value
1223 print 'Traceback :',tb
1223 print 'Traceback :',tb
1224 print 'Source code :','\n'.join(self.buffer)
1224 print 'Source code :','\n'.join(self.buffer)
1225
1225
1226 if handler is None: handler = dummy_handler
1226 if handler is None: handler = dummy_handler
1227
1227
1228 self.CustomTB = new.instancemethod(handler,self,self.__class__)
1228 self.CustomTB = new.instancemethod(handler,self,self.__class__)
1229 self.custom_exceptions = exc_tuple
1229 self.custom_exceptions = exc_tuple
1230
1230
1231 def excepthook(self, etype, value, tb):
1231 def excepthook(self, etype, value, tb):
1232 """One more defense for GUI apps that call sys.excepthook.
1232 """One more defense for GUI apps that call sys.excepthook.
1233
1233
1234 GUI frameworks like wxPython trap exceptions and call
1234 GUI frameworks like wxPython trap exceptions and call
1235 sys.excepthook themselves. I guess this is a feature that
1235 sys.excepthook themselves. I guess this is a feature that
1236 enables them to keep running after exceptions that would
1236 enables them to keep running after exceptions that would
1237 otherwise kill their mainloop. This is a bother for IPython
1237 otherwise kill their mainloop. This is a bother for IPython
1238 which excepts to catch all of the program exceptions with a try:
1238 which excepts to catch all of the program exceptions with a try:
1239 except: statement.
1239 except: statement.
1240
1240
1241 Normally, IPython sets sys.excepthook to a CrashHandler instance, so if
1241 Normally, IPython sets sys.excepthook to a CrashHandler instance, so if
1242 any app directly invokes sys.excepthook, it will look to the user like
1242 any app directly invokes sys.excepthook, it will look to the user like
1243 IPython crashed. In order to work around this, we can disable the
1243 IPython crashed. In order to work around this, we can disable the
1244 CrashHandler and replace it with this excepthook instead, which prints a
1244 CrashHandler and replace it with this excepthook instead, which prints a
1245 regular traceback using our InteractiveTB. In this fashion, apps which
1245 regular traceback using our InteractiveTB. In this fashion, apps which
1246 call sys.excepthook will generate a regular-looking exception from
1246 call sys.excepthook will generate a regular-looking exception from
1247 IPython, and the CrashHandler will only be triggered by real IPython
1247 IPython, and the CrashHandler will only be triggered by real IPython
1248 crashes.
1248 crashes.
1249
1249
1250 This hook should be used sparingly, only in places which are not likely
1250 This hook should be used sparingly, only in places which are not likely
1251 to be true IPython errors.
1251 to be true IPython errors.
1252 """
1252 """
1253 self.showtraceback((etype,value,tb),tb_offset=0)
1253 self.showtraceback((etype,value,tb),tb_offset=0)
1254
1254
1255 def showtraceback(self,exc_tuple = None,filename=None,tb_offset=None,
1255 def showtraceback(self,exc_tuple = None,filename=None,tb_offset=None,
1256 exception_only=False):
1256 exception_only=False):
1257 """Display the exception that just occurred.
1257 """Display the exception that just occurred.
1258
1258
1259 If nothing is known about the exception, this is the method which
1259 If nothing is known about the exception, this is the method which
1260 should be used throughout the code for presenting user tracebacks,
1260 should be used throughout the code for presenting user tracebacks,
1261 rather than directly invoking the InteractiveTB object.
1261 rather than directly invoking the InteractiveTB object.
1262
1262
1263 A specific showsyntaxerror() also exists, but this method can take
1263 A specific showsyntaxerror() also exists, but this method can take
1264 care of calling it if needed, so unless you are explicitly catching a
1264 care of calling it if needed, so unless you are explicitly catching a
1265 SyntaxError exception, don't try to analyze the stack manually and
1265 SyntaxError exception, don't try to analyze the stack manually and
1266 simply call this method."""
1266 simply call this method."""
1267
1267
1268 try:
1268 try:
1269 if exc_tuple is None:
1269 if exc_tuple is None:
1270 etype, value, tb = sys.exc_info()
1270 etype, value, tb = sys.exc_info()
1271 else:
1271 else:
1272 etype, value, tb = exc_tuple
1272 etype, value, tb = exc_tuple
1273
1273
1274 if etype is None:
1274 if etype is None:
1275 if hasattr(sys, 'last_type'):
1275 if hasattr(sys, 'last_type'):
1276 etype, value, tb = sys.last_type, sys.last_value, \
1276 etype, value, tb = sys.last_type, sys.last_value, \
1277 sys.last_traceback
1277 sys.last_traceback
1278 else:
1278 else:
1279 self.write_err('No traceback available to show.\n')
1279 self.write_err('No traceback available to show.\n')
1280 return
1280 return
1281
1281
1282 if etype is SyntaxError:
1282 if etype is SyntaxError:
1283 # Though this won't be called by syntax errors in the input
1283 # Though this won't be called by syntax errors in the input
1284 # line, there may be SyntaxError cases whith imported code.
1284 # line, there may be SyntaxError cases whith imported code.
1285 self.showsyntaxerror(filename)
1285 self.showsyntaxerror(filename)
1286 elif etype is UsageError:
1286 elif etype is UsageError:
1287 print "UsageError:", value
1287 print "UsageError:", value
1288 else:
1288 else:
1289 # WARNING: these variables are somewhat deprecated and not
1289 # WARNING: these variables are somewhat deprecated and not
1290 # necessarily safe to use in a threaded environment, but tools
1290 # necessarily safe to use in a threaded environment, but tools
1291 # like pdb depend on their existence, so let's set them. If we
1291 # like pdb depend on their existence, so let's set them. If we
1292 # find problems in the field, we'll need to revisit their use.
1292 # find problems in the field, we'll need to revisit their use.
1293 sys.last_type = etype
1293 sys.last_type = etype
1294 sys.last_value = value
1294 sys.last_value = value
1295 sys.last_traceback = tb
1295 sys.last_traceback = tb
1296
1296
1297 if etype in self.custom_exceptions:
1297 if etype in self.custom_exceptions:
1298 # FIXME: Old custom traceback objects may just return a
1298 # FIXME: Old custom traceback objects may just return a
1299 # string, in that case we just put it into a list
1299 # string, in that case we just put it into a list
1300 stb = self.CustomTB(etype, value, tb, tb_offset)
1300 stb = self.CustomTB(etype, value, tb, tb_offset)
1301 if isinstance(ctb, basestring):
1301 if isinstance(ctb, basestring):
1302 stb = [stb]
1302 stb = [stb]
1303 else:
1303 else:
1304 if exception_only:
1304 if exception_only:
1305 stb = ['An exception has occurred, use %tb to see '
1305 stb = ['An exception has occurred, use %tb to see '
1306 'the full traceback.\n']
1306 'the full traceback.\n']
1307 stb.extend(self.InteractiveTB.get_exception_only(etype,
1307 stb.extend(self.InteractiveTB.get_exception_only(etype,
1308 value))
1308 value))
1309 else:
1309 else:
1310 stb = self.InteractiveTB.structured_traceback(etype,
1310 stb = self.InteractiveTB.structured_traceback(etype,
1311 value, tb, tb_offset=tb_offset)
1311 value, tb, tb_offset=tb_offset)
1312 # FIXME: the pdb calling should be done by us, not by
1312 # FIXME: the pdb calling should be done by us, not by
1313 # the code computing the traceback.
1313 # the code computing the traceback.
1314 if self.InteractiveTB.call_pdb:
1314 if self.InteractiveTB.call_pdb:
1315 # pdb mucks up readline, fix it back
1315 # pdb mucks up readline, fix it back
1316 self.set_completer()
1316 self.set_completer()
1317
1317
1318 # Actually show the traceback
1318 # Actually show the traceback
1319 self._showtraceback(etype, value, stb)
1319 self._showtraceback(etype, value, stb)
1320
1320
1321 except KeyboardInterrupt:
1321 except KeyboardInterrupt:
1322 self.write_err("\nKeyboardInterrupt\n")
1322 self.write_err("\nKeyboardInterrupt\n")
1323
1323
1324 def _showtraceback(self, etype, evalue, stb):
1324 def _showtraceback(self, etype, evalue, stb):
1325 """Actually show a traceback.
1325 """Actually show a traceback.
1326
1326
1327 Subclasses may override this method to put the traceback on a different
1327 Subclasses may override this method to put the traceback on a different
1328 place, like a side channel.
1328 place, like a side channel.
1329 """
1329 """
1330 # FIXME: this should use the proper write channels, but our test suite
1330 # FIXME: this should use the proper write channels, but our test suite
1331 # relies on it coming out of stdout...
1331 # relies on it coming out of stdout...
1332 print >> sys.stdout, self.InteractiveTB.stb2text(stb)
1332 print >> sys.stdout, self.InteractiveTB.stb2text(stb)
1333
1333
1334 def showsyntaxerror(self, filename=None):
1334 def showsyntaxerror(self, filename=None):
1335 """Display the syntax error that just occurred.
1335 """Display the syntax error that just occurred.
1336
1336
1337 This doesn't display a stack trace because there isn't one.
1337 This doesn't display a stack trace because there isn't one.
1338
1338
1339 If a filename is given, it is stuffed in the exception instead
1339 If a filename is given, it is stuffed in the exception instead
1340 of what was there before (because Python's parser always uses
1340 of what was there before (because Python's parser always uses
1341 "<string>" when reading from a string).
1341 "<string>" when reading from a string).
1342 """
1342 """
1343 etype, value, last_traceback = sys.exc_info()
1343 etype, value, last_traceback = sys.exc_info()
1344
1344
1345 # See note about these variables in showtraceback() above
1345 # See note about these variables in showtraceback() above
1346 sys.last_type = etype
1346 sys.last_type = etype
1347 sys.last_value = value
1347 sys.last_value = value
1348 sys.last_traceback = last_traceback
1348 sys.last_traceback = last_traceback
1349
1349
1350 if filename and etype is SyntaxError:
1350 if filename and etype is SyntaxError:
1351 # Work hard to stuff the correct filename in the exception
1351 # Work hard to stuff the correct filename in the exception
1352 try:
1352 try:
1353 msg, (dummy_filename, lineno, offset, line) = value
1353 msg, (dummy_filename, lineno, offset, line) = value
1354 except:
1354 except:
1355 # Not the format we expect; leave it alone
1355 # Not the format we expect; leave it alone
1356 pass
1356 pass
1357 else:
1357 else:
1358 # Stuff in the right filename
1358 # Stuff in the right filename
1359 try:
1359 try:
1360 # Assume SyntaxError is a class exception
1360 # Assume SyntaxError is a class exception
1361 value = SyntaxError(msg, (filename, lineno, offset, line))
1361 value = SyntaxError(msg, (filename, lineno, offset, line))
1362 except:
1362 except:
1363 # If that failed, assume SyntaxError is a string
1363 # If that failed, assume SyntaxError is a string
1364 value = msg, (filename, lineno, offset, line)
1364 value = msg, (filename, lineno, offset, line)
1365 stb = self.SyntaxTB.structured_traceback(etype, value, [])
1365 stb = self.SyntaxTB.structured_traceback(etype, value, [])
1366 self._showtraceback(etype, value, stb)
1366 self._showtraceback(etype, value, stb)
1367
1367
1368 #-------------------------------------------------------------------------
1368 #-------------------------------------------------------------------------
1369 # Things related to tab completion
1369 # Things related to tab completion
1370 #-------------------------------------------------------------------------
1370 #-------------------------------------------------------------------------
1371
1371
1372 def complete(self, text, line=None, cursor_pos=None):
1372 def complete(self, text, line=None, cursor_pos=None):
1373 """Return a sorted list of all possible completions on text.
1373 """Return a sorted list of all possible completions on text.
1374
1374
1375 Parameters
1375 Parameters
1376 ----------
1376 ----------
1377
1377
1378 text : string
1378 text : string
1379 A string of text to be completed on.
1379 A string of text to be completed on.
1380
1380
1381 line : string, optional
1381 line : string, optional
1382 The complete line that text is part of.
1382 The complete line that text is part of.
1383
1383
1384 cursor_pos : int, optional
1384 cursor_pos : int, optional
1385 The position of the cursor on the input line.
1385 The position of the cursor on the input line.
1386
1386
1387 The optional arguments allow the completion to take more context into
1387 The optional arguments allow the completion to take more context into
1388 account, and are part of the low-level completion API.
1388 account, and are part of the low-level completion API.
1389
1389
1390 This is a wrapper around the completion mechanism, similar to what
1390 This is a wrapper around the completion mechanism, similar to what
1391 readline does at the command line when the TAB key is hit. By
1391 readline does at the command line when the TAB key is hit. By
1392 exposing it as a method, it can be used by other non-readline
1392 exposing it as a method, it can be used by other non-readline
1393 environments (such as GUIs) for text completion.
1393 environments (such as GUIs) for text completion.
1394
1394
1395 Simple usage example:
1395 Simple usage example:
1396
1396
1397 In [7]: x = 'hello'
1397 In [7]: x = 'hello'
1398
1398
1399 In [8]: x
1399 In [8]: x
1400 Out[8]: 'hello'
1400 Out[8]: 'hello'
1401
1401
1402 In [9]: print x
1402 In [9]: print x
1403 hello
1403 hello
1404
1404
1405 In [10]: _ip.complete('x.l')
1405 In [10]: _ip.complete('x.l')
1406 Out[10]: ['x.ljust', 'x.lower', 'x.lstrip']
1406 Out[10]: ['x.ljust', 'x.lower', 'x.lstrip']
1407 """
1407 """
1408
1408
1409 # Inject names into __builtin__ so we can complete on the added names.
1409 # Inject names into __builtin__ so we can complete on the added names.
1410 with self.builtin_trap:
1410 with self.builtin_trap:
1411 return self.Completer.complete(text,line_buffer=text)
1411 return self.Completer.complete(text,line_buffer=text)
1412
1412
1413 def set_custom_completer(self,completer,pos=0):
1413 def set_custom_completer(self,completer,pos=0):
1414 """Adds a new custom completer function.
1414 """Adds a new custom completer function.
1415
1415
1416 The position argument (defaults to 0) is the index in the completers
1416 The position argument (defaults to 0) is the index in the completers
1417 list where you want the completer to be inserted."""
1417 list where you want the completer to be inserted."""
1418
1418
1419 newcomp = new.instancemethod(completer,self.Completer,
1419 newcomp = new.instancemethod(completer,self.Completer,
1420 self.Completer.__class__)
1420 self.Completer.__class__)
1421 self.Completer.matchers.insert(pos,newcomp)
1421 self.Completer.matchers.insert(pos,newcomp)
1422
1422
1423 def set_completer(self):
1423 def set_completer(self):
1424 """Reset readline's completer to be our own."""
1424 """Reset readline's completer to be our own."""
1425 self.readline.set_completer(self.Completer.rlcomplete)
1425 self.readline.set_completer(self.Completer.rlcomplete)
1426
1426
1427 def set_completer_frame(self, frame=None):
1427 def set_completer_frame(self, frame=None):
1428 """Set the frame of the completer."""
1428 """Set the frame of the completer."""
1429 if frame:
1429 if frame:
1430 self.Completer.namespace = frame.f_locals
1430 self.Completer.namespace = frame.f_locals
1431 self.Completer.global_namespace = frame.f_globals
1431 self.Completer.global_namespace = frame.f_globals
1432 else:
1432 else:
1433 self.Completer.namespace = self.user_ns
1433 self.Completer.namespace = self.user_ns
1434 self.Completer.global_namespace = self.user_global_ns
1434 self.Completer.global_namespace = self.user_global_ns
1435
1435
1436 #-------------------------------------------------------------------------
1436 #-------------------------------------------------------------------------
1437 # Things related to readline
1437 # Things related to readline
1438 #-------------------------------------------------------------------------
1438 #-------------------------------------------------------------------------
1439
1439
1440 def init_readline(self):
1440 def init_readline(self):
1441 """Command history completion/saving/reloading."""
1441 """Command history completion/saving/reloading."""
1442
1442
1443 if self.readline_use:
1443 if self.readline_use:
1444 import IPython.utils.rlineimpl as readline
1444 import IPython.utils.rlineimpl as readline
1445
1445
1446 self.rl_next_input = None
1446 self.rl_next_input = None
1447 self.rl_do_indent = False
1447 self.rl_do_indent = False
1448
1448
1449 if not self.readline_use or not readline.have_readline:
1449 if not self.readline_use or not readline.have_readline:
1450 self.has_readline = False
1450 self.has_readline = False
1451 self.readline = None
1451 self.readline = None
1452 # Set a number of methods that depend on readline to be no-op
1452 # Set a number of methods that depend on readline to be no-op
1453 self.savehist = no_op
1453 self.savehist = no_op
1454 self.reloadhist = no_op
1454 self.reloadhist = no_op
1455 self.set_completer = no_op
1455 self.set_completer = no_op
1456 self.set_custom_completer = no_op
1456 self.set_custom_completer = no_op
1457 self.set_completer_frame = no_op
1457 self.set_completer_frame = no_op
1458 warn('Readline services not available or not loaded.')
1458 warn('Readline services not available or not loaded.')
1459 else:
1459 else:
1460 self.has_readline = True
1460 self.has_readline = True
1461 self.readline = readline
1461 self.readline = readline
1462 sys.modules['readline'] = readline
1462 sys.modules['readline'] = readline
1463 import atexit
1463 import atexit
1464 from IPython.core.completer import IPCompleter
1464 from IPython.core.completer import IPCompleter
1465 self.Completer = IPCompleter(self,
1465 self.Completer = IPCompleter(self,
1466 self.user_ns,
1466 self.user_ns,
1467 self.user_global_ns,
1467 self.user_global_ns,
1468 self.readline_omit__names,
1468 self.readline_omit__names,
1469 self.alias_manager.alias_table)
1469 self.alias_manager.alias_table)
1470 sdisp = self.strdispatchers.get('complete_command', StrDispatch())
1470 sdisp = self.strdispatchers.get('complete_command', StrDispatch())
1471 self.strdispatchers['complete_command'] = sdisp
1471 self.strdispatchers['complete_command'] = sdisp
1472 self.Completer.custom_completers = sdisp
1472 self.Completer.custom_completers = sdisp
1473 # Platform-specific configuration
1473 # Platform-specific configuration
1474 if os.name == 'nt':
1474 if os.name == 'nt':
1475 self.readline_startup_hook = readline.set_pre_input_hook
1475 self.readline_startup_hook = readline.set_pre_input_hook
1476 else:
1476 else:
1477 self.readline_startup_hook = readline.set_startup_hook
1477 self.readline_startup_hook = readline.set_startup_hook
1478
1478
1479 # Load user's initrc file (readline config)
1479 # Load user's initrc file (readline config)
1480 # Or if libedit is used, load editrc.
1480 # Or if libedit is used, load editrc.
1481 inputrc_name = os.environ.get('INPUTRC')
1481 inputrc_name = os.environ.get('INPUTRC')
1482 if inputrc_name is None:
1482 if inputrc_name is None:
1483 home_dir = get_home_dir()
1483 home_dir = get_home_dir()
1484 if home_dir is not None:
1484 if home_dir is not None:
1485 inputrc_name = '.inputrc'
1485 inputrc_name = '.inputrc'
1486 if readline.uses_libedit:
1486 if readline.uses_libedit:
1487 inputrc_name = '.editrc'
1487 inputrc_name = '.editrc'
1488 inputrc_name = os.path.join(home_dir, inputrc_name)
1488 inputrc_name = os.path.join(home_dir, inputrc_name)
1489 if os.path.isfile(inputrc_name):
1489 if os.path.isfile(inputrc_name):
1490 try:
1490 try:
1491 readline.read_init_file(inputrc_name)
1491 readline.read_init_file(inputrc_name)
1492 except:
1492 except:
1493 warn('Problems reading readline initialization file <%s>'
1493 warn('Problems reading readline initialization file <%s>'
1494 % inputrc_name)
1494 % inputrc_name)
1495
1495
1496 # save this in sys so embedded copies can restore it properly
1496 # save this in sys so embedded copies can restore it properly
1497 sys.ipcompleter = self.Completer.rlcomplete
1497 sys.ipcompleter = self.Completer.rlcomplete
1498 self.set_completer()
1498 self.set_completer()
1499
1499
1500 # Configure readline according to user's prefs
1500 # Configure readline according to user's prefs
1501 # This is only done if GNU readline is being used. If libedit
1501 # This is only done if GNU readline is being used. If libedit
1502 # is being used (as on Leopard) the readline config is
1502 # is being used (as on Leopard) the readline config is
1503 # not run as the syntax for libedit is different.
1503 # not run as the syntax for libedit is different.
1504 if not readline.uses_libedit:
1504 if not readline.uses_libedit:
1505 for rlcommand in self.readline_parse_and_bind:
1505 for rlcommand in self.readline_parse_and_bind:
1506 #print "loading rl:",rlcommand # dbg
1506 #print "loading rl:",rlcommand # dbg
1507 readline.parse_and_bind(rlcommand)
1507 readline.parse_and_bind(rlcommand)
1508
1508
1509 # Remove some chars from the delimiters list. If we encounter
1509 # Remove some chars from the delimiters list. If we encounter
1510 # unicode chars, discard them.
1510 # unicode chars, discard them.
1511 delims = readline.get_completer_delims().encode("ascii", "ignore")
1511 delims = readline.get_completer_delims().encode("ascii", "ignore")
1512 delims = delims.translate(string._idmap,
1512 delims = delims.translate(string._idmap,
1513 self.readline_remove_delims)
1513 self.readline_remove_delims)
1514 readline.set_completer_delims(delims)
1514 readline.set_completer_delims(delims)
1515 # otherwise we end up with a monster history after a while:
1515 # otherwise we end up with a monster history after a while:
1516 readline.set_history_length(1000)
1516 readline.set_history_length(1000)
1517 try:
1517 try:
1518 #print '*** Reading readline history' # dbg
1518 #print '*** Reading readline history' # dbg
1519 readline.read_history_file(self.histfile)
1519 readline.read_history_file(self.histfile)
1520 except IOError:
1520 except IOError:
1521 pass # It doesn't exist yet.
1521 pass # It doesn't exist yet.
1522
1522
1523 atexit.register(self.atexit_operations)
1523 atexit.register(self.atexit_operations)
1524 del atexit
1524 del atexit
1525
1525
1526 # Configure auto-indent for all platforms
1526 # Configure auto-indent for all platforms
1527 self.set_autoindent(self.autoindent)
1527 self.set_autoindent(self.autoindent)
1528
1528
1529 def set_next_input(self, s):
1529 def set_next_input(self, s):
1530 """ Sets the 'default' input string for the next command line.
1530 """ Sets the 'default' input string for the next command line.
1531
1531
1532 Requires readline.
1532 Requires readline.
1533
1533
1534 Example:
1534 Example:
1535
1535
1536 [D:\ipython]|1> _ip.set_next_input("Hello Word")
1536 [D:\ipython]|1> _ip.set_next_input("Hello Word")
1537 [D:\ipython]|2> Hello Word_ # cursor is here
1537 [D:\ipython]|2> Hello Word_ # cursor is here
1538 """
1538 """
1539
1539
1540 self.rl_next_input = s
1540 self.rl_next_input = s
1541
1541
1542 # Maybe move this to the terminal subclass?
1542 # Maybe move this to the terminal subclass?
1543 def pre_readline(self):
1543 def pre_readline(self):
1544 """readline hook to be used at the start of each line.
1544 """readline hook to be used at the start of each line.
1545
1545
1546 Currently it handles auto-indent only."""
1546 Currently it handles auto-indent only."""
1547
1547
1548 if self.rl_do_indent:
1548 if self.rl_do_indent:
1549 self.readline.insert_text(self._indent_current_str())
1549 self.readline.insert_text(self._indent_current_str())
1550 if self.rl_next_input is not None:
1550 if self.rl_next_input is not None:
1551 self.readline.insert_text(self.rl_next_input)
1551 self.readline.insert_text(self.rl_next_input)
1552 self.rl_next_input = None
1552 self.rl_next_input = None
1553
1553
1554 def _indent_current_str(self):
1554 def _indent_current_str(self):
1555 """return the current level of indentation as a string"""
1555 """return the current level of indentation as a string"""
1556 return self.indent_current_nsp * ' '
1556 return self.indent_current_nsp * ' '
1557
1557
1558 #-------------------------------------------------------------------------
1558 #-------------------------------------------------------------------------
1559 # Things related to magics
1559 # Things related to magics
1560 #-------------------------------------------------------------------------
1560 #-------------------------------------------------------------------------
1561
1561
1562 def init_magics(self):
1562 def init_magics(self):
1563 # FIXME: Move the color initialization to the DisplayHook, which
1563 # FIXME: Move the color initialization to the DisplayHook, which
1564 # should be split into a prompt manager and displayhook. We probably
1564 # should be split into a prompt manager and displayhook. We probably
1565 # even need a centralize colors management object.
1565 # even need a centralize colors management object.
1566 self.magic_colors(self.colors)
1566 self.magic_colors(self.colors)
1567 # History was moved to a separate module
1567 # History was moved to a separate module
1568 from . import history
1568 from . import history
1569 history.init_ipython(self)
1569 history.init_ipython(self)
1570
1570
1571 def magic(self,arg_s):
1571 def magic(self,arg_s):
1572 """Call a magic function by name.
1572 """Call a magic function by name.
1573
1573
1574 Input: a string containing the name of the magic function to call and any
1574 Input: a string containing the name of the magic function to call and any
1575 additional arguments to be passed to the magic.
1575 additional arguments to be passed to the magic.
1576
1576
1577 magic('name -opt foo bar') is equivalent to typing at the ipython
1577 magic('name -opt foo bar') is equivalent to typing at the ipython
1578 prompt:
1578 prompt:
1579
1579
1580 In[1]: %name -opt foo bar
1580 In[1]: %name -opt foo bar
1581
1581
1582 To call a magic without arguments, simply use magic('name').
1582 To call a magic without arguments, simply use magic('name').
1583
1583
1584 This provides a proper Python function to call IPython's magics in any
1584 This provides a proper Python function to call IPython's magics in any
1585 valid Python code you can type at the interpreter, including loops and
1585 valid Python code you can type at the interpreter, including loops and
1586 compound statements.
1586 compound statements.
1587 """
1587 """
1588 args = arg_s.split(' ',1)
1588 args = arg_s.split(' ',1)
1589 magic_name = args[0]
1589 magic_name = args[0]
1590 magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
1590 magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
1591
1591
1592 try:
1592 try:
1593 magic_args = args[1]
1593 magic_args = args[1]
1594 except IndexError:
1594 except IndexError:
1595 magic_args = ''
1595 magic_args = ''
1596 fn = getattr(self,'magic_'+magic_name,None)
1596 fn = getattr(self,'magic_'+magic_name,None)
1597 if fn is None:
1597 if fn is None:
1598 error("Magic function `%s` not found." % magic_name)
1598 error("Magic function `%s` not found." % magic_name)
1599 else:
1599 else:
1600 magic_args = self.var_expand(magic_args,1)
1600 magic_args = self.var_expand(magic_args,1)
1601 with nested(self.builtin_trap,):
1601 with nested(self.builtin_trap,):
1602 result = fn(magic_args)
1602 result = fn(magic_args)
1603 return result
1603 return result
1604
1604
1605 def define_magic(self, magicname, func):
1605 def define_magic(self, magicname, func):
1606 """Expose own function as magic function for ipython
1606 """Expose own function as magic function for ipython
1607
1607
1608 def foo_impl(self,parameter_s=''):
1608 def foo_impl(self,parameter_s=''):
1609 'My very own magic!. (Use docstrings, IPython reads them).'
1609 'My very own magic!. (Use docstrings, IPython reads them).'
1610 print 'Magic function. Passed parameter is between < >:'
1610 print 'Magic function. Passed parameter is between < >:'
1611 print '<%s>' % parameter_s
1611 print '<%s>' % parameter_s
1612 print 'The self object is:',self
1612 print 'The self object is:',self
1613
1613
1614 self.define_magic('foo',foo_impl)
1614 self.define_magic('foo',foo_impl)
1615 """
1615 """
1616
1616
1617 import new
1617 import new
1618 im = new.instancemethod(func,self, self.__class__)
1618 im = new.instancemethod(func,self, self.__class__)
1619 old = getattr(self, "magic_" + magicname, None)
1619 old = getattr(self, "magic_" + magicname, None)
1620 setattr(self, "magic_" + magicname, im)
1620 setattr(self, "magic_" + magicname, im)
1621 return old
1621 return old
1622
1622
1623 #-------------------------------------------------------------------------
1623 #-------------------------------------------------------------------------
1624 # Things related to macros
1624 # Things related to macros
1625 #-------------------------------------------------------------------------
1625 #-------------------------------------------------------------------------
1626
1626
1627 def define_macro(self, name, themacro):
1627 def define_macro(self, name, themacro):
1628 """Define a new macro
1628 """Define a new macro
1629
1629
1630 Parameters
1630 Parameters
1631 ----------
1631 ----------
1632 name : str
1632 name : str
1633 The name of the macro.
1633 The name of the macro.
1634 themacro : str or Macro
1634 themacro : str or Macro
1635 The action to do upon invoking the macro. If a string, a new
1635 The action to do upon invoking the macro. If a string, a new
1636 Macro object is created by passing the string to it.
1636 Macro object is created by passing the string to it.
1637 """
1637 """
1638
1638
1639 from IPython.core import macro
1639 from IPython.core import macro
1640
1640
1641 if isinstance(themacro, basestring):
1641 if isinstance(themacro, basestring):
1642 themacro = macro.Macro(themacro)
1642 themacro = macro.Macro(themacro)
1643 if not isinstance(themacro, macro.Macro):
1643 if not isinstance(themacro, macro.Macro):
1644 raise ValueError('A macro must be a string or a Macro instance.')
1644 raise ValueError('A macro must be a string or a Macro instance.')
1645 self.user_ns[name] = themacro
1645 self.user_ns[name] = themacro
1646
1646
1647 #-------------------------------------------------------------------------
1647 #-------------------------------------------------------------------------
1648 # Things related to the running of system commands
1648 # Things related to the running of system commands
1649 #-------------------------------------------------------------------------
1649 #-------------------------------------------------------------------------
1650
1650
1651 def system(self, cmd):
1651 def system(self, cmd):
1652 """Make a system call, using IPython."""
1652 """Make a system call, using IPython."""
1653 return self.hooks.shell_hook(self.var_expand(cmd, depth=2))
1653 return self.hooks.shell_hook(self.var_expand(cmd, depth=2))
1654
1654
1655 #-------------------------------------------------------------------------
1655 #-------------------------------------------------------------------------
1656 # Things related to aliases
1656 # Things related to aliases
1657 #-------------------------------------------------------------------------
1657 #-------------------------------------------------------------------------
1658
1658
1659 def init_alias(self):
1659 def init_alias(self):
1660 self.alias_manager = AliasManager(shell=self, config=self.config)
1660 self.alias_manager = AliasManager(shell=self, config=self.config)
1661 self.ns_table['alias'] = self.alias_manager.alias_table,
1661 self.ns_table['alias'] = self.alias_manager.alias_table,
1662
1662
1663 #-------------------------------------------------------------------------
1663 #-------------------------------------------------------------------------
1664 # Things related to extensions and plugins
1664 # Things related to extensions and plugins
1665 #-------------------------------------------------------------------------
1665 #-------------------------------------------------------------------------
1666
1666
1667 def init_extension_manager(self):
1667 def init_extension_manager(self):
1668 self.extension_manager = ExtensionManager(shell=self, config=self.config)
1668 self.extension_manager = ExtensionManager(shell=self, config=self.config)
1669
1669
1670 def init_plugin_manager(self):
1670 def init_plugin_manager(self):
1671 self.plugin_manager = PluginManager(config=self.config)
1671 self.plugin_manager = PluginManager(config=self.config)
1672
1672
1673 #-------------------------------------------------------------------------
1673 #-------------------------------------------------------------------------
1674 # Things related to payloads
1674 # Things related to payloads
1675 #-------------------------------------------------------------------------
1675 #-------------------------------------------------------------------------
1676
1676
1677 def init_payload(self):
1677 def init_payload(self):
1678 self.payload_manager = PayloadManager(config=self.config)
1678 self.payload_manager = PayloadManager(config=self.config)
1679
1679
1680 #-------------------------------------------------------------------------
1680 #-------------------------------------------------------------------------
1681 # Things related to the prefilter
1681 # Things related to the prefilter
1682 #-------------------------------------------------------------------------
1682 #-------------------------------------------------------------------------
1683
1683
1684 def init_prefilter(self):
1684 def init_prefilter(self):
1685 self.prefilter_manager = PrefilterManager(shell=self, config=self.config)
1685 self.prefilter_manager = PrefilterManager(shell=self, config=self.config)
1686 # Ultimately this will be refactored in the new interpreter code, but
1686 # Ultimately this will be refactored in the new interpreter code, but
1687 # for now, we should expose the main prefilter method (there's legacy
1687 # for now, we should expose the main prefilter method (there's legacy
1688 # code out there that may rely on this).
1688 # code out there that may rely on this).
1689 self.prefilter = self.prefilter_manager.prefilter_lines
1689 self.prefilter = self.prefilter_manager.prefilter_lines
1690
1690
1691 #-------------------------------------------------------------------------
1691 #-------------------------------------------------------------------------
1692 # Things related to the running of code
1692 # Things related to the running of code
1693 #-------------------------------------------------------------------------
1693 #-------------------------------------------------------------------------
1694
1694
1695 def ex(self, cmd):
1695 def ex(self, cmd):
1696 """Execute a normal python statement in user namespace."""
1696 """Execute a normal python statement in user namespace."""
1697 with nested(self.builtin_trap,):
1697 with nested(self.builtin_trap,):
1698 exec cmd in self.user_global_ns, self.user_ns
1698 exec cmd in self.user_global_ns, self.user_ns
1699
1699
1700 def ev(self, expr):
1700 def ev(self, expr):
1701 """Evaluate python expression expr in user namespace.
1701 """Evaluate python expression expr in user namespace.
1702
1702
1703 Returns the result of evaluation
1703 Returns the result of evaluation
1704 """
1704 """
1705 with nested(self.builtin_trap,):
1705 with nested(self.builtin_trap,):
1706 return eval(expr, self.user_global_ns, self.user_ns)
1706 return eval(expr, self.user_global_ns, self.user_ns)
1707
1707
1708 def safe_execfile(self, fname, *where, **kw):
1708 def safe_execfile(self, fname, *where, **kw):
1709 """A safe version of the builtin execfile().
1709 """A safe version of the builtin execfile().
1710
1710
1711 This version will never throw an exception, but instead print
1711 This version will never throw an exception, but instead print
1712 helpful error messages to the screen. This only works on pure
1712 helpful error messages to the screen. This only works on pure
1713 Python files with the .py extension.
1713 Python files with the .py extension.
1714
1714
1715 Parameters
1715 Parameters
1716 ----------
1716 ----------
1717 fname : string
1717 fname : string
1718 The name of the file to be executed.
1718 The name of the file to be executed.
1719 where : tuple
1719 where : tuple
1720 One or two namespaces, passed to execfile() as (globals,locals).
1720 One or two namespaces, passed to execfile() as (globals,locals).
1721 If only one is given, it is passed as both.
1721 If only one is given, it is passed as both.
1722 exit_ignore : bool (False)
1722 exit_ignore : bool (False)
1723 If True, then silence SystemExit for non-zero status (it is always
1723 If True, then silence SystemExit for non-zero status (it is always
1724 silenced for zero status, as it is so common).
1724 silenced for zero status, as it is so common).
1725 """
1725 """
1726 kw.setdefault('exit_ignore', False)
1726 kw.setdefault('exit_ignore', False)
1727
1727
1728 fname = os.path.abspath(os.path.expanduser(fname))
1728 fname = os.path.abspath(os.path.expanduser(fname))
1729
1729
1730 # Make sure we have a .py file
1730 # Make sure we have a .py file
1731 if not fname.endswith('.py'):
1731 if not fname.endswith('.py'):
1732 warn('File must end with .py to be run using execfile: <%s>' % fname)
1732 warn('File must end with .py to be run using execfile: <%s>' % fname)
1733
1733
1734 # Make sure we can open the file
1734 # Make sure we can open the file
1735 try:
1735 try:
1736 with open(fname) as thefile:
1736 with open(fname) as thefile:
1737 pass
1737 pass
1738 except:
1738 except:
1739 warn('Could not open file <%s> for safe execution.' % fname)
1739 warn('Could not open file <%s> for safe execution.' % fname)
1740 return
1740 return
1741
1741
1742 # Find things also in current directory. This is needed to mimic the
1742 # Find things also in current directory. This is needed to mimic the
1743 # behavior of running a script from the system command line, where
1743 # behavior of running a script from the system command line, where
1744 # Python inserts the script's directory into sys.path
1744 # Python inserts the script's directory into sys.path
1745 dname = os.path.dirname(fname)
1745 dname = os.path.dirname(fname)
1746
1746
1747 with prepended_to_syspath(dname):
1747 with prepended_to_syspath(dname):
1748 try:
1748 try:
1749 execfile(fname,*where)
1749 execfile(fname,*where)
1750 except SystemExit, status:
1750 except SystemExit, status:
1751 # If the call was made with 0 or None exit status (sys.exit(0)
1751 # If the call was made with 0 or None exit status (sys.exit(0)
1752 # or sys.exit() ), don't bother showing a traceback, as both of
1752 # or sys.exit() ), don't bother showing a traceback, as both of
1753 # these are considered normal by the OS:
1753 # these are considered normal by the OS:
1754 # > python -c'import sys;sys.exit(0)'; echo $?
1754 # > python -c'import sys;sys.exit(0)'; echo $?
1755 # 0
1755 # 0
1756 # > python -c'import sys;sys.exit()'; echo $?
1756 # > python -c'import sys;sys.exit()'; echo $?
1757 # 0
1757 # 0
1758 # For other exit status, we show the exception unless
1758 # For other exit status, we show the exception unless
1759 # explicitly silenced, but only in short form.
1759 # explicitly silenced, but only in short form.
1760 if status.code not in (0, None) and not kw['exit_ignore']:
1760 if status.code not in (0, None) and not kw['exit_ignore']:
1761 self.showtraceback(exception_only=True)
1761 self.showtraceback(exception_only=True)
1762 except:
1762 except:
1763 self.showtraceback()
1763 self.showtraceback()
1764
1764
1765 def safe_execfile_ipy(self, fname):
1765 def safe_execfile_ipy(self, fname):
1766 """Like safe_execfile, but for .ipy files with IPython syntax.
1766 """Like safe_execfile, but for .ipy files with IPython syntax.
1767
1767
1768 Parameters
1768 Parameters
1769 ----------
1769 ----------
1770 fname : str
1770 fname : str
1771 The name of the file to execute. The filename must have a
1771 The name of the file to execute. The filename must have a
1772 .ipy extension.
1772 .ipy extension.
1773 """
1773 """
1774 fname = os.path.abspath(os.path.expanduser(fname))
1774 fname = os.path.abspath(os.path.expanduser(fname))
1775
1775
1776 # Make sure we have a .py file
1776 # Make sure we have a .py file
1777 if not fname.endswith('.ipy'):
1777 if not fname.endswith('.ipy'):
1778 warn('File must end with .py to be run using execfile: <%s>' % fname)
1778 warn('File must end with .py to be run using execfile: <%s>' % fname)
1779
1779
1780 # Make sure we can open the file
1780 # Make sure we can open the file
1781 try:
1781 try:
1782 with open(fname) as thefile:
1782 with open(fname) as thefile:
1783 pass
1783 pass
1784 except:
1784 except:
1785 warn('Could not open file <%s> for safe execution.' % fname)
1785 warn('Could not open file <%s> for safe execution.' % fname)
1786 return
1786 return
1787
1787
1788 # Find things also in current directory. This is needed to mimic the
1788 # Find things also in current directory. This is needed to mimic the
1789 # behavior of running a script from the system command line, where
1789 # behavior of running a script from the system command line, where
1790 # Python inserts the script's directory into sys.path
1790 # Python inserts the script's directory into sys.path
1791 dname = os.path.dirname(fname)
1791 dname = os.path.dirname(fname)
1792
1792
1793 with prepended_to_syspath(dname):
1793 with prepended_to_syspath(dname):
1794 try:
1794 try:
1795 with open(fname) as thefile:
1795 with open(fname) as thefile:
1796 script = thefile.read()
1796 script = thefile.read()
1797 # self.runlines currently captures all exceptions
1797 # self.runlines currently captures all exceptions
1798 # raise in user code. It would be nice if there were
1798 # raise in user code. It would be nice if there were
1799 # versions of runlines, execfile that did raise, so
1799 # versions of runlines, execfile that did raise, so
1800 # we could catch the errors.
1800 # we could catch the errors.
1801 self.runlines(script, clean=True)
1801 self.runlines(script, clean=True)
1802 except:
1802 except:
1803 self.showtraceback()
1803 self.showtraceback()
1804 warn('Unknown failure executing file: <%s>' % fname)
1804 warn('Unknown failure executing file: <%s>' % fname)
1805
1805
1806 def runlines(self, lines, clean=False):
1806 def runlines(self, lines, clean=False):
1807 """Run a string of one or more lines of source.
1807 """Run a string of one or more lines of source.
1808
1808
1809 This method is capable of running a string containing multiple source
1809 This method is capable of running a string containing multiple source
1810 lines, as if they had been entered at the IPython prompt. Since it
1810 lines, as if they had been entered at the IPython prompt. Since it
1811 exposes IPython's processing machinery, the given strings can contain
1811 exposes IPython's processing machinery, the given strings can contain
1812 magic calls (%magic), special shell access (!cmd), etc.
1812 magic calls (%magic), special shell access (!cmd), etc.
1813 """
1813 """
1814
1814
1815 if isinstance(lines, (list, tuple)):
1815 if isinstance(lines, (list, tuple)):
1816 lines = '\n'.join(lines)
1816 lines = '\n'.join(lines)
1817
1817
1818 if clean:
1818 if clean:
1819 lines = self._cleanup_ipy_script(lines)
1819 lines = self._cleanup_ipy_script(lines)
1820
1820
1821 # We must start with a clean buffer, in case this is run from an
1821 # We must start with a clean buffer, in case this is run from an
1822 # interactive IPython session (via a magic, for example).
1822 # interactive IPython session (via a magic, for example).
1823 self.resetbuffer()
1823 self.resetbuffer()
1824 lines = lines.splitlines()
1824 lines = lines.splitlines()
1825 more = 0
1825 more = 0
1826
1826
1827 with nested(self.builtin_trap, self.display_trap):
1827 with nested(self.builtin_trap, self.display_trap):
1828 for line in lines:
1828 for line in lines:
1829 # skip blank lines so we don't mess up the prompt counter, but do
1829 # skip blank lines so we don't mess up the prompt counter, but do
1830 # NOT skip even a blank line if we are in a code block (more is
1830 # NOT skip even a blank line if we are in a code block (more is
1831 # true)
1831 # true)
1832
1832
1833 if line or more:
1833 if line or more:
1834 # push to raw history, so hist line numbers stay in sync
1834 # push to raw history, so hist line numbers stay in sync
1835 self.input_hist_raw.append("# " + line + "\n")
1835 self.input_hist_raw.append("# " + line + "\n")
1836 prefiltered = self.prefilter_manager.prefilter_lines(line,more)
1836 prefiltered = self.prefilter_manager.prefilter_lines(line,more)
1837 more = self.push_line(prefiltered)
1837 more = self.push_line(prefiltered)
1838 # IPython's runsource returns None if there was an error
1838 # IPython's runsource returns None if there was an error
1839 # compiling the code. This allows us to stop processing right
1839 # compiling the code. This allows us to stop processing right
1840 # away, so the user gets the error message at the right place.
1840 # away, so the user gets the error message at the right place.
1841 if more is None:
1841 if more is None:
1842 break
1842 break
1843 else:
1843 else:
1844 self.input_hist_raw.append("\n")
1844 self.input_hist_raw.append("\n")
1845 # final newline in case the input didn't have it, so that the code
1845 # final newline in case the input didn't have it, so that the code
1846 # actually does get executed
1846 # actually does get executed
1847 if more:
1847 if more:
1848 self.push_line('\n')
1848 self.push_line('\n')
1849
1849
1850 def runsource(self, source, filename='<input>', symbol='single'):
1850 def runsource(self, source, filename='<input>', symbol='single'):
1851 """Compile and run some source in the interpreter.
1851 """Compile and run some source in the interpreter.
1852
1852
1853 Arguments are as for compile_command().
1853 Arguments are as for compile_command().
1854
1854
1855 One several things can happen:
1855 One several things can happen:
1856
1856
1857 1) The input is incorrect; compile_command() raised an
1857 1) The input is incorrect; compile_command() raised an
1858 exception (SyntaxError or OverflowError). A syntax traceback
1858 exception (SyntaxError or OverflowError). A syntax traceback
1859 will be printed by calling the showsyntaxerror() method.
1859 will be printed by calling the showsyntaxerror() method.
1860
1860
1861 2) The input is incomplete, and more input is required;
1861 2) The input is incomplete, and more input is required;
1862 compile_command() returned None. Nothing happens.
1862 compile_command() returned None. Nothing happens.
1863
1863
1864 3) The input is complete; compile_command() returned a code
1864 3) The input is complete; compile_command() returned a code
1865 object. The code is executed by calling self.runcode() (which
1865 object. The code is executed by calling self.runcode() (which
1866 also handles run-time exceptions, except for SystemExit).
1866 also handles run-time exceptions, except for SystemExit).
1867
1867
1868 The return value is:
1868 The return value is:
1869
1869
1870 - True in case 2
1870 - True in case 2
1871
1871
1872 - False in the other cases, unless an exception is raised, where
1872 - False in the other cases, unless an exception is raised, where
1873 None is returned instead. This can be used by external callers to
1873 None is returned instead. This can be used by external callers to
1874 know whether to continue feeding input or not.
1874 know whether to continue feeding input or not.
1875
1875
1876 The return value can be used to decide whether to use sys.ps1 or
1876 The return value can be used to decide whether to use sys.ps1 or
1877 sys.ps2 to prompt the next line."""
1877 sys.ps2 to prompt the next line."""
1878
1878
1879 # if the source code has leading blanks, add 'if 1:\n' to it
1879 # if the source code has leading blanks, add 'if 1:\n' to it
1880 # this allows execution of indented pasted code. It is tempting
1880 # this allows execution of indented pasted code. It is tempting
1881 # to add '\n' at the end of source to run commands like ' a=1'
1881 # to add '\n' at the end of source to run commands like ' a=1'
1882 # directly, but this fails for more complicated scenarios
1882 # directly, but this fails for more complicated scenarios
1883 source=source.encode(self.stdin_encoding)
1883 source=source.encode(self.stdin_encoding)
1884 if source[:1] in [' ', '\t']:
1884 if source[:1] in [' ', '\t']:
1885 source = 'if 1:\n%s' % source
1885 source = 'if 1:\n%s' % source
1886
1886
1887 try:
1887 try:
1888 code = self.compile(source,filename,symbol)
1888 code = self.compile(source,filename,symbol)
1889 except (OverflowError, SyntaxError, ValueError, TypeError, MemoryError):
1889 except (OverflowError, SyntaxError, ValueError, TypeError, MemoryError):
1890 # Case 1
1890 # Case 1
1891 self.showsyntaxerror(filename)
1891 self.showsyntaxerror(filename)
1892 return None
1892 return None
1893
1893
1894 if code is None:
1894 if code is None:
1895 # Case 2
1895 # Case 2
1896 return True
1896 return True
1897
1897
1898 # Case 3
1898 # Case 3
1899 # We store the code object so that threaded shells and
1899 # We store the code object so that threaded shells and
1900 # custom exception handlers can access all this info if needed.
1900 # custom exception handlers can access all this info if needed.
1901 # The source corresponding to this can be obtained from the
1901 # The source corresponding to this can be obtained from the
1902 # buffer attribute as '\n'.join(self.buffer).
1902 # buffer attribute as '\n'.join(self.buffer).
1903 self.code_to_run = code
1903 self.code_to_run = code
1904 # now actually execute the code object
1904 # now actually execute the code object
1905 if self.runcode(code) == 0:
1905 if self.runcode(code) == 0:
1906 return False
1906 return False
1907 else:
1907 else:
1908 return None
1908 return None
1909
1909
1910 def runcode(self,code_obj):
1910 def runcode(self,code_obj):
1911 """Execute a code object.
1911 """Execute a code object.
1912
1912
1913 When an exception occurs, self.showtraceback() is called to display a
1913 When an exception occurs, self.showtraceback() is called to display a
1914 traceback.
1914 traceback.
1915
1915
1916 Return value: a flag indicating whether the code to be run completed
1916 Return value: a flag indicating whether the code to be run completed
1917 successfully:
1917 successfully:
1918
1918
1919 - 0: successful execution.
1919 - 0: successful execution.
1920 - 1: an error occurred.
1920 - 1: an error occurred.
1921 """
1921 """
1922
1922
1923 # Set our own excepthook in case the user code tries to call it
1923 # Set our own excepthook in case the user code tries to call it
1924 # directly, so that the IPython crash handler doesn't get triggered
1924 # directly, so that the IPython crash handler doesn't get triggered
1925 old_excepthook,sys.excepthook = sys.excepthook, self.excepthook
1925 old_excepthook,sys.excepthook = sys.excepthook, self.excepthook
1926
1926
1927 # we save the original sys.excepthook in the instance, in case config
1927 # we save the original sys.excepthook in the instance, in case config
1928 # code (such as magics) needs access to it.
1928 # code (such as magics) needs access to it.
1929 self.sys_excepthook = old_excepthook
1929 self.sys_excepthook = old_excepthook
1930 outflag = 1 # happens in more places, so it's easier as default
1930 outflag = 1 # happens in more places, so it's easier as default
1931 try:
1931 try:
1932 try:
1932 try:
1933 self.hooks.pre_runcode_hook()
1933 self.hooks.pre_runcode_hook()
1934 #rprint('Running code') # dbg
1934 #rprint('Running code') # dbg
1935 exec code_obj in self.user_global_ns, self.user_ns
1935 exec code_obj in self.user_global_ns, self.user_ns
1936 finally:
1936 finally:
1937 # Reset our crash handler in place
1937 # Reset our crash handler in place
1938 sys.excepthook = old_excepthook
1938 sys.excepthook = old_excepthook
1939 except SystemExit:
1939 except SystemExit:
1940 self.resetbuffer()
1940 self.resetbuffer()
1941 self.showtraceback(exception_only=True)
1941 self.showtraceback(exception_only=True)
1942 warn("To exit: use any of 'exit', 'quit', %Exit or Ctrl-D.", level=1)
1942 warn("To exit: use any of 'exit', 'quit', %Exit or Ctrl-D.", level=1)
1943 except self.custom_exceptions:
1943 except self.custom_exceptions:
1944 etype,value,tb = sys.exc_info()
1944 etype,value,tb = sys.exc_info()
1945 self.CustomTB(etype,value,tb)
1945 self.CustomTB(etype,value,tb)
1946 except:
1946 except:
1947 self.showtraceback()
1947 self.showtraceback()
1948 else:
1948 else:
1949 outflag = 0
1949 outflag = 0
1950 if softspace(sys.stdout, 0):
1950 if softspace(sys.stdout, 0):
1951 print
1951 print
1952 # Flush out code object which has been run (and source)
1952 # Flush out code object which has been run (and source)
1953 self.code_to_run = None
1953 self.code_to_run = None
1954 return outflag
1954 return outflag
1955
1955
1956 def push_line(self, line):
1956 def push_line(self, line):
1957 """Push a line to the interpreter.
1957 """Push a line to the interpreter.
1958
1958
1959 The line should not have a trailing newline; it may have
1959 The line should not have a trailing newline; it may have
1960 internal newlines. The line is appended to a buffer and the
1960 internal newlines. The line is appended to a buffer and the
1961 interpreter's runsource() method is called with the
1961 interpreter's runsource() method is called with the
1962 concatenated contents of the buffer as source. If this
1962 concatenated contents of the buffer as source. If this
1963 indicates that the command was executed or invalid, the buffer
1963 indicates that the command was executed or invalid, the buffer
1964 is reset; otherwise, the command is incomplete, and the buffer
1964 is reset; otherwise, the command is incomplete, and the buffer
1965 is left as it was after the line was appended. The return
1965 is left as it was after the line was appended. The return
1966 value is 1 if more input is required, 0 if the line was dealt
1966 value is 1 if more input is required, 0 if the line was dealt
1967 with in some way (this is the same as runsource()).
1967 with in some way (this is the same as runsource()).
1968 """
1968 """
1969
1969
1970 # autoindent management should be done here, and not in the
1970 # autoindent management should be done here, and not in the
1971 # interactive loop, since that one is only seen by keyboard input. We
1971 # interactive loop, since that one is only seen by keyboard input. We
1972 # need this done correctly even for code run via runlines (which uses
1972 # need this done correctly even for code run via runlines (which uses
1973 # push).
1973 # push).
1974
1974
1975 #print 'push line: <%s>' % line # dbg
1975 #print 'push line: <%s>' % line # dbg
1976 for subline in line.splitlines():
1976 for subline in line.splitlines():
1977 self._autoindent_update(subline)
1977 self._autoindent_update(subline)
1978 self.buffer.append(line)
1978 self.buffer.append(line)
1979 more = self.runsource('\n'.join(self.buffer), self.filename)
1979 more = self.runsource('\n'.join(self.buffer), self.filename)
1980 if not more:
1980 if not more:
1981 self.resetbuffer()
1981 self.resetbuffer()
1982 return more
1982 return more
1983
1983
1984 def resetbuffer(self):
1984 def resetbuffer(self):
1985 """Reset the input buffer."""
1985 """Reset the input buffer."""
1986 self.buffer[:] = []
1986 self.buffer[:] = []
1987
1987
1988 def _is_secondary_block_start(self, s):
1988 def _is_secondary_block_start(self, s):
1989 if not s.endswith(':'):
1989 if not s.endswith(':'):
1990 return False
1990 return False
1991 if (s.startswith('elif') or
1991 if (s.startswith('elif') or
1992 s.startswith('else') or
1992 s.startswith('else') or
1993 s.startswith('except') or
1993 s.startswith('except') or
1994 s.startswith('finally')):
1994 s.startswith('finally')):
1995 return True
1995 return True
1996
1996
1997 def _cleanup_ipy_script(self, script):
1997 def _cleanup_ipy_script(self, script):
1998 """Make a script safe for self.runlines()
1998 """Make a script safe for self.runlines()
1999
1999
2000 Currently, IPython is lines based, with blocks being detected by
2000 Currently, IPython is lines based, with blocks being detected by
2001 empty lines. This is a problem for block based scripts that may
2001 empty lines. This is a problem for block based scripts that may
2002 not have empty lines after blocks. This script adds those empty
2002 not have empty lines after blocks. This script adds those empty
2003 lines to make scripts safe for running in the current line based
2003 lines to make scripts safe for running in the current line based
2004 IPython.
2004 IPython.
2005 """
2005 """
2006 res = []
2006 res = []
2007 lines = script.splitlines()
2007 lines = script.splitlines()
2008 level = 0
2008 level = 0
2009
2009
2010 for l in lines:
2010 for l in lines:
2011 lstripped = l.lstrip()
2011 lstripped = l.lstrip()
2012 stripped = l.strip()
2012 stripped = l.strip()
2013 if not stripped:
2013 if not stripped:
2014 continue
2014 continue
2015 newlevel = len(l) - len(lstripped)
2015 newlevel = len(l) - len(lstripped)
2016 if level > 0 and newlevel == 0 and \
2016 if level > 0 and newlevel == 0 and \
2017 not self._is_secondary_block_start(stripped):
2017 not self._is_secondary_block_start(stripped):
2018 # add empty line
2018 # add empty line
2019 res.append('')
2019 res.append('')
2020 res.append(l)
2020 res.append(l)
2021 level = newlevel
2021 level = newlevel
2022
2022
2023 return '\n'.join(res) + '\n'
2023 return '\n'.join(res) + '\n'
2024
2024
2025 def _autoindent_update(self,line):
2025 def _autoindent_update(self,line):
2026 """Keep track of the indent level."""
2026 """Keep track of the indent level."""
2027
2027
2028 #debugx('line')
2028 #debugx('line')
2029 #debugx('self.indent_current_nsp')
2029 #debugx('self.indent_current_nsp')
2030 if self.autoindent:
2030 if self.autoindent:
2031 if line:
2031 if line:
2032 inisp = num_ini_spaces(line)
2032 inisp = num_ini_spaces(line)
2033 if inisp < self.indent_current_nsp:
2033 if inisp < self.indent_current_nsp:
2034 self.indent_current_nsp = inisp
2034 self.indent_current_nsp = inisp
2035
2035
2036 if line[-1] == ':':
2036 if line[-1] == ':':
2037 self.indent_current_nsp += 4
2037 self.indent_current_nsp += 4
2038 elif dedent_re.match(line):
2038 elif dedent_re.match(line):
2039 self.indent_current_nsp -= 4
2039 self.indent_current_nsp -= 4
2040 else:
2040 else:
2041 self.indent_current_nsp = 0
2041 self.indent_current_nsp = 0
2042
2042
2043 #-------------------------------------------------------------------------
2043 #-------------------------------------------------------------------------
2044 # Things related to GUI support and pylab
2044 # Things related to GUI support and pylab
2045 #-------------------------------------------------------------------------
2045 #-------------------------------------------------------------------------
2046
2046
2047 def enable_pylab(self, gui=None):
2047 def enable_pylab(self, gui=None):
2048 raise NotImplementedError('Implement enable_pylab in a subclass')
2048 raise NotImplementedError('Implement enable_pylab in a subclass')
2049
2049
2050 #-------------------------------------------------------------------------
2050 #-------------------------------------------------------------------------
2051 # Utilities
2051 # Utilities
2052 #-------------------------------------------------------------------------
2052 #-------------------------------------------------------------------------
2053
2053
2054 def getoutput(self, cmd):
2054 def getoutput(self, cmd):
2055 return getoutput(self.var_expand(cmd,depth=2),
2055 return getoutput(self.var_expand(cmd,depth=2),
2056 header=self.system_header,
2056 header=self.system_header,
2057 verbose=self.system_verbose)
2057 verbose=self.system_verbose)
2058
2058
2059 def getoutputerror(self, cmd):
2059 def getoutputerror(self, cmd):
2060 return getoutputerror(self.var_expand(cmd,depth=2),
2060 return getoutputerror(self.var_expand(cmd,depth=2),
2061 header=self.system_header,
2061 header=self.system_header,
2062 verbose=self.system_verbose)
2062 verbose=self.system_verbose)
2063
2063
2064 def var_expand(self,cmd,depth=0):
2064 def var_expand(self,cmd,depth=0):
2065 """Expand python variables in a string.
2065 """Expand python variables in a string.
2066
2066
2067 The depth argument indicates how many frames above the caller should
2067 The depth argument indicates how many frames above the caller should
2068 be walked to look for the local namespace where to expand variables.
2068 be walked to look for the local namespace where to expand variables.
2069
2069
2070 The global namespace for expansion is always the user's interactive
2070 The global namespace for expansion is always the user's interactive
2071 namespace.
2071 namespace.
2072 """
2072 """
2073
2073
2074 return str(ItplNS(cmd,
2074 return str(ItplNS(cmd,
2075 self.user_ns, # globals
2075 self.user_ns, # globals
2076 # Skip our own frame in searching for locals:
2076 # Skip our own frame in searching for locals:
2077 sys._getframe(depth+1).f_locals # locals
2077 sys._getframe(depth+1).f_locals # locals
2078 ))
2078 ))
2079
2079
2080 def mktempfile(self,data=None):
2080 def mktempfile(self,data=None):
2081 """Make a new tempfile and return its filename.
2081 """Make a new tempfile and return its filename.
2082
2082
2083 This makes a call to tempfile.mktemp, but it registers the created
2083 This makes a call to tempfile.mktemp, but it registers the created
2084 filename internally so ipython cleans it up at exit time.
2084 filename internally so ipython cleans it up at exit time.
2085
2085
2086 Optional inputs:
2086 Optional inputs:
2087
2087
2088 - data(None): if data is given, it gets written out to the temp file
2088 - data(None): if data is given, it gets written out to the temp file
2089 immediately, and the file is closed again."""
2089 immediately, and the file is closed again."""
2090
2090
2091 filename = tempfile.mktemp('.py','ipython_edit_')
2091 filename = tempfile.mktemp('.py','ipython_edit_')
2092 self.tempfiles.append(filename)
2092 self.tempfiles.append(filename)
2093
2093
2094 if data:
2094 if data:
2095 tmp_file = open(filename,'w')
2095 tmp_file = open(filename,'w')
2096 tmp_file.write(data)
2096 tmp_file.write(data)
2097 tmp_file.close()
2097 tmp_file.close()
2098 return filename
2098 return filename
2099
2099
2100 # TODO: This should be removed when Term is refactored.
2100 # TODO: This should be removed when Term is refactored.
2101 def write(self,data):
2101 def write(self,data):
2102 """Write a string to the default output"""
2102 """Write a string to the default output"""
2103 io.Term.cout.write(data)
2103 io.Term.cout.write(data)
2104
2104
2105 # TODO: This should be removed when Term is refactored.
2105 # TODO: This should be removed when Term is refactored.
2106 def write_err(self,data):
2106 def write_err(self,data):
2107 """Write a string to the default error output"""
2107 """Write a string to the default error output"""
2108 io.Term.cerr.write(data)
2108 io.Term.cerr.write(data)
2109
2109
2110 def ask_yes_no(self,prompt,default=True):
2110 def ask_yes_no(self,prompt,default=True):
2111 if self.quiet:
2111 if self.quiet:
2112 return True
2112 return True
2113 return ask_yes_no(prompt,default)
2113 return ask_yes_no(prompt,default)
2114
2114
2115 #-------------------------------------------------------------------------
2115 #-------------------------------------------------------------------------
2116 # Things related to IPython exiting
2116 # Things related to IPython exiting
2117 #-------------------------------------------------------------------------
2117 #-------------------------------------------------------------------------
2118
2118
2119 def atexit_operations(self):
2119 def atexit_operations(self):
2120 """This will be executed at the time of exit.
2120 """This will be executed at the time of exit.
2121
2121
2122 Saving of persistent data should be performed here.
2122 Saving of persistent data should be performed here.
2123 """
2123 """
2124 self.savehist()
2124 self.savehist()
2125
2125
2126 # Cleanup all tempfiles left around
2126 # Cleanup all tempfiles left around
2127 for tfile in self.tempfiles:
2127 for tfile in self.tempfiles:
2128 try:
2128 try:
2129 os.unlink(tfile)
2129 os.unlink(tfile)
2130 except OSError:
2130 except OSError:
2131 pass
2131 pass
2132
2132
2133 # Clear all user namespaces to release all references cleanly.
2133 # Clear all user namespaces to release all references cleanly.
2134 self.reset()
2134 self.reset()
2135
2135
2136 # Run user hooks
2136 # Run user hooks
2137 self.hooks.shutdown_hook()
2137 self.hooks.shutdown_hook()
2138
2138
2139 def cleanup(self):
2139 def cleanup(self):
2140 self.restore_sys_module_state()
2140 self.restore_sys_module_state()
2141
2141
2142
2142
2143 class InteractiveShellABC(object):
2143 class InteractiveShellABC(object):
2144 """An abstract base class for InteractiveShell."""
2144 """An abstract base class for InteractiveShell."""
2145 __metaclass__ = abc.ABCMeta
2145 __metaclass__ = abc.ABCMeta
2146
2146
2147 InteractiveShellABC.register(InteractiveShell)
2147 InteractiveShellABC.register(InteractiveShell)
@@ -1,147 +1,183
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """Pylab (matplotlib) support utilities.
2 """Pylab (matplotlib) support utilities.
3
3
4 Authors
4 Authors
5 -------
5 -------
6 Fernando Perez.
6 Fernando Perez.
7 """
7 """
8
8
9 #-----------------------------------------------------------------------------
9 #-----------------------------------------------------------------------------
10 # Copyright (C) 2009 The IPython Development Team
10 # Copyright (C) 2009 The IPython Development Team
11 #
11 #
12 # Distributed under the terms of the BSD License. The full license is in
12 # Distributed under the terms of the BSD License. The full license is in
13 # the file COPYING, distributed as part of this software.
13 # the file COPYING, distributed as part of this software.
14 #-----------------------------------------------------------------------------
14 #-----------------------------------------------------------------------------
15
15
16 #-----------------------------------------------------------------------------
16 #-----------------------------------------------------------------------------
17 # Imports
17 # Imports
18 #-----------------------------------------------------------------------------
18 #-----------------------------------------------------------------------------
19
19
20 from IPython.utils.decorators import flag_calls
20 from IPython.utils.decorators import flag_calls
21
21
22 #-----------------------------------------------------------------------------
22 #-----------------------------------------------------------------------------
23 # Main classes and functions
23 # Main classes and functions
24 #-----------------------------------------------------------------------------
24 #-----------------------------------------------------------------------------
25
25
26 def pylab_activate(user_ns, gui=None, import_all=True):
27 """Activate pylab mode in the user's namespace.
28
26
29 Loads and initializes numpy, matplotlib and friends for interactive use.
27 def find_gui_and_backend(gui=None):
28 """Given a gui string return the gui and mpl backend.
30
29
31 Parameters
30 Parameters
32 ----------
31 ----------
33 user_ns : dict
32 gui : str
34 Namespace where the imports will occur.
33 Can be one of ('tk','gtk','wx','qt','qt4','payload-svg').
35
36 gui : optional, string
37 A valid gui name following the conventions of the %gui magic.
38
39 import_all : optional, boolean
40 If true, an 'import *' is done from numpy and pylab.
41
34
42 Returns
35 Returns
43 -------
36 -------
44 The actual gui used (if not given as input, it was obtained from matplotlib
37 A tuple of (gui, backend) where backend is one of ('TkAgg','GTKAgg',
45 itself, and will be needed next to configure IPython's gui integration.
38 'WXAgg','Qt4Agg','module://IPython.zmq.pylab.backend_payload_svg').
46 """
39 """
47
40
48 # Initialize matplotlib to interactive mode always
49 import matplotlib
41 import matplotlib
50
42
51 # If user specifies a GUI, that dictates the backend, otherwise we read the
43 # If user specifies a GUI, that dictates the backend, otherwise we read the
52 # user's mpl default from the mpl rc structure
44 # user's mpl default from the mpl rc structure
53 g2b = {'tk': 'TkAgg',
45 g2b = {'tk': 'TkAgg',
54 'gtk': 'GTKAgg',
46 'gtk': 'GTKAgg',
55 'wx': 'WXAgg',
47 'wx': 'WXAgg',
56 'qt': 'Qt4Agg', # qt3 not supported
48 'qt': 'Qt4Agg', # qt3 not supported
57 'qt4': 'Qt4Agg' }
49 'qt4': 'Qt4Agg',
50 'payload-svg' : \
51 'module://IPython.zmq.pylab.backend_payload_svg'}
58
52
59 if gui:
53 if gui:
60 # select backend based on requested gui
54 # select backend based on requested gui
61 backend = g2b[gui]
55 backend = g2b[gui]
62 else:
56 else:
63 backend = matplotlib.rcParams['backend']
57 backend = matplotlib.rcParams['backend']
64 # In this case, we need to find what the appropriate gui selection call
58 # In this case, we need to find what the appropriate gui selection call
65 # should be for IPython, so we can activate inputhook accordingly
59 # should be for IPython, so we can activate inputhook accordingly
66 b2g = dict(zip(g2b.values(),g2b.keys()))
60 b2g = dict(zip(g2b.values(),g2b.keys()))
67 gui = b2g.get(backend, None)
61 gui = b2g.get(backend, None)
62 return gui, backend
68
63
69 # We must set the desired backend before importing pylab
70 matplotlib.use(backend)
71
64
72 # This must be imported last in the matplotlib series, after
65 def activate_matplotlib(backend):
73 # backend/interactivity choices have been made
66 """Activate the given backend and set interactive to True."""
67
68 import matplotlib
69 if backend.startswith('module://'):
70 # Work around bug in matplotlib: matplotlib.use converts the
71 # backend_id to lowercase even if a module name is specified!
72 matplotlib.rcParams['backend'] = backend
73 else:
74 matplotlib.use(backend)
75 matplotlib.interactive(True)
74 import matplotlib.pylab as pylab
76 import matplotlib.pylab as pylab
75
77
76 # XXX For now leave this commented out, but depending on discussions with
77 # mpl-dev, we may be able to allow interactive switching...
78 #import matplotlib.pyplot
79 #matplotlib.pyplot.switch_backend(backend)
80
78
81 pylab.show._needmain = False
79 def import_pylab(user_ns, import_all=True):
82 # We need to detect at runtime whether show() is called by the user.
80 """Import the standard pylab symbols into user_ns."""
83 # For this, we wrap it into a decorator which adds a 'called' flag.
84 pylab.draw_if_interactive = flag_calls(pylab.draw_if_interactive)
85
81
86 # Import numpy as np/pyplot as plt are conventions we're trying to
82 # Import numpy as np/pyplot as plt are conventions we're trying to
87 # somewhat standardize on. Making them available to users by default
83 # somewhat standardize on. Making them available to users by default
88 # will greatly help this.
84 # will greatly help this.
89 exec ("import numpy\n"
85 exec ("import numpy\n"
90 "import matplotlib\n"
86 "import matplotlib\n"
91 "from matplotlib import pylab, mlab, pyplot\n"
87 "from matplotlib import pylab, mlab, pyplot\n"
92 "np = numpy\n"
88 "np = numpy\n"
93 "plt = pyplot\n"
89 "plt = pyplot\n"
94 ) in user_ns
90 ) in user_ns
95
91
96 if import_all:
92 if import_all:
97 exec("from matplotlib.pylab import *\n"
93 exec("from matplotlib.pylab import *\n"
98 "from numpy import *\n") in user_ns
94 "from numpy import *\n") in user_ns
99
95
100 matplotlib.interactive(True)
96
97 def pylab_activate(user_ns, gui=None, import_all=True):
98 """Activate pylab mode in the user's namespace.
99
100 Loads and initializes numpy, matplotlib and friends for interactive use.
101
102 Parameters
103 ----------
104 user_ns : dict
105 Namespace where the imports will occur.
106
107 gui : optional, string
108 A valid gui name following the conventions of the %gui magic.
109
110 import_all : optional, boolean
111 If true, an 'import *' is done from numpy and pylab.
112
113 Returns
114 -------
115 The actual gui used (if not given as input, it was obtained from matplotlib
116 itself, and will be needed next to configure IPython's gui integration.
117 """
118
119 gui, backend = find_gui_and_backend(gui)
120 activate_matplotlib(backend)
121
122 # This must be imported last in the matplotlib series, after
123 # backend/interactivity choices have been made
124 import matplotlib.pylab as pylab
125
126 # XXX For now leave this commented out, but depending on discussions with
127 # mpl-dev, we may be able to allow interactive switching...
128 #import matplotlib.pyplot
129 #matplotlib.pyplot.switch_backend(backend)
130
131 pylab.show._needmain = False
132 # We need to detect at runtime whether show() is called by the user.
133 # For this, we wrap it into a decorator which adds a 'called' flag.
134 pylab.draw_if_interactive = flag_calls(pylab.draw_if_interactive)
135
136 import_pylab(user_ns)
101
137
102 print """
138 print """
103 Welcome to pylab, a matplotlib-based Python environment [backend: %s].
139 Welcome to pylab, a matplotlib-based Python environment [backend: %s].
104 For more information, type 'help(pylab)'.""" % backend
140 For more information, type 'help(pylab)'.""" % backend
105
141
106 return gui
142 return gui
107
143
108 # We need a little factory function here to create the closure where
144 # We need a little factory function here to create the closure where
109 # safe_execfile can live.
145 # safe_execfile can live.
110 def mpl_runner(safe_execfile):
146 def mpl_runner(safe_execfile):
111 """Factory to return a matplotlib-enabled runner for %run.
147 """Factory to return a matplotlib-enabled runner for %run.
112
148
113 Parameters
149 Parameters
114 ----------
150 ----------
115 safe_execfile : function
151 safe_execfile : function
116 This must be a function with the same interface as the
152 This must be a function with the same interface as the
117 :meth:`safe_execfile` method of IPython.
153 :meth:`safe_execfile` method of IPython.
118
154
119 Returns
155 Returns
120 -------
156 -------
121 A function suitable for use as the ``runner`` argument of the %run magic
157 A function suitable for use as the ``runner`` argument of the %run magic
122 function.
158 function.
123 """
159 """
124
160
125 def mpl_execfile(fname,*where,**kw):
161 def mpl_execfile(fname,*where,**kw):
126 """matplotlib-aware wrapper around safe_execfile.
162 """matplotlib-aware wrapper around safe_execfile.
127
163
128 Its interface is identical to that of the :func:`execfile` builtin.
164 Its interface is identical to that of the :func:`execfile` builtin.
129
165
130 This is ultimately a call to execfile(), but wrapped in safeties to
166 This is ultimately a call to execfile(), but wrapped in safeties to
131 properly handle interactive rendering."""
167 properly handle interactive rendering."""
132
168
133 import matplotlib
169 import matplotlib
134 import matplotlib.pylab as pylab
170 import matplotlib.pylab as pylab
135
171
136 #print '*** Matplotlib runner ***' # dbg
172 #print '*** Matplotlib runner ***' # dbg
137 # turn off rendering until end of script
173 # turn off rendering until end of script
138 is_interactive = matplotlib.rcParams['interactive']
174 is_interactive = matplotlib.rcParams['interactive']
139 matplotlib.interactive(False)
175 matplotlib.interactive(False)
140 safe_execfile(fname,*where,**kw)
176 safe_execfile(fname,*where,**kw)
141 matplotlib.interactive(is_interactive)
177 matplotlib.interactive(is_interactive)
142 # make rendering call now, if the user tried to do it
178 # make rendering call now, if the user tried to do it
143 if pylab.draw_if_interactive.called:
179 if pylab.draw_if_interactive.called:
144 pylab.draw()
180 pylab.draw()
145 pylab.draw_if_interactive.called = False
181 pylab.draw_if_interactive.called = False
146
182
147 return mpl_execfile
183 return mpl_execfile
@@ -1,191 +1,192
1 """ Defines helper functions for creating kernel entry points and process
1 """ Defines helper functions for creating kernel entry points and process
2 launchers.
2 launchers.
3 """
3 """
4
4
5 # Standard library imports.
5 # Standard library imports.
6 import socket
6 import socket
7 from subprocess import Popen
7 from subprocess import Popen
8 import sys
8 import sys
9
9
10 # System library imports.
10 # System library imports.
11 import zmq
11 import zmq
12
12
13 # Local imports.
13 # Local imports.
14 from IPython.external.argparse import ArgumentParser
14 from IPython.external.argparse import ArgumentParser
15 from exitpoller import ExitPollerUnix, ExitPollerWindows
15 from exitpoller import ExitPollerUnix, ExitPollerWindows
16 from displayhook import DisplayHook
16 from displayhook import DisplayHook
17 from iostream import OutStream
17 from iostream import OutStream
18 from session import Session
18 from session import Session
19
19
20
20
21 def bind_port(socket, ip, port):
21 def bind_port(socket, ip, port):
22 """ Binds the specified ZMQ socket. If the port is zero, a random port is
22 """ Binds the specified ZMQ socket. If the port is zero, a random port is
23 chosen. Returns the port that was bound.
23 chosen. Returns the port that was bound.
24 """
24 """
25 connection = 'tcp://%s' % ip
25 connection = 'tcp://%s' % ip
26 if port <= 0:
26 if port <= 0:
27 port = socket.bind_to_random_port(connection)
27 port = socket.bind_to_random_port(connection)
28 else:
28 else:
29 connection += ':%i' % port
29 connection += ':%i' % port
30 socket.bind(connection)
30 socket.bind(connection)
31 return port
31 return port
32
32
33
33
34 def make_argument_parser():
34 def make_argument_parser():
35 """ Creates an ArgumentParser for the generic arguments supported by all
35 """ Creates an ArgumentParser for the generic arguments supported by all
36 kernel entry points.
36 kernel entry points.
37 """
37 """
38 parser = ArgumentParser()
38 parser = ArgumentParser()
39 parser.add_argument('--ip', type=str, default='127.0.0.1',
39 parser.add_argument('--ip', type=str, default='127.0.0.1',
40 help='set the kernel\'s IP address [default: local]')
40 help='set the kernel\'s IP address [default: local]')
41 parser.add_argument('--xrep', type=int, metavar='PORT', default=0,
41 parser.add_argument('--xrep', type=int, metavar='PORT', default=0,
42 help='set the XREP channel port [default: random]')
42 help='set the XREP channel port [default: random]')
43 parser.add_argument('--pub', type=int, metavar='PORT', default=0,
43 parser.add_argument('--pub', type=int, metavar='PORT', default=0,
44 help='set the PUB channel port [default: random]')
44 help='set the PUB channel port [default: random]')
45 parser.add_argument('--req', type=int, metavar='PORT', default=0,
45 parser.add_argument('--req', type=int, metavar='PORT', default=0,
46 help='set the REQ channel port [default: random]')
46 help='set the REQ channel port [default: random]')
47
47
48 if sys.platform == 'win32':
48 if sys.platform == 'win32':
49 parser.add_argument('--parent', type=int, metavar='HANDLE',
49 parser.add_argument('--parent', type=int, metavar='HANDLE',
50 default=0, help='kill this process if the process '
50 default=0, help='kill this process if the process '
51 'with HANDLE dies')
51 'with HANDLE dies')
52 else:
52 else:
53 parser.add_argument('--parent', action='store_true',
53 parser.add_argument('--parent', action='store_true',
54 help='kill this process if its parent dies')
54 help='kill this process if its parent dies')
55
55
56 return parser
56 return parser
57
57
58
58
59 def make_kernel(namespace, kernel_factory,
59 def make_kernel(namespace, kernel_factory,
60 out_stream_factory=None, display_hook_factory=None):
60 out_stream_factory=None, display_hook_factory=None):
61 """ Creates a kernel.
61 """ Creates a kernel.
62 """
62 """
63 # Create a context, a session, and the kernel sockets.
63 # Create a context, a session, and the kernel sockets.
64 print >>sys.__stdout__, "Starting the kernel..."
64 print >>sys.__stdout__, "Starting the kernel..."
65 context = zmq.Context()
65 context = zmq.Context()
66 session = Session(username=u'kernel')
66 session = Session(username=u'kernel')
67
67
68 reply_socket = context.socket(zmq.XREP)
68 reply_socket = context.socket(zmq.XREP)
69 xrep_port = bind_port(reply_socket, namespace.ip, namespace.xrep)
69 xrep_port = bind_port(reply_socket, namespace.ip, namespace.xrep)
70 print >>sys.__stdout__, "XREP Channel on port", xrep_port
70 print >>sys.__stdout__, "XREP Channel on port", xrep_port
71
71
72 pub_socket = context.socket(zmq.PUB)
72 pub_socket = context.socket(zmq.PUB)
73 pub_port = bind_port(pub_socket, namespace.ip, namespace.pub)
73 pub_port = bind_port(pub_socket, namespace.ip, namespace.pub)
74 print >>sys.__stdout__, "PUB Channel on port", pub_port
74 print >>sys.__stdout__, "PUB Channel on port", pub_port
75
75
76 req_socket = context.socket(zmq.XREQ)
76 req_socket = context.socket(zmq.XREQ)
77 req_port = bind_port(req_socket, namespace.ip, namespace.req)
77 req_port = bind_port(req_socket, namespace.ip, namespace.req)
78 print >>sys.__stdout__, "REQ Channel on port", req_port
78 print >>sys.__stdout__, "REQ Channel on port", req_port
79
79
80 # Redirect input streams and set a display hook.
80 # Redirect input streams and set a display hook.
81 if out_stream_factory:
81 if out_stream_factory:
82 pass
82 sys.stdout = out_stream_factory(session, pub_socket, u'stdout')
83 sys.stdout = out_stream_factory(session, pub_socket, u'stdout')
83 sys.stderr = out_stream_factory(session, pub_socket, u'stderr')
84 sys.stderr = out_stream_factory(session, pub_socket, u'stderr')
84 if display_hook_factory:
85 if display_hook_factory:
85 sys.displayhook = display_hook_factory(session, pub_socket)
86 sys.displayhook = display_hook_factory(session, pub_socket)
86
87
87 # Create the kernel.
88 # Create the kernel.
88 return kernel_factory(session=session, reply_socket=reply_socket,
89 return kernel_factory(session=session, reply_socket=reply_socket,
89 pub_socket=pub_socket, req_socket=req_socket)
90 pub_socket=pub_socket, req_socket=req_socket)
90
91
91
92
92 def start_kernel(namespace, kernel):
93 def start_kernel(namespace, kernel):
93 """ Starts a kernel.
94 """ Starts a kernel.
94 """
95 """
95 # Configure this kernel/process to die on parent termination, if necessary.
96 # Configure this kernel/process to die on parent termination, if necessary.
96 if namespace.parent:
97 if namespace.parent:
97 if sys.platform == 'win32':
98 if sys.platform == 'win32':
98 poller = ExitPollerWindows(namespace.parent)
99 poller = ExitPollerWindows(namespace.parent)
99 else:
100 else:
100 poller = ExitPollerUnix()
101 poller = ExitPollerUnix()
101 poller.start()
102 poller.start()
102
103
103 # Start the kernel mainloop.
104 # Start the kernel mainloop.
104 kernel.start()
105 kernel.start()
105
106
106
107
107 def make_default_main(kernel_factory):
108 def make_default_main(kernel_factory):
108 """ Creates the simplest possible kernel entry point.
109 """ Creates the simplest possible kernel entry point.
109 """
110 """
110 def main():
111 def main():
111 namespace = make_argument_parser().parse_args()
112 namespace = make_argument_parser().parse_args()
112 kernel = make_kernel(namespace, kernel_factory, OutStream, DisplayHook)
113 kernel = make_kernel(namespace, kernel_factory, OutStream, DisplayHook)
113 start_kernel(namespace, kernel)
114 start_kernel(namespace, kernel)
114 return main
115 return main
115
116
116
117
117 def base_launch_kernel(code, xrep_port=0, pub_port=0, req_port=0,
118 def base_launch_kernel(code, xrep_port=0, pub_port=0, req_port=0,
118 independent=False, extra_arguments=[]):
119 independent=False, extra_arguments=[]):
119 """ Launches a localhost kernel, binding to the specified ports.
120 """ Launches a localhost kernel, binding to the specified ports.
120
121
121 Parameters
122 Parameters
122 ----------
123 ----------
123 code : str,
124 code : str,
124 A string of Python code that imports and executes a kernel entry point.
125 A string of Python code that imports and executes a kernel entry point.
125
126
126 xrep_port : int, optional
127 xrep_port : int, optional
127 The port to use for XREP channel.
128 The port to use for XREP channel.
128
129
129 pub_port : int, optional
130 pub_port : int, optional
130 The port to use for the SUB channel.
131 The port to use for the SUB channel.
131
132
132 req_port : int, optional
133 req_port : int, optional
133 The port to use for the REQ (raw input) channel.
134 The port to use for the REQ (raw input) channel.
134
135
135 independent : bool, optional (default False)
136 independent : bool, optional (default False)
136 If set, the kernel process is guaranteed to survive if this process
137 If set, the kernel process is guaranteed to survive if this process
137 dies. If not set, an effort is made to ensure that the kernel is killed
138 dies. If not set, an effort is made to ensure that the kernel is killed
138 when this process dies. Note that in this case it is still good practice
139 when this process dies. Note that in this case it is still good practice
139 to kill kernels manually before exiting.
140 to kill kernels manually before exiting.
140
141
141 extra_arguments = list, optional
142 extra_arguments = list, optional
142 A list of extra arguments to pass when executing the launch code.
143 A list of extra arguments to pass when executing the launch code.
143
144
144 Returns
145 Returns
145 -------
146 -------
146 A tuple of form:
147 A tuple of form:
147 (kernel_process, xrep_port, pub_port, req_port)
148 (kernel_process, xrep_port, pub_port, req_port)
148 where kernel_process is a Popen object and the ports are integers.
149 where kernel_process is a Popen object and the ports are integers.
149 """
150 """
150 # Find open ports as necessary.
151 # Find open ports as necessary.
151 ports = []
152 ports = []
152 ports_needed = int(xrep_port <= 0) + int(pub_port <= 0) + int(req_port <= 0)
153 ports_needed = int(xrep_port <= 0) + int(pub_port <= 0) + int(req_port <= 0)
153 for i in xrange(ports_needed):
154 for i in xrange(ports_needed):
154 sock = socket.socket()
155 sock = socket.socket()
155 sock.bind(('', 0))
156 sock.bind(('', 0))
156 ports.append(sock)
157 ports.append(sock)
157 for i, sock in enumerate(ports):
158 for i, sock in enumerate(ports):
158 port = sock.getsockname()[1]
159 port = sock.getsockname()[1]
159 sock.close()
160 sock.close()
160 ports[i] = port
161 ports[i] = port
161 if xrep_port <= 0:
162 if xrep_port <= 0:
162 xrep_port = ports.pop(0)
163 xrep_port = ports.pop(0)
163 if pub_port <= 0:
164 if pub_port <= 0:
164 pub_port = ports.pop(0)
165 pub_port = ports.pop(0)
165 if req_port <= 0:
166 if req_port <= 0:
166 req_port = ports.pop(0)
167 req_port = ports.pop(0)
167
168
168 # Build the kernel launch command.
169 # Build the kernel launch command.
169 arguments = [ sys.executable, '-c', code, '--xrep', str(xrep_port),
170 arguments = [ sys.executable, '-c', code, '--xrep', str(xrep_port),
170 '--pub', str(pub_port), '--req', str(req_port) ]
171 '--pub', str(pub_port), '--req', str(req_port) ]
171 arguments.extend(extra_arguments)
172 arguments.extend(extra_arguments)
172
173
173 # Spawn a kernel.
174 # Spawn a kernel.
174 if independent:
175 if independent:
175 if sys.platform == 'win32':
176 if sys.platform == 'win32':
176 proc = Popen(['start', '/b'] + arguments, shell=True)
177 proc = Popen(['start', '/b'] + arguments, shell=True)
177 else:
178 else:
178 proc = Popen(arguments, preexec_fn=lambda: os.setsid())
179 proc = Popen(arguments, preexec_fn=lambda: os.setsid())
179 else:
180 else:
180 if sys.platform == 'win32':
181 if sys.platform == 'win32':
181 from _subprocess import DuplicateHandle, GetCurrentProcess, \
182 from _subprocess import DuplicateHandle, GetCurrentProcess, \
182 DUPLICATE_SAME_ACCESS
183 DUPLICATE_SAME_ACCESS
183 pid = GetCurrentProcess()
184 pid = GetCurrentProcess()
184 handle = DuplicateHandle(pid, pid, pid, 0,
185 handle = DuplicateHandle(pid, pid, pid, 0,
185 True, # Inheritable by new processes.
186 True, # Inheritable by new processes.
186 DUPLICATE_SAME_ACCESS)
187 DUPLICATE_SAME_ACCESS)
187 proc = Popen(arguments + ['--parent', str(int(handle))])
188 proc = Popen(arguments + ['--parent', str(int(handle))])
188 else:
189 else:
189 proc = Popen(arguments + ['--parent'])
190 proc = Popen(arguments + ['--parent'])
190
191
191 return proc, xrep_port, pub_port, req_port
192 return proc, xrep_port, pub_port, req_port
@@ -1,399 +1,391
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
16
17 # Standard library imports.
17 # Standard library imports.
18 import __builtin__
18 import __builtin__
19 import sys
19 import sys
20 import time
20 import time
21 import traceback
21 import traceback
22
22
23 # System library imports.
23 # System library imports.
24 import zmq
24 import zmq
25
25
26 # Local imports.
26 # Local imports.
27 from IPython.config.configurable import Configurable
27 from IPython.config.configurable import Configurable
28 from IPython.lib import pylabtools
28 from IPython.utils.traitlets import Instance
29 from IPython.utils.traitlets import Instance
29 from completer import KernelCompleter
30 from entry_point import base_launch_kernel, make_argument_parser, make_kernel, \
30 from entry_point import base_launch_kernel, make_argument_parser, make_kernel, \
31 start_kernel
31 start_kernel
32 from iostream import OutStream
32 from iostream import OutStream
33 from session import Session, Message
33 from session import Session, Message
34 from zmqshell import ZMQInteractiveShell
34 from zmqshell import ZMQInteractiveShell
35
35
36 #-----------------------------------------------------------------------------
36 #-----------------------------------------------------------------------------
37 # Main kernel class
37 # Main kernel class
38 #-----------------------------------------------------------------------------
38 #-----------------------------------------------------------------------------
39
39
40 class Kernel(Configurable):
40 class Kernel(Configurable):
41
41
42 #---------------------------------------------------------------------------
42 #---------------------------------------------------------------------------
43 # Kernel interface
43 # Kernel interface
44 #---------------------------------------------------------------------------
44 #---------------------------------------------------------------------------
45
45
46 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC')
46 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC')
47 session = Instance(Session)
47 session = Instance(Session)
48 reply_socket = Instance('zmq.Socket')
48 reply_socket = Instance('zmq.Socket')
49 pub_socket = Instance('zmq.Socket')
49 pub_socket = Instance('zmq.Socket')
50 req_socket = Instance('zmq.Socket')
50 req_socket = Instance('zmq.Socket')
51
51
52 # Maps user-friendly backend names to matplotlib backend identifiers.
53 _pylab_map = { 'tk': 'TkAgg',
54 'gtk': 'GTKAgg',
55 'wx': 'WXAgg',
56 'qt': 'Qt4Agg', # qt3 not supported
57 'qt4': 'Qt4Agg',
58 'payload-svg' : \
59 'module://IPython.zmq.pylab.backend_payload_svg' }
60
61 def __init__(self, **kwargs):
52 def __init__(self, **kwargs):
62 super(Kernel, self).__init__(**kwargs)
53 super(Kernel, self).__init__(**kwargs)
63
54
64 # Initialize the InteractiveShell subclass
55 # Initialize the InteractiveShell subclass
65 self.shell = ZMQInteractiveShell.instance()
56 self.shell = ZMQInteractiveShell.instance()
66 self.shell.displayhook.session = self.session
57 self.shell.displayhook.session = self.session
67 self.shell.displayhook.pub_socket = self.pub_socket
58 self.shell.displayhook.pub_socket = self.pub_socket
68
59
69 # TMP - hack while developing
60 # TMP - hack while developing
70 self.shell._reply_content = None
61 self.shell._reply_content = None
71
62
72 # Build dict of handlers for message types
63 # Build dict of handlers for message types
73 msg_types = [ 'execute_request', 'complete_request',
64 msg_types = [ 'execute_request', 'complete_request',
74 'object_info_request', 'prompt_request',
65 'object_info_request', 'prompt_request',
75 'history_request' ]
66 'history_request' ]
76 self.handlers = {}
67 self.handlers = {}
77 for msg_type in msg_types:
68 for msg_type in msg_types:
78 self.handlers[msg_type] = getattr(self, msg_type)
69 self.handlers[msg_type] = getattr(self, msg_type)
79
70
80 def activate_pylab(self, backend=None, import_all=True):
71 def do_one_iteration(self):
81 """ Activates pylab in this kernel's namespace.
72 try:
82
73 ident = self.reply_socket.recv(zmq.NOBLOCK)
83 Parameters:
74 except zmq.ZMQError, e:
84 -----------
75 if e.errno == zmq.EAGAIN:
85 backend : str, optional
76 return
86 A valid backend name.
87
88 import_all : bool, optional
89 If true, an 'import *' is done from numpy and pylab.
90 """
91 # FIXME: This is adapted from IPython.lib.pylabtools.pylab_activate.
92 # Common functionality should be refactored.
93
94 # We must set the desired backend before importing pylab.
95 import matplotlib
96 if backend:
97 backend_id = self._pylab_map[backend]
98 if backend_id.startswith('module://'):
99 # Work around bug in matplotlib: matplotlib.use converts the
100 # backend_id to lowercase even if a module name is specified!
101 matplotlib.rcParams['backend'] = backend_id
102 else:
77 else:
103 matplotlib.use(backend_id)
78 raise
104
79 # FIXME: Bug in pyzmq/zmq?
105 # Import numpy as np/pyplot as plt are conventions we're trying to
80 # assert self.reply_socket.rcvmore(), "Missing message part."
106 # somewhat standardize on. Making them available to users by default
107 # will greatly help this.
108 exec ("import numpy\n"
109 "import matplotlib\n"
110 "from matplotlib import pylab, mlab, pyplot\n"
111 "np = numpy\n"
112 "plt = pyplot\n"
113 ) in self.shell.user_ns
114
115 if import_all:
116 exec("from matplotlib.pylab import *\n"
117 "from numpy import *\n") in self.shell.user_ns
118
119 matplotlib.interactive(True)
120
121 def start(self):
122 """ Start the kernel main loop.
123 """
124 while True:
125 ident = self.reply_socket.recv()
126 assert self.reply_socket.rcvmore(), "Missing message part."
127 msg = self.reply_socket.recv_json()
81 msg = self.reply_socket.recv_json()
128 omsg = Message(msg)
82 omsg = Message(msg)
129 print>>sys.__stdout__
83 print>>sys.__stdout__
130 print>>sys.__stdout__, omsg
84 print>>sys.__stdout__, omsg
131 handler = self.handlers.get(omsg.msg_type, None)
85 handler = self.handlers.get(omsg.msg_type, None)
132 if handler is None:
86 if handler is None:
133 print >> sys.__stderr__, "UNKNOWN MESSAGE TYPE:", omsg
87 print >> sys.__stderr__, "UNKNOWN MESSAGE TYPE:", omsg
134 else:
88 else:
135 handler(ident, omsg)
89 handler(ident, omsg)
136
90
91 def start(self):
92 """ Start the kernel main loop.
93 """
94 while True:
95 time.sleep(0.05)
96 self.do_one_iteration()
97
137 #---------------------------------------------------------------------------
98 #---------------------------------------------------------------------------
138 # Kernel request handlers
99 # Kernel request handlers
139 #---------------------------------------------------------------------------
100 #---------------------------------------------------------------------------
140
101
141 def execute_request(self, ident, parent):
102 def execute_request(self, ident, parent):
142 try:
103 try:
143 code = parent[u'content'][u'code']
104 code = parent[u'content'][u'code']
144 except:
105 except:
145 print>>sys.__stderr__, "Got bad msg: "
106 print>>sys.__stderr__, "Got bad msg: "
146 print>>sys.__stderr__, Message(parent)
107 print>>sys.__stderr__, Message(parent)
147 return
108 return
148 pyin_msg = self.session.msg(u'pyin',{u'code':code}, parent=parent)
109 pyin_msg = self.session.msg(u'pyin',{u'code':code}, parent=parent)
149 self.pub_socket.send_json(pyin_msg)
110 self.pub_socket.send_json(pyin_msg)
150
111
151 try:
112 try:
152 # Replace raw_input. Note that is not sufficient to replace
113 # Replace raw_input. Note that is not sufficient to replace
153 # raw_input in the user namespace.
114 # raw_input in the user namespace.
154 raw_input = lambda prompt='': self._raw_input(prompt, ident, parent)
115 raw_input = lambda prompt='': self._raw_input(prompt, ident, parent)
155 __builtin__.raw_input = raw_input
116 __builtin__.raw_input = raw_input
156
117
157 # Set the parent message of the display hook and out streams.
118 # Set the parent message of the display hook and out streams.
158 self.shell.displayhook.set_parent(parent)
119 self.shell.displayhook.set_parent(parent)
159 sys.stdout.set_parent(parent)
120 sys.stdout.set_parent(parent)
160 sys.stderr.set_parent(parent)
121 sys.stderr.set_parent(parent)
161
122
162 # FIXME: runlines calls the exception handler itself. We should
123 # FIXME: runlines calls the exception handler itself. We should
163 # clean this up.
124 # clean this up.
164 self.shell._reply_content = None
125 self.shell._reply_content = None
165 self.shell.runlines(code)
126 self.shell.runlines(code)
166 except:
127 except:
167 # FIXME: this code right now isn't being used yet by default,
128 # FIXME: this code right now isn't being used yet by default,
168 # because the runlines() call above directly fires off exception
129 # because the runlines() call above directly fires off exception
169 # reporting. This code, therefore, is only active in the scenario
130 # reporting. This code, therefore, is only active in the scenario
170 # where runlines itself has an unhandled exception. We need to
131 # where runlines itself has an unhandled exception. We need to
171 # uniformize this, for all exception construction to come from a
132 # uniformize this, for all exception construction to come from a
172 # single location in the codbase.
133 # single location in the codbase.
173 etype, evalue, tb = sys.exc_info()
134 etype, evalue, tb = sys.exc_info()
174 tb_list = traceback.format_exception(etype, evalue, tb)
135 tb_list = traceback.format_exception(etype, evalue, tb)
175 reply_content = self.shell._showtraceback(etype, evalue, tb_list)
136 reply_content = self.shell._showtraceback(etype, evalue, tb_list)
176 else:
137 else:
177 payload = self.shell.payload_manager.read_payload()
138 payload = self.shell.payload_manager.read_payload()
178 # Be agressive about clearing the payload because we don't want
139 # Be agressive about clearing the payload because we don't want
179 # it to sit in memory until the next execute_request comes in.
140 # it to sit in memory until the next execute_request comes in.
180 self.shell.payload_manager.clear_payload()
141 self.shell.payload_manager.clear_payload()
181 reply_content = { 'status' : 'ok', 'payload' : payload }
142 reply_content = { 'status' : 'ok', 'payload' : payload }
182
143
183 # Compute the prompt information
144 # Compute the prompt information
184 prompt_number = self.shell.displayhook.prompt_count
145 prompt_number = self.shell.displayhook.prompt_count
185 reply_content['prompt_number'] = prompt_number
146 reply_content['prompt_number'] = prompt_number
186 prompt_string = self.shell.displayhook.prompt1.peek_next_prompt()
147 prompt_string = self.shell.displayhook.prompt1.peek_next_prompt()
187 next_prompt = {'prompt_string' : prompt_string,
148 next_prompt = {'prompt_string' : prompt_string,
188 'prompt_number' : prompt_number+1,
149 'prompt_number' : prompt_number+1,
189 'input_sep' : self.shell.displayhook.input_sep}
150 'input_sep' : self.shell.displayhook.input_sep}
190 reply_content['next_prompt'] = next_prompt
151 reply_content['next_prompt'] = next_prompt
191
152
192 # TMP - fish exception info out of shell, possibly left there by
153 # TMP - fish exception info out of shell, possibly left there by
193 # runlines
154 # runlines
194 if self.shell._reply_content is not None:
155 if self.shell._reply_content is not None:
195 reply_content.update(self.shell._reply_content)
156 reply_content.update(self.shell._reply_content)
196
157
197 # Flush output before sending the reply.
158 # Flush output before sending the reply.
198 sys.stderr.flush()
159 sys.stderr.flush()
199 sys.stdout.flush()
160 sys.stdout.flush()
200
161
201 # Send the reply.
162 # Send the reply.
202 reply_msg = self.session.msg(u'execute_reply', reply_content, parent)
163 reply_msg = self.session.msg(u'execute_reply', reply_content, parent)
203 print>>sys.__stdout__, Message(reply_msg)
164 print>>sys.__stdout__, Message(reply_msg)
204 self.reply_socket.send(ident, zmq.SNDMORE)
165 self.reply_socket.send(ident, zmq.SNDMORE)
205 self.reply_socket.send_json(reply_msg)
166 self.reply_socket.send_json(reply_msg)
206 if reply_msg['content']['status'] == u'error':
167 if reply_msg['content']['status'] == u'error':
207 self._abort_queue()
168 self._abort_queue()
208
169
209 def complete_request(self, ident, parent):
170 def complete_request(self, ident, parent):
210 matches = {'matches' : self._complete(parent),
171 matches = {'matches' : self._complete(parent),
211 'status' : 'ok'}
172 'status' : 'ok'}
212 completion_msg = self.session.send(self.reply_socket, 'complete_reply',
173 completion_msg = self.session.send(self.reply_socket, 'complete_reply',
213 matches, parent, ident)
174 matches, parent, ident)
214 print >> sys.__stdout__, completion_msg
175 print >> sys.__stdout__, completion_msg
215
176
216 def object_info_request(self, ident, parent):
177 def object_info_request(self, ident, parent):
217 context = parent['content']['oname'].split('.')
178 context = parent['content']['oname'].split('.')
218 object_info = self._object_info(context)
179 object_info = self._object_info(context)
219 msg = self.session.send(self.reply_socket, 'object_info_reply',
180 msg = self.session.send(self.reply_socket, 'object_info_reply',
220 object_info, parent, ident)
181 object_info, parent, ident)
221 print >> sys.__stdout__, msg
182 print >> sys.__stdout__, msg
222
183
223 def prompt_request(self, ident, parent):
184 def prompt_request(self, ident, parent):
224 prompt_number = self.shell.displayhook.prompt_count
185 prompt_number = self.shell.displayhook.prompt_count
225 prompt_string = self.shell.displayhook.prompt1.peek_next_prompt()
186 prompt_string = self.shell.displayhook.prompt1.peek_next_prompt()
226 content = {'prompt_string' : prompt_string,
187 content = {'prompt_string' : prompt_string,
227 'prompt_number' : prompt_number+1,
188 'prompt_number' : prompt_number+1,
228 'input_sep' : self.shell.displayhook.input_sep}
189 'input_sep' : self.shell.displayhook.input_sep}
229 msg = self.session.send(self.reply_socket, 'prompt_reply',
190 msg = self.session.send(self.reply_socket, 'prompt_reply',
230 content, parent, ident)
191 content, parent, ident)
231 print >> sys.__stdout__, msg
192 print >> sys.__stdout__, msg
232
193
233 def history_request(self, ident, parent):
194 def history_request(self, ident, parent):
234 output = parent['content']['output']
195 output = parent['content']['output']
235 index = parent['content']['index']
196 index = parent['content']['index']
236 raw = parent['content']['raw']
197 raw = parent['content']['raw']
237 hist = self.shell.get_history(index=index, raw=raw, output=output)
198 hist = self.shell.get_history(index=index, raw=raw, output=output)
238 content = {'history' : hist}
199 content = {'history' : hist}
239 msg = self.session.send(self.reply_socket, 'history_reply',
200 msg = self.session.send(self.reply_socket, 'history_reply',
240 content, parent, ident)
201 content, parent, ident)
241 print >> sys.__stdout__, msg
202 print >> sys.__stdout__, msg
242
203
243 #---------------------------------------------------------------------------
204 #---------------------------------------------------------------------------
244 # Protected interface
205 # Protected interface
245 #---------------------------------------------------------------------------
206 #---------------------------------------------------------------------------
246
207
247 def _abort_queue(self):
208 def _abort_queue(self):
248 while True:
209 while True:
249 try:
210 try:
250 ident = self.reply_socket.recv(zmq.NOBLOCK)
211 ident = self.reply_socket.recv(zmq.NOBLOCK)
251 except zmq.ZMQError, e:
212 except zmq.ZMQError, e:
252 if e.errno == zmq.EAGAIN:
213 if e.errno == zmq.EAGAIN:
253 break
214 break
254 else:
215 else:
255 assert self.reply_socket.rcvmore(), "Unexpected missing message part."
216 assert self.reply_socket.rcvmore(), "Unexpected missing message part."
256 msg = self.reply_socket.recv_json()
217 msg = self.reply_socket.recv_json()
257 print>>sys.__stdout__, "Aborting:"
218 print>>sys.__stdout__, "Aborting:"
258 print>>sys.__stdout__, Message(msg)
219 print>>sys.__stdout__, Message(msg)
259 msg_type = msg['msg_type']
220 msg_type = msg['msg_type']
260 reply_type = msg_type.split('_')[0] + '_reply'
221 reply_type = msg_type.split('_')[0] + '_reply'
261 reply_msg = self.session.msg(reply_type, {'status' : 'aborted'}, msg)
222 reply_msg = self.session.msg(reply_type, {'status' : 'aborted'}, msg)
262 print>>sys.__stdout__, Message(reply_msg)
223 print>>sys.__stdout__, Message(reply_msg)
263 self.reply_socket.send(ident,zmq.SNDMORE)
224 self.reply_socket.send(ident,zmq.SNDMORE)
264 self.reply_socket.send_json(reply_msg)
225 self.reply_socket.send_json(reply_msg)
265 # We need to wait a bit for requests to come in. This can probably
226 # We need to wait a bit for requests to come in. This can probably
266 # be set shorter for true asynchronous clients.
227 # be set shorter for true asynchronous clients.
267 time.sleep(0.1)
228 time.sleep(0.1)
268
229
269 def _raw_input(self, prompt, ident, parent):
230 def _raw_input(self, prompt, ident, parent):
270 # Flush output before making the request.
231 # Flush output before making the request.
271 sys.stderr.flush()
232 sys.stderr.flush()
272 sys.stdout.flush()
233 sys.stdout.flush()
273
234
274 # Send the input request.
235 # Send the input request.
275 content = dict(prompt=prompt)
236 content = dict(prompt=prompt)
276 msg = self.session.msg(u'input_request', content, parent)
237 msg = self.session.msg(u'input_request', content, parent)
277 self.req_socket.send_json(msg)
238 self.req_socket.send_json(msg)
278
239
279 # Await a response.
240 # Await a response.
280 reply = self.req_socket.recv_json()
241 reply = self.req_socket.recv_json()
281 try:
242 try:
282 value = reply['content']['value']
243 value = reply['content']['value']
283 except:
244 except:
284 print>>sys.__stderr__, "Got bad raw_input reply: "
245 print>>sys.__stderr__, "Got bad raw_input reply: "
285 print>>sys.__stderr__, Message(parent)
246 print>>sys.__stderr__, Message(parent)
286 value = ''
247 value = ''
287 return value
248 return value
288
249
289 def _complete(self, msg):
250 def _complete(self, msg):
290 #from IPython.utils.io import rprint # dbg
251 #from IPython.utils.io import rprint # dbg
291 #rprint('\n\n**MSG**\n\n', msg) # dbg
252 #rprint('\n\n**MSG**\n\n', msg) # dbg
292 #import traceback; rprint(''.join(traceback.format_stack())) # dbg
253 #import traceback; rprint(''.join(traceback.format_stack())) # dbg
293 c = msg['content']
254 c = msg['content']
294 try:
255 try:
295 cpos = int(c['cursor_pos'])
256 cpos = int(c['cursor_pos'])
296 except:
257 except:
297 # If we don't get something that we can convert to an integer, at
258 # If we don't get something that we can convert to an integer, at
298 # leasat attempt the completion guessing the cursor is at the end
259 # leasat attempt the completion guessing the cursor is at the end
299 # of the text
260 # of the text
300 cpos = len(c['text'])
261 cpos = len(c['text'])
301 return self.shell.complete(c['text'], c['line'], cpos)
262 return self.shell.complete(c['text'], c['line'], cpos)
302
263
303 def _object_info(self, context):
264 def _object_info(self, context):
304 symbol, leftover = self._symbol_from_context(context)
265 symbol, leftover = self._symbol_from_context(context)
305 if symbol is not None and not leftover:
266 if symbol is not None and not leftover:
306 doc = getattr(symbol, '__doc__', '')
267 doc = getattr(symbol, '__doc__', '')
307 else:
268 else:
308 doc = ''
269 doc = ''
309 object_info = dict(docstring = doc)
270 object_info = dict(docstring = doc)
310 return object_info
271 return object_info
311
272
312 def _symbol_from_context(self, context):
273 def _symbol_from_context(self, context):
313 if not context:
274 if not context:
314 return None, context
275 return None, context
315
276
316 base_symbol_string = context[0]
277 base_symbol_string = context[0]
317 symbol = self.shell.user_ns.get(base_symbol_string, None)
278 symbol = self.shell.user_ns.get(base_symbol_string, None)
318 if symbol is None:
279 if symbol is None:
319 symbol = __builtin__.__dict__.get(base_symbol_string, None)
280 symbol = __builtin__.__dict__.get(base_symbol_string, None)
320 if symbol is None:
281 if symbol is None:
321 return None, context
282 return None, context
322
283
323 context = context[1:]
284 context = context[1:]
324 for i, name in enumerate(context):
285 for i, name in enumerate(context):
325 new_symbol = getattr(symbol, name, None)
286 new_symbol = getattr(symbol, name, None)
326 if new_symbol is None:
287 if new_symbol is None:
327 return symbol, context[i:]
288 return symbol, context[i:]
328 else:
289 else:
329 symbol = new_symbol
290 symbol = new_symbol
330
291
331 return symbol, []
292 return symbol, []
332
293
294
295 class QtKernel(Kernel):
296
297 def start(self):
298 """Start a kernel with QtPy4 event loop integration."""
299 from PyQt4 import QtGui, QtCore
300 self.qapp = app = QtGui.QApplication([])
301 self.qtimer = QtCore.QTimer()
302 self.qtimer.timeout.connect(self.do_one_iteration)
303 self.qtimer.start(50)
304 self.qapp.exec_()
305
306
333 #-----------------------------------------------------------------------------
307 #-----------------------------------------------------------------------------
334 # Kernel main and launch functions
308 # Kernel main and launch functions
335 #-----------------------------------------------------------------------------
309 #-----------------------------------------------------------------------------
336
310
337 def launch_kernel(xrep_port=0, pub_port=0, req_port=0, independent=False,
311 def launch_kernel(xrep_port=0, pub_port=0, req_port=0, independent=False,
338 pylab=False):
312 pylab=False):
339 """ Launches a localhost kernel, binding to the specified ports.
313 """ Launches a localhost kernel, binding to the specified ports.
340
314
341 Parameters
315 Parameters
342 ----------
316 ----------
343 xrep_port : int, optional
317 xrep_port : int, optional
344 The port to use for XREP channel.
318 The port to use for XREP channel.
345
319
346 pub_port : int, optional
320 pub_port : int, optional
347 The port to use for the SUB channel.
321 The port to use for the SUB channel.
348
322
349 req_port : int, optional
323 req_port : int, optional
350 The port to use for the REQ (raw input) channel.
324 The port to use for the REQ (raw input) channel.
351
325
352 independent : bool, optional (default False)
326 independent : bool, optional (default False)
353 If set, the kernel process is guaranteed to survive if this process
327 If set, the kernel process is guaranteed to survive if this process
354 dies. If not set, an effort is made to ensure that the kernel is killed
328 dies. If not set, an effort is made to ensure that the kernel is killed
355 when this process dies. Note that in this case it is still good practice
329 when this process dies. Note that in this case it is still good practice
356 to kill kernels manually before exiting.
330 to kill kernels manually before exiting.
357
331
358 pylab : bool or string, optional (default False)
332 pylab : bool or string, optional (default False)
359 If not False, the kernel will be launched with pylab enabled. If a
333 If not False, the kernel will be launched with pylab enabled. If a
360 string is passed, matplotlib will use the specified backend. Otherwise,
334 string is passed, matplotlib will use the specified backend. Otherwise,
361 matplotlib's default backend will be used.
335 matplotlib's default backend will be used.
362
336
363 Returns
337 Returns
364 -------
338 -------
365 A tuple of form:
339 A tuple of form:
366 (kernel_process, xrep_port, pub_port, req_port)
340 (kernel_process, xrep_port, pub_port, req_port)
367 where kernel_process is a Popen object and the ports are integers.
341 where kernel_process is a Popen object and the ports are integers.
368 """
342 """
369 extra_arguments = []
343 extra_arguments = []
370 if pylab:
344 if pylab:
371 extra_arguments.append('--pylab')
345 extra_arguments.append('--pylab')
372 if isinstance(pylab, basestring):
346 if isinstance(pylab, basestring):
373 extra_arguments.append(pylab)
347 extra_arguments.append(pylab)
374 return base_launch_kernel('from IPython.zmq.ipkernel import main; main()',
348 return base_launch_kernel('from IPython.zmq.ipkernel import main; main()',
375 xrep_port, pub_port, req_port, independent,
349 xrep_port, pub_port, req_port, independent,
376 extra_arguments)
350 extra_arguments)
377
351
378 def main():
352 def main():
379 """ The IPython kernel main entry point.
353 """ The IPython kernel main entry point.
380 """
354 """
381 parser = make_argument_parser()
355 parser = make_argument_parser()
382 parser.add_argument('--pylab', type=str, metavar='GUI', nargs='?',
356 parser.add_argument('--pylab', type=str, metavar='GUI', nargs='?',
383 const='auto', help = \
357 const='auto', help = \
384 "Pre-load matplotlib and numpy for interactive use. If GUI is not \
358 "Pre-load matplotlib and numpy for interactive use. If GUI is not \
385 given, the GUI backend is matplotlib's, otherwise use one of: \
359 given, the GUI backend is matplotlib's, otherwise use one of: \
386 ['tk', 'gtk', 'qt', 'wx', 'payload-svg'].")
360 ['tk', 'gtk', 'qt', 'wx', 'payload-svg'].")
387 namespace = parser.parse_args()
361 namespace = parser.parse_args()
388
362
389 kernel = make_kernel(namespace, Kernel, OutStream)
363 kernel_class = Kernel
364
365 _kernel_classes = {
366 'qt' : QtKernel,
367 'qt4' : QtKernel,
368 'payload-svg':Kernel
369 }
390 if namespace.pylab:
370 if namespace.pylab:
391 if namespace.pylab == 'auto':
371 if namespace.pylab == 'auto':
392 kernel.activate_pylab()
372 gui, backend = pylabtools.find_gui_and_backend()
393 else:
373 else:
394 kernel.activate_pylab(namespace.pylab)
374 gui, backend = pylabtools.find_gui_and_backend(namespace.pylab)
375 print gui, backend
376 kernel_class = _kernel_classes.get(gui)
377 if kernel_class is None:
378 raise ValueError('GUI is not supported: %r' % gui)
379 pylabtools.activate_matplotlib(backend)
380
381 print>>sys.__stdout__, kernel_class
382 kernel = make_kernel(namespace, kernel_class, OutStream)
383 print >>sys.__stdout__, kernel
384
385 if namespace.pylab:
386 pylabtools.import_pylab(kernel.shell.user_ns)
395
387
396 start_kernel(namespace, kernel)
388 start_kernel(namespace, kernel)
397
389
398 if __name__ == '__main__':
390 if __name__ == '__main__':
399 main()
391 main()
@@ -1,389 +1,389
1 import inspect
1 import inspect
2 import re
2 import re
3 import sys
3 import sys
4 from subprocess import Popen, PIPE
4 from subprocess import Popen, PIPE
5
5
6 from IPython.core.interactiveshell import (
6 from IPython.core.interactiveshell import (
7 InteractiveShell, InteractiveShellABC
7 InteractiveShell, InteractiveShellABC
8 )
8 )
9 from IPython.core.displayhook import DisplayHook
9 from IPython.core.displayhook import DisplayHook
10 from IPython.core.macro import Macro
10 from IPython.core.macro import Macro
11 from IPython.utils.io import rprint
11 from IPython.utils.io import rprint
12 from IPython.utils.path import get_py_filename
12 from IPython.utils.path import get_py_filename
13 from IPython.utils.text import StringTypes
13 from IPython.utils.text import StringTypes
14 from IPython.utils.traitlets import Instance, Type, Dict
14 from IPython.utils.traitlets import Instance, Type, Dict
15 from IPython.utils.warn import warn
15 from IPython.utils.warn import warn
16 from IPython.zmq.session import extract_header
16 from IPython.zmq.session import extract_header
17 from IPython.core.payloadpage import install_payload_page
17 from IPython.core.payloadpage import install_payload_page
18
18 from session import Session
19
19
20 # Install the payload version of page.
20 # Install the payload version of page.
21 install_payload_page()
21 install_payload_page()
22
22
23
23
24 class ZMQDisplayHook(DisplayHook):
24 class ZMQDisplayHook(DisplayHook):
25
25
26 session = Instance('IPython.zmq.session.Session')
26 session = Instance(Session)
27 pub_socket = Instance('zmq.Socket')
27 pub_socket = Instance('zmq.Socket')
28 parent_header = Dict({})
28 parent_header = Dict({})
29
29
30 def set_parent(self, parent):
30 def set_parent(self, parent):
31 """Set the parent for outbound messages."""
31 """Set the parent for outbound messages."""
32 self.parent_header = extract_header(parent)
32 self.parent_header = extract_header(parent)
33
33
34 def start_displayhook(self):
34 def start_displayhook(self):
35 self.msg = self.session.msg(u'pyout', {}, parent=self.parent_header)
35 self.msg = self.session.msg(u'pyout', {}, parent=self.parent_header)
36
36
37 def write_output_prompt(self):
37 def write_output_prompt(self):
38 """Write the output prompt."""
38 """Write the output prompt."""
39 if self.do_full_cache:
39 if self.do_full_cache:
40 self.msg['content']['output_sep'] = self.output_sep
40 self.msg['content']['output_sep'] = self.output_sep
41 self.msg['content']['prompt_string'] = str(self.prompt_out)
41 self.msg['content']['prompt_string'] = str(self.prompt_out)
42 self.msg['content']['prompt_number'] = self.prompt_count
42 self.msg['content']['prompt_number'] = self.prompt_count
43 self.msg['content']['output_sep2'] = self.output_sep2
43 self.msg['content']['output_sep2'] = self.output_sep2
44
44
45 def write_result_repr(self, result_repr):
45 def write_result_repr(self, result_repr):
46 self.msg['content']['data'] = result_repr
46 self.msg['content']['data'] = result_repr
47
47
48 def finish_displayhook(self):
48 def finish_displayhook(self):
49 """Finish up all displayhook activities."""
49 """Finish up all displayhook activities."""
50 self.pub_socket.send_json(self.msg)
50 self.pub_socket.send_json(self.msg)
51 self.msg = None
51 self.msg = None
52
52
53
53
54 class ZMQInteractiveShell(InteractiveShell):
54 class ZMQInteractiveShell(InteractiveShell):
55 """A subclass of InteractiveShell for ZMQ."""
55 """A subclass of InteractiveShell for ZMQ."""
56
56
57 displayhook_class = Type(ZMQDisplayHook)
57 displayhook_class = Type(ZMQDisplayHook)
58
58
59 def system(self, cmd):
59 def system(self, cmd):
60 cmd = self.var_expand(cmd, depth=2)
60 cmd = self.var_expand(cmd, depth=2)
61 sys.stdout.flush()
61 sys.stdout.flush()
62 sys.stderr.flush()
62 sys.stderr.flush()
63 p = Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE)
63 p = Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE)
64 for line in p.stdout.read().split('\n'):
64 for line in p.stdout.read().split('\n'):
65 if len(line) > 0:
65 if len(line) > 0:
66 print line
66 print line
67 for line in p.stderr.read().split('\n'):
67 for line in p.stderr.read().split('\n'):
68 if len(line) > 0:
68 if len(line) > 0:
69 print line
69 print line
70 p.wait()
70 p.wait()
71
71
72 def init_io(self):
72 def init_io(self):
73 # This will just use sys.stdout and sys.stderr. If you want to
73 # This will just use sys.stdout and sys.stderr. If you want to
74 # override sys.stdout and sys.stderr themselves, you need to do that
74 # override sys.stdout and sys.stderr themselves, you need to do that
75 # *before* instantiating this class, because Term holds onto
75 # *before* instantiating this class, because Term holds onto
76 # references to the underlying streams.
76 # references to the underlying streams.
77 import IPython.utils.io
77 import IPython.utils.io
78 Term = IPython.utils.io.IOTerm()
78 Term = IPython.utils.io.IOTerm()
79 IPython.utils.io.Term = Term
79 IPython.utils.io.Term = Term
80
80
81 def magic_edit(self,parameter_s='',last_call=['','']):
81 def magic_edit(self,parameter_s='',last_call=['','']):
82 """Bring up an editor and execute the resulting code.
82 """Bring up an editor and execute the resulting code.
83
83
84 Usage:
84 Usage:
85 %edit [options] [args]
85 %edit [options] [args]
86
86
87 %edit runs IPython's editor hook. The default version of this hook is
87 %edit runs IPython's editor hook. The default version of this hook is
88 set to call the __IPYTHON__.rc.editor command. This is read from your
88 set to call the __IPYTHON__.rc.editor command. This is read from your
89 environment variable $EDITOR. If this isn't found, it will default to
89 environment variable $EDITOR. If this isn't found, it will default to
90 vi under Linux/Unix and to notepad under Windows. See the end of this
90 vi under Linux/Unix and to notepad under Windows. See the end of this
91 docstring for how to change the editor hook.
91 docstring for how to change the editor hook.
92
92
93 You can also set the value of this editor via the command line option
93 You can also set the value of this editor via the command line option
94 '-editor' or in your ipythonrc file. This is useful if you wish to use
94 '-editor' or in your ipythonrc file. This is useful if you wish to use
95 specifically for IPython an editor different from your typical default
95 specifically for IPython an editor different from your typical default
96 (and for Windows users who typically don't set environment variables).
96 (and for Windows users who typically don't set environment variables).
97
97
98 This command allows you to conveniently edit multi-line code right in
98 This command allows you to conveniently edit multi-line code right in
99 your IPython session.
99 your IPython session.
100
100
101 If called without arguments, %edit opens up an empty editor with a
101 If called without arguments, %edit opens up an empty editor with a
102 temporary file and will execute the contents of this file when you
102 temporary file and will execute the contents of this file when you
103 close it (don't forget to save it!).
103 close it (don't forget to save it!).
104
104
105
105
106 Options:
106 Options:
107
107
108 -n <number>: open the editor at a specified line number. By default,
108 -n <number>: open the editor at a specified line number. By default,
109 the IPython editor hook uses the unix syntax 'editor +N filename', but
109 the IPython editor hook uses the unix syntax 'editor +N filename', but
110 you can configure this by providing your own modified hook if your
110 you can configure this by providing your own modified hook if your
111 favorite editor supports line-number specifications with a different
111 favorite editor supports line-number specifications with a different
112 syntax.
112 syntax.
113
113
114 -p: this will call the editor with the same data as the previous time
114 -p: this will call the editor with the same data as the previous time
115 it was used, regardless of how long ago (in your current session) it
115 it was used, regardless of how long ago (in your current session) it
116 was.
116 was.
117
117
118 -r: use 'raw' input. This option only applies to input taken from the
118 -r: use 'raw' input. This option only applies to input taken from the
119 user's history. By default, the 'processed' history is used, so that
119 user's history. By default, the 'processed' history is used, so that
120 magics are loaded in their transformed version to valid Python. If
120 magics are loaded in their transformed version to valid Python. If
121 this option is given, the raw input as typed as the command line is
121 this option is given, the raw input as typed as the command line is
122 used instead. When you exit the editor, it will be executed by
122 used instead. When you exit the editor, it will be executed by
123 IPython's own processor.
123 IPython's own processor.
124
124
125 -x: do not execute the edited code immediately upon exit. This is
125 -x: do not execute the edited code immediately upon exit. This is
126 mainly useful if you are editing programs which need to be called with
126 mainly useful if you are editing programs which need to be called with
127 command line arguments, which you can then do using %run.
127 command line arguments, which you can then do using %run.
128
128
129
129
130 Arguments:
130 Arguments:
131
131
132 If arguments are given, the following possibilites exist:
132 If arguments are given, the following possibilites exist:
133
133
134 - The arguments are numbers or pairs of colon-separated numbers (like
134 - The arguments are numbers or pairs of colon-separated numbers (like
135 1 4:8 9). These are interpreted as lines of previous input to be
135 1 4:8 9). These are interpreted as lines of previous input to be
136 loaded into the editor. The syntax is the same of the %macro command.
136 loaded into the editor. The syntax is the same of the %macro command.
137
137
138 - If the argument doesn't start with a number, it is evaluated as a
138 - If the argument doesn't start with a number, it is evaluated as a
139 variable and its contents loaded into the editor. You can thus edit
139 variable and its contents loaded into the editor. You can thus edit
140 any string which contains python code (including the result of
140 any string which contains python code (including the result of
141 previous edits).
141 previous edits).
142
142
143 - If the argument is the name of an object (other than a string),
143 - If the argument is the name of an object (other than a string),
144 IPython will try to locate the file where it was defined and open the
144 IPython will try to locate the file where it was defined and open the
145 editor at the point where it is defined. You can use `%edit function`
145 editor at the point where it is defined. You can use `%edit function`
146 to load an editor exactly at the point where 'function' is defined,
146 to load an editor exactly at the point where 'function' is defined,
147 edit it and have the file be executed automatically.
147 edit it and have the file be executed automatically.
148
148
149 If the object is a macro (see %macro for details), this opens up your
149 If the object is a macro (see %macro for details), this opens up your
150 specified editor with a temporary file containing the macro's data.
150 specified editor with a temporary file containing the macro's data.
151 Upon exit, the macro is reloaded with the contents of the file.
151 Upon exit, the macro is reloaded with the contents of the file.
152
152
153 Note: opening at an exact line is only supported under Unix, and some
153 Note: opening at an exact line is only supported under Unix, and some
154 editors (like kedit and gedit up to Gnome 2.8) do not understand the
154 editors (like kedit and gedit up to Gnome 2.8) do not understand the
155 '+NUMBER' parameter necessary for this feature. Good editors like
155 '+NUMBER' parameter necessary for this feature. Good editors like
156 (X)Emacs, vi, jed, pico and joe all do.
156 (X)Emacs, vi, jed, pico and joe all do.
157
157
158 - If the argument is not found as a variable, IPython will look for a
158 - If the argument is not found as a variable, IPython will look for a
159 file with that name (adding .py if necessary) and load it into the
159 file with that name (adding .py if necessary) and load it into the
160 editor. It will execute its contents with execfile() when you exit,
160 editor. It will execute its contents with execfile() when you exit,
161 loading any code in the file into your interactive namespace.
161 loading any code in the file into your interactive namespace.
162
162
163 After executing your code, %edit will return as output the code you
163 After executing your code, %edit will return as output the code you
164 typed in the editor (except when it was an existing file). This way
164 typed in the editor (except when it was an existing file). This way
165 you can reload the code in further invocations of %edit as a variable,
165 you can reload the code in further invocations of %edit as a variable,
166 via _<NUMBER> or Out[<NUMBER>], where <NUMBER> is the prompt number of
166 via _<NUMBER> or Out[<NUMBER>], where <NUMBER> is the prompt number of
167 the output.
167 the output.
168
168
169 Note that %edit is also available through the alias %ed.
169 Note that %edit is also available through the alias %ed.
170
170
171 This is an example of creating a simple function inside the editor and
171 This is an example of creating a simple function inside the editor and
172 then modifying it. First, start up the editor:
172 then modifying it. First, start up the editor:
173
173
174 In [1]: ed
174 In [1]: ed
175 Editing... done. Executing edited code...
175 Editing... done. Executing edited code...
176 Out[1]: 'def foo():n print "foo() was defined in an editing session"n'
176 Out[1]: 'def foo():n print "foo() was defined in an editing session"n'
177
177
178 We can then call the function foo():
178 We can then call the function foo():
179
179
180 In [2]: foo()
180 In [2]: foo()
181 foo() was defined in an editing session
181 foo() was defined in an editing session
182
182
183 Now we edit foo. IPython automatically loads the editor with the
183 Now we edit foo. IPython automatically loads the editor with the
184 (temporary) file where foo() was previously defined:
184 (temporary) file where foo() was previously defined:
185
185
186 In [3]: ed foo
186 In [3]: ed foo
187 Editing... done. Executing edited code...
187 Editing... done. Executing edited code...
188
188
189 And if we call foo() again we get the modified version:
189 And if we call foo() again we get the modified version:
190
190
191 In [4]: foo()
191 In [4]: foo()
192 foo() has now been changed!
192 foo() has now been changed!
193
193
194 Here is an example of how to edit a code snippet successive
194 Here is an example of how to edit a code snippet successive
195 times. First we call the editor:
195 times. First we call the editor:
196
196
197 In [5]: ed
197 In [5]: ed
198 Editing... done. Executing edited code...
198 Editing... done. Executing edited code...
199 hello
199 hello
200 Out[5]: "print 'hello'n"
200 Out[5]: "print 'hello'n"
201
201
202 Now we call it again with the previous output (stored in _):
202 Now we call it again with the previous output (stored in _):
203
203
204 In [6]: ed _
204 In [6]: ed _
205 Editing... done. Executing edited code...
205 Editing... done. Executing edited code...
206 hello world
206 hello world
207 Out[6]: "print 'hello world'n"
207 Out[6]: "print 'hello world'n"
208
208
209 Now we call it with the output #8 (stored in _8, also as Out[8]):
209 Now we call it with the output #8 (stored in _8, also as Out[8]):
210
210
211 In [7]: ed _8
211 In [7]: ed _8
212 Editing... done. Executing edited code...
212 Editing... done. Executing edited code...
213 hello again
213 hello again
214 Out[7]: "print 'hello again'n"
214 Out[7]: "print 'hello again'n"
215
215
216
216
217 Changing the default editor hook:
217 Changing the default editor hook:
218
218
219 If you wish to write your own editor hook, you can put it in a
219 If you wish to write your own editor hook, you can put it in a
220 configuration file which you load at startup time. The default hook
220 configuration file which you load at startup time. The default hook
221 is defined in the IPython.core.hooks module, and you can use that as a
221 is defined in the IPython.core.hooks module, and you can use that as a
222 starting example for further modifications. That file also has
222 starting example for further modifications. That file also has
223 general instructions on how to set a new hook for use once you've
223 general instructions on how to set a new hook for use once you've
224 defined it."""
224 defined it."""
225
225
226 # FIXME: This function has become a convoluted mess. It needs a
226 # FIXME: This function has become a convoluted mess. It needs a
227 # ground-up rewrite with clean, simple logic.
227 # ground-up rewrite with clean, simple logic.
228
228
229 def make_filename(arg):
229 def make_filename(arg):
230 "Make a filename from the given args"
230 "Make a filename from the given args"
231 try:
231 try:
232 filename = get_py_filename(arg)
232 filename = get_py_filename(arg)
233 except IOError:
233 except IOError:
234 if args.endswith('.py'):
234 if args.endswith('.py'):
235 filename = arg
235 filename = arg
236 else:
236 else:
237 filename = None
237 filename = None
238 return filename
238 return filename
239
239
240 # custom exceptions
240 # custom exceptions
241 class DataIsObject(Exception): pass
241 class DataIsObject(Exception): pass
242
242
243 opts,args = self.parse_options(parameter_s,'prn:')
243 opts,args = self.parse_options(parameter_s,'prn:')
244 # Set a few locals from the options for convenience:
244 # Set a few locals from the options for convenience:
245 opts_p = opts.has_key('p')
245 opts_p = opts.has_key('p')
246 opts_r = opts.has_key('r')
246 opts_r = opts.has_key('r')
247
247
248 # Default line number value
248 # Default line number value
249 lineno = opts.get('n',None)
249 lineno = opts.get('n',None)
250 if lineno is not None:
250 if lineno is not None:
251 try:
251 try:
252 lineno = int(lineno)
252 lineno = int(lineno)
253 except:
253 except:
254 warn("The -n argument must be an integer.")
254 warn("The -n argument must be an integer.")
255 return
255 return
256
256
257 if opts_p:
257 if opts_p:
258 args = '_%s' % last_call[0]
258 args = '_%s' % last_call[0]
259 if not self.shell.user_ns.has_key(args):
259 if not self.shell.user_ns.has_key(args):
260 args = last_call[1]
260 args = last_call[1]
261
261
262 # use last_call to remember the state of the previous call, but don't
262 # use last_call to remember the state of the previous call, but don't
263 # let it be clobbered by successive '-p' calls.
263 # let it be clobbered by successive '-p' calls.
264 try:
264 try:
265 last_call[0] = self.shell.displayhook.prompt_count
265 last_call[0] = self.shell.displayhook.prompt_count
266 if not opts_p:
266 if not opts_p:
267 last_call[1] = parameter_s
267 last_call[1] = parameter_s
268 except:
268 except:
269 pass
269 pass
270
270
271 # by default this is done with temp files, except when the given
271 # by default this is done with temp files, except when the given
272 # arg is a filename
272 # arg is a filename
273 use_temp = 1
273 use_temp = 1
274
274
275 if re.match(r'\d',args):
275 if re.match(r'\d',args):
276 # Mode where user specifies ranges of lines, like in %macro.
276 # Mode where user specifies ranges of lines, like in %macro.
277 # This means that you can't edit files whose names begin with
277 # This means that you can't edit files whose names begin with
278 # numbers this way. Tough.
278 # numbers this way. Tough.
279 ranges = args.split()
279 ranges = args.split()
280 data = ''.join(self.extract_input_slices(ranges,opts_r))
280 data = ''.join(self.extract_input_slices(ranges,opts_r))
281 elif args.endswith('.py'):
281 elif args.endswith('.py'):
282 filename = make_filename(args)
282 filename = make_filename(args)
283 data = ''
283 data = ''
284 use_temp = 0
284 use_temp = 0
285 elif args:
285 elif args:
286 try:
286 try:
287 # Load the parameter given as a variable. If not a string,
287 # Load the parameter given as a variable. If not a string,
288 # process it as an object instead (below)
288 # process it as an object instead (below)
289
289
290 #print '*** args',args,'type',type(args) # dbg
290 #print '*** args',args,'type',type(args) # dbg
291 data = eval(args,self.shell.user_ns)
291 data = eval(args,self.shell.user_ns)
292 if not type(data) in StringTypes:
292 if not type(data) in StringTypes:
293 raise DataIsObject
293 raise DataIsObject
294
294
295 except (NameError,SyntaxError):
295 except (NameError,SyntaxError):
296 # given argument is not a variable, try as a filename
296 # given argument is not a variable, try as a filename
297 filename = make_filename(args)
297 filename = make_filename(args)
298 if filename is None:
298 if filename is None:
299 warn("Argument given (%s) can't be found as a variable "
299 warn("Argument given (%s) can't be found as a variable "
300 "or as a filename." % args)
300 "or as a filename." % args)
301 return
301 return
302
302
303 data = ''
303 data = ''
304 use_temp = 0
304 use_temp = 0
305 except DataIsObject:
305 except DataIsObject:
306
306
307 # macros have a special edit function
307 # macros have a special edit function
308 if isinstance(data,Macro):
308 if isinstance(data,Macro):
309 self._edit_macro(args,data)
309 self._edit_macro(args,data)
310 return
310 return
311
311
312 # For objects, try to edit the file where they are defined
312 # For objects, try to edit the file where they are defined
313 try:
313 try:
314 filename = inspect.getabsfile(data)
314 filename = inspect.getabsfile(data)
315 if 'fakemodule' in filename.lower() and inspect.isclass(data):
315 if 'fakemodule' in filename.lower() and inspect.isclass(data):
316 # class created by %edit? Try to find source
316 # class created by %edit? Try to find source
317 # by looking for method definitions instead, the
317 # by looking for method definitions instead, the
318 # __module__ in those classes is FakeModule.
318 # __module__ in those classes is FakeModule.
319 attrs = [getattr(data, aname) for aname in dir(data)]
319 attrs = [getattr(data, aname) for aname in dir(data)]
320 for attr in attrs:
320 for attr in attrs:
321 if not inspect.ismethod(attr):
321 if not inspect.ismethod(attr):
322 continue
322 continue
323 filename = inspect.getabsfile(attr)
323 filename = inspect.getabsfile(attr)
324 if filename and 'fakemodule' not in filename.lower():
324 if filename and 'fakemodule' not in filename.lower():
325 # change the attribute to be the edit target instead
325 # change the attribute to be the edit target instead
326 data = attr
326 data = attr
327 break
327 break
328
328
329 datafile = 1
329 datafile = 1
330 except TypeError:
330 except TypeError:
331 filename = make_filename(args)
331 filename = make_filename(args)
332 datafile = 1
332 datafile = 1
333 warn('Could not find file where `%s` is defined.\n'
333 warn('Could not find file where `%s` is defined.\n'
334 'Opening a file named `%s`' % (args,filename))
334 'Opening a file named `%s`' % (args,filename))
335 # Now, make sure we can actually read the source (if it was in
335 # Now, make sure we can actually read the source (if it was in
336 # a temp file it's gone by now).
336 # a temp file it's gone by now).
337 if datafile:
337 if datafile:
338 try:
338 try:
339 if lineno is None:
339 if lineno is None:
340 lineno = inspect.getsourcelines(data)[1]
340 lineno = inspect.getsourcelines(data)[1]
341 except IOError:
341 except IOError:
342 filename = make_filename(args)
342 filename = make_filename(args)
343 if filename is None:
343 if filename is None:
344 warn('The file `%s` where `%s` was defined cannot '
344 warn('The file `%s` where `%s` was defined cannot '
345 'be read.' % (filename,data))
345 'be read.' % (filename,data))
346 return
346 return
347 use_temp = 0
347 use_temp = 0
348 else:
348 else:
349 data = ''
349 data = ''
350
350
351 if use_temp:
351 if use_temp:
352 filename = self.shell.mktempfile(data)
352 filename = self.shell.mktempfile(data)
353 print 'IPython will make a temporary file named:',filename
353 print 'IPython will make a temporary file named:',filename
354
354
355 payload = {
355 payload = {
356 'source' : 'IPython.zmq.zmqshell.ZMQInteractiveShell.edit_magic',
356 'source' : 'IPython.zmq.zmqshell.ZMQInteractiveShell.edit_magic',
357 'filename' : filename,
357 'filename' : filename,
358 'line_number' : lineno
358 'line_number' : lineno
359 }
359 }
360 self.payload_manager.write_payload(payload)
360 self.payload_manager.write_payload(payload)
361
361
362
362
363 def _showtraceback(self, etype, evalue, stb):
363 def _showtraceback(self, etype, evalue, stb):
364
364
365 exc_content = {
365 exc_content = {
366 u'status' : u'error',
366 u'status' : u'error',
367 u'traceback' : stb,
367 u'traceback' : stb,
368 u'ename' : unicode(etype.__name__),
368 u'ename' : unicode(etype.__name__),
369 u'evalue' : unicode(evalue)
369 u'evalue' : unicode(evalue)
370 }
370 }
371
371
372 dh = self.displayhook
372 dh = self.displayhook
373 exc_msg = dh.session.msg(u'pyerr', exc_content, dh.parent_header)
373 exc_msg = dh.session.msg(u'pyerr', exc_content, dh.parent_header)
374 # Send exception info over pub socket for other clients than the caller
374 # Send exception info over pub socket for other clients than the caller
375 # to pick up
375 # to pick up
376 dh.pub_socket.send_json(exc_msg)
376 dh.pub_socket.send_json(exc_msg)
377
377
378 # FIXME - Hack: store exception info in shell object. Right now, the
378 # FIXME - Hack: store exception info in shell object. Right now, the
379 # caller is reading this info after the fact, we need to fix this logic
379 # caller is reading this info after the fact, we need to fix this logic
380 # to remove this hack.
380 # to remove this hack.
381 self._reply_content = exc_content
381 self._reply_content = exc_content
382 # /FIXME
382 # /FIXME
383
383
384 return exc_content
384 return exc_content
385
385
386 def runlines(self, lines, clean=False):
386 def runlines(self, lines, clean=False):
387 return InteractiveShell.runlines(self, lines, clean)
387 return InteractiveShell.runlines(self, lines, clean)
388
388
389 InteractiveShellABC.register(ZMQInteractiveShell)
389 InteractiveShellABC.register(ZMQInteractiveShell)
General Comments 0
You need to be logged in to leave comments. Login now