[pypy-svn] rev 940 - in pypy/trunk/src/pypy/objspace/ann: . test

gvanrossum at codespeak.net gvanrossum at codespeak.net
Sun Jun 22 14:36:16 CEST 2003


Author: gvanrossum
Date: Sun Jun 22 14:36:16 2003
New Revision: 940

Added:
   pypy/trunk/src/pypy/objspace/ann/wrapper.py
Modified:
   pypy/trunk/src/pypy/objspace/ann/cloningcontext.py
   pypy/trunk/src/pypy/objspace/ann/objspace.py
   pypy/trunk/src/pypy/objspace/ann/test/test_objspace.py
Log:
Refactoring.  Move wrapper objects to new file.

Modified: pypy/trunk/src/pypy/objspace/ann/cloningcontext.py
==============================================================================
--- pypy/trunk/src/pypy/objspace/ann/cloningcontext.py	(original)
+++ pypy/trunk/src/pypy/objspace/ann/cloningcontext.py	Sun Jun 22 14:36:16 2003
@@ -1,5 +1,6 @@
 from pypy.interpreter.executioncontext import ExecutionContext
 from pypy.interpreter.pyframe import ControlFlowException
+from pypy.objspace.ann.wrapper import union
 
 class IndeterminateCondition(ControlFlowException):
 
@@ -34,7 +35,7 @@
                 assert w_obj.force == True
                 w_obj.force = False
             r = ExecutionContext.eval_frame(self, f)
-            result = space.union(result, r)
+            result = union(result, r)
             if isinstance(result, W_Anything):
                 break
         return result

Modified: pypy/trunk/src/pypy/objspace/ann/objspace.py
==============================================================================
--- pypy/trunk/src/pypy/objspace/ann/objspace.py	(original)
+++ pypy/trunk/src/pypy/objspace/ann/objspace.py	Sun Jun 22 14:36:16 2003
@@ -7,35 +7,8 @@
 from pypy.objspace.ann.cloningcontext import CloningExecutionContext
 from pypy.objspace.ann.cloningcontext import IndeterminateCondition
 
+from pypy.objspace.ann.wrapper import *
 
-class W_Object(object):
-    pass
-
-class W_Anything(W_Object):
-    pass
-
-class W_Integer(W_Object):
-    pass
-
-class W_Constant(W_Object):
-    def __init__(self, value):
-        self.value = value
-    def __repr__(self):
-        return '<constant %r>' % self.value
-
-class W_KnownKeysContainer(W_Object):
-    def __init__(self, args_w):
-        self.args_w = args_w
-    def __len__(self):
-        return len(self.args_w)
-    def __getitem__(self, i):
-        return self.args_w[i]
-    def clone(self):
-        args_w = self.args_w
-        if isinstance(args_w, dict):
-            args_w = args_w.copy()
-        # XXX Recurse down the values?
-        return W_KnownKeysContainer(args_w)
 
 
 class AnnException(Exception):
@@ -98,27 +71,6 @@
         assert isinstance(w_locals, W_KnownKeysContainer)
         return w_locals.clone()
 
-    def union(self, r1, r2):
-        # Unite two results
-        if r1 is r2:
-            return r1
-        if r1 is None:
-            return r2
-        if r2 is None:
-            return r1
-        if isinstance(r1, W_Anything) or isinstance(r2, W_Anything):
-            return W_Anything()
-        if (isinstance(r1, W_Constant) and isinstance(r2, W_Constant) and
-            r1.value == r2.value):
-            return W_Constant(r1.value)
-        if self.is_int(r1) and self.is_int(r2):
-            return W_Integer()
-        if (isinstance(r1, W_KnownKeysContainer) and
-            isinstance(r2, W_KnownKeysContainer) and
-            r1.args_w == r2.args_w):
-            return W_KnownKeysContainer(r1.args_w)
-        # XXX Could do more cases.  This will blow up as we add more types
-        return W_Anything()
 
     # Specialized creators whose interface is in the abstract base class
     
@@ -158,19 +110,11 @@
             pass
         else:
             return self.wrap(left + right)
-        if self.is_int(w_left) and self.is_int(w_right):
+        if is_int(w_left) and is_int(w_right):
             return W_Integer()
         else:
             return W_Anything()
 
-    def is_int(self, w_obj):
-        if isinstance(w_obj, W_Integer):
-            return True
-        if isinstance(w_obj, W_Constant):
-            return isinstance(w_obj.value, int)
-        else:
-            return False
-
     def call(self, w_func, w_args, w_kwds):
         func = self.unwrap(w_func) # Would be bad it it was W_Anything
         code = func.func_code

