mirror of
https://github.com/python/cpython.git
synced 2025-09-17 14:16:02 +00:00
Add py3k warnings for object, type, cell and dict comparisons. This should resolve issue2342 and partly resolve issue2373.
This commit is contained in:
parent
a8b09fd4c3
commit
ae42f33cdf
6 changed files with 166 additions and 3 deletions
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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 */
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue