[pypy-svn] r47975 - in pypy/branch/dist-future-fixing/pypy/interpreter/pyparser: . test

jacob at codespeak.net jacob at codespeak.net
Thu Oct 25 19:57:47 CEST 2007


Author: jacob
Date: Thu Oct 25 19:57:46 2007
New Revision: 47975

Modified:
   pypy/branch/dist-future-fixing/pypy/interpreter/pyparser/future.py
   pypy/branch/dist-future-fixing/pypy/interpreter/pyparser/test/test_futureautomaton.py
Log:
Made the future automaton use the __future__.py module directly.

Modified: pypy/branch/dist-future-fixing/pypy/interpreter/pyparser/future.py
==============================================================================
--- pypy/branch/dist-future-fixing/pypy/interpreter/pyparser/future.py	(original)
+++ pypy/branch/dist-future-fixing/pypy/interpreter/pyparser/future.py	Thu Oct 25 19:57:46 2007
@@ -64,7 +64,6 @@
     
     def __init__(self, string):
         self.s = string
-        self.end = len(string)
         self.pos = 0
         self.docstringConsumed = False
         self.flags = 0
@@ -237,10 +236,47 @@
             self.getMore(parenList=parenList)
 
     def setFlag(self, feature):
-        if feature == "division":
-            self.flags |= CO_FUTURE_DIVISION
-        elif feature == "generators":
-            self.flags |= CO_GENERATOR_ALLOWED
-        elif feature == "with_statement":
-            self.flags |= CO_FUTURE_WITH_STATEMENT
-
+        try:
+            self.flags |= futureFlags.compiler_features[feature]
+        except IndexError:
+            pass
+
+from codeop import PyCF_DONT_IMPLY_DEDENT
+from pypy.interpreter.error import OperationError
+
+from pypy.tool import stdlib___future__ as future
+
+class FutureFlags(object):
+    def __init__(self, version):
+        compiler_flags = 0
+        self.compiler_features = {}
+        self.mandatory_flags = 0
+        for fname in future.all_feature_names:
+            feature = getattr(future, fname)
+            if version >= feature.getOptionalRelease():
+                flag = feature.compiler_flag
+                compiler_flags |= flag
+                self.compiler_features[fname] = flag
+            if version >= feature.getMandatoryRelease():
+                self.mandatory_flags |= feature.compiler_flag
+        self.allowed_flags = compiler_flags | PyCF_DONT_IMPLY_DEDENT
+
+    def get_flag_names(self, space, flags):
+        if flags & ~self.allowed_flags:
+            raise OperationError(space.w_ValueError,
+                                 space.wrap("compile(): unrecognized flags"))
+        flag_names = []
+        for name, value in self.compiler_features.items():
+            if flags & value:
+                flag_names.append(name)
+        return flag_names
+
+# XXX This is a hack to deal with the fact that we currently are
+#     using the Python 2.4.1 libraries even when running Python 2.5
+#     and that we have a hacked __future__ module.
+from pypy.config.pypyoption import get_pypy_config
+config = get_pypy_config(translating=False)
+if config.objspace.pyversion == '2.4':
+    futureFlags = FutureFlags((2, 4, 4, 'final', 0))
+else:
+    futureFlags = FutureFlags((2, 5, 0, 'final', 0))

Modified: pypy/branch/dist-future-fixing/pypy/interpreter/pyparser/test/test_futureautomaton.py
==============================================================================
--- pypy/branch/dist-future-fixing/pypy/interpreter/pyparser/test/test_futureautomaton.py	(original)
+++ pypy/branch/dist-future-fixing/pypy/interpreter/pyparser/test/test_futureautomaton.py	Thu Oct 25 19:57:46 2007
@@ -1,7 +1,6 @@
 import py
 import pypy.interpreter.pyparser.future as future
-from pypy.interpreter.astcompiler.consts import CO_GENERATOR_ALLOWED, \
-    CO_FUTURE_DIVISION, CO_FUTURE_WITH_STATEMENT
+from pypy.tool import stdlib___future__ as fut
 
 def run(s):
     f = future.FutureAutomaton(s)
@@ -15,7 +14,7 @@
     s = '"Docstring\\" "\nfrom  __future__ import division\n'
     f = run(s)
     assert f.pos == len(s)
-    assert f.flags == CO_FUTURE_DIVISION
+    assert f.flags == fut.CO_FUTURE_DIVISION
 
 def test_comment():
     s = '# A comment about nothing ;\n'
@@ -49,88 +48,88 @@
     s = 'from  __future__ import division\n'
     f = run(s)
     assert f.pos == len(s)
-    assert f.flags == CO_FUTURE_DIVISION
+    assert f.flags == fut.CO_FUTURE_DIVISION
 
 def test_froms():
     s = 'from  __future__ import division, generators, with_statement\n'
     f = run(s)
     assert f.pos == len(s)
-    assert f.flags == (CO_FUTURE_DIVISION |
-                       CO_GENERATOR_ALLOWED |
-                       CO_FUTURE_WITH_STATEMENT)
+    assert f.flags == (fut.CO_FUTURE_DIVISION |
+                       fut.CO_GENERATOR_ALLOWED |
+                       fut.CO_FUTURE_WITH_STATEMENT)
 
 def test_from_as():
     s = 'from  __future__ import division as b\n'
     f = run(s)
     assert f.pos == len(s)
