##// END OF EJS Templates
Calculate indentation based on tokens, not regexes...
Thomas Kluyver -
Show More
@@ -1,679 +1,787 b''
1 """Input handling and transformation machinery.
1 """Input handling and transformation machinery.
2
2
3 The first class in this module, :class:`InputSplitter`, is designed to tell when
3 The first class in this module, :class:`InputSplitter`, is designed to tell when
4 input from a line-oriented frontend is complete and should be executed, and when
4 input from a line-oriented frontend is complete and should be executed, and when
5 the user should be prompted for another line of code instead. The name 'input
5 the user should be prompted for another line of code instead. The name 'input
6 splitter' is largely for historical reasons.
6 splitter' is largely for historical reasons.
7
7
8 A companion, :class:`IPythonInputSplitter`, provides the same functionality but
8 A companion, :class:`IPythonInputSplitter`, provides the same functionality but
9 with full support for the extended IPython syntax (magics, system calls, etc).
9 with full support for the extended IPython syntax (magics, system calls, etc).
10 The code to actually do these transformations is in :mod:`IPython.core.inputtransformer`.
10 The code to actually do these transformations is in :mod:`IPython.core.inputtransformer`.
11 :class:`IPythonInputSplitter` feeds the raw code to the transformers in order
11 :class:`IPythonInputSplitter` feeds the raw code to the transformers in order
12 and stores the results.
12 and stores the results.
13
13
14 For more details, see the class docstrings below.
14 For more details, see the class docstrings below.
15 """
15 """
16
16
17 # Copyright (c) IPython Development Team.
17 # Copyright (c) IPython Development Team.
18 # Distributed under the terms of the Modified BSD License.
18 # Distributed under the terms of the Modified BSD License.
19 import ast
19 import ast
20 import codeop
20 import codeop
21 import io
21 import re
22 import re
22 import sys
23 import sys
24 import tokenize
23 import warnings
25 import warnings
24
26
25 from IPython.utils.py3compat import cast_unicode
27 from IPython.utils.py3compat import cast_unicode
26 from IPython.core.inputtransformer import (leading_indent,
28 from IPython.core.inputtransformer import (leading_indent,
27 classic_prompt,
29 classic_prompt,
28 ipy_prompt,
30 ipy_prompt,
29 cellmagic,
31 cellmagic,
30 assemble_logical_lines,
32 assemble_logical_lines,
31 help_end,
33 help_end,
32 escaped_commands,
34 escaped_commands,
33 assign_from_magic,
35 assign_from_magic,
34 assign_from_system,
36 assign_from_system,
35 assemble_python_lines,
37 assemble_python_lines,
36 )
38 )
37
39
38 # These are available in this module for backwards compatibility.
40 # These are available in this module for backwards compatibility.
39 from IPython.core.inputtransformer import (ESC_SHELL, ESC_SH_CAP, ESC_HELP,
41 from IPython.core.inputtransformer import (ESC_SHELL, ESC_SH_CAP, ESC_HELP,
40 ESC_HELP2, ESC_MAGIC, ESC_MAGIC2,
42 ESC_HELP2, ESC_MAGIC, ESC_MAGIC2,
41 ESC_QUOTE, ESC_QUOTE2, ESC_PAREN, ESC_SEQUENCES)
43 ESC_QUOTE, ESC_QUOTE2, ESC_PAREN, ESC_SEQUENCES)
42
44
43 #-----------------------------------------------------------------------------
45 #-----------------------------------------------------------------------------
44 # Utilities
46 # Utilities
45 #-----------------------------------------------------------------------------
47 #-----------------------------------------------------------------------------
46
48
47 # FIXME: These are general-purpose utilities that later can be moved to the
49 # FIXME: These are general-purpose utilities that later can be moved to the
48 # general ward. Kept here for now because we're being very strict about test
50 # general ward. Kept here for now because we're being very strict about test
49 # coverage with this code, and this lets us ensure that we keep 100% coverage
51 # coverage with this code, and this lets us ensure that we keep 100% coverage
50 # while developing.
52 # while developing.
51
53
52 # compiled regexps for autoindent management
54 # compiled regexps for autoindent management
53 dedent_re = re.compile('|'.join([
55 dedent_re = re.compile('|'.join([
54 r'^\s+raise(\s.*)?$', # raise statement (+ space + other stuff, maybe)
56 r'^\s+raise(\s.*)?$', # raise statement (+ space + other stuff, maybe)
55 r'^\s+raise\([^\)]*\).*$', # wacky raise with immediate open paren
57 r'^\s+raise\([^\)]*\).*$', # wacky raise with immediate open paren
56 r'^\s+return(\s.*)?$', # normal return (+ space + other stuff, maybe)
58 r'^\s+return(\s.*)?$', # normal return (+ space + other stuff, maybe)
57 r'^\s+return\([^\)]*\).*$', # wacky return with immediate open paren
59 r'^\s+return\([^\)]*\).*$', # wacky return with immediate open paren
58 r'^\s+pass\s*$', # pass (optionally followed by trailing spaces)
60 r'^\s+pass\s*$', # pass (optionally followed by trailing spaces)
59 r'^\s+break\s*$', # break (optionally followed by trailing spaces)
61 r'^\s+break\s*$', # break (optionally followed by trailing spaces)
60 r'^\s+continue\s*$', # continue (optionally followed by trailing spaces)
62 r'^\s+continue\s*$', # continue (optionally followed by trailing spaces)
61 ]))
63 ]))
62 ini_spaces_re = re.compile(r'^([ \t\r\f\v]+)')
64 ini_spaces_re = re.compile(r'^([ \t\r\f\v]+)')
63
65
64 # regexp to match pure comment lines so we don't accidentally insert 'if 1:'
66 # regexp to match pure comment lines so we don't accidentally insert 'if 1:'
65 # before pure comments
67 # before pure comments
66 comment_line_re = re.compile('^\s*\#')
68 comment_line_re = re.compile('^\s*\#')
67
69
68
70
69 def num_ini_spaces(s):
71 def num_ini_spaces(s):
70 """Return the number of initial spaces in a string.
72 """Return the number of initial spaces in a string.
71
73
72 Note that tabs are counted as a single space. For now, we do *not* support
74 Note that tabs are counted as a single space. For now, we do *not* support
73 mixing of tabs and spaces in the user's input.
75 mixing of tabs and spaces in the user's input.
74
76
75 Parameters
77 Parameters
76 ----------
78 ----------
77 s : string
79 s : string
78
80
79 Returns
81 Returns
80 -------
82 -------
81 n : int
83 n : int
82 """
84 """
83
85
84 ini_spaces = ini_spaces_re.match(s)
86 ini_spaces = ini_spaces_re.match(s)
85 if ini_spaces:
87 if ini_spaces:
86 return ini_spaces.end()
88 return ini_spaces.end()
87 else:
89 else:
88 return 0
90 return 0
89
91
92 # Fake token types for partial_tokenize:
93 INCOMPLETE_STRING = tokenize.N_TOKENS
94 IN_MULTILINE_STATEMENT = tokenize.N_TOKENS + 1
95
96 # The 2 classes below have the same API as TokenInfo, but don't try to look up
97 # a token type name that they won't find.
98 class IncompleteString:
99 type = exact_type = INCOMPLETE_STRING
100 def __init__(self, s, start, end, line):
101 self.s = s
102 self.start = start
103 self.end = end
104 self.line = line
105
106 class InMultilineStatement:
107 type = exact_type = IN_MULTILINE_STATEMENT
108 def __init__(self, pos, line):
109 self.s = ''
110 self.start = self.end = pos
111 self.line = line
112
113 def partial_tokens(s):
114 """Iterate over tokens from a possibly-incomplete string of code.
115
116 This adds two special token types: INCOMPLETE_STRING and
117 IN_MULTILINE_STATEMENT. These can only occur as the last token yielded, and
118 represent the two main ways for code to be incomplete.
119 """
120 readline = io.StringIO(s).readline
121 token = tokenize.TokenInfo(tokenize.NEWLINE, '', (1, 0), (1, 0), '')
122 try:
123 for token in tokenize.generate_tokens(readline):
124 yield token
125 except tokenize.TokenError as e:
126 # catch EOF error
127 lines = s.splitlines(keepends=True)
128 end = len(lines), len(lines[-1])
129 if 'multi-line string' in e.args[0]:
130 l, c = start = token.end
131 s = lines[l-1][c:] + ''.join(lines[l:])
132 yield IncompleteString(s, start, end, lines[-1])
133 elif 'multi-line statement' in e.args[0]:
134 yield InMultilineStatement(end, lines[-1])
135 else:
136 raise
137
138 def find_next_indent(code):
139 """Find the number of spaces for the next line of indentation"""
140 tokens = list(partial_tokens(code))
141 if tokens[-1].type == tokenize.ENDMARKER:
142 tokens.pop()
143 if not tokens:
144 return 0
145 if tokens[-1].type in {tokenize.DEDENT, tokenize.NEWLINE, tokenize.COMMENT}:
146 tokens.pop()
147
148 if tokens[-1].type == INCOMPLETE_STRING:
149 # Inside a multiline string
150 return 0
151
152 # Find the indents used before
153 prev_indents = [0]
154 def _add_indent(n):
155 if n != prev_indents[-1]:
156 prev_indents.append(n)
157
158 tokiter = iter(tokens)
159 for tok in tokiter:
160 if tok.type in {tokenize.INDENT, tokenize.DEDENT}:
161 _add_indent(tok.end[1])
162 elif (tok.type == tokenize.NL):
163 try:
164 _add_indent(next(tokiter).start[1])
165 except StopIteration:
166 break
167
168 last_indent = prev_indents.pop()
169
170 if tokens[-1].type == IN_MULTILINE_STATEMENT:
171 if tokens[-2].exact_type in {tokenize.LPAR, tokenize.LSQB, tokenize.LBRACE}:
172 return last_indent + 4
173 return last_indent
174
175 if tokens[-1].exact_type == tokenize.COLON:
176 # Line ends with colon - indent
177 return last_indent + 4
178
179 if last_indent:
180 # Examine the last line for dedent cues - statements like return or
181 # raise which normally end a block of code.
182 last_line_starts = 0
183 for i, tok in enumerate(tokens):
184 if tok.type == tokenize.NEWLINE:
185 last_line_starts = i + 1
186
187 last_line_tokens = tokens[last_line_starts:]
188 names = [t.string for t in last_line_tokens if t.type == tokenize.NAME]
189 if names and names[0] in {'raise', 'return', 'pass', 'break', 'continue'}:
190 # Find the most recent indentation less than the current level
191 for indent in reversed(prev_indents):
192 if indent < last_indent:
193 return indent
194
195 return last_indent
196
197
90 def last_blank(src):
198 def last_blank(src):
91 """Determine if the input source ends in a blank.
199 """Determine if the input source ends in a blank.
92
200
93 A blank is either a newline or a line consisting of whitespace.
201 A blank is either a newline or a line consisting of whitespace.
94
202
95 Parameters
203 Parameters
96 ----------
204 ----------
97 src : string
205 src : string
98 A single or multiline string.
206 A single or multiline string.
99 """
207 """
100 if not src: return False
208 if not src: return False
101 ll = src.splitlines()[-1]
209 ll = src.splitlines()[-1]
102 return (ll == '') or ll.isspace()
210 return (ll == '') or ll.isspace()
103
211
104
212
105 last_two_blanks_re = re.compile(r'\n\s*\n\s*$', re.MULTILINE)
213 last_two_blanks_re = re.compile(r'\n\s*\n\s*$', re.MULTILINE)
106 last_two_blanks_re2 = re.compile(r'.+\n\s*\n\s+$', re.MULTILINE)
214 last_two_blanks_re2 = re.compile(r'.+\n\s*\n\s+$', re.MULTILINE)
107
215
108 def last_two_blanks(src):
216 def last_two_blanks(src):
109 """Determine if the input source ends in two blanks.
217 """Determine if the input source ends in two blanks.
110
218
111 A blank is either a newline or a line consisting of whitespace.
219 A blank is either a newline or a line consisting of whitespace.
112
220
113 Parameters
221 Parameters
114 ----------
222 ----------
115 src : string
223 src : string
116 A single or multiline string.
224 A single or multiline string.
117 """
225 """
118 if not src: return False
226 if not src: return False
119 # The logic here is tricky: I couldn't get a regexp to work and pass all
227 # The logic here is tricky: I couldn't get a regexp to work and pass all
120 # the tests, so I took a different approach: split the source by lines,
228 # the tests, so I took a different approach: split the source by lines,
121 # grab the last two and prepend '###\n' as a stand-in for whatever was in
229 # grab the last two and prepend '###\n' as a stand-in for whatever was in
122 # the body before the last two lines. Then, with that structure, it's
230 # the body before the last two lines. Then, with that structure, it's
123 # possible to analyze with two regexps. Not the most elegant solution, but
231 # possible to analyze with two regexps. Not the most elegant solution, but
124 # it works. If anyone tries to change this logic, make sure to validate
232 # it works. If anyone tries to change this logic, make sure to validate
125 # the whole test suite first!
233 # the whole test suite first!
126 new_src = '\n'.join(['###\n'] + src.splitlines()[-2:])
234 new_src = '\n'.join(['###\n'] + src.splitlines()[-2:])
127 return (bool(last_two_blanks_re.match(new_src)) or
235 return (bool(last_two_blanks_re.match(new_src)) or
128 bool(last_two_blanks_re2.match(new_src)) )
236 bool(last_two_blanks_re2.match(new_src)) )
129
237
130
238
131 def remove_comments(src):
239 def remove_comments(src):
132 """Remove all comments from input source.
240 """Remove all comments from input source.
133
241
134 Note: comments are NOT recognized inside of strings!
242 Note: comments are NOT recognized inside of strings!
135
243
136 Parameters
244 Parameters
137 ----------
245 ----------
138 src : string
246 src : string
139 A single or multiline input string.
247 A single or multiline input string.
140
248
141 Returns
249 Returns
142 -------
250 -------
143 String with all Python comments removed.
251 String with all Python comments removed.
144 """
252 """
145
253
146 return re.sub('#.*', '', src)
254 return re.sub('#.*', '', src)
147
255
148
256
149 def get_input_encoding():
257 def get_input_encoding():
150 """Return the default standard input encoding.
258 """Return the default standard input encoding.
151
259
152 If sys.stdin has no encoding, 'ascii' is returned."""
260 If sys.stdin has no encoding, 'ascii' is returned."""
153 # There are strange environments for which sys.stdin.encoding is None. We
261 # There are strange environments for which sys.stdin.encoding is None. We
154 # ensure that a valid encoding is returned.
262 # ensure that a valid encoding is returned.
155 encoding = getattr(sys.stdin, 'encoding', None)
263 encoding = getattr(sys.stdin, 'encoding', None)
156 if encoding is None:
264 if encoding is None:
157 encoding = 'ascii'
265 encoding = 'ascii'
158 return encoding
266 return encoding
159
267
160 #-----------------------------------------------------------------------------
268 #-----------------------------------------------------------------------------
161 # Classes and functions for normal Python syntax handling
269 # Classes and functions for normal Python syntax handling
162 #-----------------------------------------------------------------------------
270 #-----------------------------------------------------------------------------
163
271
164 class InputSplitter(object):
272 class InputSplitter(object):
165 r"""An object that can accumulate lines of Python source before execution.
273 r"""An object that can accumulate lines of Python source before execution.
166
274
167 This object is designed to be fed python source line-by-line, using
275 This object is designed to be fed python source line-by-line, using
168 :meth:`push`. It will return on each push whether the currently pushed
276 :meth:`push`. It will return on each push whether the currently pushed
169 code could be executed already. In addition, it provides a method called
277 code could be executed already. In addition, it provides a method called
170 :meth:`push_accepts_more` that can be used to query whether more input
278 :meth:`push_accepts_more` that can be used to query whether more input
171 can be pushed into a single interactive block.
279 can be pushed into a single interactive block.
172
280
173 This is a simple example of how an interactive terminal-based client can use
281 This is a simple example of how an interactive terminal-based client can use
174 this tool::
282 this tool::
175
283
176 isp = InputSplitter()
284 isp = InputSplitter()
177 while isp.push_accepts_more():
285 while isp.push_accepts_more():
178 indent = ' '*isp.indent_spaces
286 indent = ' '*isp.indent_spaces
179 prompt = '>>> ' + indent
287 prompt = '>>> ' + indent
180 line = indent + raw_input(prompt)
288 line = indent + raw_input(prompt)
181 isp.push(line)
289 isp.push(line)
182 print 'Input source was:\n', isp.source_reset(),
290 print 'Input source was:\n', isp.source_reset(),
183 """
291 """
184 # Number of spaces of indentation computed from input that has been pushed
292 # Number of spaces of indentation computed from input that has been pushed
185 # so far. This is the attributes callers should query to get the current
293 # so far. This is the attributes callers should query to get the current
186 # indentation level, in order to provide auto-indent facilities.
294 # indentation level, in order to provide auto-indent facilities.
187 indent_spaces = 0
295 indent_spaces = 0
188 # String, indicating the default input encoding. It is computed by default
296 # String, indicating the default input encoding. It is computed by default
189 # at initialization time via get_input_encoding(), but it can be reset by a
297 # at initialization time via get_input_encoding(), but it can be reset by a
190 # client with specific knowledge of the encoding.
298 # client with specific knowledge of the encoding.
191 encoding = ''
299 encoding = ''
192 # String where the current full source input is stored, properly encoded.
300 # String where the current full source input is stored, properly encoded.
193 # Reading this attribute is the normal way of querying the currently pushed
301 # Reading this attribute is the normal way of querying the currently pushed
194 # source code, that has been properly encoded.
302 # source code, that has been properly encoded.
195 source = ''
303 source = ''
196 # Code object corresponding to the current source. It is automatically
304 # Code object corresponding to the current source. It is automatically
197 # synced to the source, so it can be queried at any time to obtain the code
305 # synced to the source, so it can be queried at any time to obtain the code
198 # object; it will be None if the source doesn't compile to valid Python.
306 # object; it will be None if the source doesn't compile to valid Python.
199 code = None
307 code = None
200
308
201 # Private attributes
309 # Private attributes
202
310
203 # List with lines of input accumulated so far
311 # List with lines of input accumulated so far
204 _buffer = None
312 _buffer = None
205 # Command compiler
313 # Command compiler
206 _compile = None
314 _compile = None
207 # Mark when input has changed indentation all the way back to flush-left
315 # Mark when input has changed indentation all the way back to flush-left
208 _full_dedent = False
316 _full_dedent = False
209 # Boolean indicating whether the current block is complete
317 # Boolean indicating whether the current block is complete
210 _is_complete = None
318 _is_complete = None
211 # Boolean indicating whether the current block has an unrecoverable syntax error
319 # Boolean indicating whether the current block has an unrecoverable syntax error
212 _is_invalid = False
320 _is_invalid = False
213
321
214 def __init__(self):
322 def __init__(self):
215 """Create a new InputSplitter instance.
323 """Create a new InputSplitter instance.
216 """
324 """
217 self._buffer = []
325 self._buffer = []
218 self._compile = codeop.CommandCompiler()
326 self._compile = codeop.CommandCompiler()
219 self.encoding = get_input_encoding()
327 self.encoding = get_input_encoding()
220
328
221 def reset(self):
329 def reset(self):
222 """Reset the input buffer and associated state."""
330 """Reset the input buffer and associated state."""
223 self.indent_spaces = 0
331 self.indent_spaces = 0
224 self._buffer[:] = []
332 self._buffer[:] = []
225 self.source = ''
333 self.source = ''
226 self.code = None
334 self.code = None
227 self._is_complete = False
335 self._is_complete = False
228 self._is_invalid = False
336 self._is_invalid = False
229 self._full_dedent = False
337 self._full_dedent = False
230
338
231 def source_reset(self):
339 def source_reset(self):
232 """Return the input source and perform a full reset.
340 """Return the input source and perform a full reset.
233 """
341 """
234 out = self.source
342 out = self.source
235 self.reset()
343 self.reset()
236 return out
344 return out
237
345
238 def check_complete(self, source):
346 def check_complete(self, source):
239 """Return whether a block of code is ready to execute, or should be continued
347 """Return whether a block of code is ready to execute, or should be continued
240
348
241 This is a non-stateful API, and will reset the state of this InputSplitter.
349 This is a non-stateful API, and will reset the state of this InputSplitter.
242
350
243 Parameters
351 Parameters
244 ----------
352 ----------
245 source : string
353 source : string
246 Python input code, which can be multiline.
354 Python input code, which can be multiline.
247
355
248 Returns
356 Returns
249 -------
357 -------
250 status : str
358 status : str
251 One of 'complete', 'incomplete', or 'invalid' if source is not a
359 One of 'complete', 'incomplete', or 'invalid' if source is not a
252 prefix of valid code.
360 prefix of valid code.
253 indent_spaces : int or None
361 indent_spaces : int or None
254 The number of spaces by which to indent the next line of code. If
362 The number of spaces by which to indent the next line of code. If
255 status is not 'incomplete', this is None.
363 status is not 'incomplete', this is None.
256 """
364 """
257 self.reset()
365 self.reset()
258 try:
366 try:
259 self.push(source)
367 self.push(source)
260 except SyntaxError:
368 except SyntaxError:
261 # Transformers in IPythonInputSplitter can raise SyntaxError,
369 # Transformers in IPythonInputSplitter can raise SyntaxError,
262 # which push() will not catch.
370 # which push() will not catch.
263 return 'invalid', None
371 return 'invalid', None
264 else:
372 else:
265 if self._is_invalid:
373 if self._is_invalid:
266 return 'invalid', None
374 return 'invalid', None
267 elif self.push_accepts_more():
375 elif self.push_accepts_more():
268 return 'incomplete', self.indent_spaces
376 return 'incomplete', self.indent_spaces
269 else:
377 else:
270 return 'complete', None
378 return 'complete', None
271 finally:
379 finally:
272 self.reset()
380 self.reset()
273
381
274 def push(self, lines):
382 def push(self, lines):
275 """Push one or more lines of input.
383 """Push one or more lines of input.
276
384
277 This stores the given lines and returns a status code indicating
385 This stores the given lines and returns a status code indicating
278 whether the code forms a complete Python block or not.
386 whether the code forms a complete Python block or not.
279
387
280 Any exceptions generated in compilation are swallowed, but if an
388 Any exceptions generated in compilation are swallowed, but if an
281 exception was produced, the method returns True.
389 exception was produced, the method returns True.
282
390
283 Parameters
391 Parameters
284 ----------
392 ----------
285 lines : string
393 lines : string
286 One or more lines of Python input.
394 One or more lines of Python input.
287
395
288 Returns
396 Returns
289 -------
397 -------
290 is_complete : boolean
398 is_complete : boolean
291 True if the current input source (the result of the current input
399 True if the current input source (the result of the current input
292 plus prior inputs) forms a complete Python execution block. Note that
400 plus prior inputs) forms a complete Python execution block. Note that
293 this value is also stored as a private attribute (``_is_complete``), so it
401 this value is also stored as a private attribute (``_is_complete``), so it
294 can be queried at any time.
402 can be queried at any time.
295 """
403 """
296 self._store(lines)
404 self._store(lines)
297 source = self.source
405 source = self.source
298
406
299 # Before calling _compile(), reset the code object to None so that if an
407 # Before calling _compile(), reset the code object to None so that if an
300 # exception is raised in compilation, we don't mislead by having
408 # exception is raised in compilation, we don't mislead by having
301 # inconsistent code/source attributes.
409 # inconsistent code/source attributes.
302 self.code, self._is_complete = None, None
410 self.code, self._is_complete = None, None
303 self._is_invalid = False
411 self._is_invalid = False
304
412
305 # Honor termination lines properly
413 # Honor termination lines properly
306 if source.endswith('\\\n'):
414 if source.endswith('\\\n'):
307 return False
415 return False
308
416
309 self._update_indent(lines)
417 self._update_indent()
310 try:
418 try:
311 with warnings.catch_warnings():
419 with warnings.catch_warnings():
312 warnings.simplefilter('error', SyntaxWarning)
420 warnings.simplefilter('error', SyntaxWarning)
313 self.code = self._compile(source, symbol="exec")
421 self.code = self._compile(source, symbol="exec")
314 # Invalid syntax can produce any of a number of different errors from
422 # Invalid syntax can produce any of a number of different errors from
315 # inside the compiler, so we have to catch them all. Syntax errors
423 # inside the compiler, so we have to catch them all. Syntax errors
316 # immediately produce a 'ready' block, so the invalid Python can be
424 # immediately produce a 'ready' block, so the invalid Python can be
317 # sent to the kernel for evaluation with possible ipython
425 # sent to the kernel for evaluation with possible ipython
318 # special-syntax conversion.
426 # special-syntax conversion.
319 except (SyntaxError, OverflowError, ValueError, TypeError,
427 except (SyntaxError, OverflowError, ValueError, TypeError,
320 MemoryError, SyntaxWarning):
428 MemoryError, SyntaxWarning):
321 self._is_complete = True
429 self._is_complete = True
322 self._is_invalid = True
430 self._is_invalid = True
323 else:
431 else:
324 # Compilation didn't produce any exceptions (though it may not have
432 # Compilation didn't produce any exceptions (though it may not have
325 # given a complete code object)
433 # given a complete code object)
326 self._is_complete = self.code is not None
434 self._is_complete = self.code is not None
327
435
328 return self._is_complete
436 return self._is_complete
329
437
330 def push_accepts_more(self):
438 def push_accepts_more(self):
331 """Return whether a block of interactive input can accept more input.
439 """Return whether a block of interactive input can accept more input.
332
440
333 This method is meant to be used by line-oriented frontends, who need to
441 This method is meant to be used by line-oriented frontends, who need to
334 guess whether a block is complete or not based solely on prior and
442 guess whether a block is complete or not based solely on prior and
335 current input lines. The InputSplitter considers it has a complete
443 current input lines. The InputSplitter considers it has a complete
336 interactive block and will not accept more input when either:
444 interactive block and will not accept more input when either:
337
445
338 * A SyntaxError is raised
446 * A SyntaxError is raised
339
447
340 * The code is complete and consists of a single line or a single
448 * The code is complete and consists of a single line or a single
341 non-compound statement
449 non-compound statement
342
450
343 * The code is complete and has a blank line at the end
451 * The code is complete and has a blank line at the end
344
452
345 If the current input produces a syntax error, this method immediately
453 If the current input produces a syntax error, this method immediately
346 returns False but does *not* raise the syntax error exception, as
454 returns False but does *not* raise the syntax error exception, as
347 typically clients will want to send invalid syntax to an execution
455 typically clients will want to send invalid syntax to an execution
348 backend which might convert the invalid syntax into valid Python via
456 backend which might convert the invalid syntax into valid Python via
349 one of the dynamic IPython mechanisms.
457 one of the dynamic IPython mechanisms.
350 """
458 """
351
459
352 # With incomplete input, unconditionally accept more
460 # With incomplete input, unconditionally accept more
353 # A syntax error also sets _is_complete to True - see push()
461 # A syntax error also sets _is_complete to True - see push()
354 if not self._is_complete:
462 if not self._is_complete:
355 #print("Not complete") # debug
463 #print("Not complete") # debug
356 return True
464 return True
357
465
358 # The user can make any (complete) input execute by leaving a blank line
466 # The user can make any (complete) input execute by leaving a blank line
359 last_line = self.source.splitlines()[-1]
467 last_line = self.source.splitlines()[-1]
360 if (not last_line) or last_line.isspace():
468 if (not last_line) or last_line.isspace():
361 #print("Blank line") # debug
469 #print("Blank line") # debug
362 return False
470 return False
363
471
364 # If there's just a single line or AST node, and we're flush left, as is
472 # If there's just a single line or AST node, and we're flush left, as is
365 # the case after a simple statement such as 'a=1', we want to execute it
473 # the case after a simple statement such as 'a=1', we want to execute it
366 # straight away.
474 # straight away.
367 if self.indent_spaces==0:
475 if self.indent_spaces==0:
368 if len(self.source.splitlines()) <= 1:
476 if len(self.source.splitlines()) <= 1:
369 return False
477 return False
370
478
371 try:
479 try:
372 code_ast = ast.parse(u''.join(self._buffer))
480 code_ast = ast.parse(u''.join(self._buffer))
373 except Exception:
481 except Exception:
374 #print("Can't parse AST") # debug
482 #print("Can't parse AST") # debug
375 return False
483 return False
376 else:
484 else:
377 if len(code_ast.body) == 1 and \
485 if len(code_ast.body) == 1 and \
378 not hasattr(code_ast.body[0], 'body'):
486 not hasattr(code_ast.body[0], 'body'):
379 #print("Simple statement") # debug
487 #print("Simple statement") # debug
380 return False
488 return False
381
489
382 # General fallback - accept more code
490 # General fallback - accept more code
383 return True
491 return True
384
492
385 #------------------------------------------------------------------------
493 #------------------------------------------------------------------------
386 # Private interface
494 # Private interface
387 #------------------------------------------------------------------------
495 #------------------------------------------------------------------------
388
496
389 def _find_indent(self, line):
497 def _find_indent(self, line):
390 """Compute the new indentation level for a single line.
498 """Compute the new indentation level for a single line.
391
499
392 Parameters
500 Parameters
393 ----------
501 ----------
394 line : str
502 line : str
395 A single new line of non-whitespace, non-comment Python input.
503 A single new line of non-whitespace, non-comment Python input.
396
504
397 Returns
505 Returns
398 -------
506 -------
399 indent_spaces : int
507 indent_spaces : int
400 New value for the indent level (it may be equal to self.indent_spaces
508 New value for the indent level (it may be equal to self.indent_spaces
401 if indentation doesn't change.
509 if indentation doesn't change.
402
510
403 full_dedent : boolean
511 full_dedent : boolean
404 Whether the new line causes a full flush-left dedent.
512 Whether the new line causes a full flush-left dedent.
405 """
513 """
406 indent_spaces = self.indent_spaces
514 indent_spaces = self.indent_spaces
407 full_dedent = self._full_dedent
515 full_dedent = self._full_dedent
408
516
409 inisp = num_ini_spaces(line)
517 inisp = num_ini_spaces(line)
410 if inisp < indent_spaces:
518 if inisp < indent_spaces:
411 indent_spaces = inisp
519 indent_spaces = inisp
412 if indent_spaces <= 0:
520 if indent_spaces <= 0:
413 #print 'Full dedent in text',self.source # dbg
521 #print 'Full dedent in text',self.source # dbg
414 full_dedent = True
522 full_dedent = True
415
523
416 if line.rstrip()[-1] == ':':
524 if line.rstrip()[-1] == ':':
417 indent_spaces += 4
525 indent_spaces += 4
418 elif dedent_re.match(line):
526 elif dedent_re.match(line):
419 indent_spaces -= 4
527 indent_spaces -= 4
420 if indent_spaces <= 0:
528 if indent_spaces <= 0:
421 full_dedent = True
529 full_dedent = True
422
530
423 # Safety
531 # Safety
424 if indent_spaces < 0:
532 if indent_spaces < 0:
425 indent_spaces = 0
533 indent_spaces = 0
426 #print 'safety' # dbg
534 #print 'safety' # dbg
427
535
428 return indent_spaces, full_dedent
536 return indent_spaces, full_dedent
429
537
430 def _update_indent(self, lines):
538 def _update_indent(self):
431 for line in remove_comments(lines).splitlines():
539 # self.source always has a trailing newline
432 if line and not line.isspace():
540 self.indent_spaces = find_next_indent(self.source[:-1])
433 self.indent_spaces, self._full_dedent = self._find_indent(line)
541 self._full_dedent = (self.indent_spaces == 0)
434
542
435 def _store(self, lines, buffer=None, store='source'):
543 def _store(self, lines, buffer=None, store='source'):
436 """Store one or more lines of input.
544 """Store one or more lines of input.
437
545
438 If input lines are not newline-terminated, a newline is automatically
546 If input lines are not newline-terminated, a newline is automatically
439 appended."""
547 appended."""
440
548
441 if buffer is None:
549 if buffer is None:
442 buffer = self._buffer
550 buffer = self._buffer
443
551
444 if lines.endswith('\n'):
552 if lines.endswith('\n'):
445 buffer.append(lines)
553 buffer.append(lines)
446 else:
554 else:
447 buffer.append(lines+'\n')
555 buffer.append(lines+'\n')
448 setattr(self, store, self._set_source(buffer))
556 setattr(self, store, self._set_source(buffer))
449
557
450 def _set_source(self, buffer):
558 def _set_source(self, buffer):
451 return u''.join(buffer)
559 return u''.join(buffer)
452
560
453
561
454 class IPythonInputSplitter(InputSplitter):
562 class IPythonInputSplitter(InputSplitter):
455 """An input splitter that recognizes all of IPython's special syntax."""
563 """An input splitter that recognizes all of IPython's special syntax."""
456
564
457 # String with raw, untransformed input.
565 # String with raw, untransformed input.
458 source_raw = ''
566 source_raw = ''
459
567
460 # Flag to track when a transformer has stored input that it hasn't given
568 # Flag to track when a transformer has stored input that it hasn't given
461 # back yet.
569 # back yet.
462 transformer_accumulating = False
570 transformer_accumulating = False
463
571
464 # Flag to track when assemble_python_lines has stored input that it hasn't
572 # Flag to track when assemble_python_lines has stored input that it hasn't
465 # given back yet.
573 # given back yet.
466 within_python_line = False
574 within_python_line = False
467
575
468 # Private attributes
576 # Private attributes
469
577
470 # List with lines of raw input accumulated so far.
578 # List with lines of raw input accumulated so far.
471 _buffer_raw = None
579 _buffer_raw = None
472
580
473 def __init__(self, line_input_checker=True, physical_line_transforms=None,
581 def __init__(self, line_input_checker=True, physical_line_transforms=None,
474 logical_line_transforms=None, python_line_transforms=None):
582 logical_line_transforms=None, python_line_transforms=None):
475 super(IPythonInputSplitter, self).__init__()
583 super(IPythonInputSplitter, self).__init__()
476 self._buffer_raw = []
584 self._buffer_raw = []
477 self._validate = True
585 self._validate = True
478
586
479 if physical_line_transforms is not None:
587 if physical_line_transforms is not None:
480 self.physical_line_transforms = physical_line_transforms
588 self.physical_line_transforms = physical_line_transforms
481 else:
589 else:
482 self.physical_line_transforms = [
590 self.physical_line_transforms = [
483 leading_indent(),
591 leading_indent(),
484 classic_prompt(),
592 classic_prompt(),
485 ipy_prompt(),
593 ipy_prompt(),
486 cellmagic(end_on_blank_line=line_input_checker),
594 cellmagic(end_on_blank_line=line_input_checker),
487 ]
595 ]
488
596
489 self.assemble_logical_lines = assemble_logical_lines()
597 self.assemble_logical_lines = assemble_logical_lines()
490 if logical_line_transforms is not None:
598 if logical_line_transforms is not None:
491 self.logical_line_transforms = logical_line_transforms
599 self.logical_line_transforms = logical_line_transforms
492 else:
600 else:
493 self.logical_line_transforms = [
601 self.logical_line_transforms = [
494 help_end(),
602 help_end(),
495 escaped_commands(),
603 escaped_commands(),
496 assign_from_magic(),
604 assign_from_magic(),
497 assign_from_system(),
605 assign_from_system(),
498 ]
606 ]
499
607
500 self.assemble_python_lines = assemble_python_lines()
608 self.assemble_python_lines = assemble_python_lines()
501 if python_line_transforms is not None:
609 if python_line_transforms is not None:
502 self.python_line_transforms = python_line_transforms
610 self.python_line_transforms = python_line_transforms
503 else:
611 else:
504 # We don't use any of these at present
612 # We don't use any of these at present
505 self.python_line_transforms = []
613 self.python_line_transforms = []
506
614
507 @property
615 @property
508 def transforms(self):
616 def transforms(self):
509 "Quick access to all transformers."
617 "Quick access to all transformers."
510 return self.physical_line_transforms + \
618 return self.physical_line_transforms + \
511 [self.assemble_logical_lines] + self.logical_line_transforms + \
619 [self.assemble_logical_lines] + self.logical_line_transforms + \
512 [self.assemble_python_lines] + self.python_line_transforms
620 [self.assemble_python_lines] + self.python_line_transforms
513
621
514 @property
622 @property
515 def transforms_in_use(self):
623 def transforms_in_use(self):
516 """Transformers, excluding logical line transformers if we're in a
624 """Transformers, excluding logical line transformers if we're in a
517 Python line."""
625 Python line."""
518 t = self.physical_line_transforms[:]
626 t = self.physical_line_transforms[:]
519 if not self.within_python_line:
627 if not self.within_python_line:
520 t += [self.assemble_logical_lines] + self.logical_line_transforms
628 t += [self.assemble_logical_lines] + self.logical_line_transforms
521 return t + [self.assemble_python_lines] + self.python_line_transforms
629 return t + [self.assemble_python_lines] + self.python_line_transforms
522
630
523 def reset(self):
631 def reset(self):
524 """Reset the input buffer and associated state."""
632 """Reset the input buffer and associated state."""
525 super(IPythonInputSplitter, self).reset()
633 super(IPythonInputSplitter, self).reset()
526 self._buffer_raw[:] = []
634 self._buffer_raw[:] = []
527 self.source_raw = ''
635 self.source_raw = ''
528 self.transformer_accumulating = False
636 self.transformer_accumulating = False
529 self.within_python_line = False
637 self.within_python_line = False
530
638
531 for t in self.transforms:
639 for t in self.transforms:
532 try:
640 try:
533 t.reset()
641 t.reset()
534 except SyntaxError:
642 except SyntaxError:
535 # Nothing that calls reset() expects to handle transformer
643 # Nothing that calls reset() expects to handle transformer
536 # errors
644 # errors
537 pass
645 pass
538
646
539 def flush_transformers(self):
647 def flush_transformers(self):
540 def _flush(transform, outs):
648 def _flush(transform, outs):
541 """yield transformed lines
649 """yield transformed lines
542
650
543 always strings, never None
651 always strings, never None
544
652
545 transform: the current transform
653 transform: the current transform
546 outs: an iterable of previously transformed inputs.
654 outs: an iterable of previously transformed inputs.
547 Each may be multiline, which will be passed
655 Each may be multiline, which will be passed
548 one line at a time to transform.
656 one line at a time to transform.
549 """
657 """
550 for out in outs:
658 for out in outs:
551 for line in out.splitlines():
659 for line in out.splitlines():
552 # push one line at a time
660 # push one line at a time
553 tmp = transform.push(line)
661 tmp = transform.push(line)
554 if tmp is not None:
662 if tmp is not None:
555 yield tmp
663 yield tmp
556
664
557 # reset the transform
665 # reset the transform
558 tmp = transform.reset()
666 tmp = transform.reset()
559 if tmp is not None:
667 if tmp is not None:
560 yield tmp
668 yield tmp
561
669
562 out = []
670 out = []
563 for t in self.transforms_in_use:
671 for t in self.transforms_in_use:
564 out = _flush(t, out)
672 out = _flush(t, out)
565
673
566 out = list(out)
674 out = list(out)
567 if out:
675 if out:
568 self._store('\n'.join(out))
676 self._store('\n'.join(out))
569
677
570 def raw_reset(self):
678 def raw_reset(self):
571 """Return raw input only and perform a full reset.
679 """Return raw input only and perform a full reset.
572 """
680 """
573 out = self.source_raw
681 out = self.source_raw
574 self.reset()
682 self.reset()
575 return out
683 return out
576
684
577 def source_reset(self):
685 def source_reset(self):
578 try:
686 try:
579 self.flush_transformers()
687 self.flush_transformers()
580 return self.source
688 return self.source
581 finally:
689 finally:
582 self.reset()
690 self.reset()
583
691
584 def push_accepts_more(self):
692 def push_accepts_more(self):
585 if self.transformer_accumulating:
693 if self.transformer_accumulating:
586 return True
694 return True
587 else:
695 else:
588 return super(IPythonInputSplitter, self).push_accepts_more()
696 return super(IPythonInputSplitter, self).push_accepts_more()
589
697
590 def transform_cell(self, cell):
698 def transform_cell(self, cell):
591 """Process and translate a cell of input.
699 """Process and translate a cell of input.
592 """
700 """
593 self.reset()
701 self.reset()
594 try:
702 try:
595 self.push(cell)
703 self.push(cell)
596 self.flush_transformers()
704 self.flush_transformers()
597 return self.source
705 return self.source
598 finally:
706 finally:
599 self.reset()
707 self.reset()
600
708
601 def push(self, lines):
709 def push(self, lines):
602 """Push one or more lines of IPython input.
710 """Push one or more lines of IPython input.
603
711
604 This stores the given lines and returns a status code indicating
712 This stores the given lines and returns a status code indicating
605 whether the code forms a complete Python block or not, after processing
713 whether the code forms a complete Python block or not, after processing
606 all input lines for special IPython syntax.
714 all input lines for special IPython syntax.
607
715
608 Any exceptions generated in compilation are swallowed, but if an
716 Any exceptions generated in compilation are swallowed, but if an
609 exception was produced, the method returns True.
717 exception was produced, the method returns True.
610
718
611 Parameters
719 Parameters
612 ----------
720 ----------
613 lines : string
721 lines : string
614 One or more lines of Python input.
722 One or more lines of Python input.
615
723
616 Returns
724 Returns
617 -------
725 -------
618 is_complete : boolean
726 is_complete : boolean
619 True if the current input source (the result of the current input
727 True if the current input source (the result of the current input
620 plus prior inputs) forms a complete Python execution block. Note that
728 plus prior inputs) forms a complete Python execution block. Note that
621 this value is also stored as a private attribute (_is_complete), so it
729 this value is also stored as a private attribute (_is_complete), so it
622 can be queried at any time.
730 can be queried at any time.
623 """
731 """
624
732
625 # We must ensure all input is pure unicode
733 # We must ensure all input is pure unicode
626 lines = cast_unicode(lines, self.encoding)
734 lines = cast_unicode(lines, self.encoding)
627 # ''.splitlines() --> [], but we need to push the empty line to transformers
735 # ''.splitlines() --> [], but we need to push the empty line to transformers
628 lines_list = lines.splitlines()
736 lines_list = lines.splitlines()
629 if not lines_list:
737 if not lines_list:
630 lines_list = ['']
738 lines_list = ['']
631
739
632 # Store raw source before applying any transformations to it. Note
740 # Store raw source before applying any transformations to it. Note
633 # that this must be done *after* the reset() call that would otherwise
741 # that this must be done *after* the reset() call that would otherwise
634 # flush the buffer.
742 # flush the buffer.
635 self._store(lines, self._buffer_raw, 'source_raw')
743 self._store(lines, self._buffer_raw, 'source_raw')
636
744
637 for line in lines_list:
745 for line in lines_list:
638 out = self.push_line(line)
746 out = self.push_line(line)
639
747
640 return out
748 return out
641
749
642 def push_line(self, line):
750 def push_line(self, line):
643 buf = self._buffer
751 buf = self._buffer
644
752
645 def _accumulating(dbg):
753 def _accumulating(dbg):
646 #print(dbg)
754 #print(dbg)
647 self.transformer_accumulating = True
755 self.transformer_accumulating = True
648 return False
756 return False
649
757
650 for transformer in self.physical_line_transforms:
758 for transformer in self.physical_line_transforms:
651 line = transformer.push(line)
759 line = transformer.push(line)
652 if line is None:
760 if line is None:
653 return _accumulating(transformer)
761 return _accumulating(transformer)
654
762
655 if not self.within_python_line:
763 if not self.within_python_line:
656 line = self.assemble_logical_lines.push(line)
764 line = self.assemble_logical_lines.push(line)
657 if line is None:
765 if line is None:
658 return _accumulating('acc logical line')
766 return _accumulating('acc logical line')
659
767
660 for transformer in self.logical_line_transforms:
768 for transformer in self.logical_line_transforms:
661 line = transformer.push(line)
769 line = transformer.push(line)
662 if line is None:
770 if line is None:
663 return _accumulating(transformer)
771 return _accumulating(transformer)
664
772
665 line = self.assemble_python_lines.push(line)
773 line = self.assemble_python_lines.push(line)
666 if line is None:
774 if line is None:
667 self.within_python_line = True
775 self.within_python_line = True
668 return _accumulating('acc python line')
776 return _accumulating('acc python line')
669 else:
777 else:
670 self.within_python_line = False
778 self.within_python_line = False
671
779
672 for transformer in self.python_line_transforms:
780 for transformer in self.python_line_transforms:
673 line = transformer.push(line)
781 line = transformer.push(line)
674 if line is None:
782 if line is None:
675 return _accumulating(transformer)
783 return _accumulating(transformer)
676
784
677 #print("transformers clear") #debug
785 #print("transformers clear") #debug
678 self.transformer_accumulating = False
786 self.transformer_accumulating = False
679 return super(IPythonInputSplitter, self).push(line)
787 return super(IPythonInputSplitter, self).push(line)
@@ -1,614 +1,638 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """Tests for the inputsplitter module."""
2 """Tests for the inputsplitter module."""
3
3
4
4
5 # Copyright (c) IPython Development Team.
5 # Copyright (c) IPython Development Team.
6 # Distributed under the terms of the Modified BSD License.
6 # Distributed under the terms of the Modified BSD License.
7
7
8 import unittest
8 import unittest
9 import sys
9 import sys
10
10
11 import nose.tools as nt
11 import nose.tools as nt
12
12
13 from IPython.core import inputsplitter as isp
13 from IPython.core import inputsplitter as isp
14 from IPython.core.inputtransformer import InputTransformer
14 from IPython.core.inputtransformer import InputTransformer
15 from IPython.core.tests.test_inputtransformer import syntax, syntax_ml
15 from IPython.core.tests.test_inputtransformer import syntax, syntax_ml
16 from IPython.testing import tools as tt
16 from IPython.testing import tools as tt
17 from IPython.utils import py3compat
17 from IPython.utils import py3compat
18 from IPython.utils.py3compat import input
18 from IPython.utils.py3compat import input
19
19
20 #-----------------------------------------------------------------------------
20 #-----------------------------------------------------------------------------
21 # Semi-complete examples (also used as tests)
21 # Semi-complete examples (also used as tests)
22 #-----------------------------------------------------------------------------
22 #-----------------------------------------------------------------------------
23
23
24 # Note: at the bottom, there's a slightly more complete version of this that
24 # Note: at the bottom, there's a slightly more complete version of this that
25 # can be useful during development of code here.
25 # can be useful during development of code here.
26
26
27 def mini_interactive_loop(input_func):
27 def mini_interactive_loop(input_func):
28 """Minimal example of the logic of an interactive interpreter loop.
28 """Minimal example of the logic of an interactive interpreter loop.
29
29
30 This serves as an example, and it is used by the test system with a fake
30 This serves as an example, and it is used by the test system with a fake
31 raw_input that simulates interactive input."""
31 raw_input that simulates interactive input."""
32
32
33 from IPython.core.inputsplitter import InputSplitter
33 from IPython.core.inputsplitter import InputSplitter
34
34
35 isp = InputSplitter()
35 isp = InputSplitter()
36 # In practice, this input loop would be wrapped in an outside loop to read
36 # In practice, this input loop would be wrapped in an outside loop to read
37 # input indefinitely, until some exit/quit command was issued. Here we
37 # input indefinitely, until some exit/quit command was issued. Here we
38 # only illustrate the basic inner loop.
38 # only illustrate the basic inner loop.
39 while isp.push_accepts_more():
39 while isp.push_accepts_more():
40 indent = ' '*isp.indent_spaces
40 indent = ' '*isp.indent_spaces
41 prompt = '>>> ' + indent
41 prompt = '>>> ' + indent
42 line = indent + input_func(prompt)
42 line = indent + input_func(prompt)
43 isp.push(line)
43 isp.push(line)
44
44
45 # Here we just return input so we can use it in a test suite, but a real
45 # Here we just return input so we can use it in a test suite, but a real
46 # interpreter would instead send it for execution somewhere.
46 # interpreter would instead send it for execution somewhere.
47 src = isp.source_reset()
47 src = isp.source_reset()
48 #print 'Input source was:\n', src # dbg
48 #print 'Input source was:\n', src # dbg
49 return src
49 return src
50
50
51 #-----------------------------------------------------------------------------
51 #-----------------------------------------------------------------------------
52 # Test utilities, just for local use
52 # Test utilities, just for local use
53 #-----------------------------------------------------------------------------
53 #-----------------------------------------------------------------------------
54
54
55 def assemble(block):
55 def assemble(block):
56 """Assemble a block into multi-line sub-blocks."""
56 """Assemble a block into multi-line sub-blocks."""
57 return ['\n'.join(sub_block)+'\n' for sub_block in block]
57 return ['\n'.join(sub_block)+'\n' for sub_block in block]
58
58
59
59
60 def pseudo_input(lines):
60 def pseudo_input(lines):
61 """Return a function that acts like raw_input but feeds the input list."""
61 """Return a function that acts like raw_input but feeds the input list."""
62 ilines = iter(lines)
62 ilines = iter(lines)
63 def raw_in(prompt):
63 def raw_in(prompt):
64 try:
64 try:
65 return next(ilines)
65 return next(ilines)
66 except StopIteration:
66 except StopIteration:
67 return ''
67 return ''
68 return raw_in
68 return raw_in
69
69
70 #-----------------------------------------------------------------------------
70 #-----------------------------------------------------------------------------
71 # Tests
71 # Tests
72 #-----------------------------------------------------------------------------
72 #-----------------------------------------------------------------------------
73 def test_spaces():
73 def test_spaces():
74 tests = [('', 0),
74 tests = [('', 0),
75 (' ', 1),
75 (' ', 1),
76 ('\n', 0),
76 ('\n', 0),
77 (' \n', 1),
77 (' \n', 1),
78 ('x', 0),
78 ('x', 0),
79 (' x', 1),
79 (' x', 1),
80 (' x',2),
80 (' x',2),
81 (' x',4),
81 (' x',4),
82 # Note: tabs are counted as a single whitespace!
82 # Note: tabs are counted as a single whitespace!
83 ('\tx', 1),
83 ('\tx', 1),
84 ('\t x', 2),
84 ('\t x', 2),
85 ]
85 ]
86 tt.check_pairs(isp.num_ini_spaces, tests)
86 tt.check_pairs(isp.num_ini_spaces, tests)
87
87
88
88
89 def test_remove_comments():
89 def test_remove_comments():
90 tests = [('text', 'text'),
90 tests = [('text', 'text'),
91 ('text # comment', 'text '),
91 ('text # comment', 'text '),
92 ('text # comment\n', 'text \n'),
92 ('text # comment\n', 'text \n'),
93 ('text # comment \n', 'text \n'),
93 ('text # comment \n', 'text \n'),
94 ('line # c \nline\n','line \nline\n'),
94 ('line # c \nline\n','line \nline\n'),
95 ('line # c \nline#c2 \nline\nline #c\n\n',
95 ('line # c \nline#c2 \nline\nline #c\n\n',
96 'line \nline\nline\nline \n\n'),
96 'line \nline\nline\nline \n\n'),
97 ]
97 ]
98 tt.check_pairs(isp.remove_comments, tests)
98 tt.check_pairs(isp.remove_comments, tests)
99
99
100
100
101 def test_get_input_encoding():
101 def test_get_input_encoding():
102 encoding = isp.get_input_encoding()
102 encoding = isp.get_input_encoding()
103 nt.assert_true(isinstance(encoding, str))
103 nt.assert_true(isinstance(encoding, str))
104 # simple-minded check that at least encoding a simple string works with the
104 # simple-minded check that at least encoding a simple string works with the
105 # encoding we got.
105 # encoding we got.
106 nt.assert_equal(u'test'.encode(encoding), b'test')
106 nt.assert_equal(u'test'.encode(encoding), b'test')
107
107
108
108
109 class NoInputEncodingTestCase(unittest.TestCase):
109 class NoInputEncodingTestCase(unittest.TestCase):
110 def setUp(self):
110 def setUp(self):
111 self.old_stdin = sys.stdin
111 self.old_stdin = sys.stdin
112 class X: pass
112 class X: pass
113 fake_stdin = X()
113 fake_stdin = X()
114 sys.stdin = fake_stdin
114 sys.stdin = fake_stdin
115
115
116 def test(self):
116 def test(self):
117 # Verify that if sys.stdin has no 'encoding' attribute we do the right
117 # Verify that if sys.stdin has no 'encoding' attribute we do the right
118 # thing
118 # thing
119 enc = isp.get_input_encoding()
119 enc = isp.get_input_encoding()
120 self.assertEqual(enc, 'ascii')
120 self.assertEqual(enc, 'ascii')
121
121
122 def tearDown(self):
122 def tearDown(self):
123 sys.stdin = self.old_stdin
123 sys.stdin = self.old_stdin
124
124
125
125
126 class InputSplitterTestCase(unittest.TestCase):
126 class InputSplitterTestCase(unittest.TestCase):
127 def setUp(self):
127 def setUp(self):
128 self.isp = isp.InputSplitter()
128 self.isp = isp.InputSplitter()
129
129
130 def test_reset(self):
130 def test_reset(self):
131 isp = self.isp
131 isp = self.isp
132 isp.push('x=1')
132 isp.push('x=1')
133 isp.reset()
133 isp.reset()
134 self.assertEqual(isp._buffer, [])
134 self.assertEqual(isp._buffer, [])
135 self.assertEqual(isp.indent_spaces, 0)
135 self.assertEqual(isp.indent_spaces, 0)
136 self.assertEqual(isp.source, '')
136 self.assertEqual(isp.source, '')
137 self.assertEqual(isp.code, None)
137 self.assertEqual(isp.code, None)
138 self.assertEqual(isp._is_complete, False)
138 self.assertEqual(isp._is_complete, False)
139
139
140 def test_source(self):
140 def test_source(self):
141 self.isp._store('1')
141 self.isp._store('1')
142 self.isp._store('2')
142 self.isp._store('2')
143 self.assertEqual(self.isp.source, '1\n2\n')
143 self.assertEqual(self.isp.source, '1\n2\n')
144 self.assertEqual(len(self.isp._buffer)>0, True)
144 self.assertEqual(len(self.isp._buffer)>0, True)
145 self.assertEqual(self.isp.source_reset(), '1\n2\n')
145 self.assertEqual(self.isp.source_reset(), '1\n2\n')
146 self.assertEqual(self.isp._buffer, [])
146 self.assertEqual(self.isp._buffer, [])
147 self.assertEqual(self.isp.source, '')
147 self.assertEqual(self.isp.source, '')
148
148
149 def test_indent(self):
149 def test_indent(self):
150 isp = self.isp # shorthand
150 isp = self.isp # shorthand
151 isp.push('x=1')
151 isp.push('x=1')
152 self.assertEqual(isp.indent_spaces, 0)
152 self.assertEqual(isp.indent_spaces, 0)
153 isp.push('if 1:\n x=1')
153 isp.push('if 1:\n x=1')
154 self.assertEqual(isp.indent_spaces, 4)
154 self.assertEqual(isp.indent_spaces, 4)
155 isp.push('y=2\n')
155 isp.push('y=2\n')
156 self.assertEqual(isp.indent_spaces, 0)
156 self.assertEqual(isp.indent_spaces, 0)
157
157
158 def test_indent2(self):
158 def test_indent2(self):
159 isp = self.isp
159 isp = self.isp
160 isp.push('if 1:')
160 isp.push('if 1:')
161 self.assertEqual(isp.indent_spaces, 4)
161 self.assertEqual(isp.indent_spaces, 4)
162 isp.push(' x=1')
162 isp.push(' x=1')
163 self.assertEqual(isp.indent_spaces, 4)
163 self.assertEqual(isp.indent_spaces, 4)
164 # Blank lines shouldn't change the indent level
164 # Blank lines shouldn't change the indent level
165 isp.push(' '*2)
165 isp.push(' '*2)
166 self.assertEqual(isp.indent_spaces, 4)
166 self.assertEqual(isp.indent_spaces, 4)
167
167
168 def test_indent3(self):
168 def test_indent3(self):
169 isp = self.isp
169 isp = self.isp
170 # When a multiline statement contains parens or multiline strings, we
170 # When a multiline statement contains parens or multiline strings, we
171 # shouldn't get confused.
171 # shouldn't get confused.
172 isp.push("if 1:")
172 isp.push("if 1:")
173 isp.push(" x = (1+\n 2)")
173 isp.push(" x = (1+\n 2)")
174 self.assertEqual(isp.indent_spaces, 4)
174 self.assertEqual(isp.indent_spaces, 4)
175
175
176 def test_indent4(self):
176 def test_indent4(self):
177 isp = self.isp
177 isp = self.isp
178 # whitespace after ':' should not screw up indent level
178 # whitespace after ':' should not screw up indent level
179 isp.push('if 1: \n x=1')
179 isp.push('if 1: \n x=1')
180 self.assertEqual(isp.indent_spaces, 4)
180 self.assertEqual(isp.indent_spaces, 4)
181 isp.push('y=2\n')
181 isp.push('y=2\n')
182 self.assertEqual(isp.indent_spaces, 0)
182 self.assertEqual(isp.indent_spaces, 0)
183 isp.push('if 1:\t\n x=1')
183 isp.push('if 1:\t\n x=1')
184 self.assertEqual(isp.indent_spaces, 4)
184 self.assertEqual(isp.indent_spaces, 4)
185 isp.push('y=2\n')
185 isp.push('y=2\n')
186 self.assertEqual(isp.indent_spaces, 0)
186 self.assertEqual(isp.indent_spaces, 0)
187
187
188 def test_dedent_pass(self):
188 def test_dedent_pass(self):
189 isp = self.isp # shorthand
189 isp = self.isp # shorthand
190 # should NOT cause dedent
190 # should NOT cause dedent
191 isp.push('if 1:\n passes = 5')
191 isp.push('if 1:\n passes = 5')
192 self.assertEqual(isp.indent_spaces, 4)
192 self.assertEqual(isp.indent_spaces, 4)
193 isp.push('if 1:\n pass')
193 isp.push('if 1:\n pass')
194 self.assertEqual(isp.indent_spaces, 0)
194 self.assertEqual(isp.indent_spaces, 0)
195 isp.push('if 1:\n pass ')
195 isp.push('if 1:\n pass ')
196 self.assertEqual(isp.indent_spaces, 0)
196 self.assertEqual(isp.indent_spaces, 0)
197
197
198 def test_dedent_break(self):
198 def test_dedent_break(self):
199 isp = self.isp # shorthand
199 isp = self.isp # shorthand
200 # should NOT cause dedent
200 # should NOT cause dedent
201 isp.push('while 1:\n breaks = 5')
201 isp.push('while 1:\n breaks = 5')
202 self.assertEqual(isp.indent_spaces, 4)
202 self.assertEqual(isp.indent_spaces, 4)
203 isp.push('while 1:\n break')
203 isp.push('while 1:\n break')
204 self.assertEqual(isp.indent_spaces, 0)
204 self.assertEqual(isp.indent_spaces, 0)
205 isp.push('while 1:\n break ')
205 isp.push('while 1:\n break ')
206 self.assertEqual(isp.indent_spaces, 0)
206 self.assertEqual(isp.indent_spaces, 0)
207
207
208 def test_dedent_continue(self):
208 def test_dedent_continue(self):
209 isp = self.isp # shorthand
209 isp = self.isp # shorthand
210 # should NOT cause dedent
210 # should NOT cause dedent
211 isp.push('while 1:\n continues = 5')
211 isp.push('while 1:\n continues = 5')
212 self.assertEqual(isp.indent_spaces, 4)
212 self.assertEqual(isp.indent_spaces, 4)
213 isp.push('while 1:\n continue')
213 isp.push('while 1:\n continue')
214 self.assertEqual(isp.indent_spaces, 0)
214 self.assertEqual(isp.indent_spaces, 0)
215 isp.push('while 1:\n continue ')
215 isp.push('while 1:\n continue ')
216 self.assertEqual(isp.indent_spaces, 0)
216 self.assertEqual(isp.indent_spaces, 0)
217
217
218 def test_dedent_raise(self):
218 def test_dedent_raise(self):
219 isp = self.isp # shorthand
219 isp = self.isp # shorthand
220 # should NOT cause dedent
220 # should NOT cause dedent
221 isp.push('if 1:\n raised = 4')
221 isp.push('if 1:\n raised = 4')
222 self.assertEqual(isp.indent_spaces, 4)
222 self.assertEqual(isp.indent_spaces, 4)
223 isp.push('if 1:\n raise TypeError()')
223 isp.push('if 1:\n raise TypeError()')
224 self.assertEqual(isp.indent_spaces, 0)
224 self.assertEqual(isp.indent_spaces, 0)
225 isp.push('if 1:\n raise')
225 isp.push('if 1:\n raise')
226 self.assertEqual(isp.indent_spaces, 0)
226 self.assertEqual(isp.indent_spaces, 0)
227 isp.push('if 1:\n raise ')
227 isp.push('if 1:\n raise ')
228 self.assertEqual(isp.indent_spaces, 0)
228 self.assertEqual(isp.indent_spaces, 0)
229
229
230 def test_dedent_return(self):
230 def test_dedent_return(self):
231 isp = self.isp # shorthand
231 isp = self.isp # shorthand
232 # should NOT cause dedent
232 # should NOT cause dedent
233 isp.push('if 1:\n returning = 4')
233 isp.push('if 1:\n returning = 4')
234 self.assertEqual(isp.indent_spaces, 4)
234 self.assertEqual(isp.indent_spaces, 4)
235 isp.push('if 1:\n return 5 + 493')
235 isp.push('if 1:\n return 5 + 493')
236 self.assertEqual(isp.indent_spaces, 0)
236 self.assertEqual(isp.indent_spaces, 0)
237 isp.push('if 1:\n return')
237 isp.push('if 1:\n return')
238 self.assertEqual(isp.indent_spaces, 0)
238 self.assertEqual(isp.indent_spaces, 0)
239 isp.push('if 1:\n return ')
239 isp.push('if 1:\n return ')
240 self.assertEqual(isp.indent_spaces, 0)
240 self.assertEqual(isp.indent_spaces, 0)
241 isp.push('if 1:\n return(0)')
241 isp.push('if 1:\n return(0)')
242 self.assertEqual(isp.indent_spaces, 0)
242 self.assertEqual(isp.indent_spaces, 0)
243
243
244 def test_push(self):
244 def test_push(self):
245 isp = self.isp
245 isp = self.isp
246 self.assertEqual(isp.push('x=1'), True)
246 self.assertEqual(isp.push('x=1'), True)
247
247
248 def test_push2(self):
248 def test_push2(self):
249 isp = self.isp
249 isp = self.isp
250 self.assertEqual(isp.push('if 1:'), False)
250 self.assertEqual(isp.push('if 1:'), False)
251 for line in [' x=1', '# a comment', ' y=2']:
251 for line in [' x=1', '# a comment', ' y=2']:
252 print(line)
252 print(line)
253 self.assertEqual(isp.push(line), True)
253 self.assertEqual(isp.push(line), True)
254
254
255 def test_push3(self):
255 def test_push3(self):
256 isp = self.isp
256 isp = self.isp
257 isp.push('if True:')
257 isp.push('if True:')
258 isp.push(' a = 1')
258 isp.push(' a = 1')
259 self.assertEqual(isp.push('b = [1,'), False)
259 self.assertEqual(isp.push('b = [1,'), False)
260
260
261 def test_push_accepts_more(self):
261 def test_push_accepts_more(self):
262 isp = self.isp
262 isp = self.isp
263 isp.push('x=1')
263 isp.push('x=1')
264 self.assertEqual(isp.push_accepts_more(), False)
264 self.assertEqual(isp.push_accepts_more(), False)
265
265
266 def test_push_accepts_more2(self):
266 def test_push_accepts_more2(self):
267 isp = self.isp
267 isp = self.isp
268 isp.push('if 1:')
268 isp.push('if 1:')
269 self.assertEqual(isp.push_accepts_more(), True)
269 self.assertEqual(isp.push_accepts_more(), True)
270 isp.push(' x=1')
270 isp.push(' x=1')
271 self.assertEqual(isp.push_accepts_more(), True)
271 self.assertEqual(isp.push_accepts_more(), True)
272 isp.push('')
272 isp.push('')
273 self.assertEqual(isp.push_accepts_more(), False)
273 self.assertEqual(isp.push_accepts_more(), False)
274
274
275 def test_push_accepts_more3(self):
275 def test_push_accepts_more3(self):
276 isp = self.isp
276 isp = self.isp
277 isp.push("x = (2+\n3)")
277 isp.push("x = (2+\n3)")
278 self.assertEqual(isp.push_accepts_more(), False)
278 self.assertEqual(isp.push_accepts_more(), False)
279
279
280 def test_push_accepts_more4(self):
280 def test_push_accepts_more4(self):
281 isp = self.isp
281 isp = self.isp
282 # When a multiline statement contains parens or multiline strings, we
282 # When a multiline statement contains parens or multiline strings, we
283 # shouldn't get confused.
283 # shouldn't get confused.
284 # FIXME: we should be able to better handle de-dents in statements like
284 # FIXME: we should be able to better handle de-dents in statements like
285 # multiline strings and multiline expressions (continued with \ or
285 # multiline strings and multiline expressions (continued with \ or
286 # parens). Right now we aren't handling the indentation tracking quite
286 # parens). Right now we aren't handling the indentation tracking quite
287 # correctly with this, though in practice it may not be too much of a
287 # correctly with this, though in practice it may not be too much of a
288 # problem. We'll need to see.
288 # problem. We'll need to see.
289 isp.push("if 1:")
289 isp.push("if 1:")
290 isp.push(" x = (2+")
290 isp.push(" x = (2+")
291 isp.push(" 3)")
291 isp.push(" 3)")
292 self.assertEqual(isp.push_accepts_more(), True)
292 self.assertEqual(isp.push_accepts_more(), True)
293 isp.push(" y = 3")
293 isp.push(" y = 3")
294 self.assertEqual(isp.push_accepts_more(), True)
294 self.assertEqual(isp.push_accepts_more(), True)
295 isp.push('')
295 isp.push('')
296 self.assertEqual(isp.push_accepts_more(), False)
296 self.assertEqual(isp.push_accepts_more(), False)
297
297
298 def test_push_accepts_more5(self):
298 def test_push_accepts_more5(self):
299 isp = self.isp
299 isp = self.isp
300 isp.push('try:')
300 isp.push('try:')
301 isp.push(' a = 5')
301 isp.push(' a = 5')
302 isp.push('except:')
302 isp.push('except:')
303 isp.push(' raise')
303 isp.push(' raise')
304 # We want to be able to add an else: block at this point, so it should
304 # We want to be able to add an else: block at this point, so it should
305 # wait for a blank line.
305 # wait for a blank line.
306 self.assertEqual(isp.push_accepts_more(), True)
306 self.assertEqual(isp.push_accepts_more(), True)
307
307
308 def test_continuation(self):
308 def test_continuation(self):
309 isp = self.isp
309 isp = self.isp
310 isp.push("import os, \\")
310 isp.push("import os, \\")
311 self.assertEqual(isp.push_accepts_more(), True)
311 self.assertEqual(isp.push_accepts_more(), True)
312 isp.push("sys")
312 isp.push("sys")
313 self.assertEqual(isp.push_accepts_more(), False)
313 self.assertEqual(isp.push_accepts_more(), False)
314
314
315 def test_syntax_error(self):
315 def test_syntax_error(self):
316 isp = self.isp
316 isp = self.isp
317 # Syntax errors immediately produce a 'ready' block, so the invalid
317 # Syntax errors immediately produce a 'ready' block, so the invalid
318 # Python can be sent to the kernel for evaluation with possible ipython
318 # Python can be sent to the kernel for evaluation with possible ipython
319 # special-syntax conversion.
319 # special-syntax conversion.
320 isp.push('run foo')
320 isp.push('run foo')
321 self.assertEqual(isp.push_accepts_more(), False)
321 self.assertEqual(isp.push_accepts_more(), False)
322
322
323 def test_unicode(self):
323 def test_unicode(self):
324 self.isp.push(u"Pérez")
324 self.isp.push(u"Pérez")
325 self.isp.push(u'\xc3\xa9')
325 self.isp.push(u'\xc3\xa9')
326 self.isp.push(u"u'\xc3\xa9'")
326 self.isp.push(u"u'\xc3\xa9'")
327
327
328 def test_line_continuation(self):
328 def test_line_continuation(self):
329 """ Test issue #2108."""
329 """ Test issue #2108."""
330 isp = self.isp
330 isp = self.isp
331 # A blank line after a line continuation should not accept more
331 # A blank line after a line continuation should not accept more
332 isp.push("1 \\\n\n")
332 isp.push("1 \\\n\n")
333 self.assertEqual(isp.push_accepts_more(), False)
333 self.assertEqual(isp.push_accepts_more(), False)
334 # Whitespace after a \ is a SyntaxError. The only way to test that
334 # Whitespace after a \ is a SyntaxError. The only way to test that
335 # here is to test that push doesn't accept more (as with
335 # here is to test that push doesn't accept more (as with
336 # test_syntax_error() above).
336 # test_syntax_error() above).
337 isp.push(r"1 \ ")
337 isp.push(r"1 \ ")
338 self.assertEqual(isp.push_accepts_more(), False)
338 self.assertEqual(isp.push_accepts_more(), False)
339 # Even if the line is continuable (c.f. the regular Python
339 # Even if the line is continuable (c.f. the regular Python
340 # interpreter)
340 # interpreter)
341 isp.push(r"(1 \ ")
341 isp.push(r"(1 \ ")
342 self.assertEqual(isp.push_accepts_more(), False)
342 self.assertEqual(isp.push_accepts_more(), False)
343
343
344 def test_check_complete(self):
344 def test_check_complete(self):
345 isp = self.isp
345 isp = self.isp
346 self.assertEqual(isp.check_complete("a = 1"), ('complete', None))
346 self.assertEqual(isp.check_complete("a = 1"), ('complete', None))
347 self.assertEqual(isp.check_complete("for a in range(5):"), ('incomplete', 4))
347 self.assertEqual(isp.check_complete("for a in range(5):"), ('incomplete', 4))
348 self.assertEqual(isp.check_complete("raise = 2"), ('invalid', None))
348 self.assertEqual(isp.check_complete("raise = 2"), ('invalid', None))
349 self.assertEqual(isp.check_complete("a = [1,\n2,"), ('incomplete', 0))
349 self.assertEqual(isp.check_complete("a = [1,\n2,"), ('incomplete', 0))
350 self.assertEqual(isp.check_complete("def a():\n x=1\n global x"), ('invalid', None))
350 self.assertEqual(isp.check_complete("def a():\n x=1\n global x"), ('invalid', None))
351
351
352 class InteractiveLoopTestCase(unittest.TestCase):
352 class InteractiveLoopTestCase(unittest.TestCase):
353 """Tests for an interactive loop like a python shell.
353 """Tests for an interactive loop like a python shell.
354 """
354 """
355 def check_ns(self, lines, ns):
355 def check_ns(self, lines, ns):
356 """Validate that the given input lines produce the resulting namespace.
356 """Validate that the given input lines produce the resulting namespace.
357
357
358 Note: the input lines are given exactly as they would be typed in an
358 Note: the input lines are given exactly as they would be typed in an
359 auto-indenting environment, as mini_interactive_loop above already does
359 auto-indenting environment, as mini_interactive_loop above already does
360 auto-indenting and prepends spaces to the input.
360 auto-indenting and prepends spaces to the input.
361 """
361 """
362 src = mini_interactive_loop(pseudo_input(lines))
362 src = mini_interactive_loop(pseudo_input(lines))
363 test_ns = {}
363 test_ns = {}
364 exec(src, test_ns)
364 exec(src, test_ns)
365 # We can't check that the provided ns is identical to the test_ns,
365 # We can't check that the provided ns is identical to the test_ns,
366 # because Python fills test_ns with extra keys (copyright, etc). But
366 # because Python fills test_ns with extra keys (copyright, etc). But
367 # we can check that the given dict is *contained* in test_ns
367 # we can check that the given dict is *contained* in test_ns
368 for k,v in ns.items():
368 for k,v in ns.items():
369 self.assertEqual(test_ns[k], v)
369 self.assertEqual(test_ns[k], v)
370
370
371 def test_simple(self):
371 def test_simple(self):
372 self.check_ns(['x=1'], dict(x=1))
372 self.check_ns(['x=1'], dict(x=1))
373
373
374 def test_simple2(self):
374 def test_simple2(self):
375 self.check_ns(['if 1:', 'x=2'], dict(x=2))
375 self.check_ns(['if 1:', 'x=2'], dict(x=2))
376
376
377 def test_xy(self):
377 def test_xy(self):
378 self.check_ns(['x=1; y=2'], dict(x=1, y=2))
378 self.check_ns(['x=1; y=2'], dict(x=1, y=2))
379
379
380 def test_abc(self):
380 def test_abc(self):
381 self.check_ns(['if 1:','a=1','b=2','c=3'], dict(a=1, b=2, c=3))
381 self.check_ns(['if 1:','a=1','b=2','c=3'], dict(a=1, b=2, c=3))
382
382
383 def test_multi(self):
383 def test_multi(self):
384 self.check_ns(['x =(1+','1+','2)'], dict(x=4))
384 self.check_ns(['x =(1+','1+','2)'], dict(x=4))
385
385
386
386
387 class IPythonInputTestCase(InputSplitterTestCase):
387 class IPythonInputTestCase(InputSplitterTestCase):
388 """By just creating a new class whose .isp is a different instance, we
388 """By just creating a new class whose .isp is a different instance, we
389 re-run the same test battery on the new input splitter.
389 re-run the same test battery on the new input splitter.
390
390
391 In addition, this runs the tests over the syntax and syntax_ml dicts that
391 In addition, this runs the tests over the syntax and syntax_ml dicts that
392 were tested by individual functions, as part of the OO interface.
392 were tested by individual functions, as part of the OO interface.
393
393
394 It also makes some checks on the raw buffer storage.
394 It also makes some checks on the raw buffer storage.
395 """
395 """
396
396
397 def setUp(self):
397 def setUp(self):
398 self.isp = isp.IPythonInputSplitter()
398 self.isp = isp.IPythonInputSplitter()
399
399
400 def test_syntax(self):
400 def test_syntax(self):
401 """Call all single-line syntax tests from the main object"""
401 """Call all single-line syntax tests from the main object"""
402 isp = self.isp
402 isp = self.isp
403 for example in syntax.values():
403 for example in syntax.values():
404 for raw, out_t in example:
404 for raw, out_t in example:
405 if raw.startswith(' '):
405 if raw.startswith(' '):
406 continue
406 continue
407
407
408 isp.push(raw+'\n')
408 isp.push(raw+'\n')
409 out_raw = isp.source_raw
409 out_raw = isp.source_raw
410 out = isp.source_reset()
410 out = isp.source_reset()
411 self.assertEqual(out.rstrip(), out_t,
411 self.assertEqual(out.rstrip(), out_t,
412 tt.pair_fail_msg.format("inputsplitter",raw, out_t, out))
412 tt.pair_fail_msg.format("inputsplitter",raw, out_t, out))
413 self.assertEqual(out_raw.rstrip(), raw.rstrip())
413 self.assertEqual(out_raw.rstrip(), raw.rstrip())
414
414
415 def test_syntax_multiline(self):
415 def test_syntax_multiline(self):
416 isp = self.isp
416 isp = self.isp
417 for example in syntax_ml.values():
417 for example in syntax_ml.values():
418 for line_pairs in example:
418 for line_pairs in example:
419 out_t_parts = []
419 out_t_parts = []
420 raw_parts = []
420 raw_parts = []
421 for lraw, out_t_part in line_pairs:
421 for lraw, out_t_part in line_pairs:
422 if out_t_part is not None:
422 if out_t_part is not None:
423 out_t_parts.append(out_t_part)
423 out_t_parts.append(out_t_part)
424
424
425 if lraw is not None:
425 if lraw is not None:
426 isp.push(lraw)
426 isp.push(lraw)
427 raw_parts.append(lraw)
427 raw_parts.append(lraw)
428
428
429 out_raw = isp.source_raw
429 out_raw = isp.source_raw
430 out = isp.source_reset()
430 out = isp.source_reset()
431 out_t = '\n'.join(out_t_parts).rstrip()
431 out_t = '\n'.join(out_t_parts).rstrip()
432 raw = '\n'.join(raw_parts).rstrip()
432 raw = '\n'.join(raw_parts).rstrip()
433 self.assertEqual(out.rstrip(), out_t)
433 self.assertEqual(out.rstrip(), out_t)
434 self.assertEqual(out_raw.rstrip(), raw)
434 self.assertEqual(out_raw.rstrip(), raw)
435
435
436 def test_syntax_multiline_cell(self):
436 def test_syntax_multiline_cell(self):
437 isp = self.isp
437 isp = self.isp
438 for example in syntax_ml.values():
438 for example in syntax_ml.values():
439
439
440 out_t_parts = []
440 out_t_parts = []
441 for line_pairs in example:
441 for line_pairs in example:
442 raw = '\n'.join(r for r, _ in line_pairs if r is not None)
442 raw = '\n'.join(r for r, _ in line_pairs if r is not None)
443 out_t = '\n'.join(t for _,t in line_pairs if t is not None)
443 out_t = '\n'.join(t for _,t in line_pairs if t is not None)
444 out = isp.transform_cell(raw)
444 out = isp.transform_cell(raw)
445 # Match ignoring trailing whitespace
445 # Match ignoring trailing whitespace
446 self.assertEqual(out.rstrip(), out_t.rstrip())
446 self.assertEqual(out.rstrip(), out_t.rstrip())
447
447
448 def test_cellmagic_preempt(self):
448 def test_cellmagic_preempt(self):
449 isp = self.isp
449 isp = self.isp
450 for raw, name, line, cell in [
450 for raw, name, line, cell in [
451 ("%%cellm a\nIn[1]:", u'cellm', u'a', u'In[1]:'),
451 ("%%cellm a\nIn[1]:", u'cellm', u'a', u'In[1]:'),
452 ("%%cellm \nline\n>>> hi", u'cellm', u'', u'line\n>>> hi'),
452 ("%%cellm \nline\n>>> hi", u'cellm', u'', u'line\n>>> hi'),
453 (">>> %%cellm \nline\n>>> hi", u'cellm', u'', u'line\nhi'),
453 (">>> %%cellm \nline\n>>> hi", u'cellm', u'', u'line\nhi'),
454 ("%%cellm \n>>> hi", u'cellm', u'', u'>>> hi'),
454 ("%%cellm \n>>> hi", u'cellm', u'', u'>>> hi'),
455 ("%%cellm \nline1\nline2", u'cellm', u'', u'line1\nline2'),
455 ("%%cellm \nline1\nline2", u'cellm', u'', u'line1\nline2'),
456 ("%%cellm \nline1\\\\\nline2", u'cellm', u'', u'line1\\\\\nline2'),
456 ("%%cellm \nline1\\\\\nline2", u'cellm', u'', u'line1\\\\\nline2'),
457 ]:
457 ]:
458 expected = "get_ipython().run_cell_magic(%r, %r, %r)" % (
458 expected = "get_ipython().run_cell_magic(%r, %r, %r)" % (
459 name, line, cell
459 name, line, cell
460 )
460 )
461 out = isp.transform_cell(raw)
461 out = isp.transform_cell(raw)
462 self.assertEqual(out.rstrip(), expected.rstrip())
462 self.assertEqual(out.rstrip(), expected.rstrip())
463
463
464 def test_multiline_passthrough(self):
464 def test_multiline_passthrough(self):
465 isp = self.isp
465 isp = self.isp
466 class CommentTransformer(InputTransformer):
466 class CommentTransformer(InputTransformer):
467 def __init__(self):
467 def __init__(self):
468 self._lines = []
468 self._lines = []
469
469
470 def push(self, line):
470 def push(self, line):
471 self._lines.append(line + '#')
471 self._lines.append(line + '#')
472
472
473 def reset(self):
473 def reset(self):
474 text = '\n'.join(self._lines)
474 text = '\n'.join(self._lines)
475 self._lines = []
475 self._lines = []
476 return text
476 return text
477
477
478 isp.physical_line_transforms.insert(0, CommentTransformer())
478 isp.physical_line_transforms.insert(0, CommentTransformer())
479
479
480 for raw, expected in [
480 for raw, expected in [
481 ("a=5", "a=5#"),
481 ("a=5", "a=5#"),
482 ("%ls foo", "get_ipython().magic(%r)" % u'ls foo#'),
482 ("%ls foo", "get_ipython().magic(%r)" % u'ls foo#'),
483 ("!ls foo\n%ls bar", "get_ipython().system(%r)\nget_ipython().magic(%r)" % (
483 ("!ls foo\n%ls bar", "get_ipython().system(%r)\nget_ipython().magic(%r)" % (
484 u'ls foo#', u'ls bar#'
484 u'ls foo#', u'ls bar#'
485 )),
485 )),
486 ("1\n2\n3\n%ls foo\n4\n5", "1#\n2#\n3#\nget_ipython().magic(%r)\n4#\n5#" % u'ls foo#'),
486 ("1\n2\n3\n%ls foo\n4\n5", "1#\n2#\n3#\nget_ipython().magic(%r)\n4#\n5#" % u'ls foo#'),
487 ]:
487 ]:
488 out = isp.transform_cell(raw)
488 out = isp.transform_cell(raw)
489 self.assertEqual(out.rstrip(), expected.rstrip())
489 self.assertEqual(out.rstrip(), expected.rstrip())
490
490
491 #-----------------------------------------------------------------------------
491 #-----------------------------------------------------------------------------
492 # Main - use as a script, mostly for developer experiments
492 # Main - use as a script, mostly for developer experiments
493 #-----------------------------------------------------------------------------
493 #-----------------------------------------------------------------------------
494
494
495 if __name__ == '__main__':
495 if __name__ == '__main__':
496 # A simple demo for interactive experimentation. This code will not get
496 # A simple demo for interactive experimentation. This code will not get
497 # picked up by any test suite.
497 # picked up by any test suite.
498 from IPython.core.inputsplitter import IPythonInputSplitter
498 from IPython.core.inputsplitter import IPythonInputSplitter
499
499
500 # configure here the syntax to use, prompt and whether to autoindent
500 # configure here the syntax to use, prompt and whether to autoindent
501 #isp, start_prompt = InputSplitter(), '>>> '
501 #isp, start_prompt = InputSplitter(), '>>> '
502 isp, start_prompt = IPythonInputSplitter(), 'In> '
502 isp, start_prompt = IPythonInputSplitter(), 'In> '
503
503
504 autoindent = True
504 autoindent = True
505 #autoindent = False
505 #autoindent = False
506
506
507 try:
507 try:
508 while True:
508 while True:
509 prompt = start_prompt
509 prompt = start_prompt
510 while isp.push_accepts_more():
510 while isp.push_accepts_more():
511 indent = ' '*isp.indent_spaces
511 indent = ' '*isp.indent_spaces
512 if autoindent:
512 if autoindent:
513 line = indent + input(prompt+indent)
513 line = indent + input(prompt+indent)
514 else:
514 else:
515 line = input(prompt)
515 line = input(prompt)
516 isp.push(line)
516 isp.push(line)
517 prompt = '... '
517 prompt = '... '
518
518
519 # Here we just return input so we can use it in a test suite, but a
519 # Here we just return input so we can use it in a test suite, but a
520 # real interpreter would instead send it for execution somewhere.
520 # real interpreter would instead send it for execution somewhere.
521 #src = isp.source; raise EOFError # dbg
521 #src = isp.source; raise EOFError # dbg
522 raw = isp.source_raw
522 raw = isp.source_raw
523 src = isp.source_reset()
523 src = isp.source_reset()
524 print('Input source was:\n', src)
524 print('Input source was:\n', src)
525 print('Raw source was:\n', raw)
525 print('Raw source was:\n', raw)
526 except EOFError:
526 except EOFError:
527 print('Bye')
527 print('Bye')
528
528
529 # Tests for cell magics support
529 # Tests for cell magics support
530
530
531 def test_last_blank():
531 def test_last_blank():
532 nt.assert_false(isp.last_blank(''))
532 nt.assert_false(isp.last_blank(''))
533 nt.assert_false(isp.last_blank('abc'))
533 nt.assert_false(isp.last_blank('abc'))
534 nt.assert_false(isp.last_blank('abc\n'))
534 nt.assert_false(isp.last_blank('abc\n'))
535 nt.assert_false(isp.last_blank('abc\na'))
535 nt.assert_false(isp.last_blank('abc\na'))
536
536
537 nt.assert_true(isp.last_blank('\n'))
537 nt.assert_true(isp.last_blank('\n'))
538 nt.assert_true(isp.last_blank('\n '))
538 nt.assert_true(isp.last_blank('\n '))
539 nt.assert_true(isp.last_blank('abc\n '))
539 nt.assert_true(isp.last_blank('abc\n '))
540 nt.assert_true(isp.last_blank('abc\n\n'))
540 nt.assert_true(isp.last_blank('abc\n\n'))
541 nt.assert_true(isp.last_blank('abc\nd\n\n'))
541 nt.assert_true(isp.last_blank('abc\nd\n\n'))
542 nt.assert_true(isp.last_blank('abc\nd\ne\n\n'))
542 nt.assert_true(isp.last_blank('abc\nd\ne\n\n'))
543 nt.assert_true(isp.last_blank('abc \n \n \n\n'))
543 nt.assert_true(isp.last_blank('abc \n \n \n\n'))
544
544
545
545
546 def test_last_two_blanks():
546 def test_last_two_blanks():
547 nt.assert_false(isp.last_two_blanks(''))
547 nt.assert_false(isp.last_two_blanks(''))
548 nt.assert_false(isp.last_two_blanks('abc'))
548 nt.assert_false(isp.last_two_blanks('abc'))
549 nt.assert_false(isp.last_two_blanks('abc\n'))
549 nt.assert_false(isp.last_two_blanks('abc\n'))
550 nt.assert_false(isp.last_two_blanks('abc\n\na'))
550 nt.assert_false(isp.last_two_blanks('abc\n\na'))
551 nt.assert_false(isp.last_two_blanks('abc\n \n'))
551 nt.assert_false(isp.last_two_blanks('abc\n \n'))
552 nt.assert_false(isp.last_two_blanks('abc\n\n'))
552 nt.assert_false(isp.last_two_blanks('abc\n\n'))
553
553
554 nt.assert_true(isp.last_two_blanks('\n\n'))
554 nt.assert_true(isp.last_two_blanks('\n\n'))
555 nt.assert_true(isp.last_two_blanks('\n\n '))
555 nt.assert_true(isp.last_two_blanks('\n\n '))
556 nt.assert_true(isp.last_two_blanks('\n \n'))
556 nt.assert_true(isp.last_two_blanks('\n \n'))
557 nt.assert_true(isp.last_two_blanks('abc\n\n '))
557 nt.assert_true(isp.last_two_blanks('abc\n\n '))
558 nt.assert_true(isp.last_two_blanks('abc\n\n\n'))
558 nt.assert_true(isp.last_two_blanks('abc\n\n\n'))
559 nt.assert_true(isp.last_two_blanks('abc\n\n \n'))
559 nt.assert_true(isp.last_two_blanks('abc\n\n \n'))
560 nt.assert_true(isp.last_two_blanks('abc\n\n \n '))
560 nt.assert_true(isp.last_two_blanks('abc\n\n \n '))
561 nt.assert_true(isp.last_two_blanks('abc\n\n \n \n'))
561 nt.assert_true(isp.last_two_blanks('abc\n\n \n \n'))
562 nt.assert_true(isp.last_two_blanks('abc\nd\n\n\n'))
562 nt.assert_true(isp.last_two_blanks('abc\nd\n\n\n'))
563 nt.assert_true(isp.last_two_blanks('abc\nd\ne\nf\n\n\n'))
563 nt.assert_true(isp.last_two_blanks('abc\nd\ne\nf\n\n\n'))
564
564
565
565
566 class CellMagicsCommon(object):
566 class CellMagicsCommon(object):
567
567
568 def test_whole_cell(self):
568 def test_whole_cell(self):
569 src = "%%cellm line\nbody\n"
569 src = "%%cellm line\nbody\n"
570 out = self.sp.transform_cell(src)
570 out = self.sp.transform_cell(src)
571 ref = u"get_ipython().run_cell_magic({u}'cellm', {u}'line', {u}'body')\n"
571 ref = u"get_ipython().run_cell_magic({u}'cellm', {u}'line', {u}'body')\n"
572 nt.assert_equal(out, py3compat.u_format(ref))
572 nt.assert_equal(out, py3compat.u_format(ref))
573
573
574 def test_cellmagic_help(self):
574 def test_cellmagic_help(self):
575 self.sp.push('%%cellm?')
575 self.sp.push('%%cellm?')
576 nt.assert_false(self.sp.push_accepts_more())
576 nt.assert_false(self.sp.push_accepts_more())
577
577
578 def tearDown(self):
578 def tearDown(self):
579 self.sp.reset()
579 self.sp.reset()
580
580
581
581
582 class CellModeCellMagics(CellMagicsCommon, unittest.TestCase):
582 class CellModeCellMagics(CellMagicsCommon, unittest.TestCase):
583 sp = isp.IPythonInputSplitter(line_input_checker=False)
583 sp = isp.IPythonInputSplitter(line_input_checker=False)
584
584
585 def test_incremental(self):
585 def test_incremental(self):
586 sp = self.sp
586 sp = self.sp
587 sp.push('%%cellm firstline\n')
587 sp.push('%%cellm firstline\n')
588 nt.assert_true(sp.push_accepts_more()) #1
588 nt.assert_true(sp.push_accepts_more()) #1
589 sp.push('line2\n')
589 sp.push('line2\n')
590 nt.assert_true(sp.push_accepts_more()) #2
590 nt.assert_true(sp.push_accepts_more()) #2
591 sp.push('\n')
591 sp.push('\n')
592 # This should accept a blank line and carry on until the cell is reset
592 # This should accept a blank line and carry on until the cell is reset
593 nt.assert_true(sp.push_accepts_more()) #3
593 nt.assert_true(sp.push_accepts_more()) #3
594
594
595 def test_no_strip_coding(self):
595 def test_no_strip_coding(self):
596 src = '\n'.join([
596 src = '\n'.join([
597 '%%writefile foo.py',
597 '%%writefile foo.py',
598 '# coding: utf-8',
598 '# coding: utf-8',
599 'print(u"üñîçø∂é")',
599 'print(u"üñîçø∂é")',
600 ])
600 ])
601 out = self.sp.transform_cell(src)
601 out = self.sp.transform_cell(src)
602 nt.assert_in('# coding: utf-8', out)
602 nt.assert_in('# coding: utf-8', out)
603
603
604
604
605 class LineModeCellMagics(CellMagicsCommon, unittest.TestCase):
605 class LineModeCellMagics(CellMagicsCommon, unittest.TestCase):
606 sp = isp.IPythonInputSplitter(line_input_checker=True)
606 sp = isp.IPythonInputSplitter(line_input_checker=True)
607
607
608 def test_incremental(self):
608 def test_incremental(self):
609 sp = self.sp
609 sp = self.sp
610 sp.push('%%cellm line2\n')
610 sp.push('%%cellm line2\n')
611 nt.assert_true(sp.push_accepts_more()) #1
611 nt.assert_true(sp.push_accepts_more()) #1
612 sp.push('\n')
612 sp.push('\n')
613 # In this case, a blank line should end the cell magic
613 # In this case, a blank line should end the cell magic
614 nt.assert_false(sp.push_accepts_more()) #2
614 nt.assert_false(sp.push_accepts_more()) #2
615
616 indentation_samples = [
617 ('a = 1', 0),
618 ('for a in b:', 4),
619 ('def f():', 4),
620 ('def f(): #comment', 4),
621 ('a = ":#not a comment"', 0),
622 ('def f():\n a = 1', 4),
623 ('def f():\n return 1', 0),
624 ('for a in b:\n'
625 ' if a < 0:'
626 ' continue', 3),
627 ('a = {', 4),
628 ('a = {\n'
629 ' 1,', 5),
630 ('b = """123', 0),
631 ('', 0),
632 ]
633
634 def test_find_next_indent():
635 for code, exp in indentation_samples:
636 res = isp.find_next_indent(code)
637 msg = "{!r} != {!r} (expected)\n Code: {!r}".format(res, exp, code)
638 assert res == exp, msg
General Comments 0
You need to be logged in to leave comments. Login now