From: Ben Darnell Date: Mon, 28 Sep 2015 01:05:04 +0000 (-0400) Subject: Accept arguments in `raise Finish()`. X-Git-Tag: v4.3.0b1~27^2~1 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=5390ea3da16a7f1cc1f9e71d65fb955cb333e8ac;p=thirdparty%2Ftornado.git Accept arguments in `raise Finish()`. Fixes #1474. --- diff --git a/tornado/test/web_test.py b/tornado/test/web_test.py index 89a54cfab..5ddd68de9 100644 --- a/tornado/test/web_test.py +++ b/tornado/test/web_test.py @@ -2544,15 +2544,19 @@ class FinishExceptionTest(SimpleHandlerTestCase): def get(self): self.set_status(401) self.set_header('WWW-Authenticate', 'Basic realm="something"') - self.write('authentication required') - raise Finish() + if self.get_argument('finish_value', ''): + raise Finish('authentication required') + else: + self.write('authentication required') + raise Finish() def test_finish_exception(self): - response = self.fetch('/') - self.assertEqual(response.code, 401) - self.assertEqual('Basic realm="something"', - response.headers.get('WWW-Authenticate')) - self.assertEqual(b'authentication required', response.body) + for url in ['/', '/?finish_value=1']: + response = self.fetch(url) + self.assertEqual(response.code, 401) + self.assertEqual('Basic realm="something"', + response.headers.get('WWW-Authenticate')) + self.assertEqual(b'authentication required', response.body) @wsgi_safe diff --git a/tornado/web.py b/tornado/web.py index b33ddbb48..f6ae767b0 100644 --- a/tornado/web.py +++ b/tornado/web.py @@ -1473,7 +1473,7 @@ class RequestHandler(object): if isinstance(e, Finish): # Not an error; just finish the request without logging. if not self._finished: - self.finish() + self.finish(*e.args) return try: self.log_exception(*sys.exc_info()) @@ -2084,10 +2084,14 @@ class HTTPError(Exception): class Finish(Exception): """An exception that ends the request without producing an error response. - When `Finish` is raised in a `RequestHandler`, the request will end - (calling `RequestHandler.finish` if it hasn't already been called), - but the outgoing response will not be modified and the error-handling - methods (including `RequestHandler.write_error`) will not be called. + When `Finish` is raised in a `RequestHandler`, the request will + end (calling `RequestHandler.finish` if it hasn't already been + called), but the error-handling methods (including + `RequestHandler.write_error`) will not be called. + + If `Finish()` was created with no arguments, the pending response + will be sent as-is. If `Finish()` was given an argument, that + argument will be passed to `RequestHandler.finish()`. This can be a more convenient way to implement custom error pages than overriding ``write_error`` (especially in library code):: @@ -2096,6 +2100,10 @@ class Finish(Exception): self.set_status(401) self.set_header('WWW-Authenticate', 'Basic realm="something"') raise Finish() + + .. versionchanged:: 4.3 + Arguments passed to ``Finish()`` will be passed on to + `RequestHandler.finish`. """ pass