diff --git a/Lib/test/test_email/test_email.py b/Lib/test/test_email/test_email.py index 8da39b269ad4a7..95f14f0a577d96 100644 --- a/Lib/test/test_email/test_email.py +++ b/Lib/test/test_email/test_email.py @@ -748,11 +748,14 @@ def test_invalid_header_names(self): policy=thispolicy.__class__.__name__, method=method, ): - with self.assertRaises(ValueError) as cm: - getattr(Message(policy=thispolicy), method)(name, value) - msg = str(cm.exception) - self.assertRegex(msg,'(?i)(?=.*invalid)(?=.*header)(?=.*name)') - self.assertIn(f"{name!r}", msg) + message = Message(policy=thispolicy) + method = getattr(message, method) + with self.assertRaisesRegex( + ValueError, + '(?i)(?=.*invalid)(?=.*header)(?=.*name)' + ) as cm: + method(name, value) + self.assertIn(f"{name!r}", str(cm.exception)) def test_binary_quopri_payload(self):