Sequence-length - Missing the obvious ?
Aahz Maruch
aahz at netcom.com
Fri Jun 2 19:27:35 EDT 2000
In article <39385BAC.771293BE at stud.ntnu.no>,
Peter Schneider-Kamp <peter at schneider-kamp.de> wrote:
>Aahz Maruch wrote:
>>
>> 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
Yes and no. For reference, here's some code I posted earlier that is
precisely equivalent to a for loop:
i = 0
while 1:
try:
object.__getitem__(i)
except IndexError:
break
i = i + 1
Now, in Python terms (not C), what would you say happens to the
exception at the break?
--
--- Aahz (Copyright 2000 by aahz at netcom.com)
Androgynous poly kinky vanilla queer het <*> http://www.rahul.net/aahz/
Hugs and backrubs -- I break Rule 6
Have you coined a word today? --Aahz
More information about the Python-list
mailing list