|
13 | 13 | import weakref |
14 | 14 |
|
15 | 15 | from copy import deepcopy |
| 16 | +from contextlib import redirect_stdout |
16 | 17 | from test import support |
17 | 18 |
|
18 | 19 | try: |
@@ -1445,12 +1446,9 @@ def mysetattr(self, name, value): |
1445 | 1446 | raise AttributeError |
1446 | 1447 | return object.__setattr__(self, name, value) |
1447 | 1448 | C.__setattr__ = mysetattr |
1448 | | - try: |
| 1449 | + with self.assertRaises(AttributeError): |
1449 | 1450 | a.spam = "not spam" |
1450 | | - except AttributeError: |
1451 | | - pass |
1452 | | - else: |
1453 | | - self.fail("expected AttributeError") |
| 1451 | + |
1454 | 1452 | self.assertEqual(a.spam, "spam") |
1455 | 1453 | class D(C): |
1456 | 1454 | pass |
@@ -2431,12 +2429,8 @@ def test_dict_constructors(self): |
2431 | 2429 | else: |
2432 | 2430 | self.fail("no TypeError from dict(%r)" % badarg) |
2433 | 2431 |
|
2434 | | - try: |
| 2432 | + with self.assertRaises(TypeError): |
2435 | 2433 | dict({}, {}) |
2436 | | - except TypeError: |
2437 | | - pass |
2438 | | - else: |
2439 | | - self.fail("no TypeError from dict({}, {})") |
2440 | 2434 |
|
2441 | 2435 | class Mapping: |
2442 | 2436 | # Lacks a .keys() method; will be added later. |
@@ -3589,12 +3583,8 @@ class A(object): |
3589 | 3583 | pass |
3590 | 3584 |
|
3591 | 3585 | A.__call__ = A() |
3592 | | - try: |
| 3586 | + with self.assertRaises(RecursionError): |
3593 | 3587 | A()() |
3594 | | - except RecursionError: |
3595 | | - pass |
3596 | | - else: |
3597 | | - self.fail("Recursion limit should have been reached for __call__()") |
3598 | 3588 |
|
3599 | 3589 | def test_delete_hook(self): |
3600 | 3590 | # Testing __del__ hook... |
@@ -4440,20 +4430,14 @@ def test_wrapper_segfault(self): |
4440 | 4430 |
|
4441 | 4431 | def test_file_fault(self): |
4442 | 4432 | # Testing sys.stdout is changed in getattr... |
4443 | | - test_stdout = sys.stdout |
4444 | 4433 | class StdoutGuard: |
4445 | 4434 | def __getattr__(self, attr): |
4446 | 4435 | 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!") |
4457 | 4441 |
|
4458 | 4442 | def test_vicious_descriptor_nonsense(self): |
4459 | 4443 | # Testing vicious_descriptor_nonsense... |
|
0 commit comments