]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-106300: Improve `assertRaises(Exception)` usages in tests (GH-106302)
authorNikita Sobolev <mail@sobolevn.me>
Fri, 7 Jul 2023 20:42:40 +0000 (23:42 +0300)
committerGitHub <noreply@github.com>
Fri, 7 Jul 2023 20:42:40 +0000 (13:42 -0700)
Lib/test/test_abc.py
Lib/test/test_codecs.py
Lib/test/test_email/test_message.py
Lib/test/test_importlib/test_main.py
Lib/test/test_mailbox.py
Lib/test/test_shutil.py
Lib/test/test_unittest/testmock/testasync.py

index 86f31a9acb4d557113ae8410f5501d1a38fc329c..5ce57cc209ea85c63c04e860cc0919cfa917f60e 100644 (file)
@@ -448,15 +448,16 @@ def test_factory(abc_ABCMeta, abc_get_cache_token):
 
             # Also check that issubclass() propagates exceptions raised by
             # __subclasses__.
+            class CustomError(Exception): ...
             exc_msg = "exception from __subclasses__"
 
             def raise_exc():
-                raise Exception(exc_msg)
+                raise CustomError(exc_msg)
 
             class S(metaclass=abc_ABCMeta):
                 __subclasses__ = raise_exc
 
-            with self.assertRaisesRegex(Exception, exc_msg):
+            with self.assertRaisesRegex(CustomError, exc_msg):
                 issubclass(int, S)
 
         def test_subclasshook(self):
index 376175f90f63eb6e02a93e1d4179cb470d34c01c..91d7eaf997ae20fc5763115ca269616948618eb5 100644 (file)
@@ -2822,14 +2822,15 @@ class TransformCodecTest(unittest.TestCase):
     def test_custom_zlib_error_is_noted(self):
         # Check zlib codec gives a good error for malformed input
         msg = "decoding with 'zlib_codec' codec failed"
-        with self.assertRaises(Exception) as failure:
+        with self.assertRaises(zlib.error) as failure:
             codecs.decode(b"hello", "zlib_codec")
         self.assertEqual(msg, failure.exception.__notes__[0])
 
     def test_custom_hex_error_is_noted(self):
         # Check hex codec gives a good error for malformed input
+        import binascii
         msg = "decoding with 'hex_codec' codec failed"
-        with self.assertRaises(Exception) as failure:
+        with self.assertRaises(binascii.Error) as failure:
             codecs.decode(b"hello", "hex_codec")
         self.assertEqual(msg, failure.exception.__notes__[0])
 
index 4c754bf40fc300826bf2fbbdd206c3b94f8e8477..d3f396f02e7a725cdf84411f6795f0e52af7dff7 100644 (file)
@@ -696,14 +696,16 @@ class TestEmailMessageBase:
             self.assertIsNone(part['Content-Disposition'])
 
     class _TestSetRaisingContentManager:
+        class CustomError(Exception):
+            pass
         def set_content(self, msg, content, *args, **kw):
-            raise Exception('test')
+            raise self.CustomError('test')
 
     def test_default_content_manager_for_add_comes_from_policy(self):
         cm = self._TestSetRaisingContentManager()
         m = self.message(policy=self.policy.clone(content_manager=cm))
         for method in ('add_related', 'add_alternative', 'add_attachment'):
-            with self.assertRaises(Exception) as ar:
+            with self.assertRaises(self._TestSetRaisingContentManager.CustomError) as ar:
                 getattr(m, method)('')
             self.assertEqual(str(ar.exception), 'test')
 
index 6469aad7130fce7c1c0bbe46548e3e32a62824a1..3b49227255eb58d10dabb9f387157659e19eff11 100644 (file)
@@ -68,7 +68,7 @@ class BasicTests(fixtures.DistInfoPkg, unittest.TestCase):
         dict(name=''),
     )
     def test_invalid_inputs_to_from_name(self, name):
-        with self.assertRaises(Exception):
+        with self.assertRaises(ValueError):
             Distribution.from_name(name)
 
 
index 4c592eaf34da23ce3dceab44e54c279338f7ad25..4977a9369ddf88cbbb8ddd90861fe3148b3c10f7 100644 (file)
@@ -116,10 +116,13 @@ class TestMailbox(TestBase):
         self.assertMailboxEmpty()
 
     def test_add_that_raises_leaves_mailbox_empty(self):
+        class CustomError(Exception): ...
+        exc_msg = "a fake error"
+
         def raiser(*args, **kw):
-            raise Exception("a fake error")
+            raise CustomError(exc_msg)
         support.patch(self, email.generator.BytesGenerator, 'flatten', raiser)
-        with self.assertRaises(Exception):
+        with self.assertRaisesRegex(CustomError, exc_msg):
             self._box.add(email.message_from_string("From: Alphöso"))
         self.assertEqual(len(self._box), 0)
         self._box.close()
index 878a21a629f3cc23d50597ab10ca9d863c96d1f1..93f20a6ff4133230478394cc9bfe6836811019d5 100644 (file)
@@ -2738,7 +2738,7 @@ class _ZeroCopyFileTest(object):
     def test_same_file(self):
         self.addCleanup(self.reset)
         with self.get_files() as (src, dst):
-            with self.assertRaises(Exception):
+            with self.assertRaises((OSError, _GiveupOnFastCopy)):
                 self.zerocopy_fun(src, src)
         # Make sure src file is not corrupted.
         self.assertEqual(read_file(TESTFN, binary=True), self.FILEDATA)
index e9e1f63e84d4e7efd8872079f47c771341053a98..f57b83f457f279d26c9698d098e2d9bc35559728 100644 (file)
@@ -459,9 +459,10 @@ class AsyncArguments(IsolatedAsyncioTestCase):
         self.assertEqual(output, 10)
 
     async def test_add_side_effect_exception(self):
+        class CustomError(Exception): pass
         async def addition(var): pass
-        mock = AsyncMock(addition, side_effect=Exception('err'))
-        with self.assertRaises(Exception):
+        mock = AsyncMock(addition, side_effect=CustomError('side-effect'))
+        with self.assertRaisesRegex(CustomError, 'side-effect'):
             await mock(5)
 
     async def test_add_side_effect_coroutine(self):