##// END OF EJS Templates
fix some deprecations...
Min RK -
Show More
@@ -1,295 +1,295 b''
1 1 # -*- coding: utf-8 -*-
2 2 """Displayhook for IPython.
3 3
4 4 This defines a callable class that IPython uses for `sys.displayhook`.
5 5 """
6 6
7 7 # Copyright (c) IPython Development Team.
8 8 # Distributed under the terms of the Modified BSD License.
9 9
10 10 from __future__ import print_function
11 11
12 12 import sys
13 13 import io as _io
14 14 import tokenize
15 15
16 16 from traitlets.config.configurable import Configurable
17 17 from IPython.utils.py3compat import builtin_mod, cast_unicode_py2
18 18 from traitlets import Instance, Float
19 19 from warnings import warn
20 20
21 21 # TODO: Move the various attributes (cache_size, [others now moved]). Some
22 22 # of these are also attributes of InteractiveShell. They should be on ONE object
23 23 # only and the other objects should ask that one object for their values.
24 24
25 25 class DisplayHook(Configurable):
26 26 """The custom IPython displayhook to replace sys.displayhook.
27 27
28 28 This class does many things, but the basic idea is that it is a callable
29 29 that gets called anytime user code returns a value.
30 30 """
31 31
32 32 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC',
33 33 allow_none=True)
34 34 exec_result = Instance('IPython.core.interactiveshell.ExecutionResult',
35 35 allow_none=True)
36 36 cull_fraction = Float(0.2)
37 37
38 38 def __init__(self, shell=None, cache_size=1000, **kwargs):
39 39 super(DisplayHook, self).__init__(shell=shell, **kwargs)
40 40 cache_size_min = 3
41 41 if cache_size <= 0:
42 42 self.do_full_cache = 0
43 43 cache_size = 0
44 44 elif cache_size < cache_size_min:
45 45 self.do_full_cache = 0
46 46 cache_size = 0
47 47 warn('caching was disabled (min value for cache size is %s).' %
48 48 cache_size_min,level=3)
49 49 else:
50 50 self.do_full_cache = 1
51 51
52 52 self.cache_size = cache_size
53 53
54 54 # we need a reference to the user-level namespace
55 55 self.shell = shell
56 56
57 57 self._,self.__,self.___ = '','',''
58 58
59 59 # these are deliberately global:
60 60 to_user_ns = {'_':self._,'__':self.__,'___':self.___}
61 61 self.shell.user_ns.update(to_user_ns)
62 62
63 63 @property
64 64 def prompt_count(self):
65 65 return self.shell.execution_count
66 66
67 67 #-------------------------------------------------------------------------
68 68 # Methods used in __call__. Override these methods to modify the behavior
69 69 # of the displayhook.
70 70 #-------------------------------------------------------------------------
71 71
72 72 def check_for_underscore(self):
73 73 """Check if the user has set the '_' variable by hand."""
74 74 # If something injected a '_' variable in __builtin__, delete
75 75 # ipython's automatic one so we don't clobber that. gettext() in
76 76 # particular uses _, so we need to stay away from it.
77 77 if '_' in builtin_mod.__dict__:
78 78 try:
79 79 del self.shell.user_ns['_']
80 80 except KeyError:
81 81 pass
82 82
83 83 def quiet(self):
84 84 """Should we silence the display hook because of ';'?"""
85 85 # do not print output if input ends in ';'
86 86
87 87 try:
88 88 cell = cast_unicode_py2(self.shell.history_manager.input_hist_parsed[-1])
89 89 except IndexError:
90 90 # some uses of ipshellembed may fail here
91 91 return False
92 92
93 93 sio = _io.StringIO(cell)
94 94 tokens = list(tokenize.generate_tokens(sio.readline))
95 95
96 96 for token in reversed(tokens):
97 97 if token[0] in (tokenize.ENDMARKER, tokenize.NL, tokenize.NEWLINE, tokenize.COMMENT):
98 98 continue
99 99 if (token[0] == tokenize.OP) and (token[1] == ';'):
100 100 return True
101 101 else:
102 102 return False
103 103
104 104 def start_displayhook(self):
105 105 """Start the displayhook, initializing resources."""
106 106 pass
107 107
108 108 def write_output_prompt(self):
109 109 """Write the output prompt.
110 110
111 111 The default implementation simply writes the prompt to
112 ``io.stdout``.
112 ``sys.stdout``.
113 113 """
114 114 # Use write, not print which adds an extra space.
115 115 sys.stdout.write(self.shell.separate_out)
116 116 outprompt = 'Out[{}]: '.format(self.shell.execution_count)
117 117 if self.do_full_cache:
118 118 sys.stdout.write(outprompt)
119 119
120 120 def compute_format_data(self, result):
121 121 """Compute format data of the object to be displayed.
122 122
123 123 The format data is a generalization of the :func:`repr` of an object.
124 124 In the default implementation the format data is a :class:`dict` of
125 125 key value pair where the keys are valid MIME types and the values
126 126 are JSON'able data structure containing the raw data for that MIME
127 127 type. It is up to frontends to determine pick a MIME to to use and
128 128 display that data in an appropriate manner.
129 129
130 130 This method only computes the format data for the object and should
131 131 NOT actually print or write that to a stream.
132 132
133 133 Parameters
134 134 ----------
135 135 result : object
136 136 The Python object passed to the display hook, whose format will be
137 137 computed.
138 138
139 139 Returns
140 140 -------
141 141 (format_dict, md_dict) : dict
142 142 format_dict is a :class:`dict` whose keys are valid MIME types and values are
143 143 JSON'able raw data for that MIME type. It is recommended that
144 144 all return values of this should always include the "text/plain"
145 145 MIME type representation of the object.
146 146 md_dict is a :class:`dict` with the same MIME type keys
147 147 of metadata associated with each output.
148 148
149 149 """
150 150 return self.shell.display_formatter.format(result)
151 151
152 152 # This can be set to True by the write_output_prompt method in a subclass
153 153 prompt_end_newline = False
154 154
155 155 def write_format_data(self, format_dict, md_dict=None):
156 156 """Write the format data dict to the frontend.
157 157
158 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 160 override this method to send the entire `format_dict` to the
161 161 frontends.
162 162
163 163 Parameters
164 164 ----------
165 165 format_dict : dict
166 166 The format dict for the object passed to `sys.displayhook`.
167 167 md_dict : dict (optional)
168 168 The metadata dict to be associated with the display data.
169 169 """
170 170 if 'text/plain' not in format_dict:
171 171 # nothing to do
172 172 return
173 173 # We want to print because we want to always make sure we have a
174 174 # newline, even if all the prompt separators are ''. This is the
175 175 # standard IPython behavior.
176 176 result_repr = format_dict['text/plain']
177 177 if '\n' in result_repr:
178 178 # So that multi-line strings line up with the left column of
179 179 # the screen, instead of having the output prompt mess up
180 180 # their first line.
181 181 # We use the prompt template instead of the expanded prompt
182 182 # because the expansion may add ANSI escapes that will interfere
183 183 # with our ability to determine whether or not we should add
184 184 # a newline.
185 185 if not self.prompt_end_newline:
186 186 # But avoid extraneous empty lines.
187 187 result_repr = '\n' + result_repr
188 188
189 189 print(result_repr)
190 190
191 191 def update_user_ns(self, result):
192 192 """Update user_ns with various things like _, __, _1, etc."""
193 193
194 194 # Avoid recursive reference when displaying _oh/Out
195 195 if result is not self.shell.user_ns['_oh']:
196 196 if len(self.shell.user_ns['_oh']) >= self.cache_size and self.do_full_cache:
197 197 self.cull_cache()
198 198 # Don't overwrite '_' and friends if '_' is in __builtin__ (otherwise
199 199 # we cause buggy behavior for things like gettext).
200 200
201 201 if '_' not in builtin_mod.__dict__:
202 202 self.___ = self.__
203 203 self.__ = self._
204 204 self._ = result
205 205 self.shell.push({'_':self._,
206 206 '__':self.__,
207 207 '___':self.___}, interactive=False)
208 208
209 209 # hackish access to top-level namespace to create _1,_2... dynamically
210 210 to_main = {}
211 211 if self.do_full_cache:
212 212 new_result = '_'+repr(self.prompt_count)
213 213 to_main[new_result] = result
214 214 self.shell.push(to_main, interactive=False)
215 215 self.shell.user_ns['_oh'][self.prompt_count] = result
216 216
217 217 def fill_exec_result(self, result):
218 218 if self.exec_result is not None:
219 219 self.exec_result.result = result
220 220
221 221 def log_output(self, format_dict):
222 222 """Log the output."""
223 223 if 'text/plain' not in format_dict:
224 224 # nothing to do
225 225 return
226 226 if self.shell.logger.log_output:
227 227 self.shell.logger.log_write(format_dict['text/plain'], 'output')
228 228 self.shell.history_manager.output_hist_reprs[self.prompt_count] = \
229 229 format_dict['text/plain']
230 230
231 231 def finish_displayhook(self):
232 232 """Finish up all displayhook activities."""
233 233 sys.stdout.write(self.shell.separate_out2)
234 234 sys.stdout.flush()
235 235
236 236 def __call__(self, result=None):
237 237 """Printing with history cache management.
238 238
239 239 This is invoked everytime the interpreter needs to print, and is
240 240 activated by setting the variable sys.displayhook to it.
241 241 """
242 242 self.check_for_underscore()
243 243 if result is not None and not self.quiet():
244 244 self.start_displayhook()
245 245 self.write_output_prompt()
246 246 format_dict, md_dict = self.compute_format_data(result)
247 247 self.update_user_ns(result)
248 248 self.fill_exec_result(result)
249 249 if format_dict:
250 250 self.write_format_data(format_dict, md_dict)
251 251 self.log_output(format_dict)
252 252 self.finish_displayhook()
253 253
254 254 def cull_cache(self):
255 255 """Output cache is full, cull the oldest entries"""
256 256 oh = self.shell.user_ns.get('_oh', {})
257 257 sz = len(oh)
258 258 cull_count = max(int(sz * self.cull_fraction), 2)
259 259 warn('Output cache limit (currently {sz} entries) hit.\n'
260 260 'Flushing oldest {cull_count} entries.'.format(sz=sz, cull_count=cull_count))
261 261
262 262 for i, n in enumerate(sorted(oh)):
263 263 if i >= cull_count:
264 264 break
265 265 self.shell.user_ns.pop('_%i' % n, None)
266 266 oh.pop(n, None)
267 267
268 268
269 269 def flush(self):
270 270 if not self.do_full_cache:
271 271 raise ValueError("You shouldn't have reached the cache flush "
272 272 "if full caching is not enabled!")
273 273 # delete auto-generated vars from global namespace
274 274
275 275 for n in range(1,self.prompt_count + 1):
276 276 key = '_'+repr(n)
277 277 try:
278 278 del self.shell.user_ns[key]
279 279 except: pass
280 280 # In some embedded circumstances, the user_ns doesn't have the
281 281 # '_oh' key set up.
282 282 oh = self.shell.user_ns.get('_oh', None)
283 283 if oh is not None:
284 284 oh.clear()
285 285
286 286 # Release our own references to objects:
287 287 self._, self.__, self.___ = '', '', ''
288 288
289 289 if '_' not in builtin_mod.__dict__:
290 290 self.shell.user_ns.update({'_':None,'__':None, '___':None})
291 291 import gc
292 292 # TODO: Is this really needed?
293 293 # IronPython blocks here forever
294 294 if sys.platform != "cli":
295 295 gc.collect()
@@ -1,117 +1,117 b''
1 1 """An interface for publishing rich data to frontends.
2 2
3 3 There are two components of the display system:
4 4
5 5 * Display formatters, which take a Python object and compute the
6 6 representation of the object in various formats (text, HTML, SVG, etc.).
7 7 * The display publisher that is used to send the representation data to the
8 8 various frontends.
9 9
10 10 This module defines the logic display publishing. The display publisher uses
11 11 the ``display_data`` message type that is defined in the IPython messaging
12 12 spec.
13 13 """
14 14
15 15 # Copyright (c) IPython Development Team.
16 16 # Distributed under the terms of the Modified BSD License.
17 17
18 18 from __future__ import print_function
19 19
20 20 import sys
21 21
22 22 from traitlets.config.configurable import Configurable
23 23 from traitlets import List
24 24
25 25 # This used to be defined here - it is imported for backwards compatibility
26 26 from .display import publish_display_data
27 27
28 28 #-----------------------------------------------------------------------------
29 29 # Main payload class
30 30 #-----------------------------------------------------------------------------
31 31
32 32 class DisplayPublisher(Configurable):
33 33 """A traited class that publishes display data to frontends.
34 34
35 35 Instances of this class are created by the main IPython object and should
36 36 be accessed there.
37 37 """
38 38
39 39 def _validate_data(self, data, metadata=None):
40 40 """Validate the display data.
41 41
42 42 Parameters
43 43 ----------
44 44 data : dict
45 45 The formata data dictionary.
46 46 metadata : dict
47 47 Any metadata for the data.
48 48 """
49 49
50 50 if not isinstance(data, dict):
51 51 raise TypeError('data must be a dict, got: %r' % data)
52 52 if metadata is not None:
53 53 if not isinstance(metadata, dict):
54 54 raise TypeError('metadata must be a dict, got: %r' % data)
55 55
56 56 def publish(self, data, metadata=None, source=None):
57 57 """Publish data and metadata to all frontends.
58 58
59 59 See the ``display_data`` message in the messaging documentation for
60 60 more details about this message type.
61 61
62 62 The following MIME types are currently implemented:
63 63
64 64 * text/plain
65 65 * text/html
66 66 * text/markdown
67 67 * text/latex
68 68 * application/json
69 69 * application/javascript
70 70 * image/png
71 71 * image/jpeg
72 72 * image/svg+xml
73 73
74 74 Parameters
75 75 ----------
76 76 data : dict
77 77 A dictionary having keys that are valid MIME types (like
78 78 'text/plain' or 'image/svg+xml') and values that are the data for
79 79 that MIME type. The data itself must be a JSON'able data
80 80 structure. Minimally all data should have the 'text/plain' data,
81 81 which can be displayed by all frontends. If more than the plain
82 82 text is given, it is up to the frontend to decide which
83 83 representation to use.
84 84 metadata : dict
85 85 A dictionary for metadata related to the data. This can contain
86 86 arbitrary key, value pairs that frontends can use to interpret
87 87 the data. Metadata specific to each mime-type can be specified
88 88 in the metadata dict with the same mime-type keys as
89 89 the data itself.
90 90 source : str, deprecated
91 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 95 if 'text/plain' in data:
96 96 print(data['text/plain'])
97 97
98 98 def clear_output(self, wait=False):
99 99 """Clear the output of the cell receiving output."""
100 100 print('\033[2K\r', end='')
101 101 sys.stdout.flush()
102 102 print('\033[2K\r', end='')
103 103 sys.stderr.flush()
104 104
105 105
106 106 class CapturingDisplayPublisher(DisplayPublisher):
107 107 """A DisplayPublisher that stores"""
108 108 outputs = List()
109 109
110 110 def publish(self, data, metadata=None, source=None):
111 111 self.outputs.append((data, metadata))
112 112
113 113 def clear_output(self, wait=False):
114 114 super(CapturingDisplayPublisher, self).clear_output(wait)
115 115
116 116 # empty the list, *do not* reassign a new list
117 117 del self.outputs[:]
@@ -1,945 +1,947 b''
1 1 # -*- coding: utf-8 -*-
2 2 """Display formatters.
3 3
4 4 Inheritance diagram:
5 5
6 6 .. inheritance-diagram:: IPython.core.formatters
7 7 :parts: 3
8 8 """
9 9
10 10 # Copyright (c) IPython Development Team.
11 11 # Distributed under the terms of the Modified BSD License.
12 12
13 13 import abc
14 14 import json
15 15 import sys
16 16 import traceback
17 17 import warnings
18 18
19 19 from decorator import decorator
20 20
21 21 from traitlets.config.configurable import Configurable
22 22 from IPython.core.getipython import get_ipython
23 23 from IPython.utils.sentinel import Sentinel
24 24 from IPython.utils.dir2 import get_real_method
25 25 from IPython.lib import pretty
26 26 from traitlets import (
27 27 Bool, Dict, Integer, Unicode, CUnicode, ObjectName, List,
28 28 ForwardDeclaredInstance,
29 29 default, observe,
30 30 )
31 31 from IPython.utils.py3compat import (
32 32 with_metaclass, string_types, unicode_type,
33 33 )
34 34
35 35
36 36 class DisplayFormatter(Configurable):
37 37
38 38 active_types = List(Unicode(),
39 39 help="""List of currently active mime-types to display.
40 40 You can use this to set a white-list for formats to display.
41 41
42 42 Most users will not need to change this value.
43 43 """).tag(config=True)
44 44
45 45 @default('active_types')
46 46 def _active_types_default(self):
47 47 return self.format_types
48 48
49 49 @observe('active_types')
50 50 def _active_types_changed(self, change):
51 51 for key, formatter in self.formatters.items():
52 52 if key in change['new']:
53 53 formatter.enabled = True
54 54 else:
55 55 formatter.enabled = False
56 56
57 57 ipython_display_formatter = ForwardDeclaredInstance('FormatterABC')
58 58 @default('ipython_display_formatter')
59 59 def _default_formatter(self):
60 60 return IPythonDisplayFormatter(parent=self)
61 61
62 62 # A dict of formatter whose keys are format types (MIME types) and whose
63 63 # values are subclasses of BaseFormatter.
64 64 formatters = Dict()
65 65 @default('formatters')
66 66 def _formatters_default(self):
67 67 """Activate the default formatters."""
68 68 formatter_classes = [
69 69 PlainTextFormatter,
70 70 HTMLFormatter,
71 71 MarkdownFormatter,
72 72 SVGFormatter,
73 73 PNGFormatter,
74 74 PDFFormatter,
75 75 JPEGFormatter,
76 76 LatexFormatter,
77 77 JSONFormatter,
78 78 JavascriptFormatter
79 79 ]
80 80 d = {}
81 81 for cls in formatter_classes:
82 82 f = cls(parent=self)
83 83 d[f.format_type] = f
84 84 return d
85 85
86 86 def format(self, obj, include=None, exclude=None):
87 87 """Return a format data dict for an object.
88 88
89 89 By default all format types will be computed.
90 90
91 91 The following MIME types are currently implemented:
92 92
93 93 * text/plain
94 94 * text/html
95 95 * text/markdown
96 96 * text/latex
97 97 * application/json
98 98 * application/javascript
99 99 * application/pdf
100 100 * image/png
101 101 * image/jpeg
102 102 * image/svg+xml
103 103
104 104 Parameters
105 105 ----------
106 106 obj : object
107 107 The Python object whose format data will be computed.
108 108 include : list or tuple, optional
109 109 A list of format type strings (MIME types) to include in the
110 110 format data dict. If this is set *only* the format types included
111 111 in this list will be computed.
112 112 exclude : list or tuple, optional
113 113 A list of format type string (MIME types) to exclude in the format
114 114 data dict. If this is set all format types will be computed,
115 115 except for those included in this argument.
116 116
117 117 Returns
118 118 -------
119 119 (format_dict, metadata_dict) : tuple of two dicts
120 120
121 121 format_dict is a dictionary of key/value pairs, one of each format that was
122 122 generated for the object. The keys are the format types, which
123 123 will usually be MIME type strings and the values and JSON'able
124 124 data structure containing the raw data for the representation in
125 125 that format.
126 126
127 127 metadata_dict is a dictionary of metadata about each mime-type output.
128 128 Its keys will be a strict subset of the keys in format_dict.
129 129 """
130 130 format_dict = {}
131 131 md_dict = {}
132 132
133 133 if self.ipython_display_formatter(obj):
134 134 # object handled itself, don't proceed
135 135 return {}, {}
136 136
137 137 for format_type, formatter in self.formatters.items():
138 138 if include and format_type not in include:
139 139 continue
140 140 if exclude and format_type in exclude:
141 141 continue
142 142
143 143 md = None
144 144 try:
145 145 data = formatter(obj)
146 146 except:
147 147 # FIXME: log the exception
148 148 raise
149 149
150 150 # formatters can return raw data or (data, metadata)
151 151 if isinstance(data, tuple) and len(data) == 2:
152 152 data, md = data
153 153
154 154 if data is not None:
155 155 format_dict[format_type] = data
156 156 if md is not None:
157 157 md_dict[format_type] = md
158 158
159 159 return format_dict, md_dict
160 160
161 161 @property
162 162 def format_types(self):
163 163 """Return the format types (MIME types) of the active formatters."""
164 164 return list(self.formatters.keys())
165 165
166 166
167 167 #-----------------------------------------------------------------------------
168 168 # Formatters for specific format types (text, html, svg, etc.)
169 169 #-----------------------------------------------------------------------------
170 170
171 171
172 172 def _safe_repr(obj):
173 173 """Try to return a repr of an object
174 174
175 175 always returns a string, at least.
176 176 """
177 177 try:
178 178 return repr(obj)
179 179 except Exception as e:
180 180 return "un-repr-able object (%r)" % e
181 181
182 182
183 183 class FormatterWarning(UserWarning):
184 184 """Warning class for errors in formatters"""
185 185
186 186 @decorator
187 187 def catch_format_error(method, self, *args, **kwargs):
188 188 """show traceback on failed format call"""
189 189 try:
190 190 r = method(self, *args, **kwargs)
191 191 except NotImplementedError:
192 192 # don't warn on NotImplementedErrors
193 193 return None
194 194 except Exception:
195 195 exc_info = sys.exc_info()
196 196 ip = get_ipython()
197 197 if ip is not None:
198 198 ip.showtraceback(exc_info)
199 199 else:
200 200 traceback.print_exception(*exc_info)
201 201 return None
202 202 return self._check_return(r, args[0])
203 203
204 204
205 205 class FormatterABC(with_metaclass(abc.ABCMeta, object)):
206 206 """ Abstract base class for Formatters.
207 207
208 208 A formatter is a callable class that is responsible for computing the
209 209 raw format data for a particular format type (MIME type). For example,
210 210 an HTML formatter would have a format type of `text/html` and would return
211 211 the HTML representation of the object when called.
212 212 """
213 213
214 214 # The format type of the data returned, usually a MIME type.
215 215 format_type = 'text/plain'
216 216
217 217 # Is the formatter enabled...
218 218 enabled = True
219 219
220 220 @abc.abstractmethod
221 221 def __call__(self, obj):
222 222 """Return a JSON'able representation of the object.
223 223
224 224 If the object cannot be formatted by this formatter,
225 225 warn and return None.
226 226 """
227 227 return repr(obj)
228 228
229 229
230 230 def _mod_name_key(typ):
231 231 """Return a (__module__, __name__) tuple for a type.
232 232
233 233 Used as key in Formatter.deferred_printers.
234 234 """
235 235 module = getattr(typ, '__module__', None)
236 236 name = getattr(typ, '__name__', None)
237 237 return (module, name)
238 238
239 239
240 240 def _get_type(obj):
241 241 """Return the type of an instance (old and new-style)"""
242 242 return getattr(obj, '__class__', None) or type(obj)
243 243
244 244
245 245 _raise_key_error = Sentinel('_raise_key_error', __name__,
246 246 """
247 247 Special value to raise a KeyError
248 248
249 249 Raise KeyError in `BaseFormatter.pop` if passed as the default value to `pop`
250 250 """)
251 251
252 252
253 253 class BaseFormatter(Configurable):
254 254 """A base formatter class that is configurable.
255 255
256 256 This formatter should usually be used as the base class of all formatters.
257 257 It is a traited :class:`Configurable` class and includes an extensible
258 258 API for users to determine how their objects are formatted. The following
259 259 logic is used to find a function to format an given object.
260 260
261 261 1. The object is introspected to see if it has a method with the name
262 262 :attr:`print_method`. If is does, that object is passed to that method
263 263 for formatting.
264 264 2. If no print method is found, three internal dictionaries are consulted
265 265 to find print method: :attr:`singleton_printers`, :attr:`type_printers`
266 266 and :attr:`deferred_printers`.
267 267
268 268 Users should use these dictionaries to register functions that will be
269 269 used to compute the format data for their objects (if those objects don't
270 270 have the special print methods). The easiest way of using these
271 271 dictionaries is through the :meth:`for_type` and :meth:`for_type_by_name`
272 272 methods.
273 273
274 274 If no function/callable is found to compute the format data, ``None`` is
275 275 returned and this format type is not used.
276 276 """
277 277
278 278 format_type = Unicode('text/plain')
279 279 _return_type = string_types
280 280
281 281 enabled = Bool(True).tag(config=True)
282 282
283 283 print_method = ObjectName('__repr__')
284 284
285 285 # The singleton printers.
286 286 # Maps the IDs of the builtin singleton objects to the format functions.
287 287 singleton_printers = Dict().tag(config=True)
288 288
289 289 # The type-specific printers.
290 290 # Map type objects to the format functions.
291 291 type_printers = Dict().tag(config=True)
292 292
293 293 # The deferred-import type-specific printers.
294 294 # Map (modulename, classname) pairs to the format functions.
295 295 deferred_printers = Dict().tag(config=True)
296 296
297 297 @catch_format_error
298 298 def __call__(self, obj):
299 299 """Compute the format for an object."""
300 300 if self.enabled:
301 301 # lookup registered printer
302 302 try:
303 303 printer = self.lookup(obj)
304 304 except KeyError:
305 305 pass
306 306 else:
307 307 return printer(obj)
308 308 # Finally look for special method names
309 309 method = get_real_method(obj, self.print_method)
310 310 if method is not None:
311 311 return method()
312 312 return None
313 313 else:
314 314 return None
315 315
316 316 def __contains__(self, typ):
317 317 """map in to lookup_by_type"""
318 318 try:
319 319 self.lookup_by_type(typ)
320 320 except KeyError:
321 321 return False
322 322 else:
323 323 return True
324 324
325 325 def _check_return(self, r, obj):
326 326 """Check that a return value is appropriate
327 327
328 328 Return the value if so, None otherwise, warning if invalid.
329 329 """
330 330 if r is None or isinstance(r, self._return_type) or \
331 331 (isinstance(r, tuple) and r and isinstance(r[0], self._return_type)):
332 332 return r
333 333 else:
334 334 warnings.warn(
335 335 "%s formatter returned invalid type %s (expected %s) for object: %s" % \
336 336 (self.format_type, type(r), self._return_type, _safe_repr(obj)),
337 337 FormatterWarning
338 338 )
339 339
340 340 def lookup(self, obj):
341 341 """Look up the formatter for a given instance.
342 342
343 343 Parameters
344 344 ----------
345 345 obj : object instance
346 346
347 347 Returns
348 348 -------
349 349 f : callable
350 350 The registered formatting callable for the type.
351 351
352 352 Raises
353 353 ------
354 354 KeyError if the type has not been registered.
355 355 """
356 356 # look for singleton first
357 357 obj_id = id(obj)
358 358 if obj_id in self.singleton_printers:
359 359 return self.singleton_printers[obj_id]
360 360 # then lookup by type
361 361 return self.lookup_by_type(_get_type(obj))
362 362
363 363 def lookup_by_type(self, typ):
364 364 """Look up the registered formatter for a type.
365 365
366 366 Parameters
367 367 ----------
368 368 typ : type or '__module__.__name__' string for a type
369 369
370 370 Returns
371 371 -------
372 372 f : callable
373 373 The registered formatting callable for the type.
374 374
375 375 Raises
376 376 ------
377 377 KeyError if the type has not been registered.
378 378 """
379 379 if isinstance(typ, string_types):
380 380 typ_key = tuple(typ.rsplit('.',1))
381 381 if typ_key not in self.deferred_printers:
382 382 # We may have it cached in the type map. We will have to
383 383 # iterate over all of the types to check.
384 384 for cls in self.type_printers:
385 385 if _mod_name_key(cls) == typ_key:
386 386 return self.type_printers[cls]
387 387 else:
388 388 return self.deferred_printers[typ_key]
389 389 else:
390 390 for cls in pretty._get_mro(typ):
391 391 if cls in self.type_printers or self._in_deferred_types(cls):
392 392 return self.type_printers[cls]
393 393
394 394 # If we have reached here, the lookup failed.
395 395 raise KeyError("No registered printer for {0!r}".format(typ))
396 396
397 397 def for_type(self, typ, func=None):
398 398 """Add a format function for a given type.
399 399
400 400 Parameters
401 401 -----------
402 402 typ : type or '__module__.__name__' string for a type
403 403 The class of the object that will be formatted using `func`.
404 404 func : callable
405 405 A callable for computing the format data.
406 406 `func` will be called with the object to be formatted,
407 407 and will return the raw data in this formatter's format.
408 408 Subclasses may use a different call signature for the
409 409 `func` argument.
410 410
411 411 If `func` is None or not specified, there will be no change,
412 412 only returning the current value.
413 413
414 414 Returns
415 415 -------
416 416 oldfunc : callable
417 417 The currently registered callable.
418 418 If you are registering a new formatter,
419 419 this will be the previous value (to enable restoring later).
420 420 """
421 421 # if string given, interpret as 'pkg.module.class_name'
422 422 if isinstance(typ, string_types):
423 423 type_module, type_name = typ.rsplit('.', 1)
424 424 return self.for_type_by_name(type_module, type_name, func)
425 425
426 426 try:
427 427 oldfunc = self.lookup_by_type(typ)
428 428 except KeyError:
429 429 oldfunc = None
430 430
431 431 if func is not None:
432 432 self.type_printers[typ] = func
433 433
434 434 return oldfunc
435 435
436 436 def for_type_by_name(self, type_module, type_name, func=None):
437 437 """Add a format function for a type specified by the full dotted
438 438 module and name of the type, rather than the type of the object.
439 439
440 440 Parameters
441 441 ----------
442 442 type_module : str
443 443 The full dotted name of the module the type is defined in, like
444 444 ``numpy``.
445 445 type_name : str
446 446 The name of the type (the class name), like ``dtype``
447 447 func : callable
448 448 A callable for computing the format data.
449 449 `func` will be called with the object to be formatted,
450 450 and will return the raw data in this formatter's format.
451 451 Subclasses may use a different call signature for the
452 452 `func` argument.
453 453
454 454 If `func` is None or unspecified, there will be no change,
455 455 only returning the current value.
456 456
457 457 Returns
458 458 -------
459 459 oldfunc : callable
460 460 The currently registered callable.
461 461 If you are registering a new formatter,
462 462 this will be the previous value (to enable restoring later).
463 463 """
464 464 key = (type_module, type_name)
465 465
466 466 try:
467 467 oldfunc = self.lookup_by_type("%s.%s" % key)
468 468 except KeyError:
469 469 oldfunc = None
470 470
471 471 if func is not None:
472 472 self.deferred_printers[key] = func
473 473 return oldfunc
474 474
475 475 def pop(self, typ, default=_raise_key_error):
476 476 """Pop a formatter for the given type.
477 477
478 478 Parameters
479 479 ----------
480 480 typ : type or '__module__.__name__' string for a type
481 481 default : object
482 482 value to be returned if no formatter is registered for typ.
483 483
484 484 Returns
485 485 -------
486 486 obj : object
487 487 The last registered object for the type.
488 488
489 489 Raises
490 490 ------
491 491 KeyError if the type is not registered and default is not specified.
492 492 """
493 493
494 494 if isinstance(typ, string_types):
495 495 typ_key = tuple(typ.rsplit('.',1))
496 496 if typ_key not in self.deferred_printers:
497 497 # We may have it cached in the type map. We will have to
498 498 # iterate over all of the types to check.
499 499 for cls in self.type_printers:
500 500 if _mod_name_key(cls) == typ_key:
501 501 old = self.type_printers.pop(cls)
502 502 break
503 503 else:
504 504 old = default
505 505 else:
506 506 old = self.deferred_printers.pop(typ_key)
507 507 else:
508 508 if typ in self.type_printers:
509 509 old = self.type_printers.pop(typ)
510 510 else:
511 511 old = self.deferred_printers.pop(_mod_name_key(typ), default)
512 512 if old is _raise_key_error:
513 513 raise KeyError("No registered value for {0!r}".format(typ))
514 514 return old
515 515
516 516 def _in_deferred_types(self, cls):
517 517 """
518 518 Check if the given class is specified in the deferred type registry.
519 519
520 520 Successful matches will be moved to the regular type registry for future use.
521 521 """
522 522 mod = getattr(cls, '__module__', None)
523 523 name = getattr(cls, '__name__', None)
524 524 key = (mod, name)
525 525 if key in self.deferred_printers:
526 526 # Move the printer over to the regular registry.
527 527 printer = self.deferred_printers.pop(key)
528 528 self.type_printers[cls] = printer
529 529 return True
530 530 return False
531 531
532 532
533 533 class PlainTextFormatter(BaseFormatter):
534 534 """The default pretty-printer.
535 535
536 536 This uses :mod:`IPython.lib.pretty` to compute the format data of
537 537 the object. If the object cannot be pretty printed, :func:`repr` is used.
538 538 See the documentation of :mod:`IPython.lib.pretty` for details on
539 539 how to write pretty printers. Here is a simple example::
540 540
541 541 def dtype_pprinter(obj, p, cycle):
542 542 if cycle:
543 543 return p.text('dtype(...)')
544 544 if hasattr(obj, 'fields'):
545 545 if obj.fields is None:
546 546 p.text(repr(obj))
547 547 else:
548 548 p.begin_group(7, 'dtype([')
549 549 for i, field in enumerate(obj.descr):
550 550 if i > 0:
551 551 p.text(',')
552 552 p.breakable()
553 553 p.pretty(field)
554 554 p.end_group(7, '])')
555 555 """
556 556
557 557 # The format type of data returned.
558 558 format_type = Unicode('text/plain')
559 559
560 560 # This subclass ignores this attribute as it always need to return
561 561 # something.
562 562 enabled = Bool(True).tag(config=False)
563 563
564 564 max_seq_length = Integer(pretty.MAX_SEQ_LENGTH,
565 565 help="""Truncate large collections (lists, dicts, tuples, sets) to this size.
566 566
567 567 Set to 0 to disable truncation.
568 568 """
569 569 ).tag(config=True)
570 570
571 571 # Look for a _repr_pretty_ methods to use for pretty printing.
572 572 print_method = ObjectName('_repr_pretty_')
573 573
574 574 # Whether to pretty-print or not.
575 575 pprint = Bool(True).tag(config=True)
576 576
577 577 # Whether to be verbose or not.
578 578 verbose = Bool(False).tag(config=True)
579 579
580 580 # The maximum width.
581 581 max_width = Integer(79).tag(config=True)
582 582
583 583 # The newline character.
584 584 newline = Unicode('\n').tag(config=True)
585 585
586 586 # format-string for pprinting floats
587 587 float_format = Unicode('%r')
588 588 # setter for float precision, either int or direct format-string
589 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 593 """float_precision changed, set float_format accordingly.
593 594
594 595 float_precision can be set by int or str.
595 596 This will set float_format, after interpreting input.
596 597 If numpy has been imported, numpy print precision will also be set.
597 598
598 599 integer `n` sets format to '%.nf', otherwise, format set directly.
599 600
600 601 An empty string returns to defaults (repr for float, 8 for numpy).
601 602
602 603 This parameter can be set via the '%precision' magic.
603 604 """
604 605
606 new = change['new']
605 607 if '%' in new:
606 608 # got explicit format string
607 609 fmt = new
608 610 try:
609 611 fmt%3.14159
610 612 except Exception:
611 613 raise ValueError("Precision must be int or format string, not %r"%new)
612 614 elif new:
613 615 # otherwise, should be an int
614 616 try:
615 617 i = int(new)
616 618 assert i >= 0
617 619 except ValueError:
618 620 raise ValueError("Precision must be int or format string, not %r"%new)
619 621 except AssertionError:
620 622 raise ValueError("int precision must be non-negative, not %r"%i)
621 623
622 624 fmt = '%%.%if'%i
623 625 if 'numpy' in sys.modules:
624 626 # set numpy precision if it has been imported
625 627 import numpy
626 628 numpy.set_printoptions(precision=i)
627 629 else:
628 630 # default back to repr
629 631 fmt = '%r'
630 632 if 'numpy' in sys.modules:
631 633 import numpy
632 634 # numpy default is 8
633 635 numpy.set_printoptions(precision=8)
634 636 self.float_format = fmt
635 637
636 638 # Use the default pretty printers from IPython.lib.pretty.
637 639 @default('singleton_printers')
638 640 def _singleton_printers_default(self):
639 641 return pretty._singleton_pprinters.copy()
640 642
641 643 @default('type_printers')
642 644 def _type_printers_default(self):
643 645 d = pretty._type_pprinters.copy()
644 646 d[float] = lambda obj,p,cycle: p.text(self.float_format%obj)
645 647 return d
646 648
647 649 @default('deferred_printers')
648 650 def _deferred_printers_default(self):
649 651 return pretty._deferred_type_pprinters.copy()
650 652
651 653 #### FormatterABC interface ####
652 654
653 655 @catch_format_error
654 656 def __call__(self, obj):
655 657 """Compute the pretty representation of the object."""
656 658 if not self.pprint:
657 659 return repr(obj)
658 660 else:
659 661 # handle str and unicode on Python 2
660 662 # io.StringIO only accepts unicode,
661 663 # cStringIO doesn't handle unicode on py2,
662 664 # StringIO allows str, unicode but only ascii str
663 665 stream = pretty.CUnicodeIO()
664 666 printer = pretty.RepresentationPrinter(stream, self.verbose,
665 667 self.max_width, self.newline,
666 668 max_seq_length=self.max_seq_length,
667 669 singleton_pprinters=self.singleton_printers,
668 670 type_pprinters=self.type_printers,
669 671 deferred_pprinters=self.deferred_printers)
670 672 printer.pretty(obj)
671 673 printer.flush()
672 674 return stream.getvalue()
673 675
674 676
675 677 class HTMLFormatter(BaseFormatter):
676 678 """An HTML formatter.
677 679
678 680 To define the callables that compute the HTML representation of your
679 681 objects, define a :meth:`_repr_html_` method or use the :meth:`for_type`
680 682 or :meth:`for_type_by_name` methods to register functions that handle
681 683 this.
682 684
683 685 The return value of this formatter should be a valid HTML snippet that
684 686 could be injected into an existing DOM. It should *not* include the
685 687 ```<html>`` or ```<body>`` tags.
686 688 """
687 689 format_type = Unicode('text/html')
688 690
689 691 print_method = ObjectName('_repr_html_')
690 692
691 693
692 694 class MarkdownFormatter(BaseFormatter):
693 695 """A Markdown formatter.
694 696
695 697 To define the callables that compute the Markdown representation of your
696 698 objects, define a :meth:`_repr_markdown_` method or use the :meth:`for_type`
697 699 or :meth:`for_type_by_name` methods to register functions that handle
698 700 this.
699 701
700 702 The return value of this formatter should be a valid Markdown.
701 703 """
702 704 format_type = Unicode('text/markdown')
703 705
704 706 print_method = ObjectName('_repr_markdown_')
705 707
706 708 class SVGFormatter(BaseFormatter):
707 709 """An SVG formatter.
708 710
709 711 To define the callables that compute the SVG representation of your
710 712 objects, define a :meth:`_repr_svg_` method or use the :meth:`for_type`
711 713 or :meth:`for_type_by_name` methods to register functions that handle
712 714 this.
713 715
714 716 The return value of this formatter should be valid SVG enclosed in
715 717 ```<svg>``` tags, that could be injected into an existing DOM. It should
716 718 *not* include the ```<html>`` or ```<body>`` tags.
717 719 """
718 720 format_type = Unicode('image/svg+xml')
719 721
720 722 print_method = ObjectName('_repr_svg_')
721 723
722 724
723 725 class PNGFormatter(BaseFormatter):
724 726 """A PNG formatter.
725 727
726 728 To define the callables that compute the PNG representation of your
727 729 objects, define a :meth:`_repr_png_` method or use the :meth:`for_type`
728 730 or :meth:`for_type_by_name` methods to register functions that handle
729 731 this.
730 732
731 733 The return value of this formatter should be raw PNG data, *not*
732 734 base64 encoded.
733 735 """
734 736 format_type = Unicode('image/png')
735 737
736 738 print_method = ObjectName('_repr_png_')
737 739
738 740 _return_type = (bytes, unicode_type)
739 741
740 742
741 743 class JPEGFormatter(BaseFormatter):
742 744 """A JPEG formatter.
743 745
744 746 To define the callables that compute the JPEG representation of your
745 747 objects, define a :meth:`_repr_jpeg_` method or use the :meth:`for_type`
746 748 or :meth:`for_type_by_name` methods to register functions that handle
747 749 this.
748 750
749 751 The return value of this formatter should be raw JPEG data, *not*
750 752 base64 encoded.
751 753 """
752 754 format_type = Unicode('image/jpeg')
753 755
754 756 print_method = ObjectName('_repr_jpeg_')
755 757
756 758 _return_type = (bytes, unicode_type)
757 759
758 760
759 761 class LatexFormatter(BaseFormatter):
760 762 """A LaTeX formatter.
761 763
762 764 To define the callables that compute the LaTeX representation of your
763 765 objects, define a :meth:`_repr_latex_` method or use the :meth:`for_type`
764 766 or :meth:`for_type_by_name` methods to register functions that handle
765 767 this.
766 768
767 769 The return value of this formatter should be a valid LaTeX equation,
768 770 enclosed in either ```$```, ```$$``` or another LaTeX equation
769 771 environment.
770 772 """
771 773 format_type = Unicode('text/latex')
772 774
773 775 print_method = ObjectName('_repr_latex_')
774 776
775 777
776 778 class JSONFormatter(BaseFormatter):
777 779 """A JSON string formatter.
778 780
779 781 To define the callables that compute the JSONable representation of
780 782 your objects, define a :meth:`_repr_json_` method or use the :meth:`for_type`
781 783 or :meth:`for_type_by_name` methods to register functions that handle
782 784 this.
783 785
784 786 The return value of this formatter should be a JSONable list or dict.
785 787 JSON scalars (None, number, string) are not allowed, only dict or list containers.
786 788 """
787 789 format_type = Unicode('application/json')
788 790 _return_type = (list, dict)
789 791
790 792 print_method = ObjectName('_repr_json_')
791 793
792 794 def _check_return(self, r, obj):
793 795 """Check that a return value is appropriate
794 796
795 797 Return the value if so, None otherwise, warning if invalid.
796 798 """
797 799 if r is None:
798 800 return
799 801 md = None
800 802 if isinstance(r, tuple):
801 803 # unpack data, metadata tuple for type checking on first element
802 804 r, md = r
803 805
804 806 # handle deprecated JSON-as-string form from IPython < 3
805 807 if isinstance(r, string_types):
806 808 warnings.warn("JSON expects JSONable list/dict containers, not JSON strings",
807 809 FormatterWarning)
808 810 r = json.loads(r)
809 811
810 812 if md is not None:
811 813 # put the tuple back together
812 814 r = (r, md)
813 815 return super(JSONFormatter, self)._check_return(r, obj)
814 816
815 817
816 818 class JavascriptFormatter(BaseFormatter):
817 819 """A Javascript formatter.
818 820
819 821 To define the callables that compute the Javascript representation of
820 822 your objects, define a :meth:`_repr_javascript_` method or use the
821 823 :meth:`for_type` or :meth:`for_type_by_name` methods to register functions
822 824 that handle this.
823 825
824 826 The return value of this formatter should be valid Javascript code and
825 827 should *not* be enclosed in ```<script>``` tags.
826 828 """
827 829 format_type = Unicode('application/javascript')
828 830
829 831 print_method = ObjectName('_repr_javascript_')
830 832
831 833
832 834 class PDFFormatter(BaseFormatter):
833 835 """A PDF formatter.
834 836
835 837 To define the callables that compute the PDF representation of your
836 838 objects, define a :meth:`_repr_pdf_` method or use the :meth:`for_type`
837 839 or :meth:`for_type_by_name` methods to register functions that handle
838 840 this.
839 841
840 842 The return value of this formatter should be raw PDF data, *not*
841 843 base64 encoded.
842 844 """
843 845 format_type = Unicode('application/pdf')
844 846
845 847 print_method = ObjectName('_repr_pdf_')
846 848
847 849 _return_type = (bytes, unicode_type)
848 850
849 851 class IPythonDisplayFormatter(BaseFormatter):
850 852 """A Formatter for objects that know how to display themselves.
851 853
852 854 To define the callables that compute the representation of your
853 855 objects, define a :meth:`_ipython_display_` method or use the :meth:`for_type`
854 856 or :meth:`for_type_by_name` methods to register functions that handle
855 857 this. Unlike mime-type displays, this method should not return anything,
856 858 instead calling any appropriate display methods itself.
857 859
858 860 This display formatter has highest priority.
859 861 If it fires, no other display formatter will be called.
860 862 """
861 863 print_method = ObjectName('_ipython_display_')
862 864 _return_type = (type(None), bool)
863 865
864 866
865 867 @catch_format_error
866 868 def __call__(self, obj):
867 869 """Compute the format for an object."""
868 870 if self.enabled:
869 871 # lookup registered printer
870 872 try:
871 873 printer = self.lookup(obj)
872 874 except KeyError:
873 875 pass
874 876 else:
875 877 printer(obj)
876 878 return True
877 879 # Finally look for special method names
878 880 method = get_real_method(obj, self.print_method)
879 881 if method is not None:
880 882 method()
881 883 return True
882 884
883 885
884 886 FormatterABC.register(BaseFormatter)
885 887 FormatterABC.register(PlainTextFormatter)
886 888 FormatterABC.register(HTMLFormatter)
887 889 FormatterABC.register(MarkdownFormatter)
888 890 FormatterABC.register(SVGFormatter)
889 891 FormatterABC.register(PNGFormatter)
890 892 FormatterABC.register(PDFFormatter)
891 893 FormatterABC.register(JPEGFormatter)
892 894 FormatterABC.register(LatexFormatter)
893 895 FormatterABC.register(JSONFormatter)
894 896 FormatterABC.register(JavascriptFormatter)
895 897 FormatterABC.register(IPythonDisplayFormatter)
896 898
897 899
898 900 def format_display_data(obj, include=None, exclude=None):
899 901 """Return a format data dict for an object.
900 902
901 903 By default all format types will be computed.
902 904
903 905 The following MIME types are currently implemented:
904 906
905 907 * text/plain
906 908 * text/html
907 909 * text/markdown
908 910 * text/latex
909 911 * application/json
910 912 * application/javascript
911 913 * application/pdf
912 914 * image/png
913 915 * image/jpeg
914 916 * image/svg+xml
915 917
916 918 Parameters
917 919 ----------
918 920 obj : object
919 921 The Python object whose format data will be computed.
920 922
921 923 Returns
922 924 -------
923 925 format_dict : dict
924 926 A dictionary of key/value pairs, one or each format that was
925 927 generated for the object. The keys are the format types, which
926 928 will usually be MIME type strings and the values and JSON'able
927 929 data structure containing the raw data for the representation in
928 930 that format.
929 931 include : list or tuple, optional
930 932 A list of format type strings (MIME types) to include in the
931 933 format data dict. If this is set *only* the format types included
932 934 in this list will be computed.
933 935 exclude : list or tuple, optional
934 936 A list of format type string (MIME types) to exclue in the format
935 937 data dict. If this is set all format types will be computed,
936 938 except for those included in this argument.
937 939 """
938 940 from IPython.core.interactiveshell import InteractiveShell
939 941
940 942 return InteractiveShell.instance().display_formatter.format(
941 943 obj,
942 944 include,
943 945 exclude
944 946 )
945 947
@@ -1,3241 +1,3245 b''
1 1 # -*- coding: utf-8 -*-
2 2 """Main IPython class."""
3 3
4 4 #-----------------------------------------------------------------------------
5 5 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de>
6 6 # Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu>
7 7 # Copyright (C) 2008-2011 The IPython Development Team
8 8 #
9 9 # Distributed under the terms of the BSD License. The full license is in
10 10 # the file COPYING, distributed as part of this software.
11 11 #-----------------------------------------------------------------------------
12 12
13 13 from __future__ import absolute_import, print_function
14 14
15 15 import __future__
16 16 import abc
17 17 import ast
18 18 import atexit
19 19 import functools
20 20 import os
21 21 import re
22 22 import runpy
23 23 import sys
24 24 import tempfile
25 25 import traceback
26 26 import types
27 27 import subprocess
28 28 import warnings
29 29 from io import open as io_open
30 30
31 31 from pickleshare import PickleShareDB
32 32
33 33 from traitlets.config.configurable import SingletonConfigurable
34 34 from IPython.core import oinspect
35 35 from IPython.core import magic
36 36 from IPython.core import page
37 37 from IPython.core import prefilter
38 38 from IPython.core import shadowns
39 39 from IPython.core import ultratb
40 40 from IPython.core.alias import Alias, AliasManager
41 41 from IPython.core.autocall import ExitAutocall
42 42 from IPython.core.builtin_trap import BuiltinTrap
43 43 from IPython.core.events import EventManager, available_events
44 44 from IPython.core.compilerop import CachingCompiler, check_linecache_ipython
45 45 from IPython.core.debugger import Pdb
46 46 from IPython.core.display_trap import DisplayTrap
47 47 from IPython.core.displayhook import DisplayHook
48 48 from IPython.core.displaypub import DisplayPublisher
49 49 from IPython.core.error import InputRejected, UsageError
50 50 from IPython.core.extensions import ExtensionManager
51 51 from IPython.core.formatters import DisplayFormatter
52 52 from IPython.core.history import HistoryManager
53 53 from IPython.core.inputsplitter import ESC_MAGIC, ESC_MAGIC2
54 54 from IPython.core.logger import Logger
55 55 from IPython.core.macro import Macro
56 56 from IPython.core.payload import PayloadManager
57 57 from IPython.core.prefilter import PrefilterManager
58 58 from IPython.core.profiledir import ProfileDir
59 59 from IPython.core.usage import default_banner
60 60 from IPython.testing.skipdoctest import skip_doctest_py2, skip_doctest
61 61 from IPython.utils import PyColorize
62 62 from IPython.utils import io
63 63 from IPython.utils import py3compat
64 64 from IPython.utils import openpy
65 65 from IPython.utils.decorators import undoc
66 66 from IPython.utils.io import ask_yes_no
67 67 from IPython.utils.ipstruct import Struct
68 68 from IPython.paths import get_ipython_dir
69 69 from IPython.utils.path import get_home_dir, get_py_filename, ensure_dir_exists
70 70 from IPython.utils.process import system, getoutput
71 71 from IPython.utils.py3compat import (builtin_mod, unicode_type, string_types,
72 72 with_metaclass, iteritems)
73 73 from IPython.utils.strdispatch import StrDispatch
74 74 from IPython.utils.syspathcontext import prepended_to_syspath
75 75 from IPython.utils.text import format_screen, LSString, SList, DollarFormatter
76 76 from IPython.utils.tempdir import TemporaryDirectory
77 77 from traitlets import (
78 78 Integer, Bool, CaselessStrEnum, Enum, List, Dict, Unicode, Instance, Type,
79 79 observe, default,
80 80 )
81 81 from warnings import warn
82 82 from logging import error
83 83 import IPython.core.hooks
84 84
85 85 # NoOpContext is deprecated, but ipykernel imports it from here.
86 86 # See https://github.com/ipython/ipykernel/issues/157
87 87 from IPython.utils.contexts import NoOpContext
88 88
89 89 try:
90 90 import docrepr.sphinxify as sphx
91 91
92 92 def sphinxify(doc):
93 93 with TemporaryDirectory() as dirname:
94 94 return {
95 95 'text/html': sphx.sphinxify(doc, dirname),
96 96 'text/plain': doc
97 97 }
98 98 except ImportError:
99 99 sphinxify = None
100 100
101 101
102 102 class ProvisionalWarning(DeprecationWarning):
103 103 """
104 104 Warning class for unstable features
105 105 """
106 106 pass
107 107
108 108 #-----------------------------------------------------------------------------
109 109 # Globals
110 110 #-----------------------------------------------------------------------------
111 111
112 112 # compiled regexps for autoindent management
113 113 dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass')
114 114
115 115 #-----------------------------------------------------------------------------
116 116 # Utilities
117 117 #-----------------------------------------------------------------------------
118 118
119 119 @undoc
120 120 def softspace(file, newvalue):
121 121 """Copied from code.py, to remove the dependency"""
122 122
123 123 oldvalue = 0
124 124 try:
125 125 oldvalue = file.softspace
126 126 except AttributeError:
127 127 pass
128 128 try:
129 129 file.softspace = newvalue
130 130 except (AttributeError, TypeError):
131 131 # "attribute-less object" or "read-only attributes"
132 132 pass
133 133 return oldvalue
134 134
135 135 @undoc
136 136 def no_op(*a, **kw): pass
137 137
138 138
139 139 class SpaceInInput(Exception): pass
140 140
141 141
142 142 def get_default_colors():
143 143 "DEPRECATED"
144 144 warn('get_default_color is Deprecated, and is `Neutral` on all platforms.',
145 145 DeprecationWarning, stacklevel=2)
146 146 return 'Neutral'
147 147
148 148
149 149 class SeparateUnicode(Unicode):
150 150 r"""A Unicode subclass to validate separate_in, separate_out, etc.
151 151
152 152 This is a Unicode based trait that converts '0'->'' and ``'\\n'->'\n'``.
153 153 """
154 154
155 155 def validate(self, obj, value):
156 156 if value == '0': value = ''
157 157 value = value.replace('\\n','\n')
158 158 return super(SeparateUnicode, self).validate(obj, value)
159 159
160 160
161 161 @undoc
162 162 class DummyMod(object):
163 163 """A dummy module used for IPython's interactive module when
164 164 a namespace must be assigned to the module's __dict__."""
165 165 pass
166 166
167 167
168 168 class ExecutionResult(object):
169 169 """The result of a call to :meth:`InteractiveShell.run_cell`
170 170
171 171 Stores information about what took place.
172 172 """
173 173 execution_count = None
174 174 error_before_exec = None
175 175 error_in_exec = None
176 176 result = None
177 177
178 178 @property
179 179 def success(self):
180 180 return (self.error_before_exec is None) and (self.error_in_exec is None)
181 181
182 182 def raise_error(self):
183 183 """Reraises error if `success` is `False`, otherwise does nothing"""
184 184 if self.error_before_exec is not None:
185 185 raise self.error_before_exec
186 186 if self.error_in_exec is not None:
187 187 raise self.error_in_exec
188 188
189 189 def __repr__(self):
190 190 if sys.version_info > (3,):
191 191 name = self.__class__.__qualname__
192 192 else:
193 193 name = self.__class__.__name__
194 194 return '<%s object at %x, execution_count=%s error_before_exec=%s error_in_exec=%s result=%s>' %\
195 195 (name, id(self), self.execution_count, self.error_before_exec, self.error_in_exec, repr(self.result))
196 196
197 197
198 198 class InteractiveShell(SingletonConfigurable):
199 199 """An enhanced, interactive shell for Python."""
200 200
201 201 _instance = None
202 202
203 203 ast_transformers = List([], help=
204 204 """
205 205 A list of ast.NodeTransformer subclass instances, which will be applied
206 206 to user input before code is run.
207 207 """
208 208 ).tag(config=True)
209 209
210 210 autocall = Enum((0,1,2), default_value=0, help=
211 211 """
212 212 Make IPython automatically call any callable object even if you didn't
213 213 type explicit parentheses. For example, 'str 43' becomes 'str(43)'
214 214 automatically. The value can be '0' to disable the feature, '1' for
215 215 'smart' autocall, where it is not applied if there are no more
216 216 arguments on the line, and '2' for 'full' autocall, where all callable
217 217 objects are automatically called (even if no arguments are present).
218 218 """
219 219 ).tag(config=True)
220 220 # TODO: remove all autoindent logic and put into frontends.
221 221 # We can't do this yet because even runlines uses the autoindent.
222 222 autoindent = Bool(True, help=
223 223 """
224 224 Autoindent IPython code entered interactively.
225 225 """
226 226 ).tag(config=True)
227 227
228 228 automagic = Bool(True, help=
229 229 """
230 230 Enable magic commands to be called without the leading %.
231 231 """
232 232 ).tag(config=True)
233 233
234 234 banner1 = Unicode(default_banner,
235 235 help="""The part of the banner to be printed before the profile"""
236 236 ).tag(config=True)
237 237 banner2 = Unicode('',
238 238 help="""The part of the banner to be printed after the profile"""
239 239 ).tag(config=True)
240 240
241 241 cache_size = Integer(1000, help=
242 242 """
243 243 Set the size of the output cache. The default is 1000, you can
244 244 change it permanently in your config file. Setting it to 0 completely
245 245 disables the caching system, and the minimum value accepted is 20 (if
246 246 you provide a value less than 20, it is reset to 0 and a warning is
247 247 issued). This limit is defined because otherwise you'll spend more
248 248 time re-flushing a too small cache than working
249 249 """
250 250 ).tag(config=True)
251 251 color_info = Bool(True, help=
252 252 """
253 253 Use colors for displaying information about objects. Because this
254 254 information is passed through a pager (like 'less'), and some pagers
255 255 get confused with color codes, this capability can be turned off.
256 256 """
257 257 ).tag(config=True)
258 258 colors = CaselessStrEnum(('Neutral', 'NoColor','LightBG','Linux'),
259 259 default_value='Neutral',
260 260 help="Set the color scheme (NoColor, Neutral, Linux, or LightBG)."
261 261 ).tag(config=True)
262 262 debug = Bool(False).tag(config=True)
263 263 deep_reload = Bool(False, help=
264 264 """
265 265 **Deprecated**
266 266
267 267 Will be removed in IPython 6.0
268 268
269 269 Enable deep (recursive) reloading by default. IPython can use the
270 270 deep_reload module which reloads changes in modules recursively (it
271 271 replaces the reload() function, so you don't need to change anything to
272 272 use it). `deep_reload` forces a full reload of modules whose code may
273 273 have changed, which the default reload() function does not. When
274 274 deep_reload is off, IPython will use the normal reload(), but
275 275 deep_reload will still be available as dreload().
276 276 """
277 277 ).tag(config=True)
278 278 disable_failing_post_execute = Bool(False,
279 279 help="Don't call post-execute functions that have failed in the past."
280 280 ).tag(config=True)
281 281 display_formatter = Instance(DisplayFormatter, allow_none=True)
282 282 displayhook_class = Type(DisplayHook)
283 283 display_pub_class = Type(DisplayPublisher)
284 284
285 285 sphinxify_docstring = Bool(False, help=
286 286 """
287 287 Enables rich html representation of docstrings. (This requires the
288 288 docrepr module).
289 289 """).tag(config=True)
290 290
291 291 @observe("sphinxify_docstring")
292 292 def _sphinxify_docstring_changed(self, change):
293 293 if change['new']:
294 294 warn("`sphinxify_docstring` is provisional since IPython 5.0 and might change in future versions." , ProvisionalWarning)
295 295
296 296 enable_html_pager = Bool(False, help=
297 297 """
298 298 (Provisional API) enables html representation in mime bundles sent
299 299 to pagers.
300 300 """).tag(config=True)
301 301
302 302 @observe("enable_html_pager")
303 303 def _enable_html_pager_changed(self, change):
304 304 if change['new']:
305 305 warn("`enable_html_pager` is provisional since IPython 5.0 and might change in future versions.", ProvisionalWarning)
306 306
307 307 data_pub_class = None
308 308
309 309 exit_now = Bool(False)
310 310 exiter = Instance(ExitAutocall)
311 311 @default('exiter')
312 312 def _exiter_default(self):
313 313 return ExitAutocall(self)
314 314 # Monotonically increasing execution counter
315 315 execution_count = Integer(1)
316 316 filename = Unicode("<ipython console>")
317 317 ipython_dir= Unicode('').tag(config=True) # Set to get_ipython_dir() in __init__
318 318
319 319 # Input splitter, to transform input line by line and detect when a block
320 320 # is ready to be executed.
321 321 input_splitter = Instance('IPython.core.inputsplitter.IPythonInputSplitter',
322 322 (), {'line_input_checker': True})
323 323
324 324 # This InputSplitter instance is used to transform completed cells before
325 325 # running them. It allows cell magics to contain blank lines.
326 326 input_transformer_manager = Instance('IPython.core.inputsplitter.IPythonInputSplitter',
327 327 (), {'line_input_checker': False})
328 328
329 329 logstart = Bool(False, help=
330 330 """
331 331 Start logging to the default log file in overwrite mode.
332 332 Use `logappend` to specify a log file to **append** logs to.
333 333 """
334 334 ).tag(config=True)
335 335 logfile = Unicode('', help=
336 336 """
337 337 The name of the logfile to use.
338 338 """
339 339 ).tag(config=True)
340 340 logappend = Unicode('', help=
341 341 """
342 342 Start logging to the given file in append mode.
343 343 Use `logfile` to specify a log file to **overwrite** logs to.
344 344 """
345 345 ).tag(config=True)
346 346 object_info_string_level = Enum((0,1,2), default_value=0,
347 347 ).tag(config=True)
348 348 pdb = Bool(False, help=
349 349 """
350 350 Automatically call the pdb debugger after every exception.
351 351 """
352 352 ).tag(config=True)
353 353 display_page = Bool(False,
354 354 help="""If True, anything that would be passed to the pager
355 355 will be displayed as regular output instead."""
356 356 ).tag(config=True)
357 357
358 358 # deprecated prompt traits:
359 359
360 360 prompt_in1 = Unicode('In [\\#]: ',
361 361 help="Deprecated since IPython 4.0 and ignored since 5.0, set TerminalInteractiveShell.prompts object directly."
362 362 ).tag(config=True)
363 363 prompt_in2 = Unicode(' .\\D.: ',
364 364 help="Deprecated since IPython 4.0 and ignored since 5.0, set TerminalInteractiveShell.prompts object directly."
365 365 ).tag(config=True)
366 366 prompt_out = Unicode('Out[\\#]: ',
367 367 help="Deprecated since IPython 4.0 and ignored since 5.0, set TerminalInteractiveShell.prompts object directly."
368 368 ).tag(config=True)
369 369 prompts_pad_left = Bool(True,
370 370 help="Deprecated since IPython 4.0 and ignored since 5.0, set TerminalInteractiveShell.prompts object directly."
371 371 ).tag(config=True)
372 372
373 373 @observe('prompt_in1', 'prompt_in2', 'prompt_out', 'prompt_pad_left')
374 374 def _prompt_trait_changed(self, change):
375 375 name = change['name']
376 376 warn("InteractiveShell.{name} is deprecated since IPython 4.0 and ignored since 5.0, set TerminalInteractiveShell.prompts object directly.".format(
377 377 name=name)
378 378 )
379 379 # protect against weird cases where self.config may not exist:
380 380
381 381 show_rewritten_input = Bool(True,
382 382 help="Show rewritten input, e.g. for autocall."
383 383 ).tag(config=True)
384 384
385 385 quiet = Bool(False).tag(config=True)
386 386
387 387 history_length = Integer(10000,
388 388 help='Total length of command history'
389 389 ).tag(config=True)
390 390
391 391 history_load_length = Integer(1000, help=
392 392 """
393 393 The number of saved history entries to be loaded
394 394 into the history buffer at startup.
395 395 """
396 396 ).tag(config=True)
397 397
398 398 ast_node_interactivity = Enum(['all', 'last', 'last_expr', 'none'],
399 399 default_value='last_expr',
400 400 help="""
401 401 'all', 'last', 'last_expr' or 'none', specifying which nodes should be
402 402 run interactively (displaying output from expressions)."""
403 403 ).tag(config=True)
404 404
405 405 # TODO: this part of prompt management should be moved to the frontends.
406 406 # Use custom TraitTypes that convert '0'->'' and '\\n'->'\n'
407 407 separate_in = SeparateUnicode('\n').tag(config=True)
408 408 separate_out = SeparateUnicode('').tag(config=True)
409 409 separate_out2 = SeparateUnicode('').tag(config=True)
410 410 wildcards_case_sensitive = Bool(True).tag(config=True)
411 411 xmode = CaselessStrEnum(('Context','Plain', 'Verbose'),
412 412 default_value='Context').tag(config=True)
413 413
414 414 # Subcomponents of InteractiveShell
415 415 alias_manager = Instance('IPython.core.alias.AliasManager', allow_none=True)
416 416 prefilter_manager = Instance('IPython.core.prefilter.PrefilterManager', allow_none=True)
417 417 builtin_trap = Instance('IPython.core.builtin_trap.BuiltinTrap', allow_none=True)
418 418 display_trap = Instance('IPython.core.display_trap.DisplayTrap', allow_none=True)
419 419 extension_manager = Instance('IPython.core.extensions.ExtensionManager', allow_none=True)
420 420 payload_manager = Instance('IPython.core.payload.PayloadManager', allow_none=True)
421 421 history_manager = Instance('IPython.core.history.HistoryAccessorBase', allow_none=True)
422 422 magics_manager = Instance('IPython.core.magic.MagicsManager', allow_none=True)
423 423
424 424 profile_dir = Instance('IPython.core.application.ProfileDir', allow_none=True)
425 425 @property
426 426 def profile(self):
427 427 if self.profile_dir is not None:
428 428 name = os.path.basename(self.profile_dir.location)
429 429 return name.replace('profile_','')
430 430
431 431
432 432 # Private interface
433 433 _post_execute = Dict()
434 434
435 435 # Tracks any GUI loop loaded for pylab
436 436 pylab_gui_select = None
437 437
438 438 last_execution_succeeded = Bool(True, help='Did last executed command succeeded')
439 439
440 440 def __init__(self, ipython_dir=None, profile_dir=None,
441 441 user_module=None, user_ns=None,
442 442 custom_exceptions=((), None), **kwargs):
443 443
444 444 # This is where traits with a config_key argument are updated
445 445 # from the values on config.
446 446 super(InteractiveShell, self).__init__(**kwargs)
447 447 if 'PromptManager' in self.config:
448 448 warn('As of IPython 5.0 `PromptManager` config will have no effect'
449 449 ' and has been replaced by TerminalInteractiveShell.prompts_class')
450 450 self.configurables = [self]
451 451
452 452 # These are relatively independent and stateless
453 453 self.init_ipython_dir(ipython_dir)
454 454 self.init_profile_dir(profile_dir)
455 455 self.init_instance_attrs()
456 456 self.init_environment()
457 457
458 458 # Check if we're in a virtualenv, and set up sys.path.
459 459 self.init_virtualenv()
460 460
461 461 # Create namespaces (user_ns, user_global_ns, etc.)
462 462 self.init_create_namespaces(user_module, user_ns)
463 463 # This has to be done after init_create_namespaces because it uses
464 464 # something in self.user_ns, but before init_sys_modules, which
465 465 # is the first thing to modify sys.
466 466 # TODO: When we override sys.stdout and sys.stderr before this class
467 467 # is created, we are saving the overridden ones here. Not sure if this
468 468 # is what we want to do.
469 469 self.save_sys_module_state()
470 470 self.init_sys_modules()
471 471
472 472 # While we're trying to have each part of the code directly access what
473 473 # it needs without keeping redundant references to objects, we have too
474 474 # much legacy code that expects ip.db to exist.
475 475 self.db = PickleShareDB(os.path.join(self.profile_dir.location, 'db'))
476 476
477 477 self.init_history()
478 478 self.init_encoding()
479 479 self.init_prefilter()
480 480
481 481 self.init_syntax_highlighting()
482 482 self.init_hooks()
483 483 self.init_events()
484 484 self.init_pushd_popd_magic()
485 485 self.init_user_ns()
486 486 self.init_logger()
487 487 self.init_builtins()
488 488
489 489 # The following was in post_config_initialization
490 490 self.init_inspector()
491 491 if py3compat.PY3:
492 492 self.raw_input_original = input
493 493 else:
494 494 self.raw_input_original = raw_input
495 495 self.init_completer()
496 496 # TODO: init_io() needs to happen before init_traceback handlers
497 497 # because the traceback handlers hardcode the stdout/stderr streams.
498 498 # This logic in in debugger.Pdb and should eventually be changed.
499 499 self.init_io()
500 500 self.init_traceback_handlers(custom_exceptions)
501 501 self.init_prompts()
502 502 self.init_display_formatter()
503 503 self.init_display_pub()
504 504 self.init_data_pub()
505 505 self.init_displayhook()
506 506 self.init_magics()
507 507 self.init_alias()
508 508 self.init_logstart()
509 509 self.init_pdb()
510 510 self.init_extension_manager()
511 511 self.init_payload()
512 512 self.init_deprecation_warnings()
513 513 self.hooks.late_startup_hook()
514 514 self.events.trigger('shell_initialized', self)
515 515 atexit.register(self.atexit_operations)
516 516
517 517 def get_ipython(self):
518 518 """Return the currently running IPython instance."""
519 519 return self
520 520
521 521 #-------------------------------------------------------------------------
522 522 # Trait changed handlers
523 523 #-------------------------------------------------------------------------
524 524 @observe('ipython_dir')
525 525 def _ipython_dir_changed(self, change):
526 526 ensure_dir_exists(change['new'])
527 527
528 528 def set_autoindent(self,value=None):
529 529 """Set the autoindent flag.
530 530
531 531 If called with no arguments, it acts as a toggle."""
532 532 if value is None:
533 533 self.autoindent = not self.autoindent
534 534 else:
535 535 self.autoindent = value
536 536
537 537 #-------------------------------------------------------------------------
538 538 # init_* methods called by __init__
539 539 #-------------------------------------------------------------------------
540 540
541 541 def init_ipython_dir(self, ipython_dir):
542 542 if ipython_dir is not None:
543 543 self.ipython_dir = ipython_dir
544 544 return
545 545
546 546 self.ipython_dir = get_ipython_dir()
547 547
548 548 def init_profile_dir(self, profile_dir):
549 549 if profile_dir is not None:
550 550 self.profile_dir = profile_dir
551 551 return
552 552 self.profile_dir =\
553 553 ProfileDir.create_profile_dir_by_name(self.ipython_dir, 'default')
554 554
555 555 def init_instance_attrs(self):
556 556 self.more = False
557 557
558 558 # command compiler
559 559 self.compile = CachingCompiler()
560 560
561 561 # Make an empty namespace, which extension writers can rely on both
562 562 # existing and NEVER being used by ipython itself. This gives them a
563 563 # convenient location for storing additional information and state
564 564 # their extensions may require, without fear of collisions with other
565 565 # ipython names that may develop later.
566 566 self.meta = Struct()
567 567
568 568 # Temporary files used for various purposes. Deleted at exit.
569 569 self.tempfiles = []
570 570 self.tempdirs = []
571 571
572 572 # keep track of where we started running (mainly for crash post-mortem)
573 573 # This is not being used anywhere currently.
574 574 self.starting_dir = py3compat.getcwd()
575 575
576 576 # Indentation management
577 577 self.indent_current_nsp = 0
578 578
579 579 # Dict to track post-execution functions that have been registered
580 580 self._post_execute = {}
581 581
582 582 def init_environment(self):
583 583 """Any changes we need to make to the user's environment."""
584 584 pass
585 585
586 586 def init_encoding(self):
587 587 # Get system encoding at startup time. Certain terminals (like Emacs
588 588 # under Win32 have it set to None, and we need to have a known valid
589 589 # encoding to use in the raw_input() method
590 590 try:
591 591 self.stdin_encoding = sys.stdin.encoding or 'ascii'
592 592 except AttributeError:
593 593 self.stdin_encoding = 'ascii'
594 594
595 595 def init_syntax_highlighting(self):
596 596 # Python source parser/formatter for syntax highlighting
597 597 pyformat = PyColorize.Parser().format
598 598 self.pycolorize = lambda src: pyformat(src,'str',self.colors)
599 599
600 600 def refresh_style(self):
601 601 # No-op here, used in subclass
602 602 pass
603 603
604 604 def init_pushd_popd_magic(self):
605 605 # for pushd/popd management
606 606 self.home_dir = get_home_dir()
607 607
608 608 self.dir_stack = []
609 609
610 610 def init_logger(self):
611 611 self.logger = Logger(self.home_dir, logfname='ipython_log.py',
612 612 logmode='rotate')
613 613
614 614 def init_logstart(self):
615 615 """Initialize logging in case it was requested at the command line.
616 616 """
617 617 if self.logappend:
618 618 self.magic('logstart %s append' % self.logappend)
619 619 elif self.logfile:
620 620 self.magic('logstart %s' % self.logfile)
621 621 elif self.logstart:
622 622 self.magic('logstart')
623 623
624 624 def init_deprecation_warnings(self):
625 625 """
626 626 register default filter for deprecation warning.
627 627
628 628 This will allow deprecation warning of function used interactively to show
629 629 warning to users, and still hide deprecation warning from libraries import.
630 630 """
631 631 warnings.filterwarnings("default", category=DeprecationWarning, module=self.user_ns.get("__name__"))
632 632
633 633 def init_builtins(self):
634 634 # A single, static flag that we set to True. Its presence indicates
635 635 # that an IPython shell has been created, and we make no attempts at
636 636 # removing on exit or representing the existence of more than one
637 637 # IPython at a time.
638 638 builtin_mod.__dict__['__IPYTHON__'] = True
639 639
640 640 self.builtin_trap = BuiltinTrap(shell=self)
641 641
642 642 def init_inspector(self):
643 643 # Object inspector
644 644 self.inspector = oinspect.Inspector(oinspect.InspectColors,
645 645 PyColorize.ANSICodeColors,
646 646 'NoColor',
647 647 self.object_info_string_level)
648 648
649 649 def init_io(self):
650 650 # This will just use sys.stdout and sys.stderr. If you want to
651 651 # override sys.stdout and sys.stderr themselves, you need to do that
652 652 # *before* instantiating this class, because io holds onto
653 653 # references to the underlying streams.
654 io.stdout = io.IOStream(sys.stdout)
655 io.stderr = io.IOStream(sys.stderr)
654 # io.std* are deprecated, but don't show our own deprecation warnings
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 661 def init_prompts(self):
658 662 # Set system prompts, so that scripts can decide if they are running
659 663 # interactively.
660 664 sys.ps1 = 'In : '
661 665 sys.ps2 = '...: '
662 666 sys.ps3 = 'Out: '
663 667
664 668 def init_display_formatter(self):
665 669 self.display_formatter = DisplayFormatter(parent=self)
666 670 self.configurables.append(self.display_formatter)
667 671
668 672 def init_display_pub(self):
669 673 self.display_pub = self.display_pub_class(parent=self)
670 674 self.configurables.append(self.display_pub)
671 675
672 676 def init_data_pub(self):
673 677 if not self.data_pub_class:
674 678 self.data_pub = None
675 679 return
676 680 self.data_pub = self.data_pub_class(parent=self)
677 681 self.configurables.append(self.data_pub)
678 682
679 683 def init_displayhook(self):
680 684 # Initialize displayhook, set in/out prompts and printing system
681 685 self.displayhook = self.displayhook_class(
682 686 parent=self,
683 687 shell=self,
684 688 cache_size=self.cache_size,
685 689 )
686 690 self.configurables.append(self.displayhook)
687 691 # This is a context manager that installs/revmoes the displayhook at
688 692 # the appropriate time.
689 693 self.display_trap = DisplayTrap(hook=self.displayhook)
690 694
691 695 def init_virtualenv(self):
692 696 """Add a virtualenv to sys.path so the user can import modules from it.
693 697 This isn't perfect: it doesn't use the Python interpreter with which the
694 698 virtualenv was built, and it ignores the --no-site-packages option. A
695 699 warning will appear suggesting the user installs IPython in the
696 700 virtualenv, but for many cases, it probably works well enough.
697 701
698 702 Adapted from code snippets online.
699 703
700 704 http://blog.ufsoft.org/2009/1/29/ipython-and-virtualenv
701 705 """
702 706 if 'VIRTUAL_ENV' not in os.environ:
703 707 # Not in a virtualenv
704 708 return
705 709
706 710 # venv detection:
707 711 # stdlib venv may symlink sys.executable, so we can't use realpath.
708 712 # but others can symlink *to* the venv Python, so we can't just use sys.executable.
709 713 # So we just check every item in the symlink tree (generally <= 3)
710 714 p = os.path.normcase(sys.executable)
711 715 paths = [p]
712 716 while os.path.islink(p):
713 717 p = os.path.normcase(os.path.join(os.path.dirname(p), os.readlink(p)))
714 718 paths.append(p)
715 719 p_venv = os.path.normcase(os.environ['VIRTUAL_ENV'])
716 720 if any(p.startswith(p_venv) for p in paths):
717 721 # Running properly in the virtualenv, don't need to do anything
718 722 return
719 723
720 724 warn("Attempting to work in a virtualenv. If you encounter problems, please "
721 725 "install IPython inside the virtualenv.")
722 726 if sys.platform == "win32":
723 727 virtual_env = os.path.join(os.environ['VIRTUAL_ENV'], 'Lib', 'site-packages')
724 728 else:
725 729 virtual_env = os.path.join(os.environ['VIRTUAL_ENV'], 'lib',
726 730 'python%d.%d' % sys.version_info[:2], 'site-packages')
727 731
728 732 import site
729 733 sys.path.insert(0, virtual_env)
730 734 site.addsitedir(virtual_env)
731 735
732 736 #-------------------------------------------------------------------------
733 737 # Things related to injections into the sys module
734 738 #-------------------------------------------------------------------------
735 739
736 740 def save_sys_module_state(self):
737 741 """Save the state of hooks in the sys module.
738 742
739 743 This has to be called after self.user_module is created.
740 744 """
741 745 self._orig_sys_module_state = {'stdin': sys.stdin,
742 746 'stdout': sys.stdout,
743 747 'stderr': sys.stderr,
744 748 'excepthook': sys.excepthook}
745 749 self._orig_sys_modules_main_name = self.user_module.__name__
746 750 self._orig_sys_modules_main_mod = sys.modules.get(self.user_module.__name__)
747 751
748 752 def restore_sys_module_state(self):
749 753 """Restore the state of the sys module."""
750 754 try:
751 755 for k, v in iteritems(self._orig_sys_module_state):
752 756 setattr(sys, k, v)
753 757 except AttributeError:
754 758 pass
755 759 # Reset what what done in self.init_sys_modules
756 760 if self._orig_sys_modules_main_mod is not None:
757 761 sys.modules[self._orig_sys_modules_main_name] = self._orig_sys_modules_main_mod
758 762
759 763 #-------------------------------------------------------------------------
760 764 # Things related to the banner
761 765 #-------------------------------------------------------------------------
762 766
763 767 @property
764 768 def banner(self):
765 769 banner = self.banner1
766 770 if self.profile and self.profile != 'default':
767 771 banner += '\nIPython profile: %s\n' % self.profile
768 772 if self.banner2:
769 773 banner += '\n' + self.banner2
770 774 return banner
771 775
772 776 def show_banner(self, banner=None):
773 777 if banner is None:
774 778 banner = self.banner
775 779 sys.stdout.write(banner)
776 780
777 781 #-------------------------------------------------------------------------
778 782 # Things related to hooks
779 783 #-------------------------------------------------------------------------
780 784
781 785 def init_hooks(self):
782 786 # hooks holds pointers used for user-side customizations
783 787 self.hooks = Struct()
784 788
785 789 self.strdispatchers = {}
786 790
787 791 # Set all default hooks, defined in the IPython.hooks module.
788 792 hooks = IPython.core.hooks
789 793 for hook_name in hooks.__all__:
790 794 # default hooks have priority 100, i.e. low; user hooks should have
791 795 # 0-100 priority
792 796 self.set_hook(hook_name,getattr(hooks,hook_name), 100, _warn_deprecated=False)
793 797
794 798 if self.display_page:
795 799 self.set_hook('show_in_pager', page.as_hook(page.display_page), 90)
796 800
797 801 def set_hook(self,name,hook, priority=50, str_key=None, re_key=None,
798 802 _warn_deprecated=True):
799 803 """set_hook(name,hook) -> sets an internal IPython hook.
800 804
801 805 IPython exposes some of its internal API as user-modifiable hooks. By
802 806 adding your function to one of these hooks, you can modify IPython's
803 807 behavior to call at runtime your own routines."""
804 808
805 809 # At some point in the future, this should validate the hook before it
806 810 # accepts it. Probably at least check that the hook takes the number
807 811 # of args it's supposed to.
808 812
809 813 f = types.MethodType(hook,self)
810 814
811 815 # check if the hook is for strdispatcher first
812 816 if str_key is not None:
813 817 sdp = self.strdispatchers.get(name, StrDispatch())
814 818 sdp.add_s(str_key, f, priority )
815 819 self.strdispatchers[name] = sdp
816 820 return
817 821 if re_key is not None:
818 822 sdp = self.strdispatchers.get(name, StrDispatch())
819 823 sdp.add_re(re.compile(re_key), f, priority )
820 824 self.strdispatchers[name] = sdp
821 825 return
822 826
823 827 dp = getattr(self.hooks, name, None)
824 828 if name not in IPython.core.hooks.__all__:
825 829 print("Warning! Hook '%s' is not one of %s" % \
826 830 (name, IPython.core.hooks.__all__ ))
827 831
828 832 if _warn_deprecated and (name in IPython.core.hooks.deprecated):
829 833 alternative = IPython.core.hooks.deprecated[name]
830 834 warn("Hook {} is deprecated. Use {} instead.".format(name, alternative))
831 835
832 836 if not dp:
833 837 dp = IPython.core.hooks.CommandChainDispatcher()
834 838
835 839 try:
836 840 dp.add(f,priority)
837 841 except AttributeError:
838 842 # it was not commandchain, plain old func - replace
839 843 dp = f
840 844
841 845 setattr(self.hooks,name, dp)
842 846
843 847 #-------------------------------------------------------------------------
844 848 # Things related to events
845 849 #-------------------------------------------------------------------------
846 850
847 851 def init_events(self):
848 852 self.events = EventManager(self, available_events)
849 853
850 854 self.events.register("pre_execute", self._clear_warning_registry)
851 855
852 856 def register_post_execute(self, func):
853 857 """DEPRECATED: Use ip.events.register('post_run_cell', func)
854 858
855 859 Register a function for calling after code execution.
856 860 """
857 861 warn("ip.register_post_execute is deprecated, use "
858 862 "ip.events.register('post_run_cell', func) instead.")
859 863 self.events.register('post_run_cell', func)
860 864
861 865 def _clear_warning_registry(self):
862 866 # clear the warning registry, so that different code blocks with
863 867 # overlapping line number ranges don't cause spurious suppression of
864 868 # warnings (see gh-6611 for details)
865 869 if "__warningregistry__" in self.user_global_ns:
866 870 del self.user_global_ns["__warningregistry__"]
867 871
868 872 #-------------------------------------------------------------------------
869 873 # Things related to the "main" module
870 874 #-------------------------------------------------------------------------
871 875
872 876 def new_main_mod(self, filename, modname):
873 877 """Return a new 'main' module object for user code execution.
874 878
875 879 ``filename`` should be the path of the script which will be run in the
876 880 module. Requests with the same filename will get the same module, with
877 881 its namespace cleared.
878 882
879 883 ``modname`` should be the module name - normally either '__main__' or
880 884 the basename of the file without the extension.
881 885
882 886 When scripts are executed via %run, we must keep a reference to their
883 887 __main__ module around so that Python doesn't
884 888 clear it, rendering references to module globals useless.
885 889
886 890 This method keeps said reference in a private dict, keyed by the
887 891 absolute path of the script. This way, for multiple executions of the
888 892 same script we only keep one copy of the namespace (the last one),
889 893 thus preventing memory leaks from old references while allowing the
890 894 objects from the last execution to be accessible.
891 895 """
892 896 filename = os.path.abspath(filename)
893 897 try:
894 898 main_mod = self._main_mod_cache[filename]
895 899 except KeyError:
896 900 main_mod = self._main_mod_cache[filename] = types.ModuleType(
897 901 py3compat.cast_bytes_py2(modname),
898 902 doc="Module created for script run in IPython")
899 903 else:
900 904 main_mod.__dict__.clear()
901 905 main_mod.__name__ = modname
902 906
903 907 main_mod.__file__ = filename
904 908 # It seems pydoc (and perhaps others) needs any module instance to
905 909 # implement a __nonzero__ method
906 910 main_mod.__nonzero__ = lambda : True
907 911
908 912 return main_mod
909 913
910 914 def clear_main_mod_cache(self):
911 915 """Clear the cache of main modules.
912 916
913 917 Mainly for use by utilities like %reset.
914 918
915 919 Examples
916 920 --------
917 921
918 922 In [15]: import IPython
919 923
920 924 In [16]: m = _ip.new_main_mod(IPython.__file__, 'IPython')
921 925
922 926 In [17]: len(_ip._main_mod_cache) > 0
923 927 Out[17]: True
924 928
925 929 In [18]: _ip.clear_main_mod_cache()
926 930
927 931 In [19]: len(_ip._main_mod_cache) == 0
928 932 Out[19]: True
929 933 """
930 934 self._main_mod_cache.clear()
931 935
932 936 #-------------------------------------------------------------------------
933 937 # Things related to debugging
934 938 #-------------------------------------------------------------------------
935 939
936 940 def init_pdb(self):
937 941 # Set calling of pdb on exceptions
938 942 # self.call_pdb is a property
939 943 self.call_pdb = self.pdb
940 944
941 945 def _get_call_pdb(self):
942 946 return self._call_pdb
943 947
944 948 def _set_call_pdb(self,val):
945 949
946 950 if val not in (0,1,False,True):
947 951 raise ValueError('new call_pdb value must be boolean')
948 952
949 953 # store value in instance
950 954 self._call_pdb = val
951 955
952 956 # notify the actual exception handlers
953 957 self.InteractiveTB.call_pdb = val
954 958
955 959 call_pdb = property(_get_call_pdb,_set_call_pdb,None,
956 960 'Control auto-activation of pdb at exceptions')
957 961
958 962 def debugger(self,force=False):
959 963 """Call the pdb debugger.
960 964
961 965 Keywords:
962 966
963 967 - force(False): by default, this routine checks the instance call_pdb
964 968 flag and does not actually invoke the debugger if the flag is false.
965 969 The 'force' option forces the debugger to activate even if the flag
966 970 is false.
967 971 """
968 972
969 973 if not (force or self.call_pdb):
970 974 return
971 975
972 976 if not hasattr(sys,'last_traceback'):
973 977 error('No traceback has been produced, nothing to debug.')
974 978 return
975 979
976 980 self.InteractiveTB.debugger(force=True)
977 981
978 982 #-------------------------------------------------------------------------
979 983 # Things related to IPython's various namespaces
980 984 #-------------------------------------------------------------------------
981 985 default_user_namespaces = True
982 986
983 987 def init_create_namespaces(self, user_module=None, user_ns=None):
984 988 # Create the namespace where the user will operate. user_ns is
985 989 # normally the only one used, and it is passed to the exec calls as
986 990 # the locals argument. But we do carry a user_global_ns namespace
987 991 # given as the exec 'globals' argument, This is useful in embedding
988 992 # situations where the ipython shell opens in a context where the
989 993 # distinction between locals and globals is meaningful. For
990 994 # non-embedded contexts, it is just the same object as the user_ns dict.
991 995
992 996 # FIXME. For some strange reason, __builtins__ is showing up at user
993 997 # level as a dict instead of a module. This is a manual fix, but I
994 998 # should really track down where the problem is coming from. Alex
995 999 # Schmolck reported this problem first.
996 1000
997 1001 # A useful post by Alex Martelli on this topic:
998 1002 # Re: inconsistent value from __builtins__
999 1003 # Von: Alex Martelli <aleaxit@yahoo.com>
1000 1004 # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends
1001 1005 # Gruppen: comp.lang.python
1002 1006
1003 1007 # Michael Hohn <hohn@hooknose.lbl.gov> wrote:
1004 1008 # > >>> print type(builtin_check.get_global_binding('__builtins__'))
1005 1009 # > <type 'dict'>
1006 1010 # > >>> print type(__builtins__)
1007 1011 # > <type 'module'>
1008 1012 # > Is this difference in return value intentional?
1009 1013
1010 1014 # Well, it's documented that '__builtins__' can be either a dictionary
1011 1015 # or a module, and it's been that way for a long time. Whether it's
1012 1016 # intentional (or sensible), I don't know. In any case, the idea is
1013 1017 # that if you need to access the built-in namespace directly, you
1014 1018 # should start with "import __builtin__" (note, no 's') which will
1015 1019 # definitely give you a module. Yeah, it's somewhat confusing:-(.
1016 1020
1017 1021 # These routines return a properly built module and dict as needed by
1018 1022 # the rest of the code, and can also be used by extension writers to
1019 1023 # generate properly initialized namespaces.
1020 1024 if (user_ns is not None) or (user_module is not None):
1021 1025 self.default_user_namespaces = False
1022 1026 self.user_module, self.user_ns = self.prepare_user_module(user_module, user_ns)
1023 1027
1024 1028 # A record of hidden variables we have added to the user namespace, so
1025 1029 # we can list later only variables defined in actual interactive use.
1026 1030 self.user_ns_hidden = {}
1027 1031
1028 1032 # Now that FakeModule produces a real module, we've run into a nasty
1029 1033 # problem: after script execution (via %run), the module where the user
1030 1034 # code ran is deleted. Now that this object is a true module (needed
1031 1035 # so doctest and other tools work correctly), the Python module
1032 1036 # teardown mechanism runs over it, and sets to None every variable
1033 1037 # present in that module. Top-level references to objects from the
1034 1038 # script survive, because the user_ns is updated with them. However,
1035 1039 # calling functions defined in the script that use other things from
1036 1040 # the script will fail, because the function's closure had references
1037 1041 # to the original objects, which are now all None. So we must protect
1038 1042 # these modules from deletion by keeping a cache.
1039 1043 #
1040 1044 # To avoid keeping stale modules around (we only need the one from the
1041 1045 # last run), we use a dict keyed with the full path to the script, so
1042 1046 # only the last version of the module is held in the cache. Note,
1043 1047 # however, that we must cache the module *namespace contents* (their
1044 1048 # __dict__). Because if we try to cache the actual modules, old ones
1045 1049 # (uncached) could be destroyed while still holding references (such as
1046 1050 # those held by GUI objects that tend to be long-lived)>
1047 1051 #
1048 1052 # The %reset command will flush this cache. See the cache_main_mod()
1049 1053 # and clear_main_mod_cache() methods for details on use.
1050 1054
1051 1055 # This is the cache used for 'main' namespaces
1052 1056 self._main_mod_cache = {}
1053 1057
1054 1058 # A table holding all the namespaces IPython deals with, so that
1055 1059 # introspection facilities can search easily.
1056 1060 self.ns_table = {'user_global':self.user_module.__dict__,
1057 1061 'user_local':self.user_ns,
1058 1062 'builtin':builtin_mod.__dict__
1059 1063 }
1060 1064
1061 1065 @property
1062 1066 def user_global_ns(self):
1063 1067 return self.user_module.__dict__
1064 1068
1065 1069 def prepare_user_module(self, user_module=None, user_ns=None):
1066 1070 """Prepare the module and namespace in which user code will be run.
1067 1071
1068 1072 When IPython is started normally, both parameters are None: a new module
1069 1073 is created automatically, and its __dict__ used as the namespace.
1070 1074
1071 1075 If only user_module is provided, its __dict__ is used as the namespace.
1072 1076 If only user_ns is provided, a dummy module is created, and user_ns
1073 1077 becomes the global namespace. If both are provided (as they may be
1074 1078 when embedding), user_ns is the local namespace, and user_module
1075 1079 provides the global namespace.
1076 1080
1077 1081 Parameters
1078 1082 ----------
1079 1083 user_module : module, optional
1080 1084 The current user module in which IPython is being run. If None,
1081 1085 a clean module will be created.
1082 1086 user_ns : dict, optional
1083 1087 A namespace in which to run interactive commands.
1084 1088
1085 1089 Returns
1086 1090 -------
1087 1091 A tuple of user_module and user_ns, each properly initialised.
1088 1092 """
1089 1093 if user_module is None and user_ns is not None:
1090 1094 user_ns.setdefault("__name__", "__main__")
1091 1095 user_module = DummyMod()
1092 1096 user_module.__dict__ = user_ns
1093 1097
1094 1098 if user_module is None:
1095 1099 user_module = types.ModuleType("__main__",
1096 1100 doc="Automatically created module for IPython interactive environment")
1097 1101
1098 1102 # We must ensure that __builtin__ (without the final 's') is always
1099 1103 # available and pointing to the __builtin__ *module*. For more details:
1100 1104 # http://mail.python.org/pipermail/python-dev/2001-April/014068.html
1101 1105 user_module.__dict__.setdefault('__builtin__', builtin_mod)
1102 1106 user_module.__dict__.setdefault('__builtins__', builtin_mod)
1103 1107
1104 1108 if user_ns is None:
1105 1109 user_ns = user_module.__dict__
1106 1110
1107 1111 return user_module, user_ns
1108 1112
1109 1113 def init_sys_modules(self):
1110 1114 # We need to insert into sys.modules something that looks like a
1111 1115 # module but which accesses the IPython namespace, for shelve and
1112 1116 # pickle to work interactively. Normally they rely on getting
1113 1117 # everything out of __main__, but for embedding purposes each IPython
1114 1118 # instance has its own private namespace, so we can't go shoving
1115 1119 # everything into __main__.
1116 1120
1117 1121 # note, however, that we should only do this for non-embedded
1118 1122 # ipythons, which really mimic the __main__.__dict__ with their own
1119 1123 # namespace. Embedded instances, on the other hand, should not do
1120 1124 # this because they need to manage the user local/global namespaces
1121 1125 # only, but they live within a 'normal' __main__ (meaning, they
1122 1126 # shouldn't overtake the execution environment of the script they're
1123 1127 # embedded in).
1124 1128
1125 1129 # This is overridden in the InteractiveShellEmbed subclass to a no-op.
1126 1130 main_name = self.user_module.__name__
1127 1131 sys.modules[main_name] = self.user_module
1128 1132
1129 1133 def init_user_ns(self):
1130 1134 """Initialize all user-visible namespaces to their minimum defaults.
1131 1135
1132 1136 Certain history lists are also initialized here, as they effectively
1133 1137 act as user namespaces.
1134 1138
1135 1139 Notes
1136 1140 -----
1137 1141 All data structures here are only filled in, they are NOT reset by this
1138 1142 method. If they were not empty before, data will simply be added to
1139 1143 therm.
1140 1144 """
1141 1145 # This function works in two parts: first we put a few things in
1142 1146 # user_ns, and we sync that contents into user_ns_hidden so that these
1143 1147 # initial variables aren't shown by %who. After the sync, we add the
1144 1148 # rest of what we *do* want the user to see with %who even on a new
1145 1149 # session (probably nothing, so they really only see their own stuff)
1146 1150
1147 1151 # The user dict must *always* have a __builtin__ reference to the
1148 1152 # Python standard __builtin__ namespace, which must be imported.
1149 1153 # This is so that certain operations in prompt evaluation can be
1150 1154 # reliably executed with builtins. Note that we can NOT use
1151 1155 # __builtins__ (note the 's'), because that can either be a dict or a
1152 1156 # module, and can even mutate at runtime, depending on the context
1153 1157 # (Python makes no guarantees on it). In contrast, __builtin__ is
1154 1158 # always a module object, though it must be explicitly imported.
1155 1159
1156 1160 # For more details:
1157 1161 # http://mail.python.org/pipermail/python-dev/2001-April/014068.html
1158 1162 ns = dict()
1159 1163
1160 1164 # make global variables for user access to the histories
1161 1165 ns['_ih'] = self.history_manager.input_hist_parsed
1162 1166 ns['_oh'] = self.history_manager.output_hist
1163 1167 ns['_dh'] = self.history_manager.dir_hist
1164 1168
1165 1169 ns['_sh'] = shadowns
1166 1170
1167 1171 # user aliases to input and output histories. These shouldn't show up
1168 1172 # in %who, as they can have very large reprs.
1169 1173 ns['In'] = self.history_manager.input_hist_parsed
1170 1174 ns['Out'] = self.history_manager.output_hist
1171 1175
1172 1176 # Store myself as the public api!!!
1173 1177 ns['get_ipython'] = self.get_ipython
1174 1178
1175 1179 ns['exit'] = self.exiter
1176 1180 ns['quit'] = self.exiter
1177 1181
1178 1182 # Sync what we've added so far to user_ns_hidden so these aren't seen
1179 1183 # by %who
1180 1184 self.user_ns_hidden.update(ns)
1181 1185
1182 1186 # Anything put into ns now would show up in %who. Think twice before
1183 1187 # putting anything here, as we really want %who to show the user their
1184 1188 # stuff, not our variables.
1185 1189
1186 1190 # Finally, update the real user's namespace
1187 1191 self.user_ns.update(ns)
1188 1192
1189 1193 @property
1190 1194 def all_ns_refs(self):
1191 1195 """Get a list of references to all the namespace dictionaries in which
1192 1196 IPython might store a user-created object.
1193 1197
1194 1198 Note that this does not include the displayhook, which also caches
1195 1199 objects from the output."""
1196 1200 return [self.user_ns, self.user_global_ns, self.user_ns_hidden] + \
1197 1201 [m.__dict__ for m in self._main_mod_cache.values()]
1198 1202
1199 1203 def reset(self, new_session=True):
1200 1204 """Clear all internal namespaces, and attempt to release references to
1201 1205 user objects.
1202 1206
1203 1207 If new_session is True, a new history session will be opened.
1204 1208 """
1205 1209 # Clear histories
1206 1210 self.history_manager.reset(new_session)
1207 1211 # Reset counter used to index all histories
1208 1212 if new_session:
1209 1213 self.execution_count = 1
1210 1214
1211 1215 # Flush cached output items
1212 1216 if self.displayhook.do_full_cache:
1213 1217 self.displayhook.flush()
1214 1218
1215 1219 # The main execution namespaces must be cleared very carefully,
1216 1220 # skipping the deletion of the builtin-related keys, because doing so
1217 1221 # would cause errors in many object's __del__ methods.
1218 1222 if self.user_ns is not self.user_global_ns:
1219 1223 self.user_ns.clear()
1220 1224 ns = self.user_global_ns
1221 1225 drop_keys = set(ns.keys())
1222 1226 drop_keys.discard('__builtin__')
1223 1227 drop_keys.discard('__builtins__')
1224 1228 drop_keys.discard('__name__')
1225 1229 for k in drop_keys:
1226 1230 del ns[k]
1227 1231
1228 1232 self.user_ns_hidden.clear()
1229 1233
1230 1234 # Restore the user namespaces to minimal usability
1231 1235 self.init_user_ns()
1232 1236
1233 1237 # Restore the default and user aliases
1234 1238 self.alias_manager.clear_aliases()
1235 1239 self.alias_manager.init_aliases()
1236 1240
1237 1241 # Flush the private list of module references kept for script
1238 1242 # execution protection
1239 1243 self.clear_main_mod_cache()
1240 1244
1241 1245 def del_var(self, varname, by_name=False):
1242 1246 """Delete a variable from the various namespaces, so that, as
1243 1247 far as possible, we're not keeping any hidden references to it.
1244 1248
1245 1249 Parameters
1246 1250 ----------
1247 1251 varname : str
1248 1252 The name of the variable to delete.
1249 1253 by_name : bool
1250 1254 If True, delete variables with the given name in each
1251 1255 namespace. If False (default), find the variable in the user
1252 1256 namespace, and delete references to it.
1253 1257 """
1254 1258 if varname in ('__builtin__', '__builtins__'):
1255 1259 raise ValueError("Refusing to delete %s" % varname)
1256 1260
1257 1261 ns_refs = self.all_ns_refs
1258 1262
1259 1263 if by_name: # Delete by name
1260 1264 for ns in ns_refs:
1261 1265 try:
1262 1266 del ns[varname]
1263 1267 except KeyError:
1264 1268 pass
1265 1269 else: # Delete by object
1266 1270 try:
1267 1271 obj = self.user_ns[varname]
1268 1272 except KeyError:
1269 1273 raise NameError("name '%s' is not defined" % varname)
1270 1274 # Also check in output history
1271 1275 ns_refs.append(self.history_manager.output_hist)
1272 1276 for ns in ns_refs:
1273 1277 to_delete = [n for n, o in iteritems(ns) if o is obj]
1274 1278 for name in to_delete:
1275 1279 del ns[name]
1276 1280
1277 1281 # displayhook keeps extra references, but not in a dictionary
1278 1282 for name in ('_', '__', '___'):
1279 1283 if getattr(self.displayhook, name) is obj:
1280 1284 setattr(self.displayhook, name, None)
1281 1285
1282 1286 def reset_selective(self, regex=None):
1283 1287 """Clear selective variables from internal namespaces based on a
1284 1288 specified regular expression.
1285 1289
1286 1290 Parameters
1287 1291 ----------
1288 1292 regex : string or compiled pattern, optional
1289 1293 A regular expression pattern that will be used in searching
1290 1294 variable names in the users namespaces.
1291 1295 """
1292 1296 if regex is not None:
1293 1297 try:
1294 1298 m = re.compile(regex)
1295 1299 except TypeError:
1296 1300 raise TypeError('regex must be a string or compiled pattern')
1297 1301 # Search for keys in each namespace that match the given regex
1298 1302 # If a match is found, delete the key/value pair.
1299 1303 for ns in self.all_ns_refs:
1300 1304 for var in ns:
1301 1305 if m.search(var):
1302 1306 del ns[var]
1303 1307
1304 1308 def push(self, variables, interactive=True):
1305 1309 """Inject a group of variables into the IPython user namespace.
1306 1310
1307 1311 Parameters
1308 1312 ----------
1309 1313 variables : dict, str or list/tuple of str
1310 1314 The variables to inject into the user's namespace. If a dict, a
1311 1315 simple update is done. If a str, the string is assumed to have
1312 1316 variable names separated by spaces. A list/tuple of str can also
1313 1317 be used to give the variable names. If just the variable names are
1314 1318 give (list/tuple/str) then the variable values looked up in the
1315 1319 callers frame.
1316 1320 interactive : bool
1317 1321 If True (default), the variables will be listed with the ``who``
1318 1322 magic.
1319 1323 """
1320 1324 vdict = None
1321 1325
1322 1326 # We need a dict of name/value pairs to do namespace updates.
1323 1327 if isinstance(variables, dict):
1324 1328 vdict = variables
1325 1329 elif isinstance(variables, string_types+(list, tuple)):
1326 1330 if isinstance(variables, string_types):
1327 1331 vlist = variables.split()
1328 1332 else:
1329 1333 vlist = variables
1330 1334 vdict = {}
1331 1335 cf = sys._getframe(1)
1332 1336 for name in vlist:
1333 1337 try:
1334 1338 vdict[name] = eval(name, cf.f_globals, cf.f_locals)
1335 1339 except:
1336 1340 print('Could not get variable %s from %s' %
1337 1341 (name,cf.f_code.co_name))
1338 1342 else:
1339 1343 raise ValueError('variables must be a dict/str/list/tuple')
1340 1344
1341 1345 # Propagate variables to user namespace
1342 1346 self.user_ns.update(vdict)
1343 1347
1344 1348 # And configure interactive visibility
1345 1349 user_ns_hidden = self.user_ns_hidden
1346 1350 if interactive:
1347 1351 for name in vdict:
1348 1352 user_ns_hidden.pop(name, None)
1349 1353 else:
1350 1354 user_ns_hidden.update(vdict)
1351 1355
1352 1356 def drop_by_id(self, variables):
1353 1357 """Remove a dict of variables from the user namespace, if they are the
1354 1358 same as the values in the dictionary.
1355 1359
1356 1360 This is intended for use by extensions: variables that they've added can
1357 1361 be taken back out if they are unloaded, without removing any that the
1358 1362 user has overwritten.
1359 1363
1360 1364 Parameters
1361 1365 ----------
1362 1366 variables : dict
1363 1367 A dictionary mapping object names (as strings) to the objects.
1364 1368 """
1365 1369 for name, obj in iteritems(variables):
1366 1370 if name in self.user_ns and self.user_ns[name] is obj:
1367 1371 del self.user_ns[name]
1368 1372 self.user_ns_hidden.pop(name, None)
1369 1373
1370 1374 #-------------------------------------------------------------------------
1371 1375 # Things related to object introspection
1372 1376 #-------------------------------------------------------------------------
1373 1377
1374 1378 def _ofind(self, oname, namespaces=None):
1375 1379 """Find an object in the available namespaces.
1376 1380
1377 1381 self._ofind(oname) -> dict with keys: found,obj,ospace,ismagic
1378 1382
1379 1383 Has special code to detect magic functions.
1380 1384 """
1381 1385 oname = oname.strip()
1382 1386 #print '1- oname: <%r>' % oname # dbg
1383 1387 if not oname.startswith(ESC_MAGIC) and \
1384 1388 not oname.startswith(ESC_MAGIC2) and \
1385 1389 not py3compat.isidentifier(oname, dotted=True):
1386 1390 return dict(found=False)
1387 1391
1388 1392 if namespaces is None:
1389 1393 # Namespaces to search in:
1390 1394 # Put them in a list. The order is important so that we
1391 1395 # find things in the same order that Python finds them.
1392 1396 namespaces = [ ('Interactive', self.user_ns),
1393 1397 ('Interactive (global)', self.user_global_ns),
1394 1398 ('Python builtin', builtin_mod.__dict__),
1395 1399 ]
1396 1400
1397 1401 # initialize results to 'null'
1398 1402 found = False; obj = None; ospace = None;
1399 1403 ismagic = False; isalias = False; parent = None
1400 1404
1401 1405 # We need to special-case 'print', which as of python2.6 registers as a
1402 1406 # function but should only be treated as one if print_function was
1403 1407 # loaded with a future import. In this case, just bail.
1404 1408 if (oname == 'print' and not py3compat.PY3 and not \
1405 1409 (self.compile.compiler_flags & __future__.CO_FUTURE_PRINT_FUNCTION)):
1406 1410 return {'found':found, 'obj':obj, 'namespace':ospace,
1407 1411 'ismagic':ismagic, 'isalias':isalias, 'parent':parent}
1408 1412
1409 1413 # Look for the given name by splitting it in parts. If the head is
1410 1414 # found, then we look for all the remaining parts as members, and only
1411 1415 # declare success if we can find them all.
1412 1416 oname_parts = oname.split('.')
1413 1417 oname_head, oname_rest = oname_parts[0],oname_parts[1:]
1414 1418 for nsname,ns in namespaces:
1415 1419 try:
1416 1420 obj = ns[oname_head]
1417 1421 except KeyError:
1418 1422 continue
1419 1423 else:
1420 1424 #print 'oname_rest:', oname_rest # dbg
1421 1425 for idx, part in enumerate(oname_rest):
1422 1426 try:
1423 1427 parent = obj
1424 1428 # The last part is looked up in a special way to avoid
1425 1429 # descriptor invocation as it may raise or have side
1426 1430 # effects.
1427 1431 if idx == len(oname_rest) - 1:
1428 1432 obj = self._getattr_property(obj, part)
1429 1433 else:
1430 1434 obj = getattr(obj, part)
1431 1435 except:
1432 1436 # Blanket except b/c some badly implemented objects
1433 1437 # allow __getattr__ to raise exceptions other than
1434 1438 # AttributeError, which then crashes IPython.
1435 1439 break
1436 1440 else:
1437 1441 # If we finish the for loop (no break), we got all members
1438 1442 found = True
1439 1443 ospace = nsname
1440 1444 break # namespace loop
1441 1445
1442 1446 # Try to see if it's magic
1443 1447 if not found:
1444 1448 obj = None
1445 1449 if oname.startswith(ESC_MAGIC2):
1446 1450 oname = oname.lstrip(ESC_MAGIC2)
1447 1451 obj = self.find_cell_magic(oname)
1448 1452 elif oname.startswith(ESC_MAGIC):
1449 1453 oname = oname.lstrip(ESC_MAGIC)
1450 1454 obj = self.find_line_magic(oname)
1451 1455 else:
1452 1456 # search without prefix, so run? will find %run?
1453 1457 obj = self.find_line_magic(oname)
1454 1458 if obj is None:
1455 1459 obj = self.find_cell_magic(oname)
1456 1460 if obj is not None:
1457 1461 found = True
1458 1462 ospace = 'IPython internal'
1459 1463 ismagic = True
1460 1464 isalias = isinstance(obj, Alias)
1461 1465
1462 1466 # Last try: special-case some literals like '', [], {}, etc:
1463 1467 if not found and oname_head in ["''",'""','[]','{}','()']:
1464 1468 obj = eval(oname_head)
1465 1469 found = True
1466 1470 ospace = 'Interactive'
1467 1471
1468 1472 return {'found':found, 'obj':obj, 'namespace':ospace,
1469 1473 'ismagic':ismagic, 'isalias':isalias, 'parent':parent}
1470 1474
1471 1475 @staticmethod
1472 1476 def _getattr_property(obj, attrname):
1473 1477 """Property-aware getattr to use in object finding.
1474 1478
1475 1479 If attrname represents a property, return it unevaluated (in case it has
1476 1480 side effects or raises an error.
1477 1481
1478 1482 """
1479 1483 if not isinstance(obj, type):
1480 1484 try:
1481 1485 # `getattr(type(obj), attrname)` is not guaranteed to return
1482 1486 # `obj`, but does so for property:
1483 1487 #
1484 1488 # property.__get__(self, None, cls) -> self
1485 1489 #
1486 1490 # The universal alternative is to traverse the mro manually
1487 1491 # searching for attrname in class dicts.
1488 1492 attr = getattr(type(obj), attrname)
1489 1493 except AttributeError:
1490 1494 pass
1491 1495 else:
1492 1496 # This relies on the fact that data descriptors (with both
1493 1497 # __get__ & __set__ magic methods) take precedence over
1494 1498 # instance-level attributes:
1495 1499 #
1496 1500 # class A(object):
1497 1501 # @property
1498 1502 # def foobar(self): return 123
1499 1503 # a = A()
1500 1504 # a.__dict__['foobar'] = 345
1501 1505 # a.foobar # == 123
1502 1506 #
1503 1507 # So, a property may be returned right away.
1504 1508 if isinstance(attr, property):
1505 1509 return attr
1506 1510
1507 1511 # Nothing helped, fall back.
1508 1512 return getattr(obj, attrname)
1509 1513
1510 1514 def _object_find(self, oname, namespaces=None):
1511 1515 """Find an object and return a struct with info about it."""
1512 1516 return Struct(self._ofind(oname, namespaces))
1513 1517
1514 1518 def _inspect(self, meth, oname, namespaces=None, **kw):
1515 1519 """Generic interface to the inspector system.
1516 1520
1517 1521 This function is meant to be called by pdef, pdoc & friends.
1518 1522 """
1519 1523 info = self._object_find(oname, namespaces)
1520 1524 docformat = sphinxify if self.sphinxify_docstring else None
1521 1525 if info.found:
1522 1526 pmethod = getattr(self.inspector, meth)
1523 1527 # TODO: only apply format_screen to the plain/text repr of the mime
1524 1528 # bundle.
1525 1529 formatter = format_screen if info.ismagic else docformat
1526 1530 if meth == 'pdoc':
1527 1531 pmethod(info.obj, oname, formatter)
1528 1532 elif meth == 'pinfo':
1529 1533 pmethod(info.obj, oname, formatter, info,
1530 1534 enable_html_pager=self.enable_html_pager, **kw)
1531 1535 else:
1532 1536 pmethod(info.obj, oname)
1533 1537 else:
1534 1538 print('Object `%s` not found.' % oname)
1535 1539 return 'not found' # so callers can take other action
1536 1540
1537 1541 def object_inspect(self, oname, detail_level=0):
1538 1542 """Get object info about oname"""
1539 1543 with self.builtin_trap:
1540 1544 info = self._object_find(oname)
1541 1545 if info.found:
1542 1546 return self.inspector.info(info.obj, oname, info=info,
1543 1547 detail_level=detail_level
1544 1548 )
1545 1549 else:
1546 1550 return oinspect.object_info(name=oname, found=False)
1547 1551
1548 1552 def object_inspect_text(self, oname, detail_level=0):
1549 1553 """Get object info as formatted text"""
1550 1554 return self.object_inspect_mime(oname, detail_level)['text/plain']
1551 1555
1552 1556 def object_inspect_mime(self, oname, detail_level=0):
1553 1557 """Get object info as a mimebundle of formatted representations.
1554 1558
1555 1559 A mimebundle is a dictionary, keyed by mime-type.
1556 1560 It must always have the key `'text/plain'`.
1557 1561 """
1558 1562 with self.builtin_trap:
1559 1563 info = self._object_find(oname)
1560 1564 if info.found:
1561 1565 return self.inspector._get_info(info.obj, oname, info=info,
1562 1566 detail_level=detail_level
1563 1567 )
1564 1568 else:
1565 1569 raise KeyError(oname)
1566 1570
1567 1571 #-------------------------------------------------------------------------
1568 1572 # Things related to history management
1569 1573 #-------------------------------------------------------------------------
1570 1574
1571 1575 def init_history(self):
1572 1576 """Sets up the command history, and starts regular autosaves."""
1573 1577 self.history_manager = HistoryManager(shell=self, parent=self)
1574 1578 self.configurables.append(self.history_manager)
1575 1579
1576 1580 #-------------------------------------------------------------------------
1577 1581 # Things related to exception handling and tracebacks (not debugging)
1578 1582 #-------------------------------------------------------------------------
1579 1583
1580 1584 debugger_cls = Pdb
1581 1585
1582 1586 def init_traceback_handlers(self, custom_exceptions):
1583 1587 # Syntax error handler.
1584 1588 self.SyntaxTB = ultratb.SyntaxTB(color_scheme='NoColor')
1585 1589
1586 1590 # The interactive one is initialized with an offset, meaning we always
1587 1591 # want to remove the topmost item in the traceback, which is our own
1588 1592 # internal code. Valid modes: ['Plain','Context','Verbose']
1589 1593 self.InteractiveTB = ultratb.AutoFormattedTB(mode = 'Plain',
1590 1594 color_scheme='NoColor',
1591 1595 tb_offset = 1,
1592 1596 check_cache=check_linecache_ipython,
1593 1597 debugger_cls=self.debugger_cls)
1594 1598
1595 1599 # The instance will store a pointer to the system-wide exception hook,
1596 1600 # so that runtime code (such as magics) can access it. This is because
1597 1601 # during the read-eval loop, it may get temporarily overwritten.
1598 1602 self.sys_excepthook = sys.excepthook
1599 1603
1600 1604 # and add any custom exception handlers the user may have specified
1601 1605 self.set_custom_exc(*custom_exceptions)
1602 1606
1603 1607 # Set the exception mode
1604 1608 self.InteractiveTB.set_mode(mode=self.xmode)
1605 1609
1606 1610 def set_custom_exc(self, exc_tuple, handler):
1607 1611 """set_custom_exc(exc_tuple, handler)
1608 1612
1609 1613 Set a custom exception handler, which will be called if any of the
1610 1614 exceptions in exc_tuple occur in the mainloop (specifically, in the
1611 1615 run_code() method).
1612 1616
1613 1617 Parameters
1614 1618 ----------
1615 1619
1616 1620 exc_tuple : tuple of exception classes
1617 1621 A *tuple* of exception classes, for which to call the defined
1618 1622 handler. It is very important that you use a tuple, and NOT A
1619 1623 LIST here, because of the way Python's except statement works. If
1620 1624 you only want to trap a single exception, use a singleton tuple::
1621 1625
1622 1626 exc_tuple == (MyCustomException,)
1623 1627
1624 1628 handler : callable
1625 1629 handler must have the following signature::
1626 1630
1627 1631 def my_handler(self, etype, value, tb, tb_offset=None):
1628 1632 ...
1629 1633 return structured_traceback
1630 1634
1631 1635 Your handler must return a structured traceback (a list of strings),
1632 1636 or None.
1633 1637
1634 1638 This will be made into an instance method (via types.MethodType)
1635 1639 of IPython itself, and it will be called if any of the exceptions
1636 1640 listed in the exc_tuple are caught. If the handler is None, an
1637 1641 internal basic one is used, which just prints basic info.
1638 1642
1639 1643 To protect IPython from crashes, if your handler ever raises an
1640 1644 exception or returns an invalid result, it will be immediately
1641 1645 disabled.
1642 1646
1643 1647 WARNING: by putting in your own exception handler into IPython's main
1644 1648 execution loop, you run a very good chance of nasty crashes. This
1645 1649 facility should only be used if you really know what you are doing."""
1646 1650
1647 1651 assert type(exc_tuple)==type(()) , \
1648 1652 "The custom exceptions must be given AS A TUPLE."
1649 1653
1650 1654 def dummy_handler(self, etype, value, tb, tb_offset=None):
1651 1655 print('*** Simple custom exception handler ***')
1652 1656 print('Exception type :',etype)
1653 1657 print('Exception value:',value)
1654 1658 print('Traceback :',tb)
1655 1659 #print 'Source code :','\n'.join(self.buffer)
1656 1660
1657 1661 def validate_stb(stb):
1658 1662 """validate structured traceback return type
1659 1663
1660 1664 return type of CustomTB *should* be a list of strings, but allow
1661 1665 single strings or None, which are harmless.
1662 1666
1663 1667 This function will *always* return a list of strings,
1664 1668 and will raise a TypeError if stb is inappropriate.
1665 1669 """
1666 1670 msg = "CustomTB must return list of strings, not %r" % stb
1667 1671 if stb is None:
1668 1672 return []
1669 1673 elif isinstance(stb, string_types):
1670 1674 return [stb]
1671 1675 elif not isinstance(stb, list):
1672 1676 raise TypeError(msg)
1673 1677 # it's a list
1674 1678 for line in stb:
1675 1679 # check every element
1676 1680 if not isinstance(line, string_types):
1677 1681 raise TypeError(msg)
1678 1682 return stb
1679 1683
1680 1684 if handler is None:
1681 1685 wrapped = dummy_handler
1682 1686 else:
1683 1687 def wrapped(self,etype,value,tb,tb_offset=None):
1684 1688 """wrap CustomTB handler, to protect IPython from user code
1685 1689
1686 1690 This makes it harder (but not impossible) for custom exception
1687 1691 handlers to crash IPython.
1688 1692 """
1689 1693 try:
1690 1694 stb = handler(self,etype,value,tb,tb_offset=tb_offset)
1691 1695 return validate_stb(stb)
1692 1696 except:
1693 1697 # clear custom handler immediately
1694 1698 self.set_custom_exc((), None)
1695 1699 print("Custom TB Handler failed, unregistering", file=sys.stderr)
1696 1700 # show the exception in handler first
1697 1701 stb = self.InteractiveTB.structured_traceback(*sys.exc_info())
1698 1702 print(self.InteractiveTB.stb2text(stb))
1699 1703 print("The original exception:")
1700 1704 stb = self.InteractiveTB.structured_traceback(
1701 1705 (etype,value,tb), tb_offset=tb_offset
1702 1706 )
1703 1707 return stb
1704 1708
1705 1709 self.CustomTB = types.MethodType(wrapped,self)
1706 1710 self.custom_exceptions = exc_tuple
1707 1711
1708 1712 def excepthook(self, etype, value, tb):
1709 1713 """One more defense for GUI apps that call sys.excepthook.
1710 1714
1711 1715 GUI frameworks like wxPython trap exceptions and call
1712 1716 sys.excepthook themselves. I guess this is a feature that
1713 1717 enables them to keep running after exceptions that would
1714 1718 otherwise kill their mainloop. This is a bother for IPython
1715 1719 which excepts to catch all of the program exceptions with a try:
1716 1720 except: statement.
1717 1721
1718 1722 Normally, IPython sets sys.excepthook to a CrashHandler instance, so if
1719 1723 any app directly invokes sys.excepthook, it will look to the user like
1720 1724 IPython crashed. In order to work around this, we can disable the
1721 1725 CrashHandler and replace it with this excepthook instead, which prints a
1722 1726 regular traceback using our InteractiveTB. In this fashion, apps which
1723 1727 call sys.excepthook will generate a regular-looking exception from
1724 1728 IPython, and the CrashHandler will only be triggered by real IPython
1725 1729 crashes.
1726 1730
1727 1731 This hook should be used sparingly, only in places which are not likely
1728 1732 to be true IPython errors.
1729 1733 """
1730 1734 self.showtraceback((etype, value, tb), tb_offset=0)
1731 1735
1732 1736 def _get_exc_info(self, exc_tuple=None):
1733 1737 """get exc_info from a given tuple, sys.exc_info() or sys.last_type etc.
1734 1738
1735 1739 Ensures sys.last_type,value,traceback hold the exc_info we found,
1736 1740 from whichever source.
1737 1741
1738 1742 raises ValueError if none of these contain any information
1739 1743 """
1740 1744 if exc_tuple is None:
1741 1745 etype, value, tb = sys.exc_info()
1742 1746 else:
1743 1747 etype, value, tb = exc_tuple
1744 1748
1745 1749 if etype is None:
1746 1750 if hasattr(sys, 'last_type'):
1747 1751 etype, value, tb = sys.last_type, sys.last_value, \
1748 1752 sys.last_traceback
1749 1753
1750 1754 if etype is None:
1751 1755 raise ValueError("No exception to find")
1752 1756
1753 1757 # Now store the exception info in sys.last_type etc.
1754 1758 # WARNING: these variables are somewhat deprecated and not
1755 1759 # necessarily safe to use in a threaded environment, but tools
1756 1760 # like pdb depend on their existence, so let's set them. If we
1757 1761 # find problems in the field, we'll need to revisit their use.
1758 1762 sys.last_type = etype
1759 1763 sys.last_value = value
1760 1764 sys.last_traceback = tb
1761 1765
1762 1766 return etype, value, tb
1763 1767
1764 1768 def show_usage_error(self, exc):
1765 1769 """Show a short message for UsageErrors
1766 1770
1767 1771 These are special exceptions that shouldn't show a traceback.
1768 1772 """
1769 1773 print("UsageError: %s" % exc, file=sys.stderr)
1770 1774
1771 1775 def get_exception_only(self, exc_tuple=None):
1772 1776 """
1773 1777 Return as a string (ending with a newline) the exception that
1774 1778 just occurred, without any traceback.
1775 1779 """
1776 1780 etype, value, tb = self._get_exc_info(exc_tuple)
1777 1781 msg = traceback.format_exception_only(etype, value)
1778 1782 return ''.join(msg)
1779 1783
1780 1784 def showtraceback(self, exc_tuple=None, filename=None, tb_offset=None,
1781 1785 exception_only=False):
1782 1786 """Display the exception that just occurred.
1783 1787
1784 1788 If nothing is known about the exception, this is the method which
1785 1789 should be used throughout the code for presenting user tracebacks,
1786 1790 rather than directly invoking the InteractiveTB object.
1787 1791
1788 1792 A specific showsyntaxerror() also exists, but this method can take
1789 1793 care of calling it if needed, so unless you are explicitly catching a
1790 1794 SyntaxError exception, don't try to analyze the stack manually and
1791 1795 simply call this method."""
1792 1796
1793 1797 try:
1794 1798 try:
1795 1799 etype, value, tb = self._get_exc_info(exc_tuple)
1796 1800 except ValueError:
1797 1801 print('No traceback available to show.', file=sys.stderr)
1798 1802 return
1799 1803
1800 1804 if issubclass(etype, SyntaxError):
1801 1805 # Though this won't be called by syntax errors in the input
1802 1806 # line, there may be SyntaxError cases with imported code.
1803 1807 self.showsyntaxerror(filename)
1804 1808 elif etype is UsageError:
1805 1809 self.show_usage_error(value)
1806 1810 else:
1807 1811 if exception_only:
1808 1812 stb = ['An exception has occurred, use %tb to see '
1809 1813 'the full traceback.\n']
1810 1814 stb.extend(self.InteractiveTB.get_exception_only(etype,
1811 1815 value))
1812 1816 else:
1813 1817 try:
1814 1818 # Exception classes can customise their traceback - we
1815 1819 # use this in IPython.parallel for exceptions occurring
1816 1820 # in the engines. This should return a list of strings.
1817 1821 stb = value._render_traceback_()
1818 1822 except Exception:
1819 1823 stb = self.InteractiveTB.structured_traceback(etype,
1820 1824 value, tb, tb_offset=tb_offset)
1821 1825
1822 1826 self._showtraceback(etype, value, stb)
1823 1827 if self.call_pdb:
1824 1828 # drop into debugger
1825 1829 self.debugger(force=True)
1826 1830 return
1827 1831
1828 1832 # Actually show the traceback
1829 1833 self._showtraceback(etype, value, stb)
1830 1834
1831 1835 except KeyboardInterrupt:
1832 1836 print('\n' + self.get_exception_only(), file=sys.stderr)
1833 1837
1834 1838 def _showtraceback(self, etype, evalue, stb):
1835 1839 """Actually show a traceback.
1836 1840
1837 1841 Subclasses may override this method to put the traceback on a different
1838 1842 place, like a side channel.
1839 1843 """
1840 1844 print(self.InteractiveTB.stb2text(stb))
1841 1845
1842 1846 def showsyntaxerror(self, filename=None):
1843 1847 """Display the syntax error that just occurred.
1844 1848
1845 1849 This doesn't display a stack trace because there isn't one.
1846 1850
1847 1851 If a filename is given, it is stuffed in the exception instead
1848 1852 of what was there before (because Python's parser always uses
1849 1853 "<string>" when reading from a string).
1850 1854 """
1851 1855 etype, value, last_traceback = self._get_exc_info()
1852 1856
1853 1857 if filename and issubclass(etype, SyntaxError):
1854 1858 try:
1855 1859 value.filename = filename
1856 1860 except:
1857 1861 # Not the format we expect; leave it alone
1858 1862 pass
1859 1863
1860 1864 stb = self.SyntaxTB.structured_traceback(etype, value, [])
1861 1865 self._showtraceback(etype, value, stb)
1862 1866
1863 1867 # This is overridden in TerminalInteractiveShell to show a message about
1864 1868 # the %paste magic.
1865 1869 def showindentationerror(self):
1866 1870 """Called by run_cell when there's an IndentationError in code entered
1867 1871 at the prompt.
1868 1872
1869 1873 This is overridden in TerminalInteractiveShell to show a message about
1870 1874 the %paste magic."""
1871 1875 self.showsyntaxerror()
1872 1876
1873 1877 #-------------------------------------------------------------------------
1874 1878 # Things related to readline
1875 1879 #-------------------------------------------------------------------------
1876 1880
1877 1881 def init_readline(self):
1878 1882 """DEPRECATED
1879 1883
1880 1884 Moved to terminal subclass, here only to simplify the init logic."""
1881 1885 # Set a number of methods that depend on readline to be no-op
1882 1886 warnings.warn('`init_readline` is no-op since IPython 5.0 and is Deprecated',
1883 1887 DeprecationWarning, stacklevel=2)
1884 1888 self.set_custom_completer = no_op
1885 1889
1886 1890 @skip_doctest
1887 1891 def set_next_input(self, s, replace=False):
1888 1892 """ Sets the 'default' input string for the next command line.
1889 1893
1890 1894 Example::
1891 1895
1892 1896 In [1]: _ip.set_next_input("Hello Word")
1893 1897 In [2]: Hello Word_ # cursor is here
1894 1898 """
1895 1899 self.rl_next_input = py3compat.cast_bytes_py2(s)
1896 1900
1897 1901 def _indent_current_str(self):
1898 1902 """return the current level of indentation as a string"""
1899 1903 return self.input_splitter.indent_spaces * ' '
1900 1904
1901 1905 #-------------------------------------------------------------------------
1902 1906 # Things related to text completion
1903 1907 #-------------------------------------------------------------------------
1904 1908
1905 1909 def init_completer(self):
1906 1910 """Initialize the completion machinery.
1907 1911
1908 1912 This creates completion machinery that can be used by client code,
1909 1913 either interactively in-process (typically triggered by the readline
1910 1914 library), programmatically (such as in test suites) or out-of-process
1911 1915 (typically over the network by remote frontends).
1912 1916 """
1913 1917 from IPython.core.completer import IPCompleter
1914 1918 from IPython.core.completerlib import (module_completer,
1915 1919 magic_run_completer, cd_completer, reset_completer)
1916 1920
1917 1921 self.Completer = IPCompleter(shell=self,
1918 1922 namespace=self.user_ns,
1919 1923 global_namespace=self.user_global_ns,
1920 1924 use_readline=False,
1921 1925 parent=self,
1922 1926 )
1923 1927 self.configurables.append(self.Completer)
1924 1928
1925 1929 # Add custom completers to the basic ones built into IPCompleter
1926 1930 sdisp = self.strdispatchers.get('complete_command', StrDispatch())
1927 1931 self.strdispatchers['complete_command'] = sdisp
1928 1932 self.Completer.custom_completers = sdisp
1929 1933
1930 1934 self.set_hook('complete_command', module_completer, str_key = 'import')
1931 1935 self.set_hook('complete_command', module_completer, str_key = 'from')
1932 1936 self.set_hook('complete_command', module_completer, str_key = '%aimport')
1933 1937 self.set_hook('complete_command', magic_run_completer, str_key = '%run')
1934 1938 self.set_hook('complete_command', cd_completer, str_key = '%cd')
1935 1939 self.set_hook('complete_command', reset_completer, str_key = '%reset')
1936 1940
1937 1941
1938 1942 @skip_doctest_py2
1939 1943 def complete(self, text, line=None, cursor_pos=None):
1940 1944 """Return the completed text and a list of completions.
1941 1945
1942 1946 Parameters
1943 1947 ----------
1944 1948
1945 1949 text : string
1946 1950 A string of text to be completed on. It can be given as empty and
1947 1951 instead a line/position pair are given. In this case, the
1948 1952 completer itself will split the line like readline does.
1949 1953
1950 1954 line : string, optional
1951 1955 The complete line that text is part of.
1952 1956
1953 1957 cursor_pos : int, optional
1954 1958 The position of the cursor on the input line.
1955 1959
1956 1960 Returns
1957 1961 -------
1958 1962 text : string
1959 1963 The actual text that was completed.
1960 1964
1961 1965 matches : list
1962 1966 A sorted list with all possible completions.
1963 1967
1964 1968 The optional arguments allow the completion to take more context into
1965 1969 account, and are part of the low-level completion API.
1966 1970
1967 1971 This is a wrapper around the completion mechanism, similar to what
1968 1972 readline does at the command line when the TAB key is hit. By
1969 1973 exposing it as a method, it can be used by other non-readline
1970 1974 environments (such as GUIs) for text completion.
1971 1975
1972 1976 Simple usage example:
1973 1977
1974 1978 In [1]: x = 'hello'
1975 1979
1976 1980 In [2]: _ip.complete('x.l')
1977 1981 Out[2]: ('x.l', ['x.ljust', 'x.lower', 'x.lstrip'])
1978 1982 """
1979 1983
1980 1984 # Inject names into __builtin__ so we can complete on the added names.
1981 1985 with self.builtin_trap:
1982 1986 return self.Completer.complete(text, line, cursor_pos)
1983 1987
1984 1988 def set_custom_completer(self, completer, pos=0):
1985 1989 """Adds a new custom completer function.
1986 1990
1987 1991 The position argument (defaults to 0) is the index in the completers
1988 1992 list where you want the completer to be inserted."""
1989 1993
1990 1994 newcomp = types.MethodType(completer,self.Completer)
1991 1995 self.Completer.matchers.insert(pos,newcomp)
1992 1996
1993 1997 def set_completer_frame(self, frame=None):
1994 1998 """Set the frame of the completer."""
1995 1999 if frame:
1996 2000 self.Completer.namespace = frame.f_locals
1997 2001 self.Completer.global_namespace = frame.f_globals
1998 2002 else:
1999 2003 self.Completer.namespace = self.user_ns
2000 2004 self.Completer.global_namespace = self.user_global_ns
2001 2005
2002 2006 #-------------------------------------------------------------------------
2003 2007 # Things related to magics
2004 2008 #-------------------------------------------------------------------------
2005 2009
2006 2010 def init_magics(self):
2007 2011 from IPython.core import magics as m
2008 2012 self.magics_manager = magic.MagicsManager(shell=self,
2009 2013 parent=self,
2010 2014 user_magics=m.UserMagics(self))
2011 2015 self.configurables.append(self.magics_manager)
2012 2016
2013 2017 # Expose as public API from the magics manager
2014 2018 self.register_magics = self.magics_manager.register
2015 2019
2016 2020 self.register_magics(m.AutoMagics, m.BasicMagics, m.CodeMagics,
2017 2021 m.ConfigMagics, m.DisplayMagics, m.ExecutionMagics,
2018 2022 m.ExtensionMagics, m.HistoryMagics, m.LoggingMagics,
2019 2023 m.NamespaceMagics, m.OSMagics, m.PylabMagics, m.ScriptMagics,
2020 2024 )
2021 2025
2022 2026 # Register Magic Aliases
2023 2027 mman = self.magics_manager
2024 2028 # FIXME: magic aliases should be defined by the Magics classes
2025 2029 # or in MagicsManager, not here
2026 2030 mman.register_alias('ed', 'edit')
2027 2031 mman.register_alias('hist', 'history')
2028 2032 mman.register_alias('rep', 'recall')
2029 2033 mman.register_alias('SVG', 'svg', 'cell')
2030 2034 mman.register_alias('HTML', 'html', 'cell')
2031 2035 mman.register_alias('file', 'writefile', 'cell')
2032 2036
2033 2037 # FIXME: Move the color initialization to the DisplayHook, which
2034 2038 # should be split into a prompt manager and displayhook. We probably
2035 2039 # even need a centralize colors management object.
2036 2040 self.magic('colors %s' % self.colors)
2037 2041
2038 2042 # Defined here so that it's included in the documentation
2039 2043 @functools.wraps(magic.MagicsManager.register_function)
2040 2044 def register_magic_function(self, func, magic_kind='line', magic_name=None):
2041 2045 self.magics_manager.register_function(func,
2042 2046 magic_kind=magic_kind, magic_name=magic_name)
2043 2047
2044 2048 def run_line_magic(self, magic_name, line):
2045 2049 """Execute the given line magic.
2046 2050
2047 2051 Parameters
2048 2052 ----------
2049 2053 magic_name : str
2050 2054 Name of the desired magic function, without '%' prefix.
2051 2055
2052 2056 line : str
2053 2057 The rest of the input line as a single string.
2054 2058 """
2055 2059 fn = self.find_line_magic(magic_name)
2056 2060 if fn is None:
2057 2061 cm = self.find_cell_magic(magic_name)
2058 2062 etpl = "Line magic function `%%%s` not found%s."
2059 2063 extra = '' if cm is None else (' (But cell magic `%%%%%s` exists, '
2060 2064 'did you mean that instead?)' % magic_name )
2061 2065 error(etpl % (magic_name, extra))
2062 2066 else:
2063 2067 # Note: this is the distance in the stack to the user's frame.
2064 2068 # This will need to be updated if the internal calling logic gets
2065 2069 # refactored, or else we'll be expanding the wrong variables.
2066 2070 stack_depth = 2
2067 2071 magic_arg_s = self.var_expand(line, stack_depth)
2068 2072 # Put magic args in a list so we can call with f(*a) syntax
2069 2073 args = [magic_arg_s]
2070 2074 kwargs = {}
2071 2075 # Grab local namespace if we need it:
2072 2076 if getattr(fn, "needs_local_scope", False):
2073 2077 kwargs['local_ns'] = sys._getframe(stack_depth).f_locals
2074 2078 with self.builtin_trap:
2075 2079 result = fn(*args,**kwargs)
2076 2080 return result
2077 2081
2078 2082 def run_cell_magic(self, magic_name, line, cell):
2079 2083 """Execute the given cell magic.
2080 2084
2081 2085 Parameters
2082 2086 ----------
2083 2087 magic_name : str
2084 2088 Name of the desired magic function, without '%' prefix.
2085 2089
2086 2090 line : str
2087 2091 The rest of the first input line as a single string.
2088 2092
2089 2093 cell : str
2090 2094 The body of the cell as a (possibly multiline) string.
2091 2095 """
2092 2096 fn = self.find_cell_magic(magic_name)
2093 2097 if fn is None:
2094 2098 lm = self.find_line_magic(magic_name)
2095 2099 etpl = "Cell magic `%%{0}` not found{1}."
2096 2100 extra = '' if lm is None else (' (But line magic `%{0}` exists, '
2097 2101 'did you mean that instead?)'.format(magic_name))
2098 2102 error(etpl.format(magic_name, extra))
2099 2103 elif cell == '':
2100 2104 message = '%%{0} is a cell magic, but the cell body is empty.'.format(magic_name)
2101 2105 if self.find_line_magic(magic_name) is not None:
2102 2106 message += ' Did you mean the line magic %{0} (single %)?'.format(magic_name)
2103 2107 raise UsageError(message)
2104 2108 else:
2105 2109 # Note: this is the distance in the stack to the user's frame.
2106 2110 # This will need to be updated if the internal calling logic gets
2107 2111 # refactored, or else we'll be expanding the wrong variables.
2108 2112 stack_depth = 2
2109 2113 magic_arg_s = self.var_expand(line, stack_depth)
2110 2114 with self.builtin_trap:
2111 2115 result = fn(magic_arg_s, cell)
2112 2116 return result
2113 2117
2114 2118 def find_line_magic(self, magic_name):
2115 2119 """Find and return a line magic by name.
2116 2120
2117 2121 Returns None if the magic isn't found."""
2118 2122 return self.magics_manager.magics['line'].get(magic_name)
2119 2123
2120 2124 def find_cell_magic(self, magic_name):
2121 2125 """Find and return a cell magic by name.
2122 2126
2123 2127 Returns None if the magic isn't found."""
2124 2128 return self.magics_manager.magics['cell'].get(magic_name)
2125 2129
2126 2130 def find_magic(self, magic_name, magic_kind='line'):
2127 2131 """Find and return a magic of the given type by name.
2128 2132
2129 2133 Returns None if the magic isn't found."""
2130 2134 return self.magics_manager.magics[magic_kind].get(magic_name)
2131 2135
2132 2136 def magic(self, arg_s):
2133 2137 """DEPRECATED. Use run_line_magic() instead.
2134 2138
2135 2139 Call a magic function by name.
2136 2140
2137 2141 Input: a string containing the name of the magic function to call and
2138 2142 any additional arguments to be passed to the magic.
2139 2143
2140 2144 magic('name -opt foo bar') is equivalent to typing at the ipython
2141 2145 prompt:
2142 2146
2143 2147 In[1]: %name -opt foo bar
2144 2148
2145 2149 To call a magic without arguments, simply use magic('name').
2146 2150
2147 2151 This provides a proper Python function to call IPython's magics in any
2148 2152 valid Python code you can type at the interpreter, including loops and
2149 2153 compound statements.
2150 2154 """
2151 2155 # TODO: should we issue a loud deprecation warning here?
2152 2156 magic_name, _, magic_arg_s = arg_s.partition(' ')
2153 2157 magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
2154 2158 return self.run_line_magic(magic_name, magic_arg_s)
2155 2159
2156 2160 #-------------------------------------------------------------------------
2157 2161 # Things related to macros
2158 2162 #-------------------------------------------------------------------------
2159 2163
2160 2164 def define_macro(self, name, themacro):
2161 2165 """Define a new macro
2162 2166
2163 2167 Parameters
2164 2168 ----------
2165 2169 name : str
2166 2170 The name of the macro.
2167 2171 themacro : str or Macro
2168 2172 The action to do upon invoking the macro. If a string, a new
2169 2173 Macro object is created by passing the string to it.
2170 2174 """
2171 2175
2172 2176 from IPython.core import macro
2173 2177
2174 2178 if isinstance(themacro, string_types):
2175 2179 themacro = macro.Macro(themacro)
2176 2180 if not isinstance(themacro, macro.Macro):
2177 2181 raise ValueError('A macro must be a string or a Macro instance.')
2178 2182 self.user_ns[name] = themacro
2179 2183
2180 2184 #-------------------------------------------------------------------------
2181 2185 # Things related to the running of system commands
2182 2186 #-------------------------------------------------------------------------
2183 2187
2184 2188 def system_piped(self, cmd):
2185 2189 """Call the given cmd in a subprocess, piping stdout/err
2186 2190
2187 2191 Parameters
2188 2192 ----------
2189 2193 cmd : str
2190 2194 Command to execute (can not end in '&', as background processes are
2191 2195 not supported. Should not be a command that expects input
2192 2196 other than simple text.
2193 2197 """
2194 2198 if cmd.rstrip().endswith('&'):
2195 2199 # this is *far* from a rigorous test
2196 2200 # We do not support backgrounding processes because we either use
2197 2201 # pexpect or pipes to read from. Users can always just call
2198 2202 # os.system() or use ip.system=ip.system_raw
2199 2203 # if they really want a background process.
2200 2204 raise OSError("Background processes not supported.")
2201 2205
2202 2206 # we explicitly do NOT return the subprocess status code, because
2203 2207 # a non-None value would trigger :func:`sys.displayhook` calls.
2204 2208 # Instead, we store the exit_code in user_ns.
2205 2209 self.user_ns['_exit_code'] = system(self.var_expand(cmd, depth=1))
2206 2210
2207 2211 def system_raw(self, cmd):
2208 2212 """Call the given cmd in a subprocess using os.system on Windows or
2209 2213 subprocess.call using the system shell on other platforms.
2210 2214
2211 2215 Parameters
2212 2216 ----------
2213 2217 cmd : str
2214 2218 Command to execute.
2215 2219 """
2216 2220 cmd = self.var_expand(cmd, depth=1)
2217 2221 # protect os.system from UNC paths on Windows, which it can't handle:
2218 2222 if sys.platform == 'win32':
2219 2223 from IPython.utils._process_win32 import AvoidUNCPath
2220 2224 with AvoidUNCPath() as path:
2221 2225 if path is not None:
2222 2226 cmd = '"pushd %s &&"%s' % (path, cmd)
2223 2227 cmd = py3compat.unicode_to_str(cmd)
2224 2228 try:
2225 2229 ec = os.system(cmd)
2226 2230 except KeyboardInterrupt:
2227 2231 print('\n' + self.get_exception_only(), file=sys.stderr)
2228 2232 ec = -2
2229 2233 else:
2230 2234 cmd = py3compat.unicode_to_str(cmd)
2231 2235 # For posix the result of the subprocess.call() below is an exit
2232 2236 # code, which by convention is zero for success, positive for
2233 2237 # program failure. Exit codes above 128 are reserved for signals,
2234 2238 # and the formula for converting a signal to an exit code is usually
2235 2239 # signal_number+128. To more easily differentiate between exit
2236 2240 # codes and signals, ipython uses negative numbers. For instance
2237 2241 # since control-c is signal 2 but exit code 130, ipython's
2238 2242 # _exit_code variable will read -2. Note that some shells like
2239 2243 # csh and fish don't follow sh/bash conventions for exit codes.
2240 2244 executable = os.environ.get('SHELL', None)
2241 2245 try:
2242 2246 # Use env shell instead of default /bin/sh
2243 2247 ec = subprocess.call(cmd, shell=True, executable=executable)
2244 2248 except KeyboardInterrupt:
2245 2249 # intercept control-C; a long traceback is not useful here
2246 2250 print('\n' + self.get_exception_only(), file=sys.stderr)
2247 2251 ec = 130
2248 2252 if ec > 128:
2249 2253 ec = -(ec - 128)
2250 2254
2251 2255 # We explicitly do NOT return the subprocess status code, because
2252 2256 # a non-None value would trigger :func:`sys.displayhook` calls.
2253 2257 # Instead, we store the exit_code in user_ns. Note the semantics
2254 2258 # of _exit_code: for control-c, _exit_code == -signal.SIGNIT,
2255 2259 # but raising SystemExit(_exit_code) will give status 254!
2256 2260 self.user_ns['_exit_code'] = ec
2257 2261
2258 2262 # use piped system by default, because it is better behaved
2259 2263 system = system_piped
2260 2264
2261 2265 def getoutput(self, cmd, split=True, depth=0):
2262 2266 """Get output (possibly including stderr) from a subprocess.
2263 2267
2264 2268 Parameters
2265 2269 ----------
2266 2270 cmd : str
2267 2271 Command to execute (can not end in '&', as background processes are
2268 2272 not supported.
2269 2273 split : bool, optional
2270 2274 If True, split the output into an IPython SList. Otherwise, an
2271 2275 IPython LSString is returned. These are objects similar to normal
2272 2276 lists and strings, with a few convenience attributes for easier
2273 2277 manipulation of line-based output. You can use '?' on them for
2274 2278 details.
2275 2279 depth : int, optional
2276 2280 How many frames above the caller are the local variables which should
2277 2281 be expanded in the command string? The default (0) assumes that the
2278 2282 expansion variables are in the stack frame calling this function.
2279 2283 """
2280 2284 if cmd.rstrip().endswith('&'):
2281 2285 # this is *far* from a rigorous test
2282 2286 raise OSError("Background processes not supported.")
2283 2287 out = getoutput(self.var_expand(cmd, depth=depth+1))
2284 2288 if split:
2285 2289 out = SList(out.splitlines())
2286 2290 else:
2287 2291 out = LSString(out)
2288 2292 return out
2289 2293
2290 2294 #-------------------------------------------------------------------------
2291 2295 # Things related to aliases
2292 2296 #-------------------------------------------------------------------------
2293 2297
2294 2298 def init_alias(self):
2295 2299 self.alias_manager = AliasManager(shell=self, parent=self)
2296 2300 self.configurables.append(self.alias_manager)
2297 2301
2298 2302 #-------------------------------------------------------------------------
2299 2303 # Things related to extensions
2300 2304 #-------------------------------------------------------------------------
2301 2305
2302 2306 def init_extension_manager(self):
2303 2307 self.extension_manager = ExtensionManager(shell=self, parent=self)
2304 2308 self.configurables.append(self.extension_manager)
2305 2309
2306 2310 #-------------------------------------------------------------------------
2307 2311 # Things related to payloads
2308 2312 #-------------------------------------------------------------------------
2309 2313
2310 2314 def init_payload(self):
2311 2315 self.payload_manager = PayloadManager(parent=self)
2312 2316 self.configurables.append(self.payload_manager)
2313 2317
2314 2318 #-------------------------------------------------------------------------
2315 2319 # Things related to the prefilter
2316 2320 #-------------------------------------------------------------------------
2317 2321
2318 2322 def init_prefilter(self):
2319 2323 self.prefilter_manager = PrefilterManager(shell=self, parent=self)
2320 2324 self.configurables.append(self.prefilter_manager)
2321 2325 # Ultimately this will be refactored in the new interpreter code, but
2322 2326 # for now, we should expose the main prefilter method (there's legacy
2323 2327 # code out there that may rely on this).
2324 2328 self.prefilter = self.prefilter_manager.prefilter_lines
2325 2329
2326 2330 def auto_rewrite_input(self, cmd):
2327 2331 """Print to the screen the rewritten form of the user's command.
2328 2332
2329 2333 This shows visual feedback by rewriting input lines that cause
2330 2334 automatic calling to kick in, like::
2331 2335
2332 2336 /f x
2333 2337
2334 2338 into::
2335 2339
2336 2340 ------> f(x)
2337 2341
2338 2342 after the user's input prompt. This helps the user understand that the
2339 2343 input line was transformed automatically by IPython.
2340 2344 """
2341 2345 if not self.show_rewritten_input:
2342 2346 return
2343 2347
2344 2348 # This is overridden in TerminalInteractiveShell to use fancy prompts
2345 2349 print("------> " + cmd)
2346 2350
2347 2351 #-------------------------------------------------------------------------
2348 2352 # Things related to extracting values/expressions from kernel and user_ns
2349 2353 #-------------------------------------------------------------------------
2350 2354
2351 2355 def _user_obj_error(self):
2352 2356 """return simple exception dict
2353 2357
2354 2358 for use in user_expressions
2355 2359 """
2356 2360
2357 2361 etype, evalue, tb = self._get_exc_info()
2358 2362 stb = self.InteractiveTB.get_exception_only(etype, evalue)
2359 2363
2360 2364 exc_info = {
2361 2365 u'status' : 'error',
2362 2366 u'traceback' : stb,
2363 2367 u'ename' : unicode_type(etype.__name__),
2364 2368 u'evalue' : py3compat.safe_unicode(evalue),
2365 2369 }
2366 2370
2367 2371 return exc_info
2368 2372
2369 2373 def _format_user_obj(self, obj):
2370 2374 """format a user object to display dict
2371 2375
2372 2376 for use in user_expressions
2373 2377 """
2374 2378
2375 2379 data, md = self.display_formatter.format(obj)
2376 2380 value = {
2377 2381 'status' : 'ok',
2378 2382 'data' : data,
2379 2383 'metadata' : md,
2380 2384 }
2381 2385 return value
2382 2386
2383 2387 def user_expressions(self, expressions):
2384 2388 """Evaluate a dict of expressions in the user's namespace.
2385 2389
2386 2390 Parameters
2387 2391 ----------
2388 2392 expressions : dict
2389 2393 A dict with string keys and string values. The expression values
2390 2394 should be valid Python expressions, each of which will be evaluated
2391 2395 in the user namespace.
2392 2396
2393 2397 Returns
2394 2398 -------
2395 2399 A dict, keyed like the input expressions dict, with the rich mime-typed
2396 2400 display_data of each value.
2397 2401 """
2398 2402 out = {}
2399 2403 user_ns = self.user_ns
2400 2404 global_ns = self.user_global_ns
2401 2405
2402 2406 for key, expr in iteritems(expressions):
2403 2407 try:
2404 2408 value = self._format_user_obj(eval(expr, global_ns, user_ns))
2405 2409 except:
2406 2410 value = self._user_obj_error()
2407 2411 out[key] = value
2408 2412 return out
2409 2413
2410 2414 #-------------------------------------------------------------------------
2411 2415 # Things related to the running of code
2412 2416 #-------------------------------------------------------------------------
2413 2417
2414 2418 def ex(self, cmd):
2415 2419 """Execute a normal python statement in user namespace."""
2416 2420 with self.builtin_trap:
2417 2421 exec(cmd, self.user_global_ns, self.user_ns)
2418 2422
2419 2423 def ev(self, expr):
2420 2424 """Evaluate python expression expr in user namespace.
2421 2425
2422 2426 Returns the result of evaluation
2423 2427 """
2424 2428 with self.builtin_trap:
2425 2429 return eval(expr, self.user_global_ns, self.user_ns)
2426 2430
2427 2431 def safe_execfile(self, fname, *where, **kw):
2428 2432 """A safe version of the builtin execfile().
2429 2433
2430 2434 This version will never throw an exception, but instead print
2431 2435 helpful error messages to the screen. This only works on pure
2432 2436 Python files with the .py extension.
2433 2437
2434 2438 Parameters
2435 2439 ----------
2436 2440 fname : string
2437 2441 The name of the file to be executed.
2438 2442 where : tuple
2439 2443 One or two namespaces, passed to execfile() as (globals,locals).
2440 2444 If only one is given, it is passed as both.
2441 2445 exit_ignore : bool (False)
2442 2446 If True, then silence SystemExit for non-zero status (it is always
2443 2447 silenced for zero status, as it is so common).
2444 2448 raise_exceptions : bool (False)
2445 2449 If True raise exceptions everywhere. Meant for testing.
2446 2450 shell_futures : bool (False)
2447 2451 If True, the code will share future statements with the interactive
2448 2452 shell. It will both be affected by previous __future__ imports, and
2449 2453 any __future__ imports in the code will affect the shell. If False,
2450 2454 __future__ imports are not shared in either direction.
2451 2455
2452 2456 """
2453 2457 kw.setdefault('exit_ignore', False)
2454 2458 kw.setdefault('raise_exceptions', False)
2455 2459 kw.setdefault('shell_futures', False)
2456 2460
2457 2461 fname = os.path.abspath(os.path.expanduser(fname))
2458 2462
2459 2463 # Make sure we can open the file
2460 2464 try:
2461 2465 with open(fname):
2462 2466 pass
2463 2467 except:
2464 2468 warn('Could not open file <%s> for safe execution.' % fname)
2465 2469 return
2466 2470
2467 2471 # Find things also in current directory. This is needed to mimic the
2468 2472 # behavior of running a script from the system command line, where
2469 2473 # Python inserts the script's directory into sys.path
2470 2474 dname = os.path.dirname(fname)
2471 2475
2472 2476 with prepended_to_syspath(dname):
2473 2477 try:
2474 2478 glob, loc = (where + (None, ))[:2]
2475 2479 py3compat.execfile(
2476 2480 fname, glob, loc,
2477 2481 self.compile if kw['shell_futures'] else None)
2478 2482 except SystemExit as status:
2479 2483 # If the call was made with 0 or None exit status (sys.exit(0)
2480 2484 # or sys.exit() ), don't bother showing a traceback, as both of
2481 2485 # these are considered normal by the OS:
2482 2486 # > python -c'import sys;sys.exit(0)'; echo $?
2483 2487 # 0
2484 2488 # > python -c'import sys;sys.exit()'; echo $?
2485 2489 # 0
2486 2490 # For other exit status, we show the exception unless
2487 2491 # explicitly silenced, but only in short form.
2488 2492 if status.code:
2489 2493 if kw['raise_exceptions']:
2490 2494 raise
2491 2495 if not kw['exit_ignore']:
2492 2496 self.showtraceback(exception_only=True)
2493 2497 except:
2494 2498 if kw['raise_exceptions']:
2495 2499 raise
2496 2500 # tb offset is 2 because we wrap execfile
2497 2501 self.showtraceback(tb_offset=2)
2498 2502
2499 2503 def safe_execfile_ipy(self, fname, shell_futures=False, raise_exceptions=False):
2500 2504 """Like safe_execfile, but for .ipy or .ipynb files with IPython syntax.
2501 2505
2502 2506 Parameters
2503 2507 ----------
2504 2508 fname : str
2505 2509 The name of the file to execute. The filename must have a
2506 2510 .ipy or .ipynb extension.
2507 2511 shell_futures : bool (False)
2508 2512 If True, the code will share future statements with the interactive
2509 2513 shell. It will both be affected by previous __future__ imports, and
2510 2514 any __future__ imports in the code will affect the shell. If False,
2511 2515 __future__ imports are not shared in either direction.
2512 2516 raise_exceptions : bool (False)
2513 2517 If True raise exceptions everywhere. Meant for testing.
2514 2518 """
2515 2519 fname = os.path.abspath(os.path.expanduser(fname))
2516 2520
2517 2521 # Make sure we can open the file
2518 2522 try:
2519 2523 with open(fname):
2520 2524 pass
2521 2525 except:
2522 2526 warn('Could not open file <%s> for safe execution.' % fname)
2523 2527 return
2524 2528
2525 2529 # Find things also in current directory. This is needed to mimic the
2526 2530 # behavior of running a script from the system command line, where
2527 2531 # Python inserts the script's directory into sys.path
2528 2532 dname = os.path.dirname(fname)
2529 2533
2530 2534 def get_cells():
2531 2535 """generator for sequence of code blocks to run"""
2532 2536 if fname.endswith('.ipynb'):
2533 2537 from nbformat import read
2534 2538 with io_open(fname) as f:
2535 2539 nb = read(f, as_version=4)
2536 2540 if not nb.cells:
2537 2541 return
2538 2542 for cell in nb.cells:
2539 2543 if cell.cell_type == 'code':
2540 2544 yield cell.source
2541 2545 else:
2542 2546 with open(fname) as f:
2543 2547 yield f.read()
2544 2548
2545 2549 with prepended_to_syspath(dname):
2546 2550 try:
2547 2551 for cell in get_cells():
2548 2552 result = self.run_cell(cell, silent=True, shell_futures=shell_futures)
2549 2553 if raise_exceptions:
2550 2554 result.raise_error()
2551 2555 elif not result.success:
2552 2556 break
2553 2557 except:
2554 2558 if raise_exceptions:
2555 2559 raise
2556 2560 self.showtraceback()
2557 2561 warn('Unknown failure executing file: <%s>' % fname)
2558 2562
2559 2563 def safe_run_module(self, mod_name, where):
2560 2564 """A safe version of runpy.run_module().
2561 2565
2562 2566 This version will never throw an exception, but instead print
2563 2567 helpful error messages to the screen.
2564 2568
2565 2569 `SystemExit` exceptions with status code 0 or None are ignored.
2566 2570
2567 2571 Parameters
2568 2572 ----------
2569 2573 mod_name : string
2570 2574 The name of the module to be executed.
2571 2575 where : dict
2572 2576 The globals namespace.
2573 2577 """
2574 2578 try:
2575 2579 try:
2576 2580 where.update(
2577 2581 runpy.run_module(str(mod_name), run_name="__main__",
2578 2582 alter_sys=True)
2579 2583 )
2580 2584 except SystemExit as status:
2581 2585 if status.code:
2582 2586 raise
2583 2587 except:
2584 2588 self.showtraceback()
2585 2589 warn('Unknown failure executing module: <%s>' % mod_name)
2586 2590
2587 2591 def run_cell(self, raw_cell, store_history=False, silent=False, shell_futures=True):
2588 2592 """Run a complete IPython cell.
2589 2593
2590 2594 Parameters
2591 2595 ----------
2592 2596 raw_cell : str
2593 2597 The code (including IPython code such as %magic functions) to run.
2594 2598 store_history : bool
2595 2599 If True, the raw and translated cell will be stored in IPython's
2596 2600 history. For user code calling back into IPython's machinery, this
2597 2601 should be set to False.
2598 2602 silent : bool
2599 2603 If True, avoid side-effects, such as implicit displayhooks and
2600 2604 and logging. silent=True forces store_history=False.
2601 2605 shell_futures : bool
2602 2606 If True, the code will share future statements with the interactive
2603 2607 shell. It will both be affected by previous __future__ imports, and
2604 2608 any __future__ imports in the code will affect the shell. If False,
2605 2609 __future__ imports are not shared in either direction.
2606 2610
2607 2611 Returns
2608 2612 -------
2609 2613 result : :class:`ExecutionResult`
2610 2614 """
2611 2615 result = ExecutionResult()
2612 2616
2613 2617 if (not raw_cell) or raw_cell.isspace():
2614 2618 self.last_execution_succeeded = True
2615 2619 return result
2616 2620
2617 2621 if silent:
2618 2622 store_history = False
2619 2623
2620 2624 if store_history:
2621 2625 result.execution_count = self.execution_count
2622 2626
2623 2627 def error_before_exec(value):
2624 2628 result.error_before_exec = value
2625 2629 self.last_execution_succeeded = False
2626 2630 return result
2627 2631
2628 2632 self.events.trigger('pre_execute')
2629 2633 if not silent:
2630 2634 self.events.trigger('pre_run_cell')
2631 2635
2632 2636 # If any of our input transformation (input_transformer_manager or
2633 2637 # prefilter_manager) raises an exception, we store it in this variable
2634 2638 # so that we can display the error after logging the input and storing
2635 2639 # it in the history.
2636 2640 preprocessing_exc_tuple = None
2637 2641 try:
2638 2642 # Static input transformations
2639 2643 cell = self.input_transformer_manager.transform_cell(raw_cell)
2640 2644 except SyntaxError:
2641 2645 preprocessing_exc_tuple = sys.exc_info()
2642 2646 cell = raw_cell # cell has to exist so it can be stored/logged
2643 2647 else:
2644 2648 if len(cell.splitlines()) == 1:
2645 2649 # Dynamic transformations - only applied for single line commands
2646 2650 with self.builtin_trap:
2647 2651 try:
2648 2652 # use prefilter_lines to handle trailing newlines
2649 2653 # restore trailing newline for ast.parse
2650 2654 cell = self.prefilter_manager.prefilter_lines(cell) + '\n'
2651 2655 except Exception:
2652 2656 # don't allow prefilter errors to crash IPython
2653 2657 preprocessing_exc_tuple = sys.exc_info()
2654 2658
2655 2659 # Store raw and processed history
2656 2660 if store_history:
2657 2661 self.history_manager.store_inputs(self.execution_count,
2658 2662 cell, raw_cell)
2659 2663 if not silent:
2660 2664 self.logger.log(cell, raw_cell)
2661 2665
2662 2666 # Display the exception if input processing failed.
2663 2667 if preprocessing_exc_tuple is not None:
2664 2668 self.showtraceback(preprocessing_exc_tuple)
2665 2669 if store_history:
2666 2670 self.execution_count += 1
2667 2671 return error_before_exec(preprocessing_exc_tuple[2])
2668 2672
2669 2673 # Our own compiler remembers the __future__ environment. If we want to
2670 2674 # run code with a separate __future__ environment, use the default
2671 2675 # compiler
2672 2676 compiler = self.compile if shell_futures else CachingCompiler()
2673 2677
2674 2678 with self.builtin_trap:
2675 2679 cell_name = self.compile.cache(cell, self.execution_count)
2676 2680
2677 2681 with self.display_trap:
2678 2682 # Compile to bytecode
2679 2683 try:
2680 2684 code_ast = compiler.ast_parse(cell, filename=cell_name)
2681 2685 except self.custom_exceptions as e:
2682 2686 etype, value, tb = sys.exc_info()
2683 2687 self.CustomTB(etype, value, tb)
2684 2688 return error_before_exec(e)
2685 2689 except IndentationError as e:
2686 2690 self.showindentationerror()
2687 2691 if store_history:
2688 2692 self.execution_count += 1
2689 2693 return error_before_exec(e)
2690 2694 except (OverflowError, SyntaxError, ValueError, TypeError,
2691 2695 MemoryError) as e:
2692 2696 self.showsyntaxerror()
2693 2697 if store_history:
2694 2698 self.execution_count += 1
2695 2699 return error_before_exec(e)
2696 2700
2697 2701 # Apply AST transformations
2698 2702 try:
2699 2703 code_ast = self.transform_ast(code_ast)
2700 2704 except InputRejected as e:
2701 2705 self.showtraceback()
2702 2706 if store_history:
2703 2707 self.execution_count += 1
2704 2708 return error_before_exec(e)
2705 2709
2706 2710 # Give the displayhook a reference to our ExecutionResult so it
2707 2711 # can fill in the output value.
2708 2712 self.displayhook.exec_result = result
2709 2713
2710 2714 # Execute the user code
2711 2715 interactivity = "none" if silent else self.ast_node_interactivity
2712 2716 has_raised = self.run_ast_nodes(code_ast.body, cell_name,
2713 2717 interactivity=interactivity, compiler=compiler, result=result)
2714 2718
2715 2719 self.last_execution_succeeded = not has_raised
2716 2720
2717 2721 # Reset this so later displayed values do not modify the
2718 2722 # ExecutionResult
2719 2723 self.displayhook.exec_result = None
2720 2724
2721 2725 self.events.trigger('post_execute')
2722 2726 if not silent:
2723 2727 self.events.trigger('post_run_cell')
2724 2728
2725 2729 if store_history:
2726 2730 # Write output to the database. Does nothing unless
2727 2731 # history output logging is enabled.
2728 2732 self.history_manager.store_output(self.execution_count)
2729 2733 # Each cell is a *single* input, regardless of how many lines it has
2730 2734 self.execution_count += 1
2731 2735
2732 2736 return result
2733 2737
2734 2738 def transform_ast(self, node):
2735 2739 """Apply the AST transformations from self.ast_transformers
2736 2740
2737 2741 Parameters
2738 2742 ----------
2739 2743 node : ast.Node
2740 2744 The root node to be transformed. Typically called with the ast.Module
2741 2745 produced by parsing user input.
2742 2746
2743 2747 Returns
2744 2748 -------
2745 2749 An ast.Node corresponding to the node it was called with. Note that it
2746 2750 may also modify the passed object, so don't rely on references to the
2747 2751 original AST.
2748 2752 """
2749 2753 for transformer in self.ast_transformers:
2750 2754 try:
2751 2755 node = transformer.visit(node)
2752 2756 except InputRejected:
2753 2757 # User-supplied AST transformers can reject an input by raising
2754 2758 # an InputRejected. Short-circuit in this case so that we
2755 2759 # don't unregister the transform.
2756 2760 raise
2757 2761 except Exception:
2758 2762 warn("AST transformer %r threw an error. It will be unregistered." % transformer)
2759 2763 self.ast_transformers.remove(transformer)
2760 2764
2761 2765 if self.ast_transformers:
2762 2766 ast.fix_missing_locations(node)
2763 2767 return node
2764 2768
2765 2769
2766 2770 def run_ast_nodes(self, nodelist, cell_name, interactivity='last_expr',
2767 2771 compiler=compile, result=None):
2768 2772 """Run a sequence of AST nodes. The execution mode depends on the
2769 2773 interactivity parameter.
2770 2774
2771 2775 Parameters
2772 2776 ----------
2773 2777 nodelist : list
2774 2778 A sequence of AST nodes to run.
2775 2779 cell_name : str
2776 2780 Will be passed to the compiler as the filename of the cell. Typically
2777 2781 the value returned by ip.compile.cache(cell).
2778 2782 interactivity : str
2779 2783 'all', 'last', 'last_expr' or 'none', specifying which nodes should be
2780 2784 run interactively (displaying output from expressions). 'last_expr'
2781 2785 will run the last node interactively only if it is an expression (i.e.
2782 2786 expressions in loops or other blocks are not displayed. Other values
2783 2787 for this parameter will raise a ValueError.
2784 2788 compiler : callable
2785 2789 A function with the same interface as the built-in compile(), to turn
2786 2790 the AST nodes into code objects. Default is the built-in compile().
2787 2791 result : ExecutionResult, optional
2788 2792 An object to store exceptions that occur during execution.
2789 2793
2790 2794 Returns
2791 2795 -------
2792 2796 True if an exception occurred while running code, False if it finished
2793 2797 running.
2794 2798 """
2795 2799 if not nodelist:
2796 2800 return
2797 2801
2798 2802 if interactivity == 'last_expr':
2799 2803 if isinstance(nodelist[-1], ast.Expr):
2800 2804 interactivity = "last"
2801 2805 else:
2802 2806 interactivity = "none"
2803 2807
2804 2808 if interactivity == 'none':
2805 2809 to_run_exec, to_run_interactive = nodelist, []
2806 2810 elif interactivity == 'last':
2807 2811 to_run_exec, to_run_interactive = nodelist[:-1], nodelist[-1:]
2808 2812 elif interactivity == 'all':
2809 2813 to_run_exec, to_run_interactive = [], nodelist
2810 2814 else:
2811 2815 raise ValueError("Interactivity was %r" % interactivity)
2812 2816
2813 2817 try:
2814 2818 for i, node in enumerate(to_run_exec):
2815 2819 mod = ast.Module([node])
2816 2820 code = compiler(mod, cell_name, "exec")
2817 2821 if self.run_code(code, result):
2818 2822 return True
2819 2823
2820 2824 for i, node in enumerate(to_run_interactive):
2821 2825 mod = ast.Interactive([node])
2822 2826 code = compiler(mod, cell_name, "single")
2823 2827 if self.run_code(code, result):
2824 2828 return True
2825 2829
2826 2830 # Flush softspace
2827 2831 if softspace(sys.stdout, 0):
2828 2832 print()
2829 2833
2830 2834 except:
2831 2835 # It's possible to have exceptions raised here, typically by
2832 2836 # compilation of odd code (such as a naked 'return' outside a
2833 2837 # function) that did parse but isn't valid. Typically the exception
2834 2838 # is a SyntaxError, but it's safest just to catch anything and show
2835 2839 # the user a traceback.
2836 2840
2837 2841 # We do only one try/except outside the loop to minimize the impact
2838 2842 # on runtime, and also because if any node in the node list is
2839 2843 # broken, we should stop execution completely.
2840 2844 if result:
2841 2845 result.error_before_exec = sys.exc_info()[1]
2842 2846 self.showtraceback()
2843 2847 return True
2844 2848
2845 2849 return False
2846 2850
2847 2851 def run_code(self, code_obj, result=None):
2848 2852 """Execute a code object.
2849 2853
2850 2854 When an exception occurs, self.showtraceback() is called to display a
2851 2855 traceback.
2852 2856
2853 2857 Parameters
2854 2858 ----------
2855 2859 code_obj : code object
2856 2860 A compiled code object, to be executed
2857 2861 result : ExecutionResult, optional
2858 2862 An object to store exceptions that occur during execution.
2859 2863
2860 2864 Returns
2861 2865 -------
2862 2866 False : successful execution.
2863 2867 True : an error occurred.
2864 2868 """
2865 2869 # Set our own excepthook in case the user code tries to call it
2866 2870 # directly, so that the IPython crash handler doesn't get triggered
2867 2871 old_excepthook, sys.excepthook = sys.excepthook, self.excepthook
2868 2872
2869 2873 # we save the original sys.excepthook in the instance, in case config
2870 2874 # code (such as magics) needs access to it.
2871 2875 self.sys_excepthook = old_excepthook
2872 2876 outflag = 1 # happens in more places, so it's easier as default
2873 2877 try:
2874 2878 try:
2875 2879 self.hooks.pre_run_code_hook()
2876 2880 #rprint('Running code', repr(code_obj)) # dbg
2877 2881 exec(code_obj, self.user_global_ns, self.user_ns)
2878 2882 finally:
2879 2883 # Reset our crash handler in place
2880 2884 sys.excepthook = old_excepthook
2881 2885 except SystemExit as e:
2882 2886 if result is not None:
2883 2887 result.error_in_exec = e
2884 2888 self.showtraceback(exception_only=True)
2885 2889 warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1)
2886 2890 except self.custom_exceptions:
2887 2891 etype, value, tb = sys.exc_info()
2888 2892 if result is not None:
2889 2893 result.error_in_exec = value
2890 2894 self.CustomTB(etype, value, tb)
2891 2895 except:
2892 2896 if result is not None:
2893 2897 result.error_in_exec = sys.exc_info()[1]
2894 2898 self.showtraceback()
2895 2899 else:
2896 2900 outflag = 0
2897 2901 return outflag
2898 2902
2899 2903 # For backwards compatibility
2900 2904 runcode = run_code
2901 2905
2902 2906 #-------------------------------------------------------------------------
2903 2907 # Things related to GUI support and pylab
2904 2908 #-------------------------------------------------------------------------
2905 2909
2906 2910 def enable_gui(self, gui=None):
2907 2911 raise NotImplementedError('Implement enable_gui in a subclass')
2908 2912
2909 2913 def enable_matplotlib(self, gui=None):
2910 2914 """Enable interactive matplotlib and inline figure support.
2911 2915
2912 2916 This takes the following steps:
2913 2917
2914 2918 1. select the appropriate eventloop and matplotlib backend
2915 2919 2. set up matplotlib for interactive use with that backend
2916 2920 3. configure formatters for inline figure display
2917 2921 4. enable the selected gui eventloop
2918 2922
2919 2923 Parameters
2920 2924 ----------
2921 2925 gui : optional, string
2922 2926 If given, dictates the choice of matplotlib GUI backend to use
2923 2927 (should be one of IPython's supported backends, 'qt', 'osx', 'tk',
2924 2928 'gtk', 'wx' or 'inline'), otherwise we use the default chosen by
2925 2929 matplotlib (as dictated by the matplotlib build-time options plus the
2926 2930 user's matplotlibrc configuration file). Note that not all backends
2927 2931 make sense in all contexts, for example a terminal ipython can't
2928 2932 display figures inline.
2929 2933 """
2930 2934 from IPython.core import pylabtools as pt
2931 2935 gui, backend = pt.find_gui_and_backend(gui, self.pylab_gui_select)
2932 2936
2933 2937 if gui != 'inline':
2934 2938 # If we have our first gui selection, store it
2935 2939 if self.pylab_gui_select is None:
2936 2940 self.pylab_gui_select = gui
2937 2941 # Otherwise if they are different
2938 2942 elif gui != self.pylab_gui_select:
2939 2943 print ('Warning: Cannot change to a different GUI toolkit: %s.'
2940 2944 ' Using %s instead.' % (gui, self.pylab_gui_select))
2941 2945 gui, backend = pt.find_gui_and_backend(self.pylab_gui_select)
2942 2946
2943 2947 pt.activate_matplotlib(backend)
2944 2948 pt.configure_inline_support(self, backend)
2945 2949
2946 2950 # Now we must activate the gui pylab wants to use, and fix %run to take
2947 2951 # plot updates into account
2948 2952 self.enable_gui(gui)
2949 2953 self.magics_manager.registry['ExecutionMagics'].default_runner = \
2950 2954 pt.mpl_runner(self.safe_execfile)
2951 2955
2952 2956 return gui, backend
2953 2957
2954 2958 def enable_pylab(self, gui=None, import_all=True, welcome_message=False):
2955 2959 """Activate pylab support at runtime.
2956 2960
2957 2961 This turns on support for matplotlib, preloads into the interactive
2958 2962 namespace all of numpy and pylab, and configures IPython to correctly
2959 2963 interact with the GUI event loop. The GUI backend to be used can be
2960 2964 optionally selected with the optional ``gui`` argument.
2961 2965
2962 2966 This method only adds preloading the namespace to InteractiveShell.enable_matplotlib.
2963 2967
2964 2968 Parameters
2965 2969 ----------
2966 2970 gui : optional, string
2967 2971 If given, dictates the choice of matplotlib GUI backend to use
2968 2972 (should be one of IPython's supported backends, 'qt', 'osx', 'tk',
2969 2973 'gtk', 'wx' or 'inline'), otherwise we use the default chosen by
2970 2974 matplotlib (as dictated by the matplotlib build-time options plus the
2971 2975 user's matplotlibrc configuration file). Note that not all backends
2972 2976 make sense in all contexts, for example a terminal ipython can't
2973 2977 display figures inline.
2974 2978 import_all : optional, bool, default: True
2975 2979 Whether to do `from numpy import *` and `from pylab import *`
2976 2980 in addition to module imports.
2977 2981 welcome_message : deprecated
2978 2982 This argument is ignored, no welcome message will be displayed.
2979 2983 """
2980 2984 from IPython.core.pylabtools import import_pylab
2981 2985
2982 2986 gui, backend = self.enable_matplotlib(gui)
2983 2987
2984 2988 # We want to prevent the loading of pylab to pollute the user's
2985 2989 # namespace as shown by the %who* magics, so we execute the activation
2986 2990 # code in an empty namespace, and we update *both* user_ns and
2987 2991 # user_ns_hidden with this information.
2988 2992 ns = {}
2989 2993 import_pylab(ns, import_all)
2990 2994 # warn about clobbered names
2991 2995 ignored = {"__builtins__"}
2992 2996 both = set(ns).intersection(self.user_ns).difference(ignored)
2993 2997 clobbered = [ name for name in both if self.user_ns[name] is not ns[name] ]
2994 2998 self.user_ns.update(ns)
2995 2999 self.user_ns_hidden.update(ns)
2996 3000 return gui, backend, clobbered
2997 3001
2998 3002 #-------------------------------------------------------------------------
2999 3003 # Utilities
3000 3004 #-------------------------------------------------------------------------
3001 3005
3002 3006 def var_expand(self, cmd, depth=0, formatter=DollarFormatter()):
3003 3007 """Expand python variables in a string.
3004 3008
3005 3009 The depth argument indicates how many frames above the caller should
3006 3010 be walked to look for the local namespace where to expand variables.
3007 3011
3008 3012 The global namespace for expansion is always the user's interactive
3009 3013 namespace.
3010 3014 """
3011 3015 ns = self.user_ns.copy()
3012 3016 try:
3013 3017 frame = sys._getframe(depth+1)
3014 3018 except ValueError:
3015 3019 # This is thrown if there aren't that many frames on the stack,
3016 3020 # e.g. if a script called run_line_magic() directly.
3017 3021 pass
3018 3022 else:
3019 3023 ns.update(frame.f_locals)
3020 3024
3021 3025 try:
3022 3026 # We have to use .vformat() here, because 'self' is a valid and common
3023 3027 # name, and expanding **ns for .format() would make it collide with
3024 3028 # the 'self' argument of the method.
3025 3029 cmd = formatter.vformat(cmd, args=[], kwargs=ns)
3026 3030 except Exception:
3027 3031 # if formatter couldn't format, just let it go untransformed
3028 3032 pass
3029 3033 return cmd
3030 3034
3031 3035 def mktempfile(self, data=None, prefix='ipython_edit_'):
3032 3036 """Make a new tempfile and return its filename.
3033 3037
3034 3038 This makes a call to tempfile.mkstemp (created in a tempfile.mkdtemp),
3035 3039 but it registers the created filename internally so ipython cleans it up
3036 3040 at exit time.
3037 3041
3038 3042 Optional inputs:
3039 3043
3040 3044 - data(None): if data is given, it gets written out to the temp file
3041 3045 immediately, and the file is closed again."""
3042 3046
3043 3047 dirname = tempfile.mkdtemp(prefix=prefix)
3044 3048 self.tempdirs.append(dirname)
3045 3049
3046 3050 handle, filename = tempfile.mkstemp('.py', prefix, dir=dirname)
3047 3051 os.close(handle) # On Windows, there can only be one open handle on a file
3048 3052 self.tempfiles.append(filename)
3049 3053
3050 3054 if data:
3051 3055 tmp_file = open(filename,'w')
3052 3056 tmp_file.write(data)
3053 3057 tmp_file.close()
3054 3058 return filename
3055 3059
3056 3060 @undoc
3057 3061 def write(self,data):
3058 3062 """DEPRECATED: Write a string to the default output"""
3059 3063 warn('InteractiveShell.write() is deprecated, use sys.stdout instead',
3060 3064 DeprecationWarning, stacklevel=2)
3061 3065 sys.stdout.write(data)
3062 3066
3063 3067 @undoc
3064 3068 def write_err(self,data):
3065 3069 """DEPRECATED: Write a string to the default error output"""
3066 3070 warn('InteractiveShell.write_err() is deprecated, use sys.stderr instead',
3067 3071 DeprecationWarning, stacklevel=2)
3068 3072 sys.stderr.write(data)
3069 3073
3070 3074 def ask_yes_no(self, prompt, default=None, interrupt=None):
3071 3075 if self.quiet:
3072 3076 return True
3073 3077 return ask_yes_no(prompt,default,interrupt)
3074 3078
3075 3079 def show_usage(self):
3076 3080 """Show a usage message"""
3077 3081 page.page(IPython.core.usage.interactive_usage)
3078 3082
3079 3083 def extract_input_lines(self, range_str, raw=False):
3080 3084 """Return as a string a set of input history slices.
3081 3085
3082 3086 Parameters
3083 3087 ----------
3084 3088 range_str : string
3085 3089 The set of slices is given as a string, like "~5/6-~4/2 4:8 9",
3086 3090 since this function is for use by magic functions which get their
3087 3091 arguments as strings. The number before the / is the session
3088 3092 number: ~n goes n back from the current session.
3089 3093
3090 3094 raw : bool, optional
3091 3095 By default, the processed input is used. If this is true, the raw
3092 3096 input history is used instead.
3093 3097
3094 3098 Notes
3095 3099 -----
3096 3100
3097 3101 Slices can be described with two notations:
3098 3102
3099 3103 * ``N:M`` -> standard python form, means including items N...(M-1).
3100 3104 * ``N-M`` -> include items N..M (closed endpoint).
3101 3105 """
3102 3106 lines = self.history_manager.get_range_by_str(range_str, raw=raw)
3103 3107 return "\n".join(x for _, _, x in lines)
3104 3108
3105 3109 def find_user_code(self, target, raw=True, py_only=False, skip_encoding_cookie=True, search_ns=False):
3106 3110 """Get a code string from history, file, url, or a string or macro.
3107 3111
3108 3112 This is mainly used by magic functions.
3109 3113
3110 3114 Parameters
3111 3115 ----------
3112 3116
3113 3117 target : str
3114 3118
3115 3119 A string specifying code to retrieve. This will be tried respectively
3116 3120 as: ranges of input history (see %history for syntax), url,
3117 3121 corresponding .py file, filename, or an expression evaluating to a
3118 3122 string or Macro in the user namespace.
3119 3123
3120 3124 raw : bool
3121 3125 If true (default), retrieve raw history. Has no effect on the other
3122 3126 retrieval mechanisms.
3123 3127
3124 3128 py_only : bool (default False)
3125 3129 Only try to fetch python code, do not try alternative methods to decode file
3126 3130 if unicode fails.
3127 3131
3128 3132 Returns
3129 3133 -------
3130 3134 A string of code.
3131 3135
3132 3136 ValueError is raised if nothing is found, and TypeError if it evaluates
3133 3137 to an object of another type. In each case, .args[0] is a printable
3134 3138 message.
3135 3139 """
3136 3140 code = self.extract_input_lines(target, raw=raw) # Grab history
3137 3141 if code:
3138 3142 return code
3139 3143 try:
3140 3144 if target.startswith(('http://', 'https://')):
3141 3145 return openpy.read_py_url(target, skip_encoding_cookie=skip_encoding_cookie)
3142 3146 except UnicodeDecodeError:
3143 3147 if not py_only :
3144 3148 # Deferred import
3145 3149 try:
3146 3150 from urllib.request import urlopen # Py3
3147 3151 except ImportError:
3148 3152 from urllib import urlopen
3149 3153 response = urlopen(target)
3150 3154 return response.read().decode('latin1')
3151 3155 raise ValueError(("'%s' seem to be unreadable.") % target)
3152 3156
3153 3157 potential_target = [target]
3154 3158 try :
3155 3159 potential_target.insert(0,get_py_filename(target))
3156 3160 except IOError:
3157 3161 pass
3158 3162
3159 3163 for tgt in potential_target :
3160 3164 if os.path.isfile(tgt): # Read file
3161 3165 try :
3162 3166 return openpy.read_py_file(tgt, skip_encoding_cookie=skip_encoding_cookie)
3163 3167 except UnicodeDecodeError :
3164 3168 if not py_only :
3165 3169 with io_open(tgt,'r', encoding='latin1') as f :
3166 3170 return f.read()
3167 3171 raise ValueError(("'%s' seem to be unreadable.") % target)
3168 3172 elif os.path.isdir(os.path.expanduser(tgt)):
3169 3173 raise ValueError("'%s' is a directory, not a regular file." % target)
3170 3174
3171 3175 if search_ns:
3172 3176 # Inspect namespace to load object source
3173 3177 object_info = self.object_inspect(target, detail_level=1)
3174 3178 if object_info['found'] and object_info['source']:
3175 3179 return object_info['source']
3176 3180
3177 3181 try: # User namespace
3178 3182 codeobj = eval(target, self.user_ns)
3179 3183 except Exception:
3180 3184 raise ValueError(("'%s' was not found in history, as a file, url, "
3181 3185 "nor in the user namespace.") % target)
3182 3186
3183 3187 if isinstance(codeobj, string_types):
3184 3188 return codeobj
3185 3189 elif isinstance(codeobj, Macro):
3186 3190 return codeobj.value
3187 3191
3188 3192 raise TypeError("%s is neither a string nor a macro." % target,
3189 3193 codeobj)
3190 3194
3191 3195 #-------------------------------------------------------------------------
3192 3196 # Things related to IPython exiting
3193 3197 #-------------------------------------------------------------------------
3194 3198 def atexit_operations(self):
3195 3199 """This will be executed at the time of exit.
3196 3200
3197 3201 Cleanup operations and saving of persistent data that is done
3198 3202 unconditionally by IPython should be performed here.
3199 3203
3200 3204 For things that may depend on startup flags or platform specifics (such
3201 3205 as having readline or not), register a separate atexit function in the
3202 3206 code that has the appropriate information, rather than trying to
3203 3207 clutter
3204 3208 """
3205 3209 # Close the history session (this stores the end time and line count)
3206 3210 # this must be *before* the tempfile cleanup, in case of temporary
3207 3211 # history db
3208 3212 self.history_manager.end_session()
3209 3213
3210 3214 # Cleanup all tempfiles and folders left around
3211 3215 for tfile in self.tempfiles:
3212 3216 try:
3213 3217 os.unlink(tfile)
3214 3218 except OSError:
3215 3219 pass
3216 3220
3217 3221 for tdir in self.tempdirs:
3218 3222 try:
3219 3223 os.rmdir(tdir)
3220 3224 except OSError:
3221 3225 pass
3222 3226
3223 3227 # Clear all user namespaces to release all references cleanly.
3224 3228 self.reset(new_session=False)
3225 3229
3226 3230 # Run user hooks
3227 3231 self.hooks.shutdown_hook()
3228 3232
3229 3233 def cleanup(self):
3230 3234 self.restore_sys_module_state()
3231 3235
3232 3236
3233 3237 # Overridden in terminal subclass to change prompts
3234 3238 def switch_doctest_mode(self, mode):
3235 3239 pass
3236 3240
3237 3241
3238 3242 class InteractiveShellABC(with_metaclass(abc.ABCMeta, object)):
3239 3243 """An abstract base class for InteractiveShell."""
3240 3244
3241 3245 InteractiveShellABC.register(InteractiveShell)
@@ -1,411 +1,411 b''
1 1 # encoding: utf-8
2 2 """
3 3 A mixin for :class:`~IPython.core.application.Application` classes that
4 4 launch InteractiveShell instances, load extensions, etc.
5 5 """
6 6
7 7 # Copyright (c) IPython Development Team.
8 8 # Distributed under the terms of the Modified BSD License.
9 9
10 10 from __future__ import absolute_import
11 11 from __future__ import print_function
12 12
13 13 import glob
14 14 import os
15 15 import sys
16 16
17 17 from traitlets.config.application import boolean_flag
18 18 from traitlets.config.configurable import Configurable
19 19 from traitlets.config.loader import Config
20 20 from IPython.core import pylabtools
21 21 from IPython.utils import py3compat
22 22 from IPython.utils.contexts import preserve_keys
23 23 from IPython.utils.path import filefind
24 24 from traitlets import (
25 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 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 35 backend_keys = sorted(pylabtools.backends.keys())
36 36 backend_keys.insert(0, 'auto')
37 37
38 38 shell_flags = {}
39 39
40 40 addflag = lambda *args: shell_flags.update(boolean_flag(*args))
41 41 addflag('autoindent', 'InteractiveShell.autoindent',
42 42 'Turn on autoindenting.', 'Turn off autoindenting.'
43 43 )
44 44 addflag('automagic', 'InteractiveShell.automagic',
45 45 """Turn on the auto calling of magic commands. Type %%magic at the
46 46 IPython prompt for more information.""",
47 47 'Turn off the auto calling of magic commands.'
48 48 )
49 49 addflag('pdb', 'InteractiveShell.pdb',
50 50 "Enable auto calling the pdb debugger after every exception.",
51 51 "Disable auto calling the pdb debugger after every exception."
52 52 )
53 53 addflag('pprint', 'PlainTextFormatter.pprint',
54 54 "Enable auto pretty printing of results.",
55 55 "Disable auto pretty printing of results."
56 56 )
57 57 addflag('color-info', 'InteractiveShell.color_info',
58 58 """IPython can display information about objects via a set of functions,
59 59 and optionally can use colors for this, syntax highlighting
60 60 source code and various other elements. This is on by default, but can cause
61 61 problems with some pagers. If you see such problems, you can disable the
62 62 colours.""",
63 63 "Disable using colors for info related things."
64 64 )
65 65 nosep_config = Config()
66 66 nosep_config.InteractiveShell.separate_in = ''
67 67 nosep_config.InteractiveShell.separate_out = ''
68 68 nosep_config.InteractiveShell.separate_out2 = ''
69 69
70 70 shell_flags['nosep']=(nosep_config, "Eliminate all spacing between prompts.")
71 71 shell_flags['pylab'] = (
72 72 {'InteractiveShellApp' : {'pylab' : 'auto'}},
73 73 """Pre-load matplotlib and numpy for interactive use with
74 74 the default matplotlib backend."""
75 75 )
76 76 shell_flags['matplotlib'] = (
77 77 {'InteractiveShellApp' : {'matplotlib' : 'auto'}},
78 78 """Configure matplotlib for interactive use with
79 79 the default matplotlib backend."""
80 80 )
81 81
82 82 # it's possible we don't want short aliases for *all* of these:
83 83 shell_aliases = dict(
84 84 autocall='InteractiveShell.autocall',
85 85 colors='InteractiveShell.colors',
86 86 logfile='InteractiveShell.logfile',
87 87 logappend='InteractiveShell.logappend',
88 88 c='InteractiveShellApp.code_to_run',
89 89 m='InteractiveShellApp.module_to_run',
90 90 ext='InteractiveShellApp.extra_extension',
91 91 gui='InteractiveShellApp.gui',
92 92 pylab='InteractiveShellApp.pylab',
93 93 matplotlib='InteractiveShellApp.matplotlib',
94 94 )
95 95 shell_aliases['cache-size'] = 'InteractiveShell.cache_size'
96 96
97 97 #-----------------------------------------------------------------------------
98 98 # Main classes and functions
99 99 #-----------------------------------------------------------------------------
100 100
101 101 class InteractiveShellApp(Configurable):
102 102 """A Mixin for applications that start InteractiveShell instances.
103 103
104 104 Provides configurables for loading extensions and executing files
105 105 as part of configuring a Shell environment.
106 106
107 107 The following methods should be called by the :meth:`initialize` method
108 108 of the subclass:
109 109
110 110 - :meth:`init_path`
111 111 - :meth:`init_shell` (to be implemented by the subclass)
112 112 - :meth:`init_gui_pylab`
113 113 - :meth:`init_extensions`
114 114 - :meth:`init_code`
115 115 """
116 116 extensions = List(Unicode(),
117 117 help="A list of dotted module names of IPython extensions to load."
118 118 ).tag(config=True)
119 119 extra_extension = Unicode('',
120 120 help="dotted module name of an IPython extension to load."
121 121 ).tag(config=True)
122 122
123 123 reraise_ipython_extension_failures = Bool(False,
124 124 help="Reraise exceptions encountered loading IPython extensions?",
125 125 ).tag(config=True)
126 126
127 127 # Extensions that are always loaded (not configurable)
128 128 default_extensions = List(Unicode(), [u'storemagic']).tag(config=False)
129 129
130 130 hide_initial_ns = Bool(True,
131 131 help="""Should variables loaded at startup (by startup files, exec_lines, etc.)
132 132 be hidden from tools like %who?"""
133 133 ).tag(config=True)
134 134
135 135 exec_files = List(Unicode(),
136 136 help="""List of files to run at IPython startup."""
137 137 ).tag(config=True)
138 138 exec_PYTHONSTARTUP = Bool(True,
139 139 help="""Run the file referenced by the PYTHONSTARTUP environment
140 140 variable at IPython startup."""
141 141 ).tag(config=True)
142 142 file_to_run = Unicode('',
143 143 help="""A file to be run""").tag(config=True)
144 144
145 145 exec_lines = List(Unicode(),
146 146 help="""lines of code to run at IPython startup."""
147 147 ).tag(config=True)
148 148 code_to_run = Unicode('',
149 149 help="Execute the given command string."
150 150 ).tag(config=True)
151 151 module_to_run = Unicode('',
152 152 help="Run the module as a script."
153 153 ).tag(config=True)
154 154 gui = CaselessStrEnum(gui_keys, allow_none=True,
155 155 help="Enable GUI event loop integration with any of {0}.".format(gui_keys)
156 156 ).tag(config=True)
157 157 matplotlib = CaselessStrEnum(backend_keys, allow_none=True,
158 158 help="""Configure matplotlib for interactive use with
159 159 the default matplotlib backend."""
160 160 ).tag(config=True)
161 161 pylab = CaselessStrEnum(backend_keys, allow_none=True,
162 162 help="""Pre-load matplotlib and numpy for interactive use,
163 163 selecting a particular matplotlib backend and loop integration.
164 164 """
165 165 ).tag(config=True)
166 166 pylab_import_all = Bool(True,
167 167 help="""If true, IPython will populate the user namespace with numpy, pylab, etc.
168 168 and an ``import *`` is done from numpy and pylab, when using pylab mode.
169 169
170 170 When False, pylab mode should not import any names into the user namespace.
171 171 """
172 172 ).tag(config=True)
173 173 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC',
174 174 allow_none=True)
175 175 # whether interact-loop should start
176 176 interact = Bool(True)
177 177
178 178 user_ns = Instance(dict, args=None, allow_none=True)
179 179 @observe('user_ns')
180 180 def _user_ns_changed(self, change):
181 181 if self.shell is not None:
182 182 self.shell.user_ns = change['new']
183 183 self.shell.init_user_ns()
184 184
185 185 def init_path(self):
186 186 """Add current working directory, '', to sys.path"""
187 187 if sys.path[0] != '':
188 188 sys.path.insert(0, '')
189 189
190 190 def init_shell(self):
191 191 raise NotImplementedError("Override in subclasses")
192 192
193 193 def init_gui_pylab(self):
194 194 """Enable GUI event loop integration, taking pylab into account."""
195 195 enable = False
196 196 shell = self.shell
197 197 if self.pylab:
198 198 enable = lambda key: shell.enable_pylab(key, import_all=self.pylab_import_all)
199 199 key = self.pylab
200 200 elif self.matplotlib:
201 201 enable = shell.enable_matplotlib
202 202 key = self.matplotlib
203 203 elif self.gui:
204 204 enable = shell.enable_gui
205 205 key = self.gui
206 206
207 207 if not enable:
208 208 return
209 209
210 210 try:
211 211 r = enable(key)
212 212 except ImportError:
213 213 self.log.warning("Eventloop or matplotlib integration failed. Is matplotlib installed?")
214 214 self.shell.showtraceback()
215 215 return
216 216 except Exception:
217 217 self.log.warning("GUI event loop or pylab initialization failed")
218 218 self.shell.showtraceback()
219 219 return
220 220
221 221 if isinstance(r, tuple):
222 222 gui, backend = r[:2]
223 223 self.log.info("Enabling GUI event loop integration, "
224 224 "eventloop=%s, matplotlib=%s", gui, backend)
225 225 if key == "auto":
226 226 print("Using matplotlib backend: %s" % backend)
227 227 else:
228 228 gui = r
229 229 self.log.info("Enabling GUI event loop integration, "
230 230 "eventloop=%s", gui)
231 231
232 232 def init_extensions(self):
233 233 """Load all IPython extensions in IPythonApp.extensions.
234 234
235 235 This uses the :meth:`ExtensionManager.load_extensions` to load all
236 236 the extensions listed in ``self.extensions``.
237 237 """
238 238 try:
239 239 self.log.debug("Loading IPython extensions...")
240 240 extensions = self.default_extensions + self.extensions
241 241 if self.extra_extension:
242 242 extensions.append(self.extra_extension)
243 243 for ext in extensions:
244 244 try:
245 245 self.log.info("Loading IPython extension: %s" % ext)
246 246 self.shell.extension_manager.load_extension(ext)
247 247 except:
248 248 if self.reraise_ipython_extension_failures:
249 249 raise
250 250 msg = ("Error in loading extension: {ext}\n"
251 251 "Check your config files in {location}".format(
252 252 ext=ext,
253 253 location=self.profile_dir.location
254 254 ))
255 255 self.log.warning(msg, exc_info=True)
256 256 except:
257 257 if self.reraise_ipython_extension_failures:
258 258 raise
259 259 self.log.warning("Unknown error in loading extensions:", exc_info=True)
260 260
261 261 def init_code(self):
262 262 """run the pre-flight code, specified via exec_lines"""
263 263 self._run_startup_files()
264 264 self._run_exec_lines()
265 265 self._run_exec_files()
266 266
267 267 # Hide variables defined here from %who etc.
268 268 if self.hide_initial_ns:
269 269 self.shell.user_ns_hidden.update(self.shell.user_ns)
270 270
271 271 # command-line execution (ipython -i script.py, ipython -m module)
272 272 # should *not* be excluded from %whos
273 273 self._run_cmd_line_code()
274 274 self._run_module()
275 275
276 276 # flush output, so itwon't be attached to the first cell
277 277 sys.stdout.flush()
278 278 sys.stderr.flush()
279 279
280 280 def _run_exec_lines(self):
281 281 """Run lines of code in IPythonApp.exec_lines in the user's namespace."""
282 282 if not self.exec_lines:
283 283 return
284 284 try:
285 285 self.log.debug("Running code from IPythonApp.exec_lines...")
286 286 for line in self.exec_lines:
287 287 try:
288 288 self.log.info("Running code in user namespace: %s" %
289 289 line)
290 290 self.shell.run_cell(line, store_history=False)
291 291 except:
292 292 self.log.warning("Error in executing line in user "
293 293 "namespace: %s" % line)
294 294 self.shell.showtraceback()
295 295 except:
296 296 self.log.warning("Unknown error in handling IPythonApp.exec_lines:")
297 297 self.shell.showtraceback()
298 298
299 299 def _exec_file(self, fname, shell_futures=False):
300 300 try:
301 301 full_filename = filefind(fname, [u'.', self.ipython_dir])
302 302 except IOError:
303 303 self.log.warning("File not found: %r"%fname)
304 304 return
305 305 # Make sure that the running script gets a proper sys.argv as if it
306 306 # were run from a system shell.
307 307 save_argv = sys.argv
308 308 sys.argv = [full_filename] + self.extra_args[1:]
309 309 # protect sys.argv from potential unicode strings on Python 2:
310 310 if not py3compat.PY3:
311 311 sys.argv = [ py3compat.cast_bytes(a) for a in sys.argv ]
312 312 try:
313 313 if os.path.isfile(full_filename):
314 314 self.log.info("Running file in user namespace: %s" %
315 315 full_filename)
316 316 # Ensure that __file__ is always defined to match Python
317 317 # behavior.
318 318 with preserve_keys(self.shell.user_ns, '__file__'):
319 319 self.shell.user_ns['__file__'] = fname
320 320 if full_filename.endswith('.ipy'):
321 321 self.shell.safe_execfile_ipy(full_filename,
322 322 shell_futures=shell_futures)
323 323 else:
324 324 # default to python, even without extension
325 325 self.shell.safe_execfile(full_filename,
326 326 self.shell.user_ns,
327 327 shell_futures=shell_futures,
328 328 raise_exceptions=True)
329 329 finally:
330 330 sys.argv = save_argv
331 331
332 332 def _run_startup_files(self):
333 333 """Run files from profile startup directory"""
334 334 startup_dir = self.profile_dir.startup_dir
335 335 startup_files = []
336 336
337 337 if self.exec_PYTHONSTARTUP and os.environ.get('PYTHONSTARTUP', False) and \
338 338 not (self.file_to_run or self.code_to_run or self.module_to_run):
339 339 python_startup = os.environ['PYTHONSTARTUP']
340 340 self.log.debug("Running PYTHONSTARTUP file %s...", python_startup)
341 341 try:
342 342 self._exec_file(python_startup)
343 343 except:
344 344 self.log.warning("Unknown error in handling PYTHONSTARTUP file %s:", python_startup)
345 345 self.shell.showtraceback()
346 346
347 347 startup_files += glob.glob(os.path.join(startup_dir, '*.py'))
348 348 startup_files += glob.glob(os.path.join(startup_dir, '*.ipy'))
349 349 if not startup_files:
350 350 return
351 351
352 352 self.log.debug("Running startup files from %s...", startup_dir)
353 353 try:
354 354 for fname in sorted(startup_files):
355 355 self._exec_file(fname)
356 356 except:
357 357 self.log.warning("Unknown error in handling startup files:")
358 358 self.shell.showtraceback()
359 359
360 360 def _run_exec_files(self):
361 361 """Run files from IPythonApp.exec_files"""
362 362 if not self.exec_files:
363 363 return
364 364
365 365 self.log.debug("Running files in IPythonApp.exec_files...")
366 366 try:
367 367 for fname in self.exec_files:
368 368 self._exec_file(fname)
369 369 except:
370 370 self.log.warning("Unknown error in handling IPythonApp.exec_files:")
371 371 self.shell.showtraceback()
372 372
373 373 def _run_cmd_line_code(self):
374 374 """Run code or file specified at the command-line"""
375 375 if self.code_to_run:
376 376 line = self.code_to_run
377 377 try:
378 378 self.log.info("Running code given at command line (c=): %s" %
379 379 line)
380 380 self.shell.run_cell(line, store_history=False)
381 381 except:
382 382 self.log.warning("Error in executing line in user namespace: %s" %
383 383 line)
384 384 self.shell.showtraceback()
385 385 if not self.interact:
386 386 self.exit(1)
387 387
388 388 # Like Python itself, ignore the second if the first of these is present
389 389 elif self.file_to_run:
390 390 fname = self.file_to_run
391 391 if os.path.isdir(fname):
392 392 fname = os.path.join(fname, "__main__.py")
393 393 try:
394 394 self._exec_file(fname, shell_futures=True)
395 395 except:
396 396 self.shell.showtraceback(tb_offset=4)
397 397 if not self.interact:
398 398 self.exit(1)
399 399
400 400 def _run_module(self):
401 401 """Run module specified at the command-line."""
402 402 if self.module_to_run:
403 403 # Make sure that the module gets a proper sys.argv as if it were
404 404 # run using `python -m`.
405 405 save_argv = sys.argv
406 406 sys.argv = [sys.executable] + self.extra_args
407 407 try:
408 408 self.shell.safe_run_module(self.module_to_run,
409 409 self.shell.user_ns)
410 410 finally:
411 411 sys.argv = save_argv
@@ -1,233 +1,226 b''
1 1 """Tests for debugging machinery.
2 2 """
3 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 #-----------------------------------------------------------------------------
13 # Imports
14 #-----------------------------------------------------------------------------
5 # Copyright (c) IPython Development Team.
6 # Distributed under the terms of the Modified BSD License.
15 7
16 8 import sys
9 import warnings
17 10
18 # third-party
19 11 import nose.tools as nt
20 12
21 # Our own
22 13 from IPython.core import debugger
23 14
24 15 #-----------------------------------------------------------------------------
25 16 # Helper classes, from CPython's Pdb test suite
26 17 #-----------------------------------------------------------------------------
27 18
28 19 class _FakeInput(object):
29 20 """
30 21 A fake input stream for pdb's interactive debugger. Whenever a
31 22 line is read, print it (to simulate the user typing it), and then
32 23 return it. The set of lines to return is specified in the
33 24 constructor; they should not have trailing newlines.
34 25 """
35 26 def __init__(self, lines):
36 27 self.lines = iter(lines)
37 28
38 29 def readline(self):
39 30 line = next(self.lines)
40 31 print(line)
41 32 return line+'\n'
42 33
43 34 class PdbTestInput(object):
44 35 """Context manager that makes testing Pdb in doctests easier."""
45 36
46 37 def __init__(self, input):
47 38 self.input = input
48 39
49 40 def __enter__(self):
50 41 self.real_stdin = sys.stdin
51 42 sys.stdin = _FakeInput(self.input)
52 43
53 44 def __exit__(self, *exc):
54 45 sys.stdin = self.real_stdin
55 46
56 47 #-----------------------------------------------------------------------------
57 48 # Tests
58 49 #-----------------------------------------------------------------------------
59 50
60 51 def test_longer_repr():
61 52 try:
62 53 from reprlib import repr as trepr # Py 3
63 54 except ImportError:
64 55 from repr import repr as trepr # Py 2
65 56
66 57 a = '1234567890'* 7
67 58 ar = "'1234567890123456789012345678901234567890123456789012345678901234567890'"
68 59 a_trunc = "'123456789012...8901234567890'"
69 60 nt.assert_equal(trepr(a), a_trunc)
70 61 # The creation of our tracer modifies the repr module's repr function
71 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 66 nt.assert_equal(trepr(a), ar)
74 67
75 68 def test_ipdb_magics():
76 69 '''Test calling some IPython magics from ipdb.
77 70
78 71 First, set up some test functions and classes which we can inspect.
79 72
80 73 >>> class ExampleClass(object):
81 74 ... """Docstring for ExampleClass."""
82 75 ... def __init__(self):
83 76 ... """Docstring for ExampleClass.__init__"""
84 77 ... pass
85 78 ... def __str__(self):
86 79 ... return "ExampleClass()"
87 80
88 81 >>> def example_function(x, y, z="hello"):
89 82 ... """Docstring for example_function."""
90 83 ... pass
91 84
92 85 >>> old_trace = sys.gettrace()
93 86
94 87 Create a function which triggers ipdb.
95 88
96 89 >>> def trigger_ipdb():
97 90 ... a = ExampleClass()
98 91 ... debugger.Pdb().set_trace()
99 92
100 93 >>> with PdbTestInput([
101 94 ... 'pdef example_function',
102 95 ... 'pdoc ExampleClass',
103 96 ... 'up',
104 97 ... 'down',
105 98 ... 'list',
106 99 ... 'pinfo a',
107 100 ... 'll',
108 101 ... 'continue',
109 102 ... ]):
110 103 ... trigger_ipdb()
111 104 --Return--
112 105 None
113 106 > <doctest ...>(3)trigger_ipdb()
114 107 1 def trigger_ipdb():
115 108 2 a = ExampleClass()
116 109 ----> 3 debugger.Pdb().set_trace()
117 110 <BLANKLINE>
118 111 ipdb> pdef example_function
119 112 example_function(x, y, z='hello')
120 113 ipdb> pdoc ExampleClass
121 114 Class docstring:
122 115 Docstring for ExampleClass.
123 116 Init docstring:
124 117 Docstring for ExampleClass.__init__
125 118 ipdb> up
126 119 > <doctest ...>(11)<module>()
127 120 7 'pinfo a',
128 121 8 'll',
129 122 9 'continue',
130 123 10 ]):
131 124 ---> 11 trigger_ipdb()
132 125 <BLANKLINE>
133 126 ipdb> down
134 127 None
135 128 > <doctest ...>(3)trigger_ipdb()
136 129 1 def trigger_ipdb():
137 130 2 a = ExampleClass()
138 131 ----> 3 debugger.Pdb().set_trace()
139 132 <BLANKLINE>
140 133 ipdb> list
141 134 1 def trigger_ipdb():
142 135 2 a = ExampleClass()
143 136 ----> 3 debugger.Pdb().set_trace()
144 137 <BLANKLINE>
145 138 ipdb> pinfo a
146 139 Type: ExampleClass
147 140 String form: ExampleClass()
148 141 Namespace: Local...
149 142 Docstring: Docstring for ExampleClass.
150 143 Init docstring: Docstring for ExampleClass.__init__
151 144 ipdb> ll
152 145 1 def trigger_ipdb():
153 146 2 a = ExampleClass()
154 147 ----> 3 debugger.Pdb().set_trace()
155 148 <BLANKLINE>
156 149 ipdb> continue
157 150
158 151 Restore previous trace function, e.g. for coverage.py
159 152
160 153 >>> sys.settrace(old_trace)
161 154 '''
162 155
163 156 def test_ipdb_magics2():
164 157 '''Test ipdb with a very short function.
165 158
166 159 >>> old_trace = sys.gettrace()
167 160
168 161 >>> def bar():
169 162 ... pass
170 163
171 164 Run ipdb.
172 165
173 166 >>> with PdbTestInput([
174 167 ... 'continue',
175 168 ... ]):
176 169 ... debugger.Pdb().runcall(bar)
177 170 > <doctest ...>(2)bar()
178 171 1 def bar():
179 172 ----> 2 pass
180 173 <BLANKLINE>
181 174 ipdb> continue
182 175
183 176 Restore previous trace function, e.g. for coverage.py
184 177
185 178 >>> sys.settrace(old_trace)
186 179 '''
187 180
188 181 def can_quit():
189 182 '''Test that quit work in ipydb
190 183
191 184 >>> old_trace = sys.gettrace()
192 185
193 186 >>> def bar():
194 187 ... pass
195 188
196 189 >>> with PdbTestInput([
197 190 ... 'quit',
198 191 ... ]):
199 192 ... debugger.Pdb().runcall(bar)
200 193 > <doctest ...>(2)bar()
201 194 1 def bar():
202 195 ----> 2 pass
203 196 <BLANKLINE>
204 197 ipdb> quit
205 198
206 199 Restore previous trace function, e.g. for coverage.py
207 200
208 201 >>> sys.settrace(old_trace)
209 202 '''
210 203
211 204
212 205 def can_exit():
213 206 '''Test that quit work in ipydb
214 207
215 208 >>> old_trace = sys.gettrace()
216 209
217 210 >>> def bar():
218 211 ... pass
219 212
220 213 >>> with PdbTestInput([
221 214 ... 'exit',
222 215 ... ]):
223 216 ... debugger.Pdb().runcall(bar)
224 217 > <doctest ...>(2)bar()
225 218 1 def bar():
226 219 ----> 2 pass
227 220 <BLANKLINE>
228 221 ipdb> exit
229 222
230 223 Restore previous trace function, e.g. for coverage.py
231 224
232 225 >>> sys.settrace(old_trace)
233 226 '''
@@ -1,1494 +1,1494 b''
1 1 # -*- coding: utf-8 -*-
2 2 """
3 3 Verbose and colourful traceback formatting.
4 4
5 5 **ColorTB**
6 6
7 7 I've always found it a bit hard to visually parse tracebacks in Python. The
8 8 ColorTB class is a solution to that problem. It colors the different parts of a
9 9 traceback in a manner similar to what you would expect from a syntax-highlighting
10 10 text editor.
11 11
12 12 Installation instructions for ColorTB::
13 13
14 14 import sys,ultratb
15 15 sys.excepthook = ultratb.ColorTB()
16 16
17 17 **VerboseTB**
18 18
19 19 I've also included a port of Ka-Ping Yee's "cgitb.py" that produces all kinds
20 20 of useful info when a traceback occurs. Ping originally had it spit out HTML
21 21 and intended it for CGI programmers, but why should they have all the fun? I
22 22 altered it to spit out colored text to the terminal. It's a bit overwhelming,
23 23 but kind of neat, and maybe useful for long-running programs that you believe
24 24 are bug-free. If a crash *does* occur in that type of program you want details.
25 25 Give it a shot--you'll love it or you'll hate it.
26 26
27 27 .. note::
28 28
29 29 The Verbose mode prints the variables currently visible where the exception
30 30 happened (shortening their strings if too long). This can potentially be
31 31 very slow, if you happen to have a huge data structure whose string
32 32 representation is complex to compute. Your computer may appear to freeze for
33 33 a while with cpu usage at 100%. If this occurs, you can cancel the traceback
34 34 with Ctrl-C (maybe hitting it more than once).
35 35
36 36 If you encounter this kind of situation often, you may want to use the
37 37 Verbose_novars mode instead of the regular Verbose, which avoids formatting
38 38 variables (but otherwise includes the information and context given by
39 39 Verbose).
40 40
41 41 .. note::
42 42
43 43 The verbose mode print all variables in the stack, which means it can
44 44 potentially leak sensitive information like access keys, or unencryted
45 45 password.
46 46
47 47 Installation instructions for VerboseTB::
48 48
49 49 import sys,ultratb
50 50 sys.excepthook = ultratb.VerboseTB()
51 51
52 52 Note: Much of the code in this module was lifted verbatim from the standard
53 53 library module 'traceback.py' and Ka-Ping Yee's 'cgitb.py'.
54 54
55 55 Color schemes
56 56 -------------
57 57
58 58 The colors are defined in the class TBTools through the use of the
59 59 ColorSchemeTable class. Currently the following exist:
60 60
61 61 - NoColor: allows all of this module to be used in any terminal (the color
62 62 escapes are just dummy blank strings).
63 63
64 64 - Linux: is meant to look good in a terminal like the Linux console (black
65 65 or very dark background).
66 66
67 67 - LightBG: similar to Linux but swaps dark/light colors to be more readable
68 68 in light background terminals.
69 69
70 70 - Neutral: a neutral color scheme that should be readable on both light and
71 71 dark background
72 72
73 73 You can implement other color schemes easily, the syntax is fairly
74 74 self-explanatory. Please send back new schemes you develop to the author for
75 75 possible inclusion in future releases.
76 76
77 77 Inheritance diagram:
78 78
79 79 .. inheritance-diagram:: IPython.core.ultratb
80 80 :parts: 3
81 81 """
82 82
83 83 #*****************************************************************************
84 84 # Copyright (C) 2001 Nathaniel Gray <n8gray@caltech.edu>
85 85 # Copyright (C) 2001-2004 Fernando Perez <fperez@colorado.edu>
86 86 #
87 87 # Distributed under the terms of the BSD License. The full license is in
88 88 # the file COPYING, distributed as part of this software.
89 89 #*****************************************************************************
90 90
91 91 from __future__ import absolute_import
92 92 from __future__ import unicode_literals
93 93 from __future__ import print_function
94 94
95 95 import dis
96 96 import inspect
97 97 import keyword
98 98 import linecache
99 99 import os
100 100 import pydoc
101 101 import re
102 102 import sys
103 103 import time
104 104 import tokenize
105 105 import traceback
106 106 import types
107 107
108 108 try: # Python 2
109 109 generate_tokens = tokenize.generate_tokens
110 110 except AttributeError: # Python 3
111 111 generate_tokens = tokenize.tokenize
112 112
113 113 # For purposes of monkeypatching inspect to fix a bug in it.
114 114 from inspect import getsourcefile, getfile, getmodule, \
115 115 ismodule, isclass, ismethod, isfunction, istraceback, isframe, iscode
116 116
117 117 # IPython's own modules
118 118 from IPython import get_ipython
119 119 from IPython.core import debugger
120 120 from IPython.core.display_trap import DisplayTrap
121 121 from IPython.core.excolors import exception_colors
122 122 from IPython.utils import PyColorize
123 123 from IPython.utils import openpy
124 124 from IPython.utils import path as util_path
125 125 from IPython.utils import py3compat
126 126 from IPython.utils import ulinecache
127 127 from IPython.utils.data import uniq_stable
128 128 from IPython.utils.terminal import get_terminal_size
129 129 from logging import info, error
130 130
131 131 import IPython.utils.colorable as colorable
132 132
133 133 # Globals
134 134 # amount of space to put line numbers before verbose tracebacks
135 135 INDENT_SIZE = 8
136 136
137 137 # Default color scheme. This is used, for example, by the traceback
138 138 # formatter. When running in an actual IPython instance, the user's rc.colors
139 139 # value is used, but having a module global makes this functionality available
140 140 # to users of ultratb who are NOT running inside ipython.
141 141 DEFAULT_SCHEME = 'NoColor'
142 142
143 143 # ---------------------------------------------------------------------------
144 144 # Code begins
145 145
146 146 # Utility functions
147 147 def inspect_error():
148 148 """Print a message about internal inspect errors.
149 149
150 150 These are unfortunately quite common."""
151 151
152 152 error('Internal Python error in the inspect module.\n'
153 153 'Below is the traceback from this internal error.\n')
154 154
155 155
156 156 # This function is a monkeypatch we apply to the Python inspect module. We have
157 157 # now found when it's needed (see discussion on issue gh-1456), and we have a
158 158 # test case (IPython.core.tests.test_ultratb.ChangedPyFileTest) that fails if
159 159 # the monkeypatch is not applied. TK, Aug 2012.
160 160 def findsource(object):
161 161 """Return the entire source file and starting line number for an object.
162 162
163 163 The argument may be a module, class, method, function, traceback, frame,
164 164 or code object. The source code is returned as a list of all the lines
165 165 in the file and the line number indexes a line in that list. An IOError
166 166 is raised if the source code cannot be retrieved.
167 167
168 168 FIXED version with which we monkeypatch the stdlib to work around a bug."""
169 169
170 170 file = getsourcefile(object) or getfile(object)
171 171 # If the object is a frame, then trying to get the globals dict from its
172 172 # module won't work. Instead, the frame object itself has the globals
173 173 # dictionary.
174 174 globals_dict = None
175 175 if inspect.isframe(object):
176 176 # XXX: can this ever be false?
177 177 globals_dict = object.f_globals
178 178 else:
179 179 module = getmodule(object, file)
180 180 if module:
181 181 globals_dict = module.__dict__
182 182 lines = linecache.getlines(file, globals_dict)
183 183 if not lines:
184 184 raise IOError('could not get source code')
185 185
186 186 if ismodule(object):
187 187 return lines, 0
188 188
189 189 if isclass(object):
190 190 name = object.__name__
191 191 pat = re.compile(r'^(\s*)class\s*' + name + r'\b')
192 192 # make some effort to find the best matching class definition:
193 193 # use the one with the least indentation, which is the one
194 194 # that's most probably not inside a function definition.
195 195 candidates = []
196 196 for i in range(len(lines)):
197 197 match = pat.match(lines[i])
198 198 if match:
199 199 # if it's at toplevel, it's already the best one
200 200 if lines[i][0] == 'c':
201 201 return lines, i
202 202 # else add whitespace to candidate list
203 203 candidates.append((match.group(1), i))
204 204 if candidates:
205 205 # this will sort by whitespace, and by line number,
206 206 # less whitespace first
207 207 candidates.sort()
208 208 return lines, candidates[0][1]
209 209 else:
210 210 raise IOError('could not find class definition')
211 211
212 212 if ismethod(object):
213 213 object = object.__func__
214 214 if isfunction(object):
215 215 object = object.__code__
216 216 if istraceback(object):
217 217 object = object.tb_frame
218 218 if isframe(object):
219 219 object = object.f_code
220 220 if iscode(object):
221 221 if not hasattr(object, 'co_firstlineno'):
222 222 raise IOError('could not find function definition')
223 223 pat = re.compile(r'^(\s*def\s)|(.*(?<!\w)lambda(:|\s))|^(\s*@)')
224 224 pmatch = pat.match
225 225 # fperez - fix: sometimes, co_firstlineno can give a number larger than
226 226 # the length of lines, which causes an error. Safeguard against that.
227 227 lnum = min(object.co_firstlineno, len(lines)) - 1
228 228 while lnum > 0:
229 229 if pmatch(lines[lnum]):
230 230 break
231 231 lnum -= 1
232 232
233 233 return lines, lnum
234 234 raise IOError('could not find code object')
235 235
236 236
237 237 # This is a patched version of inspect.getargs that applies the (unmerged)
238 238 # patch for http://bugs.python.org/issue14611 by Stefano Taschini. This fixes
239 239 # https://github.com/ipython/ipython/issues/8205 and
240 240 # https://github.com/ipython/ipython/issues/8293
241 241 def getargs(co):
242 242 """Get information about the arguments accepted by a code object.
243 243
244 244 Three things are returned: (args, varargs, varkw), where 'args' is
245 245 a list of argument names (possibly containing nested lists), and
246 246 'varargs' and 'varkw' are the names of the * and ** arguments or None."""
247 247 if not iscode(co):
248 248 raise TypeError('{!r} is not a code object'.format(co))
249 249
250 250 nargs = co.co_argcount
251 251 names = co.co_varnames
252 252 args = list(names[:nargs])
253 253 step = 0
254 254
255 255 # The following acrobatics are for anonymous (tuple) arguments.
256 256 for i in range(nargs):
257 257 if args[i][:1] in ('', '.'):
258 258 stack, remain, count = [], [], []
259 259 while step < len(co.co_code):
260 260 op = ord(co.co_code[step])
261 261 step = step + 1
262 262 if op >= dis.HAVE_ARGUMENT:
263 263 opname = dis.opname[op]
264 264 value = ord(co.co_code[step]) + ord(co.co_code[step+1])*256
265 265 step = step + 2
266 266 if opname in ('UNPACK_TUPLE', 'UNPACK_SEQUENCE'):
267 267 remain.append(value)
268 268 count.append(value)
269 269 elif opname in ('STORE_FAST', 'STORE_DEREF'):
270 270 if op in dis.haslocal:
271 271 stack.append(co.co_varnames[value])
272 272 elif op in dis.hasfree:
273 273 stack.append((co.co_cellvars + co.co_freevars)[value])
274 274 # Special case for sublists of length 1: def foo((bar))
275 275 # doesn't generate the UNPACK_TUPLE bytecode, so if
276 276 # `remain` is empty here, we have such a sublist.
277 277 if not remain:
278 278 stack[0] = [stack[0]]
279 279 break
280 280 else:
281 281 remain[-1] = remain[-1] - 1
282 282 while remain[-1] == 0:
283 283 remain.pop()
284 284 size = count.pop()
285 285 stack[-size:] = [stack[-size:]]
286 286 if not remain:
287 287 break
288 288 remain[-1] = remain[-1] - 1
289 289 if not remain:
290 290 break
291 291 args[i] = stack[0]
292 292
293 293 varargs = None
294 294 if co.co_flags & inspect.CO_VARARGS:
295 295 varargs = co.co_varnames[nargs]
296 296 nargs = nargs + 1
297 297 varkw = None
298 298 if co.co_flags & inspect.CO_VARKEYWORDS:
299 299 varkw = co.co_varnames[nargs]
300 300 return inspect.Arguments(args, varargs, varkw)
301 301
302 302
303 303 # Monkeypatch inspect to apply our bugfix.
304 304 def with_patch_inspect(f):
305 305 """decorator for monkeypatching inspect.findsource"""
306 306
307 307 def wrapped(*args, **kwargs):
308 308 save_findsource = inspect.findsource
309 309 save_getargs = inspect.getargs
310 310 inspect.findsource = findsource
311 311 inspect.getargs = getargs
312 312 try:
313 313 return f(*args, **kwargs)
314 314 finally:
315 315 inspect.findsource = save_findsource
316 316 inspect.getargs = save_getargs
317 317
318 318 return wrapped
319 319
320 320
321 321 if py3compat.PY3:
322 322 fixed_getargvalues = inspect.getargvalues
323 323 else:
324 324 # Fixes for https://github.com/ipython/ipython/issues/8293
325 325 # and https://github.com/ipython/ipython/issues/8205.
326 326 # The relevant bug is caused by failure to correctly handle anonymous tuple
327 327 # unpacking, which only exists in Python 2.
328 328 fixed_getargvalues = with_patch_inspect(inspect.getargvalues)
329 329
330 330
331 331 def fix_frame_records_filenames(records):
332 332 """Try to fix the filenames in each record from inspect.getinnerframes().
333 333
334 334 Particularly, modules loaded from within zip files have useless filenames
335 335 attached to their code object, and inspect.getinnerframes() just uses it.
336 336 """
337 337 fixed_records = []
338 338 for frame, filename, line_no, func_name, lines, index in records:
339 339 # Look inside the frame's globals dictionary for __file__,
340 340 # which should be better. However, keep Cython filenames since
341 341 # we prefer the source filenames over the compiled .so file.
342 342 filename = py3compat.cast_unicode_py2(filename, "utf-8")
343 343 if not filename.endswith(('.pyx', '.pxd', '.pxi')):
344 344 better_fn = frame.f_globals.get('__file__', None)
345 345 if isinstance(better_fn, str):
346 346 # Check the type just in case someone did something weird with
347 347 # __file__. It might also be None if the error occurred during
348 348 # import.
349 349 filename = better_fn
350 350 fixed_records.append((frame, filename, line_no, func_name, lines, index))
351 351 return fixed_records
352 352
353 353
354 354 @with_patch_inspect
355 355 def _fixed_getinnerframes(etb, context=1, tb_offset=0):
356 356 LNUM_POS, LINES_POS, INDEX_POS = 2, 4, 5
357 357
358 358 records = fix_frame_records_filenames(inspect.getinnerframes(etb, context))
359 359 # If the error is at the console, don't build any context, since it would
360 360 # otherwise produce 5 blank lines printed out (there is no file at the
361 361 # console)
362 362 rec_check = records[tb_offset:]
363 363 try:
364 364 rname = rec_check[0][1]
365 365 if rname == '<ipython console>' or rname.endswith('<string>'):
366 366 return rec_check
367 367 except IndexError:
368 368 pass
369 369
370 370 aux = traceback.extract_tb(etb)
371 371 assert len(records) == len(aux)
372 372 for i, (file, lnum, _, _) in zip(range(len(records)), aux):
373 373 maybeStart = lnum - 1 - context // 2
374 374 start = max(maybeStart, 0)
375 375 end = start + context
376 376 lines = ulinecache.getlines(file)[start:end]
377 377 buf = list(records[i])
378 378 buf[LNUM_POS] = lnum
379 379 buf[INDEX_POS] = lnum - 1 - start
380 380 buf[LINES_POS] = lines
381 381 records[i] = tuple(buf)
382 382 return records[tb_offset:]
383 383
384 384 # Helper function -- largely belongs to VerboseTB, but we need the same
385 385 # functionality to produce a pseudo verbose TB for SyntaxErrors, so that they
386 386 # can be recognized properly by ipython.el's py-traceback-line-re
387 387 # (SyntaxErrors have to be treated specially because they have no traceback)
388 388
389 389 _parser = PyColorize.Parser()
390 390
391 391
392 392 def _format_traceback_lines(lnum, index, lines, Colors, lvals=None, scheme=None):
393 393 numbers_width = INDENT_SIZE - 1
394 394 res = []
395 395 i = lnum - index
396 396
397 397 # This lets us get fully syntax-highlighted tracebacks.
398 398 if scheme is None:
399 399 ipinst = get_ipython()
400 400 if ipinst is not None:
401 401 scheme = ipinst.colors
402 402 else:
403 403 scheme = DEFAULT_SCHEME
404 404
405 405 _line_format = _parser.format2
406 406
407 407 for line in lines:
408 408 line = py3compat.cast_unicode(line)
409 409
410 410 new_line, err = _line_format(line, 'str', scheme)
411 411 if not err: line = new_line
412 412
413 413 if i == lnum:
414 414 # This is the line with the error
415 415 pad = numbers_width - len(str(i))
416 416 num = '%s%s' % (debugger.make_arrow(pad), str(lnum))
417 417 line = '%s%s%s %s%s' % (Colors.linenoEm, num,
418 418 Colors.line, line, Colors.Normal)
419 419 else:
420 420 num = '%*s' % (numbers_width, i)
421 421 line = '%s%s%s %s' % (Colors.lineno, num,
422 422 Colors.Normal, line)
423 423
424 424 res.append(line)
425 425 if lvals and i == lnum:
426 426 res.append(lvals + '\n')
427 427 i = i + 1
428 428 return res
429 429
430 430 def is_recursion_error(etype, value, records):
431 431 try:
432 432 # RecursionError is new in Python 3.5
433 433 recursion_error_type = RecursionError
434 434 except NameError:
435 435 recursion_error_type = RuntimeError
436 436
437 437 # The default recursion limit is 1000, but some of that will be taken up
438 438 # by stack frames in IPython itself. >500 frames probably indicates
439 439 # a recursion error.
440 440 return (etype is recursion_error_type) \
441 441 and "recursion" in str(value).lower() \
442 442 and len(records) > 500
443 443
444 444 def find_recursion(etype, value, records):
445 445 """Identify the repeating stack frames from a RecursionError traceback
446 446
447 447 'records' is a list as returned by VerboseTB.get_records()
448 448
449 449 Returns (last_unique, repeat_length)
450 450 """
451 451 # This involves a bit of guesswork - we want to show enough of the traceback
452 452 # to indicate where the recursion is occurring. We guess that the innermost
453 453 # quarter of the traceback (250 frames by default) is repeats, and find the
454 454 # first frame (from in to out) that looks different.
455 455 if not is_recursion_error(etype, value, records):
456 456 return len(records), 0
457 457
458 458 # Select filename, lineno, func_name to track frames with
459 459 records = [r[1:4] for r in records]
460 460 inner_frames = records[-(len(records)//4):]
461 461 frames_repeated = set(inner_frames)
462 462
463 463 last_seen_at = {}
464 464 longest_repeat = 0
465 465 i = len(records)
466 466 for frame in reversed(records):
467 467 i -= 1
468 468 if frame not in frames_repeated:
469 469 last_unique = i
470 470 break
471 471
472 472 if frame in last_seen_at:
473 473 distance = last_seen_at[frame] - i
474 474 longest_repeat = max(longest_repeat, distance)
475 475
476 476 last_seen_at[frame] = i
477 477 else:
478 478 last_unique = 0 # The whole traceback was recursion
479 479
480 480 return last_unique, longest_repeat
481 481
482 482 #---------------------------------------------------------------------------
483 483 # Module classes
484 484 class TBTools(colorable.Colorable):
485 485 """Basic tools used by all traceback printer classes."""
486 486
487 487 # Number of frames to skip when reporting tracebacks
488 488 tb_offset = 0
489 489
490 490 def __init__(self, color_scheme='NoColor', call_pdb=False, ostream=None, parent=None, config=None):
491 491 # Whether to call the interactive pdb debugger after printing
492 492 # tracebacks or not
493 493 super(TBTools, self).__init__(parent=parent, config=config)
494 494 self.call_pdb = call_pdb
495 495
496 496 # Output stream to write to. Note that we store the original value in
497 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
499 # things are written now, the io.stdout object is dynamically managed
498 # that we can delay accessing sys.stdout until runtime. The way
499 # things are written now, the sys.stdout object is dynamically managed
500 500 # so a reference to it should NEVER be stored statically. This
501 501 # property approach confines this detail to a single location, and all
502 502 # subclasses can simply access self.ostream for writing.
503 503 self._ostream = ostream
504 504
505 505 # Create color table
506 506 self.color_scheme_table = exception_colors()
507 507
508 508 self.set_colors(color_scheme)
509 509 self.old_scheme = color_scheme # save initial value for toggles
510 510
511 511 if call_pdb:
512 self.pdb = debugger.Pdb(self.color_scheme_table.active_scheme_name)
512 self.pdb = debugger.Pdb()
513 513 else:
514 514 self.pdb = None
515 515
516 516 def _get_ostream(self):
517 517 """Output stream that exceptions are written to.
518 518
519 519 Valid values are:
520 520
521 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 523 Windows (where plain stdout doesn't recognize ANSI escapes).
524 524
525 525 - Any object with 'write' and 'flush' attributes.
526 526 """
527 527 return sys.stdout if self._ostream is None else self._ostream
528 528
529 529 def _set_ostream(self, val):
530 530 assert val is None or (hasattr(val, 'write') and hasattr(val, 'flush'))
531 531 self._ostream = val
532 532
533 533 ostream = property(_get_ostream, _set_ostream)
534 534
535 535 def set_colors(self, *args, **kw):
536 536 """Shorthand access to the color table scheme selector method."""
537 537
538 538 # Set own color table
539 539 self.color_scheme_table.set_active_scheme(*args, **kw)
540 540 # for convenience, set Colors to the active scheme
541 541 self.Colors = self.color_scheme_table.active_colors
542 542 # Also set colors of debugger
543 543 if hasattr(self, 'pdb') and self.pdb is not None:
544 544 self.pdb.set_colors(*args, **kw)
545 545
546 546 def color_toggle(self):
547 547 """Toggle between the currently active color scheme and NoColor."""
548 548
549 549 if self.color_scheme_table.active_scheme_name == 'NoColor':
550 550 self.color_scheme_table.set_active_scheme(self.old_scheme)
551 551 self.Colors = self.color_scheme_table.active_colors
552 552 else:
553 553 self.old_scheme = self.color_scheme_table.active_scheme_name
554 554 self.color_scheme_table.set_active_scheme('NoColor')
555 555 self.Colors = self.color_scheme_table.active_colors
556 556
557 557 def stb2text(self, stb):
558 558 """Convert a structured traceback (a list) to a string."""
559 559 return '\n'.join(stb)
560 560
561 561 def text(self, etype, value, tb, tb_offset=None, context=5):
562 562 """Return formatted traceback.
563 563
564 564 Subclasses may override this if they add extra arguments.
565 565 """
566 566 tb_list = self.structured_traceback(etype, value, tb,
567 567 tb_offset, context)
568 568 return self.stb2text(tb_list)
569 569
570 570 def structured_traceback(self, etype, evalue, tb, tb_offset=None,
571 571 context=5, mode=None):
572 572 """Return a list of traceback frames.
573 573
574 574 Must be implemented by each class.
575 575 """
576 576 raise NotImplementedError()
577 577
578 578
579 579 #---------------------------------------------------------------------------
580 580 class ListTB(TBTools):
581 581 """Print traceback information from a traceback list, with optional color.
582 582
583 583 Calling requires 3 arguments: (etype, evalue, elist)
584 584 as would be obtained by::
585 585
586 586 etype, evalue, tb = sys.exc_info()
587 587 if tb:
588 588 elist = traceback.extract_tb(tb)
589 589 else:
590 590 elist = None
591 591
592 592 It can thus be used by programs which need to process the traceback before
593 593 printing (such as console replacements based on the code module from the
594 594 standard library).
595 595
596 596 Because they are meant to be called without a full traceback (only a
597 597 list), instances of this class can't call the interactive pdb debugger."""
598 598
599 599 def __init__(self, color_scheme='NoColor', call_pdb=False, ostream=None, parent=None):
600 600 TBTools.__init__(self, color_scheme=color_scheme, call_pdb=call_pdb,
601 601 ostream=ostream, parent=parent)
602 602
603 603 def __call__(self, etype, value, elist):
604 604 self.ostream.flush()
605 605 self.ostream.write(self.text(etype, value, elist))
606 606 self.ostream.write('\n')
607 607
608 608 def structured_traceback(self, etype, value, elist, tb_offset=None,
609 609 context=5):
610 610 """Return a color formatted string with the traceback info.
611 611
612 612 Parameters
613 613 ----------
614 614 etype : exception type
615 615 Type of the exception raised.
616 616
617 617 value : object
618 618 Data stored in the exception
619 619
620 620 elist : list
621 621 List of frames, see class docstring for details.
622 622
623 623 tb_offset : int, optional
624 624 Number of frames in the traceback to skip. If not given, the
625 625 instance value is used (set in constructor).
626 626
627 627 context : int, optional
628 628 Number of lines of context information to print.
629 629
630 630 Returns
631 631 -------
632 632 String with formatted exception.
633 633 """
634 634 tb_offset = self.tb_offset if tb_offset is None else tb_offset
635 635 Colors = self.Colors
636 636 out_list = []
637 637 if elist:
638 638
639 639 if tb_offset and len(elist) > tb_offset:
640 640 elist = elist[tb_offset:]
641 641
642 642 out_list.append('Traceback %s(most recent call last)%s:' %
643 643 (Colors.normalEm, Colors.Normal) + '\n')
644 644 out_list.extend(self._format_list(elist))
645 645 # The exception info should be a single entry in the list.
646 646 lines = ''.join(self._format_exception_only(etype, value))
647 647 out_list.append(lines)
648 648
649 649 # Note: this code originally read:
650 650
651 651 ## for line in lines[:-1]:
652 652 ## out_list.append(" "+line)
653 653 ## out_list.append(lines[-1])
654 654
655 655 # This means it was indenting everything but the last line by a little
656 656 # bit. I've disabled this for now, but if we see ugliness somewhere we
657 657 # can restore it.
658 658
659 659 return out_list
660 660
661 661 def _format_list(self, extracted_list):
662 662 """Format a list of traceback entry tuples for printing.
663 663
664 664 Given a list of tuples as returned by extract_tb() or
665 665 extract_stack(), return a list of strings ready for printing.
666 666 Each string in the resulting list corresponds to the item with the
667 667 same index in the argument list. Each string ends in a newline;
668 668 the strings may contain internal newlines as well, for those items
669 669 whose source text line is not None.
670 670
671 671 Lifted almost verbatim from traceback.py
672 672 """
673 673
674 674 Colors = self.Colors
675 675 list = []
676 676 for filename, lineno, name, line in extracted_list[:-1]:
677 677 item = ' File %s"%s"%s, line %s%d%s, in %s%s%s\n' % \
678 678 (Colors.filename, py3compat.cast_unicode_py2(filename, "utf-8"), Colors.Normal,
679 679 Colors.lineno, lineno, Colors.Normal,
680 680 Colors.name, py3compat.cast_unicode_py2(name, "utf-8"), Colors.Normal)
681 681 if line:
682 682 item += ' %s\n' % line.strip()
683 683 list.append(item)
684 684 # Emphasize the last entry
685 685 filename, lineno, name, line = extracted_list[-1]
686 686 item = '%s File %s"%s"%s, line %s%d%s, in %s%s%s%s\n' % \
687 687 (Colors.normalEm,
688 688 Colors.filenameEm, py3compat.cast_unicode_py2(filename, "utf-8"), Colors.normalEm,
689 689 Colors.linenoEm, lineno, Colors.normalEm,
690 690 Colors.nameEm, py3compat.cast_unicode_py2(name, "utf-8"), Colors.normalEm,
691 691 Colors.Normal)
692 692 if line:
693 693 item += '%s %s%s\n' % (Colors.line, line.strip(),
694 694 Colors.Normal)
695 695 list.append(item)
696 696 return list
697 697
698 698 def _format_exception_only(self, etype, value):
699 699 """Format the exception part of a traceback.
700 700
701 701 The arguments are the exception type and value such as given by
702 702 sys.exc_info()[:2]. The return value is a list of strings, each ending
703 703 in a newline. Normally, the list contains a single string; however,
704 704 for SyntaxError exceptions, it contains several lines that (when
705 705 printed) display detailed information about where the syntax error
706 706 occurred. The message indicating which exception occurred is the
707 707 always last string in the list.
708 708
709 709 Also lifted nearly verbatim from traceback.py
710 710 """
711 711 have_filedata = False
712 712 Colors = self.Colors
713 713 list = []
714 714 stype = py3compat.cast_unicode(Colors.excName + etype.__name__ + Colors.Normal)
715 715 if value is None:
716 716 # Not sure if this can still happen in Python 2.6 and above
717 717 list.append(stype + '\n')
718 718 else:
719 719 if issubclass(etype, SyntaxError):
720 720 have_filedata = True
721 721 if not value.filename: value.filename = "<string>"
722 722 if value.lineno:
723 723 lineno = value.lineno
724 724 textline = ulinecache.getline(value.filename, value.lineno)
725 725 else:
726 726 lineno = 'unknown'
727 727 textline = ''
728 728 list.append('%s File %s"%s"%s, line %s%s%s\n' % \
729 729 (Colors.normalEm,
730 730 Colors.filenameEm, py3compat.cast_unicode(value.filename), Colors.normalEm,
731 731 Colors.linenoEm, lineno, Colors.Normal ))
732 732 if textline == '':
733 733 textline = py3compat.cast_unicode(value.text, "utf-8")
734 734
735 735 if textline is not None:
736 736 i = 0
737 737 while i < len(textline) and textline[i].isspace():
738 738 i += 1
739 739 list.append('%s %s%s\n' % (Colors.line,
740 740 textline.strip(),
741 741 Colors.Normal))
742 742 if value.offset is not None:
743 743 s = ' '
744 744 for c in textline[i:value.offset - 1]:
745 745 if c.isspace():
746 746 s += c
747 747 else:
748 748 s += ' '
749 749 list.append('%s%s^%s\n' % (Colors.caret, s,
750 750 Colors.Normal))
751 751
752 752 try:
753 753 s = value.msg
754 754 except Exception:
755 755 s = self._some_str(value)
756 756 if s:
757 757 list.append('%s%s:%s %s\n' % (stype, Colors.excName,
758 758 Colors.Normal, s))
759 759 else:
760 760 list.append('%s\n' % stype)
761 761
762 762 # sync with user hooks
763 763 if have_filedata:
764 764 ipinst = get_ipython()
765 765 if ipinst is not None:
766 766 ipinst.hooks.synchronize_with_editor(value.filename, value.lineno, 0)
767 767
768 768 return list
769 769
770 770 def get_exception_only(self, etype, value):
771 771 """Only print the exception type and message, without a traceback.
772 772
773 773 Parameters
774 774 ----------
775 775 etype : exception type
776 776 value : exception value
777 777 """
778 778 return ListTB.structured_traceback(self, etype, value, [])
779 779
780 780 def show_exception_only(self, etype, evalue):
781 781 """Only print the exception type and message, without a traceback.
782 782
783 783 Parameters
784 784 ----------
785 785 etype : exception type
786 786 value : exception value
787 787 """
788 788 # This method needs to use __call__ from *this* class, not the one from
789 789 # a subclass whose signature or behavior may be different
790 790 ostream = self.ostream
791 791 ostream.flush()
792 792 ostream.write('\n'.join(self.get_exception_only(etype, evalue)))
793 793 ostream.flush()
794 794
795 795 def _some_str(self, value):
796 796 # Lifted from traceback.py
797 797 try:
798 798 return py3compat.cast_unicode(str(value))
799 799 except:
800 800 return u'<unprintable %s object>' % type(value).__name__
801 801
802 802
803 803 #----------------------------------------------------------------------------
804 804 class VerboseTB(TBTools):
805 805 """A port of Ka-Ping Yee's cgitb.py module that outputs color text instead
806 806 of HTML. Requires inspect and pydoc. Crazy, man.
807 807
808 808 Modified version which optionally strips the topmost entries from the
809 809 traceback, to be used with alternate interpreters (because their own code
810 810 would appear in the traceback)."""
811 811
812 812 def __init__(self, color_scheme='Linux', call_pdb=False, ostream=None,
813 813 tb_offset=0, long_header=False, include_vars=True,
814 814 check_cache=None, debugger_cls = None):
815 815 """Specify traceback offset, headers and color scheme.
816 816
817 817 Define how many frames to drop from the tracebacks. Calling it with
818 818 tb_offset=1 allows use of this handler in interpreters which will have
819 819 their own code at the top of the traceback (VerboseTB will first
820 820 remove that frame before printing the traceback info)."""
821 821 TBTools.__init__(self, color_scheme=color_scheme, call_pdb=call_pdb,
822 822 ostream=ostream)
823 823 self.tb_offset = tb_offset
824 824 self.long_header = long_header
825 825 self.include_vars = include_vars
826 826 # By default we use linecache.checkcache, but the user can provide a
827 827 # different check_cache implementation. This is used by the IPython
828 828 # kernel to provide tracebacks for interactive code that is cached,
829 829 # by a compiler instance that flushes the linecache but preserves its
830 830 # own code cache.
831 831 if check_cache is None:
832 832 check_cache = linecache.checkcache
833 833 self.check_cache = check_cache
834 834
835 835 self.debugger_cls = debugger_cls or debugger.Pdb
836 836
837 837 def format_records(self, records, last_unique, recursion_repeat):
838 838 """Format the stack frames of the traceback"""
839 839 frames = []
840 840 for r in records[:last_unique+recursion_repeat+1]:
841 841 #print '*** record:',file,lnum,func,lines,index # dbg
842 842 frames.append(self.format_record(*r))
843 843
844 844 if recursion_repeat:
845 845 frames.append('... last %d frames repeated, from the frame below ...\n' % recursion_repeat)
846 846 frames.append(self.format_record(*records[last_unique+recursion_repeat+1]))
847 847
848 848 return frames
849 849
850 850 def format_record(self, frame, file, lnum, func, lines, index):
851 851 """Format a single stack frame"""
852 852 Colors = self.Colors # just a shorthand + quicker name lookup
853 853 ColorsNormal = Colors.Normal # used a lot
854 854 col_scheme = self.color_scheme_table.active_scheme_name
855 855 indent = ' ' * INDENT_SIZE
856 856 em_normal = '%s\n%s%s' % (Colors.valEm, indent, ColorsNormal)
857 857 undefined = '%sundefined%s' % (Colors.em, ColorsNormal)
858 858 tpl_link = '%s%%s%s' % (Colors.filenameEm, ColorsNormal)
859 859 tpl_call = 'in %s%%s%s%%s%s' % (Colors.vName, Colors.valEm,
860 860 ColorsNormal)
861 861 tpl_call_fail = 'in %s%%s%s(***failed resolving arguments***)%s' % \
862 862 (Colors.vName, Colors.valEm, ColorsNormal)
863 863 tpl_local_var = '%s%%s%s' % (Colors.vName, ColorsNormal)
864 864 tpl_global_var = '%sglobal%s %s%%s%s' % (Colors.em, ColorsNormal,
865 865 Colors.vName, ColorsNormal)
866 866 tpl_name_val = '%%s %s= %%s%s' % (Colors.valEm, ColorsNormal)
867 867
868 868 tpl_line = '%s%%s%s %%s' % (Colors.lineno, ColorsNormal)
869 869 tpl_line_em = '%s%%s%s %%s%s' % (Colors.linenoEm, Colors.line,
870 870 ColorsNormal)
871 871
872 872 abspath = os.path.abspath
873 873
874 874
875 875 if not file:
876 876 file = '?'
877 877 elif file.startswith(str("<")) and file.endswith(str(">")):
878 878 # Not a real filename, no problem...
879 879 pass
880 880 elif not os.path.isabs(file):
881 881 # Try to make the filename absolute by trying all
882 882 # sys.path entries (which is also what linecache does)
883 883 for dirname in sys.path:
884 884 try:
885 885 fullname = os.path.join(dirname, file)
886 886 if os.path.isfile(fullname):
887 887 file = os.path.abspath(fullname)
888 888 break
889 889 except Exception:
890 890 # Just in case that sys.path contains very
891 891 # strange entries...
892 892 pass
893 893
894 894 file = py3compat.cast_unicode(file, util_path.fs_encoding)
895 895 link = tpl_link % file
896 896 args, varargs, varkw, locals = fixed_getargvalues(frame)
897 897
898 898 if func == '?':
899 899 call = ''
900 900 else:
901 901 # Decide whether to include variable details or not
902 902 var_repr = self.include_vars and eqrepr or nullrepr
903 903 try:
904 904 call = tpl_call % (func, inspect.formatargvalues(args,
905 905 varargs, varkw,
906 906 locals, formatvalue=var_repr))
907 907 except KeyError:
908 908 # This happens in situations like errors inside generator
909 909 # expressions, where local variables are listed in the
910 910 # line, but can't be extracted from the frame. I'm not
911 911 # 100% sure this isn't actually a bug in inspect itself,
912 912 # but since there's no info for us to compute with, the
913 913 # best we can do is report the failure and move on. Here
914 914 # we must *not* call any traceback construction again,
915 915 # because that would mess up use of %debug later on. So we
916 916 # simply report the failure and move on. The only
917 917 # limitation will be that this frame won't have locals
918 918 # listed in the call signature. Quite subtle problem...
919 919 # I can't think of a good way to validate this in a unit
920 920 # test, but running a script consisting of:
921 921 # dict( (k,v.strip()) for (k,v) in range(10) )
922 922 # will illustrate the error, if this exception catch is
923 923 # disabled.
924 924 call = tpl_call_fail % func
925 925
926 926 # Don't attempt to tokenize binary files.
927 927 if file.endswith(('.so', '.pyd', '.dll')):
928 928 return '%s %s\n' % (link, call)
929 929
930 930 elif file.endswith(('.pyc', '.pyo')):
931 931 # Look up the corresponding source file.
932 932 try:
933 933 file = openpy.source_from_cache(file)
934 934 except ValueError:
935 935 # Failed to get the source file for some reason
936 936 # E.g. https://github.com/ipython/ipython/issues/9486
937 937 return '%s %s\n' % (link, call)
938 938
939 939 def linereader(file=file, lnum=[lnum], getline=ulinecache.getline):
940 940 line = getline(file, lnum[0])
941 941 lnum[0] += 1
942 942 return line
943 943
944 944 # Build the list of names on this line of code where the exception
945 945 # occurred.
946 946 try:
947 947 names = []
948 948 name_cont = False
949 949
950 950 for token_type, token, start, end, line in generate_tokens(linereader):
951 951 # build composite names
952 952 if token_type == tokenize.NAME and token not in keyword.kwlist:
953 953 if name_cont:
954 954 # Continuation of a dotted name
955 955 try:
956 956 names[-1].append(token)
957 957 except IndexError:
958 958 names.append([token])
959 959 name_cont = False
960 960 else:
961 961 # Regular new names. We append everything, the caller
962 962 # will be responsible for pruning the list later. It's
963 963 # very tricky to try to prune as we go, b/c composite
964 964 # names can fool us. The pruning at the end is easy
965 965 # to do (or the caller can print a list with repeated
966 966 # names if so desired.
967 967 names.append([token])
968 968 elif token == '.':
969 969 name_cont = True
970 970 elif token_type == tokenize.NEWLINE:
971 971 break
972 972
973 973 except (IndexError, UnicodeDecodeError, SyntaxError):
974 974 # signals exit of tokenizer
975 975 # SyntaxError can occur if the file is not actually Python
976 976 # - see gh-6300
977 977 pass
978 978 except tokenize.TokenError as msg:
979 979 _m = ("An unexpected error occurred while tokenizing input\n"
980 980 "The following traceback may be corrupted or invalid\n"
981 981 "The error message is: %s\n" % msg)
982 982 error(_m)
983 983
984 984 # Join composite names (e.g. "dict.fromkeys")
985 985 names = ['.'.join(n) for n in names]
986 986 # prune names list of duplicates, but keep the right order
987 987 unique_names = uniq_stable(names)
988 988
989 989 # Start loop over vars
990 990 lvals = []
991 991 if self.include_vars:
992 992 for name_full in unique_names:
993 993 name_base = name_full.split('.', 1)[0]
994 994 if name_base in frame.f_code.co_varnames:
995 995 if name_base in locals:
996 996 try:
997 997 value = repr(eval(name_full, locals))
998 998 except:
999 999 value = undefined
1000 1000 else:
1001 1001 value = undefined
1002 1002 name = tpl_local_var % name_full
1003 1003 else:
1004 1004 if name_base in frame.f_globals:
1005 1005 try:
1006 1006 value = repr(eval(name_full, frame.f_globals))
1007 1007 except:
1008 1008 value = undefined
1009 1009 else:
1010 1010 value = undefined
1011 1011 name = tpl_global_var % name_full
1012 1012 lvals.append(tpl_name_val % (name, value))
1013 1013 if lvals:
1014 1014 lvals = '%s%s' % (indent, em_normal.join(lvals))
1015 1015 else:
1016 1016 lvals = ''
1017 1017
1018 1018 level = '%s %s\n' % (link, call)
1019 1019
1020 1020 if index is None:
1021 1021 return level
1022 1022 else:
1023 1023 return '%s%s' % (level, ''.join(
1024 1024 _format_traceback_lines(lnum, index, lines, Colors, lvals,
1025 1025 col_scheme)))
1026 1026
1027 1027 def prepare_chained_exception_message(self, cause):
1028 1028 direct_cause = "\nThe above exception was the direct cause of the following exception:\n"
1029 1029 exception_during_handling = "\nDuring handling of the above exception, another exception occurred:\n"
1030 1030
1031 1031 if cause:
1032 1032 message = [[direct_cause]]
1033 1033 else:
1034 1034 message = [[exception_during_handling]]
1035 1035 return message
1036 1036
1037 1037 def prepare_header(self, etype, long_version=False):
1038 1038 colors = self.Colors # just a shorthand + quicker name lookup
1039 1039 colorsnormal = colors.Normal # used a lot
1040 1040 exc = '%s%s%s' % (colors.excName, etype, colorsnormal)
1041 1041 width = min(75, get_terminal_size()[0])
1042 1042 if long_version:
1043 1043 # Header with the exception type, python version, and date
1044 1044 pyver = 'Python ' + sys.version.split()[0] + ': ' + sys.executable
1045 1045 date = time.ctime(time.time())
1046 1046
1047 1047 head = '%s%s%s\n%s%s%s\n%s' % (colors.topline, '-' * width, colorsnormal,
1048 1048 exc, ' ' * (width - len(str(etype)) - len(pyver)),
1049 1049 pyver, date.rjust(width) )
1050 1050 head += "\nA problem occurred executing Python code. Here is the sequence of function" \
1051 1051 "\ncalls leading up to the error, with the most recent (innermost) call last."
1052 1052 else:
1053 1053 # Simplified header
1054 1054 head = '%s%s' % (exc, 'Traceback (most recent call last)'. \
1055 1055 rjust(width - len(str(etype))) )
1056 1056
1057 1057 return head
1058 1058
1059 1059 def format_exception(self, etype, evalue):
1060 1060 colors = self.Colors # just a shorthand + quicker name lookup
1061 1061 colorsnormal = colors.Normal # used a lot
1062 1062 indent = ' ' * INDENT_SIZE
1063 1063 # Get (safely) a string form of the exception info
1064 1064 try:
1065 1065 etype_str, evalue_str = map(str, (etype, evalue))
1066 1066 except:
1067 1067 # User exception is improperly defined.
1068 1068 etype, evalue = str, sys.exc_info()[:2]
1069 1069 etype_str, evalue_str = map(str, (etype, evalue))
1070 1070 # ... and format it
1071 1071 exception = ['%s%s%s: %s' % (colors.excName, etype_str,
1072 1072 colorsnormal, py3compat.cast_unicode(evalue_str))]
1073 1073
1074 1074 if (not py3compat.PY3) and type(evalue) is types.InstanceType:
1075 1075 try:
1076 1076 names = [w for w in dir(evalue) if isinstance(w, py3compat.string_types)]
1077 1077 except:
1078 1078 # Every now and then, an object with funny internals blows up
1079 1079 # when dir() is called on it. We do the best we can to report
1080 1080 # the problem and continue
1081 1081 _m = '%sException reporting error (object with broken dir())%s:'
1082 1082 exception.append(_m % (colors.excName, colorsnormal))
1083 1083 etype_str, evalue_str = map(str, sys.exc_info()[:2])
1084 1084 exception.append('%s%s%s: %s' % (colors.excName, etype_str,
1085 1085 colorsnormal, py3compat.cast_unicode(evalue_str)))
1086 1086 names = []
1087 1087 for name in names:
1088 1088 value = text_repr(getattr(evalue, name))
1089 1089 exception.append('\n%s%s = %s' % (indent, name, value))
1090 1090
1091 1091 return exception
1092 1092
1093 1093 def format_exception_as_a_whole(self, etype, evalue, etb, number_of_lines_of_context, tb_offset):
1094 1094 """Formats the header, traceback and exception message for a single exception.
1095 1095
1096 1096 This may be called multiple times by Python 3 exception chaining
1097 1097 (PEP 3134).
1098 1098 """
1099 1099 # some locals
1100 1100 orig_etype = etype
1101 1101 try:
1102 1102 etype = etype.__name__
1103 1103 except AttributeError:
1104 1104 pass
1105 1105
1106 1106 tb_offset = self.tb_offset if tb_offset is None else tb_offset
1107 1107 head = self.prepare_header(etype, self.long_header)
1108 1108 records = self.get_records(etb, number_of_lines_of_context, tb_offset)
1109 1109
1110 1110 if records is None:
1111 1111 return ""
1112 1112
1113 1113 last_unique, recursion_repeat = find_recursion(orig_etype, evalue, records)
1114 1114
1115 1115 frames = self.format_records(records, last_unique, recursion_repeat)
1116 1116
1117 1117 formatted_exception = self.format_exception(etype, evalue)
1118 1118 if records:
1119 1119 filepath, lnum = records[-1][1:3]
1120 1120 filepath = os.path.abspath(filepath)
1121 1121 ipinst = get_ipython()
1122 1122 if ipinst is not None:
1123 1123 ipinst.hooks.synchronize_with_editor(filepath, lnum, 0)
1124 1124
1125 1125 return [[head] + frames + [''.join(formatted_exception[0])]]
1126 1126
1127 1127 def get_records(self, etb, number_of_lines_of_context, tb_offset):
1128 1128 try:
1129 1129 # Try the default getinnerframes and Alex's: Alex's fixes some
1130 1130 # problems, but it generates empty tracebacks for console errors
1131 1131 # (5 blanks lines) where none should be returned.
1132 1132 return _fixed_getinnerframes(etb, number_of_lines_of_context, tb_offset)
1133 1133 except:
1134 1134 # FIXME: I've been getting many crash reports from python 2.3
1135 1135 # users, traceable to inspect.py. If I can find a small test-case
1136 1136 # to reproduce this, I should either write a better workaround or
1137 1137 # file a bug report against inspect (if that's the real problem).
1138 1138 # So far, I haven't been able to find an isolated example to
1139 1139 # reproduce the problem.
1140 1140 inspect_error()
1141 1141 traceback.print_exc(file=self.ostream)
1142 1142 info('\nUnfortunately, your original traceback can not be constructed.\n')
1143 1143 return None
1144 1144
1145 1145 def get_parts_of_chained_exception(self, evalue):
1146 1146 def get_chained_exception(exception_value):
1147 1147 cause = getattr(exception_value, '__cause__', None)
1148 1148 if cause:
1149 1149 return cause
1150 1150 if getattr(exception_value, '__suppress_context__', False):
1151 1151 return None
1152 1152 return getattr(exception_value, '__context__', None)
1153 1153
1154 1154 chained_evalue = get_chained_exception(evalue)
1155 1155
1156 1156 if chained_evalue:
1157 1157 return chained_evalue.__class__, chained_evalue, chained_evalue.__traceback__
1158 1158
1159 1159 def structured_traceback(self, etype, evalue, etb, tb_offset=None,
1160 1160 number_of_lines_of_context=5):
1161 1161 """Return a nice text document describing the traceback."""
1162 1162
1163 1163 formatted_exception = self.format_exception_as_a_whole(etype, evalue, etb, number_of_lines_of_context,
1164 1164 tb_offset)
1165 1165
1166 1166 colors = self.Colors # just a shorthand + quicker name lookup
1167 1167 colorsnormal = colors.Normal # used a lot
1168 1168 head = '%s%s%s' % (colors.topline, '-' * min(75, get_terminal_size()[0]), colorsnormal)
1169 1169 structured_traceback_parts = [head]
1170 1170 if py3compat.PY3:
1171 1171 chained_exceptions_tb_offset = 0
1172 1172 lines_of_context = 3
1173 1173 formatted_exceptions = formatted_exception
1174 1174 exception = self.get_parts_of_chained_exception(evalue)
1175 1175 if exception:
1176 1176 formatted_exceptions += self.prepare_chained_exception_message(evalue.__cause__)
1177 1177 etype, evalue, etb = exception
1178 1178 else:
1179 1179 evalue = None
1180 1180 chained_exc_ids = set()
1181 1181 while evalue:
1182 1182 formatted_exceptions += self.format_exception_as_a_whole(etype, evalue, etb, lines_of_context,
1183 1183 chained_exceptions_tb_offset)
1184 1184 exception = self.get_parts_of_chained_exception(evalue)
1185 1185
1186 1186 if exception and not id(exception[1]) in chained_exc_ids:
1187 1187 chained_exc_ids.add(id(exception[1])) # trace exception to avoid infinite 'cause' loop
1188 1188 formatted_exceptions += self.prepare_chained_exception_message(evalue.__cause__)
1189 1189 etype, evalue, etb = exception
1190 1190 else:
1191 1191 evalue = None
1192 1192
1193 1193 # we want to see exceptions in a reversed order:
1194 1194 # the first exception should be on top
1195 1195 for formatted_exception in reversed(formatted_exceptions):
1196 1196 structured_traceback_parts += formatted_exception
1197 1197 else:
1198 1198 structured_traceback_parts += formatted_exception[0]
1199 1199
1200 1200 return structured_traceback_parts
1201 1201
1202 1202 def debugger(self, force=False):
1203 1203 """Call up the pdb debugger if desired, always clean up the tb
1204 1204 reference.
1205 1205
1206 1206 Keywords:
1207 1207
1208 1208 - force(False): by default, this routine checks the instance call_pdb
1209 1209 flag and does not actually invoke the debugger if the flag is false.
1210 1210 The 'force' option forces the debugger to activate even if the flag
1211 1211 is false.
1212 1212
1213 1213 If the call_pdb flag is set, the pdb interactive debugger is
1214 1214 invoked. In all cases, the self.tb reference to the current traceback
1215 1215 is deleted to prevent lingering references which hamper memory
1216 1216 management.
1217 1217
1218 1218 Note that each call to pdb() does an 'import readline', so if your app
1219 1219 requires a special setup for the readline completers, you'll have to
1220 1220 fix that by hand after invoking the exception handler."""
1221 1221
1222 1222 if force or self.call_pdb:
1223 1223 if self.pdb is None:
1224 1224 self.pdb = self.debugger_cls(
1225 1225 self.color_scheme_table.active_scheme_name)
1226 1226 # the system displayhook may have changed, restore the original
1227 1227 # for pdb
1228 1228 display_trap = DisplayTrap(hook=sys.__displayhook__)
1229 1229 with display_trap:
1230 1230 self.pdb.reset()
1231 1231 # Find the right frame so we don't pop up inside ipython itself
1232 1232 if hasattr(self, 'tb') and self.tb is not None:
1233 1233 etb = self.tb
1234 1234 else:
1235 1235 etb = self.tb = sys.last_traceback
1236 1236 while self.tb is not None and self.tb.tb_next is not None:
1237 1237 self.tb = self.tb.tb_next
1238 1238 if etb and etb.tb_next:
1239 1239 etb = etb.tb_next
1240 1240 self.pdb.botframe = etb.tb_frame
1241 1241 self.pdb.interaction(self.tb.tb_frame, self.tb)
1242 1242
1243 1243 if hasattr(self, 'tb'):
1244 1244 del self.tb
1245 1245
1246 1246 def handler(self, info=None):
1247 1247 (etype, evalue, etb) = info or sys.exc_info()
1248 1248 self.tb = etb
1249 1249 ostream = self.ostream
1250 1250 ostream.flush()
1251 1251 ostream.write(self.text(etype, evalue, etb))
1252 1252 ostream.write('\n')
1253 1253 ostream.flush()
1254 1254
1255 1255 # Changed so an instance can just be called as VerboseTB_inst() and print
1256 1256 # out the right info on its own.
1257 1257 def __call__(self, etype=None, evalue=None, etb=None):
1258 1258 """This hook can replace sys.excepthook (for Python 2.1 or higher)."""
1259 1259 if etb is None:
1260 1260 self.handler()
1261 1261 else:
1262 1262 self.handler((etype, evalue, etb))
1263 1263 try:
1264 1264 self.debugger()
1265 1265 except KeyboardInterrupt:
1266 1266 print("\nKeyboardInterrupt")
1267 1267
1268 1268
1269 1269 #----------------------------------------------------------------------------
1270 1270 class FormattedTB(VerboseTB, ListTB):
1271 1271 """Subclass ListTB but allow calling with a traceback.
1272 1272
1273 1273 It can thus be used as a sys.excepthook for Python > 2.1.
1274 1274
1275 1275 Also adds 'Context' and 'Verbose' modes, not available in ListTB.
1276 1276
1277 1277 Allows a tb_offset to be specified. This is useful for situations where
1278 1278 one needs to remove a number of topmost frames from the traceback (such as
1279 1279 occurs with python programs that themselves execute other python code,
1280 1280 like Python shells). """
1281 1281
1282 1282 def __init__(self, mode='Plain', color_scheme='Linux', call_pdb=False,
1283 1283 ostream=None,
1284 1284 tb_offset=0, long_header=False, include_vars=False,
1285 1285 check_cache=None, debugger_cls=None):
1286 1286
1287 1287 # NEVER change the order of this list. Put new modes at the end:
1288 1288 self.valid_modes = ['Plain', 'Context', 'Verbose']
1289 1289 self.verbose_modes = self.valid_modes[1:3]
1290 1290
1291 1291 VerboseTB.__init__(self, color_scheme=color_scheme, call_pdb=call_pdb,
1292 1292 ostream=ostream, tb_offset=tb_offset,
1293 1293 long_header=long_header, include_vars=include_vars,
1294 1294 check_cache=check_cache, debugger_cls=debugger_cls)
1295 1295
1296 1296 # Different types of tracebacks are joined with different separators to
1297 1297 # form a single string. They are taken from this dict
1298 1298 self._join_chars = dict(Plain='', Context='\n', Verbose='\n')
1299 1299 # set_mode also sets the tb_join_char attribute
1300 1300 self.set_mode(mode)
1301 1301
1302 1302 def _extract_tb(self, tb):
1303 1303 if tb:
1304 1304 return traceback.extract_tb(tb)
1305 1305 else:
1306 1306 return None
1307 1307
1308 1308 def structured_traceback(self, etype, value, tb, tb_offset=None, number_of_lines_of_context=5):
1309 1309 tb_offset = self.tb_offset if tb_offset is None else tb_offset
1310 1310 mode = self.mode
1311 1311 if mode in self.verbose_modes:
1312 1312 # Verbose modes need a full traceback
1313 1313 return VerboseTB.structured_traceback(
1314 1314 self, etype, value, tb, tb_offset, number_of_lines_of_context
1315 1315 )
1316 1316 else:
1317 1317 # We must check the source cache because otherwise we can print
1318 1318 # out-of-date source code.
1319 1319 self.check_cache()
1320 1320 # Now we can extract and format the exception
1321 1321 elist = self._extract_tb(tb)
1322 1322 return ListTB.structured_traceback(
1323 1323 self, etype, value, elist, tb_offset, number_of_lines_of_context
1324 1324 )
1325 1325
1326 1326 def stb2text(self, stb):
1327 1327 """Convert a structured traceback (a list) to a string."""
1328 1328 return self.tb_join_char.join(stb)
1329 1329
1330 1330
1331 1331 def set_mode(self, mode=None):
1332 1332 """Switch to the desired mode.
1333 1333
1334 1334 If mode is not specified, cycles through the available modes."""
1335 1335
1336 1336 if not mode:
1337 1337 new_idx = (self.valid_modes.index(self.mode) + 1 ) % \
1338 1338 len(self.valid_modes)
1339 1339 self.mode = self.valid_modes[new_idx]
1340 1340 elif mode not in self.valid_modes:
1341 1341 raise ValueError('Unrecognized mode in FormattedTB: <' + mode + '>\n'
1342 1342 'Valid modes: ' + str(self.valid_modes))
1343 1343 else:
1344 1344 self.mode = mode
1345 1345 # include variable details only in 'Verbose' mode
1346 1346 self.include_vars = (self.mode == self.valid_modes[2])
1347 1347 # Set the join character for generating text tracebacks
1348 1348 self.tb_join_char = self._join_chars[self.mode]
1349 1349
1350 1350 # some convenient shortcuts
1351 1351 def plain(self):
1352 1352 self.set_mode(self.valid_modes[0])
1353 1353
1354 1354 def context(self):
1355 1355 self.set_mode(self.valid_modes[1])
1356 1356
1357 1357 def verbose(self):
1358 1358 self.set_mode(self.valid_modes[2])
1359 1359
1360 1360
1361 1361 #----------------------------------------------------------------------------
1362 1362 class AutoFormattedTB(FormattedTB):
1363 1363 """A traceback printer which can be called on the fly.
1364 1364
1365 1365 It will find out about exceptions by itself.
1366 1366
1367 1367 A brief example::
1368 1368
1369 1369 AutoTB = AutoFormattedTB(mode = 'Verbose',color_scheme='Linux')
1370 1370 try:
1371 1371 ...
1372 1372 except:
1373 1373 AutoTB() # or AutoTB(out=logfile) where logfile is an open file object
1374 1374 """
1375 1375
1376 1376 def __call__(self, etype=None, evalue=None, etb=None,
1377 1377 out=None, tb_offset=None):
1378 1378 """Print out a formatted exception traceback.
1379 1379
1380 1380 Optional arguments:
1381 1381 - out: an open file-like object to direct output to.
1382 1382
1383 1383 - tb_offset: the number of frames to skip over in the stack, on a
1384 1384 per-call basis (this overrides temporarily the instance's tb_offset
1385 1385 given at initialization time. """
1386 1386
1387 1387 if out is None:
1388 1388 out = self.ostream
1389 1389 out.flush()
1390 1390 out.write(self.text(etype, evalue, etb, tb_offset))
1391 1391 out.write('\n')
1392 1392 out.flush()
1393 1393 # FIXME: we should remove the auto pdb behavior from here and leave
1394 1394 # that to the clients.
1395 1395 try:
1396 1396 self.debugger()
1397 1397 except KeyboardInterrupt:
1398 1398 print("\nKeyboardInterrupt")
1399 1399
1400 1400 def structured_traceback(self, etype=None, value=None, tb=None,
1401 1401 tb_offset=None, number_of_lines_of_context=5):
1402 1402 if etype is None:
1403 1403 etype, value, tb = sys.exc_info()
1404 1404 self.tb = tb
1405 1405 return FormattedTB.structured_traceback(
1406 1406 self, etype, value, tb, tb_offset, number_of_lines_of_context)
1407 1407
1408 1408
1409 1409 #---------------------------------------------------------------------------
1410 1410
1411 1411 # A simple class to preserve Nathan's original functionality.
1412 1412 class ColorTB(FormattedTB):
1413 1413 """Shorthand to initialize a FormattedTB in Linux colors mode."""
1414 1414
1415 1415 def __init__(self, color_scheme='Linux', call_pdb=0, **kwargs):
1416 1416 FormattedTB.__init__(self, color_scheme=color_scheme,
1417 1417 call_pdb=call_pdb, **kwargs)
1418 1418
1419 1419
1420 1420 class SyntaxTB(ListTB):
1421 1421 """Extension which holds some state: the last exception value"""
1422 1422
1423 1423 def __init__(self, color_scheme='NoColor'):
1424 1424 ListTB.__init__(self, color_scheme)
1425 1425 self.last_syntax_error = None
1426 1426
1427 1427 def __call__(self, etype, value, elist):
1428 1428 self.last_syntax_error = value
1429 1429
1430 1430 ListTB.__call__(self, etype, value, elist)
1431 1431
1432 1432 def structured_traceback(self, etype, value, elist, tb_offset=None,
1433 1433 context=5):
1434 1434 # If the source file has been edited, the line in the syntax error can
1435 1435 # be wrong (retrieved from an outdated cache). This replaces it with
1436 1436 # the current value.
1437 1437 if isinstance(value, SyntaxError) \
1438 1438 and isinstance(value.filename, py3compat.string_types) \
1439 1439 and isinstance(value.lineno, int):
1440 1440 linecache.checkcache(value.filename)
1441 1441 newtext = ulinecache.getline(value.filename, value.lineno)
1442 1442 if newtext:
1443 1443 value.text = newtext
1444 1444 self.last_syntax_error = value
1445 1445 return super(SyntaxTB, self).structured_traceback(etype, value, elist,
1446 1446 tb_offset=tb_offset, context=context)
1447 1447
1448 1448 def clear_err_state(self):
1449 1449 """Return the current error state and clear it"""
1450 1450 e = self.last_syntax_error
1451 1451 self.last_syntax_error = None
1452 1452 return e
1453 1453
1454 1454 def stb2text(self, stb):
1455 1455 """Convert a structured traceback (a list) to a string."""
1456 1456 return ''.join(stb)
1457 1457
1458 1458
1459 1459 # some internal-use functions
1460 1460 def text_repr(value):
1461 1461 """Hopefully pretty robust repr equivalent."""
1462 1462 # this is pretty horrible but should always return *something*
1463 1463 try:
1464 1464 return pydoc.text.repr(value)
1465 1465 except KeyboardInterrupt:
1466 1466 raise
1467 1467 except:
1468 1468 try:
1469 1469 return repr(value)
1470 1470 except KeyboardInterrupt:
1471 1471 raise
1472 1472 except:
1473 1473 try:
1474 1474 # all still in an except block so we catch
1475 1475 # getattr raising
1476 1476 name = getattr(value, '__name__', None)
1477 1477 if name:
1478 1478 # ick, recursion
1479 1479 return text_repr(name)
1480 1480 klass = getattr(value, '__class__', None)
1481 1481 if klass:
1482 1482 return '%s instance' % text_repr(klass)
1483 1483 except KeyboardInterrupt:
1484 1484 raise
1485 1485 except:
1486 1486 return 'UNRECOVERABLE REPR FAILURE'
1487 1487
1488 1488
1489 1489 def eqrepr(value, repr=text_repr):
1490 1490 return '=%s' % repr(value)
1491 1491
1492 1492
1493 1493 def nullrepr(value, repr=text_repr):
1494 1494 return ''
@@ -1,583 +1,583 b''
1 1 """Module for interactive demos using IPython.
2 2
3 3 This module implements a few classes for running Python scripts interactively
4 4 in IPython for demonstrations. With very simple markup (a few tags in
5 5 comments), you can control points where the script stops executing and returns
6 6 control to IPython.
7 7
8 8
9 9 Provided classes
10 10 ----------------
11 11
12 12 The classes are (see their docstrings for further details):
13 13
14 14 - Demo: pure python demos
15 15
16 16 - IPythonDemo: demos with input to be processed by IPython as if it had been
17 17 typed interactively (so magics work, as well as any other special syntax you
18 18 may have added via input prefilters).
19 19
20 20 - LineDemo: single-line version of the Demo class. These demos are executed
21 21 one line at a time, and require no markup.
22 22
23 23 - IPythonLineDemo: IPython version of the LineDemo class (the demo is
24 24 executed a line at a time, but processed via IPython).
25 25
26 26 - ClearMixin: mixin to make Demo classes with less visual clutter. It
27 27 declares an empty marquee and a pre_cmd that clears the screen before each
28 28 block (see Subclassing below).
29 29
30 30 - ClearDemo, ClearIPDemo: mixin-enabled versions of the Demo and IPythonDemo
31 31 classes.
32 32
33 33 Inheritance diagram:
34 34
35 35 .. inheritance-diagram:: IPython.lib.demo
36 36 :parts: 3
37 37
38 38 Subclassing
39 39 -----------
40 40
41 41 The classes here all include a few methods meant to make customization by
42 42 subclassing more convenient. Their docstrings below have some more details:
43 43
44 44 - marquee(): generates a marquee to provide visible on-screen markers at each
45 45 block start and end.
46 46
47 47 - pre_cmd(): run right before the execution of each block.
48 48
49 49 - post_cmd(): run right after the execution of each block. If the block
50 50 raises an exception, this is NOT called.
51 51
52 52
53 53 Operation
54 54 ---------
55 55
56 56 The file is run in its own empty namespace (though you can pass it a string of
57 57 arguments as if in a command line environment, and it will see those as
58 58 sys.argv). But at each stop, the global IPython namespace is updated with the
59 59 current internal demo namespace, so you can work interactively with the data
60 60 accumulated so far.
61 61
62 62 By default, each block of code is printed (with syntax highlighting) before
63 63 executing it and you have to confirm execution. This is intended to show the
64 64 code to an audience first so you can discuss it, and only proceed with
65 65 execution once you agree. There are a few tags which allow you to modify this
66 66 behavior.
67 67
68 68 The supported tags are:
69 69
70 70 # <demo> stop
71 71
72 72 Defines block boundaries, the points where IPython stops execution of the
73 73 file and returns to the interactive prompt.
74 74
75 75 You can optionally mark the stop tag with extra dashes before and after the
76 76 word 'stop', to help visually distinguish the blocks in a text editor:
77 77
78 78 # <demo> --- stop ---
79 79
80 80
81 81 # <demo> silent
82 82
83 83 Make a block execute silently (and hence automatically). Typically used in
84 84 cases where you have some boilerplate or initialization code which you need
85 85 executed but do not want to be seen in the demo.
86 86
87 87 # <demo> auto
88 88
89 89 Make a block execute automatically, but still being printed. Useful for
90 90 simple code which does not warrant discussion, since it avoids the extra
91 91 manual confirmation.
92 92
93 93 # <demo> auto_all
94 94
95 95 This tag can _only_ be in the first block, and if given it overrides the
96 96 individual auto tags to make the whole demo fully automatic (no block asks
97 97 for confirmation). It can also be given at creation time (or the attribute
98 98 set later) to override what's in the file.
99 99
100 100 While _any_ python file can be run as a Demo instance, if there are no stop
101 101 tags the whole file will run in a single block (no different that calling
102 102 first %pycat and then %run). The minimal markup to make this useful is to
103 103 place a set of stop tags; the other tags are only there to let you fine-tune
104 104 the execution.
105 105
106 106 This is probably best explained with the simple example file below. You can
107 107 copy this into a file named ex_demo.py, and try running it via::
108 108
109 109 from IPython.demo import Demo
110 110 d = Demo('ex_demo.py')
111 111 d()
112 112
113 113 Each time you call the demo object, it runs the next block. The demo object
114 114 has a few useful methods for navigation, like again(), edit(), jump(), seek()
115 115 and back(). It can be reset for a new run via reset() or reloaded from disk
116 116 (in case you've edited the source) via reload(). See their docstrings below.
117 117
118 118 Note: To make this simpler to explore, a file called "demo-exercizer.py" has
119 119 been added to the "docs/examples/core" directory. Just cd to this directory in
120 120 an IPython session, and type::
121 121
122 122 %run demo-exercizer.py
123 123
124 124 and then follow the directions.
125 125
126 126 Example
127 127 -------
128 128
129 129 The following is a very simple example of a valid demo file.
130 130
131 131 ::
132 132
133 133 #################### EXAMPLE DEMO <ex_demo.py> ###############################
134 134 '''A simple interactive demo to illustrate the use of IPython's Demo class.'''
135 135
136 136 print 'Hello, welcome to an interactive IPython demo.'
137 137
138 138 # The mark below defines a block boundary, which is a point where IPython will
139 139 # stop execution and return to the interactive prompt. The dashes are actually
140 140 # optional and used only as a visual aid to clearly separate blocks while
141 141 # editing the demo code.
142 142 # <demo> stop
143 143
144 144 x = 1
145 145 y = 2
146 146
147 147 # <demo> stop
148 148
149 149 # the mark below makes this block as silent
150 150 # <demo> silent
151 151
152 152 print 'This is a silent block, which gets executed but not printed.'
153 153
154 154 # <demo> stop
155 155 # <demo> auto
156 156 print 'This is an automatic block.'
157 157 print 'It is executed without asking for confirmation, but printed.'
158 158 z = x+y
159 159
160 160 print 'z=',x
161 161
162 162 # <demo> stop
163 163 # This is just another normal block.
164 164 print 'z is now:', z
165 165
166 166 print 'bye!'
167 167 ################### END EXAMPLE DEMO <ex_demo.py> ############################
168 168 """
169 169
170 170 from __future__ import unicode_literals
171 171
172 172 #*****************************************************************************
173 173 # Copyright (C) 2005-2006 Fernando Perez. <Fernando.Perez@colorado.edu>
174 174 #
175 175 # Distributed under the terms of the BSD License. The full license is in
176 176 # the file COPYING, distributed as part of this software.
177 177 #
178 178 #*****************************************************************************
179 179 from __future__ import print_function
180 180
181 181 import os
182 182 import re
183 183 import shlex
184 184 import sys
185 185
186 186 from IPython.utils import io
187 187 from IPython.utils.text import marquee
188 188 from IPython.utils import openpy
189 189 from IPython.utils import py3compat
190 190 __all__ = ['Demo','IPythonDemo','LineDemo','IPythonLineDemo','DemoError']
191 191
192 192 class DemoError(Exception): pass
193 193
194 194 def re_mark(mark):
195 195 return re.compile(r'^\s*#\s+<demo>\s+%s\s*$' % mark,re.MULTILINE)
196 196
197 197 class Demo(object):
198 198
199 199 re_stop = re_mark('-*\s?stop\s?-*')
200 200 re_silent = re_mark('silent')
201 201 re_auto = re_mark('auto')
202 202 re_auto_all = re_mark('auto_all')
203 203
204 204 def __init__(self,src,title='',arg_str='',auto_all=None):
205 205 """Make a new demo object. To run the demo, simply call the object.
206 206
207 207 See the module docstring for full details and an example (you can use
208 208 IPython.Demo? in IPython to see it).
209 209
210 210 Inputs:
211 211
212 212 - src is either a file, or file-like object, or a
213 213 string that can be resolved to a filename.
214 214
215 215 Optional inputs:
216 216
217 217 - title: a string to use as the demo name. Of most use when the demo
218 218 you are making comes from an object that has no filename, or if you
219 219 want an alternate denotation distinct from the filename.
220 220
221 221 - arg_str(''): a string of arguments, internally converted to a list
222 222 just like sys.argv, so the demo script can see a similar
223 223 environment.
224 224
225 225 - auto_all(None): global flag to run all blocks automatically without
226 226 confirmation. This attribute overrides the block-level tags and
227 227 applies to the whole demo. It is an attribute of the object, and
228 228 can be changed at runtime simply by reassigning it to a boolean
229 229 value.
230 230 """
231 231 if hasattr(src, "read"):
232 232 # It seems to be a file or a file-like object
233 233 self.fname = "from a file-like object"
234 234 if title == '':
235 235 self.title = "from a file-like object"
236 236 else:
237 237 self.title = title
238 238 else:
239 239 # Assume it's a string or something that can be converted to one
240 240 self.fname = src
241 241 if title == '':
242 242 (filepath, filename) = os.path.split(src)
243 243 self.title = filename
244 244 else:
245 245 self.title = title
246 246 self.sys_argv = [src] + shlex.split(arg_str)
247 247 self.auto_all = auto_all
248 248 self.src = src
249 249
250 250 # get a few things from ipython. While it's a bit ugly design-wise,
251 251 # it ensures that things like color scheme and the like are always in
252 252 # sync with the ipython mode being used. This class is only meant to
253 253 # be used inside ipython anyways, so it's OK.
254 254 ip = get_ipython() # this is in builtins whenever IPython is running
255 255 self.ip_ns = ip.user_ns
256 256 self.ip_colorize = ip.pycolorize
257 257 self.ip_showtb = ip.showtraceback
258 258 self.ip_run_cell = ip.run_cell
259 259 self.shell = ip
260 260
261 261 # load user data and initialize data structures
262 262 self.reload()
263 263
264 264 def fload(self):
265 265 """Load file object."""
266 266 # read data and parse into blocks
267 267 if hasattr(self, 'fobj') and self.fobj is not None:
268 268 self.fobj.close()
269 269 if hasattr(self.src, "read"):
270 270 # It seems to be a file or a file-like object
271 271 self.fobj = self.src
272 272 else:
273 273 # Assume it's a string or something that can be converted to one
274 274 self.fobj = openpy.open(self.fname)
275 275
276 276 def reload(self):
277 277 """Reload source from disk and initialize state."""
278 278 self.fload()
279 279
280 280 self.src = "".join(openpy.strip_encoding_cookie(self.fobj))
281 281 src_b = [b.strip() for b in self.re_stop.split(self.src) if b]
282 282 self._silent = [bool(self.re_silent.findall(b)) for b in src_b]
283 283 self._auto = [bool(self.re_auto.findall(b)) for b in src_b]
284 284
285 285 # if auto_all is not given (def. None), we read it from the file
286 286 if self.auto_all is None:
287 287 self.auto_all = bool(self.re_auto_all.findall(src_b[0]))
288 288 else:
289 289 self.auto_all = bool(self.auto_all)
290 290
291 291 # Clean the sources from all markup so it doesn't get displayed when
292 292 # running the demo
293 293 src_blocks = []
294 294 auto_strip = lambda s: self.re_auto.sub('',s)
295 295 for i,b in enumerate(src_b):
296 296 if self._auto[i]:
297 297 src_blocks.append(auto_strip(b))
298 298 else:
299 299 src_blocks.append(b)
300 300 # remove the auto_all marker
301 301 src_blocks[0] = self.re_auto_all.sub('',src_blocks[0])
302 302
303 303 self.nblocks = len(src_blocks)
304 304 self.src_blocks = src_blocks
305 305
306 306 # also build syntax-highlighted source
307 307 self.src_blocks_colored = list(map(self.ip_colorize,self.src_blocks))
308 308
309 309 # ensure clean namespace and seek offset
310 310 self.reset()
311 311
312 312 def reset(self):
313 313 """Reset the namespace and seek pointer to restart the demo"""
314 314 self.user_ns = {}
315 315 self.finished = False
316 316 self.block_index = 0
317 317
318 318 def _validate_index(self,index):
319 319 if index<0 or index>=self.nblocks:
320 320 raise ValueError('invalid block index %s' % index)
321 321
322 322 def _get_index(self,index):
323 323 """Get the current block index, validating and checking status.
324 324
325 325 Returns None if the demo is finished"""
326 326
327 327 if index is None:
328 328 if self.finished:
329 329 print('Demo finished. Use <demo_name>.reset() if you want to rerun it.')
330 330 return None
331 331 index = self.block_index
332 332 else:
333 333 self._validate_index(index)
334 334 return index
335 335
336 336 def seek(self,index):
337 337 """Move the current seek pointer to the given block.
338 338
339 339 You can use negative indices to seek from the end, with identical
340 340 semantics to those of Python lists."""
341 341 if index<0:
342 342 index = self.nblocks + index
343 343 self._validate_index(index)
344 344 self.block_index = index
345 345 self.finished = False
346 346
347 347 def back(self,num=1):
348 348 """Move the seek pointer back num blocks (default is 1)."""
349 349 self.seek(self.block_index-num)
350 350
351 351 def jump(self,num=1):
352 352 """Jump a given number of blocks relative to the current one.
353 353
354 354 The offset can be positive or negative, defaults to 1."""
355 355 self.seek(self.block_index+num)
356 356
357 357 def again(self):
358 358 """Move the seek pointer back one block and re-execute."""
359 359 self.back(1)
360 360 self()
361 361
362 362 def edit(self,index=None):
363 363 """Edit a block.
364 364
365 365 If no number is given, use the last block executed.
366 366
367 367 This edits the in-memory copy of the demo, it does NOT modify the
368 368 original source file. If you want to do that, simply open the file in
369 369 an editor and use reload() when you make changes to the file. This
370 370 method is meant to let you change a block during a demonstration for
371 371 explanatory purposes, without damaging your original script."""
372 372
373 373 index = self._get_index(index)
374 374 if index is None:
375 375 return
376 376 # decrease the index by one (unless we're at the very beginning), so
377 377 # that the default demo.edit() call opens up the sblock we've last run
378 378 if index>0:
379 379 index -= 1
380 380
381 381 filename = self.shell.mktempfile(self.src_blocks[index])
382 382 self.shell.hooks.editor(filename,1)
383 383 with open(filename, 'r') as f:
384 384 new_block = f.read()
385 385 # update the source and colored block
386 386 self.src_blocks[index] = new_block
387 387 self.src_blocks_colored[index] = self.ip_colorize(new_block)
388 388 self.block_index = index
389 389 # call to run with the newly edited index
390 390 self()
391 391
392 392 def show(self,index=None):
393 393 """Show a single block on screen"""
394 394
395 395 index = self._get_index(index)
396 396 if index is None:
397 397 return
398 398
399 399 print(self.marquee('<%s> block # %s (%s remaining)' %
400 400 (self.title,index,self.nblocks-index-1)))
401 401 print(self.src_blocks_colored[index])
402 402 sys.stdout.flush()
403 403
404 404 def show_all(self):
405 405 """Show entire demo on screen, block by block"""
406 406
407 407 fname = self.title
408 408 title = self.title
409 409 nblocks = self.nblocks
410 410 silent = self._silent
411 411 marquee = self.marquee
412 412 for index,block in enumerate(self.src_blocks_colored):
413 413 if silent[index]:
414 414 print(marquee('<%s> SILENT block # %s (%s remaining)' %
415 415 (title,index,nblocks-index-1)))
416 416 else:
417 417 print(marquee('<%s> block # %s (%s remaining)' %
418 418 (title,index,nblocks-index-1)))
419 419 print(block, end=' ')
420 420 sys.stdout.flush()
421 421
422 422 def run_cell(self,source):
423 423 """Execute a string with one or more lines of code"""
424 424
425 425 exec(source, self.user_ns)
426 426
427 427 def __call__(self,index=None):
428 428 """run a block of the demo.
429 429
430 430 If index is given, it should be an integer >=1 and <= nblocks. This
431 431 means that the calling convention is one off from typical Python
432 432 lists. The reason for the inconsistency is that the demo always
433 433 prints 'Block n/N, and N is the total, so it would be very odd to use
434 434 zero-indexing here."""
435 435
436 436 index = self._get_index(index)
437 437 if index is None:
438 438 return
439 439 try:
440 440 marquee = self.marquee
441 441 next_block = self.src_blocks[index]
442 442 self.block_index += 1
443 443 if self._silent[index]:
444 444 print(marquee('Executing silent block # %s (%s remaining)' %
445 445 (index,self.nblocks-index-1)))
446 446 else:
447 447 self.pre_cmd()
448 448 self.show(index)
449 449 if self.auto_all or self._auto[index]:
450 450 print(marquee('output:'))
451 451 else:
452 452 print(marquee('Press <q> to quit, <Enter> to execute...'), end=' ')
453 453 ans = py3compat.input().strip()
454 454 if ans:
455 455 print(marquee('Block NOT executed'))
456 456 return
457 457 try:
458 458 save_argv = sys.argv
459 459 sys.argv = self.sys_argv
460 460 self.run_cell(next_block)
461 461 self.post_cmd()
462 462 finally:
463 463 sys.argv = save_argv
464 464
465 465 except:
466 466 self.ip_showtb(filename=self.fname)
467 467 else:
468 468 self.ip_ns.update(self.user_ns)
469 469
470 470 if self.block_index == self.nblocks:
471 471 mq1 = self.marquee('END OF DEMO')
472 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 474 print()
475 475 print(mq1)
476 476 print(self.marquee('Use <demo_name>.reset() if you want to rerun it.'))
477 477 self.finished = True
478 478
479 479 # These methods are meant to be overridden by subclasses who may wish to
480 480 # customize the behavior of of their demos.
481 481 def marquee(self,txt='',width=78,mark='*'):
482 482 """Return the input string centered in a 'marquee'."""
483 483 return marquee(txt,width,mark)
484 484
485 485 def pre_cmd(self):
486 486 """Method called before executing each block."""
487 487 pass
488 488
489 489 def post_cmd(self):
490 490 """Method called after executing each block."""
491 491 pass
492 492
493 493
494 494 class IPythonDemo(Demo):
495 495 """Class for interactive demos with IPython's input processing applied.
496 496
497 497 This subclasses Demo, but instead of executing each block by the Python
498 498 interpreter (via exec), it actually calls IPython on it, so that any input
499 499 filters which may be in place are applied to the input block.
500 500
501 501 If you have an interactive environment which exposes special input
502 502 processing, you can use this class instead to write demo scripts which
503 503 operate exactly as if you had typed them interactively. The default Demo
504 504 class requires the input to be valid, pure Python code.
505 505 """
506 506
507 507 def run_cell(self,source):
508 508 """Execute a string with one or more lines of code"""
509 509
510 510 self.shell.run_cell(source)
511 511
512 512 class LineDemo(Demo):
513 513 """Demo where each line is executed as a separate block.
514 514
515 515 The input script should be valid Python code.
516 516
517 517 This class doesn't require any markup at all, and it's meant for simple
518 518 scripts (with no nesting or any kind of indentation) which consist of
519 519 multiple lines of input to be executed, one at a time, as if they had been
520 520 typed in the interactive prompt.
521 521
522 522 Note: the input can not have *any* indentation, which means that only
523 523 single-lines of input are accepted, not even function definitions are
524 524 valid."""
525 525
526 526 def reload(self):
527 527 """Reload source from disk and initialize state."""
528 528 # read data and parse into blocks
529 529 self.fload()
530 530 lines = self.fobj.readlines()
531 531 src_b = [l for l in lines if l.strip()]
532 532 nblocks = len(src_b)
533 533 self.src = ''.join(lines)
534 534 self._silent = [False]*nblocks
535 535 self._auto = [True]*nblocks
536 536 self.auto_all = True
537 537 self.nblocks = nblocks
538 538 self.src_blocks = src_b
539 539
540 540 # also build syntax-highlighted source
541 541 self.src_blocks_colored = map(self.ip_colorize,self.src_blocks)
542 542
543 543 # ensure clean namespace and seek offset
544 544 self.reset()
545 545
546 546
547 547 class IPythonLineDemo(IPythonDemo,LineDemo):
548 548 """Variant of the LineDemo class whose input is processed by IPython."""
549 549 pass
550 550
551 551
552 552 class ClearMixin(object):
553 553 """Use this mixin to make Demo classes with less visual clutter.
554 554
555 555 Demos using this mixin will clear the screen before every block and use
556 556 blank marquees.
557 557
558 558 Note that in order for the methods defined here to actually override those
559 559 of the classes it's mixed with, it must go /first/ in the inheritance
560 560 tree. For example:
561 561
562 562 class ClearIPDemo(ClearMixin,IPythonDemo): pass
563 563
564 564 will provide an IPythonDemo class with the mixin's features.
565 565 """
566 566
567 567 def marquee(self,txt='',width=78,mark='*'):
568 568 """Blank marquee that returns '' no matter what the input."""
569 569 return ''
570 570
571 571 def pre_cmd(self):
572 572 """Method called before executing each block.
573 573
574 574 This one simply clears the screen."""
575 575 from IPython.utils.terminal import term_clear
576 576 term_clear()
577 577
578 578 class ClearDemo(ClearMixin,Demo):
579 579 pass
580 580
581 581
582 582 class ClearIPDemo(ClearMixin,IPythonDemo):
583 583 pass
@@ -1,139 +1,139 b''
1 1 # encoding: utf-8
2 2 """Tests for IPython.utils.path.py"""
3 3
4 4 # Copyright (c) IPython Development Team.
5 5 # Distributed under the terms of the Modified BSD License.
6 6
7 7 try:
8 8 from unittest.mock import patch
9 9 except ImportError:
10 10 from mock import patch
11 11
12 12 import nose.tools as nt
13 13
14 14 from IPython.lib import latextools
15 15 from IPython.testing.decorators import onlyif_cmds_exist, skipif_not_matplotlib
16 16 from IPython.utils.process import FindCmdError
17 17
18 18
19 19 def test_latex_to_png_dvipng_fails_when_no_cmd():
20 20 """
21 21 `latex_to_png_dvipng` should return None when there is no required command
22 22 """
23 23 for command in ['latex', 'dvipng']:
24 24 yield (check_latex_to_png_dvipng_fails_when_no_cmd, command)
25 25
26 26
27 27 def check_latex_to_png_dvipng_fails_when_no_cmd(command):
28 28 def mock_find_cmd(arg):
29 29 if arg == command:
30 30 raise FindCmdError
31 31
32 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 34 None)
35 35
36 36
37 37 @onlyif_cmds_exist('latex', 'dvipng')
38 38 def test_latex_to_png_dvipng_runs():
39 39 """
40 40 Test that latex_to_png_dvipng just runs without error.
41 41 """
42 42 def mock_kpsewhich(filename):
43 nt.assert_equals(filename, "breqn.sty")
43 nt.assert_equal(filename, "breqn.sty")
44 44 return None
45 45
46 46 for (s, wrap) in [(u"$$x^2$$", False), (u"x^2", True)]:
47 47 yield (latextools.latex_to_png_dvipng, s, wrap)
48 48
49 49 with patch.object(latextools, "kpsewhich", mock_kpsewhich):
50 50 yield (latextools.latex_to_png_dvipng, s, wrap)
51 51
52 52 @skipif_not_matplotlib
53 53 def test_latex_to_png_mpl_runs():
54 54 """
55 55 Test that latex_to_png_mpl just runs without error.
56 56 """
57 57 def mock_kpsewhich(filename):
58 nt.assert_equals(filename, "breqn.sty")
58 nt.assert_equal(filename, "breqn.sty")
59 59 return None
60 60
61 61 for (s, wrap) in [("$x^2$", False), ("x^2", True)]:
62 62 yield (latextools.latex_to_png_mpl, s, wrap)
63 63
64 64 with patch.object(latextools, "kpsewhich", mock_kpsewhich):
65 65 yield (latextools.latex_to_png_mpl, s, wrap)
66 66
67 67 @skipif_not_matplotlib
68 68 def test_latex_to_html():
69 69 img = latextools.latex_to_html("$x^2$")
70 70 nt.assert_in("data:image/png;base64,iVBOR", img)
71 71
72 72
73 73 def test_genelatex_no_wrap():
74 74 """
75 75 Test genelatex with wrap=False.
76 76 """
77 77 def mock_kpsewhich(filename):
78 78 assert False, ("kpsewhich should not be called "
79 79 "(called with {0})".format(filename))
80 80
81 81 with patch.object(latextools, "kpsewhich", mock_kpsewhich):
82 nt.assert_equals(
82 nt.assert_equal(
83 83 '\n'.join(latextools.genelatex("body text", False)),
84 84 r'''\documentclass{article}
85 85 \usepackage{amsmath}
86 86 \usepackage{amsthm}
87 87 \usepackage{amssymb}
88 88 \usepackage{bm}
89 89 \pagestyle{empty}
90 90 \begin{document}
91 91 body text
92 92 \end{document}''')
93 93
94 94
95 95 def test_genelatex_wrap_with_breqn():
96 96 """
97 97 Test genelatex with wrap=True for the case breqn.sty is installed.
98 98 """
99 99 def mock_kpsewhich(filename):
100 nt.assert_equals(filename, "breqn.sty")
100 nt.assert_equal(filename, "breqn.sty")
101 101 return "path/to/breqn.sty"
102 102
103 103 with patch.object(latextools, "kpsewhich", mock_kpsewhich):
104 nt.assert_equals(
104 nt.assert_equal(
105 105 '\n'.join(latextools.genelatex("x^2", True)),
106 106 r'''\documentclass{article}
107 107 \usepackage{amsmath}
108 108 \usepackage{amsthm}
109 109 \usepackage{amssymb}
110 110 \usepackage{bm}
111 111 \usepackage{breqn}
112 112 \pagestyle{empty}
113 113 \begin{document}
114 114 \begin{dmath*}
115 115 x^2
116 116 \end{dmath*}
117 117 \end{document}''')
118 118
119 119
120 120 def test_genelatex_wrap_without_breqn():
121 121 """
122 122 Test genelatex with wrap=True for the case breqn.sty is not installed.
123 123 """
124 124 def mock_kpsewhich(filename):
125 nt.assert_equals(filename, "breqn.sty")
125 nt.assert_equal(filename, "breqn.sty")
126 126 return None
127 127
128 128 with patch.object(latextools, "kpsewhich", mock_kpsewhich):
129 nt.assert_equals(
129 nt.assert_equal(
130 130 '\n'.join(latextools.genelatex("x^2", True)),
131 131 r'''\documentclass{article}
132 132 \usepackage{amsmath}
133 133 \usepackage{amsthm}
134 134 \usepackage{amssymb}
135 135 \usepackage{bm}
136 136 \pagestyle{empty}
137 137 \begin{document}
138 138 $$x^2$$
139 139 \end{document}''')
@@ -1,1185 +1,1184 b''
1 1 # -*- coding: utf-8 -*-
2 2 """
3 3 Sphinx directive to support embedded IPython code.
4 4
5 5 This directive allows pasting of entire interactive IPython sessions, prompts
6 6 and all, and their code will actually get re-executed at doc build time, with
7 7 all prompts renumbered sequentially. It also allows you to input code as a pure
8 8 python input by giving the argument python to the directive. The output looks
9 9 like an interactive ipython section.
10 10
11 11 To enable this directive, simply list it in your Sphinx ``conf.py`` file
12 12 (making sure the directory where you placed it is visible to sphinx, as is
13 13 needed for all Sphinx directives). For example, to enable syntax highlighting
14 14 and the IPython directive::
15 15
16 16 extensions = ['IPython.sphinxext.ipython_console_highlighting',
17 17 'IPython.sphinxext.ipython_directive']
18 18
19 19 The IPython directive outputs code-blocks with the language 'ipython'. So
20 20 if you do not have the syntax highlighting extension enabled as well, then
21 21 all rendered code-blocks will be uncolored. By default this directive assumes
22 22 that your prompts are unchanged IPython ones, but this can be customized.
23 23 The configurable options that can be placed in conf.py are:
24 24
25 25 ipython_savefig_dir:
26 26 The directory in which to save the figures. This is relative to the
27 27 Sphinx source directory. The default is `html_static_path`.
28 28 ipython_rgxin:
29 29 The compiled regular expression to denote the start of IPython input
30 30 lines. The default is re.compile('In \[(\d+)\]:\s?(.*)\s*'). You
31 31 shouldn't need to change this.
32 32 ipython_rgxout:
33 33 The compiled regular expression to denote the start of IPython output
34 34 lines. The default is re.compile('Out\[(\d+)\]:\s?(.*)\s*'). You
35 35 shouldn't need to change this.
36 36 ipython_promptin:
37 37 The string to represent the IPython input prompt in the generated ReST.
38 38 The default is 'In [%d]:'. This expects that the line numbers are used
39 39 in the prompt.
40 40 ipython_promptout:
41 41 The string to represent the IPython prompt in the generated ReST. The
42 42 default is 'Out [%d]:'. This expects that the line numbers are used
43 43 in the prompt.
44 44 ipython_mplbackend:
45 45 The string which specifies if the embedded Sphinx shell should import
46 46 Matplotlib and set the backend. The value specifies a backend that is
47 47 passed to `matplotlib.use()` before any lines in `ipython_execlines` are
48 48 executed. If not specified in conf.py, then the default value of 'agg' is
49 49 used. To use the IPython directive without matplotlib as a dependency, set
50 50 the value to `None`. It may end up that matplotlib is still imported
51 51 if the user specifies so in `ipython_execlines` or makes use of the
52 52 @savefig pseudo decorator.
53 53 ipython_execlines:
54 54 A list of strings to be exec'd in the embedded Sphinx shell. Typical
55 55 usage is to make certain packages always available. Set this to an empty
56 56 list if you wish to have no imports always available. If specified in
57 57 conf.py as `None`, then it has the effect of making no imports available.
58 58 If omitted from conf.py altogether, then the default value of
59 59 ['import numpy as np', 'import matplotlib.pyplot as plt'] is used.
60 60 ipython_holdcount
61 61 When the @suppress pseudo-decorator is used, the execution count can be
62 62 incremented or not. The default behavior is to hold the execution count,
63 63 corresponding to a value of `True`. Set this to `False` to increment
64 64 the execution count after each suppressed command.
65 65
66 66 As an example, to use the IPython directive when `matplotlib` is not available,
67 67 one sets the backend to `None`::
68 68
69 69 ipython_mplbackend = None
70 70
71 71 An example usage of the directive is:
72 72
73 73 .. code-block:: rst
74 74
75 75 .. ipython::
76 76
77 77 In [1]: x = 1
78 78
79 79 In [2]: y = x**2
80 80
81 81 In [3]: print(y)
82 82
83 83 See http://matplotlib.org/sampledoc/ipython_directive.html for additional
84 84 documentation.
85 85
86 86 Pseudo-Decorators
87 87 =================
88 88
89 89 Note: Only one decorator is supported per input. If more than one decorator
90 90 is specified, then only the last one is used.
91 91
92 92 In addition to the Pseudo-Decorators/options described at the above link,
93 93 several enhancements have been made. The directive will emit a message to the
94 94 console at build-time if code-execution resulted in an exception or warning.
95 95 You can suppress these on a per-block basis by specifying the :okexcept:
96 96 or :okwarning: options:
97 97
98 98 .. code-block:: rst
99 99
100 100 .. ipython::
101 101 :okexcept:
102 102 :okwarning:
103 103
104 104 In [1]: 1/0
105 105 In [2]: # raise warning.
106 106
107 107 ToDo
108 108 ----
109 109
110 110 - Turn the ad-hoc test() function into a real test suite.
111 111 - Break up ipython-specific functionality from matplotlib stuff into better
112 112 separated code.
113 113
114 114 Authors
115 115 -------
116 116
117 117 - John D Hunter: orignal author.
118 118 - Fernando Perez: refactoring, documentation, cleanups, port to 0.11.
119 119 - VΓ‘clavΕ milauer <eudoxos-AT-arcig.cz>: Prompt generalizations.
120 120 - Skipper Seabold, refactoring, cleanups, pure python addition
121 121 """
122 122 from __future__ import print_function
123 123
124 124 #-----------------------------------------------------------------------------
125 125 # Imports
126 126 #-----------------------------------------------------------------------------
127 127
128 128 # Stdlib
129 129 import atexit
130 130 import os
131 131 import re
132 132 import sys
133 133 import tempfile
134 134 import ast
135 135 import warnings
136 136 import shutil
137 137
138 138
139 139 # Third-party
140 140 from docutils.parsers.rst import directives
141 141 from sphinx.util.compat import Directive
142 142
143 143 # Our own
144 144 from traitlets.config import Config
145 145 from IPython import InteractiveShell
146 146 from IPython.core.profiledir import ProfileDir
147 147 from IPython.utils import io
148 148 from IPython.utils.py3compat import PY3
149 149
150 150 if PY3:
151 151 from io import StringIO
152 152 else:
153 153 from StringIO import StringIO
154 154
155 155 #-----------------------------------------------------------------------------
156 156 # Globals
157 157 #-----------------------------------------------------------------------------
158 158 # for tokenizing blocks
159 159 COMMENT, INPUT, OUTPUT = range(3)
160 160
161 161 #-----------------------------------------------------------------------------
162 162 # Functions and class declarations
163 163 #-----------------------------------------------------------------------------
164 164
165 165 def block_parser(part, rgxin, rgxout, fmtin, fmtout):
166 166 """
167 167 part is a string of ipython text, comprised of at most one
168 168 input, one output, comments, and blank lines. The block parser
169 169 parses the text into a list of::
170 170
171 171 blocks = [ (TOKEN0, data0), (TOKEN1, data1), ...]
172 172
173 173 where TOKEN is one of [COMMENT | INPUT | OUTPUT ] and
174 174 data is, depending on the type of token::
175 175
176 176 COMMENT : the comment string
177 177
178 178 INPUT: the (DECORATOR, INPUT_LINE, REST) where
179 179 DECORATOR: the input decorator (or None)
180 180 INPUT_LINE: the input as string (possibly multi-line)
181 181 REST : any stdout generated by the input line (not OUTPUT)
182 182
183 183 OUTPUT: the output string, possibly multi-line
184 184
185 185 """
186 186 block = []
187 187 lines = part.split('\n')
188 188 N = len(lines)
189 189 i = 0
190 190 decorator = None
191 191 while 1:
192 192
193 193 if i==N:
194 194 # nothing left to parse -- the last line
195 195 break
196 196
197 197 line = lines[i]
198 198 i += 1
199 199 line_stripped = line.strip()
200 200 if line_stripped.startswith('#'):
201 201 block.append((COMMENT, line))
202 202 continue
203 203
204 204 if line_stripped.startswith('@'):
205 205 # Here is where we assume there is, at most, one decorator.
206 206 # Might need to rethink this.
207 207 decorator = line_stripped
208 208 continue
209 209
210 210 # does this look like an input line?
211 211 matchin = rgxin.match(line)
212 212 if matchin:
213 213 lineno, inputline = int(matchin.group(1)), matchin.group(2)
214 214
215 215 # the ....: continuation string
216 216 continuation = ' %s:'%''.join(['.']*(len(str(lineno))+2))
217 217 Nc = len(continuation)
218 218 # input lines can continue on for more than one line, if
219 219 # we have a '\' line continuation char or a function call
220 220 # echo line 'print'. The input line can only be
221 221 # terminated by the end of the block or an output line, so
222 222 # we parse out the rest of the input line if it is
223 223 # multiline as well as any echo text
224 224
225 225 rest = []
226 226 while i<N:
227 227
228 228 # look ahead; if the next line is blank, or a comment, or
229 229 # an output line, we're done
230 230
231 231 nextline = lines[i]
232 232 matchout = rgxout.match(nextline)
233 233 #print "nextline=%s, continuation=%s, starts=%s"%(nextline, continuation, nextline.startswith(continuation))
234 234 if matchout or nextline.startswith('#'):
235 235 break
236 236 elif nextline.startswith(continuation):
237 237 # The default ipython_rgx* treat the space following the colon as optional.
238 238 # However, If the space is there we must consume it or code
239 239 # employing the cython_magic extension will fail to execute.
240 240 #
241 241 # This works with the default ipython_rgx* patterns,
242 242 # If you modify them, YMMV.
243 243 nextline = nextline[Nc:]
244 244 if nextline and nextline[0] == ' ':
245 245 nextline = nextline[1:]
246 246
247 247 inputline += '\n' + nextline
248 248 else:
249 249 rest.append(nextline)
250 250 i+= 1
251 251
252 252 block.append((INPUT, (decorator, inputline, '\n'.join(rest))))
253 253 continue
254 254
255 255 # if it looks like an output line grab all the text to the end
256 256 # of the block
257 257 matchout = rgxout.match(line)
258 258 if matchout:
259 259 lineno, output = int(matchout.group(1)), matchout.group(2)
260 260 if i<N-1:
261 261 output = '\n'.join([output] + lines[i:])
262 262
263 263 block.append((OUTPUT, output))
264 264 break
265 265
266 266 return block
267 267
268 268
269 269 class EmbeddedSphinxShell(object):
270 270 """An embedded IPython instance to run inside Sphinx"""
271 271
272 272 def __init__(self, exec_lines=None):
273 273
274 274 self.cout = StringIO()
275 275
276 276 if exec_lines is None:
277 277 exec_lines = []
278 278
279 279 # Create config object for IPython
280 280 config = Config()
281 281 config.HistoryManager.hist_file = ':memory:'
282 282 config.InteractiveShell.autocall = False
283 283 config.InteractiveShell.autoindent = False
284 284 config.InteractiveShell.colors = 'NoColor'
285 285
286 286 # create a profile so instance history isn't saved
287 287 tmp_profile_dir = tempfile.mkdtemp(prefix='profile_')
288 288 profname = 'auto_profile_sphinx_build'
289 289 pdir = os.path.join(tmp_profile_dir,profname)
290 290 profile = ProfileDir.create_profile_dir(pdir)
291 291
292 292 # Create and initialize global ipython, but don't start its mainloop.
293 293 # This will persist across different EmbededSphinxShell instances.
294 294 IP = InteractiveShell.instance(config=config, profile_dir=profile)
295 295 atexit.register(self.cleanup)
296 296
297 # io.stdout redirect must be done after instantiating InteractiveShell
298 io.stdout = self.cout
299 io.stderr = self.cout
297 sys.stdout = self.cout
298 sys.stderr = self.cout
300 299
301 300 # For debugging, so we can see normal output, use this:
302 301 #from IPython.utils.io import Tee
303 #io.stdout = Tee(self.cout, channel='stdout') # dbg
304 #io.stderr = Tee(self.cout, channel='stderr') # dbg
302 #sys.stdout = Tee(self.cout, channel='stdout') # dbg
303 #sys.stderr = Tee(self.cout, channel='stderr') # dbg
305 304
306 305 # Store a few parts of IPython we'll need.
307 306 self.IP = IP
308 307 self.user_ns = self.IP.user_ns
309 308 self.user_global_ns = self.IP.user_global_ns
310 309
311 310 self.input = ''
312 311 self.output = ''
313 312 self.tmp_profile_dir = tmp_profile_dir
314 313
315 314 self.is_verbatim = False
316 315 self.is_doctest = False
317 316 self.is_suppress = False
318 317
319 318 # Optionally, provide more detailed information to shell.
320 319 # this is assigned by the SetUp method of IPythonDirective
321 320 # to point at itself.
322 321 #
323 322 # So, you can access handy things at self.directive.state
324 323 self.directive = None
325 324
326 325 # on the first call to the savefig decorator, we'll import
327 326 # pyplot as plt so we can make a call to the plt.gcf().savefig
328 327 self._pyplot_imported = False
329 328
330 329 # Prepopulate the namespace.
331 330 for line in exec_lines:
332 331 self.process_input_line(line, store_history=False)
333 332
334 333 def cleanup(self):
335 334 shutil.rmtree(self.tmp_profile_dir, ignore_errors=True)
336 335
337 336 def clear_cout(self):
338 337 self.cout.seek(0)
339 338 self.cout.truncate(0)
340 339
341 340 def process_input_line(self, line, store_history=True):
342 341 """process the input, capturing stdout"""
343 342
344 343 stdout = sys.stdout
345 344 splitter = self.IP.input_splitter
346 345 try:
347 346 sys.stdout = self.cout
348 347 splitter.push(line)
349 348 more = splitter.push_accepts_more()
350 349 if not more:
351 350 source_raw = splitter.raw_reset()
352 351 self.IP.run_cell(source_raw, store_history=store_history)
353 352 finally:
354 353 sys.stdout = stdout
355 354
356 355 def process_image(self, decorator):
357 356 """
358 357 # build out an image directive like
359 358 # .. image:: somefile.png
360 359 # :width 4in
361 360 #
362 361 # from an input like
363 362 # savefig somefile.png width=4in
364 363 """
365 364 savefig_dir = self.savefig_dir
366 365 source_dir = self.source_dir
367 366 saveargs = decorator.split(' ')
368 367 filename = saveargs[1]
369 368 # insert relative path to image file in source
370 369 outfile = os.path.relpath(os.path.join(savefig_dir,filename),
371 370 source_dir)
372 371
373 372 imagerows = ['.. image:: %s'%outfile]
374 373
375 374 for kwarg in saveargs[2:]:
376 375 arg, val = kwarg.split('=')
377 376 arg = arg.strip()
378 377 val = val.strip()
379 378 imagerows.append(' :%s: %s'%(arg, val))
380 379
381 380 image_file = os.path.basename(outfile) # only return file name
382 381 image_directive = '\n'.join(imagerows)
383 382 return image_file, image_directive
384 383
385 384 # Callbacks for each type of token
386 385 def process_input(self, data, input_prompt, lineno):
387 386 """
388 387 Process data block for INPUT token.
389 388
390 389 """
391 390 decorator, input, rest = data
392 391 image_file = None
393 392 image_directive = None
394 393
395 394 is_verbatim = decorator=='@verbatim' or self.is_verbatim
396 395 is_doctest = (decorator is not None and \
397 396 decorator.startswith('@doctest')) or self.is_doctest
398 397 is_suppress = decorator=='@suppress' or self.is_suppress
399 398 is_okexcept = decorator=='@okexcept' or self.is_okexcept
400 399 is_okwarning = decorator=='@okwarning' or self.is_okwarning
401 400 is_savefig = decorator is not None and \
402 401 decorator.startswith('@savefig')
403 402
404 403 input_lines = input.split('\n')
405 404 if len(input_lines) > 1:
406 405 if input_lines[-1] != "":
407 406 input_lines.append('') # make sure there's a blank line
408 407 # so splitter buffer gets reset
409 408
410 409 continuation = ' %s:'%''.join(['.']*(len(str(lineno))+2))
411 410
412 411 if is_savefig:
413 412 image_file, image_directive = self.process_image(decorator)
414 413
415 414 ret = []
416 415 is_semicolon = False
417 416
418 417 # Hold the execution count, if requested to do so.
419 418 if is_suppress and self.hold_count:
420 419 store_history = False
421 420 else:
422 421 store_history = True
423 422
424 423 # Note: catch_warnings is not thread safe
425 424 with warnings.catch_warnings(record=True) as ws:
426 425 for i, line in enumerate(input_lines):
427 426 if line.endswith(';'):
428 427 is_semicolon = True
429 428
430 429 if i == 0:
431 430 # process the first input line
432 431 if is_verbatim:
433 432 self.process_input_line('')
434 433 self.IP.execution_count += 1 # increment it anyway
435 434 else:
436 435 # only submit the line in non-verbatim mode
437 436 self.process_input_line(line, store_history=store_history)
438 437 formatted_line = '%s %s'%(input_prompt, line)
439 438 else:
440 439 # process a continuation line
441 440 if not is_verbatim:
442 441 self.process_input_line(line, store_history=store_history)
443 442
444 443 formatted_line = '%s %s'%(continuation, line)
445 444
446 445 if not is_suppress:
447 446 ret.append(formatted_line)
448 447
449 448 if not is_suppress and len(rest.strip()) and is_verbatim:
450 449 # The "rest" is the standard output of the input. This needs to be
451 450 # added when in verbatim mode. If there is no "rest", then we don't
452 451 # add it, as the new line will be added by the processed output.
453 452 ret.append(rest)
454 453
455 454 # Fetch the processed output. (This is not the submitted output.)
456 455 self.cout.seek(0)
457 456 processed_output = self.cout.read()
458 457 if not is_suppress and not is_semicolon:
459 458 #
460 459 # In IPythonDirective.run, the elements of `ret` are eventually
461 460 # combined such that '' entries correspond to newlines. So if
462 461 # `processed_output` is equal to '', then the adding it to `ret`
463 462 # ensures that there is a blank line between consecutive inputs
464 463 # that have no outputs, as in:
465 464 #
466 465 # In [1]: x = 4
467 466 #
468 467 # In [2]: x = 5
469 468 #
470 469 # When there is processed output, it has a '\n' at the tail end. So
471 470 # adding the output to `ret` will provide the necessary spacing
472 471 # between consecutive input/output blocks, as in:
473 472 #
474 473 # In [1]: x
475 474 # Out[1]: 5
476 475 #
477 476 # In [2]: x
478 477 # Out[2]: 5
479 478 #
480 479 # When there is stdout from the input, it also has a '\n' at the
481 480 # tail end, and so this ensures proper spacing as well. E.g.:
482 481 #
483 482 # In [1]: print x
484 483 # 5
485 484 #
486 485 # In [2]: x = 5
487 486 #
488 487 # When in verbatim mode, `processed_output` is empty (because
489 488 # nothing was passed to IP. Sometimes the submitted code block has
490 489 # an Out[] portion and sometimes it does not. When it does not, we
491 490 # need to ensure proper spacing, so we have to add '' to `ret`.
492 491 # However, if there is an Out[] in the submitted code, then we do
493 492 # not want to add a newline as `process_output` has stuff to add.
494 493 # The difficulty is that `process_input` doesn't know if
495 494 # `process_output` will be called---so it doesn't know if there is
496 495 # Out[] in the code block. The requires that we include a hack in
497 496 # `process_block`. See the comments there.
498 497 #
499 498 ret.append(processed_output)
500 499 elif is_semicolon:
501 500 # Make sure there is a newline after the semicolon.
502 501 ret.append('')
503 502
504 503 # context information
505 504 filename = "Unknown"
506 505 lineno = 0
507 506 if self.directive.state:
508 507 filename = self.directive.state.document.current_source
509 508 lineno = self.directive.state.document.current_line
510 509
511 510 # output any exceptions raised during execution to stdout
512 511 # unless :okexcept: has been specified.
513 512 if not is_okexcept and "Traceback" in processed_output:
514 513 s = "\nException in %s at block ending on line %s\n" % (filename, lineno)
515 514 s += "Specify :okexcept: as an option in the ipython:: block to suppress this message\n"
516 515 sys.stdout.write('\n\n>>>' + ('-' * 73))
517 516 sys.stdout.write(s)
518 517 sys.stdout.write(processed_output)
519 518 sys.stdout.write('<<<' + ('-' * 73) + '\n\n')
520 519
521 520 # output any warning raised during execution to stdout
522 521 # unless :okwarning: has been specified.
523 522 if not is_okwarning:
524 523 for w in ws:
525 524 s = "\nWarning in %s at block ending on line %s\n" % (filename, lineno)
526 525 s += "Specify :okwarning: as an option in the ipython:: block to suppress this message\n"
527 526 sys.stdout.write('\n\n>>>' + ('-' * 73))
528 527 sys.stdout.write(s)
529 528 sys.stdout.write(('-' * 76) + '\n')
530 529 s=warnings.formatwarning(w.message, w.category,
531 530 w.filename, w.lineno, w.line)
532 531 sys.stdout.write(s)
533 532 sys.stdout.write('<<<' + ('-' * 73) + '\n')
534 533
535 534 self.cout.truncate(0)
536 535
537 536 return (ret, input_lines, processed_output,
538 537 is_doctest, decorator, image_file, image_directive)
539 538
540 539
541 540 def process_output(self, data, output_prompt, input_lines, output,
542 541 is_doctest, decorator, image_file):
543 542 """
544 543 Process data block for OUTPUT token.
545 544
546 545 """
547 546 # Recall: `data` is the submitted output, and `output` is the processed
548 547 # output from `input_lines`.
549 548
550 549 TAB = ' ' * 4
551 550
552 551 if is_doctest and output is not None:
553 552
554 553 found = output # This is the processed output
555 554 found = found.strip()
556 555 submitted = data.strip()
557 556
558 557 if self.directive is None:
559 558 source = 'Unavailable'
560 559 content = 'Unavailable'
561 560 else:
562 561 source = self.directive.state.document.current_source
563 562 content = self.directive.content
564 563 # Add tabs and join into a single string.
565 564 content = '\n'.join([TAB + line for line in content])
566 565
567 566 # Make sure the output contains the output prompt.
568 567 ind = found.find(output_prompt)
569 568 if ind < 0:
570 569 e = ('output does not contain output prompt\n\n'
571 570 'Document source: {0}\n\n'
572 571 'Raw content: \n{1}\n\n'
573 572 'Input line(s):\n{TAB}{2}\n\n'
574 573 'Output line(s):\n{TAB}{3}\n\n')
575 574 e = e.format(source, content, '\n'.join(input_lines),
576 575 repr(found), TAB=TAB)
577 576 raise RuntimeError(e)
578 577 found = found[len(output_prompt):].strip()
579 578
580 579 # Handle the actual doctest comparison.
581 580 if decorator.strip() == '@doctest':
582 581 # Standard doctest
583 582 if found != submitted:
584 583 e = ('doctest failure\n\n'
585 584 'Document source: {0}\n\n'
586 585 'Raw content: \n{1}\n\n'
587 586 'On input line(s):\n{TAB}{2}\n\n'
588 587 'we found output:\n{TAB}{3}\n\n'
589 588 'instead of the expected:\n{TAB}{4}\n\n')
590 589 e = e.format(source, content, '\n'.join(input_lines),
591 590 repr(found), repr(submitted), TAB=TAB)
592 591 raise RuntimeError(e)
593 592 else:
594 593 self.custom_doctest(decorator, input_lines, found, submitted)
595 594
596 595 # When in verbatim mode, this holds additional submitted output
597 596 # to be written in the final Sphinx output.
598 597 # https://github.com/ipython/ipython/issues/5776
599 598 out_data = []
600 599
601 600 is_verbatim = decorator=='@verbatim' or self.is_verbatim
602 601 if is_verbatim and data.strip():
603 602 # Note that `ret` in `process_block` has '' as its last element if
604 603 # the code block was in verbatim mode. So if there is no submitted
605 604 # output, then we will have proper spacing only if we do not add
606 605 # an additional '' to `out_data`. This is why we condition on
607 606 # `and data.strip()`.
608 607
609 608 # The submitted output has no output prompt. If we want the
610 609 # prompt and the code to appear, we need to join them now
611 610 # instead of adding them separately---as this would create an
612 611 # undesired newline. How we do this ultimately depends on the
613 612 # format of the output regex. I'll do what works for the default
614 613 # prompt for now, and we might have to adjust if it doesn't work
615 614 # in other cases. Finally, the submitted output does not have
616 615 # a trailing newline, so we must add it manually.
617 616 out_data.append("{0} {1}\n".format(output_prompt, data))
618 617
619 618 return out_data
620 619
621 620 def process_comment(self, data):
622 621 """Process data fPblock for COMMENT token."""
623 622 if not self.is_suppress:
624 623 return [data]
625 624
626 625 def save_image(self, image_file):
627 626 """
628 627 Saves the image file to disk.
629 628 """
630 629 self.ensure_pyplot()
631 630 command = 'plt.gcf().savefig("%s")'%image_file
632 631 #print 'SAVEFIG', command # dbg
633 632 self.process_input_line('bookmark ipy_thisdir', store_history=False)
634 633 self.process_input_line('cd -b ipy_savedir', store_history=False)
635 634 self.process_input_line(command, store_history=False)
636 635 self.process_input_line('cd -b ipy_thisdir', store_history=False)
637 636 self.process_input_line('bookmark -d ipy_thisdir', store_history=False)
638 637 self.clear_cout()
639 638
640 639 def process_block(self, block):
641 640 """
642 641 process block from the block_parser and return a list of processed lines
643 642 """
644 643 ret = []
645 644 output = None
646 645 input_lines = None
647 646 lineno = self.IP.execution_count
648 647
649 648 input_prompt = self.promptin % lineno
650 649 output_prompt = self.promptout % lineno
651 650 image_file = None
652 651 image_directive = None
653 652
654 653 found_input = False
655 654 for token, data in block:
656 655 if token == COMMENT:
657 656 out_data = self.process_comment(data)
658 657 elif token == INPUT:
659 658 found_input = True
660 659 (out_data, input_lines, output, is_doctest,
661 660 decorator, image_file, image_directive) = \
662 661 self.process_input(data, input_prompt, lineno)
663 662 elif token == OUTPUT:
664 663 if not found_input:
665 664
666 665 TAB = ' ' * 4
667 666 linenumber = 0
668 667 source = 'Unavailable'
669 668 content = 'Unavailable'
670 669 if self.directive:
671 670 linenumber = self.directive.state.document.current_line
672 671 source = self.directive.state.document.current_source
673 672 content = self.directive.content
674 673 # Add tabs and join into a single string.
675 674 content = '\n'.join([TAB + line for line in content])
676 675
677 676 e = ('\n\nInvalid block: Block contains an output prompt '
678 677 'without an input prompt.\n\n'
679 678 'Document source: {0}\n\n'
680 679 'Content begins at line {1}: \n\n{2}\n\n'
681 680 'Problematic block within content: \n\n{TAB}{3}\n\n')
682 681 e = e.format(source, linenumber, content, block, TAB=TAB)
683 682
684 683 # Write, rather than include in exception, since Sphinx
685 684 # will truncate tracebacks.
686 685 sys.stdout.write(e)
687 686 raise RuntimeError('An invalid block was detected.')
688 687
689 688 out_data = \
690 689 self.process_output(data, output_prompt, input_lines,
691 690 output, is_doctest, decorator,
692 691 image_file)
693 692 if out_data:
694 693 # Then there was user submitted output in verbatim mode.
695 694 # We need to remove the last element of `ret` that was
696 695 # added in `process_input`, as it is '' and would introduce
697 696 # an undesirable newline.
698 697 assert(ret[-1] == '')
699 698 del ret[-1]
700 699
701 700 if out_data:
702 701 ret.extend(out_data)
703 702
704 703 # save the image files
705 704 if image_file is not None:
706 705 self.save_image(image_file)
707 706
708 707 return ret, image_directive
709 708
710 709 def ensure_pyplot(self):
711 710 """
712 711 Ensures that pyplot has been imported into the embedded IPython shell.
713 712
714 713 Also, makes sure to set the backend appropriately if not set already.
715 714
716 715 """
717 716 # We are here if the @figure pseudo decorator was used. Thus, it's
718 717 # possible that we could be here even if python_mplbackend were set to
719 718 # `None`. That's also strange and perhaps worthy of raising an
720 719 # exception, but for now, we just set the backend to 'agg'.
721 720
722 721 if not self._pyplot_imported:
723 722 if 'matplotlib.backends' not in sys.modules:
724 723 # Then ipython_matplotlib was set to None but there was a
725 724 # call to the @figure decorator (and ipython_execlines did
726 725 # not set a backend).
727 726 #raise Exception("No backend was set, but @figure was used!")
728 727 import matplotlib
729 728 matplotlib.use('agg')
730 729
731 730 # Always import pyplot into embedded shell.
732 731 self.process_input_line('import matplotlib.pyplot as plt',
733 732 store_history=False)
734 733 self._pyplot_imported = True
735 734
736 735 def process_pure_python(self, content):
737 736 """
738 737 content is a list of strings. it is unedited directive content
739 738
740 739 This runs it line by line in the InteractiveShell, prepends
741 740 prompts as needed capturing stderr and stdout, then returns
742 741 the content as a list as if it were ipython code
743 742 """
744 743 output = []
745 744 savefig = False # keep up with this to clear figure
746 745 multiline = False # to handle line continuation
747 746 multiline_start = None
748 747 fmtin = self.promptin
749 748
750 749 ct = 0
751 750
752 751 for lineno, line in enumerate(content):
753 752
754 753 line_stripped = line.strip()
755 754 if not len(line):
756 755 output.append(line)
757 756 continue
758 757
759 758 # handle decorators
760 759 if line_stripped.startswith('@'):
761 760 output.extend([line])
762 761 if 'savefig' in line:
763 762 savefig = True # and need to clear figure
764 763 continue
765 764
766 765 # handle comments
767 766 if line_stripped.startswith('#'):
768 767 output.extend([line])
769 768 continue
770 769
771 770 # deal with lines checking for multiline
772 771 continuation = u' %s:'% ''.join(['.']*(len(str(ct))+2))
773 772 if not multiline:
774 773 modified = u"%s %s" % (fmtin % ct, line_stripped)
775 774 output.append(modified)
776 775 ct += 1
777 776 try:
778 777 ast.parse(line_stripped)
779 778 output.append(u'')
780 779 except Exception: # on a multiline
781 780 multiline = True
782 781 multiline_start = lineno
783 782 else: # still on a multiline
784 783 modified = u'%s %s' % (continuation, line)
785 784 output.append(modified)
786 785
787 786 # if the next line is indented, it should be part of multiline
788 787 if len(content) > lineno + 1:
789 788 nextline = content[lineno + 1]
790 789 if len(nextline) - len(nextline.lstrip()) > 3:
791 790 continue
792 791 try:
793 792 mod = ast.parse(
794 793 '\n'.join(content[multiline_start:lineno+1]))
795 794 if isinstance(mod.body[0], ast.FunctionDef):
796 795 # check to see if we have the whole function
797 796 for element in mod.body[0].body:
798 797 if isinstance(element, ast.Return):
799 798 multiline = False
800 799 else:
801 800 output.append(u'')
802 801 multiline = False
803 802 except Exception:
804 803 pass
805 804
806 805 if savefig: # clear figure if plotted
807 806 self.ensure_pyplot()
808 807 self.process_input_line('plt.clf()', store_history=False)
809 808 self.clear_cout()
810 809 savefig = False
811 810
812 811 return output
813 812
814 813 def custom_doctest(self, decorator, input_lines, found, submitted):
815 814 """
816 815 Perform a specialized doctest.
817 816
818 817 """
819 818 from .custom_doctests import doctests
820 819
821 820 args = decorator.split()
822 821 doctest_type = args[1]
823 822 if doctest_type in doctests:
824 823 doctests[doctest_type](self, args, input_lines, found, submitted)
825 824 else:
826 825 e = "Invalid option to @doctest: {0}".format(doctest_type)
827 826 raise Exception(e)
828 827
829 828
830 829 class IPythonDirective(Directive):
831 830
832 831 has_content = True
833 832 required_arguments = 0
834 833 optional_arguments = 4 # python, suppress, verbatim, doctest
835 834 final_argumuent_whitespace = True
836 835 option_spec = { 'python': directives.unchanged,
837 836 'suppress' : directives.flag,
838 837 'verbatim' : directives.flag,
839 838 'doctest' : directives.flag,
840 839 'okexcept': directives.flag,
841 840 'okwarning': directives.flag
842 841 }
843 842
844 843 shell = None
845 844
846 845 seen_docs = set()
847 846
848 847 def get_config_options(self):
849 848 # contains sphinx configuration variables
850 849 config = self.state.document.settings.env.config
851 850
852 851 # get config variables to set figure output directory
853 852 outdir = self.state.document.settings.env.app.outdir
854 853 savefig_dir = config.ipython_savefig_dir
855 854 source_dir = os.path.dirname(self.state.document.current_source)
856 855 if savefig_dir is None:
857 856 savefig_dir = config.html_static_path or '_static'
858 857 if isinstance(savefig_dir, list):
859 858 savefig_dir = os.path.join(*savefig_dir)
860 859 savefig_dir = os.path.join(outdir, savefig_dir)
861 860
862 861 # get regex and prompt stuff
863 862 rgxin = config.ipython_rgxin
864 863 rgxout = config.ipython_rgxout
865 864 promptin = config.ipython_promptin
866 865 promptout = config.ipython_promptout
867 866 mplbackend = config.ipython_mplbackend
868 867 exec_lines = config.ipython_execlines
869 868 hold_count = config.ipython_holdcount
870 869
871 870 return (savefig_dir, source_dir, rgxin, rgxout,
872 871 promptin, promptout, mplbackend, exec_lines, hold_count)
873 872
874 873 def setup(self):
875 874 # Get configuration values.
876 875 (savefig_dir, source_dir, rgxin, rgxout, promptin, promptout,
877 876 mplbackend, exec_lines, hold_count) = self.get_config_options()
878 877
879 878 if self.shell is None:
880 879 # We will be here many times. However, when the
881 880 # EmbeddedSphinxShell is created, its interactive shell member
882 881 # is the same for each instance.
883 882
884 883 if mplbackend and 'matplotlib.backends' not in sys.modules:
885 884 import matplotlib
886 885 matplotlib.use(mplbackend)
887 886
888 887 # Must be called after (potentially) importing matplotlib and
889 888 # setting its backend since exec_lines might import pylab.
890 889 self.shell = EmbeddedSphinxShell(exec_lines)
891 890
892 891 # Store IPython directive to enable better error messages
893 892 self.shell.directive = self
894 893
895 894 # reset the execution count if we haven't processed this doc
896 895 #NOTE: this may be borked if there are multiple seen_doc tmp files
897 896 #check time stamp?
898 897 if not self.state.document.current_source in self.seen_docs:
899 898 self.shell.IP.history_manager.reset()
900 899 self.shell.IP.execution_count = 1
901 900 self.seen_docs.add(self.state.document.current_source)
902 901
903 902 # and attach to shell so we don't have to pass them around
904 903 self.shell.rgxin = rgxin
905 904 self.shell.rgxout = rgxout
906 905 self.shell.promptin = promptin
907 906 self.shell.promptout = promptout
908 907 self.shell.savefig_dir = savefig_dir
909 908 self.shell.source_dir = source_dir
910 909 self.shell.hold_count = hold_count
911 910
912 911 # setup bookmark for saving figures directory
913 912 self.shell.process_input_line('bookmark ipy_savedir %s'%savefig_dir,
914 913 store_history=False)
915 914 self.shell.clear_cout()
916 915
917 916 return rgxin, rgxout, promptin, promptout
918 917
919 918 def teardown(self):
920 919 # delete last bookmark
921 920 self.shell.process_input_line('bookmark -d ipy_savedir',
922 921 store_history=False)
923 922 self.shell.clear_cout()
924 923
925 924 def run(self):
926 925 debug = False
927 926
928 927 #TODO, any reason block_parser can't be a method of embeddable shell
929 928 # then we wouldn't have to carry these around
930 929 rgxin, rgxout, promptin, promptout = self.setup()
931 930
932 931 options = self.options
933 932 self.shell.is_suppress = 'suppress' in options
934 933 self.shell.is_doctest = 'doctest' in options
935 934 self.shell.is_verbatim = 'verbatim' in options
936 935 self.shell.is_okexcept = 'okexcept' in options
937 936 self.shell.is_okwarning = 'okwarning' in options
938 937
939 938 # handle pure python code
940 939 if 'python' in self.arguments:
941 940 content = self.content
942 941 self.content = self.shell.process_pure_python(content)
943 942
944 943 # parts consists of all text within the ipython-block.
945 944 # Each part is an input/output block.
946 945 parts = '\n'.join(self.content).split('\n\n')
947 946
948 947 lines = ['.. code-block:: ipython', '']
949 948 figures = []
950 949
951 950 for part in parts:
952 951 block = block_parser(part, rgxin, rgxout, promptin, promptout)
953 952 if len(block):
954 953 rows, figure = self.shell.process_block(block)
955 954 for row in rows:
956 955 lines.extend([' {0}'.format(line)
957 956 for line in row.split('\n')])
958 957
959 958 if figure is not None:
960 959 figures.append(figure)
961 960
962 961 for figure in figures:
963 962 lines.append('')
964 963 lines.extend(figure.split('\n'))
965 964 lines.append('')
966 965
967 966 if len(lines) > 2:
968 967 if debug:
969 968 print('\n'.join(lines))
970 969 else:
971 970 # This has to do with input, not output. But if we comment
972 971 # these lines out, then no IPython code will appear in the
973 972 # final output.
974 973 self.state_machine.insert_input(
975 974 lines, self.state_machine.input_lines.source(0))
976 975
977 976 # cleanup
978 977 self.teardown()
979 978
980 979 return []
981 980
982 981 # Enable as a proper Sphinx directive
983 982 def setup(app):
984 983 setup.app = app
985 984
986 985 app.add_directive('ipython', IPythonDirective)
987 986 app.add_config_value('ipython_savefig_dir', None, 'env')
988 987 app.add_config_value('ipython_rgxin',
989 988 re.compile('In \[(\d+)\]:\s?(.*)\s*'), 'env')
990 989 app.add_config_value('ipython_rgxout',
991 990 re.compile('Out\[(\d+)\]:\s?(.*)\s*'), 'env')
992 991 app.add_config_value('ipython_promptin', 'In [%d]:', 'env')
993 992 app.add_config_value('ipython_promptout', 'Out[%d]:', 'env')
994 993
995 994 # We could just let matplotlib pick whatever is specified as the default
996 995 # backend in the matplotlibrc file, but this would cause issues if the
997 996 # backend didn't work in headless environments. For this reason, 'agg'
998 997 # is a good default backend choice.
999 998 app.add_config_value('ipython_mplbackend', 'agg', 'env')
1000 999
1001 1000 # If the user sets this config value to `None`, then EmbeddedSphinxShell's
1002 1001 # __init__ method will treat it as [].
1003 1002 execlines = ['import numpy as np', 'import matplotlib.pyplot as plt']
1004 1003 app.add_config_value('ipython_execlines', execlines, 'env')
1005 1004
1006 1005 app.add_config_value('ipython_holdcount', True, 'env')
1007 1006
1008 1007 metadata = {'parallel_read_safe': True, 'parallel_write_safe': True}
1009 1008 return metadata
1010 1009
1011 1010 # Simple smoke test, needs to be converted to a proper automatic test.
1012 1011 def test():
1013 1012
1014 1013 examples = [
1015 1014 r"""
1016 1015 In [9]: pwd
1017 1016 Out[9]: '/home/jdhunter/py4science/book'
1018 1017
1019 1018 In [10]: cd bookdata/
1020 1019 /home/jdhunter/py4science/book/bookdata
1021 1020
1022 1021 In [2]: from pylab import *
1023 1022
1024 1023 In [2]: ion()
1025 1024
1026 1025 In [3]: im = imread('stinkbug.png')
1027 1026
1028 1027 @savefig mystinkbug.png width=4in
1029 1028 In [4]: imshow(im)
1030 1029 Out[4]: <matplotlib.image.AxesImage object at 0x39ea850>
1031 1030
1032 1031 """,
1033 1032 r"""
1034 1033
1035 1034 In [1]: x = 'hello world'
1036 1035
1037 1036 # string methods can be
1038 1037 # used to alter the string
1039 1038 @doctest
1040 1039 In [2]: x.upper()
1041 1040 Out[2]: 'HELLO WORLD'
1042 1041
1043 1042 @verbatim
1044 1043 In [3]: x.st<TAB>
1045 1044 x.startswith x.strip
1046 1045 """,
1047 1046 r"""
1048 1047
1049 1048 In [130]: url = 'http://ichart.finance.yahoo.com/table.csv?s=CROX\
1050 1049 .....: &d=9&e=22&f=2009&g=d&a=1&br=8&c=2006&ignore=.csv'
1051 1050
1052 1051 In [131]: print url.split('&')
1053 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 1054 In [60]: import urllib
1056 1055
1057 1056 """,
1058 1057 r"""\
1059 1058
1060 1059 In [133]: import numpy.random
1061 1060
1062 1061 @suppress
1063 1062 In [134]: numpy.random.seed(2358)
1064 1063
1065 1064 @doctest
1066 1065 In [135]: numpy.random.rand(10,2)
1067 1066 Out[135]:
1068 1067 array([[ 0.64524308, 0.59943846],
1069 1068 [ 0.47102322, 0.8715456 ],
1070 1069 [ 0.29370834, 0.74776844],
1071 1070 [ 0.99539577, 0.1313423 ],
1072 1071 [ 0.16250302, 0.21103583],
1073 1072 [ 0.81626524, 0.1312433 ],
1074 1073 [ 0.67338089, 0.72302393],
1075 1074 [ 0.7566368 , 0.07033696],
1076 1075 [ 0.22591016, 0.77731835],
1077 1076 [ 0.0072729 , 0.34273127]])
1078 1077
1079 1078 """,
1080 1079
1081 1080 r"""
1082 1081 In [106]: print x
1083 1082 jdh
1084 1083
1085 1084 In [109]: for i in range(10):
1086 1085 .....: print i
1087 1086 .....:
1088 1087 .....:
1089 1088 0
1090 1089 1
1091 1090 2
1092 1091 3
1093 1092 4
1094 1093 5
1095 1094 6
1096 1095 7
1097 1096 8
1098 1097 9
1099 1098 """,
1100 1099
1101 1100 r"""
1102 1101
1103 1102 In [144]: from pylab import *
1104 1103
1105 1104 In [145]: ion()
1106 1105
1107 1106 # use a semicolon to suppress the output
1108 1107 @savefig test_hist.png width=4in
1109 1108 In [151]: hist(np.random.randn(10000), 100);
1110 1109
1111 1110
1112 1111 @savefig test_plot.png width=4in
1113 1112 In [151]: plot(np.random.randn(10000), 'o');
1114 1113 """,
1115 1114
1116 1115 r"""
1117 1116 # use a semicolon to suppress the output
1118 1117 In [151]: plt.clf()
1119 1118
1120 1119 @savefig plot_simple.png width=4in
1121 1120 In [151]: plot([1,2,3])
1122 1121
1123 1122 @savefig hist_simple.png width=4in
1124 1123 In [151]: hist(np.random.randn(10000), 100);
1125 1124
1126 1125 """,
1127 1126 r"""
1128 1127 # update the current fig
1129 1128 In [151]: ylabel('number')
1130 1129
1131 1130 In [152]: title('normal distribution')
1132 1131
1133 1132
1134 1133 @savefig hist_with_text.png
1135 1134 In [153]: grid(True)
1136 1135
1137 1136 @doctest float
1138 1137 In [154]: 0.1 + 0.2
1139 1138 Out[154]: 0.3
1140 1139
1141 1140 @doctest float
1142 1141 In [155]: np.arange(16).reshape(4,4)
1143 1142 Out[155]:
1144 1143 array([[ 0, 1, 2, 3],
1145 1144 [ 4, 5, 6, 7],
1146 1145 [ 8, 9, 10, 11],
1147 1146 [12, 13, 14, 15]])
1148 1147
1149 1148 In [1]: x = np.arange(16, dtype=float).reshape(4,4)
1150 1149
1151 1150 In [2]: x[0,0] = np.inf
1152 1151
1153 1152 In [3]: x[0,1] = np.nan
1154 1153
1155 1154 @doctest float
1156 1155 In [4]: x
1157 1156 Out[4]:
1158 1157 array([[ inf, nan, 2., 3.],
1159 1158 [ 4., 5., 6., 7.],
1160 1159 [ 8., 9., 10., 11.],
1161 1160 [ 12., 13., 14., 15.]])
1162 1161
1163 1162
1164 1163 """,
1165 1164 ]
1166 1165 # skip local-file depending first example:
1167 1166 examples = examples[1:]
1168 1167
1169 1168 #ipython_directive.DEBUG = True # dbg
1170 1169 #options = dict(suppress=True) # dbg
1171 1170 options = dict()
1172 1171 for example in examples:
1173 1172 content = example.split('\n')
1174 1173 IPythonDirective('debug', arguments=None, options=options,
1175 1174 content=content, lineno=0,
1176 1175 content_offset=None, block_text=None,
1177 1176 state=None, state_machine=None,
1178 1177 )
1179 1178
1180 1179 # Run test suite as a script
1181 1180 if __name__=='__main__':
1182 1181 if not os.path.isdir('_static'):
1183 1182 os.mkdir('_static')
1184 1183 test()
1185 1184 print('All OK? Check figures in _static/')
@@ -1,479 +1,484 b''
1 1 """IPython terminal interface using prompt_toolkit"""
2 2 from __future__ import print_function
3 3
4 4 import os
5 5 import sys
6 import warnings
6 7 from warnings import warn
7 8
8 9 from IPython.core.interactiveshell import InteractiveShell, InteractiveShellABC
10 from IPython.utils import io
9 11 from IPython.utils.py3compat import PY3, cast_unicode_py2, input
10 12 from IPython.utils.terminal import toggle_set_term_title, set_term_title
11 13 from IPython.utils.process import abbrev_cwd
12 14 from traitlets import Bool, Unicode, Dict, Integer, observe, Instance, Type, default, Enum
13 15
14 16 from prompt_toolkit.enums import DEFAULT_BUFFER, EditingMode
15 17 from prompt_toolkit.filters import (HasFocus, Condition, IsDone)
16 18 from prompt_toolkit.history import InMemoryHistory
17 19 from prompt_toolkit.shortcuts import create_prompt_application, create_eventloop, create_prompt_layout, create_output
18 20 from prompt_toolkit.interface import CommandLineInterface
19 21 from prompt_toolkit.key_binding.manager import KeyBindingManager
20 22 from prompt_toolkit.layout.processors import ConditionalProcessor, HighlightMatchingBracketProcessor
21 23 from prompt_toolkit.styles import PygmentsStyle, DynamicStyle
22 24
23 25 from pygments.styles import get_style_by_name, get_all_styles
24 26 from pygments.token import Token
25 27
26 28 from .debugger import TerminalPdb, Pdb
27 29 from .magics import TerminalMagics
28 30 from .pt_inputhooks import get_inputhook_func
29 31 from .prompts import Prompts, ClassicPrompts, RichPromptDisplayHook
30 32 from .ptutils import IPythonPTCompleter, IPythonPTLexer
31 33 from .shortcuts import register_ipython_shortcuts
32 34
33 35 DISPLAY_BANNER_DEPRECATED = object()
34 36
35 37
36 38 from pygments.style import Style
37 39
38 40 class _NoStyle(Style): pass
39 41
40 42
41 43
42 44 _style_overrides_light_bg = {
43 45 Token.Prompt: '#0000ff',
44 46 Token.PromptNum: '#0000ee bold',
45 47 Token.OutPrompt: '#cc0000',
46 48 Token.OutPromptNum: '#bb0000 bold',
47 49 }
48 50
49 51 _style_overrides_linux = {
50 52 Token.Prompt: '#00cc00',
51 53 Token.PromptNum: '#00bb00 bold',
52 54 Token.OutPrompt: '#cc0000',
53 55 Token.OutPromptNum: '#bb0000 bold',
54 56 }
55 57
56 58
57 59
58 60 def get_default_editor():
59 61 try:
60 62 ed = os.environ['EDITOR']
61 63 if not PY3:
62 64 ed = ed.decode()
63 65 return ed
64 66 except KeyError:
65 67 pass
66 68 except UnicodeError:
67 69 warn("$EDITOR environment variable is not pure ASCII. Using platform "
68 70 "default editor.")
69 71
70 72 if os.name == 'posix':
71 73 return 'vi' # the only one guaranteed to be there!
72 74 else:
73 75 return 'notepad' # same in Windows!
74 76
75 77
76 78 if sys.stdin and sys.stdout and sys.stderr:
77 79 _is_tty = (sys.stdin.isatty()) and (sys.stdout.isatty()) and (sys.stderr.isatty())
78 80 else:
79 81 _is_tty = False
80 82
81 83
82 84 _use_simple_prompt = ('IPY_TEST_SIMPLE_PROMPT' in os.environ) or (not _is_tty)
83 85
84 86 class TerminalInteractiveShell(InteractiveShell):
85 87 space_for_menu = Integer(6, help='Number of line at the bottom of the screen '
86 88 'to reserve for the completion menu'
87 89 ).tag(config=True)
88 90
89 91 def _space_for_menu_changed(self, old, new):
90 92 self._update_layout()
91 93
92 94 pt_cli = None
93 95 debugger_history = None
94 96 _pt_app = None
95 97
96 98 simple_prompt = Bool(_use_simple_prompt,
97 99 help="""Use `raw_input` for the REPL, without completion, multiline input, and prompt colors.
98 100
99 101 Useful when controlling IPython as a subprocess, and piping STDIN/OUT/ERR. Known usage are:
100 102 IPython own testing machinery, and emacs inferior-shell integration through elpy.
101 103
102 104 This mode default to `True` if the `IPY_TEST_SIMPLE_PROMPT`
103 105 environment variable is set, or the current terminal is not a tty.
104 106
105 107 """
106 108 ).tag(config=True)
107 109
108 110 @property
109 111 def debugger_cls(self):
110 112 return Pdb if self.simple_prompt else TerminalPdb
111 113
112 114 confirm_exit = Bool(True,
113 115 help="""
114 116 Set to confirm when you try to exit IPython with an EOF (Control-D
115 117 in Unix, Control-Z/Enter in Windows). By typing 'exit' or 'quit',
116 118 you can force a direct exit without any confirmation.""",
117 119 ).tag(config=True)
118 120
119 121 editing_mode = Unicode('emacs',
120 122 help="Shortcut style to use at the prompt. 'vi' or 'emacs'.",
121 123 ).tag(config=True)
122 124
123 125 mouse_support = Bool(False,
124 126 help="Enable mouse support in the prompt"
125 127 ).tag(config=True)
126 128
127 129 highlighting_style = Unicode('legacy',
128 130 help="The name of a Pygments style to use for syntax highlighting: \n %s" % ', '.join(get_all_styles())
129 131 ).tag(config=True)
130 132
131 133
132 134 @observe('highlighting_style')
133 135 @observe('colors')
134 136 def _highlighting_style_changed(self, change):
135 137 self.refresh_style()
136 138
137 139 def refresh_style(self):
138 140 self._style = self._make_style_from_name(self.highlighting_style)
139 141
140 142
141 143 highlighting_style_overrides = Dict(
142 144 help="Override highlighting format for specific tokens"
143 145 ).tag(config=True)
144 146
145 147 true_color = Bool(False,
146 148 help=("Use 24bit colors instead of 256 colors in prompt highlighting. "
147 149 "If your terminal supports true color, the following command "
148 150 "should print 'TRUECOLOR' in orange: "
149 151 "printf \"\\x1b[38;2;255;100;0mTRUECOLOR\\x1b[0m\\n\"")
150 152 ).tag(config=True)
151 153
152 154 editor = Unicode(get_default_editor(),
153 155 help="Set the editor used by IPython (default to $EDITOR/vi/notepad)."
154 156 ).tag(config=True)
155 157
156 158 prompts_class = Type(Prompts, help='Class used to generate Prompt token for prompt_toolkit').tag(config=True)
157 159
158 160 prompts = Instance(Prompts)
159 161
160 162 @default('prompts')
161 163 def _prompts_default(self):
162 164 return self.prompts_class(self)
163 165
164 166 @observe('prompts')
165 167 def _(self, change):
166 168 self._update_layout()
167 169
168 170 @default('displayhook_class')
169 171 def _displayhook_class_default(self):
170 172 return RichPromptDisplayHook
171 173
172 174 term_title = Bool(True,
173 175 help="Automatically set the terminal title"
174 176 ).tag(config=True)
175 177
176 178 display_completions = Enum(('column', 'multicolumn','readlinelike'),
177 179 help= ( "Options for displaying tab completions, 'column', 'multicolumn', and "
178 180 "'readlinelike'. These options are for `prompt_toolkit`, see "
179 181 "`prompt_toolkit` documentation for more information."
180 182 ),
181 183 default_value='multicolumn').tag(config=True)
182 184
183 185 highlight_matching_brackets = Bool(True,
184 186 help="Highlight matching brackets .",
185 187 ).tag(config=True)
186 188
187 189 @observe('term_title')
188 190 def init_term_title(self, change=None):
189 191 # Enable or disable the terminal title.
190 192 if self.term_title:
191 193 toggle_set_term_title(True)
192 194 set_term_title('IPython: ' + abbrev_cwd())
193 195 else:
194 196 toggle_set_term_title(False)
195 197
196 198 def init_display_formatter(self):
197 199 super(TerminalInteractiveShell, self).init_display_formatter()
198 200 # terminal only supports plain text
199 201 self.display_formatter.active_types = ['text/plain']
200 202
201 203 def init_prompt_toolkit_cli(self):
202 204 if self.simple_prompt:
203 205 # Fall back to plain non-interactive output for tests.
204 206 # This is very limited, and only accepts a single line.
205 207 def prompt():
206 208 return cast_unicode_py2(input('In [%d]: ' % self.execution_count))
207 209 self.prompt_for_code = prompt
208 210 return
209 211
210 212 # Set up keyboard shortcuts
211 213 kbmanager = KeyBindingManager.for_prompt()
212 214 register_ipython_shortcuts(kbmanager.registry, self)
213 215
214 216 # Pre-populate history from IPython's history database
215 217 history = InMemoryHistory()
216 218 last_cell = u""
217 219 for __, ___, cell in self.history_manager.get_tail(self.history_load_length,
218 220 include_latest=True):
219 221 # Ignore blank lines and consecutive duplicates
220 222 cell = cell.rstrip()
221 223 if cell and (cell != last_cell):
222 224 history.append(cell)
223 225
224 226 self._style = self._make_style_from_name(self.highlighting_style)
225 227 style = DynamicStyle(lambda: self._style)
226 228
227 229 editing_mode = getattr(EditingMode, self.editing_mode.upper())
228 230
229 231 self._pt_app = create_prompt_application(
230 232 editing_mode=editing_mode,
231 233 key_bindings_registry=kbmanager.registry,
232 234 history=history,
233 235 completer=IPythonPTCompleter(self.Completer),
234 236 enable_history_search=True,
235 237 style=style,
236 238 mouse_support=self.mouse_support,
237 239 **self._layout_options()
238 240 )
239 241 self._eventloop = create_eventloop(self.inputhook)
240 242 self.pt_cli = CommandLineInterface(
241 243 self._pt_app, eventloop=self._eventloop,
242 244 output=create_output(true_color=self.true_color))
243 245
244 246 def _make_style_from_name(self, name):
245 247 """
246 248 Small wrapper that make an IPython compatible style from a style name
247 249
248 250 We need that to add style for prompt ... etc.
249 251 """
250 252 style_overrides = {}
251 253 if name == 'legacy':
252 254 legacy = self.colors.lower()
253 255 if legacy == 'linux':
254 256 style_cls = get_style_by_name('monokai')
255 257 style_overrides = _style_overrides_linux
256 258 elif legacy == 'lightbg':
257 259 style_overrides = _style_overrides_light_bg
258 260 style_cls = get_style_by_name('pastie')
259 261 elif legacy == 'neutral':
260 262 # The default theme needs to be visible on both a dark background
261 263 # and a light background, because we can't tell what the terminal
262 264 # looks like. These tweaks to the default theme help with that.
263 265 style_cls = get_style_by_name('default')
264 266 style_overrides.update({
265 267 Token.Number: '#007700',
266 268 Token.Operator: 'noinherit',
267 269 Token.String: '#BB6622',
268 270 Token.Name.Function: '#2080D0',
269 271 Token.Name.Class: 'bold #2080D0',
270 272 Token.Name.Namespace: 'bold #2080D0',
271 273 Token.Prompt: '#009900',
272 274 Token.PromptNum: '#00ff00 bold',
273 275 Token.OutPrompt: '#990000',
274 276 Token.OutPromptNum: '#ff0000 bold',
275 277 })
276 278 elif legacy =='nocolor':
277 279 style_cls=_NoStyle
278 280 style_overrides = {}
279 281 else :
280 282 raise ValueError('Got unknown colors: ', legacy)
281 283 else :
282 284 style_cls = get_style_by_name(name)
283 285 style_overrides = {
284 286 Token.Prompt: '#009900',
285 287 Token.PromptNum: '#00ff00 bold',
286 288 Token.OutPrompt: '#990000',
287 289 Token.OutPromptNum: '#ff0000 bold',
288 290 }
289 291 style_overrides.update(self.highlighting_style_overrides)
290 292 style = PygmentsStyle.from_defaults(pygments_style_cls=style_cls,
291 293 style_dict=style_overrides)
292 294
293 295 return style
294 296
295 297 def _layout_options(self):
296 298 """
297 299 Return the current layout option for the current Terminal InteractiveShell
298 300 """
299 301 return {
300 302 'lexer':IPythonPTLexer(),
301 303 'reserve_space_for_menu':self.space_for_menu,
302 304 'get_prompt_tokens':self.prompts.in_prompt_tokens,
303 305 'get_continuation_tokens':self.prompts.continuation_prompt_tokens,
304 306 'multiline':True,
305 307 'display_completions_in_columns': (self.display_completions == 'multicolumn'),
306 308
307 309 # Highlight matching brackets, but only when this setting is
308 310 # enabled, and only when the DEFAULT_BUFFER has the focus.
309 311 'extra_input_processors': [ConditionalProcessor(
310 312 processor=HighlightMatchingBracketProcessor(chars='[](){}'),
311 313 filter=HasFocus(DEFAULT_BUFFER) & ~IsDone() &
312 314 Condition(lambda cli: self.highlight_matching_brackets))],
313 315 }
314 316
315 317 def _update_layout(self):
316 318 """
317 319 Ask for a re computation of the application layout, if for example ,
318 320 some configuration options have changed.
319 321 """
320 322 if self._pt_app:
321 323 self._pt_app.layout = create_prompt_layout(**self._layout_options())
322 324
323 325 def prompt_for_code(self):
324 326 document = self.pt_cli.run(
325 327 pre_run=self.pre_prompt, reset_current_buffer=True)
326 328 return document.text
327 329
328 330 def enable_win_unicode_console(self):
329 331 import win_unicode_console
330 332
331 333 if PY3:
332 334 win_unicode_console.enable()
333 335 else:
334 336 # https://github.com/ipython/ipython/issues/9768
335 337 from win_unicode_console.streams import (TextStreamWrapper,
336 338 stdout_text_transcoded, stderr_text_transcoded)
337 339
338 340 class LenientStrStreamWrapper(TextStreamWrapper):
339 341 def write(self, s):
340 342 if isinstance(s, bytes):
341 343 s = s.decode(self.encoding, 'replace')
342 344
343 345 self.base.write(s)
344 346
345 347 stdout_text_str = LenientStrStreamWrapper(stdout_text_transcoded)
346 348 stderr_text_str = LenientStrStreamWrapper(stderr_text_transcoded)
347 349
348 350 win_unicode_console.enable(stdout=stdout_text_str,
349 351 stderr=stderr_text_str)
350 352
351 353 def init_io(self):
352 354 if sys.platform not in {'win32', 'cli'}:
353 355 return
354 356
355 357 self.enable_win_unicode_console()
356 358
357 359 import colorama
358 360 colorama.init()
359 361
360 362 # For some reason we make these wrappers around stdout/stderr.
361 363 # For now, we need to reset them so all output gets coloured.
362 364 # https://github.com/ipython/ipython/issues/8669
363 from IPython.utils import io
364 io.stdout = io.IOStream(sys.stdout)
365 io.stderr = io.IOStream(sys.stderr)
365 # io.std* are deprecated, but don't show our own deprecation warnings
366 # during initialization of the deprecated API.
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 372 def init_magics(self):
368 373 super(TerminalInteractiveShell, self).init_magics()
369 374 self.register_magics(TerminalMagics)
370 375
371 376 def init_alias(self):
372 377 # The parent class defines aliases that can be safely used with any
373 378 # frontend.
374 379 super(TerminalInteractiveShell, self).init_alias()
375 380
376 381 # Now define aliases that only make sense on the terminal, because they
377 382 # need direct access to the console in a way that we can't emulate in
378 383 # GUI or web frontend
379 384 if os.name == 'posix':
380 385 for cmd in ['clear', 'more', 'less', 'man']:
381 386 self.alias_manager.soft_define_alias(cmd, cmd)
382 387
383 388
384 389 def __init__(self, *args, **kwargs):
385 390 super(TerminalInteractiveShell, self).__init__(*args, **kwargs)
386 391 self.init_prompt_toolkit_cli()
387 392 self.init_term_title()
388 393 self.keep_running = True
389 394
390 395 self.debugger_history = InMemoryHistory()
391 396
392 397 def ask_exit(self):
393 398 self.keep_running = False
394 399
395 400 rl_next_input = None
396 401
397 402 def pre_prompt(self):
398 403 if self.rl_next_input:
399 404 self.pt_cli.application.buffer.text = cast_unicode_py2(self.rl_next_input)
400 405 self.rl_next_input = None
401 406
402 407 def interact(self, display_banner=DISPLAY_BANNER_DEPRECATED):
403 408
404 409 if display_banner is not DISPLAY_BANNER_DEPRECATED:
405 410 warn('interact `display_banner` argument is deprecated since IPython 5.0. Call `show_banner()` if needed.', DeprecationWarning, stacklevel=2)
406 411
407 412 while self.keep_running:
408 413 print(self.separate_in, end='')
409 414
410 415 try:
411 416 code = self.prompt_for_code()
412 417 except EOFError:
413 418 if (not self.confirm_exit) \
414 419 or self.ask_yes_no('Do you really want to exit ([y]/n)?','y','n'):
415 420 self.ask_exit()
416 421
417 422 else:
418 423 if code:
419 424 self.run_cell(code, store_history=True)
420 425
421 426 def mainloop(self, display_banner=DISPLAY_BANNER_DEPRECATED):
422 427 # An extra layer of protection in case someone mashing Ctrl-C breaks
423 428 # out of our internal code.
424 429 if display_banner is not DISPLAY_BANNER_DEPRECATED:
425 430 warn('mainloop `display_banner` argument is deprecated since IPython 5.0. Call `show_banner()` if needed.', DeprecationWarning, stacklevel=2)
426 431 while True:
427 432 try:
428 433 self.interact()
429 434 break
430 435 except KeyboardInterrupt:
431 436 print("\nKeyboardInterrupt escaped interact()\n")
432 437
433 438 if hasattr(self, '_eventloop'):
434 439 self._eventloop.close()
435 440
436 441 _inputhook = None
437 442 def inputhook(self, context):
438 443 if self._inputhook is not None:
439 444 self._inputhook(context)
440 445
441 446 def enable_gui(self, gui=None):
442 447 if gui:
443 448 self._inputhook = get_inputhook_func(gui)
444 449 else:
445 450 self._inputhook = None
446 451
447 452 # Run !system commands directly, not through pipes, so terminal programs
448 453 # work correctly.
449 454 system = InteractiveShell.system_raw
450 455
451 456 def auto_rewrite_input(self, cmd):
452 457 """Overridden from the parent class to use fancy rewriting prompt"""
453 458 if not self.show_rewritten_input:
454 459 return
455 460
456 461 tokens = self.prompts.rewrite_prompt_tokens()
457 462 if self.pt_cli:
458 463 self.pt_cli.print_tokens(tokens)
459 464 print(cmd)
460 465 else:
461 466 prompt = ''.join(s for t, s in tokens)
462 467 print(prompt, cmd, sep='')
463 468
464 469 _prompts_before = None
465 470 def switch_doctest_mode(self, mode):
466 471 """Switch prompts to classic for %doctest_mode"""
467 472 if mode:
468 473 self._prompts_before = self.prompts
469 474 self.prompts = ClassicPrompts(self)
470 475 elif self._prompts_before:
471 476 self.prompts = self._prompts_before
472 477 self._prompts_before = None
473 478 self._update_layout()
474 479
475 480
476 481 InteractiveShellABC.register(TerminalInteractiveShell)
477 482
478 483 if __name__ == '__main__':
479 484 TerminalInteractiveShell.instance().interact()
@@ -1,151 +1,138 b''
1 1 """Global IPython app to support test running.
2 2
3 3 We must start our own ipython object and heavily muck with it so that all the
4 4 modifications IPython makes to system behavior don't send the doctest machinery
5 5 into a fit. This code should be considered a gross hack, but it gets the job
6 6 done.
7 7 """
8 8 from __future__ import absolute_import
9 9 from __future__ import print_function
10 10
11 #-----------------------------------------------------------------------------
12 # Copyright (C) 2009-2011 The IPython Development Team
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
11 # Copyright (c) IPython Development Team.
12 # Distributed under the terms of the Modified BSD License.
13
23 14 import sys
15 import warnings
24 16
25 # our own
26 17 from . import tools
27 18
28 19 from IPython.core import page
29 20 from IPython.utils import io
30 21 from IPython.utils import py3compat
31 22 from IPython.utils.py3compat import builtin_mod
32 23 from IPython.terminal.interactiveshell import TerminalInteractiveShell
33 24
34 #-----------------------------------------------------------------------------
35 # Functions
36 #-----------------------------------------------------------------------------
37 25
38 26 class StreamProxy(io.IOStream):
39 27 """Proxy for sys.stdout/err. This will request the stream *at call time*
40 28 allowing for nose's Capture plugin's redirection of sys.stdout/err.
41 29
42 30 Parameters
43 31 ----------
44 32 name : str
45 33 The name of the stream. This will be requested anew at every call
46 34 """
47 35
48 36 def __init__(self, name):
37 warnings.warn("StreamProxy is deprecated and unused as of IPython 5", DeprecationWarning,
38 stacklevel=2,
39 )
49 40 self.name=name
50 41
51 42 @property
52 43 def stream(self):
53 44 return getattr(sys, self.name)
54 45
55 46 def flush(self):
56 47 self.stream.flush()
57 48
58 49
59 50 def get_ipython():
60 51 # This will get replaced by the real thing once we start IPython below
61 52 return start_ipython()
62 53
63 54
64 55 # A couple of methods to override those in the running IPython to interact
65 56 # better with doctest (doctest captures on raw stdout, so we need to direct
66 57 # various types of output there otherwise it will miss them).
67 58
68 59 def xsys(self, cmd):
69 60 """Replace the default system call with a capturing one for doctest.
70 61 """
71 62 # We use getoutput, but we need to strip it because pexpect captures
72 63 # the trailing newline differently from commands.getoutput
73 64 print(self.getoutput(cmd, split=False, depth=1).rstrip(), end='', file=sys.stdout)
74 65 sys.stdout.flush()
75 66
76 67
77 68 def _showtraceback(self, etype, evalue, stb):
78 69 """Print the traceback purely on stdout for doctest to capture it.
79 70 """
80 71 print(self.InteractiveTB.stb2text(stb), file=sys.stdout)
81 72
82 73
83 74 def start_ipython():
84 75 """Start a global IPython shell, which we need for IPython-specific syntax.
85 76 """
86 77 global get_ipython
87 78
88 79 # This function should only ever run once!
89 80 if hasattr(start_ipython, 'already_called'):
90 81 return
91 82 start_ipython.already_called = True
92 83
93 84 # Store certain global objects that IPython modifies
94 85 _displayhook = sys.displayhook
95 86 _excepthook = sys.excepthook
96 87 _main = sys.modules.get('__main__')
97 88
98 89 # Create custom argv and namespaces for our IPython to be test-friendly
99 90 config = tools.default_config()
100 91 config.TerminalInteractiveShell.simple_prompt = True
101 92
102 93 # Create and initialize our test-friendly IPython instance.
103 94 shell = TerminalInteractiveShell.instance(config=config,
104 95 )
105 96
106 97 # A few more tweaks needed for playing nicely with doctests...
107 98
108 99 # remove history file
109 100 shell.tempfiles.append(config.HistoryManager.hist_file)
110 101
111 102 # These traps are normally only active for interactive use, set them
112 103 # permanently since we'll be mocking interactive sessions.
113 104 shell.builtin_trap.activate()
114 105
115 106 # Modify the IPython system call with one that uses getoutput, so that we
116 107 # can capture subcommands and print them to Python's stdout, otherwise the
117 108 # doctest machinery would miss them.
118 109 shell.system = py3compat.MethodType(xsys, shell)
119 110
120 111 shell._showtraceback = py3compat.MethodType(_showtraceback, shell)
121 112
122 113 # IPython is ready, now clean up some global state...
123 114
124 115 # Deactivate the various python system hooks added by ipython for
125 116 # interactive convenience so we don't confuse the doctest system
126 117 sys.modules['__main__'] = _main
127 118 sys.displayhook = _displayhook
128 119 sys.excepthook = _excepthook
129 120
130 121 # So that ipython magics and aliases can be doctested (they work by making
131 122 # a call into a global _ip object). Also make the top-level get_ipython
132 123 # now return this without recursively calling here again.
133 124 _ip = shell
134 125 get_ipython = _ip.get_ipython
135 126 builtin_mod._ip = _ip
136 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 129 # Override paging, so we don't require user interaction during the tests.
143 130 def nopage(strng, start=0, screen_lines=0, pager_cmd=None):
144 131 if isinstance(strng, dict):
145 132 strng = strng.get('text/plain', '')
146 133 print(strng)
147 134
148 135 page.orig_page = page.pager_page
149 136 page.pager_page = nopage
150 137
151 138 return _ip
@@ -1,235 +1,241 b''
1 1 # encoding: utf-8
2 2 """
3 3 IO related utilities.
4 4 """
5 5
6 6 # Copyright (c) IPython Development Team.
7 7 # Distributed under the terms of the Modified BSD License.
8 8
9 9 from __future__ import print_function
10 10 from __future__ import absolute_import
11 11
12 12
13 13 import atexit
14 14 import os
15 15 import sys
16 16 import tempfile
17 import warnings
17 18 from warnings import warn
18 19
19 20 from IPython.utils.decorators import undoc
20 21 from .capture import CapturedIO, capture_output
21 22 from .py3compat import string_types, input, PY3
22 23
23 24 @undoc
24 25 class IOStream:
25 26
26 27 def __init__(self, stream, fallback=None):
27 28 warn('IOStream is deprecated since IPython 5.0, use sys.{stdin,stdout,stderr} instead',
28 29 DeprecationWarning, stacklevel=2)
29 30 if not hasattr(stream,'write') or not hasattr(stream,'flush'):
30 31 if fallback is not None:
31 32 stream = fallback
32 33 else:
33 34 raise ValueError("fallback required, but not specified")
34 35 self.stream = stream
35 36 self._swrite = stream.write
36 37
37 38 # clone all methods not overridden:
38 39 def clone(meth):
39 40 return not hasattr(self, meth) and not meth.startswith('_')
40 41 for meth in filter(clone, dir(stream)):
41 42 setattr(self, meth, getattr(stream, meth))
42 43
43 44 def __repr__(self):
44 45 cls = self.__class__
45 46 tpl = '{mod}.{cls}({args})'
46 47 return tpl.format(mod=cls.__module__, cls=cls.__name__, args=self.stream)
47 48
48 49 def write(self,data):
49 50 warn('IOStream is deprecated since IPython 5.0, use sys.{stdin,stdout,stderr} instead',
50 51 DeprecationWarning, stacklevel=2)
51 52 try:
52 53 self._swrite(data)
53 54 except:
54 55 try:
55 56 # print handles some unicode issues which may trip a plain
56 57 # write() call. Emulate write() by using an empty end
57 58 # argument.
58 59 print(data, end='', file=self.stream)
59 60 except:
60 61 # if we get here, something is seriously broken.
61 62 print('ERROR - failed to write data to stream:', self.stream,
62 63 file=sys.stderr)
63 64
64 65 def writelines(self, lines):
65 66 warn('IOStream is deprecated since IPython 5.0, use sys.{stdin,stdout,stderr} instead',
66 67 DeprecationWarning, stacklevel=2)
67 68 if isinstance(lines, string_types):
68 69 lines = [lines]
69 70 for line in lines:
70 71 self.write(line)
71 72
72 73 # This class used to have a writeln method, but regular files and streams
73 74 # in Python don't have this method. We need to keep this completely
74 75 # compatible so we removed it.
75 76
76 77 @property
77 78 def closed(self):
78 79 return self.stream.closed
79 80
80 81 def close(self):
81 82 pass
82 83
83 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 86 atexit.register(devnull.close)
86 stdin = IOStream(sys.stdin, fallback=devnull)
87 stdout = IOStream(sys.stdout, fallback=devnull)
88 stderr = IOStream(sys.stderr, fallback=devnull)
87
88 # io.std* are deprecated, but don't show our own deprecation warnings
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 96 class Tee(object):
91 97 """A class to duplicate an output stream to stdout/err.
92 98
93 99 This works in a manner very similar to the Unix 'tee' command.
94 100
95 101 When the object is closed or deleted, it closes the original file given to
96 102 it for duplication.
97 103 """
98 104 # Inspired by:
99 105 # http://mail.python.org/pipermail/python-list/2007-May/442737.html
100 106
101 107 def __init__(self, file_or_name, mode="w", channel='stdout'):
102 108 """Construct a new Tee object.
103 109
104 110 Parameters
105 111 ----------
106 112 file_or_name : filename or open filehandle (writable)
107 113 File that will be duplicated
108 114
109 115 mode : optional, valid mode for open().
110 116 If a filename was give, open with this mode.
111 117
112 118 channel : str, one of ['stdout', 'stderr']
113 119 """
114 120 if channel not in ['stdout', 'stderr']:
115 121 raise ValueError('Invalid channel spec %s' % channel)
116 122
117 123 if hasattr(file_or_name, 'write') and hasattr(file_or_name, 'seek'):
118 124 self.file = file_or_name
119 125 else:
120 126 self.file = open(file_or_name, mode)
121 127 self.channel = channel
122 128 self.ostream = getattr(sys, channel)
123 129 setattr(sys, channel, self)
124 130 self._closed = False
125 131
126 132 def close(self):
127 133 """Close the file and restore the channel."""
128 134 self.flush()
129 135 setattr(sys, self.channel, self.ostream)
130 136 self.file.close()
131 137 self._closed = True
132 138
133 139 def write(self, data):
134 140 """Write data to both channels."""
135 141 self.file.write(data)
136 142 self.ostream.write(data)
137 143 self.ostream.flush()
138 144
139 145 def flush(self):
140 146 """Flush both channels."""
141 147 self.file.flush()
142 148 self.ostream.flush()
143 149
144 150 def __del__(self):
145 151 if not self._closed:
146 152 self.close()
147 153
148 154
149 155 def ask_yes_no(prompt, default=None, interrupt=None):
150 156 """Asks a question and returns a boolean (y/n) answer.
151 157
152 158 If default is given (one of 'y','n'), it is used if the user input is
153 159 empty. If interrupt is given (one of 'y','n'), it is used if the user
154 160 presses Ctrl-C. Otherwise the question is repeated until an answer is
155 161 given.
156 162
157 163 An EOF is treated as the default answer. If there is no default, an
158 164 exception is raised to prevent infinite loops.
159 165
160 166 Valid answers are: y/yes/n/no (match is not case sensitive)."""
161 167
162 168 answers = {'y':True,'n':False,'yes':True,'no':False}
163 169 ans = None
164 170 while ans not in answers.keys():
165 171 try:
166 172 ans = input(prompt+' ').lower()
167 173 if not ans: # response was an empty string
168 174 ans = default
169 175 except KeyboardInterrupt:
170 176 if interrupt:
171 177 ans = interrupt
172 178 except EOFError:
173 179 if default in answers.keys():
174 180 ans = default
175 181 print()
176 182 else:
177 183 raise
178 184
179 185 return answers[ans]
180 186
181 187
182 188 def temp_pyfile(src, ext='.py'):
183 189 """Make a temporary python file, return filename and filehandle.
184 190
185 191 Parameters
186 192 ----------
187 193 src : string or list of strings (no need for ending newlines if list)
188 194 Source code to be written to the file.
189 195
190 196 ext : optional, string
191 197 Extension for the generated file.
192 198
193 199 Returns
194 200 -------
195 201 (filename, open filehandle)
196 202 It is the caller's responsibility to close the open file and unlink it.
197 203 """
198 204 fname = tempfile.mkstemp(ext)[1]
199 205 f = open(fname,'w')
200 206 f.write(src)
201 207 f.flush()
202 208 return fname, f
203 209
204 210 def atomic_writing(*args, **kwargs):
205 211 """DEPRECATED: moved to notebook.services.contents.fileio"""
206 212 warn("IPython.utils.io.atomic_writing has moved to notebook.services.contents.fileio")
207 213 from notebook.services.contents.fileio import atomic_writing
208 214 return atomic_writing(*args, **kwargs)
209 215
210 216 def raw_print(*args, **kw):
211 217 """Raw print to sys.__stdout__, otherwise identical interface to print()."""
212 218
213 219 print(*args, sep=kw.get('sep', ' '), end=kw.get('end', '\n'),
214 220 file=sys.__stdout__)
215 221 sys.__stdout__.flush()
216 222
217 223
218 224 def raw_print_err(*args, **kw):
219 225 """Raw print to sys.__stderr__, otherwise identical interface to print()."""
220 226
221 227 print(*args, sep=kw.get('sep', ' '), end=kw.get('end', '\n'),
222 228 file=sys.__stderr__)
223 229 sys.__stderr__.flush()
224 230
225 231
226 232 # Short aliases for quick debugging, do NOT use these in production code.
227 233 rprint = raw_print
228 234 rprinte = raw_print_err
229 235
230 236
231 237 def unicode_std_stream(stream='stdout'):
232 238 """DEPRECATED, moved to nbconvert.utils.io"""
233 239 warn("IPython.utils.io.unicode_std_stream has moved to nbconvert.utils.io")
234 240 from nbconvert.utils.io import unicode_std_stream
235 241 return unicode_std_stream(stream)
@@ -1,65 +1,65 b''
1 1 # encoding: utf-8
2 2 """
3 3 Utilities for warnings. Shoudn't we just use the built in warnings module.
4 4 """
5 5
6 6 # Copyright (c) IPython Development Team.
7 7 # Distributed under the terms of the Modified BSD License.
8 8
9 9 from __future__ import print_function
10 10
11 11 import sys
12 12 import warnings
13 13
14 14 warnings.warn("The module IPython.utils.warn is deprecated since IPython 4.0, use the standard warnings module instead", DeprecationWarning)
15 15
16 16 def warn(msg,level=2,exit_val=1):
17 17 """Deprecated
18 18
19 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 23 Options:
24 24
25 25 -level(2): allows finer control:
26 26 0 -> Do nothing, dummy function.
27 27 1 -> Print message.
28 28 2 -> Print 'WARNING:' + message. (Default level).
29 29 3 -> Print 'ERROR:' + message.
30 30 4 -> Print 'FATAL ERROR:' + message and trigger a sys.exit(exit_val).
31 31
32 32 -exit_val (1): exit value returned by sys.exit() for a level 4
33 33 warning. Ignored for all other levels."""
34 34
35 35 warnings.warn("The module IPython.utils.warn is deprecated since IPython 4.0, use the standard warnings module instead", DeprecationWarning)
36 36 if level>0:
37 37 header = ['','','WARNING: ','ERROR: ','FATAL ERROR: ']
38 38 print(header[level], msg, sep='', file=sys.stderr)
39 39 if level == 4:
40 40 print('Exiting.\n', file=sys.stderr)
41 41 sys.exit(exit_val)
42 42
43 43
44 44 def info(msg):
45 45 """Deprecated
46 46
47 47 Equivalent to warn(msg,level=1)."""
48 48
49 49 warn(msg,level=1)
50 50
51 51
52 52 def error(msg):
53 53 """Deprecated
54 54
55 55 Equivalent to warn(msg,level=3)."""
56 56
57 57 warn(msg,level=3)
58 58
59 59
60 60 def fatal(msg,exit_val=1):
61 61 """Deprecated
62 62
63 63 Equivalent to warn(msg,exit_val=exit_val,level=4)."""
64 64
65 65 warn(msg,exit_val=exit_val,level=4)
General Comments 0
You need to be logged in to leave comments. Login now