]> granicus.if.org Git - python/commitdiff
PyObject_Compare can raise an exception now.
authorGuido van Rossum <guido@python.org>
Fri, 23 May 1997 00:06:51 +0000 (00:06 +0000)
committerGuido van Rossum <guido@python.org>
Fri, 23 May 1997 00:06:51 +0000 (00:06 +0000)
Modules/arraymodule.c
Objects/classobject.c
Objects/listobject.c
Objects/object.c
Python/bltinmodule.c
Python/ceval.c
Python/compile.c

index 190600efa73fc8ae8693f6928d2e607021dbd6b6..97af9ac5b62b91da64e0370ccba21df736fbbba2 100644 (file)
@@ -484,10 +484,8 @@ array_compare(v, w)
                        cmp = -1;
                Py_XDECREF(ai);
                Py_XDECREF(bi);
-               if (cmp != 0) {
-                       PyErr_Clear(); /* XXX Can't report errors here */
+               if (cmp != 0)
                        return cmp;
-               }
        }
        return v->ob_size - w->ob_size;
 }
@@ -823,6 +821,7 @@ array_index(self, args)
        for (i = 0; i < self->ob_size; i++) {
                if (PyObject_Compare(self->ob_item[i], args) == 0)
                        return PyInt_FromLong((long)i);
+               /* XXX PyErr_Occurred */
        }
        PyErr_SetString(PyExc_ValueError, "array.index(x): x not in array");
        return NULL;
@@ -845,6 +844,7 @@ array_count(self, args)
        for (i = 0; i < self->ob_size; i++) {
                if (PyObject_Compare(self->ob_item[i], args) == 0)
                        count++;
+               /* XXX PyErr_Occurred */
        }
        return PyInt_FromLong((long)count);
 }
@@ -870,7 +870,7 @@ array_remove(self, args)
                        Py_INCREF(Py_None);
                        return Py_None;
                }
-                       
+               /* XXX PyErr_Occurred */
        }
        PyErr_SetString(PyExc_ValueError, "array.remove(x): x not in array");
        return NULL;
index 4ff34beb131e9049f270e198f95f9420d2d20de5..749a06a0eb238fa4ab8d2dc8281c40749c193853 100644 (file)
@@ -349,7 +349,6 @@ instance_dealloc(inst)
                        PyObject *f, *t, *v, *tb;
                        PyErr_Fetch(&t, &v, &tb);
                        f = PySys_GetObject("stderr");
-                       PyErr_Clear();
                        if (f != NULL) {
                                PyFile_WriteString("Exception ", f);
                                if (t) {
@@ -565,9 +564,13 @@ instance_compare(inst, other)
        PyObject *result;
        long outcome;
        result = instance_compare1(inst, other);
-       if (result == NULL || !PyInt_Check(result)) {
-               PyErr_Clear();
-               return (inst < other) ? -1 : 1;
+       if (result == NULL)
+               return -1;
+       if (!PyInt_Check(result)) {
+               Py_DECREF(result);
+               PyErr_SetString(PyExc_TypeError,
+                               "comparison did not return an int");
+               return -1;
        }
        outcome = PyInt_AsLong(result);
        Py_DECREF(result);
@@ -899,6 +902,11 @@ PyInstance_DoBinOp(v, w, opname, ropname, thisfunc)
                return result;
        if (halfbinop(w, v, ropname, &result, thisfunc, 1) <= 0)
                return result;
+       /* Sigh -- special case for comnparisons */
+       if (strcmp(opname, "__cmp__") == 0) {
+               long c = (v < w) ? -1 : (v > w) ? 1 : 0;
+               return PyInt_FromLong(c);
+       }
        sprintf(buf, "%s nor %s defined for these operands", opname, ropname);
        PyErr_SetString(PyExc_TypeError, buf);
        return NULL;
index 16d63b80b879a10218a7c0cc5103ce934e01dbc1..588053c7411e5e39d07939fe6a58d781abb389c6 100644 (file)
@@ -580,8 +580,12 @@ docompare(x, y, compare)
        PyObject *args, *res;
        int i;
 
-       if (compare == NULL)
-               return PyObject_Compare(x, y);
+       if (compare == NULL) {
+               i = PyObject_Compare(x, y);
+               if (i && PyErr_Occurred())
+                       i = CMPERROR;
+               return i;
+       }
 
        args = Py_BuildValue("(OO)", x, y);
        if (args == NULL)
@@ -955,6 +959,8 @@ listindex(self, args)
        for (i = 0; i < self->ob_size; i++) {
                if (PyObject_Compare(self->ob_item[i], args) == 0)
                        return PyInt_FromLong((long)i);
+               if (PyErr_Occurred())
+                       return NULL;
        }
        PyErr_SetString(PyExc_ValueError, "list.index(x): x not in list");
        return NULL;
@@ -975,6 +981,8 @@ listcount(self, args)
        for (i = 0; i < self->ob_size; i++) {
                if (PyObject_Compare(self->ob_item[i], args) == 0)
                        count++;
+               if (PyErr_Occurred())
+                       return NULL;
        }
        return PyInt_FromLong((long)count);
 }
@@ -998,7 +1006,8 @@ listremove(self, args)
                        Py_INCREF(Py_None);
                        return Py_None;
                }
-                       
+               if (PyErr_Occurred())
+                       return NULL;
        }
        PyErr_SetString(PyExc_ValueError, "list.remove(x): x not in list");
        return NULL;
