Show More
@@ -0,0 +1,89 b'' | |||
|
1 | """Tests for the object inspection functionality. | |
|
2 | """ | |
|
3 | #----------------------------------------------------------------------------- | |
|
4 | # Copyright (C) 2010 The IPython Development Team. | |
|
5 | # | |
|
6 | # Distributed under the terms of the BSD License. | |
|
7 | # | |
|
8 | # The full license is in the file COPYING.txt, distributed with this software. | |
|
9 | #----------------------------------------------------------------------------- | |
|
10 | ||
|
11 | #----------------------------------------------------------------------------- | |
|
12 | # Imports | |
|
13 | #----------------------------------------------------------------------------- | |
|
14 | from __future__ import print_function | |
|
15 | ||
|
16 | # Stdlib imports | |
|
17 | ||
|
18 | # Third-party imports | |
|
19 | import nose.tools as nt | |
|
20 | ||
|
21 | # Our own imports | |
|
22 | from .. import oinspect | |
|
23 | ||
|
24 | #----------------------------------------------------------------------------- | |
|
25 | # Globals and constants | |
|
26 | #----------------------------------------------------------------------------- | |
|
27 | ||
|
28 | inspector = oinspect.Inspector() | |
|
29 | ||
|
30 | #----------------------------------------------------------------------------- | |
|
31 | # Local utilities | |
|
32 | #----------------------------------------------------------------------------- | |
|
33 | ||
|
34 | # A few generic objects we can then inspect in the tests below | |
|
35 | ||
|
36 | class Call(object): | |
|
37 | """This is the class docstring.""" | |
|
38 | ||
|
39 | def __init__(self, x, y=1): | |
|
40 | """This is the constructor docstring.""" | |
|
41 | ||
|
42 | def __call__(self, *a, **kw): | |
|
43 | """This is the call docstring.""" | |
|
44 | ||
|
45 | def method(self, x, z=2): | |
|
46 | """Some method's docstring""" | |
|
47 | ||
|
48 | def f(x, y=2, *a, **kw): | |
|
49 | """A simple function.""" | |
|
50 | ||
|
51 | def g(y, z=3, *a, **kw): | |
|
52 | pass # no docstring | |
|
53 | ||
|
54 | ||
|
55 | def check_calltip(obj, name, call, docstring): | |
|
56 | """Generic check pattern all calltip tests will use""" | |
|
57 | info = inspector.info(obj, name) | |
|
58 | call_line, ds = oinspect.call_tip(info) | |
|
59 | nt.assert_equal(call_line, call) | |
|
60 | nt.assert_equal(ds, docstring) | |
|
61 | ||
|
62 | #----------------------------------------------------------------------------- | |
|
63 | # Tests | |
|
64 | #----------------------------------------------------------------------------- | |
|
65 | ||
|
66 | def test_calltip_class(): | |
|
67 | check_calltip(Call, 'Call', 'Call(x, y=1)', Call.__init__.__doc__) | |
|
68 | ||
|
69 | ||
|
70 | def test_calltip_instance(): | |
|
71 | c = Call(1) | |
|
72 | check_calltip(c, 'c', 'c(*a, **kw)', c.__call__.__doc__) | |
|
73 | ||
|
74 | ||
|
75 | def test_calltip_method(): | |
|
76 | c = Call(1) | |
|
77 | check_calltip(c.method, 'c.method', 'c.method(x, z=2)', c.method.__doc__) | |
|
78 | ||
|
79 | ||
|
80 | def test_calltip_function(): | |
|
81 | check_calltip(f, 'f', 'f(x, y=2, *a, **kw)', f.__doc__) | |
|
82 | ||
|
83 | ||
|
84 | def test_calltip_function2(): | |
|
85 | check_calltip(g, 'g', 'g(y, z=3, *a, **kw)', '<no docstring>') | |
|
86 | ||
|
87 | ||
|
88 | def test_calltip_builtin(): | |
|
89 | check_calltip(sum, 'sum', None, sum.__doc__) |
@@ -1,2572 +1,2573 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """Main IPython class.""" |
|
3 | 3 | |
|
4 | 4 | #----------------------------------------------------------------------------- |
|
5 | 5 | # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> |
|
6 | 6 | # Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu> |
|
7 | 7 | # Copyright (C) 2008-2010 The IPython Development Team |
|
8 | 8 | # |
|
9 | 9 | # Distributed under the terms of the BSD License. The full license is in |
|
10 | 10 | # the file COPYING, distributed as part of this software. |
|
11 | 11 | #----------------------------------------------------------------------------- |
|
12 | 12 | |
|
13 | 13 | #----------------------------------------------------------------------------- |
|
14 | 14 | # Imports |
|
15 | 15 | #----------------------------------------------------------------------------- |
|
16 | 16 | |
|
17 | 17 | from __future__ import with_statement |
|
18 | 18 | from __future__ import absolute_import |
|
19 | 19 | |
|
20 | 20 | import __builtin__ |
|
21 | 21 | import __future__ |
|
22 | 22 | import abc |
|
23 | 23 | import atexit |
|
24 | 24 | import codeop |
|
25 | 25 | import exceptions |
|
26 | 26 | import new |
|
27 | 27 | import os |
|
28 | 28 | import re |
|
29 | 29 | import string |
|
30 | 30 | import sys |
|
31 | 31 | import tempfile |
|
32 | 32 | from contextlib import nested |
|
33 | 33 | |
|
34 | 34 | from IPython.config.configurable import Configurable |
|
35 | 35 | from IPython.core import debugger, oinspect |
|
36 | 36 | from IPython.core import history as ipcorehist |
|
37 | 37 | from IPython.core import page |
|
38 | 38 | from IPython.core import prefilter |
|
39 | 39 | from IPython.core import shadowns |
|
40 | 40 | from IPython.core import ultratb |
|
41 | 41 | from IPython.core.alias import AliasManager |
|
42 | 42 | from IPython.core.builtin_trap import BuiltinTrap |
|
43 | 43 | from IPython.core.display_trap import DisplayTrap |
|
44 | 44 | from IPython.core.displayhook import DisplayHook |
|
45 | 45 | from IPython.core.error import TryNext, UsageError |
|
46 | 46 | from IPython.core.extensions import ExtensionManager |
|
47 | 47 | from IPython.core.fakemodule import FakeModule, init_fakemod_dict |
|
48 | 48 | from IPython.core.inputlist import InputList |
|
49 | 49 | from IPython.core.inputsplitter import IPythonInputSplitter |
|
50 | 50 | from IPython.core.logger import Logger |
|
51 | 51 | from IPython.core.magic import Magic |
|
52 | 52 | from IPython.core.payload import PayloadManager |
|
53 | 53 | from IPython.core.plugin import PluginManager |
|
54 | 54 | from IPython.core.prefilter import PrefilterManager, ESC_MAGIC |
|
55 | 55 | from IPython.external.Itpl import ItplNS |
|
56 | 56 | from IPython.utils import PyColorize |
|
57 | 57 | from IPython.utils import io |
|
58 | 58 | from IPython.utils import pickleshare |
|
59 | 59 | from IPython.utils.doctestreload import doctest_reload |
|
60 | 60 | from IPython.utils.io import ask_yes_no, rprint |
|
61 | 61 | from IPython.utils.ipstruct import Struct |
|
62 | 62 | from IPython.utils.path import get_home_dir, get_ipython_dir, HomeDirError |
|
63 | 63 | from IPython.utils.process import system, getoutput |
|
64 | 64 | from IPython.utils.strdispatch import StrDispatch |
|
65 | 65 | from IPython.utils.syspathcontext import prepended_to_syspath |
|
66 | 66 | from IPython.utils.text import num_ini_spaces, format_screen, LSString, SList |
|
67 | 67 | from IPython.utils.traitlets import (Int, Str, CBool, CaselessStrEnum, Enum, |
|
68 | 68 | List, Unicode, Instance, Type) |
|
69 | 69 | from IPython.utils.warn import warn, error, fatal |
|
70 | 70 | import IPython.core.hooks |
|
71 | 71 | |
|
72 | 72 | #----------------------------------------------------------------------------- |
|
73 | 73 | # Globals |
|
74 | 74 | #----------------------------------------------------------------------------- |
|
75 | 75 | |
|
76 | 76 | # compiled regexps for autoindent management |
|
77 | 77 | dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass') |
|
78 | 78 | |
|
79 | 79 | #----------------------------------------------------------------------------- |
|
80 | 80 | # Utilities |
|
81 | 81 | #----------------------------------------------------------------------------- |
|
82 | 82 | |
|
83 | 83 | # store the builtin raw_input globally, and use this always, in case user code |
|
84 | 84 | # overwrites it (like wx.py.PyShell does) |
|
85 | 85 | raw_input_original = raw_input |
|
86 | 86 | |
|
87 | 87 | def softspace(file, newvalue): |
|
88 | 88 | """Copied from code.py, to remove the dependency""" |
|
89 | 89 | |
|
90 | 90 | oldvalue = 0 |
|
91 | 91 | try: |
|
92 | 92 | oldvalue = file.softspace |
|
93 | 93 | except AttributeError: |
|
94 | 94 | pass |
|
95 | 95 | try: |
|
96 | 96 | file.softspace = newvalue |
|
97 | 97 | except (AttributeError, TypeError): |
|
98 | 98 | # "attribute-less object" or "read-only attributes" |
|
99 | 99 | pass |
|
100 | 100 | return oldvalue |
|
101 | 101 | |
|
102 | 102 | |
|
103 | 103 | def no_op(*a, **kw): pass |
|
104 | 104 | |
|
105 | 105 | class SpaceInInput(exceptions.Exception): pass |
|
106 | 106 | |
|
107 | 107 | class Bunch: pass |
|
108 | 108 | |
|
109 | 109 | |
|
110 | 110 | def get_default_colors(): |
|
111 | 111 | if sys.platform=='darwin': |
|
112 | 112 | return "LightBG" |
|
113 | 113 | elif os.name=='nt': |
|
114 | 114 | return 'Linux' |
|
115 | 115 | else: |
|
116 | 116 | return 'Linux' |
|
117 | 117 | |
|
118 | 118 | |
|
119 | 119 | class SeparateStr(Str): |
|
120 | 120 | """A Str subclass to validate separate_in, separate_out, etc. |
|
121 | 121 | |
|
122 | 122 | This is a Str based trait that converts '0'->'' and '\\n'->'\n'. |
|
123 | 123 | """ |
|
124 | 124 | |
|
125 | 125 | def validate(self, obj, value): |
|
126 | 126 | if value == '0': value = '' |
|
127 | 127 | value = value.replace('\\n','\n') |
|
128 | 128 | return super(SeparateStr, self).validate(obj, value) |
|
129 | 129 | |
|
130 | 130 | class MultipleInstanceError(Exception): |
|
131 | 131 | pass |
|
132 | 132 | |
|
133 | 133 | |
|
134 | 134 | #----------------------------------------------------------------------------- |
|
135 | 135 | # Main IPython class |
|
136 | 136 | #----------------------------------------------------------------------------- |
|
137 | 137 | |
|
138 | 138 | |
|
139 | 139 | class InteractiveShell(Configurable, Magic): |
|
140 | 140 | """An enhanced, interactive shell for Python.""" |
|
141 | 141 | |
|
142 | 142 | _instance = None |
|
143 | 143 | autocall = Enum((0,1,2), default_value=1, config=True) |
|
144 | 144 | # TODO: remove all autoindent logic and put into frontends. |
|
145 | 145 | # We can't do this yet because even runlines uses the autoindent. |
|
146 | 146 | autoindent = CBool(True, config=True) |
|
147 | 147 | automagic = CBool(True, config=True) |
|
148 | 148 | cache_size = Int(1000, config=True) |
|
149 | 149 | color_info = CBool(True, config=True) |
|
150 | 150 | colors = CaselessStrEnum(('NoColor','LightBG','Linux'), |
|
151 | 151 | default_value=get_default_colors(), config=True) |
|
152 | 152 | debug = CBool(False, config=True) |
|
153 | 153 | deep_reload = CBool(False, config=True) |
|
154 | 154 | displayhook_class = Type(DisplayHook) |
|
155 | 155 | exit_now = CBool(False) |
|
156 | 156 | filename = Str("<ipython console>") |
|
157 | 157 | ipython_dir= Unicode('', config=True) # Set to get_ipython_dir() in __init__ |
|
158 | 158 | |
|
159 | 159 | # Input splitter, to split entire cells of input into either individual |
|
160 | 160 | # interactive statements or whole blocks. |
|
161 | 161 | input_splitter = Instance('IPython.core.inputsplitter.IPythonInputSplitter', |
|
162 | 162 | (), {}) |
|
163 | 163 | logstart = CBool(False, config=True) |
|
164 | 164 | logfile = Str('', config=True) |
|
165 | 165 | logappend = Str('', config=True) |
|
166 | 166 | object_info_string_level = Enum((0,1,2), default_value=0, |
|
167 | 167 | config=True) |
|
168 | 168 | pdb = CBool(False, config=True) |
|
169 | 169 | |
|
170 | 170 | pprint = CBool(True, config=True) |
|
171 | 171 | profile = Str('', config=True) |
|
172 | 172 | prompt_in1 = Str('In [\\#]: ', config=True) |
|
173 | 173 | prompt_in2 = Str(' .\\D.: ', config=True) |
|
174 | 174 | prompt_out = Str('Out[\\#]: ', config=True) |
|
175 | 175 | prompts_pad_left = CBool(True, config=True) |
|
176 | 176 | quiet = CBool(False, config=True) |
|
177 | 177 | |
|
178 | 178 | # The readline stuff will eventually be moved to the terminal subclass |
|
179 | 179 | # but for now, we can't do that as readline is welded in everywhere. |
|
180 | 180 | readline_use = CBool(True, config=True) |
|
181 | 181 | readline_merge_completions = CBool(True, config=True) |
|
182 | 182 | readline_omit__names = Enum((0,1,2), default_value=0, config=True) |
|
183 | 183 | readline_remove_delims = Str('-/~', config=True) |
|
184 | 184 | readline_parse_and_bind = List([ |
|
185 | 185 | 'tab: complete', |
|
186 | 186 | '"\C-l": clear-screen', |
|
187 | 187 | 'set show-all-if-ambiguous on', |
|
188 | 188 | '"\C-o": tab-insert', |
|
189 | 189 | '"\M-i": " "', |
|
190 | 190 | '"\M-o": "\d\d\d\d"', |
|
191 | 191 | '"\M-I": "\d\d\d\d"', |
|
192 | 192 | '"\C-r": reverse-search-history', |
|
193 | 193 | '"\C-s": forward-search-history', |
|
194 | 194 | '"\C-p": history-search-backward', |
|
195 | 195 | '"\C-n": history-search-forward', |
|
196 | 196 | '"\e[A": history-search-backward', |
|
197 | 197 | '"\e[B": history-search-forward', |
|
198 | 198 | '"\C-k": kill-line', |
|
199 | 199 | '"\C-u": unix-line-discard', |
|
200 | 200 | ], allow_none=False, config=True) |
|
201 | 201 | |
|
202 | 202 | # TODO: this part of prompt management should be moved to the frontends. |
|
203 | 203 | # Use custom TraitTypes that convert '0'->'' and '\\n'->'\n' |
|
204 | 204 | separate_in = SeparateStr('\n', config=True) |
|
205 | 205 | separate_out = SeparateStr('', config=True) |
|
206 | 206 | separate_out2 = SeparateStr('', config=True) |
|
207 | 207 | wildcards_case_sensitive = CBool(True, config=True) |
|
208 | 208 | xmode = CaselessStrEnum(('Context','Plain', 'Verbose'), |
|
209 | 209 | default_value='Context', config=True) |
|
210 | 210 | |
|
211 | 211 | # Subcomponents of InteractiveShell |
|
212 | 212 | alias_manager = Instance('IPython.core.alias.AliasManager') |
|
213 | 213 | prefilter_manager = Instance('IPython.core.prefilter.PrefilterManager') |
|
214 | 214 | builtin_trap = Instance('IPython.core.builtin_trap.BuiltinTrap') |
|
215 | 215 | display_trap = Instance('IPython.core.display_trap.DisplayTrap') |
|
216 | 216 | extension_manager = Instance('IPython.core.extensions.ExtensionManager') |
|
217 | 217 | plugin_manager = Instance('IPython.core.plugin.PluginManager') |
|
218 | 218 | payload_manager = Instance('IPython.core.payload.PayloadManager') |
|
219 | 219 | |
|
220 | 220 | # Private interface |
|
221 | 221 | _post_execute = set() |
|
222 | 222 | |
|
223 | 223 | def __init__(self, config=None, ipython_dir=None, |
|
224 | 224 | user_ns=None, user_global_ns=None, |
|
225 | 225 | custom_exceptions=((), None)): |
|
226 | 226 | |
|
227 | 227 | # This is where traits with a config_key argument are updated |
|
228 | 228 | # from the values on config. |
|
229 | 229 | super(InteractiveShell, self).__init__(config=config) |
|
230 | 230 | |
|
231 | 231 | # These are relatively independent and stateless |
|
232 | 232 | self.init_ipython_dir(ipython_dir) |
|
233 | 233 | self.init_instance_attrs() |
|
234 | 234 | self.init_environment() |
|
235 | 235 | |
|
236 | 236 | # Create namespaces (user_ns, user_global_ns, etc.) |
|
237 | 237 | self.init_create_namespaces(user_ns, user_global_ns) |
|
238 | 238 | # This has to be done after init_create_namespaces because it uses |
|
239 | 239 | # something in self.user_ns, but before init_sys_modules, which |
|
240 | 240 | # is the first thing to modify sys. |
|
241 | 241 | # TODO: When we override sys.stdout and sys.stderr before this class |
|
242 | 242 | # is created, we are saving the overridden ones here. Not sure if this |
|
243 | 243 | # is what we want to do. |
|
244 | 244 | self.save_sys_module_state() |
|
245 | 245 | self.init_sys_modules() |
|
246 | 246 | |
|
247 | 247 | self.init_history() |
|
248 | 248 | self.init_encoding() |
|
249 | 249 | self.init_prefilter() |
|
250 | 250 | |
|
251 | 251 | Magic.__init__(self, self) |
|
252 | 252 | |
|
253 | 253 | self.init_syntax_highlighting() |
|
254 | 254 | self.init_hooks() |
|
255 | 255 | self.init_pushd_popd_magic() |
|
256 | 256 | # self.init_traceback_handlers use to be here, but we moved it below |
|
257 | 257 | # because it and init_io have to come after init_readline. |
|
258 | 258 | self.init_user_ns() |
|
259 | 259 | self.init_logger() |
|
260 | 260 | self.init_alias() |
|
261 | 261 | self.init_builtins() |
|
262 | 262 | |
|
263 | 263 | # pre_config_initialization |
|
264 | 264 | self.init_shadow_hist() |
|
265 | 265 | |
|
266 | 266 | # The next section should contain everything that was in ipmaker. |
|
267 | 267 | self.init_logstart() |
|
268 | 268 | |
|
269 | 269 | # The following was in post_config_initialization |
|
270 | 270 | self.init_inspector() |
|
271 | 271 | # init_readline() must come before init_io(), because init_io uses |
|
272 | 272 | # readline related things. |
|
273 | 273 | self.init_readline() |
|
274 | 274 | # init_completer must come after init_readline, because it needs to |
|
275 | 275 | # know whether readline is present or not system-wide to configure the |
|
276 | 276 | # completers, since the completion machinery can now operate |
|
277 | 277 | # independently of readline (e.g. over the network) |
|
278 | 278 | self.init_completer() |
|
279 | 279 | # TODO: init_io() needs to happen before init_traceback handlers |
|
280 | 280 | # because the traceback handlers hardcode the stdout/stderr streams. |
|
281 | 281 | # This logic in in debugger.Pdb and should eventually be changed. |
|
282 | 282 | self.init_io() |
|
283 | 283 | self.init_traceback_handlers(custom_exceptions) |
|
284 | 284 | self.init_prompts() |
|
285 | 285 | self.init_displayhook() |
|
286 | 286 | self.init_reload_doctest() |
|
287 | 287 | self.init_magics() |
|
288 | 288 | self.init_pdb() |
|
289 | 289 | self.init_extension_manager() |
|
290 | 290 | self.init_plugin_manager() |
|
291 | 291 | self.init_payload() |
|
292 | 292 | self.hooks.late_startup_hook() |
|
293 | 293 | atexit.register(self.atexit_operations) |
|
294 | 294 | |
|
295 | 295 | @classmethod |
|
296 | 296 | def instance(cls, *args, **kwargs): |
|
297 | 297 | """Returns a global InteractiveShell instance.""" |
|
298 | 298 | if cls._instance is None: |
|
299 | 299 | inst = cls(*args, **kwargs) |
|
300 | 300 | # Now make sure that the instance will also be returned by |
|
301 | 301 | # the subclasses instance attribute. |
|
302 | 302 | for subclass in cls.mro(): |
|
303 | 303 | if issubclass(cls, subclass) and \ |
|
304 | 304 | issubclass(subclass, InteractiveShell): |
|
305 | 305 | subclass._instance = inst |
|
306 | 306 | else: |
|
307 | 307 | break |
|
308 | 308 | if isinstance(cls._instance, cls): |
|
309 | 309 | return cls._instance |
|
310 | 310 | else: |
|
311 | 311 | raise MultipleInstanceError( |
|
312 | 312 | 'Multiple incompatible subclass instances of ' |
|
313 | 313 | 'InteractiveShell are being created.' |
|
314 | 314 | ) |
|
315 | 315 | |
|
316 | 316 | @classmethod |
|
317 | 317 | def initialized(cls): |
|
318 | 318 | return hasattr(cls, "_instance") |
|
319 | 319 | |
|
320 | 320 | def get_ipython(self): |
|
321 | 321 | """Return the currently running IPython instance.""" |
|
322 | 322 | return self |
|
323 | 323 | |
|
324 | 324 | #------------------------------------------------------------------------- |
|
325 | 325 | # Trait changed handlers |
|
326 | 326 | #------------------------------------------------------------------------- |
|
327 | 327 | |
|
328 | 328 | def _ipython_dir_changed(self, name, new): |
|
329 | 329 | if not os.path.isdir(new): |
|
330 | 330 | os.makedirs(new, mode = 0777) |
|
331 | 331 | |
|
332 | 332 | def set_autoindent(self,value=None): |
|
333 | 333 | """Set the autoindent flag, checking for readline support. |
|
334 | 334 | |
|
335 | 335 | If called with no arguments, it acts as a toggle.""" |
|
336 | 336 | |
|
337 | 337 | if not self.has_readline: |
|
338 | 338 | if os.name == 'posix': |
|
339 | 339 | warn("The auto-indent feature requires the readline library") |
|
340 | 340 | self.autoindent = 0 |
|
341 | 341 | return |
|
342 | 342 | if value is None: |
|
343 | 343 | self.autoindent = not self.autoindent |
|
344 | 344 | else: |
|
345 | 345 | self.autoindent = value |
|
346 | 346 | |
|
347 | 347 | #------------------------------------------------------------------------- |
|
348 | 348 | # init_* methods called by __init__ |
|
349 | 349 | #------------------------------------------------------------------------- |
|
350 | 350 | |
|
351 | 351 | def init_ipython_dir(self, ipython_dir): |
|
352 | 352 | if ipython_dir is not None: |
|
353 | 353 | self.ipython_dir = ipython_dir |
|
354 | 354 | self.config.Global.ipython_dir = self.ipython_dir |
|
355 | 355 | return |
|
356 | 356 | |
|
357 | 357 | if hasattr(self.config.Global, 'ipython_dir'): |
|
358 | 358 | self.ipython_dir = self.config.Global.ipython_dir |
|
359 | 359 | else: |
|
360 | 360 | self.ipython_dir = get_ipython_dir() |
|
361 | 361 | |
|
362 | 362 | # All children can just read this |
|
363 | 363 | self.config.Global.ipython_dir = self.ipython_dir |
|
364 | 364 | |
|
365 | 365 | def init_instance_attrs(self): |
|
366 | 366 | self.more = False |
|
367 | 367 | |
|
368 | 368 | # command compiler |
|
369 | 369 | self.compile = codeop.CommandCompiler() |
|
370 | 370 | |
|
371 | 371 | # User input buffer |
|
372 | 372 | self.buffer = [] |
|
373 | 373 | |
|
374 | 374 | # Make an empty namespace, which extension writers can rely on both |
|
375 | 375 | # existing and NEVER being used by ipython itself. This gives them a |
|
376 | 376 | # convenient location for storing additional information and state |
|
377 | 377 | # their extensions may require, without fear of collisions with other |
|
378 | 378 | # ipython names that may develop later. |
|
379 | 379 | self.meta = Struct() |
|
380 | 380 | |
|
381 | 381 | # Object variable to store code object waiting execution. This is |
|
382 | 382 | # used mainly by the multithreaded shells, but it can come in handy in |
|
383 | 383 | # other situations. No need to use a Queue here, since it's a single |
|
384 | 384 | # item which gets cleared once run. |
|
385 | 385 | self.code_to_run = None |
|
386 | 386 | |
|
387 | 387 | # Temporary files used for various purposes. Deleted at exit. |
|
388 | 388 | self.tempfiles = [] |
|
389 | 389 | |
|
390 | 390 | # Keep track of readline usage (later set by init_readline) |
|
391 | 391 | self.has_readline = False |
|
392 | 392 | |
|
393 | 393 | # keep track of where we started running (mainly for crash post-mortem) |
|
394 | 394 | # This is not being used anywhere currently. |
|
395 | 395 | self.starting_dir = os.getcwd() |
|
396 | 396 | |
|
397 | 397 | # Indentation management |
|
398 | 398 | self.indent_current_nsp = 0 |
|
399 | 399 | |
|
400 | 400 | def init_environment(self): |
|
401 | 401 | """Any changes we need to make to the user's environment.""" |
|
402 | 402 | pass |
|
403 | 403 | |
|
404 | 404 | def init_encoding(self): |
|
405 | 405 | # Get system encoding at startup time. Certain terminals (like Emacs |
|
406 | 406 | # under Win32 have it set to None, and we need to have a known valid |
|
407 | 407 | # encoding to use in the raw_input() method |
|
408 | 408 | try: |
|
409 | 409 | self.stdin_encoding = sys.stdin.encoding or 'ascii' |
|
410 | 410 | except AttributeError: |
|
411 | 411 | self.stdin_encoding = 'ascii' |
|
412 | 412 | |
|
413 | 413 | def init_syntax_highlighting(self): |
|
414 | 414 | # Python source parser/formatter for syntax highlighting |
|
415 | 415 | pyformat = PyColorize.Parser().format |
|
416 | 416 | self.pycolorize = lambda src: pyformat(src,'str',self.colors) |
|
417 | 417 | |
|
418 | 418 | def init_pushd_popd_magic(self): |
|
419 | 419 | # for pushd/popd management |
|
420 | 420 | try: |
|
421 | 421 | self.home_dir = get_home_dir() |
|
422 | 422 | except HomeDirError, msg: |
|
423 | 423 | fatal(msg) |
|
424 | 424 | |
|
425 | 425 | self.dir_stack = [] |
|
426 | 426 | |
|
427 | 427 | def init_logger(self): |
|
428 | 428 | self.logger = Logger(self, logfname='ipython_log.py', logmode='rotate') |
|
429 | 429 | # local shortcut, this is used a LOT |
|
430 | 430 | self.log = self.logger.log |
|
431 | 431 | |
|
432 | 432 | def init_logstart(self): |
|
433 | 433 | if self.logappend: |
|
434 | 434 | self.magic_logstart(self.logappend + ' append') |
|
435 | 435 | elif self.logfile: |
|
436 | 436 | self.magic_logstart(self.logfile) |
|
437 | 437 | elif self.logstart: |
|
438 | 438 | self.magic_logstart() |
|
439 | 439 | |
|
440 | 440 | def init_builtins(self): |
|
441 | 441 | self.builtin_trap = BuiltinTrap(shell=self) |
|
442 | 442 | |
|
443 | 443 | def init_inspector(self): |
|
444 | 444 | # Object inspector |
|
445 | 445 | self.inspector = oinspect.Inspector(oinspect.InspectColors, |
|
446 | 446 | PyColorize.ANSICodeColors, |
|
447 | 447 | 'NoColor', |
|
448 | 448 | self.object_info_string_level) |
|
449 | 449 | |
|
450 | 450 | def init_io(self): |
|
451 | 451 | # This will just use sys.stdout and sys.stderr. If you want to |
|
452 | 452 | # override sys.stdout and sys.stderr themselves, you need to do that |
|
453 | 453 | # *before* instantiating this class, because Term holds onto |
|
454 | 454 | # references to the underlying streams. |
|
455 | 455 | if sys.platform == 'win32' and self.has_readline: |
|
456 | 456 | Term = io.IOTerm(cout=self.readline._outputfile, |
|
457 | 457 | cerr=self.readline._outputfile) |
|
458 | 458 | else: |
|
459 | 459 | Term = io.IOTerm() |
|
460 | 460 | io.Term = Term |
|
461 | 461 | |
|
462 | 462 | def init_prompts(self): |
|
463 | 463 | # TODO: This is a pass for now because the prompts are managed inside |
|
464 | 464 | # the DisplayHook. Once there is a separate prompt manager, this |
|
465 | 465 | # will initialize that object and all prompt related information. |
|
466 | 466 | pass |
|
467 | 467 | |
|
468 | 468 | def init_displayhook(self): |
|
469 | 469 | # Initialize displayhook, set in/out prompts and printing system |
|
470 | 470 | self.displayhook = self.displayhook_class( |
|
471 | 471 | shell=self, |
|
472 | 472 | cache_size=self.cache_size, |
|
473 | 473 | input_sep = self.separate_in, |
|
474 | 474 | output_sep = self.separate_out, |
|
475 | 475 | output_sep2 = self.separate_out2, |
|
476 | 476 | ps1 = self.prompt_in1, |
|
477 | 477 | ps2 = self.prompt_in2, |
|
478 | 478 | ps_out = self.prompt_out, |
|
479 | 479 | pad_left = self.prompts_pad_left |
|
480 | 480 | ) |
|
481 | 481 | # This is a context manager that installs/revmoes the displayhook at |
|
482 | 482 | # the appropriate time. |
|
483 | 483 | self.display_trap = DisplayTrap(hook=self.displayhook) |
|
484 | 484 | |
|
485 | 485 | def init_reload_doctest(self): |
|
486 | 486 | # Do a proper resetting of doctest, including the necessary displayhook |
|
487 | 487 | # monkeypatching |
|
488 | 488 | try: |
|
489 | 489 | doctest_reload() |
|
490 | 490 | except ImportError: |
|
491 | 491 | warn("doctest module does not exist.") |
|
492 | 492 | |
|
493 | 493 | #------------------------------------------------------------------------- |
|
494 | 494 | # Things related to injections into the sys module |
|
495 | 495 | #------------------------------------------------------------------------- |
|
496 | 496 | |
|
497 | 497 | def save_sys_module_state(self): |
|
498 | 498 | """Save the state of hooks in the sys module. |
|
499 | 499 | |
|
500 | 500 | This has to be called after self.user_ns is created. |
|
501 | 501 | """ |
|
502 | 502 | self._orig_sys_module_state = {} |
|
503 | 503 | self._orig_sys_module_state['stdin'] = sys.stdin |
|
504 | 504 | self._orig_sys_module_state['stdout'] = sys.stdout |
|
505 | 505 | self._orig_sys_module_state['stderr'] = sys.stderr |
|
506 | 506 | self._orig_sys_module_state['excepthook'] = sys.excepthook |
|
507 | 507 | try: |
|
508 | 508 | self._orig_sys_modules_main_name = self.user_ns['__name__'] |
|
509 | 509 | except KeyError: |
|
510 | 510 | pass |
|
511 | 511 | |
|
512 | 512 | def restore_sys_module_state(self): |
|
513 | 513 | """Restore the state of the sys module.""" |
|
514 | 514 | try: |
|
515 | 515 | for k, v in self._orig_sys_module_state.items(): |
|
516 | 516 | setattr(sys, k, v) |
|
517 | 517 | except AttributeError: |
|
518 | 518 | pass |
|
519 | 519 | # Reset what what done in self.init_sys_modules |
|
520 | 520 | try: |
|
521 | 521 | sys.modules[self.user_ns['__name__']] = self._orig_sys_modules_main_name |
|
522 | 522 | except (AttributeError, KeyError): |
|
523 | 523 | pass |
|
524 | 524 | |
|
525 | 525 | #------------------------------------------------------------------------- |
|
526 | 526 | # Things related to hooks |
|
527 | 527 | #------------------------------------------------------------------------- |
|
528 | 528 | |
|
529 | 529 | def init_hooks(self): |
|
530 | 530 | # hooks holds pointers used for user-side customizations |
|
531 | 531 | self.hooks = Struct() |
|
532 | 532 | |
|
533 | 533 | self.strdispatchers = {} |
|
534 | 534 | |
|
535 | 535 | # Set all default hooks, defined in the IPython.hooks module. |
|
536 | 536 | hooks = IPython.core.hooks |
|
537 | 537 | for hook_name in hooks.__all__: |
|
538 | 538 | # default hooks have priority 100, i.e. low; user hooks should have |
|
539 | 539 | # 0-100 priority |
|
540 | 540 | self.set_hook(hook_name,getattr(hooks,hook_name), 100) |
|
541 | 541 | |
|
542 | 542 | def set_hook(self,name,hook, priority = 50, str_key = None, re_key = None): |
|
543 | 543 | """set_hook(name,hook) -> sets an internal IPython hook. |
|
544 | 544 | |
|
545 | 545 | IPython exposes some of its internal API as user-modifiable hooks. By |
|
546 | 546 | adding your function to one of these hooks, you can modify IPython's |
|
547 | 547 | behavior to call at runtime your own routines.""" |
|
548 | 548 | |
|
549 | 549 | # At some point in the future, this should validate the hook before it |
|
550 | 550 | # accepts it. Probably at least check that the hook takes the number |
|
551 | 551 | # of args it's supposed to. |
|
552 | 552 | |
|
553 | 553 | f = new.instancemethod(hook,self,self.__class__) |
|
554 | 554 | |
|
555 | 555 | # check if the hook is for strdispatcher first |
|
556 | 556 | if str_key is not None: |
|
557 | 557 | sdp = self.strdispatchers.get(name, StrDispatch()) |
|
558 | 558 | sdp.add_s(str_key, f, priority ) |
|
559 | 559 | self.strdispatchers[name] = sdp |
|
560 | 560 | return |
|
561 | 561 | if re_key is not None: |
|
562 | 562 | sdp = self.strdispatchers.get(name, StrDispatch()) |
|
563 | 563 | sdp.add_re(re.compile(re_key), f, priority ) |
|
564 | 564 | self.strdispatchers[name] = sdp |
|
565 | 565 | return |
|
566 | 566 | |
|
567 | 567 | dp = getattr(self.hooks, name, None) |
|
568 | 568 | if name not in IPython.core.hooks.__all__: |
|
569 | 569 | print "Warning! Hook '%s' is not one of %s" % \ |
|
570 | 570 | (name, IPython.core.hooks.__all__ ) |
|
571 | 571 | if not dp: |
|
572 | 572 | dp = IPython.core.hooks.CommandChainDispatcher() |
|
573 | 573 | |
|
574 | 574 | try: |
|
575 | 575 | dp.add(f,priority) |
|
576 | 576 | except AttributeError: |
|
577 | 577 | # it was not commandchain, plain old func - replace |
|
578 | 578 | dp = f |
|
579 | 579 | |
|
580 | 580 | setattr(self.hooks,name, dp) |
|
581 | 581 | |
|
582 | 582 | def register_post_execute(self, func): |
|
583 | 583 | """Register a function for calling after code execution. |
|
584 | 584 | """ |
|
585 | 585 | if not callable(func): |
|
586 | 586 | raise ValueError('argument %s must be callable' % func) |
|
587 | 587 | self._post_execute.add(func) |
|
588 | 588 | |
|
589 | 589 | #------------------------------------------------------------------------- |
|
590 | 590 | # Things related to the "main" module |
|
591 | 591 | #------------------------------------------------------------------------- |
|
592 | 592 | |
|
593 | 593 | def new_main_mod(self,ns=None): |
|
594 | 594 | """Return a new 'main' module object for user code execution. |
|
595 | 595 | """ |
|
596 | 596 | main_mod = self._user_main_module |
|
597 | 597 | init_fakemod_dict(main_mod,ns) |
|
598 | 598 | return main_mod |
|
599 | 599 | |
|
600 | 600 | def cache_main_mod(self,ns,fname): |
|
601 | 601 | """Cache a main module's namespace. |
|
602 | 602 | |
|
603 | 603 | When scripts are executed via %run, we must keep a reference to the |
|
604 | 604 | namespace of their __main__ module (a FakeModule instance) around so |
|
605 | 605 | that Python doesn't clear it, rendering objects defined therein |
|
606 | 606 | useless. |
|
607 | 607 | |
|
608 | 608 | This method keeps said reference in a private dict, keyed by the |
|
609 | 609 | absolute path of the module object (which corresponds to the script |
|
610 | 610 | path). This way, for multiple executions of the same script we only |
|
611 | 611 | keep one copy of the namespace (the last one), thus preventing memory |
|
612 | 612 | leaks from old references while allowing the objects from the last |
|
613 | 613 | execution to be accessible. |
|
614 | 614 | |
|
615 | 615 | Note: we can not allow the actual FakeModule instances to be deleted, |
|
616 | 616 | because of how Python tears down modules (it hard-sets all their |
|
617 | 617 | references to None without regard for reference counts). This method |
|
618 | 618 | must therefore make a *copy* of the given namespace, to allow the |
|
619 | 619 | original module's __dict__ to be cleared and reused. |
|
620 | 620 | |
|
621 | 621 | |
|
622 | 622 | Parameters |
|
623 | 623 | ---------- |
|
624 | 624 | ns : a namespace (a dict, typically) |
|
625 | 625 | |
|
626 | 626 | fname : str |
|
627 | 627 | Filename associated with the namespace. |
|
628 | 628 | |
|
629 | 629 | Examples |
|
630 | 630 | -------- |
|
631 | 631 | |
|
632 | 632 | In [10]: import IPython |
|
633 | 633 | |
|
634 | 634 | In [11]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__) |
|
635 | 635 | |
|
636 | 636 | In [12]: IPython.__file__ in _ip._main_ns_cache |
|
637 | 637 | Out[12]: True |
|
638 | 638 | """ |
|
639 | 639 | self._main_ns_cache[os.path.abspath(fname)] = ns.copy() |
|
640 | 640 | |
|
641 | 641 | def clear_main_mod_cache(self): |
|
642 | 642 | """Clear the cache of main modules. |
|
643 | 643 | |
|
644 | 644 | Mainly for use by utilities like %reset. |
|
645 | 645 | |
|
646 | 646 | Examples |
|
647 | 647 | -------- |
|
648 | 648 | |
|
649 | 649 | In [15]: import IPython |
|
650 | 650 | |
|
651 | 651 | In [16]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__) |
|
652 | 652 | |
|
653 | 653 | In [17]: len(_ip._main_ns_cache) > 0 |
|
654 | 654 | Out[17]: True |
|
655 | 655 | |
|
656 | 656 | In [18]: _ip.clear_main_mod_cache() |
|
657 | 657 | |
|
658 | 658 | In [19]: len(_ip._main_ns_cache) == 0 |
|
659 | 659 | Out[19]: True |
|
660 | 660 | """ |
|
661 | 661 | self._main_ns_cache.clear() |
|
662 | 662 | |
|
663 | 663 | #------------------------------------------------------------------------- |
|
664 | 664 | # Things related to debugging |
|
665 | 665 | #------------------------------------------------------------------------- |
|
666 | 666 | |
|
667 | 667 | def init_pdb(self): |
|
668 | 668 | # Set calling of pdb on exceptions |
|
669 | 669 | # self.call_pdb is a property |
|
670 | 670 | self.call_pdb = self.pdb |
|
671 | 671 | |
|
672 | 672 | def _get_call_pdb(self): |
|
673 | 673 | return self._call_pdb |
|
674 | 674 | |
|
675 | 675 | def _set_call_pdb(self,val): |
|
676 | 676 | |
|
677 | 677 | if val not in (0,1,False,True): |
|
678 | 678 | raise ValueError,'new call_pdb value must be boolean' |
|
679 | 679 | |
|
680 | 680 | # store value in instance |
|
681 | 681 | self._call_pdb = val |
|
682 | 682 | |
|
683 | 683 | # notify the actual exception handlers |
|
684 | 684 | self.InteractiveTB.call_pdb = val |
|
685 | 685 | |
|
686 | 686 | call_pdb = property(_get_call_pdb,_set_call_pdb,None, |
|
687 | 687 | 'Control auto-activation of pdb at exceptions') |
|
688 | 688 | |
|
689 | 689 | def debugger(self,force=False): |
|
690 | 690 | """Call the pydb/pdb debugger. |
|
691 | 691 | |
|
692 | 692 | Keywords: |
|
693 | 693 | |
|
694 | 694 | - force(False): by default, this routine checks the instance call_pdb |
|
695 | 695 | flag and does not actually invoke the debugger if the flag is false. |
|
696 | 696 | The 'force' option forces the debugger to activate even if the flag |
|
697 | 697 | is false. |
|
698 | 698 | """ |
|
699 | 699 | |
|
700 | 700 | if not (force or self.call_pdb): |
|
701 | 701 | return |
|
702 | 702 | |
|
703 | 703 | if not hasattr(sys,'last_traceback'): |
|
704 | 704 | error('No traceback has been produced, nothing to debug.') |
|
705 | 705 | return |
|
706 | 706 | |
|
707 | 707 | # use pydb if available |
|
708 | 708 | if debugger.has_pydb: |
|
709 | 709 | from pydb import pm |
|
710 | 710 | else: |
|
711 | 711 | # fallback to our internal debugger |
|
712 | 712 | pm = lambda : self.InteractiveTB.debugger(force=True) |
|
713 | 713 | self.history_saving_wrapper(pm)() |
|
714 | 714 | |
|
715 | 715 | #------------------------------------------------------------------------- |
|
716 | 716 | # Things related to IPython's various namespaces |
|
717 | 717 | #------------------------------------------------------------------------- |
|
718 | 718 | |
|
719 | 719 | def init_create_namespaces(self, user_ns=None, user_global_ns=None): |
|
720 | 720 | # Create the namespace where the user will operate. user_ns is |
|
721 | 721 | # normally the only one used, and it is passed to the exec calls as |
|
722 | 722 | # the locals argument. But we do carry a user_global_ns namespace |
|
723 | 723 | # given as the exec 'globals' argument, This is useful in embedding |
|
724 | 724 | # situations where the ipython shell opens in a context where the |
|
725 | 725 | # distinction between locals and globals is meaningful. For |
|
726 | 726 | # non-embedded contexts, it is just the same object as the user_ns dict. |
|
727 | 727 | |
|
728 | 728 | # FIXME. For some strange reason, __builtins__ is showing up at user |
|
729 | 729 | # level as a dict instead of a module. This is a manual fix, but I |
|
730 | 730 | # should really track down where the problem is coming from. Alex |
|
731 | 731 | # Schmolck reported this problem first. |
|
732 | 732 | |
|
733 | 733 | # A useful post by Alex Martelli on this topic: |
|
734 | 734 | # Re: inconsistent value from __builtins__ |
|
735 | 735 | # Von: Alex Martelli <aleaxit@yahoo.com> |
|
736 | 736 | # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends |
|
737 | 737 | # Gruppen: comp.lang.python |
|
738 | 738 | |
|
739 | 739 | # Michael Hohn <hohn@hooknose.lbl.gov> wrote: |
|
740 | 740 | # > >>> print type(builtin_check.get_global_binding('__builtins__')) |
|
741 | 741 | # > <type 'dict'> |
|
742 | 742 | # > >>> print type(__builtins__) |
|
743 | 743 | # > <type 'module'> |
|
744 | 744 | # > Is this difference in return value intentional? |
|
745 | 745 | |
|
746 | 746 | # Well, it's documented that '__builtins__' can be either a dictionary |
|
747 | 747 | # or a module, and it's been that way for a long time. Whether it's |
|
748 | 748 | # intentional (or sensible), I don't know. In any case, the idea is |
|
749 | 749 | # that if you need to access the built-in namespace directly, you |
|
750 | 750 | # should start with "import __builtin__" (note, no 's') which will |
|
751 | 751 | # definitely give you a module. Yeah, it's somewhat confusing:-(. |
|
752 | 752 | |
|
753 | 753 | # These routines return properly built dicts as needed by the rest of |
|
754 | 754 | # the code, and can also be used by extension writers to generate |
|
755 | 755 | # properly initialized namespaces. |
|
756 | 756 | user_ns, user_global_ns = self.make_user_namespaces(user_ns, |
|
757 | 757 | user_global_ns) |
|
758 | 758 | |
|
759 | 759 | # Assign namespaces |
|
760 | 760 | # This is the namespace where all normal user variables live |
|
761 | 761 | self.user_ns = user_ns |
|
762 | 762 | self.user_global_ns = user_global_ns |
|
763 | 763 | |
|
764 | 764 | # An auxiliary namespace that checks what parts of the user_ns were |
|
765 | 765 | # loaded at startup, so we can list later only variables defined in |
|
766 | 766 | # actual interactive use. Since it is always a subset of user_ns, it |
|
767 | 767 | # doesn't need to be separately tracked in the ns_table. |
|
768 | 768 | self.user_ns_hidden = {} |
|
769 | 769 | |
|
770 | 770 | # A namespace to keep track of internal data structures to prevent |
|
771 | 771 | # them from cluttering user-visible stuff. Will be updated later |
|
772 | 772 | self.internal_ns = {} |
|
773 | 773 | |
|
774 | 774 | # Now that FakeModule produces a real module, we've run into a nasty |
|
775 | 775 | # problem: after script execution (via %run), the module where the user |
|
776 | 776 | # code ran is deleted. Now that this object is a true module (needed |
|
777 | 777 | # so docetst and other tools work correctly), the Python module |
|
778 | 778 | # teardown mechanism runs over it, and sets to None every variable |
|
779 | 779 | # present in that module. Top-level references to objects from the |
|
780 | 780 | # script survive, because the user_ns is updated with them. However, |
|
781 | 781 | # calling functions defined in the script that use other things from |
|
782 | 782 | # the script will fail, because the function's closure had references |
|
783 | 783 | # to the original objects, which are now all None. So we must protect |
|
784 | 784 | # these modules from deletion by keeping a cache. |
|
785 | 785 | # |
|
786 | 786 | # To avoid keeping stale modules around (we only need the one from the |
|
787 | 787 | # last run), we use a dict keyed with the full path to the script, so |
|
788 | 788 | # only the last version of the module is held in the cache. Note, |
|
789 | 789 | # however, that we must cache the module *namespace contents* (their |
|
790 | 790 | # __dict__). Because if we try to cache the actual modules, old ones |
|
791 | 791 | # (uncached) could be destroyed while still holding references (such as |
|
792 | 792 | # those held by GUI objects that tend to be long-lived)> |
|
793 | 793 | # |
|
794 | 794 | # The %reset command will flush this cache. See the cache_main_mod() |
|
795 | 795 | # and clear_main_mod_cache() methods for details on use. |
|
796 | 796 | |
|
797 | 797 | # This is the cache used for 'main' namespaces |
|
798 | 798 | self._main_ns_cache = {} |
|
799 | 799 | # And this is the single instance of FakeModule whose __dict__ we keep |
|
800 | 800 | # copying and clearing for reuse on each %run |
|
801 | 801 | self._user_main_module = FakeModule() |
|
802 | 802 | |
|
803 | 803 | # A table holding all the namespaces IPython deals with, so that |
|
804 | 804 | # introspection facilities can search easily. |
|
805 | 805 | self.ns_table = {'user':user_ns, |
|
806 | 806 | 'user_global':user_global_ns, |
|
807 | 807 | 'internal':self.internal_ns, |
|
808 | 808 | 'builtin':__builtin__.__dict__ |
|
809 | 809 | } |
|
810 | 810 | |
|
811 | 811 | # Similarly, track all namespaces where references can be held and that |
|
812 | 812 | # we can safely clear (so it can NOT include builtin). This one can be |
|
813 | 813 | # a simple list. |
|
814 | 814 | self.ns_refs_table = [ user_ns, user_global_ns, self.user_ns_hidden, |
|
815 | 815 | self.internal_ns, self._main_ns_cache ] |
|
816 | 816 | |
|
817 | 817 | def make_user_namespaces(self, user_ns=None, user_global_ns=None): |
|
818 | 818 | """Return a valid local and global user interactive namespaces. |
|
819 | 819 | |
|
820 | 820 | This builds a dict with the minimal information needed to operate as a |
|
821 | 821 | valid IPython user namespace, which you can pass to the various |
|
822 | 822 | embedding classes in ipython. The default implementation returns the |
|
823 | 823 | same dict for both the locals and the globals to allow functions to |
|
824 | 824 | refer to variables in the namespace. Customized implementations can |
|
825 | 825 | return different dicts. The locals dictionary can actually be anything |
|
826 | 826 | following the basic mapping protocol of a dict, but the globals dict |
|
827 | 827 | must be a true dict, not even a subclass. It is recommended that any |
|
828 | 828 | custom object for the locals namespace synchronize with the globals |
|
829 | 829 | dict somehow. |
|
830 | 830 | |
|
831 | 831 | Raises TypeError if the provided globals namespace is not a true dict. |
|
832 | 832 | |
|
833 | 833 | Parameters |
|
834 | 834 | ---------- |
|
835 | 835 | user_ns : dict-like, optional |
|
836 | 836 | The current user namespace. The items in this namespace should |
|
837 | 837 | be included in the output. If None, an appropriate blank |
|
838 | 838 | namespace should be created. |
|
839 | 839 | user_global_ns : dict, optional |
|
840 | 840 | The current user global namespace. The items in this namespace |
|
841 | 841 | should be included in the output. If None, an appropriate |
|
842 | 842 | blank namespace should be created. |
|
843 | 843 | |
|
844 | 844 | Returns |
|
845 | 845 | ------- |
|
846 | 846 | A pair of dictionary-like object to be used as the local namespace |
|
847 | 847 | of the interpreter and a dict to be used as the global namespace. |
|
848 | 848 | """ |
|
849 | 849 | |
|
850 | 850 | |
|
851 | 851 | # We must ensure that __builtin__ (without the final 's') is always |
|
852 | 852 | # available and pointing to the __builtin__ *module*. For more details: |
|
853 | 853 | # http://mail.python.org/pipermail/python-dev/2001-April/014068.html |
|
854 | 854 | |
|
855 | 855 | if user_ns is None: |
|
856 | 856 | # Set __name__ to __main__ to better match the behavior of the |
|
857 | 857 | # normal interpreter. |
|
858 | 858 | user_ns = {'__name__' :'__main__', |
|
859 | 859 | '__builtin__' : __builtin__, |
|
860 | 860 | '__builtins__' : __builtin__, |
|
861 | 861 | } |
|
862 | 862 | else: |
|
863 | 863 | user_ns.setdefault('__name__','__main__') |
|
864 | 864 | user_ns.setdefault('__builtin__',__builtin__) |
|
865 | 865 | user_ns.setdefault('__builtins__',__builtin__) |
|
866 | 866 | |
|
867 | 867 | if user_global_ns is None: |
|
868 | 868 | user_global_ns = user_ns |
|
869 | 869 | if type(user_global_ns) is not dict: |
|
870 | 870 | raise TypeError("user_global_ns must be a true dict; got %r" |
|
871 | 871 | % type(user_global_ns)) |
|
872 | 872 | |
|
873 | 873 | return user_ns, user_global_ns |
|
874 | 874 | |
|
875 | 875 | def init_sys_modules(self): |
|
876 | 876 | # We need to insert into sys.modules something that looks like a |
|
877 | 877 | # module but which accesses the IPython namespace, for shelve and |
|
878 | 878 | # pickle to work interactively. Normally they rely on getting |
|
879 | 879 | # everything out of __main__, but for embedding purposes each IPython |
|
880 | 880 | # instance has its own private namespace, so we can't go shoving |
|
881 | 881 | # everything into __main__. |
|
882 | 882 | |
|
883 | 883 | # note, however, that we should only do this for non-embedded |
|
884 | 884 | # ipythons, which really mimic the __main__.__dict__ with their own |
|
885 | 885 | # namespace. Embedded instances, on the other hand, should not do |
|
886 | 886 | # this because they need to manage the user local/global namespaces |
|
887 | 887 | # only, but they live within a 'normal' __main__ (meaning, they |
|
888 | 888 | # shouldn't overtake the execution environment of the script they're |
|
889 | 889 | # embedded in). |
|
890 | 890 | |
|
891 | 891 | # This is overridden in the InteractiveShellEmbed subclass to a no-op. |
|
892 | 892 | |
|
893 | 893 | try: |
|
894 | 894 | main_name = self.user_ns['__name__'] |
|
895 | 895 | except KeyError: |
|
896 | 896 | raise KeyError('user_ns dictionary MUST have a "__name__" key') |
|
897 | 897 | else: |
|
898 | 898 | sys.modules[main_name] = FakeModule(self.user_ns) |
|
899 | 899 | |
|
900 | 900 | def init_user_ns(self): |
|
901 | 901 | """Initialize all user-visible namespaces to their minimum defaults. |
|
902 | 902 | |
|
903 | 903 | Certain history lists are also initialized here, as they effectively |
|
904 | 904 | act as user namespaces. |
|
905 | 905 | |
|
906 | 906 | Notes |
|
907 | 907 | ----- |
|
908 | 908 | All data structures here are only filled in, they are NOT reset by this |
|
909 | 909 | method. If they were not empty before, data will simply be added to |
|
910 | 910 | therm. |
|
911 | 911 | """ |
|
912 | 912 | # This function works in two parts: first we put a few things in |
|
913 | 913 | # user_ns, and we sync that contents into user_ns_hidden so that these |
|
914 | 914 | # initial variables aren't shown by %who. After the sync, we add the |
|
915 | 915 | # rest of what we *do* want the user to see with %who even on a new |
|
916 | 916 | # session (probably nothing, so theye really only see their own stuff) |
|
917 | 917 | |
|
918 | 918 | # The user dict must *always* have a __builtin__ reference to the |
|
919 | 919 | # Python standard __builtin__ namespace, which must be imported. |
|
920 | 920 | # This is so that certain operations in prompt evaluation can be |
|
921 | 921 | # reliably executed with builtins. Note that we can NOT use |
|
922 | 922 | # __builtins__ (note the 's'), because that can either be a dict or a |
|
923 | 923 | # module, and can even mutate at runtime, depending on the context |
|
924 | 924 | # (Python makes no guarantees on it). In contrast, __builtin__ is |
|
925 | 925 | # always a module object, though it must be explicitly imported. |
|
926 | 926 | |
|
927 | 927 | # For more details: |
|
928 | 928 | # http://mail.python.org/pipermail/python-dev/2001-April/014068.html |
|
929 | 929 | ns = dict(__builtin__ = __builtin__) |
|
930 | 930 | |
|
931 | 931 | # Put 'help' in the user namespace |
|
932 | 932 | try: |
|
933 | 933 | from site import _Helper |
|
934 | 934 | ns['help'] = _Helper() |
|
935 | 935 | except ImportError: |
|
936 | 936 | warn('help() not available - check site.py') |
|
937 | 937 | |
|
938 | 938 | # make global variables for user access to the histories |
|
939 | 939 | ns['_ih'] = self.input_hist |
|
940 | 940 | ns['_oh'] = self.output_hist |
|
941 | 941 | ns['_dh'] = self.dir_hist |
|
942 | 942 | |
|
943 | 943 | ns['_sh'] = shadowns |
|
944 | 944 | |
|
945 | 945 | # user aliases to input and output histories. These shouldn't show up |
|
946 | 946 | # in %who, as they can have very large reprs. |
|
947 | 947 | ns['In'] = self.input_hist |
|
948 | 948 | ns['Out'] = self.output_hist |
|
949 | 949 | |
|
950 | 950 | # Store myself as the public api!!! |
|
951 | 951 | ns['get_ipython'] = self.get_ipython |
|
952 | 952 | |
|
953 | 953 | # Sync what we've added so far to user_ns_hidden so these aren't seen |
|
954 | 954 | # by %who |
|
955 | 955 | self.user_ns_hidden.update(ns) |
|
956 | 956 | |
|
957 | 957 | # Anything put into ns now would show up in %who. Think twice before |
|
958 | 958 | # putting anything here, as we really want %who to show the user their |
|
959 | 959 | # stuff, not our variables. |
|
960 | 960 | |
|
961 | 961 | # Finally, update the real user's namespace |
|
962 | 962 | self.user_ns.update(ns) |
|
963 | 963 | |
|
964 | 964 | |
|
965 | 965 | def reset(self): |
|
966 | 966 | """Clear all internal namespaces. |
|
967 | 967 | |
|
968 | 968 | Note that this is much more aggressive than %reset, since it clears |
|
969 | 969 | fully all namespaces, as well as all input/output lists. |
|
970 | 970 | """ |
|
971 | 971 | for ns in self.ns_refs_table: |
|
972 | 972 | ns.clear() |
|
973 | 973 | |
|
974 | 974 | self.alias_manager.clear_aliases() |
|
975 | 975 | |
|
976 | 976 | # Clear input and output histories |
|
977 | 977 | self.input_hist[:] = [] |
|
978 | 978 | self.input_hist_raw[:] = [] |
|
979 | 979 | self.output_hist.clear() |
|
980 | 980 | |
|
981 | 981 | # Restore the user namespaces to minimal usability |
|
982 | 982 | self.init_user_ns() |
|
983 | 983 | |
|
984 | 984 | # Restore the default and user aliases |
|
985 | 985 | self.alias_manager.init_aliases() |
|
986 | 986 | |
|
987 | 987 | def reset_selective(self, regex=None): |
|
988 | 988 | """Clear selective variables from internal namespaces based on a |
|
989 | 989 | specified regular expression. |
|
990 | 990 | |
|
991 | 991 | Parameters |
|
992 | 992 | ---------- |
|
993 | 993 | regex : string or compiled pattern, optional |
|
994 | 994 | A regular expression pattern that will be used in searching |
|
995 | 995 | variable names in the users namespaces. |
|
996 | 996 | """ |
|
997 | 997 | if regex is not None: |
|
998 | 998 | try: |
|
999 | 999 | m = re.compile(regex) |
|
1000 | 1000 | except TypeError: |
|
1001 | 1001 | raise TypeError('regex must be a string or compiled pattern') |
|
1002 | 1002 | # Search for keys in each namespace that match the given regex |
|
1003 | 1003 | # If a match is found, delete the key/value pair. |
|
1004 | 1004 | for ns in self.ns_refs_table: |
|
1005 | 1005 | for var in ns: |
|
1006 | 1006 | if m.search(var): |
|
1007 | 1007 | del ns[var] |
|
1008 | 1008 | |
|
1009 | 1009 | def push(self, variables, interactive=True): |
|
1010 | 1010 | """Inject a group of variables into the IPython user namespace. |
|
1011 | 1011 | |
|
1012 | 1012 | Parameters |
|
1013 | 1013 | ---------- |
|
1014 | 1014 | variables : dict, str or list/tuple of str |
|
1015 | 1015 | The variables to inject into the user's namespace. If a dict, a |
|
1016 | 1016 | simple update is done. If a str, the string is assumed to have |
|
1017 | 1017 | variable names separated by spaces. A list/tuple of str can also |
|
1018 | 1018 | be used to give the variable names. If just the variable names are |
|
1019 | 1019 | give (list/tuple/str) then the variable values looked up in the |
|
1020 | 1020 | callers frame. |
|
1021 | 1021 | interactive : bool |
|
1022 | 1022 | If True (default), the variables will be listed with the ``who`` |
|
1023 | 1023 | magic. |
|
1024 | 1024 | """ |
|
1025 | 1025 | vdict = None |
|
1026 | 1026 | |
|
1027 | 1027 | # We need a dict of name/value pairs to do namespace updates. |
|
1028 | 1028 | if isinstance(variables, dict): |
|
1029 | 1029 | vdict = variables |
|
1030 | 1030 | elif isinstance(variables, (basestring, list, tuple)): |
|
1031 | 1031 | if isinstance(variables, basestring): |
|
1032 | 1032 | vlist = variables.split() |
|
1033 | 1033 | else: |
|
1034 | 1034 | vlist = variables |
|
1035 | 1035 | vdict = {} |
|
1036 | 1036 | cf = sys._getframe(1) |
|
1037 | 1037 | for name in vlist: |
|
1038 | 1038 | try: |
|
1039 | 1039 | vdict[name] = eval(name, cf.f_globals, cf.f_locals) |
|
1040 | 1040 | except: |
|
1041 | 1041 | print ('Could not get variable %s from %s' % |
|
1042 | 1042 | (name,cf.f_code.co_name)) |
|
1043 | 1043 | else: |
|
1044 | 1044 | raise ValueError('variables must be a dict/str/list/tuple') |
|
1045 | 1045 | |
|
1046 | 1046 | # Propagate variables to user namespace |
|
1047 | 1047 | self.user_ns.update(vdict) |
|
1048 | 1048 | |
|
1049 | 1049 | # And configure interactive visibility |
|
1050 | 1050 | config_ns = self.user_ns_hidden |
|
1051 | 1051 | if interactive: |
|
1052 | 1052 | for name, val in vdict.iteritems(): |
|
1053 | 1053 | config_ns.pop(name, None) |
|
1054 | 1054 | else: |
|
1055 | 1055 | for name,val in vdict.iteritems(): |
|
1056 | 1056 | config_ns[name] = val |
|
1057 | 1057 | |
|
1058 | 1058 | #------------------------------------------------------------------------- |
|
1059 | 1059 | # Things related to object introspection |
|
1060 | 1060 | #------------------------------------------------------------------------- |
|
1061 | 1061 | |
|
1062 | 1062 | def _ofind(self, oname, namespaces=None): |
|
1063 | 1063 | """Find an object in the available namespaces. |
|
1064 | 1064 | |
|
1065 | 1065 | self._ofind(oname) -> dict with keys: found,obj,ospace,ismagic |
|
1066 | 1066 | |
|
1067 | 1067 | Has special code to detect magic functions. |
|
1068 | 1068 | """ |
|
1069 | 1069 | #oname = oname.strip() |
|
1070 | 1070 | #print '1- oname: <%r>' % oname # dbg |
|
1071 | 1071 | try: |
|
1072 | 1072 | oname = oname.strip().encode('ascii') |
|
1073 | 1073 | #print '2- oname: <%r>' % oname # dbg |
|
1074 | 1074 | except UnicodeEncodeError: |
|
1075 | 1075 | print 'Python identifiers can only contain ascii characters.' |
|
1076 | 1076 | return dict(found=False) |
|
1077 | 1077 | |
|
1078 | 1078 | alias_ns = None |
|
1079 | 1079 | if namespaces is None: |
|
1080 | 1080 | # Namespaces to search in: |
|
1081 | 1081 | # Put them in a list. The order is important so that we |
|
1082 | 1082 | # find things in the same order that Python finds them. |
|
1083 | 1083 | namespaces = [ ('Interactive', self.user_ns), |
|
1084 | 1084 | ('IPython internal', self.internal_ns), |
|
1085 | 1085 | ('Python builtin', __builtin__.__dict__), |
|
1086 | 1086 | ('Alias', self.alias_manager.alias_table), |
|
1087 | 1087 | ] |
|
1088 | 1088 | alias_ns = self.alias_manager.alias_table |
|
1089 | 1089 | |
|
1090 | 1090 | # initialize results to 'null' |
|
1091 | 1091 | found = False; obj = None; ospace = None; ds = None; |
|
1092 | 1092 | ismagic = False; isalias = False; parent = None |
|
1093 | 1093 | |
|
1094 | 1094 | # We need to special-case 'print', which as of python2.6 registers as a |
|
1095 | 1095 | # function but should only be treated as one if print_function was |
|
1096 | 1096 | # loaded with a future import. In this case, just bail. |
|
1097 | 1097 | if (oname == 'print' and not (self.compile.compiler.flags & |
|
1098 | 1098 | __future__.CO_FUTURE_PRINT_FUNCTION)): |
|
1099 | 1099 | return {'found':found, 'obj':obj, 'namespace':ospace, |
|
1100 | 1100 | 'ismagic':ismagic, 'isalias':isalias, 'parent':parent} |
|
1101 | 1101 | |
|
1102 | 1102 | # Look for the given name by splitting it in parts. If the head is |
|
1103 | 1103 | # found, then we look for all the remaining parts as members, and only |
|
1104 | 1104 | # declare success if we can find them all. |
|
1105 | 1105 | oname_parts = oname.split('.') |
|
1106 | 1106 | oname_head, oname_rest = oname_parts[0],oname_parts[1:] |
|
1107 | 1107 | for nsname,ns in namespaces: |
|
1108 | 1108 | try: |
|
1109 | 1109 | obj = ns[oname_head] |
|
1110 | 1110 | except KeyError: |
|
1111 | 1111 | continue |
|
1112 | 1112 | else: |
|
1113 | 1113 | #print 'oname_rest:', oname_rest # dbg |
|
1114 | 1114 | for part in oname_rest: |
|
1115 | 1115 | try: |
|
1116 | 1116 | parent = obj |
|
1117 | 1117 | obj = getattr(obj,part) |
|
1118 | 1118 | except: |
|
1119 | 1119 | # Blanket except b/c some badly implemented objects |
|
1120 | 1120 | # allow __getattr__ to raise exceptions other than |
|
1121 | 1121 | # AttributeError, which then crashes IPython. |
|
1122 | 1122 | break |
|
1123 | 1123 | else: |
|
1124 | 1124 | # If we finish the for loop (no break), we got all members |
|
1125 | 1125 | found = True |
|
1126 | 1126 | ospace = nsname |
|
1127 | 1127 | if ns == alias_ns: |
|
1128 | 1128 | isalias = True |
|
1129 | 1129 | break # namespace loop |
|
1130 | 1130 | |
|
1131 | 1131 | # Try to see if it's magic |
|
1132 | 1132 | if not found: |
|
1133 | 1133 | if oname.startswith(ESC_MAGIC): |
|
1134 | 1134 | oname = oname[1:] |
|
1135 | 1135 | obj = getattr(self,'magic_'+oname,None) |
|
1136 | 1136 | if obj is not None: |
|
1137 | 1137 | found = True |
|
1138 | 1138 | ospace = 'IPython internal' |
|
1139 | 1139 | ismagic = True |
|
1140 | 1140 | |
|
1141 | 1141 | # Last try: special-case some literals like '', [], {}, etc: |
|
1142 | 1142 | if not found and oname_head in ["''",'""','[]','{}','()']: |
|
1143 | 1143 | obj = eval(oname_head) |
|
1144 | 1144 | found = True |
|
1145 | 1145 | ospace = 'Interactive' |
|
1146 | 1146 | |
|
1147 | 1147 | return {'found':found, 'obj':obj, 'namespace':ospace, |
|
1148 | 1148 | 'ismagic':ismagic, 'isalias':isalias, 'parent':parent} |
|
1149 | 1149 | |
|
1150 | 1150 | def _ofind_property(self, oname, info): |
|
1151 | 1151 | """Second part of object finding, to look for property details.""" |
|
1152 | 1152 | if info.found: |
|
1153 | 1153 | # Get the docstring of the class property if it exists. |
|
1154 | 1154 | path = oname.split('.') |
|
1155 | 1155 | root = '.'.join(path[:-1]) |
|
1156 | 1156 | if info.parent is not None: |
|
1157 | 1157 | try: |
|
1158 | 1158 | target = getattr(info.parent, '__class__') |
|
1159 | 1159 | # The object belongs to a class instance. |
|
1160 | 1160 | try: |
|
1161 | 1161 | target = getattr(target, path[-1]) |
|
1162 | 1162 | # The class defines the object. |
|
1163 | 1163 | if isinstance(target, property): |
|
1164 | 1164 | oname = root + '.__class__.' + path[-1] |
|
1165 | 1165 | info = Struct(self._ofind(oname)) |
|
1166 | 1166 | except AttributeError: pass |
|
1167 | 1167 | except AttributeError: pass |
|
1168 | 1168 | |
|
1169 | 1169 | # We return either the new info or the unmodified input if the object |
|
1170 | 1170 | # hadn't been found |
|
1171 | 1171 | return info |
|
1172 | 1172 | |
|
1173 | 1173 | def _object_find(self, oname, namespaces=None): |
|
1174 | 1174 | """Find an object and return a struct with info about it.""" |
|
1175 | 1175 | inf = Struct(self._ofind(oname, namespaces)) |
|
1176 | 1176 | return Struct(self._ofind_property(oname, inf)) |
|
1177 | 1177 | |
|
1178 | 1178 | def _inspect(self, meth, oname, namespaces=None, **kw): |
|
1179 | 1179 | """Generic interface to the inspector system. |
|
1180 | 1180 | |
|
1181 | 1181 | This function is meant to be called by pdef, pdoc & friends.""" |
|
1182 | 1182 | info = self._object_find(oname) |
|
1183 | 1183 | if info.found: |
|
1184 | 1184 | pmethod = getattr(self.inspector, meth) |
|
1185 | 1185 | formatter = format_screen if info.ismagic else None |
|
1186 | 1186 | if meth == 'pdoc': |
|
1187 | 1187 | pmethod(info.obj, oname, formatter) |
|
1188 | 1188 | elif meth == 'pinfo': |
|
1189 | 1189 | pmethod(info.obj, oname, formatter, info, **kw) |
|
1190 | 1190 | else: |
|
1191 | 1191 | pmethod(info.obj, oname) |
|
1192 | 1192 | else: |
|
1193 | 1193 | print 'Object `%s` not found.' % oname |
|
1194 | 1194 | return 'not found' # so callers can take other action |
|
1195 | 1195 | |
|
1196 | 1196 | def object_inspect(self, oname): |
|
1197 | 1197 | info = self._object_find(oname) |
|
1198 | 1198 | if info.found: |
|
1199 | return self.inspector.info(info.obj, info=info) | |
|
1199 | return self.inspector.info(info.obj, oname, info=info) | |
|
1200 | 1200 | else: |
|
1201 |
return oinspect.mk_object_info({' |
|
|
1201 | return oinspect.mk_object_info({'name' : oname, | |
|
1202 | 'found' : False}) | |
|
1202 | 1203 | |
|
1203 | 1204 | #------------------------------------------------------------------------- |
|
1204 | 1205 | # Things related to history management |
|
1205 | 1206 | #------------------------------------------------------------------------- |
|
1206 | 1207 | |
|
1207 | 1208 | def init_history(self): |
|
1208 | 1209 | # List of input with multi-line handling. |
|
1209 | 1210 | self.input_hist = InputList() |
|
1210 | 1211 | # This one will hold the 'raw' input history, without any |
|
1211 | 1212 | # pre-processing. This will allow users to retrieve the input just as |
|
1212 | 1213 | # it was exactly typed in by the user, with %hist -r. |
|
1213 | 1214 | self.input_hist_raw = InputList() |
|
1214 | 1215 | |
|
1215 | 1216 | # list of visited directories |
|
1216 | 1217 | try: |
|
1217 | 1218 | self.dir_hist = [os.getcwd()] |
|
1218 | 1219 | except OSError: |
|
1219 | 1220 | self.dir_hist = [] |
|
1220 | 1221 | |
|
1221 | 1222 | # dict of output history |
|
1222 | 1223 | self.output_hist = {} |
|
1223 | 1224 | |
|
1224 | 1225 | # Now the history file |
|
1225 | 1226 | if self.profile: |
|
1226 | 1227 | histfname = 'history-%s' % self.profile |
|
1227 | 1228 | else: |
|
1228 | 1229 | histfname = 'history' |
|
1229 | 1230 | self.histfile = os.path.join(self.ipython_dir, histfname) |
|
1230 | 1231 | |
|
1231 | 1232 | # Fill the history zero entry, user counter starts at 1 |
|
1232 | 1233 | self.input_hist.append('\n') |
|
1233 | 1234 | self.input_hist_raw.append('\n') |
|
1234 | 1235 | |
|
1235 | 1236 | def init_shadow_hist(self): |
|
1236 | 1237 | try: |
|
1237 | 1238 | self.db = pickleshare.PickleShareDB(self.ipython_dir + "/db") |
|
1238 | 1239 | except exceptions.UnicodeDecodeError: |
|
1239 | 1240 | print "Your ipython_dir can't be decoded to unicode!" |
|
1240 | 1241 | print "Please set HOME environment variable to something that" |
|
1241 | 1242 | print r"only has ASCII characters, e.g. c:\home" |
|
1242 | 1243 | print "Now it is", self.ipython_dir |
|
1243 | 1244 | sys.exit() |
|
1244 | 1245 | self.shadowhist = ipcorehist.ShadowHist(self.db) |
|
1245 | 1246 | |
|
1246 | 1247 | def savehist(self): |
|
1247 | 1248 | """Save input history to a file (via readline library).""" |
|
1248 | 1249 | |
|
1249 | 1250 | try: |
|
1250 | 1251 | self.readline.write_history_file(self.histfile) |
|
1251 | 1252 | except: |
|
1252 | 1253 | print 'Unable to save IPython command history to file: ' + \ |
|
1253 | 1254 | `self.histfile` |
|
1254 | 1255 | |
|
1255 | 1256 | def reloadhist(self): |
|
1256 | 1257 | """Reload the input history from disk file.""" |
|
1257 | 1258 | |
|
1258 | 1259 | try: |
|
1259 | 1260 | self.readline.clear_history() |
|
1260 | 1261 | self.readline.read_history_file(self.shell.histfile) |
|
1261 | 1262 | except AttributeError: |
|
1262 | 1263 | pass |
|
1263 | 1264 | |
|
1264 | 1265 | def history_saving_wrapper(self, func): |
|
1265 | 1266 | """ Wrap func for readline history saving |
|
1266 | 1267 | |
|
1267 | 1268 | Convert func into callable that saves & restores |
|
1268 | 1269 | history around the call """ |
|
1269 | 1270 | |
|
1270 | 1271 | if self.has_readline: |
|
1271 | 1272 | from IPython.utils import rlineimpl as readline |
|
1272 | 1273 | else: |
|
1273 | 1274 | return func |
|
1274 | 1275 | |
|
1275 | 1276 | def wrapper(): |
|
1276 | 1277 | self.savehist() |
|
1277 | 1278 | try: |
|
1278 | 1279 | func() |
|
1279 | 1280 | finally: |
|
1280 | 1281 | readline.read_history_file(self.histfile) |
|
1281 | 1282 | return wrapper |
|
1282 | 1283 | |
|
1283 | 1284 | def get_history(self, index=None, raw=False, output=True): |
|
1284 | 1285 | """Get the history list. |
|
1285 | 1286 | |
|
1286 | 1287 | Get the input and output history. |
|
1287 | 1288 | |
|
1288 | 1289 | Parameters |
|
1289 | 1290 | ---------- |
|
1290 | 1291 | index : n or (n1, n2) or None |
|
1291 | 1292 | If n, then the last entries. If a tuple, then all in |
|
1292 | 1293 | range(n1, n2). If None, then all entries. Raises IndexError if |
|
1293 | 1294 | the format of index is incorrect. |
|
1294 | 1295 | raw : bool |
|
1295 | 1296 | If True, return the raw input. |
|
1296 | 1297 | output : bool |
|
1297 | 1298 | If True, then return the output as well. |
|
1298 | 1299 | |
|
1299 | 1300 | Returns |
|
1300 | 1301 | ------- |
|
1301 | 1302 | If output is True, then return a dict of tuples, keyed by the prompt |
|
1302 | 1303 | numbers and with values of (input, output). If output is False, then |
|
1303 | 1304 | a dict, keyed by the prompt number with the values of input. Raises |
|
1304 | 1305 | IndexError if no history is found. |
|
1305 | 1306 | """ |
|
1306 | 1307 | if raw: |
|
1307 | 1308 | input_hist = self.input_hist_raw |
|
1308 | 1309 | else: |
|
1309 | 1310 | input_hist = self.input_hist |
|
1310 | 1311 | if output: |
|
1311 | 1312 | output_hist = self.user_ns['Out'] |
|
1312 | 1313 | n = len(input_hist) |
|
1313 | 1314 | if index is None: |
|
1314 | 1315 | start=0; stop=n |
|
1315 | 1316 | elif isinstance(index, int): |
|
1316 | 1317 | start=n-index; stop=n |
|
1317 | 1318 | elif isinstance(index, tuple) and len(index) == 2: |
|
1318 | 1319 | start=index[0]; stop=index[1] |
|
1319 | 1320 | else: |
|
1320 | 1321 | raise IndexError('Not a valid index for the input history: %r' |
|
1321 | 1322 | % index) |
|
1322 | 1323 | hist = {} |
|
1323 | 1324 | for i in range(start, stop): |
|
1324 | 1325 | if output: |
|
1325 | 1326 | hist[i] = (input_hist[i], output_hist.get(i)) |
|
1326 | 1327 | else: |
|
1327 | 1328 | hist[i] = input_hist[i] |
|
1328 | 1329 | if len(hist)==0: |
|
1329 | 1330 | raise IndexError('No history for range of indices: %r' % index) |
|
1330 | 1331 | return hist |
|
1331 | 1332 | |
|
1332 | 1333 | #------------------------------------------------------------------------- |
|
1333 | 1334 | # Things related to exception handling and tracebacks (not debugging) |
|
1334 | 1335 | #------------------------------------------------------------------------- |
|
1335 | 1336 | |
|
1336 | 1337 | def init_traceback_handlers(self, custom_exceptions): |
|
1337 | 1338 | # Syntax error handler. |
|
1338 | 1339 | self.SyntaxTB = ultratb.SyntaxTB(color_scheme='NoColor') |
|
1339 | 1340 | |
|
1340 | 1341 | # The interactive one is initialized with an offset, meaning we always |
|
1341 | 1342 | # want to remove the topmost item in the traceback, which is our own |
|
1342 | 1343 | # internal code. Valid modes: ['Plain','Context','Verbose'] |
|
1343 | 1344 | self.InteractiveTB = ultratb.AutoFormattedTB(mode = 'Plain', |
|
1344 | 1345 | color_scheme='NoColor', |
|
1345 | 1346 | tb_offset = 1) |
|
1346 | 1347 | |
|
1347 | 1348 | # The instance will store a pointer to the system-wide exception hook, |
|
1348 | 1349 | # so that runtime code (such as magics) can access it. This is because |
|
1349 | 1350 | # during the read-eval loop, it may get temporarily overwritten. |
|
1350 | 1351 | self.sys_excepthook = sys.excepthook |
|
1351 | 1352 | |
|
1352 | 1353 | # and add any custom exception handlers the user may have specified |
|
1353 | 1354 | self.set_custom_exc(*custom_exceptions) |
|
1354 | 1355 | |
|
1355 | 1356 | # Set the exception mode |
|
1356 | 1357 | self.InteractiveTB.set_mode(mode=self.xmode) |
|
1357 | 1358 | |
|
1358 | 1359 | def set_custom_exc(self, exc_tuple, handler): |
|
1359 | 1360 | """set_custom_exc(exc_tuple,handler) |
|
1360 | 1361 | |
|
1361 | 1362 | Set a custom exception handler, which will be called if any of the |
|
1362 | 1363 | exceptions in exc_tuple occur in the mainloop (specifically, in the |
|
1363 | 1364 | runcode() method. |
|
1364 | 1365 | |
|
1365 | 1366 | Inputs: |
|
1366 | 1367 | |
|
1367 | 1368 | - exc_tuple: a *tuple* of valid exceptions to call the defined |
|
1368 | 1369 | handler for. It is very important that you use a tuple, and NOT A |
|
1369 | 1370 | LIST here, because of the way Python's except statement works. If |
|
1370 | 1371 | you only want to trap a single exception, use a singleton tuple: |
|
1371 | 1372 | |
|
1372 | 1373 | exc_tuple == (MyCustomException,) |
|
1373 | 1374 | |
|
1374 | 1375 | - handler: this must be defined as a function with the following |
|
1375 | 1376 | basic interface:: |
|
1376 | 1377 | |
|
1377 | 1378 | def my_handler(self, etype, value, tb, tb_offset=None) |
|
1378 | 1379 | ... |
|
1379 | 1380 | # The return value must be |
|
1380 | 1381 | return structured_traceback |
|
1381 | 1382 | |
|
1382 | 1383 | This will be made into an instance method (via new.instancemethod) |
|
1383 | 1384 | of IPython itself, and it will be called if any of the exceptions |
|
1384 | 1385 | listed in the exc_tuple are caught. If the handler is None, an |
|
1385 | 1386 | internal basic one is used, which just prints basic info. |
|
1386 | 1387 | |
|
1387 | 1388 | WARNING: by putting in your own exception handler into IPython's main |
|
1388 | 1389 | execution loop, you run a very good chance of nasty crashes. This |
|
1389 | 1390 | facility should only be used if you really know what you are doing.""" |
|
1390 | 1391 | |
|
1391 | 1392 | assert type(exc_tuple)==type(()) , \ |
|
1392 | 1393 | "The custom exceptions must be given AS A TUPLE." |
|
1393 | 1394 | |
|
1394 | 1395 | def dummy_handler(self,etype,value,tb): |
|
1395 | 1396 | print '*** Simple custom exception handler ***' |
|
1396 | 1397 | print 'Exception type :',etype |
|
1397 | 1398 | print 'Exception value:',value |
|
1398 | 1399 | print 'Traceback :',tb |
|
1399 | 1400 | print 'Source code :','\n'.join(self.buffer) |
|
1400 | 1401 | |
|
1401 | 1402 | if handler is None: handler = dummy_handler |
|
1402 | 1403 | |
|
1403 | 1404 | self.CustomTB = new.instancemethod(handler,self,self.__class__) |
|
1404 | 1405 | self.custom_exceptions = exc_tuple |
|
1405 | 1406 | |
|
1406 | 1407 | def excepthook(self, etype, value, tb): |
|
1407 | 1408 | """One more defense for GUI apps that call sys.excepthook. |
|
1408 | 1409 | |
|
1409 | 1410 | GUI frameworks like wxPython trap exceptions and call |
|
1410 | 1411 | sys.excepthook themselves. I guess this is a feature that |
|
1411 | 1412 | enables them to keep running after exceptions that would |
|
1412 | 1413 | otherwise kill their mainloop. This is a bother for IPython |
|
1413 | 1414 | which excepts to catch all of the program exceptions with a try: |
|
1414 | 1415 | except: statement. |
|
1415 | 1416 | |
|
1416 | 1417 | Normally, IPython sets sys.excepthook to a CrashHandler instance, so if |
|
1417 | 1418 | any app directly invokes sys.excepthook, it will look to the user like |
|
1418 | 1419 | IPython crashed. In order to work around this, we can disable the |
|
1419 | 1420 | CrashHandler and replace it with this excepthook instead, which prints a |
|
1420 | 1421 | regular traceback using our InteractiveTB. In this fashion, apps which |
|
1421 | 1422 | call sys.excepthook will generate a regular-looking exception from |
|
1422 | 1423 | IPython, and the CrashHandler will only be triggered by real IPython |
|
1423 | 1424 | crashes. |
|
1424 | 1425 | |
|
1425 | 1426 | This hook should be used sparingly, only in places which are not likely |
|
1426 | 1427 | to be true IPython errors. |
|
1427 | 1428 | """ |
|
1428 | 1429 | self.showtraceback((etype,value,tb),tb_offset=0) |
|
1429 | 1430 | |
|
1430 | 1431 | def showtraceback(self,exc_tuple = None,filename=None,tb_offset=None, |
|
1431 | 1432 | exception_only=False): |
|
1432 | 1433 | """Display the exception that just occurred. |
|
1433 | 1434 | |
|
1434 | 1435 | If nothing is known about the exception, this is the method which |
|
1435 | 1436 | should be used throughout the code for presenting user tracebacks, |
|
1436 | 1437 | rather than directly invoking the InteractiveTB object. |
|
1437 | 1438 | |
|
1438 | 1439 | A specific showsyntaxerror() also exists, but this method can take |
|
1439 | 1440 | care of calling it if needed, so unless you are explicitly catching a |
|
1440 | 1441 | SyntaxError exception, don't try to analyze the stack manually and |
|
1441 | 1442 | simply call this method.""" |
|
1442 | 1443 | |
|
1443 | 1444 | try: |
|
1444 | 1445 | if exc_tuple is None: |
|
1445 | 1446 | etype, value, tb = sys.exc_info() |
|
1446 | 1447 | else: |
|
1447 | 1448 | etype, value, tb = exc_tuple |
|
1448 | 1449 | |
|
1449 | 1450 | if etype is None: |
|
1450 | 1451 | if hasattr(sys, 'last_type'): |
|
1451 | 1452 | etype, value, tb = sys.last_type, sys.last_value, \ |
|
1452 | 1453 | sys.last_traceback |
|
1453 | 1454 | else: |
|
1454 | 1455 | self.write_err('No traceback available to show.\n') |
|
1455 | 1456 | return |
|
1456 | 1457 | |
|
1457 | 1458 | if etype is SyntaxError: |
|
1458 | 1459 | # Though this won't be called by syntax errors in the input |
|
1459 | 1460 | # line, there may be SyntaxError cases whith imported code. |
|
1460 | 1461 | self.showsyntaxerror(filename) |
|
1461 | 1462 | elif etype is UsageError: |
|
1462 | 1463 | print "UsageError:", value |
|
1463 | 1464 | else: |
|
1464 | 1465 | # WARNING: these variables are somewhat deprecated and not |
|
1465 | 1466 | # necessarily safe to use in a threaded environment, but tools |
|
1466 | 1467 | # like pdb depend on their existence, so let's set them. If we |
|
1467 | 1468 | # find problems in the field, we'll need to revisit their use. |
|
1468 | 1469 | sys.last_type = etype |
|
1469 | 1470 | sys.last_value = value |
|
1470 | 1471 | sys.last_traceback = tb |
|
1471 | 1472 | |
|
1472 | 1473 | if etype in self.custom_exceptions: |
|
1473 | 1474 | # FIXME: Old custom traceback objects may just return a |
|
1474 | 1475 | # string, in that case we just put it into a list |
|
1475 | 1476 | stb = self.CustomTB(etype, value, tb, tb_offset) |
|
1476 | 1477 | if isinstance(ctb, basestring): |
|
1477 | 1478 | stb = [stb] |
|
1478 | 1479 | else: |
|
1479 | 1480 | if exception_only: |
|
1480 | 1481 | stb = ['An exception has occurred, use %tb to see ' |
|
1481 | 1482 | 'the full traceback.\n'] |
|
1482 | 1483 | stb.extend(self.InteractiveTB.get_exception_only(etype, |
|
1483 | 1484 | value)) |
|
1484 | 1485 | else: |
|
1485 | 1486 | stb = self.InteractiveTB.structured_traceback(etype, |
|
1486 | 1487 | value, tb, tb_offset=tb_offset) |
|
1487 | 1488 | # FIXME: the pdb calling should be done by us, not by |
|
1488 | 1489 | # the code computing the traceback. |
|
1489 | 1490 | if self.InteractiveTB.call_pdb: |
|
1490 | 1491 | # pdb mucks up readline, fix it back |
|
1491 | 1492 | self.set_readline_completer() |
|
1492 | 1493 | |
|
1493 | 1494 | # Actually show the traceback |
|
1494 | 1495 | self._showtraceback(etype, value, stb) |
|
1495 | 1496 | |
|
1496 | 1497 | except KeyboardInterrupt: |
|
1497 | 1498 | self.write_err("\nKeyboardInterrupt\n") |
|
1498 | 1499 | |
|
1499 | 1500 | def _showtraceback(self, etype, evalue, stb): |
|
1500 | 1501 | """Actually show a traceback. |
|
1501 | 1502 | |
|
1502 | 1503 | Subclasses may override this method to put the traceback on a different |
|
1503 | 1504 | place, like a side channel. |
|
1504 | 1505 | """ |
|
1505 | 1506 | print >> io.Term.cout, self.InteractiveTB.stb2text(stb) |
|
1506 | 1507 | |
|
1507 | 1508 | def showsyntaxerror(self, filename=None): |
|
1508 | 1509 | """Display the syntax error that just occurred. |
|
1509 | 1510 | |
|
1510 | 1511 | This doesn't display a stack trace because there isn't one. |
|
1511 | 1512 | |
|
1512 | 1513 | If a filename is given, it is stuffed in the exception instead |
|
1513 | 1514 | of what was there before (because Python's parser always uses |
|
1514 | 1515 | "<string>" when reading from a string). |
|
1515 | 1516 | """ |
|
1516 | 1517 | etype, value, last_traceback = sys.exc_info() |
|
1517 | 1518 | |
|
1518 | 1519 | # See note about these variables in showtraceback() above |
|
1519 | 1520 | sys.last_type = etype |
|
1520 | 1521 | sys.last_value = value |
|
1521 | 1522 | sys.last_traceback = last_traceback |
|
1522 | 1523 | |
|
1523 | 1524 | if filename and etype is SyntaxError: |
|
1524 | 1525 | # Work hard to stuff the correct filename in the exception |
|
1525 | 1526 | try: |
|
1526 | 1527 | msg, (dummy_filename, lineno, offset, line) = value |
|
1527 | 1528 | except: |
|
1528 | 1529 | # Not the format we expect; leave it alone |
|
1529 | 1530 | pass |
|
1530 | 1531 | else: |
|
1531 | 1532 | # Stuff in the right filename |
|
1532 | 1533 | try: |
|
1533 | 1534 | # Assume SyntaxError is a class exception |
|
1534 | 1535 | value = SyntaxError(msg, (filename, lineno, offset, line)) |
|
1535 | 1536 | except: |
|
1536 | 1537 | # If that failed, assume SyntaxError is a string |
|
1537 | 1538 | value = msg, (filename, lineno, offset, line) |
|
1538 | 1539 | stb = self.SyntaxTB.structured_traceback(etype, value, []) |
|
1539 | 1540 | self._showtraceback(etype, value, stb) |
|
1540 | 1541 | |
|
1541 | 1542 | #------------------------------------------------------------------------- |
|
1542 | 1543 | # Things related to readline |
|
1543 | 1544 | #------------------------------------------------------------------------- |
|
1544 | 1545 | |
|
1545 | 1546 | def init_readline(self): |
|
1546 | 1547 | """Command history completion/saving/reloading.""" |
|
1547 | 1548 | |
|
1548 | 1549 | if self.readline_use: |
|
1549 | 1550 | import IPython.utils.rlineimpl as readline |
|
1550 | 1551 | |
|
1551 | 1552 | self.rl_next_input = None |
|
1552 | 1553 | self.rl_do_indent = False |
|
1553 | 1554 | |
|
1554 | 1555 | if not self.readline_use or not readline.have_readline: |
|
1555 | 1556 | self.has_readline = False |
|
1556 | 1557 | self.readline = None |
|
1557 | 1558 | # Set a number of methods that depend on readline to be no-op |
|
1558 | 1559 | self.savehist = no_op |
|
1559 | 1560 | self.reloadhist = no_op |
|
1560 | 1561 | self.set_readline_completer = no_op |
|
1561 | 1562 | self.set_custom_completer = no_op |
|
1562 | 1563 | self.set_completer_frame = no_op |
|
1563 | 1564 | warn('Readline services not available or not loaded.') |
|
1564 | 1565 | else: |
|
1565 | 1566 | self.has_readline = True |
|
1566 | 1567 | self.readline = readline |
|
1567 | 1568 | sys.modules['readline'] = readline |
|
1568 | 1569 | |
|
1569 | 1570 | # Platform-specific configuration |
|
1570 | 1571 | if os.name == 'nt': |
|
1571 | 1572 | # FIXME - check with Frederick to see if we can harmonize |
|
1572 | 1573 | # naming conventions with pyreadline to avoid this |
|
1573 | 1574 | # platform-dependent check |
|
1574 | 1575 | self.readline_startup_hook = readline.set_pre_input_hook |
|
1575 | 1576 | else: |
|
1576 | 1577 | self.readline_startup_hook = readline.set_startup_hook |
|
1577 | 1578 | |
|
1578 | 1579 | # Load user's initrc file (readline config) |
|
1579 | 1580 | # Or if libedit is used, load editrc. |
|
1580 | 1581 | inputrc_name = os.environ.get('INPUTRC') |
|
1581 | 1582 | if inputrc_name is None: |
|
1582 | 1583 | home_dir = get_home_dir() |
|
1583 | 1584 | if home_dir is not None: |
|
1584 | 1585 | inputrc_name = '.inputrc' |
|
1585 | 1586 | if readline.uses_libedit: |
|
1586 | 1587 | inputrc_name = '.editrc' |
|
1587 | 1588 | inputrc_name = os.path.join(home_dir, inputrc_name) |
|
1588 | 1589 | if os.path.isfile(inputrc_name): |
|
1589 | 1590 | try: |
|
1590 | 1591 | readline.read_init_file(inputrc_name) |
|
1591 | 1592 | except: |
|
1592 | 1593 | warn('Problems reading readline initialization file <%s>' |
|
1593 | 1594 | % inputrc_name) |
|
1594 | 1595 | |
|
1595 | 1596 | # Configure readline according to user's prefs |
|
1596 | 1597 | # This is only done if GNU readline is being used. If libedit |
|
1597 | 1598 | # is being used (as on Leopard) the readline config is |
|
1598 | 1599 | # not run as the syntax for libedit is different. |
|
1599 | 1600 | if not readline.uses_libedit: |
|
1600 | 1601 | for rlcommand in self.readline_parse_and_bind: |
|
1601 | 1602 | #print "loading rl:",rlcommand # dbg |
|
1602 | 1603 | readline.parse_and_bind(rlcommand) |
|
1603 | 1604 | |
|
1604 | 1605 | # Remove some chars from the delimiters list. If we encounter |
|
1605 | 1606 | # unicode chars, discard them. |
|
1606 | 1607 | delims = readline.get_completer_delims().encode("ascii", "ignore") |
|
1607 | 1608 | delims = delims.translate(string._idmap, |
|
1608 | 1609 | self.readline_remove_delims) |
|
1609 | 1610 | delims = delims.replace(ESC_MAGIC, '') |
|
1610 | 1611 | readline.set_completer_delims(delims) |
|
1611 | 1612 | # otherwise we end up with a monster history after a while: |
|
1612 | 1613 | readline.set_history_length(1000) |
|
1613 | 1614 | try: |
|
1614 | 1615 | #print '*** Reading readline history' # dbg |
|
1615 | 1616 | readline.read_history_file(self.histfile) |
|
1616 | 1617 | except IOError: |
|
1617 | 1618 | pass # It doesn't exist yet. |
|
1618 | 1619 | |
|
1619 | 1620 | # If we have readline, we want our history saved upon ipython |
|
1620 | 1621 | # exiting. |
|
1621 | 1622 | atexit.register(self.savehist) |
|
1622 | 1623 | |
|
1623 | 1624 | # Configure auto-indent for all platforms |
|
1624 | 1625 | self.set_autoindent(self.autoindent) |
|
1625 | 1626 | |
|
1626 | 1627 | def set_next_input(self, s): |
|
1627 | 1628 | """ Sets the 'default' input string for the next command line. |
|
1628 | 1629 | |
|
1629 | 1630 | Requires readline. |
|
1630 | 1631 | |
|
1631 | 1632 | Example: |
|
1632 | 1633 | |
|
1633 | 1634 | [D:\ipython]|1> _ip.set_next_input("Hello Word") |
|
1634 | 1635 | [D:\ipython]|2> Hello Word_ # cursor is here |
|
1635 | 1636 | """ |
|
1636 | 1637 | |
|
1637 | 1638 | self.rl_next_input = s |
|
1638 | 1639 | |
|
1639 | 1640 | # Maybe move this to the terminal subclass? |
|
1640 | 1641 | def pre_readline(self): |
|
1641 | 1642 | """readline hook to be used at the start of each line. |
|
1642 | 1643 | |
|
1643 | 1644 | Currently it handles auto-indent only.""" |
|
1644 | 1645 | |
|
1645 | 1646 | if self.rl_do_indent: |
|
1646 | 1647 | self.readline.insert_text(self._indent_current_str()) |
|
1647 | 1648 | if self.rl_next_input is not None: |
|
1648 | 1649 | self.readline.insert_text(self.rl_next_input) |
|
1649 | 1650 | self.rl_next_input = None |
|
1650 | 1651 | |
|
1651 | 1652 | def _indent_current_str(self): |
|
1652 | 1653 | """return the current level of indentation as a string""" |
|
1653 | 1654 | return self.indent_current_nsp * ' ' |
|
1654 | 1655 | |
|
1655 | 1656 | #------------------------------------------------------------------------- |
|
1656 | 1657 | # Things related to text completion |
|
1657 | 1658 | #------------------------------------------------------------------------- |
|
1658 | 1659 | |
|
1659 | 1660 | def init_completer(self): |
|
1660 | 1661 | """Initialize the completion machinery. |
|
1661 | 1662 | |
|
1662 | 1663 | This creates completion machinery that can be used by client code, |
|
1663 | 1664 | either interactively in-process (typically triggered by the readline |
|
1664 | 1665 | library), programatically (such as in test suites) or out-of-prcess |
|
1665 | 1666 | (typically over the network by remote frontends). |
|
1666 | 1667 | """ |
|
1667 | 1668 | from IPython.core.completer import IPCompleter |
|
1668 | 1669 | from IPython.core.completerlib import (module_completer, |
|
1669 | 1670 | magic_run_completer, cd_completer) |
|
1670 | 1671 | |
|
1671 | 1672 | self.Completer = IPCompleter(self, |
|
1672 | 1673 | self.user_ns, |
|
1673 | 1674 | self.user_global_ns, |
|
1674 | 1675 | self.readline_omit__names, |
|
1675 | 1676 | self.alias_manager.alias_table, |
|
1676 | 1677 | self.has_readline) |
|
1677 | 1678 | |
|
1678 | 1679 | # Add custom completers to the basic ones built into IPCompleter |
|
1679 | 1680 | sdisp = self.strdispatchers.get('complete_command', StrDispatch()) |
|
1680 | 1681 | self.strdispatchers['complete_command'] = sdisp |
|
1681 | 1682 | self.Completer.custom_completers = sdisp |
|
1682 | 1683 | |
|
1683 | 1684 | self.set_hook('complete_command', module_completer, str_key = 'import') |
|
1684 | 1685 | self.set_hook('complete_command', module_completer, str_key = 'from') |
|
1685 | 1686 | self.set_hook('complete_command', magic_run_completer, str_key = '%run') |
|
1686 | 1687 | self.set_hook('complete_command', cd_completer, str_key = '%cd') |
|
1687 | 1688 | |
|
1688 | 1689 | # Only configure readline if we truly are using readline. IPython can |
|
1689 | 1690 | # do tab-completion over the network, in GUIs, etc, where readline |
|
1690 | 1691 | # itself may be absent |
|
1691 | 1692 | if self.has_readline: |
|
1692 | 1693 | self.set_readline_completer() |
|
1693 | 1694 | |
|
1694 | 1695 | def complete(self, text, line=None, cursor_pos=None): |
|
1695 | 1696 | """Return the completed text and a list of completions. |
|
1696 | 1697 | |
|
1697 | 1698 | Parameters |
|
1698 | 1699 | ---------- |
|
1699 | 1700 | |
|
1700 | 1701 | text : string |
|
1701 | 1702 | A string of text to be completed on. It can be given as empty and |
|
1702 | 1703 | instead a line/position pair are given. In this case, the |
|
1703 | 1704 | completer itself will split the line like readline does. |
|
1704 | 1705 | |
|
1705 | 1706 | line : string, optional |
|
1706 | 1707 | The complete line that text is part of. |
|
1707 | 1708 | |
|
1708 | 1709 | cursor_pos : int, optional |
|
1709 | 1710 | The position of the cursor on the input line. |
|
1710 | 1711 | |
|
1711 | 1712 | Returns |
|
1712 | 1713 | ------- |
|
1713 | 1714 | text : string |
|
1714 | 1715 | The actual text that was completed. |
|
1715 | 1716 | |
|
1716 | 1717 | matches : list |
|
1717 | 1718 | A sorted list with all possible completions. |
|
1718 | 1719 | |
|
1719 | 1720 | The optional arguments allow the completion to take more context into |
|
1720 | 1721 | account, and are part of the low-level completion API. |
|
1721 | 1722 | |
|
1722 | 1723 | This is a wrapper around the completion mechanism, similar to what |
|
1723 | 1724 | readline does at the command line when the TAB key is hit. By |
|
1724 | 1725 | exposing it as a method, it can be used by other non-readline |
|
1725 | 1726 | environments (such as GUIs) for text completion. |
|
1726 | 1727 | |
|
1727 | 1728 | Simple usage example: |
|
1728 | 1729 | |
|
1729 | 1730 | In [1]: x = 'hello' |
|
1730 | 1731 | |
|
1731 | 1732 | In [2]: _ip.complete('x.l') |
|
1732 | 1733 | Out[2]: ('x.l', ['x.ljust', 'x.lower', 'x.lstrip']) |
|
1733 | 1734 | """ |
|
1734 | 1735 | |
|
1735 | 1736 | # Inject names into __builtin__ so we can complete on the added names. |
|
1736 | 1737 | with self.builtin_trap: |
|
1737 | 1738 | return self.Completer.complete(text, line, cursor_pos) |
|
1738 | 1739 | |
|
1739 | 1740 | def set_custom_completer(self, completer, pos=0): |
|
1740 | 1741 | """Adds a new custom completer function. |
|
1741 | 1742 | |
|
1742 | 1743 | The position argument (defaults to 0) is the index in the completers |
|
1743 | 1744 | list where you want the completer to be inserted.""" |
|
1744 | 1745 | |
|
1745 | 1746 | newcomp = new.instancemethod(completer,self.Completer, |
|
1746 | 1747 | self.Completer.__class__) |
|
1747 | 1748 | self.Completer.matchers.insert(pos,newcomp) |
|
1748 | 1749 | |
|
1749 | 1750 | def set_readline_completer(self): |
|
1750 | 1751 | """Reset readline's completer to be our own.""" |
|
1751 | 1752 | self.readline.set_completer(self.Completer.rlcomplete) |
|
1752 | 1753 | |
|
1753 | 1754 | def set_completer_frame(self, frame=None): |
|
1754 | 1755 | """Set the frame of the completer.""" |
|
1755 | 1756 | if frame: |
|
1756 | 1757 | self.Completer.namespace = frame.f_locals |
|
1757 | 1758 | self.Completer.global_namespace = frame.f_globals |
|
1758 | 1759 | else: |
|
1759 | 1760 | self.Completer.namespace = self.user_ns |
|
1760 | 1761 | self.Completer.global_namespace = self.user_global_ns |
|
1761 | 1762 | |
|
1762 | 1763 | #------------------------------------------------------------------------- |
|
1763 | 1764 | # Things related to magics |
|
1764 | 1765 | #------------------------------------------------------------------------- |
|
1765 | 1766 | |
|
1766 | 1767 | def init_magics(self): |
|
1767 | 1768 | # FIXME: Move the color initialization to the DisplayHook, which |
|
1768 | 1769 | # should be split into a prompt manager and displayhook. We probably |
|
1769 | 1770 | # even need a centralize colors management object. |
|
1770 | 1771 | self.magic_colors(self.colors) |
|
1771 | 1772 | # History was moved to a separate module |
|
1772 | 1773 | from . import history |
|
1773 | 1774 | history.init_ipython(self) |
|
1774 | 1775 | |
|
1775 | 1776 | def magic(self,arg_s): |
|
1776 | 1777 | """Call a magic function by name. |
|
1777 | 1778 | |
|
1778 | 1779 | Input: a string containing the name of the magic function to call and |
|
1779 | 1780 | any additional arguments to be passed to the magic. |
|
1780 | 1781 | |
|
1781 | 1782 | magic('name -opt foo bar') is equivalent to typing at the ipython |
|
1782 | 1783 | prompt: |
|
1783 | 1784 | |
|
1784 | 1785 | In[1]: %name -opt foo bar |
|
1785 | 1786 | |
|
1786 | 1787 | To call a magic without arguments, simply use magic('name'). |
|
1787 | 1788 | |
|
1788 | 1789 | This provides a proper Python function to call IPython's magics in any |
|
1789 | 1790 | valid Python code you can type at the interpreter, including loops and |
|
1790 | 1791 | compound statements. |
|
1791 | 1792 | """ |
|
1792 | 1793 | args = arg_s.split(' ',1) |
|
1793 | 1794 | magic_name = args[0] |
|
1794 | 1795 | magic_name = magic_name.lstrip(prefilter.ESC_MAGIC) |
|
1795 | 1796 | |
|
1796 | 1797 | try: |
|
1797 | 1798 | magic_args = args[1] |
|
1798 | 1799 | except IndexError: |
|
1799 | 1800 | magic_args = '' |
|
1800 | 1801 | fn = getattr(self,'magic_'+magic_name,None) |
|
1801 | 1802 | if fn is None: |
|
1802 | 1803 | error("Magic function `%s` not found." % magic_name) |
|
1803 | 1804 | else: |
|
1804 | 1805 | magic_args = self.var_expand(magic_args,1) |
|
1805 | 1806 | with nested(self.builtin_trap,): |
|
1806 | 1807 | result = fn(magic_args) |
|
1807 | 1808 | return result |
|
1808 | 1809 | |
|
1809 | 1810 | def define_magic(self, magicname, func): |
|
1810 | 1811 | """Expose own function as magic function for ipython |
|
1811 | 1812 | |
|
1812 | 1813 | def foo_impl(self,parameter_s=''): |
|
1813 | 1814 | 'My very own magic!. (Use docstrings, IPython reads them).' |
|
1814 | 1815 | print 'Magic function. Passed parameter is between < >:' |
|
1815 | 1816 | print '<%s>' % parameter_s |
|
1816 | 1817 | print 'The self object is:',self |
|
1817 | 1818 | |
|
1818 | 1819 | self.define_magic('foo',foo_impl) |
|
1819 | 1820 | """ |
|
1820 | 1821 | |
|
1821 | 1822 | import new |
|
1822 | 1823 | im = new.instancemethod(func,self, self.__class__) |
|
1823 | 1824 | old = getattr(self, "magic_" + magicname, None) |
|
1824 | 1825 | setattr(self, "magic_" + magicname, im) |
|
1825 | 1826 | return old |
|
1826 | 1827 | |
|
1827 | 1828 | #------------------------------------------------------------------------- |
|
1828 | 1829 | # Things related to macros |
|
1829 | 1830 | #------------------------------------------------------------------------- |
|
1830 | 1831 | |
|
1831 | 1832 | def define_macro(self, name, themacro): |
|
1832 | 1833 | """Define a new macro |
|
1833 | 1834 | |
|
1834 | 1835 | Parameters |
|
1835 | 1836 | ---------- |
|
1836 | 1837 | name : str |
|
1837 | 1838 | The name of the macro. |
|
1838 | 1839 | themacro : str or Macro |
|
1839 | 1840 | The action to do upon invoking the macro. If a string, a new |
|
1840 | 1841 | Macro object is created by passing the string to it. |
|
1841 | 1842 | """ |
|
1842 | 1843 | |
|
1843 | 1844 | from IPython.core import macro |
|
1844 | 1845 | |
|
1845 | 1846 | if isinstance(themacro, basestring): |
|
1846 | 1847 | themacro = macro.Macro(themacro) |
|
1847 | 1848 | if not isinstance(themacro, macro.Macro): |
|
1848 | 1849 | raise ValueError('A macro must be a string or a Macro instance.') |
|
1849 | 1850 | self.user_ns[name] = themacro |
|
1850 | 1851 | |
|
1851 | 1852 | #------------------------------------------------------------------------- |
|
1852 | 1853 | # Things related to the running of system commands |
|
1853 | 1854 | #------------------------------------------------------------------------- |
|
1854 | 1855 | |
|
1855 | 1856 | def system(self, cmd): |
|
1856 | 1857 | """Call the given cmd in a subprocess. |
|
1857 | 1858 | |
|
1858 | 1859 | Parameters |
|
1859 | 1860 | ---------- |
|
1860 | 1861 | cmd : str |
|
1861 | 1862 | Command to execute (can not end in '&', as bacground processes are |
|
1862 | 1863 | not supported. |
|
1863 | 1864 | """ |
|
1864 | 1865 | # We do not support backgrounding processes because we either use |
|
1865 | 1866 | # pexpect or pipes to read from. Users can always just call |
|
1866 | 1867 | # os.system() if they really want a background process. |
|
1867 | 1868 | if cmd.endswith('&'): |
|
1868 | 1869 | raise OSError("Background processes not supported.") |
|
1869 | 1870 | |
|
1870 | 1871 | return system(self.var_expand(cmd, depth=2)) |
|
1871 | 1872 | |
|
1872 | 1873 | def getoutput(self, cmd, split=True): |
|
1873 | 1874 | """Get output (possibly including stderr) from a subprocess. |
|
1874 | 1875 | |
|
1875 | 1876 | Parameters |
|
1876 | 1877 | ---------- |
|
1877 | 1878 | cmd : str |
|
1878 | 1879 | Command to execute (can not end in '&', as background processes are |
|
1879 | 1880 | not supported. |
|
1880 | 1881 | split : bool, optional |
|
1881 | 1882 | |
|
1882 | 1883 | If True, split the output into an IPython SList. Otherwise, an |
|
1883 | 1884 | IPython LSString is returned. These are objects similar to normal |
|
1884 | 1885 | lists and strings, with a few convenience attributes for easier |
|
1885 | 1886 | manipulation of line-based output. You can use '?' on them for |
|
1886 | 1887 | details. |
|
1887 | 1888 | """ |
|
1888 | 1889 | if cmd.endswith('&'): |
|
1889 | 1890 | raise OSError("Background processes not supported.") |
|
1890 | 1891 | out = getoutput(self.var_expand(cmd, depth=2)) |
|
1891 | 1892 | if split: |
|
1892 | 1893 | out = SList(out.splitlines()) |
|
1893 | 1894 | else: |
|
1894 | 1895 | out = LSString(out) |
|
1895 | 1896 | return out |
|
1896 | 1897 | |
|
1897 | 1898 | #------------------------------------------------------------------------- |
|
1898 | 1899 | # Things related to aliases |
|
1899 | 1900 | #------------------------------------------------------------------------- |
|
1900 | 1901 | |
|
1901 | 1902 | def init_alias(self): |
|
1902 | 1903 | self.alias_manager = AliasManager(shell=self, config=self.config) |
|
1903 | 1904 | self.ns_table['alias'] = self.alias_manager.alias_table, |
|
1904 | 1905 | |
|
1905 | 1906 | #------------------------------------------------------------------------- |
|
1906 | 1907 | # Things related to extensions and plugins |
|
1907 | 1908 | #------------------------------------------------------------------------- |
|
1908 | 1909 | |
|
1909 | 1910 | def init_extension_manager(self): |
|
1910 | 1911 | self.extension_manager = ExtensionManager(shell=self, config=self.config) |
|
1911 | 1912 | |
|
1912 | 1913 | def init_plugin_manager(self): |
|
1913 | 1914 | self.plugin_manager = PluginManager(config=self.config) |
|
1914 | 1915 | |
|
1915 | 1916 | #------------------------------------------------------------------------- |
|
1916 | 1917 | # Things related to payloads |
|
1917 | 1918 | #------------------------------------------------------------------------- |
|
1918 | 1919 | |
|
1919 | 1920 | def init_payload(self): |
|
1920 | 1921 | self.payload_manager = PayloadManager(config=self.config) |
|
1921 | 1922 | |
|
1922 | 1923 | #------------------------------------------------------------------------- |
|
1923 | 1924 | # Things related to the prefilter |
|
1924 | 1925 | #------------------------------------------------------------------------- |
|
1925 | 1926 | |
|
1926 | 1927 | def init_prefilter(self): |
|
1927 | 1928 | self.prefilter_manager = PrefilterManager(shell=self, config=self.config) |
|
1928 | 1929 | # Ultimately this will be refactored in the new interpreter code, but |
|
1929 | 1930 | # for now, we should expose the main prefilter method (there's legacy |
|
1930 | 1931 | # code out there that may rely on this). |
|
1931 | 1932 | self.prefilter = self.prefilter_manager.prefilter_lines |
|
1932 | 1933 | |
|
1933 | 1934 | |
|
1934 | 1935 | def auto_rewrite_input(self, cmd): |
|
1935 | 1936 | """Print to the screen the rewritten form of the user's command. |
|
1936 | 1937 | |
|
1937 | 1938 | This shows visual feedback by rewriting input lines that cause |
|
1938 | 1939 | automatic calling to kick in, like:: |
|
1939 | 1940 | |
|
1940 | 1941 | /f x |
|
1941 | 1942 | |
|
1942 | 1943 | into:: |
|
1943 | 1944 | |
|
1944 | 1945 | ------> f(x) |
|
1945 | 1946 | |
|
1946 | 1947 | after the user's input prompt. This helps the user understand that the |
|
1947 | 1948 | input line was transformed automatically by IPython. |
|
1948 | 1949 | """ |
|
1949 | 1950 | rw = self.displayhook.prompt1.auto_rewrite() + cmd |
|
1950 | 1951 | |
|
1951 | 1952 | try: |
|
1952 | 1953 | # plain ascii works better w/ pyreadline, on some machines, so |
|
1953 | 1954 | # we use it and only print uncolored rewrite if we have unicode |
|
1954 | 1955 | rw = str(rw) |
|
1955 | 1956 | print >> IPython.utils.io.Term.cout, rw |
|
1956 | 1957 | except UnicodeEncodeError: |
|
1957 | 1958 | print "------> " + cmd |
|
1958 | 1959 | |
|
1959 | 1960 | #------------------------------------------------------------------------- |
|
1960 | 1961 | # Things related to extracting values/expressions from kernel and user_ns |
|
1961 | 1962 | #------------------------------------------------------------------------- |
|
1962 | 1963 | |
|
1963 | 1964 | def _simple_error(self): |
|
1964 | 1965 | etype, value = sys.exc_info()[:2] |
|
1965 | 1966 | return u'[ERROR] {e.__name__}: {v}'.format(e=etype, v=value) |
|
1966 | 1967 | |
|
1967 | 1968 | def user_variables(self, names): |
|
1968 | 1969 | """Get a list of variable names from the user's namespace. |
|
1969 | 1970 | |
|
1970 | 1971 | Parameters |
|
1971 | 1972 | ---------- |
|
1972 | 1973 | names : list of strings |
|
1973 | 1974 | A list of names of variables to be read from the user namespace. |
|
1974 | 1975 | |
|
1975 | 1976 | Returns |
|
1976 | 1977 | ------- |
|
1977 | 1978 | A dict, keyed by the input names and with the repr() of each value. |
|
1978 | 1979 | """ |
|
1979 | 1980 | out = {} |
|
1980 | 1981 | user_ns = self.user_ns |
|
1981 | 1982 | for varname in names: |
|
1982 | 1983 | try: |
|
1983 | 1984 | value = repr(user_ns[varname]) |
|
1984 | 1985 | except: |
|
1985 | 1986 | value = self._simple_error() |
|
1986 | 1987 | out[varname] = value |
|
1987 | 1988 | return out |
|
1988 | 1989 | |
|
1989 | 1990 | def user_expressions(self, expressions): |
|
1990 | 1991 | """Evaluate a dict of expressions in the user's namespace. |
|
1991 | 1992 | |
|
1992 | 1993 | Parameters |
|
1993 | 1994 | ---------- |
|
1994 | 1995 | expressions : dict |
|
1995 | 1996 | A dict with string keys and string values. The expression values |
|
1996 | 1997 | should be valid Python expressions, each of which will be evaluated |
|
1997 | 1998 | in the user namespace. |
|
1998 | 1999 | |
|
1999 | 2000 | Returns |
|
2000 | 2001 | ------- |
|
2001 | 2002 | A dict, keyed like the input expressions dict, with the repr() of each |
|
2002 | 2003 | value. |
|
2003 | 2004 | """ |
|
2004 | 2005 | out = {} |
|
2005 | 2006 | user_ns = self.user_ns |
|
2006 | 2007 | global_ns = self.user_global_ns |
|
2007 | 2008 | for key, expr in expressions.iteritems(): |
|
2008 | 2009 | try: |
|
2009 | 2010 | value = repr(eval(expr, global_ns, user_ns)) |
|
2010 | 2011 | except: |
|
2011 | 2012 | value = self._simple_error() |
|
2012 | 2013 | out[key] = value |
|
2013 | 2014 | return out |
|
2014 | 2015 | |
|
2015 | 2016 | #------------------------------------------------------------------------- |
|
2016 | 2017 | # Things related to the running of code |
|
2017 | 2018 | #------------------------------------------------------------------------- |
|
2018 | 2019 | |
|
2019 | 2020 | def ex(self, cmd): |
|
2020 | 2021 | """Execute a normal python statement in user namespace.""" |
|
2021 | 2022 | with nested(self.builtin_trap,): |
|
2022 | 2023 | exec cmd in self.user_global_ns, self.user_ns |
|
2023 | 2024 | |
|
2024 | 2025 | def ev(self, expr): |
|
2025 | 2026 | """Evaluate python expression expr in user namespace. |
|
2026 | 2027 | |
|
2027 | 2028 | Returns the result of evaluation |
|
2028 | 2029 | """ |
|
2029 | 2030 | with nested(self.builtin_trap,): |
|
2030 | 2031 | return eval(expr, self.user_global_ns, self.user_ns) |
|
2031 | 2032 | |
|
2032 | 2033 | def safe_execfile(self, fname, *where, **kw): |
|
2033 | 2034 | """A safe version of the builtin execfile(). |
|
2034 | 2035 | |
|
2035 | 2036 | This version will never throw an exception, but instead print |
|
2036 | 2037 | helpful error messages to the screen. This only works on pure |
|
2037 | 2038 | Python files with the .py extension. |
|
2038 | 2039 | |
|
2039 | 2040 | Parameters |
|
2040 | 2041 | ---------- |
|
2041 | 2042 | fname : string |
|
2042 | 2043 | The name of the file to be executed. |
|
2043 | 2044 | where : tuple |
|
2044 | 2045 | One or two namespaces, passed to execfile() as (globals,locals). |
|
2045 | 2046 | If only one is given, it is passed as both. |
|
2046 | 2047 | exit_ignore : bool (False) |
|
2047 | 2048 | If True, then silence SystemExit for non-zero status (it is always |
|
2048 | 2049 | silenced for zero status, as it is so common). |
|
2049 | 2050 | """ |
|
2050 | 2051 | kw.setdefault('exit_ignore', False) |
|
2051 | 2052 | |
|
2052 | 2053 | fname = os.path.abspath(os.path.expanduser(fname)) |
|
2053 | 2054 | |
|
2054 | 2055 | # Make sure we have a .py file |
|
2055 | 2056 | if not fname.endswith('.py'): |
|
2056 | 2057 | warn('File must end with .py to be run using execfile: <%s>' % fname) |
|
2057 | 2058 | |
|
2058 | 2059 | # Make sure we can open the file |
|
2059 | 2060 | try: |
|
2060 | 2061 | with open(fname) as thefile: |
|
2061 | 2062 | pass |
|
2062 | 2063 | except: |
|
2063 | 2064 | warn('Could not open file <%s> for safe execution.' % fname) |
|
2064 | 2065 | return |
|
2065 | 2066 | |
|
2066 | 2067 | # Find things also in current directory. This is needed to mimic the |
|
2067 | 2068 | # behavior of running a script from the system command line, where |
|
2068 | 2069 | # Python inserts the script's directory into sys.path |
|
2069 | 2070 | dname = os.path.dirname(fname) |
|
2070 | 2071 | |
|
2071 | 2072 | with prepended_to_syspath(dname): |
|
2072 | 2073 | try: |
|
2073 | 2074 | execfile(fname,*where) |
|
2074 | 2075 | except SystemExit, status: |
|
2075 | 2076 | # If the call was made with 0 or None exit status (sys.exit(0) |
|
2076 | 2077 | # or sys.exit() ), don't bother showing a traceback, as both of |
|
2077 | 2078 | # these are considered normal by the OS: |
|
2078 | 2079 | # > python -c'import sys;sys.exit(0)'; echo $? |
|
2079 | 2080 | # 0 |
|
2080 | 2081 | # > python -c'import sys;sys.exit()'; echo $? |
|
2081 | 2082 | # 0 |
|
2082 | 2083 | # For other exit status, we show the exception unless |
|
2083 | 2084 | # explicitly silenced, but only in short form. |
|
2084 | 2085 | if status.code not in (0, None) and not kw['exit_ignore']: |
|
2085 | 2086 | self.showtraceback(exception_only=True) |
|
2086 | 2087 | except: |
|
2087 | 2088 | self.showtraceback() |
|
2088 | 2089 | |
|
2089 | 2090 | def safe_execfile_ipy(self, fname): |
|
2090 | 2091 | """Like safe_execfile, but for .ipy files with IPython syntax. |
|
2091 | 2092 | |
|
2092 | 2093 | Parameters |
|
2093 | 2094 | ---------- |
|
2094 | 2095 | fname : str |
|
2095 | 2096 | The name of the file to execute. The filename must have a |
|
2096 | 2097 | .ipy extension. |
|
2097 | 2098 | """ |
|
2098 | 2099 | fname = os.path.abspath(os.path.expanduser(fname)) |
|
2099 | 2100 | |
|
2100 | 2101 | # Make sure we have a .py file |
|
2101 | 2102 | if not fname.endswith('.ipy'): |
|
2102 | 2103 | warn('File must end with .py to be run using execfile: <%s>' % fname) |
|
2103 | 2104 | |
|
2104 | 2105 | # Make sure we can open the file |
|
2105 | 2106 | try: |
|
2106 | 2107 | with open(fname) as thefile: |
|
2107 | 2108 | pass |
|
2108 | 2109 | except: |
|
2109 | 2110 | warn('Could not open file <%s> for safe execution.' % fname) |
|
2110 | 2111 | return |
|
2111 | 2112 | |
|
2112 | 2113 | # Find things also in current directory. This is needed to mimic the |
|
2113 | 2114 | # behavior of running a script from the system command line, where |
|
2114 | 2115 | # Python inserts the script's directory into sys.path |
|
2115 | 2116 | dname = os.path.dirname(fname) |
|
2116 | 2117 | |
|
2117 | 2118 | with prepended_to_syspath(dname): |
|
2118 | 2119 | try: |
|
2119 | 2120 | with open(fname) as thefile: |
|
2120 | 2121 | script = thefile.read() |
|
2121 | 2122 | # self.runlines currently captures all exceptions |
|
2122 | 2123 | # raise in user code. It would be nice if there were |
|
2123 | 2124 | # versions of runlines, execfile that did raise, so |
|
2124 | 2125 | # we could catch the errors. |
|
2125 | 2126 | self.runlines(script, clean=True) |
|
2126 | 2127 | except: |
|
2127 | 2128 | self.showtraceback() |
|
2128 | 2129 | warn('Unknown failure executing file: <%s>' % fname) |
|
2129 | 2130 | |
|
2130 | 2131 | def run_cell(self, cell): |
|
2131 | 2132 | """Run the contents of an entire multiline 'cell' of code. |
|
2132 | 2133 | |
|
2133 | 2134 | The cell is split into separate blocks which can be executed |
|
2134 | 2135 | individually. Then, based on how many blocks there are, they are |
|
2135 | 2136 | executed as follows: |
|
2136 | 2137 | |
|
2137 | 2138 | - A single block: 'single' mode. |
|
2138 | 2139 | |
|
2139 | 2140 | If there's more than one block, it depends: |
|
2140 | 2141 | |
|
2141 | 2142 | - if the last one is no more than two lines long, run all but the last |
|
2142 | 2143 | in 'exec' mode and the very last one in 'single' mode. This makes it |
|
2143 | 2144 | easy to type simple expressions at the end to see computed values. - |
|
2144 | 2145 | otherwise (last one is also multiline), run all in 'exec' mode |
|
2145 | 2146 | |
|
2146 | 2147 | When code is executed in 'single' mode, :func:`sys.displayhook` fires, |
|
2147 | 2148 | results are displayed and output prompts are computed. In 'exec' mode, |
|
2148 | 2149 | no results are displayed unless :func:`print` is called explicitly; |
|
2149 | 2150 | this mode is more akin to running a script. |
|
2150 | 2151 | |
|
2151 | 2152 | Parameters |
|
2152 | 2153 | ---------- |
|
2153 | 2154 | cell : str |
|
2154 | 2155 | A single or multiline string. |
|
2155 | 2156 | """ |
|
2156 | 2157 | ################################################################# |
|
2157 | 2158 | # FIXME |
|
2158 | 2159 | # ===== |
|
2159 | 2160 | # This execution logic should stop calling runlines altogether, and |
|
2160 | 2161 | # instead we should do what runlines does, in a controlled manner, here |
|
2161 | 2162 | # (runlines mutates lots of state as it goes calling sub-methods that |
|
2162 | 2163 | # also mutate state). Basically we should: |
|
2163 | 2164 | # - apply dynamic transforms for single-line input (the ones that |
|
2164 | 2165 | # split_blocks won't apply since they need context). |
|
2165 | 2166 | # - increment the global execution counter (we need to pull that out |
|
2166 | 2167 | # from outputcache's control; outputcache should instead read it from |
|
2167 | 2168 | # the main object). |
|
2168 | 2169 | # - do any logging of input |
|
2169 | 2170 | # - update histories (raw/translated) |
|
2170 | 2171 | # - then, call plain runsource (for single blocks, so displayhook is |
|
2171 | 2172 | # triggered) or runcode (for multiline blocks in exec mode). |
|
2172 | 2173 | # |
|
2173 | 2174 | # Once this is done, we'll be able to stop using runlines and we'll |
|
2174 | 2175 | # also have a much cleaner separation of logging, input history and |
|
2175 | 2176 | # output cache management. |
|
2176 | 2177 | ################################################################# |
|
2177 | 2178 | |
|
2178 | 2179 | # We need to break up the input into executable blocks that can be run |
|
2179 | 2180 | # in 'single' mode, to provide comfortable user behavior. |
|
2180 | 2181 | blocks = self.input_splitter.split_blocks(cell) |
|
2181 | 2182 | |
|
2182 | 2183 | if not blocks: |
|
2183 | 2184 | return |
|
2184 | 2185 | |
|
2185 | 2186 | # Single-block input should behave like an interactive prompt |
|
2186 | 2187 | if len(blocks) == 1: |
|
2187 | 2188 | self.runlines(blocks[0]) |
|
2188 | 2189 | return |
|
2189 | 2190 | |
|
2190 | 2191 | # In multi-block input, if the last block is a simple (one-two lines) |
|
2191 | 2192 | # expression, run it in single mode so it produces output. Otherwise |
|
2192 | 2193 | # just feed the whole thing to runcode. |
|
2193 | 2194 | # This seems like a reasonable usability design. |
|
2194 | 2195 | last = blocks[-1] |
|
2195 | 2196 | |
|
2196 | 2197 | # Note: below, whenever we call runcode, we must sync history |
|
2197 | 2198 | # ourselves, because runcode is NOT meant to manage history at all. |
|
2198 | 2199 | if len(last.splitlines()) < 2: |
|
2199 | 2200 | # Get the main body to run as a cell |
|
2200 | 2201 | body = ''.join(blocks[:-1]) |
|
2201 | 2202 | self.input_hist.append(body) |
|
2202 | 2203 | self.input_hist_raw.append(body) |
|
2203 | 2204 | self.runcode(body, post_execute=False) |
|
2204 | 2205 | # And the last expression via runlines so it produces output |
|
2205 | 2206 | self.runlines(last) |
|
2206 | 2207 | else: |
|
2207 | 2208 | # Run the whole cell as one entity |
|
2208 | 2209 | self.input_hist.append(cell) |
|
2209 | 2210 | self.input_hist_raw.append(cell) |
|
2210 | 2211 | self.runcode(cell) |
|
2211 | 2212 | |
|
2212 | 2213 | def runlines(self, lines, clean=False): |
|
2213 | 2214 | """Run a string of one or more lines of source. |
|
2214 | 2215 | |
|
2215 | 2216 | This method is capable of running a string containing multiple source |
|
2216 | 2217 | lines, as if they had been entered at the IPython prompt. Since it |
|
2217 | 2218 | exposes IPython's processing machinery, the given strings can contain |
|
2218 | 2219 | magic calls (%magic), special shell access (!cmd), etc. |
|
2219 | 2220 | """ |
|
2220 | 2221 | |
|
2221 | 2222 | if isinstance(lines, (list, tuple)): |
|
2222 | 2223 | lines = '\n'.join(lines) |
|
2223 | 2224 | |
|
2224 | 2225 | if clean: |
|
2225 | 2226 | lines = self._cleanup_ipy_script(lines) |
|
2226 | 2227 | |
|
2227 | 2228 | # We must start with a clean buffer, in case this is run from an |
|
2228 | 2229 | # interactive IPython session (via a magic, for example). |
|
2229 | 2230 | self.resetbuffer() |
|
2230 | 2231 | lines = lines.splitlines() |
|
2231 | 2232 | more = 0 |
|
2232 | 2233 | with nested(self.builtin_trap, self.display_trap): |
|
2233 | 2234 | for line in lines: |
|
2234 | 2235 | # skip blank lines so we don't mess up the prompt counter, but |
|
2235 | 2236 | # do NOT skip even a blank line if we are in a code block (more |
|
2236 | 2237 | # is true) |
|
2237 | 2238 | |
|
2238 | 2239 | if line or more: |
|
2239 | 2240 | # push to raw history, so hist line numbers stay in sync |
|
2240 | 2241 | self.input_hist_raw.append(line + '\n') |
|
2241 | 2242 | prefiltered = self.prefilter_manager.prefilter_lines(line, |
|
2242 | 2243 | more) |
|
2243 | 2244 | more = self.push_line(prefiltered) |
|
2244 | 2245 | # IPython's runsource returns None if there was an error |
|
2245 | 2246 | # compiling the code. This allows us to stop processing |
|
2246 | 2247 | # right away, so the user gets the error message at the |
|
2247 | 2248 | # right place. |
|
2248 | 2249 | if more is None: |
|
2249 | 2250 | break |
|
2250 | 2251 | else: |
|
2251 | 2252 | self.input_hist_raw.append("\n") |
|
2252 | 2253 | # final newline in case the input didn't have it, so that the code |
|
2253 | 2254 | # actually does get executed |
|
2254 | 2255 | if more: |
|
2255 | 2256 | self.push_line('\n') |
|
2256 | 2257 | |
|
2257 | 2258 | def runsource(self, source, filename='<input>', symbol='single'): |
|
2258 | 2259 | """Compile and run some source in the interpreter. |
|
2259 | 2260 | |
|
2260 | 2261 | Arguments are as for compile_command(). |
|
2261 | 2262 | |
|
2262 | 2263 | One several things can happen: |
|
2263 | 2264 | |
|
2264 | 2265 | 1) The input is incorrect; compile_command() raised an |
|
2265 | 2266 | exception (SyntaxError or OverflowError). A syntax traceback |
|
2266 | 2267 | will be printed by calling the showsyntaxerror() method. |
|
2267 | 2268 | |
|
2268 | 2269 | 2) The input is incomplete, and more input is required; |
|
2269 | 2270 | compile_command() returned None. Nothing happens. |
|
2270 | 2271 | |
|
2271 | 2272 | 3) The input is complete; compile_command() returned a code |
|
2272 | 2273 | object. The code is executed by calling self.runcode() (which |
|
2273 | 2274 | also handles run-time exceptions, except for SystemExit). |
|
2274 | 2275 | |
|
2275 | 2276 | The return value is: |
|
2276 | 2277 | |
|
2277 | 2278 | - True in case 2 |
|
2278 | 2279 | |
|
2279 | 2280 | - False in the other cases, unless an exception is raised, where |
|
2280 | 2281 | None is returned instead. This can be used by external callers to |
|
2281 | 2282 | know whether to continue feeding input or not. |
|
2282 | 2283 | |
|
2283 | 2284 | The return value can be used to decide whether to use sys.ps1 or |
|
2284 | 2285 | sys.ps2 to prompt the next line.""" |
|
2285 | 2286 | |
|
2286 | 2287 | # We need to ensure that the source is unicode from here on. |
|
2287 | 2288 | if type(source)==str: |
|
2288 | 2289 | source = source.decode(self.stdin_encoding) |
|
2289 | 2290 | |
|
2290 | 2291 | # if the source code has leading blanks, add 'if 1:\n' to it |
|
2291 | 2292 | # this allows execution of indented pasted code. It is tempting |
|
2292 | 2293 | # to add '\n' at the end of source to run commands like ' a=1' |
|
2293 | 2294 | # directly, but this fails for more complicated scenarios |
|
2294 | 2295 | |
|
2295 | 2296 | if source[:1] in [' ', '\t']: |
|
2296 | 2297 | source = u'if 1:\n%s' % source |
|
2297 | 2298 | |
|
2298 | 2299 | try: |
|
2299 | 2300 | code = self.compile(source,filename,symbol) |
|
2300 | 2301 | except (OverflowError, SyntaxError, ValueError, TypeError, MemoryError): |
|
2301 | 2302 | # Case 1 |
|
2302 | 2303 | self.showsyntaxerror(filename) |
|
2303 | 2304 | return None |
|
2304 | 2305 | |
|
2305 | 2306 | if code is None: |
|
2306 | 2307 | # Case 2 |
|
2307 | 2308 | return True |
|
2308 | 2309 | |
|
2309 | 2310 | # Case 3 |
|
2310 | 2311 | # We store the code object so that threaded shells and |
|
2311 | 2312 | # custom exception handlers can access all this info if needed. |
|
2312 | 2313 | # The source corresponding to this can be obtained from the |
|
2313 | 2314 | # buffer attribute as '\n'.join(self.buffer). |
|
2314 | 2315 | self.code_to_run = code |
|
2315 | 2316 | # now actually execute the code object |
|
2316 | 2317 | if self.runcode(code) == 0: |
|
2317 | 2318 | return False |
|
2318 | 2319 | else: |
|
2319 | 2320 | return None |
|
2320 | 2321 | |
|
2321 | 2322 | def runcode(self, code_obj, post_execute=True): |
|
2322 | 2323 | """Execute a code object. |
|
2323 | 2324 | |
|
2324 | 2325 | When an exception occurs, self.showtraceback() is called to display a |
|
2325 | 2326 | traceback. |
|
2326 | 2327 | |
|
2327 | 2328 | Return value: a flag indicating whether the code to be run completed |
|
2328 | 2329 | successfully: |
|
2329 | 2330 | |
|
2330 | 2331 | - 0: successful execution. |
|
2331 | 2332 | - 1: an error occurred. |
|
2332 | 2333 | """ |
|
2333 | 2334 | |
|
2334 | 2335 | # Set our own excepthook in case the user code tries to call it |
|
2335 | 2336 | # directly, so that the IPython crash handler doesn't get triggered |
|
2336 | 2337 | old_excepthook,sys.excepthook = sys.excepthook, self.excepthook |
|
2337 | 2338 | |
|
2338 | 2339 | # we save the original sys.excepthook in the instance, in case config |
|
2339 | 2340 | # code (such as magics) needs access to it. |
|
2340 | 2341 | self.sys_excepthook = old_excepthook |
|
2341 | 2342 | outflag = 1 # happens in more places, so it's easier as default |
|
2342 | 2343 | try: |
|
2343 | 2344 | try: |
|
2344 | 2345 | self.hooks.pre_runcode_hook() |
|
2345 | 2346 | #rprint('Running code') # dbg |
|
2346 | 2347 | exec code_obj in self.user_global_ns, self.user_ns |
|
2347 | 2348 | finally: |
|
2348 | 2349 | # Reset our crash handler in place |
|
2349 | 2350 | sys.excepthook = old_excepthook |
|
2350 | 2351 | except SystemExit: |
|
2351 | 2352 | self.resetbuffer() |
|
2352 | 2353 | self.showtraceback(exception_only=True) |
|
2353 | 2354 | warn("To exit: use any of 'exit', 'quit', %Exit or Ctrl-D.", level=1) |
|
2354 | 2355 | except self.custom_exceptions: |
|
2355 | 2356 | etype,value,tb = sys.exc_info() |
|
2356 | 2357 | self.CustomTB(etype,value,tb) |
|
2357 | 2358 | except: |
|
2358 | 2359 | self.showtraceback() |
|
2359 | 2360 | else: |
|
2360 | 2361 | outflag = 0 |
|
2361 | 2362 | if softspace(sys.stdout, 0): |
|
2362 | 2363 | |
|
2363 | 2364 | |
|
2364 | 2365 | # Execute any registered post-execution functions. Here, any errors |
|
2365 | 2366 | # are reported only minimally and just on the terminal, because the |
|
2366 | 2367 | # main exception channel may be occupied with a user traceback. |
|
2367 | 2368 | # FIXME: we need to think this mechanism a little more carefully. |
|
2368 | 2369 | if post_execute: |
|
2369 | 2370 | for func in self._post_execute: |
|
2370 | 2371 | try: |
|
2371 | 2372 | func() |
|
2372 | 2373 | except: |
|
2373 | 2374 | head = '[ ERROR ] Evaluating post_execute function: %s' % \ |
|
2374 | 2375 | func |
|
2375 | 2376 | print >> io.Term.cout, head |
|
2376 | 2377 | print >> io.Term.cout, self._simple_error() |
|
2377 | 2378 | print >> io.Term.cout, 'Removing from post_execute' |
|
2378 | 2379 | self._post_execute.remove(func) |
|
2379 | 2380 | |
|
2380 | 2381 | # Flush out code object which has been run (and source) |
|
2381 | 2382 | self.code_to_run = None |
|
2382 | 2383 | return outflag |
|
2383 | 2384 | |
|
2384 | 2385 | def push_line(self, line): |
|
2385 | 2386 | """Push a line to the interpreter. |
|
2386 | 2387 | |
|
2387 | 2388 | The line should not have a trailing newline; it may have |
|
2388 | 2389 | internal newlines. The line is appended to a buffer and the |
|
2389 | 2390 | interpreter's runsource() method is called with the |
|
2390 | 2391 | concatenated contents of the buffer as source. If this |
|
2391 | 2392 | indicates that the command was executed or invalid, the buffer |
|
2392 | 2393 | is reset; otherwise, the command is incomplete, and the buffer |
|
2393 | 2394 | is left as it was after the line was appended. The return |
|
2394 | 2395 | value is 1 if more input is required, 0 if the line was dealt |
|
2395 | 2396 | with in some way (this is the same as runsource()). |
|
2396 | 2397 | """ |
|
2397 | 2398 | |
|
2398 | 2399 | # autoindent management should be done here, and not in the |
|
2399 | 2400 | # interactive loop, since that one is only seen by keyboard input. We |
|
2400 | 2401 | # need this done correctly even for code run via runlines (which uses |
|
2401 | 2402 | # push). |
|
2402 | 2403 | |
|
2403 | 2404 | #print 'push line: <%s>' % line # dbg |
|
2404 | 2405 | for subline in line.splitlines(): |
|
2405 | 2406 | self._autoindent_update(subline) |
|
2406 | 2407 | self.buffer.append(line) |
|
2407 | 2408 | more = self.runsource('\n'.join(self.buffer), self.filename) |
|
2408 | 2409 | if not more: |
|
2409 | 2410 | self.resetbuffer() |
|
2410 | 2411 | return more |
|
2411 | 2412 | |
|
2412 | 2413 | def resetbuffer(self): |
|
2413 | 2414 | """Reset the input buffer.""" |
|
2414 | 2415 | self.buffer[:] = [] |
|
2415 | 2416 | |
|
2416 | 2417 | def _is_secondary_block_start(self, s): |
|
2417 | 2418 | if not s.endswith(':'): |
|
2418 | 2419 | return False |
|
2419 | 2420 | if (s.startswith('elif') or |
|
2420 | 2421 | s.startswith('else') or |
|
2421 | 2422 | s.startswith('except') or |
|
2422 | 2423 | s.startswith('finally')): |
|
2423 | 2424 | return True |
|
2424 | 2425 | |
|
2425 | 2426 | def _cleanup_ipy_script(self, script): |
|
2426 | 2427 | """Make a script safe for self.runlines() |
|
2427 | 2428 | |
|
2428 | 2429 | Currently, IPython is lines based, with blocks being detected by |
|
2429 | 2430 | empty lines. This is a problem for block based scripts that may |
|
2430 | 2431 | not have empty lines after blocks. This script adds those empty |
|
2431 | 2432 | lines to make scripts safe for running in the current line based |
|
2432 | 2433 | IPython. |
|
2433 | 2434 | """ |
|
2434 | 2435 | res = [] |
|
2435 | 2436 | lines = script.splitlines() |
|
2436 | 2437 | level = 0 |
|
2437 | 2438 | |
|
2438 | 2439 | for l in lines: |
|
2439 | 2440 | lstripped = l.lstrip() |
|
2440 | 2441 | stripped = l.strip() |
|
2441 | 2442 | if not stripped: |
|
2442 | 2443 | continue |
|
2443 | 2444 | newlevel = len(l) - len(lstripped) |
|
2444 | 2445 | if level > 0 and newlevel == 0 and \ |
|
2445 | 2446 | not self._is_secondary_block_start(stripped): |
|
2446 | 2447 | # add empty line |
|
2447 | 2448 | res.append('') |
|
2448 | 2449 | res.append(l) |
|
2449 | 2450 | level = newlevel |
|
2450 | 2451 | |
|
2451 | 2452 | return '\n'.join(res) + '\n' |
|
2452 | 2453 | |
|
2453 | 2454 | def _autoindent_update(self,line): |
|
2454 | 2455 | """Keep track of the indent level.""" |
|
2455 | 2456 | |
|
2456 | 2457 | #debugx('line') |
|
2457 | 2458 | #debugx('self.indent_current_nsp') |
|
2458 | 2459 | if self.autoindent: |
|
2459 | 2460 | if line: |
|
2460 | 2461 | inisp = num_ini_spaces(line) |
|
2461 | 2462 | if inisp < self.indent_current_nsp: |
|
2462 | 2463 | self.indent_current_nsp = inisp |
|
2463 | 2464 | |
|
2464 | 2465 | if line[-1] == ':': |
|
2465 | 2466 | self.indent_current_nsp += 4 |
|
2466 | 2467 | elif dedent_re.match(line): |
|
2467 | 2468 | self.indent_current_nsp -= 4 |
|
2468 | 2469 | else: |
|
2469 | 2470 | self.indent_current_nsp = 0 |
|
2470 | 2471 | |
|
2471 | 2472 | #------------------------------------------------------------------------- |
|
2472 | 2473 | # Things related to GUI support and pylab |
|
2473 | 2474 | #------------------------------------------------------------------------- |
|
2474 | 2475 | |
|
2475 | 2476 | def enable_pylab(self, gui=None): |
|
2476 | 2477 | raise NotImplementedError('Implement enable_pylab in a subclass') |
|
2477 | 2478 | |
|
2478 | 2479 | #------------------------------------------------------------------------- |
|
2479 | 2480 | # Utilities |
|
2480 | 2481 | #------------------------------------------------------------------------- |
|
2481 | 2482 | |
|
2482 | 2483 | def var_expand(self,cmd,depth=0): |
|
2483 | 2484 | """Expand python variables in a string. |
|
2484 | 2485 | |
|
2485 | 2486 | The depth argument indicates how many frames above the caller should |
|
2486 | 2487 | be walked to look for the local namespace where to expand variables. |
|
2487 | 2488 | |
|
2488 | 2489 | The global namespace for expansion is always the user's interactive |
|
2489 | 2490 | namespace. |
|
2490 | 2491 | """ |
|
2491 | 2492 | |
|
2492 | 2493 | return str(ItplNS(cmd, |
|
2493 | 2494 | self.user_ns, # globals |
|
2494 | 2495 | # Skip our own frame in searching for locals: |
|
2495 | 2496 | sys._getframe(depth+1).f_locals # locals |
|
2496 | 2497 | )) |
|
2497 | 2498 | |
|
2498 | 2499 | def mktempfile(self,data=None): |
|
2499 | 2500 | """Make a new tempfile and return its filename. |
|
2500 | 2501 | |
|
2501 | 2502 | This makes a call to tempfile.mktemp, but it registers the created |
|
2502 | 2503 | filename internally so ipython cleans it up at exit time. |
|
2503 | 2504 | |
|
2504 | 2505 | Optional inputs: |
|
2505 | 2506 | |
|
2506 | 2507 | - data(None): if data is given, it gets written out to the temp file |
|
2507 | 2508 | immediately, and the file is closed again.""" |
|
2508 | 2509 | |
|
2509 | 2510 | filename = tempfile.mktemp('.py','ipython_edit_') |
|
2510 | 2511 | self.tempfiles.append(filename) |
|
2511 | 2512 | |
|
2512 | 2513 | if data: |
|
2513 | 2514 | tmp_file = open(filename,'w') |
|
2514 | 2515 | tmp_file.write(data) |
|
2515 | 2516 | tmp_file.close() |
|
2516 | 2517 | return filename |
|
2517 | 2518 | |
|
2518 | 2519 | # TODO: This should be removed when Term is refactored. |
|
2519 | 2520 | def write(self,data): |
|
2520 | 2521 | """Write a string to the default output""" |
|
2521 | 2522 | io.Term.cout.write(data) |
|
2522 | 2523 | |
|
2523 | 2524 | # TODO: This should be removed when Term is refactored. |
|
2524 | 2525 | def write_err(self,data): |
|
2525 | 2526 | """Write a string to the default error output""" |
|
2526 | 2527 | io.Term.cerr.write(data) |
|
2527 | 2528 | |
|
2528 | 2529 | def ask_yes_no(self,prompt,default=True): |
|
2529 | 2530 | if self.quiet: |
|
2530 | 2531 | return True |
|
2531 | 2532 | return ask_yes_no(prompt,default) |
|
2532 | 2533 | |
|
2533 | 2534 | def show_usage(self): |
|
2534 | 2535 | """Show a usage message""" |
|
2535 | 2536 | page.page(IPython.core.usage.interactive_usage) |
|
2536 | 2537 | |
|
2537 | 2538 | #------------------------------------------------------------------------- |
|
2538 | 2539 | # Things related to IPython exiting |
|
2539 | 2540 | #------------------------------------------------------------------------- |
|
2540 | 2541 | def atexit_operations(self): |
|
2541 | 2542 | """This will be executed at the time of exit. |
|
2542 | 2543 | |
|
2543 | 2544 | Cleanup operations and saving of persistent data that is done |
|
2544 | 2545 | unconditionally by IPython should be performed here. |
|
2545 | 2546 | |
|
2546 | 2547 | For things that may depend on startup flags or platform specifics (such |
|
2547 | 2548 | as having readline or not), register a separate atexit function in the |
|
2548 | 2549 | code that has the appropriate information, rather than trying to |
|
2549 | 2550 | clutter |
|
2550 | 2551 | """ |
|
2551 | 2552 | # Cleanup all tempfiles left around |
|
2552 | 2553 | for tfile in self.tempfiles: |
|
2553 | 2554 | try: |
|
2554 | 2555 | os.unlink(tfile) |
|
2555 | 2556 | except OSError: |
|
2556 | 2557 | pass |
|
2557 | 2558 | |
|
2558 | 2559 | # Clear all user namespaces to release all references cleanly. |
|
2559 | 2560 | self.reset() |
|
2560 | 2561 | |
|
2561 | 2562 | # Run user hooks |
|
2562 | 2563 | self.hooks.shutdown_hook() |
|
2563 | 2564 | |
|
2564 | 2565 | def cleanup(self): |
|
2565 | 2566 | self.restore_sys_module_state() |
|
2566 | 2567 | |
|
2567 | 2568 | |
|
2568 | 2569 | class InteractiveShellABC(object): |
|
2569 | 2570 | """An abstract base class for InteractiveShell.""" |
|
2570 | 2571 | __metaclass__ = abc.ABCMeta |
|
2571 | 2572 | |
|
2572 | 2573 | InteractiveShellABC.register(InteractiveShell) |
@@ -1,810 +1,892 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """Tools for inspecting Python objects. |
|
3 | 3 | |
|
4 | 4 | Uses syntax highlighting for presenting the various information elements. |
|
5 | 5 | |
|
6 | 6 | Similar in spirit to the inspect module, but all calls take a name argument to |
|
7 | 7 | reference the name under which an object is being read. |
|
8 | 8 | """ |
|
9 | 9 | |
|
10 | 10 | #***************************************************************************** |
|
11 | 11 | # Copyright (C) 2001-2004 Fernando Perez <fperez@colorado.edu> |
|
12 | 12 | # |
|
13 | 13 | # Distributed under the terms of the BSD License. The full license is in |
|
14 | 14 | # the file COPYING, distributed as part of this software. |
|
15 | 15 | #***************************************************************************** |
|
16 | 16 | |
|
17 | 17 | __all__ = ['Inspector','InspectColors'] |
|
18 | 18 | |
|
19 | 19 | # stdlib modules |
|
20 | 20 | import __builtin__ |
|
21 | 21 | import StringIO |
|
22 | 22 | import inspect |
|
23 | 23 | import linecache |
|
24 | 24 | import os |
|
25 | 25 | import string |
|
26 | 26 | import sys |
|
27 | 27 | import types |
|
28 | 28 | from collections import namedtuple |
|
29 | 29 | from itertools import izip_longest |
|
30 | 30 | |
|
31 | 31 | # IPython's own |
|
32 | 32 | from IPython.core import page |
|
33 | 33 | from IPython.external.Itpl import itpl |
|
34 | 34 | from IPython.utils import PyColorize |
|
35 | 35 | import IPython.utils.io |
|
36 | 36 | from IPython.utils.text import indent |
|
37 | 37 | from IPython.utils.wildcard import list_namespace |
|
38 | 38 | from IPython.utils.coloransi import * |
|
39 | 39 | |
|
40 | 40 | #**************************************************************************** |
|
41 | 41 | # Builtin color schemes |
|
42 | 42 | |
|
43 | 43 | Colors = TermColors # just a shorthand |
|
44 | 44 | |
|
45 | 45 | # Build a few color schemes |
|
46 | 46 | NoColor = ColorScheme( |
|
47 | 47 | 'NoColor',{ |
|
48 | 48 | 'header' : Colors.NoColor, |
|
49 | 49 | 'normal' : Colors.NoColor # color off (usu. Colors.Normal) |
|
50 | 50 | } ) |
|
51 | 51 | |
|
52 | 52 | LinuxColors = ColorScheme( |
|
53 | 53 | 'Linux',{ |
|
54 | 54 | 'header' : Colors.LightRed, |
|
55 | 55 | 'normal' : Colors.Normal # color off (usu. Colors.Normal) |
|
56 | 56 | } ) |
|
57 | 57 | |
|
58 | 58 | LightBGColors = ColorScheme( |
|
59 | 59 | 'LightBG',{ |
|
60 | 60 | 'header' : Colors.Red, |
|
61 | 61 | 'normal' : Colors.Normal # color off (usu. Colors.Normal) |
|
62 | 62 | } ) |
|
63 | 63 | |
|
64 | 64 | # Build table of color schemes (needed by the parser) |
|
65 | 65 | InspectColors = ColorSchemeTable([NoColor,LinuxColors,LightBGColors], |
|
66 | 66 | 'Linux') |
|
67 | 67 | |
|
68 | 68 | #**************************************************************************** |
|
69 | 69 | # Auxiliary functions and objects |
|
70 | 70 | |
|
71 | 71 | # See the messaging spec for the definition of all these fields. This list |
|
72 | 72 | # effectively defines the order of display |
|
73 | 73 | info_fields = ['type_name', 'base_class', 'string_form', 'namespace', |
|
74 | 74 | 'length', 'file', 'definition', 'docstring', 'source', |
|
75 | 75 | 'init_definition', 'class_docstring', 'init_docstring', |
|
76 | 76 | 'call_def', 'call_docstring', |
|
77 | 77 | # These won't be printed but will be used to determine how to |
|
78 | 78 | # format the object |
|
79 | 'ismagic', 'isalias', 'argspec', 'found', | |
|
79 | 'ismagic', 'isalias', 'argspec', 'found', 'name', | |
|
80 | 80 | ] |
|
81 | 81 | |
|
82 | 82 | |
|
83 | ObjectInfo = namedtuple('ObjectInfo', info_fields) | |
|
84 | ||
|
85 | ||
|
86 | def mk_object_info(kw): | |
|
87 | """Make a f""" | |
|
83 | def object_info(**kw): | |
|
84 | """Make an object info dict with all fields present.""" | |
|
88 | 85 | infodict = dict(izip_longest(info_fields, [None])) |
|
89 | 86 | infodict.update(kw) |
|
90 |
return |
|
|
87 | return infodict | |
|
91 | 88 | |
|
92 | 89 | |
|
93 | 90 | def getdoc(obj): |
|
94 | 91 | """Stable wrapper around inspect.getdoc. |
|
95 | 92 | |
|
96 | 93 | This can't crash because of attribute problems. |
|
97 | 94 | |
|
98 | 95 | It also attempts to call a getdoc() method on the given object. This |
|
99 | 96 | allows objects which provide their docstrings via non-standard mechanisms |
|
100 | 97 | (like Pyro proxies) to still be inspected by ipython's ? system.""" |
|
101 | 98 | |
|
102 | 99 | ds = None # default return value |
|
103 | 100 | try: |
|
104 | 101 | ds = inspect.getdoc(obj) |
|
105 | 102 | except: |
|
106 | 103 | # Harden against an inspect failure, which can occur with |
|
107 | 104 | # SWIG-wrapped extensions. |
|
108 | 105 | pass |
|
109 | 106 | # Allow objects to offer customized documentation via a getdoc method: |
|
110 | 107 | try: |
|
111 | 108 | ds2 = obj.getdoc() |
|
112 | 109 | except: |
|
113 | 110 | pass |
|
114 | 111 | else: |
|
115 | 112 | # if we get extra info, we add it to the normal docstring. |
|
116 | 113 | if ds is None: |
|
117 | 114 | ds = ds2 |
|
118 | 115 | else: |
|
119 | 116 | ds = '%s\n%s' % (ds,ds2) |
|
120 | 117 | return ds |
|
121 | 118 | |
|
122 | 119 | |
|
123 | 120 | def getsource(obj,is_binary=False): |
|
124 | 121 | """Wrapper around inspect.getsource. |
|
125 | 122 | |
|
126 | 123 | This can be modified by other projects to provide customized source |
|
127 | 124 | extraction. |
|
128 | 125 | |
|
129 | 126 | Inputs: |
|
130 | 127 | |
|
131 | 128 | - obj: an object whose source code we will attempt to extract. |
|
132 | 129 | |
|
133 | 130 | Optional inputs: |
|
134 | 131 | |
|
135 | 132 | - is_binary: whether the object is known to come from a binary source. |
|
136 | 133 | This implementation will skip returning any output for binary objects, but |
|
137 | 134 | custom extractors may know how to meaningfully process them.""" |
|
138 | 135 | |
|
139 | 136 | if is_binary: |
|
140 | 137 | return None |
|
141 | 138 | else: |
|
142 | 139 | try: |
|
143 | 140 | src = inspect.getsource(obj) |
|
144 | 141 | except TypeError: |
|
145 | 142 | if hasattr(obj,'__class__'): |
|
146 | 143 | src = inspect.getsource(obj.__class__) |
|
147 | 144 | return src |
|
148 | 145 | |
|
149 | 146 | def getargspec(obj): |
|
150 | 147 | """Get the names and default values of a function's arguments. |
|
151 | 148 | |
|
152 | 149 | A tuple of four things is returned: (args, varargs, varkw, defaults). |
|
153 | 150 | 'args' is a list of the argument names (it may contain nested lists). |
|
154 | 151 | 'varargs' and 'varkw' are the names of the * and ** arguments or None. |
|
155 | 152 | 'defaults' is an n-tuple of the default values of the last n arguments. |
|
156 | 153 | |
|
157 | 154 | Modified version of inspect.getargspec from the Python Standard |
|
158 | 155 | Library.""" |
|
159 | 156 | |
|
160 | 157 | if inspect.isfunction(obj): |
|
161 | 158 | func_obj = obj |
|
162 | 159 | elif inspect.ismethod(obj): |
|
163 | 160 | func_obj = obj.im_func |
|
161 | elif hasattr(obj, '__call__'): | |
|
162 | func_obj = obj.__call__ | |
|
164 | 163 | else: |
|
165 | 164 | raise TypeError('arg is not a Python function') |
|
166 | 165 | args, varargs, varkw = inspect.getargs(func_obj.func_code) |
|
167 | 166 | return args, varargs, varkw, func_obj.func_defaults |
|
168 | 167 | |
|
168 | ||
|
169 | def format_argspec(argspec): | |
|
170 | """Format argspect, convenience wrapper around inspect's. | |
|
171 | ||
|
172 | This takes a dict instead of ordered arguments and calls | |
|
173 | inspect.format_argspec with the arguments in the necessary order. | |
|
174 | """ | |
|
175 | return inspect.formatargspec(argspec['args'], argspec['varargs'], | |
|
176 | argspec['varkw'], argspec['defaults']) | |
|
177 | ||
|
178 | ||
|
179 | def call_tip(oinfo, format_call=True): | |
|
180 | """Extract call tip data from an oinfo dict. | |
|
181 | ||
|
182 | Parameters | |
|
183 | ---------- | |
|
184 | oinfo : dict | |
|
185 | ||
|
186 | format_call : bool, optional | |
|
187 | If True, the call line is formatted and returned as a string. If not, a | |
|
188 | tuple of (name, argspec) is returned. | |
|
189 | ||
|
190 | Returns | |
|
191 | ------- | |
|
192 | call_info : None, str or (str, dict) tuple. | |
|
193 | When format_call is True, the whole call information is formattted as a | |
|
194 | single string. Otherwise, the object's name and its argspec dict are | |
|
195 | returned. If no call information is available, None is returned. | |
|
196 | ||
|
197 | docstring : str or None | |
|
198 | The most relevant docstring for calling purposes is returned, if | |
|
199 | available. The priority is: call docstring for callable instances, then | |
|
200 | constructor docstring for classes, then main object's docstring otherwise | |
|
201 | (regular functions). | |
|
202 | """ | |
|
203 | # Get call definition | |
|
204 | argspec = oinfo['argspec'] | |
|
205 | if argspec is None: | |
|
206 | call_line = None | |
|
207 | else: | |
|
208 | # Callable objects will have 'self' as their first argument, prune | |
|
209 | # it out if it's there for clarity (since users do *not* pass an | |
|
210 | # extra first argument explicitly). | |
|
211 | try: | |
|
212 | has_self = argspec['args'][0] == 'self' | |
|
213 | except (KeyError, IndexError): | |
|
214 | pass | |
|
215 | else: | |
|
216 | if has_self: | |
|
217 | argspec['args'] = argspec['args'][1:] | |
|
218 | ||
|
219 | call_line = oinfo['name']+format_argspec(argspec) | |
|
220 | ||
|
221 | # Now get docstring. | |
|
222 | # The priority is: call docstring, constructor docstring, main one. | |
|
223 | doc = oinfo['call_docstring'] | |
|
224 | if doc is None: | |
|
225 | doc = oinfo['init_docstring'] | |
|
226 | if doc is None: | |
|
227 | doc = oinfo['docstring'] | |
|
228 | ||
|
229 | return call_line, doc | |
|
230 | ||
|
169 | 231 | #**************************************************************************** |
|
170 | 232 | # Class definitions |
|
171 | 233 | |
|
172 | 234 | class myStringIO(StringIO.StringIO): |
|
173 | 235 | """Adds a writeln method to normal StringIO.""" |
|
174 | 236 | def writeln(self,*arg,**kw): |
|
175 | 237 | """Does a write() and then a write('\n')""" |
|
176 | 238 | self.write(*arg,**kw) |
|
177 | 239 | self.write('\n') |
|
178 | 240 | |
|
179 | 241 | |
|
180 | 242 | class Inspector: |
|
181 |
def __init__(self,color_table |
|
|
243 | def __init__(self, color_table=InspectColors, | |
|
244 | code_color_table=PyColorize.ANSICodeColors, | |
|
245 | scheme='NoColor', | |
|
182 | 246 | str_detail_level=0): |
|
183 | 247 | self.color_table = color_table |
|
184 | 248 | self.parser = PyColorize.Parser(code_color_table,out='str') |
|
185 | 249 | self.format = self.parser.format |
|
186 | 250 | self.str_detail_level = str_detail_level |
|
187 | 251 | self.set_active_scheme(scheme) |
|
188 | 252 | |
|
189 | 253 | def _getdef(self,obj,oname=''): |
|
190 | 254 | """Return the definition header for any callable object. |
|
191 | 255 | |
|
192 | 256 | If any exception is generated, None is returned instead and the |
|
193 | 257 | exception is suppressed.""" |
|
194 | 258 | |
|
195 | 259 | try: |
|
196 | 260 | # We need a plain string here, NOT unicode! |
|
197 | 261 | hdef = oname + inspect.formatargspec(*getargspec(obj)) |
|
198 | 262 | return hdef.encode('ascii') |
|
199 | 263 | except: |
|
200 | 264 | return None |
|
201 | 265 | |
|
202 | 266 | def __head(self,h): |
|
203 | 267 | """Return a header string with proper colors.""" |
|
204 | 268 | return '%s%s%s' % (self.color_table.active_colors.header,h, |
|
205 | 269 | self.color_table.active_colors.normal) |
|
206 | 270 | |
|
207 | 271 | def set_active_scheme(self,scheme): |
|
208 | 272 | self.color_table.set_active_scheme(scheme) |
|
209 | 273 | self.parser.color_table.set_active_scheme(scheme) |
|
210 | 274 | |
|
211 | 275 | def noinfo(self,msg,oname): |
|
212 | 276 | """Generic message when no information is found.""" |
|
213 | 277 | print 'No %s found' % msg, |
|
214 | 278 | if oname: |
|
215 | 279 | print 'for %s' % oname |
|
216 | 280 | else: |
|
217 | 281 | |
|
218 | 282 | |
|
219 | 283 | def pdef(self,obj,oname=''): |
|
220 | 284 | """Print the definition header for any callable object. |
|
221 | 285 | |
|
222 | 286 | If the object is a class, print the constructor information.""" |
|
223 | 287 | |
|
224 | 288 | if not callable(obj): |
|
225 | 289 | print 'Object is not callable.' |
|
226 | 290 | return |
|
227 | 291 | |
|
228 | 292 | header = '' |
|
229 | 293 | |
|
230 | 294 | if inspect.isclass(obj): |
|
231 | 295 | header = self.__head('Class constructor information:\n') |
|
232 | 296 | obj = obj.__init__ |
|
233 | 297 | elif type(obj) is types.InstanceType: |
|
234 | 298 | obj = obj.__call__ |
|
235 | 299 | |
|
236 | 300 | output = self._getdef(obj,oname) |
|
237 | 301 | if output is None: |
|
238 | 302 | self.noinfo('definition header',oname) |
|
239 | 303 | else: |
|
240 | 304 | print >>IPython.utils.io.Term.cout, header,self.format(output), |
|
241 | 305 | |
|
242 | 306 | def pdoc(self,obj,oname='',formatter = None): |
|
243 | 307 | """Print the docstring for any object. |
|
244 | 308 | |
|
245 | 309 | Optional: |
|
246 | 310 | -formatter: a function to run the docstring through for specially |
|
247 | 311 | formatted docstrings.""" |
|
248 | 312 | |
|
249 | 313 | head = self.__head # so that itpl can find it even if private |
|
250 | 314 | ds = getdoc(obj) |
|
251 | 315 | if formatter: |
|
252 | 316 | ds = formatter(ds) |
|
253 | 317 | if inspect.isclass(obj): |
|
254 | 318 | init_ds = getdoc(obj.__init__) |
|
255 | 319 | output = itpl('$head("Class Docstring:")\n' |
|
256 | 320 | '$indent(ds)\n' |
|
257 | 321 | '$head("Constructor Docstring"):\n' |
|
258 | 322 | '$indent(init_ds)') |
|
259 | 323 | elif (type(obj) is types.InstanceType or isinstance(obj,object)) \ |
|
260 | 324 | and hasattr(obj,'__call__'): |
|
261 | 325 | call_ds = getdoc(obj.__call__) |
|
262 | 326 | if call_ds: |
|
263 | 327 | output = itpl('$head("Class Docstring:")\n$indent(ds)\n' |
|
264 | 328 | '$head("Calling Docstring:")\n$indent(call_ds)') |
|
265 | 329 | else: |
|
266 | 330 | output = ds |
|
267 | 331 | else: |
|
268 | 332 | output = ds |
|
269 | 333 | if output is None: |
|
270 | 334 | self.noinfo('documentation',oname) |
|
271 | 335 | return |
|
272 | 336 | page.page(output) |
|
273 | 337 | |
|
274 | 338 | def psource(self,obj,oname=''): |
|
275 | 339 | """Print the source code for an object.""" |
|
276 | 340 | |
|
277 | 341 | # Flush the source cache because inspect can return out-of-date source |
|
278 | 342 | linecache.checkcache() |
|
279 | 343 | try: |
|
280 | 344 | src = getsource(obj) |
|
281 | 345 | except: |
|
282 | 346 | self.noinfo('source',oname) |
|
283 | 347 | else: |
|
284 | 348 | page.page(self.format(src)) |
|
285 | 349 | |
|
286 | 350 | def pfile(self,obj,oname=''): |
|
287 | 351 | """Show the whole file where an object was defined.""" |
|
288 | 352 | |
|
289 | 353 | try: |
|
290 | 354 | try: |
|
291 | 355 | lineno = inspect.getsourcelines(obj)[1] |
|
292 | 356 | except TypeError: |
|
293 | 357 | # For instances, try the class object like getsource() does |
|
294 | 358 | if hasattr(obj,'__class__'): |
|
295 | 359 | lineno = inspect.getsourcelines(obj.__class__)[1] |
|
296 | 360 | # Adjust the inspected object so getabsfile() below works |
|
297 | 361 | obj = obj.__class__ |
|
298 | 362 | except: |
|
299 | 363 | self.noinfo('file',oname) |
|
300 | 364 | return |
|
301 | 365 | |
|
302 | 366 | # We only reach this point if object was successfully queried |
|
303 | 367 | |
|
304 | 368 | # run contents of file through pager starting at line |
|
305 | 369 | # where the object is defined |
|
306 | 370 | ofile = inspect.getabsfile(obj) |
|
307 | 371 | |
|
308 | 372 | if (ofile.endswith('.so') or ofile.endswith('.dll')): |
|
309 | 373 | print 'File %r is binary, not printing.' % ofile |
|
310 | 374 | elif not os.path.isfile(ofile): |
|
311 | 375 | print 'File %r does not exist, not printing.' % ofile |
|
312 | 376 | else: |
|
313 | 377 | # Print only text files, not extension binaries. Note that |
|
314 | 378 | # getsourcelines returns lineno with 1-offset and page() uses |
|
315 | 379 | # 0-offset, so we must adjust. |
|
316 | 380 | page.page(self.format(open(ofile).read()),lineno-1) |
|
317 | 381 | |
|
318 | 382 | def pinfo(self,obj,oname='',formatter=None,info=None,detail_level=0): |
|
319 | 383 | """Show detailed information about an object. |
|
320 | 384 | |
|
321 | 385 | Optional arguments: |
|
322 | 386 | |
|
323 | 387 | - oname: name of the variable pointing to the object. |
|
324 | 388 | |
|
325 | 389 | - formatter: special formatter for docstrings (see pdoc) |
|
326 | 390 | |
|
327 | 391 | - info: a structure with some information fields which may have been |
|
328 | 392 | precomputed already. |
|
329 | 393 | |
|
330 | 394 | - detail_level: if set to 1, more information is given. |
|
331 | 395 | """ |
|
332 | 396 | |
|
333 | 397 | obj_type = type(obj) |
|
334 | 398 | |
|
335 | 399 | header = self.__head |
|
336 | 400 | if info is None: |
|
337 | 401 | ismagic = 0 |
|
338 | 402 | isalias = 0 |
|
339 | 403 | ospace = '' |
|
340 | 404 | else: |
|
341 | 405 | ismagic = info.ismagic |
|
342 | 406 | isalias = info.isalias |
|
343 | 407 | ospace = info.namespace |
|
344 | 408 | # Get docstring, special-casing aliases: |
|
345 | 409 | if isalias: |
|
346 | 410 | if not callable(obj): |
|
347 | 411 | try: |
|
348 | 412 | ds = "Alias to the system command:\n %s" % obj[1] |
|
349 | 413 | except: |
|
350 | 414 | ds = "Alias: " + str(obj) |
|
351 | 415 | else: |
|
352 | 416 | ds = "Alias to " + str(obj) |
|
353 | 417 | if obj.__doc__: |
|
354 | 418 | ds += "\nDocstring:\n" + obj.__doc__ |
|
355 | 419 | else: |
|
356 | 420 | ds = getdoc(obj) |
|
357 | 421 | if ds is None: |
|
358 | 422 | ds = '<no docstring>' |
|
359 | 423 | if formatter is not None: |
|
360 | 424 | ds = formatter(ds) |
|
361 | 425 | |
|
362 | 426 | # store output in a list which gets joined with \n at the end. |
|
363 | 427 | out = myStringIO() |
|
364 | 428 | |
|
365 | 429 | string_max = 200 # max size of strings to show (snipped if longer) |
|
366 | 430 | shalf = int((string_max -5)/2) |
|
367 | 431 | |
|
368 | 432 | if ismagic: |
|
369 | 433 | obj_type_name = 'Magic function' |
|
370 | 434 | elif isalias: |
|
371 | 435 | obj_type_name = 'System alias' |
|
372 | 436 | else: |
|
373 | 437 | obj_type_name = obj_type.__name__ |
|
374 | 438 | out.writeln(header('Type:\t\t')+obj_type_name) |
|
375 | 439 | |
|
376 | 440 | try: |
|
377 | 441 | bclass = obj.__class__ |
|
378 | 442 | out.writeln(header('Base Class:\t')+str(bclass)) |
|
379 | 443 | except: pass |
|
380 | 444 | |
|
381 | 445 | # String form, but snip if too long in ? form (full in ??) |
|
382 | 446 | if detail_level >= self.str_detail_level: |
|
383 | 447 | try: |
|
384 | 448 | ostr = str(obj) |
|
385 | 449 | str_head = 'String Form:' |
|
386 | 450 | if not detail_level and len(ostr)>string_max: |
|
387 | 451 | ostr = ostr[:shalf] + ' <...> ' + ostr[-shalf:] |
|
388 | 452 | ostr = ("\n" + " " * len(str_head.expandtabs())).\ |
|
389 | 453 | join(map(string.strip,ostr.split("\n"))) |
|
390 | 454 | if ostr.find('\n') > -1: |
|
391 | 455 | # Print multi-line strings starting at the next line. |
|
392 | 456 | str_sep = '\n' |
|
393 | 457 | else: |
|
394 | 458 | str_sep = '\t' |
|
395 | 459 | out.writeln("%s%s%s" % (header(str_head),str_sep,ostr)) |
|
396 | 460 | except: |
|
397 | 461 | pass |
|
398 | 462 | |
|
399 | 463 | if ospace: |
|
400 | 464 | out.writeln(header('Namespace:\t')+ospace) |
|
401 | 465 | |
|
402 | 466 | # Length (for strings and lists) |
|
403 | 467 | try: |
|
404 | 468 | length = str(len(obj)) |
|
405 | 469 | out.writeln(header('Length:\t\t')+length) |
|
406 | 470 | except: pass |
|
407 | 471 | |
|
408 | 472 | # Filename where object was defined |
|
409 | 473 | binary_file = False |
|
410 | 474 | try: |
|
411 | 475 | try: |
|
412 | 476 | fname = inspect.getabsfile(obj) |
|
413 | 477 | except TypeError: |
|
414 | 478 | # For an instance, the file that matters is where its class was |
|
415 | 479 | # declared. |
|
416 | 480 | if hasattr(obj,'__class__'): |
|
417 | 481 | fname = inspect.getabsfile(obj.__class__) |
|
418 | 482 | if fname.endswith('<string>'): |
|
419 | 483 | fname = 'Dynamically generated function. No source code available.' |
|
420 | 484 | if (fname.endswith('.so') or fname.endswith('.dll')): |
|
421 | 485 | binary_file = True |
|
422 | 486 | out.writeln(header('File:\t\t')+fname) |
|
423 | 487 | except: |
|
424 | 488 | # if anything goes wrong, we don't want to show source, so it's as |
|
425 | 489 | # if the file was binary |
|
426 | 490 | binary_file = True |
|
427 | 491 | |
|
428 | 492 | # reconstruct the function definition and print it: |
|
429 | 493 | defln = self._getdef(obj,oname) |
|
430 | 494 | if defln: |
|
431 | 495 | out.write(header('Definition:\t')+self.format(defln)) |
|
432 | 496 | |
|
433 | 497 | # Docstrings only in detail 0 mode, since source contains them (we |
|
434 | 498 | # avoid repetitions). If source fails, we add them back, see below. |
|
435 | 499 | if ds and detail_level == 0: |
|
436 | 500 | out.writeln(header('Docstring:\n') + indent(ds)) |
|
437 | 501 | |
|
438 | 502 | # Original source code for any callable |
|
439 | 503 | if detail_level: |
|
440 | 504 | # Flush the source cache because inspect can return out-of-date |
|
441 | 505 | # source |
|
442 | 506 | linecache.checkcache() |
|
443 | 507 | source_success = False |
|
444 | 508 | try: |
|
445 | 509 | try: |
|
446 | 510 | src = getsource(obj,binary_file) |
|
447 | 511 | except TypeError: |
|
448 | 512 | if hasattr(obj,'__class__'): |
|
449 | 513 | src = getsource(obj.__class__,binary_file) |
|
450 | 514 | if src is not None: |
|
451 | 515 | source = self.format(src) |
|
452 | 516 | out.write(header('Source:\n')+source.rstrip()) |
|
453 | 517 | source_success = True |
|
454 | 518 | except Exception, msg: |
|
455 | 519 | pass |
|
456 | 520 | |
|
457 | 521 | if ds and not source_success: |
|
458 | 522 | out.writeln(header('Docstring [source file open failed]:\n') |
|
459 | 523 | + indent(ds)) |
|
460 | 524 | |
|
461 | 525 | # Constructor docstring for classes |
|
462 | 526 | if inspect.isclass(obj): |
|
463 | 527 | # reconstruct the function definition and print it: |
|
464 | 528 | try: |
|
465 | 529 | obj_init = obj.__init__ |
|
466 | 530 | except AttributeError: |
|
467 | 531 | init_def = init_ds = None |
|
468 | 532 | else: |
|
469 | 533 | init_def = self._getdef(obj_init,oname) |
|
470 | 534 | init_ds = getdoc(obj_init) |
|
471 | 535 | # Skip Python's auto-generated docstrings |
|
472 | 536 | if init_ds and \ |
|
473 | 537 | init_ds.startswith('x.__init__(...) initializes'): |
|
474 | 538 | init_ds = None |
|
475 | 539 | |
|
476 | 540 | if init_def or init_ds: |
|
477 | 541 | out.writeln(header('\nConstructor information:')) |
|
478 | 542 | if init_def: |
|
479 | 543 | out.write(header('Definition:\t')+ self.format(init_def)) |
|
480 | 544 | if init_ds: |
|
481 | 545 | out.writeln(header('Docstring:\n') + indent(init_ds)) |
|
482 | 546 | # and class docstring for instances: |
|
483 | 547 | elif obj_type is types.InstanceType or \ |
|
484 | 548 | isinstance(obj,object): |
|
485 | 549 | |
|
486 | 550 | # First, check whether the instance docstring is identical to the |
|
487 | 551 | # class one, and print it separately if they don't coincide. In |
|
488 | 552 | # most cases they will, but it's nice to print all the info for |
|
489 | 553 | # objects which use instance-customized docstrings. |
|
490 | 554 | if ds: |
|
491 | 555 | try: |
|
492 | 556 | cls = getattr(obj,'__class__') |
|
493 | 557 | except: |
|
494 | 558 | class_ds = None |
|
495 | 559 | else: |
|
496 | 560 | class_ds = getdoc(cls) |
|
497 | 561 | # Skip Python's auto-generated docstrings |
|
498 | 562 | if class_ds and \ |
|
499 | 563 | (class_ds.startswith('function(code, globals[,') or \ |
|
500 | 564 | class_ds.startswith('instancemethod(function, instance,') or \ |
|
501 | 565 | class_ds.startswith('module(name[,') ): |
|
502 | 566 | class_ds = None |
|
503 | 567 | if class_ds and ds != class_ds: |
|
504 | 568 | out.writeln(header('Class Docstring:\n') + |
|
505 | 569 | indent(class_ds)) |
|
506 | 570 | |
|
507 | 571 | # Next, try to show constructor docstrings |
|
508 | 572 | try: |
|
509 | 573 | init_ds = getdoc(obj.__init__) |
|
510 | 574 | # Skip Python's auto-generated docstrings |
|
511 | 575 | if init_ds and \ |
|
512 | 576 | init_ds.startswith('x.__init__(...) initializes'): |
|
513 | 577 | init_ds = None |
|
514 | 578 | except AttributeError: |
|
515 | 579 | init_ds = None |
|
516 | 580 | if init_ds: |
|
517 | 581 | out.writeln(header('Constructor Docstring:\n') + |
|
518 | 582 | indent(init_ds)) |
|
519 | 583 | |
|
520 | 584 | # Call form docstring for callable instances |
|
521 | 585 | if hasattr(obj,'__call__'): |
|
522 | 586 | #out.writeln(header('Callable:\t')+'Yes') |
|
523 | 587 | call_def = self._getdef(obj.__call__,oname) |
|
524 | 588 | #if call_def is None: |
|
525 | 589 | # out.writeln(header('Call def:\t')+ |
|
526 | 590 | # 'Calling definition not available.') |
|
527 | 591 | if call_def is not None: |
|
528 | 592 | out.writeln(header('Call def:\t')+self.format(call_def)) |
|
529 | 593 | call_ds = getdoc(obj.__call__) |
|
530 | 594 | # Skip Python's auto-generated docstrings |
|
531 | 595 | if call_ds and call_ds.startswith('x.__call__(...) <==> x(...)'): |
|
532 | 596 | call_ds = None |
|
533 | 597 | if call_ds: |
|
534 | 598 | out.writeln(header('Call docstring:\n') + indent(call_ds)) |
|
535 | 599 | |
|
536 | 600 | # Finally send to printer/pager |
|
537 | 601 | output = out.getvalue() |
|
538 | 602 | if output: |
|
539 | 603 | page.page(output) |
|
540 | 604 | # end pinfo |
|
541 | 605 | |
|
542 | 606 | def info(self, obj, oname='', formatter=None, info=None, detail_level=0): |
|
543 | 607 | """Compute a dict with detailed information about an object. |
|
544 | 608 | |
|
545 | 609 | Optional arguments: |
|
546 | 610 | |
|
547 | 611 | - oname: name of the variable pointing to the object. |
|
548 | 612 | |
|
549 | 613 | - formatter: special formatter for docstrings (see pdoc) |
|
550 | 614 | |
|
551 | 615 | - info: a structure with some information fields which may have been |
|
552 | 616 | precomputed already. |
|
553 | 617 | |
|
554 | 618 | - detail_level: if set to 1, more information is given. |
|
555 | 619 | """ |
|
556 | 620 | |
|
557 | 621 | obj_type = type(obj) |
|
558 | 622 | |
|
559 | 623 | header = self.__head |
|
560 | 624 | if info is None: |
|
561 | 625 | ismagic = 0 |
|
562 | 626 | isalias = 0 |
|
563 | 627 | ospace = '' |
|
564 | 628 | else: |
|
565 | 629 | ismagic = info.ismagic |
|
566 | 630 | isalias = info.isalias |
|
567 | 631 | ospace = info.namespace |
|
632 | ||
|
568 | 633 | # Get docstring, special-casing aliases: |
|
569 | 634 | if isalias: |
|
570 | 635 | if not callable(obj): |
|
571 | 636 | try: |
|
572 | 637 | ds = "Alias to the system command:\n %s" % obj[1] |
|
573 | 638 | except: |
|
574 | 639 | ds = "Alias: " + str(obj) |
|
575 | 640 | else: |
|
576 | 641 | ds = "Alias to " + str(obj) |
|
577 | 642 | if obj.__doc__: |
|
578 | 643 | ds += "\nDocstring:\n" + obj.__doc__ |
|
579 | 644 | else: |
|
580 | 645 | ds = getdoc(obj) |
|
581 | 646 | if ds is None: |
|
582 | 647 | ds = '<no docstring>' |
|
583 | 648 | if formatter is not None: |
|
584 | 649 | ds = formatter(ds) |
|
585 | 650 | |
|
586 |
# store output in a dict, we |
|
|
587 | # initialize it here and fill it as we go | |
|
588 | out = dict(found=True, isalias=isalias, ismagic=ismagic) | |
|
651 | # store output in a dict, we initialize it here and fill it as we go | |
|
652 | out = dict(name=oname, found=True, isalias=isalias, ismagic=ismagic) | |
|
589 | 653 | |
|
590 | 654 | string_max = 200 # max size of strings to show (snipped if longer) |
|
591 | 655 | shalf = int((string_max -5)/2) |
|
592 | 656 | |
|
593 | 657 | if ismagic: |
|
594 | 658 | obj_type_name = 'Magic function' |
|
595 | 659 | elif isalias: |
|
596 | 660 | obj_type_name = 'System alias' |
|
597 | 661 | else: |
|
598 | 662 | obj_type_name = obj_type.__name__ |
|
599 | 663 | out['type_name'] = obj_type_name |
|
600 | 664 | |
|
601 | 665 | try: |
|
602 | 666 | bclass = obj.__class__ |
|
603 | 667 | out['base_class'] = str(bclass) |
|
604 | 668 | except: pass |
|
605 | 669 | |
|
606 | 670 | # String form, but snip if too long in ? form (full in ??) |
|
607 | 671 | if detail_level >= self.str_detail_level: |
|
608 | 672 | try: |
|
609 | 673 | ostr = str(obj) |
|
610 | 674 | str_head = 'string_form' |
|
611 | 675 | if not detail_level and len(ostr)>string_max: |
|
612 | 676 | ostr = ostr[:shalf] + ' <...> ' + ostr[-shalf:] |
|
613 | 677 | ostr = ("\n" + " " * len(str_head.expandtabs())).\ |
|
614 | 678 | join(map(string.strip,ostr.split("\n"))) |
|
615 | 679 | if ostr.find('\n') > -1: |
|
616 | 680 | # Print multi-line strings starting at the next line. |
|
617 | 681 | str_sep = '\n' |
|
618 | 682 | else: |
|
619 | 683 | str_sep = '\t' |
|
620 | 684 | out[str_head] = ostr |
|
621 | 685 | except: |
|
622 | 686 | pass |
|
623 | 687 | |
|
624 | 688 | if ospace: |
|
625 | 689 | out['namespace'] = ospace |
|
626 | 690 | |
|
627 | 691 | # Length (for strings and lists) |
|
628 | 692 | try: |
|
629 | 693 | out['length'] = str(len(obj)) |
|
630 | 694 | except: pass |
|
631 | 695 | |
|
632 | 696 | # Filename where object was defined |
|
633 | 697 | binary_file = False |
|
634 | 698 | try: |
|
635 | 699 | try: |
|
636 | 700 | fname = inspect.getabsfile(obj) |
|
637 | 701 | except TypeError: |
|
638 | 702 | # For an instance, the file that matters is where its class was |
|
639 | 703 | # declared. |
|
640 | 704 | if hasattr(obj,'__class__'): |
|
641 | 705 | fname = inspect.getabsfile(obj.__class__) |
|
642 | 706 | if fname.endswith('<string>'): |
|
643 | 707 | fname = 'Dynamically generated function. No source code available.' |
|
644 | 708 | if (fname.endswith('.so') or fname.endswith('.dll')): |
|
645 | 709 | binary_file = True |
|
646 | 710 | out['file'] = fname |
|
647 | 711 | except: |
|
648 | 712 | # if anything goes wrong, we don't want to show source, so it's as |
|
649 | 713 | # if the file was binary |
|
650 | 714 | binary_file = True |
|
651 | 715 | |
|
652 | 716 | # reconstruct the function definition and print it: |
|
653 | defln = self._getdef(obj,oname) | |
|
717 | defln = self._getdef(obj, oname) | |
|
654 | 718 | if defln: |
|
655 | 719 | out['definition'] = self.format(defln) |
|
656 | args, varargs, varkw, func_defaults = getargspec(obj) | |
|
657 | out['argspec'] = dict(args=args, varargs=varargs, | |
|
658 | varkw=varkw, func_defaults=func_defaults) | |
|
659 | ||
|
720 | ||
|
660 | 721 | # Docstrings only in detail 0 mode, since source contains them (we |
|
661 | 722 | # avoid repetitions). If source fails, we add them back, see below. |
|
662 | 723 | if ds and detail_level == 0: |
|
663 |
out['docstring'] = |
|
|
724 | out['docstring'] = ds | |
|
664 | 725 | |
|
665 | 726 | # Original source code for any callable |
|
666 | 727 | if detail_level: |
|
667 | 728 | # Flush the source cache because inspect can return out-of-date |
|
668 | 729 | # source |
|
669 | 730 | linecache.checkcache() |
|
670 | 731 | source_success = False |
|
671 | 732 | try: |
|
672 | 733 | try: |
|
673 | 734 | src = getsource(obj,binary_file) |
|
674 | 735 | except TypeError: |
|
675 | 736 | if hasattr(obj,'__class__'): |
|
676 | 737 | src = getsource(obj.__class__,binary_file) |
|
677 | 738 | if src is not None: |
|
678 | 739 | source = self.format(src) |
|
679 | 740 | out['source'] = source.rstrip() |
|
680 | 741 | source_success = True |
|
681 | 742 | except Exception, msg: |
|
682 | 743 | pass |
|
683 | 744 | |
|
684 | 745 | # Constructor docstring for classes |
|
685 | 746 | if inspect.isclass(obj): |
|
686 | 747 | # reconstruct the function definition and print it: |
|
687 | 748 | try: |
|
688 | 749 | obj_init = obj.__init__ |
|
689 | 750 | except AttributeError: |
|
690 | 751 | init_def = init_ds = None |
|
691 | 752 | else: |
|
692 | 753 | init_def = self._getdef(obj_init,oname) |
|
693 | 754 | init_ds = getdoc(obj_init) |
|
694 | 755 | # Skip Python's auto-generated docstrings |
|
695 | 756 | if init_ds and \ |
|
696 | 757 | init_ds.startswith('x.__init__(...) initializes'): |
|
697 | 758 | init_ds = None |
|
698 | 759 | |
|
699 | 760 | if init_def or init_ds: |
|
700 | 761 | if init_def: |
|
701 | 762 | out['init_definition'] = self.format(init_def) |
|
702 | 763 | if init_ds: |
|
703 |
out['init_docstring'] = |
|
|
764 | out['init_docstring'] = init_ds | |
|
765 | ||
|
704 | 766 | # and class docstring for instances: |
|
705 | 767 | elif obj_type is types.InstanceType or \ |
|
706 | isinstance(obj,object): | |
|
707 | ||
|
768 | isinstance(obj, object): | |
|
708 | 769 | # First, check whether the instance docstring is identical to the |
|
709 | 770 | # class one, and print it separately if they don't coincide. In |
|
710 | 771 | # most cases they will, but it's nice to print all the info for |
|
711 | 772 | # objects which use instance-customized docstrings. |
|
712 | 773 | if ds: |
|
713 | 774 | try: |
|
714 | 775 | cls = getattr(obj,'__class__') |
|
715 | 776 | except: |
|
716 | 777 | class_ds = None |
|
717 | 778 | else: |
|
718 | 779 | class_ds = getdoc(cls) |
|
719 | 780 | # Skip Python's auto-generated docstrings |
|
720 | 781 | if class_ds and \ |
|
721 | 782 | (class_ds.startswith('function(code, globals[,') or \ |
|
722 | 783 | class_ds.startswith('instancemethod(function, instance,') or \ |
|
723 | 784 | class_ds.startswith('module(name[,') ): |
|
724 | 785 | class_ds = None |
|
725 | 786 | if class_ds and ds != class_ds: |
|
726 |
out['class_docstring'] = |
|
|
787 | out['class_docstring'] = class_ds | |
|
727 | 788 | |
|
728 | 789 | # Next, try to show constructor docstrings |
|
729 | 790 | try: |
|
730 | 791 | init_ds = getdoc(obj.__init__) |
|
731 | 792 | # Skip Python's auto-generated docstrings |
|
732 | 793 | if init_ds and \ |
|
733 | 794 | init_ds.startswith('x.__init__(...) initializes'): |
|
734 | 795 | init_ds = None |
|
735 | 796 | except AttributeError: |
|
736 | 797 | init_ds = None |
|
737 | 798 | if init_ds: |
|
738 |
out['init_docstring'] = |
|
|
799 | out['init_docstring'] = init_ds | |
|
739 | 800 | |
|
740 | 801 | # Call form docstring for callable instances |
|
741 | if hasattr(obj,'__call__'): | |
|
742 | call_def = self._getdef(obj.__call__,oname) | |
|
802 | if hasattr(obj, '__call__'): | |
|
803 | call_def = self._getdef(obj.__call__, oname) | |
|
743 | 804 | if call_def is not None: |
|
744 | 805 | out['call_def'] = self.format(call_def) |
|
745 | 806 | call_ds = getdoc(obj.__call__) |
|
746 | 807 | # Skip Python's auto-generated docstrings |
|
747 | 808 | if call_ds and call_ds.startswith('x.__call__(...) <==> x(...)'): |
|
748 | 809 | call_ds = None |
|
749 | 810 | if call_ds: |
|
750 |
out['call_docstring'] = |
|
|
811 | out['call_docstring'] = call_ds | |
|
812 | ||
|
813 | # Compute the object's argspec as a callable. The key is to decide | |
|
814 | # whether to pull it from the object itself, from its __init__ or | |
|
815 | # from its __call__ method. | |
|
816 | ||
|
817 | if inspect.isclass(obj): | |
|
818 | callable_obj = obj.__init__ | |
|
819 | elif callable(obj): | |
|
820 | callable_obj = obj | |
|
821 | else: | |
|
822 | callable_obj = None | |
|
823 | ||
|
824 | if callable_obj: | |
|
825 | try: | |
|
826 | args, varargs, varkw, defaults = getargspec(callable_obj) | |
|
827 | except (TypeError, AttributeError): | |
|
828 | # For extensions/builtins we can't retrieve the argspec | |
|
829 | pass | |
|
830 | else: | |
|
831 | out['argspec'] = dict(args=args, varargs=varargs, | |
|
832 | varkw=varkw, defaults=defaults) | |
|
751 | 833 | |
|
752 |
return |
|
|
834 | return object_info(**out) | |
|
753 | 835 | |
|
754 | 836 | |
|
755 | 837 | def psearch(self,pattern,ns_table,ns_search=[], |
|
756 | 838 | ignore_case=False,show_all=False): |
|
757 | 839 | """Search namespaces with wildcards for objects. |
|
758 | 840 | |
|
759 | 841 | Arguments: |
|
760 | 842 | |
|
761 | 843 | - pattern: string containing shell-like wildcards to use in namespace |
|
762 | 844 | searches and optionally a type specification to narrow the search to |
|
763 | 845 | objects of that type. |
|
764 | 846 | |
|
765 | 847 | - ns_table: dict of name->namespaces for search. |
|
766 | 848 | |
|
767 | 849 | Optional arguments: |
|
768 | 850 | |
|
769 | 851 | - ns_search: list of namespace names to include in search. |
|
770 | 852 | |
|
771 | 853 | - ignore_case(False): make the search case-insensitive. |
|
772 | 854 | |
|
773 | 855 | - show_all(False): show all names, including those starting with |
|
774 | 856 | underscores. |
|
775 | 857 | """ |
|
776 | 858 | #print 'ps pattern:<%r>' % pattern # dbg |
|
777 | 859 | |
|
778 | 860 | # defaults |
|
779 | 861 | type_pattern = 'all' |
|
780 | 862 | filter = '' |
|
781 | 863 | |
|
782 | 864 | cmds = pattern.split() |
|
783 | 865 | len_cmds = len(cmds) |
|
784 | 866 | if len_cmds == 1: |
|
785 | 867 | # Only filter pattern given |
|
786 | 868 | filter = cmds[0] |
|
787 | 869 | elif len_cmds == 2: |
|
788 | 870 | # Both filter and type specified |
|
789 | 871 | filter,type_pattern = cmds |
|
790 | 872 | else: |
|
791 | 873 | raise ValueError('invalid argument string for psearch: <%s>' % |
|
792 | 874 | pattern) |
|
793 | 875 | |
|
794 | 876 | # filter search namespaces |
|
795 | 877 | for name in ns_search: |
|
796 | 878 | if name not in ns_table: |
|
797 | 879 | raise ValueError('invalid namespace <%s>. Valid names: %s' % |
|
798 | 880 | (name,ns_table.keys())) |
|
799 | 881 | |
|
800 | 882 | #print 'type_pattern:',type_pattern # dbg |
|
801 | 883 | search_result = [] |
|
802 | 884 | for ns_name in ns_search: |
|
803 | 885 | ns = ns_table[ns_name] |
|
804 | 886 | tmp_res = list(list_namespace(ns,type_pattern,filter, |
|
805 | 887 | ignore_case=ignore_case, |
|
806 | 888 | show_all=show_all)) |
|
807 | 889 | search_result.extend(tmp_res) |
|
808 | 890 | search_result.sort() |
|
809 | 891 | |
|
810 | 892 | page.page('\n'.join(search_result)) |
@@ -1,220 +1,225 b'' | |||
|
1 | 1 | # Standard library imports |
|
2 | 2 | import re |
|
3 | 3 | from textwrap import dedent |
|
4 | 4 | |
|
5 | 5 | # System library imports |
|
6 | 6 | from PyQt4 import QtCore, QtGui |
|
7 | 7 | |
|
8 | 8 | |
|
9 | 9 | class CallTipWidget(QtGui.QLabel): |
|
10 | 10 | """ Shows call tips by parsing the current text of Q[Plain]TextEdit. |
|
11 | 11 | """ |
|
12 | 12 | |
|
13 | 13 | #-------------------------------------------------------------------------- |
|
14 | 14 | # 'QObject' interface |
|
15 | 15 | #-------------------------------------------------------------------------- |
|
16 | 16 | |
|
17 | 17 | def __init__(self, text_edit): |
|
18 | 18 | """ Create a call tip manager that is attached to the specified Qt |
|
19 | 19 | text edit widget. |
|
20 | 20 | """ |
|
21 | 21 | assert isinstance(text_edit, (QtGui.QTextEdit, QtGui.QPlainTextEdit)) |
|
22 | 22 | super(CallTipWidget, self).__init__(None, QtCore.Qt.ToolTip) |
|
23 | 23 | |
|
24 | 24 | self._hide_timer = QtCore.QBasicTimer() |
|
25 | 25 | self._text_edit = text_edit |
|
26 | 26 | |
|
27 | 27 | self.setFont(text_edit.document().defaultFont()) |
|
28 | 28 | self.setForegroundRole(QtGui.QPalette.ToolTipText) |
|
29 | 29 | self.setBackgroundRole(QtGui.QPalette.ToolTipBase) |
|
30 | 30 | self.setPalette(QtGui.QToolTip.palette()) |
|
31 | 31 | |
|
32 | 32 | self.setAlignment(QtCore.Qt.AlignLeft) |
|
33 | 33 | self.setIndent(1) |
|
34 | 34 | self.setFrameStyle(QtGui.QFrame.NoFrame) |
|
35 | 35 | self.setMargin(1 + self.style().pixelMetric( |
|
36 | 36 | QtGui.QStyle.PM_ToolTipLabelFrameWidth, None, self)) |
|
37 | 37 | self.setWindowOpacity(self.style().styleHint( |
|
38 | 38 | QtGui.QStyle.SH_ToolTipLabel_Opacity, None, self) / 255.0) |
|
39 | 39 | |
|
40 | 40 | def eventFilter(self, obj, event): |
|
41 | 41 | """ Reimplemented to hide on certain key presses and on text edit focus |
|
42 | 42 | changes. |
|
43 | 43 | """ |
|
44 | 44 | if obj == self._text_edit: |
|
45 | 45 | etype = event.type() |
|
46 | 46 | |
|
47 | 47 | if etype == QtCore.QEvent.KeyPress: |
|
48 | 48 | key = event.key() |
|
49 | 49 | if key in (QtCore.Qt.Key_Enter, QtCore.Qt.Key_Return): |
|
50 | 50 | self.hide() |
|
51 | 51 | elif key == QtCore.Qt.Key_Escape: |
|
52 | 52 | self.hide() |
|
53 | 53 | return True |
|
54 | 54 | |
|
55 | 55 | elif etype == QtCore.QEvent.FocusOut: |
|
56 | 56 | self.hide() |
|
57 | 57 | |
|
58 | 58 | elif etype == QtCore.QEvent.Enter: |
|
59 | 59 | self._hide_timer.stop() |
|
60 | 60 | |
|
61 | 61 | elif etype == QtCore.QEvent.Leave: |
|
62 | 62 | self._hide_later() |
|
63 | 63 | |
|
64 | 64 | return super(CallTipWidget, self).eventFilter(obj, event) |
|
65 | 65 | |
|
66 | 66 | def timerEvent(self, event): |
|
67 | 67 | """ Reimplemented to hide the widget when the hide timer fires. |
|
68 | 68 | """ |
|
69 | 69 | if event.timerId() == self._hide_timer.timerId(): |
|
70 | 70 | self._hide_timer.stop() |
|
71 | 71 | self.hide() |
|
72 | 72 | |
|
73 | 73 | #-------------------------------------------------------------------------- |
|
74 | 74 | # 'QWidget' interface |
|
75 | 75 | #-------------------------------------------------------------------------- |
|
76 | 76 | |
|
77 | 77 | def enterEvent(self, event): |
|
78 | 78 | """ Reimplemented to cancel the hide timer. |
|
79 | 79 | """ |
|
80 | 80 | super(CallTipWidget, self).enterEvent(event) |
|
81 | 81 | self._hide_timer.stop() |
|
82 | 82 | |
|
83 | 83 | def hideEvent(self, event): |
|
84 | 84 | """ Reimplemented to disconnect signal handlers and event filter. |
|
85 | 85 | """ |
|
86 | 86 | super(CallTipWidget, self).hideEvent(event) |
|
87 | 87 | self._text_edit.cursorPositionChanged.disconnect( |
|
88 | 88 | self._cursor_position_changed) |
|
89 | 89 | self._text_edit.removeEventFilter(self) |
|
90 | 90 | |
|
91 | 91 | def leaveEvent(self, event): |
|
92 | 92 | """ Reimplemented to start the hide timer. |
|
93 | 93 | """ |
|
94 | 94 | super(CallTipWidget, self).leaveEvent(event) |
|
95 | 95 | self._hide_later() |
|
96 | 96 | |
|
97 | 97 | def paintEvent(self, event): |
|
98 | 98 | """ Reimplemented to paint the background panel. |
|
99 | 99 | """ |
|
100 | 100 | painter = QtGui.QStylePainter(self) |
|
101 | 101 | option = QtGui.QStyleOptionFrame() |
|
102 | 102 | option.init(self) |
|
103 | 103 | painter.drawPrimitive(QtGui.QStyle.PE_PanelTipLabel, option) |
|
104 | 104 | painter.end() |
|
105 | 105 | |
|
106 | 106 | super(CallTipWidget, self).paintEvent(event) |
|
107 | 107 | |
|
108 | 108 | def setFont(self, font): |
|
109 | 109 | """ Reimplemented to allow use of this method as a slot. |
|
110 | 110 | """ |
|
111 | 111 | super(CallTipWidget, self).setFont(font) |
|
112 | 112 | |
|
113 | 113 | def showEvent(self, event): |
|
114 | 114 | """ Reimplemented to connect signal handlers and event filter. |
|
115 | 115 | """ |
|
116 | 116 | super(CallTipWidget, self).showEvent(event) |
|
117 | 117 | self._text_edit.cursorPositionChanged.connect( |
|
118 | 118 | self._cursor_position_changed) |
|
119 | 119 | self._text_edit.installEventFilter(self) |
|
120 | 120 | |
|
121 | 121 | #-------------------------------------------------------------------------- |
|
122 | 122 | # 'CallTipWidget' interface |
|
123 | 123 | #-------------------------------------------------------------------------- |
|
124 | 124 | |
|
125 |
def show_ |
|
|
126 |
""" Attempts to show the specified docstring at the |
|
|
127 |
location. The docstring is |
|
|
125 | def show_call_info(self, call_line=None, doc=None, maxlines=20): | |
|
126 | """ Attempts to show the specified call line and docstring at the | |
|
127 | current cursor location. The docstring is possibly truncated for | |
|
128 | 128 | length. |
|
129 | 129 | """ |
|
130 | doc = dedent(doc.rstrip()).lstrip() | |
|
131 | match = re.match("(?:[^\n]*\n){%i}" % maxlines, doc) | |
|
132 | if match: | |
|
133 | doc = doc[:match.end()] + '\n[Documentation continues...]' | |
|
130 | if doc: | |
|
131 | match = re.match("(?:[^\n]*\n){%i}" % maxlines, doc) | |
|
132 | if match: | |
|
133 | doc = doc[:match.end()] + '\n[Documentation continues...]' | |
|
134 | else: | |
|
135 | doc = '' | |
|
136 | ||
|
137 | if call_line: | |
|
138 | doc = '\n\n'.join([call_line, doc]) | |
|
134 | 139 | return self.show_tip(doc) |
|
135 | 140 | |
|
136 | 141 | def show_tip(self, tip): |
|
137 | 142 | """ Attempts to show the specified tip at the current cursor location. |
|
138 | 143 | """ |
|
139 | 144 | # Attempt to find the cursor position at which to show the call tip. |
|
140 | 145 | text_edit = self._text_edit |
|
141 | 146 | document = text_edit.document() |
|
142 | 147 | cursor = text_edit.textCursor() |
|
143 | 148 | search_pos = cursor.position() - 1 |
|
144 | 149 | self._start_position, _ = self._find_parenthesis(search_pos, |
|
145 | 150 | forward=False) |
|
146 | 151 | if self._start_position == -1: |
|
147 | 152 | return False |
|
148 | 153 | |
|
149 | 154 | # Set the text and resize the widget accordingly. |
|
150 | 155 | self.setText(tip) |
|
151 | 156 | self.resize(self.sizeHint()) |
|
152 | 157 | |
|
153 | 158 | # Locate and show the widget. Place the tip below the current line |
|
154 | 159 | # unless it would be off the screen. In that case, place it above |
|
155 | 160 | # the current line. |
|
156 | 161 | padding = 3 # Distance in pixels between cursor bounds and tip box. |
|
157 | 162 | cursor_rect = text_edit.cursorRect(cursor) |
|
158 | 163 | screen_rect = QtGui.qApp.desktop().screenGeometry(text_edit) |
|
159 | 164 | point = text_edit.mapToGlobal(cursor_rect.bottomRight()) |
|
160 | 165 | point.setY(point.y() + padding) |
|
161 | 166 | tip_height = self.size().height() |
|
162 | 167 | if point.y() + tip_height > screen_rect.height(): |
|
163 | 168 | point = text_edit.mapToGlobal(cursor_rect.topRight()) |
|
164 | 169 | point.setY(point.y() - tip_height - padding) |
|
165 | 170 | self.move(point) |
|
166 | 171 | self.show() |
|
167 | 172 | return True |
|
168 | 173 | |
|
169 | 174 | #-------------------------------------------------------------------------- |
|
170 | 175 | # Protected interface |
|
171 | 176 | #-------------------------------------------------------------------------- |
|
172 | 177 | |
|
173 | 178 | def _find_parenthesis(self, position, forward=True): |
|
174 | 179 | """ If 'forward' is True (resp. False), proceed forwards |
|
175 | 180 | (resp. backwards) through the line that contains 'position' until an |
|
176 | 181 | unmatched closing (resp. opening) parenthesis is found. Returns a |
|
177 | 182 | tuple containing the position of this parenthesis (or -1 if it is |
|
178 | 183 | not found) and the number commas (at depth 0) found along the way. |
|
179 | 184 | """ |
|
180 | 185 | commas = depth = 0 |
|
181 | 186 | document = self._text_edit.document() |
|
182 | 187 | qchar = document.characterAt(position) |
|
183 | 188 | while (position > 0 and qchar.isPrint() and |
|
184 | 189 | # Need to check explicitly for line/paragraph separators: |
|
185 | 190 | qchar.unicode() not in (0x2028, 0x2029)): |
|
186 | 191 | char = qchar.toAscii() |
|
187 | 192 | if char == ',' and depth == 0: |
|
188 | 193 | commas += 1 |
|
189 | 194 | elif char == ')': |
|
190 | 195 | if forward and depth == 0: |
|
191 | 196 | break |
|
192 | 197 | depth += 1 |
|
193 | 198 | elif char == '(': |
|
194 | 199 | if not forward and depth == 0: |
|
195 | 200 | break |
|
196 | 201 | depth -= 1 |
|
197 | 202 | position += 1 if forward else -1 |
|
198 | 203 | qchar = document.characterAt(position) |
|
199 | 204 | else: |
|
200 | 205 | position = -1 |
|
201 | 206 | return position, commas |
|
202 | 207 | |
|
203 | 208 | def _hide_later(self): |
|
204 | 209 | """ Hides the tooltip after some time has passed. |
|
205 | 210 | """ |
|
206 | 211 | if not self._hide_timer.isActive(): |
|
207 | 212 | self._hide_timer.start(300, self) |
|
208 | 213 | |
|
209 | 214 | #------ Signal handlers ---------------------------------------------------- |
|
210 | 215 | |
|
211 | 216 | def _cursor_position_changed(self): |
|
212 | 217 | """ Updates the tip based on user cursor movement. |
|
213 | 218 | """ |
|
214 | 219 | cursor = self._text_edit.textCursor() |
|
215 | 220 | if cursor.position() <= self._start_position: |
|
216 | 221 | self.hide() |
|
217 | 222 | else: |
|
218 | 223 | position, commas = self._find_parenthesis(self._start_position + 1) |
|
219 | 224 | if position != -1: |
|
220 | 225 | self.hide() |
@@ -1,549 +1,554 b'' | |||
|
1 | 1 | from __future__ import print_function |
|
2 | 2 | |
|
3 | 3 | # Standard library imports |
|
4 | 4 | from collections import namedtuple |
|
5 | 5 | import sys |
|
6 | 6 | |
|
7 | 7 | # System library imports |
|
8 | 8 | from pygments.lexers import PythonLexer |
|
9 | 9 | from PyQt4 import QtCore, QtGui |
|
10 | 10 | |
|
11 | 11 | # Local imports |
|
12 | 12 | from IPython.core.inputsplitter import InputSplitter, transform_classic_prompt |
|
13 | from IPython.core.oinspect import call_tip | |
|
13 | 14 | from IPython.frontend.qt.base_frontend_mixin import BaseFrontendMixin |
|
14 | 15 | from IPython.utils.traitlets import Bool |
|
15 | 16 | from bracket_matcher import BracketMatcher |
|
16 | 17 | from call_tip_widget import CallTipWidget |
|
17 | 18 | from completion_lexer import CompletionLexer |
|
18 | 19 | from history_console_widget import HistoryConsoleWidget |
|
19 | 20 | from pygments_highlighter import PygmentsHighlighter |
|
20 | 21 | |
|
21 | 22 | |
|
22 | 23 | class FrontendHighlighter(PygmentsHighlighter): |
|
23 | 24 | """ A PygmentsHighlighter that can be turned on and off and that ignores |
|
24 | 25 | prompts. |
|
25 | 26 | """ |
|
26 | 27 | |
|
27 | 28 | def __init__(self, frontend): |
|
28 | 29 | super(FrontendHighlighter, self).__init__(frontend._control.document()) |
|
29 | 30 | self._current_offset = 0 |
|
30 | 31 | self._frontend = frontend |
|
31 | 32 | self.highlighting_on = False |
|
32 | 33 | |
|
33 | 34 | def highlightBlock(self, qstring): |
|
34 | 35 | """ Highlight a block of text. Reimplemented to highlight selectively. |
|
35 | 36 | """ |
|
36 | 37 | if not self.highlighting_on: |
|
37 | 38 | return |
|
38 | 39 | |
|
39 | 40 | # The input to this function is unicode string that may contain |
|
40 | 41 | # paragraph break characters, non-breaking spaces, etc. Here we acquire |
|
41 | 42 | # the string as plain text so we can compare it. |
|
42 | 43 | current_block = self.currentBlock() |
|
43 | 44 | string = self._frontend._get_block_plain_text(current_block) |
|
44 | 45 | |
|
45 | 46 | # Decide whether to check for the regular or continuation prompt. |
|
46 | 47 | if current_block.contains(self._frontend._prompt_pos): |
|
47 | 48 | prompt = self._frontend._prompt |
|
48 | 49 | else: |
|
49 | 50 | prompt = self._frontend._continuation_prompt |
|
50 | 51 | |
|
51 | 52 | # Don't highlight the part of the string that contains the prompt. |
|
52 | 53 | if string.startswith(prompt): |
|
53 | 54 | self._current_offset = len(prompt) |
|
54 | 55 | qstring.remove(0, len(prompt)) |
|
55 | 56 | else: |
|
56 | 57 | self._current_offset = 0 |
|
57 | 58 | |
|
58 | 59 | PygmentsHighlighter.highlightBlock(self, qstring) |
|
59 | 60 | |
|
60 | 61 | def rehighlightBlock(self, block): |
|
61 | 62 | """ Reimplemented to temporarily enable highlighting if disabled. |
|
62 | 63 | """ |
|
63 | 64 | old = self.highlighting_on |
|
64 | 65 | self.highlighting_on = True |
|
65 | 66 | super(FrontendHighlighter, self).rehighlightBlock(block) |
|
66 | 67 | self.highlighting_on = old |
|
67 | 68 | |
|
68 | 69 | def setFormat(self, start, count, format): |
|
69 | 70 | """ Reimplemented to highlight selectively. |
|
70 | 71 | """ |
|
71 | 72 | start += self._current_offset |
|
72 | 73 | PygmentsHighlighter.setFormat(self, start, count, format) |
|
73 | 74 | |
|
74 | 75 | |
|
75 | 76 | class FrontendWidget(HistoryConsoleWidget, BaseFrontendMixin): |
|
76 | 77 | """ A Qt frontend for a generic Python kernel. |
|
77 | 78 | """ |
|
78 | 79 | |
|
79 | 80 | # An option and corresponding signal for overriding the default kernel |
|
80 | 81 | # interrupt behavior. |
|
81 | 82 | custom_interrupt = Bool(False) |
|
82 | 83 | custom_interrupt_requested = QtCore.pyqtSignal() |
|
83 | 84 | |
|
84 | 85 | # An option and corresponding signals for overriding the default kernel |
|
85 | 86 | # restart behavior. |
|
86 | 87 | custom_restart = Bool(False) |
|
87 | 88 | custom_restart_kernel_died = QtCore.pyqtSignal(float) |
|
88 | 89 | custom_restart_requested = QtCore.pyqtSignal() |
|
89 | 90 | |
|
90 | 91 | # Emitted when an 'execute_reply' has been received from the kernel and |
|
91 | 92 | # processed by the FrontendWidget. |
|
92 | 93 | executed = QtCore.pyqtSignal(object) |
|
93 | 94 | |
|
94 | 95 | # Emitted when an exit request has been received from the kernel. |
|
95 | 96 | exit_requested = QtCore.pyqtSignal() |
|
96 | 97 | |
|
97 | 98 | # Protected class variables. |
|
98 | 99 | _CallTipRequest = namedtuple('_CallTipRequest', ['id', 'pos']) |
|
99 | 100 | _CompletionRequest = namedtuple('_CompletionRequest', ['id', 'pos']) |
|
100 | 101 | _ExecutionRequest = namedtuple('_ExecutionRequest', ['id', 'kind']) |
|
101 | 102 | _input_splitter_class = InputSplitter |
|
102 | 103 | |
|
103 | 104 | #--------------------------------------------------------------------------- |
|
104 | 105 | # 'object' interface |
|
105 | 106 | #--------------------------------------------------------------------------- |
|
106 | 107 | |
|
107 | 108 | def __init__(self, *args, **kw): |
|
108 | 109 | super(FrontendWidget, self).__init__(*args, **kw) |
|
109 | 110 | |
|
110 | 111 | # FrontendWidget protected variables. |
|
111 | 112 | self._bracket_matcher = BracketMatcher(self._control) |
|
112 | 113 | self._call_tip_widget = CallTipWidget(self._control) |
|
113 | 114 | self._completion_lexer = CompletionLexer(PythonLexer()) |
|
114 | 115 | self._copy_raw_action = QtGui.QAction('Copy (Raw Text)', None) |
|
115 | 116 | self._hidden = False |
|
116 | 117 | self._highlighter = FrontendHighlighter(self) |
|
117 | 118 | self._input_splitter = self._input_splitter_class(input_mode='cell') |
|
118 | 119 | self._kernel_manager = None |
|
119 | 120 | self._request_info = {} |
|
120 | 121 | |
|
121 | 122 | # Configure the ConsoleWidget. |
|
122 | 123 | self.tab_width = 4 |
|
123 | 124 | self._set_continuation_prompt('... ') |
|
124 | 125 | |
|
125 | 126 | # Configure the CallTipWidget. |
|
126 | 127 | self._call_tip_widget.setFont(self.font) |
|
127 | 128 | self.font_changed.connect(self._call_tip_widget.setFont) |
|
128 | 129 | |
|
129 | 130 | # Configure actions. |
|
130 | 131 | action = self._copy_raw_action |
|
131 | 132 | key = QtCore.Qt.CTRL | QtCore.Qt.SHIFT | QtCore.Qt.Key_C |
|
132 | 133 | action.setEnabled(False) |
|
133 | 134 | action.setShortcut(QtGui.QKeySequence(key)) |
|
134 | 135 | action.setShortcutContext(QtCore.Qt.WidgetWithChildrenShortcut) |
|
135 | 136 | action.triggered.connect(self.copy_raw) |
|
136 | 137 | self.copy_available.connect(action.setEnabled) |
|
137 | 138 | self.addAction(action) |
|
138 | 139 | |
|
139 | 140 | # Connect signal handlers. |
|
140 | 141 | document = self._control.document() |
|
141 | 142 | document.contentsChange.connect(self._document_contents_change) |
|
142 | 143 | |
|
143 | 144 | #--------------------------------------------------------------------------- |
|
144 | 145 | # 'ConsoleWidget' public interface |
|
145 | 146 | #--------------------------------------------------------------------------- |
|
146 | 147 | |
|
147 | 148 | def copy(self): |
|
148 | 149 | """ Copy the currently selected text to the clipboard, removing prompts. |
|
149 | 150 | """ |
|
150 | 151 | text = unicode(self._control.textCursor().selection().toPlainText()) |
|
151 | 152 | if text: |
|
152 | 153 | lines = map(transform_classic_prompt, text.splitlines()) |
|
153 | 154 | text = '\n'.join(lines) |
|
154 | 155 | QtGui.QApplication.clipboard().setText(text) |
|
155 | 156 | |
|
156 | 157 | #--------------------------------------------------------------------------- |
|
157 | 158 | # 'ConsoleWidget' abstract interface |
|
158 | 159 | #--------------------------------------------------------------------------- |
|
159 | 160 | |
|
160 | 161 | def _is_complete(self, source, interactive): |
|
161 | 162 | """ Returns whether 'source' can be completely processed and a new |
|
162 | 163 | prompt created. When triggered by an Enter/Return key press, |
|
163 | 164 | 'interactive' is True; otherwise, it is False. |
|
164 | 165 | """ |
|
165 | 166 | complete = self._input_splitter.push(source) |
|
166 | 167 | if interactive: |
|
167 | 168 | complete = not self._input_splitter.push_accepts_more() |
|
168 | 169 | return complete |
|
169 | 170 | |
|
170 | 171 | def _execute(self, source, hidden): |
|
171 | 172 | """ Execute 'source'. If 'hidden', do not show any output. |
|
172 | 173 | |
|
173 | 174 | See parent class :meth:`execute` docstring for full details. |
|
174 | 175 | """ |
|
175 | 176 | msg_id = self.kernel_manager.xreq_channel.execute(source, hidden) |
|
176 | 177 | self._request_info['execute'] = self._ExecutionRequest(msg_id, 'user') |
|
177 | 178 | self._hidden = hidden |
|
178 | 179 | |
|
179 | 180 | def _prompt_started_hook(self): |
|
180 | 181 | """ Called immediately after a new prompt is displayed. |
|
181 | 182 | """ |
|
182 | 183 | if not self._reading: |
|
183 | 184 | self._highlighter.highlighting_on = True |
|
184 | 185 | |
|
185 | 186 | def _prompt_finished_hook(self): |
|
186 | 187 | """ Called immediately after a prompt is finished, i.e. when some input |
|
187 | 188 | will be processed and a new prompt displayed. |
|
188 | 189 | """ |
|
189 | 190 | if not self._reading: |
|
190 | 191 | self._highlighter.highlighting_on = False |
|
191 | 192 | |
|
192 | 193 | def _tab_pressed(self): |
|
193 | 194 | """ Called when the tab key is pressed. Returns whether to continue |
|
194 | 195 | processing the event. |
|
195 | 196 | """ |
|
196 | 197 | # Perform tab completion if: |
|
197 | 198 | # 1) The cursor is in the input buffer. |
|
198 | 199 | # 2) There is a non-whitespace character before the cursor. |
|
199 | 200 | text = self._get_input_buffer_cursor_line() |
|
200 | 201 | if text is None: |
|
201 | 202 | return False |
|
202 | 203 | complete = bool(text[:self._get_input_buffer_cursor_column()].strip()) |
|
203 | 204 | if complete: |
|
204 | 205 | self._complete() |
|
205 | 206 | return not complete |
|
206 | 207 | |
|
207 | 208 | #--------------------------------------------------------------------------- |
|
208 | 209 | # 'ConsoleWidget' protected interface |
|
209 | 210 | #--------------------------------------------------------------------------- |
|
210 | 211 | |
|
211 | 212 | def _context_menu_make(self, pos): |
|
212 | 213 | """ Reimplemented to add an action for raw copy. |
|
213 | 214 | """ |
|
214 | 215 | menu = super(FrontendWidget, self)._context_menu_make(pos) |
|
215 | 216 | for before_action in menu.actions(): |
|
216 | 217 | if before_action.shortcut().matches(QtGui.QKeySequence.Paste) == \ |
|
217 | 218 | QtGui.QKeySequence.ExactMatch: |
|
218 | 219 | menu.insertAction(before_action, self._copy_raw_action) |
|
219 | 220 | break |
|
220 | 221 | return menu |
|
221 | 222 | |
|
222 | 223 | def _event_filter_console_keypress(self, event): |
|
223 | 224 | """ Reimplemented for execution interruption and smart backspace. |
|
224 | 225 | """ |
|
225 | 226 | key = event.key() |
|
226 | 227 | if self._control_key_down(event.modifiers(), include_command=False): |
|
227 | 228 | |
|
228 | 229 | if key == QtCore.Qt.Key_C and self._executing: |
|
229 | 230 | self.interrupt_kernel() |
|
230 | 231 | return True |
|
231 | 232 | |
|
232 | 233 | elif key == QtCore.Qt.Key_Period: |
|
233 | 234 | message = 'Are you sure you want to restart the kernel?' |
|
234 | 235 | self.restart_kernel(message, now=False) |
|
235 | 236 | return True |
|
236 | 237 | |
|
237 | 238 | elif not event.modifiers() & QtCore.Qt.AltModifier: |
|
238 | 239 | |
|
239 | 240 | # Smart backspace: remove four characters in one backspace if: |
|
240 | 241 | # 1) everything left of the cursor is whitespace |
|
241 | 242 | # 2) the four characters immediately left of the cursor are spaces |
|
242 | 243 | if key == QtCore.Qt.Key_Backspace: |
|
243 | 244 | col = self._get_input_buffer_cursor_column() |
|
244 | 245 | cursor = self._control.textCursor() |
|
245 | 246 | if col > 3 and not cursor.hasSelection(): |
|
246 | 247 | text = self._get_input_buffer_cursor_line()[:col] |
|
247 | 248 | if text.endswith(' ') and not text.strip(): |
|
248 | 249 | cursor.movePosition(QtGui.QTextCursor.Left, |
|
249 | 250 | QtGui.QTextCursor.KeepAnchor, 4) |
|
250 | 251 | cursor.removeSelectedText() |
|
251 | 252 | return True |
|
252 | 253 | |
|
253 | 254 | return super(FrontendWidget, self)._event_filter_console_keypress(event) |
|
254 | 255 | |
|
255 | 256 | def _insert_continuation_prompt(self, cursor): |
|
256 | 257 | """ Reimplemented for auto-indentation. |
|
257 | 258 | """ |
|
258 | 259 | super(FrontendWidget, self)._insert_continuation_prompt(cursor) |
|
259 | 260 | cursor.insertText(' ' * self._input_splitter.indent_spaces) |
|
260 | 261 | |
|
261 | 262 | #--------------------------------------------------------------------------- |
|
262 | 263 | # 'BaseFrontendMixin' abstract interface |
|
263 | 264 | #--------------------------------------------------------------------------- |
|
264 | 265 | |
|
265 | 266 | def _handle_complete_reply(self, rep): |
|
266 | 267 | """ Handle replies for tab completion. |
|
267 | 268 | """ |
|
268 | 269 | cursor = self._get_cursor() |
|
269 | 270 | info = self._request_info.get('complete') |
|
270 | 271 | if info and info.id == rep['parent_header']['msg_id'] and \ |
|
271 | 272 | info.pos == cursor.position(): |
|
272 | 273 | text = '.'.join(self._get_context()) |
|
273 | 274 | cursor.movePosition(QtGui.QTextCursor.Left, n=len(text)) |
|
274 | 275 | self._complete_with_items(cursor, rep['content']['matches']) |
|
275 | 276 | |
|
276 | 277 | def _handle_execute_reply(self, msg): |
|
277 | 278 | """ Handles replies for code execution. |
|
278 | 279 | """ |
|
279 | 280 | info = self._request_info.get('execute') |
|
280 | 281 | if info and info.id == msg['parent_header']['msg_id'] and \ |
|
281 | 282 | info.kind == 'user' and not self._hidden: |
|
282 | 283 | # Make sure that all output from the SUB channel has been processed |
|
283 | 284 | # before writing a new prompt. |
|
284 | 285 | self.kernel_manager.sub_channel.flush() |
|
285 | 286 | |
|
286 | 287 | # Reset the ANSI style information to prevent bad text in stdout |
|
287 | 288 | # from messing up our colors. We're not a true terminal so we're |
|
288 | 289 | # allowed to do this. |
|
289 | 290 | if self.ansi_codes: |
|
290 | 291 | self._ansi_processor.reset_sgr() |
|
291 | 292 | |
|
292 | 293 | content = msg['content'] |
|
293 | 294 | status = content['status'] |
|
294 | 295 | if status == 'ok': |
|
295 | 296 | self._process_execute_ok(msg) |
|
296 | 297 | elif status == 'error': |
|
297 | 298 | self._process_execute_error(msg) |
|
298 | 299 | elif status == 'abort': |
|
299 | 300 | self._process_execute_abort(msg) |
|
300 | 301 | |
|
301 | 302 | self._show_interpreter_prompt_for_reply(msg) |
|
302 | 303 | self.executed.emit(msg) |
|
303 | 304 | |
|
304 | 305 | def _handle_input_request(self, msg): |
|
305 | 306 | """ Handle requests for raw_input. |
|
306 | 307 | """ |
|
307 | 308 | if self._hidden: |
|
308 | 309 | raise RuntimeError('Request for raw input during hidden execution.') |
|
309 | 310 | |
|
310 | 311 | # Make sure that all output from the SUB channel has been processed |
|
311 | 312 | # before entering readline mode. |
|
312 | 313 | self.kernel_manager.sub_channel.flush() |
|
313 | 314 | |
|
314 | 315 | def callback(line): |
|
315 | 316 | self.kernel_manager.rep_channel.input(line) |
|
316 | 317 | self._readline(msg['content']['prompt'], callback=callback) |
|
317 | 318 | |
|
318 | 319 | def _handle_kernel_died(self, since_last_heartbeat): |
|
319 | 320 | """ Handle the kernel's death by asking if the user wants to restart. |
|
320 | 321 | """ |
|
321 | 322 | if self.custom_restart: |
|
322 | 323 | self.custom_restart_kernel_died.emit(since_last_heartbeat) |
|
323 | 324 | else: |
|
324 | 325 | message = 'The kernel heartbeat has been inactive for %.2f ' \ |
|
325 | 326 | 'seconds. Do you want to restart the kernel? You may ' \ |
|
326 | 327 | 'first want to check the network connection.' % \ |
|
327 | 328 | since_last_heartbeat |
|
328 | 329 | self.restart_kernel(message, now=True) |
|
329 | 330 | |
|
330 | 331 | def _handle_object_info_reply(self, rep): |
|
331 | 332 | """ Handle replies for call tips. |
|
332 | 333 | """ |
|
333 | 334 | cursor = self._get_cursor() |
|
334 | 335 | info = self._request_info.get('call_tip') |
|
335 | 336 | if info and info.id == rep['parent_header']['msg_id'] and \ |
|
336 | 337 | info.pos == cursor.position(): |
|
337 | doc = rep['content']['docstring'] | |
|
338 | if doc: | |
|
339 | self._call_tip_widget.show_docstring(doc) | |
|
338 | # Get the information for a call tip. For now we format the call | |
|
339 | # line as string, later we can pass False to format_call and | |
|
340 | # syntax-highlight it ourselves for nicer formatting in the | |
|
341 | # calltip. | |
|
342 | call_info, doc = call_tip(rep['content'], format_call=True) | |
|
343 | if call_info or doc: | |
|
344 | self._call_tip_widget.show_call_info(call_info, doc) | |
|
340 | 345 | |
|
341 | 346 | def _handle_pyout(self, msg): |
|
342 | 347 | """ Handle display hook output. |
|
343 | 348 | """ |
|
344 | 349 | if not self._hidden and self._is_from_this_session(msg): |
|
345 | 350 | self._append_plain_text(msg['content']['data'] + '\n') |
|
346 | 351 | |
|
347 | 352 | def _handle_stream(self, msg): |
|
348 | 353 | """ Handle stdout, stderr, and stdin. |
|
349 | 354 | """ |
|
350 | 355 | if not self._hidden and self._is_from_this_session(msg): |
|
351 | 356 | # Most consoles treat tabs as being 8 space characters. Convert tabs |
|
352 | 357 | # to spaces so that output looks as expected regardless of this |
|
353 | 358 | # widget's tab width. |
|
354 | 359 | text = msg['content']['data'].expandtabs(8) |
|
355 | 360 | |
|
356 | 361 | self._append_plain_text(text) |
|
357 | 362 | self._control.moveCursor(QtGui.QTextCursor.End) |
|
358 | 363 | |
|
359 | 364 | def _started_channels(self): |
|
360 | 365 | """ Called when the KernelManager channels have started listening or |
|
361 | 366 | when the frontend is assigned an already listening KernelManager. |
|
362 | 367 | """ |
|
363 | 368 | self.reset() |
|
364 | 369 | |
|
365 | 370 | #--------------------------------------------------------------------------- |
|
366 | 371 | # 'FrontendWidget' public interface |
|
367 | 372 | #--------------------------------------------------------------------------- |
|
368 | 373 | |
|
369 | 374 | def copy_raw(self): |
|
370 | 375 | """ Copy the currently selected text to the clipboard without attempting |
|
371 | 376 | to remove prompts or otherwise alter the text. |
|
372 | 377 | """ |
|
373 | 378 | self._control.copy() |
|
374 | 379 | |
|
375 | 380 | def execute_file(self, path, hidden=False): |
|
376 | 381 | """ Attempts to execute file with 'path'. If 'hidden', no output is |
|
377 | 382 | shown. |
|
378 | 383 | """ |
|
379 | 384 | self.execute('execfile("%s")' % path, hidden=hidden) |
|
380 | 385 | |
|
381 | 386 | def interrupt_kernel(self): |
|
382 | 387 | """ Attempts to interrupt the running kernel. |
|
383 | 388 | """ |
|
384 | 389 | if self.custom_interrupt: |
|
385 | 390 | self.custom_interrupt_requested.emit() |
|
386 | 391 | elif self.kernel_manager.has_kernel: |
|
387 | 392 | self.kernel_manager.interrupt_kernel() |
|
388 | 393 | else: |
|
389 | 394 | self._append_plain_text('Kernel process is either remote or ' |
|
390 | 395 | 'unspecified. Cannot interrupt.\n') |
|
391 | 396 | |
|
392 | 397 | def reset(self): |
|
393 | 398 | """ Resets the widget to its initial state. Similar to ``clear``, but |
|
394 | 399 | also re-writes the banner and aborts execution if necessary. |
|
395 | 400 | """ |
|
396 | 401 | if self._executing: |
|
397 | 402 | self._executing = False |
|
398 | 403 | self._request_info['execute'] = None |
|
399 | 404 | self._reading = False |
|
400 | 405 | self._highlighter.highlighting_on = False |
|
401 | 406 | |
|
402 | 407 | self._control.clear() |
|
403 | 408 | self._append_plain_text(self._get_banner()) |
|
404 | 409 | self._show_interpreter_prompt() |
|
405 | 410 | |
|
406 | 411 | def restart_kernel(self, message, now=False): |
|
407 | 412 | """ Attempts to restart the running kernel. |
|
408 | 413 | """ |
|
409 | 414 | # FIXME: now should be configurable via a checkbox in the dialog. Right |
|
410 | 415 | # now at least the heartbeat path sets it to True and the manual restart |
|
411 | 416 | # to False. But those should just be the pre-selected states of a |
|
412 | 417 | # checkbox that the user could override if so desired. But I don't know |
|
413 | 418 | # enough Qt to go implementing the checkbox now. |
|
414 | 419 | |
|
415 | 420 | if self.custom_restart: |
|
416 | 421 | self.custom_restart_requested.emit() |
|
417 | 422 | |
|
418 | 423 | elif self.kernel_manager.has_kernel: |
|
419 | 424 | # Pause the heart beat channel to prevent further warnings. |
|
420 | 425 | self.kernel_manager.hb_channel.pause() |
|
421 | 426 | |
|
422 | 427 | # Prompt the user to restart the kernel. Un-pause the heartbeat if |
|
423 | 428 | # they decline. (If they accept, the heartbeat will be un-paused |
|
424 | 429 | # automatically when the kernel is restarted.) |
|
425 | 430 | buttons = QtGui.QMessageBox.Yes | QtGui.QMessageBox.No |
|
426 | 431 | result = QtGui.QMessageBox.question(self, 'Restart kernel?', |
|
427 | 432 | message, buttons) |
|
428 | 433 | if result == QtGui.QMessageBox.Yes: |
|
429 | 434 | try: |
|
430 | 435 | self.kernel_manager.restart_kernel(now=now) |
|
431 | 436 | except RuntimeError: |
|
432 | 437 | self._append_plain_text('Kernel started externally. ' |
|
433 | 438 | 'Cannot restart.\n') |
|
434 | 439 | else: |
|
435 | 440 | self.reset() |
|
436 | 441 | else: |
|
437 | 442 | self.kernel_manager.hb_channel.unpause() |
|
438 | 443 | |
|
439 | 444 | else: |
|
440 | 445 | self._append_plain_text('Kernel process is either remote or ' |
|
441 | 446 | 'unspecified. Cannot restart.\n') |
|
442 | 447 | |
|
443 | 448 | #--------------------------------------------------------------------------- |
|
444 | 449 | # 'FrontendWidget' protected interface |
|
445 | 450 | #--------------------------------------------------------------------------- |
|
446 | 451 | |
|
447 | 452 | def _call_tip(self): |
|
448 | 453 | """ Shows a call tip, if appropriate, at the current cursor location. |
|
449 | 454 | """ |
|
450 | 455 | # Decide if it makes sense to show a call tip |
|
451 | 456 | cursor = self._get_cursor() |
|
452 | 457 | cursor.movePosition(QtGui.QTextCursor.Left) |
|
453 | 458 | if cursor.document().characterAt(cursor.position()).toAscii() != '(': |
|
454 | 459 | return False |
|
455 | 460 | context = self._get_context(cursor) |
|
456 | 461 | if not context: |
|
457 | 462 | return False |
|
458 | 463 | |
|
459 | 464 | # Send the metadata request to the kernel |
|
460 | 465 | name = '.'.join(context) |
|
461 | 466 | msg_id = self.kernel_manager.xreq_channel.object_info(name) |
|
462 | 467 | pos = self._get_cursor().position() |
|
463 | 468 | self._request_info['call_tip'] = self._CallTipRequest(msg_id, pos) |
|
464 | 469 | return True |
|
465 | 470 | |
|
466 | 471 | def _complete(self): |
|
467 | 472 | """ Performs completion at the current cursor location. |
|
468 | 473 | """ |
|
469 | 474 | context = self._get_context() |
|
470 | 475 | if context: |
|
471 | 476 | # Send the completion request to the kernel |
|
472 | 477 | msg_id = self.kernel_manager.xreq_channel.complete( |
|
473 | 478 | '.'.join(context), # text |
|
474 | 479 | self._get_input_buffer_cursor_line(), # line |
|
475 | 480 | self._get_input_buffer_cursor_column(), # cursor_pos |
|
476 | 481 | self.input_buffer) # block |
|
477 | 482 | pos = self._get_cursor().position() |
|
478 | 483 | info = self._CompletionRequest(msg_id, pos) |
|
479 | 484 | self._request_info['complete'] = info |
|
480 | 485 | |
|
481 | 486 | def _get_banner(self): |
|
482 | 487 | """ Gets a banner to display at the beginning of a session. |
|
483 | 488 | """ |
|
484 | 489 | banner = 'Python %s on %s\nType "help", "copyright", "credits" or ' \ |
|
485 | 490 | '"license" for more information.' |
|
486 | 491 | return banner % (sys.version, sys.platform) |
|
487 | 492 | |
|
488 | 493 | def _get_context(self, cursor=None): |
|
489 | 494 | """ Gets the context for the specified cursor (or the current cursor |
|
490 | 495 | if none is specified). |
|
491 | 496 | """ |
|
492 | 497 | if cursor is None: |
|
493 | 498 | cursor = self._get_cursor() |
|
494 | 499 | cursor.movePosition(QtGui.QTextCursor.StartOfBlock, |
|
495 | 500 | QtGui.QTextCursor.KeepAnchor) |
|
496 | 501 | text = unicode(cursor.selection().toPlainText()) |
|
497 | 502 | return self._completion_lexer.get_context(text) |
|
498 | 503 | |
|
499 | 504 | def _process_execute_abort(self, msg): |
|
500 | 505 | """ Process a reply for an aborted execution request. |
|
501 | 506 | """ |
|
502 | 507 | self._append_plain_text("ERROR: execution aborted\n") |
|
503 | 508 | |
|
504 | 509 | def _process_execute_error(self, msg): |
|
505 | 510 | """ Process a reply for an execution request that resulted in an error. |
|
506 | 511 | """ |
|
507 | 512 | content = msg['content'] |
|
508 | 513 | traceback = ''.join(content['traceback']) |
|
509 | 514 | self._append_plain_text(traceback) |
|
510 | 515 | |
|
511 | 516 | def _process_execute_ok(self, msg): |
|
512 | 517 | """ Process a reply for a successful execution equest. |
|
513 | 518 | """ |
|
514 | 519 | payload = msg['content']['payload'] |
|
515 | 520 | for item in payload: |
|
516 | 521 | if not self._process_execute_payload(item): |
|
517 | 522 | warning = 'Warning: received unknown payload of type %s' |
|
518 | 523 | print(warning % repr(item['source'])) |
|
519 | 524 | |
|
520 | 525 | def _process_execute_payload(self, item): |
|
521 | 526 | """ Process a single payload item from the list of payload items in an |
|
522 | 527 | execution reply. Returns whether the payload was handled. |
|
523 | 528 | """ |
|
524 | 529 | # The basic FrontendWidget doesn't handle payloads, as they are a |
|
525 | 530 | # mechanism for going beyond the standard Python interpreter model. |
|
526 | 531 | return False |
|
527 | 532 | |
|
528 | 533 | def _show_interpreter_prompt(self): |
|
529 | 534 | """ Shows a prompt for the interpreter. |
|
530 | 535 | """ |
|
531 | 536 | self._show_prompt('>>> ') |
|
532 | 537 | |
|
533 | 538 | def _show_interpreter_prompt_for_reply(self, msg): |
|
534 | 539 | """ Shows a prompt for the interpreter given an 'execute_reply' message. |
|
535 | 540 | """ |
|
536 | 541 | self._show_interpreter_prompt() |
|
537 | 542 | |
|
538 | 543 | #------ Signal handlers ---------------------------------------------------- |
|
539 | 544 | |
|
540 | 545 | def _document_contents_change(self, position, removed, added): |
|
541 | 546 | """ Called whenever the document's content changes. Display a call tip |
|
542 | 547 | if appropriate. |
|
543 | 548 | """ |
|
544 | 549 | # Calculate where the cursor should be *after* the change: |
|
545 | 550 | position += added |
|
546 | 551 | |
|
547 | 552 | document = self._control.document() |
|
548 | 553 | if position == self._get_cursor().position(): |
|
549 | 554 | self._call_tip() |
@@ -1,627 +1,626 b'' | |||
|
1 | 1 | #!/usr/bin/env python |
|
2 | 2 | """A simple interactive kernel that talks to a frontend over 0MQ. |
|
3 | 3 | |
|
4 | 4 | Things to do: |
|
5 | 5 | |
|
6 | 6 | * Implement `set_parent` logic. Right before doing exec, the Kernel should |
|
7 | 7 | call set_parent on all the PUB objects with the message about to be executed. |
|
8 | 8 | * Implement random port and security key logic. |
|
9 | 9 | * Implement control messages. |
|
10 | 10 | * Implement event loop and poll version. |
|
11 | 11 | """ |
|
12 | 12 | |
|
13 | 13 | #----------------------------------------------------------------------------- |
|
14 | 14 | # Imports |
|
15 | 15 | #----------------------------------------------------------------------------- |
|
16 | 16 | from __future__ import print_function |
|
17 | 17 | |
|
18 | 18 | # Standard library imports. |
|
19 | 19 | import __builtin__ |
|
20 | 20 | import atexit |
|
21 | 21 | import sys |
|
22 | 22 | import time |
|
23 | 23 | import traceback |
|
24 | 24 | |
|
25 | 25 | # System library imports. |
|
26 | 26 | import zmq |
|
27 | 27 | |
|
28 | 28 | # Local imports. |
|
29 | 29 | from IPython.config.configurable import Configurable |
|
30 | 30 | from IPython.utils import io |
|
31 | 31 | from IPython.utils.jsonutil import json_clean |
|
32 | 32 | from IPython.lib import pylabtools |
|
33 | 33 | from IPython.utils.traitlets import Instance, Float |
|
34 | 34 | from entry_point import (base_launch_kernel, make_argument_parser, make_kernel, |
|
35 | 35 | start_kernel) |
|
36 | 36 | from iostream import OutStream |
|
37 | 37 | from session import Session, Message |
|
38 | 38 | from zmqshell import ZMQInteractiveShell |
|
39 | 39 | |
|
40 | 40 | #----------------------------------------------------------------------------- |
|
41 | 41 | # Main kernel class |
|
42 | 42 | #----------------------------------------------------------------------------- |
|
43 | 43 | |
|
44 | 44 | class Kernel(Configurable): |
|
45 | 45 | |
|
46 | 46 | #--------------------------------------------------------------------------- |
|
47 | 47 | # Kernel interface |
|
48 | 48 | #--------------------------------------------------------------------------- |
|
49 | 49 | |
|
50 | 50 | shell = Instance('IPython.core.interactiveshell.InteractiveShellABC') |
|
51 | 51 | session = Instance(Session) |
|
52 | 52 | reply_socket = Instance('zmq.Socket') |
|
53 | 53 | pub_socket = Instance('zmq.Socket') |
|
54 | 54 | req_socket = Instance('zmq.Socket') |
|
55 | 55 | |
|
56 | 56 | # Private interface |
|
57 | 57 | |
|
58 | 58 | # Time to sleep after flushing the stdout/err buffers in each execute |
|
59 | 59 | # cycle. While this introduces a hard limit on the minimal latency of the |
|
60 | 60 | # execute cycle, it helps prevent output synchronization problems for |
|
61 | 61 | # clients. |
|
62 | 62 | # Units are in seconds. The minimum zmq latency on local host is probably |
|
63 | 63 | # ~150 microseconds, set this to 500us for now. We may need to increase it |
|
64 | 64 | # a little if it's not enough after more interactive testing. |
|
65 | 65 | _execute_sleep = Float(0.0005, config=True) |
|
66 | 66 | |
|
67 | 67 | # Frequency of the kernel's event loop. |
|
68 | 68 | # Units are in seconds, kernel subclasses for GUI toolkits may need to |
|
69 | 69 | # adapt to milliseconds. |
|
70 | 70 | _poll_interval = Float(0.05, config=True) |
|
71 | 71 | |
|
72 | 72 | # If the shutdown was requested over the network, we leave here the |
|
73 | 73 | # necessary reply message so it can be sent by our registered atexit |
|
74 | 74 | # handler. This ensures that the reply is only sent to clients truly at |
|
75 | 75 | # the end of our shutdown process (which happens after the underlying |
|
76 | 76 | # IPython shell's own shutdown). |
|
77 | 77 | _shutdown_message = None |
|
78 | 78 | |
|
79 | 79 | # This is a dict of port number that the kernel is listening on. It is set |
|
80 | 80 | # by record_ports and used by connect_request. |
|
81 | 81 | _recorded_ports = None |
|
82 | 82 | |
|
83 | 83 | def __init__(self, **kwargs): |
|
84 | 84 | super(Kernel, self).__init__(**kwargs) |
|
85 | 85 | |
|
86 | 86 | # Before we even start up the shell, register *first* our exit handlers |
|
87 | 87 | # so they come before the shell's |
|
88 | 88 | atexit.register(self._at_shutdown) |
|
89 | 89 | |
|
90 | 90 | # Initialize the InteractiveShell subclass |
|
91 | 91 | self.shell = ZMQInteractiveShell.instance() |
|
92 | 92 | self.shell.displayhook.session = self.session |
|
93 | 93 | self.shell.displayhook.pub_socket = self.pub_socket |
|
94 | 94 | |
|
95 | 95 | # TMP - hack while developing |
|
96 | 96 | self.shell._reply_content = None |
|
97 | 97 | |
|
98 | 98 | # Build dict of handlers for message types |
|
99 | 99 | msg_types = [ 'execute_request', 'complete_request', |
|
100 | 100 | 'object_info_request', 'history_request', |
|
101 | 101 | 'connect_request', 'shutdown_request'] |
|
102 | 102 | self.handlers = {} |
|
103 | 103 | for msg_type in msg_types: |
|
104 | 104 | self.handlers[msg_type] = getattr(self, msg_type) |
|
105 | 105 | |
|
106 | 106 | def do_one_iteration(self): |
|
107 | 107 | """Do one iteration of the kernel's evaluation loop. |
|
108 | 108 | """ |
|
109 | 109 | try: |
|
110 | 110 | ident = self.reply_socket.recv(zmq.NOBLOCK) |
|
111 | 111 | except zmq.ZMQError, e: |
|
112 | 112 | if e.errno == zmq.EAGAIN: |
|
113 | 113 | return |
|
114 | 114 | else: |
|
115 | 115 | raise |
|
116 | 116 | # FIXME: Bug in pyzmq/zmq? |
|
117 | 117 | # assert self.reply_socket.rcvmore(), "Missing message part." |
|
118 | 118 | msg = self.reply_socket.recv_json() |
|
119 | 119 | |
|
120 | 120 | # Print some info about this message and leave a '--->' marker, so it's |
|
121 | 121 | # easier to trace visually the message chain when debugging. Each |
|
122 | 122 | # handler prints its message at the end. |
|
123 | 123 | # Eventually we'll move these from stdout to a logger. |
|
124 | 124 | io.raw_print('\n*** MESSAGE TYPE:', msg['msg_type'], '***') |
|
125 | 125 | io.raw_print(' Content: ', msg['content'], |
|
126 | 126 | '\n --->\n ', sep='', end='') |
|
127 | 127 | |
|
128 | 128 | # Find and call actual handler for message |
|
129 | 129 | handler = self.handlers.get(msg['msg_type'], None) |
|
130 | 130 | if handler is None: |
|
131 | 131 | io.raw_print_err("UNKNOWN MESSAGE TYPE:", msg) |
|
132 | 132 | else: |
|
133 | 133 | handler(ident, msg) |
|
134 | 134 | |
|
135 | 135 | # Check whether we should exit, in case the incoming message set the |
|
136 | 136 | # exit flag on |
|
137 | 137 | if self.shell.exit_now: |
|
138 | 138 | io.raw_print('\nExiting IPython kernel...') |
|
139 | 139 | # We do a normal, clean exit, which allows any actions registered |
|
140 | 140 | # via atexit (such as history saving) to take place. |
|
141 | 141 | sys.exit(0) |
|
142 | 142 | |
|
143 | 143 | |
|
144 | 144 | def start(self): |
|
145 | 145 | """ Start the kernel main loop. |
|
146 | 146 | """ |
|
147 | 147 | while True: |
|
148 | 148 | time.sleep(self._poll_interval) |
|
149 | 149 | self.do_one_iteration() |
|
150 | 150 | |
|
151 | 151 | def record_ports(self, xrep_port, pub_port, req_port, hb_port): |
|
152 | 152 | """Record the ports that this kernel is using. |
|
153 | 153 | |
|
154 | 154 | The creator of the Kernel instance must call this methods if they |
|
155 | 155 | want the :meth:`connect_request` method to return the port numbers. |
|
156 | 156 | """ |
|
157 | 157 | self._recorded_ports = { |
|
158 | 158 | 'xrep_port' : xrep_port, |
|
159 | 159 | 'pub_port' : pub_port, |
|
160 | 160 | 'req_port' : req_port, |
|
161 | 161 | 'hb_port' : hb_port |
|
162 | 162 | } |
|
163 | 163 | |
|
164 | 164 | #--------------------------------------------------------------------------- |
|
165 | 165 | # Kernel request handlers |
|
166 | 166 | #--------------------------------------------------------------------------- |
|
167 | 167 | |
|
168 | 168 | def _publish_pyin(self, code, parent): |
|
169 | 169 | """Publish the code request on the pyin stream.""" |
|
170 | 170 | |
|
171 | 171 | pyin_msg = self.session.msg(u'pyin',{u'code':code}, parent=parent) |
|
172 | 172 | self.pub_socket.send_json(pyin_msg) |
|
173 | 173 | |
|
174 | 174 | def execute_request(self, ident, parent): |
|
175 | 175 | |
|
176 | 176 | status_msg = self.session.msg( |
|
177 | 177 | u'status', |
|
178 | 178 | {u'execution_state':u'busy'}, |
|
179 | 179 | parent=parent |
|
180 | 180 | ) |
|
181 | 181 | self.pub_socket.send_json(status_msg) |
|
182 | 182 | |
|
183 | 183 | try: |
|
184 | 184 | content = parent[u'content'] |
|
185 | 185 | code = content[u'code'] |
|
186 | 186 | silent = content[u'silent'] |
|
187 | 187 | except: |
|
188 | 188 | io.raw_print_err("Got bad msg: ") |
|
189 | 189 | io.raw_print_err(Message(parent)) |
|
190 | 190 | return |
|
191 | 191 | |
|
192 | 192 | shell = self.shell # we'll need this a lot here |
|
193 | 193 | |
|
194 | 194 | # Replace raw_input. Note that is not sufficient to replace |
|
195 | 195 | # raw_input in the user namespace. |
|
196 | 196 | raw_input = lambda prompt='': self._raw_input(prompt, ident, parent) |
|
197 | 197 | __builtin__.raw_input = raw_input |
|
198 | 198 | |
|
199 | 199 | # Set the parent message of the display hook and out streams. |
|
200 | 200 | shell.displayhook.set_parent(parent) |
|
201 | 201 | sys.stdout.set_parent(parent) |
|
202 | 202 | sys.stderr.set_parent(parent) |
|
203 | 203 | |
|
204 | 204 | # Re-broadcast our input for the benefit of listening clients, and |
|
205 | 205 | # start computing output |
|
206 | 206 | if not silent: |
|
207 | 207 | self._publish_pyin(code, parent) |
|
208 | 208 | |
|
209 | 209 | reply_content = {} |
|
210 | 210 | try: |
|
211 | 211 | if silent: |
|
212 | 212 | # runcode uses 'exec' mode, so no displayhook will fire, and it |
|
213 | 213 | # doesn't call logging or history manipulations. Print |
|
214 | 214 | # statements in that code will obviously still execute. |
|
215 | 215 | shell.runcode(code) |
|
216 | 216 | else: |
|
217 | 217 | # FIXME: runlines calls the exception handler itself. |
|
218 | 218 | shell._reply_content = None |
|
219 | 219 | |
|
220 | 220 | # For now leave this here until we're sure we can stop using it |
|
221 | 221 | #shell.runlines(code) |
|
222 | 222 | |
|
223 | 223 | # Experimental: cell mode! Test more before turning into |
|
224 | 224 | # default and removing the hacks around runlines. |
|
225 | 225 | shell.run_cell(code) |
|
226 | 226 | except: |
|
227 | 227 | status = u'error' |
|
228 | 228 | # FIXME: this code right now isn't being used yet by default, |
|
229 | 229 | # because the runlines() call above directly fires off exception |
|
230 | 230 | # reporting. This code, therefore, is only active in the scenario |
|
231 | 231 | # where runlines itself has an unhandled exception. We need to |
|
232 | 232 | # uniformize this, for all exception construction to come from a |
|
233 | 233 | # single location in the codbase. |
|
234 | 234 | etype, evalue, tb = sys.exc_info() |
|
235 | 235 | tb_list = traceback.format_exception(etype, evalue, tb) |
|
236 | 236 | reply_content.update(shell._showtraceback(etype, evalue, tb_list)) |
|
237 | 237 | else: |
|
238 | 238 | status = u'ok' |
|
239 | 239 | |
|
240 | 240 | reply_content[u'status'] = status |
|
241 | 241 | # Compute the execution counter so clients can display prompts |
|
242 | 242 | reply_content['execution_count'] = shell.displayhook.prompt_count |
|
243 | 243 | |
|
244 | 244 | # FIXME - fish exception info out of shell, possibly left there by |
|
245 | 245 | # runlines. We'll need to clean up this logic later. |
|
246 | 246 | if shell._reply_content is not None: |
|
247 | 247 | reply_content.update(shell._reply_content) |
|
248 | 248 | |
|
249 | 249 | # At this point, we can tell whether the main code execution succeeded |
|
250 | 250 | # or not. If it did, we proceed to evaluate user_variables/expressions |
|
251 | 251 | if reply_content['status'] == 'ok': |
|
252 | 252 | reply_content[u'user_variables'] = \ |
|
253 | 253 | shell.user_variables(content[u'user_variables']) |
|
254 | 254 | reply_content[u'user_expressions'] = \ |
|
255 | 255 | shell.user_expressions(content[u'user_expressions']) |
|
256 | 256 | else: |
|
257 | 257 | # If there was an error, don't even try to compute variables or |
|
258 | 258 | # expressions |
|
259 | 259 | reply_content[u'user_variables'] = {} |
|
260 | 260 | reply_content[u'user_expressions'] = {} |
|
261 | 261 | |
|
262 | 262 | # Payloads should be retrieved regardless of outcome, so we can both |
|
263 | 263 | # recover partial output (that could have been generated early in a |
|
264 | 264 | # block, before an error) and clear the payload system always. |
|
265 | 265 | reply_content[u'payload'] = shell.payload_manager.read_payload() |
|
266 | 266 | # Be agressive about clearing the payload because we don't want |
|
267 | 267 | # it to sit in memory until the next execute_request comes in. |
|
268 | 268 | shell.payload_manager.clear_payload() |
|
269 | 269 | |
|
270 | 270 | # Send the reply. |
|
271 | 271 | reply_msg = self.session.msg(u'execute_reply', reply_content, parent) |
|
272 | 272 | io.raw_print(reply_msg) |
|
273 | 273 | |
|
274 | 274 | # Flush output before sending the reply. |
|
275 | 275 | sys.stdout.flush() |
|
276 | 276 | sys.stderr.flush() |
|
277 | 277 | # FIXME: on rare occasions, the flush doesn't seem to make it to the |
|
278 | 278 | # clients... This seems to mitigate the problem, but we definitely need |
|
279 | 279 | # to better understand what's going on. |
|
280 | 280 | if self._execute_sleep: |
|
281 | 281 | time.sleep(self._execute_sleep) |
|
282 | 282 | |
|
283 | 283 | self.reply_socket.send(ident, zmq.SNDMORE) |
|
284 | 284 | self.reply_socket.send_json(reply_msg) |
|
285 | 285 | if reply_msg['content']['status'] == u'error': |
|
286 | 286 | self._abort_queue() |
|
287 | 287 | |
|
288 | 288 | status_msg = self.session.msg( |
|
289 | 289 | u'status', |
|
290 | 290 | {u'execution_state':u'idle'}, |
|
291 | 291 | parent=parent |
|
292 | 292 | ) |
|
293 | 293 | self.pub_socket.send_json(status_msg) |
|
294 | 294 | |
|
295 | 295 | def complete_request(self, ident, parent): |
|
296 | 296 | txt, matches = self._complete(parent) |
|
297 | 297 | matches = {'matches' : matches, |
|
298 | 298 | 'matched_text' : txt, |
|
299 | 299 | 'status' : 'ok'} |
|
300 | 300 | completion_msg = self.session.send(self.reply_socket, 'complete_reply', |
|
301 | 301 | matches, parent, ident) |
|
302 | 302 | io.raw_print(completion_msg) |
|
303 | 303 | |
|
304 | 304 | def object_info_request(self, ident, parent): |
|
305 | 305 | object_info = self.shell.object_inspect(parent['content']['oname']) |
|
306 |
# Before we send this object over, we |
|
|
307 | # it for JSON usage | |
|
308 | oinfo = json_clean(object_info._asdict()) | |
|
306 | # Before we send this object over, we scrub it for JSON usage | |
|
307 | oinfo = json_clean(object_info) | |
|
309 | 308 | msg = self.session.send(self.reply_socket, 'object_info_reply', |
|
310 | 309 | oinfo, parent, ident) |
|
311 | 310 | io.raw_print(msg) |
|
312 | 311 | |
|
313 | 312 | def history_request(self, ident, parent): |
|
314 | 313 | output = parent['content']['output'] |
|
315 | 314 | index = parent['content']['index'] |
|
316 | 315 | raw = parent['content']['raw'] |
|
317 | 316 | hist = self.shell.get_history(index=index, raw=raw, output=output) |
|
318 | 317 | content = {'history' : hist} |
|
319 | 318 | msg = self.session.send(self.reply_socket, 'history_reply', |
|
320 | 319 | content, parent, ident) |
|
321 | 320 | io.raw_print(msg) |
|
322 | 321 | |
|
323 | 322 | def connect_request(self, ident, parent): |
|
324 | 323 | if self._recorded_ports is not None: |
|
325 | 324 | content = self._recorded_ports.copy() |
|
326 | 325 | else: |
|
327 | 326 | content = {} |
|
328 | 327 | msg = self.session.send(self.reply_socket, 'connect_reply', |
|
329 | 328 | content, parent, ident) |
|
330 | 329 | io.raw_print(msg) |
|
331 | 330 | |
|
332 | 331 | def shutdown_request(self, ident, parent): |
|
333 | 332 | self.shell.exit_now = True |
|
334 | 333 | self._shutdown_message = self.session.msg(u'shutdown_reply', {}, parent) |
|
335 | 334 | sys.exit(0) |
|
336 | 335 | |
|
337 | 336 | #--------------------------------------------------------------------------- |
|
338 | 337 | # Protected interface |
|
339 | 338 | #--------------------------------------------------------------------------- |
|
340 | 339 | |
|
341 | 340 | def _abort_queue(self): |
|
342 | 341 | while True: |
|
343 | 342 | try: |
|
344 | 343 | ident = self.reply_socket.recv(zmq.NOBLOCK) |
|
345 | 344 | except zmq.ZMQError, e: |
|
346 | 345 | if e.errno == zmq.EAGAIN: |
|
347 | 346 | break |
|
348 | 347 | else: |
|
349 | 348 | assert self.reply_socket.rcvmore(), \ |
|
350 | 349 | "Unexpected missing message part." |
|
351 | 350 | msg = self.reply_socket.recv_json() |
|
352 | 351 | io.raw_print("Aborting:\n", Message(msg)) |
|
353 | 352 | msg_type = msg['msg_type'] |
|
354 | 353 | reply_type = msg_type.split('_')[0] + '_reply' |
|
355 | 354 | reply_msg = self.session.msg(reply_type, {'status' : 'aborted'}, msg) |
|
356 | 355 | io.raw_print(reply_msg) |
|
357 | 356 | self.reply_socket.send(ident,zmq.SNDMORE) |
|
358 | 357 | self.reply_socket.send_json(reply_msg) |
|
359 | 358 | # We need to wait a bit for requests to come in. This can probably |
|
360 | 359 | # be set shorter for true asynchronous clients. |
|
361 | 360 | time.sleep(0.1) |
|
362 | 361 | |
|
363 | 362 | def _raw_input(self, prompt, ident, parent): |
|
364 | 363 | # Flush output before making the request. |
|
365 | 364 | sys.stderr.flush() |
|
366 | 365 | sys.stdout.flush() |
|
367 | 366 | |
|
368 | 367 | # Send the input request. |
|
369 | 368 | content = dict(prompt=prompt) |
|
370 | 369 | msg = self.session.msg(u'input_request', content, parent) |
|
371 | 370 | self.req_socket.send_json(msg) |
|
372 | 371 | |
|
373 | 372 | # Await a response. |
|
374 | 373 | reply = self.req_socket.recv_json() |
|
375 | 374 | try: |
|
376 | 375 | value = reply['content']['value'] |
|
377 | 376 | except: |
|
378 | 377 | io.raw_print_err("Got bad raw_input reply: ") |
|
379 | 378 | io.raw_print_err(Message(parent)) |
|
380 | 379 | value = '' |
|
381 | 380 | return value |
|
382 | 381 | |
|
383 | 382 | def _complete(self, msg): |
|
384 | 383 | c = msg['content'] |
|
385 | 384 | try: |
|
386 | 385 | cpos = int(c['cursor_pos']) |
|
387 | 386 | except: |
|
388 | 387 | # If we don't get something that we can convert to an integer, at |
|
389 | 388 | # least attempt the completion guessing the cursor is at the end of |
|
390 | 389 | # the text, if there's any, and otherwise of the line |
|
391 | 390 | cpos = len(c['text']) |
|
392 | 391 | if cpos==0: |
|
393 | 392 | cpos = len(c['line']) |
|
394 | 393 | return self.shell.complete(c['text'], c['line'], cpos) |
|
395 | 394 | |
|
396 | 395 | def _object_info(self, context): |
|
397 | 396 | symbol, leftover = self._symbol_from_context(context) |
|
398 | 397 | if symbol is not None and not leftover: |
|
399 | 398 | doc = getattr(symbol, '__doc__', '') |
|
400 | 399 | else: |
|
401 | 400 | doc = '' |
|
402 | 401 | object_info = dict(docstring = doc) |
|
403 | 402 | return object_info |
|
404 | 403 | |
|
405 | 404 | def _symbol_from_context(self, context): |
|
406 | 405 | if not context: |
|
407 | 406 | return None, context |
|
408 | 407 | |
|
409 | 408 | base_symbol_string = context[0] |
|
410 | 409 | symbol = self.shell.user_ns.get(base_symbol_string, None) |
|
411 | 410 | if symbol is None: |
|
412 | 411 | symbol = __builtin__.__dict__.get(base_symbol_string, None) |
|
413 | 412 | if symbol is None: |
|
414 | 413 | return None, context |
|
415 | 414 | |
|
416 | 415 | context = context[1:] |
|
417 | 416 | for i, name in enumerate(context): |
|
418 | 417 | new_symbol = getattr(symbol, name, None) |
|
419 | 418 | if new_symbol is None: |
|
420 | 419 | return symbol, context[i:] |
|
421 | 420 | else: |
|
422 | 421 | symbol = new_symbol |
|
423 | 422 | |
|
424 | 423 | return symbol, [] |
|
425 | 424 | |
|
426 | 425 | def _at_shutdown(self): |
|
427 | 426 | """Actions taken at shutdown by the kernel, called by python's atexit. |
|
428 | 427 | """ |
|
429 | 428 | # io.rprint("Kernel at_shutdown") # dbg |
|
430 | 429 | if self._shutdown_message is not None: |
|
431 | 430 | self.reply_socket.send_json(self._shutdown_message) |
|
432 | 431 | io.raw_print(self._shutdown_message) |
|
433 | 432 | # A very short sleep to give zmq time to flush its message buffers |
|
434 | 433 | # before Python truly shuts down. |
|
435 | 434 | time.sleep(0.01) |
|
436 | 435 | |
|
437 | 436 | |
|
438 | 437 | class QtKernel(Kernel): |
|
439 | 438 | """A Kernel subclass with Qt support.""" |
|
440 | 439 | |
|
441 | 440 | def start(self): |
|
442 | 441 | """Start a kernel with QtPy4 event loop integration.""" |
|
443 | 442 | |
|
444 | 443 | from PyQt4 import QtCore |
|
445 | 444 | from IPython.lib.guisupport import get_app_qt4, start_event_loop_qt4 |
|
446 | 445 | |
|
447 | 446 | self.app = get_app_qt4([" "]) |
|
448 | 447 | self.app.setQuitOnLastWindowClosed(False) |
|
449 | 448 | self.timer = QtCore.QTimer() |
|
450 | 449 | self.timer.timeout.connect(self.do_one_iteration) |
|
451 | 450 | # Units for the timer are in milliseconds |
|
452 | 451 | self.timer.start(1000*self._poll_interval) |
|
453 | 452 | start_event_loop_qt4(self.app) |
|
454 | 453 | |
|
455 | 454 | |
|
456 | 455 | class WxKernel(Kernel): |
|
457 | 456 | """A Kernel subclass with Wx support.""" |
|
458 | 457 | |
|
459 | 458 | def start(self): |
|
460 | 459 | """Start a kernel with wx event loop support.""" |
|
461 | 460 | |
|
462 | 461 | import wx |
|
463 | 462 | from IPython.lib.guisupport import start_event_loop_wx |
|
464 | 463 | |
|
465 | 464 | doi = self.do_one_iteration |
|
466 | 465 | # Wx uses milliseconds |
|
467 | 466 | poll_interval = int(1000*self._poll_interval) |
|
468 | 467 | |
|
469 | 468 | # We have to put the wx.Timer in a wx.Frame for it to fire properly. |
|
470 | 469 | # We make the Frame hidden when we create it in the main app below. |
|
471 | 470 | class TimerFrame(wx.Frame): |
|
472 | 471 | def __init__(self, func): |
|
473 | 472 | wx.Frame.__init__(self, None, -1) |
|
474 | 473 | self.timer = wx.Timer(self) |
|
475 | 474 | # Units for the timer are in milliseconds |
|
476 | 475 | self.timer.Start(poll_interval) |
|
477 | 476 | self.Bind(wx.EVT_TIMER, self.on_timer) |
|
478 | 477 | self.func = func |
|
479 | 478 | |
|
480 | 479 | def on_timer(self, event): |
|
481 | 480 | self.func() |
|
482 | 481 | |
|
483 | 482 | # We need a custom wx.App to create our Frame subclass that has the |
|
484 | 483 | # wx.Timer to drive the ZMQ event loop. |
|
485 | 484 | class IPWxApp(wx.App): |
|
486 | 485 | def OnInit(self): |
|
487 | 486 | self.frame = TimerFrame(doi) |
|
488 | 487 | self.frame.Show(False) |
|
489 | 488 | return True |
|
490 | 489 | |
|
491 | 490 | # The redirect=False here makes sure that wx doesn't replace |
|
492 | 491 | # sys.stdout/stderr with its own classes. |
|
493 | 492 | self.app = IPWxApp(redirect=False) |
|
494 | 493 | start_event_loop_wx(self.app) |
|
495 | 494 | |
|
496 | 495 | |
|
497 | 496 | class TkKernel(Kernel): |
|
498 | 497 | """A Kernel subclass with Tk support.""" |
|
499 | 498 | |
|
500 | 499 | def start(self): |
|
501 | 500 | """Start a Tk enabled event loop.""" |
|
502 | 501 | |
|
503 | 502 | import Tkinter |
|
504 | 503 | doi = self.do_one_iteration |
|
505 | 504 | # Tk uses milliseconds |
|
506 | 505 | poll_interval = int(1000*self._poll_interval) |
|
507 | 506 | # For Tkinter, we create a Tk object and call its withdraw method. |
|
508 | 507 | class Timer(object): |
|
509 | 508 | def __init__(self, func): |
|
510 | 509 | self.app = Tkinter.Tk() |
|
511 | 510 | self.app.withdraw() |
|
512 | 511 | self.func = func |
|
513 | 512 | |
|
514 | 513 | def on_timer(self): |
|
515 | 514 | self.func() |
|
516 | 515 | self.app.after(poll_interval, self.on_timer) |
|
517 | 516 | |
|
518 | 517 | def start(self): |
|
519 | 518 | self.on_timer() # Call it once to get things going. |
|
520 | 519 | self.app.mainloop() |
|
521 | 520 | |
|
522 | 521 | self.timer = Timer(doi) |
|
523 | 522 | self.timer.start() |
|
524 | 523 | |
|
525 | 524 | |
|
526 | 525 | class GTKKernel(Kernel): |
|
527 | 526 | """A Kernel subclass with GTK support.""" |
|
528 | 527 | |
|
529 | 528 | def start(self): |
|
530 | 529 | """Start the kernel, coordinating with the GTK event loop""" |
|
531 | 530 | from .gui.gtkembed import GTKEmbed |
|
532 | 531 | |
|
533 | 532 | gtk_kernel = GTKEmbed(self) |
|
534 | 533 | gtk_kernel.start() |
|
535 | 534 | |
|
536 | 535 | |
|
537 | 536 | #----------------------------------------------------------------------------- |
|
538 | 537 | # Kernel main and launch functions |
|
539 | 538 | #----------------------------------------------------------------------------- |
|
540 | 539 | |
|
541 | 540 | def launch_kernel(xrep_port=0, pub_port=0, req_port=0, hb_port=0, |
|
542 | 541 | independent=False, pylab=False): |
|
543 | 542 | """Launches a localhost kernel, binding to the specified ports. |
|
544 | 543 | |
|
545 | 544 | Parameters |
|
546 | 545 | ---------- |
|
547 | 546 | xrep_port : int, optional |
|
548 | 547 | The port to use for XREP channel. |
|
549 | 548 | |
|
550 | 549 | pub_port : int, optional |
|
551 | 550 | The port to use for the SUB channel. |
|
552 | 551 | |
|
553 | 552 | req_port : int, optional |
|
554 | 553 | The port to use for the REQ (raw input) channel. |
|
555 | 554 | |
|
556 | 555 | hb_port : int, optional |
|
557 | 556 | The port to use for the hearbeat REP channel. |
|
558 | 557 | |
|
559 | 558 | independent : bool, optional (default False) |
|
560 | 559 | If set, the kernel process is guaranteed to survive if this process |
|
561 | 560 | dies. If not set, an effort is made to ensure that the kernel is killed |
|
562 | 561 | when this process dies. Note that in this case it is still good practice |
|
563 | 562 | to kill kernels manually before exiting. |
|
564 | 563 | |
|
565 | 564 | pylab : bool or string, optional (default False) |
|
566 | 565 | If not False, the kernel will be launched with pylab enabled. If a |
|
567 | 566 | string is passed, matplotlib will use the specified backend. Otherwise, |
|
568 | 567 | matplotlib's default backend will be used. |
|
569 | 568 | |
|
570 | 569 | Returns |
|
571 | 570 | ------- |
|
572 | 571 | A tuple of form: |
|
573 | 572 | (kernel_process, xrep_port, pub_port, req_port) |
|
574 | 573 | where kernel_process is a Popen object and the ports are integers. |
|
575 | 574 | """ |
|
576 | 575 | extra_arguments = [] |
|
577 | 576 | if pylab: |
|
578 | 577 | extra_arguments.append('--pylab') |
|
579 | 578 | if isinstance(pylab, basestring): |
|
580 | 579 | extra_arguments.append(pylab) |
|
581 | 580 | return base_launch_kernel('from IPython.zmq.ipkernel import main; main()', |
|
582 | 581 | xrep_port, pub_port, req_port, hb_port, |
|
583 | 582 | independent, extra_arguments) |
|
584 | 583 | |
|
585 | 584 | |
|
586 | 585 | def main(): |
|
587 | 586 | """ The IPython kernel main entry point. |
|
588 | 587 | """ |
|
589 | 588 | parser = make_argument_parser() |
|
590 | 589 | parser.add_argument('--pylab', type=str, metavar='GUI', nargs='?', |
|
591 | 590 | const='auto', help = \ |
|
592 | 591 | "Pre-load matplotlib and numpy for interactive use. If GUI is not \ |
|
593 | 592 | given, the GUI backend is matplotlib's, otherwise use one of: \ |
|
594 | 593 | ['tk', 'gtk', 'qt', 'wx', 'inline'].") |
|
595 | 594 | namespace = parser.parse_args() |
|
596 | 595 | |
|
597 | 596 | kernel_class = Kernel |
|
598 | 597 | |
|
599 | 598 | kernel_classes = { |
|
600 | 599 | 'qt' : QtKernel, |
|
601 | 600 | 'qt4': QtKernel, |
|
602 | 601 | 'inline': Kernel, |
|
603 | 602 | 'wx' : WxKernel, |
|
604 | 603 | 'tk' : TkKernel, |
|
605 | 604 | 'gtk': GTKKernel, |
|
606 | 605 | } |
|
607 | 606 | if namespace.pylab: |
|
608 | 607 | if namespace.pylab == 'auto': |
|
609 | 608 | gui, backend = pylabtools.find_gui_and_backend() |
|
610 | 609 | else: |
|
611 | 610 | gui, backend = pylabtools.find_gui_and_backend(namespace.pylab) |
|
612 | 611 | kernel_class = kernel_classes.get(gui) |
|
613 | 612 | if kernel_class is None: |
|
614 | 613 | raise ValueError('GUI is not supported: %r' % gui) |
|
615 | 614 | pylabtools.activate_matplotlib(backend) |
|
616 | 615 | |
|
617 | 616 | kernel = make_kernel(namespace, kernel_class, OutStream) |
|
618 | 617 | |
|
619 | 618 | if namespace.pylab: |
|
620 | 619 | pylabtools.import_pylab(kernel.shell.user_ns, backend, |
|
621 | 620 | shell=kernel.shell) |
|
622 | 621 | |
|
623 | 622 | start_kernel(namespace, kernel) |
|
624 | 623 | |
|
625 | 624 | |
|
626 | 625 | if __name__ == '__main__': |
|
627 | 626 | main() |
@@ -1,868 +1,871 b'' | |||
|
1 | 1 | .. _messaging: |
|
2 | 2 | |
|
3 | 3 | ====================== |
|
4 | 4 | Messaging in IPython |
|
5 | 5 | ====================== |
|
6 | 6 | |
|
7 | 7 | |
|
8 | 8 | Introduction |
|
9 | 9 | ============ |
|
10 | 10 | |
|
11 | 11 | This document explains the basic communications design and messaging |
|
12 | 12 | specification for how the various IPython objects interact over a network |
|
13 | 13 | transport. The current implementation uses the ZeroMQ_ library for messaging |
|
14 | 14 | within and between hosts. |
|
15 | 15 | |
|
16 | 16 | .. Note:: |
|
17 | 17 | |
|
18 | 18 | This document should be considered the authoritative description of the |
|
19 | 19 | IPython messaging protocol, and all developers are strongly encouraged to |
|
20 | 20 | keep it updated as the implementation evolves, so that we have a single |
|
21 | 21 | common reference for all protocol details. |
|
22 | 22 | |
|
23 | 23 | The basic design is explained in the following diagram: |
|
24 | 24 | |
|
25 | 25 | .. image:: frontend-kernel.png |
|
26 | 26 | :width: 450px |
|
27 | 27 | :alt: IPython kernel/frontend messaging architecture. |
|
28 | 28 | :align: center |
|
29 | 29 | :target: ../_images/frontend-kernel.png |
|
30 | 30 | |
|
31 | 31 | A single kernel can be simultaneously connected to one or more frontends. The |
|
32 | 32 | kernel has three sockets that serve the following functions: |
|
33 | 33 | |
|
34 | 34 | 1. REQ: this socket is connected to a *single* frontend at a time, and it allows |
|
35 | 35 | the kernel to request input from a frontend when :func:`raw_input` is called. |
|
36 | 36 | The frontend holding the matching REP socket acts as a 'virtual keyboard' |
|
37 | 37 | for the kernel while this communication is happening (illustrated in the |
|
38 | 38 | figure by the black outline around the central keyboard). In practice, |
|
39 | 39 | frontends may display such kernel requests using a special input widget or |
|
40 | 40 | otherwise indicating that the user is to type input for the kernel instead |
|
41 | 41 | of normal commands in the frontend. |
|
42 | 42 | |
|
43 | 43 | 2. XREP: this single sockets allows multiple incoming connections from |
|
44 | 44 | frontends, and this is the socket where requests for code execution, object |
|
45 | 45 | information, prompts, etc. are made to the kernel by any frontend. The |
|
46 | 46 | communication on this socket is a sequence of request/reply actions from |
|
47 | 47 | each frontend and the kernel. |
|
48 | 48 | |
|
49 | 49 | 3. PUB: this socket is the 'broadcast channel' where the kernel publishes all |
|
50 | 50 | side effects (stdout, stderr, etc.) as well as the requests coming from any |
|
51 | 51 | client over the XREP socket and its own requests on the REP socket. There |
|
52 | 52 | are a number of actions in Python which generate side effects: :func:`print` |
|
53 | 53 | writes to ``sys.stdout``, errors generate tracebacks, etc. Additionally, in |
|
54 | 54 | a multi-client scenario, we want all frontends to be able to know what each |
|
55 | 55 | other has sent to the kernel (this can be useful in collaborative scenarios, |
|
56 | 56 | for example). This socket allows both side effects and the information |
|
57 | 57 | about communications taking place with one client over the XREQ/XREP channel |
|
58 | 58 | to be made available to all clients in a uniform manner. |
|
59 | 59 | |
|
60 | 60 | All messages are tagged with enough information (details below) for clients |
|
61 | 61 | to know which messages come from their own interaction with the kernel and |
|
62 | 62 | which ones are from other clients, so they can display each type |
|
63 | 63 | appropriately. |
|
64 | 64 | |
|
65 | 65 | The actual format of the messages allowed on each of these channels is |
|
66 | 66 | specified below. Messages are dicts of dicts with string keys and values that |
|
67 | 67 | are reasonably representable in JSON. Our current implementation uses JSON |
|
68 | 68 | explicitly as its message format, but this shouldn't be considered a permanent |
|
69 | 69 | feature. As we've discovered that JSON has non-trivial performance issues due |
|
70 | 70 | to excessive copying, we may in the future move to a pure pickle-based raw |
|
71 | 71 | message format. However, it should be possible to easily convert from the raw |
|
72 | 72 | objects to JSON, since we may have non-python clients (e.g. a web frontend). |
|
73 | 73 | As long as it's easy to make a JSON version of the objects that is a faithful |
|
74 | 74 | representation of all the data, we can communicate with such clients. |
|
75 | 75 | |
|
76 | 76 | .. Note:: |
|
77 | 77 | |
|
78 | 78 | Not all of these have yet been fully fleshed out, but the key ones are, see |
|
79 | 79 | kernel and frontend files for actual implementation details. |
|
80 | 80 | |
|
81 | 81 | |
|
82 | 82 | Python functional API |
|
83 | 83 | ===================== |
|
84 | 84 | |
|
85 | 85 | As messages are dicts, they map naturally to a ``func(**kw)`` call form. We |
|
86 | 86 | should develop, at a few key points, functional forms of all the requests that |
|
87 | 87 | take arguments in this manner and automatically construct the necessary dict |
|
88 | 88 | for sending. |
|
89 | 89 | |
|
90 | 90 | |
|
91 | 91 | General Message Format |
|
92 | 92 | ====================== |
|
93 | 93 | |
|
94 | 94 | All messages send or received by any IPython process should have the following |
|
95 | 95 | generic structure:: |
|
96 | 96 | |
|
97 | 97 | { |
|
98 | 98 | # The message header contains a pair of unique identifiers for the |
|
99 | 99 | # originating session and the actual message id, in addition to the |
|
100 | 100 | # username for the process that generated the message. This is useful in |
|
101 | 101 | # collaborative settings where multiple users may be interacting with the |
|
102 | 102 | # same kernel simultaneously, so that frontends can label the various |
|
103 | 103 | # messages in a meaningful way. |
|
104 | 104 | 'header' : { 'msg_id' : uuid, |
|
105 | 105 | 'username' : str, |
|
106 | 106 | 'session' : uuid |
|
107 | 107 | }, |
|
108 | 108 | |
|
109 | 109 | # In a chain of messages, the header from the parent is copied so that |
|
110 | 110 | # clients can track where messages come from. |
|
111 | 111 | 'parent_header' : dict, |
|
112 | 112 | |
|
113 | 113 | # All recognized message type strings are listed below. |
|
114 | 114 | 'msg_type' : str, |
|
115 | 115 | |
|
116 | 116 | # The actual content of the message must be a dict, whose structure |
|
117 | 117 | # depends on the message type.x |
|
118 | 118 | 'content' : dict, |
|
119 | 119 | } |
|
120 | 120 | |
|
121 | 121 | For each message type, the actual content will differ and all existing message |
|
122 | 122 | types are specified in what follows of this document. |
|
123 | 123 | |
|
124 | 124 | |
|
125 | 125 | Messages on the XREP/XREQ socket |
|
126 | 126 | ================================ |
|
127 | 127 | |
|
128 | 128 | .. _execute: |
|
129 | 129 | |
|
130 | 130 | Execute |
|
131 | 131 | ------- |
|
132 | 132 | |
|
133 | 133 | This message type is used by frontends to ask the kernel to execute code on |
|
134 | 134 | behalf of the user, in a namespace reserved to the user's variables (and thus |
|
135 | 135 | separate from the kernel's own internal code and variables). |
|
136 | 136 | |
|
137 | 137 | Message type: ``execute_request``:: |
|
138 | 138 | |
|
139 | 139 | content = { |
|
140 | 140 | # Source code to be executed by the kernel, one or more lines. |
|
141 | 141 | 'code' : str, |
|
142 | 142 | |
|
143 | 143 | # A boolean flag which, if True, signals the kernel to execute this |
|
144 | 144 | # code as quietly as possible. This means that the kernel will compile |
|
145 | 145 | # the code witIPython/core/tests/h 'exec' instead of 'single' (so |
|
146 | 146 | # sys.displayhook will not fire), and will *not*: |
|
147 | 147 | # - broadcast exceptions on the PUB socket |
|
148 | 148 | # - do any logging |
|
149 | 149 | # - populate any history |
|
150 | 150 | # |
|
151 | 151 | # The default is False. |
|
152 | 152 | 'silent' : bool, |
|
153 | 153 | |
|
154 | 154 | # A list of variable names from the user's namespace to be retrieved. What |
|
155 | 155 | # returns is a JSON string of the variable's repr(), not a python object. |
|
156 | 156 | 'user_variables' : list, |
|
157 | 157 | |
|
158 | 158 | # Similarly, a dict mapping names to expressions to be evaluated in the |
|
159 | 159 | # user's dict. |
|
160 | 160 | 'user_expressions' : dict, |
|
161 | 161 | } |
|
162 | 162 | |
|
163 | 163 | The ``code`` field contains a single string (possibly multiline). The kernel |
|
164 | 164 | is responsible for splitting this into one or more independent execution blocks |
|
165 | 165 | and deciding whether to compile these in 'single' or 'exec' mode (see below for |
|
166 | 166 | detailed execution semantics). |
|
167 | 167 | |
|
168 | 168 | The ``user_`` fields deserve a detailed explanation. In the past, IPython had |
|
169 | 169 | the notion of a prompt string that allowed arbitrary code to be evaluated, and |
|
170 | 170 | this was put to good use by many in creating prompts that displayed system |
|
171 | 171 | status, path information, and even more esoteric uses like remote instrument |
|
172 | 172 | status aqcuired over the network. But now that IPython has a clean separation |
|
173 | 173 | between the kernel and the clients, the kernel has no prompt knowledge; prompts |
|
174 | 174 | are a frontend-side feature, and it should be even possible for different |
|
175 | 175 | frontends to display different prompts while interacting with the same kernel. |
|
176 | 176 | |
|
177 | 177 | The kernel now provides the ability to retrieve data from the user's namespace |
|
178 | 178 | after the execution of the main ``code``, thanks to two fields in the |
|
179 | 179 | ``execute_request`` message: |
|
180 | 180 | |
|
181 | 181 | - ``user_variables``: If only variables from the user's namespace are needed, a |
|
182 | 182 | list of variable names can be passed and a dict with these names as keys and |
|
183 | 183 | their :func:`repr()` as values will be returned. |
|
184 | 184 | |
|
185 | 185 | - ``user_expressions``: For more complex expressions that require function |
|
186 | 186 | evaluations, a dict can be provided with string keys and arbitrary python |
|
187 | 187 | expressions as values. The return message will contain also a dict with the |
|
188 | 188 | same keys and the :func:`repr()` of the evaluated expressions as value. |
|
189 | 189 | |
|
190 | 190 | With this information, frontends can display any status information they wish |
|
191 | 191 | in the form that best suits each frontend (a status line, a popup, inline for a |
|
192 | 192 | terminal, etc). |
|
193 | 193 | |
|
194 | 194 | .. Note:: |
|
195 | 195 | |
|
196 | 196 | In order to obtain the current execution counter for the purposes of |
|
197 | 197 | displaying input prompts, frontends simply make an execution request with an |
|
198 | 198 | empty code string and ``silent=True``. |
|
199 | 199 | |
|
200 | 200 | Execution semantics |
|
201 | 201 | ~~~~~~~~~~~~~~~~~~~ |
|
202 | 202 | |
|
203 | 203 | When the silent flag is false, the execution of use code consists of the |
|
204 | 204 | following phases (in silent mode, only the ``code`` field is executed): |
|
205 | 205 | |
|
206 | 206 | 1. Run the ``pre_runcode_hook``. |
|
207 | 207 | |
|
208 | 208 | 2. Execute the ``code`` field, see below for details. |
|
209 | 209 | |
|
210 | 210 | 3. If #2 succeeds, compute ``user_variables`` and ``user_expressions`` are |
|
211 | 211 | computed. This ensures that any error in the latter don't harm the main |
|
212 | 212 | code execution. |
|
213 | 213 | |
|
214 | 214 | 4. Call any method registered with :meth:`register_post_execute`. |
|
215 | 215 | |
|
216 | 216 | .. warning:: |
|
217 | 217 | |
|
218 | 218 | The API for running code before/after the main code block is likely to |
|
219 | 219 | change soon. Both the ``pre_runcode_hook`` and the |
|
220 | 220 | :meth:`register_post_execute` are susceptible to modification, as we find a |
|
221 | 221 | consistent model for both. |
|
222 | 222 | |
|
223 | 223 | To understand how the ``code`` field is executed, one must know that Python |
|
224 | 224 | code can be compiled in one of three modes (controlled by the ``mode`` argument |
|
225 | 225 | to the :func:`compile` builtin): |
|
226 | 226 | |
|
227 | 227 | *single* |
|
228 | 228 | Valid for a single interactive statement (though the source can contain |
|
229 | 229 | multiple lines, such as a for loop). When compiled in this mode, the |
|
230 | 230 | generated bytecode contains special instructions that trigger the calling of |
|
231 | 231 | :func:`sys.displayhook` for any expression in the block that returns a value. |
|
232 | 232 | This means that a single statement can actually produce multiple calls to |
|
233 | 233 | :func:`sys.displayhook`, if for example it contains a loop where each |
|
234 | 234 | iteration computes an unassigned expression would generate 10 calls:: |
|
235 | 235 | |
|
236 | 236 | for i in range(10): |
|
237 | 237 | i**2 |
|
238 | 238 | |
|
239 | 239 | *exec* |
|
240 | 240 | An arbitrary amount of source code, this is how modules are compiled. |
|
241 | 241 | :func:`sys.displayhook` is *never* implicitly called. |
|
242 | 242 | |
|
243 | 243 | *eval* |
|
244 | 244 | A single expression that returns a value. :func:`sys.displayhook` is *never* |
|
245 | 245 | implicitly called. |
|
246 | 246 | |
|
247 | 247 | |
|
248 | 248 | The ``code`` field is split into individual blocks each of which is valid for |
|
249 | 249 | execution in 'single' mode, and then: |
|
250 | 250 | |
|
251 | 251 | - If there is only a single block: it is executed in 'single' mode. |
|
252 | 252 | |
|
253 | 253 | - If there is more than one block: |
|
254 | 254 | |
|
255 | 255 | * if the last one is a single line long, run all but the last in 'exec' mode |
|
256 | 256 | and the very last one in 'single' mode. This makes it easy to type simple |
|
257 | 257 | expressions at the end to see computed values. |
|
258 | 258 | |
|
259 | 259 | * if the last one is no more than two lines long, run all but the last in |
|
260 | 260 | 'exec' mode and the very last one in 'single' mode. This makes it easy to |
|
261 | 261 | type simple expressions at the end to see computed values. - otherwise |
|
262 | 262 | (last one is also multiline), run all in 'exec' mode |
|
263 | 263 | |
|
264 | 264 | * otherwise (last one is also multiline), run all in 'exec' mode as a single |
|
265 | 265 | unit. |
|
266 | 266 | |
|
267 | 267 | Any error in retrieving the ``user_variables`` or evaluating the |
|
268 | 268 | ``user_expressions`` will result in a simple error message in the return fields |
|
269 | 269 | of the form:: |
|
270 | 270 | |
|
271 | 271 | [ERROR] ExceptionType: Exception message |
|
272 | 272 | |
|
273 | 273 | The user can simply send the same variable name or expression for evaluation to |
|
274 | 274 | see a regular traceback. |
|
275 | 275 | |
|
276 | 276 | Errors in any registered post_execute functions are also reported similarly, |
|
277 | 277 | and the failing function is removed from the post_execution set so that it does |
|
278 | 278 | not continue triggering failures. |
|
279 | 279 | |
|
280 | 280 | Upon completion of the execution request, the kernel *always* sends a reply, |
|
281 | 281 | with a status code indicating what happened and additional data depending on |
|
282 | 282 | the outcome. See :ref:`below <execution_results>` for the possible return |
|
283 | 283 | codes and associated data. |
|
284 | 284 | |
|
285 | 285 | |
|
286 | 286 | Execution counter (old prompt number) |
|
287 | 287 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|
288 | 288 | |
|
289 | 289 | The kernel has a single, monotonically increasing counter of all execution |
|
290 | 290 | requests that are made with ``silent=False``. This counter is used to populate |
|
291 | 291 | the ``In[n]``, ``Out[n]`` and ``_n`` variables, so clients will likely want to |
|
292 | 292 | display it in some form to the user, which will typically (but not necessarily) |
|
293 | 293 | be done in the prompts. The value of this counter will be returned as the |
|
294 | 294 | ``execution_count`` field of all ``execute_reply`` messages. |
|
295 | 295 | |
|
296 | 296 | .. _execution_results: |
|
297 | 297 | |
|
298 | 298 | Execution results |
|
299 | 299 | ~~~~~~~~~~~~~~~~~ |
|
300 | 300 | |
|
301 | 301 | Message type: ``execute_reply``:: |
|
302 | 302 | |
|
303 | 303 | content = { |
|
304 | 304 | # One of: 'ok' OR 'error' OR 'abort' |
|
305 | 305 | 'status' : str, |
|
306 | 306 | |
|
307 | 307 | # The global kernel counter that increases by one with each non-silent |
|
308 | 308 | # executed request. This will typically be used by clients to display |
|
309 | 309 | # prompt numbers to the user. If the request was a silent one, this will |
|
310 | 310 | # be the current value of the counter in the kernel. |
|
311 | 311 | 'execution_count' : int, |
|
312 | 312 | } |
|
313 | 313 | |
|
314 | 314 | When status is 'ok', the following extra fields are present:: |
|
315 | 315 | |
|
316 | 316 | { |
|
317 | 317 | # The execution payload is a dict with string keys that may have been |
|
318 | 318 | # produced by the code being executed. It is retrieved by the kernel at |
|
319 | 319 | # the end of the execution and sent back to the front end, which can take |
|
320 | 320 | # action on it as needed. See main text for further details. |
|
321 | 321 | 'payload' : dict, |
|
322 | 322 | |
|
323 | 323 | # Results for the user_variables and user_expressions. |
|
324 | 324 | 'user_variables' : dict, |
|
325 | 325 | 'user_expressions' : dict, |
|
326 | 326 | |
|
327 | 327 | # The kernel will often transform the input provided to it. If the |
|
328 | 328 | # '---->' transform had been applied, this is filled, otherwise it's the |
|
329 | 329 | # empty string. So transformations like magics don't appear here, only |
|
330 | 330 | # autocall ones. |
|
331 | 331 | 'transformed_code' : str, |
|
332 | 332 | } |
|
333 | 333 | |
|
334 | 334 | .. admonition:: Execution payloads |
|
335 | 335 | |
|
336 | 336 | The notion of an 'execution payload' is different from a return value of a |
|
337 | 337 | given set of code, which normally is just displayed on the pyout stream |
|
338 | 338 | through the PUB socket. The idea of a payload is to allow special types of |
|
339 | 339 | code, typically magics, to populate a data container in the IPython kernel |
|
340 | 340 | that will be shipped back to the caller via this channel. The kernel will |
|
341 | 341 | have an API for this, probably something along the lines of:: |
|
342 | 342 | |
|
343 | 343 | ip.exec_payload_add(key, value) |
|
344 | 344 | |
|
345 | 345 | though this API is still in the design stages. The data returned in this |
|
346 | 346 | payload will allow frontends to present special views of what just happened. |
|
347 | 347 | |
|
348 | 348 | |
|
349 | 349 | When status is 'error', the following extra fields are present:: |
|
350 | 350 | |
|
351 | 351 | { |
|
352 | 352 | 'exc_name' : str, # Exception name, as a string |
|
353 | 353 | 'exc_value' : str, # Exception value, as a string |
|
354 | 354 | |
|
355 | 355 | # The traceback will contain a list of frames, represented each as a |
|
356 | 356 | # string. For now we'll stick to the existing design of ultraTB, which |
|
357 | 357 | # controls exception level of detail statefully. But eventually we'll |
|
358 | 358 | # want to grow into a model where more information is collected and |
|
359 | 359 | # packed into the traceback object, with clients deciding how little or |
|
360 | 360 | # how much of it to unpack. But for now, let's start with a simple list |
|
361 | 361 | # of strings, since that requires only minimal changes to ultratb as |
|
362 | 362 | # written. |
|
363 | 363 | 'traceback' : list, |
|
364 | 364 | } |
|
365 | 365 | |
|
366 | 366 | |
|
367 | 367 | When status is 'abort', there are for now no additional data fields. This |
|
368 | 368 | happens when the kernel was interrupted by a signal. |
|
369 | 369 | |
|
370 | 370 | Kernel attribute access |
|
371 | 371 | ----------------------- |
|
372 | 372 | |
|
373 | 373 | .. warning:: |
|
374 | 374 | |
|
375 | 375 | This part of the messaging spec is not actually implemented in the kernel |
|
376 | 376 | yet. |
|
377 | 377 | |
|
378 | 378 | While this protocol does not specify full RPC access to arbitrary methods of |
|
379 | 379 | the kernel object, the kernel does allow read (and in some cases write) access |
|
380 | 380 | to certain attributes. |
|
381 | 381 | |
|
382 | 382 | The policy for which attributes can be read is: any attribute of the kernel, or |
|
383 | 383 | its sub-objects, that belongs to a :class:`Configurable` object and has been |
|
384 | 384 | declared at the class-level with Traits validation, is in principle accessible |
|
385 | 385 | as long as its name does not begin with a leading underscore. The attribute |
|
386 | 386 | itself will have metadata indicating whether it allows remote read and/or write |
|
387 | 387 | access. The message spec follows for attribute read and write requests. |
|
388 | 388 | |
|
389 | 389 | Message type: ``getattr_request``:: |
|
390 | 390 | |
|
391 | 391 | content = { |
|
392 | 392 | # The (possibly dotted) name of the attribute |
|
393 | 393 | 'name' : str, |
|
394 | 394 | } |
|
395 | 395 | |
|
396 | 396 | When a ``getattr_request`` fails, there are two possible error types: |
|
397 | 397 | |
|
398 | 398 | - AttributeError: this type of error was raised when trying to access the |
|
399 | 399 | given name by the kernel itself. This means that the attribute likely |
|
400 | 400 | doesn't exist. |
|
401 | 401 | |
|
402 | 402 | - AccessError: the attribute exists but its value is not readable remotely. |
|
403 | 403 | |
|
404 | 404 | |
|
405 | 405 | Message type: ``getattr_reply``:: |
|
406 | 406 | |
|
407 | 407 | content = { |
|
408 | 408 | # One of ['ok', 'AttributeError', 'AccessError']. |
|
409 | 409 | 'status' : str, |
|
410 | 410 | # If status is 'ok', a JSON object. |
|
411 | 411 | 'value' : object, |
|
412 | 412 | } |
|
413 | 413 | |
|
414 | 414 | Message type: ``setattr_request``:: |
|
415 | 415 | |
|
416 | 416 | content = { |
|
417 | 417 | # The (possibly dotted) name of the attribute |
|
418 | 418 | 'name' : str, |
|
419 | 419 | |
|
420 | 420 | # A JSON-encoded object, that will be validated by the Traits |
|
421 | 421 | # information in the kernel |
|
422 | 422 | 'value' : object, |
|
423 | 423 | } |
|
424 | 424 | |
|
425 | 425 | When a ``setattr_request`` fails, there are also two possible error types with |
|
426 | 426 | similar meanings as those of the ``getattr_request`` case, but for writing. |
|
427 | 427 | |
|
428 | 428 | Message type: ``setattr_reply``:: |
|
429 | 429 | |
|
430 | 430 | content = { |
|
431 | 431 | # One of ['ok', 'AttributeError', 'AccessError']. |
|
432 | 432 | 'status' : str, |
|
433 | 433 | } |
|
434 | 434 | |
|
435 | 435 | |
|
436 | 436 | |
|
437 | 437 | Object information |
|
438 | 438 | ------------------ |
|
439 | 439 | |
|
440 | 440 | One of IPython's most used capabilities is the introspection of Python objects |
|
441 | 441 | in the user's namespace, typically invoked via the ``?`` and ``??`` characters |
|
442 | 442 | (which in reality are shorthands for the ``%pinfo`` magic). This is used often |
|
443 | 443 | enough that it warrants an explicit message type, especially because frontends |
|
444 | 444 | may want to get object information in response to user keystrokes (like Tab or |
|
445 | 445 | F1) besides from the user explicitly typing code like ``x??``. |
|
446 | 446 | |
|
447 | 447 | Message type: ``object_info_request``:: |
|
448 | 448 | |
|
449 | 449 | content = { |
|
450 | 450 | # The (possibly dotted) name of the object to be searched in all |
|
451 | 451 | # relevant namespaces |
|
452 | 452 | 'name' : str, |
|
453 | 453 | |
|
454 | 454 | # The level of detail desired. The default (0) is equivalent to typing |
|
455 | 455 | # 'x?' at the prompt, 1 is equivalent to 'x??'. |
|
456 | 456 | 'detail_level' : int, |
|
457 | 457 | } |
|
458 | 458 | |
|
459 | 459 | The returned information will be a dictionary with keys very similar to the |
|
460 | 460 | field names that IPython prints at the terminal. |
|
461 | 461 | |
|
462 | 462 | Message type: ``object_info_reply``:: |
|
463 | 463 | |
|
464 | 464 | content = { |
|
465 | # The name the object was requested under | |
|
466 | 'name' : str, | |
|
467 | ||
|
465 | 468 | # Boolean flag indicating whether the named object was found or not. If |
|
466 | 469 | # it's false, all other fields will be empty. |
|
467 | 470 | 'found' : bool, |
|
468 | 471 | |
|
469 | 472 | # Flags for magics and system aliases |
|
470 | 473 | 'ismagic' : bool, |
|
471 | 474 | 'isalias' : bool, |
|
472 | 475 | |
|
473 | 476 | # The name of the namespace where the object was found ('builtin', |
|
474 | 477 | # 'magics', 'alias', 'interactive', etc.) |
|
475 | 478 | 'namespace' : str, |
|
476 | 479 | |
|
477 | 480 | # The type name will be type.__name__ for normal Python objects, but it |
|
478 | 481 | # can also be a string like 'Magic function' or 'System alias' |
|
479 | 482 | 'type_name' : str, |
|
480 | 483 | |
|
481 | 484 | 'string_form' : str, |
|
482 | 485 | |
|
483 | 486 | # For objects with a __class__ attribute this will be set |
|
484 | 487 | 'base_class' : str, |
|
485 | 488 | |
|
486 | 489 | # For objects with a __len__ attribute this will be set |
|
487 | 490 | 'length' : int, |
|
488 | 491 | |
|
489 | 492 | # If the object is a function, class or method whose file we can find, |
|
490 | 493 | # we give its full path |
|
491 | 494 | 'file' : str, |
|
492 | 495 | |
|
493 | 496 | # For pure Python callable objects, we can reconstruct the object |
|
494 | 497 | # definition line which provides its call signature. For convenience this |
|
495 | 498 | # is returned as a single 'definition' field, but below the raw parts that |
|
496 | 499 | # compose it are also returned as the argspec field. |
|
497 | 500 | 'definition' : str, |
|
498 | 501 | |
|
499 | 502 | # The individual parts that together form the definition string. Clients |
|
500 | 503 | # with rich display capabilities may use this to provide a richer and more |
|
501 | 504 | # precise representation of the definition line (e.g. by highlighting |
|
502 | 505 | # arguments based on the user's cursor position). For non-callable |
|
503 | 506 | # objects, this field is empty. |
|
504 | 507 | 'argspec' : { # The names of all the arguments |
|
505 | 508 | args : list, |
|
506 | 509 | # The name of the varargs (*args), if any |
|
507 | 510 | varargs : str, |
|
508 | 511 | # The name of the varkw (**kw), if any |
|
509 | 512 | varkw : str, |
|
510 | 513 | # The values (as strings) of all default arguments. Note |
|
511 | 514 | # that these must be matched *in reverse* with the 'args' |
|
512 | 515 | # list above, since the first positional args have no default |
|
513 | 516 | # value at all. |
|
514 |
|
|
|
517 | defaults : list, | |
|
515 | 518 | }, |
|
516 | 519 | |
|
517 | 520 | # For instances, provide the constructor signature (the definition of |
|
518 | 521 | # the __init__ method): |
|
519 | 522 | 'init_definition' : str, |
|
520 | 523 | |
|
521 | 524 | # Docstrings: for any object (function, method, module, package) with a |
|
522 | 525 | # docstring, we show it. But in addition, we may provide additional |
|
523 | 526 | # docstrings. For example, for instances we will show the constructor |
|
524 | 527 | # and class docstrings as well, if available. |
|
525 | 528 | 'docstring' : str, |
|
526 | 529 | |
|
527 | 530 | # For instances, provide the constructor and class docstrings |
|
528 | 531 | 'init_docstring' : str, |
|
529 | 532 | 'class_docstring' : str, |
|
530 | 533 | |
|
531 | 534 | # If it's a callable object whose call method has a separate docstring and |
|
532 | 535 | # definition line: |
|
533 | 536 | 'call_def' : str, |
|
534 | 537 | 'call_docstring' : str, |
|
535 | 538 | |
|
536 | 539 | # If detail_level was 1, we also try to find the source code that |
|
537 | 540 | # defines the object, if possible. The string 'None' will indicate |
|
538 | 541 | # that no source was found. |
|
539 | 542 | 'source' : str, |
|
540 | 543 | } |
|
541 | 544 | ' |
|
542 | 545 | |
|
543 | 546 | Complete |
|
544 | 547 | -------- |
|
545 | 548 | |
|
546 | 549 | Message type: ``complete_request``:: |
|
547 | 550 | |
|
548 | 551 | content = { |
|
549 | 552 | # The text to be completed, such as 'a.is' |
|
550 | 553 | 'text' : str, |
|
551 | 554 | |
|
552 | 555 | # The full line, such as 'print a.is'. This allows completers to |
|
553 | 556 | # make decisions that may require information about more than just the |
|
554 | 557 | # current word. |
|
555 | 558 | 'line' : str, |
|
556 | 559 | |
|
557 | 560 | # The entire block of text where the line is. This may be useful in the |
|
558 | 561 | # case of multiline completions where more context may be needed. Note: if |
|
559 | 562 | # in practice this field proves unnecessary, remove it to lighten the |
|
560 | 563 | # messages. |
|
561 | 564 | |
|
562 | 565 | 'block' : str, |
|
563 | 566 | |
|
564 | 567 | # The position of the cursor where the user hit 'TAB' on the line. |
|
565 | 568 | 'cursor_pos' : int, |
|
566 | 569 | } |
|
567 | 570 | |
|
568 | 571 | Message type: ``complete_reply``:: |
|
569 | 572 | |
|
570 | 573 | content = { |
|
571 | 574 | # The list of all matches to the completion request, such as |
|
572 | 575 | # ['a.isalnum', 'a.isalpha'] for the above example. |
|
573 | 576 | 'matches' : list |
|
574 | 577 | } |
|
575 | 578 | |
|
576 | 579 | |
|
577 | 580 | History |
|
578 | 581 | ------- |
|
579 | 582 | |
|
580 | 583 | For clients to explicitly request history from a kernel. The kernel has all |
|
581 | 584 | the actual execution history stored in a single location, so clients can |
|
582 | 585 | request it from the kernel when needed. |
|
583 | 586 | |
|
584 | 587 | Message type: ``history_request``:: |
|
585 | 588 | |
|
586 | 589 | content = { |
|
587 | 590 | |
|
588 | 591 | # If True, also return output history in the resulting dict. |
|
589 | 592 | 'output' : bool, |
|
590 | 593 | |
|
591 | 594 | # If True, return the raw input history, else the transformed input. |
|
592 | 595 | 'raw' : bool, |
|
593 | 596 | |
|
594 | 597 | # This parameter can be one of: A number, a pair of numbers, None |
|
595 | 598 | # If not given, last 40 are returned. |
|
596 | 599 | # - number n: return the last n entries. |
|
597 | 600 | # - pair n1, n2: return entries in the range(n1, n2). |
|
598 | 601 | # - None: return all history |
|
599 | 602 | 'index' : n or (n1, n2) or None, |
|
600 | 603 | } |
|
601 | 604 | |
|
602 | 605 | Message type: ``history_reply``:: |
|
603 | 606 | |
|
604 | 607 | content = { |
|
605 | 608 | # A dict with prompt numbers as keys and either (input, output) or input |
|
606 | 609 | # as the value depending on whether output was True or False, |
|
607 | 610 | # respectively. |
|
608 | 611 | 'history' : dict, |
|
609 | 612 | } |
|
610 | 613 | |
|
611 | 614 | |
|
612 | 615 | Connect |
|
613 | 616 | ------- |
|
614 | 617 | |
|
615 | 618 | When a client connects to the request/reply socket of the kernel, it can issue |
|
616 | 619 | a connect request to get basic information about the kernel, such as the ports |
|
617 | 620 | the other ZeroMQ sockets are listening on. This allows clients to only have |
|
618 | 621 | to know about a single port (the XREQ/XREP channel) to connect to a kernel. |
|
619 | 622 | |
|
620 | 623 | Message type: ``connect_request``:: |
|
621 | 624 | |
|
622 | 625 | content = { |
|
623 | 626 | } |
|
624 | 627 | |
|
625 | 628 | Message type: ``connect_reply``:: |
|
626 | 629 | |
|
627 | 630 | content = { |
|
628 | 631 | 'xrep_port' : int # The port the XREP socket is listening on. |
|
629 | 632 | 'pub_port' : int # The port the PUB socket is listening on. |
|
630 | 633 | 'req_port' : int # The port the REQ socket is listening on. |
|
631 | 634 | 'hb_port' : int # The port the heartbeat socket is listening on. |
|
632 | 635 | } |
|
633 | 636 | |
|
634 | 637 | |
|
635 | 638 | |
|
636 | 639 | Kernel shutdown |
|
637 | 640 | --------------- |
|
638 | 641 | |
|
639 | 642 | The clients can request the kernel to shut itself down; this is used in |
|
640 | 643 | multiple cases: |
|
641 | 644 | |
|
642 | 645 | - when the user chooses to close the client application via a menu or window |
|
643 | 646 | control. |
|
644 | 647 | - when the user types 'exit' or 'quit' (or their uppercase magic equivalents). |
|
645 | 648 | - when the user chooses a GUI method (like the 'Ctrl-C' shortcut in the |
|
646 | 649 | IPythonQt client) to force a kernel restart to get a clean kernel without |
|
647 | 650 | losing client-side state like history or inlined figures. |
|
648 | 651 | |
|
649 | 652 | The client sends a shutdown request to the kernel, and once it receives the |
|
650 | 653 | reply message (which is otherwise empty), it can assume that the kernel has |
|
651 | 654 | completed shutdown safely. |
|
652 | 655 | |
|
653 | 656 | Upon their own shutdown, client applications will typically execute a last |
|
654 | 657 | minute sanity check and forcefully terminate any kernel that is still alive, to |
|
655 | 658 | avoid leaving stray processes in the user's machine. |
|
656 | 659 | |
|
657 | 660 | For both shutdown request and reply, there is no actual content that needs to |
|
658 | 661 | be sent, so the content dict is empty. |
|
659 | 662 | |
|
660 | 663 | Message type: ``shutdown_request``:: |
|
661 | 664 | |
|
662 | 665 | content = { |
|
663 | 666 | } |
|
664 | 667 | |
|
665 | 668 | Message type: ``shutdown_reply``:: |
|
666 | 669 | |
|
667 | 670 | content = { |
|
668 | 671 | } |
|
669 | 672 | |
|
670 | 673 | .. Note:: |
|
671 | 674 | |
|
672 | 675 | When the clients detect a dead kernel thanks to inactivity on the heartbeat |
|
673 | 676 | socket, they simply send a forceful process termination signal, since a dead |
|
674 | 677 | process is unlikely to respond in any useful way to messages. |
|
675 | 678 | |
|
676 | 679 | |
|
677 | 680 | Messages on the PUB/SUB socket |
|
678 | 681 | ============================== |
|
679 | 682 | |
|
680 | 683 | Streams (stdout, stderr, etc) |
|
681 | 684 | ------------------------------ |
|
682 | 685 | |
|
683 | 686 | Message type: ``stream``:: |
|
684 | 687 | |
|
685 | 688 | content = { |
|
686 | 689 | # The name of the stream is one of 'stdin', 'stdout', 'stderr' |
|
687 | 690 | 'name' : str, |
|
688 | 691 | |
|
689 | 692 | # The data is an arbitrary string to be written to that stream |
|
690 | 693 | 'data' : str, |
|
691 | 694 | } |
|
692 | 695 | |
|
693 | 696 | When a kernel receives a raw_input call, it should also broadcast it on the pub |
|
694 | 697 | socket with the names 'stdin' and 'stdin_reply'. This will allow other clients |
|
695 | 698 | to monitor/display kernel interactions and possibly replay them to their user |
|
696 | 699 | or otherwise expose them. |
|
697 | 700 | |
|
698 | 701 | Python inputs |
|
699 | 702 | ------------- |
|
700 | 703 | |
|
701 | 704 | These messages are the re-broadcast of the ``execute_request``. |
|
702 | 705 | |
|
703 | 706 | Message type: ``pyin``:: |
|
704 | 707 | |
|
705 | 708 | content = { |
|
706 | 709 | # Source code to be executed, one or more lines |
|
707 | 710 | 'code' : str |
|
708 | 711 | } |
|
709 | 712 | |
|
710 | 713 | Python outputs |
|
711 | 714 | -------------- |
|
712 | 715 | |
|
713 | 716 | When Python produces output from code that has been compiled in with the |
|
714 | 717 | 'single' flag to :func:`compile`, any expression that produces a value (such as |
|
715 | 718 | ``1+1``) is passed to ``sys.displayhook``, which is a callable that can do with |
|
716 | 719 | this value whatever it wants. The default behavior of ``sys.displayhook`` in |
|
717 | 720 | the Python interactive prompt is to print to ``sys.stdout`` the :func:`repr` of |
|
718 | 721 | the value as long as it is not ``None`` (which isn't printed at all). In our |
|
719 | 722 | case, the kernel instantiates as ``sys.displayhook`` an object which has |
|
720 | 723 | similar behavior, but which instead of printing to stdout, broadcasts these |
|
721 | 724 | values as ``pyout`` messages for clients to display appropriately. |
|
722 | 725 | |
|
723 | 726 | Message type: ``pyout``:: |
|
724 | 727 | |
|
725 | 728 | content = { |
|
726 | 729 | # The data is typically the repr() of the object. |
|
727 | 730 | 'data' : str, |
|
728 | 731 | |
|
729 | 732 | # The counter for this execution is also provided so that clients can |
|
730 | 733 | # display it, since IPython automatically creates variables called _N (for |
|
731 | 734 | # prompt N). |
|
732 | 735 | 'execution_count' : int, |
|
733 | 736 | } |
|
734 | 737 | |
|
735 | 738 | Python errors |
|
736 | 739 | ------------- |
|
737 | 740 | |
|
738 | 741 | When an error occurs during code execution |
|
739 | 742 | |
|
740 | 743 | Message type: ``pyerr``:: |
|
741 | 744 | |
|
742 | 745 | content = { |
|
743 | 746 | # Similar content to the execute_reply messages for the 'error' case, |
|
744 | 747 | # except the 'status' field is omitted. |
|
745 | 748 | } |
|
746 | 749 | |
|
747 | 750 | Kernel status |
|
748 | 751 | ------------- |
|
749 | 752 | |
|
750 | 753 | This message type is used by frontends to monitor the status of the kernel. |
|
751 | 754 | |
|
752 | 755 | Message type: ``status``:: |
|
753 | 756 | |
|
754 | 757 | content = { |
|
755 | 758 | # When the kernel starts to execute code, it will enter the 'busy' |
|
756 | 759 | # state and when it finishes, it will enter the 'idle' state. |
|
757 | 760 | execution_state : ('busy', 'idle') |
|
758 | 761 | } |
|
759 | 762 | |
|
760 | 763 | Kernel crashes |
|
761 | 764 | -------------- |
|
762 | 765 | |
|
763 | 766 | When the kernel has an unexpected exception, caught by the last-resort |
|
764 | 767 | sys.excepthook, we should broadcast the crash handler's output before exiting. |
|
765 | 768 | This will allow clients to notice that a kernel died, inform the user and |
|
766 | 769 | propose further actions. |
|
767 | 770 | |
|
768 | 771 | Message type: ``crash``:: |
|
769 | 772 | |
|
770 | 773 | content = { |
|
771 | 774 | # Similarly to the 'error' case for execute_reply messages, this will |
|
772 | 775 | # contain exc_name, exc_type and traceback fields. |
|
773 | 776 | |
|
774 | 777 | # An additional field with supplementary information such as where to |
|
775 | 778 | # send the crash message |
|
776 | 779 | 'info' : str, |
|
777 | 780 | } |
|
778 | 781 | |
|
779 | 782 | |
|
780 | 783 | Future ideas |
|
781 | 784 | ------------ |
|
782 | 785 | |
|
783 | 786 | Other potential message types, currently unimplemented, listed below as ideas. |
|
784 | 787 | |
|
785 | 788 | Message type: ``file``:: |
|
786 | 789 | |
|
787 | 790 | content = { |
|
788 | 791 | 'path' : 'cool.jpg', |
|
789 | 792 | 'mimetype' : str, |
|
790 | 793 | 'data' : str, |
|
791 | 794 | } |
|
792 | 795 | |
|
793 | 796 | |
|
794 | 797 | Messages on the REQ/REP socket |
|
795 | 798 | ============================== |
|
796 | 799 | |
|
797 | 800 | This is a socket that goes in the opposite direction: from the kernel to a |
|
798 | 801 | *single* frontend, and its purpose is to allow ``raw_input`` and similar |
|
799 | 802 | operations that read from ``sys.stdin`` on the kernel to be fulfilled by the |
|
800 | 803 | client. For now we will keep these messages as simple as possible, since they |
|
801 | 804 | basically only mean to convey the ``raw_input(prompt)`` call. |
|
802 | 805 | |
|
803 | 806 | Message type: ``input_request``:: |
|
804 | 807 | |
|
805 | 808 | content = { 'prompt' : str } |
|
806 | 809 | |
|
807 | 810 | Message type: ``input_reply``:: |
|
808 | 811 | |
|
809 | 812 | content = { 'value' : str } |
|
810 | 813 | |
|
811 | 814 | .. Note:: |
|
812 | 815 | |
|
813 | 816 | We do not explicitly try to forward the raw ``sys.stdin`` object, because in |
|
814 | 817 | practice the kernel should behave like an interactive program. When a |
|
815 | 818 | program is opened on the console, the keyboard effectively takes over the |
|
816 | 819 | ``stdin`` file descriptor, and it can't be used for raw reading anymore. |
|
817 | 820 | Since the IPython kernel effectively behaves like a console program (albeit |
|
818 | 821 | one whose "keyboard" is actually living in a separate process and |
|
819 | 822 | transported over the zmq connection), raw ``stdin`` isn't expected to be |
|
820 | 823 | available. |
|
821 | 824 | |
|
822 | 825 | |
|
823 | 826 | Heartbeat for kernels |
|
824 | 827 | ===================== |
|
825 | 828 | |
|
826 | 829 | Initially we had considered using messages like those above over ZMQ for a |
|
827 | 830 | kernel 'heartbeat' (a way to detect quickly and reliably whether a kernel is |
|
828 | 831 | alive at all, even if it may be busy executing user code). But this has the |
|
829 | 832 | problem that if the kernel is locked inside extension code, it wouldn't execute |
|
830 | 833 | the python heartbeat code. But it turns out that we can implement a basic |
|
831 | 834 | heartbeat with pure ZMQ, without using any Python messaging at all. |
|
832 | 835 | |
|
833 | 836 | The monitor sends out a single zmq message (right now, it is a str of the |
|
834 | 837 | monitor's lifetime in seconds), and gets the same message right back, prefixed |
|
835 | 838 | with the zmq identity of the XREQ socket in the heartbeat process. This can be |
|
836 | 839 | a uuid, or even a full message, but there doesn't seem to be a need for packing |
|
837 | 840 | up a message when the sender and receiver are the exact same Python object. |
|
838 | 841 | |
|
839 | 842 | The model is this:: |
|
840 | 843 | |
|
841 | 844 | monitor.send(str(self.lifetime)) # '1.2345678910' |
|
842 | 845 | |
|
843 | 846 | and the monitor receives some number of messages of the form:: |
|
844 | 847 | |
|
845 | 848 | ['uuid-abcd-dead-beef', '1.2345678910'] |
|
846 | 849 | |
|
847 | 850 | where the first part is the zmq.IDENTITY of the heart's XREQ on the engine, and |
|
848 | 851 | the rest is the message sent by the monitor. No Python code ever has any |
|
849 | 852 | access to the message between the monitor's send, and the monitor's recv. |
|
850 | 853 | |
|
851 | 854 | |
|
852 | 855 | ToDo |
|
853 | 856 | ==== |
|
854 | 857 | |
|
855 | 858 | Missing things include: |
|
856 | 859 | |
|
857 | 860 | * Important: finish thinking through the payload concept and API. |
|
858 | 861 | |
|
859 | 862 | * Important: ensure that we have a good solution for magics like %edit. It's |
|
860 | 863 | likely that with the payload concept we can build a full solution, but not |
|
861 | 864 | 100% clear yet. |
|
862 | 865 | |
|
863 | 866 | * Finishing the details of the heartbeat protocol. |
|
864 | 867 | |
|
865 | 868 | * Signal handling: specify what kind of information kernel should broadcast (or |
|
866 | 869 | not) when it receives signals. |
|
867 | 870 | |
|
868 | 871 | .. include:: ../links.rst |
General Comments 0
You need to be logged in to leave comments.
Login now