Skip to content

gh-134657: Remove newly added private names from asyncio.__all__ #134665

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
29 changes: 19 additions & 10 deletions Lib/asyncio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,24 @@
def __getattr__(name: str):
import warnings

deprecated = {
"AbstractEventLoopPolicy",
"DefaultEventLoopPolicy",
"WindowsSelectorEventLoopPolicy",
"WindowsProactorEventLoopPolicy",
}
if name in deprecated:
warnings._deprecated(f"asyncio.{name}", remove=(3, 16))
# deprecated things have underscores in front of them
return globals()["_" + name]
match name:
case "AbstractEventLoopPolicy":
warnings._deprecated(f"asyncio.{name}", remove=(3, 16))
return events._AbstractEventLoopPolicy
case "DefaultEventLoopPolicy":
warnings._deprecated(f"asyncio.{name}", remove=(3, 16))
if sys.platform == 'win32':
return windows_events._DefaultEventLoopPolicy
return unix_events._DefaultEventLoopPolicy
case "WindowsSelectorEventLoopPolicy":
if sys.platform == 'win32':
warnings._deprecated(f"asyncio.{name}", remove=(3, 16))
return windows_events._WindowsSelectorEventLoopPolicy
# Else fall through to the AttributeError below.
case "WindowsProactorEventLoopPolicy":
if sys.platform == 'win32':
warnings._deprecated(f"asyncio.{name}", remove=(3, 16))
return windows_events._WindowsProactorEventLoopPolicy
# Else fall through to the AttributeError below.

raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
8 changes: 4 additions & 4 deletions Lib/asyncio/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,11 @@
# SPDX-FileCopyrightText: Copyright (c) 2015-2021 MagicStack Inc. http://magic.io

