diff --git a/demos/blog/blog.py b/demos/blog/blog.py index b531782219..05ef84db30 100755 --- a/demos/blog/blog.py +++ b/demos/blog/blog.py @@ -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() diff --git a/docs/web.rst b/docs/web.rst index 71e5806b7d..889b429956 100644 --- a/docs/web.rst +++ b/docs/web.rst @@ -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 diff --git a/docs/websocket.rst b/docs/websocket.rst index 219254966f..7c200511a9 100644 --- a/docs/websocket.rst +++ b/docs/websocket.rst @@ -29,7 +29,6 @@ Other ----- - .. automethod:: WebSocketHandler.async_callback .. automethod:: WebSocketHandler.ping .. automethod:: WebSocketHandler.on_pong .. autoexception:: WebSocketClosedError diff --git a/tornado/web.py b/tornado/web.py index 7a3c694f68..209b7ecd02 100644 --- a/tornado/web.py +++ b/tornado/web.py @@ -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): diff --git a/tornado/websocket.py b/tornado/websocket.py index ee61d03394..2704c26c1a 100644 --- a/tornado/websocket.py +++ b/tornado/websocket.py @@ -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") @@ -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() @@ -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): @@ -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() @@ -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): @@ -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 @@ -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()