summaryrefslogtreecommitdiffstats
path: root/Lib/test
diff options
context:
space:
mode:
authorYury Selivanov <yury@magic.io>2016-06-09 19:08:31 (GMT)
committerYury Selivanov <yury@magic.io>2016-06-09 19:08:31 (GMT)
commita6f6edbda8648698289a8ee7abef6a35c924151b (patch)
tree9eb77fd4f552bcabfb46a3938d0ded084e7709f9 /Lib/test
parentebe95fdabb42b02ff7eecab6bc9637cf5ccf1d2c (diff)
downloadcpython-a6f6edbda8648698289a8ee7abef6a35c924151b.zip
cpython-a6f6edbda8648698289a8ee7abef6a35c924151b.tar.gz
cpython-a6f6edbda8648698289a8ee7abef6a35c924151b.tar.bz2
Issue #27243: Fix __aiter__ protocol
Diffstat (limited to 'Lib/test')
-rw-r--r--Lib/test/test_coroutines.py98
-rw-r--r--Lib/test/test_grammar.py2
2 files changed, 79 insertions, 21 deletions
diff --git a/Lib/test/test_coroutines.py b/Lib/test/test_coroutines.py
index 187348d..4f725ae 100644
--- a/Lib/test/test_coroutines.py
+++ b/Lib/test/test_coroutines.py
@@ -1255,8 +1255,9 @@ class CoroutineTest(unittest.TestCase):
buffer = []
async def test1():
- async for i1, i2 in AsyncIter():
- buffer.append(i1 + i2)
+ with self.assertWarnsRegex(PendingDeprecationWarning, "legacy"):
+ async for i1, i2 in AsyncIter():
+ buffer.append(i1 + i2)
yielded, _ = run_async(test1())
# Make sure that __aiter__ was called only once
@@ -1268,12 +1269,13 @@ class CoroutineTest(unittest.TestCase):
buffer = []
async def test2():
nonlocal buffer
- async for i in AsyncIter():
- buffer.append(i[0])
- if i[0] == 20:
- break
- else:
- buffer.append('what?')
+ with self.assertWarnsRegex(PendingDeprecationWarning, "legacy"):
+ async for i in AsyncIter():
+ buffer.append(i[0])
+ if i[0] == 20:
+ break
+ else:
+ buffer.append('what?')
buffer.append('end')
yielded, _ = run_async(test2())
@@ -1286,12 +1288,13 @@ class CoroutineTest(unittest.TestCase):
buffer = []
async def test3():
nonlocal buffer
- async for i in AsyncIter():
- if i[0] > 20:
- continue
- buffer.append(i[0])
- else:
- buffer.append('what?')
+ with self.assertWarnsRegex(PendingDeprecationWarning, "legacy"):
+ async for i in AsyncIter():
+ if i[0] > 20:
+ continue
+ buffer.append(i[0])
+ else:
+ buffer.append('what?')
buffer.append('end')
yielded, _ = run_async(test3())
@@ -1338,7 +1341,7 @@ class CoroutineTest(unittest.TestCase):
def test_for_4(self):
class I:
- async def __aiter__(self):
+ def __aiter__(self):
return self
def __anext__(self):
@@ -1368,8 +1371,9 @@ class CoroutineTest(unittest.TestCase):
return 123
async def foo():
- async for i in I():
- print('never going to happen')
+ with self.assertWarnsRegex(PendingDeprecationWarning, "legacy"):
+ async for i in I():
+ print('never going to happen')
with self.assertRaisesRegex(
TypeError,
@@ -1393,7 +1397,7 @@ class CoroutineTest(unittest.TestCase):
def __init__(self):
self.i = 0
- async def __aiter__(self):
+ def __aiter__(self):
return self
async def __anext__(self):
@@ -1417,7 +1421,11 @@ class CoroutineTest(unittest.TestCase):
I += 1
I += 1000
- run_async(main())
+ with warnings.catch_warnings():
+ warnings.simplefilter("error")
+ # Test that __aiter__ that returns an asyncronous iterator
+ # directly does not throw any warnings.
+ run_async(main())
self.assertEqual(I, 111011)
self.assertEqual(sys.getrefcount(manager), mrefs_before)
@@ -1472,13 +1480,63 @@ class CoroutineTest(unittest.TestCase):
1/0
async def foo():
nonlocal CNT
+ with self.assertWarnsRegex(PendingDeprecationWarning, "legacy"):
+ async for i in AI():
+ CNT += 1
+ CNT += 10
+ with self.assertRaises(ZeroDivisionError):
+ run_async(foo())
+ self.assertEqual(CNT, 0)
+
+ def test_for_8(self):
+ CNT = 0
+ class AI:
+ def __aiter__(self):
+ 1/0
+ async def foo():
+ nonlocal CNT
async for i in AI():
CNT += 1
CNT += 10
with self.assertRaises(ZeroDivisionError):
- run_async(foo())
+ with warnings.catch_warnings():
+ warnings.simplefilter("error")
+ # Test that if __aiter__ raises an exception it propagates
+ # without any kind of warning.
+ run_async(foo())
self.assertEqual(CNT, 0)
+ def test_for_9(self):
+ # Test that PendingDeprecationWarning can safely be converted into
+ # an exception (__aiter__ should not have a chance to raise
+ # a ZeroDivisionError.)
+ class AI:
+ async def __aiter__(self):
+ 1/0
+ async def foo():
+ async for i in AI():
+ pass
+
+ with self.assertRaises(PendingDeprecationWarning):
+ with warnings.catch_warnings():
+ warnings.simplefilter("error")
+ run_async(foo())
+
+ def test_for_10(self):
+ # Test that PendingDeprecationWarning can safely be converted into
+ # an exception.
+ class AI:
+ async def __aiter__(self):
+ pass
+ async def foo():
+ async for i in AI():
+ pass
+
+ with self.assertRaises(PendingDeprecationWarning):
+ with warnings.catch_warnings():
+ warnings.simplefilter("error")
+ run_async(foo())
+
def test_copy(self):
async def func(): pass
coro = func()
diff --git a/Lib/test/test_grammar.py b/Lib/test/test_grammar.py
index d68cc7d..154e3b6 100644
--- a/Lib/test/test_grammar.py
+++ b/Lib/test/test_grammar.py
@@ -1076,7 +1076,7 @@ class GrammarTests(unittest.TestCase):
class Done(Exception): pass
class AIter:
- async def __aiter__(self):
+ def __aiter__(self):
return self
async def __anext__(self):
raise StopAsyncIteration