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