[pypy-svn] r7498 - pypy/trunk/src/pypy/objspace/std

arigo at codespeak.net arigo at codespeak.net
Sat Nov 20 12:12:06 CET 2004


Author: arigo
Date: Sat Nov 20 12:12:06 2004
New Revision: 7498

Modified:
   pypy/trunk/src/pypy/objspace/std/listobject.py
Log:
RPython-ify the implementation of list.sort().



Modified: pypy/trunk/src/pypy/objspace/std/listobject.py
==============================================================================
--- pypy/trunk/src/pypy/objspace/std/listobject.py	(original)
+++ pypy/trunk/src/pypy/objspace/std/listobject.py	Sat Nov 20 12:12:06 2004
@@ -499,17 +499,33 @@
         _quicksort(list, start, split-1, lt)        # ... and sort both halves.
         _quicksort(list, split+1, end, lt)
 
+class Comparer:
+    """Just a dumb container class for a space and a w_cmp, because
+    we can't use nested scopes for that in RPython.
+    """
+    def __init__(self, space, w_cmp):
+        self.space = space
+        self.w_cmp = w_cmp
+
+    def simple_lt(self, a, b):
+        space = self.space
+        return space.is_true(space.lt(a, b))
+
+    def complex_lt(self, a, b):
+        space = self.space
+        w_cmp = self.w_cmp
+        result = space.unwrap(space.call_function(w_cmp, a, b))
+        if not isinstance(result,int):
+            raise OperationError(space.w_TypeError,
+                     space.wrap("comparison function must return int"))
+        return result < 0
+
 def list_sort__List_ANY(space, w_list, w_cmp):
+    comparer = Comparer(space, w_cmp)
     if w_cmp is space.w_None:
-        def lt(a,b):
-            return space.is_true(space.lt(a,b))
+        lt = comparer.simple_lt
     else:
-        def lt(a,b):
-            result = space.unwrap(space.call_function(w_cmp, a, b))
-            if not isinstance(result,int):
-                raise OperationError(space.w_TypeError,
-                         space.wrap("comparison function must return int"))
-            return result < 0
+        lt = comparer.complex_lt
 
     # XXX Basic quicksort implementation
     # XXX this is not stable !!



More information about the Pypy-commit mailing list