Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_slice.py @ 87690:66c1c9f32567 default tip
Add use_weakref option to functools.lru_cache
The use_weakref option makes lru_cache create weak references to the function
arguments when creating the cache key, which prevents them from staying in the
memory forever even after all other references to them are gone.
This is a proof of concept for http://bugs.python.org/issue19859
| author | Radomir Dopieralski <devel@sheep.art.pl> |
|---|---|
| date | Tue, 03 Dec 2013 16:02:48 +0100 |
| parents | 9214f8440c44 |
| children |
| rev | line source |
|---|---|
|
24319
c43466f7dae8
A few days ago, Guido said (in the thread "[Python-Dev] Python
Michael W. Hudson <mwh@python.net>
parents:
diff
changeset
|
1 # tests for slice objects; in particular the indices method. |
|
c43466f7dae8
A few days ago, Guido said (in the thread "[Python-Dev] Python
Michael W. Hudson <mwh@python.net>
parents:
diff
changeset
|
2 |
|
30277
66ba0e0ec202
SF patch #736962: Port tests to unittest (Part 2)
Raymond Hettinger <python@rcn.com>
parents:
26035
diff
changeset
|
3 import unittest |
|
47201
9c930a32cebb
#2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents:
44160
diff
changeset
|
4 from test import support |
|
42322
3e33215c66ed
Getting rid of cPickle. Mmm, feels good!
Guido van Rossum <guido@python.org>
parents:
41642
diff
changeset
|
5 from pickle import loads, dumps |
|
30277
66ba0e0ec202
SF patch #736962: Port tests to unittest (Part 2)
Raymond Hettinger <python@rcn.com>
parents:
26035
diff
changeset
|
6 |
|
80338
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
7 import itertools |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
8 import operator |
|
26035
6ed2e23aae45
Some days, I think my comment of
Michael W. Hudson <mwh@python.net>
parents:
24674
diff
changeset
|
9 import sys |
|
24319
c43466f7dae8
A few days ago, Guido said (in the thread "[Python-Dev] Python
Michael W. Hudson <mwh@python.net>
parents:
diff
changeset
|
10 |
|
80338
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
11 |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
12 def evaluate_slice_index(arg): |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
13 """ |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
14 Helper function to convert a slice argument to an integer, and raise |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
15 TypeError with a suitable message on failure. |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
16 |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
17 """ |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
18 if hasattr(arg, '__index__'): |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
19 return operator.index(arg) |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
20 else: |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
21 raise TypeError( |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
22 "slice indices must be integers or " |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
23 "None or have an __index__ method") |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
24 |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
25 def slice_indices(slice, length): |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
26 """ |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
27 Reference implementation for the slice.indices method. |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
28 |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
29 """ |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
30 # Compute step and length as integers. |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
31 length = operator.index(length) |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
32 step = 1 if slice.step is None else evaluate_slice_index(slice.step) |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
33 |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
34 # Raise ValueError for negative length or zero step. |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
35 if length < 0: |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
36 raise ValueError("length should not be negative") |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
37 if step == 0: |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
38 raise ValueError("slice step cannot be zero") |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
39 |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
40 # Find lower and upper bounds for start and stop. |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
41 lower = -1 if step < 0 else 0 |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
42 upper = length - 1 if step < 0 else length |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
43 |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
44 # Compute start. |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
45 if slice.start is None: |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
46 start = upper if step < 0 else lower |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
47 else: |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
48 start = evaluate_slice_index(slice.start) |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
49 start = max(start + length, lower) if start < 0 else min(start, upper) |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
50 |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
51 # Compute stop. |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
52 if slice.stop is None: |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
53 stop = lower if step < 0 else upper |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
54 else: |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
55 stop = evaluate_slice_index(slice.stop) |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
56 stop = max(stop + length, lower) if stop < 0 else min(stop, upper) |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
57 |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
58 return start, stop, step |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
59 |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
60 |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
61 # Class providing an __index__ method. Used for testing slice.indices. |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
62 |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
63 class MyIndexable(object): |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
64 def __init__(self, value): |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
65 self.value = value |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
66 |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
67 def __index__(self): |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
68 return self.value |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
69 |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
70 |
|
30277
66ba0e0ec202
SF patch #736962: Port tests to unittest (Part 2)
Raymond Hettinger <python@rcn.com>
parents:
26035
diff
changeset
|
71 class SliceTest(unittest.TestCase): |
|
66ba0e0ec202
SF patch #736962: Port tests to unittest (Part 2)
Raymond Hettinger <python@rcn.com>
parents:
26035
diff
changeset
|
72 |
|
66ba0e0ec202
SF patch #736962: Port tests to unittest (Part 2)
Raymond Hettinger <python@rcn.com>
parents:
26035
diff
changeset
|
73 def test_constructor(self): |
|
66ba0e0ec202
SF patch #736962: Port tests to unittest (Part 2)
Raymond Hettinger <python@rcn.com>
parents:
26035
diff
changeset
|
74 self.assertRaises(TypeError, slice) |
|
66ba0e0ec202
SF patch #736962: Port tests to unittest (Part 2)
Raymond Hettinger <python@rcn.com>
parents:
26035
diff
changeset
|
75 self.assertRaises(TypeError, slice, 1, 2, 3, 4) |
|
66ba0e0ec202
SF patch #736962: Port tests to unittest (Part 2)
Raymond Hettinger <python@rcn.com>
parents:
26035
diff
changeset
|
76 |
|
66ba0e0ec202
SF patch #736962: Port tests to unittest (Part 2)
Raymond Hettinger <python@rcn.com>
parents:
26035
diff
changeset
|
77 def test_repr(self): |
|
66ba0e0ec202
SF patch #736962: Port tests to unittest (Part 2)
Raymond Hettinger <python@rcn.com>
parents:
26035
diff
changeset
|
78 self.assertEqual(repr(slice(1, 2, 3)), "slice(1, 2, 3)") |
|
66ba0e0ec202
SF patch #736962: Port tests to unittest (Part 2)
Raymond Hettinger <python@rcn.com>
parents:
26035
diff
changeset
|
79 |
|
30332
6b84802c9524
SF bug #800796: Difference between hash() and __hash__()
Raymond Hettinger <python@rcn.com>
parents:
30277
diff
changeset
|
80 def test_hash(self): |
|
6b84802c9524
SF bug #800796: Difference between hash() and __hash__()
Raymond Hettinger <python@rcn.com>
parents:
30277
diff
changeset
|
81 # Verify clearing of SF bug #800796 |
|
6b84802c9524
SF bug #800796: Difference between hash() and __hash__()
Raymond Hettinger <python@rcn.com>
parents:
30277
diff
changeset
|
82 self.assertRaises(TypeError, hash, slice(5)) |
|
6b84802c9524
SF bug #800796: Difference between hash() and __hash__()
Raymond Hettinger <python@rcn.com>
parents:
30277
diff
changeset
|
83 self.assertRaises(TypeError, slice(5).__hash__) |
|
6b84802c9524
SF bug #800796: Difference between hash() and __hash__()
Raymond Hettinger <python@rcn.com>
parents:
30277
diff
changeset
|
84 |
|
30277
66ba0e0ec202
SF patch #736962: Port tests to unittest (Part 2)
Raymond Hettinger <python@rcn.com>
parents:
26035
diff
changeset
|
85 def test_cmp(self): |
|
66ba0e0ec202
SF patch #736962: Port tests to unittest (Part 2)
Raymond Hettinger <python@rcn.com>
parents:
26035
diff
changeset
|
86 s1 = slice(1, 2, 3) |
|
66ba0e0ec202
SF patch #736962: Port tests to unittest (Part 2)
Raymond Hettinger <python@rcn.com>
parents:
26035
diff
changeset
|
87 s2 = slice(1, 2, 3) |
|
66ba0e0ec202
SF patch #736962: Port tests to unittest (Part 2)
Raymond Hettinger <python@rcn.com>
parents:
26035
diff
changeset
|
88 s3 = slice(1, 2, 4) |
|
66ba0e0ec202
SF patch #736962: Port tests to unittest (Part 2)
Raymond Hettinger <python@rcn.com>
parents:
26035
diff
changeset
|
89 self.assertEqual(s1, s2) |
|
66ba0e0ec202
SF patch #736962: Port tests to unittest (Part 2)
Raymond Hettinger <python@rcn.com>
parents:
26035
diff
changeset
|
90 self.assertNotEqual(s1, s3) |
|
42955
30511068d3d4
Fix buglet in sliceobjects, they were not returning Py_NotImplemented when
Thomas Wouters <thomas@python.org>
parents:
42322
diff
changeset
|
91 self.assertNotEqual(s1, None) |
|
30511068d3d4
Fix buglet in sliceobjects, they were not returning Py_NotImplemented when
Thomas Wouters <thomas@python.org>
parents:
42322
diff
changeset
|
92 self.assertNotEqual(s1, (1, 2, 3)) |
|
30511068d3d4
Fix buglet in sliceobjects, they were not returning Py_NotImplemented when
Thomas Wouters <thomas@python.org>
parents:
42322
diff
changeset
|
93 self.assertNotEqual(s1, "") |
|
30277
66ba0e0ec202
SF patch #736962: Port tests to unittest (Part 2)
Raymond Hettinger <python@rcn.com>
parents:
26035
diff
changeset
|
94 |
|
66ba0e0ec202
SF patch #736962: Port tests to unittest (Part 2)
Raymond Hettinger <python@rcn.com>
parents:
26035
diff
changeset
|
95 class Exc(Exception): |
|
66ba0e0ec202
SF patch #736962: Port tests to unittest (Part 2)
Raymond Hettinger <python@rcn.com>
parents:
26035
diff
changeset
|
96 pass |
|
66ba0e0ec202
SF patch #736962: Port tests to unittest (Part 2)
Raymond Hettinger <python@rcn.com>
parents:
26035
diff
changeset
|
97 |
|
66ba0e0ec202
SF patch #736962: Port tests to unittest (Part 2)
Raymond Hettinger <python@rcn.com>
parents:
26035
diff
changeset
|
98 class BadCmp(object): |
|
66ba0e0ec202
SF patch #736962: Port tests to unittest (Part 2)
Raymond Hettinger <python@rcn.com>
parents:
26035
diff
changeset
|
99 def __eq__(self, other): |
|
66ba0e0ec202
SF patch #736962: Port tests to unittest (Part 2)
Raymond Hettinger <python@rcn.com>
parents:
26035
diff
changeset
|
100 raise Exc |
|
66ba0e0ec202
SF patch #736962: Port tests to unittest (Part 2)
Raymond Hettinger <python@rcn.com>
parents:
26035
diff
changeset
|
101 |
|
66ba0e0ec202
SF patch #736962: Port tests to unittest (Part 2)
Raymond Hettinger <python@rcn.com>
parents:
26035
diff
changeset
|
102 s1 = slice(BadCmp()) |
|
66ba0e0ec202
SF patch #736962: Port tests to unittest (Part 2)
Raymond Hettinger <python@rcn.com>
parents:
26035
diff
changeset
|
103 s2 = slice(BadCmp()) |
|
66ba0e0ec202
SF patch #736962: Port tests to unittest (Part 2)
Raymond Hettinger <python@rcn.com>
parents:
26035
diff
changeset
|
104 self.assertEqual(s1, s1) |
|
39644
13d5e5f0e993
Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents:
30332
diff
changeset
|
105 self.assertRaises(Exc, lambda: s1 == s2) |
|
30277
66ba0e0ec202
SF patch #736962: Port tests to unittest (Part 2)
Raymond Hettinger <python@rcn.com>
parents:
26035
diff
changeset
|
106 |
|
66ba0e0ec202
SF patch #736962: Port tests to unittest (Part 2)
Raymond Hettinger <python@rcn.com>
parents:
26035
diff
changeset
|
107 s1 = slice(1, BadCmp()) |
|
66ba0e0ec202
SF patch #736962: Port tests to unittest (Part 2)
Raymond Hettinger <python@rcn.com>
parents:
26035
diff
changeset
|
108 s2 = slice(1, BadCmp()) |
|
66ba0e0ec202
SF patch #736962: Port tests to unittest (Part 2)
Raymond Hettinger <python@rcn.com>
parents:
26035
diff
changeset
|
109 self.assertEqual(s1, s1) |
|
39644
13d5e5f0e993
Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents:
30332
diff
changeset
|
110 self.assertRaises(Exc, lambda: s1 == s2) |
|
30277
66ba0e0ec202
SF patch #736962: Port tests to unittest (Part 2)
Raymond Hettinger <python@rcn.com>
parents:
26035
diff
changeset
|
111 |
|
66ba0e0ec202
SF patch #736962: Port tests to unittest (Part 2)
Raymond Hettinger <python@rcn.com>
parents:
26035
diff
changeset
|
112 s1 = slice(1, 2, BadCmp()) |
|
66ba0e0ec202
SF patch #736962: Port tests to unittest (Part 2)
Raymond Hettinger <python@rcn.com>
parents:
26035
diff
changeset
|
113 s2 = slice(1, 2, BadCmp()) |
|
66ba0e0ec202
SF patch #736962: Port tests to unittest (Part 2)
Raymond Hettinger <python@rcn.com>
parents:
26035
diff
changeset
|
114 self.assertEqual(s1, s1) |
|
39644
13d5e5f0e993
Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents:
30332
diff
changeset
|
115 self.assertRaises(Exc, lambda: s1 == s2) |
|
30277
66ba0e0ec202
SF patch #736962: Port tests to unittest (Part 2)
Raymond Hettinger <python@rcn.com>
parents:
26035
diff
changeset
|
116 |
|
66ba0e0ec202
SF patch #736962: Port tests to unittest (Part 2)
Raymond Hettinger <python@rcn.com>
parents:
26035
diff
changeset
|
117 def test_members(self): |
|
66ba0e0ec202
SF patch #736962: Port tests to unittest (Part 2)
Raymond Hettinger <python@rcn.com>
parents:
26035
diff
changeset
|
118 s = slice(1) |
|
66ba0e0ec202
SF patch #736962: Port tests to unittest (Part 2)
Raymond Hettinger <python@rcn.com>
parents:
26035
diff
changeset
|
119 self.assertEqual(s.start, None) |
|
66ba0e0ec202
SF patch #736962: Port tests to unittest (Part 2)
Raymond Hettinger <python@rcn.com>
parents:
26035
diff
changeset
|
120 self.assertEqual(s.stop, 1) |
|
66ba0e0ec202
SF patch #736962: Port tests to unittest (Part 2)
Raymond Hettinger <python@rcn.com>
parents:
26035
diff
changeset
|
121 self.assertEqual(s.step, None) |
|
26035
6ed2e23aae45
Some days, I think my comment of
Michael W. Hudson <mwh@python.net>
parents:
24674
diff
changeset
|
122 |
|
30277
66ba0e0ec202
SF patch #736962: Port tests to unittest (Part 2)
Raymond Hettinger <python@rcn.com>
parents:
26035
diff
changeset
|
123 s = slice(1, 2) |
|
66ba0e0ec202
SF patch #736962: Port tests to unittest (Part 2)
Raymond Hettinger <python@rcn.com>
parents:
26035
diff
changeset
|
124 self.assertEqual(s.start, 1) |
|
66ba0e0ec202
SF patch #736962: Port tests to unittest (Part 2)
Raymond Hettinger <python@rcn.com>
parents:
26035
diff
changeset
|
125 self.assertEqual(s.stop, 2) |
|
66ba0e0ec202
SF patch #736962: Port tests to unittest (Part 2)
Raymond Hettinger <python@rcn.com>
parents:
26035
diff
changeset
|
126 self.assertEqual(s.step, None) |
|
66ba0e0ec202
SF patch #736962: Port tests to unittest (Part 2)
Raymond Hettinger <python@rcn.com>
parents:
26035
diff
changeset
|
127 |
|
66ba0e0ec202
SF patch #736962: Port tests to unittest (Part 2)
Raymond Hettinger <python@rcn.com>
parents:
26035
diff
changeset
|
128 s = slice(1, 2, 3) |
|
66ba0e0ec202
SF patch #736962: Port tests to unittest (Part 2)
Raymond Hettinger <python@rcn.com>
parents:
26035
diff
changeset
|
129 self.assertEqual(s.start, 1) |
|
66ba0e0ec202
SF patch #736962: Port tests to unittest (Part 2)
Raymond Hettinger <python@rcn.com>
parents:
26035
diff
changeset
|
130 self.assertEqual(s.stop, 2) |
|
66ba0e0ec202
SF patch #736962: Port tests to unittest (Part 2)
Raymond Hettinger <python@rcn.com>
parents:
26035
diff
changeset
|
131 self.assertEqual(s.step, 3) |
|
66ba0e0ec202
SF patch #736962: Port tests to unittest (Part 2)
Raymond Hettinger <python@rcn.com>
parents:
26035
diff
changeset
|
132 |
|
66ba0e0ec202
SF patch #736962: Port tests to unittest (Part 2)
Raymond Hettinger <python@rcn.com>
parents:
26035
diff
changeset
|
133 class AnyClass: |
|
66ba0e0ec202
SF patch #736962: Port tests to unittest (Part 2)
Raymond Hettinger <python@rcn.com>
parents:
26035
diff
changeset
|
134 pass |
|
66ba0e0ec202
SF patch #736962: Port tests to unittest (Part 2)
Raymond Hettinger <python@rcn.com>
parents:
26035
diff
changeset
|
135 |
|
66ba0e0ec202
SF patch #736962: Port tests to unittest (Part 2)
Raymond Hettinger <python@rcn.com>
parents:
26035
diff
changeset
|
136 obj = AnyClass() |
|
66ba0e0ec202
SF patch #736962: Port tests to unittest (Part 2)
Raymond Hettinger <python@rcn.com>
parents:
26035
diff
changeset
|
137 s = slice(obj) |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
48407
diff
changeset
|
138 self.assertTrue(s.stop is obj) |
|
30277
66ba0e0ec202
SF patch #736962: Port tests to unittest (Part 2)
Raymond Hettinger <python@rcn.com>
parents:
26035
diff
changeset
|
139 |
|
80338
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
140 def check_indices(self, slice, length): |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
141 try: |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
142 actual = slice.indices(length) |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
143 except ValueError: |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
144 actual = "valueerror" |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
145 try: |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
146 expected = slice_indices(slice, length) |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
147 except ValueError: |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
148 expected = "valueerror" |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
149 self.assertEqual(actual, expected) |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
150 |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
151 if length >= 0 and slice.step != 0: |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
152 actual = range(*slice.indices(length)) |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
153 expected = range(length)[slice] |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
154 self.assertEqual(actual, expected) |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
155 |
|
30277
66ba0e0ec202
SF patch #736962: Port tests to unittest (Part 2)
Raymond Hettinger <python@rcn.com>
parents:
26035
diff
changeset
|
156 def test_indices(self): |
|
66ba0e0ec202
SF patch #736962: Port tests to unittest (Part 2)
Raymond Hettinger <python@rcn.com>
parents:
26035
diff
changeset
|
157 self.assertEqual(slice(None ).indices(10), (0, 10, 1)) |
|
66ba0e0ec202
SF patch #736962: Port tests to unittest (Part 2)
Raymond Hettinger <python@rcn.com>
parents:
26035
diff
changeset
|
158 self.assertEqual(slice(None, None, 2).indices(10), (0, 10, 2)) |
|
66ba0e0ec202
SF patch #736962: Port tests to unittest (Part 2)
Raymond Hettinger <python@rcn.com>
parents:
26035
diff
changeset
|
159 self.assertEqual(slice(1, None, 2).indices(10), (1, 10, 2)) |
|
66ba0e0ec202
SF patch #736962: Port tests to unittest (Part 2)
Raymond Hettinger <python@rcn.com>
parents:
26035
diff
changeset
|
160 self.assertEqual(slice(None, None, -1).indices(10), (9, -1, -1)) |
|
66ba0e0ec202
SF patch #736962: Port tests to unittest (Part 2)
Raymond Hettinger <python@rcn.com>
parents:
26035
diff
changeset
|
161 self.assertEqual(slice(None, None, -2).indices(10), (9, -1, -2)) |
|
66ba0e0ec202
SF patch #736962: Port tests to unittest (Part 2)
Raymond Hettinger <python@rcn.com>
parents:
26035
diff
changeset
|
162 self.assertEqual(slice(3, None, -2).indices(10), (3, -1, -2)) |
|
48407
e22deaeb6c1b
Merged revisions 64365,64370,64406,64408-64409,64412,64416-64417,64420-64421,64425-64428 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
47201
diff
changeset
|
163 # issue 3004 tests |
|
e22deaeb6c1b
Merged revisions 64365,64370,64406,64408-64409,64412,64416-64417,64420-64421,64425-64428 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
47201
diff
changeset
|
164 self.assertEqual(slice(None, -9).indices(10), (0, 1, 1)) |
|
e22deaeb6c1b
Merged revisions 64365,64370,64406,64408-64409,64412,64416-64417,64420-64421,64425-64428 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
47201
diff
changeset
|
165 self.assertEqual(slice(None, -10).indices(10), (0, 0, 1)) |
|
e22deaeb6c1b
Merged revisions 64365,64370,64406,64408-64409,64412,64416-64417,64420-64421,64425-64428 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
47201
diff
changeset
|
166 self.assertEqual(slice(None, -11).indices(10), (0, 0, 1)) |
|
e22deaeb6c1b
Merged revisions 64365,64370,64406,64408-64409,64412,64416-64417,64420-64421,64425-64428 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
47201
diff
changeset
|
167 self.assertEqual(slice(None, -10, -1).indices(10), (9, 0, -1)) |
|
e22deaeb6c1b
Merged revisions 64365,64370,64406,64408-64409,64412,64416-64417,64420-64421,64425-64428 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
47201
diff
changeset
|
168 self.assertEqual(slice(None, -11, -1).indices(10), (9, -1, -1)) |
|
e22deaeb6c1b
Merged revisions 64365,64370,64406,64408-64409,64412,64416-64417,64420-64421,64425-64428 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
47201
diff
changeset
|
169 self.assertEqual(slice(None, -12, -1).indices(10), (9, -1, -1)) |
|
e22deaeb6c1b
Merged revisions 64365,64370,64406,64408-64409,64412,64416-64417,64420-64421,64425-64428 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
47201
diff
changeset
|
170 self.assertEqual(slice(None, 9).indices(10), (0, 9, 1)) |
|
e22deaeb6c1b
Merged revisions 64365,64370,64406,64408-64409,64412,64416-64417,64420-64421,64425-64428 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
47201
diff
changeset
|
171 self.assertEqual(slice(None, 10).indices(10), (0, 10, 1)) |
|
e22deaeb6c1b
Merged revisions 64365,64370,64406,64408-64409,64412,64416-64417,64420-64421,64425-64428 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
47201
diff
changeset
|
172 self.assertEqual(slice(None, 11).indices(10), (0, 10, 1)) |
|
e22deaeb6c1b
Merged revisions 64365,64370,64406,64408-64409,64412,64416-64417,64420-64421,64425-64428 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
47201
diff
changeset
|
173 self.assertEqual(slice(None, 8, -1).indices(10), (9, 8, -1)) |
|
e22deaeb6c1b
Merged revisions 64365,64370,64406,64408-64409,64412,64416-64417,64420-64421,64425-64428 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
47201
diff
changeset
|
174 self.assertEqual(slice(None, 9, -1).indices(10), (9, 9, -1)) |
|
e22deaeb6c1b
Merged revisions 64365,64370,64406,64408-64409,64412,64416-64417,64420-64421,64425-64428 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
47201
diff
changeset
|
175 self.assertEqual(slice(None, 10, -1).indices(10), (9, 9, -1)) |
|
e22deaeb6c1b
Merged revisions 64365,64370,64406,64408-64409,64412,64416-64417,64420-64421,64425-64428 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
47201
diff
changeset
|
176 |
|
30277
66ba0e0ec202
SF patch #736962: Port tests to unittest (Part 2)
Raymond Hettinger <python@rcn.com>
parents:
26035
diff
changeset
|
177 self.assertEqual( |
|
66ba0e0ec202
SF patch #736962: Port tests to unittest (Part 2)
Raymond Hettinger <python@rcn.com>
parents:
26035
diff
changeset
|
178 slice(-100, 100 ).indices(10), |
|
66ba0e0ec202
SF patch #736962: Port tests to unittest (Part 2)
Raymond Hettinger <python@rcn.com>
parents:
26035
diff
changeset
|
179 slice(None).indices(10) |
|
66ba0e0ec202
SF patch #736962: Port tests to unittest (Part 2)
Raymond Hettinger <python@rcn.com>
parents:
26035
diff
changeset
|
180 ) |
|
66ba0e0ec202
SF patch #736962: Port tests to unittest (Part 2)
Raymond Hettinger <python@rcn.com>
parents:
26035
diff
changeset
|
181 self.assertEqual( |
|
66ba0e0ec202
SF patch #736962: Port tests to unittest (Part 2)
Raymond Hettinger <python@rcn.com>
parents:
26035
diff
changeset
|
182 slice(100, -100, -1).indices(10), |
|
66ba0e0ec202
SF patch #736962: Port tests to unittest (Part 2)
Raymond Hettinger <python@rcn.com>
parents:
26035
diff
changeset
|
183 slice(None, None, -1).indices(10) |
|
66ba0e0ec202
SF patch #736962: Port tests to unittest (Part 2)
Raymond Hettinger <python@rcn.com>
parents:
26035
diff
changeset
|
184 ) |
|
40644
1c26505c1de6
Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
185 self.assertEqual(slice(-100, 100, 2).indices(10), (0, 10, 2)) |
|
30277
66ba0e0ec202
SF patch #736962: Port tests to unittest (Part 2)
Raymond Hettinger <python@rcn.com>
parents:
26035
diff
changeset
|
186 |
|
44160
9d152530910a
Removed PyInt_GetMax and sys.maxint
Christian Heimes <christian@cheimes.de>
parents:
43100
diff
changeset
|
187 self.assertEqual(list(range(10))[::sys.maxsize - 1], [0]) |
|
30277
66ba0e0ec202
SF patch #736962: Port tests to unittest (Part 2)
Raymond Hettinger <python@rcn.com>
parents:
26035
diff
changeset
|
188 |
|
80338
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
189 # Check a variety of start, stop, step and length values, including |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
190 # values exceeding sys.maxsize (see issue #14794). |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
191 vals = [None, -2**100, -2**30, -53, -7, -1, 0, 1, 7, 53, 2**30, 2**100] |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
192 lengths = [0, 1, 7, 53, 2**30, 2**100] |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
193 for slice_args in itertools.product(vals, repeat=3): |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
194 s = slice(*slice_args) |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
195 for length in lengths: |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
196 self.check_indices(s, length) |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
197 self.check_indices(slice(0, 10, 1), -3) |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
198 |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
199 # Negative length should raise ValueError |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
200 with self.assertRaises(ValueError): |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
201 slice(None).indices(-1) |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
202 |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
203 # Zero step should raise ValueError |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
204 with self.assertRaises(ValueError): |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
205 slice(0, 10, 0).indices(5) |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
206 |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
207 # Using a start, stop or step or length that can't be interpreted as an |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
208 # integer should give a TypeError ... |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
209 with self.assertRaises(TypeError): |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
210 slice(0.0, 10, 1).indices(5) |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
211 with self.assertRaises(TypeError): |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
212 slice(0, 10.0, 1).indices(5) |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
213 with self.assertRaises(TypeError): |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
214 slice(0, 10, 1.0).indices(5) |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
215 with self.assertRaises(TypeError): |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
216 slice(0, 10, 1).indices(5.0) |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
217 |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
218 # ... but it should be fine to use a custom class that provides index. |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
219 self.assertEqual(slice(0, 10, 1).indices(5), (0, 5, 1)) |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
220 self.assertEqual(slice(MyIndexable(0), 10, 1).indices(5), (0, 5, 1)) |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
221 self.assertEqual(slice(0, MyIndexable(10), 1).indices(5), (0, 5, 1)) |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
222 self.assertEqual(slice(0, 10, MyIndexable(1)).indices(5), (0, 5, 1)) |
|
9214f8440c44
Issue #14794: slice.indices no longer returns OverflowError for out-of-range start, stop, step or length.
Mark Dickinson <mdickinson@enthought.com>
parents:
66310
diff
changeset
|
223 self.assertEqual(slice(0, 10, 1).indices(MyIndexable(5)), (0, 5, 1)) |
|
30277
66ba0e0ec202
SF patch #736962: Port tests to unittest (Part 2)
Raymond Hettinger <python@rcn.com>
parents:
26035
diff
changeset
|
224 |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40644
diff
changeset
|
225 def test_setslice_without_getslice(self): |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40644
diff
changeset
|
226 tmp = [] |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40644
diff
changeset
|
227 class X(object): |
|
43100
4e6b4ba60c3b
Remove the simple slicing API. All slicing is now done with slice objects.
Thomas Wouters <thomas@python.org>
parents:
42955
diff
changeset
|
228 def __setitem__(self, i, k): |
|
4e6b4ba60c3b
Remove the simple slicing API. All slicing is now done with slice objects.
Thomas Wouters <thomas@python.org>
parents:
42955
diff
changeset
|
229 tmp.append((i, k)) |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40644
diff
changeset
|
230 |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40644
diff
changeset
|
231 x = X() |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40644
diff
changeset
|
232 x[1:2] = 42 |
|
66310
9fe76c26ea9f
#9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
55593
diff
changeset
|
233 self.assertEqual(tmp, [(slice(1, 2), 42)]) |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40644
diff
changeset
|
234 |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40644
diff
changeset
|
235 def test_pickle(self): |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40644
diff
changeset
|
236 s = slice(10, 20, 3) |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40644
diff
changeset
|
237 for protocol in (0,1,2): |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40644
diff
changeset
|
238 t = loads(dumps(s, protocol)) |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40644
diff
changeset
|
239 self.assertEqual(s, t) |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40644
diff
changeset
|
240 self.assertEqual(s.indices(15), t.indices(15)) |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40644
diff
changeset
|
241 self.assertNotEqual(id(s), id(t)) |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40644
diff
changeset
|
242 |
|
30277
66ba0e0ec202
SF patch #736962: Port tests to unittest (Part 2)
Raymond Hettinger <python@rcn.com>
parents:
26035
diff
changeset
|
243 def test_main(): |
|
47201
9c930a32cebb
#2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents:
44160
diff
changeset
|
244 support.run_unittest(SliceTest) |
|
30277
66ba0e0ec202
SF patch #736962: Port tests to unittest (Part 2)
Raymond Hettinger <python@rcn.com>
parents:
26035
diff
changeset
|
245 |
|
66ba0e0ec202
SF patch #736962: Port tests to unittest (Part 2)
Raymond Hettinger <python@rcn.com>
parents:
26035
diff
changeset
|
246 if __name__ == "__main__": |
|
66ba0e0ec202
SF patch #736962: Port tests to unittest (Part 2)
Raymond Hettinger <python@rcn.com>
parents:
26035
diff
changeset
|
247 test_main() |
