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