##// END OF EJS Templates
support negative indexing
Matthias Bussonnier -
Show More
@@ -1,795 +1,797 b''
1 """Input transformer machinery to support IPython special syntax.
1 """Input transformer machinery to support IPython special syntax.
2
2
3 This includes the machinery to recognise and transform ``%magic`` commands,
3 This includes the machinery to recognise and transform ``%magic`` commands,
4 ``!system`` commands, ``help?`` querying, prompt stripping, and so forth.
4 ``!system`` commands, ``help?`` querying, prompt stripping, and so forth.
5
5
6 Added: IPython 7.0. Replaces inputsplitter and inputtransformer which were
6 Added: IPython 7.0. Replaces inputsplitter and inputtransformer which were
7 deprecated in 7.0.
7 deprecated in 7.0.
8 """
8 """
9
9
10 # Copyright (c) IPython Development Team.
10 # Copyright (c) IPython Development Team.
11 # Distributed under the terms of the Modified BSD License.
11 # Distributed under the terms of the Modified BSD License.
12
12
13 import ast
13 import ast
14 from codeop import CommandCompiler, Compile
14 from codeop import CommandCompiler, Compile
15 import re
15 import re
16 import tokenize
16 import tokenize
17 from typing import List, Tuple, Optional, Any
17 from typing import List, Tuple, Optional, Any
18 import warnings
18 import warnings
19
19
20 _indent_re = re.compile(r'^[ \t]+')
20 _indent_re = re.compile(r'^[ \t]+')
21
21
22 def leading_empty_lines(lines):
22 def leading_empty_lines(lines):
23 """Remove leading empty lines
23 """Remove leading empty lines
24
24
25 If the leading lines are empty or contain only whitespace, they will be
25 If the leading lines are empty or contain only whitespace, they will be
26 removed.
26 removed.
27 """
27 """
28 if not lines:
28 if not lines:
29 return lines
29 return lines
30 for i, line in enumerate(lines):
30 for i, line in enumerate(lines):
31 if line and not line.isspace():
31 if line and not line.isspace():
32 return lines[i:]
32 return lines[i:]
33 return lines
33 return lines
34
34
35 def leading_indent(lines):
35 def leading_indent(lines):
36 """Remove leading indentation.
36 """Remove leading indentation.
37
37
38 If the first line starts with a spaces or tabs, the same whitespace will be
38 If the first line starts with a spaces or tabs, the same whitespace will be
39 removed from each following line in the cell.
39 removed from each following line in the cell.
40 """
40 """
41 if not lines:
41 if not lines:
42 return lines
42 return lines
43 m = _indent_re.match(lines[0])
43 m = _indent_re.match(lines[0])
44 if not m:
44 if not m:
45 return lines
45 return lines
46 space = m.group(0)
46 space = m.group(0)
47 n = len(space)
47 n = len(space)
48 return [l[n:] if l.startswith(space) else l
48 return [l[n:] if l.startswith(space) else l
49 for l in lines]
49 for l in lines]
50
50
51 class PromptStripper:
51 class PromptStripper:
52 """Remove matching input prompts from a block of input.
52 """Remove matching input prompts from a block of input.
53
53
54 Parameters
54 Parameters
55 ----------
55 ----------
56 prompt_re : regular expression
56 prompt_re : regular expression
57 A regular expression matching any input prompt (including continuation,
57 A regular expression matching any input prompt (including continuation,
58 e.g. ``...``)
58 e.g. ``...``)
59 initial_re : regular expression, optional
59 initial_re : regular expression, optional
60 A regular expression matching only the initial prompt, but not continuation.
60 A regular expression matching only the initial prompt, but not continuation.
61 If no initial expression is given, prompt_re will be used everywhere.
61 If no initial expression is given, prompt_re will be used everywhere.
62 Used mainly for plain Python prompts (``>>>``), where the continuation prompt
62 Used mainly for plain Python prompts (``>>>``), where the continuation prompt
63 ``...`` is a valid Python expression in Python 3, so shouldn't be stripped.
63 ``...`` is a valid Python expression in Python 3, so shouldn't be stripped.
64
64
65 Notes
65 Notes
66 -----
66 -----
67
67
68 If initial_re and prompt_re differ,
68 If initial_re and prompt_re differ,
69 only initial_re will be tested against the first line.
69 only initial_re will be tested against the first line.
70 If any prompt is found on the first two lines,
70 If any prompt is found on the first two lines,
71 prompts will be stripped from the rest of the block.
71 prompts will be stripped from the rest of the block.
72 """
72 """
73 def __init__(self, prompt_re, initial_re=None):
73 def __init__(self, prompt_re, initial_re=None):
74 self.prompt_re = prompt_re
74 self.prompt_re = prompt_re
75 self.initial_re = initial_re or prompt_re
75 self.initial_re = initial_re or prompt_re
76
76
77 def _strip(self, lines):
77 def _strip(self, lines):
78 return [self.prompt_re.sub('', l, count=1) for l in lines]
78 return [self.prompt_re.sub('', l, count=1) for l in lines]
79
79
80 def __call__(self, lines):
80 def __call__(self, lines):
81 if not lines:
81 if not lines:
82 return lines
82 return lines
83 if self.initial_re.match(lines[0]) or \
83 if self.initial_re.match(lines[0]) or \
84 (len(lines) > 1 and self.prompt_re.match(lines[1])):
84 (len(lines) > 1 and self.prompt_re.match(lines[1])):
85 return self._strip(lines)
85 return self._strip(lines)
86 return lines
86 return lines
87
87
88 classic_prompt = PromptStripper(
88 classic_prompt = PromptStripper(
89 prompt_re=re.compile(r'^(>>>|\.\.\.)( |$)'),
89 prompt_re=re.compile(r'^(>>>|\.\.\.)( |$)'),
90 initial_re=re.compile(r'^>>>( |$)')
90 initial_re=re.compile(r'^>>>( |$)')
91 )
91 )
92
92
93 ipython_prompt = PromptStripper(
93 ipython_prompt = PromptStripper(
94 re.compile(
94 re.compile(
95 r"""
95 r"""
96 ^( # Match from the beginning of a line, either:
96 ^( # Match from the beginning of a line, either:
97
97
98 # 1. First-line prompt:
98 # 1. First-line prompt:
99 ((\[nav\]|\[ins\])?\ )? # Vi editing mode prompt, if it's there
99 ((\[nav\]|\[ins\])?\ )? # Vi editing mode prompt, if it's there
100 In\ # The 'In' of the prompt, with a space
100 In\ # The 'In' of the prompt, with a space
101 \[\d+\]: # Command index, as displayed in the prompt
101 \[\d+\]: # Command index, as displayed in the prompt
102 \ # With a mandatory trailing space
102 \ # With a mandatory trailing space
103
103
104 | # ... or ...
104 | # ... or ...
105
105
106 # 2. The three dots of the multiline prompt
106 # 2. The three dots of the multiline prompt
107 \s* # All leading whitespace characters
107 \s* # All leading whitespace characters
108 \.{3,}: # The three (or more) dots
108 \.{3,}: # The three (or more) dots
109 \ ? # With an optional trailing space
109 \ ? # With an optional trailing space
110
110
111 )
111 )
112 """,
112 """,
113 re.VERBOSE,
113 re.VERBOSE,
114 )
114 )
115 )
115 )
116
116
117
117
118 def cell_magic(lines):
118 def cell_magic(lines):
119 if not lines or not lines[0].startswith('%%'):
119 if not lines or not lines[0].startswith('%%'):
120 return lines
120 return lines
121 if re.match(r'%%\w+\?', lines[0]):
121 if re.match(r'%%\w+\?', lines[0]):
122 # This case will be handled by help_end
122 # This case will be handled by help_end
123 return lines
123 return lines
124 magic_name, _, first_line = lines[0][2:].rstrip().partition(' ')
124 magic_name, _, first_line = lines[0][2:].rstrip().partition(' ')
125 body = ''.join(lines[1:])
125 body = ''.join(lines[1:])
126 return ['get_ipython().run_cell_magic(%r, %r, %r)\n'
126 return ['get_ipython().run_cell_magic(%r, %r, %r)\n'
127 % (magic_name, first_line, body)]
127 % (magic_name, first_line, body)]
128
128
129
129
130 def _find_assign_op(token_line) -> Optional[int]:
130 def _find_assign_op(token_line) -> Optional[int]:
131 """Get the index of the first assignment in the line ('=' not inside brackets)
131 """Get the index of the first assignment in the line ('=' not inside brackets)
132
132
133 Note: We don't try to support multiple special assignment (a = b = %foo)
133 Note: We don't try to support multiple special assignment (a = b = %foo)
134 """
134 """
135 paren_level = 0
135 paren_level = 0
136 for i, ti in enumerate(token_line):
136 for i, ti in enumerate(token_line):
137 s = ti.string
137 s = ti.string
138 if s == '=' and paren_level == 0:
138 if s == '=' and paren_level == 0:
139 return i
139 return i
140 if s in {'(','[','{'}:
140 if s in {'(','[','{'}:
141 paren_level += 1
141 paren_level += 1
142 elif s in {')', ']', '}'}:
142 elif s in {')', ']', '}'}:
143 if paren_level > 0:
143 if paren_level > 0:
144 paren_level -= 1
144 paren_level -= 1
145 return None
145 return None
146
146
147 def find_end_of_continued_line(lines, start_line: int):
147 def find_end_of_continued_line(lines, start_line: int):
148 """Find the last line of a line explicitly extended using backslashes.
148 """Find the last line of a line explicitly extended using backslashes.
149
149
150 Uses 0-indexed line numbers.
150 Uses 0-indexed line numbers.
151 """
151 """
152 end_line = start_line
152 end_line = start_line
153 while lines[end_line].endswith('\\\n'):
153 while lines[end_line].endswith('\\\n'):
154 end_line += 1
154 end_line += 1
155 if end_line >= len(lines):
155 if end_line >= len(lines):
156 break
156 break
157 return end_line
157 return end_line
158
158
159 def assemble_continued_line(lines, start: Tuple[int, int], end_line: int):
159 def assemble_continued_line(lines, start: Tuple[int, int], end_line: int):
160 r"""Assemble a single line from multiple continued line pieces
160 r"""Assemble a single line from multiple continued line pieces
161
161
162 Continued lines are lines ending in ``\``, and the line following the last
162 Continued lines are lines ending in ``\``, and the line following the last
163 ``\`` in the block.
163 ``\`` in the block.
164
164
165 For example, this code continues over multiple lines::
165 For example, this code continues over multiple lines::
166
166
167 if (assign_ix is not None) \
167 if (assign_ix is not None) \
168 and (len(line) >= assign_ix + 2) \
168 and (len(line) >= assign_ix + 2) \
169 and (line[assign_ix+1].string == '%') \
169 and (line[assign_ix+1].string == '%') \
170 and (line[assign_ix+2].type == tokenize.NAME):
170 and (line[assign_ix+2].type == tokenize.NAME):
171
171
172 This statement contains four continued line pieces.
172 This statement contains four continued line pieces.
173 Assembling these pieces into a single line would give::
173 Assembling these pieces into a single line would give::
174
174
175 if (assign_ix is not None) and (len(line) >= assign_ix + 2) and (line[...
175 if (assign_ix is not None) and (len(line) >= assign_ix + 2) and (line[...
176
176
177 This uses 0-indexed line numbers. *start* is (lineno, colno).
177 This uses 0-indexed line numbers. *start* is (lineno, colno).
178
178
179 Used to allow ``%magic`` and ``!system`` commands to be continued over
179 Used to allow ``%magic`` and ``!system`` commands to be continued over
180 multiple lines.
180 multiple lines.
181 """
181 """
182 parts = [lines[start[0]][start[1]:]] + lines[start[0]+1:end_line+1]
182 parts = [lines[start[0]][start[1]:]] + lines[start[0]+1:end_line+1]
183 return ' '.join([p.rstrip()[:-1] for p in parts[:-1]] # Strip backslash+newline
183 return ' '.join([p.rstrip()[:-1] for p in parts[:-1]] # Strip backslash+newline
184 + [parts[-1].rstrip()]) # Strip newline from last line
184 + [parts[-1].rstrip()]) # Strip newline from last line
185
185
186 class TokenTransformBase:
186 class TokenTransformBase:
187 """Base class for transformations which examine tokens.
187 """Base class for transformations which examine tokens.
188
188
189 Special syntax should not be transformed when it occurs inside strings or
189 Special syntax should not be transformed when it occurs inside strings or
190 comments. This is hard to reliably avoid with regexes. The solution is to
190 comments. This is hard to reliably avoid with regexes. The solution is to
191 tokenise the code as Python, and recognise the special syntax in the tokens.
191 tokenise the code as Python, and recognise the special syntax in the tokens.
192
192
193 IPython's special syntax is not valid Python syntax, so tokenising may go
193 IPython's special syntax is not valid Python syntax, so tokenising may go
194 wrong after the special syntax starts. These classes therefore find and
194 wrong after the special syntax starts. These classes therefore find and
195 transform *one* instance of special syntax at a time into regular Python
195 transform *one* instance of special syntax at a time into regular Python
196 syntax. After each transformation, tokens are regenerated to find the next
196 syntax. After each transformation, tokens are regenerated to find the next
197 piece of special syntax.
197 piece of special syntax.
198
198
199 Subclasses need to implement one class method (find)
199 Subclasses need to implement one class method (find)
200 and one regular method (transform).
200 and one regular method (transform).
201
201
202 The priority attribute can select which transformation to apply if multiple
202 The priority attribute can select which transformation to apply if multiple
203 transformers match in the same place. Lower numbers have higher priority.
203 transformers match in the same place. Lower numbers have higher priority.
204 This allows "%magic?" to be turned into a help call rather than a magic call.
204 This allows "%magic?" to be turned into a help call rather than a magic call.
205 """
205 """
206 # Lower numbers -> higher priority (for matches in the same location)
206 # Lower numbers -> higher priority (for matches in the same location)
207 priority = 10
207 priority = 10
208
208
209 def sortby(self):
209 def sortby(self):
210 return self.start_line, self.start_col, self.priority
210 return self.start_line, self.start_col, self.priority
211
211
212 def __init__(self, start):
212 def __init__(self, start):
213 self.start_line = start[0] - 1 # Shift from 1-index to 0-index
213 self.start_line = start[0] - 1 # Shift from 1-index to 0-index
214 self.start_col = start[1]
214 self.start_col = start[1]
215
215
216 @classmethod
216 @classmethod
217 def find(cls, tokens_by_line):
217 def find(cls, tokens_by_line):
218 """Find one instance of special syntax in the provided tokens.
218 """Find one instance of special syntax in the provided tokens.
219
219
220 Tokens are grouped into logical lines for convenience,
220 Tokens are grouped into logical lines for convenience,
221 so it is easy to e.g. look at the first token of each line.
221 so it is easy to e.g. look at the first token of each line.
222 *tokens_by_line* is a list of lists of tokenize.TokenInfo objects.
222 *tokens_by_line* is a list of lists of tokenize.TokenInfo objects.
223
223
224 This should return an instance of its class, pointing to the start
224 This should return an instance of its class, pointing to the start
225 position it has found, or None if it found no match.
225 position it has found, or None if it found no match.
226 """
226 """
227 raise NotImplementedError
227 raise NotImplementedError
228
228
229 def transform(self, lines: List[str]):
229 def transform(self, lines: List[str]):
230 """Transform one instance of special syntax found by ``find()``
230 """Transform one instance of special syntax found by ``find()``
231
231
232 Takes a list of strings representing physical lines,
232 Takes a list of strings representing physical lines,
233 returns a similar list of transformed lines.
233 returns a similar list of transformed lines.
234 """
234 """
235 raise NotImplementedError
235 raise NotImplementedError
236
236
237 class MagicAssign(TokenTransformBase):
237 class MagicAssign(TokenTransformBase):
238 """Transformer for assignments from magics (a = %foo)"""
238 """Transformer for assignments from magics (a = %foo)"""
239 @classmethod
239 @classmethod
240 def find(cls, tokens_by_line):
240 def find(cls, tokens_by_line):
241 """Find the first magic assignment (a = %foo) in the cell.
241 """Find the first magic assignment (a = %foo) in the cell.
242 """
242 """
243 for line in tokens_by_line:
243 for line in tokens_by_line:
244 assign_ix = _find_assign_op(line)
244 assign_ix = _find_assign_op(line)
245 if (assign_ix is not None) \
245 if (assign_ix is not None) \
246 and (len(line) >= assign_ix + 2) \
246 and (len(line) >= assign_ix + 2) \
247 and (line[assign_ix+1].string == '%') \
247 and (line[assign_ix+1].string == '%') \
248 and (line[assign_ix+2].type == tokenize.NAME):
248 and (line[assign_ix+2].type == tokenize.NAME):
249 return cls(line[assign_ix+1].start)
249 return cls(line[assign_ix+1].start)
250
250
251 def transform(self, lines: List[str]):
251 def transform(self, lines: List[str]):
252 """Transform a magic assignment found by the ``find()`` classmethod.
252 """Transform a magic assignment found by the ``find()`` classmethod.
253 """
253 """
254 start_line, start_col = self.start_line, self.start_col
254 start_line, start_col = self.start_line, self.start_col
255 lhs = lines[start_line][:start_col]
255 lhs = lines[start_line][:start_col]
256 end_line = find_end_of_continued_line(lines, start_line)
256 end_line = find_end_of_continued_line(lines, start_line)
257 rhs = assemble_continued_line(lines, (start_line, start_col), end_line)
257 rhs = assemble_continued_line(lines, (start_line, start_col), end_line)
258 assert rhs.startswith('%'), rhs
258 assert rhs.startswith('%'), rhs
259 magic_name, _, args = rhs[1:].partition(' ')
259 magic_name, _, args = rhs[1:].partition(' ')
260
260
261 lines_before = lines[:start_line]
261 lines_before = lines[:start_line]
262 call = "get_ipython().run_line_magic({!r}, {!r})".format(magic_name, args)
262 call = "get_ipython().run_line_magic({!r}, {!r})".format(magic_name, args)
263 new_line = lhs + call + '\n'
263 new_line = lhs + call + '\n'
264 lines_after = lines[end_line+1:]
264 lines_after = lines[end_line+1:]
265
265
266 return lines_before + [new_line] + lines_after
266 return lines_before + [new_line] + lines_after
267
267
268
268
269 class SystemAssign(TokenTransformBase):
269 class SystemAssign(TokenTransformBase):
270 """Transformer for assignments from system commands (a = !foo)"""
270 """Transformer for assignments from system commands (a = !foo)"""
271 @classmethod
271 @classmethod
272 def find(cls, tokens_by_line):
272 def find(cls, tokens_by_line):
273 """Find the first system assignment (a = !foo) in the cell.
273 """Find the first system assignment (a = !foo) in the cell.
274 """
274 """
275 for line in tokens_by_line:
275 for line in tokens_by_line:
276 assign_ix = _find_assign_op(line)
276 assign_ix = _find_assign_op(line)
277 if (assign_ix is not None) \
277 if (assign_ix is not None) \
278 and not line[assign_ix].line.strip().startswith('=') \
278 and not line[assign_ix].line.strip().startswith('=') \
279 and (len(line) >= assign_ix + 2) \
279 and (len(line) >= assign_ix + 2) \
280 and (line[assign_ix + 1].type == tokenize.ERRORTOKEN):
280 and (line[assign_ix + 1].type == tokenize.ERRORTOKEN):
281 ix = assign_ix + 1
281 ix = assign_ix + 1
282
282
283 while ix < len(line) and line[ix].type == tokenize.ERRORTOKEN:
283 while ix < len(line) and line[ix].type == tokenize.ERRORTOKEN:
284 if line[ix].string == '!':
284 if line[ix].string == '!':
285 return cls(line[ix].start)
285 return cls(line[ix].start)
286 elif not line[ix].string.isspace():
286 elif not line[ix].string.isspace():
287 break
287 break
288 ix += 1
288 ix += 1
289
289
290 def transform(self, lines: List[str]):
290 def transform(self, lines: List[str]):
291 """Transform a system assignment found by the ``find()`` classmethod.
291 """Transform a system assignment found by the ``find()`` classmethod.
292 """
292 """
293 start_line, start_col = self.start_line, self.start_col
293 start_line, start_col = self.start_line, self.start_col
294
294
295 lhs = lines[start_line][:start_col]
295 lhs = lines[start_line][:start_col]
296 end_line = find_end_of_continued_line(lines, start_line)
296 end_line = find_end_of_continued_line(lines, start_line)
297 rhs = assemble_continued_line(lines, (start_line, start_col), end_line)
297 rhs = assemble_continued_line(lines, (start_line, start_col), end_line)
298 assert rhs.startswith('!'), rhs
298 assert rhs.startswith('!'), rhs
299 cmd = rhs[1:]
299 cmd = rhs[1:]
300
300
301 lines_before = lines[:start_line]
301 lines_before = lines[:start_line]
302 call = "get_ipython().getoutput({!r})".format(cmd)
302 call = "get_ipython().getoutput({!r})".format(cmd)
303 new_line = lhs + call + '\n'
303 new_line = lhs + call + '\n'
304 lines_after = lines[end_line + 1:]
304 lines_after = lines[end_line + 1:]
305
305
306 return lines_before + [new_line] + lines_after
306 return lines_before + [new_line] + lines_after
307
307
308 # The escape sequences that define the syntax transformations IPython will
308 # The escape sequences that define the syntax transformations IPython will
309 # apply to user input. These can NOT be just changed here: many regular
309 # apply to user input. These can NOT be just changed here: many regular
310 # expressions and other parts of the code may use their hardcoded values, and
310 # expressions and other parts of the code may use their hardcoded values, and
311 # for all intents and purposes they constitute the 'IPython syntax', so they
311 # for all intents and purposes they constitute the 'IPython syntax', so they
312 # should be considered fixed.
312 # should be considered fixed.
313
313
314 ESC_SHELL = '!' # Send line to underlying system shell
314 ESC_SHELL = '!' # Send line to underlying system shell
315 ESC_SH_CAP = '!!' # Send line to system shell and capture output
315 ESC_SH_CAP = '!!' # Send line to system shell and capture output
316 ESC_HELP = '?' # Find information about object
316 ESC_HELP = '?' # Find information about object
317 ESC_HELP2 = '??' # Find extra-detailed information about object
317 ESC_HELP2 = '??' # Find extra-detailed information about object
318 ESC_MAGIC = '%' # Call magic function
318 ESC_MAGIC = '%' # Call magic function
319 ESC_MAGIC2 = '%%' # Call cell-magic function
319 ESC_MAGIC2 = '%%' # Call cell-magic function
320 ESC_QUOTE = ',' # Split args on whitespace, quote each as string and call
320 ESC_QUOTE = ',' # Split args on whitespace, quote each as string and call
321 ESC_QUOTE2 = ';' # Quote all args as a single string, call
321 ESC_QUOTE2 = ';' # Quote all args as a single string, call
322 ESC_PAREN = '/' # Call first argument with rest of line as arguments
322 ESC_PAREN = '/' # Call first argument with rest of line as arguments
323
323
324 ESCAPE_SINGLES = {'!', '?', '%', ',', ';', '/'}
324 ESCAPE_SINGLES = {'!', '?', '%', ',', ';', '/'}
325 ESCAPE_DOUBLES = {'!!', '??'} # %% (cell magic) is handled separately
325 ESCAPE_DOUBLES = {'!!', '??'} # %% (cell magic) is handled separately
326
326
327 def _make_help_call(target, esc):
327 def _make_help_call(target, esc):
328 """Prepares a pinfo(2)/psearch call from a target name and the escape
328 """Prepares a pinfo(2)/psearch call from a target name and the escape
329 (i.e. ? or ??)"""
329 (i.e. ? or ??)"""
330 method = 'pinfo2' if esc == '??' \
330 method = 'pinfo2' if esc == '??' \
331 else 'psearch' if '*' in target \
331 else 'psearch' if '*' in target \
332 else 'pinfo'
332 else 'pinfo'
333 arg = " ".join([method, target])
333 arg = " ".join([method, target])
334 #Prepare arguments for get_ipython().run_line_magic(magic_name, magic_args)
334 #Prepare arguments for get_ipython().run_line_magic(magic_name, magic_args)
335 t_magic_name, _, t_magic_arg_s = arg.partition(' ')
335 t_magic_name, _, t_magic_arg_s = arg.partition(' ')
336 t_magic_name = t_magic_name.lstrip(ESC_MAGIC)
336 t_magic_name = t_magic_name.lstrip(ESC_MAGIC)
337 return "get_ipython().run_line_magic(%r, %r)" % (t_magic_name, t_magic_arg_s)
337 return "get_ipython().run_line_magic(%r, %r)" % (t_magic_name, t_magic_arg_s)
338
338
339
339
340 def _tr_help(content):
340 def _tr_help(content):
341 """Translate lines escaped with: ?
341 """Translate lines escaped with: ?
342
342
343 A naked help line should fire the intro help screen (shell.show_usage())
343 A naked help line should fire the intro help screen (shell.show_usage())
344 """
344 """
345 if not content:
345 if not content:
346 return 'get_ipython().show_usage()'
346 return 'get_ipython().show_usage()'
347
347
348 return _make_help_call(content, '?')
348 return _make_help_call(content, '?')
349
349
350 def _tr_help2(content):
350 def _tr_help2(content):
351 """Translate lines escaped with: ??
351 """Translate lines escaped with: ??
352
352
353 A naked help line should fire the intro help screen (shell.show_usage())
353 A naked help line should fire the intro help screen (shell.show_usage())
354 """
354 """
355 if not content:
355 if not content:
356 return 'get_ipython().show_usage()'
356 return 'get_ipython().show_usage()'
357
357
358 return _make_help_call(content, '??')
358 return _make_help_call(content, '??')
359
359
360 def _tr_magic(content):
360 def _tr_magic(content):
361 "Translate lines escaped with a percent sign: %"
361 "Translate lines escaped with a percent sign: %"
362 name, _, args = content.partition(' ')
362 name, _, args = content.partition(' ')
363 return 'get_ipython().run_line_magic(%r, %r)' % (name, args)
363 return 'get_ipython().run_line_magic(%r, %r)' % (name, args)
364
364
365 def _tr_quote(content):
365 def _tr_quote(content):
366 "Translate lines escaped with a comma: ,"
366 "Translate lines escaped with a comma: ,"
367 name, _, args = content.partition(' ')
367 name, _, args = content.partition(' ')
368 return '%s("%s")' % (name, '", "'.join(args.split()) )
368 return '%s("%s")' % (name, '", "'.join(args.split()) )
369
369
370 def _tr_quote2(content):
370 def _tr_quote2(content):
371 "Translate lines escaped with a semicolon: ;"
371 "Translate lines escaped with a semicolon: ;"
372 name, _, args = content.partition(' ')
372 name, _, args = content.partition(' ')
373 return '%s("%s")' % (name, args)
373 return '%s("%s")' % (name, args)
374
374
375 def _tr_paren(content):
375 def _tr_paren(content):
376 "Translate lines escaped with a slash: /"
376 "Translate lines escaped with a slash: /"
377 name, _, args = content.partition(' ')
377 name, _, args = content.partition(' ')
378 return '%s(%s)' % (name, ", ".join(args.split()))
378 return '%s(%s)' % (name, ", ".join(args.split()))
379
379
380 tr = { ESC_SHELL : 'get_ipython().system({!r})'.format,
380 tr = { ESC_SHELL : 'get_ipython().system({!r})'.format,
381 ESC_SH_CAP : 'get_ipython().getoutput({!r})'.format,
381 ESC_SH_CAP : 'get_ipython().getoutput({!r})'.format,
382 ESC_HELP : _tr_help,
382 ESC_HELP : _tr_help,
383 ESC_HELP2 : _tr_help2,
383 ESC_HELP2 : _tr_help2,
384 ESC_MAGIC : _tr_magic,
384 ESC_MAGIC : _tr_magic,
385 ESC_QUOTE : _tr_quote,
385 ESC_QUOTE : _tr_quote,
386 ESC_QUOTE2 : _tr_quote2,
386 ESC_QUOTE2 : _tr_quote2,
387 ESC_PAREN : _tr_paren }
387 ESC_PAREN : _tr_paren }
388
388
389 class EscapedCommand(TokenTransformBase):
389 class EscapedCommand(TokenTransformBase):
390 """Transformer for escaped commands like %foo, !foo, or /foo"""
390 """Transformer for escaped commands like %foo, !foo, or /foo"""
391 @classmethod
391 @classmethod
392 def find(cls, tokens_by_line):
392 def find(cls, tokens_by_line):
393 """Find the first escaped command (%foo, !foo, etc.) in the cell.
393 """Find the first escaped command (%foo, !foo, etc.) in the cell.
394 """
394 """
395 for line in tokens_by_line:
395 for line in tokens_by_line:
396 if not line:
396 if not line:
397 continue
397 continue
398 ix = 0
398 ix = 0
399 ll = len(line)
399 ll = len(line)
400 while ll > ix and line[ix].type in {tokenize.INDENT, tokenize.DEDENT}:
400 while ll > ix and line[ix].type in {tokenize.INDENT, tokenize.DEDENT}:
401 ix += 1
401 ix += 1
402 if ix >= ll:
402 if ix >= ll:
403 continue
403 continue
404 if line[ix].string in ESCAPE_SINGLES:
404 if line[ix].string in ESCAPE_SINGLES:
405 return cls(line[ix].start)
405 return cls(line[ix].start)
406
406
407 def transform(self, lines):
407 def transform(self, lines):
408 """Transform an escaped line found by the ``find()`` classmethod.
408 """Transform an escaped line found by the ``find()`` classmethod.
409 """
409 """
410 start_line, start_col = self.start_line, self.start_col
410 start_line, start_col = self.start_line, self.start_col
411
411
412 indent = lines[start_line][:start_col]
412 indent = lines[start_line][:start_col]
413 end_line = find_end_of_continued_line(lines, start_line)
413 end_line = find_end_of_continued_line(lines, start_line)
414 line = assemble_continued_line(lines, (start_line, start_col), end_line)
414 line = assemble_continued_line(lines, (start_line, start_col), end_line)
415
415
416 if len(line) > 1 and line[:2] in ESCAPE_DOUBLES:
416 if len(line) > 1 and line[:2] in ESCAPE_DOUBLES:
417 escape, content = line[:2], line[2:]
417 escape, content = line[:2], line[2:]
418 else:
418 else:
419 escape, content = line[:1], line[1:]
419 escape, content = line[:1], line[1:]
420
420
421 if escape in tr:
421 if escape in tr:
422 call = tr[escape](content)
422 call = tr[escape](content)
423 else:
423 else:
424 call = ''
424 call = ''
425
425
426 lines_before = lines[:start_line]
426 lines_before = lines[:start_line]
427 new_line = indent + call + '\n'
427 new_line = indent + call + '\n'
428 lines_after = lines[end_line + 1:]
428 lines_after = lines[end_line + 1:]
429
429
430 return lines_before + [new_line] + lines_after
430 return lines_before + [new_line] + lines_after
431
431
432
432
433 _help_end_re = re.compile(
433 _help_end_re = re.compile(
434 r"""(%{0,2}
434 r"""(%{0,2}
435 (?!\d)[\w*]+ # Variable name
435 (?!\d)[\w*]+ # Variable name
436 (\.(?!\d)[\w*]+|\[[0-9]+\])* # .etc.etc or [0], we only support literal integers.
436 (\.(?!\d)[\w*]+|\[-?[0-9]+\])* # .etc.etc or [0], we only support literal integers.
437 )
437 )
438 (\?\??)$ # ? or ??
438 (\?\??)$ # ? or ??
439 """,
439 """,
440 re.VERBOSE)
440 re.VERBOSE,
441 )
442
441
443
442 class HelpEnd(TokenTransformBase):
444 class HelpEnd(TokenTransformBase):
443 """Transformer for help syntax: obj? and obj??"""
445 """Transformer for help syntax: obj? and obj??"""
444 # This needs to be higher priority (lower number) than EscapedCommand so
446 # This needs to be higher priority (lower number) than EscapedCommand so
445 # that inspecting magics (%foo?) works.
447 # that inspecting magics (%foo?) works.
446 priority = 5
448 priority = 5
447
449
448 def __init__(self, start, q_locn):
450 def __init__(self, start, q_locn):
449 super().__init__(start)
451 super().__init__(start)
450 self.q_line = q_locn[0] - 1 # Shift from 1-indexed to 0-indexed
452 self.q_line = q_locn[0] - 1 # Shift from 1-indexed to 0-indexed
451 self.q_col = q_locn[1]
453 self.q_col = q_locn[1]
452
454
453 @classmethod
455 @classmethod
454 def find(cls, tokens_by_line):
456 def find(cls, tokens_by_line):
455 """Find the first help command (foo?) in the cell.
457 """Find the first help command (foo?) in the cell.
456 """
458 """
457 for line in tokens_by_line:
459 for line in tokens_by_line:
458 # Last token is NEWLINE; look at last but one
460 # Last token is NEWLINE; look at last but one
459 if len(line) > 2 and line[-2].string == '?':
461 if len(line) > 2 and line[-2].string == '?':
460 # Find the first token that's not INDENT/DEDENT
462 # Find the first token that's not INDENT/DEDENT
461 ix = 0
463 ix = 0
462 while line[ix].type in {tokenize.INDENT, tokenize.DEDENT}:
464 while line[ix].type in {tokenize.INDENT, tokenize.DEDENT}:
463 ix += 1
465 ix += 1
464 return cls(line[ix].start, line[-2].start)
466 return cls(line[ix].start, line[-2].start)
465
467
466 def transform(self, lines):
468 def transform(self, lines):
467 """Transform a help command found by the ``find()`` classmethod.
469 """Transform a help command found by the ``find()`` classmethod.
468 """
470 """
469
471
470 piece = "".join(lines[self.start_line : self.q_line + 1])
472 piece = "".join(lines[self.start_line : self.q_line + 1])
471 indent, content = piece[: self.start_col], piece[self.start_col :]
473 indent, content = piece[: self.start_col], piece[self.start_col :]
472 lines_before = lines[: self.start_line]
474 lines_before = lines[: self.start_line]
473 lines_after = lines[self.q_line + 1 :]
475 lines_after = lines[self.q_line + 1 :]
474
476
475 m = _help_end_re.search(content)
477 m = _help_end_re.search(content)
476 if not m:
478 if not m:
477 raise SyntaxError(content)
479 raise SyntaxError(content)
478 assert m is not None, content
480 assert m is not None, content
479 target = m.group(1)
481 target = m.group(1)
480 esc = m.group(3)
482 esc = m.group(3)
481
483
482
484
483 call = _make_help_call(target, esc)
485 call = _make_help_call(target, esc)
484 new_line = indent + call + '\n'
486 new_line = indent + call + '\n'
485
487
486 return lines_before + [new_line] + lines_after
488 return lines_before + [new_line] + lines_after
487
489
488 def make_tokens_by_line(lines:List[str]):
490 def make_tokens_by_line(lines:List[str]):
489 """Tokenize a series of lines and group tokens by line.
491 """Tokenize a series of lines and group tokens by line.
490
492
491 The tokens for a multiline Python string or expression are grouped as one
493 The tokens for a multiline Python string or expression are grouped as one
492 line. All lines except the last lines should keep their line ending ('\\n',
494 line. All lines except the last lines should keep their line ending ('\\n',
493 '\\r\\n') for this to properly work. Use `.splitlines(keeplineending=True)`
495 '\\r\\n') for this to properly work. Use `.splitlines(keeplineending=True)`
494 for example when passing block of text to this function.
496 for example when passing block of text to this function.
495
497
496 """
498 """
497 # NL tokens are used inside multiline expressions, but also after blank
499 # NL tokens are used inside multiline expressions, but also after blank
498 # lines or comments. This is intentional - see https://bugs.python.org/issue17061
500 # lines or comments. This is intentional - see https://bugs.python.org/issue17061
499 # We want to group the former case together but split the latter, so we
501 # We want to group the former case together but split the latter, so we
500 # track parentheses level, similar to the internals of tokenize.
502 # track parentheses level, similar to the internals of tokenize.
501
503
502 # reexported from token on 3.7+
504 # reexported from token on 3.7+
503 NEWLINE, NL = tokenize.NEWLINE, tokenize.NL # type: ignore
505 NEWLINE, NL = tokenize.NEWLINE, tokenize.NL # type: ignore
504 tokens_by_line: List[List[Any]] = [[]]
506 tokens_by_line: List[List[Any]] = [[]]
505 if len(lines) > 1 and not lines[0].endswith(("\n", "\r", "\r\n", "\x0b", "\x0c")):
507 if len(lines) > 1 and not lines[0].endswith(("\n", "\r", "\r\n", "\x0b", "\x0c")):
506 warnings.warn(
508 warnings.warn(
507 "`make_tokens_by_line` received a list of lines which do not have lineending markers ('\\n', '\\r', '\\r\\n', '\\x0b', '\\x0c'), behavior will be unspecified",
509 "`make_tokens_by_line` received a list of lines which do not have lineending markers ('\\n', '\\r', '\\r\\n', '\\x0b', '\\x0c'), behavior will be unspecified",
508 stacklevel=2,
510 stacklevel=2,
509 )
511 )
510 parenlev = 0
512 parenlev = 0
511 try:
513 try:
512 for token in tokenize.generate_tokens(iter(lines).__next__):
514 for token in tokenize.generate_tokens(iter(lines).__next__):
513 tokens_by_line[-1].append(token)
515 tokens_by_line[-1].append(token)
514 if (token.type == NEWLINE) \
516 if (token.type == NEWLINE) \
515 or ((token.type == NL) and (parenlev <= 0)):
517 or ((token.type == NL) and (parenlev <= 0)):
516 tokens_by_line.append([])
518 tokens_by_line.append([])
517 elif token.string in {'(', '[', '{'}:
519 elif token.string in {'(', '[', '{'}:
518 parenlev += 1
520 parenlev += 1
519 elif token.string in {')', ']', '}'}:
521 elif token.string in {')', ']', '}'}:
520 if parenlev > 0:
522 if parenlev > 0:
521 parenlev -= 1
523 parenlev -= 1
522 except tokenize.TokenError:
524 except tokenize.TokenError:
523 # Input ended in a multiline string or expression. That's OK for us.
525 # Input ended in a multiline string or expression. That's OK for us.
524 pass
526 pass
525
527
526
528
527 if not tokens_by_line[-1]:
529 if not tokens_by_line[-1]:
528 tokens_by_line.pop()
530 tokens_by_line.pop()
529
531
530
532
531 return tokens_by_line
533 return tokens_by_line
532
534
533
535
534 def has_sunken_brackets(tokens: List[tokenize.TokenInfo]):
536 def has_sunken_brackets(tokens: List[tokenize.TokenInfo]):
535 """Check if the depth of brackets in the list of tokens drops below 0"""
537 """Check if the depth of brackets in the list of tokens drops below 0"""
536 parenlev = 0
538 parenlev = 0
537 for token in tokens:
539 for token in tokens:
538 if token.string in {"(", "[", "{"}:
540 if token.string in {"(", "[", "{"}:
539 parenlev += 1
541 parenlev += 1
540 elif token.string in {")", "]", "}"}:
542 elif token.string in {")", "]", "}"}:
541 parenlev -= 1
543 parenlev -= 1
542 if parenlev < 0:
544 if parenlev < 0:
543 return True
545 return True
544 return False
546 return False
545
547
546
548
547 def show_linewise_tokens(s: str):
549 def show_linewise_tokens(s: str):
548 """For investigation and debugging"""
550 """For investigation and debugging"""
549 warnings.warn(
551 warnings.warn(
550 "show_linewise_tokens is deprecated since IPython 8.6",
552 "show_linewise_tokens is deprecated since IPython 8.6",
551 DeprecationWarning,
553 DeprecationWarning,
552 stacklevel=2,
554 stacklevel=2,
553 )
555 )
554 if not s.endswith("\n"):
556 if not s.endswith("\n"):
555 s += "\n"
557 s += "\n"
556 lines = s.splitlines(keepends=True)
558 lines = s.splitlines(keepends=True)
557 for line in make_tokens_by_line(lines):
559 for line in make_tokens_by_line(lines):
558 print("Line -------")
560 print("Line -------")
559 for tokinfo in line:
561 for tokinfo in line:
560 print(" ", tokinfo)
562 print(" ", tokinfo)
561
563
562 # Arbitrary limit to prevent getting stuck in infinite loops
564 # Arbitrary limit to prevent getting stuck in infinite loops
563 TRANSFORM_LOOP_LIMIT = 500
565 TRANSFORM_LOOP_LIMIT = 500
564
566
565 class TransformerManager:
567 class TransformerManager:
566 """Applies various transformations to a cell or code block.
568 """Applies various transformations to a cell or code block.
567
569
568 The key methods for external use are ``transform_cell()``
570 The key methods for external use are ``transform_cell()``
569 and ``check_complete()``.
571 and ``check_complete()``.
570 """
572 """
571 def __init__(self):
573 def __init__(self):
572 self.cleanup_transforms = [
574 self.cleanup_transforms = [
573 leading_empty_lines,
575 leading_empty_lines,
574 leading_indent,
576 leading_indent,
575 classic_prompt,
577 classic_prompt,
576 ipython_prompt,
578 ipython_prompt,
577 ]
579 ]
578 self.line_transforms = [
580 self.line_transforms = [
579 cell_magic,
581 cell_magic,
580 ]
582 ]
581 self.token_transformers = [
583 self.token_transformers = [
582 MagicAssign,
584 MagicAssign,
583 SystemAssign,
585 SystemAssign,
584 EscapedCommand,
586 EscapedCommand,
585 HelpEnd,
587 HelpEnd,
586 ]
588 ]
587
589
588 def do_one_token_transform(self, lines):
590 def do_one_token_transform(self, lines):
589 """Find and run the transform earliest in the code.
591 """Find and run the transform earliest in the code.
590
592
591 Returns (changed, lines).
593 Returns (changed, lines).
592
594
593 This method is called repeatedly until changed is False, indicating
595 This method is called repeatedly until changed is False, indicating
594 that all available transformations are complete.
596 that all available transformations are complete.
595
597
596 The tokens following IPython special syntax might not be valid, so
598 The tokens following IPython special syntax might not be valid, so
597 the transformed code is retokenised every time to identify the next
599 the transformed code is retokenised every time to identify the next
598 piece of special syntax. Hopefully long code cells are mostly valid
600 piece of special syntax. Hopefully long code cells are mostly valid
599 Python, not using lots of IPython special syntax, so this shouldn't be
601 Python, not using lots of IPython special syntax, so this shouldn't be
600 a performance issue.
602 a performance issue.
601 """
603 """
602 tokens_by_line = make_tokens_by_line(lines)
604 tokens_by_line = make_tokens_by_line(lines)
603 candidates = []
605 candidates = []
604 for transformer_cls in self.token_transformers:
606 for transformer_cls in self.token_transformers:
605 transformer = transformer_cls.find(tokens_by_line)
607 transformer = transformer_cls.find(tokens_by_line)
606 if transformer:
608 if transformer:
607 candidates.append(transformer)
609 candidates.append(transformer)
608
610
609 if not candidates:
611 if not candidates:
610 # Nothing to transform
612 # Nothing to transform
611 return False, lines
613 return False, lines
612 ordered_transformers = sorted(candidates, key=TokenTransformBase.sortby)
614 ordered_transformers = sorted(candidates, key=TokenTransformBase.sortby)
613 for transformer in ordered_transformers:
615 for transformer in ordered_transformers:
614 try:
616 try:
615 return True, transformer.transform(lines)
617 return True, transformer.transform(lines)
616 except SyntaxError:
618 except SyntaxError:
617 pass
619 pass
618 return False, lines
620 return False, lines
619
621
620 def do_token_transforms(self, lines):
622 def do_token_transforms(self, lines):
621 for _ in range(TRANSFORM_LOOP_LIMIT):
623 for _ in range(TRANSFORM_LOOP_LIMIT):
622 changed, lines = self.do_one_token_transform(lines)
624 changed, lines = self.do_one_token_transform(lines)
623 if not changed:
625 if not changed:
624 return lines
626 return lines
625
627
626 raise RuntimeError("Input transformation still changing after "
628 raise RuntimeError("Input transformation still changing after "
627 "%d iterations. Aborting." % TRANSFORM_LOOP_LIMIT)
629 "%d iterations. Aborting." % TRANSFORM_LOOP_LIMIT)
628
630
629 def transform_cell(self, cell: str) -> str:
631 def transform_cell(self, cell: str) -> str:
630 """Transforms a cell of input code"""
632 """Transforms a cell of input code"""
631 if not cell.endswith('\n'):
633 if not cell.endswith('\n'):
632 cell += '\n' # Ensure the cell has a trailing newline
634 cell += '\n' # Ensure the cell has a trailing newline
633 lines = cell.splitlines(keepends=True)
635 lines = cell.splitlines(keepends=True)
634 for transform in self.cleanup_transforms + self.line_transforms:
636 for transform in self.cleanup_transforms + self.line_transforms:
635 lines = transform(lines)
637 lines = transform(lines)
636
638
637 lines = self.do_token_transforms(lines)
639 lines = self.do_token_transforms(lines)
638 return ''.join(lines)
640 return ''.join(lines)
639
641
640 def check_complete(self, cell: str):
642 def check_complete(self, cell: str):
641 """Return whether a block of code is ready to execute, or should be continued
643 """Return whether a block of code is ready to execute, or should be continued
642
644
643 Parameters
645 Parameters
644 ----------
646 ----------
645 cell : string
647 cell : string
646 Python input code, which can be multiline.
648 Python input code, which can be multiline.
647
649
648 Returns
650 Returns
649 -------
651 -------
650 status : str
652 status : str
651 One of 'complete', 'incomplete', or 'invalid' if source is not a
653 One of 'complete', 'incomplete', or 'invalid' if source is not a
652 prefix of valid code.
654 prefix of valid code.
653 indent_spaces : int or None
655 indent_spaces : int or None
654 The number of spaces by which to indent the next line of code. If
656 The number of spaces by which to indent the next line of code. If
655 status is not 'incomplete', this is None.
657 status is not 'incomplete', this is None.
656 """
658 """
657 # Remember if the lines ends in a new line.
659 # Remember if the lines ends in a new line.
658 ends_with_newline = False
660 ends_with_newline = False
659 for character in reversed(cell):
661 for character in reversed(cell):
660 if character == '\n':
662 if character == '\n':
661 ends_with_newline = True
663 ends_with_newline = True
662 break
664 break
663 elif character.strip():
665 elif character.strip():
664 break
666 break
665 else:
667 else:
666 continue
668 continue
667
669
668 if not ends_with_newline:
670 if not ends_with_newline:
669 # Append an newline for consistent tokenization
671 # Append an newline for consistent tokenization
670 # See https://bugs.python.org/issue33899
672 # See https://bugs.python.org/issue33899
671 cell += '\n'
673 cell += '\n'
672
674
673 lines = cell.splitlines(keepends=True)
675 lines = cell.splitlines(keepends=True)
674
676
675 if not lines:
677 if not lines:
676 return 'complete', None
678 return 'complete', None
677
679
678 if lines[-1].endswith('\\'):
680 if lines[-1].endswith('\\'):
679 # Explicit backslash continuation
681 # Explicit backslash continuation
680 return 'incomplete', find_last_indent(lines)
682 return 'incomplete', find_last_indent(lines)
681
683
682 try:
684 try:
683 for transform in self.cleanup_transforms:
685 for transform in self.cleanup_transforms:
684 if not getattr(transform, 'has_side_effects', False):
686 if not getattr(transform, 'has_side_effects', False):
685 lines = transform(lines)
687 lines = transform(lines)
686 except SyntaxError:
688 except SyntaxError:
687 return 'invalid', None
689 return 'invalid', None
688
690
689 if lines[0].startswith('%%'):
691 if lines[0].startswith('%%'):
690 # Special case for cell magics - completion marked by blank line
692 # Special case for cell magics - completion marked by blank line
691 if lines[-1].strip():
693 if lines[-1].strip():
692 return 'incomplete', find_last_indent(lines)
694 return 'incomplete', find_last_indent(lines)
693 else:
695 else:
694 return 'complete', None
696 return 'complete', None
695
697
696 try:
698 try:
697 for transform in self.line_transforms:
699 for transform in self.line_transforms:
698 if not getattr(transform, 'has_side_effects', False):
700 if not getattr(transform, 'has_side_effects', False):
699 lines = transform(lines)
701 lines = transform(lines)
700 lines = self.do_token_transforms(lines)
702 lines = self.do_token_transforms(lines)
701 except SyntaxError:
703 except SyntaxError:
702 return 'invalid', None
704 return 'invalid', None
703
705
704 tokens_by_line = make_tokens_by_line(lines)
706 tokens_by_line = make_tokens_by_line(lines)
705
707
706 # Bail if we got one line and there are more closing parentheses than
708 # Bail if we got one line and there are more closing parentheses than
707 # the opening ones
709 # the opening ones
708 if (
710 if (
709 len(lines) == 1
711 len(lines) == 1
710 and tokens_by_line
712 and tokens_by_line
711 and has_sunken_brackets(tokens_by_line[0])
713 and has_sunken_brackets(tokens_by_line[0])
712 ):
714 ):
713 return "invalid", None
715 return "invalid", None
714
716
715 if not tokens_by_line:
717 if not tokens_by_line:
716 return 'incomplete', find_last_indent(lines)
718 return 'incomplete', find_last_indent(lines)
717
719
718 if tokens_by_line[-1][-1].type != tokenize.ENDMARKER:
720 if tokens_by_line[-1][-1].type != tokenize.ENDMARKER:
719 # We're in a multiline string or expression
721 # We're in a multiline string or expression
720 return 'incomplete', find_last_indent(lines)
722 return 'incomplete', find_last_indent(lines)
721
723
722 newline_types = {tokenize.NEWLINE, tokenize.COMMENT, tokenize.ENDMARKER} # type: ignore
724 newline_types = {tokenize.NEWLINE, tokenize.COMMENT, tokenize.ENDMARKER} # type: ignore
723
725
724 # Pop the last line which only contains DEDENTs and ENDMARKER
726 # Pop the last line which only contains DEDENTs and ENDMARKER
725 last_token_line = None
727 last_token_line = None
726 if {t.type for t in tokens_by_line[-1]} in [
728 if {t.type for t in tokens_by_line[-1]} in [
727 {tokenize.DEDENT, tokenize.ENDMARKER},
729 {tokenize.DEDENT, tokenize.ENDMARKER},
728 {tokenize.ENDMARKER}
730 {tokenize.ENDMARKER}
729 ] and len(tokens_by_line) > 1:
731 ] and len(tokens_by_line) > 1:
730 last_token_line = tokens_by_line.pop()
732 last_token_line = tokens_by_line.pop()
731
733
732 while tokens_by_line[-1] and tokens_by_line[-1][-1].type in newline_types:
734 while tokens_by_line[-1] and tokens_by_line[-1][-1].type in newline_types:
733 tokens_by_line[-1].pop()
735 tokens_by_line[-1].pop()
734
736
735 if not tokens_by_line[-1]:
737 if not tokens_by_line[-1]:
736 return 'incomplete', find_last_indent(lines)
738 return 'incomplete', find_last_indent(lines)
737
739
738 if tokens_by_line[-1][-1].string == ':':
740 if tokens_by_line[-1][-1].string == ':':
739 # The last line starts a block (e.g. 'if foo:')
741 # The last line starts a block (e.g. 'if foo:')
740 ix = 0
742 ix = 0
741 while tokens_by_line[-1][ix].type in {tokenize.INDENT, tokenize.DEDENT}:
743 while tokens_by_line[-1][ix].type in {tokenize.INDENT, tokenize.DEDENT}:
742 ix += 1
744 ix += 1
743
745
744 indent = tokens_by_line[-1][ix].start[1]
746 indent = tokens_by_line[-1][ix].start[1]
745 return 'incomplete', indent + 4
747 return 'incomplete', indent + 4
746
748
747 if tokens_by_line[-1][0].line.endswith('\\'):
749 if tokens_by_line[-1][0].line.endswith('\\'):
748 return 'incomplete', None
750 return 'incomplete', None
749
751
750 # At this point, our checks think the code is complete (or invalid).
752 # At this point, our checks think the code is complete (or invalid).
751 # We'll use codeop.compile_command to check this with the real parser
753 # We'll use codeop.compile_command to check this with the real parser
752 try:
754 try:
753 with warnings.catch_warnings():
755 with warnings.catch_warnings():
754 warnings.simplefilter('error', SyntaxWarning)
756 warnings.simplefilter('error', SyntaxWarning)
755 res = compile_command(''.join(lines), symbol='exec')
757 res = compile_command(''.join(lines), symbol='exec')
756 except (SyntaxError, OverflowError, ValueError, TypeError,
758 except (SyntaxError, OverflowError, ValueError, TypeError,
757 MemoryError, SyntaxWarning):
759 MemoryError, SyntaxWarning):
758 return 'invalid', None
760 return 'invalid', None
759 else:
761 else:
760 if res is None:
762 if res is None:
761 return 'incomplete', find_last_indent(lines)
763 return 'incomplete', find_last_indent(lines)
762
764
763 if last_token_line and last_token_line[0].type == tokenize.DEDENT:
765 if last_token_line and last_token_line[0].type == tokenize.DEDENT:
764 if ends_with_newline:
766 if ends_with_newline:
765 return 'complete', None
767 return 'complete', None
766 return 'incomplete', find_last_indent(lines)
768 return 'incomplete', find_last_indent(lines)
767
769
768 # If there's a blank line at the end, assume we're ready to execute
770 # If there's a blank line at the end, assume we're ready to execute
769 if not lines[-1].strip():
771 if not lines[-1].strip():
770 return 'complete', None
772 return 'complete', None
771
773
772 return 'complete', None
774 return 'complete', None
773
775
774
776
775 def find_last_indent(lines):
777 def find_last_indent(lines):
776 m = _indent_re.match(lines[-1])
778 m = _indent_re.match(lines[-1])
777 if not m:
779 if not m:
778 return 0
780 return 0
779 return len(m.group(0).replace('\t', ' '*4))
781 return len(m.group(0).replace('\t', ' '*4))
780
782
781
783
782 class MaybeAsyncCompile(Compile):
784 class MaybeAsyncCompile(Compile):
783 def __init__(self, extra_flags=0):
785 def __init__(self, extra_flags=0):
784 super().__init__()
786 super().__init__()
785 self.flags |= extra_flags
787 self.flags |= extra_flags
786
788
787
789
788 class MaybeAsyncCommandCompiler(CommandCompiler):
790 class MaybeAsyncCommandCompiler(CommandCompiler):
789 def __init__(self, extra_flags=0):
791 def __init__(self, extra_flags=0):
790 self.compiler = MaybeAsyncCompile(extra_flags=extra_flags)
792 self.compiler = MaybeAsyncCompile(extra_flags=extra_flags)
791
793
792
794
793 _extra_flags = ast.PyCF_ALLOW_TOP_LEVEL_AWAIT
795 _extra_flags = ast.PyCF_ALLOW_TOP_LEVEL_AWAIT
794
796
795 compile_command = MaybeAsyncCommandCompiler(extra_flags=_extra_flags)
797 compile_command = MaybeAsyncCommandCompiler(extra_flags=_extra_flags)
@@ -1,3809 +1,3820 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """Main IPython class."""
2 """Main IPython class."""
3
3
4 #-----------------------------------------------------------------------------
4 #-----------------------------------------------------------------------------
5 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de>
5 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de>
6 # Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu>
6 # Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu>
7 # Copyright (C) 2008-2011 The IPython Development Team
7 # Copyright (C) 2008-2011 The IPython Development Team
8 #
8 #
9 # Distributed under the terms of the BSD License. The full license is in
9 # Distributed under the terms of the BSD License. The full license is in
10 # the file COPYING, distributed as part of this software.
10 # the file COPYING, distributed as part of this software.
11 #-----------------------------------------------------------------------------
11 #-----------------------------------------------------------------------------
12
12
13
13
14 import abc
14 import abc
15 import ast
15 import ast
16 import atexit
16 import atexit
17 import bdb
17 import bdb
18 import builtins as builtin_mod
18 import builtins as builtin_mod
19 import functools
19 import functools
20 import inspect
20 import inspect
21 import os
21 import os
22 import re
22 import re
23 import runpy
23 import runpy
24 import subprocess
24 import subprocess
25 import sys
25 import sys
26 import tempfile
26 import tempfile
27 import traceback
27 import traceback
28 import types
28 import types
29 import warnings
29 import warnings
30 from ast import stmt
30 from ast import stmt
31 from io import open as io_open
31 from io import open as io_open
32 from logging import error
32 from logging import error
33 from pathlib import Path
33 from pathlib import Path
34 from typing import Callable
34 from typing import Callable
35 from typing import List as ListType
35 from typing import List as ListType
36 from typing import Optional, Tuple
36 from typing import Optional, Tuple
37 from warnings import warn
37 from warnings import warn
38
38
39 from pickleshare import PickleShareDB
39 from pickleshare import PickleShareDB
40 from tempfile import TemporaryDirectory
40 from tempfile import TemporaryDirectory
41 from traitlets import (
41 from traitlets import (
42 Any,
42 Any,
43 Bool,
43 Bool,
44 CaselessStrEnum,
44 CaselessStrEnum,
45 Dict,
45 Dict,
46 Enum,
46 Enum,
47 Instance,
47 Instance,
48 Integer,
48 Integer,
49 List,
49 List,
50 Type,
50 Type,
51 Unicode,
51 Unicode,
52 default,
52 default,
53 observe,
53 observe,
54 validate,
54 validate,
55 )
55 )
56 from traitlets.config.configurable import SingletonConfigurable
56 from traitlets.config.configurable import SingletonConfigurable
57 from traitlets.utils.importstring import import_item
57 from traitlets.utils.importstring import import_item
58
58
59 import IPython.core.hooks
59 import IPython.core.hooks
60 from IPython.core import magic, oinspect, page, prefilter, ultratb
60 from IPython.core import magic, oinspect, page, prefilter, ultratb
61 from IPython.core.alias import Alias, AliasManager
61 from IPython.core.alias import Alias, AliasManager
62 from IPython.core.autocall import ExitAutocall
62 from IPython.core.autocall import ExitAutocall
63 from IPython.core.builtin_trap import BuiltinTrap
63 from IPython.core.builtin_trap import BuiltinTrap
64 from IPython.core.compilerop import CachingCompiler, check_linecache_ipython
64 from IPython.core.compilerop import CachingCompiler, check_linecache_ipython
65 from IPython.core.debugger import InterruptiblePdb
65 from IPython.core.debugger import InterruptiblePdb
66 from IPython.core.display_trap import DisplayTrap
66 from IPython.core.display_trap import DisplayTrap
67 from IPython.core.displayhook import DisplayHook
67 from IPython.core.displayhook import DisplayHook
68 from IPython.core.displaypub import DisplayPublisher
68 from IPython.core.displaypub import DisplayPublisher
69 from IPython.core.error import InputRejected, UsageError
69 from IPython.core.error import InputRejected, UsageError
70 from IPython.core.events import EventManager, available_events
70 from IPython.core.events import EventManager, available_events
71 from IPython.core.extensions import ExtensionManager
71 from IPython.core.extensions import ExtensionManager
72 from IPython.core.formatters import DisplayFormatter
72 from IPython.core.formatters import DisplayFormatter
73 from IPython.core.history import HistoryManager
73 from IPython.core.history import HistoryManager
74 from IPython.core.inputtransformer2 import ESC_MAGIC, ESC_MAGIC2
74 from IPython.core.inputtransformer2 import ESC_MAGIC, ESC_MAGIC2
75 from IPython.core.logger import Logger
75 from IPython.core.logger import Logger
76 from IPython.core.macro import Macro
76 from IPython.core.macro import Macro
77 from IPython.core.payload import PayloadManager
77 from IPython.core.payload import PayloadManager
78 from IPython.core.prefilter import PrefilterManager
78 from IPython.core.prefilter import PrefilterManager
79 from IPython.core.profiledir import ProfileDir
79 from IPython.core.profiledir import ProfileDir
80 from IPython.core.usage import default_banner
80 from IPython.core.usage import default_banner
81 from IPython.display import display
81 from IPython.display import display
82 from IPython.paths import get_ipython_dir
82 from IPython.paths import get_ipython_dir
83 from IPython.testing.skipdoctest import skip_doctest
83 from IPython.testing.skipdoctest import skip_doctest
84 from IPython.utils import PyColorize, io, openpy, py3compat
84 from IPython.utils import PyColorize, io, openpy, py3compat
85 from IPython.utils.decorators import undoc
85 from IPython.utils.decorators import undoc
86 from IPython.utils.io import ask_yes_no
86 from IPython.utils.io import ask_yes_no
87 from IPython.utils.ipstruct import Struct
87 from IPython.utils.ipstruct import Struct
88 from IPython.utils.path import ensure_dir_exists, get_home_dir, get_py_filename
88 from IPython.utils.path import ensure_dir_exists, get_home_dir, get_py_filename
89 from IPython.utils.process import getoutput, system
89 from IPython.utils.process import getoutput, system
90 from IPython.utils.strdispatch import StrDispatch
90 from IPython.utils.strdispatch import StrDispatch
91 from IPython.utils.syspathcontext import prepended_to_syspath
91 from IPython.utils.syspathcontext import prepended_to_syspath
92 from IPython.utils.text import DollarFormatter, LSString, SList, format_screen
92 from IPython.utils.text import DollarFormatter, LSString, SList, format_screen
93
93
94 sphinxify: Optional[Callable]
94 sphinxify: Optional[Callable]
95
95
96 try:
96 try:
97 import docrepr.sphinxify as sphx
97 import docrepr.sphinxify as sphx
98
98
99 def sphinxify(oinfo):
99 def sphinxify(oinfo):
100 wrapped_docstring = sphx.wrap_main_docstring(oinfo)
100 wrapped_docstring = sphx.wrap_main_docstring(oinfo)
101
101
102 def sphinxify_docstring(docstring):
102 def sphinxify_docstring(docstring):
103 with TemporaryDirectory() as dirname:
103 with TemporaryDirectory() as dirname:
104 return {
104 return {
105 "text/html": sphx.sphinxify(wrapped_docstring, dirname),
105 "text/html": sphx.sphinxify(wrapped_docstring, dirname),
106 "text/plain": docstring,
106 "text/plain": docstring,
107 }
107 }
108
108
109 return sphinxify_docstring
109 return sphinxify_docstring
110 except ImportError:
110 except ImportError:
111 sphinxify = None
111 sphinxify = None
112
112
113
113
114 class ProvisionalWarning(DeprecationWarning):
114 class ProvisionalWarning(DeprecationWarning):
115 """
115 """
116 Warning class for unstable features
116 Warning class for unstable features
117 """
117 """
118 pass
118 pass
119
119
120 from ast import Module
120 from ast import Module
121
121
122 _assign_nodes = (ast.AugAssign, ast.AnnAssign, ast.Assign)
122 _assign_nodes = (ast.AugAssign, ast.AnnAssign, ast.Assign)
123 _single_targets_nodes = (ast.AugAssign, ast.AnnAssign)
123 _single_targets_nodes = (ast.AugAssign, ast.AnnAssign)
124
124
125 #-----------------------------------------------------------------------------
125 #-----------------------------------------------------------------------------
126 # Await Helpers
126 # Await Helpers
127 #-----------------------------------------------------------------------------
127 #-----------------------------------------------------------------------------
128
128
129 # we still need to run things using the asyncio eventloop, but there is no
129 # we still need to run things using the asyncio eventloop, but there is no
130 # async integration
130 # async integration
131 from .async_helpers import (
131 from .async_helpers import (
132 _asyncio_runner,
132 _asyncio_runner,
133 _curio_runner,
133 _curio_runner,
134 _pseudo_sync_runner,
134 _pseudo_sync_runner,
135 _should_be_async,
135 _should_be_async,
136 _trio_runner,
136 _trio_runner,
137 )
137 )
138
138
139 #-----------------------------------------------------------------------------
139 #-----------------------------------------------------------------------------
140 # Globals
140 # Globals
141 #-----------------------------------------------------------------------------
141 #-----------------------------------------------------------------------------
142
142
143 # compiled regexps for autoindent management
143 # compiled regexps for autoindent management
144 dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass')
144 dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass')
145
145
146 #-----------------------------------------------------------------------------
146 #-----------------------------------------------------------------------------
147 # Utilities
147 # Utilities
148 #-----------------------------------------------------------------------------
148 #-----------------------------------------------------------------------------
149
149
150 def is_integer_string(s:str):
151 """
152 Variant of "str.isnumeric()" that allow negative values and other ints.
153 """
154 try:
155 int(s)
156 return True
157 except ValueError:
158 return False
159 raise ValueError('Unexpected error')
160
150 @undoc
161 @undoc
151 def softspace(file, newvalue):
162 def softspace(file, newvalue):
152 """Copied from code.py, to remove the dependency"""
163 """Copied from code.py, to remove the dependency"""
153
164
154 oldvalue = 0
165 oldvalue = 0
155 try:
166 try:
156 oldvalue = file.softspace
167 oldvalue = file.softspace
157 except AttributeError:
168 except AttributeError:
158 pass
169 pass
159 try:
170 try:
160 file.softspace = newvalue
171 file.softspace = newvalue
161 except (AttributeError, TypeError):
172 except (AttributeError, TypeError):
162 # "attribute-less object" or "read-only attributes"
173 # "attribute-less object" or "read-only attributes"
163 pass
174 pass
164 return oldvalue
175 return oldvalue
165
176
166 @undoc
177 @undoc
167 def no_op(*a, **kw):
178 def no_op(*a, **kw):
168 pass
179 pass
169
180
170
181
171 class SpaceInInput(Exception): pass
182 class SpaceInInput(Exception): pass
172
183
173
184
174 class SeparateUnicode(Unicode):
185 class SeparateUnicode(Unicode):
175 r"""A Unicode subclass to validate separate_in, separate_out, etc.
186 r"""A Unicode subclass to validate separate_in, separate_out, etc.
176
187
177 This is a Unicode based trait that converts '0'->'' and ``'\\n'->'\n'``.
188 This is a Unicode based trait that converts '0'->'' and ``'\\n'->'\n'``.
178 """
189 """
179
190
180 def validate(self, obj, value):
191 def validate(self, obj, value):
181 if value == '0': value = ''
192 if value == '0': value = ''
182 value = value.replace('\\n','\n')
193 value = value.replace('\\n','\n')
183 return super(SeparateUnicode, self).validate(obj, value)
194 return super(SeparateUnicode, self).validate(obj, value)
184
195
185
196
186 @undoc
197 @undoc
187 class DummyMod(object):
198 class DummyMod(object):
188 """A dummy module used for IPython's interactive module when
199 """A dummy module used for IPython's interactive module when
189 a namespace must be assigned to the module's __dict__."""
200 a namespace must be assigned to the module's __dict__."""
190 __spec__ = None
201 __spec__ = None
191
202
192
203
193 class ExecutionInfo(object):
204 class ExecutionInfo(object):
194 """The arguments used for a call to :meth:`InteractiveShell.run_cell`
205 """The arguments used for a call to :meth:`InteractiveShell.run_cell`
195
206
196 Stores information about what is going to happen.
207 Stores information about what is going to happen.
197 """
208 """
198 raw_cell = None
209 raw_cell = None
199 store_history = False
210 store_history = False
200 silent = False
211 silent = False
201 shell_futures = True
212 shell_futures = True
202 cell_id = None
213 cell_id = None
203
214
204 def __init__(self, raw_cell, store_history, silent, shell_futures, cell_id):
215 def __init__(self, raw_cell, store_history, silent, shell_futures, cell_id):
205 self.raw_cell = raw_cell
216 self.raw_cell = raw_cell
206 self.store_history = store_history
217 self.store_history = store_history
207 self.silent = silent
218 self.silent = silent
208 self.shell_futures = shell_futures
219 self.shell_futures = shell_futures
209 self.cell_id = cell_id
220 self.cell_id = cell_id
210
221
211 def __repr__(self):
222 def __repr__(self):
212 name = self.__class__.__qualname__
223 name = self.__class__.__qualname__
213 raw_cell = (
224 raw_cell = (
214 (self.raw_cell[:50] + "..") if len(self.raw_cell) > 50 else self.raw_cell
225 (self.raw_cell[:50] + "..") if len(self.raw_cell) > 50 else self.raw_cell
215 )
226 )
216 return '<%s object at %x, raw_cell="%s" store_history=%s silent=%s shell_futures=%s cell_id=%s>' % (
227 return '<%s object at %x, raw_cell="%s" store_history=%s silent=%s shell_futures=%s cell_id=%s>' % (
217 name,
228 name,
218 id(self),
229 id(self),
219 raw_cell,
230 raw_cell,
220 self.store_history,
231 self.store_history,
221 self.silent,
232 self.silent,
222 self.shell_futures,
233 self.shell_futures,
223 self.cell_id,
234 self.cell_id,
224 )
235 )
225
236
226
237
227 class ExecutionResult(object):
238 class ExecutionResult(object):
228 """The result of a call to :meth:`InteractiveShell.run_cell`
239 """The result of a call to :meth:`InteractiveShell.run_cell`
229
240
230 Stores information about what took place.
241 Stores information about what took place.
231 """
242 """
232 execution_count = None
243 execution_count = None
233 error_before_exec = None
244 error_before_exec = None
234 error_in_exec: Optional[BaseException] = None
245 error_in_exec: Optional[BaseException] = None
235 info = None
246 info = None
236 result = None
247 result = None
237
248
238 def __init__(self, info):
249 def __init__(self, info):
239 self.info = info
250 self.info = info
240
251
241 @property
252 @property
242 def success(self):
253 def success(self):
243 return (self.error_before_exec is None) and (self.error_in_exec is None)
254 return (self.error_before_exec is None) and (self.error_in_exec is None)
244
255
245 def raise_error(self):
256 def raise_error(self):
246 """Reraises error if `success` is `False`, otherwise does nothing"""
257 """Reraises error if `success` is `False`, otherwise does nothing"""
247 if self.error_before_exec is not None:
258 if self.error_before_exec is not None:
248 raise self.error_before_exec
259 raise self.error_before_exec
249 if self.error_in_exec is not None:
260 if self.error_in_exec is not None:
250 raise self.error_in_exec
261 raise self.error_in_exec
251
262
252 def __repr__(self):
263 def __repr__(self):
253 name = self.__class__.__qualname__
264 name = self.__class__.__qualname__
254 return '<%s object at %x, execution_count=%s error_before_exec=%s error_in_exec=%s info=%s result=%s>' %\
265 return '<%s object at %x, execution_count=%s error_before_exec=%s error_in_exec=%s info=%s result=%s>' %\
255 (name, id(self), self.execution_count, self.error_before_exec, self.error_in_exec, repr(self.info), repr(self.result))
266 (name, id(self), self.execution_count, self.error_before_exec, self.error_in_exec, repr(self.info), repr(self.result))
256
267
257
268
258 class InteractiveShell(SingletonConfigurable):
269 class InteractiveShell(SingletonConfigurable):
259 """An enhanced, interactive shell for Python."""
270 """An enhanced, interactive shell for Python."""
260
271
261 _instance = None
272 _instance = None
262
273
263 ast_transformers = List([], help=
274 ast_transformers = List([], help=
264 """
275 """
265 A list of ast.NodeTransformer subclass instances, which will be applied
276 A list of ast.NodeTransformer subclass instances, which will be applied
266 to user input before code is run.
277 to user input before code is run.
267 """
278 """
268 ).tag(config=True)
279 ).tag(config=True)
269
280
270 autocall = Enum((0,1,2), default_value=0, help=
281 autocall = Enum((0,1,2), default_value=0, help=
271 """
282 """
272 Make IPython automatically call any callable object even if you didn't
283 Make IPython automatically call any callable object even if you didn't
273 type explicit parentheses. For example, 'str 43' becomes 'str(43)'
284 type explicit parentheses. For example, 'str 43' becomes 'str(43)'
274 automatically. The value can be '0' to disable the feature, '1' for
285 automatically. The value can be '0' to disable the feature, '1' for
275 'smart' autocall, where it is not applied if there are no more
286 'smart' autocall, where it is not applied if there are no more
276 arguments on the line, and '2' for 'full' autocall, where all callable
287 arguments on the line, and '2' for 'full' autocall, where all callable
277 objects are automatically called (even if no arguments are present).
288 objects are automatically called (even if no arguments are present).
278 """
289 """
279 ).tag(config=True)
290 ).tag(config=True)
280
291
281 autoindent = Bool(True, help=
292 autoindent = Bool(True, help=
282 """
293 """
283 Autoindent IPython code entered interactively.
294 Autoindent IPython code entered interactively.
284 """
295 """
285 ).tag(config=True)
296 ).tag(config=True)
286
297
287 autoawait = Bool(True, help=
298 autoawait = Bool(True, help=
288 """
299 """
289 Automatically run await statement in the top level repl.
300 Automatically run await statement in the top level repl.
290 """
301 """
291 ).tag(config=True)
302 ).tag(config=True)
292
303
293 loop_runner_map ={
304 loop_runner_map ={
294 'asyncio':(_asyncio_runner, True),
305 'asyncio':(_asyncio_runner, True),
295 'curio':(_curio_runner, True),
306 'curio':(_curio_runner, True),
296 'trio':(_trio_runner, True),
307 'trio':(_trio_runner, True),
297 'sync': (_pseudo_sync_runner, False)
308 'sync': (_pseudo_sync_runner, False)
298 }
309 }
299
310
300 loop_runner = Any(default_value="IPython.core.interactiveshell._asyncio_runner",
311 loop_runner = Any(default_value="IPython.core.interactiveshell._asyncio_runner",
301 allow_none=True,
312 allow_none=True,
302 help="""Select the loop runner that will be used to execute top-level asynchronous code"""
313 help="""Select the loop runner that will be used to execute top-level asynchronous code"""
303 ).tag(config=True)
314 ).tag(config=True)
304
315
305 @default('loop_runner')
316 @default('loop_runner')
306 def _default_loop_runner(self):
317 def _default_loop_runner(self):
307 return import_item("IPython.core.interactiveshell._asyncio_runner")
318 return import_item("IPython.core.interactiveshell._asyncio_runner")
308
319
309 @validate('loop_runner')
320 @validate('loop_runner')
310 def _import_runner(self, proposal):
321 def _import_runner(self, proposal):
311 if isinstance(proposal.value, str):
322 if isinstance(proposal.value, str):
312 if proposal.value in self.loop_runner_map:
323 if proposal.value in self.loop_runner_map:
313 runner, autoawait = self.loop_runner_map[proposal.value]
324 runner, autoawait = self.loop_runner_map[proposal.value]
314 self.autoawait = autoawait
325 self.autoawait = autoawait
315 return runner
326 return runner
316 runner = import_item(proposal.value)
327 runner = import_item(proposal.value)
317 if not callable(runner):
328 if not callable(runner):
318 raise ValueError('loop_runner must be callable')
329 raise ValueError('loop_runner must be callable')
319 return runner
330 return runner
320 if not callable(proposal.value):
331 if not callable(proposal.value):
321 raise ValueError('loop_runner must be callable')
332 raise ValueError('loop_runner must be callable')
322 return proposal.value
333 return proposal.value
323
334
324 automagic = Bool(True, help=
335 automagic = Bool(True, help=
325 """
336 """
326 Enable magic commands to be called without the leading %.
337 Enable magic commands to be called without the leading %.
327 """
338 """
328 ).tag(config=True)
339 ).tag(config=True)
329
340
330 banner1 = Unicode(default_banner,
341 banner1 = Unicode(default_banner,
331 help="""The part of the banner to be printed before the profile"""
342 help="""The part of the banner to be printed before the profile"""
332 ).tag(config=True)
343 ).tag(config=True)
333 banner2 = Unicode('',
344 banner2 = Unicode('',
334 help="""The part of the banner to be printed after the profile"""
345 help="""The part of the banner to be printed after the profile"""
335 ).tag(config=True)
346 ).tag(config=True)
336
347
337 cache_size = Integer(1000, help=
348 cache_size = Integer(1000, help=
338 """
349 """
339 Set the size of the output cache. The default is 1000, you can
350 Set the size of the output cache. The default is 1000, you can
340 change it permanently in your config file. Setting it to 0 completely
351 change it permanently in your config file. Setting it to 0 completely
341 disables the caching system, and the minimum value accepted is 3 (if
352 disables the caching system, and the minimum value accepted is 3 (if
342 you provide a value less than 3, it is reset to 0 and a warning is
353 you provide a value less than 3, it is reset to 0 and a warning is
343 issued). This limit is defined because otherwise you'll spend more
354 issued). This limit is defined because otherwise you'll spend more
344 time re-flushing a too small cache than working
355 time re-flushing a too small cache than working
345 """
356 """
346 ).tag(config=True)
357 ).tag(config=True)
347 color_info = Bool(True, help=
358 color_info = Bool(True, help=
348 """
359 """
349 Use colors for displaying information about objects. Because this
360 Use colors for displaying information about objects. Because this
350 information is passed through a pager (like 'less'), and some pagers
361 information is passed through a pager (like 'less'), and some pagers
351 get confused with color codes, this capability can be turned off.
362 get confused with color codes, this capability can be turned off.
352 """
363 """
353 ).tag(config=True)
364 ).tag(config=True)
354 colors = CaselessStrEnum(('Neutral', 'NoColor','LightBG','Linux'),
365 colors = CaselessStrEnum(('Neutral', 'NoColor','LightBG','Linux'),
355 default_value='Neutral',
366 default_value='Neutral',
356 help="Set the color scheme (NoColor, Neutral, Linux, or LightBG)."
367 help="Set the color scheme (NoColor, Neutral, Linux, or LightBG)."
357 ).tag(config=True)
368 ).tag(config=True)
358 debug = Bool(False).tag(config=True)
369 debug = Bool(False).tag(config=True)
359 disable_failing_post_execute = Bool(False,
370 disable_failing_post_execute = Bool(False,
360 help="Don't call post-execute functions that have failed in the past."
371 help="Don't call post-execute functions that have failed in the past."
361 ).tag(config=True)
372 ).tag(config=True)
362 display_formatter = Instance(DisplayFormatter, allow_none=True)
373 display_formatter = Instance(DisplayFormatter, allow_none=True)
363 displayhook_class = Type(DisplayHook)
374 displayhook_class = Type(DisplayHook)
364 display_pub_class = Type(DisplayPublisher)
375 display_pub_class = Type(DisplayPublisher)
365 compiler_class = Type(CachingCompiler)
376 compiler_class = Type(CachingCompiler)
366
377
367 sphinxify_docstring = Bool(False, help=
378 sphinxify_docstring = Bool(False, help=
368 """
379 """
369 Enables rich html representation of docstrings. (This requires the
380 Enables rich html representation of docstrings. (This requires the
370 docrepr module).
381 docrepr module).
371 """).tag(config=True)
382 """).tag(config=True)
372
383
373 @observe("sphinxify_docstring")
384 @observe("sphinxify_docstring")
374 def _sphinxify_docstring_changed(self, change):
385 def _sphinxify_docstring_changed(self, change):
375 if change['new']:
386 if change['new']:
376 warn("`sphinxify_docstring` is provisional since IPython 5.0 and might change in future versions." , ProvisionalWarning)
387 warn("`sphinxify_docstring` is provisional since IPython 5.0 and might change in future versions." , ProvisionalWarning)
377
388
378 enable_html_pager = Bool(False, help=
389 enable_html_pager = Bool(False, help=
379 """
390 """
380 (Provisional API) enables html representation in mime bundles sent
391 (Provisional API) enables html representation in mime bundles sent
381 to pagers.
392 to pagers.
382 """).tag(config=True)
393 """).tag(config=True)
383
394
384 @observe("enable_html_pager")
395 @observe("enable_html_pager")
385 def _enable_html_pager_changed(self, change):
396 def _enable_html_pager_changed(self, change):
386 if change['new']:
397 if change['new']:
387 warn("`enable_html_pager` is provisional since IPython 5.0 and might change in future versions.", ProvisionalWarning)
398 warn("`enable_html_pager` is provisional since IPython 5.0 and might change in future versions.", ProvisionalWarning)
388
399
389 data_pub_class = None
400 data_pub_class = None
390
401
391 exit_now = Bool(False)
402 exit_now = Bool(False)
392 exiter = Instance(ExitAutocall)
403 exiter = Instance(ExitAutocall)
393 @default('exiter')
404 @default('exiter')
394 def _exiter_default(self):
405 def _exiter_default(self):
395 return ExitAutocall(self)
406 return ExitAutocall(self)
396 # Monotonically increasing execution counter
407 # Monotonically increasing execution counter
397 execution_count = Integer(1)
408 execution_count = Integer(1)
398 filename = Unicode("<ipython console>")
409 filename = Unicode("<ipython console>")
399 ipython_dir= Unicode('').tag(config=True) # Set to get_ipython_dir() in __init__
410 ipython_dir= Unicode('').tag(config=True) # Set to get_ipython_dir() in __init__
400
411
401 # Used to transform cells before running them, and check whether code is complete
412 # Used to transform cells before running them, and check whether code is complete
402 input_transformer_manager = Instance('IPython.core.inputtransformer2.TransformerManager',
413 input_transformer_manager = Instance('IPython.core.inputtransformer2.TransformerManager',
403 ())
414 ())
404
415
405 @property
416 @property
406 def input_transformers_cleanup(self):
417 def input_transformers_cleanup(self):
407 return self.input_transformer_manager.cleanup_transforms
418 return self.input_transformer_manager.cleanup_transforms
408
419
409 input_transformers_post = List([],
420 input_transformers_post = List([],
410 help="A list of string input transformers, to be applied after IPython's "
421 help="A list of string input transformers, to be applied after IPython's "
411 "own input transformations."
422 "own input transformations."
412 )
423 )
413
424
414 @property
425 @property
415 def input_splitter(self):
426 def input_splitter(self):
416 """Make this available for backward compatibility (pre-7.0 release) with existing code.
427 """Make this available for backward compatibility (pre-7.0 release) with existing code.
417
428
418 For example, ipykernel ipykernel currently uses
429 For example, ipykernel ipykernel currently uses
419 `shell.input_splitter.check_complete`
430 `shell.input_splitter.check_complete`
420 """
431 """
421 from warnings import warn
432 from warnings import warn
422 warn("`input_splitter` is deprecated since IPython 7.0, prefer `input_transformer_manager`.",
433 warn("`input_splitter` is deprecated since IPython 7.0, prefer `input_transformer_manager`.",
423 DeprecationWarning, stacklevel=2
434 DeprecationWarning, stacklevel=2
424 )
435 )
425 return self.input_transformer_manager
436 return self.input_transformer_manager
426
437
427 logstart = Bool(False, help=
438 logstart = Bool(False, help=
428 """
439 """
429 Start logging to the default log file in overwrite mode.
440 Start logging to the default log file in overwrite mode.
430 Use `logappend` to specify a log file to **append** logs to.
441 Use `logappend` to specify a log file to **append** logs to.
431 """
442 """
432 ).tag(config=True)
443 ).tag(config=True)
433 logfile = Unicode('', help=
444 logfile = Unicode('', help=
434 """
445 """
435 The name of the logfile to use.
446 The name of the logfile to use.
436 """
447 """
437 ).tag(config=True)
448 ).tag(config=True)
438 logappend = Unicode('', help=
449 logappend = Unicode('', help=
439 """
450 """
440 Start logging to the given file in append mode.
451 Start logging to the given file in append mode.
441 Use `logfile` to specify a log file to **overwrite** logs to.
452 Use `logfile` to specify a log file to **overwrite** logs to.
442 """
453 """
443 ).tag(config=True)
454 ).tag(config=True)
444 object_info_string_level = Enum((0,1,2), default_value=0,
455 object_info_string_level = Enum((0,1,2), default_value=0,
445 ).tag(config=True)
456 ).tag(config=True)
446 pdb = Bool(False, help=
457 pdb = Bool(False, help=
447 """
458 """
448 Automatically call the pdb debugger after every exception.
459 Automatically call the pdb debugger after every exception.
449 """
460 """
450 ).tag(config=True)
461 ).tag(config=True)
451 display_page = Bool(False,
462 display_page = Bool(False,
452 help="""If True, anything that would be passed to the pager
463 help="""If True, anything that would be passed to the pager
453 will be displayed as regular output instead."""
464 will be displayed as regular output instead."""
454 ).tag(config=True)
465 ).tag(config=True)
455
466
456
467
457 show_rewritten_input = Bool(True,
468 show_rewritten_input = Bool(True,
458 help="Show rewritten input, e.g. for autocall."
469 help="Show rewritten input, e.g. for autocall."
459 ).tag(config=True)
470 ).tag(config=True)
460
471
461 quiet = Bool(False).tag(config=True)
472 quiet = Bool(False).tag(config=True)
462
473
463 history_length = Integer(10000,
474 history_length = Integer(10000,
464 help='Total length of command history'
475 help='Total length of command history'
465 ).tag(config=True)
476 ).tag(config=True)
466
477
467 history_load_length = Integer(1000, help=
478 history_load_length = Integer(1000, help=
468 """
479 """
469 The number of saved history entries to be loaded
480 The number of saved history entries to be loaded
470 into the history buffer at startup.
481 into the history buffer at startup.
471 """
482 """
472 ).tag(config=True)
483 ).tag(config=True)
473
484
474 ast_node_interactivity = Enum(['all', 'last', 'last_expr', 'none', 'last_expr_or_assign'],
485 ast_node_interactivity = Enum(['all', 'last', 'last_expr', 'none', 'last_expr_or_assign'],
475 default_value='last_expr',
486 default_value='last_expr',
476 help="""
487 help="""
477 'all', 'last', 'last_expr' or 'none', 'last_expr_or_assign' specifying
488 'all', 'last', 'last_expr' or 'none', 'last_expr_or_assign' specifying
478 which nodes should be run interactively (displaying output from expressions).
489 which nodes should be run interactively (displaying output from expressions).
479 """
490 """
480 ).tag(config=True)
491 ).tag(config=True)
481
492
482 warn_venv = Bool(
493 warn_venv = Bool(
483 True,
494 True,
484 help="Warn if running in a virtual environment with no IPython installed (so IPython from the global environment is used).",
495 help="Warn if running in a virtual environment with no IPython installed (so IPython from the global environment is used).",
485 ).tag(config=True)
496 ).tag(config=True)
486
497
487 # TODO: this part of prompt management should be moved to the frontends.
498 # TODO: this part of prompt management should be moved to the frontends.
488 # Use custom TraitTypes that convert '0'->'' and '\\n'->'\n'
499 # Use custom TraitTypes that convert '0'->'' and '\\n'->'\n'
489 separate_in = SeparateUnicode('\n').tag(config=True)
500 separate_in = SeparateUnicode('\n').tag(config=True)
490 separate_out = SeparateUnicode('').tag(config=True)
501 separate_out = SeparateUnicode('').tag(config=True)
491 separate_out2 = SeparateUnicode('').tag(config=True)
502 separate_out2 = SeparateUnicode('').tag(config=True)
492 wildcards_case_sensitive = Bool(True).tag(config=True)
503 wildcards_case_sensitive = Bool(True).tag(config=True)
493 xmode = CaselessStrEnum(('Context', 'Plain', 'Verbose', 'Minimal'),
504 xmode = CaselessStrEnum(('Context', 'Plain', 'Verbose', 'Minimal'),
494 default_value='Context',
505 default_value='Context',
495 help="Switch modes for the IPython exception handlers."
506 help="Switch modes for the IPython exception handlers."
496 ).tag(config=True)
507 ).tag(config=True)
497
508
498 # Subcomponents of InteractiveShell
509 # Subcomponents of InteractiveShell
499 alias_manager = Instance('IPython.core.alias.AliasManager', allow_none=True)
510 alias_manager = Instance('IPython.core.alias.AliasManager', allow_none=True)
500 prefilter_manager = Instance('IPython.core.prefilter.PrefilterManager', allow_none=True)
511 prefilter_manager = Instance('IPython.core.prefilter.PrefilterManager', allow_none=True)
501 builtin_trap = Instance('IPython.core.builtin_trap.BuiltinTrap', allow_none=True)
512 builtin_trap = Instance('IPython.core.builtin_trap.BuiltinTrap', allow_none=True)
502 display_trap = Instance('IPython.core.display_trap.DisplayTrap', allow_none=True)
513 display_trap = Instance('IPython.core.display_trap.DisplayTrap', allow_none=True)
503 extension_manager = Instance('IPython.core.extensions.ExtensionManager', allow_none=True)
514 extension_manager = Instance('IPython.core.extensions.ExtensionManager', allow_none=True)
504 payload_manager = Instance('IPython.core.payload.PayloadManager', allow_none=True)
515 payload_manager = Instance('IPython.core.payload.PayloadManager', allow_none=True)
505 history_manager = Instance('IPython.core.history.HistoryAccessorBase', allow_none=True)
516 history_manager = Instance('IPython.core.history.HistoryAccessorBase', allow_none=True)
506 magics_manager = Instance('IPython.core.magic.MagicsManager', allow_none=True)
517 magics_manager = Instance('IPython.core.magic.MagicsManager', allow_none=True)
507
518
508 profile_dir = Instance('IPython.core.application.ProfileDir', allow_none=True)
519 profile_dir = Instance('IPython.core.application.ProfileDir', allow_none=True)
509 @property
520 @property
510 def profile(self):
521 def profile(self):
511 if self.profile_dir is not None:
522 if self.profile_dir is not None:
512 name = os.path.basename(self.profile_dir.location)
523 name = os.path.basename(self.profile_dir.location)
513 return name.replace('profile_','')
524 return name.replace('profile_','')
514
525
515
526
516 # Private interface
527 # Private interface
517 _post_execute = Dict()
528 _post_execute = Dict()
518
529
519 # Tracks any GUI loop loaded for pylab
530 # Tracks any GUI loop loaded for pylab
520 pylab_gui_select = None
531 pylab_gui_select = None
521
532
522 last_execution_succeeded = Bool(True, help='Did last executed command succeeded')
533 last_execution_succeeded = Bool(True, help='Did last executed command succeeded')
523
534
524 last_execution_result = Instance('IPython.core.interactiveshell.ExecutionResult', help='Result of executing the last command', allow_none=True)
535 last_execution_result = Instance('IPython.core.interactiveshell.ExecutionResult', help='Result of executing the last command', allow_none=True)
525
536
526 def __init__(self, ipython_dir=None, profile_dir=None,
537 def __init__(self, ipython_dir=None, profile_dir=None,
527 user_module=None, user_ns=None,
538 user_module=None, user_ns=None,
528 custom_exceptions=((), None), **kwargs):
539 custom_exceptions=((), None), **kwargs):
529 # This is where traits with a config_key argument are updated
540 # This is where traits with a config_key argument are updated
530 # from the values on config.
541 # from the values on config.
531 super(InteractiveShell, self).__init__(**kwargs)
542 super(InteractiveShell, self).__init__(**kwargs)
532 if 'PromptManager' in self.config:
543 if 'PromptManager' in self.config:
533 warn('As of IPython 5.0 `PromptManager` config will have no effect'
544 warn('As of IPython 5.0 `PromptManager` config will have no effect'
534 ' and has been replaced by TerminalInteractiveShell.prompts_class')
545 ' and has been replaced by TerminalInteractiveShell.prompts_class')
535 self.configurables = [self]
546 self.configurables = [self]
536
547
537 # These are relatively independent and stateless
548 # These are relatively independent and stateless
538 self.init_ipython_dir(ipython_dir)
549 self.init_ipython_dir(ipython_dir)
539 self.init_profile_dir(profile_dir)
550 self.init_profile_dir(profile_dir)
540 self.init_instance_attrs()
551 self.init_instance_attrs()
541 self.init_environment()
552 self.init_environment()
542
553
543 # Check if we're in a virtualenv, and set up sys.path.
554 # Check if we're in a virtualenv, and set up sys.path.
544 self.init_virtualenv()
555 self.init_virtualenv()
545
556
546 # Create namespaces (user_ns, user_global_ns, etc.)
557 # Create namespaces (user_ns, user_global_ns, etc.)
547 self.init_create_namespaces(user_module, user_ns)
558 self.init_create_namespaces(user_module, user_ns)
548 # This has to be done after init_create_namespaces because it uses
559 # This has to be done after init_create_namespaces because it uses
549 # something in self.user_ns, but before init_sys_modules, which
560 # something in self.user_ns, but before init_sys_modules, which
550 # is the first thing to modify sys.
561 # is the first thing to modify sys.
551 # TODO: When we override sys.stdout and sys.stderr before this class
562 # TODO: When we override sys.stdout and sys.stderr before this class
552 # is created, we are saving the overridden ones here. Not sure if this
563 # is created, we are saving the overridden ones here. Not sure if this
553 # is what we want to do.
564 # is what we want to do.
554 self.save_sys_module_state()
565 self.save_sys_module_state()
555 self.init_sys_modules()
566 self.init_sys_modules()
556
567
557 # While we're trying to have each part of the code directly access what
568 # While we're trying to have each part of the code directly access what
558 # it needs without keeping redundant references to objects, we have too
569 # it needs without keeping redundant references to objects, we have too
559 # much legacy code that expects ip.db to exist.
570 # much legacy code that expects ip.db to exist.
560 self.db = PickleShareDB(os.path.join(self.profile_dir.location, 'db'))
571 self.db = PickleShareDB(os.path.join(self.profile_dir.location, 'db'))
561
572
562 self.init_history()
573 self.init_history()
563 self.init_encoding()
574 self.init_encoding()
564 self.init_prefilter()
575 self.init_prefilter()
565
576
566 self.init_syntax_highlighting()
577 self.init_syntax_highlighting()
567 self.init_hooks()
578 self.init_hooks()
568 self.init_events()
579 self.init_events()
569 self.init_pushd_popd_magic()
580 self.init_pushd_popd_magic()
570 self.init_user_ns()
581 self.init_user_ns()
571 self.init_logger()
582 self.init_logger()
572 self.init_builtins()
583 self.init_builtins()
573
584
574 # The following was in post_config_initialization
585 # The following was in post_config_initialization
575 self.init_inspector()
586 self.init_inspector()
576 self.raw_input_original = input
587 self.raw_input_original = input
577 self.init_completer()
588 self.init_completer()
578 # TODO: init_io() needs to happen before init_traceback handlers
589 # TODO: init_io() needs to happen before init_traceback handlers
579 # because the traceback handlers hardcode the stdout/stderr streams.
590 # because the traceback handlers hardcode the stdout/stderr streams.
580 # This logic in in debugger.Pdb and should eventually be changed.
591 # This logic in in debugger.Pdb and should eventually be changed.
581 self.init_io()
592 self.init_io()
582 self.init_traceback_handlers(custom_exceptions)
593 self.init_traceback_handlers(custom_exceptions)
583 self.init_prompts()
594 self.init_prompts()
584 self.init_display_formatter()
595 self.init_display_formatter()
585 self.init_display_pub()
596 self.init_display_pub()
586 self.init_data_pub()
597 self.init_data_pub()
587 self.init_displayhook()
598 self.init_displayhook()
588 self.init_magics()
599 self.init_magics()
589 self.init_alias()
600 self.init_alias()
590 self.init_logstart()
601 self.init_logstart()
591 self.init_pdb()
602 self.init_pdb()
592 self.init_extension_manager()
603 self.init_extension_manager()
593 self.init_payload()
604 self.init_payload()
594 self.events.trigger('shell_initialized', self)
605 self.events.trigger('shell_initialized', self)
595 atexit.register(self.atexit_operations)
606 atexit.register(self.atexit_operations)
596
607
597 # The trio runner is used for running Trio in the foreground thread. It
608 # The trio runner is used for running Trio in the foreground thread. It
598 # is different from `_trio_runner(async_fn)` in `async_helpers.py`
609 # is different from `_trio_runner(async_fn)` in `async_helpers.py`
599 # which calls `trio.run()` for every cell. This runner runs all cells
610 # which calls `trio.run()` for every cell. This runner runs all cells
600 # inside a single Trio event loop. If used, it is set from
611 # inside a single Trio event loop. If used, it is set from
601 # `ipykernel.kernelapp`.
612 # `ipykernel.kernelapp`.
602 self.trio_runner = None
613 self.trio_runner = None
603
614
604 def get_ipython(self):
615 def get_ipython(self):
605 """Return the currently running IPython instance."""
616 """Return the currently running IPython instance."""
606 return self
617 return self
607
618
608 #-------------------------------------------------------------------------
619 #-------------------------------------------------------------------------
609 # Trait changed handlers
620 # Trait changed handlers
610 #-------------------------------------------------------------------------
621 #-------------------------------------------------------------------------
611 @observe('ipython_dir')
622 @observe('ipython_dir')
612 def _ipython_dir_changed(self, change):
623 def _ipython_dir_changed(self, change):
613 ensure_dir_exists(change['new'])
624 ensure_dir_exists(change['new'])
614
625
615 def set_autoindent(self,value=None):
626 def set_autoindent(self,value=None):
616 """Set the autoindent flag.
627 """Set the autoindent flag.
617
628
618 If called with no arguments, it acts as a toggle."""
629 If called with no arguments, it acts as a toggle."""
619 if value is None:
630 if value is None:
620 self.autoindent = not self.autoindent
631 self.autoindent = not self.autoindent
621 else:
632 else:
622 self.autoindent = value
633 self.autoindent = value
623
634
624 def set_trio_runner(self, tr):
635 def set_trio_runner(self, tr):
625 self.trio_runner = tr
636 self.trio_runner = tr
626
637
627 #-------------------------------------------------------------------------
638 #-------------------------------------------------------------------------
628 # init_* methods called by __init__
639 # init_* methods called by __init__
629 #-------------------------------------------------------------------------
640 #-------------------------------------------------------------------------
630
641
631 def init_ipython_dir(self, ipython_dir):
642 def init_ipython_dir(self, ipython_dir):
632 if ipython_dir is not None:
643 if ipython_dir is not None:
633 self.ipython_dir = ipython_dir
644 self.ipython_dir = ipython_dir
634 return
645 return
635
646
636 self.ipython_dir = get_ipython_dir()
647 self.ipython_dir = get_ipython_dir()
637
648
638 def init_profile_dir(self, profile_dir):
649 def init_profile_dir(self, profile_dir):
639 if profile_dir is not None:
650 if profile_dir is not None:
640 self.profile_dir = profile_dir
651 self.profile_dir = profile_dir
641 return
652 return
642 self.profile_dir = ProfileDir.create_profile_dir_by_name(
653 self.profile_dir = ProfileDir.create_profile_dir_by_name(
643 self.ipython_dir, "default"
654 self.ipython_dir, "default"
644 )
655 )
645
656
646 def init_instance_attrs(self):
657 def init_instance_attrs(self):
647 self.more = False
658 self.more = False
648
659
649 # command compiler
660 # command compiler
650 self.compile = self.compiler_class()
661 self.compile = self.compiler_class()
651
662
652 # Make an empty namespace, which extension writers can rely on both
663 # Make an empty namespace, which extension writers can rely on both
653 # existing and NEVER being used by ipython itself. This gives them a
664 # existing and NEVER being used by ipython itself. This gives them a
654 # convenient location for storing additional information and state
665 # convenient location for storing additional information and state
655 # their extensions may require, without fear of collisions with other
666 # their extensions may require, without fear of collisions with other
656 # ipython names that may develop later.
667 # ipython names that may develop later.
657 self.meta = Struct()
668 self.meta = Struct()
658
669
659 # Temporary files used for various purposes. Deleted at exit.
670 # Temporary files used for various purposes. Deleted at exit.
660 # The files here are stored with Path from Pathlib
671 # The files here are stored with Path from Pathlib
661 self.tempfiles = []
672 self.tempfiles = []
662 self.tempdirs = []
673 self.tempdirs = []
663
674
664 # keep track of where we started running (mainly for crash post-mortem)
675 # keep track of where we started running (mainly for crash post-mortem)
665 # This is not being used anywhere currently.
676 # This is not being used anywhere currently.
666 self.starting_dir = os.getcwd()
677 self.starting_dir = os.getcwd()
667
678
668 # Indentation management
679 # Indentation management
669 self.indent_current_nsp = 0
680 self.indent_current_nsp = 0
670
681
671 # Dict to track post-execution functions that have been registered
682 # Dict to track post-execution functions that have been registered
672 self._post_execute = {}
683 self._post_execute = {}
673
684
674 def init_environment(self):
685 def init_environment(self):
675 """Any changes we need to make to the user's environment."""
686 """Any changes we need to make to the user's environment."""
676 pass
687 pass
677
688
678 def init_encoding(self):
689 def init_encoding(self):
679 # Get system encoding at startup time. Certain terminals (like Emacs
690 # Get system encoding at startup time. Certain terminals (like Emacs
680 # under Win32 have it set to None, and we need to have a known valid
691 # under Win32 have it set to None, and we need to have a known valid
681 # encoding to use in the raw_input() method
692 # encoding to use in the raw_input() method
682 try:
693 try:
683 self.stdin_encoding = sys.stdin.encoding or 'ascii'
694 self.stdin_encoding = sys.stdin.encoding or 'ascii'
684 except AttributeError:
695 except AttributeError:
685 self.stdin_encoding = 'ascii'
696 self.stdin_encoding = 'ascii'
686
697
687
698
688 @observe('colors')
699 @observe('colors')
689 def init_syntax_highlighting(self, changes=None):
700 def init_syntax_highlighting(self, changes=None):
690 # Python source parser/formatter for syntax highlighting
701 # Python source parser/formatter for syntax highlighting
691 pyformat = PyColorize.Parser(style=self.colors, parent=self).format
702 pyformat = PyColorize.Parser(style=self.colors, parent=self).format
692 self.pycolorize = lambda src: pyformat(src,'str')
703 self.pycolorize = lambda src: pyformat(src,'str')
693
704
694 def refresh_style(self):
705 def refresh_style(self):
695 # No-op here, used in subclass
706 # No-op here, used in subclass
696 pass
707 pass
697
708
698 def init_pushd_popd_magic(self):
709 def init_pushd_popd_magic(self):
699 # for pushd/popd management
710 # for pushd/popd management
700 self.home_dir = get_home_dir()
711 self.home_dir = get_home_dir()
701
712
702 self.dir_stack = []
713 self.dir_stack = []
703
714
704 def init_logger(self):
715 def init_logger(self):
705 self.logger = Logger(self.home_dir, logfname='ipython_log.py',
716 self.logger = Logger(self.home_dir, logfname='ipython_log.py',
706 logmode='rotate')
717 logmode='rotate')
707
718
708 def init_logstart(self):
719 def init_logstart(self):
709 """Initialize logging in case it was requested at the command line.
720 """Initialize logging in case it was requested at the command line.
710 """
721 """
711 if self.logappend:
722 if self.logappend:
712 self.magic('logstart %s append' % self.logappend)
723 self.magic('logstart %s append' % self.logappend)
713 elif self.logfile:
724 elif self.logfile:
714 self.magic('logstart %s' % self.logfile)
725 self.magic('logstart %s' % self.logfile)
715 elif self.logstart:
726 elif self.logstart:
716 self.magic('logstart')
727 self.magic('logstart')
717
728
718
729
719 def init_builtins(self):
730 def init_builtins(self):
720 # A single, static flag that we set to True. Its presence indicates
731 # A single, static flag that we set to True. Its presence indicates
721 # that an IPython shell has been created, and we make no attempts at
732 # that an IPython shell has been created, and we make no attempts at
722 # removing on exit or representing the existence of more than one
733 # removing on exit or representing the existence of more than one
723 # IPython at a time.
734 # IPython at a time.
724 builtin_mod.__dict__['__IPYTHON__'] = True
735 builtin_mod.__dict__['__IPYTHON__'] = True
725 builtin_mod.__dict__['display'] = display
736 builtin_mod.__dict__['display'] = display
726
737
727 self.builtin_trap = BuiltinTrap(shell=self)
738 self.builtin_trap = BuiltinTrap(shell=self)
728
739
729 @observe('colors')
740 @observe('colors')
730 def init_inspector(self, changes=None):
741 def init_inspector(self, changes=None):
731 # Object inspector
742 # Object inspector
732 self.inspector = oinspect.Inspector(oinspect.InspectColors,
743 self.inspector = oinspect.Inspector(oinspect.InspectColors,
733 PyColorize.ANSICodeColors,
744 PyColorize.ANSICodeColors,
734 self.colors,
745 self.colors,
735 self.object_info_string_level)
746 self.object_info_string_level)
736
747
737 def init_io(self):
748 def init_io(self):
738 # implemented in subclasses, TerminalInteractiveShell does call
749 # implemented in subclasses, TerminalInteractiveShell does call
739 # colorama.init().
750 # colorama.init().
740 pass
751 pass
741
752
742 def init_prompts(self):
753 def init_prompts(self):
743 # Set system prompts, so that scripts can decide if they are running
754 # Set system prompts, so that scripts can decide if they are running
744 # interactively.
755 # interactively.
745 sys.ps1 = 'In : '
756 sys.ps1 = 'In : '
746 sys.ps2 = '...: '
757 sys.ps2 = '...: '
747 sys.ps3 = 'Out: '
758 sys.ps3 = 'Out: '
748
759
749 def init_display_formatter(self):
760 def init_display_formatter(self):
750 self.display_formatter = DisplayFormatter(parent=self)
761 self.display_formatter = DisplayFormatter(parent=self)
751 self.configurables.append(self.display_formatter)
762 self.configurables.append(self.display_formatter)
752
763
753 def init_display_pub(self):
764 def init_display_pub(self):
754 self.display_pub = self.display_pub_class(parent=self, shell=self)
765 self.display_pub = self.display_pub_class(parent=self, shell=self)
755 self.configurables.append(self.display_pub)
766 self.configurables.append(self.display_pub)
756
767
757 def init_data_pub(self):
768 def init_data_pub(self):
758 if not self.data_pub_class:
769 if not self.data_pub_class:
759 self.data_pub = None
770 self.data_pub = None
760 return
771 return
761 self.data_pub = self.data_pub_class(parent=self)
772 self.data_pub = self.data_pub_class(parent=self)
762 self.configurables.append(self.data_pub)
773 self.configurables.append(self.data_pub)
763
774
764 def init_displayhook(self):
775 def init_displayhook(self):
765 # Initialize displayhook, set in/out prompts and printing system
776 # Initialize displayhook, set in/out prompts and printing system
766 self.displayhook = self.displayhook_class(
777 self.displayhook = self.displayhook_class(
767 parent=self,
778 parent=self,
768 shell=self,
779 shell=self,
769 cache_size=self.cache_size,
780 cache_size=self.cache_size,
770 )
781 )
771 self.configurables.append(self.displayhook)
782 self.configurables.append(self.displayhook)
772 # This is a context manager that installs/revmoes the displayhook at
783 # This is a context manager that installs/revmoes the displayhook at
773 # the appropriate time.
784 # the appropriate time.
774 self.display_trap = DisplayTrap(hook=self.displayhook)
785 self.display_trap = DisplayTrap(hook=self.displayhook)
775
786
776 @staticmethod
787 @staticmethod
777 def get_path_links(p: Path):
788 def get_path_links(p: Path):
778 """Gets path links including all symlinks
789 """Gets path links including all symlinks
779
790
780 Examples
791 Examples
781 --------
792 --------
782 In [1]: from IPython.core.interactiveshell import InteractiveShell
793 In [1]: from IPython.core.interactiveshell import InteractiveShell
783
794
784 In [2]: import sys, pathlib
795 In [2]: import sys, pathlib
785
796
786 In [3]: paths = InteractiveShell.get_path_links(pathlib.Path(sys.executable))
797 In [3]: paths = InteractiveShell.get_path_links(pathlib.Path(sys.executable))
787
798
788 In [4]: len(paths) == len(set(paths))
799 In [4]: len(paths) == len(set(paths))
789 Out[4]: True
800 Out[4]: True
790
801
791 In [5]: bool(paths)
802 In [5]: bool(paths)
792 Out[5]: True
803 Out[5]: True
793 """
804 """
794 paths = [p]
805 paths = [p]
795 while p.is_symlink():
806 while p.is_symlink():
796 new_path = Path(os.readlink(p))
807 new_path = Path(os.readlink(p))
797 if not new_path.is_absolute():
808 if not new_path.is_absolute():
798 new_path = p.parent / new_path
809 new_path = p.parent / new_path
799 p = new_path
810 p = new_path
800 paths.append(p)
811 paths.append(p)
801 return paths
812 return paths
802
813
803 def init_virtualenv(self):
814 def init_virtualenv(self):
804 """Add the current virtualenv to sys.path so the user can import modules from it.
815 """Add the current virtualenv to sys.path so the user can import modules from it.
805 This isn't perfect: it doesn't use the Python interpreter with which the
816 This isn't perfect: it doesn't use the Python interpreter with which the
806 virtualenv was built, and it ignores the --no-site-packages option. A
817 virtualenv was built, and it ignores the --no-site-packages option. A
807 warning will appear suggesting the user installs IPython in the
818 warning will appear suggesting the user installs IPython in the
808 virtualenv, but for many cases, it probably works well enough.
819 virtualenv, but for many cases, it probably works well enough.
809
820
810 Adapted from code snippets online.
821 Adapted from code snippets online.
811
822
812 http://blog.ufsoft.org/2009/1/29/ipython-and-virtualenv
823 http://blog.ufsoft.org/2009/1/29/ipython-and-virtualenv
813 """
824 """
814 if 'VIRTUAL_ENV' not in os.environ:
825 if 'VIRTUAL_ENV' not in os.environ:
815 # Not in a virtualenv
826 # Not in a virtualenv
816 return
827 return
817 elif os.environ["VIRTUAL_ENV"] == "":
828 elif os.environ["VIRTUAL_ENV"] == "":
818 warn("Virtual env path set to '', please check if this is intended.")
829 warn("Virtual env path set to '', please check if this is intended.")
819 return
830 return
820
831
821 p = Path(sys.executable)
832 p = Path(sys.executable)
822 p_venv = Path(os.environ["VIRTUAL_ENV"])
833 p_venv = Path(os.environ["VIRTUAL_ENV"])
823
834
824 # fallback venv detection:
835 # fallback venv detection:
825 # stdlib venv may symlink sys.executable, so we can't use realpath.
836 # stdlib venv may symlink sys.executable, so we can't use realpath.
826 # but others can symlink *to* the venv Python, so we can't just use sys.executable.
837 # but others can symlink *to* the venv Python, so we can't just use sys.executable.
827 # So we just check every item in the symlink tree (generally <= 3)
838 # So we just check every item in the symlink tree (generally <= 3)
828 paths = self.get_path_links(p)
839 paths = self.get_path_links(p)
829
840
830 # In Cygwin paths like "c:\..." and '\cygdrive\c\...' are possible
841 # In Cygwin paths like "c:\..." and '\cygdrive\c\...' are possible
831 if p_venv.parts[1] == "cygdrive":
842 if p_venv.parts[1] == "cygdrive":
832 drive_name = p_venv.parts[2]
843 drive_name = p_venv.parts[2]
833 p_venv = (drive_name + ":/") / Path(*p_venv.parts[3:])
844 p_venv = (drive_name + ":/") / Path(*p_venv.parts[3:])
834
845
835 if any(p_venv == p.parents[1] for p in paths):
846 if any(p_venv == p.parents[1] for p in paths):
836 # Our exe is inside or has access to the virtualenv, don't need to do anything.
847 # Our exe is inside or has access to the virtualenv, don't need to do anything.
837 return
848 return
838
849
839 if sys.platform == "win32":
850 if sys.platform == "win32":
840 virtual_env = str(Path(os.environ["VIRTUAL_ENV"], "Lib", "site-packages"))
851 virtual_env = str(Path(os.environ["VIRTUAL_ENV"], "Lib", "site-packages"))
841 else:
852 else:
842 virtual_env_path = Path(
853 virtual_env_path = Path(
843 os.environ["VIRTUAL_ENV"], "lib", "python{}.{}", "site-packages"
854 os.environ["VIRTUAL_ENV"], "lib", "python{}.{}", "site-packages"
844 )
855 )
845 p_ver = sys.version_info[:2]
856 p_ver = sys.version_info[:2]
846
857
847 # Predict version from py[thon]-x.x in the $VIRTUAL_ENV
858 # Predict version from py[thon]-x.x in the $VIRTUAL_ENV
848 re_m = re.search(r"\bpy(?:thon)?([23])\.(\d+)\b", os.environ["VIRTUAL_ENV"])
859 re_m = re.search(r"\bpy(?:thon)?([23])\.(\d+)\b", os.environ["VIRTUAL_ENV"])
849 if re_m:
860 if re_m:
850 predicted_path = Path(str(virtual_env_path).format(*re_m.groups()))
861 predicted_path = Path(str(virtual_env_path).format(*re_m.groups()))
851 if predicted_path.exists():
862 if predicted_path.exists():
852 p_ver = re_m.groups()
863 p_ver = re_m.groups()
853
864
854 virtual_env = str(virtual_env_path).format(*p_ver)
865 virtual_env = str(virtual_env_path).format(*p_ver)
855 if self.warn_venv:
866 if self.warn_venv:
856 warn(
867 warn(
857 "Attempting to work in a virtualenv. If you encounter problems, "
868 "Attempting to work in a virtualenv. If you encounter problems, "
858 "please install IPython inside the virtualenv."
869 "please install IPython inside the virtualenv."
859 )
870 )
860 import site
871 import site
861 sys.path.insert(0, virtual_env)
872 sys.path.insert(0, virtual_env)
862 site.addsitedir(virtual_env)
873 site.addsitedir(virtual_env)
863
874
864 #-------------------------------------------------------------------------
875 #-------------------------------------------------------------------------
865 # Things related to injections into the sys module
876 # Things related to injections into the sys module
866 #-------------------------------------------------------------------------
877 #-------------------------------------------------------------------------
867
878
868 def save_sys_module_state(self):
879 def save_sys_module_state(self):
869 """Save the state of hooks in the sys module.
880 """Save the state of hooks in the sys module.
870
881
871 This has to be called after self.user_module is created.
882 This has to be called after self.user_module is created.
872 """
883 """
873 self._orig_sys_module_state = {'stdin': sys.stdin,
884 self._orig_sys_module_state = {'stdin': sys.stdin,
874 'stdout': sys.stdout,
885 'stdout': sys.stdout,
875 'stderr': sys.stderr,
886 'stderr': sys.stderr,
876 'excepthook': sys.excepthook}
887 'excepthook': sys.excepthook}
877 self._orig_sys_modules_main_name = self.user_module.__name__
888 self._orig_sys_modules_main_name = self.user_module.__name__
878 self._orig_sys_modules_main_mod = sys.modules.get(self.user_module.__name__)
889 self._orig_sys_modules_main_mod = sys.modules.get(self.user_module.__name__)
879
890
880 def restore_sys_module_state(self):
891 def restore_sys_module_state(self):
881 """Restore the state of the sys module."""
892 """Restore the state of the sys module."""
882 try:
893 try:
883 for k, v in self._orig_sys_module_state.items():
894 for k, v in self._orig_sys_module_state.items():
884 setattr(sys, k, v)
895 setattr(sys, k, v)
885 except AttributeError:
896 except AttributeError:
886 pass
897 pass
887 # Reset what what done in self.init_sys_modules
898 # Reset what what done in self.init_sys_modules
888 if self._orig_sys_modules_main_mod is not None:
899 if self._orig_sys_modules_main_mod is not None:
889 sys.modules[self._orig_sys_modules_main_name] = self._orig_sys_modules_main_mod
900 sys.modules[self._orig_sys_modules_main_name] = self._orig_sys_modules_main_mod
890
901
891 #-------------------------------------------------------------------------
902 #-------------------------------------------------------------------------
892 # Things related to the banner
903 # Things related to the banner
893 #-------------------------------------------------------------------------
904 #-------------------------------------------------------------------------
894
905
895 @property
906 @property
896 def banner(self):
907 def banner(self):
897 banner = self.banner1
908 banner = self.banner1
898 if self.profile and self.profile != 'default':
909 if self.profile and self.profile != 'default':
899 banner += '\nIPython profile: %s\n' % self.profile
910 banner += '\nIPython profile: %s\n' % self.profile
900 if self.banner2:
911 if self.banner2:
901 banner += '\n' + self.banner2
912 banner += '\n' + self.banner2
902 return banner
913 return banner
903
914
904 def show_banner(self, banner=None):
915 def show_banner(self, banner=None):
905 if banner is None:
916 if banner is None:
906 banner = self.banner
917 banner = self.banner
907 sys.stdout.write(banner)
918 sys.stdout.write(banner)
908
919
909 #-------------------------------------------------------------------------
920 #-------------------------------------------------------------------------
910 # Things related to hooks
921 # Things related to hooks
911 #-------------------------------------------------------------------------
922 #-------------------------------------------------------------------------
912
923
913 def init_hooks(self):
924 def init_hooks(self):
914 # hooks holds pointers used for user-side customizations
925 # hooks holds pointers used for user-side customizations
915 self.hooks = Struct()
926 self.hooks = Struct()
916
927
917 self.strdispatchers = {}
928 self.strdispatchers = {}
918
929
919 # Set all default hooks, defined in the IPython.hooks module.
930 # Set all default hooks, defined in the IPython.hooks module.
920 hooks = IPython.core.hooks
931 hooks = IPython.core.hooks
921 for hook_name in hooks.__all__:
932 for hook_name in hooks.__all__:
922 # default hooks have priority 100, i.e. low; user hooks should have
933 # default hooks have priority 100, i.e. low; user hooks should have
923 # 0-100 priority
934 # 0-100 priority
924 self.set_hook(hook_name, getattr(hooks, hook_name), 100)
935 self.set_hook(hook_name, getattr(hooks, hook_name), 100)
925
936
926 if self.display_page:
937 if self.display_page:
927 self.set_hook('show_in_pager', page.as_hook(page.display_page), 90)
938 self.set_hook('show_in_pager', page.as_hook(page.display_page), 90)
928
939
929 def set_hook(self, name, hook, priority=50, str_key=None, re_key=None):
940 def set_hook(self, name, hook, priority=50, str_key=None, re_key=None):
930 """set_hook(name,hook) -> sets an internal IPython hook.
941 """set_hook(name,hook) -> sets an internal IPython hook.
931
942
932 IPython exposes some of its internal API as user-modifiable hooks. By
943 IPython exposes some of its internal API as user-modifiable hooks. By
933 adding your function to one of these hooks, you can modify IPython's
944 adding your function to one of these hooks, you can modify IPython's
934 behavior to call at runtime your own routines."""
945 behavior to call at runtime your own routines."""
935
946
936 # At some point in the future, this should validate the hook before it
947 # At some point in the future, this should validate the hook before it
937 # accepts it. Probably at least check that the hook takes the number
948 # accepts it. Probably at least check that the hook takes the number
938 # of args it's supposed to.
949 # of args it's supposed to.
939
950
940 f = types.MethodType(hook,self)
951 f = types.MethodType(hook,self)
941
952
942 # check if the hook is for strdispatcher first
953 # check if the hook is for strdispatcher first
943 if str_key is not None:
954 if str_key is not None:
944 sdp = self.strdispatchers.get(name, StrDispatch())
955 sdp = self.strdispatchers.get(name, StrDispatch())
945 sdp.add_s(str_key, f, priority )
956 sdp.add_s(str_key, f, priority )
946 self.strdispatchers[name] = sdp
957 self.strdispatchers[name] = sdp
947 return
958 return
948 if re_key is not None:
959 if re_key is not None:
949 sdp = self.strdispatchers.get(name, StrDispatch())
960 sdp = self.strdispatchers.get(name, StrDispatch())
950 sdp.add_re(re.compile(re_key), f, priority )
961 sdp.add_re(re.compile(re_key), f, priority )
951 self.strdispatchers[name] = sdp
962 self.strdispatchers[name] = sdp
952 return
963 return
953
964
954 dp = getattr(self.hooks, name, None)
965 dp = getattr(self.hooks, name, None)
955 if name not in IPython.core.hooks.__all__:
966 if name not in IPython.core.hooks.__all__:
956 print("Warning! Hook '%s' is not one of %s" % \
967 print("Warning! Hook '%s' is not one of %s" % \
957 (name, IPython.core.hooks.__all__ ))
968 (name, IPython.core.hooks.__all__ ))
958
969
959 if name in IPython.core.hooks.deprecated:
970 if name in IPython.core.hooks.deprecated:
960 alternative = IPython.core.hooks.deprecated[name]
971 alternative = IPython.core.hooks.deprecated[name]
961 raise ValueError(
972 raise ValueError(
962 "Hook {} has been deprecated since IPython 5.0. Use {} instead.".format(
973 "Hook {} has been deprecated since IPython 5.0. Use {} instead.".format(
963 name, alternative
974 name, alternative
964 )
975 )
965 )
976 )
966
977
967 if not dp:
978 if not dp:
968 dp = IPython.core.hooks.CommandChainDispatcher()
979 dp = IPython.core.hooks.CommandChainDispatcher()
969
980
970 try:
981 try:
971 dp.add(f,priority)
982 dp.add(f,priority)
972 except AttributeError:
983 except AttributeError:
973 # it was not commandchain, plain old func - replace
984 # it was not commandchain, plain old func - replace
974 dp = f
985 dp = f
975
986
976 setattr(self.hooks,name, dp)
987 setattr(self.hooks,name, dp)
977
988
978 #-------------------------------------------------------------------------
989 #-------------------------------------------------------------------------
979 # Things related to events
990 # Things related to events
980 #-------------------------------------------------------------------------
991 #-------------------------------------------------------------------------
981
992
982 def init_events(self):
993 def init_events(self):
983 self.events = EventManager(self, available_events)
994 self.events = EventManager(self, available_events)
984
995
985 self.events.register("pre_execute", self._clear_warning_registry)
996 self.events.register("pre_execute", self._clear_warning_registry)
986
997
987 def register_post_execute(self, func):
998 def register_post_execute(self, func):
988 """DEPRECATED: Use ip.events.register('post_run_cell', func)
999 """DEPRECATED: Use ip.events.register('post_run_cell', func)
989
1000
990 Register a function for calling after code execution.
1001 Register a function for calling after code execution.
991 """
1002 """
992 raise ValueError(
1003 raise ValueError(
993 "ip.register_post_execute is deprecated since IPython 1.0, use "
1004 "ip.register_post_execute is deprecated since IPython 1.0, use "
994 "ip.events.register('post_run_cell', func) instead."
1005 "ip.events.register('post_run_cell', func) instead."
995 )
1006 )
996
1007
997 def _clear_warning_registry(self):
1008 def _clear_warning_registry(self):
998 # clear the warning registry, so that different code blocks with
1009 # clear the warning registry, so that different code blocks with
999 # overlapping line number ranges don't cause spurious suppression of
1010 # overlapping line number ranges don't cause spurious suppression of
1000 # warnings (see gh-6611 for details)
1011 # warnings (see gh-6611 for details)
1001 if "__warningregistry__" in self.user_global_ns:
1012 if "__warningregistry__" in self.user_global_ns:
1002 del self.user_global_ns["__warningregistry__"]
1013 del self.user_global_ns["__warningregistry__"]
1003
1014
1004 #-------------------------------------------------------------------------
1015 #-------------------------------------------------------------------------
1005 # Things related to the "main" module
1016 # Things related to the "main" module
1006 #-------------------------------------------------------------------------
1017 #-------------------------------------------------------------------------
1007
1018
1008 def new_main_mod(self, filename, modname):
1019 def new_main_mod(self, filename, modname):
1009 """Return a new 'main' module object for user code execution.
1020 """Return a new 'main' module object for user code execution.
1010
1021
1011 ``filename`` should be the path of the script which will be run in the
1022 ``filename`` should be the path of the script which will be run in the
1012 module. Requests with the same filename will get the same module, with
1023 module. Requests with the same filename will get the same module, with
1013 its namespace cleared.
1024 its namespace cleared.
1014
1025
1015 ``modname`` should be the module name - normally either '__main__' or
1026 ``modname`` should be the module name - normally either '__main__' or
1016 the basename of the file without the extension.
1027 the basename of the file without the extension.
1017
1028
1018 When scripts are executed via %run, we must keep a reference to their
1029 When scripts are executed via %run, we must keep a reference to their
1019 __main__ module around so that Python doesn't
1030 __main__ module around so that Python doesn't
1020 clear it, rendering references to module globals useless.
1031 clear it, rendering references to module globals useless.
1021
1032
1022 This method keeps said reference in a private dict, keyed by the
1033 This method keeps said reference in a private dict, keyed by the
1023 absolute path of the script. This way, for multiple executions of the
1034 absolute path of the script. This way, for multiple executions of the
1024 same script we only keep one copy of the namespace (the last one),
1035 same script we only keep one copy of the namespace (the last one),
1025 thus preventing memory leaks from old references while allowing the
1036 thus preventing memory leaks from old references while allowing the
1026 objects from the last execution to be accessible.
1037 objects from the last execution to be accessible.
1027 """
1038 """
1028 filename = os.path.abspath(filename)
1039 filename = os.path.abspath(filename)
1029 try:
1040 try:
1030 main_mod = self._main_mod_cache[filename]
1041 main_mod = self._main_mod_cache[filename]
1031 except KeyError:
1042 except KeyError:
1032 main_mod = self._main_mod_cache[filename] = types.ModuleType(
1043 main_mod = self._main_mod_cache[filename] = types.ModuleType(
1033 modname,
1044 modname,
1034 doc="Module created for script run in IPython")
1045 doc="Module created for script run in IPython")
1035 else:
1046 else:
1036 main_mod.__dict__.clear()
1047 main_mod.__dict__.clear()
1037 main_mod.__name__ = modname
1048 main_mod.__name__ = modname
1038
1049
1039 main_mod.__file__ = filename
1050 main_mod.__file__ = filename
1040 # It seems pydoc (and perhaps others) needs any module instance to
1051 # It seems pydoc (and perhaps others) needs any module instance to
1041 # implement a __nonzero__ method
1052 # implement a __nonzero__ method
1042 main_mod.__nonzero__ = lambda : True
1053 main_mod.__nonzero__ = lambda : True
1043
1054
1044 return main_mod
1055 return main_mod
1045
1056
1046 def clear_main_mod_cache(self):
1057 def clear_main_mod_cache(self):
1047 """Clear the cache of main modules.
1058 """Clear the cache of main modules.
1048
1059
1049 Mainly for use by utilities like %reset.
1060 Mainly for use by utilities like %reset.
1050
1061
1051 Examples
1062 Examples
1052 --------
1063 --------
1053 In [15]: import IPython
1064 In [15]: import IPython
1054
1065
1055 In [16]: m = _ip.new_main_mod(IPython.__file__, 'IPython')
1066 In [16]: m = _ip.new_main_mod(IPython.__file__, 'IPython')
1056
1067
1057 In [17]: len(_ip._main_mod_cache) > 0
1068 In [17]: len(_ip._main_mod_cache) > 0
1058 Out[17]: True
1069 Out[17]: True
1059
1070
1060 In [18]: _ip.clear_main_mod_cache()
1071 In [18]: _ip.clear_main_mod_cache()
1061
1072
1062 In [19]: len(_ip._main_mod_cache) == 0
1073 In [19]: len(_ip._main_mod_cache) == 0
1063 Out[19]: True
1074 Out[19]: True
1064 """
1075 """
1065 self._main_mod_cache.clear()
1076 self._main_mod_cache.clear()
1066
1077
1067 #-------------------------------------------------------------------------
1078 #-------------------------------------------------------------------------
1068 # Things related to debugging
1079 # Things related to debugging
1069 #-------------------------------------------------------------------------
1080 #-------------------------------------------------------------------------
1070
1081
1071 def init_pdb(self):
1082 def init_pdb(self):
1072 # Set calling of pdb on exceptions
1083 # Set calling of pdb on exceptions
1073 # self.call_pdb is a property
1084 # self.call_pdb is a property
1074 self.call_pdb = self.pdb
1085 self.call_pdb = self.pdb
1075
1086
1076 def _get_call_pdb(self):
1087 def _get_call_pdb(self):
1077 return self._call_pdb
1088 return self._call_pdb
1078
1089
1079 def _set_call_pdb(self,val):
1090 def _set_call_pdb(self,val):
1080
1091
1081 if val not in (0,1,False,True):
1092 if val not in (0,1,False,True):
1082 raise ValueError('new call_pdb value must be boolean')
1093 raise ValueError('new call_pdb value must be boolean')
1083
1094
1084 # store value in instance
1095 # store value in instance
1085 self._call_pdb = val
1096 self._call_pdb = val
1086
1097
1087 # notify the actual exception handlers
1098 # notify the actual exception handlers
1088 self.InteractiveTB.call_pdb = val
1099 self.InteractiveTB.call_pdb = val
1089
1100
1090 call_pdb = property(_get_call_pdb,_set_call_pdb,None,
1101 call_pdb = property(_get_call_pdb,_set_call_pdb,None,
1091 'Control auto-activation of pdb at exceptions')
1102 'Control auto-activation of pdb at exceptions')
1092
1103
1093 def debugger(self,force=False):
1104 def debugger(self,force=False):
1094 """Call the pdb debugger.
1105 """Call the pdb debugger.
1095
1106
1096 Keywords:
1107 Keywords:
1097
1108
1098 - force(False): by default, this routine checks the instance call_pdb
1109 - force(False): by default, this routine checks the instance call_pdb
1099 flag and does not actually invoke the debugger if the flag is false.
1110 flag and does not actually invoke the debugger if the flag is false.
1100 The 'force' option forces the debugger to activate even if the flag
1111 The 'force' option forces the debugger to activate even if the flag
1101 is false.
1112 is false.
1102 """
1113 """
1103
1114
1104 if not (force or self.call_pdb):
1115 if not (force or self.call_pdb):
1105 return
1116 return
1106
1117
1107 if not hasattr(sys,'last_traceback'):
1118 if not hasattr(sys,'last_traceback'):
1108 error('No traceback has been produced, nothing to debug.')
1119 error('No traceback has been produced, nothing to debug.')
1109 return
1120 return
1110
1121
1111 self.InteractiveTB.debugger(force=True)
1122 self.InteractiveTB.debugger(force=True)
1112
1123
1113 #-------------------------------------------------------------------------
1124 #-------------------------------------------------------------------------
1114 # Things related to IPython's various namespaces
1125 # Things related to IPython's various namespaces
1115 #-------------------------------------------------------------------------
1126 #-------------------------------------------------------------------------
1116 default_user_namespaces = True
1127 default_user_namespaces = True
1117
1128
1118 def init_create_namespaces(self, user_module=None, user_ns=None):
1129 def init_create_namespaces(self, user_module=None, user_ns=None):
1119 # Create the namespace where the user will operate. user_ns is
1130 # Create the namespace where the user will operate. user_ns is
1120 # normally the only one used, and it is passed to the exec calls as
1131 # normally the only one used, and it is passed to the exec calls as
1121 # the locals argument. But we do carry a user_global_ns namespace
1132 # the locals argument. But we do carry a user_global_ns namespace
1122 # given as the exec 'globals' argument, This is useful in embedding
1133 # given as the exec 'globals' argument, This is useful in embedding
1123 # situations where the ipython shell opens in a context where the
1134 # situations where the ipython shell opens in a context where the
1124 # distinction between locals and globals is meaningful. For
1135 # distinction between locals and globals is meaningful. For
1125 # non-embedded contexts, it is just the same object as the user_ns dict.
1136 # non-embedded contexts, it is just the same object as the user_ns dict.
1126
1137
1127 # FIXME. For some strange reason, __builtins__ is showing up at user
1138 # FIXME. For some strange reason, __builtins__ is showing up at user
1128 # level as a dict instead of a module. This is a manual fix, but I
1139 # level as a dict instead of a module. This is a manual fix, but I
1129 # should really track down where the problem is coming from. Alex
1140 # should really track down where the problem is coming from. Alex
1130 # Schmolck reported this problem first.
1141 # Schmolck reported this problem first.
1131
1142
1132 # A useful post by Alex Martelli on this topic:
1143 # A useful post by Alex Martelli on this topic:
1133 # Re: inconsistent value from __builtins__
1144 # Re: inconsistent value from __builtins__
1134 # Von: Alex Martelli <aleaxit@yahoo.com>
1145 # Von: Alex Martelli <aleaxit@yahoo.com>
1135 # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends
1146 # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends
1136 # Gruppen: comp.lang.python
1147 # Gruppen: comp.lang.python
1137
1148
1138 # Michael Hohn <hohn@hooknose.lbl.gov> wrote:
1149 # Michael Hohn <hohn@hooknose.lbl.gov> wrote:
1139 # > >>> print type(builtin_check.get_global_binding('__builtins__'))
1150 # > >>> print type(builtin_check.get_global_binding('__builtins__'))
1140 # > <type 'dict'>
1151 # > <type 'dict'>
1141 # > >>> print type(__builtins__)
1152 # > >>> print type(__builtins__)
1142 # > <type 'module'>
1153 # > <type 'module'>
1143 # > Is this difference in return value intentional?
1154 # > Is this difference in return value intentional?
1144
1155
1145 # Well, it's documented that '__builtins__' can be either a dictionary
1156 # Well, it's documented that '__builtins__' can be either a dictionary
1146 # or a module, and it's been that way for a long time. Whether it's
1157 # or a module, and it's been that way for a long time. Whether it's
1147 # intentional (or sensible), I don't know. In any case, the idea is
1158 # intentional (or sensible), I don't know. In any case, the idea is
1148 # that if you need to access the built-in namespace directly, you
1159 # that if you need to access the built-in namespace directly, you
1149 # should start with "import __builtin__" (note, no 's') which will
1160 # should start with "import __builtin__" (note, no 's') which will
1150 # definitely give you a module. Yeah, it's somewhat confusing:-(.
1161 # definitely give you a module. Yeah, it's somewhat confusing:-(.
1151
1162
1152 # These routines return a properly built module and dict as needed by
1163 # These routines return a properly built module and dict as needed by
1153 # the rest of the code, and can also be used by extension writers to
1164 # the rest of the code, and can also be used by extension writers to
1154 # generate properly initialized namespaces.
1165 # generate properly initialized namespaces.
1155 if (user_ns is not None) or (user_module is not None):
1166 if (user_ns is not None) or (user_module is not None):
1156 self.default_user_namespaces = False
1167 self.default_user_namespaces = False
1157 self.user_module, self.user_ns = self.prepare_user_module(user_module, user_ns)
1168 self.user_module, self.user_ns = self.prepare_user_module(user_module, user_ns)
1158
1169
1159 # A record of hidden variables we have added to the user namespace, so
1170 # A record of hidden variables we have added to the user namespace, so
1160 # we can list later only variables defined in actual interactive use.
1171 # we can list later only variables defined in actual interactive use.
1161 self.user_ns_hidden = {}
1172 self.user_ns_hidden = {}
1162
1173
1163 # Now that FakeModule produces a real module, we've run into a nasty
1174 # Now that FakeModule produces a real module, we've run into a nasty
1164 # problem: after script execution (via %run), the module where the user
1175 # problem: after script execution (via %run), the module where the user
1165 # code ran is deleted. Now that this object is a true module (needed
1176 # code ran is deleted. Now that this object is a true module (needed
1166 # so doctest and other tools work correctly), the Python module
1177 # so doctest and other tools work correctly), the Python module
1167 # teardown mechanism runs over it, and sets to None every variable
1178 # teardown mechanism runs over it, and sets to None every variable
1168 # present in that module. Top-level references to objects from the
1179 # present in that module. Top-level references to objects from the
1169 # script survive, because the user_ns is updated with them. However,
1180 # script survive, because the user_ns is updated with them. However,
1170 # calling functions defined in the script that use other things from
1181 # calling functions defined in the script that use other things from
1171 # the script will fail, because the function's closure had references
1182 # the script will fail, because the function's closure had references
1172 # to the original objects, which are now all None. So we must protect
1183 # to the original objects, which are now all None. So we must protect
1173 # these modules from deletion by keeping a cache.
1184 # these modules from deletion by keeping a cache.
1174 #
1185 #
1175 # To avoid keeping stale modules around (we only need the one from the
1186 # To avoid keeping stale modules around (we only need the one from the
1176 # last run), we use a dict keyed with the full path to the script, so
1187 # last run), we use a dict keyed with the full path to the script, so
1177 # only the last version of the module is held in the cache. Note,
1188 # only the last version of the module is held in the cache. Note,
1178 # however, that we must cache the module *namespace contents* (their
1189 # however, that we must cache the module *namespace contents* (their
1179 # __dict__). Because if we try to cache the actual modules, old ones
1190 # __dict__). Because if we try to cache the actual modules, old ones
1180 # (uncached) could be destroyed while still holding references (such as
1191 # (uncached) could be destroyed while still holding references (such as
1181 # those held by GUI objects that tend to be long-lived)>
1192 # those held by GUI objects that tend to be long-lived)>
1182 #
1193 #
1183 # The %reset command will flush this cache. See the cache_main_mod()
1194 # The %reset command will flush this cache. See the cache_main_mod()
1184 # and clear_main_mod_cache() methods for details on use.
1195 # and clear_main_mod_cache() methods for details on use.
1185
1196
1186 # This is the cache used for 'main' namespaces
1197 # This is the cache used for 'main' namespaces
1187 self._main_mod_cache = {}
1198 self._main_mod_cache = {}
1188
1199
1189 # A table holding all the namespaces IPython deals with, so that
1200 # A table holding all the namespaces IPython deals with, so that
1190 # introspection facilities can search easily.
1201 # introspection facilities can search easily.
1191 self.ns_table = {'user_global':self.user_module.__dict__,
1202 self.ns_table = {'user_global':self.user_module.__dict__,
1192 'user_local':self.user_ns,
1203 'user_local':self.user_ns,
1193 'builtin':builtin_mod.__dict__
1204 'builtin':builtin_mod.__dict__
1194 }
1205 }
1195
1206
1196 @property
1207 @property
1197 def user_global_ns(self):
1208 def user_global_ns(self):
1198 return self.user_module.__dict__
1209 return self.user_module.__dict__
1199
1210
1200 def prepare_user_module(self, user_module=None, user_ns=None):
1211 def prepare_user_module(self, user_module=None, user_ns=None):
1201 """Prepare the module and namespace in which user code will be run.
1212 """Prepare the module and namespace in which user code will be run.
1202
1213
1203 When IPython is started normally, both parameters are None: a new module
1214 When IPython is started normally, both parameters are None: a new module
1204 is created automatically, and its __dict__ used as the namespace.
1215 is created automatically, and its __dict__ used as the namespace.
1205
1216
1206 If only user_module is provided, its __dict__ is used as the namespace.
1217 If only user_module is provided, its __dict__ is used as the namespace.
1207 If only user_ns is provided, a dummy module is created, and user_ns
1218 If only user_ns is provided, a dummy module is created, and user_ns
1208 becomes the global namespace. If both are provided (as they may be
1219 becomes the global namespace. If both are provided (as they may be
1209 when embedding), user_ns is the local namespace, and user_module
1220 when embedding), user_ns is the local namespace, and user_module
1210 provides the global namespace.
1221 provides the global namespace.
1211
1222
1212 Parameters
1223 Parameters
1213 ----------
1224 ----------
1214 user_module : module, optional
1225 user_module : module, optional
1215 The current user module in which IPython is being run. If None,
1226 The current user module in which IPython is being run. If None,
1216 a clean module will be created.
1227 a clean module will be created.
1217 user_ns : dict, optional
1228 user_ns : dict, optional
1218 A namespace in which to run interactive commands.
1229 A namespace in which to run interactive commands.
1219
1230
1220 Returns
1231 Returns
1221 -------
1232 -------
1222 A tuple of user_module and user_ns, each properly initialised.
1233 A tuple of user_module and user_ns, each properly initialised.
1223 """
1234 """
1224 if user_module is None and user_ns is not None:
1235 if user_module is None and user_ns is not None:
1225 user_ns.setdefault("__name__", "__main__")
1236 user_ns.setdefault("__name__", "__main__")
1226 user_module = DummyMod()
1237 user_module = DummyMod()
1227 user_module.__dict__ = user_ns
1238 user_module.__dict__ = user_ns
1228
1239
1229 if user_module is None:
1240 if user_module is None:
1230 user_module = types.ModuleType("__main__",
1241 user_module = types.ModuleType("__main__",
1231 doc="Automatically created module for IPython interactive environment")
1242 doc="Automatically created module for IPython interactive environment")
1232
1243
1233 # We must ensure that __builtin__ (without the final 's') is always
1244 # We must ensure that __builtin__ (without the final 's') is always
1234 # available and pointing to the __builtin__ *module*. For more details:
1245 # available and pointing to the __builtin__ *module*. For more details:
1235 # http://mail.python.org/pipermail/python-dev/2001-April/014068.html
1246 # http://mail.python.org/pipermail/python-dev/2001-April/014068.html
1236 user_module.__dict__.setdefault('__builtin__', builtin_mod)
1247 user_module.__dict__.setdefault('__builtin__', builtin_mod)
1237 user_module.__dict__.setdefault('__builtins__', builtin_mod)
1248 user_module.__dict__.setdefault('__builtins__', builtin_mod)
1238
1249
1239 if user_ns is None:
1250 if user_ns is None:
1240 user_ns = user_module.__dict__
1251 user_ns = user_module.__dict__
1241
1252
1242 return user_module, user_ns
1253 return user_module, user_ns
1243
1254
1244 def init_sys_modules(self):
1255 def init_sys_modules(self):
1245 # We need to insert into sys.modules something that looks like a
1256 # We need to insert into sys.modules something that looks like a
1246 # module but which accesses the IPython namespace, for shelve and
1257 # module but which accesses the IPython namespace, for shelve and
1247 # pickle to work interactively. Normally they rely on getting
1258 # pickle to work interactively. Normally they rely on getting
1248 # everything out of __main__, but for embedding purposes each IPython
1259 # everything out of __main__, but for embedding purposes each IPython
1249 # instance has its own private namespace, so we can't go shoving
1260 # instance has its own private namespace, so we can't go shoving
1250 # everything into __main__.
1261 # everything into __main__.
1251
1262
1252 # note, however, that we should only do this for non-embedded
1263 # note, however, that we should only do this for non-embedded
1253 # ipythons, which really mimic the __main__.__dict__ with their own
1264 # ipythons, which really mimic the __main__.__dict__ with their own
1254 # namespace. Embedded instances, on the other hand, should not do
1265 # namespace. Embedded instances, on the other hand, should not do
1255 # this because they need to manage the user local/global namespaces
1266 # this because they need to manage the user local/global namespaces
1256 # only, but they live within a 'normal' __main__ (meaning, they
1267 # only, but they live within a 'normal' __main__ (meaning, they
1257 # shouldn't overtake the execution environment of the script they're
1268 # shouldn't overtake the execution environment of the script they're
1258 # embedded in).
1269 # embedded in).
1259
1270
1260 # This is overridden in the InteractiveShellEmbed subclass to a no-op.
1271 # This is overridden in the InteractiveShellEmbed subclass to a no-op.
1261 main_name = self.user_module.__name__
1272 main_name = self.user_module.__name__
1262 sys.modules[main_name] = self.user_module
1273 sys.modules[main_name] = self.user_module
1263
1274
1264 def init_user_ns(self):
1275 def init_user_ns(self):
1265 """Initialize all user-visible namespaces to their minimum defaults.
1276 """Initialize all user-visible namespaces to their minimum defaults.
1266
1277
1267 Certain history lists are also initialized here, as they effectively
1278 Certain history lists are also initialized here, as they effectively
1268 act as user namespaces.
1279 act as user namespaces.
1269
1280
1270 Notes
1281 Notes
1271 -----
1282 -----
1272 All data structures here are only filled in, they are NOT reset by this
1283 All data structures here are only filled in, they are NOT reset by this
1273 method. If they were not empty before, data will simply be added to
1284 method. If they were not empty before, data will simply be added to
1274 them.
1285 them.
1275 """
1286 """
1276 # This function works in two parts: first we put a few things in
1287 # This function works in two parts: first we put a few things in
1277 # user_ns, and we sync that contents into user_ns_hidden so that these
1288 # user_ns, and we sync that contents into user_ns_hidden so that these
1278 # initial variables aren't shown by %who. After the sync, we add the
1289 # initial variables aren't shown by %who. After the sync, we add the
1279 # rest of what we *do* want the user to see with %who even on a new
1290 # rest of what we *do* want the user to see with %who even on a new
1280 # session (probably nothing, so they really only see their own stuff)
1291 # session (probably nothing, so they really only see their own stuff)
1281
1292
1282 # The user dict must *always* have a __builtin__ reference to the
1293 # The user dict must *always* have a __builtin__ reference to the
1283 # Python standard __builtin__ namespace, which must be imported.
1294 # Python standard __builtin__ namespace, which must be imported.
1284 # This is so that certain operations in prompt evaluation can be
1295 # This is so that certain operations in prompt evaluation can be
1285 # reliably executed with builtins. Note that we can NOT use
1296 # reliably executed with builtins. Note that we can NOT use
1286 # __builtins__ (note the 's'), because that can either be a dict or a
1297 # __builtins__ (note the 's'), because that can either be a dict or a
1287 # module, and can even mutate at runtime, depending on the context
1298 # module, and can even mutate at runtime, depending on the context
1288 # (Python makes no guarantees on it). In contrast, __builtin__ is
1299 # (Python makes no guarantees on it). In contrast, __builtin__ is
1289 # always a module object, though it must be explicitly imported.
1300 # always a module object, though it must be explicitly imported.
1290
1301
1291 # For more details:
1302 # For more details:
1292 # http://mail.python.org/pipermail/python-dev/2001-April/014068.html
1303 # http://mail.python.org/pipermail/python-dev/2001-April/014068.html
1293 ns = {}
1304 ns = {}
1294
1305
1295 # make global variables for user access to the histories
1306 # make global variables for user access to the histories
1296 ns['_ih'] = self.history_manager.input_hist_parsed
1307 ns['_ih'] = self.history_manager.input_hist_parsed
1297 ns['_oh'] = self.history_manager.output_hist
1308 ns['_oh'] = self.history_manager.output_hist
1298 ns['_dh'] = self.history_manager.dir_hist
1309 ns['_dh'] = self.history_manager.dir_hist
1299
1310
1300 # user aliases to input and output histories. These shouldn't show up
1311 # user aliases to input and output histories. These shouldn't show up
1301 # in %who, as they can have very large reprs.
1312 # in %who, as they can have very large reprs.
1302 ns['In'] = self.history_manager.input_hist_parsed
1313 ns['In'] = self.history_manager.input_hist_parsed
1303 ns['Out'] = self.history_manager.output_hist
1314 ns['Out'] = self.history_manager.output_hist
1304
1315
1305 # Store myself as the public api!!!
1316 # Store myself as the public api!!!
1306 ns['get_ipython'] = self.get_ipython
1317 ns['get_ipython'] = self.get_ipython
1307
1318
1308 ns['exit'] = self.exiter
1319 ns['exit'] = self.exiter
1309 ns['quit'] = self.exiter
1320 ns['quit'] = self.exiter
1310
1321
1311 # Sync what we've added so far to user_ns_hidden so these aren't seen
1322 # Sync what we've added so far to user_ns_hidden so these aren't seen
1312 # by %who
1323 # by %who
1313 self.user_ns_hidden.update(ns)
1324 self.user_ns_hidden.update(ns)
1314
1325
1315 # Anything put into ns now would show up in %who. Think twice before
1326 # Anything put into ns now would show up in %who. Think twice before
1316 # putting anything here, as we really want %who to show the user their
1327 # putting anything here, as we really want %who to show the user their
1317 # stuff, not our variables.
1328 # stuff, not our variables.
1318
1329
1319 # Finally, update the real user's namespace
1330 # Finally, update the real user's namespace
1320 self.user_ns.update(ns)
1331 self.user_ns.update(ns)
1321
1332
1322 @property
1333 @property
1323 def all_ns_refs(self):
1334 def all_ns_refs(self):
1324 """Get a list of references to all the namespace dictionaries in which
1335 """Get a list of references to all the namespace dictionaries in which
1325 IPython might store a user-created object.
1336 IPython might store a user-created object.
1326
1337
1327 Note that this does not include the displayhook, which also caches
1338 Note that this does not include the displayhook, which also caches
1328 objects from the output."""
1339 objects from the output."""
1329 return [self.user_ns, self.user_global_ns, self.user_ns_hidden] + \
1340 return [self.user_ns, self.user_global_ns, self.user_ns_hidden] + \
1330 [m.__dict__ for m in self._main_mod_cache.values()]
1341 [m.__dict__ for m in self._main_mod_cache.values()]
1331
1342
1332 def reset(self, new_session=True, aggressive=False):
1343 def reset(self, new_session=True, aggressive=False):
1333 """Clear all internal namespaces, and attempt to release references to
1344 """Clear all internal namespaces, and attempt to release references to
1334 user objects.
1345 user objects.
1335
1346
1336 If new_session is True, a new history session will be opened.
1347 If new_session is True, a new history session will be opened.
1337 """
1348 """
1338 # Clear histories
1349 # Clear histories
1339 self.history_manager.reset(new_session)
1350 self.history_manager.reset(new_session)
1340 # Reset counter used to index all histories
1351 # Reset counter used to index all histories
1341 if new_session:
1352 if new_session:
1342 self.execution_count = 1
1353 self.execution_count = 1
1343
1354
1344 # Reset last execution result
1355 # Reset last execution result
1345 self.last_execution_succeeded = True
1356 self.last_execution_succeeded = True
1346 self.last_execution_result = None
1357 self.last_execution_result = None
1347
1358
1348 # Flush cached output items
1359 # Flush cached output items
1349 if self.displayhook.do_full_cache:
1360 if self.displayhook.do_full_cache:
1350 self.displayhook.flush()
1361 self.displayhook.flush()
1351
1362
1352 # The main execution namespaces must be cleared very carefully,
1363 # The main execution namespaces must be cleared very carefully,
1353 # skipping the deletion of the builtin-related keys, because doing so
1364 # skipping the deletion of the builtin-related keys, because doing so
1354 # would cause errors in many object's __del__ methods.
1365 # would cause errors in many object's __del__ methods.
1355 if self.user_ns is not self.user_global_ns:
1366 if self.user_ns is not self.user_global_ns:
1356 self.user_ns.clear()
1367 self.user_ns.clear()
1357 ns = self.user_global_ns
1368 ns = self.user_global_ns
1358 drop_keys = set(ns.keys())
1369 drop_keys = set(ns.keys())
1359 drop_keys.discard('__builtin__')
1370 drop_keys.discard('__builtin__')
1360 drop_keys.discard('__builtins__')
1371 drop_keys.discard('__builtins__')
1361 drop_keys.discard('__name__')
1372 drop_keys.discard('__name__')
1362 for k in drop_keys:
1373 for k in drop_keys:
1363 del ns[k]
1374 del ns[k]
1364
1375
1365 self.user_ns_hidden.clear()
1376 self.user_ns_hidden.clear()
1366
1377
1367 # Restore the user namespaces to minimal usability
1378 # Restore the user namespaces to minimal usability
1368 self.init_user_ns()
1379 self.init_user_ns()
1369 if aggressive and not hasattr(self, "_sys_modules_keys"):
1380 if aggressive and not hasattr(self, "_sys_modules_keys"):
1370 print("Cannot restore sys.module, no snapshot")
1381 print("Cannot restore sys.module, no snapshot")
1371 elif aggressive:
1382 elif aggressive:
1372 print("culling sys module...")
1383 print("culling sys module...")
1373 current_keys = set(sys.modules.keys())
1384 current_keys = set(sys.modules.keys())
1374 for k in current_keys - self._sys_modules_keys:
1385 for k in current_keys - self._sys_modules_keys:
1375 if k.startswith("multiprocessing"):
1386 if k.startswith("multiprocessing"):
1376 continue
1387 continue
1377 del sys.modules[k]
1388 del sys.modules[k]
1378
1389
1379 # Restore the default and user aliases
1390 # Restore the default and user aliases
1380 self.alias_manager.clear_aliases()
1391 self.alias_manager.clear_aliases()
1381 self.alias_manager.init_aliases()
1392 self.alias_manager.init_aliases()
1382
1393
1383 # Now define aliases that only make sense on the terminal, because they
1394 # Now define aliases that only make sense on the terminal, because they
1384 # need direct access to the console in a way that we can't emulate in
1395 # need direct access to the console in a way that we can't emulate in
1385 # GUI or web frontend
1396 # GUI or web frontend
1386 if os.name == 'posix':
1397 if os.name == 'posix':
1387 for cmd in ('clear', 'more', 'less', 'man'):
1398 for cmd in ('clear', 'more', 'less', 'man'):
1388 if cmd not in self.magics_manager.magics['line']:
1399 if cmd not in self.magics_manager.magics['line']:
1389 self.alias_manager.soft_define_alias(cmd, cmd)
1400 self.alias_manager.soft_define_alias(cmd, cmd)
1390
1401
1391 # Flush the private list of module references kept for script
1402 # Flush the private list of module references kept for script
1392 # execution protection
1403 # execution protection
1393 self.clear_main_mod_cache()
1404 self.clear_main_mod_cache()
1394
1405
1395 def del_var(self, varname, by_name=False):
1406 def del_var(self, varname, by_name=False):
1396 """Delete a variable from the various namespaces, so that, as
1407 """Delete a variable from the various namespaces, so that, as
1397 far as possible, we're not keeping any hidden references to it.
1408 far as possible, we're not keeping any hidden references to it.
1398
1409
1399 Parameters
1410 Parameters
1400 ----------
1411 ----------
1401 varname : str
1412 varname : str
1402 The name of the variable to delete.
1413 The name of the variable to delete.
1403 by_name : bool
1414 by_name : bool
1404 If True, delete variables with the given name in each
1415 If True, delete variables with the given name in each
1405 namespace. If False (default), find the variable in the user
1416 namespace. If False (default), find the variable in the user
1406 namespace, and delete references to it.
1417 namespace, and delete references to it.
1407 """
1418 """
1408 if varname in ('__builtin__', '__builtins__'):
1419 if varname in ('__builtin__', '__builtins__'):
1409 raise ValueError("Refusing to delete %s" % varname)
1420 raise ValueError("Refusing to delete %s" % varname)
1410
1421
1411 ns_refs = self.all_ns_refs
1422 ns_refs = self.all_ns_refs
1412
1423
1413 if by_name: # Delete by name
1424 if by_name: # Delete by name
1414 for ns in ns_refs:
1425 for ns in ns_refs:
1415 try:
1426 try:
1416 del ns[varname]
1427 del ns[varname]
1417 except KeyError:
1428 except KeyError:
1418 pass
1429 pass
1419 else: # Delete by object
1430 else: # Delete by object
1420 try:
1431 try:
1421 obj = self.user_ns[varname]
1432 obj = self.user_ns[varname]
1422 except KeyError as e:
1433 except KeyError as e:
1423 raise NameError("name '%s' is not defined" % varname) from e
1434 raise NameError("name '%s' is not defined" % varname) from e
1424 # Also check in output history
1435 # Also check in output history
1425 ns_refs.append(self.history_manager.output_hist)
1436 ns_refs.append(self.history_manager.output_hist)
1426 for ns in ns_refs:
1437 for ns in ns_refs:
1427 to_delete = [n for n, o in ns.items() if o is obj]
1438 to_delete = [n for n, o in ns.items() if o is obj]
1428 for name in to_delete:
1439 for name in to_delete:
1429 del ns[name]
1440 del ns[name]
1430
1441
1431 # Ensure it is removed from the last execution result
1442 # Ensure it is removed from the last execution result
1432 if self.last_execution_result.result is obj:
1443 if self.last_execution_result.result is obj:
1433 self.last_execution_result = None
1444 self.last_execution_result = None
1434
1445
1435 # displayhook keeps extra references, but not in a dictionary
1446 # displayhook keeps extra references, but not in a dictionary
1436 for name in ('_', '__', '___'):
1447 for name in ('_', '__', '___'):
1437 if getattr(self.displayhook, name) is obj:
1448 if getattr(self.displayhook, name) is obj:
1438 setattr(self.displayhook, name, None)
1449 setattr(self.displayhook, name, None)
1439
1450
1440 def reset_selective(self, regex=None):
1451 def reset_selective(self, regex=None):
1441 """Clear selective variables from internal namespaces based on a
1452 """Clear selective variables from internal namespaces based on a
1442 specified regular expression.
1453 specified regular expression.
1443
1454
1444 Parameters
1455 Parameters
1445 ----------
1456 ----------
1446 regex : string or compiled pattern, optional
1457 regex : string or compiled pattern, optional
1447 A regular expression pattern that will be used in searching
1458 A regular expression pattern that will be used in searching
1448 variable names in the users namespaces.
1459 variable names in the users namespaces.
1449 """
1460 """
1450 if regex is not None:
1461 if regex is not None:
1451 try:
1462 try:
1452 m = re.compile(regex)
1463 m = re.compile(regex)
1453 except TypeError as e:
1464 except TypeError as e:
1454 raise TypeError('regex must be a string or compiled pattern') from e
1465 raise TypeError('regex must be a string or compiled pattern') from e
1455 # Search for keys in each namespace that match the given regex
1466 # Search for keys in each namespace that match the given regex
1456 # If a match is found, delete the key/value pair.
1467 # If a match is found, delete the key/value pair.
1457 for ns in self.all_ns_refs:
1468 for ns in self.all_ns_refs:
1458 for var in ns:
1469 for var in ns:
1459 if m.search(var):
1470 if m.search(var):
1460 del ns[var]
1471 del ns[var]
1461
1472
1462 def push(self, variables, interactive=True):
1473 def push(self, variables, interactive=True):
1463 """Inject a group of variables into the IPython user namespace.
1474 """Inject a group of variables into the IPython user namespace.
1464
1475
1465 Parameters
1476 Parameters
1466 ----------
1477 ----------
1467 variables : dict, str or list/tuple of str
1478 variables : dict, str or list/tuple of str
1468 The variables to inject into the user's namespace. If a dict, a
1479 The variables to inject into the user's namespace. If a dict, a
1469 simple update is done. If a str, the string is assumed to have
1480 simple update is done. If a str, the string is assumed to have
1470 variable names separated by spaces. A list/tuple of str can also
1481 variable names separated by spaces. A list/tuple of str can also
1471 be used to give the variable names. If just the variable names are
1482 be used to give the variable names. If just the variable names are
1472 give (list/tuple/str) then the variable values looked up in the
1483 give (list/tuple/str) then the variable values looked up in the
1473 callers frame.
1484 callers frame.
1474 interactive : bool
1485 interactive : bool
1475 If True (default), the variables will be listed with the ``who``
1486 If True (default), the variables will be listed with the ``who``
1476 magic.
1487 magic.
1477 """
1488 """
1478 vdict = None
1489 vdict = None
1479
1490
1480 # We need a dict of name/value pairs to do namespace updates.
1491 # We need a dict of name/value pairs to do namespace updates.
1481 if isinstance(variables, dict):
1492 if isinstance(variables, dict):
1482 vdict = variables
1493 vdict = variables
1483 elif isinstance(variables, (str, list, tuple)):
1494 elif isinstance(variables, (str, list, tuple)):
1484 if isinstance(variables, str):
1495 if isinstance(variables, str):
1485 vlist = variables.split()
1496 vlist = variables.split()
1486 else:
1497 else:
1487 vlist = variables
1498 vlist = variables
1488 vdict = {}
1499 vdict = {}
1489 cf = sys._getframe(1)
1500 cf = sys._getframe(1)
1490 for name in vlist:
1501 for name in vlist:
1491 try:
1502 try:
1492 vdict[name] = eval(name, cf.f_globals, cf.f_locals)
1503 vdict[name] = eval(name, cf.f_globals, cf.f_locals)
1493 except:
1504 except:
1494 print('Could not get variable %s from %s' %
1505 print('Could not get variable %s from %s' %
1495 (name,cf.f_code.co_name))
1506 (name,cf.f_code.co_name))
1496 else:
1507 else:
1497 raise ValueError('variables must be a dict/str/list/tuple')
1508 raise ValueError('variables must be a dict/str/list/tuple')
1498
1509
1499 # Propagate variables to user namespace
1510 # Propagate variables to user namespace
1500 self.user_ns.update(vdict)
1511 self.user_ns.update(vdict)
1501
1512
1502 # And configure interactive visibility
1513 # And configure interactive visibility
1503 user_ns_hidden = self.user_ns_hidden
1514 user_ns_hidden = self.user_ns_hidden
1504 if interactive:
1515 if interactive:
1505 for name in vdict:
1516 for name in vdict:
1506 user_ns_hidden.pop(name, None)
1517 user_ns_hidden.pop(name, None)
1507 else:
1518 else:
1508 user_ns_hidden.update(vdict)
1519 user_ns_hidden.update(vdict)
1509
1520
1510 def drop_by_id(self, variables):
1521 def drop_by_id(self, variables):
1511 """Remove a dict of variables from the user namespace, if they are the
1522 """Remove a dict of variables from the user namespace, if they are the
1512 same as the values in the dictionary.
1523 same as the values in the dictionary.
1513
1524
1514 This is intended for use by extensions: variables that they've added can
1525 This is intended for use by extensions: variables that they've added can
1515 be taken back out if they are unloaded, without removing any that the
1526 be taken back out if they are unloaded, without removing any that the
1516 user has overwritten.
1527 user has overwritten.
1517
1528
1518 Parameters
1529 Parameters
1519 ----------
1530 ----------
1520 variables : dict
1531 variables : dict
1521 A dictionary mapping object names (as strings) to the objects.
1532 A dictionary mapping object names (as strings) to the objects.
1522 """
1533 """
1523 for name, obj in variables.items():
1534 for name, obj in variables.items():
1524 if name in self.user_ns and self.user_ns[name] is obj:
1535 if name in self.user_ns and self.user_ns[name] is obj:
1525 del self.user_ns[name]
1536 del self.user_ns[name]
1526 self.user_ns_hidden.pop(name, None)
1537 self.user_ns_hidden.pop(name, None)
1527
1538
1528 #-------------------------------------------------------------------------
1539 #-------------------------------------------------------------------------
1529 # Things related to object introspection
1540 # Things related to object introspection
1530 #-------------------------------------------------------------------------
1541 #-------------------------------------------------------------------------
1531
1542
1532 def _ofind(self, oname, namespaces=None):
1543 def _ofind(self, oname, namespaces=None):
1533 """Find an object in the available namespaces.
1544 """Find an object in the available namespaces.
1534
1545
1535 self._ofind(oname) -> dict with keys: found,obj,ospace,ismagic
1546 self._ofind(oname) -> dict with keys: found,obj,ospace,ismagic
1536
1547
1537 Has special code to detect magic functions.
1548 Has special code to detect magic functions.
1538 """
1549 """
1539 oname = oname.strip()
1550 oname = oname.strip()
1540 raw_parts = oname.split(".")
1551 raw_parts = oname.split(".")
1541 parts = []
1552 parts = []
1542 parts_ok = True
1553 parts_ok = True
1543 for p in raw_parts:
1554 for p in raw_parts:
1544 if p.endswith("]"):
1555 if p.endswith("]"):
1545 var, *indices = p.split("[")
1556 var, *indices = p.split("[")
1546 if not var.isidentifier():
1557 if not var.isidentifier():
1547 parts_ok = False
1558 parts_ok = False
1548 break
1559 break
1549 parts.append(var)
1560 parts.append(var)
1550 for ind in indices:
1561 for ind in indices:
1551 if ind[-1] != "]" and not ind[:-1].isnumeric():
1562 if ind[-1] != "]" and not is_integer_string(ind[:-1]):
1552 parts_ok = False
1563 parts_ok = False
1553 break
1564 break
1554 parts.append(ind[:-1])
1565 parts.append(ind[:-1])
1555 continue
1566 continue
1556
1567
1557 if not p.isidentifier():
1568 if not p.isidentifier():
1558 parts_ok = False
1569 parts_ok = False
1559 parts.append(p)
1570 parts.append(p)
1560
1571
1561 if (
1572 if (
1562 not oname.startswith(ESC_MAGIC)
1573 not oname.startswith(ESC_MAGIC)
1563 and not oname.startswith(ESC_MAGIC2)
1574 and not oname.startswith(ESC_MAGIC2)
1564 and not parts_ok
1575 and not parts_ok
1565 ):
1576 ):
1566 return {"found": False}
1577 return {"found": False}
1567
1578
1568 if namespaces is None:
1579 if namespaces is None:
1569 # Namespaces to search in:
1580 # Namespaces to search in:
1570 # Put them in a list. The order is important so that we
1581 # Put them in a list. The order is important so that we
1571 # find things in the same order that Python finds them.
1582 # find things in the same order that Python finds them.
1572 namespaces = [ ('Interactive', self.user_ns),
1583 namespaces = [ ('Interactive', self.user_ns),
1573 ('Interactive (global)', self.user_global_ns),
1584 ('Interactive (global)', self.user_global_ns),
1574 ('Python builtin', builtin_mod.__dict__),
1585 ('Python builtin', builtin_mod.__dict__),
1575 ]
1586 ]
1576
1587
1577 ismagic = False
1588 ismagic = False
1578 isalias = False
1589 isalias = False
1579 found = False
1590 found = False
1580 ospace = None
1591 ospace = None
1581 parent = None
1592 parent = None
1582 obj = None
1593 obj = None
1583
1594
1584
1595
1585 # Look for the given name by splitting it in parts. If the head is
1596 # Look for the given name by splitting it in parts. If the head is
1586 # found, then we look for all the remaining parts as members, and only
1597 # found, then we look for all the remaining parts as members, and only
1587 # declare success if we can find them all.
1598 # declare success if we can find them all.
1588 oname_parts = parts
1599 oname_parts = parts
1589 oname_head, oname_rest = oname_parts[0],oname_parts[1:]
1600 oname_head, oname_rest = oname_parts[0],oname_parts[1:]
1590 for nsname,ns in namespaces:
1601 for nsname,ns in namespaces:
1591 try:
1602 try:
1592 obj = ns[oname_head]
1603 obj = ns[oname_head]
1593 except KeyError:
1604 except KeyError:
1594 continue
1605 continue
1595 else:
1606 else:
1596 for idx, part in enumerate(oname_rest):
1607 for idx, part in enumerate(oname_rest):
1597 try:
1608 try:
1598 parent = obj
1609 parent = obj
1599 # The last part is looked up in a special way to avoid
1610 # The last part is looked up in a special way to avoid
1600 # descriptor invocation as it may raise or have side
1611 # descriptor invocation as it may raise or have side
1601 # effects.
1612 # effects.
1602 if idx == len(oname_rest) - 1:
1613 if idx == len(oname_rest) - 1:
1603 obj = self._getattr_property(obj, part)
1614 obj = self._getattr_property(obj, part)
1604 else:
1615 else:
1605 if part.isnumeric():
1616 if is_integer_string(part):
1606 obj = obj[int(part)]
1617 obj = obj[int(part)]
1607 else:
1618 else:
1608 obj = getattr(obj, part)
1619 obj = getattr(obj, part)
1609 except:
1620 except:
1610 # Blanket except b/c some badly implemented objects
1621 # Blanket except b/c some badly implemented objects
1611 # allow __getattr__ to raise exceptions other than
1622 # allow __getattr__ to raise exceptions other than
1612 # AttributeError, which then crashes IPython.
1623 # AttributeError, which then crashes IPython.
1613 break
1624 break
1614 else:
1625 else:
1615 # If we finish the for loop (no break), we got all members
1626 # If we finish the for loop (no break), we got all members
1616 found = True
1627 found = True
1617 ospace = nsname
1628 ospace = nsname
1618 break # namespace loop
1629 break # namespace loop
1619
1630
1620 # Try to see if it's magic
1631 # Try to see if it's magic
1621 if not found:
1632 if not found:
1622 obj = None
1633 obj = None
1623 if oname.startswith(ESC_MAGIC2):
1634 if oname.startswith(ESC_MAGIC2):
1624 oname = oname.lstrip(ESC_MAGIC2)
1635 oname = oname.lstrip(ESC_MAGIC2)
1625 obj = self.find_cell_magic(oname)
1636 obj = self.find_cell_magic(oname)
1626 elif oname.startswith(ESC_MAGIC):
1637 elif oname.startswith(ESC_MAGIC):
1627 oname = oname.lstrip(ESC_MAGIC)
1638 oname = oname.lstrip(ESC_MAGIC)
1628 obj = self.find_line_magic(oname)
1639 obj = self.find_line_magic(oname)
1629 else:
1640 else:
1630 # search without prefix, so run? will find %run?
1641 # search without prefix, so run? will find %run?
1631 obj = self.find_line_magic(oname)
1642 obj = self.find_line_magic(oname)
1632 if obj is None:
1643 if obj is None:
1633 obj = self.find_cell_magic(oname)
1644 obj = self.find_cell_magic(oname)
1634 if obj is not None:
1645 if obj is not None:
1635 found = True
1646 found = True
1636 ospace = 'IPython internal'
1647 ospace = 'IPython internal'
1637 ismagic = True
1648 ismagic = True
1638 isalias = isinstance(obj, Alias)
1649 isalias = isinstance(obj, Alias)
1639
1650
1640 # Last try: special-case some literals like '', [], {}, etc:
1651 # Last try: special-case some literals like '', [], {}, etc:
1641 if not found and oname_head in ["''",'""','[]','{}','()']:
1652 if not found and oname_head in ["''",'""','[]','{}','()']:
1642 obj = eval(oname_head)
1653 obj = eval(oname_head)
1643 found = True
1654 found = True
1644 ospace = 'Interactive'
1655 ospace = 'Interactive'
1645
1656
1646 return {
1657 return {
1647 'obj':obj,
1658 'obj':obj,
1648 'found':found,
1659 'found':found,
1649 'parent':parent,
1660 'parent':parent,
1650 'ismagic':ismagic,
1661 'ismagic':ismagic,
1651 'isalias':isalias,
1662 'isalias':isalias,
1652 'namespace':ospace
1663 'namespace':ospace
1653 }
1664 }
1654
1665
1655 @staticmethod
1666 @staticmethod
1656 def _getattr_property(obj, attrname):
1667 def _getattr_property(obj, attrname):
1657 """Property-aware getattr to use in object finding.
1668 """Property-aware getattr to use in object finding.
1658
1669
1659 If attrname represents a property, return it unevaluated (in case it has
1670 If attrname represents a property, return it unevaluated (in case it has
1660 side effects or raises an error.
1671 side effects or raises an error.
1661
1672
1662 """
1673 """
1663 if not isinstance(obj, type):
1674 if not isinstance(obj, type):
1664 try:
1675 try:
1665 # `getattr(type(obj), attrname)` is not guaranteed to return
1676 # `getattr(type(obj), attrname)` is not guaranteed to return
1666 # `obj`, but does so for property:
1677 # `obj`, but does so for property:
1667 #
1678 #
1668 # property.__get__(self, None, cls) -> self
1679 # property.__get__(self, None, cls) -> self
1669 #
1680 #
1670 # The universal alternative is to traverse the mro manually
1681 # The universal alternative is to traverse the mro manually
1671 # searching for attrname in class dicts.
1682 # searching for attrname in class dicts.
1672 if attrname.isnumeric():
1683 if is_integer_string(attrname):
1673 return obj[int(attrname)]
1684 return obj[int(attrname)]
1674 else:
1685 else:
1675 attr = getattr(type(obj), attrname)
1686 attr = getattr(type(obj), attrname)
1676 except AttributeError:
1687 except AttributeError:
1677 pass
1688 pass
1678 else:
1689 else:
1679 # This relies on the fact that data descriptors (with both
1690 # This relies on the fact that data descriptors (with both
1680 # __get__ & __set__ magic methods) take precedence over
1691 # __get__ & __set__ magic methods) take precedence over
1681 # instance-level attributes:
1692 # instance-level attributes:
1682 #
1693 #
1683 # class A(object):
1694 # class A(object):
1684 # @property
1695 # @property
1685 # def foobar(self): return 123
1696 # def foobar(self): return 123
1686 # a = A()
1697 # a = A()
1687 # a.__dict__['foobar'] = 345
1698 # a.__dict__['foobar'] = 345
1688 # a.foobar # == 123
1699 # a.foobar # == 123
1689 #
1700 #
1690 # So, a property may be returned right away.
1701 # So, a property may be returned right away.
1691 if isinstance(attr, property):
1702 if isinstance(attr, property):
1692 return attr
1703 return attr
1693
1704
1694 # Nothing helped, fall back.
1705 # Nothing helped, fall back.
1695 return getattr(obj, attrname)
1706 return getattr(obj, attrname)
1696
1707
1697 def _object_find(self, oname, namespaces=None):
1708 def _object_find(self, oname, namespaces=None):
1698 """Find an object and return a struct with info about it."""
1709 """Find an object and return a struct with info about it."""
1699 return Struct(self._ofind(oname, namespaces))
1710 return Struct(self._ofind(oname, namespaces))
1700
1711
1701 def _inspect(self, meth, oname, namespaces=None, **kw):
1712 def _inspect(self, meth, oname, namespaces=None, **kw):
1702 """Generic interface to the inspector system.
1713 """Generic interface to the inspector system.
1703
1714
1704 This function is meant to be called by pdef, pdoc & friends.
1715 This function is meant to be called by pdef, pdoc & friends.
1705 """
1716 """
1706 info = self._object_find(oname, namespaces)
1717 info = self._object_find(oname, namespaces)
1707 docformat = (
1718 docformat = (
1708 sphinxify(self.object_inspect(oname)) if self.sphinxify_docstring else None
1719 sphinxify(self.object_inspect(oname)) if self.sphinxify_docstring else None
1709 )
1720 )
1710 if info.found:
1721 if info.found:
1711 pmethod = getattr(self.inspector, meth)
1722 pmethod = getattr(self.inspector, meth)
1712 # TODO: only apply format_screen to the plain/text repr of the mime
1723 # TODO: only apply format_screen to the plain/text repr of the mime
1713 # bundle.
1724 # bundle.
1714 formatter = format_screen if info.ismagic else docformat
1725 formatter = format_screen if info.ismagic else docformat
1715 if meth == 'pdoc':
1726 if meth == 'pdoc':
1716 pmethod(info.obj, oname, formatter)
1727 pmethod(info.obj, oname, formatter)
1717 elif meth == 'pinfo':
1728 elif meth == 'pinfo':
1718 pmethod(
1729 pmethod(
1719 info.obj,
1730 info.obj,
1720 oname,
1731 oname,
1721 formatter,
1732 formatter,
1722 info,
1733 info,
1723 enable_html_pager=self.enable_html_pager,
1734 enable_html_pager=self.enable_html_pager,
1724 **kw,
1735 **kw,
1725 )
1736 )
1726 else:
1737 else:
1727 pmethod(info.obj, oname)
1738 pmethod(info.obj, oname)
1728 else:
1739 else:
1729 print('Object `%s` not found.' % oname)
1740 print('Object `%s` not found.' % oname)
1730 return 'not found' # so callers can take other action
1741 return 'not found' # so callers can take other action
1731
1742
1732 def object_inspect(self, oname, detail_level=0):
1743 def object_inspect(self, oname, detail_level=0):
1733 """Get object info about oname"""
1744 """Get object info about oname"""
1734 with self.builtin_trap:
1745 with self.builtin_trap:
1735 info = self._object_find(oname)
1746 info = self._object_find(oname)
1736 if info.found:
1747 if info.found:
1737 return self.inspector.info(info.obj, oname, info=info,
1748 return self.inspector.info(info.obj, oname, info=info,
1738 detail_level=detail_level
1749 detail_level=detail_level
1739 )
1750 )
1740 else:
1751 else:
1741 return oinspect.object_info(name=oname, found=False)
1752 return oinspect.object_info(name=oname, found=False)
1742
1753
1743 def object_inspect_text(self, oname, detail_level=0):
1754 def object_inspect_text(self, oname, detail_level=0):
1744 """Get object info as formatted text"""
1755 """Get object info as formatted text"""
1745 return self.object_inspect_mime(oname, detail_level)['text/plain']
1756 return self.object_inspect_mime(oname, detail_level)['text/plain']
1746
1757
1747 def object_inspect_mime(self, oname, detail_level=0, omit_sections=()):
1758 def object_inspect_mime(self, oname, detail_level=0, omit_sections=()):
1748 """Get object info as a mimebundle of formatted representations.
1759 """Get object info as a mimebundle of formatted representations.
1749
1760
1750 A mimebundle is a dictionary, keyed by mime-type.
1761 A mimebundle is a dictionary, keyed by mime-type.
1751 It must always have the key `'text/plain'`.
1762 It must always have the key `'text/plain'`.
1752 """
1763 """
1753 with self.builtin_trap:
1764 with self.builtin_trap:
1754 info = self._object_find(oname)
1765 info = self._object_find(oname)
1755 if info.found:
1766 if info.found:
1756 docformat = (
1767 docformat = (
1757 sphinxify(self.object_inspect(oname))
1768 sphinxify(self.object_inspect(oname))
1758 if self.sphinxify_docstring
1769 if self.sphinxify_docstring
1759 else None
1770 else None
1760 )
1771 )
1761 return self.inspector._get_info(
1772 return self.inspector._get_info(
1762 info.obj,
1773 info.obj,
1763 oname,
1774 oname,
1764 info=info,
1775 info=info,
1765 detail_level=detail_level,
1776 detail_level=detail_level,
1766 formatter=docformat,
1777 formatter=docformat,
1767 omit_sections=omit_sections,
1778 omit_sections=omit_sections,
1768 )
1779 )
1769 else:
1780 else:
1770 raise KeyError(oname)
1781 raise KeyError(oname)
1771
1782
1772 #-------------------------------------------------------------------------
1783 #-------------------------------------------------------------------------
1773 # Things related to history management
1784 # Things related to history management
1774 #-------------------------------------------------------------------------
1785 #-------------------------------------------------------------------------
1775
1786
1776 def init_history(self):
1787 def init_history(self):
1777 """Sets up the command history, and starts regular autosaves."""
1788 """Sets up the command history, and starts regular autosaves."""
1778 self.history_manager = HistoryManager(shell=self, parent=self)
1789 self.history_manager = HistoryManager(shell=self, parent=self)
1779 self.configurables.append(self.history_manager)
1790 self.configurables.append(self.history_manager)
1780
1791
1781 #-------------------------------------------------------------------------
1792 #-------------------------------------------------------------------------
1782 # Things related to exception handling and tracebacks (not debugging)
1793 # Things related to exception handling and tracebacks (not debugging)
1783 #-------------------------------------------------------------------------
1794 #-------------------------------------------------------------------------
1784
1795
1785 debugger_cls = InterruptiblePdb
1796 debugger_cls = InterruptiblePdb
1786
1797
1787 def init_traceback_handlers(self, custom_exceptions):
1798 def init_traceback_handlers(self, custom_exceptions):
1788 # Syntax error handler.
1799 # Syntax error handler.
1789 self.SyntaxTB = ultratb.SyntaxTB(color_scheme='NoColor', parent=self)
1800 self.SyntaxTB = ultratb.SyntaxTB(color_scheme='NoColor', parent=self)
1790
1801
1791 # The interactive one is initialized with an offset, meaning we always
1802 # The interactive one is initialized with an offset, meaning we always
1792 # want to remove the topmost item in the traceback, which is our own
1803 # want to remove the topmost item in the traceback, which is our own
1793 # internal code. Valid modes: ['Plain','Context','Verbose','Minimal']
1804 # internal code. Valid modes: ['Plain','Context','Verbose','Minimal']
1794 self.InteractiveTB = ultratb.AutoFormattedTB(mode = 'Plain',
1805 self.InteractiveTB = ultratb.AutoFormattedTB(mode = 'Plain',
1795 color_scheme='NoColor',
1806 color_scheme='NoColor',
1796 tb_offset = 1,
1807 tb_offset = 1,
1797 check_cache=check_linecache_ipython,
1808 check_cache=check_linecache_ipython,
1798 debugger_cls=self.debugger_cls, parent=self)
1809 debugger_cls=self.debugger_cls, parent=self)
1799
1810
1800 # The instance will store a pointer to the system-wide exception hook,
1811 # The instance will store a pointer to the system-wide exception hook,
1801 # so that runtime code (such as magics) can access it. This is because
1812 # so that runtime code (such as magics) can access it. This is because
1802 # during the read-eval loop, it may get temporarily overwritten.
1813 # during the read-eval loop, it may get temporarily overwritten.
1803 self.sys_excepthook = sys.excepthook
1814 self.sys_excepthook = sys.excepthook
1804
1815
1805 # and add any custom exception handlers the user may have specified
1816 # and add any custom exception handlers the user may have specified
1806 self.set_custom_exc(*custom_exceptions)
1817 self.set_custom_exc(*custom_exceptions)
1807
1818
1808 # Set the exception mode
1819 # Set the exception mode
1809 self.InteractiveTB.set_mode(mode=self.xmode)
1820 self.InteractiveTB.set_mode(mode=self.xmode)
1810
1821
1811 def set_custom_exc(self, exc_tuple, handler):
1822 def set_custom_exc(self, exc_tuple, handler):
1812 """set_custom_exc(exc_tuple, handler)
1823 """set_custom_exc(exc_tuple, handler)
1813
1824
1814 Set a custom exception handler, which will be called if any of the
1825 Set a custom exception handler, which will be called if any of the
1815 exceptions in exc_tuple occur in the mainloop (specifically, in the
1826 exceptions in exc_tuple occur in the mainloop (specifically, in the
1816 run_code() method).
1827 run_code() method).
1817
1828
1818 Parameters
1829 Parameters
1819 ----------
1830 ----------
1820 exc_tuple : tuple of exception classes
1831 exc_tuple : tuple of exception classes
1821 A *tuple* of exception classes, for which to call the defined
1832 A *tuple* of exception classes, for which to call the defined
1822 handler. It is very important that you use a tuple, and NOT A
1833 handler. It is very important that you use a tuple, and NOT A
1823 LIST here, because of the way Python's except statement works. If
1834 LIST here, because of the way Python's except statement works. If
1824 you only want to trap a single exception, use a singleton tuple::
1835 you only want to trap a single exception, use a singleton tuple::
1825
1836
1826 exc_tuple == (MyCustomException,)
1837 exc_tuple == (MyCustomException,)
1827
1838
1828 handler : callable
1839 handler : callable
1829 handler must have the following signature::
1840 handler must have the following signature::
1830
1841
1831 def my_handler(self, etype, value, tb, tb_offset=None):
1842 def my_handler(self, etype, value, tb, tb_offset=None):
1832 ...
1843 ...
1833 return structured_traceback
1844 return structured_traceback
1834
1845
1835 Your handler must return a structured traceback (a list of strings),
1846 Your handler must return a structured traceback (a list of strings),
1836 or None.
1847 or None.
1837
1848
1838 This will be made into an instance method (via types.MethodType)
1849 This will be made into an instance method (via types.MethodType)
1839 of IPython itself, and it will be called if any of the exceptions
1850 of IPython itself, and it will be called if any of the exceptions
1840 listed in the exc_tuple are caught. If the handler is None, an
1851 listed in the exc_tuple are caught. If the handler is None, an
1841 internal basic one is used, which just prints basic info.
1852 internal basic one is used, which just prints basic info.
1842
1853
1843 To protect IPython from crashes, if your handler ever raises an
1854 To protect IPython from crashes, if your handler ever raises an
1844 exception or returns an invalid result, it will be immediately
1855 exception or returns an invalid result, it will be immediately
1845 disabled.
1856 disabled.
1846
1857
1847 Notes
1858 Notes
1848 -----
1859 -----
1849 WARNING: by putting in your own exception handler into IPython's main
1860 WARNING: by putting in your own exception handler into IPython's main
1850 execution loop, you run a very good chance of nasty crashes. This
1861 execution loop, you run a very good chance of nasty crashes. This
1851 facility should only be used if you really know what you are doing.
1862 facility should only be used if you really know what you are doing.
1852 """
1863 """
1853
1864
1854 if not isinstance(exc_tuple, tuple):
1865 if not isinstance(exc_tuple, tuple):
1855 raise TypeError("The custom exceptions must be given as a tuple.")
1866 raise TypeError("The custom exceptions must be given as a tuple.")
1856
1867
1857 def dummy_handler(self, etype, value, tb, tb_offset=None):
1868 def dummy_handler(self, etype, value, tb, tb_offset=None):
1858 print('*** Simple custom exception handler ***')
1869 print('*** Simple custom exception handler ***')
1859 print('Exception type :', etype)
1870 print('Exception type :', etype)
1860 print('Exception value:', value)
1871 print('Exception value:', value)
1861 print('Traceback :', tb)
1872 print('Traceback :', tb)
1862
1873
1863 def validate_stb(stb):
1874 def validate_stb(stb):
1864 """validate structured traceback return type
1875 """validate structured traceback return type
1865
1876
1866 return type of CustomTB *should* be a list of strings, but allow
1877 return type of CustomTB *should* be a list of strings, but allow
1867 single strings or None, which are harmless.
1878 single strings or None, which are harmless.
1868
1879
1869 This function will *always* return a list of strings,
1880 This function will *always* return a list of strings,
1870 and will raise a TypeError if stb is inappropriate.
1881 and will raise a TypeError if stb is inappropriate.
1871 """
1882 """
1872 msg = "CustomTB must return list of strings, not %r" % stb
1883 msg = "CustomTB must return list of strings, not %r" % stb
1873 if stb is None:
1884 if stb is None:
1874 return []
1885 return []
1875 elif isinstance(stb, str):
1886 elif isinstance(stb, str):
1876 return [stb]
1887 return [stb]
1877 elif not isinstance(stb, list):
1888 elif not isinstance(stb, list):
1878 raise TypeError(msg)
1889 raise TypeError(msg)
1879 # it's a list
1890 # it's a list
1880 for line in stb:
1891 for line in stb:
1881 # check every element
1892 # check every element
1882 if not isinstance(line, str):
1893 if not isinstance(line, str):
1883 raise TypeError(msg)
1894 raise TypeError(msg)
1884 return stb
1895 return stb
1885
1896
1886 if handler is None:
1897 if handler is None:
1887 wrapped = dummy_handler
1898 wrapped = dummy_handler
1888 else:
1899 else:
1889 def wrapped(self,etype,value,tb,tb_offset=None):
1900 def wrapped(self,etype,value,tb,tb_offset=None):
1890 """wrap CustomTB handler, to protect IPython from user code
1901 """wrap CustomTB handler, to protect IPython from user code
1891
1902
1892 This makes it harder (but not impossible) for custom exception
1903 This makes it harder (but not impossible) for custom exception
1893 handlers to crash IPython.
1904 handlers to crash IPython.
1894 """
1905 """
1895 try:
1906 try:
1896 stb = handler(self,etype,value,tb,tb_offset=tb_offset)
1907 stb = handler(self,etype,value,tb,tb_offset=tb_offset)
1897 return validate_stb(stb)
1908 return validate_stb(stb)
1898 except:
1909 except:
1899 # clear custom handler immediately
1910 # clear custom handler immediately
1900 self.set_custom_exc((), None)
1911 self.set_custom_exc((), None)
1901 print("Custom TB Handler failed, unregistering", file=sys.stderr)
1912 print("Custom TB Handler failed, unregistering", file=sys.stderr)
1902 # show the exception in handler first
1913 # show the exception in handler first
1903 stb = self.InteractiveTB.structured_traceback(*sys.exc_info())
1914 stb = self.InteractiveTB.structured_traceback(*sys.exc_info())
1904 print(self.InteractiveTB.stb2text(stb))
1915 print(self.InteractiveTB.stb2text(stb))
1905 print("The original exception:")
1916 print("The original exception:")
1906 stb = self.InteractiveTB.structured_traceback(
1917 stb = self.InteractiveTB.structured_traceback(
1907 (etype,value,tb), tb_offset=tb_offset
1918 (etype,value,tb), tb_offset=tb_offset
1908 )
1919 )
1909 return stb
1920 return stb
1910
1921
1911 self.CustomTB = types.MethodType(wrapped,self)
1922 self.CustomTB = types.MethodType(wrapped,self)
1912 self.custom_exceptions = exc_tuple
1923 self.custom_exceptions = exc_tuple
1913
1924
1914 def excepthook(self, etype, value, tb):
1925 def excepthook(self, etype, value, tb):
1915 """One more defense for GUI apps that call sys.excepthook.
1926 """One more defense for GUI apps that call sys.excepthook.
1916
1927
1917 GUI frameworks like wxPython trap exceptions and call
1928 GUI frameworks like wxPython trap exceptions and call
1918 sys.excepthook themselves. I guess this is a feature that
1929 sys.excepthook themselves. I guess this is a feature that
1919 enables them to keep running after exceptions that would
1930 enables them to keep running after exceptions that would
1920 otherwise kill their mainloop. This is a bother for IPython
1931 otherwise kill their mainloop. This is a bother for IPython
1921 which expects to catch all of the program exceptions with a try:
1932 which expects to catch all of the program exceptions with a try:
1922 except: statement.
1933 except: statement.
1923
1934
1924 Normally, IPython sets sys.excepthook to a CrashHandler instance, so if
1935 Normally, IPython sets sys.excepthook to a CrashHandler instance, so if
1925 any app directly invokes sys.excepthook, it will look to the user like
1936 any app directly invokes sys.excepthook, it will look to the user like
1926 IPython crashed. In order to work around this, we can disable the
1937 IPython crashed. In order to work around this, we can disable the
1927 CrashHandler and replace it with this excepthook instead, which prints a
1938 CrashHandler and replace it with this excepthook instead, which prints a
1928 regular traceback using our InteractiveTB. In this fashion, apps which
1939 regular traceback using our InteractiveTB. In this fashion, apps which
1929 call sys.excepthook will generate a regular-looking exception from
1940 call sys.excepthook will generate a regular-looking exception from
1930 IPython, and the CrashHandler will only be triggered by real IPython
1941 IPython, and the CrashHandler will only be triggered by real IPython
1931 crashes.
1942 crashes.
1932
1943
1933 This hook should be used sparingly, only in places which are not likely
1944 This hook should be used sparingly, only in places which are not likely
1934 to be true IPython errors.
1945 to be true IPython errors.
1935 """
1946 """
1936 self.showtraceback((etype, value, tb), tb_offset=0)
1947 self.showtraceback((etype, value, tb), tb_offset=0)
1937
1948
1938 def _get_exc_info(self, exc_tuple=None):
1949 def _get_exc_info(self, exc_tuple=None):
1939 """get exc_info from a given tuple, sys.exc_info() or sys.last_type etc.
1950 """get exc_info from a given tuple, sys.exc_info() or sys.last_type etc.
1940
1951
1941 Ensures sys.last_type,value,traceback hold the exc_info we found,
1952 Ensures sys.last_type,value,traceback hold the exc_info we found,
1942 from whichever source.
1953 from whichever source.
1943
1954
1944 raises ValueError if none of these contain any information
1955 raises ValueError if none of these contain any information
1945 """
1956 """
1946 if exc_tuple is None:
1957 if exc_tuple is None:
1947 etype, value, tb = sys.exc_info()
1958 etype, value, tb = sys.exc_info()
1948 else:
1959 else:
1949 etype, value, tb = exc_tuple
1960 etype, value, tb = exc_tuple
1950
1961
1951 if etype is None:
1962 if etype is None:
1952 if hasattr(sys, 'last_type'):
1963 if hasattr(sys, 'last_type'):
1953 etype, value, tb = sys.last_type, sys.last_value, \
1964 etype, value, tb = sys.last_type, sys.last_value, \
1954 sys.last_traceback
1965 sys.last_traceback
1955
1966
1956 if etype is None:
1967 if etype is None:
1957 raise ValueError("No exception to find")
1968 raise ValueError("No exception to find")
1958
1969
1959 # Now store the exception info in sys.last_type etc.
1970 # Now store the exception info in sys.last_type etc.
1960 # WARNING: these variables are somewhat deprecated and not
1971 # WARNING: these variables are somewhat deprecated and not
1961 # necessarily safe to use in a threaded environment, but tools
1972 # necessarily safe to use in a threaded environment, but tools
1962 # like pdb depend on their existence, so let's set them. If we
1973 # like pdb depend on their existence, so let's set them. If we
1963 # find problems in the field, we'll need to revisit their use.
1974 # find problems in the field, we'll need to revisit their use.
1964 sys.last_type = etype
1975 sys.last_type = etype
1965 sys.last_value = value
1976 sys.last_value = value
1966 sys.last_traceback = tb
1977 sys.last_traceback = tb
1967
1978
1968 return etype, value, tb
1979 return etype, value, tb
1969
1980
1970 def show_usage_error(self, exc):
1981 def show_usage_error(self, exc):
1971 """Show a short message for UsageErrors
1982 """Show a short message for UsageErrors
1972
1983
1973 These are special exceptions that shouldn't show a traceback.
1984 These are special exceptions that shouldn't show a traceback.
1974 """
1985 """
1975 print("UsageError: %s" % exc, file=sys.stderr)
1986 print("UsageError: %s" % exc, file=sys.stderr)
1976
1987
1977 def get_exception_only(self, exc_tuple=None):
1988 def get_exception_only(self, exc_tuple=None):
1978 """
1989 """
1979 Return as a string (ending with a newline) the exception that
1990 Return as a string (ending with a newline) the exception that
1980 just occurred, without any traceback.
1991 just occurred, without any traceback.
1981 """
1992 """
1982 etype, value, tb = self._get_exc_info(exc_tuple)
1993 etype, value, tb = self._get_exc_info(exc_tuple)
1983 msg = traceback.format_exception_only(etype, value)
1994 msg = traceback.format_exception_only(etype, value)
1984 return ''.join(msg)
1995 return ''.join(msg)
1985
1996
1986 def showtraceback(self, exc_tuple=None, filename=None, tb_offset=None,
1997 def showtraceback(self, exc_tuple=None, filename=None, tb_offset=None,
1987 exception_only=False, running_compiled_code=False):
1998 exception_only=False, running_compiled_code=False):
1988 """Display the exception that just occurred.
1999 """Display the exception that just occurred.
1989
2000
1990 If nothing is known about the exception, this is the method which
2001 If nothing is known about the exception, this is the method which
1991 should be used throughout the code for presenting user tracebacks,
2002 should be used throughout the code for presenting user tracebacks,
1992 rather than directly invoking the InteractiveTB object.
2003 rather than directly invoking the InteractiveTB object.
1993
2004
1994 A specific showsyntaxerror() also exists, but this method can take
2005 A specific showsyntaxerror() also exists, but this method can take
1995 care of calling it if needed, so unless you are explicitly catching a
2006 care of calling it if needed, so unless you are explicitly catching a
1996 SyntaxError exception, don't try to analyze the stack manually and
2007 SyntaxError exception, don't try to analyze the stack manually and
1997 simply call this method."""
2008 simply call this method."""
1998
2009
1999 try:
2010 try:
2000 try:
2011 try:
2001 etype, value, tb = self._get_exc_info(exc_tuple)
2012 etype, value, tb = self._get_exc_info(exc_tuple)
2002 except ValueError:
2013 except ValueError:
2003 print('No traceback available to show.', file=sys.stderr)
2014 print('No traceback available to show.', file=sys.stderr)
2004 return
2015 return
2005
2016
2006 if issubclass(etype, SyntaxError):
2017 if issubclass(etype, SyntaxError):
2007 # Though this won't be called by syntax errors in the input
2018 # Though this won't be called by syntax errors in the input
2008 # line, there may be SyntaxError cases with imported code.
2019 # line, there may be SyntaxError cases with imported code.
2009 self.showsyntaxerror(filename, running_compiled_code)
2020 self.showsyntaxerror(filename, running_compiled_code)
2010 elif etype is UsageError:
2021 elif etype is UsageError:
2011 self.show_usage_error(value)
2022 self.show_usage_error(value)
2012 else:
2023 else:
2013 if exception_only:
2024 if exception_only:
2014 stb = ['An exception has occurred, use %tb to see '
2025 stb = ['An exception has occurred, use %tb to see '
2015 'the full traceback.\n']
2026 'the full traceback.\n']
2016 stb.extend(self.InteractiveTB.get_exception_only(etype,
2027 stb.extend(self.InteractiveTB.get_exception_only(etype,
2017 value))
2028 value))
2018 else:
2029 else:
2019 try:
2030 try:
2020 # Exception classes can customise their traceback - we
2031 # Exception classes can customise their traceback - we
2021 # use this in IPython.parallel for exceptions occurring
2032 # use this in IPython.parallel for exceptions occurring
2022 # in the engines. This should return a list of strings.
2033 # in the engines. This should return a list of strings.
2023 if hasattr(value, "_render_traceback_"):
2034 if hasattr(value, "_render_traceback_"):
2024 stb = value._render_traceback_()
2035 stb = value._render_traceback_()
2025 else:
2036 else:
2026 stb = self.InteractiveTB.structured_traceback(
2037 stb = self.InteractiveTB.structured_traceback(
2027 etype, value, tb, tb_offset=tb_offset
2038 etype, value, tb, tb_offset=tb_offset
2028 )
2039 )
2029
2040
2030 except Exception:
2041 except Exception:
2031 print(
2042 print(
2032 "Unexpected exception formatting exception. Falling back to standard exception"
2043 "Unexpected exception formatting exception. Falling back to standard exception"
2033 )
2044 )
2034 traceback.print_exc()
2045 traceback.print_exc()
2035 return None
2046 return None
2036
2047
2037 self._showtraceback(etype, value, stb)
2048 self._showtraceback(etype, value, stb)
2038 if self.call_pdb:
2049 if self.call_pdb:
2039 # drop into debugger
2050 # drop into debugger
2040 self.debugger(force=True)
2051 self.debugger(force=True)
2041 return
2052 return
2042
2053
2043 # Actually show the traceback
2054 # Actually show the traceback
2044 self._showtraceback(etype, value, stb)
2055 self._showtraceback(etype, value, stb)
2045
2056
2046 except KeyboardInterrupt:
2057 except KeyboardInterrupt:
2047 print('\n' + self.get_exception_only(), file=sys.stderr)
2058 print('\n' + self.get_exception_only(), file=sys.stderr)
2048
2059
2049 def _showtraceback(self, etype, evalue, stb: str):
2060 def _showtraceback(self, etype, evalue, stb: str):
2050 """Actually show a traceback.
2061 """Actually show a traceback.
2051
2062
2052 Subclasses may override this method to put the traceback on a different
2063 Subclasses may override this method to put the traceback on a different
2053 place, like a side channel.
2064 place, like a side channel.
2054 """
2065 """
2055 val = self.InteractiveTB.stb2text(stb)
2066 val = self.InteractiveTB.stb2text(stb)
2056 try:
2067 try:
2057 print(val)
2068 print(val)
2058 except UnicodeEncodeError:
2069 except UnicodeEncodeError:
2059 print(val.encode("utf-8", "backslashreplace").decode())
2070 print(val.encode("utf-8", "backslashreplace").decode())
2060
2071
2061 def showsyntaxerror(self, filename=None, running_compiled_code=False):
2072 def showsyntaxerror(self, filename=None, running_compiled_code=False):
2062 """Display the syntax error that just occurred.
2073 """Display the syntax error that just occurred.
2063
2074
2064 This doesn't display a stack trace because there isn't one.
2075 This doesn't display a stack trace because there isn't one.
2065
2076
2066 If a filename is given, it is stuffed in the exception instead
2077 If a filename is given, it is stuffed in the exception instead
2067 of what was there before (because Python's parser always uses
2078 of what was there before (because Python's parser always uses
2068 "<string>" when reading from a string).
2079 "<string>" when reading from a string).
2069
2080
2070 If the syntax error occurred when running a compiled code (i.e. running_compile_code=True),
2081 If the syntax error occurred when running a compiled code (i.e. running_compile_code=True),
2071 longer stack trace will be displayed.
2082 longer stack trace will be displayed.
2072 """
2083 """
2073 etype, value, last_traceback = self._get_exc_info()
2084 etype, value, last_traceback = self._get_exc_info()
2074
2085
2075 if filename and issubclass(etype, SyntaxError):
2086 if filename and issubclass(etype, SyntaxError):
2076 try:
2087 try:
2077 value.filename = filename
2088 value.filename = filename
2078 except:
2089 except:
2079 # Not the format we expect; leave it alone
2090 # Not the format we expect; leave it alone
2080 pass
2091 pass
2081
2092
2082 # If the error occurred when executing compiled code, we should provide full stacktrace.
2093 # If the error occurred when executing compiled code, we should provide full stacktrace.
2083 elist = traceback.extract_tb(last_traceback) if running_compiled_code else []
2094 elist = traceback.extract_tb(last_traceback) if running_compiled_code else []
2084 stb = self.SyntaxTB.structured_traceback(etype, value, elist)
2095 stb = self.SyntaxTB.structured_traceback(etype, value, elist)
2085 self._showtraceback(etype, value, stb)
2096 self._showtraceback(etype, value, stb)
2086
2097
2087 # This is overridden in TerminalInteractiveShell to show a message about
2098 # This is overridden in TerminalInteractiveShell to show a message about
2088 # the %paste magic.
2099 # the %paste magic.
2089 def showindentationerror(self):
2100 def showindentationerror(self):
2090 """Called by _run_cell when there's an IndentationError in code entered
2101 """Called by _run_cell when there's an IndentationError in code entered
2091 at the prompt.
2102 at the prompt.
2092
2103
2093 This is overridden in TerminalInteractiveShell to show a message about
2104 This is overridden in TerminalInteractiveShell to show a message about
2094 the %paste magic."""
2105 the %paste magic."""
2095 self.showsyntaxerror()
2106 self.showsyntaxerror()
2096
2107
2097 @skip_doctest
2108 @skip_doctest
2098 def set_next_input(self, s, replace=False):
2109 def set_next_input(self, s, replace=False):
2099 """ Sets the 'default' input string for the next command line.
2110 """ Sets the 'default' input string for the next command line.
2100
2111
2101 Example::
2112 Example::
2102
2113
2103 In [1]: _ip.set_next_input("Hello Word")
2114 In [1]: _ip.set_next_input("Hello Word")
2104 In [2]: Hello Word_ # cursor is here
2115 In [2]: Hello Word_ # cursor is here
2105 """
2116 """
2106 self.rl_next_input = s
2117 self.rl_next_input = s
2107
2118
2108 def _indent_current_str(self):
2119 def _indent_current_str(self):
2109 """return the current level of indentation as a string"""
2120 """return the current level of indentation as a string"""
2110 return self.input_splitter.get_indent_spaces() * ' '
2121 return self.input_splitter.get_indent_spaces() * ' '
2111
2122
2112 #-------------------------------------------------------------------------
2123 #-------------------------------------------------------------------------
2113 # Things related to text completion
2124 # Things related to text completion
2114 #-------------------------------------------------------------------------
2125 #-------------------------------------------------------------------------
2115
2126
2116 def init_completer(self):
2127 def init_completer(self):
2117 """Initialize the completion machinery.
2128 """Initialize the completion machinery.
2118
2129
2119 This creates completion machinery that can be used by client code,
2130 This creates completion machinery that can be used by client code,
2120 either interactively in-process (typically triggered by the readline
2131 either interactively in-process (typically triggered by the readline
2121 library), programmatically (such as in test suites) or out-of-process
2132 library), programmatically (such as in test suites) or out-of-process
2122 (typically over the network by remote frontends).
2133 (typically over the network by remote frontends).
2123 """
2134 """
2124 from IPython.core.completer import IPCompleter
2135 from IPython.core.completer import IPCompleter
2125 from IPython.core.completerlib import (
2136 from IPython.core.completerlib import (
2126 cd_completer,
2137 cd_completer,
2127 magic_run_completer,
2138 magic_run_completer,
2128 module_completer,
2139 module_completer,
2129 reset_completer,
2140 reset_completer,
2130 )
2141 )
2131
2142
2132 self.Completer = IPCompleter(shell=self,
2143 self.Completer = IPCompleter(shell=self,
2133 namespace=self.user_ns,
2144 namespace=self.user_ns,
2134 global_namespace=self.user_global_ns,
2145 global_namespace=self.user_global_ns,
2135 parent=self,
2146 parent=self,
2136 )
2147 )
2137 self.configurables.append(self.Completer)
2148 self.configurables.append(self.Completer)
2138
2149
2139 # Add custom completers to the basic ones built into IPCompleter
2150 # Add custom completers to the basic ones built into IPCompleter
2140 sdisp = self.strdispatchers.get('complete_command', StrDispatch())
2151 sdisp = self.strdispatchers.get('complete_command', StrDispatch())
2141 self.strdispatchers['complete_command'] = sdisp
2152 self.strdispatchers['complete_command'] = sdisp
2142 self.Completer.custom_completers = sdisp
2153 self.Completer.custom_completers = sdisp
2143
2154
2144 self.set_hook('complete_command', module_completer, str_key = 'import')
2155 self.set_hook('complete_command', module_completer, str_key = 'import')
2145 self.set_hook('complete_command', module_completer, str_key = 'from')
2156 self.set_hook('complete_command', module_completer, str_key = 'from')
2146 self.set_hook('complete_command', module_completer, str_key = '%aimport')
2157 self.set_hook('complete_command', module_completer, str_key = '%aimport')
2147 self.set_hook('complete_command', magic_run_completer, str_key = '%run')
2158 self.set_hook('complete_command', magic_run_completer, str_key = '%run')
2148 self.set_hook('complete_command', cd_completer, str_key = '%cd')
2159 self.set_hook('complete_command', cd_completer, str_key = '%cd')
2149 self.set_hook('complete_command', reset_completer, str_key = '%reset')
2160 self.set_hook('complete_command', reset_completer, str_key = '%reset')
2150
2161
2151 @skip_doctest
2162 @skip_doctest
2152 def complete(self, text, line=None, cursor_pos=None):
2163 def complete(self, text, line=None, cursor_pos=None):
2153 """Return the completed text and a list of completions.
2164 """Return the completed text and a list of completions.
2154
2165
2155 Parameters
2166 Parameters
2156 ----------
2167 ----------
2157 text : string
2168 text : string
2158 A string of text to be completed on. It can be given as empty and
2169 A string of text to be completed on. It can be given as empty and
2159 instead a line/position pair are given. In this case, the
2170 instead a line/position pair are given. In this case, the
2160 completer itself will split the line like readline does.
2171 completer itself will split the line like readline does.
2161 line : string, optional
2172 line : string, optional
2162 The complete line that text is part of.
2173 The complete line that text is part of.
2163 cursor_pos : int, optional
2174 cursor_pos : int, optional
2164 The position of the cursor on the input line.
2175 The position of the cursor on the input line.
2165
2176
2166 Returns
2177 Returns
2167 -------
2178 -------
2168 text : string
2179 text : string
2169 The actual text that was completed.
2180 The actual text that was completed.
2170 matches : list
2181 matches : list
2171 A sorted list with all possible completions.
2182 A sorted list with all possible completions.
2172
2183
2173 Notes
2184 Notes
2174 -----
2185 -----
2175 The optional arguments allow the completion to take more context into
2186 The optional arguments allow the completion to take more context into
2176 account, and are part of the low-level completion API.
2187 account, and are part of the low-level completion API.
2177
2188
2178 This is a wrapper around the completion mechanism, similar to what
2189 This is a wrapper around the completion mechanism, similar to what
2179 readline does at the command line when the TAB key is hit. By
2190 readline does at the command line when the TAB key is hit. By
2180 exposing it as a method, it can be used by other non-readline
2191 exposing it as a method, it can be used by other non-readline
2181 environments (such as GUIs) for text completion.
2192 environments (such as GUIs) for text completion.
2182
2193
2183 Examples
2194 Examples
2184 --------
2195 --------
2185 In [1]: x = 'hello'
2196 In [1]: x = 'hello'
2186
2197
2187 In [2]: _ip.complete('x.l')
2198 In [2]: _ip.complete('x.l')
2188 Out[2]: ('x.l', ['x.ljust', 'x.lower', 'x.lstrip'])
2199 Out[2]: ('x.l', ['x.ljust', 'x.lower', 'x.lstrip'])
2189 """
2200 """
2190
2201
2191 # Inject names into __builtin__ so we can complete on the added names.
2202 # Inject names into __builtin__ so we can complete on the added names.
2192 with self.builtin_trap:
2203 with self.builtin_trap:
2193 return self.Completer.complete(text, line, cursor_pos)
2204 return self.Completer.complete(text, line, cursor_pos)
2194
2205
2195 def set_custom_completer(self, completer, pos=0) -> None:
2206 def set_custom_completer(self, completer, pos=0) -> None:
2196 """Adds a new custom completer function.
2207 """Adds a new custom completer function.
2197
2208
2198 The position argument (defaults to 0) is the index in the completers
2209 The position argument (defaults to 0) is the index in the completers
2199 list where you want the completer to be inserted.
2210 list where you want the completer to be inserted.
2200
2211
2201 `completer` should have the following signature::
2212 `completer` should have the following signature::
2202
2213
2203 def completion(self: Completer, text: string) -> List[str]:
2214 def completion(self: Completer, text: string) -> List[str]:
2204 raise NotImplementedError
2215 raise NotImplementedError
2205
2216
2206 It will be bound to the current Completer instance and pass some text
2217 It will be bound to the current Completer instance and pass some text
2207 and return a list with current completions to suggest to the user.
2218 and return a list with current completions to suggest to the user.
2208 """
2219 """
2209
2220
2210 newcomp = types.MethodType(completer, self.Completer)
2221 newcomp = types.MethodType(completer, self.Completer)
2211 self.Completer.custom_matchers.insert(pos,newcomp)
2222 self.Completer.custom_matchers.insert(pos,newcomp)
2212
2223
2213 def set_completer_frame(self, frame=None):
2224 def set_completer_frame(self, frame=None):
2214 """Set the frame of the completer."""
2225 """Set the frame of the completer."""
2215 if frame:
2226 if frame:
2216 self.Completer.namespace = frame.f_locals
2227 self.Completer.namespace = frame.f_locals
2217 self.Completer.global_namespace = frame.f_globals
2228 self.Completer.global_namespace = frame.f_globals
2218 else:
2229 else:
2219 self.Completer.namespace = self.user_ns
2230 self.Completer.namespace = self.user_ns
2220 self.Completer.global_namespace = self.user_global_ns
2231 self.Completer.global_namespace = self.user_global_ns
2221
2232
2222 #-------------------------------------------------------------------------
2233 #-------------------------------------------------------------------------
2223 # Things related to magics
2234 # Things related to magics
2224 #-------------------------------------------------------------------------
2235 #-------------------------------------------------------------------------
2225
2236
2226 def init_magics(self):
2237 def init_magics(self):
2227 from IPython.core import magics as m
2238 from IPython.core import magics as m
2228 self.magics_manager = magic.MagicsManager(shell=self,
2239 self.magics_manager = magic.MagicsManager(shell=self,
2229 parent=self,
2240 parent=self,
2230 user_magics=m.UserMagics(self))
2241 user_magics=m.UserMagics(self))
2231 self.configurables.append(self.magics_manager)
2242 self.configurables.append(self.magics_manager)
2232
2243
2233 # Expose as public API from the magics manager
2244 # Expose as public API from the magics manager
2234 self.register_magics = self.magics_manager.register
2245 self.register_magics = self.magics_manager.register
2235
2246
2236 self.register_magics(m.AutoMagics, m.BasicMagics, m.CodeMagics,
2247 self.register_magics(m.AutoMagics, m.BasicMagics, m.CodeMagics,
2237 m.ConfigMagics, m.DisplayMagics, m.ExecutionMagics,
2248 m.ConfigMagics, m.DisplayMagics, m.ExecutionMagics,
2238 m.ExtensionMagics, m.HistoryMagics, m.LoggingMagics,
2249 m.ExtensionMagics, m.HistoryMagics, m.LoggingMagics,
2239 m.NamespaceMagics, m.OSMagics, m.PackagingMagics,
2250 m.NamespaceMagics, m.OSMagics, m.PackagingMagics,
2240 m.PylabMagics, m.ScriptMagics,
2251 m.PylabMagics, m.ScriptMagics,
2241 )
2252 )
2242 self.register_magics(m.AsyncMagics)
2253 self.register_magics(m.AsyncMagics)
2243
2254
2244 # Register Magic Aliases
2255 # Register Magic Aliases
2245 mman = self.magics_manager
2256 mman = self.magics_manager
2246 # FIXME: magic aliases should be defined by the Magics classes
2257 # FIXME: magic aliases should be defined by the Magics classes
2247 # or in MagicsManager, not here
2258 # or in MagicsManager, not here
2248 mman.register_alias('ed', 'edit')
2259 mman.register_alias('ed', 'edit')
2249 mman.register_alias('hist', 'history')
2260 mman.register_alias('hist', 'history')
2250 mman.register_alias('rep', 'recall')
2261 mman.register_alias('rep', 'recall')
2251 mman.register_alias('SVG', 'svg', 'cell')
2262 mman.register_alias('SVG', 'svg', 'cell')
2252 mman.register_alias('HTML', 'html', 'cell')
2263 mman.register_alias('HTML', 'html', 'cell')
2253 mman.register_alias('file', 'writefile', 'cell')
2264 mman.register_alias('file', 'writefile', 'cell')
2254
2265
2255 # FIXME: Move the color initialization to the DisplayHook, which
2266 # FIXME: Move the color initialization to the DisplayHook, which
2256 # should be split into a prompt manager and displayhook. We probably
2267 # should be split into a prompt manager and displayhook. We probably
2257 # even need a centralize colors management object.
2268 # even need a centralize colors management object.
2258 self.run_line_magic('colors', self.colors)
2269 self.run_line_magic('colors', self.colors)
2259
2270
2260 # Defined here so that it's included in the documentation
2271 # Defined here so that it's included in the documentation
2261 @functools.wraps(magic.MagicsManager.register_function)
2272 @functools.wraps(magic.MagicsManager.register_function)
2262 def register_magic_function(self, func, magic_kind='line', magic_name=None):
2273 def register_magic_function(self, func, magic_kind='line', magic_name=None):
2263 self.magics_manager.register_function(
2274 self.magics_manager.register_function(
2264 func, magic_kind=magic_kind, magic_name=magic_name
2275 func, magic_kind=magic_kind, magic_name=magic_name
2265 )
2276 )
2266
2277
2267 def _find_with_lazy_load(self, /, type_, magic_name: str):
2278 def _find_with_lazy_load(self, /, type_, magic_name: str):
2268 """
2279 """
2269 Try to find a magic potentially lazy-loading it.
2280 Try to find a magic potentially lazy-loading it.
2270
2281
2271 Parameters
2282 Parameters
2272 ----------
2283 ----------
2273
2284
2274 type_: "line"|"cell"
2285 type_: "line"|"cell"
2275 the type of magics we are trying to find/lazy load.
2286 the type of magics we are trying to find/lazy load.
2276 magic_name: str
2287 magic_name: str
2277 The name of the magic we are trying to find/lazy load
2288 The name of the magic we are trying to find/lazy load
2278
2289
2279
2290
2280 Note that this may have any side effects
2291 Note that this may have any side effects
2281 """
2292 """
2282 finder = {"line": self.find_line_magic, "cell": self.find_cell_magic}[type_]
2293 finder = {"line": self.find_line_magic, "cell": self.find_cell_magic}[type_]
2283 fn = finder(magic_name)
2294 fn = finder(magic_name)
2284 if fn is not None:
2295 if fn is not None:
2285 return fn
2296 return fn
2286 lazy = self.magics_manager.lazy_magics.get(magic_name)
2297 lazy = self.magics_manager.lazy_magics.get(magic_name)
2287 if lazy is None:
2298 if lazy is None:
2288 return None
2299 return None
2289
2300
2290 self.run_line_magic("load_ext", lazy)
2301 self.run_line_magic("load_ext", lazy)
2291 res = finder(magic_name)
2302 res = finder(magic_name)
2292 return res
2303 return res
2293
2304
2294 def run_line_magic(self, magic_name: str, line, _stack_depth=1):
2305 def run_line_magic(self, magic_name: str, line, _stack_depth=1):
2295 """Execute the given line magic.
2306 """Execute the given line magic.
2296
2307
2297 Parameters
2308 Parameters
2298 ----------
2309 ----------
2299 magic_name : str
2310 magic_name : str
2300 Name of the desired magic function, without '%' prefix.
2311 Name of the desired magic function, without '%' prefix.
2301 line : str
2312 line : str
2302 The rest of the input line as a single string.
2313 The rest of the input line as a single string.
2303 _stack_depth : int
2314 _stack_depth : int
2304 If run_line_magic() is called from magic() then _stack_depth=2.
2315 If run_line_magic() is called from magic() then _stack_depth=2.
2305 This is added to ensure backward compatibility for use of 'get_ipython().magic()'
2316 This is added to ensure backward compatibility for use of 'get_ipython().magic()'
2306 """
2317 """
2307 fn = self._find_with_lazy_load("line", magic_name)
2318 fn = self._find_with_lazy_load("line", magic_name)
2308 if fn is None:
2319 if fn is None:
2309 lazy = self.magics_manager.lazy_magics.get(magic_name)
2320 lazy = self.magics_manager.lazy_magics.get(magic_name)
2310 if lazy:
2321 if lazy:
2311 self.run_line_magic("load_ext", lazy)
2322 self.run_line_magic("load_ext", lazy)
2312 fn = self.find_line_magic(magic_name)
2323 fn = self.find_line_magic(magic_name)
2313 if fn is None:
2324 if fn is None:
2314 cm = self.find_cell_magic(magic_name)
2325 cm = self.find_cell_magic(magic_name)
2315 etpl = "Line magic function `%%%s` not found%s."
2326 etpl = "Line magic function `%%%s` not found%s."
2316 extra = '' if cm is None else (' (But cell magic `%%%%%s` exists, '
2327 extra = '' if cm is None else (' (But cell magic `%%%%%s` exists, '
2317 'did you mean that instead?)' % magic_name )
2328 'did you mean that instead?)' % magic_name )
2318 raise UsageError(etpl % (magic_name, extra))
2329 raise UsageError(etpl % (magic_name, extra))
2319 else:
2330 else:
2320 # Note: this is the distance in the stack to the user's frame.
2331 # Note: this is the distance in the stack to the user's frame.
2321 # This will need to be updated if the internal calling logic gets
2332 # This will need to be updated if the internal calling logic gets
2322 # refactored, or else we'll be expanding the wrong variables.
2333 # refactored, or else we'll be expanding the wrong variables.
2323
2334
2324 # Determine stack_depth depending on where run_line_magic() has been called
2335 # Determine stack_depth depending on where run_line_magic() has been called
2325 stack_depth = _stack_depth
2336 stack_depth = _stack_depth
2326 if getattr(fn, magic.MAGIC_NO_VAR_EXPAND_ATTR, False):
2337 if getattr(fn, magic.MAGIC_NO_VAR_EXPAND_ATTR, False):
2327 # magic has opted out of var_expand
2338 # magic has opted out of var_expand
2328 magic_arg_s = line
2339 magic_arg_s = line
2329 else:
2340 else:
2330 magic_arg_s = self.var_expand(line, stack_depth)
2341 magic_arg_s = self.var_expand(line, stack_depth)
2331 # Put magic args in a list so we can call with f(*a) syntax
2342 # Put magic args in a list so we can call with f(*a) syntax
2332 args = [magic_arg_s]
2343 args = [magic_arg_s]
2333 kwargs = {}
2344 kwargs = {}
2334 # Grab local namespace if we need it:
2345 # Grab local namespace if we need it:
2335 if getattr(fn, "needs_local_scope", False):
2346 if getattr(fn, "needs_local_scope", False):
2336 kwargs['local_ns'] = self.get_local_scope(stack_depth)
2347 kwargs['local_ns'] = self.get_local_scope(stack_depth)
2337 with self.builtin_trap:
2348 with self.builtin_trap:
2338 result = fn(*args, **kwargs)
2349 result = fn(*args, **kwargs)
2339 return result
2350 return result
2340
2351
2341 def get_local_scope(self, stack_depth):
2352 def get_local_scope(self, stack_depth):
2342 """Get local scope at given stack depth.
2353 """Get local scope at given stack depth.
2343
2354
2344 Parameters
2355 Parameters
2345 ----------
2356 ----------
2346 stack_depth : int
2357 stack_depth : int
2347 Depth relative to calling frame
2358 Depth relative to calling frame
2348 """
2359 """
2349 return sys._getframe(stack_depth + 1).f_locals
2360 return sys._getframe(stack_depth + 1).f_locals
2350
2361
2351 def run_cell_magic(self, magic_name, line, cell):
2362 def run_cell_magic(self, magic_name, line, cell):
2352 """Execute the given cell magic.
2363 """Execute the given cell magic.
2353
2364
2354 Parameters
2365 Parameters
2355 ----------
2366 ----------
2356 magic_name : str
2367 magic_name : str
2357 Name of the desired magic function, without '%' prefix.
2368 Name of the desired magic function, without '%' prefix.
2358 line : str
2369 line : str
2359 The rest of the first input line as a single string.
2370 The rest of the first input line as a single string.
2360 cell : str
2371 cell : str
2361 The body of the cell as a (possibly multiline) string.
2372 The body of the cell as a (possibly multiline) string.
2362 """
2373 """
2363 fn = self._find_with_lazy_load("cell", magic_name)
2374 fn = self._find_with_lazy_load("cell", magic_name)
2364 if fn is None:
2375 if fn is None:
2365 lm = self.find_line_magic(magic_name)
2376 lm = self.find_line_magic(magic_name)
2366 etpl = "Cell magic `%%{0}` not found{1}."
2377 etpl = "Cell magic `%%{0}` not found{1}."
2367 extra = '' if lm is None else (' (But line magic `%{0}` exists, '
2378 extra = '' if lm is None else (' (But line magic `%{0}` exists, '
2368 'did you mean that instead?)'.format(magic_name))
2379 'did you mean that instead?)'.format(magic_name))
2369 raise UsageError(etpl.format(magic_name, extra))
2380 raise UsageError(etpl.format(magic_name, extra))
2370 elif cell == '':
2381 elif cell == '':
2371 message = '%%{0} is a cell magic, but the cell body is empty.'.format(magic_name)
2382 message = '%%{0} is a cell magic, but the cell body is empty.'.format(magic_name)
2372 if self.find_line_magic(magic_name) is not None:
2383 if self.find_line_magic(magic_name) is not None:
2373 message += ' Did you mean the line magic %{0} (single %)?'.format(magic_name)
2384 message += ' Did you mean the line magic %{0} (single %)?'.format(magic_name)
2374 raise UsageError(message)
2385 raise UsageError(message)
2375 else:
2386 else:
2376 # Note: this is the distance in the stack to the user's frame.
2387 # Note: this is the distance in the stack to the user's frame.
2377 # This will need to be updated if the internal calling logic gets
2388 # This will need to be updated if the internal calling logic gets
2378 # refactored, or else we'll be expanding the wrong variables.
2389 # refactored, or else we'll be expanding the wrong variables.
2379 stack_depth = 2
2390 stack_depth = 2
2380 if getattr(fn, magic.MAGIC_NO_VAR_EXPAND_ATTR, False):
2391 if getattr(fn, magic.MAGIC_NO_VAR_EXPAND_ATTR, False):
2381 # magic has opted out of var_expand
2392 # magic has opted out of var_expand
2382 magic_arg_s = line
2393 magic_arg_s = line
2383 else:
2394 else:
2384 magic_arg_s = self.var_expand(line, stack_depth)
2395 magic_arg_s = self.var_expand(line, stack_depth)
2385 kwargs = {}
2396 kwargs = {}
2386 if getattr(fn, "needs_local_scope", False):
2397 if getattr(fn, "needs_local_scope", False):
2387 kwargs['local_ns'] = self.user_ns
2398 kwargs['local_ns'] = self.user_ns
2388
2399
2389 with self.builtin_trap:
2400 with self.builtin_trap:
2390 args = (magic_arg_s, cell)
2401 args = (magic_arg_s, cell)
2391 result = fn(*args, **kwargs)
2402 result = fn(*args, **kwargs)
2392 return result
2403 return result
2393
2404
2394 def find_line_magic(self, magic_name):
2405 def find_line_magic(self, magic_name):
2395 """Find and return a line magic by name.
2406 """Find and return a line magic by name.
2396
2407
2397 Returns None if the magic isn't found."""
2408 Returns None if the magic isn't found."""
2398 return self.magics_manager.magics['line'].get(magic_name)
2409 return self.magics_manager.magics['line'].get(magic_name)
2399
2410
2400 def find_cell_magic(self, magic_name):
2411 def find_cell_magic(self, magic_name):
2401 """Find and return a cell magic by name.
2412 """Find and return a cell magic by name.
2402
2413
2403 Returns None if the magic isn't found."""
2414 Returns None if the magic isn't found."""
2404 return self.magics_manager.magics['cell'].get(magic_name)
2415 return self.magics_manager.magics['cell'].get(magic_name)
2405
2416
2406 def find_magic(self, magic_name, magic_kind='line'):
2417 def find_magic(self, magic_name, magic_kind='line'):
2407 """Find and return a magic of the given type by name.
2418 """Find and return a magic of the given type by name.
2408
2419
2409 Returns None if the magic isn't found."""
2420 Returns None if the magic isn't found."""
2410 return self.magics_manager.magics[magic_kind].get(magic_name)
2421 return self.magics_manager.magics[magic_kind].get(magic_name)
2411
2422
2412 def magic(self, arg_s):
2423 def magic(self, arg_s):
2413 """
2424 """
2414 DEPRECATED
2425 DEPRECATED
2415
2426
2416 Deprecated since IPython 0.13 (warning added in
2427 Deprecated since IPython 0.13 (warning added in
2417 8.1), use run_line_magic(magic_name, parameter_s).
2428 8.1), use run_line_magic(magic_name, parameter_s).
2418
2429
2419 Call a magic function by name.
2430 Call a magic function by name.
2420
2431
2421 Input: a string containing the name of the magic function to call and
2432 Input: a string containing the name of the magic function to call and
2422 any additional arguments to be passed to the magic.
2433 any additional arguments to be passed to the magic.
2423
2434
2424 magic('name -opt foo bar') is equivalent to typing at the ipython
2435 magic('name -opt foo bar') is equivalent to typing at the ipython
2425 prompt:
2436 prompt:
2426
2437
2427 In[1]: %name -opt foo bar
2438 In[1]: %name -opt foo bar
2428
2439
2429 To call a magic without arguments, simply use magic('name').
2440 To call a magic without arguments, simply use magic('name').
2430
2441
2431 This provides a proper Python function to call IPython's magics in any
2442 This provides a proper Python function to call IPython's magics in any
2432 valid Python code you can type at the interpreter, including loops and
2443 valid Python code you can type at the interpreter, including loops and
2433 compound statements.
2444 compound statements.
2434 """
2445 """
2435 warnings.warn(
2446 warnings.warn(
2436 "`magic(...)` is deprecated since IPython 0.13 (warning added in "
2447 "`magic(...)` is deprecated since IPython 0.13 (warning added in "
2437 "8.1), use run_line_magic(magic_name, parameter_s).",
2448 "8.1), use run_line_magic(magic_name, parameter_s).",
2438 DeprecationWarning,
2449 DeprecationWarning,
2439 stacklevel=2,
2450 stacklevel=2,
2440 )
2451 )
2441 # TODO: should we issue a loud deprecation warning here?
2452 # TODO: should we issue a loud deprecation warning here?
2442 magic_name, _, magic_arg_s = arg_s.partition(' ')
2453 magic_name, _, magic_arg_s = arg_s.partition(' ')
2443 magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
2454 magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
2444 return self.run_line_magic(magic_name, magic_arg_s, _stack_depth=2)
2455 return self.run_line_magic(magic_name, magic_arg_s, _stack_depth=2)
2445
2456
2446 #-------------------------------------------------------------------------
2457 #-------------------------------------------------------------------------
2447 # Things related to macros
2458 # Things related to macros
2448 #-------------------------------------------------------------------------
2459 #-------------------------------------------------------------------------
2449
2460
2450 def define_macro(self, name, themacro):
2461 def define_macro(self, name, themacro):
2451 """Define a new macro
2462 """Define a new macro
2452
2463
2453 Parameters
2464 Parameters
2454 ----------
2465 ----------
2455 name : str
2466 name : str
2456 The name of the macro.
2467 The name of the macro.
2457 themacro : str or Macro
2468 themacro : str or Macro
2458 The action to do upon invoking the macro. If a string, a new
2469 The action to do upon invoking the macro. If a string, a new
2459 Macro object is created by passing the string to it.
2470 Macro object is created by passing the string to it.
2460 """
2471 """
2461
2472
2462 from IPython.core import macro
2473 from IPython.core import macro
2463
2474
2464 if isinstance(themacro, str):
2475 if isinstance(themacro, str):
2465 themacro = macro.Macro(themacro)
2476 themacro = macro.Macro(themacro)
2466 if not isinstance(themacro, macro.Macro):
2477 if not isinstance(themacro, macro.Macro):
2467 raise ValueError('A macro must be a string or a Macro instance.')
2478 raise ValueError('A macro must be a string or a Macro instance.')
2468 self.user_ns[name] = themacro
2479 self.user_ns[name] = themacro
2469
2480
2470 #-------------------------------------------------------------------------
2481 #-------------------------------------------------------------------------
2471 # Things related to the running of system commands
2482 # Things related to the running of system commands
2472 #-------------------------------------------------------------------------
2483 #-------------------------------------------------------------------------
2473
2484
2474 def system_piped(self, cmd):
2485 def system_piped(self, cmd):
2475 """Call the given cmd in a subprocess, piping stdout/err
2486 """Call the given cmd in a subprocess, piping stdout/err
2476
2487
2477 Parameters
2488 Parameters
2478 ----------
2489 ----------
2479 cmd : str
2490 cmd : str
2480 Command to execute (can not end in '&', as background processes are
2491 Command to execute (can not end in '&', as background processes are
2481 not supported. Should not be a command that expects input
2492 not supported. Should not be a command that expects input
2482 other than simple text.
2493 other than simple text.
2483 """
2494 """
2484 if cmd.rstrip().endswith('&'):
2495 if cmd.rstrip().endswith('&'):
2485 # this is *far* from a rigorous test
2496 # this is *far* from a rigorous test
2486 # We do not support backgrounding processes because we either use
2497 # We do not support backgrounding processes because we either use
2487 # pexpect or pipes to read from. Users can always just call
2498 # pexpect or pipes to read from. Users can always just call
2488 # os.system() or use ip.system=ip.system_raw
2499 # os.system() or use ip.system=ip.system_raw
2489 # if they really want a background process.
2500 # if they really want a background process.
2490 raise OSError("Background processes not supported.")
2501 raise OSError("Background processes not supported.")
2491
2502
2492 # we explicitly do NOT return the subprocess status code, because
2503 # we explicitly do NOT return the subprocess status code, because
2493 # a non-None value would trigger :func:`sys.displayhook` calls.
2504 # a non-None value would trigger :func:`sys.displayhook` calls.
2494 # Instead, we store the exit_code in user_ns.
2505 # Instead, we store the exit_code in user_ns.
2495 self.user_ns['_exit_code'] = system(self.var_expand(cmd, depth=1))
2506 self.user_ns['_exit_code'] = system(self.var_expand(cmd, depth=1))
2496
2507
2497 def system_raw(self, cmd):
2508 def system_raw(self, cmd):
2498 """Call the given cmd in a subprocess using os.system on Windows or
2509 """Call the given cmd in a subprocess using os.system on Windows or
2499 subprocess.call using the system shell on other platforms.
2510 subprocess.call using the system shell on other platforms.
2500
2511
2501 Parameters
2512 Parameters
2502 ----------
2513 ----------
2503 cmd : str
2514 cmd : str
2504 Command to execute.
2515 Command to execute.
2505 """
2516 """
2506 cmd = self.var_expand(cmd, depth=1)
2517 cmd = self.var_expand(cmd, depth=1)
2507 # warn if there is an IPython magic alternative.
2518 # warn if there is an IPython magic alternative.
2508 main_cmd = cmd.split()[0]
2519 main_cmd = cmd.split()[0]
2509 has_magic_alternatives = ("pip", "conda", "cd")
2520 has_magic_alternatives = ("pip", "conda", "cd")
2510
2521
2511 if main_cmd in has_magic_alternatives:
2522 if main_cmd in has_magic_alternatives:
2512 warnings.warn(
2523 warnings.warn(
2513 (
2524 (
2514 "You executed the system command !{0} which may not work "
2525 "You executed the system command !{0} which may not work "
2515 "as expected. Try the IPython magic %{0} instead."
2526 "as expected. Try the IPython magic %{0} instead."
2516 ).format(main_cmd)
2527 ).format(main_cmd)
2517 )
2528 )
2518
2529
2519 # protect os.system from UNC paths on Windows, which it can't handle:
2530 # protect os.system from UNC paths on Windows, which it can't handle:
2520 if sys.platform == 'win32':
2531 if sys.platform == 'win32':
2521 from IPython.utils._process_win32 import AvoidUNCPath
2532 from IPython.utils._process_win32 import AvoidUNCPath
2522 with AvoidUNCPath() as path:
2533 with AvoidUNCPath() as path:
2523 if path is not None:
2534 if path is not None:
2524 cmd = '"pushd %s &&"%s' % (path, cmd)
2535 cmd = '"pushd %s &&"%s' % (path, cmd)
2525 try:
2536 try:
2526 ec = os.system(cmd)
2537 ec = os.system(cmd)
2527 except KeyboardInterrupt:
2538 except KeyboardInterrupt:
2528 print('\n' + self.get_exception_only(), file=sys.stderr)
2539 print('\n' + self.get_exception_only(), file=sys.stderr)
2529 ec = -2
2540 ec = -2
2530 else:
2541 else:
2531 # For posix the result of the subprocess.call() below is an exit
2542 # For posix the result of the subprocess.call() below is an exit
2532 # code, which by convention is zero for success, positive for
2543 # code, which by convention is zero for success, positive for
2533 # program failure. Exit codes above 128 are reserved for signals,
2544 # program failure. Exit codes above 128 are reserved for signals,
2534 # and the formula for converting a signal to an exit code is usually
2545 # and the formula for converting a signal to an exit code is usually
2535 # signal_number+128. To more easily differentiate between exit
2546 # signal_number+128. To more easily differentiate between exit
2536 # codes and signals, ipython uses negative numbers. For instance
2547 # codes and signals, ipython uses negative numbers. For instance
2537 # since control-c is signal 2 but exit code 130, ipython's
2548 # since control-c is signal 2 but exit code 130, ipython's
2538 # _exit_code variable will read -2. Note that some shells like
2549 # _exit_code variable will read -2. Note that some shells like
2539 # csh and fish don't follow sh/bash conventions for exit codes.
2550 # csh and fish don't follow sh/bash conventions for exit codes.
2540 executable = os.environ.get('SHELL', None)
2551 executable = os.environ.get('SHELL', None)
2541 try:
2552 try:
2542 # Use env shell instead of default /bin/sh
2553 # Use env shell instead of default /bin/sh
2543 ec = subprocess.call(cmd, shell=True, executable=executable)
2554 ec = subprocess.call(cmd, shell=True, executable=executable)
2544 except KeyboardInterrupt:
2555 except KeyboardInterrupt:
2545 # intercept control-C; a long traceback is not useful here
2556 # intercept control-C; a long traceback is not useful here
2546 print('\n' + self.get_exception_only(), file=sys.stderr)
2557 print('\n' + self.get_exception_only(), file=sys.stderr)
2547 ec = 130
2558 ec = 130
2548 if ec > 128:
2559 if ec > 128:
2549 ec = -(ec - 128)
2560 ec = -(ec - 128)
2550
2561
2551 # We explicitly do NOT return the subprocess status code, because
2562 # We explicitly do NOT return the subprocess status code, because
2552 # a non-None value would trigger :func:`sys.displayhook` calls.
2563 # a non-None value would trigger :func:`sys.displayhook` calls.
2553 # Instead, we store the exit_code in user_ns. Note the semantics
2564 # Instead, we store the exit_code in user_ns. Note the semantics
2554 # of _exit_code: for control-c, _exit_code == -signal.SIGNIT,
2565 # of _exit_code: for control-c, _exit_code == -signal.SIGNIT,
2555 # but raising SystemExit(_exit_code) will give status 254!
2566 # but raising SystemExit(_exit_code) will give status 254!
2556 self.user_ns['_exit_code'] = ec
2567 self.user_ns['_exit_code'] = ec
2557
2568
2558 # use piped system by default, because it is better behaved
2569 # use piped system by default, because it is better behaved
2559 system = system_piped
2570 system = system_piped
2560
2571
2561 def getoutput(self, cmd, split=True, depth=0):
2572 def getoutput(self, cmd, split=True, depth=0):
2562 """Get output (possibly including stderr) from a subprocess.
2573 """Get output (possibly including stderr) from a subprocess.
2563
2574
2564 Parameters
2575 Parameters
2565 ----------
2576 ----------
2566 cmd : str
2577 cmd : str
2567 Command to execute (can not end in '&', as background processes are
2578 Command to execute (can not end in '&', as background processes are
2568 not supported.
2579 not supported.
2569 split : bool, optional
2580 split : bool, optional
2570 If True, split the output into an IPython SList. Otherwise, an
2581 If True, split the output into an IPython SList. Otherwise, an
2571 IPython LSString is returned. These are objects similar to normal
2582 IPython LSString is returned. These are objects similar to normal
2572 lists and strings, with a few convenience attributes for easier
2583 lists and strings, with a few convenience attributes for easier
2573 manipulation of line-based output. You can use '?' on them for
2584 manipulation of line-based output. You can use '?' on them for
2574 details.
2585 details.
2575 depth : int, optional
2586 depth : int, optional
2576 How many frames above the caller are the local variables which should
2587 How many frames above the caller are the local variables which should
2577 be expanded in the command string? The default (0) assumes that the
2588 be expanded in the command string? The default (0) assumes that the
2578 expansion variables are in the stack frame calling this function.
2589 expansion variables are in the stack frame calling this function.
2579 """
2590 """
2580 if cmd.rstrip().endswith('&'):
2591 if cmd.rstrip().endswith('&'):
2581 # this is *far* from a rigorous test
2592 # this is *far* from a rigorous test
2582 raise OSError("Background processes not supported.")
2593 raise OSError("Background processes not supported.")
2583 out = getoutput(self.var_expand(cmd, depth=depth+1))
2594 out = getoutput(self.var_expand(cmd, depth=depth+1))
2584 if split:
2595 if split:
2585 out = SList(out.splitlines())
2596 out = SList(out.splitlines())
2586 else:
2597 else:
2587 out = LSString(out)
2598 out = LSString(out)
2588 return out
2599 return out
2589
2600
2590 #-------------------------------------------------------------------------
2601 #-------------------------------------------------------------------------
2591 # Things related to aliases
2602 # Things related to aliases
2592 #-------------------------------------------------------------------------
2603 #-------------------------------------------------------------------------
2593
2604
2594 def init_alias(self):
2605 def init_alias(self):
2595 self.alias_manager = AliasManager(shell=self, parent=self)
2606 self.alias_manager = AliasManager(shell=self, parent=self)
2596 self.configurables.append(self.alias_manager)
2607 self.configurables.append(self.alias_manager)
2597
2608
2598 #-------------------------------------------------------------------------
2609 #-------------------------------------------------------------------------
2599 # Things related to extensions
2610 # Things related to extensions
2600 #-------------------------------------------------------------------------
2611 #-------------------------------------------------------------------------
2601
2612
2602 def init_extension_manager(self):
2613 def init_extension_manager(self):
2603 self.extension_manager = ExtensionManager(shell=self, parent=self)
2614 self.extension_manager = ExtensionManager(shell=self, parent=self)
2604 self.configurables.append(self.extension_manager)
2615 self.configurables.append(self.extension_manager)
2605
2616
2606 #-------------------------------------------------------------------------
2617 #-------------------------------------------------------------------------
2607 # Things related to payloads
2618 # Things related to payloads
2608 #-------------------------------------------------------------------------
2619 #-------------------------------------------------------------------------
2609
2620
2610 def init_payload(self):
2621 def init_payload(self):
2611 self.payload_manager = PayloadManager(parent=self)
2622 self.payload_manager = PayloadManager(parent=self)
2612 self.configurables.append(self.payload_manager)
2623 self.configurables.append(self.payload_manager)
2613
2624
2614 #-------------------------------------------------------------------------
2625 #-------------------------------------------------------------------------
2615 # Things related to the prefilter
2626 # Things related to the prefilter
2616 #-------------------------------------------------------------------------
2627 #-------------------------------------------------------------------------
2617
2628
2618 def init_prefilter(self):
2629 def init_prefilter(self):
2619 self.prefilter_manager = PrefilterManager(shell=self, parent=self)
2630 self.prefilter_manager = PrefilterManager(shell=self, parent=self)
2620 self.configurables.append(self.prefilter_manager)
2631 self.configurables.append(self.prefilter_manager)
2621 # Ultimately this will be refactored in the new interpreter code, but
2632 # Ultimately this will be refactored in the new interpreter code, but
2622 # for now, we should expose the main prefilter method (there's legacy
2633 # for now, we should expose the main prefilter method (there's legacy
2623 # code out there that may rely on this).
2634 # code out there that may rely on this).
2624 self.prefilter = self.prefilter_manager.prefilter_lines
2635 self.prefilter = self.prefilter_manager.prefilter_lines
2625
2636
2626 def auto_rewrite_input(self, cmd):
2637 def auto_rewrite_input(self, cmd):
2627 """Print to the screen the rewritten form of the user's command.
2638 """Print to the screen the rewritten form of the user's command.
2628
2639
2629 This shows visual feedback by rewriting input lines that cause
2640 This shows visual feedback by rewriting input lines that cause
2630 automatic calling to kick in, like::
2641 automatic calling to kick in, like::
2631
2642
2632 /f x
2643 /f x
2633
2644
2634 into::
2645 into::
2635
2646
2636 ------> f(x)
2647 ------> f(x)
2637
2648
2638 after the user's input prompt. This helps the user understand that the
2649 after the user's input prompt. This helps the user understand that the
2639 input line was transformed automatically by IPython.
2650 input line was transformed automatically by IPython.
2640 """
2651 """
2641 if not self.show_rewritten_input:
2652 if not self.show_rewritten_input:
2642 return
2653 return
2643
2654
2644 # This is overridden in TerminalInteractiveShell to use fancy prompts
2655 # This is overridden in TerminalInteractiveShell to use fancy prompts
2645 print("------> " + cmd)
2656 print("------> " + cmd)
2646
2657
2647 #-------------------------------------------------------------------------
2658 #-------------------------------------------------------------------------
2648 # Things related to extracting values/expressions from kernel and user_ns
2659 # Things related to extracting values/expressions from kernel and user_ns
2649 #-------------------------------------------------------------------------
2660 #-------------------------------------------------------------------------
2650
2661
2651 def _user_obj_error(self):
2662 def _user_obj_error(self):
2652 """return simple exception dict
2663 """return simple exception dict
2653
2664
2654 for use in user_expressions
2665 for use in user_expressions
2655 """
2666 """
2656
2667
2657 etype, evalue, tb = self._get_exc_info()
2668 etype, evalue, tb = self._get_exc_info()
2658 stb = self.InteractiveTB.get_exception_only(etype, evalue)
2669 stb = self.InteractiveTB.get_exception_only(etype, evalue)
2659
2670
2660 exc_info = {
2671 exc_info = {
2661 "status": "error",
2672 "status": "error",
2662 "traceback": stb,
2673 "traceback": stb,
2663 "ename": etype.__name__,
2674 "ename": etype.__name__,
2664 "evalue": py3compat.safe_unicode(evalue),
2675 "evalue": py3compat.safe_unicode(evalue),
2665 }
2676 }
2666
2677
2667 return exc_info
2678 return exc_info
2668
2679
2669 def _format_user_obj(self, obj):
2680 def _format_user_obj(self, obj):
2670 """format a user object to display dict
2681 """format a user object to display dict
2671
2682
2672 for use in user_expressions
2683 for use in user_expressions
2673 """
2684 """
2674
2685
2675 data, md = self.display_formatter.format(obj)
2686 data, md = self.display_formatter.format(obj)
2676 value = {
2687 value = {
2677 'status' : 'ok',
2688 'status' : 'ok',
2678 'data' : data,
2689 'data' : data,
2679 'metadata' : md,
2690 'metadata' : md,
2680 }
2691 }
2681 return value
2692 return value
2682
2693
2683 def user_expressions(self, expressions):
2694 def user_expressions(self, expressions):
2684 """Evaluate a dict of expressions in the user's namespace.
2695 """Evaluate a dict of expressions in the user's namespace.
2685
2696
2686 Parameters
2697 Parameters
2687 ----------
2698 ----------
2688 expressions : dict
2699 expressions : dict
2689 A dict with string keys and string values. The expression values
2700 A dict with string keys and string values. The expression values
2690 should be valid Python expressions, each of which will be evaluated
2701 should be valid Python expressions, each of which will be evaluated
2691 in the user namespace.
2702 in the user namespace.
2692
2703
2693 Returns
2704 Returns
2694 -------
2705 -------
2695 A dict, keyed like the input expressions dict, with the rich mime-typed
2706 A dict, keyed like the input expressions dict, with the rich mime-typed
2696 display_data of each value.
2707 display_data of each value.
2697 """
2708 """
2698 out = {}
2709 out = {}
2699 user_ns = self.user_ns
2710 user_ns = self.user_ns
2700 global_ns = self.user_global_ns
2711 global_ns = self.user_global_ns
2701
2712
2702 for key, expr in expressions.items():
2713 for key, expr in expressions.items():
2703 try:
2714 try:
2704 value = self._format_user_obj(eval(expr, global_ns, user_ns))
2715 value = self._format_user_obj(eval(expr, global_ns, user_ns))
2705 except:
2716 except:
2706 value = self._user_obj_error()
2717 value = self._user_obj_error()
2707 out[key] = value
2718 out[key] = value
2708 return out
2719 return out
2709
2720
2710 #-------------------------------------------------------------------------
2721 #-------------------------------------------------------------------------
2711 # Things related to the running of code
2722 # Things related to the running of code
2712 #-------------------------------------------------------------------------
2723 #-------------------------------------------------------------------------
2713
2724
2714 def ex(self, cmd):
2725 def ex(self, cmd):
2715 """Execute a normal python statement in user namespace."""
2726 """Execute a normal python statement in user namespace."""
2716 with self.builtin_trap:
2727 with self.builtin_trap:
2717 exec(cmd, self.user_global_ns, self.user_ns)
2728 exec(cmd, self.user_global_ns, self.user_ns)
2718
2729
2719 def ev(self, expr):
2730 def ev(self, expr):
2720 """Evaluate python expression expr in user namespace.
2731 """Evaluate python expression expr in user namespace.
2721
2732
2722 Returns the result of evaluation
2733 Returns the result of evaluation
2723 """
2734 """
2724 with self.builtin_trap:
2735 with self.builtin_trap:
2725 return eval(expr, self.user_global_ns, self.user_ns)
2736 return eval(expr, self.user_global_ns, self.user_ns)
2726
2737
2727 def safe_execfile(self, fname, *where, exit_ignore=False, raise_exceptions=False, shell_futures=False):
2738 def safe_execfile(self, fname, *where, exit_ignore=False, raise_exceptions=False, shell_futures=False):
2728 """A safe version of the builtin execfile().
2739 """A safe version of the builtin execfile().
2729
2740
2730 This version will never throw an exception, but instead print
2741 This version will never throw an exception, but instead print
2731 helpful error messages to the screen. This only works on pure
2742 helpful error messages to the screen. This only works on pure
2732 Python files with the .py extension.
2743 Python files with the .py extension.
2733
2744
2734 Parameters
2745 Parameters
2735 ----------
2746 ----------
2736 fname : string
2747 fname : string
2737 The name of the file to be executed.
2748 The name of the file to be executed.
2738 *where : tuple
2749 *where : tuple
2739 One or two namespaces, passed to execfile() as (globals,locals).
2750 One or two namespaces, passed to execfile() as (globals,locals).
2740 If only one is given, it is passed as both.
2751 If only one is given, it is passed as both.
2741 exit_ignore : bool (False)
2752 exit_ignore : bool (False)
2742 If True, then silence SystemExit for non-zero status (it is always
2753 If True, then silence SystemExit for non-zero status (it is always
2743 silenced for zero status, as it is so common).
2754 silenced for zero status, as it is so common).
2744 raise_exceptions : bool (False)
2755 raise_exceptions : bool (False)
2745 If True raise exceptions everywhere. Meant for testing.
2756 If True raise exceptions everywhere. Meant for testing.
2746 shell_futures : bool (False)
2757 shell_futures : bool (False)
2747 If True, the code will share future statements with the interactive
2758 If True, the code will share future statements with the interactive
2748 shell. It will both be affected by previous __future__ imports, and
2759 shell. It will both be affected by previous __future__ imports, and
2749 any __future__ imports in the code will affect the shell. If False,
2760 any __future__ imports in the code will affect the shell. If False,
2750 __future__ imports are not shared in either direction.
2761 __future__ imports are not shared in either direction.
2751
2762
2752 """
2763 """
2753 fname = Path(fname).expanduser().resolve()
2764 fname = Path(fname).expanduser().resolve()
2754
2765
2755 # Make sure we can open the file
2766 # Make sure we can open the file
2756 try:
2767 try:
2757 with fname.open("rb"):
2768 with fname.open("rb"):
2758 pass
2769 pass
2759 except:
2770 except:
2760 warn('Could not open file <%s> for safe execution.' % fname)
2771 warn('Could not open file <%s> for safe execution.' % fname)
2761 return
2772 return
2762
2773
2763 # Find things also in current directory. This is needed to mimic the
2774 # Find things also in current directory. This is needed to mimic the
2764 # behavior of running a script from the system command line, where
2775 # behavior of running a script from the system command line, where
2765 # Python inserts the script's directory into sys.path
2776 # Python inserts the script's directory into sys.path
2766 dname = str(fname.parent)
2777 dname = str(fname.parent)
2767
2778
2768 with prepended_to_syspath(dname), self.builtin_trap:
2779 with prepended_to_syspath(dname), self.builtin_trap:
2769 try:
2780 try:
2770 glob, loc = (where + (None, ))[:2]
2781 glob, loc = (where + (None, ))[:2]
2771 py3compat.execfile(
2782 py3compat.execfile(
2772 fname, glob, loc,
2783 fname, glob, loc,
2773 self.compile if shell_futures else None)
2784 self.compile if shell_futures else None)
2774 except SystemExit as status:
2785 except SystemExit as status:
2775 # If the call was made with 0 or None exit status (sys.exit(0)
2786 # If the call was made with 0 or None exit status (sys.exit(0)
2776 # or sys.exit() ), don't bother showing a traceback, as both of
2787 # or sys.exit() ), don't bother showing a traceback, as both of
2777 # these are considered normal by the OS:
2788 # these are considered normal by the OS:
2778 # > python -c'import sys;sys.exit(0)'; echo $?
2789 # > python -c'import sys;sys.exit(0)'; echo $?
2779 # 0
2790 # 0
2780 # > python -c'import sys;sys.exit()'; echo $?
2791 # > python -c'import sys;sys.exit()'; echo $?
2781 # 0
2792 # 0
2782 # For other exit status, we show the exception unless
2793 # For other exit status, we show the exception unless
2783 # explicitly silenced, but only in short form.
2794 # explicitly silenced, but only in short form.
2784 if status.code:
2795 if status.code:
2785 if raise_exceptions:
2796 if raise_exceptions:
2786 raise
2797 raise
2787 if not exit_ignore:
2798 if not exit_ignore:
2788 self.showtraceback(exception_only=True)
2799 self.showtraceback(exception_only=True)
2789 except:
2800 except:
2790 if raise_exceptions:
2801 if raise_exceptions:
2791 raise
2802 raise
2792 # tb offset is 2 because we wrap execfile
2803 # tb offset is 2 because we wrap execfile
2793 self.showtraceback(tb_offset=2)
2804 self.showtraceback(tb_offset=2)
2794
2805
2795 def safe_execfile_ipy(self, fname, shell_futures=False, raise_exceptions=False):
2806 def safe_execfile_ipy(self, fname, shell_futures=False, raise_exceptions=False):
2796 """Like safe_execfile, but for .ipy or .ipynb files with IPython syntax.
2807 """Like safe_execfile, but for .ipy or .ipynb files with IPython syntax.
2797
2808
2798 Parameters
2809 Parameters
2799 ----------
2810 ----------
2800 fname : str
2811 fname : str
2801 The name of the file to execute. The filename must have a
2812 The name of the file to execute. The filename must have a
2802 .ipy or .ipynb extension.
2813 .ipy or .ipynb extension.
2803 shell_futures : bool (False)
2814 shell_futures : bool (False)
2804 If True, the code will share future statements with the interactive
2815 If True, the code will share future statements with the interactive
2805 shell. It will both be affected by previous __future__ imports, and
2816 shell. It will both be affected by previous __future__ imports, and
2806 any __future__ imports in the code will affect the shell. If False,
2817 any __future__ imports in the code will affect the shell. If False,
2807 __future__ imports are not shared in either direction.
2818 __future__ imports are not shared in either direction.
2808 raise_exceptions : bool (False)
2819 raise_exceptions : bool (False)
2809 If True raise exceptions everywhere. Meant for testing.
2820 If True raise exceptions everywhere. Meant for testing.
2810 """
2821 """
2811 fname = Path(fname).expanduser().resolve()
2822 fname = Path(fname).expanduser().resolve()
2812
2823
2813 # Make sure we can open the file
2824 # Make sure we can open the file
2814 try:
2825 try:
2815 with fname.open("rb"):
2826 with fname.open("rb"):
2816 pass
2827 pass
2817 except:
2828 except:
2818 warn('Could not open file <%s> for safe execution.' % fname)
2829 warn('Could not open file <%s> for safe execution.' % fname)
2819 return
2830 return
2820
2831
2821 # Find things also in current directory. This is needed to mimic the
2832 # Find things also in current directory. This is needed to mimic the
2822 # behavior of running a script from the system command line, where
2833 # behavior of running a script from the system command line, where
2823 # Python inserts the script's directory into sys.path
2834 # Python inserts the script's directory into sys.path
2824 dname = str(fname.parent)
2835 dname = str(fname.parent)
2825
2836
2826 def get_cells():
2837 def get_cells():
2827 """generator for sequence of code blocks to run"""
2838 """generator for sequence of code blocks to run"""
2828 if fname.suffix == ".ipynb":
2839 if fname.suffix == ".ipynb":
2829 from nbformat import read
2840 from nbformat import read
2830 nb = read(fname, as_version=4)
2841 nb = read(fname, as_version=4)
2831 if not nb.cells:
2842 if not nb.cells:
2832 return
2843 return
2833 for cell in nb.cells:
2844 for cell in nb.cells:
2834 if cell.cell_type == 'code':
2845 if cell.cell_type == 'code':
2835 yield cell.source
2846 yield cell.source
2836 else:
2847 else:
2837 yield fname.read_text(encoding="utf-8")
2848 yield fname.read_text(encoding="utf-8")
2838
2849
2839 with prepended_to_syspath(dname):
2850 with prepended_to_syspath(dname):
2840 try:
2851 try:
2841 for cell in get_cells():
2852 for cell in get_cells():
2842 result = self.run_cell(cell, silent=True, shell_futures=shell_futures)
2853 result = self.run_cell(cell, silent=True, shell_futures=shell_futures)
2843 if raise_exceptions:
2854 if raise_exceptions:
2844 result.raise_error()
2855 result.raise_error()
2845 elif not result.success:
2856 elif not result.success:
2846 break
2857 break
2847 except:
2858 except:
2848 if raise_exceptions:
2859 if raise_exceptions:
2849 raise
2860 raise
2850 self.showtraceback()
2861 self.showtraceback()
2851 warn('Unknown failure executing file: <%s>' % fname)
2862 warn('Unknown failure executing file: <%s>' % fname)
2852
2863
2853 def safe_run_module(self, mod_name, where):
2864 def safe_run_module(self, mod_name, where):
2854 """A safe version of runpy.run_module().
2865 """A safe version of runpy.run_module().
2855
2866
2856 This version will never throw an exception, but instead print
2867 This version will never throw an exception, but instead print
2857 helpful error messages to the screen.
2868 helpful error messages to the screen.
2858
2869
2859 `SystemExit` exceptions with status code 0 or None are ignored.
2870 `SystemExit` exceptions with status code 0 or None are ignored.
2860
2871
2861 Parameters
2872 Parameters
2862 ----------
2873 ----------
2863 mod_name : string
2874 mod_name : string
2864 The name of the module to be executed.
2875 The name of the module to be executed.
2865 where : dict
2876 where : dict
2866 The globals namespace.
2877 The globals namespace.
2867 """
2878 """
2868 try:
2879 try:
2869 try:
2880 try:
2870 where.update(
2881 where.update(
2871 runpy.run_module(str(mod_name), run_name="__main__",
2882 runpy.run_module(str(mod_name), run_name="__main__",
2872 alter_sys=True)
2883 alter_sys=True)
2873 )
2884 )
2874 except SystemExit as status:
2885 except SystemExit as status:
2875 if status.code:
2886 if status.code:
2876 raise
2887 raise
2877 except:
2888 except:
2878 self.showtraceback()
2889 self.showtraceback()
2879 warn('Unknown failure executing module: <%s>' % mod_name)
2890 warn('Unknown failure executing module: <%s>' % mod_name)
2880
2891
2881 def run_cell(
2892 def run_cell(
2882 self,
2893 self,
2883 raw_cell,
2894 raw_cell,
2884 store_history=False,
2895 store_history=False,
2885 silent=False,
2896 silent=False,
2886 shell_futures=True,
2897 shell_futures=True,
2887 cell_id=None,
2898 cell_id=None,
2888 ):
2899 ):
2889 """Run a complete IPython cell.
2900 """Run a complete IPython cell.
2890
2901
2891 Parameters
2902 Parameters
2892 ----------
2903 ----------
2893 raw_cell : str
2904 raw_cell : str
2894 The code (including IPython code such as %magic functions) to run.
2905 The code (including IPython code such as %magic functions) to run.
2895 store_history : bool
2906 store_history : bool
2896 If True, the raw and translated cell will be stored in IPython's
2907 If True, the raw and translated cell will be stored in IPython's
2897 history. For user code calling back into IPython's machinery, this
2908 history. For user code calling back into IPython's machinery, this
2898 should be set to False.
2909 should be set to False.
2899 silent : bool
2910 silent : bool
2900 If True, avoid side-effects, such as implicit displayhooks and
2911 If True, avoid side-effects, such as implicit displayhooks and
2901 and logging. silent=True forces store_history=False.
2912 and logging. silent=True forces store_history=False.
2902 shell_futures : bool
2913 shell_futures : bool
2903 If True, the code will share future statements with the interactive
2914 If True, the code will share future statements with the interactive
2904 shell. It will both be affected by previous __future__ imports, and
2915 shell. It will both be affected by previous __future__ imports, and
2905 any __future__ imports in the code will affect the shell. If False,
2916 any __future__ imports in the code will affect the shell. If False,
2906 __future__ imports are not shared in either direction.
2917 __future__ imports are not shared in either direction.
2907
2918
2908 Returns
2919 Returns
2909 -------
2920 -------
2910 result : :class:`ExecutionResult`
2921 result : :class:`ExecutionResult`
2911 """
2922 """
2912 result = None
2923 result = None
2913 try:
2924 try:
2914 result = self._run_cell(
2925 result = self._run_cell(
2915 raw_cell, store_history, silent, shell_futures, cell_id
2926 raw_cell, store_history, silent, shell_futures, cell_id
2916 )
2927 )
2917 finally:
2928 finally:
2918 self.events.trigger('post_execute')
2929 self.events.trigger('post_execute')
2919 if not silent:
2930 if not silent:
2920 self.events.trigger('post_run_cell', result)
2931 self.events.trigger('post_run_cell', result)
2921 return result
2932 return result
2922
2933
2923 def _run_cell(
2934 def _run_cell(
2924 self,
2935 self,
2925 raw_cell: str,
2936 raw_cell: str,
2926 store_history: bool,
2937 store_history: bool,
2927 silent: bool,
2938 silent: bool,
2928 shell_futures: bool,
2939 shell_futures: bool,
2929 cell_id: str,
2940 cell_id: str,
2930 ) -> ExecutionResult:
2941 ) -> ExecutionResult:
2931 """Internal method to run a complete IPython cell."""
2942 """Internal method to run a complete IPython cell."""
2932
2943
2933 # we need to avoid calling self.transform_cell multiple time on the same thing
2944 # we need to avoid calling self.transform_cell multiple time on the same thing
2934 # so we need to store some results:
2945 # so we need to store some results:
2935 preprocessing_exc_tuple = None
2946 preprocessing_exc_tuple = None
2936 try:
2947 try:
2937 transformed_cell = self.transform_cell(raw_cell)
2948 transformed_cell = self.transform_cell(raw_cell)
2938 except Exception:
2949 except Exception:
2939 transformed_cell = raw_cell
2950 transformed_cell = raw_cell
2940 preprocessing_exc_tuple = sys.exc_info()
2951 preprocessing_exc_tuple = sys.exc_info()
2941
2952
2942 assert transformed_cell is not None
2953 assert transformed_cell is not None
2943 coro = self.run_cell_async(
2954 coro = self.run_cell_async(
2944 raw_cell,
2955 raw_cell,
2945 store_history=store_history,
2956 store_history=store_history,
2946 silent=silent,
2957 silent=silent,
2947 shell_futures=shell_futures,
2958 shell_futures=shell_futures,
2948 transformed_cell=transformed_cell,
2959 transformed_cell=transformed_cell,
2949 preprocessing_exc_tuple=preprocessing_exc_tuple,
2960 preprocessing_exc_tuple=preprocessing_exc_tuple,
2950 cell_id=cell_id,
2961 cell_id=cell_id,
2951 )
2962 )
2952
2963
2953 # run_cell_async is async, but may not actually need an eventloop.
2964 # run_cell_async is async, but may not actually need an eventloop.
2954 # when this is the case, we want to run it using the pseudo_sync_runner
2965 # when this is the case, we want to run it using the pseudo_sync_runner
2955 # so that code can invoke eventloops (for example via the %run , and
2966 # so that code can invoke eventloops (for example via the %run , and
2956 # `%paste` magic.
2967 # `%paste` magic.
2957 if self.trio_runner:
2968 if self.trio_runner:
2958 runner = self.trio_runner
2969 runner = self.trio_runner
2959 elif self.should_run_async(
2970 elif self.should_run_async(
2960 raw_cell,
2971 raw_cell,
2961 transformed_cell=transformed_cell,
2972 transformed_cell=transformed_cell,
2962 preprocessing_exc_tuple=preprocessing_exc_tuple,
2973 preprocessing_exc_tuple=preprocessing_exc_tuple,
2963 ):
2974 ):
2964 runner = self.loop_runner
2975 runner = self.loop_runner
2965 else:
2976 else:
2966 runner = _pseudo_sync_runner
2977 runner = _pseudo_sync_runner
2967
2978
2968 try:
2979 try:
2969 return runner(coro)
2980 return runner(coro)
2970 except BaseException as e:
2981 except BaseException as e:
2971 info = ExecutionInfo(
2982 info = ExecutionInfo(
2972 raw_cell, store_history, silent, shell_futures, cell_id
2983 raw_cell, store_history, silent, shell_futures, cell_id
2973 )
2984 )
2974 result = ExecutionResult(info)
2985 result = ExecutionResult(info)
2975 result.error_in_exec = e
2986 result.error_in_exec = e
2976 self.showtraceback(running_compiled_code=True)
2987 self.showtraceback(running_compiled_code=True)
2977 return result
2988 return result
2978
2989
2979 def should_run_async(
2990 def should_run_async(
2980 self, raw_cell: str, *, transformed_cell=None, preprocessing_exc_tuple=None
2991 self, raw_cell: str, *, transformed_cell=None, preprocessing_exc_tuple=None
2981 ) -> bool:
2992 ) -> bool:
2982 """Return whether a cell should be run asynchronously via a coroutine runner
2993 """Return whether a cell should be run asynchronously via a coroutine runner
2983
2994
2984 Parameters
2995 Parameters
2985 ----------
2996 ----------
2986 raw_cell : str
2997 raw_cell : str
2987 The code to be executed
2998 The code to be executed
2988
2999
2989 Returns
3000 Returns
2990 -------
3001 -------
2991 result: bool
3002 result: bool
2992 Whether the code needs to be run with a coroutine runner or not
3003 Whether the code needs to be run with a coroutine runner or not
2993 .. versionadded:: 7.0
3004 .. versionadded:: 7.0
2994 """
3005 """
2995 if not self.autoawait:
3006 if not self.autoawait:
2996 return False
3007 return False
2997 if preprocessing_exc_tuple is not None:
3008 if preprocessing_exc_tuple is not None:
2998 return False
3009 return False
2999 assert preprocessing_exc_tuple is None
3010 assert preprocessing_exc_tuple is None
3000 if transformed_cell is None:
3011 if transformed_cell is None:
3001 warnings.warn(
3012 warnings.warn(
3002 "`should_run_async` will not call `transform_cell`"
3013 "`should_run_async` will not call `transform_cell`"
3003 " automatically in the future. Please pass the result to"
3014 " automatically in the future. Please pass the result to"
3004 " `transformed_cell` argument and any exception that happen"
3015 " `transformed_cell` argument and any exception that happen"
3005 " during the"
3016 " during the"
3006 "transform in `preprocessing_exc_tuple` in"
3017 "transform in `preprocessing_exc_tuple` in"
3007 " IPython 7.17 and above.",
3018 " IPython 7.17 and above.",
3008 DeprecationWarning,
3019 DeprecationWarning,
3009 stacklevel=2,
3020 stacklevel=2,
3010 )
3021 )
3011 try:
3022 try:
3012 cell = self.transform_cell(raw_cell)
3023 cell = self.transform_cell(raw_cell)
3013 except Exception:
3024 except Exception:
3014 # any exception during transform will be raised
3025 # any exception during transform will be raised
3015 # prior to execution
3026 # prior to execution
3016 return False
3027 return False
3017 else:
3028 else:
3018 cell = transformed_cell
3029 cell = transformed_cell
3019 return _should_be_async(cell)
3030 return _should_be_async(cell)
3020
3031
3021 async def run_cell_async(
3032 async def run_cell_async(
3022 self,
3033 self,
3023 raw_cell: str,
3034 raw_cell: str,
3024 store_history=False,
3035 store_history=False,
3025 silent=False,
3036 silent=False,
3026 shell_futures=True,
3037 shell_futures=True,
3027 *,
3038 *,
3028 transformed_cell: Optional[str] = None,
3039 transformed_cell: Optional[str] = None,
3029 preprocessing_exc_tuple: Optional[Any] = None,
3040 preprocessing_exc_tuple: Optional[Any] = None,
3030 cell_id=None,
3041 cell_id=None,
3031 ) -> ExecutionResult:
3042 ) -> ExecutionResult:
3032 """Run a complete IPython cell asynchronously.
3043 """Run a complete IPython cell asynchronously.
3033
3044
3034 Parameters
3045 Parameters
3035 ----------
3046 ----------
3036 raw_cell : str
3047 raw_cell : str
3037 The code (including IPython code such as %magic functions) to run.
3048 The code (including IPython code such as %magic functions) to run.
3038 store_history : bool
3049 store_history : bool
3039 If True, the raw and translated cell will be stored in IPython's
3050 If True, the raw and translated cell will be stored in IPython's
3040 history. For user code calling back into IPython's machinery, this
3051 history. For user code calling back into IPython's machinery, this
3041 should be set to False.
3052 should be set to False.
3042 silent : bool
3053 silent : bool
3043 If True, avoid side-effects, such as implicit displayhooks and
3054 If True, avoid side-effects, such as implicit displayhooks and
3044 and logging. silent=True forces store_history=False.
3055 and logging. silent=True forces store_history=False.
3045 shell_futures : bool
3056 shell_futures : bool
3046 If True, the code will share future statements with the interactive
3057 If True, the code will share future statements with the interactive
3047 shell. It will both be affected by previous __future__ imports, and
3058 shell. It will both be affected by previous __future__ imports, and
3048 any __future__ imports in the code will affect the shell. If False,
3059 any __future__ imports in the code will affect the shell. If False,
3049 __future__ imports are not shared in either direction.
3060 __future__ imports are not shared in either direction.
3050 transformed_cell: str
3061 transformed_cell: str
3051 cell that was passed through transformers
3062 cell that was passed through transformers
3052 preprocessing_exc_tuple:
3063 preprocessing_exc_tuple:
3053 trace if the transformation failed.
3064 trace if the transformation failed.
3054
3065
3055 Returns
3066 Returns
3056 -------
3067 -------
3057 result : :class:`ExecutionResult`
3068 result : :class:`ExecutionResult`
3058
3069
3059 .. versionadded:: 7.0
3070 .. versionadded:: 7.0
3060 """
3071 """
3061 info = ExecutionInfo(raw_cell, store_history, silent, shell_futures, cell_id)
3072 info = ExecutionInfo(raw_cell, store_history, silent, shell_futures, cell_id)
3062 result = ExecutionResult(info)
3073 result = ExecutionResult(info)
3063
3074
3064 if (not raw_cell) or raw_cell.isspace():
3075 if (not raw_cell) or raw_cell.isspace():
3065 self.last_execution_succeeded = True
3076 self.last_execution_succeeded = True
3066 self.last_execution_result = result
3077 self.last_execution_result = result
3067 return result
3078 return result
3068
3079
3069 if silent:
3080 if silent:
3070 store_history = False
3081 store_history = False
3071
3082
3072 if store_history:
3083 if store_history:
3073 result.execution_count = self.execution_count
3084 result.execution_count = self.execution_count
3074
3085
3075 def error_before_exec(value):
3086 def error_before_exec(value):
3076 if store_history:
3087 if store_history:
3077 self.execution_count += 1
3088 self.execution_count += 1
3078 result.error_before_exec = value
3089 result.error_before_exec = value
3079 self.last_execution_succeeded = False
3090 self.last_execution_succeeded = False
3080 self.last_execution_result = result
3091 self.last_execution_result = result
3081 return result
3092 return result
3082
3093
3083 self.events.trigger('pre_execute')
3094 self.events.trigger('pre_execute')
3084 if not silent:
3095 if not silent:
3085 self.events.trigger('pre_run_cell', info)
3096 self.events.trigger('pre_run_cell', info)
3086
3097
3087 if transformed_cell is None:
3098 if transformed_cell is None:
3088 warnings.warn(
3099 warnings.warn(
3089 "`run_cell_async` will not call `transform_cell`"
3100 "`run_cell_async` will not call `transform_cell`"
3090 " automatically in the future. Please pass the result to"
3101 " automatically in the future. Please pass the result to"
3091 " `transformed_cell` argument and any exception that happen"
3102 " `transformed_cell` argument and any exception that happen"
3092 " during the"
3103 " during the"
3093 "transform in `preprocessing_exc_tuple` in"
3104 "transform in `preprocessing_exc_tuple` in"
3094 " IPython 7.17 and above.",
3105 " IPython 7.17 and above.",
3095 DeprecationWarning,
3106 DeprecationWarning,
3096 stacklevel=2,
3107 stacklevel=2,
3097 )
3108 )
3098 # If any of our input transformation (input_transformer_manager or
3109 # If any of our input transformation (input_transformer_manager or
3099 # prefilter_manager) raises an exception, we store it in this variable
3110 # prefilter_manager) raises an exception, we store it in this variable
3100 # so that we can display the error after logging the input and storing
3111 # so that we can display the error after logging the input and storing
3101 # it in the history.
3112 # it in the history.
3102 try:
3113 try:
3103 cell = self.transform_cell(raw_cell)
3114 cell = self.transform_cell(raw_cell)
3104 except Exception:
3115 except Exception:
3105 preprocessing_exc_tuple = sys.exc_info()
3116 preprocessing_exc_tuple = sys.exc_info()
3106 cell = raw_cell # cell has to exist so it can be stored/logged
3117 cell = raw_cell # cell has to exist so it can be stored/logged
3107 else:
3118 else:
3108 preprocessing_exc_tuple = None
3119 preprocessing_exc_tuple = None
3109 else:
3120 else:
3110 if preprocessing_exc_tuple is None:
3121 if preprocessing_exc_tuple is None:
3111 cell = transformed_cell
3122 cell = transformed_cell
3112 else:
3123 else:
3113 cell = raw_cell
3124 cell = raw_cell
3114
3125
3115 # Store raw and processed history
3126 # Store raw and processed history
3116 if store_history and raw_cell.strip(" %") != "paste":
3127 if store_history and raw_cell.strip(" %") != "paste":
3117 self.history_manager.store_inputs(self.execution_count, cell, raw_cell)
3128 self.history_manager.store_inputs(self.execution_count, cell, raw_cell)
3118 if not silent:
3129 if not silent:
3119 self.logger.log(cell, raw_cell)
3130 self.logger.log(cell, raw_cell)
3120
3131
3121 # Display the exception if input processing failed.
3132 # Display the exception if input processing failed.
3122 if preprocessing_exc_tuple is not None:
3133 if preprocessing_exc_tuple is not None:
3123 self.showtraceback(preprocessing_exc_tuple)
3134 self.showtraceback(preprocessing_exc_tuple)
3124 if store_history:
3135 if store_history:
3125 self.execution_count += 1
3136 self.execution_count += 1
3126 return error_before_exec(preprocessing_exc_tuple[1])
3137 return error_before_exec(preprocessing_exc_tuple[1])
3127
3138
3128 # Our own compiler remembers the __future__ environment. If we want to
3139 # Our own compiler remembers the __future__ environment. If we want to
3129 # run code with a separate __future__ environment, use the default
3140 # run code with a separate __future__ environment, use the default
3130 # compiler
3141 # compiler
3131 compiler = self.compile if shell_futures else self.compiler_class()
3142 compiler = self.compile if shell_futures else self.compiler_class()
3132
3143
3133 _run_async = False
3144 _run_async = False
3134
3145
3135 with self.builtin_trap:
3146 with self.builtin_trap:
3136 cell_name = compiler.cache(cell, self.execution_count, raw_code=raw_cell)
3147 cell_name = compiler.cache(cell, self.execution_count, raw_code=raw_cell)
3137
3148
3138 with self.display_trap:
3149 with self.display_trap:
3139 # Compile to bytecode
3150 # Compile to bytecode
3140 try:
3151 try:
3141 code_ast = compiler.ast_parse(cell, filename=cell_name)
3152 code_ast = compiler.ast_parse(cell, filename=cell_name)
3142 except self.custom_exceptions as e:
3153 except self.custom_exceptions as e:
3143 etype, value, tb = sys.exc_info()
3154 etype, value, tb = sys.exc_info()
3144 self.CustomTB(etype, value, tb)
3155 self.CustomTB(etype, value, tb)
3145 return error_before_exec(e)
3156 return error_before_exec(e)
3146 except IndentationError as e:
3157 except IndentationError as e:
3147 self.showindentationerror()
3158 self.showindentationerror()
3148 return error_before_exec(e)
3159 return error_before_exec(e)
3149 except (OverflowError, SyntaxError, ValueError, TypeError,
3160 except (OverflowError, SyntaxError, ValueError, TypeError,
3150 MemoryError) as e:
3161 MemoryError) as e:
3151 self.showsyntaxerror()
3162 self.showsyntaxerror()
3152 return error_before_exec(e)
3163 return error_before_exec(e)
3153
3164
3154 # Apply AST transformations
3165 # Apply AST transformations
3155 try:
3166 try:
3156 code_ast = self.transform_ast(code_ast)
3167 code_ast = self.transform_ast(code_ast)
3157 except InputRejected as e:
3168 except InputRejected as e:
3158 self.showtraceback()
3169 self.showtraceback()
3159 return error_before_exec(e)
3170 return error_before_exec(e)
3160
3171
3161 # Give the displayhook a reference to our ExecutionResult so it
3172 # Give the displayhook a reference to our ExecutionResult so it
3162 # can fill in the output value.
3173 # can fill in the output value.
3163 self.displayhook.exec_result = result
3174 self.displayhook.exec_result = result
3164
3175
3165 # Execute the user code
3176 # Execute the user code
3166 interactivity = "none" if silent else self.ast_node_interactivity
3177 interactivity = "none" if silent else self.ast_node_interactivity
3167
3178
3168 has_raised = await self.run_ast_nodes(code_ast.body, cell_name,
3179 has_raised = await self.run_ast_nodes(code_ast.body, cell_name,
3169 interactivity=interactivity, compiler=compiler, result=result)
3180 interactivity=interactivity, compiler=compiler, result=result)
3170
3181
3171 self.last_execution_succeeded = not has_raised
3182 self.last_execution_succeeded = not has_raised
3172 self.last_execution_result = result
3183 self.last_execution_result = result
3173
3184
3174 # Reset this so later displayed values do not modify the
3185 # Reset this so later displayed values do not modify the
3175 # ExecutionResult
3186 # ExecutionResult
3176 self.displayhook.exec_result = None
3187 self.displayhook.exec_result = None
3177
3188
3178 if store_history:
3189 if store_history:
3179 # Write output to the database. Does nothing unless
3190 # Write output to the database. Does nothing unless
3180 # history output logging is enabled.
3191 # history output logging is enabled.
3181 self.history_manager.store_output(self.execution_count)
3192 self.history_manager.store_output(self.execution_count)
3182 # Each cell is a *single* input, regardless of how many lines it has
3193 # Each cell is a *single* input, regardless of how many lines it has
3183 self.execution_count += 1
3194 self.execution_count += 1
3184
3195
3185 return result
3196 return result
3186
3197
3187 def transform_cell(self, raw_cell):
3198 def transform_cell(self, raw_cell):
3188 """Transform an input cell before parsing it.
3199 """Transform an input cell before parsing it.
3189
3200
3190 Static transformations, implemented in IPython.core.inputtransformer2,
3201 Static transformations, implemented in IPython.core.inputtransformer2,
3191 deal with things like ``%magic`` and ``!system`` commands.
3202 deal with things like ``%magic`` and ``!system`` commands.
3192 These run on all input.
3203 These run on all input.
3193 Dynamic transformations, for things like unescaped magics and the exit
3204 Dynamic transformations, for things like unescaped magics and the exit
3194 autocall, depend on the state of the interpreter.
3205 autocall, depend on the state of the interpreter.
3195 These only apply to single line inputs.
3206 These only apply to single line inputs.
3196
3207
3197 These string-based transformations are followed by AST transformations;
3208 These string-based transformations are followed by AST transformations;
3198 see :meth:`transform_ast`.
3209 see :meth:`transform_ast`.
3199 """
3210 """
3200 # Static input transformations
3211 # Static input transformations
3201 cell = self.input_transformer_manager.transform_cell(raw_cell)
3212 cell = self.input_transformer_manager.transform_cell(raw_cell)
3202
3213
3203 if len(cell.splitlines()) == 1:
3214 if len(cell.splitlines()) == 1:
3204 # Dynamic transformations - only applied for single line commands
3215 # Dynamic transformations - only applied for single line commands
3205 with self.builtin_trap:
3216 with self.builtin_trap:
3206 # use prefilter_lines to handle trailing newlines
3217 # use prefilter_lines to handle trailing newlines
3207 # restore trailing newline for ast.parse
3218 # restore trailing newline for ast.parse
3208 cell = self.prefilter_manager.prefilter_lines(cell) + '\n'
3219 cell = self.prefilter_manager.prefilter_lines(cell) + '\n'
3209
3220
3210 lines = cell.splitlines(keepends=True)
3221 lines = cell.splitlines(keepends=True)
3211 for transform in self.input_transformers_post:
3222 for transform in self.input_transformers_post:
3212 lines = transform(lines)
3223 lines = transform(lines)
3213 cell = ''.join(lines)
3224 cell = ''.join(lines)
3214
3225
3215 return cell
3226 return cell
3216
3227
3217 def transform_ast(self, node):
3228 def transform_ast(self, node):
3218 """Apply the AST transformations from self.ast_transformers
3229 """Apply the AST transformations from self.ast_transformers
3219
3230
3220 Parameters
3231 Parameters
3221 ----------
3232 ----------
3222 node : ast.Node
3233 node : ast.Node
3223 The root node to be transformed. Typically called with the ast.Module
3234 The root node to be transformed. Typically called with the ast.Module
3224 produced by parsing user input.
3235 produced by parsing user input.
3225
3236
3226 Returns
3237 Returns
3227 -------
3238 -------
3228 An ast.Node corresponding to the node it was called with. Note that it
3239 An ast.Node corresponding to the node it was called with. Note that it
3229 may also modify the passed object, so don't rely on references to the
3240 may also modify the passed object, so don't rely on references to the
3230 original AST.
3241 original AST.
3231 """
3242 """
3232 for transformer in self.ast_transformers:
3243 for transformer in self.ast_transformers:
3233 try:
3244 try:
3234 node = transformer.visit(node)
3245 node = transformer.visit(node)
3235 except InputRejected:
3246 except InputRejected:
3236 # User-supplied AST transformers can reject an input by raising
3247 # User-supplied AST transformers can reject an input by raising
3237 # an InputRejected. Short-circuit in this case so that we
3248 # an InputRejected. Short-circuit in this case so that we
3238 # don't unregister the transform.
3249 # don't unregister the transform.
3239 raise
3250 raise
3240 except Exception:
3251 except Exception:
3241 warn("AST transformer %r threw an error. It will be unregistered." % transformer)
3252 warn("AST transformer %r threw an error. It will be unregistered." % transformer)
3242 self.ast_transformers.remove(transformer)
3253 self.ast_transformers.remove(transformer)
3243
3254
3244 if self.ast_transformers:
3255 if self.ast_transformers:
3245 ast.fix_missing_locations(node)
3256 ast.fix_missing_locations(node)
3246 return node
3257 return node
3247
3258
3248 async def run_ast_nodes(
3259 async def run_ast_nodes(
3249 self,
3260 self,
3250 nodelist: ListType[stmt],
3261 nodelist: ListType[stmt],
3251 cell_name: str,
3262 cell_name: str,
3252 interactivity="last_expr",
3263 interactivity="last_expr",
3253 compiler=compile,
3264 compiler=compile,
3254 result=None,
3265 result=None,
3255 ):
3266 ):
3256 """Run a sequence of AST nodes. The execution mode depends on the
3267 """Run a sequence of AST nodes. The execution mode depends on the
3257 interactivity parameter.
3268 interactivity parameter.
3258
3269
3259 Parameters
3270 Parameters
3260 ----------
3271 ----------
3261 nodelist : list
3272 nodelist : list
3262 A sequence of AST nodes to run.
3273 A sequence of AST nodes to run.
3263 cell_name : str
3274 cell_name : str
3264 Will be passed to the compiler as the filename of the cell. Typically
3275 Will be passed to the compiler as the filename of the cell. Typically
3265 the value returned by ip.compile.cache(cell).
3276 the value returned by ip.compile.cache(cell).
3266 interactivity : str
3277 interactivity : str
3267 'all', 'last', 'last_expr' , 'last_expr_or_assign' or 'none',
3278 'all', 'last', 'last_expr' , 'last_expr_or_assign' or 'none',
3268 specifying which nodes should be run interactively (displaying output
3279 specifying which nodes should be run interactively (displaying output
3269 from expressions). 'last_expr' will run the last node interactively
3280 from expressions). 'last_expr' will run the last node interactively
3270 only if it is an expression (i.e. expressions in loops or other blocks
3281 only if it is an expression (i.e. expressions in loops or other blocks
3271 are not displayed) 'last_expr_or_assign' will run the last expression
3282 are not displayed) 'last_expr_or_assign' will run the last expression
3272 or the last assignment. Other values for this parameter will raise a
3283 or the last assignment. Other values for this parameter will raise a
3273 ValueError.
3284 ValueError.
3274
3285
3275 compiler : callable
3286 compiler : callable
3276 A function with the same interface as the built-in compile(), to turn
3287 A function with the same interface as the built-in compile(), to turn
3277 the AST nodes into code objects. Default is the built-in compile().
3288 the AST nodes into code objects. Default is the built-in compile().
3278 result : ExecutionResult, optional
3289 result : ExecutionResult, optional
3279 An object to store exceptions that occur during execution.
3290 An object to store exceptions that occur during execution.
3280
3291
3281 Returns
3292 Returns
3282 -------
3293 -------
3283 True if an exception occurred while running code, False if it finished
3294 True if an exception occurred while running code, False if it finished
3284 running.
3295 running.
3285 """
3296 """
3286 if not nodelist:
3297 if not nodelist:
3287 return
3298 return
3288
3299
3289
3300
3290 if interactivity == 'last_expr_or_assign':
3301 if interactivity == 'last_expr_or_assign':
3291 if isinstance(nodelist[-1], _assign_nodes):
3302 if isinstance(nodelist[-1], _assign_nodes):
3292 asg = nodelist[-1]
3303 asg = nodelist[-1]
3293 if isinstance(asg, ast.Assign) and len(asg.targets) == 1:
3304 if isinstance(asg, ast.Assign) and len(asg.targets) == 1:
3294 target = asg.targets[0]
3305 target = asg.targets[0]
3295 elif isinstance(asg, _single_targets_nodes):
3306 elif isinstance(asg, _single_targets_nodes):
3296 target = asg.target
3307 target = asg.target
3297 else:
3308 else:
3298 target = None
3309 target = None
3299 if isinstance(target, ast.Name):
3310 if isinstance(target, ast.Name):
3300 nnode = ast.Expr(ast.Name(target.id, ast.Load()))
3311 nnode = ast.Expr(ast.Name(target.id, ast.Load()))
3301 ast.fix_missing_locations(nnode)
3312 ast.fix_missing_locations(nnode)
3302 nodelist.append(nnode)
3313 nodelist.append(nnode)
3303 interactivity = 'last_expr'
3314 interactivity = 'last_expr'
3304
3315
3305 _async = False
3316 _async = False
3306 if interactivity == 'last_expr':
3317 if interactivity == 'last_expr':
3307 if isinstance(nodelist[-1], ast.Expr):
3318 if isinstance(nodelist[-1], ast.Expr):
3308 interactivity = "last"
3319 interactivity = "last"
3309 else:
3320 else:
3310 interactivity = "none"
3321 interactivity = "none"
3311
3322
3312 if interactivity == 'none':
3323 if interactivity == 'none':
3313 to_run_exec, to_run_interactive = nodelist, []
3324 to_run_exec, to_run_interactive = nodelist, []
3314 elif interactivity == 'last':
3325 elif interactivity == 'last':
3315 to_run_exec, to_run_interactive = nodelist[:-1], nodelist[-1:]
3326 to_run_exec, to_run_interactive = nodelist[:-1], nodelist[-1:]
3316 elif interactivity == 'all':
3327 elif interactivity == 'all':
3317 to_run_exec, to_run_interactive = [], nodelist
3328 to_run_exec, to_run_interactive = [], nodelist
3318 else:
3329 else:
3319 raise ValueError("Interactivity was %r" % interactivity)
3330 raise ValueError("Interactivity was %r" % interactivity)
3320
3331
3321 try:
3332 try:
3322
3333
3323 def compare(code):
3334 def compare(code):
3324 is_async = inspect.CO_COROUTINE & code.co_flags == inspect.CO_COROUTINE
3335 is_async = inspect.CO_COROUTINE & code.co_flags == inspect.CO_COROUTINE
3325 return is_async
3336 return is_async
3326
3337
3327 # refactor that to just change the mod constructor.
3338 # refactor that to just change the mod constructor.
3328 to_run = []
3339 to_run = []
3329 for node in to_run_exec:
3340 for node in to_run_exec:
3330 to_run.append((node, "exec"))
3341 to_run.append((node, "exec"))
3331
3342
3332 for node in to_run_interactive:
3343 for node in to_run_interactive:
3333 to_run.append((node, "single"))
3344 to_run.append((node, "single"))
3334
3345
3335 for node, mode in to_run:
3346 for node, mode in to_run:
3336 if mode == "exec":
3347 if mode == "exec":
3337 mod = Module([node], [])
3348 mod = Module([node], [])
3338 elif mode == "single":
3349 elif mode == "single":
3339 mod = ast.Interactive([node])
3350 mod = ast.Interactive([node])
3340 with compiler.extra_flags(
3351 with compiler.extra_flags(
3341 getattr(ast, "PyCF_ALLOW_TOP_LEVEL_AWAIT", 0x0)
3352 getattr(ast, "PyCF_ALLOW_TOP_LEVEL_AWAIT", 0x0)
3342 if self.autoawait
3353 if self.autoawait
3343 else 0x0
3354 else 0x0
3344 ):
3355 ):
3345 code = compiler(mod, cell_name, mode)
3356 code = compiler(mod, cell_name, mode)
3346 asy = compare(code)
3357 asy = compare(code)
3347 if await self.run_code(code, result, async_=asy):
3358 if await self.run_code(code, result, async_=asy):
3348 return True
3359 return True
3349
3360
3350 # Flush softspace
3361 # Flush softspace
3351 if softspace(sys.stdout, 0):
3362 if softspace(sys.stdout, 0):
3352 print()
3363 print()
3353
3364
3354 except:
3365 except:
3355 # It's possible to have exceptions raised here, typically by
3366 # It's possible to have exceptions raised here, typically by
3356 # compilation of odd code (such as a naked 'return' outside a
3367 # compilation of odd code (such as a naked 'return' outside a
3357 # function) that did parse but isn't valid. Typically the exception
3368 # function) that did parse but isn't valid. Typically the exception
3358 # is a SyntaxError, but it's safest just to catch anything and show
3369 # is a SyntaxError, but it's safest just to catch anything and show
3359 # the user a traceback.
3370 # the user a traceback.
3360
3371
3361 # We do only one try/except outside the loop to minimize the impact
3372 # We do only one try/except outside the loop to minimize the impact
3362 # on runtime, and also because if any node in the node list is
3373 # on runtime, and also because if any node in the node list is
3363 # broken, we should stop execution completely.
3374 # broken, we should stop execution completely.
3364 if result:
3375 if result:
3365 result.error_before_exec = sys.exc_info()[1]
3376 result.error_before_exec = sys.exc_info()[1]
3366 self.showtraceback()
3377 self.showtraceback()
3367 return True
3378 return True
3368
3379
3369 return False
3380 return False
3370
3381
3371 async def run_code(self, code_obj, result=None, *, async_=False):
3382 async def run_code(self, code_obj, result=None, *, async_=False):
3372 """Execute a code object.
3383 """Execute a code object.
3373
3384
3374 When an exception occurs, self.showtraceback() is called to display a
3385 When an exception occurs, self.showtraceback() is called to display a
3375 traceback.
3386 traceback.
3376
3387
3377 Parameters
3388 Parameters
3378 ----------
3389 ----------
3379 code_obj : code object
3390 code_obj : code object
3380 A compiled code object, to be executed
3391 A compiled code object, to be executed
3381 result : ExecutionResult, optional
3392 result : ExecutionResult, optional
3382 An object to store exceptions that occur during execution.
3393 An object to store exceptions that occur during execution.
3383 async_ : Bool (Experimental)
3394 async_ : Bool (Experimental)
3384 Attempt to run top-level asynchronous code in a default loop.
3395 Attempt to run top-level asynchronous code in a default loop.
3385
3396
3386 Returns
3397 Returns
3387 -------
3398 -------
3388 False : successful execution.
3399 False : successful execution.
3389 True : an error occurred.
3400 True : an error occurred.
3390 """
3401 """
3391 # special value to say that anything above is IPython and should be
3402 # special value to say that anything above is IPython and should be
3392 # hidden.
3403 # hidden.
3393 __tracebackhide__ = "__ipython_bottom__"
3404 __tracebackhide__ = "__ipython_bottom__"
3394 # Set our own excepthook in case the user code tries to call it
3405 # Set our own excepthook in case the user code tries to call it
3395 # directly, so that the IPython crash handler doesn't get triggered
3406 # directly, so that the IPython crash handler doesn't get triggered
3396 old_excepthook, sys.excepthook = sys.excepthook, self.excepthook
3407 old_excepthook, sys.excepthook = sys.excepthook, self.excepthook
3397
3408
3398 # we save the original sys.excepthook in the instance, in case config
3409 # we save the original sys.excepthook in the instance, in case config
3399 # code (such as magics) needs access to it.
3410 # code (such as magics) needs access to it.
3400 self.sys_excepthook = old_excepthook
3411 self.sys_excepthook = old_excepthook
3401 outflag = True # happens in more places, so it's easier as default
3412 outflag = True # happens in more places, so it's easier as default
3402 try:
3413 try:
3403 try:
3414 try:
3404 if async_:
3415 if async_:
3405 await eval(code_obj, self.user_global_ns, self.user_ns)
3416 await eval(code_obj, self.user_global_ns, self.user_ns)
3406 else:
3417 else:
3407 exec(code_obj, self.user_global_ns, self.user_ns)
3418 exec(code_obj, self.user_global_ns, self.user_ns)
3408 finally:
3419 finally:
3409 # Reset our crash handler in place
3420 # Reset our crash handler in place
3410 sys.excepthook = old_excepthook
3421 sys.excepthook = old_excepthook
3411 except SystemExit as e:
3422 except SystemExit as e:
3412 if result is not None:
3423 if result is not None:
3413 result.error_in_exec = e
3424 result.error_in_exec = e
3414 self.showtraceback(exception_only=True)
3425 self.showtraceback(exception_only=True)
3415 warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1)
3426 warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1)
3416 except bdb.BdbQuit:
3427 except bdb.BdbQuit:
3417 etype, value, tb = sys.exc_info()
3428 etype, value, tb = sys.exc_info()
3418 if result is not None:
3429 if result is not None:
3419 result.error_in_exec = value
3430 result.error_in_exec = value
3420 # the BdbQuit stops here
3431 # the BdbQuit stops here
3421 except self.custom_exceptions:
3432 except self.custom_exceptions:
3422 etype, value, tb = sys.exc_info()
3433 etype, value, tb = sys.exc_info()
3423 if result is not None:
3434 if result is not None:
3424 result.error_in_exec = value
3435 result.error_in_exec = value
3425 self.CustomTB(etype, value, tb)
3436 self.CustomTB(etype, value, tb)
3426 except:
3437 except:
3427 if result is not None:
3438 if result is not None:
3428 result.error_in_exec = sys.exc_info()[1]
3439 result.error_in_exec = sys.exc_info()[1]
3429 self.showtraceback(running_compiled_code=True)
3440 self.showtraceback(running_compiled_code=True)
3430 else:
3441 else:
3431 outflag = False
3442 outflag = False
3432 return outflag
3443 return outflag
3433
3444
3434 # For backwards compatibility
3445 # For backwards compatibility
3435 runcode = run_code
3446 runcode = run_code
3436
3447
3437 def check_complete(self, code: str) -> Tuple[str, str]:
3448 def check_complete(self, code: str) -> Tuple[str, str]:
3438 """Return whether a block of code is ready to execute, or should be continued
3449 """Return whether a block of code is ready to execute, or should be continued
3439
3450
3440 Parameters
3451 Parameters
3441 ----------
3452 ----------
3442 code : string
3453 code : string
3443 Python input code, which can be multiline.
3454 Python input code, which can be multiline.
3444
3455
3445 Returns
3456 Returns
3446 -------
3457 -------
3447 status : str
3458 status : str
3448 One of 'complete', 'incomplete', or 'invalid' if source is not a
3459 One of 'complete', 'incomplete', or 'invalid' if source is not a
3449 prefix of valid code.
3460 prefix of valid code.
3450 indent : str
3461 indent : str
3451 When status is 'incomplete', this is some whitespace to insert on
3462 When status is 'incomplete', this is some whitespace to insert on
3452 the next line of the prompt.
3463 the next line of the prompt.
3453 """
3464 """
3454 status, nspaces = self.input_transformer_manager.check_complete(code)
3465 status, nspaces = self.input_transformer_manager.check_complete(code)
3455 return status, ' ' * (nspaces or 0)
3466 return status, ' ' * (nspaces or 0)
3456
3467
3457 #-------------------------------------------------------------------------
3468 #-------------------------------------------------------------------------
3458 # Things related to GUI support and pylab
3469 # Things related to GUI support and pylab
3459 #-------------------------------------------------------------------------
3470 #-------------------------------------------------------------------------
3460
3471
3461 active_eventloop = None
3472 active_eventloop = None
3462
3473
3463 def enable_gui(self, gui=None):
3474 def enable_gui(self, gui=None):
3464 raise NotImplementedError('Implement enable_gui in a subclass')
3475 raise NotImplementedError('Implement enable_gui in a subclass')
3465
3476
3466 def enable_matplotlib(self, gui=None):
3477 def enable_matplotlib(self, gui=None):
3467 """Enable interactive matplotlib and inline figure support.
3478 """Enable interactive matplotlib and inline figure support.
3468
3479
3469 This takes the following steps:
3480 This takes the following steps:
3470
3481
3471 1. select the appropriate eventloop and matplotlib backend
3482 1. select the appropriate eventloop and matplotlib backend
3472 2. set up matplotlib for interactive use with that backend
3483 2. set up matplotlib for interactive use with that backend
3473 3. configure formatters for inline figure display
3484 3. configure formatters for inline figure display
3474 4. enable the selected gui eventloop
3485 4. enable the selected gui eventloop
3475
3486
3476 Parameters
3487 Parameters
3477 ----------
3488 ----------
3478 gui : optional, string
3489 gui : optional, string
3479 If given, dictates the choice of matplotlib GUI backend to use
3490 If given, dictates the choice of matplotlib GUI backend to use
3480 (should be one of IPython's supported backends, 'qt', 'osx', 'tk',
3491 (should be one of IPython's supported backends, 'qt', 'osx', 'tk',
3481 'gtk', 'wx' or 'inline'), otherwise we use the default chosen by
3492 'gtk', 'wx' or 'inline'), otherwise we use the default chosen by
3482 matplotlib (as dictated by the matplotlib build-time options plus the
3493 matplotlib (as dictated by the matplotlib build-time options plus the
3483 user's matplotlibrc configuration file). Note that not all backends
3494 user's matplotlibrc configuration file). Note that not all backends
3484 make sense in all contexts, for example a terminal ipython can't
3495 make sense in all contexts, for example a terminal ipython can't
3485 display figures inline.
3496 display figures inline.
3486 """
3497 """
3487 from matplotlib_inline.backend_inline import configure_inline_support
3498 from matplotlib_inline.backend_inline import configure_inline_support
3488
3499
3489 from IPython.core import pylabtools as pt
3500 from IPython.core import pylabtools as pt
3490 gui, backend = pt.find_gui_and_backend(gui, self.pylab_gui_select)
3501 gui, backend = pt.find_gui_and_backend(gui, self.pylab_gui_select)
3491
3502
3492 if gui != 'inline':
3503 if gui != 'inline':
3493 # If we have our first gui selection, store it
3504 # If we have our first gui selection, store it
3494 if self.pylab_gui_select is None:
3505 if self.pylab_gui_select is None:
3495 self.pylab_gui_select = gui
3506 self.pylab_gui_select = gui
3496 # Otherwise if they are different
3507 # Otherwise if they are different
3497 elif gui != self.pylab_gui_select:
3508 elif gui != self.pylab_gui_select:
3498 print('Warning: Cannot change to a different GUI toolkit: %s.'
3509 print('Warning: Cannot change to a different GUI toolkit: %s.'
3499 ' Using %s instead.' % (gui, self.pylab_gui_select))
3510 ' Using %s instead.' % (gui, self.pylab_gui_select))
3500 gui, backend = pt.find_gui_and_backend(self.pylab_gui_select)
3511 gui, backend = pt.find_gui_and_backend(self.pylab_gui_select)
3501
3512
3502 pt.activate_matplotlib(backend)
3513 pt.activate_matplotlib(backend)
3503 configure_inline_support(self, backend)
3514 configure_inline_support(self, backend)
3504
3515
3505 # Now we must activate the gui pylab wants to use, and fix %run to take
3516 # Now we must activate the gui pylab wants to use, and fix %run to take
3506 # plot updates into account
3517 # plot updates into account
3507 self.enable_gui(gui)
3518 self.enable_gui(gui)
3508 self.magics_manager.registry['ExecutionMagics'].default_runner = \
3519 self.magics_manager.registry['ExecutionMagics'].default_runner = \
3509 pt.mpl_runner(self.safe_execfile)
3520 pt.mpl_runner(self.safe_execfile)
3510
3521
3511 return gui, backend
3522 return gui, backend
3512
3523
3513 def enable_pylab(self, gui=None, import_all=True, welcome_message=False):
3524 def enable_pylab(self, gui=None, import_all=True, welcome_message=False):
3514 """Activate pylab support at runtime.
3525 """Activate pylab support at runtime.
3515
3526
3516 This turns on support for matplotlib, preloads into the interactive
3527 This turns on support for matplotlib, preloads into the interactive
3517 namespace all of numpy and pylab, and configures IPython to correctly
3528 namespace all of numpy and pylab, and configures IPython to correctly
3518 interact with the GUI event loop. The GUI backend to be used can be
3529 interact with the GUI event loop. The GUI backend to be used can be
3519 optionally selected with the optional ``gui`` argument.
3530 optionally selected with the optional ``gui`` argument.
3520
3531
3521 This method only adds preloading the namespace to InteractiveShell.enable_matplotlib.
3532 This method only adds preloading the namespace to InteractiveShell.enable_matplotlib.
3522
3533
3523 Parameters
3534 Parameters
3524 ----------
3535 ----------
3525 gui : optional, string
3536 gui : optional, string
3526 If given, dictates the choice of matplotlib GUI backend to use
3537 If given, dictates the choice of matplotlib GUI backend to use
3527 (should be one of IPython's supported backends, 'qt', 'osx', 'tk',
3538 (should be one of IPython's supported backends, 'qt', 'osx', 'tk',
3528 'gtk', 'wx' or 'inline'), otherwise we use the default chosen by
3539 'gtk', 'wx' or 'inline'), otherwise we use the default chosen by
3529 matplotlib (as dictated by the matplotlib build-time options plus the
3540 matplotlib (as dictated by the matplotlib build-time options plus the
3530 user's matplotlibrc configuration file). Note that not all backends
3541 user's matplotlibrc configuration file). Note that not all backends
3531 make sense in all contexts, for example a terminal ipython can't
3542 make sense in all contexts, for example a terminal ipython can't
3532 display figures inline.
3543 display figures inline.
3533 import_all : optional, bool, default: True
3544 import_all : optional, bool, default: True
3534 Whether to do `from numpy import *` and `from pylab import *`
3545 Whether to do `from numpy import *` and `from pylab import *`
3535 in addition to module imports.
3546 in addition to module imports.
3536 welcome_message : deprecated
3547 welcome_message : deprecated
3537 This argument is ignored, no welcome message will be displayed.
3548 This argument is ignored, no welcome message will be displayed.
3538 """
3549 """
3539 from IPython.core.pylabtools import import_pylab
3550 from IPython.core.pylabtools import import_pylab
3540
3551
3541 gui, backend = self.enable_matplotlib(gui)
3552 gui, backend = self.enable_matplotlib(gui)
3542
3553
3543 # We want to prevent the loading of pylab to pollute the user's
3554 # We want to prevent the loading of pylab to pollute the user's
3544 # namespace as shown by the %who* magics, so we execute the activation
3555 # namespace as shown by the %who* magics, so we execute the activation
3545 # code in an empty namespace, and we update *both* user_ns and
3556 # code in an empty namespace, and we update *both* user_ns and
3546 # user_ns_hidden with this information.
3557 # user_ns_hidden with this information.
3547 ns = {}
3558 ns = {}
3548 import_pylab(ns, import_all)
3559 import_pylab(ns, import_all)
3549 # warn about clobbered names
3560 # warn about clobbered names
3550 ignored = {"__builtins__"}
3561 ignored = {"__builtins__"}
3551 both = set(ns).intersection(self.user_ns).difference(ignored)
3562 both = set(ns).intersection(self.user_ns).difference(ignored)
3552 clobbered = [ name for name in both if self.user_ns[name] is not ns[name] ]
3563 clobbered = [ name for name in both if self.user_ns[name] is not ns[name] ]
3553 self.user_ns.update(ns)
3564 self.user_ns.update(ns)
3554 self.user_ns_hidden.update(ns)
3565 self.user_ns_hidden.update(ns)
3555 return gui, backend, clobbered
3566 return gui, backend, clobbered
3556
3567
3557 #-------------------------------------------------------------------------
3568 #-------------------------------------------------------------------------
3558 # Utilities
3569 # Utilities
3559 #-------------------------------------------------------------------------
3570 #-------------------------------------------------------------------------
3560
3571
3561 def var_expand(self, cmd, depth=0, formatter=DollarFormatter()):
3572 def var_expand(self, cmd, depth=0, formatter=DollarFormatter()):
3562 """Expand python variables in a string.
3573 """Expand python variables in a string.
3563
3574
3564 The depth argument indicates how many frames above the caller should
3575 The depth argument indicates how many frames above the caller should
3565 be walked to look for the local namespace where to expand variables.
3576 be walked to look for the local namespace where to expand variables.
3566
3577
3567 The global namespace for expansion is always the user's interactive
3578 The global namespace for expansion is always the user's interactive
3568 namespace.
3579 namespace.
3569 """
3580 """
3570 ns = self.user_ns.copy()
3581 ns = self.user_ns.copy()
3571 try:
3582 try:
3572 frame = sys._getframe(depth+1)
3583 frame = sys._getframe(depth+1)
3573 except ValueError:
3584 except ValueError:
3574 # This is thrown if there aren't that many frames on the stack,
3585 # This is thrown if there aren't that many frames on the stack,
3575 # e.g. if a script called run_line_magic() directly.
3586 # e.g. if a script called run_line_magic() directly.
3576 pass
3587 pass
3577 else:
3588 else:
3578 ns.update(frame.f_locals)
3589 ns.update(frame.f_locals)
3579
3590
3580 try:
3591 try:
3581 # We have to use .vformat() here, because 'self' is a valid and common
3592 # We have to use .vformat() here, because 'self' is a valid and common
3582 # name, and expanding **ns for .format() would make it collide with
3593 # name, and expanding **ns for .format() would make it collide with
3583 # the 'self' argument of the method.
3594 # the 'self' argument of the method.
3584 cmd = formatter.vformat(cmd, args=[], kwargs=ns)
3595 cmd = formatter.vformat(cmd, args=[], kwargs=ns)
3585 except Exception:
3596 except Exception:
3586 # if formatter couldn't format, just let it go untransformed
3597 # if formatter couldn't format, just let it go untransformed
3587 pass
3598 pass
3588 return cmd
3599 return cmd
3589
3600
3590 def mktempfile(self, data=None, prefix='ipython_edit_'):
3601 def mktempfile(self, data=None, prefix='ipython_edit_'):
3591 """Make a new tempfile and return its filename.
3602 """Make a new tempfile and return its filename.
3592
3603
3593 This makes a call to tempfile.mkstemp (created in a tempfile.mkdtemp),
3604 This makes a call to tempfile.mkstemp (created in a tempfile.mkdtemp),
3594 but it registers the created filename internally so ipython cleans it up
3605 but it registers the created filename internally so ipython cleans it up
3595 at exit time.
3606 at exit time.
3596
3607
3597 Optional inputs:
3608 Optional inputs:
3598
3609
3599 - data(None): if data is given, it gets written out to the temp file
3610 - data(None): if data is given, it gets written out to the temp file
3600 immediately, and the file is closed again."""
3611 immediately, and the file is closed again."""
3601
3612
3602 dir_path = Path(tempfile.mkdtemp(prefix=prefix))
3613 dir_path = Path(tempfile.mkdtemp(prefix=prefix))
3603 self.tempdirs.append(dir_path)
3614 self.tempdirs.append(dir_path)
3604
3615
3605 handle, filename = tempfile.mkstemp(".py", prefix, dir=str(dir_path))
3616 handle, filename = tempfile.mkstemp(".py", prefix, dir=str(dir_path))
3606 os.close(handle) # On Windows, there can only be one open handle on a file
3617 os.close(handle) # On Windows, there can only be one open handle on a file
3607
3618
3608 file_path = Path(filename)
3619 file_path = Path(filename)
3609 self.tempfiles.append(file_path)
3620 self.tempfiles.append(file_path)
3610
3621
3611 if data:
3622 if data:
3612 file_path.write_text(data, encoding="utf-8")
3623 file_path.write_text(data, encoding="utf-8")
3613 return filename
3624 return filename
3614
3625
3615 def ask_yes_no(self, prompt, default=None, interrupt=None):
3626 def ask_yes_no(self, prompt, default=None, interrupt=None):
3616 if self.quiet:
3627 if self.quiet:
3617 return True
3628 return True
3618 return ask_yes_no(prompt,default,interrupt)
3629 return ask_yes_no(prompt,default,interrupt)
3619
3630
3620 def show_usage(self):
3631 def show_usage(self):
3621 """Show a usage message"""
3632 """Show a usage message"""
3622 page.page(IPython.core.usage.interactive_usage)
3633 page.page(IPython.core.usage.interactive_usage)
3623
3634
3624 def extract_input_lines(self, range_str, raw=False):
3635 def extract_input_lines(self, range_str, raw=False):
3625 """Return as a string a set of input history slices.
3636 """Return as a string a set of input history slices.
3626
3637
3627 Parameters
3638 Parameters
3628 ----------
3639 ----------
3629 range_str : str
3640 range_str : str
3630 The set of slices is given as a string, like "~5/6-~4/2 4:8 9",
3641 The set of slices is given as a string, like "~5/6-~4/2 4:8 9",
3631 since this function is for use by magic functions which get their
3642 since this function is for use by magic functions which get their
3632 arguments as strings. The number before the / is the session
3643 arguments as strings. The number before the / is the session
3633 number: ~n goes n back from the current session.
3644 number: ~n goes n back from the current session.
3634
3645
3635 If empty string is given, returns history of current session
3646 If empty string is given, returns history of current session
3636 without the last input.
3647 without the last input.
3637
3648
3638 raw : bool, optional
3649 raw : bool, optional
3639 By default, the processed input is used. If this is true, the raw
3650 By default, the processed input is used. If this is true, the raw
3640 input history is used instead.
3651 input history is used instead.
3641
3652
3642 Notes
3653 Notes
3643 -----
3654 -----
3644 Slices can be described with two notations:
3655 Slices can be described with two notations:
3645
3656
3646 * ``N:M`` -> standard python form, means including items N...(M-1).
3657 * ``N:M`` -> standard python form, means including items N...(M-1).
3647 * ``N-M`` -> include items N..M (closed endpoint).
3658 * ``N-M`` -> include items N..M (closed endpoint).
3648 """
3659 """
3649 lines = self.history_manager.get_range_by_str(range_str, raw=raw)
3660 lines = self.history_manager.get_range_by_str(range_str, raw=raw)
3650 text = "\n".join(x for _, _, x in lines)
3661 text = "\n".join(x for _, _, x in lines)
3651
3662
3652 # Skip the last line, as it's probably the magic that called this
3663 # Skip the last line, as it's probably the magic that called this
3653 if not range_str:
3664 if not range_str:
3654 if "\n" not in text:
3665 if "\n" not in text:
3655 text = ""
3666 text = ""
3656 else:
3667 else:
3657 text = text[: text.rfind("\n")]
3668 text = text[: text.rfind("\n")]
3658
3669
3659 return text
3670 return text
3660
3671
3661 def find_user_code(self, target, raw=True, py_only=False, skip_encoding_cookie=True, search_ns=False):
3672 def find_user_code(self, target, raw=True, py_only=False, skip_encoding_cookie=True, search_ns=False):
3662 """Get a code string from history, file, url, or a string or macro.
3673 """Get a code string from history, file, url, or a string or macro.
3663
3674
3664 This is mainly used by magic functions.
3675 This is mainly used by magic functions.
3665
3676
3666 Parameters
3677 Parameters
3667 ----------
3678 ----------
3668 target : str
3679 target : str
3669 A string specifying code to retrieve. This will be tried respectively
3680 A string specifying code to retrieve. This will be tried respectively
3670 as: ranges of input history (see %history for syntax), url,
3681 as: ranges of input history (see %history for syntax), url,
3671 corresponding .py file, filename, or an expression evaluating to a
3682 corresponding .py file, filename, or an expression evaluating to a
3672 string or Macro in the user namespace.
3683 string or Macro in the user namespace.
3673
3684
3674 If empty string is given, returns complete history of current
3685 If empty string is given, returns complete history of current
3675 session, without the last line.
3686 session, without the last line.
3676
3687
3677 raw : bool
3688 raw : bool
3678 If true (default), retrieve raw history. Has no effect on the other
3689 If true (default), retrieve raw history. Has no effect on the other
3679 retrieval mechanisms.
3690 retrieval mechanisms.
3680
3691
3681 py_only : bool (default False)
3692 py_only : bool (default False)
3682 Only try to fetch python code, do not try alternative methods to decode file
3693 Only try to fetch python code, do not try alternative methods to decode file
3683 if unicode fails.
3694 if unicode fails.
3684
3695
3685 Returns
3696 Returns
3686 -------
3697 -------
3687 A string of code.
3698 A string of code.
3688 ValueError is raised if nothing is found, and TypeError if it evaluates
3699 ValueError is raised if nothing is found, and TypeError if it evaluates
3689 to an object of another type. In each case, .args[0] is a printable
3700 to an object of another type. In each case, .args[0] is a printable
3690 message.
3701 message.
3691 """
3702 """
3692 code = self.extract_input_lines(target, raw=raw) # Grab history
3703 code = self.extract_input_lines(target, raw=raw) # Grab history
3693 if code:
3704 if code:
3694 return code
3705 return code
3695 try:
3706 try:
3696 if target.startswith(('http://', 'https://')):
3707 if target.startswith(('http://', 'https://')):
3697 return openpy.read_py_url(target, skip_encoding_cookie=skip_encoding_cookie)
3708 return openpy.read_py_url(target, skip_encoding_cookie=skip_encoding_cookie)
3698 except UnicodeDecodeError as e:
3709 except UnicodeDecodeError as e:
3699 if not py_only :
3710 if not py_only :
3700 # Deferred import
3711 # Deferred import
3701 from urllib.request import urlopen
3712 from urllib.request import urlopen
3702 response = urlopen(target)
3713 response = urlopen(target)
3703 return response.read().decode('latin1')
3714 return response.read().decode('latin1')
3704 raise ValueError(("'%s' seem to be unreadable.") % target) from e
3715 raise ValueError(("'%s' seem to be unreadable.") % target) from e
3705
3716
3706 potential_target = [target]
3717 potential_target = [target]
3707 try :
3718 try :
3708 potential_target.insert(0,get_py_filename(target))
3719 potential_target.insert(0,get_py_filename(target))
3709 except IOError:
3720 except IOError:
3710 pass
3721 pass
3711
3722
3712 for tgt in potential_target :
3723 for tgt in potential_target :
3713 if os.path.isfile(tgt): # Read file
3724 if os.path.isfile(tgt): # Read file
3714 try :
3725 try :
3715 return openpy.read_py_file(tgt, skip_encoding_cookie=skip_encoding_cookie)
3726 return openpy.read_py_file(tgt, skip_encoding_cookie=skip_encoding_cookie)
3716 except UnicodeDecodeError as e:
3727 except UnicodeDecodeError as e:
3717 if not py_only :
3728 if not py_only :
3718 with io_open(tgt,'r', encoding='latin1') as f :
3729 with io_open(tgt,'r', encoding='latin1') as f :
3719 return f.read()
3730 return f.read()
3720 raise ValueError(("'%s' seem to be unreadable.") % target) from e
3731 raise ValueError(("'%s' seem to be unreadable.") % target) from e
3721 elif os.path.isdir(os.path.expanduser(tgt)):
3732 elif os.path.isdir(os.path.expanduser(tgt)):
3722 raise ValueError("'%s' is a directory, not a regular file." % target)
3733 raise ValueError("'%s' is a directory, not a regular file." % target)
3723
3734
3724 if search_ns:
3735 if search_ns:
3725 # Inspect namespace to load object source
3736 # Inspect namespace to load object source
3726 object_info = self.object_inspect(target, detail_level=1)
3737 object_info = self.object_inspect(target, detail_level=1)
3727 if object_info['found'] and object_info['source']:
3738 if object_info['found'] and object_info['source']:
3728 return object_info['source']
3739 return object_info['source']
3729
3740
3730 try: # User namespace
3741 try: # User namespace
3731 codeobj = eval(target, self.user_ns)
3742 codeobj = eval(target, self.user_ns)
3732 except Exception as e:
3743 except Exception as e:
3733 raise ValueError(("'%s' was not found in history, as a file, url, "
3744 raise ValueError(("'%s' was not found in history, as a file, url, "
3734 "nor in the user namespace.") % target) from e
3745 "nor in the user namespace.") % target) from e
3735
3746
3736 if isinstance(codeobj, str):
3747 if isinstance(codeobj, str):
3737 return codeobj
3748 return codeobj
3738 elif isinstance(codeobj, Macro):
3749 elif isinstance(codeobj, Macro):
3739 return codeobj.value
3750 return codeobj.value
3740
3751
3741 raise TypeError("%s is neither a string nor a macro." % target,
3752 raise TypeError("%s is neither a string nor a macro." % target,
3742 codeobj)
3753 codeobj)
3743
3754
3744 def _atexit_once(self):
3755 def _atexit_once(self):
3745 """
3756 """
3746 At exist operation that need to be called at most once.
3757 At exist operation that need to be called at most once.
3747 Second call to this function per instance will do nothing.
3758 Second call to this function per instance will do nothing.
3748 """
3759 """
3749
3760
3750 if not getattr(self, "_atexit_once_called", False):
3761 if not getattr(self, "_atexit_once_called", False):
3751 self._atexit_once_called = True
3762 self._atexit_once_called = True
3752 # Clear all user namespaces to release all references cleanly.
3763 # Clear all user namespaces to release all references cleanly.
3753 self.reset(new_session=False)
3764 self.reset(new_session=False)
3754 # Close the history session (this stores the end time and line count)
3765 # Close the history session (this stores the end time and line count)
3755 # this must be *before* the tempfile cleanup, in case of temporary
3766 # this must be *before* the tempfile cleanup, in case of temporary
3756 # history db
3767 # history db
3757 self.history_manager.end_session()
3768 self.history_manager.end_session()
3758 self.history_manager = None
3769 self.history_manager = None
3759
3770
3760 #-------------------------------------------------------------------------
3771 #-------------------------------------------------------------------------
3761 # Things related to IPython exiting
3772 # Things related to IPython exiting
3762 #-------------------------------------------------------------------------
3773 #-------------------------------------------------------------------------
3763 def atexit_operations(self):
3774 def atexit_operations(self):
3764 """This will be executed at the time of exit.
3775 """This will be executed at the time of exit.
3765
3776
3766 Cleanup operations and saving of persistent data that is done
3777 Cleanup operations and saving of persistent data that is done
3767 unconditionally by IPython should be performed here.
3778 unconditionally by IPython should be performed here.
3768
3779
3769 For things that may depend on startup flags or platform specifics (such
3780 For things that may depend on startup flags or platform specifics (such
3770 as having readline or not), register a separate atexit function in the
3781 as having readline or not), register a separate atexit function in the
3771 code that has the appropriate information, rather than trying to
3782 code that has the appropriate information, rather than trying to
3772 clutter
3783 clutter
3773 """
3784 """
3774 self._atexit_once()
3785 self._atexit_once()
3775
3786
3776 # Cleanup all tempfiles and folders left around
3787 # Cleanup all tempfiles and folders left around
3777 for tfile in self.tempfiles:
3788 for tfile in self.tempfiles:
3778 try:
3789 try:
3779 tfile.unlink()
3790 tfile.unlink()
3780 self.tempfiles.remove(tfile)
3791 self.tempfiles.remove(tfile)
3781 except FileNotFoundError:
3792 except FileNotFoundError:
3782 pass
3793 pass
3783 del self.tempfiles
3794 del self.tempfiles
3784 for tdir in self.tempdirs:
3795 for tdir in self.tempdirs:
3785 try:
3796 try:
3786 tdir.rmdir()
3797 tdir.rmdir()
3787 self.tempdirs.remove(tdir)
3798 self.tempdirs.remove(tdir)
3788 except FileNotFoundError:
3799 except FileNotFoundError:
3789 pass
3800 pass
3790 del self.tempdirs
3801 del self.tempdirs
3791
3802
3792 # Restore user's cursor
3803 # Restore user's cursor
3793 if hasattr(self, "editing_mode") and self.editing_mode == "vi":
3804 if hasattr(self, "editing_mode") and self.editing_mode == "vi":
3794 sys.stdout.write("\x1b[0 q")
3805 sys.stdout.write("\x1b[0 q")
3795 sys.stdout.flush()
3806 sys.stdout.flush()
3796
3807
3797 def cleanup(self):
3808 def cleanup(self):
3798 self.restore_sys_module_state()
3809 self.restore_sys_module_state()
3799
3810
3800
3811
3801 # Overridden in terminal subclass to change prompts
3812 # Overridden in terminal subclass to change prompts
3802 def switch_doctest_mode(self, mode):
3813 def switch_doctest_mode(self, mode):
3803 pass
3814 pass
3804
3815
3805
3816
3806 class InteractiveShellABC(metaclass=abc.ABCMeta):
3817 class InteractiveShellABC(metaclass=abc.ABCMeta):
3807 """An abstract base class for InteractiveShell."""
3818 """An abstract base class for InteractiveShell."""
3808
3819
3809 InteractiveShellABC.register(InteractiveShell)
3820 InteractiveShellABC.register(InteractiveShell)
@@ -1,527 +1,540 b''
1 """Tests for the object inspection functionality.
1 """Tests for the object inspection functionality.
2 """
2 """
3
3
4 # Copyright (c) IPython Development Team.
4 # Copyright (c) IPython Development Team.
5 # Distributed under the terms of the Modified BSD License.
5 # Distributed under the terms of the Modified BSD License.
6
6
7
7
8 from contextlib import contextmanager
8 from contextlib import contextmanager
9 from inspect import signature, Signature, Parameter
9 from inspect import signature, Signature, Parameter
10 import inspect
10 import inspect
11 import os
11 import os
12 import pytest
12 import pytest
13 import re
13 import re
14 import sys
14 import sys
15
15
16 from .. import oinspect
16 from .. import oinspect
17
17
18 from decorator import decorator
18 from decorator import decorator
19
19
20 from IPython.testing.tools import AssertPrints, AssertNotPrints
20 from IPython.testing.tools import AssertPrints, AssertNotPrints
21 from IPython.utils.path import compress_user
21 from IPython.utils.path import compress_user
22
22
23
23
24 #-----------------------------------------------------------------------------
24 #-----------------------------------------------------------------------------
25 # Globals and constants
25 # Globals and constants
26 #-----------------------------------------------------------------------------
26 #-----------------------------------------------------------------------------
27
27
28 inspector = None
28 inspector = None
29
29
30 def setup_module():
30 def setup_module():
31 global inspector
31 global inspector
32 inspector = oinspect.Inspector()
32 inspector = oinspect.Inspector()
33
33
34
34
35 class SourceModuleMainTest:
35 class SourceModuleMainTest:
36 __module__ = "__main__"
36 __module__ = "__main__"
37
37
38
38
39 #-----------------------------------------------------------------------------
39 #-----------------------------------------------------------------------------
40 # Local utilities
40 # Local utilities
41 #-----------------------------------------------------------------------------
41 #-----------------------------------------------------------------------------
42
42
43 # WARNING: since this test checks the line number where a function is
43 # WARNING: since this test checks the line number where a function is
44 # defined, if any code is inserted above, the following line will need to be
44 # defined, if any code is inserted above, the following line will need to be
45 # updated. Do NOT insert any whitespace between the next line and the function
45 # updated. Do NOT insert any whitespace between the next line and the function
46 # definition below.
46 # definition below.
47 THIS_LINE_NUMBER = 47 # Put here the actual number of this line
47 THIS_LINE_NUMBER = 47 # Put here the actual number of this line
48
48
49
49
50 def test_find_source_lines():
50 def test_find_source_lines():
51 assert oinspect.find_source_lines(test_find_source_lines) == THIS_LINE_NUMBER + 3
51 assert oinspect.find_source_lines(test_find_source_lines) == THIS_LINE_NUMBER + 3
52 assert oinspect.find_source_lines(type) is None
52 assert oinspect.find_source_lines(type) is None
53 assert oinspect.find_source_lines(SourceModuleMainTest) is None
53 assert oinspect.find_source_lines(SourceModuleMainTest) is None
54 assert oinspect.find_source_lines(SourceModuleMainTest()) is None
54 assert oinspect.find_source_lines(SourceModuleMainTest()) is None
55
55
56
56
57 def test_getsource():
57 def test_getsource():
58 assert oinspect.getsource(type) is None
58 assert oinspect.getsource(type) is None
59 assert oinspect.getsource(SourceModuleMainTest) is None
59 assert oinspect.getsource(SourceModuleMainTest) is None
60 assert oinspect.getsource(SourceModuleMainTest()) is None
60 assert oinspect.getsource(SourceModuleMainTest()) is None
61
61
62
62
63 def test_inspect_getfile_raises_exception():
63 def test_inspect_getfile_raises_exception():
64 """Check oinspect.find_file/getsource/find_source_lines expectations"""
64 """Check oinspect.find_file/getsource/find_source_lines expectations"""
65 with pytest.raises(TypeError):
65 with pytest.raises(TypeError):
66 inspect.getfile(type)
66 inspect.getfile(type)
67 with pytest.raises(OSError if sys.version_info >= (3, 10) else TypeError):
67 with pytest.raises(OSError if sys.version_info >= (3, 10) else TypeError):
68 inspect.getfile(SourceModuleMainTest)
68 inspect.getfile(SourceModuleMainTest)
69
69
70
70
71 # A couple of utilities to ensure these tests work the same from a source or a
71 # A couple of utilities to ensure these tests work the same from a source or a
72 # binary install
72 # binary install
73 def pyfile(fname):
73 def pyfile(fname):
74 return os.path.normcase(re.sub('.py[co]$', '.py', fname))
74 return os.path.normcase(re.sub('.py[co]$', '.py', fname))
75
75
76
76
77 def match_pyfiles(f1, f2):
77 def match_pyfiles(f1, f2):
78 assert pyfile(f1) == pyfile(f2)
78 assert pyfile(f1) == pyfile(f2)
79
79
80
80
81 def test_find_file():
81 def test_find_file():
82 match_pyfiles(oinspect.find_file(test_find_file), os.path.abspath(__file__))
82 match_pyfiles(oinspect.find_file(test_find_file), os.path.abspath(__file__))
83 assert oinspect.find_file(type) is None
83 assert oinspect.find_file(type) is None
84 assert oinspect.find_file(SourceModuleMainTest) is None
84 assert oinspect.find_file(SourceModuleMainTest) is None
85 assert oinspect.find_file(SourceModuleMainTest()) is None
85 assert oinspect.find_file(SourceModuleMainTest()) is None
86
86
87
87
88 def test_find_file_decorated1():
88 def test_find_file_decorated1():
89
89
90 @decorator
90 @decorator
91 def noop1(f):
91 def noop1(f):
92 def wrapper(*a, **kw):
92 def wrapper(*a, **kw):
93 return f(*a, **kw)
93 return f(*a, **kw)
94 return wrapper
94 return wrapper
95
95
96 @noop1
96 @noop1
97 def f(x):
97 def f(x):
98 "My docstring"
98 "My docstring"
99
99
100 match_pyfiles(oinspect.find_file(f), os.path.abspath(__file__))
100 match_pyfiles(oinspect.find_file(f), os.path.abspath(__file__))
101 assert f.__doc__ == "My docstring"
101 assert f.__doc__ == "My docstring"
102
102
103
103
104 def test_find_file_decorated2():
104 def test_find_file_decorated2():
105
105
106 @decorator
106 @decorator
107 def noop2(f, *a, **kw):
107 def noop2(f, *a, **kw):
108 return f(*a, **kw)
108 return f(*a, **kw)
109
109
110 @noop2
110 @noop2
111 @noop2
111 @noop2
112 @noop2
112 @noop2
113 def f(x):
113 def f(x):
114 "My docstring 2"
114 "My docstring 2"
115
115
116 match_pyfiles(oinspect.find_file(f), os.path.abspath(__file__))
116 match_pyfiles(oinspect.find_file(f), os.path.abspath(__file__))
117 assert f.__doc__ == "My docstring 2"
117 assert f.__doc__ == "My docstring 2"
118
118
119
119
120 def test_find_file_magic():
120 def test_find_file_magic():
121 run = ip.find_line_magic('run')
121 run = ip.find_line_magic('run')
122 assert oinspect.find_file(run) is not None
122 assert oinspect.find_file(run) is not None
123
123
124
124
125 # A few generic objects we can then inspect in the tests below
125 # A few generic objects we can then inspect in the tests below
126
126
127 class Call(object):
127 class Call(object):
128 """This is the class docstring."""
128 """This is the class docstring."""
129
129
130 def __init__(self, x, y=1):
130 def __init__(self, x, y=1):
131 """This is the constructor docstring."""
131 """This is the constructor docstring."""
132
132
133 def __call__(self, *a, **kw):
133 def __call__(self, *a, **kw):
134 """This is the call docstring."""
134 """This is the call docstring."""
135
135
136 def method(self, x, z=2):
136 def method(self, x, z=2):
137 """Some method's docstring"""
137 """Some method's docstring"""
138
138
139 class HasSignature(object):
139 class HasSignature(object):
140 """This is the class docstring."""
140 """This is the class docstring."""
141 __signature__ = Signature([Parameter('test', Parameter.POSITIONAL_OR_KEYWORD)])
141 __signature__ = Signature([Parameter('test', Parameter.POSITIONAL_OR_KEYWORD)])
142
142
143 def __init__(self, *args):
143 def __init__(self, *args):
144 """This is the init docstring"""
144 """This is the init docstring"""
145
145
146
146
147 class SimpleClass(object):
147 class SimpleClass(object):
148 def method(self, x, z=2):
148 def method(self, x, z=2):
149 """Some method's docstring"""
149 """Some method's docstring"""
150
150
151
151
152 class Awkward(object):
152 class Awkward(object):
153 def __getattr__(self, name):
153 def __getattr__(self, name):
154 raise Exception(name)
154 raise Exception(name)
155
155
156 class NoBoolCall:
156 class NoBoolCall:
157 """
157 """
158 callable with `__bool__` raising should still be inspect-able.
158 callable with `__bool__` raising should still be inspect-able.
159 """
159 """
160
160
161 def __call__(self):
161 def __call__(self):
162 """does nothing"""
162 """does nothing"""
163 pass
163 pass
164
164
165 def __bool__(self):
165 def __bool__(self):
166 """just raise NotImplemented"""
166 """just raise NotImplemented"""
167 raise NotImplementedError('Must be implemented')
167 raise NotImplementedError('Must be implemented')
168
168
169
169
170 class SerialLiar(object):
170 class SerialLiar(object):
171 """Attribute accesses always get another copy of the same class.
171 """Attribute accesses always get another copy of the same class.
172
172
173 unittest.mock.call does something similar, but it's not ideal for testing
173 unittest.mock.call does something similar, but it's not ideal for testing
174 as the failure mode is to eat all your RAM. This gives up after 10k levels.
174 as the failure mode is to eat all your RAM. This gives up after 10k levels.
175 """
175 """
176 def __init__(self, max_fibbing_twig, lies_told=0):
176 def __init__(self, max_fibbing_twig, lies_told=0):
177 if lies_told > 10000:
177 if lies_told > 10000:
178 raise RuntimeError('Nose too long, honesty is the best policy')
178 raise RuntimeError('Nose too long, honesty is the best policy')
179 self.max_fibbing_twig = max_fibbing_twig
179 self.max_fibbing_twig = max_fibbing_twig
180 self.lies_told = lies_told
180 self.lies_told = lies_told
181 max_fibbing_twig[0] = max(max_fibbing_twig[0], lies_told)
181 max_fibbing_twig[0] = max(max_fibbing_twig[0], lies_told)
182
182
183 def __getattr__(self, item):
183 def __getattr__(self, item):
184 return SerialLiar(self.max_fibbing_twig, self.lies_told + 1)
184 return SerialLiar(self.max_fibbing_twig, self.lies_told + 1)
185
185
186 #-----------------------------------------------------------------------------
186 #-----------------------------------------------------------------------------
187 # Tests
187 # Tests
188 #-----------------------------------------------------------------------------
188 #-----------------------------------------------------------------------------
189
189
190 def test_info():
190 def test_info():
191 "Check that Inspector.info fills out various fields as expected."
191 "Check that Inspector.info fills out various fields as expected."
192 i = inspector.info(Call, oname="Call")
192 i = inspector.info(Call, oname="Call")
193 assert i["type_name"] == "type"
193 assert i["type_name"] == "type"
194 expected_class = str(type(type)) # <class 'type'> (Python 3) or <type 'type'>
194 expected_class = str(type(type)) # <class 'type'> (Python 3) or <type 'type'>
195 assert i["base_class"] == expected_class
195 assert i["base_class"] == expected_class
196 assert re.search(
196 assert re.search(
197 "<class 'IPython.core.tests.test_oinspect.Call'( at 0x[0-9a-f]{1,9})?>",
197 "<class 'IPython.core.tests.test_oinspect.Call'( at 0x[0-9a-f]{1,9})?>",
198 i["string_form"],
198 i["string_form"],
199 )
199 )
200 fname = __file__
200 fname = __file__
201 if fname.endswith(".pyc"):
201 if fname.endswith(".pyc"):
202 fname = fname[:-1]
202 fname = fname[:-1]
203 # case-insensitive comparison needed on some filesystems
203 # case-insensitive comparison needed on some filesystems
204 # e.g. Windows:
204 # e.g. Windows:
205 assert i["file"].lower() == compress_user(fname).lower()
205 assert i["file"].lower() == compress_user(fname).lower()
206 assert i["definition"] == None
206 assert i["definition"] == None
207 assert i["docstring"] == Call.__doc__
207 assert i["docstring"] == Call.__doc__
208 assert i["source"] == None
208 assert i["source"] == None
209 assert i["isclass"] is True
209 assert i["isclass"] is True
210 assert i["init_definition"] == "Call(x, y=1)"
210 assert i["init_definition"] == "Call(x, y=1)"
211 assert i["init_docstring"] == Call.__init__.__doc__
211 assert i["init_docstring"] == Call.__init__.__doc__
212
212
213 i = inspector.info(Call, detail_level=1)
213 i = inspector.info(Call, detail_level=1)
214 assert i["source"] is not None
214 assert i["source"] is not None
215 assert i["docstring"] == None
215 assert i["docstring"] == None
216
216
217 c = Call(1)
217 c = Call(1)
218 c.__doc__ = "Modified instance docstring"
218 c.__doc__ = "Modified instance docstring"
219 i = inspector.info(c)
219 i = inspector.info(c)
220 assert i["type_name"] == "Call"
220 assert i["type_name"] == "Call"
221 assert i["docstring"] == "Modified instance docstring"
221 assert i["docstring"] == "Modified instance docstring"
222 assert i["class_docstring"] == Call.__doc__
222 assert i["class_docstring"] == Call.__doc__
223 assert i["init_docstring"] == Call.__init__.__doc__
223 assert i["init_docstring"] == Call.__init__.__doc__
224 assert i["call_docstring"] == Call.__call__.__doc__
224 assert i["call_docstring"] == Call.__call__.__doc__
225
225
226
226
227 def test_class_signature():
227 def test_class_signature():
228 info = inspector.info(HasSignature, "HasSignature")
228 info = inspector.info(HasSignature, "HasSignature")
229 assert info["init_definition"] == "HasSignature(test)"
229 assert info["init_definition"] == "HasSignature(test)"
230 assert info["init_docstring"] == HasSignature.__init__.__doc__
230 assert info["init_docstring"] == HasSignature.__init__.__doc__
231
231
232
232
233 def test_info_awkward():
233 def test_info_awkward():
234 # Just test that this doesn't throw an error.
234 # Just test that this doesn't throw an error.
235 inspector.info(Awkward())
235 inspector.info(Awkward())
236
236
237 def test_bool_raise():
237 def test_bool_raise():
238 inspector.info(NoBoolCall())
238 inspector.info(NoBoolCall())
239
239
240 def test_info_serialliar():
240 def test_info_serialliar():
241 fib_tracker = [0]
241 fib_tracker = [0]
242 inspector.info(SerialLiar(fib_tracker))
242 inspector.info(SerialLiar(fib_tracker))
243
243
244 # Nested attribute access should be cut off at 100 levels deep to avoid
244 # Nested attribute access should be cut off at 100 levels deep to avoid
245 # infinite loops: https://github.com/ipython/ipython/issues/9122
245 # infinite loops: https://github.com/ipython/ipython/issues/9122
246 assert fib_tracker[0] < 9000
246 assert fib_tracker[0] < 9000
247
247
248 def support_function_one(x, y=2, *a, **kw):
248 def support_function_one(x, y=2, *a, **kw):
249 """A simple function."""
249 """A simple function."""
250
250
251 def test_calldef_none():
251 def test_calldef_none():
252 # We should ignore __call__ for all of these.
252 # We should ignore __call__ for all of these.
253 for obj in [support_function_one, SimpleClass().method, any, str.upper]:
253 for obj in [support_function_one, SimpleClass().method, any, str.upper]:
254 i = inspector.info(obj)
254 i = inspector.info(obj)
255 assert i["call_def"] is None
255 assert i["call_def"] is None
256
256
257
257
258 def f_kwarg(pos, *, kwonly):
258 def f_kwarg(pos, *, kwonly):
259 pass
259 pass
260
260
261 def test_definition_kwonlyargs():
261 def test_definition_kwonlyargs():
262 i = inspector.info(f_kwarg, oname="f_kwarg") # analysis:ignore
262 i = inspector.info(f_kwarg, oname="f_kwarg") # analysis:ignore
263 assert i["definition"] == "f_kwarg(pos, *, kwonly)"
263 assert i["definition"] == "f_kwarg(pos, *, kwonly)"
264
264
265
265
266 def test_getdoc():
266 def test_getdoc():
267 class A(object):
267 class A(object):
268 """standard docstring"""
268 """standard docstring"""
269 pass
269 pass
270
270
271 class B(object):
271 class B(object):
272 """standard docstring"""
272 """standard docstring"""
273 def getdoc(self):
273 def getdoc(self):
274 return "custom docstring"
274 return "custom docstring"
275
275
276 class C(object):
276 class C(object):
277 """standard docstring"""
277 """standard docstring"""
278 def getdoc(self):
278 def getdoc(self):
279 return None
279 return None
280
280
281 a = A()
281 a = A()
282 b = B()
282 b = B()
283 c = C()
283 c = C()
284
284
285 assert oinspect.getdoc(a) == "standard docstring"
285 assert oinspect.getdoc(a) == "standard docstring"
286 assert oinspect.getdoc(b) == "custom docstring"
286 assert oinspect.getdoc(b) == "custom docstring"
287 assert oinspect.getdoc(c) == "standard docstring"
287 assert oinspect.getdoc(c) == "standard docstring"
288
288
289
289
290 def test_empty_property_has_no_source():
290 def test_empty_property_has_no_source():
291 i = inspector.info(property(), detail_level=1)
291 i = inspector.info(property(), detail_level=1)
292 assert i["source"] is None
292 assert i["source"] is None
293
293
294
294
295 def test_property_sources():
295 def test_property_sources():
296 # A simple adder whose source and signature stays
296 # A simple adder whose source and signature stays
297 # the same across Python distributions
297 # the same across Python distributions
298 def simple_add(a, b):
298 def simple_add(a, b):
299 "Adds two numbers"
299 "Adds two numbers"
300 return a + b
300 return a + b
301
301
302 class A(object):
302 class A(object):
303 @property
303 @property
304 def foo(self):
304 def foo(self):
305 return 'bar'
305 return 'bar'
306
306
307 foo = foo.setter(lambda self, v: setattr(self, 'bar', v))
307 foo = foo.setter(lambda self, v: setattr(self, 'bar', v))
308
308
309 dname = property(oinspect.getdoc)
309 dname = property(oinspect.getdoc)
310 adder = property(simple_add)
310 adder = property(simple_add)
311
311
312 i = inspector.info(A.foo, detail_level=1)
312 i = inspector.info(A.foo, detail_level=1)
313 assert "def foo(self):" in i["source"]
313 assert "def foo(self):" in i["source"]
314 assert "lambda self, v:" in i["source"]
314 assert "lambda self, v:" in i["source"]
315
315
316 i = inspector.info(A.dname, detail_level=1)
316 i = inspector.info(A.dname, detail_level=1)
317 assert "def getdoc(obj)" in i["source"]
317 assert "def getdoc(obj)" in i["source"]
318
318
319 i = inspector.info(A.adder, detail_level=1)
319 i = inspector.info(A.adder, detail_level=1)
320 assert "def simple_add(a, b)" in i["source"]
320 assert "def simple_add(a, b)" in i["source"]
321
321
322
322
323 def test_property_docstring_is_in_info_for_detail_level_0():
323 def test_property_docstring_is_in_info_for_detail_level_0():
324 class A(object):
324 class A(object):
325 @property
325 @property
326 def foobar(self):
326 def foobar(self):
327 """This is `foobar` property."""
327 """This is `foobar` property."""
328 pass
328 pass
329
329
330 ip.user_ns["a_obj"] = A()
330 ip.user_ns["a_obj"] = A()
331 assert (
331 assert (
332 "This is `foobar` property."
332 "This is `foobar` property."
333 == ip.object_inspect("a_obj.foobar", detail_level=0)["docstring"]
333 == ip.object_inspect("a_obj.foobar", detail_level=0)["docstring"]
334 )
334 )
335
335
336 ip.user_ns["a_cls"] = A
336 ip.user_ns["a_cls"] = A
337 assert (
337 assert (
338 "This is `foobar` property."
338 "This is `foobar` property."
339 == ip.object_inspect("a_cls.foobar", detail_level=0)["docstring"]
339 == ip.object_inspect("a_cls.foobar", detail_level=0)["docstring"]
340 )
340 )
341
341
342
342
343 def test_pdef():
343 def test_pdef():
344 # See gh-1914
344 # See gh-1914
345 def foo(): pass
345 def foo(): pass
346 inspector.pdef(foo, 'foo')
346 inspector.pdef(foo, 'foo')
347
347
348
348
349 @contextmanager
349 @contextmanager
350 def cleanup_user_ns(**kwargs):
350 def cleanup_user_ns(**kwargs):
351 """
351 """
352 On exit delete all the keys that were not in user_ns before entering.
352 On exit delete all the keys that were not in user_ns before entering.
353
353
354 It does not restore old values !
354 It does not restore old values !
355
355
356 Parameters
356 Parameters
357 ----------
357 ----------
358
358
359 **kwargs
359 **kwargs
360 used to update ip.user_ns
360 used to update ip.user_ns
361
361
362 """
362 """
363 try:
363 try:
364 known = set(ip.user_ns.keys())
364 known = set(ip.user_ns.keys())
365 ip.user_ns.update(kwargs)
365 ip.user_ns.update(kwargs)
366 yield
366 yield
367 finally:
367 finally:
368 added = set(ip.user_ns.keys()) - known
368 added = set(ip.user_ns.keys()) - known
369 for k in added:
369 for k in added:
370 del ip.user_ns[k]
370 del ip.user_ns[k]
371
371
372
372
373 def test_pinfo_getindex():
373 def test_pinfo_getindex():
374 def dummy():
374 def dummy():
375 """
375 """
376 MARKER
376 MARKER
377 """
377 """
378
378
379 container = [dummy]
379 container = [dummy]
380 with cleanup_user_ns(container=container):
380 with cleanup_user_ns(container=container):
381 with AssertPrints("MARKER"):
381 with AssertPrints("MARKER"):
382 ip._inspect("pinfo", "container[0]", detail_level=0)
382 ip._inspect("pinfo", "container[0]", detail_level=0)
383 assert "container" not in ip.user_ns.keys()
383 assert "container" not in ip.user_ns.keys()
384
384
385
385
386 def test_qmark_getindex():
386 def test_qmark_getindex():
387 def dummy():
387 def dummy():
388 """
388 """
389 MARKER 2
389 MARKER 2
390 """
390 """
391
391
392 container = [dummy]
392 container = [dummy]
393 with cleanup_user_ns(container=container):
393 with cleanup_user_ns(container=container):
394 with AssertPrints("MARKER 2"):
394 with AssertPrints("MARKER 2"):
395 ip.run_cell("container[0]?")
395 ip.run_cell("container[0]?")
396 assert "container" not in ip.user_ns.keys()
396 assert "container" not in ip.user_ns.keys()
397
397
398 def test_qmark_getindex_negatif():
399 def dummy():
400 """
401 MARKER 3
402 """
403
404 container = [dummy]
405 with cleanup_user_ns(container=container):
406 with AssertPrints("MARKER 3"):
407 ip.run_cell("container[-1]?")
408 assert "container" not in ip.user_ns.keys()
409
410
398
411
399 def test_pinfo_nonascii():
412 def test_pinfo_nonascii():
400 # See gh-1177
413 # See gh-1177
401 from . import nonascii2
414 from . import nonascii2
402 ip.user_ns['nonascii2'] = nonascii2
415 ip.user_ns['nonascii2'] = nonascii2
403 ip._inspect('pinfo', 'nonascii2', detail_level=1)
416 ip._inspect('pinfo', 'nonascii2', detail_level=1)
404
417
405 def test_pinfo_type():
418 def test_pinfo_type():
406 """
419 """
407 type can fail in various edge case, for example `type.__subclass__()`
420 type can fail in various edge case, for example `type.__subclass__()`
408 """
421 """
409 ip._inspect('pinfo', 'type')
422 ip._inspect('pinfo', 'type')
410
423
411
424
412 def test_pinfo_docstring_no_source():
425 def test_pinfo_docstring_no_source():
413 """Docstring should be included with detail_level=1 if there is no source"""
426 """Docstring should be included with detail_level=1 if there is no source"""
414 with AssertPrints('Docstring:'):
427 with AssertPrints('Docstring:'):
415 ip._inspect('pinfo', 'str.format', detail_level=0)
428 ip._inspect('pinfo', 'str.format', detail_level=0)
416 with AssertPrints('Docstring:'):
429 with AssertPrints('Docstring:'):
417 ip._inspect('pinfo', 'str.format', detail_level=1)
430 ip._inspect('pinfo', 'str.format', detail_level=1)
418
431
419
432
420 def test_pinfo_no_docstring_if_source():
433 def test_pinfo_no_docstring_if_source():
421 """Docstring should not be included with detail_level=1 if source is found"""
434 """Docstring should not be included with detail_level=1 if source is found"""
422 def foo():
435 def foo():
423 """foo has a docstring"""
436 """foo has a docstring"""
424
437
425 ip.user_ns['foo'] = foo
438 ip.user_ns['foo'] = foo
426
439
427 with AssertPrints('Docstring:'):
440 with AssertPrints('Docstring:'):
428 ip._inspect('pinfo', 'foo', detail_level=0)
441 ip._inspect('pinfo', 'foo', detail_level=0)
429 with AssertPrints('Source:'):
442 with AssertPrints('Source:'):
430 ip._inspect('pinfo', 'foo', detail_level=1)
443 ip._inspect('pinfo', 'foo', detail_level=1)
431 with AssertNotPrints('Docstring:'):
444 with AssertNotPrints('Docstring:'):
432 ip._inspect('pinfo', 'foo', detail_level=1)
445 ip._inspect('pinfo', 'foo', detail_level=1)
433
446
434
447
435 def test_pinfo_docstring_if_detail_and_no_source():
448 def test_pinfo_docstring_if_detail_and_no_source():
436 """ Docstring should be displayed if source info not available """
449 """ Docstring should be displayed if source info not available """
437 obj_def = '''class Foo(object):
450 obj_def = '''class Foo(object):
438 """ This is a docstring for Foo """
451 """ This is a docstring for Foo """
439 def bar(self):
452 def bar(self):
440 """ This is a docstring for Foo.bar """
453 """ This is a docstring for Foo.bar """
441 pass
454 pass
442 '''
455 '''
443
456
444 ip.run_cell(obj_def)
457 ip.run_cell(obj_def)
445 ip.run_cell('foo = Foo()')
458 ip.run_cell('foo = Foo()')
446
459
447 with AssertNotPrints("Source:"):
460 with AssertNotPrints("Source:"):
448 with AssertPrints('Docstring:'):
461 with AssertPrints('Docstring:'):
449 ip._inspect('pinfo', 'foo', detail_level=0)
462 ip._inspect('pinfo', 'foo', detail_level=0)
450 with AssertPrints('Docstring:'):
463 with AssertPrints('Docstring:'):
451 ip._inspect('pinfo', 'foo', detail_level=1)
464 ip._inspect('pinfo', 'foo', detail_level=1)
452 with AssertPrints('Docstring:'):
465 with AssertPrints('Docstring:'):
453 ip._inspect('pinfo', 'foo.bar', detail_level=0)
466 ip._inspect('pinfo', 'foo.bar', detail_level=0)
454
467
455 with AssertNotPrints('Docstring:'):
468 with AssertNotPrints('Docstring:'):
456 with AssertPrints('Source:'):
469 with AssertPrints('Source:'):
457 ip._inspect('pinfo', 'foo.bar', detail_level=1)
470 ip._inspect('pinfo', 'foo.bar', detail_level=1)
458
471
459
472
460 def test_pinfo_magic():
473 def test_pinfo_magic():
461 with AssertPrints('Docstring:'):
474 with AssertPrints('Docstring:'):
462 ip._inspect('pinfo', 'lsmagic', detail_level=0)
475 ip._inspect('pinfo', 'lsmagic', detail_level=0)
463
476
464 with AssertPrints('Source:'):
477 with AssertPrints('Source:'):
465 ip._inspect('pinfo', 'lsmagic', detail_level=1)
478 ip._inspect('pinfo', 'lsmagic', detail_level=1)
466
479
467
480
468 def test_init_colors():
481 def test_init_colors():
469 # ensure colors are not present in signature info
482 # ensure colors are not present in signature info
470 info = inspector.info(HasSignature)
483 info = inspector.info(HasSignature)
471 init_def = info["init_definition"]
484 init_def = info["init_definition"]
472 assert "[0m" not in init_def
485 assert "[0m" not in init_def
473
486
474
487
475 def test_builtin_init():
488 def test_builtin_init():
476 info = inspector.info(list)
489 info = inspector.info(list)
477 init_def = info['init_definition']
490 init_def = info['init_definition']
478 assert init_def is not None
491 assert init_def is not None
479
492
480
493
481 def test_render_signature_short():
494 def test_render_signature_short():
482 def short_fun(a=1): pass
495 def short_fun(a=1): pass
483 sig = oinspect._render_signature(
496 sig = oinspect._render_signature(
484 signature(short_fun),
497 signature(short_fun),
485 short_fun.__name__,
498 short_fun.__name__,
486 )
499 )
487 assert sig == "short_fun(a=1)"
500 assert sig == "short_fun(a=1)"
488
501
489
502
490 def test_render_signature_long():
503 def test_render_signature_long():
491 from typing import Optional
504 from typing import Optional
492
505
493 def long_function(
506 def long_function(
494 a_really_long_parameter: int,
507 a_really_long_parameter: int,
495 and_another_long_one: bool = False,
508 and_another_long_one: bool = False,
496 let_us_make_sure_this_is_looong: Optional[str] = None,
509 let_us_make_sure_this_is_looong: Optional[str] = None,
497 ) -> bool: pass
510 ) -> bool: pass
498
511
499 sig = oinspect._render_signature(
512 sig = oinspect._render_signature(
500 signature(long_function),
513 signature(long_function),
501 long_function.__name__,
514 long_function.__name__,
502 )
515 )
503 assert sig in [
516 assert sig in [
504 # Python >=3.9
517 # Python >=3.9
505 '''\
518 '''\
506 long_function(
519 long_function(
507 a_really_long_parameter: int,
520 a_really_long_parameter: int,
508 and_another_long_one: bool = False,
521 and_another_long_one: bool = False,
509 let_us_make_sure_this_is_looong: Optional[str] = None,
522 let_us_make_sure_this_is_looong: Optional[str] = None,
510 ) -> bool\
523 ) -> bool\
511 ''',
524 ''',
512 # Python >=3.7
525 # Python >=3.7
513 '''\
526 '''\
514 long_function(
527 long_function(
515 a_really_long_parameter: int,
528 a_really_long_parameter: int,
516 and_another_long_one: bool = False,
529 and_another_long_one: bool = False,
517 let_us_make_sure_this_is_looong: Union[str, NoneType] = None,
530 let_us_make_sure_this_is_looong: Union[str, NoneType] = None,
518 ) -> bool\
531 ) -> bool\
519 ''', # Python <=3.6
532 ''', # Python <=3.6
520 '''\
533 '''\
521 long_function(
534 long_function(
522 a_really_long_parameter:int,
535 a_really_long_parameter:int,
523 and_another_long_one:bool=False,
536 and_another_long_one:bool=False,
524 let_us_make_sure_this_is_looong:Union[str, NoneType]=None,
537 let_us_make_sure_this_is_looong:Union[str, NoneType]=None,
525 ) -> bool\
538 ) -> bool\
526 ''',
539 ''',
527 ]
540 ]
General Comments 0
You need to be logged in to leave comments. Login now