__all__ = (
"_AbstractEventLoopPolicy",
"AbstractEventLoop",
"AbstractServer",
"Handle",
"TimerHandle",
"_get_event_loop_policy",
"get_event_loop_policy",
"_set_event_loop_policy",
"set_event_loop_policy",
"get_event_loop",
"set_event_loop",
Expand Down Expand Up @@ -791,7 +788,10 @@ def _init_event_loop_policy():
global _event_loop_policy
with _lock:
if _event_loop_policy is None: # pragma: no branch
from . import _DefaultEventLoopPolicy
if sys.platform == 'win32':
from .windows_events import _DefaultEventLoopPolicy
else:
from .unix_events import _DefaultEventLoopPolicy
_event_loop_policy = _DefaultEventLoopPolicy()


Expand Down
1 change: 0 additions & 1 deletion Lib/asyncio/unix_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@

__all__ = (
'SelectorEventLoop',
'_DefaultEventLoopPolicy',
'EventLoop',
)

Expand Down
2 changes: 1 addition & 1 deletion Lib/test/libregrtest/save_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def get_asyncio_events__event_loop_policy(self):
return support.maybe_get_event_loop_policy()
def restore_asyncio_events__event_loop_policy(self, policy):
asyncio = self.get_module('asyncio')
asyncio._set_event_loop_policy(policy)
asyncio.events._set_event_loop_policy(policy)

def get_sys_argv(self):
return id(sys.argv), sys.argv, sys.argv[:]
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_asyncgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,7 @@ def setUp(self):
def tearDown(self):
self.loop.close()
self.loop = None
asyncio._set_event_loop_policy(None)
asyncio.events._set_event_loop_policy(None)

def check_async_iterator_anext(self, ait_class):
with self.subTest(anext="pure-Python"):
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_asyncio/test_base_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@


def tearDownModule():
asyncio._set_event_loop_policy(None)
asyncio.events._set_event_loop_policy(None)


def mock_socket_module():
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_asyncio/test_buffered_proto.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@


def tearDownModule():
asyncio._set_event_loop_policy(None)
asyncio.events._set_event_loop_policy(None)


class ReceiveStuffProto(asyncio.BufferedProtocol):
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_asyncio/test_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@


def tearDownModule():
asyncio._set_event_loop_policy(None)
asyncio.events._set_event_loop_policy(None)


@unittest.skipUnless(decimal.HAVE_CONTEXTVAR, "decimal is built with a thread-local context")
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_asyncio/test_eager_task_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@


def tearDownModule():
asyncio._set_event_loop_policy(None)
asyncio.events._set_event_loop_policy(None)


class EagerTaskFactoryLoopTests:
Expand Down
34 changes: 17 additions & 17 deletions Lib/test/test_asyncio/test_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
from test.support import ALWAYS_EQ, LARGEST, SMALLEST

def tearDownModule():
asyncio._set_event_loop_policy(None)
asyncio.events._set_event_loop_policy(None)


def broken_unix_getsockname():
Expand Down Expand Up @@ -2843,21 +2843,21 @@ def test_default_event_loop_policy_deprecation(self):
self.assertIsInstance(policy, asyncio.DefaultEventLoopPolicy)

def test_event_loop_policy(self):
policy = asyncio._AbstractEventLoopPolicy()
policy = asyncio.events._AbstractEventLoopPolicy()
self.assertRaises(NotImplementedError, policy.get_event_loop)
self.assertRaises(NotImplementedError, policy.set_event_loop, object())
self.assertRaises(NotImplementedError, policy.new_event_loop)

def test_get_event_loop(self):
policy = asyncio._DefaultEventLoopPolicy()
policy = test_utils.DefaultEventLoopPolicy()
self.assertIsNone(policy._local._loop)

with self.assertRaises(RuntimeError):
loop = policy.get_event_loop()
self.assertIsNone(policy._local._loop)

def test_get_event_loop_does_not_call_set_event_loop(self):
policy = asyncio._DefaultEventLoopPolicy()
policy = test_utils.DefaultEventLoopPolicy()

with mock.patch.object(
policy, "set_event_loop",
Expand All @@ -2869,30 +2869,30 @@ def test_get_event_loop_does_not_call_set_event_loop(self):
m_set_event_loop.assert_not_called()

def test_get_event_loop_after_set_none(self):
policy = asyncio._DefaultEventLoopPolicy()
policy = test_utils.DefaultEventLoopPolicy()
policy.set_event_loop(None)
self.assertRaises(RuntimeError, policy.get_event_loop)

@mock.patch('asyncio.events.threading.current_thread')
def test_get_event_loop_thread(self, m_current_thread):

def f():
policy = asyncio._DefaultEventLoopPolicy()
policy = test_utils.DefaultEventLoopPolicy()
self.assertRaises(RuntimeError, policy.get_event_loop)

th = threading.Thread(target=f)
th.start()
th.join()

def test_new_event_loop(self):
policy = asyncio._DefaultEventLoopPolicy()
policy = test_utils.DefaultEventLoopPolicy()

loop = policy.new_event_loop()
self.assertIsInstance(loop, asyncio.AbstractEventLoop)
loop.close()

def test_set_event_loop(self):
policy = asyncio._DefaultEventLoopPolicy()
policy = test_utils.DefaultEventLoopPolicy()
old_loop = policy.new_event_loop()
policy.set_event_loop(old_loop)

Expand All @@ -2909,7 +2909,7 @@ def test_get_event_loop_policy(self):
with self.assertWarnsRegex(
DeprecationWarning, "'asyncio.get_event_loop_policy' is deprecated"):
policy = asyncio.get_event_loop_policy()
self.assertIsInstance(policy, asyncio._AbstractEventLoopPolicy)
self.assertIsInstance(policy, asyncio.events._AbstractEventLoopPolicy)
self.assertIs(policy, asyncio.get_event_loop_policy())

def test_set_event_loop_policy(self):
Expand All @@ -2922,7 +2922,7 @@ def test_set_event_loop_policy(self):
DeprecationWarning, "'asyncio.get_event_loop_policy' is deprecated"):
old_policy = asyncio.get_event_loop_policy()

policy = asyncio._DefaultEventLoopPolicy()
policy = test_utils.DefaultEventLoopPolicy()
with self.assertWarnsRegex(
DeprecationWarning, "'asyncio.set_event_loop_policy' is deprecated"):
asyncio.set_event_loop_policy(policy)
Expand Down Expand Up @@ -3034,13 +3034,13 @@ def test_get_event_loop_returns_running_loop(self):
class TestError(Exception):
pass

class Policy(asyncio._DefaultEventLoopPolicy):
class Policy(test_utils.DefaultEventLoopPolicy):
def get_event_loop(self):
raise TestError

old_policy = asyncio._get_event_loop_policy()
old_policy = asyncio.events._get_event_loop_policy()
try:
asyncio._set_event_loop_policy(Policy())
asyncio.events._set_event_loop_policy(Policy())
loop = asyncio.new_event_loop()

with self.assertRaises(TestError):
Expand Down Expand Up @@ -3068,7 +3068,7 @@ async def func():
asyncio.get_event_loop()

finally:
asyncio._set_event_loop_policy(old_policy)
asyncio.events._set_event_loop_policy(old_policy)
if loop is not None:
loop.close()

Expand All @@ -3078,9 +3078,9 @@ async def func():
self.assertIs(asyncio._get_running_loop(), None)

def test_get_event_loop_returns_running_loop2(self):
old_policy = asyncio._get_event_loop_policy()
old_policy = asyncio.events._get_event_loop_policy()
try:
asyncio._set_event_loop_policy(asyncio._DefaultEventLoopPolicy())
asyncio.events._set_event_loop_policy(test_utils.DefaultEventLoopPolicy())
loop = asyncio.new_event_loop()
self.addCleanup(loop.close)

Expand All @@ -3106,7 +3106,7 @@ async def func():
asyncio.get_event_loop()

finally:
asyncio._set_event_loop_policy(old_policy)
asyncio.events._set_event_loop_policy(old_policy)
if loop is not None:
loop.close()

Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_asyncio/test_free_threading.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class MyException(Exception):


def tearDownModule():
asyncio._set_event_loop_policy(None)
asyncio.events._set_event_loop_policy(None)


class TestFreeThreading:
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_asyncio/test_futures.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@


def tearDownModule():
asyncio._set_event_loop_policy(None)
asyncio.events._set_event_loop_policy(None)


def _fakefunc(f):
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_asyncio/test_futures2.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@


def tearDownModule():
asyncio._set_event_loop_policy(None)
asyncio.events._set_event_loop_policy(None)


class FutureTests:
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_asyncio/test_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

# To prevent a warning "test altered the execution environment"
def tearDownModule():
asyncio._set_event_loop_policy(None)
asyncio.events._set_event_loop_policy(None)


def capture_test_stack(*, fut=None, depth=1):
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_asyncio/test_locks.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@


def tearDownModule():
asyncio._set_event_loop_policy(None)
asyncio.events._set_event_loop_policy(None)


class LockTests(unittest.IsolatedAsyncioTestCase):
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_asyncio/test_pep492.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@


def tearDownModule():
asyncio._set_event_loop_policy(None)
asyncio.events._set_event_loop_policy(None)


# Test that asyncio.iscoroutine() uses collections.abc.Coroutine
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_asyncio/test_proactor_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@


def tearDownModule():
asyncio._set_event_loop_policy(None)
asyncio.events._set_event_loop_policy(None)


def close_transport(transport):
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_asyncio/test_protocols.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
def tearDownModule():
# not needed for the test file but added for uniformness with all other
# asyncio test files for the sake of unified cleanup
asyncio._set_event_loop_policy(None)
asyncio.events._set_event_loop_policy(None)


class ProtocolsAbsTests(unittest.TestCase):
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_asyncio/test_queues.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@


def tearDownModule():
asyncio._set_event_loop_policy(None)
asyncio.events._set_event_loop_policy(None)


class QueueBasicTests(unittest.IsolatedAsyncioTestCase):
Expand Down
Loading
Loading