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