]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Replace a couple of contextmanager functions with ExceptionStackContext.
authorBen Darnell <ben@bendarnell.com>
Sun, 9 Dec 2012 00:34:16 +0000 (19:34 -0500)
committerBen Darnell <ben@bendarnell.com>
Sun, 9 Dec 2012 00:34:16 +0000 (19:34 -0500)
tornado/simple_httpclient.py
tornado/testing.py

index 2c759a6dab6b1fa48db8e4dbc5f4ee13b4e24f81..7000d987e73da5b96b9c484bbaed483586dde2f8 100644 (file)
@@ -12,7 +12,6 @@ from tornado.util import b, GzipDecompressor
 
 import base64
 import collections
-import contextlib
 import copy
 import functools
 import os.path
@@ -134,7 +133,7 @@ class _HTTPConnection(object):
         self._decompressor = None
         # Timeout handle returned by IOLoop.add_timeout
         self._timeout = None
-        with stack_context.StackContext(self.cleanup):
+        with stack_context.ExceptionStackContext(self._handle_exception):
             self.parsed = urlparse.urlsplit(_unicode(self.request.url))
             if ssl is None and self.parsed.scheme == "https":
                 raise ValueError("HTTPS requires either python2.6+ or "
@@ -311,25 +310,22 @@ class _HTTPConnection(object):
             self.final_callback = None
             self.io_loop.add_callback(final_callback, response)
 
-    @contextlib.contextmanager
-    def cleanup(self):
-        try:
-            yield
-        except Exception, e:
-            if self.final_callback:
-                gen_log.warning("uncaught exception", exc_info=True)
-                self._run_callback(HTTPResponse(self.request, 599, error=e,
-                                    request_time=self.io_loop.time() - self.start_time,
-                                    ))
-
-                if hasattr(self, "stream"):
-                    self.stream.close()
-            else:
-                # If our callback has already been called, we are probably
-                # catching an exception that is not caused by us but rather
-                # some child of our callback. Rather than drop it on the floor,
-                # pass it along.
-                raise
+    def _handle_exception(self, typ, value, tb):
+        if self.final_callback:
+            gen_log.warning("uncaught exception", exc_info=(typ, value, tb))
+            self._run_callback(HTTPResponse(self.request, 599, error=value,
+                                request_time=self.io_loop.time() - self.start_time,
+                                ))
+
+            if hasattr(self, "stream"):
+                self.stream.close()
+            return True
+        else:
+            # If our callback has already been called, we are probably
+            # catching an exception that is not caused by us but rather
+            # some child of our callback. Rather than drop it on the floor,
+            # pass it along.
+            return False
 
     def _on_close(self):
         if self.final_callback is not None:
index 5945643366f7c8f7f844ec284ea774b435187846..22376627e58903fb958c777547b008d978ecd58f 100644 (file)
@@ -36,9 +36,8 @@ except ImportError:
     netutil = None
     SimpleAsyncHTTPClient = None
 from tornado.log import gen_log
-from tornado.stack_context import StackContext
+from tornado.stack_context import ExceptionStackContext
 from tornado.util import raise_exc_info
-import contextlib
 import logging
 import os
 import re
@@ -167,13 +166,10 @@ class AsyncTestCase(unittest.TestCase):
         '''
         return IOLoop()
 
-    @contextlib.contextmanager
-    def _stack_context(self):
-        try:
-            yield
-        except Exception:
-            self.__failure = sys.exc_info()
-            self.stop()
+    def _handle_exception(self, typ, value, tb):
+        self.__failure = sys.exc_info()
+        self.stop()
+        return True
 
     def __rethrow(self):
         if self.__failure is not None:
@@ -182,7 +178,7 @@ class AsyncTestCase(unittest.TestCase):
             raise_exc_info(failure)
 
     def run(self, result=None):
-        with StackContext(self._stack_context):
+        with ExceptionStackContext(self._handle_exception):
             super(AsyncTestCase, self).run(result)
         # In case an exception escaped super.run or the StackContext caught
         # an exception when there wasn't a wait() to re-raise it, do so here.