Skip to content

Commit 5ac3d0f

Browse files
miss-islingtonambv
andauthored
gh-91323: Revert "Allow overriding a future compliance check in asyncio.Task (GH-32197)" (GH-95442) (GH-95652)
This reverts commit d4bb38f. (cherry picked from commit 0342c93) Co-authored-by: Łukasz Langa <[email protected]>
1 parent 2d84fe5 commit 5ac3d0f

File tree

5 files changed

+10
-122
lines changed

5 files changed

+10
-122
lines changed

Doc/library/asyncio-extending.rst

-6
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,6 @@ For this purpose the following, *private* constructors are listed:
6363

6464
*context* argument is added.
6565

66-
.. method:: Task._check_future(future)
67-
68-
Return ``True`` if *future* is attached to the same loop as the task, ``False``
69-
otherwise.
70-
71-
.. versionadded:: 3.11
7266

7367

7468
Task lifetime support

Lib/asyncio/tasks.py

+1-5
Original file line numberDiff line numberDiff line change
@@ -252,10 +252,6 @@ def uncancel(self):
252252
self._num_cancels_requested -= 1
253253
return self._num_cancels_requested
254254

255-
def _check_future(self, future):
256-
"""Return False if task and future loops are not compatible."""
257-
return futures._get_loop(future) is self._loop
258-
259255
def __step(self, exc=None):
260256
if self.done():
261257
raise exceptions.InvalidStateError(
@@ -296,7 +292,7 @@ def __step(self, exc=None):
296292
blocking = getattr(result, '_asyncio_future_blocking', None)
297293
if blocking is not None:
298294
# Yielded Future must come from Future.__iter__().
299-
if not self._check_future(result):
295+
if futures._get_loop(result) is not self._loop:
300296
new_exc = RuntimeError(
301297
f'Task {self!r} got Future '
302298
f'{result!r} attached to a different loop')

Lib/test/test_asyncio/test_tasks.py

+1-9
Original file line numberDiff line numberDiff line change
@@ -2384,13 +2384,7 @@ def add_done_callback(self, *args, **kwargs):
23842384
return super().add_done_callback(*args, **kwargs)
23852385

23862386
class Task(CommonFuture, BaseTask):
2387-
def __init__(self, *args, **kwargs):
2388-
self._check_future_called = 0
2389-
super().__init__(*args, **kwargs)
2390-
2391-
def _check_future(self, future):
2392-
self._check_future_called += 1
2393-
return super()._check_future(future)
2387+
pass
23942388

23952389
class Future(CommonFuture, BaseFuture):
23962390
pass
@@ -2416,8 +2410,6 @@ async def func():
24162410
dict(fut.calls),
24172411
{'add_done_callback': 1})
24182412

2419-
self.assertEqual(1, task._check_future_called)
2420-
24212413
# Add patched Task & Future back to the test case
24222414
cls.Task = Task
24232415
cls.Future = Future

Modules/_asynciomodule.c

+7-64
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ _Py_IDENTIFIER(call_soon);
2323
_Py_IDENTIFIER(cancel);
2424
_Py_IDENTIFIER(get_event_loop);
2525
_Py_IDENTIFIER(throw);
26-
_Py_IDENTIFIER(_check_future);
2726

2827

2928
/* State of the _asyncio module */
@@ -1810,8 +1809,6 @@ class _asyncio.Task "TaskObj *" "&Task_Type"
18101809
static int task_call_step_soon(TaskObj *, PyObject *);
18111810
static PyObject * task_wakeup(TaskObj *, PyObject *);
18121811
static PyObject * task_step(TaskObj *, PyObject *);
1813-
static int task_check_future(TaskObj *, PyObject *);
1814-
static int task_check_future_exact(TaskObj *, PyObject *);
18151812

18161813
/* ----- Task._step wrapper */
18171814

@@ -2286,28 +2283,14 @@ Returns the remaining number of cancellation requests.
22862283
static PyObject *
22872284
_asyncio_Task_uncancel_impl(TaskObj *self)
22882285
/*[clinic end generated code: output=58184d236a817d3c input=68f81a4b90b46be2]*/
2286+
/*[clinic end generated code]*/
22892287
{
22902288
if (self->task_num_cancels_requested > 0) {
22912289
self->task_num_cancels_requested -= 1;
22922290
}
22932291
return PyLong_FromLong(self->task_num_cancels_requested);
22942292
}
22952293

