Author: Manuel Jacob
Branch: remove-tuple-smm
Changeset: r64415:8da13dda52f2
Date: 2013-05-22 03:03 +0200
http://bitbucket.org/pypy/pypy/changeset/8da13dda52f2/

Log:    IN-PROGRESS: Make specialisedtupleobject work.

diff --git a/pypy/objspace/std/model.py b/pypy/objspace/std/model.py
--- a/pypy/objspace/std/model.py
+++ b/pypy/objspace/std/model.py
@@ -187,12 +187,6 @@
                                        strbufobject.delegate_buf2unicode)
                 ]
 
-        # XXX fix specialisedtuple
-        #if config.objspace.std.withspecialisedtuple:
-        #    from pypy.objspace.std import specialisedtupleobject
-        #    self.typeorder[specialisedtupleobject.W_SpecialisedTupleObject] 
+= [
-        #        (tupleobject.W_TupleObject, 
specialisedtupleobject.delegate_SpecialisedTuple2Tuple)]
-
         # put W_Root everywhere
         self.typeorder[W_Root] = []
         for type in self.typeorder:
diff --git a/pypy/objspace/std/smalltupleobject.py 
b/pypy/objspace/std/smalltupleobject.py
--- a/pypy/objspace/std/smalltupleobject.py
+++ b/pypy/objspace/std/smalltupleobject.py
@@ -1,5 +1,4 @@
 from pypy.interpreter.error import OperationError
-from pypy.objspace.std.sliceobject import W_SliceObject
 from pypy.objspace.std.tupleobject import W_AbstractTupleObject
 from pypy.objspace.std.util import negate
 from rpython.rlib.rarithmetic import intmask
@@ -28,16 +27,9 @@
         def length(self):
             return n
 
-        def descr_getitem(self, space, w_index):
-            if isinstance(w_index, W_SliceObject):
-                return self._getslice(space, w_index)
-            index = space.getindex_w(w_index, space.w_IndexError,
-                                     "tuple index")
+        def getitem(self, space, index):
             if index < 0:
-                index += self.length()
-            return self.getitem(space, index)
-
-        def getitem(self, space, index):
+                index += n
             for i in iter_n:
                 if index == i:
                     return getattr(self,'w_value%s' % i)
diff --git a/pypy/objspace/std/specialisedtupleobject.py 
b/pypy/objspace/std/specialisedtupleobject.py
--- a/pypy/objspace/std/specialisedtupleobject.py
+++ b/pypy/objspace/std/specialisedtupleobject.py
@@ -1,52 +1,15 @@
 from pypy.interpreter.error import OperationError
-from pypy.objspace.std.model import registerimplementation
-from pypy.objspace.std.register_all import register_all
-from pypy.objspace.std.multimethod import FailedToImplement
 from pypy.objspace.std.tupleobject import W_AbstractTupleObject
-from pypy.objspace.std.tupleobject import W_TupleObject
-from pypy.objspace.std.sliceobject import W_SliceObject, normalize_simple_slice
+from pypy.objspace.std.util import negate
+from rpython.rlib.objectmodel import compute_hash
 from rpython.rlib.rarithmetic import intmask
-from rpython.rlib.objectmodel import compute_hash
 from rpython.rlib.unroll import unrolling_iterable
 from rpython.tool.sourcetools import func_with_new_name
 
+
 class NotSpecialised(Exception):
     pass
 
-class W_SpecialisedTupleObject(W_AbstractTupleObject):
-    from pypy.objspace.std.tupletype import tuple_typedef as typedef
-    __slots__ = []
-
-    def __repr__(self):
-        """ representation for debugging purposes """
-        reprlist = [repr(item) for item in self._to_unwrapped_list()]
-        return "%s(%s)" % (self.__class__.__name__, ', '.join(reprlist))
-
-    def _to_unwrapped_list(self):
-        "NOT_RPYTHON"
-        raise NotImplementedError
-
-    def length(self):
-        raise NotImplementedError
-
-    def getitem(self, index):
-        raise NotImplementedError
-
-    def hash(self, space):
-        raise NotImplementedError
-
-    def eq(self, space, w_other):
-        raise NotImplementedError
-
-    def setitem(self, index, w_item):
-        raise NotImplementedError
-
-    def unwrap(self, space):
-        return tuple(self._to_unwrapped_list())
-
-    def delegating(self):
-        pass     # for tests only
-
 
 def make_specialised_class(typetuple):
     assert type(typetuple) == tuple
