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