Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 1 | import unittest |
Armin Ronacher | aa9a79d | 2012-10-06 14:03:24 +0200 | [diff] [blame] | 2 | import operator |
Michael W. Hudson | 0edc7a0 | 2005-07-12 10:21:19 +0000 | [diff] [blame] | 3 | import sys |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 4 | import pickle |
Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 5 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 6 | from test import support |
Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 7 | |
Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 8 | class G: |
| 9 | 'Sequence using __getitem__' |
| 10 | def __init__(self, seqn): |
| 11 | self.seqn = seqn |
| 12 | def __getitem__(self, i): |
| 13 | return self.seqn[i] |
| 14 | |
| 15 | class I: |
| 16 | 'Sequence using iterator protocol' |
| 17 | def __init__(self, seqn): |
| 18 | self.seqn = seqn |
| 19 | self.i = 0 |
| 20 | def __iter__(self): |
| 21 | return self |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 22 | def __next__(self): |
Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 23 | if self.i >= len(self.seqn): raise StopIteration |
| 24 | v = self.seqn[self.i] |
| 25 | self.i += 1 |
| 26 | return v |
| 27 | |
| 28 | class Ig: |
| 29 | 'Sequence using iterator protocol defined with a generator' |
| 30 | def __init__(self, seqn): |
| 31 | self.seqn = seqn |
| 32 | self.i = 0 |
| 33 | def __iter__(self): |
| 34 | for val in self.seqn: |
| 35 | yield val |
| 36 | |
| 37 | class X: |
| 38 | 'Missing __getitem__ and __iter__' |
| 39 | def __init__(self, seqn): |
| 40 | self.seqn = seqn |
| 41 | self.i = 0 |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 42 | def __next__(self): |
Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 43 | if self.i >= len(self.seqn): raise StopIteration |
| 44 | v = self.seqn[self.i] |
| 45 | self.i += 1 |
| 46 | return v |
| 47 | |
| 48 | class E: |
| 49 | 'Test propagation of exceptions' |
| 50 | def __init__(self, seqn): |
| 51 | self.seqn = seqn |
| 52 | self.i = 0 |
| 53 | def __iter__(self): |
| 54 | return self |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 55 | def __next__(self): |
Raymond Hettinger | ffdb8bb | 2004-09-27 15:29:05 +0000 | [diff] [blame] | 56 | 3 // 0 |
Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 57 | |
| 58 | class N: |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 59 | 'Iterator missing __next__()' |
Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 60 | def __init__(self, seqn): |
| 61 | self.seqn = seqn |
| 62 | self.i = 0 |
| 63 | def __iter__(self): |
| 64 | return self |
| 65 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 66 | class PickleTest: |
| 67 | # Helper to check picklability |
| 68 | def check_pickle(self, itorg, seq): |
Serhiy Storchaka | bad1257 | 2014-12-15 14:03:42 +0200 | [diff] [blame] | 69 | for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
| 70 | d = pickle.dumps(itorg, proto) |
| 71 | it = pickle.loads(d) |
| 72 | self.assertEqual(type(itorg), type(it)) |
| 73 | self.assertEqual(list(it), seq) |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 74 | |
Serhiy Storchaka | bad1257 | 2014-12-15 14:03:42 +0200 | [diff] [blame] | 75 | it = pickle.loads(d) |
| 76 | try: |
| 77 | next(it) |
| 78 | except StopIteration: |
| 79 | self.assertFalse(seq[1:]) |
| 80 | continue |
| 81 | d = pickle.dumps(it, proto) |
| 82 | it = pickle.loads(d) |
| 83 | self.assertEqual(list(it), seq[1:]) |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 84 | |
| 85 | class EnumerateTestCase(unittest.TestCase, PickleTest): |
Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 86 | |
| 87 | enum = enumerate |
Raymond Hettinger | e8b0f04 | 2003-05-28 14:05:34 +0000 | [diff] [blame] | 88 | seq, res = 'abc', [(0,'a'), (1,'b'), (2,'c')] |
Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 89 | |
| 90 | def test_basicfunction(self): |
Raymond Hettinger | e8b0f04 | 2003-05-28 14:05:34 +0000 | [diff] [blame] | 91 | self.assertEqual(type(self.enum(self.seq)), self.enum) |
| 92 | e = self.enum(self.seq) |
Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 93 | self.assertEqual(iter(e), e) |
Raymond Hettinger | e8b0f04 | 2003-05-28 14:05:34 +0000 | [diff] [blame] | 94 | self.assertEqual(list(self.enum(self.seq)), self.res) |
Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 95 | self.enum.__doc__ |
| 96 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 97 | def test_pickle(self): |
| 98 | self.check_pickle(self.enum(self.seq), self.res) |
| 99 | |
Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 100 | def test_getitemseqn(self): |
Raymond Hettinger | e8b0f04 | 2003-05-28 14:05:34 +0000 | [diff] [blame] | 101 | self.assertEqual(list(self.enum(G(self.seq))), self.res) |
Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 102 | e = self.enum(G('')) |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 103 | self.assertRaises(StopIteration, next, e) |
Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 104 | |
| 105 | def test_iteratorseqn(self): |
Raymond Hettinger | e8b0f04 | 2003-05-28 14:05:34 +0000 | [diff] [blame] | 106 | self.assertEqual(list(self.enum(I(self.seq))), self.res) |
Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 107 | e = self.enum(I('')) |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 108 | self.assertRaises(StopIteration, next, e) |
Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 109 | |
| 110 | def test_iteratorgenerator(self): |
Raymond Hettinger | e8b0f04 | 2003-05-28 14:05:34 +0000 | [diff] [blame] | 111 | self.assertEqual(list(self.enum(Ig(self.seq))), self.res) |
Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 112 | e = self.enum(Ig('')) |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 113 | self.assertRaises(StopIteration, next, e) |
Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 114 | |
| 115 | def test_noniterable(self): |
Raymond Hettinger | e8b0f04 | 2003-05-28 14:05:34 +0000 | [diff] [blame] | 116 | self.assertRaises(TypeError, self.enum, X(self.seq)) |
Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 117 | |
| 118 | def test_illformediterable(self): |
Thomas Wouters | 1034dad | 2006-04-15 09:16:16 +0000 | [diff] [blame] | 119 | self.assertRaises(TypeError, self.enum, N(self.seq)) |
Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 120 | |
| 121 | def test_exception_propagation(self): |
Raymond Hettinger | e8b0f04 | 2003-05-28 14:05:34 +0000 | [diff] [blame] | 122 | self.assertRaises(ZeroDivisionError, list, self.enum(E(self.seq))) |
| 123 | |
| 124 | def test_argumentcheck(self): |
| 125 | self.assertRaises(TypeError, self.enum) # no arguments |
| 126 | self.assertRaises(TypeError, self.enum, 1) # wrong type (not iterable) |
Alexandre Vassalotti | e9f305f | 2008-05-16 04:39:54 +0000 | [diff] [blame] | 127 | self.assertRaises(TypeError, self.enum, 'abc', 'a') # wrong type |
| 128 | self.assertRaises(TypeError, self.enum, 'abc', 2, 3) # too many arguments |
Raymond Hettinger | e8b0f04 | 2003-05-28 14:05:34 +0000 | [diff] [blame] | 129 | |
Benjamin Peterson | 4fd283a | 2010-06-25 23:24:35 +0000 | [diff] [blame] | 130 | @support.cpython_only |
Raymond Hettinger | e8b0f04 | 2003-05-28 14:05:34 +0000 | [diff] [blame] | 131 | def test_tuple_reuse(self): |
| 132 | # Tests an implementation detail where tuple is reused |
| 133 | # whenever nothing else holds a reference to it |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 134 | self.assertEqual(len(set(map(id, list(enumerate(self.seq))))), len(self.seq)) |
| 135 | self.assertEqual(len(set(map(id, enumerate(self.seq)))), min(1,len(self.seq))) |
Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 136 | |
| 137 | class MyEnum(enumerate): |
| 138 | pass |
| 139 | |
| 140 | class SubclassTestCase(EnumerateTestCase): |
| 141 | |
| 142 | enum = MyEnum |
| 143 | |
Raymond Hettinger | e8b0f04 | 2003-05-28 14:05:34 +0000 | [diff] [blame] | 144 | class TestEmpty(EnumerateTestCase): |
| 145 | |
| 146 | seq, res = '', [] |
| 147 | |
| 148 | class TestBig(EnumerateTestCase): |
| 149 | |
| 150 | seq = range(10,20000,2) |
Guido van Rossum | 801f0d7 | 2006-08-24 19:48:10 +0000 | [diff] [blame] | 151 | res = list(zip(range(20000), seq)) |
Raymond Hettinger | e8b0f04 | 2003-05-28 14:05:34 +0000 | [diff] [blame] | 152 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 153 | class TestReversed(unittest.TestCase, PickleTest): |
Raymond Hettinger | 85c20a4 | 2003-11-06 14:06:48 +0000 | [diff] [blame] | 154 | |
| 155 | def test_simple(self): |
| 156 | class A: |
| 157 | def __getitem__(self, i): |
| 158 | if i < 5: |
| 159 | return str(i) |
| 160 | raise StopIteration |
| 161 | def __len__(self): |
| 162 | return 5 |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 163 | for data in 'abc', range(5), tuple(enumerate('abc')), A(), range(1,17,5): |
Raymond Hettinger | 85c20a4 | 2003-11-06 14:06:48 +0000 | [diff] [blame] | 164 | self.assertEqual(list(data)[::-1], list(reversed(data))) |
| 165 | self.assertRaises(TypeError, reversed, {}) |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 166 | # don't allow keyword arguments |
| 167 | self.assertRaises(TypeError, reversed, [], a=1) |
Raymond Hettinger | 85c20a4 | 2003-11-06 14:06:48 +0000 | [diff] [blame] | 168 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 169 | def test_range_optimization(self): |
| 170 | x = range(1) |
Raymond Hettinger | 85c20a4 | 2003-11-06 14:06:48 +0000 | [diff] [blame] | 171 | self.assertEqual(type(reversed(x)), type(iter(x))) |
Raymond Hettinger | e8b0f04 | 2003-05-28 14:05:34 +0000 | [diff] [blame] | 172 | |
Raymond Hettinger | 029dba5 | 2004-02-10 09:33:39 +0000 | [diff] [blame] | 173 | def test_len(self): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 174 | for s in ('hello', tuple('hello'), list('hello'), range(5)): |
Armin Ronacher | aa9a79d | 2012-10-06 14:03:24 +0200 | [diff] [blame] | 175 | self.assertEqual(operator.length_hint(reversed(s)), len(s)) |
Raymond Hettinger | ff5dc0e | 2004-09-29 11:40:50 +0000 | [diff] [blame] | 176 | r = reversed(s) |
| 177 | list(r) |
Armin Ronacher | aa9a79d | 2012-10-06 14:03:24 +0200 | [diff] [blame] | 178 | self.assertEqual(operator.length_hint(r), 0) |
Raymond Hettinger | ff5dc0e | 2004-09-29 11:40:50 +0000 | [diff] [blame] | 179 | class SeqWithWeirdLen: |
| 180 | called = False |
| 181 | def __len__(self): |
| 182 | if not self.called: |
| 183 | self.called = True |
| 184 | return 10 |
| 185 | raise ZeroDivisionError |
| 186 | def __getitem__(self, index): |
| 187 | return index |
| 188 | r = reversed(SeqWithWeirdLen()) |
Armin Ronacher | aa9a79d | 2012-10-06 14:03:24 +0200 | [diff] [blame] | 189 | self.assertRaises(ZeroDivisionError, operator.length_hint, r) |
Raymond Hettinger | ff5dc0e | 2004-09-29 11:40:50 +0000 | [diff] [blame] | 190 | |
| 191 | |
| 192 | def test_gc(self): |
| 193 | class Seq: |
| 194 | def __len__(self): |
| 195 | return 10 |
| 196 | def __getitem__(self, index): |
| 197 | return index |
| 198 | s = Seq() |
| 199 | r = reversed(s) |
| 200 | s.r = r |
| 201 | |
| 202 | def test_args(self): |
| 203 | self.assertRaises(TypeError, reversed) |
| 204 | self.assertRaises(TypeError, reversed, [], 'extra') |
Raymond Hettinger | 029dba5 | 2004-02-10 09:33:39 +0000 | [diff] [blame] | 205 | |
Serhiy Storchaka | 4376763 | 2013-11-03 21:31:38 +0200 | [diff] [blame] | 206 | @unittest.skipUnless(hasattr(sys, 'getrefcount'), 'test needs sys.getrefcount()') |
Michael W. Hudson | 0edc7a0 | 2005-07-12 10:21:19 +0000 | [diff] [blame] | 207 | def test_bug1229429(self): |
| 208 | # this bug was never in reversed, it was in |
| 209 | # PyObject_CallMethod, and reversed_new calls that sometimes. |
Michael W. Hudson | 0edc7a0 | 2005-07-12 10:21:19 +0000 | [diff] [blame] | 210 | def f(): |
| 211 | pass |
| 212 | r = f.__reversed__ = object() |
| 213 | rc = sys.getrefcount(r) |
| 214 | for i in range(10): |
| 215 | try: |
| 216 | reversed(f) |
| 217 | except TypeError: |
| 218 | pass |
| 219 | else: |
| 220 | self.fail("non-callable __reversed__ didn't raise!") |
| 221 | self.assertEqual(rc, sys.getrefcount(r)) |
Tim Peters | f5f32b4 | 2005-07-17 23:16:17 +0000 | [diff] [blame] | 222 | |
Mark Dickinson | fb3dc94 | 2010-05-25 19:06:24 +0000 | [diff] [blame] | 223 | def test_objmethods(self): |
| 224 | # Objects must have __len__() and __getitem__() implemented. |
| 225 | class NoLen(object): |
Guido van Rossum | 97c1adf | 2016-08-18 09:22:23 -0700 | [diff] [blame] | 226 | def __getitem__(self, i): return 1 |
Mark Dickinson | fb3dc94 | 2010-05-25 19:06:24 +0000 | [diff] [blame] | 227 | nl = NoLen() |
| 228 | self.assertRaises(TypeError, reversed, nl) |
| 229 | |
| 230 | class NoGetItem(object): |
| 231 | def __len__(self): return 2 |
| 232 | ngi = NoGetItem() |
| 233 | self.assertRaises(TypeError, reversed, ngi) |
| 234 | |
Guido van Rossum | 97c1adf | 2016-08-18 09:22:23 -0700 | [diff] [blame] | 235 | class Blocked(object): |
| 236 | def __getitem__(self, i): return 1 |
| 237 | def __len__(self): return 2 |
| 238 | __reversed__ = None |
| 239 | b = Blocked() |
| 240 | self.assertRaises(TypeError, reversed, b) |
| 241 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 242 | def test_pickle(self): |
| 243 | for data in 'abc', range(5), tuple(enumerate('abc')), range(1,17,5): |
| 244 | self.check_pickle(reversed(data), list(data)[::-1]) |
| 245 | |
Michael W. Hudson | 0edc7a0 | 2005-07-12 10:21:19 +0000 | [diff] [blame] | 246 | |
Benjamin Peterson | 06507eb | 2010-05-08 16:51:16 +0000 | [diff] [blame] | 247 | class EnumerateStartTestCase(EnumerateTestCase): |
Alexandre Vassalotti | e9f305f | 2008-05-16 04:39:54 +0000 | [diff] [blame] | 248 | |
Benjamin Peterson | 06507eb | 2010-05-08 16:51:16 +0000 | [diff] [blame] | 249 | def test_basicfunction(self): |
| 250 | e = self.enum(self.seq) |
| 251 | self.assertEqual(iter(e), e) |
| 252 | self.assertEqual(list(self.enum(self.seq)), self.res) |
Alexandre Vassalotti | e9f305f | 2008-05-16 04:39:54 +0000 | [diff] [blame] | 253 | |
| 254 | |
Benjamin Peterson | 06507eb | 2010-05-08 16:51:16 +0000 | [diff] [blame] | 255 | class TestStart(EnumerateStartTestCase): |
Alexandre Vassalotti | e9f305f | 2008-05-16 04:39:54 +0000 | [diff] [blame] | 256 | |
Benjamin Peterson | 06507eb | 2010-05-08 16:51:16 +0000 | [diff] [blame] | 257 | enum = lambda self, i: enumerate(i, start=11) |
| 258 | seq, res = 'abc', [(11, 'a'), (12, 'b'), (13, 'c')] |
| 259 | |
| 260 | |
| 261 | class TestLongStart(EnumerateStartTestCase): |
| 262 | |
| 263 | enum = lambda self, i: enumerate(i, start=sys.maxsize+1) |
Alexandre Vassalotti | e9f305f | 2008-05-16 04:39:54 +0000 | [diff] [blame] | 264 | seq, res = 'abc', [(sys.maxsize+1,'a'), (sys.maxsize+2,'b'), |
| 265 | (sys.maxsize+3,'c')] |
| 266 | |
| 267 | |
Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 268 | if __name__ == "__main__": |
Zachary Ware | 38c707e | 2015-04-13 15:00:43 -0500 | [diff] [blame] | 269 | unittest.main() |