Example #1
0
 def _called(self):
     self._active = False
     self._reactor._removeDelayedCall(self)
     try:
         self._func()
     except:
         app_log.error("_called caught exception", exc_info=True)
Example #2
0
 def error_callback(future):
     try:
         future.result()
     except Exception as e:
         if not isinstance(e, quiet_exceptions):
             app_log.error("Exception in Future %r after timeout",
                           future,
                           exc_info=True)
Example #3
0
 def _handle_exception(self, typ, value, tb):
     if self.__failure is None:
         self.__failure = (typ, value, tb)
     else:
         app_log.error("multiple unhandled exceptions in test",
                       exc_info=(typ, value, tb))
     self.stop()
     return True
Example #4
0
        def __del__(self, is_finalizing=is_finalizing):
            if is_finalizing() or not self._log_traceback:
                # set_exception() was not called, or result() or exception()
                # has consumed the exception
                return

            tb = traceback.format_exception(*self._exc_info)

            app_log.error('Future %r exception was never retrieved: %s',
                          self, ''.join(tb).rstrip())
Example #5
0
    def _handle_connection(self, connection, address):
        if self.ssl_options is not None:
            assert ssl, "Python 2.6+ and OpenSSL required for SSL"
            try:
                connection = ssl_wrap_socket(connection,
                                             self.ssl_options,
                                             server_side=True,
                                             do_handshake_on_connect=False)
            except ssl.SSLError as err:
                if err.args[0] == ssl.SSL_ERROR_EOF:
                    return connection.close()
                else:
                    raise
            except socket.error as err:
                # If the connection is closed immediately after it is created
                # (as in a port scan), we can get one of several errors.
                # wrap_socket makes an internal call to getpeername,
                # which may return either EINVAL (Mac OS X) or ENOTCONN
                # (Linux).  If it returns ENOTCONN, this error is
                # silently swallowed by the ssl module, so we need to
                # catch another error later on (AttributeError in
                # SSLIOStream._do_ssl_handshake).
                # To test this behavior, try nmap with the -sT flag.
                # https://github.com/tornadoweb/tornado/pull/750
                if errno_from_exception(err) in (errno.ECONNABORTED,
                                                 errno.EINVAL):
                    return connection.close()
                else:
                    raise
        try:
            if self.ssl_options is not None:
                stream = SSLIOStream(connection,
                                     max_buffer_size=self.max_buffer_size,
                                     read_chunk_size=self.read_chunk_size)
            else:
                stream = IOStream(connection,
                                  max_buffer_size=self.max_buffer_size,
                                  read_chunk_size=self.read_chunk_size)

            future = self.handle_stream(stream, address)
            if future is not None:
                IOLoop.current().add_future(gen.convert_yielded(future),
                                            lambda f: f.result())
        except Exception:
            app_log.error("Error in connection callback", exc_info=True)
Example #6
0
 def get_result(self):
     result_list = []
     exc_info = None
     for f in self.children:
         try:
             result_list.append(f.get_result())
         except Exception as e:
             if exc_info is None:
                 exc_info = sys.exc_info()
             else:
                 if not isinstance(e, self.quiet_exceptions):
                     app_log.error("Multiple exceptions in yield list",
                                   exc_info=True)
     if exc_info is not None:
         raise_exc_info(exc_info)
     if self.keys is not None:
         return dict(zip(self.keys, result_list))
     else:
         return list(result_list)
Example #7
0
 def callback(f):
     unfinished_children.remove(f)
     if not unfinished_children:
         result_list = []
         for f in children:
             try:
                 result_list.append(f.result())
             except Exception as e:
                 if future.done():
                     if not isinstance(e, quiet_exceptions):
                         app_log.error("Multiple exceptions in yield list",
                                       exc_info=True)
                 else:
                     future_set_exc_info(future, sys.exc_info())
         if not future.done():
             if keys is not None:
                 future_set_result_unless_cancelled(
                     future, dict(zip(keys, result_list)))
             else:
                 future_set_result_unless_cancelled(future, result_list)
Example #8
0
    def handle_callback_exception(self, callback):
        """This method is called whenever a callback run by the `IOLoop`
        throws an exception.

        By default simply logs the exception as an error.  Subclasses
        may override this method to customize reporting of exceptions.

        The exception itself is not passed explicitly, but is available
        in `sys.exc_info`.

        .. versionchanged:: 5.0

           When the `asyncio` event loop is used (which is now the
           default on Python 3), some callback errors will be handled by
           `asyncio` instead of this method.

        .. deprecated: 5.1

           Support for this method will be removed in Tornado 6.0.
        """
        app_log.error("Exception in callback %r", callback, exc_info=True)
Example #9
0
    def __init__(self,
                 template_string,
                 name="<string>",
                 loader=None,
                 compress_whitespace=_UNSET,
                 autoescape=_UNSET,
                 whitespace=None):
        """Construct a Template.

        :arg str template_string: the contents of the template file.
        :arg str name: the filename from which the template was loaded
            (used for error message).
        :arg tornado.template.BaseLoader loader: the `~tornado.template.BaseLoader` responsible
            for this template, used to resolve ``{% include %}`` and ``{% extend %}`` directives.
        :arg bool compress_whitespace: Deprecated since Tornado 4.3.
            Equivalent to ``whitespace="single"`` if true and
            ``whitespace="all"`` if false.
        :arg str autoescape: The name of a function in the template
            namespace, or ``None`` to disable escaping by default.
        :arg str whitespace: A string specifying treatment of whitespace;
            see `filter_whitespace` for options.

        .. versionchanged:: 4.3
           Added ``whitespace`` parameter; deprecated ``compress_whitespace``.
        """
        self.name = escape.native_str(name)

        if compress_whitespace is not _UNSET:
            # Convert deprecated compress_whitespace (bool) to whitespace (str).
            if whitespace is not None:
                raise Exception(
                    "cannot set both whitespace and compress_whitespace")
            whitespace = "single" if compress_whitespace else "all"
        if whitespace is None:
            if loader and loader.whitespace:
                whitespace = loader.whitespace
            else:
                # Whitespace defaults by filename.
                if name.endswith(".html") or name.endswith(".js"):
                    whitespace = "single"
                else:
                    whitespace = "all"
        # Validate the whitespace setting.
        filter_whitespace(whitespace, '')

        if autoescape is not _UNSET:
            self.autoescape = autoescape
        elif loader:
            self.autoescape = loader.autoescape
        else:
            self.autoescape = _DEFAULT_AUTOESCAPE

        self.namespace = loader.namespace if loader else {}
        reader = _TemplateReader(name, escape.native_str(template_string),
                                 whitespace)
        self.file = _File(self, _parse(reader, self))
        self.code = self._generate_python(loader)
        self.loader = loader
        try:
            # Under python2.5, the fake filename used here must match
            # the module name used in __name__ below.
            # The dont_inherit flag prevents template.py's future imports
            # from being applied to the generated code.
            self.compiled = compile(escape.to_unicode(self.code),
                                    "%s.generated.py" %
                                    self.name.replace('.', '_'),
                                    "exec",
                                    dont_inherit=True)
        except Exception:
            formatted_code = _format_code(self.code).rstrip()
            app_log.error("%s code:\n%s", self.name, formatted_code)
            raise
Example #10
0
 def __del__(self, is_finalizing=is_finalizing):
     if not is_finalizing() and self.formatted_tb:
         app_log.error('Future exception was never retrieved: %s',
                       ''.join(self.formatted_tb).rstrip())