Index: Parser/parsetok.c =================================================================== --- Parser/parsetok.c (revision 66561) +++ Parser/parsetok.c (working copy) @@ -137,19 +137,22 @@ err_ret->error = tok->done; break; } - if (type == ENDMARKER && started) { - type = NEWLINE; /* Add an extra newline */ - handling_with = handling_import = 0; - started = 0; - /* Add the right number of dedent tokens, - except if a certain flag is given -- - codeop.py uses this. */ - if (tok->indent && - !(flags & PyPARSE_DONT_IMPLY_DEDENT)) - { - tok->pendin = -tok->indent; - tok->indent = 0; + if (started) { + if (type == ENDMARKER) { + type = NEWLINE; /* Add an extra newline */ + started = 0; + /* Add the right number of dedent tokens, + except if a certain flag is given -- + codeop.py uses this. */ + if (tok->indent && + !(flags & PyPARSE_DONT_IMPLY_DEDENT)) + { + tok->pendin = -tok->indent; + tok->indent = 0; + } } + if (type == NEWLINE) + handling_with = handling_import = 0; } else started = 1; Index: Lib/test/test_with.py =================================================================== --- Lib/test/test_with.py (revision 66561) +++ Lib/test/test_with.py (working copy) @@ -9,6 +9,7 @@ import sys import unittest +import StringIO from collections import deque from contextlib import GeneratorContextManager, contextmanager from test.test_support import run_unittest @@ -625,12 +626,44 @@ self.fail("ZeroDivisionError should have been raised") +class NewKeywordsWarningTestCase(unittest.TestCase): + + def check(self, code, word=None): + save = sys.stderr + sys.stderr = stream = StringIO.StringIO() + try: + compile(code, "", "exec", 0, True) + finally: + sys.stderr = save + if word: + self.assert_("Warning: %r will become a reserved keyword in Python 2.6" % word + in stream.getvalue()) + else: + self.assertEqual(stream.getvalue(), "") + + def test_basic(self): + self.check("as = 4", "as") + self.check("with = 4", "with") + self.check("class as: pass", "as") + self.check("class with: pass", "with") + self.check("obj.as = 4", "as") + self.check("with.obj = 4", "with") + self.check("def with(): pass", "with") + self.check("do(); with = 23", "with") + + def test_after_import(self): + # issue 3936 + self.check("import sys\nas = 4", "as") + self.check("import sys\nwith = 4", "with") + + def test_main(): run_unittest(FailureTestCase, NonexceptionalTestCase, NestedNonexceptionalTestCase, ExceptionalTestCase, NonLocalFlowControlTestCase, AssignmentTargetTestCase, - ExitSwallowsExceptionTestCase) + ExitSwallowsExceptionTestCase, + NewKeywordsWarningTestCase) if __name__ == '__main__':