]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Merged revisions 69188 via svnmerge from
authorMark Dickinson <dickinsm@gmail.com>
Sun, 1 Feb 2009 14:09:04 +0000 (14:09 +0000)
committerMark Dickinson <dickinsm@gmail.com>
Sun, 1 Feb 2009 14:09:04 +0000 (14:09 +0000)
svn+ssh://pythondev@svn.python.org/python/branches/py3k

........
  r69188 | mark.dickinson | 2009-02-01 13:59:22 +0000 (Sun, 01 Feb 2009) | 6 lines

  Issue #1717, continued:  remove PyObject_Compare and Py_CmpToRich declarations
  from object.h; don't inherit tp_compare slot on subclasses; and raise TypeError
  when initializing a type that has a nonzero tp_compare slot.  Fix up
  comparison-related comments in object.c and code.h.
........

Include/code.h
Include/object.h
Misc/NEWS
Objects/object.c
Objects/typeobject.c

index 40cce180f51618431becfef341fc6b3c3a66144e..d738d8dd8355067ed9a004c4a77c9993781c3265 100644 (file)
@@ -20,7 +20,7 @@ typedef struct {
     PyObject *co_varnames;     /* tuple of strings (local variable names) */
     PyObject *co_freevars;     /* tuple of strings (free variable names) */
     PyObject *co_cellvars;      /* tuple of strings (cell variable names) */
-    /* The rest doesn't count for hash/cmp */
+    /* The rest doesn't count for hash or comparisons */
     PyObject *co_filename;     /* unicode (where it was loaded from) */
     PyObject *co_name;         /* unicode (name, for reference) */
     int co_firstlineno;                /* first source line number */
index c0f61813555c73a8ffa2bcac0143a567e95d74e6..e9c67fda0811ff95420fa3c91c1f7fb28dd85b0d 100644 (file)
@@ -426,10 +426,8 @@ PyAPI_FUNC(PyObject *) PyObject_Repr(PyObject *);
 PyAPI_FUNC(PyObject *) PyObject_Str(PyObject *);
 PyAPI_FUNC(PyObject *) PyObject_ASCII(PyObject *);
 PyAPI_FUNC(PyObject *) PyObject_Bytes(PyObject *);
-PyAPI_FUNC(int) PyObject_Compare(PyObject *, PyObject *);
 PyAPI_FUNC(PyObject *) PyObject_RichCompare(PyObject *, PyObject *, int);
 PyAPI_FUNC(int) PyObject_RichCompareBool(PyObject *, PyObject *, int);
-PyAPI_FUNC(PyObject *) Py_CmpToRich(int op, int cmp);
 PyAPI_FUNC(PyObject *) PyObject_GetAttrString(PyObject *, const char *);
 PyAPI_FUNC(int) PyObject_SetAttrString(PyObject *, const char *, PyObject *);
 PyAPI_FUNC(int) PyObject_HasAttrString(PyObject *, const char *);
index 7062da2040be34803cd960eae71d0956405258be..8967d9194e3b4242a6e78bf7f54c9cbf0bc8d863 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,9 +12,11 @@ What's New in Python 3.0.1?
 Core and Builtins
 -----------------
 
-- Issue #1717: Remove builtin cmp() function, C-API functions
-  PyObject_Cmp and PyObject_Compare, and the support function
-  Py_CmpToRich.
+- Issue #1717: Removed builtin cmp() function, dropped tp_compare
+  slot, the C API functions PyObject_Compare and PyUnicode_Compare and
+  the type definition cmpfunc.  The tp_compare slot is reserved for
+  future usage.  An attempt to initialize a type with a nonzero
+  tp_compare slot will raise TypeError.
 
 - Issue #4707: round(x, n) now returns an integer if x is an integer.
   Previously it returned a float.
index 184c9a864ca5c9d5694912e1107be3b4bcea75df..05010a5166500c4bd65d21ac75e939f0789aa994 100644 (file)
@@ -495,35 +495,19 @@ PyObject_Bytes(PyObject *v)
        return PyBytes_FromObject(v);
 }
 
