[pypy-svn] r66520 - in pypy/branch/parser-compiler/pypy/interpreter: astcompiler pyparser

benjamin at codespeak.net benjamin at codespeak.net
Thu Jul 23 00:05:48 CEST 2009


Author: benjamin
Date: Thu Jul 23 00:05:47 2009
New Revision: 66520

Modified:
   pypy/branch/parser-compiler/pypy/interpreter/astcompiler/codegen.py
   pypy/branch/parser-compiler/pypy/interpreter/pyparser/future.py
Log:
check for invalid __future__ statements with the column offset

Modified: pypy/branch/parser-compiler/pypy/interpreter/astcompiler/codegen.py
==============================================================================
--- pypy/branch/parser-compiler/pypy/interpreter/astcompiler/codegen.py	(original)
+++ pypy/branch/parser-compiler/pypy/interpreter/astcompiler/codegen.py	Thu Jul 23 00:05:47 2009
@@ -119,7 +119,6 @@
         self.symbols = symbols
         self.frame_blocks = []
         self.interactive = False
-        self.done_with_future = False
         self.temporary_name_counter = 1
         self._compile(tree)
 
@@ -552,7 +551,6 @@
 
     def visit_Import(self, imp):
         self.update_position(imp.lineno, True)
-        self.done_with_future = True
         for alias in imp.names:
             assert isinstance(alias, ast.alias)
             if self.compile_info.flags & consts.CO_FUTURE_ABSOLUTE_IMPORT:
@@ -579,8 +577,10 @@
         assert isinstance(first, ast.alias)
         star_import = len(imp.names) == 1 and first.name == "*"
         if imp.module == "__future__":
-            if self.done_with_future or \
-                    imp.lineno > self.compile_info.last_future_import:
+            last_line, last_offset = self.compile_info.last_future_import
+            if imp.lineno > last_line or \
+                    imp.lineno == last_line and imp.col_offset > last_offset:
+                print last_offset, imp.lineno, imp.col_offset
                 self.error("__future__ statements must appear at beginning " \
                                "of file", imp)
             if star_import:
@@ -590,8 +590,6 @@
                 if alias.name not in future.futureFlags_2_5.compiler_features:
                     self.error("future feature %s is not defined" %
                                (alias.name,), imp)
-        else:
-            self.done_with_future = True
         if imp.level == 0 and \
                 not self.compile_info.flags & consts.CO_FUTURE_ABSOLUTE_IMPORT:
             level = -1

Modified: pypy/branch/parser-compiler/pypy/interpreter/pyparser/future.py
==============================================================================
--- pypy/branch/parser-compiler/pypy/interpreter/pyparser/future.py	(original)
+++ pypy/branch/parser-compiler/pypy/interpreter/pyparser/future.py	Thu Jul 23 00:05:47 2009
@@ -33,7 +33,7 @@
         futures.start()
     except DoneException, e:
         pass
-    return futures.flags, futures.lineno
+    return futures.flags, (futures.lineno, futures.col_offset)
     
 class DoneException(Exception):
     pass
@@ -68,6 +68,8 @@
         self.s = string
         self.pos = 0
         self.lineno = 1
+        self.line_start_pos = 0
+        self.col_offset = 0
         self.docstringConsumed = False
         self.flags = 0
 
@@ -90,6 +92,10 @@
         else:
             return
 
+    def atbol(self):
+        self.lineno += 1
+        self.line_start_pos = self.pos
+
     def consumeDocstring(self):
         self.docstringConsumed = True
         endchar = self.getc()
@@ -104,11 +110,11 @@
                     if c != endchar:
                         self.pos += 1
                         if c == '\n':
-                            self.lineno += 1
+                            self.atbol()
                         elif c == '\r':
                             if self.getc() == '\n':
                                 self.pos += 1
-                                self.lineno += 1
+                                self.atbol()
                     else:
                         self.pos += 1
                         if (self.getc() == endchar and
@@ -156,9 +162,9 @@
             if c == '\r':
                 if self.getc() == '\n':
                     self.pos += 1
-                    self.lineno += 1
+                    self.atbol()
             else:
-                self.lineno += 1
+                self.atbol()
             self.start()
 
     def consumeComment(self):
@@ -168,6 +174,7 @@
         self.consumeEmptyLine()
 
     def consumeFrom(self):
+        col_offset = self.pos - self.line_start_pos
         self.pos += 1
         if self.getc() == 'r' and self.getc(+1) == 'o' and self.getc(+2) == 'm':
             self.docstringConsumed = True
@@ -190,6 +197,7 @@
             else:
                 self.setFlag(self.getName())
                 self.getMore()
+            self.col_offset = col_offset
             self.consumeEmptyLine()
         else:
             return
@@ -210,13 +218,13 @@
                 c = self.getc()
                 if c == '\n':
                     self.pos += 1
-                    self.lineno += 1
+                    self.atbol()
                     continue
                 elif c == '\r':
                     self.pos += 1
                     if self.getc() == '\n':
                         self.pos += 1
-                        self.lineno += 1
+                        self.atbol()
                 else:
                     raise DoneException
             else:



More information about the Pypy-commit mailing list