Skip to content

Commit 8425de4

Browse files
authored
bpo-33562: Check the global asyncio event loop policy isn't set after any tests (GH-7328)
1 parent de65162 commit 8425de4

26 files changed

+98
-2
lines changed

Lib/test/libregrtest/save_env.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import asyncio
12
import builtins
23
import locale
34
import logging
@@ -65,8 +66,14 @@ def __init__(self, testname, verbose=0, quiet=False, *, pgo=False):
6566
'sysconfig._CONFIG_VARS', 'sysconfig._INSTALL_SCHEMES',
6667
'files', 'locale', 'warnings.showwarning',
6768
'shutil_archive_formats', 'shutil_unpack_formats',
69+
'asyncio.events._event_loop_policy',
6870
)
6971

72+
def get_asyncio_events__event_loop_policy(self):
73+
return support.maybe_get_event_loop_policy()
74+
def restore_asyncio_events__event_loop_policy(self, policy):
75+
asyncio.set_event_loop_policy(policy)
76+
7077
def get_sys_argv(self):
7178
return id(sys.argv), sys.argv, sys.argv[:]
7279
def restore_sys_argv(self, saved_argv):

Lib/test/support/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
if __name__ != 'test.support':
44
raise ImportError('support must be imported from the test package')
55

6+
import asyncio.events
67
import collections.abc
78
import contextlib
89
import errno
@@ -2878,3 +2879,8 @@ def __fspath__(self):
28782879
raise self.path
28792880
else:
28802881
return self.path
2882+
2883+
2884+
def maybe_get_event_loop_policy():
2885+
"""Return the global event loop policy if one is set, else return None."""
2886+
return asyncio.events._event_loop_policy

Lib/test/test_asyncgen.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,7 @@ def setUp(self):
328328
def tearDown(self):
329329
self.loop.close()
330330
self.loop = None
331+
asyncio.set_event_loop_policy(None)
331332

332333
async def to_list(self, gen):
333334
res = []

Lib/test/test_asyncio/test_base_events.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424
PY34 = sys.version_info >= (3, 4)
2525

2626

27+
def tearDownModule():
28+
asyncio.set_event_loop_policy(None)
29+
30+
2731
def mock_socket_module():
2832
m_socket = mock.MagicMock(spec=socket)
2933
for name in (

Lib/test/test_asyncio/test_buffered_proto.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
from test.test_asyncio import functional as func_tests
55

66

7+
def tearDownModule():
8+
asyncio.set_event_loop_policy(None)
9+
10+
711
class ReceiveStuffProto(asyncio.BufferedProtocol):
812
def __init__(self, cb, con_lost_fut):
913
self.cb = cb

Lib/test/test_asyncio/test_context.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
import unittest
44

55

6+
def tearDownModule():
7+
asyncio.set_event_loop_policy(None)
8+
9+
610
class DecimalContextTest(unittest.TestCase):
711

812
def test_asyncio_task_decimal_context(self):

Lib/test/test_asyncio/test_events.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@
3737
from test import support
3838

3939

40+
def tearDownModule():
41+
asyncio.set_event_loop_policy(None)
42+
43+
4044
def osx_tiger():
4145
"""Return True if the platform is Mac OS 10.4 or older."""
4246
if sys.platform != 'darwin':

Lib/test/test_asyncio/test_futures.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
from test import support
1515

1616

17+
def tearDownModule():
18+
asyncio.set_event_loop_policy(None)
19+
20+
1721
def _fakefunc(f):
1822
return f
1923

Lib/test/test_asyncio/test_locks.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
RGX_REPR = re.compile(STR_RGX_REPR)
1717

1818

19+
def tearDownModule():
20+
asyncio.set_event_loop_policy(None)
21+
22+
1923
class LockTests(test_utils.TestCase):
2024

2125
def setUp(self):

Lib/test/test_asyncio/test_pep492.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
from test.test_asyncio import utils as test_utils
1212

1313

14+
def tearDownModule():
15+
asyncio.set_event_loop_policy(None)
16+
17+
1418
# Test that asyncio.iscoroutine() uses collections.abc.Coroutine
1519
class FakeCoro:
1620
def send(self, value):

0 commit comments

Comments
 (0)