[pypy-svn] r4264 - in pypy/branch/typeinference/pypy: annotation translator translator/test

arigo at codespeak.net arigo at codespeak.net
Mon May 3 20:54:05 CEST 2004


Author: arigo
Date: Mon May  3 20:54:05 2004
New Revision: 4264

Added:
   pypy/branch/typeinference/pypy/annotation/unaryop.py
Modified:
   pypy/branch/typeinference/pypy/annotation/binaryop.py
   pypy/branch/typeinference/pypy/annotation/model.py
   pypy/branch/typeinference/pypy/translator/annrpython.py
   pypy/branch/typeinference/pypy/translator/test/test_annrpython.py
Log:
Most tests not involving user classes are passing.


Modified: pypy/branch/typeinference/pypy/annotation/binaryop.py
==============================================================================
--- pypy/branch/typeinference/pypy/annotation/binaryop.py	(original)
+++ pypy/branch/typeinference/pypy/annotation/binaryop.py	Mon May  3 20:54:05 2004
@@ -6,31 +6,24 @@
 from pypy.annotation.model import SomeObject, SomeInteger, SomeBool
 from pypy.annotation.model import SomeString, SomeList
 from pypy.annotation.model import SomeTuple, SomeImpossibleValue
+from pypy.annotation.model import set, setunion, missing_operation
 from pypy.annotation.factory import NeedGeneralization
 
 
-def setunion(d1, d2):
-    "Union of two sets represented as dictionaries."
-    d = d1.copy()
-    d.update(d2)
-    return d
-
-def set(it):
-    "Turn an iterable into a set."
-    d = {}
-    for x in it:
-        d[x] = True
-    return d
-
-
+# XXX unify this with ObjSpace.MethodTable
 BINARY_OPERATIONS = set(['add', 'sub', 'mul', 'getitem', 'setitem',
+                         'inplace_add',
+                         'lt', 'le', 'eq', 'ne', 'gt', 'ge',
                          'union'])
 
-def _defaultcase((obj1, obj2), *args):
-    return SomeObject()
+for opname in BINARY_OPERATIONS:
+    missing_operation(pairtype(SomeObject, SomeObject), opname)
+
+
+class __extend__(pairtype(SomeObject, SomeObject)):
 
-for name in BINARY_OPERATIONS:
-    setattr(pairtype(SomeObject, SomeObject), name, _defaultcase)
+    def union((obj1, obj2)):
+        return SomeObject()
 
 
 class __extend__(pairtype(SomeInteger, SomeInteger)):
@@ -41,9 +34,18 @@
     def add((int1, int2)):
         return SomeInteger(nonneg = int1.nonneg and int2.nonneg)
 
+    mul = add
+
     def sub((int1, int2)):
         return SomeInteger()
 
+    def lt((int1, int2)): return SomeBool()
+    def le((int1, int2)): return SomeBool()
+    def eq((int1, int2)): return SomeBool()
+    def ne((int1, int2)): return SomeBool()
+    def gt((int1, int2)): return SomeBool()
+    def ge((int1, int2)): return SomeBool()
+
 
 class __extend__(pairtype(SomeBool, SomeBool)):
 
@@ -68,6 +70,9 @@
 
     add = union
 
+    def inplace_add((lst1, lst2)):
+        pair(lst1, SomeInteger()).setitem(lst2.s_item)
+
 
 class __extend__(pairtype(SomeTuple, SomeTuple)):
 

Modified: pypy/branch/typeinference/pypy/annotation/model.py
==============================================================================
--- pypy/branch/typeinference/pypy/annotation/model.py	(original)
+++ pypy/branch/typeinference/pypy/annotation/model.py	Mon May  3 20:54:05 2004
@@ -28,12 +28,13 @@
 #
 
 
-from pypy.annotation.pairtype import pair
+from pypy.annotation.pairtype import pair, extendabletype
 
 
 class SomeObject:
     """The set of all objects.  Each instance stands
     for an arbitrary object about which nothing is known."""
