summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorSteven Bethard <steven.bethard@gmail.com>2008-03-18 17:26:10 (GMT)
committerSteven Bethard <steven.bethard@gmail.com>2008-03-18 17:26:10 (GMT)
commitae42f33cdfb68be2fc71be0fb67ec025f90160e8 (patch)
tree07985bc750d9266f2e725b6ba46b0f71c17c8d08 /Objects
parenta8b09fd4c33572a204a80c81e5755cc6d164805d (diff)
downloadcpython-ae42f33cdfb68be2fc71be0fb67ec025f90160e8.zip
cpython-ae42f33cdfb68be2fc71be0fb67ec025f90160e8.tar.gz
cpython-ae42f33cdfb68be2fc71be0fb67ec025f90160e8.tar.bz2
Add py3k warnings for object, type, cell and dict comparisons. This should resolve issue2342 and partly resolve issue2373.
Diffstat (limited to 'Objects')
-rw-r--r--Objects/cellobject.c6
-rw-r--r--Objects/dictobject.c8
-rw-r--r--Objects/object.c12
-rw-r--r--Objects/typeobject.c44
4 files changed, 67 insertions, 3 deletions
diff --git a/Objects/cellobject.c b/Objects/cellobject.c
index b72d43b..37cde41 100644
--- a/Objects/cellobject.c
+++ b/Objects/cellobject.c
@@ -54,6 +54,12 @@ cell_dealloc(PyCellObject *op)
static int
cell_compare(PyCellObject *a, PyCellObject *b)
{
+ /* Py3K warning for comparisons */
+ if (Py_Py3kWarningFlag && PyErr_Warn(PyExc_DeprecationWarning,
+ "cell comparisons not supported in 3.x.") < 0) {
+ return NULL;
+ }
+
if (a->ob_ref == NULL) {
if (b->ob_ref == NULL)
return 0;
diff --git a/Objects/dictobject.c b/Objects/dictobject.c
index 1246785..1ca2830 100644
--- a/Objects/dictobject.c
+++ b/Objects/dictobject.c
@@ -1776,8 +1776,14 @@ dict_richcompare(PyObject *v, PyObject *w, int op)
return NULL;
res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
}
- else
+ else {
+ /* Py3K warning if comparison isn't == or != */
+ if (Py_Py3kWarningFlag && PyErr_Warn(PyExc_DeprecationWarning,
+ "dict inequality comparisons not supported in 3.x.") < 0) {
+ return NULL;
+ }
res = Py_NotImplemented;
+ }
Py_INCREF(res);
return res;
}
diff --git a/Objects/object.c b/Objects/object.c
index 698ba47..a10ac7c 100644
--- a/Objects/object.c
+++ b/Objects/object.c
@@ -863,8 +863,18 @@ try_3way_to_rich_compare(PyObject *v, PyObject *w, int op)
int c;
c = try_3way_compare(v, w);
- if (c >= 2)
+ if (c >= 2) {
+
+ /* Py3K warning if types are not equal and comparison isn't == or != */
+ if (Py_Py3kWarningFlag &&
+ v->ob_type != w->ob_type && op != Py_EQ && op != Py_NE &&
+ PyErr_Warn(PyExc_DeprecationWarning,
+ "comparing unequal types not supported in 3.x.") < 0) {
+ return NULL;
+ }
+
c = default_3way_compare(v, w);
+ }
if (c <= -2)
return NULL;
return convert_3way_to_object(op, c);
diff --git a/Objects/typeobject.c b/Objects/typeobject.c
index 7db6dac..82ced39 100644
--- a/Objects/typeobject.c
+++ b/Objects/typeobject.c
@@ -593,6 +593,48 @@ type_compare(PyObject *v, PyObject *w)
return (vv < ww) ? -1 : (vv > ww) ? 1 : 0;
}
+static PyObject*
+type_richcompare(PyObject *v, PyObject *w, int op)
+{
+ PyObject *result;
+ Py_uintptr_t vv, ww;
+ int c;
+
+ /* Make sure both arguments are types. */
+ if (!PyType_Check(v) || !PyType_Check(w)) {
+ result = Py_NotImplemented;
+ goto out;
+ }
+
+ /* Py3K warning if comparison isn't == or != */
+ if (Py_Py3kWarningFlag && op != Py_EQ && op != Py_NE &&
+ PyErr_Warn(PyExc_DeprecationWarning,
+ "type inequality comparisons not supported in 3.x.") < 0) {
+ return NULL;
+ }
+
+ /* Compare addresses */
+ vv = (Py_uintptr_t)v;
+ ww = (Py_uintptr_t)w;
+ switch (op) {
+ case Py_LT: c = vv < ww; break;
+ case Py_LE: c = vv <= ww; break;
+ case Py_EQ: c = vv == ww; break;
+ case Py_NE: c = vv != ww; break;
+ case Py_GT: c = vv > ww; break;
+ case Py_GE: c = vv >= ww; break;
+ default:
+ result = Py_NotImplemented;
+ goto out;
+ }
+ result = c ? Py_True : Py_False;
+
+ /* incref and return */
+ out:
+ Py_INCREF(result);
+ return result;
+}
+
static PyObject *
type_repr(PyTypeObject *type)
{
@@ -2666,7 +2708,7 @@ PyTypeObject PyType_Type = {
type_doc, /* tp_doc */
(traverseproc)type_traverse, /* tp_traverse */
(inquiry)type_clear, /* tp_clear */
- 0, /* tp_richcompare */
+ type_richcompare, /* tp_richcompare */
offsetof(PyTypeObject, tp_weaklist), /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */