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 09:42:05 2012 +1000 @@ -237,11 +237,10 @@ self.resetbuffer() more = 0 - def push(self, line): + def push(self, line, symbol='single'): """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 @@ -250,10 +249,12 @@ value is 1 if more input is required, 0 if the line was dealt with in some way (this is the same as runsource()). + The symbol controls the grammar start symbol. It should be 'symbol' + (the default) or 'eval'. """ self.buffer.append(line) source = "\n".join(self.buffer) - more = self.runsource(source, self.filename) + more = self.runsource(source, self.filename, symbol) if not more: self.resetbuffer() return more 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 09:42:05 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, "single") + self.assertFalse(more) + self.assertIn("beast", self.console.locals) + + def test_gramar_exec(self): + line = "if True:\n beast='Rabbit of Caerbannog'\n" + more = self.console.push(line, "exec") + self.assertFalse(more) + self.assertIn("beast", self.console.locals) + def test_main(): support.run_unittest(TestInteractiveConsole)