2296-
/*[clinic input]
2297-
_asyncio.Task._check_future -> bool
2298-
2299-
future: object
2300-
2301-
Return False if task and future loops are not compatible.
2302-
[clinic start generated code]*/
2303-
2304-
static int
2305-
_asyncio_Task__check_future_impl(TaskObj *self, PyObject *future)
2306-
/*[clinic end generated code: output=a3bfba79295c8d57 input=3b1d6dfd6fe90aa5]*/
2307-
{
2308-
return task_check_future_exact(self, future);
2309-
}
2310-
23112294
/*[clinic input]
23122295
_asyncio.Task.get_stack
23132296
@@ -2533,7 +2516,6 @@ static PyMethodDef TaskType_methods[] = {
25332516
_ASYNCIO_TASK_CANCEL_METHODDEF
25342517
_ASYNCIO_TASK_CANCELLING_METHODDEF
25352518
_ASYNCIO_TASK_UNCANCEL_METHODDEF
2536-
_ASYNCIO_TASK__CHECK_FUTURE_METHODDEF
25372519
_ASYNCIO_TASK_GET_STACK_METHODDEF
25382520
_ASYNCIO_TASK_PRINT_STACK_METHODDEF
25392521
_ASYNCIO_TASK__MAKE_CANCELLED_ERROR_METHODDEF
@@ -2601,43 +2583,6 @@ TaskObj_dealloc(PyObject *self)
26012583
Py_TYPE(task)->tp_free(task);
26022584
}
26032585

2604-
static int
2605-
task_check_future_exact(TaskObj *task, PyObject *future)
2606-
{
2607-
int res;
2608-
if (Future_CheckExact(future) || Task_CheckExact(future)) {
2609-
FutureObj *fut = (FutureObj *)future;
2610-
res = (fut->fut_loop == task->task_loop);
2611-
} else {
2612-
PyObject *oloop = get_future_loop(future);
2613-
if (oloop == NULL) {
2614-
return -1;
2615-
}
2616-
res = (oloop == task->task_loop);
2617-
Py_DECREF(oloop);
2618-
}
2619-
return res;
2620-
}
2621-
2622-
2623-
static int
2624-
task_check_future(TaskObj *task, PyObject *future)
2625-
{
2626-
if (Task_CheckExact(task)) {
2627-
return task_check_future_exact(task, future);
2628-
} else {
2629-
PyObject * ret = _PyObject_CallMethodIdOneArg((PyObject *)task,
2630-
&PyId__check_future,
2631-
future);
2632-
if (ret == NULL) {
2633-
return -1;
2634-
}
2635-
int is_true = PyObject_IsTrue(ret);
2636-
Py_DECREF(ret);
2637-
return is_true;
2638-
}
2639-
}
2640-
26412586
static int
26422587
task_call_step_soon(TaskObj *task, PyObject *arg)
26432588
{
@@ -2859,11 +2804,7 @@ task_step_impl(TaskObj *task, PyObject *exc)
28592804
FutureObj *fut = (FutureObj*)result;
28602805

28612806
/* Check if `result` future is attached to a different loop */
2862-
res = task_check_future(task, result);
2863-
if (res == -1) {
2864-
goto fail;
2865-
}
2866-
if (res == 0) {
2807+
if (fut->fut_loop != task->task_loop) {
28672808
goto different_loop;
28682809
}
28692810

@@ -2935,13 +2876,15 @@ task_step_impl(TaskObj *task, PyObject *exc)
29352876
}
29362877

29372878
/* Check if `result` future is attached to a different loop */
2938-
res = task_check_future(task, result);
2939-
if (res == -1) {
2879+
PyObject *oloop = get_future_loop(result);
2880+
if (oloop == NULL) {
29402881
goto fail;
29412882
}
2942-
if (res == 0) {
2883+
if (oloop != task->task_loop) {
2884+
Py_DECREF(oloop);
29432885
goto different_loop;
29442886
}
2887+
Py_DECREF(oloop);
29452888

29462889
if (!blocking) {
29472890
goto yield_insteadof_yf;

Modules/clinic/_asynciomodule.c.h

+1-38
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)