##// END OF EJS Templates
Merge pull request #9823 from minrk/deprecations...
Thomas Kluyver -
r22743:bf3b45fb merge
parent child Browse files
Show More
@@ -1,295 +1,295 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 from __future__ import print_function
10 from __future__ import print_function
11
11
12 import sys
12 import sys
13 import io as _io
13 import io as _io
14 import tokenize
14 import tokenize
15
15
16 from traitlets.config.configurable import Configurable
16 from traitlets.config.configurable import Configurable
17 from IPython.utils.py3compat import builtin_mod, cast_unicode_py2
17 from IPython.utils.py3compat import builtin_mod, cast_unicode_py2
18 from traitlets import Instance, Float
18 from traitlets import Instance, Float
19 from warnings import warn
19 from warnings import warn
20
20
21 # TODO: Move the various attributes (cache_size, [others now moved]). Some
21 # TODO: Move the various attributes (cache_size, [others now moved]). Some
22 # of these are also attributes of InteractiveShell. They should be on ONE object
22 # of these are also attributes of InteractiveShell. They should be on ONE object
23 # only and the other objects should ask that one object for their values.
23 # only and the other objects should ask that one object for their values.
24
24
25 class DisplayHook(Configurable):
25 class DisplayHook(Configurable):
26 """The custom IPython displayhook to replace sys.displayhook.
26 """The custom IPython displayhook to replace sys.displayhook.
27
27
28 This class does many things, but the basic idea is that it is a callable
28 This class does many things, but the basic idea is that it is a callable
29 that gets called anytime user code returns a value.
29 that gets called anytime user code returns a value.
30 """
30 """
31
31
32 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC',
32 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC',
33 allow_none=True)
33 allow_none=True)
34 exec_result = Instance('IPython.core.interactiveshell.ExecutionResult',
34 exec_result = Instance('IPython.core.interactiveshell.ExecutionResult',
35 allow_none=True)
35 allow_none=True)
36 cull_fraction = Float(0.2)
36 cull_fraction = Float(0.2)
37
37
38 def __init__(self, shell=None, cache_size=1000, **kwargs):
38 def __init__(self, shell=None, cache_size=1000, **kwargs):
39 super(DisplayHook, self).__init__(shell=shell, **kwargs)
39 super(DisplayHook, self).__init__(shell=shell, **kwargs)
40 cache_size_min = 3
40 cache_size_min = 3
41 if cache_size <= 0:
41 if cache_size <= 0:
42 self.do_full_cache = 0
42 self.do_full_cache = 0
43 cache_size = 0
43 cache_size = 0
44 elif cache_size < cache_size_min:
44 elif cache_size < cache_size_min:
45 self.do_full_cache = 0
45 self.do_full_cache = 0
46 cache_size = 0
46 cache_size = 0
47 warn('caching was disabled (min value for cache size is %s).' %
47 warn('caching was disabled (min value for cache size is %s).' %
48 cache_size_min,level=3)
48 cache_size_min,level=3)
49 else:
49 else:
50 self.do_full_cache = 1
50 self.do_full_cache = 1
51
51
52 self.cache_size = cache_size
52 self.cache_size = cache_size
53
53
54 # we need a reference to the user-level namespace
54 # we need a reference to the user-level namespace
55 self.shell = shell
55 self.shell = shell
56
56
57 self._,self.__,self.___ = '','',''
57 self._,self.__,self.___ = '','',''
58
58
59 # these are deliberately global:
59 # these are deliberately global:
60 to_user_ns = {'_':self._,'__':self.__,'___':self.___}
60 to_user_ns = {'_':self._,'__':self.__,'___':self.___}
61 self.shell.user_ns.update(to_user_ns)
61 self.shell.user_ns.update(to_user_ns)
62
62
63 @property
63 @property
64 def prompt_count(self):
64 def prompt_count(self):
65 return self.shell.execution_count
65 return self.shell.execution_count
66
66
67 #-------------------------------------------------------------------------
67 #-------------------------------------------------------------------------
68 # Methods used in __call__. Override these methods to modify the behavior
68 # Methods used in __call__. Override these methods to modify the behavior
69 # of the displayhook.
69 # of the displayhook.
70 #-------------------------------------------------------------------------
70 #-------------------------------------------------------------------------
71
71
72 def check_for_underscore(self):
72 def check_for_underscore(self):
73 """Check if the user has set the '_' variable by hand."""
73 """Check if the user has set the '_' variable by hand."""
74 # If something injected a '_' variable in __builtin__, delete
74 # If something injected a '_' variable in __builtin__, delete
75 # ipython's automatic one so we don't clobber that. gettext() in
75 # ipython's automatic one so we don't clobber that. gettext() in
76 # particular uses _, so we need to stay away from it.
76 # particular uses _, so we need to stay away from it.
77 if '_' in builtin_mod.__dict__:
77 if '_' in builtin_mod.__dict__:
78 try:
78 try:
79 del self.shell.user_ns['_']
79 del self.shell.user_ns['_']
80 except KeyError:
80 except KeyError:
81 pass
81 pass
82
82
83 def quiet(self):
83 def quiet(self):
84 """Should we silence the display hook because of ';'?"""
84 """Should we silence the display hook because of ';'?"""
85 # do not print output if input ends in ';'
85 # do not print output if input ends in ';'
86
86
87 try:
87 try:
88 cell = cast_unicode_py2(self.shell.history_manager.input_hist_parsed[-1])
88 cell = cast_unicode_py2(self.shell.history_manager.input_hist_parsed[-1])
89 except IndexError:
89 except IndexError:
90 # some uses of ipshellembed may fail here
90 # some uses of ipshellembed may fail here
91 return False
91 return False
92
92
93 sio = _io.StringIO(cell)
93 sio = _io.StringIO(cell)
94 tokens = list(tokenize.generate_tokens(sio.readline))
94 tokens = list(tokenize.generate_tokens(sio.readline))
95
95
96 for token in reversed(tokens):
96 for token in reversed(tokens):
97 if token[0] in (tokenize.ENDMARKER, tokenize.NL, tokenize.NEWLINE, tokenize.COMMENT):
97 if token[0] in (tokenize.ENDMARKER, tokenize.NL, tokenize.NEWLINE, tokenize.COMMENT):
98 continue
98 continue
99 if (token[0] == tokenize.OP) and (token[1] == ';'):
99 if (token[0] == tokenize.OP) and (token[1] == ';'):
100 return True
100 return True
101 else:
101 else:
102 return False
102 return False
103
103
104 def start_displayhook(self):
104 def start_displayhook(self):
105 """Start the displayhook, initializing resources."""
105 """Start the displayhook, initializing resources."""
106 pass
106 pass
107
107
108 def write_output_prompt(self):
108 def write_output_prompt(self):
109 """Write the output prompt.
109 """Write the output prompt.
110
110
111 The default implementation simply writes the prompt to
111 The default implementation simply writes the prompt to
112 ``io.stdout``.
112 ``sys.stdout``.
113 """
113 """
114 # Use write, not print which adds an extra space.
114 # Use write, not print which adds an extra space.
115 sys.stdout.write(self.shell.separate_out)
115 sys.stdout.write(self.shell.separate_out)
116 outprompt = 'Out[{}]: '.format(self.shell.execution_count)
116 outprompt = 'Out[{}]: '.format(self.shell.execution_count)
117 if self.do_full_cache:
117 if self.do_full_cache:
118 sys.stdout.write(outprompt)
118 sys.stdout.write(outprompt)
119
119
120 def compute_format_data(self, result):
120 def compute_format_data(self, result):
121 """Compute format data of the object to be displayed.
121 """Compute format data of the object to be displayed.
122
122
123 The format data is a generalization of the :func:`repr` of an object.
123 The format data is a generalization of the :func:`repr` of an object.
124 In the default implementation the format data is a :class:`dict` of
124 In the default implementation the format data is a :class:`dict` of
125 key value pair where the keys are valid MIME types and the values
125 key value pair where the keys are valid MIME types and the values
126 are JSON'able data structure containing the raw data for that MIME
126 are JSON'able data structure containing the raw data for that MIME
127 type. It is up to frontends to determine pick a MIME to to use and
127 type. It is up to frontends to determine pick a MIME to to use and
128 display that data in an appropriate manner.
128 display that data in an appropriate manner.
129
129
130 This method only computes the format data for the object and should
130 This method only computes the format data for the object and should
131 NOT actually print or write that to a stream.
131 NOT actually print or write that to a stream.
132
132
133 Parameters
133 Parameters
134 ----------
134 ----------
135 result : object
135 result : object
136 The Python object passed to the display hook, whose format will be
136 The Python object passed to the display hook, whose format will be
137 computed.
137 computed.
138
138
139 Returns
139 Returns
140 -------
140 -------
141 (format_dict, md_dict) : dict
141 (format_dict, md_dict) : dict
142 format_dict is a :class:`dict` whose keys are valid MIME types and values are
142 format_dict is a :class:`dict` whose keys are valid MIME types and values are
143 JSON'able raw data for that MIME type. It is recommended that
143 JSON'able raw data for that MIME type. It is recommended that
144 all return values of this should always include the "text/plain"
144 all return values of this should always include the "text/plain"
145 MIME type representation of the object.
145 MIME type representation of the object.
146 md_dict is a :class:`dict` with the same MIME type keys
146 md_dict is a :class:`dict` with the same MIME type keys
147 of metadata associated with each output.
147 of metadata associated with each output.
148
148
149 """
149 """
150 return self.shell.display_formatter.format(result)
150 return self.shell.display_formatter.format(result)
151
151
152 # This can be set to True by the write_output_prompt method in a subclass
152 # This can be set to True by the write_output_prompt method in a subclass
153 prompt_end_newline = False
153 prompt_end_newline = False
154
154
155 def write_format_data(self, format_dict, md_dict=None):
155 def write_format_data(self, format_dict, md_dict=None):
156 """Write the format data dict to the frontend.
156 """Write the format data dict to the frontend.
157
157
158 This default version of this method simply writes the plain text
158 This default version of this method simply writes the plain text
159 representation of the object to ``io.stdout``. Subclasses should
159 representation of the object to ``sys.stdout``. Subclasses should
160 override this method to send the entire `format_dict` to the
160 override this method to send the entire `format_dict` to the
161 frontends.
161 frontends.
162
162
163 Parameters
163 Parameters
164 ----------
164 ----------
165 format_dict : dict
165 format_dict : dict
166 The format dict for the object passed to `sys.displayhook`.
166 The format dict for the object passed to `sys.displayhook`.
167 md_dict : dict (optional)
167 md_dict : dict (optional)
168 The metadata dict to be associated with the display data.
168 The metadata dict to be associated with the display data.
169 """
169 """
170 if 'text/plain' not in format_dict:
170 if 'text/plain' not in format_dict:
171 # nothing to do
171 # nothing to do
172 return
172 return
173 # We want to print because we want to always make sure we have a
173 # We want to print because we want to always make sure we have a
174 # newline, even if all the prompt separators are ''. This is the
174 # newline, even if all the prompt separators are ''. This is the
175 # standard IPython behavior.
175 # standard IPython behavior.
176 result_repr = format_dict['text/plain']
176 result_repr = format_dict['text/plain']
177 if '\n' in result_repr:
177 if '\n' in result_repr:
178 # So that multi-line strings line up with the left column of
178 # So that multi-line strings line up with the left column of
179 # the screen, instead of having the output prompt mess up
179 # the screen, instead of having the output prompt mess up
180 # their first line.
180 # their first line.
181 # We use the prompt template instead of the expanded prompt
181 # We use the prompt template instead of the expanded prompt
182 # because the expansion may add ANSI escapes that will interfere
182 # because the expansion may add ANSI escapes that will interfere
183 # with our ability to determine whether or not we should add
183 # with our ability to determine whether or not we should add
184 # a newline.
184 # a newline.
185 if not self.prompt_end_newline:
185 if not self.prompt_end_newline:
186 # But avoid extraneous empty lines.
186 # But avoid extraneous empty lines.
187 result_repr = '\n' + result_repr
187 result_repr = '\n' + result_repr
188
188
189 print(result_repr)
189 print(result_repr)
190
190
191 def update_user_ns(self, result):
191 def update_user_ns(self, result):
192 """Update user_ns with various things like _, __, _1, etc."""
192 """Update user_ns with various things like _, __, _1, etc."""
193
193
194 # Avoid recursive reference when displaying _oh/Out
194 # Avoid recursive reference when displaying _oh/Out
195 if result is not self.shell.user_ns['_oh']:
195 if result is not self.shell.user_ns['_oh']:
196 if len(self.shell.user_ns['_oh']) >= self.cache_size and self.do_full_cache:
196 if len(self.shell.user_ns['_oh']) >= self.cache_size and self.do_full_cache:
197 self.cull_cache()
197 self.cull_cache()
198 # Don't overwrite '_' and friends if '_' is in __builtin__ (otherwise
198 # Don't overwrite '_' and friends if '_' is in __builtin__ (otherwise
199 # we cause buggy behavior for things like gettext).
199 # we cause buggy behavior for things like gettext).
200
200
201 if '_' not in builtin_mod.__dict__:
201 if '_' not in builtin_mod.__dict__:
202 self.___ = self.__
202 self.___ = self.__
203 self.__ = self._
203 self.__ = self._
204 self._ = result
204 self._ = result
205 self.shell.push({'_':self._,
205 self.shell.push({'_':self._,
206 '__':self.__,
206 '__':self.__,
207 '___':self.___}, interactive=False)
207 '___':self.___}, interactive=False)
208
208
209 # hackish access to top-level namespace to create _1,_2... dynamically
209 # hackish access to top-level namespace to create _1,_2... dynamically
210 to_main = {}
210 to_main = {}
211 if self.do_full_cache:
211 if self.do_full_cache:
212 new_result = '_'+repr(self.prompt_count)
212 new_result = '_'+repr(self.prompt_count)
213 to_main[new_result] = result
213 to_main[new_result] = result
214 self.shell.push(to_main, interactive=False)
214 self.shell.push(to_main, interactive=False)
215 self.shell.user_ns['_oh'][self.prompt_count] = result
215 self.shell.user_ns['_oh'][self.prompt_count] = result
216
216
217 def fill_exec_result(self, result):
217 def fill_exec_result(self, result):
218 if self.exec_result is not None:
218 if self.exec_result is not None:
219 self.exec_result.result = result
219 self.exec_result.result = result
220
220
221 def log_output(self, format_dict):
221 def log_output(self, format_dict):
222 """Log the output."""
222 """Log the output."""
223 if 'text/plain' not in format_dict:
223 if 'text/plain' not in format_dict:
224 # nothing to do
224 # nothing to do
225 return
225 return
226 if self.shell.logger.log_output:
226 if self.shell.logger.log_output:
227 self.shell.logger.log_write(format_dict['text/plain'], 'output')
227 self.shell.logger.log_write(format_dict['text/plain'], 'output')
228 self.shell.history_manager.output_hist_reprs[self.prompt_count] = \
228 self.shell.history_manager.output_hist_reprs[self.prompt_count] = \
229 format_dict['text/plain']
229 format_dict['text/plain']
230
230
231 def finish_displayhook(self):
231 def finish_displayhook(self):
232 """Finish up all displayhook activities."""
232 """Finish up all displayhook activities."""
233 sys.stdout.write(self.shell.separate_out2)
233 sys.stdout.write(self.shell.separate_out2)
234 sys.stdout.flush()
234 sys.stdout.flush()
235
235
236 def __call__(self, result=None):
236 def __call__(self, result=None):
237 """Printing with history cache management.
237 """Printing with history cache management.
238
238
239 This is invoked everytime the interpreter needs to print, and is
239 This is invoked everytime the interpreter needs to print, and is
240 activated by setting the variable sys.displayhook to it.
240 activated by setting the variable sys.displayhook to it.
241 """
241 """
242 self.check_for_underscore()
242 self.check_for_underscore()
243 if result is not None and not self.quiet():
243 if result is not None and not self.quiet():
244 self.start_displayhook()
244 self.start_displayhook()
245 self.write_output_prompt()
245 self.write_output_prompt()
246 format_dict, md_dict = self.compute_format_data(result)
246 format_dict, md_dict = self.compute_format_data(result)
247 self.update_user_ns(result)
247 self.update_user_ns(result)
248 self.fill_exec_result(result)
248 self.fill_exec_result(result)
249 if format_dict:
249 if format_dict:
250 self.write_format_data(format_dict, md_dict)
250 self.write_format_data(format_dict, md_dict)
251 self.log_output(format_dict)
251 self.log_output(format_dict)
252 self.finish_displayhook()
252 self.finish_displayhook()
253
253
254 def cull_cache(self):
254 def cull_cache(self):
255 """Output cache is full, cull the oldest entries"""
255 """Output cache is full, cull the oldest entries"""
256 oh = self.shell.user_ns.get('_oh', {})
256 oh = self.shell.user_ns.get('_oh', {})
257 sz = len(oh)
257 sz = len(oh)
258 cull_count = max(int(sz * self.cull_fraction), 2)
258 cull_count = max(int(sz * self.cull_fraction), 2)
259 warn('Output cache limit (currently {sz} entries) hit.\n'
259 warn('Output cache limit (currently {sz} entries) hit.\n'
260 'Flushing oldest {cull_count} entries.'.format(sz=sz, cull_count=cull_count))
260 'Flushing oldest {cull_count} entries.'.format(sz=sz, cull_count=cull_count))
261
261
262 for i, n in enumerate(sorted(oh)):
262 for i, n in enumerate(sorted(oh)):
263 if i >= cull_count:
263 if i >= cull_count:
264 break
264 break
265 self.shell.user_ns.pop('_%i' % n, None)
265 self.shell.user_ns.pop('_%i' % n, None)
266 oh.pop(n, None)
266 oh.pop(n, None)
267
267
268
268
269 def flush(self):
269 def flush(self):
270 if not self.do_full_cache:
270 if not self.do_full_cache:
271 raise ValueError("You shouldn't have reached the cache flush "
271 raise ValueError("You shouldn't have reached the cache flush "
272 "if full caching is not enabled!")
272 "if full caching is not enabled!")
273 # delete auto-generated vars from global namespace
273 # delete auto-generated vars from global namespace
274
274
275 for n in range(1,self.prompt_count + 1):
275 for n in range(1,self.prompt_count + 1):
276 key = '_'+repr(n)
276 key = '_'+repr(n)
277 try:
277 try:
278 del self.shell.user_ns[key]
278 del self.shell.user_ns[key]
279 except: pass
279 except: pass
280 # In some embedded circumstances, the user_ns doesn't have the
280 # In some embedded circumstances, the user_ns doesn't have the
281 # '_oh' key set up.
281 # '_oh' key set up.
282 oh = self.shell.user_ns.get('_oh', None)
282 oh = self.shell.user_ns.get('_oh', None)
283 if oh is not None:
283 if oh is not None:
284 oh.clear()
284 oh.clear()
285
285
286 # Release our own references to objects:
286 # Release our own references to objects:
287 self._, self.__, self.___ = '', '', ''
287 self._, self.__, self.___ = '', '', ''
288
288
289 if '_' not in builtin_mod.__dict__:
289 if '_' not in builtin_mod.__dict__:
290 self.shell.user_ns.update({'_':None,'__':None, '___':None})
290 self.shell.user_ns.update({'_':None,'__':None, '___':None})
291 import gc
291 import gc
292 # TODO: Is this really needed?
292 # TODO: Is this really needed?
293 # IronPython blocks here forever
293 # IronPython blocks here forever
294 if sys.platform != "cli":
294 if sys.platform != "cli":
295 gc.collect()
295 gc.collect()
@@ -1,117 +1,117 b''
1 """An interface for publishing rich data to frontends.
1 """An interface for publishing rich data to frontends.
2
2
3 There are two components of the display system:
3 There are two components of the display system:
4
4
5 * Display formatters, which take a Python object and compute the
5 * Display formatters, which take a Python object and compute the
6 representation of the object in various formats (text, HTML, SVG, etc.).
6 representation of the object in various formats (text, HTML, SVG, etc.).
7 * The display publisher that is used to send the representation data to the
7 * The display publisher that is used to send the representation data to the
8 various frontends.
8 various frontends.
9
9
10 This module defines the logic display publishing. The display publisher uses
10 This module defines the logic display publishing. The display publisher uses
11 the ``display_data`` message type that is defined in the IPython messaging
11 the ``display_data`` message type that is defined in the IPython messaging
12 spec.
12 spec.
13 """
13 """
14
14
15 # Copyright (c) IPython Development Team.
15 # Copyright (c) IPython Development Team.
16 # Distributed under the terms of the Modified BSD License.
16 # Distributed under the terms of the Modified BSD License.
17
17
18 from __future__ import print_function
18 from __future__ import print_function
19
19
20 import sys
20 import sys
21
21
22 from traitlets.config.configurable import Configurable
22 from traitlets.config.configurable import Configurable
23 from traitlets import List
23 from traitlets import List
24
24
25 # This used to be defined here - it is imported for backwards compatibility
25 # This used to be defined here - it is imported for backwards compatibility
26 from .display import publish_display_data
26 from .display import publish_display_data
27
27
28 #-----------------------------------------------------------------------------
28 #-----------------------------------------------------------------------------
29 # Main payload class
29 # Main payload class
30 #-----------------------------------------------------------------------------
30 #-----------------------------------------------------------------------------
31
31
32 class DisplayPublisher(Configurable):
32 class DisplayPublisher(Configurable):
33 """A traited class that publishes display data to frontends.
33 """A traited class that publishes display data to frontends.
34
34
35 Instances of this class are created by the main IPython object and should
35 Instances of this class are created by the main IPython object and should
36 be accessed there.
36 be accessed there.
37 """
37 """
38
38
39 def _validate_data(self, data, metadata=None):
39 def _validate_data(self, data, metadata=None):
40 """Validate the display data.
40 """Validate the display data.
41
41
42 Parameters
42 Parameters
43 ----------
43 ----------
44 data : dict
44 data : dict
45 The formata data dictionary.
45 The formata data dictionary.
46 metadata : dict
46 metadata : dict
47 Any metadata for the data.
47 Any metadata for the data.
48 """
48 """
49
49
50 if not isinstance(data, dict):
50 if not isinstance(data, dict):
51 raise TypeError('data must be a dict, got: %r' % data)
51 raise TypeError('data must be a dict, got: %r' % data)
52 if metadata is not None:
52 if metadata is not None:
53 if not isinstance(metadata, dict):
53 if not isinstance(metadata, dict):
54 raise TypeError('metadata must be a dict, got: %r' % data)
54 raise TypeError('metadata must be a dict, got: %r' % data)
55
55
56 def publish(self, data, metadata=None, source=None):
56 def publish(self, data, metadata=None, source=None):
57 """Publish data and metadata to all frontends.
57 """Publish data and metadata to all frontends.
58
58
59 See the ``display_data`` message in the messaging documentation for
59 See the ``display_data`` message in the messaging documentation for
60 more details about this message type.
60 more details about this message type.
61
61
62 The following MIME types are currently implemented:
62 The following MIME types are currently implemented:
63
63
64 * text/plain
64 * text/plain
65 * text/html
65 * text/html
66 * text/markdown
66 * text/markdown
67 * text/latex
67 * text/latex
68 * application/json
68 * application/json
69 * application/javascript
69 * application/javascript
70 * image/png
70 * image/png
71 * image/jpeg
71 * image/jpeg
72 * image/svg+xml
72 * image/svg+xml
73
73
74 Parameters
74 Parameters
75 ----------
75 ----------
76 data : dict
76 data : dict
77 A dictionary having keys that are valid MIME types (like
77 A dictionary having keys that are valid MIME types (like
78 'text/plain' or 'image/svg+xml') and values that are the data for
78 'text/plain' or 'image/svg+xml') and values that are the data for
79 that MIME type. The data itself must be a JSON'able data
79 that MIME type. The data itself must be a JSON'able data
80 structure. Minimally all data should have the 'text/plain' data,
80 structure. Minimally all data should have the 'text/plain' data,
81 which can be displayed by all frontends. If more than the plain
81 which can be displayed by all frontends. If more than the plain
82 text is given, it is up to the frontend to decide which
82 text is given, it is up to the frontend to decide which
83 representation to use.
83 representation to use.
84 metadata : dict
84 metadata : dict
85 A dictionary for metadata related to the data. This can contain
85 A dictionary for metadata related to the data. This can contain
86 arbitrary key, value pairs that frontends can use to interpret
86 arbitrary key, value pairs that frontends can use to interpret
87 the data. Metadata specific to each mime-type can be specified
87 the data. Metadata specific to each mime-type can be specified
88 in the metadata dict with the same mime-type keys as
88 in the metadata dict with the same mime-type keys as
89 the data itself.
89 the data itself.
90 source : str, deprecated
90 source : str, deprecated
91 Unused.
91 Unused.
92 """
92 """
93
93
94 # The default is to simply write the plain text data using io.stdout.
94 # The default is to simply write the plain text data using sys.stdout.
95 if 'text/plain' in data:
95 if 'text/plain' in data:
96 print(data['text/plain'])
96 print(data['text/plain'])
97
97
98 def clear_output(self, wait=False):
98 def clear_output(self, wait=False):
99 """Clear the output of the cell receiving output."""
99 """Clear the output of the cell receiving output."""
100 print('\033[2K\r', end='')
100 print('\033[2K\r', end='')
101 sys.stdout.flush()
101 sys.stdout.flush()
102 print('\033[2K\r', end='')
102 print('\033[2K\r', end='')
103 sys.stderr.flush()
103 sys.stderr.flush()
104
104
105
105
106 class CapturingDisplayPublisher(DisplayPublisher):
106 class CapturingDisplayPublisher(DisplayPublisher):
107 """A DisplayPublisher that stores"""
107 """A DisplayPublisher that stores"""
108 outputs = List()
108 outputs = List()
109
109
110 def publish(self, data, metadata=None, source=None):
110 def publish(self, data, metadata=None, source=None):
111 self.outputs.append((data, metadata))
111 self.outputs.append((data, metadata))
112
112
113 def clear_output(self, wait=False):
113 def clear_output(self, wait=False):
114 super(CapturingDisplayPublisher, self).clear_output(wait)
114 super(CapturingDisplayPublisher, self).clear_output(wait)
115
115
116 # empty the list, *do not* reassign a new list
116 # empty the list, *do not* reassign a new list
117 del self.outputs[:]
117 del self.outputs[:]
@@ -1,945 +1,947 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """Display formatters.
2 """Display formatters.
3
3
4 Inheritance diagram:
4 Inheritance diagram:
5
5
6 .. inheritance-diagram:: IPython.core.formatters
6 .. inheritance-diagram:: IPython.core.formatters
7 :parts: 3
7 :parts: 3
8 """
8 """
9
9
10 # Copyright (c) IPython Development Team.
10 # Copyright (c) IPython Development Team.
11 # Distributed under the terms of the Modified BSD License.
11 # Distributed under the terms of the Modified BSD License.
12
12
13 import abc
13 import abc
14 import json
14 import json
15 import sys
15 import sys
16 import traceback
16 import traceback
17 import warnings
17 import warnings
18
18
19 from decorator import decorator
19 from decorator import decorator
20
20
21 from traitlets.config.configurable import Configurable
21 from traitlets.config.configurable import Configurable
22 from IPython.core.getipython import get_ipython
22 from IPython.core.getipython import get_ipython
23 from IPython.utils.sentinel import Sentinel
23 from IPython.utils.sentinel import Sentinel
24 from IPython.utils.dir2 import get_real_method
24 from IPython.utils.dir2 import get_real_method
25 from IPython.lib import pretty
25 from IPython.lib import pretty
26 from traitlets import (
26 from traitlets import (
27 Bool, Dict, Integer, Unicode, CUnicode, ObjectName, List,
27 Bool, Dict, Integer, Unicode, CUnicode, ObjectName, List,
28 ForwardDeclaredInstance,
28 ForwardDeclaredInstance,
29 default, observe,
29 default, observe,
30 )
30 )
31 from IPython.utils.py3compat import (
31 from IPython.utils.py3compat import (
32 with_metaclass, string_types, unicode_type,
32 with_metaclass, string_types, unicode_type,
33 )
33 )
34
34
35
35
36 class DisplayFormatter(Configurable):
36 class DisplayFormatter(Configurable):
37
37
38 active_types = List(Unicode(),
38 active_types = List(Unicode(),
39 help="""List of currently active mime-types to display.
39 help="""List of currently active mime-types to display.
40 You can use this to set a white-list for formats to display.
40 You can use this to set a white-list for formats to display.
41
41
42 Most users will not need to change this value.
42 Most users will not need to change this value.
43 """).tag(config=True)
43 """).tag(config=True)
44
44
45 @default('active_types')
45 @default('active_types')
46 def _active_types_default(self):
46 def _active_types_default(self):
47 return self.format_types
47 return self.format_types
48
48
49 @observe('active_types')
49 @observe('active_types')
50 def _active_types_changed(self, change):
50 def _active_types_changed(self, change):
51 for key, formatter in self.formatters.items():
51 for key, formatter in self.formatters.items():
52 if key in change['new']:
52 if key in change['new']:
53 formatter.enabled = True
53 formatter.enabled = True
54 else:
54 else:
55 formatter.enabled = False
55 formatter.enabled = False
56
56
57 ipython_display_formatter = ForwardDeclaredInstance('FormatterABC')
57 ipython_display_formatter = ForwardDeclaredInstance('FormatterABC')
58 @default('ipython_display_formatter')
58 @default('ipython_display_formatter')
59 def _default_formatter(self):
59 def _default_formatter(self):
60 return IPythonDisplayFormatter(parent=self)
60 return IPythonDisplayFormatter(parent=self)
61
61
62 # A dict of formatter whose keys are format types (MIME types) and whose
62 # A dict of formatter whose keys are format types (MIME types) and whose
63 # values are subclasses of BaseFormatter.
63 # values are subclasses of BaseFormatter.
64 formatters = Dict()
64 formatters = Dict()
65 @default('formatters')
65 @default('formatters')
66 def _formatters_default(self):
66 def _formatters_default(self):
67 """Activate the default formatters."""
67 """Activate the default formatters."""
68 formatter_classes = [
68 formatter_classes = [
69 PlainTextFormatter,
69 PlainTextFormatter,
70 HTMLFormatter,
70 HTMLFormatter,
71 MarkdownFormatter,
71 MarkdownFormatter,
72 SVGFormatter,
72 SVGFormatter,
73 PNGFormatter,
73 PNGFormatter,
74 PDFFormatter,
74 PDFFormatter,
75 JPEGFormatter,
75 JPEGFormatter,
76 LatexFormatter,
76 LatexFormatter,
77 JSONFormatter,
77 JSONFormatter,
78 JavascriptFormatter
78 JavascriptFormatter
79 ]
79 ]
80 d = {}
80 d = {}
81 for cls in formatter_classes:
81 for cls in formatter_classes:
82 f = cls(parent=self)
82 f = cls(parent=self)
83 d[f.format_type] = f
83 d[f.format_type] = f
84 return d
84 return d
85
85
86 def format(self, obj, include=None, exclude=None):
86 def format(self, obj, include=None, exclude=None):
87 """Return a format data dict for an object.
87 """Return a format data dict for an object.
88
88
89 By default all format types will be computed.
89 By default all format types will be computed.
90
90
91 The following MIME types are currently implemented:
91 The following MIME types are currently implemented:
92
92
93 * text/plain
93 * text/plain
94 * text/html
94 * text/html
95 * text/markdown
95 * text/markdown
96 * text/latex
96 * text/latex
97 * application/json
97 * application/json
98 * application/javascript
98 * application/javascript
99 * application/pdf
99 * application/pdf
100 * image/png
100 * image/png
101 * image/jpeg
101 * image/jpeg
102 * image/svg+xml
102 * image/svg+xml
103
103
104 Parameters
104 Parameters
105 ----------
105 ----------
106 obj : object
106 obj : object
107 The Python object whose format data will be computed.
107 The Python object whose format data will be computed.
108 include : list or tuple, optional
108 include : list or tuple, optional
109 A list of format type strings (MIME types) to include in the
109 A list of format type strings (MIME types) to include in the
110 format data dict. If this is set *only* the format types included
110 format data dict. If this is set *only* the format types included
111 in this list will be computed.
111 in this list will be computed.
112 exclude : list or tuple, optional
112 exclude : list or tuple, optional
113 A list of format type string (MIME types) to exclude in the format
113 A list of format type string (MIME types) to exclude in the format
114 data dict. If this is set all format types will be computed,
114 data dict. If this is set all format types will be computed,
115 except for those included in this argument.
115 except for those included in this argument.
116
116
117 Returns
117 Returns
118 -------
118 -------
119 (format_dict, metadata_dict) : tuple of two dicts
119 (format_dict, metadata_dict) : tuple of two dicts
120
120
121 format_dict is a dictionary of key/value pairs, one of each format that was
121 format_dict is a dictionary of key/value pairs, one of each format that was
122 generated for the object. The keys are the format types, which
122 generated for the object. The keys are the format types, which
123 will usually be MIME type strings and the values and JSON'able
123 will usually be MIME type strings and the values and JSON'able
124 data structure containing the raw data for the representation in
124 data structure containing the raw data for the representation in
125 that format.
125 that format.
126
126
127 metadata_dict is a dictionary of metadata about each mime-type output.
127 metadata_dict is a dictionary of metadata about each mime-type output.
128 Its keys will be a strict subset of the keys in format_dict.
128 Its keys will be a strict subset of the keys in format_dict.
129 """
129 """
130 format_dict = {}
130 format_dict = {}
131 md_dict = {}
131 md_dict = {}
132
132
133 if self.ipython_display_formatter(obj):
133 if self.ipython_display_formatter(obj):
134 # object handled itself, don't proceed
134 # object handled itself, don't proceed
135 return {}, {}
135 return {}, {}
136
136
137 for format_type, formatter in self.formatters.items():
137 for format_type, formatter in self.formatters.items():
138 if include and format_type not in include:
138 if include and format_type not in include:
139 continue
139 continue
140 if exclude and format_type in exclude:
140 if exclude and format_type in exclude:
141 continue
141 continue
142
142
143 md = None
143 md = None
144 try:
144 try:
145 data = formatter(obj)
145 data = formatter(obj)
146 except:
146 except:
147 # FIXME: log the exception
147 # FIXME: log the exception
148 raise
148 raise
149
149
150 # formatters can return raw data or (data, metadata)
150 # formatters can return raw data or (data, metadata)
151 if isinstance(data, tuple) and len(data) == 2:
151 if isinstance(data, tuple) and len(data) == 2:
152 data, md = data
152 data, md = data
153
153
154 if data is not None:
154 if data is not None:
155 format_dict[format_type] = data
155 format_dict[format_type] = data
156 if md is not None:
156 if md is not None:
157 md_dict[format_type] = md
157 md_dict[format_type] = md
158
158
159 return format_dict, md_dict
159 return format_dict, md_dict
160
160
161 @property
161 @property
162 def format_types(self):
162 def format_types(self):
163 """Return the format types (MIME types) of the active formatters."""
163 """Return the format types (MIME types) of the active formatters."""
164 return list(self.formatters.keys())
164 return list(self.formatters.keys())
165
165
166
166
167 #-----------------------------------------------------------------------------
167 #-----------------------------------------------------------------------------
168 # Formatters for specific format types (text, html, svg, etc.)
168 # Formatters for specific format types (text, html, svg, etc.)
169 #-----------------------------------------------------------------------------
169 #-----------------------------------------------------------------------------
170
170
171
171
172 def _safe_repr(obj):
172 def _safe_repr(obj):
173 """Try to return a repr of an object
173 """Try to return a repr of an object
174
174
175 always returns a string, at least.
175 always returns a string, at least.
176 """
176 """
177 try:
177 try:
178 return repr(obj)
178 return repr(obj)
179 except Exception as e:
179 except Exception as e:
180 return "un-repr-able object (%r)" % e
180 return "un-repr-able object (%r)" % e
181
181
182
182
183 class FormatterWarning(UserWarning):
183 class FormatterWarning(UserWarning):
184 """Warning class for errors in formatters"""
184 """Warning class for errors in formatters"""
185
185
186 @decorator
186 @decorator
187 def catch_format_error(method, self, *args, **kwargs):
187 def catch_format_error(method, self, *args, **kwargs):
188 """show traceback on failed format call"""
188 """show traceback on failed format call"""
189 try:
189 try:
190 r = method(self, *args, **kwargs)
190 r = method(self, *args, **kwargs)
191 except NotImplementedError:
191 except NotImplementedError:
192 # don't warn on NotImplementedErrors
192 # don't warn on NotImplementedErrors
193 return None
193 return None
194 except Exception:
194 except Exception:
195 exc_info = sys.exc_info()
195 exc_info = sys.exc_info()
196 ip = get_ipython()
196 ip = get_ipython()
197 if ip is not None:
197 if ip is not None:
198 ip.showtraceback(exc_info)
198 ip.showtraceback(exc_info)
199 else:
199 else:
200 traceback.print_exception(*exc_info)
200 traceback.print_exception(*exc_info)
201 return None
201 return None
202 return self._check_return(r, args[0])
202 return self._check_return(r, args[0])
203
203
204
204
205 class FormatterABC(with_metaclass(abc.ABCMeta, object)):
205 class FormatterABC(with_metaclass(abc.ABCMeta, object)):
206 """ Abstract base class for Formatters.
206 """ Abstract base class for Formatters.
207
207
208 A formatter is a callable class that is responsible for computing the
208 A formatter is a callable class that is responsible for computing the
209 raw format data for a particular format type (MIME type). For example,
209 raw format data for a particular format type (MIME type). For example,
210 an HTML formatter would have a format type of `text/html` and would return
210 an HTML formatter would have a format type of `text/html` and would return
211 the HTML representation of the object when called.
211 the HTML representation of the object when called.
212 """
212 """
213
213
214 # The format type of the data returned, usually a MIME type.
214 # The format type of the data returned, usually a MIME type.
215 format_type = 'text/plain'
215 format_type = 'text/plain'
216
216
217 # Is the formatter enabled...
217 # Is the formatter enabled...
218 enabled = True
218 enabled = True
219
219
220 @abc.abstractmethod
220 @abc.abstractmethod
221 def __call__(self, obj):
221 def __call__(self, obj):
222 """Return a JSON'able representation of the object.
222 """Return a JSON'able representation of the object.
223
223
224 If the object cannot be formatted by this formatter,
224 If the object cannot be formatted by this formatter,
225 warn and return None.
225 warn and return None.
226 """
226 """
227 return repr(obj)
227 return repr(obj)
228
228
229
229
230 def _mod_name_key(typ):
230 def _mod_name_key(typ):
231 """Return a (__module__, __name__) tuple for a type.
231 """Return a (__module__, __name__) tuple for a type.
232
232
233 Used as key in Formatter.deferred_printers.
233 Used as key in Formatter.deferred_printers.
234 """
234 """
235 module = getattr(typ, '__module__', None)
235 module = getattr(typ, '__module__', None)
236 name = getattr(typ, '__name__', None)
236 name = getattr(typ, '__name__', None)
237 return (module, name)
237 return (module, name)
238
238
239
239
240 def _get_type(obj):
240 def _get_type(obj):
241 """Return the type of an instance (old and new-style)"""
241 """Return the type of an instance (old and new-style)"""
242 return getattr(obj, '__class__', None) or type(obj)
242 return getattr(obj, '__class__', None) or type(obj)
243
243
244
244
245 _raise_key_error = Sentinel('_raise_key_error', __name__,
245 _raise_key_error = Sentinel('_raise_key_error', __name__,
246 """
246 """
247 Special value to raise a KeyError
247 Special value to raise a KeyError
248
248
249 Raise KeyError in `BaseFormatter.pop` if passed as the default value to `pop`
249 Raise KeyError in `BaseFormatter.pop` if passed as the default value to `pop`
250 """)
250 """)
251
251
252
252
253 class BaseFormatter(Configurable):
253 class BaseFormatter(Configurable):
254 """A base formatter class that is configurable.
254 """A base formatter class that is configurable.
255
255
256 This formatter should usually be used as the base class of all formatters.
256 This formatter should usually be used as the base class of all formatters.
257 It is a traited :class:`Configurable` class and includes an extensible
257 It is a traited :class:`Configurable` class and includes an extensible
258 API for users to determine how their objects are formatted. The following
258 API for users to determine how their objects are formatted. The following
259 logic is used to find a function to format an given object.
259 logic is used to find a function to format an given object.
260
260
261 1. The object is introspected to see if it has a method with the name
261 1. The object is introspected to see if it has a method with the name
262 :attr:`print_method`. If is does, that object is passed to that method
262 :attr:`print_method`. If is does, that object is passed to that method
263 for formatting.
263 for formatting.
264 2. If no print method is found, three internal dictionaries are consulted
264 2. If no print method is found, three internal dictionaries are consulted
265 to find print method: :attr:`singleton_printers`, :attr:`type_printers`
265 to find print method: :attr:`singleton_printers`, :attr:`type_printers`
266 and :attr:`deferred_printers`.
266 and :attr:`deferred_printers`.
267
267
268 Users should use these dictionaries to register functions that will be
268 Users should use these dictionaries to register functions that will be
269 used to compute the format data for their objects (if those objects don't
269 used to compute the format data for their objects (if those objects don't
270 have the special print methods). The easiest way of using these
270 have the special print methods). The easiest way of using these
271 dictionaries is through the :meth:`for_type` and :meth:`for_type_by_name`
271 dictionaries is through the :meth:`for_type` and :meth:`for_type_by_name`
272 methods.
272 methods.
273
273
274 If no function/callable is found to compute the format data, ``None`` is
274 If no function/callable is found to compute the format data, ``None`` is
275 returned and this format type is not used.
275 returned and this format type is not used.
276 """
276 """
277
277
278 format_type = Unicode('text/plain')
278 format_type = Unicode('text/plain')
279 _return_type = string_types
279 _return_type = string_types
280
280
281 enabled = Bool(True).tag(config=True)
281 enabled = Bool(True).tag(config=True)
282
282
283 print_method = ObjectName('__repr__')
283 print_method = ObjectName('__repr__')
284
284
285 # The singleton printers.
285 # The singleton printers.
286 # Maps the IDs of the builtin singleton objects to the format functions.
286 # Maps the IDs of the builtin singleton objects to the format functions.
287 singleton_printers = Dict().tag(config=True)
287 singleton_printers = Dict().tag(config=True)
288
288
289 # The type-specific printers.
289 # The type-specific printers.
290 # Map type objects to the format functions.
290 # Map type objects to the format functions.
291 type_printers = Dict().tag(config=True)
291 type_printers = Dict().tag(config=True)
292
292
293 # The deferred-import type-specific printers.
293 # The deferred-import type-specific printers.
294 # Map (modulename, classname) pairs to the format functions.
294 # Map (modulename, classname) pairs to the format functions.
295 deferred_printers = Dict().tag(config=True)
295 deferred_printers = Dict().tag(config=True)
296
296
297 @catch_format_error
297 @catch_format_error
298 def __call__(self, obj):
298 def __call__(self, obj):
299 """Compute the format for an object."""
299 """Compute the format for an object."""
300 if self.enabled:
300 if self.enabled:
301 # lookup registered printer
301 # lookup registered printer
302 try:
302 try:
303 printer = self.lookup(obj)
303 printer = self.lookup(obj)
304 except KeyError:
304 except KeyError:
305 pass
305 pass
306 else:
306 else:
307 return printer(obj)
307 return printer(obj)
308 # Finally look for special method names
308 # Finally look for special method names
309 method = get_real_method(obj, self.print_method)
309 method = get_real_method(obj, self.print_method)
310 if method is not None:
310 if method is not None:
311 return method()
311 return method()
312 return None
312 return None
313 else:
313 else:
314 return None
314 return None
315
315
316 def __contains__(self, typ):
316 def __contains__(self, typ):
317 """map in to lookup_by_type"""
317 """map in to lookup_by_type"""
318 try:
318 try:
319 self.lookup_by_type(typ)
319 self.lookup_by_type(typ)
320 except KeyError:
320 except KeyError:
321 return False
321 return False
322 else:
322 else:
323 return True
323 return True
324
324
325 def _check_return(self, r, obj):
325 def _check_return(self, r, obj):
326 """Check that a return value is appropriate
326 """Check that a return value is appropriate
327
327
328 Return the value if so, None otherwise, warning if invalid.
328 Return the value if so, None otherwise, warning if invalid.
329 """
329 """
330 if r is None or isinstance(r, self._return_type) or \
330 if r is None or isinstance(r, self._return_type) or \
331 (isinstance(r, tuple) and r and isinstance(r[0], self._return_type)):
331 (isinstance(r, tuple) and r and isinstance(r[0], self._return_type)):
332 return r
332 return r
333 else:
333 else:
334 warnings.warn(
334 warnings.warn(
335 "%s formatter returned invalid type %s (expected %s) for object: %s" % \
335 "%s formatter returned invalid type %s (expected %s) for object: %s" % \
336 (self.format_type, type(r), self._return_type, _safe_repr(obj)),
336 (self.format_type, type(r), self._return_type, _safe_repr(obj)),
337 FormatterWarning
337 FormatterWarning
338 )
338 )
339
339
340 def lookup(self, obj):
340 def lookup(self, obj):
341 """Look up the formatter for a given instance.
341 """Look up the formatter for a given instance.
342
342
343 Parameters
343 Parameters
344 ----------
344 ----------
345 obj : object instance
345 obj : object instance
346
346
347 Returns
347 Returns
348 -------
348 -------
349 f : callable
349 f : callable
350 The registered formatting callable for the type.
350 The registered formatting callable for the type.
351
351
352 Raises
352 Raises
353 ------
353 ------
354 KeyError if the type has not been registered.
354 KeyError if the type has not been registered.
355 """
355 """
356 # look for singleton first
356 # look for singleton first
357 obj_id = id(obj)
357 obj_id = id(obj)
358 if obj_id in self.singleton_printers:
358 if obj_id in self.singleton_printers:
359 return self.singleton_printers[obj_id]
359 return self.singleton_printers[obj_id]
360 # then lookup by type
360 # then lookup by type
361 return self.lookup_by_type(_get_type(obj))
361 return self.lookup_by_type(_get_type(obj))
362
362
363 def lookup_by_type(self, typ):
363 def lookup_by_type(self, typ):
364 """Look up the registered formatter for a type.
364 """Look up the registered formatter for a type.
365
365
366 Parameters
366 Parameters
367 ----------
367 ----------
368 typ : type or '__module__.__name__' string for a type
368 typ : type or '__module__.__name__' string for a type
369
369
370 Returns
370 Returns
371 -------
371 -------
372 f : callable
372 f : callable
373 The registered formatting callable for the type.
373 The registered formatting callable for the type.
374
374
375 Raises
375 Raises
376 ------
376 ------
377 KeyError if the type has not been registered.
377 KeyError if the type has not been registered.
378 """
378 """
379 if isinstance(typ, string_types):
379 if isinstance(typ, string_types):
380 typ_key = tuple(typ.rsplit('.',1))
380 typ_key = tuple(typ.rsplit('.',1))
381 if typ_key not in self.deferred_printers:
381 if typ_key not in self.deferred_printers:
382 # We may have it cached in the type map. We will have to
382 # We may have it cached in the type map. We will have to
383 # iterate over all of the types to check.
383 # iterate over all of the types to check.
384 for cls in self.type_printers:
384 for cls in self.type_printers:
385 if _mod_name_key(cls) == typ_key:
385 if _mod_name_key(cls) == typ_key:
386 return self.type_printers[cls]
386 return self.type_printers[cls]
387 else:
387 else:
388 return self.deferred_printers[typ_key]
388 return self.deferred_printers[typ_key]
389 else:
389 else:
390 for cls in pretty._get_mro(typ):
390 for cls in pretty._get_mro(typ):
391 if cls in self.type_printers or self._in_deferred_types(cls):
391 if cls in self.type_printers or self._in_deferred_types(cls):
392 return self.type_printers[cls]
392 return self.type_printers[cls]
393
393
394 # If we have reached here, the lookup failed.
394 # If we have reached here, the lookup failed.
395 raise KeyError("No registered printer for {0!r}".format(typ))
395 raise KeyError("No registered printer for {0!r}".format(typ))
396
396
397 def for_type(self, typ, func=None):
397 def for_type(self, typ, func=None):
398 """Add a format function for a given type.
398 """Add a format function for a given type.
399
399
400 Parameters
400 Parameters
401 -----------
401 -----------
402 typ : type or '__module__.__name__' string for a type
402 typ : type or '__module__.__name__' string for a type
403 The class of the object that will be formatted using `func`.
403 The class of the object that will be formatted using `func`.
404 func : callable
404 func : callable
405 A callable for computing the format data.
405 A callable for computing the format data.
406 `func` will be called with the object to be formatted,
406 `func` will be called with the object to be formatted,
407 and will return the raw data in this formatter's format.
407 and will return the raw data in this formatter's format.
408 Subclasses may use a different call signature for the
408 Subclasses may use a different call signature for the
409 `func` argument.
409 `func` argument.
410
410
411 If `func` is None or not specified, there will be no change,
411 If `func` is None or not specified, there will be no change,
412 only returning the current value.
412 only returning the current value.
413
413
414 Returns
414 Returns
415 -------
415 -------
416 oldfunc : callable
416 oldfunc : callable
417 The currently registered callable.
417 The currently registered callable.
418 If you are registering a new formatter,
418 If you are registering a new formatter,
419 this will be the previous value (to enable restoring later).
419 this will be the previous value (to enable restoring later).
420 """
420 """
421 # if string given, interpret as 'pkg.module.class_name'
421 # if string given, interpret as 'pkg.module.class_name'
422 if isinstance(typ, string_types):
422 if isinstance(typ, string_types):
423 type_module, type_name = typ.rsplit('.', 1)
423 type_module, type_name = typ.rsplit('.', 1)
424 return self.for_type_by_name(type_module, type_name, func)
424 return self.for_type_by_name(type_module, type_name, func)
425
425
426 try:
426 try:
427 oldfunc = self.lookup_by_type(typ)
427 oldfunc = self.lookup_by_type(typ)
428 except KeyError:
428 except KeyError:
429 oldfunc = None
429 oldfunc = None
430
430
431 if func is not None:
431 if func is not None:
432 self.type_printers[typ] = func
432 self.type_printers[typ] = func
433
433
434 return oldfunc
434 return oldfunc
435
435
436 def for_type_by_name(self, type_module, type_name, func=None):
436 def for_type_by_name(self, type_module, type_name, func=None):
437 """Add a format function for a type specified by the full dotted
437 """Add a format function for a type specified by the full dotted
438 module and name of the type, rather than the type of the object.
438 module and name of the type, rather than the type of the object.
439
439
440 Parameters
440 Parameters
441 ----------
441 ----------
442 type_module : str
442 type_module : str
443 The full dotted name of the module the type is defined in, like
443 The full dotted name of the module the type is defined in, like
444 ``numpy``.
444 ``numpy``.
445 type_name : str
445 type_name : str
446 The name of the type (the class name), like ``dtype``
446 The name of the type (the class name), like ``dtype``
447 func : callable
447 func : callable
448 A callable for computing the format data.
448 A callable for computing the format data.
449 `func` will be called with the object to be formatted,
449 `func` will be called with the object to be formatted,
450 and will return the raw data in this formatter's format.
450 and will return the raw data in this formatter's format.
451 Subclasses may use a different call signature for the
451 Subclasses may use a different call signature for the
452 `func` argument.
452 `func` argument.
453
453
454 If `func` is None or unspecified, there will be no change,
454 If `func` is None or unspecified, there will be no change,
455 only returning the current value.
455 only returning the current value.
456
456
457 Returns
457 Returns
458 -------
458 -------
459 oldfunc : callable
459 oldfunc : callable
460 The currently registered callable.
460 The currently registered callable.
461 If you are registering a new formatter,
461 If you are registering a new formatter,
462 this will be the previous value (to enable restoring later).
462 this will be the previous value (to enable restoring later).
463 """
463 """
464 key = (type_module, type_name)
464 key = (type_module, type_name)
465
465
466 try:
466 try:
467 oldfunc = self.lookup_by_type("%s.%s" % key)
467 oldfunc = self.lookup_by_type("%s.%s" % key)
468 except KeyError:
468 except KeyError:
469 oldfunc = None
469 oldfunc = None
470
470
471 if func is not None:
471 if func is not None:
472 self.deferred_printers[key] = func
472 self.deferred_printers[key] = func
473 return oldfunc
473 return oldfunc
474
474
475 def pop(self, typ, default=_raise_key_error):
475 def pop(self, typ, default=_raise_key_error):
476 """Pop a formatter for the given type.
476 """Pop a formatter for the given type.
477
477
478 Parameters
478 Parameters
479 ----------
479 ----------
480 typ : type or '__module__.__name__' string for a type
480 typ : type or '__module__.__name__' string for a type
481 default : object
481 default : object
482 value to be returned if no formatter is registered for typ.
482 value to be returned if no formatter is registered for typ.
483
483
484 Returns
484 Returns
485 -------
485 -------
486 obj : object
486 obj : object
487 The last registered object for the type.
487 The last registered object for the type.
488
488
489 Raises
489 Raises
490 ------
490 ------
491 KeyError if the type is not registered and default is not specified.
491 KeyError if the type is not registered and default is not specified.
492 """
492 """
493
493
494 if isinstance(typ, string_types):
494 if isinstance(typ, string_types):
495 typ_key = tuple(typ.rsplit('.',1))
495 typ_key = tuple(typ.rsplit('.',1))
496 if typ_key not in self.deferred_printers:
496 if typ_key not in self.deferred_printers:
497 # We may have it cached in the type map. We will have to
497 # We may have it cached in the type map. We will have to
498 # iterate over all of the types to check.
498 # iterate over all of the types to check.
499 for cls in self.type_printers:
499 for cls in self.type_printers:
500 if _mod_name_key(cls) == typ_key:
500 if _mod_name_key(cls) == typ_key:
501 old = self.type_printers.pop(cls)
501 old = self.type_printers.pop(cls)
502 break
502 break
503 else:
503 else:
504 old = default
504 old = default
505 else:
505 else:
506 old = self.deferred_printers.pop(typ_key)
506 old = self.deferred_printers.pop(typ_key)
507 else:
507 else:
508 if typ in self.type_printers:
508 if typ in self.type_printers:
509 old = self.type_printers.pop(typ)
509 old = self.type_printers.pop(typ)
510 else:
510 else:
511 old = self.deferred_printers.pop(_mod_name_key(typ), default)
511 old = self.deferred_printers.pop(_mod_name_key(typ), default)
512 if old is _raise_key_error:
512 if old is _raise_key_error:
513 raise KeyError("No registered value for {0!r}".format(typ))
513 raise KeyError("No registered value for {0!r}".format(typ))
514 return old
514 return old
515
515
516 def _in_deferred_types(self, cls):
516 def _in_deferred_types(self, cls):
517 """
517 """
518 Check if the given class is specified in the deferred type registry.
518 Check if the given class is specified in the deferred type registry.
519
519
520 Successful matches will be moved to the regular type registry for future use.
520 Successful matches will be moved to the regular type registry for future use.
521 """
521 """
522 mod = getattr(cls, '__module__', None)
522 mod = getattr(cls, '__module__', None)
523 name = getattr(cls, '__name__', None)
523 name = getattr(cls, '__name__', None)
524 key = (mod, name)
524 key = (mod, name)
525 if key in self.deferred_printers:
525 if key in self.deferred_printers:
526 # Move the printer over to the regular registry.
526 # Move the printer over to the regular registry.
527 printer = self.deferred_printers.pop(key)
527 printer = self.deferred_printers.pop(key)
528 self.type_printers[cls] = printer
528 self.type_printers[cls] = printer
529 return True
529 return True
530 return False
530 return False
531
531
532
532
533 class PlainTextFormatter(BaseFormatter):
533 class PlainTextFormatter(BaseFormatter):
534 """The default pretty-printer.
534 """The default pretty-printer.
535
535
536 This uses :mod:`IPython.lib.pretty` to compute the format data of
536 This uses :mod:`IPython.lib.pretty` to compute the format data of
537 the object. If the object cannot be pretty printed, :func:`repr` is used.
537 the object. If the object cannot be pretty printed, :func:`repr` is used.
538 See the documentation of :mod:`IPython.lib.pretty` for details on
538 See the documentation of :mod:`IPython.lib.pretty` for details on
539 how to write pretty printers. Here is a simple example::
539 how to write pretty printers. Here is a simple example::
540
540
541 def dtype_pprinter(obj, p, cycle):
541 def dtype_pprinter(obj, p, cycle):
542 if cycle:
542 if cycle:
543 return p.text('dtype(...)')
543 return p.text('dtype(...)')
544 if hasattr(obj, 'fields'):
544 if hasattr(obj, 'fields'):
545 if obj.fields is None:
545 if obj.fields is None:
546 p.text(repr(obj))
546 p.text(repr(obj))
547 else:
547 else:
548 p.begin_group(7, 'dtype([')
548 p.begin_group(7, 'dtype([')
549 for i, field in enumerate(obj.descr):
549 for i, field in enumerate(obj.descr):
550 if i > 0:
550 if i > 0:
551 p.text(',')
551 p.text(',')
552 p.breakable()
552 p.breakable()
553 p.pretty(field)
553 p.pretty(field)
554 p.end_group(7, '])')
554 p.end_group(7, '])')
555 """
555 """
556
556
557 # The format type of data returned.
557 # The format type of data returned.
558 format_type = Unicode('text/plain')
558 format_type = Unicode('text/plain')
559
559
560 # This subclass ignores this attribute as it always need to return
560 # This subclass ignores this attribute as it always need to return
561 # something.
561 # something.
562 enabled = Bool(True).tag(config=False)
562 enabled = Bool(True).tag(config=False)
563
563
564 max_seq_length = Integer(pretty.MAX_SEQ_LENGTH,
564 max_seq_length = Integer(pretty.MAX_SEQ_LENGTH,
565 help="""Truncate large collections (lists, dicts, tuples, sets) to this size.
565 help="""Truncate large collections (lists, dicts, tuples, sets) to this size.
566
566
567 Set to 0 to disable truncation.
567 Set to 0 to disable truncation.
568 """
568 """
569 ).tag(config=True)
569 ).tag(config=True)
570
570
571 # Look for a _repr_pretty_ methods to use for pretty printing.
571 # Look for a _repr_pretty_ methods to use for pretty printing.
572 print_method = ObjectName('_repr_pretty_')
572 print_method = ObjectName('_repr_pretty_')
573
573
574 # Whether to pretty-print or not.
574 # Whether to pretty-print or not.
575 pprint = Bool(True).tag(config=True)
575 pprint = Bool(True).tag(config=True)
576
576
577 # Whether to be verbose or not.
577 # Whether to be verbose or not.
578 verbose = Bool(False).tag(config=True)
578 verbose = Bool(False).tag(config=True)
579
579
580 # The maximum width.
580 # The maximum width.
581 max_width = Integer(79).tag(config=True)
581 max_width = Integer(79).tag(config=True)
582
582
583 # The newline character.
583 # The newline character.
584 newline = Unicode('\n').tag(config=True)
584 newline = Unicode('\n').tag(config=True)
585
585
586 # format-string for pprinting floats
586 # format-string for pprinting floats
587 float_format = Unicode('%r')
587 float_format = Unicode('%r')
588 # setter for float precision, either int or direct format-string
588 # setter for float precision, either int or direct format-string
589 float_precision = CUnicode('').tag(config=True)
589 float_precision = CUnicode('').tag(config=True)
590
590
591 def _float_precision_changed(self, name, old, new):
591 @observe('float_precision')
592 def _float_precision_changed(self, change):
592 """float_precision changed, set float_format accordingly.
593 """float_precision changed, set float_format accordingly.
593
594
594 float_precision can be set by int or str.
595 float_precision can be set by int or str.
595 This will set float_format, after interpreting input.
596 This will set float_format, after interpreting input.
596 If numpy has been imported, numpy print precision will also be set.
597 If numpy has been imported, numpy print precision will also be set.
597
598
598 integer `n` sets format to '%.nf', otherwise, format set directly.
599 integer `n` sets format to '%.nf', otherwise, format set directly.
599
600
600 An empty string returns to defaults (repr for float, 8 for numpy).
601 An empty string returns to defaults (repr for float, 8 for numpy).
601
602
602 This parameter can be set via the '%precision' magic.
603 This parameter can be set via the '%precision' magic.
603 """
604 """
604
605
606 new = change['new']
605 if '%' in new:
607 if '%' in new:
606 # got explicit format string
608 # got explicit format string
607 fmt = new
609 fmt = new
608 try:
610 try:
609 fmt%3.14159
611 fmt%3.14159
610 except Exception:
612 except Exception:
611 raise ValueError("Precision must be int or format string, not %r"%new)
613 raise ValueError("Precision must be int or format string, not %r"%new)
612 elif new:
614 elif new:
613 # otherwise, should be an int
615 # otherwise, should be an int
614 try:
616 try:
615 i = int(new)
617 i = int(new)
616 assert i >= 0
618 assert i >= 0
617 except ValueError:
619 except ValueError:
618 raise ValueError("Precision must be int or format string, not %r"%new)
620 raise ValueError("Precision must be int or format string, not %r"%new)
619 except AssertionError:
621 except AssertionError:
620 raise ValueError("int precision must be non-negative, not %r"%i)
622 raise ValueError("int precision must be non-negative, not %r"%i)
621
623
622 fmt = '%%.%if'%i
624 fmt = '%%.%if'%i
623 if 'numpy' in sys.modules:
625 if 'numpy' in sys.modules:
624 # set numpy precision if it has been imported
626 # set numpy precision if it has been imported
625 import numpy
627 import numpy
626 numpy.set_printoptions(precision=i)
628 numpy.set_printoptions(precision=i)
627 else:
629 else:
628 # default back to repr
630 # default back to repr
629 fmt = '%r'
631 fmt = '%r'
630 if 'numpy' in sys.modules:
632 if 'numpy' in sys.modules:
631 import numpy
633 import numpy
632 # numpy default is 8
634 # numpy default is 8
633 numpy.set_printoptions(precision=8)
635 numpy.set_printoptions(precision=8)
634 self.float_format = fmt
636 self.float_format = fmt
635
637
636 # Use the default pretty printers from IPython.lib.pretty.
638 # Use the default pretty printers from IPython.lib.pretty.
637 @default('singleton_printers')
639 @default('singleton_printers')
638 def _singleton_printers_default(self):
640 def _singleton_printers_default(self):
639 return pretty._singleton_pprinters.copy()
641 return pretty._singleton_pprinters.copy()
640
642
641 @default('type_printers')
643 @default('type_printers')
642 def _type_printers_default(self):
644 def _type_printers_default(self):
643 d = pretty._type_pprinters.copy()
645 d = pretty._type_pprinters.copy()
644 d[float] = lambda obj,p,cycle: p.text(self.float_format%obj)
646 d[float] = lambda obj,p,cycle: p.text(self.float_format%obj)
645 return d
647 return d
646
648
647 @default('deferred_printers')
649 @default('deferred_printers')
648 def _deferred_printers_default(self):
650 def _deferred_printers_default(self):
649 return pretty._deferred_type_pprinters.copy()
651 return pretty._deferred_type_pprinters.copy()
650
652
651 #### FormatterABC interface ####
653 #### FormatterABC interface ####
652
654
653 @catch_format_error
655 @catch_format_error
654 def __call__(self, obj):
656 def __call__(self, obj):
655 """Compute the pretty representation of the object."""
657 """Compute the pretty representation of the object."""
656 if not self.pprint:
658 if not self.pprint:
657 return repr(obj)
659 return repr(obj)
658 else:
660 else:
659 # handle str and unicode on Python 2
661 # handle str and unicode on Python 2
660 # io.StringIO only accepts unicode,
662 # io.StringIO only accepts unicode,
661 # cStringIO doesn't handle unicode on py2,
663 # cStringIO doesn't handle unicode on py2,
662 # StringIO allows str, unicode but only ascii str
664 # StringIO allows str, unicode but only ascii str
663 stream = pretty.CUnicodeIO()
665 stream = pretty.CUnicodeIO()
664 printer = pretty.RepresentationPrinter(stream, self.verbose,
666 printer = pretty.RepresentationPrinter(stream, self.verbose,
665 self.max_width, self.newline,
667 self.max_width, self.newline,
666 max_seq_length=self.max_seq_length,
668 max_seq_length=self.max_seq_length,
667 singleton_pprinters=self.singleton_printers,
669 singleton_pprinters=self.singleton_printers,
668 type_pprinters=self.type_printers,
670 type_pprinters=self.type_printers,
669 deferred_pprinters=self.deferred_printers)
671 deferred_pprinters=self.deferred_printers)
670 printer.pretty(obj)
672 printer.pretty(obj)
671 printer.flush()
673 printer.flush()
672 return stream.getvalue()
674 return stream.getvalue()
673
675
674
676
675 class HTMLFormatter(BaseFormatter):
677 class HTMLFormatter(BaseFormatter):
676 """An HTML formatter.
678 """An HTML formatter.
677
679
678 To define the callables that compute the HTML representation of your
680 To define the callables that compute the HTML representation of your
679 objects, define a :meth:`_repr_html_` method or use the :meth:`for_type`
681 objects, define a :meth:`_repr_html_` method or use the :meth:`for_type`
680 or :meth:`for_type_by_name` methods to register functions that handle
682 or :meth:`for_type_by_name` methods to register functions that handle
681 this.
683 this.
682
684
683 The return value of this formatter should be a valid HTML snippet that
685 The return value of this formatter should be a valid HTML snippet that
684 could be injected into an existing DOM. It should *not* include the
686 could be injected into an existing DOM. It should *not* include the
685 ```<html>`` or ```<body>`` tags.
687 ```<html>`` or ```<body>`` tags.
686 """
688 """
687 format_type = Unicode('text/html')
689 format_type = Unicode('text/html')
688
690
689 print_method = ObjectName('_repr_html_')
691 print_method = ObjectName('_repr_html_')
690
692
691
693
692 class MarkdownFormatter(BaseFormatter):
694 class MarkdownFormatter(BaseFormatter):
693 """A Markdown formatter.
695 """A Markdown formatter.
694
696
695 To define the callables that compute the Markdown representation of your
697 To define the callables that compute the Markdown representation of your
696 objects, define a :meth:`_repr_markdown_` method or use the :meth:`for_type`
698 objects, define a :meth:`_repr_markdown_` method or use the :meth:`for_type`
697 or :meth:`for_type_by_name` methods to register functions that handle
699 or :meth:`for_type_by_name` methods to register functions that handle
698 this.
700 this.
699
701
700 The return value of this formatter should be a valid Markdown.
702 The return value of this formatter should be a valid Markdown.
701 """
703 """
702 format_type = Unicode('text/markdown')
704 format_type = Unicode('text/markdown')
703
705
704 print_method = ObjectName('_repr_markdown_')
706 print_method = ObjectName('_repr_markdown_')
705
707
706 class SVGFormatter(BaseFormatter):
708 class SVGFormatter(BaseFormatter):
707 """An SVG formatter.
709 """An SVG formatter.
708
710
709 To define the callables that compute the SVG representation of your
711 To define the callables that compute the SVG representation of your
710 objects, define a :meth:`_repr_svg_` method or use the :meth:`for_type`
712 objects, define a :meth:`_repr_svg_` method or use the :meth:`for_type`
711 or :meth:`for_type_by_name` methods to register functions that handle
713 or :meth:`for_type_by_name` methods to register functions that handle
712 this.
714 this.
713
715
714 The return value of this formatter should be valid SVG enclosed in
716 The return value of this formatter should be valid SVG enclosed in
715 ```<svg>``` tags, that could be injected into an existing DOM. It should
717 ```<svg>``` tags, that could be injected into an existing DOM. It should
716 *not* include the ```<html>`` or ```<body>`` tags.
718 *not* include the ```<html>`` or ```<body>`` tags.
717 """
719 """
718 format_type = Unicode('image/svg+xml')
720 format_type = Unicode('image/svg+xml')
719
721
720 print_method = ObjectName('_repr_svg_')
722 print_method = ObjectName('_repr_svg_')
721
723
722
724
723 class PNGFormatter(BaseFormatter):
725 class PNGFormatter(BaseFormatter):
724 """A PNG formatter.
726 """A PNG formatter.
725
727
726 To define the callables that compute the PNG representation of your
728 To define the callables that compute the PNG representation of your
727 objects, define a :meth:`_repr_png_` method or use the :meth:`for_type`
729 objects, define a :meth:`_repr_png_` method or use the :meth:`for_type`
728 or :meth:`for_type_by_name` methods to register functions that handle
730 or :meth:`for_type_by_name` methods to register functions that handle
729 this.
731 this.
730
732
731 The return value of this formatter should be raw PNG data, *not*
733 The return value of this formatter should be raw PNG data, *not*
732 base64 encoded.
734 base64 encoded.
733 """
735 """
734 format_type = Unicode('image/png')
736 format_type = Unicode('image/png')
735
737
736 print_method = ObjectName('_repr_png_')
738 print_method = ObjectName('_repr_png_')
737
739
738 _return_type = (bytes, unicode_type)
740 _return_type = (bytes, unicode_type)
739
741
740
742
741 class JPEGFormatter(BaseFormatter):
743 class JPEGFormatter(BaseFormatter):
742 """A JPEG formatter.
744 """A JPEG formatter.
743
745
744 To define the callables that compute the JPEG representation of your
746 To define the callables that compute the JPEG representation of your
745 objects, define a :meth:`_repr_jpeg_` method or use the :meth:`for_type`
747 objects, define a :meth:`_repr_jpeg_` method or use the :meth:`for_type`
746 or :meth:`for_type_by_name` methods to register functions that handle
748 or :meth:`for_type_by_name` methods to register functions that handle
747 this.
749 this.
748
750
749 The return value of this formatter should be raw JPEG data, *not*
751 The return value of this formatter should be raw JPEG data, *not*
750 base64 encoded.
752 base64 encoded.
751 """
753 """
752 format_type = Unicode('image/jpeg')
754 format_type = Unicode('image/jpeg')
753
755
754 print_method = ObjectName('_repr_jpeg_')
756 print_method = ObjectName('_repr_jpeg_')
755
757
756 _return_type = (bytes, unicode_type)
758 _return_type = (bytes, unicode_type)
757
759
758
760
759 class LatexFormatter(BaseFormatter):
761 class LatexFormatter(BaseFormatter):
760 """A LaTeX formatter.
762 """A LaTeX formatter.
761
763
762 To define the callables that compute the LaTeX representation of your
764 To define the callables that compute the LaTeX representation of your
763 objects, define a :meth:`_repr_latex_` method or use the :meth:`for_type`
765 objects, define a :meth:`_repr_latex_` method or use the :meth:`for_type`
764 or :meth:`for_type_by_name` methods to register functions that handle
766 or :meth:`for_type_by_name` methods to register functions that handle
765 this.
767 this.
766
768
767 The return value of this formatter should be a valid LaTeX equation,
769 The return value of this formatter should be a valid LaTeX equation,
768 enclosed in either ```$```, ```$$``` or another LaTeX equation
770 enclosed in either ```$```, ```$$``` or another LaTeX equation
769 environment.
771 environment.
770 """
772 """
771 format_type = Unicode('text/latex')
773 format_type = Unicode('text/latex')
772
774
773 print_method = ObjectName('_repr_latex_')
775 print_method = ObjectName('_repr_latex_')
774
776
775
777
776 class JSONFormatter(BaseFormatter):
778 class JSONFormatter(BaseFormatter):
777 """A JSON string formatter.
779 """A JSON string formatter.
778
780
779 To define the callables that compute the JSONable representation of
781 To define the callables that compute the JSONable representation of
780 your objects, define a :meth:`_repr_json_` method or use the :meth:`for_type`
782 your objects, define a :meth:`_repr_json_` method or use the :meth:`for_type`
781 or :meth:`for_type_by_name` methods to register functions that handle
783 or :meth:`for_type_by_name` methods to register functions that handle
782 this.
784 this.
783
785
784 The return value of this formatter should be a JSONable list or dict.
786 The return value of this formatter should be a JSONable list or dict.
785 JSON scalars (None, number, string) are not allowed, only dict or list containers.
787 JSON scalars (None, number, string) are not allowed, only dict or list containers.
786 """
788 """
787 format_type = Unicode('application/json')
789 format_type = Unicode('application/json')
788 _return_type = (list, dict)
790 _return_type = (list, dict)
789
791
790 print_method = ObjectName('_repr_json_')
792 print_method = ObjectName('_repr_json_')
791
793
792 def _check_return(self, r, obj):
794 def _check_return(self, r, obj):
793 """Check that a return value is appropriate
795 """Check that a return value is appropriate
794
796
795 Return the value if so, None otherwise, warning if invalid.
797 Return the value if so, None otherwise, warning if invalid.
796 """
798 """
797 if r is None:
799 if r is None:
798 return
800 return
799 md = None
801 md = None
800 if isinstance(r, tuple):
802 if isinstance(r, tuple):
801 # unpack data, metadata tuple for type checking on first element
803 # unpack data, metadata tuple for type checking on first element
802 r, md = r
804 r, md = r
803
805
804 # handle deprecated JSON-as-string form from IPython < 3
806 # handle deprecated JSON-as-string form from IPython < 3
805 if isinstance(r, string_types):
807 if isinstance(r, string_types):
806 warnings.warn("JSON expects JSONable list/dict containers, not JSON strings",
808 warnings.warn("JSON expects JSONable list/dict containers, not JSON strings",
807 FormatterWarning)
809 FormatterWarning)
808 r = json.loads(r)
810 r = json.loads(r)
809
811
810 if md is not None:
812 if md is not None:
811 # put the tuple back together
813 # put the tuple back together
812 r = (r, md)
814 r = (r, md)
813 return super(JSONFormatter, self)._check_return(r, obj)
815 return super(JSONFormatter, self)._check_return(r, obj)
814
816
815
817
816 class JavascriptFormatter(BaseFormatter):
818 class JavascriptFormatter(BaseFormatter):
817 """A Javascript formatter.
819 """A Javascript formatter.
818
820
819 To define the callables that compute the Javascript representation of
821 To define the callables that compute the Javascript representation of
820 your objects, define a :meth:`_repr_javascript_` method or use the
822 your objects, define a :meth:`_repr_javascript_` method or use the
821 :meth:`for_type` or :meth:`for_type_by_name` methods to register functions
823 :meth:`for_type` or :meth:`for_type_by_name` methods to register functions
822 that handle this.
824 that handle this.
823
825
824 The return value of this formatter should be valid Javascript code and
826 The return value of this formatter should be valid Javascript code and
825 should *not* be enclosed in ```<script>``` tags.
827 should *not* be enclosed in ```<script>``` tags.
826 """
828 """
827 format_type = Unicode('application/javascript')
829 format_type = Unicode('application/javascript')
828
830
829 print_method = ObjectName('_repr_javascript_')
831 print_method = ObjectName('_repr_javascript_')
830
832
831
833
832 class PDFFormatter(BaseFormatter):
834 class PDFFormatter(BaseFormatter):
833 """A PDF formatter.
835 """A PDF formatter.
834
836
835 To define the callables that compute the PDF representation of your
837 To define the callables that compute the PDF representation of your
836 objects, define a :meth:`_repr_pdf_` method or use the :meth:`for_type`
838 objects, define a :meth:`_repr_pdf_` method or use the :meth:`for_type`
837 or :meth:`for_type_by_name` methods to register functions that handle
839 or :meth:`for_type_by_name` methods to register functions that handle
838 this.
840 this.
839
841
840 The return value of this formatter should be raw PDF data, *not*
842 The return value of this formatter should be raw PDF data, *not*
841 base64 encoded.
843 base64 encoded.
842 """
844 """
843 format_type = Unicode('application/pdf')
845 format_type = Unicode('application/pdf')
844
846
845 print_method = ObjectName('_repr_pdf_')
847 print_method = ObjectName('_repr_pdf_')
846
848
847 _return_type = (bytes, unicode_type)
849 _return_type = (bytes, unicode_type)
848
850
849 class IPythonDisplayFormatter(BaseFormatter):
851 class IPythonDisplayFormatter(BaseFormatter):
850 """A Formatter for objects that know how to display themselves.
852 """A Formatter for objects that know how to display themselves.
851
853
852 To define the callables that compute the representation of your
854 To define the callables that compute the representation of your
853 objects, define a :meth:`_ipython_display_` method or use the :meth:`for_type`
855 objects, define a :meth:`_ipython_display_` method or use the :meth:`for_type`
854 or :meth:`for_type_by_name` methods to register functions that handle
856 or :meth:`for_type_by_name` methods to register functions that handle
855 this. Unlike mime-type displays, this method should not return anything,
857 this. Unlike mime-type displays, this method should not return anything,
856 instead calling any appropriate display methods itself.
858 instead calling any appropriate display methods itself.
857
859
858 This display formatter has highest priority.
860 This display formatter has highest priority.
859 If it fires, no other display formatter will be called.
861 If it fires, no other display formatter will be called.
860 """
862 """
861 print_method = ObjectName('_ipython_display_')
863 print_method = ObjectName('_ipython_display_')
862 _return_type = (type(None), bool)
864 _return_type = (type(None), bool)
863
865
864
866
865 @catch_format_error
867 @catch_format_error
866 def __call__(self, obj):
868 def __call__(self, obj):
867 """Compute the format for an object."""
869 """Compute the format for an object."""
868 if self.enabled:
870 if self.enabled:
869 # lookup registered printer
871 # lookup registered printer
870 try:
872 try:
871 printer = self.lookup(obj)
873 printer = self.lookup(obj)
872 except KeyError:
874 except KeyError:
873 pass
875 pass
874 else:
876 else:
875 printer(obj)
877 printer(obj)
876 return True
878 return True
877 # Finally look for special method names
879 # Finally look for special method names
878 method = get_real_method(obj, self.print_method)
880 method = get_real_method(obj, self.print_method)
879 if method is not None:
881 if method is not None:
880 method()
882 method()
881 return True
883 return True
882
884
883
885
884 FormatterABC.register(BaseFormatter)
886 FormatterABC.register(BaseFormatter)
885 FormatterABC.register(PlainTextFormatter)
887 FormatterABC.register(PlainTextFormatter)
886 FormatterABC.register(HTMLFormatter)
888 FormatterABC.register(HTMLFormatter)
887 FormatterABC.register(MarkdownFormatter)
889 FormatterABC.register(MarkdownFormatter)
888 FormatterABC.register(SVGFormatter)
890 FormatterABC.register(SVGFormatter)
889 FormatterABC.register(PNGFormatter)
891 FormatterABC.register(PNGFormatter)
890 FormatterABC.register(PDFFormatter)
892 FormatterABC.register(PDFFormatter)
891 FormatterABC.register(JPEGFormatter)
893 FormatterABC.register(JPEGFormatter)
892 FormatterABC.register(LatexFormatter)
894 FormatterABC.register(LatexFormatter)
893 FormatterABC.register(JSONFormatter)
895 FormatterABC.register(JSONFormatter)
894 FormatterABC.register(JavascriptFormatter)
896 FormatterABC.register(JavascriptFormatter)
895 FormatterABC.register(IPythonDisplayFormatter)
897 FormatterABC.register(IPythonDisplayFormatter)
896
898
897
899
898 def format_display_data(obj, include=None, exclude=None):
900 def format_display_data(obj, include=None, exclude=None):
899 """Return a format data dict for an object.
901 """Return a format data dict for an object.
900
902
901 By default all format types will be computed.
903 By default all format types will be computed.
902
904
903 The following MIME types are currently implemented:
905 The following MIME types are currently implemented:
904
906
905 * text/plain
907 * text/plain
906 * text/html
908 * text/html
907 * text/markdown
909 * text/markdown
908 * text/latex
910 * text/latex
909 * application/json
911 * application/json
910 * application/javascript
912 * application/javascript
911 * application/pdf
913 * application/pdf
912 * image/png
914 * image/png
913 * image/jpeg
915 * image/jpeg
914 * image/svg+xml
916 * image/svg+xml
915
917
916 Parameters
918 Parameters
917 ----------
919 ----------
918 obj : object
920 obj : object
919 The Python object whose format data will be computed.
921 The Python object whose format data will be computed.
920
922
921 Returns
923 Returns
922 -------
924 -------
923 format_dict : dict
925 format_dict : dict
924 A dictionary of key/value pairs, one or each format that was
926 A dictionary of key/value pairs, one or each format that was
925 generated for the object. The keys are the format types, which
927 generated for the object. The keys are the format types, which
926 will usually be MIME type strings and the values and JSON'able
928 will usually be MIME type strings and the values and JSON'able
927 data structure containing the raw data for the representation in
929 data structure containing the raw data for the representation in
928 that format.
930 that format.
929 include : list or tuple, optional
931 include : list or tuple, optional
930 A list of format type strings (MIME types) to include in the
932 A list of format type strings (MIME types) to include in the
931 format data dict. If this is set *only* the format types included
933 format data dict. If this is set *only* the format types included
932 in this list will be computed.
934 in this list will be computed.
933 exclude : list or tuple, optional
935 exclude : list or tuple, optional
934 A list of format type string (MIME types) to exclue in the format
936 A list of format type string (MIME types) to exclue in the format
935 data dict. If this is set all format types will be computed,
937 data dict. If this is set all format types will be computed,
936 except for those included in this argument.
938 except for those included in this argument.
937 """
939 """
938 from IPython.core.interactiveshell import InteractiveShell
940 from IPython.core.interactiveshell import InteractiveShell
939
941
940 return InteractiveShell.instance().display_formatter.format(
942 return InteractiveShell.instance().display_formatter.format(
941 obj,
943 obj,
942 include,
944 include,
943 exclude
945 exclude
944 )
946 )
945
947
@@ -1,3241 +1,3245 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """Main IPython class."""
2 """Main IPython class."""
3
3
4 #-----------------------------------------------------------------------------
4 #-----------------------------------------------------------------------------
5 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de>
5 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de>
6 # Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu>
6 # Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu>
7 # Copyright (C) 2008-2011 The IPython Development Team
7 # Copyright (C) 2008-2011 The IPython Development Team
8 #
8 #
9 # Distributed under the terms of the BSD License. The full license is in
9 # Distributed under the terms of the BSD License. The full license is in
10 # the file COPYING, distributed as part of this software.
10 # the file COPYING, distributed as part of this software.
11 #-----------------------------------------------------------------------------
11 #-----------------------------------------------------------------------------
12
12
13 from __future__ import absolute_import, print_function
13 from __future__ import absolute_import, print_function
14
14
15 import __future__
15 import __future__
16 import abc
16 import abc
17 import ast
17 import ast
18 import atexit
18 import atexit
19 import functools
19 import functools
20 import os
20 import os
21 import re
21 import re
22 import runpy
22 import runpy
23 import sys
23 import sys
24 import tempfile
24 import tempfile
25 import traceback
25 import traceback
26 import types
26 import types
27 import subprocess
27 import subprocess
28 import warnings
28 import warnings
29 from io import open as io_open
29 from io import open as io_open
30
30
31 from pickleshare import PickleShareDB
31 from pickleshare import PickleShareDB
32
32
33 from traitlets.config.configurable import SingletonConfigurable
33 from traitlets.config.configurable import SingletonConfigurable
34 from IPython.core import oinspect
34 from IPython.core import oinspect
35 from IPython.core import magic
35 from IPython.core import magic
36 from IPython.core import page
36 from IPython.core import page
37 from IPython.core import prefilter
37 from IPython.core import prefilter
38 from IPython.core import shadowns
38 from IPython.core import shadowns
39 from IPython.core import ultratb
39 from IPython.core import ultratb
40 from IPython.core.alias import Alias, AliasManager
40 from IPython.core.alias import Alias, AliasManager
41 from IPython.core.autocall import ExitAutocall
41 from IPython.core.autocall import ExitAutocall
42 from IPython.core.builtin_trap import BuiltinTrap
42 from IPython.core.builtin_trap import BuiltinTrap
43 from IPython.core.events import EventManager, available_events
43 from IPython.core.events import EventManager, available_events
44 from IPython.core.compilerop import CachingCompiler, check_linecache_ipython
44 from IPython.core.compilerop import CachingCompiler, check_linecache_ipython
45 from IPython.core.debugger import Pdb
45 from IPython.core.debugger import Pdb
46 from IPython.core.display_trap import DisplayTrap
46 from IPython.core.display_trap import DisplayTrap
47 from IPython.core.displayhook import DisplayHook
47 from IPython.core.displayhook import DisplayHook
48 from IPython.core.displaypub import DisplayPublisher
48 from IPython.core.displaypub import DisplayPublisher
49 from IPython.core.error import InputRejected, UsageError
49 from IPython.core.error import InputRejected, UsageError
50 from IPython.core.extensions import ExtensionManager
50 from IPython.core.extensions import ExtensionManager
51 from IPython.core.formatters import DisplayFormatter
51 from IPython.core.formatters import DisplayFormatter
52 from IPython.core.history import HistoryManager
52 from IPython.core.history import HistoryManager
53 from IPython.core.inputsplitter import ESC_MAGIC, ESC_MAGIC2
53 from IPython.core.inputsplitter import ESC_MAGIC, ESC_MAGIC2
54 from IPython.core.logger import Logger
54 from IPython.core.logger import Logger
55 from IPython.core.macro import Macro
55 from IPython.core.macro import Macro
56 from IPython.core.payload import PayloadManager
56 from IPython.core.payload import PayloadManager
57 from IPython.core.prefilter import PrefilterManager
57 from IPython.core.prefilter import PrefilterManager
58 from IPython.core.profiledir import ProfileDir
58 from IPython.core.profiledir import ProfileDir
59 from IPython.core.usage import default_banner
59 from IPython.core.usage import default_banner
60 from IPython.testing.skipdoctest import skip_doctest_py2, skip_doctest
60 from IPython.testing.skipdoctest import skip_doctest_py2, skip_doctest
61 from IPython.utils import PyColorize
61 from IPython.utils import PyColorize
62 from IPython.utils import io
62 from IPython.utils import io
63 from IPython.utils import py3compat
63 from IPython.utils import py3compat
64 from IPython.utils import openpy
64 from IPython.utils import openpy
65 from IPython.utils.decorators import undoc
65 from IPython.utils.decorators import undoc
66 from IPython.utils.io import ask_yes_no
66 from IPython.utils.io import ask_yes_no
67 from IPython.utils.ipstruct import Struct
67 from IPython.utils.ipstruct import Struct
68 from IPython.paths import get_ipython_dir
68 from IPython.paths import get_ipython_dir
69 from IPython.utils.path import get_home_dir, get_py_filename, ensure_dir_exists
69 from IPython.utils.path import get_home_dir, get_py_filename, ensure_dir_exists
70 from IPython.utils.process import system, getoutput
70 from IPython.utils.process import system, getoutput
71 from IPython.utils.py3compat import (builtin_mod, unicode_type, string_types,
71 from IPython.utils.py3compat import (builtin_mod, unicode_type, string_types,
72 with_metaclass, iteritems)
72 with_metaclass, iteritems)
73 from IPython.utils.strdispatch import StrDispatch
73 from IPython.utils.strdispatch import StrDispatch
74 from IPython.utils.syspathcontext import prepended_to_syspath
74 from IPython.utils.syspathcontext import prepended_to_syspath
75 from IPython.utils.text import format_screen, LSString, SList, DollarFormatter
75 from IPython.utils.text import format_screen, LSString, SList, DollarFormatter
76 from IPython.utils.tempdir import TemporaryDirectory
76 from IPython.utils.tempdir import TemporaryDirectory
77 from traitlets import (
77 from traitlets import (
78 Integer, Bool, CaselessStrEnum, Enum, List, Dict, Unicode, Instance, Type,
78 Integer, Bool, CaselessStrEnum, Enum, List, Dict, Unicode, Instance, Type,
79 observe, default,
79 observe, default,
80 )
80 )
81 from warnings import warn
81 from warnings import warn
82 from logging import error
82 from logging import error
83 import IPython.core.hooks
83 import IPython.core.hooks
84
84
85 # NoOpContext is deprecated, but ipykernel imports it from here.
85 # NoOpContext is deprecated, but ipykernel imports it from here.
86 # See https://github.com/ipython/ipykernel/issues/157
86 # See https://github.com/ipython/ipykernel/issues/157
87 from IPython.utils.contexts import NoOpContext
87 from IPython.utils.contexts import NoOpContext
88
88
89 try:
89 try:
90 import docrepr.sphinxify as sphx
90 import docrepr.sphinxify as sphx
91
91
92 def sphinxify(doc):
92 def sphinxify(doc):
93 with TemporaryDirectory() as dirname:
93 with TemporaryDirectory() as dirname:
94 return {
94 return {
95 'text/html': sphx.sphinxify(doc, dirname),
95 'text/html': sphx.sphinxify(doc, dirname),
96 'text/plain': doc
96 'text/plain': doc
97 }
97 }
98 except ImportError:
98 except ImportError:
99 sphinxify = None
99 sphinxify = None
100
100
101
101
102 class ProvisionalWarning(DeprecationWarning):
102 class ProvisionalWarning(DeprecationWarning):
103 """
103 """
104 Warning class for unstable features
104 Warning class for unstable features
105 """
105 """
106 pass
106 pass
107
107
108 #-----------------------------------------------------------------------------
108 #-----------------------------------------------------------------------------
109 # Globals
109 # Globals
110 #-----------------------------------------------------------------------------
110 #-----------------------------------------------------------------------------
111
111
112 # compiled regexps for autoindent management
112 # compiled regexps for autoindent management
113 dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass')
113 dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass')
114
114
115 #-----------------------------------------------------------------------------
115 #-----------------------------------------------------------------------------
116 # Utilities
116 # Utilities
117 #-----------------------------------------------------------------------------
117 #-----------------------------------------------------------------------------
118
118
119 @undoc
119 @undoc
120 def softspace(file, newvalue):
120 def softspace(file, newvalue):
121 """Copied from code.py, to remove the dependency"""
121 """Copied from code.py, to remove the dependency"""
122
122
123 oldvalue = 0
123 oldvalue = 0
124 try:
124 try:
125 oldvalue = file.softspace
125 oldvalue = file.softspace
126 except AttributeError:
126 except AttributeError:
127 pass
127 pass
128 try:
128 try:
129 file.softspace = newvalue
129 file.softspace = newvalue
130 except (AttributeError, TypeError):
130 except (AttributeError, TypeError):
131 # "attribute-less object" or "read-only attributes"
131 # "attribute-less object" or "read-only attributes"
132 pass
132 pass
133 return oldvalue
133 return oldvalue
134
134
135 @undoc
135 @undoc
136 def no_op(*a, **kw): pass
136 def no_op(*a, **kw): pass
137
137
138
138
139 class SpaceInInput(Exception): pass
139 class SpaceInInput(Exception): pass
140
140
141
141
142 def get_default_colors():
142 def get_default_colors():
143 "DEPRECATED"
143 "DEPRECATED"
144 warn('get_default_color is Deprecated, and is `Neutral` on all platforms.',
144 warn('get_default_color is Deprecated, and is `Neutral` on all platforms.',
145 DeprecationWarning, stacklevel=2)
145 DeprecationWarning, stacklevel=2)
146 return 'Neutral'
146 return 'Neutral'
147
147
148
148
149 class SeparateUnicode(Unicode):
149 class SeparateUnicode(Unicode):
150 r"""A Unicode subclass to validate separate_in, separate_out, etc.
150 r"""A Unicode subclass to validate separate_in, separate_out, etc.
151
151
152 This is a Unicode based trait that converts '0'->'' and ``'\\n'->'\n'``.
152 This is a Unicode based trait that converts '0'->'' and ``'\\n'->'\n'``.
153 """
153 """
154
154
155 def validate(self, obj, value):
155 def validate(self, obj, value):
156 if value == '0': value = ''
156 if value == '0': value = ''
157 value = value.replace('\\n','\n')
157 value = value.replace('\\n','\n')
158 return super(SeparateUnicode, self).validate(obj, value)
158 return super(SeparateUnicode, self).validate(obj, value)
159
159
160
160
161 @undoc
161 @undoc
162 class DummyMod(object):
162 class DummyMod(object):
163 """A dummy module used for IPython's interactive module when
163 """A dummy module used for IPython's interactive module when
164 a namespace must be assigned to the module's __dict__."""
164 a namespace must be assigned to the module's __dict__."""
165 pass
165 pass
166
166
167
167
168 class ExecutionResult(object):
168 class ExecutionResult(object):
169 """The result of a call to :meth:`InteractiveShell.run_cell`
169 """The result of a call to :meth:`InteractiveShell.run_cell`
170
170
171 Stores information about what took place.
171 Stores information about what took place.
172 """
172 """
173 execution_count = None
173 execution_count = None
174 error_before_exec = None
174 error_before_exec = None
175 error_in_exec = None
175 error_in_exec = None
176 result = None
176 result = None
177
177
178 @property
178 @property
179 def success(self):
179 def success(self):
180 return (self.error_before_exec is None) and (self.error_in_exec is None)
180 return (self.error_before_exec is None) and (self.error_in_exec is None)
181
181
182 def raise_error(self):
182 def raise_error(self):
183 """Reraises error if `success` is `False`, otherwise does nothing"""
183 """Reraises error if `success` is `False`, otherwise does nothing"""
184 if self.error_before_exec is not None:
184 if self.error_before_exec is not None:
185 raise self.error_before_exec
185 raise self.error_before_exec
186 if self.error_in_exec is not None:
186 if self.error_in_exec is not None:
187 raise self.error_in_exec
187 raise self.error_in_exec
188
188
189 def __repr__(self):
189 def __repr__(self):
190 if sys.version_info > (3,):
190 if sys.version_info > (3,):
191 name = self.__class__.__qualname__
191 name = self.__class__.__qualname__
192 else:
192 else:
193 name = self.__class__.__name__
193 name = self.__class__.__name__
194 return '<%s object at %x, execution_count=%s error_before_exec=%s error_in_exec=%s result=%s>' %\
194 return '<%s object at %x, execution_count=%s error_before_exec=%s error_in_exec=%s result=%s>' %\
195 (name, id(self), self.execution_count, self.error_before_exec, self.error_in_exec, repr(self.result))
195 (name, id(self), self.execution_count, self.error_before_exec, self.error_in_exec, repr(self.result))
196
196
197
197
198 class InteractiveShell(SingletonConfigurable):
198 class InteractiveShell(SingletonConfigurable):
199 """An enhanced, interactive shell for Python."""
199 """An enhanced, interactive shell for Python."""
200
200
201 _instance = None
201 _instance = None
202
202
203 ast_transformers = List([], help=
203 ast_transformers = List([], help=
204 """
204 """
205 A list of ast.NodeTransformer subclass instances, which will be applied
205 A list of ast.NodeTransformer subclass instances, which will be applied
206 to user input before code is run.
206 to user input before code is run.
207 """
207 """
208 ).tag(config=True)
208 ).tag(config=True)
209
209
210 autocall = Enum((0,1,2), default_value=0, help=
210 autocall = Enum((0,1,2), default_value=0, help=
211 """
211 """
212 Make IPython automatically call any callable object even if you didn't
212 Make IPython automatically call any callable object even if you didn't
213 type explicit parentheses. For example, 'str 43' becomes 'str(43)'
213 type explicit parentheses. For example, 'str 43' becomes 'str(43)'
214 automatically. The value can be '0' to disable the feature, '1' for
214 automatically. The value can be '0' to disable the feature, '1' for
215 'smart' autocall, where it is not applied if there are no more
215 'smart' autocall, where it is not applied if there are no more
216 arguments on the line, and '2' for 'full' autocall, where all callable
216 arguments on the line, and '2' for 'full' autocall, where all callable
217 objects are automatically called (even if no arguments are present).
217 objects are automatically called (even if no arguments are present).
218 """
218 """
219 ).tag(config=True)
219 ).tag(config=True)
220 # TODO: remove all autoindent logic and put into frontends.
220 # TODO: remove all autoindent logic and put into frontends.
221 # We can't do this yet because even runlines uses the autoindent.
221 # We can't do this yet because even runlines uses the autoindent.
222 autoindent = Bool(True, help=
222 autoindent = Bool(True, help=
223 """
223 """
224 Autoindent IPython code entered interactively.
224 Autoindent IPython code entered interactively.
225 """
225 """
226 ).tag(config=True)
226 ).tag(config=True)
227
227
228 automagic = Bool(True, help=
228 automagic = Bool(True, help=
229 """
229 """
230 Enable magic commands to be called without the leading %.
230 Enable magic commands to be called without the leading %.
231 """
231 """
232 ).tag(config=True)
232 ).tag(config=True)
233
233
234 banner1 = Unicode(default_banner,
234 banner1 = Unicode(default_banner,
235 help="""The part of the banner to be printed before the profile"""
235 help="""The part of the banner to be printed before the profile"""
236 ).tag(config=True)
236 ).tag(config=True)
237 banner2 = Unicode('',
237 banner2 = Unicode('',
238 help="""The part of the banner to be printed after the profile"""
238 help="""The part of the banner to be printed after the profile"""
239 ).tag(config=True)
239 ).tag(config=True)
240
240
241 cache_size = Integer(1000, help=
241 cache_size = Integer(1000, help=
242 """
242 """
243 Set the size of the output cache. The default is 1000, you can
243 Set the size of the output cache. The default is 1000, you can
244 change it permanently in your config file. Setting it to 0 completely
244 change it permanently in your config file. Setting it to 0 completely
245 disables the caching system, and the minimum value accepted is 20 (if
245 disables the caching system, and the minimum value accepted is 20 (if
246 you provide a value less than 20, it is reset to 0 and a warning is
246 you provide a value less than 20, it is reset to 0 and a warning is
247 issued). This limit is defined because otherwise you'll spend more
247 issued). This limit is defined because otherwise you'll spend more
248 time re-flushing a too small cache than working
248 time re-flushing a too small cache than working
249 """
249 """
250 ).tag(config=True)
250 ).tag(config=True)
251 color_info = Bool(True, help=
251 color_info = Bool(True, help=
252 """
252 """
253 Use colors for displaying information about objects. Because this
253 Use colors for displaying information about objects. Because this
254 information is passed through a pager (like 'less'), and some pagers
254 information is passed through a pager (like 'less'), and some pagers
255 get confused with color codes, this capability can be turned off.
255 get confused with color codes, this capability can be turned off.
256 """
256 """
257 ).tag(config=True)
257 ).tag(config=True)
258 colors = CaselessStrEnum(('Neutral', 'NoColor','LightBG','Linux'),
258 colors = CaselessStrEnum(('Neutral', 'NoColor','LightBG','Linux'),
259 default_value='Neutral',
259 default_value='Neutral',
260 help="Set the color scheme (NoColor, Neutral, Linux, or LightBG)."
260 help="Set the color scheme (NoColor, Neutral, Linux, or LightBG)."
261 ).tag(config=True)
261 ).tag(config=True)
262 debug = Bool(False).tag(config=True)
262 debug = Bool(False).tag(config=True)
263 deep_reload = Bool(False, help=
263 deep_reload = Bool(False, help=
264 """
264 """
265 **Deprecated**
265 **Deprecated**
266
266
267 Will be removed in IPython 6.0
267 Will be removed in IPython 6.0
268
268
269 Enable deep (recursive) reloading by default. IPython can use the
269 Enable deep (recursive) reloading by default. IPython can use the
270 deep_reload module which reloads changes in modules recursively (it
270 deep_reload module which reloads changes in modules recursively (it
271 replaces the reload() function, so you don't need to change anything to
271 replaces the reload() function, so you don't need to change anything to
272 use it). `deep_reload` forces a full reload of modules whose code may
272 use it). `deep_reload` forces a full reload of modules whose code may
273 have changed, which the default reload() function does not. When
273 have changed, which the default reload() function does not. When
274 deep_reload is off, IPython will use the normal reload(), but
274 deep_reload is off, IPython will use the normal reload(), but
275 deep_reload will still be available as dreload().
275 deep_reload will still be available as dreload().
276 """
276 """
277 ).tag(config=True)
277 ).tag(config=True)
278 disable_failing_post_execute = Bool(False,
278 disable_failing_post_execute = Bool(False,
279 help="Don't call post-execute functions that have failed in the past."
279 help="Don't call post-execute functions that have failed in the past."
280 ).tag(config=True)
280 ).tag(config=True)
281 display_formatter = Instance(DisplayFormatter, allow_none=True)
281 display_formatter = Instance(DisplayFormatter, allow_none=True)
282 displayhook_class = Type(DisplayHook)
282 displayhook_class = Type(DisplayHook)
283 display_pub_class = Type(DisplayPublisher)
283 display_pub_class = Type(DisplayPublisher)
284
284
285 sphinxify_docstring = Bool(False, help=
285 sphinxify_docstring = Bool(False, help=
286 """
286 """
287 Enables rich html representation of docstrings. (This requires the
287 Enables rich html representation of docstrings. (This requires the
288 docrepr module).
288 docrepr module).
289 """).tag(config=True)
289 """).tag(config=True)
290
290
291 @observe("sphinxify_docstring")
291 @observe("sphinxify_docstring")
292 def _sphinxify_docstring_changed(self, change):
292 def _sphinxify_docstring_changed(self, change):
293 if change['new']:
293 if change['new']:
294 warn("`sphinxify_docstring` is provisional since IPython 5.0 and might change in future versions." , ProvisionalWarning)
294 warn("`sphinxify_docstring` is provisional since IPython 5.0 and might change in future versions." , ProvisionalWarning)
295
295
296 enable_html_pager = Bool(False, help=
296 enable_html_pager = Bool(False, help=
297 """
297 """
298 (Provisional API) enables html representation in mime bundles sent
298 (Provisional API) enables html representation in mime bundles sent
299 to pagers.
299 to pagers.
300 """).tag(config=True)
300 """).tag(config=True)
301
301
302 @observe("enable_html_pager")
302 @observe("enable_html_pager")
303 def _enable_html_pager_changed(self, change):
303 def _enable_html_pager_changed(self, change):
304 if change['new']:
304 if change['new']:
305 warn("`enable_html_pager` is provisional since IPython 5.0 and might change in future versions.", ProvisionalWarning)
305 warn("`enable_html_pager` is provisional since IPython 5.0 and might change in future versions.", ProvisionalWarning)
306
306
307 data_pub_class = None
307 data_pub_class = None
308
308
309 exit_now = Bool(False)
309 exit_now = Bool(False)
310 exiter = Instance(ExitAutocall)
310 exiter = Instance(ExitAutocall)
311 @default('exiter')
311 @default('exiter')
312 def _exiter_default(self):
312 def _exiter_default(self):
313 return ExitAutocall(self)
313 return ExitAutocall(self)
314 # Monotonically increasing execution counter
314 # Monotonically increasing execution counter
315 execution_count = Integer(1)
315 execution_count = Integer(1)
316 filename = Unicode("<ipython console>")
316 filename = Unicode("<ipython console>")
317 ipython_dir= Unicode('').tag(config=True) # Set to get_ipython_dir() in __init__
317 ipython_dir= Unicode('').tag(config=True) # Set to get_ipython_dir() in __init__
318
318
319 # Input splitter, to transform input line by line and detect when a block
319 # Input splitter, to transform input line by line and detect when a block
320 # is ready to be executed.
320 # is ready to be executed.
321 input_splitter = Instance('IPython.core.inputsplitter.IPythonInputSplitter',
321 input_splitter = Instance('IPython.core.inputsplitter.IPythonInputSplitter',
322 (), {'line_input_checker': True})
322 (), {'line_input_checker': True})
323
323
324 # This InputSplitter instance is used to transform completed cells before
324 # This InputSplitter instance is used to transform completed cells before
325 # running them. It allows cell magics to contain blank lines.
325 # running them. It allows cell magics to contain blank lines.
326 input_transformer_manager = Instance('IPython.core.inputsplitter.IPythonInputSplitter',
326 input_transformer_manager = Instance('IPython.core.inputsplitter.IPythonInputSplitter',
327 (), {'line_input_checker': False})
327 (), {'line_input_checker': False})
328
328
329 logstart = Bool(False, help=
329 logstart = Bool(False, help=
330 """
330 """
331 Start logging to the default log file in overwrite mode.
331 Start logging to the default log file in overwrite mode.
332 Use `logappend` to specify a log file to **append** logs to.
332 Use `logappend` to specify a log file to **append** logs to.
333 """
333 """
334 ).tag(config=True)
334 ).tag(config=True)
335 logfile = Unicode('', help=
335 logfile = Unicode('', help=
336 """
336 """
337 The name of the logfile to use.
337 The name of the logfile to use.
338 """
338 """
339 ).tag(config=True)
339 ).tag(config=True)
340 logappend = Unicode('', help=
340 logappend = Unicode('', help=
341 """
341 """
342 Start logging to the given file in append mode.
342 Start logging to the given file in append mode.
343 Use `logfile` to specify a log file to **overwrite** logs to.
343 Use `logfile` to specify a log file to **overwrite** logs to.
344 """
344 """
345 ).tag(config=True)
345 ).tag(config=True)
346 object_info_string_level = Enum((0,1,2), default_value=0,
346 object_info_string_level = Enum((0,1,2), default_value=0,
347 ).tag(config=True)
347 ).tag(config=True)
348 pdb = Bool(False, help=
348 pdb = Bool(False, help=
349 """
349 """
350 Automatically call the pdb debugger after every exception.
350 Automatically call the pdb debugger after every exception.
351 """
351 """
352 ).tag(config=True)
352 ).tag(config=True)
353 display_page = Bool(False,
353 display_page = Bool(False,
354 help="""If True, anything that would be passed to the pager
354 help="""If True, anything that would be passed to the pager
355 will be displayed as regular output instead."""
355 will be displayed as regular output instead."""
356 ).tag(config=True)
356 ).tag(config=True)
357
357
358 # deprecated prompt traits:
358 # deprecated prompt traits:
359
359
360 prompt_in1 = Unicode('In [\\#]: ',
360 prompt_in1 = Unicode('In [\\#]: ',
361 help="Deprecated since IPython 4.0 and ignored since 5.0, set TerminalInteractiveShell.prompts object directly."
361 help="Deprecated since IPython 4.0 and ignored since 5.0, set TerminalInteractiveShell.prompts object directly."
362 ).tag(config=True)
362 ).tag(config=True)
363 prompt_in2 = Unicode(' .\\D.: ',
363 prompt_in2 = Unicode(' .\\D.: ',
364 help="Deprecated since IPython 4.0 and ignored since 5.0, set TerminalInteractiveShell.prompts object directly."
364 help="Deprecated since IPython 4.0 and ignored since 5.0, set TerminalInteractiveShell.prompts object directly."
365 ).tag(config=True)
365 ).tag(config=True)
366 prompt_out = Unicode('Out[\\#]: ',
366 prompt_out = Unicode('Out[\\#]: ',
367 help="Deprecated since IPython 4.0 and ignored since 5.0, set TerminalInteractiveShell.prompts object directly."
367 help="Deprecated since IPython 4.0 and ignored since 5.0, set TerminalInteractiveShell.prompts object directly."
368 ).tag(config=True)
368 ).tag(config=True)
369 prompts_pad_left = Bool(True,
369 prompts_pad_left = Bool(True,
370 help="Deprecated since IPython 4.0 and ignored since 5.0, set TerminalInteractiveShell.prompts object directly."
370 help="Deprecated since IPython 4.0 and ignored since 5.0, set TerminalInteractiveShell.prompts object directly."
371 ).tag(config=True)
371 ).tag(config=True)
372
372
373 @observe('prompt_in1', 'prompt_in2', 'prompt_out', 'prompt_pad_left')
373 @observe('prompt_in1', 'prompt_in2', 'prompt_out', 'prompt_pad_left')
374 def _prompt_trait_changed(self, change):
374 def _prompt_trait_changed(self, change):
375 name = change['name']
375 name = change['name']
376 warn("InteractiveShell.{name} is deprecated since IPython 4.0 and ignored since 5.0, set TerminalInteractiveShell.prompts object directly.".format(
376 warn("InteractiveShell.{name} is deprecated since IPython 4.0 and ignored since 5.0, set TerminalInteractiveShell.prompts object directly.".format(
377 name=name)
377 name=name)
378 )
378 )
379 # protect against weird cases where self.config may not exist:
379 # protect against weird cases where self.config may not exist:
380
380
381 show_rewritten_input = Bool(True,
381 show_rewritten_input = Bool(True,
382 help="Show rewritten input, e.g. for autocall."
382 help="Show rewritten input, e.g. for autocall."
383 ).tag(config=True)
383 ).tag(config=True)
384
384
385 quiet = Bool(False).tag(config=True)
385 quiet = Bool(False).tag(config=True)
386
386
387 history_length = Integer(10000,
387 history_length = Integer(10000,
388 help='Total length of command history'
388 help='Total length of command history'
389 ).tag(config=True)
389 ).tag(config=True)
390
390
391 history_load_length = Integer(1000, help=
391 history_load_length = Integer(1000, help=
392 """
392 """
393 The number of saved history entries to be loaded
393 The number of saved history entries to be loaded
394 into the history buffer at startup.
394 into the history buffer at startup.
395 """
395 """
396 ).tag(config=True)
396 ).tag(config=True)
397
397
398 ast_node_interactivity = Enum(['all', 'last', 'last_expr', 'none'],
398 ast_node_interactivity = Enum(['all', 'last', 'last_expr', 'none'],
399 default_value='last_expr',
399 default_value='last_expr',
400 help="""
400 help="""
401 'all', 'last', 'last_expr' or 'none', specifying which nodes should be
401 'all', 'last', 'last_expr' or 'none', specifying which nodes should be
402 run interactively (displaying output from expressions)."""
402 run interactively (displaying output from expressions)."""
403 ).tag(config=True)
403 ).tag(config=True)
404
404
405 # TODO: this part of prompt management should be moved to the frontends.
405 # TODO: this part of prompt management should be moved to the frontends.
406 # Use custom TraitTypes that convert '0'->'' and '\\n'->'\n'
406 # Use custom TraitTypes that convert '0'->'' and '\\n'->'\n'
407 separate_in = SeparateUnicode('\n').tag(config=True)
407 separate_in = SeparateUnicode('\n').tag(config=True)
408 separate_out = SeparateUnicode('').tag(config=True)
408 separate_out = SeparateUnicode('').tag(config=True)
409 separate_out2 = SeparateUnicode('').tag(config=True)
409 separate_out2 = SeparateUnicode('').tag(config=True)
410 wildcards_case_sensitive = Bool(True).tag(config=True)
410 wildcards_case_sensitive = Bool(True).tag(config=True)
411 xmode = CaselessStrEnum(('Context','Plain', 'Verbose'),
411 xmode = CaselessStrEnum(('Context','Plain', 'Verbose'),
412 default_value='Context').tag(config=True)
412 default_value='Context').tag(config=True)
413
413
414 # Subcomponents of InteractiveShell
414 # Subcomponents of InteractiveShell
415 alias_manager = Instance('IPython.core.alias.AliasManager', allow_none=True)
415 alias_manager = Instance('IPython.core.alias.AliasManager', allow_none=True)
416 prefilter_manager = Instance('IPython.core.prefilter.PrefilterManager', allow_none=True)
416 prefilter_manager = Instance('IPython.core.prefilter.PrefilterManager', allow_none=True)
417 builtin_trap = Instance('IPython.core.builtin_trap.BuiltinTrap', allow_none=True)
417 builtin_trap = Instance('IPython.core.builtin_trap.BuiltinTrap', allow_none=True)
418 display_trap = Instance('IPython.core.display_trap.DisplayTrap', allow_none=True)
418 display_trap = Instance('IPython.core.display_trap.DisplayTrap', allow_none=True)
419 extension_manager = Instance('IPython.core.extensions.ExtensionManager', allow_none=True)
419 extension_manager = Instance('IPython.core.extensions.ExtensionManager', allow_none=True)
420 payload_manager = Instance('IPython.core.payload.PayloadManager', allow_none=True)
420 payload_manager = Instance('IPython.core.payload.PayloadManager', allow_none=True)
421 history_manager = Instance('IPython.core.history.HistoryAccessorBase', allow_none=True)
421 history_manager = Instance('IPython.core.history.HistoryAccessorBase', allow_none=True)
422 magics_manager = Instance('IPython.core.magic.MagicsManager', allow_none=True)
422 magics_manager = Instance('IPython.core.magic.MagicsManager', allow_none=True)
423
423
424 profile_dir = Instance('IPython.core.application.ProfileDir', allow_none=True)
424 profile_dir = Instance('IPython.core.application.ProfileDir', allow_none=True)
425 @property
425 @property
426 def profile(self):
426 def profile(self):
427 if self.profile_dir is not None:
427 if self.profile_dir is not None:
428 name = os.path.basename(self.profile_dir.location)
428 name = os.path.basename(self.profile_dir.location)
429 return name.replace('profile_','')
429 return name.replace('profile_','')
430
430
431
431
432 # Private interface
432 # Private interface
433 _post_execute = Dict()
433 _post_execute = Dict()
434
434
435 # Tracks any GUI loop loaded for pylab
435 # Tracks any GUI loop loaded for pylab
436 pylab_gui_select = None
436 pylab_gui_select = None
437
437
438 last_execution_succeeded = Bool(True, help='Did last executed command succeeded')
438 last_execution_succeeded = Bool(True, help='Did last executed command succeeded')
439
439
440 def __init__(self, ipython_dir=None, profile_dir=None,
440 def __init__(self, ipython_dir=None, profile_dir=None,
441 user_module=None, user_ns=None,
441 user_module=None, user_ns=None,
442 custom_exceptions=((), None), **kwargs):
442 custom_exceptions=((), None), **kwargs):
443
443
444 # This is where traits with a config_key argument are updated
444 # This is where traits with a config_key argument are updated
445 # from the values on config.
445 # from the values on config.
446 super(InteractiveShell, self).__init__(**kwargs)
446 super(InteractiveShell, self).__init__(**kwargs)
447 if 'PromptManager' in self.config:
447 if 'PromptManager' in self.config:
448 warn('As of IPython 5.0 `PromptManager` config will have no effect'
448 warn('As of IPython 5.0 `PromptManager` config will have no effect'
449 ' and has been replaced by TerminalInteractiveShell.prompts_class')
449 ' and has been replaced by TerminalInteractiveShell.prompts_class')
450 self.configurables = [self]
450 self.configurables = [self]
451
451
452 # These are relatively independent and stateless
452 # These are relatively independent and stateless
453 self.init_ipython_dir(ipython_dir)
453 self.init_ipython_dir(ipython_dir)
454 self.init_profile_dir(profile_dir)
454 self.init_profile_dir(profile_dir)
455 self.init_instance_attrs()
455 self.init_instance_attrs()
456 self.init_environment()
456 self.init_environment()
457
457
458 # Check if we're in a virtualenv, and set up sys.path.
458 # Check if we're in a virtualenv, and set up sys.path.
459 self.init_virtualenv()
459 self.init_virtualenv()
460
460
461 # Create namespaces (user_ns, user_global_ns, etc.)
461 # Create namespaces (user_ns, user_global_ns, etc.)
462 self.init_create_namespaces(user_module, user_ns)
462 self.init_create_namespaces(user_module, user_ns)
463 # This has to be done after init_create_namespaces because it uses
463 # This has to be done after init_create_namespaces because it uses
464 # something in self.user_ns, but before init_sys_modules, which
464 # something in self.user_ns, but before init_sys_modules, which
465 # is the first thing to modify sys.
465 # is the first thing to modify sys.
466 # TODO: When we override sys.stdout and sys.stderr before this class
466 # TODO: When we override sys.stdout and sys.stderr before this class
467 # is created, we are saving the overridden ones here. Not sure if this
467 # is created, we are saving the overridden ones here. Not sure if this
468 # is what we want to do.
468 # is what we want to do.
469 self.save_sys_module_state()
469 self.save_sys_module_state()
470 self.init_sys_modules()
470 self.init_sys_modules()
471
471
472 # While we're trying to have each part of the code directly access what
472 # While we're trying to have each part of the code directly access what
473 # it needs without keeping redundant references to objects, we have too
473 # it needs without keeping redundant references to objects, we have too
474 # much legacy code that expects ip.db to exist.
474 # much legacy code that expects ip.db to exist.
475 self.db = PickleShareDB(os.path.join(self.profile_dir.location, 'db'))
475 self.db = PickleShareDB(os.path.join(self.profile_dir.location, 'db'))
476
476
477 self.init_history()
477 self.init_history()
478 self.init_encoding()
478 self.init_encoding()
479 self.init_prefilter()
479 self.init_prefilter()
480
480
481 self.init_syntax_highlighting()
481 self.init_syntax_highlighting()
482 self.init_hooks()
482 self.init_hooks()
483 self.init_events()
483 self.init_events()
484 self.init_pushd_popd_magic()
484 self.init_pushd_popd_magic()
485 self.init_user_ns()
485 self.init_user_ns()
486 self.init_logger()
486 self.init_logger()
487 self.init_builtins()
487 self.init_builtins()
488
488
489 # The following was in post_config_initialization
489 # The following was in post_config_initialization
490 self.init_inspector()
490 self.init_inspector()
491 if py3compat.PY3:
491 if py3compat.PY3:
492 self.raw_input_original = input
492 self.raw_input_original = input
493 else:
493 else:
494 self.raw_input_original = raw_input
494 self.raw_input_original = raw_input
495 self.init_completer()
495 self.init_completer()
496 # TODO: init_io() needs to happen before init_traceback handlers
496 # TODO: init_io() needs to happen before init_traceback handlers
497 # because the traceback handlers hardcode the stdout/stderr streams.
497 # because the traceback handlers hardcode the stdout/stderr streams.
498 # This logic in in debugger.Pdb and should eventually be changed.
498 # This logic in in debugger.Pdb and should eventually be changed.
499 self.init_io()
499 self.init_io()
500 self.init_traceback_handlers(custom_exceptions)
500 self.init_traceback_handlers(custom_exceptions)
501 self.init_prompts()
501 self.init_prompts()
502 self.init_display_formatter()
502 self.init_display_formatter()
503 self.init_display_pub()
503 self.init_display_pub()
504 self.init_data_pub()
504 self.init_data_pub()
505 self.init_displayhook()
505 self.init_displayhook()
506 self.init_magics()
506 self.init_magics()
507 self.init_alias()
507 self.init_alias()
508 self.init_logstart()
508 self.init_logstart()
509 self.init_pdb()
509 self.init_pdb()
510 self.init_extension_manager()
510 self.init_extension_manager()
511 self.init_payload()
511 self.init_payload()
512 self.init_deprecation_warnings()
512 self.init_deprecation_warnings()
513 self.hooks.late_startup_hook()
513 self.hooks.late_startup_hook()
514 self.events.trigger('shell_initialized', self)
514 self.events.trigger('shell_initialized', self)
515 atexit.register(self.atexit_operations)
515 atexit.register(self.atexit_operations)
516
516
517 def get_ipython(self):
517 def get_ipython(self):
518 """Return the currently running IPython instance."""
518 """Return the currently running IPython instance."""
519 return self
519 return self
520
520
521 #-------------------------------------------------------------------------
521 #-------------------------------------------------------------------------
522 # Trait changed handlers
522 # Trait changed handlers
523 #-------------------------------------------------------------------------
523 #-------------------------------------------------------------------------
524 @observe('ipython_dir')
524 @observe('ipython_dir')
525 def _ipython_dir_changed(self, change):
525 def _ipython_dir_changed(self, change):
526 ensure_dir_exists(change['new'])
526 ensure_dir_exists(change['new'])
527
527
528 def set_autoindent(self,value=None):
528 def set_autoindent(self,value=None):
529 """Set the autoindent flag.
529 """Set the autoindent flag.
530
530
531 If called with no arguments, it acts as a toggle."""
531 If called with no arguments, it acts as a toggle."""
532 if value is None:
532 if value is None:
533 self.autoindent = not self.autoindent
533 self.autoindent = not self.autoindent
534 else:
534 else:
535 self.autoindent = value
535 self.autoindent = value
536
536
537 #-------------------------------------------------------------------------
537 #-------------------------------------------------------------------------
538 # init_* methods called by __init__
538 # init_* methods called by __init__
539 #-------------------------------------------------------------------------
539 #-------------------------------------------------------------------------
540
540
541 def init_ipython_dir(self, ipython_dir):
541 def init_ipython_dir(self, ipython_dir):
542 if ipython_dir is not None:
542 if ipython_dir is not None:
543 self.ipython_dir = ipython_dir
543 self.ipython_dir = ipython_dir
544 return
544 return
545
545
546 self.ipython_dir = get_ipython_dir()
546 self.ipython_dir = get_ipython_dir()
547
547
548 def init_profile_dir(self, profile_dir):
548 def init_profile_dir(self, profile_dir):
549 if profile_dir is not None:
549 if profile_dir is not None:
550 self.profile_dir = profile_dir
550 self.profile_dir = profile_dir
551 return
551 return
552 self.profile_dir =\
552 self.profile_dir =\
553 ProfileDir.create_profile_dir_by_name(self.ipython_dir, 'default')
553 ProfileDir.create_profile_dir_by_name(self.ipython_dir, 'default')
554
554
555 def init_instance_attrs(self):
555 def init_instance_attrs(self):
556 self.more = False
556 self.more = False
557
557
558 # command compiler
558 # command compiler
559 self.compile = CachingCompiler()
559 self.compile = CachingCompiler()
560
560
561 # Make an empty namespace, which extension writers can rely on both
561 # Make an empty namespace, which extension writers can rely on both
562 # existing and NEVER being used by ipython itself. This gives them a
562 # existing and NEVER being used by ipython itself. This gives them a
563 # convenient location for storing additional information and state
563 # convenient location for storing additional information and state
564 # their extensions may require, without fear of collisions with other
564 # their extensions may require, without fear of collisions with other
565 # ipython names that may develop later.
565 # ipython names that may develop later.
566 self.meta = Struct()
566 self.meta = Struct()
567
567
568 # Temporary files used for various purposes. Deleted at exit.
568 # Temporary files used for various purposes. Deleted at exit.
569 self.tempfiles = []
569 self.tempfiles = []
570 self.tempdirs = []
570 self.tempdirs = []
571
571
572 # keep track of where we started running (mainly for crash post-mortem)
572 # keep track of where we started running (mainly for crash post-mortem)
573 # This is not being used anywhere currently.
573 # This is not being used anywhere currently.
574 self.starting_dir = py3compat.getcwd()
574 self.starting_dir = py3compat.getcwd()
575
575
576 # Indentation management
576 # Indentation management
577 self.indent_current_nsp = 0
577 self.indent_current_nsp = 0
578
578
579 # Dict to track post-execution functions that have been registered
579 # Dict to track post-execution functions that have been registered
580 self._post_execute = {}
580 self._post_execute = {}
581
581
582 def init_environment(self):
582 def init_environment(self):
583 """Any changes we need to make to the user's environment."""
583 """Any changes we need to make to the user's environment."""
584 pass
584 pass
585
585
586 def init_encoding(self):
586 def init_encoding(self):
587 # Get system encoding at startup time. Certain terminals (like Emacs
587 # Get system encoding at startup time. Certain terminals (like Emacs
588 # under Win32 have it set to None, and we need to have a known valid
588 # under Win32 have it set to None, and we need to have a known valid
589 # encoding to use in the raw_input() method
589 # encoding to use in the raw_input() method
590 try:
590 try:
591 self.stdin_encoding = sys.stdin.encoding or 'ascii'
591 self.stdin_encoding = sys.stdin.encoding or 'ascii'
592 except AttributeError:
592 except AttributeError:
593 self.stdin_encoding = 'ascii'
593 self.stdin_encoding = 'ascii'
594
594
595 def init_syntax_highlighting(self):
595 def init_syntax_highlighting(self):
596 # Python source parser/formatter for syntax highlighting
596 # Python source parser/formatter for syntax highlighting
597 pyformat = PyColorize.Parser().format
597 pyformat = PyColorize.Parser().format
598 self.pycolorize = lambda src: pyformat(src,'str',self.colors)
598 self.pycolorize = lambda src: pyformat(src,'str',self.colors)
599
599
600 def refresh_style(self):
600 def refresh_style(self):
601 # No-op here, used in subclass
601 # No-op here, used in subclass
602 pass
602 pass
603
603
604 def init_pushd_popd_magic(self):
604 def init_pushd_popd_magic(self):
605 # for pushd/popd management
605 # for pushd/popd management
606 self.home_dir = get_home_dir()
606 self.home_dir = get_home_dir()
607
607
608 self.dir_stack = []
608 self.dir_stack = []
609
609
610 def init_logger(self):
610 def init_logger(self):
611 self.logger = Logger(self.home_dir, logfname='ipython_log.py',
611 self.logger = Logger(self.home_dir, logfname='ipython_log.py',
612 logmode='rotate')
612 logmode='rotate')
613
613
614 def init_logstart(self):
614 def init_logstart(self):
615 """Initialize logging in case it was requested at the command line.
615 """Initialize logging in case it was requested at the command line.
616 """
616 """
617 if self.logappend:
617 if self.logappend:
618 self.magic('logstart %s append' % self.logappend)
618 self.magic('logstart %s append' % self.logappend)
619 elif self.logfile:
619 elif self.logfile:
620 self.magic('logstart %s' % self.logfile)
620 self.magic('logstart %s' % self.logfile)
621 elif self.logstart:
621 elif self.logstart:
622 self.magic('logstart')
622 self.magic('logstart')
623
623
624 def init_deprecation_warnings(self):
624 def init_deprecation_warnings(self):
625 """
625 """
626 register default filter for deprecation warning.
626 register default filter for deprecation warning.
627
627
628 This will allow deprecation warning of function used interactively to show
628 This will allow deprecation warning of function used interactively to show
629 warning to users, and still hide deprecation warning from libraries import.
629 warning to users, and still hide deprecation warning from libraries import.
630 """
630 """
631 warnings.filterwarnings("default", category=DeprecationWarning, module=self.user_ns.get("__name__"))
631 warnings.filterwarnings("default", category=DeprecationWarning, module=self.user_ns.get("__name__"))
632
632
633 def init_builtins(self):
633 def init_builtins(self):
634 # A single, static flag that we set to True. Its presence indicates
634 # A single, static flag that we set to True. Its presence indicates
635 # that an IPython shell has been created, and we make no attempts at
635 # that an IPython shell has been created, and we make no attempts at
636 # removing on exit or representing the existence of more than one
636 # removing on exit or representing the existence of more than one
637 # IPython at a time.
637 # IPython at a time.
638 builtin_mod.__dict__['__IPYTHON__'] = True
638 builtin_mod.__dict__['__IPYTHON__'] = True
639
639
640 self.builtin_trap = BuiltinTrap(shell=self)
640 self.builtin_trap = BuiltinTrap(shell=self)
641
641
642 def init_inspector(self):
642 def init_inspector(self):
643 # Object inspector
643 # Object inspector
644 self.inspector = oinspect.Inspector(oinspect.InspectColors,
644 self.inspector = oinspect.Inspector(oinspect.InspectColors,
645 PyColorize.ANSICodeColors,
645 PyColorize.ANSICodeColors,
646 'NoColor',
646 'NoColor',
647 self.object_info_string_level)
647 self.object_info_string_level)
648
648
649 def init_io(self):
649 def init_io(self):
650 # This will just use sys.stdout and sys.stderr. If you want to
650 # This will just use sys.stdout and sys.stderr. If you want to
651 # override sys.stdout and sys.stderr themselves, you need to do that
651 # override sys.stdout and sys.stderr themselves, you need to do that
652 # *before* instantiating this class, because io holds onto
652 # *before* instantiating this class, because io holds onto
653 # references to the underlying streams.
653 # references to the underlying streams.
654 io.stdout = io.IOStream(sys.stdout)
654 # io.std* are deprecated, but don't show our own deprecation warnings
655 io.stderr = io.IOStream(sys.stderr)
655 # during initialization of the deprecated API.
656 with warnings.catch_warnings():
657 warnings.simplefilter('ignore', DeprecationWarning)
658 io.stdout = io.IOStream(sys.stdout)
659 io.stderr = io.IOStream(sys.stderr)
656
660
657 def init_prompts(self):
661 def init_prompts(self):
658 # Set system prompts, so that scripts can decide if they are running
662 # Set system prompts, so that scripts can decide if they are running
659 # interactively.
663 # interactively.
660 sys.ps1 = 'In : '
664 sys.ps1 = 'In : '
661 sys.ps2 = '...: '
665 sys.ps2 = '...: '
662 sys.ps3 = 'Out: '
666 sys.ps3 = 'Out: '
663
667
664 def init_display_formatter(self):
668 def init_display_formatter(self):
665 self.display_formatter = DisplayFormatter(parent=self)
669 self.display_formatter = DisplayFormatter(parent=self)
666 self.configurables.append(self.display_formatter)
670 self.configurables.append(self.display_formatter)
667
671
668 def init_display_pub(self):
672 def init_display_pub(self):
669 self.display_pub = self.display_pub_class(parent=self)
673 self.display_pub = self.display_pub_class(parent=self)
670 self.configurables.append(self.display_pub)
674 self.configurables.append(self.display_pub)
671
675
672 def init_data_pub(self):
676 def init_data_pub(self):
673 if not self.data_pub_class:
677 if not self.data_pub_class:
674 self.data_pub = None
678 self.data_pub = None
675 return
679 return
676 self.data_pub = self.data_pub_class(parent=self)
680 self.data_pub = self.data_pub_class(parent=self)
677 self.configurables.append(self.data_pub)
681 self.configurables.append(self.data_pub)
678
682
679 def init_displayhook(self):
683 def init_displayhook(self):
680 # Initialize displayhook, set in/out prompts and printing system
684 # Initialize displayhook, set in/out prompts and printing system
681 self.displayhook = self.displayhook_class(
685 self.displayhook = self.displayhook_class(
682 parent=self,
686 parent=self,
683 shell=self,
687 shell=self,
684 cache_size=self.cache_size,
688 cache_size=self.cache_size,
685 )
689 )
686 self.configurables.append(self.displayhook)
690 self.configurables.append(self.displayhook)
687 # This is a context manager that installs/revmoes the displayhook at
691 # This is a context manager that installs/revmoes the displayhook at
688 # the appropriate time.
692 # the appropriate time.
689 self.display_trap = DisplayTrap(hook=self.displayhook)
693 self.display_trap = DisplayTrap(hook=self.displayhook)
690
694
691 def init_virtualenv(self):
695 def init_virtualenv(self):
692 """Add a virtualenv to sys.path so the user can import modules from it.
696 """Add a virtualenv to sys.path so the user can import modules from it.
693 This isn't perfect: it doesn't use the Python interpreter with which the
697 This isn't perfect: it doesn't use the Python interpreter with which the
694 virtualenv was built, and it ignores the --no-site-packages option. A
698 virtualenv was built, and it ignores the --no-site-packages option. A
695 warning will appear suggesting the user installs IPython in the
699 warning will appear suggesting the user installs IPython in the
696 virtualenv, but for many cases, it probably works well enough.
700 virtualenv, but for many cases, it probably works well enough.
697
701
698 Adapted from code snippets online.
702 Adapted from code snippets online.
699
703
700 http://blog.ufsoft.org/2009/1/29/ipython-and-virtualenv
704 http://blog.ufsoft.org/2009/1/29/ipython-and-virtualenv
701 """
705 """
702 if 'VIRTUAL_ENV' not in os.environ:
706 if 'VIRTUAL_ENV' not in os.environ:
703 # Not in a virtualenv
707 # Not in a virtualenv
704 return
708 return
705
709
706 # venv detection:
710 # venv detection:
707 # stdlib venv may symlink sys.executable, so we can't use realpath.
711 # stdlib venv may symlink sys.executable, so we can't use realpath.
708 # but others can symlink *to* the venv Python, so we can't just use sys.executable.
712 # but others can symlink *to* the venv Python, so we can't just use sys.executable.
709 # So we just check every item in the symlink tree (generally <= 3)
713 # So we just check every item in the symlink tree (generally <= 3)
710 p = os.path.normcase(sys.executable)
714 p = os.path.normcase(sys.executable)
711 paths = [p]
715 paths = [p]
712 while os.path.islink(p):
716 while os.path.islink(p):
713 p = os.path.normcase(os.path.join(os.path.dirname(p), os.readlink(p)))
717 p = os.path.normcase(os.path.join(os.path.dirname(p), os.readlink(p)))
714 paths.append(p)
718 paths.append(p)
715 p_venv = os.path.normcase(os.environ['VIRTUAL_ENV'])
719 p_venv = os.path.normcase(os.environ['VIRTUAL_ENV'])
716 if any(p.startswith(p_venv) for p in paths):
720 if any(p.startswith(p_venv) for p in paths):
717 # Running properly in the virtualenv, don't need to do anything
721 # Running properly in the virtualenv, don't need to do anything
718 return
722 return
719
723
720 warn("Attempting to work in a virtualenv. If you encounter problems, please "
724 warn("Attempting to work in a virtualenv. If you encounter problems, please "
721 "install IPython inside the virtualenv.")
725 "install IPython inside the virtualenv.")
722 if sys.platform == "win32":
726 if sys.platform == "win32":
723 virtual_env = os.path.join(os.environ['VIRTUAL_ENV'], 'Lib', 'site-packages')
727 virtual_env = os.path.join(os.environ['VIRTUAL_ENV'], 'Lib', 'site-packages')
724 else:
728 else:
725 virtual_env = os.path.join(os.environ['VIRTUAL_ENV'], 'lib',
729 virtual_env = os.path.join(os.environ['VIRTUAL_ENV'], 'lib',
726 'python%d.%d' % sys.version_info[:2], 'site-packages')
730 'python%d.%d' % sys.version_info[:2], 'site-packages')
727
731
728 import site
732 import site
729 sys.path.insert(0, virtual_env)
733 sys.path.insert(0, virtual_env)
730 site.addsitedir(virtual_env)
734 site.addsitedir(virtual_env)
731
735
732 #-------------------------------------------------------------------------
736 #-------------------------------------------------------------------------
733 # Things related to injections into the sys module
737 # Things related to injections into the sys module
734 #-------------------------------------------------------------------------
738 #-------------------------------------------------------------------------
735
739
736 def save_sys_module_state(self):
740 def save_sys_module_state(self):
737 """Save the state of hooks in the sys module.
741 """Save the state of hooks in the sys module.
738
742
739 This has to be called after self.user_module is created.
743 This has to be called after self.user_module is created.
740 """
744 """
741 self._orig_sys_module_state = {'stdin': sys.stdin,
745 self._orig_sys_module_state = {'stdin': sys.stdin,
742 'stdout': sys.stdout,
746 'stdout': sys.stdout,
743 'stderr': sys.stderr,
747 'stderr': sys.stderr,
744 'excepthook': sys.excepthook}
748 'excepthook': sys.excepthook}
745 self._orig_sys_modules_main_name = self.user_module.__name__
749 self._orig_sys_modules_main_name = self.user_module.__name__
746 self._orig_sys_modules_main_mod = sys.modules.get(self.user_module.__name__)
750 self._orig_sys_modules_main_mod = sys.modules.get(self.user_module.__name__)
747
751
748 def restore_sys_module_state(self):
752 def restore_sys_module_state(self):
749 """Restore the state of the sys module."""
753 """Restore the state of the sys module."""
750 try:
754 try:
751 for k, v in iteritems(self._orig_sys_module_state):
755 for k, v in iteritems(self._orig_sys_module_state):
752 setattr(sys, k, v)
756 setattr(sys, k, v)
753 except AttributeError:
757 except AttributeError:
754 pass
758 pass
755 # Reset what what done in self.init_sys_modules
759 # Reset what what done in self.init_sys_modules
756 if self._orig_sys_modules_main_mod is not None:
760 if self._orig_sys_modules_main_mod is not None:
757 sys.modules[self._orig_sys_modules_main_name] = self._orig_sys_modules_main_mod
761 sys.modules[self._orig_sys_modules_main_name] = self._orig_sys_modules_main_mod
758
762
759 #-------------------------------------------------------------------------
763 #-------------------------------------------------------------------------
760 # Things related to the banner
764 # Things related to the banner
761 #-------------------------------------------------------------------------
765 #-------------------------------------------------------------------------
762
766
763 @property
767 @property
764 def banner(self):
768 def banner(self):
765 banner = self.banner1
769 banner = self.banner1
766 if self.profile and self.profile != 'default':
770 if self.profile and self.profile != 'default':
767 banner += '\nIPython profile: %s\n' % self.profile
771 banner += '\nIPython profile: %s\n' % self.profile
768 if self.banner2:
772 if self.banner2:
769 banner += '\n' + self.banner2
773 banner += '\n' + self.banner2
770 return banner
774 return banner
771
775
772 def show_banner(self, banner=None):
776 def show_banner(self, banner=None):
773 if banner is None:
777 if banner is None:
774 banner = self.banner
778 banner = self.banner
775 sys.stdout.write(banner)
779 sys.stdout.write(banner)
776
780
777 #-------------------------------------------------------------------------
781 #-------------------------------------------------------------------------
778 # Things related to hooks
782 # Things related to hooks
779 #-------------------------------------------------------------------------
783 #-------------------------------------------------------------------------
780
784
781 def init_hooks(self):
785 def init_hooks(self):
782 # hooks holds pointers used for user-side customizations
786 # hooks holds pointers used for user-side customizations
783 self.hooks = Struct()
787 self.hooks = Struct()
784
788
785 self.strdispatchers = {}
789 self.strdispatchers = {}
786
790
787 # Set all default hooks, defined in the IPython.hooks module.
791 # Set all default hooks, defined in the IPython.hooks module.
788 hooks = IPython.core.hooks
792 hooks = IPython.core.hooks
789 for hook_name in hooks.__all__:
793 for hook_name in hooks.__all__:
790 # default hooks have priority 100, i.e. low; user hooks should have
794 # default hooks have priority 100, i.e. low; user hooks should have
791 # 0-100 priority
795 # 0-100 priority
792 self.set_hook(hook_name,getattr(hooks,hook_name), 100, _warn_deprecated=False)
796 self.set_hook(hook_name,getattr(hooks,hook_name), 100, _warn_deprecated=False)
793
797
794 if self.display_page:
798 if self.display_page:
795 self.set_hook('show_in_pager', page.as_hook(page.display_page), 90)
799 self.set_hook('show_in_pager', page.as_hook(page.display_page), 90)
796
800
797 def set_hook(self,name,hook, priority=50, str_key=None, re_key=None,
801 def set_hook(self,name,hook, priority=50, str_key=None, re_key=None,
798 _warn_deprecated=True):
802 _warn_deprecated=True):
799 """set_hook(name,hook) -> sets an internal IPython hook.
803 """set_hook(name,hook) -> sets an internal IPython hook.
800
804
801 IPython exposes some of its internal API as user-modifiable hooks. By
805 IPython exposes some of its internal API as user-modifiable hooks. By
802 adding your function to one of these hooks, you can modify IPython's
806 adding your function to one of these hooks, you can modify IPython's
803 behavior to call at runtime your own routines."""
807 behavior to call at runtime your own routines."""
804
808
805 # At some point in the future, this should validate the hook before it
809 # At some point in the future, this should validate the hook before it
806 # accepts it. Probably at least check that the hook takes the number
810 # accepts it. Probably at least check that the hook takes the number
807 # of args it's supposed to.
811 # of args it's supposed to.
808
812
809 f = types.MethodType(hook,self)
813 f = types.MethodType(hook,self)
810
814
811 # check if the hook is for strdispatcher first
815 # check if the hook is for strdispatcher first
812 if str_key is not None:
816 if str_key is not None:
813 sdp = self.strdispatchers.get(name, StrDispatch())
817 sdp = self.strdispatchers.get(name, StrDispatch())
814 sdp.add_s(str_key, f, priority )
818 sdp.add_s(str_key, f, priority )
815 self.strdispatchers[name] = sdp
819 self.strdispatchers[name] = sdp
816 return
820 return
817 if re_key is not None:
821 if re_key is not None:
818 sdp = self.strdispatchers.get(name, StrDispatch())
822 sdp = self.strdispatchers.get(name, StrDispatch())
819 sdp.add_re(re.compile(re_key), f, priority )
823 sdp.add_re(re.compile(re_key), f, priority )
820 self.strdispatchers[name] = sdp
824 self.strdispatchers[name] = sdp
821 return
825 return
822
826
823 dp = getattr(self.hooks, name, None)
827 dp = getattr(self.hooks, name, None)
824 if name not in IPython.core.hooks.__all__:
828 if name not in IPython.core.hooks.__all__:
825 print("Warning! Hook '%s' is not one of %s" % \
829 print("Warning! Hook '%s' is not one of %s" % \
826 (name, IPython.core.hooks.__all__ ))
830 (name, IPython.core.hooks.__all__ ))
827
831
828 if _warn_deprecated and (name in IPython.core.hooks.deprecated):
832 if _warn_deprecated and (name in IPython.core.hooks.deprecated):
829 alternative = IPython.core.hooks.deprecated[name]
833 alternative = IPython.core.hooks.deprecated[name]
830 warn("Hook {} is deprecated. Use {} instead.".format(name, alternative))
834 warn("Hook {} is deprecated. Use {} instead.".format(name, alternative))
831
835
832 if not dp:
836 if not dp:
833 dp = IPython.core.hooks.CommandChainDispatcher()
837 dp = IPython.core.hooks.CommandChainDispatcher()
834
838
835 try:
839 try:
836 dp.add(f,priority)
840 dp.add(f,priority)
837 except AttributeError:
841 except AttributeError:
838 # it was not commandchain, plain old func - replace
842 # it was not commandchain, plain old func - replace
839 dp = f
843 dp = f
840
844
841 setattr(self.hooks,name, dp)
845 setattr(self.hooks,name, dp)
842
846
843 #-------------------------------------------------------------------------
847 #-------------------------------------------------------------------------
844 # Things related to events
848 # Things related to events
845 #-------------------------------------------------------------------------
849 #-------------------------------------------------------------------------
846
850
847 def init_events(self):
851 def init_events(self):
848 self.events = EventManager(self, available_events)
852 self.events = EventManager(self, available_events)
849
853
850 self.events.register("pre_execute", self._clear_warning_registry)
854 self.events.register("pre_execute", self._clear_warning_registry)
851
855
852 def register_post_execute(self, func):
856 def register_post_execute(self, func):
853 """DEPRECATED: Use ip.events.register('post_run_cell', func)
857 """DEPRECATED: Use ip.events.register('post_run_cell', func)
854
858
855 Register a function for calling after code execution.
859 Register a function for calling after code execution.
856 """
860 """
857 warn("ip.register_post_execute is deprecated, use "
861 warn("ip.register_post_execute is deprecated, use "
858 "ip.events.register('post_run_cell', func) instead.")
862 "ip.events.register('post_run_cell', func) instead.")
859 self.events.register('post_run_cell', func)
863 self.events.register('post_run_cell', func)
860
864
861 def _clear_warning_registry(self):
865 def _clear_warning_registry(self):
862 # clear the warning registry, so that different code blocks with
866 # clear the warning registry, so that different code blocks with
863 # overlapping line number ranges don't cause spurious suppression of
867 # overlapping line number ranges don't cause spurious suppression of
864 # warnings (see gh-6611 for details)
868 # warnings (see gh-6611 for details)
865 if "__warningregistry__" in self.user_global_ns:
869 if "__warningregistry__" in self.user_global_ns:
866 del self.user_global_ns["__warningregistry__"]
870 del self.user_global_ns["__warningregistry__"]
867
871
868 #-------------------------------------------------------------------------
872 #-------------------------------------------------------------------------
869 # Things related to the "main" module
873 # Things related to the "main" module
870 #-------------------------------------------------------------------------
874 #-------------------------------------------------------------------------
871
875
872 def new_main_mod(self, filename, modname):
876 def new_main_mod(self, filename, modname):
873 """Return a new 'main' module object for user code execution.
877 """Return a new 'main' module object for user code execution.
874
878
875 ``filename`` should be the path of the script which will be run in the
879 ``filename`` should be the path of the script which will be run in the
876 module. Requests with the same filename will get the same module, with
880 module. Requests with the same filename will get the same module, with
877 its namespace cleared.
881 its namespace cleared.
878
882
879 ``modname`` should be the module name - normally either '__main__' or
883 ``modname`` should be the module name - normally either '__main__' or
880 the basename of the file without the extension.
884 the basename of the file without the extension.
881
885
882 When scripts are executed via %run, we must keep a reference to their
886 When scripts are executed via %run, we must keep a reference to their
883 __main__ module around so that Python doesn't
887 __main__ module around so that Python doesn't
884 clear it, rendering references to module globals useless.
888 clear it, rendering references to module globals useless.
885
889
886 This method keeps said reference in a private dict, keyed by the
890 This method keeps said reference in a private dict, keyed by the
887 absolute path of the script. This way, for multiple executions of the
891 absolute path of the script. This way, for multiple executions of the
888 same script we only keep one copy of the namespace (the last one),
892 same script we only keep one copy of the namespace (the last one),
889 thus preventing memory leaks from old references while allowing the
893 thus preventing memory leaks from old references while allowing the
890 objects from the last execution to be accessible.
894 objects from the last execution to be accessible.
891 """
895 """
892 filename = os.path.abspath(filename)
896 filename = os.path.abspath(filename)
893 try:
897 try:
894 main_mod = self._main_mod_cache[filename]
898 main_mod = self._main_mod_cache[filename]
895 except KeyError:
899 except KeyError:
896 main_mod = self._main_mod_cache[filename] = types.ModuleType(
900 main_mod = self._main_mod_cache[filename] = types.ModuleType(
897 py3compat.cast_bytes_py2(modname),
901 py3compat.cast_bytes_py2(modname),
898 doc="Module created for script run in IPython")
902 doc="Module created for script run in IPython")
899 else:
903 else:
900 main_mod.__dict__.clear()
904 main_mod.__dict__.clear()
901 main_mod.__name__ = modname
905 main_mod.__name__ = modname
902
906
903 main_mod.__file__ = filename
907 main_mod.__file__ = filename
904 # It seems pydoc (and perhaps others) needs any module instance to
908 # It seems pydoc (and perhaps others) needs any module instance to
905 # implement a __nonzero__ method
909 # implement a __nonzero__ method
906 main_mod.__nonzero__ = lambda : True
910 main_mod.__nonzero__ = lambda : True
907
911
908 return main_mod
912 return main_mod
909
913
910 def clear_main_mod_cache(self):
914 def clear_main_mod_cache(self):
911 """Clear the cache of main modules.
915 """Clear the cache of main modules.
912
916
913 Mainly for use by utilities like %reset.
917 Mainly for use by utilities like %reset.
914
918
915 Examples
919 Examples
916 --------
920 --------
917
921
918 In [15]: import IPython
922 In [15]: import IPython
919
923
920 In [16]: m = _ip.new_main_mod(IPython.__file__, 'IPython')
924 In [16]: m = _ip.new_main_mod(IPython.__file__, 'IPython')
921
925
922 In [17]: len(_ip._main_mod_cache) > 0
926 In [17]: len(_ip._main_mod_cache) > 0
923 Out[17]: True
927 Out[17]: True
924
928
925 In [18]: _ip.clear_main_mod_cache()
929 In [18]: _ip.clear_main_mod_cache()
926
930
927 In [19]: len(_ip._main_mod_cache) == 0
931 In [19]: len(_ip._main_mod_cache) == 0
928 Out[19]: True
932 Out[19]: True
929 """
933 """
930 self._main_mod_cache.clear()
934 self._main_mod_cache.clear()
931
935
932 #-------------------------------------------------------------------------
936 #-------------------------------------------------------------------------
933 # Things related to debugging
937 # Things related to debugging
934 #-------------------------------------------------------------------------
938 #-------------------------------------------------------------------------
935
939
936 def init_pdb(self):
940 def init_pdb(self):
937 # Set calling of pdb on exceptions
941 # Set calling of pdb on exceptions
938 # self.call_pdb is a property
942 # self.call_pdb is a property
939 self.call_pdb = self.pdb
943 self.call_pdb = self.pdb
940
944
941 def _get_call_pdb(self):
945 def _get_call_pdb(self):
942 return self._call_pdb
946 return self._call_pdb
943
947
944 def _set_call_pdb(self,val):
948 def _set_call_pdb(self,val):
945
949
946 if val not in (0,1,False,True):
950 if val not in (0,1,False,True):
947 raise ValueError('new call_pdb value must be boolean')
951 raise ValueError('new call_pdb value must be boolean')
948
952
949 # store value in instance
953 # store value in instance
950 self._call_pdb = val
954 self._call_pdb = val
951
955
952 # notify the actual exception handlers
956 # notify the actual exception handlers
953 self.InteractiveTB.call_pdb = val
957 self.InteractiveTB.call_pdb = val
954
958
955 call_pdb = property(_get_call_pdb,_set_call_pdb,None,
959 call_pdb = property(_get_call_pdb,_set_call_pdb,None,
956 'Control auto-activation of pdb at exceptions')
960 'Control auto-activation of pdb at exceptions')
957
961
958 def debugger(self,force=False):
962 def debugger(self,force=False):
959 """Call the pdb debugger.
963 """Call the pdb debugger.
960
964
961 Keywords:
965 Keywords:
962
966
963 - force(False): by default, this routine checks the instance call_pdb
967 - force(False): by default, this routine checks the instance call_pdb
964 flag and does not actually invoke the debugger if the flag is false.
968 flag and does not actually invoke the debugger if the flag is false.
965 The 'force' option forces the debugger to activate even if the flag
969 The 'force' option forces the debugger to activate even if the flag
966 is false.
970 is false.
967 """
971 """
968
972
969 if not (force or self.call_pdb):
973 if not (force or self.call_pdb):
970 return
974 return
971
975
972 if not hasattr(sys,'last_traceback'):
976 if not hasattr(sys,'last_traceback'):
973 error('No traceback has been produced, nothing to debug.')
977 error('No traceback has been produced, nothing to debug.')
974 return
978 return
975
979
976 self.InteractiveTB.debugger(force=True)
980 self.InteractiveTB.debugger(force=True)
977
981
978 #-------------------------------------------------------------------------
982 #-------------------------------------------------------------------------
979 # Things related to IPython's various namespaces
983 # Things related to IPython's various namespaces
980 #-------------------------------------------------------------------------
984 #-------------------------------------------------------------------------
981 default_user_namespaces = True
985 default_user_namespaces = True
982
986
983 def init_create_namespaces(self, user_module=None, user_ns=None):
987 def init_create_namespaces(self, user_module=None, user_ns=None):
984 # Create the namespace where the user will operate. user_ns is
988 # Create the namespace where the user will operate. user_ns is
985 # normally the only one used, and it is passed to the exec calls as
989 # normally the only one used, and it is passed to the exec calls as
986 # the locals argument. But we do carry a user_global_ns namespace
990 # the locals argument. But we do carry a user_global_ns namespace
987 # given as the exec 'globals' argument, This is useful in embedding
991 # given as the exec 'globals' argument, This is useful in embedding
988 # situations where the ipython shell opens in a context where the
992 # situations where the ipython shell opens in a context where the
989 # distinction between locals and globals is meaningful. For
993 # distinction between locals and globals is meaningful. For
990 # non-embedded contexts, it is just the same object as the user_ns dict.
994 # non-embedded contexts, it is just the same object as the user_ns dict.
991
995
992 # FIXME. For some strange reason, __builtins__ is showing up at user
996 # FIXME. For some strange reason, __builtins__ is showing up at user
993 # level as a dict instead of a module. This is a manual fix, but I
997 # level as a dict instead of a module. This is a manual fix, but I
994 # should really track down where the problem is coming from. Alex
998 # should really track down where the problem is coming from. Alex
995 # Schmolck reported this problem first.
999 # Schmolck reported this problem first.
996
1000
997 # A useful post by Alex Martelli on this topic:
1001 # A useful post by Alex Martelli on this topic:
998 # Re: inconsistent value from __builtins__
1002 # Re: inconsistent value from __builtins__
999 # Von: Alex Martelli <aleaxit@yahoo.com>
1003 # Von: Alex Martelli <aleaxit@yahoo.com>
1000 # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends
1004 # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends
1001 # Gruppen: comp.lang.python
1005 # Gruppen: comp.lang.python
1002
1006
1003 # Michael Hohn <hohn@hooknose.lbl.gov> wrote:
1007 # Michael Hohn <hohn@hooknose.lbl.gov> wrote:
1004 # > >>> print type(builtin_check.get_global_binding('__builtins__'))
1008 # > >>> print type(builtin_check.get_global_binding('__builtins__'))
1005 # > <type 'dict'>
1009 # > <type 'dict'>
1006 # > >>> print type(__builtins__)
1010 # > >>> print type(__builtins__)
1007 # > <type 'module'>
1011 # > <type 'module'>
1008 # > Is this difference in return value intentional?
1012 # > Is this difference in return value intentional?
1009
1013
1010 # Well, it's documented that '__builtins__' can be either a dictionary
1014 # Well, it's documented that '__builtins__' can be either a dictionary
1011 # or a module, and it's been that way for a long time. Whether it's
1015 # or a module, and it's been that way for a long time. Whether it's
1012 # intentional (or sensible), I don't know. In any case, the idea is
1016 # intentional (or sensible), I don't know. In any case, the idea is
1013 # that if you need to access the built-in namespace directly, you
1017 # that if you need to access the built-in namespace directly, you
1014 # should start with "import __builtin__" (note, no 's') which will
1018 # should start with "import __builtin__" (note, no 's') which will
1015 # definitely give you a module. Yeah, it's somewhat confusing:-(.
1019 # definitely give you a module. Yeah, it's somewhat confusing:-(.
1016
1020
1017 # These routines return a properly built module and dict as needed by
1021 # These routines return a properly built module and dict as needed by
1018 # the rest of the code, and can also be used by extension writers to
1022 # the rest of the code, and can also be used by extension writers to
1019 # generate properly initialized namespaces.
1023 # generate properly initialized namespaces.
1020 if (user_ns is not None) or (user_module is not None):
1024 if (user_ns is not None) or (user_module is not None):
1021 self.default_user_namespaces = False
1025 self.default_user_namespaces = False
1022 self.user_module, self.user_ns = self.prepare_user_module(user_module, user_ns)
1026 self.user_module, self.user_ns = self.prepare_user_module(user_module, user_ns)
1023
1027
1024 # A record of hidden variables we have added to the user namespace, so
1028 # A record of hidden variables we have added to the user namespace, so
1025 # we can list later only variables defined in actual interactive use.
1029 # we can list later only variables defined in actual interactive use.
1026 self.user_ns_hidden = {}
1030 self.user_ns_hidden = {}
1027
1031
1028 # Now that FakeModule produces a real module, we've run into a nasty
1032 # Now that FakeModule produces a real module, we've run into a nasty
1029 # problem: after script execution (via %run), the module where the user
1033 # problem: after script execution (via %run), the module where the user
1030 # code ran is deleted. Now that this object is a true module (needed
1034 # code ran is deleted. Now that this object is a true module (needed
1031 # so doctest and other tools work correctly), the Python module
1035 # so doctest and other tools work correctly), the Python module
1032 # teardown mechanism runs over it, and sets to None every variable
1036 # teardown mechanism runs over it, and sets to None every variable
1033 # present in that module. Top-level references to objects from the
1037 # present in that module. Top-level references to objects from the
1034 # script survive, because the user_ns is updated with them. However,
1038 # script survive, because the user_ns is updated with them. However,
1035 # calling functions defined in the script that use other things from
1039 # calling functions defined in the script that use other things from
1036 # the script will fail, because the function's closure had references
1040 # the script will fail, because the function's closure had references
1037 # to the original objects, which are now all None. So we must protect
1041 # to the original objects, which are now all None. So we must protect
1038 # these modules from deletion by keeping a cache.
1042 # these modules from deletion by keeping a cache.
1039 #
1043 #
1040 # To avoid keeping stale modules around (we only need the one from the
1044 # To avoid keeping stale modules around (we only need the one from the
1041 # last run), we use a dict keyed with the full path to the script, so
1045 # last run), we use a dict keyed with the full path to the script, so
1042 # only the last version of the module is held in the cache. Note,
1046 # only the last version of the module is held in the cache. Note,
1043 # however, that we must cache the module *namespace contents* (their
1047 # however, that we must cache the module *namespace contents* (their
1044 # __dict__). Because if we try to cache the actual modules, old ones
1048 # __dict__). Because if we try to cache the actual modules, old ones
1045 # (uncached) could be destroyed while still holding references (such as
1049 # (uncached) could be destroyed while still holding references (such as
1046 # those held by GUI objects that tend to be long-lived)>
1050 # those held by GUI objects that tend to be long-lived)>
1047 #
1051 #
1048 # The %reset command will flush this cache. See the cache_main_mod()
1052 # The %reset command will flush this cache. See the cache_main_mod()
1049 # and clear_main_mod_cache() methods for details on use.
1053 # and clear_main_mod_cache() methods for details on use.
1050
1054
1051 # This is the cache used for 'main' namespaces
1055 # This is the cache used for 'main' namespaces
1052 self._main_mod_cache = {}
1056 self._main_mod_cache = {}
1053
1057
1054 # A table holding all the namespaces IPython deals with, so that
1058 # A table holding all the namespaces IPython deals with, so that
1055 # introspection facilities can search easily.
1059 # introspection facilities can search easily.
1056 self.ns_table = {'user_global':self.user_module.__dict__,
1060 self.ns_table = {'user_global':self.user_module.__dict__,
1057 'user_local':self.user_ns,
1061 'user_local':self.user_ns,
1058 'builtin':builtin_mod.__dict__
1062 'builtin':builtin_mod.__dict__
1059 }
1063 }
1060
1064
1061 @property
1065 @property
1062 def user_global_ns(self):
1066 def user_global_ns(self):
1063 return self.user_module.__dict__
1067 return self.user_module.__dict__
1064
1068
1065 def prepare_user_module(self, user_module=None, user_ns=None):
1069 def prepare_user_module(self, user_module=None, user_ns=None):
1066 """Prepare the module and namespace in which user code will be run.
1070 """Prepare the module and namespace in which user code will be run.
1067
1071
1068 When IPython is started normally, both parameters are None: a new module
1072 When IPython is started normally, both parameters are None: a new module
1069 is created automatically, and its __dict__ used as the namespace.
1073 is created automatically, and its __dict__ used as the namespace.
1070
1074
1071 If only user_module is provided, its __dict__ is used as the namespace.
1075 If only user_module is provided, its __dict__ is used as the namespace.
1072 If only user_ns is provided, a dummy module is created, and user_ns
1076 If only user_ns is provided, a dummy module is created, and user_ns
1073 becomes the global namespace. If both are provided (as they may be
1077 becomes the global namespace. If both are provided (as they may be
1074 when embedding), user_ns is the local namespace, and user_module
1078 when embedding), user_ns is the local namespace, and user_module
1075 provides the global namespace.
1079 provides the global namespace.
1076
1080
1077 Parameters
1081 Parameters
1078 ----------
1082 ----------
1079 user_module : module, optional
1083 user_module : module, optional
1080 The current user module in which IPython is being run. If None,
1084 The current user module in which IPython is being run. If None,
1081 a clean module will be created.
1085 a clean module will be created.
1082 user_ns : dict, optional
1086 user_ns : dict, optional
1083 A namespace in which to run interactive commands.
1087 A namespace in which to run interactive commands.
1084
1088
1085 Returns
1089 Returns
1086 -------
1090 -------
1087 A tuple of user_module and user_ns, each properly initialised.
1091 A tuple of user_module and user_ns, each properly initialised.
1088 """
1092 """
1089 if user_module is None and user_ns is not None:
1093 if user_module is None and user_ns is not None:
1090 user_ns.setdefault("__name__", "__main__")
1094 user_ns.setdefault("__name__", "__main__")
1091 user_module = DummyMod()
1095 user_module = DummyMod()
1092 user_module.__dict__ = user_ns
1096 user_module.__dict__ = user_ns
1093
1097
1094 if user_module is None:
1098 if user_module is None:
1095 user_module = types.ModuleType("__main__",
1099 user_module = types.ModuleType("__main__",
1096 doc="Automatically created module for IPython interactive environment")
1100 doc="Automatically created module for IPython interactive environment")
1097
1101
1098 # We must ensure that __builtin__ (without the final 's') is always
1102 # We must ensure that __builtin__ (without the final 's') is always
1099 # available and pointing to the __builtin__ *module*. For more details:
1103 # available and pointing to the __builtin__ *module*. For more details:
1100 # http://mail.python.org/pipermail/python-dev/2001-April/014068.html
1104 # http://mail.python.org/pipermail/python-dev/2001-April/014068.html
1101 user_module.__dict__.setdefault('__builtin__', builtin_mod)
1105 user_module.__dict__.setdefault('__builtin__', builtin_mod)
1102 user_module.__dict__.setdefault('__builtins__', builtin_mod)
1106 user_module.__dict__.setdefault('__builtins__', builtin_mod)
1103
1107
1104 if user_ns is None:
1108 if user_ns is None:
1105 user_ns = user_module.__dict__
1109 user_ns = user_module.__dict__
1106
1110
1107 return user_module, user_ns
1111 return user_module, user_ns
1108
1112
1109 def init_sys_modules(self):
1113 def init_sys_modules(self):
1110 # We need to insert into sys.modules something that looks like a
1114 # We need to insert into sys.modules something that looks like a
1111 # module but which accesses the IPython namespace, for shelve and
1115 # module but which accesses the IPython namespace, for shelve and
1112 # pickle to work interactively. Normally they rely on getting
1116 # pickle to work interactively. Normally they rely on getting
1113 # everything out of __main__, but for embedding purposes each IPython
1117 # everything out of __main__, but for embedding purposes each IPython
1114 # instance has its own private namespace, so we can't go shoving
1118 # instance has its own private namespace, so we can't go shoving
1115 # everything into __main__.
1119 # everything into __main__.
1116
1120
1117 # note, however, that we should only do this for non-embedded
1121 # note, however, that we should only do this for non-embedded
1118 # ipythons, which really mimic the __main__.__dict__ with their own
1122 # ipythons, which really mimic the __main__.__dict__ with their own
1119 # namespace. Embedded instances, on the other hand, should not do
1123 # namespace. Embedded instances, on the other hand, should not do
1120 # this because they need to manage the user local/global namespaces
1124 # this because they need to manage the user local/global namespaces
1121 # only, but they live within a 'normal' __main__ (meaning, they
1125 # only, but they live within a 'normal' __main__ (meaning, they
1122 # shouldn't overtake the execution environment of the script they're
1126 # shouldn't overtake the execution environment of the script they're
1123 # embedded in).
1127 # embedded in).
1124
1128
1125 # This is overridden in the InteractiveShellEmbed subclass to a no-op.
1129 # This is overridden in the InteractiveShellEmbed subclass to a no-op.
1126 main_name = self.user_module.__name__
1130 main_name = self.user_module.__name__
1127 sys.modules[main_name] = self.user_module
1131 sys.modules[main_name] = self.user_module
1128
1132
1129 def init_user_ns(self):
1133 def init_user_ns(self):
1130 """Initialize all user-visible namespaces to their minimum defaults.
1134 """Initialize all user-visible namespaces to their minimum defaults.
1131
1135
1132 Certain history lists are also initialized here, as they effectively
1136 Certain history lists are also initialized here, as they effectively
1133 act as user namespaces.
1137 act as user namespaces.
1134
1138
1135 Notes
1139 Notes
1136 -----
1140 -----
1137 All data structures here are only filled in, they are NOT reset by this
1141 All data structures here are only filled in, they are NOT reset by this
1138 method. If they were not empty before, data will simply be added to
1142 method. If they were not empty before, data will simply be added to
1139 therm.
1143 therm.
1140 """
1144 """
1141 # This function works in two parts: first we put a few things in
1145 # This function works in two parts: first we put a few things in
1142 # user_ns, and we sync that contents into user_ns_hidden so that these
1146 # user_ns, and we sync that contents into user_ns_hidden so that these
1143 # initial variables aren't shown by %who. After the sync, we add the
1147 # initial variables aren't shown by %who. After the sync, we add the
1144 # rest of what we *do* want the user to see with %who even on a new
1148 # rest of what we *do* want the user to see with %who even on a new
1145 # session (probably nothing, so they really only see their own stuff)
1149 # session (probably nothing, so they really only see their own stuff)
1146
1150
1147 # The user dict must *always* have a __builtin__ reference to the
1151 # The user dict must *always* have a __builtin__ reference to the
1148 # Python standard __builtin__ namespace, which must be imported.
1152 # Python standard __builtin__ namespace, which must be imported.
1149 # This is so that certain operations in prompt evaluation can be
1153 # This is so that certain operations in prompt evaluation can be
1150 # reliably executed with builtins. Note that we can NOT use
1154 # reliably executed with builtins. Note that we can NOT use
1151 # __builtins__ (note the 's'), because that can either be a dict or a
1155 # __builtins__ (note the 's'), because that can either be a dict or a
1152 # module, and can even mutate at runtime, depending on the context
1156 # module, and can even mutate at runtime, depending on the context
1153 # (Python makes no guarantees on it). In contrast, __builtin__ is
1157 # (Python makes no guarantees on it). In contrast, __builtin__ is
1154 # always a module object, though it must be explicitly imported.
1158 # always a module object, though it must be explicitly imported.
1155
1159
1156 # For more details:
1160 # For more details:
1157 # http://mail.python.org/pipermail/python-dev/2001-April/014068.html
1161 # http://mail.python.org/pipermail/python-dev/2001-April/014068.html
1158 ns = dict()
1162 ns = dict()
1159
1163
1160 # make global variables for user access to the histories
1164 # make global variables for user access to the histories
1161 ns['_ih'] = self.history_manager.input_hist_parsed
1165 ns['_ih'] = self.history_manager.input_hist_parsed
1162 ns['_oh'] = self.history_manager.output_hist
1166 ns['_oh'] = self.history_manager.output_hist
1163 ns['_dh'] = self.history_manager.dir_hist
1167 ns['_dh'] = self.history_manager.dir_hist
1164
1168
1165 ns['_sh'] = shadowns
1169 ns['_sh'] = shadowns
1166
1170
1167 # user aliases to input and output histories. These shouldn't show up
1171 # user aliases to input and output histories. These shouldn't show up
1168 # in %who, as they can have very large reprs.
1172 # in %who, as they can have very large reprs.
1169 ns['In'] = self.history_manager.input_hist_parsed
1173 ns['In'] = self.history_manager.input_hist_parsed
1170 ns['Out'] = self.history_manager.output_hist
1174 ns['Out'] = self.history_manager.output_hist
1171
1175
1172 # Store myself as the public api!!!
1176 # Store myself as the public api!!!
1173 ns['get_ipython'] = self.get_ipython
1177 ns['get_ipython'] = self.get_ipython
1174
1178
1175 ns['exit'] = self.exiter
1179 ns['exit'] = self.exiter
1176 ns['quit'] = self.exiter
1180 ns['quit'] = self.exiter
1177
1181
1178 # Sync what we've added so far to user_ns_hidden so these aren't seen
1182 # Sync what we've added so far to user_ns_hidden so these aren't seen
1179 # by %who
1183 # by %who
1180 self.user_ns_hidden.update(ns)
1184 self.user_ns_hidden.update(ns)
1181
1185
1182 # Anything put into ns now would show up in %who. Think twice before
1186 # Anything put into ns now would show up in %who. Think twice before
1183 # putting anything here, as we really want %who to show the user their
1187 # putting anything here, as we really want %who to show the user their
1184 # stuff, not our variables.
1188 # stuff, not our variables.
1185
1189
1186 # Finally, update the real user's namespace
1190 # Finally, update the real user's namespace
1187 self.user_ns.update(ns)
1191 self.user_ns.update(ns)
1188
1192
1189 @property
1193 @property
1190 def all_ns_refs(self):
1194 def all_ns_refs(self):
1191 """Get a list of references to all the namespace dictionaries in which
1195 """Get a list of references to all the namespace dictionaries in which
1192 IPython might store a user-created object.
1196 IPython might store a user-created object.
1193
1197
1194 Note that this does not include the displayhook, which also caches
1198 Note that this does not include the displayhook, which also caches
1195 objects from the output."""
1199 objects from the output."""
1196 return [self.user_ns, self.user_global_ns, self.user_ns_hidden] + \
1200 return [self.user_ns, self.user_global_ns, self.user_ns_hidden] + \
1197 [m.__dict__ for m in self._main_mod_cache.values()]
1201 [m.__dict__ for m in self._main_mod_cache.values()]
1198
1202
1199 def reset(self, new_session=True):
1203 def reset(self, new_session=True):
1200 """Clear all internal namespaces, and attempt to release references to
1204 """Clear all internal namespaces, and attempt to release references to
1201 user objects.
1205 user objects.
1202
1206
1203 If new_session is True, a new history session will be opened.
1207 If new_session is True, a new history session will be opened.
1204 """
1208 """
1205 # Clear histories
1209 # Clear histories
1206 self.history_manager.reset(new_session)
1210 self.history_manager.reset(new_session)
1207 # Reset counter used to index all histories
1211 # Reset counter used to index all histories
1208 if new_session:
1212 if new_session:
1209 self.execution_count = 1
1213 self.execution_count = 1
1210
1214
1211 # Flush cached output items
1215 # Flush cached output items
1212 if self.displayhook.do_full_cache:
1216 if self.displayhook.do_full_cache:
1213 self.displayhook.flush()
1217 self.displayhook.flush()
1214
1218
1215 # The main execution namespaces must be cleared very carefully,
1219 # The main execution namespaces must be cleared very carefully,
1216 # skipping the deletion of the builtin-related keys, because doing so
1220 # skipping the deletion of the builtin-related keys, because doing so
1217 # would cause errors in many object's __del__ methods.
1221 # would cause errors in many object's __del__ methods.
1218 if self.user_ns is not self.user_global_ns:
1222 if self.user_ns is not self.user_global_ns:
1219 self.user_ns.clear()
1223 self.user_ns.clear()
1220 ns = self.user_global_ns
1224 ns = self.user_global_ns
1221 drop_keys = set(ns.keys())
1225 drop_keys = set(ns.keys())
1222 drop_keys.discard('__builtin__')
1226 drop_keys.discard('__builtin__')
1223 drop_keys.discard('__builtins__')
1227 drop_keys.discard('__builtins__')
1224 drop_keys.discard('__name__')
1228 drop_keys.discard('__name__')
1225 for k in drop_keys:
1229 for k in drop_keys:
1226 del ns[k]
1230 del ns[k]
1227
1231
1228 self.user_ns_hidden.clear()
1232 self.user_ns_hidden.clear()
1229
1233
1230 # Restore the user namespaces to minimal usability
1234 # Restore the user namespaces to minimal usability
1231 self.init_user_ns()
1235 self.init_user_ns()
1232
1236
1233 # Restore the default and user aliases
1237 # Restore the default and user aliases
1234 self.alias_manager.clear_aliases()
1238 self.alias_manager.clear_aliases()
1235 self.alias_manager.init_aliases()
1239 self.alias_manager.init_aliases()
1236
1240
1237 # Flush the private list of module references kept for script
1241 # Flush the private list of module references kept for script
1238 # execution protection
1242 # execution protection
1239 self.clear_main_mod_cache()
1243 self.clear_main_mod_cache()
1240
1244
1241 def del_var(self, varname, by_name=False):
1245 def del_var(self, varname, by_name=False):
1242 """Delete a variable from the various namespaces, so that, as
1246 """Delete a variable from the various namespaces, so that, as
1243 far as possible, we're not keeping any hidden references to it.
1247 far as possible, we're not keeping any hidden references to it.
1244
1248
1245 Parameters
1249 Parameters
1246 ----------
1250 ----------
1247 varname : str
1251 varname : str
1248 The name of the variable to delete.
1252 The name of the variable to delete.
1249 by_name : bool
1253 by_name : bool
1250 If True, delete variables with the given name in each
1254 If True, delete variables with the given name in each
1251 namespace. If False (default), find the variable in the user
1255 namespace. If False (default), find the variable in the user
1252 namespace, and delete references to it.
1256 namespace, and delete references to it.
1253 """
1257 """
1254 if varname in ('__builtin__', '__builtins__'):
1258 if varname in ('__builtin__', '__builtins__'):
1255 raise ValueError("Refusing to delete %s" % varname)
1259 raise ValueError("Refusing to delete %s" % varname)
1256
1260
1257 ns_refs = self.all_ns_refs
1261 ns_refs = self.all_ns_refs
1258
1262
1259 if by_name: # Delete by name
1263 if by_name: # Delete by name
1260 for ns in ns_refs:
1264 for ns in ns_refs:
1261 try:
1265 try:
1262 del ns[varname]
1266 del ns[varname]
1263 except KeyError:
1267 except KeyError:
1264 pass
1268 pass
1265 else: # Delete by object
1269 else: # Delete by object
1266 try:
1270 try:
1267 obj = self.user_ns[varname]
1271 obj = self.user_ns[varname]
1268 except KeyError:
1272 except KeyError:
1269 raise NameError("name '%s' is not defined" % varname)
1273 raise NameError("name '%s' is not defined" % varname)
1270 # Also check in output history
1274 # Also check in output history
1271 ns_refs.append(self.history_manager.output_hist)
1275 ns_refs.append(self.history_manager.output_hist)
1272 for ns in ns_refs:
1276 for ns in ns_refs:
1273 to_delete = [n for n, o in iteritems(ns) if o is obj]
1277 to_delete = [n for n, o in iteritems(ns) if o is obj]
1274 for name in to_delete:
1278 for name in to_delete:
1275 del ns[name]
1279 del ns[name]
1276
1280
1277 # displayhook keeps extra references, but not in a dictionary
1281 # displayhook keeps extra references, but not in a dictionary
1278 for name in ('_', '__', '___'):
1282 for name in ('_', '__', '___'):
1279 if getattr(self.displayhook, name) is obj:
1283 if getattr(self.displayhook, name) is obj:
1280 setattr(self.displayhook, name, None)
1284 setattr(self.displayhook, name, None)
1281
1285
1282 def reset_selective(self, regex=None):
1286 def reset_selective(self, regex=None):
1283 """Clear selective variables from internal namespaces based on a
1287 """Clear selective variables from internal namespaces based on a
1284 specified regular expression.
1288 specified regular expression.
1285
1289
1286 Parameters
1290 Parameters
1287 ----------
1291 ----------
1288 regex : string or compiled pattern, optional
1292 regex : string or compiled pattern, optional
1289 A regular expression pattern that will be used in searching
1293 A regular expression pattern that will be used in searching
1290 variable names in the users namespaces.
1294 variable names in the users namespaces.
1291 """
1295 """
1292 if regex is not None:
1296 if regex is not None:
1293 try:
1297 try:
1294 m = re.compile(regex)
1298 m = re.compile(regex)
1295 except TypeError:
1299 except TypeError:
1296 raise TypeError('regex must be a string or compiled pattern')
1300 raise TypeError('regex must be a string or compiled pattern')
1297 # Search for keys in each namespace that match the given regex
1301 # Search for keys in each namespace that match the given regex
1298 # If a match is found, delete the key/value pair.
1302 # If a match is found, delete the key/value pair.
1299 for ns in self.all_ns_refs:
1303 for ns in self.all_ns_refs:
1300 for var in ns:
1304 for var in ns:
1301 if m.search(var):
1305 if m.search(var):
1302 del ns[var]
1306 del ns[var]
1303
1307
1304 def push(self, variables, interactive=True):
1308 def push(self, variables, interactive=True):
1305 """Inject a group of variables into the IPython user namespace.
1309 """Inject a group of variables into the IPython user namespace.
1306
1310
1307 Parameters
1311 Parameters
1308 ----------
1312 ----------
1309 variables : dict, str or list/tuple of str
1313 variables : dict, str or list/tuple of str
1310 The variables to inject into the user's namespace. If a dict, a
1314 The variables to inject into the user's namespace. If a dict, a
1311 simple update is done. If a str, the string is assumed to have
1315 simple update is done. If a str, the string is assumed to have
1312 variable names separated by spaces. A list/tuple of str can also
1316 variable names separated by spaces. A list/tuple of str can also
1313 be used to give the variable names. If just the variable names are
1317 be used to give the variable names. If just the variable names are
1314 give (list/tuple/str) then the variable values looked up in the
1318 give (list/tuple/str) then the variable values looked up in the
1315 callers frame.
1319 callers frame.
1316 interactive : bool
1320 interactive : bool
1317 If True (default), the variables will be listed with the ``who``
1321 If True (default), the variables will be listed with the ``who``
1318 magic.
1322 magic.
1319 """
1323 """
1320 vdict = None
1324 vdict = None
1321
1325
1322 # We need a dict of name/value pairs to do namespace updates.
1326 # We need a dict of name/value pairs to do namespace updates.
1323 if isinstance(variables, dict):
1327 if isinstance(variables, dict):
1324 vdict = variables
1328 vdict = variables
1325 elif isinstance(variables, string_types+(list, tuple)):
1329 elif isinstance(variables, string_types+(list, tuple)):
1326 if isinstance(variables, string_types):
1330 if isinstance(variables, string_types):
1327 vlist = variables.split()
1331 vlist = variables.split()
1328 else:
1332 else:
1329 vlist = variables
1333 vlist = variables
1330 vdict = {}
1334 vdict = {}
1331 cf = sys._getframe(1)
1335 cf = sys._getframe(1)
1332 for name in vlist:
1336 for name in vlist:
1333 try:
1337 try:
1334 vdict[name] = eval(name, cf.f_globals, cf.f_locals)
1338 vdict[name] = eval(name, cf.f_globals, cf.f_locals)
1335 except:
1339 except:
1336 print('Could not get variable %s from %s' %
1340 print('Could not get variable %s from %s' %
1337 (name,cf.f_code.co_name))
1341 (name,cf.f_code.co_name))
1338 else:
1342 else:
1339 raise ValueError('variables must be a dict/str/list/tuple')
1343 raise ValueError('variables must be a dict/str/list/tuple')
1340
1344
1341 # Propagate variables to user namespace
1345 # Propagate variables to user namespace
1342 self.user_ns.update(vdict)
1346 self.user_ns.update(vdict)
1343
1347
1344 # And configure interactive visibility
1348 # And configure interactive visibility
1345 user_ns_hidden = self.user_ns_hidden
1349 user_ns_hidden = self.user_ns_hidden
1346 if interactive:
1350 if interactive:
1347 for name in vdict:
1351 for name in vdict:
1348 user_ns_hidden.pop(name, None)
1352 user_ns_hidden.pop(name, None)
1349 else:
1353 else:
1350 user_ns_hidden.update(vdict)
1354 user_ns_hidden.update(vdict)
1351
1355
1352 def drop_by_id(self, variables):
1356 def drop_by_id(self, variables):
1353 """Remove a dict of variables from the user namespace, if they are the
1357 """Remove a dict of variables from the user namespace, if they are the
1354 same as the values in the dictionary.
1358 same as the values in the dictionary.
1355
1359
1356 This is intended for use by extensions: variables that they've added can
1360 This is intended for use by extensions: variables that they've added can
1357 be taken back out if they are unloaded, without removing any that the
1361 be taken back out if they are unloaded, without removing any that the
1358 user has overwritten.
1362 user has overwritten.
1359
1363
1360 Parameters
1364 Parameters
1361 ----------
1365 ----------
1362 variables : dict
1366 variables : dict
1363 A dictionary mapping object names (as strings) to the objects.
1367 A dictionary mapping object names (as strings) to the objects.
1364 """
1368 """
1365 for name, obj in iteritems(variables):
1369 for name, obj in iteritems(variables):
1366 if name in self.user_ns and self.user_ns[name] is obj:
1370 if name in self.user_ns and self.user_ns[name] is obj:
1367 del self.user_ns[name]
1371 del self.user_ns[name]
1368 self.user_ns_hidden.pop(name, None)
1372 self.user_ns_hidden.pop(name, None)
1369
1373
1370 #-------------------------------------------------------------------------
1374 #-------------------------------------------------------------------------
1371 # Things related to object introspection
1375 # Things related to object introspection
1372 #-------------------------------------------------------------------------
1376 #-------------------------------------------------------------------------
1373
1377
1374 def _ofind(self, oname, namespaces=None):
1378 def _ofind(self, oname, namespaces=None):
1375 """Find an object in the available namespaces.
1379 """Find an object in the available namespaces.
1376
1380
1377 self._ofind(oname) -> dict with keys: found,obj,ospace,ismagic
1381 self._ofind(oname) -> dict with keys: found,obj,ospace,ismagic
1378
1382
1379 Has special code to detect magic functions.
1383 Has special code to detect magic functions.
1380 """
1384 """
1381 oname = oname.strip()
1385 oname = oname.strip()
1382 #print '1- oname: <%r>' % oname # dbg
1386 #print '1- oname: <%r>' % oname # dbg
1383 if not oname.startswith(ESC_MAGIC) and \
1387 if not oname.startswith(ESC_MAGIC) and \
1384 not oname.startswith(ESC_MAGIC2) and \
1388 not oname.startswith(ESC_MAGIC2) and \
1385 not py3compat.isidentifier(oname, dotted=True):
1389 not py3compat.isidentifier(oname, dotted=True):
1386 return dict(found=False)
1390 return dict(found=False)
1387
1391
1388 if namespaces is None:
1392 if namespaces is None:
1389 # Namespaces to search in:
1393 # Namespaces to search in:
1390 # Put them in a list. The order is important so that we
1394 # Put them in a list. The order is important so that we
1391 # find things in the same order that Python finds them.
1395 # find things in the same order that Python finds them.
1392 namespaces = [ ('Interactive', self.user_ns),
1396 namespaces = [ ('Interactive', self.user_ns),
1393 ('Interactive (global)', self.user_global_ns),
1397 ('Interactive (global)', self.user_global_ns),
1394 ('Python builtin', builtin_mod.__dict__),
1398 ('Python builtin', builtin_mod.__dict__),
1395 ]
1399 ]
1396
1400
1397 # initialize results to 'null'
1401 # initialize results to 'null'
1398 found = False; obj = None; ospace = None;
1402 found = False; obj = None; ospace = None;
1399 ismagic = False; isalias = False; parent = None
1403 ismagic = False; isalias = False; parent = None
1400
1404
1401 # We need to special-case 'print', which as of python2.6 registers as a
1405 # We need to special-case 'print', which as of python2.6 registers as a
1402 # function but should only be treated as one if print_function was
1406 # function but should only be treated as one if print_function was
1403 # loaded with a future import. In this case, just bail.
1407 # loaded with a future import. In this case, just bail.
1404 if (oname == 'print' and not py3compat.PY3 and not \
1408 if (oname == 'print' and not py3compat.PY3 and not \
1405 (self.compile.compiler_flags & __future__.CO_FUTURE_PRINT_FUNCTION)):
1409 (self.compile.compiler_flags & __future__.CO_FUTURE_PRINT_FUNCTION)):
1406 return {'found':found, 'obj':obj, 'namespace':ospace,
1410 return {'found':found, 'obj':obj, 'namespace':ospace,
1407 'ismagic':ismagic, 'isalias':isalias, 'parent':parent}
1411 'ismagic':ismagic, 'isalias':isalias, 'parent':parent}
1408
1412
1409 # Look for the given name by splitting it in parts. If the head is
1413 # Look for the given name by splitting it in parts. If the head is
1410 # found, then we look for all the remaining parts as members, and only
1414 # found, then we look for all the remaining parts as members, and only
1411 # declare success if we can find them all.
1415 # declare success if we can find them all.
1412 oname_parts = oname.split('.')
1416 oname_parts = oname.split('.')
1413 oname_head, oname_rest = oname_parts[0],oname_parts[1:]
1417 oname_head, oname_rest = oname_parts[0],oname_parts[1:]
1414 for nsname,ns in namespaces:
1418 for nsname,ns in namespaces:
1415 try:
1419 try:
1416 obj = ns[oname_head]
1420 obj = ns[oname_head]
1417 except KeyError:
1421 except KeyError:
1418 continue
1422 continue
1419 else:
1423 else:
1420 #print 'oname_rest:', oname_rest # dbg
1424 #print 'oname_rest:', oname_rest # dbg
1421 for idx, part in enumerate(oname_rest):
1425 for idx, part in enumerate(oname_rest):
1422 try:
1426 try:
1423 parent = obj
1427 parent = obj
1424 # The last part is looked up in a special way to avoid
1428 # The last part is looked up in a special way to avoid
1425 # descriptor invocation as it may raise or have side
1429 # descriptor invocation as it may raise or have side
1426 # effects.
1430 # effects.
1427 if idx == len(oname_rest) - 1:
1431 if idx == len(oname_rest) - 1:
1428 obj = self._getattr_property(obj, part)
1432 obj = self._getattr_property(obj, part)
1429 else:
1433 else:
1430 obj = getattr(obj, part)
1434 obj = getattr(obj, part)
1431 except:
1435 except:
1432 # Blanket except b/c some badly implemented objects
1436 # Blanket except b/c some badly implemented objects
1433 # allow __getattr__ to raise exceptions other than
1437 # allow __getattr__ to raise exceptions other than
1434 # AttributeError, which then crashes IPython.
1438 # AttributeError, which then crashes IPython.
1435 break
1439 break
1436 else:
1440 else:
1437 # If we finish the for loop (no break), we got all members
1441 # If we finish the for loop (no break), we got all members
1438 found = True
1442 found = True
1439 ospace = nsname
1443 ospace = nsname
1440 break # namespace loop
1444 break # namespace loop
1441
1445
1442 # Try to see if it's magic
1446 # Try to see if it's magic
1443 if not found:
1447 if not found:
1444 obj = None
1448 obj = None
1445 if oname.startswith(ESC_MAGIC2):
1449 if oname.startswith(ESC_MAGIC2):
1446 oname = oname.lstrip(ESC_MAGIC2)
1450 oname = oname.lstrip(ESC_MAGIC2)
1447 obj = self.find_cell_magic(oname)
1451 obj = self.find_cell_magic(oname)
1448 elif oname.startswith(ESC_MAGIC):
1452 elif oname.startswith(ESC_MAGIC):
1449 oname = oname.lstrip(ESC_MAGIC)
1453 oname = oname.lstrip(ESC_MAGIC)
1450 obj = self.find_line_magic(oname)
1454 obj = self.find_line_magic(oname)
1451 else:
1455 else:
1452 # search without prefix, so run? will find %run?
1456 # search without prefix, so run? will find %run?
1453 obj = self.find_line_magic(oname)
1457 obj = self.find_line_magic(oname)
1454 if obj is None:
1458 if obj is None:
1455 obj = self.find_cell_magic(oname)
1459 obj = self.find_cell_magic(oname)
1456 if obj is not None:
1460 if obj is not None:
1457 found = True
1461 found = True
1458 ospace = 'IPython internal'
1462 ospace = 'IPython internal'
1459 ismagic = True
1463 ismagic = True
1460 isalias = isinstance(obj, Alias)
1464 isalias = isinstance(obj, Alias)
1461
1465
1462 # Last try: special-case some literals like '', [], {}, etc:
1466 # Last try: special-case some literals like '', [], {}, etc:
1463 if not found and oname_head in ["''",'""','[]','{}','()']:
1467 if not found and oname_head in ["''",'""','[]','{}','()']:
1464 obj = eval(oname_head)
1468 obj = eval(oname_head)
1465 found = True
1469 found = True
1466 ospace = 'Interactive'
1470 ospace = 'Interactive'
1467
1471
1468 return {'found':found, 'obj':obj, 'namespace':ospace,
1472 return {'found':found, 'obj':obj, 'namespace':ospace,
1469 'ismagic':ismagic, 'isalias':isalias, 'parent':parent}
1473 'ismagic':ismagic, 'isalias':isalias, 'parent':parent}
1470
1474
1471 @staticmethod
1475 @staticmethod
1472 def _getattr_property(obj, attrname):
1476 def _getattr_property(obj, attrname):
1473 """Property-aware getattr to use in object finding.
1477 """Property-aware getattr to use in object finding.
1474
1478
1475 If attrname represents a property, return it unevaluated (in case it has
1479 If attrname represents a property, return it unevaluated (in case it has
1476 side effects or raises an error.
1480 side effects or raises an error.
1477
1481
1478 """
1482 """
1479 if not isinstance(obj, type):
1483 if not isinstance(obj, type):
1480 try:
1484 try:
1481 # `getattr(type(obj), attrname)` is not guaranteed to return
1485 # `getattr(type(obj), attrname)` is not guaranteed to return
1482 # `obj`, but does so for property:
1486 # `obj`, but does so for property:
1483 #
1487 #
1484 # property.__get__(self, None, cls) -> self
1488 # property.__get__(self, None, cls) -> self
1485 #
1489 #
1486 # The universal alternative is to traverse the mro manually
1490 # The universal alternative is to traverse the mro manually
1487 # searching for attrname in class dicts.
1491 # searching for attrname in class dicts.
1488 attr = getattr(type(obj), attrname)
1492 attr = getattr(type(obj), attrname)
1489 except AttributeError:
1493 except AttributeError:
1490 pass
1494 pass
1491 else:
1495 else:
1492 # This relies on the fact that data descriptors (with both
1496 # This relies on the fact that data descriptors (with both
1493 # __get__ & __set__ magic methods) take precedence over
1497 # __get__ & __set__ magic methods) take precedence over
1494 # instance-level attributes:
1498 # instance-level attributes:
1495 #
1499 #
1496 # class A(object):
1500 # class A(object):
1497 # @property
1501 # @property
1498 # def foobar(self): return 123
1502 # def foobar(self): return 123
1499 # a = A()
1503 # a = A()
1500 # a.__dict__['foobar'] = 345
1504 # a.__dict__['foobar'] = 345
1501 # a.foobar # == 123
1505 # a.foobar # == 123
1502 #
1506 #
1503 # So, a property may be returned right away.
1507 # So, a property may be returned right away.
1504 if isinstance(attr, property):
1508 if isinstance(attr, property):
1505 return attr
1509 return attr
1506
1510
1507 # Nothing helped, fall back.
1511 # Nothing helped, fall back.
1508 return getattr(obj, attrname)
1512 return getattr(obj, attrname)
1509
1513
1510 def _object_find(self, oname, namespaces=None):
1514 def _object_find(self, oname, namespaces=None):
1511 """Find an object and return a struct with info about it."""
1515 """Find an object and return a struct with info about it."""
1512 return Struct(self._ofind(oname, namespaces))
1516 return Struct(self._ofind(oname, namespaces))
1513
1517
1514 def _inspect(self, meth, oname, namespaces=None, **kw):
1518 def _inspect(self, meth, oname, namespaces=None, **kw):
1515 """Generic interface to the inspector system.
1519 """Generic interface to the inspector system.
1516
1520
1517 This function is meant to be called by pdef, pdoc & friends.
1521 This function is meant to be called by pdef, pdoc & friends.
1518 """
1522 """
1519 info = self._object_find(oname, namespaces)
1523 info = self._object_find(oname, namespaces)
1520 docformat = sphinxify if self.sphinxify_docstring else None
1524 docformat = sphinxify if self.sphinxify_docstring else None
1521 if info.found:
1525 if info.found:
1522 pmethod = getattr(self.inspector, meth)
1526 pmethod = getattr(self.inspector, meth)
1523 # TODO: only apply format_screen to the plain/text repr of the mime
1527 # TODO: only apply format_screen to the plain/text repr of the mime
1524 # bundle.
1528 # bundle.
1525 formatter = format_screen if info.ismagic else docformat
1529 formatter = format_screen if info.ismagic else docformat
1526 if meth == 'pdoc':
1530 if meth == 'pdoc':
1527 pmethod(info.obj, oname, formatter)
1531 pmethod(info.obj, oname, formatter)
1528 elif meth == 'pinfo':
1532 elif meth == 'pinfo':
1529 pmethod(info.obj, oname, formatter, info,
1533 pmethod(info.obj, oname, formatter, info,
1530 enable_html_pager=self.enable_html_pager, **kw)
1534 enable_html_pager=self.enable_html_pager, **kw)
1531 else:
1535 else:
1532 pmethod(info.obj, oname)
1536 pmethod(info.obj, oname)
1533 else:
1537 else:
1534 print('Object `%s` not found.' % oname)
1538 print('Object `%s` not found.' % oname)
1535 return 'not found' # so callers can take other action
1539 return 'not found' # so callers can take other action
1536
1540
1537 def object_inspect(self, oname, detail_level=0):
1541 def object_inspect(self, oname, detail_level=0):
1538 """Get object info about oname"""
1542 """Get object info about oname"""
1539 with self.builtin_trap:
1543 with self.builtin_trap:
1540 info = self._object_find(oname)
1544 info = self._object_find(oname)
1541 if info.found:
1545 if info.found:
1542 return self.inspector.info(info.obj, oname, info=info,
1546 return self.inspector.info(info.obj, oname, info=info,
1543 detail_level=detail_level
1547 detail_level=detail_level
1544 )
1548 )
1545 else:
1549 else:
1546 return oinspect.object_info(name=oname, found=False)
1550 return oinspect.object_info(name=oname, found=False)
1547
1551
1548 def object_inspect_text(self, oname, detail_level=0):
1552 def object_inspect_text(self, oname, detail_level=0):
1549 """Get object info as formatted text"""
1553 """Get object info as formatted text"""
1550 return self.object_inspect_mime(oname, detail_level)['text/plain']
1554 return self.object_inspect_mime(oname, detail_level)['text/plain']
1551
1555
1552 def object_inspect_mime(self, oname, detail_level=0):
1556 def object_inspect_mime(self, oname, detail_level=0):
1553 """Get object info as a mimebundle of formatted representations.
1557 """Get object info as a mimebundle of formatted representations.
1554
1558
1555 A mimebundle is a dictionary, keyed by mime-type.
1559 A mimebundle is a dictionary, keyed by mime-type.
1556 It must always have the key `'text/plain'`.
1560 It must always have the key `'text/plain'`.
1557 """
1561 """
1558 with self.builtin_trap:
1562 with self.builtin_trap:
1559 info = self._object_find(oname)
1563 info = self._object_find(oname)
1560 if info.found:
1564 if info.found:
1561 return self.inspector._get_info(info.obj, oname, info=info,
1565 return self.inspector._get_info(info.obj, oname, info=info,
1562 detail_level=detail_level
1566 detail_level=detail_level
1563 )
1567 )
1564 else:
1568 else:
1565 raise KeyError(oname)
1569 raise KeyError(oname)
1566
1570
1567 #-------------------------------------------------------------------------
1571 #-------------------------------------------------------------------------
1568 # Things related to history management
1572 # Things related to history management
1569 #-------------------------------------------------------------------------
1573 #-------------------------------------------------------------------------
1570
1574
1571 def init_history(self):
1575 def init_history(self):
1572 """Sets up the command history, and starts regular autosaves."""
1576 """Sets up the command history, and starts regular autosaves."""
1573 self.history_manager = HistoryManager(shell=self, parent=self)
1577 self.history_manager = HistoryManager(shell=self, parent=self)
1574 self.configurables.append(self.history_manager)
1578 self.configurables.append(self.history_manager)
1575
1579
1576 #-------------------------------------------------------------------------
1580 #-------------------------------------------------------------------------
1577 # Things related to exception handling and tracebacks (not debugging)
1581 # Things related to exception handling and tracebacks (not debugging)
1578 #-------------------------------------------------------------------------
1582 #-------------------------------------------------------------------------
1579
1583
1580 debugger_cls = Pdb
1584 debugger_cls = Pdb
1581
1585
1582 def init_traceback_handlers(self, custom_exceptions):
1586 def init_traceback_handlers(self, custom_exceptions):
1583 # Syntax error handler.
1587 # Syntax error handler.
1584 self.SyntaxTB = ultratb.SyntaxTB(color_scheme='NoColor')
1588 self.SyntaxTB = ultratb.SyntaxTB(color_scheme='NoColor')
1585
1589
1586 # The interactive one is initialized with an offset, meaning we always
1590 # The interactive one is initialized with an offset, meaning we always
1587 # want to remove the topmost item in the traceback, which is our own
1591 # want to remove the topmost item in the traceback, which is our own
1588 # internal code. Valid modes: ['Plain','Context','Verbose']
1592 # internal code. Valid modes: ['Plain','Context','Verbose']
1589 self.InteractiveTB = ultratb.AutoFormattedTB(mode = 'Plain',
1593 self.InteractiveTB = ultratb.AutoFormattedTB(mode = 'Plain',
1590 color_scheme='NoColor',
1594 color_scheme='NoColor',
1591 tb_offset = 1,
1595 tb_offset = 1,
1592 check_cache=check_linecache_ipython,
1596 check_cache=check_linecache_ipython,
1593 debugger_cls=self.debugger_cls)
1597 debugger_cls=self.debugger_cls)
1594
1598
1595 # The instance will store a pointer to the system-wide exception hook,
1599 # The instance will store a pointer to the system-wide exception hook,
1596 # so that runtime code (such as magics) can access it. This is because
1600 # so that runtime code (such as magics) can access it. This is because
1597 # during the read-eval loop, it may get temporarily overwritten.
1601 # during the read-eval loop, it may get temporarily overwritten.
1598 self.sys_excepthook = sys.excepthook
1602 self.sys_excepthook = sys.excepthook
1599
1603
1600 # and add any custom exception handlers the user may have specified
1604 # and add any custom exception handlers the user may have specified
1601 self.set_custom_exc(*custom_exceptions)
1605 self.set_custom_exc(*custom_exceptions)
1602
1606
1603 # Set the exception mode
1607 # Set the exception mode
1604 self.InteractiveTB.set_mode(mode=self.xmode)
1608 self.InteractiveTB.set_mode(mode=self.xmode)
1605
1609
1606 def set_custom_exc(self, exc_tuple, handler):
1610 def set_custom_exc(self, exc_tuple, handler):
1607 """set_custom_exc(exc_tuple, handler)
1611 """set_custom_exc(exc_tuple, handler)
1608
1612
1609 Set a custom exception handler, which will be called if any of the
1613 Set a custom exception handler, which will be called if any of the
1610 exceptions in exc_tuple occur in the mainloop (specifically, in the
1614 exceptions in exc_tuple occur in the mainloop (specifically, in the
1611 run_code() method).
1615 run_code() method).
1612
1616
1613 Parameters
1617 Parameters
1614 ----------
1618 ----------
1615
1619
1616 exc_tuple : tuple of exception classes
1620 exc_tuple : tuple of exception classes
1617 A *tuple* of exception classes, for which to call the defined
1621 A *tuple* of exception classes, for which to call the defined
1618 handler. It is very important that you use a tuple, and NOT A
1622 handler. It is very important that you use a tuple, and NOT A
1619 LIST here, because of the way Python's except statement works. If
1623 LIST here, because of the way Python's except statement works. If
1620 you only want to trap a single exception, use a singleton tuple::
1624 you only want to trap a single exception, use a singleton tuple::
1621
1625
1622 exc_tuple == (MyCustomException,)
1626 exc_tuple == (MyCustomException,)
1623
1627
1624 handler : callable
1628 handler : callable
1625 handler must have the following signature::
1629 handler must have the following signature::
1626
1630
1627 def my_handler(self, etype, value, tb, tb_offset=None):
1631 def my_handler(self, etype, value, tb, tb_offset=None):
1628 ...
1632 ...
1629 return structured_traceback
1633 return structured_traceback
1630
1634
1631 Your handler must return a structured traceback (a list of strings),
1635 Your handler must return a structured traceback (a list of strings),
1632 or None.
1636 or None.
1633
1637
1634 This will be made into an instance method (via types.MethodType)
1638 This will be made into an instance method (via types.MethodType)
1635 of IPython itself, and it will be called if any of the exceptions
1639 of IPython itself, and it will be called if any of the exceptions
1636 listed in the exc_tuple are caught. If the handler is None, an
1640 listed in the exc_tuple are caught. If the handler is None, an
1637 internal basic one is used, which just prints basic info.
1641 internal basic one is used, which just prints basic info.
1638
1642
1639 To protect IPython from crashes, if your handler ever raises an
1643 To protect IPython from crashes, if your handler ever raises an
1640 exception or returns an invalid result, it will be immediately
1644 exception or returns an invalid result, it will be immediately
1641 disabled.
1645 disabled.
1642
1646
1643 WARNING: by putting in your own exception handler into IPython's main
1647 WARNING: by putting in your own exception handler into IPython's main
1644 execution loop, you run a very good chance of nasty crashes. This
1648 execution loop, you run a very good chance of nasty crashes. This
1645 facility should only be used if you really know what you are doing."""
1649 facility should only be used if you really know what you are doing."""
1646
1650
1647 assert type(exc_tuple)==type(()) , \
1651 assert type(exc_tuple)==type(()) , \
1648 "The custom exceptions must be given AS A TUPLE."
1652 "The custom exceptions must be given AS A TUPLE."
1649
1653
1650 def dummy_handler(self, etype, value, tb, tb_offset=None):
1654 def dummy_handler(self, etype, value, tb, tb_offset=None):
1651 print('*** Simple custom exception handler ***')
1655 print('*** Simple custom exception handler ***')
1652 print('Exception type :',etype)
1656 print('Exception type :',etype)
1653 print('Exception value:',value)
1657 print('Exception value:',value)
1654 print('Traceback :',tb)
1658 print('Traceback :',tb)
1655 #print 'Source code :','\n'.join(self.buffer)
1659 #print 'Source code :','\n'.join(self.buffer)
1656
1660
1657 def validate_stb(stb):
1661 def validate_stb(stb):
1658 """validate structured traceback return type
1662 """validate structured traceback return type
1659
1663
1660 return type of CustomTB *should* be a list of strings, but allow
1664 return type of CustomTB *should* be a list of strings, but allow
1661 single strings or None, which are harmless.
1665 single strings or None, which are harmless.
1662
1666
1663 This function will *always* return a list of strings,
1667 This function will *always* return a list of strings,
1664 and will raise a TypeError if stb is inappropriate.
1668 and will raise a TypeError if stb is inappropriate.
1665 """
1669 """
1666 msg = "CustomTB must return list of strings, not %r" % stb
1670 msg = "CustomTB must return list of strings, not %r" % stb
1667 if stb is None:
1671 if stb is None:
1668 return []
1672 return []
1669 elif isinstance(stb, string_types):
1673 elif isinstance(stb, string_types):
1670 return [stb]
1674 return [stb]
1671 elif not isinstance(stb, list):
1675 elif not isinstance(stb, list):
1672 raise TypeError(msg)
1676 raise TypeError(msg)
1673 # it's a list
1677 # it's a list
1674 for line in stb:
1678 for line in stb:
1675 # check every element
1679 # check every element
1676 if not isinstance(line, string_types):
1680 if not isinstance(line, string_types):
1677 raise TypeError(msg)
1681 raise TypeError(msg)
1678 return stb
1682 return stb
1679
1683
1680 if handler is None:
1684 if handler is None:
1681 wrapped = dummy_handler
1685 wrapped = dummy_handler
1682 else:
1686 else:
1683 def wrapped(self,etype,value,tb,tb_offset=None):
1687 def wrapped(self,etype,value,tb,tb_offset=None):
1684 """wrap CustomTB handler, to protect IPython from user code
1688 """wrap CustomTB handler, to protect IPython from user code
1685
1689
1686 This makes it harder (but not impossible) for custom exception
1690 This makes it harder (but not impossible) for custom exception
1687 handlers to crash IPython.
1691 handlers to crash IPython.
1688 """
1692 """
1689 try:
1693 try:
1690 stb = handler(self,etype,value,tb,tb_offset=tb_offset)
1694 stb = handler(self,etype,value,tb,tb_offset=tb_offset)
1691 return validate_stb(stb)
1695 return validate_stb(stb)
1692 except:
1696 except:
1693 # clear custom handler immediately
1697 # clear custom handler immediately
1694 self.set_custom_exc((), None)
1698 self.set_custom_exc((), None)
1695 print("Custom TB Handler failed, unregistering", file=sys.stderr)
1699 print("Custom TB Handler failed, unregistering", file=sys.stderr)
1696 # show the exception in handler first
1700 # show the exception in handler first
1697 stb = self.InteractiveTB.structured_traceback(*sys.exc_info())
1701 stb = self.InteractiveTB.structured_traceback(*sys.exc_info())
1698 print(self.InteractiveTB.stb2text(stb))
1702 print(self.InteractiveTB.stb2text(stb))
1699 print("The original exception:")
1703 print("The original exception:")
1700 stb = self.InteractiveTB.structured_traceback(
1704 stb = self.InteractiveTB.structured_traceback(
1701 (etype,value,tb), tb_offset=tb_offset
1705 (etype,value,tb), tb_offset=tb_offset
1702 )
1706 )
1703 return stb
1707 return stb
1704
1708
1705 self.CustomTB = types.MethodType(wrapped,self)
1709 self.CustomTB = types.MethodType(wrapped,self)
1706 self.custom_exceptions = exc_tuple
1710 self.custom_exceptions = exc_tuple
1707
1711
1708 def excepthook(self, etype, value, tb):
1712 def excepthook(self, etype, value, tb):
1709 """One more defense for GUI apps that call sys.excepthook.
1713 """One more defense for GUI apps that call sys.excepthook.
1710
1714
1711 GUI frameworks like wxPython trap exceptions and call
1715 GUI frameworks like wxPython trap exceptions and call
1712 sys.excepthook themselves. I guess this is a feature that
1716 sys.excepthook themselves. I guess this is a feature that
1713 enables them to keep running after exceptions that would
1717 enables them to keep running after exceptions that would
1714 otherwise kill their mainloop. This is a bother for IPython
1718 otherwise kill their mainloop. This is a bother for IPython
1715 which excepts to catch all of the program exceptions with a try:
1719 which excepts to catch all of the program exceptions with a try:
1716 except: statement.
1720 except: statement.
1717
1721
1718 Normally, IPython sets sys.excepthook to a CrashHandler instance, so if
1722 Normally, IPython sets sys.excepthook to a CrashHandler instance, so if
1719 any app directly invokes sys.excepthook, it will look to the user like
1723 any app directly invokes sys.excepthook, it will look to the user like
1720 IPython crashed. In order to work around this, we can disable the
1724 IPython crashed. In order to work around this, we can disable the
1721 CrashHandler and replace it with this excepthook instead, which prints a
1725 CrashHandler and replace it with this excepthook instead, which prints a
1722 regular traceback using our InteractiveTB. In this fashion, apps which
1726 regular traceback using our InteractiveTB. In this fashion, apps which
1723 call sys.excepthook will generate a regular-looking exception from
1727 call sys.excepthook will generate a regular-looking exception from
1724 IPython, and the CrashHandler will only be triggered by real IPython
1728 IPython, and the CrashHandler will only be triggered by real IPython
1725 crashes.
1729 crashes.
1726
1730
1727 This hook should be used sparingly, only in places which are not likely
1731 This hook should be used sparingly, only in places which are not likely
1728 to be true IPython errors.
1732 to be true IPython errors.
1729 """
1733 """
1730 self.showtraceback((etype, value, tb), tb_offset=0)
1734 self.showtraceback((etype, value, tb), tb_offset=0)
1731
1735
1732 def _get_exc_info(self, exc_tuple=None):
1736 def _get_exc_info(self, exc_tuple=None):
1733 """get exc_info from a given tuple, sys.exc_info() or sys.last_type etc.
1737 """get exc_info from a given tuple, sys.exc_info() or sys.last_type etc.
1734
1738
1735 Ensures sys.last_type,value,traceback hold the exc_info we found,
1739 Ensures sys.last_type,value,traceback hold the exc_info we found,
1736 from whichever source.
1740 from whichever source.
1737
1741
1738 raises ValueError if none of these contain any information
1742 raises ValueError if none of these contain any information
1739 """
1743 """
1740 if exc_tuple is None:
1744 if exc_tuple is None:
1741 etype, value, tb = sys.exc_info()
1745 etype, value, tb = sys.exc_info()
1742 else:
1746 else:
1743 etype, value, tb = exc_tuple
1747 etype, value, tb = exc_tuple
1744
1748
1745 if etype is None:
1749 if etype is None:
1746 if hasattr(sys, 'last_type'):
1750 if hasattr(sys, 'last_type'):
1747 etype, value, tb = sys.last_type, sys.last_value, \
1751 etype, value, tb = sys.last_type, sys.last_value, \
1748 sys.last_traceback
1752 sys.last_traceback
1749
1753
1750 if etype is None:
1754 if etype is None:
1751 raise ValueError("No exception to find")
1755 raise ValueError("No exception to find")
1752
1756
1753 # Now store the exception info in sys.last_type etc.
1757 # Now store the exception info in sys.last_type etc.
1754 # WARNING: these variables are somewhat deprecated and not
1758 # WARNING: these variables are somewhat deprecated and not
1755 # necessarily safe to use in a threaded environment, but tools
1759 # necessarily safe to use in a threaded environment, but tools
1756 # like pdb depend on their existence, so let's set them. If we
1760 # like pdb depend on their existence, so let's set them. If we
1757 # find problems in the field, we'll need to revisit their use.
1761 # find problems in the field, we'll need to revisit their use.
1758 sys.last_type = etype
1762 sys.last_type = etype
1759 sys.last_value = value
1763 sys.last_value = value
1760 sys.last_traceback = tb
1764 sys.last_traceback = tb
1761
1765
1762 return etype, value, tb
1766 return etype, value, tb
1763
1767
1764 def show_usage_error(self, exc):
1768 def show_usage_error(self, exc):
1765 """Show a short message for UsageErrors
1769 """Show a short message for UsageErrors
1766
1770
1767 These are special exceptions that shouldn't show a traceback.
1771 These are special exceptions that shouldn't show a traceback.
1768 """
1772 """
1769 print("UsageError: %s" % exc, file=sys.stderr)
1773 print("UsageError: %s" % exc, file=sys.stderr)
1770
1774
1771 def get_exception_only(self, exc_tuple=None):
1775 def get_exception_only(self, exc_tuple=None):
1772 """
1776 """
1773 Return as a string (ending with a newline) the exception that
1777 Return as a string (ending with a newline) the exception that
1774 just occurred, without any traceback.
1778 just occurred, without any traceback.
1775 """
1779 """
1776 etype, value, tb = self._get_exc_info(exc_tuple)
1780 etype, value, tb = self._get_exc_info(exc_tuple)
1777 msg = traceback.format_exception_only(etype, value)
1781 msg = traceback.format_exception_only(etype, value)
1778 return ''.join(msg)
1782 return ''.join(msg)
1779
1783
1780 def showtraceback(self, exc_tuple=None, filename=None, tb_offset=None,
1784 def showtraceback(self, exc_tuple=None, filename=None, tb_offset=None,
1781 exception_only=False):
1785 exception_only=False):
1782 """Display the exception that just occurred.
1786 """Display the exception that just occurred.
1783
1787
1784 If nothing is known about the exception, this is the method which
1788 If nothing is known about the exception, this is the method which
1785 should be used throughout the code for presenting user tracebacks,
1789 should be used throughout the code for presenting user tracebacks,
1786 rather than directly invoking the InteractiveTB object.
1790 rather than directly invoking the InteractiveTB object.
1787
1791
1788 A specific showsyntaxerror() also exists, but this method can take
1792 A specific showsyntaxerror() also exists, but this method can take
1789 care of calling it if needed, so unless you are explicitly catching a
1793 care of calling it if needed, so unless you are explicitly catching a
1790 SyntaxError exception, don't try to analyze the stack manually and
1794 SyntaxError exception, don't try to analyze the stack manually and
1791 simply call this method."""
1795 simply call this method."""
1792
1796
1793 try:
1797 try:
1794 try:
1798 try:
1795 etype, value, tb = self._get_exc_info(exc_tuple)
1799 etype, value, tb = self._get_exc_info(exc_tuple)
1796 except ValueError:
1800 except ValueError:
1797 print('No traceback available to show.', file=sys.stderr)
1801 print('No traceback available to show.', file=sys.stderr)
1798 return
1802 return
1799
1803
1800 if issubclass(etype, SyntaxError):
1804 if issubclass(etype, SyntaxError):
1801 # Though this won't be called by syntax errors in the input
1805 # Though this won't be called by syntax errors in the input
1802 # line, there may be SyntaxError cases with imported code.
1806 # line, there may be SyntaxError cases with imported code.
1803 self.showsyntaxerror(filename)
1807 self.showsyntaxerror(filename)
1804 elif etype is UsageError:
1808 elif etype is UsageError:
1805 self.show_usage_error(value)
1809 self.show_usage_error(value)
1806 else:
1810 else:
1807 if exception_only:
1811 if exception_only:
1808 stb = ['An exception has occurred, use %tb to see '
1812 stb = ['An exception has occurred, use %tb to see '
1809 'the full traceback.\n']
1813 'the full traceback.\n']
1810 stb.extend(self.InteractiveTB.get_exception_only(etype,
1814 stb.extend(self.InteractiveTB.get_exception_only(etype,
1811 value))
1815 value))
1812 else:
1816 else:
1813 try:
1817 try:
1814 # Exception classes can customise their traceback - we
1818 # Exception classes can customise their traceback - we
1815 # use this in IPython.parallel for exceptions occurring
1819 # use this in IPython.parallel for exceptions occurring
1816 # in the engines. This should return a list of strings.
1820 # in the engines. This should return a list of strings.
1817 stb = value._render_traceback_()
1821 stb = value._render_traceback_()
1818 except Exception:
1822 except Exception:
1819 stb = self.InteractiveTB.structured_traceback(etype,
1823 stb = self.InteractiveTB.structured_traceback(etype,
1820 value, tb, tb_offset=tb_offset)
1824 value, tb, tb_offset=tb_offset)
1821
1825
1822 self._showtraceback(etype, value, stb)
1826 self._showtraceback(etype, value, stb)
1823 if self.call_pdb:
1827 if self.call_pdb:
1824 # drop into debugger
1828 # drop into debugger
1825 self.debugger(force=True)
1829 self.debugger(force=True)
1826 return
1830 return
1827
1831
1828 # Actually show the traceback
1832 # Actually show the traceback
1829 self._showtraceback(etype, value, stb)
1833 self._showtraceback(etype, value, stb)
1830
1834
1831 except KeyboardInterrupt:
1835 except KeyboardInterrupt:
1832 print('\n' + self.get_exception_only(), file=sys.stderr)
1836 print('\n' + self.get_exception_only(), file=sys.stderr)
1833
1837
1834 def _showtraceback(self, etype, evalue, stb):
1838 def _showtraceback(self, etype, evalue, stb):
1835 """Actually show a traceback.
1839 """Actually show a traceback.
1836
1840
1837 Subclasses may override this method to put the traceback on a different
1841 Subclasses may override this method to put the traceback on a different
1838 place, like a side channel.
1842 place, like a side channel.
1839 """
1843 """
1840 print(self.InteractiveTB.stb2text(stb))
1844 print(self.InteractiveTB.stb2text(stb))
1841
1845
1842 def showsyntaxerror(self, filename=None):
1846 def showsyntaxerror(self, filename=None):
1843 """Display the syntax error that just occurred.
1847 """Display the syntax error that just occurred.
1844
1848
1845 This doesn't display a stack trace because there isn't one.
1849 This doesn't display a stack trace because there isn't one.
1846
1850
1847 If a filename is given, it is stuffed in the exception instead
1851 If a filename is given, it is stuffed in the exception instead
1848 of what was there before (because Python's parser always uses
1852 of what was there before (because Python's parser always uses
1849 "<string>" when reading from a string).
1853 "<string>" when reading from a string).
1850 """
1854 """
1851 etype, value, last_traceback = self._get_exc_info()
1855 etype, value, last_traceback = self._get_exc_info()
1852
1856
1853 if filename and issubclass(etype, SyntaxError):
1857 if filename and issubclass(etype, SyntaxError):
1854 try:
1858 try:
1855 value.filename = filename
1859 value.filename = filename
1856 except:
1860 except:
1857 # Not the format we expect; leave it alone
1861 # Not the format we expect; leave it alone
1858 pass
1862 pass
1859
1863
1860 stb = self.SyntaxTB.structured_traceback(etype, value, [])
1864 stb = self.SyntaxTB.structured_traceback(etype, value, [])
1861 self._showtraceback(etype, value, stb)
1865 self._showtraceback(etype, value, stb)
1862
1866
1863 # This is overridden in TerminalInteractiveShell to show a message about
1867 # This is overridden in TerminalInteractiveShell to show a message about
1864 # the %paste magic.
1868 # the %paste magic.
1865 def showindentationerror(self):
1869 def showindentationerror(self):
1866 """Called by run_cell when there's an IndentationError in code entered
1870 """Called by run_cell when there's an IndentationError in code entered
1867 at the prompt.
1871 at the prompt.
1868
1872
1869 This is overridden in TerminalInteractiveShell to show a message about
1873 This is overridden in TerminalInteractiveShell to show a message about
1870 the %paste magic."""
1874 the %paste magic."""
1871 self.showsyntaxerror()
1875 self.showsyntaxerror()
1872
1876
1873 #-------------------------------------------------------------------------
1877 #-------------------------------------------------------------------------
1874 # Things related to readline
1878 # Things related to readline
1875 #-------------------------------------------------------------------------
1879 #-------------------------------------------------------------------------
1876
1880
1877 def init_readline(self):
1881 def init_readline(self):
1878 """DEPRECATED
1882 """DEPRECATED
1879
1883
1880 Moved to terminal subclass, here only to simplify the init logic."""
1884 Moved to terminal subclass, here only to simplify the init logic."""
1881 # Set a number of methods that depend on readline to be no-op
1885 # Set a number of methods that depend on readline to be no-op
1882 warnings.warn('`init_readline` is no-op since IPython 5.0 and is Deprecated',
1886 warnings.warn('`init_readline` is no-op since IPython 5.0 and is Deprecated',
1883 DeprecationWarning, stacklevel=2)
1887 DeprecationWarning, stacklevel=2)
1884 self.set_custom_completer = no_op
1888 self.set_custom_completer = no_op
1885
1889
1886 @skip_doctest
1890 @skip_doctest
1887 def set_next_input(self, s, replace=False):
1891 def set_next_input(self, s, replace=False):
1888 """ Sets the 'default' input string for the next command line.
1892 """ Sets the 'default' input string for the next command line.
1889
1893
1890 Example::
1894 Example::
1891
1895
1892 In [1]: _ip.set_next_input("Hello Word")
1896 In [1]: _ip.set_next_input("Hello Word")
1893 In [2]: Hello Word_ # cursor is here
1897 In [2]: Hello Word_ # cursor is here
1894 """
1898 """
1895 self.rl_next_input = py3compat.cast_bytes_py2(s)
1899 self.rl_next_input = py3compat.cast_bytes_py2(s)
1896
1900
1897 def _indent_current_str(self):
1901 def _indent_current_str(self):
1898 """return the current level of indentation as a string"""
1902 """return the current level of indentation as a string"""
1899 return self.input_splitter.indent_spaces * ' '
1903 return self.input_splitter.indent_spaces * ' '
1900
1904
1901 #-------------------------------------------------------------------------
1905 #-------------------------------------------------------------------------
1902 # Things related to text completion
1906 # Things related to text completion
1903 #-------------------------------------------------------------------------
1907 #-------------------------------------------------------------------------
1904
1908
1905 def init_completer(self):
1909 def init_completer(self):
1906 """Initialize the completion machinery.
1910 """Initialize the completion machinery.
1907
1911
1908 This creates completion machinery that can be used by client code,
1912 This creates completion machinery that can be used by client code,
1909 either interactively in-process (typically triggered by the readline
1913 either interactively in-process (typically triggered by the readline
1910 library), programmatically (such as in test suites) or out-of-process
1914 library), programmatically (such as in test suites) or out-of-process
1911 (typically over the network by remote frontends).
1915 (typically over the network by remote frontends).
1912 """
1916 """
1913 from IPython.core.completer import IPCompleter
1917 from IPython.core.completer import IPCompleter
1914 from IPython.core.completerlib import (module_completer,
1918 from IPython.core.completerlib import (module_completer,
1915 magic_run_completer, cd_completer, reset_completer)
1919 magic_run_completer, cd_completer, reset_completer)
1916
1920
1917 self.Completer = IPCompleter(shell=self,
1921 self.Completer = IPCompleter(shell=self,
1918 namespace=self.user_ns,
1922 namespace=self.user_ns,
1919 global_namespace=self.user_global_ns,
1923 global_namespace=self.user_global_ns,
1920 use_readline=False,
1924 use_readline=False,
1921 parent=self,
1925 parent=self,
1922 )
1926 )
1923 self.configurables.append(self.Completer)
1927 self.configurables.append(self.Completer)
1924
1928
1925 # Add custom completers to the basic ones built into IPCompleter
1929 # Add custom completers to the basic ones built into IPCompleter
1926 sdisp = self.strdispatchers.get('complete_command', StrDispatch())
1930 sdisp = self.strdispatchers.get('complete_command', StrDispatch())
1927 self.strdispatchers['complete_command'] = sdisp
1931 self.strdispatchers['complete_command'] = sdisp
1928 self.Completer.custom_completers = sdisp
1932 self.Completer.custom_completers = sdisp
1929
1933
1930 self.set_hook('complete_command', module_completer, str_key = 'import')
1934 self.set_hook('complete_command', module_completer, str_key = 'import')
1931 self.set_hook('complete_command', module_completer, str_key = 'from')
1935 self.set_hook('complete_command', module_completer, str_key = 'from')
1932 self.set_hook('complete_command', module_completer, str_key = '%aimport')
1936 self.set_hook('complete_command', module_completer, str_key = '%aimport')
1933 self.set_hook('complete_command', magic_run_completer, str_key = '%run')
1937 self.set_hook('complete_command', magic_run_completer, str_key = '%run')
1934 self.set_hook('complete_command', cd_completer, str_key = '%cd')
1938 self.set_hook('complete_command', cd_completer, str_key = '%cd')
1935 self.set_hook('complete_command', reset_completer, str_key = '%reset')
1939 self.set_hook('complete_command', reset_completer, str_key = '%reset')
1936
1940
1937
1941
1938 @skip_doctest_py2
1942 @skip_doctest_py2
1939 def complete(self, text, line=None, cursor_pos=None):
1943 def complete(self, text, line=None, cursor_pos=None):
1940 """Return the completed text and a list of completions.
1944 """Return the completed text and a list of completions.
1941
1945
1942 Parameters
1946 Parameters
1943 ----------
1947 ----------
1944
1948
1945 text : string
1949 text : string
1946 A string of text to be completed on. It can be given as empty and
1950 A string of text to be completed on. It can be given as empty and
1947 instead a line/position pair are given. In this case, the
1951 instead a line/position pair are given. In this case, the
1948 completer itself will split the line like readline does.
1952 completer itself will split the line like readline does.
1949
1953
1950 line : string, optional
1954 line : string, optional
1951 The complete line that text is part of.
1955 The complete line that text is part of.
1952
1956
1953 cursor_pos : int, optional
1957 cursor_pos : int, optional
1954 The position of the cursor on the input line.
1958 The position of the cursor on the input line.
1955
1959
1956 Returns
1960 Returns
1957 -------
1961 -------
1958 text : string
1962 text : string
1959 The actual text that was completed.
1963 The actual text that was completed.
1960
1964
1961 matches : list
1965 matches : list
1962 A sorted list with all possible completions.
1966 A sorted list with all possible completions.
1963
1967
1964 The optional arguments allow the completion to take more context into
1968 The optional arguments allow the completion to take more context into
1965 account, and are part of the low-level completion API.
1969 account, and are part of the low-level completion API.
1966
1970
1967 This is a wrapper around the completion mechanism, similar to what
1971 This is a wrapper around the completion mechanism, similar to what
1968 readline does at the command line when the TAB key is hit. By
1972 readline does at the command line when the TAB key is hit. By
1969 exposing it as a method, it can be used by other non-readline
1973 exposing it as a method, it can be used by other non-readline
1970 environments (such as GUIs) for text completion.
1974 environments (such as GUIs) for text completion.
1971
1975
1972 Simple usage example:
1976 Simple usage example:
1973
1977
1974 In [1]: x = 'hello'
1978 In [1]: x = 'hello'
1975
1979
1976 In [2]: _ip.complete('x.l')
1980 In [2]: _ip.complete('x.l')
1977 Out[2]: ('x.l', ['x.ljust', 'x.lower', 'x.lstrip'])
1981 Out[2]: ('x.l', ['x.ljust', 'x.lower', 'x.lstrip'])
1978 """
1982 """
1979
1983
1980 # Inject names into __builtin__ so we can complete on the added names.
1984 # Inject names into __builtin__ so we can complete on the added names.
1981 with self.builtin_trap:
1985 with self.builtin_trap:
1982 return self.Completer.complete(text, line, cursor_pos)
1986 return self.Completer.complete(text, line, cursor_pos)
1983
1987
1984 def set_custom_completer(self, completer, pos=0):
1988 def set_custom_completer(self, completer, pos=0):
1985 """Adds a new custom completer function.
1989 """Adds a new custom completer function.
1986
1990
1987 The position argument (defaults to 0) is the index in the completers
1991 The position argument (defaults to 0) is the index in the completers
1988 list where you want the completer to be inserted."""
1992 list where you want the completer to be inserted."""
1989
1993
1990 newcomp = types.MethodType(completer,self.Completer)
1994 newcomp = types.MethodType(completer,self.Completer)
1991 self.Completer.matchers.insert(pos,newcomp)
1995 self.Completer.matchers.insert(pos,newcomp)
1992
1996
1993 def set_completer_frame(self, frame=None):
1997 def set_completer_frame(self, frame=None):
1994 """Set the frame of the completer."""
1998 """Set the frame of the completer."""
1995 if frame:
1999 if frame:
1996 self.Completer.namespace = frame.f_locals
2000 self.Completer.namespace = frame.f_locals
1997 self.Completer.global_namespace = frame.f_globals
2001 self.Completer.global_namespace = frame.f_globals
1998 else:
2002 else:
1999 self.Completer.namespace = self.user_ns
2003 self.Completer.namespace = self.user_ns
2000 self.Completer.global_namespace = self.user_global_ns
2004 self.Completer.global_namespace = self.user_global_ns
2001
2005
2002 #-------------------------------------------------------------------------
2006 #-------------------------------------------------------------------------
2003 # Things related to magics
2007 # Things related to magics
2004 #-------------------------------------------------------------------------
2008 #-------------------------------------------------------------------------
2005
2009
2006 def init_magics(self):
2010 def init_magics(self):
2007 from IPython.core import magics as m
2011 from IPython.core import magics as m
2008 self.magics_manager = magic.MagicsManager(shell=self,
2012 self.magics_manager = magic.MagicsManager(shell=self,
2009 parent=self,
2013 parent=self,
2010 user_magics=m.UserMagics(self))
2014 user_magics=m.UserMagics(self))
2011 self.configurables.append(self.magics_manager)
2015 self.configurables.append(self.magics_manager)
2012
2016
2013 # Expose as public API from the magics manager
2017 # Expose as public API from the magics manager
2014 self.register_magics = self.magics_manager.register
2018 self.register_magics = self.magics_manager.register
2015
2019
2016 self.register_magics(m.AutoMagics, m.BasicMagics, m.CodeMagics,
2020 self.register_magics(m.AutoMagics, m.BasicMagics, m.CodeMagics,
2017 m.ConfigMagics, m.DisplayMagics, m.ExecutionMagics,
2021 m.ConfigMagics, m.DisplayMagics, m.ExecutionMagics,
2018 m.ExtensionMagics, m.HistoryMagics, m.LoggingMagics,
2022 m.ExtensionMagics, m.HistoryMagics, m.LoggingMagics,
2019 m.NamespaceMagics, m.OSMagics, m.PylabMagics, m.ScriptMagics,
2023 m.NamespaceMagics, m.OSMagics, m.PylabMagics, m.ScriptMagics,
2020 )
2024 )
2021
2025
2022 # Register Magic Aliases
2026 # Register Magic Aliases
2023 mman = self.magics_manager
2027 mman = self.magics_manager
2024 # FIXME: magic aliases should be defined by the Magics classes
2028 # FIXME: magic aliases should be defined by the Magics classes
2025 # or in MagicsManager, not here
2029 # or in MagicsManager, not here
2026 mman.register_alias('ed', 'edit')
2030 mman.register_alias('ed', 'edit')
2027 mman.register_alias('hist', 'history')
2031 mman.register_alias('hist', 'history')
2028 mman.register_alias('rep', 'recall')
2032 mman.register_alias('rep', 'recall')
2029 mman.register_alias('SVG', 'svg', 'cell')
2033 mman.register_alias('SVG', 'svg', 'cell')
2030 mman.register_alias('HTML', 'html', 'cell')
2034 mman.register_alias('HTML', 'html', 'cell')
2031 mman.register_alias('file', 'writefile', 'cell')
2035 mman.register_alias('file', 'writefile', 'cell')
2032
2036
2033 # FIXME: Move the color initialization to the DisplayHook, which
2037 # FIXME: Move the color initialization to the DisplayHook, which
2034 # should be split into a prompt manager and displayhook. We probably
2038 # should be split into a prompt manager and displayhook. We probably
2035 # even need a centralize colors management object.
2039 # even need a centralize colors management object.
2036 self.magic('colors %s' % self.colors)
2040 self.magic('colors %s' % self.colors)
2037
2041
2038 # Defined here so that it's included in the documentation
2042 # Defined here so that it's included in the documentation
2039 @functools.wraps(magic.MagicsManager.register_function)
2043 @functools.wraps(magic.MagicsManager.register_function)
2040 def register_magic_function(self, func, magic_kind='line', magic_name=None):
2044 def register_magic_function(self, func, magic_kind='line', magic_name=None):
2041 self.magics_manager.register_function(func,
2045 self.magics_manager.register_function(func,
2042 magic_kind=magic_kind, magic_name=magic_name)
2046 magic_kind=magic_kind, magic_name=magic_name)
2043
2047
2044 def run_line_magic(self, magic_name, line):
2048 def run_line_magic(self, magic_name, line):
2045 """Execute the given line magic.
2049 """Execute the given line magic.
2046
2050
2047 Parameters
2051 Parameters
2048 ----------
2052 ----------
2049 magic_name : str
2053 magic_name : str
2050 Name of the desired magic function, without '%' prefix.
2054 Name of the desired magic function, without '%' prefix.
2051
2055
2052 line : str
2056 line : str
2053 The rest of the input line as a single string.
2057 The rest of the input line as a single string.
2054 """
2058 """
2055 fn = self.find_line_magic(magic_name)
2059 fn = self.find_line_magic(magic_name)
2056 if fn is None:
2060 if fn is None:
2057 cm = self.find_cell_magic(magic_name)
2061 cm = self.find_cell_magic(magic_name)
2058 etpl = "Line magic function `%%%s` not found%s."
2062 etpl = "Line magic function `%%%s` not found%s."
2059 extra = '' if cm is None else (' (But cell magic `%%%%%s` exists, '
2063 extra = '' if cm is None else (' (But cell magic `%%%%%s` exists, '
2060 'did you mean that instead?)' % magic_name )
2064 'did you mean that instead?)' % magic_name )
2061 error(etpl % (magic_name, extra))
2065 error(etpl % (magic_name, extra))
2062 else:
2066 else:
2063 # Note: this is the distance in the stack to the user's frame.
2067 # Note: this is the distance in the stack to the user's frame.
2064 # This will need to be updated if the internal calling logic gets
2068 # This will need to be updated if the internal calling logic gets
2065 # refactored, or else we'll be expanding the wrong variables.
2069 # refactored, or else we'll be expanding the wrong variables.
2066 stack_depth = 2
2070 stack_depth = 2
2067 magic_arg_s = self.var_expand(line, stack_depth)
2071 magic_arg_s = self.var_expand(line, stack_depth)
2068 # Put magic args in a list so we can call with f(*a) syntax
2072 # Put magic args in a list so we can call with f(*a) syntax
2069 args = [magic_arg_s]
2073 args = [magic_arg_s]
2070 kwargs = {}
2074 kwargs = {}
2071 # Grab local namespace if we need it:
2075 # Grab local namespace if we need it:
2072 if getattr(fn, "needs_local_scope", False):
2076 if getattr(fn, "needs_local_scope", False):
2073 kwargs['local_ns'] = sys._getframe(stack_depth).f_locals
2077 kwargs['local_ns'] = sys._getframe(stack_depth).f_locals
2074 with self.builtin_trap:
2078 with self.builtin_trap:
2075 result = fn(*args,**kwargs)
2079 result = fn(*args,**kwargs)
2076 return result
2080 return result
2077
2081
2078 def run_cell_magic(self, magic_name, line, cell):
2082 def run_cell_magic(self, magic_name, line, cell):
2079 """Execute the given cell magic.
2083 """Execute the given cell magic.
2080
2084
2081 Parameters
2085 Parameters
2082 ----------
2086 ----------
2083 magic_name : str
2087 magic_name : str
2084 Name of the desired magic function, without '%' prefix.
2088 Name of the desired magic function, without '%' prefix.
2085
2089
2086 line : str
2090 line : str
2087 The rest of the first input line as a single string.
2091 The rest of the first input line as a single string.
2088
2092
2089 cell : str
2093 cell : str
2090 The body of the cell as a (possibly multiline) string.
2094 The body of the cell as a (possibly multiline) string.
2091 """
2095 """
2092 fn = self.find_cell_magic(magic_name)
2096 fn = self.find_cell_magic(magic_name)
2093 if fn is None:
2097 if fn is None:
2094 lm = self.find_line_magic(magic_name)
2098 lm = self.find_line_magic(magic_name)
2095 etpl = "Cell magic `%%{0}` not found{1}."
2099 etpl = "Cell magic `%%{0}` not found{1}."
2096 extra = '' if lm is None else (' (But line magic `%{0}` exists, '
2100 extra = '' if lm is None else (' (But line magic `%{0}` exists, '
2097 'did you mean that instead?)'.format(magic_name))
2101 'did you mean that instead?)'.format(magic_name))
2098 error(etpl.format(magic_name, extra))
2102 error(etpl.format(magic_name, extra))
2099 elif cell == '':
2103 elif cell == '':
2100 message = '%%{0} is a cell magic, but the cell body is empty.'.format(magic_name)
2104 message = '%%{0} is a cell magic, but the cell body is empty.'.format(magic_name)
2101 if self.find_line_magic(magic_name) is not None:
2105 if self.find_line_magic(magic_name) is not None:
2102 message += ' Did you mean the line magic %{0} (single %)?'.format(magic_name)
2106 message += ' Did you mean the line magic %{0} (single %)?'.format(magic_name)
2103 raise UsageError(message)
2107 raise UsageError(message)
2104 else:
2108 else:
2105 # Note: this is the distance in the stack to the user's frame.
2109 # Note: this is the distance in the stack to the user's frame.
2106 # This will need to be updated if the internal calling logic gets
2110 # This will need to be updated if the internal calling logic gets
2107 # refactored, or else we'll be expanding the wrong variables.
2111 # refactored, or else we'll be expanding the wrong variables.
2108 stack_depth = 2
2112 stack_depth = 2
2109 magic_arg_s = self.var_expand(line, stack_depth)
2113 magic_arg_s = self.var_expand(line, stack_depth)
2110 with self.builtin_trap:
2114 with self.builtin_trap:
2111 result = fn(magic_arg_s, cell)
2115 result = fn(magic_arg_s, cell)
2112 return result
2116 return result
2113
2117
2114 def find_line_magic(self, magic_name):
2118 def find_line_magic(self, magic_name):
2115 """Find and return a line magic by name.
2119 """Find and return a line magic by name.
2116
2120
2117 Returns None if the magic isn't found."""
2121 Returns None if the magic isn't found."""
2118 return self.magics_manager.magics['line'].get(magic_name)
2122 return self.magics_manager.magics['line'].get(magic_name)
2119
2123
2120 def find_cell_magic(self, magic_name):
2124 def find_cell_magic(self, magic_name):
2121 """Find and return a cell magic by name.
2125 """Find and return a cell magic by name.
2122
2126
2123 Returns None if the magic isn't found."""
2127 Returns None if the magic isn't found."""
2124 return self.magics_manager.magics['cell'].get(magic_name)
2128 return self.magics_manager.magics['cell'].get(magic_name)
2125
2129
2126 def find_magic(self, magic_name, magic_kind='line'):
2130 def find_magic(self, magic_name, magic_kind='line'):
2127 """Find and return a magic of the given type by name.
2131 """Find and return a magic of the given type by name.
2128
2132
2129 Returns None if the magic isn't found."""
2133 Returns None if the magic isn't found."""
2130 return self.magics_manager.magics[magic_kind].get(magic_name)
2134 return self.magics_manager.magics[magic_kind].get(magic_name)
2131
2135
2132 def magic(self, arg_s):
2136 def magic(self, arg_s):
2133 """DEPRECATED. Use run_line_magic() instead.
2137 """DEPRECATED. Use run_line_magic() instead.
2134
2138
2135 Call a magic function by name.
2139 Call a magic function by name.
2136
2140
2137 Input: a string containing the name of the magic function to call and
2141 Input: a string containing the name of the magic function to call and
2138 any additional arguments to be passed to the magic.
2142 any additional arguments to be passed to the magic.
2139
2143
2140 magic('name -opt foo bar') is equivalent to typing at the ipython
2144 magic('name -opt foo bar') is equivalent to typing at the ipython
2141 prompt:
2145 prompt:
2142
2146
2143 In[1]: %name -opt foo bar
2147 In[1]: %name -opt foo bar
2144
2148
2145 To call a magic without arguments, simply use magic('name').
2149 To call a magic without arguments, simply use magic('name').
2146
2150
2147 This provides a proper Python function to call IPython's magics in any
2151 This provides a proper Python function to call IPython's magics in any
2148 valid Python code you can type at the interpreter, including loops and
2152 valid Python code you can type at the interpreter, including loops and
2149 compound statements.
2153 compound statements.
2150 """
2154 """
2151 # TODO: should we issue a loud deprecation warning here?
2155 # TODO: should we issue a loud deprecation warning here?
2152 magic_name, _, magic_arg_s = arg_s.partition(' ')
2156 magic_name, _, magic_arg_s = arg_s.partition(' ')
2153 magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
2157 magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
2154 return self.run_line_magic(magic_name, magic_arg_s)
2158 return self.run_line_magic(magic_name, magic_arg_s)
2155
2159
2156 #-------------------------------------------------------------------------
2160 #-------------------------------------------------------------------------
2157 # Things related to macros
2161 # Things related to macros
2158 #-------------------------------------------------------------------------
2162 #-------------------------------------------------------------------------
2159
2163
2160 def define_macro(self, name, themacro):
2164 def define_macro(self, name, themacro):
2161 """Define a new macro
2165 """Define a new macro
2162
2166
2163 Parameters
2167 Parameters
2164 ----------
2168 ----------
2165 name : str
2169 name : str
2166 The name of the macro.
2170 The name of the macro.
2167 themacro : str or Macro
2171 themacro : str or Macro
2168 The action to do upon invoking the macro. If a string, a new
2172 The action to do upon invoking the macro. If a string, a new
2169 Macro object is created by passing the string to it.
2173 Macro object is created by passing the string to it.
2170 """
2174 """
2171
2175
2172 from IPython.core import macro
2176 from IPython.core import macro
2173
2177
2174 if isinstance(themacro, string_types):
2178 if isinstance(themacro, string_types):
2175 themacro = macro.Macro(themacro)
2179 themacro = macro.Macro(themacro)
2176 if not isinstance(themacro, macro.Macro):
2180 if not isinstance(themacro, macro.Macro):
2177 raise ValueError('A macro must be a string or a Macro instance.')
2181 raise ValueError('A macro must be a string or a Macro instance.')
2178 self.user_ns[name] = themacro
2182 self.user_ns[name] = themacro
2179
2183
2180 #-------------------------------------------------------------------------
2184 #-------------------------------------------------------------------------
2181 # Things related to the running of system commands
2185 # Things related to the running of system commands
2182 #-------------------------------------------------------------------------
2186 #-------------------------------------------------------------------------
2183
2187
2184 def system_piped(self, cmd):
2188 def system_piped(self, cmd):
2185 """Call the given cmd in a subprocess, piping stdout/err
2189 """Call the given cmd in a subprocess, piping stdout/err
2186
2190
2187 Parameters
2191 Parameters
2188 ----------
2192 ----------
2189 cmd : str
2193 cmd : str
2190 Command to execute (can not end in '&', as background processes are
2194 Command to execute (can not end in '&', as background processes are
2191 not supported. Should not be a command that expects input
2195 not supported. Should not be a command that expects input
2192 other than simple text.
2196 other than simple text.
2193 """
2197 """
2194 if cmd.rstrip().endswith('&'):
2198 if cmd.rstrip().endswith('&'):
2195 # this is *far* from a rigorous test
2199 # this is *far* from a rigorous test
2196 # We do not support backgrounding processes because we either use
2200 # We do not support backgrounding processes because we either use
2197 # pexpect or pipes to read from. Users can always just call
2201 # pexpect or pipes to read from. Users can always just call
2198 # os.system() or use ip.system=ip.system_raw
2202 # os.system() or use ip.system=ip.system_raw
2199 # if they really want a background process.
2203 # if they really want a background process.
2200 raise OSError("Background processes not supported.")
2204 raise OSError("Background processes not supported.")
2201
2205
2202 # we explicitly do NOT return the subprocess status code, because
2206 # we explicitly do NOT return the subprocess status code, because
2203 # a non-None value would trigger :func:`sys.displayhook` calls.
2207 # a non-None value would trigger :func:`sys.displayhook` calls.
2204 # Instead, we store the exit_code in user_ns.
2208 # Instead, we store the exit_code in user_ns.
2205 self.user_ns['_exit_code'] = system(self.var_expand(cmd, depth=1))
2209 self.user_ns['_exit_code'] = system(self.var_expand(cmd, depth=1))
2206
2210
2207 def system_raw(self, cmd):
2211 def system_raw(self, cmd):
2208 """Call the given cmd in a subprocess using os.system on Windows or
2212 """Call the given cmd in a subprocess using os.system on Windows or
2209 subprocess.call using the system shell on other platforms.
2213 subprocess.call using the system shell on other platforms.
2210
2214
2211 Parameters
2215 Parameters
2212 ----------
2216 ----------
2213 cmd : str
2217 cmd : str
2214 Command to execute.
2218 Command to execute.
2215 """
2219 """
2216 cmd = self.var_expand(cmd, depth=1)
2220 cmd = self.var_expand(cmd, depth=1)
2217 # protect os.system from UNC paths on Windows, which it can't handle:
2221 # protect os.system from UNC paths on Windows, which it can't handle:
2218 if sys.platform == 'win32':
2222 if sys.platform == 'win32':
2219 from IPython.utils._process_win32 import AvoidUNCPath
2223 from IPython.utils._process_win32 import AvoidUNCPath
2220 with AvoidUNCPath() as path:
2224 with AvoidUNCPath() as path:
2221 if path is not None:
2225 if path is not None:
2222 cmd = '"pushd %s &&"%s' % (path, cmd)
2226 cmd = '"pushd %s &&"%s' % (path, cmd)
2223 cmd = py3compat.unicode_to_str(cmd)
2227 cmd = py3compat.unicode_to_str(cmd)
2224 try:
2228 try:
2225 ec = os.system(cmd)
2229 ec = os.system(cmd)
2226 except KeyboardInterrupt:
2230 except KeyboardInterrupt:
2227 print('\n' + self.get_exception_only(), file=sys.stderr)
2231 print('\n' + self.get_exception_only(), file=sys.stderr)
2228 ec = -2
2232 ec = -2
2229 else:
2233 else:
2230 cmd = py3compat.unicode_to_str(cmd)
2234 cmd = py3compat.unicode_to_str(cmd)
2231 # For posix the result of the subprocess.call() below is an exit
2235 # For posix the result of the subprocess.call() below is an exit
2232 # code, which by convention is zero for success, positive for
2236 # code, which by convention is zero for success, positive for
2233 # program failure. Exit codes above 128 are reserved for signals,
2237 # program failure. Exit codes above 128 are reserved for signals,
2234 # and the formula for converting a signal to an exit code is usually
2238 # and the formula for converting a signal to an exit code is usually
2235 # signal_number+128. To more easily differentiate between exit
2239 # signal_number+128. To more easily differentiate between exit
2236 # codes and signals, ipython uses negative numbers. For instance
2240 # codes and signals, ipython uses negative numbers. For instance
2237 # since control-c is signal 2 but exit code 130, ipython's
2241 # since control-c is signal 2 but exit code 130, ipython's
2238 # _exit_code variable will read -2. Note that some shells like
2242 # _exit_code variable will read -2. Note that some shells like
2239 # csh and fish don't follow sh/bash conventions for exit codes.
2243 # csh and fish don't follow sh/bash conventions for exit codes.
2240 executable = os.environ.get('SHELL', None)
2244 executable = os.environ.get('SHELL', None)
2241 try:
2245 try:
2242 # Use env shell instead of default /bin/sh
2246 # Use env shell instead of default /bin/sh
2243 ec = subprocess.call(cmd, shell=True, executable=executable)
2247 ec = subprocess.call(cmd, shell=True, executable=executable)
2244 except KeyboardInterrupt:
2248 except KeyboardInterrupt:
2245 # intercept control-C; a long traceback is not useful here
2249 # intercept control-C; a long traceback is not useful here
2246 print('\n' + self.get_exception_only(), file=sys.stderr)
2250 print('\n' + self.get_exception_only(), file=sys.stderr)
2247 ec = 130
2251 ec = 130
2248 if ec > 128:
2252 if ec > 128:
2249 ec = -(ec - 128)
2253 ec = -(ec - 128)
2250
2254
2251 # We explicitly do NOT return the subprocess status code, because
2255 # We explicitly do NOT return the subprocess status code, because
2252 # a non-None value would trigger :func:`sys.displayhook` calls.
2256 # a non-None value would trigger :func:`sys.displayhook` calls.
2253 # Instead, we store the exit_code in user_ns. Note the semantics
2257 # Instead, we store the exit_code in user_ns. Note the semantics
2254 # of _exit_code: for control-c, _exit_code == -signal.SIGNIT,
2258 # of _exit_code: for control-c, _exit_code == -signal.SIGNIT,
2255 # but raising SystemExit(_exit_code) will give status 254!
2259 # but raising SystemExit(_exit_code) will give status 254!
2256 self.user_ns['_exit_code'] = ec
2260 self.user_ns['_exit_code'] = ec
2257
2261
2258 # use piped system by default, because it is better behaved
2262 # use piped system by default, because it is better behaved
2259 system = system_piped
2263 system = system_piped
2260
2264
2261 def getoutput(self, cmd, split=True, depth=0):
2265 def getoutput(self, cmd, split=True, depth=0):
2262 """Get output (possibly including stderr) from a subprocess.
2266 """Get output (possibly including stderr) from a subprocess.
2263
2267
2264 Parameters
2268 Parameters
2265 ----------
2269 ----------
2266 cmd : str
2270 cmd : str
2267 Command to execute (can not end in '&', as background processes are
2271 Command to execute (can not end in '&', as background processes are
2268 not supported.
2272 not supported.
2269 split : bool, optional
2273 split : bool, optional
2270 If True, split the output into an IPython SList. Otherwise, an
2274 If True, split the output into an IPython SList. Otherwise, an
2271 IPython LSString is returned. These are objects similar to normal
2275 IPython LSString is returned. These are objects similar to normal
2272 lists and strings, with a few convenience attributes for easier
2276 lists and strings, with a few convenience attributes for easier
2273 manipulation of line-based output. You can use '?' on them for
2277 manipulation of line-based output. You can use '?' on them for
2274 details.
2278 details.
2275 depth : int, optional
2279 depth : int, optional
2276 How many frames above the caller are the local variables which should
2280 How many frames above the caller are the local variables which should
2277 be expanded in the command string? The default (0) assumes that the
2281 be expanded in the command string? The default (0) assumes that the
2278 expansion variables are in the stack frame calling this function.
2282 expansion variables are in the stack frame calling this function.
2279 """
2283 """
2280 if cmd.rstrip().endswith('&'):
2284 if cmd.rstrip().endswith('&'):
2281 # this is *far* from a rigorous test
2285 # this is *far* from a rigorous test
2282 raise OSError("Background processes not supported.")
2286 raise OSError("Background processes not supported.")
2283 out = getoutput(self.var_expand(cmd, depth=depth+1))
2287 out = getoutput(self.var_expand(cmd, depth=depth+1))
2284 if split:
2288 if split:
2285 out = SList(out.splitlines())
2289 out = SList(out.splitlines())
2286 else:
2290 else:
2287 out = LSString(out)
2291 out = LSString(out)
2288 return out
2292 return out
2289
2293
2290 #-------------------------------------------------------------------------
2294 #-------------------------------------------------------------------------
2291 # Things related to aliases
2295 # Things related to aliases
2292 #-------------------------------------------------------------------------
2296 #-------------------------------------------------------------------------
2293
2297
2294 def init_alias(self):
2298 def init_alias(self):
2295 self.alias_manager = AliasManager(shell=self, parent=self)
2299 self.alias_manager = AliasManager(shell=self, parent=self)
2296 self.configurables.append(self.alias_manager)
2300 self.configurables.append(self.alias_manager)
2297
2301
2298 #-------------------------------------------------------------------------
2302 #-------------------------------------------------------------------------
2299 # Things related to extensions
2303 # Things related to extensions
2300 #-------------------------------------------------------------------------
2304 #-------------------------------------------------------------------------
2301
2305
2302 def init_extension_manager(self):
2306 def init_extension_manager(self):
2303 self.extension_manager = ExtensionManager(shell=self, parent=self)
2307 self.extension_manager = ExtensionManager(shell=self, parent=self)
2304 self.configurables.append(self.extension_manager)
2308 self.configurables.append(self.extension_manager)
2305
2309
2306 #-------------------------------------------------------------------------
2310 #-------------------------------------------------------------------------
2307 # Things related to payloads
2311 # Things related to payloads
2308 #-------------------------------------------------------------------------
2312 #-------------------------------------------------------------------------
2309
2313
2310 def init_payload(self):
2314 def init_payload(self):
2311 self.payload_manager = PayloadManager(parent=self)
2315 self.payload_manager = PayloadManager(parent=self)
2312 self.configurables.append(self.payload_manager)
2316 self.configurables.append(self.payload_manager)
2313
2317
2314 #-------------------------------------------------------------------------
2318 #-------------------------------------------------------------------------
2315 # Things related to the prefilter
2319 # Things related to the prefilter
2316 #-------------------------------------------------------------------------
2320 #-------------------------------------------------------------------------
2317
2321
2318 def init_prefilter(self):
2322 def init_prefilter(self):
2319 self.prefilter_manager = PrefilterManager(shell=self, parent=self)
2323 self.prefilter_manager = PrefilterManager(shell=self, parent=self)
2320 self.configurables.append(self.prefilter_manager)
2324 self.configurables.append(self.prefilter_manager)
2321 # Ultimately this will be refactored in the new interpreter code, but
2325 # Ultimately this will be refactored in the new interpreter code, but
2322 # for now, we should expose the main prefilter method (there's legacy
2326 # for now, we should expose the main prefilter method (there's legacy
2323 # code out there that may rely on this).
2327 # code out there that may rely on this).
2324 self.prefilter = self.prefilter_manager.prefilter_lines
2328 self.prefilter = self.prefilter_manager.prefilter_lines
2325
2329
2326 def auto_rewrite_input(self, cmd):
2330 def auto_rewrite_input(self, cmd):
2327 """Print to the screen the rewritten form of the user's command.
2331 """Print to the screen the rewritten form of the user's command.
2328
2332
2329 This shows visual feedback by rewriting input lines that cause
2333 This shows visual feedback by rewriting input lines that cause
2330 automatic calling to kick in, like::
2334 automatic calling to kick in, like::
2331
2335
2332 /f x
2336 /f x
2333
2337
2334 into::
2338 into::
2335
2339
2336 ------> f(x)
2340 ------> f(x)
2337
2341
2338 after the user's input prompt. This helps the user understand that the
2342 after the user's input prompt. This helps the user understand that the
2339 input line was transformed automatically by IPython.
2343 input line was transformed automatically by IPython.
2340 """
2344 """
2341 if not self.show_rewritten_input:
2345 if not self.show_rewritten_input:
2342 return
2346 return
2343
2347
2344 # This is overridden in TerminalInteractiveShell to use fancy prompts
2348 # This is overridden in TerminalInteractiveShell to use fancy prompts
2345 print("------> " + cmd)
2349 print("------> " + cmd)
2346
2350
2347 #-------------------------------------------------------------------------
2351 #-------------------------------------------------------------------------
2348 # Things related to extracting values/expressions from kernel and user_ns
2352 # Things related to extracting values/expressions from kernel and user_ns
2349 #-------------------------------------------------------------------------
2353 #-------------------------------------------------------------------------
2350
2354
2351 def _user_obj_error(self):
2355 def _user_obj_error(self):
2352 """return simple exception dict
2356 """return simple exception dict
2353
2357
2354 for use in user_expressions
2358 for use in user_expressions
2355 """
2359 """
2356
2360
2357 etype, evalue, tb = self._get_exc_info()
2361 etype, evalue, tb = self._get_exc_info()
2358 stb = self.InteractiveTB.get_exception_only(etype, evalue)
2362 stb = self.InteractiveTB.get_exception_only(etype, evalue)
2359
2363
2360 exc_info = {
2364 exc_info = {
2361 u'status' : 'error',
2365 u'status' : 'error',
2362 u'traceback' : stb,
2366 u'traceback' : stb,
2363 u'ename' : unicode_type(etype.__name__),
2367 u'ename' : unicode_type(etype.__name__),
2364 u'evalue' : py3compat.safe_unicode(evalue),
2368 u'evalue' : py3compat.safe_unicode(evalue),
2365 }
2369 }
2366
2370
2367 return exc_info
2371 return exc_info
2368
2372
2369 def _format_user_obj(self, obj):
2373 def _format_user_obj(self, obj):
2370 """format a user object to display dict
2374 """format a user object to display dict
2371
2375
2372 for use in user_expressions
2376 for use in user_expressions
2373 """
2377 """
2374
2378
2375 data, md = self.display_formatter.format(obj)
2379 data, md = self.display_formatter.format(obj)
2376 value = {
2380 value = {
2377 'status' : 'ok',
2381 'status' : 'ok',
2378 'data' : data,
2382 'data' : data,
2379 'metadata' : md,
2383 'metadata' : md,
2380 }
2384 }
2381 return value
2385 return value
2382
2386
2383 def user_expressions(self, expressions):
2387 def user_expressions(self, expressions):
2384 """Evaluate a dict of expressions in the user's namespace.
2388 """Evaluate a dict of expressions in the user's namespace.
2385
2389
2386 Parameters
2390 Parameters
2387 ----------
2391 ----------
2388 expressions : dict
2392 expressions : dict
2389 A dict with string keys and string values. The expression values
2393 A dict with string keys and string values. The expression values
2390 should be valid Python expressions, each of which will be evaluated
2394 should be valid Python expressions, each of which will be evaluated
2391 in the user namespace.
2395 in the user namespace.
2392
2396
2393 Returns
2397 Returns
2394 -------
2398 -------
2395 A dict, keyed like the input expressions dict, with the rich mime-typed
2399 A dict, keyed like the input expressions dict, with the rich mime-typed
2396 display_data of each value.
2400 display_data of each value.
2397 """
2401 """
2398 out = {}
2402 out = {}
2399 user_ns = self.user_ns
2403 user_ns = self.user_ns
2400 global_ns = self.user_global_ns
2404 global_ns = self.user_global_ns
2401
2405
2402 for key, expr in iteritems(expressions):
2406 for key, expr in iteritems(expressions):
2403 try:
2407 try:
2404 value = self._format_user_obj(eval(expr, global_ns, user_ns))
2408 value = self._format_user_obj(eval(expr, global_ns, user_ns))
2405 except:
2409 except:
2406 value = self._user_obj_error()
2410 value = self._user_obj_error()
2407 out[key] = value
2411 out[key] = value
2408 return out
2412 return out
2409
2413
2410 #-------------------------------------------------------------------------
2414 #-------------------------------------------------------------------------
2411 # Things related to the running of code
2415 # Things related to the running of code
2412 #-------------------------------------------------------------------------
2416 #-------------------------------------------------------------------------
2413
2417
2414 def ex(self, cmd):
2418 def ex(self, cmd):
2415 """Execute a normal python statement in user namespace."""
2419 """Execute a normal python statement in user namespace."""
2416 with self.builtin_trap:
2420 with self.builtin_trap:
2417 exec(cmd, self.user_global_ns, self.user_ns)
2421 exec(cmd, self.user_global_ns, self.user_ns)
2418
2422
2419 def ev(self, expr):
2423 def ev(self, expr):
2420 """Evaluate python expression expr in user namespace.
2424 """Evaluate python expression expr in user namespace.
2421
2425
2422 Returns the result of evaluation
2426 Returns the result of evaluation
2423 """
2427 """
2424 with self.builtin_trap:
2428 with self.builtin_trap:
2425 return eval(expr, self.user_global_ns, self.user_ns)
2429 return eval(expr, self.user_global_ns, self.user_ns)
2426
2430
2427 def safe_execfile(self, fname, *where, **kw):
2431 def safe_execfile(self, fname, *where, **kw):
2428 """A safe version of the builtin execfile().
2432 """A safe version of the builtin execfile().
2429
2433
2430 This version will never throw an exception, but instead print
2434 This version will never throw an exception, but instead print
2431 helpful error messages to the screen. This only works on pure
2435 helpful error messages to the screen. This only works on pure
2432 Python files with the .py extension.
2436 Python files with the .py extension.
2433
2437
2434 Parameters
2438 Parameters
2435 ----------
2439 ----------
2436 fname : string
2440 fname : string
2437 The name of the file to be executed.
2441 The name of the file to be executed.
2438 where : tuple
2442 where : tuple
2439 One or two namespaces, passed to execfile() as (globals,locals).
2443 One or two namespaces, passed to execfile() as (globals,locals).
2440 If only one is given, it is passed as both.
2444 If only one is given, it is passed as both.
2441 exit_ignore : bool (False)
2445 exit_ignore : bool (False)
2442 If True, then silence SystemExit for non-zero status (it is always
2446 If True, then silence SystemExit for non-zero status (it is always
2443 silenced for zero status, as it is so common).
2447 silenced for zero status, as it is so common).
2444 raise_exceptions : bool (False)
2448 raise_exceptions : bool (False)
2445 If True raise exceptions everywhere. Meant for testing.
2449 If True raise exceptions everywhere. Meant for testing.
2446 shell_futures : bool (False)
2450 shell_futures : bool (False)
2447 If True, the code will share future statements with the interactive
2451 If True, the code will share future statements with the interactive
2448 shell. It will both be affected by previous __future__ imports, and
2452 shell. It will both be affected by previous __future__ imports, and
2449 any __future__ imports in the code will affect the shell. If False,
2453 any __future__ imports in the code will affect the shell. If False,
2450 __future__ imports are not shared in either direction.
2454 __future__ imports are not shared in either direction.
2451
2455
2452 """
2456 """
2453 kw.setdefault('exit_ignore', False)
2457 kw.setdefault('exit_ignore', False)
2454 kw.setdefault('raise_exceptions', False)
2458 kw.setdefault('raise_exceptions', False)
2455 kw.setdefault('shell_futures', False)
2459 kw.setdefault('shell_futures', False)
2456
2460
2457 fname = os.path.abspath(os.path.expanduser(fname))
2461 fname = os.path.abspath(os.path.expanduser(fname))
2458
2462
2459 # Make sure we can open the file
2463 # Make sure we can open the file
2460 try:
2464 try:
2461 with open(fname):
2465 with open(fname):
2462 pass
2466 pass
2463 except:
2467 except:
2464 warn('Could not open file <%s> for safe execution.' % fname)
2468 warn('Could not open file <%s> for safe execution.' % fname)
2465 return
2469 return
2466
2470
2467 # Find things also in current directory. This is needed to mimic the
2471 # Find things also in current directory. This is needed to mimic the
2468 # behavior of running a script from the system command line, where
2472 # behavior of running a script from the system command line, where
2469 # Python inserts the script's directory into sys.path
2473 # Python inserts the script's directory into sys.path
2470 dname = os.path.dirname(fname)
2474 dname = os.path.dirname(fname)
2471
2475
2472 with prepended_to_syspath(dname), self.builtin_trap:
2476 with prepended_to_syspath(dname), self.builtin_trap:
2473 try:
2477 try:
2474 glob, loc = (where + (None, ))[:2]
2478 glob, loc = (where + (None, ))[:2]
2475 py3compat.execfile(
2479 py3compat.execfile(
2476 fname, glob, loc,
2480 fname, glob, loc,
2477 self.compile if kw['shell_futures'] else None)
2481 self.compile if kw['shell_futures'] else None)
2478 except SystemExit as status:
2482 except SystemExit as status:
2479 # If the call was made with 0 or None exit status (sys.exit(0)
2483 # If the call was made with 0 or None exit status (sys.exit(0)
2480 # or sys.exit() ), don't bother showing a traceback, as both of
2484 # or sys.exit() ), don't bother showing a traceback, as both of
2481 # these are considered normal by the OS:
2485 # these are considered normal by the OS:
2482 # > python -c'import sys;sys.exit(0)'; echo $?
2486 # > python -c'import sys;sys.exit(0)'; echo $?
2483 # 0
2487 # 0
2484 # > python -c'import sys;sys.exit()'; echo $?
2488 # > python -c'import sys;sys.exit()'; echo $?
2485 # 0
2489 # 0
2486 # For other exit status, we show the exception unless
2490 # For other exit status, we show the exception unless
2487 # explicitly silenced, but only in short form.
2491 # explicitly silenced, but only in short form.
2488 if status.code:
2492 if status.code:
2489 if kw['raise_exceptions']:
2493 if kw['raise_exceptions']:
2490 raise
2494 raise
2491 if not kw['exit_ignore']:
2495 if not kw['exit_ignore']:
2492 self.showtraceback(exception_only=True)
2496 self.showtraceback(exception_only=True)
2493 except:
2497 except:
2494 if kw['raise_exceptions']:
2498 if kw['raise_exceptions']:
2495 raise
2499 raise
2496 # tb offset is 2 because we wrap execfile
2500 # tb offset is 2 because we wrap execfile
2497 self.showtraceback(tb_offset=2)
2501 self.showtraceback(tb_offset=2)
2498
2502
2499 def safe_execfile_ipy(self, fname, shell_futures=False, raise_exceptions=False):
2503 def safe_execfile_ipy(self, fname, shell_futures=False, raise_exceptions=False):
2500 """Like safe_execfile, but for .ipy or .ipynb files with IPython syntax.
2504 """Like safe_execfile, but for .ipy or .ipynb files with IPython syntax.
2501
2505
2502 Parameters
2506 Parameters
2503 ----------
2507 ----------
2504 fname : str
2508 fname : str
2505 The name of the file to execute. The filename must have a
2509 The name of the file to execute. The filename must have a
2506 .ipy or .ipynb extension.
2510 .ipy or .ipynb extension.
2507 shell_futures : bool (False)
2511 shell_futures : bool (False)
2508 If True, the code will share future statements with the interactive
2512 If True, the code will share future statements with the interactive
2509 shell. It will both be affected by previous __future__ imports, and
2513 shell. It will both be affected by previous __future__ imports, and
2510 any __future__ imports in the code will affect the shell. If False,
2514 any __future__ imports in the code will affect the shell. If False,
2511 __future__ imports are not shared in either direction.
2515 __future__ imports are not shared in either direction.
2512 raise_exceptions : bool (False)
2516 raise_exceptions : bool (False)
2513 If True raise exceptions everywhere. Meant for testing.
2517 If True raise exceptions everywhere. Meant for testing.
2514 """
2518 """
2515 fname = os.path.abspath(os.path.expanduser(fname))
2519 fname = os.path.abspath(os.path.expanduser(fname))
2516
2520
2517 # Make sure we can open the file
2521 # Make sure we can open the file
2518 try:
2522 try:
2519 with open(fname):
2523 with open(fname):
2520 pass
2524 pass
2521 except:
2525 except:
2522 warn('Could not open file <%s> for safe execution.' % fname)
2526 warn('Could not open file <%s> for safe execution.' % fname)
2523 return
2527 return
2524
2528
2525 # Find things also in current directory. This is needed to mimic the
2529 # Find things also in current directory. This is needed to mimic the
2526 # behavior of running a script from the system command line, where
2530 # behavior of running a script from the system command line, where
2527 # Python inserts the script's directory into sys.path
2531 # Python inserts the script's directory into sys.path
2528 dname = os.path.dirname(fname)
2532 dname = os.path.dirname(fname)
2529
2533
2530 def get_cells():
2534 def get_cells():
2531 """generator for sequence of code blocks to run"""
2535 """generator for sequence of code blocks to run"""
2532 if fname.endswith('.ipynb'):
2536 if fname.endswith('.ipynb'):
2533 from nbformat import read
2537 from nbformat import read
2534 with io_open(fname) as f:
2538 with io_open(fname) as f:
2535 nb = read(f, as_version=4)
2539 nb = read(f, as_version=4)
2536 if not nb.cells:
2540 if not nb.cells:
2537 return
2541 return
2538 for cell in nb.cells:
2542 for cell in nb.cells:
2539 if cell.cell_type == 'code':
2543 if cell.cell_type == 'code':
2540 yield cell.source
2544 yield cell.source
2541 else:
2545 else:
2542 with open(fname) as f:
2546 with open(fname) as f:
2543 yield f.read()
2547 yield f.read()
2544
2548
2545 with prepended_to_syspath(dname):
2549 with prepended_to_syspath(dname):
2546 try:
2550 try:
2547 for cell in get_cells():
2551 for cell in get_cells():
2548 result = self.run_cell(cell, silent=True, shell_futures=shell_futures)
2552 result = self.run_cell(cell, silent=True, shell_futures=shell_futures)
2549 if raise_exceptions:
2553 if raise_exceptions:
2550 result.raise_error()
2554 result.raise_error()
2551 elif not result.success:
2555 elif not result.success:
2552 break
2556 break
2553 except:
2557 except:
2554 if raise_exceptions:
2558 if raise_exceptions:
2555 raise
2559 raise
2556 self.showtraceback()
2560 self.showtraceback()
2557 warn('Unknown failure executing file: <%s>' % fname)
2561 warn('Unknown failure executing file: <%s>' % fname)
2558
2562
2559 def safe_run_module(self, mod_name, where):
2563 def safe_run_module(self, mod_name, where):
2560 """A safe version of runpy.run_module().
2564 """A safe version of runpy.run_module().
2561
2565
2562 This version will never throw an exception, but instead print
2566 This version will never throw an exception, but instead print
2563 helpful error messages to the screen.
2567 helpful error messages to the screen.
2564
2568
2565 `SystemExit` exceptions with status code 0 or None are ignored.
2569 `SystemExit` exceptions with status code 0 or None are ignored.
2566
2570
2567 Parameters
2571 Parameters
2568 ----------
2572 ----------
2569 mod_name : string
2573 mod_name : string
2570 The name of the module to be executed.
2574 The name of the module to be executed.
2571 where : dict
2575 where : dict
2572 The globals namespace.
2576 The globals namespace.
2573 """
2577 """
2574 try:
2578 try:
2575 try:
2579 try:
2576 where.update(
2580 where.update(
2577 runpy.run_module(str(mod_name), run_name="__main__",
2581 runpy.run_module(str(mod_name), run_name="__main__",
2578 alter_sys=True)
2582 alter_sys=True)
2579 )
2583 )
2580 except SystemExit as status:
2584 except SystemExit as status:
2581 if status.code:
2585 if status.code:
2582 raise
2586 raise
2583 except:
2587 except:
2584 self.showtraceback()
2588 self.showtraceback()
2585 warn('Unknown failure executing module: <%s>' % mod_name)
2589 warn('Unknown failure executing module: <%s>' % mod_name)
2586
2590
2587 def run_cell(self, raw_cell, store_history=False, silent=False, shell_futures=True):
2591 def run_cell(self, raw_cell, store_history=False, silent=False, shell_futures=True):
2588 """Run a complete IPython cell.
2592 """Run a complete IPython cell.
2589
2593
2590 Parameters
2594 Parameters
2591 ----------
2595 ----------
2592 raw_cell : str
2596 raw_cell : str
2593 The code (including IPython code such as %magic functions) to run.
2597 The code (including IPython code such as %magic functions) to run.
2594 store_history : bool
2598 store_history : bool
2595 If True, the raw and translated cell will be stored in IPython's
2599 If True, the raw and translated cell will be stored in IPython's
2596 history. For user code calling back into IPython's machinery, this
2600 history. For user code calling back into IPython's machinery, this
2597 should be set to False.
2601 should be set to False.
2598 silent : bool
2602 silent : bool
2599 If True, avoid side-effects, such as implicit displayhooks and
2603 If True, avoid side-effects, such as implicit displayhooks and
2600 and logging. silent=True forces store_history=False.
2604 and logging. silent=True forces store_history=False.
2601 shell_futures : bool
2605 shell_futures : bool
2602 If True, the code will share future statements with the interactive
2606 If True, the code will share future statements with the interactive
2603 shell. It will both be affected by previous __future__ imports, and
2607 shell. It will both be affected by previous __future__ imports, and
2604 any __future__ imports in the code will affect the shell. If False,
2608 any __future__ imports in the code will affect the shell. If False,
2605 __future__ imports are not shared in either direction.
2609 __future__ imports are not shared in either direction.
2606
2610
2607 Returns
2611 Returns
2608 -------
2612 -------
2609 result : :class:`ExecutionResult`
2613 result : :class:`ExecutionResult`
2610 """
2614 """
2611 result = ExecutionResult()
2615 result = ExecutionResult()
2612
2616
2613 if (not raw_cell) or raw_cell.isspace():
2617 if (not raw_cell) or raw_cell.isspace():
2614 self.last_execution_succeeded = True
2618 self.last_execution_succeeded = True
2615 return result
2619 return result
2616
2620
2617 if silent:
2621 if silent:
2618 store_history = False
2622 store_history = False
2619
2623
2620 if store_history:
2624 if store_history:
2621 result.execution_count = self.execution_count
2625 result.execution_count = self.execution_count
2622
2626
2623 def error_before_exec(value):
2627 def error_before_exec(value):
2624 result.error_before_exec = value
2628 result.error_before_exec = value
2625 self.last_execution_succeeded = False
2629 self.last_execution_succeeded = False
2626 return result
2630 return result
2627
2631
2628 self.events.trigger('pre_execute')
2632 self.events.trigger('pre_execute')
2629 if not silent:
2633 if not silent:
2630 self.events.trigger('pre_run_cell')
2634 self.events.trigger('pre_run_cell')
2631
2635
2632 # If any of our input transformation (input_transformer_manager or
2636 # If any of our input transformation (input_transformer_manager or
2633 # prefilter_manager) raises an exception, we store it in this variable
2637 # prefilter_manager) raises an exception, we store it in this variable
2634 # so that we can display the error after logging the input and storing
2638 # so that we can display the error after logging the input and storing
2635 # it in the history.
2639 # it in the history.
2636 preprocessing_exc_tuple = None
2640 preprocessing_exc_tuple = None
2637 try:
2641 try:
2638 # Static input transformations
2642 # Static input transformations
2639 cell = self.input_transformer_manager.transform_cell(raw_cell)
2643 cell = self.input_transformer_manager.transform_cell(raw_cell)
2640 except SyntaxError:
2644 except SyntaxError:
2641 preprocessing_exc_tuple = sys.exc_info()
2645 preprocessing_exc_tuple = sys.exc_info()
2642 cell = raw_cell # cell has to exist so it can be stored/logged
2646 cell = raw_cell # cell has to exist so it can be stored/logged
2643 else:
2647 else:
2644 if len(cell.splitlines()) == 1:
2648 if len(cell.splitlines()) == 1:
2645 # Dynamic transformations - only applied for single line commands
2649 # Dynamic transformations - only applied for single line commands
2646 with self.builtin_trap:
2650 with self.builtin_trap:
2647 try:
2651 try:
2648 # use prefilter_lines to handle trailing newlines
2652 # use prefilter_lines to handle trailing newlines
2649 # restore trailing newline for ast.parse
2653 # restore trailing newline for ast.parse
2650 cell = self.prefilter_manager.prefilter_lines(cell) + '\n'
2654 cell = self.prefilter_manager.prefilter_lines(cell) + '\n'
2651 except Exception:
2655 except Exception:
2652 # don't allow prefilter errors to crash IPython
2656 # don't allow prefilter errors to crash IPython
2653 preprocessing_exc_tuple = sys.exc_info()
2657 preprocessing_exc_tuple = sys.exc_info()
2654
2658
2655 # Store raw and processed history
2659 # Store raw and processed history
2656 if store_history:
2660 if store_history:
2657 self.history_manager.store_inputs(self.execution_count,
2661 self.history_manager.store_inputs(self.execution_count,
2658 cell, raw_cell)
2662 cell, raw_cell)
2659 if not silent:
2663 if not silent:
2660 self.logger.log(cell, raw_cell)
2664 self.logger.log(cell, raw_cell)
2661
2665
2662 # Display the exception if input processing failed.
2666 # Display the exception if input processing failed.
2663 if preprocessing_exc_tuple is not None:
2667 if preprocessing_exc_tuple is not None:
2664 self.showtraceback(preprocessing_exc_tuple)
2668 self.showtraceback(preprocessing_exc_tuple)
2665 if store_history:
2669 if store_history:
2666 self.execution_count += 1
2670 self.execution_count += 1
2667 return error_before_exec(preprocessing_exc_tuple[2])
2671 return error_before_exec(preprocessing_exc_tuple[2])
2668
2672
2669 # Our own compiler remembers the __future__ environment. If we want to
2673 # Our own compiler remembers the __future__ environment. If we want to
2670 # run code with a separate __future__ environment, use the default
2674 # run code with a separate __future__ environment, use the default
2671 # compiler
2675 # compiler
2672 compiler = self.compile if shell_futures else CachingCompiler()
2676 compiler = self.compile if shell_futures else CachingCompiler()
2673
2677
2674 with self.builtin_trap:
2678 with self.builtin_trap:
2675 cell_name = self.compile.cache(cell, self.execution_count)
2679 cell_name = self.compile.cache(cell, self.execution_count)
2676
2680
2677 with self.display_trap:
2681 with self.display_trap:
2678 # Compile to bytecode
2682 # Compile to bytecode
2679 try:
2683 try:
2680 code_ast = compiler.ast_parse(cell, filename=cell_name)
2684 code_ast = compiler.ast_parse(cell, filename=cell_name)
2681 except self.custom_exceptions as e:
2685 except self.custom_exceptions as e:
2682 etype, value, tb = sys.exc_info()
2686 etype, value, tb = sys.exc_info()
2683 self.CustomTB(etype, value, tb)
2687 self.CustomTB(etype, value, tb)
2684 return error_before_exec(e)
2688 return error_before_exec(e)
2685 except IndentationError as e:
2689 except IndentationError as e:
2686 self.showindentationerror()
2690 self.showindentationerror()
2687 if store_history:
2691 if store_history:
2688 self.execution_count += 1
2692 self.execution_count += 1
2689 return error_before_exec(e)
2693 return error_before_exec(e)
2690 except (OverflowError, SyntaxError, ValueError, TypeError,
2694 except (OverflowError, SyntaxError, ValueError, TypeError,
2691 MemoryError) as e:
2695 MemoryError) as e:
2692 self.showsyntaxerror()
2696 self.showsyntaxerror()
2693 if store_history:
2697 if store_history:
2694 self.execution_count += 1
2698 self.execution_count += 1
2695 return error_before_exec(e)
2699 return error_before_exec(e)
2696
2700
2697 # Apply AST transformations
2701 # Apply AST transformations
2698 try:
2702 try:
2699 code_ast = self.transform_ast(code_ast)
2703 code_ast = self.transform_ast(code_ast)
2700 except InputRejected as e:
2704 except InputRejected as e:
2701 self.showtraceback()
2705 self.showtraceback()
2702 if store_history:
2706 if store_history:
2703 self.execution_count += 1
2707 self.execution_count += 1
2704 return error_before_exec(e)
2708 return error_before_exec(e)
2705
2709
2706 # Give the displayhook a reference to our ExecutionResult so it
2710 # Give the displayhook a reference to our ExecutionResult so it
2707 # can fill in the output value.
2711 # can fill in the output value.
2708 self.displayhook.exec_result = result
2712 self.displayhook.exec_result = result
2709
2713
2710 # Execute the user code
2714 # Execute the user code
2711 interactivity = "none" if silent else self.ast_node_interactivity
2715 interactivity = "none" if silent else self.ast_node_interactivity
2712 has_raised = self.run_ast_nodes(code_ast.body, cell_name,
2716 has_raised = self.run_ast_nodes(code_ast.body, cell_name,
2713 interactivity=interactivity, compiler=compiler, result=result)
2717 interactivity=interactivity, compiler=compiler, result=result)
2714
2718
2715 self.last_execution_succeeded = not has_raised
2719 self.last_execution_succeeded = not has_raised
2716
2720
2717 # Reset this so later displayed values do not modify the
2721 # Reset this so later displayed values do not modify the
2718 # ExecutionResult
2722 # ExecutionResult
2719 self.displayhook.exec_result = None
2723 self.displayhook.exec_result = None
2720
2724
2721 self.events.trigger('post_execute')
2725 self.events.trigger('post_execute')
2722 if not silent:
2726 if not silent:
2723 self.events.trigger('post_run_cell')
2727 self.events.trigger('post_run_cell')
2724
2728
2725 if store_history:
2729 if store_history:
2726 # Write output to the database. Does nothing unless
2730 # Write output to the database. Does nothing unless
2727 # history output logging is enabled.
2731 # history output logging is enabled.
2728 self.history_manager.store_output(self.execution_count)
2732 self.history_manager.store_output(self.execution_count)
2729 # Each cell is a *single* input, regardless of how many lines it has
2733 # Each cell is a *single* input, regardless of how many lines it has
2730 self.execution_count += 1
2734 self.execution_count += 1
2731
2735
2732 return result
2736 return result
2733
2737
2734 def transform_ast(self, node):
2738 def transform_ast(self, node):
2735 """Apply the AST transformations from self.ast_transformers
2739 """Apply the AST transformations from self.ast_transformers
2736
2740
2737 Parameters
2741 Parameters
2738 ----------
2742 ----------
2739 node : ast.Node
2743 node : ast.Node
2740 The root node to be transformed. Typically called with the ast.Module
2744 The root node to be transformed. Typically called with the ast.Module
2741 produced by parsing user input.
2745 produced by parsing user input.
2742
2746
2743 Returns
2747 Returns
2744 -------
2748 -------
2745 An ast.Node corresponding to the node it was called with. Note that it
2749 An ast.Node corresponding to the node it was called with. Note that it
2746 may also modify the passed object, so don't rely on references to the
2750 may also modify the passed object, so don't rely on references to the
2747 original AST.
2751 original AST.
2748 """
2752 """
2749 for transformer in self.ast_transformers:
2753 for transformer in self.ast_transformers:
2750 try:
2754 try:
2751 node = transformer.visit(node)
2755 node = transformer.visit(node)
2752 except InputRejected:
2756 except InputRejected:
2753 # User-supplied AST transformers can reject an input by raising
2757 # User-supplied AST transformers can reject an input by raising
2754 # an InputRejected. Short-circuit in this case so that we
2758 # an InputRejected. Short-circuit in this case so that we
2755 # don't unregister the transform.
2759 # don't unregister the transform.
2756 raise
2760 raise
2757 except Exception:
2761 except Exception:
2758 warn("AST transformer %r threw an error. It will be unregistered." % transformer)
2762 warn("AST transformer %r threw an error. It will be unregistered." % transformer)
2759 self.ast_transformers.remove(transformer)
2763 self.ast_transformers.remove(transformer)
2760
2764
2761 if self.ast_transformers:
2765 if self.ast_transformers:
2762 ast.fix_missing_locations(node)
2766 ast.fix_missing_locations(node)
2763 return node
2767 return node
2764
2768
2765
2769
2766 def run_ast_nodes(self, nodelist, cell_name, interactivity='last_expr',
2770 def run_ast_nodes(self, nodelist, cell_name, interactivity='last_expr',
2767 compiler=compile, result=None):
2771 compiler=compile, result=None):
2768 """Run a sequence of AST nodes. The execution mode depends on the
2772 """Run a sequence of AST nodes. The execution mode depends on the
2769 interactivity parameter.
2773 interactivity parameter.
2770
2774
2771 Parameters
2775 Parameters
2772 ----------
2776 ----------
2773 nodelist : list
2777 nodelist : list
2774 A sequence of AST nodes to run.
2778 A sequence of AST nodes to run.
2775 cell_name : str
2779 cell_name : str
2776 Will be passed to the compiler as the filename of the cell. Typically
2780 Will be passed to the compiler as the filename of the cell. Typically
2777 the value returned by ip.compile.cache(cell).
2781 the value returned by ip.compile.cache(cell).
2778 interactivity : str
2782 interactivity : str
2779 'all', 'last', 'last_expr' or 'none', specifying which nodes should be
2783 'all', 'last', 'last_expr' or 'none', specifying which nodes should be
2780 run interactively (displaying output from expressions). 'last_expr'
2784 run interactively (displaying output from expressions). 'last_expr'
2781 will run the last node interactively only if it is an expression (i.e.
2785 will run the last node interactively only if it is an expression (i.e.
2782 expressions in loops or other blocks are not displayed. Other values
2786 expressions in loops or other blocks are not displayed. Other values
2783 for this parameter will raise a ValueError.
2787 for this parameter will raise a ValueError.
2784 compiler : callable
2788 compiler : callable
2785 A function with the same interface as the built-in compile(), to turn
2789 A function with the same interface as the built-in compile(), to turn
2786 the AST nodes into code objects. Default is the built-in compile().
2790 the AST nodes into code objects. Default is the built-in compile().
2787 result : ExecutionResult, optional
2791 result : ExecutionResult, optional
2788 An object to store exceptions that occur during execution.
2792 An object to store exceptions that occur during execution.
2789
2793
2790 Returns
2794 Returns
2791 -------
2795 -------
2792 True if an exception occurred while running code, False if it finished
2796 True if an exception occurred while running code, False if it finished
2793 running.
2797 running.
2794 """
2798 """
2795 if not nodelist:
2799 if not nodelist:
2796 return
2800 return
2797
2801
2798 if interactivity == 'last_expr':
2802 if interactivity == 'last_expr':
2799 if isinstance(nodelist[-1], ast.Expr):
2803 if isinstance(nodelist[-1], ast.Expr):
2800 interactivity = "last"
2804 interactivity = "last"
2801 else:
2805 else:
2802 interactivity = "none"
2806 interactivity = "none"
2803
2807
2804 if interactivity == 'none':
2808 if interactivity == 'none':
2805 to_run_exec, to_run_interactive = nodelist, []
2809 to_run_exec, to_run_interactive = nodelist, []
2806 elif interactivity == 'last':
2810 elif interactivity == 'last':
2807 to_run_exec, to_run_interactive = nodelist[:-1], nodelist[-1:]
2811 to_run_exec, to_run_interactive = nodelist[:-1], nodelist[-1:]
2808 elif interactivity == 'all':
2812 elif interactivity == 'all':
2809 to_run_exec, to_run_interactive = [], nodelist
2813 to_run_exec, to_run_interactive = [], nodelist
2810 else:
2814 else:
2811 raise ValueError("Interactivity was %r" % interactivity)
2815 raise ValueError("Interactivity was %r" % interactivity)
2812
2816
2813 try:
2817 try:
2814 for i, node in enumerate(to_run_exec):
2818 for i, node in enumerate(to_run_exec):
2815 mod = ast.Module([node])
2819 mod = ast.Module([node])
2816 code = compiler(mod, cell_name, "exec")
2820 code = compiler(mod, cell_name, "exec")
2817 if self.run_code(code, result):
2821 if self.run_code(code, result):
2818 return True
2822 return True
2819
2823
2820 for i, node in enumerate(to_run_interactive):
2824 for i, node in enumerate(to_run_interactive):
2821 mod = ast.Interactive([node])
2825 mod = ast.Interactive([node])
2822 code = compiler(mod, cell_name, "single")
2826 code = compiler(mod, cell_name, "single")
2823 if self.run_code(code, result):
2827 if self.run_code(code, result):
2824 return True
2828 return True
2825
2829
2826 # Flush softspace
2830 # Flush softspace
2827 if softspace(sys.stdout, 0):
2831 if softspace(sys.stdout, 0):
2828 print()
2832 print()
2829
2833
2830 except:
2834 except:
2831 # It's possible to have exceptions raised here, typically by
2835 # It's possible to have exceptions raised here, typically by
2832 # compilation of odd code (such as a naked 'return' outside a
2836 # compilation of odd code (such as a naked 'return' outside a
2833 # function) that did parse but isn't valid. Typically the exception
2837 # function) that did parse but isn't valid. Typically the exception
2834 # is a SyntaxError, but it's safest just to catch anything and show
2838 # is a SyntaxError, but it's safest just to catch anything and show
2835 # the user a traceback.
2839 # the user a traceback.
2836
2840
2837 # We do only one try/except outside the loop to minimize the impact
2841 # We do only one try/except outside the loop to minimize the impact
2838 # on runtime, and also because if any node in the node list is
2842 # on runtime, and also because if any node in the node list is
2839 # broken, we should stop execution completely.
2843 # broken, we should stop execution completely.
2840 if result:
2844 if result:
2841 result.error_before_exec = sys.exc_info()[1]
2845 result.error_before_exec = sys.exc_info()[1]
2842 self.showtraceback()
2846 self.showtraceback()
2843 return True
2847 return True
2844
2848
2845 return False
2849 return False
2846
2850
2847 def run_code(self, code_obj, result=None):
2851 def run_code(self, code_obj, result=None):
2848 """Execute a code object.
2852 """Execute a code object.
2849
2853
2850 When an exception occurs, self.showtraceback() is called to display a
2854 When an exception occurs, self.showtraceback() is called to display a
2851 traceback.
2855 traceback.
2852
2856
2853 Parameters
2857 Parameters
2854 ----------
2858 ----------
2855 code_obj : code object
2859 code_obj : code object
2856 A compiled code object, to be executed
2860 A compiled code object, to be executed
2857 result : ExecutionResult, optional
2861 result : ExecutionResult, optional
2858 An object to store exceptions that occur during execution.
2862 An object to store exceptions that occur during execution.
2859
2863
2860 Returns
2864 Returns
2861 -------
2865 -------
2862 False : successful execution.
2866 False : successful execution.
2863 True : an error occurred.
2867 True : an error occurred.
2864 """
2868 """
2865 # Set our own excepthook in case the user code tries to call it
2869 # Set our own excepthook in case the user code tries to call it
2866 # directly, so that the IPython crash handler doesn't get triggered
2870 # directly, so that the IPython crash handler doesn't get triggered
2867 old_excepthook, sys.excepthook = sys.excepthook, self.excepthook
2871 old_excepthook, sys.excepthook = sys.excepthook, self.excepthook
2868
2872
2869 # we save the original sys.excepthook in the instance, in case config
2873 # we save the original sys.excepthook in the instance, in case config
2870 # code (such as magics) needs access to it.
2874 # code (such as magics) needs access to it.
2871 self.sys_excepthook = old_excepthook
2875 self.sys_excepthook = old_excepthook
2872 outflag = 1 # happens in more places, so it's easier as default
2876 outflag = 1 # happens in more places, so it's easier as default
2873 try:
2877 try:
2874 try:
2878 try:
2875 self.hooks.pre_run_code_hook()
2879 self.hooks.pre_run_code_hook()
2876 #rprint('Running code', repr(code_obj)) # dbg
2880 #rprint('Running code', repr(code_obj)) # dbg
2877 exec(code_obj, self.user_global_ns, self.user_ns)
2881 exec(code_obj, self.user_global_ns, self.user_ns)
2878 finally:
2882 finally:
2879 # Reset our crash handler in place
2883 # Reset our crash handler in place
2880 sys.excepthook = old_excepthook
2884 sys.excepthook = old_excepthook
2881 except SystemExit as e:
2885 except SystemExit as e:
2882 if result is not None:
2886 if result is not None:
2883 result.error_in_exec = e
2887 result.error_in_exec = e
2884 self.showtraceback(exception_only=True)
2888 self.showtraceback(exception_only=True)
2885 warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1)
2889 warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1)
2886 except self.custom_exceptions:
2890 except self.custom_exceptions:
2887 etype, value, tb = sys.exc_info()
2891 etype, value, tb = sys.exc_info()
2888 if result is not None:
2892 if result is not None:
2889 result.error_in_exec = value
2893 result.error_in_exec = value
2890 self.CustomTB(etype, value, tb)
2894 self.CustomTB(etype, value, tb)
2891 except:
2895 except:
2892 if result is not None:
2896 if result is not None:
2893 result.error_in_exec = sys.exc_info()[1]
2897 result.error_in_exec = sys.exc_info()[1]
2894 self.showtraceback()
2898 self.showtraceback()
2895 else:
2899 else:
2896 outflag = 0
2900 outflag = 0
2897 return outflag
2901 return outflag
2898
2902
2899 # For backwards compatibility
2903 # For backwards compatibility
2900 runcode = run_code
2904 runcode = run_code
2901
2905
2902 #-------------------------------------------------------------------------
2906 #-------------------------------------------------------------------------
2903 # Things related to GUI support and pylab
2907 # Things related to GUI support and pylab
2904 #-------------------------------------------------------------------------
2908 #-------------------------------------------------------------------------
2905
2909
2906 def enable_gui(self, gui=None):
2910 def enable_gui(self, gui=None):
2907 raise NotImplementedError('Implement enable_gui in a subclass')
2911 raise NotImplementedError('Implement enable_gui in a subclass')
2908
2912
2909 def enable_matplotlib(self, gui=None):
2913 def enable_matplotlib(self, gui=None):
2910 """Enable interactive matplotlib and inline figure support.
2914 """Enable interactive matplotlib and inline figure support.
2911
2915
2912 This takes the following steps:
2916 This takes the following steps:
2913
2917
2914 1. select the appropriate eventloop and matplotlib backend
2918 1. select the appropriate eventloop and matplotlib backend
2915 2. set up matplotlib for interactive use with that backend
2919 2. set up matplotlib for interactive use with that backend
2916 3. configure formatters for inline figure display
2920 3. configure formatters for inline figure display
2917 4. enable the selected gui eventloop
2921 4. enable the selected gui eventloop
2918
2922
2919 Parameters
2923 Parameters
2920 ----------
2924 ----------
2921 gui : optional, string
2925 gui : optional, string
2922 If given, dictates the choice of matplotlib GUI backend to use
2926 If given, dictates the choice of matplotlib GUI backend to use
2923 (should be one of IPython's supported backends, 'qt', 'osx', 'tk',
2927 (should be one of IPython's supported backends, 'qt', 'osx', 'tk',
2924 'gtk', 'wx' or 'inline'), otherwise we use the default chosen by
2928 'gtk', 'wx' or 'inline'), otherwise we use the default chosen by
2925 matplotlib (as dictated by the matplotlib build-time options plus the
2929 matplotlib (as dictated by the matplotlib build-time options plus the
2926 user's matplotlibrc configuration file). Note that not all backends
2930 user's matplotlibrc configuration file). Note that not all backends
2927 make sense in all contexts, for example a terminal ipython can't
2931 make sense in all contexts, for example a terminal ipython can't
2928 display figures inline.
2932 display figures inline.
2929 """
2933 """
2930 from IPython.core import pylabtools as pt
2934 from IPython.core import pylabtools as pt
2931 gui, backend = pt.find_gui_and_backend(gui, self.pylab_gui_select)
2935 gui, backend = pt.find_gui_and_backend(gui, self.pylab_gui_select)
2932
2936
2933 if gui != 'inline':
2937 if gui != 'inline':
2934 # If we have our first gui selection, store it
2938 # If we have our first gui selection, store it
2935 if self.pylab_gui_select is None:
2939 if self.pylab_gui_select is None:
2936 self.pylab_gui_select = gui
2940 self.pylab_gui_select = gui
2937 # Otherwise if they are different
2941 # Otherwise if they are different
2938 elif gui != self.pylab_gui_select:
2942 elif gui != self.pylab_gui_select:
2939 print ('Warning: Cannot change to a different GUI toolkit: %s.'
2943 print ('Warning: Cannot change to a different GUI toolkit: %s.'
2940 ' Using %s instead.' % (gui, self.pylab_gui_select))
2944 ' Using %s instead.' % (gui, self.pylab_gui_select))
2941 gui, backend = pt.find_gui_and_backend(self.pylab_gui_select)
2945 gui, backend = pt.find_gui_and_backend(self.pylab_gui_select)
2942
2946
2943 pt.activate_matplotlib(backend)
2947 pt.activate_matplotlib(backend)
2944 pt.configure_inline_support(self, backend)
2948 pt.configure_inline_support(self, backend)
2945
2949
2946 # Now we must activate the gui pylab wants to use, and fix %run to take
2950 # Now we must activate the gui pylab wants to use, and fix %run to take
2947 # plot updates into account
2951 # plot updates into account
2948 self.enable_gui(gui)
2952 self.enable_gui(gui)
2949 self.magics_manager.registry['ExecutionMagics'].default_runner = \
2953 self.magics_manager.registry['ExecutionMagics'].default_runner = \
2950 pt.mpl_runner(self.safe_execfile)
2954 pt.mpl_runner(self.safe_execfile)
2951
2955
2952 return gui, backend
2956 return gui, backend
2953
2957
2954 def enable_pylab(self, gui=None, import_all=True, welcome_message=False):
2958 def enable_pylab(self, gui=None, import_all=True, welcome_message=False):
2955 """Activate pylab support at runtime.
2959 """Activate pylab support at runtime.
2956
2960
2957 This turns on support for matplotlib, preloads into the interactive
2961 This turns on support for matplotlib, preloads into the interactive
2958 namespace all of numpy and pylab, and configures IPython to correctly
2962 namespace all of numpy and pylab, and configures IPython to correctly
2959 interact with the GUI event loop. The GUI backend to be used can be
2963 interact with the GUI event loop. The GUI backend to be used can be
2960 optionally selected with the optional ``gui`` argument.
2964 optionally selected with the optional ``gui`` argument.
2961
2965
2962 This method only adds preloading the namespace to InteractiveShell.enable_matplotlib.
2966 This method only adds preloading the namespace to InteractiveShell.enable_matplotlib.
2963
2967
2964 Parameters
2968 Parameters
2965 ----------
2969 ----------
2966 gui : optional, string
2970 gui : optional, string
2967 If given, dictates the choice of matplotlib GUI backend to use
2971 If given, dictates the choice of matplotlib GUI backend to use
2968 (should be one of IPython's supported backends, 'qt', 'osx', 'tk',
2972 (should be one of IPython's supported backends, 'qt', 'osx', 'tk',
2969 'gtk', 'wx' or 'inline'), otherwise we use the default chosen by
2973 'gtk', 'wx' or 'inline'), otherwise we use the default chosen by
2970 matplotlib (as dictated by the matplotlib build-time options plus the
2974 matplotlib (as dictated by the matplotlib build-time options plus the
2971 user's matplotlibrc configuration file). Note that not all backends
2975 user's matplotlibrc configuration file). Note that not all backends
2972 make sense in all contexts, for example a terminal ipython can't
2976 make sense in all contexts, for example a terminal ipython can't
2973 display figures inline.
2977 display figures inline.
2974 import_all : optional, bool, default: True
2978 import_all : optional, bool, default: True
2975 Whether to do `from numpy import *` and `from pylab import *`
2979 Whether to do `from numpy import *` and `from pylab import *`
2976 in addition to module imports.
2980 in addition to module imports.
2977 welcome_message : deprecated
2981 welcome_message : deprecated
2978 This argument is ignored, no welcome message will be displayed.
2982 This argument is ignored, no welcome message will be displayed.
2979 """
2983 """
2980 from IPython.core.pylabtools import import_pylab
2984 from IPython.core.pylabtools import import_pylab
2981
2985
2982 gui, backend = self.enable_matplotlib(gui)
2986 gui, backend = self.enable_matplotlib(gui)
2983
2987
2984 # We want to prevent the loading of pylab to pollute the user's
2988 # We want to prevent the loading of pylab to pollute the user's
2985 # namespace as shown by the %who* magics, so we execute the activation
2989 # namespace as shown by the %who* magics, so we execute the activation
2986 # code in an empty namespace, and we update *both* user_ns and
2990 # code in an empty namespace, and we update *both* user_ns and
2987 # user_ns_hidden with this information.
2991 # user_ns_hidden with this information.
2988 ns = {}
2992 ns = {}
2989 import_pylab(ns, import_all)
2993 import_pylab(ns, import_all)
2990 # warn about clobbered names
2994 # warn about clobbered names
2991 ignored = {"__builtins__"}
2995 ignored = {"__builtins__"}
2992 both = set(ns).intersection(self.user_ns).difference(ignored)
2996 both = set(ns).intersection(self.user_ns).difference(ignored)
2993 clobbered = [ name for name in both if self.user_ns[name] is not ns[name] ]
2997 clobbered = [ name for name in both if self.user_ns[name] is not ns[name] ]
2994 self.user_ns.update(ns)
2998 self.user_ns.update(ns)
2995 self.user_ns_hidden.update(ns)
2999 self.user_ns_hidden.update(ns)
2996 return gui, backend, clobbered
3000 return gui, backend, clobbered
2997
3001
2998 #-------------------------------------------------------------------------
3002 #-------------------------------------------------------------------------
2999 # Utilities
3003 # Utilities
3000 #-------------------------------------------------------------------------
3004 #-------------------------------------------------------------------------
3001
3005
3002 def var_expand(self, cmd, depth=0, formatter=DollarFormatter()):
3006 def var_expand(self, cmd, depth=0, formatter=DollarFormatter()):
3003 """Expand python variables in a string.
3007 """Expand python variables in a string.
3004
3008
3005 The depth argument indicates how many frames above the caller should
3009 The depth argument indicates how many frames above the caller should
3006 be walked to look for the local namespace where to expand variables.
3010 be walked to look for the local namespace where to expand variables.
3007
3011
3008 The global namespace for expansion is always the user's interactive
3012 The global namespace for expansion is always the user's interactive
3009 namespace.
3013 namespace.
3010 """
3014 """
3011 ns = self.user_ns.copy()
3015 ns = self.user_ns.copy()
3012 try:
3016 try:
3013 frame = sys._getframe(depth+1)
3017 frame = sys._getframe(depth+1)
3014 except ValueError:
3018 except ValueError:
3015 # This is thrown if there aren't that many frames on the stack,
3019 # This is thrown if there aren't that many frames on the stack,
3016 # e.g. if a script called run_line_magic() directly.
3020 # e.g. if a script called run_line_magic() directly.
3017 pass
3021 pass
3018 else:
3022 else:
3019 ns.update(frame.f_locals)
3023 ns.update(frame.f_locals)
3020
3024
3021 try:
3025 try:
3022 # We have to use .vformat() here, because 'self' is a valid and common
3026 # We have to use .vformat() here, because 'self' is a valid and common
3023 # name, and expanding **ns for .format() would make it collide with
3027 # name, and expanding **ns for .format() would make it collide with
3024 # the 'self' argument of the method.
3028 # the 'self' argument of the method.
3025 cmd = formatter.vformat(cmd, args=[], kwargs=ns)
3029 cmd = formatter.vformat(cmd, args=[], kwargs=ns)
3026 except Exception:
3030 except Exception:
3027 # if formatter couldn't format, just let it go untransformed
3031 # if formatter couldn't format, just let it go untransformed
3028 pass
3032 pass
3029 return cmd
3033 return cmd
3030
3034
3031 def mktempfile(self, data=None, prefix='ipython_edit_'):
3035 def mktempfile(self, data=None, prefix='ipython_edit_'):
3032 """Make a new tempfile and return its filename.
3036 """Make a new tempfile and return its filename.
3033
3037
3034 This makes a call to tempfile.mkstemp (created in a tempfile.mkdtemp),
3038 This makes a call to tempfile.mkstemp (created in a tempfile.mkdtemp),
3035 but it registers the created filename internally so ipython cleans it up
3039 but it registers the created filename internally so ipython cleans it up
3036 at exit time.
3040 at exit time.
3037
3041
3038 Optional inputs:
3042 Optional inputs:
3039
3043
3040 - data(None): if data is given, it gets written out to the temp file
3044 - data(None): if data is given, it gets written out to the temp file
3041 immediately, and the file is closed again."""
3045 immediately, and the file is closed again."""
3042
3046
3043 dirname = tempfile.mkdtemp(prefix=prefix)
3047 dirname = tempfile.mkdtemp(prefix=prefix)
3044 self.tempdirs.append(dirname)
3048 self.tempdirs.append(dirname)
3045
3049
3046 handle, filename = tempfile.mkstemp('.py', prefix, dir=dirname)
3050 handle, filename = tempfile.mkstemp('.py', prefix, dir=dirname)
3047 os.close(handle) # On Windows, there can only be one open handle on a file
3051 os.close(handle) # On Windows, there can only be one open handle on a file
3048 self.tempfiles.append(filename)
3052 self.tempfiles.append(filename)
3049
3053
3050 if data:
3054 if data:
3051 tmp_file = open(filename,'w')
3055 tmp_file = open(filename,'w')
3052 tmp_file.write(data)
3056 tmp_file.write(data)
3053 tmp_file.close()
3057 tmp_file.close()
3054 return filename
3058 return filename
3055
3059
3056 @undoc
3060 @undoc
3057 def write(self,data):
3061 def write(self,data):
3058 """DEPRECATED: Write a string to the default output"""
3062 """DEPRECATED: Write a string to the default output"""
3059 warn('InteractiveShell.write() is deprecated, use sys.stdout instead',
3063 warn('InteractiveShell.write() is deprecated, use sys.stdout instead',
3060 DeprecationWarning, stacklevel=2)
3064 DeprecationWarning, stacklevel=2)
3061 sys.stdout.write(data)
3065 sys.stdout.write(data)
3062
3066
3063 @undoc
3067 @undoc
3064 def write_err(self,data):
3068 def write_err(self,data):
3065 """DEPRECATED: Write a string to the default error output"""
3069 """DEPRECATED: Write a string to the default error output"""
3066 warn('InteractiveShell.write_err() is deprecated, use sys.stderr instead',
3070 warn('InteractiveShell.write_err() is deprecated, use sys.stderr instead',
3067 DeprecationWarning, stacklevel=2)
3071 DeprecationWarning, stacklevel=2)
3068 sys.stderr.write(data)
3072 sys.stderr.write(data)
3069
3073
3070 def ask_yes_no(self, prompt, default=None, interrupt=None):
3074 def ask_yes_no(self, prompt, default=None, interrupt=None):
3071 if self.quiet:
3075 if self.quiet:
3072 return True
3076 return True
3073 return ask_yes_no(prompt,default,interrupt)
3077 return ask_yes_no(prompt,default,interrupt)
3074
3078
3075 def show_usage(self):
3079 def show_usage(self):
3076 """Show a usage message"""
3080 """Show a usage message"""
3077 page.page(IPython.core.usage.interactive_usage)
3081 page.page(IPython.core.usage.interactive_usage)
3078
3082
3079 def extract_input_lines(self, range_str, raw=False):
3083 def extract_input_lines(self, range_str, raw=False):
3080 """Return as a string a set of input history slices.
3084 """Return as a string a set of input history slices.
3081
3085
3082 Parameters
3086 Parameters
3083 ----------
3087 ----------
3084 range_str : string
3088 range_str : string
3085 The set of slices is given as a string, like "~5/6-~4/2 4:8 9",
3089 The set of slices is given as a string, like "~5/6-~4/2 4:8 9",
3086 since this function is for use by magic functions which get their
3090 since this function is for use by magic functions which get their
3087 arguments as strings. The number before the / is the session
3091 arguments as strings. The number before the / is the session
3088 number: ~n goes n back from the current session.
3092 number: ~n goes n back from the current session.
3089
3093
3090 raw : bool, optional
3094 raw : bool, optional
3091 By default, the processed input is used. If this is true, the raw
3095 By default, the processed input is used. If this is true, the raw
3092 input history is used instead.
3096 input history is used instead.
3093
3097
3094 Notes
3098 Notes
3095 -----
3099 -----
3096
3100
3097 Slices can be described with two notations:
3101 Slices can be described with two notations:
3098
3102
3099 * ``N:M`` -> standard python form, means including items N...(M-1).
3103 * ``N:M`` -> standard python form, means including items N...(M-1).
3100 * ``N-M`` -> include items N..M (closed endpoint).
3104 * ``N-M`` -> include items N..M (closed endpoint).
3101 """
3105 """
3102 lines = self.history_manager.get_range_by_str(range_str, raw=raw)
3106 lines = self.history_manager.get_range_by_str(range_str, raw=raw)
3103 return "\n".join(x for _, _, x in lines)
3107 return "\n".join(x for _, _, x in lines)
3104
3108
3105 def find_user_code(self, target, raw=True, py_only=False, skip_encoding_cookie=True, search_ns=False):
3109 def find_user_code(self, target, raw=True, py_only=False, skip_encoding_cookie=True, search_ns=False):
3106 """Get a code string from history, file, url, or a string or macro.
3110 """Get a code string from history, file, url, or a string or macro.
3107
3111
3108 This is mainly used by magic functions.
3112 This is mainly used by magic functions.
3109
3113
3110 Parameters
3114 Parameters
3111 ----------
3115 ----------
3112
3116
3113 target : str
3117 target : str
3114
3118
3115 A string specifying code to retrieve. This will be tried respectively
3119 A string specifying code to retrieve. This will be tried respectively
3116 as: ranges of input history (see %history for syntax), url,
3120 as: ranges of input history (see %history for syntax), url,
3117 corresponding .py file, filename, or an expression evaluating to a
3121 corresponding .py file, filename, or an expression evaluating to a
3118 string or Macro in the user namespace.
3122 string or Macro in the user namespace.
3119
3123
3120 raw : bool
3124 raw : bool
3121 If true (default), retrieve raw history. Has no effect on the other
3125 If true (default), retrieve raw history. Has no effect on the other
3122 retrieval mechanisms.
3126 retrieval mechanisms.
3123
3127
3124 py_only : bool (default False)
3128 py_only : bool (default False)
3125 Only try to fetch python code, do not try alternative methods to decode file
3129 Only try to fetch python code, do not try alternative methods to decode file
3126 if unicode fails.
3130 if unicode fails.
3127
3131
3128 Returns
3132 Returns
3129 -------
3133 -------
3130 A string of code.
3134 A string of code.
3131
3135
3132 ValueError is raised if nothing is found, and TypeError if it evaluates
3136 ValueError is raised if nothing is found, and TypeError if it evaluates
3133 to an object of another type. In each case, .args[0] is a printable
3137 to an object of another type. In each case, .args[0] is a printable
3134 message.
3138 message.
3135 """
3139 """
3136 code = self.extract_input_lines(target, raw=raw) # Grab history
3140 code = self.extract_input_lines(target, raw=raw) # Grab history
3137 if code:
3141 if code:
3138 return code
3142 return code
3139 try:
3143 try:
3140 if target.startswith(('http://', 'https://')):
3144 if target.startswith(('http://', 'https://')):
3141 return openpy.read_py_url(target, skip_encoding_cookie=skip_encoding_cookie)
3145 return openpy.read_py_url(target, skip_encoding_cookie=skip_encoding_cookie)
3142 except UnicodeDecodeError:
3146 except UnicodeDecodeError:
3143 if not py_only :
3147 if not py_only :
3144 # Deferred import
3148 # Deferred import
3145 try:
3149 try:
3146 from urllib.request import urlopen # Py3
3150 from urllib.request import urlopen # Py3
3147 except ImportError:
3151 except ImportError:
3148 from urllib import urlopen
3152 from urllib import urlopen
3149 response = urlopen(target)
3153 response = urlopen(target)
3150 return response.read().decode('latin1')
3154 return response.read().decode('latin1')
3151 raise ValueError(("'%s' seem to be unreadable.") % target)
3155 raise ValueError(("'%s' seem to be unreadable.") % target)
3152
3156
3153 potential_target = [target]
3157 potential_target = [target]
3154 try :
3158 try :
3155 potential_target.insert(0,get_py_filename(target))
3159 potential_target.insert(0,get_py_filename(target))
3156 except IOError:
3160 except IOError:
3157 pass
3161 pass
3158
3162
3159 for tgt in potential_target :
3163 for tgt in potential_target :
3160 if os.path.isfile(tgt): # Read file
3164 if os.path.isfile(tgt): # Read file
3161 try :
3165 try :
3162 return openpy.read_py_file(tgt, skip_encoding_cookie=skip_encoding_cookie)
3166 return openpy.read_py_file(tgt, skip_encoding_cookie=skip_encoding_cookie)
3163 except UnicodeDecodeError :
3167 except UnicodeDecodeError :
3164 if not py_only :
3168 if not py_only :
3165 with io_open(tgt,'r', encoding='latin1') as f :
3169 with io_open(tgt,'r', encoding='latin1') as f :
3166 return f.read()
3170 return f.read()
3167 raise ValueError(("'%s' seem to be unreadable.") % target)
3171 raise ValueError(("'%s' seem to be unreadable.") % target)
3168 elif os.path.isdir(os.path.expanduser(tgt)):
3172 elif os.path.isdir(os.path.expanduser(tgt)):
3169 raise ValueError("'%s' is a directory, not a regular file." % target)
3173 raise ValueError("'%s' is a directory, not a regular file." % target)
3170
3174
3171 if search_ns:
3175 if search_ns:
3172 # Inspect namespace to load object source
3176 # Inspect namespace to load object source
3173 object_info = self.object_inspect(target, detail_level=1)
3177 object_info = self.object_inspect(target, detail_level=1)
3174 if object_info['found'] and object_info['source']:
3178 if object_info['found'] and object_info['source']:
3175 return object_info['source']
3179 return object_info['source']
3176
3180
3177 try: # User namespace
3181 try: # User namespace
3178 codeobj = eval(target, self.user_ns)
3182 codeobj = eval(target, self.user_ns)
3179 except Exception:
3183 except Exception:
3180 raise ValueError(("'%s' was not found in history, as a file, url, "
3184 raise ValueError(("'%s' was not found in history, as a file, url, "
3181 "nor in the user namespace.") % target)
3185 "nor in the user namespace.") % target)
3182
3186
3183 if isinstance(codeobj, string_types):
3187 if isinstance(codeobj, string_types):
3184 return codeobj
3188 return codeobj
3185 elif isinstance(codeobj, Macro):
3189 elif isinstance(codeobj, Macro):
3186 return codeobj.value
3190 return codeobj.value
3187
3191
3188 raise TypeError("%s is neither a string nor a macro." % target,
3192 raise TypeError("%s is neither a string nor a macro." % target,
3189 codeobj)
3193 codeobj)
3190
3194
3191 #-------------------------------------------------------------------------
3195 #-------------------------------------------------------------------------
3192 # Things related to IPython exiting
3196 # Things related to IPython exiting
3193 #-------------------------------------------------------------------------
3197 #-------------------------------------------------------------------------
3194 def atexit_operations(self):
3198 def atexit_operations(self):
3195 """This will be executed at the time of exit.
3199 """This will be executed at the time of exit.
3196
3200
3197 Cleanup operations and saving of persistent data that is done
3201 Cleanup operations and saving of persistent data that is done
3198 unconditionally by IPython should be performed here.
3202 unconditionally by IPython should be performed here.
3199
3203
3200 For things that may depend on startup flags or platform specifics (such
3204 For things that may depend on startup flags or platform specifics (such
3201 as having readline or not), register a separate atexit function in the
3205 as having readline or not), register a separate atexit function in the
3202 code that has the appropriate information, rather than trying to
3206 code that has the appropriate information, rather than trying to
3203 clutter
3207 clutter
3204 """
3208 """
3205 # Close the history session (this stores the end time and line count)
3209 # Close the history session (this stores the end time and line count)
3206 # this must be *before* the tempfile cleanup, in case of temporary
3210 # this must be *before* the tempfile cleanup, in case of temporary
3207 # history db
3211 # history db
3208 self.history_manager.end_session()
3212 self.history_manager.end_session()
3209
3213
3210 # Cleanup all tempfiles and folders left around
3214 # Cleanup all tempfiles and folders left around
3211 for tfile in self.tempfiles:
3215 for tfile in self.tempfiles:
3212 try:
3216 try:
3213 os.unlink(tfile)
3217 os.unlink(tfile)
3214 except OSError:
3218 except OSError:
3215 pass
3219 pass
3216
3220
3217 for tdir in self.tempdirs:
3221 for tdir in self.tempdirs:
3218 try:
3222 try:
3219 os.rmdir(tdir)
3223 os.rmdir(tdir)
3220 except OSError:
3224 except OSError:
3221 pass
3225 pass
3222
3226
3223 # Clear all user namespaces to release all references cleanly.
3227 # Clear all user namespaces to release all references cleanly.
3224 self.reset(new_session=False)
3228 self.reset(new_session=False)
3225
3229
3226 # Run user hooks
3230 # Run user hooks
3227 self.hooks.shutdown_hook()
3231 self.hooks.shutdown_hook()
3228
3232
3229 def cleanup(self):
3233 def cleanup(self):
3230 self.restore_sys_module_state()
3234 self.restore_sys_module_state()
3231
3235
3232
3236
3233 # Overridden in terminal subclass to change prompts
3237 # Overridden in terminal subclass to change prompts
3234 def switch_doctest_mode(self, mode):
3238 def switch_doctest_mode(self, mode):
3235 pass
3239 pass
3236
3240
3237
3241
3238 class InteractiveShellABC(with_metaclass(abc.ABCMeta, object)):
3242 class InteractiveShellABC(with_metaclass(abc.ABCMeta, object)):
3239 """An abstract base class for InteractiveShell."""
3243 """An abstract base class for InteractiveShell."""
3240
3244
3241 InteractiveShellABC.register(InteractiveShell)
3245 InteractiveShellABC.register(InteractiveShell)
@@ -1,411 +1,411 b''
1 # encoding: utf-8
1 # encoding: utf-8
2 """
2 """
3 A mixin for :class:`~IPython.core.application.Application` classes that
3 A mixin for :class:`~IPython.core.application.Application` classes that
4 launch InteractiveShell instances, load extensions, etc.
4 launch InteractiveShell instances, load extensions, etc.
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 from __future__ import absolute_import
10 from __future__ import absolute_import
11 from __future__ import print_function
11 from __future__ import print_function
12
12
13 import glob
13 import glob
14 import os
14 import os
15 import sys
15 import sys
16
16
17 from traitlets.config.application import boolean_flag
17 from traitlets.config.application import boolean_flag
18 from traitlets.config.configurable import Configurable
18 from traitlets.config.configurable import Configurable
19 from traitlets.config.loader import Config
19 from traitlets.config.loader import Config
20 from IPython.core import pylabtools
20 from IPython.core import pylabtools
21 from IPython.utils import py3compat
21 from IPython.utils import py3compat
22 from IPython.utils.contexts import preserve_keys
22 from IPython.utils.contexts import preserve_keys
23 from IPython.utils.path import filefind
23 from IPython.utils.path import filefind
24 from traitlets import (
24 from traitlets import (
25 Unicode, Instance, List, Bool, CaselessStrEnum, observe,
25 Unicode, Instance, List, Bool, CaselessStrEnum, observe,
26 )
26 )
27 from IPython.lib.inputhook import guis
27 from IPython.terminal import pt_inputhooks
28
28
29 #-----------------------------------------------------------------------------
29 #-----------------------------------------------------------------------------
30 # Aliases and Flags
30 # Aliases and Flags
31 #-----------------------------------------------------------------------------
31 #-----------------------------------------------------------------------------
32
32
33 gui_keys = tuple(sorted([ key for key in guis if key is not None ]))
33 gui_keys = tuple(sorted(pt_inputhooks.backends) + sorted(pt_inputhooks.aliases))
34
34
35 backend_keys = sorted(pylabtools.backends.keys())
35 backend_keys = sorted(pylabtools.backends.keys())
36 backend_keys.insert(0, 'auto')
36 backend_keys.insert(0, 'auto')
37
37
38 shell_flags = {}
38 shell_flags = {}
39
39
40 addflag = lambda *args: shell_flags.update(boolean_flag(*args))
40 addflag = lambda *args: shell_flags.update(boolean_flag(*args))
41 addflag('autoindent', 'InteractiveShell.autoindent',
41 addflag('autoindent', 'InteractiveShell.autoindent',
42 'Turn on autoindenting.', 'Turn off autoindenting.'
42 'Turn on autoindenting.', 'Turn off autoindenting.'
43 )
43 )
44 addflag('automagic', 'InteractiveShell.automagic',
44 addflag('automagic', 'InteractiveShell.automagic',
45 """Turn on the auto calling of magic commands. Type %%magic at the
45 """Turn on the auto calling of magic commands. Type %%magic at the
46 IPython prompt for more information.""",
46 IPython prompt for more information.""",
47 'Turn off the auto calling of magic commands.'
47 'Turn off the auto calling of magic commands.'
48 )
48 )
49 addflag('pdb', 'InteractiveShell.pdb',
49 addflag('pdb', 'InteractiveShell.pdb',
50 "Enable auto calling the pdb debugger after every exception.",
50 "Enable auto calling the pdb debugger after every exception.",
51 "Disable auto calling the pdb debugger after every exception."
51 "Disable auto calling the pdb debugger after every exception."
52 )
52 )
53 addflag('pprint', 'PlainTextFormatter.pprint',
53 addflag('pprint', 'PlainTextFormatter.pprint',
54 "Enable auto pretty printing of results.",
54 "Enable auto pretty printing of results.",
55 "Disable auto pretty printing of results."
55 "Disable auto pretty printing of results."
56 )
56 )
57 addflag('color-info', 'InteractiveShell.color_info',
57 addflag('color-info', 'InteractiveShell.color_info',
58 """IPython can display information about objects via a set of functions,
58 """IPython can display information about objects via a set of functions,
59 and optionally can use colors for this, syntax highlighting
59 and optionally can use colors for this, syntax highlighting
60 source code and various other elements. This is on by default, but can cause
60 source code and various other elements. This is on by default, but can cause
61 problems with some pagers. If you see such problems, you can disable the
61 problems with some pagers. If you see such problems, you can disable the
62 colours.""",
62 colours.""",
63 "Disable using colors for info related things."
63 "Disable using colors for info related things."
64 )
64 )
65 nosep_config = Config()
65 nosep_config = Config()
66 nosep_config.InteractiveShell.separate_in = ''
66 nosep_config.InteractiveShell.separate_in = ''
67 nosep_config.InteractiveShell.separate_out = ''
67 nosep_config.InteractiveShell.separate_out = ''
68 nosep_config.InteractiveShell.separate_out2 = ''
68 nosep_config.InteractiveShell.separate_out2 = ''
69
69
70 shell_flags['nosep']=(nosep_config, "Eliminate all spacing between prompts.")
70 shell_flags['nosep']=(nosep_config, "Eliminate all spacing between prompts.")
71 shell_flags['pylab'] = (
71 shell_flags['pylab'] = (
72 {'InteractiveShellApp' : {'pylab' : 'auto'}},
72 {'InteractiveShellApp' : {'pylab' : 'auto'}},
73 """Pre-load matplotlib and numpy for interactive use with
73 """Pre-load matplotlib and numpy for interactive use with
74 the default matplotlib backend."""
74 the default matplotlib backend."""
75 )
75 )
76 shell_flags['matplotlib'] = (
76 shell_flags['matplotlib'] = (
77 {'InteractiveShellApp' : {'matplotlib' : 'auto'}},
77 {'InteractiveShellApp' : {'matplotlib' : 'auto'}},
78 """Configure matplotlib for interactive use with
78 """Configure matplotlib for interactive use with
79 the default matplotlib backend."""
79 the default matplotlib backend."""
80 )
80 )
81
81
82 # it's possible we don't want short aliases for *all* of these:
82 # it's possible we don't want short aliases for *all* of these:
83 shell_aliases = dict(
83 shell_aliases = dict(
84 autocall='InteractiveShell.autocall',
84 autocall='InteractiveShell.autocall',
85 colors='InteractiveShell.colors',
85 colors='InteractiveShell.colors',
86 logfile='InteractiveShell.logfile',
86 logfile='InteractiveShell.logfile',
87 logappend='InteractiveShell.logappend',
87 logappend='InteractiveShell.logappend',
88 c='InteractiveShellApp.code_to_run',
88 c='InteractiveShellApp.code_to_run',
89 m='InteractiveShellApp.module_to_run',
89 m='InteractiveShellApp.module_to_run',
90 ext='InteractiveShellApp.extra_extension',
90 ext='InteractiveShellApp.extra_extension',
91 gui='InteractiveShellApp.gui',
91 gui='InteractiveShellApp.gui',
92 pylab='InteractiveShellApp.pylab',
92 pylab='InteractiveShellApp.pylab',
93 matplotlib='InteractiveShellApp.matplotlib',
93 matplotlib='InteractiveShellApp.matplotlib',
94 )
94 )
95 shell_aliases['cache-size'] = 'InteractiveShell.cache_size'
95 shell_aliases['cache-size'] = 'InteractiveShell.cache_size'
96
96
97 #-----------------------------------------------------------------------------
97 #-----------------------------------------------------------------------------
98 # Main classes and functions
98 # Main classes and functions
99 #-----------------------------------------------------------------------------
99 #-----------------------------------------------------------------------------
100
100
101 class InteractiveShellApp(Configurable):
101 class InteractiveShellApp(Configurable):
102 """A Mixin for applications that start InteractiveShell instances.
102 """A Mixin for applications that start InteractiveShell instances.
103
103
104 Provides configurables for loading extensions and executing files
104 Provides configurables for loading extensions and executing files
105 as part of configuring a Shell environment.
105 as part of configuring a Shell environment.
106
106
107 The following methods should be called by the :meth:`initialize` method
107 The following methods should be called by the :meth:`initialize` method
108 of the subclass:
108 of the subclass:
109
109
110 - :meth:`init_path`
110 - :meth:`init_path`
111 - :meth:`init_shell` (to be implemented by the subclass)
111 - :meth:`init_shell` (to be implemented by the subclass)
112 - :meth:`init_gui_pylab`
112 - :meth:`init_gui_pylab`
113 - :meth:`init_extensions`
113 - :meth:`init_extensions`
114 - :meth:`init_code`
114 - :meth:`init_code`
115 """
115 """
116 extensions = List(Unicode(),
116 extensions = List(Unicode(),
117 help="A list of dotted module names of IPython extensions to load."
117 help="A list of dotted module names of IPython extensions to load."
118 ).tag(config=True)
118 ).tag(config=True)
119 extra_extension = Unicode('',
119 extra_extension = Unicode('',
120 help="dotted module name of an IPython extension to load."
120 help="dotted module name of an IPython extension to load."
121 ).tag(config=True)
121 ).tag(config=True)
122
122
123 reraise_ipython_extension_failures = Bool(False,
123 reraise_ipython_extension_failures = Bool(False,
124 help="Reraise exceptions encountered loading IPython extensions?",
124 help="Reraise exceptions encountered loading IPython extensions?",
125 ).tag(config=True)
125 ).tag(config=True)
126
126
127 # Extensions that are always loaded (not configurable)
127 # Extensions that are always loaded (not configurable)
128 default_extensions = List(Unicode(), [u'storemagic']).tag(config=False)
128 default_extensions = List(Unicode(), [u'storemagic']).tag(config=False)
129
129
130 hide_initial_ns = Bool(True,
130 hide_initial_ns = Bool(True,
131 help="""Should variables loaded at startup (by startup files, exec_lines, etc.)
131 help="""Should variables loaded at startup (by startup files, exec_lines, etc.)
132 be hidden from tools like %who?"""
132 be hidden from tools like %who?"""
133 ).tag(config=True)
133 ).tag(config=True)
134
134
135 exec_files = List(Unicode(),
135 exec_files = List(Unicode(),
136 help="""List of files to run at IPython startup."""
136 help="""List of files to run at IPython startup."""
137 ).tag(config=True)
137 ).tag(config=True)
138 exec_PYTHONSTARTUP = Bool(True,
138 exec_PYTHONSTARTUP = Bool(True,
139 help="""Run the file referenced by the PYTHONSTARTUP environment
139 help="""Run the file referenced by the PYTHONSTARTUP environment
140 variable at IPython startup."""
140 variable at IPython startup."""
141 ).tag(config=True)
141 ).tag(config=True)
142 file_to_run = Unicode('',
142 file_to_run = Unicode('',
143 help="""A file to be run""").tag(config=True)
143 help="""A file to be run""").tag(config=True)
144
144
145 exec_lines = List(Unicode(),
145 exec_lines = List(Unicode(),
146 help="""lines of code to run at IPython startup."""
146 help="""lines of code to run at IPython startup."""
147 ).tag(config=True)
147 ).tag(config=True)
148 code_to_run = Unicode('',
148 code_to_run = Unicode('',
149 help="Execute the given command string."
149 help="Execute the given command string."
150 ).tag(config=True)
150 ).tag(config=True)
151 module_to_run = Unicode('',
151 module_to_run = Unicode('',
152 help="Run the module as a script."
152 help="Run the module as a script."
153 ).tag(config=True)
153 ).tag(config=True)
154 gui = CaselessStrEnum(gui_keys, allow_none=True,
154 gui = CaselessStrEnum(gui_keys, allow_none=True,
155 help="Enable GUI event loop integration with any of {0}.".format(gui_keys)
155 help="Enable GUI event loop integration with any of {0}.".format(gui_keys)
156 ).tag(config=True)
156 ).tag(config=True)
157 matplotlib = CaselessStrEnum(backend_keys, allow_none=True,
157 matplotlib = CaselessStrEnum(backend_keys, allow_none=True,
158 help="""Configure matplotlib for interactive use with
158 help="""Configure matplotlib for interactive use with
159 the default matplotlib backend."""
159 the default matplotlib backend."""
160 ).tag(config=True)
160 ).tag(config=True)
161 pylab = CaselessStrEnum(backend_keys, allow_none=True,
161 pylab = CaselessStrEnum(backend_keys, allow_none=True,
162 help="""Pre-load matplotlib and numpy for interactive use,
162 help="""Pre-load matplotlib and numpy for interactive use,
163 selecting a particular matplotlib backend and loop integration.
163 selecting a particular matplotlib backend and loop integration.
164 """
164 """
165 ).tag(config=True)
165 ).tag(config=True)
166 pylab_import_all = Bool(True,
166 pylab_import_all = Bool(True,
167 help="""If true, IPython will populate the user namespace with numpy, pylab, etc.
167 help="""If true, IPython will populate the user namespace with numpy, pylab, etc.
168 and an ``import *`` is done from numpy and pylab, when using pylab mode.
168 and an ``import *`` is done from numpy and pylab, when using pylab mode.
169
169
170 When False, pylab mode should not import any names into the user namespace.
170 When False, pylab mode should not import any names into the user namespace.
171 """
171 """
172 ).tag(config=True)
172 ).tag(config=True)
173 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC',
173 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC',
174 allow_none=True)
174 allow_none=True)
175 # whether interact-loop should start
175 # whether interact-loop should start
176 interact = Bool(True)
176 interact = Bool(True)
177
177
178 user_ns = Instance(dict, args=None, allow_none=True)
178 user_ns = Instance(dict, args=None, allow_none=True)
179 @observe('user_ns')
179 @observe('user_ns')
180 def _user_ns_changed(self, change):
180 def _user_ns_changed(self, change):
181 if self.shell is not None:
181 if self.shell is not None:
182 self.shell.user_ns = change['new']
182 self.shell.user_ns = change['new']
183 self.shell.init_user_ns()
183 self.shell.init_user_ns()
184
184
185 def init_path(self):
185 def init_path(self):
186 """Add current working directory, '', to sys.path"""
186 """Add current working directory, '', to sys.path"""
187 if sys.path[0] != '':
187 if sys.path[0] != '':
188 sys.path.insert(0, '')
188 sys.path.insert(0, '')
189
189
190 def init_shell(self):
190 def init_shell(self):
191 raise NotImplementedError("Override in subclasses")
191 raise NotImplementedError("Override in subclasses")
192
192
193 def init_gui_pylab(self):
193 def init_gui_pylab(self):
194 """Enable GUI event loop integration, taking pylab into account."""
194 """Enable GUI event loop integration, taking pylab into account."""
195 enable = False
195 enable = False
196 shell = self.shell
196 shell = self.shell
197 if self.pylab:
197 if self.pylab:
198 enable = lambda key: shell.enable_pylab(key, import_all=self.pylab_import_all)
198 enable = lambda key: shell.enable_pylab(key, import_all=self.pylab_import_all)
199 key = self.pylab
199 key = self.pylab
200 elif self.matplotlib:
200 elif self.matplotlib:
201 enable = shell.enable_matplotlib
201 enable = shell.enable_matplotlib
202 key = self.matplotlib
202 key = self.matplotlib
203 elif self.gui:
203 elif self.gui:
204 enable = shell.enable_gui
204 enable = shell.enable_gui
205 key = self.gui
205 key = self.gui
206
206
207 if not enable:
207 if not enable:
208 return
208 return
209
209
210 try:
210 try:
211 r = enable(key)
211 r = enable(key)
212 except ImportError:
212 except ImportError:
213 self.log.warning("Eventloop or matplotlib integration failed. Is matplotlib installed?")
213 self.log.warning("Eventloop or matplotlib integration failed. Is matplotlib installed?")
214 self.shell.showtraceback()
214 self.shell.showtraceback()
215 return
215 return
216 except Exception:
216 except Exception:
217 self.log.warning("GUI event loop or pylab initialization failed")
217 self.log.warning("GUI event loop or pylab initialization failed")
218 self.shell.showtraceback()
218 self.shell.showtraceback()
219 return
219 return
220
220
221 if isinstance(r, tuple):
221 if isinstance(r, tuple):
222 gui, backend = r[:2]
222 gui, backend = r[:2]
223 self.log.info("Enabling GUI event loop integration, "
223 self.log.info("Enabling GUI event loop integration, "
224 "eventloop=%s, matplotlib=%s", gui, backend)
224 "eventloop=%s, matplotlib=%s", gui, backend)
225 if key == "auto":
225 if key == "auto":
226 print("Using matplotlib backend: %s" % backend)
226 print("Using matplotlib backend: %s" % backend)
227 else:
227 else:
228 gui = r
228 gui = r
229 self.log.info("Enabling GUI event loop integration, "
229 self.log.info("Enabling GUI event loop integration, "
230 "eventloop=%s", gui)
230 "eventloop=%s", gui)
231
231
232 def init_extensions(self):
232 def init_extensions(self):
233 """Load all IPython extensions in IPythonApp.extensions.
233 """Load all IPython extensions in IPythonApp.extensions.
234
234
235 This uses the :meth:`ExtensionManager.load_extensions` to load all
235 This uses the :meth:`ExtensionManager.load_extensions` to load all
236 the extensions listed in ``self.extensions``.
236 the extensions listed in ``self.extensions``.
237 """
237 """
238 try:
238 try:
239 self.log.debug("Loading IPython extensions...")
239 self.log.debug("Loading IPython extensions...")
240 extensions = self.default_extensions + self.extensions
240 extensions = self.default_extensions + self.extensions
241 if self.extra_extension:
241 if self.extra_extension:
242 extensions.append(self.extra_extension)
242 extensions.append(self.extra_extension)
243 for ext in extensions:
243 for ext in extensions:
244 try:
244 try:
245 self.log.info("Loading IPython extension: %s" % ext)
245 self.log.info("Loading IPython extension: %s" % ext)
246 self.shell.extension_manager.load_extension(ext)
246 self.shell.extension_manager.load_extension(ext)
247 except:
247 except:
248 if self.reraise_ipython_extension_failures:
248 if self.reraise_ipython_extension_failures:
249 raise
249 raise
250 msg = ("Error in loading extension: {ext}\n"
250 msg = ("Error in loading extension: {ext}\n"
251 "Check your config files in {location}".format(
251 "Check your config files in {location}".format(
252 ext=ext,
252 ext=ext,
253 location=self.profile_dir.location
253 location=self.profile_dir.location
254 ))
254 ))
255 self.log.warning(msg, exc_info=True)
255 self.log.warning(msg, exc_info=True)
256 except:
256 except:
257 if self.reraise_ipython_extension_failures:
257 if self.reraise_ipython_extension_failures:
258 raise
258 raise
259 self.log.warning("Unknown error in loading extensions:", exc_info=True)
259 self.log.warning("Unknown error in loading extensions:", exc_info=True)
260
260
261 def init_code(self):
261 def init_code(self):
262 """run the pre-flight code, specified via exec_lines"""
262 """run the pre-flight code, specified via exec_lines"""
263 self._run_startup_files()
263 self._run_startup_files()
264 self._run_exec_lines()
264 self._run_exec_lines()
265 self._run_exec_files()
265 self._run_exec_files()
266
266
267 # Hide variables defined here from %who etc.
267 # Hide variables defined here from %who etc.
268 if self.hide_initial_ns:
268 if self.hide_initial_ns:
269 self.shell.user_ns_hidden.update(self.shell.user_ns)
269 self.shell.user_ns_hidden.update(self.shell.user_ns)
270
270
271 # command-line execution (ipython -i script.py, ipython -m module)
271 # command-line execution (ipython -i script.py, ipython -m module)
272 # should *not* be excluded from %whos
272 # should *not* be excluded from %whos
273 self._run_cmd_line_code()
273 self._run_cmd_line_code()
274 self._run_module()
274 self._run_module()
275
275
276 # flush output, so itwon't be attached to the first cell
276 # flush output, so itwon't be attached to the first cell
277 sys.stdout.flush()
277 sys.stdout.flush()
278 sys.stderr.flush()
278 sys.stderr.flush()
279
279
280 def _run_exec_lines(self):
280 def _run_exec_lines(self):
281 """Run lines of code in IPythonApp.exec_lines in the user's namespace."""
281 """Run lines of code in IPythonApp.exec_lines in the user's namespace."""
282 if not self.exec_lines:
282 if not self.exec_lines:
283 return
283 return
284 try:
284 try:
285 self.log.debug("Running code from IPythonApp.exec_lines...")
285 self.log.debug("Running code from IPythonApp.exec_lines...")
286 for line in self.exec_lines:
286 for line in self.exec_lines:
287 try:
287 try:
288 self.log.info("Running code in user namespace: %s" %
288 self.log.info("Running code in user namespace: %s" %
289 line)
289 line)
290 self.shell.run_cell(line, store_history=False)
290 self.shell.run_cell(line, store_history=False)
291 except:
291 except:
292 self.log.warning("Error in executing line in user "
292 self.log.warning("Error in executing line in user "
293 "namespace: %s" % line)
293 "namespace: %s" % line)
294 self.shell.showtraceback()
294 self.shell.showtraceback()
295 except:
295 except:
296 self.log.warning("Unknown error in handling IPythonApp.exec_lines:")
296 self.log.warning("Unknown error in handling IPythonApp.exec_lines:")
297 self.shell.showtraceback()
297 self.shell.showtraceback()
298
298
299 def _exec_file(self, fname, shell_futures=False):
299 def _exec_file(self, fname, shell_futures=False):
300 try:
300 try:
301 full_filename = filefind(fname, [u'.', self.ipython_dir])
301 full_filename = filefind(fname, [u'.', self.ipython_dir])
302 except IOError:
302 except IOError:
303 self.log.warning("File not found: %r"%fname)
303 self.log.warning("File not found: %r"%fname)
304 return
304 return
305 # Make sure that the running script gets a proper sys.argv as if it
305 # Make sure that the running script gets a proper sys.argv as if it
306 # were run from a system shell.
306 # were run from a system shell.
307 save_argv = sys.argv
307 save_argv = sys.argv
308 sys.argv = [full_filename] + self.extra_args[1:]
308 sys.argv = [full_filename] + self.extra_args[1:]
309 # protect sys.argv from potential unicode strings on Python 2:
309 # protect sys.argv from potential unicode strings on Python 2:
310 if not py3compat.PY3:
310 if not py3compat.PY3:
311 sys.argv = [ py3compat.cast_bytes(a) for a in sys.argv ]
311 sys.argv = [ py3compat.cast_bytes(a) for a in sys.argv ]
312 try:
312 try:
313 if os.path.isfile(full_filename):
313 if os.path.isfile(full_filename):
314 self.log.info("Running file in user namespace: %s" %
314 self.log.info("Running file in user namespace: %s" %
315 full_filename)
315 full_filename)
316 # Ensure that __file__ is always defined to match Python
316 # Ensure that __file__ is always defined to match Python
317 # behavior.
317 # behavior.
318 with preserve_keys(self.shell.user_ns, '__file__'):
318 with preserve_keys(self.shell.user_ns, '__file__'):
319 self.shell.user_ns['__file__'] = fname
319 self.shell.user_ns['__file__'] = fname
320 if full_filename.endswith('.ipy'):
320 if full_filename.endswith('.ipy'):
321 self.shell.safe_execfile_ipy(full_filename,
321 self.shell.safe_execfile_ipy(full_filename,
322 shell_futures=shell_futures)
322 shell_futures=shell_futures)
323 else:
323 else:
324 # default to python, even without extension
324 # default to python, even without extension
325 self.shell.safe_execfile(full_filename,
325 self.shell.safe_execfile(full_filename,
326 self.shell.user_ns,
326 self.shell.user_ns,
327 shell_futures=shell_futures,
327 shell_futures=shell_futures,
328 raise_exceptions=True)
328 raise_exceptions=True)
329 finally:
329 finally:
330 sys.argv = save_argv
330 sys.argv = save_argv
331
331
332 def _run_startup_files(self):
332 def _run_startup_files(self):
333 """Run files from profile startup directory"""
333 """Run files from profile startup directory"""
334 startup_dir = self.profile_dir.startup_dir
334 startup_dir = self.profile_dir.startup_dir
335 startup_files = []
335 startup_files = []
336
336
337 if self.exec_PYTHONSTARTUP and os.environ.get('PYTHONSTARTUP', False) and \
337 if self.exec_PYTHONSTARTUP and os.environ.get('PYTHONSTARTUP', False) and \
338 not (self.file_to_run or self.code_to_run or self.module_to_run):
338 not (self.file_to_run or self.code_to_run or self.module_to_run):
339 python_startup = os.environ['PYTHONSTARTUP']
339 python_startup = os.environ['PYTHONSTARTUP']
340 self.log.debug("Running PYTHONSTARTUP file %s...", python_startup)
340 self.log.debug("Running PYTHONSTARTUP file %s...", python_startup)
341 try:
341 try:
342 self._exec_file(python_startup)
342 self._exec_file(python_startup)
343 except:
343 except:
344 self.log.warning("Unknown error in handling PYTHONSTARTUP file %s:", python_startup)
344 self.log.warning("Unknown error in handling PYTHONSTARTUP file %s:", python_startup)
345 self.shell.showtraceback()
345 self.shell.showtraceback()
346
346
347 startup_files += glob.glob(os.path.join(startup_dir, '*.py'))
347 startup_files += glob.glob(os.path.join(startup_dir, '*.py'))
348 startup_files += glob.glob(os.path.join(startup_dir, '*.ipy'))
348 startup_files += glob.glob(os.path.join(startup_dir, '*.ipy'))
349 if not startup_files:
349 if not startup_files:
350 return
350 return
351
351
352 self.log.debug("Running startup files from %s...", startup_dir)
352 self.log.debug("Running startup files from %s...", startup_dir)
353 try:
353 try:
354 for fname in sorted(startup_files):
354 for fname in sorted(startup_files):
355 self._exec_file(fname)
355 self._exec_file(fname)
356 except:
356 except:
357 self.log.warning("Unknown error in handling startup files:")
357 self.log.warning("Unknown error in handling startup files:")
358 self.shell.showtraceback()
358 self.shell.showtraceback()
359
359
360 def _run_exec_files(self):
360 def _run_exec_files(self):
361 """Run files from IPythonApp.exec_files"""
361 """Run files from IPythonApp.exec_files"""
362 if not self.exec_files:
362 if not self.exec_files:
363 return
363 return
364
364
365 self.log.debug("Running files in IPythonApp.exec_files...")
365 self.log.debug("Running files in IPythonApp.exec_files...")
366 try:
366 try:
367 for fname in self.exec_files:
367 for fname in self.exec_files:
368 self._exec_file(fname)
368 self._exec_file(fname)
369 except:
369 except:
370 self.log.warning("Unknown error in handling IPythonApp.exec_files:")
370 self.log.warning("Unknown error in handling IPythonApp.exec_files:")
371 self.shell.showtraceback()
371 self.shell.showtraceback()
372
372
373 def _run_cmd_line_code(self):
373 def _run_cmd_line_code(self):
374 """Run code or file specified at the command-line"""
374 """Run code or file specified at the command-line"""
375 if self.code_to_run:
375 if self.code_to_run:
376 line = self.code_to_run
376 line = self.code_to_run
377 try:
377 try:
378 self.log.info("Running code given at command line (c=): %s" %
378 self.log.info("Running code given at command line (c=): %s" %
379 line)
379 line)
380 self.shell.run_cell(line, store_history=False)
380 self.shell.run_cell(line, store_history=False)
381 except:
381 except:
382 self.log.warning("Error in executing line in user namespace: %s" %
382 self.log.warning("Error in executing line in user namespace: %s" %
383 line)
383 line)
384 self.shell.showtraceback()
384 self.shell.showtraceback()
385 if not self.interact:
385 if not self.interact:
386 self.exit(1)
386 self.exit(1)
387
387
388 # Like Python itself, ignore the second if the first of these is present
388 # Like Python itself, ignore the second if the first of these is present
389 elif self.file_to_run:
389 elif self.file_to_run:
390 fname = self.file_to_run
390 fname = self.file_to_run
391 if os.path.isdir(fname):
391 if os.path.isdir(fname):
392 fname = os.path.join(fname, "__main__.py")
392 fname = os.path.join(fname, "__main__.py")
393 try:
393 try:
394 self._exec_file(fname, shell_futures=True)
394 self._exec_file(fname, shell_futures=True)
395 except:
395 except:
396 self.shell.showtraceback(tb_offset=4)
396 self.shell.showtraceback(tb_offset=4)
397 if not self.interact:
397 if not self.interact:
398 self.exit(1)
398 self.exit(1)
399
399
400 def _run_module(self):
400 def _run_module(self):
401 """Run module specified at the command-line."""
401 """Run module specified at the command-line."""
402 if self.module_to_run:
402 if self.module_to_run:
403 # Make sure that the module gets a proper sys.argv as if it were
403 # Make sure that the module gets a proper sys.argv as if it were
404 # run using `python -m`.
404 # run using `python -m`.
405 save_argv = sys.argv
405 save_argv = sys.argv
406 sys.argv = [sys.executable] + self.extra_args
406 sys.argv = [sys.executable] + self.extra_args
407 try:
407 try:
408 self.shell.safe_run_module(self.module_to_run,
408 self.shell.safe_run_module(self.module_to_run,
409 self.shell.user_ns)
409 self.shell.user_ns)
410 finally:
410 finally:
411 sys.argv = save_argv
411 sys.argv = save_argv
@@ -1,233 +1,226 b''
1 """Tests for debugging machinery.
1 """Tests for debugging machinery.
2 """
2 """
3 from __future__ import print_function
3 from __future__ import print_function
4 #-----------------------------------------------------------------------------
5 # Copyright (c) 2012, The IPython Development Team.
6 #
7 # Distributed under the terms of the Modified BSD License.
8 #
9 # The full license is in the file COPYING.txt, distributed with this software.
10 #-----------------------------------------------------------------------------
11
4
12 #-----------------------------------------------------------------------------
5 # Copyright (c) IPython Development Team.
13 # Imports
6 # Distributed under the terms of the Modified BSD License.
14 #-----------------------------------------------------------------------------
15
7
16 import sys
8 import sys
9 import warnings
17
10
18 # third-party
19 import nose.tools as nt
11 import nose.tools as nt
20
12
21 # Our own
22 from IPython.core import debugger
13 from IPython.core import debugger
23
14
24 #-----------------------------------------------------------------------------
15 #-----------------------------------------------------------------------------
25 # Helper classes, from CPython's Pdb test suite
16 # Helper classes, from CPython's Pdb test suite
26 #-----------------------------------------------------------------------------
17 #-----------------------------------------------------------------------------
27
18
28 class _FakeInput(object):
19 class _FakeInput(object):
29 """
20 """
30 A fake input stream for pdb's interactive debugger. Whenever a
21 A fake input stream for pdb's interactive debugger. Whenever a
31 line is read, print it (to simulate the user typing it), and then
22 line is read, print it (to simulate the user typing it), and then
32 return it. The set of lines to return is specified in the
23 return it. The set of lines to return is specified in the
33 constructor; they should not have trailing newlines.
24 constructor; they should not have trailing newlines.
34 """
25 """
35 def __init__(self, lines):
26 def __init__(self, lines):
36 self.lines = iter(lines)
27 self.lines = iter(lines)
37
28
38 def readline(self):
29 def readline(self):
39 line = next(self.lines)
30 line = next(self.lines)
40 print(line)
31 print(line)
41 return line+'\n'
32 return line+'\n'
42
33
43 class PdbTestInput(object):
34 class PdbTestInput(object):
44 """Context manager that makes testing Pdb in doctests easier."""
35 """Context manager that makes testing Pdb in doctests easier."""
45
36
46 def __init__(self, input):
37 def __init__(self, input):
47 self.input = input
38 self.input = input
48
39
49 def __enter__(self):
40 def __enter__(self):
50 self.real_stdin = sys.stdin
41 self.real_stdin = sys.stdin
51 sys.stdin = _FakeInput(self.input)
42 sys.stdin = _FakeInput(self.input)
52
43
53 def __exit__(self, *exc):
44 def __exit__(self, *exc):
54 sys.stdin = self.real_stdin
45 sys.stdin = self.real_stdin
55
46
56 #-----------------------------------------------------------------------------
47 #-----------------------------------------------------------------------------
57 # Tests
48 # Tests
58 #-----------------------------------------------------------------------------
49 #-----------------------------------------------------------------------------
59
50
60 def test_longer_repr():
51 def test_longer_repr():
61 try:
52 try:
62 from reprlib import repr as trepr # Py 3
53 from reprlib import repr as trepr # Py 3
63 except ImportError:
54 except ImportError:
64 from repr import repr as trepr # Py 2
55 from repr import repr as trepr # Py 2
65
56
66 a = '1234567890'* 7
57 a = '1234567890'* 7
67 ar = "'1234567890123456789012345678901234567890123456789012345678901234567890'"
58 ar = "'1234567890123456789012345678901234567890123456789012345678901234567890'"
68 a_trunc = "'123456789012...8901234567890'"
59 a_trunc = "'123456789012...8901234567890'"
69 nt.assert_equal(trepr(a), a_trunc)
60 nt.assert_equal(trepr(a), a_trunc)
70 # The creation of our tracer modifies the repr module's repr function
61 # The creation of our tracer modifies the repr module's repr function
71 # in-place, since that global is used directly by the stdlib's pdb module.
62 # in-place, since that global is used directly by the stdlib's pdb module.
72 debugger.Tracer()
63 with warnings.catch_warnings():
64 warnings.simplefilter('ignore', DeprecationWarning)
65 debugger.Tracer()
73 nt.assert_equal(trepr(a), ar)
66 nt.assert_equal(trepr(a), ar)
74
67
75 def test_ipdb_magics():
68 def test_ipdb_magics():
76 '''Test calling some IPython magics from ipdb.
69 '''Test calling some IPython magics from ipdb.
77
70
78 First, set up some test functions and classes which we can inspect.
71 First, set up some test functions and classes which we can inspect.
79
72
80 >>> class ExampleClass(object):
73 >>> class ExampleClass(object):
81 ... """Docstring for ExampleClass."""
74 ... """Docstring for ExampleClass."""
82 ... def __init__(self):
75 ... def __init__(self):
83 ... """Docstring for ExampleClass.__init__"""
76 ... """Docstring for ExampleClass.__init__"""
84 ... pass
77 ... pass
85 ... def __str__(self):
78 ... def __str__(self):
86 ... return "ExampleClass()"
79 ... return "ExampleClass()"
87
80
88 >>> def example_function(x, y, z="hello"):
81 >>> def example_function(x, y, z="hello"):
89 ... """Docstring for example_function."""
82 ... """Docstring for example_function."""
90 ... pass
83 ... pass
91
84
92 >>> old_trace = sys.gettrace()
85 >>> old_trace = sys.gettrace()
93
86
94 Create a function which triggers ipdb.
87 Create a function which triggers ipdb.
95
88
96 >>> def trigger_ipdb():
89 >>> def trigger_ipdb():
97 ... a = ExampleClass()
90 ... a = ExampleClass()
98 ... debugger.Pdb().set_trace()
91 ... debugger.Pdb().set_trace()
99
92
100 >>> with PdbTestInput([
93 >>> with PdbTestInput([
101 ... 'pdef example_function',
94 ... 'pdef example_function',
102 ... 'pdoc ExampleClass',
95 ... 'pdoc ExampleClass',
103 ... 'up',
96 ... 'up',
104 ... 'down',
97 ... 'down',
105 ... 'list',
98 ... 'list',
106 ... 'pinfo a',
99 ... 'pinfo a',
107 ... 'll',
100 ... 'll',
108 ... 'continue',
101 ... 'continue',
109 ... ]):
102 ... ]):
110 ... trigger_ipdb()
103 ... trigger_ipdb()
111 --Return--
104 --Return--
112 None
105 None
113 > <doctest ...>(3)trigger_ipdb()
106 > <doctest ...>(3)trigger_ipdb()
114 1 def trigger_ipdb():
107 1 def trigger_ipdb():
115 2 a = ExampleClass()
108 2 a = ExampleClass()
116 ----> 3 debugger.Pdb().set_trace()
109 ----> 3 debugger.Pdb().set_trace()
117 <BLANKLINE>
110 <BLANKLINE>
118 ipdb> pdef example_function
111 ipdb> pdef example_function
119 example_function(x, y, z='hello')
112 example_function(x, y, z='hello')
120 ipdb> pdoc ExampleClass
113 ipdb> pdoc ExampleClass
121 Class docstring:
114 Class docstring:
122 Docstring for ExampleClass.
115 Docstring for ExampleClass.
123 Init docstring:
116 Init docstring:
124 Docstring for ExampleClass.__init__
117 Docstring for ExampleClass.__init__
125 ipdb> up
118 ipdb> up
126 > <doctest ...>(11)<module>()
119 > <doctest ...>(11)<module>()
127 7 'pinfo a',
120 7 'pinfo a',
128 8 'll',
121 8 'll',
129 9 'continue',
122 9 'continue',
130 10 ]):
123 10 ]):
131 ---> 11 trigger_ipdb()
124 ---> 11 trigger_ipdb()
132 <BLANKLINE>
125 <BLANKLINE>
133 ipdb> down
126 ipdb> down
134 None
127 None
135 > <doctest ...>(3)trigger_ipdb()
128 > <doctest ...>(3)trigger_ipdb()
136 1 def trigger_ipdb():
129 1 def trigger_ipdb():
137 2 a = ExampleClass()
130 2 a = ExampleClass()
138 ----> 3 debugger.Pdb().set_trace()
131 ----> 3 debugger.Pdb().set_trace()
139 <BLANKLINE>
132 <BLANKLINE>
140 ipdb> list
133 ipdb> list
141 1 def trigger_ipdb():
134 1 def trigger_ipdb():
142 2 a = ExampleClass()
135 2 a = ExampleClass()
143 ----> 3 debugger.Pdb().set_trace()
136 ----> 3 debugger.Pdb().set_trace()
144 <BLANKLINE>
137 <BLANKLINE>
145 ipdb> pinfo a
138 ipdb> pinfo a
146 Type: ExampleClass
139 Type: ExampleClass
147 String form: ExampleClass()
140 String form: ExampleClass()
148 Namespace: Local...
141 Namespace: Local...
149 Docstring: Docstring for ExampleClass.
142 Docstring: Docstring for ExampleClass.
150 Init docstring: Docstring for ExampleClass.__init__
143 Init docstring: Docstring for ExampleClass.__init__
151 ipdb> ll
144 ipdb> ll
152 1 def trigger_ipdb():
145 1 def trigger_ipdb():
153 2 a = ExampleClass()
146 2 a = ExampleClass()
154 ----> 3 debugger.Pdb().set_trace()
147 ----> 3 debugger.Pdb().set_trace()
155 <BLANKLINE>
148 <BLANKLINE>
156 ipdb> continue
149 ipdb> continue
157
150
158 Restore previous trace function, e.g. for coverage.py
151 Restore previous trace function, e.g. for coverage.py
159
152
160 >>> sys.settrace(old_trace)
153 >>> sys.settrace(old_trace)
161 '''
154 '''
162
155
163 def test_ipdb_magics2():
156 def test_ipdb_magics2():
164 '''Test ipdb with a very short function.
157 '''Test ipdb with a very short function.
165
158
166 >>> old_trace = sys.gettrace()
159 >>> old_trace = sys.gettrace()
167
160
168 >>> def bar():
161 >>> def bar():
169 ... pass
162 ... pass
170
163
171 Run ipdb.
164 Run ipdb.
172
165
173 >>> with PdbTestInput([
166 >>> with PdbTestInput([
174 ... 'continue',
167 ... 'continue',
175 ... ]):
168 ... ]):
176 ... debugger.Pdb().runcall(bar)
169 ... debugger.Pdb().runcall(bar)
177 > <doctest ...>(2)bar()
170 > <doctest ...>(2)bar()
178 1 def bar():
171 1 def bar():
179 ----> 2 pass
172 ----> 2 pass
180 <BLANKLINE>
173 <BLANKLINE>
181 ipdb> continue
174 ipdb> continue
182
175
183 Restore previous trace function, e.g. for coverage.py
176 Restore previous trace function, e.g. for coverage.py
184
177
185 >>> sys.settrace(old_trace)
178 >>> sys.settrace(old_trace)
186 '''
179 '''
187
180
188 def can_quit():
181 def can_quit():
189 '''Test that quit work in ipydb
182 '''Test that quit work in ipydb
190
183
191 >>> old_trace = sys.gettrace()
184 >>> old_trace = sys.gettrace()
192
185
193 >>> def bar():
186 >>> def bar():
194 ... pass
187 ... pass
195
188
196 >>> with PdbTestInput([
189 >>> with PdbTestInput([
197 ... 'quit',
190 ... 'quit',
198 ... ]):
191 ... ]):
199 ... debugger.Pdb().runcall(bar)
192 ... debugger.Pdb().runcall(bar)
200 > <doctest ...>(2)bar()
193 > <doctest ...>(2)bar()
201 1 def bar():
194 1 def bar():
202 ----> 2 pass
195 ----> 2 pass
203 <BLANKLINE>
196 <BLANKLINE>
204 ipdb> quit
197 ipdb> quit
205
198
206 Restore previous trace function, e.g. for coverage.py
199 Restore previous trace function, e.g. for coverage.py
207
200
208 >>> sys.settrace(old_trace)
201 >>> sys.settrace(old_trace)
209 '''
202 '''
210
203
211
204
212 def can_exit():
205 def can_exit():
213 '''Test that quit work in ipydb
206 '''Test that quit work in ipydb
214
207
215 >>> old_trace = sys.gettrace()
208 >>> old_trace = sys.gettrace()
216
209
217 >>> def bar():
210 >>> def bar():
218 ... pass
211 ... pass
219
212
220 >>> with PdbTestInput([
213 >>> with PdbTestInput([
221 ... 'exit',
214 ... 'exit',
222 ... ]):
215 ... ]):
223 ... debugger.Pdb().runcall(bar)
216 ... debugger.Pdb().runcall(bar)
224 > <doctest ...>(2)bar()
217 > <doctest ...>(2)bar()
225 1 def bar():
218 1 def bar():
226 ----> 2 pass
219 ----> 2 pass
227 <BLANKLINE>
220 <BLANKLINE>
228 ipdb> exit
221 ipdb> exit
229
222
230 Restore previous trace function, e.g. for coverage.py
223 Restore previous trace function, e.g. for coverage.py
231
224
232 >>> sys.settrace(old_trace)
225 >>> sys.settrace(old_trace)
233 '''
226 '''
@@ -1,1494 +1,1494 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """
2 """
3 Verbose and colourful traceback formatting.
3 Verbose and colourful traceback formatting.
4
4
5 **ColorTB**
5 **ColorTB**
6
6
7 I've always found it a bit hard to visually parse tracebacks in Python. The
7 I've always found it a bit hard to visually parse tracebacks in Python. The
8 ColorTB class is a solution to that problem. It colors the different parts of a
8 ColorTB class is a solution to that problem. It colors the different parts of a
9 traceback in a manner similar to what you would expect from a syntax-highlighting
9 traceback in a manner similar to what you would expect from a syntax-highlighting
10 text editor.
10 text editor.
11
11
12 Installation instructions for ColorTB::
12 Installation instructions for ColorTB::
13
13
14 import sys,ultratb
14 import sys,ultratb
15 sys.excepthook = ultratb.ColorTB()
15 sys.excepthook = ultratb.ColorTB()
16
16
17 **VerboseTB**
17 **VerboseTB**
18
18
19 I've also included a port of Ka-Ping Yee's "cgitb.py" that produces all kinds
19 I've also included a port of Ka-Ping Yee's "cgitb.py" that produces all kinds
20 of useful info when a traceback occurs. Ping originally had it spit out HTML
20 of useful info when a traceback occurs. Ping originally had it spit out HTML
21 and intended it for CGI programmers, but why should they have all the fun? I
21 and intended it for CGI programmers, but why should they have all the fun? I
22 altered it to spit out colored text to the terminal. It's a bit overwhelming,
22 altered it to spit out colored text to the terminal. It's a bit overwhelming,
23 but kind of neat, and maybe useful for long-running programs that you believe
23 but kind of neat, and maybe useful for long-running programs that you believe
24 are bug-free. If a crash *does* occur in that type of program you want details.
24 are bug-free. If a crash *does* occur in that type of program you want details.
25 Give it a shot--you'll love it or you'll hate it.
25 Give it a shot--you'll love it or you'll hate it.
26
26
27 .. note::
27 .. note::
28
28
29 The Verbose mode prints the variables currently visible where the exception
29 The Verbose mode prints the variables currently visible where the exception
30 happened (shortening their strings if too long). This can potentially be
30 happened (shortening their strings if too long). This can potentially be
31 very slow, if you happen to have a huge data structure whose string
31 very slow, if you happen to have a huge data structure whose string
32 representation is complex to compute. Your computer may appear to freeze for
32 representation is complex to compute. Your computer may appear to freeze for
33 a while with cpu usage at 100%. If this occurs, you can cancel the traceback
33 a while with cpu usage at 100%. If this occurs, you can cancel the traceback
34 with Ctrl-C (maybe hitting it more than once).
34 with Ctrl-C (maybe hitting it more than once).
35
35
36 If you encounter this kind of situation often, you may want to use the
36 If you encounter this kind of situation often, you may want to use the
37 Verbose_novars mode instead of the regular Verbose, which avoids formatting
37 Verbose_novars mode instead of the regular Verbose, which avoids formatting
38 variables (but otherwise includes the information and context given by
38 variables (but otherwise includes the information and context given by
39 Verbose).
39 Verbose).
40
40
41 .. note::
41 .. note::
42
42
43 The verbose mode print all variables in the stack, which means it can
43 The verbose mode print all variables in the stack, which means it can
44 potentially leak sensitive information like access keys, or unencryted
44 potentially leak sensitive information like access keys, or unencryted
45 password.
45 password.
46
46
47 Installation instructions for VerboseTB::
47 Installation instructions for VerboseTB::
48
48
49 import sys,ultratb
49 import sys,ultratb
50 sys.excepthook = ultratb.VerboseTB()
50 sys.excepthook = ultratb.VerboseTB()
51
51
52 Note: Much of the code in this module was lifted verbatim from the standard
52 Note: Much of the code in this module was lifted verbatim from the standard
53 library module 'traceback.py' and Ka-Ping Yee's 'cgitb.py'.
53 library module 'traceback.py' and Ka-Ping Yee's 'cgitb.py'.
54
54
55 Color schemes
55 Color schemes
56 -------------
56 -------------
57
57
58 The colors are defined in the class TBTools through the use of the
58 The colors are defined in the class TBTools through the use of the
59 ColorSchemeTable class. Currently the following exist:
59 ColorSchemeTable class. Currently the following exist:
60
60
61 - NoColor: allows all of this module to be used in any terminal (the color
61 - NoColor: allows all of this module to be used in any terminal (the color
62 escapes are just dummy blank strings).
62 escapes are just dummy blank strings).
63
63
64 - Linux: is meant to look good in a terminal like the Linux console (black
64 - Linux: is meant to look good in a terminal like the Linux console (black
65 or very dark background).
65 or very dark background).
66
66
67 - LightBG: similar to Linux but swaps dark/light colors to be more readable
67 - LightBG: similar to Linux but swaps dark/light colors to be more readable
68 in light background terminals.
68 in light background terminals.
69
69
70 - Neutral: a neutral color scheme that should be readable on both light and
70 - Neutral: a neutral color scheme that should be readable on both light and
71 dark background
71 dark background
72
72
73 You can implement other color schemes easily, the syntax is fairly
73 You can implement other color schemes easily, the syntax is fairly
74 self-explanatory. Please send back new schemes you develop to the author for
74 self-explanatory. Please send back new schemes you develop to the author for
75 possible inclusion in future releases.
75 possible inclusion in future releases.
76
76
77 Inheritance diagram:
77 Inheritance diagram:
78
78
79 .. inheritance-diagram:: IPython.core.ultratb
79 .. inheritance-diagram:: IPython.core.ultratb
80 :parts: 3
80 :parts: 3
81 """
81 """
82
82
83 #*****************************************************************************
83 #*****************************************************************************
84 # Copyright (C) 2001 Nathaniel Gray <n8gray@caltech.edu>
84 # Copyright (C) 2001 Nathaniel Gray <n8gray@caltech.edu>
85 # Copyright (C) 2001-2004 Fernando Perez <fperez@colorado.edu>
85 # Copyright (C) 2001-2004 Fernando Perez <fperez@colorado.edu>
86 #
86 #
87 # Distributed under the terms of the BSD License. The full license is in
87 # Distributed under the terms of the BSD License. The full license is in
88 # the file COPYING, distributed as part of this software.
88 # the file COPYING, distributed as part of this software.
89 #*****************************************************************************
89 #*****************************************************************************
90
90
91 from __future__ import absolute_import
91 from __future__ import absolute_import
92 from __future__ import unicode_literals
92 from __future__ import unicode_literals
93 from __future__ import print_function
93 from __future__ import print_function
94
94
95 import dis
95 import dis
96 import inspect
96 import inspect
97 import keyword
97 import keyword
98 import linecache
98 import linecache
99 import os
99 import os
100 import pydoc
100 import pydoc
101 import re
101 import re
102 import sys
102 import sys
103 import time
103 import time
104 import tokenize
104 import tokenize
105 import traceback
105 import traceback
106 import types
106 import types
107
107
108 try: # Python 2
108 try: # Python 2
109 generate_tokens = tokenize.generate_tokens
109 generate_tokens = tokenize.generate_tokens
110 except AttributeError: # Python 3
110 except AttributeError: # Python 3
111 generate_tokens = tokenize.tokenize
111 generate_tokens = tokenize.tokenize
112
112
113 # For purposes of monkeypatching inspect to fix a bug in it.
113 # For purposes of monkeypatching inspect to fix a bug in it.
114 from inspect import getsourcefile, getfile, getmodule, \
114 from inspect import getsourcefile, getfile, getmodule, \
115 ismodule, isclass, ismethod, isfunction, istraceback, isframe, iscode
115 ismodule, isclass, ismethod, isfunction, istraceback, isframe, iscode
116
116
117 # IPython's own modules
117 # IPython's own modules
118 from IPython import get_ipython
118 from IPython import get_ipython
119 from IPython.core import debugger
119 from IPython.core import debugger
120 from IPython.core.display_trap import DisplayTrap
120 from IPython.core.display_trap import DisplayTrap
121 from IPython.core.excolors import exception_colors
121 from IPython.core.excolors import exception_colors
122 from IPython.utils import PyColorize
122 from IPython.utils import PyColorize
123 from IPython.utils import openpy
123 from IPython.utils import openpy
124 from IPython.utils import path as util_path
124 from IPython.utils import path as util_path
125 from IPython.utils import py3compat
125 from IPython.utils import py3compat
126 from IPython.utils import ulinecache
126 from IPython.utils import ulinecache
127 from IPython.utils.data import uniq_stable
127 from IPython.utils.data import uniq_stable
128 from IPython.utils.terminal import get_terminal_size
128 from IPython.utils.terminal import get_terminal_size
129 from logging import info, error
129 from logging import info, error
130
130
131 import IPython.utils.colorable as colorable
131 import IPython.utils.colorable as colorable
132
132
133 # Globals
133 # Globals
134 # amount of space to put line numbers before verbose tracebacks
134 # amount of space to put line numbers before verbose tracebacks
135 INDENT_SIZE = 8
135 INDENT_SIZE = 8
136
136
137 # Default color scheme. This is used, for example, by the traceback
137 # Default color scheme. This is used, for example, by the traceback
138 # formatter. When running in an actual IPython instance, the user's rc.colors
138 # formatter. When running in an actual IPython instance, the user's rc.colors
139 # value is used, but having a module global makes this functionality available
139 # value is used, but having a module global makes this functionality available
140 # to users of ultratb who are NOT running inside ipython.
140 # to users of ultratb who are NOT running inside ipython.
141 DEFAULT_SCHEME = 'NoColor'
141 DEFAULT_SCHEME = 'NoColor'
142
142
143 # ---------------------------------------------------------------------------
143 # ---------------------------------------------------------------------------
144 # Code begins
144 # Code begins
145
145
146 # Utility functions
146 # Utility functions
147 def inspect_error():
147 def inspect_error():
148 """Print a message about internal inspect errors.
148 """Print a message about internal inspect errors.
149
149
150 These are unfortunately quite common."""
150 These are unfortunately quite common."""
151
151
152 error('Internal Python error in the inspect module.\n'
152 error('Internal Python error in the inspect module.\n'
153 'Below is the traceback from this internal error.\n')
153 'Below is the traceback from this internal error.\n')
154
154
155
155
156 # This function is a monkeypatch we apply to the Python inspect module. We have
156 # This function is a monkeypatch we apply to the Python inspect module. We have
157 # now found when it's needed (see discussion on issue gh-1456), and we have a
157 # now found when it's needed (see discussion on issue gh-1456), and we have a
158 # test case (IPython.core.tests.test_ultratb.ChangedPyFileTest) that fails if
158 # test case (IPython.core.tests.test_ultratb.ChangedPyFileTest) that fails if
159 # the monkeypatch is not applied. TK, Aug 2012.
159 # the monkeypatch is not applied. TK, Aug 2012.
160 def findsource(object):
160 def findsource(object):
161 """Return the entire source file and starting line number for an object.
161 """Return the entire source file and starting line number for an object.
162
162
163 The argument may be a module, class, method, function, traceback, frame,
163 The argument may be a module, class, method, function, traceback, frame,
164 or code object. The source code is returned as a list of all the lines
164 or code object. The source code is returned as a list of all the lines
165 in the file and the line number indexes a line in that list. An IOError
165 in the file and the line number indexes a line in that list. An IOError
166 is raised if the source code cannot be retrieved.
166 is raised if the source code cannot be retrieved.
167
167
168 FIXED version with which we monkeypatch the stdlib to work around a bug."""
168 FIXED version with which we monkeypatch the stdlib to work around a bug."""
169
169
170 file = getsourcefile(object) or getfile(object)
170 file = getsourcefile(object) or getfile(object)
171 # If the object is a frame, then trying to get the globals dict from its
171 # If the object is a frame, then trying to get the globals dict from its
172 # module won't work. Instead, the frame object itself has the globals
172 # module won't work. Instead, the frame object itself has the globals
173 # dictionary.
173 # dictionary.
174 globals_dict = None
174 globals_dict = None
175 if inspect.isframe(object):
175 if inspect.isframe(object):
176 # XXX: can this ever be false?
176 # XXX: can this ever be false?
177 globals_dict = object.f_globals
177 globals_dict = object.f_globals
178 else:
178 else:
179 module = getmodule(object, file)
179 module = getmodule(object, file)
180 if module:
180 if module:
181 globals_dict = module.__dict__
181 globals_dict = module.__dict__
182 lines = linecache.getlines(file, globals_dict)
182 lines = linecache.getlines(file, globals_dict)
183 if not lines:
183 if not lines:
184 raise IOError('could not get source code')
184 raise IOError('could not get source code')
185
185
186 if ismodule(object):
186 if ismodule(object):
187 return lines, 0
187 return lines, 0
188
188
189 if isclass(object):
189 if isclass(object):
190 name = object.__name__
190 name = object.__name__
191 pat = re.compile(r'^(\s*)class\s*' + name + r'\b')
191 pat = re.compile(r'^(\s*)class\s*' + name + r'\b')
192 # make some effort to find the best matching class definition:
192 # make some effort to find the best matching class definition:
193 # use the one with the least indentation, which is the one
193 # use the one with the least indentation, which is the one
194 # that's most probably not inside a function definition.
194 # that's most probably not inside a function definition.
195 candidates = []
195 candidates = []
196 for i in range(len(lines)):
196 for i in range(len(lines)):
197 match = pat.match(lines[i])
197 match = pat.match(lines[i])
198 if match:
198 if match:
199 # if it's at toplevel, it's already the best one
199 # if it's at toplevel, it's already the best one
200 if lines[i][0] == 'c':
200 if lines[i][0] == 'c':
201 return lines, i
201 return lines, i
202 # else add whitespace to candidate list
202 # else add whitespace to candidate list
203 candidates.append((match.group(1), i))
203 candidates.append((match.group(1), i))
204 if candidates:
204 if candidates:
205 # this will sort by whitespace, and by line number,
205 # this will sort by whitespace, and by line number,
206 # less whitespace first
206 # less whitespace first
207 candidates.sort()
207 candidates.sort()
208 return lines, candidates[0][1]
208 return lines, candidates[0][1]
209 else:
209 else:
210 raise IOError('could not find class definition')
210 raise IOError('could not find class definition')
211
211
212 if ismethod(object):
212 if ismethod(object):
213 object = object.__func__
213 object = object.__func__
214 if isfunction(object):
214 if isfunction(object):
215 object = object.__code__
215 object = object.__code__
216 if istraceback(object):
216 if istraceback(object):
217 object = object.tb_frame
217 object = object.tb_frame
218 if isframe(object):
218 if isframe(object):
219 object = object.f_code
219 object = object.f_code
220 if iscode(object):
220 if iscode(object):
221 if not hasattr(object, 'co_firstlineno'):
221 if not hasattr(object, 'co_firstlineno'):
222 raise IOError('could not find function definition')
222 raise IOError('could not find function definition')
223 pat = re.compile(r'^(\s*def\s)|(.*(?<!\w)lambda(:|\s))|^(\s*@)')
223 pat = re.compile(r'^(\s*def\s)|(.*(?<!\w)lambda(:|\s))|^(\s*@)')
224 pmatch = pat.match
224 pmatch = pat.match
225 # fperez - fix: sometimes, co_firstlineno can give a number larger than
225 # fperez - fix: sometimes, co_firstlineno can give a number larger than
226 # the length of lines, which causes an error. Safeguard against that.
226 # the length of lines, which causes an error. Safeguard against that.
227 lnum = min(object.co_firstlineno, len(lines)) - 1
227 lnum = min(object.co_firstlineno, len(lines)) - 1
228 while lnum > 0:
228 while lnum > 0:
229 if pmatch(lines[lnum]):
229 if pmatch(lines[lnum]):
230 break
230 break
231 lnum -= 1
231 lnum -= 1
232
232
233 return lines, lnum
233 return lines, lnum
234 raise IOError('could not find code object')
234 raise IOError('could not find code object')
235
235
236
236
237 # This is a patched version of inspect.getargs that applies the (unmerged)
237 # This is a patched version of inspect.getargs that applies the (unmerged)
238 # patch for http://bugs.python.org/issue14611 by Stefano Taschini. This fixes
238 # patch for http://bugs.python.org/issue14611 by Stefano Taschini. This fixes
239 # https://github.com/ipython/ipython/issues/8205 and
239 # https://github.com/ipython/ipython/issues/8205 and
240 # https://github.com/ipython/ipython/issues/8293
240 # https://github.com/ipython/ipython/issues/8293
241 def getargs(co):
241 def getargs(co):
242 """Get information about the arguments accepted by a code object.
242 """Get information about the arguments accepted by a code object.
243
243
244 Three things are returned: (args, varargs, varkw), where 'args' is
244 Three things are returned: (args, varargs, varkw), where 'args' is
245 a list of argument names (possibly containing nested lists), and
245 a list of argument names (possibly containing nested lists), and
246 'varargs' and 'varkw' are the names of the * and ** arguments or None."""
246 'varargs' and 'varkw' are the names of the * and ** arguments or None."""
247 if not iscode(co):
247 if not iscode(co):
248 raise TypeError('{!r} is not a code object'.format(co))
248 raise TypeError('{!r} is not a code object'.format(co))
249
249
250 nargs = co.co_argcount
250 nargs = co.co_argcount
251 names = co.co_varnames
251 names = co.co_varnames
252 args = list(names[:nargs])
252 args = list(names[:nargs])
253 step = 0
253 step = 0
254
254
255 # The following acrobatics are for anonymous (tuple) arguments.
255 # The following acrobatics are for anonymous (tuple) arguments.
256 for i in range(nargs):
256 for i in range(nargs):
257 if args[i][:1] in ('', '.'):
257 if args[i][:1] in ('', '.'):
258 stack, remain, count = [], [], []
258 stack, remain, count = [], [], []
259 while step < len(co.co_code):
259 while step < len(co.co_code):
260 op = ord(co.co_code[step])
260 op = ord(co.co_code[step])
261 step = step + 1
261 step = step + 1
262 if op >= dis.HAVE_ARGUMENT:
262 if op >= dis.HAVE_ARGUMENT:
263 opname = dis.opname[op]
263 opname = dis.opname[op]
264 value = ord(co.co_code[step]) + ord(co.co_code[step+1])*256
264 value = ord(co.co_code[step]) + ord(co.co_code[step+1])*256
265 step = step + 2
265 step = step + 2
266 if opname in ('UNPACK_TUPLE', 'UNPACK_SEQUENCE'):
266 if opname in ('UNPACK_TUPLE', 'UNPACK_SEQUENCE'):
267 remain.append(value)
267 remain.append(value)
268 count.append(value)
268 count.append(value)
269 elif opname in ('STORE_FAST', 'STORE_DEREF'):
269 elif opname in ('STORE_FAST', 'STORE_DEREF'):
270 if op in dis.haslocal:
270 if op in dis.haslocal:
271 stack.append(co.co_varnames[value])
271 stack.append(co.co_varnames[value])
272 elif op in dis.hasfree:
272 elif op in dis.hasfree:
273 stack.append((co.co_cellvars + co.co_freevars)[value])
273 stack.append((co.co_cellvars + co.co_freevars)[value])
274 # Special case for sublists of length 1: def foo((bar))
274 # Special case for sublists of length 1: def foo((bar))
275 # doesn't generate the UNPACK_TUPLE bytecode, so if
275 # doesn't generate the UNPACK_TUPLE bytecode, so if
276 # `remain` is empty here, we have such a sublist.
276 # `remain` is empty here, we have such a sublist.
277 if not remain:
277 if not remain:
278 stack[0] = [stack[0]]
278 stack[0] = [stack[0]]
279 break
279 break
280 else:
280 else:
281 remain[-1] = remain[-1] - 1
281 remain[-1] = remain[-1] - 1
282 while remain[-1] == 0:
282 while remain[-1] == 0:
283 remain.pop()
283 remain.pop()
284 size = count.pop()
284 size = count.pop()
285 stack[-size:] = [stack[-size:]]
285 stack[-size:] = [stack[-size:]]
286 if not remain:
286 if not remain:
287 break
287 break
288 remain[-1] = remain[-1] - 1
288 remain[-1] = remain[-1] - 1
289 if not remain:
289 if not remain:
290 break
290 break
291 args[i] = stack[0]
291 args[i] = stack[0]
292
292
293 varargs = None
293 varargs = None
294 if co.co_flags & inspect.CO_VARARGS:
294 if co.co_flags & inspect.CO_VARARGS:
295 varargs = co.co_varnames[nargs]
295 varargs = co.co_varnames[nargs]
296 nargs = nargs + 1
296 nargs = nargs + 1
297 varkw = None
297 varkw = None
298 if co.co_flags & inspect.CO_VARKEYWORDS:
298 if co.co_flags & inspect.CO_VARKEYWORDS:
299 varkw = co.co_varnames[nargs]
299 varkw = co.co_varnames[nargs]
300 return inspect.Arguments(args, varargs, varkw)
300 return inspect.Arguments(args, varargs, varkw)
301
301
302
302
303 # Monkeypatch inspect to apply our bugfix.
303 # Monkeypatch inspect to apply our bugfix.
304 def with_patch_inspect(f):
304 def with_patch_inspect(f):
305 """decorator for monkeypatching inspect.findsource"""
305 """decorator for monkeypatching inspect.findsource"""
306
306
307 def wrapped(*args, **kwargs):
307 def wrapped(*args, **kwargs):
308 save_findsource = inspect.findsource
308 save_findsource = inspect.findsource
309 save_getargs = inspect.getargs
309 save_getargs = inspect.getargs
310 inspect.findsource = findsource
310 inspect.findsource = findsource
311 inspect.getargs = getargs
311 inspect.getargs = getargs
312 try:
312 try:
313 return f(*args, **kwargs)
313 return f(*args, **kwargs)
314 finally:
314 finally:
315 inspect.findsource = save_findsource
315 inspect.findsource = save_findsource
316 inspect.getargs = save_getargs
316 inspect.getargs = save_getargs
317
317
318 return wrapped
318 return wrapped
319
319
320
320
321 if py3compat.PY3:
321 if py3compat.PY3:
322 fixed_getargvalues = inspect.getargvalues
322 fixed_getargvalues = inspect.getargvalues
323 else:
323 else:
324 # Fixes for https://github.com/ipython/ipython/issues/8293
324 # Fixes for https://github.com/ipython/ipython/issues/8293
325 # and https://github.com/ipython/ipython/issues/8205.
325 # and https://github.com/ipython/ipython/issues/8205.
326 # The relevant bug is caused by failure to correctly handle anonymous tuple
326 # The relevant bug is caused by failure to correctly handle anonymous tuple
327 # unpacking, which only exists in Python 2.
327 # unpacking, which only exists in Python 2.
328 fixed_getargvalues = with_patch_inspect(inspect.getargvalues)
328 fixed_getargvalues = with_patch_inspect(inspect.getargvalues)
329
329
330
330
331 def fix_frame_records_filenames(records):
331 def fix_frame_records_filenames(records):
332 """Try to fix the filenames in each record from inspect.getinnerframes().
332 """Try to fix the filenames in each record from inspect.getinnerframes().
333
333
334 Particularly, modules loaded from within zip files have useless filenames
334 Particularly, modules loaded from within zip files have useless filenames
335 attached to their code object, and inspect.getinnerframes() just uses it.
335 attached to their code object, and inspect.getinnerframes() just uses it.
336 """
336 """
337 fixed_records = []
337 fixed_records = []
338 for frame, filename, line_no, func_name, lines, index in records:
338 for frame, filename, line_no, func_name, lines, index in records:
339 # Look inside the frame's globals dictionary for __file__,
339 # Look inside the frame's globals dictionary for __file__,
340 # which should be better. However, keep Cython filenames since
340 # which should be better. However, keep Cython filenames since
341 # we prefer the source filenames over the compiled .so file.
341 # we prefer the source filenames over the compiled .so file.
342 filename = py3compat.cast_unicode_py2(filename, "utf-8")
342 filename = py3compat.cast_unicode_py2(filename, "utf-8")
343 if not filename.endswith(('.pyx', '.pxd', '.pxi')):
343 if not filename.endswith(('.pyx', '.pxd', '.pxi')):
344 better_fn = frame.f_globals.get('__file__', None)
344 better_fn = frame.f_globals.get('__file__', None)
345 if isinstance(better_fn, str):
345 if isinstance(better_fn, str):
346 # Check the type just in case someone did something weird with
346 # Check the type just in case someone did something weird with
347 # __file__. It might also be None if the error occurred during
347 # __file__. It might also be None if the error occurred during
348 # import.
348 # import.
349 filename = better_fn
349 filename = better_fn
350 fixed_records.append((frame, filename, line_no, func_name, lines, index))
350 fixed_records.append((frame, filename, line_no, func_name, lines, index))
351 return fixed_records
351 return fixed_records
352
352
353
353
354 @with_patch_inspect
354 @with_patch_inspect
355 def _fixed_getinnerframes(etb, context=1, tb_offset=0):
355 def _fixed_getinnerframes(etb, context=1, tb_offset=0):
356 LNUM_POS, LINES_POS, INDEX_POS = 2, 4, 5
356 LNUM_POS, LINES_POS, INDEX_POS = 2, 4, 5
357
357
358 records = fix_frame_records_filenames(inspect.getinnerframes(etb, context))
358 records = fix_frame_records_filenames(inspect.getinnerframes(etb, context))
359 # If the error is at the console, don't build any context, since it would
359 # If the error is at the console, don't build any context, since it would
360 # otherwise produce 5 blank lines printed out (there is no file at the
360 # otherwise produce 5 blank lines printed out (there is no file at the
361 # console)
361 # console)
362 rec_check = records[tb_offset:]
362 rec_check = records[tb_offset:]
363 try:
363 try:
364 rname = rec_check[0][1]
364 rname = rec_check[0][1]
365 if rname == '<ipython console>' or rname.endswith('<string>'):
365 if rname == '<ipython console>' or rname.endswith('<string>'):
366 return rec_check
366 return rec_check
367 except IndexError:
367 except IndexError:
368 pass
368 pass
369
369
370 aux = traceback.extract_tb(etb)
370 aux = traceback.extract_tb(etb)
371 assert len(records) == len(aux)
371 assert len(records) == len(aux)
372 for i, (file, lnum, _, _) in zip(range(len(records)), aux):
372 for i, (file, lnum, _, _) in zip(range(len(records)), aux):
373 maybeStart = lnum - 1 - context // 2
373 maybeStart = lnum - 1 - context // 2
374 start = max(maybeStart, 0)
374 start = max(maybeStart, 0)
375 end = start + context
375 end = start + context
376 lines = ulinecache.getlines(file)[start:end]
376 lines = ulinecache.getlines(file)[start:end]
377 buf = list(records[i])
377 buf = list(records[i])
378 buf[LNUM_POS] = lnum
378 buf[LNUM_POS] = lnum
379 buf[INDEX_POS] = lnum - 1 - start
379 buf[INDEX_POS] = lnum - 1 - start
380 buf[LINES_POS] = lines
380 buf[LINES_POS] = lines
381 records[i] = tuple(buf)
381 records[i] = tuple(buf)
382 return records[tb_offset:]
382 return records[tb_offset:]
383
383
384 # Helper function -- largely belongs to VerboseTB, but we need the same
384 # Helper function -- largely belongs to VerboseTB, but we need the same
385 # functionality to produce a pseudo verbose TB for SyntaxErrors, so that they
385 # functionality to produce a pseudo verbose TB for SyntaxErrors, so that they
386 # can be recognized properly by ipython.el's py-traceback-line-re
386 # can be recognized properly by ipython.el's py-traceback-line-re
387 # (SyntaxErrors have to be treated specially because they have no traceback)
387 # (SyntaxErrors have to be treated specially because they have no traceback)
388
388
389 _parser = PyColorize.Parser()
389 _parser = PyColorize.Parser()
390
390
391
391
392 def _format_traceback_lines(lnum, index, lines, Colors, lvals=None, scheme=None):
392 def _format_traceback_lines(lnum, index, lines, Colors, lvals=None, scheme=None):
393 numbers_width = INDENT_SIZE - 1
393 numbers_width = INDENT_SIZE - 1
394 res = []
394 res = []
395 i = lnum - index
395 i = lnum - index
396
396
397 # This lets us get fully syntax-highlighted tracebacks.
397 # This lets us get fully syntax-highlighted tracebacks.
398 if scheme is None:
398 if scheme is None:
399 ipinst = get_ipython()
399 ipinst = get_ipython()
400 if ipinst is not None:
400 if ipinst is not None:
401 scheme = ipinst.colors
401 scheme = ipinst.colors
402 else:
402 else:
403 scheme = DEFAULT_SCHEME
403 scheme = DEFAULT_SCHEME
404
404
405 _line_format = _parser.format2
405 _line_format = _parser.format2
406
406
407 for line in lines:
407 for line in lines:
408 line = py3compat.cast_unicode(line)
408 line = py3compat.cast_unicode(line)
409
409
410 new_line, err = _line_format(line, 'str', scheme)
410 new_line, err = _line_format(line, 'str', scheme)
411 if not err: line = new_line
411 if not err: line = new_line
412
412
413 if i == lnum:
413 if i == lnum:
414 # This is the line with the error
414 # This is the line with the error
415 pad = numbers_width - len(str(i))
415 pad = numbers_width - len(str(i))
416 num = '%s%s' % (debugger.make_arrow(pad), str(lnum))
416 num = '%s%s' % (debugger.make_arrow(pad), str(lnum))
417 line = '%s%s%s %s%s' % (Colors.linenoEm, num,
417 line = '%s%s%s %s%s' % (Colors.linenoEm, num,
418 Colors.line, line, Colors.Normal)
418 Colors.line, line, Colors.Normal)
419 else:
419 else:
420 num = '%*s' % (numbers_width, i)
420 num = '%*s' % (numbers_width, i)
421 line = '%s%s%s %s' % (Colors.lineno, num,
421 line = '%s%s%s %s' % (Colors.lineno, num,
422 Colors.Normal, line)
422 Colors.Normal, line)
423
423
424 res.append(line)
424 res.append(line)
425 if lvals and i == lnum:
425 if lvals and i == lnum:
426 res.append(lvals + '\n')
426 res.append(lvals + '\n')
427 i = i + 1
427 i = i + 1
428 return res
428 return res
429
429
430 def is_recursion_error(etype, value, records):
430 def is_recursion_error(etype, value, records):
431 try:
431 try:
432 # RecursionError is new in Python 3.5
432 # RecursionError is new in Python 3.5
433 recursion_error_type = RecursionError
433 recursion_error_type = RecursionError
434 except NameError:
434 except NameError:
435 recursion_error_type = RuntimeError
435 recursion_error_type = RuntimeError
436
436
437 # The default recursion limit is 1000, but some of that will be taken up
437 # The default recursion limit is 1000, but some of that will be taken up
438 # by stack frames in IPython itself. >500 frames probably indicates
438 # by stack frames in IPython itself. >500 frames probably indicates
439 # a recursion error.
439 # a recursion error.
440 return (etype is recursion_error_type) \
440 return (etype is recursion_error_type) \
441 and "recursion" in str(value).lower() \
441 and "recursion" in str(value).lower() \
442 and len(records) > 500
442 and len(records) > 500
443
443
444 def find_recursion(etype, value, records):
444 def find_recursion(etype, value, records):
445 """Identify the repeating stack frames from a RecursionError traceback
445 """Identify the repeating stack frames from a RecursionError traceback
446
446
447 'records' is a list as returned by VerboseTB.get_records()
447 'records' is a list as returned by VerboseTB.get_records()
448
448
449 Returns (last_unique, repeat_length)
449 Returns (last_unique, repeat_length)
450 """
450 """
451 # This involves a bit of guesswork - we want to show enough of the traceback
451 # This involves a bit of guesswork - we want to show enough of the traceback
452 # to indicate where the recursion is occurring. We guess that the innermost
452 # to indicate where the recursion is occurring. We guess that the innermost
453 # quarter of the traceback (250 frames by default) is repeats, and find the
453 # quarter of the traceback (250 frames by default) is repeats, and find the
454 # first frame (from in to out) that looks different.
454 # first frame (from in to out) that looks different.
455 if not is_recursion_error(etype, value, records):
455 if not is_recursion_error(etype, value, records):
456 return len(records), 0
456 return len(records), 0
457
457
458 # Select filename, lineno, func_name to track frames with
458 # Select filename, lineno, func_name to track frames with
459 records = [r[1:4] for r in records]
459 records = [r[1:4] for r in records]
460 inner_frames = records[-(len(records)//4):]
460 inner_frames = records[-(len(records)//4):]
461 frames_repeated = set(inner_frames)
461 frames_repeated = set(inner_frames)
462
462
463 last_seen_at = {}
463 last_seen_at = {}
464 longest_repeat = 0
464 longest_repeat = 0
465 i = len(records)
465 i = len(records)
466 for frame in reversed(records):
466 for frame in reversed(records):
467 i -= 1
467 i -= 1
468 if frame not in frames_repeated:
468 if frame not in frames_repeated:
469 last_unique = i
469 last_unique = i
470 break
470 break
471
471
472 if frame in last_seen_at:
472 if frame in last_seen_at:
473 distance = last_seen_at[frame] - i
473 distance = last_seen_at[frame] - i
474 longest_repeat = max(longest_repeat, distance)
474 longest_repeat = max(longest_repeat, distance)
475
475
476 last_seen_at[frame] = i
476 last_seen_at[frame] = i
477 else:
477 else:
478 last_unique = 0 # The whole traceback was recursion
478 last_unique = 0 # The whole traceback was recursion
479
479
480 return last_unique, longest_repeat
480 return last_unique, longest_repeat
481
481
482 #---------------------------------------------------------------------------
482 #---------------------------------------------------------------------------
483 # Module classes
483 # Module classes
484 class TBTools(colorable.Colorable):
484 class TBTools(colorable.Colorable):
485 """Basic tools used by all traceback printer classes."""
485 """Basic tools used by all traceback printer classes."""
486
486
487 # Number of frames to skip when reporting tracebacks
487 # Number of frames to skip when reporting tracebacks
488 tb_offset = 0
488 tb_offset = 0
489
489
490 def __init__(self, color_scheme='NoColor', call_pdb=False, ostream=None, parent=None, config=None):
490 def __init__(self, color_scheme='NoColor', call_pdb=False, ostream=None, parent=None, config=None):
491 # Whether to call the interactive pdb debugger after printing
491 # Whether to call the interactive pdb debugger after printing
492 # tracebacks or not
492 # tracebacks or not
493 super(TBTools, self).__init__(parent=parent, config=config)
493 super(TBTools, self).__init__(parent=parent, config=config)
494 self.call_pdb = call_pdb
494 self.call_pdb = call_pdb
495
495
496 # Output stream to write to. Note that we store the original value in
496 # Output stream to write to. Note that we store the original value in
497 # a private attribute and then make the public ostream a property, so
497 # a private attribute and then make the public ostream a property, so
498 # that we can delay accessing io.stdout until runtime. The way
498 # that we can delay accessing sys.stdout until runtime. The way
499 # things are written now, the io.stdout object is dynamically managed
499 # things are written now, the sys.stdout object is dynamically managed
500 # so a reference to it should NEVER be stored statically. This
500 # so a reference to it should NEVER be stored statically. This
501 # property approach confines this detail to a single location, and all
501 # property approach confines this detail to a single location, and all
502 # subclasses can simply access self.ostream for writing.
502 # subclasses can simply access self.ostream for writing.
503 self._ostream = ostream
503 self._ostream = ostream
504
504
505 # Create color table
505 # Create color table
506 self.color_scheme_table = exception_colors()
506 self.color_scheme_table = exception_colors()
507
507
508 self.set_colors(color_scheme)
508 self.set_colors(color_scheme)
509 self.old_scheme = color_scheme # save initial value for toggles
509 self.old_scheme = color_scheme # save initial value for toggles
510
510
511 if call_pdb:
511 if call_pdb:
512 self.pdb = debugger.Pdb(self.color_scheme_table.active_scheme_name)
512 self.pdb = debugger.Pdb()
513 else:
513 else:
514 self.pdb = None
514 self.pdb = None
515
515
516 def _get_ostream(self):
516 def _get_ostream(self):
517 """Output stream that exceptions are written to.
517 """Output stream that exceptions are written to.
518
518
519 Valid values are:
519 Valid values are:
520
520
521 - None: the default, which means that IPython will dynamically resolve
521 - None: the default, which means that IPython will dynamically resolve
522 to io.stdout. This ensures compatibility with most tools, including
522 to sys.stdout. This ensures compatibility with most tools, including
523 Windows (where plain stdout doesn't recognize ANSI escapes).
523 Windows (where plain stdout doesn't recognize ANSI escapes).
524
524
525 - Any object with 'write' and 'flush' attributes.
525 - Any object with 'write' and 'flush' attributes.
526 """
526 """
527 return sys.stdout if self._ostream is None else self._ostream
527 return sys.stdout if self._ostream is None else self._ostream
528
528
529 def _set_ostream(self, val):
529 def _set_ostream(self, val):
530 assert val is None or (hasattr(val, 'write') and hasattr(val, 'flush'))
530 assert val is None or (hasattr(val, 'write') and hasattr(val, 'flush'))
531 self._ostream = val
531 self._ostream = val
532
532
533 ostream = property(_get_ostream, _set_ostream)
533 ostream = property(_get_ostream, _set_ostream)
534
534
535 def set_colors(self, *args, **kw):
535 def set_colors(self, *args, **kw):
536 """Shorthand access to the color table scheme selector method."""
536 """Shorthand access to the color table scheme selector method."""
537
537
538 # Set own color table
538 # Set own color table
539 self.color_scheme_table.set_active_scheme(*args, **kw)
539 self.color_scheme_table.set_active_scheme(*args, **kw)
540 # for convenience, set Colors to the active scheme
540 # for convenience, set Colors to the active scheme
541 self.Colors = self.color_scheme_table.active_colors
541 self.Colors = self.color_scheme_table.active_colors
542 # Also set colors of debugger
542 # Also set colors of debugger
543 if hasattr(self, 'pdb') and self.pdb is not None:
543 if hasattr(self, 'pdb') and self.pdb is not None:
544 self.pdb.set_colors(*args, **kw)
544 self.pdb.set_colors(*args, **kw)
545
545
546 def color_toggle(self):
546 def color_toggle(self):
547 """Toggle between the currently active color scheme and NoColor."""
547 """Toggle between the currently active color scheme and NoColor."""
548
548
549 if self.color_scheme_table.active_scheme_name == 'NoColor':
549 if self.color_scheme_table.active_scheme_name == 'NoColor':
550 self.color_scheme_table.set_active_scheme(self.old_scheme)
550 self.color_scheme_table.set_active_scheme(self.old_scheme)
551 self.Colors = self.color_scheme_table.active_colors
551 self.Colors = self.color_scheme_table.active_colors
552 else:
552 else:
553 self.old_scheme = self.color_scheme_table.active_scheme_name
553 self.old_scheme = self.color_scheme_table.active_scheme_name
554 self.color_scheme_table.set_active_scheme('NoColor')
554 self.color_scheme_table.set_active_scheme('NoColor')
555 self.Colors = self.color_scheme_table.active_colors
555 self.Colors = self.color_scheme_table.active_colors
556
556
557 def stb2text(self, stb):
557 def stb2text(self, stb):
558 """Convert a structured traceback (a list) to a string."""
558 """Convert a structured traceback (a list) to a string."""
559 return '\n'.join(stb)
559 return '\n'.join(stb)
560
560
561 def text(self, etype, value, tb, tb_offset=None, context=5):
561 def text(self, etype, value, tb, tb_offset=None, context=5):
562 """Return formatted traceback.
562 """Return formatted traceback.
563
563
564 Subclasses may override this if they add extra arguments.
564 Subclasses may override this if they add extra arguments.
565 """
565 """
566 tb_list = self.structured_traceback(etype, value, tb,
566 tb_list = self.structured_traceback(etype, value, tb,
567 tb_offset, context)
567 tb_offset, context)
568 return self.stb2text(tb_list)
568 return self.stb2text(tb_list)
569
569
570 def structured_traceback(self, etype, evalue, tb, tb_offset=None,
570 def structured_traceback(self, etype, evalue, tb, tb_offset=None,
571 context=5, mode=None):
571 context=5, mode=None):
572 """Return a list of traceback frames.
572 """Return a list of traceback frames.
573
573
574 Must be implemented by each class.
574 Must be implemented by each class.
575 """
575 """
576 raise NotImplementedError()
576 raise NotImplementedError()
577
577
578
578
579 #---------------------------------------------------------------------------
579 #---------------------------------------------------------------------------
580 class ListTB(TBTools):
580 class ListTB(TBTools):
581 """Print traceback information from a traceback list, with optional color.
581 """Print traceback information from a traceback list, with optional color.
582
582
583 Calling requires 3 arguments: (etype, evalue, elist)
583 Calling requires 3 arguments: (etype, evalue, elist)
584 as would be obtained by::
584 as would be obtained by::
585
585
586 etype, evalue, tb = sys.exc_info()
586 etype, evalue, tb = sys.exc_info()
587 if tb:
587 if tb:
588 elist = traceback.extract_tb(tb)
588 elist = traceback.extract_tb(tb)
589 else:
589 else:
590 elist = None
590 elist = None
591
591
592 It can thus be used by programs which need to process the traceback before
592 It can thus be used by programs which need to process the traceback before
593 printing (such as console replacements based on the code module from the
593 printing (such as console replacements based on the code module from the
594 standard library).
594 standard library).
595
595
596 Because they are meant to be called without a full traceback (only a
596 Because they are meant to be called without a full traceback (only a
597 list), instances of this class can't call the interactive pdb debugger."""
597 list), instances of this class can't call the interactive pdb debugger."""
598
598
599 def __init__(self, color_scheme='NoColor', call_pdb=False, ostream=None, parent=None):
599 def __init__(self, color_scheme='NoColor', call_pdb=False, ostream=None, parent=None):
600 TBTools.__init__(self, color_scheme=color_scheme, call_pdb=call_pdb,
600 TBTools.__init__(self, color_scheme=color_scheme, call_pdb=call_pdb,
601 ostream=ostream, parent=parent)
601 ostream=ostream, parent=parent)
602
602
603 def __call__(self, etype, value, elist):
603 def __call__(self, etype, value, elist):
604 self.ostream.flush()
604 self.ostream.flush()
605 self.ostream.write(self.text(etype, value, elist))
605 self.ostream.write(self.text(etype, value, elist))
606 self.ostream.write('\n')
606 self.ostream.write('\n')
607
607
608 def structured_traceback(self, etype, value, elist, tb_offset=None,
608 def structured_traceback(self, etype, value, elist, tb_offset=None,
609 context=5):
609 context=5):
610 """Return a color formatted string with the traceback info.
610 """Return a color formatted string with the traceback info.
611
611
612 Parameters
612 Parameters
613 ----------
613 ----------
614 etype : exception type
614 etype : exception type
615 Type of the exception raised.
615 Type of the exception raised.
616
616
617 value : object
617 value : object
618 Data stored in the exception
618 Data stored in the exception
619
619
620 elist : list
620 elist : list
621 List of frames, see class docstring for details.
621 List of frames, see class docstring for details.
622
622
623 tb_offset : int, optional
623 tb_offset : int, optional
624 Number of frames in the traceback to skip. If not given, the
624 Number of frames in the traceback to skip. If not given, the
625 instance value is used (set in constructor).
625 instance value is used (set in constructor).
626
626
627 context : int, optional
627 context : int, optional
628 Number of lines of context information to print.
628 Number of lines of context information to print.
629
629
630 Returns
630 Returns
631 -------
631 -------
632 String with formatted exception.
632 String with formatted exception.
633 """
633 """
634 tb_offset = self.tb_offset if tb_offset is None else tb_offset
634 tb_offset = self.tb_offset if tb_offset is None else tb_offset
635 Colors = self.Colors
635 Colors = self.Colors
636 out_list = []
636 out_list = []
637 if elist:
637 if elist:
638
638
639 if tb_offset and len(elist) > tb_offset:
639 if tb_offset and len(elist) > tb_offset:
640 elist = elist[tb_offset:]
640 elist = elist[tb_offset:]
641
641
642 out_list.append('Traceback %s(most recent call last)%s:' %
642 out_list.append('Traceback %s(most recent call last)%s:' %
643 (Colors.normalEm, Colors.Normal) + '\n')
643 (Colors.normalEm, Colors.Normal) + '\n')
644 out_list.extend(self._format_list(elist))
644 out_list.extend(self._format_list(elist))
645 # The exception info should be a single entry in the list.
645 # The exception info should be a single entry in the list.
646 lines = ''.join(self._format_exception_only(etype, value))
646 lines = ''.join(self._format_exception_only(etype, value))
647 out_list.append(lines)
647 out_list.append(lines)
648
648
649 # Note: this code originally read:
649 # Note: this code originally read:
650
650
651 ## for line in lines[:-1]:
651 ## for line in lines[:-1]:
652 ## out_list.append(" "+line)
652 ## out_list.append(" "+line)
653 ## out_list.append(lines[-1])
653 ## out_list.append(lines[-1])
654
654
655 # This means it was indenting everything but the last line by a little
655 # This means it was indenting everything but the last line by a little
656 # bit. I've disabled this for now, but if we see ugliness somewhere we
656 # bit. I've disabled this for now, but if we see ugliness somewhere we
657 # can restore it.
657 # can restore it.
658
658
659 return out_list
659 return out_list
660
660
661 def _format_list(self, extracted_list):
661 def _format_list(self, extracted_list):
662 """Format a list of traceback entry tuples for printing.
662 """Format a list of traceback entry tuples for printing.
663
663
664 Given a list of tuples as returned by extract_tb() or
664 Given a list of tuples as returned by extract_tb() or
665 extract_stack(), return a list of strings ready for printing.
665 extract_stack(), return a list of strings ready for printing.
666 Each string in the resulting list corresponds to the item with the
666 Each string in the resulting list corresponds to the item with the
667 same index in the argument list. Each string ends in a newline;
667 same index in the argument list. Each string ends in a newline;
668 the strings may contain internal newlines as well, for those items
668 the strings may contain internal newlines as well, for those items
669 whose source text line is not None.
669 whose source text line is not None.
670
670
671 Lifted almost verbatim from traceback.py
671 Lifted almost verbatim from traceback.py
672 """
672 """
673
673
674 Colors = self.Colors
674 Colors = self.Colors
675 list = []
675 list = []
676 for filename, lineno, name, line in extracted_list[:-1]:
676 for filename, lineno, name, line in extracted_list[:-1]:
677 item = ' File %s"%s"%s, line %s%d%s, in %s%s%s\n' % \
677 item = ' File %s"%s"%s, line %s%d%s, in %s%s%s\n' % \
678 (Colors.filename, py3compat.cast_unicode_py2(filename, "utf-8"), Colors.Normal,
678 (Colors.filename, py3compat.cast_unicode_py2(filename, "utf-8"), Colors.Normal,
679 Colors.lineno, lineno, Colors.Normal,
679 Colors.lineno, lineno, Colors.Normal,
680 Colors.name, py3compat.cast_unicode_py2(name, "utf-8"), Colors.Normal)
680 Colors.name, py3compat.cast_unicode_py2(name, "utf-8"), Colors.Normal)
681 if line:
681 if line:
682 item += ' %s\n' % line.strip()
682 item += ' %s\n' % line.strip()
683 list.append(item)
683 list.append(item)
684 # Emphasize the last entry
684 # Emphasize the last entry
685 filename, lineno, name, line = extracted_list[-1]
685 filename, lineno, name, line = extracted_list[-1]
686 item = '%s File %s"%s"%s, line %s%d%s, in %s%s%s%s\n' % \
686 item = '%s File %s"%s"%s, line %s%d%s, in %s%s%s%s\n' % \
687 (Colors.normalEm,
687 (Colors.normalEm,
688 Colors.filenameEm, py3compat.cast_unicode_py2(filename, "utf-8"), Colors.normalEm,
688 Colors.filenameEm, py3compat.cast_unicode_py2(filename, "utf-8"), Colors.normalEm,
689 Colors.linenoEm, lineno, Colors.normalEm,
689 Colors.linenoEm, lineno, Colors.normalEm,
690 Colors.nameEm, py3compat.cast_unicode_py2(name, "utf-8"), Colors.normalEm,
690 Colors.nameEm, py3compat.cast_unicode_py2(name, "utf-8"), Colors.normalEm,
691 Colors.Normal)
691 Colors.Normal)
692 if line:
692 if line:
693 item += '%s %s%s\n' % (Colors.line, line.strip(),
693 item += '%s %s%s\n' % (Colors.line, line.strip(),
694 Colors.Normal)
694 Colors.Normal)
695 list.append(item)
695 list.append(item)
696 return list
696 return list
697
697
698 def _format_exception_only(self, etype, value):
698 def _format_exception_only(self, etype, value):
699 """Format the exception part of a traceback.
699 """Format the exception part of a traceback.
700
700
701 The arguments are the exception type and value such as given by
701 The arguments are the exception type and value such as given by
702 sys.exc_info()[:2]. The return value is a list of strings, each ending
702 sys.exc_info()[:2]. The return value is a list of strings, each ending
703 in a newline. Normally, the list contains a single string; however,
703 in a newline. Normally, the list contains a single string; however,
704 for SyntaxError exceptions, it contains several lines that (when
704 for SyntaxError exceptions, it contains several lines that (when
705 printed) display detailed information about where the syntax error
705 printed) display detailed information about where the syntax error
706 occurred. The message indicating which exception occurred is the
706 occurred. The message indicating which exception occurred is the
707 always last string in the list.
707 always last string in the list.
708
708
709 Also lifted nearly verbatim from traceback.py
709 Also lifted nearly verbatim from traceback.py
710 """
710 """
711 have_filedata = False
711 have_filedata = False
712 Colors = self.Colors
712 Colors = self.Colors
713 list = []
713 list = []
714 stype = py3compat.cast_unicode(Colors.excName + etype.__name__ + Colors.Normal)
714 stype = py3compat.cast_unicode(Colors.excName + etype.__name__ + Colors.Normal)
715 if value is None:
715 if value is None:
716 # Not sure if this can still happen in Python 2.6 and above
716 # Not sure if this can still happen in Python 2.6 and above
717 list.append(stype + '\n')
717 list.append(stype + '\n')
718 else:
718 else:
719 if issubclass(etype, SyntaxError):
719 if issubclass(etype, SyntaxError):
720 have_filedata = True
720 have_filedata = True
721 if not value.filename: value.filename = "<string>"
721 if not value.filename: value.filename = "<string>"
722 if value.lineno:
722 if value.lineno:
723 lineno = value.lineno
723 lineno = value.lineno
724 textline = ulinecache.getline(value.filename, value.lineno)
724 textline = ulinecache.getline(value.filename, value.lineno)
725 else:
725 else:
726 lineno = 'unknown'
726 lineno = 'unknown'
727 textline = ''
727 textline = ''
728 list.append('%s File %s"%s"%s, line %s%s%s\n' % \
728 list.append('%s File %s"%s"%s, line %s%s%s\n' % \
729 (Colors.normalEm,
729 (Colors.normalEm,
730 Colors.filenameEm, py3compat.cast_unicode(value.filename), Colors.normalEm,
730 Colors.filenameEm, py3compat.cast_unicode(value.filename), Colors.normalEm,
731 Colors.linenoEm, lineno, Colors.Normal ))
731 Colors.linenoEm, lineno, Colors.Normal ))
732 if textline == '':
732 if textline == '':
733 textline = py3compat.cast_unicode(value.text, "utf-8")
733 textline = py3compat.cast_unicode(value.text, "utf-8")
734
734
735 if textline is not None:
735 if textline is not None:
736 i = 0
736 i = 0
737 while i < len(textline) and textline[i].isspace():
737 while i < len(textline) and textline[i].isspace():
738 i += 1
738 i += 1
739 list.append('%s %s%s\n' % (Colors.line,
739 list.append('%s %s%s\n' % (Colors.line,
740 textline.strip(),
740 textline.strip(),
741 Colors.Normal))
741 Colors.Normal))
742 if value.offset is not None:
742 if value.offset is not None:
743 s = ' '
743 s = ' '
744 for c in textline[i:value.offset - 1]:
744 for c in textline[i:value.offset - 1]:
745 if c.isspace():
745 if c.isspace():
746 s += c
746 s += c
747 else:
747 else:
748 s += ' '
748 s += ' '
749 list.append('%s%s^%s\n' % (Colors.caret, s,
749 list.append('%s%s^%s\n' % (Colors.caret, s,
750 Colors.Normal))
750 Colors.Normal))
751
751
752 try:
752 try:
753 s = value.msg
753 s = value.msg
754 except Exception:
754 except Exception:
755 s = self._some_str(value)
755 s = self._some_str(value)
756 if s:
756 if s:
757 list.append('%s%s:%s %s\n' % (stype, Colors.excName,
757 list.append('%s%s:%s %s\n' % (stype, Colors.excName,
758 Colors.Normal, s))
758 Colors.Normal, s))
759 else:
759 else:
760 list.append('%s\n' % stype)
760 list.append('%s\n' % stype)
761
761
762 # sync with user hooks
762 # sync with user hooks
763 if have_filedata:
763 if have_filedata:
764 ipinst = get_ipython()
764 ipinst = get_ipython()
765 if ipinst is not None:
765 if ipinst is not None:
766 ipinst.hooks.synchronize_with_editor(value.filename, value.lineno, 0)
766 ipinst.hooks.synchronize_with_editor(value.filename, value.lineno, 0)
767
767
768 return list
768 return list
769
769
770 def get_exception_only(self, etype, value):
770 def get_exception_only(self, etype, value):
771 """Only print the exception type and message, without a traceback.
771 """Only print the exception type and message, without a traceback.
772
772
773 Parameters
773 Parameters
774 ----------
774 ----------
775 etype : exception type
775 etype : exception type
776 value : exception value
776 value : exception value
777 """
777 """
778 return ListTB.structured_traceback(self, etype, value, [])
778 return ListTB.structured_traceback(self, etype, value, [])
779
779
780 def show_exception_only(self, etype, evalue):
780 def show_exception_only(self, etype, evalue):
781 """Only print the exception type and message, without a traceback.
781 """Only print the exception type and message, without a traceback.
782
782
783 Parameters
783 Parameters
784 ----------
784 ----------
785 etype : exception type
785 etype : exception type
786 value : exception value
786 value : exception value
787 """
787 """
788 # This method needs to use __call__ from *this* class, not the one from
788 # This method needs to use __call__ from *this* class, not the one from
789 # a subclass whose signature or behavior may be different
789 # a subclass whose signature or behavior may be different
790 ostream = self.ostream
790 ostream = self.ostream
791 ostream.flush()
791 ostream.flush()
792 ostream.write('\n'.join(self.get_exception_only(etype, evalue)))
792 ostream.write('\n'.join(self.get_exception_only(etype, evalue)))
793 ostream.flush()
793 ostream.flush()
794
794
795 def _some_str(self, value):
795 def _some_str(self, value):
796 # Lifted from traceback.py
796 # Lifted from traceback.py
797 try:
797 try:
798 return py3compat.cast_unicode(str(value))
798 return py3compat.cast_unicode(str(value))
799 except:
799 except:
800 return u'<unprintable %s object>' % type(value).__name__
800 return u'<unprintable %s object>' % type(value).__name__
801
801
802
802
803 #----------------------------------------------------------------------------
803 #----------------------------------------------------------------------------
804 class VerboseTB(TBTools):
804 class VerboseTB(TBTools):
805 """A port of Ka-Ping Yee's cgitb.py module that outputs color text instead
805 """A port of Ka-Ping Yee's cgitb.py module that outputs color text instead
806 of HTML. Requires inspect and pydoc. Crazy, man.
806 of HTML. Requires inspect and pydoc. Crazy, man.
807
807
808 Modified version which optionally strips the topmost entries from the
808 Modified version which optionally strips the topmost entries from the
809 traceback, to be used with alternate interpreters (because their own code
809 traceback, to be used with alternate interpreters (because their own code
810 would appear in the traceback)."""
810 would appear in the traceback)."""
811
811
812 def __init__(self, color_scheme='Linux', call_pdb=False, ostream=None,
812 def __init__(self, color_scheme='Linux', call_pdb=False, ostream=None,
813 tb_offset=0, long_header=False, include_vars=True,
813 tb_offset=0, long_header=False, include_vars=True,
814 check_cache=None, debugger_cls = None):
814 check_cache=None, debugger_cls = None):
815 """Specify traceback offset, headers and color scheme.
815 """Specify traceback offset, headers and color scheme.
816
816
817 Define how many frames to drop from the tracebacks. Calling it with
817 Define how many frames to drop from the tracebacks. Calling it with
818 tb_offset=1 allows use of this handler in interpreters which will have
818 tb_offset=1 allows use of this handler in interpreters which will have
819 their own code at the top of the traceback (VerboseTB will first
819 their own code at the top of the traceback (VerboseTB will first
820 remove that frame before printing the traceback info)."""
820 remove that frame before printing the traceback info)."""
821 TBTools.__init__(self, color_scheme=color_scheme, call_pdb=call_pdb,
821 TBTools.__init__(self, color_scheme=color_scheme, call_pdb=call_pdb,
822 ostream=ostream)
822 ostream=ostream)
823 self.tb_offset = tb_offset
823 self.tb_offset = tb_offset
824 self.long_header = long_header
824 self.long_header = long_header
825 self.include_vars = include_vars
825 self.include_vars = include_vars
826 # By default we use linecache.checkcache, but the user can provide a
826 # By default we use linecache.checkcache, but the user can provide a
827 # different check_cache implementation. This is used by the IPython
827 # different check_cache implementation. This is used by the IPython
828 # kernel to provide tracebacks for interactive code that is cached,
828 # kernel to provide tracebacks for interactive code that is cached,
829 # by a compiler instance that flushes the linecache but preserves its
829 # by a compiler instance that flushes the linecache but preserves its
830 # own code cache.
830 # own code cache.
831 if check_cache is None:
831 if check_cache is None:
832 check_cache = linecache.checkcache
832 check_cache = linecache.checkcache
833 self.check_cache = check_cache
833 self.check_cache = check_cache
834
834
835 self.debugger_cls = debugger_cls or debugger.Pdb
835 self.debugger_cls = debugger_cls or debugger.Pdb
836
836
837 def format_records(self, records, last_unique, recursion_repeat):
837 def format_records(self, records, last_unique, recursion_repeat):
838 """Format the stack frames of the traceback"""
838 """Format the stack frames of the traceback"""
839 frames = []
839 frames = []
840 for r in records[:last_unique+recursion_repeat+1]:
840 for r in records[:last_unique+recursion_repeat+1]:
841 #print '*** record:',file,lnum,func,lines,index # dbg
841 #print '*** record:',file,lnum,func,lines,index # dbg
842 frames.append(self.format_record(*r))
842 frames.append(self.format_record(*r))
843
843
844 if recursion_repeat:
844 if recursion_repeat:
845 frames.append('... last %d frames repeated, from the frame below ...\n' % recursion_repeat)
845 frames.append('... last %d frames repeated, from the frame below ...\n' % recursion_repeat)
846 frames.append(self.format_record(*records[last_unique+recursion_repeat+1]))
846 frames.append(self.format_record(*records[last_unique+recursion_repeat+1]))
847
847
848 return frames
848 return frames
849
849
850 def format_record(self, frame, file, lnum, func, lines, index):
850 def format_record(self, frame, file, lnum, func, lines, index):
851 """Format a single stack frame"""
851 """Format a single stack frame"""
852 Colors = self.Colors # just a shorthand + quicker name lookup
852 Colors = self.Colors # just a shorthand + quicker name lookup
853 ColorsNormal = Colors.Normal # used a lot
853 ColorsNormal = Colors.Normal # used a lot
854 col_scheme = self.color_scheme_table.active_scheme_name
854 col_scheme = self.color_scheme_table.active_scheme_name
855 indent = ' ' * INDENT_SIZE
855 indent = ' ' * INDENT_SIZE
856 em_normal = '%s\n%s%s' % (Colors.valEm, indent, ColorsNormal)
856 em_normal = '%s\n%s%s' % (Colors.valEm, indent, ColorsNormal)
857 undefined = '%sundefined%s' % (Colors.em, ColorsNormal)
857 undefined = '%sundefined%s' % (Colors.em, ColorsNormal)
858 tpl_link = '%s%%s%s' % (Colors.filenameEm, ColorsNormal)
858 tpl_link = '%s%%s%s' % (Colors.filenameEm, ColorsNormal)
859 tpl_call = 'in %s%%s%s%%s%s' % (Colors.vName, Colors.valEm,
859 tpl_call = 'in %s%%s%s%%s%s' % (Colors.vName, Colors.valEm,
860 ColorsNormal)
860 ColorsNormal)
861 tpl_call_fail = 'in %s%%s%s(***failed resolving arguments***)%s' % \
861 tpl_call_fail = 'in %s%%s%s(***failed resolving arguments***)%s' % \
862 (Colors.vName, Colors.valEm, ColorsNormal)
862 (Colors.vName, Colors.valEm, ColorsNormal)
863 tpl_local_var = '%s%%s%s' % (Colors.vName, ColorsNormal)
863 tpl_local_var = '%s%%s%s' % (Colors.vName, ColorsNormal)
864 tpl_global_var = '%sglobal%s %s%%s%s' % (Colors.em, ColorsNormal,
864 tpl_global_var = '%sglobal%s %s%%s%s' % (Colors.em, ColorsNormal,
865 Colors.vName, ColorsNormal)
865 Colors.vName, ColorsNormal)
866 tpl_name_val = '%%s %s= %%s%s' % (Colors.valEm, ColorsNormal)
866 tpl_name_val = '%%s %s= %%s%s' % (Colors.valEm, ColorsNormal)
867
867
868 tpl_line = '%s%%s%s %%s' % (Colors.lineno, ColorsNormal)
868 tpl_line = '%s%%s%s %%s' % (Colors.lineno, ColorsNormal)
869 tpl_line_em = '%s%%s%s %%s%s' % (Colors.linenoEm, Colors.line,
869 tpl_line_em = '%s%%s%s %%s%s' % (Colors.linenoEm, Colors.line,
870 ColorsNormal)
870 ColorsNormal)
871
871
872 abspath = os.path.abspath
872 abspath = os.path.abspath
873
873
874
874
875 if not file:
875 if not file:
876 file = '?'
876 file = '?'
877 elif file.startswith(str("<")) and file.endswith(str(">")):
877 elif file.startswith(str("<")) and file.endswith(str(">")):
878 # Not a real filename, no problem...
878 # Not a real filename, no problem...
879 pass
879 pass
880 elif not os.path.isabs(file):
880 elif not os.path.isabs(file):
881 # Try to make the filename absolute by trying all
881 # Try to make the filename absolute by trying all
882 # sys.path entries (which is also what linecache does)
882 # sys.path entries (which is also what linecache does)
883 for dirname in sys.path:
883 for dirname in sys.path:
884 try:
884 try:
885 fullname = os.path.join(dirname, file)
885 fullname = os.path.join(dirname, file)
886 if os.path.isfile(fullname):
886 if os.path.isfile(fullname):
887 file = os.path.abspath(fullname)
887 file = os.path.abspath(fullname)
888 break
888 break
889 except Exception:
889 except Exception:
890 # Just in case that sys.path contains very
890 # Just in case that sys.path contains very
891 # strange entries...
891 # strange entries...
892 pass
892 pass
893
893
894 file = py3compat.cast_unicode(file, util_path.fs_encoding)
894 file = py3compat.cast_unicode(file, util_path.fs_encoding)
895 link = tpl_link % file
895 link = tpl_link % file
896 args, varargs, varkw, locals = fixed_getargvalues(frame)
896 args, varargs, varkw, locals = fixed_getargvalues(frame)
897
897
898 if func == '?':
898 if func == '?':
899 call = ''
899 call = ''
900 else:
900 else:
901 # Decide whether to include variable details or not
901 # Decide whether to include variable details or not
902 var_repr = self.include_vars and eqrepr or nullrepr
902 var_repr = self.include_vars and eqrepr or nullrepr
903 try:
903 try:
904 call = tpl_call % (func, inspect.formatargvalues(args,
904 call = tpl_call % (func, inspect.formatargvalues(args,
905 varargs, varkw,
905 varargs, varkw,
906 locals, formatvalue=var_repr))
906 locals, formatvalue=var_repr))
907 except KeyError:
907 except KeyError:
908 # This happens in situations like errors inside generator
908 # This happens in situations like errors inside generator
909 # expressions, where local variables are listed in the
909 # expressions, where local variables are listed in the
910 # line, but can't be extracted from the frame. I'm not
910 # line, but can't be extracted from the frame. I'm not
911 # 100% sure this isn't actually a bug in inspect itself,
911 # 100% sure this isn't actually a bug in inspect itself,
912 # but since there's no info for us to compute with, the
912 # but since there's no info for us to compute with, the
913 # best we can do is report the failure and move on. Here
913 # best we can do is report the failure and move on. Here
914 # we must *not* call any traceback construction again,
914 # we must *not* call any traceback construction again,
915 # because that would mess up use of %debug later on. So we
915 # because that would mess up use of %debug later on. So we
916 # simply report the failure and move on. The only
916 # simply report the failure and move on. The only
917 # limitation will be that this frame won't have locals
917 # limitation will be that this frame won't have locals
918 # listed in the call signature. Quite subtle problem...
918 # listed in the call signature. Quite subtle problem...
919 # I can't think of a good way to validate this in a unit
919 # I can't think of a good way to validate this in a unit
920 # test, but running a script consisting of:
920 # test, but running a script consisting of:
921 # dict( (k,v.strip()) for (k,v) in range(10) )
921 # dict( (k,v.strip()) for (k,v) in range(10) )
922 # will illustrate the error, if this exception catch is
922 # will illustrate the error, if this exception catch is
923 # disabled.
923 # disabled.
924 call = tpl_call_fail % func
924 call = tpl_call_fail % func
925
925
926 # Don't attempt to tokenize binary files.
926 # Don't attempt to tokenize binary files.
927 if file.endswith(('.so', '.pyd', '.dll')):
927 if file.endswith(('.so', '.pyd', '.dll')):
928 return '%s %s\n' % (link, call)
928 return '%s %s\n' % (link, call)
929
929
930 elif file.endswith(('.pyc', '.pyo')):
930 elif file.endswith(('.pyc', '.pyo')):
931 # Look up the corresponding source file.
931 # Look up the corresponding source file.
932 try:
932 try:
933 file = openpy.source_from_cache(file)
933 file = openpy.source_from_cache(file)
934 except ValueError:
934 except ValueError:
935 # Failed to get the source file for some reason
935 # Failed to get the source file for some reason
936 # E.g. https://github.com/ipython/ipython/issues/9486
936 # E.g. https://github.com/ipython/ipython/issues/9486
937 return '%s %s\n' % (link, call)
937 return '%s %s\n' % (link, call)
938
938
939 def linereader(file=file, lnum=[lnum], getline=ulinecache.getline):
939 def linereader(file=file, lnum=[lnum], getline=ulinecache.getline):
940 line = getline(file, lnum[0])
940 line = getline(file, lnum[0])
941 lnum[0] += 1
941 lnum[0] += 1
942 return line
942 return line
943
943
944 # Build the list of names on this line of code where the exception
944 # Build the list of names on this line of code where the exception
945 # occurred.
945 # occurred.
946 try:
946 try:
947 names = []
947 names = []
948 name_cont = False
948 name_cont = False
949
949
950 for token_type, token, start, end, line in generate_tokens(linereader):
950 for token_type, token, start, end, line in generate_tokens(linereader):
951 # build composite names
951 # build composite names
952 if token_type == tokenize.NAME and token not in keyword.kwlist:
952 if token_type == tokenize.NAME and token not in keyword.kwlist:
953 if name_cont:
953 if name_cont:
954 # Continuation of a dotted name
954 # Continuation of a dotted name
955 try:
955 try:
956 names[-1].append(token)
956 names[-1].append(token)
957 except IndexError:
957 except IndexError:
958 names.append([token])
958 names.append([token])
959 name_cont = False
959 name_cont = False
960 else:
960 else:
961 # Regular new names. We append everything, the caller
961 # Regular new names. We append everything, the caller
962 # will be responsible for pruning the list later. It's
962 # will be responsible for pruning the list later. It's
963 # very tricky to try to prune as we go, b/c composite
963 # very tricky to try to prune as we go, b/c composite
964 # names can fool us. The pruning at the end is easy
964 # names can fool us. The pruning at the end is easy
965 # to do (or the caller can print a list with repeated
965 # to do (or the caller can print a list with repeated
966 # names if so desired.
966 # names if so desired.
967 names.append([token])
967 names.append([token])
968 elif token == '.':
968 elif token == '.':
969 name_cont = True
969 name_cont = True
970 elif token_type == tokenize.NEWLINE:
970 elif token_type == tokenize.NEWLINE:
971 break
971 break
972
972
973 except (IndexError, UnicodeDecodeError, SyntaxError):
973 except (IndexError, UnicodeDecodeError, SyntaxError):
974 # signals exit of tokenizer
974 # signals exit of tokenizer
975 # SyntaxError can occur if the file is not actually Python
975 # SyntaxError can occur if the file is not actually Python
976 # - see gh-6300
976 # - see gh-6300
977 pass
977 pass
978 except tokenize.TokenError as msg:
978 except tokenize.TokenError as msg:
979 _m = ("An unexpected error occurred while tokenizing input\n"
979 _m = ("An unexpected error occurred while tokenizing input\n"
980 "The following traceback may be corrupted or invalid\n"
980 "The following traceback may be corrupted or invalid\n"
981 "The error message is: %s\n" % msg)
981 "The error message is: %s\n" % msg)
982 error(_m)
982 error(_m)
983
983
984 # Join composite names (e.g. "dict.fromkeys")
984 # Join composite names (e.g. "dict.fromkeys")
985 names = ['.'.join(n) for n in names]
985 names = ['.'.join(n) for n in names]
986 # prune names list of duplicates, but keep the right order
986 # prune names list of duplicates, but keep the right order
987 unique_names = uniq_stable(names)
987 unique_names = uniq_stable(names)
988
988
989 # Start loop over vars
989 # Start loop over vars
990 lvals = []
990 lvals = []
991 if self.include_vars:
991 if self.include_vars:
992 for name_full in unique_names:
992 for name_full in unique_names:
993 name_base = name_full.split('.', 1)[0]
993 name_base = name_full.split('.', 1)[0]
994 if name_base in frame.f_code.co_varnames:
994 if name_base in frame.f_code.co_varnames:
995 if name_base in locals:
995 if name_base in locals:
996 try:
996 try:
997 value = repr(eval(name_full, locals))
997 value = repr(eval(name_full, locals))
998 except:
998 except:
999 value = undefined
999 value = undefined
1000 else:
1000 else:
1001 value = undefined
1001 value = undefined
1002 name = tpl_local_var % name_full
1002 name = tpl_local_var % name_full
1003 else:
1003 else:
1004 if name_base in frame.f_globals:
1004 if name_base in frame.f_globals:
1005 try:
1005 try:
1006 value = repr(eval(name_full, frame.f_globals))
1006 value = repr(eval(name_full, frame.f_globals))
1007 except:
1007 except:
1008 value = undefined
1008 value = undefined
1009 else:
1009 else:
1010 value = undefined
1010 value = undefined
1011 name = tpl_global_var % name_full
1011 name = tpl_global_var % name_full
1012 lvals.append(tpl_name_val % (name, value))
1012 lvals.append(tpl_name_val % (name, value))
1013 if lvals:
1013 if lvals:
1014 lvals = '%s%s' % (indent, em_normal.join(lvals))
1014 lvals = '%s%s' % (indent, em_normal.join(lvals))
1015 else:
1015 else:
1016 lvals = ''
1016 lvals = ''
1017
1017
1018 level = '%s %s\n' % (link, call)
1018 level = '%s %s\n' % (link, call)
1019
1019
1020 if index is None:
1020 if index is None:
1021 return level
1021 return level
1022 else:
1022 else:
1023 return '%s%s' % (level, ''.join(
1023 return '%s%s' % (level, ''.join(
1024 _format_traceback_lines(lnum, index, lines, Colors, lvals,
1024 _format_traceback_lines(lnum, index, lines, Colors, lvals,
1025 col_scheme)))
1025 col_scheme)))
1026
1026
1027 def prepare_chained_exception_message(self, cause):
1027 def prepare_chained_exception_message(self, cause):
1028 direct_cause = "\nThe above exception was the direct cause of the following exception:\n"
1028 direct_cause = "\nThe above exception was the direct cause of the following exception:\n"
1029 exception_during_handling = "\nDuring handling of the above exception, another exception occurred:\n"
1029 exception_during_handling = "\nDuring handling of the above exception, another exception occurred:\n"
1030
1030
1031 if cause:
1031 if cause:
1032 message = [[direct_cause]]
1032 message = [[direct_cause]]
1033 else:
1033 else:
1034 message = [[exception_during_handling]]
1034 message = [[exception_during_handling]]
1035 return message
1035 return message
1036
1036
1037 def prepare_header(self, etype, long_version=False):
1037 def prepare_header(self, etype, long_version=False):
1038 colors = self.Colors # just a shorthand + quicker name lookup
1038 colors = self.Colors # just a shorthand + quicker name lookup
1039 colorsnormal = colors.Normal # used a lot
1039 colorsnormal = colors.Normal # used a lot
1040 exc = '%s%s%s' % (colors.excName, etype, colorsnormal)
1040 exc = '%s%s%s' % (colors.excName, etype, colorsnormal)
1041 width = min(75, get_terminal_size()[0])
1041 width = min(75, get_terminal_size()[0])
1042 if long_version:
1042 if long_version:
1043 # Header with the exception type, python version, and date
1043 # Header with the exception type, python version, and date
1044 pyver = 'Python ' + sys.version.split()[0] + ': ' + sys.executable
1044 pyver = 'Python ' + sys.version.split()[0] + ': ' + sys.executable
1045 date = time.ctime(time.time())
1045 date = time.ctime(time.time())
1046
1046
1047 head = '%s%s%s\n%s%s%s\n%s' % (colors.topline, '-' * width, colorsnormal,
1047 head = '%s%s%s\n%s%s%s\n%s' % (colors.topline, '-' * width, colorsnormal,
1048 exc, ' ' * (width - len(str(etype)) - len(pyver)),
1048 exc, ' ' * (width - len(str(etype)) - len(pyver)),
1049 pyver, date.rjust(width) )
1049 pyver, date.rjust(width) )
1050 head += "\nA problem occurred executing Python code. Here is the sequence of function" \
1050 head += "\nA problem occurred executing Python code. Here is the sequence of function" \
1051 "\ncalls leading up to the error, with the most recent (innermost) call last."
1051 "\ncalls leading up to the error, with the most recent (innermost) call last."
1052 else:
1052 else:
1053 # Simplified header
1053 # Simplified header
1054 head = '%s%s' % (exc, 'Traceback (most recent call last)'. \
1054 head = '%s%s' % (exc, 'Traceback (most recent call last)'. \
1055 rjust(width - len(str(etype))) )
1055 rjust(width - len(str(etype))) )
1056
1056
1057 return head
1057 return head
1058
1058
1059 def format_exception(self, etype, evalue):
1059 def format_exception(self, etype, evalue):
1060 colors = self.Colors # just a shorthand + quicker name lookup
1060 colors = self.Colors # just a shorthand + quicker name lookup
1061 colorsnormal = colors.Normal # used a lot
1061 colorsnormal = colors.Normal # used a lot
1062 indent = ' ' * INDENT_SIZE
1062 indent = ' ' * INDENT_SIZE
1063 # Get (safely) a string form of the exception info
1063 # Get (safely) a string form of the exception info
1064 try:
1064 try:
1065 etype_str, evalue_str = map(str, (etype, evalue))
1065 etype_str, evalue_str = map(str, (etype, evalue))
1066 except:
1066 except:
1067 # User exception is improperly defined.
1067 # User exception is improperly defined.
1068 etype, evalue = str, sys.exc_info()[:2]
1068 etype, evalue = str, sys.exc_info()[:2]
1069 etype_str, evalue_str = map(str, (etype, evalue))
1069 etype_str, evalue_str = map(str, (etype, evalue))
1070 # ... and format it
1070 # ... and format it
1071 exception = ['%s%s%s: %s' % (colors.excName, etype_str,
1071 exception = ['%s%s%s: %s' % (colors.excName, etype_str,
1072 colorsnormal, py3compat.cast_unicode(evalue_str))]
1072 colorsnormal, py3compat.cast_unicode(evalue_str))]
1073
1073
1074 if (not py3compat.PY3) and type(evalue) is types.InstanceType:
1074 if (not py3compat.PY3) and type(evalue) is types.InstanceType:
1075 try:
1075 try:
1076 names = [w for w in dir(evalue) if isinstance(w, py3compat.string_types)]
1076 names = [w for w in dir(evalue) if isinstance(w, py3compat.string_types)]
1077 except:
1077 except:
1078 # Every now and then, an object with funny internals blows up
1078 # Every now and then, an object with funny internals blows up
1079 # when dir() is called on it. We do the best we can to report
1079 # when dir() is called on it. We do the best we can to report
1080 # the problem and continue
1080 # the problem and continue
1081 _m = '%sException reporting error (object with broken dir())%s:'
1081 _m = '%sException reporting error (object with broken dir())%s:'
1082 exception.append(_m % (colors.excName, colorsnormal))
1082 exception.append(_m % (colors.excName, colorsnormal))
1083 etype_str, evalue_str = map(str, sys.exc_info()[:2])
1083 etype_str, evalue_str = map(str, sys.exc_info()[:2])
1084 exception.append('%s%s%s: %s' % (colors.excName, etype_str,
1084 exception.append('%s%s%s: %s' % (colors.excName, etype_str,
1085 colorsnormal, py3compat.cast_unicode(evalue_str)))
1085 colorsnormal, py3compat.cast_unicode(evalue_str)))
1086 names = []
1086 names = []
1087 for name in names:
1087 for name in names:
1088 value = text_repr(getattr(evalue, name))
1088 value = text_repr(getattr(evalue, name))
1089 exception.append('\n%s%s = %s' % (indent, name, value))
1089 exception.append('\n%s%s = %s' % (indent, name, value))
1090
1090
1091 return exception
1091 return exception
1092
1092
1093 def format_exception_as_a_whole(self, etype, evalue, etb, number_of_lines_of_context, tb_offset):
1093 def format_exception_as_a_whole(self, etype, evalue, etb, number_of_lines_of_context, tb_offset):
1094 """Formats the header, traceback and exception message for a single exception.
1094 """Formats the header, traceback and exception message for a single exception.
1095
1095
1096 This may be called multiple times by Python 3 exception chaining
1096 This may be called multiple times by Python 3 exception chaining
1097 (PEP 3134).
1097 (PEP 3134).
1098 """
1098 """
1099 # some locals
1099 # some locals
1100 orig_etype = etype
1100 orig_etype = etype
1101 try:
1101 try:
1102 etype = etype.__name__
1102 etype = etype.__name__
1103 except AttributeError:
1103 except AttributeError:
1104 pass
1104 pass
1105
1105
1106 tb_offset = self.tb_offset if tb_offset is None else tb_offset
1106 tb_offset = self.tb_offset if tb_offset is None else tb_offset
1107 head = self.prepare_header(etype, self.long_header)
1107 head = self.prepare_header(etype, self.long_header)
1108 records = self.get_records(etb, number_of_lines_of_context, tb_offset)
1108 records = self.get_records(etb, number_of_lines_of_context, tb_offset)
1109
1109
1110 if records is None:
1110 if records is None:
1111 return ""
1111 return ""
1112
1112
1113 last_unique, recursion_repeat = find_recursion(orig_etype, evalue, records)
1113 last_unique, recursion_repeat = find_recursion(orig_etype, evalue, records)
1114
1114
1115 frames = self.format_records(records, last_unique, recursion_repeat)
1115 frames = self.format_records(records, last_unique, recursion_repeat)
1116
1116
1117 formatted_exception = self.format_exception(etype, evalue)
1117 formatted_exception = self.format_exception(etype, evalue)
1118 if records:
1118 if records:
1119 filepath, lnum = records[-1][1:3]
1119 filepath, lnum = records[-1][1:3]
1120 filepath = os.path.abspath(filepath)
1120 filepath = os.path.abspath(filepath)
1121 ipinst = get_ipython()
1121 ipinst = get_ipython()
1122 if ipinst is not None:
1122 if ipinst is not None:
1123 ipinst.hooks.synchronize_with_editor(filepath, lnum, 0)
1123 ipinst.hooks.synchronize_with_editor(filepath, lnum, 0)
1124
1124
1125 return [[head] + frames + [''.join(formatted_exception[0])]]
1125 return [[head] + frames + [''.join(formatted_exception[0])]]
1126
1126
1127 def get_records(self, etb, number_of_lines_of_context, tb_offset):
1127 def get_records(self, etb, number_of_lines_of_context, tb_offset):
1128 try:
1128 try:
1129 # Try the default getinnerframes and Alex's: Alex's fixes some
1129 # Try the default getinnerframes and Alex's: Alex's fixes some
1130 # problems, but it generates empty tracebacks for console errors
1130 # problems, but it generates empty tracebacks for console errors
1131 # (5 blanks lines) where none should be returned.
1131 # (5 blanks lines) where none should be returned.
1132 return _fixed_getinnerframes(etb, number_of_lines_of_context, tb_offset)
1132 return _fixed_getinnerframes(etb, number_of_lines_of_context, tb_offset)
1133 except:
1133 except:
1134 # FIXME: I've been getting many crash reports from python 2.3
1134 # FIXME: I've been getting many crash reports from python 2.3
1135 # users, traceable to inspect.py. If I can find a small test-case
1135 # users, traceable to inspect.py. If I can find a small test-case
1136 # to reproduce this, I should either write a better workaround or
1136 # to reproduce this, I should either write a better workaround or
1137 # file a bug report against inspect (if that's the real problem).
1137 # file a bug report against inspect (if that's the real problem).
1138 # So far, I haven't been able to find an isolated example to
1138 # So far, I haven't been able to find an isolated example to
1139 # reproduce the problem.
1139 # reproduce the problem.
1140 inspect_error()
1140 inspect_error()
1141 traceback.print_exc(file=self.ostream)
1141 traceback.print_exc(file=self.ostream)
1142 info('\nUnfortunately, your original traceback can not be constructed.\n')
1142 info('\nUnfortunately, your original traceback can not be constructed.\n')
1143 return None
1143 return None
1144
1144
1145 def get_parts_of_chained_exception(self, evalue):
1145 def get_parts_of_chained_exception(self, evalue):
1146 def get_chained_exception(exception_value):
1146 def get_chained_exception(exception_value):
1147 cause = getattr(exception_value, '__cause__', None)
1147 cause = getattr(exception_value, '__cause__', None)
1148 if cause:
1148 if cause:
1149 return cause
1149 return cause
1150 if getattr(exception_value, '__suppress_context__', False):
1150 if getattr(exception_value, '__suppress_context__', False):
1151 return None
1151 return None
1152 return getattr(exception_value, '__context__', None)
1152 return getattr(exception_value, '__context__', None)
1153
1153
1154 chained_evalue = get_chained_exception(evalue)
1154 chained_evalue = get_chained_exception(evalue)
1155
1155
1156 if chained_evalue:
1156 if chained_evalue:
1157 return chained_evalue.__class__, chained_evalue, chained_evalue.__traceback__
1157 return chained_evalue.__class__, chained_evalue, chained_evalue.__traceback__
1158
1158
1159 def structured_traceback(self, etype, evalue, etb, tb_offset=None,
1159 def structured_traceback(self, etype, evalue, etb, tb_offset=None,
1160 number_of_lines_of_context=5):
1160 number_of_lines_of_context=5):
1161 """Return a nice text document describing the traceback."""
1161 """Return a nice text document describing the traceback."""
1162
1162
1163 formatted_exception = self.format_exception_as_a_whole(etype, evalue, etb, number_of_lines_of_context,
1163 formatted_exception = self.format_exception_as_a_whole(etype, evalue, etb, number_of_lines_of_context,
1164 tb_offset)
1164 tb_offset)
1165
1165
1166 colors = self.Colors # just a shorthand + quicker name lookup
1166 colors = self.Colors # just a shorthand + quicker name lookup
1167 colorsnormal = colors.Normal # used a lot
1167 colorsnormal = colors.Normal # used a lot
1168 head = '%s%s%s' % (colors.topline, '-' * min(75, get_terminal_size()[0]), colorsnormal)
1168 head = '%s%s%s' % (colors.topline, '-' * min(75, get_terminal_size()[0]), colorsnormal)
1169 structured_traceback_parts = [head]
1169 structured_traceback_parts = [head]
1170 if py3compat.PY3:
1170 if py3compat.PY3:
1171 chained_exceptions_tb_offset = 0
1171 chained_exceptions_tb_offset = 0
1172 lines_of_context = 3
1172 lines_of_context = 3
1173 formatted_exceptions = formatted_exception
1173 formatted_exceptions = formatted_exception
1174 exception = self.get_parts_of_chained_exception(evalue)
1174 exception = self.get_parts_of_chained_exception(evalue)
1175 if exception:
1175 if exception:
1176 formatted_exceptions += self.prepare_chained_exception_message(evalue.__cause__)
1176 formatted_exceptions += self.prepare_chained_exception_message(evalue.__cause__)
1177 etype, evalue, etb = exception
1177 etype, evalue, etb = exception
1178 else:
1178 else:
1179 evalue = None
1179 evalue = None
1180 chained_exc_ids = set()
1180 chained_exc_ids = set()
1181 while evalue:
1181 while evalue:
1182 formatted_exceptions += self.format_exception_as_a_whole(etype, evalue, etb, lines_of_context,
1182 formatted_exceptions += self.format_exception_as_a_whole(etype, evalue, etb, lines_of_context,
1183 chained_exceptions_tb_offset)
1183 chained_exceptions_tb_offset)
1184 exception = self.get_parts_of_chained_exception(evalue)
1184 exception = self.get_parts_of_chained_exception(evalue)
1185
1185
1186 if exception and not id(exception[1]) in chained_exc_ids:
1186 if exception and not id(exception[1]) in chained_exc_ids:
1187 chained_exc_ids.add(id(exception[1])) # trace exception to avoid infinite 'cause' loop
1187 chained_exc_ids.add(id(exception[1])) # trace exception to avoid infinite 'cause' loop
1188 formatted_exceptions += self.prepare_chained_exception_message(evalue.__cause__)
1188 formatted_exceptions += self.prepare_chained_exception_message(evalue.__cause__)
1189 etype, evalue, etb = exception
1189 etype, evalue, etb = exception
1190 else:
1190 else:
1191 evalue = None
1191 evalue = None
1192
1192
1193 # we want to see exceptions in a reversed order:
1193 # we want to see exceptions in a reversed order:
1194 # the first exception should be on top
1194 # the first exception should be on top
1195 for formatted_exception in reversed(formatted_exceptions):
1195 for formatted_exception in reversed(formatted_exceptions):
1196 structured_traceback_parts += formatted_exception
1196 structured_traceback_parts += formatted_exception
1197 else:
1197 else:
1198 structured_traceback_parts += formatted_exception[0]
1198 structured_traceback_parts += formatted_exception[0]
1199
1199
1200 return structured_traceback_parts
1200 return structured_traceback_parts
1201
1201
1202 def debugger(self, force=False):
1202 def debugger(self, force=False):
1203 """Call up the pdb debugger if desired, always clean up the tb
1203 """Call up the pdb debugger if desired, always clean up the tb
1204 reference.
1204 reference.
1205
1205
1206 Keywords:
1206 Keywords:
1207
1207
1208 - force(False): by default, this routine checks the instance call_pdb
1208 - force(False): by default, this routine checks the instance call_pdb
1209 flag and does not actually invoke the debugger if the flag is false.
1209 flag and does not actually invoke the debugger if the flag is false.
1210 The 'force' option forces the debugger to activate even if the flag
1210 The 'force' option forces the debugger to activate even if the flag
1211 is false.
1211 is false.
1212
1212
1213 If the call_pdb flag is set, the pdb interactive debugger is
1213 If the call_pdb flag is set, the pdb interactive debugger is
1214 invoked. In all cases, the self.tb reference to the current traceback
1214 invoked. In all cases, the self.tb reference to the current traceback
1215 is deleted to prevent lingering references which hamper memory
1215 is deleted to prevent lingering references which hamper memory
1216 management.
1216 management.
1217
1217
1218 Note that each call to pdb() does an 'import readline', so if your app
1218 Note that each call to pdb() does an 'import readline', so if your app
1219 requires a special setup for the readline completers, you'll have to
1219 requires a special setup for the readline completers, you'll have to
1220 fix that by hand after invoking the exception handler."""
1220 fix that by hand after invoking the exception handler."""
1221
1221
1222 if force or self.call_pdb:
1222 if force or self.call_pdb:
1223 if self.pdb is None:
1223 if self.pdb is None:
1224 self.pdb = self.debugger_cls(
1224 self.pdb = self.debugger_cls(
1225 self.color_scheme_table.active_scheme_name)
1225 self.color_scheme_table.active_scheme_name)
1226 # the system displayhook may have changed, restore the original
1226 # the system displayhook may have changed, restore the original
1227 # for pdb
1227 # for pdb
1228 display_trap = DisplayTrap(hook=sys.__displayhook__)
1228 display_trap = DisplayTrap(hook=sys.__displayhook__)
1229 with display_trap:
1229 with display_trap:
1230 self.pdb.reset()
1230 self.pdb.reset()
1231 # Find the right frame so we don't pop up inside ipython itself
1231 # Find the right frame so we don't pop up inside ipython itself
1232 if hasattr(self, 'tb') and self.tb is not None:
1232 if hasattr(self, 'tb') and self.tb is not None:
1233 etb = self.tb
1233 etb = self.tb
1234 else:
1234 else:
1235 etb = self.tb = sys.last_traceback
1235 etb = self.tb = sys.last_traceback
1236 while self.tb is not None and self.tb.tb_next is not None:
1236 while self.tb is not None and self.tb.tb_next is not None:
1237 self.tb = self.tb.tb_next
1237 self.tb = self.tb.tb_next
1238 if etb and etb.tb_next:
1238 if etb and etb.tb_next:
1239 etb = etb.tb_next
1239 etb = etb.tb_next
1240 self.pdb.botframe = etb.tb_frame
1240 self.pdb.botframe = etb.tb_frame
1241 self.pdb.interaction(self.tb.tb_frame, self.tb)
1241 self.pdb.interaction(self.tb.tb_frame, self.tb)
1242
1242
1243 if hasattr(self, 'tb'):
1243 if hasattr(self, 'tb'):
1244 del self.tb
1244 del self.tb
1245
1245
1246 def handler(self, info=None):
1246 def handler(self, info=None):
1247 (etype, evalue, etb) = info or sys.exc_info()
1247 (etype, evalue, etb) = info or sys.exc_info()
1248 self.tb = etb
1248 self.tb = etb
1249 ostream = self.ostream
1249 ostream = self.ostream
1250 ostream.flush()
1250 ostream.flush()
1251 ostream.write(self.text(etype, evalue, etb))
1251 ostream.write(self.text(etype, evalue, etb))
1252 ostream.write('\n')
1252 ostream.write('\n')
1253 ostream.flush()
1253 ostream.flush()
1254
1254
1255 # Changed so an instance can just be called as VerboseTB_inst() and print
1255 # Changed so an instance can just be called as VerboseTB_inst() and print
1256 # out the right info on its own.
1256 # out the right info on its own.
1257 def __call__(self, etype=None, evalue=None, etb=None):
1257 def __call__(self, etype=None, evalue=None, etb=None):
1258 """This hook can replace sys.excepthook (for Python 2.1 or higher)."""
1258 """This hook can replace sys.excepthook (for Python 2.1 or higher)."""
1259 if etb is None:
1259 if etb is None:
1260 self.handler()
1260 self.handler()
1261 else:
1261 else:
1262 self.handler((etype, evalue, etb))
1262 self.handler((etype, evalue, etb))
1263 try:
1263 try:
1264 self.debugger()
1264 self.debugger()
1265 except KeyboardInterrupt:
1265 except KeyboardInterrupt:
1266 print("\nKeyboardInterrupt")
1266 print("\nKeyboardInterrupt")
1267
1267
1268
1268
1269 #----------------------------------------------------------------------------
1269 #----------------------------------------------------------------------------
1270 class FormattedTB(VerboseTB, ListTB):
1270 class FormattedTB(VerboseTB, ListTB):
1271 """Subclass ListTB but allow calling with a traceback.
1271 """Subclass ListTB but allow calling with a traceback.
1272
1272
1273 It can thus be used as a sys.excepthook for Python > 2.1.
1273 It can thus be used as a sys.excepthook for Python > 2.1.
1274
1274
1275 Also adds 'Context' and 'Verbose' modes, not available in ListTB.
1275 Also adds 'Context' and 'Verbose' modes, not available in ListTB.
1276
1276
1277 Allows a tb_offset to be specified. This is useful for situations where
1277 Allows a tb_offset to be specified. This is useful for situations where
1278 one needs to remove a number of topmost frames from the traceback (such as
1278 one needs to remove a number of topmost frames from the traceback (such as
1279 occurs with python programs that themselves execute other python code,
1279 occurs with python programs that themselves execute other python code,
1280 like Python shells). """
1280 like Python shells). """
1281
1281
1282 def __init__(self, mode='Plain', color_scheme='Linux', call_pdb=False,
1282 def __init__(self, mode='Plain', color_scheme='Linux', call_pdb=False,
1283 ostream=None,
1283 ostream=None,
1284 tb_offset=0, long_header=False, include_vars=False,
1284 tb_offset=0, long_header=False, include_vars=False,
1285 check_cache=None, debugger_cls=None):
1285 check_cache=None, debugger_cls=None):
1286
1286
1287 # NEVER change the order of this list. Put new modes at the end:
1287 # NEVER change the order of this list. Put new modes at the end:
1288 self.valid_modes = ['Plain', 'Context', 'Verbose']
1288 self.valid_modes = ['Plain', 'Context', 'Verbose']
1289 self.verbose_modes = self.valid_modes[1:3]
1289 self.verbose_modes = self.valid_modes[1:3]
1290
1290
1291 VerboseTB.__init__(self, color_scheme=color_scheme, call_pdb=call_pdb,
1291 VerboseTB.__init__(self, color_scheme=color_scheme, call_pdb=call_pdb,
1292 ostream=ostream, tb_offset=tb_offset,
1292 ostream=ostream, tb_offset=tb_offset,
1293 long_header=long_header, include_vars=include_vars,
1293 long_header=long_header, include_vars=include_vars,
1294 check_cache=check_cache, debugger_cls=debugger_cls)
1294 check_cache=check_cache, debugger_cls=debugger_cls)
1295
1295
1296 # Different types of tracebacks are joined with different separators to
1296 # Different types of tracebacks are joined with different separators to
1297 # form a single string. They are taken from this dict
1297 # form a single string. They are taken from this dict
1298 self._join_chars = dict(Plain='', Context='\n', Verbose='\n')
1298 self._join_chars = dict(Plain='', Context='\n', Verbose='\n')
1299 # set_mode also sets the tb_join_char attribute
1299 # set_mode also sets the tb_join_char attribute
1300 self.set_mode(mode)
1300 self.set_mode(mode)
1301
1301
1302 def _extract_tb(self, tb):
1302 def _extract_tb(self, tb):
1303 if tb:
1303 if tb:
1304 return traceback.extract_tb(tb)
1304 return traceback.extract_tb(tb)
1305 else:
1305 else:
1306 return None
1306 return None
1307
1307
1308 def structured_traceback(self, etype, value, tb, tb_offset=None, number_of_lines_of_context=5):
1308 def structured_traceback(self, etype, value, tb, tb_offset=None, number_of_lines_of_context=5):
1309 tb_offset = self.tb_offset if tb_offset is None else tb_offset
1309 tb_offset = self.tb_offset if tb_offset is None else tb_offset
1310 mode = self.mode
1310 mode = self.mode
1311 if mode in self.verbose_modes:
1311 if mode in self.verbose_modes:
1312 # Verbose modes need a full traceback
1312 # Verbose modes need a full traceback
1313 return VerboseTB.structured_traceback(
1313 return VerboseTB.structured_traceback(
1314 self, etype, value, tb, tb_offset, number_of_lines_of_context
1314 self, etype, value, tb, tb_offset, number_of_lines_of_context
1315 )
1315 )
1316 else:
1316 else:
1317 # We must check the source cache because otherwise we can print
1317 # We must check the source cache because otherwise we can print
1318 # out-of-date source code.
1318 # out-of-date source code.
1319 self.check_cache()
1319 self.check_cache()
1320 # Now we can extract and format the exception
1320 # Now we can extract and format the exception
1321 elist = self._extract_tb(tb)
1321 elist = self._extract_tb(tb)
1322 return ListTB.structured_traceback(
1322 return ListTB.structured_traceback(
1323 self, etype, value, elist, tb_offset, number_of_lines_of_context
1323 self, etype, value, elist, tb_offset, number_of_lines_of_context
1324 )
1324 )
1325
1325
1326 def stb2text(self, stb):
1326 def stb2text(self, stb):
1327 """Convert a structured traceback (a list) to a string."""
1327 """Convert a structured traceback (a list) to a string."""
1328 return self.tb_join_char.join(stb)
1328 return self.tb_join_char.join(stb)
1329
1329
1330
1330
1331 def set_mode(self, mode=None):
1331 def set_mode(self, mode=None):
1332 """Switch to the desired mode.
1332 """Switch to the desired mode.
1333
1333
1334 If mode is not specified, cycles through the available modes."""
1334 If mode is not specified, cycles through the available modes."""
1335
1335
1336 if not mode:
1336 if not mode:
1337 new_idx = (self.valid_modes.index(self.mode) + 1 ) % \
1337 new_idx = (self.valid_modes.index(self.mode) + 1 ) % \
1338 len(self.valid_modes)
1338 len(self.valid_modes)
1339 self.mode = self.valid_modes[new_idx]
1339 self.mode = self.valid_modes[new_idx]
1340 elif mode not in self.valid_modes:
1340 elif mode not in self.valid_modes:
1341 raise ValueError('Unrecognized mode in FormattedTB: <' + mode + '>\n'
1341 raise ValueError('Unrecognized mode in FormattedTB: <' + mode + '>\n'
1342 'Valid modes: ' + str(self.valid_modes))
1342 'Valid modes: ' + str(self.valid_modes))
1343 else:
1343 else:
1344 self.mode = mode
1344 self.mode = mode
1345 # include variable details only in 'Verbose' mode
1345 # include variable details only in 'Verbose' mode
1346 self.include_vars = (self.mode == self.valid_modes[2])
1346 self.include_vars = (self.mode == self.valid_modes[2])
1347 # Set the join character for generating text tracebacks
1347 # Set the join character for generating text tracebacks
1348 self.tb_join_char = self._join_chars[self.mode]
1348 self.tb_join_char = self._join_chars[self.mode]
1349
1349
1350 # some convenient shortcuts
1350 # some convenient shortcuts
1351 def plain(self):
1351 def plain(self):
1352 self.set_mode(self.valid_modes[0])
1352 self.set_mode(self.valid_modes[0])
1353
1353
1354 def context(self):
1354 def context(self):
1355 self.set_mode(self.valid_modes[1])
1355 self.set_mode(self.valid_modes[1])
1356
1356
1357 def verbose(self):
1357 def verbose(self):
1358 self.set_mode(self.valid_modes[2])
1358 self.set_mode(self.valid_modes[2])
1359
1359
1360
1360
1361 #----------------------------------------------------------------------------
1361 #----------------------------------------------------------------------------
1362 class AutoFormattedTB(FormattedTB):
1362 class AutoFormattedTB(FormattedTB):
1363 """A traceback printer which can be called on the fly.
1363 """A traceback printer which can be called on the fly.
1364
1364
1365 It will find out about exceptions by itself.
1365 It will find out about exceptions by itself.
1366
1366
1367 A brief example::
1367 A brief example::
1368
1368
1369 AutoTB = AutoFormattedTB(mode = 'Verbose',color_scheme='Linux')
1369 AutoTB = AutoFormattedTB(mode = 'Verbose',color_scheme='Linux')
1370 try:
1370 try:
1371 ...
1371 ...
1372 except:
1372 except:
1373 AutoTB() # or AutoTB(out=logfile) where logfile is an open file object
1373 AutoTB() # or AutoTB(out=logfile) where logfile is an open file object
1374 """
1374 """
1375
1375
1376 def __call__(self, etype=None, evalue=None, etb=None,
1376 def __call__(self, etype=None, evalue=None, etb=None,
1377 out=None, tb_offset=None):
1377 out=None, tb_offset=None):
1378 """Print out a formatted exception traceback.
1378 """Print out a formatted exception traceback.
1379
1379
1380 Optional arguments:
1380 Optional arguments:
1381 - out: an open file-like object to direct output to.
1381 - out: an open file-like object to direct output to.
1382
1382
1383 - tb_offset: the number of frames to skip over in the stack, on a
1383 - tb_offset: the number of frames to skip over in the stack, on a
1384 per-call basis (this overrides temporarily the instance's tb_offset
1384 per-call basis (this overrides temporarily the instance's tb_offset
1385 given at initialization time. """
1385 given at initialization time. """
1386
1386
1387 if out is None:
1387 if out is None:
1388 out = self.ostream
1388 out = self.ostream
1389 out.flush()
1389 out.flush()
1390 out.write(self.text(etype, evalue, etb, tb_offset))
1390 out.write(self.text(etype, evalue, etb, tb_offset))
1391 out.write('\n')
1391 out.write('\n')
1392 out.flush()
1392 out.flush()
1393 # FIXME: we should remove the auto pdb behavior from here and leave
1393 # FIXME: we should remove the auto pdb behavior from here and leave
1394 # that to the clients.
1394 # that to the clients.
1395 try:
1395 try:
1396 self.debugger()
1396 self.debugger()
1397 except KeyboardInterrupt:
1397 except KeyboardInterrupt:
1398 print("\nKeyboardInterrupt")
1398 print("\nKeyboardInterrupt")
1399
1399
1400 def structured_traceback(self, etype=None, value=None, tb=None,
1400 def structured_traceback(self, etype=None, value=None, tb=None,
1401 tb_offset=None, number_of_lines_of_context=5):
1401 tb_offset=None, number_of_lines_of_context=5):
1402 if etype is None:
1402 if etype is None:
1403 etype, value, tb = sys.exc_info()
1403 etype, value, tb = sys.exc_info()
1404 self.tb = tb
1404 self.tb = tb
1405 return FormattedTB.structured_traceback(
1405 return FormattedTB.structured_traceback(
1406 self, etype, value, tb, tb_offset, number_of_lines_of_context)
1406 self, etype, value, tb, tb_offset, number_of_lines_of_context)
1407
1407
1408
1408
1409 #---------------------------------------------------------------------------
1409 #---------------------------------------------------------------------------
1410
1410
1411 # A simple class to preserve Nathan's original functionality.
1411 # A simple class to preserve Nathan's original functionality.
1412 class ColorTB(FormattedTB):
1412 class ColorTB(FormattedTB):
1413 """Shorthand to initialize a FormattedTB in Linux colors mode."""
1413 """Shorthand to initialize a FormattedTB in Linux colors mode."""
1414
1414
1415 def __init__(self, color_scheme='Linux', call_pdb=0, **kwargs):
1415 def __init__(self, color_scheme='Linux', call_pdb=0, **kwargs):
1416 FormattedTB.__init__(self, color_scheme=color_scheme,
1416 FormattedTB.__init__(self, color_scheme=color_scheme,
1417 call_pdb=call_pdb, **kwargs)
1417 call_pdb=call_pdb, **kwargs)
1418
1418
1419
1419
1420 class SyntaxTB(ListTB):
1420 class SyntaxTB(ListTB):
1421 """Extension which holds some state: the last exception value"""
1421 """Extension which holds some state: the last exception value"""
1422
1422
1423 def __init__(self, color_scheme='NoColor'):
1423 def __init__(self, color_scheme='NoColor'):
1424 ListTB.__init__(self, color_scheme)
1424 ListTB.__init__(self, color_scheme)
1425 self.last_syntax_error = None
1425 self.last_syntax_error = None
1426
1426
1427 def __call__(self, etype, value, elist):
1427 def __call__(self, etype, value, elist):
1428 self.last_syntax_error = value
1428 self.last_syntax_error = value
1429
1429
1430 ListTB.__call__(self, etype, value, elist)
1430 ListTB.__call__(self, etype, value, elist)
1431
1431
1432 def structured_traceback(self, etype, value, elist, tb_offset=None,
1432 def structured_traceback(self, etype, value, elist, tb_offset=None,
1433 context=5):
1433 context=5):
1434 # If the source file has been edited, the line in the syntax error can
1434 # If the source file has been edited, the line in the syntax error can
1435 # be wrong (retrieved from an outdated cache). This replaces it with
1435 # be wrong (retrieved from an outdated cache). This replaces it with
1436 # the current value.
1436 # the current value.
1437 if isinstance(value, SyntaxError) \
1437 if isinstance(value, SyntaxError) \
1438 and isinstance(value.filename, py3compat.string_types) \
1438 and isinstance(value.filename, py3compat.string_types) \
1439 and isinstance(value.lineno, int):
1439 and isinstance(value.lineno, int):
1440 linecache.checkcache(value.filename)
1440 linecache.checkcache(value.filename)
1441 newtext = ulinecache.getline(value.filename, value.lineno)
1441 newtext = ulinecache.getline(value.filename, value.lineno)
1442 if newtext:
1442 if newtext:
1443 value.text = newtext
1443 value.text = newtext
1444 self.last_syntax_error = value
1444 self.last_syntax_error = value
1445 return super(SyntaxTB, self).structured_traceback(etype, value, elist,
1445 return super(SyntaxTB, self).structured_traceback(etype, value, elist,
1446 tb_offset=tb_offset, context=context)
1446 tb_offset=tb_offset, context=context)
1447
1447
1448 def clear_err_state(self):
1448 def clear_err_state(self):
1449 """Return the current error state and clear it"""
1449 """Return the current error state and clear it"""
1450 e = self.last_syntax_error
1450 e = self.last_syntax_error
1451 self.last_syntax_error = None
1451 self.last_syntax_error = None
1452 return e
1452 return e
1453
1453
1454 def stb2text(self, stb):
1454 def stb2text(self, stb):
1455 """Convert a structured traceback (a list) to a string."""
1455 """Convert a structured traceback (a list) to a string."""
1456 return ''.join(stb)
1456 return ''.join(stb)
1457
1457
1458
1458
1459 # some internal-use functions
1459 # some internal-use functions
1460 def text_repr(value):
1460 def text_repr(value):
1461 """Hopefully pretty robust repr equivalent."""
1461 """Hopefully pretty robust repr equivalent."""
1462 # this is pretty horrible but should always return *something*
1462 # this is pretty horrible but should always return *something*
1463 try:
1463 try:
1464 return pydoc.text.repr(value)
1464 return pydoc.text.repr(value)
1465 except KeyboardInterrupt:
1465 except KeyboardInterrupt:
1466 raise
1466 raise
1467 except:
1467 except:
1468 try:
1468 try:
1469 return repr(value)
1469 return repr(value)
1470 except KeyboardInterrupt:
1470 except KeyboardInterrupt:
1471 raise
1471 raise
1472 except:
1472 except:
1473 try:
1473 try:
1474 # all still in an except block so we catch
1474 # all still in an except block so we catch
1475 # getattr raising
1475 # getattr raising
1476 name = getattr(value, '__name__', None)
1476 name = getattr(value, '__name__', None)
1477 if name:
1477 if name:
1478 # ick, recursion
1478 # ick, recursion
1479 return text_repr(name)
1479 return text_repr(name)
1480 klass = getattr(value, '__class__', None)
1480 klass = getattr(value, '__class__', None)
1481 if klass:
1481 if klass:
1482 return '%s instance' % text_repr(klass)
1482 return '%s instance' % text_repr(klass)
1483 except KeyboardInterrupt:
1483 except KeyboardInterrupt:
1484 raise
1484 raise
1485 except:
1485 except:
1486 return 'UNRECOVERABLE REPR FAILURE'
1486 return 'UNRECOVERABLE REPR FAILURE'
1487
1487
1488
1488
1489 def eqrepr(value, repr=text_repr):
1489 def eqrepr(value, repr=text_repr):
1490 return '=%s' % repr(value)
1490 return '=%s' % repr(value)
1491
1491
1492
1492
1493 def nullrepr(value, repr=text_repr):
1493 def nullrepr(value, repr=text_repr):
1494 return ''
1494 return ''
@@ -1,583 +1,583 b''
1 """Module for interactive demos using IPython.
1 """Module for interactive demos using IPython.
2
2
3 This module implements a few classes for running Python scripts interactively
3 This module implements a few classes for running Python scripts interactively
4 in IPython for demonstrations. With very simple markup (a few tags in
4 in IPython for demonstrations. With very simple markup (a few tags in
5 comments), you can control points where the script stops executing and returns
5 comments), you can control points where the script stops executing and returns
6 control to IPython.
6 control to IPython.
7
7
8
8
9 Provided classes
9 Provided classes
10 ----------------
10 ----------------
11
11
12 The classes are (see their docstrings for further details):
12 The classes are (see their docstrings for further details):
13
13
14 - Demo: pure python demos
14 - Demo: pure python demos
15
15
16 - IPythonDemo: demos with input to be processed by IPython as if it had been
16 - IPythonDemo: demos with input to be processed by IPython as if it had been
17 typed interactively (so magics work, as well as any other special syntax you
17 typed interactively (so magics work, as well as any other special syntax you
18 may have added via input prefilters).
18 may have added via input prefilters).
19
19
20 - LineDemo: single-line version of the Demo class. These demos are executed
20 - LineDemo: single-line version of the Demo class. These demos are executed
21 one line at a time, and require no markup.
21 one line at a time, and require no markup.
22
22
23 - IPythonLineDemo: IPython version of the LineDemo class (the demo is
23 - IPythonLineDemo: IPython version of the LineDemo class (the demo is
24 executed a line at a time, but processed via IPython).
24 executed a line at a time, but processed via IPython).
25
25
26 - ClearMixin: mixin to make Demo classes with less visual clutter. It
26 - ClearMixin: mixin to make Demo classes with less visual clutter. It
27 declares an empty marquee and a pre_cmd that clears the screen before each
27 declares an empty marquee and a pre_cmd that clears the screen before each
28 block (see Subclassing below).
28 block (see Subclassing below).
29
29
30 - ClearDemo, ClearIPDemo: mixin-enabled versions of the Demo and IPythonDemo
30 - ClearDemo, ClearIPDemo: mixin-enabled versions of the Demo and IPythonDemo
31 classes.
31 classes.
32
32
33 Inheritance diagram:
33 Inheritance diagram:
34
34
35 .. inheritance-diagram:: IPython.lib.demo
35 .. inheritance-diagram:: IPython.lib.demo
36 :parts: 3
36 :parts: 3
37
37
38 Subclassing
38 Subclassing
39 -----------
39 -----------
40
40
41 The classes here all include a few methods meant to make customization by
41 The classes here all include a few methods meant to make customization by
42 subclassing more convenient. Their docstrings below have some more details:
42 subclassing more convenient. Their docstrings below have some more details:
43
43
44 - marquee(): generates a marquee to provide visible on-screen markers at each
44 - marquee(): generates a marquee to provide visible on-screen markers at each
45 block start and end.
45 block start and end.
46
46
47 - pre_cmd(): run right before the execution of each block.
47 - pre_cmd(): run right before the execution of each block.
48
48
49 - post_cmd(): run right after the execution of each block. If the block
49 - post_cmd(): run right after the execution of each block. If the block
50 raises an exception, this is NOT called.
50 raises an exception, this is NOT called.
51
51
52
52
53 Operation
53 Operation
54 ---------
54 ---------
55
55
56 The file is run in its own empty namespace (though you can pass it a string of
56 The file is run in its own empty namespace (though you can pass it a string of
57 arguments as if in a command line environment, and it will see those as
57 arguments as if in a command line environment, and it will see those as
58 sys.argv). But at each stop, the global IPython namespace is updated with the
58 sys.argv). But at each stop, the global IPython namespace is updated with the
59 current internal demo namespace, so you can work interactively with the data
59 current internal demo namespace, so you can work interactively with the data
60 accumulated so far.
60 accumulated so far.
61
61
62 By default, each block of code is printed (with syntax highlighting) before
62 By default, each block of code is printed (with syntax highlighting) before
63 executing it and you have to confirm execution. This is intended to show the
63 executing it and you have to confirm execution. This is intended to show the
64 code to an audience first so you can discuss it, and only proceed with
64 code to an audience first so you can discuss it, and only proceed with
65 execution once you agree. There are a few tags which allow you to modify this
65 execution once you agree. There are a few tags which allow you to modify this
66 behavior.
66 behavior.
67
67
68 The supported tags are:
68 The supported tags are:
69
69
70 # <demo> stop
70 # <demo> stop
71
71
72 Defines block boundaries, the points where IPython stops execution of the
72 Defines block boundaries, the points where IPython stops execution of the
73 file and returns to the interactive prompt.
73 file and returns to the interactive prompt.
74
74
75 You can optionally mark the stop tag with extra dashes before and after the
75 You can optionally mark the stop tag with extra dashes before and after the
76 word 'stop', to help visually distinguish the blocks in a text editor:
76 word 'stop', to help visually distinguish the blocks in a text editor:
77
77
78 # <demo> --- stop ---
78 # <demo> --- stop ---
79
79
80
80
81 # <demo> silent
81 # <demo> silent
82
82
83 Make a block execute silently (and hence automatically). Typically used in
83 Make a block execute silently (and hence automatically). Typically used in
84 cases where you have some boilerplate or initialization code which you need
84 cases where you have some boilerplate or initialization code which you need
85 executed but do not want to be seen in the demo.
85 executed but do not want to be seen in the demo.
86
86
87 # <demo> auto
87 # <demo> auto
88
88
89 Make a block execute automatically, but still being printed. Useful for
89 Make a block execute automatically, but still being printed. Useful for
90 simple code which does not warrant discussion, since it avoids the extra
90 simple code which does not warrant discussion, since it avoids the extra
91 manual confirmation.
91 manual confirmation.
92
92
93 # <demo> auto_all
93 # <demo> auto_all
94
94
95 This tag can _only_ be in the first block, and if given it overrides the
95 This tag can _only_ be in the first block, and if given it overrides the
96 individual auto tags to make the whole demo fully automatic (no block asks
96 individual auto tags to make the whole demo fully automatic (no block asks
97 for confirmation). It can also be given at creation time (or the attribute
97 for confirmation). It can also be given at creation time (or the attribute
98 set later) to override what's in the file.
98 set later) to override what's in the file.
99
99
100 While _any_ python file can be run as a Demo instance, if there are no stop
100 While _any_ python file can be run as a Demo instance, if there are no stop
101 tags the whole file will run in a single block (no different that calling
101 tags the whole file will run in a single block (no different that calling
102 first %pycat and then %run). The minimal markup to make this useful is to
102 first %pycat and then %run). The minimal markup to make this useful is to
103 place a set of stop tags; the other tags are only there to let you fine-tune
103 place a set of stop tags; the other tags are only there to let you fine-tune
104 the execution.
104 the execution.
105
105
106 This is probably best explained with the simple example file below. You can
106 This is probably best explained with the simple example file below. You can
107 copy this into a file named ex_demo.py, and try running it via::
107 copy this into a file named ex_demo.py, and try running it via::
108
108
109 from IPython.demo import Demo
109 from IPython.demo import Demo
110 d = Demo('ex_demo.py')
110 d = Demo('ex_demo.py')
111 d()
111 d()
112
112
113 Each time you call the demo object, it runs the next block. The demo object
113 Each time you call the demo object, it runs the next block. The demo object
114 has a few useful methods for navigation, like again(), edit(), jump(), seek()
114 has a few useful methods for navigation, like again(), edit(), jump(), seek()
115 and back(). It can be reset for a new run via reset() or reloaded from disk
115 and back(). It can be reset for a new run via reset() or reloaded from disk
116 (in case you've edited the source) via reload(). See their docstrings below.
116 (in case you've edited the source) via reload(). See their docstrings below.
117
117
118 Note: To make this simpler to explore, a file called "demo-exercizer.py" has
118 Note: To make this simpler to explore, a file called "demo-exercizer.py" has
119 been added to the "docs/examples/core" directory. Just cd to this directory in
119 been added to the "docs/examples/core" directory. Just cd to this directory in
120 an IPython session, and type::
120 an IPython session, and type::
121
121
122 %run demo-exercizer.py
122 %run demo-exercizer.py
123
123
124 and then follow the directions.
124 and then follow the directions.
125
125
126 Example
126 Example
127 -------
127 -------
128
128
129 The following is a very simple example of a valid demo file.
129 The following is a very simple example of a valid demo file.
130
130
131 ::
131 ::
132
132
133 #################### EXAMPLE DEMO <ex_demo.py> ###############################
133 #################### EXAMPLE DEMO <ex_demo.py> ###############################
134 '''A simple interactive demo to illustrate the use of IPython's Demo class.'''
134 '''A simple interactive demo to illustrate the use of IPython's Demo class.'''
135
135
136 print 'Hello, welcome to an interactive IPython demo.'
136 print 'Hello, welcome to an interactive IPython demo.'
137
137
138 # The mark below defines a block boundary, which is a point where IPython will
138 # The mark below defines a block boundary, which is a point where IPython will
139 # stop execution and return to the interactive prompt. The dashes are actually
139 # stop execution and return to the interactive prompt. The dashes are actually
140 # optional and used only as a visual aid to clearly separate blocks while
140 # optional and used only as a visual aid to clearly separate blocks while
141 # editing the demo code.
141 # editing the demo code.
142 # <demo> stop
142 # <demo> stop
143
143
144 x = 1
144 x = 1
145 y = 2
145 y = 2
146
146
147 # <demo> stop
147 # <demo> stop
148
148
149 # the mark below makes this block as silent
149 # the mark below makes this block as silent
150 # <demo> silent
150 # <demo> silent
151
151
152 print 'This is a silent block, which gets executed but not printed.'
152 print 'This is a silent block, which gets executed but not printed.'
153
153
154 # <demo> stop
154 # <demo> stop
155 # <demo> auto
155 # <demo> auto
156 print 'This is an automatic block.'
156 print 'This is an automatic block.'
157 print 'It is executed without asking for confirmation, but printed.'
157 print 'It is executed without asking for confirmation, but printed.'
158 z = x+y
158 z = x+y
159
159
160 print 'z=',x
160 print 'z=',x
161
161
162 # <demo> stop
162 # <demo> stop
163 # This is just another normal block.
163 # This is just another normal block.
164 print 'z is now:', z
164 print 'z is now:', z
165
165
166 print 'bye!'
166 print 'bye!'
167 ################### END EXAMPLE DEMO <ex_demo.py> ############################
167 ################### END EXAMPLE DEMO <ex_demo.py> ############################
168 """
168 """
169
169
170 from __future__ import unicode_literals
170 from __future__ import unicode_literals
171
171
172 #*****************************************************************************
172 #*****************************************************************************
173 # Copyright (C) 2005-2006 Fernando Perez. <Fernando.Perez@colorado.edu>
173 # Copyright (C) 2005-2006 Fernando Perez. <Fernando.Perez@colorado.edu>
174 #
174 #
175 # Distributed under the terms of the BSD License. The full license is in
175 # Distributed under the terms of the BSD License. The full license is in
176 # the file COPYING, distributed as part of this software.
176 # the file COPYING, distributed as part of this software.
177 #
177 #
178 #*****************************************************************************
178 #*****************************************************************************
179 from __future__ import print_function
179 from __future__ import print_function
180
180
181 import os
181 import os
182 import re
182 import re
183 import shlex
183 import shlex
184 import sys
184 import sys
185
185
186 from IPython.utils import io
186 from IPython.utils import io
187 from IPython.utils.text import marquee
187 from IPython.utils.text import marquee
188 from IPython.utils import openpy
188 from IPython.utils import openpy
189 from IPython.utils import py3compat
189 from IPython.utils import py3compat
190 __all__ = ['Demo','IPythonDemo','LineDemo','IPythonLineDemo','DemoError']
190 __all__ = ['Demo','IPythonDemo','LineDemo','IPythonLineDemo','DemoError']
191
191
192 class DemoError(Exception): pass
192 class DemoError(Exception): pass
193
193
194 def re_mark(mark):
194 def re_mark(mark):
195 return re.compile(r'^\s*#\s+<demo>\s+%s\s*$' % mark,re.MULTILINE)
195 return re.compile(r'^\s*#\s+<demo>\s+%s\s*$' % mark,re.MULTILINE)
196
196
197 class Demo(object):
197 class Demo(object):
198
198
199 re_stop = re_mark('-*\s?stop\s?-*')
199 re_stop = re_mark('-*\s?stop\s?-*')
200 re_silent = re_mark('silent')
200 re_silent = re_mark('silent')
201 re_auto = re_mark('auto')
201 re_auto = re_mark('auto')
202 re_auto_all = re_mark('auto_all')
202 re_auto_all = re_mark('auto_all')
203
203
204 def __init__(self,src,title='',arg_str='',auto_all=None):
204 def __init__(self,src,title='',arg_str='',auto_all=None):
205 """Make a new demo object. To run the demo, simply call the object.
205 """Make a new demo object. To run the demo, simply call the object.
206
206
207 See the module docstring for full details and an example (you can use
207 See the module docstring for full details and an example (you can use
208 IPython.Demo? in IPython to see it).
208 IPython.Demo? in IPython to see it).
209
209
210 Inputs:
210 Inputs:
211
211
212 - src is either a file, or file-like object, or a
212 - src is either a file, or file-like object, or a
213 string that can be resolved to a filename.
213 string that can be resolved to a filename.
214
214
215 Optional inputs:
215 Optional inputs:
216
216
217 - title: a string to use as the demo name. Of most use when the demo
217 - title: a string to use as the demo name. Of most use when the demo
218 you are making comes from an object that has no filename, or if you
218 you are making comes from an object that has no filename, or if you
219 want an alternate denotation distinct from the filename.
219 want an alternate denotation distinct from the filename.
220
220
221 - arg_str(''): a string of arguments, internally converted to a list
221 - arg_str(''): a string of arguments, internally converted to a list
222 just like sys.argv, so the demo script can see a similar
222 just like sys.argv, so the demo script can see a similar
223 environment.
223 environment.
224
224
225 - auto_all(None): global flag to run all blocks automatically without
225 - auto_all(None): global flag to run all blocks automatically without
226 confirmation. This attribute overrides the block-level tags and
226 confirmation. This attribute overrides the block-level tags and
227 applies to the whole demo. It is an attribute of the object, and
227 applies to the whole demo. It is an attribute of the object, and
228 can be changed at runtime simply by reassigning it to a boolean
228 can be changed at runtime simply by reassigning it to a boolean
229 value.
229 value.
230 """
230 """
231 if hasattr(src, "read"):
231 if hasattr(src, "read"):
232 # It seems to be a file or a file-like object
232 # It seems to be a file or a file-like object
233 self.fname = "from a file-like object"
233 self.fname = "from a file-like object"
234 if title == '':
234 if title == '':
235 self.title = "from a file-like object"
235 self.title = "from a file-like object"
236 else:
236 else:
237 self.title = title
237 self.title = title
238 else:
238 else:
239 # Assume it's a string or something that can be converted to one
239 # Assume it's a string or something that can be converted to one
240 self.fname = src
240 self.fname = src
241 if title == '':
241 if title == '':
242 (filepath, filename) = os.path.split(src)
242 (filepath, filename) = os.path.split(src)
243 self.title = filename
243 self.title = filename
244 else:
244 else:
245 self.title = title
245 self.title = title
246 self.sys_argv = [src] + shlex.split(arg_str)
246 self.sys_argv = [src] + shlex.split(arg_str)
247 self.auto_all = auto_all
247 self.auto_all = auto_all
248 self.src = src
248 self.src = src
249
249
250 # get a few things from ipython. While it's a bit ugly design-wise,
250 # get a few things from ipython. While it's a bit ugly design-wise,
251 # it ensures that things like color scheme and the like are always in
251 # it ensures that things like color scheme and the like are always in
252 # sync with the ipython mode being used. This class is only meant to
252 # sync with the ipython mode being used. This class is only meant to
253 # be used inside ipython anyways, so it's OK.
253 # be used inside ipython anyways, so it's OK.
254 ip = get_ipython() # this is in builtins whenever IPython is running
254 ip = get_ipython() # this is in builtins whenever IPython is running
255 self.ip_ns = ip.user_ns
255 self.ip_ns = ip.user_ns
256 self.ip_colorize = ip.pycolorize
256 self.ip_colorize = ip.pycolorize
257 self.ip_showtb = ip.showtraceback
257 self.ip_showtb = ip.showtraceback
258 self.ip_run_cell = ip.run_cell
258 self.ip_run_cell = ip.run_cell
259 self.shell = ip
259 self.shell = ip
260
260
261 # load user data and initialize data structures
261 # load user data and initialize data structures
262 self.reload()
262 self.reload()
263
263
264 def fload(self):
264 def fload(self):
265 """Load file object."""
265 """Load file object."""
266 # read data and parse into blocks
266 # read data and parse into blocks
267 if hasattr(self, 'fobj') and self.fobj is not None:
267 if hasattr(self, 'fobj') and self.fobj is not None:
268 self.fobj.close()
268 self.fobj.close()
269 if hasattr(self.src, "read"):
269 if hasattr(self.src, "read"):
270 # It seems to be a file or a file-like object
270 # It seems to be a file or a file-like object
271 self.fobj = self.src
271 self.fobj = self.src
272 else:
272 else:
273 # Assume it's a string or something that can be converted to one
273 # Assume it's a string or something that can be converted to one
274 self.fobj = openpy.open(self.fname)
274 self.fobj = openpy.open(self.fname)
275
275
276 def reload(self):
276 def reload(self):
277 """Reload source from disk and initialize state."""
277 """Reload source from disk and initialize state."""
278 self.fload()
278 self.fload()
279
279
280 self.src = "".join(openpy.strip_encoding_cookie(self.fobj))
280 self.src = "".join(openpy.strip_encoding_cookie(self.fobj))
281 src_b = [b.strip() for b in self.re_stop.split(self.src) if b]
281 src_b = [b.strip() for b in self.re_stop.split(self.src) if b]
282 self._silent = [bool(self.re_silent.findall(b)) for b in src_b]
282 self._silent = [bool(self.re_silent.findall(b)) for b in src_b]
283 self._auto = [bool(self.re_auto.findall(b)) for b in src_b]
283 self._auto = [bool(self.re_auto.findall(b)) for b in src_b]
284
284
285 # if auto_all is not given (def. None), we read it from the file
285 # if auto_all is not given (def. None), we read it from the file
286 if self.auto_all is None:
286 if self.auto_all is None:
287 self.auto_all = bool(self.re_auto_all.findall(src_b[0]))
287 self.auto_all = bool(self.re_auto_all.findall(src_b[0]))
288 else:
288 else:
289 self.auto_all = bool(self.auto_all)
289 self.auto_all = bool(self.auto_all)
290
290
291 # Clean the sources from all markup so it doesn't get displayed when
291 # Clean the sources from all markup so it doesn't get displayed when
292 # running the demo
292 # running the demo
293 src_blocks = []
293 src_blocks = []
294 auto_strip = lambda s: self.re_auto.sub('',s)
294 auto_strip = lambda s: self.re_auto.sub('',s)
295 for i,b in enumerate(src_b):
295 for i,b in enumerate(src_b):
296 if self._auto[i]:
296 if self._auto[i]:
297 src_blocks.append(auto_strip(b))
297 src_blocks.append(auto_strip(b))
298 else:
298 else:
299 src_blocks.append(b)
299 src_blocks.append(b)
300 # remove the auto_all marker
300 # remove the auto_all marker
301 src_blocks[0] = self.re_auto_all.sub('',src_blocks[0])
301 src_blocks[0] = self.re_auto_all.sub('',src_blocks[0])
302
302
303 self.nblocks = len(src_blocks)
303 self.nblocks = len(src_blocks)
304 self.src_blocks = src_blocks
304 self.src_blocks = src_blocks
305
305
306 # also build syntax-highlighted source
306 # also build syntax-highlighted source
307 self.src_blocks_colored = list(map(self.ip_colorize,self.src_blocks))
307 self.src_blocks_colored = list(map(self.ip_colorize,self.src_blocks))
308
308
309 # ensure clean namespace and seek offset
309 # ensure clean namespace and seek offset
310 self.reset()
310 self.reset()
311
311
312 def reset(self):
312 def reset(self):
313 """Reset the namespace and seek pointer to restart the demo"""
313 """Reset the namespace and seek pointer to restart the demo"""
314 self.user_ns = {}
314 self.user_ns = {}
315 self.finished = False
315 self.finished = False
316 self.block_index = 0
316 self.block_index = 0
317
317
318 def _validate_index(self,index):
318 def _validate_index(self,index):
319 if index<0 or index>=self.nblocks:
319 if index<0 or index>=self.nblocks:
320 raise ValueError('invalid block index %s' % index)
320 raise ValueError('invalid block index %s' % index)
321
321
322 def _get_index(self,index):
322 def _get_index(self,index):
323 """Get the current block index, validating and checking status.
323 """Get the current block index, validating and checking status.
324
324
325 Returns None if the demo is finished"""
325 Returns None if the demo is finished"""
326
326
327 if index is None:
327 if index is None:
328 if self.finished:
328 if self.finished:
329 print('Demo finished. Use <demo_name>.reset() if you want to rerun it.')
329 print('Demo finished. Use <demo_name>.reset() if you want to rerun it.')
330 return None
330 return None
331 index = self.block_index
331 index = self.block_index
332 else:
332 else:
333 self._validate_index(index)
333 self._validate_index(index)
334 return index
334 return index
335
335
336 def seek(self,index):
336 def seek(self,index):
337 """Move the current seek pointer to the given block.
337 """Move the current seek pointer to the given block.
338
338
339 You can use negative indices to seek from the end, with identical
339 You can use negative indices to seek from the end, with identical
340 semantics to those of Python lists."""
340 semantics to those of Python lists."""
341 if index<0:
341 if index<0:
342 index = self.nblocks + index
342 index = self.nblocks + index
343 self._validate_index(index)
343 self._validate_index(index)
344 self.block_index = index
344 self.block_index = index
345 self.finished = False
345 self.finished = False
346
346
347 def back(self,num=1):
347 def back(self,num=1):
348 """Move the seek pointer back num blocks (default is 1)."""
348 """Move the seek pointer back num blocks (default is 1)."""
349 self.seek(self.block_index-num)
349 self.seek(self.block_index-num)
350
350
351 def jump(self,num=1):
351 def jump(self,num=1):
352 """Jump a given number of blocks relative to the current one.
352 """Jump a given number of blocks relative to the current one.
353
353
354 The offset can be positive or negative, defaults to 1."""
354 The offset can be positive or negative, defaults to 1."""
355 self.seek(self.block_index+num)
355 self.seek(self.block_index+num)
356
356
357 def again(self):
357 def again(self):
358 """Move the seek pointer back one block and re-execute."""
358 """Move the seek pointer back one block and re-execute."""
359 self.back(1)
359 self.back(1)
360 self()
360 self()
361
361
362 def edit(self,index=None):
362 def edit(self,index=None):
363 """Edit a block.
363 """Edit a block.
364
364
365 If no number is given, use the last block executed.
365 If no number is given, use the last block executed.
366
366
367 This edits the in-memory copy of the demo, it does NOT modify the
367 This edits the in-memory copy of the demo, it does NOT modify the
368 original source file. If you want to do that, simply open the file in
368 original source file. If you want to do that, simply open the file in
369 an editor and use reload() when you make changes to the file. This
369 an editor and use reload() when you make changes to the file. This
370 method is meant to let you change a block during a demonstration for
370 method is meant to let you change a block during a demonstration for
371 explanatory purposes, without damaging your original script."""
371 explanatory purposes, without damaging your original script."""
372
372
373 index = self._get_index(index)
373 index = self._get_index(index)
374 if index is None:
374 if index is None:
375 return
375 return
376 # decrease the index by one (unless we're at the very beginning), so
376 # decrease the index by one (unless we're at the very beginning), so
377 # that the default demo.edit() call opens up the sblock we've last run
377 # that the default demo.edit() call opens up the sblock we've last run
378 if index>0:
378 if index>0:
379 index -= 1
379 index -= 1
380
380
381 filename = self.shell.mktempfile(self.src_blocks[index])
381 filename = self.shell.mktempfile(self.src_blocks[index])
382 self.shell.hooks.editor(filename,1)
382 self.shell.hooks.editor(filename,1)
383 with open(filename, 'r') as f:
383 with open(filename, 'r') as f:
384 new_block = f.read()
384 new_block = f.read()
385 # update the source and colored block
385 # update the source and colored block
386 self.src_blocks[index] = new_block
386 self.src_blocks[index] = new_block
387 self.src_blocks_colored[index] = self.ip_colorize(new_block)
387 self.src_blocks_colored[index] = self.ip_colorize(new_block)
388 self.block_index = index
388 self.block_index = index
389 # call to run with the newly edited index
389 # call to run with the newly edited index
390 self()
390 self()
391
391
392 def show(self,index=None):
392 def show(self,index=None):
393 """Show a single block on screen"""
393 """Show a single block on screen"""
394
394
395 index = self._get_index(index)
395 index = self._get_index(index)
396 if index is None:
396 if index is None:
397 return
397 return
398
398
399 print(self.marquee('<%s> block # %s (%s remaining)' %
399 print(self.marquee('<%s> block # %s (%s remaining)' %
400 (self.title,index,self.nblocks-index-1)))
400 (self.title,index,self.nblocks-index-1)))
401 print(self.src_blocks_colored[index])
401 print(self.src_blocks_colored[index])
402 sys.stdout.flush()
402 sys.stdout.flush()
403
403
404 def show_all(self):
404 def show_all(self):
405 """Show entire demo on screen, block by block"""
405 """Show entire demo on screen, block by block"""
406
406
407 fname = self.title
407 fname = self.title
408 title = self.title
408 title = self.title
409 nblocks = self.nblocks
409 nblocks = self.nblocks
410 silent = self._silent
410 silent = self._silent
411 marquee = self.marquee
411 marquee = self.marquee
412 for index,block in enumerate(self.src_blocks_colored):
412 for index,block in enumerate(self.src_blocks_colored):
413 if silent[index]:
413 if silent[index]:
414 print(marquee('<%s> SILENT block # %s (%s remaining)' %
414 print(marquee('<%s> SILENT block # %s (%s remaining)' %
415 (title,index,nblocks-index-1)))
415 (title,index,nblocks-index-1)))
416 else:
416 else:
417 print(marquee('<%s> block # %s (%s remaining)' %
417 print(marquee('<%s> block # %s (%s remaining)' %
418 (title,index,nblocks-index-1)))
418 (title,index,nblocks-index-1)))
419 print(block, end=' ')
419 print(block, end=' ')
420 sys.stdout.flush()
420 sys.stdout.flush()
421
421
422 def run_cell(self,source):
422 def run_cell(self,source):
423 """Execute a string with one or more lines of code"""
423 """Execute a string with one or more lines of code"""
424
424
425 exec(source, self.user_ns)
425 exec(source, self.user_ns)
426
426
427 def __call__(self,index=None):
427 def __call__(self,index=None):
428 """run a block of the demo.
428 """run a block of the demo.
429
429
430 If index is given, it should be an integer >=1 and <= nblocks. This
430 If index is given, it should be an integer >=1 and <= nblocks. This
431 means that the calling convention is one off from typical Python
431 means that the calling convention is one off from typical Python
432 lists. The reason for the inconsistency is that the demo always
432 lists. The reason for the inconsistency is that the demo always
433 prints 'Block n/N, and N is the total, so it would be very odd to use
433 prints 'Block n/N, and N is the total, so it would be very odd to use
434 zero-indexing here."""
434 zero-indexing here."""
435
435
436 index = self._get_index(index)
436 index = self._get_index(index)
437 if index is None:
437 if index is None:
438 return
438 return
439 try:
439 try:
440 marquee = self.marquee
440 marquee = self.marquee
441 next_block = self.src_blocks[index]
441 next_block = self.src_blocks[index]
442 self.block_index += 1
442 self.block_index += 1
443 if self._silent[index]:
443 if self._silent[index]:
444 print(marquee('Executing silent block # %s (%s remaining)' %
444 print(marquee('Executing silent block # %s (%s remaining)' %
445 (index,self.nblocks-index-1)))
445 (index,self.nblocks-index-1)))
446 else:
446 else:
447 self.pre_cmd()
447 self.pre_cmd()
448 self.show(index)
448 self.show(index)
449 if self.auto_all or self._auto[index]:
449 if self.auto_all or self._auto[index]:
450 print(marquee('output:'))
450 print(marquee('output:'))
451 else:
451 else:
452 print(marquee('Press <q> to quit, <Enter> to execute...'), end=' ')
452 print(marquee('Press <q> to quit, <Enter> to execute...'), end=' ')
453 ans = py3compat.input().strip()
453 ans = py3compat.input().strip()
454 if ans:
454 if ans:
455 print(marquee('Block NOT executed'))
455 print(marquee('Block NOT executed'))
456 return
456 return
457 try:
457 try:
458 save_argv = sys.argv
458 save_argv = sys.argv
459 sys.argv = self.sys_argv
459 sys.argv = self.sys_argv
460 self.run_cell(next_block)
460 self.run_cell(next_block)
461 self.post_cmd()
461 self.post_cmd()
462 finally:
462 finally:
463 sys.argv = save_argv
463 sys.argv = save_argv
464
464
465 except:
465 except:
466 self.ip_showtb(filename=self.fname)
466 self.ip_showtb(filename=self.fname)
467 else:
467 else:
468 self.ip_ns.update(self.user_ns)
468 self.ip_ns.update(self.user_ns)
469
469
470 if self.block_index == self.nblocks:
470 if self.block_index == self.nblocks:
471 mq1 = self.marquee('END OF DEMO')
471 mq1 = self.marquee('END OF DEMO')
472 if mq1:
472 if mq1:
473 # avoid spurious print >>io.stdout,s if empty marquees are used
473 # avoid spurious print if empty marquees are used
474 print()
474 print()
475 print(mq1)
475 print(mq1)
476 print(self.marquee('Use <demo_name>.reset() if you want to rerun it.'))
476 print(self.marquee('Use <demo_name>.reset() if you want to rerun it.'))
477 self.finished = True
477 self.finished = True
478
478
479 # These methods are meant to be overridden by subclasses who may wish to
479 # These methods are meant to be overridden by subclasses who may wish to
480 # customize the behavior of of their demos.
480 # customize the behavior of of their demos.
481 def marquee(self,txt='',width=78,mark='*'):
481 def marquee(self,txt='',width=78,mark='*'):
482 """Return the input string centered in a 'marquee'."""
482 """Return the input string centered in a 'marquee'."""
483 return marquee(txt,width,mark)
483 return marquee(txt,width,mark)
484
484
485 def pre_cmd(self):
485 def pre_cmd(self):
486 """Method called before executing each block."""
486 """Method called before executing each block."""
487 pass
487 pass
488
488
489 def post_cmd(self):
489 def post_cmd(self):
490 """Method called after executing each block."""
490 """Method called after executing each block."""
491 pass
491 pass
492
492
493
493
494 class IPythonDemo(Demo):
494 class IPythonDemo(Demo):
495 """Class for interactive demos with IPython's input processing applied.
495 """Class for interactive demos with IPython's input processing applied.
496
496
497 This subclasses Demo, but instead of executing each block by the Python
497 This subclasses Demo, but instead of executing each block by the Python
498 interpreter (via exec), it actually calls IPython on it, so that any input
498 interpreter (via exec), it actually calls IPython on it, so that any input
499 filters which may be in place are applied to the input block.
499 filters which may be in place are applied to the input block.
500
500
501 If you have an interactive environment which exposes special input
501 If you have an interactive environment which exposes special input
502 processing, you can use this class instead to write demo scripts which
502 processing, you can use this class instead to write demo scripts which
503 operate exactly as if you had typed them interactively. The default Demo
503 operate exactly as if you had typed them interactively. The default Demo
504 class requires the input to be valid, pure Python code.
504 class requires the input to be valid, pure Python code.
505 """
505 """
506
506
507 def run_cell(self,source):
507 def run_cell(self,source):
508 """Execute a string with one or more lines of code"""
508 """Execute a string with one or more lines of code"""
509
509
510 self.shell.run_cell(source)
510 self.shell.run_cell(source)
511
511
512 class LineDemo(Demo):
512 class LineDemo(Demo):
513 """Demo where each line is executed as a separate block.
513 """Demo where each line is executed as a separate block.
514
514
515 The input script should be valid Python code.
515 The input script should be valid Python code.
516
516
517 This class doesn't require any markup at all, and it's meant for simple
517 This class doesn't require any markup at all, and it's meant for simple
518 scripts (with no nesting or any kind of indentation) which consist of
518 scripts (with no nesting or any kind of indentation) which consist of
519 multiple lines of input to be executed, one at a time, as if they had been
519 multiple lines of input to be executed, one at a time, as if they had been
520 typed in the interactive prompt.
520 typed in the interactive prompt.
521
521
522 Note: the input can not have *any* indentation, which means that only
522 Note: the input can not have *any* indentation, which means that only
523 single-lines of input are accepted, not even function definitions are
523 single-lines of input are accepted, not even function definitions are
524 valid."""
524 valid."""
525
525
526 def reload(self):
526 def reload(self):
527 """Reload source from disk and initialize state."""
527 """Reload source from disk and initialize state."""
528 # read data and parse into blocks
528 # read data and parse into blocks
529 self.fload()
529 self.fload()
530 lines = self.fobj.readlines()
530 lines = self.fobj.readlines()
531 src_b = [l for l in lines if l.strip()]
531 src_b = [l for l in lines if l.strip()]
532 nblocks = len(src_b)
532 nblocks = len(src_b)
533 self.src = ''.join(lines)
533 self.src = ''.join(lines)
534 self._silent = [False]*nblocks
534 self._silent = [False]*nblocks
535 self._auto = [True]*nblocks
535 self._auto = [True]*nblocks
536 self.auto_all = True
536 self.auto_all = True
537 self.nblocks = nblocks
537 self.nblocks = nblocks
538 self.src_blocks = src_b
538 self.src_blocks = src_b
539
539
540 # also build syntax-highlighted source
540 # also build syntax-highlighted source
541 self.src_blocks_colored = map(self.ip_colorize,self.src_blocks)
541 self.src_blocks_colored = map(self.ip_colorize,self.src_blocks)
542
542
543 # ensure clean namespace and seek offset
543 # ensure clean namespace and seek offset
544 self.reset()
544 self.reset()
545
545
546
546
547 class IPythonLineDemo(IPythonDemo,LineDemo):
547 class IPythonLineDemo(IPythonDemo,LineDemo):
548 """Variant of the LineDemo class whose input is processed by IPython."""
548 """Variant of the LineDemo class whose input is processed by IPython."""
549 pass
549 pass
550
550
551
551
552 class ClearMixin(object):
552 class ClearMixin(object):
553 """Use this mixin to make Demo classes with less visual clutter.
553 """Use this mixin to make Demo classes with less visual clutter.
554
554
555 Demos using this mixin will clear the screen before every block and use
555 Demos using this mixin will clear the screen before every block and use
556 blank marquees.
556 blank marquees.
557
557
558 Note that in order for the methods defined here to actually override those
558 Note that in order for the methods defined here to actually override those
559 of the classes it's mixed with, it must go /first/ in the inheritance
559 of the classes it's mixed with, it must go /first/ in the inheritance
560 tree. For example:
560 tree. For example:
561
561
562 class ClearIPDemo(ClearMixin,IPythonDemo): pass
562 class ClearIPDemo(ClearMixin,IPythonDemo): pass
563
563
564 will provide an IPythonDemo class with the mixin's features.
564 will provide an IPythonDemo class with the mixin's features.
565 """
565 """
566
566
567 def marquee(self,txt='',width=78,mark='*'):
567 def marquee(self,txt='',width=78,mark='*'):
568 """Blank marquee that returns '' no matter what the input."""
568 """Blank marquee that returns '' no matter what the input."""
569 return ''
569 return ''
570
570
571 def pre_cmd(self):
571 def pre_cmd(self):
572 """Method called before executing each block.
572 """Method called before executing each block.
573
573
574 This one simply clears the screen."""
574 This one simply clears the screen."""
575 from IPython.utils.terminal import term_clear
575 from IPython.utils.terminal import term_clear
576 term_clear()
576 term_clear()
577
577
578 class ClearDemo(ClearMixin,Demo):
578 class ClearDemo(ClearMixin,Demo):
579 pass
579 pass
580
580
581
581
582 class ClearIPDemo(ClearMixin,IPythonDemo):
582 class ClearIPDemo(ClearMixin,IPythonDemo):
583 pass
583 pass
@@ -1,139 +1,139 b''
1 # encoding: utf-8
1 # encoding: utf-8
2 """Tests for IPython.utils.path.py"""
2 """Tests for IPython.utils.path.py"""
3
3
4 # Copyright (c) IPython Development Team.
4 # Copyright (c) IPython Development Team.
5 # Distributed under the terms of the Modified BSD License.
5 # Distributed under the terms of the Modified BSD License.
6
6
7 try:
7 try:
8 from unittest.mock import patch
8 from unittest.mock import patch
9 except ImportError:
9 except ImportError:
10 from mock import patch
10 from mock import patch
11
11
12 import nose.tools as nt
12 import nose.tools as nt
13
13
14 from IPython.lib import latextools
14 from IPython.lib import latextools
15 from IPython.testing.decorators import onlyif_cmds_exist, skipif_not_matplotlib
15 from IPython.testing.decorators import onlyif_cmds_exist, skipif_not_matplotlib
16 from IPython.utils.process import FindCmdError
16 from IPython.utils.process import FindCmdError
17
17
18
18
19 def test_latex_to_png_dvipng_fails_when_no_cmd():
19 def test_latex_to_png_dvipng_fails_when_no_cmd():
20 """
20 """
21 `latex_to_png_dvipng` should return None when there is no required command
21 `latex_to_png_dvipng` should return None when there is no required command
22 """
22 """
23 for command in ['latex', 'dvipng']:
23 for command in ['latex', 'dvipng']:
24 yield (check_latex_to_png_dvipng_fails_when_no_cmd, command)
24 yield (check_latex_to_png_dvipng_fails_when_no_cmd, command)
25
25
26
26
27 def check_latex_to_png_dvipng_fails_when_no_cmd(command):
27 def check_latex_to_png_dvipng_fails_when_no_cmd(command):
28 def mock_find_cmd(arg):
28 def mock_find_cmd(arg):
29 if arg == command:
29 if arg == command:
30 raise FindCmdError
30 raise FindCmdError
31
31
32 with patch.object(latextools, "find_cmd", mock_find_cmd):
32 with patch.object(latextools, "find_cmd", mock_find_cmd):
33 nt.assert_equals(latextools.latex_to_png_dvipng("whatever", True),
33 nt.assert_equal(latextools.latex_to_png_dvipng("whatever", True),
34 None)
34 None)
35
35
36
36
37 @onlyif_cmds_exist('latex', 'dvipng')
37 @onlyif_cmds_exist('latex', 'dvipng')
38 def test_latex_to_png_dvipng_runs():
38 def test_latex_to_png_dvipng_runs():
39 """
39 """
40 Test that latex_to_png_dvipng just runs without error.
40 Test that latex_to_png_dvipng just runs without error.
41 """
41 """
42 def mock_kpsewhich(filename):
42 def mock_kpsewhich(filename):
43 nt.assert_equals(filename, "breqn.sty")
43 nt.assert_equal(filename, "breqn.sty")
44 return None
44 return None
45
45
46 for (s, wrap) in [(u"$$x^2$$", False), (u"x^2", True)]:
46 for (s, wrap) in [(u"$$x^2$$", False), (u"x^2", True)]:
47 yield (latextools.latex_to_png_dvipng, s, wrap)
47 yield (latextools.latex_to_png_dvipng, s, wrap)
48
48
49 with patch.object(latextools, "kpsewhich", mock_kpsewhich):
49 with patch.object(latextools, "kpsewhich", mock_kpsewhich):
50 yield (latextools.latex_to_png_dvipng, s, wrap)
50 yield (latextools.latex_to_png_dvipng, s, wrap)
51
51
52 @skipif_not_matplotlib
52 @skipif_not_matplotlib
53 def test_latex_to_png_mpl_runs():
53 def test_latex_to_png_mpl_runs():
54 """
54 """
55 Test that latex_to_png_mpl just runs without error.
55 Test that latex_to_png_mpl just runs without error.
56 """
56 """
57 def mock_kpsewhich(filename):
57 def mock_kpsewhich(filename):
58 nt.assert_equals(filename, "breqn.sty")
58 nt.assert_equal(filename, "breqn.sty")
59 return None
59 return None
60
60
61 for (s, wrap) in [("$x^2$", False), ("x^2", True)]:
61 for (s, wrap) in [("$x^2$", False), ("x^2", True)]:
62 yield (latextools.latex_to_png_mpl, s, wrap)
62 yield (latextools.latex_to_png_mpl, s, wrap)
63
63
64 with patch.object(latextools, "kpsewhich", mock_kpsewhich):
64 with patch.object(latextools, "kpsewhich", mock_kpsewhich):
65 yield (latextools.latex_to_png_mpl, s, wrap)
65 yield (latextools.latex_to_png_mpl, s, wrap)
66
66
67 @skipif_not_matplotlib
67 @skipif_not_matplotlib
68 def test_latex_to_html():
68 def test_latex_to_html():
69 img = latextools.latex_to_html("$x^2$")
69 img = latextools.latex_to_html("$x^2$")
70 nt.assert_in("data:image/png;base64,iVBOR", img)
70 nt.assert_in("data:image/png;base64,iVBOR", img)
71
71
72
72
73 def test_genelatex_no_wrap():
73 def test_genelatex_no_wrap():
74 """
74 """
75 Test genelatex with wrap=False.
75 Test genelatex with wrap=False.
76 """
76 """
77 def mock_kpsewhich(filename):
77 def mock_kpsewhich(filename):
78 assert False, ("kpsewhich should not be called "
78 assert False, ("kpsewhich should not be called "
79 "(called with {0})".format(filename))
79 "(called with {0})".format(filename))
80
80
81 with patch.object(latextools, "kpsewhich", mock_kpsewhich):
81 with patch.object(latextools, "kpsewhich", mock_kpsewhich):
82 nt.assert_equals(
82 nt.assert_equal(
83 '\n'.join(latextools.genelatex("body text", False)),
83 '\n'.join(latextools.genelatex("body text", False)),
84 r'''\documentclass{article}
84 r'''\documentclass{article}
85 \usepackage{amsmath}
85 \usepackage{amsmath}
86 \usepackage{amsthm}
86 \usepackage{amsthm}
87 \usepackage{amssymb}
87 \usepackage{amssymb}
88 \usepackage{bm}
88 \usepackage{bm}
89 \pagestyle{empty}
89 \pagestyle{empty}
90 \begin{document}
90 \begin{document}
91 body text
91 body text
92 \end{document}''')
92 \end{document}''')
93
93
94
94
95 def test_genelatex_wrap_with_breqn():
95 def test_genelatex_wrap_with_breqn():
96 """
96 """
97 Test genelatex with wrap=True for the case breqn.sty is installed.
97 Test genelatex with wrap=True for the case breqn.sty is installed.
98 """
98 """
99 def mock_kpsewhich(filename):
99 def mock_kpsewhich(filename):
100 nt.assert_equals(filename, "breqn.sty")
100 nt.assert_equal(filename, "breqn.sty")
101 return "path/to/breqn.sty"
101 return "path/to/breqn.sty"
102
102
103 with patch.object(latextools, "kpsewhich", mock_kpsewhich):
103 with patch.object(latextools, "kpsewhich", mock_kpsewhich):
104 nt.assert_equals(
104 nt.assert_equal(
105 '\n'.join(latextools.genelatex("x^2", True)),
105 '\n'.join(latextools.genelatex("x^2", True)),
106 r'''\documentclass{article}
106 r'''\documentclass{article}
107 \usepackage{amsmath}
107 \usepackage{amsmath}
108 \usepackage{amsthm}
108 \usepackage{amsthm}
109 \usepackage{amssymb}
109 \usepackage{amssymb}
110 \usepackage{bm}
110 \usepackage{bm}
111 \usepackage{breqn}
111 \usepackage{breqn}
112 \pagestyle{empty}
112 \pagestyle{empty}
113 \begin{document}
113 \begin{document}
114 \begin{dmath*}
114 \begin{dmath*}
115 x^2
115 x^2
116 \end{dmath*}
116 \end{dmath*}
117 \end{document}''')
117 \end{document}''')
118
118
119
119
120 def test_genelatex_wrap_without_breqn():
120 def test_genelatex_wrap_without_breqn():
121 """
121 """
122 Test genelatex with wrap=True for the case breqn.sty is not installed.
122 Test genelatex with wrap=True for the case breqn.sty is not installed.
123 """
123 """
124 def mock_kpsewhich(filename):
124 def mock_kpsewhich(filename):
125 nt.assert_equals(filename, "breqn.sty")
125 nt.assert_equal(filename, "breqn.sty")
126 return None
126 return None
127
127
128 with patch.object(latextools, "kpsewhich", mock_kpsewhich):
128 with patch.object(latextools, "kpsewhich", mock_kpsewhich):
129 nt.assert_equals(
129 nt.assert_equal(
130 '\n'.join(latextools.genelatex("x^2", True)),
130 '\n'.join(latextools.genelatex("x^2", True)),
131 r'''\documentclass{article}
131 r'''\documentclass{article}
132 \usepackage{amsmath}
132 \usepackage{amsmath}
133 \usepackage{amsthm}
133 \usepackage{amsthm}
134 \usepackage{amssymb}
134 \usepackage{amssymb}
135 \usepackage{bm}
135 \usepackage{bm}
136 \pagestyle{empty}
136 \pagestyle{empty}
137 \begin{document}
137 \begin{document}
138 $$x^2$$
138 $$x^2$$
139 \end{document}''')
139 \end{document}''')
@@ -1,1185 +1,1184 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """
2 """
3 Sphinx directive to support embedded IPython code.
3 Sphinx directive to support embedded IPython code.
4
4
5 This directive allows pasting of entire interactive IPython sessions, prompts
5 This directive allows pasting of entire interactive IPython sessions, prompts
6 and all, and their code will actually get re-executed at doc build time, with
6 and all, and their code will actually get re-executed at doc build time, with
7 all prompts renumbered sequentially. It also allows you to input code as a pure
7 all prompts renumbered sequentially. It also allows you to input code as a pure
8 python input by giving the argument python to the directive. The output looks
8 python input by giving the argument python to the directive. The output looks
9 like an interactive ipython section.
9 like an interactive ipython section.
10
10
11 To enable this directive, simply list it in your Sphinx ``conf.py`` file
11 To enable this directive, simply list it in your Sphinx ``conf.py`` file
12 (making sure the directory where you placed it is visible to sphinx, as is
12 (making sure the directory where you placed it is visible to sphinx, as is
13 needed for all Sphinx directives). For example, to enable syntax highlighting
13 needed for all Sphinx directives). For example, to enable syntax highlighting
14 and the IPython directive::
14 and the IPython directive::
15
15
16 extensions = ['IPython.sphinxext.ipython_console_highlighting',
16 extensions = ['IPython.sphinxext.ipython_console_highlighting',
17 'IPython.sphinxext.ipython_directive']
17 'IPython.sphinxext.ipython_directive']
18
18
19 The IPython directive outputs code-blocks with the language 'ipython'. So
19 The IPython directive outputs code-blocks with the language 'ipython'. So
20 if you do not have the syntax highlighting extension enabled as well, then
20 if you do not have the syntax highlighting extension enabled as well, then
21 all rendered code-blocks will be uncolored. By default this directive assumes
21 all rendered code-blocks will be uncolored. By default this directive assumes
22 that your prompts are unchanged IPython ones, but this can be customized.
22 that your prompts are unchanged IPython ones, but this can be customized.
23 The configurable options that can be placed in conf.py are:
23 The configurable options that can be placed in conf.py are:
24
24
25 ipython_savefig_dir:
25 ipython_savefig_dir:
26 The directory in which to save the figures. This is relative to the
26 The directory in which to save the figures. This is relative to the
27 Sphinx source directory. The default is `html_static_path`.
27 Sphinx source directory. The default is `html_static_path`.
28 ipython_rgxin:
28 ipython_rgxin:
29 The compiled regular expression to denote the start of IPython input
29 The compiled regular expression to denote the start of IPython input
30 lines. The default is re.compile('In \[(\d+)\]:\s?(.*)\s*'). You
30 lines. The default is re.compile('In \[(\d+)\]:\s?(.*)\s*'). You
31 shouldn't need to change this.
31 shouldn't need to change this.
32 ipython_rgxout:
32 ipython_rgxout:
33 The compiled regular expression to denote the start of IPython output
33 The compiled regular expression to denote the start of IPython output
34 lines. The default is re.compile('Out\[(\d+)\]:\s?(.*)\s*'). You
34 lines. The default is re.compile('Out\[(\d+)\]:\s?(.*)\s*'). You
35 shouldn't need to change this.
35 shouldn't need to change this.
36 ipython_promptin:
36 ipython_promptin:
37 The string to represent the IPython input prompt in the generated ReST.
37 The string to represent the IPython input prompt in the generated ReST.
38 The default is 'In [%d]:'. This expects that the line numbers are used
38 The default is 'In [%d]:'. This expects that the line numbers are used
39 in the prompt.
39 in the prompt.
40 ipython_promptout:
40 ipython_promptout:
41 The string to represent the IPython prompt in the generated ReST. The
41 The string to represent the IPython prompt in the generated ReST. The
42 default is 'Out [%d]:'. This expects that the line numbers are used
42 default is 'Out [%d]:'. This expects that the line numbers are used
43 in the prompt.
43 in the prompt.
44 ipython_mplbackend:
44 ipython_mplbackend:
45 The string which specifies if the embedded Sphinx shell should import
45 The string which specifies if the embedded Sphinx shell should import
46 Matplotlib and set the backend. The value specifies a backend that is
46 Matplotlib and set the backend. The value specifies a backend that is
47 passed to `matplotlib.use()` before any lines in `ipython_execlines` are
47 passed to `matplotlib.use()` before any lines in `ipython_execlines` are
48 executed. If not specified in conf.py, then the default value of 'agg' is
48 executed. If not specified in conf.py, then the default value of 'agg' is
49 used. To use the IPython directive without matplotlib as a dependency, set
49 used. To use the IPython directive without matplotlib as a dependency, set
50 the value to `None`. It may end up that matplotlib is still imported
50 the value to `None`. It may end up that matplotlib is still imported
51 if the user specifies so in `ipython_execlines` or makes use of the
51 if the user specifies so in `ipython_execlines` or makes use of the
52 @savefig pseudo decorator.
52 @savefig pseudo decorator.
53 ipython_execlines:
53 ipython_execlines:
54 A list of strings to be exec'd in the embedded Sphinx shell. Typical
54 A list of strings to be exec'd in the embedded Sphinx shell. Typical
55 usage is to make certain packages always available. Set this to an empty
55 usage is to make certain packages always available. Set this to an empty
56 list if you wish to have no imports always available. If specified in
56 list if you wish to have no imports always available. If specified in
57 conf.py as `None`, then it has the effect of making no imports available.
57 conf.py as `None`, then it has the effect of making no imports available.
58 If omitted from conf.py altogether, then the default value of
58 If omitted from conf.py altogether, then the default value of
59 ['import numpy as np', 'import matplotlib.pyplot as plt'] is used.
59 ['import numpy as np', 'import matplotlib.pyplot as plt'] is used.
60 ipython_holdcount
60 ipython_holdcount
61 When the @suppress pseudo-decorator is used, the execution count can be
61 When the @suppress pseudo-decorator is used, the execution count can be
62 incremented or not. The default behavior is to hold the execution count,
62 incremented or not. The default behavior is to hold the execution count,
63 corresponding to a value of `True`. Set this to `False` to increment
63 corresponding to a value of `True`. Set this to `False` to increment
64 the execution count after each suppressed command.
64 the execution count after each suppressed command.
65
65
66 As an example, to use the IPython directive when `matplotlib` is not available,
66 As an example, to use the IPython directive when `matplotlib` is not available,
67 one sets the backend to `None`::
67 one sets the backend to `None`::
68
68
69 ipython_mplbackend = None
69 ipython_mplbackend = None
70
70
71 An example usage of the directive is:
71 An example usage of the directive is:
72
72
73 .. code-block:: rst
73 .. code-block:: rst
74
74
75 .. ipython::
75 .. ipython::
76
76
77 In [1]: x = 1
77 In [1]: x = 1
78
78
79 In [2]: y = x**2
79 In [2]: y = x**2
80
80
81 In [3]: print(y)
81 In [3]: print(y)
82
82
83 See http://matplotlib.org/sampledoc/ipython_directive.html for additional
83 See http://matplotlib.org/sampledoc/ipython_directive.html for additional
84 documentation.
84 documentation.
85
85
86 Pseudo-Decorators
86 Pseudo-Decorators
87 =================
87 =================
88
88
89 Note: Only one decorator is supported per input. If more than one decorator
89 Note: Only one decorator is supported per input. If more than one decorator
90 is specified, then only the last one is used.
90 is specified, then only the last one is used.
91
91
92 In addition to the Pseudo-Decorators/options described at the above link,
92 In addition to the Pseudo-Decorators/options described at the above link,
93 several enhancements have been made. The directive will emit a message to the
93 several enhancements have been made. The directive will emit a message to the
94 console at build-time if code-execution resulted in an exception or warning.
94 console at build-time if code-execution resulted in an exception or warning.
95 You can suppress these on a per-block basis by specifying the :okexcept:
95 You can suppress these on a per-block basis by specifying the :okexcept:
96 or :okwarning: options:
96 or :okwarning: options:
97
97
98 .. code-block:: rst
98 .. code-block:: rst
99
99
100 .. ipython::
100 .. ipython::
101 :okexcept:
101 :okexcept:
102 :okwarning:
102 :okwarning:
103
103
104 In [1]: 1/0
104 In [1]: 1/0
105 In [2]: # raise warning.
105 In [2]: # raise warning.
106
106
107 ToDo
107 ToDo
108 ----
108 ----
109
109
110 - Turn the ad-hoc test() function into a real test suite.
110 - Turn the ad-hoc test() function into a real test suite.
111 - Break up ipython-specific functionality from matplotlib stuff into better
111 - Break up ipython-specific functionality from matplotlib stuff into better
112 separated code.
112 separated code.
113
113
114 Authors
114 Authors
115 -------
115 -------
116
116
117 - John D Hunter: orignal author.
117 - John D Hunter: orignal author.
118 - Fernando Perez: refactoring, documentation, cleanups, port to 0.11.
118 - Fernando Perez: refactoring, documentation, cleanups, port to 0.11.
119 - VΓ‘clavΕ milauer <eudoxos-AT-arcig.cz>: Prompt generalizations.
119 - VΓ‘clavΕ milauer <eudoxos-AT-arcig.cz>: Prompt generalizations.
120 - Skipper Seabold, refactoring, cleanups, pure python addition
120 - Skipper Seabold, refactoring, cleanups, pure python addition
121 """
121 """
122 from __future__ import print_function
122 from __future__ import print_function
123
123
124 #-----------------------------------------------------------------------------
124 #-----------------------------------------------------------------------------
125 # Imports
125 # Imports
126 #-----------------------------------------------------------------------------
126 #-----------------------------------------------------------------------------
127
127
128 # Stdlib
128 # Stdlib
129 import atexit
129 import atexit
130 import os
130 import os
131 import re
131 import re
132 import sys
132 import sys
133 import tempfile
133 import tempfile
134 import ast
134 import ast
135 import warnings
135 import warnings
136 import shutil
136 import shutil
137
137
138
138
139 # Third-party
139 # Third-party
140 from docutils.parsers.rst import directives
140 from docutils.parsers.rst import directives
141 from sphinx.util.compat import Directive
141 from sphinx.util.compat import Directive
142
142
143 # Our own
143 # Our own
144 from traitlets.config import Config
144 from traitlets.config import Config
145 from IPython import InteractiveShell
145 from IPython import InteractiveShell
146 from IPython.core.profiledir import ProfileDir
146 from IPython.core.profiledir import ProfileDir
147 from IPython.utils import io
147 from IPython.utils import io
148 from IPython.utils.py3compat import PY3
148 from IPython.utils.py3compat import PY3
149
149
150 if PY3:
150 if PY3:
151 from io import StringIO
151 from io import StringIO
152 else:
152 else:
153 from StringIO import StringIO
153 from StringIO import StringIO
154
154
155 #-----------------------------------------------------------------------------
155 #-----------------------------------------------------------------------------
156 # Globals
156 # Globals
157 #-----------------------------------------------------------------------------
157 #-----------------------------------------------------------------------------
158 # for tokenizing blocks
158 # for tokenizing blocks
159 COMMENT, INPUT, OUTPUT = range(3)
159 COMMENT, INPUT, OUTPUT = range(3)
160
160
161 #-----------------------------------------------------------------------------
161 #-----------------------------------------------------------------------------
162 # Functions and class declarations
162 # Functions and class declarations
163 #-----------------------------------------------------------------------------
163 #-----------------------------------------------------------------------------
164
164
165 def block_parser(part, rgxin, rgxout, fmtin, fmtout):
165 def block_parser(part, rgxin, rgxout, fmtin, fmtout):
166 """
166 """
167 part is a string of ipython text, comprised of at most one
167 part is a string of ipython text, comprised of at most one
168 input, one output, comments, and blank lines. The block parser
168 input, one output, comments, and blank lines. The block parser
169 parses the text into a list of::
169 parses the text into a list of::
170
170
171 blocks = [ (TOKEN0, data0), (TOKEN1, data1), ...]
171 blocks = [ (TOKEN0, data0), (TOKEN1, data1), ...]
172
172
173 where TOKEN is one of [COMMENT | INPUT | OUTPUT ] and
173 where TOKEN is one of [COMMENT | INPUT | OUTPUT ] and
174 data is, depending on the type of token::
174 data is, depending on the type of token::
175
175
176 COMMENT : the comment string
176 COMMENT : the comment string
177
177
178 INPUT: the (DECORATOR, INPUT_LINE, REST) where
178 INPUT: the (DECORATOR, INPUT_LINE, REST) where
179 DECORATOR: the input decorator (or None)
179 DECORATOR: the input decorator (or None)
180 INPUT_LINE: the input as string (possibly multi-line)
180 INPUT_LINE: the input as string (possibly multi-line)
181 REST : any stdout generated by the input line (not OUTPUT)
181 REST : any stdout generated by the input line (not OUTPUT)
182
182
183 OUTPUT: the output string, possibly multi-line
183 OUTPUT: the output string, possibly multi-line
184
184
185 """
185 """
186 block = []
186 block = []
187 lines = part.split('\n')
187 lines = part.split('\n')
188 N = len(lines)
188 N = len(lines)
189 i = 0
189 i = 0
190 decorator = None
190 decorator = None
191 while 1:
191 while 1:
192
192
193 if i==N:
193 if i==N:
194 # nothing left to parse -- the last line
194 # nothing left to parse -- the last line
195 break
195 break
196
196
197 line = lines[i]
197 line = lines[i]
198 i += 1
198 i += 1
199 line_stripped = line.strip()
199 line_stripped = line.strip()
200 if line_stripped.startswith('#'):
200 if line_stripped.startswith('#'):
201 block.append((COMMENT, line))
201 block.append((COMMENT, line))
202 continue
202 continue
203
203
204 if line_stripped.startswith('@'):
204 if line_stripped.startswith('@'):
205 # Here is where we assume there is, at most, one decorator.
205 # Here is where we assume there is, at most, one decorator.
206 # Might need to rethink this.
206 # Might need to rethink this.
207 decorator = line_stripped
207 decorator = line_stripped
208 continue
208 continue
209
209
210 # does this look like an input line?
210 # does this look like an input line?
211 matchin = rgxin.match(line)
211 matchin = rgxin.match(line)
212 if matchin:
212 if matchin:
213 lineno, inputline = int(matchin.group(1)), matchin.group(2)
213 lineno, inputline = int(matchin.group(1)), matchin.group(2)
214
214
215 # the ....: continuation string
215 # the ....: continuation string
216 continuation = ' %s:'%''.join(['.']*(len(str(lineno))+2))
216 continuation = ' %s:'%''.join(['.']*(len(str(lineno))+2))
217 Nc = len(continuation)
217 Nc = len(continuation)
218 # input lines can continue on for more than one line, if
218 # input lines can continue on for more than one line, if
219 # we have a '\' line continuation char or a function call
219 # we have a '\' line continuation char or a function call
220 # echo line 'print'. The input line can only be
220 # echo line 'print'. The input line can only be
221 # terminated by the end of the block or an output line, so
221 # terminated by the end of the block or an output line, so
222 # we parse out the rest of the input line if it is
222 # we parse out the rest of the input line if it is
223 # multiline as well as any echo text
223 # multiline as well as any echo text
224
224
225 rest = []
225 rest = []
226 while i<N:
226 while i<N:
227
227
228 # look ahead; if the next line is blank, or a comment, or
228 # look ahead; if the next line is blank, or a comment, or
229 # an output line, we're done
229 # an output line, we're done
230
230
231 nextline = lines[i]
231 nextline = lines[i]
232 matchout = rgxout.match(nextline)
232 matchout = rgxout.match(nextline)
233 #print "nextline=%s, continuation=%s, starts=%s"%(nextline, continuation, nextline.startswith(continuation))
233 #print "nextline=%s, continuation=%s, starts=%s"%(nextline, continuation, nextline.startswith(continuation))
234 if matchout or nextline.startswith('#'):
234 if matchout or nextline.startswith('#'):
235 break
235 break
236 elif nextline.startswith(continuation):
236 elif nextline.startswith(continuation):
237 # The default ipython_rgx* treat the space following the colon as optional.
237 # The default ipython_rgx* treat the space following the colon as optional.
238 # However, If the space is there we must consume it or code
238 # However, If the space is there we must consume it or code
239 # employing the cython_magic extension will fail to execute.
239 # employing the cython_magic extension will fail to execute.
240 #
240 #
241 # This works with the default ipython_rgx* patterns,
241 # This works with the default ipython_rgx* patterns,
242 # If you modify them, YMMV.
242 # If you modify them, YMMV.
243 nextline = nextline[Nc:]
243 nextline = nextline[Nc:]
244 if nextline and nextline[0] == ' ':
244 if nextline and nextline[0] == ' ':
245 nextline = nextline[1:]
245 nextline = nextline[1:]
246
246
247 inputline += '\n' + nextline
247 inputline += '\n' + nextline
248 else:
248 else:
249 rest.append(nextline)
249 rest.append(nextline)
250 i+= 1
250 i+= 1
251
251
252 block.append((INPUT, (decorator, inputline, '\n'.join(rest))))
252 block.append((INPUT, (decorator, inputline, '\n'.join(rest))))
253 continue
253 continue
254
254
255 # if it looks like an output line grab all the text to the end
255 # if it looks like an output line grab all the text to the end
256 # of the block
256 # of the block
257 matchout = rgxout.match(line)
257 matchout = rgxout.match(line)
258 if matchout:
258 if matchout:
259 lineno, output = int(matchout.group(1)), matchout.group(2)
259 lineno, output = int(matchout.group(1)), matchout.group(2)
260 if i<N-1:
260 if i<N-1:
261 output = '\n'.join([output] + lines[i:])
261 output = '\n'.join([output] + lines[i:])
262
262
263 block.append((OUTPUT, output))
263 block.append((OUTPUT, output))
264 break
264 break
265
265
266 return block
266 return block
267
267
268
268
269 class EmbeddedSphinxShell(object):
269 class EmbeddedSphinxShell(object):
270 """An embedded IPython instance to run inside Sphinx"""
270 """An embedded IPython instance to run inside Sphinx"""
271
271
272 def __init__(self, exec_lines=None):
272 def __init__(self, exec_lines=None):
273
273
274 self.cout = StringIO()
274 self.cout = StringIO()
275
275
276 if exec_lines is None:
276 if exec_lines is None:
277 exec_lines = []
277 exec_lines = []
278
278
279 # Create config object for IPython
279 # Create config object for IPython
280 config = Config()
280 config = Config()
281 config.HistoryManager.hist_file = ':memory:'
281 config.HistoryManager.hist_file = ':memory:'
282 config.InteractiveShell.autocall = False
282 config.InteractiveShell.autocall = False
283 config.InteractiveShell.autoindent = False
283 config.InteractiveShell.autoindent = False
284 config.InteractiveShell.colors = 'NoColor'
284 config.InteractiveShell.colors = 'NoColor'
285
285
286 # create a profile so instance history isn't saved
286 # create a profile so instance history isn't saved
287 tmp_profile_dir = tempfile.mkdtemp(prefix='profile_')
287 tmp_profile_dir = tempfile.mkdtemp(prefix='profile_')
288 profname = 'auto_profile_sphinx_build'
288 profname = 'auto_profile_sphinx_build'
289 pdir = os.path.join(tmp_profile_dir,profname)
289 pdir = os.path.join(tmp_profile_dir,profname)
290 profile = ProfileDir.create_profile_dir(pdir)
290 profile = ProfileDir.create_profile_dir(pdir)
291
291
292 # Create and initialize global ipython, but don't start its mainloop.
292 # Create and initialize global ipython, but don't start its mainloop.
293 # This will persist across different EmbededSphinxShell instances.
293 # This will persist across different EmbededSphinxShell instances.
294 IP = InteractiveShell.instance(config=config, profile_dir=profile)
294 IP = InteractiveShell.instance(config=config, profile_dir=profile)
295 atexit.register(self.cleanup)
295 atexit.register(self.cleanup)
296
296
297 # io.stdout redirect must be done after instantiating InteractiveShell
297 sys.stdout = self.cout
298 io.stdout = self.cout
298 sys.stderr = self.cout
299 io.stderr = self.cout
300
299
301 # For debugging, so we can see normal output, use this:
300 # For debugging, so we can see normal output, use this:
302 #from IPython.utils.io import Tee
301 #from IPython.utils.io import Tee
303 #io.stdout = Tee(self.cout, channel='stdout') # dbg
302 #sys.stdout = Tee(self.cout, channel='stdout') # dbg
304 #io.stderr = Tee(self.cout, channel='stderr') # dbg
303 #sys.stderr = Tee(self.cout, channel='stderr') # dbg
305
304
306 # Store a few parts of IPython we'll need.
305 # Store a few parts of IPython we'll need.
307 self.IP = IP
306 self.IP = IP
308 self.user_ns = self.IP.user_ns
307 self.user_ns = self.IP.user_ns
309 self.user_global_ns = self.IP.user_global_ns
308 self.user_global_ns = self.IP.user_global_ns
310
309
311 self.input = ''
310 self.input = ''
312 self.output = ''
311 self.output = ''
313 self.tmp_profile_dir = tmp_profile_dir
312 self.tmp_profile_dir = tmp_profile_dir
314
313
315 self.is_verbatim = False
314 self.is_verbatim = False
316 self.is_doctest = False
315 self.is_doctest = False
317 self.is_suppress = False
316 self.is_suppress = False
318
317
319 # Optionally, provide more detailed information to shell.
318 # Optionally, provide more detailed information to shell.
320 # this is assigned by the SetUp method of IPythonDirective
319 # this is assigned by the SetUp method of IPythonDirective
321 # to point at itself.
320 # to point at itself.
322 #
321 #
323 # So, you can access handy things at self.directive.state
322 # So, you can access handy things at self.directive.state
324 self.directive = None
323 self.directive = None
325
324
326 # on the first call to the savefig decorator, we'll import
325 # on the first call to the savefig decorator, we'll import
327 # pyplot as plt so we can make a call to the plt.gcf().savefig
326 # pyplot as plt so we can make a call to the plt.gcf().savefig
328 self._pyplot_imported = False
327 self._pyplot_imported = False
329
328
330 # Prepopulate the namespace.
329 # Prepopulate the namespace.
331 for line in exec_lines:
330 for line in exec_lines:
332 self.process_input_line(line, store_history=False)
331 self.process_input_line(line, store_history=False)
333
332
334 def cleanup(self):
333 def cleanup(self):
335 shutil.rmtree(self.tmp_profile_dir, ignore_errors=True)
334 shutil.rmtree(self.tmp_profile_dir, ignore_errors=True)
336
335
337 def clear_cout(self):
336 def clear_cout(self):
338 self.cout.seek(0)
337 self.cout.seek(0)
339 self.cout.truncate(0)
338 self.cout.truncate(0)
340
339
341 def process_input_line(self, line, store_history=True):
340 def process_input_line(self, line, store_history=True):
342 """process the input, capturing stdout"""
341 """process the input, capturing stdout"""
343
342
344 stdout = sys.stdout
343 stdout = sys.stdout
345 splitter = self.IP.input_splitter
344 splitter = self.IP.input_splitter
346 try:
345 try:
347 sys.stdout = self.cout
346 sys.stdout = self.cout
348 splitter.push(line)
347 splitter.push(line)
349 more = splitter.push_accepts_more()
348 more = splitter.push_accepts_more()
350 if not more:
349 if not more:
351 source_raw = splitter.raw_reset()
350 source_raw = splitter.raw_reset()
352 self.IP.run_cell(source_raw, store_history=store_history)
351 self.IP.run_cell(source_raw, store_history=store_history)
353 finally:
352 finally:
354 sys.stdout = stdout
353 sys.stdout = stdout
355
354
356 def process_image(self, decorator):
355 def process_image(self, decorator):
357 """
356 """
358 # build out an image directive like
357 # build out an image directive like
359 # .. image:: somefile.png
358 # .. image:: somefile.png
360 # :width 4in
359 # :width 4in
361 #
360 #
362 # from an input like
361 # from an input like
363 # savefig somefile.png width=4in
362 # savefig somefile.png width=4in
364 """
363 """
365 savefig_dir = self.savefig_dir
364 savefig_dir = self.savefig_dir
366 source_dir = self.source_dir
365 source_dir = self.source_dir
367 saveargs = decorator.split(' ')
366 saveargs = decorator.split(' ')
368 filename = saveargs[1]
367 filename = saveargs[1]
369 # insert relative path to image file in source
368 # insert relative path to image file in source
370 outfile = os.path.relpath(os.path.join(savefig_dir,filename),
369 outfile = os.path.relpath(os.path.join(savefig_dir,filename),
371 source_dir)
370 source_dir)
372
371
373 imagerows = ['.. image:: %s'%outfile]
372 imagerows = ['.. image:: %s'%outfile]
374
373
375 for kwarg in saveargs[2:]:
374 for kwarg in saveargs[2:]:
376 arg, val = kwarg.split('=')
375 arg, val = kwarg.split('=')
377 arg = arg.strip()
376 arg = arg.strip()
378 val = val.strip()
377 val = val.strip()
379 imagerows.append(' :%s: %s'%(arg, val))
378 imagerows.append(' :%s: %s'%(arg, val))
380
379
381 image_file = os.path.basename(outfile) # only return file name
380 image_file = os.path.basename(outfile) # only return file name
382 image_directive = '\n'.join(imagerows)
381 image_directive = '\n'.join(imagerows)
383 return image_file, image_directive
382 return image_file, image_directive
384
383
385 # Callbacks for each type of token
384 # Callbacks for each type of token
386 def process_input(self, data, input_prompt, lineno):
385 def process_input(self, data, input_prompt, lineno):
387 """
386 """
388 Process data block for INPUT token.
387 Process data block for INPUT token.
389
388
390 """
389 """
391 decorator, input, rest = data
390 decorator, input, rest = data
392 image_file = None
391 image_file = None
393 image_directive = None
392 image_directive = None
394
393
395 is_verbatim = decorator=='@verbatim' or self.is_verbatim
394 is_verbatim = decorator=='@verbatim' or self.is_verbatim
396 is_doctest = (decorator is not None and \
395 is_doctest = (decorator is not None and \
397 decorator.startswith('@doctest')) or self.is_doctest
396 decorator.startswith('@doctest')) or self.is_doctest
398 is_suppress = decorator=='@suppress' or self.is_suppress
397 is_suppress = decorator=='@suppress' or self.is_suppress
399 is_okexcept = decorator=='@okexcept' or self.is_okexcept
398 is_okexcept = decorator=='@okexcept' or self.is_okexcept
400 is_okwarning = decorator=='@okwarning' or self.is_okwarning
399 is_okwarning = decorator=='@okwarning' or self.is_okwarning
401 is_savefig = decorator is not None and \
400 is_savefig = decorator is not None and \
402 decorator.startswith('@savefig')
401 decorator.startswith('@savefig')
403
402
404 input_lines = input.split('\n')
403 input_lines = input.split('\n')
405 if len(input_lines) > 1:
404 if len(input_lines) > 1:
406 if input_lines[-1] != "":
405 if input_lines[-1] != "":
407 input_lines.append('') # make sure there's a blank line
406 input_lines.append('') # make sure there's a blank line
408 # so splitter buffer gets reset
407 # so splitter buffer gets reset
409
408
410 continuation = ' %s:'%''.join(['.']*(len(str(lineno))+2))
409 continuation = ' %s:'%''.join(['.']*(len(str(lineno))+2))
411
410
412 if is_savefig:
411 if is_savefig:
413 image_file, image_directive = self.process_image(decorator)
412 image_file, image_directive = self.process_image(decorator)
414
413
415 ret = []
414 ret = []
416 is_semicolon = False
415 is_semicolon = False
417
416
418 # Hold the execution count, if requested to do so.
417 # Hold the execution count, if requested to do so.
419 if is_suppress and self.hold_count:
418 if is_suppress and self.hold_count:
420 store_history = False
419 store_history = False
421 else:
420 else:
422 store_history = True
421 store_history = True
423
422
424 # Note: catch_warnings is not thread safe
423 # Note: catch_warnings is not thread safe
425 with warnings.catch_warnings(record=True) as ws:
424 with warnings.catch_warnings(record=True) as ws:
426 for i, line in enumerate(input_lines):
425 for i, line in enumerate(input_lines):
427 if line.endswith(';'):
426 if line.endswith(';'):
428 is_semicolon = True
427 is_semicolon = True
429
428
430 if i == 0:
429 if i == 0:
431 # process the first input line
430 # process the first input line
432 if is_verbatim:
431 if is_verbatim:
433 self.process_input_line('')
432 self.process_input_line('')
434 self.IP.execution_count += 1 # increment it anyway
433 self.IP.execution_count += 1 # increment it anyway
435 else:
434 else:
436 # only submit the line in non-verbatim mode
435 # only submit the line in non-verbatim mode
437 self.process_input_line(line, store_history=store_history)
436 self.process_input_line(line, store_history=store_history)
438 formatted_line = '%s %s'%(input_prompt, line)
437 formatted_line = '%s %s'%(input_prompt, line)
439 else:
438 else:
440 # process a continuation line
439 # process a continuation line
441 if not is_verbatim:
440 if not is_verbatim:
442 self.process_input_line(line, store_history=store_history)
441 self.process_input_line(line, store_history=store_history)
443
442
444 formatted_line = '%s %s'%(continuation, line)
443 formatted_line = '%s %s'%(continuation, line)
445
444
446 if not is_suppress:
445 if not is_suppress:
447 ret.append(formatted_line)
446 ret.append(formatted_line)
448
447
449 if not is_suppress and len(rest.strip()) and is_verbatim:
448 if not is_suppress and len(rest.strip()) and is_verbatim:
450 # The "rest" is the standard output of the input. This needs to be
449 # The "rest" is the standard output of the input. This needs to be
451 # added when in verbatim mode. If there is no "rest", then we don't
450 # added when in verbatim mode. If there is no "rest", then we don't
452 # add it, as the new line will be added by the processed output.
451 # add it, as the new line will be added by the processed output.
453 ret.append(rest)
452 ret.append(rest)
454
453
455 # Fetch the processed output. (This is not the submitted output.)
454 # Fetch the processed output. (This is not the submitted output.)
456 self.cout.seek(0)
455 self.cout.seek(0)
457 processed_output = self.cout.read()
456 processed_output = self.cout.read()
458 if not is_suppress and not is_semicolon:
457 if not is_suppress and not is_semicolon:
459 #
458 #
460 # In IPythonDirective.run, the elements of `ret` are eventually
459 # In IPythonDirective.run, the elements of `ret` are eventually
461 # combined such that '' entries correspond to newlines. So if
460 # combined such that '' entries correspond to newlines. So if
462 # `processed_output` is equal to '', then the adding it to `ret`
461 # `processed_output` is equal to '', then the adding it to `ret`
463 # ensures that there is a blank line between consecutive inputs
462 # ensures that there is a blank line between consecutive inputs
464 # that have no outputs, as in:
463 # that have no outputs, as in:
465 #
464 #
466 # In [1]: x = 4
465 # In [1]: x = 4
467 #
466 #
468 # In [2]: x = 5
467 # In [2]: x = 5
469 #
468 #
470 # When there is processed output, it has a '\n' at the tail end. So
469 # When there is processed output, it has a '\n' at the tail end. So
471 # adding the output to `ret` will provide the necessary spacing
470 # adding the output to `ret` will provide the necessary spacing
472 # between consecutive input/output blocks, as in:
471 # between consecutive input/output blocks, as in:
473 #
472 #
474 # In [1]: x
473 # In [1]: x
475 # Out[1]: 5
474 # Out[1]: 5
476 #
475 #
477 # In [2]: x
476 # In [2]: x
478 # Out[2]: 5
477 # Out[2]: 5
479 #
478 #
480 # When there is stdout from the input, it also has a '\n' at the
479 # When there is stdout from the input, it also has a '\n' at the
481 # tail end, and so this ensures proper spacing as well. E.g.:
480 # tail end, and so this ensures proper spacing as well. E.g.:
482 #
481 #
483 # In [1]: print x
482 # In [1]: print x
484 # 5
483 # 5
485 #
484 #
486 # In [2]: x = 5
485 # In [2]: x = 5
487 #
486 #
488 # When in verbatim mode, `processed_output` is empty (because
487 # When in verbatim mode, `processed_output` is empty (because
489 # nothing was passed to IP. Sometimes the submitted code block has
488 # nothing was passed to IP. Sometimes the submitted code block has
490 # an Out[] portion and sometimes it does not. When it does not, we
489 # an Out[] portion and sometimes it does not. When it does not, we
491 # need to ensure proper spacing, so we have to add '' to `ret`.
490 # need to ensure proper spacing, so we have to add '' to `ret`.
492 # However, if there is an Out[] in the submitted code, then we do
491 # However, if there is an Out[] in the submitted code, then we do
493 # not want to add a newline as `process_output` has stuff to add.
492 # not want to add a newline as `process_output` has stuff to add.
494 # The difficulty is that `process_input` doesn't know if
493 # The difficulty is that `process_input` doesn't know if
495 # `process_output` will be called---so it doesn't know if there is
494 # `process_output` will be called---so it doesn't know if there is
496 # Out[] in the code block. The requires that we include a hack in
495 # Out[] in the code block. The requires that we include a hack in
497 # `process_block`. See the comments there.
496 # `process_block`. See the comments there.
498 #
497 #
499 ret.append(processed_output)
498 ret.append(processed_output)
500 elif is_semicolon:
499 elif is_semicolon:
501 # Make sure there is a newline after the semicolon.
500 # Make sure there is a newline after the semicolon.
502 ret.append('')
501 ret.append('')
503
502
504 # context information
503 # context information
505 filename = "Unknown"
504 filename = "Unknown"
506 lineno = 0
505 lineno = 0
507 if self.directive.state:
506 if self.directive.state:
508 filename = self.directive.state.document.current_source
507 filename = self.directive.state.document.current_source
509 lineno = self.directive.state.document.current_line
508 lineno = self.directive.state.document.current_line
510
509
511 # output any exceptions raised during execution to stdout
510 # output any exceptions raised during execution to stdout
512 # unless :okexcept: has been specified.
511 # unless :okexcept: has been specified.
513 if not is_okexcept and "Traceback" in processed_output:
512 if not is_okexcept and "Traceback" in processed_output:
514 s = "\nException in %s at block ending on line %s\n" % (filename, lineno)
513 s = "\nException in %s at block ending on line %s\n" % (filename, lineno)
515 s += "Specify :okexcept: as an option in the ipython:: block to suppress this message\n"
514 s += "Specify :okexcept: as an option in the ipython:: block to suppress this message\n"
516 sys.stdout.write('\n\n>>>' + ('-' * 73))
515 sys.stdout.write('\n\n>>>' + ('-' * 73))
517 sys.stdout.write(s)
516 sys.stdout.write(s)
518 sys.stdout.write(processed_output)
517 sys.stdout.write(processed_output)
519 sys.stdout.write('<<<' + ('-' * 73) + '\n\n')
518 sys.stdout.write('<<<' + ('-' * 73) + '\n\n')
520
519
521 # output any warning raised during execution to stdout
520 # output any warning raised during execution to stdout
522 # unless :okwarning: has been specified.
521 # unless :okwarning: has been specified.
523 if not is_okwarning:
522 if not is_okwarning:
524 for w in ws:
523 for w in ws:
525 s = "\nWarning in %s at block ending on line %s\n" % (filename, lineno)
524 s = "\nWarning in %s at block ending on line %s\n" % (filename, lineno)
526 s += "Specify :okwarning: as an option in the ipython:: block to suppress this message\n"
525 s += "Specify :okwarning: as an option in the ipython:: block to suppress this message\n"
527 sys.stdout.write('\n\n>>>' + ('-' * 73))
526 sys.stdout.write('\n\n>>>' + ('-' * 73))
528 sys.stdout.write(s)
527 sys.stdout.write(s)
529 sys.stdout.write(('-' * 76) + '\n')
528 sys.stdout.write(('-' * 76) + '\n')
530 s=warnings.formatwarning(w.message, w.category,
529 s=warnings.formatwarning(w.message, w.category,
531 w.filename, w.lineno, w.line)
530 w.filename, w.lineno, w.line)
532 sys.stdout.write(s)
531 sys.stdout.write(s)
533 sys.stdout.write('<<<' + ('-' * 73) + '\n')
532 sys.stdout.write('<<<' + ('-' * 73) + '\n')
534
533
535 self.cout.truncate(0)
534 self.cout.truncate(0)
536
535
537 return (ret, input_lines, processed_output,
536 return (ret, input_lines, processed_output,
538 is_doctest, decorator, image_file, image_directive)
537 is_doctest, decorator, image_file, image_directive)
539
538
540
539
541 def process_output(self, data, output_prompt, input_lines, output,
540 def process_output(self, data, output_prompt, input_lines, output,
542 is_doctest, decorator, image_file):
541 is_doctest, decorator, image_file):
543 """
542 """
544 Process data block for OUTPUT token.
543 Process data block for OUTPUT token.
545
544
546 """
545 """
547 # Recall: `data` is the submitted output, and `output` is the processed
546 # Recall: `data` is the submitted output, and `output` is the processed
548 # output from `input_lines`.
547 # output from `input_lines`.
549
548
550 TAB = ' ' * 4
549 TAB = ' ' * 4
551
550
552 if is_doctest and output is not None:
551 if is_doctest and output is not None:
553
552
554 found = output # This is the processed output
553 found = output # This is the processed output
555 found = found.strip()
554 found = found.strip()
556 submitted = data.strip()
555 submitted = data.strip()
557
556
558 if self.directive is None:
557 if self.directive is None:
559 source = 'Unavailable'
558 source = 'Unavailable'
560 content = 'Unavailable'
559 content = 'Unavailable'
561 else:
560 else:
562 source = self.directive.state.document.current_source
561 source = self.directive.state.document.current_source
563 content = self.directive.content
562 content = self.directive.content
564 # Add tabs and join into a single string.
563 # Add tabs and join into a single string.
565 content = '\n'.join([TAB + line for line in content])
564 content = '\n'.join([TAB + line for line in content])
566
565
567 # Make sure the output contains the output prompt.
566 # Make sure the output contains the output prompt.
568 ind = found.find(output_prompt)
567 ind = found.find(output_prompt)
569 if ind < 0:
568 if ind < 0:
570 e = ('output does not contain output prompt\n\n'
569 e = ('output does not contain output prompt\n\n'
571 'Document source: {0}\n\n'
570 'Document source: {0}\n\n'
572 'Raw content: \n{1}\n\n'
571 'Raw content: \n{1}\n\n'
573 'Input line(s):\n{TAB}{2}\n\n'
572 'Input line(s):\n{TAB}{2}\n\n'
574 'Output line(s):\n{TAB}{3}\n\n')
573 'Output line(s):\n{TAB}{3}\n\n')
575 e = e.format(source, content, '\n'.join(input_lines),
574 e = e.format(source, content, '\n'.join(input_lines),
576 repr(found), TAB=TAB)
575 repr(found), TAB=TAB)
577 raise RuntimeError(e)
576 raise RuntimeError(e)
578 found = found[len(output_prompt):].strip()
577 found = found[len(output_prompt):].strip()
579
578
580 # Handle the actual doctest comparison.
579 # Handle the actual doctest comparison.
581 if decorator.strip() == '@doctest':
580 if decorator.strip() == '@doctest':
582 # Standard doctest
581 # Standard doctest
583 if found != submitted:
582 if found != submitted:
584 e = ('doctest failure\n\n'
583 e = ('doctest failure\n\n'
585 'Document source: {0}\n\n'
584 'Document source: {0}\n\n'
586 'Raw content: \n{1}\n\n'
585 'Raw content: \n{1}\n\n'
587 'On input line(s):\n{TAB}{2}\n\n'
586 'On input line(s):\n{TAB}{2}\n\n'
588 'we found output:\n{TAB}{3}\n\n'
587 'we found output:\n{TAB}{3}\n\n'
589 'instead of the expected:\n{TAB}{4}\n\n')
588 'instead of the expected:\n{TAB}{4}\n\n')
590 e = e.format(source, content, '\n'.join(input_lines),
589 e = e.format(source, content, '\n'.join(input_lines),
591 repr(found), repr(submitted), TAB=TAB)
590 repr(found), repr(submitted), TAB=TAB)
592 raise RuntimeError(e)
591 raise RuntimeError(e)
593 else:
592 else:
594 self.custom_doctest(decorator, input_lines, found, submitted)
593 self.custom_doctest(decorator, input_lines, found, submitted)
595
594
596 # When in verbatim mode, this holds additional submitted output
595 # When in verbatim mode, this holds additional submitted output
597 # to be written in the final Sphinx output.
596 # to be written in the final Sphinx output.
598 # https://github.com/ipython/ipython/issues/5776
597 # https://github.com/ipython/ipython/issues/5776
599 out_data = []
598 out_data = []
600
599
601 is_verbatim = decorator=='@verbatim' or self.is_verbatim
600 is_verbatim = decorator=='@verbatim' or self.is_verbatim
602 if is_verbatim and data.strip():
601 if is_verbatim and data.strip():
603 # Note that `ret` in `process_block` has '' as its last element if
602 # Note that `ret` in `process_block` has '' as its last element if
604 # the code block was in verbatim mode. So if there is no submitted
603 # the code block was in verbatim mode. So if there is no submitted
605 # output, then we will have proper spacing only if we do not add
604 # output, then we will have proper spacing only if we do not add
606 # an additional '' to `out_data`. This is why we condition on
605 # an additional '' to `out_data`. This is why we condition on
607 # `and data.strip()`.
606 # `and data.strip()`.
608
607
609 # The submitted output has no output prompt. If we want the
608 # The submitted output has no output prompt. If we want the
610 # prompt and the code to appear, we need to join them now
609 # prompt and the code to appear, we need to join them now
611 # instead of adding them separately---as this would create an
610 # instead of adding them separately---as this would create an
612 # undesired newline. How we do this ultimately depends on the
611 # undesired newline. How we do this ultimately depends on the
613 # format of the output regex. I'll do what works for the default
612 # format of the output regex. I'll do what works for the default
614 # prompt for now, and we might have to adjust if it doesn't work
613 # prompt for now, and we might have to adjust if it doesn't work
615 # in other cases. Finally, the submitted output does not have
614 # in other cases. Finally, the submitted output does not have
616 # a trailing newline, so we must add it manually.
615 # a trailing newline, so we must add it manually.
617 out_data.append("{0} {1}\n".format(output_prompt, data))
616 out_data.append("{0} {1}\n".format(output_prompt, data))
618
617
619 return out_data
618 return out_data
620
619
621 def process_comment(self, data):
620 def process_comment(self, data):
622 """Process data fPblock for COMMENT token."""
621 """Process data fPblock for COMMENT token."""
623 if not self.is_suppress:
622 if not self.is_suppress:
624 return [data]
623 return [data]
625
624
626 def save_image(self, image_file):
625 def save_image(self, image_file):
627 """
626 """
628 Saves the image file to disk.
627 Saves the image file to disk.
629 """
628 """
630 self.ensure_pyplot()
629 self.ensure_pyplot()
631 command = 'plt.gcf().savefig("%s")'%image_file
630 command = 'plt.gcf().savefig("%s")'%image_file
632 #print 'SAVEFIG', command # dbg
631 #print 'SAVEFIG', command # dbg
633 self.process_input_line('bookmark ipy_thisdir', store_history=False)
632 self.process_input_line('bookmark ipy_thisdir', store_history=False)
634 self.process_input_line('cd -b ipy_savedir', store_history=False)
633 self.process_input_line('cd -b ipy_savedir', store_history=False)
635 self.process_input_line(command, store_history=False)
634 self.process_input_line(command, store_history=False)
636 self.process_input_line('cd -b ipy_thisdir', store_history=False)
635 self.process_input_line('cd -b ipy_thisdir', store_history=False)
637 self.process_input_line('bookmark -d ipy_thisdir', store_history=False)
636 self.process_input_line('bookmark -d ipy_thisdir', store_history=False)
638 self.clear_cout()
637 self.clear_cout()
639
638
640 def process_block(self, block):
639 def process_block(self, block):
641 """
640 """
642 process block from the block_parser and return a list of processed lines
641 process block from the block_parser and return a list of processed lines
643 """
642 """
644 ret = []
643 ret = []
645 output = None
644 output = None
646 input_lines = None
645 input_lines = None
647 lineno = self.IP.execution_count
646 lineno = self.IP.execution_count
648
647
649 input_prompt = self.promptin % lineno
648 input_prompt = self.promptin % lineno
650 output_prompt = self.promptout % lineno
649 output_prompt = self.promptout % lineno
651 image_file = None
650 image_file = None
652 image_directive = None
651 image_directive = None
653
652
654 found_input = False
653 found_input = False
655 for token, data in block:
654 for token, data in block:
656 if token == COMMENT:
655 if token == COMMENT:
657 out_data = self.process_comment(data)
656 out_data = self.process_comment(data)
658 elif token == INPUT:
657 elif token == INPUT:
659 found_input = True
658 found_input = True
660 (out_data, input_lines, output, is_doctest,
659 (out_data, input_lines, output, is_doctest,
661 decorator, image_file, image_directive) = \
660 decorator, image_file, image_directive) = \
662 self.process_input(data, input_prompt, lineno)
661 self.process_input(data, input_prompt, lineno)
663 elif token == OUTPUT:
662 elif token == OUTPUT:
664 if not found_input:
663 if not found_input:
665
664
666 TAB = ' ' * 4
665 TAB = ' ' * 4
667 linenumber = 0
666 linenumber = 0
668 source = 'Unavailable'
667 source = 'Unavailable'
669 content = 'Unavailable'
668 content = 'Unavailable'
670 if self.directive:
669 if self.directive:
671 linenumber = self.directive.state.document.current_line
670 linenumber = self.directive.state.document.current_line
672 source = self.directive.state.document.current_source
671 source = self.directive.state.document.current_source
673 content = self.directive.content
672 content = self.directive.content
674 # Add tabs and join into a single string.
673 # Add tabs and join into a single string.
675 content = '\n'.join([TAB + line for line in content])
674 content = '\n'.join([TAB + line for line in content])
676
675
677 e = ('\n\nInvalid block: Block contains an output prompt '
676 e = ('\n\nInvalid block: Block contains an output prompt '
678 'without an input prompt.\n\n'
677 'without an input prompt.\n\n'
679 'Document source: {0}\n\n'
678 'Document source: {0}\n\n'
680 'Content begins at line {1}: \n\n{2}\n\n'
679 'Content begins at line {1}: \n\n{2}\n\n'
681 'Problematic block within content: \n\n{TAB}{3}\n\n')
680 'Problematic block within content: \n\n{TAB}{3}\n\n')
682 e = e.format(source, linenumber, content, block, TAB=TAB)
681 e = e.format(source, linenumber, content, block, TAB=TAB)
683
682
684 # Write, rather than include in exception, since Sphinx
683 # Write, rather than include in exception, since Sphinx
685 # will truncate tracebacks.
684 # will truncate tracebacks.
686 sys.stdout.write(e)
685 sys.stdout.write(e)
687 raise RuntimeError('An invalid block was detected.')
686 raise RuntimeError('An invalid block was detected.')
688
687
689 out_data = \
688 out_data = \
690 self.process_output(data, output_prompt, input_lines,
689 self.process_output(data, output_prompt, input_lines,
691 output, is_doctest, decorator,
690 output, is_doctest, decorator,
692 image_file)
691 image_file)
693 if out_data:
692 if out_data:
694 # Then there was user submitted output in verbatim mode.
693 # Then there was user submitted output in verbatim mode.
695 # We need to remove the last element of `ret` that was
694 # We need to remove the last element of `ret` that was
696 # added in `process_input`, as it is '' and would introduce
695 # added in `process_input`, as it is '' and would introduce
697 # an undesirable newline.
696 # an undesirable newline.
698 assert(ret[-1] == '')
697 assert(ret[-1] == '')
699 del ret[-1]
698 del ret[-1]
700
699
701 if out_data:
700 if out_data:
702 ret.extend(out_data)
701 ret.extend(out_data)
703
702
704 # save the image files
703 # save the image files
705 if image_file is not None:
704 if image_file is not None:
706 self.save_image(image_file)
705 self.save_image(image_file)
707
706
708 return ret, image_directive
707 return ret, image_directive
709
708
710 def ensure_pyplot(self):
709 def ensure_pyplot(self):
711 """
710 """
712 Ensures that pyplot has been imported into the embedded IPython shell.
711 Ensures that pyplot has been imported into the embedded IPython shell.
713
712
714 Also, makes sure to set the backend appropriately if not set already.
713 Also, makes sure to set the backend appropriately if not set already.
715
714
716 """
715 """
717 # We are here if the @figure pseudo decorator was used. Thus, it's
716 # We are here if the @figure pseudo decorator was used. Thus, it's
718 # possible that we could be here even if python_mplbackend were set to
717 # possible that we could be here even if python_mplbackend were set to
719 # `None`. That's also strange and perhaps worthy of raising an
718 # `None`. That's also strange and perhaps worthy of raising an
720 # exception, but for now, we just set the backend to 'agg'.
719 # exception, but for now, we just set the backend to 'agg'.
721
720
722 if not self._pyplot_imported:
721 if not self._pyplot_imported:
723 if 'matplotlib.backends' not in sys.modules:
722 if 'matplotlib.backends' not in sys.modules:
724 # Then ipython_matplotlib was set to None but there was a
723 # Then ipython_matplotlib was set to None but there was a
725 # call to the @figure decorator (and ipython_execlines did
724 # call to the @figure decorator (and ipython_execlines did
726 # not set a backend).
725 # not set a backend).
727 #raise Exception("No backend was set, but @figure was used!")
726 #raise Exception("No backend was set, but @figure was used!")
728 import matplotlib
727 import matplotlib
729 matplotlib.use('agg')
728 matplotlib.use('agg')
730
729
731 # Always import pyplot into embedded shell.
730 # Always import pyplot into embedded shell.
732 self.process_input_line('import matplotlib.pyplot as plt',
731 self.process_input_line('import matplotlib.pyplot as plt',
733 store_history=False)
732 store_history=False)
734 self._pyplot_imported = True
733 self._pyplot_imported = True
735
734
736 def process_pure_python(self, content):
735 def process_pure_python(self, content):
737 """
736 """
738 content is a list of strings. it is unedited directive content
737 content is a list of strings. it is unedited directive content
739
738
740 This runs it line by line in the InteractiveShell, prepends
739 This runs it line by line in the InteractiveShell, prepends
741 prompts as needed capturing stderr and stdout, then returns
740 prompts as needed capturing stderr and stdout, then returns
742 the content as a list as if it were ipython code
741 the content as a list as if it were ipython code
743 """
742 """
744 output = []
743 output = []
745 savefig = False # keep up with this to clear figure
744 savefig = False # keep up with this to clear figure
746 multiline = False # to handle line continuation
745 multiline = False # to handle line continuation
747 multiline_start = None
746 multiline_start = None
748 fmtin = self.promptin
747 fmtin = self.promptin
749
748
750 ct = 0
749 ct = 0
751
750
752 for lineno, line in enumerate(content):
751 for lineno, line in enumerate(content):
753
752
754 line_stripped = line.strip()
753 line_stripped = line.strip()
755 if not len(line):
754 if not len(line):
756 output.append(line)
755 output.append(line)
757 continue
756 continue
758
757
759 # handle decorators
758 # handle decorators
760 if line_stripped.startswith('@'):
759 if line_stripped.startswith('@'):
761 output.extend([line])
760 output.extend([line])
762 if 'savefig' in line:
761 if 'savefig' in line:
763 savefig = True # and need to clear figure
762 savefig = True # and need to clear figure
764 continue
763 continue
765
764
766 # handle comments
765 # handle comments
767 if line_stripped.startswith('#'):
766 if line_stripped.startswith('#'):
768 output.extend([line])
767 output.extend([line])
769 continue
768 continue
770
769
771 # deal with lines checking for multiline
770 # deal with lines checking for multiline
772 continuation = u' %s:'% ''.join(['.']*(len(str(ct))+2))
771 continuation = u' %s:'% ''.join(['.']*(len(str(ct))+2))
773 if not multiline:
772 if not multiline:
774 modified = u"%s %s" % (fmtin % ct, line_stripped)
773 modified = u"%s %s" % (fmtin % ct, line_stripped)
775 output.append(modified)
774 output.append(modified)
776 ct += 1
775 ct += 1
777 try:
776 try:
778 ast.parse(line_stripped)
777 ast.parse(line_stripped)
779 output.append(u'')
778 output.append(u'')
780 except Exception: # on a multiline
779 except Exception: # on a multiline
781 multiline = True
780 multiline = True
782 multiline_start = lineno
781 multiline_start = lineno
783 else: # still on a multiline
782 else: # still on a multiline
784 modified = u'%s %s' % (continuation, line)
783 modified = u'%s %s' % (continuation, line)
785 output.append(modified)
784 output.append(modified)
786
785
787 # if the next line is indented, it should be part of multiline
786 # if the next line is indented, it should be part of multiline
788 if len(content) > lineno + 1:
787 if len(content) > lineno + 1:
789 nextline = content[lineno + 1]
788 nextline = content[lineno + 1]
790 if len(nextline) - len(nextline.lstrip()) > 3:
789 if len(nextline) - len(nextline.lstrip()) > 3:
791 continue
790 continue
792 try:
791 try:
793 mod = ast.parse(
792 mod = ast.parse(
794 '\n'.join(content[multiline_start:lineno+1]))
793 '\n'.join(content[multiline_start:lineno+1]))
795 if isinstance(mod.body[0], ast.FunctionDef):
794 if isinstance(mod.body[0], ast.FunctionDef):
796 # check to see if we have the whole function
795 # check to see if we have the whole function
797 for element in mod.body[0].body:
796 for element in mod.body[0].body:
798 if isinstance(element, ast.Return):
797 if isinstance(element, ast.Return):
799 multiline = False
798 multiline = False
800 else:
799 else:
801 output.append(u'')
800 output.append(u'')
802 multiline = False
801 multiline = False
803 except Exception:
802 except Exception:
804 pass
803 pass
805
804
806 if savefig: # clear figure if plotted
805 if savefig: # clear figure if plotted
807 self.ensure_pyplot()
806 self.ensure_pyplot()
808 self.process_input_line('plt.clf()', store_history=False)
807 self.process_input_line('plt.clf()', store_history=False)
809 self.clear_cout()
808 self.clear_cout()
810 savefig = False
809 savefig = False
811
810
812 return output
811 return output
813
812
814 def custom_doctest(self, decorator, input_lines, found, submitted):
813 def custom_doctest(self, decorator, input_lines, found, submitted):
815 """
814 """
816 Perform a specialized doctest.
815 Perform a specialized doctest.
817
816
818 """
817 """
819 from .custom_doctests import doctests
818 from .custom_doctests import doctests
820
819
821 args = decorator.split()
820 args = decorator.split()
822 doctest_type = args[1]
821 doctest_type = args[1]
823 if doctest_type in doctests:
822 if doctest_type in doctests:
824 doctests[doctest_type](self, args, input_lines, found, submitted)
823 doctests[doctest_type](self, args, input_lines, found, submitted)
825 else:
824 else:
826 e = "Invalid option to @doctest: {0}".format(doctest_type)
825 e = "Invalid option to @doctest: {0}".format(doctest_type)
827 raise Exception(e)
826 raise Exception(e)
828
827
829
828
830 class IPythonDirective(Directive):
829 class IPythonDirective(Directive):
831
830
832 has_content = True
831 has_content = True
833 required_arguments = 0
832 required_arguments = 0
834 optional_arguments = 4 # python, suppress, verbatim, doctest
833 optional_arguments = 4 # python, suppress, verbatim, doctest
835 final_argumuent_whitespace = True
834 final_argumuent_whitespace = True
836 option_spec = { 'python': directives.unchanged,
835 option_spec = { 'python': directives.unchanged,
837 'suppress' : directives.flag,
836 'suppress' : directives.flag,
838 'verbatim' : directives.flag,
837 'verbatim' : directives.flag,
839 'doctest' : directives.flag,
838 'doctest' : directives.flag,
840 'okexcept': directives.flag,
839 'okexcept': directives.flag,
841 'okwarning': directives.flag
840 'okwarning': directives.flag
842 }
841 }
843
842
844 shell = None
843 shell = None
845
844
846 seen_docs = set()
845 seen_docs = set()
847
846
848 def get_config_options(self):
847 def get_config_options(self):
849 # contains sphinx configuration variables
848 # contains sphinx configuration variables
850 config = self.state.document.settings.env.config
849 config = self.state.document.settings.env.config
851
850
852 # get config variables to set figure output directory
851 # get config variables to set figure output directory
853 outdir = self.state.document.settings.env.app.outdir
852 outdir = self.state.document.settings.env.app.outdir
854 savefig_dir = config.ipython_savefig_dir
853 savefig_dir = config.ipython_savefig_dir
855 source_dir = os.path.dirname(self.state.document.current_source)
854 source_dir = os.path.dirname(self.state.document.current_source)
856 if savefig_dir is None:
855 if savefig_dir is None:
857 savefig_dir = config.html_static_path or '_static'
856 savefig_dir = config.html_static_path or '_static'
858 if isinstance(savefig_dir, list):
857 if isinstance(savefig_dir, list):
859 savefig_dir = os.path.join(*savefig_dir)
858 savefig_dir = os.path.join(*savefig_dir)
860 savefig_dir = os.path.join(outdir, savefig_dir)
859 savefig_dir = os.path.join(outdir, savefig_dir)
861
860
862 # get regex and prompt stuff
861 # get regex and prompt stuff
863 rgxin = config.ipython_rgxin
862 rgxin = config.ipython_rgxin
864 rgxout = config.ipython_rgxout
863 rgxout = config.ipython_rgxout
865 promptin = config.ipython_promptin
864 promptin = config.ipython_promptin
866 promptout = config.ipython_promptout
865 promptout = config.ipython_promptout
867 mplbackend = config.ipython_mplbackend
866 mplbackend = config.ipython_mplbackend
868 exec_lines = config.ipython_execlines
867 exec_lines = config.ipython_execlines
869 hold_count = config.ipython_holdcount
868 hold_count = config.ipython_holdcount
870
869
871 return (savefig_dir, source_dir, rgxin, rgxout,
870 return (savefig_dir, source_dir, rgxin, rgxout,
872 promptin, promptout, mplbackend, exec_lines, hold_count)
871 promptin, promptout, mplbackend, exec_lines, hold_count)
873
872
874 def setup(self):
873 def setup(self):
875 # Get configuration values.
874 # Get configuration values.
876 (savefig_dir, source_dir, rgxin, rgxout, promptin, promptout,
875 (savefig_dir, source_dir, rgxin, rgxout, promptin, promptout,
877 mplbackend, exec_lines, hold_count) = self.get_config_options()
876 mplbackend, exec_lines, hold_count) = self.get_config_options()
878
877
879 if self.shell is None:
878 if self.shell is None:
880 # We will be here many times. However, when the
879 # We will be here many times. However, when the
881 # EmbeddedSphinxShell is created, its interactive shell member
880 # EmbeddedSphinxShell is created, its interactive shell member
882 # is the same for each instance.
881 # is the same for each instance.
883
882
884 if mplbackend and 'matplotlib.backends' not in sys.modules:
883 if mplbackend and 'matplotlib.backends' not in sys.modules:
885 import matplotlib
884 import matplotlib
886 matplotlib.use(mplbackend)
885 matplotlib.use(mplbackend)
887
886
888 # Must be called after (potentially) importing matplotlib and
887 # Must be called after (potentially) importing matplotlib and
889 # setting its backend since exec_lines might import pylab.
888 # setting its backend since exec_lines might import pylab.
890 self.shell = EmbeddedSphinxShell(exec_lines)
889 self.shell = EmbeddedSphinxShell(exec_lines)
891
890
892 # Store IPython directive to enable better error messages
891 # Store IPython directive to enable better error messages
893 self.shell.directive = self
892 self.shell.directive = self
894
893
895 # reset the execution count if we haven't processed this doc
894 # reset the execution count if we haven't processed this doc
896 #NOTE: this may be borked if there are multiple seen_doc tmp files
895 #NOTE: this may be borked if there are multiple seen_doc tmp files
897 #check time stamp?
896 #check time stamp?
898 if not self.state.document.current_source in self.seen_docs:
897 if not self.state.document.current_source in self.seen_docs:
899 self.shell.IP.history_manager.reset()
898 self.shell.IP.history_manager.reset()
900 self.shell.IP.execution_count = 1
899 self.shell.IP.execution_count = 1
901 self.seen_docs.add(self.state.document.current_source)
900 self.seen_docs.add(self.state.document.current_source)
902
901
903 # and attach to shell so we don't have to pass them around
902 # and attach to shell so we don't have to pass them around
904 self.shell.rgxin = rgxin
903 self.shell.rgxin = rgxin
905 self.shell.rgxout = rgxout
904 self.shell.rgxout = rgxout
906 self.shell.promptin = promptin
905 self.shell.promptin = promptin
907 self.shell.promptout = promptout
906 self.shell.promptout = promptout
908 self.shell.savefig_dir = savefig_dir
907 self.shell.savefig_dir = savefig_dir
909 self.shell.source_dir = source_dir
908 self.shell.source_dir = source_dir
910 self.shell.hold_count = hold_count
909 self.shell.hold_count = hold_count
911
910
912 # setup bookmark for saving figures directory
911 # setup bookmark for saving figures directory
913 self.shell.process_input_line('bookmark ipy_savedir %s'%savefig_dir,
912 self.shell.process_input_line('bookmark ipy_savedir %s'%savefig_dir,
914 store_history=False)
913 store_history=False)
915 self.shell.clear_cout()
914 self.shell.clear_cout()
916
915
917 return rgxin, rgxout, promptin, promptout
916 return rgxin, rgxout, promptin, promptout
918
917
919 def teardown(self):
918 def teardown(self):
920 # delete last bookmark
919 # delete last bookmark
921 self.shell.process_input_line('bookmark -d ipy_savedir',
920 self.shell.process_input_line('bookmark -d ipy_savedir',
922 store_history=False)
921 store_history=False)
923 self.shell.clear_cout()
922 self.shell.clear_cout()
924
923
925 def run(self):
924 def run(self):
926 debug = False
925 debug = False
927
926
928 #TODO, any reason block_parser can't be a method of embeddable shell
927 #TODO, any reason block_parser can't be a method of embeddable shell
929 # then we wouldn't have to carry these around
928 # then we wouldn't have to carry these around
930 rgxin, rgxout, promptin, promptout = self.setup()
929 rgxin, rgxout, promptin, promptout = self.setup()
931
930
932 options = self.options
931 options = self.options
933 self.shell.is_suppress = 'suppress' in options
932 self.shell.is_suppress = 'suppress' in options
934 self.shell.is_doctest = 'doctest' in options
933 self.shell.is_doctest = 'doctest' in options
935 self.shell.is_verbatim = 'verbatim' in options
934 self.shell.is_verbatim = 'verbatim' in options
936 self.shell.is_okexcept = 'okexcept' in options
935 self.shell.is_okexcept = 'okexcept' in options
937 self.shell.is_okwarning = 'okwarning' in options
936 self.shell.is_okwarning = 'okwarning' in options
938
937
939 # handle pure python code
938 # handle pure python code
940 if 'python' in self.arguments:
939 if 'python' in self.arguments:
941 content = self.content
940 content = self.content
942 self.content = self.shell.process_pure_python(content)
941 self.content = self.shell.process_pure_python(content)
943
942
944 # parts consists of all text within the ipython-block.
943 # parts consists of all text within the ipython-block.
945 # Each part is an input/output block.
944 # Each part is an input/output block.
946 parts = '\n'.join(self.content).split('\n\n')
945 parts = '\n'.join(self.content).split('\n\n')
947
946
948 lines = ['.. code-block:: ipython', '']
947 lines = ['.. code-block:: ipython', '']
949 figures = []
948 figures = []
950
949
951 for part in parts:
950 for part in parts:
952 block = block_parser(part, rgxin, rgxout, promptin, promptout)
951 block = block_parser(part, rgxin, rgxout, promptin, promptout)
953 if len(block):
952 if len(block):
954 rows, figure = self.shell.process_block(block)
953 rows, figure = self.shell.process_block(block)
955 for row in rows:
954 for row in rows:
956 lines.extend([' {0}'.format(line)
955 lines.extend([' {0}'.format(line)
957 for line in row.split('\n')])
956 for line in row.split('\n')])
958
957
959 if figure is not None:
958 if figure is not None:
960 figures.append(figure)
959 figures.append(figure)
961
960
962 for figure in figures:
961 for figure in figures:
963 lines.append('')
962 lines.append('')
964 lines.extend(figure.split('\n'))
963 lines.extend(figure.split('\n'))
965 lines.append('')
964 lines.append('')
966
965
967 if len(lines) > 2:
966 if len(lines) > 2:
968 if debug:
967 if debug:
969 print('\n'.join(lines))
968 print('\n'.join(lines))
970 else:
969 else:
971 # This has to do with input, not output. But if we comment
970 # This has to do with input, not output. But if we comment
972 # these lines out, then no IPython code will appear in the
971 # these lines out, then no IPython code will appear in the
973 # final output.
972 # final output.
974 self.state_machine.insert_input(
973 self.state_machine.insert_input(
975 lines, self.state_machine.input_lines.source(0))
974 lines, self.state_machine.input_lines.source(0))
976
975
977 # cleanup
976 # cleanup
978 self.teardown()
977 self.teardown()
979
978
980 return []
979 return []
981
980
982 # Enable as a proper Sphinx directive
981 # Enable as a proper Sphinx directive
983 def setup(app):
982 def setup(app):
984 setup.app = app
983 setup.app = app
985
984
986 app.add_directive('ipython', IPythonDirective)
985 app.add_directive('ipython', IPythonDirective)
987 app.add_config_value('ipython_savefig_dir', None, 'env')
986 app.add_config_value('ipython_savefig_dir', None, 'env')
988 app.add_config_value('ipython_rgxin',
987 app.add_config_value('ipython_rgxin',
989 re.compile('In \[(\d+)\]:\s?(.*)\s*'), 'env')
988 re.compile('In \[(\d+)\]:\s?(.*)\s*'), 'env')
990 app.add_config_value('ipython_rgxout',
989 app.add_config_value('ipython_rgxout',
991 re.compile('Out\[(\d+)\]:\s?(.*)\s*'), 'env')
990 re.compile('Out\[(\d+)\]:\s?(.*)\s*'), 'env')
992 app.add_config_value('ipython_promptin', 'In [%d]:', 'env')
991 app.add_config_value('ipython_promptin', 'In [%d]:', 'env')
993 app.add_config_value('ipython_promptout', 'Out[%d]:', 'env')
992 app.add_config_value('ipython_promptout', 'Out[%d]:', 'env')
994
993
995 # We could just let matplotlib pick whatever is specified as the default
994 # We could just let matplotlib pick whatever is specified as the default
996 # backend in the matplotlibrc file, but this would cause issues if the
995 # backend in the matplotlibrc file, but this would cause issues if the
997 # backend didn't work in headless environments. For this reason, 'agg'
996 # backend didn't work in headless environments. For this reason, 'agg'
998 # is a good default backend choice.
997 # is a good default backend choice.
999 app.add_config_value('ipython_mplbackend', 'agg', 'env')
998 app.add_config_value('ipython_mplbackend', 'agg', 'env')
1000
999
1001 # If the user sets this config value to `None`, then EmbeddedSphinxShell's
1000 # If the user sets this config value to `None`, then EmbeddedSphinxShell's
1002 # __init__ method will treat it as [].
1001 # __init__ method will treat it as [].
1003 execlines = ['import numpy as np', 'import matplotlib.pyplot as plt']
1002 execlines = ['import numpy as np', 'import matplotlib.pyplot as plt']
1004 app.add_config_value('ipython_execlines', execlines, 'env')
1003 app.add_config_value('ipython_execlines', execlines, 'env')
1005
1004
1006 app.add_config_value('ipython_holdcount', True, 'env')
1005 app.add_config_value('ipython_holdcount', True, 'env')
1007
1006
1008 metadata = {'parallel_read_safe': True, 'parallel_write_safe': True}
1007 metadata = {'parallel_read_safe': True, 'parallel_write_safe': True}
1009 return metadata
1008 return metadata
1010
1009
1011 # Simple smoke test, needs to be converted to a proper automatic test.
1010 # Simple smoke test, needs to be converted to a proper automatic test.
1012 def test():
1011 def test():
1013
1012
1014 examples = [
1013 examples = [
1015 r"""
1014 r"""
1016 In [9]: pwd
1015 In [9]: pwd
1017 Out[9]: '/home/jdhunter/py4science/book'
1016 Out[9]: '/home/jdhunter/py4science/book'
1018
1017
1019 In [10]: cd bookdata/
1018 In [10]: cd bookdata/
1020 /home/jdhunter/py4science/book/bookdata
1019 /home/jdhunter/py4science/book/bookdata
1021
1020
1022 In [2]: from pylab import *
1021 In [2]: from pylab import *
1023
1022
1024 In [2]: ion()
1023 In [2]: ion()
1025
1024
1026 In [3]: im = imread('stinkbug.png')
1025 In [3]: im = imread('stinkbug.png')
1027
1026
1028 @savefig mystinkbug.png width=4in
1027 @savefig mystinkbug.png width=4in
1029 In [4]: imshow(im)
1028 In [4]: imshow(im)
1030 Out[4]: <matplotlib.image.AxesImage object at 0x39ea850>
1029 Out[4]: <matplotlib.image.AxesImage object at 0x39ea850>
1031
1030
1032 """,
1031 """,
1033 r"""
1032 r"""
1034
1033
1035 In [1]: x = 'hello world'
1034 In [1]: x = 'hello world'
1036
1035
1037 # string methods can be
1036 # string methods can be
1038 # used to alter the string
1037 # used to alter the string
1039 @doctest
1038 @doctest
1040 In [2]: x.upper()
1039 In [2]: x.upper()
1041 Out[2]: 'HELLO WORLD'
1040 Out[2]: 'HELLO WORLD'
1042
1041
1043 @verbatim
1042 @verbatim
1044 In [3]: x.st<TAB>
1043 In [3]: x.st<TAB>
1045 x.startswith x.strip
1044 x.startswith x.strip
1046 """,
1045 """,
1047 r"""
1046 r"""
1048
1047
1049 In [130]: url = 'http://ichart.finance.yahoo.com/table.csv?s=CROX\
1048 In [130]: url = 'http://ichart.finance.yahoo.com/table.csv?s=CROX\
1050 .....: &d=9&e=22&f=2009&g=d&a=1&br=8&c=2006&ignore=.csv'
1049 .....: &d=9&e=22&f=2009&g=d&a=1&br=8&c=2006&ignore=.csv'
1051
1050
1052 In [131]: print url.split('&')
1051 In [131]: print url.split('&')
1053 ['http://ichart.finance.yahoo.com/table.csv?s=CROX', 'd=9', 'e=22', 'f=2009', 'g=d', 'a=1', 'b=8', 'c=2006', 'ignore=.csv']
1052 ['http://ichart.finance.yahoo.com/table.csv?s=CROX', 'd=9', 'e=22', 'f=2009', 'g=d', 'a=1', 'b=8', 'c=2006', 'ignore=.csv']
1054
1053
1055 In [60]: import urllib
1054 In [60]: import urllib
1056
1055
1057 """,
1056 """,
1058 r"""\
1057 r"""\
1059
1058
1060 In [133]: import numpy.random
1059 In [133]: import numpy.random
1061
1060
1062 @suppress
1061 @suppress
1063 In [134]: numpy.random.seed(2358)
1062 In [134]: numpy.random.seed(2358)
1064
1063
1065 @doctest
1064 @doctest
1066 In [135]: numpy.random.rand(10,2)
1065 In [135]: numpy.random.rand(10,2)
1067 Out[135]:
1066 Out[135]:
1068 array([[ 0.64524308, 0.59943846],
1067 array([[ 0.64524308, 0.59943846],
1069 [ 0.47102322, 0.8715456 ],
1068 [ 0.47102322, 0.8715456 ],
1070 [ 0.29370834, 0.74776844],
1069 [ 0.29370834, 0.74776844],
1071 [ 0.99539577, 0.1313423 ],
1070 [ 0.99539577, 0.1313423 ],
1072 [ 0.16250302, 0.21103583],
1071 [ 0.16250302, 0.21103583],
1073 [ 0.81626524, 0.1312433 ],
1072 [ 0.81626524, 0.1312433 ],
1074 [ 0.67338089, 0.72302393],
1073 [ 0.67338089, 0.72302393],
1075 [ 0.7566368 , 0.07033696],
1074 [ 0.7566368 , 0.07033696],
1076 [ 0.22591016, 0.77731835],
1075 [ 0.22591016, 0.77731835],
1077 [ 0.0072729 , 0.34273127]])
1076 [ 0.0072729 , 0.34273127]])
1078
1077
1079 """,
1078 """,
1080
1079
1081 r"""
1080 r"""
1082 In [106]: print x
1081 In [106]: print x
1083 jdh
1082 jdh
1084
1083
1085 In [109]: for i in range(10):
1084 In [109]: for i in range(10):
1086 .....: print i
1085 .....: print i
1087 .....:
1086 .....:
1088 .....:
1087 .....:
1089 0
1088 0
1090 1
1089 1
1091 2
1090 2
1092 3
1091 3
1093 4
1092 4
1094 5
1093 5
1095 6
1094 6
1096 7
1095 7
1097 8
1096 8
1098 9
1097 9
1099 """,
1098 """,
1100
1099
1101 r"""
1100 r"""
1102
1101
1103 In [144]: from pylab import *
1102 In [144]: from pylab import *
1104
1103
1105 In [145]: ion()
1104 In [145]: ion()
1106
1105
1107 # use a semicolon to suppress the output
1106 # use a semicolon to suppress the output
1108 @savefig test_hist.png width=4in
1107 @savefig test_hist.png width=4in
1109 In [151]: hist(np.random.randn(10000), 100);
1108 In [151]: hist(np.random.randn(10000), 100);
1110
1109
1111
1110
1112 @savefig test_plot.png width=4in
1111 @savefig test_plot.png width=4in
1113 In [151]: plot(np.random.randn(10000), 'o');
1112 In [151]: plot(np.random.randn(10000), 'o');
1114 """,
1113 """,
1115
1114
1116 r"""
1115 r"""
1117 # use a semicolon to suppress the output
1116 # use a semicolon to suppress the output
1118 In [151]: plt.clf()
1117 In [151]: plt.clf()
1119
1118
1120 @savefig plot_simple.png width=4in
1119 @savefig plot_simple.png width=4in
1121 In [151]: plot([1,2,3])
1120 In [151]: plot([1,2,3])
1122
1121
1123 @savefig hist_simple.png width=4in
1122 @savefig hist_simple.png width=4in
1124 In [151]: hist(np.random.randn(10000), 100);
1123 In [151]: hist(np.random.randn(10000), 100);
1125
1124
1126 """,
1125 """,
1127 r"""
1126 r"""
1128 # update the current fig
1127 # update the current fig
1129 In [151]: ylabel('number')
1128 In [151]: ylabel('number')
1130
1129
1131 In [152]: title('normal distribution')
1130 In [152]: title('normal distribution')
1132
1131
1133
1132
1134 @savefig hist_with_text.png
1133 @savefig hist_with_text.png
1135 In [153]: grid(True)
1134 In [153]: grid(True)
1136
1135
1137 @doctest float
1136 @doctest float
1138 In [154]: 0.1 + 0.2
1137 In [154]: 0.1 + 0.2
1139 Out[154]: 0.3
1138 Out[154]: 0.3
1140
1139
1141 @doctest float
1140 @doctest float
1142 In [155]: np.arange(16).reshape(4,4)
1141 In [155]: np.arange(16).reshape(4,4)
1143 Out[155]:
1142 Out[155]:
1144 array([[ 0, 1, 2, 3],
1143 array([[ 0, 1, 2, 3],
1145 [ 4, 5, 6, 7],
1144 [ 4, 5, 6, 7],
1146 [ 8, 9, 10, 11],
1145 [ 8, 9, 10, 11],
1147 [12, 13, 14, 15]])
1146 [12, 13, 14, 15]])
1148
1147
1149 In [1]: x = np.arange(16, dtype=float).reshape(4,4)
1148 In [1]: x = np.arange(16, dtype=float).reshape(4,4)
1150
1149
1151 In [2]: x[0,0] = np.inf
1150 In [2]: x[0,0] = np.inf
1152
1151
1153 In [3]: x[0,1] = np.nan
1152 In [3]: x[0,1] = np.nan
1154
1153
1155 @doctest float
1154 @doctest float
1156 In [4]: x
1155 In [4]: x
1157 Out[4]:
1156 Out[4]:
1158 array([[ inf, nan, 2., 3.],
1157 array([[ inf, nan, 2., 3.],
1159 [ 4., 5., 6., 7.],
1158 [ 4., 5., 6., 7.],
1160 [ 8., 9., 10., 11.],
1159 [ 8., 9., 10., 11.],
1161 [ 12., 13., 14., 15.]])
1160 [ 12., 13., 14., 15.]])
1162
1161
1163
1162
1164 """,
1163 """,
1165 ]
1164 ]
1166 # skip local-file depending first example:
1165 # skip local-file depending first example:
1167 examples = examples[1:]
1166 examples = examples[1:]
1168
1167
1169 #ipython_directive.DEBUG = True # dbg
1168 #ipython_directive.DEBUG = True # dbg
1170 #options = dict(suppress=True) # dbg
1169 #options = dict(suppress=True) # dbg
1171 options = dict()
1170 options = dict()
1172 for example in examples:
1171 for example in examples:
1173 content = example.split('\n')
1172 content = example.split('\n')
1174 IPythonDirective('debug', arguments=None, options=options,
1173 IPythonDirective('debug', arguments=None, options=options,
1175 content=content, lineno=0,
1174 content=content, lineno=0,
1176 content_offset=None, block_text=None,
1175 content_offset=None, block_text=None,
1177 state=None, state_machine=None,
1176 state=None, state_machine=None,
1178 )
1177 )
1179
1178
1180 # Run test suite as a script
1179 # Run test suite as a script
1181 if __name__=='__main__':
1180 if __name__=='__main__':
1182 if not os.path.isdir('_static'):
1181 if not os.path.isdir('_static'):
1183 os.mkdir('_static')
1182 os.mkdir('_static')
1184 test()
1183 test()
1185 print('All OK? Check figures in _static/')
1184 print('All OK? Check figures in _static/')
@@ -1,479 +1,484 b''
1 """IPython terminal interface using prompt_toolkit"""
1 """IPython terminal interface using prompt_toolkit"""
2 from __future__ import print_function
2 from __future__ import print_function
3
3
4 import os
4 import os
5 import sys
5 import sys
6 import warnings
6 from warnings import warn
7 from warnings import warn
7
8
8 from IPython.core.interactiveshell import InteractiveShell, InteractiveShellABC
9 from IPython.core.interactiveshell import InteractiveShell, InteractiveShellABC
10 from IPython.utils import io
9 from IPython.utils.py3compat import PY3, cast_unicode_py2, input
11 from IPython.utils.py3compat import PY3, cast_unicode_py2, input
10 from IPython.utils.terminal import toggle_set_term_title, set_term_title
12 from IPython.utils.terminal import toggle_set_term_title, set_term_title
11 from IPython.utils.process import abbrev_cwd
13 from IPython.utils.process import abbrev_cwd
12 from traitlets import Bool, Unicode, Dict, Integer, observe, Instance, Type, default, Enum
14 from traitlets import Bool, Unicode, Dict, Integer, observe, Instance, Type, default, Enum
13
15
14 from prompt_toolkit.enums import DEFAULT_BUFFER, EditingMode
16 from prompt_toolkit.enums import DEFAULT_BUFFER, EditingMode
15 from prompt_toolkit.filters import (HasFocus, Condition, IsDone)
17 from prompt_toolkit.filters import (HasFocus, Condition, IsDone)
16 from prompt_toolkit.history import InMemoryHistory
18 from prompt_toolkit.history import InMemoryHistory
17 from prompt_toolkit.shortcuts import create_prompt_application, create_eventloop, create_prompt_layout, create_output
19 from prompt_toolkit.shortcuts import create_prompt_application, create_eventloop, create_prompt_layout, create_output
18 from prompt_toolkit.interface import CommandLineInterface
20 from prompt_toolkit.interface import CommandLineInterface
19 from prompt_toolkit.key_binding.manager import KeyBindingManager
21 from prompt_toolkit.key_binding.manager import KeyBindingManager
20 from prompt_toolkit.layout.processors import ConditionalProcessor, HighlightMatchingBracketProcessor
22 from prompt_toolkit.layout.processors import ConditionalProcessor, HighlightMatchingBracketProcessor
21 from prompt_toolkit.styles import PygmentsStyle, DynamicStyle
23 from prompt_toolkit.styles import PygmentsStyle, DynamicStyle
22
24
23 from pygments.styles import get_style_by_name, get_all_styles
25 from pygments.styles import get_style_by_name, get_all_styles
24 from pygments.token import Token
26 from pygments.token import Token
25
27
26 from .debugger import TerminalPdb, Pdb
28 from .debugger import TerminalPdb, Pdb
27 from .magics import TerminalMagics
29 from .magics import TerminalMagics
28 from .pt_inputhooks import get_inputhook_func
30 from .pt_inputhooks import get_inputhook_func
29 from .prompts import Prompts, ClassicPrompts, RichPromptDisplayHook
31 from .prompts import Prompts, ClassicPrompts, RichPromptDisplayHook
30 from .ptutils import IPythonPTCompleter, IPythonPTLexer
32 from .ptutils import IPythonPTCompleter, IPythonPTLexer
31 from .shortcuts import register_ipython_shortcuts
33 from .shortcuts import register_ipython_shortcuts
32
34
33 DISPLAY_BANNER_DEPRECATED = object()
35 DISPLAY_BANNER_DEPRECATED = object()
34
36
35
37
36 from pygments.style import Style
38 from pygments.style import Style
37
39
38 class _NoStyle(Style): pass
40 class _NoStyle(Style): pass
39
41
40
42
41
43
42 _style_overrides_light_bg = {
44 _style_overrides_light_bg = {
43 Token.Prompt: '#0000ff',
45 Token.Prompt: '#0000ff',
44 Token.PromptNum: '#0000ee bold',
46 Token.PromptNum: '#0000ee bold',
45 Token.OutPrompt: '#cc0000',
47 Token.OutPrompt: '#cc0000',
46 Token.OutPromptNum: '#bb0000 bold',
48 Token.OutPromptNum: '#bb0000 bold',
47 }
49 }
48
50
49 _style_overrides_linux = {
51 _style_overrides_linux = {
50 Token.Prompt: '#00cc00',
52 Token.Prompt: '#00cc00',
51 Token.PromptNum: '#00bb00 bold',
53 Token.PromptNum: '#00bb00 bold',
52 Token.OutPrompt: '#cc0000',
54 Token.OutPrompt: '#cc0000',
53 Token.OutPromptNum: '#bb0000 bold',
55 Token.OutPromptNum: '#bb0000 bold',
54 }
56 }
55
57
56
58
57
59
58 def get_default_editor():
60 def get_default_editor():
59 try:
61 try:
60 ed = os.environ['EDITOR']
62 ed = os.environ['EDITOR']
61 if not PY3:
63 if not PY3:
62 ed = ed.decode()
64 ed = ed.decode()
63 return ed
65 return ed
64 except KeyError:
66 except KeyError:
65 pass
67 pass
66 except UnicodeError:
68 except UnicodeError:
67 warn("$EDITOR environment variable is not pure ASCII. Using platform "
69 warn("$EDITOR environment variable is not pure ASCII. Using platform "
68 "default editor.")
70 "default editor.")
69
71
70 if os.name == 'posix':
72 if os.name == 'posix':
71 return 'vi' # the only one guaranteed to be there!
73 return 'vi' # the only one guaranteed to be there!
72 else:
74 else:
73 return 'notepad' # same in Windows!
75 return 'notepad' # same in Windows!
74
76
75
77
76 if sys.stdin and sys.stdout and sys.stderr:
78 if sys.stdin and sys.stdout and sys.stderr:
77 _is_tty = (sys.stdin.isatty()) and (sys.stdout.isatty()) and (sys.stderr.isatty())
79 _is_tty = (sys.stdin.isatty()) and (sys.stdout.isatty()) and (sys.stderr.isatty())
78 else:
80 else:
79 _is_tty = False
81 _is_tty = False
80
82
81
83
82 _use_simple_prompt = ('IPY_TEST_SIMPLE_PROMPT' in os.environ) or (not _is_tty)
84 _use_simple_prompt = ('IPY_TEST_SIMPLE_PROMPT' in os.environ) or (not _is_tty)
83
85
84 class TerminalInteractiveShell(InteractiveShell):
86 class TerminalInteractiveShell(InteractiveShell):
85 space_for_menu = Integer(6, help='Number of line at the bottom of the screen '
87 space_for_menu = Integer(6, help='Number of line at the bottom of the screen '
86 'to reserve for the completion menu'
88 'to reserve for the completion menu'
87 ).tag(config=True)
89 ).tag(config=True)
88
90
89 def _space_for_menu_changed(self, old, new):
91 def _space_for_menu_changed(self, old, new):
90 self._update_layout()
92 self._update_layout()
91
93
92 pt_cli = None
94 pt_cli = None
93 debugger_history = None
95 debugger_history = None
94 _pt_app = None
96 _pt_app = None
95
97
96 simple_prompt = Bool(_use_simple_prompt,
98 simple_prompt = Bool(_use_simple_prompt,
97 help="""Use `raw_input` for the REPL, without completion, multiline input, and prompt colors.
99 help="""Use `raw_input` for the REPL, without completion, multiline input, and prompt colors.
98
100
99 Useful when controlling IPython as a subprocess, and piping STDIN/OUT/ERR. Known usage are:
101 Useful when controlling IPython as a subprocess, and piping STDIN/OUT/ERR. Known usage are:
100 IPython own testing machinery, and emacs inferior-shell integration through elpy.
102 IPython own testing machinery, and emacs inferior-shell integration through elpy.
101
103
102 This mode default to `True` if the `IPY_TEST_SIMPLE_PROMPT`
104 This mode default to `True` if the `IPY_TEST_SIMPLE_PROMPT`
103 environment variable is set, or the current terminal is not a tty.
105 environment variable is set, or the current terminal is not a tty.
104
106
105 """
107 """
106 ).tag(config=True)
108 ).tag(config=True)
107
109
108 @property
110 @property
109 def debugger_cls(self):
111 def debugger_cls(self):
110 return Pdb if self.simple_prompt else TerminalPdb
112 return Pdb if self.simple_prompt else TerminalPdb
111
113
112 confirm_exit = Bool(True,
114 confirm_exit = Bool(True,
113 help="""
115 help="""
114 Set to confirm when you try to exit IPython with an EOF (Control-D
116 Set to confirm when you try to exit IPython with an EOF (Control-D
115 in Unix, Control-Z/Enter in Windows). By typing 'exit' or 'quit',
117 in Unix, Control-Z/Enter in Windows). By typing 'exit' or 'quit',
116 you can force a direct exit without any confirmation.""",
118 you can force a direct exit without any confirmation.""",
117 ).tag(config=True)
119 ).tag(config=True)
118
120
119 editing_mode = Unicode('emacs',
121 editing_mode = Unicode('emacs',
120 help="Shortcut style to use at the prompt. 'vi' or 'emacs'.",
122 help="Shortcut style to use at the prompt. 'vi' or 'emacs'.",
121 ).tag(config=True)
123 ).tag(config=True)
122
124
123 mouse_support = Bool(False,
125 mouse_support = Bool(False,
124 help="Enable mouse support in the prompt"
126 help="Enable mouse support in the prompt"
125 ).tag(config=True)
127 ).tag(config=True)
126
128
127 highlighting_style = Unicode('legacy',
129 highlighting_style = Unicode('legacy',
128 help="The name of a Pygments style to use for syntax highlighting: \n %s" % ', '.join(get_all_styles())
130 help="The name of a Pygments style to use for syntax highlighting: \n %s" % ', '.join(get_all_styles())
129 ).tag(config=True)
131 ).tag(config=True)
130
132
131
133
132 @observe('highlighting_style')
134 @observe('highlighting_style')
133 @observe('colors')
135 @observe('colors')
134 def _highlighting_style_changed(self, change):
136 def _highlighting_style_changed(self, change):
135 self.refresh_style()
137 self.refresh_style()
136
138
137 def refresh_style(self):
139 def refresh_style(self):
138 self._style = self._make_style_from_name(self.highlighting_style)
140 self._style = self._make_style_from_name(self.highlighting_style)
139
141
140
142
141 highlighting_style_overrides = Dict(
143 highlighting_style_overrides = Dict(
142 help="Override highlighting format for specific tokens"
144 help="Override highlighting format for specific tokens"
143 ).tag(config=True)
145 ).tag(config=True)
144
146
145 true_color = Bool(False,
147 true_color = Bool(False,
146 help=("Use 24bit colors instead of 256 colors in prompt highlighting. "
148 help=("Use 24bit colors instead of 256 colors in prompt highlighting. "
147 "If your terminal supports true color, the following command "
149 "If your terminal supports true color, the following command "
148 "should print 'TRUECOLOR' in orange: "
150 "should print 'TRUECOLOR' in orange: "
149 "printf \"\\x1b[38;2;255;100;0mTRUECOLOR\\x1b[0m\\n\"")
151 "printf \"\\x1b[38;2;255;100;0mTRUECOLOR\\x1b[0m\\n\"")
150 ).tag(config=True)
152 ).tag(config=True)
151
153
152 editor = Unicode(get_default_editor(),
154 editor = Unicode(get_default_editor(),
153 help="Set the editor used by IPython (default to $EDITOR/vi/notepad)."
155 help="Set the editor used by IPython (default to $EDITOR/vi/notepad)."
154 ).tag(config=True)
156 ).tag(config=True)
155
157
156 prompts_class = Type(Prompts, help='Class used to generate Prompt token for prompt_toolkit').tag(config=True)
158 prompts_class = Type(Prompts, help='Class used to generate Prompt token for prompt_toolkit').tag(config=True)
157
159
158 prompts = Instance(Prompts)
160 prompts = Instance(Prompts)
159
161
160 @default('prompts')
162 @default('prompts')
161 def _prompts_default(self):
163 def _prompts_default(self):
162 return self.prompts_class(self)
164 return self.prompts_class(self)
163
165
164 @observe('prompts')
166 @observe('prompts')
165 def _(self, change):
167 def _(self, change):
166 self._update_layout()
168 self._update_layout()
167
169
168 @default('displayhook_class')
170 @default('displayhook_class')
169 def _displayhook_class_default(self):
171 def _displayhook_class_default(self):
170 return RichPromptDisplayHook
172 return RichPromptDisplayHook
171
173
172 term_title = Bool(True,
174 term_title = Bool(True,
173 help="Automatically set the terminal title"
175 help="Automatically set the terminal title"
174 ).tag(config=True)
176 ).tag(config=True)
175
177
176 display_completions = Enum(('column', 'multicolumn','readlinelike'),
178 display_completions = Enum(('column', 'multicolumn','readlinelike'),
177 help= ( "Options for displaying tab completions, 'column', 'multicolumn', and "
179 help= ( "Options for displaying tab completions, 'column', 'multicolumn', and "
178 "'readlinelike'. These options are for `prompt_toolkit`, see "
180 "'readlinelike'. These options are for `prompt_toolkit`, see "
179 "`prompt_toolkit` documentation for more information."
181 "`prompt_toolkit` documentation for more information."
180 ),
182 ),
181 default_value='multicolumn').tag(config=True)
183 default_value='multicolumn').tag(config=True)
182
184
183 highlight_matching_brackets = Bool(True,
185 highlight_matching_brackets = Bool(True,
184 help="Highlight matching brackets .",
186 help="Highlight matching brackets .",
185 ).tag(config=True)
187 ).tag(config=True)
186
188
187 @observe('term_title')
189 @observe('term_title')
188 def init_term_title(self, change=None):
190 def init_term_title(self, change=None):
189 # Enable or disable the terminal title.
191 # Enable or disable the terminal title.
190 if self.term_title:
192 if self.term_title:
191 toggle_set_term_title(True)
193 toggle_set_term_title(True)
192 set_term_title('IPython: ' + abbrev_cwd())
194 set_term_title('IPython: ' + abbrev_cwd())
193 else:
195 else:
194 toggle_set_term_title(False)
196 toggle_set_term_title(False)
195
197
196 def init_display_formatter(self):
198 def init_display_formatter(self):
197 super(TerminalInteractiveShell, self).init_display_formatter()
199 super(TerminalInteractiveShell, self).init_display_formatter()
198 # terminal only supports plain text
200 # terminal only supports plain text
199 self.display_formatter.active_types = ['text/plain']
201 self.display_formatter.active_types = ['text/plain']
200
202
201 def init_prompt_toolkit_cli(self):
203 def init_prompt_toolkit_cli(self):
202 if self.simple_prompt:
204 if self.simple_prompt:
203 # Fall back to plain non-interactive output for tests.
205 # Fall back to plain non-interactive output for tests.
204 # This is very limited, and only accepts a single line.
206 # This is very limited, and only accepts a single line.
205 def prompt():
207 def prompt():
206 return cast_unicode_py2(input('In [%d]: ' % self.execution_count))
208 return cast_unicode_py2(input('In [%d]: ' % self.execution_count))
207 self.prompt_for_code = prompt
209 self.prompt_for_code = prompt
208 return
210 return
209
211
210 # Set up keyboard shortcuts
212 # Set up keyboard shortcuts
211 kbmanager = KeyBindingManager.for_prompt()
213 kbmanager = KeyBindingManager.for_prompt()
212 register_ipython_shortcuts(kbmanager.registry, self)
214 register_ipython_shortcuts(kbmanager.registry, self)
213
215
214 # Pre-populate history from IPython's history database
216 # Pre-populate history from IPython's history database
215 history = InMemoryHistory()
217 history = InMemoryHistory()
216 last_cell = u""
218 last_cell = u""
217 for __, ___, cell in self.history_manager.get_tail(self.history_load_length,
219 for __, ___, cell in self.history_manager.get_tail(self.history_load_length,
218 include_latest=True):
220 include_latest=True):
219 # Ignore blank lines and consecutive duplicates
221 # Ignore blank lines and consecutive duplicates
220 cell = cell.rstrip()
222 cell = cell.rstrip()
221 if cell and (cell != last_cell):
223 if cell and (cell != last_cell):
222 history.append(cell)
224 history.append(cell)
223
225
224 self._style = self._make_style_from_name(self.highlighting_style)
226 self._style = self._make_style_from_name(self.highlighting_style)
225 style = DynamicStyle(lambda: self._style)
227 style = DynamicStyle(lambda: self._style)
226
228
227 editing_mode = getattr(EditingMode, self.editing_mode.upper())
229 editing_mode = getattr(EditingMode, self.editing_mode.upper())
228
230
229 self._pt_app = create_prompt_application(
231 self._pt_app = create_prompt_application(
230 editing_mode=editing_mode,
232 editing_mode=editing_mode,
231 key_bindings_registry=kbmanager.registry,
233 key_bindings_registry=kbmanager.registry,
232 history=history,
234 history=history,
233 completer=IPythonPTCompleter(self.Completer),
235 completer=IPythonPTCompleter(self.Completer),
234 enable_history_search=True,
236 enable_history_search=True,
235 style=style,
237 style=style,
236 mouse_support=self.mouse_support,
238 mouse_support=self.mouse_support,
237 **self._layout_options()
239 **self._layout_options()
238 )
240 )
239 self._eventloop = create_eventloop(self.inputhook)
241 self._eventloop = create_eventloop(self.inputhook)
240 self.pt_cli = CommandLineInterface(
242 self.pt_cli = CommandLineInterface(
241 self._pt_app, eventloop=self._eventloop,
243 self._pt_app, eventloop=self._eventloop,
242 output=create_output(true_color=self.true_color))
244 output=create_output(true_color=self.true_color))
243
245
244 def _make_style_from_name(self, name):
246 def _make_style_from_name(self, name):
245 """
247 """
246 Small wrapper that make an IPython compatible style from a style name
248 Small wrapper that make an IPython compatible style from a style name
247
249
248 We need that to add style for prompt ... etc.
250 We need that to add style for prompt ... etc.
249 """
251 """
250 style_overrides = {}
252 style_overrides = {}
251 if name == 'legacy':
253 if name == 'legacy':
252 legacy = self.colors.lower()
254 legacy = self.colors.lower()
253 if legacy == 'linux':
255 if legacy == 'linux':
254 style_cls = get_style_by_name('monokai')
256 style_cls = get_style_by_name('monokai')
255 style_overrides = _style_overrides_linux
257 style_overrides = _style_overrides_linux
256 elif legacy == 'lightbg':
258 elif legacy == 'lightbg':
257 style_overrides = _style_overrides_light_bg
259 style_overrides = _style_overrides_light_bg
258 style_cls = get_style_by_name('pastie')
260 style_cls = get_style_by_name('pastie')
259 elif legacy == 'neutral':
261 elif legacy == 'neutral':
260 # The default theme needs to be visible on both a dark background
262 # The default theme needs to be visible on both a dark background
261 # and a light background, because we can't tell what the terminal
263 # and a light background, because we can't tell what the terminal
262 # looks like. These tweaks to the default theme help with that.
264 # looks like. These tweaks to the default theme help with that.
263 style_cls = get_style_by_name('default')
265 style_cls = get_style_by_name('default')
264 style_overrides.update({
266 style_overrides.update({
265 Token.Number: '#007700',
267 Token.Number: '#007700',
266 Token.Operator: 'noinherit',
268 Token.Operator: 'noinherit',
267 Token.String: '#BB6622',
269 Token.String: '#BB6622',
268 Token.Name.Function: '#2080D0',
270 Token.Name.Function: '#2080D0',
269 Token.Name.Class: 'bold #2080D0',
271 Token.Name.Class: 'bold #2080D0',
270 Token.Name.Namespace: 'bold #2080D0',
272 Token.Name.Namespace: 'bold #2080D0',
271 Token.Prompt: '#009900',
273 Token.Prompt: '#009900',
272 Token.PromptNum: '#00ff00 bold',
274 Token.PromptNum: '#00ff00 bold',
273 Token.OutPrompt: '#990000',
275 Token.OutPrompt: '#990000',
274 Token.OutPromptNum: '#ff0000 bold',
276 Token.OutPromptNum: '#ff0000 bold',
275 })
277 })
276 elif legacy =='nocolor':
278 elif legacy =='nocolor':
277 style_cls=_NoStyle
279 style_cls=_NoStyle
278 style_overrides = {}
280 style_overrides = {}
279 else :
281 else :
280 raise ValueError('Got unknown colors: ', legacy)
282 raise ValueError('Got unknown colors: ', legacy)
281 else :
283 else :
282 style_cls = get_style_by_name(name)
284 style_cls = get_style_by_name(name)
283 style_overrides = {
285 style_overrides = {
284 Token.Prompt: '#009900',
286 Token.Prompt: '#009900',
285 Token.PromptNum: '#00ff00 bold',
287 Token.PromptNum: '#00ff00 bold',
286 Token.OutPrompt: '#990000',
288 Token.OutPrompt: '#990000',
287 Token.OutPromptNum: '#ff0000 bold',
289 Token.OutPromptNum: '#ff0000 bold',
288 }
290 }
289 style_overrides.update(self.highlighting_style_overrides)
291 style_overrides.update(self.highlighting_style_overrides)
290 style = PygmentsStyle.from_defaults(pygments_style_cls=style_cls,
292 style = PygmentsStyle.from_defaults(pygments_style_cls=style_cls,
291 style_dict=style_overrides)
293 style_dict=style_overrides)
292
294
293 return style
295 return style
294
296
295 def _layout_options(self):
297 def _layout_options(self):
296 """
298 """
297 Return the current layout option for the current Terminal InteractiveShell
299 Return the current layout option for the current Terminal InteractiveShell
298 """
300 """
299 return {
301 return {
300 'lexer':IPythonPTLexer(),
302 'lexer':IPythonPTLexer(),
301 'reserve_space_for_menu':self.space_for_menu,
303 'reserve_space_for_menu':self.space_for_menu,
302 'get_prompt_tokens':self.prompts.in_prompt_tokens,
304 'get_prompt_tokens':self.prompts.in_prompt_tokens,
303 'get_continuation_tokens':self.prompts.continuation_prompt_tokens,
305 'get_continuation_tokens':self.prompts.continuation_prompt_tokens,
304 'multiline':True,
306 'multiline':True,
305 'display_completions_in_columns': (self.display_completions == 'multicolumn'),
307 'display_completions_in_columns': (self.display_completions == 'multicolumn'),
306
308
307 # Highlight matching brackets, but only when this setting is
309 # Highlight matching brackets, but only when this setting is
308 # enabled, and only when the DEFAULT_BUFFER has the focus.
310 # enabled, and only when the DEFAULT_BUFFER has the focus.
309 'extra_input_processors': [ConditionalProcessor(
311 'extra_input_processors': [ConditionalProcessor(
310 processor=HighlightMatchingBracketProcessor(chars='[](){}'),
312 processor=HighlightMatchingBracketProcessor(chars='[](){}'),
311 filter=HasFocus(DEFAULT_BUFFER) & ~IsDone() &
313 filter=HasFocus(DEFAULT_BUFFER) & ~IsDone() &
312 Condition(lambda cli: self.highlight_matching_brackets))],
314 Condition(lambda cli: self.highlight_matching_brackets))],
313 }
315 }
314
316
315 def _update_layout(self):
317 def _update_layout(self):
316 """
318 """
317 Ask for a re computation of the application layout, if for example ,
319 Ask for a re computation of the application layout, if for example ,
318 some configuration options have changed.
320 some configuration options have changed.
319 """
321 """
320 if self._pt_app:
322 if self._pt_app:
321 self._pt_app.layout = create_prompt_layout(**self._layout_options())
323 self._pt_app.layout = create_prompt_layout(**self._layout_options())
322
324
323 def prompt_for_code(self):
325 def prompt_for_code(self):
324 document = self.pt_cli.run(
326 document = self.pt_cli.run(
325 pre_run=self.pre_prompt, reset_current_buffer=True)
327 pre_run=self.pre_prompt, reset_current_buffer=True)
326 return document.text
328 return document.text
327
329
328 def enable_win_unicode_console(self):
330 def enable_win_unicode_console(self):
329 import win_unicode_console
331 import win_unicode_console
330
332
331 if PY3:
333 if PY3:
332 win_unicode_console.enable()
334 win_unicode_console.enable()
333 else:
335 else:
334 # https://github.com/ipython/ipython/issues/9768
336 # https://github.com/ipython/ipython/issues/9768
335 from win_unicode_console.streams import (TextStreamWrapper,
337 from win_unicode_console.streams import (TextStreamWrapper,
336 stdout_text_transcoded, stderr_text_transcoded)
338 stdout_text_transcoded, stderr_text_transcoded)
337
339
338 class LenientStrStreamWrapper(TextStreamWrapper):
340 class LenientStrStreamWrapper(TextStreamWrapper):
339 def write(self, s):
341 def write(self, s):
340 if isinstance(s, bytes):
342 if isinstance(s, bytes):
341 s = s.decode(self.encoding, 'replace')
343 s = s.decode(self.encoding, 'replace')
342
344
343 self.base.write(s)
345 self.base.write(s)
344
346
345 stdout_text_str = LenientStrStreamWrapper(stdout_text_transcoded)
347 stdout_text_str = LenientStrStreamWrapper(stdout_text_transcoded)
346 stderr_text_str = LenientStrStreamWrapper(stderr_text_transcoded)
348 stderr_text_str = LenientStrStreamWrapper(stderr_text_transcoded)
347
349
348 win_unicode_console.enable(stdout=stdout_text_str,
350 win_unicode_console.enable(stdout=stdout_text_str,
349 stderr=stderr_text_str)
351 stderr=stderr_text_str)
350
352
351 def init_io(self):
353 def init_io(self):
352 if sys.platform not in {'win32', 'cli'}:
354 if sys.platform not in {'win32', 'cli'}:
353 return
355 return
354
356
355 self.enable_win_unicode_console()
357 self.enable_win_unicode_console()
356
358
357 import colorama
359 import colorama
358 colorama.init()
360 colorama.init()
359
361
360 # For some reason we make these wrappers around stdout/stderr.
362 # For some reason we make these wrappers around stdout/stderr.
361 # For now, we need to reset them so all output gets coloured.
363 # For now, we need to reset them so all output gets coloured.
362 # https://github.com/ipython/ipython/issues/8669
364 # https://github.com/ipython/ipython/issues/8669
363 from IPython.utils import io
365 # io.std* are deprecated, but don't show our own deprecation warnings
364 io.stdout = io.IOStream(sys.stdout)
366 # during initialization of the deprecated API.
365 io.stderr = io.IOStream(sys.stderr)
367 with warnings.catch_warnings():
368 warnings.simplefilter('ignore', DeprecationWarning)
369 io.stdout = io.IOStream(sys.stdout)
370 io.stderr = io.IOStream(sys.stderr)
366
371
367 def init_magics(self):
372 def init_magics(self):
368 super(TerminalInteractiveShell, self).init_magics()
373 super(TerminalInteractiveShell, self).init_magics()
369 self.register_magics(TerminalMagics)
374 self.register_magics(TerminalMagics)
370
375
371 def init_alias(self):
376 def init_alias(self):
372 # The parent class defines aliases that can be safely used with any
377 # The parent class defines aliases that can be safely used with any
373 # frontend.
378 # frontend.
374 super(TerminalInteractiveShell, self).init_alias()
379 super(TerminalInteractiveShell, self).init_alias()
375
380
376 # Now define aliases that only make sense on the terminal, because they
381 # Now define aliases that only make sense on the terminal, because they
377 # need direct access to the console in a way that we can't emulate in
382 # need direct access to the console in a way that we can't emulate in
378 # GUI or web frontend
383 # GUI or web frontend
379 if os.name == 'posix':
384 if os.name == 'posix':
380 for cmd in ['clear', 'more', 'less', 'man']:
385 for cmd in ['clear', 'more', 'less', 'man']:
381 self.alias_manager.soft_define_alias(cmd, cmd)
386 self.alias_manager.soft_define_alias(cmd, cmd)
382
387
383
388
384 def __init__(self, *args, **kwargs):
389 def __init__(self, *args, **kwargs):
385 super(TerminalInteractiveShell, self).__init__(*args, **kwargs)
390 super(TerminalInteractiveShell, self).__init__(*args, **kwargs)
386 self.init_prompt_toolkit_cli()
391 self.init_prompt_toolkit_cli()
387 self.init_term_title()
392 self.init_term_title()
388 self.keep_running = True
393 self.keep_running = True
389
394
390 self.debugger_history = InMemoryHistory()
395 self.debugger_history = InMemoryHistory()
391
396
392 def ask_exit(self):
397 def ask_exit(self):
393 self.keep_running = False
398 self.keep_running = False
394
399
395 rl_next_input = None
400 rl_next_input = None
396
401
397 def pre_prompt(self):
402 def pre_prompt(self):
398 if self.rl_next_input:
403 if self.rl_next_input:
399 self.pt_cli.application.buffer.text = cast_unicode_py2(self.rl_next_input)
404 self.pt_cli.application.buffer.text = cast_unicode_py2(self.rl_next_input)
400 self.rl_next_input = None
405 self.rl_next_input = None
401
406
402 def interact(self, display_banner=DISPLAY_BANNER_DEPRECATED):
407 def interact(self, display_banner=DISPLAY_BANNER_DEPRECATED):
403
408
404 if display_banner is not DISPLAY_BANNER_DEPRECATED:
409 if display_banner is not DISPLAY_BANNER_DEPRECATED:
405 warn('interact `display_banner` argument is deprecated since IPython 5.0. Call `show_banner()` if needed.', DeprecationWarning, stacklevel=2)
410 warn('interact `display_banner` argument is deprecated since IPython 5.0. Call `show_banner()` if needed.', DeprecationWarning, stacklevel=2)
406
411
407 while self.keep_running:
412 while self.keep_running:
408 print(self.separate_in, end='')
413 print(self.separate_in, end='')
409
414
410 try:
415 try:
411 code = self.prompt_for_code()
416 code = self.prompt_for_code()
412 except EOFError:
417 except EOFError:
413 if (not self.confirm_exit) \
418 if (not self.confirm_exit) \
414 or self.ask_yes_no('Do you really want to exit ([y]/n)?','y','n'):
419 or self.ask_yes_no('Do you really want to exit ([y]/n)?','y','n'):
415 self.ask_exit()
420 self.ask_exit()
416
421
417 else:
422 else:
418 if code:
423 if code:
419 self.run_cell(code, store_history=True)
424 self.run_cell(code, store_history=True)
420
425
421 def mainloop(self, display_banner=DISPLAY_BANNER_DEPRECATED):
426 def mainloop(self, display_banner=DISPLAY_BANNER_DEPRECATED):
422 # An extra layer of protection in case someone mashing Ctrl-C breaks
427 # An extra layer of protection in case someone mashing Ctrl-C breaks
423 # out of our internal code.
428 # out of our internal code.
424 if display_banner is not DISPLAY_BANNER_DEPRECATED:
429 if display_banner is not DISPLAY_BANNER_DEPRECATED:
425 warn('mainloop `display_banner` argument is deprecated since IPython 5.0. Call `show_banner()` if needed.', DeprecationWarning, stacklevel=2)
430 warn('mainloop `display_banner` argument is deprecated since IPython 5.0. Call `show_banner()` if needed.', DeprecationWarning, stacklevel=2)
426 while True:
431 while True:
427 try:
432 try:
428 self.interact()
433 self.interact()
429 break
434 break
430 except KeyboardInterrupt:
435 except KeyboardInterrupt:
431 print("\nKeyboardInterrupt escaped interact()\n")
436 print("\nKeyboardInterrupt escaped interact()\n")
432
437
433 if hasattr(self, '_eventloop'):
438 if hasattr(self, '_eventloop'):
434 self._eventloop.close()
439 self._eventloop.close()
435
440
436 _inputhook = None
441 _inputhook = None
437 def inputhook(self, context):
442 def inputhook(self, context):
438 if self._inputhook is not None:
443 if self._inputhook is not None:
439 self._inputhook(context)
444 self._inputhook(context)
440
445
441 def enable_gui(self, gui=None):
446 def enable_gui(self, gui=None):
442 if gui:
447 if gui:
443 self._inputhook = get_inputhook_func(gui)
448 self._inputhook = get_inputhook_func(gui)
444 else:
449 else:
445 self._inputhook = None
450 self._inputhook = None
446
451
447 # Run !system commands directly, not through pipes, so terminal programs
452 # Run !system commands directly, not through pipes, so terminal programs
448 # work correctly.
453 # work correctly.
449 system = InteractiveShell.system_raw
454 system = InteractiveShell.system_raw
450
455
451 def auto_rewrite_input(self, cmd):
456 def auto_rewrite_input(self, cmd):
452 """Overridden from the parent class to use fancy rewriting prompt"""
457 """Overridden from the parent class to use fancy rewriting prompt"""
453 if not self.show_rewritten_input:
458 if not self.show_rewritten_input:
454 return
459 return
455
460
456 tokens = self.prompts.rewrite_prompt_tokens()
461 tokens = self.prompts.rewrite_prompt_tokens()
457 if self.pt_cli:
462 if self.pt_cli:
458 self.pt_cli.print_tokens(tokens)
463 self.pt_cli.print_tokens(tokens)
459 print(cmd)
464 print(cmd)
460 else:
465 else:
461 prompt = ''.join(s for t, s in tokens)
466 prompt = ''.join(s for t, s in tokens)
462 print(prompt, cmd, sep='')
467 print(prompt, cmd, sep='')
463
468
464 _prompts_before = None
469 _prompts_before = None
465 def switch_doctest_mode(self, mode):
470 def switch_doctest_mode(self, mode):
466 """Switch prompts to classic for %doctest_mode"""
471 """Switch prompts to classic for %doctest_mode"""
467 if mode:
472 if mode:
468 self._prompts_before = self.prompts
473 self._prompts_before = self.prompts
469 self.prompts = ClassicPrompts(self)
474 self.prompts = ClassicPrompts(self)
470 elif self._prompts_before:
475 elif self._prompts_before:
471 self.prompts = self._prompts_before
476 self.prompts = self._prompts_before
472 self._prompts_before = None
477 self._prompts_before = None
473 self._update_layout()
478 self._update_layout()
474
479
475
480
476 InteractiveShellABC.register(TerminalInteractiveShell)
481 InteractiveShellABC.register(TerminalInteractiveShell)
477
482
478 if __name__ == '__main__':
483 if __name__ == '__main__':
479 TerminalInteractiveShell.instance().interact()
484 TerminalInteractiveShell.instance().interact()
@@ -1,151 +1,138 b''
1 """Global IPython app to support test running.
1 """Global IPython app to support test running.
2
2
3 We must start our own ipython object and heavily muck with it so that all the
3 We must start our own ipython object and heavily muck with it so that all the
4 modifications IPython makes to system behavior don't send the doctest machinery
4 modifications IPython makes to system behavior don't send the doctest machinery
5 into a fit. This code should be considered a gross hack, but it gets the job
5 into a fit. This code should be considered a gross hack, but it gets the job
6 done.
6 done.
7 """
7 """
8 from __future__ import absolute_import
8 from __future__ import absolute_import
9 from __future__ import print_function
9 from __future__ import print_function
10
10
11 #-----------------------------------------------------------------------------
11 # Copyright (c) IPython Development Team.
12 # Copyright (C) 2009-2011 The IPython Development Team
12 # Distributed under the terms of the Modified BSD License.
13 #
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 # stdlib
23 import sys
14 import sys
15 import warnings
24
16
25 # our own
26 from . import tools
17 from . import tools
27
18
28 from IPython.core import page
19 from IPython.core import page
29 from IPython.utils import io
20 from IPython.utils import io
30 from IPython.utils import py3compat
21 from IPython.utils import py3compat
31 from IPython.utils.py3compat import builtin_mod
22 from IPython.utils.py3compat import builtin_mod
32 from IPython.terminal.interactiveshell import TerminalInteractiveShell
23 from IPython.terminal.interactiveshell import TerminalInteractiveShell
33
24
34 #-----------------------------------------------------------------------------
35 # Functions
36 #-----------------------------------------------------------------------------
37
25
38 class StreamProxy(io.IOStream):
26 class StreamProxy(io.IOStream):
39 """Proxy for sys.stdout/err. This will request the stream *at call time*
27 """Proxy for sys.stdout/err. This will request the stream *at call time*
40 allowing for nose's Capture plugin's redirection of sys.stdout/err.
28 allowing for nose's Capture plugin's redirection of sys.stdout/err.
41
29
42 Parameters
30 Parameters
43 ----------
31 ----------
44 name : str
32 name : str
45 The name of the stream. This will be requested anew at every call
33 The name of the stream. This will be requested anew at every call
46 """
34 """
47
35
48 def __init__(self, name):
36 def __init__(self, name):
37 warnings.warn("StreamProxy is deprecated and unused as of IPython 5", DeprecationWarning,
38 stacklevel=2,
39 )
49 self.name=name
40 self.name=name
50
41
51 @property
42 @property
52 def stream(self):
43 def stream(self):
53 return getattr(sys, self.name)
44 return getattr(sys, self.name)
54
45
55 def flush(self):
46 def flush(self):
56 self.stream.flush()
47 self.stream.flush()
57
48
58
49
59 def get_ipython():
50 def get_ipython():
60 # This will get replaced by the real thing once we start IPython below
51 # This will get replaced by the real thing once we start IPython below
61 return start_ipython()
52 return start_ipython()
62
53
63
54
64 # A couple of methods to override those in the running IPython to interact
55 # A couple of methods to override those in the running IPython to interact
65 # better with doctest (doctest captures on raw stdout, so we need to direct
56 # better with doctest (doctest captures on raw stdout, so we need to direct
66 # various types of output there otherwise it will miss them).
57 # various types of output there otherwise it will miss them).
67
58
68 def xsys(self, cmd):
59 def xsys(self, cmd):
69 """Replace the default system call with a capturing one for doctest.
60 """Replace the default system call with a capturing one for doctest.
70 """
61 """
71 # We use getoutput, but we need to strip it because pexpect captures
62 # We use getoutput, but we need to strip it because pexpect captures
72 # the trailing newline differently from commands.getoutput
63 # the trailing newline differently from commands.getoutput
73 print(self.getoutput(cmd, split=False, depth=1).rstrip(), end='', file=sys.stdout)
64 print(self.getoutput(cmd, split=False, depth=1).rstrip(), end='', file=sys.stdout)
74 sys.stdout.flush()
65 sys.stdout.flush()
75
66
76
67
77 def _showtraceback(self, etype, evalue, stb):
68 def _showtraceback(self, etype, evalue, stb):
78 """Print the traceback purely on stdout for doctest to capture it.
69 """Print the traceback purely on stdout for doctest to capture it.
79 """
70 """
80 print(self.InteractiveTB.stb2text(stb), file=sys.stdout)
71 print(self.InteractiveTB.stb2text(stb), file=sys.stdout)
81
72
82
73
83 def start_ipython():
74 def start_ipython():
84 """Start a global IPython shell, which we need for IPython-specific syntax.
75 """Start a global IPython shell, which we need for IPython-specific syntax.
85 """
76 """
86 global get_ipython
77 global get_ipython
87
78
88 # This function should only ever run once!
79 # This function should only ever run once!
89 if hasattr(start_ipython, 'already_called'):
80 if hasattr(start_ipython, 'already_called'):
90 return
81 return
91 start_ipython.already_called = True
82 start_ipython.already_called = True
92
83
93 # Store certain global objects that IPython modifies
84 # Store certain global objects that IPython modifies
94 _displayhook = sys.displayhook
85 _displayhook = sys.displayhook
95 _excepthook = sys.excepthook
86 _excepthook = sys.excepthook
96 _main = sys.modules.get('__main__')
87 _main = sys.modules.get('__main__')
97
88
98 # Create custom argv and namespaces for our IPython to be test-friendly
89 # Create custom argv and namespaces for our IPython to be test-friendly
99 config = tools.default_config()
90 config = tools.default_config()
100 config.TerminalInteractiveShell.simple_prompt = True
91 config.TerminalInteractiveShell.simple_prompt = True
101
92
102 # Create and initialize our test-friendly IPython instance.
93 # Create and initialize our test-friendly IPython instance.
103 shell = TerminalInteractiveShell.instance(config=config,
94 shell = TerminalInteractiveShell.instance(config=config,
104 )
95 )
105
96
106 # A few more tweaks needed for playing nicely with doctests...
97 # A few more tweaks needed for playing nicely with doctests...
107
98
108 # remove history file
99 # remove history file
109 shell.tempfiles.append(config.HistoryManager.hist_file)
100 shell.tempfiles.append(config.HistoryManager.hist_file)
110
101
111 # These traps are normally only active for interactive use, set them
102 # These traps are normally only active for interactive use, set them
112 # permanently since we'll be mocking interactive sessions.
103 # permanently since we'll be mocking interactive sessions.
113 shell.builtin_trap.activate()
104 shell.builtin_trap.activate()
114
105
115 # Modify the IPython system call with one that uses getoutput, so that we
106 # Modify the IPython system call with one that uses getoutput, so that we
116 # can capture subcommands and print them to Python's stdout, otherwise the
107 # can capture subcommands and print them to Python's stdout, otherwise the
117 # doctest machinery would miss them.
108 # doctest machinery would miss them.
118 shell.system = py3compat.MethodType(xsys, shell)
109 shell.system = py3compat.MethodType(xsys, shell)
119
110
120 shell._showtraceback = py3compat.MethodType(_showtraceback, shell)
111 shell._showtraceback = py3compat.MethodType(_showtraceback, shell)
121
112
122 # IPython is ready, now clean up some global state...
113 # IPython is ready, now clean up some global state...
123
114
124 # Deactivate the various python system hooks added by ipython for
115 # Deactivate the various python system hooks added by ipython for
125 # interactive convenience so we don't confuse the doctest system
116 # interactive convenience so we don't confuse the doctest system
126 sys.modules['__main__'] = _main
117 sys.modules['__main__'] = _main
127 sys.displayhook = _displayhook
118 sys.displayhook = _displayhook
128 sys.excepthook = _excepthook
119 sys.excepthook = _excepthook
129
120
130 # So that ipython magics and aliases can be doctested (they work by making
121 # So that ipython magics and aliases can be doctested (they work by making
131 # a call into a global _ip object). Also make the top-level get_ipython
122 # a call into a global _ip object). Also make the top-level get_ipython
132 # now return this without recursively calling here again.
123 # now return this without recursively calling here again.
133 _ip = shell
124 _ip = shell
134 get_ipython = _ip.get_ipython
125 get_ipython = _ip.get_ipython
135 builtin_mod._ip = _ip
126 builtin_mod._ip = _ip
136 builtin_mod.get_ipython = get_ipython
127 builtin_mod.get_ipython = get_ipython
137
128
138 # To avoid extra IPython messages during testing, suppress io.stdout/stderr
139 io.stdout = StreamProxy('stdout')
140 io.stderr = StreamProxy('stderr')
141
142 # Override paging, so we don't require user interaction during the tests.
129 # Override paging, so we don't require user interaction during the tests.
143 def nopage(strng, start=0, screen_lines=0, pager_cmd=None):
130 def nopage(strng, start=0, screen_lines=0, pager_cmd=None):
144 if isinstance(strng, dict):
131 if isinstance(strng, dict):
145 strng = strng.get('text/plain', '')
132 strng = strng.get('text/plain', '')
146 print(strng)
133 print(strng)
147
134
148 page.orig_page = page.pager_page
135 page.orig_page = page.pager_page
149 page.pager_page = nopage
136 page.pager_page = nopage
150
137
151 return _ip
138 return _ip
@@ -1,235 +1,241 b''
1 # encoding: utf-8
1 # encoding: utf-8
2 """
2 """
3 IO related utilities.
3 IO related utilities.
4 """
4 """
5
5
6 # Copyright (c) IPython Development Team.
6 # Copyright (c) IPython Development Team.
7 # Distributed under the terms of the Modified BSD License.
7 # Distributed under the terms of the Modified BSD License.
8
8
9 from __future__ import print_function
9 from __future__ import print_function
10 from __future__ import absolute_import
10 from __future__ import absolute_import
11
11
12
12
13 import atexit
13 import atexit
14 import os
14 import os
15 import sys
15 import sys
16 import tempfile
16 import tempfile
17 import warnings
17 from warnings import warn
18 from warnings import warn
18
19
19 from IPython.utils.decorators import undoc
20 from IPython.utils.decorators import undoc
20 from .capture import CapturedIO, capture_output
21 from .capture import CapturedIO, capture_output
21 from .py3compat import string_types, input, PY3
22 from .py3compat import string_types, input, PY3
22
23
23 @undoc
24 @undoc
24 class IOStream:
25 class IOStream:
25
26
26 def __init__(self, stream, fallback=None):
27 def __init__(self, stream, fallback=None):
27 warn('IOStream is deprecated since IPython 5.0, use sys.{stdin,stdout,stderr} instead',
28 warn('IOStream is deprecated since IPython 5.0, use sys.{stdin,stdout,stderr} instead',
28 DeprecationWarning, stacklevel=2)
29 DeprecationWarning, stacklevel=2)
29 if not hasattr(stream,'write') or not hasattr(stream,'flush'):
30 if not hasattr(stream,'write') or not hasattr(stream,'flush'):
30 if fallback is not None:
31 if fallback is not None:
31 stream = fallback
32 stream = fallback
32 else:
33 else:
33 raise ValueError("fallback required, but not specified")
34 raise ValueError("fallback required, but not specified")
34 self.stream = stream
35 self.stream = stream
35 self._swrite = stream.write
36 self._swrite = stream.write
36
37
37 # clone all methods not overridden:
38 # clone all methods not overridden:
38 def clone(meth):
39 def clone(meth):
39 return not hasattr(self, meth) and not meth.startswith('_')
40 return not hasattr(self, meth) and not meth.startswith('_')
40 for meth in filter(clone, dir(stream)):
41 for meth in filter(clone, dir(stream)):
41 setattr(self, meth, getattr(stream, meth))
42 setattr(self, meth, getattr(stream, meth))
42
43
43 def __repr__(self):
44 def __repr__(self):
44 cls = self.__class__
45 cls = self.__class__
45 tpl = '{mod}.{cls}({args})'
46 tpl = '{mod}.{cls}({args})'
46 return tpl.format(mod=cls.__module__, cls=cls.__name__, args=self.stream)
47 return tpl.format(mod=cls.__module__, cls=cls.__name__, args=self.stream)
47
48
48 def write(self,data):
49 def write(self,data):
49 warn('IOStream is deprecated since IPython 5.0, use sys.{stdin,stdout,stderr} instead',
50 warn('IOStream is deprecated since IPython 5.0, use sys.{stdin,stdout,stderr} instead',
50 DeprecationWarning, stacklevel=2)
51 DeprecationWarning, stacklevel=2)
51 try:
52 try:
52 self._swrite(data)
53 self._swrite(data)
53 except:
54 except:
54 try:
55 try:
55 # print handles some unicode issues which may trip a plain
56 # print handles some unicode issues which may trip a plain
56 # write() call. Emulate write() by using an empty end
57 # write() call. Emulate write() by using an empty end
57 # argument.
58 # argument.
58 print(data, end='', file=self.stream)
59 print(data, end='', file=self.stream)
59 except:
60 except:
60 # if we get here, something is seriously broken.
61 # if we get here, something is seriously broken.
61 print('ERROR - failed to write data to stream:', self.stream,
62 print('ERROR - failed to write data to stream:', self.stream,
62 file=sys.stderr)
63 file=sys.stderr)
63
64
64 def writelines(self, lines):
65 def writelines(self, lines):
65 warn('IOStream is deprecated since IPython 5.0, use sys.{stdin,stdout,stderr} instead',
66 warn('IOStream is deprecated since IPython 5.0, use sys.{stdin,stdout,stderr} instead',
66 DeprecationWarning, stacklevel=2)
67 DeprecationWarning, stacklevel=2)
67 if isinstance(lines, string_types):
68 if isinstance(lines, string_types):
68 lines = [lines]
69 lines = [lines]
69 for line in lines:
70 for line in lines:
70 self.write(line)
71 self.write(line)
71
72
72 # This class used to have a writeln method, but regular files and streams
73 # This class used to have a writeln method, but regular files and streams
73 # in Python don't have this method. We need to keep this completely
74 # in Python don't have this method. We need to keep this completely
74 # compatible so we removed it.
75 # compatible so we removed it.
75
76
76 @property
77 @property
77 def closed(self):
78 def closed(self):
78 return self.stream.closed
79 return self.stream.closed
79
80
80 def close(self):
81 def close(self):
81 pass
82 pass
82
83
83 # setup stdin/stdout/stderr to sys.stdin/sys.stdout/sys.stderr
84 # setup stdin/stdout/stderr to sys.stdin/sys.stdout/sys.stderr
84 devnull = open(os.devnull, 'w')
85 devnull = open(os.devnull, 'w')
85 atexit.register(devnull.close)
86 atexit.register(devnull.close)
86 stdin = IOStream(sys.stdin, fallback=devnull)
87
87 stdout = IOStream(sys.stdout, fallback=devnull)
88 # io.std* are deprecated, but don't show our own deprecation warnings
88 stderr = IOStream(sys.stderr, fallback=devnull)
89 # during initialization of the deprecated API.
90 with warnings.catch_warnings():
91 warnings.simplefilter('ignore', DeprecationWarning)
92 stdin = IOStream(sys.stdin, fallback=devnull)
93 stdout = IOStream(sys.stdout, fallback=devnull)
94 stderr = IOStream(sys.stderr, fallback=devnull)
89
95
90 class Tee(object):
96 class Tee(object):
91 """A class to duplicate an output stream to stdout/err.
97 """A class to duplicate an output stream to stdout/err.
92
98
93 This works in a manner very similar to the Unix 'tee' command.
99 This works in a manner very similar to the Unix 'tee' command.
94
100
95 When the object is closed or deleted, it closes the original file given to
101 When the object is closed or deleted, it closes the original file given to
96 it for duplication.
102 it for duplication.
97 """
103 """
98 # Inspired by:
104 # Inspired by:
99 # http://mail.python.org/pipermail/python-list/2007-May/442737.html
105 # http://mail.python.org/pipermail/python-list/2007-May/442737.html
100
106
101 def __init__(self, file_or_name, mode="w", channel='stdout'):
107 def __init__(self, file_or_name, mode="w", channel='stdout'):
102 """Construct a new Tee object.
108 """Construct a new Tee object.
103
109
104 Parameters
110 Parameters
105 ----------
111 ----------
106 file_or_name : filename or open filehandle (writable)
112 file_or_name : filename or open filehandle (writable)
107 File that will be duplicated
113 File that will be duplicated
108
114
109 mode : optional, valid mode for open().
115 mode : optional, valid mode for open().
110 If a filename was give, open with this mode.
116 If a filename was give, open with this mode.
111
117
112 channel : str, one of ['stdout', 'stderr']
118 channel : str, one of ['stdout', 'stderr']
113 """
119 """
114 if channel not in ['stdout', 'stderr']:
120 if channel not in ['stdout', 'stderr']:
115 raise ValueError('Invalid channel spec %s' % channel)
121 raise ValueError('Invalid channel spec %s' % channel)
116
122
117 if hasattr(file_or_name, 'write') and hasattr(file_or_name, 'seek'):
123 if hasattr(file_or_name, 'write') and hasattr(file_or_name, 'seek'):
118 self.file = file_or_name
124 self.file = file_or_name
119 else:
125 else:
120 self.file = open(file_or_name, mode)
126 self.file = open(file_or_name, mode)
121 self.channel = channel
127 self.channel = channel
122 self.ostream = getattr(sys, channel)
128 self.ostream = getattr(sys, channel)
123 setattr(sys, channel, self)
129 setattr(sys, channel, self)
124 self._closed = False
130 self._closed = False
125
131
126 def close(self):
132 def close(self):
127 """Close the file and restore the channel."""
133 """Close the file and restore the channel."""
128 self.flush()
134 self.flush()
129 setattr(sys, self.channel, self.ostream)
135 setattr(sys, self.channel, self.ostream)
130 self.file.close()
136 self.file.close()
131 self._closed = True
137 self._closed = True
132
138
133 def write(self, data):
139 def write(self, data):
134 """Write data to both channels."""
140 """Write data to both channels."""
135 self.file.write(data)
141 self.file.write(data)
136 self.ostream.write(data)
142 self.ostream.write(data)
137 self.ostream.flush()
143 self.ostream.flush()
138
144
139 def flush(self):
145 def flush(self):
140 """Flush both channels."""
146 """Flush both channels."""
141 self.file.flush()
147 self.file.flush()
142 self.ostream.flush()
148 self.ostream.flush()
143
149
144 def __del__(self):
150 def __del__(self):
145 if not self._closed:
151 if not self._closed:
146 self.close()
152 self.close()
147
153
148
154
149 def ask_yes_no(prompt, default=None, interrupt=None):
155 def ask_yes_no(prompt, default=None, interrupt=None):
150 """Asks a question and returns a boolean (y/n) answer.
156 """Asks a question and returns a boolean (y/n) answer.
151
157
152 If default is given (one of 'y','n'), it is used if the user input is
158 If default is given (one of 'y','n'), it is used if the user input is
153 empty. If interrupt is given (one of 'y','n'), it is used if the user
159 empty. If interrupt is given (one of 'y','n'), it is used if the user
154 presses Ctrl-C. Otherwise the question is repeated until an answer is
160 presses Ctrl-C. Otherwise the question is repeated until an answer is
155 given.
161 given.
156
162
157 An EOF is treated as the default answer. If there is no default, an
163 An EOF is treated as the default answer. If there is no default, an
158 exception is raised to prevent infinite loops.
164 exception is raised to prevent infinite loops.
159
165
160 Valid answers are: y/yes/n/no (match is not case sensitive)."""
166 Valid answers are: y/yes/n/no (match is not case sensitive)."""
161
167
162 answers = {'y':True,'n':False,'yes':True,'no':False}
168 answers = {'y':True,'n':False,'yes':True,'no':False}
163 ans = None
169 ans = None
164 while ans not in answers.keys():
170 while ans not in answers.keys():
165 try:
171 try:
166 ans = input(prompt+' ').lower()
172 ans = input(prompt+' ').lower()
167 if not ans: # response was an empty string
173 if not ans: # response was an empty string
168 ans = default
174 ans = default
169 except KeyboardInterrupt:
175 except KeyboardInterrupt:
170 if interrupt:
176 if interrupt:
171 ans = interrupt
177 ans = interrupt
172 except EOFError:
178 except EOFError:
173 if default in answers.keys():
179 if default in answers.keys():
174 ans = default
180 ans = default
175 print()
181 print()
176 else:
182 else:
177 raise
183 raise
178
184
179 return answers[ans]
185 return answers[ans]
180
186
181
187
182 def temp_pyfile(src, ext='.py'):
188 def temp_pyfile(src, ext='.py'):
183 """Make a temporary python file, return filename and filehandle.
189 """Make a temporary python file, return filename and filehandle.
184
190
185 Parameters
191 Parameters
186 ----------
192 ----------
187 src : string or list of strings (no need for ending newlines if list)
193 src : string or list of strings (no need for ending newlines if list)
188 Source code to be written to the file.
194 Source code to be written to the file.
189
195
190 ext : optional, string
196 ext : optional, string
191 Extension for the generated file.
197 Extension for the generated file.
192
198
193 Returns
199 Returns
194 -------
200 -------
195 (filename, open filehandle)
201 (filename, open filehandle)
196 It is the caller's responsibility to close the open file and unlink it.
202 It is the caller's responsibility to close the open file and unlink it.
197 """
203 """
198 fname = tempfile.mkstemp(ext)[1]
204 fname = tempfile.mkstemp(ext)[1]
199 f = open(fname,'w')
205 f = open(fname,'w')
200 f.write(src)
206 f.write(src)
201 f.flush()
207 f.flush()
202 return fname, f
208 return fname, f
203
209
204 def atomic_writing(*args, **kwargs):
210 def atomic_writing(*args, **kwargs):
205 """DEPRECATED: moved to notebook.services.contents.fileio"""
211 """DEPRECATED: moved to notebook.services.contents.fileio"""
206 warn("IPython.utils.io.atomic_writing has moved to notebook.services.contents.fileio")
212 warn("IPython.utils.io.atomic_writing has moved to notebook.services.contents.fileio")
207 from notebook.services.contents.fileio import atomic_writing
213 from notebook.services.contents.fileio import atomic_writing
208 return atomic_writing(*args, **kwargs)
214 return atomic_writing(*args, **kwargs)
209
215
210 def raw_print(*args, **kw):
216 def raw_print(*args, **kw):
211 """Raw print to sys.__stdout__, otherwise identical interface to print()."""
217 """Raw print to sys.__stdout__, otherwise identical interface to print()."""
212
218
213 print(*args, sep=kw.get('sep', ' '), end=kw.get('end', '\n'),
219 print(*args, sep=kw.get('sep', ' '), end=kw.get('end', '\n'),
214 file=sys.__stdout__)
220 file=sys.__stdout__)
215 sys.__stdout__.flush()
221 sys.__stdout__.flush()
216
222
217
223
218 def raw_print_err(*args, **kw):
224 def raw_print_err(*args, **kw):
219 """Raw print to sys.__stderr__, otherwise identical interface to print()."""
225 """Raw print to sys.__stderr__, otherwise identical interface to print()."""
220
226
221 print(*args, sep=kw.get('sep', ' '), end=kw.get('end', '\n'),
227 print(*args, sep=kw.get('sep', ' '), end=kw.get('end', '\n'),
222 file=sys.__stderr__)
228 file=sys.__stderr__)
223 sys.__stderr__.flush()
229 sys.__stderr__.flush()
224
230
225
231
226 # Short aliases for quick debugging, do NOT use these in production code.
232 # Short aliases for quick debugging, do NOT use these in production code.
227 rprint = raw_print
233 rprint = raw_print
228 rprinte = raw_print_err
234 rprinte = raw_print_err
229
235
230
236
231 def unicode_std_stream(stream='stdout'):
237 def unicode_std_stream(stream='stdout'):
232 """DEPRECATED, moved to nbconvert.utils.io"""
238 """DEPRECATED, moved to nbconvert.utils.io"""
233 warn("IPython.utils.io.unicode_std_stream has moved to nbconvert.utils.io")
239 warn("IPython.utils.io.unicode_std_stream has moved to nbconvert.utils.io")
234 from nbconvert.utils.io import unicode_std_stream
240 from nbconvert.utils.io import unicode_std_stream
235 return unicode_std_stream(stream)
241 return unicode_std_stream(stream)
@@ -1,65 +1,65 b''
1 # encoding: utf-8
1 # encoding: utf-8
2 """
2 """
3 Utilities for warnings. Shoudn't we just use the built in warnings module.
3 Utilities for warnings. Shoudn't we just use the built in warnings module.
4 """
4 """
5
5
6 # Copyright (c) IPython Development Team.
6 # Copyright (c) IPython Development Team.
7 # Distributed under the terms of the Modified BSD License.
7 # Distributed under the terms of the Modified BSD License.
8
8
9 from __future__ import print_function
9 from __future__ import print_function
10
10
11 import sys
11 import sys
12 import warnings
12 import warnings
13
13
14 warnings.warn("The module IPython.utils.warn is deprecated since IPython 4.0, use the standard warnings module instead", DeprecationWarning)
14 warnings.warn("The module IPython.utils.warn is deprecated since IPython 4.0, use the standard warnings module instead", DeprecationWarning)
15
15
16 def warn(msg,level=2,exit_val=1):
16 def warn(msg,level=2,exit_val=1):
17 """Deprecated
17 """Deprecated
18
18
19 Standard warning printer. Gives formatting consistency.
19 Standard warning printer. Gives formatting consistency.
20
20
21 Output is sent to io.stderr (sys.stderr by default).
21 Output is sent to sys.stderr.
22
22
23 Options:
23 Options:
24
24
25 -level(2): allows finer control:
25 -level(2): allows finer control:
26 0 -> Do nothing, dummy function.
26 0 -> Do nothing, dummy function.
27 1 -> Print message.
27 1 -> Print message.
28 2 -> Print 'WARNING:' + message. (Default level).
28 2 -> Print 'WARNING:' + message. (Default level).
29 3 -> Print 'ERROR:' + message.
29 3 -> Print 'ERROR:' + message.
30 4 -> Print 'FATAL ERROR:' + message and trigger a sys.exit(exit_val).
30 4 -> Print 'FATAL ERROR:' + message and trigger a sys.exit(exit_val).
31
31
32 -exit_val (1): exit value returned by sys.exit() for a level 4
32 -exit_val (1): exit value returned by sys.exit() for a level 4
33 warning. Ignored for all other levels."""
33 warning. Ignored for all other levels."""
34
34
35 warnings.warn("The module IPython.utils.warn is deprecated since IPython 4.0, use the standard warnings module instead", DeprecationWarning)
35 warnings.warn("The module IPython.utils.warn is deprecated since IPython 4.0, use the standard warnings module instead", DeprecationWarning)
36 if level>0:
36 if level>0:
37 header = ['','','WARNING: ','ERROR: ','FATAL ERROR: ']
37 header = ['','','WARNING: ','ERROR: ','FATAL ERROR: ']
38 print(header[level], msg, sep='', file=sys.stderr)
38 print(header[level], msg, sep='', file=sys.stderr)
39 if level == 4:
39 if level == 4:
40 print('Exiting.\n', file=sys.stderr)
40 print('Exiting.\n', file=sys.stderr)
41 sys.exit(exit_val)
41 sys.exit(exit_val)
42
42
43
43
44 def info(msg):
44 def info(msg):
45 """Deprecated
45 """Deprecated
46
46
47 Equivalent to warn(msg,level=1)."""
47 Equivalent to warn(msg,level=1)."""
48
48
49 warn(msg,level=1)
49 warn(msg,level=1)
50
50
51
51
52 def error(msg):
52 def error(msg):
53 """Deprecated
53 """Deprecated
54
54
55 Equivalent to warn(msg,level=3)."""
55 Equivalent to warn(msg,level=3)."""
56
56
57 warn(msg,level=3)
57 warn(msg,level=3)
58
58
59
59
60 def fatal(msg,exit_val=1):
60 def fatal(msg,exit_val=1):
61 """Deprecated
61 """Deprecated
62
62
63 Equivalent to warn(msg,exit_val=exit_val,level=4)."""
63 Equivalent to warn(msg,exit_val=exit_val,level=4)."""
64
64
65 warn(msg,exit_val=exit_val,level=4)
65 warn(msg,exit_val=exit_val,level=4)
General Comments 0
You need to be logged in to leave comments. Login now