diff options
author | Guido van Rossum <guido@python.org> | 2002-06-13 19:17:46 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 2002-06-13 19:17:46 (GMT) |
commit | 09638c16d842c77c7b9f7c0f339508c0b2a40feb (patch) | |
tree | 54c714ee3edc4cadab4d765392ddc5f7e20fe622 /Lib/test/test_descr.py | |
parent | fea59e7f766b950ccd55f9eee87d4032a768fdcc (diff) | |
download | cpython-09638c16d842c77c7b9f7c0f339508c0b2a40feb.zip cpython-09638c16d842c77c7b9f7c0f339508c0b2a40feb.tar.gz cpython-09638c16d842c77c7b9f7c0f339508c0b2a40feb.tar.bz2 |
Hopefully this addresses the remaining issues of SF bugs 459235 and
473985. Through a subtle rearrangement of some members in the etype
struct (!), mapping methods are now preferred over sequence methods,
which is necessary to support str.__getitem__("hello", slice(4)) etc.
Diffstat (limited to 'Lib/test/test_descr.py')
-rw-r--r-- | Lib/test/test_descr.py | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/Lib/test/test_descr.py b/Lib/test/test_descr.py index b83ace8..1168106 100644 --- a/Lib/test/test_descr.py +++ b/Lib/test/test_descr.py @@ -3099,6 +3099,51 @@ def copy_setstate(): vereq(b.foo, 24) vereq(b.getfoo(), 24) +def slices(): + if verbose: + print "Testing cases with slices and overridden __getitem__ ..." + # Strings + vereq("hello"[:4], "hell") + vereq("hello"[slice(4)], "hell") + vereq(str.__getitem__("hello", slice(4)), "hell") + class S(str): + def __getitem__(self, x): + return str.__getitem__(self, x) + vereq(S("hello")[:4], "hell") + vereq(S("hello")[slice(4)], "hell") + vereq(S("hello").__getitem__(slice(4)), "hell") + # Tuples + vereq((1,2,3)[:2], (1,2)) + vereq((1,2,3)[slice(2)], (1,2)) + vereq(tuple.__getitem__((1,2,3), slice(2)), (1,2)) + class T(tuple): + def __getitem__(self, x): + return tuple.__getitem__(self, x) + vereq(T((1,2,3))[:2], (1,2)) + vereq(T((1,2,3))[slice(2)], (1,2)) + vereq(T((1,2,3)).__getitem__(slice(2)), (1,2)) + # Lists + vereq([1,2,3][:2], [1,2]) + vereq([1,2,3][slice(2)], [1,2]) + vereq(list.__getitem__([1,2,3], slice(2)), [1,2]) + class L(list): + def __getitem__(self, x): + return list.__getitem__(self, x) + vereq(L([1,2,3])[:2], [1,2]) + vereq(L([1,2,3])[slice(2)], [1,2]) + vereq(L([1,2,3]).__getitem__(slice(2)), [1,2]) + # Now do lists and __setitem__ + a = L([1,2,3]) + a[slice(1, 3)] = [3,2] + vereq(a, [1,3,2]) + a[slice(0, 2, 1)] = [3,1] + vereq(a, [3,1,2]) + a.__setitem__(slice(1, 3), [2,1]) + vereq(a, [3,2,1]) + a.__setitem__(slice(0, 2, 1), [2,3]) + vereq(a, [2,3,1]) + + def do_this_first(): if verbose: print "Testing SF bug 551412 ..." @@ -3182,6 +3227,7 @@ def test_main(): docdescriptor() string_exceptions() copy_setstate() + slices() if verbose: print "All OK" if __name__ == "__main__": |