##// END OF EJS Templates
Merge branch 'newkernel' of git://github.com/ellisonbg/ipython into qtfrontend...
epatters -
r2795:e518f8e9 merge
parent child Browse files
Show More
@@ -0,0 +1,288 b''
1 # -*- coding: utf-8 -*-
2 """Displayhook for IPython.
3
4 Authors:
5
6 * Fernando Perez
7 * Brian Granger
8 """
9
10 #-----------------------------------------------------------------------------
11 # Copyright (C) 2008-2010 The IPython Development Team
12 # Copyright (C) 2001-2007 Fernando Perez <fperez@colorado.edu>
13 #
14 # Distributed under the terms of the BSD License. The full license is in
15 # the file COPYING, distributed as part of this software.
16 #-----------------------------------------------------------------------------
17
18 #-----------------------------------------------------------------------------
19 # Imports
20 #-----------------------------------------------------------------------------
21
22 import __builtin__
23 from pprint import PrettyPrinter
24 pformat = PrettyPrinter().pformat
25
26 from IPython.config.configurable import Configurable
27 from IPython.core import prompts
28 import IPython.utils.generics
29 import IPython.utils.io
30 from IPython.utils.traitlets import Instance, Int
31 from IPython.utils.warn import warn
32
33 #-----------------------------------------------------------------------------
34 # Main displayhook class
35 #-----------------------------------------------------------------------------
36
37 # TODO: The DisplayHook class should be split into two classes, one that
38 # manages the prompts and their synchronization and another that just does the
39 # displayhook logic and calls into the prompt manager.
40
41 # TODO: Move the various attributes (cache_size, colors, input_sep,
42 # output_sep, output_sep2, ps1, ps2, ps_out, pad_left). Some of these are also
43 # attributes of InteractiveShell. They should be on ONE object only and the
44 # other objects should ask that one object for their values.
45
46 class DisplayHook(Configurable):
47 """The custom IPython displayhook to replace sys.displayhook.
48
49 This class does many things, but the basic idea is that it is a callable
50 that gets called anytime user code returns a value.
51
52 Currently this class does more than just the displayhook logic and that
53 extra logic should eventually be moved out of here.
54 """
55
56 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC')
57 # Each call to the In[] prompt raises it by 1, even the first.
58 prompt_count = Int(0)
59
60 def __init__(self, shell=None, cache_size=1000,
61 colors='NoColor', input_sep='\n',
62 output_sep='\n', output_sep2='',
63 ps1 = None, ps2 = None, ps_out = None, pad_left=True,
64 config=None):
65 super(DisplayHook, self).__init__(shell=shell, config=config)
66
67 cache_size_min = 3
68 if cache_size <= 0:
69 self.do_full_cache = 0
70 cache_size = 0
71 elif cache_size < cache_size_min:
72 self.do_full_cache = 0
73 cache_size = 0
74 warn('caching was disabled (min value for cache size is %s).' %
75 cache_size_min,level=3)
76 else:
77 self.do_full_cache = 1
78
79 self.cache_size = cache_size
80 self.input_sep = input_sep
81
82 # we need a reference to the user-level namespace
83 self.shell = shell
84
85 # Set input prompt strings and colors
86 if cache_size == 0:
87 if ps1.find('%n') > -1 or ps1.find(r'\#') > -1 \
88 or ps1.find(r'\N') > -1:
89 ps1 = '>>> '
90 if ps2.find('%n') > -1 or ps2.find(r'\#') > -1 \
91 or ps2.find(r'\N') > -1:
92 ps2 = '... '
93 self.ps1_str = self._set_prompt_str(ps1,'In [\\#]: ','>>> ')
94 self.ps2_str = self._set_prompt_str(ps2,' .\\D.: ','... ')
95 self.ps_out_str = self._set_prompt_str(ps_out,'Out[\\#]: ','')
96
97 self.color_table = prompts.PromptColors
98 self.prompt1 = prompts.Prompt1(self,sep=input_sep,prompt=self.ps1_str,
99 pad_left=pad_left)
100 self.prompt2 = prompts.Prompt2(self,prompt=self.ps2_str,pad_left=pad_left)
101 self.prompt_out = prompts.PromptOut(self,sep='',prompt=self.ps_out_str,
102 pad_left=pad_left)
103 self.set_colors(colors)
104
105 # Store the last prompt string each time, we need it for aligning
106 # continuation and auto-rewrite prompts
107 self.last_prompt = ''
108 self.output_sep = output_sep
109 self.output_sep2 = output_sep2
110 self._,self.__,self.___ = '','',''
111 self.pprint_types = map(type,[(),[],{}])
112
113 # these are deliberately global:
114 to_user_ns = {'_':self._,'__':self.__,'___':self.___}
115 self.shell.user_ns.update(to_user_ns)
116
117 def _set_prompt_str(self,p_str,cache_def,no_cache_def):
118 if p_str is None:
119 if self.do_full_cache:
120 return cache_def
121 else:
122 return no_cache_def
123 else:
124 return p_str
125
126 def set_colors(self, colors):
127 """Set the active color scheme and configure colors for the three
128 prompt subsystems."""
129
130 # FIXME: This modifying of the global prompts.prompt_specials needs
131 # to be fixed. We need to refactor all of the prompts stuff to use
132 # proper configuration and traits notifications.
133 if colors.lower()=='nocolor':
134 prompts.prompt_specials = prompts.prompt_specials_nocolor
135 else:
136 prompts.prompt_specials = prompts.prompt_specials_color
137
138 self.color_table.set_active_scheme(colors)
139 self.prompt1.set_colors()
140 self.prompt2.set_colors()
141 self.prompt_out.set_colors()
142
143 #-------------------------------------------------------------------------
144 # Methods used in __call__. Override these methods to modify the behavior
145 # of the displayhook.
146 #-------------------------------------------------------------------------
147
148 def check_for_underscore(self):
149 """Check if the user has set the '_' variable by hand."""
150 # If something injected a '_' variable in __builtin__, delete
151 # ipython's automatic one so we don't clobber that. gettext() in
152 # particular uses _, so we need to stay away from it.
153 if '_' in __builtin__.__dict__:
154 try:
155 del self.shell.user_ns['_']
156 except KeyError:
157 pass
158
159 def quiet(self):
160 """Should we silence the display hook because of ';'?"""
161 # do not print output if input ends in ';'
162 try:
163 if self.shell.input_hist[self.prompt_count].endswith(';\n'):
164 return True
165 except IndexError:
166 # some uses of ipshellembed may fail here
167 pass
168 return False
169
170 def start_displayhook(self):
171 """Start the displayhook, initializing resources."""
172 pass
173
174 def write_output_prompt(self):
175 """Write the output prompt."""
176 # Use write, not print which adds an extra space.
177 IPython.utils.io.Term.cout.write(self.output_sep)
178 outprompt = str(self.prompt_out)
179 if self.do_full_cache:
180 IPython.utils.io.Term.cout.write(outprompt)
181
182 # TODO: Make this method an extension point. The previous implementation
183 # has both a result_display hook as well as a result_display generic
184 # function to customize the repr on a per class basis. We need to rethink
185 # the hooks mechanism before doing this though.
186 def compute_result_repr(self, result):
187 """Compute and return the repr of the object to be displayed.
188
189 This method only compute the string form of the repr and should NOT
190 actual print or write that to a stream. This method may also transform
191 the result itself, but the default implementation passes the original
192 through.
193 """
194 try:
195 if self.shell.pprint:
196 result_repr = pformat(result)
197 if '\n' in result_repr:
198 # So that multi-line strings line up with the left column of
199 # the screen, instead of having the output prompt mess up
200 # their first line.
201 result_repr = '\n' + result_repr
202 else:
203 result_repr = repr(result)
204 except TypeError:
205 # This happens when result.__repr__ doesn't return a string,
206 # such as when it returns None.
207 result_repr = '\n'
208 return result, result_repr
209
210 def write_result_repr(self, result_repr):
211 # We want to print because we want to always make sure we have a
212 # newline, even if all the prompt separators are ''. This is the
213 # standard IPython behavior.
214 print >>IPython.utils.io.Term.cout, result_repr
215
216 def update_user_ns(self, result):
217 """Update user_ns with various things like _, __, _1, etc."""
218
219 # Avoid recursive reference when displaying _oh/Out
220 if result is not self.shell.user_ns['_oh']:
221 if len(self.shell.user_ns['_oh']) >= self.cache_size and self.do_full_cache:
222 warn('Output cache limit (currently '+
223 `self.cache_size`+' entries) hit.\n'
224 'Flushing cache and resetting history counter...\n'
225 'The only history variables available will be _,__,___ and _1\n'
226 'with the current result.')
227
228 self.flush()
229 # Don't overwrite '_' and friends if '_' is in __builtin__ (otherwise
230 # we cause buggy behavior for things like gettext).
231 if '_' not in __builtin__.__dict__:
232 self.___ = self.__
233 self.__ = self._
234 self._ = result
235 self.shell.user_ns.update({'_':self._,'__':self.__,'___':self.___})
236
237 # hackish access to top-level namespace to create _1,_2... dynamically
238 to_main = {}
239 if self.do_full_cache:
240 new_result = '_'+`self.prompt_count`
241 to_main[new_result] = result
242 self.shell.user_ns.update(to_main)
243 self.shell.user_ns['_oh'][self.prompt_count] = result
244
245 def log_output(self, result):
246 """Log the output."""
247 if self.shell.logger.log_output:
248 self.shell.logger.log_write(repr(result),'output')
249
250 def finish_displayhook(self):
251 """Finish up all displayhook activities."""
252 IPython.utils.io.Term.cout.write(self.output_sep2)
253 IPython.utils.io.Term.cout.flush()
254
255 def __call__(self, result=None):
256 """Printing with history cache management.
257
258 This is invoked everytime the interpreter needs to print, and is
259 activated by setting the variable sys.displayhook to it.
260 """
261 self.check_for_underscore()
262 if result is not None and not self.quiet():
263 self.start_displayhook()
264 self.write_output_prompt()
265 result, result_repr = self.compute_result_repr(result)
266 self.write_result_repr(result_repr)
267 self.update_user_ns(result)
268 self.log_output(result)
269 self.finish_displayhook()
270
271 def flush(self):
272 if not self.do_full_cache:
273 raise ValueError,"You shouldn't have reached the cache flush "\
274 "if full caching is not enabled!"
275 # delete auto-generated vars from global namespace
276
277 for n in range(1,self.prompt_count + 1):
278 key = '_'+`n`
279 try:
280 del self.shell.user_ns[key]
281 except: pass
282 self.shell.user_ns['_oh'].clear()
283
284 if '_' not in __builtin__.__dict__:
285 self.shell.user_ns.update({'_':None,'__':None, '___':None})
286 import gc
287 gc.collect() # xxx needed?
288
@@ -54,7 +54,7 b" def magic_history(self, parameter_s = ''):"
54 confirmation first if it already exists.
54 confirmation first if it already exists.
55 """
55 """
56
56
57 if not self.outputcache.do_full_cache:
57 if not self.displayhook.do_full_cache:
58 print 'This feature is only available if numbered prompts are in use.'
58 print 'This feature is only available if numbered prompts are in use.'
59 return
59 return
60 opts,args = self.parse_options(parameter_s,'gnoptsrf:',mode='list')
60 opts,args = self.parse_options(parameter_s,'gnoptsrf:',mode='list')
@@ -44,24 +44,19 b' somewhere in your configuration files or ipython command line.'
44 import os, bisect
44 import os, bisect
45 import sys
45 import sys
46
46
47 from pprint import PrettyPrinter
47 from IPython.core.error import TryNext
48
49 import IPython.utils.io
48 import IPython.utils.io
50 from IPython.utils.process import shell
49 from IPython.utils.process import shell
51
50
52 from IPython.core.error import TryNext
53
54 # List here all the default hooks. For now it's just the editor functions
51 # List here all the default hooks. For now it's just the editor functions
55 # but over time we'll move here all the public API for user-accessible things.
52 # but over time we'll move here all the public API for user-accessible things.
56
53
57 __all__ = ['editor', 'fix_error_editor', 'synchronize_with_editor', 'result_display',
54 __all__ = ['editor', 'fix_error_editor', 'synchronize_with_editor',
58 'input_prefilter', 'shutdown_hook', 'late_startup_hook',
55 'input_prefilter', 'shutdown_hook', 'late_startup_hook',
59 'generate_prompt', 'generate_output_prompt','shell_hook',
56 'generate_prompt','shell_hook',
60 'show_in_pager','pre_prompt_hook', 'pre_runcode_hook',
57 'show_in_pager','pre_prompt_hook', 'pre_runcode_hook',
61 'clipboard_get']
58 'clipboard_get']
62
59
63 pformat = PrettyPrinter().pformat
64
65 def editor(self,filename, linenum=None):
60 def editor(self,filename, linenum=None):
66 """Open the default editor at the given filename and linenumber.
61 """Open the default editor at the given filename and linenumber.
67
62
@@ -221,12 +216,8 b' def late_startup_hook(self):'
221 def generate_prompt(self, is_continuation):
216 def generate_prompt(self, is_continuation):
222 """ calculate and return a string with the prompt to display """
217 """ calculate and return a string with the prompt to display """
223 if is_continuation:
218 if is_continuation:
224 return str(self.outputcache.prompt2)
219 return str(self.displayhook.prompt2)
225 return str(self.outputcache.prompt1)
220 return str(self.displayhook.prompt1)
226
227
228 def generate_output_prompt(self):
229 return str(self.outputcache.prompt_out)
230
221
231
222
232 def shell_hook(self,cmd):
223 def shell_hook(self,cmd):
@@ -46,7 +46,7 b' from IPython.core.logger import Logger'
46 from IPython.core.magic import Magic
46 from IPython.core.magic import Magic
47 from IPython.core.plugin import PluginManager
47 from IPython.core.plugin import PluginManager
48 from IPython.core.prefilter import PrefilterManager
48 from IPython.core.prefilter import PrefilterManager
49 from IPython.core.prompts import CachedOutput
49 from IPython.core.displayhook import DisplayHook
50 import IPython.core.hooks
50 import IPython.core.hooks
51 from IPython.external.Itpl import ItplNS
51 from IPython.external.Itpl import ItplNS
52 from IPython.utils import PyColorize
52 from IPython.utils import PyColorize
@@ -62,7 +62,7 b' from IPython.utils.syspathcontext import prepended_to_syspath'
62 from IPython.utils.text import num_ini_spaces
62 from IPython.utils.text import num_ini_spaces
63 from IPython.utils.warn import warn, error, fatal
63 from IPython.utils.warn import warn, error, fatal
64 from IPython.utils.traitlets import (
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 # from IPython.utils import growl
68 # from IPython.utils import growl
@@ -105,23 +105,6 b' class SpaceInInput(exceptions.Exception): pass'
105
105
106 class Bunch: pass
106 class Bunch: pass
107
107
108 class SyntaxTB(ultratb.ListTB):
109 """Extension which holds some state: the last exception value"""
110
111 def __init__(self,color_scheme = 'NoColor'):
112 ultratb.ListTB.__init__(self,color_scheme)
113 self.last_syntax_error = None
114
115 def __call__(self, etype, value, elist):
116 self.last_syntax_error = value
117 ultratb.ListTB.__call__(self,etype,value,elist)
118
119 def clear_err_state(self):
120 """Return the current error state and clear it"""
121 e = self.last_syntax_error
122 self.last_syntax_error = None
123 return e
124
125
108
126 def get_default_colors():
109 def get_default_colors():
127 if sys.platform=='darwin':
110 if sys.platform=='darwin':
@@ -163,6 +146,7 b' class InteractiveShell(Configurable, Magic):'
163 default_value=get_default_colors(), config=True)
146 default_value=get_default_colors(), config=True)
164 debug = CBool(False, config=True)
147 debug = CBool(False, config=True)
165 deep_reload = CBool(False, config=True)
148 deep_reload = CBool(False, config=True)
149 displayhook_class = Type(DisplayHook)
166 filename = Str("<ipython console>")
150 filename = Str("<ipython console>")
167 ipython_dir= Unicode('', config=True) # Set to get_ipython_dir() in __init__
151 ipython_dir= Unicode('', config=True) # Set to get_ipython_dir() in __init__
168 logstart = CBool(False, config=True)
152 logstart = CBool(False, config=True)
@@ -206,8 +190,8 b' class InteractiveShell(Configurable, Magic):'
206 # TODO: this part of prompt management should be moved to the frontends.
190 # TODO: this part of prompt management should be moved to the frontends.
207 # Use custom TraitTypes that convert '0'->'' and '\\n'->'\n'
191 # Use custom TraitTypes that convert '0'->'' and '\\n'->'\n'
208 separate_in = SeparateStr('\n', config=True)
192 separate_in = SeparateStr('\n', config=True)
209 separate_out = SeparateStr('', config=True)
193 separate_out = SeparateStr('\n', config=True)
210 separate_out2 = SeparateStr('', config=True)
194 separate_out2 = SeparateStr('\n', config=True)
211 system_header = Str('IPython system call: ', config=True)
195 system_header = Str('IPython system call: ', config=True)
212 system_verbose = CBool(False, config=True)
196 system_verbose = CBool(False, config=True)
213 wildcards_case_sensitive = CBool(True, config=True)
197 wildcards_case_sensitive = CBool(True, config=True)
@@ -428,26 +412,27 b' class InteractiveShell(Configurable, Magic):'
428 IPython.utils.io.Term = Term
412 IPython.utils.io.Term = Term
429
413
430 def init_prompts(self):
414 def init_prompts(self):
431 # Initialize cache, set in/out prompts and printing system
415 # TODO: This is a pass for now because the prompts are managed inside
432 self.outputcache = CachedOutput(self,
416 # the DisplayHook. Once there is a separate prompt manager, this
433 self.cache_size,
417 # will initialize that object and all prompt related information.
434 self.pprint,
418 pass
435 input_sep = self.separate_in,
436 output_sep = self.separate_out,
437 output_sep2 = self.separate_out2,
438 ps1 = self.prompt_in1,
439 ps2 = self.prompt_in2,
440 ps_out = self.prompt_out,
441 pad_left = self.prompts_pad_left)
442
443 # user may have over-ridden the default print hook:
444 try:
445 self.outputcache.__class__.display = self.hooks.display
446 except AttributeError:
447 pass
448
419
449 def init_displayhook(self):
420 def init_displayhook(self):
450 self.display_trap = DisplayTrap(hook=self.outputcache)
421 # Initialize displayhook, set in/out prompts and printing system
422 self.displayhook = self.displayhook_class(
423 shell=self,
424 cache_size=self.cache_size,
425 input_sep = self.separate_in,
426 output_sep = self.separate_out,
427 output_sep2 = self.separate_out2,
428 ps1 = self.prompt_in1,
429 ps2 = self.prompt_in2,
430 ps_out = self.prompt_out,
431 pad_left = self.prompts_pad_left
432 )
433 # This is a context manager that installs/revmoes the displayhook at
434 # the appropriate time.
435 self.display_trap = DisplayTrap(hook=self.displayhook)
451
436
452 def init_reload_doctest(self):
437 def init_reload_doctest(self):
453 # Do a proper resetting of doctest, including the necessary displayhook
438 # Do a proper resetting of doctest, including the necessary displayhook
@@ -1096,13 +1081,61 b' class InteractiveShell(Configurable, Magic):'
1096 readline.read_history_file(self.histfile)
1081 readline.read_history_file(self.histfile)
1097 return wrapper
1082 return wrapper
1098
1083
1084 def get_history(self, index=None, raw=False, output=True):
1085 """Get the history list.
1086
1087 Get the input and output history.
1088
1089 Parameters
1090 ----------
1091 index : n or (n1, n2) or None
1092 If n, then the last entries. If a tuple, then all in
1093 range(n1, n2). If None, then all entries. Raises IndexError if
1094 the format of index is incorrect.
1095 raw : bool
1096 If True, return the raw input.
1097 output : bool
1098 If True, then return the output as well.
1099
1100 Returns
1101 -------
1102 If output is True, then return a dict of tuples, keyed by the prompt
1103 numbers and with values of (input, output). If output is False, then
1104 a dict, keyed by the prompt number with the values of input. Raises
1105 IndexError if no history is found.
1106 """
1107 if raw:
1108 input_hist = self.input_hist_raw
1109 else:
1110 input_hist = self.input_hist
1111 if output:
1112 output_hist = self.user_ns['Out']
1113 n = len(input_hist)
1114 if index is None:
1115 start=0; stop=n
1116 elif isinstance(index, int):
1117 start=n-index; stop=n
1118 elif isinstance(index, tuple) and len(index) == 2:
1119 start=index[0]; stop=index[1]
1120 else:
1121 raise IndexError('Not a valid index for the input history: %r' % index)
1122 hist = {}
1123 for i in range(start, stop):
1124 if output:
1125 hist[i] = (input_hist[i], output_hist.get(i))
1126 else:
1127 hist[i] = input_hist[i]
1128 if len(hist)==0:
1129 raise IndexError('No history for range of indices: %r' % index)
1130 return hist
1131
1099 #-------------------------------------------------------------------------
1132 #-------------------------------------------------------------------------
1100 # Things related to exception handling and tracebacks (not debugging)
1133 # Things related to exception handling and tracebacks (not debugging)
1101 #-------------------------------------------------------------------------
1134 #-------------------------------------------------------------------------
1102
1135
1103 def init_traceback_handlers(self, custom_exceptions):
1136 def init_traceback_handlers(self, custom_exceptions):
1104 # Syntax error handler.
1137 # Syntax error handler.
1105 self.SyntaxTB = SyntaxTB(color_scheme='NoColor')
1138 self.SyntaxTB = ultratb.SyntaxTB(color_scheme='NoColor')
1106
1139
1107 # The interactive one is initialized with an offset, meaning we always
1140 # The interactive one is initialized with an offset, meaning we always
1108 # want to remove the topmost item in the traceback, which is our own
1141 # want to remove the topmost item in the traceback, which is our own
@@ -1482,8 +1515,9 b' class InteractiveShell(Configurable, Magic):'
1482 #-------------------------------------------------------------------------
1515 #-------------------------------------------------------------------------
1483
1516
1484 def init_magics(self):
1517 def init_magics(self):
1485 # Set user colors (don't do it in the constructor above so that it
1518 # FIXME: Move the color initialization to the DisplayHook, which
1486 # doesn't crash if colors option is invalid)
1519 # should be split into a prompt manager and displayhook. We probably
1520 # even need a centralize colors management object.
1487 self.magic_colors(self.colors)
1521 self.magic_colors(self.colors)
1488 # History was moved to a separate module
1522 # History was moved to a separate module
1489 from . import history
1523 from . import history
@@ -182,14 +182,14 b' which already exists. But you must first start the logging process with'
182
182
183 # update the auto _i tables
183 # update the auto _i tables
184 #print '***logging line',line_mod # dbg
184 #print '***logging line',line_mod # dbg
185 #print '***cache_count', self.shell.outputcache.prompt_count # dbg
185 #print '***cache_count', self.shell.displayhook.prompt_count # dbg
186 try:
186 try:
187 input_hist = self.shell.user_ns['_ih']
187 input_hist = self.shell.user_ns['_ih']
188 except:
188 except:
189 #print 'userns:',self.shell.user_ns.keys() # dbg
189 #print 'userns:',self.shell.user_ns.keys() # dbg
190 return
190 return
191
191
192 out_cache = self.shell.outputcache
192 out_cache = self.shell.displayhook
193
193
194 # add blank lines if the input cache fell out of sync.
194 # add blank lines if the input cache fell out of sync.
195 if out_cache.do_full_cache and \
195 if out_cache.do_full_cache and \
@@ -208,15 +208,15 b' which already exists. But you must first start the logging process with'
208
208
209 # hackish access to top-level namespace to create _i1,_i2... dynamically
209 # hackish access to top-level namespace to create _i1,_i2... dynamically
210 to_main = {'_i':self._i,'_ii':self._ii,'_iii':self._iii}
210 to_main = {'_i':self._i,'_ii':self._ii,'_iii':self._iii}
211 if self.shell.outputcache.do_full_cache:
211 if self.shell.displayhook.do_full_cache:
212 in_num = self.shell.outputcache.prompt_count
212 in_num = self.shell.displayhook.prompt_count
213
213
214 # but if the opposite is true (a macro can produce multiple inputs
214 # but if the opposite is true (a macro can produce multiple inputs
215 # with no output display called), then bring the output counter in
215 # with no output display called), then bring the output counter in
216 # sync:
216 # sync:
217 last_num = len(input_hist)-1
217 last_num = len(input_hist)-1
218 if in_num != last_num:
218 if in_num != last_num:
219 in_num = self.shell.outputcache.prompt_count = last_num
219 in_num = self.shell.displayhook.prompt_count = last_num
220 new_i = '_i%s' % in_num
220 new_i = '_i%s' % in_num
221 if continuation:
221 if continuation:
222 self._i00 = '%s%s\n' % (self.shell.user_ns[new_i],line_mod)
222 self._i00 = '%s%s\n' % (self.shell.user_ns[new_i],line_mod)
@@ -2378,7 +2378,7 b' Currently the magic system has the following functions:\\n"""'
2378 # use last_call to remember the state of the previous call, but don't
2378 # use last_call to remember the state of the previous call, but don't
2379 # let it be clobbered by successive '-p' calls.
2379 # let it be clobbered by successive '-p' calls.
2380 try:
2380 try:
2381 last_call[0] = self.shell.outputcache.prompt_count
2381 last_call[0] = self.shell.displayhook.prompt_count
2382 if not opts_p:
2382 if not opts_p:
2383 last_call[1] = parameter_s
2383 last_call[1] = parameter_s
2384 except:
2384 except:
@@ -2566,12 +2566,12 b' Defaulting color scheme to \'NoColor\'"""'
2566
2566
2567 # Set prompt colors
2567 # Set prompt colors
2568 try:
2568 try:
2569 shell.outputcache.set_colors(new_scheme)
2569 shell.displayhook.set_colors(new_scheme)
2570 except:
2570 except:
2571 color_switch_err('prompt')
2571 color_switch_err('prompt')
2572 else:
2572 else:
2573 shell.colors = \
2573 shell.colors = \
2574 shell.outputcache.color_table.active_scheme_name
2574 shell.displayhook.color_table.active_scheme_name
2575 # Set exception colors
2575 # Set exception colors
2576 try:
2576 try:
2577 shell.InteractiveTB.set_colors(scheme = new_scheme)
2577 shell.InteractiveTB.set_colors(scheme = new_scheme)
@@ -2889,7 +2889,7 b' Defaulting color scheme to \'NoColor\'"""'
2889 if ps:
2889 if ps:
2890 try:
2890 try:
2891 os.chdir(os.path.expanduser(ps))
2891 os.chdir(os.path.expanduser(ps))
2892 if self.shell.term_title:
2892 if hasattr(self.shell, 'term_title') and self.shell.term_title:
2893 set_term_title('IPython: ' + abbrev_cwd())
2893 set_term_title('IPython: ' + abbrev_cwd())
2894 except OSError:
2894 except OSError:
2895 print sys.exc_info()[1]
2895 print sys.exc_info()[1]
@@ -2902,7 +2902,7 b' Defaulting color scheme to \'NoColor\'"""'
2902
2902
2903 else:
2903 else:
2904 os.chdir(self.shell.home_dir)
2904 os.chdir(self.shell.home_dir)
2905 if self.shell.term_title:
2905 if hasattr(self.shell, 'term_title') and self.shell.term_title:
2906 set_term_title('IPython: ' + '~')
2906 set_term_title('IPython: ' + '~')
2907 cwd = os.getcwd()
2907 cwd = os.getcwd()
2908 dhist = self.shell.user_ns['_dh']
2908 dhist = self.shell.user_ns['_dh']
@@ -3439,7 +3439,7 b' Defaulting color scheme to \'NoColor\'"""'
3439
3439
3440 # Shorthands
3440 # Shorthands
3441 shell = self.shell
3441 shell = self.shell
3442 oc = shell.outputcache
3442 oc = shell.displayhook
3443 meta = shell.meta
3443 meta = shell.meta
3444 # dstore is a data store kept in the instance metadata bag to track any
3444 # dstore is a data store kept in the instance metadata bag to track any
3445 # changes we make, so we can undo them later.
3445 # changes we make, so we can undo them later.
@@ -405,7 +405,7 b' class PrefilterManager(Configurable):'
405 normal_handler = self.get_handler_by_name('normal')
405 normal_handler = self.get_handler_by_name('normal')
406 if not stripped:
406 if not stripped:
407 if not continue_prompt:
407 if not continue_prompt:
408 self.shell.outputcache.prompt_count -= 1
408 self.shell.displayhook.prompt_count -= 1
409
409
410 return normal_handler.handle(line_info)
410 return normal_handler.handle(line_info)
411
411
@@ -916,7 +916,7 b' class AutoHandler(PrefilterHandler):'
916 newcmd = '%s(%s)' % (ifun.rstrip(), the_rest)
916 newcmd = '%s(%s)' % (ifun.rstrip(), the_rest)
917
917
918 if auto_rewrite:
918 if auto_rewrite:
919 rw = self.shell.outputcache.prompt1.auto_rewrite() + newcmd
919 rw = self.shell.displayhook.prompt1.auto_rewrite() + newcmd
920
920
921 try:
921 try:
922 # plain ascii works better w/ pyreadline, on some machines, so
922 # plain ascii works better w/ pyreadline, on some machines, so
@@ -1,19 +1,24 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """
2 """Classes for handling input/output prompts.
3 Classes for handling input/output prompts.
3
4 Authors:
5
6 * Fernando Perez
7 * Brian Granger
4 """
8 """
5
9
6 #*****************************************************************************
10 #-----------------------------------------------------------------------------
7 # Copyright (C) 2008-2009 The IPython Development Team
11 # Copyright (C) 2008-2010 The IPython Development Team
8 # Copyright (C) 2001-2007 Fernando Perez <fperez@colorado.edu>
12 # Copyright (C) 2001-2007 Fernando Perez <fperez@colorado.edu>
9 #
13 #
10 # Distributed under the terms of the BSD License. The full license is in
14 # Distributed under the terms of the BSD License. The full license is in
11 # the file COPYING, distributed as part of this software.
15 # the file COPYING, distributed as part of this software.
12 #*****************************************************************************
16 #-----------------------------------------------------------------------------
13
17
14 #****************************************************************************
18 #-----------------------------------------------------------------------------
19 # Imports
20 #-----------------------------------------------------------------------------
15
21
16 import __builtin__
17 import os
22 import os
18 import re
23 import re
19 import socket
24 import socket
@@ -21,14 +26,11 b' import sys'
21
26
22 from IPython.core import release
27 from IPython.core import release
23 from IPython.external.Itpl import ItplNS
28 from IPython.external.Itpl import ItplNS
24 from IPython.core.error import TryNext
25 from IPython.utils import coloransi
29 from IPython.utils import coloransi
26 import IPython.utils.generics
27 from IPython.utils.warn import warn
28 import IPython.utils.io
29
30
30 #****************************************************************************
31 #-----------------------------------------------------------------------------
31 #Color schemes for Prompts.
32 # Color schemes for prompts
33 #-----------------------------------------------------------------------------
32
34
33 PromptColors = coloransi.ColorSchemeTable()
35 PromptColors = coloransi.ColorSchemeTable()
34 InputColors = coloransi.InputTermColors # just a shorthand
36 InputColors = coloransi.InputTermColors # just a shorthand
@@ -76,6 +78,9 b' PromptColors.add_scheme(__PColLightBG)'
76 del Colors,InputColors
78 del Colors,InputColors
77
79
78 #-----------------------------------------------------------------------------
80 #-----------------------------------------------------------------------------
81 # Utilities
82 #-----------------------------------------------------------------------------
83
79 def multiple_replace(dict, text):
84 def multiple_replace(dict, text):
80 """ Replace in 'text' all occurences of any key in the given
85 """ Replace in 'text' all occurences of any key in the given
81 dictionary by its corresponding value. Returns the new string."""
86 dictionary by its corresponding value. Returns the new string."""
@@ -90,6 +95,7 b' def multiple_replace(dict, text):'
90
95
91 #-----------------------------------------------------------------------------
96 #-----------------------------------------------------------------------------
92 # Special characters that can be used in prompt templates, mainly bash-like
97 # Special characters that can be used in prompt templates, mainly bash-like
98 #-----------------------------------------------------------------------------
93
99
94 # If $HOME isn't defined (Windows), make it an absurd string so that it can
100 # If $HOME isn't defined (Windows), make it an absurd string so that it can
95 # never be expanded out into '~'. Basically anything which can never be a
101 # never be expanded out into '~'. Basically anything which can never be a
@@ -201,6 +207,9 b' for _color in dir(input_colors):'
201 prompt_specials = prompt_specials_nocolor
207 prompt_specials = prompt_specials_nocolor
202
208
203 #-----------------------------------------------------------------------------
209 #-----------------------------------------------------------------------------
210 # More utilities
211 #-----------------------------------------------------------------------------
212
204 def str_safe(arg):
213 def str_safe(arg):
205 """Convert to a string, without ever raising an exception.
214 """Convert to a string, without ever raising an exception.
206
215
@@ -221,6 +230,10 b' def str_safe(arg):'
221 #raise # dbg
230 #raise # dbg
222 return out
231 return out
223
232
233 #-----------------------------------------------------------------------------
234 # Prompt classes
235 #-----------------------------------------------------------------------------
236
224 class BasePrompt(object):
237 class BasePrompt(object):
225 """Interactive prompt similar to Mathematica's."""
238 """Interactive prompt similar to Mathematica's."""
226
239
@@ -234,7 +247,7 b' class BasePrompt(object):'
234 p_template = property(_get_p_template,_set_p_template,
247 p_template = property(_get_p_template,_set_p_template,
235 doc='Template for prompt string creation')
248 doc='Template for prompt string creation')
236
249
237 def __init__(self,cache,sep,prompt,pad_left=False):
250 def __init__(self, cache, sep, prompt, pad_left=False):
238
251
239 # Hack: we access information about the primary prompt through the
252 # Hack: we access information about the primary prompt through the
240 # cache argument. We need this, because we want the secondary prompt
253 # cache argument. We need this, because we want the secondary prompt
@@ -267,17 +280,17 b' class BasePrompt(object):'
267 self.p_str = ItplNS('%s%s%s' %
280 self.p_str = ItplNS('%s%s%s' %
268 ('${self.sep}${self.col_p}',
281 ('${self.sep}${self.col_p}',
269 multiple_replace(prompt_specials, self.p_template),
282 multiple_replace(prompt_specials, self.p_template),
270 '${self.col_norm}'),self.cache.user_ns,loc)
283 '${self.col_norm}'),self.cache.shell.user_ns,loc)
271
284
272 self.p_str_nocolor = ItplNS(multiple_replace(prompt_specials_nocolor,
285 self.p_str_nocolor = ItplNS(multiple_replace(prompt_specials_nocolor,
273 self.p_template),
286 self.p_template),
274 self.cache.user_ns,loc)
287 self.cache.shell.user_ns,loc)
275 except:
288 except:
276 print "Illegal prompt template (check $ usage!):",self.p_template
289 print "Illegal prompt template (check $ usage!):",self.p_template
277 self.p_str = self.p_template
290 self.p_str = self.p_template
278 self.p_str_nocolor = self.p_template
291 self.p_str_nocolor = self.p_template
279
292
280 def write(self,msg): # dbg
293 def write(self, msg):
281 sys.stdout.write(msg)
294 sys.stdout.write(msg)
282 return ''
295 return ''
283
296
@@ -301,7 +314,7 b' class BasePrompt(object):'
301
314
302 # these path filters are put in as methods so that we can control the
315 # these path filters are put in as methods so that we can control the
303 # namespace where the prompt strings get evaluated
316 # namespace where the prompt strings get evaluated
304 def cwd_filt(self,depth):
317 def cwd_filt(self, depth):
305 """Return the last depth elements of the current working directory.
318 """Return the last depth elements of the current working directory.
306
319
307 $HOME is always replaced with '~'.
320 $HOME is always replaced with '~'.
@@ -314,7 +327,7 b' class BasePrompt(object):'
314 else:
327 else:
315 return os.sep
328 return os.sep
316
329
317 def cwd_filt2(self,depth):
330 def cwd_filt2(self, depth):
318 """Return the last depth elements of the current working directory.
331 """Return the last depth elements of the current working directory.
319
332
320 $HOME is always replaced with '~'.
333 $HOME is always replaced with '~'.
@@ -341,11 +354,12 b' class BasePrompt(object):'
341
354
342 return bool(self.p_template)
355 return bool(self.p_template)
343
356
357
344 class Prompt1(BasePrompt):
358 class Prompt1(BasePrompt):
345 """Input interactive prompt similar to Mathematica's."""
359 """Input interactive prompt similar to Mathematica's."""
346
360
347 def __init__(self,cache,sep='\n',prompt='In [\\#]: ',pad_left=True):
361 def __init__(self, cache, sep='\n', prompt='In [\\#]: ', pad_left=True):
348 BasePrompt.__init__(self,cache,sep,prompt,pad_left)
362 BasePrompt.__init__(self, cache, sep, prompt, pad_left)
349
363
350 def set_colors(self):
364 def set_colors(self):
351 self.set_p_str()
365 self.set_p_str()
@@ -357,7 +371,14 b' class Prompt1(BasePrompt):'
357 # auto-call prompts used in the auto_rewrite() method.
371 # auto-call prompts used in the auto_rewrite() method.
358 self.col_p_ni = self.col_p.replace('\001','').replace('\002','')
372 self.col_p_ni = self.col_p.replace('\001','').replace('\002','')
359 self.col_norm_ni = Colors.normal
373 self.col_norm_ni = Colors.normal
360
374
375 def peek_next_prompt(self):
376 """Get the next prompt, but don't increment the counter."""
377 self.cache.prompt_count += 1
378 next_prompt = str_safe(self.p_str)
379 self.cache.prompt_count -= 1
380 return next_prompt
381
361 def __str__(self):
382 def __str__(self):
362 self.cache.prompt_count += 1
383 self.cache.prompt_count += 1
363 self.cache.last_prompt = str_safe(self.p_str_nocolor).split('\n')[-1]
384 self.cache.last_prompt = str_safe(self.p_str_nocolor).split('\n')[-1]
@@ -373,11 +394,12 b' class Prompt1(BasePrompt):'
373 return '%s%s>%s%s' % (self.col_p_ni,'-'*(len(curr)-nrspaces-1),
394 return '%s%s>%s%s' % (self.col_p_ni,'-'*(len(curr)-nrspaces-1),
374 ' '*nrspaces,self.col_norm_ni)
395 ' '*nrspaces,self.col_norm_ni)
375
396
397
376 class PromptOut(BasePrompt):
398 class PromptOut(BasePrompt):
377 """Output interactive prompt similar to Mathematica's."""
399 """Output interactive prompt similar to Mathematica's."""
378
400
379 def __init__(self,cache,sep='',prompt='Out[\\#]: ',pad_left=True):
401 def __init__(self, cache, sep='', prompt='Out[\\#]: ', pad_left=True):
380 BasePrompt.__init__(self,cache,sep,prompt,pad_left)
402 BasePrompt.__init__(self, cache, sep, prompt, pad_left)
381 if not self.p_template:
403 if not self.p_template:
382 self.__str__ = lambda: ''
404 self.__str__ = lambda: ''
383
405
@@ -388,10 +410,11 b' class PromptOut(BasePrompt):'
388 self.col_num = Colors.out_number
410 self.col_num = Colors.out_number
389 self.col_norm = Colors.normal
411 self.col_norm = Colors.normal
390
412
413
391 class Prompt2(BasePrompt):
414 class Prompt2(BasePrompt):
392 """Interactive continuation prompt."""
415 """Interactive continuation prompt."""
393
416
394 def __init__(self,cache,prompt=' .\\D.: ',pad_left=True):
417 def __init__(self, cache, prompt=' .\\D.: ', pad_left=True):
395 self.cache = cache
418 self.cache = cache
396 self.p_template = prompt
419 self.p_template = prompt
397 self.pad_left = pad_left
420 self.pad_left = pad_left
@@ -404,10 +427,10 b' class Prompt2(BasePrompt):'
404 ('${self.col_p2}',
427 ('${self.col_p2}',
405 multiple_replace(prompt_specials, self.p_template),
428 multiple_replace(prompt_specials, self.p_template),
406 '$self.col_norm'),
429 '$self.col_norm'),
407 self.cache.user_ns,loc)
430 self.cache.shell.user_ns,loc)
408 self.p_str_nocolor = ItplNS(multiple_replace(prompt_specials_nocolor,
431 self.p_str_nocolor = ItplNS(multiple_replace(prompt_specials_nocolor,
409 self.p_template),
432 self.p_template),
410 self.cache.user_ns,loc)
433 self.cache.shell.user_ns,loc)
411
434
412 def set_colors(self):
435 def set_colors(self):
413 self.set_p_str()
436 self.set_p_str()
@@ -419,221 +442,3 b' class Prompt2(BasePrompt):'
419 self.col_p = Colors.out_prompt
442 self.col_p = Colors.out_prompt
420 self.col_num = Colors.out_number
443 self.col_num = Colors.out_number
421
444
422
423 #-----------------------------------------------------------------------------
424 class CachedOutput:
425 """Class for printing output from calculations while keeping a cache of
426 reults. It dynamically creates global variables prefixed with _ which
427 contain these results.
428
429 Meant to be used as a sys.displayhook replacement, providing numbered
430 prompts and cache services.
431
432 Initialize with initial and final values for cache counter (this defines
433 the maximum size of the cache."""
434
435 def __init__(self,shell,cache_size,Pprint,
436 colors='NoColor',input_sep='\n',
437 output_sep='\n',output_sep2='',
438 ps1 = None, ps2 = None,ps_out = None,pad_left=True):
439
440 cache_size_min = 3
441 if cache_size <= 0:
442 self.do_full_cache = 0
443 cache_size = 0
444 elif cache_size < cache_size_min:
445 self.do_full_cache = 0
446 cache_size = 0
447 warn('caching was disabled (min value for cache size is %s).' %
448 cache_size_min,level=3)
449 else:
450 self.do_full_cache = 1
451
452 self.cache_size = cache_size
453 self.input_sep = input_sep
454
455 # we need a reference to the user-level namespace
456 self.shell = shell
457 self.user_ns = shell.user_ns
458 # and to the user's input
459 self.input_hist = shell.input_hist
460 # and to the user's logger, for logging output
461 self.logger = shell.logger
462
463 # Set input prompt strings and colors
464 if cache_size == 0:
465 if ps1.find('%n') > -1 or ps1.find(r'\#') > -1 \
466 or ps1.find(r'\N') > -1:
467 ps1 = '>>> '
468 if ps2.find('%n') > -1 or ps2.find(r'\#') > -1 \
469 or ps2.find(r'\N') > -1:
470 ps2 = '... '
471 self.ps1_str = self._set_prompt_str(ps1,'In [\\#]: ','>>> ')
472 self.ps2_str = self._set_prompt_str(ps2,' .\\D.: ','... ')
473 self.ps_out_str = self._set_prompt_str(ps_out,'Out[\\#]: ','')
474
475 self.color_table = PromptColors
476 self.prompt1 = Prompt1(self,sep=input_sep,prompt=self.ps1_str,
477 pad_left=pad_left)
478 self.prompt2 = Prompt2(self,prompt=self.ps2_str,pad_left=pad_left)
479 self.prompt_out = PromptOut(self,sep='',prompt=self.ps_out_str,
480 pad_left=pad_left)
481 self.set_colors(colors)
482
483 # other more normal stuff
484 # b/c each call to the In[] prompt raises it by 1, even the first.
485 self.prompt_count = 0
486 # Store the last prompt string each time, we need it for aligning
487 # continuation and auto-rewrite prompts
488 self.last_prompt = ''
489 self.Pprint = Pprint
490 self.output_sep = output_sep
491 self.output_sep2 = output_sep2
492 self._,self.__,self.___ = '','',''
493 self.pprint_types = map(type,[(),[],{}])
494
495 # these are deliberately global:
496 to_user_ns = {'_':self._,'__':self.__,'___':self.___}
497 self.user_ns.update(to_user_ns)
498
499 def _set_prompt_str(self,p_str,cache_def,no_cache_def):
500 if p_str is None:
501 if self.do_full_cache:
502 return cache_def
503 else:
504 return no_cache_def
505 else:
506 return p_str
507
508 def set_colors(self,colors):
509 """Set the active color scheme and configure colors for the three
510 prompt subsystems."""
511
512 # FIXME: the prompt_specials global should be gobbled inside this
513 # class instead. Do it when cleaning up the whole 3-prompt system.
514 global prompt_specials
515 if colors.lower()=='nocolor':
516 prompt_specials = prompt_specials_nocolor
517 else:
518 prompt_specials = prompt_specials_color
519
520 self.color_table.set_active_scheme(colors)
521 self.prompt1.set_colors()
522 self.prompt2.set_colors()
523 self.prompt_out.set_colors()
524
525 def __call__(self,arg=None):
526 """Printing with history cache management.
527
528 This is invoked everytime the interpreter needs to print, and is
529 activated by setting the variable sys.displayhook to it."""
530
531 # If something injected a '_' variable in __builtin__, delete
532 # ipython's automatic one so we don't clobber that. gettext() in
533 # particular uses _, so we need to stay away from it.
534 if '_' in __builtin__.__dict__:
535 try:
536 del self.user_ns['_']
537 except KeyError:
538 pass
539 if arg is not None:
540 cout_write = IPython.utils.io.Term.cout.write # fast lookup
541 # first handle the cache and counters
542
543 # do not print output if input ends in ';'
544 try:
545 if self.input_hist[self.prompt_count].endswith(';\n'):
546 return
547 except IndexError:
548 # some uses of ipshellembed may fail here
549 pass
550 # don't use print, puts an extra space
551 cout_write(self.output_sep)
552 outprompt = self.shell.hooks.generate_output_prompt()
553 # print "Got prompt: ", outprompt
554 if self.do_full_cache:
555 cout_write(outprompt)
556
557 # and now call a possibly user-defined print mechanism. Note that
558 # self.display typically prints as a side-effect, we don't do any
559 # printing to stdout here.
560 try:
561 manipulated_val = self.display(arg)
562 except TypeError:
563 # If the user's display hook didn't return a string we can
564 # print, we're done. Happens commonly if they return None
565 cout_write('\n')
566 return
567
568 # user display hooks can change the variable to be stored in
569 # output history
570 if manipulated_val is not None:
571 arg = manipulated_val
572
573 # avoid recursive reference when displaying _oh/Out
574 if arg is not self.user_ns['_oh']:
575 self.update(arg)
576
577 if self.logger.log_output:
578 self.logger.log_write(repr(arg),'output')
579 cout_write(self.output_sep2)
580 IPython.utils.io.Term.cout.flush()
581
582 def _display(self,arg):
583 """Default printer method, uses pprint.
584
585 Do ip.set_hook("result_display", my_displayhook) for custom result
586 display, e.g. when your own objects need special formatting.
587 """
588 try:
589 return IPython.utils.generics.result_display(arg)
590 except TryNext:
591 return self.shell.hooks.result_display(arg)
592
593 # Assign the default display method:
594 display = _display
595
596 def update(self,arg):
597 #print '***cache_count', self.cache_count # dbg
598 if len(self.user_ns['_oh']) >= self.cache_size and self.do_full_cache:
599 warn('Output cache limit (currently '+
600 `self.cache_size`+' entries) hit.\n'
601 'Flushing cache and resetting history counter...\n'
602 'The only history variables available will be _,__,___ and _1\n'
603 'with the current result.')
604
605 self.flush()
606 # Don't overwrite '_' and friends if '_' is in __builtin__ (otherwise
607 # we cause buggy behavior for things like gettext).
608 if '_' not in __builtin__.__dict__:
609 self.___ = self.__
610 self.__ = self._
611 self._ = arg
612 self.user_ns.update({'_':self._,'__':self.__,'___':self.___})
613
614 # hackish access to top-level namespace to create _1,_2... dynamically
615 to_main = {}
616 if self.do_full_cache:
617 new_result = '_'+`self.prompt_count`
618 to_main[new_result] = arg
619 self.user_ns.update(to_main)
620 self.user_ns['_oh'][self.prompt_count] = arg
621
622 def flush(self):
623 if not self.do_full_cache:
624 raise ValueError,"You shouldn't have reached the cache flush "\
625 "if full caching is not enabled!"
626 # delete auto-generated vars from global namespace
627
628 for n in range(1,self.prompt_count + 1):
629 key = '_'+`n`
630 try:
631 del self.user_ns[key]
632 except: pass
633 self.user_ns['_oh'].clear()
634
635 if '_' not in __builtin__.__dict__:
636 self.user_ns.update({'_':None,'__':None, '___':None})
637 import gc
638 gc.collect() # xxx needed?
639
@@ -178,7 +178,7 b' def test_shist():'
178 # XXX failing for now, until we get clearcmd out of quarantine. But we should
178 # XXX failing for now, until we get clearcmd out of quarantine. But we should
179 # fix this and revert the skip to happen only if numpy is not around.
179 # fix this and revert the skip to happen only if numpy is not around.
180 #@dec.skipif_not_numpy
180 #@dec.skipif_not_numpy
181 @dec.skipknownfailure
181 @dec.skip_known_failure
182 def test_numpy_clear_array_undec():
182 def test_numpy_clear_array_undec():
183 from IPython.extensions import clearcmd
183 from IPython.extensions import clearcmd
184
184
@@ -122,7 +122,7 b' class TestMagicRunPass(tt.TempFileMixin):'
122 """Test that prompts correctly generate after %run"""
122 """Test that prompts correctly generate after %run"""
123 self.run_tmpfile()
123 self.run_tmpfile()
124 _ip = get_ipython()
124 _ip = get_ipython()
125 p2 = str(_ip.outputcache.prompt2).strip()
125 p2 = str(_ip.displayhook.prompt2).strip()
126 nt.assert_equals(p2[:3], '...')
126 nt.assert_equals(p2[:3], '...')
127
127
128
128
@@ -385,7 +385,7 b' class ListTB(TBTools):'
385 IPython.utils.io.Term.cerr.write(self.text(etype,value,elist))
385 IPython.utils.io.Term.cerr.write(self.text(etype,value,elist))
386 IPython.utils.io.Term.cerr.write('\n')
386 IPython.utils.io.Term.cerr.write('\n')
387
387
388 def text(self, etype, value, elist, context=5):
388 def structured_traceback(self, etype, value, elist, context=5):
389 """Return a color formatted string with the traceback info.
389 """Return a color formatted string with the traceback info.
390
390
391 Parameters
391 Parameters
@@ -414,6 +414,12 b' class ListTB(TBTools):'
414 for line in lines[:-1]:
414 for line in lines[:-1]:
415 out_string.append(" "+line)
415 out_string.append(" "+line)
416 out_string.append(lines[-1])
416 out_string.append(lines[-1])
417 return out_string
418
419 def text(self, etype, value, elist, context=5):
420 out_string = ListTB.structured_traceback(
421 self, etype, value, elist, context
422 )
417 return ''.join(out_string)
423 return ''.join(out_string)
418
424
419 def _format_list(self, extracted_list):
425 def _format_list(self, extracted_list):
@@ -569,7 +575,7 b' class VerboseTB(TBTools):'
569 self.long_header = long_header
575 self.long_header = long_header
570 self.include_vars = include_vars
576 self.include_vars = include_vars
571
577
572 def text(self, etype, evalue, etb, context=5):
578 def structured_traceback(self, etype, evalue, etb, context=5):
573 """Return a nice text document describing the traceback."""
579 """Return a nice text document describing the traceback."""
574
580
575 # some locals
581 # some locals
@@ -861,7 +867,14 b' class VerboseTB(TBTools):'
861 # vds: <<
867 # vds: <<
862
868
863 # return all our info assembled as a single string
869 # return all our info assembled as a single string
864 return '%s\n\n%s\n%s' % (head,'\n'.join(frames),''.join(exception[0]) )
870 # return '%s\n\n%s\n%s' % (head,'\n'.join(frames),''.join(exception[0]) )
871 return [head] + frames + [''.join(exception[0])]
872
873 def text(self, etype, evalue, etb, context=5):
874 tb_list = VerboseTB.structured_traceback(
875 self, etype, evalue, etb, context
876 )
877 return '\n'.join(tb_list)
865
878
866 def debugger(self,force=False):
879 def debugger(self,force=False):
867 """Call up the pdb debugger if desired, always clean up the tb
880 """Call up the pdb debugger if desired, always clean up the tb
@@ -957,17 +970,13 b' class FormattedTB(VerboseTB,ListTB):'
957 else:
970 else:
958 return None
971 return None
959
972
960 def text(self, etype, value, tb,context=5,mode=None):
973 def structured_traceback(self, etype, value, tb, context=5, mode=None):
961 """Return formatted traceback.
974 mode = self.mode if mode is None else mode
962
963 If the optional mode parameter is given, it overrides the current
964 mode."""
965
966 if mode is None:
967 mode = self.mode
968 if mode in self.verbose_modes:
975 if mode in self.verbose_modes:
969 # verbose modes need a full traceback
976 # Verbose modes need a full traceback
970 return VerboseTB.text(self,etype, value, tb,context=5)
977 return VerboseTB.structured_traceback(
978 self, etype, value, tb, context
979 )
971 else:
980 else:
972 # We must check the source cache because otherwise we can print
981 # We must check the source cache because otherwise we can print
973 # out-of-date source code.
982 # out-of-date source code.
@@ -976,7 +985,19 b' class FormattedTB(VerboseTB,ListTB):'
976 elist = self._extract_tb(tb)
985 elist = self._extract_tb(tb)
977 if len(elist) > self.tb_offset:
986 if len(elist) > self.tb_offset:
978 del elist[:self.tb_offset]
987 del elist[:self.tb_offset]
979 return ListTB.text(self,etype,value,elist)
988 return ListTB.structured_traceback(
989 self, etype, value, elist, context
990 )
991
992 def text(self, etype, value, tb, context=5, mode=None):
993 """Return formatted traceback.
994
995 If the optional mode parameter is given, it overrides the current
996 mode."""
997 tb_list = FormattedTB.structured_traceback(
998 self, etype, value, tb, context, mode
999 )
1000 return '\n'.join(tb_list)
980
1001
981 def set_mode(self,mode=None):
1002 def set_mode(self,mode=None):
982 """Switch to the desired mode.
1003 """Switch to the desired mode.
@@ -1051,13 +1072,23 b' class AutoFormattedTB(FormattedTB):'
1051 except KeyboardInterrupt:
1072 except KeyboardInterrupt:
1052 print "\nKeyboardInterrupt"
1073 print "\nKeyboardInterrupt"
1053
1074
1054 def text(self,etype=None,value=None,tb=None,context=5,mode=None):
1075 def structured_traceback(self, etype=None, value=None, tb=None,
1076 context=5, mode=None):
1055 if etype is None:
1077 if etype is None:
1056 etype,value,tb = sys.exc_info()
1078 etype,value,tb = sys.exc_info()
1057 self.tb = tb
1079 self.tb = tb
1058 return FormattedTB.text(self,etype,value,tb,context=5,mode=mode)
1080 return FormattedTB.structured_traceback(
1081 self, etype, value, tb, context, mode
1082 )
1083
1084 def text(self, etype=None, value=None, tb=None, context=5, mode=None):
1085 tb_list = AutoFormattedTB.structured_traceback(
1086 self, etype, value, tb, context, mode
1087 )
1088 return '\n'.join(tb_list)
1059
1089
1060 #---------------------------------------------------------------------------
1090 #---------------------------------------------------------------------------
1091
1061 # A simple class to preserve Nathan's original functionality.
1092 # A simple class to preserve Nathan's original functionality.
1062 class ColorTB(FormattedTB):
1093 class ColorTB(FormattedTB):
1063 """Shorthand to initialize a FormattedTB in Linux colors mode."""
1094 """Shorthand to initialize a FormattedTB in Linux colors mode."""
@@ -1065,6 +1096,24 b' class ColorTB(FormattedTB):'
1065 FormattedTB.__init__(self,color_scheme=color_scheme,
1096 FormattedTB.__init__(self,color_scheme=color_scheme,
1066 call_pdb=call_pdb)
1097 call_pdb=call_pdb)
1067
1098
1099
1100 class SyntaxTB(ListTB):
1101 """Extension which holds some state: the last exception value"""
1102
1103 def __init__(self,color_scheme = 'NoColor'):
1104 ListTB.__init__(self,color_scheme)
1105 self.last_syntax_error = None
1106
1107 def __call__(self, etype, value, elist):
1108 self.last_syntax_error = value
1109 ListTB.__call__(self,etype,value,elist)
1110
1111 def clear_err_state(self):
1112 """Return the current error state and clear it"""
1113 e = self.last_syntax_error
1114 self.last_syntax_error = None
1115 return e
1116
1068 #----------------------------------------------------------------------------
1117 #----------------------------------------------------------------------------
1069 # module testing (minimal)
1118 # module testing (minimal)
1070 if __name__ == "__main__":
1119 if __name__ == "__main__":
@@ -46,6 +46,7 b' class TestPrettyResultDisplay(TestCase):'
46 self.ip = InteractiveShellStub()
46 self.ip = InteractiveShellStub()
47 self.prd = pretty_ext.PrettyResultDisplay(shell=self.ip, config=None)
47 self.prd = pretty_ext.PrettyResultDisplay(shell=self.ip, config=None)
48
48
49 @dec.skip_known_failure
49 def test_for_type(self):
50 def test_for_type(self):
50 self.prd.for_type(A, a_pprinter)
51 self.prd.for_type(A, a_pprinter)
51 a = A()
52 a = A()
@@ -94,6 +95,7 b' class TestPrettyInteractively(tt.TempFileMixin):'
94
95
95 # XXX Unfortunately, ipexec_validate fails under win32. If someone helps
96 # XXX Unfortunately, ipexec_validate fails under win32. If someone helps
96 # us write a win32-compatible version, we can reactivate this test.
97 # us write a win32-compatible version, we can reactivate this test.
98 @dec.skip_known_failure
97 @dec.skip_win32
99 @dec.skip_win32
98 def test_printers(self):
100 def test_printers(self):
99 self.mktmp(ipy_src, '.ipy')
101 self.mktmp(ipy_src, '.ipy')
@@ -132,6 +132,37 b' class IPythonWidget(FrontendWidget):'
132 self._set_continuation_prompt(
132 self._set_continuation_prompt(
133 self._make_continuation_prompt(self._prompt), html=True)
133 self._make_continuation_prompt(self._prompt), html=True)
134
134
135 #------ Signal handlers ----------------------------------------------------
136
137 def _handle_execute_error(self, reply):
138 """ Reimplemented for IPython-style traceback formatting.
139 """
140 content = reply['content']
141 traceback_lines = content['traceback'][:]
142 traceback = ''.join(traceback_lines)
143 traceback = traceback.replace(' ', '&nbsp;')
144 traceback = traceback.replace('\n', '<br/>')
145
146 ename = content['ename']
147 ename_styled = '<span class="error">%s</span>' % ename
148 traceback = traceback.replace(ename, ename_styled)
149
150 self._append_html(traceback)
151
152 def _handle_pyout(self, omsg):
153 """ Reimplemented for IPython-style "display hook".
154 """
155 # self._append_html(self._make_out_prompt(self._prompt_count))
156 # TODO: Also look at the output_sep, output_sep2 keys of content.
157 # They are used in terminal based frontends to add empty spaces before
158 # and after the Out[]: prompt. I doubt you want to use them, but they
159 # are there. I am thinking we should even take them out of the msg.
160 prompt_number = omsg['content']['prompt_number']
161 data = omsg['content']['data']
162 self._append_html(self._make_out_prompt(prompt_number))
163 self._save_prompt_block()
164 self._append_plain_text(data + '\n')
165
135 #---------------------------------------------------------------------------
166 #---------------------------------------------------------------------------
136 # 'IPythonWidget' interface
167 # 'IPythonWidget' interface
137 #---------------------------------------------------------------------------
168 #---------------------------------------------------------------------------
@@ -6,6 +6,7 b' from PyQt4 import QtCore'
6 import zmq
6 import zmq
7
7
8 # IPython imports.
8 # IPython imports.
9 from IPython.utils.traitlets import Type
9 from IPython.zmq.kernelmanager import KernelManager, SubSocketChannel, \
10 from IPython.zmq.kernelmanager import KernelManager, SubSocketChannel, \
10 XReqSocketChannel, RepSocketChannel
11 XReqSocketChannel, RepSocketChannel
11 from util import MetaQObjectHasTraits
12 from util import MetaQObjectHasTraits
@@ -161,9 +162,9 b' class QtKernelManager(KernelManager, QtCore.QObject):'
161 stopped_channels = QtCore.pyqtSignal()
162 stopped_channels = QtCore.pyqtSignal()
162
163
163 # Use Qt-specific channel classes that emit signals.
164 # Use Qt-specific channel classes that emit signals.
164 sub_channel_class = QtSubSocketChannel
165 sub_channel_class = Type(QtSubSocketChannel)
165 xreq_channel_class = QtXReqSocketChannel
166 xreq_channel_class = Type(QtXReqSocketChannel)
166 rep_channel_class = QtRepSocketChannel
167 rep_channel_class = Type(QtRepSocketChannel)
167
168
168 #---------------------------------------------------------------------------
169 #---------------------------------------------------------------------------
169 # 'object' interface
170 # 'object' interface
@@ -236,7 +236,7 b' class TerminalInteractiveShell(InteractiveShell):'
236 self.write('\nKeyboardInterrupt\n')
236 self.write('\nKeyboardInterrupt\n')
237 self.resetbuffer()
237 self.resetbuffer()
238 # keep cache in sync with the prompt counter:
238 # keep cache in sync with the prompt counter:
239 self.outputcache.prompt_count -= 1
239 self.displayhook.prompt_count -= 1
240
240
241 if self.autoindent:
241 if self.autoindent:
242 self.indent_current_nsp = 0
242 self.indent_current_nsp = 0
@@ -317,7 +317,7 b" skip_if_not_osx = skipif(sys.platform != 'darwin',"
317 # Other skip decorators
317 # Other skip decorators
318 skipif_not_numpy = skipif(numpy_not_available,"This test requires numpy")
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 # A null 'decorator', useful to make more readable code that needs to pick
322 # A null 'decorator', useful to make more readable code that needs to pick
323 # between different decorators based on OS or other conditions
323 # between different decorators based on OS or other conditions
@@ -127,6 +127,6 b" skip_if_not_osx = skipif(sys.platform != 'darwin',"
127 # Other skip decorators
127 # Other skip decorators
128 skipif_not_numpy = skipif(numpy_not_available,"This test requires numpy")
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
@@ -250,7 +250,7 b' class IPTester(object):'
250 return os.system(' '.join(self.call_args))
250 return os.system(' '.join(self.call_args))
251 else:
251 else:
252 def _run_cmd(self):
252 def _run_cmd(self):
253 #print >> sys.stderr, '*** CMD:', ' '.join(self.call_args) # dbg
253 # print >> sys.stderr, '*** CMD:', ' '.join(self.call_args) # dbg
254 subp = subprocess.Popen(self.call_args)
254 subp = subprocess.Popen(self.call_args)
255 self.pids.append(subp.pid)
255 self.pids.append(subp.pid)
256 # If this fails, the pid will be left in self.pids and cleaned up
256 # If this fails, the pid will be left in self.pids and cleaned up
@@ -33,12 +33,6 b' from IPython.external.simplegeneric import generic'
33
33
34
34
35 @generic
35 @generic
36 def result_display(result):
37 """Print the result of computation."""
38 raise TryNext
39
40
41 @generic
42 def inspect_object(obj):
36 def inspect_object(obj):
43 """Called when you do obj?"""
37 """Called when you do obj?"""
44 raise TryNext
38 raise TryNext
@@ -23,7 +23,6 b' import types'
23
23
24 from IPython.external.path import path
24 from IPython.external.path import path
25
25
26 from IPython.utils.generics import result_display
27 from IPython.utils.io import nlprint
26 from IPython.utils.io import nlprint
28 from IPython.utils.data import flatten
27 from IPython.utils.data import flatten
29
28
@@ -94,14 +93,17 b' class LSString(str):'
94
93
95 p = paths = property(get_paths)
94 p = paths = property(get_paths)
96
95
96 # FIXME: We need to reimplement type specific displayhook and then add this
97 # back as a custom printer. This should also be moved outside utils into the
98 # core.
97
99
98 def print_lsstring(arg):
100 # def print_lsstring(arg):
99 """ Prettier (non-repr-like) and more informative printer for LSString """
101 # """ Prettier (non-repr-like) and more informative printer for LSString """
100 print "LSString (.p, .n, .l, .s available). Value:"
102 # print "LSString (.p, .n, .l, .s available). Value:"
101 print arg
103 # print arg
102
104 #
103
105 #
104 print_lsstring = result_display.when_type(LSString)(print_lsstring)
106 # print_lsstring = result_display.when_type(LSString)(print_lsstring)
105
107
106
108
107 class SList(list):
109 class SList(list):
@@ -248,17 +250,20 b' class SList(list):'
248 return SList([t[1] for t in dsu])
250 return SList([t[1] for t in dsu])
249
251
250
252
251 def print_slist(arg):
253 # FIXME: We need to reimplement type specific displayhook and then add this
252 """ Prettier (non-repr-like) and more informative printer for SList """
254 # back as a custom printer. This should also be moved outside utils into the
253 print "SList (.p, .n, .l, .s, .grep(), .fields(), sort() available):"
255 # core.
254 if hasattr(arg, 'hideonce') and arg.hideonce:
256
255 arg.hideonce = False
257 # def print_slist(arg):
256 return
258 # """ Prettier (non-repr-like) and more informative printer for SList """
257
259 # print "SList (.p, .n, .l, .s, .grep(), .fields(), sort() available):"
258 nlprint(arg)
260 # if hasattr(arg, 'hideonce') and arg.hideonce:
259
261 # arg.hideonce = False
260
262 # return
261 print_slist = result_display.when_type(SList)(print_slist)
263 #
264 # nlprint(arg)
265 #
266 # print_slist = result_display.when_type(SList)(print_slist)
262
267
263
268
264 def esc_quotes(strng):
269 def esc_quotes(strng):
@@ -56,8 +56,8 b' def make_argument_parser():'
56 return parser
56 return parser
57
57
58
58
59 def make_kernel(namespace, kernel_factory, out_stream_factory=OutStream,
59 def make_kernel(namespace, kernel_factory,
60 display_hook_factory=DisplayHook):
60 out_stream_factory=None, display_hook_factory=None):
61 """ Creates a kernel.
61 """ Creates a kernel.
62 """
62 """
63 # Create a context, a session, and the kernel sockets.
63 # Create a context, a session, and the kernel sockets.
@@ -78,9 +78,11 b' def make_kernel(namespace, kernel_factory, out_stream_factory=OutStream,'
78 print >>sys.__stdout__, "REQ Channel on port", req_port
78 print >>sys.__stdout__, "REQ Channel on port", req_port
79
79
80 # Redirect input streams and set a display hook.
80 # Redirect input streams and set a display hook.
81 sys.stdout = out_stream_factory(session, pub_socket, u'stdout')
81 if out_stream_factory:
82 sys.stderr = out_stream_factory(session, pub_socket, u'stderr')
82 sys.stdout = out_stream_factory(session, pub_socket, u'stdout')
83 sys.displayhook = display_hook_factory(session, pub_socket)
83 sys.stderr = out_stream_factory(session, pub_socket, u'stderr')
84 if display_hook_factory:
85 sys.displayhook = display_hook_factory(session, pub_socket)
84
86
85 # Create the kernel.
87 # Create the kernel.
86 return kernel_factory(session=session, reply_socket=reply_socket,
88 return kernel_factory(session=session, reply_socket=reply_socket,
@@ -107,7 +109,7 b' def make_default_main(kernel_factory):'
107 """
109 """
108 def main():
110 def main():
109 namespace = make_argument_parser().parse_args()
111 namespace = make_argument_parser().parse_args()
110 kernel = make_kernel(namespace, kernel_factory)
112 kernel = make_kernel(namespace, kernel_factory, OutStream, DisplayHook)
111 start_kernel(namespace, kernel)
113 start_kernel(namespace, kernel)
112 return main
114 return main
113
115
@@ -16,7 +16,11 b' Things to do:'
16
16
17 # Standard library imports.
17 # Standard library imports.
18 import __builtin__
18 import __builtin__
19 <<<<<<< HEAD
19 from code import CommandCompiler
20 from code import CommandCompiler
21 =======
22 import os
23 >>>>>>> 7425745bce7a04ad0dce41b22f03bebe1d2cdd20
20 import sys
24 import sys
21 import time
25 import time
22 import traceback
26 import traceback
@@ -63,14 +67,19 b' class Kernel(Configurable):'
63
67
64 def __init__(self, **kwargs):
68 def __init__(self, **kwargs):
65 super(Kernel, self).__init__(**kwargs)
69 super(Kernel, self).__init__(**kwargs)
70
71 # Initialize the InteractiveShell subclass
66 self.shell = ZMQInteractiveShell.instance()
72 self.shell = ZMQInteractiveShell.instance()
73 self.shell.displayhook.session = self.session
74 self.shell.displayhook.pub_socket = self.pub_socket
67
75
68 # Protected variables.
76 # Protected variables.
69 self._exec_payload = {}
77 self._exec_payload = {}
70
78
71 # Build dict of handlers for message types
79 # Build dict of handlers for message types
72 msg_types = [ 'execute_request', 'complete_request',
80 msg_types = [ 'execute_request', 'complete_request',
73 'object_info_request' ]
81 'object_info_request', 'prompt_request',
82 'history_request' ]
74 self.handlers = {}
83 self.handlers = {}
75 for msg_type in msg_types:
84 for msg_type in msg_types:
76 self.handlers[msg_type] = getattr(self, msg_type)
85 self.handlers[msg_type] = getattr(self, msg_type)
@@ -173,8 +182,8 b' class Kernel(Configurable):'
173 raw_input = lambda prompt='': self._raw_input(prompt, ident, parent)
182 raw_input = lambda prompt='': self._raw_input(prompt, ident, parent)
174 __builtin__.raw_input = raw_input
183 __builtin__.raw_input = raw_input
175
184
176 # Configure the display hook.
185 # Set the parent message of the display hook.
177 sys.displayhook.set_parent(parent)
186 self.shell.displayhook.set_parent(parent)
178
187
179 self.shell.runlines(code)
188 self.shell.runlines(code)
180 # exec comp_code in self.user_ns, self.user_ns
189 # exec comp_code in self.user_ns, self.user_ns
@@ -192,7 +201,15 b' class Kernel(Configurable):'
192 reply_content = exc_content
201 reply_content = exc_content
193 else:
202 else:
194 reply_content = { 'status' : 'ok', 'payload' : self._exec_payload }
203 reply_content = { 'status' : 'ok', 'payload' : self._exec_payload }
195
204
205 # Compute the prompt information
206 prompt_number = self.shell.displayhook.prompt_count
207 reply_content['prompt_number'] = prompt_number
208 prompt_string = self.shell.displayhook.prompt1.peek_next_prompt()
209 next_prompt = {'prompt_string' : prompt_string,
210 'prompt_number' : prompt_number+1}
211 reply_content['next_prompt'] = next_prompt
212
196 # Flush output before sending the reply.
213 # Flush output before sending the reply.
197 sys.stderr.flush()
214 sys.stderr.flush()
198 sys.stdout.flush()
215 sys.stdout.flush()
@@ -218,6 +235,25 b' class Kernel(Configurable):'
218 msg = self.session.send(self.reply_socket, 'object_info_reply',
235 msg = self.session.send(self.reply_socket, 'object_info_reply',
219 object_info, parent, ident)
236 object_info, parent, ident)
220 print >> sys.__stdout__, msg
237 print >> sys.__stdout__, msg
238
239 def prompt_request(self, ident, parent):
240 prompt_number = self.shell.displayhook.prompt_count
241 prompt_string = self.shell.displayhook.prompt1.peek_next_prompt()
242 content = {'prompt_string' : prompt_string,
243 'prompt_number' : prompt_number+1}
244 msg = self.session.send(self.reply_socket, 'prompt_reply',
245 content, parent, ident)
246 print >> sys.__stdout__, msg
247
248 def history_request(self, ident, parent):
249 output = parent['content'].get('output', True)
250 index = parent['content'].get('index')
251 raw = parent['content'].get('raw', False)
252 hist = self.shell.get_history(index=index, raw=raw, output=output)
253 content = {'history' : hist}
254 msg = self.session.send(self.reply_socket, 'history_reply',
255 content, parent, ident)
256 print >> sys.__stdout__, msg
221
257
222 #---------------------------------------------------------------------------
258 #---------------------------------------------------------------------------
223 # Protected interface
259 # Protected interface
@@ -1,11 +1,49 b''
1 import sys
1 import sys
2 from subprocess import Popen, PIPE
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 ZMQDisplayHook(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 class ZMQInteractiveShell(InteractiveShell):
42 class ZMQInteractiveShell(InteractiveShell):
7 """A subclass of InteractiveShell for ZMQ."""
43 """A subclass of InteractiveShell for ZMQ."""
8
44
45 displayhook_class = Type(ZMQDisplayHook)
46
9 def system(self, cmd):
47 def system(self, cmd):
10 cmd = self.var_expand(cmd, depth=2)
48 cmd = self.var_expand(cmd, depth=2)
11 sys.stdout.flush()
49 sys.stdout.flush()
@@ -17,7 +55,7 b' class ZMQInteractiveShell(InteractiveShell):'
17 for line in p.stderr.read().split('\n'):
55 for line in p.stderr.read().split('\n'):
18 if len(line) > 0:
56 if len(line) > 0:
19 print line
57 print line
20 return p.wait()
58 p.wait()
21
59
22 def init_io(self):
60 def init_io(self):
23 # This will just use sys.stdout and sys.stderr. If you want to
61 # This will just use sys.stdout and sys.stderr. If you want to
@@ -29,3 +67,6 b' class ZMQInteractiveShell(InteractiveShell):'
29 IPython.utils.io.Term = Term
67 IPython.utils.io.Term = Term
30
68
31 InteractiveShellABC.register(ZMQInteractiveShell)
69 InteractiveShellABC.register(ZMQInteractiveShell)
70
71
72
@@ -372,15 +372,18 b' Message type: ``history_request``::'
372
372
373 content = {
373 content = {
374
374
375 # If true, also return output history in the resulting dict.
375 # If True, also return output history in the resulting dict.
376 'output' : bool,
376 'output' : bool,
377
377
378 # This parameter can be one of: A number, a pair of numbers, 'all'
378 # If True, return the raw input history, else the transformed input.
379 'raw' : bool,
380
381 # This parameter can be one of: A number, a pair of numbers, None
379 # If not given, last 40 are returned.
382 # If not given, last 40 are returned.
380 # - number n: return the last n entries.
383 # - number n: return the last n entries.
381 # - pair n1, n2: return entries in the range(n1, n2).
384 # - pair n1, n2: return entries in the range(n1, n2).
382 # - 'all': return all history
385 # - None: return all history
383 'range' : n or (n1, n2) or 'all',
386 'range' : n or (n1, n2) or None,
384
387
385 # If a filter is given, it is treated as a regular expression and only
388 # If a filter is given, it is treated as a regular expression and only
386 # matching entries are returned. re.search() is used to find matches.
389 # matching entries are returned. re.search() is used to find matches.
@@ -390,14 +393,11 b' Message type: ``history_request``::'
390 Message type: ``history_reply``::
393 Message type: ``history_reply``::
391
394
392 content = {
395 content = {
393 # A list of (number, input) pairs
396 # A dict with prompt numbers as keys and either (input, output) or input
394 'input' : list,
397 # as the value depending on whether output was True or False,
395
398 # respectively.
396 # A list of (number, output) pairs
399 'history' : dict,
397 'output' : list,
400 }
398 }
399
400
401 Messages on the PUB/SUB socket
401 Messages on the PUB/SUB socket
402 ==============================
402 ==============================
403
403
General Comments 0
You need to be logged in to leave comments. Login now