Formatting complex error message takes more time than creating a Python string from the C string.
In some cases index and size are not limited, they can be arbitrary large, and converting very large integer (e.g. 2**1000000000) to decimal string will take very long time.
Exactly. Like KeyError, where the missing key is passed as args, the proposal should be about making IndexError accept index and length as args with its constructor, which should cost very little. But unlike KeyError, IndexError shall not include index and length when formatted as a string for reasons that Serhiy pointed out. The user can then choose to format an IndexError with index and length included where it makes sense.
I see. So you mean to make IndexError more like KeyError, where the constructor takes a key and only formats the message string with key when __str__ is called. Currently IndexError’s constructor takes a preformatted error message, and we can make it append additional information when __str__ is called.
But that approach does not solve the problem that Serhiy pointed out, which is that it may be undesirable for IndexError.__str__ to include index in the formatted message by default because indexcan be too large to be converted into a string in a reasonable amount of time.
To mitigate the problem we can either leave it to the user to include index in the message, or make IndexError.__str__ conditionally include index only if it’s within a certain size limit (e.g. 64 bits).
Thanks for the link. I see that in the prior discussion the core devs believed that including the index that triggers an IndexError in the message is rarely useful to debugging, which I think is indeed usually true.
The problem with including index in the default output of IndexError.__str__ is that a generic error handler that outputs any error as a string would unconditionallly output an IndexError with an overly large index, potentially hanging the program for a long time.
Since a recent Python update we already have the limit to (default) 4300 (decimal) digits on int-to-str formatting. That is different from the old thread that was referenced above, so that argument may weigh lighter than before.
Still the limit can be disabled, but that’s a deliberate user decision.
Further discussion is pointless until we have data. We need to try and measure the time it takes to create an exception from a literal string and from a formatted message. If the difference is 1% – okay, this is a noise. If the difference is 100% – this is unacceptable for such common exception.
The exception may be common, but formatting it on the console to a user that cares about an extra few milliseconds is probably uncommon.
So I think you should also figure out how often the exception is printed, how much longer it takes to print out the text, and in which situations it really matters.
Printing a traceback on the console is already slow, printing few more characters will not make difference. In most cases where the performance is important the error message is ignored.