+    __metaclass__ = extendabletype
     knowntype = object
     def __eq__(self, other):
         return (self.__class__ is other.__class__ and
@@ -47,9 +48,6 @@
         return pair(self, other).union() == self
     def is_constant(self):
         return hasattr(self, 'const')
-    # non-binary default methods
-    def len(self):
-        return SomeInteger(nonneg=True)
 
 class SomeInteger(SomeObject):
     "Stands for an object which is known to be an integer."
@@ -117,5 +115,29 @@
         return SomeObject()
 
 
-# this has the side-effect of registering the binary operations
+# ____________________________________________________________
+# internal
+
+def setunion(d1, d2):
+    "Union of two sets represented as dictionaries."
+    d = d1.copy()
+    d.update(d2)
+    return d
+
+def set(it):
+    "Turn an iterable into a set."
+    d = {}
+    for x in it:
+        d[x] = True
+    return d
+
+def missing_operation(cls, name):
+    def default_op(*args):
+        print '* warning, no type available for %s(%s)' % (
+            name, ', '.join([repr(a) for a in args]))
+        return SomeObject()
+    setattr(cls, name, default_op)
+
+# this has the side-effect of registering the unary and binary operations
+from pypy.annotation.unaryop import UNARY_OPERATIONS
 from pypy.annotation.binaryop import BINARY_OPERATIONS

Added: pypy/branch/typeinference/pypy/annotation/unaryop.py
==============================================================================
--- (empty file)
+++ pypy/branch/typeinference/pypy/annotation/unaryop.py	Mon May  3 20:54:05 2004
@@ -0,0 +1,24 @@
+"""
+Unary operations on SomeValues.
+"""
+
+from pypy.annotation.pairtype import pair, pairtype
+from pypy.annotation.model import SomeObject, SomeInteger, SomeBool
+from pypy.annotation.model import SomeString, SomeList
+from pypy.annotation.model import SomeTuple
+from pypy.annotation.model import set, setunion, missing_operation
+
+
+UNARY_OPERATIONS = set(['len', 'is_true'])
+
+for opname in UNARY_OPERATIONS:
+    missing_operation(SomeObject, opname)
+
+
+class __extend__(SomeObject):
+    
+    def len(obj):
+        return SomeInteger(nonneg=True)
+
+    def is_true(obj):
+        return SomeBool()

Modified: pypy/branch/typeinference/pypy/translator/annrpython.py
==============================================================================
--- pypy/branch/typeinference/pypy/translator/annrpython.py	(original)
+++ pypy/branch/typeinference/pypy/translator/annrpython.py	Mon May  3 20:54:05 2004
@@ -229,13 +229,22 @@
     def default_consider_op(self, *args):
         return annmodel.SomeObject()
 
-    # All binary operations
-    for opname in annmodel.BINARY_OPERATIONS:
-        exec """
+    def _registeroperations(loc):
+        # All unary operations
+        for opname in annmodel.UNARY_OPERATIONS:
+            exec """
+def consider_op_%s(self, arg, *args):
+    return arg.%s(*args)
+""" % (opname, opname) in globals(), loc
+        # All binary operations
+        for opname in annmodel.BINARY_OPERATIONS:
+            exec """
 def consider_op_%s(self, arg1, arg2, *args):
     return pair(arg1,arg2).%s(*args)
-""" % (opname, opname)
-    del opname
+""" % (opname, opname) in globals(), loc
+
+    _registeroperations(locals())
+    del _registeroperations
 
     def consider_op_newtuple(self, *args):
         return annmodel.SomeTuple(items = args)

Modified: pypy/branch/typeinference/pypy/translator/test/test_annrpython.py
==============================================================================
--- pypy/branch/typeinference/pypy/translator/test/test_annrpython.py	(original)
+++ pypy/branch/typeinference/pypy/translator/test/test_annrpython.py	Mon May  3 20:54:05 2004
@@ -3,7 +3,7 @@
 from pypy.tool import testit
 from pypy.tool.udir import udir
 
-from pypy.translator.annrpython import RPythonAnnotator, ANN
+from pypy.translator.annrpython import RPythonAnnotator, annmodel
 from pypy.translator.translator import Translator
 from pypy.objspace.flow.model import *
 
@@ -124,9 +124,7 @@
         # result should be a list of integers
         self.assertEquals(a.gettype(fun.getreturnvar()), list)
         end_cell = a.binding(fun.getreturnvar())
-        item_cell = a.heap.get(ANN.listitems, end_cell)
-        self.assert_(item_cell)
-        self.assertEquals(a.heap.get(ANN.type, item_cell), int)
+        self.assertEquals(end_cell.s_item.knowntype, int)
 
     def test_factorial(self):
         translator = Translator(snippet.factorial)


More information about the Pypy-commit mailing list