Skip to content
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

gh-72889: remove redundant mock.Mock()._is_coroutine = False workarounds #94926

Merged
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
10 changes: 0 additions & 10 deletions Lib/test/test_asyncio/test_base_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ def mock_socket_module():

m_socket.socket = mock.MagicMock()
m_socket.socket.return_value = test_utils.mock_nonblocking_socket()
m_socket.getaddrinfo._is_coroutine = False

return m_socket

Expand Down Expand Up @@ -1286,9 +1285,7 @@ def _test_create_connection_ip_addr(self, m_socket, allow_inet_pton):
sock = m_socket.socket.return_value

self.loop._add_reader = mock.Mock()
self.loop._add_reader._is_coroutine = False
Comment on lines 1287 to -1289
Copy link
Contributor Author

@graingert graingert Jul 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when asyncio.iscoroutinefunction was implemented as:

def iscoroutinefunction(func):
"""Return True if func is a decorated coroutine function."""
return (getattr(func, '_is_coroutine', False) or
_inspect_iscoroutinefunction(func))

this meant

self.loop._add_reader = mock.Mock()
assert asyncio.iscoroutinefunction(self.loop._add_reader) is True  # mock objects have a truthy _is_coroutine attribute!
self.loop._add_reader._is_coroutine = False
assert asyncio.iscoroutinefunction(self.loop._add_reader) is False  # patching it works around the issue

however the implementation was changed to use a marker object:

# A marker for iscoroutinefunction.
_is_coroutine = object()
def iscoroutinefunction(func):
"""Return True if func is a decorated coroutine function."""
return (inspect.iscoroutinefunction(func) or
getattr(func, '_is_coroutine', None) is _is_coroutine)

and so now the _is_coroutine = False work-around is redundant:

self.loop._add_reader = mock.Mock()
assert asyncio.iscoroutinefunction(self.loop._add_reader) is False  # mock objects have an _is_coroutine but it's not the asyncio.coroutines._is_coroutine sentinel
self.loop._add_reader._is_coroutine = False
assert asyncio.iscoroutinefunction(self.loop._add_reader) is False  # it's still False so the workaround is redundant

self.loop._add_writer = mock.Mock()
self.loop._add_writer._is_coroutine = False

coro = self.loop.create_connection(asyncio.Protocol, '1.2.3.4', 80)
t, p = self.loop.run_until_complete(coro)
Expand Down Expand Up @@ -1330,9 +1327,7 @@ def test_create_connection_ipv6_scope(self, m_socket):
sock.family = socket.AF_INET6

self.loop._add_reader = mock.Mock()
self.loop._add_reader._is_coroutine = False
self.loop._add_writer = mock.Mock()
self.loop._add_writer._is_coroutine = False

coro = self.loop.create_connection(asyncio.Protocol, 'fe80::1%1', 80)
t, p = self.loop.run_until_complete(coro)
Expand All @@ -1359,9 +1354,7 @@ def test_create_connection_service_name(self, m_socket):
sock = m_socket.socket.return_value

self.loop._add_reader = mock.Mock()
self.loop._add_reader._is_coroutine = False
self.loop._add_writer = mock.Mock()
self.loop._add_writer._is_coroutine = False

for service, port in ('http', 80), (b'http', 80):
coro = self.loop.create_connection(asyncio.Protocol,
Expand Down Expand Up @@ -1590,7 +1583,6 @@ class Err(OSError):

m_socket.getaddrinfo.return_value = [
(2, 1, 6, '', ('127.0.0.1', 10100))]
m_socket.getaddrinfo._is_coroutine = False
m_sock = m_socket.socket.return_value = mock.Mock()
m_sock.bind.side_effect = Err

Expand All @@ -1601,7 +1593,6 @@ class Err(OSError):
@patch_socket
def test_create_datagram_endpoint_no_addrinfo(self, m_socket):
m_socket.getaddrinfo.return_value = []
m_socket.getaddrinfo._is_coroutine = False

coro = self.loop.create_datagram_endpoint(
MyDatagramProto, local_addr=('localhost', 0))
Expand Down Expand Up @@ -1835,7 +1826,6 @@ def getaddrinfo(*args, **kw):
m_socket.getaddrinfo = getaddrinfo
m_socket.socket.return_value.bind = bind = mock.Mock()
self.loop._add_reader = mock.Mock()
self.loop._add_reader._is_coroutine = False

reuseport_supported = hasattr(socket, 'SO_REUSEPORT')
coro = self.loop.create_datagram_endpoint(
Expand Down
1 change: 0 additions & 1 deletion Lib/test/test_asyncio/test_selector_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ def setUp(self):
def test_make_socket_transport(self):
m = mock.Mock()
self.loop.add_reader = mock.Mock()
self.loop.add_reader._is_coroutine = False
transport = self.loop._make_socket_transport(m, asyncio.Protocol())
self.assertIsInstance(transport, _SelectorSocketTransport)

Expand Down
2 changes: 0 additions & 2 deletions Lib/test/test_asyncio/test_subprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ def setUp(self):

def create_transport(self, waiter=None):
protocol = mock.Mock()
protocol.connection_made._is_coroutine = False
protocol.process_exited._is_coroutine = False
transport = TestSubprocessTransport(
self.loop, protocol, ['test'], False,
None, None, None, 0, waiter=waiter)
Expand Down