mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00

svn+ssh://pythondev@svn.python.org/python/branches/p3yk ................ r55326 | guido.van.rossum | 2007-05-14 15:07:35 -0700 (Mon, 14 May 2007) | 2 lines Don't use err.message, use err.args[0]. ................ r55327 | guido.van.rossum | 2007-05-14 15:11:37 -0700 (Mon, 14 May 2007) | 259 lines Merged revisions 54988-55226,55228-55323 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r54995 | neal.norwitz | 2007-04-26 23:45:32 -0700 (Thu, 26 Apr 2007) | 3 lines This gets the test working on Solaris. It seems a little hokey to me, but the test passed on Linux and Solaris, hopefully other platforms too. ........ r55002 | georg.brandl | 2007-04-27 12:20:00 -0700 (Fri, 27 Apr 2007) | 2 lines Version fix (bug #1708710) ........ r55021 | neal.norwitz | 2007-04-29 16:53:24 -0700 (Sun, 29 Apr 2007) | 1 line There really are some tests that are problematic. ........ r55024 | kristjan.jonsson | 2007-04-30 08:17:46 -0700 (Mon, 30 Apr 2007) | 1 line Complete revamp of PCBuild8 directory. Use subdirectories for each project under the main pcbuild solution. Now make extensive use of property sheets to simplify project configuration. x64 build fully supported, and the process for building PGO version (Profiler Guided Optimization) simplified. All projects are now present, except _ssl, which needs to be reimplemented. Also, some of the projects that require external libraries need extra work to fully compile on x64. ........ r55025 | thomas.heller | 2007-04-30 08:44:17 -0700 (Mon, 30 Apr 2007) | 4 lines Make sure to call PyErr_NoMemory() in several places where PyMem_Malloc() could potentially fail. Will backport to the release25-maint branch. ........ r55027 | thomas.heller | 2007-04-30 09:04:57 -0700 (Mon, 30 Apr 2007) | 8 lines When accessing the .value attribute of a c_wchar_p instance, and the instance does not point to a valid wchar_t zero-terminated string, raise a ValueError. c_char_p does this already. The ValueError message now contains the correct pointer address. Will backport to release25-maint. ........ r55036 | georg.brandl | 2007-04-30 23:04:11 -0700 (Mon, 30 Apr 2007) | 2 lines Bug #1710295: exceptions are now new-style classes. ........ r55038 | georg.brandl | 2007-04-30 23:08:15 -0700 (Mon, 30 Apr 2007) | 2 lines Patch #1710352: add missing public functions to locale.__all__. ........ r55041 | vinay.sajip | 2007-05-01 03:20:03 -0700 (Tue, 01 May 2007) | 1 line Added new optional credentials argument to SMTPHandler.__init__, and smtp.login() is now called in SMTPHandler.emit() if credentials are specified. ........ r55042 | vinay.sajip | 2007-05-01 03:21:45 -0700 (Tue, 01 May 2007) | 1 line Added documentation for new optional credentials argument to SMTPHandler.__init__(). ........ r55070 | neal.norwitz | 2007-05-01 21:47:55 -0700 (Tue, 01 May 2007) | 3 lines Stop using PyMem_FREE while the GIL is not held. For details see: http://mail.python.org/pipermail/python-dev/2007-May/072896.html ........ r55080 | armin.rigo | 2007-05-02 12:23:31 -0700 (Wed, 02 May 2007) | 6 lines Fix for #1303614 and #1174712: - __dict__ descriptor abuse for subclasses of built-in types - subclassing from both ModuleType and another built-in types Thanks zseil for the patch. ........ r55083 | georg.brandl | 2007-05-02 13:02:29 -0700 (Wed, 02 May 2007) | 3 lines Actually raise an exception before calling ast_error_finish. Triggers an assertion otherwise. ........ r55087 | neal.norwitz | 2007-05-02 23:47:18 -0700 (Wed, 02 May 2007) | 1 line Handle a couple of uncaught errors. This should be backported ........ r55090 | neal.norwitz | 2007-05-03 00:20:57 -0700 (Thu, 03 May 2007) | 4 lines Remove dead code. This code couldn't be reached because earlier in the function there is another check for z != Py_None. ........ r55092 | thomas.heller | 2007-05-03 05:02:08 -0700 (Thu, 03 May 2007) | 1 line Fix building _ctypes.pyd for x64 / Windows. ........ r55093 | thomas.heller | 2007-05-03 05:05:20 -0700 (Thu, 03 May 2007) | 1 line Don't truncate pointers to integers (on win64 platform). ........ r55094 | walter.doerwald | 2007-05-03 08:13:55 -0700 (Thu, 03 May 2007) | 3 lines Clarify the behaviour of PyUnicode_DecodeUTF16(): A BOM is only skipped in native order mode, and only if it's the first two bytes. ........ r55101 | kristjan.jonsson | 2007-05-03 13:04:53 -0700 (Thu, 03 May 2007) | 2 lines Fix pcbuild8 after recent overhaul: Added the version resource to python26.dll. Adjust stacksize to 2Mb and made large address aware for 32 bits, and set stacksize to 3Mb for 64 bits. Todo: Set .dll optimized load addresses, and side-by-side packaging of the python26.dll. ........ r55102 | kristjan.jonsson | 2007-05-03 13:09:56 -0700 (Thu, 03 May 2007) | 1 line Fix those parts in the testsuite that assumed that sys.maxint would cause overflow on x64. Now the testsuite is well behaved on that platform. ........ r55103 | kristjan.jonsson | 2007-05-03 13:27:03 -0700 (Thu, 03 May 2007) | 11 lines Fix problems in x64 build that were discovered by the testsuite: - Reenable modules on x64 that had been disabled aeons ago for Itanium. - Cleared up confusion about compilers for 64 bit windows. There is only Itanium and x64. Added macros MS_WINI64 and MS_WINX64 for those rare cases where it matters, such as the disabling of modules above. - Set target platform (_WIN32_WINNT and WINVER) to 0x0501 (XP) for x64, and 0x0400 (NT 4.0) otherwise, which are the targeted minimum platforms. - Fixed thread_nt.h. The emulated InterlockedCompareExchange function didn?\194?\180t work on x64, probaby due to the lack of a "volatile" specifier. Anyway, win95 is no longer a target platform. - Itertools module used wrong constant to check for overflow in count() - PyInt_AsSsize_t couldn't deal with attribute error when accessing the __long__ member. - PyLong_FromSsize_t() incorrectly specified that the operand were unsigned. With these changes, the x64 passes the testsuite, for those modules present. ........ r55107 | kristjan.jonsson | 2007-05-03 17:25:08 -0700 (Thu, 03 May 2007) | 1 line Revert compiler comment to AMD64 for x64/AMD64 builds. ........ r55115 | thomas.heller | 2007-05-04 00:14:39 -0700 (Fri, 04 May 2007) | 4 lines Fix some ctypes test crashes, when running with a debug Python version on win64 by using proper argtypes and restype function attributes. ........ r55117 | thomas.heller | 2007-05-04 01:20:41 -0700 (Fri, 04 May 2007) | 4 lines On 64-bit Windows, ffi_arg must be 8 bytes long. This fixes the remaining crashes in the ctypes tests, when functions return float or double types. ........ r55120 | kristjan.jonsson | 2007-05-04 08:48:15 -0700 (Fri, 04 May 2007) | 1 line Update the pcbuild8 solution. Straightened out the _ctypes project by using a .vsproj file and a masm64.rules file to avoid redundancy ........ r55121 | kristjan.jonsson | 2007-05-04 10:28:06 -0700 (Fri, 04 May 2007) | 1 line Minor fix of PCBuild8/_ctypes vcproj, moving include dir into the .vsprops file. ........ r55129 | thomas.heller | 2007-05-04 12:54:22 -0700 (Fri, 04 May 2007) | 3 lines Do not truncate 64-bit pointers to 32-bit integers. Fixes SF #1703286, will backport to release25-maint. ........ r55131 | thomas.heller | 2007-05-04 12:56:32 -0700 (Fri, 04 May 2007) | 1 line Oops, these tests do not run on Windows CE. ........ r55140 | brett.cannon | 2007-05-04 18:34:02 -0700 (Fri, 04 May 2007) | 2 lines Deprecate BaseException.message as per PEP 352. ........ r55154 | georg.brandl | 2007-05-05 11:55:37 -0700 (Sat, 05 May 2007) | 2 lines Bug #1713535: typo in logging example. ........ r55158 | vinay.sajip | 2007-05-06 10:53:37 -0700 (Sun, 06 May 2007) | 1 line Updates of recent changes to logging. ........ r55165 | neal.norwitz | 2007-05-07 00:02:26 -0700 (Mon, 07 May 2007) | 1 line Verify changes to the trunk go to the normal checkins list ........ r55169 | kristjan.jonsson | 2007-05-07 09:46:54 -0700 (Mon, 07 May 2007) | 1 line As per Armin Rigo's suggestion, remove special handing from intobject.c to deal with the peculiarities of classobject's implementation of the number protocol. The nb_long method of classobject now falls back to nb_int if there is no __long__ attribute present. ........ r55197 | collin.winter | 2007-05-08 21:14:36 -0700 (Tue, 08 May 2007) | 9 lines Fix a bug in test.test_support.open_urlresource(). If the call to requires() doesn't precede the filesystem check, we get the following situation: 1. ./python Lib/test/regrtest.py test_foo # test needs urlfetch, not enabled, so skipped 2. ./python Lib/test/regrtest.py -u urlfetch test_foo # test runs 3. ./python Lib/test/regrtest.py test_foo # test runs (!) By moving the call to requires() *before* the filesystem check, the fact that fetched files are cached on the local disk becomes an implementation detail, rather than a semantics-changing point of note. ........ r55198 | neal.norwitz | 2007-05-08 23:43:15 -0700 (Tue, 08 May 2007) | 1 line Add markup for True/False. Will backport ........ r55205 | walter.doerwald | 2007-05-09 11:10:47 -0700 (Wed, 09 May 2007) | 4 lines Backport checkin: Fix a segfault when b"" was passed to b2a_qp() -- it was using strchr() instead of memchr(). ........ r55241 | neal.norwitz | 2007-05-10 22:55:15 -0700 (Thu, 10 May 2007) | 6 lines Don't ever report a failure when the sum of the reference count differences are zero. This should help reduce the false positives. The message about references leaking is maintained to provide as much info as possible rather than simply suppressing the message at the source. ........ r55242 | neal.norwitz | 2007-05-10 23:23:01 -0700 (Thu, 10 May 2007) | 1 line Fix typo in docstring (the module is popen2, not 3). ........ r55244 | neal.norwitz | 2007-05-10 23:56:52 -0700 (Thu, 10 May 2007) | 1 line Remove trailing whitespace in docstring ........ r55245 | neal.norwitz | 2007-05-10 23:57:33 -0700 (Thu, 10 May 2007) | 1 line Deprecate os.popen* and popen2 module in favor of the subprocess module. ........ r55247 | neal.norwitz | 2007-05-11 00:13:30 -0700 (Fri, 11 May 2007) | 1 line Deprecate os.popen* and popen2 module in favor of the subprocess module. (forgot the doc) ........ r55253 | georg.brandl | 2007-05-11 02:41:37 -0700 (Fri, 11 May 2007) | 3 lines Remove an XXX that is unnecessary. ........ r55258 | georg.brandl | 2007-05-11 04:04:26 -0700 (Fri, 11 May 2007) | 2 lines Patch #1714700: clarify os.linesep vs. tfiles opened in text mode. (backport) ........ r55259 | georg.brandl | 2007-05-11 04:43:56 -0700 (Fri, 11 May 2007) | 2 lines Update DDJ link. ........ r55273 | raymond.hettinger | 2007-05-11 10:59:59 -0700 (Fri, 11 May 2007) | 1 line Better tests for posixpath.commonprefix ........ r55287 | georg.brandl | 2007-05-12 14:06:41 -0700 (Sat, 12 May 2007) | 2 lines Bug #1046945: document SWIG options of distutils. ........ r55290 | georg.brandl | 2007-05-13 01:04:07 -0700 (Sun, 13 May 2007) | 2 lines Add bz2 to content encodings. ........ r55297 | neal.norwitz | 2007-05-13 13:45:05 -0700 (Sun, 13 May 2007) | 3 lines Remove Amoeba doc which was removed in version 1.0! according to Misc/HISTORY. Hopefully Guido won't shed a tear. :-) ........ r55298 | neal.norwitz | 2007-05-13 13:54:19 -0700 (Sun, 13 May 2007) | 1 line Remove references to stdwin which was removed long ago. ........ r55299 | neal.norwitz | 2007-05-13 14:13:42 -0700 (Sun, 13 May 2007) | 3 lines Remove support for freebsd[23] which haven't been released since 2000 or earlier. http://www.freebsd.org/releases/index.html ........ r55320 | raymond.hettinger | 2007-05-14 13:52:31 -0700 (Mon, 14 May 2007) | 1 line Small speedup. ........ ................
1031 lines
36 KiB
Python
1031 lines
36 KiB
Python
import unittest
|
|
from test import test_support
|
|
from itertools import *
|
|
from weakref import proxy
|
|
import sys
|
|
import operator
|
|
import random
|
|
maxsize = test_support.MAX_Py_ssize_t
|
|
minsize = -maxsize-1
|
|
|
|
def lzip(*args):
|
|
return list(zip(*args))
|
|
|
|
def onearg(x):
|
|
'Test function of one argument'
|
|
return 2*x
|
|
|
|
def errfunc(*args):
|
|
'Test function that raises an error'
|
|
raise ValueError
|
|
|
|
def gen3():
|
|
'Non-restartable source sequence'
|
|
for i in (0, 1, 2):
|
|
yield i
|
|
|
|
def isEven(x):
|
|
'Test predicate'
|
|
return x%2==0
|
|
|
|
def isOdd(x):
|
|
'Test predicate'
|
|
return x%2==1
|
|
|
|
class StopNow:
|
|
'Class emulating an empty iterable.'
|
|
def __iter__(self):
|
|
return self
|
|
def __next__(self):
|
|
raise StopIteration
|
|
|
|
def take(n, seq):
|
|
'Convenience function for partially consuming a long of infinite iterable'
|
|
return list(islice(seq, n))
|
|
|
|
class TestBasicOps(unittest.TestCase):
|
|
def test_chain(self):
|
|
self.assertEqual(list(chain('abc', 'def')), list('abcdef'))
|
|
self.assertEqual(list(chain('abc')), list('abc'))
|
|
self.assertEqual(list(chain('')), [])
|
|
self.assertEqual(take(4, chain('abc', 'def')), list('abcd'))
|
|
self.assertRaises(TypeError, chain, 2, 3)
|
|
|
|
def test_count(self):
|
|
self.assertEqual(lzip('abc',count()), [('a', 0), ('b', 1), ('c', 2)])
|
|
self.assertEqual(lzip('abc',count(3)), [('a', 3), ('b', 4), ('c', 5)])
|
|
self.assertEqual(take(2, lzip('abc',count(3))), [('a', 3), ('b', 4)])
|
|
self.assertRaises(TypeError, count, 2, 3)
|
|
self.assertRaises(TypeError, count, 'a')
|
|
self.assertRaises(OverflowError, list, islice(count(maxsize-5), 10))
|
|
c = count(3)
|
|
self.assertEqual(repr(c), 'count(3)')
|
|
next(c)
|
|
self.assertEqual(repr(c), 'count(4)')
|
|
c = count(-9)
|
|
self.assertEqual(repr(c), 'count(-9)')
|
|
next(c)
|
|
self.assertEqual(next(c), -8)
|
|
|
|
def test_cycle(self):
|
|
self.assertEqual(take(10, cycle('abc')), list('abcabcabca'))
|
|
self.assertEqual(list(cycle('')), [])
|
|
self.assertRaises(TypeError, cycle)
|
|
self.assertRaises(TypeError, cycle, 5)
|
|
self.assertEqual(list(islice(cycle(gen3()),10)), [0,1,2,0,1,2,0,1,2,0])
|
|
|
|
def test_groupby(self):
|
|
# Check whether it accepts arguments correctly
|
|
self.assertEqual([], list(groupby([])))
|
|
self.assertEqual([], list(groupby([], key=id)))
|
|
self.assertRaises(TypeError, list, groupby('abc', []))
|
|
self.assertRaises(TypeError, groupby, None)
|
|
self.assertRaises(TypeError, groupby, 'abc', lambda x:x, 10)
|
|
|
|
# Check normal input
|
|
s = [(0, 10, 20), (0, 11,21), (0,12,21), (1,13,21), (1,14,22),
|
|
(2,15,22), (3,16,23), (3,17,23)]
|
|
dup = []
|
|
for k, g in groupby(s, lambda r:r[0]):
|
|
for elem in g:
|
|
self.assertEqual(k, elem[0])
|
|
dup.append(elem)
|
|
self.assertEqual(s, dup)
|
|
|
|
# Check nested case
|
|
dup = []
|
|
for k, g in groupby(s, lambda r:r[0]):
|
|
for ik, ig in groupby(g, lambda r:r[2]):
|
|
for elem in ig:
|
|
self.assertEqual(k, elem[0])
|
|
self.assertEqual(ik, elem[2])
|
|
dup.append(elem)
|
|
self.assertEqual(s, dup)
|
|
|
|
# Check case where inner iterator is not used
|
|
keys = [k for k, g in groupby(s, lambda r:r[0])]
|
|
expectedkeys = set([r[0] for r in s])
|
|
self.assertEqual(set(keys), expectedkeys)
|
|
self.assertEqual(len(keys), len(expectedkeys))
|
|
|
|
# Exercise pipes and filters style
|
|
s = 'abracadabra'
|
|
# sort s | uniq
|
|
r = [k for k, g in groupby(sorted(s))]
|
|
self.assertEqual(r, ['a', 'b', 'c', 'd', 'r'])
|
|
# sort s | uniq -d
|
|
r = [k for k, g in groupby(sorted(s)) if list(islice(g,1,2))]
|
|
self.assertEqual(r, ['a', 'b', 'r'])
|
|
# sort s | uniq -c
|
|
r = [(len(list(g)), k) for k, g in groupby(sorted(s))]
|
|
self.assertEqual(r, [(5, 'a'), (2, 'b'), (1, 'c'), (1, 'd'), (2, 'r')])
|
|
# sort s | uniq -c | sort -rn | head -3
|
|
r = sorted([(len(list(g)) , k) for k, g in groupby(sorted(s))], reverse=True)[:3]
|
|
self.assertEqual(r, [(5, 'a'), (2, 'r'), (2, 'b')])
|
|
|
|
# iter.__next__ failure
|
|
class ExpectedError(Exception):
|
|
pass
|
|
def delayed_raise(n=0):
|
|
for i in range(n):
|
|
yield 'yo'
|
|
raise ExpectedError
|
|
def gulp(iterable, keyp=None, func=list):
|
|
return [func(g) for k, g in groupby(iterable, keyp)]
|
|
|
|
# iter.__next__ failure on outer object
|
|
self.assertRaises(ExpectedError, gulp, delayed_raise(0))
|
|
# iter.__next__ failure on inner object
|
|
self.assertRaises(ExpectedError, gulp, delayed_raise(1))
|
|
|
|
# __cmp__ failure
|
|
class DummyCmp:
|
|
def __eq__(self, dst):
|
|
raise ExpectedError
|
|
s = [DummyCmp(), DummyCmp(), None]
|
|
|
|
# __eq__ failure on outer object
|
|
self.assertRaises(ExpectedError, gulp, s, func=id)
|
|
# __eq__ failure on inner object
|
|
self.assertRaises(ExpectedError, gulp, s)
|
|
|
|
# keyfunc failure
|
|
def keyfunc(obj):
|
|
if keyfunc.skip > 0:
|
|
keyfunc.skip -= 1
|
|
return obj
|
|
else:
|
|
raise ExpectedError
|
|
|
|
# keyfunc failure on outer object
|
|
keyfunc.skip = 0
|
|
self.assertRaises(ExpectedError, gulp, [None], keyfunc)
|
|
keyfunc.skip = 1
|
|
self.assertRaises(ExpectedError, gulp, [None, None], keyfunc)
|
|
|
|
def test_ifilter(self):
|
|
self.assertEqual(list(ifilter(isEven, range(6))), [0,2,4])
|
|
self.assertEqual(list(ifilter(None, [0,1,0,2,0])), [1,2])
|
|
self.assertEqual(take(4, ifilter(isEven, count())), [0,2,4,6])
|
|
self.assertRaises(TypeError, ifilter)
|
|
self.assertRaises(TypeError, ifilter, lambda x:x)
|
|
self.assertRaises(TypeError, ifilter, lambda x:x, range(6), 7)
|
|
self.assertRaises(TypeError, ifilter, isEven, 3)
|
|
self.assertRaises(TypeError, next, ifilter(range(6), range(6)))
|
|
|
|
def test_ifilterfalse(self):
|
|
self.assertEqual(list(ifilterfalse(isEven, range(6))), [1,3,5])
|
|
self.assertEqual(list(ifilterfalse(None, [0,1,0,2,0])), [0,0,0])
|
|
self.assertEqual(take(4, ifilterfalse(isEven, count())), [1,3,5,7])
|
|
self.assertRaises(TypeError, ifilterfalse)
|
|
self.assertRaises(TypeError, ifilterfalse, lambda x:x)
|
|
self.assertRaises(TypeError, ifilterfalse, lambda x:x, range(6), 7)
|
|
self.assertRaises(TypeError, ifilterfalse, isEven, 3)
|
|
self.assertRaises(TypeError, next, ifilterfalse(range(6), range(6)))
|
|
|
|
def test_izip(self):
|
|
# XXX This is rather silly now that builtin zip() calls izip()...
|
|
ans = [(x,y) for x, y in izip('abc',count())]
|
|
self.assertEqual(ans, [('a', 0), ('b', 1), ('c', 2)])
|
|
self.assertEqual(list(izip('abc', range(6))), lzip('abc', range(6)))
|
|
self.assertEqual(list(izip('abcdef', range(3))), lzip('abcdef', range(3)))
|
|
self.assertEqual(take(3,izip('abcdef', count())), lzip('abcdef', range(3)))
|
|
self.assertEqual(list(izip('abcdef')), lzip('abcdef'))
|
|
self.assertEqual(list(izip()), lzip())
|
|
self.assertRaises(TypeError, izip, 3)
|
|
self.assertRaises(TypeError, izip, range(3), 3)
|
|
# Check tuple re-use (implementation detail)
|
|
self.assertEqual([tuple(list(pair)) for pair in izip('abc', 'def')],
|
|
lzip('abc', 'def'))
|
|
self.assertEqual([pair for pair in izip('abc', 'def')],
|
|
lzip('abc', 'def'))
|
|
ids = map(id, izip('abc', 'def'))
|
|
self.assertEqual(min(ids), max(ids))
|
|
ids = map(id, list(izip('abc', 'def')))
|
|
self.assertEqual(len(dict.fromkeys(ids)), len(ids))
|
|
|
|
def test_iziplongest(self):
|
|
for args in [
|
|
['abc', range(6)],
|
|
[range(6), 'abc'],
|
|
[range(1000), range(2000,2100), range(3000,3050)],
|
|
[range(1000), range(0), range(3000,3050), range(1200), range(1500)],
|
|
[range(1000), range(0), range(3000,3050), range(1200), range(1500), range(0)],
|
|
]:
|
|
target = map(None, *args)
|
|
self.assertEqual(list(izip_longest(*args)), target)
|
|
self.assertEqual(list(izip_longest(*args, **{})), target)
|
|
target = [tuple((e is None and 'X' or e) for e in t) for t in target] # Replace None fills with 'X'
|
|
self.assertEqual(list(izip_longest(*args, **dict(fillvalue='X'))), target)
|
|
|
|
self.assertEqual(take(3,izip_longest('abcdef', count())), list(zip('abcdef', range(3)))) # take 3 from infinite input
|
|
|
|
self.assertEqual(list(izip_longest()), list(zip()))
|
|
self.assertEqual(list(izip_longest([])), list(zip([])))
|
|
self.assertEqual(list(izip_longest('abcdef')), list(zip('abcdef')))
|
|
|
|
self.assertEqual(list(izip_longest('abc', 'defg', **{})), map(None, 'abc', 'defg')) # empty keyword dict
|
|
self.assertRaises(TypeError, izip_longest, 3)
|
|
self.assertRaises(TypeError, izip_longest, range(3), 3)
|
|
|
|
for stmt in [
|
|
"izip_longest('abc', fv=1)",
|
|
"izip_longest('abc', fillvalue=1, bogus_keyword=None)",
|
|
]:
|
|
try:
|
|
eval(stmt, globals(), locals())
|
|
except TypeError:
|
|
pass
|
|
else:
|
|
self.fail('Did not raise Type in: ' + stmt)
|
|
|
|
# Check tuple re-use (implementation detail)
|
|
self.assertEqual([tuple(list(pair)) for pair in izip_longest('abc', 'def')],
|
|
list(zip('abc', 'def')))
|
|
self.assertEqual([pair for pair in izip_longest('abc', 'def')],
|
|
list(zip('abc', 'def')))
|
|
ids = map(id, izip_longest('abc', 'def'))
|
|
self.assertEqual(min(ids), max(ids))
|
|
ids = map(id, list(izip_longest('abc', 'def')))
|
|
self.assertEqual(len(dict.fromkeys(ids)), len(ids))
|
|
|
|
def test_repeat(self):
|
|
self.assertEqual(lzip(range(3),repeat('a')),
|
|
[(0, 'a'), (1, 'a'), (2, 'a')])
|
|
self.assertEqual(list(repeat('a', 3)), ['a', 'a', 'a'])
|
|
self.assertEqual(take(3, repeat('a')), ['a', 'a', 'a'])
|
|
self.assertEqual(list(repeat('a', 0)), [])
|
|
self.assertEqual(list(repeat('a', -3)), [])
|
|
self.assertRaises(TypeError, repeat)
|
|
self.assertRaises(TypeError, repeat, None, 3, 4)
|
|
self.assertRaises(TypeError, repeat, None, 'a')
|
|
r = repeat(1+0j)
|
|
self.assertEqual(repr(r), 'repeat((1+0j))')
|
|
r = repeat(1+0j, 5)
|
|
self.assertEqual(repr(r), 'repeat((1+0j), 5)')
|
|
list(r)
|
|
self.assertEqual(repr(r), 'repeat((1+0j), 0)')
|
|
|
|
def test_imap(self):
|
|
self.assertEqual(list(imap(operator.pow, range(3), range(1,7))),
|
|
[0**1, 1**2, 2**3])
|
|
self.assertEqual(list(imap(None, 'abc', range(5))),
|
|
[('a',0),('b',1),('c',2)])
|
|
self.assertEqual(list(imap(None, 'abc', count())),
|
|
[('a',0),('b',1),('c',2)])
|
|
self.assertEqual(take(2,imap(None, 'abc', count())),
|
|
[('a',0),('b',1)])
|
|
self.assertEqual(list(imap(operator.pow, [])), [])
|
|
self.assertRaises(TypeError, imap)
|
|
self.assertRaises(TypeError, imap, operator.neg)
|
|
self.assertRaises(TypeError, next, imap(10, range(5)))
|
|
self.assertRaises(ValueError, next, imap(errfunc, [4], [5]))
|
|
self.assertRaises(TypeError, next, imap(onearg, [4], [5]))
|
|
|
|
def test_starmap(self):
|
|
self.assertEqual(list(starmap(operator.pow, zip(range(3), range(1,7)))),
|
|
[0**1, 1**2, 2**3])
|
|
self.assertEqual(take(3, starmap(operator.pow, izip(count(), count(1)))),
|
|
[0**1, 1**2, 2**3])
|
|
self.assertEqual(list(starmap(operator.pow, [])), [])
|
|
self.assertRaises(TypeError, list, starmap(operator.pow, [[4,5]]))
|
|
self.assertRaises(TypeError, starmap)
|
|
self.assertRaises(TypeError, starmap, operator.pow, [(4,5)], 'extra')
|
|
self.assertRaises(TypeError, next, starmap(10, [(4,5)]))
|
|
self.assertRaises(ValueError, next, starmap(errfunc, [(4,5)]))
|
|
self.assertRaises(TypeError, next, starmap(onearg, [(4,5)]))
|
|
|
|
def test_islice(self):
|
|
for args in [ # islice(args) should agree with range(args)
|
|
(10, 20, 3),
|
|
(10, 3, 20),
|
|
(10, 20),
|
|
(10, 3),
|
|
(20,)
|
|
]:
|
|
self.assertEqual(list(islice(range(100), *args)),
|
|
list(range(*args)))
|
|
|
|
for args, tgtargs in [ # Stop when seqn is exhausted
|
|
((10, 110, 3), ((10, 100, 3))),
|
|
((10, 110), ((10, 100))),
|
|
((110,), (100,))
|
|
]:
|
|
self.assertEqual(list(islice(range(100), *args)),
|
|
list(range(*tgtargs)))
|
|
|
|
# Test stop=None
|
|
self.assertEqual(list(islice(range(10), None)), list(range(10)))
|
|
self.assertEqual(list(islice(range(10), None, None)), list(range(10)))
|
|
self.assertEqual(list(islice(range(10), None, None, None)), list(range(10)))
|
|
self.assertEqual(list(islice(range(10), 2, None)), list(range(2, 10)))
|
|
self.assertEqual(list(islice(range(10), 1, None, 2)), list(range(1, 10, 2)))
|
|
|
|
# Test number of items consumed SF #1171417
|
|
it = iter(range(10))
|
|
self.assertEqual(list(islice(it, 3)), list(range(3)))
|
|
self.assertEqual(list(it), list(range(3, 10)))
|
|
|
|
# Test invalid arguments
|
|
self.assertRaises(TypeError, islice, range(10))
|
|
self.assertRaises(TypeError, islice, range(10), 1, 2, 3, 4)
|
|
self.assertRaises(ValueError, islice, range(10), -5, 10, 1)
|
|
self.assertRaises(ValueError, islice, range(10), 1, -5, -1)
|
|
self.assertRaises(ValueError, islice, range(10), 1, 10, -1)
|
|
self.assertRaises(ValueError, islice, range(10), 1, 10, 0)
|
|
self.assertRaises(ValueError, islice, range(10), 'a')
|
|
self.assertRaises(ValueError, islice, range(10), 'a', 1)
|
|
self.assertRaises(ValueError, islice, range(10), 1, 'a')
|
|
self.assertRaises(ValueError, islice, range(10), 'a', 1, 1)
|
|
self.assertRaises(ValueError, islice, range(10), 1, 'a', 1)
|
|
self.assertEqual(len(list(islice(count(), 1, 10, maxsize))), 1)
|
|
|
|
def test_takewhile(self):
|
|
data = [1, 3, 5, 20, 2, 4, 6, 8]
|
|
underten = lambda x: x<10
|
|
self.assertEqual(list(takewhile(underten, data)), [1, 3, 5])
|
|
self.assertEqual(list(takewhile(underten, [])), [])
|
|
self.assertRaises(TypeError, takewhile)
|
|
self.assertRaises(TypeError, takewhile, operator.pow)
|
|
self.assertRaises(TypeError, takewhile, operator.pow, [(4,5)], 'extra')
|
|
self.assertRaises(TypeError, next, takewhile(10, [(4,5)]))
|
|
self.assertRaises(ValueError, next, takewhile(errfunc, [(4,5)]))
|
|
t = takewhile(bool, [1, 1, 1, 0, 0, 0])
|
|
self.assertEqual(list(t), [1, 1, 1])
|
|
self.assertRaises(StopIteration, next, t)
|
|
|
|
def test_dropwhile(self):
|
|
data = [1, 3, 5, 20, 2, 4, 6, 8]
|
|
underten = lambda x: x<10
|
|
self.assertEqual(list(dropwhile(underten, data)), [20, 2, 4, 6, 8])
|
|
self.assertEqual(list(dropwhile(underten, [])), [])
|
|
self.assertRaises(TypeError, dropwhile)
|
|
self.assertRaises(TypeError, dropwhile, operator.pow)
|
|
self.assertRaises(TypeError, dropwhile, operator.pow, [(4,5)], 'extra')
|
|
self.assertRaises(TypeError, next, dropwhile(10, [(4,5)]))
|
|
self.assertRaises(ValueError, next, dropwhile(errfunc, [(4,5)]))
|
|
|
|
def test_tee(self):
|
|
n = 200
|
|
def irange(n):
|
|
for i in range(n):
|
|
yield i
|
|
|
|
a, b = tee([]) # test empty iterator
|
|
self.assertEqual(list(a), [])
|
|
self.assertEqual(list(b), [])
|
|
|
|
a, b = tee(irange(n)) # test 100% interleaved
|
|
self.assertEqual(lzip(a,b), lzip(range(n), range(n)))
|
|
|
|
a, b = tee(irange(n)) # test 0% interleaved
|
|
self.assertEqual(list(a), list(range(n)))
|
|
self.assertEqual(list(b), list(range(n)))
|
|
|
|
a, b = tee(irange(n)) # test dealloc of leading iterator
|
|
for i in range(100):
|
|
self.assertEqual(next(a), i)
|
|
del a
|
|
self.assertEqual(list(b), list(range(n)))
|
|
|
|
a, b = tee(irange(n)) # test dealloc of trailing iterator
|
|
for i in range(100):
|
|
self.assertEqual(next(a), i)
|
|
del b
|
|
self.assertEqual(list(a), list(range(100, n)))
|
|
|
|
for j in range(5): # test randomly interleaved
|
|
order = [0]*n + [1]*n
|
|
random.shuffle(order)
|
|
lists = ([], [])
|
|
its = tee(irange(n))
|
|
for i in order:
|
|
value = next(its[i])
|
|
lists[i].append(value)
|
|
self.assertEqual(lists[0], list(range(n)))
|
|
self.assertEqual(lists[1], list(range(n)))
|
|
|
|
# test argument format checking
|
|
self.assertRaises(TypeError, tee)
|
|
self.assertRaises(TypeError, tee, 3)
|
|
self.assertRaises(TypeError, tee, [1,2], 'x')
|
|
self.assertRaises(TypeError, tee, [1,2], 3, 'x')
|
|
|
|
# tee object should be instantiable
|
|
a, b = tee('abc')
|
|
c = type(a)('def')
|
|
self.assertEqual(list(c), list('def'))
|
|
|
|
# test long-lagged and multi-way split
|
|
a, b, c = tee(range(2000), 3)
|
|
for i in range(100):
|
|
self.assertEqual(next(a), i)
|
|
self.assertEqual(list(b), list(range(2000)))
|
|
self.assertEqual([next(c), next(c)], list(range(2)))
|
|
self.assertEqual(list(a), list(range(100,2000)))
|
|
self.assertEqual(list(c), list(range(2,2000)))
|
|
|
|
# test values of n
|
|
self.assertRaises(TypeError, tee, 'abc', 'invalid')
|
|
self.assertRaises(ValueError, tee, [], -1)
|
|
for n in range(5):
|
|
result = tee('abc', n)
|
|
self.assertEqual(type(result), tuple)
|
|
self.assertEqual(len(result), n)
|
|
self.assertEqual(map(list, result), [list('abc')]*n)
|
|
|
|
# tee pass-through to copyable iterator
|
|
a, b = tee('abc')
|
|
c, d = tee(a)
|
|
self.assert_(a is c)
|
|
|
|
# test tee_new
|
|
t1, t2 = tee('abc')
|
|
tnew = type(t1)
|
|
self.assertRaises(TypeError, tnew)
|
|
self.assertRaises(TypeError, tnew, 10)
|
|
t3 = tnew(t1)
|
|
self.assert_(list(t1) == list(t2) == list(t3) == list('abc'))
|
|
|
|
# test that tee objects are weak referencable
|
|
a, b = tee(range(10))
|
|
p = proxy(a)
|
|
self.assertEqual(getattr(p, '__class__'), type(b))
|
|
del a
|
|
self.assertRaises(ReferenceError, getattr, p, '__class__')
|
|
|
|
def test_StopIteration(self):
|
|
self.assertRaises(StopIteration, next, izip())
|
|
|
|
for f in (chain, cycle, izip, groupby):
|
|
self.assertRaises(StopIteration, next, f([]))
|
|
self.assertRaises(StopIteration, next, f(StopNow()))
|
|
|
|
self.assertRaises(StopIteration, next, islice([], None))
|
|
self.assertRaises(StopIteration, next, islice(StopNow(), None))
|
|
|
|
p, q = tee([])
|
|
self.assertRaises(StopIteration, next, p)
|
|
self.assertRaises(StopIteration, next, q)
|
|
p, q = tee(StopNow())
|
|
self.assertRaises(StopIteration, next, p)
|
|
self.assertRaises(StopIteration, next, q)
|
|
|
|
self.assertRaises(StopIteration, next, repeat(None, 0))
|
|
|
|
for f in (ifilter, ifilterfalse, imap, takewhile, dropwhile, starmap):
|
|
self.assertRaises(StopIteration, next, f(lambda x:x, []))
|
|
self.assertRaises(StopIteration, next, f(lambda x:x, StopNow()))
|
|
|
|
class TestGC(unittest.TestCase):
|
|
|
|
def makecycle(self, iterator, container):
|
|
container.append(iterator)
|
|
next(iterator)
|
|
del container, iterator
|
|
|
|
def test_chain(self):
|
|
a = []
|
|
self.makecycle(chain(a), a)
|
|
|
|
def test_cycle(self):
|
|
a = []
|
|
self.makecycle(cycle([a]*2), a)
|
|
|
|
def test_dropwhile(self):
|
|
a = []
|
|
self.makecycle(dropwhile(bool, [0, a, a]), a)
|
|
|
|
def test_groupby(self):
|
|
a = []
|
|
self.makecycle(groupby([a]*2, lambda x:x), a)
|
|
|
|
def test_ifilter(self):
|
|
a = []
|
|
self.makecycle(ifilter(lambda x:True, [a]*2), a)
|
|
|
|
def test_ifilterfalse(self):
|
|
a = []
|
|
self.makecycle(ifilterfalse(lambda x:False, a), a)
|
|
|
|
def test_izip(self):
|
|
a = []
|
|
self.makecycle(izip([a]*2, [a]*3), a)
|
|
|
|
def test_imap(self):
|
|
a = []
|
|
self.makecycle(imap(lambda x:x, [a]*2), a)
|
|
|
|
def test_islice(self):
|
|
a = []
|
|
self.makecycle(islice([a]*2, None), a)
|
|
|
|
def test_repeat(self):
|
|
a = []
|
|
self.makecycle(repeat(a), a)
|
|
|
|
def test_starmap(self):
|
|
a = []
|
|
self.makecycle(starmap(lambda *t: t, [(a,a)]*2), a)
|
|
|
|
def test_takewhile(self):
|
|
a = []
|
|
self.makecycle(takewhile(bool, [1, 0, a, a]), a)
|
|
|
|
def R(seqn):
|
|
'Regular generator'
|
|
for i in seqn:
|
|
yield i
|
|
|
|
class G:
|
|
'Sequence using __getitem__'
|
|
def __init__(self, seqn):
|
|
self.seqn = seqn
|
|
def __getitem__(self, i):
|
|
return self.seqn[i]
|
|
|
|
class I:
|
|
'Sequence using iterator protocol'
|
|
def __init__(self, seqn):
|
|
self.seqn = seqn
|
|
self.i = 0
|
|
def __iter__(self):
|
|
return self
|
|
def __next__(self):
|
|
if self.i >= len(self.seqn): raise StopIteration
|
|
v = self.seqn[self.i]
|
|
self.i += 1
|
|
return v
|
|
|
|
class Ig:
|
|
'Sequence using iterator protocol defined with a generator'
|
|
def __init__(self, seqn):
|
|
self.seqn = seqn
|
|
self.i = 0
|
|
def __iter__(self):
|
|
for val in self.seqn:
|
|
yield val
|
|
|
|
class X:
|
|
'Missing __getitem__ and __iter__'
|
|
def __init__(self, seqn):
|
|
self.seqn = seqn
|
|
self.i = 0
|
|
def __next__(self):
|
|
if self.i >= len(self.seqn): raise StopIteration
|
|
v = self.seqn[self.i]
|
|
self.i += 1
|
|
return v
|
|
|
|
class N:
|
|
'Iterator missing __next__()'
|
|
def __init__(self, seqn):
|
|
self.seqn = seqn
|
|
self.i = 0
|
|
def __iter__(self):
|
|
return self
|
|
|
|
class E:
|
|
'Test propagation of exceptions'
|
|
def __init__(self, seqn):
|
|
self.seqn = seqn
|
|
self.i = 0
|
|
def __iter__(self):
|
|
return self
|
|
def __next__(self):
|
|
3 // 0
|
|
|
|
class S:
|
|
'Test immediate stop'
|
|
def __init__(self, seqn):
|
|
pass
|
|
def __iter__(self):
|
|
return self
|
|
def __next__(self):
|
|
raise StopIteration
|
|
|
|
def L(seqn):
|
|
'Test multiple tiers of iterators'
|
|
return chain(imap(lambda x:x, R(Ig(G(seqn)))))
|
|
|
|
|
|
class TestVariousIteratorArgs(unittest.TestCase):
|
|
|
|
def test_chain(self):
|
|
for s in ("123", "", range(1000), ('do', 1.2), range(2000,2200,5)):
|
|
for g in (G, I, Ig, S, L, R):
|
|
self.assertEqual(list(chain(g(s))), list(g(s)))
|
|
self.assertEqual(list(chain(g(s), g(s))), list(g(s))+list(g(s)))
|
|
self.assertRaises(TypeError, chain, X(s))
|
|
self.assertRaises(TypeError, chain, N(s))
|
|
self.assertRaises(ZeroDivisionError, list, chain(E(s)))
|
|
|
|
def test_cycle(self):
|
|
for s in ("123", "", range(1000), ('do', 1.2), range(2000,2200,5)):
|
|
for g in (G, I, Ig, S, L, R):
|
|
tgtlen = len(s) * 3
|
|
expected = list(g(s))*3
|
|
actual = list(islice(cycle(g(s)), tgtlen))
|
|
self.assertEqual(actual, expected)
|
|
self.assertRaises(TypeError, cycle, X(s))
|
|
self.assertRaises(TypeError, cycle, N(s))
|
|
self.assertRaises(ZeroDivisionError, list, cycle(E(s)))
|
|
|
|
def test_groupby(self):
|
|
for s in (range(10), range(0), range(1000), (7,11), range(2000,2200,5)):
|
|
for g in (G, I, Ig, S, L, R):
|
|
self.assertEqual([k for k, sb in groupby(g(s))], list(g(s)))
|
|
self.assertRaises(TypeError, groupby, X(s))
|
|
self.assertRaises(TypeError, groupby, N(s))
|
|
self.assertRaises(ZeroDivisionError, list, groupby(E(s)))
|
|
|
|
def test_ifilter(self):
|
|
for s in (range(10), range(0), range(1000), (7,11), range(2000,2200,5)):
|
|
for g in (G, I, Ig, S, L, R):
|
|
self.assertEqual(list(ifilter(isEven, g(s))), filter(isEven, g(s)))
|
|
self.assertRaises(TypeError, ifilter, isEven, X(s))
|
|
self.assertRaises(TypeError, ifilter, isEven, N(s))
|
|
self.assertRaises(ZeroDivisionError, list, ifilter(isEven, E(s)))
|
|
|
|
def test_ifilterfalse(self):
|
|
for s in (range(10), range(0), range(1000), (7,11), range(2000,2200,5)):
|
|
for g in (G, I, Ig, S, L, R):
|
|
self.assertEqual(list(ifilterfalse(isEven, g(s))), filter(isOdd, g(s)))
|
|
self.assertRaises(TypeError, ifilterfalse, isEven, X(s))
|
|
self.assertRaises(TypeError, ifilterfalse, isEven, N(s))
|
|
self.assertRaises(ZeroDivisionError, list, ifilterfalse(isEven, E(s)))
|
|
|
|
def test_izip(self):
|
|
for s in ("123", "", range(1000), ('do', 1.2), range(2000,2200,5)):
|
|
for g in (G, I, Ig, S, L, R):
|
|
self.assertEqual(list(izip(g(s))), lzip(g(s)))
|
|
self.assertEqual(list(izip(g(s), g(s))), lzip(g(s), g(s)))
|
|
self.assertRaises(TypeError, izip, X(s))
|
|
self.assertRaises(TypeError, izip, N(s))
|
|
self.assertRaises(ZeroDivisionError, list, izip(E(s)))
|
|
|
|
def test_iziplongest(self):
|
|
for s in ("123", "", range(1000), ('do', 1.2), range(2000,2200,5)):
|
|
for g in (G, I, Ig, S, L, R):
|
|
self.assertEqual(list(izip_longest(g(s))), list(zip(g(s))))
|
|
self.assertEqual(list(izip_longest(g(s), g(s))), list(zip(g(s), g(s))))
|
|
self.assertRaises(TypeError, izip_longest, X(s))
|
|
self.assertRaises(TypeError, izip_longest, N(s))
|
|
self.assertRaises(ZeroDivisionError, list, izip_longest(E(s)))
|
|
|
|
def test_imap(self):
|
|
for s in (range(10), range(0), range(100), (7,11), range(20,50,5)):
|
|
for g in (G, I, Ig, S, L, R):
|
|
self.assertEqual(list(imap(onearg, g(s))), map(onearg, g(s)))
|
|
self.assertEqual(list(imap(operator.pow, g(s), g(s))), map(operator.pow, g(s), g(s)))
|
|
self.assertRaises(TypeError, imap, onearg, X(s))
|
|
self.assertRaises(TypeError, imap, onearg, N(s))
|
|
self.assertRaises(ZeroDivisionError, list, imap(onearg, E(s)))
|
|
|
|
def test_islice(self):
|
|
for s in ("12345", "", range(1000), ('do', 1.2), range(2000,2200,5)):
|
|
for g in (G, I, Ig, S, L, R):
|
|
self.assertEqual(list(islice(g(s),1,None,2)), list(g(s))[1::2])
|
|
self.assertRaises(TypeError, islice, X(s), 10)
|
|
self.assertRaises(TypeError, islice, N(s), 10)
|
|
self.assertRaises(ZeroDivisionError, list, islice(E(s), 10))
|
|
|
|
def test_starmap(self):
|
|
for s in (range(10), range(0), range(100), (7,11), range(20,50,5)):
|
|
for g in (G, I, Ig, S, L, R):
|
|
ss = lzip(s, s)
|
|
self.assertEqual(list(starmap(operator.pow, g(ss))), map(operator.pow, g(s), g(s)))
|
|
self.assertRaises(TypeError, starmap, operator.pow, X(ss))
|
|
self.assertRaises(TypeError, starmap, operator.pow, N(ss))
|
|
self.assertRaises(ZeroDivisionError, list, starmap(operator.pow, E(ss)))
|
|
|
|
def test_takewhile(self):
|
|
for s in (range(10), range(0), range(1000), (7,11), range(2000,2200,5)):
|
|
for g in (G, I, Ig, S, L, R):
|
|
tgt = []
|
|
for elem in g(s):
|
|
if not isEven(elem): break
|
|
tgt.append(elem)
|
|
self.assertEqual(list(takewhile(isEven, g(s))), tgt)
|
|
self.assertRaises(TypeError, takewhile, isEven, X(s))
|
|
self.assertRaises(TypeError, takewhile, isEven, N(s))
|
|
self.assertRaises(ZeroDivisionError, list, takewhile(isEven, E(s)))
|
|
|
|
def test_dropwhile(self):
|
|
for s in (range(10), range(0), range(1000), (7,11), range(2000,2200,5)):
|
|
for g in (G, I, Ig, S, L, R):
|
|
tgt = []
|
|
for elem in g(s):
|
|
if not tgt and isOdd(elem): continue
|
|
tgt.append(elem)
|
|
self.assertEqual(list(dropwhile(isOdd, g(s))), tgt)
|
|
self.assertRaises(TypeError, dropwhile, isOdd, X(s))
|
|
self.assertRaises(TypeError, dropwhile, isOdd, N(s))
|
|
self.assertRaises(ZeroDivisionError, list, dropwhile(isOdd, E(s)))
|
|
|
|
def test_tee(self):
|
|
for s in ("123", "", range(1000), ('do', 1.2), range(2000,2200,5)):
|
|
for g in (G, I, Ig, S, L, R):
|
|
it1, it2 = tee(g(s))
|
|
self.assertEqual(list(it1), list(g(s)))
|
|
self.assertEqual(list(it2), list(g(s)))
|
|
self.assertRaises(TypeError, tee, X(s))
|
|
self.assertRaises(TypeError, tee, N(s))
|
|
self.assertRaises(ZeroDivisionError, list, tee(E(s))[0])
|
|
|
|
class LengthTransparency(unittest.TestCase):
|
|
|
|
def test_repeat(self):
|
|
from test.test_iterlen import len
|
|
self.assertEqual(len(repeat(None, 50)), 50)
|
|
self.assertRaises(TypeError, len, repeat(None))
|
|
|
|
class RegressionTests(unittest.TestCase):
|
|
|
|
def test_sf_793826(self):
|
|
# Fix Armin Rigo's successful efforts to wreak havoc
|
|
|
|
def mutatingtuple(tuple1, f, tuple2):
|
|
# this builds a tuple t which is a copy of tuple1,
|
|
# then calls f(t), then mutates t to be equal to tuple2
|
|
# (needs len(tuple1) == len(tuple2)).
|
|
def g(value, first=[1]):
|
|
if first:
|
|
del first[:]
|
|
f(next(z))
|
|
return value
|
|
items = list(tuple2)
|
|
items[1:1] = list(tuple1)
|
|
gen = imap(g, items)
|
|
z = izip(*[gen]*len(tuple1))
|
|
next(z)
|
|
|
|
def f(t):
|
|
global T
|
|
T = t
|
|
first[:] = list(T)
|
|
|
|
first = []
|
|
mutatingtuple((1,2,3), f, (4,5,6))
|
|
second = list(T)
|
|
self.assertEqual(first, second)
|
|
|
|
|
|
def test_sf_950057(self):
|
|
# Make sure that chain() and cycle() catch exceptions immediately
|
|
# rather than when shifting between input sources
|
|
|
|
def gen1():
|
|
hist.append(0)
|
|
yield 1
|
|
hist.append(1)
|
|
raise AssertionError
|
|
hist.append(2)
|
|
|
|
def gen2(x):
|
|
hist.append(3)
|
|
yield 2
|
|
hist.append(4)
|
|
if x:
|
|
raise StopIteration
|
|
|
|
hist = []
|
|
self.assertRaises(AssertionError, list, chain(gen1(), gen2(False)))
|
|
self.assertEqual(hist, [0,1])
|
|
|
|
hist = []
|
|
self.assertRaises(AssertionError, list, chain(gen1(), gen2(True)))
|
|
self.assertEqual(hist, [0,1])
|
|
|
|
hist = []
|
|
self.assertRaises(AssertionError, list, cycle(gen1()))
|
|
self.assertEqual(hist, [0,1])
|
|
|
|
class SubclassWithKwargsTest(unittest.TestCase):
|
|
def test_keywords_in_subclass(self):
|
|
# count is not subclassable...
|
|
for cls in (repeat, izip, ifilter, ifilterfalse, chain, imap,
|
|
starmap, islice, takewhile, dropwhile, cycle):
|
|
class Subclass(cls):
|
|
def __init__(self, newarg=None, *args):
|
|
cls.__init__(self, *args)
|
|
try:
|
|
Subclass(newarg=1)
|
|
except TypeError as err:
|
|
# we expect type errors because of wrong argument count
|
|
self.failIf("does not take keyword arguments" in err.args[0])
|
|
|
|
|
|
libreftest = """ Doctest for examples in the library reference: libitertools.tex
|
|
|
|
|
|
>>> amounts = [120.15, 764.05, 823.14]
|
|
>>> for checknum, amount in izip(count(1200), amounts):
|
|
... print('Check %d is for $%.2f' % (checknum, amount))
|
|
...
|
|
Check 1200 is for $120.15
|
|
Check 1201 is for $764.05
|
|
Check 1202 is for $823.14
|
|
|
|
>>> import operator
|
|
>>> for cube in imap(operator.pow, range(1,4), repeat(3)):
|
|
... print(cube)
|
|
...
|
|
1
|
|
8
|
|
27
|
|
|
|
>>> reportlines = ['EuroPython', 'Roster', '', 'alex', '', 'laura', '', 'martin', '', 'walter', '', 'samuele']
|
|
>>> for name in islice(reportlines, 3, None, 2):
|
|
... print(name.title())
|
|
...
|
|
Alex
|
|
Laura
|
|
Martin
|
|
Walter
|
|
Samuele
|
|
|
|
>>> from operator import itemgetter
|
|
>>> d = dict(a=1, b=2, c=1, d=2, e=1, f=2, g=3)
|
|
>>> di = sorted(sorted(d.items()), key=itemgetter(1))
|
|
>>> for k, g in groupby(di, itemgetter(1)):
|
|
... print(k, map(itemgetter(0), g))
|
|
...
|
|
1 ['a', 'c', 'e']
|
|
2 ['b', 'd', 'f']
|
|
3 ['g']
|
|
|
|
# Find runs of consecutive numbers using groupby. The key to the solution
|
|
# is differencing with a range so that consecutive numbers all appear in
|
|
# same group.
|
|
>>> data = [ 1, 4,5,6, 10, 15,16,17,18, 22, 25,26,27,28]
|
|
>>> for k, g in groupby(enumerate(data), lambda (i,x):i-x):
|
|
... print(map(operator.itemgetter(1), g))
|
|
...
|
|
[1]
|
|
[4, 5, 6]
|
|
[10]
|
|
[15, 16, 17, 18]
|
|
[22]
|
|
[25, 26, 27, 28]
|
|
|
|
>>> def take(n, seq):
|
|
... return list(islice(seq, n))
|
|
|
|
>>> def enumerate(iterable):
|
|
... return izip(count(), iterable)
|
|
|
|
>>> def tabulate(function):
|
|
... "Return function(0), function(1), ..."
|
|
... return imap(function, count())
|
|
|
|
>>> def iteritems(mapping):
|
|
... return izip(mapping.keys(), mapping.values())
|
|
|
|
>>> def nth(iterable, n):
|
|
... "Returns the nth item"
|
|
... return list(islice(iterable, n, n+1))
|
|
|
|
>>> def all(seq, pred=None):
|
|
... "Returns True if pred(x) is true for every element in the iterable"
|
|
... for elem in ifilterfalse(pred, seq):
|
|
... return False
|
|
... return True
|
|
|
|
>>> def any(seq, pred=None):
|
|
... "Returns True if pred(x) is true for at least one element in the iterable"
|
|
... for elem in ifilter(pred, seq):
|
|
... return True
|
|
... return False
|
|
|
|
>>> def no(seq, pred=None):
|
|
... "Returns True if pred(x) is false for every element in the iterable"
|
|
... for elem in ifilter(pred, seq):
|
|
... return False
|
|
... return True
|
|
|
|
>>> def quantify(seq, pred=None):
|
|
... "Count how many times the predicate is true in the sequence"
|
|
... return sum(imap(pred, seq))
|
|
|
|
>>> def padnone(seq):
|
|
... "Returns the sequence elements and then returns None indefinitely"
|
|
... return chain(seq, repeat(None))
|
|
|
|
>>> def ncycles(seq, n):
|
|
... "Returns the sequence elements n times"
|
|
... return chain(*repeat(seq, n))
|
|
|
|
>>> def dotproduct(vec1, vec2):
|
|
... return sum(imap(operator.mul, vec1, vec2))
|
|
|
|
>>> def flatten(listOfLists):
|
|
... return list(chain(*listOfLists))
|
|
|
|
>>> def repeatfunc(func, times=None, *args):
|
|
... "Repeat calls to func with specified arguments."
|
|
... " Example: repeatfunc(random.random)"
|
|
... if times is None:
|
|
... return starmap(func, repeat(args))
|
|
... else:
|
|
... return starmap(func, repeat(args, times))
|
|
|
|
>>> def pairwise(iterable):
|
|
... "s -> (s0,s1), (s1,s2), (s2, s3), ..."
|
|
... a, b = tee(iterable)
|
|
... try:
|
|
... next(b)
|
|
... except StopIteration:
|
|
... pass
|
|
... return izip(a, b)
|
|
|
|
This is not part of the examples but it tests to make sure the definitions
|
|
perform as purported.
|
|
|
|
>>> take(10, count())
|
|
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
|
|
|
|
>>> list(enumerate('abc'))
|
|
[(0, 'a'), (1, 'b'), (2, 'c')]
|
|
|
|
>>> list(islice(tabulate(lambda x: 2*x), 4))
|
|
[0, 2, 4, 6]
|
|
|
|
>>> nth('abcde', 3)
|
|
['d']
|
|
|
|
>>> all([2, 4, 6, 8], lambda x: x%2==0)
|
|
True
|
|
|
|
>>> all([2, 3, 6, 8], lambda x: x%2==0)
|
|
False
|
|
|
|
>>> any([2, 4, 6, 8], lambda x: x%2==0)
|
|
True
|
|
|
|
>>> any([1, 3, 5, 9], lambda x: x%2==0,)
|
|
False
|
|
|
|
>>> no([1, 3, 5, 9], lambda x: x%2==0)
|
|
True
|
|
|
|
>>> no([1, 2, 5, 9], lambda x: x%2==0)
|
|
False
|
|
|
|
>>> quantify(range(99), lambda x: x%2==0)
|
|
50
|
|
|
|
>>> a = [[1, 2, 3], [4, 5, 6]]
|
|
>>> flatten(a)
|
|
[1, 2, 3, 4, 5, 6]
|
|
|
|
>>> list(repeatfunc(pow, 5, 2, 3))
|
|
[8, 8, 8, 8, 8]
|
|
|
|
>>> import random
|
|
>>> take(5, imap(int, repeatfunc(random.random)))
|
|
[0, 0, 0, 0, 0]
|
|
|
|
>>> list(pairwise('abcd'))
|
|
[('a', 'b'), ('b', 'c'), ('c', 'd')]
|
|
|
|
>>> list(pairwise([]))
|
|
[]
|
|
|
|
>>> list(pairwise('a'))
|
|
[]
|
|
|
|
>>> list(islice(padnone('abc'), 0, 6))
|
|
['a', 'b', 'c', None, None, None]
|
|
|
|
>>> list(ncycles('abc', 3))
|
|
['a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c']
|
|
|
|
>>> dotproduct([1,2,3], [4,5,6])
|
|
32
|
|
|
|
"""
|
|
|
|
__test__ = {'libreftest' : libreftest}
|
|
|
|
def test_main(verbose=None):
|
|
test_classes = (TestBasicOps, TestVariousIteratorArgs, TestGC,
|
|
RegressionTests, LengthTransparency,
|
|
SubclassWithKwargsTest)
|
|
test_support.run_unittest(*test_classes)
|
|
|
|
# verify reference counting
|
|
if verbose and hasattr(sys, "gettotalrefcount"):
|
|
import gc
|
|
counts = [None] * 5
|
|
for i in range(len(counts)):
|
|
test_support.run_unittest(*test_classes)
|
|
gc.collect()
|
|
counts[i] = sys.gettotalrefcount()
|
|
print(counts)
|
|
|
|
# doctest the examples in the library reference
|
|
test_support.run_doctest(sys.modules[__name__], verbose)
|
|
|
|
if __name__ == "__main__":
|
|
test_main(verbose=True)
|