Rich comparisons fall-out:
- Use PyObject_RichCompare*() where possible: when comparing keyword arguments, in _PyEval_SliceIndex(), and of course in cmp_outcome(). Unrelated stuff: - Removed all trailing whitespace. - Folded some long lines.
This commit is contained in:
parent
f916e7aa62
commit
ac7be6888b
|
@ -493,8 +493,12 @@ eval_code2(PyCodeObject *co, PyObject *globals, PyObject *locals,
|
||||||
for (j = 0; j < co->co_argcount; j++) {
|
for (j = 0; j < co->co_argcount; j++) {
|
||||||
PyObject *nm = PyTuple_GET_ITEM(
|
PyObject *nm = PyTuple_GET_ITEM(
|
||||||
co->co_varnames, j);
|
co->co_varnames, j);
|
||||||
if (PyObject_Compare(keyword, nm) == 0)
|
int cmp = PyObject_RichCompareBool(
|
||||||
|
keyword, nm, Py_EQ);
|
||||||
|
if (cmp > 0)
|
||||||
break;
|
break;
|
||||||
|
else if (cmp < 0)
|
||||||
|
goto fail;
|
||||||
}
|
}
|
||||||
/* Check errors from Compare */
|
/* Check errors from Compare */
|
||||||
if (PyErr_Occurred())
|
if (PyErr_Occurred())
|
||||||
|
@ -2128,7 +2132,8 @@ eval_code2(PyCodeObject *co, PyObject *globals, PyObject *locals,
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
set_exc_info(PyThreadState *tstate, PyObject *type, PyObject *value, PyObject *tb)
|
set_exc_info(PyThreadState *tstate,
|
||||||
|
PyObject *type, PyObject *value, PyObject *tb)
|
||||||
{
|
{
|
||||||
PyFrameObject *frame;
|
PyFrameObject *frame;
|
||||||
PyObject *tmp_type, *tmp_value, *tmp_tb;
|
PyObject *tmp_type, *tmp_value, *tmp_tb;
|
||||||
|
@ -3002,13 +3007,18 @@ _PyEval_SliceIndex(PyObject *v, int *pi)
|
||||||
x = PyLong_AsLong(v);
|
x = PyLong_AsLong(v);
|
||||||
if (x==-1 && PyErr_Occurred()) {
|
if (x==-1 && PyErr_Occurred()) {
|
||||||
PyObject *long_zero;
|
PyObject *long_zero;
|
||||||
|
int cmp;
|
||||||
|
|
||||||
if (!PyErr_ExceptionMatches( PyExc_OverflowError ) ) {
|
if (!PyErr_ExceptionMatches(
|
||||||
|
PyExc_OverflowError)) {
|
||||||
/* It's not an overflow error, so just
|
/* It's not an overflow error, so just
|
||||||
signal an error */
|
signal an error */
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Clear the OverflowError */
|
||||||
|
PyErr_Clear();
|
||||||
|
|
||||||
/* It's an overflow error, so we need to
|
/* It's an overflow error, so we need to
|
||||||
check the sign of the long integer,
|
check the sign of the long integer,
|
||||||
set the value to INT_MAX or 0, and clear
|
set the value to INT_MAX or 0, and clear
|
||||||
|
@ -3019,15 +3029,15 @@ _PyEval_SliceIndex(PyObject *v, int *pi)
|
||||||
if (long_zero == NULL) return 0;
|
if (long_zero == NULL) return 0;
|
||||||
|
|
||||||
/* Check sign */
|
/* Check sign */
|
||||||
if (PyObject_Compare(long_zero, v) < 0)
|
cmp = PyObject_RichCompareBool(v, long_zero,
|
||||||
|
Py_GT);
|
||||||
|
Py_DECREF(long_zero);
|
||||||
|
if (cmp < 0)
|
||||||
|
return 0;
|
||||||
|
else if (cmp > 0)
|
||||||
x = INT_MAX;
|
x = INT_MAX;
|
||||||
else
|
else
|
||||||
x = 0;
|
x = 0;
|
||||||
|
|
||||||
/* Free the long integer we created, and clear the
|
|
||||||
OverflowError */
|
|
||||||
Py_DECREF(long_zero);
|
|
||||||
PyErr_Clear();
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
PyErr_SetString(PyExc_TypeError,
|
PyErr_SetString(PyExc_TypeError,
|
||||||
|
@ -3056,7 +3066,8 @@ apply_slice(PyObject *u, PyObject *v, PyObject *w) /* return u[v:w] */
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
assign_slice(PyObject *u, PyObject *v, PyObject *w, PyObject *x) /* u[v:w] = x */
|
assign_slice(PyObject *u, PyObject *v, PyObject *w, PyObject *x)
|
||||||
|
/* u[v:w] = x */
|
||||||
{
|
{
|
||||||
int ilow = 0, ihigh = INT_MAX;
|
int ilow = 0, ihigh = INT_MAX;
|
||||||
if (!_PyEval_SliceIndex(v, &ilow))
|
if (!_PyEval_SliceIndex(v, &ilow))
|
||||||
|
@ -3072,8 +3083,7 @@ assign_slice(PyObject *u, PyObject *v, PyObject *w, PyObject *x) /* u[v:w] = x *
|
||||||
static PyObject *
|
static PyObject *
|
||||||
cmp_outcome(int op, register PyObject *v, register PyObject *w)
|
cmp_outcome(int op, register PyObject *v, register PyObject *w)
|
||||||
{
|
{
|
||||||
register int cmp;
|
int res = 0;
|
||||||
register int res = 0;
|
|
||||||
switch (op) {
|
switch (op) {
|
||||||
case IS:
|
case IS:
|
||||||
case IS_NOT:
|
case IS_NOT:
|
||||||
|
@ -3093,18 +3103,7 @@ cmp_outcome(int op, register PyObject *v, register PyObject *w)
|
||||||
res = PyErr_GivenExceptionMatches(v, w);
|
res = PyErr_GivenExceptionMatches(v, w);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
cmp = PyObject_Compare(v, w);
|
return PyObject_RichCompare(v, w, op);
|
||||||
if (cmp && PyErr_Occurred())
|
|
||||||
return NULL;
|
|
||||||
switch (op) {
|
|
||||||
case LT: res = cmp < 0; break;
|
|
||||||
case LE: res = cmp <= 0; break;
|
|
||||||
case EQ: res = cmp == 0; break;
|
|
||||||
case NE: res = cmp != 0; break;
|
|
||||||
case GT: res = cmp > 0; break;
|
|
||||||
case GE: res = cmp >= 0; break;
|
|
||||||
/* XXX no default? (res is initialized to 0 though) */
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
v = res ? Py_True : Py_False;
|
v = res ? Py_True : Py_False;
|
||||||
Py_INCREF(v);
|
Py_INCREF(v);
|
||||||
|
|
Loading…
Reference in New Issue