]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-23819: asyncio: Replace AssertionError with TypeError where it makes sense (GH...
authorKumar Aditya <rahuladitya303@gmail.com>
Mon, 6 Dec 2021 23:40:35 +0000 (05:10 +0530)
committerGitHub <noreply@github.com>
Mon, 6 Dec 2021 23:40:35 +0000 (01:40 +0200)
Lib/asyncio/base_events.py
Lib/asyncio/events.py
Lib/test/test_asyncio/test_base_events.py
Lib/test/test_asyncio/test_events.py
Misc/NEWS.d/next/Library/2021-12-02-14-37-30.bpo-23819.An6vkT.rst [new file with mode: 0644]

index 054d7b45ec2d64bfc0dc5755500babf4d66c3ae5..cfaf082587bb2650704f5ecf9b0082b234442a69 100644 (file)
@@ -706,6 +706,8 @@ class BaseEventLoop(events.AbstractEventLoop):
         Any positional arguments after the callback will be passed to
         the callback when it is called.
         """
+        if delay is None:
+            raise TypeError('delay must not be None')
         timer = self.call_at(self.time() + delay, callback, *args,
                              context=context)
         if timer._source_traceback:
@@ -717,6 +719,8 @@ class BaseEventLoop(events.AbstractEventLoop):
 
         Absolute time corresponds to the event loop's time() method.
         """
+        if when is None:
+            raise TypeError("when cannot be None")
         self._check_closed()
         if self._debug:
             self._check_thread()
index 7abaaca2d2b284fc9a0c682ea942b90ec689f418..d91fe8db2b02044fde9ce335ee85b0b8d93134e4 100644 (file)
@@ -101,7 +101,6 @@ class TimerHandle(Handle):
     __slots__ = ['_scheduled', '_when']
 
     def __init__(self, when, callback, args, loop, context=None):
-        assert when is not None
         super().__init__(callback, args, loop, context)
         if self._source_traceback:
             del self._source_traceback[-1]
@@ -661,7 +660,8 @@ class BaseDefaultEventLoopPolicy(AbstractEventLoopPolicy):
     def set_event_loop(self, loop):
         """Set the event loop."""
         self._local._set_called = True
-        assert loop is None or isinstance(loop, AbstractEventLoop)
+        if loop is not None and not isinstance(loop, AbstractEventLoop):
+            raise TypeError(f"loop must be an instance of AbstractEventLoop or None, not '{type(loop).__name__}'")
         self._local._loop = loop
 
     def new_event_loop(self):
@@ -745,7 +745,8 @@ def set_event_loop_policy(policy):
 
     If policy is None, the default policy is restored."""
     global _event_loop_policy
-    assert policy is None or isinstance(policy, AbstractEventLoopPolicy)
+    if policy is not None and not isinstance(policy, AbstractEventLoopPolicy):
+        raise TypeError(f"policy must be an instance of AbstractEventLoopPolicy or None, not '{type(policy).__name__}'")
     _event_loop_policy = policy
 
 
index b522fac23a23bdf65081903873211687fb120fe6..d812bc9edea5717bc7cac324db52d368c86210f7 100644 (file)
@@ -255,6 +255,8 @@ class BaseEventLoopTests(test_utils.TestCase):
         self.assertIsInstance(h, asyncio.TimerHandle)
         self.assertIn(h, self.loop._scheduled)
         self.assertNotIn(h, self.loop._ready)
+        with self.assertRaises(TypeError, msg="delay must not be None"):
+            self.loop.call_later(None, cb)
 
     def test_call_later_negative_delays(self):
         calls = []
@@ -286,6 +288,8 @@ class BaseEventLoopTests(test_utils.TestCase):
         # tolerate a difference of +800 ms because some Python buildbots
         # are really slow
         self.assertLessEqual(dt, 0.9, dt)
+        with self.assertRaises(TypeError, msg="when cannot be None"):
+            self.loop.call_at(None, cb)
 
     def check_thread(self, loop, debug):
         def cb():
index e50a53d706784057c27d302062b105c1e6657378..fe791fa4232c0848fbd7ad5804b9026d2f7c5974 100644 (file)
@@ -2322,10 +2322,6 @@ class TimerTests(unittest.TestCase):
         self.assertIsNone(h._callback)
         self.assertIsNone(h._args)
 
-        # when cannot be None
-        self.assertRaises(AssertionError,
-                          asyncio.TimerHandle, None, callback, args,
-                          self.loop)
 
     def test_timer_repr(self):
         self.loop.get_debug.return_value = False
@@ -2592,7 +2588,7 @@ class PolicyTests(unittest.TestCase):
         policy = asyncio.DefaultEventLoopPolicy()
         old_loop = policy.get_event_loop()
 
-        self.assertRaises(AssertionError, policy.set_event_loop, object())
+        self.assertRaises(TypeError, policy.set_event_loop, object())
 
         loop = policy.new_event_loop()
         policy.set_event_loop(loop)
@@ -2608,7 +2604,7 @@ class PolicyTests(unittest.TestCase):
 
     def test_set_event_loop_policy(self):
         self.assertRaises(
-            AssertionError, asyncio.set_event_loop_policy, object())
+            TypeError, asyncio.set_event_loop_policy, object())
 
         old_policy = asyncio.get_event_loop_policy()
 
diff --git a/Misc/NEWS.d/next/Library/2021-12-02-14-37-30.bpo-23819.An6vkT.rst b/Misc/NEWS.d/next/Library/2021-12-02-14-37-30.bpo-23819.An6vkT.rst
new file mode 100644 (file)
index 0000000..d1ec505
--- /dev/null
@@ -0,0 +1 @@
+Replaced asserts with exceptions in asyncio, patch by Kumar Aditya.
\ No newline at end of file