Show More
@@ -1,3150 +1,3159 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 absolute_import |
|
17 | from __future__ import absolute_import | |
18 | from __future__ import print_function |
|
18 | from __future__ import print_function | |
19 |
|
19 | |||
20 | import __builtin__ as builtin_mod |
|
20 | import __builtin__ as builtin_mod | |
21 | import __future__ |
|
21 | import __future__ | |
22 | import abc |
|
22 | import abc | |
23 | import ast |
|
23 | import ast | |
24 | import atexit |
|
24 | import atexit | |
25 | import functools |
|
25 | import functools | |
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 | from io import open as io_open |
|
32 | from io import open as io_open | |
33 |
|
33 | |||
34 | from IPython.config.configurable import SingletonConfigurable |
|
34 | from IPython.config.configurable import SingletonConfigurable | |
35 | from IPython.core import debugger, oinspect |
|
35 | from IPython.core import debugger, oinspect | |
36 | from IPython.core import magic |
|
36 | from IPython.core import magic | |
37 | from IPython.core import page |
|
37 | from IPython.core import page | |
38 | from IPython.core import prefilter |
|
38 | from IPython.core import prefilter | |
39 | from IPython.core import shadowns |
|
39 | from IPython.core import shadowns | |
40 | from IPython.core import ultratb |
|
40 | from IPython.core import ultratb | |
41 | from IPython.core.alias import AliasManager, AliasError |
|
41 | from IPython.core.alias import AliasManager, AliasError | |
42 | from IPython.core.autocall import ExitAutocall |
|
42 | from IPython.core.autocall import ExitAutocall | |
43 | from IPython.core.builtin_trap import BuiltinTrap |
|
43 | from IPython.core.builtin_trap import BuiltinTrap | |
44 | from IPython.core.compilerop import CachingCompiler, check_linecache_ipython |
|
44 | from IPython.core.compilerop import CachingCompiler, check_linecache_ipython | |
45 | from IPython.core.display_trap import DisplayTrap |
|
45 | from IPython.core.display_trap import DisplayTrap | |
46 | from IPython.core.displayhook import DisplayHook |
|
46 | from IPython.core.displayhook import DisplayHook | |
47 | from IPython.core.displaypub import DisplayPublisher |
|
47 | from IPython.core.displaypub import DisplayPublisher | |
48 | from IPython.core.error import UsageError |
|
48 | from IPython.core.error import UsageError | |
49 | from IPython.core.extensions import ExtensionManager |
|
49 | from IPython.core.extensions import ExtensionManager | |
50 | from IPython.core.fakemodule import FakeModule, init_fakemod_dict |
|
|||
51 | from IPython.core.formatters import DisplayFormatter |
|
50 | from IPython.core.formatters import DisplayFormatter | |
52 | from IPython.core.history import HistoryManager |
|
51 | from IPython.core.history import HistoryManager | |
53 | from IPython.core.inputsplitter import IPythonInputSplitter, ESC_MAGIC, ESC_MAGIC2 |
|
52 | from IPython.core.inputsplitter import IPythonInputSplitter, ESC_MAGIC, ESC_MAGIC2 | |
54 | from IPython.core.logger import Logger |
|
53 | from IPython.core.logger import Logger | |
55 | from IPython.core.macro import Macro |
|
54 | from IPython.core.macro import Macro | |
56 | from IPython.core.payload import PayloadManager |
|
55 | from IPython.core.payload import PayloadManager | |
57 | from IPython.core.prefilter import PrefilterManager |
|
56 | from IPython.core.prefilter import PrefilterManager | |
58 | from IPython.core.profiledir import ProfileDir |
|
57 | from IPython.core.profiledir import ProfileDir | |
59 | from IPython.core.prompts import PromptManager |
|
58 | from IPython.core.prompts import PromptManager | |
60 | from IPython.lib.latextools import LaTeXTool |
|
59 | from IPython.lib.latextools import LaTeXTool | |
61 | from IPython.testing.skipdoctest import skip_doctest |
|
60 | from IPython.testing.skipdoctest import skip_doctest | |
62 | from IPython.utils import PyColorize |
|
61 | from IPython.utils import PyColorize | |
63 | from IPython.utils import io |
|
62 | from IPython.utils import io | |
64 | from IPython.utils import py3compat |
|
63 | from IPython.utils import py3compat | |
65 | from IPython.utils import openpy |
|
64 | from IPython.utils import openpy | |
66 | from IPython.utils.decorators import undoc |
|
65 | from IPython.utils.decorators import undoc | |
67 | from IPython.utils.io import ask_yes_no |
|
66 | from IPython.utils.io import ask_yes_no | |
68 | from IPython.utils.ipstruct import Struct |
|
67 | from IPython.utils.ipstruct import Struct | |
69 | from IPython.utils.path import get_home_dir, get_ipython_dir, get_py_filename, unquote_filename |
|
68 | from IPython.utils.path import get_home_dir, get_ipython_dir, get_py_filename, unquote_filename | |
70 | from IPython.utils.pickleshare import PickleShareDB |
|
69 | from IPython.utils.pickleshare import PickleShareDB | |
71 | from IPython.utils.process import system, getoutput |
|
70 | from IPython.utils.process import system, getoutput | |
72 | from IPython.utils.strdispatch import StrDispatch |
|
71 | from IPython.utils.strdispatch import StrDispatch | |
73 | from IPython.utils.syspathcontext import prepended_to_syspath |
|
72 | from IPython.utils.syspathcontext import prepended_to_syspath | |
74 | from IPython.utils.text import (format_screen, LSString, SList, |
|
73 | from IPython.utils.text import (format_screen, LSString, SList, | |
75 | DollarFormatter) |
|
74 | DollarFormatter) | |
76 | from IPython.utils.traitlets import (Integer, CBool, CaselessStrEnum, Enum, |
|
75 | from IPython.utils.traitlets import (Integer, CBool, CaselessStrEnum, Enum, | |
77 | List, Unicode, Instance, Type) |
|
76 | List, Unicode, Instance, Type) | |
78 | from IPython.utils.warn import warn, error |
|
77 | from IPython.utils.warn import warn, error | |
79 | import IPython.core.hooks |
|
78 | import IPython.core.hooks | |
80 |
|
79 | |||
81 | #----------------------------------------------------------------------------- |
|
80 | #----------------------------------------------------------------------------- | |
82 | # Globals |
|
81 | # Globals | |
83 | #----------------------------------------------------------------------------- |
|
82 | #----------------------------------------------------------------------------- | |
84 |
|
83 | |||
85 | # compiled regexps for autoindent management |
|
84 | # compiled regexps for autoindent management | |
86 | dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass') |
|
85 | dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass') | |
87 |
|
86 | |||
88 | #----------------------------------------------------------------------------- |
|
87 | #----------------------------------------------------------------------------- | |
89 | # Utilities |
|
88 | # Utilities | |
90 | #----------------------------------------------------------------------------- |
|
89 | #----------------------------------------------------------------------------- | |
91 |
|
90 | |||
92 | @undoc |
|
91 | @undoc | |
93 | def softspace(file, newvalue): |
|
92 | def softspace(file, newvalue): | |
94 | """Copied from code.py, to remove the dependency""" |
|
93 | """Copied from code.py, to remove the dependency""" | |
95 |
|
94 | |||
96 | oldvalue = 0 |
|
95 | oldvalue = 0 | |
97 | try: |
|
96 | try: | |
98 | oldvalue = file.softspace |
|
97 | oldvalue = file.softspace | |
99 | except AttributeError: |
|
98 | except AttributeError: | |
100 | pass |
|
99 | pass | |
101 | try: |
|
100 | try: | |
102 | file.softspace = newvalue |
|
101 | file.softspace = newvalue | |
103 | except (AttributeError, TypeError): |
|
102 | except (AttributeError, TypeError): | |
104 | # "attribute-less object" or "read-only attributes" |
|
103 | # "attribute-less object" or "read-only attributes" | |
105 | pass |
|
104 | pass | |
106 | return oldvalue |
|
105 | return oldvalue | |
107 |
|
106 | |||
108 | @undoc |
|
107 | @undoc | |
109 | def no_op(*a, **kw): pass |
|
108 | def no_op(*a, **kw): pass | |
110 |
|
109 | |||
111 | @undoc |
|
110 | @undoc | |
112 | class NoOpContext(object): |
|
111 | class NoOpContext(object): | |
113 | def __enter__(self): pass |
|
112 | def __enter__(self): pass | |
114 | def __exit__(self, type, value, traceback): pass |
|
113 | def __exit__(self, type, value, traceback): pass | |
115 | no_op_context = NoOpContext() |
|
114 | no_op_context = NoOpContext() | |
116 |
|
115 | |||
117 | class SpaceInInput(Exception): pass |
|
116 | class SpaceInInput(Exception): pass | |
118 |
|
117 | |||
119 | @undoc |
|
118 | @undoc | |
120 | class Bunch: pass |
|
119 | class Bunch: pass | |
121 |
|
120 | |||
122 |
|
121 | |||
123 | def get_default_colors(): |
|
122 | def get_default_colors(): | |
124 | if sys.platform=='darwin': |
|
123 | if sys.platform=='darwin': | |
125 | return "LightBG" |
|
124 | return "LightBG" | |
126 | elif os.name=='nt': |
|
125 | elif os.name=='nt': | |
127 | return 'Linux' |
|
126 | return 'Linux' | |
128 | else: |
|
127 | else: | |
129 | return 'Linux' |
|
128 | return 'Linux' | |
130 |
|
129 | |||
131 |
|
130 | |||
132 | class SeparateUnicode(Unicode): |
|
131 | class SeparateUnicode(Unicode): | |
133 | """A Unicode subclass to validate separate_in, separate_out, etc. |
|
132 | """A Unicode subclass to validate separate_in, separate_out, etc. | |
134 |
|
133 | |||
135 | This is a Unicode based trait that converts '0'->'' and '\\n'->'\n'. |
|
134 | This is a Unicode based trait that converts '0'->'' and '\\n'->'\n'. | |
136 | """ |
|
135 | """ | |
137 |
|
136 | |||
138 | def validate(self, obj, value): |
|
137 | def validate(self, obj, value): | |
139 | if value == '0': value = '' |
|
138 | if value == '0': value = '' | |
140 | value = value.replace('\\n','\n') |
|
139 | value = value.replace('\\n','\n') | |
141 | return super(SeparateUnicode, self).validate(obj, value) |
|
140 | return super(SeparateUnicode, self).validate(obj, value) | |
142 |
|
141 | |||
143 |
|
142 | |||
144 | class ReadlineNoRecord(object): |
|
143 | class ReadlineNoRecord(object): | |
145 | """Context manager to execute some code, then reload readline history |
|
144 | """Context manager to execute some code, then reload readline history | |
146 | so that interactive input to the code doesn't appear when pressing up.""" |
|
145 | so that interactive input to the code doesn't appear when pressing up.""" | |
147 | def __init__(self, shell): |
|
146 | def __init__(self, shell): | |
148 | self.shell = shell |
|
147 | self.shell = shell | |
149 | self._nested_level = 0 |
|
148 | self._nested_level = 0 | |
150 |
|
149 | |||
151 | def __enter__(self): |
|
150 | def __enter__(self): | |
152 | if self._nested_level == 0: |
|
151 | if self._nested_level == 0: | |
153 | try: |
|
152 | try: | |
154 | self.orig_length = self.current_length() |
|
153 | self.orig_length = self.current_length() | |
155 | self.readline_tail = self.get_readline_tail() |
|
154 | self.readline_tail = self.get_readline_tail() | |
156 | except (AttributeError, IndexError): # Can fail with pyreadline |
|
155 | except (AttributeError, IndexError): # Can fail with pyreadline | |
157 | self.orig_length, self.readline_tail = 999999, [] |
|
156 | self.orig_length, self.readline_tail = 999999, [] | |
158 | self._nested_level += 1 |
|
157 | self._nested_level += 1 | |
159 |
|
158 | |||
160 | def __exit__(self, type, value, traceback): |
|
159 | def __exit__(self, type, value, traceback): | |
161 | self._nested_level -= 1 |
|
160 | self._nested_level -= 1 | |
162 | if self._nested_level == 0: |
|
161 | if self._nested_level == 0: | |
163 | # Try clipping the end if it's got longer |
|
162 | # Try clipping the end if it's got longer | |
164 | try: |
|
163 | try: | |
165 | e = self.current_length() - self.orig_length |
|
164 | e = self.current_length() - self.orig_length | |
166 | if e > 0: |
|
165 | if e > 0: | |
167 | for _ in range(e): |
|
166 | for _ in range(e): | |
168 | self.shell.readline.remove_history_item(self.orig_length) |
|
167 | self.shell.readline.remove_history_item(self.orig_length) | |
169 |
|
168 | |||
170 | # If it still doesn't match, just reload readline history. |
|
169 | # If it still doesn't match, just reload readline history. | |
171 | if self.current_length() != self.orig_length \ |
|
170 | if self.current_length() != self.orig_length \ | |
172 | or self.get_readline_tail() != self.readline_tail: |
|
171 | or self.get_readline_tail() != self.readline_tail: | |
173 | self.shell.refill_readline_hist() |
|
172 | self.shell.refill_readline_hist() | |
174 | except (AttributeError, IndexError): |
|
173 | except (AttributeError, IndexError): | |
175 | pass |
|
174 | pass | |
176 | # Returning False will cause exceptions to propagate |
|
175 | # Returning False will cause exceptions to propagate | |
177 | return False |
|
176 | return False | |
178 |
|
177 | |||
179 | def current_length(self): |
|
178 | def current_length(self): | |
180 | return self.shell.readline.get_current_history_length() |
|
179 | return self.shell.readline.get_current_history_length() | |
181 |
|
180 | |||
182 | def get_readline_tail(self, n=10): |
|
181 | def get_readline_tail(self, n=10): | |
183 | """Get the last n items in readline history.""" |
|
182 | """Get the last n items in readline history.""" | |
184 | end = self.shell.readline.get_current_history_length() + 1 |
|
183 | end = self.shell.readline.get_current_history_length() + 1 | |
185 | start = max(end-n, 1) |
|
184 | start = max(end-n, 1) | |
186 | ghi = self.shell.readline.get_history_item |
|
185 | ghi = self.shell.readline.get_history_item | |
187 | return [ghi(x) for x in range(start, end)] |
|
186 | return [ghi(x) for x in range(start, end)] | |
188 |
|
187 | |||
189 |
|
188 | |||
190 | @undoc |
|
189 | @undoc | |
191 | class DummyMod(object): |
|
190 | class DummyMod(object): | |
192 | """A dummy module used for IPython's interactive module when |
|
191 | """A dummy module used for IPython's interactive module when | |
193 | a namespace must be assigned to the module's __dict__.""" |
|
192 | a namespace must be assigned to the module's __dict__.""" | |
194 | pass |
|
193 | pass | |
195 |
|
194 | |||
196 | #----------------------------------------------------------------------------- |
|
195 | #----------------------------------------------------------------------------- | |
197 | # Main IPython class |
|
196 | # Main IPython class | |
198 | #----------------------------------------------------------------------------- |
|
197 | #----------------------------------------------------------------------------- | |
199 |
|
198 | |||
200 | class InteractiveShell(SingletonConfigurable): |
|
199 | class InteractiveShell(SingletonConfigurable): | |
201 | """An enhanced, interactive shell for Python.""" |
|
200 | """An enhanced, interactive shell for Python.""" | |
202 |
|
201 | |||
203 | _instance = None |
|
202 | _instance = None | |
204 |
|
203 | |||
205 | ast_transformers = List([], config=True, help= |
|
204 | ast_transformers = List([], config=True, help= | |
206 | """ |
|
205 | """ | |
207 | A list of ast.NodeTransformer subclass instances, which will be applied |
|
206 | A list of ast.NodeTransformer subclass instances, which will be applied | |
208 | to user input before code is run. |
|
207 | to user input before code is run. | |
209 | """ |
|
208 | """ | |
210 | ) |
|
209 | ) | |
211 |
|
210 | |||
212 | autocall = Enum((0,1,2), default_value=0, config=True, help= |
|
211 | autocall = Enum((0,1,2), default_value=0, config=True, help= | |
213 | """ |
|
212 | """ | |
214 | Make IPython automatically call any callable object even if you didn't |
|
213 | Make IPython automatically call any callable object even if you didn't | |
215 | type explicit parentheses. For example, 'str 43' becomes 'str(43)' |
|
214 | type explicit parentheses. For example, 'str 43' becomes 'str(43)' | |
216 | automatically. The value can be '0' to disable the feature, '1' for |
|
215 | automatically. The value can be '0' to disable the feature, '1' for | |
217 | 'smart' autocall, where it is not applied if there are no more |
|
216 | 'smart' autocall, where it is not applied if there are no more | |
218 | arguments on the line, and '2' for 'full' autocall, where all callable |
|
217 | arguments on the line, and '2' for 'full' autocall, where all callable | |
219 | objects are automatically called (even if no arguments are present). |
|
218 | objects are automatically called (even if no arguments are present). | |
220 | """ |
|
219 | """ | |
221 | ) |
|
220 | ) | |
222 | # TODO: remove all autoindent logic and put into frontends. |
|
221 | # TODO: remove all autoindent logic and put into frontends. | |
223 | # We can't do this yet because even runlines uses the autoindent. |
|
222 | # We can't do this yet because even runlines uses the autoindent. | |
224 | autoindent = CBool(True, config=True, help= |
|
223 | autoindent = CBool(True, config=True, help= | |
225 | """ |
|
224 | """ | |
226 | Autoindent IPython code entered interactively. |
|
225 | Autoindent IPython code entered interactively. | |
227 | """ |
|
226 | """ | |
228 | ) |
|
227 | ) | |
229 | automagic = CBool(True, config=True, help= |
|
228 | automagic = CBool(True, config=True, help= | |
230 | """ |
|
229 | """ | |
231 | Enable magic commands to be called without the leading %. |
|
230 | Enable magic commands to be called without the leading %. | |
232 | """ |
|
231 | """ | |
233 | ) |
|
232 | ) | |
234 | cache_size = Integer(1000, config=True, help= |
|
233 | cache_size = Integer(1000, config=True, help= | |
235 | """ |
|
234 | """ | |
236 | Set the size of the output cache. The default is 1000, you can |
|
235 | Set the size of the output cache. The default is 1000, you can | |
237 | change it permanently in your config file. Setting it to 0 completely |
|
236 | change it permanently in your config file. Setting it to 0 completely | |
238 | disables the caching system, and the minimum value accepted is 20 (if |
|
237 | disables the caching system, and the minimum value accepted is 20 (if | |
239 | you provide a value less than 20, it is reset to 0 and a warning is |
|
238 | you provide a value less than 20, it is reset to 0 and a warning is | |
240 | issued). This limit is defined because otherwise you'll spend more |
|
239 | issued). This limit is defined because otherwise you'll spend more | |
241 | time re-flushing a too small cache than working |
|
240 | time re-flushing a too small cache than working | |
242 | """ |
|
241 | """ | |
243 | ) |
|
242 | ) | |
244 | color_info = CBool(True, config=True, help= |
|
243 | color_info = CBool(True, config=True, help= | |
245 | """ |
|
244 | """ | |
246 | Use colors for displaying information about objects. Because this |
|
245 | Use colors for displaying information about objects. Because this | |
247 | information is passed through a pager (like 'less'), and some pagers |
|
246 | information is passed through a pager (like 'less'), and some pagers | |
248 | get confused with color codes, this capability can be turned off. |
|
247 | get confused with color codes, this capability can be turned off. | |
249 | """ |
|
248 | """ | |
250 | ) |
|
249 | ) | |
251 | colors = CaselessStrEnum(('NoColor','LightBG','Linux'), |
|
250 | colors = CaselessStrEnum(('NoColor','LightBG','Linux'), | |
252 | default_value=get_default_colors(), config=True, |
|
251 | default_value=get_default_colors(), config=True, | |
253 | help="Set the color scheme (NoColor, Linux, or LightBG)." |
|
252 | help="Set the color scheme (NoColor, Linux, or LightBG)." | |
254 | ) |
|
253 | ) | |
255 | colors_force = CBool(False, help= |
|
254 | colors_force = CBool(False, help= | |
256 | """ |
|
255 | """ | |
257 | Force use of ANSI color codes, regardless of OS and readline |
|
256 | Force use of ANSI color codes, regardless of OS and readline | |
258 | availability. |
|
257 | availability. | |
259 | """ |
|
258 | """ | |
260 | # FIXME: This is essentially a hack to allow ZMQShell to show colors |
|
259 | # FIXME: This is essentially a hack to allow ZMQShell to show colors | |
261 | # without readline on Win32. When the ZMQ formatting system is |
|
260 | # without readline on Win32. When the ZMQ formatting system is | |
262 | # refactored, this should be removed. |
|
261 | # refactored, this should be removed. | |
263 | ) |
|
262 | ) | |
264 | debug = CBool(False, config=True) |
|
263 | debug = CBool(False, config=True) | |
265 | deep_reload = CBool(False, config=True, help= |
|
264 | deep_reload = CBool(False, config=True, help= | |
266 | """ |
|
265 | """ | |
267 | Enable deep (recursive) reloading by default. IPython can use the |
|
266 | Enable deep (recursive) reloading by default. IPython can use the | |
268 | deep_reload module which reloads changes in modules recursively (it |
|
267 | deep_reload module which reloads changes in modules recursively (it | |
269 | replaces the reload() function, so you don't need to change anything to |
|
268 | replaces the reload() function, so you don't need to change anything to | |
270 | use it). deep_reload() forces a full reload of modules whose code may |
|
269 | use it). deep_reload() forces a full reload of modules whose code may | |
271 | have changed, which the default reload() function does not. When |
|
270 | have changed, which the default reload() function does not. When | |
272 | deep_reload is off, IPython will use the normal reload(), but |
|
271 | deep_reload is off, IPython will use the normal reload(), but | |
273 | deep_reload will still be available as dreload(). |
|
272 | deep_reload will still be available as dreload(). | |
274 | """ |
|
273 | """ | |
275 | ) |
|
274 | ) | |
276 | disable_failing_post_execute = CBool(False, config=True, |
|
275 | disable_failing_post_execute = CBool(False, config=True, | |
277 | help="Don't call post-execute functions that have failed in the past." |
|
276 | help="Don't call post-execute functions that have failed in the past." | |
278 | ) |
|
277 | ) | |
279 | display_formatter = Instance(DisplayFormatter) |
|
278 | display_formatter = Instance(DisplayFormatter) | |
280 | displayhook_class = Type(DisplayHook) |
|
279 | displayhook_class = Type(DisplayHook) | |
281 | display_pub_class = Type(DisplayPublisher) |
|
280 | display_pub_class = Type(DisplayPublisher) | |
282 | data_pub_class = None |
|
281 | data_pub_class = None | |
283 |
|
282 | |||
284 | exit_now = CBool(False) |
|
283 | exit_now = CBool(False) | |
285 | exiter = Instance(ExitAutocall) |
|
284 | exiter = Instance(ExitAutocall) | |
286 | def _exiter_default(self): |
|
285 | def _exiter_default(self): | |
287 | return ExitAutocall(self) |
|
286 | return ExitAutocall(self) | |
288 | # Monotonically increasing execution counter |
|
287 | # Monotonically increasing execution counter | |
289 | execution_count = Integer(1) |
|
288 | execution_count = Integer(1) | |
290 | filename = Unicode("<ipython console>") |
|
289 | filename = Unicode("<ipython console>") | |
291 | ipython_dir= Unicode('', config=True) # Set to get_ipython_dir() in __init__ |
|
290 | ipython_dir= Unicode('', config=True) # Set to get_ipython_dir() in __init__ | |
292 |
|
291 | |||
293 | # Input splitter, to transform input line by line and detect when a block |
|
292 | # Input splitter, to transform input line by line and detect when a block | |
294 | # is ready to be executed. |
|
293 | # is ready to be executed. | |
295 | input_splitter = Instance('IPython.core.inputsplitter.IPythonInputSplitter', |
|
294 | input_splitter = Instance('IPython.core.inputsplitter.IPythonInputSplitter', | |
296 | (), {'line_input_checker': True}) |
|
295 | (), {'line_input_checker': True}) | |
297 |
|
296 | |||
298 | # This InputSplitter instance is used to transform completed cells before |
|
297 | # This InputSplitter instance is used to transform completed cells before | |
299 | # running them. It allows cell magics to contain blank lines. |
|
298 | # running them. It allows cell magics to contain blank lines. | |
300 | input_transformer_manager = Instance('IPython.core.inputsplitter.IPythonInputSplitter', |
|
299 | input_transformer_manager = Instance('IPython.core.inputsplitter.IPythonInputSplitter', | |
301 | (), {'line_input_checker': False}) |
|
300 | (), {'line_input_checker': False}) | |
302 |
|
301 | |||
303 | logstart = CBool(False, config=True, help= |
|
302 | logstart = CBool(False, config=True, help= | |
304 | """ |
|
303 | """ | |
305 | Start logging to the default log file. |
|
304 | Start logging to the default log file. | |
306 | """ |
|
305 | """ | |
307 | ) |
|
306 | ) | |
308 | logfile = Unicode('', config=True, help= |
|
307 | logfile = Unicode('', config=True, help= | |
309 | """ |
|
308 | """ | |
310 | The name of the logfile to use. |
|
309 | The name of the logfile to use. | |
311 | """ |
|
310 | """ | |
312 | ) |
|
311 | ) | |
313 | logappend = Unicode('', config=True, help= |
|
312 | logappend = Unicode('', config=True, help= | |
314 | """ |
|
313 | """ | |
315 | Start logging to the given file in append mode. |
|
314 | Start logging to the given file in append mode. | |
316 | """ |
|
315 | """ | |
317 | ) |
|
316 | ) | |
318 | object_info_string_level = Enum((0,1,2), default_value=0, |
|
317 | object_info_string_level = Enum((0,1,2), default_value=0, | |
319 | config=True) |
|
318 | config=True) | |
320 | pdb = CBool(False, config=True, help= |
|
319 | pdb = CBool(False, config=True, help= | |
321 | """ |
|
320 | """ | |
322 | Automatically call the pdb debugger after every exception. |
|
321 | Automatically call the pdb debugger after every exception. | |
323 | """ |
|
322 | """ | |
324 | ) |
|
323 | ) | |
325 | multiline_history = CBool(sys.platform != 'win32', config=True, |
|
324 | multiline_history = CBool(sys.platform != 'win32', config=True, | |
326 | help="Save multi-line entries as one entry in readline history" |
|
325 | help="Save multi-line entries as one entry in readline history" | |
327 | ) |
|
326 | ) | |
328 |
|
327 | |||
329 | # deprecated prompt traits: |
|
328 | # deprecated prompt traits: | |
330 |
|
329 | |||
331 | prompt_in1 = Unicode('In [\\#]: ', config=True, |
|
330 | prompt_in1 = Unicode('In [\\#]: ', config=True, | |
332 | help="Deprecated, use PromptManager.in_template") |
|
331 | help="Deprecated, use PromptManager.in_template") | |
333 | prompt_in2 = Unicode(' .\\D.: ', config=True, |
|
332 | prompt_in2 = Unicode(' .\\D.: ', config=True, | |
334 | help="Deprecated, use PromptManager.in2_template") |
|
333 | help="Deprecated, use PromptManager.in2_template") | |
335 | prompt_out = Unicode('Out[\\#]: ', config=True, |
|
334 | prompt_out = Unicode('Out[\\#]: ', config=True, | |
336 | help="Deprecated, use PromptManager.out_template") |
|
335 | help="Deprecated, use PromptManager.out_template") | |
337 | prompts_pad_left = CBool(True, config=True, |
|
336 | prompts_pad_left = CBool(True, config=True, | |
338 | help="Deprecated, use PromptManager.justify") |
|
337 | help="Deprecated, use PromptManager.justify") | |
339 |
|
338 | |||
340 | def _prompt_trait_changed(self, name, old, new): |
|
339 | def _prompt_trait_changed(self, name, old, new): | |
341 | table = { |
|
340 | table = { | |
342 | 'prompt_in1' : 'in_template', |
|
341 | 'prompt_in1' : 'in_template', | |
343 | 'prompt_in2' : 'in2_template', |
|
342 | 'prompt_in2' : 'in2_template', | |
344 | 'prompt_out' : 'out_template', |
|
343 | 'prompt_out' : 'out_template', | |
345 | 'prompts_pad_left' : 'justify', |
|
344 | 'prompts_pad_left' : 'justify', | |
346 | } |
|
345 | } | |
347 | warn("InteractiveShell.{name} is deprecated, use PromptManager.{newname}".format( |
|
346 | warn("InteractiveShell.{name} is deprecated, use PromptManager.{newname}".format( | |
348 | name=name, newname=table[name]) |
|
347 | name=name, newname=table[name]) | |
349 | ) |
|
348 | ) | |
350 | # protect against weird cases where self.config may not exist: |
|
349 | # protect against weird cases where self.config may not exist: | |
351 | if self.config is not None: |
|
350 | if self.config is not None: | |
352 | # propagate to corresponding PromptManager trait |
|
351 | # propagate to corresponding PromptManager trait | |
353 | setattr(self.config.PromptManager, table[name], new) |
|
352 | setattr(self.config.PromptManager, table[name], new) | |
354 |
|
353 | |||
355 | _prompt_in1_changed = _prompt_trait_changed |
|
354 | _prompt_in1_changed = _prompt_trait_changed | |
356 | _prompt_in2_changed = _prompt_trait_changed |
|
355 | _prompt_in2_changed = _prompt_trait_changed | |
357 | _prompt_out_changed = _prompt_trait_changed |
|
356 | _prompt_out_changed = _prompt_trait_changed | |
358 | _prompt_pad_left_changed = _prompt_trait_changed |
|
357 | _prompt_pad_left_changed = _prompt_trait_changed | |
359 |
|
358 | |||
360 | show_rewritten_input = CBool(True, config=True, |
|
359 | show_rewritten_input = CBool(True, config=True, | |
361 | help="Show rewritten input, e.g. for autocall." |
|
360 | help="Show rewritten input, e.g. for autocall." | |
362 | ) |
|
361 | ) | |
363 |
|
362 | |||
364 | quiet = CBool(False, config=True) |
|
363 | quiet = CBool(False, config=True) | |
365 |
|
364 | |||
366 | history_length = Integer(10000, config=True) |
|
365 | history_length = Integer(10000, config=True) | |
367 |
|
366 | |||
368 | # The readline stuff will eventually be moved to the terminal subclass |
|
367 | # 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. |
|
368 | # but for now, we can't do that as readline is welded in everywhere. | |
370 | readline_use = CBool(True, config=True) |
|
369 | readline_use = CBool(True, config=True) | |
371 | readline_remove_delims = Unicode('-/~', config=True) |
|
370 | readline_remove_delims = Unicode('-/~', config=True) | |
372 | readline_delims = Unicode() # set by init_readline() |
|
371 | readline_delims = Unicode() # set by init_readline() | |
373 | # don't use \M- bindings by default, because they |
|
372 | # don't use \M- bindings by default, because they | |
374 | # conflict with 8-bit encodings. See gh-58,gh-88 |
|
373 | # conflict with 8-bit encodings. See gh-58,gh-88 | |
375 | readline_parse_and_bind = List([ |
|
374 | readline_parse_and_bind = List([ | |
376 | 'tab: complete', |
|
375 | 'tab: complete', | |
377 | '"\C-l": clear-screen', |
|
376 | '"\C-l": clear-screen', | |
378 | 'set show-all-if-ambiguous on', |
|
377 | 'set show-all-if-ambiguous on', | |
379 | '"\C-o": tab-insert', |
|
378 | '"\C-o": tab-insert', | |
380 | '"\C-r": reverse-search-history', |
|
379 | '"\C-r": reverse-search-history', | |
381 | '"\C-s": forward-search-history', |
|
380 | '"\C-s": forward-search-history', | |
382 | '"\C-p": history-search-backward', |
|
381 | '"\C-p": history-search-backward', | |
383 | '"\C-n": history-search-forward', |
|
382 | '"\C-n": history-search-forward', | |
384 | '"\e[A": history-search-backward', |
|
383 | '"\e[A": history-search-backward', | |
385 | '"\e[B": history-search-forward', |
|
384 | '"\e[B": history-search-forward', | |
386 | '"\C-k": kill-line', |
|
385 | '"\C-k": kill-line', | |
387 | '"\C-u": unix-line-discard', |
|
386 | '"\C-u": unix-line-discard', | |
388 | ], allow_none=False, config=True) |
|
387 | ], allow_none=False, config=True) | |
389 |
|
388 | |||
390 | ast_node_interactivity = Enum(['all', 'last', 'last_expr', 'none'], |
|
389 | ast_node_interactivity = Enum(['all', 'last', 'last_expr', 'none'], | |
391 | default_value='last_expr', config=True, |
|
390 | default_value='last_expr', config=True, | |
392 | help=""" |
|
391 | help=""" | |
393 | 'all', 'last', 'last_expr' or 'none', specifying which nodes should be |
|
392 | 'all', 'last', 'last_expr' or 'none', specifying which nodes should be | |
394 | run interactively (displaying output from expressions).""") |
|
393 | run interactively (displaying output from expressions).""") | |
395 |
|
394 | |||
396 | # TODO: this part of prompt management should be moved to the frontends. |
|
395 | # TODO: this part of prompt management should be moved to the frontends. | |
397 | # Use custom TraitTypes that convert '0'->'' and '\\n'->'\n' |
|
396 | # Use custom TraitTypes that convert '0'->'' and '\\n'->'\n' | |
398 | separate_in = SeparateUnicode('\n', config=True) |
|
397 | separate_in = SeparateUnicode('\n', config=True) | |
399 | separate_out = SeparateUnicode('', config=True) |
|
398 | separate_out = SeparateUnicode('', config=True) | |
400 | separate_out2 = SeparateUnicode('', config=True) |
|
399 | separate_out2 = SeparateUnicode('', config=True) | |
401 | wildcards_case_sensitive = CBool(True, config=True) |
|
400 | wildcards_case_sensitive = CBool(True, config=True) | |
402 | xmode = CaselessStrEnum(('Context','Plain', 'Verbose'), |
|
401 | xmode = CaselessStrEnum(('Context','Plain', 'Verbose'), | |
403 | default_value='Context', config=True) |
|
402 | default_value='Context', config=True) | |
404 |
|
403 | |||
405 | # Subcomponents of InteractiveShell |
|
404 | # Subcomponents of InteractiveShell | |
406 | alias_manager = Instance('IPython.core.alias.AliasManager') |
|
405 | alias_manager = Instance('IPython.core.alias.AliasManager') | |
407 | prefilter_manager = Instance('IPython.core.prefilter.PrefilterManager') |
|
406 | prefilter_manager = Instance('IPython.core.prefilter.PrefilterManager') | |
408 | builtin_trap = Instance('IPython.core.builtin_trap.BuiltinTrap') |
|
407 | builtin_trap = Instance('IPython.core.builtin_trap.BuiltinTrap') | |
409 | display_trap = Instance('IPython.core.display_trap.DisplayTrap') |
|
408 | display_trap = Instance('IPython.core.display_trap.DisplayTrap') | |
410 | extension_manager = Instance('IPython.core.extensions.ExtensionManager') |
|
409 | extension_manager = Instance('IPython.core.extensions.ExtensionManager') | |
411 | payload_manager = Instance('IPython.core.payload.PayloadManager') |
|
410 | payload_manager = Instance('IPython.core.payload.PayloadManager') | |
412 | history_manager = Instance('IPython.core.history.HistoryManager') |
|
411 | history_manager = Instance('IPython.core.history.HistoryManager') | |
413 | magics_manager = Instance('IPython.core.magic.MagicsManager') |
|
412 | magics_manager = Instance('IPython.core.magic.MagicsManager') | |
414 |
|
413 | |||
415 | profile_dir = Instance('IPython.core.application.ProfileDir') |
|
414 | profile_dir = Instance('IPython.core.application.ProfileDir') | |
416 | @property |
|
415 | @property | |
417 | def profile(self): |
|
416 | def profile(self): | |
418 | if self.profile_dir is not None: |
|
417 | if self.profile_dir is not None: | |
419 | name = os.path.basename(self.profile_dir.location) |
|
418 | name = os.path.basename(self.profile_dir.location) | |
420 | return name.replace('profile_','') |
|
419 | return name.replace('profile_','') | |
421 |
|
420 | |||
422 |
|
421 | |||
423 | # Private interface |
|
422 | # Private interface | |
424 | _post_execute = Instance(dict) |
|
423 | _post_execute = Instance(dict) | |
425 |
|
424 | |||
426 | # Tracks any GUI loop loaded for pylab |
|
425 | # Tracks any GUI loop loaded for pylab | |
427 | pylab_gui_select = None |
|
426 | pylab_gui_select = None | |
428 |
|
427 | |||
429 | def __init__(self, ipython_dir=None, profile_dir=None, |
|
428 | def __init__(self, ipython_dir=None, profile_dir=None, | |
430 | user_module=None, user_ns=None, |
|
429 | user_module=None, user_ns=None, | |
431 | custom_exceptions=((), None), **kwargs): |
|
430 | custom_exceptions=((), None), **kwargs): | |
432 |
|
431 | |||
433 | # This is where traits with a config_key argument are updated |
|
432 | # This is where traits with a config_key argument are updated | |
434 | # from the values on config. |
|
433 | # from the values on config. | |
435 | super(InteractiveShell, self).__init__(**kwargs) |
|
434 | super(InteractiveShell, self).__init__(**kwargs) | |
436 | self.configurables = [self] |
|
435 | self.configurables = [self] | |
437 |
|
436 | |||
438 | # These are relatively independent and stateless |
|
437 | # These are relatively independent and stateless | |
439 | self.init_ipython_dir(ipython_dir) |
|
438 | self.init_ipython_dir(ipython_dir) | |
440 | self.init_profile_dir(profile_dir) |
|
439 | self.init_profile_dir(profile_dir) | |
441 | self.init_instance_attrs() |
|
440 | self.init_instance_attrs() | |
442 | self.init_environment() |
|
441 | self.init_environment() | |
443 |
|
442 | |||
444 | # Check if we're in a virtualenv, and set up sys.path. |
|
443 | # Check if we're in a virtualenv, and set up sys.path. | |
445 | self.init_virtualenv() |
|
444 | self.init_virtualenv() | |
446 |
|
445 | |||
447 | # Create namespaces (user_ns, user_global_ns, etc.) |
|
446 | # Create namespaces (user_ns, user_global_ns, etc.) | |
448 | self.init_create_namespaces(user_module, user_ns) |
|
447 | self.init_create_namespaces(user_module, user_ns) | |
449 | # This has to be done after init_create_namespaces because it uses |
|
448 | # This has to be done after init_create_namespaces because it uses | |
450 | # something in self.user_ns, but before init_sys_modules, which |
|
449 | # something in self.user_ns, but before init_sys_modules, which | |
451 | # is the first thing to modify sys. |
|
450 | # is the first thing to modify sys. | |
452 | # TODO: When we override sys.stdout and sys.stderr before this class |
|
451 | # 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 |
|
452 | # is created, we are saving the overridden ones here. Not sure if this | |
454 | # is what we want to do. |
|
453 | # is what we want to do. | |
455 | self.save_sys_module_state() |
|
454 | self.save_sys_module_state() | |
456 | self.init_sys_modules() |
|
455 | self.init_sys_modules() | |
457 |
|
456 | |||
458 | # While we're trying to have each part of the code directly access what |
|
457 | # 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 |
|
458 | # it needs without keeping redundant references to objects, we have too | |
460 | # much legacy code that expects ip.db to exist. |
|
459 | # much legacy code that expects ip.db to exist. | |
461 | self.db = PickleShareDB(os.path.join(self.profile_dir.location, 'db')) |
|
460 | self.db = PickleShareDB(os.path.join(self.profile_dir.location, 'db')) | |
462 |
|
461 | |||
463 | self.init_history() |
|
462 | self.init_history() | |
464 | self.init_encoding() |
|
463 | self.init_encoding() | |
465 | self.init_prefilter() |
|
464 | self.init_prefilter() | |
466 |
|
465 | |||
467 | self.init_syntax_highlighting() |
|
466 | self.init_syntax_highlighting() | |
468 | self.init_hooks() |
|
467 | self.init_hooks() | |
469 | self.init_pushd_popd_magic() |
|
468 | self.init_pushd_popd_magic() | |
470 | # self.init_traceback_handlers use to be here, but we moved it below |
|
469 | # 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. |
|
470 | # because it and init_io have to come after init_readline. | |
472 | self.init_user_ns() |
|
471 | self.init_user_ns() | |
473 | self.init_logger() |
|
472 | self.init_logger() | |
474 | self.init_alias() |
|
473 | self.init_alias() | |
475 | self.init_builtins() |
|
474 | self.init_builtins() | |
476 |
|
475 | |||
477 | # The following was in post_config_initialization |
|
476 | # The following was in post_config_initialization | |
478 | self.init_inspector() |
|
477 | self.init_inspector() | |
479 | # init_readline() must come before init_io(), because init_io uses |
|
478 | # init_readline() must come before init_io(), because init_io uses | |
480 | # readline related things. |
|
479 | # readline related things. | |
481 | self.init_readline() |
|
480 | self.init_readline() | |
482 | # We save this here in case user code replaces raw_input, but it needs |
|
481 | # 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 |
|
482 | # to be after init_readline(), because PyPy's readline works by replacing | |
484 | # raw_input. |
|
483 | # raw_input. | |
485 | if py3compat.PY3: |
|
484 | if py3compat.PY3: | |
486 | self.raw_input_original = input |
|
485 | self.raw_input_original = input | |
487 | else: |
|
486 | else: | |
488 | self.raw_input_original = raw_input |
|
487 | self.raw_input_original = raw_input | |
489 | # init_completer must come after init_readline, because it needs to |
|
488 | # init_completer must come after init_readline, because it needs to | |
490 | # know whether readline is present or not system-wide to configure the |
|
489 | # know whether readline is present or not system-wide to configure the | |
491 | # completers, since the completion machinery can now operate |
|
490 | # completers, since the completion machinery can now operate | |
492 | # independently of readline (e.g. over the network) |
|
491 | # independently of readline (e.g. over the network) | |
493 | self.init_completer() |
|
492 | self.init_completer() | |
494 | # TODO: init_io() needs to happen before init_traceback handlers |
|
493 | # TODO: init_io() needs to happen before init_traceback handlers | |
495 | # because the traceback handlers hardcode the stdout/stderr streams. |
|
494 | # because the traceback handlers hardcode the stdout/stderr streams. | |
496 | # This logic in in debugger.Pdb and should eventually be changed. |
|
495 | # This logic in in debugger.Pdb and should eventually be changed. | |
497 | self.init_io() |
|
496 | self.init_io() | |
498 | self.init_traceback_handlers(custom_exceptions) |
|
497 | self.init_traceback_handlers(custom_exceptions) | |
499 | self.init_prompts() |
|
498 | self.init_prompts() | |
500 | self.init_display_formatter() |
|
499 | self.init_display_formatter() | |
501 | self.init_display_pub() |
|
500 | self.init_display_pub() | |
502 | self.init_data_pub() |
|
501 | self.init_data_pub() | |
503 | self.init_displayhook() |
|
502 | self.init_displayhook() | |
504 | self.init_latextool() |
|
503 | self.init_latextool() | |
505 | self.init_magics() |
|
504 | self.init_magics() | |
506 | self.init_logstart() |
|
505 | self.init_logstart() | |
507 | self.init_pdb() |
|
506 | self.init_pdb() | |
508 | self.init_extension_manager() |
|
507 | self.init_extension_manager() | |
509 | self.init_payload() |
|
508 | self.init_payload() | |
510 | self.hooks.late_startup_hook() |
|
509 | self.hooks.late_startup_hook() | |
511 | atexit.register(self.atexit_operations) |
|
510 | atexit.register(self.atexit_operations) | |
512 |
|
511 | |||
513 | def get_ipython(self): |
|
512 | def get_ipython(self): | |
514 | """Return the currently running IPython instance.""" |
|
513 | """Return the currently running IPython instance.""" | |
515 | return self |
|
514 | return self | |
516 |
|
515 | |||
517 | #------------------------------------------------------------------------- |
|
516 | #------------------------------------------------------------------------- | |
518 | # Trait changed handlers |
|
517 | # Trait changed handlers | |
519 | #------------------------------------------------------------------------- |
|
518 | #------------------------------------------------------------------------- | |
520 |
|
519 | |||
521 | def _ipython_dir_changed(self, name, new): |
|
520 | def _ipython_dir_changed(self, name, new): | |
522 | if not os.path.isdir(new): |
|
521 | if not os.path.isdir(new): | |
523 | os.makedirs(new, mode = 0o777) |
|
522 | os.makedirs(new, mode = 0o777) | |
524 |
|
523 | |||
525 | def set_autoindent(self,value=None): |
|
524 | def set_autoindent(self,value=None): | |
526 | """Set the autoindent flag, checking for readline support. |
|
525 | """Set the autoindent flag, checking for readline support. | |
527 |
|
526 | |||
528 | If called with no arguments, it acts as a toggle.""" |
|
527 | If called with no arguments, it acts as a toggle.""" | |
529 |
|
528 | |||
530 | if value != 0 and not self.has_readline: |
|
529 | if value != 0 and not self.has_readline: | |
531 | if os.name == 'posix': |
|
530 | if os.name == 'posix': | |
532 | warn("The auto-indent feature requires the readline library") |
|
531 | warn("The auto-indent feature requires the readline library") | |
533 | self.autoindent = 0 |
|
532 | self.autoindent = 0 | |
534 | return |
|
533 | return | |
535 | if value is None: |
|
534 | if value is None: | |
536 | self.autoindent = not self.autoindent |
|
535 | self.autoindent = not self.autoindent | |
537 | else: |
|
536 | else: | |
538 | self.autoindent = value |
|
537 | self.autoindent = value | |
539 |
|
538 | |||
540 | #------------------------------------------------------------------------- |
|
539 | #------------------------------------------------------------------------- | |
541 | # init_* methods called by __init__ |
|
540 | # init_* methods called by __init__ | |
542 | #------------------------------------------------------------------------- |
|
541 | #------------------------------------------------------------------------- | |
543 |
|
542 | |||
544 | def init_ipython_dir(self, ipython_dir): |
|
543 | def init_ipython_dir(self, ipython_dir): | |
545 | if ipython_dir is not None: |
|
544 | if ipython_dir is not None: | |
546 | self.ipython_dir = ipython_dir |
|
545 | self.ipython_dir = ipython_dir | |
547 | return |
|
546 | return | |
548 |
|
547 | |||
549 | self.ipython_dir = get_ipython_dir() |
|
548 | self.ipython_dir = get_ipython_dir() | |
550 |
|
549 | |||
551 | def init_profile_dir(self, profile_dir): |
|
550 | def init_profile_dir(self, profile_dir): | |
552 | if profile_dir is not None: |
|
551 | if profile_dir is not None: | |
553 | self.profile_dir = profile_dir |
|
552 | self.profile_dir = profile_dir | |
554 | return |
|
553 | return | |
555 | self.profile_dir =\ |
|
554 | self.profile_dir =\ | |
556 | ProfileDir.create_profile_dir_by_name(self.ipython_dir, 'default') |
|
555 | ProfileDir.create_profile_dir_by_name(self.ipython_dir, 'default') | |
557 |
|
556 | |||
558 | def init_instance_attrs(self): |
|
557 | def init_instance_attrs(self): | |
559 | self.more = False |
|
558 | self.more = False | |
560 |
|
559 | |||
561 | # command compiler |
|
560 | # command compiler | |
562 | self.compile = CachingCompiler() |
|
561 | self.compile = CachingCompiler() | |
563 |
|
562 | |||
564 | # Make an empty namespace, which extension writers can rely on both |
|
563 | # Make an empty namespace, which extension writers can rely on both | |
565 | # existing and NEVER being used by ipython itself. This gives them a |
|
564 | # existing and NEVER being used by ipython itself. This gives them a | |
566 | # convenient location for storing additional information and state |
|
565 | # convenient location for storing additional information and state | |
567 | # their extensions may require, without fear of collisions with other |
|
566 | # their extensions may require, without fear of collisions with other | |
568 | # ipython names that may develop later. |
|
567 | # ipython names that may develop later. | |
569 | self.meta = Struct() |
|
568 | self.meta = Struct() | |
570 |
|
569 | |||
571 | # Temporary files used for various purposes. Deleted at exit. |
|
570 | # Temporary files used for various purposes. Deleted at exit. | |
572 | self.tempfiles = [] |
|
571 | self.tempfiles = [] | |
573 |
|
572 | |||
574 | # Keep track of readline usage (later set by init_readline) |
|
573 | # Keep track of readline usage (later set by init_readline) | |
575 | self.has_readline = False |
|
574 | self.has_readline = False | |
576 |
|
575 | |||
577 | # keep track of where we started running (mainly for crash post-mortem) |
|
576 | # keep track of where we started running (mainly for crash post-mortem) | |
578 | # This is not being used anywhere currently. |
|
577 | # This is not being used anywhere currently. | |
579 | self.starting_dir = os.getcwdu() |
|
578 | self.starting_dir = os.getcwdu() | |
580 |
|
579 | |||
581 | # Indentation management |
|
580 | # Indentation management | |
582 | self.indent_current_nsp = 0 |
|
581 | self.indent_current_nsp = 0 | |
583 |
|
582 | |||
584 | # Dict to track post-execution functions that have been registered |
|
583 | # Dict to track post-execution functions that have been registered | |
585 | self._post_execute = {} |
|
584 | self._post_execute = {} | |
586 |
|
585 | |||
587 | def init_environment(self): |
|
586 | def init_environment(self): | |
588 | """Any changes we need to make to the user's environment.""" |
|
587 | """Any changes we need to make to the user's environment.""" | |
589 | pass |
|
588 | pass | |
590 |
|
589 | |||
591 | def init_encoding(self): |
|
590 | def init_encoding(self): | |
592 | # Get system encoding at startup time. Certain terminals (like Emacs |
|
591 | # Get system encoding at startup time. Certain terminals (like Emacs | |
593 | # under Win32 have it set to None, and we need to have a known valid |
|
592 | # under Win32 have it set to None, and we need to have a known valid | |
594 | # encoding to use in the raw_input() method |
|
593 | # encoding to use in the raw_input() method | |
595 | try: |
|
594 | try: | |
596 | self.stdin_encoding = sys.stdin.encoding or 'ascii' |
|
595 | self.stdin_encoding = sys.stdin.encoding or 'ascii' | |
597 | except AttributeError: |
|
596 | except AttributeError: | |
598 | self.stdin_encoding = 'ascii' |
|
597 | self.stdin_encoding = 'ascii' | |
599 |
|
598 | |||
600 | def init_syntax_highlighting(self): |
|
599 | def init_syntax_highlighting(self): | |
601 | # Python source parser/formatter for syntax highlighting |
|
600 | # Python source parser/formatter for syntax highlighting | |
602 | pyformat = PyColorize.Parser().format |
|
601 | pyformat = PyColorize.Parser().format | |
603 | self.pycolorize = lambda src: pyformat(src,'str',self.colors) |
|
602 | self.pycolorize = lambda src: pyformat(src,'str',self.colors) | |
604 |
|
603 | |||
605 | def init_pushd_popd_magic(self): |
|
604 | def init_pushd_popd_magic(self): | |
606 | # for pushd/popd management |
|
605 | # for pushd/popd management | |
607 | self.home_dir = get_home_dir() |
|
606 | self.home_dir = get_home_dir() | |
608 |
|
607 | |||
609 | self.dir_stack = [] |
|
608 | self.dir_stack = [] | |
610 |
|
609 | |||
611 | def init_logger(self): |
|
610 | def init_logger(self): | |
612 | self.logger = Logger(self.home_dir, logfname='ipython_log.py', |
|
611 | self.logger = Logger(self.home_dir, logfname='ipython_log.py', | |
613 | logmode='rotate') |
|
612 | logmode='rotate') | |
614 |
|
613 | |||
615 | def init_logstart(self): |
|
614 | def init_logstart(self): | |
616 | """Initialize logging in case it was requested at the command line. |
|
615 | """Initialize logging in case it was requested at the command line. | |
617 | """ |
|
616 | """ | |
618 | if self.logappend: |
|
617 | if self.logappend: | |
619 | self.magic('logstart %s append' % self.logappend) |
|
618 | self.magic('logstart %s append' % self.logappend) | |
620 | elif self.logfile: |
|
619 | elif self.logfile: | |
621 | self.magic('logstart %s' % self.logfile) |
|
620 | self.magic('logstart %s' % self.logfile) | |
622 | elif self.logstart: |
|
621 | elif self.logstart: | |
623 | self.magic('logstart') |
|
622 | self.magic('logstart') | |
624 |
|
623 | |||
625 | def init_builtins(self): |
|
624 | def init_builtins(self): | |
626 | # A single, static flag that we set to True. Its presence indicates |
|
625 | # A single, static flag that we set to True. Its presence indicates | |
627 | # that an IPython shell has been created, and we make no attempts at |
|
626 | # that an IPython shell has been created, and we make no attempts at | |
628 | # removing on exit or representing the existence of more than one |
|
627 | # removing on exit or representing the existence of more than one | |
629 | # IPython at a time. |
|
628 | # IPython at a time. | |
630 | builtin_mod.__dict__['__IPYTHON__'] = True |
|
629 | builtin_mod.__dict__['__IPYTHON__'] = True | |
631 |
|
630 | |||
632 | # In 0.11 we introduced '__IPYTHON__active' as an integer we'd try to |
|
631 | # In 0.11 we introduced '__IPYTHON__active' as an integer we'd try to | |
633 | # manage on enter/exit, but with all our shells it's virtually |
|
632 | # manage on enter/exit, but with all our shells it's virtually | |
634 | # impossible to get all the cases right. We're leaving the name in for |
|
633 | # impossible to get all the cases right. We're leaving the name in for | |
635 | # those who adapted their codes to check for this flag, but will |
|
634 | # those who adapted their codes to check for this flag, but will | |
636 | # eventually remove it after a few more releases. |
|
635 | # eventually remove it after a few more releases. | |
637 | builtin_mod.__dict__['__IPYTHON__active'] = \ |
|
636 | builtin_mod.__dict__['__IPYTHON__active'] = \ | |
638 | 'Deprecated, check for __IPYTHON__' |
|
637 | 'Deprecated, check for __IPYTHON__' | |
639 |
|
638 | |||
640 | self.builtin_trap = BuiltinTrap(shell=self) |
|
639 | self.builtin_trap = BuiltinTrap(shell=self) | |
641 |
|
640 | |||
642 | def init_inspector(self): |
|
641 | def init_inspector(self): | |
643 | # Object inspector |
|
642 | # Object inspector | |
644 | self.inspector = oinspect.Inspector(oinspect.InspectColors, |
|
643 | self.inspector = oinspect.Inspector(oinspect.InspectColors, | |
645 | PyColorize.ANSICodeColors, |
|
644 | PyColorize.ANSICodeColors, | |
646 | 'NoColor', |
|
645 | 'NoColor', | |
647 | self.object_info_string_level) |
|
646 | self.object_info_string_level) | |
648 |
|
647 | |||
649 | def init_io(self): |
|
648 | def init_io(self): | |
650 | # This will just use sys.stdout and sys.stderr. If you want to |
|
649 | # This will just use sys.stdout and sys.stderr. If you want to | |
651 | # override sys.stdout and sys.stderr themselves, you need to do that |
|
650 | # override sys.stdout and sys.stderr themselves, you need to do that | |
652 | # *before* instantiating this class, because io holds onto |
|
651 | # *before* instantiating this class, because io holds onto | |
653 | # references to the underlying streams. |
|
652 | # references to the underlying streams. | |
654 | if (sys.platform == 'win32' or sys.platform == 'cli') and self.has_readline: |
|
653 | if (sys.platform == 'win32' or sys.platform == 'cli') and self.has_readline: | |
655 | io.stdout = io.stderr = io.IOStream(self.readline._outputfile) |
|
654 | io.stdout = io.stderr = io.IOStream(self.readline._outputfile) | |
656 | else: |
|
655 | else: | |
657 | io.stdout = io.IOStream(sys.stdout) |
|
656 | io.stdout = io.IOStream(sys.stdout) | |
658 | io.stderr = io.IOStream(sys.stderr) |
|
657 | io.stderr = io.IOStream(sys.stderr) | |
659 |
|
658 | |||
660 | def init_prompts(self): |
|
659 | def init_prompts(self): | |
661 | self.prompt_manager = PromptManager(shell=self, parent=self) |
|
660 | self.prompt_manager = PromptManager(shell=self, parent=self) | |
662 | self.configurables.append(self.prompt_manager) |
|
661 | self.configurables.append(self.prompt_manager) | |
663 | # Set system prompts, so that scripts can decide if they are running |
|
662 | # Set system prompts, so that scripts can decide if they are running | |
664 | # interactively. |
|
663 | # interactively. | |
665 | sys.ps1 = 'In : ' |
|
664 | sys.ps1 = 'In : ' | |
666 | sys.ps2 = '...: ' |
|
665 | sys.ps2 = '...: ' | |
667 | sys.ps3 = 'Out: ' |
|
666 | sys.ps3 = 'Out: ' | |
668 |
|
667 | |||
669 | def init_display_formatter(self): |
|
668 | def init_display_formatter(self): | |
670 | self.display_formatter = DisplayFormatter(parent=self) |
|
669 | self.display_formatter = DisplayFormatter(parent=self) | |
671 | self.configurables.append(self.display_formatter) |
|
670 | self.configurables.append(self.display_formatter) | |
672 |
|
671 | |||
673 | def init_display_pub(self): |
|
672 | def init_display_pub(self): | |
674 | self.display_pub = self.display_pub_class(parent=self) |
|
673 | self.display_pub = self.display_pub_class(parent=self) | |
675 | self.configurables.append(self.display_pub) |
|
674 | self.configurables.append(self.display_pub) | |
676 |
|
675 | |||
677 | def init_data_pub(self): |
|
676 | def init_data_pub(self): | |
678 | if not self.data_pub_class: |
|
677 | if not self.data_pub_class: | |
679 | self.data_pub = None |
|
678 | self.data_pub = None | |
680 | return |
|
679 | return | |
681 | self.data_pub = self.data_pub_class(parent=self) |
|
680 | self.data_pub = self.data_pub_class(parent=self) | |
682 | self.configurables.append(self.data_pub) |
|
681 | self.configurables.append(self.data_pub) | |
683 |
|
682 | |||
684 | def init_displayhook(self): |
|
683 | def init_displayhook(self): | |
685 | # Initialize displayhook, set in/out prompts and printing system |
|
684 | # Initialize displayhook, set in/out prompts and printing system | |
686 | self.displayhook = self.displayhook_class( |
|
685 | self.displayhook = self.displayhook_class( | |
687 | parent=self, |
|
686 | parent=self, | |
688 | shell=self, |
|
687 | shell=self, | |
689 | cache_size=self.cache_size, |
|
688 | cache_size=self.cache_size, | |
690 | ) |
|
689 | ) | |
691 | self.configurables.append(self.displayhook) |
|
690 | self.configurables.append(self.displayhook) | |
692 | # This is a context manager that installs/revmoes the displayhook at |
|
691 | # This is a context manager that installs/revmoes the displayhook at | |
693 | # the appropriate time. |
|
692 | # the appropriate time. | |
694 | self.display_trap = DisplayTrap(hook=self.displayhook) |
|
693 | self.display_trap = DisplayTrap(hook=self.displayhook) | |
695 |
|
694 | |||
696 | def init_latextool(self): |
|
695 | def init_latextool(self): | |
697 | """Configure LaTeXTool.""" |
|
696 | """Configure LaTeXTool.""" | |
698 | cfg = LaTeXTool.instance(parent=self) |
|
697 | cfg = LaTeXTool.instance(parent=self) | |
699 | if cfg not in self.configurables: |
|
698 | if cfg not in self.configurables: | |
700 | self.configurables.append(cfg) |
|
699 | self.configurables.append(cfg) | |
701 |
|
700 | |||
702 | def init_virtualenv(self): |
|
701 | def init_virtualenv(self): | |
703 | """Add a virtualenv to sys.path so the user can import modules from it. |
|
702 | """Add a virtualenv to sys.path so the user can import modules from it. | |
704 | This isn't perfect: it doesn't use the Python interpreter with which the |
|
703 | This isn't perfect: it doesn't use the Python interpreter with which the | |
705 | virtualenv was built, and it ignores the --no-site-packages option. A |
|
704 | virtualenv was built, and it ignores the --no-site-packages option. A | |
706 | warning will appear suggesting the user installs IPython in the |
|
705 | warning will appear suggesting the user installs IPython in the | |
707 | virtualenv, but for many cases, it probably works well enough. |
|
706 | virtualenv, but for many cases, it probably works well enough. | |
708 |
|
707 | |||
709 | Adapted from code snippets online. |
|
708 | Adapted from code snippets online. | |
710 |
|
709 | |||
711 | http://blog.ufsoft.org/2009/1/29/ipython-and-virtualenv |
|
710 | http://blog.ufsoft.org/2009/1/29/ipython-and-virtualenv | |
712 | """ |
|
711 | """ | |
713 | if 'VIRTUAL_ENV' not in os.environ: |
|
712 | if 'VIRTUAL_ENV' not in os.environ: | |
714 | # Not in a virtualenv |
|
713 | # Not in a virtualenv | |
715 | return |
|
714 | return | |
716 |
|
715 | |||
717 | if sys.executable.startswith(os.environ['VIRTUAL_ENV']): |
|
716 | if sys.executable.startswith(os.environ['VIRTUAL_ENV']): | |
718 | # Running properly in the virtualenv, don't need to do anything |
|
717 | # Running properly in the virtualenv, don't need to do anything | |
719 | return |
|
718 | return | |
720 |
|
719 | |||
721 | warn("Attempting to work in a virtualenv. If you encounter problems, please " |
|
720 | warn("Attempting to work in a virtualenv. If you encounter problems, please " | |
722 | "install IPython inside the virtualenv.") |
|
721 | "install IPython inside the virtualenv.") | |
723 | if sys.platform == "win32": |
|
722 | if sys.platform == "win32": | |
724 | virtual_env = os.path.join(os.environ['VIRTUAL_ENV'], 'Lib', 'site-packages') |
|
723 | virtual_env = os.path.join(os.environ['VIRTUAL_ENV'], 'Lib', 'site-packages') | |
725 | else: |
|
724 | else: | |
726 | virtual_env = os.path.join(os.environ['VIRTUAL_ENV'], 'lib', |
|
725 | virtual_env = os.path.join(os.environ['VIRTUAL_ENV'], 'lib', | |
727 | 'python%d.%d' % sys.version_info[:2], 'site-packages') |
|
726 | 'python%d.%d' % sys.version_info[:2], 'site-packages') | |
728 |
|
727 | |||
729 | import site |
|
728 | import site | |
730 | sys.path.insert(0, virtual_env) |
|
729 | sys.path.insert(0, virtual_env) | |
731 | site.addsitedir(virtual_env) |
|
730 | site.addsitedir(virtual_env) | |
732 |
|
731 | |||
733 | #------------------------------------------------------------------------- |
|
732 | #------------------------------------------------------------------------- | |
734 | # Things related to injections into the sys module |
|
733 | # Things related to injections into the sys module | |
735 | #------------------------------------------------------------------------- |
|
734 | #------------------------------------------------------------------------- | |
736 |
|
735 | |||
737 | def save_sys_module_state(self): |
|
736 | def save_sys_module_state(self): | |
738 | """Save the state of hooks in the sys module. |
|
737 | """Save the state of hooks in the sys module. | |
739 |
|
738 | |||
740 | This has to be called after self.user_module is created. |
|
739 | This has to be called after self.user_module is created. | |
741 | """ |
|
740 | """ | |
742 | self._orig_sys_module_state = {} |
|
741 | self._orig_sys_module_state = {} | |
743 | self._orig_sys_module_state['stdin'] = sys.stdin |
|
742 | self._orig_sys_module_state['stdin'] = sys.stdin | |
744 | self._orig_sys_module_state['stdout'] = sys.stdout |
|
743 | self._orig_sys_module_state['stdout'] = sys.stdout | |
745 | self._orig_sys_module_state['stderr'] = sys.stderr |
|
744 | self._orig_sys_module_state['stderr'] = sys.stderr | |
746 | self._orig_sys_module_state['excepthook'] = sys.excepthook |
|
745 | self._orig_sys_module_state['excepthook'] = sys.excepthook | |
747 | self._orig_sys_modules_main_name = self.user_module.__name__ |
|
746 | self._orig_sys_modules_main_name = self.user_module.__name__ | |
748 | self._orig_sys_modules_main_mod = sys.modules.get(self.user_module.__name__) |
|
747 | self._orig_sys_modules_main_mod = sys.modules.get(self.user_module.__name__) | |
749 |
|
748 | |||
750 | def restore_sys_module_state(self): |
|
749 | def restore_sys_module_state(self): | |
751 | """Restore the state of the sys module.""" |
|
750 | """Restore the state of the sys module.""" | |
752 | try: |
|
751 | try: | |
753 | for k, v in self._orig_sys_module_state.iteritems(): |
|
752 | for k, v in self._orig_sys_module_state.iteritems(): | |
754 | setattr(sys, k, v) |
|
753 | setattr(sys, k, v) | |
755 | except AttributeError: |
|
754 | except AttributeError: | |
756 | pass |
|
755 | pass | |
757 | # Reset what what done in self.init_sys_modules |
|
756 | # Reset what what done in self.init_sys_modules | |
758 | if self._orig_sys_modules_main_mod is not None: |
|
757 | if self._orig_sys_modules_main_mod is not None: | |
759 | sys.modules[self._orig_sys_modules_main_name] = self._orig_sys_modules_main_mod |
|
758 | sys.modules[self._orig_sys_modules_main_name] = self._orig_sys_modules_main_mod | |
760 |
|
759 | |||
761 | #------------------------------------------------------------------------- |
|
760 | #------------------------------------------------------------------------- | |
762 | # Things related to hooks |
|
761 | # Things related to hooks | |
763 | #------------------------------------------------------------------------- |
|
762 | #------------------------------------------------------------------------- | |
764 |
|
763 | |||
765 | def init_hooks(self): |
|
764 | def init_hooks(self): | |
766 | # hooks holds pointers used for user-side customizations |
|
765 | # hooks holds pointers used for user-side customizations | |
767 | self.hooks = Struct() |
|
766 | self.hooks = Struct() | |
768 |
|
767 | |||
769 | self.strdispatchers = {} |
|
768 | self.strdispatchers = {} | |
770 |
|
769 | |||
771 | # Set all default hooks, defined in the IPython.hooks module. |
|
770 | # Set all default hooks, defined in the IPython.hooks module. | |
772 | hooks = IPython.core.hooks |
|
771 | hooks = IPython.core.hooks | |
773 | for hook_name in hooks.__all__: |
|
772 | for hook_name in hooks.__all__: | |
774 | # default hooks have priority 100, i.e. low; user hooks should have |
|
773 | # default hooks have priority 100, i.e. low; user hooks should have | |
775 | # 0-100 priority |
|
774 | # 0-100 priority | |
776 | self.set_hook(hook_name,getattr(hooks,hook_name), 100) |
|
775 | self.set_hook(hook_name,getattr(hooks,hook_name), 100) | |
777 |
|
776 | |||
778 | def set_hook(self,name,hook, priority = 50, str_key = None, re_key = None): |
|
777 | def set_hook(self,name,hook, priority = 50, str_key = None, re_key = None): | |
779 | """set_hook(name,hook) -> sets an internal IPython hook. |
|
778 | """set_hook(name,hook) -> sets an internal IPython hook. | |
780 |
|
779 | |||
781 | IPython exposes some of its internal API as user-modifiable hooks. By |
|
780 | IPython exposes some of its internal API as user-modifiable hooks. By | |
782 | adding your function to one of these hooks, you can modify IPython's |
|
781 | adding your function to one of these hooks, you can modify IPython's | |
783 | behavior to call at runtime your own routines.""" |
|
782 | behavior to call at runtime your own routines.""" | |
784 |
|
783 | |||
785 | # At some point in the future, this should validate the hook before it |
|
784 | # At some point in the future, this should validate the hook before it | |
786 | # accepts it. Probably at least check that the hook takes the number |
|
785 | # accepts it. Probably at least check that the hook takes the number | |
787 | # of args it's supposed to. |
|
786 | # of args it's supposed to. | |
788 |
|
787 | |||
789 | f = types.MethodType(hook,self) |
|
788 | f = types.MethodType(hook,self) | |
790 |
|
789 | |||
791 | # check if the hook is for strdispatcher first |
|
790 | # check if the hook is for strdispatcher first | |
792 | if str_key is not None: |
|
791 | if str_key is not None: | |
793 | sdp = self.strdispatchers.get(name, StrDispatch()) |
|
792 | sdp = self.strdispatchers.get(name, StrDispatch()) | |
794 | sdp.add_s(str_key, f, priority ) |
|
793 | sdp.add_s(str_key, f, priority ) | |
795 | self.strdispatchers[name] = sdp |
|
794 | self.strdispatchers[name] = sdp | |
796 | return |
|
795 | return | |
797 | if re_key is not None: |
|
796 | if re_key is not None: | |
798 | sdp = self.strdispatchers.get(name, StrDispatch()) |
|
797 | sdp = self.strdispatchers.get(name, StrDispatch()) | |
799 | sdp.add_re(re.compile(re_key), f, priority ) |
|
798 | sdp.add_re(re.compile(re_key), f, priority ) | |
800 | self.strdispatchers[name] = sdp |
|
799 | self.strdispatchers[name] = sdp | |
801 | return |
|
800 | return | |
802 |
|
801 | |||
803 | dp = getattr(self.hooks, name, None) |
|
802 | dp = getattr(self.hooks, name, None) | |
804 | if name not in IPython.core.hooks.__all__: |
|
803 | if name not in IPython.core.hooks.__all__: | |
805 | print("Warning! Hook '%s' is not one of %s" % \ |
|
804 | print("Warning! Hook '%s' is not one of %s" % \ | |
806 | (name, IPython.core.hooks.__all__ )) |
|
805 | (name, IPython.core.hooks.__all__ )) | |
807 | if not dp: |
|
806 | if not dp: | |
808 | dp = IPython.core.hooks.CommandChainDispatcher() |
|
807 | dp = IPython.core.hooks.CommandChainDispatcher() | |
809 |
|
808 | |||
810 | try: |
|
809 | try: | |
811 | dp.add(f,priority) |
|
810 | dp.add(f,priority) | |
812 | except AttributeError: |
|
811 | except AttributeError: | |
813 | # it was not commandchain, plain old func - replace |
|
812 | # it was not commandchain, plain old func - replace | |
814 | dp = f |
|
813 | dp = f | |
815 |
|
814 | |||
816 | setattr(self.hooks,name, dp) |
|
815 | setattr(self.hooks,name, dp) | |
817 |
|
816 | |||
818 | def register_post_execute(self, func): |
|
817 | def register_post_execute(self, func): | |
819 | """Register a function for calling after code execution. |
|
818 | """Register a function for calling after code execution. | |
820 | """ |
|
819 | """ | |
821 | if not callable(func): |
|
820 | if not callable(func): | |
822 | raise ValueError('argument %s must be callable' % func) |
|
821 | raise ValueError('argument %s must be callable' % func) | |
823 | self._post_execute[func] = True |
|
822 | self._post_execute[func] = True | |
824 |
|
823 | |||
825 | #------------------------------------------------------------------------- |
|
824 | #------------------------------------------------------------------------- | |
826 | # Things related to the "main" module |
|
825 | # Things related to the "main" module | |
827 | #------------------------------------------------------------------------- |
|
826 | #------------------------------------------------------------------------- | |
828 |
|
827 | |||
829 | def new_main_mod(self, filename): |
|
828 | def new_main_mod(self, filename, modname): | |
830 | """Return a new 'main' module object for user code execution. |
|
829 | """Return a new 'main' module object for user code execution. | |
831 |
|
830 | |||
832 | ``filename`` should be the path of the script which will be run in the |
|
831 | ``filename`` should be the path of the script which will be run in the | |
833 | module. Requests with the same filename will get the same module, with |
|
832 | module. Requests with the same filename will get the same module, with | |
834 | its namespace cleared. |
|
833 | its namespace cleared. | |
835 |
|
834 | |||
|
835 | ``modname`` should be the module name - normally either '__main__' or | |||
|
836 | the basename of the file without the extension. | |||
|
837 | ||||
836 | When scripts are executed via %run, we must keep a reference to their |
|
838 | When scripts are executed via %run, we must keep a reference to their | |
837 |
__main__ module |
|
839 | __main__ module around so that Python doesn't | |
838 | clear it, rendering references to module globals useless. |
|
840 | clear it, rendering references to module globals useless. | |
839 |
|
841 | |||
840 | This method keeps said reference in a private dict, keyed by the |
|
842 | This method keeps said reference in a private dict, keyed by the | |
841 | absolute path of the script. This way, for multiple executions of the |
|
843 | absolute path of the script. This way, for multiple executions of the | |
842 | same script we only keep one copy of the namespace (the last one), |
|
844 | same script we only keep one copy of the namespace (the last one), | |
843 | thus preventing memory leaks from old references while allowing the |
|
845 | thus preventing memory leaks from old references while allowing the | |
844 | objects from the last execution to be accessible. |
|
846 | objects from the last execution to be accessible. | |
845 | """ |
|
847 | """ | |
846 | filename = os.path.abspath(filename) |
|
848 | filename = os.path.abspath(filename) | |
847 | try: |
|
849 | try: | |
848 | main_mod = self._main_mod_cache[filename] |
|
850 | main_mod = self._main_mod_cache[filename] | |
849 | except KeyError: |
|
851 | except KeyError: | |
850 |
main_mod = self._main_mod_cache[filename] = |
|
852 | main_mod = self._main_mod_cache[filename] = types.ModuleType(modname, | |
|
853 | doc="Module created for script run in IPython") | |||
851 | else: |
|
854 | else: | |
852 | init_fakemod_dict(main_mod) |
|
855 | main_mod.__dict__.clear() | |
|
856 | main_mod.__name__ = modname | |||
|
857 | ||||
|
858 | main_mod.__file__ = filename | |||
|
859 | # It seems pydoc (and perhaps others) needs any module instance to | |||
|
860 | # implement a __nonzero__ method | |||
|
861 | main_mod.__nonzero__ = lambda : True | |||
853 |
|
862 | |||
854 | return main_mod |
|
863 | return main_mod | |
855 |
|
864 | |||
856 | def clear_main_mod_cache(self): |
|
865 | def clear_main_mod_cache(self): | |
857 | """Clear the cache of main modules. |
|
866 | """Clear the cache of main modules. | |
858 |
|
867 | |||
859 | Mainly for use by utilities like %reset. |
|
868 | Mainly for use by utilities like %reset. | |
860 |
|
869 | |||
861 | Examples |
|
870 | Examples | |
862 | -------- |
|
871 | -------- | |
863 |
|
872 | |||
864 | In [15]: import IPython |
|
873 | In [15]: import IPython | |
865 |
|
874 | |||
866 | In [16]: m = _ip.new_main_mod(IPython.__file__) |
|
875 | In [16]: m = _ip.new_main_mod(IPython.__file__, 'IPython') | |
867 |
|
876 | |||
868 | In [17]: len(_ip._main_mod_cache) > 0 |
|
877 | In [17]: len(_ip._main_mod_cache) > 0 | |
869 | Out[17]: True |
|
878 | Out[17]: True | |
870 |
|
879 | |||
871 | In [18]: _ip.clear_main_mod_cache() |
|
880 | In [18]: _ip.clear_main_mod_cache() | |
872 |
|
881 | |||
873 | In [19]: len(_ip._main_mod_cache) == 0 |
|
882 | In [19]: len(_ip._main_mod_cache) == 0 | |
874 | Out[19]: True |
|
883 | Out[19]: True | |
875 | """ |
|
884 | """ | |
876 | self._main_mod_cache.clear() |
|
885 | self._main_mod_cache.clear() | |
877 |
|
886 | |||
878 | #------------------------------------------------------------------------- |
|
887 | #------------------------------------------------------------------------- | |
879 | # Things related to debugging |
|
888 | # Things related to debugging | |
880 | #------------------------------------------------------------------------- |
|
889 | #------------------------------------------------------------------------- | |
881 |
|
890 | |||
882 | def init_pdb(self): |
|
891 | def init_pdb(self): | |
883 | # Set calling of pdb on exceptions |
|
892 | # Set calling of pdb on exceptions | |
884 | # self.call_pdb is a property |
|
893 | # self.call_pdb is a property | |
885 | self.call_pdb = self.pdb |
|
894 | self.call_pdb = self.pdb | |
886 |
|
895 | |||
887 | def _get_call_pdb(self): |
|
896 | def _get_call_pdb(self): | |
888 | return self._call_pdb |
|
897 | return self._call_pdb | |
889 |
|
898 | |||
890 | def _set_call_pdb(self,val): |
|
899 | def _set_call_pdb(self,val): | |
891 |
|
900 | |||
892 | if val not in (0,1,False,True): |
|
901 | if val not in (0,1,False,True): | |
893 | raise ValueError('new call_pdb value must be boolean') |
|
902 | raise ValueError('new call_pdb value must be boolean') | |
894 |
|
903 | |||
895 | # store value in instance |
|
904 | # store value in instance | |
896 | self._call_pdb = val |
|
905 | self._call_pdb = val | |
897 |
|
906 | |||
898 | # notify the actual exception handlers |
|
907 | # notify the actual exception handlers | |
899 | self.InteractiveTB.call_pdb = val |
|
908 | self.InteractiveTB.call_pdb = val | |
900 |
|
909 | |||
901 | call_pdb = property(_get_call_pdb,_set_call_pdb,None, |
|
910 | call_pdb = property(_get_call_pdb,_set_call_pdb,None, | |
902 | 'Control auto-activation of pdb at exceptions') |
|
911 | 'Control auto-activation of pdb at exceptions') | |
903 |
|
912 | |||
904 | def debugger(self,force=False): |
|
913 | def debugger(self,force=False): | |
905 | """Call the pydb/pdb debugger. |
|
914 | """Call the pydb/pdb debugger. | |
906 |
|
915 | |||
907 | Keywords: |
|
916 | Keywords: | |
908 |
|
917 | |||
909 | - force(False): by default, this routine checks the instance call_pdb |
|
918 | - force(False): by default, this routine checks the instance call_pdb | |
910 | flag and does not actually invoke the debugger if the flag is false. |
|
919 | flag and does not actually invoke the debugger if the flag is false. | |
911 | The 'force' option forces the debugger to activate even if the flag |
|
920 | The 'force' option forces the debugger to activate even if the flag | |
912 | is false. |
|
921 | is false. | |
913 | """ |
|
922 | """ | |
914 |
|
923 | |||
915 | if not (force or self.call_pdb): |
|
924 | if not (force or self.call_pdb): | |
916 | return |
|
925 | return | |
917 |
|
926 | |||
918 | if not hasattr(sys,'last_traceback'): |
|
927 | if not hasattr(sys,'last_traceback'): | |
919 | error('No traceback has been produced, nothing to debug.') |
|
928 | error('No traceback has been produced, nothing to debug.') | |
920 | return |
|
929 | return | |
921 |
|
930 | |||
922 | # use pydb if available |
|
931 | # use pydb if available | |
923 | if debugger.has_pydb: |
|
932 | if debugger.has_pydb: | |
924 | from pydb import pm |
|
933 | from pydb import pm | |
925 | else: |
|
934 | else: | |
926 | # fallback to our internal debugger |
|
935 | # fallback to our internal debugger | |
927 | pm = lambda : self.InteractiveTB.debugger(force=True) |
|
936 | pm = lambda : self.InteractiveTB.debugger(force=True) | |
928 |
|
937 | |||
929 | with self.readline_no_record: |
|
938 | with self.readline_no_record: | |
930 | pm() |
|
939 | pm() | |
931 |
|
940 | |||
932 | #------------------------------------------------------------------------- |
|
941 | #------------------------------------------------------------------------- | |
933 | # Things related to IPython's various namespaces |
|
942 | # Things related to IPython's various namespaces | |
934 | #------------------------------------------------------------------------- |
|
943 | #------------------------------------------------------------------------- | |
935 | default_user_namespaces = True |
|
944 | default_user_namespaces = True | |
936 |
|
945 | |||
937 | def init_create_namespaces(self, user_module=None, user_ns=None): |
|
946 | def init_create_namespaces(self, user_module=None, user_ns=None): | |
938 | # Create the namespace where the user will operate. user_ns is |
|
947 | # Create the namespace where the user will operate. user_ns is | |
939 | # normally the only one used, and it is passed to the exec calls as |
|
948 | # normally the only one used, and it is passed to the exec calls as | |
940 | # the locals argument. But we do carry a user_global_ns namespace |
|
949 | # the locals argument. But we do carry a user_global_ns namespace | |
941 | # given as the exec 'globals' argument, This is useful in embedding |
|
950 | # given as the exec 'globals' argument, This is useful in embedding | |
942 | # situations where the ipython shell opens in a context where the |
|
951 | # situations where the ipython shell opens in a context where the | |
943 | # distinction between locals and globals is meaningful. For |
|
952 | # distinction between locals and globals is meaningful. For | |
944 | # non-embedded contexts, it is just the same object as the user_ns dict. |
|
953 | # non-embedded contexts, it is just the same object as the user_ns dict. | |
945 |
|
954 | |||
946 | # FIXME. For some strange reason, __builtins__ is showing up at user |
|
955 | # FIXME. For some strange reason, __builtins__ is showing up at user | |
947 | # level as a dict instead of a module. This is a manual fix, but I |
|
956 | # level as a dict instead of a module. This is a manual fix, but I | |
948 | # should really track down where the problem is coming from. Alex |
|
957 | # should really track down where the problem is coming from. Alex | |
949 | # Schmolck reported this problem first. |
|
958 | # Schmolck reported this problem first. | |
950 |
|
959 | |||
951 | # A useful post by Alex Martelli on this topic: |
|
960 | # A useful post by Alex Martelli on this topic: | |
952 | # Re: inconsistent value from __builtins__ |
|
961 | # Re: inconsistent value from __builtins__ | |
953 | # Von: Alex Martelli <aleaxit@yahoo.com> |
|
962 | # Von: Alex Martelli <aleaxit@yahoo.com> | |
954 | # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends |
|
963 | # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends | |
955 | # Gruppen: comp.lang.python |
|
964 | # Gruppen: comp.lang.python | |
956 |
|
965 | |||
957 | # Michael Hohn <hohn@hooknose.lbl.gov> wrote: |
|
966 | # Michael Hohn <hohn@hooknose.lbl.gov> wrote: | |
958 | # > >>> print type(builtin_check.get_global_binding('__builtins__')) |
|
967 | # > >>> print type(builtin_check.get_global_binding('__builtins__')) | |
959 | # > <type 'dict'> |
|
968 | # > <type 'dict'> | |
960 | # > >>> print type(__builtins__) |
|
969 | # > >>> print type(__builtins__) | |
961 | # > <type 'module'> |
|
970 | # > <type 'module'> | |
962 | # > Is this difference in return value intentional? |
|
971 | # > Is this difference in return value intentional? | |
963 |
|
972 | |||
964 | # Well, it's documented that '__builtins__' can be either a dictionary |
|
973 | # Well, it's documented that '__builtins__' can be either a dictionary | |
965 | # or a module, and it's been that way for a long time. Whether it's |
|
974 | # or a module, and it's been that way for a long time. Whether it's | |
966 | # intentional (or sensible), I don't know. In any case, the idea is |
|
975 | # intentional (or sensible), I don't know. In any case, the idea is | |
967 | # that if you need to access the built-in namespace directly, you |
|
976 | # that if you need to access the built-in namespace directly, you | |
968 | # should start with "import __builtin__" (note, no 's') which will |
|
977 | # should start with "import __builtin__" (note, no 's') which will | |
969 | # definitely give you a module. Yeah, it's somewhat confusing:-(. |
|
978 | # definitely give you a module. Yeah, it's somewhat confusing:-(. | |
970 |
|
979 | |||
971 | # These routines return a properly built module and dict as needed by |
|
980 | # These routines return a properly built module and dict as needed by | |
972 | # the rest of the code, and can also be used by extension writers to |
|
981 | # the rest of the code, and can also be used by extension writers to | |
973 | # generate properly initialized namespaces. |
|
982 | # generate properly initialized namespaces. | |
974 | if (user_ns is not None) or (user_module is not None): |
|
983 | if (user_ns is not None) or (user_module is not None): | |
975 | self.default_user_namespaces = False |
|
984 | self.default_user_namespaces = False | |
976 | self.user_module, self.user_ns = self.prepare_user_module(user_module, user_ns) |
|
985 | self.user_module, self.user_ns = self.prepare_user_module(user_module, user_ns) | |
977 |
|
986 | |||
978 | # A record of hidden variables we have added to the user namespace, so |
|
987 | # A record of hidden variables we have added to the user namespace, so | |
979 | # we can list later only variables defined in actual interactive use. |
|
988 | # we can list later only variables defined in actual interactive use. | |
980 | self.user_ns_hidden = {} |
|
989 | self.user_ns_hidden = {} | |
981 |
|
990 | |||
982 | # Now that FakeModule produces a real module, we've run into a nasty |
|
991 | # Now that FakeModule produces a real module, we've run into a nasty | |
983 | # problem: after script execution (via %run), the module where the user |
|
992 | # problem: after script execution (via %run), the module where the user | |
984 | # code ran is deleted. Now that this object is a true module (needed |
|
993 | # code ran is deleted. Now that this object is a true module (needed | |
985 | # so docetst and other tools work correctly), the Python module |
|
994 | # so docetst and other tools work correctly), the Python module | |
986 | # teardown mechanism runs over it, and sets to None every variable |
|
995 | # teardown mechanism runs over it, and sets to None every variable | |
987 | # present in that module. Top-level references to objects from the |
|
996 | # present in that module. Top-level references to objects from the | |
988 | # script survive, because the user_ns is updated with them. However, |
|
997 | # script survive, because the user_ns is updated with them. However, | |
989 | # calling functions defined in the script that use other things from |
|
998 | # calling functions defined in the script that use other things from | |
990 | # the script will fail, because the function's closure had references |
|
999 | # the script will fail, because the function's closure had references | |
991 | # to the original objects, which are now all None. So we must protect |
|
1000 | # to the original objects, which are now all None. So we must protect | |
992 | # these modules from deletion by keeping a cache. |
|
1001 | # these modules from deletion by keeping a cache. | |
993 | # |
|
1002 | # | |
994 | # To avoid keeping stale modules around (we only need the one from the |
|
1003 | # To avoid keeping stale modules around (we only need the one from the | |
995 | # last run), we use a dict keyed with the full path to the script, so |
|
1004 | # last run), we use a dict keyed with the full path to the script, so | |
996 | # only the last version of the module is held in the cache. Note, |
|
1005 | # only the last version of the module is held in the cache. Note, | |
997 | # however, that we must cache the module *namespace contents* (their |
|
1006 | # however, that we must cache the module *namespace contents* (their | |
998 | # __dict__). Because if we try to cache the actual modules, old ones |
|
1007 | # __dict__). Because if we try to cache the actual modules, old ones | |
999 | # (uncached) could be destroyed while still holding references (such as |
|
1008 | # (uncached) could be destroyed while still holding references (such as | |
1000 | # those held by GUI objects that tend to be long-lived)> |
|
1009 | # those held by GUI objects that tend to be long-lived)> | |
1001 | # |
|
1010 | # | |
1002 | # The %reset command will flush this cache. See the cache_main_mod() |
|
1011 | # The %reset command will flush this cache. See the cache_main_mod() | |
1003 | # and clear_main_mod_cache() methods for details on use. |
|
1012 | # and clear_main_mod_cache() methods for details on use. | |
1004 |
|
1013 | |||
1005 | # This is the cache used for 'main' namespaces |
|
1014 | # This is the cache used for 'main' namespaces | |
1006 | self._main_mod_cache = {} |
|
1015 | self._main_mod_cache = {} | |
1007 |
|
1016 | |||
1008 | # A table holding all the namespaces IPython deals with, so that |
|
1017 | # A table holding all the namespaces IPython deals with, so that | |
1009 | # introspection facilities can search easily. |
|
1018 | # introspection facilities can search easily. | |
1010 | self.ns_table = {'user_global':self.user_module.__dict__, |
|
1019 | self.ns_table = {'user_global':self.user_module.__dict__, | |
1011 | 'user_local':self.user_ns, |
|
1020 | 'user_local':self.user_ns, | |
1012 | 'builtin':builtin_mod.__dict__ |
|
1021 | 'builtin':builtin_mod.__dict__ | |
1013 | } |
|
1022 | } | |
1014 |
|
1023 | |||
1015 | @property |
|
1024 | @property | |
1016 | def user_global_ns(self): |
|
1025 | def user_global_ns(self): | |
1017 | return self.user_module.__dict__ |
|
1026 | return self.user_module.__dict__ | |
1018 |
|
1027 | |||
1019 | def prepare_user_module(self, user_module=None, user_ns=None): |
|
1028 | def prepare_user_module(self, user_module=None, user_ns=None): | |
1020 | """Prepare the module and namespace in which user code will be run. |
|
1029 | """Prepare the module and namespace in which user code will be run. | |
1021 |
|
1030 | |||
1022 | When IPython is started normally, both parameters are None: a new module |
|
1031 | When IPython is started normally, both parameters are None: a new module | |
1023 | is created automatically, and its __dict__ used as the namespace. |
|
1032 | is created automatically, and its __dict__ used as the namespace. | |
1024 |
|
1033 | |||
1025 | If only user_module is provided, its __dict__ is used as the namespace. |
|
1034 | If only user_module is provided, its __dict__ is used as the namespace. | |
1026 | If only user_ns is provided, a dummy module is created, and user_ns |
|
1035 | If only user_ns is provided, a dummy module is created, and user_ns | |
1027 | becomes the global namespace. If both are provided (as they may be |
|
1036 | becomes the global namespace. If both are provided (as they may be | |
1028 | when embedding), user_ns is the local namespace, and user_module |
|
1037 | when embedding), user_ns is the local namespace, and user_module | |
1029 | provides the global namespace. |
|
1038 | provides the global namespace. | |
1030 |
|
1039 | |||
1031 | Parameters |
|
1040 | Parameters | |
1032 | ---------- |
|
1041 | ---------- | |
1033 | user_module : module, optional |
|
1042 | user_module : module, optional | |
1034 | The current user module in which IPython is being run. If None, |
|
1043 | The current user module in which IPython is being run. If None, | |
1035 | a clean module will be created. |
|
1044 | a clean module will be created. | |
1036 | user_ns : dict, optional |
|
1045 | user_ns : dict, optional | |
1037 | A namespace in which to run interactive commands. |
|
1046 | A namespace in which to run interactive commands. | |
1038 |
|
1047 | |||
1039 | Returns |
|
1048 | Returns | |
1040 | ------- |
|
1049 | ------- | |
1041 | A tuple of user_module and user_ns, each properly initialised. |
|
1050 | A tuple of user_module and user_ns, each properly initialised. | |
1042 | """ |
|
1051 | """ | |
1043 | if user_module is None and user_ns is not None: |
|
1052 | if user_module is None and user_ns is not None: | |
1044 | user_ns.setdefault("__name__", "__main__") |
|
1053 | user_ns.setdefault("__name__", "__main__") | |
1045 | user_module = DummyMod() |
|
1054 | user_module = DummyMod() | |
1046 | user_module.__dict__ = user_ns |
|
1055 | user_module.__dict__ = user_ns | |
1047 |
|
1056 | |||
1048 | if user_module is None: |
|
1057 | if user_module is None: | |
1049 | user_module = types.ModuleType("__main__", |
|
1058 | user_module = types.ModuleType("__main__", | |
1050 | doc="Automatically created module for IPython interactive environment") |
|
1059 | doc="Automatically created module for IPython interactive environment") | |
1051 |
|
1060 | |||
1052 | # We must ensure that __builtin__ (without the final 's') is always |
|
1061 | # We must ensure that __builtin__ (without the final 's') is always | |
1053 | # available and pointing to the __builtin__ *module*. For more details: |
|
1062 | # available and pointing to the __builtin__ *module*. For more details: | |
1054 | # http://mail.python.org/pipermail/python-dev/2001-April/014068.html |
|
1063 | # http://mail.python.org/pipermail/python-dev/2001-April/014068.html | |
1055 | user_module.__dict__.setdefault('__builtin__', builtin_mod) |
|
1064 | user_module.__dict__.setdefault('__builtin__', builtin_mod) | |
1056 | user_module.__dict__.setdefault('__builtins__', builtin_mod) |
|
1065 | user_module.__dict__.setdefault('__builtins__', builtin_mod) | |
1057 |
|
1066 | |||
1058 | if user_ns is None: |
|
1067 | if user_ns is None: | |
1059 | user_ns = user_module.__dict__ |
|
1068 | user_ns = user_module.__dict__ | |
1060 |
|
1069 | |||
1061 | return user_module, user_ns |
|
1070 | return user_module, user_ns | |
1062 |
|
1071 | |||
1063 | def init_sys_modules(self): |
|
1072 | def init_sys_modules(self): | |
1064 | # We need to insert into sys.modules something that looks like a |
|
1073 | # We need to insert into sys.modules something that looks like a | |
1065 | # module but which accesses the IPython namespace, for shelve and |
|
1074 | # module but which accesses the IPython namespace, for shelve and | |
1066 | # pickle to work interactively. Normally they rely on getting |
|
1075 | # pickle to work interactively. Normally they rely on getting | |
1067 | # everything out of __main__, but for embedding purposes each IPython |
|
1076 | # everything out of __main__, but for embedding purposes each IPython | |
1068 | # instance has its own private namespace, so we can't go shoving |
|
1077 | # instance has its own private namespace, so we can't go shoving | |
1069 | # everything into __main__. |
|
1078 | # everything into __main__. | |
1070 |
|
1079 | |||
1071 | # note, however, that we should only do this for non-embedded |
|
1080 | # note, however, that we should only do this for non-embedded | |
1072 | # ipythons, which really mimic the __main__.__dict__ with their own |
|
1081 | # ipythons, which really mimic the __main__.__dict__ with their own | |
1073 | # namespace. Embedded instances, on the other hand, should not do |
|
1082 | # namespace. Embedded instances, on the other hand, should not do | |
1074 | # this because they need to manage the user local/global namespaces |
|
1083 | # this because they need to manage the user local/global namespaces | |
1075 | # only, but they live within a 'normal' __main__ (meaning, they |
|
1084 | # only, but they live within a 'normal' __main__ (meaning, they | |
1076 | # shouldn't overtake the execution environment of the script they're |
|
1085 | # shouldn't overtake the execution environment of the script they're | |
1077 | # embedded in). |
|
1086 | # embedded in). | |
1078 |
|
1087 | |||
1079 | # This is overridden in the InteractiveShellEmbed subclass to a no-op. |
|
1088 | # This is overridden in the InteractiveShellEmbed subclass to a no-op. | |
1080 | main_name = self.user_module.__name__ |
|
1089 | main_name = self.user_module.__name__ | |
1081 | sys.modules[main_name] = self.user_module |
|
1090 | sys.modules[main_name] = self.user_module | |
1082 |
|
1091 | |||
1083 | def init_user_ns(self): |
|
1092 | def init_user_ns(self): | |
1084 | """Initialize all user-visible namespaces to their minimum defaults. |
|
1093 | """Initialize all user-visible namespaces to their minimum defaults. | |
1085 |
|
1094 | |||
1086 | Certain history lists are also initialized here, as they effectively |
|
1095 | Certain history lists are also initialized here, as they effectively | |
1087 | act as user namespaces. |
|
1096 | act as user namespaces. | |
1088 |
|
1097 | |||
1089 | Notes |
|
1098 | Notes | |
1090 | ----- |
|
1099 | ----- | |
1091 | All data structures here are only filled in, they are NOT reset by this |
|
1100 | All data structures here are only filled in, they are NOT reset by this | |
1092 | method. If they were not empty before, data will simply be added to |
|
1101 | method. If they were not empty before, data will simply be added to | |
1093 | therm. |
|
1102 | therm. | |
1094 | """ |
|
1103 | """ | |
1095 | # This function works in two parts: first we put a few things in |
|
1104 | # This function works in two parts: first we put a few things in | |
1096 | # user_ns, and we sync that contents into user_ns_hidden so that these |
|
1105 | # user_ns, and we sync that contents into user_ns_hidden so that these | |
1097 | # initial variables aren't shown by %who. After the sync, we add the |
|
1106 | # initial variables aren't shown by %who. After the sync, we add the | |
1098 | # rest of what we *do* want the user to see with %who even on a new |
|
1107 | # rest of what we *do* want the user to see with %who even on a new | |
1099 | # session (probably nothing, so theye really only see their own stuff) |
|
1108 | # session (probably nothing, so theye really only see their own stuff) | |
1100 |
|
1109 | |||
1101 | # The user dict must *always* have a __builtin__ reference to the |
|
1110 | # The user dict must *always* have a __builtin__ reference to the | |
1102 | # Python standard __builtin__ namespace, which must be imported. |
|
1111 | # Python standard __builtin__ namespace, which must be imported. | |
1103 | # This is so that certain operations in prompt evaluation can be |
|
1112 | # This is so that certain operations in prompt evaluation can be | |
1104 | # reliably executed with builtins. Note that we can NOT use |
|
1113 | # reliably executed with builtins. Note that we can NOT use | |
1105 | # __builtins__ (note the 's'), because that can either be a dict or a |
|
1114 | # __builtins__ (note the 's'), because that can either be a dict or a | |
1106 | # module, and can even mutate at runtime, depending on the context |
|
1115 | # module, and can even mutate at runtime, depending on the context | |
1107 | # (Python makes no guarantees on it). In contrast, __builtin__ is |
|
1116 | # (Python makes no guarantees on it). In contrast, __builtin__ is | |
1108 | # always a module object, though it must be explicitly imported. |
|
1117 | # always a module object, though it must be explicitly imported. | |
1109 |
|
1118 | |||
1110 | # For more details: |
|
1119 | # For more details: | |
1111 | # http://mail.python.org/pipermail/python-dev/2001-April/014068.html |
|
1120 | # http://mail.python.org/pipermail/python-dev/2001-April/014068.html | |
1112 | ns = dict() |
|
1121 | ns = dict() | |
1113 |
|
1122 | |||
1114 | # Put 'help' in the user namespace |
|
1123 | # Put 'help' in the user namespace | |
1115 | try: |
|
1124 | try: | |
1116 | from site import _Helper |
|
1125 | from site import _Helper | |
1117 | ns['help'] = _Helper() |
|
1126 | ns['help'] = _Helper() | |
1118 | except ImportError: |
|
1127 | except ImportError: | |
1119 | warn('help() not available - check site.py') |
|
1128 | warn('help() not available - check site.py') | |
1120 |
|
1129 | |||
1121 | # make global variables for user access to the histories |
|
1130 | # make global variables for user access to the histories | |
1122 | ns['_ih'] = self.history_manager.input_hist_parsed |
|
1131 | ns['_ih'] = self.history_manager.input_hist_parsed | |
1123 | ns['_oh'] = self.history_manager.output_hist |
|
1132 | ns['_oh'] = self.history_manager.output_hist | |
1124 | ns['_dh'] = self.history_manager.dir_hist |
|
1133 | ns['_dh'] = self.history_manager.dir_hist | |
1125 |
|
1134 | |||
1126 | ns['_sh'] = shadowns |
|
1135 | ns['_sh'] = shadowns | |
1127 |
|
1136 | |||
1128 | # user aliases to input and output histories. These shouldn't show up |
|
1137 | # user aliases to input and output histories. These shouldn't show up | |
1129 | # in %who, as they can have very large reprs. |
|
1138 | # in %who, as they can have very large reprs. | |
1130 | ns['In'] = self.history_manager.input_hist_parsed |
|
1139 | ns['In'] = self.history_manager.input_hist_parsed | |
1131 | ns['Out'] = self.history_manager.output_hist |
|
1140 | ns['Out'] = self.history_manager.output_hist | |
1132 |
|
1141 | |||
1133 | # Store myself as the public api!!! |
|
1142 | # Store myself as the public api!!! | |
1134 | ns['get_ipython'] = self.get_ipython |
|
1143 | ns['get_ipython'] = self.get_ipython | |
1135 |
|
1144 | |||
1136 | ns['exit'] = self.exiter |
|
1145 | ns['exit'] = self.exiter | |
1137 | ns['quit'] = self.exiter |
|
1146 | ns['quit'] = self.exiter | |
1138 |
|
1147 | |||
1139 | # Sync what we've added so far to user_ns_hidden so these aren't seen |
|
1148 | # Sync what we've added so far to user_ns_hidden so these aren't seen | |
1140 | # by %who |
|
1149 | # by %who | |
1141 | self.user_ns_hidden.update(ns) |
|
1150 | self.user_ns_hidden.update(ns) | |
1142 |
|
1151 | |||
1143 | # Anything put into ns now would show up in %who. Think twice before |
|
1152 | # Anything put into ns now would show up in %who. Think twice before | |
1144 | # putting anything here, as we really want %who to show the user their |
|
1153 | # putting anything here, as we really want %who to show the user their | |
1145 | # stuff, not our variables. |
|
1154 | # stuff, not our variables. | |
1146 |
|
1155 | |||
1147 | # Finally, update the real user's namespace |
|
1156 | # Finally, update the real user's namespace | |
1148 | self.user_ns.update(ns) |
|
1157 | self.user_ns.update(ns) | |
1149 |
|
1158 | |||
1150 | @property |
|
1159 | @property | |
1151 | def all_ns_refs(self): |
|
1160 | def all_ns_refs(self): | |
1152 | """Get a list of references to all the namespace dictionaries in which |
|
1161 | """Get a list of references to all the namespace dictionaries in which | |
1153 | IPython might store a user-created object. |
|
1162 | IPython might store a user-created object. | |
1154 |
|
1163 | |||
1155 | Note that this does not include the displayhook, which also caches |
|
1164 | Note that this does not include the displayhook, which also caches | |
1156 | objects from the output.""" |
|
1165 | objects from the output.""" | |
1157 | return [self.user_ns, self.user_global_ns, self.user_ns_hidden] + \ |
|
1166 | return [self.user_ns, self.user_global_ns, self.user_ns_hidden] + \ | |
1158 | [m.__dict__ for m in self._main_mod_cache.values()] |
|
1167 | [m.__dict__ for m in self._main_mod_cache.values()] | |
1159 |
|
1168 | |||
1160 | def reset(self, new_session=True): |
|
1169 | def reset(self, new_session=True): | |
1161 | """Clear all internal namespaces, and attempt to release references to |
|
1170 | """Clear all internal namespaces, and attempt to release references to | |
1162 | user objects. |
|
1171 | user objects. | |
1163 |
|
1172 | |||
1164 | If new_session is True, a new history session will be opened. |
|
1173 | If new_session is True, a new history session will be opened. | |
1165 | """ |
|
1174 | """ | |
1166 | # Clear histories |
|
1175 | # Clear histories | |
1167 | self.history_manager.reset(new_session) |
|
1176 | self.history_manager.reset(new_session) | |
1168 | # Reset counter used to index all histories |
|
1177 | # Reset counter used to index all histories | |
1169 | if new_session: |
|
1178 | if new_session: | |
1170 | self.execution_count = 1 |
|
1179 | self.execution_count = 1 | |
1171 |
|
1180 | |||
1172 | # Flush cached output items |
|
1181 | # Flush cached output items | |
1173 | if self.displayhook.do_full_cache: |
|
1182 | if self.displayhook.do_full_cache: | |
1174 | self.displayhook.flush() |
|
1183 | self.displayhook.flush() | |
1175 |
|
1184 | |||
1176 | # The main execution namespaces must be cleared very carefully, |
|
1185 | # The main execution namespaces must be cleared very carefully, | |
1177 | # skipping the deletion of the builtin-related keys, because doing so |
|
1186 | # skipping the deletion of the builtin-related keys, because doing so | |
1178 | # would cause errors in many object's __del__ methods. |
|
1187 | # would cause errors in many object's __del__ methods. | |
1179 | if self.user_ns is not self.user_global_ns: |
|
1188 | if self.user_ns is not self.user_global_ns: | |
1180 | self.user_ns.clear() |
|
1189 | self.user_ns.clear() | |
1181 | ns = self.user_global_ns |
|
1190 | ns = self.user_global_ns | |
1182 | drop_keys = set(ns.keys()) |
|
1191 | drop_keys = set(ns.keys()) | |
1183 | drop_keys.discard('__builtin__') |
|
1192 | drop_keys.discard('__builtin__') | |
1184 | drop_keys.discard('__builtins__') |
|
1193 | drop_keys.discard('__builtins__') | |
1185 | drop_keys.discard('__name__') |
|
1194 | drop_keys.discard('__name__') | |
1186 | for k in drop_keys: |
|
1195 | for k in drop_keys: | |
1187 | del ns[k] |
|
1196 | del ns[k] | |
1188 |
|
1197 | |||
1189 | self.user_ns_hidden.clear() |
|
1198 | self.user_ns_hidden.clear() | |
1190 |
|
1199 | |||
1191 | # Restore the user namespaces to minimal usability |
|
1200 | # Restore the user namespaces to minimal usability | |
1192 | self.init_user_ns() |
|
1201 | self.init_user_ns() | |
1193 |
|
1202 | |||
1194 | # Restore the default and user aliases |
|
1203 | # Restore the default and user aliases | |
1195 | self.alias_manager.clear_aliases() |
|
1204 | self.alias_manager.clear_aliases() | |
1196 | self.alias_manager.init_aliases() |
|
1205 | self.alias_manager.init_aliases() | |
1197 |
|
1206 | |||
1198 | # Flush the private list of module references kept for script |
|
1207 | # Flush the private list of module references kept for script | |
1199 | # execution protection |
|
1208 | # execution protection | |
1200 | self.clear_main_mod_cache() |
|
1209 | self.clear_main_mod_cache() | |
1201 |
|
1210 | |||
1202 | def del_var(self, varname, by_name=False): |
|
1211 | def del_var(self, varname, by_name=False): | |
1203 | """Delete a variable from the various namespaces, so that, as |
|
1212 | """Delete a variable from the various namespaces, so that, as | |
1204 | far as possible, we're not keeping any hidden references to it. |
|
1213 | far as possible, we're not keeping any hidden references to it. | |
1205 |
|
1214 | |||
1206 | Parameters |
|
1215 | Parameters | |
1207 | ---------- |
|
1216 | ---------- | |
1208 | varname : str |
|
1217 | varname : str | |
1209 | The name of the variable to delete. |
|
1218 | The name of the variable to delete. | |
1210 | by_name : bool |
|
1219 | by_name : bool | |
1211 | If True, delete variables with the given name in each |
|
1220 | If True, delete variables with the given name in each | |
1212 | namespace. If False (default), find the variable in the user |
|
1221 | namespace. If False (default), find the variable in the user | |
1213 | namespace, and delete references to it. |
|
1222 | namespace, and delete references to it. | |
1214 | """ |
|
1223 | """ | |
1215 | if varname in ('__builtin__', '__builtins__'): |
|
1224 | if varname in ('__builtin__', '__builtins__'): | |
1216 | raise ValueError("Refusing to delete %s" % varname) |
|
1225 | raise ValueError("Refusing to delete %s" % varname) | |
1217 |
|
1226 | |||
1218 | ns_refs = self.all_ns_refs |
|
1227 | ns_refs = self.all_ns_refs | |
1219 |
|
1228 | |||
1220 | if by_name: # Delete by name |
|
1229 | if by_name: # Delete by name | |
1221 | for ns in ns_refs: |
|
1230 | for ns in ns_refs: | |
1222 | try: |
|
1231 | try: | |
1223 | del ns[varname] |
|
1232 | del ns[varname] | |
1224 | except KeyError: |
|
1233 | except KeyError: | |
1225 | pass |
|
1234 | pass | |
1226 | else: # Delete by object |
|
1235 | else: # Delete by object | |
1227 | try: |
|
1236 | try: | |
1228 | obj = self.user_ns[varname] |
|
1237 | obj = self.user_ns[varname] | |
1229 | except KeyError: |
|
1238 | except KeyError: | |
1230 | raise NameError("name '%s' is not defined" % varname) |
|
1239 | raise NameError("name '%s' is not defined" % varname) | |
1231 | # Also check in output history |
|
1240 | # Also check in output history | |
1232 | ns_refs.append(self.history_manager.output_hist) |
|
1241 | ns_refs.append(self.history_manager.output_hist) | |
1233 | for ns in ns_refs: |
|
1242 | for ns in ns_refs: | |
1234 | to_delete = [n for n, o in ns.iteritems() if o is obj] |
|
1243 | to_delete = [n for n, o in ns.iteritems() if o is obj] | |
1235 | for name in to_delete: |
|
1244 | for name in to_delete: | |
1236 | del ns[name] |
|
1245 | del ns[name] | |
1237 |
|
1246 | |||
1238 | # displayhook keeps extra references, but not in a dictionary |
|
1247 | # displayhook keeps extra references, but not in a dictionary | |
1239 | for name in ('_', '__', '___'): |
|
1248 | for name in ('_', '__', '___'): | |
1240 | if getattr(self.displayhook, name) is obj: |
|
1249 | if getattr(self.displayhook, name) is obj: | |
1241 | setattr(self.displayhook, name, None) |
|
1250 | setattr(self.displayhook, name, None) | |
1242 |
|
1251 | |||
1243 | def reset_selective(self, regex=None): |
|
1252 | def reset_selective(self, regex=None): | |
1244 | """Clear selective variables from internal namespaces based on a |
|
1253 | """Clear selective variables from internal namespaces based on a | |
1245 | specified regular expression. |
|
1254 | specified regular expression. | |
1246 |
|
1255 | |||
1247 | Parameters |
|
1256 | Parameters | |
1248 | ---------- |
|
1257 | ---------- | |
1249 | regex : string or compiled pattern, optional |
|
1258 | regex : string or compiled pattern, optional | |
1250 | A regular expression pattern that will be used in searching |
|
1259 | A regular expression pattern that will be used in searching | |
1251 | variable names in the users namespaces. |
|
1260 | variable names in the users namespaces. | |
1252 | """ |
|
1261 | """ | |
1253 | if regex is not None: |
|
1262 | if regex is not None: | |
1254 | try: |
|
1263 | try: | |
1255 | m = re.compile(regex) |
|
1264 | m = re.compile(regex) | |
1256 | except TypeError: |
|
1265 | except TypeError: | |
1257 | raise TypeError('regex must be a string or compiled pattern') |
|
1266 | raise TypeError('regex must be a string or compiled pattern') | |
1258 | # Search for keys in each namespace that match the given regex |
|
1267 | # Search for keys in each namespace that match the given regex | |
1259 | # If a match is found, delete the key/value pair. |
|
1268 | # If a match is found, delete the key/value pair. | |
1260 | for ns in self.all_ns_refs: |
|
1269 | for ns in self.all_ns_refs: | |
1261 | for var in ns: |
|
1270 | for var in ns: | |
1262 | if m.search(var): |
|
1271 | if m.search(var): | |
1263 | del ns[var] |
|
1272 | del ns[var] | |
1264 |
|
1273 | |||
1265 | def push(self, variables, interactive=True): |
|
1274 | def push(self, variables, interactive=True): | |
1266 | """Inject a group of variables into the IPython user namespace. |
|
1275 | """Inject a group of variables into the IPython user namespace. | |
1267 |
|
1276 | |||
1268 | Parameters |
|
1277 | Parameters | |
1269 | ---------- |
|
1278 | ---------- | |
1270 | variables : dict, str or list/tuple of str |
|
1279 | variables : dict, str or list/tuple of str | |
1271 | The variables to inject into the user's namespace. If a dict, a |
|
1280 | The variables to inject into the user's namespace. If a dict, a | |
1272 | simple update is done. If a str, the string is assumed to have |
|
1281 | simple update is done. If a str, the string is assumed to have | |
1273 | variable names separated by spaces. A list/tuple of str can also |
|
1282 | variable names separated by spaces. A list/tuple of str can also | |
1274 | be used to give the variable names. If just the variable names are |
|
1283 | be used to give the variable names. If just the variable names are | |
1275 | give (list/tuple/str) then the variable values looked up in the |
|
1284 | give (list/tuple/str) then the variable values looked up in the | |
1276 | callers frame. |
|
1285 | callers frame. | |
1277 | interactive : bool |
|
1286 | interactive : bool | |
1278 | If True (default), the variables will be listed with the ``who`` |
|
1287 | If True (default), the variables will be listed with the ``who`` | |
1279 | magic. |
|
1288 | magic. | |
1280 | """ |
|
1289 | """ | |
1281 | vdict = None |
|
1290 | vdict = None | |
1282 |
|
1291 | |||
1283 | # We need a dict of name/value pairs to do namespace updates. |
|
1292 | # We need a dict of name/value pairs to do namespace updates. | |
1284 | if isinstance(variables, dict): |
|
1293 | if isinstance(variables, dict): | |
1285 | vdict = variables |
|
1294 | vdict = variables | |
1286 | elif isinstance(variables, (basestring, list, tuple)): |
|
1295 | elif isinstance(variables, (basestring, list, tuple)): | |
1287 | if isinstance(variables, basestring): |
|
1296 | if isinstance(variables, basestring): | |
1288 | vlist = variables.split() |
|
1297 | vlist = variables.split() | |
1289 | else: |
|
1298 | else: | |
1290 | vlist = variables |
|
1299 | vlist = variables | |
1291 | vdict = {} |
|
1300 | vdict = {} | |
1292 | cf = sys._getframe(1) |
|
1301 | cf = sys._getframe(1) | |
1293 | for name in vlist: |
|
1302 | for name in vlist: | |
1294 | try: |
|
1303 | try: | |
1295 | vdict[name] = eval(name, cf.f_globals, cf.f_locals) |
|
1304 | vdict[name] = eval(name, cf.f_globals, cf.f_locals) | |
1296 | except: |
|
1305 | except: | |
1297 | print('Could not get variable %s from %s' % |
|
1306 | print('Could not get variable %s from %s' % | |
1298 | (name,cf.f_code.co_name)) |
|
1307 | (name,cf.f_code.co_name)) | |
1299 | else: |
|
1308 | else: | |
1300 | raise ValueError('variables must be a dict/str/list/tuple') |
|
1309 | raise ValueError('variables must be a dict/str/list/tuple') | |
1301 |
|
1310 | |||
1302 | # Propagate variables to user namespace |
|
1311 | # Propagate variables to user namespace | |
1303 | self.user_ns.update(vdict) |
|
1312 | self.user_ns.update(vdict) | |
1304 |
|
1313 | |||
1305 | # And configure interactive visibility |
|
1314 | # And configure interactive visibility | |
1306 | user_ns_hidden = self.user_ns_hidden |
|
1315 | user_ns_hidden = self.user_ns_hidden | |
1307 | if interactive: |
|
1316 | if interactive: | |
1308 | for name in vdict: |
|
1317 | for name in vdict: | |
1309 | user_ns_hidden.pop(name, None) |
|
1318 | user_ns_hidden.pop(name, None) | |
1310 | else: |
|
1319 | else: | |
1311 | user_ns_hidden.update(vdict) |
|
1320 | user_ns_hidden.update(vdict) | |
1312 |
|
1321 | |||
1313 | def drop_by_id(self, variables): |
|
1322 | def drop_by_id(self, variables): | |
1314 | """Remove a dict of variables from the user namespace, if they are the |
|
1323 | """Remove a dict of variables from the user namespace, if they are the | |
1315 | same as the values in the dictionary. |
|
1324 | same as the values in the dictionary. | |
1316 |
|
1325 | |||
1317 | This is intended for use by extensions: variables that they've added can |
|
1326 | This is intended for use by extensions: variables that they've added can | |
1318 | be taken back out if they are unloaded, without removing any that the |
|
1327 | be taken back out if they are unloaded, without removing any that the | |
1319 | user has overwritten. |
|
1328 | user has overwritten. | |
1320 |
|
1329 | |||
1321 | Parameters |
|
1330 | Parameters | |
1322 | ---------- |
|
1331 | ---------- | |
1323 | variables : dict |
|
1332 | variables : dict | |
1324 | A dictionary mapping object names (as strings) to the objects. |
|
1333 | A dictionary mapping object names (as strings) to the objects. | |
1325 | """ |
|
1334 | """ | |
1326 | for name, obj in variables.iteritems(): |
|
1335 | for name, obj in variables.iteritems(): | |
1327 | if name in self.user_ns and self.user_ns[name] is obj: |
|
1336 | if name in self.user_ns and self.user_ns[name] is obj: | |
1328 | del self.user_ns[name] |
|
1337 | del self.user_ns[name] | |
1329 | self.user_ns_hidden.pop(name, None) |
|
1338 | self.user_ns_hidden.pop(name, None) | |
1330 |
|
1339 | |||
1331 | #------------------------------------------------------------------------- |
|
1340 | #------------------------------------------------------------------------- | |
1332 | # Things related to object introspection |
|
1341 | # Things related to object introspection | |
1333 | #------------------------------------------------------------------------- |
|
1342 | #------------------------------------------------------------------------- | |
1334 |
|
1343 | |||
1335 | def _ofind(self, oname, namespaces=None): |
|
1344 | def _ofind(self, oname, namespaces=None): | |
1336 | """Find an object in the available namespaces. |
|
1345 | """Find an object in the available namespaces. | |
1337 |
|
1346 | |||
1338 | self._ofind(oname) -> dict with keys: found,obj,ospace,ismagic |
|
1347 | self._ofind(oname) -> dict with keys: found,obj,ospace,ismagic | |
1339 |
|
1348 | |||
1340 | Has special code to detect magic functions. |
|
1349 | Has special code to detect magic functions. | |
1341 | """ |
|
1350 | """ | |
1342 | oname = oname.strip() |
|
1351 | oname = oname.strip() | |
1343 | #print '1- oname: <%r>' % oname # dbg |
|
1352 | #print '1- oname: <%r>' % oname # dbg | |
1344 | if not oname.startswith(ESC_MAGIC) and \ |
|
1353 | if not oname.startswith(ESC_MAGIC) and \ | |
1345 | not oname.startswith(ESC_MAGIC2) and \ |
|
1354 | not oname.startswith(ESC_MAGIC2) and \ | |
1346 | not py3compat.isidentifier(oname, dotted=True): |
|
1355 | not py3compat.isidentifier(oname, dotted=True): | |
1347 | return dict(found=False) |
|
1356 | return dict(found=False) | |
1348 |
|
1357 | |||
1349 | alias_ns = None |
|
1358 | alias_ns = None | |
1350 | if namespaces is None: |
|
1359 | if namespaces is None: | |
1351 | # Namespaces to search in: |
|
1360 | # Namespaces to search in: | |
1352 | # Put them in a list. The order is important so that we |
|
1361 | # Put them in a list. The order is important so that we | |
1353 | # find things in the same order that Python finds them. |
|
1362 | # find things in the same order that Python finds them. | |
1354 | namespaces = [ ('Interactive', self.user_ns), |
|
1363 | namespaces = [ ('Interactive', self.user_ns), | |
1355 | ('Interactive (global)', self.user_global_ns), |
|
1364 | ('Interactive (global)', self.user_global_ns), | |
1356 | ('Python builtin', builtin_mod.__dict__), |
|
1365 | ('Python builtin', builtin_mod.__dict__), | |
1357 | ('Alias', self.alias_manager.alias_table), |
|
1366 | ('Alias', self.alias_manager.alias_table), | |
1358 | ] |
|
1367 | ] | |
1359 | alias_ns = self.alias_manager.alias_table |
|
1368 | alias_ns = self.alias_manager.alias_table | |
1360 |
|
1369 | |||
1361 | # initialize results to 'null' |
|
1370 | # initialize results to 'null' | |
1362 | found = False; obj = None; ospace = None; ds = None; |
|
1371 | found = False; obj = None; ospace = None; ds = None; | |
1363 | ismagic = False; isalias = False; parent = None |
|
1372 | ismagic = False; isalias = False; parent = None | |
1364 |
|
1373 | |||
1365 | # We need to special-case 'print', which as of python2.6 registers as a |
|
1374 | # We need to special-case 'print', which as of python2.6 registers as a | |
1366 | # function but should only be treated as one if print_function was |
|
1375 | # function but should only be treated as one if print_function was | |
1367 | # loaded with a future import. In this case, just bail. |
|
1376 | # loaded with a future import. In this case, just bail. | |
1368 | if (oname == 'print' and not py3compat.PY3 and not \ |
|
1377 | if (oname == 'print' and not py3compat.PY3 and not \ | |
1369 | (self.compile.compiler_flags & __future__.CO_FUTURE_PRINT_FUNCTION)): |
|
1378 | (self.compile.compiler_flags & __future__.CO_FUTURE_PRINT_FUNCTION)): | |
1370 | return {'found':found, 'obj':obj, 'namespace':ospace, |
|
1379 | return {'found':found, 'obj':obj, 'namespace':ospace, | |
1371 | 'ismagic':ismagic, 'isalias':isalias, 'parent':parent} |
|
1380 | 'ismagic':ismagic, 'isalias':isalias, 'parent':parent} | |
1372 |
|
1381 | |||
1373 | # Look for the given name by splitting it in parts. If the head is |
|
1382 | # Look for the given name by splitting it in parts. If the head is | |
1374 | # found, then we look for all the remaining parts as members, and only |
|
1383 | # found, then we look for all the remaining parts as members, and only | |
1375 | # declare success if we can find them all. |
|
1384 | # declare success if we can find them all. | |
1376 | oname_parts = oname.split('.') |
|
1385 | oname_parts = oname.split('.') | |
1377 | oname_head, oname_rest = oname_parts[0],oname_parts[1:] |
|
1386 | oname_head, oname_rest = oname_parts[0],oname_parts[1:] | |
1378 | for nsname,ns in namespaces: |
|
1387 | for nsname,ns in namespaces: | |
1379 | try: |
|
1388 | try: | |
1380 | obj = ns[oname_head] |
|
1389 | obj = ns[oname_head] | |
1381 | except KeyError: |
|
1390 | except KeyError: | |
1382 | continue |
|
1391 | continue | |
1383 | else: |
|
1392 | else: | |
1384 | #print 'oname_rest:', oname_rest # dbg |
|
1393 | #print 'oname_rest:', oname_rest # dbg | |
1385 | for part in oname_rest: |
|
1394 | for part in oname_rest: | |
1386 | try: |
|
1395 | try: | |
1387 | parent = obj |
|
1396 | parent = obj | |
1388 | obj = getattr(obj,part) |
|
1397 | obj = getattr(obj,part) | |
1389 | except: |
|
1398 | except: | |
1390 | # Blanket except b/c some badly implemented objects |
|
1399 | # Blanket except b/c some badly implemented objects | |
1391 | # allow __getattr__ to raise exceptions other than |
|
1400 | # allow __getattr__ to raise exceptions other than | |
1392 | # AttributeError, which then crashes IPython. |
|
1401 | # AttributeError, which then crashes IPython. | |
1393 | break |
|
1402 | break | |
1394 | else: |
|
1403 | else: | |
1395 | # If we finish the for loop (no break), we got all members |
|
1404 | # If we finish the for loop (no break), we got all members | |
1396 | found = True |
|
1405 | found = True | |
1397 | ospace = nsname |
|
1406 | ospace = nsname | |
1398 | if ns == alias_ns: |
|
1407 | if ns == alias_ns: | |
1399 | isalias = True |
|
1408 | isalias = True | |
1400 | break # namespace loop |
|
1409 | break # namespace loop | |
1401 |
|
1410 | |||
1402 | # Try to see if it's magic |
|
1411 | # Try to see if it's magic | |
1403 | if not found: |
|
1412 | if not found: | |
1404 | obj = None |
|
1413 | obj = None | |
1405 | if oname.startswith(ESC_MAGIC2): |
|
1414 | if oname.startswith(ESC_MAGIC2): | |
1406 | oname = oname.lstrip(ESC_MAGIC2) |
|
1415 | oname = oname.lstrip(ESC_MAGIC2) | |
1407 | obj = self.find_cell_magic(oname) |
|
1416 | obj = self.find_cell_magic(oname) | |
1408 | elif oname.startswith(ESC_MAGIC): |
|
1417 | elif oname.startswith(ESC_MAGIC): | |
1409 | oname = oname.lstrip(ESC_MAGIC) |
|
1418 | oname = oname.lstrip(ESC_MAGIC) | |
1410 | obj = self.find_line_magic(oname) |
|
1419 | obj = self.find_line_magic(oname) | |
1411 | else: |
|
1420 | else: | |
1412 | # search without prefix, so run? will find %run? |
|
1421 | # search without prefix, so run? will find %run? | |
1413 | obj = self.find_line_magic(oname) |
|
1422 | obj = self.find_line_magic(oname) | |
1414 | if obj is None: |
|
1423 | if obj is None: | |
1415 | obj = self.find_cell_magic(oname) |
|
1424 | obj = self.find_cell_magic(oname) | |
1416 | if obj is not None: |
|
1425 | if obj is not None: | |
1417 | found = True |
|
1426 | found = True | |
1418 | ospace = 'IPython internal' |
|
1427 | ospace = 'IPython internal' | |
1419 | ismagic = True |
|
1428 | ismagic = True | |
1420 |
|
1429 | |||
1421 | # Last try: special-case some literals like '', [], {}, etc: |
|
1430 | # Last try: special-case some literals like '', [], {}, etc: | |
1422 | if not found and oname_head in ["''",'""','[]','{}','()']: |
|
1431 | if not found and oname_head in ["''",'""','[]','{}','()']: | |
1423 | obj = eval(oname_head) |
|
1432 | obj = eval(oname_head) | |
1424 | found = True |
|
1433 | found = True | |
1425 | ospace = 'Interactive' |
|
1434 | ospace = 'Interactive' | |
1426 |
|
1435 | |||
1427 | return {'found':found, 'obj':obj, 'namespace':ospace, |
|
1436 | return {'found':found, 'obj':obj, 'namespace':ospace, | |
1428 | 'ismagic':ismagic, 'isalias':isalias, 'parent':parent} |
|
1437 | 'ismagic':ismagic, 'isalias':isalias, 'parent':parent} | |
1429 |
|
1438 | |||
1430 | def _ofind_property(self, oname, info): |
|
1439 | def _ofind_property(self, oname, info): | |
1431 | """Second part of object finding, to look for property details.""" |
|
1440 | """Second part of object finding, to look for property details.""" | |
1432 | if info.found: |
|
1441 | if info.found: | |
1433 | # Get the docstring of the class property if it exists. |
|
1442 | # Get the docstring of the class property if it exists. | |
1434 | path = oname.split('.') |
|
1443 | path = oname.split('.') | |
1435 | root = '.'.join(path[:-1]) |
|
1444 | root = '.'.join(path[:-1]) | |
1436 | if info.parent is not None: |
|
1445 | if info.parent is not None: | |
1437 | try: |
|
1446 | try: | |
1438 | target = getattr(info.parent, '__class__') |
|
1447 | target = getattr(info.parent, '__class__') | |
1439 | # The object belongs to a class instance. |
|
1448 | # The object belongs to a class instance. | |
1440 | try: |
|
1449 | try: | |
1441 | target = getattr(target, path[-1]) |
|
1450 | target = getattr(target, path[-1]) | |
1442 | # The class defines the object. |
|
1451 | # The class defines the object. | |
1443 | if isinstance(target, property): |
|
1452 | if isinstance(target, property): | |
1444 | oname = root + '.__class__.' + path[-1] |
|
1453 | oname = root + '.__class__.' + path[-1] | |
1445 | info = Struct(self._ofind(oname)) |
|
1454 | info = Struct(self._ofind(oname)) | |
1446 | except AttributeError: pass |
|
1455 | except AttributeError: pass | |
1447 | except AttributeError: pass |
|
1456 | except AttributeError: pass | |
1448 |
|
1457 | |||
1449 | # We return either the new info or the unmodified input if the object |
|
1458 | # We return either the new info or the unmodified input if the object | |
1450 | # hadn't been found |
|
1459 | # hadn't been found | |
1451 | return info |
|
1460 | return info | |
1452 |
|
1461 | |||
1453 | def _object_find(self, oname, namespaces=None): |
|
1462 | def _object_find(self, oname, namespaces=None): | |
1454 | """Find an object and return a struct with info about it.""" |
|
1463 | """Find an object and return a struct with info about it.""" | |
1455 | inf = Struct(self._ofind(oname, namespaces)) |
|
1464 | inf = Struct(self._ofind(oname, namespaces)) | |
1456 | return Struct(self._ofind_property(oname, inf)) |
|
1465 | return Struct(self._ofind_property(oname, inf)) | |
1457 |
|
1466 | |||
1458 | def _inspect(self, meth, oname, namespaces=None, **kw): |
|
1467 | def _inspect(self, meth, oname, namespaces=None, **kw): | |
1459 | """Generic interface to the inspector system. |
|
1468 | """Generic interface to the inspector system. | |
1460 |
|
1469 | |||
1461 | This function is meant to be called by pdef, pdoc & friends.""" |
|
1470 | This function is meant to be called by pdef, pdoc & friends.""" | |
1462 | info = self._object_find(oname, namespaces) |
|
1471 | info = self._object_find(oname, namespaces) | |
1463 | if info.found: |
|
1472 | if info.found: | |
1464 | pmethod = getattr(self.inspector, meth) |
|
1473 | pmethod = getattr(self.inspector, meth) | |
1465 | formatter = format_screen if info.ismagic else None |
|
1474 | formatter = format_screen if info.ismagic else None | |
1466 | if meth == 'pdoc': |
|
1475 | if meth == 'pdoc': | |
1467 | pmethod(info.obj, oname, formatter) |
|
1476 | pmethod(info.obj, oname, formatter) | |
1468 | elif meth == 'pinfo': |
|
1477 | elif meth == 'pinfo': | |
1469 | pmethod(info.obj, oname, formatter, info, **kw) |
|
1478 | pmethod(info.obj, oname, formatter, info, **kw) | |
1470 | else: |
|
1479 | else: | |
1471 | pmethod(info.obj, oname) |
|
1480 | pmethod(info.obj, oname) | |
1472 | else: |
|
1481 | else: | |
1473 | print('Object `%s` not found.' % oname) |
|
1482 | print('Object `%s` not found.' % oname) | |
1474 | return 'not found' # so callers can take other action |
|
1483 | return 'not found' # so callers can take other action | |
1475 |
|
1484 | |||
1476 | def object_inspect(self, oname, detail_level=0): |
|
1485 | def object_inspect(self, oname, detail_level=0): | |
1477 | with self.builtin_trap: |
|
1486 | with self.builtin_trap: | |
1478 | info = self._object_find(oname) |
|
1487 | info = self._object_find(oname) | |
1479 | if info.found: |
|
1488 | if info.found: | |
1480 | return self.inspector.info(info.obj, oname, info=info, |
|
1489 | return self.inspector.info(info.obj, oname, info=info, | |
1481 | detail_level=detail_level |
|
1490 | detail_level=detail_level | |
1482 | ) |
|
1491 | ) | |
1483 | else: |
|
1492 | else: | |
1484 | return oinspect.object_info(name=oname, found=False) |
|
1493 | return oinspect.object_info(name=oname, found=False) | |
1485 |
|
1494 | |||
1486 | #------------------------------------------------------------------------- |
|
1495 | #------------------------------------------------------------------------- | |
1487 | # Things related to history management |
|
1496 | # Things related to history management | |
1488 | #------------------------------------------------------------------------- |
|
1497 | #------------------------------------------------------------------------- | |
1489 |
|
1498 | |||
1490 | def init_history(self): |
|
1499 | def init_history(self): | |
1491 | """Sets up the command history, and starts regular autosaves.""" |
|
1500 | """Sets up the command history, and starts regular autosaves.""" | |
1492 | self.history_manager = HistoryManager(shell=self, parent=self) |
|
1501 | self.history_manager = HistoryManager(shell=self, parent=self) | |
1493 | self.configurables.append(self.history_manager) |
|
1502 | self.configurables.append(self.history_manager) | |
1494 |
|
1503 | |||
1495 | #------------------------------------------------------------------------- |
|
1504 | #------------------------------------------------------------------------- | |
1496 | # Things related to exception handling and tracebacks (not debugging) |
|
1505 | # Things related to exception handling and tracebacks (not debugging) | |
1497 | #------------------------------------------------------------------------- |
|
1506 | #------------------------------------------------------------------------- | |
1498 |
|
1507 | |||
1499 | def init_traceback_handlers(self, custom_exceptions): |
|
1508 | def init_traceback_handlers(self, custom_exceptions): | |
1500 | # Syntax error handler. |
|
1509 | # Syntax error handler. | |
1501 | self.SyntaxTB = ultratb.SyntaxTB(color_scheme='NoColor') |
|
1510 | self.SyntaxTB = ultratb.SyntaxTB(color_scheme='NoColor') | |
1502 |
|
1511 | |||
1503 | # The interactive one is initialized with an offset, meaning we always |
|
1512 | # The interactive one is initialized with an offset, meaning we always | |
1504 | # want to remove the topmost item in the traceback, which is our own |
|
1513 | # want to remove the topmost item in the traceback, which is our own | |
1505 | # internal code. Valid modes: ['Plain','Context','Verbose'] |
|
1514 | # internal code. Valid modes: ['Plain','Context','Verbose'] | |
1506 | self.InteractiveTB = ultratb.AutoFormattedTB(mode = 'Plain', |
|
1515 | self.InteractiveTB = ultratb.AutoFormattedTB(mode = 'Plain', | |
1507 | color_scheme='NoColor', |
|
1516 | color_scheme='NoColor', | |
1508 | tb_offset = 1, |
|
1517 | tb_offset = 1, | |
1509 | check_cache=check_linecache_ipython) |
|
1518 | check_cache=check_linecache_ipython) | |
1510 |
|
1519 | |||
1511 | # The instance will store a pointer to the system-wide exception hook, |
|
1520 | # The instance will store a pointer to the system-wide exception hook, | |
1512 | # so that runtime code (such as magics) can access it. This is because |
|
1521 | # so that runtime code (such as magics) can access it. This is because | |
1513 | # during the read-eval loop, it may get temporarily overwritten. |
|
1522 | # during the read-eval loop, it may get temporarily overwritten. | |
1514 | self.sys_excepthook = sys.excepthook |
|
1523 | self.sys_excepthook = sys.excepthook | |
1515 |
|
1524 | |||
1516 | # and add any custom exception handlers the user may have specified |
|
1525 | # and add any custom exception handlers the user may have specified | |
1517 | self.set_custom_exc(*custom_exceptions) |
|
1526 | self.set_custom_exc(*custom_exceptions) | |
1518 |
|
1527 | |||
1519 | # Set the exception mode |
|
1528 | # Set the exception mode | |
1520 | self.InteractiveTB.set_mode(mode=self.xmode) |
|
1529 | self.InteractiveTB.set_mode(mode=self.xmode) | |
1521 |
|
1530 | |||
1522 | def set_custom_exc(self, exc_tuple, handler): |
|
1531 | def set_custom_exc(self, exc_tuple, handler): | |
1523 | """set_custom_exc(exc_tuple,handler) |
|
1532 | """set_custom_exc(exc_tuple,handler) | |
1524 |
|
1533 | |||
1525 | Set a custom exception handler, which will be called if any of the |
|
1534 | Set a custom exception handler, which will be called if any of the | |
1526 | exceptions in exc_tuple occur in the mainloop (specifically, in the |
|
1535 | exceptions in exc_tuple occur in the mainloop (specifically, in the | |
1527 | run_code() method). |
|
1536 | run_code() method). | |
1528 |
|
1537 | |||
1529 | Parameters |
|
1538 | Parameters | |
1530 | ---------- |
|
1539 | ---------- | |
1531 |
|
1540 | |||
1532 | exc_tuple : tuple of exception classes |
|
1541 | exc_tuple : tuple of exception classes | |
1533 | A *tuple* of exception classes, for which to call the defined |
|
1542 | A *tuple* of exception classes, for which to call the defined | |
1534 | handler. It is very important that you use a tuple, and NOT A |
|
1543 | handler. It is very important that you use a tuple, and NOT A | |
1535 | LIST here, because of the way Python's except statement works. If |
|
1544 | LIST here, because of the way Python's except statement works. If | |
1536 | you only want to trap a single exception, use a singleton tuple:: |
|
1545 | you only want to trap a single exception, use a singleton tuple:: | |
1537 |
|
1546 | |||
1538 | exc_tuple == (MyCustomException,) |
|
1547 | exc_tuple == (MyCustomException,) | |
1539 |
|
1548 | |||
1540 | handler : callable |
|
1549 | handler : callable | |
1541 | handler must have the following signature:: |
|
1550 | handler must have the following signature:: | |
1542 |
|
1551 | |||
1543 | def my_handler(self, etype, value, tb, tb_offset=None): |
|
1552 | def my_handler(self, etype, value, tb, tb_offset=None): | |
1544 | ... |
|
1553 | ... | |
1545 | return structured_traceback |
|
1554 | return structured_traceback | |
1546 |
|
1555 | |||
1547 | Your handler must return a structured traceback (a list of strings), |
|
1556 | Your handler must return a structured traceback (a list of strings), | |
1548 | or None. |
|
1557 | or None. | |
1549 |
|
1558 | |||
1550 | This will be made into an instance method (via types.MethodType) |
|
1559 | This will be made into an instance method (via types.MethodType) | |
1551 | of IPython itself, and it will be called if any of the exceptions |
|
1560 | of IPython itself, and it will be called if any of the exceptions | |
1552 | listed in the exc_tuple are caught. If the handler is None, an |
|
1561 | listed in the exc_tuple are caught. If the handler is None, an | |
1553 | internal basic one is used, which just prints basic info. |
|
1562 | internal basic one is used, which just prints basic info. | |
1554 |
|
1563 | |||
1555 | To protect IPython from crashes, if your handler ever raises an |
|
1564 | To protect IPython from crashes, if your handler ever raises an | |
1556 | exception or returns an invalid result, it will be immediately |
|
1565 | exception or returns an invalid result, it will be immediately | |
1557 | disabled. |
|
1566 | disabled. | |
1558 |
|
1567 | |||
1559 | WARNING: by putting in your own exception handler into IPython's main |
|
1568 | WARNING: by putting in your own exception handler into IPython's main | |
1560 | execution loop, you run a very good chance of nasty crashes. This |
|
1569 | execution loop, you run a very good chance of nasty crashes. This | |
1561 | facility should only be used if you really know what you are doing.""" |
|
1570 | facility should only be used if you really know what you are doing.""" | |
1562 |
|
1571 | |||
1563 | assert type(exc_tuple)==type(()) , \ |
|
1572 | assert type(exc_tuple)==type(()) , \ | |
1564 | "The custom exceptions must be given AS A TUPLE." |
|
1573 | "The custom exceptions must be given AS A TUPLE." | |
1565 |
|
1574 | |||
1566 | def dummy_handler(self,etype,value,tb,tb_offset=None): |
|
1575 | def dummy_handler(self,etype,value,tb,tb_offset=None): | |
1567 | print('*** Simple custom exception handler ***') |
|
1576 | print('*** Simple custom exception handler ***') | |
1568 | print('Exception type :',etype) |
|
1577 | print('Exception type :',etype) | |
1569 | print('Exception value:',value) |
|
1578 | print('Exception value:',value) | |
1570 | print('Traceback :',tb) |
|
1579 | print('Traceback :',tb) | |
1571 | #print 'Source code :','\n'.join(self.buffer) |
|
1580 | #print 'Source code :','\n'.join(self.buffer) | |
1572 |
|
1581 | |||
1573 | def validate_stb(stb): |
|
1582 | def validate_stb(stb): | |
1574 | """validate structured traceback return type |
|
1583 | """validate structured traceback return type | |
1575 |
|
1584 | |||
1576 | return type of CustomTB *should* be a list of strings, but allow |
|
1585 | return type of CustomTB *should* be a list of strings, but allow | |
1577 | single strings or None, which are harmless. |
|
1586 | single strings or None, which are harmless. | |
1578 |
|
1587 | |||
1579 | This function will *always* return a list of strings, |
|
1588 | This function will *always* return a list of strings, | |
1580 | and will raise a TypeError if stb is inappropriate. |
|
1589 | and will raise a TypeError if stb is inappropriate. | |
1581 | """ |
|
1590 | """ | |
1582 | msg = "CustomTB must return list of strings, not %r" % stb |
|
1591 | msg = "CustomTB must return list of strings, not %r" % stb | |
1583 | if stb is None: |
|
1592 | if stb is None: | |
1584 | return [] |
|
1593 | return [] | |
1585 | elif isinstance(stb, basestring): |
|
1594 | elif isinstance(stb, basestring): | |
1586 | return [stb] |
|
1595 | return [stb] | |
1587 | elif not isinstance(stb, list): |
|
1596 | elif not isinstance(stb, list): | |
1588 | raise TypeError(msg) |
|
1597 | raise TypeError(msg) | |
1589 | # it's a list |
|
1598 | # it's a list | |
1590 | for line in stb: |
|
1599 | for line in stb: | |
1591 | # check every element |
|
1600 | # check every element | |
1592 | if not isinstance(line, basestring): |
|
1601 | if not isinstance(line, basestring): | |
1593 | raise TypeError(msg) |
|
1602 | raise TypeError(msg) | |
1594 | return stb |
|
1603 | return stb | |
1595 |
|
1604 | |||
1596 | if handler is None: |
|
1605 | if handler is None: | |
1597 | wrapped = dummy_handler |
|
1606 | wrapped = dummy_handler | |
1598 | else: |
|
1607 | else: | |
1599 | def wrapped(self,etype,value,tb,tb_offset=None): |
|
1608 | def wrapped(self,etype,value,tb,tb_offset=None): | |
1600 | """wrap CustomTB handler, to protect IPython from user code |
|
1609 | """wrap CustomTB handler, to protect IPython from user code | |
1601 |
|
1610 | |||
1602 | This makes it harder (but not impossible) for custom exception |
|
1611 | This makes it harder (but not impossible) for custom exception | |
1603 | handlers to crash IPython. |
|
1612 | handlers to crash IPython. | |
1604 | """ |
|
1613 | """ | |
1605 | try: |
|
1614 | try: | |
1606 | stb = handler(self,etype,value,tb,tb_offset=tb_offset) |
|
1615 | stb = handler(self,etype,value,tb,tb_offset=tb_offset) | |
1607 | return validate_stb(stb) |
|
1616 | return validate_stb(stb) | |
1608 | except: |
|
1617 | except: | |
1609 | # clear custom handler immediately |
|
1618 | # clear custom handler immediately | |
1610 | self.set_custom_exc((), None) |
|
1619 | self.set_custom_exc((), None) | |
1611 | print("Custom TB Handler failed, unregistering", file=io.stderr) |
|
1620 | print("Custom TB Handler failed, unregistering", file=io.stderr) | |
1612 | # show the exception in handler first |
|
1621 | # show the exception in handler first | |
1613 | stb = self.InteractiveTB.structured_traceback(*sys.exc_info()) |
|
1622 | stb = self.InteractiveTB.structured_traceback(*sys.exc_info()) | |
1614 | print(self.InteractiveTB.stb2text(stb), file=io.stdout) |
|
1623 | print(self.InteractiveTB.stb2text(stb), file=io.stdout) | |
1615 | print("The original exception:", file=io.stdout) |
|
1624 | print("The original exception:", file=io.stdout) | |
1616 | stb = self.InteractiveTB.structured_traceback( |
|
1625 | stb = self.InteractiveTB.structured_traceback( | |
1617 | (etype,value,tb), tb_offset=tb_offset |
|
1626 | (etype,value,tb), tb_offset=tb_offset | |
1618 | ) |
|
1627 | ) | |
1619 | return stb |
|
1628 | return stb | |
1620 |
|
1629 | |||
1621 | self.CustomTB = types.MethodType(wrapped,self) |
|
1630 | self.CustomTB = types.MethodType(wrapped,self) | |
1622 | self.custom_exceptions = exc_tuple |
|
1631 | self.custom_exceptions = exc_tuple | |
1623 |
|
1632 | |||
1624 | def excepthook(self, etype, value, tb): |
|
1633 | def excepthook(self, etype, value, tb): | |
1625 | """One more defense for GUI apps that call sys.excepthook. |
|
1634 | """One more defense for GUI apps that call sys.excepthook. | |
1626 |
|
1635 | |||
1627 | GUI frameworks like wxPython trap exceptions and call |
|
1636 | GUI frameworks like wxPython trap exceptions and call | |
1628 | sys.excepthook themselves. I guess this is a feature that |
|
1637 | sys.excepthook themselves. I guess this is a feature that | |
1629 | enables them to keep running after exceptions that would |
|
1638 | enables them to keep running after exceptions that would | |
1630 | otherwise kill their mainloop. This is a bother for IPython |
|
1639 | otherwise kill their mainloop. This is a bother for IPython | |
1631 | which excepts to catch all of the program exceptions with a try: |
|
1640 | which excepts to catch all of the program exceptions with a try: | |
1632 | except: statement. |
|
1641 | except: statement. | |
1633 |
|
1642 | |||
1634 | Normally, IPython sets sys.excepthook to a CrashHandler instance, so if |
|
1643 | Normally, IPython sets sys.excepthook to a CrashHandler instance, so if | |
1635 | any app directly invokes sys.excepthook, it will look to the user like |
|
1644 | any app directly invokes sys.excepthook, it will look to the user like | |
1636 | IPython crashed. In order to work around this, we can disable the |
|
1645 | IPython crashed. In order to work around this, we can disable the | |
1637 | CrashHandler and replace it with this excepthook instead, which prints a |
|
1646 | CrashHandler and replace it with this excepthook instead, which prints a | |
1638 | regular traceback using our InteractiveTB. In this fashion, apps which |
|
1647 | regular traceback using our InteractiveTB. In this fashion, apps which | |
1639 | call sys.excepthook will generate a regular-looking exception from |
|
1648 | call sys.excepthook will generate a regular-looking exception from | |
1640 | IPython, and the CrashHandler will only be triggered by real IPython |
|
1649 | IPython, and the CrashHandler will only be triggered by real IPython | |
1641 | crashes. |
|
1650 | crashes. | |
1642 |
|
1651 | |||
1643 | This hook should be used sparingly, only in places which are not likely |
|
1652 | This hook should be used sparingly, only in places which are not likely | |
1644 | to be true IPython errors. |
|
1653 | to be true IPython errors. | |
1645 | """ |
|
1654 | """ | |
1646 | self.showtraceback((etype,value,tb),tb_offset=0) |
|
1655 | self.showtraceback((etype,value,tb),tb_offset=0) | |
1647 |
|
1656 | |||
1648 | def _get_exc_info(self, exc_tuple=None): |
|
1657 | def _get_exc_info(self, exc_tuple=None): | |
1649 | """get exc_info from a given tuple, sys.exc_info() or sys.last_type etc. |
|
1658 | """get exc_info from a given tuple, sys.exc_info() or sys.last_type etc. | |
1650 |
|
1659 | |||
1651 | Ensures sys.last_type,value,traceback hold the exc_info we found, |
|
1660 | Ensures sys.last_type,value,traceback hold the exc_info we found, | |
1652 | from whichever source. |
|
1661 | from whichever source. | |
1653 |
|
1662 | |||
1654 | raises ValueError if none of these contain any information |
|
1663 | raises ValueError if none of these contain any information | |
1655 | """ |
|
1664 | """ | |
1656 | if exc_tuple is None: |
|
1665 | if exc_tuple is None: | |
1657 | etype, value, tb = sys.exc_info() |
|
1666 | etype, value, tb = sys.exc_info() | |
1658 | else: |
|
1667 | else: | |
1659 | etype, value, tb = exc_tuple |
|
1668 | etype, value, tb = exc_tuple | |
1660 |
|
1669 | |||
1661 | if etype is None: |
|
1670 | if etype is None: | |
1662 | if hasattr(sys, 'last_type'): |
|
1671 | if hasattr(sys, 'last_type'): | |
1663 | etype, value, tb = sys.last_type, sys.last_value, \ |
|
1672 | etype, value, tb = sys.last_type, sys.last_value, \ | |
1664 | sys.last_traceback |
|
1673 | sys.last_traceback | |
1665 |
|
1674 | |||
1666 | if etype is None: |
|
1675 | if etype is None: | |
1667 | raise ValueError("No exception to find") |
|
1676 | raise ValueError("No exception to find") | |
1668 |
|
1677 | |||
1669 | # Now store the exception info in sys.last_type etc. |
|
1678 | # Now store the exception info in sys.last_type etc. | |
1670 | # WARNING: these variables are somewhat deprecated and not |
|
1679 | # WARNING: these variables are somewhat deprecated and not | |
1671 | # necessarily safe to use in a threaded environment, but tools |
|
1680 | # necessarily safe to use in a threaded environment, but tools | |
1672 | # like pdb depend on their existence, so let's set them. If we |
|
1681 | # like pdb depend on their existence, so let's set them. If we | |
1673 | # find problems in the field, we'll need to revisit their use. |
|
1682 | # find problems in the field, we'll need to revisit their use. | |
1674 | sys.last_type = etype |
|
1683 | sys.last_type = etype | |
1675 | sys.last_value = value |
|
1684 | sys.last_value = value | |
1676 | sys.last_traceback = tb |
|
1685 | sys.last_traceback = tb | |
1677 |
|
1686 | |||
1678 | return etype, value, tb |
|
1687 | return etype, value, tb | |
1679 |
|
1688 | |||
1680 | def show_usage_error(self, exc): |
|
1689 | def show_usage_error(self, exc): | |
1681 | """Show a short message for UsageErrors |
|
1690 | """Show a short message for UsageErrors | |
1682 |
|
1691 | |||
1683 | These are special exceptions that shouldn't show a traceback. |
|
1692 | These are special exceptions that shouldn't show a traceback. | |
1684 | """ |
|
1693 | """ | |
1685 | self.write_err("UsageError: %s" % exc) |
|
1694 | self.write_err("UsageError: %s" % exc) | |
1686 |
|
1695 | |||
1687 | def showtraceback(self,exc_tuple = None,filename=None,tb_offset=None, |
|
1696 | def showtraceback(self,exc_tuple = None,filename=None,tb_offset=None, | |
1688 | exception_only=False): |
|
1697 | exception_only=False): | |
1689 | """Display the exception that just occurred. |
|
1698 | """Display the exception that just occurred. | |
1690 |
|
1699 | |||
1691 | 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 | |
1692 | should be used throughout the code for presenting user tracebacks, |
|
1701 | should be used throughout the code for presenting user tracebacks, | |
1693 | rather than directly invoking the InteractiveTB object. |
|
1702 | rather than directly invoking the InteractiveTB object. | |
1694 |
|
1703 | |||
1695 | A specific showsyntaxerror() also exists, but this method can take |
|
1704 | A specific showsyntaxerror() also exists, but this method can take | |
1696 | 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 | |
1697 | SyntaxError exception, don't try to analyze the stack manually and |
|
1706 | SyntaxError exception, don't try to analyze the stack manually and | |
1698 | simply call this method.""" |
|
1707 | simply call this method.""" | |
1699 |
|
1708 | |||
1700 | try: |
|
1709 | try: | |
1701 | try: |
|
1710 | try: | |
1702 | etype, value, tb = self._get_exc_info(exc_tuple) |
|
1711 | etype, value, tb = self._get_exc_info(exc_tuple) | |
1703 | except ValueError: |
|
1712 | except ValueError: | |
1704 | self.write_err('No traceback available to show.\n') |
|
1713 | self.write_err('No traceback available to show.\n') | |
1705 | return |
|
1714 | return | |
1706 |
|
1715 | |||
1707 | if issubclass(etype, SyntaxError): |
|
1716 | if issubclass(etype, SyntaxError): | |
1708 | # 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 | |
1709 | # line, there may be SyntaxError cases with imported code. |
|
1718 | # line, there may be SyntaxError cases with imported code. | |
1710 | self.showsyntaxerror(filename) |
|
1719 | self.showsyntaxerror(filename) | |
1711 | elif etype is UsageError: |
|
1720 | elif etype is UsageError: | |
1712 | self.show_usage_error(value) |
|
1721 | self.show_usage_error(value) | |
1713 | else: |
|
1722 | else: | |
1714 | if exception_only: |
|
1723 | if exception_only: | |
1715 | stb = ['An exception has occurred, use %tb to see ' |
|
1724 | stb = ['An exception has occurred, use %tb to see ' | |
1716 | 'the full traceback.\n'] |
|
1725 | 'the full traceback.\n'] | |
1717 | stb.extend(self.InteractiveTB.get_exception_only(etype, |
|
1726 | stb.extend(self.InteractiveTB.get_exception_only(etype, | |
1718 | value)) |
|
1727 | value)) | |
1719 | else: |
|
1728 | else: | |
1720 | try: |
|
1729 | try: | |
1721 | # Exception classes can customise their traceback - we |
|
1730 | # Exception classes can customise their traceback - we | |
1722 | # use this in IPython.parallel for exceptions occurring |
|
1731 | # use this in IPython.parallel for exceptions occurring | |
1723 | # in the engines. This should return a list of strings. |
|
1732 | # in the engines. This should return a list of strings. | |
1724 | stb = value._render_traceback_() |
|
1733 | stb = value._render_traceback_() | |
1725 | except Exception: |
|
1734 | except Exception: | |
1726 | stb = self.InteractiveTB.structured_traceback(etype, |
|
1735 | stb = self.InteractiveTB.structured_traceback(etype, | |
1727 | value, tb, tb_offset=tb_offset) |
|
1736 | value, tb, tb_offset=tb_offset) | |
1728 |
|
1737 | |||
1729 | self._showtraceback(etype, value, stb) |
|
1738 | self._showtraceback(etype, value, stb) | |
1730 | if self.call_pdb: |
|
1739 | if self.call_pdb: | |
1731 | # drop into debugger |
|
1740 | # drop into debugger | |
1732 | self.debugger(force=True) |
|
1741 | self.debugger(force=True) | |
1733 | return |
|
1742 | return | |
1734 |
|
1743 | |||
1735 | # Actually show the traceback |
|
1744 | # Actually show the traceback | |
1736 | self._showtraceback(etype, value, stb) |
|
1745 | self._showtraceback(etype, value, stb) | |
1737 |
|
1746 | |||
1738 | except KeyboardInterrupt: |
|
1747 | except KeyboardInterrupt: | |
1739 | self.write_err("\nKeyboardInterrupt\n") |
|
1748 | self.write_err("\nKeyboardInterrupt\n") | |
1740 |
|
1749 | |||
1741 | def _showtraceback(self, etype, evalue, stb): |
|
1750 | def _showtraceback(self, etype, evalue, stb): | |
1742 | """Actually show a traceback. |
|
1751 | """Actually show a traceback. | |
1743 |
|
1752 | |||
1744 | 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 | |
1745 | place, like a side channel. |
|
1754 | place, like a side channel. | |
1746 | """ |
|
1755 | """ | |
1747 | print(self.InteractiveTB.stb2text(stb), file=io.stdout) |
|
1756 | print(self.InteractiveTB.stb2text(stb), file=io.stdout) | |
1748 |
|
1757 | |||
1749 | def showsyntaxerror(self, filename=None): |
|
1758 | def showsyntaxerror(self, filename=None): | |
1750 | """Display the syntax error that just occurred. |
|
1759 | """Display the syntax error that just occurred. | |
1751 |
|
1760 | |||
1752 | 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. | |
1753 |
|
1762 | |||
1754 | 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 | |
1755 | of what was there before (because Python's parser always uses |
|
1764 | of what was there before (because Python's parser always uses | |
1756 | "<string>" when reading from a string). |
|
1765 | "<string>" when reading from a string). | |
1757 | """ |
|
1766 | """ | |
1758 | etype, value, last_traceback = self._get_exc_info() |
|
1767 | etype, value, last_traceback = self._get_exc_info() | |
1759 |
|
1768 | |||
1760 | if filename and issubclass(etype, SyntaxError): |
|
1769 | if filename and issubclass(etype, SyntaxError): | |
1761 | try: |
|
1770 | try: | |
1762 | value.filename = filename |
|
1771 | value.filename = filename | |
1763 | except: |
|
1772 | except: | |
1764 | # Not the format we expect; leave it alone |
|
1773 | # Not the format we expect; leave it alone | |
1765 | pass |
|
1774 | pass | |
1766 |
|
1775 | |||
1767 | stb = self.SyntaxTB.structured_traceback(etype, value, []) |
|
1776 | stb = self.SyntaxTB.structured_traceback(etype, value, []) | |
1768 | self._showtraceback(etype, value, stb) |
|
1777 | self._showtraceback(etype, value, stb) | |
1769 |
|
1778 | |||
1770 | # This is overridden in TerminalInteractiveShell to show a message about |
|
1779 | # This is overridden in TerminalInteractiveShell to show a message about | |
1771 | # the %paste magic. |
|
1780 | # the %paste magic. | |
1772 | def showindentationerror(self): |
|
1781 | def showindentationerror(self): | |
1773 | """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 | |
1774 | at the prompt. |
|
1783 | at the prompt. | |
1775 |
|
1784 | |||
1776 | This is overridden in TerminalInteractiveShell to show a message about |
|
1785 | This is overridden in TerminalInteractiveShell to show a message about | |
1777 | the %paste magic.""" |
|
1786 | the %paste magic.""" | |
1778 | self.showsyntaxerror() |
|
1787 | self.showsyntaxerror() | |
1779 |
|
1788 | |||
1780 | #------------------------------------------------------------------------- |
|
1789 | #------------------------------------------------------------------------- | |
1781 | # Things related to readline |
|
1790 | # Things related to readline | |
1782 | #------------------------------------------------------------------------- |
|
1791 | #------------------------------------------------------------------------- | |
1783 |
|
1792 | |||
1784 | def init_readline(self): |
|
1793 | def init_readline(self): | |
1785 | """Command history completion/saving/reloading.""" |
|
1794 | """Command history completion/saving/reloading.""" | |
1786 |
|
1795 | |||
1787 | if self.readline_use: |
|
1796 | if self.readline_use: | |
1788 | import IPython.utils.rlineimpl as readline |
|
1797 | import IPython.utils.rlineimpl as readline | |
1789 |
|
1798 | |||
1790 | self.rl_next_input = None |
|
1799 | self.rl_next_input = None | |
1791 | self.rl_do_indent = False |
|
1800 | self.rl_do_indent = False | |
1792 |
|
1801 | |||
1793 | if not self.readline_use or not readline.have_readline: |
|
1802 | if not self.readline_use or not readline.have_readline: | |
1794 | self.has_readline = False |
|
1803 | self.has_readline = False | |
1795 | self.readline = None |
|
1804 | self.readline = None | |
1796 | # 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 | |
1797 | self.readline_no_record = no_op_context |
|
1806 | self.readline_no_record = no_op_context | |
1798 | self.set_readline_completer = no_op |
|
1807 | self.set_readline_completer = no_op | |
1799 | self.set_custom_completer = no_op |
|
1808 | self.set_custom_completer = no_op | |
1800 | if self.readline_use: |
|
1809 | if self.readline_use: | |
1801 | warn('Readline services not available or not loaded.') |
|
1810 | warn('Readline services not available or not loaded.') | |
1802 | else: |
|
1811 | else: | |
1803 | self.has_readline = True |
|
1812 | self.has_readline = True | |
1804 | self.readline = readline |
|
1813 | self.readline = readline | |
1805 | sys.modules['readline'] = readline |
|
1814 | sys.modules['readline'] = readline | |
1806 |
|
1815 | |||
1807 | # Platform-specific configuration |
|
1816 | # Platform-specific configuration | |
1808 | if os.name == 'nt': |
|
1817 | if os.name == 'nt': | |
1809 | # FIXME - check with Frederick to see if we can harmonize |
|
1818 | # FIXME - check with Frederick to see if we can harmonize | |
1810 | # naming conventions with pyreadline to avoid this |
|
1819 | # naming conventions with pyreadline to avoid this | |
1811 | # platform-dependent check |
|
1820 | # platform-dependent check | |
1812 | self.readline_startup_hook = readline.set_pre_input_hook |
|
1821 | self.readline_startup_hook = readline.set_pre_input_hook | |
1813 | else: |
|
1822 | else: | |
1814 | self.readline_startup_hook = readline.set_startup_hook |
|
1823 | self.readline_startup_hook = readline.set_startup_hook | |
1815 |
|
1824 | |||
1816 | # Load user's initrc file (readline config) |
|
1825 | # Load user's initrc file (readline config) | |
1817 | # Or if libedit is used, load editrc. |
|
1826 | # Or if libedit is used, load editrc. | |
1818 | inputrc_name = os.environ.get('INPUTRC') |
|
1827 | inputrc_name = os.environ.get('INPUTRC') | |
1819 | if inputrc_name is None: |
|
1828 | if inputrc_name is None: | |
1820 | inputrc_name = '.inputrc' |
|
1829 | inputrc_name = '.inputrc' | |
1821 | if readline.uses_libedit: |
|
1830 | if readline.uses_libedit: | |
1822 | inputrc_name = '.editrc' |
|
1831 | inputrc_name = '.editrc' | |
1823 | inputrc_name = os.path.join(self.home_dir, inputrc_name) |
|
1832 | inputrc_name = os.path.join(self.home_dir, inputrc_name) | |
1824 | if os.path.isfile(inputrc_name): |
|
1833 | if os.path.isfile(inputrc_name): | |
1825 | try: |
|
1834 | try: | |
1826 | readline.read_init_file(inputrc_name) |
|
1835 | readline.read_init_file(inputrc_name) | |
1827 | except: |
|
1836 | except: | |
1828 | warn('Problems reading readline initialization file <%s>' |
|
1837 | warn('Problems reading readline initialization file <%s>' | |
1829 | % inputrc_name) |
|
1838 | % inputrc_name) | |
1830 |
|
1839 | |||
1831 | # Configure readline according to user's prefs |
|
1840 | # Configure readline according to user's prefs | |
1832 | # 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 | |
1833 | # is being used (as on Leopard) the readline config is |
|
1842 | # is being used (as on Leopard) the readline config is | |
1834 | # not run as the syntax for libedit is different. |
|
1843 | # not run as the syntax for libedit is different. | |
1835 | if not readline.uses_libedit: |
|
1844 | if not readline.uses_libedit: | |
1836 | for rlcommand in self.readline_parse_and_bind: |
|
1845 | for rlcommand in self.readline_parse_and_bind: | |
1837 | #print "loading rl:",rlcommand # dbg |
|
1846 | #print "loading rl:",rlcommand # dbg | |
1838 | readline.parse_and_bind(rlcommand) |
|
1847 | readline.parse_and_bind(rlcommand) | |
1839 |
|
1848 | |||
1840 | # Remove some chars from the delimiters list. If we encounter |
|
1849 | # Remove some chars from the delimiters list. If we encounter | |
1841 | # unicode chars, discard them. |
|
1850 | # unicode chars, discard them. | |
1842 | delims = readline.get_completer_delims() |
|
1851 | delims = readline.get_completer_delims() | |
1843 | if not py3compat.PY3: |
|
1852 | if not py3compat.PY3: | |
1844 | delims = delims.encode("ascii", "ignore") |
|
1853 | delims = delims.encode("ascii", "ignore") | |
1845 | for d in self.readline_remove_delims: |
|
1854 | for d in self.readline_remove_delims: | |
1846 | delims = delims.replace(d, "") |
|
1855 | delims = delims.replace(d, "") | |
1847 | delims = delims.replace(ESC_MAGIC, '') |
|
1856 | delims = delims.replace(ESC_MAGIC, '') | |
1848 | readline.set_completer_delims(delims) |
|
1857 | readline.set_completer_delims(delims) | |
1849 | # Store these so we can restore them if something like rpy2 modifies |
|
1858 | # Store these so we can restore them if something like rpy2 modifies | |
1850 | # them. |
|
1859 | # them. | |
1851 | self.readline_delims = delims |
|
1860 | self.readline_delims = delims | |
1852 | # otherwise we end up with a monster history after a while: |
|
1861 | # otherwise we end up with a monster history after a while: | |
1853 | readline.set_history_length(self.history_length) |
|
1862 | readline.set_history_length(self.history_length) | |
1854 |
|
1863 | |||
1855 | self.refill_readline_hist() |
|
1864 | self.refill_readline_hist() | |
1856 | self.readline_no_record = ReadlineNoRecord(self) |
|
1865 | self.readline_no_record = ReadlineNoRecord(self) | |
1857 |
|
1866 | |||
1858 | # Configure auto-indent for all platforms |
|
1867 | # Configure auto-indent for all platforms | |
1859 | self.set_autoindent(self.autoindent) |
|
1868 | self.set_autoindent(self.autoindent) | |
1860 |
|
1869 | |||
1861 | def refill_readline_hist(self): |
|
1870 | def refill_readline_hist(self): | |
1862 | # Load the last 1000 lines from history |
|
1871 | # Load the last 1000 lines from history | |
1863 | self.readline.clear_history() |
|
1872 | self.readline.clear_history() | |
1864 | stdin_encoding = sys.stdin.encoding or "utf-8" |
|
1873 | stdin_encoding = sys.stdin.encoding or "utf-8" | |
1865 | last_cell = u"" |
|
1874 | last_cell = u"" | |
1866 | for _, _, cell in self.history_manager.get_tail(1000, |
|
1875 | for _, _, cell in self.history_manager.get_tail(1000, | |
1867 | include_latest=True): |
|
1876 | include_latest=True): | |
1868 | # Ignore blank lines and consecutive duplicates |
|
1877 | # Ignore blank lines and consecutive duplicates | |
1869 | cell = cell.rstrip() |
|
1878 | cell = cell.rstrip() | |
1870 | if cell and (cell != last_cell): |
|
1879 | if cell and (cell != last_cell): | |
1871 | try: |
|
1880 | try: | |
1872 | if self.multiline_history: |
|
1881 | if self.multiline_history: | |
1873 | self.readline.add_history(py3compat.unicode_to_str(cell, |
|
1882 | self.readline.add_history(py3compat.unicode_to_str(cell, | |
1874 | stdin_encoding)) |
|
1883 | stdin_encoding)) | |
1875 | else: |
|
1884 | else: | |
1876 | for line in cell.splitlines(): |
|
1885 | for line in cell.splitlines(): | |
1877 | self.readline.add_history(py3compat.unicode_to_str(line, |
|
1886 | self.readline.add_history(py3compat.unicode_to_str(line, | |
1878 | stdin_encoding)) |
|
1887 | stdin_encoding)) | |
1879 | last_cell = cell |
|
1888 | last_cell = cell | |
1880 |
|
1889 | |||
1881 | except TypeError: |
|
1890 | except TypeError: | |
1882 | # The history DB can get corrupted so it returns strings |
|
1891 | # The history DB can get corrupted so it returns strings | |
1883 | # containing null bytes, which readline objects to. |
|
1892 | # containing null bytes, which readline objects to. | |
1884 | continue |
|
1893 | continue | |
1885 |
|
1894 | |||
1886 | @skip_doctest |
|
1895 | @skip_doctest | |
1887 | def set_next_input(self, s): |
|
1896 | def set_next_input(self, s): | |
1888 | """ Sets the 'default' input string for the next command line. |
|
1897 | """ Sets the 'default' input string for the next command line. | |
1889 |
|
1898 | |||
1890 | Requires readline. |
|
1899 | Requires readline. | |
1891 |
|
1900 | |||
1892 | Example:: |
|
1901 | Example:: | |
1893 |
|
1902 | |||
1894 | In [1]: _ip.set_next_input("Hello Word") |
|
1903 | In [1]: _ip.set_next_input("Hello Word") | |
1895 | In [2]: Hello Word_ # cursor is here |
|
1904 | In [2]: Hello Word_ # cursor is here | |
1896 | """ |
|
1905 | """ | |
1897 | self.rl_next_input = py3compat.cast_bytes_py2(s) |
|
1906 | self.rl_next_input = py3compat.cast_bytes_py2(s) | |
1898 |
|
1907 | |||
1899 | # Maybe move this to the terminal subclass? |
|
1908 | # Maybe move this to the terminal subclass? | |
1900 | def pre_readline(self): |
|
1909 | def pre_readline(self): | |
1901 | """readline hook to be used at the start of each line. |
|
1910 | """readline hook to be used at the start of each line. | |
1902 |
|
1911 | |||
1903 | Currently it handles auto-indent only.""" |
|
1912 | Currently it handles auto-indent only.""" | |
1904 |
|
1913 | |||
1905 | if self.rl_do_indent: |
|
1914 | if self.rl_do_indent: | |
1906 | self.readline.insert_text(self._indent_current_str()) |
|
1915 | self.readline.insert_text(self._indent_current_str()) | |
1907 | if self.rl_next_input is not None: |
|
1916 | if self.rl_next_input is not None: | |
1908 | self.readline.insert_text(self.rl_next_input) |
|
1917 | self.readline.insert_text(self.rl_next_input) | |
1909 | self.rl_next_input = None |
|
1918 | self.rl_next_input = None | |
1910 |
|
1919 | |||
1911 | def _indent_current_str(self): |
|
1920 | def _indent_current_str(self): | |
1912 | """return the current level of indentation as a string""" |
|
1921 | """return the current level of indentation as a string""" | |
1913 | return self.input_splitter.indent_spaces * ' ' |
|
1922 | return self.input_splitter.indent_spaces * ' ' | |
1914 |
|
1923 | |||
1915 | #------------------------------------------------------------------------- |
|
1924 | #------------------------------------------------------------------------- | |
1916 | # Things related to text completion |
|
1925 | # Things related to text completion | |
1917 | #------------------------------------------------------------------------- |
|
1926 | #------------------------------------------------------------------------- | |
1918 |
|
1927 | |||
1919 | def init_completer(self): |
|
1928 | def init_completer(self): | |
1920 | """Initialize the completion machinery. |
|
1929 | """Initialize the completion machinery. | |
1921 |
|
1930 | |||
1922 | This creates completion machinery that can be used by client code, |
|
1931 | This creates completion machinery that can be used by client code, | |
1923 | either interactively in-process (typically triggered by the readline |
|
1932 | either interactively in-process (typically triggered by the readline | |
1924 | library), programatically (such as in test suites) or out-of-prcess |
|
1933 | library), programatically (such as in test suites) or out-of-prcess | |
1925 | (typically over the network by remote frontends). |
|
1934 | (typically over the network by remote frontends). | |
1926 | """ |
|
1935 | """ | |
1927 | from IPython.core.completer import IPCompleter |
|
1936 | from IPython.core.completer import IPCompleter | |
1928 | from IPython.core.completerlib import (module_completer, |
|
1937 | from IPython.core.completerlib import (module_completer, | |
1929 | magic_run_completer, cd_completer, reset_completer) |
|
1938 | magic_run_completer, cd_completer, reset_completer) | |
1930 |
|
1939 | |||
1931 | self.Completer = IPCompleter(shell=self, |
|
1940 | self.Completer = IPCompleter(shell=self, | |
1932 | namespace=self.user_ns, |
|
1941 | namespace=self.user_ns, | |
1933 | global_namespace=self.user_global_ns, |
|
1942 | global_namespace=self.user_global_ns, | |
1934 | alias_table=self.alias_manager.alias_table, |
|
1943 | alias_table=self.alias_manager.alias_table, | |
1935 | use_readline=self.has_readline, |
|
1944 | use_readline=self.has_readline, | |
1936 | parent=self, |
|
1945 | parent=self, | |
1937 | ) |
|
1946 | ) | |
1938 | self.configurables.append(self.Completer) |
|
1947 | self.configurables.append(self.Completer) | |
1939 |
|
1948 | |||
1940 | # Add custom completers to the basic ones built into IPCompleter |
|
1949 | # Add custom completers to the basic ones built into IPCompleter | |
1941 | sdisp = self.strdispatchers.get('complete_command', StrDispatch()) |
|
1950 | sdisp = self.strdispatchers.get('complete_command', StrDispatch()) | |
1942 | self.strdispatchers['complete_command'] = sdisp |
|
1951 | self.strdispatchers['complete_command'] = sdisp | |
1943 | self.Completer.custom_completers = sdisp |
|
1952 | self.Completer.custom_completers = sdisp | |
1944 |
|
1953 | |||
1945 | self.set_hook('complete_command', module_completer, str_key = 'import') |
|
1954 | self.set_hook('complete_command', module_completer, str_key = 'import') | |
1946 | self.set_hook('complete_command', module_completer, str_key = 'from') |
|
1955 | self.set_hook('complete_command', module_completer, str_key = 'from') | |
1947 | self.set_hook('complete_command', magic_run_completer, str_key = '%run') |
|
1956 | self.set_hook('complete_command', magic_run_completer, str_key = '%run') | |
1948 | self.set_hook('complete_command', cd_completer, str_key = '%cd') |
|
1957 | self.set_hook('complete_command', cd_completer, str_key = '%cd') | |
1949 | self.set_hook('complete_command', reset_completer, str_key = '%reset') |
|
1958 | self.set_hook('complete_command', reset_completer, str_key = '%reset') | |
1950 |
|
1959 | |||
1951 | # Only configure readline if we truly are using readline. IPython can |
|
1960 | # Only configure readline if we truly are using readline. IPython can | |
1952 | # do tab-completion over the network, in GUIs, etc, where readline |
|
1961 | # do tab-completion over the network, in GUIs, etc, where readline | |
1953 | # itself may be absent |
|
1962 | # itself may be absent | |
1954 | if self.has_readline: |
|
1963 | if self.has_readline: | |
1955 | self.set_readline_completer() |
|
1964 | self.set_readline_completer() | |
1956 |
|
1965 | |||
1957 | def complete(self, text, line=None, cursor_pos=None): |
|
1966 | def complete(self, text, line=None, cursor_pos=None): | |
1958 | """Return the completed text and a list of completions. |
|
1967 | """Return the completed text and a list of completions. | |
1959 |
|
1968 | |||
1960 | Parameters |
|
1969 | Parameters | |
1961 | ---------- |
|
1970 | ---------- | |
1962 |
|
1971 | |||
1963 | text : string |
|
1972 | text : string | |
1964 | A string of text to be completed on. It can be given as empty and |
|
1973 | A string of text to be completed on. It can be given as empty and | |
1965 | instead a line/position pair are given. In this case, the |
|
1974 | instead a line/position pair are given. In this case, the | |
1966 | completer itself will split the line like readline does. |
|
1975 | completer itself will split the line like readline does. | |
1967 |
|
1976 | |||
1968 | line : string, optional |
|
1977 | line : string, optional | |
1969 | The complete line that text is part of. |
|
1978 | The complete line that text is part of. | |
1970 |
|
1979 | |||
1971 | cursor_pos : int, optional |
|
1980 | cursor_pos : int, optional | |
1972 | The position of the cursor on the input line. |
|
1981 | The position of the cursor on the input line. | |
1973 |
|
1982 | |||
1974 | Returns |
|
1983 | Returns | |
1975 | ------- |
|
1984 | ------- | |
1976 | text : string |
|
1985 | text : string | |
1977 | The actual text that was completed. |
|
1986 | The actual text that was completed. | |
1978 |
|
1987 | |||
1979 | matches : list |
|
1988 | matches : list | |
1980 | A sorted list with all possible completions. |
|
1989 | A sorted list with all possible completions. | |
1981 |
|
1990 | |||
1982 | The optional arguments allow the completion to take more context into |
|
1991 | The optional arguments allow the completion to take more context into | |
1983 | account, and are part of the low-level completion API. |
|
1992 | account, and are part of the low-level completion API. | |
1984 |
|
1993 | |||
1985 | This is a wrapper around the completion mechanism, similar to what |
|
1994 | This is a wrapper around the completion mechanism, similar to what | |
1986 | readline does at the command line when the TAB key is hit. By |
|
1995 | readline does at the command line when the TAB key is hit. By | |
1987 | exposing it as a method, it can be used by other non-readline |
|
1996 | exposing it as a method, it can be used by other non-readline | |
1988 | environments (such as GUIs) for text completion. |
|
1997 | environments (such as GUIs) for text completion. | |
1989 |
|
1998 | |||
1990 | Simple usage example: |
|
1999 | Simple usage example: | |
1991 |
|
2000 | |||
1992 | In [1]: x = 'hello' |
|
2001 | In [1]: x = 'hello' | |
1993 |
|
2002 | |||
1994 | In [2]: _ip.complete('x.l') |
|
2003 | In [2]: _ip.complete('x.l') | |
1995 | Out[2]: ('x.l', ['x.ljust', 'x.lower', 'x.lstrip']) |
|
2004 | Out[2]: ('x.l', ['x.ljust', 'x.lower', 'x.lstrip']) | |
1996 | """ |
|
2005 | """ | |
1997 |
|
2006 | |||
1998 | # Inject names into __builtin__ so we can complete on the added names. |
|
2007 | # Inject names into __builtin__ so we can complete on the added names. | |
1999 | with self.builtin_trap: |
|
2008 | with self.builtin_trap: | |
2000 | return self.Completer.complete(text, line, cursor_pos) |
|
2009 | return self.Completer.complete(text, line, cursor_pos) | |
2001 |
|
2010 | |||
2002 | def set_custom_completer(self, completer, pos=0): |
|
2011 | def set_custom_completer(self, completer, pos=0): | |
2003 | """Adds a new custom completer function. |
|
2012 | """Adds a new custom completer function. | |
2004 |
|
2013 | |||
2005 | The position argument (defaults to 0) is the index in the completers |
|
2014 | The position argument (defaults to 0) is the index in the completers | |
2006 | list where you want the completer to be inserted.""" |
|
2015 | list where you want the completer to be inserted.""" | |
2007 |
|
2016 | |||
2008 | newcomp = types.MethodType(completer,self.Completer) |
|
2017 | newcomp = types.MethodType(completer,self.Completer) | |
2009 | self.Completer.matchers.insert(pos,newcomp) |
|
2018 | self.Completer.matchers.insert(pos,newcomp) | |
2010 |
|
2019 | |||
2011 | def set_readline_completer(self): |
|
2020 | def set_readline_completer(self): | |
2012 | """Reset readline's completer to be our own.""" |
|
2021 | """Reset readline's completer to be our own.""" | |
2013 | self.readline.set_completer(self.Completer.rlcomplete) |
|
2022 | self.readline.set_completer(self.Completer.rlcomplete) | |
2014 |
|
2023 | |||
2015 | def set_completer_frame(self, frame=None): |
|
2024 | def set_completer_frame(self, frame=None): | |
2016 | """Set the frame of the completer.""" |
|
2025 | """Set the frame of the completer.""" | |
2017 | if frame: |
|
2026 | if frame: | |
2018 | self.Completer.namespace = frame.f_locals |
|
2027 | self.Completer.namespace = frame.f_locals | |
2019 | self.Completer.global_namespace = frame.f_globals |
|
2028 | self.Completer.global_namespace = frame.f_globals | |
2020 | else: |
|
2029 | else: | |
2021 | self.Completer.namespace = self.user_ns |
|
2030 | self.Completer.namespace = self.user_ns | |
2022 | self.Completer.global_namespace = self.user_global_ns |
|
2031 | self.Completer.global_namespace = self.user_global_ns | |
2023 |
|
2032 | |||
2024 | #------------------------------------------------------------------------- |
|
2033 | #------------------------------------------------------------------------- | |
2025 | # Things related to magics |
|
2034 | # Things related to magics | |
2026 | #------------------------------------------------------------------------- |
|
2035 | #------------------------------------------------------------------------- | |
2027 |
|
2036 | |||
2028 | def init_magics(self): |
|
2037 | def init_magics(self): | |
2029 | from IPython.core import magics as m |
|
2038 | from IPython.core import magics as m | |
2030 | self.magics_manager = magic.MagicsManager(shell=self, |
|
2039 | self.magics_manager = magic.MagicsManager(shell=self, | |
2031 | parent=self, |
|
2040 | parent=self, | |
2032 | user_magics=m.UserMagics(self)) |
|
2041 | user_magics=m.UserMagics(self)) | |
2033 | self.configurables.append(self.magics_manager) |
|
2042 | self.configurables.append(self.magics_manager) | |
2034 |
|
2043 | |||
2035 | # Expose as public API from the magics manager |
|
2044 | # Expose as public API from the magics manager | |
2036 | self.register_magics = self.magics_manager.register |
|
2045 | self.register_magics = self.magics_manager.register | |
2037 | self.define_magic = self.magics_manager.define_magic |
|
2046 | self.define_magic = self.magics_manager.define_magic | |
2038 |
|
2047 | |||
2039 | self.register_magics(m.AutoMagics, m.BasicMagics, m.CodeMagics, |
|
2048 | self.register_magics(m.AutoMagics, m.BasicMagics, m.CodeMagics, | |
2040 | m.ConfigMagics, m.DeprecatedMagics, m.DisplayMagics, m.ExecutionMagics, |
|
2049 | m.ConfigMagics, m.DeprecatedMagics, m.DisplayMagics, m.ExecutionMagics, | |
2041 | m.ExtensionMagics, m.HistoryMagics, m.LoggingMagics, |
|
2050 | m.ExtensionMagics, m.HistoryMagics, m.LoggingMagics, | |
2042 | m.NamespaceMagics, m.OSMagics, m.PylabMagics, m.ScriptMagics, |
|
2051 | m.NamespaceMagics, m.OSMagics, m.PylabMagics, m.ScriptMagics, | |
2043 | ) |
|
2052 | ) | |
2044 |
|
2053 | |||
2045 | # Register Magic Aliases |
|
2054 | # Register Magic Aliases | |
2046 | mman = self.magics_manager |
|
2055 | mman = self.magics_manager | |
2047 | # FIXME: magic aliases should be defined by the Magics classes |
|
2056 | # FIXME: magic aliases should be defined by the Magics classes | |
2048 | # or in MagicsManager, not here |
|
2057 | # or in MagicsManager, not here | |
2049 | mman.register_alias('ed', 'edit') |
|
2058 | mman.register_alias('ed', 'edit') | |
2050 | mman.register_alias('hist', 'history') |
|
2059 | mman.register_alias('hist', 'history') | |
2051 | mman.register_alias('rep', 'recall') |
|
2060 | mman.register_alias('rep', 'recall') | |
2052 | mman.register_alias('SVG', 'svg', 'cell') |
|
2061 | mman.register_alias('SVG', 'svg', 'cell') | |
2053 | mman.register_alias('HTML', 'html', 'cell') |
|
2062 | mman.register_alias('HTML', 'html', 'cell') | |
2054 | mman.register_alias('file', 'writefile', 'cell') |
|
2063 | mman.register_alias('file', 'writefile', 'cell') | |
2055 |
|
2064 | |||
2056 | # FIXME: Move the color initialization to the DisplayHook, which |
|
2065 | # FIXME: Move the color initialization to the DisplayHook, which | |
2057 | # should be split into a prompt manager and displayhook. We probably |
|
2066 | # should be split into a prompt manager and displayhook. We probably | |
2058 | # even need a centralize colors management object. |
|
2067 | # even need a centralize colors management object. | |
2059 | self.magic('colors %s' % self.colors) |
|
2068 | self.magic('colors %s' % self.colors) | |
2060 |
|
2069 | |||
2061 | # Defined here so that it's included in the documentation |
|
2070 | # Defined here so that it's included in the documentation | |
2062 | @functools.wraps(magic.MagicsManager.register_function) |
|
2071 | @functools.wraps(magic.MagicsManager.register_function) | |
2063 | def register_magic_function(self, func, magic_kind='line', magic_name=None): |
|
2072 | def register_magic_function(self, func, magic_kind='line', magic_name=None): | |
2064 | self.magics_manager.register_function(func, |
|
2073 | self.magics_manager.register_function(func, | |
2065 | magic_kind=magic_kind, magic_name=magic_name) |
|
2074 | magic_kind=magic_kind, magic_name=magic_name) | |
2066 |
|
2075 | |||
2067 | def run_line_magic(self, magic_name, line): |
|
2076 | def run_line_magic(self, magic_name, line): | |
2068 | """Execute the given line magic. |
|
2077 | """Execute the given line magic. | |
2069 |
|
2078 | |||
2070 | Parameters |
|
2079 | Parameters | |
2071 | ---------- |
|
2080 | ---------- | |
2072 | magic_name : str |
|
2081 | magic_name : str | |
2073 | Name of the desired magic function, without '%' prefix. |
|
2082 | Name of the desired magic function, without '%' prefix. | |
2074 |
|
2083 | |||
2075 | line : str |
|
2084 | line : str | |
2076 | The rest of the input line as a single string. |
|
2085 | The rest of the input line as a single string. | |
2077 | """ |
|
2086 | """ | |
2078 | fn = self.find_line_magic(magic_name) |
|
2087 | fn = self.find_line_magic(magic_name) | |
2079 | if fn is None: |
|
2088 | if fn is None: | |
2080 | cm = self.find_cell_magic(magic_name) |
|
2089 | cm = self.find_cell_magic(magic_name) | |
2081 | etpl = "Line magic function `%%%s` not found%s." |
|
2090 | etpl = "Line magic function `%%%s` not found%s." | |
2082 | extra = '' if cm is None else (' (But cell magic `%%%%%s` exists, ' |
|
2091 | extra = '' if cm is None else (' (But cell magic `%%%%%s` exists, ' | |
2083 | 'did you mean that instead?)' % magic_name ) |
|
2092 | 'did you mean that instead?)' % magic_name ) | |
2084 | error(etpl % (magic_name, extra)) |
|
2093 | error(etpl % (magic_name, extra)) | |
2085 | else: |
|
2094 | else: | |
2086 | # Note: this is the distance in the stack to the user's frame. |
|
2095 | # Note: this is the distance in the stack to the user's frame. | |
2087 | # This will need to be updated if the internal calling logic gets |
|
2096 | # This will need to be updated if the internal calling logic gets | |
2088 | # refactored, or else we'll be expanding the wrong variables. |
|
2097 | # refactored, or else we'll be expanding the wrong variables. | |
2089 | stack_depth = 2 |
|
2098 | stack_depth = 2 | |
2090 | magic_arg_s = self.var_expand(line, stack_depth) |
|
2099 | magic_arg_s = self.var_expand(line, stack_depth) | |
2091 | # Put magic args in a list so we can call with f(*a) syntax |
|
2100 | # Put magic args in a list so we can call with f(*a) syntax | |
2092 | args = [magic_arg_s] |
|
2101 | args = [magic_arg_s] | |
2093 | kwargs = {} |
|
2102 | kwargs = {} | |
2094 | # Grab local namespace if we need it: |
|
2103 | # Grab local namespace if we need it: | |
2095 | if getattr(fn, "needs_local_scope", False): |
|
2104 | if getattr(fn, "needs_local_scope", False): | |
2096 | kwargs['local_ns'] = sys._getframe(stack_depth).f_locals |
|
2105 | kwargs['local_ns'] = sys._getframe(stack_depth).f_locals | |
2097 | with self.builtin_trap: |
|
2106 | with self.builtin_trap: | |
2098 | result = fn(*args,**kwargs) |
|
2107 | result = fn(*args,**kwargs) | |
2099 | return result |
|
2108 | return result | |
2100 |
|
2109 | |||
2101 | def run_cell_magic(self, magic_name, line, cell): |
|
2110 | def run_cell_magic(self, magic_name, line, cell): | |
2102 | """Execute the given cell magic. |
|
2111 | """Execute the given cell magic. | |
2103 |
|
2112 | |||
2104 | Parameters |
|
2113 | Parameters | |
2105 | ---------- |
|
2114 | ---------- | |
2106 | magic_name : str |
|
2115 | magic_name : str | |
2107 | Name of the desired magic function, without '%' prefix. |
|
2116 | Name of the desired magic function, without '%' prefix. | |
2108 |
|
2117 | |||
2109 | line : str |
|
2118 | line : str | |
2110 | The rest of the first input line as a single string. |
|
2119 | The rest of the first input line as a single string. | |
2111 |
|
2120 | |||
2112 | cell : str |
|
2121 | cell : str | |
2113 | The body of the cell as a (possibly multiline) string. |
|
2122 | The body of the cell as a (possibly multiline) string. | |
2114 | """ |
|
2123 | """ | |
2115 | fn = self.find_cell_magic(magic_name) |
|
2124 | fn = self.find_cell_magic(magic_name) | |
2116 | if fn is None: |
|
2125 | if fn is None: | |
2117 | lm = self.find_line_magic(magic_name) |
|
2126 | lm = self.find_line_magic(magic_name) | |
2118 | etpl = "Cell magic `%%{0}` not found{1}." |
|
2127 | etpl = "Cell magic `%%{0}` not found{1}." | |
2119 | extra = '' if lm is None else (' (But line magic `%{0}` exists, ' |
|
2128 | extra = '' if lm is None else (' (But line magic `%{0}` exists, ' | |
2120 | 'did you mean that instead?)'.format(magic_name)) |
|
2129 | 'did you mean that instead?)'.format(magic_name)) | |
2121 | error(etpl.format(magic_name, extra)) |
|
2130 | error(etpl.format(magic_name, extra)) | |
2122 | elif cell == '': |
|
2131 | elif cell == '': | |
2123 | message = '%%{0} is a cell magic, but the cell body is empty.'.format(magic_name) |
|
2132 | message = '%%{0} is a cell magic, but the cell body is empty.'.format(magic_name) | |
2124 | if self.find_line_magic(magic_name) is not None: |
|
2133 | if self.find_line_magic(magic_name) is not None: | |
2125 | message += ' Did you mean the line magic %{0} (single %)?'.format(magic_name) |
|
2134 | message += ' Did you mean the line magic %{0} (single %)?'.format(magic_name) | |
2126 | raise UsageError(message) |
|
2135 | raise UsageError(message) | |
2127 | else: |
|
2136 | else: | |
2128 | # Note: this is the distance in the stack to the user's frame. |
|
2137 | # Note: this is the distance in the stack to the user's frame. | |
2129 | # This will need to be updated if the internal calling logic gets |
|
2138 | # This will need to be updated if the internal calling logic gets | |
2130 | # refactored, or else we'll be expanding the wrong variables. |
|
2139 | # refactored, or else we'll be expanding the wrong variables. | |
2131 | stack_depth = 2 |
|
2140 | stack_depth = 2 | |
2132 | magic_arg_s = self.var_expand(line, stack_depth) |
|
2141 | magic_arg_s = self.var_expand(line, stack_depth) | |
2133 | with self.builtin_trap: |
|
2142 | with self.builtin_trap: | |
2134 | result = fn(magic_arg_s, cell) |
|
2143 | result = fn(magic_arg_s, cell) | |
2135 | return result |
|
2144 | return result | |
2136 |
|
2145 | |||
2137 | def find_line_magic(self, magic_name): |
|
2146 | def find_line_magic(self, magic_name): | |
2138 | """Find and return a line magic by name. |
|
2147 | """Find and return a line magic by name. | |
2139 |
|
2148 | |||
2140 | Returns None if the magic isn't found.""" |
|
2149 | Returns None if the magic isn't found.""" | |
2141 | return self.magics_manager.magics['line'].get(magic_name) |
|
2150 | return self.magics_manager.magics['line'].get(magic_name) | |
2142 |
|
2151 | |||
2143 | def find_cell_magic(self, magic_name): |
|
2152 | def find_cell_magic(self, magic_name): | |
2144 | """Find and return a cell magic by name. |
|
2153 | """Find and return a cell magic by name. | |
2145 |
|
2154 | |||
2146 | Returns None if the magic isn't found.""" |
|
2155 | Returns None if the magic isn't found.""" | |
2147 | return self.magics_manager.magics['cell'].get(magic_name) |
|
2156 | return self.magics_manager.magics['cell'].get(magic_name) | |
2148 |
|
2157 | |||
2149 | def find_magic(self, magic_name, magic_kind='line'): |
|
2158 | def find_magic(self, magic_name, magic_kind='line'): | |
2150 | """Find and return a magic of the given type by name. |
|
2159 | """Find and return a magic of the given type by name. | |
2151 |
|
2160 | |||
2152 | Returns None if the magic isn't found.""" |
|
2161 | Returns None if the magic isn't found.""" | |
2153 | return self.magics_manager.magics[magic_kind].get(magic_name) |
|
2162 | return self.magics_manager.magics[magic_kind].get(magic_name) | |
2154 |
|
2163 | |||
2155 | def magic(self, arg_s): |
|
2164 | def magic(self, arg_s): | |
2156 | """DEPRECATED. Use run_line_magic() instead. |
|
2165 | """DEPRECATED. Use run_line_magic() instead. | |
2157 |
|
2166 | |||
2158 | Call a magic function by name. |
|
2167 | Call a magic function by name. | |
2159 |
|
2168 | |||
2160 | Input: a string containing the name of the magic function to call and |
|
2169 | Input: a string containing the name of the magic function to call and | |
2161 | any additional arguments to be passed to the magic. |
|
2170 | any additional arguments to be passed to the magic. | |
2162 |
|
2171 | |||
2163 | magic('name -opt foo bar') is equivalent to typing at the ipython |
|
2172 | magic('name -opt foo bar') is equivalent to typing at the ipython | |
2164 | prompt: |
|
2173 | prompt: | |
2165 |
|
2174 | |||
2166 | In[1]: %name -opt foo bar |
|
2175 | In[1]: %name -opt foo bar | |
2167 |
|
2176 | |||
2168 | To call a magic without arguments, simply use magic('name'). |
|
2177 | To call a magic without arguments, simply use magic('name'). | |
2169 |
|
2178 | |||
2170 | This provides a proper Python function to call IPython's magics in any |
|
2179 | This provides a proper Python function to call IPython's magics in any | |
2171 | valid Python code you can type at the interpreter, including loops and |
|
2180 | valid Python code you can type at the interpreter, including loops and | |
2172 | compound statements. |
|
2181 | compound statements. | |
2173 | """ |
|
2182 | """ | |
2174 | # TODO: should we issue a loud deprecation warning here? |
|
2183 | # TODO: should we issue a loud deprecation warning here? | |
2175 | magic_name, _, magic_arg_s = arg_s.partition(' ') |
|
2184 | magic_name, _, magic_arg_s = arg_s.partition(' ') | |
2176 | magic_name = magic_name.lstrip(prefilter.ESC_MAGIC) |
|
2185 | magic_name = magic_name.lstrip(prefilter.ESC_MAGIC) | |
2177 | return self.run_line_magic(magic_name, magic_arg_s) |
|
2186 | return self.run_line_magic(magic_name, magic_arg_s) | |
2178 |
|
2187 | |||
2179 | #------------------------------------------------------------------------- |
|
2188 | #------------------------------------------------------------------------- | |
2180 | # Things related to macros |
|
2189 | # Things related to macros | |
2181 | #------------------------------------------------------------------------- |
|
2190 | #------------------------------------------------------------------------- | |
2182 |
|
2191 | |||
2183 | def define_macro(self, name, themacro): |
|
2192 | def define_macro(self, name, themacro): | |
2184 | """Define a new macro |
|
2193 | """Define a new macro | |
2185 |
|
2194 | |||
2186 | Parameters |
|
2195 | Parameters | |
2187 | ---------- |
|
2196 | ---------- | |
2188 | name : str |
|
2197 | name : str | |
2189 | The name of the macro. |
|
2198 | The name of the macro. | |
2190 | themacro : str or Macro |
|
2199 | themacro : str or Macro | |
2191 | The action to do upon invoking the macro. If a string, a new |
|
2200 | The action to do upon invoking the macro. If a string, a new | |
2192 | Macro object is created by passing the string to it. |
|
2201 | Macro object is created by passing the string to it. | |
2193 | """ |
|
2202 | """ | |
2194 |
|
2203 | |||
2195 | from IPython.core import macro |
|
2204 | from IPython.core import macro | |
2196 |
|
2205 | |||
2197 | if isinstance(themacro, basestring): |
|
2206 | if isinstance(themacro, basestring): | |
2198 | themacro = macro.Macro(themacro) |
|
2207 | themacro = macro.Macro(themacro) | |
2199 | if not isinstance(themacro, macro.Macro): |
|
2208 | if not isinstance(themacro, macro.Macro): | |
2200 | raise ValueError('A macro must be a string or a Macro instance.') |
|
2209 | raise ValueError('A macro must be a string or a Macro instance.') | |
2201 | self.user_ns[name] = themacro |
|
2210 | self.user_ns[name] = themacro | |
2202 |
|
2211 | |||
2203 | #------------------------------------------------------------------------- |
|
2212 | #------------------------------------------------------------------------- | |
2204 | # Things related to the running of system commands |
|
2213 | # Things related to the running of system commands | |
2205 | #------------------------------------------------------------------------- |
|
2214 | #------------------------------------------------------------------------- | |
2206 |
|
2215 | |||
2207 | def system_piped(self, cmd): |
|
2216 | def system_piped(self, cmd): | |
2208 | """Call the given cmd in a subprocess, piping stdout/err |
|
2217 | """Call the given cmd in a subprocess, piping stdout/err | |
2209 |
|
2218 | |||
2210 | Parameters |
|
2219 | Parameters | |
2211 | ---------- |
|
2220 | ---------- | |
2212 | cmd : str |
|
2221 | cmd : str | |
2213 | Command to execute (can not end in '&', as background processes are |
|
2222 | Command to execute (can not end in '&', as background processes are | |
2214 | not supported. Should not be a command that expects input |
|
2223 | not supported. Should not be a command that expects input | |
2215 | other than simple text. |
|
2224 | other than simple text. | |
2216 | """ |
|
2225 | """ | |
2217 | if cmd.rstrip().endswith('&'): |
|
2226 | if cmd.rstrip().endswith('&'): | |
2218 | # this is *far* from a rigorous test |
|
2227 | # this is *far* from a rigorous test | |
2219 | # We do not support backgrounding processes because we either use |
|
2228 | # We do not support backgrounding processes because we either use | |
2220 | # pexpect or pipes to read from. Users can always just call |
|
2229 | # pexpect or pipes to read from. Users can always just call | |
2221 | # os.system() or use ip.system=ip.system_raw |
|
2230 | # os.system() or use ip.system=ip.system_raw | |
2222 | # if they really want a background process. |
|
2231 | # if they really want a background process. | |
2223 | raise OSError("Background processes not supported.") |
|
2232 | raise OSError("Background processes not supported.") | |
2224 |
|
2233 | |||
2225 | # we explicitly do NOT return the subprocess status code, because |
|
2234 | # we explicitly do NOT return the subprocess status code, because | |
2226 | # a non-None value would trigger :func:`sys.displayhook` calls. |
|
2235 | # a non-None value would trigger :func:`sys.displayhook` calls. | |
2227 | # Instead, we store the exit_code in user_ns. |
|
2236 | # Instead, we store the exit_code in user_ns. | |
2228 | self.user_ns['_exit_code'] = system(self.var_expand(cmd, depth=1)) |
|
2237 | self.user_ns['_exit_code'] = system(self.var_expand(cmd, depth=1)) | |
2229 |
|
2238 | |||
2230 | def system_raw(self, cmd): |
|
2239 | def system_raw(self, cmd): | |
2231 | """Call the given cmd in a subprocess using os.system |
|
2240 | """Call the given cmd in a subprocess using os.system | |
2232 |
|
2241 | |||
2233 | Parameters |
|
2242 | Parameters | |
2234 | ---------- |
|
2243 | ---------- | |
2235 | cmd : str |
|
2244 | cmd : str | |
2236 | Command to execute. |
|
2245 | Command to execute. | |
2237 | """ |
|
2246 | """ | |
2238 | cmd = self.var_expand(cmd, depth=1) |
|
2247 | cmd = self.var_expand(cmd, depth=1) | |
2239 | # protect os.system from UNC paths on Windows, which it can't handle: |
|
2248 | # protect os.system from UNC paths on Windows, which it can't handle: | |
2240 | if sys.platform == 'win32': |
|
2249 | if sys.platform == 'win32': | |
2241 | from IPython.utils._process_win32 import AvoidUNCPath |
|
2250 | from IPython.utils._process_win32 import AvoidUNCPath | |
2242 | with AvoidUNCPath() as path: |
|
2251 | with AvoidUNCPath() as path: | |
2243 | if path is not None: |
|
2252 | if path is not None: | |
2244 | cmd = '"pushd %s &&"%s' % (path, cmd) |
|
2253 | cmd = '"pushd %s &&"%s' % (path, cmd) | |
2245 | cmd = py3compat.unicode_to_str(cmd) |
|
2254 | cmd = py3compat.unicode_to_str(cmd) | |
2246 | ec = os.system(cmd) |
|
2255 | ec = os.system(cmd) | |
2247 | else: |
|
2256 | else: | |
2248 | cmd = py3compat.unicode_to_str(cmd) |
|
2257 | cmd = py3compat.unicode_to_str(cmd) | |
2249 | ec = os.system(cmd) |
|
2258 | ec = os.system(cmd) | |
2250 | # The high byte is the exit code, the low byte is a signal number |
|
2259 | # The high byte is the exit code, the low byte is a signal number | |
2251 | # that we discard for now. See the docs for os.wait() |
|
2260 | # that we discard for now. See the docs for os.wait() | |
2252 | if ec > 255: |
|
2261 | if ec > 255: | |
2253 | ec >>= 8 |
|
2262 | ec >>= 8 | |
2254 |
|
2263 | |||
2255 | # We explicitly do NOT return the subprocess status code, because |
|
2264 | # We explicitly do NOT return the subprocess status code, because | |
2256 | # a non-None value would trigger :func:`sys.displayhook` calls. |
|
2265 | # a non-None value would trigger :func:`sys.displayhook` calls. | |
2257 | # Instead, we store the exit_code in user_ns. |
|
2266 | # Instead, we store the exit_code in user_ns. | |
2258 | self.user_ns['_exit_code'] = ec |
|
2267 | self.user_ns['_exit_code'] = ec | |
2259 |
|
2268 | |||
2260 | # use piped system by default, because it is better behaved |
|
2269 | # use piped system by default, because it is better behaved | |
2261 | system = system_piped |
|
2270 | system = system_piped | |
2262 |
|
2271 | |||
2263 | def getoutput(self, cmd, split=True, depth=0): |
|
2272 | def getoutput(self, cmd, split=True, depth=0): | |
2264 | """Get output (possibly including stderr) from a subprocess. |
|
2273 | """Get output (possibly including stderr) from a subprocess. | |
2265 |
|
2274 | |||
2266 | Parameters |
|
2275 | Parameters | |
2267 | ---------- |
|
2276 | ---------- | |
2268 | cmd : str |
|
2277 | cmd : str | |
2269 | Command to execute (can not end in '&', as background processes are |
|
2278 | Command to execute (can not end in '&', as background processes are | |
2270 | not supported. |
|
2279 | not supported. | |
2271 | split : bool, optional |
|
2280 | split : bool, optional | |
2272 | If True, split the output into an IPython SList. Otherwise, an |
|
2281 | If True, split the output into an IPython SList. Otherwise, an | |
2273 | IPython LSString is returned. These are objects similar to normal |
|
2282 | IPython LSString is returned. These are objects similar to normal | |
2274 | lists and strings, with a few convenience attributes for easier |
|
2283 | lists and strings, with a few convenience attributes for easier | |
2275 | manipulation of line-based output. You can use '?' on them for |
|
2284 | manipulation of line-based output. You can use '?' on them for | |
2276 | details. |
|
2285 | details. | |
2277 | depth : int, optional |
|
2286 | depth : int, optional | |
2278 | How many frames above the caller are the local variables which should |
|
2287 | How many frames above the caller are the local variables which should | |
2279 | be expanded in the command string? The default (0) assumes that the |
|
2288 | be expanded in the command string? The default (0) assumes that the | |
2280 | expansion variables are in the stack frame calling this function. |
|
2289 | expansion variables are in the stack frame calling this function. | |
2281 | """ |
|
2290 | """ | |
2282 | if cmd.rstrip().endswith('&'): |
|
2291 | if cmd.rstrip().endswith('&'): | |
2283 | # this is *far* from a rigorous test |
|
2292 | # this is *far* from a rigorous test | |
2284 | raise OSError("Background processes not supported.") |
|
2293 | raise OSError("Background processes not supported.") | |
2285 | out = getoutput(self.var_expand(cmd, depth=depth+1)) |
|
2294 | out = getoutput(self.var_expand(cmd, depth=depth+1)) | |
2286 | if split: |
|
2295 | if split: | |
2287 | out = SList(out.splitlines()) |
|
2296 | out = SList(out.splitlines()) | |
2288 | else: |
|
2297 | else: | |
2289 | out = LSString(out) |
|
2298 | out = LSString(out) | |
2290 | return out |
|
2299 | return out | |
2291 |
|
2300 | |||
2292 | #------------------------------------------------------------------------- |
|
2301 | #------------------------------------------------------------------------- | |
2293 | # Things related to aliases |
|
2302 | # Things related to aliases | |
2294 | #------------------------------------------------------------------------- |
|
2303 | #------------------------------------------------------------------------- | |
2295 |
|
2304 | |||
2296 | def init_alias(self): |
|
2305 | def init_alias(self): | |
2297 | self.alias_manager = AliasManager(shell=self, parent=self) |
|
2306 | self.alias_manager = AliasManager(shell=self, parent=self) | |
2298 | self.configurables.append(self.alias_manager) |
|
2307 | self.configurables.append(self.alias_manager) | |
2299 | self.ns_table['alias'] = self.alias_manager.alias_table, |
|
2308 | self.ns_table['alias'] = self.alias_manager.alias_table, | |
2300 |
|
2309 | |||
2301 | #------------------------------------------------------------------------- |
|
2310 | #------------------------------------------------------------------------- | |
2302 | # Things related to extensions |
|
2311 | # Things related to extensions | |
2303 | #------------------------------------------------------------------------- |
|
2312 | #------------------------------------------------------------------------- | |
2304 |
|
2313 | |||
2305 | def init_extension_manager(self): |
|
2314 | def init_extension_manager(self): | |
2306 | self.extension_manager = ExtensionManager(shell=self, parent=self) |
|
2315 | self.extension_manager = ExtensionManager(shell=self, parent=self) | |
2307 | self.configurables.append(self.extension_manager) |
|
2316 | self.configurables.append(self.extension_manager) | |
2308 |
|
2317 | |||
2309 | #------------------------------------------------------------------------- |
|
2318 | #------------------------------------------------------------------------- | |
2310 | # Things related to payloads |
|
2319 | # Things related to payloads | |
2311 | #------------------------------------------------------------------------- |
|
2320 | #------------------------------------------------------------------------- | |
2312 |
|
2321 | |||
2313 | def init_payload(self): |
|
2322 | def init_payload(self): | |
2314 | self.payload_manager = PayloadManager(parent=self) |
|
2323 | self.payload_manager = PayloadManager(parent=self) | |
2315 | self.configurables.append(self.payload_manager) |
|
2324 | self.configurables.append(self.payload_manager) | |
2316 |
|
2325 | |||
2317 | #------------------------------------------------------------------------- |
|
2326 | #------------------------------------------------------------------------- | |
2318 | # Things related to the prefilter |
|
2327 | # Things related to the prefilter | |
2319 | #------------------------------------------------------------------------- |
|
2328 | #------------------------------------------------------------------------- | |
2320 |
|
2329 | |||
2321 | def init_prefilter(self): |
|
2330 | def init_prefilter(self): | |
2322 | self.prefilter_manager = PrefilterManager(shell=self, parent=self) |
|
2331 | self.prefilter_manager = PrefilterManager(shell=self, parent=self) | |
2323 | self.configurables.append(self.prefilter_manager) |
|
2332 | self.configurables.append(self.prefilter_manager) | |
2324 | # Ultimately this will be refactored in the new interpreter code, but |
|
2333 | # Ultimately this will be refactored in the new interpreter code, but | |
2325 | # for now, we should expose the main prefilter method (there's legacy |
|
2334 | # for now, we should expose the main prefilter method (there's legacy | |
2326 | # code out there that may rely on this). |
|
2335 | # code out there that may rely on this). | |
2327 | self.prefilter = self.prefilter_manager.prefilter_lines |
|
2336 | self.prefilter = self.prefilter_manager.prefilter_lines | |
2328 |
|
2337 | |||
2329 | def auto_rewrite_input(self, cmd): |
|
2338 | def auto_rewrite_input(self, cmd): | |
2330 | """Print to the screen the rewritten form of the user's command. |
|
2339 | """Print to the screen the rewritten form of the user's command. | |
2331 |
|
2340 | |||
2332 | This shows visual feedback by rewriting input lines that cause |
|
2341 | This shows visual feedback by rewriting input lines that cause | |
2333 | automatic calling to kick in, like:: |
|
2342 | automatic calling to kick in, like:: | |
2334 |
|
2343 | |||
2335 | /f x |
|
2344 | /f x | |
2336 |
|
2345 | |||
2337 | into:: |
|
2346 | into:: | |
2338 |
|
2347 | |||
2339 | ------> f(x) |
|
2348 | ------> f(x) | |
2340 |
|
2349 | |||
2341 | after the user's input prompt. This helps the user understand that the |
|
2350 | after the user's input prompt. This helps the user understand that the | |
2342 | input line was transformed automatically by IPython. |
|
2351 | input line was transformed automatically by IPython. | |
2343 | """ |
|
2352 | """ | |
2344 | if not self.show_rewritten_input: |
|
2353 | if not self.show_rewritten_input: | |
2345 | return |
|
2354 | return | |
2346 |
|
2355 | |||
2347 | rw = self.prompt_manager.render('rewrite') + cmd |
|
2356 | rw = self.prompt_manager.render('rewrite') + cmd | |
2348 |
|
2357 | |||
2349 | try: |
|
2358 | try: | |
2350 | # plain ascii works better w/ pyreadline, on some machines, so |
|
2359 | # plain ascii works better w/ pyreadline, on some machines, so | |
2351 | # we use it and only print uncolored rewrite if we have unicode |
|
2360 | # we use it and only print uncolored rewrite if we have unicode | |
2352 | rw = str(rw) |
|
2361 | rw = str(rw) | |
2353 | print(rw, file=io.stdout) |
|
2362 | print(rw, file=io.stdout) | |
2354 | except UnicodeEncodeError: |
|
2363 | except UnicodeEncodeError: | |
2355 | print("------> " + cmd) |
|
2364 | print("------> " + cmd) | |
2356 |
|
2365 | |||
2357 | #------------------------------------------------------------------------- |
|
2366 | #------------------------------------------------------------------------- | |
2358 | # Things related to extracting values/expressions from kernel and user_ns |
|
2367 | # Things related to extracting values/expressions from kernel and user_ns | |
2359 | #------------------------------------------------------------------------- |
|
2368 | #------------------------------------------------------------------------- | |
2360 |
|
2369 | |||
2361 | def _user_obj_error(self): |
|
2370 | def _user_obj_error(self): | |
2362 | """return simple exception dict |
|
2371 | """return simple exception dict | |
2363 |
|
2372 | |||
2364 | for use in user_variables / expressions |
|
2373 | for use in user_variables / expressions | |
2365 | """ |
|
2374 | """ | |
2366 |
|
2375 | |||
2367 | etype, evalue, tb = self._get_exc_info() |
|
2376 | etype, evalue, tb = self._get_exc_info() | |
2368 | stb = self.InteractiveTB.get_exception_only(etype, evalue) |
|
2377 | stb = self.InteractiveTB.get_exception_only(etype, evalue) | |
2369 |
|
2378 | |||
2370 | exc_info = { |
|
2379 | exc_info = { | |
2371 | u'status' : 'error', |
|
2380 | u'status' : 'error', | |
2372 | u'traceback' : stb, |
|
2381 | u'traceback' : stb, | |
2373 | u'ename' : unicode(etype.__name__), |
|
2382 | u'ename' : unicode(etype.__name__), | |
2374 | u'evalue' : py3compat.safe_unicode(evalue), |
|
2383 | u'evalue' : py3compat.safe_unicode(evalue), | |
2375 | } |
|
2384 | } | |
2376 |
|
2385 | |||
2377 | return exc_info |
|
2386 | return exc_info | |
2378 |
|
2387 | |||
2379 | def _format_user_obj(self, obj): |
|
2388 | def _format_user_obj(self, obj): | |
2380 | """format a user object to display dict |
|
2389 | """format a user object to display dict | |
2381 |
|
2390 | |||
2382 | for use in user_expressions / variables |
|
2391 | for use in user_expressions / variables | |
2383 | """ |
|
2392 | """ | |
2384 |
|
2393 | |||
2385 | data, md = self.display_formatter.format(obj) |
|
2394 | data, md = self.display_formatter.format(obj) | |
2386 | value = { |
|
2395 | value = { | |
2387 | 'status' : 'ok', |
|
2396 | 'status' : 'ok', | |
2388 | 'data' : data, |
|
2397 | 'data' : data, | |
2389 | 'metadata' : md, |
|
2398 | 'metadata' : md, | |
2390 | } |
|
2399 | } | |
2391 | return value |
|
2400 | return value | |
2392 |
|
2401 | |||
2393 | def user_variables(self, names): |
|
2402 | def user_variables(self, names): | |
2394 | """Get a list of variable names from the user's namespace. |
|
2403 | """Get a list of variable names from the user's namespace. | |
2395 |
|
2404 | |||
2396 | Parameters |
|
2405 | Parameters | |
2397 | ---------- |
|
2406 | ---------- | |
2398 | names : list of strings |
|
2407 | names : list of strings | |
2399 | A list of names of variables to be read from the user namespace. |
|
2408 | A list of names of variables to be read from the user namespace. | |
2400 |
|
2409 | |||
2401 | Returns |
|
2410 | Returns | |
2402 | ------- |
|
2411 | ------- | |
2403 | A dict, keyed by the input names and with the rich mime-type repr(s) of each value. |
|
2412 | A dict, keyed by the input names and with the rich mime-type repr(s) of each value. | |
2404 | Each element will be a sub-dict of the same form as a display_data message. |
|
2413 | Each element will be a sub-dict of the same form as a display_data message. | |
2405 | """ |
|
2414 | """ | |
2406 | out = {} |
|
2415 | out = {} | |
2407 | user_ns = self.user_ns |
|
2416 | user_ns = self.user_ns | |
2408 |
|
2417 | |||
2409 | for varname in names: |
|
2418 | for varname in names: | |
2410 | try: |
|
2419 | try: | |
2411 | value = self._format_user_obj(user_ns[varname]) |
|
2420 | value = self._format_user_obj(user_ns[varname]) | |
2412 | except: |
|
2421 | except: | |
2413 | value = self._user_obj_error() |
|
2422 | value = self._user_obj_error() | |
2414 | out[varname] = value |
|
2423 | out[varname] = value | |
2415 | return out |
|
2424 | return out | |
2416 |
|
2425 | |||
2417 | def user_expressions(self, expressions): |
|
2426 | def user_expressions(self, expressions): | |
2418 | """Evaluate a dict of expressions in the user's namespace. |
|
2427 | """Evaluate a dict of expressions in the user's namespace. | |
2419 |
|
2428 | |||
2420 | Parameters |
|
2429 | Parameters | |
2421 | ---------- |
|
2430 | ---------- | |
2422 | expressions : dict |
|
2431 | expressions : dict | |
2423 | A dict with string keys and string values. The expression values |
|
2432 | A dict with string keys and string values. The expression values | |
2424 | should be valid Python expressions, each of which will be evaluated |
|
2433 | should be valid Python expressions, each of which will be evaluated | |
2425 | in the user namespace. |
|
2434 | in the user namespace. | |
2426 |
|
2435 | |||
2427 | Returns |
|
2436 | Returns | |
2428 | ------- |
|
2437 | ------- | |
2429 | A dict, keyed like the input expressions dict, with the rich mime-typed |
|
2438 | A dict, keyed like the input expressions dict, with the rich mime-typed | |
2430 | display_data of each value. |
|
2439 | display_data of each value. | |
2431 | """ |
|
2440 | """ | |
2432 | out = {} |
|
2441 | out = {} | |
2433 | user_ns = self.user_ns |
|
2442 | user_ns = self.user_ns | |
2434 | global_ns = self.user_global_ns |
|
2443 | global_ns = self.user_global_ns | |
2435 |
|
2444 | |||
2436 | for key, expr in expressions.iteritems(): |
|
2445 | for key, expr in expressions.iteritems(): | |
2437 | try: |
|
2446 | try: | |
2438 | value = self._format_user_obj(eval(expr, global_ns, user_ns)) |
|
2447 | value = self._format_user_obj(eval(expr, global_ns, user_ns)) | |
2439 | except: |
|
2448 | except: | |
2440 | value = self._user_obj_error() |
|
2449 | value = self._user_obj_error() | |
2441 | out[key] = value |
|
2450 | out[key] = value | |
2442 | return out |
|
2451 | return out | |
2443 |
|
2452 | |||
2444 | #------------------------------------------------------------------------- |
|
2453 | #------------------------------------------------------------------------- | |
2445 | # Things related to the running of code |
|
2454 | # Things related to the running of code | |
2446 | #------------------------------------------------------------------------- |
|
2455 | #------------------------------------------------------------------------- | |
2447 |
|
2456 | |||
2448 | def ex(self, cmd): |
|
2457 | def ex(self, cmd): | |
2449 | """Execute a normal python statement in user namespace.""" |
|
2458 | """Execute a normal python statement in user namespace.""" | |
2450 | with self.builtin_trap: |
|
2459 | with self.builtin_trap: | |
2451 | exec cmd in self.user_global_ns, self.user_ns |
|
2460 | exec cmd in self.user_global_ns, self.user_ns | |
2452 |
|
2461 | |||
2453 | def ev(self, expr): |
|
2462 | def ev(self, expr): | |
2454 | """Evaluate python expression expr in user namespace. |
|
2463 | """Evaluate python expression expr in user namespace. | |
2455 |
|
2464 | |||
2456 | Returns the result of evaluation |
|
2465 | Returns the result of evaluation | |
2457 | """ |
|
2466 | """ | |
2458 | with self.builtin_trap: |
|
2467 | with self.builtin_trap: | |
2459 | return eval(expr, self.user_global_ns, self.user_ns) |
|
2468 | return eval(expr, self.user_global_ns, self.user_ns) | |
2460 |
|
2469 | |||
2461 | def safe_execfile(self, fname, *where, **kw): |
|
2470 | def safe_execfile(self, fname, *where, **kw): | |
2462 | """A safe version of the builtin execfile(). |
|
2471 | """A safe version of the builtin execfile(). | |
2463 |
|
2472 | |||
2464 | This version will never throw an exception, but instead print |
|
2473 | This version will never throw an exception, but instead print | |
2465 | helpful error messages to the screen. This only works on pure |
|
2474 | helpful error messages to the screen. This only works on pure | |
2466 | Python files with the .py extension. |
|
2475 | Python files with the .py extension. | |
2467 |
|
2476 | |||
2468 | Parameters |
|
2477 | Parameters | |
2469 | ---------- |
|
2478 | ---------- | |
2470 | fname : string |
|
2479 | fname : string | |
2471 | The name of the file to be executed. |
|
2480 | The name of the file to be executed. | |
2472 | where : tuple |
|
2481 | where : tuple | |
2473 | One or two namespaces, passed to execfile() as (globals,locals). |
|
2482 | One or two namespaces, passed to execfile() as (globals,locals). | |
2474 | If only one is given, it is passed as both. |
|
2483 | If only one is given, it is passed as both. | |
2475 | exit_ignore : bool (False) |
|
2484 | exit_ignore : bool (False) | |
2476 | If True, then silence SystemExit for non-zero status (it is always |
|
2485 | If True, then silence SystemExit for non-zero status (it is always | |
2477 | silenced for zero status, as it is so common). |
|
2486 | silenced for zero status, as it is so common). | |
2478 | raise_exceptions : bool (False) |
|
2487 | raise_exceptions : bool (False) | |
2479 | If True raise exceptions everywhere. Meant for testing. |
|
2488 | If True raise exceptions everywhere. Meant for testing. | |
2480 |
|
2489 | |||
2481 | """ |
|
2490 | """ | |
2482 | kw.setdefault('exit_ignore', False) |
|
2491 | kw.setdefault('exit_ignore', False) | |
2483 | kw.setdefault('raise_exceptions', False) |
|
2492 | kw.setdefault('raise_exceptions', False) | |
2484 |
|
2493 | |||
2485 | fname = os.path.abspath(os.path.expanduser(fname)) |
|
2494 | fname = os.path.abspath(os.path.expanduser(fname)) | |
2486 |
|
2495 | |||
2487 | # Make sure we can open the file |
|
2496 | # Make sure we can open the file | |
2488 | try: |
|
2497 | try: | |
2489 | with open(fname) as thefile: |
|
2498 | with open(fname) as thefile: | |
2490 | pass |
|
2499 | pass | |
2491 | except: |
|
2500 | except: | |
2492 | warn('Could not open file <%s> for safe execution.' % fname) |
|
2501 | warn('Could not open file <%s> for safe execution.' % fname) | |
2493 | return |
|
2502 | return | |
2494 |
|
2503 | |||
2495 | # Find things also in current directory. This is needed to mimic the |
|
2504 | # Find things also in current directory. This is needed to mimic the | |
2496 | # behavior of running a script from the system command line, where |
|
2505 | # behavior of running a script from the system command line, where | |
2497 | # Python inserts the script's directory into sys.path |
|
2506 | # Python inserts the script's directory into sys.path | |
2498 | dname = os.path.dirname(fname) |
|
2507 | dname = os.path.dirname(fname) | |
2499 |
|
2508 | |||
2500 | with prepended_to_syspath(dname): |
|
2509 | with prepended_to_syspath(dname): | |
2501 | try: |
|
2510 | try: | |
2502 | py3compat.execfile(fname,*where) |
|
2511 | py3compat.execfile(fname,*where) | |
2503 | except SystemExit as status: |
|
2512 | except SystemExit as status: | |
2504 | # If the call was made with 0 or None exit status (sys.exit(0) |
|
2513 | # If the call was made with 0 or None exit status (sys.exit(0) | |
2505 | # or sys.exit() ), don't bother showing a traceback, as both of |
|
2514 | # or sys.exit() ), don't bother showing a traceback, as both of | |
2506 | # these are considered normal by the OS: |
|
2515 | # these are considered normal by the OS: | |
2507 | # > python -c'import sys;sys.exit(0)'; echo $? |
|
2516 | # > python -c'import sys;sys.exit(0)'; echo $? | |
2508 | # 0 |
|
2517 | # 0 | |
2509 | # > python -c'import sys;sys.exit()'; echo $? |
|
2518 | # > python -c'import sys;sys.exit()'; echo $? | |
2510 | # 0 |
|
2519 | # 0 | |
2511 | # For other exit status, we show the exception unless |
|
2520 | # For other exit status, we show the exception unless | |
2512 | # explicitly silenced, but only in short form. |
|
2521 | # explicitly silenced, but only in short form. | |
2513 | if kw['raise_exceptions']: |
|
2522 | if kw['raise_exceptions']: | |
2514 | raise |
|
2523 | raise | |
2515 | if status.code and not kw['exit_ignore']: |
|
2524 | if status.code and not kw['exit_ignore']: | |
2516 | self.showtraceback(exception_only=True) |
|
2525 | self.showtraceback(exception_only=True) | |
2517 | except: |
|
2526 | except: | |
2518 | if kw['raise_exceptions']: |
|
2527 | if kw['raise_exceptions']: | |
2519 | raise |
|
2528 | raise | |
2520 | self.showtraceback() |
|
2529 | self.showtraceback() | |
2521 |
|
2530 | |||
2522 | def safe_execfile_ipy(self, fname): |
|
2531 | def safe_execfile_ipy(self, fname): | |
2523 | """Like safe_execfile, but for .ipy files with IPython syntax. |
|
2532 | """Like safe_execfile, but for .ipy files with IPython syntax. | |
2524 |
|
2533 | |||
2525 | Parameters |
|
2534 | Parameters | |
2526 | ---------- |
|
2535 | ---------- | |
2527 | fname : str |
|
2536 | fname : str | |
2528 | The name of the file to execute. The filename must have a |
|
2537 | The name of the file to execute. The filename must have a | |
2529 | .ipy extension. |
|
2538 | .ipy extension. | |
2530 | """ |
|
2539 | """ | |
2531 | fname = os.path.abspath(os.path.expanduser(fname)) |
|
2540 | fname = os.path.abspath(os.path.expanduser(fname)) | |
2532 |
|
2541 | |||
2533 | # Make sure we can open the file |
|
2542 | # Make sure we can open the file | |
2534 | try: |
|
2543 | try: | |
2535 | with open(fname) as thefile: |
|
2544 | with open(fname) as thefile: | |
2536 | pass |
|
2545 | pass | |
2537 | except: |
|
2546 | except: | |
2538 | warn('Could not open file <%s> for safe execution.' % fname) |
|
2547 | warn('Could not open file <%s> for safe execution.' % fname) | |
2539 | return |
|
2548 | return | |
2540 |
|
2549 | |||
2541 | # Find things also in current directory. This is needed to mimic the |
|
2550 | # Find things also in current directory. This is needed to mimic the | |
2542 | # behavior of running a script from the system command line, where |
|
2551 | # behavior of running a script from the system command line, where | |
2543 | # Python inserts the script's directory into sys.path |
|
2552 | # Python inserts the script's directory into sys.path | |
2544 | dname = os.path.dirname(fname) |
|
2553 | dname = os.path.dirname(fname) | |
2545 |
|
2554 | |||
2546 | with prepended_to_syspath(dname): |
|
2555 | with prepended_to_syspath(dname): | |
2547 | try: |
|
2556 | try: | |
2548 | with open(fname) as thefile: |
|
2557 | with open(fname) as thefile: | |
2549 | # self.run_cell currently captures all exceptions |
|
2558 | # self.run_cell currently captures all exceptions | |
2550 | # raised in user code. It would be nice if there were |
|
2559 | # raised in user code. It would be nice if there were | |
2551 | # versions of runlines, execfile that did raise, so |
|
2560 | # versions of runlines, execfile that did raise, so | |
2552 | # we could catch the errors. |
|
2561 | # we could catch the errors. | |
2553 | self.run_cell(thefile.read(), store_history=False, shell_futures=False) |
|
2562 | self.run_cell(thefile.read(), store_history=False, shell_futures=False) | |
2554 | except: |
|
2563 | except: | |
2555 | self.showtraceback() |
|
2564 | self.showtraceback() | |
2556 | warn('Unknown failure executing file: <%s>' % fname) |
|
2565 | warn('Unknown failure executing file: <%s>' % fname) | |
2557 |
|
2566 | |||
2558 | def safe_run_module(self, mod_name, where): |
|
2567 | def safe_run_module(self, mod_name, where): | |
2559 | """A safe version of runpy.run_module(). |
|
2568 | """A safe version of runpy.run_module(). | |
2560 |
|
2569 | |||
2561 | This version will never throw an exception, but instead print |
|
2570 | This version will never throw an exception, but instead print | |
2562 | helpful error messages to the screen. |
|
2571 | helpful error messages to the screen. | |
2563 |
|
2572 | |||
2564 | `SystemExit` exceptions with status code 0 or None are ignored. |
|
2573 | `SystemExit` exceptions with status code 0 or None are ignored. | |
2565 |
|
2574 | |||
2566 | Parameters |
|
2575 | Parameters | |
2567 | ---------- |
|
2576 | ---------- | |
2568 | mod_name : string |
|
2577 | mod_name : string | |
2569 | The name of the module to be executed. |
|
2578 | The name of the module to be executed. | |
2570 | where : dict |
|
2579 | where : dict | |
2571 | The globals namespace. |
|
2580 | The globals namespace. | |
2572 | """ |
|
2581 | """ | |
2573 | try: |
|
2582 | try: | |
2574 | try: |
|
2583 | try: | |
2575 | where.update( |
|
2584 | where.update( | |
2576 | runpy.run_module(str(mod_name), run_name="__main__", |
|
2585 | runpy.run_module(str(mod_name), run_name="__main__", | |
2577 | alter_sys=True) |
|
2586 | alter_sys=True) | |
2578 | ) |
|
2587 | ) | |
2579 | except SystemExit as status: |
|
2588 | except SystemExit as status: | |
2580 | if status.code: |
|
2589 | if status.code: | |
2581 | raise |
|
2590 | raise | |
2582 | except: |
|
2591 | except: | |
2583 | self.showtraceback() |
|
2592 | self.showtraceback() | |
2584 | warn('Unknown failure executing module: <%s>' % mod_name) |
|
2593 | warn('Unknown failure executing module: <%s>' % mod_name) | |
2585 |
|
2594 | |||
2586 | def _run_cached_cell_magic(self, magic_name, line): |
|
2595 | def _run_cached_cell_magic(self, magic_name, line): | |
2587 | """Special method to call a cell magic with the data stored in self. |
|
2596 | """Special method to call a cell magic with the data stored in self. | |
2588 | """ |
|
2597 | """ | |
2589 | cell = self._current_cell_magic_body |
|
2598 | cell = self._current_cell_magic_body | |
2590 | self._current_cell_magic_body = None |
|
2599 | self._current_cell_magic_body = None | |
2591 | return self.run_cell_magic(magic_name, line, cell) |
|
2600 | return self.run_cell_magic(magic_name, line, cell) | |
2592 |
|
2601 | |||
2593 | def run_cell(self, raw_cell, store_history=False, silent=False, shell_futures=True): |
|
2602 | def run_cell(self, raw_cell, store_history=False, silent=False, shell_futures=True): | |
2594 | """Run a complete IPython cell. |
|
2603 | """Run a complete IPython cell. | |
2595 |
|
2604 | |||
2596 | Parameters |
|
2605 | Parameters | |
2597 | ---------- |
|
2606 | ---------- | |
2598 | raw_cell : str |
|
2607 | raw_cell : str | |
2599 | The code (including IPython code such as %magic functions) to run. |
|
2608 | The code (including IPython code such as %magic functions) to run. | |
2600 | store_history : bool |
|
2609 | store_history : bool | |
2601 | If True, the raw and translated cell will be stored in IPython's |
|
2610 | If True, the raw and translated cell will be stored in IPython's | |
2602 | history. For user code calling back into IPython's machinery, this |
|
2611 | history. For user code calling back into IPython's machinery, this | |
2603 | should be set to False. |
|
2612 | should be set to False. | |
2604 | silent : bool |
|
2613 | silent : bool | |
2605 | If True, avoid side-effects, such as implicit displayhooks and |
|
2614 | If True, avoid side-effects, such as implicit displayhooks and | |
2606 | and logging. silent=True forces store_history=False. |
|
2615 | and logging. silent=True forces store_history=False. | |
2607 | shell_futures : bool |
|
2616 | shell_futures : bool | |
2608 | If True, the code will share future statements with the interactive |
|
2617 | If True, the code will share future statements with the interactive | |
2609 | shell. It will both be affected by previous __future__ imports, and |
|
2618 | shell. It will both be affected by previous __future__ imports, and | |
2610 | any __future__ imports in the code will affect the shell. If False, |
|
2619 | any __future__ imports in the code will affect the shell. If False, | |
2611 | __future__ imports are not shared in either direction. |
|
2620 | __future__ imports are not shared in either direction. | |
2612 | """ |
|
2621 | """ | |
2613 | if (not raw_cell) or raw_cell.isspace(): |
|
2622 | if (not raw_cell) or raw_cell.isspace(): | |
2614 | return |
|
2623 | return | |
2615 |
|
2624 | |||
2616 | if silent: |
|
2625 | if silent: | |
2617 | store_history = False |
|
2626 | store_history = False | |
2618 |
|
2627 | |||
2619 | self.input_transformer_manager.push(raw_cell) |
|
2628 | self.input_transformer_manager.push(raw_cell) | |
2620 | cell = self.input_transformer_manager.source_reset() |
|
2629 | cell = self.input_transformer_manager.source_reset() | |
2621 |
|
2630 | |||
2622 | # Our own compiler remembers the __future__ environment. If we want to |
|
2631 | # Our own compiler remembers the __future__ environment. If we want to | |
2623 | # run code with a separate __future__ environment, use the default |
|
2632 | # run code with a separate __future__ environment, use the default | |
2624 | # compiler |
|
2633 | # compiler | |
2625 | compiler = self.compile if shell_futures else CachingCompiler() |
|
2634 | compiler = self.compile if shell_futures else CachingCompiler() | |
2626 |
|
2635 | |||
2627 | with self.builtin_trap: |
|
2636 | with self.builtin_trap: | |
2628 | prefilter_failed = False |
|
2637 | prefilter_failed = False | |
2629 | if len(cell.splitlines()) == 1: |
|
2638 | if len(cell.splitlines()) == 1: | |
2630 | try: |
|
2639 | try: | |
2631 | # use prefilter_lines to handle trailing newlines |
|
2640 | # use prefilter_lines to handle trailing newlines | |
2632 | # restore trailing newline for ast.parse |
|
2641 | # restore trailing newline for ast.parse | |
2633 | cell = self.prefilter_manager.prefilter_lines(cell) + '\n' |
|
2642 | cell = self.prefilter_manager.prefilter_lines(cell) + '\n' | |
2634 | except AliasError as e: |
|
2643 | except AliasError as e: | |
2635 | error(e) |
|
2644 | error(e) | |
2636 | prefilter_failed = True |
|
2645 | prefilter_failed = True | |
2637 | except Exception: |
|
2646 | except Exception: | |
2638 | # don't allow prefilter errors to crash IPython |
|
2647 | # don't allow prefilter errors to crash IPython | |
2639 | self.showtraceback() |
|
2648 | self.showtraceback() | |
2640 | prefilter_failed = True |
|
2649 | prefilter_failed = True | |
2641 |
|
2650 | |||
2642 | # Store raw and processed history |
|
2651 | # Store raw and processed history | |
2643 | if store_history: |
|
2652 | if store_history: | |
2644 | self.history_manager.store_inputs(self.execution_count, |
|
2653 | self.history_manager.store_inputs(self.execution_count, | |
2645 | cell, raw_cell) |
|
2654 | cell, raw_cell) | |
2646 | if not silent: |
|
2655 | if not silent: | |
2647 | self.logger.log(cell, raw_cell) |
|
2656 | self.logger.log(cell, raw_cell) | |
2648 |
|
2657 | |||
2649 | if not prefilter_failed: |
|
2658 | if not prefilter_failed: | |
2650 | # don't run if prefilter failed |
|
2659 | # don't run if prefilter failed | |
2651 | cell_name = self.compile.cache(cell, self.execution_count) |
|
2660 | cell_name = self.compile.cache(cell, self.execution_count) | |
2652 |
|
2661 | |||
2653 | with self.display_trap: |
|
2662 | with self.display_trap: | |
2654 | try: |
|
2663 | try: | |
2655 | code_ast = compiler.ast_parse(cell, filename=cell_name) |
|
2664 | code_ast = compiler.ast_parse(cell, filename=cell_name) | |
2656 | except IndentationError: |
|
2665 | except IndentationError: | |
2657 | self.showindentationerror() |
|
2666 | self.showindentationerror() | |
2658 | if store_history: |
|
2667 | if store_history: | |
2659 | self.execution_count += 1 |
|
2668 | self.execution_count += 1 | |
2660 | return None |
|
2669 | return None | |
2661 | except (OverflowError, SyntaxError, ValueError, TypeError, |
|
2670 | except (OverflowError, SyntaxError, ValueError, TypeError, | |
2662 | MemoryError): |
|
2671 | MemoryError): | |
2663 | self.showsyntaxerror() |
|
2672 | self.showsyntaxerror() | |
2664 | if store_history: |
|
2673 | if store_history: | |
2665 | self.execution_count += 1 |
|
2674 | self.execution_count += 1 | |
2666 | return None |
|
2675 | return None | |
2667 |
|
2676 | |||
2668 | code_ast = self.transform_ast(code_ast) |
|
2677 | code_ast = self.transform_ast(code_ast) | |
2669 |
|
2678 | |||
2670 | interactivity = "none" if silent else self.ast_node_interactivity |
|
2679 | interactivity = "none" if silent else self.ast_node_interactivity | |
2671 | self.run_ast_nodes(code_ast.body, cell_name, |
|
2680 | self.run_ast_nodes(code_ast.body, cell_name, | |
2672 | interactivity=interactivity, compiler=compiler) |
|
2681 | interactivity=interactivity, compiler=compiler) | |
2673 |
|
2682 | |||
2674 | # Execute any registered post-execution functions. |
|
2683 | # Execute any registered post-execution functions. | |
2675 | # unless we are silent |
|
2684 | # unless we are silent | |
2676 | post_exec = [] if silent else self._post_execute.iteritems() |
|
2685 | post_exec = [] if silent else self._post_execute.iteritems() | |
2677 |
|
2686 | |||
2678 | for func, status in post_exec: |
|
2687 | for func, status in post_exec: | |
2679 | if self.disable_failing_post_execute and not status: |
|
2688 | if self.disable_failing_post_execute and not status: | |
2680 | continue |
|
2689 | continue | |
2681 | try: |
|
2690 | try: | |
2682 | func() |
|
2691 | func() | |
2683 | except KeyboardInterrupt: |
|
2692 | except KeyboardInterrupt: | |
2684 | print("\nKeyboardInterrupt", file=io.stderr) |
|
2693 | print("\nKeyboardInterrupt", file=io.stderr) | |
2685 | except Exception: |
|
2694 | except Exception: | |
2686 | # register as failing: |
|
2695 | # register as failing: | |
2687 | self._post_execute[func] = False |
|
2696 | self._post_execute[func] = False | |
2688 | self.showtraceback() |
|
2697 | self.showtraceback() | |
2689 | print('\n'.join([ |
|
2698 | print('\n'.join([ | |
2690 | "post-execution function %r produced an error." % func, |
|
2699 | "post-execution function %r produced an error." % func, | |
2691 | "If this problem persists, you can disable failing post-exec functions with:", |
|
2700 | "If this problem persists, you can disable failing post-exec functions with:", | |
2692 | "", |
|
2701 | "", | |
2693 | " get_ipython().disable_failing_post_execute = True" |
|
2702 | " get_ipython().disable_failing_post_execute = True" | |
2694 | ]), file=io.stderr) |
|
2703 | ]), file=io.stderr) | |
2695 |
|
2704 | |||
2696 | if store_history: |
|
2705 | if store_history: | |
2697 | # Write output to the database. Does nothing unless |
|
2706 | # Write output to the database. Does nothing unless | |
2698 | # history output logging is enabled. |
|
2707 | # history output logging is enabled. | |
2699 | self.history_manager.store_output(self.execution_count) |
|
2708 | self.history_manager.store_output(self.execution_count) | |
2700 | # Each cell is a *single* input, regardless of how many lines it has |
|
2709 | # Each cell is a *single* input, regardless of how many lines it has | |
2701 | self.execution_count += 1 |
|
2710 | self.execution_count += 1 | |
2702 |
|
2711 | |||
2703 | def transform_ast(self, node): |
|
2712 | def transform_ast(self, node): | |
2704 | """Apply the AST transformations from self.ast_transformers |
|
2713 | """Apply the AST transformations from self.ast_transformers | |
2705 |
|
2714 | |||
2706 | Parameters |
|
2715 | Parameters | |
2707 | ---------- |
|
2716 | ---------- | |
2708 | node : ast.Node |
|
2717 | node : ast.Node | |
2709 | The root node to be transformed. Typically called with the ast.Module |
|
2718 | The root node to be transformed. Typically called with the ast.Module | |
2710 | produced by parsing user input. |
|
2719 | produced by parsing user input. | |
2711 |
|
2720 | |||
2712 | Returns |
|
2721 | Returns | |
2713 | ------- |
|
2722 | ------- | |
2714 | An ast.Node corresponding to the node it was called with. Note that it |
|
2723 | An ast.Node corresponding to the node it was called with. Note that it | |
2715 | may also modify the passed object, so don't rely on references to the |
|
2724 | may also modify the passed object, so don't rely on references to the | |
2716 | original AST. |
|
2725 | original AST. | |
2717 | """ |
|
2726 | """ | |
2718 | for transformer in self.ast_transformers: |
|
2727 | for transformer in self.ast_transformers: | |
2719 | try: |
|
2728 | try: | |
2720 | node = transformer.visit(node) |
|
2729 | node = transformer.visit(node) | |
2721 | except Exception: |
|
2730 | except Exception: | |
2722 | warn("AST transformer %r threw an error. It will be unregistered." % transformer) |
|
2731 | warn("AST transformer %r threw an error. It will be unregistered." % transformer) | |
2723 | self.ast_transformers.remove(transformer) |
|
2732 | self.ast_transformers.remove(transformer) | |
2724 |
|
2733 | |||
2725 | if self.ast_transformers: |
|
2734 | if self.ast_transformers: | |
2726 | ast.fix_missing_locations(node) |
|
2735 | ast.fix_missing_locations(node) | |
2727 | return node |
|
2736 | return node | |
2728 |
|
2737 | |||
2729 |
|
2738 | |||
2730 | def run_ast_nodes(self, nodelist, cell_name, interactivity='last_expr', |
|
2739 | def run_ast_nodes(self, nodelist, cell_name, interactivity='last_expr', | |
2731 | compiler=compile): |
|
2740 | compiler=compile): | |
2732 | """Run a sequence of AST nodes. The execution mode depends on the |
|
2741 | """Run a sequence of AST nodes. The execution mode depends on the | |
2733 | interactivity parameter. |
|
2742 | interactivity parameter. | |
2734 |
|
2743 | |||
2735 | Parameters |
|
2744 | Parameters | |
2736 | ---------- |
|
2745 | ---------- | |
2737 | nodelist : list |
|
2746 | nodelist : list | |
2738 | A sequence of AST nodes to run. |
|
2747 | A sequence of AST nodes to run. | |
2739 | cell_name : str |
|
2748 | cell_name : str | |
2740 | Will be passed to the compiler as the filename of the cell. Typically |
|
2749 | Will be passed to the compiler as the filename of the cell. Typically | |
2741 | the value returned by ip.compile.cache(cell). |
|
2750 | the value returned by ip.compile.cache(cell). | |
2742 | interactivity : str |
|
2751 | interactivity : str | |
2743 | 'all', 'last', 'last_expr' or 'none', specifying which nodes should be |
|
2752 | 'all', 'last', 'last_expr' or 'none', specifying which nodes should be | |
2744 | run interactively (displaying output from expressions). 'last_expr' |
|
2753 | run interactively (displaying output from expressions). 'last_expr' | |
2745 | will run the last node interactively only if it is an expression (i.e. |
|
2754 | will run the last node interactively only if it is an expression (i.e. | |
2746 | expressions in loops or other blocks are not displayed. Other values |
|
2755 | expressions in loops or other blocks are not displayed. Other values | |
2747 | for this parameter will raise a ValueError. |
|
2756 | for this parameter will raise a ValueError. | |
2748 | compiler : callable |
|
2757 | compiler : callable | |
2749 | A function with the same interface as the built-in compile(), to turn |
|
2758 | A function with the same interface as the built-in compile(), to turn | |
2750 | the AST nodes into code objects. Default is the built-in compile(). |
|
2759 | the AST nodes into code objects. Default is the built-in compile(). | |
2751 | """ |
|
2760 | """ | |
2752 | if not nodelist: |
|
2761 | if not nodelist: | |
2753 | return |
|
2762 | return | |
2754 |
|
2763 | |||
2755 | if interactivity == 'last_expr': |
|
2764 | if interactivity == 'last_expr': | |
2756 | if isinstance(nodelist[-1], ast.Expr): |
|
2765 | if isinstance(nodelist[-1], ast.Expr): | |
2757 | interactivity = "last" |
|
2766 | interactivity = "last" | |
2758 | else: |
|
2767 | else: | |
2759 | interactivity = "none" |
|
2768 | interactivity = "none" | |
2760 |
|
2769 | |||
2761 | if interactivity == 'none': |
|
2770 | if interactivity == 'none': | |
2762 | to_run_exec, to_run_interactive = nodelist, [] |
|
2771 | to_run_exec, to_run_interactive = nodelist, [] | |
2763 | elif interactivity == 'last': |
|
2772 | elif interactivity == 'last': | |
2764 | to_run_exec, to_run_interactive = nodelist[:-1], nodelist[-1:] |
|
2773 | to_run_exec, to_run_interactive = nodelist[:-1], nodelist[-1:] | |
2765 | elif interactivity == 'all': |
|
2774 | elif interactivity == 'all': | |
2766 | to_run_exec, to_run_interactive = [], nodelist |
|
2775 | to_run_exec, to_run_interactive = [], nodelist | |
2767 | else: |
|
2776 | else: | |
2768 | raise ValueError("Interactivity was %r" % interactivity) |
|
2777 | raise ValueError("Interactivity was %r" % interactivity) | |
2769 |
|
2778 | |||
2770 | exec_count = self.execution_count |
|
2779 | exec_count = self.execution_count | |
2771 |
|
2780 | |||
2772 | try: |
|
2781 | try: | |
2773 | for i, node in enumerate(to_run_exec): |
|
2782 | for i, node in enumerate(to_run_exec): | |
2774 | mod = ast.Module([node]) |
|
2783 | mod = ast.Module([node]) | |
2775 | code = compiler(mod, cell_name, "exec") |
|
2784 | code = compiler(mod, cell_name, "exec") | |
2776 | if self.run_code(code): |
|
2785 | if self.run_code(code): | |
2777 | return True |
|
2786 | return True | |
2778 |
|
2787 | |||
2779 | for i, node in enumerate(to_run_interactive): |
|
2788 | for i, node in enumerate(to_run_interactive): | |
2780 | mod = ast.Interactive([node]) |
|
2789 | mod = ast.Interactive([node]) | |
2781 | code = compiler(mod, cell_name, "single") |
|
2790 | code = compiler(mod, cell_name, "single") | |
2782 | if self.run_code(code): |
|
2791 | if self.run_code(code): | |
2783 | return True |
|
2792 | return True | |
2784 |
|
2793 | |||
2785 | # Flush softspace |
|
2794 | # Flush softspace | |
2786 | if softspace(sys.stdout, 0): |
|
2795 | if softspace(sys.stdout, 0): | |
2787 | print() |
|
2796 | print() | |
2788 |
|
2797 | |||
2789 | except: |
|
2798 | except: | |
2790 | # It's possible to have exceptions raised here, typically by |
|
2799 | # It's possible to have exceptions raised here, typically by | |
2791 | # compilation of odd code (such as a naked 'return' outside a |
|
2800 | # compilation of odd code (such as a naked 'return' outside a | |
2792 | # function) that did parse but isn't valid. Typically the exception |
|
2801 | # function) that did parse but isn't valid. Typically the exception | |
2793 | # is a SyntaxError, but it's safest just to catch anything and show |
|
2802 | # is a SyntaxError, but it's safest just to catch anything and show | |
2794 | # the user a traceback. |
|
2803 | # the user a traceback. | |
2795 |
|
2804 | |||
2796 | # We do only one try/except outside the loop to minimize the impact |
|
2805 | # We do only one try/except outside the loop to minimize the impact | |
2797 | # on runtime, and also because if any node in the node list is |
|
2806 | # on runtime, and also because if any node in the node list is | |
2798 | # broken, we should stop execution completely. |
|
2807 | # broken, we should stop execution completely. | |
2799 | self.showtraceback() |
|
2808 | self.showtraceback() | |
2800 |
|
2809 | |||
2801 | return False |
|
2810 | return False | |
2802 |
|
2811 | |||
2803 | def run_code(self, code_obj): |
|
2812 | def run_code(self, code_obj): | |
2804 | """Execute a code object. |
|
2813 | """Execute a code object. | |
2805 |
|
2814 | |||
2806 | When an exception occurs, self.showtraceback() is called to display a |
|
2815 | When an exception occurs, self.showtraceback() is called to display a | |
2807 | traceback. |
|
2816 | traceback. | |
2808 |
|
2817 | |||
2809 | Parameters |
|
2818 | Parameters | |
2810 | ---------- |
|
2819 | ---------- | |
2811 | code_obj : code object |
|
2820 | code_obj : code object | |
2812 | A compiled code object, to be executed |
|
2821 | A compiled code object, to be executed | |
2813 |
|
2822 | |||
2814 | Returns |
|
2823 | Returns | |
2815 | ------- |
|
2824 | ------- | |
2816 | False : successful execution. |
|
2825 | False : successful execution. | |
2817 | True : an error occurred. |
|
2826 | True : an error occurred. | |
2818 | """ |
|
2827 | """ | |
2819 |
|
2828 | |||
2820 | # Set our own excepthook in case the user code tries to call it |
|
2829 | # Set our own excepthook in case the user code tries to call it | |
2821 | # directly, so that the IPython crash handler doesn't get triggered |
|
2830 | # directly, so that the IPython crash handler doesn't get triggered | |
2822 | old_excepthook,sys.excepthook = sys.excepthook, self.excepthook |
|
2831 | old_excepthook,sys.excepthook = sys.excepthook, self.excepthook | |
2823 |
|
2832 | |||
2824 | # we save the original sys.excepthook in the instance, in case config |
|
2833 | # we save the original sys.excepthook in the instance, in case config | |
2825 | # code (such as magics) needs access to it. |
|
2834 | # code (such as magics) needs access to it. | |
2826 | self.sys_excepthook = old_excepthook |
|
2835 | self.sys_excepthook = old_excepthook | |
2827 | outflag = 1 # happens in more places, so it's easier as default |
|
2836 | outflag = 1 # happens in more places, so it's easier as default | |
2828 | try: |
|
2837 | try: | |
2829 | try: |
|
2838 | try: | |
2830 | self.hooks.pre_run_code_hook() |
|
2839 | self.hooks.pre_run_code_hook() | |
2831 | #rprint('Running code', repr(code_obj)) # dbg |
|
2840 | #rprint('Running code', repr(code_obj)) # dbg | |
2832 | exec code_obj in self.user_global_ns, self.user_ns |
|
2841 | exec code_obj in self.user_global_ns, self.user_ns | |
2833 | finally: |
|
2842 | finally: | |
2834 | # Reset our crash handler in place |
|
2843 | # Reset our crash handler in place | |
2835 | sys.excepthook = old_excepthook |
|
2844 | sys.excepthook = old_excepthook | |
2836 | except SystemExit: |
|
2845 | except SystemExit: | |
2837 | self.showtraceback(exception_only=True) |
|
2846 | self.showtraceback(exception_only=True) | |
2838 | warn("To exit: use 'exit', 'quit', or Ctrl-D.", level=1) |
|
2847 | warn("To exit: use 'exit', 'quit', or Ctrl-D.", level=1) | |
2839 | except self.custom_exceptions: |
|
2848 | except self.custom_exceptions: | |
2840 | etype,value,tb = sys.exc_info() |
|
2849 | etype,value,tb = sys.exc_info() | |
2841 | self.CustomTB(etype,value,tb) |
|
2850 | self.CustomTB(etype,value,tb) | |
2842 | except: |
|
2851 | except: | |
2843 | self.showtraceback() |
|
2852 | self.showtraceback() | |
2844 | else: |
|
2853 | else: | |
2845 | outflag = 0 |
|
2854 | outflag = 0 | |
2846 | return outflag |
|
2855 | return outflag | |
2847 |
|
2856 | |||
2848 | # For backwards compatibility |
|
2857 | # For backwards compatibility | |
2849 | runcode = run_code |
|
2858 | runcode = run_code | |
2850 |
|
2859 | |||
2851 | #------------------------------------------------------------------------- |
|
2860 | #------------------------------------------------------------------------- | |
2852 | # Things related to GUI support and pylab |
|
2861 | # Things related to GUI support and pylab | |
2853 | #------------------------------------------------------------------------- |
|
2862 | #------------------------------------------------------------------------- | |
2854 |
|
2863 | |||
2855 | def enable_gui(self, gui=None): |
|
2864 | def enable_gui(self, gui=None): | |
2856 | raise NotImplementedError('Implement enable_gui in a subclass') |
|
2865 | raise NotImplementedError('Implement enable_gui in a subclass') | |
2857 |
|
2866 | |||
2858 | def enable_matplotlib(self, gui=None): |
|
2867 | def enable_matplotlib(self, gui=None): | |
2859 | """Enable interactive matplotlib and inline figure support. |
|
2868 | """Enable interactive matplotlib and inline figure support. | |
2860 |
|
2869 | |||
2861 | This takes the following steps: |
|
2870 | This takes the following steps: | |
2862 |
|
2871 | |||
2863 | 1. select the appropriate eventloop and matplotlib backend |
|
2872 | 1. select the appropriate eventloop and matplotlib backend | |
2864 | 2. set up matplotlib for interactive use with that backend |
|
2873 | 2. set up matplotlib for interactive use with that backend | |
2865 | 3. configure formatters for inline figure display |
|
2874 | 3. configure formatters for inline figure display | |
2866 | 4. enable the selected gui eventloop |
|
2875 | 4. enable the selected gui eventloop | |
2867 |
|
2876 | |||
2868 | Parameters |
|
2877 | Parameters | |
2869 | ---------- |
|
2878 | ---------- | |
2870 | gui : optional, string |
|
2879 | gui : optional, string | |
2871 | If given, dictates the choice of matplotlib GUI backend to use |
|
2880 | If given, dictates the choice of matplotlib GUI backend to use | |
2872 | (should be one of IPython's supported backends, 'qt', 'osx', 'tk', |
|
2881 | (should be one of IPython's supported backends, 'qt', 'osx', 'tk', | |
2873 | 'gtk', 'wx' or 'inline'), otherwise we use the default chosen by |
|
2882 | 'gtk', 'wx' or 'inline'), otherwise we use the default chosen by | |
2874 | matplotlib (as dictated by the matplotlib build-time options plus the |
|
2883 | matplotlib (as dictated by the matplotlib build-time options plus the | |
2875 | user's matplotlibrc configuration file). Note that not all backends |
|
2884 | user's matplotlibrc configuration file). Note that not all backends | |
2876 | make sense in all contexts, for example a terminal ipython can't |
|
2885 | make sense in all contexts, for example a terminal ipython can't | |
2877 | display figures inline. |
|
2886 | display figures inline. | |
2878 | """ |
|
2887 | """ | |
2879 | from IPython.core import pylabtools as pt |
|
2888 | from IPython.core import pylabtools as pt | |
2880 | gui, backend = pt.find_gui_and_backend(gui, self.pylab_gui_select) |
|
2889 | gui, backend = pt.find_gui_and_backend(gui, self.pylab_gui_select) | |
2881 |
|
2890 | |||
2882 | if gui != 'inline': |
|
2891 | if gui != 'inline': | |
2883 | # If we have our first gui selection, store it |
|
2892 | # If we have our first gui selection, store it | |
2884 | if self.pylab_gui_select is None: |
|
2893 | if self.pylab_gui_select is None: | |
2885 | self.pylab_gui_select = gui |
|
2894 | self.pylab_gui_select = gui | |
2886 | # Otherwise if they are different |
|
2895 | # Otherwise if they are different | |
2887 | elif gui != self.pylab_gui_select: |
|
2896 | elif gui != self.pylab_gui_select: | |
2888 | print ('Warning: Cannot change to a different GUI toolkit: %s.' |
|
2897 | print ('Warning: Cannot change to a different GUI toolkit: %s.' | |
2889 | ' Using %s instead.' % (gui, self.pylab_gui_select)) |
|
2898 | ' Using %s instead.' % (gui, self.pylab_gui_select)) | |
2890 | gui, backend = pt.find_gui_and_backend(self.pylab_gui_select) |
|
2899 | gui, backend = pt.find_gui_and_backend(self.pylab_gui_select) | |
2891 |
|
2900 | |||
2892 | pt.activate_matplotlib(backend) |
|
2901 | pt.activate_matplotlib(backend) | |
2893 | pt.configure_inline_support(self, backend) |
|
2902 | pt.configure_inline_support(self, backend) | |
2894 |
|
2903 | |||
2895 | # Now we must activate the gui pylab wants to use, and fix %run to take |
|
2904 | # Now we must activate the gui pylab wants to use, and fix %run to take | |
2896 | # plot updates into account |
|
2905 | # plot updates into account | |
2897 | self.enable_gui(gui) |
|
2906 | self.enable_gui(gui) | |
2898 | self.magics_manager.registry['ExecutionMagics'].default_runner = \ |
|
2907 | self.magics_manager.registry['ExecutionMagics'].default_runner = \ | |
2899 | pt.mpl_runner(self.safe_execfile) |
|
2908 | pt.mpl_runner(self.safe_execfile) | |
2900 |
|
2909 | |||
2901 | return gui, backend |
|
2910 | return gui, backend | |
2902 |
|
2911 | |||
2903 | def enable_pylab(self, gui=None, import_all=True, welcome_message=False): |
|
2912 | def enable_pylab(self, gui=None, import_all=True, welcome_message=False): | |
2904 | """Activate pylab support at runtime. |
|
2913 | """Activate pylab support at runtime. | |
2905 |
|
2914 | |||
2906 | This turns on support for matplotlib, preloads into the interactive |
|
2915 | This turns on support for matplotlib, preloads into the interactive | |
2907 | namespace all of numpy and pylab, and configures IPython to correctly |
|
2916 | namespace all of numpy and pylab, and configures IPython to correctly | |
2908 | interact with the GUI event loop. The GUI backend to be used can be |
|
2917 | interact with the GUI event loop. The GUI backend to be used can be | |
2909 | optionally selected with the optional ``gui`` argument. |
|
2918 | optionally selected with the optional ``gui`` argument. | |
2910 |
|
2919 | |||
2911 | This method only adds preloading the namespace to InteractiveShell.enable_matplotlib. |
|
2920 | This method only adds preloading the namespace to InteractiveShell.enable_matplotlib. | |
2912 |
|
2921 | |||
2913 | Parameters |
|
2922 | Parameters | |
2914 | ---------- |
|
2923 | ---------- | |
2915 | gui : optional, string |
|
2924 | gui : optional, string | |
2916 | If given, dictates the choice of matplotlib GUI backend to use |
|
2925 | If given, dictates the choice of matplotlib GUI backend to use | |
2917 | (should be one of IPython's supported backends, 'qt', 'osx', 'tk', |
|
2926 | (should be one of IPython's supported backends, 'qt', 'osx', 'tk', | |
2918 | 'gtk', 'wx' or 'inline'), otherwise we use the default chosen by |
|
2927 | 'gtk', 'wx' or 'inline'), otherwise we use the default chosen by | |
2919 | matplotlib (as dictated by the matplotlib build-time options plus the |
|
2928 | matplotlib (as dictated by the matplotlib build-time options plus the | |
2920 | user's matplotlibrc configuration file). Note that not all backends |
|
2929 | user's matplotlibrc configuration file). Note that not all backends | |
2921 | make sense in all contexts, for example a terminal ipython can't |
|
2930 | make sense in all contexts, for example a terminal ipython can't | |
2922 | display figures inline. |
|
2931 | display figures inline. | |
2923 | import_all : optional, bool, default: True |
|
2932 | import_all : optional, bool, default: True | |
2924 | Whether to do `from numpy import *` and `from pylab import *` |
|
2933 | Whether to do `from numpy import *` and `from pylab import *` | |
2925 | in addition to module imports. |
|
2934 | in addition to module imports. | |
2926 | welcome_message : deprecated |
|
2935 | welcome_message : deprecated | |
2927 | This argument is ignored, no welcome message will be displayed. |
|
2936 | This argument is ignored, no welcome message will be displayed. | |
2928 | """ |
|
2937 | """ | |
2929 | from IPython.core.pylabtools import import_pylab |
|
2938 | from IPython.core.pylabtools import import_pylab | |
2930 |
|
2939 | |||
2931 | gui, backend = self.enable_matplotlib(gui) |
|
2940 | gui, backend = self.enable_matplotlib(gui) | |
2932 |
|
2941 | |||
2933 | # We want to prevent the loading of pylab to pollute the user's |
|
2942 | # We want to prevent the loading of pylab to pollute the user's | |
2934 | # namespace as shown by the %who* magics, so we execute the activation |
|
2943 | # namespace as shown by the %who* magics, so we execute the activation | |
2935 | # code in an empty namespace, and we update *both* user_ns and |
|
2944 | # code in an empty namespace, and we update *both* user_ns and | |
2936 | # user_ns_hidden with this information. |
|
2945 | # user_ns_hidden with this information. | |
2937 | ns = {} |
|
2946 | ns = {} | |
2938 | import_pylab(ns, import_all) |
|
2947 | import_pylab(ns, import_all) | |
2939 | # warn about clobbered names |
|
2948 | # warn about clobbered names | |
2940 | ignored = set(["__builtins__"]) |
|
2949 | ignored = set(["__builtins__"]) | |
2941 | both = set(ns).intersection(self.user_ns).difference(ignored) |
|
2950 | both = set(ns).intersection(self.user_ns).difference(ignored) | |
2942 | clobbered = [ name for name in both if self.user_ns[name] is not ns[name] ] |
|
2951 | clobbered = [ name for name in both if self.user_ns[name] is not ns[name] ] | |
2943 | self.user_ns.update(ns) |
|
2952 | self.user_ns.update(ns) | |
2944 | self.user_ns_hidden.update(ns) |
|
2953 | self.user_ns_hidden.update(ns) | |
2945 | return gui, backend, clobbered |
|
2954 | return gui, backend, clobbered | |
2946 |
|
2955 | |||
2947 | #------------------------------------------------------------------------- |
|
2956 | #------------------------------------------------------------------------- | |
2948 | # Utilities |
|
2957 | # Utilities | |
2949 | #------------------------------------------------------------------------- |
|
2958 | #------------------------------------------------------------------------- | |
2950 |
|
2959 | |||
2951 | def var_expand(self, cmd, depth=0, formatter=DollarFormatter()): |
|
2960 | def var_expand(self, cmd, depth=0, formatter=DollarFormatter()): | |
2952 | """Expand python variables in a string. |
|
2961 | """Expand python variables in a string. | |
2953 |
|
2962 | |||
2954 | The depth argument indicates how many frames above the caller should |
|
2963 | The depth argument indicates how many frames above the caller should | |
2955 | be walked to look for the local namespace where to expand variables. |
|
2964 | be walked to look for the local namespace where to expand variables. | |
2956 |
|
2965 | |||
2957 | The global namespace for expansion is always the user's interactive |
|
2966 | The global namespace for expansion is always the user's interactive | |
2958 | namespace. |
|
2967 | namespace. | |
2959 | """ |
|
2968 | """ | |
2960 | ns = self.user_ns.copy() |
|
2969 | ns = self.user_ns.copy() | |
2961 | ns.update(sys._getframe(depth+1).f_locals) |
|
2970 | ns.update(sys._getframe(depth+1).f_locals) | |
2962 | try: |
|
2971 | try: | |
2963 | # We have to use .vformat() here, because 'self' is a valid and common |
|
2972 | # We have to use .vformat() here, because 'self' is a valid and common | |
2964 | # name, and expanding **ns for .format() would make it collide with |
|
2973 | # name, and expanding **ns for .format() would make it collide with | |
2965 | # the 'self' argument of the method. |
|
2974 | # the 'self' argument of the method. | |
2966 | cmd = formatter.vformat(cmd, args=[], kwargs=ns) |
|
2975 | cmd = formatter.vformat(cmd, args=[], kwargs=ns) | |
2967 | except Exception: |
|
2976 | except Exception: | |
2968 | # if formatter couldn't format, just let it go untransformed |
|
2977 | # if formatter couldn't format, just let it go untransformed | |
2969 | pass |
|
2978 | pass | |
2970 | return cmd |
|
2979 | return cmd | |
2971 |
|
2980 | |||
2972 | def mktempfile(self, data=None, prefix='ipython_edit_'): |
|
2981 | def mktempfile(self, data=None, prefix='ipython_edit_'): | |
2973 | """Make a new tempfile and return its filename. |
|
2982 | """Make a new tempfile and return its filename. | |
2974 |
|
2983 | |||
2975 | This makes a call to tempfile.mktemp, but it registers the created |
|
2984 | This makes a call to tempfile.mktemp, but it registers the created | |
2976 | filename internally so ipython cleans it up at exit time. |
|
2985 | filename internally so ipython cleans it up at exit time. | |
2977 |
|
2986 | |||
2978 | Optional inputs: |
|
2987 | Optional inputs: | |
2979 |
|
2988 | |||
2980 | - data(None): if data is given, it gets written out to the temp file |
|
2989 | - data(None): if data is given, it gets written out to the temp file | |
2981 | immediately, and the file is closed again.""" |
|
2990 | immediately, and the file is closed again.""" | |
2982 |
|
2991 | |||
2983 | filename = tempfile.mktemp('.py', prefix) |
|
2992 | filename = tempfile.mktemp('.py', prefix) | |
2984 | self.tempfiles.append(filename) |
|
2993 | self.tempfiles.append(filename) | |
2985 |
|
2994 | |||
2986 | if data: |
|
2995 | if data: | |
2987 | tmp_file = open(filename,'w') |
|
2996 | tmp_file = open(filename,'w') | |
2988 | tmp_file.write(data) |
|
2997 | tmp_file.write(data) | |
2989 | tmp_file.close() |
|
2998 | tmp_file.close() | |
2990 | return filename |
|
2999 | return filename | |
2991 |
|
3000 | |||
2992 | # TODO: This should be removed when Term is refactored. |
|
3001 | # TODO: This should be removed when Term is refactored. | |
2993 | def write(self,data): |
|
3002 | def write(self,data): | |
2994 | """Write a string to the default output""" |
|
3003 | """Write a string to the default output""" | |
2995 | io.stdout.write(data) |
|
3004 | io.stdout.write(data) | |
2996 |
|
3005 | |||
2997 | # TODO: This should be removed when Term is refactored. |
|
3006 | # TODO: This should be removed when Term is refactored. | |
2998 | def write_err(self,data): |
|
3007 | def write_err(self,data): | |
2999 | """Write a string to the default error output""" |
|
3008 | """Write a string to the default error output""" | |
3000 | io.stderr.write(data) |
|
3009 | io.stderr.write(data) | |
3001 |
|
3010 | |||
3002 | def ask_yes_no(self, prompt, default=None): |
|
3011 | def ask_yes_no(self, prompt, default=None): | |
3003 | if self.quiet: |
|
3012 | if self.quiet: | |
3004 | return True |
|
3013 | return True | |
3005 | return ask_yes_no(prompt,default) |
|
3014 | return ask_yes_no(prompt,default) | |
3006 |
|
3015 | |||
3007 | def show_usage(self): |
|
3016 | def show_usage(self): | |
3008 | """Show a usage message""" |
|
3017 | """Show a usage message""" | |
3009 | page.page(IPython.core.usage.interactive_usage) |
|
3018 | page.page(IPython.core.usage.interactive_usage) | |
3010 |
|
3019 | |||
3011 | def extract_input_lines(self, range_str, raw=False): |
|
3020 | def extract_input_lines(self, range_str, raw=False): | |
3012 | """Return as a string a set of input history slices. |
|
3021 | """Return as a string a set of input history slices. | |
3013 |
|
3022 | |||
3014 | Parameters |
|
3023 | Parameters | |
3015 | ---------- |
|
3024 | ---------- | |
3016 | range_str : string |
|
3025 | range_str : string | |
3017 | The set of slices is given as a string, like "~5/6-~4/2 4:8 9", |
|
3026 | The set of slices is given as a string, like "~5/6-~4/2 4:8 9", | |
3018 | since this function is for use by magic functions which get their |
|
3027 | since this function is for use by magic functions which get their | |
3019 | arguments as strings. The number before the / is the session |
|
3028 | arguments as strings. The number before the / is the session | |
3020 | number: ~n goes n back from the current session. |
|
3029 | number: ~n goes n back from the current session. | |
3021 |
|
3030 | |||
3022 | Optional Parameters: |
|
3031 | Optional Parameters: | |
3023 | - raw(False): by default, the processed input is used. If this is |
|
3032 | - raw(False): by default, the processed input is used. If this is | |
3024 | true, the raw input history is used instead. |
|
3033 | true, the raw input history is used instead. | |
3025 |
|
3034 | |||
3026 | Note that slices can be called with two notations: |
|
3035 | Note that slices can be called with two notations: | |
3027 |
|
3036 | |||
3028 | N:M -> standard python form, means including items N...(M-1). |
|
3037 | N:M -> standard python form, means including items N...(M-1). | |
3029 |
|
3038 | |||
3030 | N-M -> include items N..M (closed endpoint).""" |
|
3039 | N-M -> include items N..M (closed endpoint).""" | |
3031 | lines = self.history_manager.get_range_by_str(range_str, raw=raw) |
|
3040 | lines = self.history_manager.get_range_by_str(range_str, raw=raw) | |
3032 | return "\n".join(x for _, _, x in lines) |
|
3041 | return "\n".join(x for _, _, x in lines) | |
3033 |
|
3042 | |||
3034 | def find_user_code(self, target, raw=True, py_only=False, skip_encoding_cookie=True): |
|
3043 | def find_user_code(self, target, raw=True, py_only=False, skip_encoding_cookie=True): | |
3035 | """Get a code string from history, file, url, or a string or macro. |
|
3044 | """Get a code string from history, file, url, or a string or macro. | |
3036 |
|
3045 | |||
3037 | This is mainly used by magic functions. |
|
3046 | This is mainly used by magic functions. | |
3038 |
|
3047 | |||
3039 | Parameters |
|
3048 | Parameters | |
3040 | ---------- |
|
3049 | ---------- | |
3041 |
|
3050 | |||
3042 | target : str |
|
3051 | target : str | |
3043 |
|
3052 | |||
3044 | A string specifying code to retrieve. This will be tried respectively |
|
3053 | A string specifying code to retrieve. This will be tried respectively | |
3045 | as: ranges of input history (see %history for syntax), url, |
|
3054 | as: ranges of input history (see %history for syntax), url, | |
3046 | correspnding .py file, filename, or an expression evaluating to a |
|
3055 | correspnding .py file, filename, or an expression evaluating to a | |
3047 | string or Macro in the user namespace. |
|
3056 | string or Macro in the user namespace. | |
3048 |
|
3057 | |||
3049 | raw : bool |
|
3058 | raw : bool | |
3050 | If true (default), retrieve raw history. Has no effect on the other |
|
3059 | If true (default), retrieve raw history. Has no effect on the other | |
3051 | retrieval mechanisms. |
|
3060 | retrieval mechanisms. | |
3052 |
|
3061 | |||
3053 | py_only : bool (default False) |
|
3062 | py_only : bool (default False) | |
3054 | Only try to fetch python code, do not try alternative methods to decode file |
|
3063 | Only try to fetch python code, do not try alternative methods to decode file | |
3055 | if unicode fails. |
|
3064 | if unicode fails. | |
3056 |
|
3065 | |||
3057 | Returns |
|
3066 | Returns | |
3058 | ------- |
|
3067 | ------- | |
3059 | A string of code. |
|
3068 | A string of code. | |
3060 |
|
3069 | |||
3061 | ValueError is raised if nothing is found, and TypeError if it evaluates |
|
3070 | ValueError is raised if nothing is found, and TypeError if it evaluates | |
3062 | to an object of another type. In each case, .args[0] is a printable |
|
3071 | to an object of another type. In each case, .args[0] is a printable | |
3063 | message. |
|
3072 | message. | |
3064 | """ |
|
3073 | """ | |
3065 | code = self.extract_input_lines(target, raw=raw) # Grab history |
|
3074 | code = self.extract_input_lines(target, raw=raw) # Grab history | |
3066 | if code: |
|
3075 | if code: | |
3067 | return code |
|
3076 | return code | |
3068 | utarget = unquote_filename(target) |
|
3077 | utarget = unquote_filename(target) | |
3069 | try: |
|
3078 | try: | |
3070 | if utarget.startswith(('http://', 'https://')): |
|
3079 | if utarget.startswith(('http://', 'https://')): | |
3071 | return openpy.read_py_url(utarget, skip_encoding_cookie=skip_encoding_cookie) |
|
3080 | return openpy.read_py_url(utarget, skip_encoding_cookie=skip_encoding_cookie) | |
3072 | except UnicodeDecodeError: |
|
3081 | except UnicodeDecodeError: | |
3073 | if not py_only : |
|
3082 | if not py_only : | |
3074 | from urllib import urlopen # Deferred import |
|
3083 | from urllib import urlopen # Deferred import | |
3075 | response = urlopen(target) |
|
3084 | response = urlopen(target) | |
3076 | return response.read().decode('latin1') |
|
3085 | return response.read().decode('latin1') | |
3077 | raise ValueError(("'%s' seem to be unreadable.") % utarget) |
|
3086 | raise ValueError(("'%s' seem to be unreadable.") % utarget) | |
3078 |
|
3087 | |||
3079 | potential_target = [target] |
|
3088 | potential_target = [target] | |
3080 | try : |
|
3089 | try : | |
3081 | potential_target.insert(0,get_py_filename(target)) |
|
3090 | potential_target.insert(0,get_py_filename(target)) | |
3082 | except IOError: |
|
3091 | except IOError: | |
3083 | pass |
|
3092 | pass | |
3084 |
|
3093 | |||
3085 | for tgt in potential_target : |
|
3094 | for tgt in potential_target : | |
3086 | if os.path.isfile(tgt): # Read file |
|
3095 | if os.path.isfile(tgt): # Read file | |
3087 | try : |
|
3096 | try : | |
3088 | return openpy.read_py_file(tgt, skip_encoding_cookie=skip_encoding_cookie) |
|
3097 | return openpy.read_py_file(tgt, skip_encoding_cookie=skip_encoding_cookie) | |
3089 | except UnicodeDecodeError : |
|
3098 | except UnicodeDecodeError : | |
3090 | if not py_only : |
|
3099 | if not py_only : | |
3091 | with io_open(tgt,'r', encoding='latin1') as f : |
|
3100 | with io_open(tgt,'r', encoding='latin1') as f : | |
3092 | return f.read() |
|
3101 | return f.read() | |
3093 | raise ValueError(("'%s' seem to be unreadable.") % target) |
|
3102 | raise ValueError(("'%s' seem to be unreadable.") % target) | |
3094 | elif os.path.isdir(os.path.expanduser(tgt)): |
|
3103 | elif os.path.isdir(os.path.expanduser(tgt)): | |
3095 | raise ValueError("'%s' is a directory, not a regular file." % target) |
|
3104 | raise ValueError("'%s' is a directory, not a regular file." % target) | |
3096 |
|
3105 | |||
3097 | try: # User namespace |
|
3106 | try: # User namespace | |
3098 | codeobj = eval(target, self.user_ns) |
|
3107 | codeobj = eval(target, self.user_ns) | |
3099 | except Exception: |
|
3108 | except Exception: | |
3100 | raise ValueError(("'%s' was not found in history, as a file, url, " |
|
3109 | raise ValueError(("'%s' was not found in history, as a file, url, " | |
3101 | "nor in the user namespace.") % target) |
|
3110 | "nor in the user namespace.") % target) | |
3102 | if isinstance(codeobj, basestring): |
|
3111 | if isinstance(codeobj, basestring): | |
3103 | return codeobj |
|
3112 | return codeobj | |
3104 | elif isinstance(codeobj, Macro): |
|
3113 | elif isinstance(codeobj, Macro): | |
3105 | return codeobj.value |
|
3114 | return codeobj.value | |
3106 |
|
3115 | |||
3107 | raise TypeError("%s is neither a string nor a macro." % target, |
|
3116 | raise TypeError("%s is neither a string nor a macro." % target, | |
3108 | codeobj) |
|
3117 | codeobj) | |
3109 |
|
3118 | |||
3110 | #------------------------------------------------------------------------- |
|
3119 | #------------------------------------------------------------------------- | |
3111 | # Things related to IPython exiting |
|
3120 | # Things related to IPython exiting | |
3112 | #------------------------------------------------------------------------- |
|
3121 | #------------------------------------------------------------------------- | |
3113 | def atexit_operations(self): |
|
3122 | def atexit_operations(self): | |
3114 | """This will be executed at the time of exit. |
|
3123 | """This will be executed at the time of exit. | |
3115 |
|
3124 | |||
3116 | Cleanup operations and saving of persistent data that is done |
|
3125 | Cleanup operations and saving of persistent data that is done | |
3117 | unconditionally by IPython should be performed here. |
|
3126 | unconditionally by IPython should be performed here. | |
3118 |
|
3127 | |||
3119 | For things that may depend on startup flags or platform specifics (such |
|
3128 | For things that may depend on startup flags or platform specifics (such | |
3120 | as having readline or not), register a separate atexit function in the |
|
3129 | as having readline or not), register a separate atexit function in the | |
3121 | code that has the appropriate information, rather than trying to |
|
3130 | code that has the appropriate information, rather than trying to | |
3122 | clutter |
|
3131 | clutter | |
3123 | """ |
|
3132 | """ | |
3124 | # Close the history session (this stores the end time and line count) |
|
3133 | # Close the history session (this stores the end time and line count) | |
3125 | # this must be *before* the tempfile cleanup, in case of temporary |
|
3134 | # this must be *before* the tempfile cleanup, in case of temporary | |
3126 | # history db |
|
3135 | # history db | |
3127 | self.history_manager.end_session() |
|
3136 | self.history_manager.end_session() | |
3128 |
|
3137 | |||
3129 | # Cleanup all tempfiles left around |
|
3138 | # Cleanup all tempfiles left around | |
3130 | for tfile in self.tempfiles: |
|
3139 | for tfile in self.tempfiles: | |
3131 | try: |
|
3140 | try: | |
3132 | os.unlink(tfile) |
|
3141 | os.unlink(tfile) | |
3133 | except OSError: |
|
3142 | except OSError: | |
3134 | pass |
|
3143 | pass | |
3135 |
|
3144 | |||
3136 | # Clear all user namespaces to release all references cleanly. |
|
3145 | # Clear all user namespaces to release all references cleanly. | |
3137 | self.reset(new_session=False) |
|
3146 | self.reset(new_session=False) | |
3138 |
|
3147 | |||
3139 | # Run user hooks |
|
3148 | # Run user hooks | |
3140 | self.hooks.shutdown_hook() |
|
3149 | self.hooks.shutdown_hook() | |
3141 |
|
3150 | |||
3142 | def cleanup(self): |
|
3151 | def cleanup(self): | |
3143 | self.restore_sys_module_state() |
|
3152 | self.restore_sys_module_state() | |
3144 |
|
3153 | |||
3145 |
|
3154 | |||
3146 | class InteractiveShellABC(object): |
|
3155 | class InteractiveShellABC(object): | |
3147 | """An abstract base class for InteractiveShell.""" |
|
3156 | """An abstract base class for InteractiveShell.""" | |
3148 | __metaclass__ = abc.ABCMeta |
|
3157 | __metaclass__ = abc.ABCMeta | |
3149 |
|
3158 | |||
3150 | InteractiveShellABC.register(InteractiveShell) |
|
3159 | InteractiveShellABC.register(InteractiveShell) |
@@ -1,1231 +1,1231 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 | """Implementation of execution-related magic functions. |
|
2 | """Implementation of execution-related magic functions. | |
3 | """ |
|
3 | """ | |
4 | #----------------------------------------------------------------------------- |
|
4 | #----------------------------------------------------------------------------- | |
5 | # Copyright (c) 2012 The IPython Development Team. |
|
5 | # Copyright (c) 2012 The IPython Development Team. | |
6 | # |
|
6 | # | |
7 | # Distributed under the terms of the Modified BSD License. |
|
7 | # Distributed under the terms of the Modified BSD License. | |
8 | # |
|
8 | # | |
9 | # The full license is in the file COPYING.txt, distributed with this software. |
|
9 | # The full license is in the file COPYING.txt, distributed with this software. | |
10 | #----------------------------------------------------------------------------- |
|
10 | #----------------------------------------------------------------------------- | |
11 |
|
11 | |||
12 | #----------------------------------------------------------------------------- |
|
12 | #----------------------------------------------------------------------------- | |
13 | # Imports |
|
13 | # Imports | |
14 | #----------------------------------------------------------------------------- |
|
14 | #----------------------------------------------------------------------------- | |
15 |
|
15 | |||
16 | # Stdlib |
|
16 | # Stdlib | |
17 | import __builtin__ as builtin_mod |
|
17 | import __builtin__ as builtin_mod | |
18 | import ast |
|
18 | import ast | |
19 | import bdb |
|
19 | import bdb | |
20 | import os |
|
20 | import os | |
21 | import sys |
|
21 | import sys | |
22 | import time |
|
22 | import time | |
23 | from StringIO import StringIO |
|
23 | from StringIO import StringIO | |
24 |
|
24 | |||
25 | # cProfile was added in Python2.5 |
|
25 | # cProfile was added in Python2.5 | |
26 | try: |
|
26 | try: | |
27 | import cProfile as profile |
|
27 | import cProfile as profile | |
28 | import pstats |
|
28 | import pstats | |
29 | except ImportError: |
|
29 | except ImportError: | |
30 | # profile isn't bundled by default in Debian for license reasons |
|
30 | # profile isn't bundled by default in Debian for license reasons | |
31 | try: |
|
31 | try: | |
32 | import profile, pstats |
|
32 | import profile, pstats | |
33 | except ImportError: |
|
33 | except ImportError: | |
34 | profile = pstats = None |
|
34 | profile = pstats = None | |
35 |
|
35 | |||
36 | # Our own packages |
|
36 | # Our own packages | |
37 | from IPython.core import debugger, oinspect |
|
37 | from IPython.core import debugger, oinspect | |
38 | from IPython.core import magic_arguments |
|
38 | from IPython.core import magic_arguments | |
39 | from IPython.core import page |
|
39 | from IPython.core import page | |
40 | from IPython.core.error import UsageError |
|
40 | from IPython.core.error import UsageError | |
41 | from IPython.core.macro import Macro |
|
41 | from IPython.core.macro import Macro | |
42 | from IPython.core.magic import (Magics, magics_class, line_magic, cell_magic, |
|
42 | from IPython.core.magic import (Magics, magics_class, line_magic, cell_magic, | |
43 | line_cell_magic, on_off, needs_local_scope) |
|
43 | line_cell_magic, on_off, needs_local_scope) | |
44 | from IPython.testing.skipdoctest import skip_doctest |
|
44 | from IPython.testing.skipdoctest import skip_doctest | |
45 | from IPython.utils import py3compat |
|
45 | from IPython.utils import py3compat | |
46 | from IPython.utils.contexts import preserve_keys |
|
46 | from IPython.utils.contexts import preserve_keys | |
47 | from IPython.utils.io import capture_output |
|
47 | from IPython.utils.io import capture_output | |
48 | from IPython.utils.ipstruct import Struct |
|
48 | from IPython.utils.ipstruct import Struct | |
49 | from IPython.utils.module_paths import find_mod |
|
49 | from IPython.utils.module_paths import find_mod | |
50 | from IPython.utils.path import get_py_filename, unquote_filename, shellglob |
|
50 | from IPython.utils.path import get_py_filename, unquote_filename, shellglob | |
51 | from IPython.utils.timing import clock, clock2 |
|
51 | from IPython.utils.timing import clock, clock2 | |
52 | from IPython.utils.warn import warn, error |
|
52 | from IPython.utils.warn import warn, error | |
53 |
|
53 | |||
54 |
|
54 | |||
55 | #----------------------------------------------------------------------------- |
|
55 | #----------------------------------------------------------------------------- | |
56 | # Magic implementation classes |
|
56 | # Magic implementation classes | |
57 | #----------------------------------------------------------------------------- |
|
57 | #----------------------------------------------------------------------------- | |
58 |
|
58 | |||
59 | @magics_class |
|
59 | @magics_class | |
60 | class ExecutionMagics(Magics): |
|
60 | class ExecutionMagics(Magics): | |
61 | """Magics related to code execution, debugging, profiling, etc. |
|
61 | """Magics related to code execution, debugging, profiling, etc. | |
62 |
|
62 | |||
63 | """ |
|
63 | """ | |
64 |
|
64 | |||
65 | def __init__(self, shell): |
|
65 | def __init__(self, shell): | |
66 | super(ExecutionMagics, self).__init__(shell) |
|
66 | super(ExecutionMagics, self).__init__(shell) | |
67 | if profile is None: |
|
67 | if profile is None: | |
68 | self.prun = self.profile_missing_notice |
|
68 | self.prun = self.profile_missing_notice | |
69 | # Default execution function used to actually run user code. |
|
69 | # Default execution function used to actually run user code. | |
70 | self.default_runner = None |
|
70 | self.default_runner = None | |
71 |
|
71 | |||
72 | def profile_missing_notice(self, *args, **kwargs): |
|
72 | def profile_missing_notice(self, *args, **kwargs): | |
73 | error("""\ |
|
73 | error("""\ | |
74 | The profile module could not be found. It has been removed from the standard |
|
74 | The profile module could not be found. It has been removed from the standard | |
75 | python packages because of its non-free license. To use profiling, install the |
|
75 | python packages because of its non-free license. To use profiling, install the | |
76 | python-profiler package from non-free.""") |
|
76 | python-profiler package from non-free.""") | |
77 |
|
77 | |||
78 | @skip_doctest |
|
78 | @skip_doctest | |
79 | @line_cell_magic |
|
79 | @line_cell_magic | |
80 | def prun(self, parameter_s='', cell=None): |
|
80 | def prun(self, parameter_s='', cell=None): | |
81 |
|
81 | |||
82 | """Run a statement through the python code profiler. |
|
82 | """Run a statement through the python code profiler. | |
83 |
|
83 | |||
84 | Usage, in line mode: |
|
84 | Usage, in line mode: | |
85 | %prun [options] statement |
|
85 | %prun [options] statement | |
86 |
|
86 | |||
87 | Usage, in cell mode: |
|
87 | Usage, in cell mode: | |
88 | %%prun [options] [statement] |
|
88 | %%prun [options] [statement] | |
89 | code... |
|
89 | code... | |
90 | code... |
|
90 | code... | |
91 |
|
91 | |||
92 | In cell mode, the additional code lines are appended to the (possibly |
|
92 | In cell mode, the additional code lines are appended to the (possibly | |
93 | empty) statement in the first line. Cell mode allows you to easily |
|
93 | empty) statement in the first line. Cell mode allows you to easily | |
94 | profile multiline blocks without having to put them in a separate |
|
94 | profile multiline blocks without having to put them in a separate | |
95 | function. |
|
95 | function. | |
96 |
|
96 | |||
97 | The given statement (which doesn't require quote marks) is run via the |
|
97 | The given statement (which doesn't require quote marks) is run via the | |
98 | python profiler in a manner similar to the profile.run() function. |
|
98 | python profiler in a manner similar to the profile.run() function. | |
99 | Namespaces are internally managed to work correctly; profile.run |
|
99 | Namespaces are internally managed to work correctly; profile.run | |
100 | cannot be used in IPython because it makes certain assumptions about |
|
100 | cannot be used in IPython because it makes certain assumptions about | |
101 | namespaces which do not hold under IPython. |
|
101 | namespaces which do not hold under IPython. | |
102 |
|
102 | |||
103 | Options: |
|
103 | Options: | |
104 |
|
104 | |||
105 | -l <limit>: you can place restrictions on what or how much of the |
|
105 | -l <limit>: you can place restrictions on what or how much of the | |
106 | profile gets printed. The limit value can be: |
|
106 | profile gets printed. The limit value can be: | |
107 |
|
107 | |||
108 | * A string: only information for function names containing this string |
|
108 | * A string: only information for function names containing this string | |
109 | is printed. |
|
109 | is printed. | |
110 |
|
110 | |||
111 | * An integer: only these many lines are printed. |
|
111 | * An integer: only these many lines are printed. | |
112 |
|
112 | |||
113 | * A float (between 0 and 1): this fraction of the report is printed |
|
113 | * A float (between 0 and 1): this fraction of the report is printed | |
114 | (for example, use a limit of 0.4 to see the topmost 40% only). |
|
114 | (for example, use a limit of 0.4 to see the topmost 40% only). | |
115 |
|
115 | |||
116 | You can combine several limits with repeated use of the option. For |
|
116 | You can combine several limits with repeated use of the option. For | |
117 | example, '-l __init__ -l 5' will print only the topmost 5 lines of |
|
117 | example, '-l __init__ -l 5' will print only the topmost 5 lines of | |
118 | information about class constructors. |
|
118 | information about class constructors. | |
119 |
|
119 | |||
120 | -r: return the pstats.Stats object generated by the profiling. This |
|
120 | -r: return the pstats.Stats object generated by the profiling. This | |
121 | object has all the information about the profile in it, and you can |
|
121 | object has all the information about the profile in it, and you can | |
122 | later use it for further analysis or in other functions. |
|
122 | later use it for further analysis or in other functions. | |
123 |
|
123 | |||
124 | -s <key>: sort profile by given key. You can provide more than one key |
|
124 | -s <key>: sort profile by given key. You can provide more than one key | |
125 | by using the option several times: '-s key1 -s key2 -s key3...'. The |
|
125 | by using the option several times: '-s key1 -s key2 -s key3...'. The | |
126 | default sorting key is 'time'. |
|
126 | default sorting key is 'time'. | |
127 |
|
127 | |||
128 | The following is copied verbatim from the profile documentation |
|
128 | The following is copied verbatim from the profile documentation | |
129 | referenced below: |
|
129 | referenced below: | |
130 |
|
130 | |||
131 | When more than one key is provided, additional keys are used as |
|
131 | When more than one key is provided, additional keys are used as | |
132 | secondary criteria when the there is equality in all keys selected |
|
132 | secondary criteria when the there is equality in all keys selected | |
133 | before them. |
|
133 | before them. | |
134 |
|
134 | |||
135 | Abbreviations can be used for any key names, as long as the |
|
135 | Abbreviations can be used for any key names, as long as the | |
136 | abbreviation is unambiguous. The following are the keys currently |
|
136 | abbreviation is unambiguous. The following are the keys currently | |
137 | defined: |
|
137 | defined: | |
138 |
|
138 | |||
139 | Valid Arg Meaning |
|
139 | Valid Arg Meaning | |
140 | "calls" call count |
|
140 | "calls" call count | |
141 | "cumulative" cumulative time |
|
141 | "cumulative" cumulative time | |
142 | "file" file name |
|
142 | "file" file name | |
143 | "module" file name |
|
143 | "module" file name | |
144 | "pcalls" primitive call count |
|
144 | "pcalls" primitive call count | |
145 | "line" line number |
|
145 | "line" line number | |
146 | "name" function name |
|
146 | "name" function name | |
147 | "nfl" name/file/line |
|
147 | "nfl" name/file/line | |
148 | "stdname" standard name |
|
148 | "stdname" standard name | |
149 | "time" internal time |
|
149 | "time" internal time | |
150 |
|
150 | |||
151 | Note that all sorts on statistics are in descending order (placing |
|
151 | Note that all sorts on statistics are in descending order (placing | |
152 | most time consuming items first), where as name, file, and line number |
|
152 | most time consuming items first), where as name, file, and line number | |
153 | searches are in ascending order (i.e., alphabetical). The subtle |
|
153 | searches are in ascending order (i.e., alphabetical). The subtle | |
154 | distinction between "nfl" and "stdname" is that the standard name is a |
|
154 | distinction between "nfl" and "stdname" is that the standard name is a | |
155 | sort of the name as printed, which means that the embedded line |
|
155 | sort of the name as printed, which means that the embedded line | |
156 | numbers get compared in an odd way. For example, lines 3, 20, and 40 |
|
156 | numbers get compared in an odd way. For example, lines 3, 20, and 40 | |
157 | would (if the file names were the same) appear in the string order |
|
157 | would (if the file names were the same) appear in the string order | |
158 | "20" "3" and "40". In contrast, "nfl" does a numeric compare of the |
|
158 | "20" "3" and "40". In contrast, "nfl" does a numeric compare of the | |
159 | line numbers. In fact, sort_stats("nfl") is the same as |
|
159 | line numbers. In fact, sort_stats("nfl") is the same as | |
160 | sort_stats("name", "file", "line"). |
|
160 | sort_stats("name", "file", "line"). | |
161 |
|
161 | |||
162 | -T <filename>: save profile results as shown on screen to a text |
|
162 | -T <filename>: save profile results as shown on screen to a text | |
163 | file. The profile is still shown on screen. |
|
163 | file. The profile is still shown on screen. | |
164 |
|
164 | |||
165 | -D <filename>: save (via dump_stats) profile statistics to given |
|
165 | -D <filename>: save (via dump_stats) profile statistics to given | |
166 | filename. This data is in a format understood by the pstats module, and |
|
166 | filename. This data is in a format understood by the pstats module, and | |
167 | is generated by a call to the dump_stats() method of profile |
|
167 | is generated by a call to the dump_stats() method of profile | |
168 | objects. The profile is still shown on screen. |
|
168 | objects. The profile is still shown on screen. | |
169 |
|
169 | |||
170 | -q: suppress output to the pager. Best used with -T and/or -D above. |
|
170 | -q: suppress output to the pager. Best used with -T and/or -D above. | |
171 |
|
171 | |||
172 | If you want to run complete programs under the profiler's control, use |
|
172 | If you want to run complete programs under the profiler's control, use | |
173 | '%run -p [prof_opts] filename.py [args to program]' where prof_opts |
|
173 | '%run -p [prof_opts] filename.py [args to program]' where prof_opts | |
174 | contains profiler specific options as described here. |
|
174 | contains profiler specific options as described here. | |
175 |
|
175 | |||
176 | You can read the complete documentation for the profile module with:: |
|
176 | You can read the complete documentation for the profile module with:: | |
177 |
|
177 | |||
178 | In [1]: import profile; profile.help() |
|
178 | In [1]: import profile; profile.help() | |
179 | """ |
|
179 | """ | |
180 | opts, arg_str = self.parse_options(parameter_s, 'D:l:rs:T:q', |
|
180 | opts, arg_str = self.parse_options(parameter_s, 'D:l:rs:T:q', | |
181 | list_all=True, posix=False) |
|
181 | list_all=True, posix=False) | |
182 | if cell is not None: |
|
182 | if cell is not None: | |
183 | arg_str += '\n' + cell |
|
183 | arg_str += '\n' + cell | |
184 | arg_str = self.shell.input_splitter.transform_cell(arg_str) |
|
184 | arg_str = self.shell.input_splitter.transform_cell(arg_str) | |
185 | return self._run_with_profiler(arg_str, opts, self.shell.user_ns) |
|
185 | return self._run_with_profiler(arg_str, opts, self.shell.user_ns) | |
186 |
|
186 | |||
187 | def _run_with_profiler(self, code, opts, namespace): |
|
187 | def _run_with_profiler(self, code, opts, namespace): | |
188 | """ |
|
188 | """ | |
189 | Run `code` with profiler. Used by ``%prun`` and ``%run -p``. |
|
189 | Run `code` with profiler. Used by ``%prun`` and ``%run -p``. | |
190 |
|
190 | |||
191 | Parameters |
|
191 | Parameters | |
192 | ---------- |
|
192 | ---------- | |
193 | code : str |
|
193 | code : str | |
194 | Code to be executed. |
|
194 | Code to be executed. | |
195 | opts : Struct |
|
195 | opts : Struct | |
196 | Options parsed by `self.parse_options`. |
|
196 | Options parsed by `self.parse_options`. | |
197 | namespace : dict |
|
197 | namespace : dict | |
198 | A dictionary for Python namespace (e.g., `self.shell.user_ns`). |
|
198 | A dictionary for Python namespace (e.g., `self.shell.user_ns`). | |
199 |
|
199 | |||
200 | """ |
|
200 | """ | |
201 |
|
201 | |||
202 | # Fill default values for unspecified options: |
|
202 | # Fill default values for unspecified options: | |
203 | opts.merge(Struct(D=[''], l=[], s=['time'], T=[''])) |
|
203 | opts.merge(Struct(D=[''], l=[], s=['time'], T=[''])) | |
204 |
|
204 | |||
205 | prof = profile.Profile() |
|
205 | prof = profile.Profile() | |
206 | try: |
|
206 | try: | |
207 | prof = prof.runctx(code, namespace, namespace) |
|
207 | prof = prof.runctx(code, namespace, namespace) | |
208 | sys_exit = '' |
|
208 | sys_exit = '' | |
209 | except SystemExit: |
|
209 | except SystemExit: | |
210 | sys_exit = """*** SystemExit exception caught in code being profiled.""" |
|
210 | sys_exit = """*** SystemExit exception caught in code being profiled.""" | |
211 |
|
211 | |||
212 | stats = pstats.Stats(prof).strip_dirs().sort_stats(*opts.s) |
|
212 | stats = pstats.Stats(prof).strip_dirs().sort_stats(*opts.s) | |
213 |
|
213 | |||
214 | lims = opts.l |
|
214 | lims = opts.l | |
215 | if lims: |
|
215 | if lims: | |
216 | lims = [] # rebuild lims with ints/floats/strings |
|
216 | lims = [] # rebuild lims with ints/floats/strings | |
217 | for lim in opts.l: |
|
217 | for lim in opts.l: | |
218 | try: |
|
218 | try: | |
219 | lims.append(int(lim)) |
|
219 | lims.append(int(lim)) | |
220 | except ValueError: |
|
220 | except ValueError: | |
221 | try: |
|
221 | try: | |
222 | lims.append(float(lim)) |
|
222 | lims.append(float(lim)) | |
223 | except ValueError: |
|
223 | except ValueError: | |
224 | lims.append(lim) |
|
224 | lims.append(lim) | |
225 |
|
225 | |||
226 | # Trap output. |
|
226 | # Trap output. | |
227 | stdout_trap = StringIO() |
|
227 | stdout_trap = StringIO() | |
228 | stats_stream = stats.stream |
|
228 | stats_stream = stats.stream | |
229 | try: |
|
229 | try: | |
230 | stats.stream = stdout_trap |
|
230 | stats.stream = stdout_trap | |
231 | stats.print_stats(*lims) |
|
231 | stats.print_stats(*lims) | |
232 | finally: |
|
232 | finally: | |
233 | stats.stream = stats_stream |
|
233 | stats.stream = stats_stream | |
234 |
|
234 | |||
235 | output = stdout_trap.getvalue() |
|
235 | output = stdout_trap.getvalue() | |
236 | output = output.rstrip() |
|
236 | output = output.rstrip() | |
237 |
|
237 | |||
238 | if 'q' not in opts: |
|
238 | if 'q' not in opts: | |
239 | page.page(output) |
|
239 | page.page(output) | |
240 | print sys_exit, |
|
240 | print sys_exit, | |
241 |
|
241 | |||
242 | dump_file = opts.D[0] |
|
242 | dump_file = opts.D[0] | |
243 | text_file = opts.T[0] |
|
243 | text_file = opts.T[0] | |
244 | if dump_file: |
|
244 | if dump_file: | |
245 | dump_file = unquote_filename(dump_file) |
|
245 | dump_file = unquote_filename(dump_file) | |
246 | prof.dump_stats(dump_file) |
|
246 | prof.dump_stats(dump_file) | |
247 | print '\n*** Profile stats marshalled to file',\ |
|
247 | print '\n*** Profile stats marshalled to file',\ | |
248 | repr(dump_file)+'.',sys_exit |
|
248 | repr(dump_file)+'.',sys_exit | |
249 | if text_file: |
|
249 | if text_file: | |
250 | text_file = unquote_filename(text_file) |
|
250 | text_file = unquote_filename(text_file) | |
251 | pfile = open(text_file,'w') |
|
251 | pfile = open(text_file,'w') | |
252 | pfile.write(output) |
|
252 | pfile.write(output) | |
253 | pfile.close() |
|
253 | pfile.close() | |
254 | print '\n*** Profile printout saved to text file',\ |
|
254 | print '\n*** Profile printout saved to text file',\ | |
255 | repr(text_file)+'.',sys_exit |
|
255 | repr(text_file)+'.',sys_exit | |
256 |
|
256 | |||
257 | if 'r' in opts: |
|
257 | if 'r' in opts: | |
258 | return stats |
|
258 | return stats | |
259 | else: |
|
259 | else: | |
260 | return None |
|
260 | return None | |
261 |
|
261 | |||
262 | @line_magic |
|
262 | @line_magic | |
263 | def pdb(self, parameter_s=''): |
|
263 | def pdb(self, parameter_s=''): | |
264 | """Control the automatic calling of the pdb interactive debugger. |
|
264 | """Control the automatic calling of the pdb interactive debugger. | |
265 |
|
265 | |||
266 | Call as '%pdb on', '%pdb 1', '%pdb off' or '%pdb 0'. If called without |
|
266 | Call as '%pdb on', '%pdb 1', '%pdb off' or '%pdb 0'. If called without | |
267 | argument it works as a toggle. |
|
267 | argument it works as a toggle. | |
268 |
|
268 | |||
269 | When an exception is triggered, IPython can optionally call the |
|
269 | When an exception is triggered, IPython can optionally call the | |
270 | interactive pdb debugger after the traceback printout. %pdb toggles |
|
270 | interactive pdb debugger after the traceback printout. %pdb toggles | |
271 | this feature on and off. |
|
271 | this feature on and off. | |
272 |
|
272 | |||
273 | The initial state of this feature is set in your configuration |
|
273 | The initial state of this feature is set in your configuration | |
274 | file (the option is ``InteractiveShell.pdb``). |
|
274 | file (the option is ``InteractiveShell.pdb``). | |
275 |
|
275 | |||
276 | If you want to just activate the debugger AFTER an exception has fired, |
|
276 | If you want to just activate the debugger AFTER an exception has fired, | |
277 | without having to type '%pdb on' and rerunning your code, you can use |
|
277 | without having to type '%pdb on' and rerunning your code, you can use | |
278 | the %debug magic.""" |
|
278 | the %debug magic.""" | |
279 |
|
279 | |||
280 | par = parameter_s.strip().lower() |
|
280 | par = parameter_s.strip().lower() | |
281 |
|
281 | |||
282 | if par: |
|
282 | if par: | |
283 | try: |
|
283 | try: | |
284 | new_pdb = {'off':0,'0':0,'on':1,'1':1}[par] |
|
284 | new_pdb = {'off':0,'0':0,'on':1,'1':1}[par] | |
285 | except KeyError: |
|
285 | except KeyError: | |
286 | print ('Incorrect argument. Use on/1, off/0, ' |
|
286 | print ('Incorrect argument. Use on/1, off/0, ' | |
287 | 'or nothing for a toggle.') |
|
287 | 'or nothing for a toggle.') | |
288 | return |
|
288 | return | |
289 | else: |
|
289 | else: | |
290 | # toggle |
|
290 | # toggle | |
291 | new_pdb = not self.shell.call_pdb |
|
291 | new_pdb = not self.shell.call_pdb | |
292 |
|
292 | |||
293 | # set on the shell |
|
293 | # set on the shell | |
294 | self.shell.call_pdb = new_pdb |
|
294 | self.shell.call_pdb = new_pdb | |
295 | print 'Automatic pdb calling has been turned',on_off(new_pdb) |
|
295 | print 'Automatic pdb calling has been turned',on_off(new_pdb) | |
296 |
|
296 | |||
297 | @skip_doctest |
|
297 | @skip_doctest | |
298 | @magic_arguments.magic_arguments() |
|
298 | @magic_arguments.magic_arguments() | |
299 | @magic_arguments.argument('--breakpoint', '-b', metavar='FILE:LINE', |
|
299 | @magic_arguments.argument('--breakpoint', '-b', metavar='FILE:LINE', | |
300 | help=""" |
|
300 | help=""" | |
301 | Set break point at LINE in FILE. |
|
301 | Set break point at LINE in FILE. | |
302 | """ |
|
302 | """ | |
303 | ) |
|
303 | ) | |
304 | @magic_arguments.argument('statement', nargs='*', |
|
304 | @magic_arguments.argument('statement', nargs='*', | |
305 | help=""" |
|
305 | help=""" | |
306 | Code to run in debugger. |
|
306 | Code to run in debugger. | |
307 | You can omit this in cell magic mode. |
|
307 | You can omit this in cell magic mode. | |
308 | """ |
|
308 | """ | |
309 | ) |
|
309 | ) | |
310 | @line_cell_magic |
|
310 | @line_cell_magic | |
311 | def debug(self, line='', cell=None): |
|
311 | def debug(self, line='', cell=None): | |
312 | """Activate the interactive debugger. |
|
312 | """Activate the interactive debugger. | |
313 |
|
313 | |||
314 | This magic command support two ways of activating debugger. |
|
314 | This magic command support two ways of activating debugger. | |
315 | One is to activate debugger before executing code. This way, you |
|
315 | One is to activate debugger before executing code. This way, you | |
316 | can set a break point, to step through the code from the point. |
|
316 | can set a break point, to step through the code from the point. | |
317 | You can use this mode by giving statements to execute and optionally |
|
317 | You can use this mode by giving statements to execute and optionally | |
318 | a breakpoint. |
|
318 | a breakpoint. | |
319 |
|
319 | |||
320 | The other one is to activate debugger in post-mortem mode. You can |
|
320 | The other one is to activate debugger in post-mortem mode. You can | |
321 | activate this mode simply running %debug without any argument. |
|
321 | activate this mode simply running %debug without any argument. | |
322 | If an exception has just occurred, this lets you inspect its stack |
|
322 | If an exception has just occurred, this lets you inspect its stack | |
323 | frames interactively. Note that this will always work only on the last |
|
323 | frames interactively. Note that this will always work only on the last | |
324 | traceback that occurred, so you must call this quickly after an |
|
324 | traceback that occurred, so you must call this quickly after an | |
325 | exception that you wish to inspect has fired, because if another one |
|
325 | exception that you wish to inspect has fired, because if another one | |
326 | occurs, it clobbers the previous one. |
|
326 | occurs, it clobbers the previous one. | |
327 |
|
327 | |||
328 | If you want IPython to automatically do this on every exception, see |
|
328 | If you want IPython to automatically do this on every exception, see | |
329 | the %pdb magic for more details. |
|
329 | the %pdb magic for more details. | |
330 | """ |
|
330 | """ | |
331 | args = magic_arguments.parse_argstring(self.debug, line) |
|
331 | args = magic_arguments.parse_argstring(self.debug, line) | |
332 |
|
332 | |||
333 | if not (args.breakpoint or args.statement or cell): |
|
333 | if not (args.breakpoint or args.statement or cell): | |
334 | self._debug_post_mortem() |
|
334 | self._debug_post_mortem() | |
335 | else: |
|
335 | else: | |
336 | code = "\n".join(args.statement) |
|
336 | code = "\n".join(args.statement) | |
337 | if cell: |
|
337 | if cell: | |
338 | code += "\n" + cell |
|
338 | code += "\n" + cell | |
339 | self._debug_exec(code, args.breakpoint) |
|
339 | self._debug_exec(code, args.breakpoint) | |
340 |
|
340 | |||
341 | def _debug_post_mortem(self): |
|
341 | def _debug_post_mortem(self): | |
342 | self.shell.debugger(force=True) |
|
342 | self.shell.debugger(force=True) | |
343 |
|
343 | |||
344 | def _debug_exec(self, code, breakpoint): |
|
344 | def _debug_exec(self, code, breakpoint): | |
345 | if breakpoint: |
|
345 | if breakpoint: | |
346 | (filename, bp_line) = breakpoint.split(':', 1) |
|
346 | (filename, bp_line) = breakpoint.split(':', 1) | |
347 | bp_line = int(bp_line) |
|
347 | bp_line = int(bp_line) | |
348 | else: |
|
348 | else: | |
349 | (filename, bp_line) = (None, None) |
|
349 | (filename, bp_line) = (None, None) | |
350 | self._run_with_debugger(code, self.shell.user_ns, filename, bp_line) |
|
350 | self._run_with_debugger(code, self.shell.user_ns, filename, bp_line) | |
351 |
|
351 | |||
352 | @line_magic |
|
352 | @line_magic | |
353 | def tb(self, s): |
|
353 | def tb(self, s): | |
354 | """Print the last traceback with the currently active exception mode. |
|
354 | """Print the last traceback with the currently active exception mode. | |
355 |
|
355 | |||
356 | See %xmode for changing exception reporting modes.""" |
|
356 | See %xmode for changing exception reporting modes.""" | |
357 | self.shell.showtraceback() |
|
357 | self.shell.showtraceback() | |
358 |
|
358 | |||
359 | @skip_doctest |
|
359 | @skip_doctest | |
360 | @line_magic |
|
360 | @line_magic | |
361 | def run(self, parameter_s='', runner=None, |
|
361 | def run(self, parameter_s='', runner=None, | |
362 | file_finder=get_py_filename): |
|
362 | file_finder=get_py_filename): | |
363 | """Run the named file inside IPython as a program. |
|
363 | """Run the named file inside IPython as a program. | |
364 |
|
364 | |||
365 | Usage: |
|
365 | Usage: | |
366 | %run [-n -i -e -G] |
|
366 | %run [-n -i -e -G] | |
367 | [( -t [-N<N>] | -d [-b<N>] | -p [profile options] )] |
|
367 | [( -t [-N<N>] | -d [-b<N>] | -p [profile options] )] | |
368 | ( -m mod | file ) [args] |
|
368 | ( -m mod | file ) [args] | |
369 |
|
369 | |||
370 | Parameters after the filename are passed as command-line arguments to |
|
370 | Parameters after the filename are passed as command-line arguments to | |
371 | the program (put in sys.argv). Then, control returns to IPython's |
|
371 | the program (put in sys.argv). Then, control returns to IPython's | |
372 | prompt. |
|
372 | prompt. | |
373 |
|
373 | |||
374 | This is similar to running at a system prompt:\\ |
|
374 | This is similar to running at a system prompt:\\ | |
375 | $ python file args\\ |
|
375 | $ python file args\\ | |
376 | but with the advantage of giving you IPython's tracebacks, and of |
|
376 | but with the advantage of giving you IPython's tracebacks, and of | |
377 | loading all variables into your interactive namespace for further use |
|
377 | loading all variables into your interactive namespace for further use | |
378 | (unless -p is used, see below). |
|
378 | (unless -p is used, see below). | |
379 |
|
379 | |||
380 | The file is executed in a namespace initially consisting only of |
|
380 | The file is executed in a namespace initially consisting only of | |
381 | __name__=='__main__' and sys.argv constructed as indicated. It thus |
|
381 | __name__=='__main__' and sys.argv constructed as indicated. It thus | |
382 | sees its environment as if it were being run as a stand-alone program |
|
382 | sees its environment as if it were being run as a stand-alone program | |
383 | (except for sharing global objects such as previously imported |
|
383 | (except for sharing global objects such as previously imported | |
384 | modules). But after execution, the IPython interactive namespace gets |
|
384 | modules). But after execution, the IPython interactive namespace gets | |
385 | updated with all variables defined in the program (except for __name__ |
|
385 | updated with all variables defined in the program (except for __name__ | |
386 | and sys.argv). This allows for very convenient loading of code for |
|
386 | and sys.argv). This allows for very convenient loading of code for | |
387 | interactive work, while giving each program a 'clean sheet' to run in. |
|
387 | interactive work, while giving each program a 'clean sheet' to run in. | |
388 |
|
388 | |||
389 | Arguments are expanded using shell-like glob match. Patterns |
|
389 | Arguments are expanded using shell-like glob match. Patterns | |
390 | '*', '?', '[seq]' and '[!seq]' can be used. Additionally, |
|
390 | '*', '?', '[seq]' and '[!seq]' can be used. Additionally, | |
391 | tilde '~' will be expanded into user's home directory. Unlike |
|
391 | tilde '~' will be expanded into user's home directory. Unlike | |
392 | real shells, quotation does not suppress expansions. Use |
|
392 | real shells, quotation does not suppress expansions. Use | |
393 | *two* back slashes (e.g., '\\\\*') to suppress expansions. |
|
393 | *two* back slashes (e.g., '\\\\*') to suppress expansions. | |
394 | To completely disable these expansions, you can use -G flag. |
|
394 | To completely disable these expansions, you can use -G flag. | |
395 |
|
395 | |||
396 | Options: |
|
396 | Options: | |
397 |
|
397 | |||
398 | -n: __name__ is NOT set to '__main__', but to the running file's name |
|
398 | -n: __name__ is NOT set to '__main__', but to the running file's name | |
399 | without extension (as python does under import). This allows running |
|
399 | without extension (as python does under import). This allows running | |
400 | scripts and reloading the definitions in them without calling code |
|
400 | scripts and reloading the definitions in them without calling code | |
401 | protected by an ' if __name__ == "__main__" ' clause. |
|
401 | protected by an ' if __name__ == "__main__" ' clause. | |
402 |
|
402 | |||
403 | -i: run the file in IPython's namespace instead of an empty one. This |
|
403 | -i: run the file in IPython's namespace instead of an empty one. This | |
404 | is useful if you are experimenting with code written in a text editor |
|
404 | is useful if you are experimenting with code written in a text editor | |
405 | which depends on variables defined interactively. |
|
405 | which depends on variables defined interactively. | |
406 |
|
406 | |||
407 | -e: ignore sys.exit() calls or SystemExit exceptions in the script |
|
407 | -e: ignore sys.exit() calls or SystemExit exceptions in the script | |
408 | being run. This is particularly useful if IPython is being used to |
|
408 | being run. This is particularly useful if IPython is being used to | |
409 | run unittests, which always exit with a sys.exit() call. In such |
|
409 | run unittests, which always exit with a sys.exit() call. In such | |
410 | cases you are interested in the output of the test results, not in |
|
410 | cases you are interested in the output of the test results, not in | |
411 | seeing a traceback of the unittest module. |
|
411 | seeing a traceback of the unittest module. | |
412 |
|
412 | |||
413 | -t: print timing information at the end of the run. IPython will give |
|
413 | -t: print timing information at the end of the run. IPython will give | |
414 | you an estimated CPU time consumption for your script, which under |
|
414 | you an estimated CPU time consumption for your script, which under | |
415 | Unix uses the resource module to avoid the wraparound problems of |
|
415 | Unix uses the resource module to avoid the wraparound problems of | |
416 | time.clock(). Under Unix, an estimate of time spent on system tasks |
|
416 | time.clock(). Under Unix, an estimate of time spent on system tasks | |
417 | is also given (for Windows platforms this is reported as 0.0). |
|
417 | is also given (for Windows platforms this is reported as 0.0). | |
418 |
|
418 | |||
419 | If -t is given, an additional -N<N> option can be given, where <N> |
|
419 | If -t is given, an additional -N<N> option can be given, where <N> | |
420 | must be an integer indicating how many times you want the script to |
|
420 | must be an integer indicating how many times you want the script to | |
421 | run. The final timing report will include total and per run results. |
|
421 | run. The final timing report will include total and per run results. | |
422 |
|
422 | |||
423 | For example (testing the script uniq_stable.py):: |
|
423 | For example (testing the script uniq_stable.py):: | |
424 |
|
424 | |||
425 | In [1]: run -t uniq_stable |
|
425 | In [1]: run -t uniq_stable | |
426 |
|
426 | |||
427 | IPython CPU timings (estimated):\\ |
|
427 | IPython CPU timings (estimated):\\ | |
428 | User : 0.19597 s.\\ |
|
428 | User : 0.19597 s.\\ | |
429 | System: 0.0 s.\\ |
|
429 | System: 0.0 s.\\ | |
430 |
|
430 | |||
431 | In [2]: run -t -N5 uniq_stable |
|
431 | In [2]: run -t -N5 uniq_stable | |
432 |
|
432 | |||
433 | IPython CPU timings (estimated):\\ |
|
433 | IPython CPU timings (estimated):\\ | |
434 | Total runs performed: 5\\ |
|
434 | Total runs performed: 5\\ | |
435 | Times : Total Per run\\ |
|
435 | Times : Total Per run\\ | |
436 | User : 0.910862 s, 0.1821724 s.\\ |
|
436 | User : 0.910862 s, 0.1821724 s.\\ | |
437 | System: 0.0 s, 0.0 s. |
|
437 | System: 0.0 s, 0.0 s. | |
438 |
|
438 | |||
439 | -d: run your program under the control of pdb, the Python debugger. |
|
439 | -d: run your program under the control of pdb, the Python debugger. | |
440 | This allows you to execute your program step by step, watch variables, |
|
440 | This allows you to execute your program step by step, watch variables, | |
441 | etc. Internally, what IPython does is similar to calling: |
|
441 | etc. Internally, what IPython does is similar to calling: | |
442 |
|
442 | |||
443 | pdb.run('execfile("YOURFILENAME")') |
|
443 | pdb.run('execfile("YOURFILENAME")') | |
444 |
|
444 | |||
445 | with a breakpoint set on line 1 of your file. You can change the line |
|
445 | with a breakpoint set on line 1 of your file. You can change the line | |
446 | number for this automatic breakpoint to be <N> by using the -bN option |
|
446 | number for this automatic breakpoint to be <N> by using the -bN option | |
447 | (where N must be an integer). For example:: |
|
447 | (where N must be an integer). For example:: | |
448 |
|
448 | |||
449 | %run -d -b40 myscript |
|
449 | %run -d -b40 myscript | |
450 |
|
450 | |||
451 | will set the first breakpoint at line 40 in myscript.py. Note that |
|
451 | will set the first breakpoint at line 40 in myscript.py. Note that | |
452 | the first breakpoint must be set on a line which actually does |
|
452 | the first breakpoint must be set on a line which actually does | |
453 | something (not a comment or docstring) for it to stop execution. |
|
453 | something (not a comment or docstring) for it to stop execution. | |
454 |
|
454 | |||
455 | Or you can specify a breakpoint in a different file:: |
|
455 | Or you can specify a breakpoint in a different file:: | |
456 |
|
456 | |||
457 | %run -d -b myotherfile.py:20 myscript |
|
457 | %run -d -b myotherfile.py:20 myscript | |
458 |
|
458 | |||
459 | When the pdb debugger starts, you will see a (Pdb) prompt. You must |
|
459 | When the pdb debugger starts, you will see a (Pdb) prompt. You must | |
460 | first enter 'c' (without quotes) to start execution up to the first |
|
460 | first enter 'c' (without quotes) to start execution up to the first | |
461 | breakpoint. |
|
461 | breakpoint. | |
462 |
|
462 | |||
463 | Entering 'help' gives information about the use of the debugger. You |
|
463 | Entering 'help' gives information about the use of the debugger. You | |
464 | can easily see pdb's full documentation with "import pdb;pdb.help()" |
|
464 | can easily see pdb's full documentation with "import pdb;pdb.help()" | |
465 | at a prompt. |
|
465 | at a prompt. | |
466 |
|
466 | |||
467 | -p: run program under the control of the Python profiler module (which |
|
467 | -p: run program under the control of the Python profiler module (which | |
468 | prints a detailed report of execution times, function calls, etc). |
|
468 | prints a detailed report of execution times, function calls, etc). | |
469 |
|
469 | |||
470 | You can pass other options after -p which affect the behavior of the |
|
470 | You can pass other options after -p which affect the behavior of the | |
471 | profiler itself. See the docs for %prun for details. |
|
471 | profiler itself. See the docs for %prun for details. | |
472 |
|
472 | |||
473 | In this mode, the program's variables do NOT propagate back to the |
|
473 | In this mode, the program's variables do NOT propagate back to the | |
474 | IPython interactive namespace (because they remain in the namespace |
|
474 | IPython interactive namespace (because they remain in the namespace | |
475 | where the profiler executes them). |
|
475 | where the profiler executes them). | |
476 |
|
476 | |||
477 | Internally this triggers a call to %prun, see its documentation for |
|
477 | Internally this triggers a call to %prun, see its documentation for | |
478 | details on the options available specifically for profiling. |
|
478 | details on the options available specifically for profiling. | |
479 |
|
479 | |||
480 | There is one special usage for which the text above doesn't apply: |
|
480 | There is one special usage for which the text above doesn't apply: | |
481 | if the filename ends with .ipy, the file is run as ipython script, |
|
481 | if the filename ends with .ipy, the file is run as ipython script, | |
482 | just as if the commands were written on IPython prompt. |
|
482 | just as if the commands were written on IPython prompt. | |
483 |
|
483 | |||
484 | -m: specify module name to load instead of script path. Similar to |
|
484 | -m: specify module name to load instead of script path. Similar to | |
485 | the -m option for the python interpreter. Use this option last if you |
|
485 | the -m option for the python interpreter. Use this option last if you | |
486 | want to combine with other %run options. Unlike the python interpreter |
|
486 | want to combine with other %run options. Unlike the python interpreter | |
487 | only source modules are allowed no .pyc or .pyo files. |
|
487 | only source modules are allowed no .pyc or .pyo files. | |
488 | For example:: |
|
488 | For example:: | |
489 |
|
489 | |||
490 | %run -m example |
|
490 | %run -m example | |
491 |
|
491 | |||
492 | will run the example module. |
|
492 | will run the example module. | |
493 |
|
493 | |||
494 | -G: disable shell-like glob expansion of arguments. |
|
494 | -G: disable shell-like glob expansion of arguments. | |
495 |
|
495 | |||
496 | """ |
|
496 | """ | |
497 |
|
497 | |||
498 | # get arguments and set sys.argv for program to be run. |
|
498 | # get arguments and set sys.argv for program to be run. | |
499 | opts, arg_lst = self.parse_options(parameter_s, |
|
499 | opts, arg_lst = self.parse_options(parameter_s, | |
500 | 'nidtN:b:pD:l:rs:T:em:G', |
|
500 | 'nidtN:b:pD:l:rs:T:em:G', | |
501 | mode='list', list_all=1) |
|
501 | mode='list', list_all=1) | |
502 | if "m" in opts: |
|
502 | if "m" in opts: | |
503 | modulename = opts["m"][0] |
|
503 | modulename = opts["m"][0] | |
504 | modpath = find_mod(modulename) |
|
504 | modpath = find_mod(modulename) | |
505 | if modpath is None: |
|
505 | if modpath is None: | |
506 | warn('%r is not a valid modulename on sys.path'%modulename) |
|
506 | warn('%r is not a valid modulename on sys.path'%modulename) | |
507 | return |
|
507 | return | |
508 | arg_lst = [modpath] + arg_lst |
|
508 | arg_lst = [modpath] + arg_lst | |
509 | try: |
|
509 | try: | |
510 | filename = file_finder(arg_lst[0]) |
|
510 | filename = file_finder(arg_lst[0]) | |
511 | except IndexError: |
|
511 | except IndexError: | |
512 | warn('you must provide at least a filename.') |
|
512 | warn('you must provide at least a filename.') | |
513 | print '\n%run:\n', oinspect.getdoc(self.run) |
|
513 | print '\n%run:\n', oinspect.getdoc(self.run) | |
514 | return |
|
514 | return | |
515 | except IOError as e: |
|
515 | except IOError as e: | |
516 | try: |
|
516 | try: | |
517 | msg = str(e) |
|
517 | msg = str(e) | |
518 | except UnicodeError: |
|
518 | except UnicodeError: | |
519 | msg = e.message |
|
519 | msg = e.message | |
520 | error(msg) |
|
520 | error(msg) | |
521 | return |
|
521 | return | |
522 |
|
522 | |||
523 | if filename.lower().endswith('.ipy'): |
|
523 | if filename.lower().endswith('.ipy'): | |
524 | with preserve_keys(self.shell.user_ns, '__file__'): |
|
524 | with preserve_keys(self.shell.user_ns, '__file__'): | |
525 | self.shell.user_ns['__file__'] = filename |
|
525 | self.shell.user_ns['__file__'] = filename | |
526 | self.shell.safe_execfile_ipy(filename) |
|
526 | self.shell.safe_execfile_ipy(filename) | |
527 | return |
|
527 | return | |
528 |
|
528 | |||
529 | # Control the response to exit() calls made by the script being run |
|
529 | # Control the response to exit() calls made by the script being run | |
530 | exit_ignore = 'e' in opts |
|
530 | exit_ignore = 'e' in opts | |
531 |
|
531 | |||
532 | # Make sure that the running script gets a proper sys.argv as if it |
|
532 | # Make sure that the running script gets a proper sys.argv as if it | |
533 | # were run from a system shell. |
|
533 | # were run from a system shell. | |
534 | save_argv = sys.argv # save it for later restoring |
|
534 | save_argv = sys.argv # save it for later restoring | |
535 |
|
535 | |||
536 | if 'G' in opts: |
|
536 | if 'G' in opts: | |
537 | args = arg_lst[1:] |
|
537 | args = arg_lst[1:] | |
538 | else: |
|
538 | else: | |
539 | # tilde and glob expansion |
|
539 | # tilde and glob expansion | |
540 | args = shellglob(map(os.path.expanduser, arg_lst[1:])) |
|
540 | args = shellglob(map(os.path.expanduser, arg_lst[1:])) | |
541 |
|
541 | |||
542 | sys.argv = [filename] + args # put in the proper filename |
|
542 | sys.argv = [filename] + args # put in the proper filename | |
543 | # protect sys.argv from potential unicode strings on Python 2: |
|
543 | # protect sys.argv from potential unicode strings on Python 2: | |
544 | if not py3compat.PY3: |
|
544 | if not py3compat.PY3: | |
545 | sys.argv = [ py3compat.cast_bytes(a) for a in sys.argv ] |
|
545 | sys.argv = [ py3compat.cast_bytes(a) for a in sys.argv ] | |
546 |
|
546 | |||
547 | if 'i' in opts: |
|
547 | if 'i' in opts: | |
548 | # Run in user's interactive namespace |
|
548 | # Run in user's interactive namespace | |
549 | prog_ns = self.shell.user_ns |
|
549 | prog_ns = self.shell.user_ns | |
550 | __name__save = self.shell.user_ns['__name__'] |
|
550 | __name__save = self.shell.user_ns['__name__'] | |
551 | prog_ns['__name__'] = '__main__' |
|
551 | prog_ns['__name__'] = '__main__' | |
552 | main_mod = self.shell.user_module |
|
552 | main_mod = self.shell.user_module | |
|
553 | ||||
|
554 | # Since '%run foo' emulates 'python foo.py' at the cmd line, we must | |||
|
555 | # set the __file__ global in the script's namespace | |||
|
556 | # TK: Is this necessary in interactive mode? | |||
|
557 | prog_ns['__file__'] = filename | |||
553 | else: |
|
558 | else: | |
554 | # Run in a fresh, empty namespace |
|
559 | # Run in a fresh, empty namespace | |
555 | if 'n' in opts: |
|
560 | if 'n' in opts: | |
556 | name = os.path.splitext(os.path.basename(filename))[0] |
|
561 | name = os.path.splitext(os.path.basename(filename))[0] | |
557 | else: |
|
562 | else: | |
558 | name = '__main__' |
|
563 | name = '__main__' | |
559 |
|
564 | |||
560 | # The shell MUST hold a reference to prog_ns so after %run |
|
565 | # The shell MUST hold a reference to prog_ns so after %run | |
561 | # exits, the python deletion mechanism doesn't zero it out |
|
566 | # exits, the python deletion mechanism doesn't zero it out | |
562 | # (leaving dangling references). See interactiveshell for details |
|
567 | # (leaving dangling references). See interactiveshell for details | |
563 | main_mod = self.shell.new_main_mod(filename) |
|
568 | main_mod = self.shell.new_main_mod(filename, name) | |
564 | prog_ns = main_mod.__dict__ |
|
569 | prog_ns = main_mod.__dict__ | |
565 | prog_ns['__name__'] = name |
|
|||
566 |
|
||||
567 | # Since '%run foo' emulates 'python foo.py' at the cmd line, we must |
|
|||
568 | # set the __file__ global in the script's namespace |
|
|||
569 | prog_ns['__file__'] = filename |
|
|||
570 |
|
570 | |||
571 | # pickle fix. See interactiveshell for an explanation. But we need to |
|
571 | # pickle fix. See interactiveshell for an explanation. But we need to | |
572 | # make sure that, if we overwrite __main__, we replace it at the end |
|
572 | # make sure that, if we overwrite __main__, we replace it at the end | |
573 | main_mod_name = prog_ns['__name__'] |
|
573 | main_mod_name = prog_ns['__name__'] | |
574 |
|
574 | |||
575 | if main_mod_name == '__main__': |
|
575 | if main_mod_name == '__main__': | |
576 | restore_main = sys.modules['__main__'] |
|
576 | restore_main = sys.modules['__main__'] | |
577 | else: |
|
577 | else: | |
578 | restore_main = False |
|
578 | restore_main = False | |
579 |
|
579 | |||
580 | # This needs to be undone at the end to prevent holding references to |
|
580 | # This needs to be undone at the end to prevent holding references to | |
581 | # every single object ever created. |
|
581 | # every single object ever created. | |
582 | sys.modules[main_mod_name] = main_mod |
|
582 | sys.modules[main_mod_name] = main_mod | |
583 |
|
583 | |||
584 | if 'p' in opts or 'd' in opts: |
|
584 | if 'p' in opts or 'd' in opts: | |
585 | if 'm' in opts: |
|
585 | if 'm' in opts: | |
586 | code = 'run_module(modulename, prog_ns)' |
|
586 | code = 'run_module(modulename, prog_ns)' | |
587 | code_ns = { |
|
587 | code_ns = { | |
588 | 'run_module': self.shell.safe_run_module, |
|
588 | 'run_module': self.shell.safe_run_module, | |
589 | 'prog_ns': prog_ns, |
|
589 | 'prog_ns': prog_ns, | |
590 | 'modulename': modulename, |
|
590 | 'modulename': modulename, | |
591 | } |
|
591 | } | |
592 | else: |
|
592 | else: | |
593 | code = 'execfile(filename, prog_ns)' |
|
593 | code = 'execfile(filename, prog_ns)' | |
594 | code_ns = { |
|
594 | code_ns = { | |
595 | 'execfile': self.shell.safe_execfile, |
|
595 | 'execfile': self.shell.safe_execfile, | |
596 | 'prog_ns': prog_ns, |
|
596 | 'prog_ns': prog_ns, | |
597 | 'filename': get_py_filename(filename), |
|
597 | 'filename': get_py_filename(filename), | |
598 | } |
|
598 | } | |
599 |
|
599 | |||
600 | try: |
|
600 | try: | |
601 | stats = None |
|
601 | stats = None | |
602 | with self.shell.readline_no_record: |
|
602 | with self.shell.readline_no_record: | |
603 | if 'p' in opts: |
|
603 | if 'p' in opts: | |
604 | stats = self._run_with_profiler(code, opts, code_ns) |
|
604 | stats = self._run_with_profiler(code, opts, code_ns) | |
605 | else: |
|
605 | else: | |
606 | if 'd' in opts: |
|
606 | if 'd' in opts: | |
607 | bp_file, bp_line = parse_breakpoint( |
|
607 | bp_file, bp_line = parse_breakpoint( | |
608 | opts.get('b', ['1'])[0], filename) |
|
608 | opts.get('b', ['1'])[0], filename) | |
609 | self._run_with_debugger( |
|
609 | self._run_with_debugger( | |
610 | code, code_ns, filename, bp_line, bp_file) |
|
610 | code, code_ns, filename, bp_line, bp_file) | |
611 | else: |
|
611 | else: | |
612 | if 'm' in opts: |
|
612 | if 'm' in opts: | |
613 | def run(): |
|
613 | def run(): | |
614 | self.shell.safe_run_module(modulename, prog_ns) |
|
614 | self.shell.safe_run_module(modulename, prog_ns) | |
615 | else: |
|
615 | else: | |
616 | if runner is None: |
|
616 | if runner is None: | |
617 | runner = self.default_runner |
|
617 | runner = self.default_runner | |
618 | if runner is None: |
|
618 | if runner is None: | |
619 | runner = self.shell.safe_execfile |
|
619 | runner = self.shell.safe_execfile | |
620 |
|
620 | |||
621 | def run(): |
|
621 | def run(): | |
622 | runner(filename, prog_ns, prog_ns, |
|
622 | runner(filename, prog_ns, prog_ns, | |
623 | exit_ignore=exit_ignore) |
|
623 | exit_ignore=exit_ignore) | |
624 |
|
624 | |||
625 | if 't' in opts: |
|
625 | if 't' in opts: | |
626 | # timed execution |
|
626 | # timed execution | |
627 | try: |
|
627 | try: | |
628 | nruns = int(opts['N'][0]) |
|
628 | nruns = int(opts['N'][0]) | |
629 | if nruns < 1: |
|
629 | if nruns < 1: | |
630 | error('Number of runs must be >=1') |
|
630 | error('Number of runs must be >=1') | |
631 | return |
|
631 | return | |
632 | except (KeyError): |
|
632 | except (KeyError): | |
633 | nruns = 1 |
|
633 | nruns = 1 | |
634 | self._run_with_timing(run, nruns) |
|
634 | self._run_with_timing(run, nruns) | |
635 | else: |
|
635 | else: | |
636 | # regular execution |
|
636 | # regular execution | |
637 | run() |
|
637 | run() | |
638 |
|
638 | |||
639 | if 'i' in opts: |
|
639 | if 'i' in opts: | |
640 | self.shell.user_ns['__name__'] = __name__save |
|
640 | self.shell.user_ns['__name__'] = __name__save | |
641 | else: |
|
641 | else: | |
642 | # update IPython interactive namespace |
|
642 | # update IPython interactive namespace | |
643 |
|
643 | |||
644 | # Some forms of read errors on the file may mean the |
|
644 | # Some forms of read errors on the file may mean the | |
645 | # __name__ key was never set; using pop we don't have to |
|
645 | # __name__ key was never set; using pop we don't have to | |
646 | # worry about a possible KeyError. |
|
646 | # worry about a possible KeyError. | |
647 | prog_ns.pop('__name__', None) |
|
647 | prog_ns.pop('__name__', None) | |
648 |
|
648 | |||
649 | with preserve_keys(self.shell.user_ns, '__file__'): |
|
649 | with preserve_keys(self.shell.user_ns, '__file__'): | |
650 | self.shell.user_ns.update(prog_ns) |
|
650 | self.shell.user_ns.update(prog_ns) | |
651 | finally: |
|
651 | finally: | |
652 | # It's a bit of a mystery why, but __builtins__ can change from |
|
652 | # It's a bit of a mystery why, but __builtins__ can change from | |
653 | # being a module to becoming a dict missing some key data after |
|
653 | # being a module to becoming a dict missing some key data after | |
654 | # %run. As best I can see, this is NOT something IPython is doing |
|
654 | # %run. As best I can see, this is NOT something IPython is doing | |
655 | # at all, and similar problems have been reported before: |
|
655 | # at all, and similar problems have been reported before: | |
656 | # http://coding.derkeiler.com/Archive/Python/comp.lang.python/2004-10/0188.html |
|
656 | # http://coding.derkeiler.com/Archive/Python/comp.lang.python/2004-10/0188.html | |
657 | # Since this seems to be done by the interpreter itself, the best |
|
657 | # Since this seems to be done by the interpreter itself, the best | |
658 | # we can do is to at least restore __builtins__ for the user on |
|
658 | # we can do is to at least restore __builtins__ for the user on | |
659 | # exit. |
|
659 | # exit. | |
660 | self.shell.user_ns['__builtins__'] = builtin_mod |
|
660 | self.shell.user_ns['__builtins__'] = builtin_mod | |
661 |
|
661 | |||
662 | # Ensure key global structures are restored |
|
662 | # Ensure key global structures are restored | |
663 | sys.argv = save_argv |
|
663 | sys.argv = save_argv | |
664 | if restore_main: |
|
664 | if restore_main: | |
665 | sys.modules['__main__'] = restore_main |
|
665 | sys.modules['__main__'] = restore_main | |
666 | else: |
|
666 | else: | |
667 | # Remove from sys.modules the reference to main_mod we'd |
|
667 | # Remove from sys.modules the reference to main_mod we'd | |
668 | # added. Otherwise it will trap references to objects |
|
668 | # added. Otherwise it will trap references to objects | |
669 | # contained therein. |
|
669 | # contained therein. | |
670 | del sys.modules[main_mod_name] |
|
670 | del sys.modules[main_mod_name] | |
671 |
|
671 | |||
672 | return stats |
|
672 | return stats | |
673 |
|
673 | |||
674 | def _run_with_debugger(self, code, code_ns, filename=None, |
|
674 | def _run_with_debugger(self, code, code_ns, filename=None, | |
675 | bp_line=None, bp_file=None): |
|
675 | bp_line=None, bp_file=None): | |
676 | """ |
|
676 | """ | |
677 | Run `code` in debugger with a break point. |
|
677 | Run `code` in debugger with a break point. | |
678 |
|
678 | |||
679 | Parameters |
|
679 | Parameters | |
680 | ---------- |
|
680 | ---------- | |
681 | code : str |
|
681 | code : str | |
682 | Code to execute. |
|
682 | Code to execute. | |
683 | code_ns : dict |
|
683 | code_ns : dict | |
684 | A namespace in which `code` is executed. |
|
684 | A namespace in which `code` is executed. | |
685 | filename : str |
|
685 | filename : str | |
686 | `code` is ran as if it is in `filename`. |
|
686 | `code` is ran as if it is in `filename`. | |
687 | bp_line : int, optional |
|
687 | bp_line : int, optional | |
688 | Line number of the break point. |
|
688 | Line number of the break point. | |
689 | bp_file : str, optional |
|
689 | bp_file : str, optional | |
690 | Path to the file in which break point is specified. |
|
690 | Path to the file in which break point is specified. | |
691 | `filename` is used if not given. |
|
691 | `filename` is used if not given. | |
692 |
|
692 | |||
693 | Raises |
|
693 | Raises | |
694 | ------ |
|
694 | ------ | |
695 | UsageError |
|
695 | UsageError | |
696 | If the break point given by `bp_line` is not valid. |
|
696 | If the break point given by `bp_line` is not valid. | |
697 |
|
697 | |||
698 | """ |
|
698 | """ | |
699 | deb = debugger.Pdb(self.shell.colors) |
|
699 | deb = debugger.Pdb(self.shell.colors) | |
700 | # reset Breakpoint state, which is moronically kept |
|
700 | # reset Breakpoint state, which is moronically kept | |
701 | # in a class |
|
701 | # in a class | |
702 | bdb.Breakpoint.next = 1 |
|
702 | bdb.Breakpoint.next = 1 | |
703 | bdb.Breakpoint.bplist = {} |
|
703 | bdb.Breakpoint.bplist = {} | |
704 | bdb.Breakpoint.bpbynumber = [None] |
|
704 | bdb.Breakpoint.bpbynumber = [None] | |
705 | if bp_line is not None: |
|
705 | if bp_line is not None: | |
706 | # Set an initial breakpoint to stop execution |
|
706 | # Set an initial breakpoint to stop execution | |
707 | maxtries = 10 |
|
707 | maxtries = 10 | |
708 | bp_file = bp_file or filename |
|
708 | bp_file = bp_file or filename | |
709 | checkline = deb.checkline(bp_file, bp_line) |
|
709 | checkline = deb.checkline(bp_file, bp_line) | |
710 | if not checkline: |
|
710 | if not checkline: | |
711 | for bp in range(bp_line + 1, bp_line + maxtries + 1): |
|
711 | for bp in range(bp_line + 1, bp_line + maxtries + 1): | |
712 | if deb.checkline(bp_file, bp): |
|
712 | if deb.checkline(bp_file, bp): | |
713 | break |
|
713 | break | |
714 | else: |
|
714 | else: | |
715 | msg = ("\nI failed to find a valid line to set " |
|
715 | msg = ("\nI failed to find a valid line to set " | |
716 | "a breakpoint\n" |
|
716 | "a breakpoint\n" | |
717 | "after trying up to line: %s.\n" |
|
717 | "after trying up to line: %s.\n" | |
718 | "Please set a valid breakpoint manually " |
|
718 | "Please set a valid breakpoint manually " | |
719 | "with the -b option." % bp) |
|
719 | "with the -b option." % bp) | |
720 | raise UsageError(msg) |
|
720 | raise UsageError(msg) | |
721 | # if we find a good linenumber, set the breakpoint |
|
721 | # if we find a good linenumber, set the breakpoint | |
722 | deb.do_break('%s:%s' % (bp_file, bp_line)) |
|
722 | deb.do_break('%s:%s' % (bp_file, bp_line)) | |
723 |
|
723 | |||
724 | if filename: |
|
724 | if filename: | |
725 | # Mimic Pdb._runscript(...) |
|
725 | # Mimic Pdb._runscript(...) | |
726 | deb._wait_for_mainpyfile = True |
|
726 | deb._wait_for_mainpyfile = True | |
727 | deb.mainpyfile = deb.canonic(filename) |
|
727 | deb.mainpyfile = deb.canonic(filename) | |
728 |
|
728 | |||
729 | # Start file run |
|
729 | # Start file run | |
730 | print "NOTE: Enter 'c' at the %s prompt to continue execution." % deb.prompt |
|
730 | print "NOTE: Enter 'c' at the %s prompt to continue execution." % deb.prompt | |
731 | try: |
|
731 | try: | |
732 | if filename: |
|
732 | if filename: | |
733 | # save filename so it can be used by methods on the deb object |
|
733 | # save filename so it can be used by methods on the deb object | |
734 | deb._exec_filename = filename |
|
734 | deb._exec_filename = filename | |
735 | deb.run(code, code_ns) |
|
735 | deb.run(code, code_ns) | |
736 |
|
736 | |||
737 | except: |
|
737 | except: | |
738 | etype, value, tb = sys.exc_info() |
|
738 | etype, value, tb = sys.exc_info() | |
739 | # Skip three frames in the traceback: the %run one, |
|
739 | # Skip three frames in the traceback: the %run one, | |
740 | # one inside bdb.py, and the command-line typed by the |
|
740 | # one inside bdb.py, and the command-line typed by the | |
741 | # user (run by exec in pdb itself). |
|
741 | # user (run by exec in pdb itself). | |
742 | self.shell.InteractiveTB(etype, value, tb, tb_offset=3) |
|
742 | self.shell.InteractiveTB(etype, value, tb, tb_offset=3) | |
743 |
|
743 | |||
744 | @staticmethod |
|
744 | @staticmethod | |
745 | def _run_with_timing(run, nruns): |
|
745 | def _run_with_timing(run, nruns): | |
746 | """ |
|
746 | """ | |
747 | Run function `run` and print timing information. |
|
747 | Run function `run` and print timing information. | |
748 |
|
748 | |||
749 | Parameters |
|
749 | Parameters | |
750 | ---------- |
|
750 | ---------- | |
751 | run : callable |
|
751 | run : callable | |
752 | Any callable object which takes no argument. |
|
752 | Any callable object which takes no argument. | |
753 | nruns : int |
|
753 | nruns : int | |
754 | Number of times to execute `run`. |
|
754 | Number of times to execute `run`. | |
755 |
|
755 | |||
756 | """ |
|
756 | """ | |
757 | twall0 = time.time() |
|
757 | twall0 = time.time() | |
758 | if nruns == 1: |
|
758 | if nruns == 1: | |
759 | t0 = clock2() |
|
759 | t0 = clock2() | |
760 | run() |
|
760 | run() | |
761 | t1 = clock2() |
|
761 | t1 = clock2() | |
762 | t_usr = t1[0] - t0[0] |
|
762 | t_usr = t1[0] - t0[0] | |
763 | t_sys = t1[1] - t0[1] |
|
763 | t_sys = t1[1] - t0[1] | |
764 | print "\nIPython CPU timings (estimated):" |
|
764 | print "\nIPython CPU timings (estimated):" | |
765 | print " User : %10.2f s." % t_usr |
|
765 | print " User : %10.2f s." % t_usr | |
766 | print " System : %10.2f s." % t_sys |
|
766 | print " System : %10.2f s." % t_sys | |
767 | else: |
|
767 | else: | |
768 | runs = range(nruns) |
|
768 | runs = range(nruns) | |
769 | t0 = clock2() |
|
769 | t0 = clock2() | |
770 | for nr in runs: |
|
770 | for nr in runs: | |
771 | run() |
|
771 | run() | |
772 | t1 = clock2() |
|
772 | t1 = clock2() | |
773 | t_usr = t1[0] - t0[0] |
|
773 | t_usr = t1[0] - t0[0] | |
774 | t_sys = t1[1] - t0[1] |
|
774 | t_sys = t1[1] - t0[1] | |
775 | print "\nIPython CPU timings (estimated):" |
|
775 | print "\nIPython CPU timings (estimated):" | |
776 | print "Total runs performed:", nruns |
|
776 | print "Total runs performed:", nruns | |
777 | print " Times : %10s %10s" % ('Total', 'Per run') |
|
777 | print " Times : %10s %10s" % ('Total', 'Per run') | |
778 | print " User : %10.2f s, %10.2f s." % (t_usr, t_usr / nruns) |
|
778 | print " User : %10.2f s, %10.2f s." % (t_usr, t_usr / nruns) | |
779 | print " System : %10.2f s, %10.2f s." % (t_sys, t_sys / nruns) |
|
779 | print " System : %10.2f s, %10.2f s." % (t_sys, t_sys / nruns) | |
780 | twall1 = time.time() |
|
780 | twall1 = time.time() | |
781 | print "Wall time: %10.2f s." % (twall1 - twall0) |
|
781 | print "Wall time: %10.2f s." % (twall1 - twall0) | |
782 |
|
782 | |||
783 | @skip_doctest |
|
783 | @skip_doctest | |
784 | @line_cell_magic |
|
784 | @line_cell_magic | |
785 | def timeit(self, line='', cell=None): |
|
785 | def timeit(self, line='', cell=None): | |
786 | """Time execution of a Python statement or expression |
|
786 | """Time execution of a Python statement or expression | |
787 |
|
787 | |||
788 | Usage, in line mode: |
|
788 | Usage, in line mode: | |
789 | %timeit [-n<N> -r<R> [-t|-c]] statement |
|
789 | %timeit [-n<N> -r<R> [-t|-c]] statement | |
790 | or in cell mode: |
|
790 | or in cell mode: | |
791 | %%timeit [-n<N> -r<R> [-t|-c]] setup_code |
|
791 | %%timeit [-n<N> -r<R> [-t|-c]] setup_code | |
792 | code |
|
792 | code | |
793 | code... |
|
793 | code... | |
794 |
|
794 | |||
795 | Time execution of a Python statement or expression using the timeit |
|
795 | Time execution of a Python statement or expression using the timeit | |
796 | module. This function can be used both as a line and cell magic: |
|
796 | module. This function can be used both as a line and cell magic: | |
797 |
|
797 | |||
798 | - In line mode you can time a single-line statement (though multiple |
|
798 | - In line mode you can time a single-line statement (though multiple | |
799 | ones can be chained with using semicolons). |
|
799 | ones can be chained with using semicolons). | |
800 |
|
800 | |||
801 | - In cell mode, the statement in the first line is used as setup code |
|
801 | - In cell mode, the statement in the first line is used as setup code | |
802 | (executed but not timed) and the body of the cell is timed. The cell |
|
802 | (executed but not timed) and the body of the cell is timed. The cell | |
803 | body has access to any variables created in the setup code. |
|
803 | body has access to any variables created in the setup code. | |
804 |
|
804 | |||
805 | Options: |
|
805 | Options: | |
806 | -n<N>: execute the given statement <N> times in a loop. If this value |
|
806 | -n<N>: execute the given statement <N> times in a loop. If this value | |
807 | is not given, a fitting value is chosen. |
|
807 | is not given, a fitting value is chosen. | |
808 |
|
808 | |||
809 | -r<R>: repeat the loop iteration <R> times and take the best result. |
|
809 | -r<R>: repeat the loop iteration <R> times and take the best result. | |
810 | Default: 3 |
|
810 | Default: 3 | |
811 |
|
811 | |||
812 | -t: use time.time to measure the time, which is the default on Unix. |
|
812 | -t: use time.time to measure the time, which is the default on Unix. | |
813 | This function measures wall time. |
|
813 | This function measures wall time. | |
814 |
|
814 | |||
815 | -c: use time.clock to measure the time, which is the default on |
|
815 | -c: use time.clock to measure the time, which is the default on | |
816 | Windows and measures wall time. On Unix, resource.getrusage is used |
|
816 | Windows and measures wall time. On Unix, resource.getrusage is used | |
817 | instead and returns the CPU user time. |
|
817 | instead and returns the CPU user time. | |
818 |
|
818 | |||
819 | -p<P>: use a precision of <P> digits to display the timing result. |
|
819 | -p<P>: use a precision of <P> digits to display the timing result. | |
820 | Default: 3 |
|
820 | Default: 3 | |
821 |
|
821 | |||
822 |
|
822 | |||
823 | Examples |
|
823 | Examples | |
824 | -------- |
|
824 | -------- | |
825 | :: |
|
825 | :: | |
826 |
|
826 | |||
827 | In [1]: %timeit pass |
|
827 | In [1]: %timeit pass | |
828 | 10000000 loops, best of 3: 53.3 ns per loop |
|
828 | 10000000 loops, best of 3: 53.3 ns per loop | |
829 |
|
829 | |||
830 | In [2]: u = None |
|
830 | In [2]: u = None | |
831 |
|
831 | |||
832 | In [3]: %timeit u is None |
|
832 | In [3]: %timeit u is None | |
833 | 10000000 loops, best of 3: 184 ns per loop |
|
833 | 10000000 loops, best of 3: 184 ns per loop | |
834 |
|
834 | |||
835 | In [4]: %timeit -r 4 u == None |
|
835 | In [4]: %timeit -r 4 u == None | |
836 | 1000000 loops, best of 4: 242 ns per loop |
|
836 | 1000000 loops, best of 4: 242 ns per loop | |
837 |
|
837 | |||
838 | In [5]: import time |
|
838 | In [5]: import time | |
839 |
|
839 | |||
840 | In [6]: %timeit -n1 time.sleep(2) |
|
840 | In [6]: %timeit -n1 time.sleep(2) | |
841 | 1 loops, best of 3: 2 s per loop |
|
841 | 1 loops, best of 3: 2 s per loop | |
842 |
|
842 | |||
843 |
|
843 | |||
844 | The times reported by %timeit will be slightly higher than those |
|
844 | The times reported by %timeit will be slightly higher than those | |
845 | reported by the timeit.py script when variables are accessed. This is |
|
845 | reported by the timeit.py script when variables are accessed. This is | |
846 | due to the fact that %timeit executes the statement in the namespace |
|
846 | due to the fact that %timeit executes the statement in the namespace | |
847 | of the shell, compared with timeit.py, which uses a single setup |
|
847 | of the shell, compared with timeit.py, which uses a single setup | |
848 | statement to import function or create variables. Generally, the bias |
|
848 | statement to import function or create variables. Generally, the bias | |
849 | does not matter as long as results from timeit.py are not mixed with |
|
849 | does not matter as long as results from timeit.py are not mixed with | |
850 | those from %timeit.""" |
|
850 | those from %timeit.""" | |
851 |
|
851 | |||
852 | import timeit |
|
852 | import timeit | |
853 |
|
853 | |||
854 | opts, stmt = self.parse_options(line,'n:r:tcp:', |
|
854 | opts, stmt = self.parse_options(line,'n:r:tcp:', | |
855 | posix=False, strict=False) |
|
855 | posix=False, strict=False) | |
856 | if stmt == "" and cell is None: |
|
856 | if stmt == "" and cell is None: | |
857 | return |
|
857 | return | |
858 |
|
858 | |||
859 | timefunc = timeit.default_timer |
|
859 | timefunc = timeit.default_timer | |
860 | number = int(getattr(opts, "n", 0)) |
|
860 | number = int(getattr(opts, "n", 0)) | |
861 | repeat = int(getattr(opts, "r", timeit.default_repeat)) |
|
861 | repeat = int(getattr(opts, "r", timeit.default_repeat)) | |
862 | precision = int(getattr(opts, "p", 3)) |
|
862 | precision = int(getattr(opts, "p", 3)) | |
863 | if hasattr(opts, "t"): |
|
863 | if hasattr(opts, "t"): | |
864 | timefunc = time.time |
|
864 | timefunc = time.time | |
865 | if hasattr(opts, "c"): |
|
865 | if hasattr(opts, "c"): | |
866 | timefunc = clock |
|
866 | timefunc = clock | |
867 |
|
867 | |||
868 | timer = timeit.Timer(timer=timefunc) |
|
868 | timer = timeit.Timer(timer=timefunc) | |
869 | # this code has tight coupling to the inner workings of timeit.Timer, |
|
869 | # this code has tight coupling to the inner workings of timeit.Timer, | |
870 | # but is there a better way to achieve that the code stmt has access |
|
870 | # but is there a better way to achieve that the code stmt has access | |
871 | # to the shell namespace? |
|
871 | # to the shell namespace? | |
872 | transform = self.shell.input_splitter.transform_cell |
|
872 | transform = self.shell.input_splitter.transform_cell | |
873 |
|
873 | |||
874 | if cell is None: |
|
874 | if cell is None: | |
875 | # called as line magic |
|
875 | # called as line magic | |
876 | ast_setup = ast.parse("pass") |
|
876 | ast_setup = ast.parse("pass") | |
877 | ast_stmt = ast.parse(transform(stmt)) |
|
877 | ast_stmt = ast.parse(transform(stmt)) | |
878 | else: |
|
878 | else: | |
879 | ast_setup = ast.parse(transform(stmt)) |
|
879 | ast_setup = ast.parse(transform(stmt)) | |
880 | ast_stmt = ast.parse(transform(cell)) |
|
880 | ast_stmt = ast.parse(transform(cell)) | |
881 |
|
881 | |||
882 | ast_setup = self.shell.transform_ast(ast_setup) |
|
882 | ast_setup = self.shell.transform_ast(ast_setup) | |
883 | ast_stmt = self.shell.transform_ast(ast_stmt) |
|
883 | ast_stmt = self.shell.transform_ast(ast_stmt) | |
884 |
|
884 | |||
885 | # This codestring is taken from timeit.template - we fill it in as an |
|
885 | # This codestring is taken from timeit.template - we fill it in as an | |
886 | # AST, so that we can apply our AST transformations to the user code |
|
886 | # AST, so that we can apply our AST transformations to the user code | |
887 | # without affecting the timing code. |
|
887 | # without affecting the timing code. | |
888 | timeit_ast_template = ast.parse('def inner(_it, _timer):\n' |
|
888 | timeit_ast_template = ast.parse('def inner(_it, _timer):\n' | |
889 | ' setup\n' |
|
889 | ' setup\n' | |
890 | ' _t0 = _timer()\n' |
|
890 | ' _t0 = _timer()\n' | |
891 | ' for _i in _it:\n' |
|
891 | ' for _i in _it:\n' | |
892 | ' stmt\n' |
|
892 | ' stmt\n' | |
893 | ' _t1 = _timer()\n' |
|
893 | ' _t1 = _timer()\n' | |
894 | ' return _t1 - _t0\n') |
|
894 | ' return _t1 - _t0\n') | |
895 |
|
895 | |||
896 | class TimeitTemplateFiller(ast.NodeTransformer): |
|
896 | class TimeitTemplateFiller(ast.NodeTransformer): | |
897 | "This is quite tightly tied to the template definition above." |
|
897 | "This is quite tightly tied to the template definition above." | |
898 | def visit_FunctionDef(self, node): |
|
898 | def visit_FunctionDef(self, node): | |
899 | "Fill in the setup statement" |
|
899 | "Fill in the setup statement" | |
900 | self.generic_visit(node) |
|
900 | self.generic_visit(node) | |
901 | if node.name == "inner": |
|
901 | if node.name == "inner": | |
902 | node.body[:1] = ast_setup.body |
|
902 | node.body[:1] = ast_setup.body | |
903 |
|
903 | |||
904 | return node |
|
904 | return node | |
905 |
|
905 | |||
906 | def visit_For(self, node): |
|
906 | def visit_For(self, node): | |
907 | "Fill in the statement to be timed" |
|
907 | "Fill in the statement to be timed" | |
908 | if getattr(getattr(node.body[0], 'value', None), 'id', None) == 'stmt': |
|
908 | if getattr(getattr(node.body[0], 'value', None), 'id', None) == 'stmt': | |
909 | node.body = ast_stmt.body |
|
909 | node.body = ast_stmt.body | |
910 | return node |
|
910 | return node | |
911 |
|
911 | |||
912 | timeit_ast = TimeitTemplateFiller().visit(timeit_ast_template) |
|
912 | timeit_ast = TimeitTemplateFiller().visit(timeit_ast_template) | |
913 | timeit_ast = ast.fix_missing_locations(timeit_ast) |
|
913 | timeit_ast = ast.fix_missing_locations(timeit_ast) | |
914 |
|
914 | |||
915 | # Track compilation time so it can be reported if too long |
|
915 | # Track compilation time so it can be reported if too long | |
916 | # Minimum time above which compilation time will be reported |
|
916 | # Minimum time above which compilation time will be reported | |
917 | tc_min = 0.1 |
|
917 | tc_min = 0.1 | |
918 |
|
918 | |||
919 | t0 = clock() |
|
919 | t0 = clock() | |
920 | code = compile(timeit_ast, "<magic-timeit>", "exec") |
|
920 | code = compile(timeit_ast, "<magic-timeit>", "exec") | |
921 | tc = clock()-t0 |
|
921 | tc = clock()-t0 | |
922 |
|
922 | |||
923 | ns = {} |
|
923 | ns = {} | |
924 | exec code in self.shell.user_ns, ns |
|
924 | exec code in self.shell.user_ns, ns | |
925 | timer.inner = ns["inner"] |
|
925 | timer.inner = ns["inner"] | |
926 |
|
926 | |||
927 | if number == 0: |
|
927 | if number == 0: | |
928 | # determine number so that 0.2 <= total time < 2.0 |
|
928 | # determine number so that 0.2 <= total time < 2.0 | |
929 | number = 1 |
|
929 | number = 1 | |
930 | for i in range(1, 10): |
|
930 | for i in range(1, 10): | |
931 | if timer.timeit(number) >= 0.2: |
|
931 | if timer.timeit(number) >= 0.2: | |
932 | break |
|
932 | break | |
933 | number *= 10 |
|
933 | number *= 10 | |
934 |
|
934 | |||
935 | best = min(timer.repeat(repeat, number)) / number |
|
935 | best = min(timer.repeat(repeat, number)) / number | |
936 |
|
936 | |||
937 | print u"%d loops, best of %d: %s per loop" % (number, repeat, |
|
937 | print u"%d loops, best of %d: %s per loop" % (number, repeat, | |
938 | _format_time(best, precision)) |
|
938 | _format_time(best, precision)) | |
939 | if tc > tc_min: |
|
939 | if tc > tc_min: | |
940 | print "Compiler time: %.2f s" % tc |
|
940 | print "Compiler time: %.2f s" % tc | |
941 |
|
941 | |||
942 | @skip_doctest |
|
942 | @skip_doctest | |
943 | @needs_local_scope |
|
943 | @needs_local_scope | |
944 | @line_cell_magic |
|
944 | @line_cell_magic | |
945 | def time(self,line='', cell=None, local_ns=None): |
|
945 | def time(self,line='', cell=None, local_ns=None): | |
946 | """Time execution of a Python statement or expression. |
|
946 | """Time execution of a Python statement or expression. | |
947 |
|
947 | |||
948 | The CPU and wall clock times are printed, and the value of the |
|
948 | The CPU and wall clock times are printed, and the value of the | |
949 | expression (if any) is returned. Note that under Win32, system time |
|
949 | expression (if any) is returned. Note that under Win32, system time | |
950 | is always reported as 0, since it can not be measured. |
|
950 | is always reported as 0, since it can not be measured. | |
951 |
|
951 | |||
952 | This function can be used both as a line and cell magic: |
|
952 | This function can be used both as a line and cell magic: | |
953 |
|
953 | |||
954 | - In line mode you can time a single-line statement (though multiple |
|
954 | - In line mode you can time a single-line statement (though multiple | |
955 | ones can be chained with using semicolons). |
|
955 | ones can be chained with using semicolons). | |
956 |
|
956 | |||
957 | - In cell mode, you can time the cell body (a directly |
|
957 | - In cell mode, you can time the cell body (a directly | |
958 | following statement raises an error). |
|
958 | following statement raises an error). | |
959 |
|
959 | |||
960 | This function provides very basic timing functionality. Use the timeit |
|
960 | This function provides very basic timing functionality. Use the timeit | |
961 | magic for more controll over the measurement. |
|
961 | magic for more controll over the measurement. | |
962 |
|
962 | |||
963 | Examples |
|
963 | Examples | |
964 | -------- |
|
964 | -------- | |
965 | :: |
|
965 | :: | |
966 |
|
966 | |||
967 | In [1]: %time 2**128 |
|
967 | In [1]: %time 2**128 | |
968 | CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s |
|
968 | CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s | |
969 | Wall time: 0.00 |
|
969 | Wall time: 0.00 | |
970 | Out[1]: 340282366920938463463374607431768211456L |
|
970 | Out[1]: 340282366920938463463374607431768211456L | |
971 |
|
971 | |||
972 | In [2]: n = 1000000 |
|
972 | In [2]: n = 1000000 | |
973 |
|
973 | |||
974 | In [3]: %time sum(range(n)) |
|
974 | In [3]: %time sum(range(n)) | |
975 | CPU times: user 1.20 s, sys: 0.05 s, total: 1.25 s |
|
975 | CPU times: user 1.20 s, sys: 0.05 s, total: 1.25 s | |
976 | Wall time: 1.37 |
|
976 | Wall time: 1.37 | |
977 | Out[3]: 499999500000L |
|
977 | Out[3]: 499999500000L | |
978 |
|
978 | |||
979 | In [4]: %time print 'hello world' |
|
979 | In [4]: %time print 'hello world' | |
980 | hello world |
|
980 | hello world | |
981 | CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s |
|
981 | CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s | |
982 | Wall time: 0.00 |
|
982 | Wall time: 0.00 | |
983 |
|
983 | |||
984 | Note that the time needed by Python to compile the given expression |
|
984 | Note that the time needed by Python to compile the given expression | |
985 | will be reported if it is more than 0.1s. In this example, the |
|
985 | will be reported if it is more than 0.1s. In this example, the | |
986 | actual exponentiation is done by Python at compilation time, so while |
|
986 | actual exponentiation is done by Python at compilation time, so while | |
987 | the expression can take a noticeable amount of time to compute, that |
|
987 | the expression can take a noticeable amount of time to compute, that | |
988 | time is purely due to the compilation: |
|
988 | time is purely due to the compilation: | |
989 |
|
989 | |||
990 | In [5]: %time 3**9999; |
|
990 | In [5]: %time 3**9999; | |
991 | CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s |
|
991 | CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s | |
992 | Wall time: 0.00 s |
|
992 | Wall time: 0.00 s | |
993 |
|
993 | |||
994 | In [6]: %time 3**999999; |
|
994 | In [6]: %time 3**999999; | |
995 | CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s |
|
995 | CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s | |
996 | Wall time: 0.00 s |
|
996 | Wall time: 0.00 s | |
997 | Compiler : 0.78 s |
|
997 | Compiler : 0.78 s | |
998 | """ |
|
998 | """ | |
999 |
|
999 | |||
1000 | # fail immediately if the given expression can't be compiled |
|
1000 | # fail immediately if the given expression can't be compiled | |
1001 |
|
1001 | |||
1002 | if line and cell: |
|
1002 | if line and cell: | |
1003 | raise UsageError("Can't use statement directly after '%%time'!") |
|
1003 | raise UsageError("Can't use statement directly after '%%time'!") | |
1004 |
|
1004 | |||
1005 | if cell: |
|
1005 | if cell: | |
1006 | expr = self.shell.input_transformer_manager.transform_cell(cell) |
|
1006 | expr = self.shell.input_transformer_manager.transform_cell(cell) | |
1007 | else: |
|
1007 | else: | |
1008 | expr = self.shell.input_transformer_manager.transform_cell(line) |
|
1008 | expr = self.shell.input_transformer_manager.transform_cell(line) | |
1009 |
|
1009 | |||
1010 | # Minimum time above which parse time will be reported |
|
1010 | # Minimum time above which parse time will be reported | |
1011 | tp_min = 0.1 |
|
1011 | tp_min = 0.1 | |
1012 |
|
1012 | |||
1013 | t0 = clock() |
|
1013 | t0 = clock() | |
1014 | expr_ast = ast.parse(expr) |
|
1014 | expr_ast = ast.parse(expr) | |
1015 | tp = clock()-t0 |
|
1015 | tp = clock()-t0 | |
1016 |
|
1016 | |||
1017 | # Apply AST transformations |
|
1017 | # Apply AST transformations | |
1018 | expr_ast = self.shell.transform_ast(expr_ast) |
|
1018 | expr_ast = self.shell.transform_ast(expr_ast) | |
1019 |
|
1019 | |||
1020 | # Minimum time above which compilation time will be reported |
|
1020 | # Minimum time above which compilation time will be reported | |
1021 | tc_min = 0.1 |
|
1021 | tc_min = 0.1 | |
1022 |
|
1022 | |||
1023 | if len(expr_ast.body)==1 and isinstance(expr_ast.body[0], ast.Expr): |
|
1023 | if len(expr_ast.body)==1 and isinstance(expr_ast.body[0], ast.Expr): | |
1024 | mode = 'eval' |
|
1024 | mode = 'eval' | |
1025 | source = '<timed eval>' |
|
1025 | source = '<timed eval>' | |
1026 | expr_ast = ast.Expression(expr_ast.body[0].value) |
|
1026 | expr_ast = ast.Expression(expr_ast.body[0].value) | |
1027 | else: |
|
1027 | else: | |
1028 | mode = 'exec' |
|
1028 | mode = 'exec' | |
1029 | source = '<timed exec>' |
|
1029 | source = '<timed exec>' | |
1030 | t0 = clock() |
|
1030 | t0 = clock() | |
1031 | code = compile(expr_ast, source, mode) |
|
1031 | code = compile(expr_ast, source, mode) | |
1032 | tc = clock()-t0 |
|
1032 | tc = clock()-t0 | |
1033 |
|
1033 | |||
1034 | # skew measurement as little as possible |
|
1034 | # skew measurement as little as possible | |
1035 | glob = self.shell.user_ns |
|
1035 | glob = self.shell.user_ns | |
1036 | wtime = time.time |
|
1036 | wtime = time.time | |
1037 | # time execution |
|
1037 | # time execution | |
1038 | wall_st = wtime() |
|
1038 | wall_st = wtime() | |
1039 | if mode=='eval': |
|
1039 | if mode=='eval': | |
1040 | st = clock2() |
|
1040 | st = clock2() | |
1041 | out = eval(code, glob, local_ns) |
|
1041 | out = eval(code, glob, local_ns) | |
1042 | end = clock2() |
|
1042 | end = clock2() | |
1043 | else: |
|
1043 | else: | |
1044 | st = clock2() |
|
1044 | st = clock2() | |
1045 | exec code in glob, local_ns |
|
1045 | exec code in glob, local_ns | |
1046 | end = clock2() |
|
1046 | end = clock2() | |
1047 | out = None |
|
1047 | out = None | |
1048 | wall_end = wtime() |
|
1048 | wall_end = wtime() | |
1049 | # Compute actual times and report |
|
1049 | # Compute actual times and report | |
1050 | wall_time = wall_end-wall_st |
|
1050 | wall_time = wall_end-wall_st | |
1051 | cpu_user = end[0]-st[0] |
|
1051 | cpu_user = end[0]-st[0] | |
1052 | cpu_sys = end[1]-st[1] |
|
1052 | cpu_sys = end[1]-st[1] | |
1053 | cpu_tot = cpu_user+cpu_sys |
|
1053 | cpu_tot = cpu_user+cpu_sys | |
1054 | # On windows cpu_sys is always zero, so no new information to the next print |
|
1054 | # On windows cpu_sys is always zero, so no new information to the next print | |
1055 | if sys.platform != 'win32': |
|
1055 | if sys.platform != 'win32': | |
1056 | print "CPU times: user %s, sys: %s, total: %s" % \ |
|
1056 | print "CPU times: user %s, sys: %s, total: %s" % \ | |
1057 | (_format_time(cpu_user),_format_time(cpu_sys),_format_time(cpu_tot)) |
|
1057 | (_format_time(cpu_user),_format_time(cpu_sys),_format_time(cpu_tot)) | |
1058 | print "Wall time: %s" % _format_time(wall_time) |
|
1058 | print "Wall time: %s" % _format_time(wall_time) | |
1059 | if tc > tc_min: |
|
1059 | if tc > tc_min: | |
1060 | print "Compiler : %s" % _format_time(tc) |
|
1060 | print "Compiler : %s" % _format_time(tc) | |
1061 | if tp > tp_min: |
|
1061 | if tp > tp_min: | |
1062 | print "Parser : %s" % _format_time(tp) |
|
1062 | print "Parser : %s" % _format_time(tp) | |
1063 | return out |
|
1063 | return out | |
1064 |
|
1064 | |||
1065 | @skip_doctest |
|
1065 | @skip_doctest | |
1066 | @line_magic |
|
1066 | @line_magic | |
1067 | def macro(self, parameter_s=''): |
|
1067 | def macro(self, parameter_s=''): | |
1068 | """Define a macro for future re-execution. It accepts ranges of history, |
|
1068 | """Define a macro for future re-execution. It accepts ranges of history, | |
1069 | filenames or string objects. |
|
1069 | filenames or string objects. | |
1070 |
|
1070 | |||
1071 | Usage:\\ |
|
1071 | Usage:\\ | |
1072 | %macro [options] name n1-n2 n3-n4 ... n5 .. n6 ... |
|
1072 | %macro [options] name n1-n2 n3-n4 ... n5 .. n6 ... | |
1073 |
|
1073 | |||
1074 | Options: |
|
1074 | Options: | |
1075 |
|
1075 | |||
1076 | -r: use 'raw' input. By default, the 'processed' history is used, |
|
1076 | -r: use 'raw' input. By default, the 'processed' history is used, | |
1077 | so that magics are loaded in their transformed version to valid |
|
1077 | so that magics are loaded in their transformed version to valid | |
1078 | Python. If this option is given, the raw input as typed at the |
|
1078 | Python. If this option is given, the raw input as typed at the | |
1079 | command line is used instead. |
|
1079 | command line is used instead. | |
1080 |
|
1080 | |||
1081 | -q: quiet macro definition. By default, a tag line is printed |
|
1081 | -q: quiet macro definition. By default, a tag line is printed | |
1082 | to indicate the macro has been created, and then the contents of |
|
1082 | to indicate the macro has been created, and then the contents of | |
1083 | the macro are printed. If this option is given, then no printout |
|
1083 | the macro are printed. If this option is given, then no printout | |
1084 | is produced once the macro is created. |
|
1084 | is produced once the macro is created. | |
1085 |
|
1085 | |||
1086 | This will define a global variable called `name` which is a string |
|
1086 | This will define a global variable called `name` which is a string | |
1087 | made of joining the slices and lines you specify (n1,n2,... numbers |
|
1087 | made of joining the slices and lines you specify (n1,n2,... numbers | |
1088 | above) from your input history into a single string. This variable |
|
1088 | above) from your input history into a single string. This variable | |
1089 | acts like an automatic function which re-executes those lines as if |
|
1089 | acts like an automatic function which re-executes those lines as if | |
1090 | you had typed them. You just type 'name' at the prompt and the code |
|
1090 | you had typed them. You just type 'name' at the prompt and the code | |
1091 | executes. |
|
1091 | executes. | |
1092 |
|
1092 | |||
1093 | The syntax for indicating input ranges is described in %history. |
|
1093 | The syntax for indicating input ranges is described in %history. | |
1094 |
|
1094 | |||
1095 | Note: as a 'hidden' feature, you can also use traditional python slice |
|
1095 | Note: as a 'hidden' feature, you can also use traditional python slice | |
1096 | notation, where N:M means numbers N through M-1. |
|
1096 | notation, where N:M means numbers N through M-1. | |
1097 |
|
1097 | |||
1098 | For example, if your history contains (print using %hist -n ):: |
|
1098 | For example, if your history contains (print using %hist -n ):: | |
1099 |
|
1099 | |||
1100 | 44: x=1 |
|
1100 | 44: x=1 | |
1101 | 45: y=3 |
|
1101 | 45: y=3 | |
1102 | 46: z=x+y |
|
1102 | 46: z=x+y | |
1103 | 47: print x |
|
1103 | 47: print x | |
1104 | 48: a=5 |
|
1104 | 48: a=5 | |
1105 | 49: print 'x',x,'y',y |
|
1105 | 49: print 'x',x,'y',y | |
1106 |
|
1106 | |||
1107 | you can create a macro with lines 44 through 47 (included) and line 49 |
|
1107 | you can create a macro with lines 44 through 47 (included) and line 49 | |
1108 | called my_macro with:: |
|
1108 | called my_macro with:: | |
1109 |
|
1109 | |||
1110 | In [55]: %macro my_macro 44-47 49 |
|
1110 | In [55]: %macro my_macro 44-47 49 | |
1111 |
|
1111 | |||
1112 | Now, typing `my_macro` (without quotes) will re-execute all this code |
|
1112 | Now, typing `my_macro` (without quotes) will re-execute all this code | |
1113 | in one pass. |
|
1113 | in one pass. | |
1114 |
|
1114 | |||
1115 | You don't need to give the line-numbers in order, and any given line |
|
1115 | You don't need to give the line-numbers in order, and any given line | |
1116 | number can appear multiple times. You can assemble macros with any |
|
1116 | number can appear multiple times. You can assemble macros with any | |
1117 | lines from your input history in any order. |
|
1117 | lines from your input history in any order. | |
1118 |
|
1118 | |||
1119 | The macro is a simple object which holds its value in an attribute, |
|
1119 | The macro is a simple object which holds its value in an attribute, | |
1120 | but IPython's display system checks for macros and executes them as |
|
1120 | but IPython's display system checks for macros and executes them as | |
1121 | code instead of printing them when you type their name. |
|
1121 | code instead of printing them when you type their name. | |
1122 |
|
1122 | |||
1123 | You can view a macro's contents by explicitly printing it with:: |
|
1123 | You can view a macro's contents by explicitly printing it with:: | |
1124 |
|
1124 | |||
1125 | print macro_name |
|
1125 | print macro_name | |
1126 |
|
1126 | |||
1127 | """ |
|
1127 | """ | |
1128 | opts,args = self.parse_options(parameter_s,'rq',mode='list') |
|
1128 | opts,args = self.parse_options(parameter_s,'rq',mode='list') | |
1129 | if not args: # List existing macros |
|
1129 | if not args: # List existing macros | |
1130 | return sorted(k for k,v in self.shell.user_ns.iteritems() if\ |
|
1130 | return sorted(k for k,v in self.shell.user_ns.iteritems() if\ | |
1131 | isinstance(v, Macro)) |
|
1131 | isinstance(v, Macro)) | |
1132 | if len(args) == 1: |
|
1132 | if len(args) == 1: | |
1133 | raise UsageError( |
|
1133 | raise UsageError( | |
1134 | "%macro insufficient args; usage '%macro name n1-n2 n3-4...") |
|
1134 | "%macro insufficient args; usage '%macro name n1-n2 n3-4...") | |
1135 | name, codefrom = args[0], " ".join(args[1:]) |
|
1135 | name, codefrom = args[0], " ".join(args[1:]) | |
1136 |
|
1136 | |||
1137 | #print 'rng',ranges # dbg |
|
1137 | #print 'rng',ranges # dbg | |
1138 | try: |
|
1138 | try: | |
1139 | lines = self.shell.find_user_code(codefrom, 'r' in opts) |
|
1139 | lines = self.shell.find_user_code(codefrom, 'r' in opts) | |
1140 | except (ValueError, TypeError) as e: |
|
1140 | except (ValueError, TypeError) as e: | |
1141 | print e.args[0] |
|
1141 | print e.args[0] | |
1142 | return |
|
1142 | return | |
1143 | macro = Macro(lines) |
|
1143 | macro = Macro(lines) | |
1144 | self.shell.define_macro(name, macro) |
|
1144 | self.shell.define_macro(name, macro) | |
1145 | if not ( 'q' in opts) : |
|
1145 | if not ( 'q' in opts) : | |
1146 | print 'Macro `%s` created. To execute, type its name (without quotes).' % name |
|
1146 | print 'Macro `%s` created. To execute, type its name (without quotes).' % name | |
1147 | print '=== Macro contents: ===' |
|
1147 | print '=== Macro contents: ===' | |
1148 | print macro, |
|
1148 | print macro, | |
1149 |
|
1149 | |||
1150 | @magic_arguments.magic_arguments() |
|
1150 | @magic_arguments.magic_arguments() | |
1151 | @magic_arguments.argument('output', type=str, default='', nargs='?', |
|
1151 | @magic_arguments.argument('output', type=str, default='', nargs='?', | |
1152 | help="""The name of the variable in which to store output. |
|
1152 | help="""The name of the variable in which to store output. | |
1153 | This is a utils.io.CapturedIO object with stdout/err attributes |
|
1153 | This is a utils.io.CapturedIO object with stdout/err attributes | |
1154 | for the text of the captured output. |
|
1154 | for the text of the captured output. | |
1155 |
|
1155 | |||
1156 | CapturedOutput also has a show() method for displaying the output, |
|
1156 | CapturedOutput also has a show() method for displaying the output, | |
1157 | and __call__ as well, so you can use that to quickly display the |
|
1157 | and __call__ as well, so you can use that to quickly display the | |
1158 | output. |
|
1158 | output. | |
1159 |
|
1159 | |||
1160 | If unspecified, captured output is discarded. |
|
1160 | If unspecified, captured output is discarded. | |
1161 | """ |
|
1161 | """ | |
1162 | ) |
|
1162 | ) | |
1163 | @magic_arguments.argument('--no-stderr', action="store_true", |
|
1163 | @magic_arguments.argument('--no-stderr', action="store_true", | |
1164 | help="""Don't capture stderr.""" |
|
1164 | help="""Don't capture stderr.""" | |
1165 | ) |
|
1165 | ) | |
1166 | @magic_arguments.argument('--no-stdout', action="store_true", |
|
1166 | @magic_arguments.argument('--no-stdout', action="store_true", | |
1167 | help="""Don't capture stdout.""" |
|
1167 | help="""Don't capture stdout.""" | |
1168 | ) |
|
1168 | ) | |
1169 | @magic_arguments.argument('--no-display', action="store_true", |
|
1169 | @magic_arguments.argument('--no-display', action="store_true", | |
1170 | help="""Don't capture IPython's rich display.""" |
|
1170 | help="""Don't capture IPython's rich display.""" | |
1171 | ) |
|
1171 | ) | |
1172 | @cell_magic |
|
1172 | @cell_magic | |
1173 | def capture(self, line, cell): |
|
1173 | def capture(self, line, cell): | |
1174 | """run the cell, capturing stdout, stderr, and IPython's rich display() calls.""" |
|
1174 | """run the cell, capturing stdout, stderr, and IPython's rich display() calls.""" | |
1175 | args = magic_arguments.parse_argstring(self.capture, line) |
|
1175 | args = magic_arguments.parse_argstring(self.capture, line) | |
1176 | out = not args.no_stdout |
|
1176 | out = not args.no_stdout | |
1177 | err = not args.no_stderr |
|
1177 | err = not args.no_stderr | |
1178 | disp = not args.no_display |
|
1178 | disp = not args.no_display | |
1179 | with capture_output(out, err, disp) as io: |
|
1179 | with capture_output(out, err, disp) as io: | |
1180 | self.shell.run_cell(cell) |
|
1180 | self.shell.run_cell(cell) | |
1181 | if args.output: |
|
1181 | if args.output: | |
1182 | self.shell.user_ns[args.output] = io |
|
1182 | self.shell.user_ns[args.output] = io | |
1183 |
|
1183 | |||
1184 | def parse_breakpoint(text, current_file): |
|
1184 | def parse_breakpoint(text, current_file): | |
1185 | '''Returns (file, line) for file:line and (current_file, line) for line''' |
|
1185 | '''Returns (file, line) for file:line and (current_file, line) for line''' | |
1186 | colon = text.find(':') |
|
1186 | colon = text.find(':') | |
1187 | if colon == -1: |
|
1187 | if colon == -1: | |
1188 | return current_file, int(text) |
|
1188 | return current_file, int(text) | |
1189 | else: |
|
1189 | else: | |
1190 | return text[:colon], int(text[colon+1:]) |
|
1190 | return text[:colon], int(text[colon+1:]) | |
1191 |
|
1191 | |||
1192 | def _format_time(timespan, precision=3): |
|
1192 | def _format_time(timespan, precision=3): | |
1193 | """Formats the timespan in a human readable form""" |
|
1193 | """Formats the timespan in a human readable form""" | |
1194 | import math |
|
1194 | import math | |
1195 |
|
1195 | |||
1196 | if timespan >= 60.0: |
|
1196 | if timespan >= 60.0: | |
1197 | # we have more than a minute, format that in a human readable form |
|
1197 | # we have more than a minute, format that in a human readable form | |
1198 | # Idea from http://snipplr.com/view/5713/ |
|
1198 | # Idea from http://snipplr.com/view/5713/ | |
1199 | parts = [("d", 60*60*24),("h", 60*60),("min", 60), ("s", 1)] |
|
1199 | parts = [("d", 60*60*24),("h", 60*60),("min", 60), ("s", 1)] | |
1200 | time = [] |
|
1200 | time = [] | |
1201 | leftover = timespan |
|
1201 | leftover = timespan | |
1202 | for suffix, length in parts: |
|
1202 | for suffix, length in parts: | |
1203 | value = int(leftover / length) |
|
1203 | value = int(leftover / length) | |
1204 | if value > 0: |
|
1204 | if value > 0: | |
1205 | leftover = leftover % length |
|
1205 | leftover = leftover % length | |
1206 | time.append(u'%s%s' % (str(value), suffix)) |
|
1206 | time.append(u'%s%s' % (str(value), suffix)) | |
1207 | if leftover < 1: |
|
1207 | if leftover < 1: | |
1208 | break |
|
1208 | break | |
1209 | return " ".join(time) |
|
1209 | return " ".join(time) | |
1210 |
|
1210 | |||
1211 |
|
1211 | |||
1212 | # Unfortunately the unicode 'micro' symbol can cause problems in |
|
1212 | # Unfortunately the unicode 'micro' symbol can cause problems in | |
1213 | # certain terminals. |
|
1213 | # certain terminals. | |
1214 | # See bug: https://bugs.launchpad.net/ipython/+bug/348466 |
|
1214 | # See bug: https://bugs.launchpad.net/ipython/+bug/348466 | |
1215 | # Try to prevent crashes by being more secure than it needs to |
|
1215 | # Try to prevent crashes by being more secure than it needs to | |
1216 | # E.g. eclipse is able to print a µ, but has no sys.stdout.encoding set. |
|
1216 | # E.g. eclipse is able to print a µ, but has no sys.stdout.encoding set. | |
1217 | units = [u"s", u"ms",u'us',"ns"] # the save value |
|
1217 | units = [u"s", u"ms",u'us',"ns"] # the save value | |
1218 | if hasattr(sys.stdout, 'encoding') and sys.stdout.encoding: |
|
1218 | if hasattr(sys.stdout, 'encoding') and sys.stdout.encoding: | |
1219 | try: |
|
1219 | try: | |
1220 | u'\xb5'.encode(sys.stdout.encoding) |
|
1220 | u'\xb5'.encode(sys.stdout.encoding) | |
1221 | units = [u"s", u"ms",u'\xb5s',"ns"] |
|
1221 | units = [u"s", u"ms",u'\xb5s',"ns"] | |
1222 | except: |
|
1222 | except: | |
1223 | pass |
|
1223 | pass | |
1224 | scaling = [1, 1e3, 1e6, 1e9] |
|
1224 | scaling = [1, 1e3, 1e6, 1e9] | |
1225 |
|
1225 | |||
1226 | if timespan > 0.0: |
|
1226 | if timespan > 0.0: | |
1227 | order = min(-int(math.floor(math.log10(timespan)) // 3), 3) |
|
1227 | order = min(-int(math.floor(math.log10(timespan)) // 3), 3) | |
1228 | else: |
|
1228 | else: | |
1229 | order = 3 |
|
1229 | order = 3 | |
1230 | ret = u"%.*g %s" % (precision, timespan * scaling[order], units[order]) |
|
1230 | ret = u"%.*g %s" % (precision, timespan * scaling[order], units[order]) | |
1231 | return ret |
|
1231 | return ret |
General Comments 0
You need to be logged in to leave comments.
Login now