Skip to content

Commit 140770f

Browse files
authored
Merge branch 'main' into setobject-note
2 parents ed90b66 + 2dc9463 commit 140770f

12 files changed

+95
-48
lines changed

Lib/asyncio/tasks.py

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
from . import exceptions
2626
from . import futures
2727
from . import timeouts
28-
from .coroutines import _is_coroutine
2928

3029
# Helper to generate new task names
3130
# This uses itertools.count() instead of a "+= 1" operation because the latter
@@ -635,11 +634,14 @@ def ensure_future(coro_or_future, *, loop=None):
635634
raise ValueError('The future belongs to a different loop than '
636635
'the one specified as the loop argument')
637636
return coro_or_future
638-
called_wrap_awaitable = False
637+
should_close = True
639638
if not coroutines.iscoroutine(coro_or_future):
640639
if inspect.isawaitable(coro_or_future):
640+
async def _wrap_awaitable(awaitable):
641+
return await awaitable
642+
641643
coro_or_future = _wrap_awaitable(coro_or_future)
642-
called_wrap_awaitable = True
644+
should_close = False
643645
else:
644646
raise TypeError('An asyncio.Future, a coroutine or an awaitable '
645647
'is required')
@@ -649,23 +651,11 @@ def ensure_future(coro_or_future, *, loop=None):
649651
try:
650652
return loop.create_task(coro_or_future)
651653
except RuntimeError:
652-
if not called_wrap_awaitable:
654+
if should_close:
653655
coro_or_future.close()
654656
raise
655657

656658

