Skip to content

Commit

Permalink
Deprecate bare connector close (#3417)
Browse files Browse the repository at this point in the history
  • Loading branch information
asvetlov committed Dec 1, 2018
1 parent 66d1f9c commit 1618fe6
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGES/3417.removal
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Deprecate bare connector close, use ``async with connector:`` and ``await connector.close()`` instead.
2 changes: 1 addition & 1 deletion aiohttp/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -819,7 +819,7 @@ async def close(self) -> None:
"""
if not self.closed:
if self._connector is not None and self._connector_owner:
self._connector.close()
await self._connector.close()
self._connector = None

@property
Expand Down
45 changes: 40 additions & 5 deletions aiohttp/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from http.cookies import SimpleCookie
from itertools import cycle, islice
from time import monotonic
from types import TracebackType
from typing import (TYPE_CHECKING, Any, Awaitable, Callable, # noqa
DefaultDict, Dict, Iterator, List, Optional, Set, Tuple,
Type, Union, cast)
Expand All @@ -27,7 +28,7 @@
from .client_proto import ResponseHandler
from .client_reqrep import ClientRequest, Fingerprint, _merge_ssl_params
from .helpers import (PY_36, CeilTimeout, get_running_loop, is_ip_address,
noop, sentinel)
noop2, sentinel)
from .http import RESPONSES
from .locks import EventResultOrError
from .resolver import DefaultResolver
Expand All @@ -50,6 +51,24 @@
from .tracing import Trace # noqa


class _DeprecationWaiter:
__slots__ = ('_awaitable', '_awaited')

def __init__(self, awaitable: Awaitable[Any]) -> None:
self._awaitable = awaitable
self._awaited = False

def __await__(self) -> Any:
self._awaited = True
return self._awaitable.__await__()

def __del__(self) -> None:
if not self._awaited:
warnings.warn("Connector.close() is a coroutine, "
"please use await connector.close()",
DeprecationWarning)


class Connection:

_source_traceback = None
Expand Down Expand Up @@ -223,7 +242,7 @@ def __del__(self, _warnings: Any=warnings) -> None:

conns = [repr(c) for c in self._conns.values()]

self.close()
self._close()

if PY_36:
kwargs = {'source': self}
Expand All @@ -240,11 +259,24 @@ def __del__(self, _warnings: Any=warnings) -> None:
self._loop.call_exception_handler(context)

def __enter__(self) -> 'BaseConnector':
warnings.warn('"witn Connector():" is deprecated, '
'use "async with Connector():" instead',
DeprecationWarning)
return self

def __exit__(self, *exc: Any) -> None:
self.close()

async def __aenter__(self) -> 'BaseConnector':
return self

async def __aexit__(self,
exc_type: Optional[Type[BaseException]]=None,
exc_value: Optional[BaseException]=None,
exc_traceback: Optional[TracebackType]=None
) -> None:
await self.close()

@property
def force_close(self) -> bool:
"""Ultimately close connection on releasing if True."""
Expand Down Expand Up @@ -334,14 +366,18 @@ def _cleanup_closed(self) -> None:

def close(self) -> Awaitable[None]:
"""Close all opened transports."""
self._close()
return _DeprecationWaiter(noop2())

def _close(self) -> None:
if self._closed:
return noop()
return

self._closed = True

try:
if self._loop.is_closed():
return noop()
return

# cancel cleanup task
if self._cleanup_handle:
Expand Down Expand Up @@ -369,7 +405,6 @@ def close(self) -> Awaitable[None]:
self._cleanup_handle = None
self._cleanup_closed_transports.clear()
self._cleanup_closed_handle = None
return noop()

@property
def closed(self) -> bool:
Expand Down
4 changes: 4 additions & 0 deletions aiohttp/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ def noop(*args, **kwargs): # type: ignore
return # type: ignore


async def noop2(*args: Any, **kwargs: Any) -> None:
return


coroutines._DEBUG = old_debug # type: ignore


Expand Down
2 changes: 1 addition & 1 deletion docs/client_reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -843,7 +843,7 @@ BaseConnector

Read-only property.

.. method:: close()
.. comethod:: close()

Close all opened connections.

Expand Down
15 changes: 12 additions & 3 deletions tests/test_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,12 +222,21 @@ async def test_create_conn(loop) -> None:

async def test_context_manager(loop) -> None:
conn = aiohttp.BaseConnector(loop=loop)
conn.close = mock.Mock()

with conn as c:
with pytest.warns(DeprecationWarning):
with conn as c:
assert conn is c

assert conn.closed


async def test_async_context_manager(loop) -> None:
conn = aiohttp.BaseConnector(loop=loop)

async with conn as c:
assert conn is c

assert conn.close.called
assert conn.closed


async def test_close(loop) -> None:
Expand Down

0 comments on commit 1618fe6

Please sign in to comment.