Sequence-length - Missing the obvious ?

Peter Schneider-Kamp petersc at stud.ntnu.no
Fri Jun 2 21:13:16 EDT 2000


Aahz Maruch wrote:
> 
> >As far as I understand the for loop goes on until an IndexError is
> >raised. Then the error is cleared and the for loop ends.
> 
> Let's be technically precise here: an exception is raised and caught by
> the "for" handler; the for loop is then broken (precisely as if the
> break statement was executed).  Like any other caught exception, the
> exception is thrown away (not "error is cleared").

Okay, then we will talk source. From dist/src/Python/ceval.c of the
current CVS tree (haven't checked, but should be similar with 1.5.2):

static PyObject *
loop_subscript(v, w)
	PyObject *v, *w;
{
	PySequenceMethods *sq = v->ob_type->tp_as_sequence;
	int i;
	if (sq == NULL || sq->sq_item == NULL) {
		PyErr_SetString(PyExc_TypeError, "loop over non-sequence");
		return NULL;
	}
	i = PyInt_AsLong(w);
	v = (*sq->sq_item)(v, i);
	if (v)
            return v;
	if (PyErr_ExceptionMatches(PyExc_IndexError))
		PyErr_Clear();
	return NULL;
}

Note the line PyErr_Clear() which is a call into
dist/src/Python/errors.c
where it is defined as PyErr_Restore(NULL,NULL,NULL) which just puts
NULL
values into the type, value and traceback fields. In my opinion this
(besides the name) qualifies as "error is cleared".

am-I-missing-something-obvious-ly y'rs
Peter
--
Peter Schneider-Kamp          ++47-7388-7331
Herman Krags veg 51-11        mailto:peter at schneider-kamp.de
N-7050 Trondheim              http://schneider-kamp.de




More information about the Python-list mailing list