Quiz for Python run-time wizards

Guido.van.Rossum@cwi.nl
Tue, 07 Mar 1995 16:44:59 +0100

Here's a short piece of C code with a subtle bug in it.

======================================================================
#include "allobjects.h" /* Implies #include <stdio.h> */

void
subtle_bug(object *list) {
object *item;
item = getlistitem(list, 0);
setlistitem(list, 1, newintobject(0L));
printobject(item, stdout, 1); /*BUG!*/
}
======================================================================

This is more or less equivalent to the Python function

def subtle_bug(list):
item = list[0]
list[1] = 0
print item,

and indeed will work as expected if you test it on a simple case.

I've compiled it successfully so there is no obvious syntax error.
Assume it will only ever be called with list arguments that have at
least two elements.

However there is a situation where, even when it is called with a
valid argument, the reference to 'item' in the line labeled with
/*BUG!*/ is invalid. Can you figure out what it is?

Send your solutions by email to me <guido@cwi.nl> only -- don't spoil
it by posting to comp.lang.python or mailing to python-list@cwi.nl!

A token prize is available for a correct answer -- if I receive more
than one correct answer I'll use the Python random number generator to
select a winner.

Jim Roskind is excluded from participation.

--Guido van Rossum, CWI, Amsterdam <mailto:Guido.van.Rossum@cwi.nl>
<http://www.cwi.nl/cwi/people/Guido.van.Rossum.html>