Skip to content

Commit 45d44b9

Browse files
authored
bpo-46299: Improve test_descr (GH-30475)
1 parent e63066c commit 45d44b9

File tree

1 file changed

+10
-26
lines changed

1 file changed

+10
-26
lines changed

Lib/test/test_descr.py

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import weakref
1414

1515
from copy import deepcopy
16+
from contextlib import redirect_stdout
1617
from test import support
1718

1819
try:
@@ -1445,12 +1446,9 @@ def mysetattr(self, name, value):
14451446
raise AttributeError
14461447
return object.__setattr__(self, name, value)
14471448
C.__setattr__ = mysetattr
1448-
try:
1449+
with self.assertRaises(AttributeError):
14491450
a.spam = "not spam"
1450-
except AttributeError:
1451-
pass
1452-
else:
1453-
self.fail("expected AttributeError")
1451+
14541452
self.assertEqual(a.spam, "spam")
14551453
class D(C):
14561454
pass
@@ -2431,12 +2429,8 @@ def test_dict_constructors(self):
24312429
else:
24322430
self.fail("no TypeError from dict(%r)" % badarg)
24332431

2434-
try:
2432+
with self.assertRaises(TypeError):
24352433
dict({}, {})
2436-
except TypeError:
2437-
pass
2438-
else:
2439-
self.fail("no TypeError from dict({}, {})")
24402434

24412435
class Mapping:
24422436
# Lacks a .keys() method; will be added later.
@@ -3589,12 +3583,8 @@ class A(object):
35893583
pass
35903584

35913585
A.__call__ = A()
3592-
try:
3586+
with self.assertRaises(RecursionError):
35933587
A()()
3594-
except RecursionError:
3595-
pass
3596-
else:
3597-
self.fail("Recursion limit should have been reached for __call__()")
35983588

35993589
def test_delete_hook(self):
36003590
# Testing __del__ hook...
@@ -4440,20 +4430,14 @@ def test_wrapper_segfault(self):
44404430

44414431
def test_file_fault(self):
44424432
# Testing sys.stdout is changed in getattr...
4443-
test_stdout = sys.stdout
44444433
class StdoutGuard:
44454434
def __getattr__(self, attr):
44464435
sys.stdout = sys.__stdout__
4447-
raise RuntimeError("Premature access to sys.stdout.%s" % attr)
4448-
sys.stdout = StdoutGuard()
4449-
try:
4450-
print("Oops!")
4451-
except RuntimeError:
4452-
pass
4453-
else:
4454-
self.fail("Didn't raise RuntimeError")
4455-
finally:
4456-
sys.stdout = test_stdout
4436+
raise RuntimeError(f"Premature access to sys.stdout.{attr}")
4437+
4438+
with redirect_stdout(StdoutGuard()):
4439+
with self.assertRaises(RuntimeError):
4440+
print("Oops!")
44574441

44584442
def test_vicious_descriptor_nonsense(self):
44594443
# Testing vicious_descriptor_nonsense...

0 commit comments

Comments
 (0)