@@ -54,7 +17,7 @@
     nValues = len(typetuple)
     iter_n = unrolling_iterable(range(nValues))
 
-    class cls(W_SpecialisedTupleObject):
+    class cls(W_AbstractTupleObject):
         def __init__(self, space, *values_w):
             self.space = space
             assert len(values_w) == nValues
@@ -88,17 +51,7 @@
         # same source code, but builds and returns a resizable list
         getitems_copy = func_with_new_name(tolist, 'getitems_copy')
 
-        def _to_unwrapped_list(self):
-            "NOT_RPYTHON"
-            list_w = [None] * nValues
-            for i in iter_n:
-                value = getattr(self, 'value%s' % i)
-                if typetuple[i] == object:
-                    value = self.space.unwrap(value)
-                list_w[i] = value
-            return list_w
-
-        def hash(self, space):
+        def descr_hash(self, space):
             # XXX duplicate logic from tupleobject.py
             mult = 1000003
             x = 0x345678
@@ -120,36 +73,35 @@
             x += 97531
             return space.wrap(intmask(x))
 
-        def _eq(self, w_other):
+        def descr_eq(self, space, w_other):
             if not isinstance(w_other, cls):
                 # if we are not comparing same types, give up
-                raise FailedToImplement
+                return space.w_NotImplemented
             for i in iter_n:
                 myval    = getattr(self,    'value%s' % i)
                 otherval = getattr(w_other, 'value%s' % i)
                 if typetuple[i] == object:
                     if not self.space.eq_w(myval, otherval):
-                        return False
+                        return space.w_False
                 else:
                     if myval != otherval:
-                        return False
+                        return space.w_False
             else:
-                return True
+                return space.w_True
 
-        def eq(self, space, w_other):
-            return space.newbool(self._eq(w_other))
+        descr_ne = negate(descr_eq)
 
-        def ne(self, space, w_other):
-            return space.newbool(not self._eq(w_other))
-
-        def getitem(self, index):
+        def getitem(self, space, index):
+            if index < 0:
+                index += nValues
             for i in iter_n:
                 if index == i:
                     value = getattr(self, 'value%s' % i)
                     if typetuple[i] != object:
-                        value = self.space.wrap(value)
+                        value = space.wrap(value)
                     return value
-            raise IndexError
+            raise OperationError(space.w_IndexError,
+                                 space.wrap("tuple index out of range"))
 
     cls.__name__ = ('W_SpecialisedTupleObject_' +
                     ''.join([t.__name__[0] for t in typetuple]))
@@ -178,64 +130,3 @@
         return Cls_oo(space, w_arg1, w_arg2)
     else:
         raise NotSpecialised
-
-# ____________________________________________________________
-
-registerimplementation(W_SpecialisedTupleObject)
-
-def delegate_SpecialisedTuple2Tuple(space, w_specialised):
-    w_specialised.delegating()
-    return W_TupleObject(w_specialised.tolist())
-
-def len__SpecialisedTuple(space, w_tuple):
-    return space.wrap(w_tuple.length())
-
-def getitem__SpecialisedTuple_ANY(space, w_tuple, w_index):
-    index = space.getindex_w(w_index, space.w_IndexError, "tuple index")
-    if index < 0:
-        index += w_tuple.length()
-    try:
-        return w_tuple.getitem(index)
-    except IndexError:
-        raise OperationError(space.w_IndexError,
-                             space.wrap("tuple index out of range"))
-
-def getitem__SpecialisedTuple_Slice(space, w_tuple, w_slice):
-    length = w_tuple.length()
-    start, stop, step, slicelength = w_slice.indices4(space, length)
-    assert slicelength >= 0
-    subitems = [None] * slicelength
-    for i in range(slicelength):
-        subitems[i] = w_tuple.getitem(start)
-        start += step
-    return space.newtuple(subitems)
-
-def mul_specialisedtuple_times(space, w_tuple, w_times):
-    try:
-        times = space.getindex_w(w_times, space.w_OverflowError)
-    except OperationError, e:
-        if e.match(space, space.w_TypeError):
-            raise FailedToImplement
-        raise
-    if times == 1 and space.type(w_tuple) == space.w_tuple:
-        return w_tuple
-    items = w_tuple.tolist()
-    return space.newtuple(items * times)
-
-def mul__SpecialisedTuple_ANY(space, w_tuple, w_times):
-    return mul_specialisedtuple_times(space, w_tuple, w_times)
-
-def mul__ANY_SpecialisedTuple(space, w_times, w_tuple):
-    return mul_specialisedtuple_times(space, w_tuple, w_times)
-
-def eq__SpecialisedTuple_SpecialisedTuple(space, w_tuple1, w_tuple2):
-    return w_tuple1.eq(space, w_tuple2)
-
-def ne__SpecialisedTuple_SpecialisedTuple(space, w_tuple1, w_tuple2):
-    return w_tuple1.ne(space, w_tuple2)
-
-def hash__SpecialisedTuple(space, w_tuple):
-    return w_tuple.hash(space)
-
-from pypy.objspace.std import tupletype
-register_all(vars(), tupletype)
diff --git a/pypy/objspace/std/test/test_specialisedtupleobject.py 
b/pypy/objspace/std/test/test_specialisedtupleobject.py
--- a/pypy/objspace/std/test/test_specialisedtupleobject.py
+++ b/pypy/objspace/std/test/test_specialisedtupleobject.py
@@ -1,6 +1,5 @@
 import py, sys
 from pypy.objspace.std.tupleobject import W_TupleObject
