diff -r a22ed8b509f5 Lib/code.py --- a/Lib/code.py Tue Aug 21 00:53:06 2012 +0200 +++ b/Lib/code.py Tue Aug 21 10:06:55 2012 +1000 @@ -5,6 +5,7 @@ # Inspired by similar code by Jeff Epler and Fredrik Lundh. +import os import sys import traceback from codeop import CommandCompiler, compile_command @@ -240,8 +241,7 @@ def push(self, line): """Push a line to the interpreter. - The line should not have a trailing newline; it may have - internal newlines. The line is appended to a buffer and the + The line is appended to a buffer and the interpreter's runsource() method is called with the concatenated contents of the buffer as source. If this indicates that the command was executed or invalid, the buffer @@ -258,6 +258,19 @@ self.resetbuffer() return more + def push_lines(self, linestring, sep=os.linesep): + """Split a lines into mulitple lines and push to the interpreter. + + The line is split using the os.linesep by default. This may be + changed by passing a different separator by default. The return value + is the final continuation value returned by push(). + + """ + more = 0 + for line in linestring.split(sep): + more = self.push(line) + return more + def raw_input(self, prompt=""): """Write a prompt and read a line. @@ -272,7 +285,6 @@ return input(prompt) - def interact(banner=None, readfunc=None, local=None): """Closely emulate the interactive Python interpreter. diff -r a22ed8b509f5 Lib/test/test_code_module.py --- a/Lib/test/test_code_module.py Tue Aug 21 00:53:06 2012 +0200 +++ b/Lib/test/test_code_module.py Tue Aug 21 10:06:55 2012 +1000 @@ -64,6 +64,18 @@ self.console.interact() self.assertTrue(hook.called) + def test_gramar_newline(self): + line = "beast='Rabbit of Caerbannog'\n" + more = self.console.push(line) + self.assertFalse(more) + self.assertIn("beast", self.console.locals) + + def test_push_lines(self): + line = "if True:\n beast='Rabbit of Caerbannog'\n" + more = self.console.push_lines(line, "\n") + self.assertFalse(more) + self.assertIn("beast", self.console.locals) + def test_main(): support.run_unittest(TestInteractiveConsole)