From: Ben Darnell Date: Tue, 19 Feb 2013 04:20:16 +0000 (-0500) Subject: Test the @return_future return value assertion and move it again. X-Git-Tag: v3.0.0~102 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1c1f85d027e902c4d1ebaff26ee2dc3a7223af4d;p=thirdparty%2Ftornado.git Test the @return_future return value assertion and move it again. --- diff --git a/tornado/concurrent.py b/tornado/concurrent.py index 66cea30be..59075a3a4 100644 --- a/tornado/concurrent.py +++ b/tornado/concurrent.py @@ -26,6 +26,8 @@ try: except ImportError: futures = None +class ReturnValueIgnoredError(Exception): + pass class DummyFuture(object): def __init__(self): @@ -158,11 +160,13 @@ def return_future(f): with ExceptionStackContext(handle_error): try: result = f(*args, **kwargs) + if result is not None: + raise ReturnValueIgnoredError( + "@return_future should not be used with functions " + "that return values") except: exc_info = sys.exc_info() raise - assert result is None, ("@return_future should not be used with " - "functions that return values") if exc_info is not None: # If the initial synchronous part of f() raised an exception, # go ahead and raise it to the caller directly without waiting diff --git a/tornado/test/concurrent_test.py b/tornado/test/concurrent_test.py index f0add7794..94ca25151 100644 --- a/tornado/test/concurrent_test.py +++ b/tornado/test/concurrent_test.py @@ -19,7 +19,7 @@ import logging import re import socket -from tornado.concurrent import Future, return_future +from tornado.concurrent import Future, return_future, ReturnValueIgnoredError from tornado.escape import utf8, to_unicode from tornado import gen from tornado.iostream import IOStream @@ -44,6 +44,13 @@ class ReturnFutureTest(AsyncTestCase): def delayed_failure(self, callback): self.io_loop.add_callback(lambda: 1 / 0) + @return_future + def return_value(self, callback): + # Note that the result of both running the callback and returning + # a value (or raising an exception) is unspecified; with current + # implementations the last event prior to callback resolution wins. + return 42 + def test_immediate_failure(self): with self.assertRaises(ZeroDivisionError): # The caller sees the error just like a normal function. @@ -53,6 +60,13 @@ class ReturnFutureTest(AsyncTestCase): with self.assertRaises(ZeroDivisionError): future.result() + def test_return_value(self): + with self.assertRaises(ReturnValueIgnoredError): + self.return_value(callback=self.stop) + future = self.wait() + with self.assertRaises(ReturnValueIgnoredError): + future.result() + def test_callback_kw(self): future = self.sync_future(callback=self.stop) future2 = self.wait()