-    assert f.flags == CO_FUTURE_DIVISION
+    assert f.flags == fut.CO_FUTURE_DIVISION
     
 def test_froms_as():
     s = 'from  __future__ import division as b, generators as c\n'
     f = run(s)
     assert f.pos == len(s)
-    assert f.flags == (CO_FUTURE_DIVISION |
-                       CO_GENERATOR_ALLOWED)
+    assert f.flags == (fut.CO_FUTURE_DIVISION |
+                       fut.CO_GENERATOR_ALLOWED)
 
 def test_from_paren():
     s = 'from  __future__ import (division)\n'
     f = run(s)
     assert f.pos == len(s)
-    assert f.flags == CO_FUTURE_DIVISION
+    assert f.flags == fut.CO_FUTURE_DIVISION
 
 def test_froms_paren():
     s = 'from  __future__ import (division, generators)\n'
     f = run(s)
     assert f.pos == len(s)
-    assert f.flags == (CO_FUTURE_DIVISION |
-                       CO_GENERATOR_ALLOWED)
+    assert f.flags == (fut.CO_FUTURE_DIVISION |
+                       fut.CO_GENERATOR_ALLOWED)
 
 def test_froms_paren_as():
     s = 'from  __future__ import (division as b, generators,)\n'
     f = run(s)
     assert f.pos == len(s)
-    assert f.flags == (CO_FUTURE_DIVISION |
-                       CO_GENERATOR_ALLOWED)
+    assert f.flags == (fut.CO_FUTURE_DIVISION |
+                       fut.CO_GENERATOR_ALLOWED)
 
 def test_multiline():
     s = '"abc" #def\n  #ghi\nfrom  __future__ import (division as b, generators,)\nfrom __future__ import with_statement\n'
     f = run(s)
     assert f.pos == len(s)
-    assert f.flags == (CO_FUTURE_DIVISION |
-                       CO_GENERATOR_ALLOWED |
-                       CO_FUTURE_WITH_STATEMENT)
+    assert f.flags == (fut.CO_FUTURE_DIVISION |
+                       fut.CO_GENERATOR_ALLOWED |
+                       fut.CO_FUTURE_WITH_STATEMENT)
 
 def test_windows_style_lineendings():
     s = '"abc" #def\r\n  #ghi\r\nfrom  __future__ import (division as b, generators,)\r\nfrom __future__ import with_statement\r\n'
     f = run(s)
     assert f.pos == len(s)
-    assert f.flags == (CO_FUTURE_DIVISION |
-                       CO_GENERATOR_ALLOWED |
-                       CO_FUTURE_WITH_STATEMENT)
+    assert f.flags == (fut.CO_FUTURE_DIVISION |
+                       fut.CO_GENERATOR_ALLOWED |
+                       fut.CO_FUTURE_WITH_STATEMENT)
 
 def test_mac_style_lineendings():
     s = '"abc" #def\r  #ghi\rfrom  __future__ import (division as b, generators,)\rfrom __future__ import with_statement\r'
     f = run(s)
     assert f.pos == len(s)
-    assert f.flags == (CO_FUTURE_DIVISION |
-                       CO_GENERATOR_ALLOWED |
-                       CO_FUTURE_WITH_STATEMENT)
+    assert f.flags == (fut.CO_FUTURE_DIVISION |
+                       fut.CO_GENERATOR_ALLOWED |
+                       fut.CO_FUTURE_WITH_STATEMENT)
 def test_semicolon():
     s = '"abc" #def\n  #ghi\nfrom  __future__ import (division as b, generators,);  from __future__ import with_statement\n'
     f = run(s)
     assert f.pos == len(s)
-    assert f.flags == (CO_FUTURE_DIVISION |
-                       CO_GENERATOR_ALLOWED |
-                       CO_FUTURE_WITH_STATEMENT)
+    assert f.flags == (fut.CO_FUTURE_DIVISION |
+                       fut.CO_GENERATOR_ALLOWED |
+                       fut.CO_FUTURE_WITH_STATEMENT)
 
 def test_full_chain():
     s = '"abc" #def\n  #ghi\nfrom  __future__ import (division as b, generators,);  from __future__ import with_statement\n'
     flags = future.getFutures(s)
-    assert flags == (CO_FUTURE_DIVISION |
-                     CO_GENERATOR_ALLOWED |
-                     CO_FUTURE_WITH_STATEMENT)
+    assert flags == (fut.CO_FUTURE_DIVISION |
+                     fut.CO_GENERATOR_ALLOWED |
+                     fut.CO_FUTURE_WITH_STATEMENT)
 
 def test_intervening_code():
     s = 'from  __future__ import (division as b, generators,)\nfrom sys import modules\nfrom __future__ import with_statement\n'
     flags = future.getFutures(s)
-    assert flags & CO_FUTURE_WITH_STATEMENT == 0
+    assert flags & fut.CO_FUTURE_WITH_STATEMENT == 0



More information about the Pypy-commit mailing list