diff --git a/aiohttp/__init__.py b/aiohttp/__init__.py index 6b89bc0db87..7aeb03437cc 100644 --- a/aiohttp/__init__.py +++ b/aiohttp/__init__.py @@ -10,7 +10,7 @@ WSMsgType, WSCloseCode, WSMessage, WebSocketError) # noqa from .streams import * # noqa from .multipart import * # noqa -from .cookiejar import CookieJar # noqa +from .cookiejar import * # noqa from .payload import * # noqa from .payload_streamer import * # noqa from .resolver import * # noqa @@ -23,6 +23,7 @@ __all__ = (client.__all__ + # noqa + cookiejar.__all__ + # noqa formdata.__all__ + # noqa helpers.__all__ + # noqa multipart.__all__ + # noqa @@ -31,5 +32,5 @@ streams.__all__ + # noqa ('hdrs', 'HttpVersion', 'HttpVersion10', 'HttpVersion11', 'WSMsgType', 'WSCloseCode', - 'WebSocketError', 'WSMessage', 'CookieJar', + 'WebSocketError', 'WSMessage', ) + workers) diff --git a/aiohttp/cookiejar.py b/aiohttp/cookiejar.py index 37e802cb15d..07c263cbab6 100644 --- a/aiohttp/cookiejar.py +++ b/aiohttp/cookiejar.py @@ -13,6 +13,9 @@ from .helpers import SimpleCookie, is_ip_address +__all__ = ('CookieJar', 'DummyCookieJar') + + class CookieJar(AbstractCookieJar): """Implements cookie storage adhering to RFC 6265.""" @@ -304,3 +307,30 @@ def _parse_date(cls, date_str): return datetime.datetime(year, month, day, hour, minute, second, tzinfo=datetime.timezone.utc) + + +class DummyCookieJar(AbstractCookieJar): + """Implements a dummy cookie storage. + + It can be used with the ClientSession when no cookie processing is needed. + + """ + + def __init__(self, *, loop=None): + super().__init__(loop=loop) + + def __iter__(self): + while False: + yield None + + def __len__(self): + return 0 + + def clear(self): + pass + + def update_cookies(self, cookies, response_url=None): + pass + + def filter_cookies(self, request_url): + return None diff --git a/aiohttp/helpers.py b/aiohttp/helpers.py index b3cf1c4415f..cb8725bc9fa 100644 --- a/aiohttp/helpers.py +++ b/aiohttp/helpers.py @@ -21,7 +21,6 @@ from async_timeout import timeout from . import hdrs -from .abc import AbstractCookieJar try: @@ -40,7 +39,7 @@ __all__ = ('BasicAuth', 'create_future', 'parse_mimetype', - 'Timeout', 'ensure_future', 'noop', 'DummyCookieJar') + 'Timeout', 'ensure_future', 'noop') sentinel = object() @@ -794,30 +793,3 @@ def content_length(self, *, _CONTENT_LENGTH=hdrs.CONTENT_LENGTH): return None else: return int(l) - - -class DummyCookieJar(AbstractCookieJar): - """Implements a dummy cookie storage. - - It can be used with the ClientSession when no cookie processing is needed. - - """ - - def __init__(self, *, loop=None): - super().__init__(loop=loop) - - def __iter__(self): - while False: - yield None - - def __len__(self): - return 0 - - def clear(self): - pass - - def update_cookies(self, cookies, response_url=None): - pass - - def filter_cookies(self, request_url): - return None diff --git a/changes/2242.feature b/changes/2242.feature new file mode 100644 index 00000000000..8beb87d707a --- /dev/null +++ b/changes/2242.feature @@ -0,0 +1 @@ +Move DummyCookieJar into cookiejar.py diff --git a/docs/client.rst b/docs/client.rst index daba07f2efe..4ba558fefc7 100644 --- a/docs/client.rst +++ b/docs/client.rst @@ -440,8 +440,21 @@ cookies. It should be done by passing `unsafe=True` to :class:`aiohttp.CookieJar` constructor:: - jar = aiohttp.CookieJar(unsafe=True) - session = aiohttp.ClientSession(cookie_jar=jar) + jar = aiohttp.CookieJar(unsafe=True) + session = aiohttp.ClientSession(cookie_jar=jar) + + +.. _aiohttp-client-dummy-cookie-jar: + +Dummy Cookie Jar +---------------- + +Sometimes cookie processing is not desirable. For this purpose it's +possible to pass :class:`aiohttp.DummyCookieJar` instance into client +session:: + + jar = aiohttp.DummyCookieJar() + session = aiohttp.ClientSession(cookie_jar=jar) Connectors diff --git a/docs/client_reference.rst b/docs/client_reference.rst index 00d60c9ee82..42a29e9080b 100644 --- a/docs/client_reference.rst +++ b/docs/client_reference.rst @@ -1345,7 +1345,7 @@ BasicAuth CookieJar ^^^^^^^^^ -.. class:: CookieJar(unsafe=False, loop=None) +.. class:: CookieJar(*, unsafe=False, loop=None) The cookie jar instance is available as :attr:`ClientSession.cookie_jar`. @@ -1416,6 +1416,19 @@ CookieJar imported, :class:`str` or :class:`pathlib.Path` instance. + +.. class:: DummyCookieJar(*, loop=None) + + Dummy cookie jar which does not store cookies but ignores them. + + Could be useful e.g. for web crawlers to iterate over Internet + without blowing up with saved cookies information. + + To install dummy cookie jar pass it into session instance:: + + jar = aiohttp.DummyCookieJar() + session = aiohttp.ClientSession(cookie_jar=DummyCookieJar()) + Client exceptions ----------------- diff --git a/tests/test_cookiejar.py b/tests/test_cookiejar.py index 28475f26dcc..fba021248bb 100644 --- a/tests/test_cookiejar.py +++ b/tests/test_cookiejar.py @@ -9,7 +9,7 @@ import pytest from yarl import URL -from aiohttp import CookieJar +from aiohttp import CookieJar, DummyCookieJar from aiohttp.helpers import SimpleCookie @@ -596,3 +596,15 @@ def test_cookie_not_expired_when_added_after_removal(self): # Assert that there is a cookie. assert len(jar) == 1 + + +def test_dummy_cookie_jar(loop): + cookie = SimpleCookie('foo=bar; Domain=example.com;') + dummy_jar = DummyCookieJar(loop=loop) + assert len(dummy_jar) == 0 + dummy_jar.update_cookies(cookie) + assert len(dummy_jar) == 0 + with pytest.raises(StopIteration): + next(iter(dummy_jar)) + assert dummy_jar.filter_cookies(URL("http://example.com/")) is None + dummy_jar.clear() diff --git a/tests/test_helpers.py b/tests/test_helpers.py index 00c7a3dab13..fba1b059a58 100644 --- a/tests/test_helpers.py +++ b/tests/test_helpers.py @@ -6,7 +6,6 @@ from unittest import mock import pytest -from yarl import URL from aiohttp import helpers @@ -547,15 +546,3 @@ def test_set_content_disposition_bad_param(): with pytest.raises(ValueError): helpers.content_disposition_header('inline', **{'foo\x00bar': 'baz'}) - - -def test_dummy_cookie_jar(loop): - cookie = helpers.SimpleCookie('foo=bar; Domain=example.com;') - dummy_jar = helpers.DummyCookieJar(loop=loop) - assert len(dummy_jar) == 0 - dummy_jar.update_cookies(cookie) - assert len(dummy_jar) == 0 - with pytest.raises(StopIteration): - next(iter(dummy_jar)) - assert dummy_jar.filter_cookies(URL("http://example.com/")) is None - dummy_jar.clear()