"tuple index out of range" side effect of C extension??

Andrew MacIntyre andymac at bullseye.apana.org.au
Sat Sep 20 06:51:14 EDT 2003


[I replied to Tim privately; this response for the archives]
On Thu, 18 Sep 2003, Tim Peters wrote:

> [Andrew MacIntyre]
> > I'm seeing a bizarre situation where IndexErrors are being thrown with
> > "tuple index out of range" error strings.
> >
> > The scenario is something like:
> >
> > l = [(v1a, v1b), (v2a, v2b), ..., (vna, vnb)]
> >
> > for a, b in l:
> >     ...
> >
> >
> >
> > with the IndexError being thrown on the "for..." line after the last
> > element of l has been through the for loop.
> >
> > I've also seen the same exception thrown on first access to an
> > attribute of a simple class.
> >
> > The exception being thrown is not consistent,
>
> It's probably the case then that C code is setting an exception in a
> function F but neglecting to make F return its "there's an error" return
> value.  The most common way this happens is for an extension module to call
> a Python C API function and neglect to check *it* for an error return value.
> In general, every Python C API function call must be checked for an error
> return, and, when one occurs, the calling function must either return an
> error code itself, or explicitly clear the error.
>
> If an error return isn't made, ceval.c (the interpreter's main loop) doesn't
> know an exception is pending, and not all bytecodes check for a pending
> exception.  The exception doesn't get triggered then until the interpreter
> happens to execute a bytecode that does check for a pending exception.  The
> original C code that set the exception is long gone at that point, and the
> reported exception doesn't appear to make sense.

Tim's diagnosis was accurate - many thanks!

Regards,
Andrew.

--
Andrew I MacIntyre                     "These thoughts are mine alone..."
E-mail: andymac at bullseye.apana.org.au  (pref) | Snail: PO Box 370
        andymac at pcug.org.au             (alt) |        Belconnen  ACT  2616
Web:    http://www.andymac.org/               |        Australia





More information about the Python-list mailing list