Author: Maciej Fijalkowski <[email protected]>
Branch: remove-list-smm
Changeset: r62515:bbb0aca228e5
Date: 2013-03-19 16:54 -0700
http://bitbucket.org/pypy/pypy/changeset/bbb0aca228e5/

Log:    (alex, arigo, fijal) start removing strange stuff on lists
        (multimethods in particular)

diff --git a/pypy/interpreter/gateway.py b/pypy/interpreter/gateway.py
--- a/pypy/interpreter/gateway.py
+++ b/pypy/interpreter/gateway.py
@@ -159,8 +159,13 @@
         self.orig_arg()
 
     def visit__W_Root(self, el, app_sig):
+        argname = self.orig_arg()
+        if argname == 'self':
+            # for W_ListObject and similar to be possible to have
+            # unwrap_spec in methods
+            app_sig.append(argname)
+            return
         assert el is W_Root, "%s is not W_Root (forgotten to put .im_func in 
interp2app argument?)" % (el,)
-        argname = self.orig_arg()
         assert argname.startswith('w_'), (
             "argument %s of built-in function %r should "
             "start with 'w_'" % (argname, self.func))
diff --git a/pypy/module/__builtin__/app_functional.py 
b/pypy/module/__builtin__/app_functional.py
--- a/pypy/module/__builtin__/app_functional.py
+++ b/pypy/module/__builtin__/app_functional.py
@@ -14,7 +14,7 @@
 
 # ____________________________________________________________
 
-def sorted(lst, cmp=None, key=None, reverse=None):
+def sorted(lst, cmp=None, key=None, reverse=False):
     "sorted(iterable, cmp=None, key=None, reverse=False) --> new sorted list"
     sorted_lst = list(lst)
     sorted_lst.sort(cmp, key, reverse)
diff --git a/pypy/objspace/std/bytearrayobject.py 
b/pypy/objspace/std/bytearrayobject.py
--- a/pypy/objspace/std/bytearrayobject.py
+++ b/pypy/objspace/std/bytearrayobject.py
@@ -9,8 +9,7 @@
 from rpython.rlib.debug import check_annotation
 from pypy.objspace.std import stringobject
 from pypy.objspace.std.intobject import W_IntObject
-from pypy.objspace.std.listobject import get_positive_index
-from pypy.objspace.std.listtype import get_list_index
+from pypy.objspace.std.listobject import get_positive_index, get_list_index
 from pypy.objspace.std.sliceobject import W_SliceObject, normalize_simple_slice
 from pypy.objspace.std.stringobject import W_StringObject
 from pypy.objspace.std.strutil import ParseStringError
diff --git a/pypy/objspace/std/bytearraytype.py 
b/pypy/objspace/std/bytearraytype.py
--- a/pypy/objspace/std/bytearraytype.py
+++ b/pypy/objspace/std/bytearraytype.py
@@ -13,8 +13,8 @@
     str_expandtabs, str_ljust, str_rjust, str_center, str_zfill,
     str_join, str_split, str_rsplit, str_partition, str_rpartition,
     str_splitlines, str_translate)
-from pypy.objspace.std.listtype import (
-    list_append, list_extend)
+from pypy.objspace.std.listobject import list_append, list_extend
+
 from rpython.rlib.objectmodel import newlist_hint, resizelist_hint
 
 
diff --git a/pypy/objspace/std/listobject.py b/pypy/objspace/std/listobject.py
--- a/pypy/objspace/std/listobject.py
+++ b/pypy/objspace/std/listobject.py
@@ -4,7 +4,6 @@
 from pypy.interpreter.error import OperationError, operationerrfmt
 from pypy.interpreter.generator import GeneratorIterator
 from pypy.objspace.std.inttype import wrapint
-from pypy.objspace.std.listtype import get_list_index
 from pypy.objspace.std.sliceobject import W_SliceObject, normalize_simple_slice
 from pypy.objspace.std import slicetype
 from pypy.interpreter import gateway, baseobjspace
@@ -14,6 +13,8 @@
 from rpython.rlib.listsort import make_timsort_class
 from rpython.rlib import rerased, jit, debug
 from rpython.tool.sourcetools import func_with_new_name
+from pypy.objspace.std.stdtypedef import StdTypeDef, SMM
+from sys import maxint
 
 UNROLL_CUTOFF = 5
 
@@ -122,8 +123,6 @@
     return type(w_object) is W_FloatObject
 
 class W_ListObject(W_AbstractListObject):
