[Python-checkins] bpo-46299: Improve test_descr (GH-30475)

corona10 webhook-mailer at python.org
Sat Jan 8 03:14:52 EST 2022


https://github.com/python/cpython/commit/45d44b950f1dab0ef90d0a8f4fa75ffaae71500b
commit: 45d44b950f1dab0ef90d0a8f4fa75ffaae71500b
branch: main
author: Dong-hee Na <donghee.na at python.org>
committer: corona10 <donghee.na92 at gmail.com>
date: 2022-01-08T17:14:40+09:00
summary:

bpo-46299: Improve test_descr (GH-30475)

files:
M Lib/test/test_descr.py

diff --git a/Lib/test/test_descr.py b/Lib/test/test_descr.py
index e8ecacb5c4d76..707c93140e251 100644
--- a/Lib/test/test_descr.py
+++ b/Lib/test/test_descr.py
@@ -13,6 +13,7 @@
 import weakref
 
 from copy import deepcopy
+from contextlib import redirect_stdout
 from test import support
 
 try:
@@ -1445,12 +1446,9 @@ def mysetattr(self, name, value):
                 raise AttributeError
             return object.__setattr__(self, name, value)
         C.__setattr__ = mysetattr
-        try:
+        with self.assertRaises(AttributeError):
             a.spam = "not spam"
-        except AttributeError:
-            pass
-        else:
-            self.fail("expected AttributeError")
+
         self.assertEqual(a.spam, "spam")
         class D(C):
             pass
@@ -2431,12 +2429,8 @@ def test_dict_constructors(self):
             else:
                 self.fail("no TypeError from dict(%r)" % badarg)
 
-        try:
+        with self.assertRaises(TypeError):
             dict({}, {})
-        except TypeError:
-            pass
-        else:
-            self.fail("no TypeError from dict({}, {})")
 
         class Mapping:
             # Lacks a .keys() method; will be added later.
@@ -3589,12 +3583,8 @@ class A(object):
             pass
 
         A.__call__ = A()
-        try:
+        with self.assertRaises(RecursionError):
             A()()
-        except RecursionError:
-            pass
-        else:
-            self.fail("Recursion limit should have been reached for __call__()")
 
     def test_delete_hook(self):
         # Testing __del__ hook...
@@ -4440,20 +4430,14 @@ def test_wrapper_segfault(self):
 
     def test_file_fault(self):
         # Testing sys.stdout is changed in getattr...
-        test_stdout = sys.stdout
         class StdoutGuard:
             def __getattr__(self, attr):
                 sys.stdout = sys.__stdout__
-                raise RuntimeError("Premature access to sys.stdout.%s" % attr)
-        sys.stdout = StdoutGuard()
-        try:
-            print("Oops!")
-        except RuntimeError:
-            pass
-        else:
-            self.fail("Didn't raise RuntimeError")
-        finally:
-            sys.stdout = test_stdout
+                raise RuntimeError(f"Premature access to sys.stdout.{attr}")
+
+        with redirect_stdout(StdoutGuard()):
+             with self.assertRaises(RuntimeError):
+                 print("Oops!")
 
     def test_vicious_descriptor_nonsense(self):
         # Testing vicious_descriptor_nonsense...



More information about the Python-checkins mailing list