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