-    from pypy.objspace.std.listtype import list_typedef as typedef
-
     def __init__(w_self, space, wrappeditems, sizehint=-1):
         assert isinstance(wrappeditems, list)
         w_self.space = space
@@ -324,6 +323,80 @@
         argument reverse. Argument must be unwrapped."""
         self.strategy.sort(self, reverse)
 
+    @gateway.unwrap_spec(reverse=bool)
+    def descr_sort(self, space, w_cmp=None, w_key=None, reverse=False):
+        """ L.sort(cmp=None, key=None, reverse=False) -- stable
+        sort *IN PLACE*;
+        cmp(x, y) -> -1, 0, 1"""
+        has_cmp = not space.is_none(w_cmp)
+        has_key = not space.is_none(w_key)
+
+        # create and setup a TimSort instance
+        if has_cmp:
+            if has_key:
+                sorterclass = CustomKeyCompareSort
+            else:
+                sorterclass = CustomCompareSort
+        else:
+            if has_key:
+                sorterclass = CustomKeySort
+            else:
+                if self.strategy is space.fromcache(ObjectListStrategy):
+                    sorterclass = SimpleSort
+                else:
+                    self.sort(reverse)
+                    return space.w_None
+
+        sorter = sorterclass(self.getitems(), self.length())
+        sorter.space = space
+        sorter.w_cmp = w_cmp
+
+        try:
+            # The list is temporarily made empty, so that mutations performed
+            # by comparison functions can't affect the slice of memory we're
+            # sorting (allowing mutations during sorting is an IndexError or
+            # core-dump factory, since the storage may change).
+            self.__init__(space, [])
+
+            # wrap each item in a KeyContainer if needed
+            if has_key:
+                for i in range(sorter.listlength):
+                    w_item = sorter.list[i]
+                    w_keyitem = space.call_function(w_key, w_item)
+                    sorter.list[i] = KeyContainer(w_keyitem, w_item)
+
+            # Reverse sort stability achieved by initially reversing the list,
+            # applying a stable forward sort, then reversing the final result.
+            if reverse:
+                sorter.list.reverse()
+
+            # perform the sort
+            sorter.sort()
+
+            # reverse again
+            if reverse:
+                sorter.list.reverse()
+
+        finally:
+            # unwrap each item if needed
+            if has_key:
+                for i in range(sorter.listlength):
+                    w_obj = sorter.list[i]
+                    if isinstance(w_obj, KeyContainer):
+                        sorter.list[i] = w_obj.w_item
+
+            # check if the user mucked with the list during the sort
+            mucked = self.length() > 0
+
+            # put the items back into the list
+            self.__init__(space, sorter.list)
+
+        if mucked:
+            raise OperationError(space.w_ValueError,
+                                 space.wrap("list modified during sort"))
+
+        return space.w_None
+
 registerimplementation(W_ListObject)
 
 
@@ -1560,78 +1633,57 @@
         assert isinstance(b, KeyContainer)
         return CustomCompareSort.lt(self, a.w_key, b.w_key)
 
-def list_sort__List_ANY_ANY_ANY(space, w_list, w_cmp, w_keyfunc, w_reverse):
+def list_reversed__ANY(space, w_list):
+    from pypy.objspace.std.iterobject import W_ReverseSeqIterObject
+    return W_ReverseSeqIterObject(space, w_list, -1)
 
-    has_cmp = not space.is_w(w_cmp, space.w_None)
-    has_key = not space.is_w(w_keyfunc, space.w_None)
-    has_reverse = space.is_true(w_reverse)
+# ____________________________________________________________
 
-    # create and setup a TimSort instance
-    if has_cmp:
-        if has_key:
-            sorterclass = CustomKeyCompareSort
-        else:
-            sorterclass = CustomCompareSort
-    else:
-        if has_key:
-            sorterclass = CustomKeySort
-        else:
-            if w_list.strategy is space.fromcache(ObjectListStrategy):
-                sorterclass = SimpleSort
-            else:
-                w_list.sort(has_reverse)
-                return space.w_None
+def descr_new(space, w_listtype, __args__):
+    w_obj = space.allocate_instance(W_ListObject, w_listtype)
+    w_obj.clear(space)
+    return w_obj
 
-    sorter = sorterclass(w_list.getitems(), w_list.length())
-    sorter.space = space
-    sorter.w_cmp = w_cmp
+# ____________________________________________________________
 
-    try:
-        # The list is temporarily made empty, so that mutations performed
-        # by comparison functions can't affect the slice of memory we're
-        # sorting (allowing mutations during sorting is an IndexError or
-        # core-dump factory, since the storage may change).
-        w_list.__init__(space, [])
+# ____________________________________________________________
 
-        # wrap each item in a KeyContainer if needed
-        if has_key:
-            for i in range(sorter.listlength):
-                w_item = sorter.list[i]
-                w_key = space.call_function(w_keyfunc, w_item)
-                sorter.list[i] = KeyContainer(w_key, w_item)
+def get_list_index(space, w_index):
+    return space.getindex_w(w_index, space.w_IndexError, "list index")
 
-        # Reverse sort stability achieved by initially reversing the list,
-        # applying a stable forward sort, then reversing the final result.
-        if has_reverse:
-            sorter.list.reverse()
+list_append   = SMM('append', 2,
+                doc='L.append(object) -- append object to end')
+list_insert   = SMM('insert', 3,
+                    doc='L.insert(index, object) -- insert object before 
index')
+list_extend   = SMM('extend', 2,
+                    doc='L.extend(iterable) -- extend list by appending'
+                        ' elements from the iterable')
+list_pop      = SMM('pop',    2, defaults=(None,),
+                    doc='L.pop([index]) -> item -- remove and return item at'
+                        ' index (default last)')
+list_remove   = SMM('remove', 2,
+                    doc='L.remove(value) -- remove first occurrence of value')
+list_index    = SMM('index',  4, defaults=(0,maxint),
+                    doc='L.index(value, [start, [stop]]) -> integer -- return'
+                        ' first index of value')
+list_count    = SMM('count',  2,
+                    doc='L.count(value) -> integer -- return number of'
+                        ' occurrences of value')
+list_reverse  = SMM('reverse',1,
+                    doc='L.reverse() -- reverse *IN PLACE*')
+list_reversed = SMM('__reversed__', 1,
+                    doc='L.__reversed__() -- return a reverse iterator over'
+                        ' the list')
 
-        # perform the sort
-        sorter.sort()
+register_all(vars(), globals())
 
-        # reverse again
-        if has_reverse:
-            sorter.list.reverse()
+W_ListObject.typedef = StdTypeDef("list",
+    __doc__ = """list() -> new list
+list(sequence) -> new list initialized from sequence's items""",
+    __new__ = gateway.interp2app(descr_new),
+    __hash__ = None,
+    sort = gateway.interp2app(W_ListObject.descr_sort),
+    )
+W_ListObject.typedef.registermethods(globals())
 
