Commits

Armin Rigo committed acd6e35

- rename ANY to Any to avoid confusion

- use a more pragmatic approach of having 10 specialized
versions, for all for 2-tuples in all combinations,
with the exception that floats only get the (float, float)
combination.

Comments (0)

Files changed (2)

pypy/objspace/std/specialisedtupleobject.py

 from pypy.rlib.objectmodel import compute_hash
 from pypy.rlib.unroll import unrolling_iterable
 
-class ANY(type):
+class Any(object):
     pass
 
 class NotSpecialised(Exception):
     
     class cls(W_SpecialisedTupleObject):
         def __init__(self, space, values):
-            print cls,cls.__class__, values
+            #print cls,cls.__class__, values
             assert len(values) == nValues
             for i in iter_n:
-                if typetuple[i] != ANY:
+                if typetuple[i] != Any:
                     assert isinstance(values[i], typetuple[i])
             self.space = space
             for i in iter_n:
                 elif val_type == str:
                     if space.type(param) != space.w_str:
                         raise NotSpecialised
-                elif val_type == ANY:
+                elif val_type == Any:
                     pass
                 else:
                     raise NotSpecialised 
                     unwrappedparams[i] = space.float_w(paramlist[i])
                 elif typetuple[i] == str:
                     unwrappedparams[i] = space.str_w(paramlist[i])
-                elif typetuple[i] == ANY:
+                elif typetuple[i] == Any:
                     unwrappedparams[i] = paramlist[i]
                 else:
                     raise NotSpecialised 
         def tolist(self):
             list_w = [None] * nValues            
             for i in iter_n:
-                if typetuple[i] == ANY:
+                if typetuple[i] == Any:
                     list_w[i] = getattr(self, 'value%s' % i)
                 else:
                     list_w[i] = self.space.wrap(getattr(self, 'value%s' % i))
         def _to_unwrapped_list(self):
             list_w = [None] * nValues            
             for i in iter_n:
-                if typetuple[i] == ANY:
+                if typetuple[i] == Any:
                     list_w[i] = space.unwrap(getattr(self, 'value%s' % i))#xxx
                 else:
                     list_w[i] = getattr(self, 'value%s' % i)
             z = 2
             for i in iter_n:
                 value = getattr(self, 'value%s' % i)
-                if typetuple[i] == ANY:
+                if typetuple[i] == Any:
                     y = space.int_w(space.hash(value))    
                 elif typetuple[i] == float: # get correct hash for float which is an integer & other less frequent cases
                     y = _hash_float(space, value)
             if not isinstance(w_other, cls): #so we will be sure we are comparing same types
                 raise FailedToImplement
             for i in iter_n:
-                if typetuple[i] == ANY:
+                if typetuple[i] == Any:
                     if not self.space.is_true(self.space.eq(getattr(self, 'value%s' % i), getattr(w_other, 'value%s' % i))):
                        return False
                 else:
                 raise FailedToImplement
             ncmp = min(self.length(), w_other.length())
             for i in iter_n:
-                if typetuple[i] == ANY:#like space.eq on wrapped or two params?
+                if typetuple[i] == Any:#like space.eq on wrapped or two params?
                     raise FailedToImplement
                 if ncmp > i:
                     l_val = getattr(self, 'value%s' % i)
         def getitem(self, index):
             for i in iter_n:
                 if index == i:
-                    if typetuple[i] == ANY:
+                    if typetuple[i] == Any:
                         return getattr(self, 'value%s' % i)
                     else:
                         return self.space.wrap(getattr(self, 'value%s' % i))
     cls.__name__ = 'W_SpecialisedTupleObject' + ''.join([t.__name__.capitalize() for t in typetuple])      
     _specialisations.append(cls)
     return cls
-    
-    
-W_SpecialisedTupleObjectIntInt     = make_specialised_class((int,int))
-W_SpecialisedTupleObjectIntAny     = make_specialised_class((int, ANY))
-W_SpecialisedTupleObjectIntIntInt  = make_specialised_class((int,int,int))
-W_SpecialisedTupleObjectFloatFloat = make_specialised_class((float,float))
-W_SpecialisedTupleObjectStrStr     = make_specialised_class((str, str))
-W_SpecialisedTupleObjectStrAny     = make_specialised_class((str, ANY))
-W_SpecialisedTupleObjectIntFloatStr= make_specialised_class((int, float, str))
-W_SpecialisedTupleObjectIntStrFloatAny= make_specialised_class((int, float, str, ANY))
+
+make_specialised_class((float, float))
+for _typ1 in [int, str, Any]:
+    for _typ2 in [int, str, Any]:
+        make_specialised_class((_typ1, _typ2))
+
+# ____________________________________________________________
 
 registerimplementation(W_SpecialisedTupleObject)
 

pypy/objspace/std/test/test_specialisedtupleobject.py

 import py
 from pypy.objspace.std.tupleobject import W_TupleObject
-from pypy.objspace.std.specialisedtupleobject import W_SpecialisedTupleObject,W_SpecialisedTupleObjectIntInt
+from pypy.objspace.std.specialisedtupleobject import W_SpecialisedTupleObject
+from pypy.objspace.std.specialisedtupleobject import _specialisations
 from pypy.interpreter.error import OperationError
 from pypy.conftest import gettestobjspace
 from pypy.objspace.std.test.test_tupleobject import AppTestW_TupleObject
 from pypy.interpreter import gateway
 
 
+for cls in _specialisations:
+    globals()[cls.__name__] = cls
+
+
 class TestW_SpecialisedTupleObject():
 
     def setup_class(cls):
             w_tuple.tolist = delegation_forbidden
             return w_tuple
         cls.w_forbid_delegation = cls.space.wrap(gateway.interp2app(forbid_delegation))
-            
-    def w_isspecialised(self, obj):
-       import __pypy__
-       return "SpecialisedTuple" in __pypy__.internal_repr(obj)
-       
+
+    def w_isspecialised(self, obj, expected=''):
+        import __pypy__
+        r = __pypy__.internal_repr(obj)
+        print obj, '==>', r, '   (expected: %r)' % expected
+        return ("SpecialisedTupleObject" + expected) in r
 
     def test_createspecialisedtuple(self):
-        assert self.isspecialised((42,43))
-        assert self.isspecialised((4.2,4.3))
-        assert self.isspecialised((1.0,2.0))
-        assert self.isspecialised(('a','b'))
-        
+        spec = {int: 'Int',
+                float: 'Float',
+                str: 'Str',
+                list: 'Any'}
+        #
+        for x in [42, 4.2, "foo", []]:
+            for y in [43, 4.3, "bar", []]:
+                expected1 = spec[type(x)]
+                expected2 = spec[type(y)]
+                if (expected1 == 'Float') ^ (expected2 == 'Float'):
+                    if expected1 == 'Float': expected1 = 'Any'
+                    if expected2 == 'Float': expected2 = 'Any'
+                obj = (x, y)
+                assert self.isspecialised(obj, expected1 + expected2)
+
     def test_len(self):
         t = self.forbid_delegation((42,43))
         assert len(t) == 2
 
     def test_notspecialisedtuple(self):
         assert not self.isspecialised((42,43,44,45))
-        assert not self.isspecialised((1.5,2))
-        assert not self.isspecialised((1.0,2))
+        assert not self.isspecialised((1.5,))
 
     def test_slicing_to_specialised(self):
         assert self.isspecialised((1, 2, 3)[0:2])