[Patches] indexerror: better error messages

Peter Schneider-Kamp peter@schneider-kamp.de
Sun, 11 Jun 2000 13:41:57 +0200


Moshe Zadka wrote:
> 
> Did you check it? You're taking a formatting hit for every IndexError!

No, I did not. Do you have some suggestions what kind of code I should
profile for comparison?

> That doesn't seem this negligible. Why not simply have IndexError
> with enough members for formatting, and do the formatting (including
> the intellegient about checking if the size is 0) inside IndexError's
> __str__?

That's how I started. I passed a tuple containing the offending index
and the length of the structure to IndexError__init__ and reused it in
IndexError__str__. Then I realized that for the error message to contain
the type of the sequence I have to pass down that one, too. For the
"intelligent" size == 0 checking I additionally needed the type
of the operation (I prefer "cannot get item from empty list" to
"cannot perform operation on empty sequence"). Passing all of this
seemed not much better than the one formatting and the if-else
construct.

Another possibility I see is to pass down an error number to encode
the operation and the sequence type. But I hesitate to add long rows of:
#define indexerror_list_getitem 0
#define indexerror_list_setitem 1
...
#define indexerror_array_getitem 0
It would also take away some flexibility. If my self-written Python
class wants to use this "intelligent" error processing I cannot give
a correct error number.

Passing down the object itself (to get type and size) does not help,
too, because that would mean a lot of
if (PyList_Check(obj)) ...
else if (PyString_Check(obj)) ...
The size members etc. are inconsistently named.

I see 3 sensible possibilities at the moment (please point out further
ones I missed, maybe there are some obvious ones):
* keep the old error messages
* do some of the processing at the error location
* just pass down the size and index and be happy with
  "sequence index 7 out of range [0..3]" and
  ("cannot perform operation on empty sequence" or
   "sequence index 0 out of range [0..-1]")

while 1:
  if feedback: break
--
Peter Schneider-Kamp          ++47-7388-7331
Herman Krags veg 51-11        mailto:peter@schneider-kamp.de
N-7050 Trondheim              http://schneider-kamp.de