##// END OF EJS Templates
Added fix for display hook call output format
Matthew Seal -
Show More
@@ -1,320 +1,320 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """Displayhook for IPython.
2 """Displayhook for IPython.
3
3
4 This defines a callable class that IPython uses for `sys.displayhook`.
4 This defines a callable class that IPython uses for `sys.displayhook`.
5 """
5 """
6
6
7 # Copyright (c) IPython Development Team.
7 # Copyright (c) IPython Development Team.
8 # Distributed under the terms of the Modified BSD License.
8 # Distributed under the terms of the Modified BSD License.
9
9
10 import builtins as builtin_mod
10 import builtins as builtin_mod
11 import sys
11 import sys
12 import io as _io
12 import io as _io
13 import tokenize
13 import tokenize
14
14
15 from traitlets.config.configurable import Configurable
15 from traitlets.config.configurable import Configurable
16 from traitlets import Instance, Float
16 from traitlets import Instance, Float
17 from warnings import warn
17 from warnings import warn
18
18
19 # TODO: Move the various attributes (cache_size, [others now moved]). Some
19 # TODO: Move the various attributes (cache_size, [others now moved]). Some
20 # of these are also attributes of InteractiveShell. They should be on ONE object
20 # of these are also attributes of InteractiveShell. They should be on ONE object
21 # only and the other objects should ask that one object for their values.
21 # only and the other objects should ask that one object for their values.
22
22
23 class DisplayHook(Configurable):
23 class DisplayHook(Configurable):
24 """The custom IPython displayhook to replace sys.displayhook.
24 """The custom IPython displayhook to replace sys.displayhook.
25
25
26 This class does many things, but the basic idea is that it is a callable
26 This class does many things, but the basic idea is that it is a callable
27 that gets called anytime user code returns a value.
27 that gets called anytime user code returns a value.
28 """
28 """
29
29
30 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC',
30 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC',
31 allow_none=True)
31 allow_none=True)
32 exec_result = Instance('IPython.core.interactiveshell.ExecutionResult',
32 exec_result = Instance('IPython.core.interactiveshell.ExecutionResult',
33 allow_none=True)
33 allow_none=True)
34 cull_fraction = Float(0.2)
34 cull_fraction = Float(0.2)
35
35
36 def __init__(self, shell=None, cache_size=1000, **kwargs):
36 def __init__(self, shell=None, cache_size=1000, **kwargs):
37 super(DisplayHook, self).__init__(shell=shell, **kwargs)
37 super(DisplayHook, self).__init__(shell=shell, **kwargs)
38 cache_size_min = 3
38 cache_size_min = 3
39 if cache_size <= 0:
39 if cache_size <= 0:
40 self.do_full_cache = 0
40 self.do_full_cache = 0
41 cache_size = 0
41 cache_size = 0
42 elif cache_size < cache_size_min:
42 elif cache_size < cache_size_min:
43 self.do_full_cache = 0
43 self.do_full_cache = 0
44 cache_size = 0
44 cache_size = 0
45 warn('caching was disabled (min value for cache size is %s).' %
45 warn('caching was disabled (min value for cache size is %s).' %
46 cache_size_min,stacklevel=3)
46 cache_size_min,stacklevel=3)
47 else:
47 else:
48 self.do_full_cache = 1
48 self.do_full_cache = 1
49
49
50 self.cache_size = cache_size
50 self.cache_size = cache_size
51
51
52 # we need a reference to the user-level namespace
52 # we need a reference to the user-level namespace
53 self.shell = shell
53 self.shell = shell
54
54
55 self._,self.__,self.___ = '','',''
55 self._,self.__,self.___ = '','',''
56
56
57 # these are deliberately global:
57 # these are deliberately global:
58 to_user_ns = {'_':self._,'__':self.__,'___':self.___}
58 to_user_ns = {'_':self._,'__':self.__,'___':self.___}
59 self.shell.user_ns.update(to_user_ns)
59 self.shell.user_ns.update(to_user_ns)
60
60
61 @property
61 @property
62 def prompt_count(self):
62 def prompt_count(self):
63 return self.shell.execution_count
63 return self.shell.execution_count
64
64
65 #-------------------------------------------------------------------------
65 #-------------------------------------------------------------------------
66 # Methods used in __call__. Override these methods to modify the behavior
66 # Methods used in __call__. Override these methods to modify the behavior
67 # of the displayhook.
67 # of the displayhook.
68 #-------------------------------------------------------------------------
68 #-------------------------------------------------------------------------
69
69
70 def check_for_underscore(self):
70 def check_for_underscore(self):
71 """Check if the user has set the '_' variable by hand."""
71 """Check if the user has set the '_' variable by hand."""
72 # If something injected a '_' variable in __builtin__, delete
72 # If something injected a '_' variable in __builtin__, delete
73 # ipython's automatic one so we don't clobber that. gettext() in
73 # ipython's automatic one so we don't clobber that. gettext() in
74 # particular uses _, so we need to stay away from it.
74 # particular uses _, so we need to stay away from it.
75 if '_' in builtin_mod.__dict__:
75 if '_' in builtin_mod.__dict__:
76 try:
76 try:
77 user_value = self.shell.user_ns['_']
77 user_value = self.shell.user_ns['_']
78 if user_value is not self._:
78 if user_value is not self._:
79 return
79 return
80 del self.shell.user_ns['_']
80 del self.shell.user_ns['_']
81 except KeyError:
81 except KeyError:
82 pass
82 pass
83
83
84 def quiet(self):
84 def quiet(self):
85 """Should we silence the display hook because of ';'?"""
85 """Should we silence the display hook because of ';'?"""
86 # do not print output if input ends in ';'
86 # do not print output if input ends in ';'
87
87
88 try:
88 try:
89 cell = self.shell.history_manager.input_hist_parsed[-1]
89 cell = self.shell.history_manager.input_hist_parsed[-1]
90 except IndexError:
90 except IndexError:
91 # some uses of ipshellembed may fail here
91 # some uses of ipshellembed may fail here
92 return False
92 return False
93
93
94 sio = _io.StringIO(cell)
94 sio = _io.StringIO(cell)
95 tokens = list(tokenize.generate_tokens(sio.readline))
95 tokens = list(tokenize.generate_tokens(sio.readline))
96
96
97 for token in reversed(tokens):
97 for token in reversed(tokens):
98 if token[0] in (tokenize.ENDMARKER, tokenize.NL, tokenize.NEWLINE, tokenize.COMMENT):
98 if token[0] in (tokenize.ENDMARKER, tokenize.NL, tokenize.NEWLINE, tokenize.COMMENT):
99 continue
99 continue
100 if (token[0] == tokenize.OP) and (token[1] == ';'):
100 if (token[0] == tokenize.OP) and (token[1] == ';'):
101 return True
101 return True
102 else:
102 else:
103 return False
103 return False
104
104
105 def start_displayhook(self):
105 def start_displayhook(self):
106 """Start the displayhook, initializing resources."""
106 """Start the displayhook, initializing resources."""
107 pass
107 pass
108
108
109 def write_output_prompt(self):
109 def write_output_prompt(self):
110 """Write the output prompt.
110 """Write the output prompt.
111
111
112 The default implementation simply writes the prompt to
112 The default implementation simply writes the prompt to
113 ``sys.stdout``.
113 ``sys.stdout``.
114 """
114 """
115 # Use write, not print which adds an extra space.
115 # Use write, not print which adds an extra space.
116 sys.stdout.write(self.shell.separate_out)
116 sys.stdout.write(self.shell.separate_out)
117 outprompt = 'Out[{}]: '.format(self.shell.execution_count)
117 outprompt = 'Out[{}]: '.format(self.shell.execution_count)
118 if self.do_full_cache:
118 if self.do_full_cache:
119 sys.stdout.write(outprompt)
119 sys.stdout.write(outprompt)
120
120
121 def compute_format_data(self, result):
121 def compute_format_data(self, result):
122 """Compute format data of the object to be displayed.
122 """Compute format data of the object to be displayed.
123
123
124 The format data is a generalization of the :func:`repr` of an object.
124 The format data is a generalization of the :func:`repr` of an object.
125 In the default implementation the format data is a :class:`dict` of
125 In the default implementation the format data is a :class:`dict` of
126 key value pair where the keys are valid MIME types and the values
126 key value pair where the keys are valid MIME types and the values
127 are JSON'able data structure containing the raw data for that MIME
127 are JSON'able data structure containing the raw data for that MIME
128 type. It is up to frontends to determine pick a MIME to to use and
128 type. It is up to frontends to determine pick a MIME to to use and
129 display that data in an appropriate manner.
129 display that data in an appropriate manner.
130
130
131 This method only computes the format data for the object and should
131 This method only computes the format data for the object and should
132 NOT actually print or write that to a stream.
132 NOT actually print or write that to a stream.
133
133
134 Parameters
134 Parameters
135 ----------
135 ----------
136 result : object
136 result : object
137 The Python object passed to the display hook, whose format will be
137 The Python object passed to the display hook, whose format will be
138 computed.
138 computed.
139
139
140 Returns
140 Returns
141 -------
141 -------
142 (format_dict, md_dict) : dict
142 (format_dict, md_dict) : dict
143 format_dict is a :class:`dict` whose keys are valid MIME types and values are
143 format_dict is a :class:`dict` whose keys are valid MIME types and values are
144 JSON'able raw data for that MIME type. It is recommended that
144 JSON'able raw data for that MIME type. It is recommended that
145 all return values of this should always include the "text/plain"
145 all return values of this should always include the "text/plain"
146 MIME type representation of the object.
146 MIME type representation of the object.
147 md_dict is a :class:`dict` with the same MIME type keys
147 md_dict is a :class:`dict` with the same MIME type keys
148 of metadata associated with each output.
148 of metadata associated with each output.
149
149
150 """
150 """
151 return self.shell.display_formatter.format(result)
151 return self.shell.display_formatter.format(result)
152
152
153 # This can be set to True by the write_output_prompt method in a subclass
153 # This can be set to True by the write_output_prompt method in a subclass
154 prompt_end_newline = False
154 prompt_end_newline = False
155
155
156 def write_format_data(self, format_dict, md_dict=None):
156 def write_format_data(self, format_dict, md_dict=None):
157 """Write the format data dict to the frontend.
157 """Write the format data dict to the frontend.
158
158
159 This default version of this method simply writes the plain text
159 This default version of this method simply writes the plain text
160 representation of the object to ``sys.stdout``. Subclasses should
160 representation of the object to ``sys.stdout``. Subclasses should
161 override this method to send the entire `format_dict` to the
161 override this method to send the entire `format_dict` to the
162 frontends.
162 frontends.
163
163
164 Parameters
164 Parameters
165 ----------
165 ----------
166 format_dict : dict
166 format_dict : dict
167 The format dict for the object passed to `sys.displayhook`.
167 The format dict for the object passed to `sys.displayhook`.
168 md_dict : dict (optional)
168 md_dict : dict (optional)
169 The metadata dict to be associated with the display data.
169 The metadata dict to be associated with the display data.
170 """
170 """
171 if 'text/plain' not in format_dict:
171 if 'text/plain' not in format_dict:
172 # nothing to do
172 # nothing to do
173 return
173 return
174 # We want to print because we want to always make sure we have a
174 # We want to print because we want to always make sure we have a
175 # newline, even if all the prompt separators are ''. This is the
175 # newline, even if all the prompt separators are ''. This is the
176 # standard IPython behavior.
176 # standard IPython behavior.
177 result_repr = format_dict['text/plain']
177 result_repr = format_dict['text/plain']
178 if '\n' in result_repr:
178 if '\n' in result_repr:
179 # So that multi-line strings line up with the left column of
179 # So that multi-line strings line up with the left column of
180 # the screen, instead of having the output prompt mess up
180 # the screen, instead of having the output prompt mess up
181 # their first line.
181 # their first line.
182 # We use the prompt template instead of the expanded prompt
182 # We use the prompt template instead of the expanded prompt
183 # because the expansion may add ANSI escapes that will interfere
183 # because the expansion may add ANSI escapes that will interfere
184 # with our ability to determine whether or not we should add
184 # with our ability to determine whether or not we should add
185 # a newline.
185 # a newline.
186 if not self.prompt_end_newline:
186 if not self.prompt_end_newline:
187 # But avoid extraneous empty lines.
187 # But avoid extraneous empty lines.
188 result_repr = '\n' + result_repr
188 result_repr = '\n' + result_repr
189
189
190 print(result_repr)
190 print(result_repr)
191
191
192 def update_user_ns(self, result):
192 def update_user_ns(self, result):
193 """Update user_ns with various things like _, __, _1, etc."""
193 """Update user_ns with various things like _, __, _1, etc."""
194
194
195 # Avoid recursive reference when displaying _oh/Out
195 # Avoid recursive reference when displaying _oh/Out
196 if result is not self.shell.user_ns['_oh']:
196 if result is not self.shell.user_ns['_oh']:
197 if len(self.shell.user_ns['_oh']) >= self.cache_size and self.do_full_cache:
197 if len(self.shell.user_ns['_oh']) >= self.cache_size and self.do_full_cache:
198 self.cull_cache()
198 self.cull_cache()
199
199
200 # Don't overwrite '_' and friends if '_' is in __builtin__
200 # Don't overwrite '_' and friends if '_' is in __builtin__
201 # (otherwise we cause buggy behavior for things like gettext). and
201 # (otherwise we cause buggy behavior for things like gettext). and
202 # do not overwrite _, __ or ___ if one of these has been assigned
202 # do not overwrite _, __ or ___ if one of these has been assigned
203 # by the user.
203 # by the user.
204 update_unders = True
204 update_unders = True
205 for unders in ['_'*i for i in range(1,4)]:
205 for unders in ['_'*i for i in range(1,4)]:
206 if not unders in self.shell.user_ns:
206 if not unders in self.shell.user_ns:
207 continue
207 continue
208 if getattr(self, unders) is not self.shell.user_ns.get(unders):
208 if getattr(self, unders) is not self.shell.user_ns.get(unders):
209 update_unders = False
209 update_unders = False
210
210
211 self.___ = self.__
211 self.___ = self.__
212 self.__ = self._
212 self.__ = self._
213 self._ = result
213 self._ = result
214
214
215 if ('_' not in builtin_mod.__dict__) and (update_unders):
215 if ('_' not in builtin_mod.__dict__) and (update_unders):
216 self.shell.push({'_':self._,
216 self.shell.push({'_':self._,
217 '__':self.__,
217 '__':self.__,
218 '___':self.___}, interactive=False)
218 '___':self.___}, interactive=False)
219
219
220 # hackish access to top-level namespace to create _1,_2... dynamically
220 # hackish access to top-level namespace to create _1,_2... dynamically
221 to_main = {}
221 to_main = {}
222 if self.do_full_cache:
222 if self.do_full_cache:
223 new_result = '_%s' % self.prompt_count
223 new_result = '_%s' % self.prompt_count
224 to_main[new_result] = result
224 to_main[new_result] = result
225 self.shell.push(to_main, interactive=False)
225 self.shell.push(to_main, interactive=False)
226 self.shell.user_ns['_oh'][self.prompt_count] = result
226 self.shell.user_ns['_oh'][self.prompt_count] = result
227
227
228 def fill_exec_result(self, result):
228 def fill_exec_result(self, result):
229 if self.exec_result is not None:
229 if self.exec_result is not None:
230 self.exec_result.result = result
230 self.exec_result.result = result
231
231
232 def log_output(self, format_dict):
232 def log_output(self, format_dict):
233 """Log the output."""
233 """Log the output."""
234 if 'text/plain' not in format_dict:
234 if 'text/plain' not in format_dict:
235 # nothing to do
235 # nothing to do
236 return
236 return
237 if self.shell.logger.log_output:
237 if self.shell.logger.log_output:
238 self.shell.logger.log_write(format_dict['text/plain'], 'output')
238 self.shell.logger.log_write(format_dict['text/plain'], 'output')
239 self.shell.history_manager.output_hist_reprs[self.prompt_count] = \
239 self.shell.history_manager.output_hist_reprs[self.prompt_count] = \
240 format_dict['text/plain']
240 format_dict['text/plain']
241
241
242 def finish_displayhook(self):
242 def finish_displayhook(self):
243 """Finish up all displayhook activities."""
243 """Finish up all displayhook activities."""
244 sys.stdout.write(self.shell.separate_out2)
244 sys.stdout.write(self.shell.separate_out2)
245 sys.stdout.flush()
245 sys.stdout.flush()
246
246
247 def __call__(self, result=None):
247 def __call__(self, result=None):
248 """Printing with history cache management.
248 """Printing with history cache management.
249
249
250 This is invoked everytime the interpreter needs to print, and is
250 This is invoked everytime the interpreter needs to print, and is
251 activated by setting the variable sys.displayhook to it.
251 activated by setting the variable sys.displayhook to it.
252 """
252 """
253 self.check_for_underscore()
253 self.check_for_underscore()
254 if result is not None and not self.quiet():
254 if result is not None and not self.quiet():
255 self.start_displayhook()
255 self.start_displayhook()
256 self.write_output_prompt()
256 self.write_output_prompt()
257 format_dict, md_dict = self.compute_format_data(result)
257 format_dict, md_dict = self.compute_format_data(result)
258 self.update_user_ns(result)
258 self.update_user_ns(result)
259 self.fill_exec_result(result)
259 self.fill_exec_result(result)
260 if format_dict:
260 if format_dict:
261 self.write_format_data(format_dict, md_dict)
261 self.write_format_data(format_dict, md_dict)
262 self.log_output(format_dict)
262 self.log_output(format_dict)
263 self.finish_displayhook()
263 self.finish_displayhook()
264
264
265 def cull_cache(self):
265 def cull_cache(self):
266 """Output cache is full, cull the oldest entries"""
266 """Output cache is full, cull the oldest entries"""
267 oh = self.shell.user_ns.get('_oh', {})
267 oh = self.shell.user_ns.get('_oh', {})
268 sz = len(oh)
268 sz = len(oh)
269 cull_count = max(int(sz * self.cull_fraction), 2)
269 cull_count = max(int(sz * self.cull_fraction), 2)
270 warn('Output cache limit (currently {sz} entries) hit.\n'
270 warn('Output cache limit (currently {sz} entries) hit.\n'
271 'Flushing oldest {cull_count} entries.'.format(sz=sz, cull_count=cull_count))
271 'Flushing oldest {cull_count} entries.'.format(sz=sz, cull_count=cull_count))
272
272
273 for i, n in enumerate(sorted(oh)):
273 for i, n in enumerate(sorted(oh)):
274 if i >= cull_count:
274 if i >= cull_count:
275 break
275 break
276 self.shell.user_ns.pop('_%i' % n, None)
276 self.shell.user_ns.pop('_%i' % n, None)
277 oh.pop(n, None)
277 oh.pop(n, None)
278
278
279
279
280 def flush(self):
280 def flush(self):
281 if not self.do_full_cache:
281 if not self.do_full_cache:
282 raise ValueError("You shouldn't have reached the cache flush "
282 raise ValueError("You shouldn't have reached the cache flush "
283 "if full caching is not enabled!")
283 "if full caching is not enabled!")
284 # delete auto-generated vars from global namespace
284 # delete auto-generated vars from global namespace
285
285
286 for n in range(1,self.prompt_count + 1):
286 for n in range(1,self.prompt_count + 1):
287 key = '_'+repr(n)
287 key = '_'+repr(n)
288 try:
288 try:
289 del self.shell.user_ns[key]
289 del self.shell.user_ns[key]
290 except: pass
290 except: pass
291 # In some embedded circumstances, the user_ns doesn't have the
291 # In some embedded circumstances, the user_ns doesn't have the
292 # '_oh' key set up.
292 # '_oh' key set up.
293 oh = self.shell.user_ns.get('_oh', None)
293 oh = self.shell.user_ns.get('_oh', None)
294 if oh is not None:
294 if oh is not None:
295 oh.clear()
295 oh.clear()
296
296
297 # Release our own references to objects:
297 # Release our own references to objects:
298 self._, self.__, self.___ = '', '', ''
298 self._, self.__, self.___ = '', '', ''
299
299
300 if '_' not in builtin_mod.__dict__:
300 if '_' not in builtin_mod.__dict__:
301 self.shell.user_ns.update({'_':None,'__':None, '___':None})
301 self.shell.user_ns.update({'_':None,'__':None, '___':None})
302 import gc
302 import gc
303 # TODO: Is this really needed?
303 # TODO: Is this really needed?
304 # IronPython blocks here forever
304 # IronPython blocks here forever
305 if sys.platform != "cli":
305 if sys.platform != "cli":
306 gc.collect()
306 gc.collect()
307
307
308
308
309 class CapturingDisplayHook(object):
309 class CapturingDisplayHook(object):
310 def __init__(self, shell, outputs=None):
310 def __init__(self, shell, outputs=None):
311 self.shell = shell
311 self.shell = shell
312 if outputs is None:
312 if outputs is None:
313 outputs = []
313 outputs = []
314 self.outputs = outputs
314 self.outputs = outputs
315
315
316 def __call__(self, result=None):
316 def __call__(self, result=None):
317 if result is None:
317 if result is None:
318 return
318 return
319 format_dict, md_dict = self.shell.display_formatter.format(result)
319 format_dict, md_dict = self.shell.display_formatter.format(result)
320 self.outputs.append((format_dict, md_dict))
320 self.outputs.append({ 'data': format_dict, 'metadata': md_dict })
@@ -1,103 +1,111 b''
1 from IPython.testing.tools import AssertPrints, AssertNotPrints
1 from IPython.testing.tools import AssertPrints, AssertNotPrints
2
2
3 ip = get_ipython()
3 ip = get_ipython()
4
4
5 def test_output_displayed():
5 def test_output_displayed():
6 """Checking to make sure that output is displayed"""
6 """Checking to make sure that output is displayed"""
7
7
8 with AssertPrints('2'):
8 with AssertPrints('2'):
9 ip.run_cell('1+1', store_history=True)
9 ip.run_cell('1+1', store_history=True)
10
10
11 with AssertPrints('2'):
11 with AssertPrints('2'):
12 ip.run_cell('1+1 # comment with a semicolon;', store_history=True)
12 ip.run_cell('1+1 # comment with a semicolon;', store_history=True)
13
13
14 with AssertPrints('2'):
14 with AssertPrints('2'):
15 ip.run_cell('1+1\n#commented_out_function();', store_history=True)
15 ip.run_cell('1+1\n#commented_out_function();', store_history=True)
16
16
17
17
18 def test_output_quiet():
18 def test_output_quiet():
19 """Checking to make sure that output is quiet"""
19 """Checking to make sure that output is quiet"""
20
20
21 with AssertNotPrints('2'):
21 with AssertNotPrints('2'):
22 ip.run_cell('1+1;', store_history=True)
22 ip.run_cell('1+1;', store_history=True)
23
23
24 with AssertNotPrints('2'):
24 with AssertNotPrints('2'):
25 ip.run_cell('1+1; # comment with a semicolon', store_history=True)
25 ip.run_cell('1+1; # comment with a semicolon', store_history=True)
26
26
27 with AssertNotPrints('2'):
27 with AssertNotPrints('2'):
28 ip.run_cell('1+1;\n#commented_out_function()', store_history=True)
28 ip.run_cell('1+1;\n#commented_out_function()', store_history=True)
29
29
30 def test_underscore_no_overrite_user():
30 def test_underscore_no_overrite_user():
31 ip.run_cell('_ = 42', store_history=True)
31 ip.run_cell('_ = 42', store_history=True)
32 ip.run_cell('1+1', store_history=True)
32 ip.run_cell('1+1', store_history=True)
33
33
34 with AssertPrints('42'):
34 with AssertPrints('42'):
35 ip.run_cell('print(_)', store_history=True)
35 ip.run_cell('print(_)', store_history=True)
36
36
37 ip.run_cell('del _', store_history=True)
37 ip.run_cell('del _', store_history=True)
38 ip.run_cell('6+6', store_history=True)
38 ip.run_cell('6+6', store_history=True)
39 with AssertPrints('12'):
39 with AssertPrints('12'):
40 ip.run_cell('_', store_history=True)
40 ip.run_cell('_', store_history=True)
41
41
42
42
43 def test_underscore_no_overrite_builtins():
43 def test_underscore_no_overrite_builtins():
44 ip.run_cell("import gettext ; gettext.install('foo')", store_history=True)
44 ip.run_cell("import gettext ; gettext.install('foo')", store_history=True)
45 ip.run_cell('3+3', store_history=True)
45 ip.run_cell('3+3', store_history=True)
46
46
47 with AssertPrints('gettext'):
47 with AssertPrints('gettext'):
48 ip.run_cell('print(_)', store_history=True)
48 ip.run_cell('print(_)', store_history=True)
49
49
50 ip.run_cell('_ = "userset"', store_history=True)
50 ip.run_cell('_ = "userset"', store_history=True)
51
51
52 with AssertPrints('userset'):
52 with AssertPrints('userset'):
53 ip.run_cell('print(_)', store_history=True)
53 ip.run_cell('print(_)', store_history=True)
54 ip.run_cell('import builtins; del builtins._')
54 ip.run_cell('import builtins; del builtins._')
55
55
56
56
57 def test_interactivehooks_ast_modes():
57 def test_interactivehooks_ast_modes():
58 """
58 """
59 Test that ast nodes can be triggered with different modes
59 Test that ast nodes can be triggered with different modes
60 """
60 """
61 saved_mode = ip.ast_node_interactivity
61 saved_mode = ip.ast_node_interactivity
62 ip.ast_node_interactivity = 'last_expr_or_assign'
62 ip.ast_node_interactivity = 'last_expr_or_assign'
63
63
64 try:
64 try:
65 with AssertPrints('2'):
65 with AssertPrints('2'):
66 ip.run_cell('a = 1+1', store_history=True)
66 ip.run_cell('a = 1+1', store_history=True)
67
67
68 with AssertPrints('9'):
68 with AssertPrints('9'):
69 ip.run_cell('b = 1+8 # comment with a semicolon;', store_history=False)
69 ip.run_cell('b = 1+8 # comment with a semicolon;', store_history=False)
70
70
71 with AssertPrints('7'):
71 with AssertPrints('7'):
72 ip.run_cell('c = 1+6\n#commented_out_function();', store_history=True)
72 ip.run_cell('c = 1+6\n#commented_out_function();', store_history=True)
73
73
74 ip.run_cell('d = 11', store_history=True)
74 ip.run_cell('d = 11', store_history=True)
75 with AssertPrints('12'):
75 with AssertPrints('12'):
76 ip.run_cell('d += 1', store_history=True)
76 ip.run_cell('d += 1', store_history=True)
77
77
78 with AssertNotPrints('42'):
78 with AssertNotPrints('42'):
79 ip.run_cell('(u,v) = (41+1, 43-1)')
79 ip.run_cell('(u,v) = (41+1, 43-1)')
80
80
81 finally:
81 finally:
82 ip.ast_node_interactivity = saved_mode
82 ip.ast_node_interactivity = saved_mode
83
83
84 def test_interactivehooks_ast_modes_semi_supress():
84 def test_interactivehooks_ast_modes_semi_supress():
85 """
85 """
86 Test that ast nodes can be triggered with different modes and suppressed
86 Test that ast nodes can be triggered with different modes and suppressed
87 by semicolon
87 by semicolon
88 """
88 """
89 saved_mode = ip.ast_node_interactivity
89 saved_mode = ip.ast_node_interactivity
90 ip.ast_node_interactivity = 'last_expr_or_assign'
90 ip.ast_node_interactivity = 'last_expr_or_assign'
91
91
92 try:
92 try:
93 with AssertNotPrints('2'):
93 with AssertNotPrints('2'):
94 ip.run_cell('x = 1+1;', store_history=True)
94 ip.run_cell('x = 1+1;', store_history=True)
95
95
96 with AssertNotPrints('7'):
96 with AssertNotPrints('7'):
97 ip.run_cell('y = 1+6; # comment with a semicolon', store_history=True)
97 ip.run_cell('y = 1+6; # comment with a semicolon', store_history=True)
98
98
99 with AssertNotPrints('9'):
99 with AssertNotPrints('9'):
100 ip.run_cell('z = 1+8;\n#commented_out_function()', store_history=True)
100 ip.run_cell('z = 1+8;\n#commented_out_function()', store_history=True)
101
101
102 finally:
102 finally:
103 ip.ast_node_interactivity = saved_mode
103 ip.ast_node_interactivity = saved_mode
104
105 def test_capture_display_hook_format():
106 """Tests that the capture display hook conforms to the CapturedIO output format"""
107 hook = CapturingDisplayHook(ip)
108 hook({"foo": "bar"})
109 captured = CapturedIO(sys.stdout, sys.stderr, hook.outputs)
110 # Should not raise with RichOutput transformation error
111 captured.outputs
General Comments 0
You need to be logged in to leave comments. Login now