657-
@types.coroutine
658-
def _wrap_awaitable(awaitable):
659-
"""Helper for asyncio.ensure_future().
660-
661-
Wraps awaitable (an object with __await__) into a coroutine
662-
that will later be wrapped in a Task by ensure_future().
663-
"""
664-
return (yield from awaitable.__await__())
665-
666-
_wrap_awaitable._is_coroutine = _is_coroutine
667-
668-
669659
class _GatheringFuture(futures.Future):
670660
"""Helper for gather().
671661

Lib/concurrent/futures/process.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,11 @@ def run(self):
366366
if self.is_shutting_down():
367367
self.flag_executor_shutting_down()
368368

369+
# When only canceled futures remain in pending_work_items, our
370+
# next call to wait_result_broken_or_wakeup would hang forever.
371+
# This makes sure we have some running futures or none at all.
372+
self.add_call_item_to_queue()
373+
369374
# Since no new work items can be added, it is safe to shutdown
370375
# this thread if there are no pending work items.
371376
if not self.pending_work_items:

Lib/test/test_asyncio/test_tasks.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import re
99
import sys
1010
import traceback
11+
import types
1112
import unittest
1213
from unittest import mock
1314
from types import GenericAlias
@@ -274,6 +275,20 @@ async def coro():
274275
loop.run_until_complete(fut)
275276
self.assertEqual(fut.result(), 'ok')
276277

278+
def test_ensure_future_task_awaitable(self):
279+
class Aw:
280+
def __await__(self):
281+
return asyncio.sleep(0, result='ok').__await__()
282+
283+
loop = asyncio.new_event_loop()
284+
self.set_event_loop(loop)
285+
task = asyncio.ensure_future(Aw(), loop=loop)
286+
loop.run_until_complete(task)
287+
self.assertTrue(task.done())
288+
self.assertEqual(task.result(), 'ok')
289+
self.assertIsInstance(task.get_coro(), types.CoroutineType)
290+
loop.close()
291+
277292
def test_ensure_future_neither(self):
278293
with self.assertRaises(TypeError):
279294
asyncio.ensure_future('ok')

Lib/test/test_concurrent_futures.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from logging.handlers import QueueHandler
1515
import os
1616
import queue
17+
import signal
1718
import sys
1819
import threading
1920
import time
@@ -397,6 +398,33 @@ def test_hang_gh83386(self):
397398
self.assertFalse(err)
398399
self.assertEqual(out.strip(), b"apple")
399400

401+
def test_hang_gh94440(self):
402+
"""shutdown(wait=True) doesn't hang when a future was submitted and
403+
quickly canceled right before shutdown.
404+
405+
See https://github.com/python/cpython/issues/94440.
406+
"""
407+
if not hasattr(signal, 'alarm'):
408+
raise unittest.SkipTest(
409+
"Tested platform does not support the alarm signal")
410+
411+
def timeout(_signum, _frame):
412+
raise RuntimeError("timed out waiting for shutdown")
413+
414+
kwargs = {}
415+
if getattr(self, 'ctx', None):
416+
kwargs['mp_context'] = self.get_context()
417+
executor = self.executor_type(max_workers=1, **kwargs)
418+
executor.submit(int).result()
419+
old_handler = signal.signal(signal.SIGALRM, timeout)
420+
try:
421+
signal.alarm(5)
422+
executor.submit(int).cancel()
423+
executor.shutdown(wait=True)
424+
finally:
425+
signal.alarm(0)
426+
signal.signal(signal.SIGALRM, old_handler)
427+
400428

401429
class ThreadPoolShutdownTest(ThreadPoolMixin, ExecutorShutdownTest, BaseTestCase):
402430
def test_threads_terminate(self):

Lib/webbrowser.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -542,11 +542,15 @@ def register_standard_browsers():
542542
# First try to use the default Windows browser
543543
register("windows-default", WindowsDefault)
544544

545-
# Detect some common Windows browsers, fallback to IE
546-
iexplore = os.path.join(os.environ.get("PROGRAMFILES", "C:\\Program Files"),
547-
"Internet Explorer\\IEXPLORE.EXE")
545+
# Detect some common Windows browsers, fallback to Microsoft Edge
546+
# location in 64-bit Windows
547+
edge64 = os.path.join(os.environ.get("PROGRAMFILES(x86)", "C:\\Program Files (x86)"),
548+
"Microsoft\\Edge\\Application\\msedge.exe")
549+
# location in 32-bit Windows
550+
edge32 = os.path.join(os.environ.get("PROGRAMFILES", "C:\\Program Files"),
551+
"Microsoft\\Edge\\Application\\msedge.exe")
548552
for browser in ("firefox", "firebird", "seamonkey", "mozilla",
549-
"netscape", "opera", iexplore):
553+
"opera", edge64, edge32):
550554
if shutil.which(browser):
551555
register(browser, None, BackgroundBrowser(browser))
552556
else:

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1385,6 +1385,7 @@ Thomas Perl
13851385
Mathieu Perreault
13861386
Mark Perrego
13871387
Trevor Perrin
1388+
Yonatan Perry
13881389
Gabriel de Perthuis
13891390
Tim Peters
13901391
Benjamin Peterson
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix a :mod:`concurrent.futures.process` bug where ``ProcessPoolExecutor`` shutdown
2+
could hang after a future has been quickly submitted and canceled.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
:meth:`asyncio.Task.get_coro` now always returns a coroutine when wrapping an awaitable object. Patch by Kumar Aditya.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Update :mod:`webbrowser` to fall back to Microsoft Edge instead of Internet Explorer.

Python/bltinmodule.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3098,9 +3098,6 @@ _PyBuiltin_Init(PyInterpreterState *interp)
30983098
}
30993099
Py_DECREF(debug);
31003100

3101-
/* m_copy of Py_None means it is copied some other way. */
3102-
builtinsmodule.m_base.m_copy = Py_NewRef(Py_None);
3103-
31043101
return mod;
31053102
#undef ADD_TO_ALL
31063103
#undef SETBUILTIN

Python/import.c

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -978,14 +978,29 @@ _PyImport_CheckSubinterpIncompatibleExtensionAllowed(const char *name)
978978
return 0;
979979
}
980980

981-
static inline int
982-
match_mod_name(PyObject *actual, const char *expected)
981+
static PyObject *
982+
get_core_module_dict(PyInterpreterState *interp,
983+
PyObject *name, PyObject *filename)
983984
{
984-
if (PyUnicode_CompareWithASCIIString(actual, expected) == 0) {
985-
return 1;
985+
/* Only builtin modules are core. */
986+
if (filename == name) {
987+
assert(!PyErr_Occurred());
988+
if (PyUnicode_CompareWithASCIIString(name, "sys") == 0) {
989+
return interp->sysdict_copy;
990+
}
991+
assert(!PyErr_Occurred());
992+
if (PyUnicode_CompareWithASCIIString(name, "builtins") == 0) {
993+
return interp->builtins_copy;
994+
}
995+
assert(!PyErr_Occurred());
986996
}
987-
assert(!PyErr_Occurred());
988-
return 0;
997+
return NULL;
998+
}
999+
1000+
static inline int
1001+
is_core_module(PyInterpreterState *interp, PyObject *name, PyObject *filename)
1002+
{
1003+
return get_core_module_dict(interp, name, filename) != NULL;
9891004
}
9901005

9911006
static int
@@ -1009,10 +1024,8 @@ fix_up_extension(PyObject *mod, PyObject *name, PyObject *filename)
10091024

10101025
// bpo-44050: Extensions and def->m_base.m_copy can be updated
10111026
// when the extension module doesn't support sub-interpreters.
1012-
// XXX Why special-case the main interpreter?
1013-
if (_Py_IsMainInterpreter(tstate->interp) || def->m_size == -1) {
1014-
/* m_copy of Py_None means it is copied some other way. */
1015-
if (def->m_size == -1 && def->m_base.m_copy != Py_None) {
1027+
if (def->m_size == -1) {
1028+
if (!is_core_module(tstate->interp, name, filename)) {
10161029
if (def->m_base.m_copy) {
10171030
/* Somebody already imported the module,
10181031
likely under a different name.
@@ -1028,7 +1041,10 @@ fix_up_extension(PyObject *mod, PyObject *name, PyObject *filename)
10281041
return -1;
10291042
}
10301043
}
1044+
}
10311045

1046+
// XXX Why special-case the main interpreter?
1047+
if (_Py_IsMainInterpreter(tstate->interp) || def->m_size == -1) {
10321048
if (_extensions_cache_set(filename, name, def) < 0) {
10331049
return -1;
10341050
}
@@ -1069,21 +1085,11 @@ import_find_extension(PyThreadState *tstate, PyObject *name,
10691085
PyObject *m_copy = def->m_base.m_copy;
10701086
/* Module does not support repeated initialization */
10711087
if (m_copy == NULL) {
1072-
return NULL;
1073-
}
1074-
else if (m_copy == Py_None) {
1075-
if (match_mod_name(name, "sys")) {
1076-
m_copy = tstate->interp->sysdict_copy;
1077-
}
1078-
else if (match_mod_name(name, "builtins")) {
1079-
m_copy = tstate->interp->builtins_copy;
1080-
}
1081-
else {
1082-
_PyErr_SetString(tstate, PyExc_ImportError, "missing m_copy");
1088+
m_copy = get_core_module_dict(tstate->interp, name, filename);
1089+
if (m_copy == NULL) {
10831090
return NULL;
10841091
}
10851092
}
1086-
/* m_copy of Py_None means it is copied some other way. */
10871093
mod = import_add_module(tstate, name);
10881094
if (mod == NULL) {
10891095
return NULL;

Python/sysmodule.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3425,9 +3425,6 @@ _PySys_Create(PyThreadState *tstate, PyObject **sysmod_p)
34253425
return _PyStatus_ERR("failed to create a module object");
34263426
}
34273427

3428-
/* m_copy of Py_None means it is copied some other way. */
3429-
sysmodule.m_base.m_copy = Py_NewRef(Py_None);
3430-
34313428
PyObject *sysdict = PyModule_GetDict(sysmod);
34323429
if (sysdict == NULL) {
34333430
goto error;

0 commit comments

Comments
 (0)