index 29269d052e34251ddaa76011228e65d92411113a..023477f4d12fe9d05e7c4e66d8a7582290f2c7ff 100644 (file)
@@ -255,13 +255,17 @@ static PyObject *
 do_cmp(v, w)
        PyObject *v, *w;
 {
+       long c;
        /* __rcmp__ actually won't be called unless __cmp__ isn't defined,
           because the check in cmpobject() reverses the objects first.
           This is intentional -- it makes no sense to define cmp(x,y)
           different than -cmp(y,x). */
        if (PyInstance_Check(v) || PyInstance_Check(w))
                return PyInstance_DoBinOp(v, w, "__cmp__", "__rcmp__", do_cmp);
-       return PyInt_FromLong((long)PyObject_Compare(v, w));
+       c = PyObject_Compare(v, w);
+       if (c && PyErr_Occurred())
+               return NULL;
+       return PyInt_FromLong(c);
 }
 
 int
@@ -269,25 +273,25 @@ PyObject_Compare(v, w)
        PyObject *v, *w;
 {
        PyTypeObject *tp;
+       if (v == NULL || w == NULL) {
+               PyErr_BadInternalCall();
+               return -1;
+       }
        if (v == w)
                return 0;
-       if (v == NULL)
-               return -1;
-       if (w == NULL)
-               return 1;
        if (PyInstance_Check(v) || PyInstance_Check(w)) {
                PyObject *res;
                int c;
                if (!PyInstance_Check(v))
                        return -PyObject_Compare(w, v);
                res = do_cmp(v, w);
-               if (res == NULL) {
-                       PyErr_Clear();
-                       return (v < w) ? -1 : 1;
-               }
+               if (res == NULL)
+                       return -1;
                if (!PyInt_Check(res)) {
                        Py_DECREF(res);
-                       return (v < w) ? -1 : 1;
+                       PyErr_SetString(PyExc_TypeError,
+                                       "comparison did not return an int");
+                       return -1;
                }
                c = PyInt_AsLong(res);
                Py_DECREF(res);
@@ -296,11 +300,8 @@ PyObject_Compare(v, w)
        if ((tp = v->ob_type) != w->ob_type) {
                if (tp->tp_as_number != NULL &&
                                w->ob_type->tp_as_number != NULL) {
-                       if (PyNumber_Coerce(&v, &w) != 0) {
-                               PyErr_Clear();
-                               /* XXX Should report the error,
-                                  XXX but the interface isn't there... */
-                       }
+                       if (PyNumber_Coerce(&v, &w) != 0)
+                               return -1;
                        else {
                                int cmp = (*v->ob_type->tp_compare)(v, w);
                                Py_DECREF(v);
index 4029e1b9c1e6bd232fa9e706e4b11b6f77576ccf..245d31ead9b62c4bfbc574f5219dbe39204a8a9c 100644 (file)
@@ -245,10 +245,14 @@ builtin_cmp(self, args)
        PyObject *args;
 {
        PyObject *a, *b;
+       long c;
 
        if (!PyArg_ParseTuple(args, "OO:cmp", &a, &b))
                return NULL;
-       return PyInt_FromLong((long)PyObject_Compare(a, b));
+       c = PyObject_Compare(a, b);
+       if (c && PyErr_Occurred())
+               return NULL;
+       return PyInt_FromLong(c);
 }
 
 static PyObject *
@@ -1073,7 +1077,13 @@ min_max(args, sign)
                if (w == NULL)
                        w = x;
                else {
-                       if (PyObject_Compare(x, w) * sign > 0) {
+                       int c = PyObject_Compare(x, w);
+                       if (c && PyErr_Occurred()) {
+                               Py_DECREF(x);
+                               Py_XDECREF(w);
+                               return NULL;
+                       }
+                       if (c * sign > 0) {
                                Py_DECREF(w);
                                w = x;
                        }
@@ -1360,8 +1370,8 @@ builtin_raw_input(self, args)
                        PyErr_SetString(PyExc_RuntimeError, "lost sys.stdout");
                        return NULL;
                }
-               Py_FlushLine();
-               if (PyFile_WriteObject(v, f, Py_PRINT_RAW) != 0)
+               if (Py_FlushLine() != 0 ||
+                   PyFile_WriteObject(v, f, Py_PRINT_RAW) != 0)
                        return NULL;
        }
        f = PySys_GetObject("stdin");
index 7897006841923354a903af04d0b7a7299bf33021..bd312b2f2fd1c68bf5df749832ea8bf1f171a49f 100644 (file)
@@ -456,6 +456,9 @@ eval_code2(co, globals, locals,
                                if (PyObject_Compare(keyword, nm) == 0)
                                        break;
                        }
+                       /* Check errors from Compare */
+                       if (PyErr_Occurred())
+                               goto fail;
                        if (j >= co->co_argcount) {
                                if (kwdict == NULL) {
                                        PyErr_Format(PyExc_TypeError,
@@ -2475,6 +2478,8 @@ cmp_member(v, w)
                Py_XDECREF(x);
                if (cmp == 0)
                        return 1;
+               if (PyErr_Occurred())
+                       return -1;
        }
        return 0;
 }
@@ -2507,6 +2512,8 @@ cmp_outcome(op, v, w)
                break;
        default:
                cmp = PyObject_Compare(v, w);
+               if (cmp && PyErr_Occurred())
+                       return NULL;
                switch (op) {
                case LT: res = cmp <  0; break;
                case LE: res = cmp <= 0; break;
index 155142aa8bddf173769f2599fd0136b6410fba64..36304e38e7392b9d96c69a81f6cbe16b76b673b5 100644 (file)
@@ -666,7 +666,8 @@ com_add(c, list, v)
                if (v->ob_type == w->ob_type && PyObject_Compare(v, w) == 0)
                        return i;
        }
-       if (PyList_Append(list, v) != 0)
+       /* Check for error from PyObject_Compare */
+       if (PyErr_Occurred() || PyList_Append(list, v) != 0)
                c->c_errors++;
        return n;
 }