Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions Lib/test/test_coroutines.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import unittest
import warnings
from test import support
from test.support.script_helper import assert_python_ok


class AsyncYieldFrom:
Expand Down Expand Up @@ -2168,6 +2169,27 @@ async def corofn():
finally:
warnings._warn_unawaited_coroutine = orig_wuc


class UnawaitedWarningDuringShutdownTest(unittest.TestCase):
# https://bugs.python.org/issue32591#msg310726
def test_unawaited_warning_during_shutdown(self):
code = ("import asyncio\n"
"async def f(): pass\n"
"asyncio.gather(f())\n")
assert_python_ok("-c", code)

code = ("import sys\n"
"async def f(): pass\n"
"sys.coro = f()\n")
assert_python_ok("-c", code)

code = ("import sys\n"
"async def f(): pass\n"
"sys.corocycle = [f()]\n"
"sys.corocycle.append(sys.corocycle)\n")
assert_python_ok("-c", code)


@support.cpython_only
class CAPITest(unittest.TestCase):

Expand Down
7 changes: 7 additions & 0 deletions Python/_warnings.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ get_warnings_attr(_Py_Identifier *attr_id, int try_import)
}
}
else {
/* if we're so late into Python finalization that the module dict is
gone, then we can't even use PyImport_GetModule without triggering
an interpreter abort.
*/
if (!PyThreadState_GET()->interp->modules) {
return NULL;
}
warnings_module = PyImport_GetModule(warnings_str);
if (warnings_module == NULL)
return NULL;
Expand Down