-from pypy.objspace.std.specialisedtupleobject import W_SpecialisedTupleObject
 from pypy.objspace.std.specialisedtupleobject import _specialisations
 from pypy.interpreter.error import OperationError
 from pypy.tool.pytest.objspace import gettestobjspace
@@ -21,7 +20,7 @@
         
     def test_isnotspecialisedtupleobject(self):
         w_tuple = self.space.newtuple([self.space.wrap({})])
-        assert not isinstance(w_tuple, W_SpecialisedTupleObject)
+        assert not 'W_SpecialisedTupleObject' in type(w_tuple).__name__
         
     def test_specialisedtupleclassname(self):
         w_tuple = self.space.newtuple([self.space.wrap(1), self.space.wrap(2)])
@@ -38,7 +37,7 @@
             S_w_tuple = S_space.newtuple(S_values_w)
 
             if must_be_specialized:
-                assert isinstance(S_w_tuple, W_SpecialisedTupleObject)
+                assert 'W_SpecialisedTupleObject' in type(S_w_tuple).__name__
             assert isinstance(N_w_tuple, W_TupleObject)
             assert S_space.is_true(S_space.eq(N_w_tuple, S_w_tuple))
             assert S_space.is_true(S_space.eq(N_space.hash(N_w_tuple), 
S_space.hash(S_w_tuple)))
@@ -57,23 +56,6 @@
 class AppTestW_SpecialisedTupleObject:
     spaceconfig = {"objspace.std.withspecialisedtuple": True}
 
-    def setup_class(cls):
-        def forbid_delegation(space, w_tuple):
-            def delegation_forbidden():
-                # haaaack
-                co = sys._getframe(2).f_code
-                if co.co_name.startswith('_mm_repr_tuple'):
-                    return
-                raise OperationError(space.w_ReferenceError, w_tuple)
-            w_tuple.delegating = delegation_forbidden
-            return w_tuple
-        if cls.runappdirect:
-            cls.w_forbid_delegation = lambda self, x: x
-            cls.test_delegation = lambda self: skip("runappdirect")
-        else:
-            cls.w_forbid_delegation = cls.space.wrap(
-                gateway.interp2app(forbid_delegation))
-
     def w_isspecialised(self, obj, expected=''):
         import __pypy__
         r = __pypy__.internal_repr(obj)
@@ -101,12 +83,8 @@
             obj = (1, 2, 3)
             assert self.isspecialised(obj, '_ooo')
 
-    def test_delegation(self):
-        t = self.forbid_delegation((42, 43))
-        raises(ReferenceError, t.__getslice__, 0, 1)
-
     def test_len(self):
-        t = self.forbid_delegation((42,43))
+        t = (42, 43)
         assert len(t) == 2
 
     def test_notspecialisedtuple(self):
@@ -133,7 +111,7 @@
 
     def test_eq_no_delegation(self):
         t = (1,)
-        a = self.forbid_delegation(t + (2,))
+        a = t + (2,)
         b = (1, 2)
         assert a == b
 
@@ -151,7 +129,7 @@
                 assert ((1,2) == (x,y)) == (1 == x and 2 == y)
 
     def test_neq(self):
-        a = self.forbid_delegation((1,2))
+        a = (1,2)
         b = (1,)
         b = b+(2,)
         assert not a != b
@@ -160,7 +138,7 @@
         assert a != c
         
     def test_ordering(self):
