Show More
@@ -1,3032 +1,3018 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 | """Main IPython class.""" |
|
2 | """Main IPython class.""" | |
3 |
|
3 | |||
4 | #----------------------------------------------------------------------------- |
|
4 | #----------------------------------------------------------------------------- | |
5 | # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> |
|
5 | # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> | |
6 | # Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu> |
|
6 | # Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu> | |
7 | # Copyright (C) 2008-2011 The IPython Development Team |
|
7 | # Copyright (C) 2008-2011 The IPython Development Team | |
8 | # |
|
8 | # | |
9 | # Distributed under the terms of the BSD License. The full license is in |
|
9 | # Distributed under the terms of the BSD License. The full license is in | |
10 | # the file COPYING, distributed as part of this software. |
|
10 | # the file COPYING, distributed as part of this software. | |
11 | #----------------------------------------------------------------------------- |
|
11 | #----------------------------------------------------------------------------- | |
12 |
|
12 | |||
13 | #----------------------------------------------------------------------------- |
|
13 | #----------------------------------------------------------------------------- | |
14 | # Imports |
|
14 | # Imports | |
15 | #----------------------------------------------------------------------------- |
|
15 | #----------------------------------------------------------------------------- | |
16 |
|
16 | |||
17 | from __future__ import with_statement |
|
17 | from __future__ import with_statement | |
18 | from __future__ import absolute_import |
|
18 | from __future__ import absolute_import | |
19 | from __future__ import print_function |
|
19 | from __future__ import print_function | |
20 |
|
20 | |||
21 | import __builtin__ as builtin_mod |
|
21 | import __builtin__ as builtin_mod | |
22 | import __future__ |
|
22 | import __future__ | |
23 | import abc |
|
23 | import abc | |
24 | import ast |
|
24 | import ast | |
25 | import atexit |
|
25 | import atexit | |
26 | import os |
|
26 | import os | |
27 | import re |
|
27 | import re | |
28 | import runpy |
|
28 | import runpy | |
29 | import sys |
|
29 | import sys | |
30 | import tempfile |
|
30 | import tempfile | |
31 | import types |
|
31 | import types | |
32 |
|
32 | |||
33 | # We need to use nested to support python 2.6, once we move to >=2.7, we can |
|
33 | # We need to use nested to support python 2.6, once we move to >=2.7, we can | |
34 | # use the with keyword's new builtin support for nested managers |
|
34 | # use the with keyword's new builtin support for nested managers | |
35 | try: |
|
35 | try: | |
36 | from contextlib import nested |
|
36 | from contextlib import nested | |
37 | except: |
|
37 | except: | |
38 | from IPython.utils.nested_context import nested |
|
38 | from IPython.utils.nested_context import nested | |
39 |
|
39 | |||
40 | from IPython.config.configurable import SingletonConfigurable |
|
40 | from IPython.config.configurable import SingletonConfigurable | |
41 | from IPython.core import debugger, oinspect |
|
41 | from IPython.core import debugger, oinspect | |
42 | from IPython.core import history as ipcorehist |
|
42 | from IPython.core import history as ipcorehist | |
43 | from IPython.core import magic |
|
43 | from IPython.core import magic | |
44 | from IPython.core import page |
|
44 | from IPython.core import page | |
45 | from IPython.core import prefilter |
|
45 | from IPython.core import prefilter | |
46 | from IPython.core import shadowns |
|
46 | from IPython.core import shadowns | |
47 | from IPython.core import ultratb |
|
47 | from IPython.core import ultratb | |
48 | from IPython.core.alias import AliasManager, AliasError |
|
48 | from IPython.core.alias import AliasManager, AliasError | |
49 | from IPython.core.autocall import ExitAutocall |
|
49 | from IPython.core.autocall import ExitAutocall | |
50 | from IPython.core.builtin_trap import BuiltinTrap |
|
50 | from IPython.core.builtin_trap import BuiltinTrap | |
51 | from IPython.core.compilerop import CachingCompiler |
|
51 | from IPython.core.compilerop import CachingCompiler | |
52 | from IPython.core.display_trap import DisplayTrap |
|
52 | from IPython.core.display_trap import DisplayTrap | |
53 | from IPython.core.displayhook import DisplayHook |
|
53 | from IPython.core.displayhook import DisplayHook | |
54 | from IPython.core.displaypub import DisplayPublisher |
|
54 | from IPython.core.displaypub import DisplayPublisher | |
55 | from IPython.core.error import UsageError |
|
55 | from IPython.core.error import UsageError | |
56 | from IPython.core.extensions import ExtensionManager |
|
56 | from IPython.core.extensions import ExtensionManager | |
57 | from IPython.core.fakemodule import FakeModule, init_fakemod_dict |
|
57 | from IPython.core.fakemodule import FakeModule, init_fakemod_dict | |
58 | from IPython.core.formatters import DisplayFormatter |
|
58 | from IPython.core.formatters import DisplayFormatter | |
59 | from IPython.core.history import HistoryManager |
|
59 | from IPython.core.history import HistoryManager | |
60 | from IPython.core.inputsplitter import IPythonInputSplitter, ESC_MAGIC, ESC_MAGIC2 |
|
60 | from IPython.core.inputsplitter import IPythonInputSplitter, ESC_MAGIC, ESC_MAGIC2 | |
61 | from IPython.core.logger import Logger |
|
61 | from IPython.core.logger import Logger | |
62 | from IPython.core.macro import Macro |
|
62 | from IPython.core.macro import Macro | |
63 | from IPython.core.payload import PayloadManager |
|
63 | from IPython.core.payload import PayloadManager | |
64 | from IPython.core.plugin import PluginManager |
|
64 | from IPython.core.plugin import PluginManager | |
65 | from IPython.core.prefilter import PrefilterManager |
|
65 | from IPython.core.prefilter import PrefilterManager | |
66 | from IPython.core.profiledir import ProfileDir |
|
66 | from IPython.core.profiledir import ProfileDir | |
67 | from IPython.core.pylabtools import pylab_activate |
|
67 | from IPython.core.pylabtools import pylab_activate | |
68 | from IPython.core.prompts import PromptManager |
|
68 | from IPython.core.prompts import PromptManager | |
69 | from IPython.lib.latextools import LaTeXTool |
|
69 | from IPython.lib.latextools import LaTeXTool | |
70 | from IPython.utils import PyColorize |
|
70 | from IPython.utils import PyColorize | |
71 | from IPython.utils import io |
|
71 | from IPython.utils import io | |
72 | from IPython.utils import py3compat |
|
72 | from IPython.utils import py3compat | |
73 | from IPython.utils import openpy |
|
73 | from IPython.utils import openpy | |
74 | from IPython.utils.doctestreload import doctest_reload |
|
74 | from IPython.utils.doctestreload import doctest_reload | |
75 | from IPython.utils.io import ask_yes_no |
|
75 | from IPython.utils.io import ask_yes_no | |
76 | from IPython.utils.ipstruct import Struct |
|
76 | from IPython.utils.ipstruct import Struct | |
77 | from IPython.utils.path import get_home_dir, get_ipython_dir, get_py_filename, unquote_filename |
|
77 | from IPython.utils.path import get_home_dir, get_ipython_dir, get_py_filename, unquote_filename | |
78 | from IPython.utils.pickleshare import PickleShareDB |
|
78 | from IPython.utils.pickleshare import PickleShareDB | |
79 | from IPython.utils.process import system, getoutput |
|
79 | from IPython.utils.process import system, getoutput | |
80 | from IPython.utils.strdispatch import StrDispatch |
|
80 | from IPython.utils.strdispatch import StrDispatch | |
81 | from IPython.utils.syspathcontext import prepended_to_syspath |
|
81 | from IPython.utils.syspathcontext import prepended_to_syspath | |
82 | from IPython.utils.text import (format_screen, LSString, SList, |
|
82 | from IPython.utils.text import (format_screen, LSString, SList, | |
83 | DollarFormatter) |
|
83 | DollarFormatter) | |
84 | from IPython.utils.traitlets import (Integer, CBool, CaselessStrEnum, Enum, |
|
84 | from IPython.utils.traitlets import (Integer, CBool, CaselessStrEnum, Enum, | |
85 | List, Unicode, Instance, Type) |
|
85 | List, Unicode, Instance, Type) | |
86 | from IPython.utils.warn import warn, error |
|
86 | from IPython.utils.warn import warn, error | |
87 | import IPython.core.hooks |
|
87 | import IPython.core.hooks | |
88 |
|
88 | |||
89 | # FIXME: do this in a function to avoid circular dependencies |
|
|||
90 | # A better solution is to remove IPython.parallel.error, |
|
|||
91 | # and place those classes in IPython.core.error. |
|
|||
92 |
|
||||
93 | class RemoteError(Exception): |
|
|||
94 | pass |
|
|||
95 |
|
||||
96 | def _import_remote_error(): |
|
|||
97 | global RemoteError |
|
|||
98 | try: |
|
|||
99 | from IPython.parallel.error import RemoteError |
|
|||
100 | except: |
|
|||
101 | pass |
|
|||
102 |
|
||||
103 | _import_remote_error() |
|
|||
104 |
|
||||
105 | #----------------------------------------------------------------------------- |
|
89 | #----------------------------------------------------------------------------- | |
106 | # Globals |
|
90 | # Globals | |
107 | #----------------------------------------------------------------------------- |
|
91 | #----------------------------------------------------------------------------- | |
108 |
|
92 | |||
109 | # compiled regexps for autoindent management |
|
93 | # compiled regexps for autoindent management | |
110 | dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass') |
|
94 | dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass') | |
111 |
|
95 | |||
112 | #----------------------------------------------------------------------------- |
|
96 | #----------------------------------------------------------------------------- | |
113 | # Utilities |
|
97 | # Utilities | |
114 | #----------------------------------------------------------------------------- |
|
98 | #----------------------------------------------------------------------------- | |
115 |
|
99 | |||
116 | def softspace(file, newvalue): |
|
100 | def softspace(file, newvalue): | |
117 | """Copied from code.py, to remove the dependency""" |
|
101 | """Copied from code.py, to remove the dependency""" | |
118 |
|
102 | |||
119 | oldvalue = 0 |
|
103 | oldvalue = 0 | |
120 | try: |
|
104 | try: | |
121 | oldvalue = file.softspace |
|
105 | oldvalue = file.softspace | |
122 | except AttributeError: |
|
106 | except AttributeError: | |
123 | pass |
|
107 | pass | |
124 | try: |
|
108 | try: | |
125 | file.softspace = newvalue |
|
109 | file.softspace = newvalue | |
126 | except (AttributeError, TypeError): |
|
110 | except (AttributeError, TypeError): | |
127 | # "attribute-less object" or "read-only attributes" |
|
111 | # "attribute-less object" or "read-only attributes" | |
128 | pass |
|
112 | pass | |
129 | return oldvalue |
|
113 | return oldvalue | |
130 |
|
114 | |||
131 |
|
115 | |||
132 | def no_op(*a, **kw): pass |
|
116 | def no_op(*a, **kw): pass | |
133 |
|
117 | |||
134 | class NoOpContext(object): |
|
118 | class NoOpContext(object): | |
135 | def __enter__(self): pass |
|
119 | def __enter__(self): pass | |
136 | def __exit__(self, type, value, traceback): pass |
|
120 | def __exit__(self, type, value, traceback): pass | |
137 | no_op_context = NoOpContext() |
|
121 | no_op_context = NoOpContext() | |
138 |
|
122 | |||
139 | class SpaceInInput(Exception): pass |
|
123 | class SpaceInInput(Exception): pass | |
140 |
|
124 | |||
141 | class Bunch: pass |
|
125 | class Bunch: pass | |
142 |
|
126 | |||
143 |
|
127 | |||
144 | def get_default_colors(): |
|
128 | def get_default_colors(): | |
145 | if sys.platform=='darwin': |
|
129 | if sys.platform=='darwin': | |
146 | return "LightBG" |
|
130 | return "LightBG" | |
147 | elif os.name=='nt': |
|
131 | elif os.name=='nt': | |
148 | return 'Linux' |
|
132 | return 'Linux' | |
149 | else: |
|
133 | else: | |
150 | return 'Linux' |
|
134 | return 'Linux' | |
151 |
|
135 | |||
152 |
|
136 | |||
153 | class SeparateUnicode(Unicode): |
|
137 | class SeparateUnicode(Unicode): | |
154 | """A Unicode subclass to validate separate_in, separate_out, etc. |
|
138 | """A Unicode subclass to validate separate_in, separate_out, etc. | |
155 |
|
139 | |||
156 | This is a Unicode based trait that converts '0'->'' and '\\n'->'\n'. |
|
140 | This is a Unicode based trait that converts '0'->'' and '\\n'->'\n'. | |
157 | """ |
|
141 | """ | |
158 |
|
142 | |||
159 | def validate(self, obj, value): |
|
143 | def validate(self, obj, value): | |
160 | if value == '0': value = '' |
|
144 | if value == '0': value = '' | |
161 | value = value.replace('\\n','\n') |
|
145 | value = value.replace('\\n','\n') | |
162 | return super(SeparateUnicode, self).validate(obj, value) |
|
146 | return super(SeparateUnicode, self).validate(obj, value) | |
163 |
|
147 | |||
164 |
|
148 | |||
165 | class ReadlineNoRecord(object): |
|
149 | class ReadlineNoRecord(object): | |
166 | """Context manager to execute some code, then reload readline history |
|
150 | """Context manager to execute some code, then reload readline history | |
167 | so that interactive input to the code doesn't appear when pressing up.""" |
|
151 | so that interactive input to the code doesn't appear when pressing up.""" | |
168 | def __init__(self, shell): |
|
152 | def __init__(self, shell): | |
169 | self.shell = shell |
|
153 | self.shell = shell | |
170 | self._nested_level = 0 |
|
154 | self._nested_level = 0 | |
171 |
|
155 | |||
172 | def __enter__(self): |
|
156 | def __enter__(self): | |
173 | if self._nested_level == 0: |
|
157 | if self._nested_level == 0: | |
174 | try: |
|
158 | try: | |
175 | self.orig_length = self.current_length() |
|
159 | self.orig_length = self.current_length() | |
176 | self.readline_tail = self.get_readline_tail() |
|
160 | self.readline_tail = self.get_readline_tail() | |
177 | except (AttributeError, IndexError): # Can fail with pyreadline |
|
161 | except (AttributeError, IndexError): # Can fail with pyreadline | |
178 | self.orig_length, self.readline_tail = 999999, [] |
|
162 | self.orig_length, self.readline_tail = 999999, [] | |
179 | self._nested_level += 1 |
|
163 | self._nested_level += 1 | |
180 |
|
164 | |||
181 | def __exit__(self, type, value, traceback): |
|
165 | def __exit__(self, type, value, traceback): | |
182 | self._nested_level -= 1 |
|
166 | self._nested_level -= 1 | |
183 | if self._nested_level == 0: |
|
167 | if self._nested_level == 0: | |
184 | # Try clipping the end if it's got longer |
|
168 | # Try clipping the end if it's got longer | |
185 | try: |
|
169 | try: | |
186 | e = self.current_length() - self.orig_length |
|
170 | e = self.current_length() - self.orig_length | |
187 | if e > 0: |
|
171 | if e > 0: | |
188 | for _ in range(e): |
|
172 | for _ in range(e): | |
189 | self.shell.readline.remove_history_item(self.orig_length) |
|
173 | self.shell.readline.remove_history_item(self.orig_length) | |
190 |
|
174 | |||
191 | # If it still doesn't match, just reload readline history. |
|
175 | # If it still doesn't match, just reload readline history. | |
192 | if self.current_length() != self.orig_length \ |
|
176 | if self.current_length() != self.orig_length \ | |
193 | or self.get_readline_tail() != self.readline_tail: |
|
177 | or self.get_readline_tail() != self.readline_tail: | |
194 | self.shell.refill_readline_hist() |
|
178 | self.shell.refill_readline_hist() | |
195 | except (AttributeError, IndexError): |
|
179 | except (AttributeError, IndexError): | |
196 | pass |
|
180 | pass | |
197 | # Returning False will cause exceptions to propagate |
|
181 | # Returning False will cause exceptions to propagate | |
198 | return False |
|
182 | return False | |
199 |
|
183 | |||
200 | def current_length(self): |
|
184 | def current_length(self): | |
201 | return self.shell.readline.get_current_history_length() |
|
185 | return self.shell.readline.get_current_history_length() | |
202 |
|
186 | |||
203 | def get_readline_tail(self, n=10): |
|
187 | def get_readline_tail(self, n=10): | |
204 | """Get the last n items in readline history.""" |
|
188 | """Get the last n items in readline history.""" | |
205 | end = self.shell.readline.get_current_history_length() + 1 |
|
189 | end = self.shell.readline.get_current_history_length() + 1 | |
206 | start = max(end-n, 1) |
|
190 | start = max(end-n, 1) | |
207 | ghi = self.shell.readline.get_history_item |
|
191 | ghi = self.shell.readline.get_history_item | |
208 | return [ghi(x) for x in range(start, end)] |
|
192 | return [ghi(x) for x in range(start, end)] | |
209 |
|
193 | |||
210 | #----------------------------------------------------------------------------- |
|
194 | #----------------------------------------------------------------------------- | |
211 | # Main IPython class |
|
195 | # Main IPython class | |
212 | #----------------------------------------------------------------------------- |
|
196 | #----------------------------------------------------------------------------- | |
213 |
|
197 | |||
214 | class InteractiveShell(SingletonConfigurable): |
|
198 | class InteractiveShell(SingletonConfigurable): | |
215 | """An enhanced, interactive shell for Python.""" |
|
199 | """An enhanced, interactive shell for Python.""" | |
216 |
|
200 | |||
217 | _instance = None |
|
201 | _instance = None | |
218 |
|
202 | |||
219 | autocall = Enum((0,1,2), default_value=0, config=True, help= |
|
203 | autocall = Enum((0,1,2), default_value=0, config=True, help= | |
220 | """ |
|
204 | """ | |
221 | Make IPython automatically call any callable object even if you didn't |
|
205 | Make IPython automatically call any callable object even if you didn't | |
222 | type explicit parentheses. For example, 'str 43' becomes 'str(43)' |
|
206 | type explicit parentheses. For example, 'str 43' becomes 'str(43)' | |
223 | automatically. The value can be '0' to disable the feature, '1' for |
|
207 | automatically. The value can be '0' to disable the feature, '1' for | |
224 | 'smart' autocall, where it is not applied if there are no more |
|
208 | 'smart' autocall, where it is not applied if there are no more | |
225 | arguments on the line, and '2' for 'full' autocall, where all callable |
|
209 | arguments on the line, and '2' for 'full' autocall, where all callable | |
226 | objects are automatically called (even if no arguments are present). |
|
210 | objects are automatically called (even if no arguments are present). | |
227 | """ |
|
211 | """ | |
228 | ) |
|
212 | ) | |
229 | # TODO: remove all autoindent logic and put into frontends. |
|
213 | # TODO: remove all autoindent logic and put into frontends. | |
230 | # We can't do this yet because even runlines uses the autoindent. |
|
214 | # We can't do this yet because even runlines uses the autoindent. | |
231 | autoindent = CBool(True, config=True, help= |
|
215 | autoindent = CBool(True, config=True, help= | |
232 | """ |
|
216 | """ | |
233 | Autoindent IPython code entered interactively. |
|
217 | Autoindent IPython code entered interactively. | |
234 | """ |
|
218 | """ | |
235 | ) |
|
219 | ) | |
236 | automagic = CBool(True, config=True, help= |
|
220 | automagic = CBool(True, config=True, help= | |
237 | """ |
|
221 | """ | |
238 | Enable magic commands to be called without the leading %. |
|
222 | Enable magic commands to be called without the leading %. | |
239 | """ |
|
223 | """ | |
240 | ) |
|
224 | ) | |
241 | cache_size = Integer(1000, config=True, help= |
|
225 | cache_size = Integer(1000, config=True, help= | |
242 | """ |
|
226 | """ | |
243 | Set the size of the output cache. The default is 1000, you can |
|
227 | Set the size of the output cache. The default is 1000, you can | |
244 | change it permanently in your config file. Setting it to 0 completely |
|
228 | change it permanently in your config file. Setting it to 0 completely | |
245 | disables the caching system, and the minimum value accepted is 20 (if |
|
229 | disables the caching system, and the minimum value accepted is 20 (if | |
246 | you provide a value less than 20, it is reset to 0 and a warning is |
|
230 | you provide a value less than 20, it is reset to 0 and a warning is | |
247 | issued). This limit is defined because otherwise you'll spend more |
|
231 | issued). This limit is defined because otherwise you'll spend more | |
248 | time re-flushing a too small cache than working |
|
232 | time re-flushing a too small cache than working | |
249 | """ |
|
233 | """ | |
250 | ) |
|
234 | ) | |
251 | color_info = CBool(True, config=True, help= |
|
235 | color_info = CBool(True, config=True, help= | |
252 | """ |
|
236 | """ | |
253 | Use colors for displaying information about objects. Because this |
|
237 | Use colors for displaying information about objects. Because this | |
254 | information is passed through a pager (like 'less'), and some pagers |
|
238 | information is passed through a pager (like 'less'), and some pagers | |
255 | get confused with color codes, this capability can be turned off. |
|
239 | get confused with color codes, this capability can be turned off. | |
256 | """ |
|
240 | """ | |
257 | ) |
|
241 | ) | |
258 | colors = CaselessStrEnum(('NoColor','LightBG','Linux'), |
|
242 | colors = CaselessStrEnum(('NoColor','LightBG','Linux'), | |
259 | default_value=get_default_colors(), config=True, |
|
243 | default_value=get_default_colors(), config=True, | |
260 | help="Set the color scheme (NoColor, Linux, or LightBG)." |
|
244 | help="Set the color scheme (NoColor, Linux, or LightBG)." | |
261 | ) |
|
245 | ) | |
262 | colors_force = CBool(False, help= |
|
246 | colors_force = CBool(False, help= | |
263 | """ |
|
247 | """ | |
264 | Force use of ANSI color codes, regardless of OS and readline |
|
248 | Force use of ANSI color codes, regardless of OS and readline | |
265 | availability. |
|
249 | availability. | |
266 | """ |
|
250 | """ | |
267 | # FIXME: This is essentially a hack to allow ZMQShell to show colors |
|
251 | # FIXME: This is essentially a hack to allow ZMQShell to show colors | |
268 | # without readline on Win32. When the ZMQ formatting system is |
|
252 | # without readline on Win32. When the ZMQ formatting system is | |
269 | # refactored, this should be removed. |
|
253 | # refactored, this should be removed. | |
270 | ) |
|
254 | ) | |
271 | debug = CBool(False, config=True) |
|
255 | debug = CBool(False, config=True) | |
272 | deep_reload = CBool(False, config=True, help= |
|
256 | deep_reload = CBool(False, config=True, help= | |
273 | """ |
|
257 | """ | |
274 | Enable deep (recursive) reloading by default. IPython can use the |
|
258 | Enable deep (recursive) reloading by default. IPython can use the | |
275 | deep_reload module which reloads changes in modules recursively (it |
|
259 | deep_reload module which reloads changes in modules recursively (it | |
276 | replaces the reload() function, so you don't need to change anything to |
|
260 | replaces the reload() function, so you don't need to change anything to | |
277 | use it). deep_reload() forces a full reload of modules whose code may |
|
261 | use it). deep_reload() forces a full reload of modules whose code may | |
278 | have changed, which the default reload() function does not. When |
|
262 | have changed, which the default reload() function does not. When | |
279 | deep_reload is off, IPython will use the normal reload(), but |
|
263 | deep_reload is off, IPython will use the normal reload(), but | |
280 | deep_reload will still be available as dreload(). |
|
264 | deep_reload will still be available as dreload(). | |
281 | """ |
|
265 | """ | |
282 | ) |
|
266 | ) | |
283 | disable_failing_post_execute = CBool(False, config=True, |
|
267 | disable_failing_post_execute = CBool(False, config=True, | |
284 | help="Don't call post-execute functions that have failed in the past." |
|
268 | help="Don't call post-execute functions that have failed in the past." | |
285 | ) |
|
269 | ) | |
286 | display_formatter = Instance(DisplayFormatter) |
|
270 | display_formatter = Instance(DisplayFormatter) | |
287 | displayhook_class = Type(DisplayHook) |
|
271 | displayhook_class = Type(DisplayHook) | |
288 | display_pub_class = Type(DisplayPublisher) |
|
272 | display_pub_class = Type(DisplayPublisher) | |
289 |
|
273 | |||
290 | exit_now = CBool(False) |
|
274 | exit_now = CBool(False) | |
291 | exiter = Instance(ExitAutocall) |
|
275 | exiter = Instance(ExitAutocall) | |
292 | def _exiter_default(self): |
|
276 | def _exiter_default(self): | |
293 | return ExitAutocall(self) |
|
277 | return ExitAutocall(self) | |
294 | # Monotonically increasing execution counter |
|
278 | # Monotonically increasing execution counter | |
295 | execution_count = Integer(1) |
|
279 | execution_count = Integer(1) | |
296 | filename = Unicode("<ipython console>") |
|
280 | filename = Unicode("<ipython console>") | |
297 | ipython_dir= Unicode('', config=True) # Set to get_ipython_dir() in __init__ |
|
281 | ipython_dir= Unicode('', config=True) # Set to get_ipython_dir() in __init__ | |
298 |
|
282 | |||
299 | # Input splitter, to split entire cells of input into either individual |
|
283 | # Input splitter, to split entire cells of input into either individual | |
300 | # interactive statements or whole blocks. |
|
284 | # interactive statements or whole blocks. | |
301 | input_splitter = Instance('IPython.core.inputsplitter.IPythonInputSplitter', |
|
285 | input_splitter = Instance('IPython.core.inputsplitter.IPythonInputSplitter', | |
302 | (), {}) |
|
286 | (), {}) | |
303 | logstart = CBool(False, config=True, help= |
|
287 | logstart = CBool(False, config=True, help= | |
304 | """ |
|
288 | """ | |
305 | Start logging to the default log file. |
|
289 | Start logging to the default log file. | |
306 | """ |
|
290 | """ | |
307 | ) |
|
291 | ) | |
308 | logfile = Unicode('', config=True, help= |
|
292 | logfile = Unicode('', config=True, help= | |
309 | """ |
|
293 | """ | |
310 | The name of the logfile to use. |
|
294 | The name of the logfile to use. | |
311 | """ |
|
295 | """ | |
312 | ) |
|
296 | ) | |
313 | logappend = Unicode('', config=True, help= |
|
297 | logappend = Unicode('', config=True, help= | |
314 | """ |
|
298 | """ | |
315 | Start logging to the given file in append mode. |
|
299 | Start logging to the given file in append mode. | |
316 | """ |
|
300 | """ | |
317 | ) |
|
301 | ) | |
318 | object_info_string_level = Enum((0,1,2), default_value=0, |
|
302 | object_info_string_level = Enum((0,1,2), default_value=0, | |
319 | config=True) |
|
303 | config=True) | |
320 | pdb = CBool(False, config=True, help= |
|
304 | pdb = CBool(False, config=True, help= | |
321 | """ |
|
305 | """ | |
322 | Automatically call the pdb debugger after every exception. |
|
306 | Automatically call the pdb debugger after every exception. | |
323 | """ |
|
307 | """ | |
324 | ) |
|
308 | ) | |
325 | multiline_history = CBool(sys.platform != 'win32', config=True, |
|
309 | multiline_history = CBool(sys.platform != 'win32', config=True, | |
326 | help="Save multi-line entries as one entry in readline history" |
|
310 | help="Save multi-line entries as one entry in readline history" | |
327 | ) |
|
311 | ) | |
328 |
|
312 | |||
329 | # deprecated prompt traits: |
|
313 | # deprecated prompt traits: | |
330 |
|
314 | |||
331 | prompt_in1 = Unicode('In [\\#]: ', config=True, |
|
315 | prompt_in1 = Unicode('In [\\#]: ', config=True, | |
332 | help="Deprecated, use PromptManager.in_template") |
|
316 | help="Deprecated, use PromptManager.in_template") | |
333 | prompt_in2 = Unicode(' .\\D.: ', config=True, |
|
317 | prompt_in2 = Unicode(' .\\D.: ', config=True, | |
334 | help="Deprecated, use PromptManager.in2_template") |
|
318 | help="Deprecated, use PromptManager.in2_template") | |
335 | prompt_out = Unicode('Out[\\#]: ', config=True, |
|
319 | prompt_out = Unicode('Out[\\#]: ', config=True, | |
336 | help="Deprecated, use PromptManager.out_template") |
|
320 | help="Deprecated, use PromptManager.out_template") | |
337 | prompts_pad_left = CBool(True, config=True, |
|
321 | prompts_pad_left = CBool(True, config=True, | |
338 | help="Deprecated, use PromptManager.justify") |
|
322 | help="Deprecated, use PromptManager.justify") | |
339 |
|
323 | |||
340 | def _prompt_trait_changed(self, name, old, new): |
|
324 | def _prompt_trait_changed(self, name, old, new): | |
341 | table = { |
|
325 | table = { | |
342 | 'prompt_in1' : 'in_template', |
|
326 | 'prompt_in1' : 'in_template', | |
343 | 'prompt_in2' : 'in2_template', |
|
327 | 'prompt_in2' : 'in2_template', | |
344 | 'prompt_out' : 'out_template', |
|
328 | 'prompt_out' : 'out_template', | |
345 | 'prompts_pad_left' : 'justify', |
|
329 | 'prompts_pad_left' : 'justify', | |
346 | } |
|
330 | } | |
347 | warn("InteractiveShell.{name} is deprecated, use PromptManager.{newname}\n".format( |
|
331 | warn("InteractiveShell.{name} is deprecated, use PromptManager.{newname}\n".format( | |
348 | name=name, newname=table[name]) |
|
332 | name=name, newname=table[name]) | |
349 | ) |
|
333 | ) | |
350 | # protect against weird cases where self.config may not exist: |
|
334 | # protect against weird cases where self.config may not exist: | |
351 | if self.config is not None: |
|
335 | if self.config is not None: | |
352 | # propagate to corresponding PromptManager trait |
|
336 | # propagate to corresponding PromptManager trait | |
353 | setattr(self.config.PromptManager, table[name], new) |
|
337 | setattr(self.config.PromptManager, table[name], new) | |
354 |
|
338 | |||
355 | _prompt_in1_changed = _prompt_trait_changed |
|
339 | _prompt_in1_changed = _prompt_trait_changed | |
356 | _prompt_in2_changed = _prompt_trait_changed |
|
340 | _prompt_in2_changed = _prompt_trait_changed | |
357 | _prompt_out_changed = _prompt_trait_changed |
|
341 | _prompt_out_changed = _prompt_trait_changed | |
358 | _prompt_pad_left_changed = _prompt_trait_changed |
|
342 | _prompt_pad_left_changed = _prompt_trait_changed | |
359 |
|
343 | |||
360 | show_rewritten_input = CBool(True, config=True, |
|
344 | show_rewritten_input = CBool(True, config=True, | |
361 | help="Show rewritten input, e.g. for autocall." |
|
345 | help="Show rewritten input, e.g. for autocall." | |
362 | ) |
|
346 | ) | |
363 |
|
347 | |||
364 | quiet = CBool(False, config=True) |
|
348 | quiet = CBool(False, config=True) | |
365 |
|
349 | |||
366 | history_length = Integer(10000, config=True) |
|
350 | history_length = Integer(10000, config=True) | |
367 |
|
351 | |||
368 | # The readline stuff will eventually be moved to the terminal subclass |
|
352 | # The readline stuff will eventually be moved to the terminal subclass | |
369 | # but for now, we can't do that as readline is welded in everywhere. |
|
353 | # but for now, we can't do that as readline is welded in everywhere. | |
370 | readline_use = CBool(True, config=True) |
|
354 | readline_use = CBool(True, config=True) | |
371 | readline_remove_delims = Unicode('-/~', config=True) |
|
355 | readline_remove_delims = Unicode('-/~', config=True) | |
372 | # don't use \M- bindings by default, because they |
|
356 | # don't use \M- bindings by default, because they | |
373 | # conflict with 8-bit encodings. See gh-58,gh-88 |
|
357 | # conflict with 8-bit encodings. See gh-58,gh-88 | |
374 | readline_parse_and_bind = List([ |
|
358 | readline_parse_and_bind = List([ | |
375 | 'tab: complete', |
|
359 | 'tab: complete', | |
376 | '"\C-l": clear-screen', |
|
360 | '"\C-l": clear-screen', | |
377 | 'set show-all-if-ambiguous on', |
|
361 | 'set show-all-if-ambiguous on', | |
378 | '"\C-o": tab-insert', |
|
362 | '"\C-o": tab-insert', | |
379 | '"\C-r": reverse-search-history', |
|
363 | '"\C-r": reverse-search-history', | |
380 | '"\C-s": forward-search-history', |
|
364 | '"\C-s": forward-search-history', | |
381 | '"\C-p": history-search-backward', |
|
365 | '"\C-p": history-search-backward', | |
382 | '"\C-n": history-search-forward', |
|
366 | '"\C-n": history-search-forward', | |
383 | '"\e[A": history-search-backward', |
|
367 | '"\e[A": history-search-backward', | |
384 | '"\e[B": history-search-forward', |
|
368 | '"\e[B": history-search-forward', | |
385 | '"\C-k": kill-line', |
|
369 | '"\C-k": kill-line', | |
386 | '"\C-u": unix-line-discard', |
|
370 | '"\C-u": unix-line-discard', | |
387 | ], allow_none=False, config=True) |
|
371 | ], allow_none=False, config=True) | |
388 |
|
372 | |||
389 | ast_node_interactivity = Enum(['all', 'last', 'last_expr', 'none'], |
|
373 | ast_node_interactivity = Enum(['all', 'last', 'last_expr', 'none'], | |
390 | default_value='last_expr', config=True, |
|
374 | default_value='last_expr', config=True, | |
391 | help=""" |
|
375 | help=""" | |
392 | 'all', 'last', 'last_expr' or 'none', specifying which nodes should be |
|
376 | 'all', 'last', 'last_expr' or 'none', specifying which nodes should be | |
393 | run interactively (displaying output from expressions).""") |
|
377 | run interactively (displaying output from expressions).""") | |
394 |
|
378 | |||
395 | # TODO: this part of prompt management should be moved to the frontends. |
|
379 | # TODO: this part of prompt management should be moved to the frontends. | |
396 | # Use custom TraitTypes that convert '0'->'' and '\\n'->'\n' |
|
380 | # Use custom TraitTypes that convert '0'->'' and '\\n'->'\n' | |
397 | separate_in = SeparateUnicode('\n', config=True) |
|
381 | separate_in = SeparateUnicode('\n', config=True) | |
398 | separate_out = SeparateUnicode('', config=True) |
|
382 | separate_out = SeparateUnicode('', config=True) | |
399 | separate_out2 = SeparateUnicode('', config=True) |
|
383 | separate_out2 = SeparateUnicode('', config=True) | |
400 | wildcards_case_sensitive = CBool(True, config=True) |
|
384 | wildcards_case_sensitive = CBool(True, config=True) | |
401 | xmode = CaselessStrEnum(('Context','Plain', 'Verbose'), |
|
385 | xmode = CaselessStrEnum(('Context','Plain', 'Verbose'), | |
402 | default_value='Context', config=True) |
|
386 | default_value='Context', config=True) | |
403 |
|
387 | |||
404 | # Subcomponents of InteractiveShell |
|
388 | # Subcomponents of InteractiveShell | |
405 | alias_manager = Instance('IPython.core.alias.AliasManager') |
|
389 | alias_manager = Instance('IPython.core.alias.AliasManager') | |
406 | prefilter_manager = Instance('IPython.core.prefilter.PrefilterManager') |
|
390 | prefilter_manager = Instance('IPython.core.prefilter.PrefilterManager') | |
407 | builtin_trap = Instance('IPython.core.builtin_trap.BuiltinTrap') |
|
391 | builtin_trap = Instance('IPython.core.builtin_trap.BuiltinTrap') | |
408 | display_trap = Instance('IPython.core.display_trap.DisplayTrap') |
|
392 | display_trap = Instance('IPython.core.display_trap.DisplayTrap') | |
409 | extension_manager = Instance('IPython.core.extensions.ExtensionManager') |
|
393 | extension_manager = Instance('IPython.core.extensions.ExtensionManager') | |
410 | plugin_manager = Instance('IPython.core.plugin.PluginManager') |
|
394 | plugin_manager = Instance('IPython.core.plugin.PluginManager') | |
411 | payload_manager = Instance('IPython.core.payload.PayloadManager') |
|
395 | payload_manager = Instance('IPython.core.payload.PayloadManager') | |
412 | history_manager = Instance('IPython.core.history.HistoryManager') |
|
396 | history_manager = Instance('IPython.core.history.HistoryManager') | |
413 | magics_manager = Instance('IPython.core.magic.MagicsManager') |
|
397 | magics_manager = Instance('IPython.core.magic.MagicsManager') | |
414 |
|
398 | |||
415 | profile_dir = Instance('IPython.core.application.ProfileDir') |
|
399 | profile_dir = Instance('IPython.core.application.ProfileDir') | |
416 | @property |
|
400 | @property | |
417 | def profile(self): |
|
401 | def profile(self): | |
418 | if self.profile_dir is not None: |
|
402 | if self.profile_dir is not None: | |
419 | name = os.path.basename(self.profile_dir.location) |
|
403 | name = os.path.basename(self.profile_dir.location) | |
420 | return name.replace('profile_','') |
|
404 | return name.replace('profile_','') | |
421 |
|
405 | |||
422 |
|
406 | |||
423 | # Private interface |
|
407 | # Private interface | |
424 | _post_execute = Instance(dict) |
|
408 | _post_execute = Instance(dict) | |
425 |
|
409 | |||
426 | # Tracks any GUI loop loaded for pylab |
|
410 | # Tracks any GUI loop loaded for pylab | |
427 | pylab_gui_select = None |
|
411 | pylab_gui_select = None | |
428 |
|
412 | |||
429 | def __init__(self, config=None, ipython_dir=None, profile_dir=None, |
|
413 | def __init__(self, config=None, ipython_dir=None, profile_dir=None, | |
430 | user_module=None, user_ns=None, |
|
414 | user_module=None, user_ns=None, | |
431 | custom_exceptions=((), None)): |
|
415 | custom_exceptions=((), None)): | |
432 |
|
416 | |||
433 | # This is where traits with a config_key argument are updated |
|
417 | # This is where traits with a config_key argument are updated | |
434 | # from the values on config. |
|
418 | # from the values on config. | |
435 | super(InteractiveShell, self).__init__(config=config) |
|
419 | super(InteractiveShell, self).__init__(config=config) | |
436 | self.configurables = [self] |
|
420 | self.configurables = [self] | |
437 |
|
421 | |||
438 | # These are relatively independent and stateless |
|
422 | # These are relatively independent and stateless | |
439 | self.init_ipython_dir(ipython_dir) |
|
423 | self.init_ipython_dir(ipython_dir) | |
440 | self.init_profile_dir(profile_dir) |
|
424 | self.init_profile_dir(profile_dir) | |
441 | self.init_instance_attrs() |
|
425 | self.init_instance_attrs() | |
442 | self.init_environment() |
|
426 | self.init_environment() | |
443 |
|
427 | |||
444 | # Check if we're in a virtualenv, and set up sys.path. |
|
428 | # Check if we're in a virtualenv, and set up sys.path. | |
445 | self.init_virtualenv() |
|
429 | self.init_virtualenv() | |
446 |
|
430 | |||
447 | # Create namespaces (user_ns, user_global_ns, etc.) |
|
431 | # Create namespaces (user_ns, user_global_ns, etc.) | |
448 | self.init_create_namespaces(user_module, user_ns) |
|
432 | self.init_create_namespaces(user_module, user_ns) | |
449 | # This has to be done after init_create_namespaces because it uses |
|
433 | # This has to be done after init_create_namespaces because it uses | |
450 | # something in self.user_ns, but before init_sys_modules, which |
|
434 | # something in self.user_ns, but before init_sys_modules, which | |
451 | # is the first thing to modify sys. |
|
435 | # is the first thing to modify sys. | |
452 | # TODO: When we override sys.stdout and sys.stderr before this class |
|
436 | # TODO: When we override sys.stdout and sys.stderr before this class | |
453 | # is created, we are saving the overridden ones here. Not sure if this |
|
437 | # is created, we are saving the overridden ones here. Not sure if this | |
454 | # is what we want to do. |
|
438 | # is what we want to do. | |
455 | self.save_sys_module_state() |
|
439 | self.save_sys_module_state() | |
456 | self.init_sys_modules() |
|
440 | self.init_sys_modules() | |
457 |
|
441 | |||
458 | # While we're trying to have each part of the code directly access what |
|
442 | # While we're trying to have each part of the code directly access what | |
459 | # it needs without keeping redundant references to objects, we have too |
|
443 | # it needs without keeping redundant references to objects, we have too | |
460 | # much legacy code that expects ip.db to exist. |
|
444 | # much legacy code that expects ip.db to exist. | |
461 | self.db = PickleShareDB(os.path.join(self.profile_dir.location, 'db')) |
|
445 | self.db = PickleShareDB(os.path.join(self.profile_dir.location, 'db')) | |
462 |
|
446 | |||
463 | self.init_history() |
|
447 | self.init_history() | |
464 | self.init_encoding() |
|
448 | self.init_encoding() | |
465 | self.init_prefilter() |
|
449 | self.init_prefilter() | |
466 |
|
450 | |||
467 | self.init_syntax_highlighting() |
|
451 | self.init_syntax_highlighting() | |
468 | self.init_hooks() |
|
452 | self.init_hooks() | |
469 | self.init_pushd_popd_magic() |
|
453 | self.init_pushd_popd_magic() | |
470 | # self.init_traceback_handlers use to be here, but we moved it below |
|
454 | # self.init_traceback_handlers use to be here, but we moved it below | |
471 | # because it and init_io have to come after init_readline. |
|
455 | # because it and init_io have to come after init_readline. | |
472 | self.init_user_ns() |
|
456 | self.init_user_ns() | |
473 | self.init_logger() |
|
457 | self.init_logger() | |
474 | self.init_alias() |
|
458 | self.init_alias() | |
475 | self.init_builtins() |
|
459 | self.init_builtins() | |
476 |
|
460 | |||
477 | # The following was in post_config_initialization |
|
461 | # The following was in post_config_initialization | |
478 | self.init_inspector() |
|
462 | self.init_inspector() | |
479 | # init_readline() must come before init_io(), because init_io uses |
|
463 | # init_readline() must come before init_io(), because init_io uses | |
480 | # readline related things. |
|
464 | # readline related things. | |
481 | self.init_readline() |
|
465 | self.init_readline() | |
482 | # We save this here in case user code replaces raw_input, but it needs |
|
466 | # We save this here in case user code replaces raw_input, but it needs | |
483 | # to be after init_readline(), because PyPy's readline works by replacing |
|
467 | # to be after init_readline(), because PyPy's readline works by replacing | |
484 | # raw_input. |
|
468 | # raw_input. | |
485 | if py3compat.PY3: |
|
469 | if py3compat.PY3: | |
486 | self.raw_input_original = input |
|
470 | self.raw_input_original = input | |
487 | else: |
|
471 | else: | |
488 | self.raw_input_original = raw_input |
|
472 | self.raw_input_original = raw_input | |
489 | # init_completer must come after init_readline, because it needs to |
|
473 | # init_completer must come after init_readline, because it needs to | |
490 | # know whether readline is present or not system-wide to configure the |
|
474 | # know whether readline is present or not system-wide to configure the | |
491 | # completers, since the completion machinery can now operate |
|
475 | # completers, since the completion machinery can now operate | |
492 | # independently of readline (e.g. over the network) |
|
476 | # independently of readline (e.g. over the network) | |
493 | self.init_completer() |
|
477 | self.init_completer() | |
494 | # TODO: init_io() needs to happen before init_traceback handlers |
|
478 | # TODO: init_io() needs to happen before init_traceback handlers | |
495 | # because the traceback handlers hardcode the stdout/stderr streams. |
|
479 | # because the traceback handlers hardcode the stdout/stderr streams. | |
496 | # This logic in in debugger.Pdb and should eventually be changed. |
|
480 | # This logic in in debugger.Pdb and should eventually be changed. | |
497 | self.init_io() |
|
481 | self.init_io() | |
498 | self.init_traceback_handlers(custom_exceptions) |
|
482 | self.init_traceback_handlers(custom_exceptions) | |
499 | self.init_prompts() |
|
483 | self.init_prompts() | |
500 | self.init_display_formatter() |
|
484 | self.init_display_formatter() | |
501 | self.init_display_pub() |
|
485 | self.init_display_pub() | |
502 | self.init_displayhook() |
|
486 | self.init_displayhook() | |
503 | self.init_reload_doctest() |
|
487 | self.init_reload_doctest() | |
504 | self.init_latextool() |
|
488 | self.init_latextool() | |
505 | self.init_magics() |
|
489 | self.init_magics() | |
506 | self.init_logstart() |
|
490 | self.init_logstart() | |
507 | self.init_pdb() |
|
491 | self.init_pdb() | |
508 | self.init_extension_manager() |
|
492 | self.init_extension_manager() | |
509 | self.init_plugin_manager() |
|
493 | self.init_plugin_manager() | |
510 | self.init_payload() |
|
494 | self.init_payload() | |
511 | self.hooks.late_startup_hook() |
|
495 | self.hooks.late_startup_hook() | |
512 | atexit.register(self.atexit_operations) |
|
496 | atexit.register(self.atexit_operations) | |
513 |
|
497 | |||
514 | def get_ipython(self): |
|
498 | def get_ipython(self): | |
515 | """Return the currently running IPython instance.""" |
|
499 | """Return the currently running IPython instance.""" | |
516 | return self |
|
500 | return self | |
517 |
|
501 | |||
518 | #------------------------------------------------------------------------- |
|
502 | #------------------------------------------------------------------------- | |
519 | # Trait changed handlers |
|
503 | # Trait changed handlers | |
520 | #------------------------------------------------------------------------- |
|
504 | #------------------------------------------------------------------------- | |
521 |
|
505 | |||
522 | def _ipython_dir_changed(self, name, new): |
|
506 | def _ipython_dir_changed(self, name, new): | |
523 | if not os.path.isdir(new): |
|
507 | if not os.path.isdir(new): | |
524 | os.makedirs(new, mode = 0777) |
|
508 | os.makedirs(new, mode = 0777) | |
525 |
|
509 | |||
526 | def set_autoindent(self,value=None): |
|
510 | def set_autoindent(self,value=None): | |
527 | """Set the autoindent flag, checking for readline support. |
|
511 | """Set the autoindent flag, checking for readline support. | |
528 |
|
512 | |||
529 | If called with no arguments, it acts as a toggle.""" |
|
513 | If called with no arguments, it acts as a toggle.""" | |
530 |
|
514 | |||
531 | if value != 0 and not self.has_readline: |
|
515 | if value != 0 and not self.has_readline: | |
532 | if os.name == 'posix': |
|
516 | if os.name == 'posix': | |
533 | warn("The auto-indent feature requires the readline library") |
|
517 | warn("The auto-indent feature requires the readline library") | |
534 | self.autoindent = 0 |
|
518 | self.autoindent = 0 | |
535 | return |
|
519 | return | |
536 | if value is None: |
|
520 | if value is None: | |
537 | self.autoindent = not self.autoindent |
|
521 | self.autoindent = not self.autoindent | |
538 | else: |
|
522 | else: | |
539 | self.autoindent = value |
|
523 | self.autoindent = value | |
540 |
|
524 | |||
541 | #------------------------------------------------------------------------- |
|
525 | #------------------------------------------------------------------------- | |
542 | # init_* methods called by __init__ |
|
526 | # init_* methods called by __init__ | |
543 | #------------------------------------------------------------------------- |
|
527 | #------------------------------------------------------------------------- | |
544 |
|
528 | |||
545 | def init_ipython_dir(self, ipython_dir): |
|
529 | def init_ipython_dir(self, ipython_dir): | |
546 | if ipython_dir is not None: |
|
530 | if ipython_dir is not None: | |
547 | self.ipython_dir = ipython_dir |
|
531 | self.ipython_dir = ipython_dir | |
548 | return |
|
532 | return | |
549 |
|
533 | |||
550 | self.ipython_dir = get_ipython_dir() |
|
534 | self.ipython_dir = get_ipython_dir() | |
551 |
|
535 | |||
552 | def init_profile_dir(self, profile_dir): |
|
536 | def init_profile_dir(self, profile_dir): | |
553 | if profile_dir is not None: |
|
537 | if profile_dir is not None: | |
554 | self.profile_dir = profile_dir |
|
538 | self.profile_dir = profile_dir | |
555 | return |
|
539 | return | |
556 | self.profile_dir =\ |
|
540 | self.profile_dir =\ | |
557 | ProfileDir.create_profile_dir_by_name(self.ipython_dir, 'default') |
|
541 | ProfileDir.create_profile_dir_by_name(self.ipython_dir, 'default') | |
558 |
|
542 | |||
559 | def init_instance_attrs(self): |
|
543 | def init_instance_attrs(self): | |
560 | self.more = False |
|
544 | self.more = False | |
561 |
|
545 | |||
562 | # command compiler |
|
546 | # command compiler | |
563 | self.compile = CachingCompiler() |
|
547 | self.compile = CachingCompiler() | |
564 |
|
548 | |||
565 | # Make an empty namespace, which extension writers can rely on both |
|
549 | # Make an empty namespace, which extension writers can rely on both | |
566 | # existing and NEVER being used by ipython itself. This gives them a |
|
550 | # existing and NEVER being used by ipython itself. This gives them a | |
567 | # convenient location for storing additional information and state |
|
551 | # convenient location for storing additional information and state | |
568 | # their extensions may require, without fear of collisions with other |
|
552 | # their extensions may require, without fear of collisions with other | |
569 | # ipython names that may develop later. |
|
553 | # ipython names that may develop later. | |
570 | self.meta = Struct() |
|
554 | self.meta = Struct() | |
571 |
|
555 | |||
572 | # Temporary files used for various purposes. Deleted at exit. |
|
556 | # Temporary files used for various purposes. Deleted at exit. | |
573 | self.tempfiles = [] |
|
557 | self.tempfiles = [] | |
574 |
|
558 | |||
575 | # Keep track of readline usage (later set by init_readline) |
|
559 | # Keep track of readline usage (later set by init_readline) | |
576 | self.has_readline = False |
|
560 | self.has_readline = False | |
577 |
|
561 | |||
578 | # keep track of where we started running (mainly for crash post-mortem) |
|
562 | # keep track of where we started running (mainly for crash post-mortem) | |
579 | # This is not being used anywhere currently. |
|
563 | # This is not being used anywhere currently. | |
580 | self.starting_dir = os.getcwdu() |
|
564 | self.starting_dir = os.getcwdu() | |
581 |
|
565 | |||
582 | # Indentation management |
|
566 | # Indentation management | |
583 | self.indent_current_nsp = 0 |
|
567 | self.indent_current_nsp = 0 | |
584 |
|
568 | |||
585 | # Dict to track post-execution functions that have been registered |
|
569 | # Dict to track post-execution functions that have been registered | |
586 | self._post_execute = {} |
|
570 | self._post_execute = {} | |
587 |
|
571 | |||
588 | def init_environment(self): |
|
572 | def init_environment(self): | |
589 | """Any changes we need to make to the user's environment.""" |
|
573 | """Any changes we need to make to the user's environment.""" | |
590 | pass |
|
574 | pass | |
591 |
|
575 | |||
592 | def init_encoding(self): |
|
576 | def init_encoding(self): | |
593 | # Get system encoding at startup time. Certain terminals (like Emacs |
|
577 | # Get system encoding at startup time. Certain terminals (like Emacs | |
594 | # under Win32 have it set to None, and we need to have a known valid |
|
578 | # under Win32 have it set to None, and we need to have a known valid | |
595 | # encoding to use in the raw_input() method |
|
579 | # encoding to use in the raw_input() method | |
596 | try: |
|
580 | try: | |
597 | self.stdin_encoding = sys.stdin.encoding or 'ascii' |
|
581 | self.stdin_encoding = sys.stdin.encoding or 'ascii' | |
598 | except AttributeError: |
|
582 | except AttributeError: | |
599 | self.stdin_encoding = 'ascii' |
|
583 | self.stdin_encoding = 'ascii' | |
600 |
|
584 | |||
601 | def init_syntax_highlighting(self): |
|
585 | def init_syntax_highlighting(self): | |
602 | # Python source parser/formatter for syntax highlighting |
|
586 | # Python source parser/formatter for syntax highlighting | |
603 | pyformat = PyColorize.Parser().format |
|
587 | pyformat = PyColorize.Parser().format | |
604 | self.pycolorize = lambda src: pyformat(src,'str',self.colors) |
|
588 | self.pycolorize = lambda src: pyformat(src,'str',self.colors) | |
605 |
|
589 | |||
606 | def init_pushd_popd_magic(self): |
|
590 | def init_pushd_popd_magic(self): | |
607 | # for pushd/popd management |
|
591 | # for pushd/popd management | |
608 | self.home_dir = get_home_dir() |
|
592 | self.home_dir = get_home_dir() | |
609 |
|
593 | |||
610 | self.dir_stack = [] |
|
594 | self.dir_stack = [] | |
611 |
|
595 | |||
612 | def init_logger(self): |
|
596 | def init_logger(self): | |
613 | self.logger = Logger(self.home_dir, logfname='ipython_log.py', |
|
597 | self.logger = Logger(self.home_dir, logfname='ipython_log.py', | |
614 | logmode='rotate') |
|
598 | logmode='rotate') | |
615 |
|
599 | |||
616 | def init_logstart(self): |
|
600 | def init_logstart(self): | |
617 | """Initialize logging in case it was requested at the command line. |
|
601 | """Initialize logging in case it was requested at the command line. | |
618 | """ |
|
602 | """ | |
619 | if self.logappend: |
|
603 | if self.logappend: | |
620 | self.magic('logstart %s append' % self.logappend) |
|
604 | self.magic('logstart %s append' % self.logappend) | |
621 | elif self.logfile: |
|
605 | elif self.logfile: | |
622 | self.magic('logstart %s' % self.logfile) |
|
606 | self.magic('logstart %s' % self.logfile) | |
623 | elif self.logstart: |
|
607 | elif self.logstart: | |
624 | self.magic('logstart') |
|
608 | self.magic('logstart') | |
625 |
|
609 | |||
626 | def init_builtins(self): |
|
610 | def init_builtins(self): | |
627 | # A single, static flag that we set to True. Its presence indicates |
|
611 | # A single, static flag that we set to True. Its presence indicates | |
628 | # that an IPython shell has been created, and we make no attempts at |
|
612 | # that an IPython shell has been created, and we make no attempts at | |
629 | # removing on exit or representing the existence of more than one |
|
613 | # removing on exit or representing the existence of more than one | |
630 | # IPython at a time. |
|
614 | # IPython at a time. | |
631 | builtin_mod.__dict__['__IPYTHON__'] = True |
|
615 | builtin_mod.__dict__['__IPYTHON__'] = True | |
632 |
|
616 | |||
633 | # In 0.11 we introduced '__IPYTHON__active' as an integer we'd try to |
|
617 | # In 0.11 we introduced '__IPYTHON__active' as an integer we'd try to | |
634 | # manage on enter/exit, but with all our shells it's virtually |
|
618 | # manage on enter/exit, but with all our shells it's virtually | |
635 | # impossible to get all the cases right. We're leaving the name in for |
|
619 | # impossible to get all the cases right. We're leaving the name in for | |
636 | # those who adapted their codes to check for this flag, but will |
|
620 | # those who adapted their codes to check for this flag, but will | |
637 | # eventually remove it after a few more releases. |
|
621 | # eventually remove it after a few more releases. | |
638 | builtin_mod.__dict__['__IPYTHON__active'] = \ |
|
622 | builtin_mod.__dict__['__IPYTHON__active'] = \ | |
639 | 'Deprecated, check for __IPYTHON__' |
|
623 | 'Deprecated, check for __IPYTHON__' | |
640 |
|
624 | |||
641 | self.builtin_trap = BuiltinTrap(shell=self) |
|
625 | self.builtin_trap = BuiltinTrap(shell=self) | |
642 |
|
626 | |||
643 | def init_inspector(self): |
|
627 | def init_inspector(self): | |
644 | # Object inspector |
|
628 | # Object inspector | |
645 | self.inspector = oinspect.Inspector(oinspect.InspectColors, |
|
629 | self.inspector = oinspect.Inspector(oinspect.InspectColors, | |
646 | PyColorize.ANSICodeColors, |
|
630 | PyColorize.ANSICodeColors, | |
647 | 'NoColor', |
|
631 | 'NoColor', | |
648 | self.object_info_string_level) |
|
632 | self.object_info_string_level) | |
649 |
|
633 | |||
650 | def init_io(self): |
|
634 | def init_io(self): | |
651 | # This will just use sys.stdout and sys.stderr. If you want to |
|
635 | # This will just use sys.stdout and sys.stderr. If you want to | |
652 | # override sys.stdout and sys.stderr themselves, you need to do that |
|
636 | # override sys.stdout and sys.stderr themselves, you need to do that | |
653 | # *before* instantiating this class, because io holds onto |
|
637 | # *before* instantiating this class, because io holds onto | |
654 | # references to the underlying streams. |
|
638 | # references to the underlying streams. | |
655 | if sys.platform == 'win32' and self.has_readline: |
|
639 | if sys.platform == 'win32' and self.has_readline: | |
656 | io.stdout = io.stderr = io.IOStream(self.readline._outputfile) |
|
640 | io.stdout = io.stderr = io.IOStream(self.readline._outputfile) | |
657 | else: |
|
641 | else: | |
658 | io.stdout = io.IOStream(sys.stdout) |
|
642 | io.stdout = io.IOStream(sys.stdout) | |
659 | io.stderr = io.IOStream(sys.stderr) |
|
643 | io.stderr = io.IOStream(sys.stderr) | |
660 |
|
644 | |||
661 | def init_prompts(self): |
|
645 | def init_prompts(self): | |
662 | self.prompt_manager = PromptManager(shell=self, config=self.config) |
|
646 | self.prompt_manager = PromptManager(shell=self, config=self.config) | |
663 | self.configurables.append(self.prompt_manager) |
|
647 | self.configurables.append(self.prompt_manager) | |
664 | # Set system prompts, so that scripts can decide if they are running |
|
648 | # Set system prompts, so that scripts can decide if they are running | |
665 | # interactively. |
|
649 | # interactively. | |
666 | sys.ps1 = 'In : ' |
|
650 | sys.ps1 = 'In : ' | |
667 | sys.ps2 = '...: ' |
|
651 | sys.ps2 = '...: ' | |
668 | sys.ps3 = 'Out: ' |
|
652 | sys.ps3 = 'Out: ' | |
669 |
|
653 | |||
670 | def init_display_formatter(self): |
|
654 | def init_display_formatter(self): | |
671 | self.display_formatter = DisplayFormatter(config=self.config) |
|
655 | self.display_formatter = DisplayFormatter(config=self.config) | |
672 | self.configurables.append(self.display_formatter) |
|
656 | self.configurables.append(self.display_formatter) | |
673 |
|
657 | |||
674 | def init_display_pub(self): |
|
658 | def init_display_pub(self): | |
675 | self.display_pub = self.display_pub_class(config=self.config) |
|
659 | self.display_pub = self.display_pub_class(config=self.config) | |
676 | self.configurables.append(self.display_pub) |
|
660 | self.configurables.append(self.display_pub) | |
677 |
|
661 | |||
678 | def init_displayhook(self): |
|
662 | def init_displayhook(self): | |
679 | # Initialize displayhook, set in/out prompts and printing system |
|
663 | # Initialize displayhook, set in/out prompts and printing system | |
680 | self.displayhook = self.displayhook_class( |
|
664 | self.displayhook = self.displayhook_class( | |
681 | config=self.config, |
|
665 | config=self.config, | |
682 | shell=self, |
|
666 | shell=self, | |
683 | cache_size=self.cache_size, |
|
667 | cache_size=self.cache_size, | |
684 | ) |
|
668 | ) | |
685 | self.configurables.append(self.displayhook) |
|
669 | self.configurables.append(self.displayhook) | |
686 | # This is a context manager that installs/revmoes the displayhook at |
|
670 | # This is a context manager that installs/revmoes the displayhook at | |
687 | # the appropriate time. |
|
671 | # the appropriate time. | |
688 | self.display_trap = DisplayTrap(hook=self.displayhook) |
|
672 | self.display_trap = DisplayTrap(hook=self.displayhook) | |
689 |
|
673 | |||
690 | def init_reload_doctest(self): |
|
674 | def init_reload_doctest(self): | |
691 | # Do a proper resetting of doctest, including the necessary displayhook |
|
675 | # Do a proper resetting of doctest, including the necessary displayhook | |
692 | # monkeypatching |
|
676 | # monkeypatching | |
693 | try: |
|
677 | try: | |
694 | doctest_reload() |
|
678 | doctest_reload() | |
695 | except ImportError: |
|
679 | except ImportError: | |
696 | warn("doctest module does not exist.") |
|
680 | warn("doctest module does not exist.") | |
697 |
|
681 | |||
698 | def init_latextool(self): |
|
682 | def init_latextool(self): | |
699 | """Configure LaTeXTool.""" |
|
683 | """Configure LaTeXTool.""" | |
700 | cfg = LaTeXTool.instance(config=self.config) |
|
684 | cfg = LaTeXTool.instance(config=self.config) | |
701 | if cfg not in self.configurables: |
|
685 | if cfg not in self.configurables: | |
702 | self.configurables.append(cfg) |
|
686 | self.configurables.append(cfg) | |
703 |
|
687 | |||
704 | def init_virtualenv(self): |
|
688 | def init_virtualenv(self): | |
705 | """Add a virtualenv to sys.path so the user can import modules from it. |
|
689 | """Add a virtualenv to sys.path so the user can import modules from it. | |
706 | This isn't perfect: it doesn't use the Python interpreter with which the |
|
690 | This isn't perfect: it doesn't use the Python interpreter with which the | |
707 | virtualenv was built, and it ignores the --no-site-packages option. A |
|
691 | virtualenv was built, and it ignores the --no-site-packages option. A | |
708 | warning will appear suggesting the user installs IPython in the |
|
692 | warning will appear suggesting the user installs IPython in the | |
709 | virtualenv, but for many cases, it probably works well enough. |
|
693 | virtualenv, but for many cases, it probably works well enough. | |
710 |
|
694 | |||
711 | Adapted from code snippets online. |
|
695 | Adapted from code snippets online. | |
712 |
|
696 | |||
713 | http://blog.ufsoft.org/2009/1/29/ipython-and-virtualenv |
|
697 | http://blog.ufsoft.org/2009/1/29/ipython-and-virtualenv | |
714 | """ |
|
698 | """ | |
715 | if 'VIRTUAL_ENV' not in os.environ: |
|
699 | if 'VIRTUAL_ENV' not in os.environ: | |
716 | # Not in a virtualenv |
|
700 | # Not in a virtualenv | |
717 | return |
|
701 | return | |
718 |
|
702 | |||
719 | if sys.executable.startswith(os.environ['VIRTUAL_ENV']): |
|
703 | if sys.executable.startswith(os.environ['VIRTUAL_ENV']): | |
720 | # Running properly in the virtualenv, don't need to do anything |
|
704 | # Running properly in the virtualenv, don't need to do anything | |
721 | return |
|
705 | return | |
722 |
|
706 | |||
723 | warn("Attempting to work in a virtualenv. If you encounter problems, please " |
|
707 | warn("Attempting to work in a virtualenv. If you encounter problems, please " | |
724 | "install IPython inside the virtualenv.\n") |
|
708 | "install IPython inside the virtualenv.\n") | |
725 | if sys.platform == "win32": |
|
709 | if sys.platform == "win32": | |
726 | virtual_env = os.path.join(os.environ['VIRTUAL_ENV'], 'Lib', 'site-packages') |
|
710 | virtual_env = os.path.join(os.environ['VIRTUAL_ENV'], 'Lib', 'site-packages') | |
727 | else: |
|
711 | else: | |
728 | virtual_env = os.path.join(os.environ['VIRTUAL_ENV'], 'lib', |
|
712 | virtual_env = os.path.join(os.environ['VIRTUAL_ENV'], 'lib', | |
729 | 'python%d.%d' % sys.version_info[:2], 'site-packages') |
|
713 | 'python%d.%d' % sys.version_info[:2], 'site-packages') | |
730 |
|
714 | |||
731 | import site |
|
715 | import site | |
732 | sys.path.insert(0, virtual_env) |
|
716 | sys.path.insert(0, virtual_env) | |
733 | site.addsitedir(virtual_env) |
|
717 | site.addsitedir(virtual_env) | |
734 |
|
718 | |||
735 | #------------------------------------------------------------------------- |
|
719 | #------------------------------------------------------------------------- | |
736 | # Things related to injections into the sys module |
|
720 | # Things related to injections into the sys module | |
737 | #------------------------------------------------------------------------- |
|
721 | #------------------------------------------------------------------------- | |
738 |
|
722 | |||
739 | def save_sys_module_state(self): |
|
723 | def save_sys_module_state(self): | |
740 | """Save the state of hooks in the sys module. |
|
724 | """Save the state of hooks in the sys module. | |
741 |
|
725 | |||
742 | This has to be called after self.user_module is created. |
|
726 | This has to be called after self.user_module is created. | |
743 | """ |
|
727 | """ | |
744 | self._orig_sys_module_state = {} |
|
728 | self._orig_sys_module_state = {} | |
745 | self._orig_sys_module_state['stdin'] = sys.stdin |
|
729 | self._orig_sys_module_state['stdin'] = sys.stdin | |
746 | self._orig_sys_module_state['stdout'] = sys.stdout |
|
730 | self._orig_sys_module_state['stdout'] = sys.stdout | |
747 | self._orig_sys_module_state['stderr'] = sys.stderr |
|
731 | self._orig_sys_module_state['stderr'] = sys.stderr | |
748 | self._orig_sys_module_state['excepthook'] = sys.excepthook |
|
732 | self._orig_sys_module_state['excepthook'] = sys.excepthook | |
749 | self._orig_sys_modules_main_name = self.user_module.__name__ |
|
733 | self._orig_sys_modules_main_name = self.user_module.__name__ | |
750 | self._orig_sys_modules_main_mod = sys.modules.get(self.user_module.__name__) |
|
734 | self._orig_sys_modules_main_mod = sys.modules.get(self.user_module.__name__) | |
751 |
|
735 | |||
752 | def restore_sys_module_state(self): |
|
736 | def restore_sys_module_state(self): | |
753 | """Restore the state of the sys module.""" |
|
737 | """Restore the state of the sys module.""" | |
754 | try: |
|
738 | try: | |
755 | for k, v in self._orig_sys_module_state.iteritems(): |
|
739 | for k, v in self._orig_sys_module_state.iteritems(): | |
756 | setattr(sys, k, v) |
|
740 | setattr(sys, k, v) | |
757 | except AttributeError: |
|
741 | except AttributeError: | |
758 | pass |
|
742 | pass | |
759 | # Reset what what done in self.init_sys_modules |
|
743 | # Reset what what done in self.init_sys_modules | |
760 | if self._orig_sys_modules_main_mod is not None: |
|
744 | if self._orig_sys_modules_main_mod is not None: | |
761 | sys.modules[self._orig_sys_modules_main_name] = self._orig_sys_modules_main_mod |
|
745 | sys.modules[self._orig_sys_modules_main_name] = self._orig_sys_modules_main_mod | |
762 |
|
746 | |||
763 | #------------------------------------------------------------------------- |
|
747 | #------------------------------------------------------------------------- | |
764 | # Things related to hooks |
|
748 | # Things related to hooks | |
765 | #------------------------------------------------------------------------- |
|
749 | #------------------------------------------------------------------------- | |
766 |
|
750 | |||
767 | def init_hooks(self): |
|
751 | def init_hooks(self): | |
768 | # hooks holds pointers used for user-side customizations |
|
752 | # hooks holds pointers used for user-side customizations | |
769 | self.hooks = Struct() |
|
753 | self.hooks = Struct() | |
770 |
|
754 | |||
771 | self.strdispatchers = {} |
|
755 | self.strdispatchers = {} | |
772 |
|
756 | |||
773 | # Set all default hooks, defined in the IPython.hooks module. |
|
757 | # Set all default hooks, defined in the IPython.hooks module. | |
774 | hooks = IPython.core.hooks |
|
758 | hooks = IPython.core.hooks | |
775 | for hook_name in hooks.__all__: |
|
759 | for hook_name in hooks.__all__: | |
776 | # default hooks have priority 100, i.e. low; user hooks should have |
|
760 | # default hooks have priority 100, i.e. low; user hooks should have | |
777 | # 0-100 priority |
|
761 | # 0-100 priority | |
778 | self.set_hook(hook_name,getattr(hooks,hook_name), 100) |
|
762 | self.set_hook(hook_name,getattr(hooks,hook_name), 100) | |
779 |
|
763 | |||
780 | def set_hook(self,name,hook, priority = 50, str_key = None, re_key = None): |
|
764 | def set_hook(self,name,hook, priority = 50, str_key = None, re_key = None): | |
781 | """set_hook(name,hook) -> sets an internal IPython hook. |
|
765 | """set_hook(name,hook) -> sets an internal IPython hook. | |
782 |
|
766 | |||
783 | IPython exposes some of its internal API as user-modifiable hooks. By |
|
767 | IPython exposes some of its internal API as user-modifiable hooks. By | |
784 | adding your function to one of these hooks, you can modify IPython's |
|
768 | adding your function to one of these hooks, you can modify IPython's | |
785 | behavior to call at runtime your own routines.""" |
|
769 | behavior to call at runtime your own routines.""" | |
786 |
|
770 | |||
787 | # At some point in the future, this should validate the hook before it |
|
771 | # At some point in the future, this should validate the hook before it | |
788 | # accepts it. Probably at least check that the hook takes the number |
|
772 | # accepts it. Probably at least check that the hook takes the number | |
789 | # of args it's supposed to. |
|
773 | # of args it's supposed to. | |
790 |
|
774 | |||
791 | f = types.MethodType(hook,self) |
|
775 | f = types.MethodType(hook,self) | |
792 |
|
776 | |||
793 | # check if the hook is for strdispatcher first |
|
777 | # check if the hook is for strdispatcher first | |
794 | if str_key is not None: |
|
778 | if str_key is not None: | |
795 | sdp = self.strdispatchers.get(name, StrDispatch()) |
|
779 | sdp = self.strdispatchers.get(name, StrDispatch()) | |
796 | sdp.add_s(str_key, f, priority ) |
|
780 | sdp.add_s(str_key, f, priority ) | |
797 | self.strdispatchers[name] = sdp |
|
781 | self.strdispatchers[name] = sdp | |
798 | return |
|
782 | return | |
799 | if re_key is not None: |
|
783 | if re_key is not None: | |
800 | sdp = self.strdispatchers.get(name, StrDispatch()) |
|
784 | sdp = self.strdispatchers.get(name, StrDispatch()) | |
801 | sdp.add_re(re.compile(re_key), f, priority ) |
|
785 | sdp.add_re(re.compile(re_key), f, priority ) | |
802 | self.strdispatchers[name] = sdp |
|
786 | self.strdispatchers[name] = sdp | |
803 | return |
|
787 | return | |
804 |
|
788 | |||
805 | dp = getattr(self.hooks, name, None) |
|
789 | dp = getattr(self.hooks, name, None) | |
806 | if name not in IPython.core.hooks.__all__: |
|
790 | if name not in IPython.core.hooks.__all__: | |
807 | print("Warning! Hook '%s' is not one of %s" % \ |
|
791 | print("Warning! Hook '%s' is not one of %s" % \ | |
808 | (name, IPython.core.hooks.__all__ )) |
|
792 | (name, IPython.core.hooks.__all__ )) | |
809 | if not dp: |
|
793 | if not dp: | |
810 | dp = IPython.core.hooks.CommandChainDispatcher() |
|
794 | dp = IPython.core.hooks.CommandChainDispatcher() | |
811 |
|
795 | |||
812 | try: |
|
796 | try: | |
813 | dp.add(f,priority) |
|
797 | dp.add(f,priority) | |
814 | except AttributeError: |
|
798 | except AttributeError: | |
815 | # it was not commandchain, plain old func - replace |
|
799 | # it was not commandchain, plain old func - replace | |
816 | dp = f |
|
800 | dp = f | |
817 |
|
801 | |||
818 | setattr(self.hooks,name, dp) |
|
802 | setattr(self.hooks,name, dp) | |
819 |
|
803 | |||
820 | def register_post_execute(self, func): |
|
804 | def register_post_execute(self, func): | |
821 | """Register a function for calling after code execution. |
|
805 | """Register a function for calling after code execution. | |
822 | """ |
|
806 | """ | |
823 | if not callable(func): |
|
807 | if not callable(func): | |
824 | raise ValueError('argument %s must be callable' % func) |
|
808 | raise ValueError('argument %s must be callable' % func) | |
825 | self._post_execute[func] = True |
|
809 | self._post_execute[func] = True | |
826 |
|
810 | |||
827 | #------------------------------------------------------------------------- |
|
811 | #------------------------------------------------------------------------- | |
828 | # Things related to the "main" module |
|
812 | # Things related to the "main" module | |
829 | #------------------------------------------------------------------------- |
|
813 | #------------------------------------------------------------------------- | |
830 |
|
814 | |||
831 | def new_main_mod(self,ns=None): |
|
815 | def new_main_mod(self,ns=None): | |
832 | """Return a new 'main' module object for user code execution. |
|
816 | """Return a new 'main' module object for user code execution. | |
833 | """ |
|
817 | """ | |
834 | main_mod = self._user_main_module |
|
818 | main_mod = self._user_main_module | |
835 | init_fakemod_dict(main_mod,ns) |
|
819 | init_fakemod_dict(main_mod,ns) | |
836 | return main_mod |
|
820 | return main_mod | |
837 |
|
821 | |||
838 | def cache_main_mod(self,ns,fname): |
|
822 | def cache_main_mod(self,ns,fname): | |
839 | """Cache a main module's namespace. |
|
823 | """Cache a main module's namespace. | |
840 |
|
824 | |||
841 | When scripts are executed via %run, we must keep a reference to the |
|
825 | When scripts are executed via %run, we must keep a reference to the | |
842 | namespace of their __main__ module (a FakeModule instance) around so |
|
826 | namespace of their __main__ module (a FakeModule instance) around so | |
843 | that Python doesn't clear it, rendering objects defined therein |
|
827 | that Python doesn't clear it, rendering objects defined therein | |
844 | useless. |
|
828 | useless. | |
845 |
|
829 | |||
846 | This method keeps said reference in a private dict, keyed by the |
|
830 | This method keeps said reference in a private dict, keyed by the | |
847 | absolute path of the module object (which corresponds to the script |
|
831 | absolute path of the module object (which corresponds to the script | |
848 | path). This way, for multiple executions of the same script we only |
|
832 | path). This way, for multiple executions of the same script we only | |
849 | keep one copy of the namespace (the last one), thus preventing memory |
|
833 | keep one copy of the namespace (the last one), thus preventing memory | |
850 | leaks from old references while allowing the objects from the last |
|
834 | leaks from old references while allowing the objects from the last | |
851 | execution to be accessible. |
|
835 | execution to be accessible. | |
852 |
|
836 | |||
853 | Note: we can not allow the actual FakeModule instances to be deleted, |
|
837 | Note: we can not allow the actual FakeModule instances to be deleted, | |
854 | because of how Python tears down modules (it hard-sets all their |
|
838 | because of how Python tears down modules (it hard-sets all their | |
855 | references to None without regard for reference counts). This method |
|
839 | references to None without regard for reference counts). This method | |
856 | must therefore make a *copy* of the given namespace, to allow the |
|
840 | must therefore make a *copy* of the given namespace, to allow the | |
857 | original module's __dict__ to be cleared and reused. |
|
841 | original module's __dict__ to be cleared and reused. | |
858 |
|
842 | |||
859 |
|
843 | |||
860 | Parameters |
|
844 | Parameters | |
861 | ---------- |
|
845 | ---------- | |
862 | ns : a namespace (a dict, typically) |
|
846 | ns : a namespace (a dict, typically) | |
863 |
|
847 | |||
864 | fname : str |
|
848 | fname : str | |
865 | Filename associated with the namespace. |
|
849 | Filename associated with the namespace. | |
866 |
|
850 | |||
867 | Examples |
|
851 | Examples | |
868 | -------- |
|
852 | -------- | |
869 |
|
853 | |||
870 | In [10]: import IPython |
|
854 | In [10]: import IPython | |
871 |
|
855 | |||
872 | In [11]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__) |
|
856 | In [11]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__) | |
873 |
|
857 | |||
874 | In [12]: IPython.__file__ in _ip._main_ns_cache |
|
858 | In [12]: IPython.__file__ in _ip._main_ns_cache | |
875 | Out[12]: True |
|
859 | Out[12]: True | |
876 | """ |
|
860 | """ | |
877 | self._main_ns_cache[os.path.abspath(fname)] = ns.copy() |
|
861 | self._main_ns_cache[os.path.abspath(fname)] = ns.copy() | |
878 |
|
862 | |||
879 | def clear_main_mod_cache(self): |
|
863 | def clear_main_mod_cache(self): | |
880 | """Clear the cache of main modules. |
|
864 | """Clear the cache of main modules. | |
881 |
|
865 | |||
882 | Mainly for use by utilities like %reset. |
|
866 | Mainly for use by utilities like %reset. | |
883 |
|
867 | |||
884 | Examples |
|
868 | Examples | |
885 | -------- |
|
869 | -------- | |
886 |
|
870 | |||
887 | In [15]: import IPython |
|
871 | In [15]: import IPython | |
888 |
|
872 | |||
889 | In [16]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__) |
|
873 | In [16]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__) | |
890 |
|
874 | |||
891 | In [17]: len(_ip._main_ns_cache) > 0 |
|
875 | In [17]: len(_ip._main_ns_cache) > 0 | |
892 | Out[17]: True |
|
876 | Out[17]: True | |
893 |
|
877 | |||
894 | In [18]: _ip.clear_main_mod_cache() |
|
878 | In [18]: _ip.clear_main_mod_cache() | |
895 |
|
879 | |||
896 | In [19]: len(_ip._main_ns_cache) == 0 |
|
880 | In [19]: len(_ip._main_ns_cache) == 0 | |
897 | Out[19]: True |
|
881 | Out[19]: True | |
898 | """ |
|
882 | """ | |
899 | self._main_ns_cache.clear() |
|
883 | self._main_ns_cache.clear() | |
900 |
|
884 | |||
901 | #------------------------------------------------------------------------- |
|
885 | #------------------------------------------------------------------------- | |
902 | # Things related to debugging |
|
886 | # Things related to debugging | |
903 | #------------------------------------------------------------------------- |
|
887 | #------------------------------------------------------------------------- | |
904 |
|
888 | |||
905 | def init_pdb(self): |
|
889 | def init_pdb(self): | |
906 | # Set calling of pdb on exceptions |
|
890 | # Set calling of pdb on exceptions | |
907 | # self.call_pdb is a property |
|
891 | # self.call_pdb is a property | |
908 | self.call_pdb = self.pdb |
|
892 | self.call_pdb = self.pdb | |
909 |
|
893 | |||
910 | def _get_call_pdb(self): |
|
894 | def _get_call_pdb(self): | |
911 | return self._call_pdb |
|
895 | return self._call_pdb | |
912 |
|
896 | |||
913 | def _set_call_pdb(self,val): |
|
897 | def _set_call_pdb(self,val): | |
914 |
|
898 | |||
915 | if val not in (0,1,False,True): |
|
899 | if val not in (0,1,False,True): | |
916 | raise ValueError('new call_pdb value must be boolean') |
|
900 | raise ValueError('new call_pdb value must be boolean') | |
917 |
|
901 | |||
918 | # store value in instance |
|
902 | # store value in instance | |
919 | self._call_pdb = val |
|
903 | self._call_pdb = val | |
920 |
|
904 | |||
921 | # notify the actual exception handlers |
|
905 | # notify the actual exception handlers | |
922 | self.InteractiveTB.call_pdb = val |
|
906 | self.InteractiveTB.call_pdb = val | |
923 |
|
907 | |||
924 | call_pdb = property(_get_call_pdb,_set_call_pdb,None, |
|
908 | call_pdb = property(_get_call_pdb,_set_call_pdb,None, | |
925 | 'Control auto-activation of pdb at exceptions') |
|
909 | 'Control auto-activation of pdb at exceptions') | |
926 |
|
910 | |||
927 | def debugger(self,force=False): |
|
911 | def debugger(self,force=False): | |
928 | """Call the pydb/pdb debugger. |
|
912 | """Call the pydb/pdb debugger. | |
929 |
|
913 | |||
930 | Keywords: |
|
914 | Keywords: | |
931 |
|
915 | |||
932 | - force(False): by default, this routine checks the instance call_pdb |
|
916 | - force(False): by default, this routine checks the instance call_pdb | |
933 | flag and does not actually invoke the debugger if the flag is false. |
|
917 | flag and does not actually invoke the debugger if the flag is false. | |
934 | The 'force' option forces the debugger to activate even if the flag |
|
918 | The 'force' option forces the debugger to activate even if the flag | |
935 | is false. |
|
919 | is false. | |
936 | """ |
|
920 | """ | |
937 |
|
921 | |||
938 | if not (force or self.call_pdb): |
|
922 | if not (force or self.call_pdb): | |
939 | return |
|
923 | return | |
940 |
|
924 | |||
941 | if not hasattr(sys,'last_traceback'): |
|
925 | if not hasattr(sys,'last_traceback'): | |
942 | error('No traceback has been produced, nothing to debug.') |
|
926 | error('No traceback has been produced, nothing to debug.') | |
943 | return |
|
927 | return | |
944 |
|
928 | |||
945 | # use pydb if available |
|
929 | # use pydb if available | |
946 | if debugger.has_pydb: |
|
930 | if debugger.has_pydb: | |
947 | from pydb import pm |
|
931 | from pydb import pm | |
948 | else: |
|
932 | else: | |
949 | # fallback to our internal debugger |
|
933 | # fallback to our internal debugger | |
950 | pm = lambda : self.InteractiveTB.debugger(force=True) |
|
934 | pm = lambda : self.InteractiveTB.debugger(force=True) | |
951 |
|
935 | |||
952 | with self.readline_no_record: |
|
936 | with self.readline_no_record: | |
953 | pm() |
|
937 | pm() | |
954 |
|
938 | |||
955 | #------------------------------------------------------------------------- |
|
939 | #------------------------------------------------------------------------- | |
956 | # Things related to IPython's various namespaces |
|
940 | # Things related to IPython's various namespaces | |
957 | #------------------------------------------------------------------------- |
|
941 | #------------------------------------------------------------------------- | |
958 | default_user_namespaces = True |
|
942 | default_user_namespaces = True | |
959 |
|
943 | |||
960 | def init_create_namespaces(self, user_module=None, user_ns=None): |
|
944 | def init_create_namespaces(self, user_module=None, user_ns=None): | |
961 | # Create the namespace where the user will operate. user_ns is |
|
945 | # Create the namespace where the user will operate. user_ns is | |
962 | # normally the only one used, and it is passed to the exec calls as |
|
946 | # normally the only one used, and it is passed to the exec calls as | |
963 | # the locals argument. But we do carry a user_global_ns namespace |
|
947 | # the locals argument. But we do carry a user_global_ns namespace | |
964 | # given as the exec 'globals' argument, This is useful in embedding |
|
948 | # given as the exec 'globals' argument, This is useful in embedding | |
965 | # situations where the ipython shell opens in a context where the |
|
949 | # situations where the ipython shell opens in a context where the | |
966 | # distinction between locals and globals is meaningful. For |
|
950 | # distinction between locals and globals is meaningful. For | |
967 | # non-embedded contexts, it is just the same object as the user_ns dict. |
|
951 | # non-embedded contexts, it is just the same object as the user_ns dict. | |
968 |
|
952 | |||
969 | # FIXME. For some strange reason, __builtins__ is showing up at user |
|
953 | # FIXME. For some strange reason, __builtins__ is showing up at user | |
970 | # level as a dict instead of a module. This is a manual fix, but I |
|
954 | # level as a dict instead of a module. This is a manual fix, but I | |
971 | # should really track down where the problem is coming from. Alex |
|
955 | # should really track down where the problem is coming from. Alex | |
972 | # Schmolck reported this problem first. |
|
956 | # Schmolck reported this problem first. | |
973 |
|
957 | |||
974 | # A useful post by Alex Martelli on this topic: |
|
958 | # A useful post by Alex Martelli on this topic: | |
975 | # Re: inconsistent value from __builtins__ |
|
959 | # Re: inconsistent value from __builtins__ | |
976 | # Von: Alex Martelli <aleaxit@yahoo.com> |
|
960 | # Von: Alex Martelli <aleaxit@yahoo.com> | |
977 | # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends |
|
961 | # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends | |
978 | # Gruppen: comp.lang.python |
|
962 | # Gruppen: comp.lang.python | |
979 |
|
963 | |||
980 | # Michael Hohn <hohn@hooknose.lbl.gov> wrote: |
|
964 | # Michael Hohn <hohn@hooknose.lbl.gov> wrote: | |
981 | # > >>> print type(builtin_check.get_global_binding('__builtins__')) |
|
965 | # > >>> print type(builtin_check.get_global_binding('__builtins__')) | |
982 | # > <type 'dict'> |
|
966 | # > <type 'dict'> | |
983 | # > >>> print type(__builtins__) |
|
967 | # > >>> print type(__builtins__) | |
984 | # > <type 'module'> |
|
968 | # > <type 'module'> | |
985 | # > Is this difference in return value intentional? |
|
969 | # > Is this difference in return value intentional? | |
986 |
|
970 | |||
987 | # Well, it's documented that '__builtins__' can be either a dictionary |
|
971 | # Well, it's documented that '__builtins__' can be either a dictionary | |
988 | # or a module, and it's been that way for a long time. Whether it's |
|
972 | # or a module, and it's been that way for a long time. Whether it's | |
989 | # intentional (or sensible), I don't know. In any case, the idea is |
|
973 | # intentional (or sensible), I don't know. In any case, the idea is | |
990 | # that if you need to access the built-in namespace directly, you |
|
974 | # that if you need to access the built-in namespace directly, you | |
991 | # should start with "import __builtin__" (note, no 's') which will |
|
975 | # should start with "import __builtin__" (note, no 's') which will | |
992 | # definitely give you a module. Yeah, it's somewhat confusing:-(. |
|
976 | # definitely give you a module. Yeah, it's somewhat confusing:-(. | |
993 |
|
977 | |||
994 | # These routines return a properly built module and dict as needed by |
|
978 | # These routines return a properly built module and dict as needed by | |
995 | # the rest of the code, and can also be used by extension writers to |
|
979 | # the rest of the code, and can also be used by extension writers to | |
996 | # generate properly initialized namespaces. |
|
980 | # generate properly initialized namespaces. | |
997 | if (user_ns is not None) or (user_module is not None): |
|
981 | if (user_ns is not None) or (user_module is not None): | |
998 | self.default_user_namespaces = False |
|
982 | self.default_user_namespaces = False | |
999 | self.user_module, self.user_ns = self.prepare_user_module(user_module, user_ns) |
|
983 | self.user_module, self.user_ns = self.prepare_user_module(user_module, user_ns) | |
1000 |
|
984 | |||
1001 | # A record of hidden variables we have added to the user namespace, so |
|
985 | # A record of hidden variables we have added to the user namespace, so | |
1002 | # we can list later only variables defined in actual interactive use. |
|
986 | # we can list later only variables defined in actual interactive use. | |
1003 | self.user_ns_hidden = set() |
|
987 | self.user_ns_hidden = set() | |
1004 |
|
988 | |||
1005 | # Now that FakeModule produces a real module, we've run into a nasty |
|
989 | # Now that FakeModule produces a real module, we've run into a nasty | |
1006 | # problem: after script execution (via %run), the module where the user |
|
990 | # problem: after script execution (via %run), the module where the user | |
1007 | # code ran is deleted. Now that this object is a true module (needed |
|
991 | # code ran is deleted. Now that this object is a true module (needed | |
1008 | # so docetst and other tools work correctly), the Python module |
|
992 | # so docetst and other tools work correctly), the Python module | |
1009 | # teardown mechanism runs over it, and sets to None every variable |
|
993 | # teardown mechanism runs over it, and sets to None every variable | |
1010 | # present in that module. Top-level references to objects from the |
|
994 | # present in that module. Top-level references to objects from the | |
1011 | # script survive, because the user_ns is updated with them. However, |
|
995 | # script survive, because the user_ns is updated with them. However, | |
1012 | # calling functions defined in the script that use other things from |
|
996 | # calling functions defined in the script that use other things from | |
1013 | # the script will fail, because the function's closure had references |
|
997 | # the script will fail, because the function's closure had references | |
1014 | # to the original objects, which are now all None. So we must protect |
|
998 | # to the original objects, which are now all None. So we must protect | |
1015 | # these modules from deletion by keeping a cache. |
|
999 | # these modules from deletion by keeping a cache. | |
1016 | # |
|
1000 | # | |
1017 | # To avoid keeping stale modules around (we only need the one from the |
|
1001 | # To avoid keeping stale modules around (we only need the one from the | |
1018 | # last run), we use a dict keyed with the full path to the script, so |
|
1002 | # last run), we use a dict keyed with the full path to the script, so | |
1019 | # only the last version of the module is held in the cache. Note, |
|
1003 | # only the last version of the module is held in the cache. Note, | |
1020 | # however, that we must cache the module *namespace contents* (their |
|
1004 | # however, that we must cache the module *namespace contents* (their | |
1021 | # __dict__). Because if we try to cache the actual modules, old ones |
|
1005 | # __dict__). Because if we try to cache the actual modules, old ones | |
1022 | # (uncached) could be destroyed while still holding references (such as |
|
1006 | # (uncached) could be destroyed while still holding references (such as | |
1023 | # those held by GUI objects that tend to be long-lived)> |
|
1007 | # those held by GUI objects that tend to be long-lived)> | |
1024 | # |
|
1008 | # | |
1025 | # The %reset command will flush this cache. See the cache_main_mod() |
|
1009 | # The %reset command will flush this cache. See the cache_main_mod() | |
1026 | # and clear_main_mod_cache() methods for details on use. |
|
1010 | # and clear_main_mod_cache() methods for details on use. | |
1027 |
|
1011 | |||
1028 | # This is the cache used for 'main' namespaces |
|
1012 | # This is the cache used for 'main' namespaces | |
1029 | self._main_ns_cache = {} |
|
1013 | self._main_ns_cache = {} | |
1030 | # And this is the single instance of FakeModule whose __dict__ we keep |
|
1014 | # And this is the single instance of FakeModule whose __dict__ we keep | |
1031 | # copying and clearing for reuse on each %run |
|
1015 | # copying and clearing for reuse on each %run | |
1032 | self._user_main_module = FakeModule() |
|
1016 | self._user_main_module = FakeModule() | |
1033 |
|
1017 | |||
1034 | # A table holding all the namespaces IPython deals with, so that |
|
1018 | # A table holding all the namespaces IPython deals with, so that | |
1035 | # introspection facilities can search easily. |
|
1019 | # introspection facilities can search easily. | |
1036 | self.ns_table = {'user_global':self.user_module.__dict__, |
|
1020 | self.ns_table = {'user_global':self.user_module.__dict__, | |
1037 | 'user_local':self.user_ns, |
|
1021 | 'user_local':self.user_ns, | |
1038 | 'builtin':builtin_mod.__dict__ |
|
1022 | 'builtin':builtin_mod.__dict__ | |
1039 | } |
|
1023 | } | |
1040 |
|
1024 | |||
1041 | @property |
|
1025 | @property | |
1042 | def user_global_ns(self): |
|
1026 | def user_global_ns(self): | |
1043 | return self.user_module.__dict__ |
|
1027 | return self.user_module.__dict__ | |
1044 |
|
1028 | |||
1045 | def prepare_user_module(self, user_module=None, user_ns=None): |
|
1029 | def prepare_user_module(self, user_module=None, user_ns=None): | |
1046 | """Prepare the module and namespace in which user code will be run. |
|
1030 | """Prepare the module and namespace in which user code will be run. | |
1047 |
|
1031 | |||
1048 | When IPython is started normally, both parameters are None: a new module |
|
1032 | When IPython is started normally, both parameters are None: a new module | |
1049 | is created automatically, and its __dict__ used as the namespace. |
|
1033 | is created automatically, and its __dict__ used as the namespace. | |
1050 |
|
1034 | |||
1051 | If only user_module is provided, its __dict__ is used as the namespace. |
|
1035 | If only user_module is provided, its __dict__ is used as the namespace. | |
1052 | If only user_ns is provided, a dummy module is created, and user_ns |
|
1036 | If only user_ns is provided, a dummy module is created, and user_ns | |
1053 | becomes the global namespace. If both are provided (as they may be |
|
1037 | becomes the global namespace. If both are provided (as they may be | |
1054 | when embedding), user_ns is the local namespace, and user_module |
|
1038 | when embedding), user_ns is the local namespace, and user_module | |
1055 | provides the global namespace. |
|
1039 | provides the global namespace. | |
1056 |
|
1040 | |||
1057 | Parameters |
|
1041 | Parameters | |
1058 | ---------- |
|
1042 | ---------- | |
1059 | user_module : module, optional |
|
1043 | user_module : module, optional | |
1060 | The current user module in which IPython is being run. If None, |
|
1044 | The current user module in which IPython is being run. If None, | |
1061 | a clean module will be created. |
|
1045 | a clean module will be created. | |
1062 | user_ns : dict, optional |
|
1046 | user_ns : dict, optional | |
1063 | A namespace in which to run interactive commands. |
|
1047 | A namespace in which to run interactive commands. | |
1064 |
|
1048 | |||
1065 | Returns |
|
1049 | Returns | |
1066 | ------- |
|
1050 | ------- | |
1067 | A tuple of user_module and user_ns, each properly initialised. |
|
1051 | A tuple of user_module and user_ns, each properly initialised. | |
1068 | """ |
|
1052 | """ | |
1069 | if user_module is None and user_ns is not None: |
|
1053 | if user_module is None and user_ns is not None: | |
1070 | user_ns.setdefault("__name__", "__main__") |
|
1054 | user_ns.setdefault("__name__", "__main__") | |
1071 | class DummyMod(object): |
|
1055 | class DummyMod(object): | |
1072 | "A dummy module used for IPython's interactive namespace." |
|
1056 | "A dummy module used for IPython's interactive namespace." | |
1073 | pass |
|
1057 | pass | |
1074 | user_module = DummyMod() |
|
1058 | user_module = DummyMod() | |
1075 | user_module.__dict__ = user_ns |
|
1059 | user_module.__dict__ = user_ns | |
1076 |
|
1060 | |||
1077 | if user_module is None: |
|
1061 | if user_module is None: | |
1078 | user_module = types.ModuleType("__main__", |
|
1062 | user_module = types.ModuleType("__main__", | |
1079 | doc="Automatically created module for IPython interactive environment") |
|
1063 | doc="Automatically created module for IPython interactive environment") | |
1080 |
|
1064 | |||
1081 | # We must ensure that __builtin__ (without the final 's') is always |
|
1065 | # We must ensure that __builtin__ (without the final 's') is always | |
1082 | # available and pointing to the __builtin__ *module*. For more details: |
|
1066 | # available and pointing to the __builtin__ *module*. For more details: | |
1083 | # http://mail.python.org/pipermail/python-dev/2001-April/014068.html |
|
1067 | # http://mail.python.org/pipermail/python-dev/2001-April/014068.html | |
1084 | user_module.__dict__.setdefault('__builtin__', builtin_mod) |
|
1068 | user_module.__dict__.setdefault('__builtin__', builtin_mod) | |
1085 | user_module.__dict__.setdefault('__builtins__', builtin_mod) |
|
1069 | user_module.__dict__.setdefault('__builtins__', builtin_mod) | |
1086 |
|
1070 | |||
1087 | if user_ns is None: |
|
1071 | if user_ns is None: | |
1088 | user_ns = user_module.__dict__ |
|
1072 | user_ns = user_module.__dict__ | |
1089 |
|
1073 | |||
1090 | return user_module, user_ns |
|
1074 | return user_module, user_ns | |
1091 |
|
1075 | |||
1092 | def init_sys_modules(self): |
|
1076 | def init_sys_modules(self): | |
1093 | # We need to insert into sys.modules something that looks like a |
|
1077 | # We need to insert into sys.modules something that looks like a | |
1094 | # module but which accesses the IPython namespace, for shelve and |
|
1078 | # module but which accesses the IPython namespace, for shelve and | |
1095 | # pickle to work interactively. Normally they rely on getting |
|
1079 | # pickle to work interactively. Normally they rely on getting | |
1096 | # everything out of __main__, but for embedding purposes each IPython |
|
1080 | # everything out of __main__, but for embedding purposes each IPython | |
1097 | # instance has its own private namespace, so we can't go shoving |
|
1081 | # instance has its own private namespace, so we can't go shoving | |
1098 | # everything into __main__. |
|
1082 | # everything into __main__. | |
1099 |
|
1083 | |||
1100 | # note, however, that we should only do this for non-embedded |
|
1084 | # note, however, that we should only do this for non-embedded | |
1101 | # ipythons, which really mimic the __main__.__dict__ with their own |
|
1085 | # ipythons, which really mimic the __main__.__dict__ with their own | |
1102 | # namespace. Embedded instances, on the other hand, should not do |
|
1086 | # namespace. Embedded instances, on the other hand, should not do | |
1103 | # this because they need to manage the user local/global namespaces |
|
1087 | # this because they need to manage the user local/global namespaces | |
1104 | # only, but they live within a 'normal' __main__ (meaning, they |
|
1088 | # only, but they live within a 'normal' __main__ (meaning, they | |
1105 | # shouldn't overtake the execution environment of the script they're |
|
1089 | # shouldn't overtake the execution environment of the script they're | |
1106 | # embedded in). |
|
1090 | # embedded in). | |
1107 |
|
1091 | |||
1108 | # This is overridden in the InteractiveShellEmbed subclass to a no-op. |
|
1092 | # This is overridden in the InteractiveShellEmbed subclass to a no-op. | |
1109 | main_name = self.user_module.__name__ |
|
1093 | main_name = self.user_module.__name__ | |
1110 | sys.modules[main_name] = self.user_module |
|
1094 | sys.modules[main_name] = self.user_module | |
1111 |
|
1095 | |||
1112 | def init_user_ns(self): |
|
1096 | def init_user_ns(self): | |
1113 | """Initialize all user-visible namespaces to their minimum defaults. |
|
1097 | """Initialize all user-visible namespaces to their minimum defaults. | |
1114 |
|
1098 | |||
1115 | Certain history lists are also initialized here, as they effectively |
|
1099 | Certain history lists are also initialized here, as they effectively | |
1116 | act as user namespaces. |
|
1100 | act as user namespaces. | |
1117 |
|
1101 | |||
1118 | Notes |
|
1102 | Notes | |
1119 | ----- |
|
1103 | ----- | |
1120 | All data structures here are only filled in, they are NOT reset by this |
|
1104 | All data structures here are only filled in, they are NOT reset by this | |
1121 | method. If they were not empty before, data will simply be added to |
|
1105 | method. If they were not empty before, data will simply be added to | |
1122 | therm. |
|
1106 | therm. | |
1123 | """ |
|
1107 | """ | |
1124 | # This function works in two parts: first we put a few things in |
|
1108 | # This function works in two parts: first we put a few things in | |
1125 | # user_ns, and we sync that contents into user_ns_hidden so that these |
|
1109 | # user_ns, and we sync that contents into user_ns_hidden so that these | |
1126 | # initial variables aren't shown by %who. After the sync, we add the |
|
1110 | # initial variables aren't shown by %who. After the sync, we add the | |
1127 | # rest of what we *do* want the user to see with %who even on a new |
|
1111 | # rest of what we *do* want the user to see with %who even on a new | |
1128 | # session (probably nothing, so theye really only see their own stuff) |
|
1112 | # session (probably nothing, so theye really only see their own stuff) | |
1129 |
|
1113 | |||
1130 | # The user dict must *always* have a __builtin__ reference to the |
|
1114 | # The user dict must *always* have a __builtin__ reference to the | |
1131 | # Python standard __builtin__ namespace, which must be imported. |
|
1115 | # Python standard __builtin__ namespace, which must be imported. | |
1132 | # This is so that certain operations in prompt evaluation can be |
|
1116 | # This is so that certain operations in prompt evaluation can be | |
1133 | # reliably executed with builtins. Note that we can NOT use |
|
1117 | # reliably executed with builtins. Note that we can NOT use | |
1134 | # __builtins__ (note the 's'), because that can either be a dict or a |
|
1118 | # __builtins__ (note the 's'), because that can either be a dict or a | |
1135 | # module, and can even mutate at runtime, depending on the context |
|
1119 | # module, and can even mutate at runtime, depending on the context | |
1136 | # (Python makes no guarantees on it). In contrast, __builtin__ is |
|
1120 | # (Python makes no guarantees on it). In contrast, __builtin__ is | |
1137 | # always a module object, though it must be explicitly imported. |
|
1121 | # always a module object, though it must be explicitly imported. | |
1138 |
|
1122 | |||
1139 | # For more details: |
|
1123 | # For more details: | |
1140 | # http://mail.python.org/pipermail/python-dev/2001-April/014068.html |
|
1124 | # http://mail.python.org/pipermail/python-dev/2001-April/014068.html | |
1141 | ns = dict() |
|
1125 | ns = dict() | |
1142 |
|
1126 | |||
1143 | # Put 'help' in the user namespace |
|
1127 | # Put 'help' in the user namespace | |
1144 | try: |
|
1128 | try: | |
1145 | from site import _Helper |
|
1129 | from site import _Helper | |
1146 | ns['help'] = _Helper() |
|
1130 | ns['help'] = _Helper() | |
1147 | except ImportError: |
|
1131 | except ImportError: | |
1148 | warn('help() not available - check site.py') |
|
1132 | warn('help() not available - check site.py') | |
1149 |
|
1133 | |||
1150 | # make global variables for user access to the histories |
|
1134 | # make global variables for user access to the histories | |
1151 | ns['_ih'] = self.history_manager.input_hist_parsed |
|
1135 | ns['_ih'] = self.history_manager.input_hist_parsed | |
1152 | ns['_oh'] = self.history_manager.output_hist |
|
1136 | ns['_oh'] = self.history_manager.output_hist | |
1153 | ns['_dh'] = self.history_manager.dir_hist |
|
1137 | ns['_dh'] = self.history_manager.dir_hist | |
1154 |
|
1138 | |||
1155 | ns['_sh'] = shadowns |
|
1139 | ns['_sh'] = shadowns | |
1156 |
|
1140 | |||
1157 | # user aliases to input and output histories. These shouldn't show up |
|
1141 | # user aliases to input and output histories. These shouldn't show up | |
1158 | # in %who, as they can have very large reprs. |
|
1142 | # in %who, as they can have very large reprs. | |
1159 | ns['In'] = self.history_manager.input_hist_parsed |
|
1143 | ns['In'] = self.history_manager.input_hist_parsed | |
1160 | ns['Out'] = self.history_manager.output_hist |
|
1144 | ns['Out'] = self.history_manager.output_hist | |
1161 |
|
1145 | |||
1162 | # Store myself as the public api!!! |
|
1146 | # Store myself as the public api!!! | |
1163 | ns['get_ipython'] = self.get_ipython |
|
1147 | ns['get_ipython'] = self.get_ipython | |
1164 |
|
1148 | |||
1165 | ns['exit'] = self.exiter |
|
1149 | ns['exit'] = self.exiter | |
1166 | ns['quit'] = self.exiter |
|
1150 | ns['quit'] = self.exiter | |
1167 |
|
1151 | |||
1168 | # Sync what we've added so far to user_ns_hidden so these aren't seen |
|
1152 | # Sync what we've added so far to user_ns_hidden so these aren't seen | |
1169 | # by %who |
|
1153 | # by %who | |
1170 | self.user_ns_hidden.update(ns) |
|
1154 | self.user_ns_hidden.update(ns) | |
1171 |
|
1155 | |||
1172 | # Anything put into ns now would show up in %who. Think twice before |
|
1156 | # Anything put into ns now would show up in %who. Think twice before | |
1173 | # putting anything here, as we really want %who to show the user their |
|
1157 | # putting anything here, as we really want %who to show the user their | |
1174 | # stuff, not our variables. |
|
1158 | # stuff, not our variables. | |
1175 |
|
1159 | |||
1176 | # Finally, update the real user's namespace |
|
1160 | # Finally, update the real user's namespace | |
1177 | self.user_ns.update(ns) |
|
1161 | self.user_ns.update(ns) | |
1178 |
|
1162 | |||
1179 | @property |
|
1163 | @property | |
1180 | def all_ns_refs(self): |
|
1164 | def all_ns_refs(self): | |
1181 | """Get a list of references to all the namespace dictionaries in which |
|
1165 | """Get a list of references to all the namespace dictionaries in which | |
1182 | IPython might store a user-created object. |
|
1166 | IPython might store a user-created object. | |
1183 |
|
1167 | |||
1184 | Note that this does not include the displayhook, which also caches |
|
1168 | Note that this does not include the displayhook, which also caches | |
1185 | objects from the output.""" |
|
1169 | objects from the output.""" | |
1186 | return [self.user_ns, self.user_global_ns, |
|
1170 | return [self.user_ns, self.user_global_ns, | |
1187 | self._user_main_module.__dict__] + self._main_ns_cache.values() |
|
1171 | self._user_main_module.__dict__] + self._main_ns_cache.values() | |
1188 |
|
1172 | |||
1189 | def reset(self, new_session=True): |
|
1173 | def reset(self, new_session=True): | |
1190 | """Clear all internal namespaces, and attempt to release references to |
|
1174 | """Clear all internal namespaces, and attempt to release references to | |
1191 | user objects. |
|
1175 | user objects. | |
1192 |
|
1176 | |||
1193 | If new_session is True, a new history session will be opened. |
|
1177 | If new_session is True, a new history session will be opened. | |
1194 | """ |
|
1178 | """ | |
1195 | # Clear histories |
|
1179 | # Clear histories | |
1196 | self.history_manager.reset(new_session) |
|
1180 | self.history_manager.reset(new_session) | |
1197 | # Reset counter used to index all histories |
|
1181 | # Reset counter used to index all histories | |
1198 | if new_session: |
|
1182 | if new_session: | |
1199 | self.execution_count = 1 |
|
1183 | self.execution_count = 1 | |
1200 |
|
1184 | |||
1201 | # Flush cached output items |
|
1185 | # Flush cached output items | |
1202 | if self.displayhook.do_full_cache: |
|
1186 | if self.displayhook.do_full_cache: | |
1203 | self.displayhook.flush() |
|
1187 | self.displayhook.flush() | |
1204 |
|
1188 | |||
1205 | # The main execution namespaces must be cleared very carefully, |
|
1189 | # The main execution namespaces must be cleared very carefully, | |
1206 | # skipping the deletion of the builtin-related keys, because doing so |
|
1190 | # skipping the deletion of the builtin-related keys, because doing so | |
1207 | # would cause errors in many object's __del__ methods. |
|
1191 | # would cause errors in many object's __del__ methods. | |
1208 | if self.user_ns is not self.user_global_ns: |
|
1192 | if self.user_ns is not self.user_global_ns: | |
1209 | self.user_ns.clear() |
|
1193 | self.user_ns.clear() | |
1210 | ns = self.user_global_ns |
|
1194 | ns = self.user_global_ns | |
1211 | drop_keys = set(ns.keys()) |
|
1195 | drop_keys = set(ns.keys()) | |
1212 | drop_keys.discard('__builtin__') |
|
1196 | drop_keys.discard('__builtin__') | |
1213 | drop_keys.discard('__builtins__') |
|
1197 | drop_keys.discard('__builtins__') | |
1214 | drop_keys.discard('__name__') |
|
1198 | drop_keys.discard('__name__') | |
1215 | for k in drop_keys: |
|
1199 | for k in drop_keys: | |
1216 | del ns[k] |
|
1200 | del ns[k] | |
1217 |
|
1201 | |||
1218 | self.user_ns_hidden.clear() |
|
1202 | self.user_ns_hidden.clear() | |
1219 |
|
1203 | |||
1220 | # Restore the user namespaces to minimal usability |
|
1204 | # Restore the user namespaces to minimal usability | |
1221 | self.init_user_ns() |
|
1205 | self.init_user_ns() | |
1222 |
|
1206 | |||
1223 | # Restore the default and user aliases |
|
1207 | # Restore the default and user aliases | |
1224 | self.alias_manager.clear_aliases() |
|
1208 | self.alias_manager.clear_aliases() | |
1225 | self.alias_manager.init_aliases() |
|
1209 | self.alias_manager.init_aliases() | |
1226 |
|
1210 | |||
1227 | # Flush the private list of module references kept for script |
|
1211 | # Flush the private list of module references kept for script | |
1228 | # execution protection |
|
1212 | # execution protection | |
1229 | self.clear_main_mod_cache() |
|
1213 | self.clear_main_mod_cache() | |
1230 |
|
1214 | |||
1231 | # Clear out the namespace from the last %run |
|
1215 | # Clear out the namespace from the last %run | |
1232 | self.new_main_mod() |
|
1216 | self.new_main_mod() | |
1233 |
|
1217 | |||
1234 | def del_var(self, varname, by_name=False): |
|
1218 | def del_var(self, varname, by_name=False): | |
1235 | """Delete a variable from the various namespaces, so that, as |
|
1219 | """Delete a variable from the various namespaces, so that, as | |
1236 | far as possible, we're not keeping any hidden references to it. |
|
1220 | far as possible, we're not keeping any hidden references to it. | |
1237 |
|
1221 | |||
1238 | Parameters |
|
1222 | Parameters | |
1239 | ---------- |
|
1223 | ---------- | |
1240 | varname : str |
|
1224 | varname : str | |
1241 | The name of the variable to delete. |
|
1225 | The name of the variable to delete. | |
1242 | by_name : bool |
|
1226 | by_name : bool | |
1243 | If True, delete variables with the given name in each |
|
1227 | If True, delete variables with the given name in each | |
1244 | namespace. If False (default), find the variable in the user |
|
1228 | namespace. If False (default), find the variable in the user | |
1245 | namespace, and delete references to it. |
|
1229 | namespace, and delete references to it. | |
1246 | """ |
|
1230 | """ | |
1247 | if varname in ('__builtin__', '__builtins__'): |
|
1231 | if varname in ('__builtin__', '__builtins__'): | |
1248 | raise ValueError("Refusing to delete %s" % varname) |
|
1232 | raise ValueError("Refusing to delete %s" % varname) | |
1249 |
|
1233 | |||
1250 | ns_refs = self.all_ns_refs |
|
1234 | ns_refs = self.all_ns_refs | |
1251 |
|
1235 | |||
1252 | if by_name: # Delete by name |
|
1236 | if by_name: # Delete by name | |
1253 | for ns in ns_refs: |
|
1237 | for ns in ns_refs: | |
1254 | try: |
|
1238 | try: | |
1255 | del ns[varname] |
|
1239 | del ns[varname] | |
1256 | except KeyError: |
|
1240 | except KeyError: | |
1257 | pass |
|
1241 | pass | |
1258 | else: # Delete by object |
|
1242 | else: # Delete by object | |
1259 | try: |
|
1243 | try: | |
1260 | obj = self.user_ns[varname] |
|
1244 | obj = self.user_ns[varname] | |
1261 | except KeyError: |
|
1245 | except KeyError: | |
1262 | raise NameError("name '%s' is not defined" % varname) |
|
1246 | raise NameError("name '%s' is not defined" % varname) | |
1263 | # Also check in output history |
|
1247 | # Also check in output history | |
1264 | ns_refs.append(self.history_manager.output_hist) |
|
1248 | ns_refs.append(self.history_manager.output_hist) | |
1265 | for ns in ns_refs: |
|
1249 | for ns in ns_refs: | |
1266 | to_delete = [n for n, o in ns.iteritems() if o is obj] |
|
1250 | to_delete = [n for n, o in ns.iteritems() if o is obj] | |
1267 | for name in to_delete: |
|
1251 | for name in to_delete: | |
1268 | del ns[name] |
|
1252 | del ns[name] | |
1269 |
|
1253 | |||
1270 | # displayhook keeps extra references, but not in a dictionary |
|
1254 | # displayhook keeps extra references, but not in a dictionary | |
1271 | for name in ('_', '__', '___'): |
|
1255 | for name in ('_', '__', '___'): | |
1272 | if getattr(self.displayhook, name) is obj: |
|
1256 | if getattr(self.displayhook, name) is obj: | |
1273 | setattr(self.displayhook, name, None) |
|
1257 | setattr(self.displayhook, name, None) | |
1274 |
|
1258 | |||
1275 | def reset_selective(self, regex=None): |
|
1259 | def reset_selective(self, regex=None): | |
1276 | """Clear selective variables from internal namespaces based on a |
|
1260 | """Clear selective variables from internal namespaces based on a | |
1277 | specified regular expression. |
|
1261 | specified regular expression. | |
1278 |
|
1262 | |||
1279 | Parameters |
|
1263 | Parameters | |
1280 | ---------- |
|
1264 | ---------- | |
1281 | regex : string or compiled pattern, optional |
|
1265 | regex : string or compiled pattern, optional | |
1282 | A regular expression pattern that will be used in searching |
|
1266 | A regular expression pattern that will be used in searching | |
1283 | variable names in the users namespaces. |
|
1267 | variable names in the users namespaces. | |
1284 | """ |
|
1268 | """ | |
1285 | if regex is not None: |
|
1269 | if regex is not None: | |
1286 | try: |
|
1270 | try: | |
1287 | m = re.compile(regex) |
|
1271 | m = re.compile(regex) | |
1288 | except TypeError: |
|
1272 | except TypeError: | |
1289 | raise TypeError('regex must be a string or compiled pattern') |
|
1273 | raise TypeError('regex must be a string or compiled pattern') | |
1290 | # Search for keys in each namespace that match the given regex |
|
1274 | # Search for keys in each namespace that match the given regex | |
1291 | # If a match is found, delete the key/value pair. |
|
1275 | # If a match is found, delete the key/value pair. | |
1292 | for ns in self.all_ns_refs: |
|
1276 | for ns in self.all_ns_refs: | |
1293 | for var in ns: |
|
1277 | for var in ns: | |
1294 | if m.search(var): |
|
1278 | if m.search(var): | |
1295 | del ns[var] |
|
1279 | del ns[var] | |
1296 |
|
1280 | |||
1297 | def push(self, variables, interactive=True): |
|
1281 | def push(self, variables, interactive=True): | |
1298 | """Inject a group of variables into the IPython user namespace. |
|
1282 | """Inject a group of variables into the IPython user namespace. | |
1299 |
|
1283 | |||
1300 | Parameters |
|
1284 | Parameters | |
1301 | ---------- |
|
1285 | ---------- | |
1302 | variables : dict, str or list/tuple of str |
|
1286 | variables : dict, str or list/tuple of str | |
1303 | The variables to inject into the user's namespace. If a dict, a |
|
1287 | The variables to inject into the user's namespace. If a dict, a | |
1304 | simple update is done. If a str, the string is assumed to have |
|
1288 | simple update is done. If a str, the string is assumed to have | |
1305 | variable names separated by spaces. A list/tuple of str can also |
|
1289 | variable names separated by spaces. A list/tuple of str can also | |
1306 | be used to give the variable names. If just the variable names are |
|
1290 | be used to give the variable names. If just the variable names are | |
1307 | give (list/tuple/str) then the variable values looked up in the |
|
1291 | give (list/tuple/str) then the variable values looked up in the | |
1308 | callers frame. |
|
1292 | callers frame. | |
1309 | interactive : bool |
|
1293 | interactive : bool | |
1310 | If True (default), the variables will be listed with the ``who`` |
|
1294 | If True (default), the variables will be listed with the ``who`` | |
1311 | magic. |
|
1295 | magic. | |
1312 | """ |
|
1296 | """ | |
1313 | vdict = None |
|
1297 | vdict = None | |
1314 |
|
1298 | |||
1315 | # We need a dict of name/value pairs to do namespace updates. |
|
1299 | # We need a dict of name/value pairs to do namespace updates. | |
1316 | if isinstance(variables, dict): |
|
1300 | if isinstance(variables, dict): | |
1317 | vdict = variables |
|
1301 | vdict = variables | |
1318 | elif isinstance(variables, (basestring, list, tuple)): |
|
1302 | elif isinstance(variables, (basestring, list, tuple)): | |
1319 | if isinstance(variables, basestring): |
|
1303 | if isinstance(variables, basestring): | |
1320 | vlist = variables.split() |
|
1304 | vlist = variables.split() | |
1321 | else: |
|
1305 | else: | |
1322 | vlist = variables |
|
1306 | vlist = variables | |
1323 | vdict = {} |
|
1307 | vdict = {} | |
1324 | cf = sys._getframe(1) |
|
1308 | cf = sys._getframe(1) | |
1325 | for name in vlist: |
|
1309 | for name in vlist: | |
1326 | try: |
|
1310 | try: | |
1327 | vdict[name] = eval(name, cf.f_globals, cf.f_locals) |
|
1311 | vdict[name] = eval(name, cf.f_globals, cf.f_locals) | |
1328 | except: |
|
1312 | except: | |
1329 | print('Could not get variable %s from %s' % |
|
1313 | print('Could not get variable %s from %s' % | |
1330 | (name,cf.f_code.co_name)) |
|
1314 | (name,cf.f_code.co_name)) | |
1331 | else: |
|
1315 | else: | |
1332 | raise ValueError('variables must be a dict/str/list/tuple') |
|
1316 | raise ValueError('variables must be a dict/str/list/tuple') | |
1333 |
|
1317 | |||
1334 | # Propagate variables to user namespace |
|
1318 | # Propagate variables to user namespace | |
1335 | self.user_ns.update(vdict) |
|
1319 | self.user_ns.update(vdict) | |
1336 |
|
1320 | |||
1337 | # And configure interactive visibility |
|
1321 | # And configure interactive visibility | |
1338 | user_ns_hidden = self.user_ns_hidden |
|
1322 | user_ns_hidden = self.user_ns_hidden | |
1339 | if interactive: |
|
1323 | if interactive: | |
1340 | user_ns_hidden.difference_update(vdict) |
|
1324 | user_ns_hidden.difference_update(vdict) | |
1341 | else: |
|
1325 | else: | |
1342 | user_ns_hidden.update(vdict) |
|
1326 | user_ns_hidden.update(vdict) | |
1343 |
|
1327 | |||
1344 | def drop_by_id(self, variables): |
|
1328 | def drop_by_id(self, variables): | |
1345 | """Remove a dict of variables from the user namespace, if they are the |
|
1329 | """Remove a dict of variables from the user namespace, if they are the | |
1346 | same as the values in the dictionary. |
|
1330 | same as the values in the dictionary. | |
1347 |
|
1331 | |||
1348 | This is intended for use by extensions: variables that they've added can |
|
1332 | This is intended for use by extensions: variables that they've added can | |
1349 | be taken back out if they are unloaded, without removing any that the |
|
1333 | be taken back out if they are unloaded, without removing any that the | |
1350 | user has overwritten. |
|
1334 | user has overwritten. | |
1351 |
|
1335 | |||
1352 | Parameters |
|
1336 | Parameters | |
1353 | ---------- |
|
1337 | ---------- | |
1354 | variables : dict |
|
1338 | variables : dict | |
1355 | A dictionary mapping object names (as strings) to the objects. |
|
1339 | A dictionary mapping object names (as strings) to the objects. | |
1356 | """ |
|
1340 | """ | |
1357 | for name, obj in variables.iteritems(): |
|
1341 | for name, obj in variables.iteritems(): | |
1358 | if name in self.user_ns and self.user_ns[name] is obj: |
|
1342 | if name in self.user_ns and self.user_ns[name] is obj: | |
1359 | del self.user_ns[name] |
|
1343 | del self.user_ns[name] | |
1360 | self.user_ns_hidden.discard(name) |
|
1344 | self.user_ns_hidden.discard(name) | |
1361 |
|
1345 | |||
1362 | #------------------------------------------------------------------------- |
|
1346 | #------------------------------------------------------------------------- | |
1363 | # Things related to object introspection |
|
1347 | # Things related to object introspection | |
1364 | #------------------------------------------------------------------------- |
|
1348 | #------------------------------------------------------------------------- | |
1365 |
|
1349 | |||
1366 | def _ofind(self, oname, namespaces=None): |
|
1350 | def _ofind(self, oname, namespaces=None): | |
1367 | """Find an object in the available namespaces. |
|
1351 | """Find an object in the available namespaces. | |
1368 |
|
1352 | |||
1369 | self._ofind(oname) -> dict with keys: found,obj,ospace,ismagic |
|
1353 | self._ofind(oname) -> dict with keys: found,obj,ospace,ismagic | |
1370 |
|
1354 | |||
1371 | Has special code to detect magic functions. |
|
1355 | Has special code to detect magic functions. | |
1372 | """ |
|
1356 | """ | |
1373 | oname = oname.strip() |
|
1357 | oname = oname.strip() | |
1374 | #print '1- oname: <%r>' % oname # dbg |
|
1358 | #print '1- oname: <%r>' % oname # dbg | |
1375 | if not oname.startswith(ESC_MAGIC) and \ |
|
1359 | if not oname.startswith(ESC_MAGIC) and \ | |
1376 | not oname.startswith(ESC_MAGIC2) and \ |
|
1360 | not oname.startswith(ESC_MAGIC2) and \ | |
1377 | not py3compat.isidentifier(oname, dotted=True): |
|
1361 | not py3compat.isidentifier(oname, dotted=True): | |
1378 | return dict(found=False) |
|
1362 | return dict(found=False) | |
1379 |
|
1363 | |||
1380 | alias_ns = None |
|
1364 | alias_ns = None | |
1381 | if namespaces is None: |
|
1365 | if namespaces is None: | |
1382 | # Namespaces to search in: |
|
1366 | # Namespaces to search in: | |
1383 | # Put them in a list. The order is important so that we |
|
1367 | # Put them in a list. The order is important so that we | |
1384 | # find things in the same order that Python finds them. |
|
1368 | # find things in the same order that Python finds them. | |
1385 | namespaces = [ ('Interactive', self.user_ns), |
|
1369 | namespaces = [ ('Interactive', self.user_ns), | |
1386 | ('Interactive (global)', self.user_global_ns), |
|
1370 | ('Interactive (global)', self.user_global_ns), | |
1387 | ('Python builtin', builtin_mod.__dict__), |
|
1371 | ('Python builtin', builtin_mod.__dict__), | |
1388 | ('Alias', self.alias_manager.alias_table), |
|
1372 | ('Alias', self.alias_manager.alias_table), | |
1389 | ] |
|
1373 | ] | |
1390 | alias_ns = self.alias_manager.alias_table |
|
1374 | alias_ns = self.alias_manager.alias_table | |
1391 |
|
1375 | |||
1392 | # initialize results to 'null' |
|
1376 | # initialize results to 'null' | |
1393 | found = False; obj = None; ospace = None; ds = None; |
|
1377 | found = False; obj = None; ospace = None; ds = None; | |
1394 | ismagic = False; isalias = False; parent = None |
|
1378 | ismagic = False; isalias = False; parent = None | |
1395 |
|
1379 | |||
1396 | # We need to special-case 'print', which as of python2.6 registers as a |
|
1380 | # We need to special-case 'print', which as of python2.6 registers as a | |
1397 | # function but should only be treated as one if print_function was |
|
1381 | # function but should only be treated as one if print_function was | |
1398 | # loaded with a future import. In this case, just bail. |
|
1382 | # loaded with a future import. In this case, just bail. | |
1399 | if (oname == 'print' and not py3compat.PY3 and not \ |
|
1383 | if (oname == 'print' and not py3compat.PY3 and not \ | |
1400 | (self.compile.compiler_flags & __future__.CO_FUTURE_PRINT_FUNCTION)): |
|
1384 | (self.compile.compiler_flags & __future__.CO_FUTURE_PRINT_FUNCTION)): | |
1401 | return {'found':found, 'obj':obj, 'namespace':ospace, |
|
1385 | return {'found':found, 'obj':obj, 'namespace':ospace, | |
1402 | 'ismagic':ismagic, 'isalias':isalias, 'parent':parent} |
|
1386 | 'ismagic':ismagic, 'isalias':isalias, 'parent':parent} | |
1403 |
|
1387 | |||
1404 | # Look for the given name by splitting it in parts. If the head is |
|
1388 | # Look for the given name by splitting it in parts. If the head is | |
1405 | # found, then we look for all the remaining parts as members, and only |
|
1389 | # found, then we look for all the remaining parts as members, and only | |
1406 | # declare success if we can find them all. |
|
1390 | # declare success if we can find them all. | |
1407 | oname_parts = oname.split('.') |
|
1391 | oname_parts = oname.split('.') | |
1408 | oname_head, oname_rest = oname_parts[0],oname_parts[1:] |
|
1392 | oname_head, oname_rest = oname_parts[0],oname_parts[1:] | |
1409 | for nsname,ns in namespaces: |
|
1393 | for nsname,ns in namespaces: | |
1410 | try: |
|
1394 | try: | |
1411 | obj = ns[oname_head] |
|
1395 | obj = ns[oname_head] | |
1412 | except KeyError: |
|
1396 | except KeyError: | |
1413 | continue |
|
1397 | continue | |
1414 | else: |
|
1398 | else: | |
1415 | #print 'oname_rest:', oname_rest # dbg |
|
1399 | #print 'oname_rest:', oname_rest # dbg | |
1416 | for part in oname_rest: |
|
1400 | for part in oname_rest: | |
1417 | try: |
|
1401 | try: | |
1418 | parent = obj |
|
1402 | parent = obj | |
1419 | obj = getattr(obj,part) |
|
1403 | obj = getattr(obj,part) | |
1420 | except: |
|
1404 | except: | |
1421 | # Blanket except b/c some badly implemented objects |
|
1405 | # Blanket except b/c some badly implemented objects | |
1422 | # allow __getattr__ to raise exceptions other than |
|
1406 | # allow __getattr__ to raise exceptions other than | |
1423 | # AttributeError, which then crashes IPython. |
|
1407 | # AttributeError, which then crashes IPython. | |
1424 | break |
|
1408 | break | |
1425 | else: |
|
1409 | else: | |
1426 | # If we finish the for loop (no break), we got all members |
|
1410 | # If we finish the for loop (no break), we got all members | |
1427 | found = True |
|
1411 | found = True | |
1428 | ospace = nsname |
|
1412 | ospace = nsname | |
1429 | if ns == alias_ns: |
|
1413 | if ns == alias_ns: | |
1430 | isalias = True |
|
1414 | isalias = True | |
1431 | break # namespace loop |
|
1415 | break # namespace loop | |
1432 |
|
1416 | |||
1433 | # Try to see if it's magic |
|
1417 | # Try to see if it's magic | |
1434 | if not found: |
|
1418 | if not found: | |
1435 | obj = None |
|
1419 | obj = None | |
1436 | if oname.startswith(ESC_MAGIC2): |
|
1420 | if oname.startswith(ESC_MAGIC2): | |
1437 | oname = oname.lstrip(ESC_MAGIC2) |
|
1421 | oname = oname.lstrip(ESC_MAGIC2) | |
1438 | obj = self.find_cell_magic(oname) |
|
1422 | obj = self.find_cell_magic(oname) | |
1439 | elif oname.startswith(ESC_MAGIC): |
|
1423 | elif oname.startswith(ESC_MAGIC): | |
1440 | oname = oname.lstrip(ESC_MAGIC) |
|
1424 | oname = oname.lstrip(ESC_MAGIC) | |
1441 | obj = self.find_line_magic(oname) |
|
1425 | obj = self.find_line_magic(oname) | |
1442 | else: |
|
1426 | else: | |
1443 | # search without prefix, so run? will find %run? |
|
1427 | # search without prefix, so run? will find %run? | |
1444 | obj = self.find_line_magic(oname) |
|
1428 | obj = self.find_line_magic(oname) | |
1445 | if obj is None: |
|
1429 | if obj is None: | |
1446 | obj = self.find_cell_magic(oname) |
|
1430 | obj = self.find_cell_magic(oname) | |
1447 | if obj is not None: |
|
1431 | if obj is not None: | |
1448 | found = True |
|
1432 | found = True | |
1449 | ospace = 'IPython internal' |
|
1433 | ospace = 'IPython internal' | |
1450 | ismagic = True |
|
1434 | ismagic = True | |
1451 |
|
1435 | |||
1452 | # Last try: special-case some literals like '', [], {}, etc: |
|
1436 | # Last try: special-case some literals like '', [], {}, etc: | |
1453 | if not found and oname_head in ["''",'""','[]','{}','()']: |
|
1437 | if not found and oname_head in ["''",'""','[]','{}','()']: | |
1454 | obj = eval(oname_head) |
|
1438 | obj = eval(oname_head) | |
1455 | found = True |
|
1439 | found = True | |
1456 | ospace = 'Interactive' |
|
1440 | ospace = 'Interactive' | |
1457 |
|
1441 | |||
1458 | return {'found':found, 'obj':obj, 'namespace':ospace, |
|
1442 | return {'found':found, 'obj':obj, 'namespace':ospace, | |
1459 | 'ismagic':ismagic, 'isalias':isalias, 'parent':parent} |
|
1443 | 'ismagic':ismagic, 'isalias':isalias, 'parent':parent} | |
1460 |
|
1444 | |||
1461 | def _ofind_property(self, oname, info): |
|
1445 | def _ofind_property(self, oname, info): | |
1462 | """Second part of object finding, to look for property details.""" |
|
1446 | """Second part of object finding, to look for property details.""" | |
1463 | if info.found: |
|
1447 | if info.found: | |
1464 | # Get the docstring of the class property if it exists. |
|
1448 | # Get the docstring of the class property if it exists. | |
1465 | path = oname.split('.') |
|
1449 | path = oname.split('.') | |
1466 | root = '.'.join(path[:-1]) |
|
1450 | root = '.'.join(path[:-1]) | |
1467 | if info.parent is not None: |
|
1451 | if info.parent is not None: | |
1468 | try: |
|
1452 | try: | |
1469 | target = getattr(info.parent, '__class__') |
|
1453 | target = getattr(info.parent, '__class__') | |
1470 | # The object belongs to a class instance. |
|
1454 | # The object belongs to a class instance. | |
1471 | try: |
|
1455 | try: | |
1472 | target = getattr(target, path[-1]) |
|
1456 | target = getattr(target, path[-1]) | |
1473 | # The class defines the object. |
|
1457 | # The class defines the object. | |
1474 | if isinstance(target, property): |
|
1458 | if isinstance(target, property): | |
1475 | oname = root + '.__class__.' + path[-1] |
|
1459 | oname = root + '.__class__.' + path[-1] | |
1476 | info = Struct(self._ofind(oname)) |
|
1460 | info = Struct(self._ofind(oname)) | |
1477 | except AttributeError: pass |
|
1461 | except AttributeError: pass | |
1478 | except AttributeError: pass |
|
1462 | except AttributeError: pass | |
1479 |
|
1463 | |||
1480 | # We return either the new info or the unmodified input if the object |
|
1464 | # We return either the new info or the unmodified input if the object | |
1481 | # hadn't been found |
|
1465 | # hadn't been found | |
1482 | return info |
|
1466 | return info | |
1483 |
|
1467 | |||
1484 | def _object_find(self, oname, namespaces=None): |
|
1468 | def _object_find(self, oname, namespaces=None): | |
1485 | """Find an object and return a struct with info about it.""" |
|
1469 | """Find an object and return a struct with info about it.""" | |
1486 | inf = Struct(self._ofind(oname, namespaces)) |
|
1470 | inf = Struct(self._ofind(oname, namespaces)) | |
1487 | return Struct(self._ofind_property(oname, inf)) |
|
1471 | return Struct(self._ofind_property(oname, inf)) | |
1488 |
|
1472 | |||
1489 | def _inspect(self, meth, oname, namespaces=None, **kw): |
|
1473 | def _inspect(self, meth, oname, namespaces=None, **kw): | |
1490 | """Generic interface to the inspector system. |
|
1474 | """Generic interface to the inspector system. | |
1491 |
|
1475 | |||
1492 | This function is meant to be called by pdef, pdoc & friends.""" |
|
1476 | This function is meant to be called by pdef, pdoc & friends.""" | |
1493 | info = self._object_find(oname, namespaces) |
|
1477 | info = self._object_find(oname, namespaces) | |
1494 | if info.found: |
|
1478 | if info.found: | |
1495 | pmethod = getattr(self.inspector, meth) |
|
1479 | pmethod = getattr(self.inspector, meth) | |
1496 | formatter = format_screen if info.ismagic else None |
|
1480 | formatter = format_screen if info.ismagic else None | |
1497 | if meth == 'pdoc': |
|
1481 | if meth == 'pdoc': | |
1498 | pmethod(info.obj, oname, formatter) |
|
1482 | pmethod(info.obj, oname, formatter) | |
1499 | elif meth == 'pinfo': |
|
1483 | elif meth == 'pinfo': | |
1500 | pmethod(info.obj, oname, formatter, info, **kw) |
|
1484 | pmethod(info.obj, oname, formatter, info, **kw) | |
1501 | else: |
|
1485 | else: | |
1502 | pmethod(info.obj, oname) |
|
1486 | pmethod(info.obj, oname) | |
1503 | else: |
|
1487 | else: | |
1504 | print('Object `%s` not found.' % oname) |
|
1488 | print('Object `%s` not found.' % oname) | |
1505 | return 'not found' # so callers can take other action |
|
1489 | return 'not found' # so callers can take other action | |
1506 |
|
1490 | |||
1507 | def object_inspect(self, oname, detail_level=0): |
|
1491 | def object_inspect(self, oname, detail_level=0): | |
1508 | with self.builtin_trap: |
|
1492 | with self.builtin_trap: | |
1509 | info = self._object_find(oname) |
|
1493 | info = self._object_find(oname) | |
1510 | if info.found: |
|
1494 | if info.found: | |
1511 | return self.inspector.info(info.obj, oname, info=info, |
|
1495 | return self.inspector.info(info.obj, oname, info=info, | |
1512 | detail_level=detail_level |
|
1496 | detail_level=detail_level | |
1513 | ) |
|
1497 | ) | |
1514 | else: |
|
1498 | else: | |
1515 | return oinspect.object_info(name=oname, found=False) |
|
1499 | return oinspect.object_info(name=oname, found=False) | |
1516 |
|
1500 | |||
1517 | #------------------------------------------------------------------------- |
|
1501 | #------------------------------------------------------------------------- | |
1518 | # Things related to history management |
|
1502 | # Things related to history management | |
1519 | #------------------------------------------------------------------------- |
|
1503 | #------------------------------------------------------------------------- | |
1520 |
|
1504 | |||
1521 | def init_history(self): |
|
1505 | def init_history(self): | |
1522 | """Sets up the command history, and starts regular autosaves.""" |
|
1506 | """Sets up the command history, and starts regular autosaves.""" | |
1523 | self.history_manager = HistoryManager(shell=self, config=self.config) |
|
1507 | self.history_manager = HistoryManager(shell=self, config=self.config) | |
1524 | self.configurables.append(self.history_manager) |
|
1508 | self.configurables.append(self.history_manager) | |
1525 |
|
1509 | |||
1526 | #------------------------------------------------------------------------- |
|
1510 | #------------------------------------------------------------------------- | |
1527 | # Things related to exception handling and tracebacks (not debugging) |
|
1511 | # Things related to exception handling and tracebacks (not debugging) | |
1528 | #------------------------------------------------------------------------- |
|
1512 | #------------------------------------------------------------------------- | |
1529 |
|
1513 | |||
1530 | def init_traceback_handlers(self, custom_exceptions): |
|
1514 | def init_traceback_handlers(self, custom_exceptions): | |
1531 | # Syntax error handler. |
|
1515 | # Syntax error handler. | |
1532 | self.SyntaxTB = ultratb.SyntaxTB(color_scheme='NoColor') |
|
1516 | self.SyntaxTB = ultratb.SyntaxTB(color_scheme='NoColor') | |
1533 |
|
1517 | |||
1534 | # The interactive one is initialized with an offset, meaning we always |
|
1518 | # The interactive one is initialized with an offset, meaning we always | |
1535 | # want to remove the topmost item in the traceback, which is our own |
|
1519 | # want to remove the topmost item in the traceback, which is our own | |
1536 | # internal code. Valid modes: ['Plain','Context','Verbose'] |
|
1520 | # internal code. Valid modes: ['Plain','Context','Verbose'] | |
1537 | self.InteractiveTB = ultratb.AutoFormattedTB(mode = 'Plain', |
|
1521 | self.InteractiveTB = ultratb.AutoFormattedTB(mode = 'Plain', | |
1538 | color_scheme='NoColor', |
|
1522 | color_scheme='NoColor', | |
1539 | tb_offset = 1, |
|
1523 | tb_offset = 1, | |
1540 | check_cache=self.compile.check_cache) |
|
1524 | check_cache=self.compile.check_cache) | |
1541 |
|
1525 | |||
1542 | # The instance will store a pointer to the system-wide exception hook, |
|
1526 | # The instance will store a pointer to the system-wide exception hook, | |
1543 | # so that runtime code (such as magics) can access it. This is because |
|
1527 | # so that runtime code (such as magics) can access it. This is because | |
1544 | # during the read-eval loop, it may get temporarily overwritten. |
|
1528 | # during the read-eval loop, it may get temporarily overwritten. | |
1545 | self.sys_excepthook = sys.excepthook |
|
1529 | self.sys_excepthook = sys.excepthook | |
1546 |
|
1530 | |||
1547 | # and add any custom exception handlers the user may have specified |
|
1531 | # and add any custom exception handlers the user may have specified | |
1548 | self.set_custom_exc(*custom_exceptions) |
|
1532 | self.set_custom_exc(*custom_exceptions) | |
1549 |
|
1533 | |||
1550 | # Set the exception mode |
|
1534 | # Set the exception mode | |
1551 | self.InteractiveTB.set_mode(mode=self.xmode) |
|
1535 | self.InteractiveTB.set_mode(mode=self.xmode) | |
1552 |
|
1536 | |||
1553 | def set_custom_exc(self, exc_tuple, handler): |
|
1537 | def set_custom_exc(self, exc_tuple, handler): | |
1554 | """set_custom_exc(exc_tuple,handler) |
|
1538 | """set_custom_exc(exc_tuple,handler) | |
1555 |
|
1539 | |||
1556 | Set a custom exception handler, which will be called if any of the |
|
1540 | Set a custom exception handler, which will be called if any of the | |
1557 | exceptions in exc_tuple occur in the mainloop (specifically, in the |
|
1541 | exceptions in exc_tuple occur in the mainloop (specifically, in the | |
1558 | run_code() method). |
|
1542 | run_code() method). | |
1559 |
|
1543 | |||
1560 | Parameters |
|
1544 | Parameters | |
1561 | ---------- |
|
1545 | ---------- | |
1562 |
|
1546 | |||
1563 | exc_tuple : tuple of exception classes |
|
1547 | exc_tuple : tuple of exception classes | |
1564 | A *tuple* of exception classes, for which to call the defined |
|
1548 | A *tuple* of exception classes, for which to call the defined | |
1565 | handler. It is very important that you use a tuple, and NOT A |
|
1549 | handler. It is very important that you use a tuple, and NOT A | |
1566 | LIST here, because of the way Python's except statement works. If |
|
1550 | LIST here, because of the way Python's except statement works. If | |
1567 | you only want to trap a single exception, use a singleton tuple:: |
|
1551 | you only want to trap a single exception, use a singleton tuple:: | |
1568 |
|
1552 | |||
1569 | exc_tuple == (MyCustomException,) |
|
1553 | exc_tuple == (MyCustomException,) | |
1570 |
|
1554 | |||
1571 | handler : callable |
|
1555 | handler : callable | |
1572 | handler must have the following signature:: |
|
1556 | handler must have the following signature:: | |
1573 |
|
1557 | |||
1574 | def my_handler(self, etype, value, tb, tb_offset=None): |
|
1558 | def my_handler(self, etype, value, tb, tb_offset=None): | |
1575 | ... |
|
1559 | ... | |
1576 | return structured_traceback |
|
1560 | return structured_traceback | |
1577 |
|
1561 | |||
1578 | Your handler must return a structured traceback (a list of strings), |
|
1562 | Your handler must return a structured traceback (a list of strings), | |
1579 | or None. |
|
1563 | or None. | |
1580 |
|
1564 | |||
1581 | This will be made into an instance method (via types.MethodType) |
|
1565 | This will be made into an instance method (via types.MethodType) | |
1582 | of IPython itself, and it will be called if any of the exceptions |
|
1566 | of IPython itself, and it will be called if any of the exceptions | |
1583 | listed in the exc_tuple are caught. If the handler is None, an |
|
1567 | listed in the exc_tuple are caught. If the handler is None, an | |
1584 | internal basic one is used, which just prints basic info. |
|
1568 | internal basic one is used, which just prints basic info. | |
1585 |
|
1569 | |||
1586 | To protect IPython from crashes, if your handler ever raises an |
|
1570 | To protect IPython from crashes, if your handler ever raises an | |
1587 | exception or returns an invalid result, it will be immediately |
|
1571 | exception or returns an invalid result, it will be immediately | |
1588 | disabled. |
|
1572 | disabled. | |
1589 |
|
1573 | |||
1590 | WARNING: by putting in your own exception handler into IPython's main |
|
1574 | WARNING: by putting in your own exception handler into IPython's main | |
1591 | execution loop, you run a very good chance of nasty crashes. This |
|
1575 | execution loop, you run a very good chance of nasty crashes. This | |
1592 | facility should only be used if you really know what you are doing.""" |
|
1576 | facility should only be used if you really know what you are doing.""" | |
1593 |
|
1577 | |||
1594 | assert type(exc_tuple)==type(()) , \ |
|
1578 | assert type(exc_tuple)==type(()) , \ | |
1595 | "The custom exceptions must be given AS A TUPLE." |
|
1579 | "The custom exceptions must be given AS A TUPLE." | |
1596 |
|
1580 | |||
1597 | def dummy_handler(self,etype,value,tb,tb_offset=None): |
|
1581 | def dummy_handler(self,etype,value,tb,tb_offset=None): | |
1598 | print('*** Simple custom exception handler ***') |
|
1582 | print('*** Simple custom exception handler ***') | |
1599 | print('Exception type :',etype) |
|
1583 | print('Exception type :',etype) | |
1600 | print('Exception value:',value) |
|
1584 | print('Exception value:',value) | |
1601 | print('Traceback :',tb) |
|
1585 | print('Traceback :',tb) | |
1602 | #print 'Source code :','\n'.join(self.buffer) |
|
1586 | #print 'Source code :','\n'.join(self.buffer) | |
1603 |
|
1587 | |||
1604 | def validate_stb(stb): |
|
1588 | def validate_stb(stb): | |
1605 | """validate structured traceback return type |
|
1589 | """validate structured traceback return type | |
1606 |
|
1590 | |||
1607 | return type of CustomTB *should* be a list of strings, but allow |
|
1591 | return type of CustomTB *should* be a list of strings, but allow | |
1608 | single strings or None, which are harmless. |
|
1592 | single strings or None, which are harmless. | |
1609 |
|
1593 | |||
1610 | This function will *always* return a list of strings, |
|
1594 | This function will *always* return a list of strings, | |
1611 | and will raise a TypeError if stb is inappropriate. |
|
1595 | and will raise a TypeError if stb is inappropriate. | |
1612 | """ |
|
1596 | """ | |
1613 | msg = "CustomTB must return list of strings, not %r" % stb |
|
1597 | msg = "CustomTB must return list of strings, not %r" % stb | |
1614 | if stb is None: |
|
1598 | if stb is None: | |
1615 | return [] |
|
1599 | return [] | |
1616 | elif isinstance(stb, basestring): |
|
1600 | elif isinstance(stb, basestring): | |
1617 | return [stb] |
|
1601 | return [stb] | |
1618 | elif not isinstance(stb, list): |
|
1602 | elif not isinstance(stb, list): | |
1619 | raise TypeError(msg) |
|
1603 | raise TypeError(msg) | |
1620 | # it's a list |
|
1604 | # it's a list | |
1621 | for line in stb: |
|
1605 | for line in stb: | |
1622 | # check every element |
|
1606 | # check every element | |
1623 | if not isinstance(line, basestring): |
|
1607 | if not isinstance(line, basestring): | |
1624 | raise TypeError(msg) |
|
1608 | raise TypeError(msg) | |
1625 | return stb |
|
1609 | return stb | |
1626 |
|
1610 | |||
1627 | if handler is None: |
|
1611 | if handler is None: | |
1628 | wrapped = dummy_handler |
|
1612 | wrapped = dummy_handler | |
1629 | else: |
|
1613 | else: | |
1630 | def wrapped(self,etype,value,tb,tb_offset=None): |
|
1614 | def wrapped(self,etype,value,tb,tb_offset=None): | |
1631 | """wrap CustomTB handler, to protect IPython from user code |
|
1615 | """wrap CustomTB handler, to protect IPython from user code | |
1632 |
|
1616 | |||
1633 | This makes it harder (but not impossible) for custom exception |
|
1617 | This makes it harder (but not impossible) for custom exception | |
1634 | handlers to crash IPython. |
|
1618 | handlers to crash IPython. | |
1635 | """ |
|
1619 | """ | |
1636 | try: |
|
1620 | try: | |
1637 | stb = handler(self,etype,value,tb,tb_offset=tb_offset) |
|
1621 | stb = handler(self,etype,value,tb,tb_offset=tb_offset) | |
1638 | return validate_stb(stb) |
|
1622 | return validate_stb(stb) | |
1639 | except: |
|
1623 | except: | |
1640 | # clear custom handler immediately |
|
1624 | # clear custom handler immediately | |
1641 | self.set_custom_exc((), None) |
|
1625 | self.set_custom_exc((), None) | |
1642 | print("Custom TB Handler failed, unregistering", file=io.stderr) |
|
1626 | print("Custom TB Handler failed, unregistering", file=io.stderr) | |
1643 | # show the exception in handler first |
|
1627 | # show the exception in handler first | |
1644 | stb = self.InteractiveTB.structured_traceback(*sys.exc_info()) |
|
1628 | stb = self.InteractiveTB.structured_traceback(*sys.exc_info()) | |
1645 | print(self.InteractiveTB.stb2text(stb), file=io.stdout) |
|
1629 | print(self.InteractiveTB.stb2text(stb), file=io.stdout) | |
1646 | print("The original exception:", file=io.stdout) |
|
1630 | print("The original exception:", file=io.stdout) | |
1647 | stb = self.InteractiveTB.structured_traceback( |
|
1631 | stb = self.InteractiveTB.structured_traceback( | |
1648 | (etype,value,tb), tb_offset=tb_offset |
|
1632 | (etype,value,tb), tb_offset=tb_offset | |
1649 | ) |
|
1633 | ) | |
1650 | return stb |
|
1634 | return stb | |
1651 |
|
1635 | |||
1652 | self.CustomTB = types.MethodType(wrapped,self) |
|
1636 | self.CustomTB = types.MethodType(wrapped,self) | |
1653 | self.custom_exceptions = exc_tuple |
|
1637 | self.custom_exceptions = exc_tuple | |
1654 |
|
1638 | |||
1655 | def excepthook(self, etype, value, tb): |
|
1639 | def excepthook(self, etype, value, tb): | |
1656 | """One more defense for GUI apps that call sys.excepthook. |
|
1640 | """One more defense for GUI apps that call sys.excepthook. | |
1657 |
|
1641 | |||
1658 | GUI frameworks like wxPython trap exceptions and call |
|
1642 | GUI frameworks like wxPython trap exceptions and call | |
1659 | sys.excepthook themselves. I guess this is a feature that |
|
1643 | sys.excepthook themselves. I guess this is a feature that | |
1660 | enables them to keep running after exceptions that would |
|
1644 | enables them to keep running after exceptions that would | |
1661 | otherwise kill their mainloop. This is a bother for IPython |
|
1645 | otherwise kill their mainloop. This is a bother for IPython | |
1662 | which excepts to catch all of the program exceptions with a try: |
|
1646 | which excepts to catch all of the program exceptions with a try: | |
1663 | except: statement. |
|
1647 | except: statement. | |
1664 |
|
1648 | |||
1665 | Normally, IPython sets sys.excepthook to a CrashHandler instance, so if |
|
1649 | Normally, IPython sets sys.excepthook to a CrashHandler instance, so if | |
1666 | any app directly invokes sys.excepthook, it will look to the user like |
|
1650 | any app directly invokes sys.excepthook, it will look to the user like | |
1667 | IPython crashed. In order to work around this, we can disable the |
|
1651 | IPython crashed. In order to work around this, we can disable the | |
1668 | CrashHandler and replace it with this excepthook instead, which prints a |
|
1652 | CrashHandler and replace it with this excepthook instead, which prints a | |
1669 | regular traceback using our InteractiveTB. In this fashion, apps which |
|
1653 | regular traceback using our InteractiveTB. In this fashion, apps which | |
1670 | call sys.excepthook will generate a regular-looking exception from |
|
1654 | call sys.excepthook will generate a regular-looking exception from | |
1671 | IPython, and the CrashHandler will only be triggered by real IPython |
|
1655 | IPython, and the CrashHandler will only be triggered by real IPython | |
1672 | crashes. |
|
1656 | crashes. | |
1673 |
|
1657 | |||
1674 | This hook should be used sparingly, only in places which are not likely |
|
1658 | This hook should be used sparingly, only in places which are not likely | |
1675 | to be true IPython errors. |
|
1659 | to be true IPython errors. | |
1676 | """ |
|
1660 | """ | |
1677 | self.showtraceback((etype,value,tb),tb_offset=0) |
|
1661 | self.showtraceback((etype,value,tb),tb_offset=0) | |
1678 |
|
1662 | |||
1679 | def _get_exc_info(self, exc_tuple=None): |
|
1663 | def _get_exc_info(self, exc_tuple=None): | |
1680 | """get exc_info from a given tuple, sys.exc_info() or sys.last_type etc. |
|
1664 | """get exc_info from a given tuple, sys.exc_info() or sys.last_type etc. | |
1681 |
|
1665 | |||
1682 | Ensures sys.last_type,value,traceback hold the exc_info we found, |
|
1666 | Ensures sys.last_type,value,traceback hold the exc_info we found, | |
1683 | from whichever source. |
|
1667 | from whichever source. | |
1684 |
|
1668 | |||
1685 | raises ValueError if none of these contain any information |
|
1669 | raises ValueError if none of these contain any information | |
1686 | """ |
|
1670 | """ | |
1687 | if exc_tuple is None: |
|
1671 | if exc_tuple is None: | |
1688 | etype, value, tb = sys.exc_info() |
|
1672 | etype, value, tb = sys.exc_info() | |
1689 | else: |
|
1673 | else: | |
1690 | etype, value, tb = exc_tuple |
|
1674 | etype, value, tb = exc_tuple | |
1691 |
|
1675 | |||
1692 | if etype is None: |
|
1676 | if etype is None: | |
1693 | if hasattr(sys, 'last_type'): |
|
1677 | if hasattr(sys, 'last_type'): | |
1694 | etype, value, tb = sys.last_type, sys.last_value, \ |
|
1678 | etype, value, tb = sys.last_type, sys.last_value, \ | |
1695 | sys.last_traceback |
|
1679 | sys.last_traceback | |
1696 |
|
1680 | |||
1697 | if etype is None: |
|
1681 | if etype is None: | |
1698 | raise ValueError("No exception to find") |
|
1682 | raise ValueError("No exception to find") | |
1699 |
|
1683 | |||
1700 | # Now store the exception info in sys.last_type etc. |
|
1684 | # Now store the exception info in sys.last_type etc. | |
1701 | # WARNING: these variables are somewhat deprecated and not |
|
1685 | # WARNING: these variables are somewhat deprecated and not | |
1702 | # necessarily safe to use in a threaded environment, but tools |
|
1686 | # necessarily safe to use in a threaded environment, but tools | |
1703 | # like pdb depend on their existence, so let's set them. If we |
|
1687 | # like pdb depend on their existence, so let's set them. If we | |
1704 | # find problems in the field, we'll need to revisit their use. |
|
1688 | # find problems in the field, we'll need to revisit their use. | |
1705 | sys.last_type = etype |
|
1689 | sys.last_type = etype | |
1706 | sys.last_value = value |
|
1690 | sys.last_value = value | |
1707 | sys.last_traceback = tb |
|
1691 | sys.last_traceback = tb | |
1708 |
|
1692 | |||
1709 | return etype, value, tb |
|
1693 | return etype, value, tb | |
1710 |
|
1694 | |||
1711 |
|
1695 | |||
1712 | def showtraceback(self,exc_tuple = None,filename=None,tb_offset=None, |
|
1696 | def showtraceback(self,exc_tuple = None,filename=None,tb_offset=None, | |
1713 | exception_only=False): |
|
1697 | exception_only=False): | |
1714 | """Display the exception that just occurred. |
|
1698 | """Display the exception that just occurred. | |
1715 |
|
1699 | |||
1716 | If nothing is known about the exception, this is the method which |
|
1700 | If nothing is known about the exception, this is the method which | |
1717 | should be used throughout the code for presenting user tracebacks, |
|
1701 | should be used throughout the code for presenting user tracebacks, | |
1718 | rather than directly invoking the InteractiveTB object. |
|
1702 | rather than directly invoking the InteractiveTB object. | |
1719 |
|
1703 | |||
1720 | A specific showsyntaxerror() also exists, but this method can take |
|
1704 | A specific showsyntaxerror() also exists, but this method can take | |
1721 | care of calling it if needed, so unless you are explicitly catching a |
|
1705 | care of calling it if needed, so unless you are explicitly catching a | |
1722 | SyntaxError exception, don't try to analyze the stack manually and |
|
1706 | SyntaxError exception, don't try to analyze the stack manually and | |
1723 | simply call this method.""" |
|
1707 | simply call this method.""" | |
1724 |
|
1708 | |||
1725 | try: |
|
1709 | try: | |
1726 | try: |
|
1710 | try: | |
1727 | etype, value, tb = self._get_exc_info(exc_tuple) |
|
1711 | etype, value, tb = self._get_exc_info(exc_tuple) | |
1728 | except ValueError: |
|
1712 | except ValueError: | |
1729 | self.write_err('No traceback available to show.\n') |
|
1713 | self.write_err('No traceback available to show.\n') | |
1730 | return |
|
1714 | return | |
1731 |
|
1715 | |||
1732 | if etype is SyntaxError: |
|
1716 | if etype is SyntaxError: | |
1733 | # Though this won't be called by syntax errors in the input |
|
1717 | # Though this won't be called by syntax errors in the input | |
1734 | # line, there may be SyntaxError cases with imported code. |
|
1718 | # line, there may be SyntaxError cases with imported code. | |
1735 | self.showsyntaxerror(filename) |
|
1719 | self.showsyntaxerror(filename) | |
1736 | elif etype is UsageError: |
|
1720 | elif etype is UsageError: | |
1737 | self.write_err("UsageError: %s" % value) |
|
1721 | self.write_err("UsageError: %s" % value) | |
1738 | elif issubclass(etype, RemoteError): |
|
|||
1739 | # IPython.parallel remote exceptions. |
|
|||
1740 | # Draw the remote traceback, not the local one. |
|
|||
1741 | self._showtraceback(etype, value, value.render_traceback()) |
|
|||
1742 | else: |
|
1722 | else: | |
1743 | if exception_only: |
|
1723 | if exception_only: | |
1744 | stb = ['An exception has occurred, use %tb to see ' |
|
1724 | stb = ['An exception has occurred, use %tb to see ' | |
1745 | 'the full traceback.\n'] |
|
1725 | 'the full traceback.\n'] | |
1746 | stb.extend(self.InteractiveTB.get_exception_only(etype, |
|
1726 | stb.extend(self.InteractiveTB.get_exception_only(etype, | |
1747 | value)) |
|
1727 | value)) | |
1748 | else: |
|
1728 | else: | |
1749 | stb = self.InteractiveTB.structured_traceback(etype, |
|
1729 | try: | |
|
1730 | # Exception classes can customise their traceback - we | |||
|
1731 | # use this in IPython.parallel for exceptions occurring | |||
|
1732 | # in the engines. This should return a list of strings. | |||
|
1733 | stb = value._render_traceback_() | |||
|
1734 | except Exception: | |||
|
1735 | stb = self.InteractiveTB.structured_traceback(etype, | |||
1750 | value, tb, tb_offset=tb_offset) |
|
1736 | value, tb, tb_offset=tb_offset) | |
1751 |
|
1737 | |||
1752 | self._showtraceback(etype, value, stb) |
|
1738 | self._showtraceback(etype, value, stb) | |
1753 | if self.call_pdb: |
|
1739 | if self.call_pdb: | |
1754 | # drop into debugger |
|
1740 | # drop into debugger | |
1755 | self.debugger(force=True) |
|
1741 | self.debugger(force=True) | |
1756 | return |
|
1742 | return | |
1757 |
|
1743 | |||
1758 | # Actually show the traceback |
|
1744 | # Actually show the traceback | |
1759 | self._showtraceback(etype, value, stb) |
|
1745 | self._showtraceback(etype, value, stb) | |
1760 |
|
1746 | |||
1761 | except KeyboardInterrupt: |
|
1747 | except KeyboardInterrupt: | |
1762 | self.write_err("\nKeyboardInterrupt\n") |
|
1748 | self.write_err("\nKeyboardInterrupt\n") | |
1763 |
|
1749 | |||
1764 | def _showtraceback(self, etype, evalue, stb): |
|
1750 | def _showtraceback(self, etype, evalue, stb): | |
1765 | """Actually show a traceback. |
|
1751 | """Actually show a traceback. | |
1766 |
|
1752 | |||
1767 | Subclasses may override this method to put the traceback on a different |
|
1753 | Subclasses may override this method to put the traceback on a different | |
1768 | place, like a side channel. |
|
1754 | place, like a side channel. | |
1769 | """ |
|
1755 | """ | |
1770 | print(self.InteractiveTB.stb2text(stb), file=io.stdout) |
|
1756 | print(self.InteractiveTB.stb2text(stb), file=io.stdout) | |
1771 |
|
1757 | |||
1772 | def showsyntaxerror(self, filename=None): |
|
1758 | def showsyntaxerror(self, filename=None): | |
1773 | """Display the syntax error that just occurred. |
|
1759 | """Display the syntax error that just occurred. | |
1774 |
|
1760 | |||
1775 | This doesn't display a stack trace because there isn't one. |
|
1761 | This doesn't display a stack trace because there isn't one. | |
1776 |
|
1762 | |||
1777 | If a filename is given, it is stuffed in the exception instead |
|
1763 | If a filename is given, it is stuffed in the exception instead | |
1778 | of what was there before (because Python's parser always uses |
|
1764 | of what was there before (because Python's parser always uses | |
1779 | "<string>" when reading from a string). |
|
1765 | "<string>" when reading from a string). | |
1780 | """ |
|
1766 | """ | |
1781 | etype, value, last_traceback = self._get_exc_info() |
|
1767 | etype, value, last_traceback = self._get_exc_info() | |
1782 |
|
1768 | |||
1783 | if filename and etype is SyntaxError: |
|
1769 | if filename and etype is SyntaxError: | |
1784 | try: |
|
1770 | try: | |
1785 | value.filename = filename |
|
1771 | value.filename = filename | |
1786 | except: |
|
1772 | except: | |
1787 | # Not the format we expect; leave it alone |
|
1773 | # Not the format we expect; leave it alone | |
1788 | pass |
|
1774 | pass | |
1789 |
|
1775 | |||
1790 | stb = self.SyntaxTB.structured_traceback(etype, value, []) |
|
1776 | stb = self.SyntaxTB.structured_traceback(etype, value, []) | |
1791 | self._showtraceback(etype, value, stb) |
|
1777 | self._showtraceback(etype, value, stb) | |
1792 |
|
1778 | |||
1793 | # This is overridden in TerminalInteractiveShell to show a message about |
|
1779 | # This is overridden in TerminalInteractiveShell to show a message about | |
1794 | # the %paste magic. |
|
1780 | # the %paste magic. | |
1795 | def showindentationerror(self): |
|
1781 | def showindentationerror(self): | |
1796 | """Called by run_cell when there's an IndentationError in code entered |
|
1782 | """Called by run_cell when there's an IndentationError in code entered | |
1797 | at the prompt. |
|
1783 | at the prompt. | |
1798 |
|
1784 | |||
1799 | This is overridden in TerminalInteractiveShell to show a message about |
|
1785 | This is overridden in TerminalInteractiveShell to show a message about | |
1800 | the %paste magic.""" |
|
1786 | the %paste magic.""" | |
1801 | self.showsyntaxerror() |
|
1787 | self.showsyntaxerror() | |
1802 |
|
1788 | |||
1803 | #------------------------------------------------------------------------- |
|
1789 | #------------------------------------------------------------------------- | |
1804 | # Things related to readline |
|
1790 | # Things related to readline | |
1805 | #------------------------------------------------------------------------- |
|
1791 | #------------------------------------------------------------------------- | |
1806 |
|
1792 | |||
1807 | def init_readline(self): |
|
1793 | def init_readline(self): | |
1808 | """Command history completion/saving/reloading.""" |
|
1794 | """Command history completion/saving/reloading.""" | |
1809 |
|
1795 | |||
1810 | if self.readline_use: |
|
1796 | if self.readline_use: | |
1811 | import IPython.utils.rlineimpl as readline |
|
1797 | import IPython.utils.rlineimpl as readline | |
1812 |
|
1798 | |||
1813 | self.rl_next_input = None |
|
1799 | self.rl_next_input = None | |
1814 | self.rl_do_indent = False |
|
1800 | self.rl_do_indent = False | |
1815 |
|
1801 | |||
1816 | if not self.readline_use or not readline.have_readline: |
|
1802 | if not self.readline_use or not readline.have_readline: | |
1817 | self.has_readline = False |
|
1803 | self.has_readline = False | |
1818 | self.readline = None |
|
1804 | self.readline = None | |
1819 | # Set a number of methods that depend on readline to be no-op |
|
1805 | # Set a number of methods that depend on readline to be no-op | |
1820 | self.readline_no_record = no_op_context |
|
1806 | self.readline_no_record = no_op_context | |
1821 | self.set_readline_completer = no_op |
|
1807 | self.set_readline_completer = no_op | |
1822 | self.set_custom_completer = no_op |
|
1808 | self.set_custom_completer = no_op | |
1823 | if self.readline_use: |
|
1809 | if self.readline_use: | |
1824 | warn('Readline services not available or not loaded.') |
|
1810 | warn('Readline services not available or not loaded.') | |
1825 | else: |
|
1811 | else: | |
1826 | self.has_readline = True |
|
1812 | self.has_readline = True | |
1827 | self.readline = readline |
|
1813 | self.readline = readline | |
1828 | sys.modules['readline'] = readline |
|
1814 | sys.modules['readline'] = readline | |
1829 |
|
1815 | |||
1830 | # Platform-specific configuration |
|
1816 | # Platform-specific configuration | |
1831 | if os.name == 'nt': |
|
1817 | if os.name == 'nt': | |
1832 | # FIXME - check with Frederick to see if we can harmonize |
|
1818 | # FIXME - check with Frederick to see if we can harmonize | |
1833 | # naming conventions with pyreadline to avoid this |
|
1819 | # naming conventions with pyreadline to avoid this | |
1834 | # platform-dependent check |
|
1820 | # platform-dependent check | |
1835 | self.readline_startup_hook = readline.set_pre_input_hook |
|
1821 | self.readline_startup_hook = readline.set_pre_input_hook | |
1836 | else: |
|
1822 | else: | |
1837 | self.readline_startup_hook = readline.set_startup_hook |
|
1823 | self.readline_startup_hook = readline.set_startup_hook | |
1838 |
|
1824 | |||
1839 | # Load user's initrc file (readline config) |
|
1825 | # Load user's initrc file (readline config) | |
1840 | # Or if libedit is used, load editrc. |
|
1826 | # Or if libedit is used, load editrc. | |
1841 | inputrc_name = os.environ.get('INPUTRC') |
|
1827 | inputrc_name = os.environ.get('INPUTRC') | |
1842 | if inputrc_name is None: |
|
1828 | if inputrc_name is None: | |
1843 | inputrc_name = '.inputrc' |
|
1829 | inputrc_name = '.inputrc' | |
1844 | if readline.uses_libedit: |
|
1830 | if readline.uses_libedit: | |
1845 | inputrc_name = '.editrc' |
|
1831 | inputrc_name = '.editrc' | |
1846 | inputrc_name = os.path.join(self.home_dir, inputrc_name) |
|
1832 | inputrc_name = os.path.join(self.home_dir, inputrc_name) | |
1847 | if os.path.isfile(inputrc_name): |
|
1833 | if os.path.isfile(inputrc_name): | |
1848 | try: |
|
1834 | try: | |
1849 | readline.read_init_file(inputrc_name) |
|
1835 | readline.read_init_file(inputrc_name) | |
1850 | except: |
|
1836 | except: | |
1851 | warn('Problems reading readline initialization file <%s>' |
|
1837 | warn('Problems reading readline initialization file <%s>' | |
1852 | % inputrc_name) |
|
1838 | % inputrc_name) | |
1853 |
|
1839 | |||
1854 | # Configure readline according to user's prefs |
|
1840 | # Configure readline according to user's prefs | |
1855 | # This is only done if GNU readline is being used. If libedit |
|
1841 | # This is only done if GNU readline is being used. If libedit | |
1856 | # is being used (as on Leopard) the readline config is |
|
1842 | # is being used (as on Leopard) the readline config is | |
1857 | # not run as the syntax for libedit is different. |
|
1843 | # not run as the syntax for libedit is different. | |
1858 | if not readline.uses_libedit: |
|
1844 | if not readline.uses_libedit: | |
1859 | for rlcommand in self.readline_parse_and_bind: |
|
1845 | for rlcommand in self.readline_parse_and_bind: | |
1860 | #print "loading rl:",rlcommand # dbg |
|
1846 | #print "loading rl:",rlcommand # dbg | |
1861 | readline.parse_and_bind(rlcommand) |
|
1847 | readline.parse_and_bind(rlcommand) | |
1862 |
|
1848 | |||
1863 | # Remove some chars from the delimiters list. If we encounter |
|
1849 | # Remove some chars from the delimiters list. If we encounter | |
1864 | # unicode chars, discard them. |
|
1850 | # unicode chars, discard them. | |
1865 | delims = readline.get_completer_delims() |
|
1851 | delims = readline.get_completer_delims() | |
1866 | if not py3compat.PY3: |
|
1852 | if not py3compat.PY3: | |
1867 | delims = delims.encode("ascii", "ignore") |
|
1853 | delims = delims.encode("ascii", "ignore") | |
1868 | for d in self.readline_remove_delims: |
|
1854 | for d in self.readline_remove_delims: | |
1869 | delims = delims.replace(d, "") |
|
1855 | delims = delims.replace(d, "") | |
1870 | delims = delims.replace(ESC_MAGIC, '') |
|
1856 | delims = delims.replace(ESC_MAGIC, '') | |
1871 | readline.set_completer_delims(delims) |
|
1857 | readline.set_completer_delims(delims) | |
1872 | # otherwise we end up with a monster history after a while: |
|
1858 | # otherwise we end up with a monster history after a while: | |
1873 | readline.set_history_length(self.history_length) |
|
1859 | readline.set_history_length(self.history_length) | |
1874 |
|
1860 | |||
1875 | self.refill_readline_hist() |
|
1861 | self.refill_readline_hist() | |
1876 | self.readline_no_record = ReadlineNoRecord(self) |
|
1862 | self.readline_no_record = ReadlineNoRecord(self) | |
1877 |
|
1863 | |||
1878 | # Configure auto-indent for all platforms |
|
1864 | # Configure auto-indent for all platforms | |
1879 | self.set_autoindent(self.autoindent) |
|
1865 | self.set_autoindent(self.autoindent) | |
1880 |
|
1866 | |||
1881 | def refill_readline_hist(self): |
|
1867 | def refill_readline_hist(self): | |
1882 | # Load the last 1000 lines from history |
|
1868 | # Load the last 1000 lines from history | |
1883 | self.readline.clear_history() |
|
1869 | self.readline.clear_history() | |
1884 | stdin_encoding = sys.stdin.encoding or "utf-8" |
|
1870 | stdin_encoding = sys.stdin.encoding or "utf-8" | |
1885 | last_cell = u"" |
|
1871 | last_cell = u"" | |
1886 | for _, _, cell in self.history_manager.get_tail(1000, |
|
1872 | for _, _, cell in self.history_manager.get_tail(1000, | |
1887 | include_latest=True): |
|
1873 | include_latest=True): | |
1888 | # Ignore blank lines and consecutive duplicates |
|
1874 | # Ignore blank lines and consecutive duplicates | |
1889 | cell = cell.rstrip() |
|
1875 | cell = cell.rstrip() | |
1890 | if cell and (cell != last_cell): |
|
1876 | if cell and (cell != last_cell): | |
1891 | if self.multiline_history: |
|
1877 | if self.multiline_history: | |
1892 | self.readline.add_history(py3compat.unicode_to_str(cell, |
|
1878 | self.readline.add_history(py3compat.unicode_to_str(cell, | |
1893 | stdin_encoding)) |
|
1879 | stdin_encoding)) | |
1894 | else: |
|
1880 | else: | |
1895 | for line in cell.splitlines(): |
|
1881 | for line in cell.splitlines(): | |
1896 | self.readline.add_history(py3compat.unicode_to_str(line, |
|
1882 | self.readline.add_history(py3compat.unicode_to_str(line, | |
1897 | stdin_encoding)) |
|
1883 | stdin_encoding)) | |
1898 | last_cell = cell |
|
1884 | last_cell = cell | |
1899 |
|
1885 | |||
1900 | def set_next_input(self, s): |
|
1886 | def set_next_input(self, s): | |
1901 | """ Sets the 'default' input string for the next command line. |
|
1887 | """ Sets the 'default' input string for the next command line. | |
1902 |
|
1888 | |||
1903 | Requires readline. |
|
1889 | Requires readline. | |
1904 |
|
1890 | |||
1905 | Example: |
|
1891 | Example: | |
1906 |
|
1892 | |||
1907 | [D:\ipython]|1> _ip.set_next_input("Hello Word") |
|
1893 | [D:\ipython]|1> _ip.set_next_input("Hello Word") | |
1908 | [D:\ipython]|2> Hello Word_ # cursor is here |
|
1894 | [D:\ipython]|2> Hello Word_ # cursor is here | |
1909 | """ |
|
1895 | """ | |
1910 | self.rl_next_input = py3compat.cast_bytes_py2(s) |
|
1896 | self.rl_next_input = py3compat.cast_bytes_py2(s) | |
1911 |
|
1897 | |||
1912 | # Maybe move this to the terminal subclass? |
|
1898 | # Maybe move this to the terminal subclass? | |
1913 | def pre_readline(self): |
|
1899 | def pre_readline(self): | |
1914 | """readline hook to be used at the start of each line. |
|
1900 | """readline hook to be used at the start of each line. | |
1915 |
|
1901 | |||
1916 | Currently it handles auto-indent only.""" |
|
1902 | Currently it handles auto-indent only.""" | |
1917 |
|
1903 | |||
1918 | if self.rl_do_indent: |
|
1904 | if self.rl_do_indent: | |
1919 | self.readline.insert_text(self._indent_current_str()) |
|
1905 | self.readline.insert_text(self._indent_current_str()) | |
1920 | if self.rl_next_input is not None: |
|
1906 | if self.rl_next_input is not None: | |
1921 | self.readline.insert_text(self.rl_next_input) |
|
1907 | self.readline.insert_text(self.rl_next_input) | |
1922 | self.rl_next_input = None |
|
1908 | self.rl_next_input = None | |
1923 |
|
1909 | |||
1924 | def _indent_current_str(self): |
|
1910 | def _indent_current_str(self): | |
1925 | """return the current level of indentation as a string""" |
|
1911 | """return the current level of indentation as a string""" | |
1926 | return self.input_splitter.indent_spaces * ' ' |
|
1912 | return self.input_splitter.indent_spaces * ' ' | |
1927 |
|
1913 | |||
1928 | #------------------------------------------------------------------------- |
|
1914 | #------------------------------------------------------------------------- | |
1929 | # Things related to text completion |
|
1915 | # Things related to text completion | |
1930 | #------------------------------------------------------------------------- |
|
1916 | #------------------------------------------------------------------------- | |
1931 |
|
1917 | |||
1932 | def init_completer(self): |
|
1918 | def init_completer(self): | |
1933 | """Initialize the completion machinery. |
|
1919 | """Initialize the completion machinery. | |
1934 |
|
1920 | |||
1935 | This creates completion machinery that can be used by client code, |
|
1921 | This creates completion machinery that can be used by client code, | |
1936 | either interactively in-process (typically triggered by the readline |
|
1922 | either interactively in-process (typically triggered by the readline | |
1937 | library), programatically (such as in test suites) or out-of-prcess |
|
1923 | library), programatically (such as in test suites) or out-of-prcess | |
1938 | (typically over the network by remote frontends). |
|
1924 | (typically over the network by remote frontends). | |
1939 | """ |
|
1925 | """ | |
1940 | from IPython.core.completer import IPCompleter |
|
1926 | from IPython.core.completer import IPCompleter | |
1941 | from IPython.core.completerlib import (module_completer, |
|
1927 | from IPython.core.completerlib import (module_completer, | |
1942 | magic_run_completer, cd_completer, reset_completer) |
|
1928 | magic_run_completer, cd_completer, reset_completer) | |
1943 |
|
1929 | |||
1944 | self.Completer = IPCompleter(shell=self, |
|
1930 | self.Completer = IPCompleter(shell=self, | |
1945 | namespace=self.user_ns, |
|
1931 | namespace=self.user_ns, | |
1946 | global_namespace=self.user_global_ns, |
|
1932 | global_namespace=self.user_global_ns, | |
1947 | alias_table=self.alias_manager.alias_table, |
|
1933 | alias_table=self.alias_manager.alias_table, | |
1948 | use_readline=self.has_readline, |
|
1934 | use_readline=self.has_readline, | |
1949 | config=self.config, |
|
1935 | config=self.config, | |
1950 | ) |
|
1936 | ) | |
1951 | self.configurables.append(self.Completer) |
|
1937 | self.configurables.append(self.Completer) | |
1952 |
|
1938 | |||
1953 | # Add custom completers to the basic ones built into IPCompleter |
|
1939 | # Add custom completers to the basic ones built into IPCompleter | |
1954 | sdisp = self.strdispatchers.get('complete_command', StrDispatch()) |
|
1940 | sdisp = self.strdispatchers.get('complete_command', StrDispatch()) | |
1955 | self.strdispatchers['complete_command'] = sdisp |
|
1941 | self.strdispatchers['complete_command'] = sdisp | |
1956 | self.Completer.custom_completers = sdisp |
|
1942 | self.Completer.custom_completers = sdisp | |
1957 |
|
1943 | |||
1958 | self.set_hook('complete_command', module_completer, str_key = 'import') |
|
1944 | self.set_hook('complete_command', module_completer, str_key = 'import') | |
1959 | self.set_hook('complete_command', module_completer, str_key = 'from') |
|
1945 | self.set_hook('complete_command', module_completer, str_key = 'from') | |
1960 | self.set_hook('complete_command', magic_run_completer, str_key = '%run') |
|
1946 | self.set_hook('complete_command', magic_run_completer, str_key = '%run') | |
1961 | self.set_hook('complete_command', cd_completer, str_key = '%cd') |
|
1947 | self.set_hook('complete_command', cd_completer, str_key = '%cd') | |
1962 | self.set_hook('complete_command', reset_completer, str_key = '%reset') |
|
1948 | self.set_hook('complete_command', reset_completer, str_key = '%reset') | |
1963 |
|
1949 | |||
1964 | # Only configure readline if we truly are using readline. IPython can |
|
1950 | # Only configure readline if we truly are using readline. IPython can | |
1965 | # do tab-completion over the network, in GUIs, etc, where readline |
|
1951 | # do tab-completion over the network, in GUIs, etc, where readline | |
1966 | # itself may be absent |
|
1952 | # itself may be absent | |
1967 | if self.has_readline: |
|
1953 | if self.has_readline: | |
1968 | self.set_readline_completer() |
|
1954 | self.set_readline_completer() | |
1969 |
|
1955 | |||
1970 | def complete(self, text, line=None, cursor_pos=None): |
|
1956 | def complete(self, text, line=None, cursor_pos=None): | |
1971 | """Return the completed text and a list of completions. |
|
1957 | """Return the completed text and a list of completions. | |
1972 |
|
1958 | |||
1973 | Parameters |
|
1959 | Parameters | |
1974 | ---------- |
|
1960 | ---------- | |
1975 |
|
1961 | |||
1976 | text : string |
|
1962 | text : string | |
1977 | A string of text to be completed on. It can be given as empty and |
|
1963 | A string of text to be completed on. It can be given as empty and | |
1978 | instead a line/position pair are given. In this case, the |
|
1964 | instead a line/position pair are given. In this case, the | |
1979 | completer itself will split the line like readline does. |
|
1965 | completer itself will split the line like readline does. | |
1980 |
|
1966 | |||
1981 | line : string, optional |
|
1967 | line : string, optional | |
1982 | The complete line that text is part of. |
|
1968 | The complete line that text is part of. | |
1983 |
|
1969 | |||
1984 | cursor_pos : int, optional |
|
1970 | cursor_pos : int, optional | |
1985 | The position of the cursor on the input line. |
|
1971 | The position of the cursor on the input line. | |
1986 |
|
1972 | |||
1987 | Returns |
|
1973 | Returns | |
1988 | ------- |
|
1974 | ------- | |
1989 | text : string |
|
1975 | text : string | |
1990 | The actual text that was completed. |
|
1976 | The actual text that was completed. | |
1991 |
|
1977 | |||
1992 | matches : list |
|
1978 | matches : list | |
1993 | A sorted list with all possible completions. |
|
1979 | A sorted list with all possible completions. | |
1994 |
|
1980 | |||
1995 | The optional arguments allow the completion to take more context into |
|
1981 | The optional arguments allow the completion to take more context into | |
1996 | account, and are part of the low-level completion API. |
|
1982 | account, and are part of the low-level completion API. | |
1997 |
|
1983 | |||
1998 | This is a wrapper around the completion mechanism, similar to what |
|
1984 | This is a wrapper around the completion mechanism, similar to what | |
1999 | readline does at the command line when the TAB key is hit. By |
|
1985 | readline does at the command line when the TAB key is hit. By | |
2000 | exposing it as a method, it can be used by other non-readline |
|
1986 | exposing it as a method, it can be used by other non-readline | |
2001 | environments (such as GUIs) for text completion. |
|
1987 | environments (such as GUIs) for text completion. | |
2002 |
|
1988 | |||
2003 | Simple usage example: |
|
1989 | Simple usage example: | |
2004 |
|
1990 | |||
2005 | In [1]: x = 'hello' |
|
1991 | In [1]: x = 'hello' | |
2006 |
|
1992 | |||
2007 | In [2]: _ip.complete('x.l') |
|
1993 | In [2]: _ip.complete('x.l') | |
2008 | Out[2]: ('x.l', ['x.ljust', 'x.lower', 'x.lstrip']) |
|
1994 | Out[2]: ('x.l', ['x.ljust', 'x.lower', 'x.lstrip']) | |
2009 | """ |
|
1995 | """ | |
2010 |
|
1996 | |||
2011 | # Inject names into __builtin__ so we can complete on the added names. |
|
1997 | # Inject names into __builtin__ so we can complete on the added names. | |
2012 | with self.builtin_trap: |
|
1998 | with self.builtin_trap: | |
2013 | return self.Completer.complete(text, line, cursor_pos) |
|
1999 | return self.Completer.complete(text, line, cursor_pos) | |
2014 |
|
2000 | |||
2015 | def set_custom_completer(self, completer, pos=0): |
|
2001 | def set_custom_completer(self, completer, pos=0): | |
2016 | """Adds a new custom completer function. |
|
2002 | """Adds a new custom completer function. | |
2017 |
|
2003 | |||
2018 | The position argument (defaults to 0) is the index in the completers |
|
2004 | The position argument (defaults to 0) is the index in the completers | |
2019 | list where you want the completer to be inserted.""" |
|
2005 | list where you want the completer to be inserted.""" | |
2020 |
|
2006 | |||
2021 | newcomp = types.MethodType(completer,self.Completer) |
|
2007 | newcomp = types.MethodType(completer,self.Completer) | |
2022 | self.Completer.matchers.insert(pos,newcomp) |
|
2008 | self.Completer.matchers.insert(pos,newcomp) | |
2023 |
|
2009 | |||
2024 | def set_readline_completer(self): |
|
2010 | def set_readline_completer(self): | |
2025 | """Reset readline's completer to be our own.""" |
|
2011 | """Reset readline's completer to be our own.""" | |
2026 | self.readline.set_completer(self.Completer.rlcomplete) |
|
2012 | self.readline.set_completer(self.Completer.rlcomplete) | |
2027 |
|
2013 | |||
2028 | def set_completer_frame(self, frame=None): |
|
2014 | def set_completer_frame(self, frame=None): | |
2029 | """Set the frame of the completer.""" |
|
2015 | """Set the frame of the completer.""" | |
2030 | if frame: |
|
2016 | if frame: | |
2031 | self.Completer.namespace = frame.f_locals |
|
2017 | self.Completer.namespace = frame.f_locals | |
2032 | self.Completer.global_namespace = frame.f_globals |
|
2018 | self.Completer.global_namespace = frame.f_globals | |
2033 | else: |
|
2019 | else: | |
2034 | self.Completer.namespace = self.user_ns |
|
2020 | self.Completer.namespace = self.user_ns | |
2035 | self.Completer.global_namespace = self.user_global_ns |
|
2021 | self.Completer.global_namespace = self.user_global_ns | |
2036 |
|
2022 | |||
2037 | #------------------------------------------------------------------------- |
|
2023 | #------------------------------------------------------------------------- | |
2038 | # Things related to magics |
|
2024 | # Things related to magics | |
2039 | #------------------------------------------------------------------------- |
|
2025 | #------------------------------------------------------------------------- | |
2040 |
|
2026 | |||
2041 | def init_magics(self): |
|
2027 | def init_magics(self): | |
2042 | from IPython.core import magics as m |
|
2028 | from IPython.core import magics as m | |
2043 | self.magics_manager = magic.MagicsManager(shell=self, |
|
2029 | self.magics_manager = magic.MagicsManager(shell=self, | |
2044 | confg=self.config, |
|
2030 | confg=self.config, | |
2045 | user_magics=m.UserMagics(self)) |
|
2031 | user_magics=m.UserMagics(self)) | |
2046 | self.configurables.append(self.magics_manager) |
|
2032 | self.configurables.append(self.magics_manager) | |
2047 |
|
2033 | |||
2048 | # Expose as public API from the magics manager |
|
2034 | # Expose as public API from the magics manager | |
2049 | self.register_magics = self.magics_manager.register |
|
2035 | self.register_magics = self.magics_manager.register | |
2050 | self.register_magic_function = self.magics_manager.register_function |
|
2036 | self.register_magic_function = self.magics_manager.register_function | |
2051 | self.define_magic = self.magics_manager.define_magic |
|
2037 | self.define_magic = self.magics_manager.define_magic | |
2052 |
|
2038 | |||
2053 | self.register_magics(m.AutoMagics, m.BasicMagics, m.CodeMagics, |
|
2039 | self.register_magics(m.AutoMagics, m.BasicMagics, m.CodeMagics, | |
2054 | m.ConfigMagics, m.DeprecatedMagics, m.DisplayMagics, m.ExecutionMagics, |
|
2040 | m.ConfigMagics, m.DeprecatedMagics, m.DisplayMagics, m.ExecutionMagics, | |
2055 | m.ExtensionMagics, m.HistoryMagics, m.LoggingMagics, |
|
2041 | m.ExtensionMagics, m.HistoryMagics, m.LoggingMagics, | |
2056 | m.NamespaceMagics, m.OSMagics, m.PylabMagics, m.ScriptMagics, |
|
2042 | m.NamespaceMagics, m.OSMagics, m.PylabMagics, m.ScriptMagics, | |
2057 | ) |
|
2043 | ) | |
2058 |
|
2044 | |||
2059 | # Register Magic Aliases |
|
2045 | # Register Magic Aliases | |
2060 | mman = self.magics_manager |
|
2046 | mman = self.magics_manager | |
2061 | mman.register_alias('ed', 'edit') |
|
2047 | mman.register_alias('ed', 'edit') | |
2062 | mman.register_alias('hist', 'history') |
|
2048 | mman.register_alias('hist', 'history') | |
2063 | mman.register_alias('rep', 'recall') |
|
2049 | mman.register_alias('rep', 'recall') | |
2064 |
|
2050 | |||
2065 | # FIXME: Move the color initialization to the DisplayHook, which |
|
2051 | # FIXME: Move the color initialization to the DisplayHook, which | |
2066 | # should be split into a prompt manager and displayhook. We probably |
|
2052 | # should be split into a prompt manager and displayhook. We probably | |
2067 | # even need a centralize colors management object. |
|
2053 | # even need a centralize colors management object. | |
2068 | self.magic('colors %s' % self.colors) |
|
2054 | self.magic('colors %s' % self.colors) | |
2069 |
|
2055 | |||
2070 | def run_line_magic(self, magic_name, line): |
|
2056 | def run_line_magic(self, magic_name, line): | |
2071 | """Execute the given line magic. |
|
2057 | """Execute the given line magic. | |
2072 |
|
2058 | |||
2073 | Parameters |
|
2059 | Parameters | |
2074 | ---------- |
|
2060 | ---------- | |
2075 | magic_name : str |
|
2061 | magic_name : str | |
2076 | Name of the desired magic function, without '%' prefix. |
|
2062 | Name of the desired magic function, without '%' prefix. | |
2077 |
|
2063 | |||
2078 | line : str |
|
2064 | line : str | |
2079 | The rest of the input line as a single string. |
|
2065 | The rest of the input line as a single string. | |
2080 | """ |
|
2066 | """ | |
2081 | fn = self.find_line_magic(magic_name) |
|
2067 | fn = self.find_line_magic(magic_name) | |
2082 | if fn is None: |
|
2068 | if fn is None: | |
2083 | cm = self.find_cell_magic(magic_name) |
|
2069 | cm = self.find_cell_magic(magic_name) | |
2084 | etpl = "Line magic function `%%%s` not found%s." |
|
2070 | etpl = "Line magic function `%%%s` not found%s." | |
2085 | extra = '' if cm is None else (' (But cell magic `%%%%%s` exists, ' |
|
2071 | extra = '' if cm is None else (' (But cell magic `%%%%%s` exists, ' | |
2086 | 'did you mean that instead?)' % magic_name ) |
|
2072 | 'did you mean that instead?)' % magic_name ) | |
2087 | error(etpl % (magic_name, extra)) |
|
2073 | error(etpl % (magic_name, extra)) | |
2088 | else: |
|
2074 | else: | |
2089 | # Note: this is the distance in the stack to the user's frame. |
|
2075 | # Note: this is the distance in the stack to the user's frame. | |
2090 | # This will need to be updated if the internal calling logic gets |
|
2076 | # This will need to be updated if the internal calling logic gets | |
2091 | # refactored, or else we'll be expanding the wrong variables. |
|
2077 | # refactored, or else we'll be expanding the wrong variables. | |
2092 | stack_depth = 2 |
|
2078 | stack_depth = 2 | |
2093 | magic_arg_s = self.var_expand(line, stack_depth) |
|
2079 | magic_arg_s = self.var_expand(line, stack_depth) | |
2094 | # Put magic args in a list so we can call with f(*a) syntax |
|
2080 | # Put magic args in a list so we can call with f(*a) syntax | |
2095 | args = [magic_arg_s] |
|
2081 | args = [magic_arg_s] | |
2096 | # Grab local namespace if we need it: |
|
2082 | # Grab local namespace if we need it: | |
2097 | if getattr(fn, "needs_local_scope", False): |
|
2083 | if getattr(fn, "needs_local_scope", False): | |
2098 | args.append(sys._getframe(stack_depth).f_locals) |
|
2084 | args.append(sys._getframe(stack_depth).f_locals) | |
2099 | with self.builtin_trap: |
|
2085 | with self.builtin_trap: | |
2100 | result = fn(*args) |
|
2086 | result = fn(*args) | |
2101 | return result |
|
2087 | return result | |
2102 |
|
2088 | |||
2103 | def run_cell_magic(self, magic_name, line, cell): |
|
2089 | def run_cell_magic(self, magic_name, line, cell): | |
2104 | """Execute the given cell magic. |
|
2090 | """Execute the given cell magic. | |
2105 |
|
2091 | |||
2106 | Parameters |
|
2092 | Parameters | |
2107 | ---------- |
|
2093 | ---------- | |
2108 | magic_name : str |
|
2094 | magic_name : str | |
2109 | Name of the desired magic function, without '%' prefix. |
|
2095 | Name of the desired magic function, without '%' prefix. | |
2110 |
|
2096 | |||
2111 | line : str |
|
2097 | line : str | |
2112 | The rest of the first input line as a single string. |
|
2098 | The rest of the first input line as a single string. | |
2113 |
|
2099 | |||
2114 | cell : str |
|
2100 | cell : str | |
2115 | The body of the cell as a (possibly multiline) string. |
|
2101 | The body of the cell as a (possibly multiline) string. | |
2116 | """ |
|
2102 | """ | |
2117 | fn = self.find_cell_magic(magic_name) |
|
2103 | fn = self.find_cell_magic(magic_name) | |
2118 | if fn is None: |
|
2104 | if fn is None: | |
2119 | lm = self.find_line_magic(magic_name) |
|
2105 | lm = self.find_line_magic(magic_name) | |
2120 | etpl = "Cell magic function `%%%%%s` not found%s." |
|
2106 | etpl = "Cell magic function `%%%%%s` not found%s." | |
2121 | extra = '' if lm is None else (' (But line magic `%%%s` exists, ' |
|
2107 | extra = '' if lm is None else (' (But line magic `%%%s` exists, ' | |
2122 | 'did you mean that instead?)' % magic_name ) |
|
2108 | 'did you mean that instead?)' % magic_name ) | |
2123 | error(etpl % (magic_name, extra)) |
|
2109 | error(etpl % (magic_name, extra)) | |
2124 | else: |
|
2110 | else: | |
2125 | # Note: this is the distance in the stack to the user's frame. |
|
2111 | # Note: this is the distance in the stack to the user's frame. | |
2126 | # This will need to be updated if the internal calling logic gets |
|
2112 | # This will need to be updated if the internal calling logic gets | |
2127 | # refactored, or else we'll be expanding the wrong variables. |
|
2113 | # refactored, or else we'll be expanding the wrong variables. | |
2128 | stack_depth = 2 |
|
2114 | stack_depth = 2 | |
2129 | magic_arg_s = self.var_expand(line, stack_depth) |
|
2115 | magic_arg_s = self.var_expand(line, stack_depth) | |
2130 | with self.builtin_trap: |
|
2116 | with self.builtin_trap: | |
2131 | result = fn(magic_arg_s, cell) |
|
2117 | result = fn(magic_arg_s, cell) | |
2132 | return result |
|
2118 | return result | |
2133 |
|
2119 | |||
2134 | def find_line_magic(self, magic_name): |
|
2120 | def find_line_magic(self, magic_name): | |
2135 | """Find and return a line magic by name. |
|
2121 | """Find and return a line magic by name. | |
2136 |
|
2122 | |||
2137 | Returns None if the magic isn't found.""" |
|
2123 | Returns None if the magic isn't found.""" | |
2138 | return self.magics_manager.magics['line'].get(magic_name) |
|
2124 | return self.magics_manager.magics['line'].get(magic_name) | |
2139 |
|
2125 | |||
2140 | def find_cell_magic(self, magic_name): |
|
2126 | def find_cell_magic(self, magic_name): | |
2141 | """Find and return a cell magic by name. |
|
2127 | """Find and return a cell magic by name. | |
2142 |
|
2128 | |||
2143 | Returns None if the magic isn't found.""" |
|
2129 | Returns None if the magic isn't found.""" | |
2144 | return self.magics_manager.magics['cell'].get(magic_name) |
|
2130 | return self.magics_manager.magics['cell'].get(magic_name) | |
2145 |
|
2131 | |||
2146 | def find_magic(self, magic_name, magic_kind='line'): |
|
2132 | def find_magic(self, magic_name, magic_kind='line'): | |
2147 | """Find and return a magic of the given type by name. |
|
2133 | """Find and return a magic of the given type by name. | |
2148 |
|
2134 | |||
2149 | Returns None if the magic isn't found.""" |
|
2135 | Returns None if the magic isn't found.""" | |
2150 | return self.magics_manager.magics[magic_kind].get(magic_name) |
|
2136 | return self.magics_manager.magics[magic_kind].get(magic_name) | |
2151 |
|
2137 | |||
2152 | def magic(self, arg_s): |
|
2138 | def magic(self, arg_s): | |
2153 | """DEPRECATED. Use run_line_magic() instead. |
|
2139 | """DEPRECATED. Use run_line_magic() instead. | |
2154 |
|
2140 | |||
2155 | Call a magic function by name. |
|
2141 | Call a magic function by name. | |
2156 |
|
2142 | |||
2157 | Input: a string containing the name of the magic function to call and |
|
2143 | Input: a string containing the name of the magic function to call and | |
2158 | any additional arguments to be passed to the magic. |
|
2144 | any additional arguments to be passed to the magic. | |
2159 |
|
2145 | |||
2160 | magic('name -opt foo bar') is equivalent to typing at the ipython |
|
2146 | magic('name -opt foo bar') is equivalent to typing at the ipython | |
2161 | prompt: |
|
2147 | prompt: | |
2162 |
|
2148 | |||
2163 | In[1]: %name -opt foo bar |
|
2149 | In[1]: %name -opt foo bar | |
2164 |
|
2150 | |||
2165 | To call a magic without arguments, simply use magic('name'). |
|
2151 | To call a magic without arguments, simply use magic('name'). | |
2166 |
|
2152 | |||
2167 | This provides a proper Python function to call IPython's magics in any |
|
2153 | This provides a proper Python function to call IPython's magics in any | |
2168 | valid Python code you can type at the interpreter, including loops and |
|
2154 | valid Python code you can type at the interpreter, including loops and | |
2169 | compound statements. |
|
2155 | compound statements. | |
2170 | """ |
|
2156 | """ | |
2171 | # TODO: should we issue a loud deprecation warning here? |
|
2157 | # TODO: should we issue a loud deprecation warning here? | |
2172 | magic_name, _, magic_arg_s = arg_s.partition(' ') |
|
2158 | magic_name, _, magic_arg_s = arg_s.partition(' ') | |
2173 | magic_name = magic_name.lstrip(prefilter.ESC_MAGIC) |
|
2159 | magic_name = magic_name.lstrip(prefilter.ESC_MAGIC) | |
2174 | return self.run_line_magic(magic_name, magic_arg_s) |
|
2160 | return self.run_line_magic(magic_name, magic_arg_s) | |
2175 |
|
2161 | |||
2176 | #------------------------------------------------------------------------- |
|
2162 | #------------------------------------------------------------------------- | |
2177 | # Things related to macros |
|
2163 | # Things related to macros | |
2178 | #------------------------------------------------------------------------- |
|
2164 | #------------------------------------------------------------------------- | |
2179 |
|
2165 | |||
2180 | def define_macro(self, name, themacro): |
|
2166 | def define_macro(self, name, themacro): | |
2181 | """Define a new macro |
|
2167 | """Define a new macro | |
2182 |
|
2168 | |||
2183 | Parameters |
|
2169 | Parameters | |
2184 | ---------- |
|
2170 | ---------- | |
2185 | name : str |
|
2171 | name : str | |
2186 | The name of the macro. |
|
2172 | The name of the macro. | |
2187 | themacro : str or Macro |
|
2173 | themacro : str or Macro | |
2188 | The action to do upon invoking the macro. If a string, a new |
|
2174 | The action to do upon invoking the macro. If a string, a new | |
2189 | Macro object is created by passing the string to it. |
|
2175 | Macro object is created by passing the string to it. | |
2190 | """ |
|
2176 | """ | |
2191 |
|
2177 | |||
2192 | from IPython.core import macro |
|
2178 | from IPython.core import macro | |
2193 |
|
2179 | |||
2194 | if isinstance(themacro, basestring): |
|
2180 | if isinstance(themacro, basestring): | |
2195 | themacro = macro.Macro(themacro) |
|
2181 | themacro = macro.Macro(themacro) | |
2196 | if not isinstance(themacro, macro.Macro): |
|
2182 | if not isinstance(themacro, macro.Macro): | |
2197 | raise ValueError('A macro must be a string or a Macro instance.') |
|
2183 | raise ValueError('A macro must be a string or a Macro instance.') | |
2198 | self.user_ns[name] = themacro |
|
2184 | self.user_ns[name] = themacro | |
2199 |
|
2185 | |||
2200 | #------------------------------------------------------------------------- |
|
2186 | #------------------------------------------------------------------------- | |
2201 | # Things related to the running of system commands |
|
2187 | # Things related to the running of system commands | |
2202 | #------------------------------------------------------------------------- |
|
2188 | #------------------------------------------------------------------------- | |
2203 |
|
2189 | |||
2204 | def system_piped(self, cmd): |
|
2190 | def system_piped(self, cmd): | |
2205 | """Call the given cmd in a subprocess, piping stdout/err |
|
2191 | """Call the given cmd in a subprocess, piping stdout/err | |
2206 |
|
2192 | |||
2207 | Parameters |
|
2193 | Parameters | |
2208 | ---------- |
|
2194 | ---------- | |
2209 | cmd : str |
|
2195 | cmd : str | |
2210 | Command to execute (can not end in '&', as background processes are |
|
2196 | Command to execute (can not end in '&', as background processes are | |
2211 | not supported. Should not be a command that expects input |
|
2197 | not supported. Should not be a command that expects input | |
2212 | other than simple text. |
|
2198 | other than simple text. | |
2213 | """ |
|
2199 | """ | |
2214 | if cmd.rstrip().endswith('&'): |
|
2200 | if cmd.rstrip().endswith('&'): | |
2215 | # this is *far* from a rigorous test |
|
2201 | # this is *far* from a rigorous test | |
2216 | # We do not support backgrounding processes because we either use |
|
2202 | # We do not support backgrounding processes because we either use | |
2217 | # pexpect or pipes to read from. Users can always just call |
|
2203 | # pexpect or pipes to read from. Users can always just call | |
2218 | # os.system() or use ip.system=ip.system_raw |
|
2204 | # os.system() or use ip.system=ip.system_raw | |
2219 | # if they really want a background process. |
|
2205 | # if they really want a background process. | |
2220 | raise OSError("Background processes not supported.") |
|
2206 | raise OSError("Background processes not supported.") | |
2221 |
|
2207 | |||
2222 | # we explicitly do NOT return the subprocess status code, because |
|
2208 | # we explicitly do NOT return the subprocess status code, because | |
2223 | # a non-None value would trigger :func:`sys.displayhook` calls. |
|
2209 | # a non-None value would trigger :func:`sys.displayhook` calls. | |
2224 | # Instead, we store the exit_code in user_ns. |
|
2210 | # Instead, we store the exit_code in user_ns. | |
2225 | self.user_ns['_exit_code'] = system(self.var_expand(cmd, depth=1)) |
|
2211 | self.user_ns['_exit_code'] = system(self.var_expand(cmd, depth=1)) | |
2226 |
|
2212 | |||
2227 | def system_raw(self, cmd): |
|
2213 | def system_raw(self, cmd): | |
2228 | """Call the given cmd in a subprocess using os.system |
|
2214 | """Call the given cmd in a subprocess using os.system | |
2229 |
|
2215 | |||
2230 | Parameters |
|
2216 | Parameters | |
2231 | ---------- |
|
2217 | ---------- | |
2232 | cmd : str |
|
2218 | cmd : str | |
2233 | Command to execute. |
|
2219 | Command to execute. | |
2234 | """ |
|
2220 | """ | |
2235 | cmd = self.var_expand(cmd, depth=1) |
|
2221 | cmd = self.var_expand(cmd, depth=1) | |
2236 | # protect os.system from UNC paths on Windows, which it can't handle: |
|
2222 | # protect os.system from UNC paths on Windows, which it can't handle: | |
2237 | if sys.platform == 'win32': |
|
2223 | if sys.platform == 'win32': | |
2238 | from IPython.utils._process_win32 import AvoidUNCPath |
|
2224 | from IPython.utils._process_win32 import AvoidUNCPath | |
2239 | with AvoidUNCPath() as path: |
|
2225 | with AvoidUNCPath() as path: | |
2240 | if path is not None: |
|
2226 | if path is not None: | |
2241 | cmd = '"pushd %s &&"%s' % (path, cmd) |
|
2227 | cmd = '"pushd %s &&"%s' % (path, cmd) | |
2242 | cmd = py3compat.unicode_to_str(cmd) |
|
2228 | cmd = py3compat.unicode_to_str(cmd) | |
2243 | ec = os.system(cmd) |
|
2229 | ec = os.system(cmd) | |
2244 | else: |
|
2230 | else: | |
2245 | cmd = py3compat.unicode_to_str(cmd) |
|
2231 | cmd = py3compat.unicode_to_str(cmd) | |
2246 | ec = os.system(cmd) |
|
2232 | ec = os.system(cmd) | |
2247 |
|
2233 | |||
2248 | # We explicitly do NOT return the subprocess status code, because |
|
2234 | # We explicitly do NOT return the subprocess status code, because | |
2249 | # a non-None value would trigger :func:`sys.displayhook` calls. |
|
2235 | # a non-None value would trigger :func:`sys.displayhook` calls. | |
2250 | # Instead, we store the exit_code in user_ns. |
|
2236 | # Instead, we store the exit_code in user_ns. | |
2251 | self.user_ns['_exit_code'] = ec |
|
2237 | self.user_ns['_exit_code'] = ec | |
2252 |
|
2238 | |||
2253 | # use piped system by default, because it is better behaved |
|
2239 | # use piped system by default, because it is better behaved | |
2254 | system = system_piped |
|
2240 | system = system_piped | |
2255 |
|
2241 | |||
2256 | def getoutput(self, cmd, split=True, depth=0): |
|
2242 | def getoutput(self, cmd, split=True, depth=0): | |
2257 | """Get output (possibly including stderr) from a subprocess. |
|
2243 | """Get output (possibly including stderr) from a subprocess. | |
2258 |
|
2244 | |||
2259 | Parameters |
|
2245 | Parameters | |
2260 | ---------- |
|
2246 | ---------- | |
2261 | cmd : str |
|
2247 | cmd : str | |
2262 | Command to execute (can not end in '&', as background processes are |
|
2248 | Command to execute (can not end in '&', as background processes are | |
2263 | not supported. |
|
2249 | not supported. | |
2264 | split : bool, optional |
|
2250 | split : bool, optional | |
2265 | If True, split the output into an IPython SList. Otherwise, an |
|
2251 | If True, split the output into an IPython SList. Otherwise, an | |
2266 | IPython LSString is returned. These are objects similar to normal |
|
2252 | IPython LSString is returned. These are objects similar to normal | |
2267 | lists and strings, with a few convenience attributes for easier |
|
2253 | lists and strings, with a few convenience attributes for easier | |
2268 | manipulation of line-based output. You can use '?' on them for |
|
2254 | manipulation of line-based output. You can use '?' on them for | |
2269 | details. |
|
2255 | details. | |
2270 | depth : int, optional |
|
2256 | depth : int, optional | |
2271 | How many frames above the caller are the local variables which should |
|
2257 | How many frames above the caller are the local variables which should | |
2272 | be expanded in the command string? The default (0) assumes that the |
|
2258 | be expanded in the command string? The default (0) assumes that the | |
2273 | expansion variables are in the stack frame calling this function. |
|
2259 | expansion variables are in the stack frame calling this function. | |
2274 | """ |
|
2260 | """ | |
2275 | if cmd.rstrip().endswith('&'): |
|
2261 | if cmd.rstrip().endswith('&'): | |
2276 | # this is *far* from a rigorous test |
|
2262 | # this is *far* from a rigorous test | |
2277 | raise OSError("Background processes not supported.") |
|
2263 | raise OSError("Background processes not supported.") | |
2278 | out = getoutput(self.var_expand(cmd, depth=depth+1)) |
|
2264 | out = getoutput(self.var_expand(cmd, depth=depth+1)) | |
2279 | if split: |
|
2265 | if split: | |
2280 | out = SList(out.splitlines()) |
|
2266 | out = SList(out.splitlines()) | |
2281 | else: |
|
2267 | else: | |
2282 | out = LSString(out) |
|
2268 | out = LSString(out) | |
2283 | return out |
|
2269 | return out | |
2284 |
|
2270 | |||
2285 | #------------------------------------------------------------------------- |
|
2271 | #------------------------------------------------------------------------- | |
2286 | # Things related to aliases |
|
2272 | # Things related to aliases | |
2287 | #------------------------------------------------------------------------- |
|
2273 | #------------------------------------------------------------------------- | |
2288 |
|
2274 | |||
2289 | def init_alias(self): |
|
2275 | def init_alias(self): | |
2290 | self.alias_manager = AliasManager(shell=self, config=self.config) |
|
2276 | self.alias_manager = AliasManager(shell=self, config=self.config) | |
2291 | self.configurables.append(self.alias_manager) |
|
2277 | self.configurables.append(self.alias_manager) | |
2292 | self.ns_table['alias'] = self.alias_manager.alias_table, |
|
2278 | self.ns_table['alias'] = self.alias_manager.alias_table, | |
2293 |
|
2279 | |||
2294 | #------------------------------------------------------------------------- |
|
2280 | #------------------------------------------------------------------------- | |
2295 | # Things related to extensions and plugins |
|
2281 | # Things related to extensions and plugins | |
2296 | #------------------------------------------------------------------------- |
|
2282 | #------------------------------------------------------------------------- | |
2297 |
|
2283 | |||
2298 | def init_extension_manager(self): |
|
2284 | def init_extension_manager(self): | |
2299 | self.extension_manager = ExtensionManager(shell=self, config=self.config) |
|
2285 | self.extension_manager = ExtensionManager(shell=self, config=self.config) | |
2300 | self.configurables.append(self.extension_manager) |
|
2286 | self.configurables.append(self.extension_manager) | |
2301 |
|
2287 | |||
2302 | def init_plugin_manager(self): |
|
2288 | def init_plugin_manager(self): | |
2303 | self.plugin_manager = PluginManager(config=self.config) |
|
2289 | self.plugin_manager = PluginManager(config=self.config) | |
2304 | self.configurables.append(self.plugin_manager) |
|
2290 | self.configurables.append(self.plugin_manager) | |
2305 |
|
2291 | |||
2306 |
|
2292 | |||
2307 | #------------------------------------------------------------------------- |
|
2293 | #------------------------------------------------------------------------- | |
2308 | # Things related to payloads |
|
2294 | # Things related to payloads | |
2309 | #------------------------------------------------------------------------- |
|
2295 | #------------------------------------------------------------------------- | |
2310 |
|
2296 | |||
2311 | def init_payload(self): |
|
2297 | def init_payload(self): | |
2312 | self.payload_manager = PayloadManager(config=self.config) |
|
2298 | self.payload_manager = PayloadManager(config=self.config) | |
2313 | self.configurables.append(self.payload_manager) |
|
2299 | self.configurables.append(self.payload_manager) | |
2314 |
|
2300 | |||
2315 | #------------------------------------------------------------------------- |
|
2301 | #------------------------------------------------------------------------- | |
2316 | # Things related to the prefilter |
|
2302 | # Things related to the prefilter | |
2317 | #------------------------------------------------------------------------- |
|
2303 | #------------------------------------------------------------------------- | |
2318 |
|
2304 | |||
2319 | def init_prefilter(self): |
|
2305 | def init_prefilter(self): | |
2320 | self.prefilter_manager = PrefilterManager(shell=self, config=self.config) |
|
2306 | self.prefilter_manager = PrefilterManager(shell=self, config=self.config) | |
2321 | self.configurables.append(self.prefilter_manager) |
|
2307 | self.configurables.append(self.prefilter_manager) | |
2322 | # Ultimately this will be refactored in the new interpreter code, but |
|
2308 | # Ultimately this will be refactored in the new interpreter code, but | |
2323 | # for now, we should expose the main prefilter method (there's legacy |
|
2309 | # for now, we should expose the main prefilter method (there's legacy | |
2324 | # code out there that may rely on this). |
|
2310 | # code out there that may rely on this). | |
2325 | self.prefilter = self.prefilter_manager.prefilter_lines |
|
2311 | self.prefilter = self.prefilter_manager.prefilter_lines | |
2326 |
|
2312 | |||
2327 | def auto_rewrite_input(self, cmd): |
|
2313 | def auto_rewrite_input(self, cmd): | |
2328 | """Print to the screen the rewritten form of the user's command. |
|
2314 | """Print to the screen the rewritten form of the user's command. | |
2329 |
|
2315 | |||
2330 | This shows visual feedback by rewriting input lines that cause |
|
2316 | This shows visual feedback by rewriting input lines that cause | |
2331 | automatic calling to kick in, like:: |
|
2317 | automatic calling to kick in, like:: | |
2332 |
|
2318 | |||
2333 | /f x |
|
2319 | /f x | |
2334 |
|
2320 | |||
2335 | into:: |
|
2321 | into:: | |
2336 |
|
2322 | |||
2337 | ------> f(x) |
|
2323 | ------> f(x) | |
2338 |
|
2324 | |||
2339 | after the user's input prompt. This helps the user understand that the |
|
2325 | after the user's input prompt. This helps the user understand that the | |
2340 | input line was transformed automatically by IPython. |
|
2326 | input line was transformed automatically by IPython. | |
2341 | """ |
|
2327 | """ | |
2342 | if not self.show_rewritten_input: |
|
2328 | if not self.show_rewritten_input: | |
2343 | return |
|
2329 | return | |
2344 |
|
2330 | |||
2345 | rw = self.prompt_manager.render('rewrite') + cmd |
|
2331 | rw = self.prompt_manager.render('rewrite') + cmd | |
2346 |
|
2332 | |||
2347 | try: |
|
2333 | try: | |
2348 | # plain ascii works better w/ pyreadline, on some machines, so |
|
2334 | # plain ascii works better w/ pyreadline, on some machines, so | |
2349 | # we use it and only print uncolored rewrite if we have unicode |
|
2335 | # we use it and only print uncolored rewrite if we have unicode | |
2350 | rw = str(rw) |
|
2336 | rw = str(rw) | |
2351 | print(rw, file=io.stdout) |
|
2337 | print(rw, file=io.stdout) | |
2352 | except UnicodeEncodeError: |
|
2338 | except UnicodeEncodeError: | |
2353 | print("------> " + cmd) |
|
2339 | print("------> " + cmd) | |
2354 |
|
2340 | |||
2355 | #------------------------------------------------------------------------- |
|
2341 | #------------------------------------------------------------------------- | |
2356 | # Things related to extracting values/expressions from kernel and user_ns |
|
2342 | # Things related to extracting values/expressions from kernel and user_ns | |
2357 | #------------------------------------------------------------------------- |
|
2343 | #------------------------------------------------------------------------- | |
2358 |
|
2344 | |||
2359 | def _simple_error(self): |
|
2345 | def _simple_error(self): | |
2360 | etype, value = sys.exc_info()[:2] |
|
2346 | etype, value = sys.exc_info()[:2] | |
2361 | return u'[ERROR] {e.__name__}: {v}'.format(e=etype, v=value) |
|
2347 | return u'[ERROR] {e.__name__}: {v}'.format(e=etype, v=value) | |
2362 |
|
2348 | |||
2363 | def user_variables(self, names): |
|
2349 | def user_variables(self, names): | |
2364 | """Get a list of variable names from the user's namespace. |
|
2350 | """Get a list of variable names from the user's namespace. | |
2365 |
|
2351 | |||
2366 | Parameters |
|
2352 | Parameters | |
2367 | ---------- |
|
2353 | ---------- | |
2368 | names : list of strings |
|
2354 | names : list of strings | |
2369 | A list of names of variables to be read from the user namespace. |
|
2355 | A list of names of variables to be read from the user namespace. | |
2370 |
|
2356 | |||
2371 | Returns |
|
2357 | Returns | |
2372 | ------- |
|
2358 | ------- | |
2373 | A dict, keyed by the input names and with the repr() of each value. |
|
2359 | A dict, keyed by the input names and with the repr() of each value. | |
2374 | """ |
|
2360 | """ | |
2375 | out = {} |
|
2361 | out = {} | |
2376 | user_ns = self.user_ns |
|
2362 | user_ns = self.user_ns | |
2377 | for varname in names: |
|
2363 | for varname in names: | |
2378 | try: |
|
2364 | try: | |
2379 | value = repr(user_ns[varname]) |
|
2365 | value = repr(user_ns[varname]) | |
2380 | except: |
|
2366 | except: | |
2381 | value = self._simple_error() |
|
2367 | value = self._simple_error() | |
2382 | out[varname] = value |
|
2368 | out[varname] = value | |
2383 | return out |
|
2369 | return out | |
2384 |
|
2370 | |||
2385 | def user_expressions(self, expressions): |
|
2371 | def user_expressions(self, expressions): | |
2386 | """Evaluate a dict of expressions in the user's namespace. |
|
2372 | """Evaluate a dict of expressions in the user's namespace. | |
2387 |
|
2373 | |||
2388 | Parameters |
|
2374 | Parameters | |
2389 | ---------- |
|
2375 | ---------- | |
2390 | expressions : dict |
|
2376 | expressions : dict | |
2391 | A dict with string keys and string values. The expression values |
|
2377 | A dict with string keys and string values. The expression values | |
2392 | should be valid Python expressions, each of which will be evaluated |
|
2378 | should be valid Python expressions, each of which will be evaluated | |
2393 | in the user namespace. |
|
2379 | in the user namespace. | |
2394 |
|
2380 | |||
2395 | Returns |
|
2381 | Returns | |
2396 | ------- |
|
2382 | ------- | |
2397 | A dict, keyed like the input expressions dict, with the repr() of each |
|
2383 | A dict, keyed like the input expressions dict, with the repr() of each | |
2398 | value. |
|
2384 | value. | |
2399 | """ |
|
2385 | """ | |
2400 | out = {} |
|
2386 | out = {} | |
2401 | user_ns = self.user_ns |
|
2387 | user_ns = self.user_ns | |
2402 | global_ns = self.user_global_ns |
|
2388 | global_ns = self.user_global_ns | |
2403 | for key, expr in expressions.iteritems(): |
|
2389 | for key, expr in expressions.iteritems(): | |
2404 | try: |
|
2390 | try: | |
2405 | value = repr(eval(expr, global_ns, user_ns)) |
|
2391 | value = repr(eval(expr, global_ns, user_ns)) | |
2406 | except: |
|
2392 | except: | |
2407 | value = self._simple_error() |
|
2393 | value = self._simple_error() | |
2408 | out[key] = value |
|
2394 | out[key] = value | |
2409 | return out |
|
2395 | return out | |
2410 |
|
2396 | |||
2411 | #------------------------------------------------------------------------- |
|
2397 | #------------------------------------------------------------------------- | |
2412 | # Things related to the running of code |
|
2398 | # Things related to the running of code | |
2413 | #------------------------------------------------------------------------- |
|
2399 | #------------------------------------------------------------------------- | |
2414 |
|
2400 | |||
2415 | def ex(self, cmd): |
|
2401 | def ex(self, cmd): | |
2416 | """Execute a normal python statement in user namespace.""" |
|
2402 | """Execute a normal python statement in user namespace.""" | |
2417 | with self.builtin_trap: |
|
2403 | with self.builtin_trap: | |
2418 | exec cmd in self.user_global_ns, self.user_ns |
|
2404 | exec cmd in self.user_global_ns, self.user_ns | |
2419 |
|
2405 | |||
2420 | def ev(self, expr): |
|
2406 | def ev(self, expr): | |
2421 | """Evaluate python expression expr in user namespace. |
|
2407 | """Evaluate python expression expr in user namespace. | |
2422 |
|
2408 | |||
2423 | Returns the result of evaluation |
|
2409 | Returns the result of evaluation | |
2424 | """ |
|
2410 | """ | |
2425 | with self.builtin_trap: |
|
2411 | with self.builtin_trap: | |
2426 | return eval(expr, self.user_global_ns, self.user_ns) |
|
2412 | return eval(expr, self.user_global_ns, self.user_ns) | |
2427 |
|
2413 | |||
2428 | def safe_execfile(self, fname, *where, **kw): |
|
2414 | def safe_execfile(self, fname, *where, **kw): | |
2429 | """A safe version of the builtin execfile(). |
|
2415 | """A safe version of the builtin execfile(). | |
2430 |
|
2416 | |||
2431 | This version will never throw an exception, but instead print |
|
2417 | This version will never throw an exception, but instead print | |
2432 | helpful error messages to the screen. This only works on pure |
|
2418 | helpful error messages to the screen. This only works on pure | |
2433 | Python files with the .py extension. |
|
2419 | Python files with the .py extension. | |
2434 |
|
2420 | |||
2435 | Parameters |
|
2421 | Parameters | |
2436 | ---------- |
|
2422 | ---------- | |
2437 | fname : string |
|
2423 | fname : string | |
2438 | The name of the file to be executed. |
|
2424 | The name of the file to be executed. | |
2439 | where : tuple |
|
2425 | where : tuple | |
2440 | One or two namespaces, passed to execfile() as (globals,locals). |
|
2426 | One or two namespaces, passed to execfile() as (globals,locals). | |
2441 | If only one is given, it is passed as both. |
|
2427 | If only one is given, it is passed as both. | |
2442 | exit_ignore : bool (False) |
|
2428 | exit_ignore : bool (False) | |
2443 | If True, then silence SystemExit for non-zero status (it is always |
|
2429 | If True, then silence SystemExit for non-zero status (it is always | |
2444 | silenced for zero status, as it is so common). |
|
2430 | silenced for zero status, as it is so common). | |
2445 | raise_exceptions : bool (False) |
|
2431 | raise_exceptions : bool (False) | |
2446 | If True raise exceptions everywhere. Meant for testing. |
|
2432 | If True raise exceptions everywhere. Meant for testing. | |
2447 |
|
2433 | |||
2448 | """ |
|
2434 | """ | |
2449 | kw.setdefault('exit_ignore', False) |
|
2435 | kw.setdefault('exit_ignore', False) | |
2450 | kw.setdefault('raise_exceptions', False) |
|
2436 | kw.setdefault('raise_exceptions', False) | |
2451 |
|
2437 | |||
2452 | fname = os.path.abspath(os.path.expanduser(fname)) |
|
2438 | fname = os.path.abspath(os.path.expanduser(fname)) | |
2453 |
|
2439 | |||
2454 | # Make sure we can open the file |
|
2440 | # Make sure we can open the file | |
2455 | try: |
|
2441 | try: | |
2456 | with open(fname) as thefile: |
|
2442 | with open(fname) as thefile: | |
2457 | pass |
|
2443 | pass | |
2458 | except: |
|
2444 | except: | |
2459 | warn('Could not open file <%s> for safe execution.' % fname) |
|
2445 | warn('Could not open file <%s> for safe execution.' % fname) | |
2460 | return |
|
2446 | return | |
2461 |
|
2447 | |||
2462 | # Find things also in current directory. This is needed to mimic the |
|
2448 | # Find things also in current directory. This is needed to mimic the | |
2463 | # behavior of running a script from the system command line, where |
|
2449 | # behavior of running a script from the system command line, where | |
2464 | # Python inserts the script's directory into sys.path |
|
2450 | # Python inserts the script's directory into sys.path | |
2465 | dname = os.path.dirname(fname) |
|
2451 | dname = os.path.dirname(fname) | |
2466 |
|
2452 | |||
2467 | with prepended_to_syspath(dname): |
|
2453 | with prepended_to_syspath(dname): | |
2468 | # Ensure that __file__ is always defined to match Python behavior |
|
2454 | # Ensure that __file__ is always defined to match Python behavior | |
2469 | save_fname = self.user_ns.get('__file__',None) |
|
2455 | save_fname = self.user_ns.get('__file__',None) | |
2470 | self.user_ns['__file__'] = fname |
|
2456 | self.user_ns['__file__'] = fname | |
2471 | try: |
|
2457 | try: | |
2472 | py3compat.execfile(fname,*where) |
|
2458 | py3compat.execfile(fname,*where) | |
2473 | except SystemExit as status: |
|
2459 | except SystemExit as status: | |
2474 | # If the call was made with 0 or None exit status (sys.exit(0) |
|
2460 | # If the call was made with 0 or None exit status (sys.exit(0) | |
2475 | # or sys.exit() ), don't bother showing a traceback, as both of |
|
2461 | # or sys.exit() ), don't bother showing a traceback, as both of | |
2476 | # these are considered normal by the OS: |
|
2462 | # these are considered normal by the OS: | |
2477 | # > python -c'import sys;sys.exit(0)'; echo $? |
|
2463 | # > python -c'import sys;sys.exit(0)'; echo $? | |
2478 | # 0 |
|
2464 | # 0 | |
2479 | # > python -c'import sys;sys.exit()'; echo $? |
|
2465 | # > python -c'import sys;sys.exit()'; echo $? | |
2480 | # 0 |
|
2466 | # 0 | |
2481 | # For other exit status, we show the exception unless |
|
2467 | # For other exit status, we show the exception unless | |
2482 | # explicitly silenced, but only in short form. |
|
2468 | # explicitly silenced, but only in short form. | |
2483 | if kw['raise_exceptions']: |
|
2469 | if kw['raise_exceptions']: | |
2484 | raise |
|
2470 | raise | |
2485 | if status.code not in (0, None) and not kw['exit_ignore']: |
|
2471 | if status.code not in (0, None) and not kw['exit_ignore']: | |
2486 | self.showtraceback(exception_only=True) |
|
2472 | self.showtraceback(exception_only=True) | |
2487 | except: |
|
2473 | except: | |
2488 | if kw['raise_exceptions']: |
|
2474 | if kw['raise_exceptions']: | |
2489 | raise |
|
2475 | raise | |
2490 | self.showtraceback() |
|
2476 | self.showtraceback() | |
2491 | finally: |
|
2477 | finally: | |
2492 | self.user_ns['__file__'] = save_fname |
|
2478 | self.user_ns['__file__'] = save_fname | |
2493 |
|
2479 | |||
2494 | def safe_execfile_ipy(self, fname): |
|
2480 | def safe_execfile_ipy(self, fname): | |
2495 | """Like safe_execfile, but for .ipy files with IPython syntax. |
|
2481 | """Like safe_execfile, but for .ipy files with IPython syntax. | |
2496 |
|
2482 | |||
2497 | Parameters |
|
2483 | Parameters | |
2498 | ---------- |
|
2484 | ---------- | |
2499 | fname : str |
|
2485 | fname : str | |
2500 | The name of the file to execute. The filename must have a |
|
2486 | The name of the file to execute. The filename must have a | |
2501 | .ipy extension. |
|
2487 | .ipy extension. | |
2502 | """ |
|
2488 | """ | |
2503 | fname = os.path.abspath(os.path.expanduser(fname)) |
|
2489 | fname = os.path.abspath(os.path.expanduser(fname)) | |
2504 |
|
2490 | |||
2505 | # Make sure we can open the file |
|
2491 | # Make sure we can open the file | |
2506 | try: |
|
2492 | try: | |
2507 | with open(fname) as thefile: |
|
2493 | with open(fname) as thefile: | |
2508 | pass |
|
2494 | pass | |
2509 | except: |
|
2495 | except: | |
2510 | warn('Could not open file <%s> for safe execution.' % fname) |
|
2496 | warn('Could not open file <%s> for safe execution.' % fname) | |
2511 | return |
|
2497 | return | |
2512 |
|
2498 | |||
2513 | # Find things also in current directory. This is needed to mimic the |
|
2499 | # Find things also in current directory. This is needed to mimic the | |
2514 | # behavior of running a script from the system command line, where |
|
2500 | # behavior of running a script from the system command line, where | |
2515 | # Python inserts the script's directory into sys.path |
|
2501 | # Python inserts the script's directory into sys.path | |
2516 | dname = os.path.dirname(fname) |
|
2502 | dname = os.path.dirname(fname) | |
2517 |
|
2503 | |||
2518 | with prepended_to_syspath(dname): |
|
2504 | with prepended_to_syspath(dname): | |
2519 | # Ensure that __file__ is always defined to match Python behavior |
|
2505 | # Ensure that __file__ is always defined to match Python behavior | |
2520 | save_fname = self.user_ns.get('__file__',None) |
|
2506 | save_fname = self.user_ns.get('__file__',None) | |
2521 | self.user_ns['__file__'] = fname |
|
2507 | self.user_ns['__file__'] = fname | |
2522 | try: |
|
2508 | try: | |
2523 | with open(fname) as thefile: |
|
2509 | with open(fname) as thefile: | |
2524 | # self.run_cell currently captures all exceptions |
|
2510 | # self.run_cell currently captures all exceptions | |
2525 | # raised in user code. It would be nice if there were |
|
2511 | # raised in user code. It would be nice if there were | |
2526 | # versions of runlines, execfile that did raise, so |
|
2512 | # versions of runlines, execfile that did raise, so | |
2527 | # we could catch the errors. |
|
2513 | # we could catch the errors. | |
2528 | self.run_cell(thefile.read(), store_history=False) |
|
2514 | self.run_cell(thefile.read(), store_history=False) | |
2529 | except: |
|
2515 | except: | |
2530 | self.showtraceback() |
|
2516 | self.showtraceback() | |
2531 | warn('Unknown failure executing file: <%s>' % fname) |
|
2517 | warn('Unknown failure executing file: <%s>' % fname) | |
2532 | finally: |
|
2518 | finally: | |
2533 | self.user_ns['__file__'] = save_fname |
|
2519 | self.user_ns['__file__'] = save_fname | |
2534 |
|
2520 | |||
2535 | def safe_run_module(self, mod_name, where): |
|
2521 | def safe_run_module(self, mod_name, where): | |
2536 | """A safe version of runpy.run_module(). |
|
2522 | """A safe version of runpy.run_module(). | |
2537 |
|
2523 | |||
2538 | This version will never throw an exception, but instead print |
|
2524 | This version will never throw an exception, but instead print | |
2539 | helpful error messages to the screen. |
|
2525 | helpful error messages to the screen. | |
2540 |
|
2526 | |||
2541 | Parameters |
|
2527 | Parameters | |
2542 | ---------- |
|
2528 | ---------- | |
2543 | mod_name : string |
|
2529 | mod_name : string | |
2544 | The name of the module to be executed. |
|
2530 | The name of the module to be executed. | |
2545 | where : dict |
|
2531 | where : dict | |
2546 | The globals namespace. |
|
2532 | The globals namespace. | |
2547 | """ |
|
2533 | """ | |
2548 | try: |
|
2534 | try: | |
2549 | where.update( |
|
2535 | where.update( | |
2550 | runpy.run_module(str(mod_name), run_name="__main__", |
|
2536 | runpy.run_module(str(mod_name), run_name="__main__", | |
2551 | alter_sys=True) |
|
2537 | alter_sys=True) | |
2552 | ) |
|
2538 | ) | |
2553 | except: |
|
2539 | except: | |
2554 | self.showtraceback() |
|
2540 | self.showtraceback() | |
2555 | warn('Unknown failure executing module: <%s>' % mod_name) |
|
2541 | warn('Unknown failure executing module: <%s>' % mod_name) | |
2556 |
|
2542 | |||
2557 | def _run_cached_cell_magic(self, magic_name, line): |
|
2543 | def _run_cached_cell_magic(self, magic_name, line): | |
2558 | """Special method to call a cell magic with the data stored in self. |
|
2544 | """Special method to call a cell magic with the data stored in self. | |
2559 | """ |
|
2545 | """ | |
2560 | cell = self._current_cell_magic_body |
|
2546 | cell = self._current_cell_magic_body | |
2561 | self._current_cell_magic_body = None |
|
2547 | self._current_cell_magic_body = None | |
2562 | return self.run_cell_magic(magic_name, line, cell) |
|
2548 | return self.run_cell_magic(magic_name, line, cell) | |
2563 |
|
2549 | |||
2564 | def run_cell(self, raw_cell, store_history=False, silent=False): |
|
2550 | def run_cell(self, raw_cell, store_history=False, silent=False): | |
2565 | """Run a complete IPython cell. |
|
2551 | """Run a complete IPython cell. | |
2566 |
|
2552 | |||
2567 | Parameters |
|
2553 | Parameters | |
2568 | ---------- |
|
2554 | ---------- | |
2569 | raw_cell : str |
|
2555 | raw_cell : str | |
2570 | The code (including IPython code such as %magic functions) to run. |
|
2556 | The code (including IPython code such as %magic functions) to run. | |
2571 | store_history : bool |
|
2557 | store_history : bool | |
2572 | If True, the raw and translated cell will be stored in IPython's |
|
2558 | If True, the raw and translated cell will be stored in IPython's | |
2573 | history. For user code calling back into IPython's machinery, this |
|
2559 | history. For user code calling back into IPython's machinery, this | |
2574 | should be set to False. |
|
2560 | should be set to False. | |
2575 | silent : bool |
|
2561 | silent : bool | |
2576 | If True, avoid side-effets, such as implicit displayhooks, history, |
|
2562 | If True, avoid side-effets, such as implicit displayhooks, history, | |
2577 | and logging. silent=True forces store_history=False. |
|
2563 | and logging. silent=True forces store_history=False. | |
2578 | """ |
|
2564 | """ | |
2579 | if (not raw_cell) or raw_cell.isspace(): |
|
2565 | if (not raw_cell) or raw_cell.isspace(): | |
2580 | return |
|
2566 | return | |
2581 |
|
2567 | |||
2582 | if silent: |
|
2568 | if silent: | |
2583 | store_history = False |
|
2569 | store_history = False | |
2584 |
|
2570 | |||
2585 | self.input_splitter.push(raw_cell) |
|
2571 | self.input_splitter.push(raw_cell) | |
2586 |
|
2572 | |||
2587 | # Check for cell magics, which leave state behind. This interface is |
|
2573 | # Check for cell magics, which leave state behind. This interface is | |
2588 | # ugly, we need to do something cleaner later... Now the logic is |
|
2574 | # ugly, we need to do something cleaner later... Now the logic is | |
2589 | # simply that the input_splitter remembers if there was a cell magic, |
|
2575 | # simply that the input_splitter remembers if there was a cell magic, | |
2590 | # and in that case we grab the cell body. |
|
2576 | # and in that case we grab the cell body. | |
2591 | if self.input_splitter.cell_magic_parts: |
|
2577 | if self.input_splitter.cell_magic_parts: | |
2592 | self._current_cell_magic_body = \ |
|
2578 | self._current_cell_magic_body = \ | |
2593 | ''.join(self.input_splitter.cell_magic_parts) |
|
2579 | ''.join(self.input_splitter.cell_magic_parts) | |
2594 | cell = self.input_splitter.source_reset() |
|
2580 | cell = self.input_splitter.source_reset() | |
2595 |
|
2581 | |||
2596 | with self.builtin_trap: |
|
2582 | with self.builtin_trap: | |
2597 | prefilter_failed = False |
|
2583 | prefilter_failed = False | |
2598 | if len(cell.splitlines()) == 1: |
|
2584 | if len(cell.splitlines()) == 1: | |
2599 | try: |
|
2585 | try: | |
2600 | # use prefilter_lines to handle trailing newlines |
|
2586 | # use prefilter_lines to handle trailing newlines | |
2601 | # restore trailing newline for ast.parse |
|
2587 | # restore trailing newline for ast.parse | |
2602 | cell = self.prefilter_manager.prefilter_lines(cell) + '\n' |
|
2588 | cell = self.prefilter_manager.prefilter_lines(cell) + '\n' | |
2603 | except AliasError as e: |
|
2589 | except AliasError as e: | |
2604 | error(e) |
|
2590 | error(e) | |
2605 | prefilter_failed = True |
|
2591 | prefilter_failed = True | |
2606 | except Exception: |
|
2592 | except Exception: | |
2607 | # don't allow prefilter errors to crash IPython |
|
2593 | # don't allow prefilter errors to crash IPython | |
2608 | self.showtraceback() |
|
2594 | self.showtraceback() | |
2609 | prefilter_failed = True |
|
2595 | prefilter_failed = True | |
2610 |
|
2596 | |||
2611 | # Store raw and processed history |
|
2597 | # Store raw and processed history | |
2612 | if store_history: |
|
2598 | if store_history: | |
2613 | self.history_manager.store_inputs(self.execution_count, |
|
2599 | self.history_manager.store_inputs(self.execution_count, | |
2614 | cell, raw_cell) |
|
2600 | cell, raw_cell) | |
2615 | if not silent: |
|
2601 | if not silent: | |
2616 | self.logger.log(cell, raw_cell) |
|
2602 | self.logger.log(cell, raw_cell) | |
2617 |
|
2603 | |||
2618 | if not prefilter_failed: |
|
2604 | if not prefilter_failed: | |
2619 | # don't run if prefilter failed |
|
2605 | # don't run if prefilter failed | |
2620 | cell_name = self.compile.cache(cell, self.execution_count) |
|
2606 | cell_name = self.compile.cache(cell, self.execution_count) | |
2621 |
|
2607 | |||
2622 | with self.display_trap: |
|
2608 | with self.display_trap: | |
2623 | try: |
|
2609 | try: | |
2624 | code_ast = self.compile.ast_parse(cell, |
|
2610 | code_ast = self.compile.ast_parse(cell, | |
2625 | filename=cell_name) |
|
2611 | filename=cell_name) | |
2626 | except IndentationError: |
|
2612 | except IndentationError: | |
2627 | self.showindentationerror() |
|
2613 | self.showindentationerror() | |
2628 | if store_history: |
|
2614 | if store_history: | |
2629 | self.execution_count += 1 |
|
2615 | self.execution_count += 1 | |
2630 | return None |
|
2616 | return None | |
2631 | except (OverflowError, SyntaxError, ValueError, TypeError, |
|
2617 | except (OverflowError, SyntaxError, ValueError, TypeError, | |
2632 | MemoryError): |
|
2618 | MemoryError): | |
2633 | self.showsyntaxerror() |
|
2619 | self.showsyntaxerror() | |
2634 | if store_history: |
|
2620 | if store_history: | |
2635 | self.execution_count += 1 |
|
2621 | self.execution_count += 1 | |
2636 | return None |
|
2622 | return None | |
2637 |
|
2623 | |||
2638 | interactivity = "none" if silent else self.ast_node_interactivity |
|
2624 | interactivity = "none" if silent else self.ast_node_interactivity | |
2639 | self.run_ast_nodes(code_ast.body, cell_name, |
|
2625 | self.run_ast_nodes(code_ast.body, cell_name, | |
2640 | interactivity=interactivity) |
|
2626 | interactivity=interactivity) | |
2641 |
|
2627 | |||
2642 | # Execute any registered post-execution functions. |
|
2628 | # Execute any registered post-execution functions. | |
2643 | # unless we are silent |
|
2629 | # unless we are silent | |
2644 | post_exec = [] if silent else self._post_execute.iteritems() |
|
2630 | post_exec = [] if silent else self._post_execute.iteritems() | |
2645 |
|
2631 | |||
2646 | for func, status in post_exec: |
|
2632 | for func, status in post_exec: | |
2647 | if self.disable_failing_post_execute and not status: |
|
2633 | if self.disable_failing_post_execute and not status: | |
2648 | continue |
|
2634 | continue | |
2649 | try: |
|
2635 | try: | |
2650 | func() |
|
2636 | func() | |
2651 | except KeyboardInterrupt: |
|
2637 | except KeyboardInterrupt: | |
2652 | print("\nKeyboardInterrupt", file=io.stderr) |
|
2638 | print("\nKeyboardInterrupt", file=io.stderr) | |
2653 | except Exception: |
|
2639 | except Exception: | |
2654 | # register as failing: |
|
2640 | # register as failing: | |
2655 | self._post_execute[func] = False |
|
2641 | self._post_execute[func] = False | |
2656 | self.showtraceback() |
|
2642 | self.showtraceback() | |
2657 | print('\n'.join([ |
|
2643 | print('\n'.join([ | |
2658 | "post-execution function %r produced an error." % func, |
|
2644 | "post-execution function %r produced an error." % func, | |
2659 | "If this problem persists, you can disable failing post-exec functions with:", |
|
2645 | "If this problem persists, you can disable failing post-exec functions with:", | |
2660 | "", |
|
2646 | "", | |
2661 | " get_ipython().disable_failing_post_execute = True" |
|
2647 | " get_ipython().disable_failing_post_execute = True" | |
2662 | ]), file=io.stderr) |
|
2648 | ]), file=io.stderr) | |
2663 |
|
2649 | |||
2664 | if store_history: |
|
2650 | if store_history: | |
2665 | # Write output to the database. Does nothing unless |
|
2651 | # Write output to the database. Does nothing unless | |
2666 | # history output logging is enabled. |
|
2652 | # history output logging is enabled. | |
2667 | self.history_manager.store_output(self.execution_count) |
|
2653 | self.history_manager.store_output(self.execution_count) | |
2668 | # Each cell is a *single* input, regardless of how many lines it has |
|
2654 | # Each cell is a *single* input, regardless of how many lines it has | |
2669 | self.execution_count += 1 |
|
2655 | self.execution_count += 1 | |
2670 |
|
2656 | |||
2671 | def run_ast_nodes(self, nodelist, cell_name, interactivity='last_expr'): |
|
2657 | def run_ast_nodes(self, nodelist, cell_name, interactivity='last_expr'): | |
2672 | """Run a sequence of AST nodes. The execution mode depends on the |
|
2658 | """Run a sequence of AST nodes. The execution mode depends on the | |
2673 | interactivity parameter. |
|
2659 | interactivity parameter. | |
2674 |
|
2660 | |||
2675 | Parameters |
|
2661 | Parameters | |
2676 | ---------- |
|
2662 | ---------- | |
2677 | nodelist : list |
|
2663 | nodelist : list | |
2678 | A sequence of AST nodes to run. |
|
2664 | A sequence of AST nodes to run. | |
2679 | cell_name : str |
|
2665 | cell_name : str | |
2680 | Will be passed to the compiler as the filename of the cell. Typically |
|
2666 | Will be passed to the compiler as the filename of the cell. Typically | |
2681 | the value returned by ip.compile.cache(cell). |
|
2667 | the value returned by ip.compile.cache(cell). | |
2682 | interactivity : str |
|
2668 | interactivity : str | |
2683 | 'all', 'last', 'last_expr' or 'none', specifying which nodes should be |
|
2669 | 'all', 'last', 'last_expr' or 'none', specifying which nodes should be | |
2684 | run interactively (displaying output from expressions). 'last_expr' |
|
2670 | run interactively (displaying output from expressions). 'last_expr' | |
2685 | will run the last node interactively only if it is an expression (i.e. |
|
2671 | will run the last node interactively only if it is an expression (i.e. | |
2686 | expressions in loops or other blocks are not displayed. Other values |
|
2672 | expressions in loops or other blocks are not displayed. Other values | |
2687 | for this parameter will raise a ValueError. |
|
2673 | for this parameter will raise a ValueError. | |
2688 | """ |
|
2674 | """ | |
2689 | if not nodelist: |
|
2675 | if not nodelist: | |
2690 | return |
|
2676 | return | |
2691 |
|
2677 | |||
2692 | if interactivity == 'last_expr': |
|
2678 | if interactivity == 'last_expr': | |
2693 | if isinstance(nodelist[-1], ast.Expr): |
|
2679 | if isinstance(nodelist[-1], ast.Expr): | |
2694 | interactivity = "last" |
|
2680 | interactivity = "last" | |
2695 | else: |
|
2681 | else: | |
2696 | interactivity = "none" |
|
2682 | interactivity = "none" | |
2697 |
|
2683 | |||
2698 | if interactivity == 'none': |
|
2684 | if interactivity == 'none': | |
2699 | to_run_exec, to_run_interactive = nodelist, [] |
|
2685 | to_run_exec, to_run_interactive = nodelist, [] | |
2700 | elif interactivity == 'last': |
|
2686 | elif interactivity == 'last': | |
2701 | to_run_exec, to_run_interactive = nodelist[:-1], nodelist[-1:] |
|
2687 | to_run_exec, to_run_interactive = nodelist[:-1], nodelist[-1:] | |
2702 | elif interactivity == 'all': |
|
2688 | elif interactivity == 'all': | |
2703 | to_run_exec, to_run_interactive = [], nodelist |
|
2689 | to_run_exec, to_run_interactive = [], nodelist | |
2704 | else: |
|
2690 | else: | |
2705 | raise ValueError("Interactivity was %r" % interactivity) |
|
2691 | raise ValueError("Interactivity was %r" % interactivity) | |
2706 |
|
2692 | |||
2707 | exec_count = self.execution_count |
|
2693 | exec_count = self.execution_count | |
2708 |
|
2694 | |||
2709 | try: |
|
2695 | try: | |
2710 | for i, node in enumerate(to_run_exec): |
|
2696 | for i, node in enumerate(to_run_exec): | |
2711 | mod = ast.Module([node]) |
|
2697 | mod = ast.Module([node]) | |
2712 | code = self.compile(mod, cell_name, "exec") |
|
2698 | code = self.compile(mod, cell_name, "exec") | |
2713 | if self.run_code(code): |
|
2699 | if self.run_code(code): | |
2714 | return True |
|
2700 | return True | |
2715 |
|
2701 | |||
2716 | for i, node in enumerate(to_run_interactive): |
|
2702 | for i, node in enumerate(to_run_interactive): | |
2717 | mod = ast.Interactive([node]) |
|
2703 | mod = ast.Interactive([node]) | |
2718 | code = self.compile(mod, cell_name, "single") |
|
2704 | code = self.compile(mod, cell_name, "single") | |
2719 | if self.run_code(code): |
|
2705 | if self.run_code(code): | |
2720 | return True |
|
2706 | return True | |
2721 |
|
2707 | |||
2722 | # Flush softspace |
|
2708 | # Flush softspace | |
2723 | if softspace(sys.stdout, 0): |
|
2709 | if softspace(sys.stdout, 0): | |
2724 | print() |
|
2710 | print() | |
2725 |
|
2711 | |||
2726 | except: |
|
2712 | except: | |
2727 | # It's possible to have exceptions raised here, typically by |
|
2713 | # It's possible to have exceptions raised here, typically by | |
2728 | # compilation of odd code (such as a naked 'return' outside a |
|
2714 | # compilation of odd code (such as a naked 'return' outside a | |
2729 | # function) that did parse but isn't valid. Typically the exception |
|
2715 | # function) that did parse but isn't valid. Typically the exception | |
2730 | # is a SyntaxError, but it's safest just to catch anything and show |
|
2716 | # is a SyntaxError, but it's safest just to catch anything and show | |
2731 | # the user a traceback. |
|
2717 | # the user a traceback. | |
2732 |
|
2718 | |||
2733 | # We do only one try/except outside the loop to minimize the impact |
|
2719 | # We do only one try/except outside the loop to minimize the impact | |
2734 | # on runtime, and also because if any node in the node list is |
|
2720 | # on runtime, and also because if any node in the node list is | |
2735 | # broken, we should stop execution completely. |
|
2721 | # broken, we should stop execution completely. | |
2736 | self.showtraceback() |
|
2722 | self.showtraceback() | |
2737 |
|
2723 | |||
2738 | return False |
|
2724 | return False | |
2739 |
|
2725 | |||
2740 | def run_code(self, code_obj): |
|
2726 | def run_code(self, code_obj): | |
2741 | """Execute a code object. |
|
2727 | """Execute a code object. | |
2742 |
|
2728 | |||
2743 | When an exception occurs, self.showtraceback() is called to display a |
|
2729 | When an exception occurs, self.showtraceback() is called to display a | |
2744 | traceback. |
|
2730 | traceback. | |
2745 |
|
2731 | |||
2746 | Parameters |
|
2732 | Parameters | |
2747 | ---------- |
|
2733 | ---------- | |
2748 | code_obj : code object |
|
2734 | code_obj : code object | |
2749 | A compiled code object, to be executed |
|
2735 | A compiled code object, to be executed | |
2750 |
|
2736 | |||
2751 | Returns |
|
2737 | Returns | |
2752 | ------- |
|
2738 | ------- | |
2753 | False : successful execution. |
|
2739 | False : successful execution. | |
2754 | True : an error occurred. |
|
2740 | True : an error occurred. | |
2755 | """ |
|
2741 | """ | |
2756 |
|
2742 | |||
2757 | # Set our own excepthook in case the user code tries to call it |
|
2743 | # Set our own excepthook in case the user code tries to call it | |
2758 | # directly, so that the IPython crash handler doesn't get triggered |
|
2744 | # directly, so that the IPython crash handler doesn't get triggered | |
2759 | old_excepthook,sys.excepthook = sys.excepthook, self.excepthook |
|
2745 | old_excepthook,sys.excepthook = sys.excepthook, self.excepthook | |
2760 |
|
2746 | |||
2761 | # we save the original sys.excepthook in the instance, in case config |
|
2747 | # we save the original sys.excepthook in the instance, in case config | |
2762 | # code (such as magics) needs access to it. |
|
2748 | # code (such as magics) needs access to it. | |
2763 | self.sys_excepthook = old_excepthook |
|
2749 | self.sys_excepthook = old_excepthook | |
2764 | outflag = 1 # happens in more places, so it's easier as default |
|
2750 | outflag = 1 # happens in more places, so it's easier as default | |
2765 | try: |
|
2751 | try: | |
2766 | try: |
|
2752 | try: | |
2767 | self.hooks.pre_run_code_hook() |
|
2753 | self.hooks.pre_run_code_hook() | |
2768 | #rprint('Running code', repr(code_obj)) # dbg |
|
2754 | #rprint('Running code', repr(code_obj)) # dbg | |
2769 | exec code_obj in self.user_global_ns, self.user_ns |
|
2755 | exec code_obj in self.user_global_ns, self.user_ns | |
2770 | finally: |
|
2756 | finally: | |
2771 | # Reset our crash handler in place |
|
2757 | # Reset our crash handler in place | |
2772 | sys.excepthook = old_excepthook |
|
2758 | sys.excepthook = old_excepthook | |
2773 | except SystemExit: |
|
2759 | except SystemExit: | |
2774 | self.showtraceback(exception_only=True) |
|
2760 | self.showtraceback(exception_only=True) | |
2775 | warn("To exit: use 'exit', 'quit', or Ctrl-D.", level=1) |
|
2761 | warn("To exit: use 'exit', 'quit', or Ctrl-D.", level=1) | |
2776 | except self.custom_exceptions: |
|
2762 | except self.custom_exceptions: | |
2777 | etype,value,tb = sys.exc_info() |
|
2763 | etype,value,tb = sys.exc_info() | |
2778 | self.CustomTB(etype,value,tb) |
|
2764 | self.CustomTB(etype,value,tb) | |
2779 | except: |
|
2765 | except: | |
2780 | self.showtraceback() |
|
2766 | self.showtraceback() | |
2781 | else: |
|
2767 | else: | |
2782 | outflag = 0 |
|
2768 | outflag = 0 | |
2783 | return outflag |
|
2769 | return outflag | |
2784 |
|
2770 | |||
2785 | # For backwards compatibility |
|
2771 | # For backwards compatibility | |
2786 | runcode = run_code |
|
2772 | runcode = run_code | |
2787 |
|
2773 | |||
2788 | #------------------------------------------------------------------------- |
|
2774 | #------------------------------------------------------------------------- | |
2789 | # Things related to GUI support and pylab |
|
2775 | # Things related to GUI support and pylab | |
2790 | #------------------------------------------------------------------------- |
|
2776 | #------------------------------------------------------------------------- | |
2791 |
|
2777 | |||
2792 | def enable_gui(self, gui=None): |
|
2778 | def enable_gui(self, gui=None): | |
2793 | raise NotImplementedError('Implement enable_gui in a subclass') |
|
2779 | raise NotImplementedError('Implement enable_gui in a subclass') | |
2794 |
|
2780 | |||
2795 | def enable_pylab(self, gui=None, import_all=True): |
|
2781 | def enable_pylab(self, gui=None, import_all=True): | |
2796 | """Activate pylab support at runtime. |
|
2782 | """Activate pylab support at runtime. | |
2797 |
|
2783 | |||
2798 | This turns on support for matplotlib, preloads into the interactive |
|
2784 | This turns on support for matplotlib, preloads into the interactive | |
2799 | namespace all of numpy and pylab, and configures IPython to correctly |
|
2785 | namespace all of numpy and pylab, and configures IPython to correctly | |
2800 | interact with the GUI event loop. The GUI backend to be used can be |
|
2786 | interact with the GUI event loop. The GUI backend to be used can be | |
2801 | optionally selected with the optional :param:`gui` argument. |
|
2787 | optionally selected with the optional :param:`gui` argument. | |
2802 |
|
2788 | |||
2803 | Parameters |
|
2789 | Parameters | |
2804 | ---------- |
|
2790 | ---------- | |
2805 | gui : optional, string |
|
2791 | gui : optional, string | |
2806 |
|
2792 | |||
2807 | If given, dictates the choice of matplotlib GUI backend to use |
|
2793 | If given, dictates the choice of matplotlib GUI backend to use | |
2808 | (should be one of IPython's supported backends, 'qt', 'osx', 'tk', |
|
2794 | (should be one of IPython's supported backends, 'qt', 'osx', 'tk', | |
2809 | 'gtk', 'wx' or 'inline'), otherwise we use the default chosen by |
|
2795 | 'gtk', 'wx' or 'inline'), otherwise we use the default chosen by | |
2810 | matplotlib (as dictated by the matplotlib build-time options plus the |
|
2796 | matplotlib (as dictated by the matplotlib build-time options plus the | |
2811 | user's matplotlibrc configuration file). Note that not all backends |
|
2797 | user's matplotlibrc configuration file). Note that not all backends | |
2812 | make sense in all contexts, for example a terminal ipython can't |
|
2798 | make sense in all contexts, for example a terminal ipython can't | |
2813 | display figures inline. |
|
2799 | display figures inline. | |
2814 | """ |
|
2800 | """ | |
2815 | from IPython.core.pylabtools import mpl_runner |
|
2801 | from IPython.core.pylabtools import mpl_runner | |
2816 | # We want to prevent the loading of pylab to pollute the user's |
|
2802 | # We want to prevent the loading of pylab to pollute the user's | |
2817 | # namespace as shown by the %who* magics, so we execute the activation |
|
2803 | # namespace as shown by the %who* magics, so we execute the activation | |
2818 | # code in an empty namespace, and we update *both* user_ns and |
|
2804 | # code in an empty namespace, and we update *both* user_ns and | |
2819 | # user_ns_hidden with this information. |
|
2805 | # user_ns_hidden with this information. | |
2820 | ns = {} |
|
2806 | ns = {} | |
2821 | try: |
|
2807 | try: | |
2822 | gui = pylab_activate(ns, gui, import_all, self) |
|
2808 | gui = pylab_activate(ns, gui, import_all, self) | |
2823 | except KeyError: |
|
2809 | except KeyError: | |
2824 | error("Backend %r not supported" % gui) |
|
2810 | error("Backend %r not supported" % gui) | |
2825 | return |
|
2811 | return | |
2826 | self.user_ns.update(ns) |
|
2812 | self.user_ns.update(ns) | |
2827 | self.user_ns_hidden.update(ns) |
|
2813 | self.user_ns_hidden.update(ns) | |
2828 | # Now we must activate the gui pylab wants to use, and fix %run to take |
|
2814 | # Now we must activate the gui pylab wants to use, and fix %run to take | |
2829 | # plot updates into account |
|
2815 | # plot updates into account | |
2830 | self.enable_gui(gui) |
|
2816 | self.enable_gui(gui) | |
2831 | self.magics_manager.registry['ExecutionMagics'].default_runner = \ |
|
2817 | self.magics_manager.registry['ExecutionMagics'].default_runner = \ | |
2832 | mpl_runner(self.safe_execfile) |
|
2818 | mpl_runner(self.safe_execfile) | |
2833 |
|
2819 | |||
2834 | #------------------------------------------------------------------------- |
|
2820 | #------------------------------------------------------------------------- | |
2835 | # Utilities |
|
2821 | # Utilities | |
2836 | #------------------------------------------------------------------------- |
|
2822 | #------------------------------------------------------------------------- | |
2837 |
|
2823 | |||
2838 | def var_expand(self, cmd, depth=0, formatter=DollarFormatter()): |
|
2824 | def var_expand(self, cmd, depth=0, formatter=DollarFormatter()): | |
2839 | """Expand python variables in a string. |
|
2825 | """Expand python variables in a string. | |
2840 |
|
2826 | |||
2841 | The depth argument indicates how many frames above the caller should |
|
2827 | The depth argument indicates how many frames above the caller should | |
2842 | be walked to look for the local namespace where to expand variables. |
|
2828 | be walked to look for the local namespace where to expand variables. | |
2843 |
|
2829 | |||
2844 | The global namespace for expansion is always the user's interactive |
|
2830 | The global namespace for expansion is always the user's interactive | |
2845 | namespace. |
|
2831 | namespace. | |
2846 | """ |
|
2832 | """ | |
2847 | ns = self.user_ns.copy() |
|
2833 | ns = self.user_ns.copy() | |
2848 | ns.update(sys._getframe(depth+1).f_locals) |
|
2834 | ns.update(sys._getframe(depth+1).f_locals) | |
2849 | ns.pop('self', None) |
|
2835 | ns.pop('self', None) | |
2850 | try: |
|
2836 | try: | |
2851 | cmd = formatter.format(cmd, **ns) |
|
2837 | cmd = formatter.format(cmd, **ns) | |
2852 | except Exception: |
|
2838 | except Exception: | |
2853 | # if formatter couldn't format, just let it go untransformed |
|
2839 | # if formatter couldn't format, just let it go untransformed | |
2854 | pass |
|
2840 | pass | |
2855 | return cmd |
|
2841 | return cmd | |
2856 |
|
2842 | |||
2857 | def mktempfile(self, data=None, prefix='ipython_edit_'): |
|
2843 | def mktempfile(self, data=None, prefix='ipython_edit_'): | |
2858 | """Make a new tempfile and return its filename. |
|
2844 | """Make a new tempfile and return its filename. | |
2859 |
|
2845 | |||
2860 | This makes a call to tempfile.mktemp, but it registers the created |
|
2846 | This makes a call to tempfile.mktemp, but it registers the created | |
2861 | filename internally so ipython cleans it up at exit time. |
|
2847 | filename internally so ipython cleans it up at exit time. | |
2862 |
|
2848 | |||
2863 | Optional inputs: |
|
2849 | Optional inputs: | |
2864 |
|
2850 | |||
2865 | - data(None): if data is given, it gets written out to the temp file |
|
2851 | - data(None): if data is given, it gets written out to the temp file | |
2866 | immediately, and the file is closed again.""" |
|
2852 | immediately, and the file is closed again.""" | |
2867 |
|
2853 | |||
2868 | filename = tempfile.mktemp('.py', prefix) |
|
2854 | filename = tempfile.mktemp('.py', prefix) | |
2869 | self.tempfiles.append(filename) |
|
2855 | self.tempfiles.append(filename) | |
2870 |
|
2856 | |||
2871 | if data: |
|
2857 | if data: | |
2872 | tmp_file = open(filename,'w') |
|
2858 | tmp_file = open(filename,'w') | |
2873 | tmp_file.write(data) |
|
2859 | tmp_file.write(data) | |
2874 | tmp_file.close() |
|
2860 | tmp_file.close() | |
2875 | return filename |
|
2861 | return filename | |
2876 |
|
2862 | |||
2877 | # TODO: This should be removed when Term is refactored. |
|
2863 | # TODO: This should be removed when Term is refactored. | |
2878 | def write(self,data): |
|
2864 | def write(self,data): | |
2879 | """Write a string to the default output""" |
|
2865 | """Write a string to the default output""" | |
2880 | io.stdout.write(data) |
|
2866 | io.stdout.write(data) | |
2881 |
|
2867 | |||
2882 | # TODO: This should be removed when Term is refactored. |
|
2868 | # TODO: This should be removed when Term is refactored. | |
2883 | def write_err(self,data): |
|
2869 | def write_err(self,data): | |
2884 | """Write a string to the default error output""" |
|
2870 | """Write a string to the default error output""" | |
2885 | io.stderr.write(data) |
|
2871 | io.stderr.write(data) | |
2886 |
|
2872 | |||
2887 | def ask_yes_no(self, prompt, default=None): |
|
2873 | def ask_yes_no(self, prompt, default=None): | |
2888 | if self.quiet: |
|
2874 | if self.quiet: | |
2889 | return True |
|
2875 | return True | |
2890 | return ask_yes_no(prompt,default) |
|
2876 | return ask_yes_no(prompt,default) | |
2891 |
|
2877 | |||
2892 | def show_usage(self): |
|
2878 | def show_usage(self): | |
2893 | """Show a usage message""" |
|
2879 | """Show a usage message""" | |
2894 | page.page(IPython.core.usage.interactive_usage) |
|
2880 | page.page(IPython.core.usage.interactive_usage) | |
2895 |
|
2881 | |||
2896 | def extract_input_lines(self, range_str, raw=False): |
|
2882 | def extract_input_lines(self, range_str, raw=False): | |
2897 | """Return as a string a set of input history slices. |
|
2883 | """Return as a string a set of input history slices. | |
2898 |
|
2884 | |||
2899 | Parameters |
|
2885 | Parameters | |
2900 | ---------- |
|
2886 | ---------- | |
2901 | range_str : string |
|
2887 | range_str : string | |
2902 | The set of slices is given as a string, like "~5/6-~4/2 4:8 9", |
|
2888 | The set of slices is given as a string, like "~5/6-~4/2 4:8 9", | |
2903 | since this function is for use by magic functions which get their |
|
2889 | since this function is for use by magic functions which get their | |
2904 | arguments as strings. The number before the / is the session |
|
2890 | arguments as strings. The number before the / is the session | |
2905 | number: ~n goes n back from the current session. |
|
2891 | number: ~n goes n back from the current session. | |
2906 |
|
2892 | |||
2907 | Optional Parameters: |
|
2893 | Optional Parameters: | |
2908 | - raw(False): by default, the processed input is used. If this is |
|
2894 | - raw(False): by default, the processed input is used. If this is | |
2909 | true, the raw input history is used instead. |
|
2895 | true, the raw input history is used instead. | |
2910 |
|
2896 | |||
2911 | Note that slices can be called with two notations: |
|
2897 | Note that slices can be called with two notations: | |
2912 |
|
2898 | |||
2913 | N:M -> standard python form, means including items N...(M-1). |
|
2899 | N:M -> standard python form, means including items N...(M-1). | |
2914 |
|
2900 | |||
2915 | N-M -> include items N..M (closed endpoint).""" |
|
2901 | N-M -> include items N..M (closed endpoint).""" | |
2916 | lines = self.history_manager.get_range_by_str(range_str, raw=raw) |
|
2902 | lines = self.history_manager.get_range_by_str(range_str, raw=raw) | |
2917 | return "\n".join(x for _, _, x in lines) |
|
2903 | return "\n".join(x for _, _, x in lines) | |
2918 |
|
2904 | |||
2919 | def find_user_code(self, target, raw=True, py_only=False): |
|
2905 | def find_user_code(self, target, raw=True, py_only=False): | |
2920 | """Get a code string from history, file, url, or a string or macro. |
|
2906 | """Get a code string from history, file, url, or a string or macro. | |
2921 |
|
2907 | |||
2922 | This is mainly used by magic functions. |
|
2908 | This is mainly used by magic functions. | |
2923 |
|
2909 | |||
2924 | Parameters |
|
2910 | Parameters | |
2925 | ---------- |
|
2911 | ---------- | |
2926 |
|
2912 | |||
2927 | target : str |
|
2913 | target : str | |
2928 |
|
2914 | |||
2929 | A string specifying code to retrieve. This will be tried respectively |
|
2915 | A string specifying code to retrieve. This will be tried respectively | |
2930 | as: ranges of input history (see %history for syntax), url, |
|
2916 | as: ranges of input history (see %history for syntax), url, | |
2931 | correspnding .py file, filename, or an expression evaluating to a |
|
2917 | correspnding .py file, filename, or an expression evaluating to a | |
2932 | string or Macro in the user namespace. |
|
2918 | string or Macro in the user namespace. | |
2933 |
|
2919 | |||
2934 | raw : bool |
|
2920 | raw : bool | |
2935 | If true (default), retrieve raw history. Has no effect on the other |
|
2921 | If true (default), retrieve raw history. Has no effect on the other | |
2936 | retrieval mechanisms. |
|
2922 | retrieval mechanisms. | |
2937 |
|
2923 | |||
2938 | py_only : bool (default False) |
|
2924 | py_only : bool (default False) | |
2939 | Only try to fetch python code, do not try alternative methods to decode file |
|
2925 | Only try to fetch python code, do not try alternative methods to decode file | |
2940 | if unicode fails. |
|
2926 | if unicode fails. | |
2941 |
|
2927 | |||
2942 | Returns |
|
2928 | Returns | |
2943 | ------- |
|
2929 | ------- | |
2944 | A string of code. |
|
2930 | A string of code. | |
2945 |
|
2931 | |||
2946 | ValueError is raised if nothing is found, and TypeError if it evaluates |
|
2932 | ValueError is raised if nothing is found, and TypeError if it evaluates | |
2947 | to an object of another type. In each case, .args[0] is a printable |
|
2933 | to an object of another type. In each case, .args[0] is a printable | |
2948 | message. |
|
2934 | message. | |
2949 | """ |
|
2935 | """ | |
2950 | code = self.extract_input_lines(target, raw=raw) # Grab history |
|
2936 | code = self.extract_input_lines(target, raw=raw) # Grab history | |
2951 | if code: |
|
2937 | if code: | |
2952 | return code |
|
2938 | return code | |
2953 | utarget = unquote_filename(target) |
|
2939 | utarget = unquote_filename(target) | |
2954 | try: |
|
2940 | try: | |
2955 | if utarget.startswith(('http://', 'https://')): |
|
2941 | if utarget.startswith(('http://', 'https://')): | |
2956 | return openpy.read_py_url(utarget, skip_encoding_cookie=True) |
|
2942 | return openpy.read_py_url(utarget, skip_encoding_cookie=True) | |
2957 | except UnicodeDecodeError: |
|
2943 | except UnicodeDecodeError: | |
2958 | if not py_only : |
|
2944 | if not py_only : | |
2959 | response = urllib.urlopen(target) |
|
2945 | response = urllib.urlopen(target) | |
2960 | return response.read().decode('latin1') |
|
2946 | return response.read().decode('latin1') | |
2961 | raise ValueError(("'%s' seem to be unreadable.") % utarget) |
|
2947 | raise ValueError(("'%s' seem to be unreadable.") % utarget) | |
2962 |
|
2948 | |||
2963 | potential_target = [target] |
|
2949 | potential_target = [target] | |
2964 | try : |
|
2950 | try : | |
2965 | potential_target.insert(0,get_py_filename(target)) |
|
2951 | potential_target.insert(0,get_py_filename(target)) | |
2966 | except IOError: |
|
2952 | except IOError: | |
2967 | pass |
|
2953 | pass | |
2968 |
|
2954 | |||
2969 | for tgt in potential_target : |
|
2955 | for tgt in potential_target : | |
2970 | if os.path.isfile(tgt): # Read file |
|
2956 | if os.path.isfile(tgt): # Read file | |
2971 | try : |
|
2957 | try : | |
2972 | return openpy.read_py_file(tgt, skip_encoding_cookie=True) |
|
2958 | return openpy.read_py_file(tgt, skip_encoding_cookie=True) | |
2973 | except UnicodeDecodeError : |
|
2959 | except UnicodeDecodeError : | |
2974 | if not py_only : |
|
2960 | if not py_only : | |
2975 | with io_open(tgt,'r', encoding='latin1') as f : |
|
2961 | with io_open(tgt,'r', encoding='latin1') as f : | |
2976 | return f.read() |
|
2962 | return f.read() | |
2977 | raise ValueError(("'%s' seem to be unreadable.") % target) |
|
2963 | raise ValueError(("'%s' seem to be unreadable.") % target) | |
2978 |
|
2964 | |||
2979 | try: # User namespace |
|
2965 | try: # User namespace | |
2980 | codeobj = eval(target, self.user_ns) |
|
2966 | codeobj = eval(target, self.user_ns) | |
2981 | except Exception: |
|
2967 | except Exception: | |
2982 | raise ValueError(("'%s' was not found in history, as a file, url, " |
|
2968 | raise ValueError(("'%s' was not found in history, as a file, url, " | |
2983 | "nor in the user namespace.") % target) |
|
2969 | "nor in the user namespace.") % target) | |
2984 | if isinstance(codeobj, basestring): |
|
2970 | if isinstance(codeobj, basestring): | |
2985 | return codeobj |
|
2971 | return codeobj | |
2986 | elif isinstance(codeobj, Macro): |
|
2972 | elif isinstance(codeobj, Macro): | |
2987 | return codeobj.value |
|
2973 | return codeobj.value | |
2988 |
|
2974 | |||
2989 | raise TypeError("%s is neither a string nor a macro." % target, |
|
2975 | raise TypeError("%s is neither a string nor a macro." % target, | |
2990 | codeobj) |
|
2976 | codeobj) | |
2991 |
|
2977 | |||
2992 | #------------------------------------------------------------------------- |
|
2978 | #------------------------------------------------------------------------- | |
2993 | # Things related to IPython exiting |
|
2979 | # Things related to IPython exiting | |
2994 | #------------------------------------------------------------------------- |
|
2980 | #------------------------------------------------------------------------- | |
2995 | def atexit_operations(self): |
|
2981 | def atexit_operations(self): | |
2996 | """This will be executed at the time of exit. |
|
2982 | """This will be executed at the time of exit. | |
2997 |
|
2983 | |||
2998 | Cleanup operations and saving of persistent data that is done |
|
2984 | Cleanup operations and saving of persistent data that is done | |
2999 | unconditionally by IPython should be performed here. |
|
2985 | unconditionally by IPython should be performed here. | |
3000 |
|
2986 | |||
3001 | For things that may depend on startup flags or platform specifics (such |
|
2987 | For things that may depend on startup flags or platform specifics (such | |
3002 | as having readline or not), register a separate atexit function in the |
|
2988 | as having readline or not), register a separate atexit function in the | |
3003 | code that has the appropriate information, rather than trying to |
|
2989 | code that has the appropriate information, rather than trying to | |
3004 | clutter |
|
2990 | clutter | |
3005 | """ |
|
2991 | """ | |
3006 | # Close the history session (this stores the end time and line count) |
|
2992 | # Close the history session (this stores the end time and line count) | |
3007 | # this must be *before* the tempfile cleanup, in case of temporary |
|
2993 | # this must be *before* the tempfile cleanup, in case of temporary | |
3008 | # history db |
|
2994 | # history db | |
3009 | self.history_manager.end_session() |
|
2995 | self.history_manager.end_session() | |
3010 |
|
2996 | |||
3011 | # Cleanup all tempfiles left around |
|
2997 | # Cleanup all tempfiles left around | |
3012 | for tfile in self.tempfiles: |
|
2998 | for tfile in self.tempfiles: | |
3013 | try: |
|
2999 | try: | |
3014 | os.unlink(tfile) |
|
3000 | os.unlink(tfile) | |
3015 | except OSError: |
|
3001 | except OSError: | |
3016 | pass |
|
3002 | pass | |
3017 |
|
3003 | |||
3018 | # Clear all user namespaces to release all references cleanly. |
|
3004 | # Clear all user namespaces to release all references cleanly. | |
3019 | self.reset(new_session=False) |
|
3005 | self.reset(new_session=False) | |
3020 |
|
3006 | |||
3021 | # Run user hooks |
|
3007 | # Run user hooks | |
3022 | self.hooks.shutdown_hook() |
|
3008 | self.hooks.shutdown_hook() | |
3023 |
|
3009 | |||
3024 | def cleanup(self): |
|
3010 | def cleanup(self): | |
3025 | self.restore_sys_module_state() |
|
3011 | self.restore_sys_module_state() | |
3026 |
|
3012 | |||
3027 |
|
3013 | |||
3028 | class InteractiveShellABC(object): |
|
3014 | class InteractiveShellABC(object): | |
3029 | """An abstract base class for InteractiveShell.""" |
|
3015 | """An abstract base class for InteractiveShell.""" | |
3030 | __metaclass__ = abc.ABCMeta |
|
3016 | __metaclass__ = abc.ABCMeta | |
3031 |
|
3017 | |||
3032 | InteractiveShellABC.register(InteractiveShell) |
|
3018 | InteractiveShellABC.register(InteractiveShell) |
@@ -1,332 +1,335 b'' | |||||
1 | # encoding: utf-8 |
|
1 | # encoding: utf-8 | |
2 |
|
2 | |||
3 | """Classes and functions for kernel related errors and exceptions. |
|
3 | """Classes and functions for kernel related errors and exceptions. | |
4 |
|
4 | |||
5 | Authors: |
|
5 | Authors: | |
6 |
|
6 | |||
7 | * Brian Granger |
|
7 | * Brian Granger | |
8 | * Min RK |
|
8 | * Min RK | |
9 | """ |
|
9 | """ | |
10 | from __future__ import print_function |
|
10 | from __future__ import print_function | |
11 |
|
11 | |||
12 | import sys |
|
12 | import sys | |
13 | import traceback |
|
13 | import traceback | |
14 |
|
14 | |||
15 | __docformat__ = "restructuredtext en" |
|
15 | __docformat__ = "restructuredtext en" | |
16 |
|
16 | |||
17 | # Tell nose to skip this module |
|
17 | # Tell nose to skip this module | |
18 | __test__ = {} |
|
18 | __test__ = {} | |
19 |
|
19 | |||
20 | #------------------------------------------------------------------------------- |
|
20 | #------------------------------------------------------------------------------- | |
21 | # Copyright (C) 2008-2011 The IPython Development Team |
|
21 | # Copyright (C) 2008-2011 The IPython Development Team | |
22 | # |
|
22 | # | |
23 | # Distributed under the terms of the BSD License. The full license is in |
|
23 | # Distributed under the terms of the BSD License. The full license is in | |
24 | # the file COPYING, distributed as part of this software. |
|
24 | # the file COPYING, distributed as part of this software. | |
25 | #------------------------------------------------------------------------------- |
|
25 | #------------------------------------------------------------------------------- | |
26 |
|
26 | |||
27 | #------------------------------------------------------------------------------- |
|
27 | #------------------------------------------------------------------------------- | |
28 | # Error classes |
|
28 | # Error classes | |
29 | #------------------------------------------------------------------------------- |
|
29 | #------------------------------------------------------------------------------- | |
30 | class IPythonError(Exception): |
|
30 | class IPythonError(Exception): | |
31 | """Base exception that all of our exceptions inherit from. |
|
31 | """Base exception that all of our exceptions inherit from. | |
32 |
|
32 | |||
33 | This can be raised by code that doesn't have any more specific |
|
33 | This can be raised by code that doesn't have any more specific | |
34 | information.""" |
|
34 | information.""" | |
35 |
|
35 | |||
36 | pass |
|
36 | pass | |
37 |
|
37 | |||
38 | # Exceptions associated with the controller objects |
|
38 | # Exceptions associated with the controller objects | |
39 | class ControllerError(IPythonError): pass |
|
39 | class ControllerError(IPythonError): pass | |
40 |
|
40 | |||
41 | class ControllerCreationError(ControllerError): pass |
|
41 | class ControllerCreationError(ControllerError): pass | |
42 |
|
42 | |||
43 |
|
43 | |||
44 | # Exceptions associated with the Engines |
|
44 | # Exceptions associated with the Engines | |
45 | class EngineError(IPythonError): pass |
|
45 | class EngineError(IPythonError): pass | |
46 |
|
46 | |||
47 | class EngineCreationError(EngineError): pass |
|
47 | class EngineCreationError(EngineError): pass | |
48 |
|
48 | |||
49 | class KernelError(IPythonError): |
|
49 | class KernelError(IPythonError): | |
50 | pass |
|
50 | pass | |
51 |
|
51 | |||
52 | class NotDefined(KernelError): |
|
52 | class NotDefined(KernelError): | |
53 | def __init__(self, name): |
|
53 | def __init__(self, name): | |
54 | self.name = name |
|
54 | self.name = name | |
55 | self.args = (name,) |
|
55 | self.args = (name,) | |
56 |
|
56 | |||
57 | def __repr__(self): |
|
57 | def __repr__(self): | |
58 | return '<NotDefined: %s>' % self.name |
|
58 | return '<NotDefined: %s>' % self.name | |
59 |
|
59 | |||
60 | __str__ = __repr__ |
|
60 | __str__ = __repr__ | |
61 |
|
61 | |||
62 |
|
62 | |||
63 | class QueueCleared(KernelError): |
|
63 | class QueueCleared(KernelError): | |
64 | pass |
|
64 | pass | |
65 |
|
65 | |||
66 |
|
66 | |||
67 | class IdInUse(KernelError): |
|
67 | class IdInUse(KernelError): | |
68 | pass |
|
68 | pass | |
69 |
|
69 | |||
70 |
|
70 | |||
71 | class ProtocolError(KernelError): |
|
71 | class ProtocolError(KernelError): | |
72 | pass |
|
72 | pass | |
73 |
|
73 | |||
74 |
|
74 | |||
75 | class ConnectionError(KernelError): |
|
75 | class ConnectionError(KernelError): | |
76 | pass |
|
76 | pass | |
77 |
|
77 | |||
78 |
|
78 | |||
79 | class InvalidEngineID(KernelError): |
|
79 | class InvalidEngineID(KernelError): | |
80 | pass |
|
80 | pass | |
81 |
|
81 | |||
82 |
|
82 | |||
83 | class NoEnginesRegistered(KernelError): |
|
83 | class NoEnginesRegistered(KernelError): | |
84 | pass |
|
84 | pass | |
85 |
|
85 | |||
86 |
|
86 | |||
87 | class InvalidClientID(KernelError): |
|
87 | class InvalidClientID(KernelError): | |
88 | pass |
|
88 | pass | |
89 |
|
89 | |||
90 |
|
90 | |||
91 | class InvalidDeferredID(KernelError): |
|
91 | class InvalidDeferredID(KernelError): | |
92 | pass |
|
92 | pass | |
93 |
|
93 | |||
94 |
|
94 | |||
95 | class SerializationError(KernelError): |
|
95 | class SerializationError(KernelError): | |
96 | pass |
|
96 | pass | |
97 |
|
97 | |||
98 |
|
98 | |||
99 | class MessageSizeError(KernelError): |
|
99 | class MessageSizeError(KernelError): | |
100 | pass |
|
100 | pass | |
101 |
|
101 | |||
102 |
|
102 | |||
103 | class PBMessageSizeError(MessageSizeError): |
|
103 | class PBMessageSizeError(MessageSizeError): | |
104 | pass |
|
104 | pass | |
105 |
|
105 | |||
106 |
|
106 | |||
107 | class ResultNotCompleted(KernelError): |
|
107 | class ResultNotCompleted(KernelError): | |
108 | pass |
|
108 | pass | |
109 |
|
109 | |||
110 |
|
110 | |||
111 | class ResultAlreadyRetrieved(KernelError): |
|
111 | class ResultAlreadyRetrieved(KernelError): | |
112 | pass |
|
112 | pass | |
113 |
|
113 | |||
114 | class ClientError(KernelError): |
|
114 | class ClientError(KernelError): | |
115 | pass |
|
115 | pass | |
116 |
|
116 | |||
117 |
|
117 | |||
118 | class TaskAborted(KernelError): |
|
118 | class TaskAborted(KernelError): | |
119 | pass |
|
119 | pass | |
120 |
|
120 | |||
121 |
|
121 | |||
122 | class TaskTimeout(KernelError): |
|
122 | class TaskTimeout(KernelError): | |
123 | pass |
|
123 | pass | |
124 |
|
124 | |||
125 |
|
125 | |||
126 | class NotAPendingResult(KernelError): |
|
126 | class NotAPendingResult(KernelError): | |
127 | pass |
|
127 | pass | |
128 |
|
128 | |||
129 |
|
129 | |||
130 | class UnpickleableException(KernelError): |
|
130 | class UnpickleableException(KernelError): | |
131 | pass |
|
131 | pass | |
132 |
|
132 | |||
133 |
|
133 | |||
134 | class AbortedPendingDeferredError(KernelError): |
|
134 | class AbortedPendingDeferredError(KernelError): | |
135 | pass |
|
135 | pass | |
136 |
|
136 | |||
137 |
|
137 | |||
138 | class InvalidProperty(KernelError): |
|
138 | class InvalidProperty(KernelError): | |
139 | pass |
|
139 | pass | |
140 |
|
140 | |||
141 |
|
141 | |||
142 | class MissingBlockArgument(KernelError): |
|
142 | class MissingBlockArgument(KernelError): | |
143 | pass |
|
143 | pass | |
144 |
|
144 | |||
145 |
|
145 | |||
146 | class StopLocalExecution(KernelError): |
|
146 | class StopLocalExecution(KernelError): | |
147 | pass |
|
147 | pass | |
148 |
|
148 | |||
149 |
|
149 | |||
150 | class SecurityError(KernelError): |
|
150 | class SecurityError(KernelError): | |
151 | pass |
|
151 | pass | |
152 |
|
152 | |||
153 |
|
153 | |||
154 | class FileTimeoutError(KernelError): |
|
154 | class FileTimeoutError(KernelError): | |
155 | pass |
|
155 | pass | |
156 |
|
156 | |||
157 | class TimeoutError(KernelError): |
|
157 | class TimeoutError(KernelError): | |
158 | pass |
|
158 | pass | |
159 |
|
159 | |||
160 | class UnmetDependency(KernelError): |
|
160 | class UnmetDependency(KernelError): | |
161 | pass |
|
161 | pass | |
162 |
|
162 | |||
163 | class ImpossibleDependency(UnmetDependency): |
|
163 | class ImpossibleDependency(UnmetDependency): | |
164 | pass |
|
164 | pass | |
165 |
|
165 | |||
166 | class DependencyTimeout(ImpossibleDependency): |
|
166 | class DependencyTimeout(ImpossibleDependency): | |
167 | pass |
|
167 | pass | |
168 |
|
168 | |||
169 | class InvalidDependency(ImpossibleDependency): |
|
169 | class InvalidDependency(ImpossibleDependency): | |
170 | pass |
|
170 | pass | |
171 |
|
171 | |||
172 | class RemoteError(KernelError): |
|
172 | class RemoteError(KernelError): | |
173 | """Error raised elsewhere""" |
|
173 | """Error raised elsewhere""" | |
174 | ename=None |
|
174 | ename=None | |
175 | evalue=None |
|
175 | evalue=None | |
176 | traceback=None |
|
176 | traceback=None | |
177 | engine_info=None |
|
177 | engine_info=None | |
178 |
|
178 | |||
179 | def __init__(self, ename, evalue, traceback, engine_info=None): |
|
179 | def __init__(self, ename, evalue, traceback, engine_info=None): | |
180 | self.ename=ename |
|
180 | self.ename=ename | |
181 | self.evalue=evalue |
|
181 | self.evalue=evalue | |
182 | self.traceback=traceback |
|
182 | self.traceback=traceback | |
183 | self.engine_info=engine_info or {} |
|
183 | self.engine_info=engine_info or {} | |
184 | self.args=(ename, evalue) |
|
184 | self.args=(ename, evalue) | |
185 |
|
185 | |||
186 | def __repr__(self): |
|
186 | def __repr__(self): | |
187 | engineid = self.engine_info.get('engine_id', ' ') |
|
187 | engineid = self.engine_info.get('engine_id', ' ') | |
188 | return "<Remote[%s]:%s(%s)>"%(engineid, self.ename, self.evalue) |
|
188 | return "<Remote[%s]:%s(%s)>"%(engineid, self.ename, self.evalue) | |
189 |
|
189 | |||
190 | def __str__(self): |
|
190 | def __str__(self): | |
191 | return "%s(%s)" % (self.ename, self.evalue) |
|
191 | return "%s(%s)" % (self.ename, self.evalue) | |
192 |
|
192 | |||
193 | def render_traceback(self): |
|
193 | def _render_traceback_(self): | |
194 | """render traceback to a list of lines""" |
|
194 | """render traceback to a list of lines""" | |
195 | return (self.traceback or "No traceback available").splitlines() |
|
195 | return (self.traceback or "No traceback available").splitlines() | |
|
196 | ||||
|
197 | # Previous name retained for backwards compatibility. | |||
|
198 | render_traceback = _render_traceback_ | |||
196 |
|
199 | |||
197 | def print_traceback(self, excid=None): |
|
200 | def print_traceback(self, excid=None): | |
198 | """print my traceback""" |
|
201 | """print my traceback""" | |
199 | print('\n'.join(self.render_traceback())) |
|
202 | print('\n'.join(self.render_traceback())) | |
200 |
|
203 | |||
201 |
|
204 | |||
202 |
|
205 | |||
203 |
|
206 | |||
204 | class TaskRejectError(KernelError): |
|
207 | class TaskRejectError(KernelError): | |
205 | """Exception to raise when a task should be rejected by an engine. |
|
208 | """Exception to raise when a task should be rejected by an engine. | |
206 |
|
209 | |||
207 | This exception can be used to allow a task running on an engine to test |
|
210 | This exception can be used to allow a task running on an engine to test | |
208 | if the engine (or the user's namespace on the engine) has the needed |
|
211 | if the engine (or the user's namespace on the engine) has the needed | |
209 | task dependencies. If not, the task should raise this exception. For |
|
212 | task dependencies. If not, the task should raise this exception. For | |
210 | the task to be retried on another engine, the task should be created |
|
213 | the task to be retried on another engine, the task should be created | |
211 | with the `retries` argument > 1. |
|
214 | with the `retries` argument > 1. | |
212 |
|
215 | |||
213 | The advantage of this approach over our older properties system is that |
|
216 | The advantage of this approach over our older properties system is that | |
214 | tasks have full access to the user's namespace on the engines and the |
|
217 | tasks have full access to the user's namespace on the engines and the | |
215 | properties don't have to be managed or tested by the controller. |
|
218 | properties don't have to be managed or tested by the controller. | |
216 | """ |
|
219 | """ | |
217 |
|
220 | |||
218 |
|
221 | |||
219 | class CompositeError(RemoteError): |
|
222 | class CompositeError(RemoteError): | |
220 | """Error for representing possibly multiple errors on engines""" |
|
223 | """Error for representing possibly multiple errors on engines""" | |
221 | def __init__(self, message, elist): |
|
224 | def __init__(self, message, elist): | |
222 | Exception.__init__(self, *(message, elist)) |
|
225 | Exception.__init__(self, *(message, elist)) | |
223 | # Don't use pack_exception because it will conflict with the .message |
|
226 | # Don't use pack_exception because it will conflict with the .message | |
224 | # attribute that is being deprecated in 2.6 and beyond. |
|
227 | # attribute that is being deprecated in 2.6 and beyond. | |
225 | self.msg = message |
|
228 | self.msg = message | |
226 | self.elist = elist |
|
229 | self.elist = elist | |
227 | self.args = [ e[0] for e in elist ] |
|
230 | self.args = [ e[0] for e in elist ] | |
228 |
|
231 | |||
229 | def _get_engine_str(self, ei): |
|
232 | def _get_engine_str(self, ei): | |
230 | if not ei: |
|
233 | if not ei: | |
231 | return '[Engine Exception]' |
|
234 | return '[Engine Exception]' | |
232 | else: |
|
235 | else: | |
233 | return '[%s:%s]: ' % (ei['engine_id'], ei['method']) |
|
236 | return '[%s:%s]: ' % (ei['engine_id'], ei['method']) | |
234 |
|
237 | |||
235 | def _get_traceback(self, ev): |
|
238 | def _get_traceback(self, ev): | |
236 | try: |
|
239 | try: | |
237 | tb = ev._ipython_traceback_text |
|
240 | tb = ev._ipython_traceback_text | |
238 | except AttributeError: |
|
241 | except AttributeError: | |
239 | return 'No traceback available' |
|
242 | return 'No traceback available' | |
240 | else: |
|
243 | else: | |
241 | return tb |
|
244 | return tb | |
242 |
|
245 | |||
243 | def __str__(self): |
|
246 | def __str__(self): | |
244 | s = str(self.msg) |
|
247 | s = str(self.msg) | |
245 | for en, ev, etb, ei in self.elist: |
|
248 | for en, ev, etb, ei in self.elist: | |
246 | engine_str = self._get_engine_str(ei) |
|
249 | engine_str = self._get_engine_str(ei) | |
247 | s = s + '\n' + engine_str + en + ': ' + str(ev) |
|
250 | s = s + '\n' + engine_str + en + ': ' + str(ev) | |
248 | return s |
|
251 | return s | |
249 |
|
252 | |||
250 | def __repr__(self): |
|
253 | def __repr__(self): | |
251 | return "CompositeError(%i)"%len(self.elist) |
|
254 | return "CompositeError(%i)"%len(self.elist) | |
252 |
|
255 | |||
253 | def render_traceback(self, excid=None): |
|
256 | def render_traceback(self, excid=None): | |
254 | """render one or all of my tracebacks to a list of lines""" |
|
257 | """render one or all of my tracebacks to a list of lines""" | |
255 | lines = [] |
|
258 | lines = [] | |
256 | if excid is None: |
|
259 | if excid is None: | |
257 | for (en,ev,etb,ei) in self.elist: |
|
260 | for (en,ev,etb,ei) in self.elist: | |
258 | lines.append(self._get_engine_str(ei)) |
|
261 | lines.append(self._get_engine_str(ei)) | |
259 | lines.extend((etb or 'No traceback available').splitlines()) |
|
262 | lines.extend((etb or 'No traceback available').splitlines()) | |
260 | lines.append('') |
|
263 | lines.append('') | |
261 | else: |
|
264 | else: | |
262 | try: |
|
265 | try: | |
263 | en,ev,etb,ei = self.elist[excid] |
|
266 | en,ev,etb,ei = self.elist[excid] | |
264 | except: |
|
267 | except: | |
265 | raise IndexError("an exception with index %i does not exist"%excid) |
|
268 | raise IndexError("an exception with index %i does not exist"%excid) | |
266 | else: |
|
269 | else: | |
267 | lines.append(self._get_engine_str(ei)) |
|
270 | lines.append(self._get_engine_str(ei)) | |
268 | lines.extend((etb or 'No traceback available').splitlines()) |
|
271 | lines.extend((etb or 'No traceback available').splitlines()) | |
269 |
|
272 | |||
270 | return lines |
|
273 | return lines | |
271 |
|
274 | |||
272 | def print_traceback(self, excid=None): |
|
275 | def print_traceback(self, excid=None): | |
273 | print('\n'.join(self.render_traceback(excid))) |
|
276 | print('\n'.join(self.render_traceback(excid))) | |
274 |
|
277 | |||
275 | def raise_exception(self, excid=0): |
|
278 | def raise_exception(self, excid=0): | |
276 | try: |
|
279 | try: | |
277 | en,ev,etb,ei = self.elist[excid] |
|
280 | en,ev,etb,ei = self.elist[excid] | |
278 | except: |
|
281 | except: | |
279 | raise IndexError("an exception with index %i does not exist"%excid) |
|
282 | raise IndexError("an exception with index %i does not exist"%excid) | |
280 | else: |
|
283 | else: | |
281 | raise RemoteError(en, ev, etb, ei) |
|
284 | raise RemoteError(en, ev, etb, ei) | |
282 |
|
285 | |||
283 |
|
286 | |||
284 | def collect_exceptions(rdict_or_list, method='unspecified'): |
|
287 | def collect_exceptions(rdict_or_list, method='unspecified'): | |
285 | """check a result dict for errors, and raise CompositeError if any exist. |
|
288 | """check a result dict for errors, and raise CompositeError if any exist. | |
286 | Passthrough otherwise.""" |
|
289 | Passthrough otherwise.""" | |
287 | elist = [] |
|
290 | elist = [] | |
288 | if isinstance(rdict_or_list, dict): |
|
291 | if isinstance(rdict_or_list, dict): | |
289 | rlist = rdict_or_list.values() |
|
292 | rlist = rdict_or_list.values() | |
290 | else: |
|
293 | else: | |
291 | rlist = rdict_or_list |
|
294 | rlist = rdict_or_list | |
292 | for r in rlist: |
|
295 | for r in rlist: | |
293 | if isinstance(r, RemoteError): |
|
296 | if isinstance(r, RemoteError): | |
294 | en, ev, etb, ei = r.ename, r.evalue, r.traceback, r.engine_info |
|
297 | en, ev, etb, ei = r.ename, r.evalue, r.traceback, r.engine_info | |
295 | # Sometimes we could have CompositeError in our list. Just take |
|
298 | # Sometimes we could have CompositeError in our list. Just take | |
296 | # the errors out of them and put them in our new list. This |
|
299 | # the errors out of them and put them in our new list. This | |
297 | # has the effect of flattening lists of CompositeErrors into one |
|
300 | # has the effect of flattening lists of CompositeErrors into one | |
298 | # CompositeError |
|
301 | # CompositeError | |
299 | if en=='CompositeError': |
|
302 | if en=='CompositeError': | |
300 | for e in ev.elist: |
|
303 | for e in ev.elist: | |
301 | elist.append(e) |
|
304 | elist.append(e) | |
302 | else: |
|
305 | else: | |
303 | elist.append((en, ev, etb, ei)) |
|
306 | elist.append((en, ev, etb, ei)) | |
304 | if len(elist)==0: |
|
307 | if len(elist)==0: | |
305 | return rdict_or_list |
|
308 | return rdict_or_list | |
306 | else: |
|
309 | else: | |
307 | msg = "one or more exceptions from call to method: %s" % (method) |
|
310 | msg = "one or more exceptions from call to method: %s" % (method) | |
308 | # This silliness is needed so the debugger has access to the exception |
|
311 | # This silliness is needed so the debugger has access to the exception | |
309 | # instance (e in this case) |
|
312 | # instance (e in this case) | |
310 | try: |
|
313 | try: | |
311 | raise CompositeError(msg, elist) |
|
314 | raise CompositeError(msg, elist) | |
312 | except CompositeError as e: |
|
315 | except CompositeError as e: | |
313 | raise e |
|
316 | raise e | |
314 |
|
317 | |||
315 | def wrap_exception(engine_info={}): |
|
318 | def wrap_exception(engine_info={}): | |
316 | etype, evalue, tb = sys.exc_info() |
|
319 | etype, evalue, tb = sys.exc_info() | |
317 | stb = traceback.format_exception(etype, evalue, tb) |
|
320 | stb = traceback.format_exception(etype, evalue, tb) | |
318 | exc_content = { |
|
321 | exc_content = { | |
319 | 'status' : 'error', |
|
322 | 'status' : 'error', | |
320 | 'traceback' : stb, |
|
323 | 'traceback' : stb, | |
321 | 'ename' : unicode(etype.__name__), |
|
324 | 'ename' : unicode(etype.__name__), | |
322 | 'evalue' : unicode(evalue), |
|
325 | 'evalue' : unicode(evalue), | |
323 | 'engine_info' : engine_info |
|
326 | 'engine_info' : engine_info | |
324 | } |
|
327 | } | |
325 | return exc_content |
|
328 | return exc_content | |
326 |
|
329 | |||
327 | def unwrap_exception(content): |
|
330 | def unwrap_exception(content): | |
328 | err = RemoteError(content['ename'], content['evalue'], |
|
331 | err = RemoteError(content['ename'], content['evalue'], | |
329 | ''.join(content['traceback']), |
|
332 | ''.join(content['traceback']), | |
330 | content.get('engine_info', {})) |
|
333 | content.get('engine_info', {})) | |
331 | return err |
|
334 | return err | |
332 |
|
335 |
General Comments 0
You need to be logged in to leave comments.
Login now