From 8738273cd1979f6f808484cdd94df0cadc0dafdd Mon Sep 17 00:00:00 2001 From: Ofey Chan Date: Wed, 28 Sep 2022 09:42:43 +0800 Subject: [PATCH] stage, exploring how to capture deprecation warning rather than supress them. --- Lib/test/test_asyncgen.py | 3 +-- Lib/test/test_generators.py | 46 ++++++++++++++----------------------- 2 files changed, 18 insertions(+), 31 deletions(-) diff --git a/Lib/test/test_asyncgen.py b/Lib/test/test_asyncgen.py index e12a94253f478a..f6184c0cab4694 100644 --- a/Lib/test/test_asyncgen.py +++ b/Lib/test/test_asyncgen.py @@ -382,8 +382,7 @@ def test_async_gen_3_arg_deprecation_warning(self): async def gen(): yield 123 - with warnings.catch_warnings(): - warnings.filterwarnings("ignore", category=DeprecationWarning) + with self.assertWarns(DeprecationWarning): gen().athrow(GeneratorExit, GeneratorExit(), None) def test_async_gen_api_01(self): diff --git a/Lib/test/test_generators.py b/Lib/test/test_generators.py index a1a41b1c326b50..34f85254ece265 100644 --- a/Lib/test/test_generators.py +++ b/Lib/test/test_generators.py @@ -2113,30 +2113,30 @@ def printsolution(self, x): >>> 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) +Traceback (most recent call last): + ... +DeprecationWarning: the (type, exc, tb) signature of throw() is deprecated, use the single-arg signature instead. >>> g.throw(ValueError, TypeError(1)) # mismatched type, rewrapped -caught ValueError (1) +Traceback (most recent call last): + ... +DeprecationWarning: the (type, exc, tb) signature of throw() is deprecated, use the single-arg signature instead. >>> g.throw(ValueError, ValueError(1), None) # explicit None traceback -caught ValueError (1) +Traceback (most recent call last): + ... +DeprecationWarning: the (type, exc, tb) signature of throw() is deprecated, use the single-arg signature instead. >>> g.throw(ValueError(1), "foo") # bad args Traceback (most recent call last): ... -TypeError: instance exception may not have a separate value +DeprecationWarning: the (type, exc, tb) signature of throw() is deprecated, use the single-arg signature instead. >>> g.throw(ValueError, "foo", 23) # bad args Traceback (most recent call last): ... -TypeError: throw() third argument must be a traceback object +DeprecationWarning: the (type, exc, tb) signature of throw() is deprecated, use the single-arg signature instead. >>> g.throw("abc") Traceback (most recent call last): @@ -2159,7 +2159,9 @@ def printsolution(self, x): ... except: ... g.throw(*sys.exc_info()) >>> throw(g,ValueError) # do it with traceback included -caught ValueError () +Traceback (most recent call last): + ... +DeprecationWarning: the (type, exc, tb) signature of throw() is deprecated, use the single-arg signature instead. >>> g.send(1) 1 @@ -2167,31 +2169,17 @@ def printsolution(self, x): >>> throw(g,TypeError) # terminate the generator Traceback (most recent call last): ... -TypeError - ->>> print(g.gi_frame) -None - ->>> g.send(2) -Traceback (most recent call last): - ... -StopIteration +DeprecationWarning: the (type, exc, tb) signature of throw() is deprecated, use the single-arg signature instead. >>> g.throw(ValueError,6) # throw on closed generator Traceback (most recent call last): ... -ValueError: 6 +DeprecationWarning: the (type, exc, tb) signature of throw() is deprecated, use the single-arg signature instead. >>> f().throw(ValueError,7) # throw on just-opened generator Traceback (most recent call last): ... -ValueError: 7 - ->>> warnings.filters.pop(0) -('ignore', None, , None, 0) - -# Re-enable DeprecationWarning: the (type, val, tb) exception representation is deprecated, -# and may be removed in a future version of Python. +DeprecationWarning: the (type, exc, tb) signature of throw() is deprecated, use the single-arg signature instead. Plain "raise" inside a generator should preserve the traceback (#13188). The traceback should have 3 levels: