Skip to content

Commit

Permalink
Merge branch 'main' into setobject-note
Browse files Browse the repository at this point in the history
  • Loading branch information
iritkatriel authored Mar 16, 2023
2 parents ed90b66 + 2dc9463 commit 140770f
Show file tree
Hide file tree
Showing 12 changed files with 95 additions and 48 deletions.
22 changes: 6 additions & 16 deletions Lib/asyncio/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
from . import exceptions
from . import futures
from . import timeouts
from .coroutines import _is_coroutine

# Helper to generate new task names
# This uses itertools.count() instead of a "+= 1" operation because the latter
Expand Down Expand Up @@ -635,11 +634,14 @@ def ensure_future(coro_or_future, *, loop=None):
raise ValueError('The future belongs to a different loop than '
'the one specified as the loop argument')
return coro_or_future
called_wrap_awaitable = False
should_close = True
if not coroutines.iscoroutine(coro_or_future):
if inspect.isawaitable(coro_or_future):
async def _wrap_awaitable(awaitable):
return await awaitable

coro_or_future = _wrap_awaitable(coro_or_future)
called_wrap_awaitable = True
should_close = False
else:
raise TypeError('An asyncio.Future, a coroutine or an awaitable '
'is required')
Expand All @@ -649,23 +651,11 @@ def ensure_future(coro_or_future, *, loop=None):
try:
return loop.create_task(coro_or_future)
except RuntimeError:
if not called_wrap_awaitable:
if should_close:
coro_or_future.close()
raise


@types.coroutine
def _wrap_awaitable(awaitable):
"""Helper for asyncio.ensure_future().
Wraps awaitable (an object with __await__) into a coroutine
that will later be wrapped in a Task by ensure_future().
"""
return (yield from awaitable.__await__())

_wrap_awaitable._is_coroutine = _is_coroutine


class _GatheringFuture(futures.Future):
"""Helper for gather().
Expand Down
5 changes: 5 additions & 0 deletions Lib/concurrent/futures/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,11 @@ def run(self):
if self.is_shutting_down():
self.flag_executor_shutting_down()

# When only canceled futures remain in pending_work_items, our
# next call to wait_result_broken_or_wakeup would hang forever.
# This makes sure we have some running futures or none at all.
self.add_call_item_to_queue()

# Since no new work items can be added, it is safe to shutdown
# this thread if there are no pending work items.
if not self.pending_work_items:
Expand Down
15 changes: 15 additions & 0 deletions Lib/test/test_asyncio/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import re
import sys
import traceback
import types
import unittest
from unittest import mock
from types import GenericAlias
Expand Down Expand Up @@ -274,6 +275,20 @@ async def coro():
loop.run_until_complete(fut)
self.assertEqual(fut.result(), 'ok')

def test_ensure_future_task_awaitable(self):
class Aw:
def __await__(self):
return asyncio.sleep(0, result='ok').__await__()

loop = asyncio.new_event_loop()
self.set_event_loop(loop)
task = asyncio.ensure_future(Aw(), loop=loop)
loop.run_until_complete(task)
self.assertTrue(task.done())
self.assertEqual(task.result(), 'ok')
self.assertIsInstance(task.get_coro(), types.CoroutineType)
loop.close()

def test_ensure_future_neither(self):
with self.assertRaises(TypeError):
asyncio.ensure_future('ok')
Expand Down
28 changes: 28 additions & 0 deletions Lib/test/test_concurrent_futures.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from logging.handlers import QueueHandler
import os
import queue
import signal
import sys
import threading
import time
Expand Down Expand Up @@ -397,6 +398,33 @@ def test_hang_gh83386(self):
self.assertFalse(err)
self.assertEqual(out.strip(), b"apple")

def test_hang_gh94440(self):
"""shutdown(wait=True) doesn't hang when a future was submitted and
quickly canceled right before shutdown.
See https://github.com/python/cpython/issues/94440.
"""
if not hasattr(signal, 'alarm'):
raise unittest.SkipTest(
"Tested platform does not support the alarm signal")

def timeout(_signum, _frame):
raise RuntimeError("timed out waiting for shutdown")

kwargs = {}
if getattr(self, 'ctx', None):
kwargs['mp_context'] = self.get_context()
executor = self.executor_type(max_workers=1, **kwargs)
executor.submit(int).result()
old_handler = signal.signal(signal.SIGALRM, timeout)
try:
signal.alarm(5)
executor.submit(int).cancel()
executor.shutdown(wait=True)
finally:
signal.alarm(0)
signal.signal(signal.SIGALRM, old_handler)


class ThreadPoolShutdownTest(ThreadPoolMixin, ExecutorShutdownTest, BaseTestCase):
def test_threads_terminate(self):
Expand Down
12 changes: 8 additions & 4 deletions Lib/webbrowser.py
Original file line number Diff line number Diff line change
Expand Up @@ -542,11 +542,15 @@ def register_standard_browsers():
# First try to use the default Windows browser
register("windows-default", WindowsDefault)

# Detect some common Windows browsers, fallback to IE
iexplore = os.path.join(os.environ.get("PROGRAMFILES", "C:\\Program Files"),
"Internet Explorer\\IEXPLORE.EXE")
# Detect some common Windows browsers, fallback to Microsoft Edge
# location in 64-bit Windows
edge64 = os.path.join(os.environ.get("PROGRAMFILES(x86)", "C:\\Program Files (x86)"),
"Microsoft\\Edge\\Application\\msedge.exe")
# location in 32-bit Windows
edge32 = os.path.join(os.environ.get("PROGRAMFILES", "C:\\Program Files"),
"Microsoft\\Edge\\Application\\msedge.exe")
for browser in ("firefox", "firebird", "seamonkey", "mozilla",
"netscape", "opera", iexplore):
"opera", edge64, edge32):
if shutil.which(browser):
register(browser, None, BackgroundBrowser(browser))
else:
Expand Down
1 change: 1 addition & 0 deletions Misc/ACKS
Original file line number Diff line number Diff line change
Expand Up @@ -1385,6 +1385,7 @@ Thomas Perl
Mathieu Perreault
Mark Perrego
Trevor Perrin
Yonatan Perry
Gabriel de Perthuis
Tim Peters
Benjamin Peterson
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix a :mod:`concurrent.futures.process` bug where ``ProcessPoolExecutor`` shutdown
could hang after a future has been quickly submitted and canceled.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
:meth:`asyncio.Task.get_coro` now always returns a coroutine when wrapping an awaitable object. Patch by Kumar Aditya.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Update :mod:`webbrowser` to fall back to Microsoft Edge instead of Internet Explorer.
3 changes: 0 additions & 3 deletions Python/bltinmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -3098,9 +3098,6 @@ _PyBuiltin_Init(PyInterpreterState *interp)
}
Py_DECREF(debug);

/* m_copy of Py_None means it is copied some other way. */
builtinsmodule.m_base.m_copy = Py_NewRef(Py_None);

return mod;
#undef ADD_TO_ALL
#undef SETBUILTIN
Expand Down
50 changes: 28 additions & 22 deletions Python/import.c
Original file line number Diff line number Diff line change
Expand Up @@ -978,14 +978,29 @@ _PyImport_CheckSubinterpIncompatibleExtensionAllowed(const char *name)
return 0;
}

static inline int
match_mod_name(PyObject *actual, const char *expected)
static PyObject *
get_core_module_dict(PyInterpreterState *interp,
PyObject *name, PyObject *filename)
{
if (PyUnicode_CompareWithASCIIString(actual, expected) == 0) {
return 1;
/* Only builtin modules are core. */
if (filename == name) {
assert(!PyErr_Occurred());
if (PyUnicode_CompareWithASCIIString(name, "sys") == 0) {
return interp->sysdict_copy;
}
assert(!PyErr_Occurred());
if (PyUnicode_CompareWithASCIIString(name, "builtins") == 0) {
return interp->builtins_copy;
}
assert(!PyErr_Occurred());
}
assert(!PyErr_Occurred());
return 0;
return NULL;
}

static inline int
is_core_module(PyInterpreterState *interp, PyObject *name, PyObject *filename)
{
return get_core_module_dict(interp, name, filename) != NULL;
}

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

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

// XXX Why special-case the main interpreter?
if (_Py_IsMainInterpreter(tstate->interp) || def->m_size == -1) {
if (_extensions_cache_set(filename, name, def) < 0) {
return -1;
}
Expand Down Expand Up @@ -1069,21 +1085,11 @@ import_find_extension(PyThreadState *tstate, PyObject *name,
PyObject *m_copy = def->m_base.m_copy;
/* Module does not support repeated initialization */
if (m_copy == NULL) {
return NULL;
}
else if (m_copy == Py_None) {
if (match_mod_name(name, "sys")) {
m_copy = tstate->interp->sysdict_copy;
}
else if (match_mod_name(name, "builtins")) {
m_copy = tstate->interp->builtins_copy;
}
else {
_PyErr_SetString(tstate, PyExc_ImportError, "missing m_copy");
m_copy = get_core_module_dict(tstate->interp, name, filename);
if (m_copy == NULL) {
return NULL;
}
}
/* m_copy of Py_None means it is copied some other way. */
mod = import_add_module(tstate, name);
if (mod == NULL) {
return NULL;
Expand Down
3 changes: 0 additions & 3 deletions Python/sysmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -3425,9 +3425,6 @@ _PySys_Create(PyThreadState *tstate, PyObject **sysmod_p)
return _PyStatus_ERR("failed to create a module object");
}

/* m_copy of Py_None means it is copied some other way. */
sysmodule.m_base.m_copy = Py_NewRef(Py_None);

PyObject *sysdict = PyModule_GetDict(sysmod);
if (sysdict == NULL) {
goto error;
Expand Down

0 comments on commit 140770f

Please sign in to comment.