-/* The new comparison philosophy is: we completely separate three-way
-   comparison from rich comparison.  That is, PyObject_Compare() and
-   PyObject_Cmp() *just* use the tp_compare slot.  And PyObject_RichCompare()
-   and PyObject_RichCompareBool() *just* use the tp_richcompare slot.
+/* For Python 3.0.1 and later, the old three-way comparison has been
+   completely removed in favour of rich comparisons.  PyObject_Compare() and
+   PyObject_Cmp() are gone, and the builtin cmp function no longer exists.
+   The old tp_compare slot will be renamed to tp_reserved, and should no
+   longer be used.  Use tp_richcompare instead.
 
    See (*) below for practical amendments.
 
-   IOW, only cmp() uses tp_compare; the comparison operators (==, !=, <=, <,
-   >=, >) only use tp_richcompare.  Note that list.sort() only uses <.
+   tp_richcompare gets called with a first argument of the appropriate type
+   and a second object of an arbitrary type.  We never do any kind of
+   coercion.
 
-   (And yes, eventually we'll rip out cmp() and tp_compare.)
-
-   The calling conventions are different: tp_compare only gets called with two
-   objects of the appropriate type; tp_richcompare gets called with a first
-   argument of the appropriate type and a second object of an arbitrary type.
-   We never do any kind of coercion.
-
-   The return conventions are also different.
-
-   The tp_compare slot should return a C int, as follows:
-
-     -1 if a < b or if an exception occurred
-      0 if a == b
-     +1 if a > b
-
-   No other return values are allowed.  PyObject_Compare() has the same
-   calling convention.
-
-  The tp_richcompare slot should return an object, as follows:
+   The tp_richcompare slot should return an object, as follows:
 
     NULL if an exception occurred
     NotImplemented if the requested comparison is not implemented
@@ -539,9 +523,6 @@ PyObject_Bytes(PyObject *v)
     comparing the object pointer (i.e. falling back to the base object
     implementation).
 
-  - If three-way comparison is not implemented, it falls back on rich
-    comparison (but not the other way around!).
-
 */
 
 /* Map rich comparison operators to their swapped version, e.g. LT <--> GT */
index 4170de7512effe8939612de8324531de1bcb4ef7..379276ed7dfc978f5d66892e92b74b479555f6a4 100644 (file)
@@ -3662,7 +3662,7 @@ inherit_slots(PyTypeObject *type, PyTypeObject *base)
                type->tp_setattr = base->tp_setattr;
                type->tp_setattro = base->tp_setattro;
        }
-       /* tp_compare see tp_richcompare */
+       /* tp_compare is ignored, see tp_richcompare */
        COPYSLOT(tp_repr);
        /* tp_hash see tp_richcompare */
        COPYSLOT(tp_call);
@@ -3670,12 +3670,10 @@ inherit_slots(PyTypeObject *type, PyTypeObject *base)
        {
                /* Copy comparison-related slots only when
                   not overriding them anywhere */
-               if (type->tp_compare == NULL &&
-                   type->tp_richcompare == NULL &&
+               if (type->tp_richcompare == NULL &&
                    type->tp_hash == NULL &&
                    !overrides_hash(type))
                {
-                       type->tp_compare = base->tp_compare;
                        type->tp_richcompare = base->tp_richcompare;
                        type->tp_hash = base->tp_hash;
                }
@@ -3888,6 +3886,13 @@ PyType_Ready(PyTypeObject *type)
                        goto error;
        }
 
+       /* Check reserved slots */
+       if (type->tp_compare) {
+               PyErr_Format(PyExc_TypeError,
+                            "type %s has tp_compare",
+                            type->tp_name);
+       }
+
        /* All done -- set the ready flag */
        assert(type->tp_dict != NULL);
        type->tp_flags =