Skip to content

Commit

Permalink
Remove async_callback wrapper functions, which have been obsolete sin…
Browse files Browse the repository at this point in the history
…ce 1.1.

Closes tornadoweb#283.
  • Loading branch information
bdarnell committed Jun 15, 2014
1 parent 409ddc4 commit 7a0eda1
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 54 deletions.
2 changes: 1 addition & 1 deletion demos/blog/blog.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ class AuthLoginHandler(BaseHandler, tornado.auth.GoogleMixin):
@tornado.web.asynchronous
def get(self):
if self.get_argument("openid.mode", None):
self.get_authenticated_user(self.async_callback(self._on_auth))
self.get_authenticated_user(self._on_auth)
return
self.authenticate_redirect()

Expand Down
1 change: 0 additions & 1 deletion docs/web.rst
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@

The `Application` object serving this request

.. automethod:: RequestHandler.async_callback
.. automethod:: RequestHandler.check_etag_header
.. automethod:: RequestHandler.check_xsrf_cookie
.. automethod:: RequestHandler.compute_etag
Expand Down
1 change: 0 additions & 1 deletion docs/websocket.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
Other
-----

.. automethod:: WebSocketHandler.async_callback
.. automethod:: WebSocketHandler.ping
.. automethod:: WebSocketHandler.on_pong
.. autoexception:: WebSocketClosedError
Expand Down
21 changes: 0 additions & 21 deletions tornado/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -1242,27 +1242,6 @@ def static_url(self, path, include_host=None, **kwargs):

return base + get_url(self.settings, path, **kwargs)

def async_callback(self, callback, *args, **kwargs):
"""Obsolete - catches exceptions from the wrapped function.
This function is unnecessary since Tornado 1.1.
"""
if callback is None:
return None
if args or kwargs:
callback = functools.partial(callback, *args, **kwargs)

def wrapper(*args, **kwargs):
try:
return callback(*args, **kwargs)
except Exception as e:
if self._headers_written:
app_log.error("Exception after headers written",
exc_info=True)
else:
self._handle_request_exception(e)
return wrapper

def require_setting(self, name, feature="this feature"):
"""Raises an exception if the given app setting is not defined."""
if not self.application.settings.get(name):
Expand Down
48 changes: 18 additions & 30 deletions tornado/websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,14 +346,6 @@ def get_websocket_scheme(self):
"""
return "wss" if self.request.protocol == "https" else "ws"

def async_callback(self, callback, *args, **kwargs):
"""Obsolete - catches exceptions from the wrapped function.
This function is normally unncecessary thanks to
`tornado.stack_context`.
"""
return self.ws_connection.async_callback(callback, *args, **kwargs)

def _not_supported(self, *args, **kwargs):
raise Exception("Method not supported for Web Sockets")

Expand All @@ -379,23 +371,17 @@ def __init__(self, handler):
self.client_terminated = False
self.server_terminated = False

def async_callback(self, callback, *args, **kwargs):
"""Wrap callbacks with this if they are used on asynchronous requests.
def _run_callback(self, callback, *args, **kwargs):
"""Runs the given callback with exception handling.
Catches exceptions properly and closes this WebSocket if an exception
is uncaught.
On error, aborts the websocket connection and returns False.
"""
if args or kwargs:
callback = functools.partial(callback, *args, **kwargs)

def wrapper(*args, **kwargs):
try:
return callback(*args, **kwargs)
except Exception:
app_log.error("Uncaught exception in %s",
self.request.path, exc_info=True)
self._abort()
return wrapper
try:
callback(*args, **kwargs)
except Exception:
app_log.error("Uncaught exception in %s",
self.request.path, exc_info=True)
self._abort()

def on_connection_close(self):
self._abort()
Expand Down Expand Up @@ -486,7 +472,8 @@ def _handle_challenge(self, challenge):

def _write_response(self, challenge):
self.stream.write(challenge)
self.async_callback(self.handler.open)(*self.handler.open_args, **self.handler.open_kwargs)
self._run_callback(self.handler.open, *self.handler.open_args,
**self.handler.open_kwargs)
self._receive_message()

def _handle_websocket_headers(self):
Expand Down Expand Up @@ -534,8 +521,8 @@ def _on_frame_type(self, byte):

def _on_end_delimiter(self, frame):
if not self.client_terminated:
self.async_callback(self.handler.on_message)(
frame[:-1].decode("utf-8", "replace"))
self._run_callback(self.handler.on_message,
frame[:-1].decode("utf-8", "replace"))
if not self.client_terminated:
self._receive_message()

Expand Down Expand Up @@ -645,7 +632,8 @@ def _accept_connection(self):
"%s"
"\r\n" % (self._challenge_response(), subprotocol_header)))

self.async_callback(self.handler.open)(*self.handler.open_args, **self.handler.open_kwargs)
self._run_callback(self.handler.open, *self.handler.open_args,
**self.handler.open_kwargs)
self._receive_frame()

def _write_frame(self, fin, opcode, data):
Expand Down Expand Up @@ -803,10 +791,10 @@ def _handle_message(self, opcode, data):
except UnicodeDecodeError:
self._abort()
return
self.async_callback(self.handler.on_message)(decoded)
self._run_callback(self.handler.on_message, decoded)
elif opcode == 0x2:
# Binary data
self.async_callback(self.handler.on_message)(data)
self._run_callback(self.handler.on_message, decoded)
elif opcode == 0x8:
# Close
self.client_terminated = True
Expand All @@ -820,7 +808,7 @@ def _handle_message(self, opcode, data):
self._write_frame(True, 0xA, data)
elif opcode == 0xA:
# Pong
self.async_callback(self.handler.on_pong)(data)
self._run_callback(self.handler.on_pong, data)
else:
self._abort()

Expand Down

0 comments on commit 7a0eda1

Please sign in to comment.