summaryrefslogtreecommitdiffstats
path: root/Lib/test
diff options
context:
space:
mode:
authorKumar Aditya <kumaraditya@python.org>2024-10-25 12:49:30 (GMT)
committerGitHub <noreply@github.com>2024-10-25 12:49:30 (GMT)
commitcae853e3b44cd5cb033b904e163c490dd28bc30a (patch)
tree31d8533074f271cf1d4710552171c8b54e62512c /Lib/test
parentebcc578dff47b1dcffb634923bedc5361c8f29f6 (diff)
downloadcpython-cae853e3b44cd5cb033b904e163c490dd28bc30a.zip
cpython-cae853e3b44cd5cb033b904e163c490dd28bc30a.tar.gz
cpython-cae853e3b44cd5cb033b904e163c490dd28bc30a.tar.bz2
GH-125789: fix `fut._callbacks` to always return a copy of callbacks (#125922)
Fix `asyncio.Future._callbacks` to always return a copy of the internal list of callbacks to avoid mutation from user code affecting the internal state.
Diffstat (limited to 'Lib/test')
-rw-r--r--Lib/test/test_asyncio/test_futures.py18
1 files changed, 18 insertions, 0 deletions
diff --git a/Lib/test/test_asyncio/test_futures.py b/Lib/test/test_asyncio/test_futures.py
index c566b28..b351740 100644
--- a/Lib/test/test_asyncio/test_futures.py
+++ b/Lib/test/test_asyncio/test_futures.py
@@ -697,6 +697,24 @@ class CFutureTests(BaseFutureTests, test_utils.TestCase):
with self.assertRaises(AttributeError):
del fut._log_traceback
+ def test_callbacks_copy(self):
+ # See https://github.com/python/cpython/issues/125789
+ # In C implementation, the `_callbacks` attribute
+ # always returns a new list to avoid mutations of internal state
+
+ fut = self._new_future(loop=self.loop)
+ f1 = lambda _: 1
+ f2 = lambda _: 2
+ fut.add_done_callback(f1)
+ fut.add_done_callback(f2)
+ callbacks = fut._callbacks
+ self.assertIsNot(callbacks, fut._callbacks)
+ fut.remove_done_callback(f1)
+ callbacks = fut._callbacks
+ self.assertIsNot(callbacks, fut._callbacks)
+ fut.remove_done_callback(f2)
+ self.assertIsNone(fut._callbacks)
+
@unittest.skipUnless(hasattr(futures, '_CFuture'),
'requires the C _asyncio module')