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