@@ -1249,10 +1249,11 @@ def test_tee(self):
1249
1249
self.assertEqual(len(result), n)
1250
1250
self.assertEqual([list(x) for x in result], [list('abc')]*n)
1251
1251
1252
-
# tee pass-through to copyable iterator
1252
+
# tee objects are independent (see bug gh-123884)
1253
1253
a, b = tee('abc')
1254
1254
c, d = tee(a)
1255
-
self.assertTrue(a is c)
1255
+
e, f = tee(c)
1256
+
self.assertTrue(len({a, b, c, d, e, f}) == 6)
1256
1257
1257
1258
# test tee_new
1258
1259
t1, t2 = tee('abc')
@@ -1759,21 +1760,36 @@ def test_tee_recipe(self):
1759
1760
1760
1761
def tee(iterable, n=2):
1761
1762
if n < 0:
1762
-
raise ValueError('n must be >= 0')
1763
-
iterator = iter(iterable)
1764
-
shared_link = [None, None]
1765
-
return tuple(_tee(iterator, shared_link) for _ in range(n))
1763
+
raise ValueError
1764
+
if n == 0:
1765
+
return ()
1766
+
iterator = _tee(iterable)
1767
+
result = [iterator]
1768
+
for _ in range(n - 1):
1769
+
result.append(_tee(iterator))
1770
+
return tuple(result)
1771
+
1772
+
class _tee:
1773
+
1774
+
def __init__(self, iterable):
1775
+
it = iter(iterable)
1776
+
if isinstance(it, _tee):
1777
+
self.iterator = it.iterator
1778
+
self.link = it.link
1779
+
else:
1780
+
self.iterator = it
1781
+
self.link = [None, None]
1766
1782
1767
-
def _tee(iterator, link):
1768
-
try:
1769
-
while True:
1770
-
if link[1] is None:
1771
-
link[0] = next(iterator)
1772
-
link[1] = [None, None]
1773
-
value, link = link
1774
-
yield value
1775
-
except StopIteration:
1776
-
return
1783
+
def __iter__(self):
1784
+
return self
1785
+
1786
+
def __next__(self):
1787
+
link = self.link
1788
+
if link[1] is None:
1789
+
link[0] = next(self.iterator)
1790
+
link[1] = [None, None]
1791
+
value, self.link = link
1792
+
return value
1777
1793
1778
1794
# End tee() recipe #############################################
1779
1795
@@ -1819,12 +1835,10 @@ def _tee(iterator, link):
1819
1835
self.assertRaises(TypeError, tee, [1,2], 'x')
1820
1836
self.assertRaises(TypeError, tee, [1,2], 3, 'x')
1821
1837
1822
-
# Tests not applicable to the tee() recipe
1823
-
if False:
1824
-
# tee object should be instantiable
1825
-
a, b = tee('abc')
1826
-
c = type(a)('def')
1827
-
self.assertEqual(list(c), list('def'))
1838
+
# tee object should be instantiable
1839
+
a, b = tee('abc')
1840
+
c = type(a)('def')
1841
+
self.assertEqual(list(c), list('def'))
1828
1842
1829
1843
# test long-lagged and multi-way split
1830
1844
a, b, c = tee(range(2000), 3)
@@ -1845,21 +1859,19 @@ def _tee(iterator, link):
1845
1859
self.assertEqual(len(result), n)
1846
1860
self.assertEqual([list(x) for x in result], [list('abc')]*n)
1847
1861
1862
+
# tee objects are independent (see bug gh-123884)
1863
+
a, b = tee('abc')
1864
+
c, d = tee(a)
1865
+
e, f = tee(c)
1866
+
self.assertTrue(len({a, b, c, d, e, f}) == 6)
1848
1867
1849
-
# Tests not applicable to the tee() recipe
1850
-
if False:
1851
-
# tee pass-through to copyable iterator
1852
-
a, b = tee('abc')
1853
-
c, d = tee(a)
1854
-
self.assertTrue(a is c)
1855
-
1856
-
# test tee_new
1857
-
t1, t2 = tee('abc')
1858
-
tnew = type(t1)
1859
-
self.assertRaises(TypeError, tnew)
1860
-
self.assertRaises(TypeError, tnew, 10)
1861
-
t3 = tnew(t1)
1862
-
self.assertTrue(list(t1) == list(t2) == list(t3) == list('abc'))
1868
+
# test tee_new
1869
+
t1, t2 = tee('abc')
1870
+
tnew = type(t1)
1871
+
self.assertRaises(TypeError, tnew)
1872
+
self.assertRaises(TypeError, tnew, 10)
1873
+
t3 = tnew(t1)
1874
+
self.assertTrue(list(t1) == list(t2) == list(t3) == list('abc'))
1863
1875
1864
1876
# test that tee objects are weak referencable
1865
1877
a, b = tee(range(10))
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4