test_iter.py 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975
  1. import unittest
  2. from time import sleep
  3. # from test.test_support import run_unittest, TESTFN, unlink, have_unicode, \
  4. # check_py3k_warnings, cpython_only
  5. # Test result of triple loop (too big to inline)
  6. TRIPLETS = [(0, 0, 0), (0, 0, 1), (0, 0, 2),
  7. (0, 1, 0), (0, 1, 1), (0, 1, 2),
  8. (0, 2, 0), (0, 2, 1), (0, 2, 2),
  9. (1, 0, 0), (1, 0, 1), (1, 0, 2),
  10. (1, 1, 0), (1, 1, 1), (1, 1, 2),
  11. (1, 2, 0), (1, 2, 1), (1, 2, 2),
  12. (2, 0, 0), (2, 0, 1), (2, 0, 2),
  13. (2, 1, 0), (2, 1, 1), (2, 1, 2),
  14. (2, 2, 0), (2, 2, 1), (2, 2, 2)]
  15. # Helper classes
  16. class BasicIterClass:
  17. def __init__(self, n):
  18. self.n = n
  19. self.i = 0
  20. def next(self):
  21. res = self.i
  22. if res >= self.n:
  23. raise StopIteration
  24. self.i = res + 1
  25. return res
  26. class IteratingSequenceClass:
  27. def __init__(self, n):
  28. self.n = n
  29. def __iter__(self):
  30. return BasicIterClass(self.n)
  31. class SleepingIterClass(BasicIterClass):
  32. def __init__(self, n):
  33. BasicIterClass.__init__(self, n)
  34. def next(self):
  35. sleep(0.01)
  36. return BasicIterClass.next(self)
  37. class SleepingSequenceClass:
  38. def __init__(self, n):
  39. self.n = n
  40. def __iter__(self):
  41. return SleepingIterClass(self.n)
  42. class SequenceClass:
  43. def __init__(self, n):
  44. self.n = n
  45. def __getitem__(self, i):
  46. if 0 <= i < self.n:
  47. return i
  48. else:
  49. raise IndexError
  50. # Main test suite
  51. class TestCase(unittest.TestCase):
  52. # Helper to check that an iterator returns a given sequence
  53. def check_iterator(self, it, seq):
  54. res = []
  55. while 1:
  56. try:
  57. val = it.next()
  58. except StopIteration:
  59. break
  60. res.append(val)
  61. self.assertEqual(res, seq)
  62. # Helper to check that a for loop generates a given sequence
  63. def check_for_loop(self, expr, seq):
  64. res = []
  65. for val in expr:
  66. res.append(val)
  67. self.assertEqual(res, seq)
  68. # Test basic use of iter() function
  69. def test_iter_basic(self):
  70. self.check_iterator(iter(range(10)), range(10))
  71. # Test that iter(iter(x)) is the same as iter(x)
  72. def test_iter_idempotency(self):
  73. seq = range(10)
  74. it = iter(seq)
  75. it2 = iter(it)
  76. self.assertTrue(it is it2)
  77. # Test that for loops over iterators work
  78. def test_iter_for_loop(self):
  79. self.check_for_loop(iter(range(10)), range(10))
  80. # Test several independent iterators over the same list
  81. def test_iter_independence(self):
  82. seq = range(3)
  83. res = []
  84. for i in iter(seq):
  85. for j in iter(seq):
  86. for k in iter(seq):
  87. res.append((i, j, k))
  88. self.assertEqual(res, TRIPLETS)
  89. # Test triple list comprehension using iterators
  90. def test_nested_comprehensions_iter(self):
  91. seq = range(3)
  92. res = [(i, j, k)
  93. for i in iter(seq) for j in iter(seq) for k in iter(seq)]
  94. self.assertEqual(res, TRIPLETS)
  95. # Test triple list comprehension without iterators
  96. def test_nested_comprehensions_for(self):
  97. seq = range(3)
  98. res = [(i, j, k) for i in seq for j in seq for k in seq]
  99. self.assertEqual(res, TRIPLETS)
  100. # Test a class with __iter__ in a for loop
  101. def test_iter_class_for(self):
  102. self.check_for_loop(IteratingSequenceClass(10), range(10))
  103. # Test a class with a suspending iterator in a for loop
  104. def test_iter_sleeping_class_for(self):
  105. self.check_for_loop(SleepingSequenceClass(10), range(10))
  106. # Test a class with __iter__ with explicit iter()
  107. def test_iter_class_iter(self):
  108. self.check_iterator(iter(IteratingSequenceClass(10)), range(10))
  109. # Test for loop on a sequence class without __iter__
  110. def test_seq_class_for(self):
  111. self.check_for_loop(SequenceClass(10), range(10))
  112. # Test iter() on a sequence class without __iter__
  113. def test_seq_class_iter(self):
  114. self.check_iterator(iter(SequenceClass(10)), range(10))
  115. # Test a new_style class with __iter__ but no next() method
  116. # def test_new_style_iter_class(self):
  117. # class IterClass(object):
  118. # def __iter__(self):
  119. # return self
  120. # self.assertRaises(TypeError, iter, IterClass())
  121. # Test two-argument iter() with callable instance
  122. def test_iter_callable(self):
  123. class C:
  124. def __init__(self):
  125. self.i = 0
  126. def __call__(self):
  127. i = self.i
  128. self.i = i + 1
  129. if i > 100:
  130. raise IndexError # Emergency stop
  131. return i
  132. self.check_iterator(iter(C(), 10), range(10))
  133. # Test two-argument iter() with function
  134. def test_iter_function(self):
  135. def spam(state=[0]):
  136. i = state[0]
  137. state[0] = i+1
  138. return i
  139. self.check_iterator(iter(spam, 10), range(10))
  140. # Test two-argument iter() with function that raises StopIteration
  141. def test_iter_function_stop(self):
  142. def spam(state=[0]):
  143. i = state[0]
  144. if i == 10:
  145. raise StopIteration
  146. state[0] = i+1
  147. return i
  148. self.check_iterator(iter(spam, 20), range(10))
  149. # Test exception propagation through function iterator
  150. def test_exception_function(self):
  151. def spam(state=[0]):
  152. i = state[0]
  153. state[0] = i+1
  154. if i == 10:
  155. raise RuntimeError
  156. return i
  157. res = []
  158. try:
  159. for x in iter(spam, 20):
  160. res.append(x)
  161. except RuntimeError:
  162. self.assertEqual(res, range(10))
  163. else:
  164. self.fail("should have raised RuntimeError")
  165. # Test exception propagation through sequence iterator
  166. def test_exception_sequence(self):
  167. class MySequenceClass(SequenceClass):
  168. def __getitem__(self, i):
  169. if i == 10:
  170. raise RuntimeError
  171. return SequenceClass.__getitem__(self, i)
  172. res = []
  173. try:
  174. for x in MySequenceClass(20):
  175. res.append(x)
  176. except RuntimeError:
  177. self.assertEqual(res, range(10))
  178. else:
  179. self.fail("should have raised RuntimeError")
  180. # Test exception propagation through explicit iterator
  181. #def test_exception_iter(self):
  182. # class MyIterClass(object):
  183. # def __init__(self):
  184. # self.i = -1
  185. # SequenceClass.__init__(self)
  186. # def next(self):
  187. # self.i += 1
  188. # if self.i == 10:
  189. # raise RuntimeError
  190. # return self.i
  191. # class MySequenceClass(SequenceClass):
  192. # def __iter__(self):
  193. # return MyIterClass()
  194. # res = []
  195. # try:
  196. # for x in MySequenceClass(20):
  197. # res.append(x)
  198. # except RuntimeError:
  199. # self.assertEqual(res, range(10))
  200. # else:
  201. # self.fail("should have raised RuntimeError")
  202. # Test for StopIteration from __getitem__
  203. def test_stop_sequence(self):
  204. class MySequenceClass(SequenceClass):
  205. def __getitem__(self, i):
  206. if i == 10:
  207. raise StopIteration
  208. return SequenceClass.__getitem__(self, i)
  209. self.check_for_loop(MySequenceClass(20), range(10))
  210. # Test a big range
  211. def test_iter_big_range(self):
  212. self.check_for_loop(iter(range(10000)), range(10000))
  213. # Test an empty list
  214. def test_iter_empty(self):
  215. self.check_for_loop(iter([]), [])
  216. # Test a tuple
  217. def test_iter_tuple(self):
  218. self.check_for_loop(iter((0,1,2,3,4,5,6,7,8,9)), range(10))
  219. # Test an xrange
  220. def test_iter_xrange(self):
  221. self.check_for_loop(iter(xrange(10)), range(10))
  222. # Test a string
  223. def test_iter_string(self):
  224. self.check_for_loop(iter("abcde"), ["a", "b", "c", "d", "e"])
  225. # Test a Unicode string
  226. # if have_unicode:
  227. # def test_iter_unicode(self):
  228. # self.check_for_loop(iter(unicode("abcde")),
  229. # [unicode("a"), unicode("b"), unicode("c"),
  230. # unicode("d"), unicode("e")])
  231. # Test a directory
  232. def test_iter_dict(self):
  233. dict = {}
  234. for i in range(10):
  235. dict[i] = None
  236. self.check_for_loop(dict, dict.keys())
  237. # Test a file
  238. # def test_iter_file(self):
  239. # f = open(TESTFN, "w")
  240. # try:
  241. # for i in range(5):
  242. # f.write("%d\n" % i)
  243. # finally:
  244. # f.close()
  245. # f = open(TESTFN, "r")
  246. # try:
  247. # self.check_for_loop(f, ["0\n", "1\n", "2\n", "3\n", "4\n"])
  248. # self.check_for_loop(f, [])
  249. # finally:
  250. # f.close()
  251. # try:
  252. # unlink(TESTFN)
  253. # except OSError:
  254. # pass
  255. # Test list()'s use of iterators.
  256. def test_builtin_list(self):
  257. self.assertEqual(list(SequenceClass(5)), range(5))
  258. self.assertEqual(list(SequenceClass(0)), [])
  259. self.assertEqual(list(()), [])
  260. self.assertEqual(list(range(10, -1, -1)), range(10, -1, -1))
  261. d = {"one": 1, "two": 2, "three": 3}
  262. self.assertEqual(list(d), d.keys())
  263. self.assertRaises(TypeError, list, list)
  264. self.assertRaises(TypeError, list, 42)
  265. # f = open(TESTFN, "w")
  266. # try:
  267. # for i in range(5):
  268. # f.write("%d\n" % i)
  269. # finally:
  270. # f.close()
  271. # f = open(TESTFN, "r")
  272. # try:
  273. # self.assertEqual(list(f), ["0\n", "1\n", "2\n", "3\n", "4\n"])
  274. # f.seek(0, 0)
  275. # self.assertEqual(list(f),
  276. # ["0\n", "1\n", "2\n", "3\n", "4\n"])
  277. # finally:
  278. # f.close()
  279. # try:
  280. # unlink(TESTFN)
  281. # except OSError:
  282. # pass
  283. # Test tuples()'s use of iterators.
  284. def test_builtin_tuple(self):
  285. self.assertEqual(tuple(SequenceClass(5)), (0, 1, 2, 3, 4))
  286. self.assertEqual(tuple(SequenceClass(0)), ())
  287. self.assertEqual(tuple([]), ())
  288. self.assertEqual(tuple(()), ())
  289. self.assertEqual(tuple("abc"), ("a", "b", "c"))
  290. d = {"one": 1, "two": 2, "three": 3}
  291. self.assertEqual(tuple(d), tuple(d.keys()))
  292. self.assertRaises(TypeError, tuple, list)
  293. self.assertRaises(TypeError, tuple, 42)
  294. # f = open(TESTFN, "w")
  295. # try:
  296. # for i in range(5):
  297. # f.write("%d\n" % i)
  298. # finally:
  299. # f.close()
  300. # f = open(TESTFN, "r")
  301. # try:
  302. # self.assertEqual(tuple(f), ("0\n", "1\n", "2\n", "3\n", "4\n"))
  303. # f.seek(0, 0)
  304. # self.assertEqual(tuple(f),
  305. # ("0\n", "1\n", "2\n", "3\n", "4\n"))
  306. # finally:
  307. # f.close()
  308. # try:
  309. # unlink(TESTFN)
  310. # except OSError:
  311. # pass
  312. # Test filter()'s use of iterators.
  313. def test_builtin_filter(self):
  314. self.assertEqual(filter(None, SequenceClass(5)), range(1, 5))
  315. self.assertEqual(filter(None, SequenceClass(0)), [])
  316. self.assertEqual(filter(None, ()), ())
  317. self.assertEqual(filter(None, "abc"), "abc")
  318. d = {"one": 1, "two": 2, "three": 3}
  319. self.assertEqual(filter(None, d), d.keys())
  320. self.assertRaises(TypeError, filter, None, list)
  321. self.assertRaises(TypeError, filter, None, 42)
  322. class Boolean:
  323. def __init__(self, truth):
  324. self.truth = truth
  325. def __nonzero__(self):
  326. return self.truth
  327. bTrue = Boolean(1)
  328. bFalse = Boolean(0)
  329. class Seq:
  330. def __init__(self, *args):
  331. self.vals = args
  332. def __iter__(self):
  333. class SeqIter:
  334. def __init__(self, vals):
  335. self.vals = vals
  336. self.i = 0
  337. def __iter__(self):
  338. return self
  339. def next(self):
  340. i = self.i
  341. self.i = i + 1
  342. if i < len(self.vals):
  343. return self.vals[i]
  344. else:
  345. raise StopIteration
  346. return SeqIter(self.vals)
  347. seq = Seq(*([bTrue, bFalse] * 25))
  348. self.assertEqual(filter(lambda x: not x, seq), [bFalse]*25)
  349. # self.assertEqual(filter(lambda x: not x, iter(seq)), [bFalse]*25)
  350. # Test max() and min()'s use of iterators.
  351. def test_builtin_max_min(self):
  352. self.assertEqual(max(SequenceClass(5)), 4)
  353. self.assertEqual(min(SequenceClass(5)), 0)
  354. self.assertEqual(max(8, -1), 8)
  355. self.assertEqual(min(8, -1), -1)
  356. d = {"one": 1, "two": 2, "three": 3}
  357. self.assertEqual(max(d), "two")
  358. self.assertEqual(min(d), "one")
  359. #self.assertEqual(max(d.itervalues()), 3)
  360. #self.assertEqual(min(iter(d.itervalues())), 1)
  361. # f = open(TESTFN, "w")
  362. # try:
  363. # f.write("medium line\n")
  364. # f.write("xtra large line\n")
  365. # f.write("itty-bitty line\n")
  366. # finally:
  367. # f.close()
  368. # f = open(TESTFN, "r")
  369. # try:
  370. # self.assertEqual(min(f), "itty-bitty line\n")
  371. # f.seek(0, 0)
  372. # self.assertEqual(max(f), "xtra large line\n")
  373. # finally:
  374. # f.close()
  375. # try:
  376. # unlink(TESTFN)
  377. # except OSError:
  378. # pass
  379. # Test map()'s use of iterators.
  380. def test_builtin_map(self):
  381. self.assertEqual(map(lambda x: x+1, SequenceClass(5)), range(1, 6))
  382. d = {"one": 1, "two": 2, "three": 3}
  383. self.assertEqual(map(lambda k, d=d: (k, d[k]), d), d.items())
  384. dkeys = d.keys()
  385. expected = [(i < len(d) and dkeys[i] or None,
  386. i,
  387. i < len(d) and dkeys[i] or None)
  388. for i in range(5)]
  389. # Deprecated map(None, ...)
  390. # with check_py3k_warnings():
  391. # self.assertEqual(map(None, SequenceClass(5)), range(5))
  392. # self.assertEqual(map(None, d), d.keys())
  393. # self.assertEqual(map(None, d,
  394. # SequenceClass(5),
  395. # iter(d.iterkeys())),
  396. # expected)
  397. # f = open(TESTFN, "w")
  398. # try:
  399. # for i in range(10):
  400. # f.write("xy" * i + "\n") # line i has len 2*i+1
  401. # finally:
  402. # f.close()
  403. # f = open(TESTFN, "r")
  404. # try:
  405. # self.assertEqual(map(len, f), range(1, 21, 2))
  406. # finally:
  407. # f.close()
  408. # try:
  409. # unlink(TESTFN)
  410. # except OSError:
  411. # pass
  412. # Test zip()'s use of iterators.
  413. def test_builtin_zip(self):
  414. self.assertEqual(zip(), [])
  415. self.assertEqual(zip(*[]), [])
  416. self.assertEqual(zip(*[(1, 2), 'ab']), [(1, 'a'), (2, 'b')])
  417. self.assertRaises(TypeError, zip, None)
  418. self.assertRaises(TypeError, zip, range(10), 42)
  419. self.assertRaises(TypeError, zip, range(10), zip)
  420. self.assertEqual(zip(IteratingSequenceClass(3)),
  421. [(0,), (1,), (2,)])
  422. self.assertEqual(zip(SequenceClass(3)),
  423. [(0,), (1,), (2,)])
  424. d = {"one": 1, "two": 2, "three": 3}
  425. #self.assertEqual(d.items(), zip(d, d.itervalues()))
  426. self.assertEqual(d.items(), zip(d, d.values()))
  427. # Generate all ints starting at constructor arg.
  428. class IntsFrom:
  429. def __init__(self, start):
  430. self.i = start
  431. def __iter__(self):
  432. return self
  433. def next(self):
  434. i = self.i
  435. self.i = i+1
  436. return i
  437. # f = open(TESTFN, "w")
  438. # try:
  439. # f.write("a\n" "bbb\n" "cc\n")
  440. # finally:
  441. # f.close()
  442. # f = open(TESTFN, "r")
  443. # try:
  444. # self.assertEqual(zip(IntsFrom(0), f, IntsFrom(-100)),
  445. # [(0, "a\n", -100),
  446. # (1, "bbb\n", -99),
  447. # (2, "cc\n", -98)])
  448. # finally:
  449. # f.close()
  450. # try:
  451. # unlink(TESTFN)
  452. # except OSError:
  453. # pass
  454. self.assertEqual(zip(xrange(5)), [(i,) for i in range(5)])
  455. # Classes that lie about their lengths.
  456. class NoGuessLen5:
  457. def __getitem__(self, i):
  458. if i >= 5:
  459. raise IndexError
  460. return i
  461. class Guess3Len5(NoGuessLen5):
  462. def __len__(self):
  463. return 3
  464. class Guess30Len5(NoGuessLen5):
  465. def __len__(self):
  466. return 30
  467. self.assertEqual(len(Guess3Len5()), 3)
  468. self.assertEqual(len(Guess30Len5()), 30)
  469. self.assertEqual(zip(NoGuessLen5()), zip(range(5)))
  470. self.assertEqual(zip(Guess3Len5()), zip(range(5)))
  471. self.assertEqual(zip(Guess30Len5()), zip(range(5)))
  472. expected = [(i, i) for i in range(5)]
  473. for x in NoGuessLen5(), Guess3Len5(), Guess30Len5():
  474. for y in NoGuessLen5(), Guess3Len5(), Guess30Len5():
  475. self.assertEqual(zip(x, y), expected)
  476. # Test reduces()'s use of iterators.
  477. # def test_deprecated_builtin_reduce(self):
  478. # with check_py3k_warnings():
  479. # self._test_builtin_reduce()
  480. def _test_builtin_reduce(self):
  481. from operator import add
  482. self.assertEqual(reduce(add, SequenceClass(5)), 10)
  483. self.assertEqual(reduce(add, SequenceClass(5), 42), 52)
  484. self.assertRaises(TypeError, reduce, add, SequenceClass(0))
  485. self.assertEqual(reduce(add, SequenceClass(0), 42), 42)
  486. self.assertEqual(reduce(add, SequenceClass(1)), 0)
  487. self.assertEqual(reduce(add, SequenceClass(1), 42), 42)
  488. d = {"one": 1, "two": 2, "three": 3}
  489. self.assertEqual(reduce(add, d), "".join(d.keys()))
  490. # @unittest.skipUnless(have_unicode, 'needs unicode support')
  491. # def test_unicode_join_endcase(self):
  492. # # This class inserts a Unicode object into its argument's natural
  493. # # iteration, in the 3rd position.
  494. # class OhPhooey:
  495. # def __init__(self, seq):
  496. # self.it = iter(seq)
  497. # self.i = 0
  498. # def __iter__(self):
  499. # return self
  500. # def next(self):
  501. # i = self.i
  502. # self.i = i+1
  503. # if i == 2:
  504. # return unicode("fooled you!")
  505. # return self.it.next()
  506. # f = open(TESTFN, "w")
  507. # try:
  508. # f.write("a\n" + "b\n" + "c\n")
  509. # finally:
  510. # f.close()
  511. # f = open(TESTFN, "r")
  512. # # Nasty: string.join(s) can't know whether unicode.join() is needed
  513. # # until it's seen all of s's elements. But in this case, f's
  514. # # iterator cannot be restarted. So what we're testing here is
  515. # # whether string.join() can manage to remember everything it's seen
  516. # # and pass that on to unicode.join().
  517. # try:
  518. # got = " - ".join(OhPhooey(f))
  519. # self.assertEqual(got, unicode("a\n - b\n - fooled you! - c\n"))
  520. # finally:
  521. # f.close()
  522. # try:
  523. # unlink(TESTFN)
  524. # except OSError:
  525. # pass
  526. # Test iterators with 'x in y' and 'x not in y'.
  527. def test_in_and_not_in(self):
  528. for sc5 in IteratingSequenceClass(5), SequenceClass(5):
  529. for i in range(5):
  530. self.assertIn(i, sc5)
  531. for i in "abc", -1, 5, 42.42, (3, 4), [], {1: 1}, 3-12j, sc5:
  532. self.assertNotIn(i, sc5)
  533. self.assertRaises(TypeError, lambda: 3 in 12)
  534. self.assertRaises(TypeError, lambda: 3 not in map)
  535. d = {"one": 1, "two": 2, "three": 3, 1j: 2j}
  536. for k in d:
  537. self.assertIn(k, d)
  538. #self.assertNotIn(k, d.itervalues())
  539. for v in d.values():
  540. #self.assertIn(v, d.itervalues())
  541. self.assertNotIn(v, d)
  542. #for k, v in d.iteritems():
  543. # self.assertIn((k, v), d.iteritems())
  544. # self.assertNotIn((v, k), d.iteritems())
  545. # f = open(TESTFN, "w")
  546. # try:
  547. # f.write("a\n" "b\n" "c\n")
  548. # finally:
  549. # f.close()
  550. # f = open(TESTFN, "r")
  551. # try:
  552. # for chunk in "abc":
  553. # f.seek(0, 0)
  554. # self.assertNotIn(chunk, f)
  555. # f.seek(0, 0)
  556. # self.assertIn((chunk + "\n"), f)
  557. # finally:
  558. # f.close()
  559. # try:
  560. # unlink(TESTFN)
  561. # except OSError:
  562. # pass
  563. # Test iterators with operator.countOf (PySequence_Count).
  564. def test_countOf(self):
  565. from operator import countOf
  566. self.assertEqual(countOf([1,2,2,3,2,5], 2), 3)
  567. self.assertEqual(countOf((1,2,2,3,2,5), 2), 3)
  568. self.assertEqual(countOf("122325", "2"), 3)
  569. self.assertEqual(countOf("122325", "6"), 0)
  570. self.assertRaises(TypeError, countOf, 42, 1)
  571. self.assertRaises(TypeError, countOf, countOf, countOf)
  572. d = {"one": 3, "two": 3, "three": 3, 1j: 2j}
  573. for k in d:
  574. self.assertEqual(countOf(d, k), 1)
  575. #self.assertEqual(countOf(d.itervalues(), 3), 3)
  576. #self.assertEqual(countOf(d.itervalues(), 2j), 1)
  577. #self.assertEqual(countOf(d.itervalues(), 1j), 0)
  578. # f = open(TESTFN, "w")
  579. # try:
  580. # f.write("a\n" "b\n" "c\n" "b\n")
  581. # finally:
  582. # f.close()
  583. # f = open(TESTFN, "r")
  584. # try:
  585. # for letter, count in ("a", 1), ("b", 2), ("c", 1), ("d", 0):
  586. # f.seek(0, 0)
  587. # self.assertEqual(countOf(f, letter + "\n"), count)
  588. # finally:
  589. # f.close()
  590. # try:
  591. # unlink(TESTFN)
  592. # except OSError:
  593. # pass
  594. # Test iterators with operator.indexOf (PySequence_Index).
  595. def test_indexOf(self):
  596. from operator import indexOf
  597. self.assertEqual(indexOf([1,2,2,3,2,5], 1), 0)
  598. self.assertEqual(indexOf((1,2,2,3,2,5), 2), 1)
  599. self.assertEqual(indexOf((1,2,2,3,2,5), 3), 3)
  600. self.assertEqual(indexOf((1,2,2,3,2,5), 5), 5)
  601. self.assertRaises(ValueError, indexOf, (1,2,2,3,2,5), 0)
  602. self.assertRaises(ValueError, indexOf, (1,2,2,3,2,5), 6)
  603. self.assertEqual(indexOf("122325", "2"), 1)
  604. self.assertEqual(indexOf("122325", "5"), 5)
  605. self.assertRaises(ValueError, indexOf, "122325", "6")
  606. self.assertRaises(TypeError, indexOf, 42, 1)
  607. self.assertRaises(TypeError, indexOf, indexOf, indexOf)
  608. # f = open(TESTFN, "w")
  609. # try:
  610. # f.write("a\n" "b\n" "c\n" "d\n" "e\n")
  611. # finally:
  612. # f.close()
  613. # f = open(TESTFN, "r")
  614. # try:
  615. # fiter = iter(f)
  616. # self.assertEqual(indexOf(fiter, "b\n"), 1)
  617. # self.assertEqual(indexOf(fiter, "d\n"), 1)
  618. # self.assertEqual(indexOf(fiter, "e\n"), 0)
  619. # self.assertRaises(ValueError, indexOf, fiter, "a\n")
  620. # finally:
  621. # f.close()
  622. # try:
  623. # unlink(TESTFN)
  624. # except OSError:
  625. # pass
  626. iclass = IteratingSequenceClass(3)
  627. for i in range(3):
  628. self.assertEqual(indexOf(iclass, i), i)
  629. self.assertRaises(ValueError, indexOf, iclass, -1)
  630. # Test iterators with file.writelines().
  631. # def test_writelines(self):
  632. # f = file(TESTFN, "w")
  633. # try:
  634. # self.assertRaises(TypeError, f.writelines, None)
  635. # self.assertRaises(TypeError, f.writelines, 42)
  636. # f.writelines(["1\n", "2\n"])
  637. # f.writelines(("3\n", "4\n"))
  638. # f.writelines({'5\n': None})
  639. # f.writelines({})
  640. # # Try a big chunk too.
  641. # class Iterator:
  642. # def __init__(self, start, finish):
  643. # self.start = start
  644. # self.finish = finish
  645. # self.i = self.start
  646. # def next(self):
  647. # if self.i >= self.finish:
  648. # raise StopIteration
  649. # result = str(self.i) + '\n'
  650. # self.i += 1
  651. # return result
  652. # def __iter__(self):
  653. # return self
  654. # class Whatever:
  655. # def __init__(self, start, finish):
  656. # self.start = start
  657. # self.finish = finish
  658. # def __iter__(self):
  659. # return Iterator(self.start, self.finish)
  660. # f.writelines(Whatever(6, 6+2000))
  661. # f.close()
  662. # f = file(TESTFN)
  663. # expected = [str(i) + "\n" for i in range(1, 2006)]
  664. # self.assertEqual(list(f), expected)
  665. # finally:
  666. # f.close()
  667. # try:
  668. # unlink(TESTFN)
  669. # except OSError:
  670. # pass
  671. # Test iterators on RHS of unpacking assignments.
  672. def test_unpack_iter(self):
  673. a, b = 1, 2
  674. self.assertEqual((a, b), (1, 2))
  675. a, b, c = IteratingSequenceClass(3)
  676. self.assertEqual((a, b, c), (0, 1, 2))
  677. try: # too many values
  678. a, b = IteratingSequenceClass(3)
  679. except ValueError:
  680. pass
  681. else:
  682. self.fail("should have raised ValueError")
  683. try: # not enough values
  684. a, b, c = IteratingSequenceClass(2)
  685. except ValueError:
  686. pass
  687. else:
  688. self.fail("should have raised ValueError")
  689. try: # not iterable
  690. a, b, c = len
  691. except TypeError:
  692. pass
  693. else:
  694. self.fail("should have raised TypeError")
  695. #a, b, c = {1: 42, 2: 42, 3: 42}.itervalues()
  696. #self.assertEqual((a, b, c), (42, 42, 42))
  697. # f = open(TESTFN, "w")
  698. # lines = ("a\n", "bb\n", "ccc\n")
  699. # try:
  700. # for line in lines:
  701. # f.write(line)
  702. # finally:
  703. # f.close()
  704. # f = open(TESTFN, "r")
  705. # try:
  706. # a, b, c = f
  707. # self.assertEqual((a, b, c), lines)
  708. # finally:
  709. # f.close()
  710. # try:
  711. # unlink(TESTFN)
  712. # except OSError:
  713. # pass
  714. (a, b), (c,) = IteratingSequenceClass(2), {42: 24}
  715. self.assertEqual((a, b, c), (0, 1, 42))
  716. # @cpython_only
  717. # def test_ref_counting_behavior(self):
  718. # class C(object):
  719. # count = 0
  720. # def __new__(cls):
  721. # cls.count += 1
  722. # return object.__new__(cls)
  723. # def __del__(self):
  724. # cls = self.__class__
  725. # assert cls.count > 0
  726. # cls.count -= 1
  727. # x = C()
  728. # self.assertEqual(C.count, 1)
  729. # del x
  730. # self.assertEqual(C.count, 0)
  731. # l = [C(), C(), C()]
  732. # self.assertEqual(C.count, 3)
  733. # try:
  734. # a, b = iter(l)
  735. # except ValueError:
  736. # pass
  737. # del l
  738. # self.assertEqual(C.count, 0)
  739. # Make sure StopIteration is a "sink state".
  740. # This tests various things that weren't sink states in Python 2.2.1,
  741. # plus various things that always were fine.
  742. def test_sinkstate_list(self):
  743. # This used to fail
  744. a = range(5)
  745. b = iter(a)
  746. # print "list of b", list(b)
  747. # print next(b)
  748. self.assertEqual(list(b), range(5))
  749. a.extend(range(5, 10))
  750. self.assertEqual(list(b), [])
  751. def test_sinkstate_tuple(self):
  752. a = (0, 1, 2, 3, 4)
  753. b = iter(a)
  754. self.assertEqual(list(b), range(5))
  755. self.assertEqual(list(b), [])
  756. def test_sinkstate_string(self):
  757. a = "abcde"
  758. b = iter(a)
  759. self.assertEqual(list(b), ['a', 'b', 'c', 'd', 'e'])
  760. self.assertEqual(list(b), [])
  761. # def test_sinkstate_callable(self):
  762. # # This used to fail
  763. # a = SequenceClass(5)
  764. # b = iter(a)
  765. # self.assertEqual(list(b), range(5))
  766. # a.n = 10
  767. # self.assertEqual(list(b), [])
  768. def test_sinkstate_callable(self):
  769. # This used to fail
  770. def spam(state=[0]):
  771. i = state[0]
  772. state[0] = i+1
  773. if i == 10:
  774. raise AssertionError, "shouldn't have gotten this far"
  775. return i
  776. b = iter(spam, 5)
  777. self.assertEqual(list(b), range(5))
  778. self.assertEqual(list(b), [])
  779. # def test_sinkstate_dict(self):
  780. # # XXX For a more thorough test, see towards the end of:
  781. # # http://mail.python.org/pipermail/python-dev/2002-July/026512.html
  782. # a = {1:1, 2:2, 0:0, 4:4, 3:3}
  783. # for b in iter(a), a.iterkeys(), a.iteritems(), a.itervalues():
  784. # b = iter(a)
  785. # self.assertEqual(len(list(b)), 5)
  786. # self.assertEqual(list(b), [])
  787. def test_sinkstate_yield(self):
  788. def gen():
  789. for i in range(5):
  790. yield i
  791. b = gen()
  792. self.assertEqual(list(b), range(5))
  793. self.assertEqual(list(b), [])
  794. def test_sinkstate_range(self):
  795. a = xrange(5)
  796. b = iter(a)
  797. self.assertEqual(list(b), range(5))
  798. self.assertEqual(list(b), [])
  799. def test_sinkstate_enumerate(self):
  800. a = range(5)
  801. e = enumerate(a)
  802. b = iter(e)
  803. self.assertEqual(list(b), zip(range(5), range(5)))
  804. self.assertEqual(list(b), [])
  805. # def test_3720(self):
  806. # # Avoid a crash, when an iterator deletes its next() method.
  807. # class BadIterator(object):
  808. # def __iter__(self):
  809. # return self
  810. # def next(self):
  811. # del BadIterator.next
  812. # return 1
  813. # try:
  814. # for i in BadIterator() :
  815. # pass
  816. # except TypeError:
  817. # pass
  818. def test_extending_list_with_iterator_does_not_segfault(self):
  819. # The code to extend a list with an iterator has a fair
  820. # amount of nontrivial logic in terms of guessing how
  821. # much memory to allocate in advance, "stealing" refs,
  822. # and then shrinking at the end. This is a basic smoke
  823. # test for that scenario.
  824. def gen():
  825. for i in range(500):
  826. yield i
  827. lst = [0] * 500
  828. for i in range(240):
  829. lst.pop(0)
  830. lst.extend(gen())
  831. self.assertEqual(len(lst), 760)
  832. if __name__ == "__main__":
  833. unittest.main()