Python-checkins
Threads by month
- ----- 2026 -----
- April
- March
- February
- January
- ----- 2025 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2024 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2023 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2022 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2021 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2020 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2019 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2018 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2017 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2016 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2015 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2014 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2013 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2012 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2011 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2010 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2009 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2008 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2007 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2006 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2005 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2004 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2003 -----
- December
- November
- October
- September
- August
December 2021
- 1 participants
- 354 discussions
https://github.com/python/cpython/commit/2ef06d412531d1163dbc72877c88aedf3e…
commit: 2ef06d412531d1163dbc72877c88aedf3ed82a25
branch: main
author: Matti Picus <matti.picus(a)gmail.com>
committer: rhettinger <rhettinger(a)users.noreply.github.com>
date: 2021-12-19T14:24:30-06:00
summary:
bpo-46131: add fastpath for PyFloat_Check() (#30200)
files:
A Misc/NEWS.d/next/C API/2021-12-19-13-02-59.bpo-46131.ZBWQtO.rst
M Doc/c-api/typeobj.rst
M Include/floatobject.h
M Include/object.h
M Objects/floatobject.c
M Objects/typeobject.c
M Tools/gdb/libpython.py
diff --git a/Doc/c-api/typeobj.rst b/Doc/c-api/typeobj.rst
index cd8723efef6bb..eed2ac2c092ec 100644
--- a/Doc/c-api/typeobj.rst
+++ b/Doc/c-api/typeobj.rst
@@ -1145,6 +1145,7 @@ and :c:type:`PyType_Type` effectively act as defaults.)
.. XXX Document more flags here?
+ .. data:: Py_TPFLAGS_FLOAT_SUBCLASS
.. data:: Py_TPFLAGS_LONG_SUBCLASS
.. data:: Py_TPFLAGS_LIST_SUBCLASS
.. data:: Py_TPFLAGS_TUPLE_SUBCLASS
diff --git a/Include/floatobject.h b/Include/floatobject.h
index 3b6ca478eaef2..51be745e11cfa 100644
--- a/Include/floatobject.h
+++ b/Include/floatobject.h
@@ -14,6 +14,8 @@ extern "C" {
PyAPI_DATA(PyTypeObject) PyFloat_Type;
#define PyFloat_Check(op) PyObject_TypeCheck(op, &PyFloat_Type)
+#define PyFloat_Check(op) \
+ PyType_FastSubclass(Py_TYPE(op), Py_TPFLAGS_FLOAT_SUBCLASS)
#define PyFloat_CheckExact(op) Py_IS_TYPE(op, &PyFloat_Type)
#ifdef Py_NAN
diff --git a/Include/object.h b/Include/object.h
index e5544e8b588ed..ee817a545a2b5 100644
--- a/Include/object.h
+++ b/Include/object.h
@@ -397,6 +397,7 @@ given type object has a specified feature.
#define _Py_TPFLAGS_MATCH_SELF (1UL << 22)
/* These flags are used to determine if a type is a subclass. */
+#define Py_TPFLAGS_FLOAT_SUBCLASS (1UL << 23)
#define Py_TPFLAGS_LONG_SUBCLASS (1UL << 24)
#define Py_TPFLAGS_LIST_SUBCLASS (1UL << 25)
#define Py_TPFLAGS_TUPLE_SUBCLASS (1UL << 26)
diff --git a/Misc/NEWS.d/next/C API/2021-12-19-13-02-59.bpo-46131.ZBWQtO.rst b/Misc/NEWS.d/next/C API/2021-12-19-13-02-59.bpo-46131.ZBWQtO.rst
new file mode 100644
index 0000000000000..08768ca594966
--- /dev/null
+++ b/Misc/NEWS.d/next/C API/2021-12-19-13-02-59.bpo-46131.ZBWQtO.rst
@@ -0,0 +1,2 @@
+Add a fast path for ``PyFloat_Check`` via a ``Py_TPFLAGS_FLOAT_SUBCLASS``
+flag.
diff --git a/Objects/floatobject.c b/Objects/floatobject.c
index f8620d6f8ef0b..37434f32bd995 100644
--- a/Objects/floatobject.c
+++ b/Objects/floatobject.c
@@ -1959,6 +1959,7 @@ PyTypeObject PyFloat_Type = {
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
+ Py_TPFLAGS_FLOAT_SUBCLASS |
_Py_TPFLAGS_MATCH_SELF, /* tp_flags */
float_new__doc__, /* tp_doc */
0, /* tp_traverse */
diff --git a/Objects/typeobject.c b/Objects/typeobject.c
index af35180cdb983..8bd92801da4ba 100644
--- a/Objects/typeobject.c
+++ b/Objects/typeobject.c
@@ -5783,6 +5783,9 @@ inherit_special(PyTypeObject *type, PyTypeObject *base)
else if (PyType_IsSubtype(base, &PyDict_Type)) {
type->tp_flags |= Py_TPFLAGS_DICT_SUBCLASS;
}
+ else if (PyType_IsSubtype(base, &PyFloat_Type)) {
+ type->tp_flags |= Py_TPFLAGS_FLOAT_SUBCLASS;
+ }
if (PyType_HasFeature(base, _Py_TPFLAGS_MATCH_SELF)) {
type->tp_flags |= _Py_TPFLAGS_MATCH_SELF;
}
diff --git a/Tools/gdb/libpython.py b/Tools/gdb/libpython.py
index a0a95e3fc63cb..41d1c3cc6e1bd 100755
--- a/Tools/gdb/libpython.py
+++ b/Tools/gdb/libpython.py
@@ -85,6 +85,7 @@ def _sizeof_void_p():
Py_TPFLAGS_MANAGED_DICT = (1 << 4)
Py_TPFLAGS_HEAPTYPE = (1 << 9)
+Py_TPFLAGS_FLOAT_SUBCLASS = (1 << 23)
Py_TPFLAGS_LONG_SUBCLASS = (1 << 24)
Py_TPFLAGS_LIST_SUBCLASS = (1 << 25)
Py_TPFLAGS_TUPLE_SUBCLASS = (1 << 26)
@@ -379,6 +380,8 @@ def subclass_from_type(cls, t):
if tp_flags & Py_TPFLAGS_HEAPTYPE:
return HeapTypeObjectPtr
+ if tp_flags & Py_TPFLAGS_FLOAT_SUBCLASS:
+ return PyFloatObjectPtr
if tp_flags & Py_TPFLAGS_LONG_SUBCLASS:
return PyLongObjectPtr
if tp_flags & Py_TPFLAGS_LIST_SUBCLASS:
@@ -910,6 +913,16 @@ class PyNoneStructPtr(PyObjectPtr):
def proxyval(self, visited):
return None
+class PyFloatObjectPtr(PyObjectPtr):
+ _typename = 'PyFloatObject'
+
+ def proxyval(self, visited):
+ return self.field('ob_fval')
+
+ def write_repr(self, out, visited):
+ proxy = self.proxyval(visited)
+ out.write("%s" % proxy)
+
class PyFrameObjectPtr(PyObjectPtr):
_typename = 'PyFrameObject'
1
0
bpo-46076: Improve documentation for per-attribute docstrings with `__slots__` (GH-30109)
by rhettinger Dec. 19, 2021
by rhettinger Dec. 19, 2021
Dec. 19, 2021
https://github.com/python/cpython/commit/aeb9ef4c7287fe367b6e9adcf1c5f994d5…
commit: aeb9ef4c7287fe367b6e9adcf1c5f994d5bc1a09
branch: main
author: Alex Waygood <Alex.Waygood(a)Gmail.com>
committer: rhettinger <rhettinger(a)users.noreply.github.com>
date: 2021-12-19T14:20:07-06:00
summary:
bpo-46076: Improve documentation for per-attribute docstrings with `__slots__` (GH-30109)
files:
M Doc/reference/datamodel.rst
diff --git a/Doc/reference/datamodel.rst b/Doc/reference/datamodel.rst
index aa0d91ad1c97d..48c54d729424c 100644
--- a/Doc/reference/datamodel.rst
+++ b/Doc/reference/datamodel.rst
@@ -1944,9 +1944,12 @@ Notes on using *__slots__*
* Nonempty *__slots__* does not work for classes derived from "variable-length"
built-in types such as :class:`int`, :class:`bytes` and :class:`tuple`.
-* Any non-string iterable may be assigned to *__slots__*. Mappings may also be
- used; however, in the future, special meaning may be assigned to the values
- corresponding to each key.
+* Any non-string :term:`iterable` may be assigned to *__slots__*.
+
+* If a :class:`dictionary <dict>` is used to assign *__slots__*, the dictionary
+ keys will be used as the slot names. The values of the dictionary can be used
+ to provide per-attribute docstrings that will be recognised by
+ :func:`inspect.getdoc` and displayed in the output of :func:`help`.
* :attr:`~instance.__class__` assignment works only if both classes have the
same *__slots__*.
1
0
https://github.com/python/cpython/commit/2352644377f0d2febc226ba30c94046e4c…
commit: 2352644377f0d2febc226ba30c94046e4cc50130
branch: 3.9
author: Andrew Svetlov <andrew.svetlov(a)gmail.com>
committer: asvetlov <andrew.svetlov(a)gmail.com>
date: 2021-12-19T20:12:24+02:00
summary:
Fix test error about deprecation warning (#30205)
files:
M Lib/test/test_asyncio/test_locks.py
diff --git a/Lib/test/test_asyncio/test_locks.py b/Lib/test/test_asyncio/test_locks.py
index cfb6421b38f60..b9aae360384ea 100644
--- a/Lib/test/test_asyncio/test_locks.py
+++ b/Lib/test/test_asyncio/test_locks.py
@@ -770,7 +770,8 @@ async def wrong_loop_in_cond():
ValueError,
"loop argument must agree with lock"
):
- asyncio.Condition(lock, loop=loop)
+ with self.assertWarns(DeprecationWarning):
+ asyncio.Condition(lock, loop=loop)
await wrong_loop_in_lock()
await wrong_loop_in_cond()
1
0
[3.9] bpo-46129: Rewrite asyncio.locks tests with IsolatedAsyncioTestCase (GH-30198) (GH-30204)
by asvetlov Dec. 19, 2021
by asvetlov Dec. 19, 2021
Dec. 19, 2021
https://github.com/python/cpython/commit/f8fce5e4e4338fbe49e5e7a5a856101f69…
commit: f8fce5e4e4338fbe49e5e7a5a856101f69081a08
branch: 3.9
author: Andrew Svetlov <andrew.svetlov(a)gmail.com>
committer: asvetlov <andrew.svetlov(a)gmail.com>
date: 2021-12-19T18:59:37+02:00
summary:
[3.9] bpo-46129: Rewrite asyncio.locks tests with IsolatedAsyncioTestCase (GH-30198) (GH-30204)
Co-authored-by: Kumar Aditya <59607654+kumaraditya303(a)users.noreply.github.com>.
(cherry picked from commit 9c06fd89514a9a2865e2adcc472095f6949cecb2)
Co-authored-by: Andrew Svetlov <andrew.svetlov(a)gmail.com>
files:
A Misc/NEWS.d/next/Tests/2021-12-19-12-20-57.bpo-46129.I3MunH.rst
M Lib/test/test_asyncio/test_locks.py
M Lib/unittest/async_case.py
diff --git a/Lib/test/test_asyncio/test_locks.py b/Lib/test/test_asyncio/test_locks.py
index 8c93fae2b51c6..cfb6421b38f60 100644
--- a/Lib/test/test_asyncio/test_locks.py
+++ b/Lib/test/test_asyncio/test_locks.py
@@ -5,7 +5,6 @@
import re
import asyncio
-from test.test_asyncio import utils as test_utils
STR_RGX_REPR = (
r'^<(?P<class>.*?) object at (?P<address>.*?)'
@@ -20,86 +19,67 @@ def tearDownModule():
asyncio.set_event_loop_policy(None)
-class LockTests(test_utils.TestCase):
+class LockTests(unittest.IsolatedAsyncioTestCase):
- def setUp(self):
- super().setUp()
- self.loop = self.new_test_loop()
-
- def test_ctor_loop(self):
+ async def test_ctor_loop(self):
loop = mock.Mock()
with self.assertWarns(DeprecationWarning):
lock = asyncio.Lock(loop=loop)
self.assertIs(lock._loop, loop)
with self.assertWarns(DeprecationWarning):
- lock = asyncio.Lock(loop=self.loop)
- self.assertIs(lock._loop, self.loop)
+ lock = asyncio.Lock(loop=asyncio.get_running_loop())
+ self.assertIs(lock._loop, asyncio.get_running_loop())
- def test_ctor_noloop(self):
- asyncio.set_event_loop(self.loop)
+ async def test_ctor_noloop(self):
lock = asyncio.Lock()
- self.assertIs(lock._loop, self.loop)
+ self.assertIs(lock._loop, asyncio.get_running_loop())
- def test_repr(self):
+ async def test_repr(self):
with self.assertWarns(DeprecationWarning):
- lock = asyncio.Lock(loop=self.loop)
+ lock = asyncio.Lock(loop=asyncio.get_running_loop())
self.assertTrue(repr(lock).endswith('[unlocked]>'))
self.assertTrue(RGX_REPR.match(repr(lock)))
- self.loop.run_until_complete(lock.acquire())
+ await lock.acquire()
self.assertTrue(repr(lock).endswith('[locked]>'))
self.assertTrue(RGX_REPR.match(repr(lock)))
- def test_lock(self):
- with self.assertWarns(DeprecationWarning):
- lock = asyncio.Lock(loop=self.loop)
-
- @asyncio.coroutine
- def acquire_lock():
- return (yield from lock)
+ async def test_lock(self):
+ lock = asyncio.Lock()
with self.assertRaisesRegex(
TypeError,
- "object is not iterable"
+ "object Lock can't be used in 'await' expression"
):
- self.loop.run_until_complete(acquire_lock())
+ await lock
self.assertFalse(lock.locked())
- def test_lock_by_with_statement(self):
- loop = asyncio.new_event_loop() # don't use TestLoop quirks
- self.set_event_loop(loop)
- with self.assertWarns(DeprecationWarning):
- primitives = [
- asyncio.Lock(loop=loop),
- asyncio.Condition(loop=loop),
- asyncio.Semaphore(loop=loop),
- asyncio.BoundedSemaphore(loop=loop),
- ]
-
- @asyncio.coroutine
- def test(lock):
- yield from asyncio.sleep(0.01)
- self.assertFalse(lock.locked())
- with self.assertRaisesRegex(
- TypeError,
- "object is not iterable"
- ):
- with (yield from lock):
- pass
- self.assertFalse(lock.locked())
+ async def test_lock_by_with_statement(self):
+ primitives = [
+ asyncio.Lock(),
+ asyncio.Condition(),
+ asyncio.Semaphore(),
+ asyncio.BoundedSemaphore(),
+ ]
- for primitive in primitives:
- loop.run_until_complete(test(primitive))
- self.assertFalse(primitive.locked())
+ for lock in primitives:
+ await asyncio.sleep(0.01)
+ self.assertFalse(lock.locked())
+ with self.assertRaisesRegex(
+ TypeError,
+ r"object \w+ can't be used in 'await' expression"
+ ):
+ with await lock:
+ pass
+ self.assertFalse(lock.locked())
- def test_acquire(self):
- with self.assertWarns(DeprecationWarning):
- lock = asyncio.Lock(loop=self.loop)
+ async def test_acquire(self):
+ lock = asyncio.Lock()
result = []
- self.assertTrue(self.loop.run_until_complete(lock.acquire()))
+ self.assertTrue(await lock.acquire())
async def c1(result):
if await lock.acquire():
@@ -116,27 +96,27 @@ async def c3(result):
result.append(3)
return True
- t1 = self.loop.create_task(c1(result))
- t2 = self.loop.create_task(c2(result))
+ t1 = asyncio.create_task(c1(result))
+ t2 = asyncio.create_task(c2(result))
- test_utils.run_briefly(self.loop)
+ await asyncio.sleep(0)
self.assertEqual([], result)
lock.release()
- test_utils.run_briefly(self.loop)
+ await asyncio.sleep(0)
self.assertEqual([1], result)
- test_utils.run_briefly(self.loop)
+ await asyncio.sleep(0)
self.assertEqual([1], result)
- t3 = self.loop.create_task(c3(result))
+ t3 = asyncio.create_task(c3(result))
lock.release()
- test_utils.run_briefly(self.loop)
+ await asyncio.sleep(0)
self.assertEqual([1, 2], result)
lock.release()
- test_utils.run_briefly(self.loop)
+ await asyncio.sleep(0)
self.assertEqual([1, 2, 3], result)
self.assertTrue(t1.done())
@@ -146,19 +126,18 @@ async def c3(result):
self.assertTrue(t3.done())
self.assertTrue(t3.result())
- def test_acquire_cancel(self):
+ async def test_acquire_cancel(self):
with self.assertWarns(DeprecationWarning):
- lock = asyncio.Lock(loop=self.loop)
- self.assertTrue(self.loop.run_until_complete(lock.acquire()))
-
- task = self.loop.create_task(lock.acquire())
- self.loop.call_soon(task.cancel)
- self.assertRaises(
- asyncio.CancelledError,
- self.loop.run_until_complete, task)
+ lock = asyncio.Lock(loop=asyncio.get_running_loop())
+ self.assertTrue(await lock.acquire())
+
+ task = asyncio.create_task(lock.acquire())
+ asyncio.get_running_loop().call_soon(task.cancel)
+ with self.assertRaises(asyncio.CancelledError):
+ await task
self.assertFalse(lock._waiters)
- def test_cancel_race(self):
+ async def test_cancel_race(self):
# Several tasks:
# - A acquires the lock
# - B is blocked in acquire()
@@ -174,7 +153,7 @@ def test_cancel_race(self):
# Setup: A has the lock, b and c are waiting.
with self.assertWarns(DeprecationWarning):
- lock = asyncio.Lock(loop=self.loop)
+ lock = asyncio.Lock(loop=asyncio.get_running_loop())
async def lockit(name, blocker):
await lock.acquire()
@@ -184,15 +163,15 @@ async def lockit(name, blocker):
finally:
lock.release()
- fa = self.loop.create_future()
- ta = self.loop.create_task(lockit('A', fa))
- test_utils.run_briefly(self.loop)
+ fa = asyncio.get_running_loop().create_future()
+ ta = asyncio.create_task(lockit('A', fa))
+ await asyncio.sleep(0)
self.assertTrue(lock.locked())
- tb = self.loop.create_task(lockit('B', None))
- test_utils.run_briefly(self.loop)
+ tb = asyncio.create_task(lockit('B', None))
+ await asyncio.sleep(0)
self.assertEqual(len(lock._waiters), 1)
- tc = self.loop.create_task(lockit('C', None))
- test_utils.run_briefly(self.loop)
+ tc = asyncio.create_task(lockit('C', None))
+ await asyncio.sleep(0)
self.assertEqual(len(lock._waiters), 2)
# Create the race and check.
@@ -200,18 +179,19 @@ async def lockit(name, blocker):
fa.set_result(None)
tb.cancel()
self.assertTrue(lock._waiters[0].cancelled())
- test_utils.run_briefly(self.loop)
+ await asyncio.sleep(0)
self.assertFalse(lock.locked())
self.assertTrue(ta.done())
self.assertTrue(tb.cancelled())
- self.assertTrue(tc.done())
+ await tc
- def test_cancel_release_race(self):
+ async def test_cancel_release_race(self):
# Issue 32734
# Acquire 4 locks, cancel second, release first
# and 2 locks are taken at once.
+ loop = asyncio.get_running_loop()
with self.assertWarns(DeprecationWarning):
- lock = asyncio.Lock(loop=self.loop)
+ lock = asyncio.Lock(loop=loop)
lock_count = 0
call_count = 0
@@ -222,27 +202,23 @@ async def lockit():
await lock.acquire()
lock_count += 1
- async def lockandtrigger():
- await lock.acquire()
- self.loop.call_soon(trigger)
-
def trigger():
t1.cancel()
lock.release()
- t0 = self.loop.create_task(lockandtrigger())
- t1 = self.loop.create_task(lockit())
- t2 = self.loop.create_task(lockit())
- t3 = self.loop.create_task(lockit())
+ await lock.acquire()
- # First loop acquires all
- test_utils.run_briefly(self.loop)
- self.assertTrue(t0.done())
+ t1 = asyncio.create_task(lockit())
+ t2 = asyncio.create_task(lockit())
+ t3 = asyncio.create_task(lockit())
- # Second loop calls trigger
- test_utils.run_briefly(self.loop)
- # Third loop calls cancellation
- test_utils.run_briefly(self.loop)
+ # Start scheduled tasks
+ await asyncio.sleep(0)
+
+ loop.call_soon(trigger)
+ with self.assertRaises(asyncio.CancelledError):
+ # Wait for cancellation
+ await t1
# Make sure only one lock was taken
self.assertEqual(lock_count, 1)
@@ -252,84 +228,78 @@ def trigger():
# Cleanup the task that is stuck on acquire.
t3.cancel()
- test_utils.run_briefly(self.loop)
+ await asyncio.sleep(0)
self.assertTrue(t3.cancelled())
- def test_finished_waiter_cancelled(self):
+ async def test_finished_waiter_cancelled(self):
with self.assertWarns(DeprecationWarning):
- lock = asyncio.Lock(loop=self.loop)
+ lock = asyncio.Lock(loop=asyncio.get_running_loop())
- ta = self.loop.create_task(lock.acquire())
- test_utils.run_briefly(self.loop)
+ await lock.acquire()
self.assertTrue(lock.locked())
- tb = self.loop.create_task(lock.acquire())
- test_utils.run_briefly(self.loop)
+ tb = asyncio.create_task(lock.acquire())
+ await asyncio.sleep(0)
self.assertEqual(len(lock._waiters), 1)
# Create a second waiter, wake up the first, and cancel it.
# Without the fix, the second was not woken up.
- tc = self.loop.create_task(lock.acquire())
- lock.release()
+ tc = asyncio.create_task(lock.acquire())
tb.cancel()
- test_utils.run_briefly(self.loop)
+ lock.release()
+ await asyncio.sleep(0)
self.assertTrue(lock.locked())
- self.assertTrue(ta.done())
self.assertTrue(tb.cancelled())
- def test_release_not_acquired(self):
+ # Cleanup
+ await tc
+
+ async def test_release_not_acquired(self):
with self.assertWarns(DeprecationWarning):
- lock = asyncio.Lock(loop=self.loop)
+ lock = asyncio.Lock(loop=asyncio.get_running_loop())
self.assertRaises(RuntimeError, lock.release)
- def test_release_no_waiters(self):
+ async def test_release_no_waiters(self):
with self.assertWarns(DeprecationWarning):
- lock = asyncio.Lock(loop=self.loop)
- self.loop.run_until_complete(lock.acquire())
+ lock = asyncio.Lock(loop=asyncio.get_running_loop())
+ await lock.acquire()
self.assertTrue(lock.locked())
lock.release()
self.assertFalse(lock.locked())
- def test_context_manager(self):
- async def f():
- lock = asyncio.Lock()
- self.assertFalse(lock.locked())
-
- async with lock:
- self.assertTrue(lock.locked())
-
- self.assertFalse(lock.locked())
+ async def test_context_manager(self):
+ lock = asyncio.Lock()
+ self.assertFalse(lock.locked())
- self.loop.run_until_complete(f())
+ async with lock:
+ self.assertTrue(lock.locked())
+ self.assertFalse(lock.locked())
-class EventTests(test_utils.TestCase):
- def setUp(self):
- super().setUp()
- self.loop = self.new_test_loop()
+class EventTests(unittest.IsolatedAsyncioTestCase):
- def test_ctor_loop(self):
+ async def test_ctor_loop(self):
loop = mock.Mock()
with self.assertWarns(DeprecationWarning):
ev = asyncio.Event(loop=loop)
self.assertIs(ev._loop, loop)
with self.assertWarns(DeprecationWarning):
- ev = asyncio.Event(loop=self.loop)
- self.assertIs(ev._loop, self.loop)
+ ev = asyncio.Event(loop=asyncio.get_running_loop())
+ self.assertIs(ev._loop, asyncio.get_running_loop())
- def test_ctor_noloop(self):
- asyncio.set_event_loop(self.loop)
+ async def test_ctor_noloop(self):
+ asyncio.set_event_loop(asyncio.get_running_loop())
ev = asyncio.Event()
- self.assertIs(ev._loop, self.loop)
+ self.assertIs(ev._loop, asyncio.get_running_loop())
- def test_repr(self):
+ async def test_repr(self):
with self.assertWarns(DeprecationWarning):
- ev = asyncio.Event(loop=self.loop)
+ ev = asyncio.Event(loop=asyncio.get_running_loop())
self.assertTrue(repr(ev).endswith('[unset]>'))
match = RGX_REPR.match(repr(ev))
self.assertEqual(match.group('extras'), 'unset')
@@ -342,9 +312,9 @@ def test_repr(self):
self.assertTrue('waiters:1' in repr(ev))
self.assertTrue(RGX_REPR.match(repr(ev)))
- def test_wait(self):
+ async def test_wait(self):
with self.assertWarns(DeprecationWarning):
- ev = asyncio.Event(loop=self.loop)
+ ev = asyncio.Event(loop=asyncio.get_running_loop())
self.assertFalse(ev.is_set())
result = []
@@ -361,16 +331,16 @@ async def c3(result):
if await ev.wait():
result.append(3)
- t1 = self.loop.create_task(c1(result))
- t2 = self.loop.create_task(c2(result))
+ t1 = asyncio.create_task(c1(result))
+ t2 = asyncio.create_task(c2(result))
- test_utils.run_briefly(self.loop)
+ await asyncio.sleep(0)
self.assertEqual([], result)
- t3 = self.loop.create_task(c3(result))
+ t3 = asyncio.create_task(c3(result))
ev.set()
- test_utils.run_briefly(self.loop)
+ await asyncio.sleep(0)
self.assertEqual([3, 1, 2], result)
self.assertTrue(t1.done())
@@ -380,28 +350,24 @@ async def c3(result):
self.assertTrue(t3.done())
self.assertIsNone(t3.result())
- def test_wait_on_set(self):
- with self.assertWarns(DeprecationWarning):
- ev = asyncio.Event(loop=self.loop)
+ async def test_wait_on_set(self):
+ ev = asyncio.Event()
ev.set()
- res = self.loop.run_until_complete(ev.wait())
+ res = await ev.wait()
self.assertTrue(res)
- def test_wait_cancel(self):
- with self.assertWarns(DeprecationWarning):
- ev = asyncio.Event(loop=self.loop)
+ async def test_wait_cancel(self):
+ ev = asyncio.Event()
- wait = self.loop.create_task(ev.wait())
- self.loop.call_soon(wait.cancel)
- self.assertRaises(
- asyncio.CancelledError,
- self.loop.run_until_complete, wait)
+ wait = asyncio.create_task(ev.wait())
+ asyncio.get_running_loop().call_soon(wait.cancel)
+ with self.assertRaises(asyncio.CancelledError):
+ await wait
self.assertFalse(ev._waiters)
- def test_clear(self):
- with self.assertWarns(DeprecationWarning):
- ev = asyncio.Event(loop=self.loop)
+ async def test_clear(self):
+ ev = asyncio.Event()
self.assertFalse(ev.is_set())
ev.set()
@@ -410,9 +376,8 @@ def test_clear(self):
ev.clear()
self.assertFalse(ev.is_set())
- def test_clear_with_waiters(self):
- with self.assertWarns(DeprecationWarning):
- ev = asyncio.Event(loop=self.loop)
+ async def test_clear_with_waiters(self):
+ ev = asyncio.Event()
result = []
async def c1(result):
@@ -420,8 +385,8 @@ async def c1(result):
result.append(1)
return True
- t = self.loop.create_task(c1(result))
- test_utils.run_briefly(self.loop)
+ t = asyncio.create_task(c1(result))
+ await asyncio.sleep(0)
self.assertEqual([], result)
ev.set()
@@ -432,7 +397,7 @@ async def c1(result):
ev.set()
self.assertEqual(1, len(ev._waiters))
- test_utils.run_briefly(self.loop)
+ await asyncio.sleep(0)
self.assertEqual([1], result)
self.assertEqual(0, len(ev._waiters))
@@ -440,29 +405,23 @@ async def c1(result):
self.assertTrue(t.result())
-class ConditionTests(test_utils.TestCase):
+class ConditionTests(unittest.IsolatedAsyncioTestCase):
- def setUp(self):
- super().setUp()
- self.loop = self.new_test_loop()
-
- def test_ctor_loop(self):
+ async def test_ctor_loop(self):
loop = mock.Mock()
with self.assertWarns(DeprecationWarning):
cond = asyncio.Condition(loop=loop)
self.assertIs(cond._loop, loop)
- cond = asyncio.Condition(loop=self.loop)
- self.assertIs(cond._loop, self.loop)
+ cond = asyncio.Condition(loop=asyncio.get_running_loop())
+ self.assertIs(cond._loop, asyncio.get_running_loop())
- def test_ctor_noloop(self):
- asyncio.set_event_loop(self.loop)
+ async def test_ctor_noloop(self):
cond = asyncio.Condition()
- self.assertIs(cond._loop, self.loop)
+ self.assertIs(cond._loop, asyncio.get_running_loop())
- def test_wait(self):
- with self.assertWarns(DeprecationWarning):
- cond = asyncio.Condition(loop=self.loop)
+ async def test_wait(self):
+ cond = asyncio.Condition()
result = []
async def c1(result):
@@ -483,37 +442,37 @@ async def c3(result):
result.append(3)
return True
- t1 = self.loop.create_task(c1(result))
- t2 = self.loop.create_task(c2(result))
- t3 = self.loop.create_task(c3(result))
+ t1 = asyncio.create_task(c1(result))
+ t2 = asyncio.create_task(c2(result))
+ t3 = asyncio.create_task(c3(result))
- test_utils.run_briefly(self.loop)
+ await asyncio.sleep(0)
self.assertEqual([], result)
self.assertFalse(cond.locked())
- self.assertTrue(self.loop.run_until_complete(cond.acquire()))
+ self.assertTrue(await cond.acquire())
cond.notify()
- test_utils.run_briefly(self.loop)
+ await asyncio.sleep(0)
self.assertEqual([], result)
self.assertTrue(cond.locked())
cond.release()
- test_utils.run_briefly(self.loop)
+ await asyncio.sleep(0)
self.assertEqual([1], result)
self.assertTrue(cond.locked())
cond.notify(2)
- test_utils.run_briefly(self.loop)
+ await asyncio.sleep(0)
self.assertEqual([1], result)
self.assertTrue(cond.locked())
cond.release()
- test_utils.run_briefly(self.loop)
+ await asyncio.sleep(0)
self.assertEqual([1, 2], result)
self.assertTrue(cond.locked())
cond.release()
- test_utils.run_briefly(self.loop)
+ await asyncio.sleep(0)
self.assertEqual([1, 2, 3], result)
self.assertTrue(cond.locked())
@@ -524,49 +483,46 @@ async def c3(result):
self.assertTrue(t3.done())
self.assertTrue(t3.result())
- def test_wait_cancel(self):
- with self.assertWarns(DeprecationWarning):
- cond = asyncio.Condition(loop=self.loop)
- self.loop.run_until_complete(cond.acquire())
-
- wait = self.loop.create_task(cond.wait())
- self.loop.call_soon(wait.cancel)
- self.assertRaises(
- asyncio.CancelledError,
- self.loop.run_until_complete, wait)
+ async def test_wait_cancel(self):
+ cond = asyncio.Condition()
+ await cond.acquire()
+
+ wait = asyncio.create_task(cond.wait())
+ asyncio.get_running_loop().call_soon(wait.cancel)
+ with self.assertRaises(asyncio.CancelledError):
+ await wait
self.assertFalse(cond._waiters)
self.assertTrue(cond.locked())
- def test_wait_cancel_contested(self):
- with self.assertWarns(DeprecationWarning):
- cond = asyncio.Condition(loop=self.loop)
+ async def test_wait_cancel_contested(self):
+ cond = asyncio.Condition()
- self.loop.run_until_complete(cond.acquire())
+ await cond.acquire()
self.assertTrue(cond.locked())
- wait_task = self.loop.create_task(cond.wait())
- test_utils.run_briefly(self.loop)
+ wait_task = asyncio.create_task(cond.wait())
+ await asyncio.sleep(0)
self.assertFalse(cond.locked())
# Notify, but contest the lock before cancelling
- self.loop.run_until_complete(cond.acquire())
+ await cond.acquire()
self.assertTrue(cond.locked())
cond.notify()
- self.loop.call_soon(wait_task.cancel)
- self.loop.call_soon(cond.release)
+ asyncio.get_running_loop().call_soon(wait_task.cancel)
+ asyncio.get_running_loop().call_soon(cond.release)
try:
- self.loop.run_until_complete(wait_task)
+ await wait_task
except asyncio.CancelledError:
# Should not happen, since no cancellation points
pass
self.assertTrue(cond.locked())
- def test_wait_cancel_after_notify(self):
+ async def test_wait_cancel_after_notify(self):
# See bpo-32841
with self.assertWarns(DeprecationWarning):
- cond = asyncio.Condition(loop=self.loop)
+ cond = asyncio.Condition(loop=asyncio.get_running_loop())
waited = False
async def wait_on_cond():
@@ -575,30 +531,27 @@ async def wait_on_cond():
waited = True # Make sure this area was reached
await cond.wait()
- waiter = asyncio.ensure_future(wait_on_cond(), loop=self.loop)
- test_utils.run_briefly(self.loop) # Start waiting
+ waiter = asyncio.create_task(wait_on_cond())
+ await asyncio.sleep(0) # Start waiting
- self.loop.run_until_complete(cond.acquire())
+ await cond.acquire()
cond.notify()
- test_utils.run_briefly(self.loop) # Get to acquire()
+ await asyncio.sleep(0) # Get to acquire()
waiter.cancel()
- test_utils.run_briefly(self.loop) # Activate cancellation
+ await asyncio.sleep(0) # Activate cancellation
cond.release()
- test_utils.run_briefly(self.loop) # Cancellation should occur
+ await asyncio.sleep(0) # Cancellation should occur
self.assertTrue(waiter.cancelled())
self.assertTrue(waited)
- def test_wait_unacquired(self):
- with self.assertWarns(DeprecationWarning):
- cond = asyncio.Condition(loop=self.loop)
- self.assertRaises(
- RuntimeError,
- self.loop.run_until_complete, cond.wait())
+ async def test_wait_unacquired(self):
+ cond = asyncio.Condition()
+ with self.assertRaises(RuntimeError):
+ await cond.wait()
- def test_wait_for(self):
- with self.assertWarns(DeprecationWarning):
- cond = asyncio.Condition(loop=self.loop)
+ async def test_wait_for(self):
+ cond = asyncio.Condition()
presult = False
def predicate():
@@ -613,43 +566,39 @@ async def c1(result):
cond.release()
return True
- t = self.loop.create_task(c1(result))
+ t = asyncio.create_task(c1(result))
- test_utils.run_briefly(self.loop)
+ await asyncio.sleep(0)
self.assertEqual([], result)
- self.loop.run_until_complete(cond.acquire())
+ await cond.acquire()
cond.notify()
cond.release()
- test_utils.run_briefly(self.loop)
+ await asyncio.sleep(0)
self.assertEqual([], result)
presult = True
- self.loop.run_until_complete(cond.acquire())
+ await cond.acquire()
cond.notify()
cond.release()
- test_utils.run_briefly(self.loop)
+ await asyncio.sleep(0)
self.assertEqual([1], result)
self.assertTrue(t.done())
self.assertTrue(t.result())
- def test_wait_for_unacquired(self):
- with self.assertWarns(DeprecationWarning):
- cond = asyncio.Condition(loop=self.loop)
+ async def test_wait_for_unacquired(self):
+ cond = asyncio.Condition()
# predicate can return true immediately
- res = self.loop.run_until_complete(cond.wait_for(lambda: [1, 2, 3]))
+ res = await cond.wait_for(lambda: [1, 2, 3])
self.assertEqual([1, 2, 3], res)
- self.assertRaises(
- RuntimeError,
- self.loop.run_until_complete,
- cond.wait_for(lambda: False))
+ with self.assertRaises(RuntimeError):
+ await cond.wait_for(lambda: False)
- def test_notify(self):
- with self.assertWarns(DeprecationWarning):
- cond = asyncio.Condition(loop=self.loop)
+ async def test_notify(self):
+ cond = asyncio.Condition()
result = []
async def c1(result):
@@ -673,24 +622,24 @@ async def c3(result):
cond.release()
return True
- t1 = self.loop.create_task(c1(result))
- t2 = self.loop.create_task(c2(result))
- t3 = self.loop.create_task(c3(result))
+ t1 = asyncio.create_task(c1(result))
+ t2 = asyncio.create_task(c2(result))
+ t3 = asyncio.create_task(c3(result))
- test_utils.run_briefly(self.loop)
+ await asyncio.sleep(0)
self.assertEqual([], result)
- self.loop.run_until_complete(cond.acquire())
+ await cond.acquire()
cond.notify(1)
cond.release()
- test_utils.run_briefly(self.loop)
+ await asyncio.sleep(0)
self.assertEqual([1], result)
- self.loop.run_until_complete(cond.acquire())
+ await cond.acquire()
cond.notify(1)
cond.notify(2048)
cond.release()
- test_utils.run_briefly(self.loop)
+ await asyncio.sleep(0)
self.assertEqual([1, 2, 3], result)
self.assertTrue(t1.done())
@@ -700,9 +649,8 @@ async def c3(result):
self.assertTrue(t3.done())
self.assertTrue(t3.result())
- def test_notify_all(self):
- with self.assertWarns(DeprecationWarning):
- cond = asyncio.Condition(loop=self.loop)
+ async def test_notify_all(self):
+ cond = asyncio.Condition()
result = []
@@ -720,16 +668,16 @@ async def c2(result):
cond.release()
return True
- t1 = self.loop.create_task(c1(result))
- t2 = self.loop.create_task(c2(result))
+ t1 = asyncio.create_task(c1(result))
+ t2 = asyncio.create_task(c2(result))
- test_utils.run_briefly(self.loop)
+ await asyncio.sleep(0)
self.assertEqual([], result)
- self.loop.run_until_complete(cond.acquire())
+ await cond.acquire()
cond.notify_all()
cond.release()
- test_utils.run_briefly(self.loop)
+ await asyncio.sleep(0)
self.assertEqual([1, 2], result)
self.assertTrue(t1.done())
@@ -738,22 +686,19 @@ async def c2(result):
self.assertTrue(t2.result())
def test_notify_unacquired(self):
- with self.assertWarns(DeprecationWarning):
- cond = asyncio.Condition(loop=self.loop)
+ cond = asyncio.Condition()
self.assertRaises(RuntimeError, cond.notify)
def test_notify_all_unacquired(self):
- with self.assertWarns(DeprecationWarning):
- cond = asyncio.Condition(loop=self.loop)
+ cond = asyncio.Condition()
self.assertRaises(RuntimeError, cond.notify_all)
- def test_repr(self):
- with self.assertWarns(DeprecationWarning):
- cond = asyncio.Condition(loop=self.loop)
+ async def test_repr(self):
+ cond = asyncio.Condition()
self.assertTrue('unlocked' in repr(cond))
self.assertTrue(RGX_REPR.match(repr(cond)))
- self.loop.run_until_complete(cond.acquire())
+ await cond.acquire()
self.assertTrue('locked' in repr(cond))
cond._waiters.append(mock.Mock())
@@ -764,79 +709,106 @@ def test_repr(self):
self.assertTrue('waiters:2' in repr(cond))
self.assertTrue(RGX_REPR.match(repr(cond)))
- def test_context_manager(self):
- async def f():
- cond = asyncio.Condition()
+ async def test_context_manager(self):
+ cond = asyncio.Condition()
+ self.assertFalse(cond.locked())
+ async with cond:
+ self.assertTrue(cond.locked())
+ self.assertFalse(cond.locked())
+
+ async def test_explicit_lock(self):
+ async def f(lock=None, cond=None):
+ if lock is None:
+ lock = asyncio.Lock()
+ if cond is None:
+ cond = asyncio.Condition(lock)
+ self.assertIs(cond._lock, lock)
+ self.assertFalse(lock.locked())
self.assertFalse(cond.locked())
async with cond:
+ self.assertTrue(lock.locked())
self.assertTrue(cond.locked())
+ self.assertFalse(lock.locked())
+ self.assertFalse(cond.locked())
+ async with lock:
+ self.assertTrue(lock.locked())
+ self.assertTrue(cond.locked())
+ self.assertFalse(lock.locked())
self.assertFalse(cond.locked())
- self.loop.run_until_complete(f())
-
- def test_explicit_lock(self):
- with self.assertWarns(DeprecationWarning):
- lock = asyncio.Lock(loop=self.loop)
- cond = asyncio.Condition(lock, loop=self.loop)
-
- self.assertIs(cond._lock, lock)
- self.assertIs(cond._loop, lock._loop)
-
- def test_ambiguous_loops(self):
- loop = self.new_test_loop()
- self.addCleanup(loop.close)
- with self.assertWarns(DeprecationWarning):
- lock = asyncio.Lock(loop=self.loop)
- with self.assertRaises(ValueError):
- asyncio.Condition(lock, loop=loop)
+ # All should work in the same way.
+ await f()
+ await f(asyncio.Lock())
+ lock = asyncio.Lock()
+ await f(lock, asyncio.Condition(lock))
- def test_timeout_in_block(self):
+ async def test_ambiguous_loops(self):
loop = asyncio.new_event_loop()
self.addCleanup(loop.close)
- async def task_timeout():
- condition = asyncio.Condition(loop=loop)
- async with condition:
- with self.assertRaises(asyncio.TimeoutError):
- await asyncio.wait_for(condition.wait(), timeout=0.5)
+ async def wrong_loop_in_lock():
+ with self.assertWarns(DeprecationWarning):
+ asyncio.Lock(loop=loop)
+ lock = asyncio.Lock()
+ lock._loop = loop # use private API for testing
+ async with lock:
+ # acquired immediately via the fast-path
+ # without interaction with any event loop.
+ # cond.acquire() will trigger waiting on the lock
+ # and it will discover the event loop mismatch.
+ with self.assertRaisesRegex(
+ ValueError,
+ "loop argument must agree with lock",
+ ):
+ asyncio.Condition(lock)
+
+ async def wrong_loop_in_cond():
+ # Same analogy here with the condition's loop.
+ lock = asyncio.Lock()
+ async with lock:
+ with self.assertRaisesRegex(
+ ValueError,
+ "loop argument must agree with lock"
+ ):
+ asyncio.Condition(lock, loop=loop)
- with self.assertWarns(DeprecationWarning):
- loop.run_until_complete(task_timeout())
+ await wrong_loop_in_lock()
+ await wrong_loop_in_cond()
+ async def test_timeout_in_block(self):
+ condition = asyncio.Condition()
+ async with condition:
+ with self.assertRaises(asyncio.TimeoutError):
+ await asyncio.wait_for(condition.wait(), timeout=0.5)
-class SemaphoreTests(test_utils.TestCase):
- def setUp(self):
- super().setUp()
- self.loop = self.new_test_loop()
+class SemaphoreTests(unittest.IsolatedAsyncioTestCase):
- def test_ctor_loop(self):
+ async def test_ctor_loop(self):
loop = mock.Mock()
with self.assertWarns(DeprecationWarning):
sem = asyncio.Semaphore(loop=loop)
self.assertIs(sem._loop, loop)
with self.assertWarns(DeprecationWarning):
- sem = asyncio.Semaphore(loop=self.loop)
- self.assertIs(sem._loop, self.loop)
+ sem = asyncio.Semaphore(loop=asyncio.get_running_loop())
+ self.assertIs(sem._loop, asyncio.get_running_loop())
- def test_ctor_noloop(self):
- asyncio.set_event_loop(self.loop)
+ async def test_ctor_noloop(self):
sem = asyncio.Semaphore()
- self.assertIs(sem._loop, self.loop)
+ self.assertIs(sem._loop, asyncio.get_running_loop())
- def test_initial_value_zero(self):
+ async def test_initial_value_zero(self):
with self.assertWarns(DeprecationWarning):
- sem = asyncio.Semaphore(0, loop=self.loop)
+ sem = asyncio.Semaphore(0, loop=asyncio.get_running_loop())
self.assertTrue(sem.locked())
- def test_repr(self):
- with self.assertWarns(DeprecationWarning):
- sem = asyncio.Semaphore(loop=self.loop)
+ async def test_repr(self):
+ sem = asyncio.Semaphore()
self.assertTrue(repr(sem).endswith('[unlocked, value:1]>'))
self.assertTrue(RGX_REPR.match(repr(sem)))
- self.loop.run_until_complete(sem.acquire())
+ await sem.acquire()
self.assertTrue(repr(sem).endswith('[locked]>'))
self.assertTrue('waiters' not in repr(sem))
self.assertTrue(RGX_REPR.match(repr(sem)))
@@ -849,21 +821,15 @@ def test_repr(self):
self.assertTrue('waiters:2' in repr(sem))
self.assertTrue(RGX_REPR.match(repr(sem)))
- def test_semaphore(self):
- with self.assertWarns(DeprecationWarning):
- sem = asyncio.Semaphore(loop=self.loop)
+ async def test_semaphore(self):
+ sem = asyncio.Semaphore()
self.assertEqual(1, sem._value)
- with self.assertWarns(DeprecationWarning):
- @asyncio.coroutine
- def acquire_lock():
- return (yield from sem)
-
with self.assertRaisesRegex(
TypeError,
- "'Semaphore' object is not iterable",
+ "object Semaphore can't be used in 'await' expression",
):
- self.loop.run_until_complete(acquire_lock())
+ await sem
self.assertFalse(sem.locked())
self.assertEqual(1, sem._value)
@@ -871,13 +837,12 @@ def acquire_lock():
def test_semaphore_value(self):
self.assertRaises(ValueError, asyncio.Semaphore, -1)
- def test_acquire(self):
- with self.assertWarns(DeprecationWarning):
- sem = asyncio.Semaphore(3, loop=self.loop)
+ async def test_acquire(self):
+ sem = asyncio.Semaphore(3)
result = []
- self.assertTrue(self.loop.run_until_complete(sem.acquire()))
- self.assertTrue(self.loop.run_until_complete(sem.acquire()))
+ self.assertTrue(await sem.acquire())
+ self.assertTrue(await sem.acquire())
self.assertFalse(sem.locked())
async def c1(result):
@@ -900,23 +865,23 @@ async def c4(result):
result.append(4)
return True
- t1 = self.loop.create_task(c1(result))
- t2 = self.loop.create_task(c2(result))
- t3 = self.loop.create_task(c3(result))
+ t1 = asyncio.create_task(c1(result))
+ t2 = asyncio.create_task(c2(result))
+ t3 = asyncio.create_task(c3(result))
- test_utils.run_briefly(self.loop)
+ await asyncio.sleep(0)
self.assertEqual([1], result)
self.assertTrue(sem.locked())
self.assertEqual(2, len(sem._waiters))
self.assertEqual(0, sem._value)
- t4 = self.loop.create_task(c4(result))
+ t4 = asyncio.create_task(c4(result))
sem.release()
sem.release()
self.assertEqual(2, sem._value)
- test_utils.run_briefly(self.loop)
+ await asyncio.sleep(0)
self.assertEqual(0, sem._value)
self.assertEqual(3, len(result))
self.assertTrue(sem.locked())
@@ -931,69 +896,65 @@ async def c4(result):
# cleanup locked semaphore
sem.release()
- self.loop.run_until_complete(asyncio.gather(*race_tasks))
+ await asyncio.gather(*race_tasks)
- def test_acquire_cancel(self):
- with self.assertWarns(DeprecationWarning):
- sem = asyncio.Semaphore(loop=self.loop)
- self.loop.run_until_complete(sem.acquire())
-
- acquire = self.loop.create_task(sem.acquire())
- self.loop.call_soon(acquire.cancel)
- self.assertRaises(
- asyncio.CancelledError,
- self.loop.run_until_complete, acquire)
+ async def test_acquire_cancel(self):
+ sem = asyncio.Semaphore()
+ await sem.acquire()
+
+ acquire = asyncio.create_task(sem.acquire())
+ asyncio.get_running_loop().call_soon(acquire.cancel)
+ with self.assertRaises(asyncio.CancelledError):
+ await acquire
self.assertTrue((not sem._waiters) or
all(waiter.done() for waiter in sem._waiters))
- def test_acquire_cancel_before_awoken(self):
- with self.assertWarns(DeprecationWarning):
- sem = asyncio.Semaphore(value=0, loop=self.loop)
+ async def test_acquire_cancel_before_awoken(self):
+ sem = asyncio.Semaphore(value=0)
- t1 = self.loop.create_task(sem.acquire())
- t2 = self.loop.create_task(sem.acquire())
- t3 = self.loop.create_task(sem.acquire())
- t4 = self.loop.create_task(sem.acquire())
+ t1 = asyncio.create_task(sem.acquire())
+ t2 = asyncio.create_task(sem.acquire())
+ t3 = asyncio.create_task(sem.acquire())
+ t4 = asyncio.create_task(sem.acquire())
- test_utils.run_briefly(self.loop)
+ await asyncio.sleep(0)
- sem.release()
t1.cancel()
t2.cancel()
+ sem.release()
- test_utils.run_briefly(self.loop)
+ await asyncio.sleep(0)
num_done = sum(t.done() for t in [t3, t4])
self.assertEqual(num_done, 1)
+ self.assertTrue(t3.done())
+ self.assertFalse(t4.done())
t3.cancel()
t4.cancel()
- test_utils.run_briefly(self.loop)
-
- def test_acquire_hang(self):
- with self.assertWarns(DeprecationWarning):
- sem = asyncio.Semaphore(value=0, loop=self.loop)
+ await asyncio.sleep(0)
- t1 = self.loop.create_task(sem.acquire())
- t2 = self.loop.create_task(sem.acquire())
+ async def test_acquire_hang(self):
+ sem = asyncio.Semaphore(value=0)
- test_utils.run_briefly(self.loop)
+ t1 = asyncio.create_task(sem.acquire())
+ t2 = asyncio.create_task(sem.acquire())
+ await asyncio.sleep(0)
- sem.release()
t1.cancel()
-
- test_utils.run_briefly(self.loop)
+ sem.release()
+ await asyncio.sleep(0)
self.assertTrue(sem.locked())
+ self.assertTrue(t2.done())
- def test_release_not_acquired(self):
+ async def test_release_not_acquired(self):
with self.assertWarns(DeprecationWarning):
- sem = asyncio.BoundedSemaphore(loop=self.loop)
+ sem = asyncio.BoundedSemaphore(loop=asyncio.get_running_loop())
self.assertRaises(ValueError, sem.release)
- def test_release_no_waiters(self):
- with self.assertWarns(DeprecationWarning):
- sem = asyncio.Semaphore(loop=self.loop)
- self.loop.run_until_complete(sem.acquire())
+ async def test_release_no_waiters(self):
+ sem = asyncio.Semaphore()
+ await sem.acquire()
self.assertTrue(sem.locked())
sem.release()
diff --git a/Lib/unittest/async_case.py b/Lib/unittest/async_case.py
index 22cb61b7211c6..a2980e797ac5b 100644
--- a/Lib/unittest/async_case.py
+++ b/Lib/unittest/async_case.py
@@ -4,7 +4,6 @@
from .case import TestCase
-
class IsolatedAsyncioTestCase(TestCase):
# Names intentionally have a long prefix
# to reduce a chance of clashing with user-defined attributes
diff --git a/Misc/NEWS.d/next/Tests/2021-12-19-12-20-57.bpo-46129.I3MunH.rst b/Misc/NEWS.d/next/Tests/2021-12-19-12-20-57.bpo-46129.I3MunH.rst
new file mode 100644
index 0000000000000..b06436a4c8460
--- /dev/null
+++ b/Misc/NEWS.d/next/Tests/2021-12-19-12-20-57.bpo-46129.I3MunH.rst
@@ -0,0 +1,2 @@
+Rewrite ``asyncio.locks`` tests with
+:class:`unittest.IsolatedAsyncioTestCase` usage.
1
0
[3.10] bpo-46129: Rewrite asyncio.locks tests with IsolatedAsyncioTestCase (GH-30198) (GH-30202)
by asvetlov Dec. 19, 2021
by asvetlov Dec. 19, 2021
Dec. 19, 2021
https://github.com/python/cpython/commit/cdb4579607ad5e0a3978ec29b8407bea4d…
commit: cdb4579607ad5e0a3978ec29b8407bea4d19b4fa
branch: 3.10
author: Andrew Svetlov <andrew.svetlov(a)gmail.com>
committer: asvetlov <andrew.svetlov(a)gmail.com>
date: 2021-12-19T17:17:40+02:00
summary:
[3.10] bpo-46129: Rewrite asyncio.locks tests with IsolatedAsyncioTestCase (GH-30198) (GH-30202)
Co-authored-by: Kumar Aditya <59607654+kumaraditya303(a)users.noreply.github.com>.
(cherry picked from commit 9c06fd89514a9a2865e2adcc472095f6949cecb2)
Co-authored-by: Andrew Svetlov <andrew.svetlov(a)gmail.com>
files:
A Misc/NEWS.d/next/Tests/2021-12-19-12-20-57.bpo-46129.I3MunH.rst
M Lib/test/test_asyncio/test_locks.py
M Lib/unittest/async_case.py
diff --git a/Lib/test/test_asyncio/test_locks.py b/Lib/test/test_asyncio/test_locks.py
index 623db5fda6437..e2cd2ba0365fd 100644
--- a/Lib/test/test_asyncio/test_locks.py
+++ b/Lib/test/test_asyncio/test_locks.py
@@ -5,7 +5,6 @@
import re
import asyncio
-from test.test_asyncio import utils as test_utils
STR_RGX_REPR = (
r'^<(?P<class>.*?) object at (?P<address>.*?)'
@@ -20,22 +19,18 @@ def tearDownModule():
asyncio.set_event_loop_policy(None)
-class LockTests(test_utils.TestCase):
+class LockTests(unittest.IsolatedAsyncioTestCase):
- def setUp(self):
- super().setUp()
- self.loop = self.new_test_loop()
-
- def test_repr(self):
+ async def test_repr(self):
lock = asyncio.Lock()
self.assertTrue(repr(lock).endswith('[unlocked]>'))
self.assertTrue(RGX_REPR.match(repr(lock)))
- self.loop.run_until_complete(lock.acquire())
+ await lock.acquire()
self.assertTrue(repr(lock).endswith('[locked]>'))
self.assertTrue(RGX_REPR.match(repr(lock)))
- def test_lock(self):
+ async def test_lock(self):
lock = asyncio.Lock()
with self.assertWarns(DeprecationWarning):
@@ -47,11 +42,11 @@ def acquire_lock():
TypeError,
"object is not iterable"
):
- self.loop.run_until_complete(acquire_lock())
+ await acquire_lock()
self.assertFalse(lock.locked())
- def test_lock_doesnt_accept_loop_parameter(self):
+ async def test_lock_doesnt_accept_loop_parameter(self):
primitives_cls = [
asyncio.Lock,
asyncio.Condition,
@@ -60,17 +55,17 @@ def test_lock_doesnt_accept_loop_parameter(self):
asyncio.BoundedSemaphore,
]
+ loop = asyncio.get_running_loop()
+
for cls in primitives_cls:
with self.assertRaisesRegex(
TypeError,
rf'As of 3.10, the \*loop\* parameter was removed from '
rf'{cls.__name__}\(\) since it is no longer necessary'
):
- cls(loop=self.loop)
+ cls(loop=loop)
- def test_lock_by_with_statement(self):
- loop = asyncio.new_event_loop() # don't use TestLoop quirks
- self.set_event_loop(loop)
+ async def test_lock_by_with_statement(self):
primitives = [
asyncio.Lock(),
asyncio.Condition(),
@@ -91,15 +86,15 @@ def test(lock):
pass
self.assertFalse(lock.locked())
- for primitive in primitives:
- loop.run_until_complete(test(primitive))
- self.assertFalse(primitive.locked())
+ for lock in primitives:
+ await test(lock)
+ self.assertFalse(lock.locked())
- def test_acquire(self):
+ async def test_acquire(self):
lock = asyncio.Lock()
result = []
- self.assertTrue(self.loop.run_until_complete(lock.acquire()))
+ self.assertTrue(await lock.acquire())
async def c1(result):
if await lock.acquire():
@@ -116,27 +111,27 @@ async def c3(result):
result.append(3)
return True
- t1 = self.loop.create_task(c1(result))
- t2 = self.loop.create_task(c2(result))
+ t1 = asyncio.create_task(c1(result))
+ t2 = asyncio.create_task(c2(result))
- test_utils.run_briefly(self.loop)
+ await asyncio.sleep(0)
self.assertEqual([], result)
lock.release()
- test_utils.run_briefly(self.loop)
+ await asyncio.sleep(0)
self.assertEqual([1], result)
- test_utils.run_briefly(self.loop)
+ await asyncio.sleep(0)
self.assertEqual([1], result)
- t3 = self.loop.create_task(c3(result))
+ t3 = asyncio.create_task(c3(result))
lock.release()
- test_utils.run_briefly(self.loop)
+ await asyncio.sleep(0)
self.assertEqual([1, 2], result)
lock.release()
- test_utils.run_briefly(self.loop)
+ await asyncio.sleep(0)
self.assertEqual([1, 2, 3], result)
self.assertTrue(t1.done())
@@ -146,18 +141,17 @@ async def c3(result):
self.assertTrue(t3.done())
self.assertTrue(t3.result())
- def test_acquire_cancel(self):
+ async def test_acquire_cancel(self):
lock = asyncio.Lock()
- self.assertTrue(self.loop.run_until_complete(lock.acquire()))
+ self.assertTrue(await lock.acquire())
- task = self.loop.create_task(lock.acquire())
- self.loop.call_soon(task.cancel)
- self.assertRaises(
- asyncio.CancelledError,
- self.loop.run_until_complete, task)
+ task = asyncio.create_task(lock.acquire())
+ asyncio.get_running_loop().call_soon(task.cancel)
+ with self.assertRaises(asyncio.CancelledError):
+ await task
self.assertFalse(lock._waiters)
- def test_cancel_race(self):
+ async def test_cancel_race(self):
# Several tasks:
# - A acquires the lock
# - B is blocked in acquire()
@@ -182,15 +176,15 @@ async def lockit(name, blocker):
finally:
lock.release()
- fa = self.loop.create_future()
- ta = self.loop.create_task(lockit('A', fa))
- test_utils.run_briefly(self.loop)
+ fa = asyncio.get_running_loop().create_future()
+ ta = asyncio.create_task(lockit('A', fa))
+ await asyncio.sleep(0)
self.assertTrue(lock.locked())
- tb = self.loop.create_task(lockit('B', None))
- test_utils.run_briefly(self.loop)
+ tb = asyncio.create_task(lockit('B', None))
+ await asyncio.sleep(0)
self.assertEqual(len(lock._waiters), 1)
- tc = self.loop.create_task(lockit('C', None))
- test_utils.run_briefly(self.loop)
+ tc = asyncio.create_task(lockit('C', None))
+ await asyncio.sleep(0)
self.assertEqual(len(lock._waiters), 2)
# Create the race and check.
@@ -198,16 +192,17 @@ async def lockit(name, blocker):
fa.set_result(None)
tb.cancel()
self.assertTrue(lock._waiters[0].cancelled())
- test_utils.run_briefly(self.loop)
+ await asyncio.sleep(0)
self.assertFalse(lock.locked())
self.assertTrue(ta.done())
self.assertTrue(tb.cancelled())
- self.assertTrue(tc.done())
+ await tc
- def test_cancel_release_race(self):
+ async def test_cancel_release_race(self):
# Issue 32734
# Acquire 4 locks, cancel second, release first
# and 2 locks are taken at once.
+ loop = asyncio.get_running_loop()
lock = asyncio.Lock()
lock_count = 0
call_count = 0
@@ -219,27 +214,23 @@ async def lockit():
await lock.acquire()
lock_count += 1
- async def lockandtrigger():
- await lock.acquire()
- self.loop.call_soon(trigger)
-
def trigger():
t1.cancel()
lock.release()
- t0 = self.loop.create_task(lockandtrigger())
- t1 = self.loop.create_task(lockit())
- t2 = self.loop.create_task(lockit())
- t3 = self.loop.create_task(lockit())
+ await lock.acquire()
- # First loop acquires all
- test_utils.run_briefly(self.loop)
- self.assertTrue(t0.done())
+ t1 = asyncio.create_task(lockit())
+ t2 = asyncio.create_task(lockit())
+ t3 = asyncio.create_task(lockit())
- # Second loop calls trigger
- test_utils.run_briefly(self.loop)
- # Third loop calls cancellation
- test_utils.run_briefly(self.loop)
+ # Start scheduled tasks
+ await asyncio.sleep(0)
+
+ loop.call_soon(trigger)
+ with self.assertRaises(asyncio.CancelledError):
+ # Wait for cancellation
+ await t1
# Make sure only one lock was taken
self.assertEqual(lock_count, 1)
@@ -249,62 +240,56 @@ def trigger():
# Cleanup the task that is stuck on acquire.
t3.cancel()
- test_utils.run_briefly(self.loop)
+ await asyncio.sleep(0)
self.assertTrue(t3.cancelled())
- def test_finished_waiter_cancelled(self):
+ async def test_finished_waiter_cancelled(self):
lock = asyncio.Lock()
- ta = self.loop.create_task(lock.acquire())
- test_utils.run_briefly(self.loop)
+ await lock.acquire()
self.assertTrue(lock.locked())
- tb = self.loop.create_task(lock.acquire())
- test_utils.run_briefly(self.loop)
+ tb = asyncio.create_task(lock.acquire())
+ await asyncio.sleep(0)
self.assertEqual(len(lock._waiters), 1)
# Create a second waiter, wake up the first, and cancel it.
# Without the fix, the second was not woken up.
- tc = self.loop.create_task(lock.acquire())
- lock.release()
+ tc = asyncio.create_task(lock.acquire())
tb.cancel()
- test_utils.run_briefly(self.loop)
+ lock.release()
+ await asyncio.sleep(0)
self.assertTrue(lock.locked())
- self.assertTrue(ta.done())
self.assertTrue(tb.cancelled())
- def test_release_not_acquired(self):
+ # Cleanup
+ await tc
+
+ async def test_release_not_acquired(self):
lock = asyncio.Lock()
self.assertRaises(RuntimeError, lock.release)
- def test_release_no_waiters(self):
+ async def test_release_no_waiters(self):
lock = asyncio.Lock()
- self.loop.run_until_complete(lock.acquire())
+ await lock.acquire()
self.assertTrue(lock.locked())
lock.release()
self.assertFalse(lock.locked())
- def test_context_manager(self):
- async def f():
- lock = asyncio.Lock()
- self.assertFalse(lock.locked())
-
- async with lock:
- self.assertTrue(lock.locked())
-
- self.assertFalse(lock.locked())
+ async def test_context_manager(self):
+ lock = asyncio.Lock()
+ self.assertFalse(lock.locked())
- self.loop.run_until_complete(f())
+ async with lock:
+ self.assertTrue(lock.locked())
+ self.assertFalse(lock.locked())
-class EventTests(test_utils.TestCase):
- def setUp(self):
- super().setUp()
- self.loop = self.new_test_loop()
+class EventTests(unittest.IsolatedAsyncioTestCase):
def test_repr(self):
ev = asyncio.Event()
@@ -320,7 +305,7 @@ def test_repr(self):
self.assertTrue('waiters:1' in repr(ev))
self.assertTrue(RGX_REPR.match(repr(ev)))
- def test_wait(self):
+ async def test_wait(self):
ev = asyncio.Event()
self.assertFalse(ev.is_set())
@@ -338,16 +323,16 @@ async def c3(result):
if await ev.wait():
result.append(3)
- t1 = self.loop.create_task(c1(result))
- t2 = self.loop.create_task(c2(result))
+ t1 = asyncio.create_task(c1(result))
+ t2 = asyncio.create_task(c2(result))
- test_utils.run_briefly(self.loop)
+ await asyncio.sleep(0)
self.assertEqual([], result)
- t3 = self.loop.create_task(c3(result))
+ t3 = asyncio.create_task(c3(result))
ev.set()
- test_utils.run_briefly(self.loop)
+ await asyncio.sleep(0)
self.assertEqual([3, 1, 2], result)
self.assertTrue(t1.done())
@@ -357,24 +342,23 @@ async def c3(result):
self.assertTrue(t3.done())
self.assertIsNone(t3.result())
- def test_wait_on_set(self):
+ async def test_wait_on_set(self):
ev = asyncio.Event()
ev.set()
- res = self.loop.run_until_complete(ev.wait())
+ res = await ev.wait()
self.assertTrue(res)
- def test_wait_cancel(self):
+ async def test_wait_cancel(self):
ev = asyncio.Event()
- wait = self.loop.create_task(ev.wait())
- self.loop.call_soon(wait.cancel)
- self.assertRaises(
- asyncio.CancelledError,
- self.loop.run_until_complete, wait)
+ wait = asyncio.create_task(ev.wait())
+ asyncio.get_running_loop().call_soon(wait.cancel)
+ with self.assertRaises(asyncio.CancelledError):
+ await wait
self.assertFalse(ev._waiters)
- def test_clear(self):
+ async def test_clear(self):
ev = asyncio.Event()
self.assertFalse(ev.is_set())
@@ -384,7 +368,7 @@ def test_clear(self):
ev.clear()
self.assertFalse(ev.is_set())
- def test_clear_with_waiters(self):
+ async def test_clear_with_waiters(self):
ev = asyncio.Event()
result = []
@@ -393,8 +377,8 @@ async def c1(result):
result.append(1)
return True
- t = self.loop.create_task(c1(result))
- test_utils.run_briefly(self.loop)
+ t = asyncio.create_task(c1(result))
+ await asyncio.sleep(0)
self.assertEqual([], result)
ev.set()
@@ -405,7 +389,7 @@ async def c1(result):
ev.set()
self.assertEqual(1, len(ev._waiters))
- test_utils.run_briefly(self.loop)
+ await asyncio.sleep(0)
self.assertEqual([1], result)
self.assertEqual(0, len(ev._waiters))
@@ -413,13 +397,9 @@ async def c1(result):
self.assertTrue(t.result())
-class ConditionTests(test_utils.TestCase):
-
- def setUp(self):
- super().setUp()
- self.loop = self.new_test_loop()
+class ConditionTests(unittest.IsolatedAsyncioTestCase):
- def test_wait(self):
+ async def test_wait(self):
cond = asyncio.Condition()
result = []
@@ -441,37 +421,37 @@ async def c3(result):
result.append(3)
return True
- t1 = self.loop.create_task(c1(result))
- t2 = self.loop.create_task(c2(result))
- t3 = self.loop.create_task(c3(result))
+ t1 = asyncio.create_task(c1(result))
+ t2 = asyncio.create_task(c2(result))
+ t3 = asyncio.create_task(c3(result))
- test_utils.run_briefly(self.loop)
+ await asyncio.sleep(0)
self.assertEqual([], result)
self.assertFalse(cond.locked())
- self.assertTrue(self.loop.run_until_complete(cond.acquire()))
+ self.assertTrue(await cond.acquire())
cond.notify()
- test_utils.run_briefly(self.loop)
+ await asyncio.sleep(0)
self.assertEqual([], result)
self.assertTrue(cond.locked())
cond.release()
- test_utils.run_briefly(self.loop)
+ await asyncio.sleep(0)
self.assertEqual([1], result)
self.assertTrue(cond.locked())
cond.notify(2)
- test_utils.run_briefly(self.loop)
+ await asyncio.sleep(0)
self.assertEqual([1], result)
self.assertTrue(cond.locked())
cond.release()
- test_utils.run_briefly(self.loop)
+ await asyncio.sleep(0)
self.assertEqual([1, 2], result)
self.assertTrue(cond.locked())
cond.release()
- test_utils.run_briefly(self.loop)
+ await asyncio.sleep(0)
self.assertEqual([1, 2, 3], result)
self.assertTrue(cond.locked())
@@ -482,49 +462,47 @@ async def c3(result):
self.assertTrue(t3.done())
self.assertTrue(t3.result())
- def test_wait_cancel(self):
+ async def test_wait_cancel(self):
cond = asyncio.Condition()
- self.loop.run_until_complete(cond.acquire())
+ await cond.acquire()
- wait = self.loop.create_task(cond.wait())
- self.loop.call_soon(wait.cancel)
- self.assertRaises(
- asyncio.CancelledError,
- self.loop.run_until_complete, wait)
+ wait = asyncio.create_task(cond.wait())
+ asyncio.get_running_loop().call_soon(wait.cancel)
+ with self.assertRaises(asyncio.CancelledError):
+ await wait
self.assertFalse(cond._waiters)
self.assertTrue(cond.locked())
- def test_wait_cancel_contested(self):
+ async def test_wait_cancel_contested(self):
cond = asyncio.Condition()
- self.loop.run_until_complete(cond.acquire())
+ await cond.acquire()
self.assertTrue(cond.locked())
- wait_task = self.loop.create_task(cond.wait())
- test_utils.run_briefly(self.loop)
+ wait_task = asyncio.create_task(cond.wait())
+ await asyncio.sleep(0)
self.assertFalse(cond.locked())
# Notify, but contest the lock before cancelling
- self.loop.run_until_complete(cond.acquire())
+ await cond.acquire()
self.assertTrue(cond.locked())
cond.notify()
- self.loop.call_soon(wait_task.cancel)
- self.loop.call_soon(cond.release)
+ asyncio.get_running_loop().call_soon(wait_task.cancel)
+ asyncio.get_running_loop().call_soon(cond.release)
try:
- self.loop.run_until_complete(wait_task)
+ await wait_task
except asyncio.CancelledError:
# Should not happen, since no cancellation points
pass
self.assertTrue(cond.locked())
- def test_wait_cancel_after_notify(self):
+ async def test_wait_cancel_after_notify(self):
# See bpo-32841
waited = False
cond = asyncio.Condition()
- cond._loop = self.loop
async def wait_on_cond():
nonlocal waited
@@ -532,27 +510,26 @@ async def wait_on_cond():
waited = True # Make sure this area was reached
await cond.wait()
- waiter = asyncio.ensure_future(wait_on_cond(), loop=self.loop)
- test_utils.run_briefly(self.loop) # Start waiting
+ waiter = asyncio.create_task(wait_on_cond())
+ await asyncio.sleep(0) # Start waiting
- self.loop.run_until_complete(cond.acquire())
+ await cond.acquire()
cond.notify()
- test_utils.run_briefly(self.loop) # Get to acquire()
+ await asyncio.sleep(0) # Get to acquire()
waiter.cancel()
- test_utils.run_briefly(self.loop) # Activate cancellation
+ await asyncio.sleep(0) # Activate cancellation
cond.release()
- test_utils.run_briefly(self.loop) # Cancellation should occur
+ await asyncio.sleep(0) # Cancellation should occur
self.assertTrue(waiter.cancelled())
self.assertTrue(waited)
- def test_wait_unacquired(self):
+ async def test_wait_unacquired(self):
cond = asyncio.Condition()
- self.assertRaises(
- RuntimeError,
- self.loop.run_until_complete, cond.wait())
+ with self.assertRaises(RuntimeError):
+ await cond.wait()
- def test_wait_for(self):
+ async def test_wait_for(self):
cond = asyncio.Condition()
presult = False
@@ -568,40 +545,38 @@ async def c1(result):
cond.release()
return True
- t = self.loop.create_task(c1(result))
+ t = asyncio.create_task(c1(result))
- test_utils.run_briefly(self.loop)
+ await asyncio.sleep(0)
self.assertEqual([], result)
- self.loop.run_until_complete(cond.acquire())
+ await cond.acquire()
cond.notify()
cond.release()
- test_utils.run_briefly(self.loop)
+ await asyncio.sleep(0)
self.assertEqual([], result)
presult = True
- self.loop.run_until_complete(cond.acquire())
+ await cond.acquire()
cond.notify()
cond.release()
- test_utils.run_briefly(self.loop)
+ await asyncio.sleep(0)
self.assertEqual([1], result)
self.assertTrue(t.done())
self.assertTrue(t.result())
- def test_wait_for_unacquired(self):
+ async def test_wait_for_unacquired(self):
cond = asyncio.Condition()
# predicate can return true immediately
- res = self.loop.run_until_complete(cond.wait_for(lambda: [1, 2, 3]))
+ res = await cond.wait_for(lambda: [1, 2, 3])
self.assertEqual([1, 2, 3], res)
- self.assertRaises(
- RuntimeError,
- self.loop.run_until_complete,
- cond.wait_for(lambda: False))
+ with self.assertRaises(RuntimeError):
+ await cond.wait_for(lambda: False)
- def test_notify(self):
+ async def test_notify(self):
cond = asyncio.Condition()
result = []
@@ -626,24 +601,24 @@ async def c3(result):
cond.release()
return True
- t1 = self.loop.create_task(c1(result))
- t2 = self.loop.create_task(c2(result))
- t3 = self.loop.create_task(c3(result))
+ t1 = asyncio.create_task(c1(result))
+ t2 = asyncio.create_task(c2(result))
+ t3 = asyncio.create_task(c3(result))
- test_utils.run_briefly(self.loop)
+ await asyncio.sleep(0)
self.assertEqual([], result)
- self.loop.run_until_complete(cond.acquire())
+ await cond.acquire()
cond.notify(1)
cond.release()
- test_utils.run_briefly(self.loop)
+ await asyncio.sleep(0)
self.assertEqual([1], result)
- self.loop.run_until_complete(cond.acquire())
+ await cond.acquire()
cond.notify(1)
cond.notify(2048)
cond.release()
- test_utils.run_briefly(self.loop)
+ await asyncio.sleep(0)
self.assertEqual([1, 2, 3], result)
self.assertTrue(t1.done())
@@ -653,7 +628,7 @@ async def c3(result):
self.assertTrue(t3.done())
self.assertTrue(t3.result())
- def test_notify_all(self):
+ async def test_notify_all(self):
cond = asyncio.Condition()
result = []
@@ -672,16 +647,16 @@ async def c2(result):
cond.release()
return True
- t1 = self.loop.create_task(c1(result))
- t2 = self.loop.create_task(c2(result))
+ t1 = asyncio.create_task(c1(result))
+ t2 = asyncio.create_task(c2(result))
- test_utils.run_briefly(self.loop)
+ await asyncio.sleep(0)
self.assertEqual([], result)
- self.loop.run_until_complete(cond.acquire())
+ await cond.acquire()
cond.notify_all()
cond.release()
- test_utils.run_briefly(self.loop)
+ await asyncio.sleep(0)
self.assertEqual([1, 2], result)
self.assertTrue(t1.done())
@@ -697,12 +672,12 @@ def test_notify_all_unacquired(self):
cond = asyncio.Condition()
self.assertRaises(RuntimeError, cond.notify_all)
- def test_repr(self):
+ async def test_repr(self):
cond = asyncio.Condition()
self.assertTrue('unlocked' in repr(cond))
self.assertTrue(RGX_REPR.match(repr(cond)))
- self.loop.run_until_complete(cond.acquire())
+ await cond.acquire()
self.assertTrue('locked' in repr(cond))
cond._waiters.append(mock.Mock())
@@ -713,17 +688,14 @@ def test_repr(self):
self.assertTrue('waiters:2' in repr(cond))
self.assertTrue(RGX_REPR.match(repr(cond)))
- def test_context_manager(self):
- async def f():
- cond = asyncio.Condition()
- self.assertFalse(cond.locked())
- async with cond:
- self.assertTrue(cond.locked())
- self.assertFalse(cond.locked())
-
- self.loop.run_until_complete(f())
+ async def test_context_manager(self):
+ cond = asyncio.Condition()
+ self.assertFalse(cond.locked())
+ async with cond:
+ self.assertTrue(cond.locked())
+ self.assertFalse(cond.locked())
- def test_explicit_lock(self):
+ async def test_explicit_lock(self):
async def f(lock=None, cond=None):
if lock is None:
lock = asyncio.Lock()
@@ -744,12 +716,12 @@ async def f(lock=None, cond=None):
self.assertFalse(cond.locked())
# All should work in the same way.
- self.loop.run_until_complete(f())
- self.loop.run_until_complete(f(asyncio.Lock()))
+ await f()
+ await f(asyncio.Lock())
lock = asyncio.Lock()
- self.loop.run_until_complete(f(lock, asyncio.Condition(lock)))
+ await f(lock, asyncio.Condition(lock))
- def test_ambiguous_loops(self):
+ async def test_ambiguous_loops(self):
loop = asyncio.new_event_loop()
self.addCleanup(loop.close)
@@ -784,38 +756,28 @@ async def wrong_loop_in_cond():
):
await cond.wait()
- self.loop.run_until_complete(wrong_loop_in_lock())
- self.loop.run_until_complete(wrong_loop_in_cond())
-
- def test_timeout_in_block(self):
- loop = asyncio.new_event_loop()
- self.addCleanup(loop.close)
+ await wrong_loop_in_lock()
+ await wrong_loop_in_cond()
- async def task_timeout():
- condition = asyncio.Condition()
- async with condition:
- with self.assertRaises(asyncio.TimeoutError):
- await asyncio.wait_for(condition.wait(), timeout=0.5)
+ async def test_timeout_in_block(self):
+ condition = asyncio.Condition()
+ async with condition:
+ with self.assertRaises(asyncio.TimeoutError):
+ await asyncio.wait_for(condition.wait(), timeout=0.5)
- loop.run_until_complete(task_timeout())
-
-class SemaphoreTests(test_utils.TestCase):
-
- def setUp(self):
- super().setUp()
- self.loop = self.new_test_loop()
+class SemaphoreTests(unittest.IsolatedAsyncioTestCase):
def test_initial_value_zero(self):
sem = asyncio.Semaphore(0)
self.assertTrue(sem.locked())
- def test_repr(self):
+ async def test_repr(self):
sem = asyncio.Semaphore()
self.assertTrue(repr(sem).endswith('[unlocked, value:1]>'))
self.assertTrue(RGX_REPR.match(repr(sem)))
- self.loop.run_until_complete(sem.acquire())
+ await sem.acquire()
self.assertTrue(repr(sem).endswith('[locked]>'))
self.assertTrue('waiters' not in repr(sem))
self.assertTrue(RGX_REPR.match(repr(sem)))
@@ -828,7 +790,7 @@ def test_repr(self):
self.assertTrue('waiters:2' in repr(sem))
self.assertTrue(RGX_REPR.match(repr(sem)))
- def test_semaphore(self):
+ async def test_semaphore(self):
sem = asyncio.Semaphore()
self.assertEqual(1, sem._value)
@@ -841,7 +803,7 @@ def acquire_lock():
TypeError,
"'Semaphore' object is not iterable",
):
- self.loop.run_until_complete(acquire_lock())
+ await acquire_lock()
self.assertFalse(sem.locked())
self.assertEqual(1, sem._value)
@@ -849,12 +811,12 @@ def acquire_lock():
def test_semaphore_value(self):
self.assertRaises(ValueError, asyncio.Semaphore, -1)
- def test_acquire(self):
+ async def test_acquire(self):
sem = asyncio.Semaphore(3)
result = []
- self.assertTrue(self.loop.run_until_complete(sem.acquire()))
- self.assertTrue(self.loop.run_until_complete(sem.acquire()))
+ self.assertTrue(await sem.acquire())
+ self.assertTrue(await sem.acquire())
self.assertFalse(sem.locked())
async def c1(result):
@@ -877,23 +839,23 @@ async def c4(result):
result.append(4)
return True
- t1 = self.loop.create_task(c1(result))
- t2 = self.loop.create_task(c2(result))
- t3 = self.loop.create_task(c3(result))
+ t1 = asyncio.create_task(c1(result))
+ t2 = asyncio.create_task(c2(result))
+ t3 = asyncio.create_task(c3(result))
- test_utils.run_briefly(self.loop)
+ await asyncio.sleep(0)
self.assertEqual([1], result)
self.assertTrue(sem.locked())
self.assertEqual(2, len(sem._waiters))
self.assertEqual(0, sem._value)
- t4 = self.loop.create_task(c4(result))
+ t4 = asyncio.create_task(c4(result))
sem.release()
sem.release()
self.assertEqual(2, sem._value)
- test_utils.run_briefly(self.loop)
+ await asyncio.sleep(0)
self.assertEqual(0, sem._value)
self.assertEqual(3, len(result))
self.assertTrue(sem.locked())
@@ -908,64 +870,64 @@ async def c4(result):
# cleanup locked semaphore
sem.release()
- self.loop.run_until_complete(asyncio.gather(*race_tasks))
+ await asyncio.gather(*race_tasks)
- def test_acquire_cancel(self):
+ async def test_acquire_cancel(self):
sem = asyncio.Semaphore()
- self.loop.run_until_complete(sem.acquire())
+ await sem.acquire()
- acquire = self.loop.create_task(sem.acquire())
- self.loop.call_soon(acquire.cancel)
- self.assertRaises(
- asyncio.CancelledError,
- self.loop.run_until_complete, acquire)
+ acquire = asyncio.create_task(sem.acquire())
+ asyncio.get_running_loop().call_soon(acquire.cancel)
+ with self.assertRaises(asyncio.CancelledError):
+ await acquire
self.assertTrue((not sem._waiters) or
all(waiter.done() for waiter in sem._waiters))
- def test_acquire_cancel_before_awoken(self):
+ async def test_acquire_cancel_before_awoken(self):
sem = asyncio.Semaphore(value=0)
- t1 = self.loop.create_task(sem.acquire())
- t2 = self.loop.create_task(sem.acquire())
- t3 = self.loop.create_task(sem.acquire())
- t4 = self.loop.create_task(sem.acquire())
+ t1 = asyncio.create_task(sem.acquire())
+ t2 = asyncio.create_task(sem.acquire())
+ t3 = asyncio.create_task(sem.acquire())
+ t4 = asyncio.create_task(sem.acquire())
- test_utils.run_briefly(self.loop)
+ await asyncio.sleep(0)
- sem.release()
t1.cancel()
t2.cancel()
+ sem.release()
- test_utils.run_briefly(self.loop)
+ await asyncio.sleep(0)
num_done = sum(t.done() for t in [t3, t4])
self.assertEqual(num_done, 1)
+ self.assertTrue(t3.done())
+ self.assertFalse(t4.done())
t3.cancel()
t4.cancel()
- test_utils.run_briefly(self.loop)
+ await asyncio.sleep(0)
- def test_acquire_hang(self):
+ async def test_acquire_hang(self):
sem = asyncio.Semaphore(value=0)
- t1 = self.loop.create_task(sem.acquire())
- t2 = self.loop.create_task(sem.acquire())
+ t1 = asyncio.create_task(sem.acquire())
+ t2 = asyncio.create_task(sem.acquire())
+ await asyncio.sleep(0)
- test_utils.run_briefly(self.loop)
-
- sem.release()
t1.cancel()
-
- test_utils.run_briefly(self.loop)
+ sem.release()
+ await asyncio.sleep(0)
self.assertTrue(sem.locked())
+ self.assertTrue(t2.done())
def test_release_not_acquired(self):
sem = asyncio.BoundedSemaphore()
self.assertRaises(ValueError, sem.release)
- def test_release_no_waiters(self):
+ async def test_release_no_waiters(self):
sem = asyncio.Semaphore()
- self.loop.run_until_complete(sem.acquire())
+ await sem.acquire()
self.assertTrue(sem.locked())
sem.release()
diff --git a/Lib/unittest/async_case.py b/Lib/unittest/async_case.py
index 4f9a80be80c5a..23231199f9870 100644
--- a/Lib/unittest/async_case.py
+++ b/Lib/unittest/async_case.py
@@ -4,7 +4,6 @@
from .case import TestCase
-
class IsolatedAsyncioTestCase(TestCase):
# Names intentionally have a long prefix
# to reduce a chance of clashing with user-defined attributes
diff --git a/Misc/NEWS.d/next/Tests/2021-12-19-12-20-57.bpo-46129.I3MunH.rst b/Misc/NEWS.d/next/Tests/2021-12-19-12-20-57.bpo-46129.I3MunH.rst
new file mode 100644
index 0000000000000..b06436a4c8460
--- /dev/null
+++ b/Misc/NEWS.d/next/Tests/2021-12-19-12-20-57.bpo-46129.I3MunH.rst
@@ -0,0 +1,2 @@
+Rewrite ``asyncio.locks`` tests with
+:class:`unittest.IsolatedAsyncioTestCase` usage.
1
0
bpo-46123: Disable optimizations for _freeze_module.exe on MSVC for faster building (GH-30181)
by zooba Dec. 19, 2021
by zooba Dec. 19, 2021
Dec. 19, 2021
https://github.com/python/cpython/commit/0b582a4a1b24472a35ed7fc973728ac9d5…
commit: 0b582a4a1b24472a35ed7fc973728ac9d595f123
branch: main
author: neonene <53406459+neonene(a)users.noreply.github.com>
committer: zooba <steve.dower(a)microsoft.com>
date: 2021-12-19T14:55:13Z
summary:
bpo-46123: Disable optimizations for _freeze_module.exe on MSVC for faster building (GH-30181)
files:
M PCbuild/_freeze_module.vcxproj
diff --git a/PCbuild/_freeze_module.vcxproj b/PCbuild/_freeze_module.vcxproj
index 42798bf8113c7..59519cade2670 100644
--- a/PCbuild/_freeze_module.vcxproj
+++ b/PCbuild/_freeze_module.vcxproj
@@ -89,10 +89,13 @@
<ItemDefinitionGroup>
<ClCompile>
<PreprocessorDefinitions>Py_NO_ENABLE_SHARED;Py_BUILD_CORE;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
+ <Optimization>Disabled</Optimization>
+ <WholeProgramOptimization>false</WholeProgramOptimization>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<AdditionalDependencies>version.lib;ws2_32.lib;pathcch.lib;bcrypt.lib;%(AdditionalDependencies)</AdditionalDependencies>
+ <LinkTimeCodeGeneration>Default</LinkTimeCodeGeneration>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
1
0
bpo-46129: Rewrite asyncio.locks tests with IsolatedAsyncioTestCase (GH-30198)
by asvetlov Dec. 19, 2021
by asvetlov Dec. 19, 2021
Dec. 19, 2021
https://github.com/python/cpython/commit/9c06fd89514a9a2865e2adcc472095f694…
commit: 9c06fd89514a9a2865e2adcc472095f6949cecb2
branch: main
author: Andrew Svetlov <andrew.svetlov(a)gmail.com>
committer: asvetlov <andrew.svetlov(a)gmail.com>
date: 2021-12-19T16:35:56+02:00
summary:
bpo-46129: Rewrite asyncio.locks tests with IsolatedAsyncioTestCase (GH-30198)
Co-authored-by: Kumar Aditya <59607654+kumaraditya303(a)users.noreply.github.com>
files:
A Misc/NEWS.d/next/Tests/2021-12-19-12-20-57.bpo-46129.I3MunH.rst
M Lib/test/test_asyncio/test_locks.py
M Lib/unittest/async_case.py
diff --git a/Lib/test/test_asyncio/test_locks.py b/Lib/test/test_asyncio/test_locks.py
index b2492c1acfece..4ce338774f748 100644
--- a/Lib/test/test_asyncio/test_locks.py
+++ b/Lib/test/test_asyncio/test_locks.py
@@ -5,7 +5,6 @@
import re
import asyncio
-from test.test_asyncio import utils as test_utils
STR_RGX_REPR = (
r'^<(?P<class>.*?) object at (?P<address>.*?)'
@@ -20,36 +19,29 @@ def tearDownModule():
asyncio.set_event_loop_policy(None)
-class LockTests(test_utils.TestCase):
+class LockTests(unittest.IsolatedAsyncioTestCase):
- def setUp(self):
- super().setUp()
- self.loop = self.new_test_loop()
-
- def test_repr(self):
+ async def test_repr(self):
lock = asyncio.Lock()
self.assertTrue(repr(lock).endswith('[unlocked]>'))
self.assertTrue(RGX_REPR.match(repr(lock)))
- self.loop.run_until_complete(lock.acquire())
+ await lock.acquire()
self.assertTrue(repr(lock).endswith('[locked]>'))
self.assertTrue(RGX_REPR.match(repr(lock)))
- def test_lock(self):
+ async def test_lock(self):
lock = asyncio.Lock()
- async def acquire_lock():
- return await lock
-
with self.assertRaisesRegex(
TypeError,
"object Lock can't be used in 'await' expression"
):
- self.loop.run_until_complete(acquire_lock())
+ await lock
self.assertFalse(lock.locked())
- def test_lock_doesnt_accept_loop_parameter(self):
+ async def test_lock_doesnt_accept_loop_parameter(self):
primitives_cls = [
asyncio.Lock,
asyncio.Condition,
@@ -58,17 +50,17 @@ def test_lock_doesnt_accept_loop_parameter(self):
asyncio.BoundedSemaphore,
]
+ loop = asyncio.get_running_loop()
+
for cls in primitives_cls:
with self.assertRaisesRegex(
TypeError,
rf'As of 3.10, the \*loop\* parameter was removed from '
rf'{cls.__name__}\(\) since it is no longer necessary'
):
- cls(loop=self.loop)
+ cls(loop=loop)
- def test_lock_by_with_statement(self):
- loop = asyncio.new_event_loop() # don't use TestLoop quirks
- self.set_event_loop(loop)
+ async def test_lock_by_with_statement(self):
primitives = [
asyncio.Lock(),
asyncio.Condition(),
@@ -76,7 +68,7 @@ def test_lock_by_with_statement(self):
asyncio.BoundedSemaphore(),
]
- async def test(lock):
+ for lock in primitives:
await asyncio.sleep(0.01)
self.assertFalse(lock.locked())
with self.assertRaisesRegex(
@@ -87,15 +79,11 @@ async def test(lock):
pass
self.assertFalse(lock.locked())
- for primitive in primitives:
- loop.run_until_complete(test(primitive))
- self.assertFalse(primitive.locked())
-
- def test_acquire(self):
+ async def test_acquire(self):
lock = asyncio.Lock()
result = []
- self.assertTrue(self.loop.run_until_complete(lock.acquire()))
+ self.assertTrue(await lock.acquire())
async def c1(result):
if await lock.acquire():
@@ -112,27 +100,27 @@ async def c3(result):
result.append(3)
return True
- t1 = self.loop.create_task(c1(result))
- t2 = self.loop.create_task(c2(result))
+ t1 = asyncio.create_task(c1(result))
+ t2 = asyncio.create_task(c2(result))
- test_utils.run_briefly(self.loop)
+ await asyncio.sleep(0)
self.assertEqual([], result)
lock.release()
- test_utils.run_briefly(self.loop)
+ await asyncio.sleep(0)
self.assertEqual([1], result)
- test_utils.run_briefly(self.loop)
+ await asyncio.sleep(0)
self.assertEqual([1], result)
- t3 = self.loop.create_task(c3(result))
+ t3 = asyncio.create_task(c3(result))
lock.release()
- test_utils.run_briefly(self.loop)
+ await asyncio.sleep(0)
self.assertEqual([1, 2], result)
lock.release()
- test_utils.run_briefly(self.loop)
+ await asyncio.sleep(0)
self.assertEqual([1, 2, 3], result)
self.assertTrue(t1.done())
@@ -142,18 +130,17 @@ async def c3(result):
self.assertTrue(t3.done())
self.assertTrue(t3.result())
- def test_acquire_cancel(self):
+ async def test_acquire_cancel(self):
lock = asyncio.Lock()
- self.assertTrue(self.loop.run_until_complete(lock.acquire()))
+ self.assertTrue(await lock.acquire())
- task = self.loop.create_task(lock.acquire())
- self.loop.call_soon(task.cancel)
- self.assertRaises(
- asyncio.CancelledError,
- self.loop.run_until_complete, task)
+ task = asyncio.create_task(lock.acquire())
+ asyncio.get_running_loop().call_soon(task.cancel)
+ with self.assertRaises(asyncio.CancelledError):
+ await task
self.assertFalse(lock._waiters)
- def test_cancel_race(self):
+ async def test_cancel_race(self):
# Several tasks:
# - A acquires the lock
# - B is blocked in acquire()
@@ -178,15 +165,15 @@ async def lockit(name, blocker):
finally:
lock.release()
- fa = self.loop.create_future()
- ta = self.loop.create_task(lockit('A', fa))
- test_utils.run_briefly(self.loop)
+ fa = asyncio.get_running_loop().create_future()
+ ta = asyncio.create_task(lockit('A', fa))
+ await asyncio.sleep(0)
self.assertTrue(lock.locked())
- tb = self.loop.create_task(lockit('B', None))
- test_utils.run_briefly(self.loop)
+ tb = asyncio.create_task(lockit('B', None))
+ await asyncio.sleep(0)
self.assertEqual(len(lock._waiters), 1)
- tc = self.loop.create_task(lockit('C', None))
- test_utils.run_briefly(self.loop)
+ tc = asyncio.create_task(lockit('C', None))
+ await asyncio.sleep(0)
self.assertEqual(len(lock._waiters), 2)
# Create the race and check.
@@ -194,16 +181,17 @@ async def lockit(name, blocker):
fa.set_result(None)
tb.cancel()
self.assertTrue(lock._waiters[0].cancelled())
- test_utils.run_briefly(self.loop)
+ await asyncio.sleep(0)
self.assertFalse(lock.locked())
self.assertTrue(ta.done())
self.assertTrue(tb.cancelled())
- self.assertTrue(tc.done())
+ await tc
- def test_cancel_release_race(self):
+ async def test_cancel_release_race(self):
# Issue 32734
# Acquire 4 locks, cancel second, release first
# and 2 locks are taken at once.
+ loop = asyncio.get_running_loop()
lock = asyncio.Lock()
lock_count = 0
call_count = 0
@@ -215,27 +203,23 @@ async def lockit():
await lock.acquire()
lock_count += 1
- async def lockandtrigger():
- await lock.acquire()
- self.loop.call_soon(trigger)
-
def trigger():
t1.cancel()
lock.release()
- t0 = self.loop.create_task(lockandtrigger())
- t1 = self.loop.create_task(lockit())
- t2 = self.loop.create_task(lockit())
- t3 = self.loop.create_task(lockit())
+ await lock.acquire()
+
+ t1 = asyncio.create_task(lockit())
+ t2 = asyncio.create_task(lockit())
+ t3 = asyncio.create_task(lockit())
- # First loop acquires all
- test_utils.run_briefly(self.loop)
- self.assertTrue(t0.done())
+ # Start scheduled tasks
+ await asyncio.sleep(0)
- # Second loop calls trigger
- test_utils.run_briefly(self.loop)
- # Third loop calls cancellation
- test_utils.run_briefly(self.loop)
+ loop.call_soon(trigger)
+ with self.assertRaises(asyncio.CancelledError):
+ # Wait for cancellation
+ await t1
# Make sure only one lock was taken
self.assertEqual(lock_count, 1)
@@ -245,62 +229,56 @@ def trigger():
# Cleanup the task that is stuck on acquire.
t3.cancel()
- test_utils.run_briefly(self.loop)
+ await asyncio.sleep(0)
self.assertTrue(t3.cancelled())
- def test_finished_waiter_cancelled(self):
+ async def test_finished_waiter_cancelled(self):
lock = asyncio.Lock()
- ta = self.loop.create_task(lock.acquire())
- test_utils.run_briefly(self.loop)
+ await lock.acquire()
self.assertTrue(lock.locked())
- tb = self.loop.create_task(lock.acquire())
- test_utils.run_briefly(self.loop)
+ tb = asyncio.create_task(lock.acquire())
+ await asyncio.sleep(0)
self.assertEqual(len(lock._waiters), 1)
# Create a second waiter, wake up the first, and cancel it.
# Without the fix, the second was not woken up.
- tc = self.loop.create_task(lock.acquire())
- lock.release()
+ tc = asyncio.create_task(lock.acquire())
tb.cancel()
- test_utils.run_briefly(self.loop)
+ lock.release()
+ await asyncio.sleep(0)
self.assertTrue(lock.locked())
- self.assertTrue(ta.done())
self.assertTrue(tb.cancelled())
- def test_release_not_acquired(self):
+ # Cleanup
+ await tc
+
+ async def test_release_not_acquired(self):
lock = asyncio.Lock()
self.assertRaises(RuntimeError, lock.release)
- def test_release_no_waiters(self):
+ async def test_release_no_waiters(self):
lock = asyncio.Lock()
- self.loop.run_until_complete(lock.acquire())
+ await lock.acquire()
self.assertTrue(lock.locked())
lock.release()
self.assertFalse(lock.locked())
- def test_context_manager(self):
- async def f():
- lock = asyncio.Lock()
- self.assertFalse(lock.locked())
-
- async with lock:
- self.assertTrue(lock.locked())
-
- self.assertFalse(lock.locked())
+ async def test_context_manager(self):
+ lock = asyncio.Lock()
+ self.assertFalse(lock.locked())
- self.loop.run_until_complete(f())
+ async with lock:
+ self.assertTrue(lock.locked())
+ self.assertFalse(lock.locked())
-class EventTests(test_utils.TestCase):
- def setUp(self):
- super().setUp()
- self.loop = self.new_test_loop()
+class EventTests(unittest.IsolatedAsyncioTestCase):
def test_repr(self):
ev = asyncio.Event()
@@ -316,7 +294,7 @@ def test_repr(self):
self.assertTrue('waiters:1' in repr(ev))
self.assertTrue(RGX_REPR.match(repr(ev)))
- def test_wait(self):
+ async def test_wait(self):
ev = asyncio.Event()
self.assertFalse(ev.is_set())
@@ -334,16 +312,16 @@ async def c3(result):
if await ev.wait():
result.append(3)
- t1 = self.loop.create_task(c1(result))
- t2 = self.loop.create_task(c2(result))
+ t1 = asyncio.create_task(c1(result))
+ t2 = asyncio.create_task(c2(result))
- test_utils.run_briefly(self.loop)
+ await asyncio.sleep(0)
self.assertEqual([], result)
- t3 = self.loop.create_task(c3(result))
+ t3 = asyncio.create_task(c3(result))
ev.set()
- test_utils.run_briefly(self.loop)
+ await asyncio.sleep(0)
self.assertEqual([3, 1, 2], result)
self.assertTrue(t1.done())
@@ -353,24 +331,23 @@ async def c3(result):
self.assertTrue(t3.done())
self.assertIsNone(t3.result())
- def test_wait_on_set(self):
+ async def test_wait_on_set(self):
ev = asyncio.Event()
ev.set()
- res = self.loop.run_until_complete(ev.wait())
+ res = await ev.wait()
self.assertTrue(res)
- def test_wait_cancel(self):
+ async def test_wait_cancel(self):
ev = asyncio.Event()
- wait = self.loop.create_task(ev.wait())
- self.loop.call_soon(wait.cancel)
- self.assertRaises(
- asyncio.CancelledError,
- self.loop.run_until_complete, wait)
+ wait = asyncio.create_task(ev.wait())
+ asyncio.get_running_loop().call_soon(wait.cancel)
+ with self.assertRaises(asyncio.CancelledError):
+ await wait
self.assertFalse(ev._waiters)
- def test_clear(self):
+ async def test_clear(self):
ev = asyncio.Event()
self.assertFalse(ev.is_set())
@@ -380,7 +357,7 @@ def test_clear(self):
ev.clear()
self.assertFalse(ev.is_set())
- def test_clear_with_waiters(self):
+ async def test_clear_with_waiters(self):
ev = asyncio.Event()
result = []
@@ -389,8 +366,8 @@ async def c1(result):
result.append(1)
return True
- t = self.loop.create_task(c1(result))
- test_utils.run_briefly(self.loop)
+ t = asyncio.create_task(c1(result))
+ await asyncio.sleep(0)
self.assertEqual([], result)
ev.set()
@@ -401,7 +378,7 @@ async def c1(result):
ev.set()
self.assertEqual(1, len(ev._waiters))
- test_utils.run_briefly(self.loop)
+ await asyncio.sleep(0)
self.assertEqual([1], result)
self.assertEqual(0, len(ev._waiters))
@@ -409,13 +386,9 @@ async def c1(result):
self.assertTrue(t.result())
-class ConditionTests(test_utils.TestCase):
-
- def setUp(self):
- super().setUp()
- self.loop = self.new_test_loop()
+class ConditionTests(unittest.IsolatedAsyncioTestCase):
- def test_wait(self):
+ async def test_wait(self):
cond = asyncio.Condition()
result = []
@@ -437,37 +410,37 @@ async def c3(result):
result.append(3)
return True
- t1 = self.loop.create_task(c1(result))
- t2 = self.loop.create_task(c2(result))
- t3 = self.loop.create_task(c3(result))
+ t1 = asyncio.create_task(c1(result))
+ t2 = asyncio.create_task(c2(result))
+ t3 = asyncio.create_task(c3(result))
- test_utils.run_briefly(self.loop)
+ await asyncio.sleep(0)
self.assertEqual([], result)
self.assertFalse(cond.locked())
- self.assertTrue(self.loop.run_until_complete(cond.acquire()))
+ self.assertTrue(await cond.acquire())
cond.notify()
- test_utils.run_briefly(self.loop)
+ await asyncio.sleep(0)
self.assertEqual([], result)
self.assertTrue(cond.locked())
cond.release()
- test_utils.run_briefly(self.loop)
+ await asyncio.sleep(0)
self.assertEqual([1], result)
self.assertTrue(cond.locked())
cond.notify(2)
- test_utils.run_briefly(self.loop)
+ await asyncio.sleep(0)
self.assertEqual([1], result)
self.assertTrue(cond.locked())
cond.release()
- test_utils.run_briefly(self.loop)
+ await asyncio.sleep(0)
self.assertEqual([1, 2], result)
self.assertTrue(cond.locked())
cond.release()
- test_utils.run_briefly(self.loop)
+ await asyncio.sleep(0)
self.assertEqual([1, 2, 3], result)
self.assertTrue(cond.locked())
@@ -478,49 +451,47 @@ async def c3(result):
self.assertTrue(t3.done())
self.assertTrue(t3.result())
- def test_wait_cancel(self):
+ async def test_wait_cancel(self):
cond = asyncio.Condition()
- self.loop.run_until_complete(cond.acquire())
+ await cond.acquire()
- wait = self.loop.create_task(cond.wait())
- self.loop.call_soon(wait.cancel)
- self.assertRaises(
- asyncio.CancelledError,
- self.loop.run_until_complete, wait)
+ wait = asyncio.create_task(cond.wait())
+ asyncio.get_running_loop().call_soon(wait.cancel)
+ with self.assertRaises(asyncio.CancelledError):
+ await wait
self.assertFalse(cond._waiters)
self.assertTrue(cond.locked())
- def test_wait_cancel_contested(self):
+ async def test_wait_cancel_contested(self):
cond = asyncio.Condition()
- self.loop.run_until_complete(cond.acquire())
+ await cond.acquire()
self.assertTrue(cond.locked())
- wait_task = self.loop.create_task(cond.wait())
- test_utils.run_briefly(self.loop)
+ wait_task = asyncio.create_task(cond.wait())
+ await asyncio.sleep(0)
self.assertFalse(cond.locked())
# Notify, but contest the lock before cancelling
- self.loop.run_until_complete(cond.acquire())
+ await cond.acquire()
self.assertTrue(cond.locked())
cond.notify()
- self.loop.call_soon(wait_task.cancel)
- self.loop.call_soon(cond.release)
+ asyncio.get_running_loop().call_soon(wait_task.cancel)
+ asyncio.get_running_loop().call_soon(cond.release)
try:
- self.loop.run_until_complete(wait_task)
+ await wait_task
except asyncio.CancelledError:
# Should not happen, since no cancellation points
pass
self.assertTrue(cond.locked())
- def test_wait_cancel_after_notify(self):
+ async def test_wait_cancel_after_notify(self):
# See bpo-32841
waited = False
cond = asyncio.Condition()
- cond._loop = self.loop
async def wait_on_cond():
nonlocal waited
@@ -528,27 +499,26 @@ async def wait_on_cond():
waited = True # Make sure this area was reached
await cond.wait()
- waiter = asyncio.ensure_future(wait_on_cond(), loop=self.loop)
- test_utils.run_briefly(self.loop) # Start waiting
+ waiter = asyncio.create_task(wait_on_cond())
+ await asyncio.sleep(0) # Start waiting
- self.loop.run_until_complete(cond.acquire())
+ await cond.acquire()
cond.notify()
- test_utils.run_briefly(self.loop) # Get to acquire()
+ await asyncio.sleep(0) # Get to acquire()
waiter.cancel()
- test_utils.run_briefly(self.loop) # Activate cancellation
+ await asyncio.sleep(0) # Activate cancellation
cond.release()
- test_utils.run_briefly(self.loop) # Cancellation should occur
+ await asyncio.sleep(0) # Cancellation should occur
self.assertTrue(waiter.cancelled())
self.assertTrue(waited)
- def test_wait_unacquired(self):
+ async def test_wait_unacquired(self):
cond = asyncio.Condition()
- self.assertRaises(
- RuntimeError,
- self.loop.run_until_complete, cond.wait())
+ with self.assertRaises(RuntimeError):
+ await cond.wait()
- def test_wait_for(self):
+ async def test_wait_for(self):
cond = asyncio.Condition()
presult = False
@@ -564,40 +534,38 @@ async def c1(result):
cond.release()
return True
- t = self.loop.create_task(c1(result))
+ t = asyncio.create_task(c1(result))
- test_utils.run_briefly(self.loop)
+ await asyncio.sleep(0)
self.assertEqual([], result)
- self.loop.run_until_complete(cond.acquire())
+ await cond.acquire()
cond.notify()
cond.release()
- test_utils.run_briefly(self.loop)
+ await asyncio.sleep(0)
self.assertEqual([], result)
presult = True
- self.loop.run_until_complete(cond.acquire())
+ await cond.acquire()
cond.notify()
cond.release()
- test_utils.run_briefly(self.loop)
+ await asyncio.sleep(0)
self.assertEqual([1], result)
self.assertTrue(t.done())
self.assertTrue(t.result())
- def test_wait_for_unacquired(self):
+ async def test_wait_for_unacquired(self):
cond = asyncio.Condition()
# predicate can return true immediately
- res = self.loop.run_until_complete(cond.wait_for(lambda: [1, 2, 3]))
+ res = await cond.wait_for(lambda: [1, 2, 3])
self.assertEqual([1, 2, 3], res)
- self.assertRaises(
- RuntimeError,
- self.loop.run_until_complete,
- cond.wait_for(lambda: False))
+ with self.assertRaises(RuntimeError):
+ await cond.wait_for(lambda: False)
- def test_notify(self):
+ async def test_notify(self):
cond = asyncio.Condition()
result = []
@@ -622,24 +590,24 @@ async def c3(result):
cond.release()
return True
- t1 = self.loop.create_task(c1(result))
- t2 = self.loop.create_task(c2(result))
- t3 = self.loop.create_task(c3(result))
+ t1 = asyncio.create_task(c1(result))
+ t2 = asyncio.create_task(c2(result))
+ t3 = asyncio.create_task(c3(result))
- test_utils.run_briefly(self.loop)
+ await asyncio.sleep(0)
self.assertEqual([], result)
- self.loop.run_until_complete(cond.acquire())
+ await cond.acquire()
cond.notify(1)
cond.release()
- test_utils.run_briefly(self.loop)
+ await asyncio.sleep(0)
self.assertEqual([1], result)
- self.loop.run_until_complete(cond.acquire())
+ await cond.acquire()
cond.notify(1)
cond.notify(2048)
cond.release()
- test_utils.run_briefly(self.loop)
+ await asyncio.sleep(0)
self.assertEqual([1, 2, 3], result)
self.assertTrue(t1.done())
@@ -649,7 +617,7 @@ async def c3(result):
self.assertTrue(t3.done())
self.assertTrue(t3.result())
- def test_notify_all(self):
+ async def test_notify_all(self):
cond = asyncio.Condition()
result = []
@@ -668,16 +636,16 @@ async def c2(result):
cond.release()
return True
- t1 = self.loop.create_task(c1(result))
- t2 = self.loop.create_task(c2(result))
+ t1 = asyncio.create_task(c1(result))
+ t2 = asyncio.create_task(c2(result))
- test_utils.run_briefly(self.loop)
+ await asyncio.sleep(0)
self.assertEqual([], result)
- self.loop.run_until_complete(cond.acquire())
+ await cond.acquire()
cond.notify_all()
cond.release()
- test_utils.run_briefly(self.loop)
+ await asyncio.sleep(0)
self.assertEqual([1, 2], result)
self.assertTrue(t1.done())
@@ -693,12 +661,12 @@ def test_notify_all_unacquired(self):
cond = asyncio.Condition()
self.assertRaises(RuntimeError, cond.notify_all)
- def test_repr(self):
+ async def test_repr(self):
cond = asyncio.Condition()
self.assertTrue('unlocked' in repr(cond))
self.assertTrue(RGX_REPR.match(repr(cond)))
- self.loop.run_until_complete(cond.acquire())
+ await cond.acquire()
self.assertTrue('locked' in repr(cond))
cond._waiters.append(mock.Mock())
@@ -709,17 +677,14 @@ def test_repr(self):
self.assertTrue('waiters:2' in repr(cond))
self.assertTrue(RGX_REPR.match(repr(cond)))
- def test_context_manager(self):
- async def f():
- cond = asyncio.Condition()
- self.assertFalse(cond.locked())
- async with cond:
- self.assertTrue(cond.locked())
- self.assertFalse(cond.locked())
-
- self.loop.run_until_complete(f())
+ async def test_context_manager(self):
+ cond = asyncio.Condition()
+ self.assertFalse(cond.locked())
+ async with cond:
+ self.assertTrue(cond.locked())
+ self.assertFalse(cond.locked())
- def test_explicit_lock(self):
+ async def test_explicit_lock(self):
async def f(lock=None, cond=None):
if lock is None:
lock = asyncio.Lock()
@@ -740,12 +705,12 @@ async def f(lock=None, cond=None):
self.assertFalse(cond.locked())
# All should work in the same way.
- self.loop.run_until_complete(f())
- self.loop.run_until_complete(f(asyncio.Lock()))
+ await f()
+ await f(asyncio.Lock())
lock = asyncio.Lock()
- self.loop.run_until_complete(f(lock, asyncio.Condition(lock)))
+ await f(lock, asyncio.Condition(lock))
- def test_ambiguous_loops(self):
+ async def test_ambiguous_loops(self):
loop = asyncio.new_event_loop()
self.addCleanup(loop.close)
@@ -780,38 +745,28 @@ async def wrong_loop_in_cond():
):
await cond.wait()
- self.loop.run_until_complete(wrong_loop_in_lock())
- self.loop.run_until_complete(wrong_loop_in_cond())
-
- def test_timeout_in_block(self):
- loop = asyncio.new_event_loop()
- self.addCleanup(loop.close)
-
- async def task_timeout():
- condition = asyncio.Condition()
- async with condition:
- with self.assertRaises(asyncio.TimeoutError):
- await asyncio.wait_for(condition.wait(), timeout=0.5)
-
- loop.run_until_complete(task_timeout())
+ await wrong_loop_in_lock()
+ await wrong_loop_in_cond()
+ async def test_timeout_in_block(self):
+ condition = asyncio.Condition()
+ async with condition:
+ with self.assertRaises(asyncio.TimeoutError):
+ await asyncio.wait_for(condition.wait(), timeout=0.5)
-class SemaphoreTests(test_utils.TestCase):
- def setUp(self):
- super().setUp()
- self.loop = self.new_test_loop()
+class SemaphoreTests(unittest.IsolatedAsyncioTestCase):
def test_initial_value_zero(self):
sem = asyncio.Semaphore(0)
self.assertTrue(sem.locked())
- def test_repr(self):
+ async def test_repr(self):
sem = asyncio.Semaphore()
self.assertTrue(repr(sem).endswith('[unlocked, value:1]>'))
self.assertTrue(RGX_REPR.match(repr(sem)))
- self.loop.run_until_complete(sem.acquire())
+ await sem.acquire()
self.assertTrue(repr(sem).endswith('[locked]>'))
self.assertTrue('waiters' not in repr(sem))
self.assertTrue(RGX_REPR.match(repr(sem)))
@@ -824,18 +779,15 @@ def test_repr(self):
self.assertTrue('waiters:2' in repr(sem))
self.assertTrue(RGX_REPR.match(repr(sem)))
- def test_semaphore(self):
+ async def test_semaphore(self):
sem = asyncio.Semaphore()
self.assertEqual(1, sem._value)
- async def acquire_lock():
- return await sem
-
with self.assertRaisesRegex(
TypeError,
"object Semaphore can't be used in 'await' expression",
):
- self.loop.run_until_complete(acquire_lock())
+ await sem
self.assertFalse(sem.locked())
self.assertEqual(1, sem._value)
@@ -843,12 +795,12 @@ async def acquire_lock():
def test_semaphore_value(self):
self.assertRaises(ValueError, asyncio.Semaphore, -1)
- def test_acquire(self):
+ async def test_acquire(self):
sem = asyncio.Semaphore(3)
result = []
- self.assertTrue(self.loop.run_until_complete(sem.acquire()))
- self.assertTrue(self.loop.run_until_complete(sem.acquire()))
+ self.assertTrue(await sem.acquire())
+ self.assertTrue(await sem.acquire())
self.assertFalse(sem.locked())
async def c1(result):
@@ -871,23 +823,23 @@ async def c4(result):
result.append(4)
return True
- t1 = self.loop.create_task(c1(result))
- t2 = self.loop.create_task(c2(result))
- t3 = self.loop.create_task(c3(result))
+ t1 = asyncio.create_task(c1(result))
+ t2 = asyncio.create_task(c2(result))
+ t3 = asyncio.create_task(c3(result))
- test_utils.run_briefly(self.loop)
+ await asyncio.sleep(0)
self.assertEqual([1], result)
self.assertTrue(sem.locked())
self.assertEqual(2, len(sem._waiters))
self.assertEqual(0, sem._value)
- t4 = self.loop.create_task(c4(result))
+ t4 = asyncio.create_task(c4(result))
sem.release()
sem.release()
self.assertEqual(2, sem._value)
- test_utils.run_briefly(self.loop)
+ await asyncio.sleep(0)
self.assertEqual(0, sem._value)
self.assertEqual(3, len(result))
self.assertTrue(sem.locked())
@@ -902,64 +854,64 @@ async def c4(result):
# cleanup locked semaphore
sem.release()
- self.loop.run_until_complete(asyncio.gather(*race_tasks))
+ await asyncio.gather(*race_tasks)
- def test_acquire_cancel(self):
+ async def test_acquire_cancel(self):
sem = asyncio.Semaphore()
- self.loop.run_until_complete(sem.acquire())
+ await sem.acquire()
- acquire = self.loop.create_task(sem.acquire())
- self.loop.call_soon(acquire.cancel)
- self.assertRaises(
- asyncio.CancelledError,
- self.loop.run_until_complete, acquire)
+ acquire = asyncio.create_task(sem.acquire())
+ asyncio.get_running_loop().call_soon(acquire.cancel)
+ with self.assertRaises(asyncio.CancelledError):
+ await acquire
self.assertTrue((not sem._waiters) or
all(waiter.done() for waiter in sem._waiters))
- def test_acquire_cancel_before_awoken(self):
+ async def test_acquire_cancel_before_awoken(self):
sem = asyncio.Semaphore(value=0)
- t1 = self.loop.create_task(sem.acquire())
- t2 = self.loop.create_task(sem.acquire())
- t3 = self.loop.create_task(sem.acquire())
- t4 = self.loop.create_task(sem.acquire())
+ t1 = asyncio.create_task(sem.acquire())
+ t2 = asyncio.create_task(sem.acquire())
+ t3 = asyncio.create_task(sem.acquire())
+ t4 = asyncio.create_task(sem.acquire())
- test_utils.run_briefly(self.loop)
+ await asyncio.sleep(0)
- sem.release()
t1.cancel()
t2.cancel()
+ sem.release()
- test_utils.run_briefly(self.loop)
+ await asyncio.sleep(0)
num_done = sum(t.done() for t in [t3, t4])
self.assertEqual(num_done, 1)
+ self.assertTrue(t3.done())
+ self.assertFalse(t4.done())
t3.cancel()
t4.cancel()
- test_utils.run_briefly(self.loop)
+ await asyncio.sleep(0)
- def test_acquire_hang(self):
+ async def test_acquire_hang(self):
sem = asyncio.Semaphore(value=0)
- t1 = self.loop.create_task(sem.acquire())
- t2 = self.loop.create_task(sem.acquire())
-
- test_utils.run_briefly(self.loop)
+ t1 = asyncio.create_task(sem.acquire())
+ t2 = asyncio.create_task(sem.acquire())
+ await asyncio.sleep(0)
- sem.release()
t1.cancel()
-
- test_utils.run_briefly(self.loop)
+ sem.release()
+ await asyncio.sleep(0)
self.assertTrue(sem.locked())
+ self.assertTrue(t2.done())
def test_release_not_acquired(self):
sem = asyncio.BoundedSemaphore()
self.assertRaises(ValueError, sem.release)
- def test_release_no_waiters(self):
+ async def test_release_no_waiters(self):
sem = asyncio.Semaphore()
- self.loop.run_until_complete(sem.acquire())
+ await sem.acquire()
self.assertTrue(sem.locked())
sem.release()
diff --git a/Lib/unittest/async_case.py b/Lib/unittest/async_case.py
index d8bfaf6b67e00..3c57bb5cda2c0 100644
--- a/Lib/unittest/async_case.py
+++ b/Lib/unittest/async_case.py
@@ -5,7 +5,6 @@
from .case import TestCase
-
class IsolatedAsyncioTestCase(TestCase):
# Names intentionally have a long prefix
# to reduce a chance of clashing with user-defined attributes
diff --git a/Misc/NEWS.d/next/Tests/2021-12-19-12-20-57.bpo-46129.I3MunH.rst b/Misc/NEWS.d/next/Tests/2021-12-19-12-20-57.bpo-46129.I3MunH.rst
new file mode 100644
index 0000000000000..b06436a4c8460
--- /dev/null
+++ b/Misc/NEWS.d/next/Tests/2021-12-19-12-20-57.bpo-46129.I3MunH.rst
@@ -0,0 +1,2 @@
+Rewrite ``asyncio.locks`` tests with
+:class:`unittest.IsolatedAsyncioTestCase` usage.
1
0
bpo-46130: [docs] Add anchor for whatsnew/3.10 type hint section (GH-30199)
by miss-islington Dec. 19, 2021
by miss-islington Dec. 19, 2021
Dec. 19, 2021
https://github.com/python/cpython/commit/9a28cf19b5d5f79eb072afc582f7baa9e7…
commit: 9a28cf19b5d5f79eb072afc582f7baa9e7bd3ac2
branch: 3.10
author: Miss Islington (bot) <31488909+miss-islington(a)users.noreply.github.com>
committer: miss-islington <31488909+miss-islington(a)users.noreply.github.com>
date: 2021-12-19T06:32:49-08:00
summary:
bpo-46130: [docs] Add anchor for whatsnew/3.10 type hint section (GH-30199)
This allows the title to be translated to other languages without linking problems.
(cherry picked from commit 3d3615f41f4ea73fe6707eb3673dfab482cb6a2b)
Co-authored-by: Rafael Fontenelle <rffontenelle(a)users.noreply.github.com>
files:
M Doc/whatsnew/3.10.rst
diff --git a/Doc/whatsnew/3.10.rst b/Doc/whatsnew/3.10.rst
index c74e9f0ae5cb6..978651f33b206 100644
--- a/Doc/whatsnew/3.10.rst
+++ b/Doc/whatsnew/3.10.rst
@@ -704,6 +704,7 @@ are added to enable the warning.
See :ref:`io-text-encoding` for more information.
+.. _new-feat-related-type-hints:
New Features Related to Type Hints
==================================
@@ -1418,7 +1419,7 @@ of types readily interpretable by type checkers.
typing
------
-For major changes, see `New Features Related to Type Hints`_.
+For major changes, see :ref:`new-feat-related-type-hints`.
The behavior of :class:`typing.Literal` was changed to conform with :pep:`586`
and to match the behavior of static type checkers specified in the PEP.
1
0
bpo-46130: [docs] Add anchor for whatsnew/3.10 type hint section (GH-30199)
by Fidget-Spinner Dec. 19, 2021
by Fidget-Spinner Dec. 19, 2021
Dec. 19, 2021
https://github.com/python/cpython/commit/3d3615f41f4ea73fe6707eb3673dfab482…
commit: 3d3615f41f4ea73fe6707eb3673dfab482cb6a2b
branch: main
author: Rafael Fontenelle <rffontenelle(a)users.noreply.github.com>
committer: Fidget-Spinner <28750310+Fidget-Spinner(a)users.noreply.github.com>
date: 2021-12-19T22:06:35+08:00
summary:
bpo-46130: [docs] Add anchor for whatsnew/3.10 type hint section (GH-30199)
This allows the title to be translated to other languages without linking problems.
files:
M Doc/whatsnew/3.10.rst
diff --git a/Doc/whatsnew/3.10.rst b/Doc/whatsnew/3.10.rst
index 118563965091f..b56663f5eae8b 100644
--- a/Doc/whatsnew/3.10.rst
+++ b/Doc/whatsnew/3.10.rst
@@ -704,6 +704,7 @@ are added to enable the warning.
See :ref:`io-text-encoding` for more information.
+.. _new-feat-related-type-hints:
New Features Related to Type Hints
==================================
@@ -1429,7 +1430,7 @@ of types readily interpretable by type checkers.
typing
------
-For major changes, see `New Features Related to Type Hints`_.
+For major changes, see :ref:`new-feat-related-type-hints`.
The behavior of :class:`typing.Literal` was changed to conform with :pep:`586`
and to match the behavior of static type checkers specified in the PEP.
1
0
bpo-42413: Replace `concurrent.futures.TimeoutError` and `asyncio.TimeoutError` with builtin `TimeoutError` (GH-30197)
by asvetlov Dec. 19, 2021
by asvetlov Dec. 19, 2021
Dec. 19, 2021
https://github.com/python/cpython/commit/da4b214304df38cf1831071804a2b83938…
commit: da4b214304df38cf1831071804a2b83938f95923
branch: main
author: Kumar Aditya <59607654+kumaraditya303(a)users.noreply.github.com>
committer: asvetlov <andrew.svetlov(a)gmail.com>
date: 2021-12-19T13:22:40+02:00
summary:
bpo-42413: Replace `concurrent.futures.TimeoutError` and `asyncio.TimeoutError` with builtin `TimeoutError` (GH-30197)
Co-authored-by: Andrew Svetlov <andrew.svetlov(a)gmail.com>
files:
A Misc/NEWS.d/next/Library/2020-11-26-10-23-46.bpo-42413.HFikOl.rst
M Doc/library/asyncio-api-index.rst
M Doc/library/asyncio-exceptions.rst
M Doc/library/asyncio-task.rst
M Doc/library/concurrent.futures.rst
M Lib/asyncio/exceptions.py
M Lib/concurrent/futures/_base.py
diff --git a/Doc/library/asyncio-api-index.rst b/Doc/library/asyncio-api-index.rst
index f558724d4a3ff..8bc7943a71739 100644
--- a/Doc/library/asyncio-api-index.rst
+++ b/Doc/library/asyncio-api-index.rst
@@ -203,11 +203,6 @@ Exceptions
:class: full-width-table
- * - :exc:`asyncio.TimeoutError`
- - Raised on timeout by functions like :func:`wait_for`.
- Keep in mind that ``asyncio.TimeoutError`` is **unrelated**
- to the built-in :exc:`TimeoutError` exception.
-
* - :exc:`asyncio.CancelledError`
- Raised when a Task is cancelled. See also :meth:`Task.cancel`.
diff --git a/Doc/library/asyncio-exceptions.rst b/Doc/library/asyncio-exceptions.rst
index 7166d5c4bd88f..9250f01b8a089 100644
--- a/Doc/library/asyncio-exceptions.rst
+++ b/Doc/library/asyncio-exceptions.rst
@@ -13,11 +13,12 @@ Exceptions
.. exception:: TimeoutError
- The operation has exceeded the given deadline.
+ A deprecated alias of :exc:`TimeoutError`,
+ raised when the operation has exceeded the given deadline.
- .. important::
- This exception is different from the builtin :exc:`TimeoutError`
- exception.
+ .. versionchanged:: 3.11
+
+ This class was made an alias of :exc:`TimeoutError`.
.. exception:: CancelledError
diff --git a/Doc/library/asyncio-task.rst b/Doc/library/asyncio-task.rst
index bfc983e304bcc..cbc42ac26e48c 100644
--- a/Doc/library/asyncio-task.rst
+++ b/Doc/library/asyncio-task.rst
@@ -490,7 +490,7 @@ Timeouts
completes.
If a timeout occurs, it cancels the task and raises
- :exc:`asyncio.TimeoutError`.
+ :exc:`TimeoutError`.
To avoid the task :meth:`cancellation <Task.cancel>`,
wrap it in :func:`shield`.
@@ -520,7 +520,7 @@ Timeouts
# Wait for at most 1 second
try:
await asyncio.wait_for(eternity(), timeout=1.0)
- except asyncio.TimeoutError:
+ except TimeoutError:
print('timeout!')
asyncio.run(main())
@@ -532,7 +532,7 @@ Timeouts
.. versionchanged:: 3.7
When *aw* is cancelled due to a timeout, ``wait_for`` waits
for *aw* to be cancelled. Previously, it raised
- :exc:`asyncio.TimeoutError` immediately.
+ :exc:`TimeoutError` immediately.
.. deprecated-removed:: 3.8 3.10
The ``loop`` parameter. This function has been implicitly getting the
@@ -561,7 +561,7 @@ Waiting Primitives
*timeout* (a float or int), if specified, can be used to control
the maximum number of seconds to wait before returning.
- Note that this function does not raise :exc:`asyncio.TimeoutError`.
+ Note that this function does not raise :exc:`TimeoutError`.
Futures or Tasks that aren't done when the timeout occurs are simply
returned in the second set.
@@ -649,7 +649,7 @@ Waiting Primitives
Each coroutine returned can be awaited to get the earliest next
result from the iterable of the remaining awaitables.
- Raises :exc:`asyncio.TimeoutError` if the timeout occurs before
+ Raises :exc:`TimeoutError` if the timeout occurs before
all Futures are done.
.. deprecated-removed:: 3.8 3.10
@@ -762,7 +762,7 @@ Scheduling From Other Threads
try:
result = future.result(timeout)
- except concurrent.futures.TimeoutError:
+ except TimeoutError:
print('The coroutine took too long, cancelling the task...')
future.cancel()
except Exception as exc:
diff --git a/Doc/library/concurrent.futures.rst b/Doc/library/concurrent.futures.rst
index b4213b451157e..c9f6aa1f2637c 100644
--- a/Doc/library/concurrent.futures.rst
+++ b/Doc/library/concurrent.futures.rst
@@ -47,7 +47,7 @@ Executor Objects
* *func* is executed asynchronously and several calls to
*func* may be made concurrently.
- The returned iterator raises a :exc:`concurrent.futures.TimeoutError`
+ The returned iterator raises a :exc:`TimeoutError`
if :meth:`~iterator.__next__` is called and the result isn't available
after *timeout* seconds from the original call to :meth:`Executor.map`.
*timeout* can be an int or a float. If *timeout* is not specified or
@@ -352,7 +352,7 @@ The :class:`Future` class encapsulates the asynchronous execution of a callable.
Return the value returned by the call. If the call hasn't yet completed
then this method will wait up to *timeout* seconds. If the call hasn't
completed in *timeout* seconds, then a
- :exc:`concurrent.futures.TimeoutError` will be raised. *timeout* can be
+ :exc:`TimeoutError` will be raised. *timeout* can be
an int or float. If *timeout* is not specified or ``None``, there is no
limit to the wait time.
@@ -366,7 +366,7 @@ The :class:`Future` class encapsulates the asynchronous execution of a callable.
Return the exception raised by the call. If the call hasn't yet
completed then this method will wait up to *timeout* seconds. If the
call hasn't completed in *timeout* seconds, then a
- :exc:`concurrent.futures.TimeoutError` will be raised. *timeout* can be
+ :exc:`TimeoutError` will be raised. *timeout* can be
an int or float. If *timeout* is not specified or ``None``, there is no
limit to the wait time.
@@ -482,7 +482,7 @@ Module Functions
they complete (finished or cancelled futures). Any futures given by *fs* that
are duplicated will be returned once. Any futures that completed before
:func:`as_completed` is called will be yielded first. The returned iterator
- raises a :exc:`concurrent.futures.TimeoutError` if :meth:`~iterator.__next__`
+ raises a :exc:`TimeoutError` if :meth:`~iterator.__next__`
is called and the result isn't available after *timeout* seconds from the
original call to :func:`as_completed`. *timeout* can be an int or float. If
*timeout* is not specified or ``None``, there is no limit to the wait time.
@@ -506,7 +506,13 @@ Exception classes
.. exception:: TimeoutError
- Raised when a future operation exceeds the given timeout.
+ A deprecated alias of :exc:`TimeoutError`,
+ raised when a future operation exceeds the given timeout.
+
+ .. versionchanged:: 3.11
+
+ This class was made an alias of :exc:`TimeoutError`.
+
.. exception:: BrokenExecutor
diff --git a/Lib/asyncio/exceptions.py b/Lib/asyncio/exceptions.py
index f07e448657738..c764c9ffcfc19 100644
--- a/Lib/asyncio/exceptions.py
+++ b/Lib/asyncio/exceptions.py
@@ -10,8 +10,7 @@ class CancelledError(BaseException):
"""The Future or Task was cancelled."""
-class TimeoutError(Exception):
- """The operation exceeded the given deadline."""
+TimeoutError = TimeoutError # make local alias for the standard exception
class InvalidStateError(Exception):
diff --git a/Lib/concurrent/futures/_base.py b/Lib/concurrent/futures/_base.py
index 6095026cb278b..b0337399e5f25 100644
--- a/Lib/concurrent/futures/_base.py
+++ b/Lib/concurrent/futures/_base.py
@@ -50,9 +50,7 @@ class CancelledError(Error):
"""The Future was cancelled."""
pass
-class TimeoutError(Error):
- """The operation exceeded the given deadline."""
- pass
+TimeoutError = TimeoutError # make local alias for the standard exception
class InvalidStateError(Error):
"""The operation is not allowed in this state."""
diff --git a/Misc/NEWS.d/next/Library/2020-11-26-10-23-46.bpo-42413.HFikOl.rst b/Misc/NEWS.d/next/Library/2020-11-26-10-23-46.bpo-42413.HFikOl.rst
new file mode 100644
index 0000000000000..85b7fe25074b3
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2020-11-26-10-23-46.bpo-42413.HFikOl.rst
@@ -0,0 +1,2 @@
+Replace ``concurrent.futures.TimeoutError`` and ``asyncio.TimeoutError``
+with builtin :exc:`TimeoutError`, keep these names as deprecated aliases.
1
0