__getslice__ incorrectness

David Ascher da at ski.org
Wed Dec 1 19:05:48 EST 1999


On Thu, 2 Dec 1999, Juergen A. Erhard wrote:

[Complains that __getslice__ with missing indices is broken]

> Will this be fixed (it's a lot better the way it's documented than the
> way it's implemented ;-)

The best way to get it fixed is to submit a patch =).

IMO, you're right, it's not doing the right thing. The behavior you're
seeing comes from Objects/abstract.c's PySequence_GetSlice, which does:

		if (i1 < 0 || i2 < 0) {
			if (m->sq_length) {
				int l = (*m->sq_length)(s);
				if (l < 0)
					return NULL;
				if (i1 < 0)
					i1 += l;
				if (i2 < 0)
					i2 += l;
			}
		}

(where i1 and i2 are the two indices, which are -1 if unspecified)

This allows the getslice behavior to do the right thing for sequences of
all kinds as long as 'the right thing' is what Python builtin sequences do
(-1 means ones from the end of the length, etc.), without having to burden
the implementation of lists, tuples, and strings with this special-case.

I would suggest that the if() block should be special-classed, and not
operate if the object m is an instance.

In other words, something like:

<		if (i1 < 0 || i2 < 0) {
>               if (!PyInstance_Check(s) && (i1 < 0 || i2 > 0)) {

(untested)

As to whether this would break existing code, who knows...

--david

See http://www.python.org/1.5/patch.html for patch submission guidelines.







More information about the Python-list mailing list