-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Resp prepare #525
Merged
Merged
Resp prepare #525
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3c5baef
In the middle of switching from start to prepare
asvetlov 058b04b
Move on
asvetlov 8e3a7a7
Convert yet another tests
asvetlov f354fe8
Convert another test file
asvetlov 32485b6
Add coroutine docorator
asvetlov b1091c3
More tests
asvetlov efee4fe
Additional tests
asvetlov 22c42c4
Update the doc
asvetlov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,12 +35,18 @@ def __init__(self, *, | |
self._autoclose = autoclose | ||
self._autoping = autoping | ||
|
||
def start(self, request): | ||
def prepare(self, request): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Coroutine decorator missed here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed |
||
# make pre-check to don't hide it by do_handshake() exceptions | ||
resp_impl = self._start_pre_check(request) | ||
if resp_impl is not None: | ||
return resp_impl | ||
|
||
parser, protocol, writer = self._pre_start(request) | ||
resp_impl = yield from super().prepare(request) | ||
self._post_start(request, parser, protocol, writer) | ||
return resp_impl | ||
|
||
def _pre_start(self, request): | ||
try: | ||
status, headers, parser, writer, protocol = do_handshake( | ||
request.method, request.headers, request.transport, | ||
|
@@ -59,14 +65,24 @@ def start(self, request): | |
for k, v in headers: | ||
self.headers[k] = v | ||
self.force_close() | ||
return parser, protocol, writer | ||
|
||
resp_impl = super().start(request) | ||
|
||
def _post_start(self, request, parser, protocol, writer): | ||
self._reader = request._reader.set_parser(parser) | ||
self._writer = writer | ||
self._protocol = protocol | ||
self._loop = request.app.loop | ||
|
||
def start(self, request): | ||
warnings.warn('use .prepare(request) instead', DeprecationWarning) | ||
# make pre-check to don't hide it by do_handshake() exceptions | ||
resp_impl = self._start_pre_check(request) | ||
if resp_impl is not None: | ||
return resp_impl | ||
|
||
parser, protocol, writer = self._pre_start(request) | ||
resp_impl = super().start(request) | ||
self._post_start(request, parser, protocol, writer) | ||
return resp_impl | ||
|
||
def can_start(self, request): | ||
|
@@ -98,22 +114,22 @@ def exception(self): | |
|
||
def ping(self, message='b'): | ||
if self._writer is None: | ||
raise RuntimeError('Call .start() first') | ||
raise RuntimeError('Call .prepare() first') | ||
if self._closed: | ||
raise RuntimeError('websocket connection is closing') | ||
self._writer.ping(message) | ||
|
||
def pong(self, message='b'): | ||
# unsolicited pong | ||
if self._writer is None: | ||
raise RuntimeError('Call .start() first') | ||
raise RuntimeError('Call .prepare() first') | ||
if self._closed: | ||
raise RuntimeError('websocket connection is closing') | ||
self._writer.pong(message) | ||
|
||
def send_str(self, data): | ||
if self._writer is None: | ||
raise RuntimeError('Call .start() first') | ||
raise RuntimeError('Call .prepare() first') | ||
if self._closed: | ||
raise RuntimeError('websocket connection is closing') | ||
if not isinstance(data, str): | ||
|
@@ -122,7 +138,7 @@ def send_str(self, data): | |
|
||
def send_bytes(self, data): | ||
if self._writer is None: | ||
raise RuntimeError('Call .start() first') | ||
raise RuntimeError('Call .prepare() first') | ||
if self._closed: | ||
raise RuntimeError('websocket connection is closing') | ||
if not isinstance(data, (bytes, bytearray, memoryview)): | ||
|
@@ -151,7 +167,7 @@ def write_eof(self): | |
@asyncio.coroutine | ||
def close(self, *, code=1000, message=b''): | ||
if self._writer is None: | ||
raise RuntimeError('Call .start() first') | ||
raise RuntimeError('Call .prepare() first') | ||
|
||
if not self._closed: | ||
self._closed = True | ||
|
@@ -190,7 +206,7 @@ def close(self, *, code=1000, message=b''): | |
@asyncio.coroutine | ||
def receive(self): | ||
if self._reader is None: | ||
raise RuntimeError('Call .start() first') | ||
raise RuntimeError('Call .prepare() first') | ||
if self._waiting: | ||
raise RuntimeError('Concurrent call to receive() is not allowed') | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems
.start
remain plain old function while.prepare
is coroutine. Are they really interchangeable during deprecation period?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They are do interchangeable with a note:
.prepare()
will process signal handing for response sending but.start()
will not.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good to know.