test_itertools.py 98 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364
  1. import doctest
  2. import unittest
  3. from test import support
  4. from test.support import threading_helper
  5. from itertools import *
  6. import weakref
  7. from decimal import Decimal
  8. from fractions import Fraction
  9. import operator
  10. import random
  11. import copy
  12. import pickle
  13. from functools import reduce
  14. import sys
  15. import struct
  16. import threading
  17. import gc
  18. maxsize = support.MAX_Py_ssize_t
  19. minsize = -maxsize-1
  20. def lzip(*args):
  21. return list(zip(*args))
  22. def onearg(x):
  23. 'Test function of one argument'
  24. return 2*x
  25. def errfunc(*args):
  26. 'Test function that raises an error'
  27. raise ValueError
  28. def gen3():
  29. 'Non-restartable source sequence'
  30. for i in (0, 1, 2):
  31. yield i
  32. def isEven(x):
  33. 'Test predicate'
  34. return x%2==0
  35. def isOdd(x):
  36. 'Test predicate'
  37. return x%2==1
  38. def tupleize(*args):
  39. return args
  40. def irange(n):
  41. for i in range(n):
  42. yield i
  43. class StopNow:
  44. 'Class emulating an empty iterable.'
  45. def __iter__(self):
  46. return self
  47. def __next__(self):
  48. raise StopIteration
  49. def take(n, seq):
  50. 'Convenience function for partially consuming a long of infinite iterable'
  51. return list(islice(seq, n))
  52. def prod(iterable):
  53. return reduce(operator.mul, iterable, 1)
  54. def fact(n):
  55. 'Factorial'
  56. return prod(range(1, n+1))
  57. # root level methods for pickling ability
  58. def testR(r):
  59. return r[0]
  60. def testR2(r):
  61. return r[2]
  62. def underten(x):
  63. return x<10
  64. picklecopiers = [lambda s, proto=proto: pickle.loads(pickle.dumps(s, proto))
  65. for proto in range(pickle.HIGHEST_PROTOCOL + 1)]
  66. class TestBasicOps(unittest.TestCase):
  67. def pickletest(self, protocol, it, stop=4, take=1, compare=None):
  68. """Test that an iterator is the same after pickling, also when part-consumed"""
  69. def expand(it, i=0):
  70. # Recursively expand iterables, within sensible bounds
  71. if i > 10:
  72. raise RuntimeError("infinite recursion encountered")
  73. if isinstance(it, str):
  74. return it
  75. try:
  76. l = list(islice(it, stop))
  77. except TypeError:
  78. return it # can't expand it
  79. return [expand(e, i+1) for e in l]
  80. # Test the initial copy against the original
  81. dump = pickle.dumps(it, protocol)
  82. i2 = pickle.loads(dump)
  83. self.assertEqual(type(it), type(i2))
  84. a, b = expand(it), expand(i2)
  85. self.assertEqual(a, b)
  86. if compare:
  87. c = expand(compare)
  88. self.assertEqual(a, c)
  89. # Take from the copy, and create another copy and compare them.
  90. i3 = pickle.loads(dump)
  91. took = 0
  92. try:
  93. for i in range(take):
  94. next(i3)
  95. took += 1
  96. except StopIteration:
  97. pass #in case there is less data than 'take'
  98. dump = pickle.dumps(i3, protocol)
  99. i4 = pickle.loads(dump)
  100. a, b = expand(i3), expand(i4)
  101. self.assertEqual(a, b)
  102. if compare:
  103. c = expand(compare[took:])
  104. self.assertEqual(a, c);
  105. def test_accumulate(self):
  106. self.assertEqual(list(accumulate(range(10))), # one positional arg
  107. [0, 1, 3, 6, 10, 15, 21, 28, 36, 45])
  108. self.assertEqual(list(accumulate(iterable=range(10))), # kw arg
  109. [0, 1, 3, 6, 10, 15, 21, 28, 36, 45])
  110. for typ in int, complex, Decimal, Fraction: # multiple types
  111. self.assertEqual(
  112. list(accumulate(map(typ, range(10)))),
  113. list(map(typ, [0, 1, 3, 6, 10, 15, 21, 28, 36, 45])))
  114. self.assertEqual(list(accumulate('abc')), ['a', 'ab', 'abc']) # works with non-numeric
  115. self.assertEqual(list(accumulate([])), []) # empty iterable
  116. self.assertEqual(list(accumulate([7])), [7]) # iterable of length one
  117. self.assertRaises(TypeError, accumulate, range(10), 5, 6) # too many args
  118. self.assertRaises(TypeError, accumulate) # too few args
  119. self.assertRaises(TypeError, accumulate, x=range(10)) # unexpected kwd arg
  120. self.assertRaises(TypeError, list, accumulate([1, []])) # args that don't add
  121. s = [2, 8, 9, 5, 7, 0, 3, 4, 1, 6]
  122. self.assertEqual(list(accumulate(s, min)),
  123. [2, 2, 2, 2, 2, 0, 0, 0, 0, 0])
  124. self.assertEqual(list(accumulate(s, max)),
  125. [2, 8, 9, 9, 9, 9, 9, 9, 9, 9])
  126. self.assertEqual(list(accumulate(s, operator.mul)),
  127. [2, 16, 144, 720, 5040, 0, 0, 0, 0, 0])
  128. with self.assertRaises(TypeError):
  129. list(accumulate(s, chr)) # unary-operation
  130. for proto in range(pickle.HIGHEST_PROTOCOL + 1):
  131. self.pickletest(proto, accumulate(range(10))) # test pickling
  132. self.pickletest(proto, accumulate(range(10), initial=7))
  133. self.assertEqual(list(accumulate([10, 5, 1], initial=None)), [10, 15, 16])
  134. self.assertEqual(list(accumulate([10, 5, 1], initial=100)), [100, 110, 115, 116])
  135. self.assertEqual(list(accumulate([], initial=100)), [100])
  136. with self.assertRaises(TypeError):
  137. list(accumulate([10, 20], 100))
  138. def test_chain(self):
  139. def chain2(*iterables):
  140. 'Pure python version in the docs'
  141. for it in iterables:
  142. for element in it:
  143. yield element
  144. for c in (chain, chain2):
  145. self.assertEqual(list(c('abc', 'def')), list('abcdef'))
  146. self.assertEqual(list(c('abc')), list('abc'))
  147. self.assertEqual(list(c('')), [])
  148. self.assertEqual(take(4, c('abc', 'def')), list('abcd'))
  149. self.assertRaises(TypeError, list,c(2, 3))
  150. def test_chain_from_iterable(self):
  151. self.assertEqual(list(chain.from_iterable(['abc', 'def'])), list('abcdef'))
  152. self.assertEqual(list(chain.from_iterable(['abc'])), list('abc'))
  153. self.assertEqual(list(chain.from_iterable([''])), [])
  154. self.assertEqual(take(4, chain.from_iterable(['abc', 'def'])), list('abcd'))
  155. self.assertRaises(TypeError, list, chain.from_iterable([2, 3]))
  156. def test_chain_reducible(self):
  157. for oper in [copy.deepcopy] + picklecopiers:
  158. it = chain('abc', 'def')
  159. self.assertEqual(list(oper(it)), list('abcdef'))
  160. self.assertEqual(next(it), 'a')
  161. self.assertEqual(list(oper(it)), list('bcdef'))
  162. self.assertEqual(list(oper(chain(''))), [])
  163. self.assertEqual(take(4, oper(chain('abc', 'def'))), list('abcd'))
  164. self.assertRaises(TypeError, list, oper(chain(2, 3)))
  165. for proto in range(pickle.HIGHEST_PROTOCOL + 1):
  166. self.pickletest(proto, chain('abc', 'def'), compare=list('abcdef'))
  167. def test_chain_setstate(self):
  168. self.assertRaises(TypeError, chain().__setstate__, ())
  169. self.assertRaises(TypeError, chain().__setstate__, [])
  170. self.assertRaises(TypeError, chain().__setstate__, 0)
  171. self.assertRaises(TypeError, chain().__setstate__, ([],))
  172. self.assertRaises(TypeError, chain().__setstate__, (iter([]), []))
  173. it = chain()
  174. it.__setstate__((iter(['abc', 'def']),))
  175. self.assertEqual(list(it), ['a', 'b', 'c', 'd', 'e', 'f'])
  176. it = chain()
  177. it.__setstate__((iter(['abc', 'def']), iter(['ghi'])))
  178. self.assertEqual(list(it), ['ghi', 'a', 'b', 'c', 'd', 'e', 'f'])
  179. def test_combinations(self):
  180. self.assertRaises(TypeError, combinations, 'abc') # missing r argument
  181. self.assertRaises(TypeError, combinations, 'abc', 2, 1) # too many arguments
  182. self.assertRaises(TypeError, combinations, None) # pool is not iterable
  183. self.assertRaises(ValueError, combinations, 'abc', -2) # r is negative
  184. for op in [lambda a:a] + picklecopiers:
  185. self.assertEqual(list(op(combinations('abc', 32))), []) # r > n
  186. self.assertEqual(list(op(combinations('ABCD', 2))),
  187. [('A','B'), ('A','C'), ('A','D'), ('B','C'), ('B','D'), ('C','D')])
  188. testIntermediate = combinations('ABCD', 2)
  189. next(testIntermediate)
  190. self.assertEqual(list(op(testIntermediate)),
  191. [('A','C'), ('A','D'), ('B','C'), ('B','D'), ('C','D')])
  192. self.assertEqual(list(op(combinations(range(4), 3))),
  193. [(0,1,2), (0,1,3), (0,2,3), (1,2,3)])
  194. testIntermediate = combinations(range(4), 3)
  195. next(testIntermediate)
  196. self.assertEqual(list(op(testIntermediate)),
  197. [(0,1,3), (0,2,3), (1,2,3)])
  198. def combinations1(iterable, r):
  199. 'Pure python version shown in the docs'
  200. pool = tuple(iterable)
  201. n = len(pool)
  202. if r > n:
  203. return
  204. indices = list(range(r))
  205. yield tuple(pool[i] for i in indices)
  206. while 1:
  207. for i in reversed(range(r)):
  208. if indices[i] != i + n - r:
  209. break
  210. else:
  211. return
  212. indices[i] += 1
  213. for j in range(i+1, r):
  214. indices[j] = indices[j-1] + 1
  215. yield tuple(pool[i] for i in indices)
  216. def combinations2(iterable, r):
  217. 'Pure python version shown in the docs'
  218. pool = tuple(iterable)
  219. n = len(pool)
  220. for indices in permutations(range(n), r):
  221. if sorted(indices) == list(indices):
  222. yield tuple(pool[i] for i in indices)
  223. def combinations3(iterable, r):
  224. 'Pure python version from cwr()'
  225. pool = tuple(iterable)
  226. n = len(pool)
  227. for indices in combinations_with_replacement(range(n), r):
  228. if len(set(indices)) == r:
  229. yield tuple(pool[i] for i in indices)
  230. for n in range(7):
  231. values = [5*x-12 for x in range(n)]
  232. for r in range(n+2):
  233. result = list(combinations(values, r))
  234. self.assertEqual(len(result), 0 if r>n else fact(n) / fact(r) / fact(n-r)) # right number of combs
  235. self.assertEqual(len(result), len(set(result))) # no repeats
  236. self.assertEqual(result, sorted(result)) # lexicographic order
  237. for c in result:
  238. self.assertEqual(len(c), r) # r-length combinations
  239. self.assertEqual(len(set(c)), r) # no duplicate elements
  240. self.assertEqual(list(c), sorted(c)) # keep original ordering
  241. self.assertTrue(all(e in values for e in c)) # elements taken from input iterable
  242. self.assertEqual(list(c),
  243. [e for e in values if e in c]) # comb is a subsequence of the input iterable
  244. self.assertEqual(result, list(combinations1(values, r))) # matches first pure python version
  245. self.assertEqual(result, list(combinations2(values, r))) # matches second pure python version
  246. self.assertEqual(result, list(combinations3(values, r))) # matches second pure python version
  247. for proto in range(pickle.HIGHEST_PROTOCOL + 1):
  248. self.pickletest(proto, combinations(values, r)) # test pickling
  249. @support.bigaddrspacetest
  250. def test_combinations_overflow(self):
  251. with self.assertRaises((OverflowError, MemoryError)):
  252. combinations("AA", 2**29)
  253. # Test implementation detail: tuple re-use
  254. @support.impl_detail("tuple reuse is specific to CPython")
  255. def test_combinations_tuple_reuse(self):
  256. self.assertEqual(len(set(map(id, combinations('abcde', 3)))), 1)
  257. self.assertNotEqual(len(set(map(id, list(combinations('abcde', 3))))), 1)
  258. def test_combinations_with_replacement(self):
  259. cwr = combinations_with_replacement
  260. self.assertRaises(TypeError, cwr, 'abc') # missing r argument
  261. self.assertRaises(TypeError, cwr, 'abc', 2, 1) # too many arguments
  262. self.assertRaises(TypeError, cwr, None) # pool is not iterable
  263. self.assertRaises(ValueError, cwr, 'abc', -2) # r is negative
  264. for op in [lambda a:a] + picklecopiers:
  265. self.assertEqual(list(op(cwr('ABC', 2))),
  266. [('A','A'), ('A','B'), ('A','C'), ('B','B'), ('B','C'), ('C','C')])
  267. testIntermediate = cwr('ABC', 2)
  268. next(testIntermediate)
  269. self.assertEqual(list(op(testIntermediate)),
  270. [('A','B'), ('A','C'), ('B','B'), ('B','C'), ('C','C')])
  271. def cwr1(iterable, r):
  272. 'Pure python version shown in the docs'
  273. # number items returned: (n+r-1)! / r! / (n-1)! when n>0
  274. pool = tuple(iterable)
  275. n = len(pool)
  276. if not n and r:
  277. return
  278. indices = [0] * r
  279. yield tuple(pool[i] for i in indices)
  280. while 1:
  281. for i in reversed(range(r)):
  282. if indices[i] != n - 1:
  283. break
  284. else:
  285. return
  286. indices[i:] = [indices[i] + 1] * (r - i)
  287. yield tuple(pool[i] for i in indices)
  288. def cwr2(iterable, r):
  289. 'Pure python version shown in the docs'
  290. pool = tuple(iterable)
  291. n = len(pool)
  292. for indices in product(range(n), repeat=r):
  293. if sorted(indices) == list(indices):
  294. yield tuple(pool[i] for i in indices)
  295. def numcombs(n, r):
  296. if not n:
  297. return 0 if r else 1
  298. return fact(n+r-1) / fact(r)/ fact(n-1)
  299. for n in range(7):
  300. values = [5*x-12 for x in range(n)]
  301. for r in range(n+2):
  302. result = list(cwr(values, r))
  303. self.assertEqual(len(result), numcombs(n, r)) # right number of combs
  304. self.assertEqual(len(result), len(set(result))) # no repeats
  305. self.assertEqual(result, sorted(result)) # lexicographic order
  306. regular_combs = list(combinations(values, r)) # compare to combs without replacement
  307. if n == 0 or r <= 1:
  308. self.assertEqual(result, regular_combs) # cases that should be identical
  309. else:
  310. self.assertTrue(set(result) >= set(regular_combs)) # rest should be supersets of regular combs
  311. for c in result:
  312. self.assertEqual(len(c), r) # r-length combinations
  313. noruns = [k for k,v in groupby(c)] # combo without consecutive repeats
  314. self.assertEqual(len(noruns), len(set(noruns))) # no repeats other than consecutive
  315. self.assertEqual(list(c), sorted(c)) # keep original ordering
  316. self.assertTrue(all(e in values for e in c)) # elements taken from input iterable
  317. self.assertEqual(noruns,
  318. [e for e in values if e in c]) # comb is a subsequence of the input iterable
  319. self.assertEqual(result, list(cwr1(values, r))) # matches first pure python version
  320. self.assertEqual(result, list(cwr2(values, r))) # matches second pure python version
  321. for proto in range(pickle.HIGHEST_PROTOCOL + 1):
  322. self.pickletest(proto, cwr(values,r)) # test pickling
  323. @support.bigaddrspacetest
  324. def test_combinations_with_replacement_overflow(self):
  325. with self.assertRaises((OverflowError, MemoryError)):
  326. combinations_with_replacement("AA", 2**30)
  327. # Test implementation detail: tuple re-use
  328. @support.impl_detail("tuple reuse is specific to CPython")
  329. def test_combinations_with_replacement_tuple_reuse(self):
  330. cwr = combinations_with_replacement
  331. self.assertEqual(len(set(map(id, cwr('abcde', 3)))), 1)
  332. self.assertNotEqual(len(set(map(id, list(cwr('abcde', 3))))), 1)
  333. def test_permutations(self):
  334. self.assertRaises(TypeError, permutations) # too few arguments
  335. self.assertRaises(TypeError, permutations, 'abc', 2, 1) # too many arguments
  336. self.assertRaises(TypeError, permutations, None) # pool is not iterable
  337. self.assertRaises(ValueError, permutations, 'abc', -2) # r is negative
  338. self.assertEqual(list(permutations('abc', 32)), []) # r > n
  339. self.assertRaises(TypeError, permutations, 'abc', 's') # r is not an int or None
  340. self.assertEqual(list(permutations(range(3), 2)),
  341. [(0,1), (0,2), (1,0), (1,2), (2,0), (2,1)])
  342. def permutations1(iterable, r=None):
  343. 'Pure python version shown in the docs'
  344. pool = tuple(iterable)
  345. n = len(pool)
  346. r = n if r is None else r
  347. if r > n:
  348. return
  349. indices = list(range(n))
  350. cycles = list(range(n-r+1, n+1))[::-1]
  351. yield tuple(pool[i] for i in indices[:r])
  352. while n:
  353. for i in reversed(range(r)):
  354. cycles[i] -= 1
  355. if cycles[i] == 0:
  356. indices[i:] = indices[i+1:] + indices[i:i+1]
  357. cycles[i] = n - i
  358. else:
  359. j = cycles[i]
  360. indices[i], indices[-j] = indices[-j], indices[i]
  361. yield tuple(pool[i] for i in indices[:r])
  362. break
  363. else:
  364. return
  365. def permutations2(iterable, r=None):
  366. 'Pure python version shown in the docs'
  367. pool = tuple(iterable)
  368. n = len(pool)
  369. r = n if r is None else r
  370. for indices in product(range(n), repeat=r):
  371. if len(set(indices)) == r:
  372. yield tuple(pool[i] for i in indices)
  373. for n in range(7):
  374. values = [5*x-12 for x in range(n)]
  375. for r in range(n+2):
  376. result = list(permutations(values, r))
  377. self.assertEqual(len(result), 0 if r>n else fact(n) / fact(n-r)) # right number of perms
  378. self.assertEqual(len(result), len(set(result))) # no repeats
  379. self.assertEqual(result, sorted(result)) # lexicographic order
  380. for p in result:
  381. self.assertEqual(len(p), r) # r-length permutations
  382. self.assertEqual(len(set(p)), r) # no duplicate elements
  383. self.assertTrue(all(e in values for e in p)) # elements taken from input iterable
  384. self.assertEqual(result, list(permutations1(values, r))) # matches first pure python version
  385. self.assertEqual(result, list(permutations2(values, r))) # matches second pure python version
  386. if r == n:
  387. self.assertEqual(result, list(permutations(values, None))) # test r as None
  388. self.assertEqual(result, list(permutations(values))) # test default r
  389. for proto in range(pickle.HIGHEST_PROTOCOL + 1):
  390. self.pickletest(proto, permutations(values, r)) # test pickling
  391. @support.bigaddrspacetest
  392. def test_permutations_overflow(self):
  393. with self.assertRaises((OverflowError, MemoryError)):
  394. permutations("A", 2**30)
  395. @support.impl_detail("tuple reuse is specific to CPython")
  396. def test_permutations_tuple_reuse(self):
  397. self.assertEqual(len(set(map(id, permutations('abcde', 3)))), 1)
  398. self.assertNotEqual(len(set(map(id, list(permutations('abcde', 3))))), 1)
  399. def test_combinatorics(self):
  400. # Test relationships between product(), permutations(),
  401. # combinations() and combinations_with_replacement().
  402. for n in range(6):
  403. s = 'ABCDEFG'[:n]
  404. for r in range(8):
  405. prod = list(product(s, repeat=r))
  406. cwr = list(combinations_with_replacement(s, r))
  407. perm = list(permutations(s, r))
  408. comb = list(combinations(s, r))
  409. # Check size
  410. self.assertEqual(len(prod), n**r)
  411. self.assertEqual(len(cwr), (fact(n+r-1) / fact(r)/ fact(n-1)) if n else (not r))
  412. self.assertEqual(len(perm), 0 if r>n else fact(n) / fact(n-r))
  413. self.assertEqual(len(comb), 0 if r>n else fact(n) / fact(r) / fact(n-r))
  414. # Check lexicographic order without repeated tuples
  415. self.assertEqual(prod, sorted(set(prod)))
  416. self.assertEqual(cwr, sorted(set(cwr)))
  417. self.assertEqual(perm, sorted(set(perm)))
  418. self.assertEqual(comb, sorted(set(comb)))
  419. # Check interrelationships
  420. self.assertEqual(cwr, [t for t in prod if sorted(t)==list(t)]) # cwr: prods which are sorted
  421. self.assertEqual(perm, [t for t in prod if len(set(t))==r]) # perm: prods with no dups
  422. self.assertEqual(comb, [t for t in perm if sorted(t)==list(t)]) # comb: perms that are sorted
  423. self.assertEqual(comb, [t for t in cwr if len(set(t))==r]) # comb: cwrs without dups
  424. self.assertEqual(comb, list(filter(set(cwr).__contains__, perm))) # comb: perm that is a cwr
  425. self.assertEqual(comb, list(filter(set(perm).__contains__, cwr))) # comb: cwr that is a perm
  426. self.assertEqual(comb, sorted(set(cwr) & set(perm))) # comb: both a cwr and a perm
  427. def test_compress(self):
  428. self.assertEqual(list(compress(data='ABCDEF', selectors=[1,0,1,0,1,1])), list('ACEF'))
  429. self.assertEqual(list(compress('ABCDEF', [1,0,1,0,1,1])), list('ACEF'))
  430. self.assertEqual(list(compress('ABCDEF', [0,0,0,0,0,0])), list(''))
  431. self.assertEqual(list(compress('ABCDEF', [1,1,1,1,1,1])), list('ABCDEF'))
  432. self.assertEqual(list(compress('ABCDEF', [1,0,1])), list('AC'))
  433. self.assertEqual(list(compress('ABC', [0,1,1,1,1,1])), list('BC'))
  434. n = 10000
  435. data = chain.from_iterable(repeat(range(6), n))
  436. selectors = chain.from_iterable(repeat((0, 1)))
  437. self.assertEqual(list(compress(data, selectors)), [1,3,5] * n)
  438. self.assertRaises(TypeError, compress, None, range(6)) # 1st arg not iterable
  439. self.assertRaises(TypeError, compress, range(6), None) # 2nd arg not iterable
  440. self.assertRaises(TypeError, compress, range(6)) # too few args
  441. self.assertRaises(TypeError, compress, range(6), None) # too many args
  442. # check copy, deepcopy, pickle
  443. for op in [lambda a:copy.copy(a), lambda a:copy.deepcopy(a)] + picklecopiers:
  444. for data, selectors, result1, result2 in [
  445. ('ABCDEF', [1,0,1,0,1,1], 'ACEF', 'CEF'),
  446. ('ABCDEF', [0,0,0,0,0,0], '', ''),
  447. ('ABCDEF', [1,1,1,1,1,1], 'ABCDEF', 'BCDEF'),
  448. ('ABCDEF', [1,0,1], 'AC', 'C'),
  449. ('ABC', [0,1,1,1,1,1], 'BC', 'C'),
  450. ]:
  451. self.assertEqual(list(op(compress(data=data, selectors=selectors))), list(result1))
  452. self.assertEqual(list(op(compress(data, selectors))), list(result1))
  453. testIntermediate = compress(data, selectors)
  454. if result1:
  455. next(testIntermediate)
  456. self.assertEqual(list(op(testIntermediate)), list(result2))
  457. def test_count(self):
  458. self.assertEqual(lzip('abc',count()), [('a', 0), ('b', 1), ('c', 2)])
  459. self.assertEqual(lzip('abc',count(3)), [('a', 3), ('b', 4), ('c', 5)])
  460. self.assertEqual(take(2, lzip('abc',count(3))), [('a', 3), ('b', 4)])
  461. self.assertEqual(take(2, zip('abc',count(-1))), [('a', -1), ('b', 0)])
  462. self.assertEqual(take(2, zip('abc',count(-3))), [('a', -3), ('b', -2)])
  463. self.assertRaises(TypeError, count, 2, 3, 4)
  464. self.assertRaises(TypeError, count, 'a')
  465. self.assertEqual(take(10, count(maxsize-5)),
  466. list(range(maxsize-5, maxsize+5)))
  467. self.assertEqual(take(10, count(-maxsize-5)),
  468. list(range(-maxsize-5, -maxsize+5)))
  469. self.assertEqual(take(3, count(3.25)), [3.25, 4.25, 5.25])
  470. self.assertEqual(take(3, count(3.25-4j)), [3.25-4j, 4.25-4j, 5.25-4j])
  471. self.assertEqual(take(3, count(Decimal('1.1'))),
  472. [Decimal('1.1'), Decimal('2.1'), Decimal('3.1')])
  473. self.assertEqual(take(3, count(Fraction(2, 3))),
  474. [Fraction(2, 3), Fraction(5, 3), Fraction(8, 3)])
  475. BIGINT = 1<<1000
  476. self.assertEqual(take(3, count(BIGINT)), [BIGINT, BIGINT+1, BIGINT+2])
  477. c = count(3)
  478. self.assertEqual(repr(c), 'count(3)')
  479. next(c)
  480. self.assertEqual(repr(c), 'count(4)')
  481. c = count(-9)
  482. self.assertEqual(repr(c), 'count(-9)')
  483. next(c)
  484. self.assertEqual(next(c), -8)
  485. self.assertEqual(repr(count(10.25)), 'count(10.25)')
  486. self.assertEqual(repr(count(10.0)), 'count(10.0)')
  487. self.assertEqual(type(next(count(10.0))), float)
  488. for i in (-sys.maxsize-5, -sys.maxsize+5 ,-10, -1, 0, 10, sys.maxsize-5, sys.maxsize+5):
  489. # Test repr
  490. r1 = repr(count(i))
  491. r2 = 'count(%r)'.__mod__(i)
  492. self.assertEqual(r1, r2)
  493. # check copy, deepcopy, pickle
  494. for value in -3, 3, maxsize-5, maxsize+5:
  495. c = count(value)
  496. self.assertEqual(next(copy.copy(c)), value)
  497. self.assertEqual(next(copy.deepcopy(c)), value)
  498. for proto in range(pickle.HIGHEST_PROTOCOL + 1):
  499. self.pickletest(proto, count(value))
  500. #check proper internal error handling for large "step' sizes
  501. count(1, maxsize+5); sys.exc_info()
  502. def test_count_with_stride(self):
  503. self.assertEqual(lzip('abc',count(2,3)), [('a', 2), ('b', 5), ('c', 8)])
  504. self.assertEqual(lzip('abc',count(start=2,step=3)),
  505. [('a', 2), ('b', 5), ('c', 8)])
  506. self.assertEqual(lzip('abc',count(step=-1)),
  507. [('a', 0), ('b', -1), ('c', -2)])
  508. self.assertRaises(TypeError, count, 'a', 'b')
  509. self.assertEqual(lzip('abc',count(2,0)), [('a', 2), ('b', 2), ('c', 2)])
  510. self.assertEqual(lzip('abc',count(2,1)), [('a', 2), ('b', 3), ('c', 4)])
  511. self.assertEqual(lzip('abc',count(2,3)), [('a', 2), ('b', 5), ('c', 8)])
  512. self.assertEqual(take(20, count(maxsize-15, 3)), take(20, range(maxsize-15, maxsize+100, 3)))
  513. self.assertEqual(take(20, count(-maxsize-15, 3)), take(20, range(-maxsize-15,-maxsize+100, 3)))
  514. self.assertEqual(take(3, count(10, maxsize+5)),
  515. list(range(10, 10+3*(maxsize+5), maxsize+5)))
  516. self.assertEqual(take(3, count(2, 1.25)), [2, 3.25, 4.5])
  517. self.assertEqual(take(3, count(2, 3.25-4j)), [2, 5.25-4j, 8.5-8j])
  518. self.assertEqual(take(3, count(Decimal('1.1'), Decimal('.1'))),
  519. [Decimal('1.1'), Decimal('1.2'), Decimal('1.3')])
  520. self.assertEqual(take(3, count(Fraction(2,3), Fraction(1,7))),
  521. [Fraction(2,3), Fraction(17,21), Fraction(20,21)])
  522. BIGINT = 1<<1000
  523. self.assertEqual(take(3, count(step=BIGINT)), [0, BIGINT, 2*BIGINT])
  524. self.assertEqual(repr(take(3, count(10, 2.5))), repr([10, 12.5, 15.0]))
  525. c = count(3, 5)
  526. self.assertEqual(repr(c), 'count(3, 5)')
  527. next(c)
  528. self.assertEqual(repr(c), 'count(8, 5)')
  529. c = count(-9, 0)
  530. self.assertEqual(repr(c), 'count(-9, 0)')
  531. next(c)
  532. self.assertEqual(repr(c), 'count(-9, 0)')
  533. c = count(-9, -3)
  534. self.assertEqual(repr(c), 'count(-9, -3)')
  535. next(c)
  536. self.assertEqual(repr(c), 'count(-12, -3)')
  537. self.assertEqual(repr(c), 'count(-12, -3)')
  538. self.assertEqual(repr(count(10.5, 1.25)), 'count(10.5, 1.25)')
  539. self.assertEqual(repr(count(10.5, 1)), 'count(10.5)') # suppress step=1 when it's an int
  540. self.assertEqual(repr(count(10.5, 1.00)), 'count(10.5, 1.0)') # do show float values lilke 1.0
  541. self.assertEqual(repr(count(10, 1.00)), 'count(10, 1.0)')
  542. c = count(10, 1.0)
  543. self.assertEqual(type(next(c)), int)
  544. self.assertEqual(type(next(c)), float)
  545. for i in (-sys.maxsize-5, -sys.maxsize+5 ,-10, -1, 0, 10, sys.maxsize-5, sys.maxsize+5):
  546. for j in (-sys.maxsize-5, -sys.maxsize+5 ,-10, -1, 0, 1, 10, sys.maxsize-5, sys.maxsize+5):
  547. # Test repr
  548. r1 = repr(count(i, j))
  549. if j == 1:
  550. r2 = ('count(%r)' % i)
  551. else:
  552. r2 = ('count(%r, %r)' % (i, j))
  553. self.assertEqual(r1, r2)
  554. for proto in range(pickle.HIGHEST_PROTOCOL + 1):
  555. self.pickletest(proto, count(i, j))
  556. def test_cycle(self):
  557. self.assertEqual(take(10, cycle('abc')), list('abcabcabca'))
  558. self.assertEqual(list(cycle('')), [])
  559. self.assertRaises(TypeError, cycle)
  560. self.assertRaises(TypeError, cycle, 5)
  561. self.assertEqual(list(islice(cycle(gen3()),10)), [0,1,2,0,1,2,0,1,2,0])
  562. def test_cycle_copy_pickle(self):
  563. # check copy, deepcopy, pickle
  564. c = cycle('abc')
  565. self.assertEqual(next(c), 'a')
  566. #simple copy currently not supported, because __reduce__ returns
  567. #an internal iterator
  568. #self.assertEqual(take(10, copy.copy(c)), list('bcabcabcab'))
  569. self.assertEqual(take(10, copy.deepcopy(c)), list('bcabcabcab'))
  570. for proto in range(pickle.HIGHEST_PROTOCOL + 1):
  571. self.assertEqual(take(10, pickle.loads(pickle.dumps(c, proto))),
  572. list('bcabcabcab'))
  573. next(c)
  574. self.assertEqual(take(10, pickle.loads(pickle.dumps(c, proto))),
  575. list('cabcabcabc'))
  576. next(c)
  577. next(c)
  578. for proto in range(pickle.HIGHEST_PROTOCOL + 1):
  579. self.pickletest(proto, cycle('abc'))
  580. for proto in range(pickle.HIGHEST_PROTOCOL + 1):
  581. # test with partial consumed input iterable
  582. it = iter('abcde')
  583. c = cycle(it)
  584. _ = [next(c) for i in range(2)] # consume 2 of 5 inputs
  585. p = pickle.dumps(c, proto)
  586. d = pickle.loads(p) # rebuild the cycle object
  587. self.assertEqual(take(20, d), list('cdeabcdeabcdeabcdeab'))
  588. # test with completely consumed input iterable
  589. it = iter('abcde')
  590. c = cycle(it)
  591. _ = [next(c) for i in range(7)] # consume 7 of 5 inputs
  592. p = pickle.dumps(c, proto)
  593. d = pickle.loads(p) # rebuild the cycle object
  594. self.assertEqual(take(20, d), list('cdeabcdeabcdeabcdeab'))
  595. def test_cycle_unpickle_compat(self):
  596. testcases = [
  597. b'citertools\ncycle\n(c__builtin__\niter\n((lI1\naI2\naI3\natRI1\nbtR((lI1\naI0\ntb.',
  598. b'citertools\ncycle\n(c__builtin__\niter\n(](K\x01K\x02K\x03etRK\x01btR(]K\x01aK\x00tb.',
  599. b'\x80\x02citertools\ncycle\nc__builtin__\niter\n](K\x01K\x02K\x03e\x85RK\x01b\x85R]K\x01aK\x00\x86b.',
  600. b'\x80\x03citertools\ncycle\ncbuiltins\niter\n](K\x01K\x02K\x03e\x85RK\x01b\x85R]K\x01aK\x00\x86b.',
  601. b'\x80\x04\x95=\x00\x00\x00\x00\x00\x00\x00\x8c\titertools\x8c\x05cycle\x93\x8c\x08builtins\x8c\x04iter\x93](K\x01K\x02K\x03e\x85RK\x01b\x85R]K\x01aK\x00\x86b.',
  602. b'citertools\ncycle\n(c__builtin__\niter\n((lp0\nI1\naI2\naI3\natRI1\nbtR(g0\nI1\ntb.',
  603. b'citertools\ncycle\n(c__builtin__\niter\n(]q\x00(K\x01K\x02K\x03etRK\x01btR(h\x00K\x01tb.',
  604. b'\x80\x02citertools\ncycle\nc__builtin__\niter\n]q\x00(K\x01K\x02K\x03e\x85RK\x01b\x85Rh\x00K\x01\x86b.',
  605. b'\x80\x03citertools\ncycle\ncbuiltins\niter\n]q\x00(K\x01K\x02K\x03e\x85RK\x01b\x85Rh\x00K\x01\x86b.',
  606. b'\x80\x04\x95<\x00\x00\x00\x00\x00\x00\x00\x8c\titertools\x8c\x05cycle\x93\x8c\x08builtins\x8c\x04iter\x93]\x94(K\x01K\x02K\x03e\x85RK\x01b\x85Rh\x00K\x01\x86b.',
  607. b'citertools\ncycle\n(c__builtin__\niter\n((lI1\naI2\naI3\natRI1\nbtR((lI1\naI00\ntb.',
  608. b'citertools\ncycle\n(c__builtin__\niter\n(](K\x01K\x02K\x03etRK\x01btR(]K\x01aI00\ntb.',
  609. b'\x80\x02citertools\ncycle\nc__builtin__\niter\n](K\x01K\x02K\x03e\x85RK\x01b\x85R]K\x01a\x89\x86b.',
  610. b'\x80\x03citertools\ncycle\ncbuiltins\niter\n](K\x01K\x02K\x03e\x85RK\x01b\x85R]K\x01a\x89\x86b.',
  611. b'\x80\x04\x95<\x00\x00\x00\x00\x00\x00\x00\x8c\titertools\x8c\x05cycle\x93\x8c\x08builtins\x8c\x04iter\x93](K\x01K\x02K\x03e\x85RK\x01b\x85R]K\x01a\x89\x86b.',
  612. b'citertools\ncycle\n(c__builtin__\niter\n((lp0\nI1\naI2\naI3\natRI1\nbtR(g0\nI01\ntb.',
  613. b'citertools\ncycle\n(c__builtin__\niter\n(]q\x00(K\x01K\x02K\x03etRK\x01btR(h\x00I01\ntb.',
  614. b'\x80\x02citertools\ncycle\nc__builtin__\niter\n]q\x00(K\x01K\x02K\x03e\x85RK\x01b\x85Rh\x00\x88\x86b.',
  615. b'\x80\x03citertools\ncycle\ncbuiltins\niter\n]q\x00(K\x01K\x02K\x03e\x85RK\x01b\x85Rh\x00\x88\x86b.',
  616. b'\x80\x04\x95;\x00\x00\x00\x00\x00\x00\x00\x8c\titertools\x8c\x05cycle\x93\x8c\x08builtins\x8c\x04iter\x93]\x94(K\x01K\x02K\x03e\x85RK\x01b\x85Rh\x00\x88\x86b.',
  617. ]
  618. assert len(testcases) == 20
  619. for t in testcases:
  620. it = pickle.loads(t)
  621. self.assertEqual(take(10, it), [2, 3, 1, 2, 3, 1, 2, 3, 1, 2])
  622. def test_cycle_setstate(self):
  623. # Verify both modes for restoring state
  624. # Mode 0 is efficient. It uses an incompletely consumed input
  625. # iterator to build a cycle object and then passes in state with
  626. # a list of previously consumed values. There is no data
  627. # overlap between the two.
  628. c = cycle('defg')
  629. c.__setstate__((list('abc'), 0))
  630. self.assertEqual(take(20, c), list('defgabcdefgabcdefgab'))
  631. # Mode 1 is inefficient. It starts with a cycle object built
  632. # from an iterator over the remaining elements in a partial
  633. # cycle and then passes in state with all of the previously
  634. # seen values (this overlaps values included in the iterator).
  635. c = cycle('defg')
  636. c.__setstate__((list('abcdefg'), 1))
  637. self.assertEqual(take(20, c), list('defgabcdefgabcdefgab'))
  638. # The first argument to setstate needs to be a tuple
  639. with self.assertRaises(TypeError):
  640. cycle('defg').__setstate__([list('abcdefg'), 0])
  641. # The first argument in the setstate tuple must be a list
  642. with self.assertRaises(TypeError):
  643. c = cycle('defg')
  644. c.__setstate__((tuple('defg'), 0))
  645. take(20, c)
  646. # The second argument in the setstate tuple must be an int
  647. with self.assertRaises(TypeError):
  648. cycle('defg').__setstate__((list('abcdefg'), 'x'))
  649. self.assertRaises(TypeError, cycle('').__setstate__, ())
  650. self.assertRaises(TypeError, cycle('').__setstate__, ([],))
  651. def test_groupby(self):
  652. # Check whether it accepts arguments correctly
  653. self.assertEqual([], list(groupby([])))
  654. self.assertEqual([], list(groupby([], key=id)))
  655. self.assertRaises(TypeError, list, groupby('abc', []))
  656. self.assertRaises(TypeError, groupby, None)
  657. self.assertRaises(TypeError, groupby, 'abc', lambda x:x, 10)
  658. # Check normal input
  659. s = [(0, 10, 20), (0, 11,21), (0,12,21), (1,13,21), (1,14,22),
  660. (2,15,22), (3,16,23), (3,17,23)]
  661. dup = []
  662. for k, g in groupby(s, lambda r:r[0]):
  663. for elem in g:
  664. self.assertEqual(k, elem[0])
  665. dup.append(elem)
  666. self.assertEqual(s, dup)
  667. # Check normal pickled
  668. for proto in range(pickle.HIGHEST_PROTOCOL + 1):
  669. dup = []
  670. for k, g in pickle.loads(pickle.dumps(groupby(s, testR), proto)):
  671. for elem in g:
  672. self.assertEqual(k, elem[0])
  673. dup.append(elem)
  674. self.assertEqual(s, dup)
  675. # Check nested case
  676. dup = []
  677. for k, g in groupby(s, testR):
  678. for ik, ig in groupby(g, testR2):
  679. for elem in ig:
  680. self.assertEqual(k, elem[0])
  681. self.assertEqual(ik, elem[2])
  682. dup.append(elem)
  683. self.assertEqual(s, dup)
  684. # Check nested and pickled
  685. for proto in range(pickle.HIGHEST_PROTOCOL + 1):
  686. dup = []
  687. for k, g in pickle.loads(pickle.dumps(groupby(s, testR), proto)):
  688. for ik, ig in pickle.loads(pickle.dumps(groupby(g, testR2), proto)):
  689. for elem in ig:
  690. self.assertEqual(k, elem[0])
  691. self.assertEqual(ik, elem[2])
  692. dup.append(elem)
  693. self.assertEqual(s, dup)
  694. # Check case where inner iterator is not used
  695. keys = [k for k, g in groupby(s, testR)]
  696. expectedkeys = set([r[0] for r in s])
  697. self.assertEqual(set(keys), expectedkeys)
  698. self.assertEqual(len(keys), len(expectedkeys))
  699. # Check case where inner iterator is used after advancing the groupby
  700. # iterator
  701. s = list(zip('AABBBAAAA', range(9)))
  702. it = groupby(s, testR)
  703. _, g1 = next(it)
  704. _, g2 = next(it)
  705. _, g3 = next(it)
  706. self.assertEqual(list(g1), [])
  707. self.assertEqual(list(g2), [])
  708. self.assertEqual(next(g3), ('A', 5))
  709. list(it) # exhaust the groupby iterator
  710. self.assertEqual(list(g3), [])
  711. for proto in range(pickle.HIGHEST_PROTOCOL + 1):
  712. it = groupby(s, testR)
  713. _, g = next(it)
  714. next(it)
  715. next(it)
  716. self.assertEqual(list(pickle.loads(pickle.dumps(g, proto))), [])
  717. # Exercise pipes and filters style
  718. s = 'abracadabra'
  719. # sort s | uniq
  720. r = [k for k, g in groupby(sorted(s))]
  721. self.assertEqual(r, ['a', 'b', 'c', 'd', 'r'])
  722. # sort s | uniq -d
  723. r = [k for k, g in groupby(sorted(s)) if list(islice(g,1,2))]
  724. self.assertEqual(r, ['a', 'b', 'r'])
  725. # sort s | uniq -c
  726. r = [(len(list(g)), k) for k, g in groupby(sorted(s))]
  727. self.assertEqual(r, [(5, 'a'), (2, 'b'), (1, 'c'), (1, 'd'), (2, 'r')])
  728. # sort s | uniq -c | sort -rn | head -3
  729. r = sorted([(len(list(g)) , k) for k, g in groupby(sorted(s))], reverse=True)[:3]
  730. self.assertEqual(r, [(5, 'a'), (2, 'r'), (2, 'b')])
  731. # iter.__next__ failure
  732. class ExpectedError(Exception):
  733. pass
  734. def delayed_raise(n=0):
  735. for i in range(n):
  736. yield 'yo'
  737. raise ExpectedError
  738. def gulp(iterable, keyp=None, func=list):
  739. return [func(g) for k, g in groupby(iterable, keyp)]
  740. # iter.__next__ failure on outer object
  741. self.assertRaises(ExpectedError, gulp, delayed_raise(0))
  742. # iter.__next__ failure on inner object
  743. self.assertRaises(ExpectedError, gulp, delayed_raise(1))
  744. # __eq__ failure
  745. class DummyCmp:
  746. def __eq__(self, dst):
  747. raise ExpectedError
  748. s = [DummyCmp(), DummyCmp(), None]
  749. # __eq__ failure on outer object
  750. self.assertRaises(ExpectedError, gulp, s, func=id)
  751. # __eq__ failure on inner object
  752. self.assertRaises(ExpectedError, gulp, s)
  753. # keyfunc failure
  754. def keyfunc(obj):
  755. if keyfunc.skip > 0:
  756. keyfunc.skip -= 1
  757. return obj
  758. else:
  759. raise ExpectedError
  760. # keyfunc failure on outer object
  761. keyfunc.skip = 0
  762. self.assertRaises(ExpectedError, gulp, [None], keyfunc)
  763. keyfunc.skip = 1
  764. self.assertRaises(ExpectedError, gulp, [None, None], keyfunc)
  765. def test_filter(self):
  766. self.assertEqual(list(filter(isEven, range(6))), [0,2,4])
  767. self.assertEqual(list(filter(None, [0,1,0,2,0])), [1,2])
  768. self.assertEqual(list(filter(bool, [0,1,0,2,0])), [1,2])
  769. self.assertEqual(take(4, filter(isEven, count())), [0,2,4,6])
  770. self.assertRaises(TypeError, filter)
  771. self.assertRaises(TypeError, filter, lambda x:x)
  772. self.assertRaises(TypeError, filter, lambda x:x, range(6), 7)
  773. self.assertRaises(TypeError, filter, isEven, 3)
  774. self.assertRaises(TypeError, next, filter(range(6), range(6)))
  775. # check copy, deepcopy, pickle
  776. ans = [0,2,4]
  777. c = filter(isEven, range(6))
  778. self.assertEqual(list(copy.copy(c)), ans)
  779. c = filter(isEven, range(6))
  780. self.assertEqual(list(copy.deepcopy(c)), ans)
  781. for proto in range(pickle.HIGHEST_PROTOCOL + 1):
  782. c = filter(isEven, range(6))
  783. self.assertEqual(list(pickle.loads(pickle.dumps(c, proto))), ans)
  784. next(c)
  785. self.assertEqual(list(pickle.loads(pickle.dumps(c, proto))), ans[1:])
  786. for proto in range(pickle.HIGHEST_PROTOCOL + 1):
  787. c = filter(isEven, range(6))
  788. self.pickletest(proto, c)
  789. def test_filterfalse(self):
  790. self.assertEqual(list(filterfalse(isEven, range(6))), [1,3,5])
  791. self.assertEqual(list(filterfalse(None, [0,1,0,2,0])), [0,0,0])
  792. self.assertEqual(list(filterfalse(bool, [0,1,0,2,0])), [0,0,0])
  793. self.assertEqual(take(4, filterfalse(isEven, count())), [1,3,5,7])
  794. self.assertRaises(TypeError, filterfalse)
  795. self.assertRaises(TypeError, filterfalse, lambda x:x)
  796. self.assertRaises(TypeError, filterfalse, lambda x:x, range(6), 7)
  797. self.assertRaises(TypeError, filterfalse, isEven, 3)
  798. self.assertRaises(TypeError, next, filterfalse(range(6), range(6)))
  799. for proto in range(pickle.HIGHEST_PROTOCOL + 1):
  800. self.pickletest(proto, filterfalse(isEven, range(6)))
  801. def test_zip(self):
  802. # XXX This is rather silly now that builtin zip() calls zip()...
  803. ans = [(x,y) for x, y in zip('abc',count())]
  804. self.assertEqual(ans, [('a', 0), ('b', 1), ('c', 2)])
  805. self.assertEqual(list(zip('abc', range(6))), lzip('abc', range(6)))
  806. self.assertEqual(list(zip('abcdef', range(3))), lzip('abcdef', range(3)))
  807. self.assertEqual(take(3,zip('abcdef', count())), lzip('abcdef', range(3)))
  808. self.assertEqual(list(zip('abcdef')), lzip('abcdef'))
  809. self.assertEqual(list(zip()), lzip())
  810. self.assertRaises(TypeError, zip, 3)
  811. self.assertRaises(TypeError, zip, range(3), 3)
  812. self.assertEqual([tuple(list(pair)) for pair in zip('abc', 'def')],
  813. lzip('abc', 'def'))
  814. self.assertEqual([pair for pair in zip('abc', 'def')],
  815. lzip('abc', 'def'))
  816. @support.impl_detail("tuple reuse is specific to CPython")
  817. def test_zip_tuple_reuse(self):
  818. ids = list(map(id, zip('abc', 'def')))
  819. self.assertEqual(min(ids), max(ids))
  820. ids = list(map(id, list(zip('abc', 'def'))))
  821. self.assertEqual(len(dict.fromkeys(ids)), len(ids))
  822. # check copy, deepcopy, pickle
  823. ans = [(x,y) for x, y in copy.copy(zip('abc',count()))]
  824. self.assertEqual(ans, [('a', 0), ('b', 1), ('c', 2)])
  825. ans = [(x,y) for x, y in copy.deepcopy(zip('abc',count()))]
  826. self.assertEqual(ans, [('a', 0), ('b', 1), ('c', 2)])
  827. for proto in range(pickle.HIGHEST_PROTOCOL + 1):
  828. ans = [(x,y) for x, y in pickle.loads(pickle.dumps(zip('abc',count()), proto))]
  829. self.assertEqual(ans, [('a', 0), ('b', 1), ('c', 2)])
  830. for proto in range(pickle.HIGHEST_PROTOCOL + 1):
  831. testIntermediate = zip('abc',count())
  832. next(testIntermediate)
  833. ans = [(x,y) for x, y in pickle.loads(pickle.dumps(testIntermediate, proto))]
  834. self.assertEqual(ans, [('b', 1), ('c', 2)])
  835. for proto in range(pickle.HIGHEST_PROTOCOL + 1):
  836. self.pickletest(proto, zip('abc', count()))
  837. def test_ziplongest(self):
  838. for args in [
  839. ['abc', range(6)],
  840. [range(6), 'abc'],
  841. [range(1000), range(2000,2100), range(3000,3050)],
  842. [range(1000), range(0), range(3000,3050), range(1200), range(1500)],
  843. [range(1000), range(0), range(3000,3050), range(1200), range(1500), range(0)],
  844. ]:
  845. target = [tuple([arg[i] if i < len(arg) else None for arg in args])
  846. for i in range(max(map(len, args)))]
  847. self.assertEqual(list(zip_longest(*args)), target)
  848. self.assertEqual(list(zip_longest(*args, **{})), target)
  849. target = [tuple((e is None and 'X' or e) for e in t) for t in target] # Replace None fills with 'X'
  850. self.assertEqual(list(zip_longest(*args, **dict(fillvalue='X'))), target)
  851. self.assertEqual(take(3,zip_longest('abcdef', count())), list(zip('abcdef', range(3)))) # take 3 from infinite input
  852. self.assertEqual(list(zip_longest()), list(zip()))
  853. self.assertEqual(list(zip_longest([])), list(zip([])))
  854. self.assertEqual(list(zip_longest('abcdef')), list(zip('abcdef')))
  855. self.assertEqual(list(zip_longest('abc', 'defg', **{})),
  856. list(zip(list('abc')+[None], 'defg'))) # empty keyword dict
  857. self.assertRaises(TypeError, zip_longest, 3)
  858. self.assertRaises(TypeError, zip_longest, range(3), 3)
  859. for stmt in [
  860. "zip_longest('abc', fv=1)",
  861. "zip_longest('abc', fillvalue=1, bogus_keyword=None)",
  862. ]:
  863. try:
  864. eval(stmt, globals(), locals())
  865. except TypeError:
  866. pass
  867. else:
  868. self.fail('Did not raise Type in: ' + stmt)
  869. self.assertEqual([tuple(list(pair)) for pair in zip_longest('abc', 'def')],
  870. list(zip('abc', 'def')))
  871. self.assertEqual([pair for pair in zip_longest('abc', 'def')],
  872. list(zip('abc', 'def')))
  873. @support.impl_detail("tuple reuse is specific to CPython")
  874. def test_zip_longest_tuple_reuse(self):
  875. ids = list(map(id, zip_longest('abc', 'def')))
  876. self.assertEqual(min(ids), max(ids))
  877. ids = list(map(id, list(zip_longest('abc', 'def'))))
  878. self.assertEqual(len(dict.fromkeys(ids)), len(ids))
  879. def test_zip_longest_pickling(self):
  880. for proto in range(pickle.HIGHEST_PROTOCOL + 1):
  881. self.pickletest(proto, zip_longest("abc", "def"))
  882. self.pickletest(proto, zip_longest("abc", "defgh"))
  883. self.pickletest(proto, zip_longest("abc", "defgh", fillvalue=1))
  884. self.pickletest(proto, zip_longest("", "defgh"))
  885. def test_zip_longest_bad_iterable(self):
  886. exception = TypeError()
  887. class BadIterable:
  888. def __iter__(self):
  889. raise exception
  890. with self.assertRaises(TypeError) as cm:
  891. zip_longest(BadIterable())
  892. self.assertIs(cm.exception, exception)
  893. def test_bug_7244(self):
  894. class Repeater:
  895. # this class is similar to itertools.repeat
  896. def __init__(self, o, t, e):
  897. self.o = o
  898. self.t = int(t)
  899. self.e = e
  900. def __iter__(self): # its iterator is itself
  901. return self
  902. def __next__(self):
  903. if self.t > 0:
  904. self.t -= 1
  905. return self.o
  906. else:
  907. raise self.e
  908. # Formerly this code in would fail in debug mode
  909. # with Undetected Error and Stop Iteration
  910. r1 = Repeater(1, 3, StopIteration)
  911. r2 = Repeater(2, 4, StopIteration)
  912. def run(r1, r2):
  913. result = []
  914. for i, j in zip_longest(r1, r2, fillvalue=0):
  915. with support.captured_output('stdout'):
  916. print((i, j))
  917. result.append((i, j))
  918. return result
  919. self.assertEqual(run(r1, r2), [(1,2), (1,2), (1,2), (0,2)])
  920. # Formerly, the RuntimeError would be lost
  921. # and StopIteration would stop as expected
  922. r1 = Repeater(1, 3, RuntimeError)
  923. r2 = Repeater(2, 4, StopIteration)
  924. it = zip_longest(r1, r2, fillvalue=0)
  925. self.assertEqual(next(it), (1, 2))
  926. self.assertEqual(next(it), (1, 2))
  927. self.assertEqual(next(it), (1, 2))
  928. self.assertRaises(RuntimeError, next, it)
  929. def test_pairwise(self):
  930. self.assertEqual(list(pairwise('')), [])
  931. self.assertEqual(list(pairwise('a')), [])
  932. self.assertEqual(list(pairwise('ab')),
  933. [('a', 'b')]),
  934. self.assertEqual(list(pairwise('abcde')),
  935. [('a', 'b'), ('b', 'c'), ('c', 'd'), ('d', 'e')])
  936. self.assertEqual(list(pairwise(range(10_000))),
  937. list(zip(range(10_000), range(1, 10_000))))
  938. with self.assertRaises(TypeError):
  939. pairwise() # too few arguments
  940. with self.assertRaises(TypeError):
  941. pairwise('abc', 10) # too many arguments
  942. with self.assertRaises(TypeError):
  943. pairwise(iterable='abc') # keyword arguments
  944. with self.assertRaises(TypeError):
  945. pairwise(None) # non-iterable argument
  946. def test_product(self):
  947. for args, result in [
  948. ([], [()]), # zero iterables
  949. (['ab'], [('a',), ('b',)]), # one iterable
  950. ([range(2), range(3)], [(0,0), (0,1), (0,2), (1,0), (1,1), (1,2)]), # two iterables
  951. ([range(0), range(2), range(3)], []), # first iterable with zero length
  952. ([range(2), range(0), range(3)], []), # middle iterable with zero length
  953. ([range(2), range(3), range(0)], []), # last iterable with zero length
  954. ]:
  955. self.assertEqual(list(product(*args)), result)
  956. for r in range(4):
  957. self.assertEqual(list(product(*(args*r))),
  958. list(product(*args, **dict(repeat=r))))
  959. self.assertEqual(len(list(product(*[range(7)]*6))), 7**6)
  960. self.assertRaises(TypeError, product, range(6), None)
  961. def product1(*args, **kwds):
  962. pools = list(map(tuple, args)) * kwds.get('repeat', 1)
  963. n = len(pools)
  964. if n == 0:
  965. yield ()
  966. return
  967. if any(len(pool) == 0 for pool in pools):
  968. return
  969. indices = [0] * n
  970. yield tuple(pool[i] for pool, i in zip(pools, indices))
  971. while 1:
  972. for i in reversed(range(n)): # right to left
  973. if indices[i] == len(pools[i]) - 1:
  974. continue
  975. indices[i] += 1
  976. for j in range(i+1, n):
  977. indices[j] = 0
  978. yield tuple(pool[i] for pool, i in zip(pools, indices))
  979. break
  980. else:
  981. return
  982. def product2(*args, **kwds):
  983. 'Pure python version used in docs'
  984. pools = list(map(tuple, args)) * kwds.get('repeat', 1)
  985. result = [[]]
  986. for pool in pools:
  987. result = [x+[y] for x in result for y in pool]
  988. for prod in result:
  989. yield tuple(prod)
  990. argtypes = ['', 'abc', '', range(0), range(4), dict(a=1, b=2, c=3),
  991. set('abcdefg'), range(11), tuple(range(13))]
  992. for i in range(100):
  993. args = [random.choice(argtypes) for j in range(random.randrange(5))]
  994. expected_len = prod(map(len, args))
  995. self.assertEqual(len(list(product(*args))), expected_len)
  996. self.assertEqual(list(product(*args)), list(product1(*args)))
  997. self.assertEqual(list(product(*args)), list(product2(*args)))
  998. args = map(iter, args)
  999. self.assertEqual(len(list(product(*args))), expected_len)
  1000. @support.bigaddrspacetest
  1001. def test_product_overflow(self):
  1002. with self.assertRaises((OverflowError, MemoryError)):
  1003. product(*(['ab']*2**5), repeat=2**25)
  1004. @support.impl_detail("tuple reuse is specific to CPython")
  1005. def test_product_tuple_reuse(self):
  1006. self.assertEqual(len(set(map(id, product('abc', 'def')))), 1)
  1007. self.assertNotEqual(len(set(map(id, list(product('abc', 'def'))))), 1)
  1008. def test_product_pickling(self):
  1009. # check copy, deepcopy, pickle
  1010. for args, result in [
  1011. ([], [()]), # zero iterables
  1012. (['ab'], [('a',), ('b',)]), # one iterable
  1013. ([range(2), range(3)], [(0,0), (0,1), (0,2), (1,0), (1,1), (1,2)]), # two iterables
  1014. ([range(0), range(2), range(3)], []), # first iterable with zero length
  1015. ([range(2), range(0), range(3)], []), # middle iterable with zero length
  1016. ([range(2), range(3), range(0)], []), # last iterable with zero length
  1017. ]:
  1018. self.assertEqual(list(copy.copy(product(*args))), result)
  1019. self.assertEqual(list(copy.deepcopy(product(*args))), result)
  1020. for proto in range(pickle.HIGHEST_PROTOCOL + 1):
  1021. self.pickletest(proto, product(*args))
  1022. def test_product_issue_25021(self):
  1023. # test that indices are properly clamped to the length of the tuples
  1024. p = product((1, 2),(3,))
  1025. p.__setstate__((0, 0x1000)) # will access tuple element 1 if not clamped
  1026. self.assertEqual(next(p), (2, 3))
  1027. # test that empty tuple in the list will result in an immediate StopIteration
  1028. p = product((1, 2), (), (3,))
  1029. p.__setstate__((0, 0, 0x1000)) # will access tuple element 1 if not clamped
  1030. self.assertRaises(StopIteration, next, p)
  1031. def test_repeat(self):
  1032. self.assertEqual(list(repeat(object='a', times=3)), ['a', 'a', 'a'])
  1033. self.assertEqual(lzip(range(3),repeat('a')),
  1034. [(0, 'a'), (1, 'a'), (2, 'a')])
  1035. self.assertEqual(list(repeat('a', 3)), ['a', 'a', 'a'])
  1036. self.assertEqual(take(3, repeat('a')), ['a', 'a', 'a'])
  1037. self.assertEqual(list(repeat('a', 0)), [])
  1038. self.assertEqual(list(repeat('a', -3)), [])
  1039. self.assertRaises(TypeError, repeat)
  1040. self.assertRaises(TypeError, repeat, None, 3, 4)
  1041. self.assertRaises(TypeError, repeat, None, 'a')
  1042. r = repeat(1+0j)
  1043. self.assertEqual(repr(r), 'repeat((1+0j))')
  1044. r = repeat(1+0j, 5)
  1045. self.assertEqual(repr(r), 'repeat((1+0j), 5)')
  1046. list(r)
  1047. self.assertEqual(repr(r), 'repeat((1+0j), 0)')
  1048. # check copy, deepcopy, pickle
  1049. c = repeat(object='a', times=10)
  1050. self.assertEqual(next(c), 'a')
  1051. self.assertEqual(take(2, copy.copy(c)), list('a' * 2))
  1052. self.assertEqual(take(2, copy.deepcopy(c)), list('a' * 2))
  1053. for proto in range(pickle.HIGHEST_PROTOCOL + 1):
  1054. self.pickletest(proto, repeat(object='a', times=10))
  1055. def test_repeat_with_negative_times(self):
  1056. self.assertEqual(repr(repeat('a', -1)), "repeat('a', 0)")
  1057. self.assertEqual(repr(repeat('a', -2)), "repeat('a', 0)")
  1058. self.assertEqual(repr(repeat('a', times=-1)), "repeat('a', 0)")
  1059. self.assertEqual(repr(repeat('a', times=-2)), "repeat('a', 0)")
  1060. def test_map(self):
  1061. self.assertEqual(list(map(operator.pow, range(3), range(1,7))),
  1062. [0**1, 1**2, 2**3])
  1063. self.assertEqual(list(map(tupleize, 'abc', range(5))),
  1064. [('a',0),('b',1),('c',2)])
  1065. self.assertEqual(list(map(tupleize, 'abc', count())),
  1066. [('a',0),('b',1),('c',2)])
  1067. self.assertEqual(take(2,map(tupleize, 'abc', count())),
  1068. [('a',0),('b',1)])
  1069. self.assertEqual(list(map(operator.pow, [])), [])
  1070. self.assertRaises(TypeError, map)
  1071. self.assertRaises(TypeError, list, map(None, range(3), range(3)))
  1072. self.assertRaises(TypeError, map, operator.neg)
  1073. self.assertRaises(TypeError, next, map(10, range(5)))
  1074. self.assertRaises(ValueError, next, map(errfunc, [4], [5]))
  1075. self.assertRaises(TypeError, next, map(onearg, [4], [5]))
  1076. # check copy, deepcopy, pickle
  1077. ans = [('a',0),('b',1),('c',2)]
  1078. c = map(tupleize, 'abc', count())
  1079. self.assertEqual(list(copy.copy(c)), ans)
  1080. c = map(tupleize, 'abc', count())
  1081. self.assertEqual(list(copy.deepcopy(c)), ans)
  1082. for proto in range(pickle.HIGHEST_PROTOCOL + 1):
  1083. c = map(tupleize, 'abc', count())
  1084. self.pickletest(proto, c)
  1085. def test_starmap(self):
  1086. self.assertEqual(list(starmap(operator.pow, zip(range(3), range(1,7)))),
  1087. [0**1, 1**2, 2**3])
  1088. self.assertEqual(take(3, starmap(operator.pow, zip(count(), count(1)))),
  1089. [0**1, 1**2, 2**3])
  1090. self.assertEqual(list(starmap(operator.pow, [])), [])
  1091. self.assertEqual(list(starmap(operator.pow, [iter([4,5])])), [4**5])
  1092. self.assertRaises(TypeError, list, starmap(operator.pow, [None]))
  1093. self.assertRaises(TypeError, starmap)
  1094. self.assertRaises(TypeError, starmap, operator.pow, [(4,5)], 'extra')
  1095. self.assertRaises(TypeError, next, starmap(10, [(4,5)]))
  1096. self.assertRaises(ValueError, next, starmap(errfunc, [(4,5)]))
  1097. self.assertRaises(TypeError, next, starmap(onearg, [(4,5)]))
  1098. # check copy, deepcopy, pickle
  1099. ans = [0**1, 1**2, 2**3]
  1100. c = starmap(operator.pow, zip(range(3), range(1,7)))
  1101. self.assertEqual(list(copy.copy(c)), ans)
  1102. c = starmap(operator.pow, zip(range(3), range(1,7)))
  1103. self.assertEqual(list(copy.deepcopy(c)), ans)
  1104. for proto in range(pickle.HIGHEST_PROTOCOL + 1):
  1105. c = starmap(operator.pow, zip(range(3), range(1,7)))
  1106. self.pickletest(proto, c)
  1107. def test_islice(self):
  1108. for args in [ # islice(args) should agree with range(args)
  1109. (10, 20, 3),
  1110. (10, 3, 20),
  1111. (10, 20),
  1112. (10, 10),
  1113. (10, 3),
  1114. (20,)
  1115. ]:
  1116. self.assertEqual(list(islice(range(100), *args)),
  1117. list(range(*args)))
  1118. for args, tgtargs in [ # Stop when seqn is exhausted
  1119. ((10, 110, 3), ((10, 100, 3))),
  1120. ((10, 110), ((10, 100))),
  1121. ((110,), (100,))
  1122. ]:
  1123. self.assertEqual(list(islice(range(100), *args)),
  1124. list(range(*tgtargs)))
  1125. # Test stop=None
  1126. self.assertEqual(list(islice(range(10), None)), list(range(10)))
  1127. self.assertEqual(list(islice(range(10), None, None)), list(range(10)))
  1128. self.assertEqual(list(islice(range(10), None, None, None)), list(range(10)))
  1129. self.assertEqual(list(islice(range(10), 2, None)), list(range(2, 10)))
  1130. self.assertEqual(list(islice(range(10), 1, None, 2)), list(range(1, 10, 2)))
  1131. # Test number of items consumed SF #1171417
  1132. it = iter(range(10))
  1133. self.assertEqual(list(islice(it, 3)), list(range(3)))
  1134. self.assertEqual(list(it), list(range(3, 10)))
  1135. it = iter(range(10))
  1136. self.assertEqual(list(islice(it, 3, 3)), [])
  1137. self.assertEqual(list(it), list(range(3, 10)))
  1138. # Test invalid arguments
  1139. ra = range(10)
  1140. self.assertRaises(TypeError, islice, ra)
  1141. self.assertRaises(TypeError, islice, ra, 1, 2, 3, 4)
  1142. self.assertRaises(ValueError, islice, ra, -5, 10, 1)
  1143. self.assertRaises(ValueError, islice, ra, 1, -5, -1)
  1144. self.assertRaises(ValueError, islice, ra, 1, 10, -1)
  1145. self.assertRaises(ValueError, islice, ra, 1, 10, 0)
  1146. self.assertRaises(ValueError, islice, ra, 'a')
  1147. self.assertRaises(ValueError, islice, ra, 'a', 1)
  1148. self.assertRaises(ValueError, islice, ra, 1, 'a')
  1149. self.assertRaises(ValueError, islice, ra, 'a', 1, 1)
  1150. self.assertRaises(ValueError, islice, ra, 1, 'a', 1)
  1151. self.assertEqual(len(list(islice(count(), 1, 10, maxsize))), 1)
  1152. # Issue #10323: Less islice in a predictable state
  1153. c = count()
  1154. self.assertEqual(list(islice(c, 1, 3, 50)), [1])
  1155. self.assertEqual(next(c), 3)
  1156. # check copy, deepcopy, pickle
  1157. for args in [ # islice(args) should agree with range(args)
  1158. (10, 20, 3),
  1159. (10, 3, 20),
  1160. (10, 20),
  1161. (10, 3),
  1162. (20,)
  1163. ]:
  1164. self.assertEqual(list(copy.copy(islice(range(100), *args))),
  1165. list(range(*args)))
  1166. self.assertEqual(list(copy.deepcopy(islice(range(100), *args))),
  1167. list(range(*args)))
  1168. for proto in range(pickle.HIGHEST_PROTOCOL + 1):
  1169. self.pickletest(proto, islice(range(100), *args))
  1170. # Issue #21321: check source iterator is not referenced
  1171. # from islice() after the latter has been exhausted
  1172. it = (x for x in (1, 2))
  1173. wr = weakref.ref(it)
  1174. it = islice(it, 1)
  1175. self.assertIsNotNone(wr())
  1176. list(it) # exhaust the iterator
  1177. support.gc_collect()
  1178. self.assertIsNone(wr())
  1179. # Issue #30537: islice can accept integer-like objects as
  1180. # arguments
  1181. class IntLike(object):
  1182. def __init__(self, val):
  1183. self.val = val
  1184. def __index__(self):
  1185. return self.val
  1186. self.assertEqual(list(islice(range(100), IntLike(10))), list(range(10)))
  1187. self.assertEqual(list(islice(range(100), IntLike(10), IntLike(50))),
  1188. list(range(10, 50)))
  1189. self.assertEqual(list(islice(range(100), IntLike(10), IntLike(50), IntLike(5))),
  1190. list(range(10,50,5)))
  1191. def test_takewhile(self):
  1192. data = [1, 3, 5, 20, 2, 4, 6, 8]
  1193. self.assertEqual(list(takewhile(underten, data)), [1, 3, 5])
  1194. self.assertEqual(list(takewhile(underten, [])), [])
  1195. self.assertRaises(TypeError, takewhile)
  1196. self.assertRaises(TypeError, takewhile, operator.pow)
  1197. self.assertRaises(TypeError, takewhile, operator.pow, [(4,5)], 'extra')
  1198. self.assertRaises(TypeError, next, takewhile(10, [(4,5)]))
  1199. self.assertRaises(ValueError, next, takewhile(errfunc, [(4,5)]))
  1200. t = takewhile(bool, [1, 1, 1, 0, 0, 0])
  1201. self.assertEqual(list(t), [1, 1, 1])
  1202. self.assertRaises(StopIteration, next, t)
  1203. # check copy, deepcopy, pickle
  1204. self.assertEqual(list(copy.copy(takewhile(underten, data))), [1, 3, 5])
  1205. self.assertEqual(list(copy.deepcopy(takewhile(underten, data))),
  1206. [1, 3, 5])
  1207. for proto in range(pickle.HIGHEST_PROTOCOL + 1):
  1208. self.pickletest(proto, takewhile(underten, data))
  1209. def test_dropwhile(self):
  1210. data = [1, 3, 5, 20, 2, 4, 6, 8]
  1211. self.assertEqual(list(dropwhile(underten, data)), [20, 2, 4, 6, 8])
  1212. self.assertEqual(list(dropwhile(underten, [])), [])
  1213. self.assertRaises(TypeError, dropwhile)
  1214. self.assertRaises(TypeError, dropwhile, operator.pow)
  1215. self.assertRaises(TypeError, dropwhile, operator.pow, [(4,5)], 'extra')
  1216. self.assertRaises(TypeError, next, dropwhile(10, [(4,5)]))
  1217. self.assertRaises(ValueError, next, dropwhile(errfunc, [(4,5)]))
  1218. # check copy, deepcopy, pickle
  1219. self.assertEqual(list(copy.copy(dropwhile(underten, data))), [20, 2, 4, 6, 8])
  1220. self.assertEqual(list(copy.deepcopy(dropwhile(underten, data))),
  1221. [20, 2, 4, 6, 8])
  1222. for proto in range(pickle.HIGHEST_PROTOCOL + 1):
  1223. self.pickletest(proto, dropwhile(underten, data))
  1224. def test_tee(self):
  1225. n = 200
  1226. a, b = tee([]) # test empty iterator
  1227. self.assertEqual(list(a), [])
  1228. self.assertEqual(list(b), [])
  1229. a, b = tee(irange(n)) # test 100% interleaved
  1230. self.assertEqual(lzip(a,b), lzip(range(n), range(n)))
  1231. a, b = tee(irange(n)) # test 0% interleaved
  1232. self.assertEqual(list(a), list(range(n)))
  1233. self.assertEqual(list(b), list(range(n)))
  1234. a, b = tee(irange(n)) # test dealloc of leading iterator
  1235. for i in range(100):
  1236. self.assertEqual(next(a), i)
  1237. del a
  1238. self.assertEqual(list(b), list(range(n)))
  1239. a, b = tee(irange(n)) # test dealloc of trailing iterator
  1240. for i in range(100):
  1241. self.assertEqual(next(a), i)
  1242. del b
  1243. self.assertEqual(list(a), list(range(100, n)))
  1244. for j in range(5): # test randomly interleaved
  1245. order = [0]*n + [1]*n
  1246. random.shuffle(order)
  1247. lists = ([], [])
  1248. its = tee(irange(n))
  1249. for i in order:
  1250. value = next(its[i])
  1251. lists[i].append(value)
  1252. self.assertEqual(lists[0], list(range(n)))
  1253. self.assertEqual(lists[1], list(range(n)))
  1254. # test argument format checking
  1255. self.assertRaises(TypeError, tee)
  1256. self.assertRaises(TypeError, tee, 3)
  1257. self.assertRaises(TypeError, tee, [1,2], 'x')
  1258. self.assertRaises(TypeError, tee, [1,2], 3, 'x')
  1259. # tee object should be instantiable
  1260. a, b = tee('abc')
  1261. c = type(a)('def')
  1262. self.assertEqual(list(c), list('def'))
  1263. # test long-lagged and multi-way split
  1264. a, b, c = tee(range(2000), 3)
  1265. for i in range(100):
  1266. self.assertEqual(next(a), i)
  1267. self.assertEqual(list(b), list(range(2000)))
  1268. self.assertEqual([next(c), next(c)], list(range(2)))
  1269. self.assertEqual(list(a), list(range(100,2000)))
  1270. self.assertEqual(list(c), list(range(2,2000)))
  1271. # test values of n
  1272. self.assertRaises(TypeError, tee, 'abc', 'invalid')
  1273. self.assertRaises(ValueError, tee, [], -1)
  1274. for n in range(5):
  1275. result = tee('abc', n)
  1276. self.assertEqual(type(result), tuple)
  1277. self.assertEqual(len(result), n)
  1278. self.assertEqual([list(x) for x in result], [list('abc')]*n)
  1279. # tee pass-through to copyable iterator
  1280. a, b = tee('abc')
  1281. c, d = tee(a)
  1282. self.assertTrue(a is c)
  1283. # test tee_new
  1284. t1, t2 = tee('abc')
  1285. tnew = type(t1)
  1286. self.assertRaises(TypeError, tnew)
  1287. self.assertRaises(TypeError, tnew, 10)
  1288. t3 = tnew(t1)
  1289. self.assertTrue(list(t1) == list(t2) == list(t3) == list('abc'))
  1290. # test that tee objects are weak referencable
  1291. a, b = tee(range(10))
  1292. p = weakref.proxy(a)
  1293. self.assertEqual(getattr(p, '__class__'), type(b))
  1294. del a
  1295. support.gc_collect() # For PyPy or other GCs.
  1296. self.assertRaises(ReferenceError, getattr, p, '__class__')
  1297. ans = list('abc')
  1298. long_ans = list(range(10000))
  1299. # check copy
  1300. a, b = tee('abc')
  1301. self.assertEqual(list(copy.copy(a)), ans)
  1302. self.assertEqual(list(copy.copy(b)), ans)
  1303. a, b = tee(list(range(10000)))
  1304. self.assertEqual(list(copy.copy(a)), long_ans)
  1305. self.assertEqual(list(copy.copy(b)), long_ans)
  1306. # check partially consumed copy
  1307. a, b = tee('abc')
  1308. take(2, a)
  1309. take(1, b)
  1310. self.assertEqual(list(copy.copy(a)), ans[2:])
  1311. self.assertEqual(list(copy.copy(b)), ans[1:])
  1312. self.assertEqual(list(a), ans[2:])
  1313. self.assertEqual(list(b), ans[1:])
  1314. a, b = tee(range(10000))
  1315. take(100, a)
  1316. take(60, b)
  1317. self.assertEqual(list(copy.copy(a)), long_ans[100:])
  1318. self.assertEqual(list(copy.copy(b)), long_ans[60:])
  1319. self.assertEqual(list(a), long_ans[100:])
  1320. self.assertEqual(list(b), long_ans[60:])
  1321. # check deepcopy
  1322. a, b = tee('abc')
  1323. self.assertEqual(list(copy.deepcopy(a)), ans)
  1324. self.assertEqual(list(copy.deepcopy(b)), ans)
  1325. self.assertEqual(list(a), ans)
  1326. self.assertEqual(list(b), ans)
  1327. a, b = tee(range(10000))
  1328. self.assertEqual(list(copy.deepcopy(a)), long_ans)
  1329. self.assertEqual(list(copy.deepcopy(b)), long_ans)
  1330. self.assertEqual(list(a), long_ans)
  1331. self.assertEqual(list(b), long_ans)
  1332. # check partially consumed deepcopy
  1333. a, b = tee('abc')
  1334. take(2, a)
  1335. take(1, b)
  1336. self.assertEqual(list(copy.deepcopy(a)), ans[2:])
  1337. self.assertEqual(list(copy.deepcopy(b)), ans[1:])
  1338. self.assertEqual(list(a), ans[2:])
  1339. self.assertEqual(list(b), ans[1:])
  1340. a, b = tee(range(10000))
  1341. take(100, a)
  1342. take(60, b)
  1343. self.assertEqual(list(copy.deepcopy(a)), long_ans[100:])
  1344. self.assertEqual(list(copy.deepcopy(b)), long_ans[60:])
  1345. self.assertEqual(list(a), long_ans[100:])
  1346. self.assertEqual(list(b), long_ans[60:])
  1347. # check pickle
  1348. for proto in range(pickle.HIGHEST_PROTOCOL + 1):
  1349. self.pickletest(proto, iter(tee('abc')))
  1350. a, b = tee('abc')
  1351. self.pickletest(proto, a, compare=ans)
  1352. self.pickletest(proto, b, compare=ans)
  1353. # Issue 13454: Crash when deleting backward iterator from tee()
  1354. def test_tee_del_backward(self):
  1355. forward, backward = tee(repeat(None, 20000000))
  1356. try:
  1357. any(forward) # exhaust the iterator
  1358. del backward
  1359. except:
  1360. del forward, backward
  1361. raise
  1362. def test_tee_reenter(self):
  1363. class I:
  1364. first = True
  1365. def __iter__(self):
  1366. return self
  1367. def __next__(self):
  1368. first = self.first
  1369. self.first = False
  1370. if first:
  1371. return next(b)
  1372. a, b = tee(I())
  1373. with self.assertRaisesRegex(RuntimeError, "tee"):
  1374. next(a)
  1375. @threading_helper.requires_working_threading()
  1376. def test_tee_concurrent(self):
  1377. start = threading.Event()
  1378. finish = threading.Event()
  1379. class I:
  1380. def __iter__(self):
  1381. return self
  1382. def __next__(self):
  1383. start.set()
  1384. finish.wait()
  1385. a, b = tee(I())
  1386. thread = threading.Thread(target=next, args=[a])
  1387. thread.start()
  1388. try:
  1389. start.wait()
  1390. with self.assertRaisesRegex(RuntimeError, "tee"):
  1391. next(b)
  1392. finally:
  1393. finish.set()
  1394. thread.join()
  1395. def test_StopIteration(self):
  1396. self.assertRaises(StopIteration, next, zip())
  1397. for f in (chain, cycle, zip, groupby):
  1398. self.assertRaises(StopIteration, next, f([]))
  1399. self.assertRaises(StopIteration, next, f(StopNow()))
  1400. self.assertRaises(StopIteration, next, islice([], None))
  1401. self.assertRaises(StopIteration, next, islice(StopNow(), None))
  1402. p, q = tee([])
  1403. self.assertRaises(StopIteration, next, p)
  1404. self.assertRaises(StopIteration, next, q)
  1405. p, q = tee(StopNow())
  1406. self.assertRaises(StopIteration, next, p)
  1407. self.assertRaises(StopIteration, next, q)
  1408. self.assertRaises(StopIteration, next, repeat(None, 0))
  1409. for f in (filter, filterfalse, map, takewhile, dropwhile, starmap):
  1410. self.assertRaises(StopIteration, next, f(lambda x:x, []))
  1411. self.assertRaises(StopIteration, next, f(lambda x:x, StopNow()))
  1412. @support.cpython_only
  1413. def test_combinations_result_gc(self):
  1414. # bpo-42536: combinations's tuple-reuse speed trick breaks the GC's
  1415. # assumptions about what can be untracked. Make sure we re-track result
  1416. # tuples whenever we reuse them.
  1417. it = combinations([None, []], 1)
  1418. next(it)
  1419. gc.collect()
  1420. # That GC collection probably untracked the recycled internal result
  1421. # tuple, which has the value (None,). Make sure it's re-tracked when
  1422. # it's mutated and returned from __next__:
  1423. self.assertTrue(gc.is_tracked(next(it)))
  1424. @support.cpython_only
  1425. def test_combinations_with_replacement_result_gc(self):
  1426. # Ditto for combinations_with_replacement.
  1427. it = combinations_with_replacement([None, []], 1)
  1428. next(it)
  1429. gc.collect()
  1430. self.assertTrue(gc.is_tracked(next(it)))
  1431. @support.cpython_only
  1432. def test_permutations_result_gc(self):
  1433. # Ditto for permutations.
  1434. it = permutations([None, []], 1)
  1435. next(it)
  1436. gc.collect()
  1437. self.assertTrue(gc.is_tracked(next(it)))
  1438. @support.cpython_only
  1439. def test_product_result_gc(self):
  1440. # Ditto for product.
  1441. it = product([None, []])
  1442. next(it)
  1443. gc.collect()
  1444. self.assertTrue(gc.is_tracked(next(it)))
  1445. @support.cpython_only
  1446. def test_zip_longest_result_gc(self):
  1447. # Ditto for zip_longest.
  1448. it = zip_longest([[]])
  1449. gc.collect()
  1450. self.assertTrue(gc.is_tracked(next(it)))
  1451. class TestExamples(unittest.TestCase):
  1452. def test_accumulate(self):
  1453. self.assertEqual(list(accumulate([1,2,3,4,5])), [1, 3, 6, 10, 15])
  1454. def test_accumulate_reducible(self):
  1455. # check copy, deepcopy, pickle
  1456. data = [1, 2, 3, 4, 5]
  1457. accumulated = [1, 3, 6, 10, 15]
  1458. for proto in range(pickle.HIGHEST_PROTOCOL + 1):
  1459. it = accumulate(data)
  1460. self.assertEqual(list(pickle.loads(pickle.dumps(it, proto))), accumulated[:])
  1461. self.assertEqual(next(it), 1)
  1462. self.assertEqual(list(pickle.loads(pickle.dumps(it, proto))), accumulated[1:])
  1463. it = accumulate(data)
  1464. self.assertEqual(next(it), 1)
  1465. self.assertEqual(list(copy.deepcopy(it)), accumulated[1:])
  1466. self.assertEqual(list(copy.copy(it)), accumulated[1:])
  1467. def test_accumulate_reducible_none(self):
  1468. # Issue #25718: total is None
  1469. it = accumulate([None, None, None], operator.is_)
  1470. self.assertEqual(next(it), None)
  1471. for proto in range(pickle.HIGHEST_PROTOCOL + 1):
  1472. it_copy = pickle.loads(pickle.dumps(it, proto))
  1473. self.assertEqual(list(it_copy), [True, False])
  1474. self.assertEqual(list(copy.deepcopy(it)), [True, False])
  1475. self.assertEqual(list(copy.copy(it)), [True, False])
  1476. def test_chain(self):
  1477. self.assertEqual(''.join(chain('ABC', 'DEF')), 'ABCDEF')
  1478. def test_chain_from_iterable(self):
  1479. self.assertEqual(''.join(chain.from_iterable(['ABC', 'DEF'])), 'ABCDEF')
  1480. def test_combinations(self):
  1481. self.assertEqual(list(combinations('ABCD', 2)),
  1482. [('A','B'), ('A','C'), ('A','D'), ('B','C'), ('B','D'), ('C','D')])
  1483. self.assertEqual(list(combinations(range(4), 3)),
  1484. [(0,1,2), (0,1,3), (0,2,3), (1,2,3)])
  1485. def test_combinations_with_replacement(self):
  1486. self.assertEqual(list(combinations_with_replacement('ABC', 2)),
  1487. [('A','A'), ('A','B'), ('A','C'), ('B','B'), ('B','C'), ('C','C')])
  1488. def test_compress(self):
  1489. self.assertEqual(list(compress('ABCDEF', [1,0,1,0,1,1])), list('ACEF'))
  1490. def test_count(self):
  1491. self.assertEqual(list(islice(count(10), 5)), [10, 11, 12, 13, 14])
  1492. def test_cycle(self):
  1493. self.assertEqual(list(islice(cycle('ABCD'), 12)), list('ABCDABCDABCD'))
  1494. def test_dropwhile(self):
  1495. self.assertEqual(list(dropwhile(lambda x: x<5, [1,4,6,4,1])), [6,4,1])
  1496. def test_groupby(self):
  1497. self.assertEqual([k for k, g in groupby('AAAABBBCCDAABBB')],
  1498. list('ABCDAB'))
  1499. self.assertEqual([(list(g)) for k, g in groupby('AAAABBBCCD')],
  1500. [list('AAAA'), list('BBB'), list('CC'), list('D')])
  1501. def test_filter(self):
  1502. self.assertEqual(list(filter(lambda x: x%2, range(10))), [1,3,5,7,9])
  1503. def test_filterfalse(self):
  1504. self.assertEqual(list(filterfalse(lambda x: x%2, range(10))), [0,2,4,6,8])
  1505. def test_map(self):
  1506. self.assertEqual(list(map(pow, (2,3,10), (5,2,3))), [32, 9, 1000])
  1507. def test_islice(self):
  1508. self.assertEqual(list(islice('ABCDEFG', 2)), list('AB'))
  1509. self.assertEqual(list(islice('ABCDEFG', 2, 4)), list('CD'))
  1510. self.assertEqual(list(islice('ABCDEFG', 2, None)), list('CDEFG'))
  1511. self.assertEqual(list(islice('ABCDEFG', 0, None, 2)), list('ACEG'))
  1512. def test_zip(self):
  1513. self.assertEqual(list(zip('ABCD', 'xy')), [('A', 'x'), ('B', 'y')])
  1514. def test_zip_longest(self):
  1515. self.assertEqual(list(zip_longest('ABCD', 'xy', fillvalue='-')),
  1516. [('A', 'x'), ('B', 'y'), ('C', '-'), ('D', '-')])
  1517. def test_permutations(self):
  1518. self.assertEqual(list(permutations('ABCD', 2)),
  1519. list(map(tuple, 'AB AC AD BA BC BD CA CB CD DA DB DC'.split())))
  1520. self.assertEqual(list(permutations(range(3))),
  1521. [(0,1,2), (0,2,1), (1,0,2), (1,2,0), (2,0,1), (2,1,0)])
  1522. def test_product(self):
  1523. self.assertEqual(list(product('ABCD', 'xy')),
  1524. list(map(tuple, 'Ax Ay Bx By Cx Cy Dx Dy'.split())))
  1525. self.assertEqual(list(product(range(2), repeat=3)),
  1526. [(0,0,0), (0,0,1), (0,1,0), (0,1,1),
  1527. (1,0,0), (1,0,1), (1,1,0), (1,1,1)])
  1528. def test_repeat(self):
  1529. self.assertEqual(list(repeat(10, 3)), [10, 10, 10])
  1530. def test_stapmap(self):
  1531. self.assertEqual(list(starmap(pow, [(2,5), (3,2), (10,3)])),
  1532. [32, 9, 1000])
  1533. def test_takewhile(self):
  1534. self.assertEqual(list(takewhile(lambda x: x<5, [1,4,6,4,1])), [1,4])
  1535. class TestPurePythonRoughEquivalents(unittest.TestCase):
  1536. @staticmethod
  1537. def islice(iterable, *args):
  1538. s = slice(*args)
  1539. start, stop, step = s.start or 0, s.stop or sys.maxsize, s.step or 1
  1540. it = iter(range(start, stop, step))
  1541. try:
  1542. nexti = next(it)
  1543. except StopIteration:
  1544. # Consume *iterable* up to the *start* position.
  1545. for i, element in zip(range(start), iterable):
  1546. pass
  1547. return
  1548. try:
  1549. for i, element in enumerate(iterable):
  1550. if i == nexti:
  1551. yield element
  1552. nexti = next(it)
  1553. except StopIteration:
  1554. # Consume to *stop*.
  1555. for i, element in zip(range(i + 1, stop), iterable):
  1556. pass
  1557. def test_islice_recipe(self):
  1558. self.assertEqual(list(self.islice('ABCDEFG', 2)), list('AB'))
  1559. self.assertEqual(list(self.islice('ABCDEFG', 2, 4)), list('CD'))
  1560. self.assertEqual(list(self.islice('ABCDEFG', 2, None)), list('CDEFG'))
  1561. self.assertEqual(list(self.islice('ABCDEFG', 0, None, 2)), list('ACEG'))
  1562. # Test items consumed.
  1563. it = iter(range(10))
  1564. self.assertEqual(list(self.islice(it, 3)), list(range(3)))
  1565. self.assertEqual(list(it), list(range(3, 10)))
  1566. it = iter(range(10))
  1567. self.assertEqual(list(self.islice(it, 3, 3)), [])
  1568. self.assertEqual(list(it), list(range(3, 10)))
  1569. # Test that slice finishes in predictable state.
  1570. c = count()
  1571. self.assertEqual(list(self.islice(c, 1, 3, 50)), [1])
  1572. self.assertEqual(next(c), 3)
  1573. class TestGC(unittest.TestCase):
  1574. def makecycle(self, iterator, container):
  1575. container.append(iterator)
  1576. next(iterator)
  1577. del container, iterator
  1578. def test_accumulate(self):
  1579. a = []
  1580. self.makecycle(accumulate([1,2,a,3]), a)
  1581. def test_chain(self):
  1582. a = []
  1583. self.makecycle(chain(a), a)
  1584. def test_chain_from_iterable(self):
  1585. a = []
  1586. self.makecycle(chain.from_iterable([a]), a)
  1587. def test_combinations(self):
  1588. a = []
  1589. self.makecycle(combinations([1,2,a,3], 3), a)
  1590. def test_combinations_with_replacement(self):
  1591. a = []
  1592. self.makecycle(combinations_with_replacement([1,2,a,3], 3), a)
  1593. def test_compress(self):
  1594. a = []
  1595. self.makecycle(compress('ABCDEF', [1,0,1,0,1,0]), a)
  1596. def test_count(self):
  1597. a = []
  1598. Int = type('Int', (int,), dict(x=a))
  1599. self.makecycle(count(Int(0), Int(1)), a)
  1600. def test_cycle(self):
  1601. a = []
  1602. self.makecycle(cycle([a]*2), a)
  1603. def test_dropwhile(self):
  1604. a = []
  1605. self.makecycle(dropwhile(bool, [0, a, a]), a)
  1606. def test_groupby(self):
  1607. a = []
  1608. self.makecycle(groupby([a]*2, lambda x:x), a)
  1609. def test_issue2246(self):
  1610. # Issue 2246 -- the _grouper iterator was not included in GC
  1611. n = 10
  1612. keyfunc = lambda x: x
  1613. for i, j in groupby(range(n), key=keyfunc):
  1614. keyfunc.__dict__.setdefault('x',[]).append(j)
  1615. def test_filter(self):
  1616. a = []
  1617. self.makecycle(filter(lambda x:True, [a]*2), a)
  1618. def test_filterfalse(self):
  1619. a = []
  1620. self.makecycle(filterfalse(lambda x:False, a), a)
  1621. def test_zip(self):
  1622. a = []
  1623. self.makecycle(zip([a]*2, [a]*3), a)
  1624. def test_zip_longest(self):
  1625. a = []
  1626. self.makecycle(zip_longest([a]*2, [a]*3), a)
  1627. b = [a, None]
  1628. self.makecycle(zip_longest([a]*2, [a]*3, fillvalue=b), a)
  1629. def test_map(self):
  1630. a = []
  1631. self.makecycle(map(lambda x:x, [a]*2), a)
  1632. def test_islice(self):
  1633. a = []
  1634. self.makecycle(islice([a]*2, None), a)
  1635. def test_pairwise(self):
  1636. a = []
  1637. self.makecycle(pairwise([a]*5), a)
  1638. def test_permutations(self):
  1639. a = []
  1640. self.makecycle(permutations([1,2,a,3], 3), a)
  1641. def test_product(self):
  1642. a = []
  1643. self.makecycle(product([1,2,a,3], repeat=3), a)
  1644. def test_repeat(self):
  1645. a = []
  1646. self.makecycle(repeat(a), a)
  1647. def test_starmap(self):
  1648. a = []
  1649. self.makecycle(starmap(lambda *t: t, [(a,a)]*2), a)
  1650. def test_takewhile(self):
  1651. a = []
  1652. self.makecycle(takewhile(bool, [1, 0, a, a]), a)
  1653. def R(seqn):
  1654. 'Regular generator'
  1655. for i in seqn:
  1656. yield i
  1657. class G:
  1658. 'Sequence using __getitem__'
  1659. def __init__(self, seqn):
  1660. self.seqn = seqn
  1661. def __getitem__(self, i):
  1662. return self.seqn[i]
  1663. class I:
  1664. 'Sequence using iterator protocol'
  1665. def __init__(self, seqn):
  1666. self.seqn = seqn
  1667. self.i = 0
  1668. def __iter__(self):
  1669. return self
  1670. def __next__(self):
  1671. if self.i >= len(self.seqn): raise StopIteration
  1672. v = self.seqn[self.i]
  1673. self.i += 1
  1674. return v
  1675. class Ig:
  1676. 'Sequence using iterator protocol defined with a generator'
  1677. def __init__(self, seqn):
  1678. self.seqn = seqn
  1679. self.i = 0
  1680. def __iter__(self):
  1681. for val in self.seqn:
  1682. yield val
  1683. class X:
  1684. 'Missing __getitem__ and __iter__'
  1685. def __init__(self, seqn):
  1686. self.seqn = seqn
  1687. self.i = 0
  1688. def __next__(self):
  1689. if self.i >= len(self.seqn): raise StopIteration
  1690. v = self.seqn[self.i]
  1691. self.i += 1
  1692. return v
  1693. class N:
  1694. 'Iterator missing __next__()'
  1695. def __init__(self, seqn):
  1696. self.seqn = seqn
  1697. self.i = 0
  1698. def __iter__(self):
  1699. return self
  1700. class E:
  1701. 'Test propagation of exceptions'
  1702. def __init__(self, seqn):
  1703. self.seqn = seqn
  1704. self.i = 0
  1705. def __iter__(self):
  1706. return self
  1707. def __next__(self):
  1708. 3 // 0
  1709. class S:
  1710. 'Test immediate stop'
  1711. def __init__(self, seqn):
  1712. pass
  1713. def __iter__(self):
  1714. return self
  1715. def __next__(self):
  1716. raise StopIteration
  1717. def L(seqn):
  1718. 'Test multiple tiers of iterators'
  1719. return chain(map(lambda x:x, R(Ig(G(seqn)))))
  1720. class TestVariousIteratorArgs(unittest.TestCase):
  1721. def test_accumulate(self):
  1722. s = [1,2,3,4,5]
  1723. r = [1,3,6,10,15]
  1724. n = len(s)
  1725. for g in (G, I, Ig, L, R):
  1726. self.assertEqual(list(accumulate(g(s))), r)
  1727. self.assertEqual(list(accumulate(S(s))), [])
  1728. self.assertRaises(TypeError, accumulate, X(s))
  1729. self.assertRaises(TypeError, accumulate, N(s))
  1730. self.assertRaises(ZeroDivisionError, list, accumulate(E(s)))
  1731. def test_chain(self):
  1732. for s in ("123", "", range(1000), ('do', 1.2), range(2000,2200,5)):
  1733. for g in (G, I, Ig, S, L, R):
  1734. self.assertEqual(list(chain(g(s))), list(g(s)))
  1735. self.assertEqual(list(chain(g(s), g(s))), list(g(s))+list(g(s)))
  1736. self.assertRaises(TypeError, list, chain(X(s)))
  1737. self.assertRaises(TypeError, list, chain(N(s)))
  1738. self.assertRaises(ZeroDivisionError, list, chain(E(s)))
  1739. def test_compress(self):
  1740. for s in ("123", "", range(1000), ('do', 1.2), range(2000,2200,5)):
  1741. n = len(s)
  1742. for g in (G, I, Ig, S, L, R):
  1743. self.assertEqual(list(compress(g(s), repeat(1))), list(g(s)))
  1744. self.assertRaises(TypeError, compress, X(s), repeat(1))
  1745. self.assertRaises(TypeError, compress, N(s), repeat(1))
  1746. self.assertRaises(ZeroDivisionError, list, compress(E(s), repeat(1)))
  1747. def test_product(self):
  1748. for s in ("123", "", range(1000), ('do', 1.2), range(2000,2200,5)):
  1749. self.assertRaises(TypeError, product, X(s))
  1750. self.assertRaises(TypeError, product, N(s))
  1751. self.assertRaises(ZeroDivisionError, product, E(s))
  1752. def test_cycle(self):
  1753. for s in ("123", "", range(1000), ('do', 1.2), range(2000,2200,5)):
  1754. for g in (G, I, Ig, S, L, R):
  1755. tgtlen = len(s) * 3
  1756. expected = list(g(s))*3
  1757. actual = list(islice(cycle(g(s)), tgtlen))
  1758. self.assertEqual(actual, expected)
  1759. self.assertRaises(TypeError, cycle, X(s))
  1760. self.assertRaises(TypeError, cycle, N(s))
  1761. self.assertRaises(ZeroDivisionError, list, cycle(E(s)))
  1762. def test_groupby(self):
  1763. for s in (range(10), range(0), range(1000), (7,11), range(2000,2200,5)):
  1764. for g in (G, I, Ig, S, L, R):
  1765. self.assertEqual([k for k, sb in groupby(g(s))], list(g(s)))
  1766. self.assertRaises(TypeError, groupby, X(s))
  1767. self.assertRaises(TypeError, groupby, N(s))
  1768. self.assertRaises(ZeroDivisionError, list, groupby(E(s)))
  1769. def test_filter(self):
  1770. for s in (range(10), range(0), range(1000), (7,11), range(2000,2200,5)):
  1771. for g in (G, I, Ig, S, L, R):
  1772. self.assertEqual(list(filter(isEven, g(s))),
  1773. [x for x in g(s) if isEven(x)])
  1774. self.assertRaises(TypeError, filter, isEven, X(s))
  1775. self.assertRaises(TypeError, filter, isEven, N(s))
  1776. self.assertRaises(ZeroDivisionError, list, filter(isEven, E(s)))
  1777. def test_filterfalse(self):
  1778. for s in (range(10), range(0), range(1000), (7,11), range(2000,2200,5)):
  1779. for g in (G, I, Ig, S, L, R):
  1780. self.assertEqual(list(filterfalse(isEven, g(s))),
  1781. [x for x in g(s) if isOdd(x)])
  1782. self.assertRaises(TypeError, filterfalse, isEven, X(s))
  1783. self.assertRaises(TypeError, filterfalse, isEven, N(s))
  1784. self.assertRaises(ZeroDivisionError, list, filterfalse(isEven, E(s)))
  1785. def test_zip(self):
  1786. for s in ("123", "", range(1000), ('do', 1.2), range(2000,2200,5)):
  1787. for g in (G, I, Ig, S, L, R):
  1788. self.assertEqual(list(zip(g(s))), lzip(g(s)))
  1789. self.assertEqual(list(zip(g(s), g(s))), lzip(g(s), g(s)))
  1790. self.assertRaises(TypeError, zip, X(s))
  1791. self.assertRaises(TypeError, zip, N(s))
  1792. self.assertRaises(ZeroDivisionError, list, zip(E(s)))
  1793. def test_ziplongest(self):
  1794. for s in ("123", "", range(1000), ('do', 1.2), range(2000,2200,5)):
  1795. for g in (G, I, Ig, S, L, R):
  1796. self.assertEqual(list(zip_longest(g(s))), list(zip(g(s))))
  1797. self.assertEqual(list(zip_longest(g(s), g(s))), list(zip(g(s), g(s))))
  1798. self.assertRaises(TypeError, zip_longest, X(s))
  1799. self.assertRaises(TypeError, zip_longest, N(s))
  1800. self.assertRaises(ZeroDivisionError, list, zip_longest(E(s)))
  1801. def test_map(self):
  1802. for s in (range(10), range(0), range(100), (7,11), range(20,50,5)):
  1803. for g in (G, I, Ig, S, L, R):
  1804. self.assertEqual(list(map(onearg, g(s))),
  1805. [onearg(x) for x in g(s)])
  1806. self.assertEqual(list(map(operator.pow, g(s), g(s))),
  1807. [x**x for x in g(s)])
  1808. self.assertRaises(TypeError, map, onearg, X(s))
  1809. self.assertRaises(TypeError, map, onearg, N(s))
  1810. self.assertRaises(ZeroDivisionError, list, map(onearg, E(s)))
  1811. def test_islice(self):
  1812. for s in ("12345", "", range(1000), ('do', 1.2), range(2000,2200,5)):
  1813. for g in (G, I, Ig, S, L, R):
  1814. self.assertEqual(list(islice(g(s),1,None,2)), list(g(s))[1::2])
  1815. self.assertRaises(TypeError, islice, X(s), 10)
  1816. self.assertRaises(TypeError, islice, N(s), 10)
  1817. self.assertRaises(ZeroDivisionError, list, islice(E(s), 10))
  1818. def test_pairwise(self):
  1819. for s in ("123", "", range(1000), ('do', 1.2), range(2000,2200,5)):
  1820. for g in (G, I, Ig, S, L, R):
  1821. seq = list(g(s))
  1822. expected = list(zip(seq, seq[1:]))
  1823. actual = list(pairwise(g(s)))
  1824. self.assertEqual(actual, expected)
  1825. self.assertRaises(TypeError, pairwise, X(s))
  1826. self.assertRaises(TypeError, pairwise, N(s))
  1827. self.assertRaises(ZeroDivisionError, list, pairwise(E(s)))
  1828. def test_starmap(self):
  1829. for s in (range(10), range(0), range(100), (7,11), range(20,50,5)):
  1830. for g in (G, I, Ig, S, L, R):
  1831. ss = lzip(s, s)
  1832. self.assertEqual(list(starmap(operator.pow, g(ss))),
  1833. [x**x for x in g(s)])
  1834. self.assertRaises(TypeError, starmap, operator.pow, X(ss))
  1835. self.assertRaises(TypeError, starmap, operator.pow, N(ss))
  1836. self.assertRaises(ZeroDivisionError, list, starmap(operator.pow, E(ss)))
  1837. def test_takewhile(self):
  1838. for s in (range(10), range(0), range(1000), (7,11), range(2000,2200,5)):
  1839. for g in (G, I, Ig, S, L, R):
  1840. tgt = []
  1841. for elem in g(s):
  1842. if not isEven(elem): break
  1843. tgt.append(elem)
  1844. self.assertEqual(list(takewhile(isEven, g(s))), tgt)
  1845. self.assertRaises(TypeError, takewhile, isEven, X(s))
  1846. self.assertRaises(TypeError, takewhile, isEven, N(s))
  1847. self.assertRaises(ZeroDivisionError, list, takewhile(isEven, E(s)))
  1848. def test_dropwhile(self):
  1849. for s in (range(10), range(0), range(1000), (7,11), range(2000,2200,5)):
  1850. for g in (G, I, Ig, S, L, R):
  1851. tgt = []
  1852. for elem in g(s):
  1853. if not tgt and isOdd(elem): continue
  1854. tgt.append(elem)
  1855. self.assertEqual(list(dropwhile(isOdd, g(s))), tgt)
  1856. self.assertRaises(TypeError, dropwhile, isOdd, X(s))
  1857. self.assertRaises(TypeError, dropwhile, isOdd, N(s))
  1858. self.assertRaises(ZeroDivisionError, list, dropwhile(isOdd, E(s)))
  1859. def test_tee(self):
  1860. for s in ("123", "", range(1000), ('do', 1.2), range(2000,2200,5)):
  1861. for g in (G, I, Ig, S, L, R):
  1862. it1, it2 = tee(g(s))
  1863. self.assertEqual(list(it1), list(g(s)))
  1864. self.assertEqual(list(it2), list(g(s)))
  1865. self.assertRaises(TypeError, tee, X(s))
  1866. self.assertRaises(TypeError, tee, N(s))
  1867. self.assertRaises(ZeroDivisionError, list, tee(E(s))[0])
  1868. class LengthTransparency(unittest.TestCase):
  1869. def test_repeat(self):
  1870. self.assertEqual(operator.length_hint(repeat(None, 50)), 50)
  1871. self.assertEqual(operator.length_hint(repeat(None, 0)), 0)
  1872. self.assertEqual(operator.length_hint(repeat(None), 12), 12)
  1873. def test_repeat_with_negative_times(self):
  1874. self.assertEqual(operator.length_hint(repeat(None, -1)), 0)
  1875. self.assertEqual(operator.length_hint(repeat(None, -2)), 0)
  1876. self.assertEqual(operator.length_hint(repeat(None, times=-1)), 0)
  1877. self.assertEqual(operator.length_hint(repeat(None, times=-2)), 0)
  1878. class RegressionTests(unittest.TestCase):
  1879. def test_sf_793826(self):
  1880. # Fix Armin Rigo's successful efforts to wreak havoc
  1881. def mutatingtuple(tuple1, f, tuple2):
  1882. # this builds a tuple t which is a copy of tuple1,
  1883. # then calls f(t), then mutates t to be equal to tuple2
  1884. # (needs len(tuple1) == len(tuple2)).
  1885. def g(value, first=[1]):
  1886. if first:
  1887. del first[:]
  1888. f(next(z))
  1889. return value
  1890. items = list(tuple2)
  1891. items[1:1] = list(tuple1)
  1892. gen = map(g, items)
  1893. z = zip(*[gen]*len(tuple1))
  1894. next(z)
  1895. def f(t):
  1896. global T
  1897. T = t
  1898. first[:] = list(T)
  1899. first = []
  1900. mutatingtuple((1,2,3), f, (4,5,6))
  1901. second = list(T)
  1902. self.assertEqual(first, second)
  1903. def test_sf_950057(self):
  1904. # Make sure that chain() and cycle() catch exceptions immediately
  1905. # rather than when shifting between input sources
  1906. def gen1():
  1907. hist.append(0)
  1908. yield 1
  1909. hist.append(1)
  1910. raise AssertionError
  1911. hist.append(2)
  1912. def gen2(x):
  1913. hist.append(3)
  1914. yield 2
  1915. hist.append(4)
  1916. hist = []
  1917. self.assertRaises(AssertionError, list, chain(gen1(), gen2(False)))
  1918. self.assertEqual(hist, [0,1])
  1919. hist = []
  1920. self.assertRaises(AssertionError, list, chain(gen1(), gen2(True)))
  1921. self.assertEqual(hist, [0,1])
  1922. hist = []
  1923. self.assertRaises(AssertionError, list, cycle(gen1()))
  1924. self.assertEqual(hist, [0,1])
  1925. @support.skip_if_pgo_task
  1926. def test_long_chain_of_empty_iterables(self):
  1927. # Make sure itertools.chain doesn't run into recursion limits when
  1928. # dealing with long chains of empty iterables. Even with a high
  1929. # number this would probably only fail in Py_DEBUG mode.
  1930. it = chain.from_iterable(() for unused in range(10000000))
  1931. with self.assertRaises(StopIteration):
  1932. next(it)
  1933. def test_issue30347_1(self):
  1934. def f(n):
  1935. if n == 5:
  1936. list(b)
  1937. return n != 6
  1938. for (k, b) in groupby(range(10), f):
  1939. list(b) # shouldn't crash
  1940. def test_issue30347_2(self):
  1941. class K:
  1942. def __init__(self, v):
  1943. pass
  1944. def __eq__(self, other):
  1945. nonlocal i
  1946. i += 1
  1947. if i == 1:
  1948. next(g, None)
  1949. return True
  1950. i = 0
  1951. g = next(groupby(range(10), K))[1]
  1952. for j in range(2):
  1953. next(g, None) # shouldn't crash
  1954. class SubclassWithKwargsTest(unittest.TestCase):
  1955. def test_keywords_in_subclass(self):
  1956. # count is not subclassable...
  1957. testcases = [
  1958. (repeat, (1, 2), [1, 1]),
  1959. (zip, ([1, 2], 'ab'), [(1, 'a'), (2, 'b')]),
  1960. (filter, (None, [0, 1]), [1]),
  1961. (filterfalse, (None, [0, 1]), [0]),
  1962. (chain, ([1, 2], [3, 4]), [1, 2, 3]),
  1963. (map, (str, [1, 2]), ['1', '2']),
  1964. (starmap, (operator.pow, ((2, 3), (3, 2))), [8, 9]),
  1965. (islice, ([1, 2, 3, 4], 1, 3), [2, 3]),
  1966. (takewhile, (isEven, [2, 3, 4]), [2]),
  1967. (dropwhile, (isEven, [2, 3, 4]), [3, 4]),
  1968. (cycle, ([1, 2],), [1, 2, 1]),
  1969. (compress, ('ABC', [1, 0, 1]), ['A', 'C']),
  1970. ]
  1971. for cls, args, result in testcases:
  1972. with self.subTest(cls):
  1973. class subclass(cls):
  1974. pass
  1975. u = subclass(*args)
  1976. self.assertIs(type(u), subclass)
  1977. self.assertEqual(list(islice(u, 0, 3)), result)
  1978. with self.assertRaises(TypeError):
  1979. subclass(*args, newarg=3)
  1980. for cls, args, result in testcases:
  1981. # Constructors of repeat, zip, compress accept keyword arguments.
  1982. # Their subclasses need overriding __new__ to support new
  1983. # keyword arguments.
  1984. if cls in [repeat, zip, compress]:
  1985. continue
  1986. with self.subTest(cls):
  1987. class subclass_with_init(cls):
  1988. def __init__(self, *args, newarg=None):
  1989. self.newarg = newarg
  1990. u = subclass_with_init(*args, newarg=3)
  1991. self.assertIs(type(u), subclass_with_init)
  1992. self.assertEqual(list(islice(u, 0, 3)), result)
  1993. self.assertEqual(u.newarg, 3)
  1994. for cls, args, result in testcases:
  1995. with self.subTest(cls):
  1996. class subclass_with_new(cls):
  1997. def __new__(cls, *args, newarg=None):
  1998. self = super().__new__(cls, *args)
  1999. self.newarg = newarg
  2000. return self
  2001. u = subclass_with_new(*args, newarg=3)
  2002. self.assertIs(type(u), subclass_with_new)
  2003. self.assertEqual(list(islice(u, 0, 3)), result)
  2004. self.assertEqual(u.newarg, 3)
  2005. @support.cpython_only
  2006. class SizeofTest(unittest.TestCase):
  2007. def setUp(self):
  2008. self.ssize_t = struct.calcsize('n')
  2009. check_sizeof = support.check_sizeof
  2010. def test_product_sizeof(self):
  2011. basesize = support.calcobjsize('3Pi')
  2012. check = self.check_sizeof
  2013. check(product('ab', '12'), basesize + 2 * self.ssize_t)
  2014. check(product(*(('abc',) * 10)), basesize + 10 * self.ssize_t)
  2015. def test_combinations_sizeof(self):
  2016. basesize = support.calcobjsize('3Pni')
  2017. check = self.check_sizeof
  2018. check(combinations('abcd', 3), basesize + 3 * self.ssize_t)
  2019. check(combinations(range(10), 4), basesize + 4 * self.ssize_t)
  2020. def test_combinations_with_replacement_sizeof(self):
  2021. cwr = combinations_with_replacement
  2022. basesize = support.calcobjsize('3Pni')
  2023. check = self.check_sizeof
  2024. check(cwr('abcd', 3), basesize + 3 * self.ssize_t)
  2025. check(cwr(range(10), 4), basesize + 4 * self.ssize_t)
  2026. def test_permutations_sizeof(self):
  2027. basesize = support.calcobjsize('4Pni')
  2028. check = self.check_sizeof
  2029. check(permutations('abcd'),
  2030. basesize + 4 * self.ssize_t + 4 * self.ssize_t)
  2031. check(permutations('abcd', 3),
  2032. basesize + 4 * self.ssize_t + 3 * self.ssize_t)
  2033. check(permutations('abcde', 3),
  2034. basesize + 5 * self.ssize_t + 3 * self.ssize_t)
  2035. check(permutations(range(10), 4),
  2036. basesize + 10 * self.ssize_t + 4 * self.ssize_t)
  2037. def load_tests(loader, tests, pattern):
  2038. tests.addTest(doctest.DocTestSuite())
  2039. return tests
  2040. if __name__ == "__main__":
  2041. unittest.main()