Modified: pypy/trunk/src/pypy/objspace/ann/test/test_objspace.py
==============================================================================
--- pypy/trunk/src/pypy/objspace/ann/test/test_objspace.py	(original)
+++ pypy/trunk/src/pypy/objspace/ann/test/test_objspace.py	Sun Jun 22 14:36:16 2003
@@ -47,6 +47,14 @@
                           'f', [W_Integer()])
         self.assertEquals(type(x), W_Integer)
 
+    def test_call(self):
+        x = self.codetest("def f(i):\n"
+                          "    return g(i)+2\n"
+                          "def g(i):\n"
+                          "     return i+1\n",
+                          'f', [self.space.wrap(0)])
+        self.assertEquals(self.space.unwrap(x), 3)
+
     def test_conditional_1(self):
         x = self.codetest("def f(i):\n"
                           "    if i < 0:\n"
@@ -65,6 +73,28 @@
                           'f', [W_Integer()])
         self.assertEquals(self.space.unwrap(x), 0)
 
+    def dont_test_while(self):
+        x = self.codetest("def f(i):\n"
+                          "    while i > 0:\n"
+                          "        i = i-1\n"
+                          "    return i\n",
+                          'f', [W_Integer()])
+        self.assertEquals(type(x), W_Integer)
+
+    def dont_test_global(self):
+        # XXX This doesn't work because we don't handle mutating globals
+        x = self.codetest("def f(i, j):\n"
+                          "    global x\n"
+                          "    x = i\n"
+                          "    if j > 0: pass\n"
+                          "    g()\n"
+                          "    return x\n"
+                          "def g():\n"
+                          "    global x\n"
+                          "    x = x+1\n",
+                          'f', [self.space.wrap(0), W_Anything()])
+        self.assertEquals(self.space.unwrap(x), 1)
+
 
 
 if __name__ == '__main__':

Added: pypy/trunk/src/pypy/objspace/ann/wrapper.py
==============================================================================
--- (empty file)
+++ pypy/trunk/src/pypy/objspace/ann/wrapper.py	Sun Jun 22 14:36:16 2003
@@ -0,0 +1,65 @@
+"""Wrapper objects used by annotating space.
+
+The classes here represent various amounts of knowledge about a
+wrapped object, from W_Constant (we know the exact value) to
+W_Anything (we know nothing).  The union() function computes unions.
+We expect that there will eventually be a class that represents a
+union of constants or other types too, in some cases.
+
+"""
+
+class W_Object(object):
+    pass
+
+class W_Anything(W_Object):
+    pass
+
+class W_Integer(W_Object):
+    pass
+
+class W_Constant(W_Object):
+    def __init__(self, value):
+        self.value = value
+    def __repr__(self):
+        return '<constant %r>' % self.value
+
+class W_KnownKeysContainer(W_Object):
+    def __init__(self, args_w):
+        self.args_w = args_w
+    def __len__(self):
+        return len(self.args_w)
+    def __getitem__(self, i):
+        return self.args_w[i]
+    def clone(self):
+        args_w = self.args_w
+        if isinstance(args_w, dict):
+            args_w = args_w.copy()
+        # XXX Recurse down the values?
+        return W_KnownKeysContainer(args_w)
+
+def union(r1, r2):
+    # Unite two results
+    if r1 is r2:
+        return r1
+    if r1 is None:
+        return r2
+    if r2 is None:
+        return r1
+    if isinstance(r1, W_Anything) or isinstance(r2, W_Anything):
+        return W_Anything()
+    if (isinstance(r1, W_Constant) and isinstance(r2, W_Constant) and
+        r1.value == r2.value):
+        return W_Constant(r1.value)
+    if is_int(r1) and is_int(r2):
+        return W_Integer()
+    if (isinstance(r1, W_KnownKeysContainer) and
+        isinstance(r2, W_KnownKeysContainer) and
+        r1.args_w == r2.args_w):
+        return W_KnownKeysContainer(r1.args_w)
+    # XXX Could do more cases.
+    # XXX This will blow up as we add more types.  Refactor when that happens.
+    return W_Anything()
+
+def is_int(w_obj):
+    return (isinstance(w_obj, W_Integer) or
+            isinstance(w_obj, W_Constant) and isinstance(w_obj.value, int))


More information about the Pypy-commit mailing list