def test_indent(self): isp = self.isp # shorthand isp.push('x=1') self.assertEqual(isp.get_indent_spaces(), 0) isp.push('if 1:\n x=1') self.assertEqual(isp.get_indent_spaces(), 4) isp.push('y=2\n') self.assertEqual(isp.get_indent_spaces(), 0)
def test_indent(self): isp = self.isp # shorthand isp.push("x=1") self.assertEqual(isp.get_indent_spaces(), 0) isp.push("if 1:\n x=1") self.assertEqual(isp.get_indent_spaces(), 4) isp.push("y=2\n") self.assertEqual(isp.get_indent_spaces(), 0)
def test_indent2(self): isp = self.isp isp.push('if 1:') self.assertEqual(isp.get_indent_spaces(), 4) isp.push(' x=1') self.assertEqual(isp.get_indent_spaces(), 4) # Blank lines shouldn't change the indent level isp.push(' ' * 2) self.assertEqual(isp.get_indent_spaces(), 4)
def test_dedent_continue(self): isp = self.isp # shorthand # should NOT cause dedent isp.push('while 1:\n continues = 5') self.assertEqual(isp.get_indent_spaces(), 4) isp.push('while 1:\n continue') self.assertEqual(isp.get_indent_spaces(), 0) isp.push('while 1:\n continue ') self.assertEqual(isp.get_indent_spaces(), 0)
def test_dedent_pass(self): isp = self.isp # shorthand # should NOT cause dedent isp.push('if 1:\n passes = 5') self.assertEqual(isp.get_indent_spaces(), 4) isp.push('if 1:\n pass') self.assertEqual(isp.get_indent_spaces(), 0) isp.push('if 1:\n pass ') self.assertEqual(isp.get_indent_spaces(), 0)
def test_indent2(self): isp = self.isp isp.push('if 1:') self.assertEqual(isp.get_indent_spaces(), 4) isp.push(' x=1') self.assertEqual(isp.get_indent_spaces(), 4) # Blank lines shouldn't change the indent level isp.push(' '*2) self.assertEqual(isp.get_indent_spaces(), 4)
def test_dedent_break(self): isp = self.isp # shorthand # should NOT cause dedent isp.push("while 1:\n breaks = 5") self.assertEqual(isp.get_indent_spaces(), 4) isp.push("while 1:\n break") self.assertEqual(isp.get_indent_spaces(), 0) isp.push("while 1:\n break ") self.assertEqual(isp.get_indent_spaces(), 0)
def test_indent4(self): isp = self.isp # whitespace after ':' should not screw up indent level isp.push('if 1: \n x=1') self.assertEqual(isp.get_indent_spaces(), 4) isp.push('y=2\n') self.assertEqual(isp.get_indent_spaces(), 0) isp.push('if 1:\t\n x=1') self.assertEqual(isp.get_indent_spaces(), 4) isp.push('y=2\n') self.assertEqual(isp.get_indent_spaces(), 0)
def test_dedent_raise(self): isp = self.isp # shorthand # should NOT cause dedent isp.push('if 1:\n raised = 4') self.assertEqual(isp.get_indent_spaces(), 4) isp.push('if 1:\n raise TypeError()') self.assertEqual(isp.get_indent_spaces(), 0) isp.push('if 1:\n raise') self.assertEqual(isp.get_indent_spaces(), 0) isp.push('if 1:\n raise ') self.assertEqual(isp.get_indent_spaces(), 0)
def test_dedent_return(self): isp = self.isp # shorthand # should NOT cause dedent isp.push('if 1:\n returning = 4') self.assertEqual(isp.get_indent_spaces(), 4) isp.push('if 1:\n return 5 + 493') self.assertEqual(isp.get_indent_spaces(), 0) isp.push('if 1:\n return') self.assertEqual(isp.get_indent_spaces(), 0) isp.push('if 1:\n return ') self.assertEqual(isp.get_indent_spaces(), 0) isp.push('if 1:\n return(0)') self.assertEqual(isp.get_indent_spaces(), 0)
def test_indent3(self): isp = self.isp # When a multiline statement contains parens or multiline strings, we # shouldn't get confused. isp.push("if 1:") isp.push(" x = (1+\n 2)") self.assertEqual(isp.get_indent_spaces(), 4)
def test_reset(self): isp = self.isp isp.push('x=1') isp.reset() self.assertEqual(isp._buffer, []) self.assertEqual(isp.get_indent_spaces(), 0) self.assertEqual(isp.source, '') self.assertEqual(isp.code, None) self.assertEqual(isp._is_complete, False)
def mini_interactive_loop(input_func): """Minimal example of the logic of an interactive interpreter loop. This serves as an example, and it is used by the test system with a fake raw_input that simulates interactive input.""" from IPython.core.inputsplitter import InputSplitter isp = InputSplitter() # In practice, this input loop would be wrapped in an outside loop to read # input indefinitely, until some exit/quit command was issued. Here we # only illustrate the basic inner loop. while isp.push_accepts_more(): indent = ' ' * isp.get_indent_spaces() prompt = '>>> ' + indent line = indent + input_func(prompt) isp.push(line) # Here we just return input so we can use it in a test suite, but a real # interpreter would instead send it for execution somewhere. src = isp.source_reset() #print 'Input source was:\n', src # dbg return src
def mini_interactive_loop(input_func): """Minimal example of the logic of an interactive interpreter loop. This serves as an example, and it is used by the test system with a fake raw_input that simulates interactive input.""" from IPython.core.inputsplitter import InputSplitter isp = InputSplitter() # In practice, this input loop would be wrapped in an outside loop to read # input indefinitely, until some exit/quit command was issued. Here we # only illustrate the basic inner loop. while isp.push_accepts_more(): indent = ' '*isp.get_indent_spaces() prompt = '>>> ' + indent line = indent + input_func(prompt) isp.push(line) # Here we just return input so we can use it in a test suite, but a real # interpreter would instead send it for execution somewhere. src = isp.source_reset() #print 'Input source was:\n', src # dbg return src
# A simple demo for interactive experimentation. This code will not get # picked up by any test suite. from IPython.core.inputsplitter import IPythonInputSplitter # configure here the syntax to use, prompt and whether to autoindent # isp, start_prompt = InputSplitter(), '>>> ' isp, start_prompt = IPythonInputSplitter(), "In> " autoindent = True # autoindent = False try: while True: prompt = start_prompt while isp.push_accepts_more(): indent = " " * isp.get_indent_spaces() if autoindent: line = indent + input(prompt + indent) else: line = input(prompt) isp.push(line) prompt = "... " # Here we just return input so we can use it in a test suite, but a # real interpreter would instead send it for execution somewhere. # src = isp.source; raise EOFError # dbg raw = isp.source_raw src = isp.source_reset() print("Input source was:\n", src) print("Raw source was:\n", raw) except EOFError:
# A simple demo for interactive experimentation. This code will not get # picked up by any test suite. from IPython.core.inputsplitter import IPythonInputSplitter # configure here the syntax to use, prompt and whether to autoindent #isp, start_prompt = InputSplitter(), '>>> ' isp, start_prompt = IPythonInputSplitter(), 'In> ' autoindent = True #autoindent = False try: while True: prompt = start_prompt while isp.push_accepts_more(): indent = ' '*isp.get_indent_spaces() if autoindent: line = indent + input(prompt+indent) else: line = input(prompt) isp.push(line) prompt = '... ' # Here we just return input so we can use it in a test suite, but a # real interpreter would instead send it for execution somewhere. #src = isp.source; raise EOFError # dbg raw = isp.source_raw src = isp.source_reset() print('Input source was:\n', src) print('Raw source was:\n', raw) except EOFError:
# A simple demo for interactive experimentation. This code will not get # picked up by any test suite. from IPython.core.inputsplitter import IPythonInputSplitter # configure here the syntax to use, prompt and whether to autoindent #isp, start_prompt = InputSplitter(), '>>> ' isp, start_prompt = IPythonInputSplitter(), 'In> ' autoindent = True #autoindent = False try: while True: prompt = start_prompt while isp.push_accepts_more(): indent = ' ' * isp.get_indent_spaces() if autoindent: line = indent + input(prompt + indent) else: line = input(prompt) isp.push(line) prompt = '... ' # Here we just return input so we can use it in a test suite, but a # real interpreter would instead send it for execution somewhere. #src = isp.source; raise EOFError # dbg raw = isp.source_raw src = isp.source_reset() print('Input source was:\n', src) print('Raw source was:\n', raw) except EOFError: