[pypy-svn] r73173 - in pypy/trunk/pypy/interpreter: pyparser pyparser/test test

benjamin at codespeak.net benjamin at codespeak.net
Tue Mar 30 16:54:35 CEST 2010


Author: benjamin
Date: Tue Mar 30 16:54:33 2010
New Revision: 73173

Modified:
   pypy/trunk/pypy/interpreter/pyparser/future.py
   pypy/trunk/pypy/interpreter/pyparser/test/test_futureautomaton.py
   pypy/trunk/pypy/interpreter/test/test_syntax.py
Log:
handle future imports after raw or unicode docstrings (issue 515)

Modified: pypy/trunk/pypy/interpreter/pyparser/future.py
==============================================================================
--- pypy/trunk/pypy/interpreter/pyparser/future.py	(original)
+++ pypy/trunk/pypy/interpreter/pyparser/future.py	Tue Mar 30 16:54:33 2010
@@ -62,7 +62,7 @@
     not recognized as a valid future statement or something that may
     precede a future statement.
     """
-    
+
     def __init__(self, future_flags, string):
         self.future_flags = future_flags
         self.s = string
@@ -83,7 +83,7 @@
 
     def start(self):
         c = self.getc()
-        if c in ["'", '"'] and not self.docstring_consumed:
+        if c in ("'", '"', "r", "u") and not self.docstring_consumed:
             self.consume_docstring()
         elif c in whitespace_or_newline:
             self.consume_empty_line()
@@ -100,6 +100,10 @@
 
     def consume_docstring(self):
         self.docstring_consumed = True
+        if self.getc() == "r":
+            self.pos += 1
+        if self.getc() == "u":
+            self.pos += 1
         endchar = self.getc()
         if (self.getc() == self.getc(+1) and
             self.getc() == self.getc(+2)):

Modified: pypy/trunk/pypy/interpreter/pyparser/test/test_futureautomaton.py
==============================================================================
--- pypy/trunk/pypy/interpreter/pyparser/test/test_futureautomaton.py	(original)
+++ pypy/trunk/pypy/interpreter/pyparser/test/test_futureautomaton.py	Tue Mar 30 16:54:33 2010
@@ -150,3 +150,20 @@
     assert f.flags == fut.CO_FUTURE_ABSOLUTE_IMPORT
 
 
+def test_raw_doc():
+    s = 'r"Doc"\nfrom __future__ import with_statement\n'
+    f = run(s)
+    assert f.pos == len(s)
+    assert f.flags == fut.CO_FUTURE_WITH_STATEMENT
+
+def test_unicode_doc():
+    s = 'u"Doc"\nfrom __future__ import with_statement\n'
+    f = run(s)
+    assert f.pos == len(s)
+    assert f.flags == fut.CO_FUTURE_WITH_STATEMENT
+
+def test_raw_unicode_doc():
+    s = 'ru"Doc"\nfrom __future__ import with_statement\n'
+    f = run(s)
+    assert f.pos == len(s)
+    assert f.flags == fut.CO_FUTURE_WITH_STATEMENT

Modified: pypy/trunk/pypy/interpreter/test/test_syntax.py
==============================================================================
--- pypy/trunk/pypy/interpreter/test/test_syntax.py	(original)
+++ pypy/trunk/pypy/interpreter/test/test_syntax.py	Tue Mar 30 16:54:33 2010
@@ -359,6 +359,22 @@
         exec s
         assert acontext.calls == '__enter__ __exit__'.split()
 
+    def test_raw_doc_string(self):
+        s = """r'doc'
+from __future__ import with_statement
+class Context(object):
+    def __enter__(self):
+        global enter
+        enter = True
+    def __exit__(self, *exc):
+        global exit
+        exit = True
+with Context() as w:
+    pass"""
+        exec s
+        assert enter
+        assert exit
+
     def test_with_as_var(self):
 
         s = """from __future__ import with_statement



More information about the Pypy-commit mailing list