##// END OF EJS Templates
New tests related to issue #142.
David Warde-Farley -
Show More
@@ -1,618 +1,647 b''
1 1 # -*- coding: utf-8 -*-
2 2 """Tests for the inputsplitter module.
3 3
4 4 Authors
5 5 -------
6 6 * Fernando Perez
7 7 * Robert Kern
8 8 """
9 9 #-----------------------------------------------------------------------------
10 10 # Copyright (C) 2010 The IPython Development Team
11 11 #
12 12 # Distributed under the terms of the BSD License. The full license is in
13 13 # the file COPYING, distributed as part of this software.
14 14 #-----------------------------------------------------------------------------
15 15
16 16 #-----------------------------------------------------------------------------
17 17 # Imports
18 18 #-----------------------------------------------------------------------------
19 19 # stdlib
20 20 import unittest
21 21 import sys
22 22
23 23 # Third party
24 24 import nose.tools as nt
25 25
26 26 # Our own
27 27 from IPython.core import inputsplitter as isp
28 28
29 29 #-----------------------------------------------------------------------------
30 30 # Semi-complete examples (also used as tests)
31 31 #-----------------------------------------------------------------------------
32 32
33 33 # Note: at the bottom, there's a slightly more complete version of this that
34 34 # can be useful during development of code here.
35 35
36 36 def mini_interactive_loop(input_func):
37 37 """Minimal example of the logic of an interactive interpreter loop.
38 38
39 39 This serves as an example, and it is used by the test system with a fake
40 40 raw_input that simulates interactive input."""
41 41
42 42 from IPython.core.inputsplitter import InputSplitter
43 43
44 44 isp = InputSplitter()
45 45 # In practice, this input loop would be wrapped in an outside loop to read
46 46 # input indefinitely, until some exit/quit command was issued. Here we
47 47 # only illustrate the basic inner loop.
48 48 while isp.push_accepts_more():
49 49 indent = ' '*isp.indent_spaces
50 50 prompt = '>>> ' + indent
51 51 line = indent + input_func(prompt)
52 52 isp.push(line)
53 53
54 54 # Here we just return input so we can use it in a test suite, but a real
55 55 # interpreter would instead send it for execution somewhere.
56 56 src = isp.source_reset()
57 57 #print 'Input source was:\n', src # dbg
58 58 return src
59 59
60 60 #-----------------------------------------------------------------------------
61 61 # Test utilities, just for local use
62 62 #-----------------------------------------------------------------------------
63 63
64 64 def assemble(block):
65 65 """Assemble a block into multi-line sub-blocks."""
66 66 return ['\n'.join(sub_block)+'\n' for sub_block in block]
67 67
68 68
69 69 def pseudo_input(lines):
70 70 """Return a function that acts like raw_input but feeds the input list."""
71 71 ilines = iter(lines)
72 72 def raw_in(prompt):
73 73 try:
74 74 return next(ilines)
75 75 except StopIteration:
76 76 return ''
77 77 return raw_in
78 78
79 79 #-----------------------------------------------------------------------------
80 80 # Tests
81 81 #-----------------------------------------------------------------------------
82 82 def test_spaces():
83 83 tests = [('', 0),
84 84 (' ', 1),
85 85 ('\n', 0),
86 86 (' \n', 1),
87 87 ('x', 0),
88 88 (' x', 1),
89 89 (' x',2),
90 90 (' x',4),
91 91 # Note: tabs are counted as a single whitespace!
92 92 ('\tx', 1),
93 93 ('\t x', 2),
94 94 ]
95 95
96 96 for s, nsp in tests:
97 97 nt.assert_equal(isp.num_ini_spaces(s), nsp)
98 98
99 99
100 100 def test_remove_comments():
101 101 tests = [('text', 'text'),
102 102 ('text # comment', 'text '),
103 103 ('text # comment\n', 'text \n'),
104 104 ('text # comment \n', 'text \n'),
105 105 ('line # c \nline\n','line \nline\n'),
106 106 ('line # c \nline#c2 \nline\nline #c\n\n',
107 107 'line \nline\nline\nline \n\n'),
108 108 ]
109 109
110 110 for inp, out in tests:
111 111 nt.assert_equal(isp.remove_comments(inp), out)
112 112
113 113
114 114 def test_get_input_encoding():
115 115 encoding = isp.get_input_encoding()
116 116 nt.assert_true(isinstance(encoding, basestring))
117 117 # simple-minded check that at least encoding a simple string works with the
118 118 # encoding we got.
119 119 nt.assert_equal('test'.encode(encoding), 'test')
120 120
121 121
122 122 class NoInputEncodingTestCase(unittest.TestCase):
123 123 def setUp(self):
124 124 self.old_stdin = sys.stdin
125 125 class X: pass
126 126 fake_stdin = X()
127 127 sys.stdin = fake_stdin
128 128
129 129 def test(self):
130 130 # Verify that if sys.stdin has no 'encoding' attribute we do the right
131 131 # thing
132 132 enc = isp.get_input_encoding()
133 133 self.assertEqual(enc, 'ascii')
134 134
135 135 def tearDown(self):
136 136 sys.stdin = self.old_stdin
137 137
138 138
139 139 class InputSplitterTestCase(unittest.TestCase):
140 140 def setUp(self):
141 141 self.isp = isp.InputSplitter()
142 142
143 143 def test_reset(self):
144 144 isp = self.isp
145 145 isp.push('x=1')
146 146 isp.reset()
147 147 self.assertEqual(isp._buffer, [])
148 148 self.assertEqual(isp.indent_spaces, 0)
149 149 self.assertEqual(isp.source, '')
150 150 self.assertEqual(isp.code, None)
151 151 self.assertEqual(isp._is_complete, False)
152 152
153 153 def test_source(self):
154 154 self.isp._store('1')
155 155 self.isp._store('2')
156 156 self.assertEqual(self.isp.source, '1\n2\n')
157 157 self.assertTrue(len(self.isp._buffer)>0)
158 158 self.assertEqual(self.isp.source_reset(), '1\n2\n')
159 159 self.assertEqual(self.isp._buffer, [])
160 160 self.assertEqual(self.isp.source, '')
161 161
162 162 def test_indent(self):
163 163 isp = self.isp # shorthand
164 164 isp.push('x=1')
165 165 self.assertEqual(isp.indent_spaces, 0)
166 166 isp.push('if 1:\n x=1')
167 167 self.assertEqual(isp.indent_spaces, 4)
168 168 isp.push('y=2\n')
169 169 self.assertEqual(isp.indent_spaces, 0)
170 170
171 171 def test_indent2(self):
172 172 # In cell mode, inputs must be fed in whole blocks, so skip this test
173 173 if self.isp.input_mode == 'cell': return
174 174
175 175 isp = self.isp
176 176 isp.push('if 1:')
177 177 self.assertEqual(isp.indent_spaces, 4)
178 178 isp.push(' x=1')
179 179 self.assertEqual(isp.indent_spaces, 4)
180 180 # Blank lines shouldn't change the indent level
181 181 isp.push(' '*2)
182 182 self.assertEqual(isp.indent_spaces, 4)
183 183
184 184 def test_indent3(self):
185 185 # In cell mode, inputs must be fed in whole blocks, so skip this test
186 186 if self.isp.input_mode == 'cell': return
187 187
188 188 isp = self.isp
189 189 # When a multiline statement contains parens or multiline strings, we
190 190 # shouldn't get confused.
191 191 isp.push("if 1:")
192 192 isp.push(" x = (1+\n 2)")
193 193 self.assertEqual(isp.indent_spaces, 4)
194 194
195 def test_dedent(self):
195 def test_dedent_pass(self):
196 196 isp = self.isp # shorthand
197 isp.push('if 1:')
197 # should NOT cause dedent
198 isp.push('if 1:\n passes = 5')
198 199 self.assertEqual(isp.indent_spaces, 4)
199 isp.push(' pass')
200 isp.push('if 1:\n pass')
200 201 self.assertEqual(isp.indent_spaces, 0)
201
202 isp.push('if 1:\n pass ')
203 self.assertEqual(isp.indent_spaces, 0)
204
205 def test_dedent_raise(self):
206 isp = self.isp # shorthand
207 # should NOT cause dedent
208 isp.push('if 1:\n raised = 4')
209 self.assertEqual(isp.indent_spaces, 4)
210 isp.push('if 1:\n raise TypeError()')
211 self.assertEqual(isp.indent_spaces, 0)
212 isp.push('if 1:\n raise')
213 self.assertEqual(isp.indent_spaces, 0)
214 isp.push('if 1:\n raise ')
215 self.assertEqual(isp.indent_spaces, 0)
216
217 def test_dedent_return(self):
218 isp = self.isp # shorthand
219 # should NOT cause dedent
220 isp.push('if 1:\n returning = 4')
221 self.assertEqual(isp.indent_spaces, 4)
222 isp.push('if 1:\n return 5 + 493')
223 self.assertEqual(isp.indent_spaces, 0)
224 isp.push('if 1:\n return')
225 self.assertEqual(isp.indent_spaces, 0)
226 isp.push('if 1:\n return ')
227 self.assertEqual(isp.indent_spaces, 0)
228 isp.push('if 1:\n return(0)')
229 self.assertEqual(isp.indent_spaces, 0)
230
202 231 def test_push(self):
203 232 isp = self.isp
204 233 self.assertTrue(isp.push('x=1'))
205 234
206 235 def test_push2(self):
207 236 isp = self.isp
208 237 self.assertFalse(isp.push('if 1:'))
209 238 for line in [' x=1', '# a comment', ' y=2']:
210 239 self.assertTrue(isp.push(line))
211 240
212 241 def test_replace_mode(self):
213 242 isp = self.isp
214 243 isp.input_mode = 'cell'
215 244 isp.push('x=1')
216 245 self.assertEqual(isp.source, 'x=1\n')
217 246 isp.push('x=2')
218 247 self.assertEqual(isp.source, 'x=2\n')
219 248
220 249 def test_push_accepts_more(self):
221 250 isp = self.isp
222 251 isp.push('x=1')
223 252 self.assertFalse(isp.push_accepts_more())
224 253
225 254 def test_push_accepts_more2(self):
226 255 # In cell mode, inputs must be fed in whole blocks, so skip this test
227 256 if self.isp.input_mode == 'cell': return
228 257
229 258 isp = self.isp
230 259 isp.push('if 1:')
231 260 self.assertTrue(isp.push_accepts_more())
232 261 isp.push(' x=1')
233 262 self.assertTrue(isp.push_accepts_more())
234 263 isp.push('')
235 264 self.assertFalse(isp.push_accepts_more())
236 265
237 266 def test_push_accepts_more3(self):
238 267 isp = self.isp
239 268 isp.push("x = (2+\n3)")
240 269 self.assertFalse(isp.push_accepts_more())
241 270
242 271 def test_push_accepts_more4(self):
243 272 # In cell mode, inputs must be fed in whole blocks, so skip this test
244 273 if self.isp.input_mode == 'cell': return
245 274
246 275 isp = self.isp
247 276 # When a multiline statement contains parens or multiline strings, we
248 277 # shouldn't get confused.
249 278 # FIXME: we should be able to better handle de-dents in statements like
250 279 # multiline strings and multiline expressions (continued with \ or
251 280 # parens). Right now we aren't handling the indentation tracking quite
252 281 # correctly with this, though in practice it may not be too much of a
253 282 # problem. We'll need to see.
254 283 isp.push("if 1:")
255 284 isp.push(" x = (2+")
256 285 isp.push(" 3)")
257 286 self.assertTrue(isp.push_accepts_more())
258 287 isp.push(" y = 3")
259 288 self.assertTrue(isp.push_accepts_more())
260 289 isp.push('')
261 290 self.assertFalse(isp.push_accepts_more())
262 291
263 292 def test_push_accepts_more5(self):
264 293 # In cell mode, inputs must be fed in whole blocks, so skip this test
265 294 if self.isp.input_mode == 'cell': return
266 295
267 296 isp = self.isp
268 297 isp.push('try:')
269 298 isp.push(' a = 5')
270 299 isp.push('except:')
271 300 isp.push(' raise')
272 301 self.assertTrue(isp.push_accepts_more())
273 302
274 303 def test_continuation(self):
275 304 isp = self.isp
276 305 isp.push("import os, \\")
277 306 self.assertTrue(isp.push_accepts_more())
278 307 isp.push("sys")
279 308 self.assertFalse(isp.push_accepts_more())
280 309
281 310 def test_syntax_error(self):
282 311 isp = self.isp
283 312 # Syntax errors immediately produce a 'ready' block, so the invalid
284 313 # Python can be sent to the kernel for evaluation with possible ipython
285 314 # special-syntax conversion.
286 315 isp.push('run foo')
287 316 self.assertFalse(isp.push_accepts_more())
288 317
289 318 def test_unicode(self):
290 319 self.isp.push(u"PΓ©rez")
291 320 self.isp.push(u'\xc3\xa9')
292 321 self.isp.push(u"u'\xc3\xa9'")
293 322
294 323 class InteractiveLoopTestCase(unittest.TestCase):
295 324 """Tests for an interactive loop like a python shell.
296 325 """
297 326 def check_ns(self, lines, ns):
298 327 """Validate that the given input lines produce the resulting namespace.
299 328
300 329 Note: the input lines are given exactly as they would be typed in an
301 330 auto-indenting environment, as mini_interactive_loop above already does
302 331 auto-indenting and prepends spaces to the input.
303 332 """
304 333 src = mini_interactive_loop(pseudo_input(lines))
305 334 test_ns = {}
306 335 exec src in test_ns
307 336 # We can't check that the provided ns is identical to the test_ns,
308 337 # because Python fills test_ns with extra keys (copyright, etc). But
309 338 # we can check that the given dict is *contained* in test_ns
310 339 for k,v in ns.iteritems():
311 340 self.assertEqual(test_ns[k], v)
312 341
313 342 def test_simple(self):
314 343 self.check_ns(['x=1'], dict(x=1))
315 344
316 345 def test_simple2(self):
317 346 self.check_ns(['if 1:', 'x=2'], dict(x=2))
318 347
319 348 def test_xy(self):
320 349 self.check_ns(['x=1; y=2'], dict(x=1, y=2))
321 350
322 351 def test_abc(self):
323 352 self.check_ns(['if 1:','a=1','b=2','c=3'], dict(a=1, b=2, c=3))
324 353
325 354 def test_multi(self):
326 355 self.check_ns(['x =(1+','1+','2)'], dict(x=4))
327 356
328 357
329 358 def test_LineInfo():
330 359 """Simple test for LineInfo construction and str()"""
331 360 linfo = isp.LineInfo(' %cd /home')
332 361 nt.assert_equals(str(linfo), 'LineInfo [ |%|cd|/home]')
333 362
334 363
335 364 def test_split_user_input():
336 365 """Unicode test - split_user_input already has good doctests"""
337 366 line = u"PΓ©rez Fernando"
338 367 parts = isp.split_user_input(line)
339 368 parts_expected = (u'', u'', u'', line)
340 369 nt.assert_equal(parts, parts_expected)
341 370
342 371
343 372 # Transformer tests
344 373 def transform_checker(tests, func):
345 374 """Utility to loop over test inputs"""
346 375 for inp, tr in tests:
347 376 nt.assert_equals(func(inp), tr)
348 377
349 378 # Data for all the syntax tests in the form of lists of pairs of
350 379 # raw/transformed input. We store it here as a global dict so that we can use
351 380 # it both within single-function tests and also to validate the behavior of the
352 381 # larger objects
353 382
354 383 syntax = \
355 384 dict(assign_system =
356 385 [('a =! ls', 'a = get_ipython().getoutput(u"ls")'),
357 386 ('b = !ls', 'b = get_ipython().getoutput(u"ls")'),
358 387 ('x=1', 'x=1'), # normal input is unmodified
359 388 (' ',' '), # blank lines are kept intact
360 389 ],
361 390
362 391 assign_magic =
363 392 [('a =% who', 'a = get_ipython().magic(u"who")'),
364 393 ('b = %who', 'b = get_ipython().magic(u"who")'),
365 394 ('x=1', 'x=1'), # normal input is unmodified
366 395 (' ',' '), # blank lines are kept intact
367 396 ],
368 397
369 398 classic_prompt =
370 399 [('>>> x=1', 'x=1'),
371 400 ('x=1', 'x=1'), # normal input is unmodified
372 401 (' ', ' '), # blank lines are kept intact
373 402 ('... ', ''), # continuation prompts
374 403 ],
375 404
376 405 ipy_prompt =
377 406 [('In [1]: x=1', 'x=1'),
378 407 ('x=1', 'x=1'), # normal input is unmodified
379 408 (' ',' '), # blank lines are kept intact
380 409 (' ....: ', ''), # continuation prompts
381 410 ],
382 411
383 412 # Tests for the escape transformer to leave normal code alone
384 413 escaped_noesc =
385 414 [ (' ', ' '),
386 415 ('x=1', 'x=1'),
387 416 ],
388 417
389 418 # System calls
390 419 escaped_shell =
391 420 [ ('!ls', 'get_ipython().system(u"ls")'),
392 421 # Double-escape shell, this means to capture the output of the
393 422 # subprocess and return it
394 423 ('!!ls', 'get_ipython().getoutput(u"ls")'),
395 424 ],
396 425
397 426 # Help/object info
398 427 escaped_help =
399 428 [ ('?', 'get_ipython().show_usage()'),
400 429 ('?x1', 'get_ipython().magic(u"pinfo x1")'),
401 430 ('??x2', 'get_ipython().magic(u"pinfo2 x2")'),
402 431 ('x3?', 'get_ipython().magic(u"pinfo x3")'),
403 432 ('x4??', 'get_ipython().magic(u"pinfo2 x4")'),
404 433 ('%hist?', 'get_ipython().magic(u"pinfo %hist")'),
405 434 ('f*?', 'get_ipython().magic(u"psearch f*")'),
406 435 ('ax.*aspe*?', 'get_ipython().magic(u"psearch ax.*aspe*")'),
407 436 ],
408 437
409 438 # Explicit magic calls
410 439 escaped_magic =
411 440 [ ('%cd', 'get_ipython().magic(u"cd")'),
412 441 ('%cd /home', 'get_ipython().magic(u"cd /home")'),
413 442 (' %magic', ' get_ipython().magic(u"magic")'),
414 443 ],
415 444
416 445 # Quoting with separate arguments
417 446 escaped_quote =
418 447 [ (',f', 'f("")'),
419 448 (',f x', 'f("x")'),
420 449 (' ,f y', ' f("y")'),
421 450 (',f a b', 'f("a", "b")'),
422 451 ],
423 452
424 453 # Quoting with single argument
425 454 escaped_quote2 =
426 455 [ (';f', 'f("")'),
427 456 (';f x', 'f("x")'),
428 457 (' ;f y', ' f("y")'),
429 458 (';f a b', 'f("a b")'),
430 459 ],
431 460
432 461 # Simply apply parens
433 462 escaped_paren =
434 463 [ ('/f', 'f()'),
435 464 ('/f x', 'f(x)'),
436 465 (' /f y', ' f(y)'),
437 466 ('/f a b', 'f(a, b)'),
438 467 ],
439 468
440 469 )
441 470
442 471 # multiline syntax examples. Each of these should be a list of lists, with
443 472 # each entry itself having pairs of raw/transformed input. The union (with
444 473 # '\n'.join() of the transformed inputs is what the splitter should produce
445 474 # when fed the raw lines one at a time via push.
446 475 syntax_ml = \
447 476 dict(classic_prompt =
448 477 [ [('>>> for i in range(10):','for i in range(10):'),
449 478 ('... print i',' print i'),
450 479 ('... ', ''),
451 480 ],
452 481 ],
453 482
454 483 ipy_prompt =
455 484 [ [('In [24]: for i in range(10):','for i in range(10):'),
456 485 (' ....: print i',' print i'),
457 486 (' ....: ', ''),
458 487 ],
459 488 ],
460 489 )
461 490
462 491
463 492 def test_assign_system():
464 493 transform_checker(syntax['assign_system'], isp.transform_assign_system)
465 494
466 495
467 496 def test_assign_magic():
468 497 transform_checker(syntax['assign_magic'], isp.transform_assign_magic)
469 498
470 499
471 500 def test_classic_prompt():
472 501 transform_checker(syntax['classic_prompt'], isp.transform_classic_prompt)
473 502 for example in syntax_ml['classic_prompt']:
474 503 transform_checker(example, isp.transform_classic_prompt)
475 504
476 505
477 506 def test_ipy_prompt():
478 507 transform_checker(syntax['ipy_prompt'], isp.transform_ipy_prompt)
479 508 for example in syntax_ml['ipy_prompt']:
480 509 transform_checker(example, isp.transform_ipy_prompt)
481 510
482 511
483 512 def test_escaped_noesc():
484 513 transform_checker(syntax['escaped_noesc'], isp.transform_escaped)
485 514
486 515
487 516 def test_escaped_shell():
488 517 transform_checker(syntax['escaped_shell'], isp.transform_escaped)
489 518
490 519
491 520 def test_escaped_help():
492 521 transform_checker(syntax['escaped_help'], isp.transform_escaped)
493 522
494 523
495 524 def test_escaped_magic():
496 525 transform_checker(syntax['escaped_magic'], isp.transform_escaped)
497 526
498 527
499 528 def test_escaped_quote():
500 529 transform_checker(syntax['escaped_quote'], isp.transform_escaped)
501 530
502 531
503 532 def test_escaped_quote2():
504 533 transform_checker(syntax['escaped_quote2'], isp.transform_escaped)
505 534
506 535
507 536 def test_escaped_paren():
508 537 transform_checker(syntax['escaped_paren'], isp.transform_escaped)
509 538
510 539
511 540 class IPythonInputTestCase(InputSplitterTestCase):
512 541 """By just creating a new class whose .isp is a different instance, we
513 542 re-run the same test battery on the new input splitter.
514 543
515 544 In addition, this runs the tests over the syntax and syntax_ml dicts that
516 545 were tested by individual functions, as part of the OO interface.
517 546
518 547 It also makes some checks on the raw buffer storage.
519 548 """
520 549
521 550 def setUp(self):
522 551 self.isp = isp.IPythonInputSplitter(input_mode='line')
523 552
524 553 def test_syntax(self):
525 554 """Call all single-line syntax tests from the main object"""
526 555 isp = self.isp
527 556 for example in syntax.itervalues():
528 557 for raw, out_t in example:
529 558 if raw.startswith(' '):
530 559 continue
531 560
532 561 isp.push(raw)
533 562 out, out_raw = isp.source_raw_reset()
534 563 self.assertEqual(out.rstrip(), out_t)
535 564 self.assertEqual(out_raw.rstrip(), raw.rstrip())
536 565
537 566 def test_syntax_multiline(self):
538 567 isp = self.isp
539 568 for example in syntax_ml.itervalues():
540 569 out_t_parts = []
541 570 raw_parts = []
542 571 for line_pairs in example:
543 572 for lraw, out_t_part in line_pairs:
544 573 isp.push(lraw)
545 574 out_t_parts.append(out_t_part)
546 575 raw_parts.append(lraw)
547 576
548 577 out, out_raw = isp.source_raw_reset()
549 578 out_t = '\n'.join(out_t_parts).rstrip()
550 579 raw = '\n'.join(raw_parts).rstrip()
551 580 self.assertEqual(out.rstrip(), out_t)
552 581 self.assertEqual(out_raw.rstrip(), raw)
553 582
554 583
555 584 class BlockIPythonInputTestCase(IPythonInputTestCase):
556 585
557 586 # Deactivate tests that don't make sense for the block mode
558 587 test_push3 = test_split = lambda s: None
559 588
560 589 def setUp(self):
561 590 self.isp = isp.IPythonInputSplitter(input_mode='cell')
562 591
563 592 def test_syntax_multiline(self):
564 593 isp = self.isp
565 594 for example in syntax_ml.itervalues():
566 595 raw_parts = []
567 596 out_t_parts = []
568 597 for line_pairs in example:
569 598 for raw, out_t_part in line_pairs:
570 599 raw_parts.append(raw)
571 600 out_t_parts.append(out_t_part)
572 601
573 602 raw = '\n'.join(raw_parts)
574 603 out_t = '\n'.join(out_t_parts)
575 604
576 605 isp.push(raw)
577 606 out, out_raw = isp.source_raw_reset()
578 607 # Match ignoring trailing whitespace
579 608 self.assertEqual(out.rstrip(), out_t.rstrip())
580 609 self.assertEqual(out_raw.rstrip(), raw.rstrip())
581 610
582 611
583 612 #-----------------------------------------------------------------------------
584 613 # Main - use as a script, mostly for developer experiments
585 614 #-----------------------------------------------------------------------------
586 615
587 616 if __name__ == '__main__':
588 617 # A simple demo for interactive experimentation. This code will not get
589 618 # picked up by any test suite.
590 619 from IPython.core.inputsplitter import InputSplitter, IPythonInputSplitter
591 620
592 621 # configure here the syntax to use, prompt and whether to autoindent
593 622 #isp, start_prompt = InputSplitter(), '>>> '
594 623 isp, start_prompt = IPythonInputSplitter(), 'In> '
595 624
596 625 autoindent = True
597 626 #autoindent = False
598 627
599 628 try:
600 629 while True:
601 630 prompt = start_prompt
602 631 while isp.push_accepts_more():
603 632 indent = ' '*isp.indent_spaces
604 633 if autoindent:
605 634 line = indent + raw_input(prompt+indent)
606 635 else:
607 636 line = raw_input(prompt)
608 637 isp.push(line)
609 638 prompt = '... '
610 639
611 640 # Here we just return input so we can use it in a test suite, but a
612 641 # real interpreter would instead send it for execution somewhere.
613 642 #src = isp.source; raise EOFError # dbg
614 643 src, raw = isp.source_raw_reset()
615 644 print 'Input source was:\n', src
616 645 print 'Raw source was:\n', raw
617 646 except EOFError:
618 647 print 'Bye'
General Comments 0
You need to be logged in to leave comments. Login now