-    finally:
-        # unwrap each item if needed
-        if has_key:
-            for i in range(sorter.listlength):
-                w_obj = sorter.list[i]
-                if isinstance(w_obj, KeyContainer):
-                    sorter.list[i] = w_obj.w_item
-
-        # check if the user mucked with the list during the sort
-        mucked = w_list.length() > 0
-
-        # put the items back into the list
-        w_list.__init__(space, sorter.list)
-
-    if mucked:
-        raise OperationError(space.w_ValueError,
-                             space.wrap("list modified during sort"))
-
-    return space.w_None
-
-
-from pypy.objspace.std import listtype
-register_all(vars(), listtype)
+list_typedef = W_ListObject.typedef
diff --git a/pypy/objspace/std/listtype.py b/pypy/objspace/std/listtype.py
deleted file mode 100644
--- a/pypy/objspace/std/listtype.py
+++ /dev/null
@@ -1,62 +0,0 @@
-from pypy.interpreter import gateway
-from pypy.interpreter.error import OperationError
-from pypy.objspace.std.stdtypedef import StdTypeDef, SMM
-from pypy.objspace.std.register_all import register_all
-from sys import maxint
-
-list_append   = SMM('append', 2,
-                    doc='L.append(object) -- append object to end')
-list_insert   = SMM('insert', 3,
-                    doc='L.insert(index, object) -- insert object before 
index')
-list_extend   = SMM('extend', 2,
-                    doc='L.extend(iterable) -- extend list by appending'
-                        ' elements from the iterable')
-list_pop      = SMM('pop',    2, defaults=(None,),
-                    doc='L.pop([index]) -> item -- remove and return item at'
-                        ' index (default last)')
-list_remove   = SMM('remove', 2,
-                    doc='L.remove(value) -- remove first occurrence of value')
-list_index    = SMM('index',  4, defaults=(0,maxint),
-                    doc='L.index(value, [start, [stop]]) -> integer -- return'
-                        ' first index of value')
-list_count    = SMM('count',  2,
-                    doc='L.count(value) -> integer -- return number of'
-                        ' occurrences of value')
-list_reverse  = SMM('reverse',1,
-                    doc='L.reverse() -- reverse *IN PLACE*')
-list_sort     = SMM('sort',   4, defaults=(None, None, False),
-                    argnames=['cmp', 'key', 'reverse'],
-                    doc='L.sort(cmp=None, key=None, reverse=False) -- stable'
-                        ' sort *IN PLACE*;\ncmp(x, y) -> -1, 0, 1')
-list_reversed = SMM('__reversed__', 1,
-                    doc='L.__reversed__() -- return a reverse iterator over'
-                        ' the list')
-
-def list_reversed__ANY(space, w_list):
-    from pypy.objspace.std.iterobject import W_ReverseSeqIterObject
-    return W_ReverseSeqIterObject(space, w_list, -1)
-
-register_all(vars(), globals())
-
-# ____________________________________________________________
-
-def descr__new__(space, w_listtype, __args__):
-    from pypy.objspace.std.listobject import W_ListObject
-    w_obj = space.allocate_instance(W_ListObject, w_listtype)
-    w_obj.clear(space)
-    return w_obj
-
-# ____________________________________________________________
-
-list_typedef = StdTypeDef("list",
-    __doc__ = '''list() -> new list
-list(sequence) -> new list initialized from sequence's items''',
-    __new__ = gateway.interp2app(descr__new__),
-    __hash__ = None,
-    )
-list_typedef.registermethods(globals())
-
-# ____________________________________________________________
-
-def get_list_index(space, w_index):
-    return space.getindex_w(w_index, space.w_IndexError, "list index")
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
@@ -20,8 +20,6 @@
     "withsmallint"   : ["smallintobject.W_SmallIntObject"],
     "withsmalllong"  : ["smalllongobject.W_SmallLongObject"],
     "withstrbuf"     : ["strbufobject.W_StringBufferObject"],
-    "withtproxy" : ["proxyobject.W_TransparentList",
-                    "proxyobject.W_TransparentDict"],
 }
 
 IDTAG_INT     = 1
