[pypy-svn] r8842 - in pypy/dist/pypy: interpreter module objspace

arigo at codespeak.net arigo at codespeak.net
Fri Feb 4 13:43:46 CET 2005


Author: arigo
Date: Thu Feb  3 20:16:29 2005
New Revision: 8842

Modified:
   pypy/dist/pypy/interpreter/baseobjspace.py
   pypy/dist/pypy/module/__builtin__interp.py
   pypy/dist/pypy/module/__builtin__module.py
   pypy/dist/pypy/objspace/descroperation.py
Log:
Argh, it seems that we need a 'cmp' space operation to implement the built-in
cmp() correctly.


Modified: pypy/dist/pypy/interpreter/baseobjspace.py
==============================================================================
--- pypy/dist/pypy/interpreter/baseobjspace.py	(original)
+++ pypy/dist/pypy/interpreter/baseobjspace.py	Thu Feb  3 20:16:29 2005
@@ -128,7 +128,7 @@
     def is_w(self, w_obj1, w_obj2):
         """shortcut for space.is_true(space.is_(w_obj1, w_obj2))"""
         return self.is_true(self.is_(w_obj1, w_obj2))
-    
+
     def newbool(self, b):
         if b:
             return self.w_True
@@ -310,6 +310,7 @@
     ('ne',              '!=',        2, ['__ne__', '__ne__']),
     ('gt',              '>',         2, ['__gt__', '__lt__']),
     ('ge',              '>=',        2, ['__ge__', '__le__']),
+    ('cmp',             'cmp',       2, ['__cmp__']),   # rich cmps preferred
     ('contains',        'contains',  2, ['__contains__']),
     ('iter',            'iter',      1, ['__iter__']),
     ('next',            'next',      1, ['next']),

Modified: pypy/dist/pypy/module/__builtin__interp.py
==============================================================================
--- pypy/dist/pypy/module/__builtin__interp.py	(original)
+++ pypy/dist/pypy/module/__builtin__interp.py	Thu Feb  3 20:16:29 2005
@@ -286,6 +286,10 @@
 def id(w_object):
     return space.id(w_object)
 
+def cmp(w_x, w_y):
+    """return 0 when x == y, -1 when x < y and 1 when x > y """
+    return space.cmp(w_x, w_y)
+
 #XXX works only for new-style classes.
 #So we have to fix it, when we add support for old-style classes
 def _issubtype(w_cls1, w_cls2):

Modified: pypy/dist/pypy/module/__builtin__module.py
==============================================================================
--- pypy/dist/pypy/module/__builtin__module.py	(original)
+++ pypy/dist/pypy/module/__builtin__module.py	Thu Feb  3 20:16:29 2005
@@ -277,20 +277,6 @@
 def divmod(x, y):
     return x//y, x%y
 
-def cmp(x, y):
-    """return 0 when x == y, -1 when x < y and 1 when x > y """
-    if x is y:
-        return 0
-    if type(x) is type(y):
-        if hasattr(x, '__cmp__'):
-            return x.__cmp__(y)
-    if x == y:
-        return 0
-    elif x < y:
-        return -1
-    else:
-        return 1
-
 def vars(*obj):
     """return a dictionary of all the attributes currently bound in obj.  If
     called with no argument, return the variables bound in local scope."""
@@ -447,7 +433,7 @@
 __interplevel__execfile('__builtin__interp.py')
 
 from __interplevel__ import abs, chr, len, ord, pow, repr
-from __interplevel__ import hash, oct, hex, round
+from __interplevel__ import hash, oct, hex, round, cmp
 from __interplevel__ import getattr, setattr, delattr, iter, hash, id
 from __interplevel__ import _issubtype
 from __interplevel__ import compile, eval

Modified: pypy/dist/pypy/objspace/descroperation.py
==============================================================================
--- pypy/dist/pypy/objspace/descroperation.py	(original)
+++ pypy/dist/pypy/objspace/descroperation.py	Thu Feb  3 20:16:29 2005
@@ -253,6 +253,24 @@
         if w_del is not None:
             space.get_and_call_function(w_del, w_obj)
 
+    def cmp(space, w_x, w_y):
+        # full compliant implementation of the built-in cmp().
+        if space.is_w(w_x, w_y):
+            return space.wrap(0)   # identical objects always compare equal.
+        if space.is_w(space.type(w_x), space.type(w_y)):
+            # for object of the same type, prefer __cmp__ over rich comparison.
+            w_cmp = space.lookup(w_x, '__cmp__')
+            w_res = _invoke_binop(space, w_cmp, w_x, w_y)
+            if w_res is not None:
+                return w_res
+        # fall back to rich comparison.
+        if space.eq_w(w_x, w_y):
+            return space.wrap(0)
+        elif space.is_true(space.lt(w_x, w_y)):
+            return space.wrap(-1)
+        else:
+            return space.wrap(1)
+
     # xxx round, ord
 
 



More information about the Pypy-commit mailing list