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

Refactor ClientResponse constructor #2820

Merged
merged 2 commits into from
Mar 10, 2018
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
2 changes: 2 additions & 0 deletions CHANGES/2820.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Refactor ``ClientResponse`` constructor: make logically required
constructor arguments mandatory, drop ``_post_init()`` method.
31 changes: 11 additions & 20 deletions aiohttp/client_reqrep.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
from .helpers import PY_36, HeadersMixin, TimerNoop, noop, reify, set_result
from .http import SERVER_SOFTWARE, HttpVersion10, HttpVersion11, StreamWriter
from .log import client_logger
from .streams import StreamReader


try:
Expand Down Expand Up @@ -532,8 +531,9 @@ async def send(self, conn):
request_info=self.request_info,
auto_decompress=self._auto_decompress,
traces=self._traces,
loop=self.loop,
session=self._session
)
self.response._post_init(self.loop, self._session)
return self.response

async def close(self):
Expand Down Expand Up @@ -566,19 +566,16 @@ class ClientResponse(HeadersMixin):
raw_headers = None # Response raw headers, a sequence of pairs

_connection = None # current connection
flow_control_class = StreamReader # reader flow control
_reader = None # input stream
_source_traceback = None
# setted up by ClientRequest after ClientResponse object creation
# post-init stage allows to not change ctor signature
_loop = None
_closed = True # to allow __del__ for non-initialized properly response
_session = None

def __init__(self, method, url, *,
writer=None, continue100=None, timer=None,
request_info=None, auto_decompress=True,
traces=None):
writer, continue100, timer,
request_info, auto_decompress,
traces, loop, session):
assert isinstance(url, URL)

self.method = method
Expand All @@ -588,16 +585,18 @@ def __init__(self, method, url, *,
self._url = url
self._content = None
self._writer = writer
self._continue = continue100
self._continue = continue100 # None by default
self._closed = True
self._history = ()
self._request_info = request_info
self._timer = timer if timer is not None else TimerNoop()
self._auto_decompress = auto_decompress
self._auto_decompress = auto_decompress # True by default
self._cache = {} # reqired for @reify method decorator
if traces is None:
traces = []
self._traces = traces
self._loop = loop
self._session = session # store a reference to session #1985
if loop.get_debug():
self._source_traceback = traceback.extract_stack(sys._getframe(1))

@property
def url(self):
Expand Down Expand Up @@ -631,15 +630,7 @@ def content_disposition(self):
filename = multipart.content_disposition_filename(params)
return ContentDisposition(disposition_type, params, filename)

def _post_init(self, loop, session):
self._loop = loop
self._session = session # store a reference to session #1985
if loop.get_debug():
self._source_traceback = traceback.extract_stack(sys._getframe(1))

def __del__(self, _warnings=warnings):
if self._loop is None:
return # not started
if self._closed:
return

Expand Down
12 changes: 10 additions & 2 deletions tests/test_client_proto.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from aiohttp.client_exceptions import ClientOSError, ServerDisconnectedError
from aiohttp.client_proto import ResponseHandler
from aiohttp.client_reqrep import ClientResponse
from aiohttp.helpers import TimerNoop


async def test_oserror(loop):
Expand Down Expand Up @@ -65,8 +66,15 @@ async def test_client_protocol_readuntil_eof(loop):

proto.data_received(b'HTTP/1.1 200 Ok\r\n\r\n')

response = ClientResponse('get', URL('http://def-cl-resp.org'))
response._post_init(loop, mock.Mock())
response = ClientResponse('get', URL('http://def-cl-resp.org'),
writer=mock.Mock(),
continue100=None,
timer=TimerNoop(),
request_info=mock.Mock(),
auto_decompress=True,
traces=[],
loop=loop,
session=mock.Mock())
await response.start(conn, read_until_eof=True)

assert not response.content.is_eof()
Expand Down
9 changes: 7 additions & 2 deletions tests/test_client_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -1077,8 +1077,13 @@ async def send(self, conn):
resp = self.response_class(self.method,
self.url,
writer=self._writer,
continue100=self._continue)
resp._post_init(self.loop, mock.Mock())
continue100=self._continue,
timer=self._timer,
request_info=self.request_info,
auto_decompress=self._auto_decompress,
traces=self._traces,
loop=self.loop,
session=self._session)
self.response = resp
nonlocal called
called = True
Expand Down
Loading