-        a = (1,2) #self.forbid_delegation((1,2)) --- code commented out
+        a = (1,2)
         assert a <  (2,2)    
         assert a <  (1,3)    
         assert not a <  (1,2) 
@@ -205,7 +183,7 @@
         assert hash(a) == hash((1L, 2L)) == hash((1.0, 2.0)) == hash((1.0, 2L))
 
     def test_getitem(self):
-        t = self.forbid_delegation((5,3))
+        t = (5, 3)
         assert (t)[0] == 5
         assert (t)[1] == 3
         assert (t)[-1] == 3
@@ -216,14 +194,14 @@
     def test_three_tuples(self):
         if not self.isspecialised((1, 2, 3)):
             skip("don't have specialization for 3-tuples")
-        b = self.forbid_delegation((1, 2, 3))
+        b = (1, 2, 3)
         c = (1,)
         d = c + (2, 3)
         assert self.isspecialised(d)
         assert b == d
 
     def test_mongrel(self):
-        a = self.forbid_delegation((2.2, '333'))
+        a = (2.2, '333')
         assert self.isspecialised(a)
         assert len(a) == 2
         assert a[0] == 2.2 and a[1] == '333'
@@ -233,7 +211,7 @@
         #
         if not self.isspecialised((1, 2, 3)):
             skip("don't have specialization for 3-tuples")
-        a = self.forbid_delegation((1, 2.2, '333'))
+        a = (1, 2.2, '333')
         assert self.isspecialised(a)
         assert len(a) == 3
         assert a[0] == 1 and a[1] == 2.2 and a[2] == '333'
diff --git a/pypy/objspace/std/tupleobject.py b/pypy/objspace/std/tupleobject.py
--- a/pypy/objspace/std/tupleobject.py
+++ b/pypy/objspace/std/tupleobject.py
@@ -142,8 +142,11 @@
         items = self.tolist()
         return space.newtuple(items * times)
 
-    def descr_getitem(self, space, w_other):
-        raise NotImplementedError
+    def descr_getitem(self, space, w_index):
+        if isinstance(w_index, W_SliceObject):
+            return self._getslice(space, w_index)
+        index = space.getindex_w(w_index, space.w_IndexError, "tuple index")
+        return self.getitem(space, index)
 
     def _getslice(self, space, w_index):
         items = self.tolist()
@@ -213,7 +216,7 @@
     __mul__ = interp2app(W_AbstractTupleObject.descr_mul),
     __rmul__ = interp2app(W_AbstractTupleObject.descr_mul),
 
-    __getitem__ = interpindirect2app(W_AbstractTupleObject.descr_getitem),
+    __getitem__ = interp2app(W_AbstractTupleObject.descr_getitem),
     __getslice__ = interp2app(W_AbstractTupleObject.descr_getslice),
 
     __getnewargs__ = interp2app(W_AbstractTupleObject.descr_getnewargs),
@@ -264,16 +267,6 @@
 
     descr_ne = negate(descr_eq)
 
-    def descr_getitem(self, space, w_index):
-        if isinstance(w_index, W_SliceObject):
-            return self._getslice(space, w_index)
-        index = space.getindex_w(w_index, space.w_IndexError, "tuple index")
-        try:
-            return self.wrappeditems[index]
-        except IndexError:
-            raise OperationError(space.w_IndexError,
-                                 space.wrap("tuple index out of range"))
-
     def getitem(self, space, index):
         try:
             return self.wrappeditems[index]
diff --git a/pypy/objspace/std/tupletype.py b/pypy/objspace/std/tupletype.py
--- a/pypy/objspace/std/tupletype.py
+++ b/pypy/objspace/std/tupletype.py
@@ -3,13 +3,12 @@
 def wraptuple(space, list_w):
     from pypy.objspace.std.tupleobject import W_TupleObject
 
-    # XXX fix specialisedtuple
-    #if space.config.objspace.std.withspecialisedtuple:
-    #    from specialisedtupleobject import makespecialisedtuple, 
NotSpecialised
-    #    try:
-    #        return makespecialisedtuple(space, list_w)
-    #    except NotSpecialised:
-    #        pass
+    if space.config.objspace.std.withspecialisedtuple:
+        from specialisedtupleobject import makespecialisedtuple, NotSpecialised
+        try:
+            return makespecialisedtuple(space, list_w)
+        except NotSpecialised:
+            pass
 
     if space.config.objspace.std.withsmalltuple:
         from pypy.objspace.std.smalltupleobject import W_SmallTupleObject2
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to