@@ -44,7 +42,7 @@
             from pypy.objspace.std.settype import set_typedef
             from pypy.objspace.std.frozensettype import frozenset_typedef
             from pypy.objspace.std.tupletype  import tuple_typedef
-            from pypy.objspace.std.listtype   import list_typedef
+            from pypy.objspace.std.listobject   import list_typedef
             from pypy.objspace.std.dicttype   import dict_typedef
             from pypy.objspace.std.basestringtype import basestring_typedef
             from pypy.objspace.std.stringtype import str_typedef
diff --git a/pypy/objspace/std/proxyobject.py b/pypy/objspace/std/proxyobject.py
--- a/pypy/objspace/std/proxyobject.py
+++ b/pypy/objspace/std/proxyobject.py
@@ -97,17 +97,3 @@
 
 class W_TransparentGenerator(W_Transparent):
     typedef = GeneratorIterator.typedef
-
-class W_TransparentList(W_TransparentObject):
-    from pypy.objspace.std.listobject import W_ListObject as original
-    from pypy.objspace.std.listtype import list_typedef as typedef
-
-class W_TransparentDict(W_TransparentObject):
-    from pypy.objspace.std.dictmultiobject import W_DictMultiObject as original
-    from pypy.objspace.std.dicttype import dict_typedef as typedef
-
-registerimplementation(W_TransparentList)
-registerimplementation(W_TransparentDict)
-
-register_type(W_TransparentList)
-register_type(W_TransparentDict)
diff --git a/pypy/objspace/std/transparent.py b/pypy/objspace/std/transparent.py
--- a/pypy/objspace/std/transparent.py
+++ b/pypy/objspace/std/transparent.py
@@ -39,10 +39,6 @@
         raise OperationError(space.w_TypeError, space.wrap("controller should 
be function"))
     
     if isinstance(w_type, W_TypeObject):
-        if space.is_true(space.issubtype(w_type, space.w_list)):
-            return W_TransparentList(space, w_type, w_controller)
-        if space.is_true(space.issubtype(w_type, space.w_dict)):
-            return W_TransparentDict(space, w_type, w_controller)
         if space.is_true(space.issubtype(w_type, 
space.gettypeobject(Function.typedef))):
             return W_TransparentFunction(space, w_type, w_controller)
         if space.is_true(space.issubtype(w_type, 
space.gettypeobject(PyTraceback.typedef))):
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to