summaryrefslogtreecommitdiffstats
path: root/Lib/test
diff options
context:
space:
mode:
authorOfey Chan <ofey206@gmail.com>2022-09-30 08:43:02 (GMT)
committerGitHub <noreply@github.com>2022-09-30 08:43:02 (GMT)
commit83a3de4e0632d90e0d1d5a9b8859a94c9ac25f65 (patch)
tree31d942b63699bc5e1848ffb6f85901fe8b93301c /Lib/test
parent9a11ed8e50492d327e4de0a8f3a473e788b14a6f (diff)
downloadcpython-83a3de4e0632d90e0d1d5a9b8859a94c9ac25f65.zip
cpython-83a3de4e0632d90e0d1d5a9b8859a94c9ac25f65.tar.gz
cpython-83a3de4e0632d90e0d1d5a9b8859a94c9ac25f65.tar.bz2
gh-96348: Deprecate the 3-arg signature of coroutine.throw and generator.throw (GH-96428)
Diffstat (limited to 'Lib/test')
-rw-r--r--Lib/test/test_asyncgen.py22
-rw-r--r--Lib/test/test_asyncio/test_futures.py13
-rw-r--r--Lib/test/test_coroutines.py9
-rw-r--r--Lib/test/test_generators.py21
-rw-r--r--Lib/test/test_types.py2
5 files changed, 54 insertions, 13 deletions
diff --git a/Lib/test/test_asyncgen.py b/Lib/test/test_asyncgen.py
index fb22f41..f6184c0 100644
--- a/Lib/test/test_asyncgen.py
+++ b/Lib/test/test_asyncgen.py
@@ -2,6 +2,7 @@ import inspect
import types
import unittest
import contextlib
+import warnings
from test.support.import_helper import import_module
from test.support import gc_collect, requires_working_socket
@@ -377,6 +378,13 @@ class AsyncGenTest(unittest.TestCase):
self.compare_generators(sync_gen_wrapper(), async_gen_wrapper())
+ def test_async_gen_3_arg_deprecation_warning(self):
+ async def gen():
+ yield 123
+
+ with self.assertWarns(DeprecationWarning):
+ gen().athrow(GeneratorExit, GeneratorExit(), None)
+
def test_async_gen_api_01(self):
async def gen():
yield 123
@@ -650,7 +658,7 @@ class AsyncGenAsyncioTest(unittest.TestCase):
agen = agenfn()
with contextlib.closing(anext(agen, "default").__await__()) as g:
self.assertEqual(g.send(None), 1)
- self.assertEqual(g.throw(MyError, MyError(), None), 2)
+ self.assertEqual(g.throw(MyError()), 2)
try:
g.send(None)
except StopIteration as e:
@@ -663,9 +671,9 @@ class AsyncGenAsyncioTest(unittest.TestCase):
agen = agenfn()
with contextlib.closing(anext(agen, "default").__await__()) as g:
self.assertEqual(g.send(None), 1)
- self.assertEqual(g.throw(MyError, MyError(), None), 2)
+ self.assertEqual(g.throw(MyError()), 2)
with self.assertRaises(MyError):
- g.throw(MyError, MyError(), None)
+ g.throw(MyError())
def test3(anext):
agen = agenfn()
@@ -692,9 +700,9 @@ class AsyncGenAsyncioTest(unittest.TestCase):
agen = agenfn()
with contextlib.closing(anext(agen, "default").__await__()) as g:
self.assertEqual(g.send(None), 10)
- self.assertEqual(g.throw(MyError, MyError(), None), 20)
+ self.assertEqual(g.throw(MyError()), 20)
with self.assertRaisesRegex(MyError, 'val'):
- g.throw(MyError, MyError('val'), None)
+ g.throw(MyError('val'))
def test5(anext):
@types.coroutine
@@ -713,7 +721,7 @@ class AsyncGenAsyncioTest(unittest.TestCase):
with contextlib.closing(anext(agen, "default").__await__()) as g:
self.assertEqual(g.send(None), 10)
with self.assertRaisesRegex(StopIteration, 'default'):
- g.throw(MyError, MyError(), None)
+ g.throw(MyError())
def test6(anext):
@types.coroutine
@@ -728,7 +736,7 @@ class AsyncGenAsyncioTest(unittest.TestCase):
agen = agenfn()
with contextlib.closing(anext(agen, "default").__await__()) as g:
with self.assertRaises(MyError):
- g.throw(MyError, MyError(), None)
+ g.throw(MyError())
def run_test(test):
with self.subTest('pure-Python anext()'):
diff --git a/Lib/test/test_asyncio/test_futures.py b/Lib/test/test_asyncio/test_futures.py
index f4a46ec..11d4273 100644
--- a/Lib/test/test_asyncio/test_futures.py
+++ b/Lib/test/test_asyncio/test_futures.py
@@ -10,6 +10,7 @@ from unittest import mock
from types import GenericAlias
import asyncio
from asyncio import futures
+import warnings
from test.test_asyncio import utils as test_utils
from test import support
@@ -619,10 +620,14 @@ class BaseFutureTests:
def test_future_iter_throw(self):
fut = self._new_future(loop=self.loop)
fi = iter(fut)
- self.assertRaises(TypeError, fi.throw,
- Exception, Exception("elephant"), 32)
- self.assertRaises(TypeError, fi.throw,
- Exception("elephant"), Exception("elephant"))
+ with self.assertWarns(DeprecationWarning):
+ self.assertRaises(Exception, fi.throw, Exception, Exception("zebra"), None)
+ with warnings.catch_warnings():
+ warnings.filterwarnings("ignore", category=DeprecationWarning)
+ self.assertRaises(TypeError, fi.throw,
+ Exception, Exception("elephant"), 32)
+ self.assertRaises(TypeError, fi.throw,
+ Exception("elephant"), Exception("elephant"))
self.assertRaises(TypeError, fi.throw, list)
def test_future_del_collect(self):
diff --git a/Lib/test/test_coroutines.py b/Lib/test/test_coroutines.py
index 8fff2d4..9a2279d 100644
--- a/Lib/test/test_coroutines.py
+++ b/Lib/test/test_coroutines.py
@@ -709,9 +709,16 @@ class CoroutineTest(unittest.TestCase):
aw = coro.__await__()
next(aw)
with self.assertRaises(ZeroDivisionError):
- aw.throw(ZeroDivisionError, None, None)
+ aw.throw(ZeroDivisionError())
self.assertEqual(N, 102)
+ coro = foo()
+ aw = coro.__await__()
+ next(aw)
+ with self.assertRaises(ZeroDivisionError):
+ with self.assertWarns(DeprecationWarning):
+ aw.throw(ZeroDivisionError, ZeroDivisionError(), None)
+
def test_func_11(self):
async def func(): pass
coro = func()
diff --git a/Lib/test/test_generators.py b/Lib/test/test_generators.py
index e5aa7da..fb2d9ce 100644
--- a/Lib/test/test_generators.py
+++ b/Lib/test/test_generators.py
@@ -342,6 +342,15 @@ class ExceptionTest(unittest.TestCase):
with self.assertRaises(StopIteration):
gen.throw(E)
+ def test_gen_3_arg_deprecation_warning(self):
+ def g():
+ yield 42
+
+ gen = g()
+ with self.assertWarns(DeprecationWarning):
+ with self.assertRaises(TypeError):
+ gen.throw(TypeError, TypeError(24), None)
+
def test_stopiteration_error(self):
# See also PEP 479.
@@ -2113,6 +2122,12 @@ caught ValueError ()
>>> g.throw(ValueError("xyz")) # value only
caught ValueError (xyz)
+>>> import warnings
+>>> warnings.filterwarnings("ignore", category=DeprecationWarning)
+
+# Filter DeprecationWarning: regarding the (type, val, tb) signature of throw().
+# Deprecation warnings are re-enabled below.
+
>>> g.throw(ValueError, ValueError(1)) # value+matching type
caught ValueError (1)
@@ -2181,6 +2196,12 @@ Traceback (most recent call last):
...
ValueError: 7
+>>> warnings.filters.pop(0)
+('ignore', None, <class 'DeprecationWarning'>, None, 0)
+
+# Re-enable DeprecationWarning: the (type, val, tb) exception representation is deprecated,
+# and may be removed in a future version of Python.
+
Plain "raise" inside a generator should preserve the traceback (#13188).
The traceback should have 3 levels:
- g.throw()
diff --git a/Lib/test/test_types.py b/Lib/test/test_types.py
index f00da0a..af09563 100644
--- a/Lib/test/test_types.py
+++ b/Lib/test/test_types.py
@@ -2072,7 +2072,7 @@ class CoroutineTests(unittest.TestCase):
wrapper = foo()
wrapper.send(None)
with self.assertRaisesRegex(Exception, 'ham'):
- wrapper.throw(Exception, Exception('ham'))
+ wrapper.throw(Exception('ham'))
# decorate foo second time
foo = types.coroutine(foo)