]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
While I'm touching every file, run autopep8 too.
authorBen Darnell <ben@bendarnell.com>
Thu, 9 Feb 2012 08:55:27 +0000 (00:55 -0800)
committerBen Darnell <ben@bendarnell.com>
Thu, 9 Feb 2012 08:55:27 +0000 (00:55 -0800)
Might as well get all the merge headaches over with at once :)

Ran with
$ autopep8 --ignore=E111,W602 i tornado/*.py tornado/platform/*.py tornado/test/*.py

47 files changed:
tornado/auth.py
tornado/autoreload.py
tornado/curl_httpclient.py
tornado/database.py
tornado/escape.py
tornado/gen.py
tornado/httpclient.py
tornado/httpserver.py
tornado/httputil.py
tornado/ioloop.py
tornado/iostream.py
tornado/locale.py
tornado/netutil.py
tornado/options.py
tornado/platform/interface.py
tornado/platform/posix.py
tornado/platform/twisted.py
tornado/platform/windows.py
tornado/process.py
tornado/simple_httpclient.py
tornado/stack_context.py
tornado/template.py
tornado/test/auth_test.py
tornado/test/curl_httpclient_test.py
tornado/test/escape_test.py
tornado/test/gen_test.py
tornado/test/httpclient_test.py
tornado/test/httpserver_test.py
tornado/test/httputil_test.py
tornado/test/import_test.py
tornado/test/ioloop_test.py
tornado/test/iostream_test.py
tornado/test/process_test.py
tornado/test/run_pyversion_tests.py
tornado/test/runtests.py
tornado/test/simple_httpclient_test.py
tornado/test/stack_context_test.py
tornado/test/template_test.py
tornado/test/testing_test.py
tornado/test/twisted_test.py
tornado/test/web_test.py
tornado/test/wsgi_test.py
tornado/testing.py
tornado/util.py
tornado/web.py
tornado/websocket.py
tornado/wsgi.py

index e2068549694ba442cc9881efd99a891ed71ac3b5..65be566bd965eafb241dd1a685064b7f68a078c3 100644 (file)
@@ -61,13 +61,14 @@ from tornado import escape
 from tornado.httputil import url_concat
 from tornado.util import bytes_type, b
 
+
 class OpenIdMixin(object):
     """Abstract implementation of OpenID and Attribute Exchange.
 
     See GoogleMixin below for example implementations.
     """
     def authenticate_redirect(self, callback_uri=None,
-                              ax_attrs=["name","email","language","username"]):
+                              ax_attrs=["name", "email", "language", "username"]):
         """Returns the authentication URL for this service.
 
         After authentication, the service will redirect back to the given
@@ -93,7 +94,8 @@ class OpenIdMixin(object):
         args = dict((k, v[-1]) for k, v in self.request.arguments.iteritems())
         args["openid.mode"] = u"check_authentication"
         url = self._OPENID_ENDPOINT
-        if http_client is None: http_client = httpclient.AsyncHTTPClient()
+        if http_client is None:
+            http_client = httpclient.AsyncHTTPClient()
         http_client.fetch(url, self.async_callback(
             self._on_authentication_verified, callback),
             method="POST", body=urllib.urlencode(args))
@@ -160,8 +162,10 @@ class OpenIdMixin(object):
                self.get_argument(name) == u"http://openid.net/srv/ax/1.0":
                 ax_ns = name[10:]
                 break
+
         def get_ax_arg(uri):
-            if not ax_ns: return u""
+            if not ax_ns:
+                return u""
             prefix = "openid." + ax_ns + ".type."
             ax_name = None
             for name in self.request.arguments.iterkeys():
@@ -169,7 +173,8 @@ class OpenIdMixin(object):
                     part = name[len(prefix):]
                     ax_name = "openid." + ax_ns + ".value." + part
                     break
-            if not ax_name: return u""
+            if not ax_name:
+                return u""
             return self.get_argument(ax_name, u"")
 
         email = get_ax_arg("http://axschema.org/contact/email")
@@ -192,9 +197,12 @@ class OpenIdMixin(object):
             user["name"] = u" ".join(name_parts)
         elif email:
             user["name"] = email.split("@")[0]
-        if email: user["email"] = email
-        if locale: user["locale"] = locale
-        if username: user["username"] = username
+        if email:
+            user["email"] = email
+        if locale:
+            user["locale"] = locale
+        if username:
+            user["username"] = username
         callback(user)
 
 
@@ -237,7 +245,6 @@ class OAuthMixin(object):
                     self._on_request_token, self._OAUTH_AUTHORIZE_URL,
                     callback_uri))
 
-
     def get_authenticated_user(self, callback, http_client=None):
         """Gets the OAuth authorized user and access token on callback.
 
@@ -271,7 +278,7 @@ class OAuthMixin(object):
         http_client.fetch(self._oauth_access_token_url(token),
                           self.async_callback(self._on_access_token, callback))
 
-    def _oauth_request_token_url(self, callback_uri= None, extra_params=None):
+    def _oauth_request_token_url(self, callback_uri=None, extra_params=None):
         consumer_token = self._oauth_consumer_token()
         url = self._OAUTH_REQUEST_TOKEN_URL
         args = dict(
@@ -285,7 +292,8 @@ class OAuthMixin(object):
             if callback_uri:
                 args["oauth_callback"] = urlparse.urljoin(
                     self.request.full_url(), callback_uri)
-            if extra_params: args.update(extra_params)
+            if extra_params:
+                args.update(extra_params)
             signature = _oauth10a_signature(consumer_token, "GET", url, args)
         else:
             signature = _oauth_signature(consumer_token, "GET", url, args)
@@ -318,7 +326,7 @@ class OAuthMixin(object):
             oauth_version=getattr(self, "_OAUTH_VERSION", "1.0a"),
         )
         if "verifier" in request_token:
-          args["oauth_verifier"]=request_token["verifier"]
+          args["oauth_verifier"] = request_token["verifier"]
 
         if getattr(self, "_OAUTH_VERSION", "1.0a") == "1.0a":
             signature = _oauth10a_signature(consumer_token, "GET", url, args,
@@ -378,11 +386,12 @@ class OAuthMixin(object):
         base_args["oauth_signature"] = signature
         return base_args
 
+
 class OAuth2Mixin(object):
     """Abstract implementation of OAuth v 2."""
 
     def authorize_redirect(self, redirect_uri=None, client_id=None,
-                           client_secret=None, extra_params=None ):
+                           client_secret=None, extra_params=None):
         """Redirects the user to obtain OAuth authorization for this service.
 
         Some providers require that you register a Callback
@@ -395,11 +404,12 @@ class OAuth2Mixin(object):
           "redirect_uri": redirect_uri,
           "client_id": client_id
         }
-        if extra_params: args.update(extra_params)
+        if extra_params:
+            args.update(extra_params)
         self.redirect(
                 url_concat(self._OAUTH_AUTHORIZE_URL, args))
 
-    def _oauth_request_token_url(self, redirect_uri= None, client_id = None,
+    def _oauth_request_token_url(self, redirect_uri=None, client_id=None,
                                  client_secret=None, code=None,
                                  extra_params=None):
         url = self._OAUTH_ACCESS_TOKEN_URL
@@ -409,9 +419,11 @@ class OAuth2Mixin(object):
             client_id=client_id,
             client_secret=client_secret,
             )
-        if extra_params: args.update(extra_params)
+        if extra_params:
+            args.update(extra_params)
         return url_concat(url, args)
 
+
 class TwitterMixin(OAuthMixin):
     """Twitter OAuth authentication.
 
@@ -452,15 +464,14 @@ class TwitterMixin(OAuthMixin):
     _OAUTH_AUTHENTICATE_URL = "http://api.twitter.com/oauth/authenticate"
     _OAUTH_NO_CALLBACKS = False
 
-
-    def authenticate_redirect(self, callback_uri = None):
+    def authenticate_redirect(self, callback_uri=None):
         """Just like authorize_redirect(), but auto-redirects if authorized.
 
         This is generally the right interface to use if you are using
         Twitter for single-sign on.
         """
         http = httpclient.AsyncHTTPClient()
-        http.fetch(self._oauth_request_token_url(callback_uri = callback_uri), self.async_callback(
+        http.fetch(self._oauth_request_token_url(callback_uri=callback_uri), self.async_callback(
             self._on_request_token, self._OAUTH_AUTHENTICATE_URL, None))
 
     def twitter_request(self, path, callback, access_token=None,
@@ -516,7 +527,8 @@ class TwitterMixin(OAuthMixin):
             oauth = self._oauth_request_parameters(
                 url, access_token, all_args, method=method)
             args.update(oauth)
-        if args: url += "?" + urllib.urlencode(args)
+        if args:
+            url += "?" + urllib.urlencode(args)
         callback = self.async_callback(self._on_twitter_request, callback)
         http = httpclient.AsyncHTTPClient()
         if post_args is not None:
@@ -592,7 +604,6 @@ class FriendFeedMixin(OAuthMixin):
     _OAUTH_NO_CALLBACKS = True
     _OAUTH_VERSION = "1.0"
 
-
     def friendfeed_request(self, path, callback, access_token=None,
                            post_args=None, **args):
         """Fetches the given relative API path, e.g., "/bret/friends"
@@ -638,7 +649,8 @@ class FriendFeedMixin(OAuthMixin):
             oauth = self._oauth_request_parameters(
                 url, access_token, all_args, method=method)
             args.update(oauth)
-        if args: url += "?" + urllib.urlencode(args)
+        if args:
+            url += "?" + urllib.urlencode(args)
         callback = self.async_callback(self._on_friendfeed_request, callback)
         http = httpclient.AsyncHTTPClient()
         if post_args is not None:
@@ -703,7 +715,7 @@ class GoogleMixin(OpenIdMixin, OAuthMixin):
     _OAUTH_ACCESS_TOKEN_URL = "https://www.google.com/accounts/OAuthGetAccessToken"
 
     def authorize_redirect(self, oauth_scope, callback_uri=None,
-                           ax_attrs=["name","email","language","username"]):
+                           ax_attrs=["name", "email", "language", "username"]):
         """Authenticates and authorizes for the given Google resource.
 
         Some of the available resources are:
@@ -748,6 +760,7 @@ class GoogleMixin(OpenIdMixin, OAuthMixin):
     def _oauth_get_user(self, access_token, callback):
         OpenIdMixin.get_authenticated_user(self, callback)
 
+
 class FacebookMixin(object):
     """Facebook Connect authentication.
 
@@ -928,9 +941,11 @@ class FacebookMixin(object):
     def _signature(self, args):
         parts = ["%s=%s" % (n, args[n]) for n in sorted(args.keys())]
         body = "".join(parts) + self.settings["facebook_secret"]
-        if isinstance(body, unicode): body = body.encode("utf-8")
+        if isinstance(body, unicode):
+            body = body.encode("utf-8")
         return hashlib.md5(body).hexdigest()
 
+
 class FacebookGraphMixin(OAuth2Mixin):
     """Facebook authentication using the new Graph API and OAuth2."""
     _OAUTH_ACCESS_TOKEN_URL = "https://graph.facebook.com/oauth/access_token?"
@@ -974,7 +989,8 @@ class FacebookGraphMixin(OAuth2Mixin):
 
       fields = set(['id', 'name', 'first_name', 'last_name',
                     'locale', 'picture', 'link'])
-      if extra_fields: fields.update(extra_fields)
+      if extra_fields:
+            fields.update(extra_fields)
 
       http.fetch(self._oauth_request_token_url(**args),
           self.async_callback(self._on_access_token, redirect_uri, client_id,
@@ -1001,7 +1017,6 @@ class FacebookGraphMixin(OAuth2Mixin):
           fields=",".join(fields)
           )
 
-
     def _on_get_user_info(self, callback, session, fields, user):
         if user is None:
             callback(None)
@@ -1055,7 +1070,8 @@ class FacebookGraphMixin(OAuth2Mixin):
             all_args["access_token"] = access_token
             all_args.update(args)
             all_args.update(post_args or {})
-        if all_args: url += "?" + urllib.urlencode(all_args)
+        if all_args:
+            url += "?" + urllib.urlencode(all_args)
         callback = self.async_callback(self._on_facebook_request, callback)
         http = httpclient.AsyncHTTPClient()
         if post_args is not None:
@@ -1072,6 +1088,7 @@ class FacebookGraphMixin(OAuth2Mixin):
             return
         callback(escape.json_decode(response.body))
 
+
 def _oauth_signature(consumer_token, method, url, parameters={}, token=None):
     """Calculates the HMAC-SHA1 OAuth signature for the given request.
 
@@ -1086,7 +1103,7 @@ def _oauth_signature(consumer_token, method, url, parameters={}, token=None):
     base_elems.append(normalized_url)
     base_elems.append("&".join("%s=%s" % (k, _oauth_escape(str(v)))
                                for k, v in sorted(parameters.items())))
-    base_string =  "&".join(_oauth_escape(e) for e in base_elems)
+    base_string = "&".join(_oauth_escape(e) for e in base_elems)
 
     key_elems = [escape.utf8(consumer_token["secret"])]
     key_elems.append(escape.utf8(token["secret"] if token else ""))
@@ -1095,6 +1112,7 @@ def _oauth_signature(consumer_token, method, url, parameters={}, token=None):
     hash = hmac.new(key, escape.utf8(base_string), hashlib.sha1)
     return binascii.b2a_base64(hash.digest())[:-1]
 
+
 def _oauth10a_signature(consumer_token, method, url, parameters={}, token=None):
     """Calculates the HMAC-SHA1 OAuth 1.0a signature for the given request.
 
@@ -1110,7 +1128,7 @@ def _oauth10a_signature(consumer_token, method, url, parameters={}, token=None):
     base_elems.append("&".join("%s=%s" % (k, _oauth_escape(str(v)))
                                for k, v in sorted(parameters.items())))
 
-    base_string =  "&".join(_oauth_escape(e) for e in base_elems)
+    base_string = "&".join(_oauth_escape(e) for e in base_elems)
     key_elems = [escape.utf8(urllib.quote(consumer_token["secret"], safe='~'))]
     key_elems.append(escape.utf8(urllib.quote(token["secret"], safe='~') if token else ""))
     key = b("&").join(key_elems)
@@ -1118,6 +1136,7 @@ def _oauth10a_signature(consumer_token, method, url, parameters={}, token=None):
     hash = hmac.new(key, escape.utf8(base_string), hashlib.sha1)
     return binascii.b2a_base64(hash.digest())[:-1]
 
+
 def _oauth_escape(val):
     if isinstance(val, unicode):
         val = val.encode("utf-8")
@@ -1132,5 +1151,3 @@ def _oauth_parse_response(body):
     special = (b("oauth_token"), b("oauth_token_secret"))
     token.update((k, p[k][0]) for k in p if k not in special)
     return token
-
-
index 2464f34683931e365bbca2a373dca51f6c7ad877..7da8df8880a84ececf31efb6b742ca2b8888bc25 100644 (file)
@@ -44,6 +44,7 @@ try:
 except ImportError:
     signal = None
 
+
 def start(io_loop=None, check_time=500):
     """Restarts the process automatically when a module is modified.
 
@@ -57,6 +58,7 @@ def start(io_loop=None, check_time=500):
     scheduler = ioloop.PeriodicCallback(callback, check_time, io_loop=io_loop)
     scheduler.start()
 
+
 def wait():
     """Wait for a watched file to change, then restart the process.
 
@@ -70,6 +72,7 @@ def wait():
 
 _watched_files = set()
 
+
 def watch(filename):
     """Add a file to the watch list.
 
@@ -79,6 +82,7 @@ def watch(filename):
 
 _reload_hooks = []
 
+
 def add_reload_hook(fn):
     """Add a function to be called before reloading the process.
 
@@ -89,6 +93,7 @@ def add_reload_hook(fn):
     """
     _reload_hooks.append(fn)
 
+
 def _close_all_fds(io_loop):
     for fd in io_loop._handlers.keys():
         try:
@@ -98,6 +103,7 @@ def _close_all_fds(io_loop):
 
 _reload_attempted = False
 
+
 def _reload_on_update(modify_times):
     if _reload_attempted:
         # We already tried to reload and it didn't work, so don't try again.
@@ -112,15 +118,18 @@ def _reload_on_update(modify_times):
         # in the standard library), and occasionally this can cause strange
         # failures in getattr.  Just ignore anything that's not an ordinary
         # module.
-        if not isinstance(module, types.ModuleType): continue
+        if not isinstance(module, types.ModuleType):
+            continue
         path = getattr(module, "__file__", None)
-        if not path: continue
+        if not path:
+            continue
         if path.endswith(".pyc") or path.endswith(".pyo"):
             path = path[:-1]
         _check_file(modify_times, path)
     for path in _watched_files:
         _check_file(modify_times, path)
 
+
 def _check_file(modify_times, path):
     try:
         modified = os.stat(path).st_mtime
@@ -133,6 +142,7 @@ def _check_file(modify_times, path):
         logging.info("%s modified; restarting server", path)
         _reload()
 
+
 def _reload():
     global _reload_attempted
     _reload_attempted = True
@@ -173,9 +183,11 @@ Usage:
   python -m tornado.autoreload -m module.to.run [args...]
   python -m tornado.autoreload path/to/script.py [args...]
 """
+
+
 def main():
     """Command-line wrapper to re-run a script whenever its source changes.
-    
+
     Scripts may be specified by filename or module name::
 
         python -m tornado.autoreload -m tornado.test.runtests
@@ -229,7 +241,7 @@ def main():
         watch(pkgutil.get_loader(module).get_filename())
 
     wait()
-    
+
 
 if __name__ == "__main__":
     # If this module is run with "python -m tornado.autoreload", the current
index c6213c05d24f90bbfd0f33b22a8154d0cfdd6995..c3903e9bf7f6ae16f41494ffd0ddae1cc983e495 100644 (file)
@@ -32,6 +32,7 @@ from tornado import stack_context
 from tornado.escape import utf8
 from tornado.httpclient import HTTPRequest, HTTPResponse, HTTPError, AsyncHTTPClient, main
 
+
 class CurlAsyncHTTPClient(AsyncHTTPClient):
     def initialize(self, io_loop=None, max_clients=10,
                    max_simultaneous_connections=None):
@@ -109,15 +110,17 @@ class CurlAsyncHTTPClient(AsyncHTTPClient):
         if self._timeout is not None:
             self.io_loop.remove_timeout(self._timeout)
         self._timeout = self.io_loop.add_timeout(
-            time.time() + msecs/1000.0, self._handle_timeout)
+            time.time() + msecs / 1000.0, self._handle_timeout)
 
     def _handle_events(self, fd, events):
         """Called by IOLoop when there is activity on one of our
         file descriptors.
         """
         action = 0
-        if events & ioloop.IOLoop.READ: action |= pycurl.CSELECT_IN
-        if events & ioloop.IOLoop.WRITE: action |= pycurl.CSELECT_OUT
+        if events & ioloop.IOLoop.READ:
+            action |= pycurl.CSELECT_IN
+        if events & ioloop.IOLoop.WRITE:
+            action |= pycurl.CSELECT_OUT
         while True:
             try:
                 ret, num_handles = self._socket_action(fd, action)
@@ -250,7 +253,6 @@ class CurlAsyncHTTPClient(AsyncHTTPClient):
         except Exception:
             self.handle_callback_exception(info["callback"])
 
-
     def handle_callback_exception(self, callback):
         self.io_loop.handle_callback_exception(callback)
 
@@ -372,7 +374,7 @@ def _curl_setup_request(curl, request, buffer, headers):
 
     # Handle curl's cryptic options for every individual HTTP method
     if request.method in ("POST", "PUT"):
-        request_buffer =  cStringIO.StringIO(utf8(request.body))
+        request_buffer = cStringIO.StringIO(utf8(request.body))
         curl.setopt(pycurl.READFUNCTION, request_buffer.read)
         if request.method == "POST":
             def ioctl(cmd):
@@ -420,6 +422,7 @@ def _curl_header_callback(headers, header_line):
         return
     headers.parse_line(header_line)
 
+
 def _curl_debug(debug_type, debug_msg):
     debug_types = ('I', '<', '>', '<', '>')
     if debug_type == 0:
index a74879b03dd34c613debf4ac0bd22cf1d7da28b9..e0abb9de55e0bd0f3b142e822d886a2a6fa851c0 100644 (file)
@@ -26,6 +26,7 @@ import itertools
 import logging
 import time
 
+
 class Connection(object):
     """A lightweight wrapper around MySQLdb DB-API connections.
 
@@ -43,7 +44,7 @@ class Connection(object):
     UTF-8 on all connections to avoid time zone and encoding errors.
     """
     def __init__(self, host, database, user=None, password=None,
-                 max_idle_time=7*3600):
+                 max_idle_time=7 * 3600):
         self.host = host
         self.database = database
         self.max_idle_time = max_idle_time
index 2136e64999923ba80d9a585b9ecc4d1390437037..4222864988f9fcbb1f353a3b8f3ba7dcd5536f42 100644 (file)
@@ -28,8 +28,10 @@ import sys
 import urllib
 
 # Python3 compatibility:  On python2.5, introduce the bytes alias from 2.6
-try: bytes
-except Exception: bytes = str
+try:
+    bytes
+except Exception:
+    bytes = str
 
 try:
     from urlparse import parse_qs  # Python 2.6+
@@ -64,6 +66,8 @@ except Exception:
 
 _XHTML_ESCAPE_RE = re.compile('[&<>"]')
 _XHTML_ESCAPE_DICT = {'&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;'}
+
+
 def xhtml_escape(value):
     """Escapes a string so it is valid within XML or XHTML."""
     return _XHTML_ESCAPE_RE.sub(lambda match: _XHTML_ESCAPE_DICT[match.group(0)],
@@ -145,13 +149,14 @@ else:
         result = parse_qs(qs, keep_blank_values, strict_parsing,
                           encoding='latin1', errors='strict')
         encoded = {}
-        for k,v in result.iteritems():
+        for k, v in result.iteritems():
             encoded[k] = [i.encode('latin1') for i in v]
         return encoded
-        
 
 
 _UTF8_TYPES = (bytes, type(None))
+
+
 def utf8(value):
     """Converts a string argument to a byte string.
 
@@ -164,6 +169,8 @@ def utf8(value):
     return value.encode("utf-8")
 
 _TO_UNICODE_TYPES = (unicode, type(None))
+
+
 def to_unicode(value):
     """Converts a string argument to a unicode string.
 
@@ -187,6 +194,8 @@ else:
     native_str = utf8
 
 _BASESTRING_TYPES = (basestring, type(None))
+
+
 def to_basestring(value):
     """Converts a string argument to a subclass of basestring.
 
@@ -201,13 +210,14 @@ def to_basestring(value):
     assert isinstance(value, bytes)
     return value.decode("utf-8")
 
+
 def recursive_unicode(obj):
     """Walks a simple data structure, converting byte strings to unicode.
 
     Supports lists, tuples, and dictionaries.
     """
     if isinstance(obj, dict):
-        return dict((recursive_unicode(k), recursive_unicode(v)) for (k,v) in obj.iteritems())
+        return dict((recursive_unicode(k), recursive_unicode(v)) for (k, v) in obj.iteritems())
     elif isinstance(obj, list):
         return list(recursive_unicode(i) for i in obj)
     elif isinstance(obj, tuple):
@@ -217,7 +227,7 @@ def recursive_unicode(obj):
     else:
         return obj
 
-# I originally used the regex from 
+# I originally used the regex from
 # http://daringfireball.net/2010/07/improved_regex_for_matching_urls
 # but it gets all exponential on certain patterns (such as too many trailing
 # dots), causing the regex matcher to never return.
index 04bc4039e16deb13be9780b4727bdeb16e987d04..752c3f24913ebb4d7ab48b313e03ec1db6dd9ca8 100644 (file)
@@ -71,10 +71,22 @@ import types
 
 from tornado.stack_context import ExceptionStackContext
 
-class KeyReuseError(Exception): pass
-class UnknownKeyError(Exception): pass
-class LeakedCallbackError(Exception): pass
-class BadYieldError(Exception): pass
+
+class KeyReuseError(Exception):
+    pass
+
+
+class UnknownKeyError(Exception):
+    pass
+
+
+class LeakedCallbackError(Exception):
+    pass
+
+
+class BadYieldError(Exception):
+    pass
+
 
 def engine(func):
     """Decorator for asynchronous generators.
@@ -92,6 +104,7 @@ def engine(func):
     @functools.wraps(func)
     def wrapper(*args, **kwargs):
         runner = None
+
         def handle_exception(typ, value, tb):
             # if the function throws an exception before its first "yield"
             # (or is not a generator at all), the Runner won't exist yet.
@@ -110,11 +123,12 @@ def engine(func):
             # no yield, so we're done
     return wrapper
 
+
 class YieldPoint(object):
     """Base class for objects that may be yielded from the generator."""
     def start(self, runner):
         """Called by the runner after the generator has yielded.
-        
+
         No other methods will be called on this object before ``start``.
         """
         raise NotImplementedError()
@@ -128,12 +142,13 @@ class YieldPoint(object):
 
     def get_result(self):
         """Returns the value to use as the result of the yield expression.
-        
+
         This method will only be called once, and only after `is_ready`
         has returned true.
         """
         raise NotImplementedError()
 
+
 class Callback(YieldPoint):
     """Returns a callable object that will allow a matching `Wait` to proceed.
 
@@ -159,6 +174,7 @@ class Callback(YieldPoint):
     def get_result(self):
         return self.runner.result_callback(self.key)
 
+
 class Wait(YieldPoint):
     """Returns the argument passed to the result of a previous `Callback`."""
     def __init__(self, key):
@@ -173,6 +189,7 @@ class Wait(YieldPoint):
     def get_result(self):
         return self.runner.pop_result(self.key)
 
+
 class WaitAll(YieldPoint):
     """Returns the results of multiple previous `Callbacks`.
 
@@ -189,10 +206,10 @@ class WaitAll(YieldPoint):
 
     def is_ready(self):
         return all(self.runner.is_ready(key) for key in self.keys)
-        
+
     def get_result(self):
         return [self.runner.pop_result(key) for key in self.keys]
-            
+
 
 class Task(YieldPoint):
     """Runs a single asynchronous operation.
@@ -203,9 +220,9 @@ class Task(YieldPoint):
 
     A `Task` is equivalent to a `Callback`/`Wait` pair (with a unique
     key generated automatically)::
-    
+
         result = yield gen.Task(func, args)
-        
+
         func(args, callback=(yield gen.Callback(key)))
         result = yield gen.Wait(key)
     """
@@ -221,13 +238,14 @@ class Task(YieldPoint):
         runner.register_callback(self.key)
         self.kwargs["callback"] = runner.result_callback(self.key)
         self.func(*self.args, **self.kwargs)
-    
+
     def is_ready(self):
         return self.runner.is_ready(self.key)
 
     def get_result(self):
         return self.runner.pop_result(self.key)
 
+
 class Multi(YieldPoint):
     """Runs multiple asynchronous operations in parallel.
 
@@ -239,7 +257,7 @@ class Multi(YieldPoint):
     def __init__(self, children):
         assert all(isinstance(i, YieldPoint) for i in children)
         self.children = children
-    
+
     def start(self, runner):
         for i in self.children:
             i.start(runner)
@@ -250,14 +268,18 @@ class Multi(YieldPoint):
     def get_result(self):
         return [i.get_result() for i in self.children]
 
+
 class _NullYieldPoint(YieldPoint):
     def start(self, runner):
         pass
+
     def is_ready(self):
         return True
+
     def get_result(self):
         return None
 
+
 class Runner(object):
     """Internal implementation of `tornado.gen.engine`.
 
@@ -366,6 +388,8 @@ class Runner(object):
             return False
 
 # in python 2.6+ this could be a collections.namedtuple
+
+
 class Arguments(tuple):
     """The result of a yield expression whose callback had more than one
     argument (or keyword arguments).
index 345b9b324009a73b1d0fcc9f526353aa8155d1f3..27edf15ef3840dca825518a95bcf83523b79e59e 100644 (file)
@@ -42,6 +42,7 @@ from tornado import httputil
 from tornado.ioloop import IOLoop
 from tornado.util import import_object, bytes_type
 
+
 class HTTPClient(object):
     """A blocking HTTP client.
 
@@ -76,7 +77,7 @@ class HTTPClient(object):
 
     def fetch(self, request, **kwargs):
         """Executes a request, returning an `HTTPResponse`.
-        
+
         The request may be either a string URL or an `HTTPRequest` object.
         If it is a string, we construct an `HTTPRequest` using any additional
         kwargs: ``HTTPRequest(request, **kwargs)``
@@ -93,6 +94,7 @@ class HTTPClient(object):
         response.rethrow()
         return response
 
+
 class AsyncHTTPClient(object):
     """An non-blocking HTTP client.
 
@@ -129,7 +131,7 @@ class AsyncHTTPClient(object):
             cls._async_client_dict = weakref.WeakKeyDictionary()
         return cls._async_client_dict
 
-    def __new__(cls, io_loop=None, max_clients=10, force_instance=False, 
+    def __new__(cls, io_loop=None, max_clients=10, force_instance=False,
                 **kwargs):
         io_loop = io_loop or IOLoop.instance()
         if cls is AsyncHTTPClient:
@@ -202,6 +204,7 @@ class AsyncHTTPClient(object):
         AsyncHTTPClient._impl_class = impl
         AsyncHTTPClient._impl_kwargs = kwargs
 
+
 class HTTPRequest(object):
     """HTTP client request object."""
     def __init__(self, url, method="GET", headers=None, body=None,
@@ -237,23 +240,23 @@ class HTTPRequest(object):
         :arg bool use_gzip: Request gzip encoding from the server
         :arg string network_interface: Network interface to use for request
         :arg callable streaming_callback: If set, `streaming_callback` will
-           be run with each chunk of data as it is received, and 
-           `~HTTPResponse.body` and `~HTTPResponse.buffer` will be empty in 
+           be run with each chunk of data as it is received, and
+           `~HTTPResponse.body` and `~HTTPResponse.buffer` will be empty in
            the final response.
         :arg callable header_callback: If set, `header_callback` will
-           be run with each header line as it is received, and 
+           be run with each header line as it is received, and
            `~HTTPResponse.headers` will be empty in the final response.
         :arg callable prepare_curl_callback: If set, will be called with
            a `pycurl.Curl` object to allow the application to make additional
            `setopt` calls.
-        :arg string proxy_host: HTTP proxy hostname.  To use proxies, 
-           `proxy_host` and `proxy_port` must be set; `proxy_username` and 
-           `proxy_pass` are optional.  Proxies are currently only support 
+        :arg string proxy_host: HTTP proxy hostname.  To use proxies,
+           `proxy_host` and `proxy_port` must be set; `proxy_username` and
+           `proxy_pass` are optional.  Proxies are currently only support
            with `curl_httpclient`.
         :arg int proxy_port: HTTP proxy port
         :arg string proxy_username: HTTP proxy username
         :arg string proxy_password: HTTP proxy password
-        :arg bool allow_nonstandard_methods: Allow unknown values for `method` 
+        :arg bool allow_nonstandard_methods: Allow unknown values for `method`
            argument?
         :arg bool validate_cert: For HTTPS requests, validate the server's
            certificate?
@@ -262,7 +265,7 @@ class HTTPRequest(object):
            any request uses a custom `ca_certs` file, they all must (they
            don't have to all use the same `ca_certs`, but it's not possible
            to mix requests with ca_certs and requests that use the defaults.
-        :arg bool allow_ipv6: Use IPv6 when available?  Default is false in 
+        :arg bool allow_ipv6: Use IPv6 when available?  Default is false in
            `simple_httpclient` and true in `curl_httpclient`
         :arg string client_key: Filename for client SSL key, if any
         :arg string client_cert: Filename for client SSL certificate, if any
index eabb5debe44ce6f64226636cb44db6caa3adcbf7..2f70f23382c667fc0b2bfa7bef80ce8393874763 100644 (file)
@@ -40,10 +40,11 @@ from tornado import stack_context
 from tornado.util import b, bytes_type
 
 try:
-    import ssl # Python 2.6+
+    import ssl  # Python 2.6+
 except ImportError:
     ssl = None
 
+
 class HTTPServer(TCPServer):
     r"""A non-blocking, single-threaded HTTP server.
 
@@ -105,7 +106,7 @@ class HTTPServer(TCPServer):
        In many cases, `tornado.web.Application.listen` can be used to avoid
        the need to explicitly create the `HTTPServer`.
 
-    2. `~tornado.netutil.TCPServer.bind`/`~tornado.netutil.TCPServer.start`: 
+    2. `~tornado.netutil.TCPServer.bind`/`~tornado.netutil.TCPServer.start`:
        simple multi-process::
 
             server = HTTPServer(app)
@@ -145,10 +146,12 @@ class HTTPServer(TCPServer):
         HTTPConnection(stream, address, self.request_callback,
                        self.no_keep_alive, self.xheaders)
 
+
 class _BadRequestException(Exception):
     """Exception class for malformed HTTP requests."""
     pass
 
+
 class HTTPConnection(object):
     """Handles a connection to an HTTP client, executing HTTP requests.
 
@@ -191,7 +194,7 @@ class HTTPConnection(object):
         if self._write_callback is not None:
             callback = self._write_callback
             self._write_callback = None
-            callback()            
+            callback()
         # _on_write_complete is enqueued on the IOLoop whenever the
         # IOStream's write buffer becomes empty, but it's possible for
         # another callback that runs on the IOLoop before it to
@@ -338,8 +341,8 @@ class HTTPRequest(object):
        GET/POST arguments are available in the arguments property, which
        maps arguments names to lists of values (to support multiple values
        for individual names). Names are of type `str`, while arguments
-       are byte strings.  Note that this is different from 
-       `RequestHandler.get_argument`, which returns argument values as 
+       are byte strings.  Note that this is different from
+       `RequestHandler.get_argument`, which returns argument values as
        unicode strings.
 
     .. attribute:: files
@@ -377,7 +380,7 @@ class HTTPRequest(object):
             self.remote_ip = remote_ip
             if protocol:
                 self.protocol = protocol
-            elif connection and isinstance(connection.stream, 
+            elif connection and isinstance(connection.stream,
                                            iostream.SSLIOStream):
                 self.protocol = "https"
             else:
@@ -395,7 +398,8 @@ class HTTPRequest(object):
         self.arguments = {}
         for name, values in arguments.iteritems():
             values = [v for v in values if v]
-            if values: self.arguments[name] = values
+            if values:
+                self.arguments[name] = values
 
     def supports_http_1_1(self):
         """Returns True if this request supports HTTP/1.1 semantics"""
@@ -475,4 +479,3 @@ class HTTPRequest(object):
                 return False
             raise
         return True
-
index 4f86c3782056e611b92930b192b5196f8bd7f461..c44b735c5cd52215a44c57cf5bde4eb13962eeb1 100644 (file)
@@ -24,6 +24,7 @@ import re
 
 from tornado.util import b, ObjectDict
 
+
 class HTTPHeaders(dict):
     """A dictionary that maintains Http-Header-Case for all keys.
 
@@ -174,7 +175,8 @@ def url_concat(url, args):
     >>> url_concat("http://example.com/foo?a=b", dict(c="d"))
     'http://example.com/foo?a=b&c=d'
     """
-    if not args: return url
+    if not args:
+        return url
     if url[-1] not in ('?', '&'):
         url += '&' if ('?' in url) else '?'
     return url + urllib.urlencode(args)
@@ -212,7 +214,8 @@ def parse_multipart_form_data(boundary, data, arguments, files):
         footer_length = len(boundary) + 4
     parts = data[:-footer_length].split(b("--") + boundary + b("\r\n"))
     for part in parts:
-        if not part: continue
+        if not part:
+            continue
         eoh = part.find(b("\r\n\r\n"))
         if eoh == -1:
             logging.warning("multipart/form-data missing headers")
@@ -252,6 +255,7 @@ def _parseparam(s):
         yield f.strip()
         s = s[end:]
 
+
 def _parse_header(line):
     """Parse a Content-type like header.
 
@@ -265,7 +269,7 @@ def _parse_header(line):
         i = p.find('=')
         if i >= 0:
             name = p[:i].strip().lower()
-            value = p[i+1:].strip()
+            value = p[i + 1:].strip()
             if len(value) >= 2 and value[0] == value[-1] == '"':
                 value = value[1:-1]
                 value = value.replace('\\\\', '\\').replace('\\"', '"')
index be294c45f11be5cf309eb91e95fea85e4c7c3b3d..ea1a9b5007bb9e3e1eeaaf705e4d0625f3076a43 100644 (file)
@@ -431,7 +431,7 @@ class _Timeout(object):
     @staticmethod
     def timedelta_to_seconds(td):
         """Equivalent to td.total_seconds() (introduced in python 2.7)."""
-        return (td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / float(10**6)
+        return (td.microseconds + (td.seconds + td.days * 24 * 3600) * 10 ** 6) / float(10 ** 6)
 
     # Comparison methods to sort by deadline, with object id as a tiebreaker
     # to guarantee a consistent ordering.  The heapq module uses __le__
@@ -474,7 +474,8 @@ class PeriodicCallback(object):
             self._timeout = None
 
     def _run(self):
-        if not self._running: return
+        if not self._running:
+            return
         try:
             self.callback()
         except Exception:
@@ -591,8 +592,10 @@ class _Select(object):
         pass
 
     def register(self, fd, events):
-        if events & IOLoop.READ: self.read_fds.add(fd)
-        if events & IOLoop.WRITE: self.write_fds.add(fd)
+        if events & IOLoop.READ:
+            self.read_fds.add(fd)
+        if events & IOLoop.WRITE:
+            self.write_fds.add(fd)
         if events & IOLoop.ERROR:
             self.error_fds.add(fd)
             # Closed connections are reported as errors by epoll and kqueue,
index b9ba13548d94768b3e97edba02afc741082aa5bd..8f0b32f30fa2ec56930ff225fbce4abfe72b2440 100644 (file)
@@ -30,16 +30,17 @@ from tornado import stack_context
 from tornado.util import b, bytes_type
 
 try:
-    import ssl # Python 2.6+
+    import ssl  # Python 2.6+
 except ImportError:
     ssl = None
 
+
 class IOStream(object):
     r"""A utility class to write to and read from a non-blocking socket.
 
     We support a non-blocking ``write()`` and a family of ``read_*()`` methods.
     All of the methods take callbacks (since writing and reading are
-    non-blocking and asynchronous). 
+    non-blocking and asynchronous).
 
     The socket parameter may either be connected or unconnected.  For
     server operations the socket is the result of calling socket.accept().
@@ -147,7 +148,7 @@ class IOStream(object):
             if self._read_to_buffer() == 0:
                 break
         self._add_io_state(self.io_loop.READ)
-        
+
     def read_until(self, delimiter, callback):
         """Call callback when we read the given delimiter."""
         assert not self._read_callback, "Already reading"
@@ -655,7 +656,6 @@ class SSLIOStream(IOStream):
         # until we've completed the SSL handshake (so certificates are
         # available, etc).
 
-
     def _read_from_socket(self):
         if self._ssl_accepting:
             # If the handshake hasn't finished yet, there can't be anything
@@ -686,6 +686,7 @@ class SSLIOStream(IOStream):
             return None
         return chunk
 
+
 def _merge_prefix(deque, size):
     """Replace the first entries in a deque of strings with a single
     string of up to size bytes.
@@ -723,6 +724,7 @@ def _merge_prefix(deque, size):
     if not deque:
         deque.appendleft(b(""))
 
+
 def doctests():
     import doctest
     return doctest.DocTestSuite()
index f36ed5a6bd64a7c144fad433f97f46c1c17ce434..fdfe0afcb7e4e2d7692eee1368dffa0f8f2376f4 100644 (file)
@@ -52,6 +52,7 @@ _translations = {}
 _supported_locales = frozenset([_default_locale])
 _use_gettext = False
 
+
 def get(*locale_codes):
     """Returns the closest match for the given locale codes.
 
@@ -111,7 +112,8 @@ def load_translations(directory):
     global _supported_locales
     _translations = {}
     for path in os.listdir(directory):
-        if not path.endswith(".csv"): continue
+        if not path.endswith(".csv"):
+            continue
         locale, extension = path.split(".")
         if not re.match("[a-z]+(_[A-Z]+)?$", locale):
             logging.error("Unrecognized locale %r (path: %s)", locale,
@@ -120,7 +122,8 @@ def load_translations(directory):
         f = open(os.path.join(directory, path), "r")
         _translations[locale] = {}
         for i, row in enumerate(csv.reader(f)):
-            if not row or len(row) < 2: continue
+            if not row or len(row) < 2:
+                continue
             row = [c.decode("utf-8").strip() for c in row]
             english, translation = row[:2]
             if len(row) > 2:
@@ -136,6 +139,7 @@ def load_translations(directory):
     _supported_locales = frozenset(_translations.keys() + [_default_locale])
     logging.info("Supported locales: %s", sorted(_supported_locales))
 
+
 def load_gettext_translations(directory, domain):
     """Loads translations from gettext's locale tree
 
@@ -160,10 +164,12 @@ def load_gettext_translations(directory, domain):
     global _use_gettext
     _translations = {}
     for lang in os.listdir(directory):
-        if lang.startswith('.'): continue  # skip .svn, etc
-        if os.path.isfile(os.path.join(directory, lang)): continue
+        if lang.startswith('.'):
+            continue  # skip .svn, etc
+        if os.path.isfile(os.path.join(directory, lang)):
+            continue
         try:
-            os.stat(os.path.join(directory, lang, "LC_MESSAGES", domain+".mo"))
+            os.stat(os.path.join(directory, lang, "LC_MESSAGES", domain + ".mo"))
             _translations[lang] = gettext.translation(domain, directory,
                                                       languages=[lang])
         except Exception, e:
@@ -189,7 +195,8 @@ class Locale(object):
     def get_closest(cls, *locale_codes):
         """Returns the closest match for the given locale code."""
         for code in locale_codes:
-            if not code: continue
+            if not code:
+                continue
             code = code.replace("-", "_")
             parts = code.split("_")
             if len(parts) > 2:
@@ -291,16 +298,16 @@ class Locale(object):
             if relative and days == 0:
                 if seconds < 50:
                     return _("1 second ago", "%(seconds)d seconds ago",
-                             seconds) % { "seconds": seconds }
+                             seconds) % {"seconds": seconds}
 
                 if seconds < 50 * 60:
                     minutes = round(seconds / 60.0)
                     return _("1 minute ago", "%(minutes)d minutes ago",
-                             minutes) % { "minutes": minutes }
+                             minutes) % {"minutes": minutes}
 
                 hours = round(seconds / (60.0 * 60))
                 return _("1 hour ago", "%(hours)d hours ago",
-                         hours) % { "hours": hours }
+                         hours) % {"hours": hours}
 
             if days == 0:
                 format = _("%(time)s")
@@ -366,8 +373,10 @@ class Locale(object):
         of size 1.
         """
         _ = self.translate
-        if len(parts) == 0: return ""
-        if len(parts) == 1: return parts[0]
+        if len(parts) == 0:
+            return ""
+        if len(parts) == 1:
+            return parts[0]
         comma = u' \u0648 ' if self.code.startswith("fa") else u", "
         return _("%(commas)s and %(last)s") % {
             "commas": comma.join(parts[:-1]),
@@ -385,6 +394,7 @@ class Locale(object):
             value = value[:-3]
         return ",".join(reversed(parts))
 
+
 class CSVLocale(Locale):
     """Locale implementation using tornado's CSV translation format."""
     def translate(self, message, plural_message=None, count=None):
@@ -399,6 +409,7 @@ class CSVLocale(Locale):
             message_dict = self.translations.get("unknown", {})
         return message_dict.get(message, message)
 
+
 class GettextLocale(Locale):
     """Locale implementation using the gettext module."""
     def translate(self, message, plural_message=None, count=None):
index 416d03062adf66792b77280c280cfc7c5053696a..d06a176b41c44de31232566054c7b3a415d310e2 100644 (file)
@@ -30,10 +30,11 @@ from tornado.iostream import IOStream, SSLIOStream
 from tornado.platform.auto import set_close_exec
 
 try:
-    import ssl # Python 2.6+
+    import ssl  # Python 2.6+
 except ImportError:
     ssl = None
 
+
 class TCPServer(object):
     r"""A non-blocking, single-threaded TCP server.
 
@@ -233,7 +234,7 @@ def bind_sockets(port, address=None, family=socket.AF_UNSPEC, backlog=128):
     or socket.AF_INET6 to restrict to ipv4 or ipv6 addresses, otherwise
     both will be used if available.
 
-    The ``backlog`` argument has the same meaning as for 
+    The ``backlog`` argument has the same meaning as for
     ``socket.listen()``.
     """
     sockets = []
@@ -277,7 +278,7 @@ if hasattr(socket, 'AF_UNIX'):
         If any other file with that name exists, an exception will be
         raised.
 
-        Returns a socket object (not a list of socket objects like 
+        Returns a socket object (not a list of socket objects like
         `bind_sockets`)
         """
         sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
@@ -299,6 +300,7 @@ if hasattr(socket, 'AF_UNIX'):
         sock.listen(backlog)
         return sock
 
+
 def add_accept_handler(sock, callback, io_loop=None):
     """Adds an ``IOLoop`` event handler to accept new connections on ``sock``.
 
@@ -310,6 +312,7 @@ def add_accept_handler(sock, callback, io_loop=None):
     """
     if io_loop is None:
         io_loop = IOLoop.instance()
+
     def accept_handler(fd, events):
         while True:
             try:
index db9521ea4f83a0e6b6c7fef17f4cf5d432912e15..feae3292cdf87d7a4ea9b2a8f2ed4ac22a37effe 100644 (file)
@@ -96,7 +96,8 @@ def define(name, default=None, type=None, help=None, metavar=None,
     frame = sys._getframe(0)
     options_file = frame.f_code.co_filename
     file_name = frame.f_back.f_code.co_filename
-    if file_name == options_file: file_name = ""
+    if file_name == options_file:
+        file_name = ""
     if type is None:
         if not multiple and default is not None:
             type = default.__class__
@@ -116,7 +117,8 @@ def parse_command_line(args=None):
 
     We return all command line arguments that are not options as a list.
     """
-    if args is None: args = sys.argv
+    if args is None:
+        args = sys.argv
     remaining = []
     for i in xrange(1, len(args)):
         # All things after the last option are command line arguments
@@ -124,7 +126,7 @@ def parse_command_line(args=None):
             remaining = args[i:]
             break
         if args[i] == "--":
-            remaining = args[i+1:]
+            remaining = args[i + 1:]
             break
         arg = args[i].lstrip("-")
         name, equals, value = arg.partition("=")
@@ -170,7 +172,8 @@ def print_help(file=sys.stdout):
         by_group.setdefault(option.group_name, []).append(option)
 
     for filename, o in sorted(by_group.items()):
-        if filename: print >> file, filename
+        if filename:
+            print >> file, filename
         o.sort(key=lambda option: option.name)
         for option in o:
             prefix = option.name
@@ -228,7 +231,7 @@ class _Option(object):
                     lo, _, hi = part.partition(":")
                     lo = _parse(lo)
                     hi = _parse(hi) if hi else lo
-                    self._value.extend(range(lo, hi+1))
+                    self._value.extend(range(lo, hi + 1))
                 else:
                     self._value.append(_parse(part))
         else:
@@ -322,7 +325,7 @@ class Error(Exception):
 
 def enable_pretty_logging():
     """Turns on formatted logging output as configured.
-    
+
     This is called automatically by `parse_command_line`.
     """
     root_logger = logging.getLogger()
@@ -350,7 +353,6 @@ def enable_pretty_logging():
         root_logger.addHandler(channel)
 
 
-
 class _LogFormatter(logging.Formatter):
     def __init__(self, color, *args, **kwargs):
         logging.Formatter.__init__(self, *args, **kwargs)
@@ -368,13 +370,13 @@ class _LogFormatter(logging.Formatter):
             if (3, 0) < sys.version_info < (3, 2, 3):
                 fg_color = unicode(fg_color, "ascii")
             self._colors = {
-                logging.DEBUG: unicode(curses.tparm(fg_color, 4), # Blue
+                logging.DEBUG: unicode(curses.tparm(fg_color, 4),  # Blue
                                        "ascii"),
-                logging.INFO: unicode(curses.tparm(fg_color, 2), # Green
+                logging.INFO: unicode(curses.tparm(fg_color, 2),  # Green
                                       "ascii"),
-                logging.WARNING: unicode(curses.tparm(fg_color, 3), # Yellow
+                logging.WARNING: unicode(curses.tparm(fg_color, 3),  # Yellow
                                          "ascii"),
-                logging.ERROR: unicode(curses.tparm(fg_color, 1), # Red
+                logging.ERROR: unicode(curses.tparm(fg_color, 1),  # Red
                                        "ascii"),
             }
             self._normal = unicode(curses.tigetstr("sgr0"), "ascii")
index c08f8eaf313cae9563c6b2d4743aae81116aecab..21e72cd9eb7dc9e38c2bebec1c3f6b5cc8c729a7 100644 (file)
@@ -23,10 +23,12 @@ implementation from `tornado.platform.auto`.
 
 from __future__ import absolute_import, division, with_statement
 
+
 def set_close_exec(fd):
     """Sets the close-on-exec bit (``FD_CLOEXEC``)for a file descriptor."""
     raise NotImplementedError()
 
+
 class Waker(object):
     """A socket-like object that can wake another thread from ``select()``.
 
@@ -38,7 +40,7 @@ class Waker(object):
     """
     def fileno(self):
         """Returns a file descriptor for this waker.
-        
+
         Must be suitable for use with ``select()`` or equivalent on the
         local platform.
         """
@@ -55,5 +57,3 @@ class Waker(object):
     def close(self):
         """Closes the waker's file descriptor(s)."""
         raise NotImplementedError()
-
-    
index ae5abed82c80b4ae1bb929f53a1fca9f30c92094..8d674c0e234b42a393c2ef5231a38474d9628cf0 100644 (file)
@@ -24,14 +24,17 @@ import os
 from tornado.platform import interface
 from tornado.util import b
 
+
 def set_close_exec(fd):
     flags = fcntl.fcntl(fd, fcntl.F_GETFD)
     fcntl.fcntl(fd, fcntl.F_SETFD, flags | fcntl.FD_CLOEXEC)
 
+
 def _set_nonblocking(fd):
     flags = fcntl.fcntl(fd, fcntl.F_GETFL)
     fcntl.fcntl(fd, fcntl.F_SETFL, flags | os.O_NONBLOCK)
-    
+
+
 class Waker(interface.Waker):
     def __init__(self):
         r, w = os.pipe()
@@ -55,7 +58,8 @@ class Waker(interface.Waker):
         try:
             while True:
                 result = self.reader.read()
-                if not result: break;
+                if not result:
+                    break
         except IOError:
             pass
 
index 7aa1a805d6e97abaf8700caa3b4b049ac824ce4e..564c5f8f9742a7ab1a605b5b01dda3ee47850f55 100644 (file)
@@ -107,6 +107,7 @@ class TornadoDelayedCall(object):
     def active(self):
         return self._active
 
+
 class TornadoReactor(PosixReactorBase):
     """Twisted reactor built on the Tornado IOLoop.
 
@@ -125,7 +126,7 @@ class TornadoReactor(PosixReactorBase):
         self._io_loop = io_loop
         self._readers = {}  # map of reader objects to fd
         self._writers = {}  # map of writer objects to fd
-        self._fds = {} # a map of fd to a (reader, writer) tuple
+        self._fds = {}  # a map of fd to a (reader, writer) tuple
         self._delayedCalls = {}
         PosixReactorBase.__init__(self)
 
@@ -295,6 +296,7 @@ class TornadoReactor(PosixReactorBase):
         if self._stopped:
             self.fireSystemEvent("shutdown")
 
+
 class _TestReactor(TornadoReactor):
     """Subclass of TornadoReactor for use in unittests.
 
@@ -319,7 +321,6 @@ class _TestReactor(TornadoReactor):
             port, protocol, interface=interface, maxPacketSize=maxPacketSize)
 
 
-
 def install(io_loop=None):
     """Install this package as the default Twisted reactor."""
     if not io_loop:
index 16fd66090b9446675cbac8433d5066da048a420a..a39a83b788703fc252687550be2d357fac4d27e2 100644 (file)
@@ -90,7 +90,8 @@ class Waker(interface.Waker):
         try:
             while True:
                 result = self.reader.recv(1024)
-                if not result: break
+                if not result:
+                    break
         except IOError:
             pass
 
index 5a33e696b3e4cc1cff013cfff0d36985f0666dfd..28a61bcd3e9392b9bc07f577630cfafa60e14e70 100644 (file)
@@ -29,10 +29,11 @@ from binascii import hexlify
 from tornado import ioloop
 
 try:
-    import multiprocessing # Python 2.6+
+    import multiprocessing  # Python 2.6+
 except ImportError:
     multiprocessing = None
 
+
 def cpu_count():
     """Returns the number of processors on this machine."""
     if multiprocessing is not None:
@@ -47,6 +48,7 @@ def cpu_count():
     logging.error("Could not detect number of processors; assuming 1")
     return 1
 
+
 def _reseed_random():
     if 'random' not in sys.modules:
         return
@@ -63,6 +65,7 @@ def _reseed_random():
 
 _task_id = None
 
+
 def fork_processes(num_processes, max_restarts=100):
     """Starts multiple worker processes.
 
@@ -97,6 +100,7 @@ def fork_processes(num_processes, max_restarts=100):
                            "IOLoop.instance() before calling start_processes()")
     logging.info("Starting %d processes", num_processes)
     children = {}
+
     def start_child(i):
         pid = os.fork()
         if pid == 0:
@@ -110,7 +114,8 @@ def fork_processes(num_processes, max_restarts=100):
             return None
     for i in range(num_processes):
         id = start_child(i)
-        if id is not None: return id
+        if id is not None:
+            return id
     num_restarts = 0
     while children:
         try:
@@ -135,13 +140,15 @@ def fork_processes(num_processes, max_restarts=100):
         if num_restarts > max_restarts:
             raise RuntimeError("Too many child restarts, giving up")
         new_id = start_child(id)
-        if new_id is not None: return new_id
+        if new_id is not None:
+            return new_id
     # All child processes exited cleanly, so exit the master process
     # instead of just returning to right after the call to
     # fork_processes (which will probably just start up another IOLoop
     # unless the caller checks the return value).
     sys.exit(0)
 
+
 def task_id():
     """Returns the current task id, if any.
 
index 1f35b0989babbcbf5f06bb3c5a00b1c3f9c8a8f6..09505dc1860161aab20442fe11f6ac03e12fe43d 100644 (file)
@@ -28,12 +28,13 @@ except ImportError:
     from cStringIO import StringIO as BytesIO  # python 2
 
 try:
-    import ssl # python 2.6+
+    import ssl  # python 2.6+
 except ImportError:
     ssl = None
 
 _DEFAULT_CA_CERTS = os.path.dirname(__file__) + '/ca-certificates.crt'
 
+
 class SimpleAsyncHTTPClient(AsyncHTTPClient):
     """Non-blocking HTTP client with no external dependencies.
 
@@ -119,7 +120,6 @@ class SimpleAsyncHTTPClient(AsyncHTTPClient):
         self._process_queue()
 
 
-
 class _HTTPConnection(object):
     _SUPPORTED_METHODS = set(["GET", "HEAD", "POST", "PUT", "DELETE"])
 
@@ -198,7 +198,7 @@ class _HTTPConnection(object):
                 # compatibility with servers configured for TLSv1 only,
                 # but nearly all servers support SSLv3:
                 # http://blog.ivanristic.com/2011/09/ssl-survey-protocol-support.html
-                if sys.version_info >= (2,7):
+                if sys.version_info >= (2, 7):
                     ssl_options["ciphers"] = "DEFAULT:!SSLv2"
                 else:
                     # This is really only necessary for pre-1.0 versions
@@ -367,7 +367,7 @@ class _HTTPConnection(object):
             self.headers.get("Content-Encoding") == "gzip"):
             # Magic parameter makes zlib module understand gzip header
             # http://stackoverflow.com/questions/1838699/how-can-i-decompress-a-gzip-stream-with-zlib
-            self._decompressor = zlib.decompressobj(16+zlib.MAX_WBITS)
+            self._decompressor = zlib.decompressobj(16 + zlib.MAX_WBITS)
         if self.headers.get("Transfer-Encoding") == "chunked":
             self.chunks = []
             self.stream.read_until(b("\r\n"), self._on_chunk_length)
@@ -417,7 +417,7 @@ class _HTTPConnection(object):
                 self.request.streaming_callback(data)
             buffer = BytesIO()
         else:
-            buffer = BytesIO(data) # TODO: don't require one big string?
+            buffer = BytesIO(data)  # TODO: don't require one big string?
         response = HTTPResponse(original_request,
                                 self.code, headers=self.headers,
                                 request_time=time.time() - self.start_time,
@@ -456,6 +456,7 @@ class _HTTPConnection(object):
 class CertificateError(ValueError):
     pass
 
+
 def _dnsname_to_pat(dn):
     pats = []
     for frag in dn.split(r'.'):
@@ -469,6 +470,7 @@ def _dnsname_to_pat(dn):
             pats.append(frag.replace(r'\*', '[^.]*'))
     return re.compile(r'\A' + r'\.'.join(pats) + r'\Z', re.IGNORECASE)
 
+
 def match_hostname(cert, hostname):
     """Verify that *cert* (in decoded format as returned by
     SSLSocket.getpeercert()) matches the *hostname*.  RFC 2818 rules
index c3fb022347d631baf4f4dd11d11892e99d63a285..5e744815d2b95a3c271ca25eb7b75ffdbc2a0dad 100644 (file)
@@ -74,11 +74,13 @@ import itertools
 import sys
 import threading
 
+
 class _State(threading.local):
     def __init__(self):
         self.contexts = ()
 _state = _State()
 
+
 class StackContext(object):
     '''Establishes the given context as a StackContext that will be transferred.
 
@@ -116,6 +118,7 @@ class StackContext(object):
         finally:
             _state.contexts = self.old_contexts
 
+
 class ExceptionStackContext(object):
     '''Specialization of StackContext for exception handling.
 
@@ -144,6 +147,7 @@ class ExceptionStackContext(object):
         finally:
             _state.contexts = self.old_contexts
 
+
 class NullContext(object):
     '''Resets the StackContext.
 
@@ -158,9 +162,11 @@ class NullContext(object):
     def __exit__(self, type, value, traceback):
         _state.contexts = self.old_contexts
 
+
 class _StackContextWrapper(functools.partial):
     pass
 
+
 def wrap(fn):
     '''Returns a callable object that will restore the current StackContext
     when executed.
@@ -173,6 +179,7 @@ def wrap(fn):
         return fn
     # functools.wraps doesn't appear to work on functools.partial objects
     #@functools.wraps(fn)
+
     def wrapped(callback, contexts, *args, **kwargs):
         if contexts is _state.contexts or not contexts:
             callback(*args, **kwargs)
@@ -190,7 +197,7 @@ def wrap(fn):
                 for a, b in itertools.izip(_state.contexts, contexts))):
             # contexts have been removed or changed, so start over
             new_contexts = ([NullContext()] +
-                            [cls(arg) for (cls,arg) in contexts])
+                            [cls(arg) for (cls, arg) in contexts])
         else:
             new_contexts = [cls(arg)
                             for (cls, arg) in contexts[len(_state.contexts):]]
@@ -207,6 +214,7 @@ def wrap(fn):
     else:
         return _StackContextWrapper(fn)
 
+
 @contextlib.contextmanager
 def _nested(*managers):
     """Support multiple context managers in a single with-statement.
@@ -241,4 +249,3 @@ def _nested(*managers):
             # the right information. Another exception may
             # have been raised and caught by an exit method
             raise exc[0], exc[1], exc[2]
-
index c3e3cccea5e7613e7d7e925a7b45f7ac507ebeae..a211f396029ec48d9d14fdaec1da01719490ead1 100644 (file)
@@ -135,7 +135,7 @@ with ``{# ... #}``.
 
 ``{% for *var* in *expr* %}...{% end %}``
     Same as the python ``for`` statement.
-    
+
 ``{% from *x* import *y* %}``
     Same as the python ``import`` statement.
 
@@ -189,6 +189,7 @@ from tornado.util import bytes_type, ObjectDict
 _DEFAULT_AUTOESCAPE = "xhtml_escape"
 _UNSET = object()
 
+
 class Template(object):
     """A compiled template.
 
@@ -217,7 +218,7 @@ class Template(object):
             # the module name used in __name__ below.
             self.compiled = compile(
                 escape.to_unicode(self.code),
-                "%s.generated.py" % self.name.replace('.','_'),
+                "%s.generated.py" % self.name.replace('.', '_'),
                 "exec")
         except Exception:
             formatted_code = _format_code(self.code).rstrip()
@@ -326,6 +327,7 @@ class BaseLoader(object):
     def _create_template(self, name):
         raise NotImplementedError()
 
+
 class Loader(BaseLoader):
     """A template loader that loads from a single root directory.
 
@@ -404,7 +406,6 @@ class _File(_Node):
         return (self.body,)
 
 
-
 class _ChunkList(_Node):
     def __init__(self, chunks):
         self.chunks = chunks
@@ -531,11 +532,13 @@ class _Expression(_Node):
                               writer.current_template.autoescape, self.line)
         writer.write_line("_append(_tmp)", self.line)
 
+
 class _Module(_Expression):
     def __init__(self, expression, line):
         super(_Module, self).__init__("_modules." + expression, line,
                                       raw=True)
 
+
 class _Text(_Node):
     def __init__(self, value, line):
         self.value = value
@@ -608,7 +611,7 @@ class _CodeWriter(object):
             ancestors = ["%s:%d" % (tmpl.name, lineno)
                          for (tmpl, lineno) in self.include_stack]
             line_comment += ' (via %s)' % ', '.join(reversed(ancestors))
-        print >> self.file, "    "*indent + line + line_comment
+        print >> self.file, "    " * indent + line + line_comment
 
 
 class _TemplateReader(object):
@@ -651,9 +654,12 @@ class _TemplateReader(object):
         if type(key) is slice:
             size = len(self)
             start, stop, step = key.indices(size)
-            if start is None: start = self.pos
-            else: start += self.pos
-            if stop is not None: stop += self.pos
+            if start is None:
+                start = self.pos
+            else:
+                start += self.pos
+            if stop is not None:
+                stop += self.pos
             return self.text[slice(start, stop, step)]
         elif key < 0:
             return self.text[key]
@@ -796,7 +802,8 @@ def _parse(reader, template, in_block=None):
                 block = _Statement(suffix, line)
             elif operator == "autoescape":
                 fn = suffix.strip()
-                if fn == "None": fn = None
+                if fn == "None":
+                    fn = None
                 template.autoescape = fn
                 continue
             elif operator == "raw":
index e77396d430fe0e6ca17b30922784f5c49eed3078..748c526f7857ac3cdb28a5d22cde7d8af49ed5ad 100644 (file)
@@ -11,6 +11,7 @@ from tornado.testing import AsyncHTTPTestCase, LogTrapTestCase
 from tornado.util import b
 from tornado.web import RequestHandler, Application, asynchronous
 
+
 class OpenIdClientLoginHandler(RequestHandler, OpenIdMixin):
     def initialize(self, test):
         self._OPENID_ENDPOINT = test.get_url('/openid/server/authenticate')
@@ -27,11 +28,13 @@ class OpenIdClientLoginHandler(RequestHandler, OpenIdMixin):
         assert user is not None
         self.finish(user)
 
+
 class OpenIdServerAuthenticateHandler(RequestHandler):
     def post(self):
         assert self.get_argument('openid.mode') == 'check_authentication'
         self.write('is_valid:true')
 
+
 class OAuth1ClientLoginHandler(RequestHandler, OAuthMixin):
     def initialize(self, test, version):
         self._OAUTH_VERSION = version
@@ -58,6 +61,7 @@ class OAuth1ClientLoginHandler(RequestHandler, OAuthMixin):
         assert access_token == dict(key=b('uiop'), secret=b('5678')), access_token
         callback(dict(email='foo@example.com'))
 
+
 class OAuth1ClientRequestParametersHandler(RequestHandler, OAuthMixin):
     def initialize(self, version):
         self._OAUTH_VERSION = version
@@ -70,17 +74,21 @@ class OAuth1ClientRequestParametersHandler(RequestHandler, OAuthMixin):
             'http://www.example.com/api/asdf',
             dict(key='uiop', secret='5678'),
             parameters=dict(foo='bar'))
-        import urllib; urllib.urlencode(params)
+        import urllib
+        urllib.urlencode(params)
         self.write(params)
 
+
 class OAuth1ServerRequestTokenHandler(RequestHandler):
     def get(self):
         self.write('oauth_token=zxcv&oauth_token_secret=1234')
 
+
 class OAuth1ServerAccessTokenHandler(RequestHandler):
     def get(self):
         self.write('oauth_token=uiop&oauth_token_secret=5678')
 
+
 class OAuth2ClientLoginHandler(RequestHandler, OAuth2Mixin):
     def initialize(self, test):
         self._OAUTH_AUTHORIZE_URL = test.get_url('/oauth2/server/authorize')
@@ -139,7 +147,7 @@ class AuthTest(AsyncHTTPTestCase, LogTrapTestCase):
     def test_oauth10_get_user(self):
         response = self.fetch(
             '/oauth10/client/login?oauth_token=zxcv',
-            headers={'Cookie':'_oauth_request_token=enhjdg==|MTIzNA=='})
+            headers={'Cookie': '_oauth_request_token=enhjdg==|MTIzNA=='})
         response.rethrow()
         parsed = json_decode(response.body)
         self.assertEqual(parsed['email'], 'foo@example.com')
@@ -167,7 +175,7 @@ class AuthTest(AsyncHTTPTestCase, LogTrapTestCase):
     def test_oauth10a_get_user(self):
         response = self.fetch(
             '/oauth10a/client/login?oauth_token=zxcv',
-            headers={'Cookie':'_oauth_request_token=enhjdg==|MTIzNA=='})
+            headers={'Cookie': '_oauth_request_token=enhjdg==|MTIzNA=='})
         response.rethrow()
         parsed = json_decode(response.body)
         self.assertEqual(parsed['email'], 'foo@example.com')
index 1085462cbe4df6ae12e7934fab502286088b6ed0..a6da39fb9797472001c68262d123c603733b9104 100644 (file)
@@ -9,6 +9,7 @@ except ImportError:
 if pycurl is not None:
     from tornado.curl_httpclient import CurlAsyncHTTPClient
 
+
 class CurlHTTPClientCommonTestCase(HTTPClientCommonTestCase):
     def get_http_client(self):
         client = CurlAsyncHTTPClient(io_loop=self.io_loop)
index 37c9383174bdceb24908d8f23a778451d08339ed..25b5dae1aba7e90fdb77e6f499c970db444975a5 100644 (file)
@@ -66,11 +66,11 @@ linkify_tests = [
     ("http://www.example.com/wpstyle/?p=364.", {},
      u'<a href="http://www.example.com/wpstyle/?p=364">http://www.example.com/wpstyle/?p=364</a>.'),
 
-    ("rdar://1234", 
+    ("rdar://1234",
      {"permitted_protocols": ["http", "rdar"]},
      u'<a href="rdar://1234">rdar://1234</a>'),
 
-    ("rdar:/1234", 
+    ("rdar:/1234",
      {"permitted_protocols": ["rdar"]},
      u'<a href="rdar:/1234">rdar:/1234</a>'),
 
@@ -99,7 +99,7 @@ linkify_tests = [
     ("Just a www.example.com link.", {},
      u'Just a <a href="http://www.example.com">www.example.com</a> link.'),
 
-    ("Just a www.example.com link.", 
+    ("Just a www.example.com link.",
      {"require_protocol": True},
      u'Just a www.example.com link.'),
 
index e4e018c73b8e0422d8e3e19d23d76e2164c8a96b..86d7d0d608f30b009ac0ec8b347d2ee0f7fe0954 100644 (file)
@@ -8,6 +8,7 @@ from tornado.web import Application, RequestHandler, asynchronous
 
 from tornado import gen
 
+
 class GenTest(AsyncTestCase):
     def run_gen(self, f):
         f()
@@ -47,7 +48,7 @@ class GenTest(AsyncTestCase):
     def test_exception_phase1(self):
         @gen.engine
         def f():
-            1/0
+            1 / 0
         self.assertRaises(ZeroDivisionError, self.run_gen, f)
 
     def test_exception_phase2(self):
@@ -55,12 +56,12 @@ class GenTest(AsyncTestCase):
         def f():
             self.io_loop.add_callback((yield gen.Callback("k1")))
             yield gen.Wait("k1")
-            1/0
+            1 / 0
         self.assertRaises(ZeroDivisionError, self.run_gen, f)
 
     def test_exception_in_task_phase1(self):
         def fail_task(callback):
-            1/0
+            1 / 0
 
         @gen.engine
         def f():
@@ -74,7 +75,7 @@ class GenTest(AsyncTestCase):
     def test_exception_in_task_phase2(self):
         # This is the case that requires the use of stack_context in gen.engine
         def fail_task(callback):
-            self.io_loop.add_callback(lambda: 1/0)
+            self.io_loop.add_callback(lambda: 1 / 0)
 
         @gen.engine
         def f():
@@ -265,6 +266,7 @@ class GenSequenceHandler(RequestHandler):
         yield gen.Wait("k1")
         self.finish("3")
 
+
 class GenTaskHandler(RequestHandler):
     @asynchronous
     @gen.engine
@@ -275,6 +277,7 @@ class GenTaskHandler(RequestHandler):
         response.rethrow()
         self.finish(b("got response: ") + response.body)
 
+
 class GenExceptionHandler(RequestHandler):
     @asynchronous
     @gen.engine
@@ -284,20 +287,23 @@ class GenExceptionHandler(RequestHandler):
         yield gen.Task(io_loop.add_callback)
         raise Exception("oops")
 
+
 class GenYieldExceptionHandler(RequestHandler):
     @asynchronous
     @gen.engine
     def get(self):
         io_loop = self.request.connection.stream.io_loop
         # Test the interaction of the two stack_contexts.
+
         def fail_task(callback):
-            io_loop.add_callback(lambda: 1/0)
+            io_loop.add_callback(lambda: 1 / 0)
         try:
             yield gen.Task(fail_task)
             raise Exception("did not get expected exception")
         except ZeroDivisionError:
             self.finish('ok')
 
+
 class GenWebTest(AsyncHTTPTestCase, LogTrapTestCase):
     def get_app(self):
         return Application([
index 8cfa8f91a6824ce08113a5d9a61c2261bceed63d..9ec967999dabdae1103194df472111f00195e835 100644 (file)
@@ -15,27 +15,32 @@ from tornado.testing import AsyncHTTPTestCase, LogTrapTestCase, get_unused_port
 from tornado.util import b, bytes_type
 from tornado.web import Application, RequestHandler, url
 
+
 class HelloWorldHandler(RequestHandler):
     def get(self):
         name = self.get_argument("name", "world")
         self.set_header("Content-Type", "text/plain")
         self.finish("Hello %s!" % name)
 
+
 class PostHandler(RequestHandler):
     def post(self):
         self.finish("Post arg1: %s, arg2: %s" % (
             self.get_argument("arg1"), self.get_argument("arg2")))
 
+
 class ChunkHandler(RequestHandler):
     def get(self):
         self.write("asdf")
         self.flush()
         self.write("qwer")
 
+
 class AuthHandler(RequestHandler):
     def get(self):
         self.finish(self.request.headers["Authorization"])
 
+
 class CountdownHandler(RequestHandler):
     def get(self, count):
         count = int(count)
@@ -44,6 +49,7 @@ class CountdownHandler(RequestHandler):
         else:
             self.write("Zero")
 
+
 class EchoPostHandler(RequestHandler):
     def post(self):
         self.write(self.request.body)
@@ -51,6 +57,8 @@ class EchoPostHandler(RequestHandler):
 # These tests end up getting run redundantly: once here with the default
 # HTTPClient implementation, and then again in each implementation's own
 # test suite.
+
+
 class HTTPClientCommonTestCase(AsyncHTTPTestCase, LogTrapTestCase):
     def get_http_client(self):
         """Returns AsyncHTTPClient instance.  May be overridden in subclass."""
@@ -124,6 +132,7 @@ Transfer-Encoding: chunked
 0
 
 """).replace(b("\n"), b("\r\n")), callback=stream.close)
+
             def accept_callback(conn, address):
                 # fake an HTTP server using chunked encoding where the final chunks
                 # and connection close all happen at once
@@ -135,7 +144,6 @@ Transfer-Encoding: chunked
             resp = self.wait()
             resp.rethrow()
             self.assertEqual(resp.body, b("12"))
-        
 
     def test_basic_auth(self):
         self.assertEqual(self.fetch("/auth", auth_username="Aladdin",
index 5f75c752581d3dc59877b1a5b85f32382e62522d..01f8f837ece85e0bd86a1928ee6f46f0501df3f6 100644 (file)
@@ -22,6 +22,7 @@ try:
 except ImportError:
     ssl = None
 
+
 class HandlerBaseTestCase(AsyncHTTPTestCase, LogTrapTestCase):
     def get_app(self):
         return Application([('/', self.__class__.Handler)])
@@ -31,6 +32,7 @@ class HandlerBaseTestCase(AsyncHTTPTestCase, LogTrapTestCase):
         response.rethrow()
         return json_decode(response.body)
 
+
 class HelloWorldRequestHandler(RequestHandler):
     def initialize(self, protocol="http"):
         self.expected_protocol = protocol
@@ -42,6 +44,7 @@ class HelloWorldRequestHandler(RequestHandler):
     def post(self):
         self.finish("Got %d bytes in POST" % len(self.request.body))
 
+
 class BaseSSLTest(AsyncHTTPTestCase, LogTrapTestCase):
     def get_ssl_version(self):
         raise NotImplementedError()
@@ -55,7 +58,7 @@ class BaseSSLTest(AsyncHTTPTestCase, LogTrapTestCase):
                                                  force_instance=True)
 
     def get_app(self):
-        return Application([('/', HelloWorldRequestHandler, 
+        return Application([('/', HelloWorldRequestHandler,
                              dict(protocol="https"))])
 
     def get_httpserver_options(self):
@@ -74,6 +77,7 @@ class BaseSSLTest(AsyncHTTPTestCase, LogTrapTestCase):
                                **kwargs)
         return self.wait()
 
+
 class SSLTestMixin(object):
     def test_ssl(self):
         response = self.fetch('/')
@@ -82,7 +86,7 @@ class SSLTestMixin(object):
     def test_large_post(self):
         response = self.fetch('/',
                               method='POST',
-                              body='A'*5000)
+                              body='A' * 5000)
         self.assertEqual(response.body, b("Got 5000 bytes in POST"))
 
     def test_non_ssl_request(self):
@@ -99,16 +103,26 @@ class SSLTestMixin(object):
 # For example, SSLv3 and TLSv1 throw an exception if you try to read
 # from the socket before the handshake is complete, but the default
 # of SSLv23 allows it.
+
+
 class SSLv23Test(BaseSSLTest, SSLTestMixin):
-    def get_ssl_version(self): return ssl.PROTOCOL_SSLv23
+    def get_ssl_version(self):
+        return ssl.PROTOCOL_SSLv23
+
+
 class SSLv3Test(BaseSSLTest, SSLTestMixin):
-    def get_ssl_version(self): return ssl.PROTOCOL_SSLv3
+    def get_ssl_version(self):
+        return ssl.PROTOCOL_SSLv3
+
+
 class TLSv1Test(BaseSSLTest, SSLTestMixin):
-    def get_ssl_version(self): return ssl.PROTOCOL_TLSv1
+    def get_ssl_version(self):
+        return ssl.PROTOCOL_TLSv1
 
 if hasattr(ssl, 'PROTOCOL_SSLv2'):
     class SSLv2Test(BaseSSLTest):
-        def get_ssl_version(self): return ssl.PROTOCOL_SSLv2
+        def get_ssl_version(self):
+            return ssl.PROTOCOL_SSLv2
 
         def test_sslv2_fail(self):
             # This is really more of a client test, but run it here since
@@ -136,7 +150,7 @@ if ssl is None:
     del SSLv23Test
     del SSLv3Test
     del TLSv1Test
-elif getattr(ssl, 'OPENSSL_VERSION_INFO', (0,0)) < (1,0):
+elif getattr(ssl, 'OPENSSL_VERSION_INFO', (0, 0)) < (1, 0):
     # In pre-1.0 versions of openssl, SSLv23 clients always send SSLv2
     # ClientHello messages, which are rejected by SSLv3 and TLSv1
     # servers.  Note that while the OPENSSL_VERSION_INFO was formally
@@ -145,6 +159,7 @@ elif getattr(ssl, 'OPENSSL_VERSION_INFO', (0,0)) < (1,0):
     del SSLv3Test
     del TLSv1Test
 
+
 class MultipartTestHandler(RequestHandler):
     def post(self):
         self.finish({"header": self.request.headers["X-Header-Encoding-Test"],
@@ -153,6 +168,7 @@ class MultipartTestHandler(RequestHandler):
                      "filebody": _unicode(self.request.files["files"][0]["body"]),
                      })
 
+
 class RawRequestHTTPConnection(simple_httpclient._HTTPConnection):
     def set_request(self, request):
         self.__next_request = request
@@ -163,6 +179,8 @@ class RawRequestHTTPConnection(simple_httpclient._HTTPConnection):
         self.stream.read_until(b("\r\n\r\n"), self._on_headers)
 
 # This test is also called from wsgi_test
+
+
 class HTTPConnectionTest(AsyncHTTPTestCase, LogTrapTestCase):
     def get_handlers(self):
         return [("/multipart", MultipartTestHandler),
@@ -176,7 +194,7 @@ class HTTPConnectionTest(AsyncHTTPTestCase, LogTrapTestCase):
         conn = RawRequestHTTPConnection(self.io_loop, client,
                                         httpclient.HTTPRequest(self.get_url("/")),
                                         None, self.stop,
-                                        1024*1024)
+                                        1024 * 1024)
         conn.set_request(
             b("\r\n").join(headers +
                            [utf8("Content-Length: %d\r\n" % len(body))]) +
@@ -239,10 +257,12 @@ class HTTPConnectionTest(AsyncHTTPTestCase, LogTrapTestCase):
         self.assertEqual(body, b("Got 1024 bytes in POST"))
         stream.close()
 
+
 class EchoHandler(RequestHandler):
     def get(self):
         self.write(recursive_unicode(self.request.arguments))
 
+
 class TypeCheckHandler(RequestHandler):
     def prepare(self):
         self.errors = {}
@@ -279,9 +299,10 @@ class TypeCheckHandler(RequestHandler):
     def check_type(self, name, obj, expected_type):
         actual_type = type(obj)
         if expected_type != actual_type:
-            self.errors[name] = "expected %s, got %s" % (expected_type, 
+            self.errors[name] = "expected %s, got %s" % (expected_type,
                                                          actual_type)
 
+
 class HTTPServerTest(AsyncHTTPTestCase, LogTrapTestCase):
     def get_app(self):
         return Application([("/echo", EchoHandler),
@@ -303,6 +324,7 @@ class HTTPServerTest(AsyncHTTPTestCase, LogTrapTestCase):
         data = json_decode(response.body)
         self.assertEqual(data, {})
 
+
 class XHeaderTest(HandlerBaseTestCase):
     class Handler(RequestHandler):
         def get(self):
index 440f6e6e77c217c02b502baeec096270bcd02e25..d0e58e9108dab5d0163f52bda41518a54eafdbee 100644 (file)
@@ -15,42 +15,42 @@ class TestUrlConcat(unittest.TestCase):
     def test_url_concat_no_query_params(self):
         url = url_concat(
                 "https://localhost/path",
-                [('y','y'), ('z','z')],
+                [('y', 'y'), ('z', 'z')],
                 )
         self.assertEqual(url, "https://localhost/path?y=y&z=z")
 
     def test_url_concat_encode_args(self):
         url = url_concat(
                 "https://localhost/path",
-                [('y','/y'), ('z','z')],
+                [('y', '/y'), ('z', 'z')],
                 )
         self.assertEqual(url, "https://localhost/path?y=%2Fy&z=z")
 
     def test_url_concat_trailing_q(self):
         url = url_concat(
                 "https://localhost/path?",
-                [('y','y'), ('z','z')],
+                [('y', 'y'), ('z', 'z')],
                 )
         self.assertEqual(url, "https://localhost/path?y=y&z=z")
 
     def test_url_concat_q_with_no_trailing_amp(self):
         url = url_concat(
                 "https://localhost/path?x",
-                [('y','y'), ('z','z')],
+                [('y', 'y'), ('z', 'z')],
                 )
         self.assertEqual(url, "https://localhost/path?x&y=y&z=z")
 
     def test_url_concat_trailing_amp(self):
         url = url_concat(
                 "https://localhost/path?x&",
-                [('y','y'), ('z','z')],
+                [('y', 'y'), ('z', 'z')],
                 )
         self.assertEqual(url, "https://localhost/path?x&y=y&z=z")
 
     def test_url_concat_mult_params(self):
         url = url_concat(
                 "https://localhost/path?a=1&b=2",
-                [('y','y'), ('z','z')],
+                [('y', 'y'), ('z', 'z')],
                 )
         self.assertEqual(url, "https://localhost/path?a=1&b=2&y=y&z=z")
 
@@ -61,6 +61,7 @@ class TestUrlConcat(unittest.TestCase):
             )
         self.assertEqual(url, "https://localhost/path?r=1&t=2")
 
+
 class MultipartFormDataTest(LogTrapTestCase):
     def test_file_upload(self):
         data = b("""\
@@ -75,7 +76,7 @@ Foo
         file = files["files"][0]
         self.assertEqual(file["filename"], "ab.txt")
         self.assertEqual(file["body"], b("Foo"))
-        
+
     def test_unquoted_names(self):
         # quotes are optional unless special characters are present
         data = b("""\
@@ -90,7 +91,7 @@ Foo
         file = files["files"][0]
         self.assertEqual(file["filename"], "ab.txt")
         self.assertEqual(file["body"], b("Foo"))
-        
+
     def test_special_filenames(self):
         filenames = ['a;b.txt',
                      'a"b.txt',
index 030bb20eb1f49c9c5ed73d58e956cad3d40f92e5..584f070d2f1c95182c429e287bca4937207ec466 100644 (file)
@@ -1,6 +1,7 @@
 from __future__ import absolute_import, division, with_statement
 import unittest
 
+
 class ImportTest(unittest.TestCase):
     def test_import_everything(self):
         # Some of our modules are not otherwise tested.  Import them
index 159f91858b08355e90a82932c4f3cb14b1c0bbb3..df2cd77710737a7ea9d8652f86fcee4cd0de0e0c 100644 (file)
@@ -8,6 +8,7 @@ import time
 
 from tornado.testing import AsyncTestCase, LogTrapTestCase
 
+
 class TestIOLoop(AsyncTestCase, LogTrapTestCase):
     def test_add_callback_wakeup(self):
         # Make sure that add_callback from inside a running IOLoop
index c58da56f45baed7abe44f2bdff30cd575e0b0ddf..01f9a984f523e349c77cf789d954a29906ffa360 100644 (file)
@@ -8,10 +8,12 @@ from tornado.web import RequestHandler, Application
 import socket
 import time
 
+
 class HelloHandler(RequestHandler):
     def get(self):
         self.write("Hello")
 
+
 class TestIOStream(AsyncHTTPTestCase, LogTrapTestCase):
     def get_app(self):
         return Application([('/', HelloHandler)])
@@ -21,9 +23,11 @@ class TestIOStream(AsyncHTTPTestCase, LogTrapTestCase):
         [listener] = netutil.bind_sockets(port, '127.0.0.1',
                                           family=socket.AF_INET)
         streams = [None, None]
+
         def accept_callback(connection, address):
             streams[0] = IOStream(connection, io_loop=self.io_loop, **kwargs)
             self.stop()
+
         def connect_callback():
             streams[1] = client_stream
             self.stop()
@@ -69,7 +73,7 @@ class TestIOStream(AsyncHTTPTestCase, LogTrapTestCase):
         self.wait()
         # As a side effect, the stream is now listening for connection
         # close (if it wasn't already), but is not listening for writes
-        self.assertEqual(server._state, IOLoop.READ|IOLoop.ERROR)
+        self.assertEqual(server._state, IOLoop.READ | IOLoop.ERROR)
         server.close()
         client.close()
 
@@ -80,6 +84,7 @@ class TestIOStream(AsyncHTTPTestCase, LogTrapTestCase):
         port = get_unused_port()
         stream = IOStream(socket.socket(), self.io_loop)
         self.connect_called = False
+
         def connect_callback():
             self.connect_called = True
         stream.set_close_callback(self.stop)
@@ -102,7 +107,7 @@ class TestIOStream(AsyncHTTPTestCase, LogTrapTestCase):
         s.connect(("localhost", self.get_http_port()))
         stream = IOStream(s, io_loop=self.io_loop)
         stream.write(b("GET / HTTP/1.0\r\n\r\n"))
-        
+
         stream.read_until_close(self.stop)
         data = self.wait()
         self.assertTrue(data.startswith(b("HTTP/1.0 200")))
@@ -113,9 +118,11 @@ class TestIOStream(AsyncHTTPTestCase, LogTrapTestCase):
         try:
             chunks = []
             final_called = []
+
             def streaming_callback(data):
                 chunks.append(data)
                 self.stop()
+
             def final_callback(data):
                 assert not data
                 final_called.append(True)
@@ -140,6 +147,7 @@ class TestIOStream(AsyncHTTPTestCase, LogTrapTestCase):
         server, client = self.make_iostream_pair()
         try:
             chunks = []
+
             def callback(data):
                 chunks.append(data)
                 self.stop()
@@ -166,10 +174,12 @@ class TestIOStream(AsyncHTTPTestCase, LogTrapTestCase):
             client.set_close_callback(self.stop)
             server.write(b("12"))
             chunks = []
+
             def callback1(data):
                 chunks.append(data)
                 client.read_bytes(1, callback2)
                 server.close()
+
             def callback2(data):
                 chunks.append(data)
             client.read_bytes(1, callback1)
index 28f4e06d1a165380733210d6948215a17fbe5945..d49e3a5727c87260ef81a817384aefa60d0fff3a 100644 (file)
@@ -18,6 +18,8 @@ from tornado.web import RequestHandler, Application
 # Not using AsyncHTTPTestCase because we need control over the IOLoop.
 # Logging is tricky here so you may want to replace LogTrapTestCase
 # with unittest.TestCase when debugging.
+
+
 class ProcessTest(LogTrapTestCase):
     def get_app(self):
         class ProcessHandler(RequestHandler):
@@ -48,6 +50,7 @@ class ProcessTest(LogTrapTestCase):
     def test_multi_process(self):
         self.assertFalse(IOLoop.initialized())
         port = get_unused_port()
+
         def get_url(path):
             return "http://127.0.0.1:%d%s" % (port, path)
         sockets = bind_sockets(port, "127.0.0.1")
@@ -60,7 +63,8 @@ class ProcessTest(LogTrapTestCase):
             # finished with status 0
             self.assertEqual(e.code, 0)
             self.assertTrue(task_id() is None)
-            for sock in sockets: sock.close()
+            for sock in sockets:
+                sock.close()
             signal.alarm(0)
             return
         signal.alarm(5)  # child process
@@ -74,7 +78,8 @@ class ProcessTest(LogTrapTestCase):
             elif id == 2:
                 signal.alarm(5)
                 self.assertEqual(id, task_id())
-                for sock in sockets: sock.close()
+                for sock in sockets:
+                    sock.close()
                 # Always use SimpleAsyncHTTPClient here; the curl
                 # version appears to get confused sometimes if the
                 # connection gets closed before it's had a chance to
@@ -118,7 +123,7 @@ class ProcessTest(LogTrapTestCase):
         except Exception:
             logging.error("exception in child process %d", id, exc_info=True)
             raise
-            
+
 
 if os.name != 'posix' or sys.platform == 'cygwin':
     # All sorts of unixisms here
index c150656e0ec668a44d63944290013230618ba53a..16806456ba4e0c7c00e0d6ae63fc613b03b269fd 100755 (executable)
@@ -15,12 +15,14 @@ INTERPRETERS = [
     "pypy",
     ]
 
+
 def exists_on_path(filename):
     for dir in os.environ["PATH"].split(":"):
         if os.path.exists(os.path.join(dir, filename)):
             return True
     return False
 
+
 def main():
     for interpreter in INTERPRETERS:
         print "=================== %s =======================" % interpreter
index 6827de0aadcd1f051ba5329e0e1b173e2f844f85..91f8b25dae4484f45da6b5d23d79e4ff2f4f6458 100755 (executable)
@@ -27,6 +27,7 @@ TEST_MODULES = [
     'tornado.test.wsgi_test',
 ]
 
+
 def all():
     return unittest.defaultTestLoader.loadTestsFromNames(TEST_MODULES)
 
index 35b30877ec30fd14642e4d37935467a648b333fd..0f5d3151f490edb00c6a26dd5db73d3fb8aa0a1a 100644 (file)
@@ -12,6 +12,7 @@ from tornado.testing import AsyncHTTPTestCase, LogTrapTestCase
 from tornado.util import b
 from tornado.web import RequestHandler, Application, asynchronous, url
 
+
 class SimpleHTTPClientCommonTestCase(HTTPClientCommonTestCase):
     def get_http_client(self):
         client = SimpleAsyncHTTPClient(io_loop=self.io_loop,
@@ -23,6 +24,7 @@ class SimpleHTTPClientCommonTestCase(HTTPClientCommonTestCase):
 # try to run it again.
 del HTTPClientCommonTestCase
 
+
 class TriggerHandler(RequestHandler):
     def initialize(self, queue, wake_callback):
         self.queue = queue
@@ -35,32 +37,38 @@ class TriggerHandler(RequestHandler):
         if self.get_argument("wake", "true") == "true":
             self.wake_callback()
 
+
 class HangHandler(RequestHandler):
     @asynchronous
     def get(self):
         pass
 
+
 class ContentLengthHandler(RequestHandler):
     def get(self):
         self.set_header("Content-Length", self.get_argument("value"))
         self.write("ok")
 
+
 class HeadHandler(RequestHandler):
     def head(self):
         self.set_header("Content-Length", "7")
 
+
 class NoContentHandler(RequestHandler):
     def get(self):
         if self.get_argument("error", None):
             self.set_header("Content-Length", "7")
         self.set_status(204)
 
+
 class SeeOther303PostHandler(RequestHandler):
     def post(self):
         assert self.request.body == b("blah")
         self.set_header("Location", "/303_get")
         self.set_status(303)
 
+
 class SeeOther303GetHandler(RequestHandler):
     def get(self):
         assert not self.request.body
index 73c84402021ef2040e7840119fc687a875d852cf..e682f6a538501add080ed9ac58001916b6131086 100644 (file)
@@ -10,6 +10,7 @@ import functools
 import logging
 import unittest
 
+
 class TestRequestHandler(RequestHandler):
     def __init__(self, app, request, io_loop):
         super(TestRequestHandler, self).__init__(app, request)
@@ -38,6 +39,7 @@ class TestRequestHandler(RequestHandler):
         else:
             return 'unexpected failure'
 
+
 class HTTPStackContextTest(AsyncHTTPTestCase, LogTrapTestCase):
     def get_app(self):
         return Application([('/', TestRequestHandler,
@@ -53,6 +55,7 @@ class HTTPStackContextTest(AsyncHTTPTestCase, LogTrapTestCase):
         self.response = response
         self.stop()
 
+
 class StackContextTest(AsyncTestCase, LogTrapTestCase):
     def setUp(self):
         super(StackContextTest, self).setUp()
@@ -73,10 +76,12 @@ class StackContextTest(AsyncTestCase, LogTrapTestCase):
             with StackContext(functools.partial(self.context, 'library')):
                 self.io_loop.add_callback(
                   functools.partial(library_inner_callback, callback))
+
         def library_inner_callback(callback):
             self.assertEqual(self.active_contexts[-2:],
                              ['application', 'library'])
             callback()
+
         def final_callback():
             # implementation detail:  the full context stack at this point
             # is ['application', 'library', 'application'].  The 'library'
index 546778848c0ec9c8d75411e8314b0f04a75c3ae4..c0175deea3f1dbbdabf108a516d4fcac6f098202 100644 (file)
@@ -7,6 +7,7 @@ from tornado.template import Template, DictLoader, ParseError
 from tornado.testing import LogTrapTestCase
 from tornado.util import b, bytes_type, ObjectDict
 
+
 class TemplateTest(LogTrapTestCase):
     def test_simple(self):
         template = Template("Hello {{ name }}!")
@@ -85,11 +86,12 @@ class TemplateTest(LogTrapTestCase):
         self.assertEqual(template.generate(), utf8(u"\u00e9"))
 
     def test_custom_namespace(self):
-        loader = DictLoader({"test.html": "{{ inc(5) }}"}, namespace={"inc": lambda x: x+1})
+        loader = DictLoader({"test.html": "{{ inc(5) }}"}, namespace={"inc": lambda x: x + 1})
         self.assertEqual(loader.load("test.html").generate(), b("6"))
 
     def test_apply(self):
-        def upper(s): return s.upper()
+        def upper(s):
+            return s.upper()
         template = Template(utf8("{% apply upper %}foo{% end %}"))
         self.assertEqual(template.generate(upper=upper), b("FOO"))
 
@@ -102,6 +104,7 @@ class TemplateTest(LogTrapTestCase):
         template = Template(utf8("{% comment blah blah %}foo"))
         self.assertEqual(template.generate(), b("foo"))
 
+
 class StackTraceTest(LogTrapTestCase):
     def test_error_line_number_expression(self):
         loader = DictLoader({"test.html": """one
@@ -157,7 +160,6 @@ three{%end%}
             exc_stack = traceback.format_exc()
         self.assertTrue("# base.html:1" in exc_stack)
 
-
     def test_error_line_number_extends_sub_error(self):
         loader = DictLoader({
             "base.html": "{% block 'block' %}{% end %}",
@@ -228,7 +230,7 @@ default: {% include 'default.html' %}
 expr: {{ name }}
 raw: {% raw name %}""",
             }
-    
+
     def test_default_off(self):
         loader = DictLoader(self.templates, autoescape=None)
         name = "Bobby <table>s"
@@ -243,7 +245,7 @@ raw: {% raw name %}""",
                          b("escaped: Bobby &lt;table&gt;s\n"
                            "unescaped: Bobby <table>s\n"
                            "default: Bobby <table>s\n"))
-        
+
     def test_default_on(self):
         loader = DictLoader(self.templates, autoescape="xhtml_escape")
         name = "Bobby <table>s"
@@ -253,7 +255,7 @@ raw: {% raw name %}""",
                          b("Bobby <table>s"))
         self.assertEqual(loader.load("default.html").generate(name=name),
                          b("Bobby &lt;table&gt;s"))
-        
+
         self.assertEqual(loader.load("include.html").generate(name=name),
                          b("escaped: Bobby &lt;table&gt;s\n"
                            "unescaped: Bobby <table>s\n"
@@ -269,7 +271,9 @@ raw: {% raw name %}""",
 
     def test_extended_block(self):
         loader = DictLoader(self.templates)
-        def render(name): return loader.load(name).generate(name="<script>")
+
+        def render(name):
+            return loader.load(name).generate(name="<script>")
         self.assertEqual(render("escaped_extends_unescaped.html"),
                          b("base: <script>"))
         self.assertEqual(render("escaped_overrides_unescaped.html"),
@@ -282,19 +286,23 @@ raw: {% raw name %}""",
 
     def test_raw_expression(self):
         loader = DictLoader(self.templates)
-        def render(name): return loader.load(name).generate(name='<>&"')
+
+        def render(name):
+            return loader.load(name).generate(name='<>&"')
         self.assertEqual(render("raw_expression.html"),
                          b("expr: &lt;&gt;&amp;&quot;\n"
                            "raw: <>&\""))
 
     def test_custom_escape(self):
-        loader = DictLoader({"foo.py": 
+        loader = DictLoader({"foo.py":
                              "{% autoescape py_escape %}s = {{ name }}\n"})
+
         def py_escape(s):
             self.assertEqual(type(s), bytes_type)
             return repr(native_str(s))
+
         def render(template, name):
-            return loader.load(template).generate(py_escape=py_escape, 
+            return loader.load(template).generate(py_escape=py_escape,
                                                   name=name)
         self.assertEqual(render("foo.py", "<html>"),
                          b("s = '<html>'\n"))
index c2887396fc52da9083b28cb29328ce4d83acf94c..f445ce5cdbf09977c4c2561ddd42f2688ad79a4c 100644 (file)
@@ -4,15 +4,17 @@ from __future__ import absolute_import, division, with_statement
 import unittest
 from tornado.testing import AsyncTestCase, LogTrapTestCase
 
+
 class AsyncTestCaseTest(AsyncTestCase, LogTrapTestCase):
     def test_exception_in_callback(self):
-        self.io_loop.add_callback(lambda: 1/0)
+        self.io_loop.add_callback(lambda: 1 / 0)
         try:
             self.wait()
             self.fail("did not get expected exception")
         except ZeroDivisionError:
             pass
 
+
 class SetUpTearDownTest(unittest.TestCase):
     def test_set_up_tear_down(self):
         """
index 60b1ff4aa75754cf59d44602e80ad3bebdaed419..cff2b998e8146f45b7cc7ae90e01298da43362ca 100644 (file)
@@ -40,7 +40,9 @@ except ImportError:
     fcntl = None
     twisted = None
     IReadDescriptor = IWriteDescriptor = None
-    def implements(f): pass
+
+    def implements(f):
+        pass
 
 from tornado.httpclient import AsyncHTTPClient
 from tornado.ioloop import IOLoop
@@ -49,6 +51,7 @@ from tornado.testing import get_unused_port
 from tornado.util import import_object
 from tornado.web import RequestHandler, Application
 
+
 class ReactorTestCase(unittest.TestCase):
     def setUp(self):
         self._io_loop = IOLoop()
@@ -57,6 +60,7 @@ class ReactorTestCase(unittest.TestCase):
     def tearDown(self):
         self._io_loop.close(all_fds=True)
 
+
 class ReactorWhenRunningTest(ReactorTestCase):
     def test_whenRunning(self):
         self._whenRunningCalled = False
@@ -74,6 +78,7 @@ class ReactorWhenRunningTest(ReactorTestCase):
     def anotherWhenRunningCallback(self):
         self._anotherWhenRunningCalled = True
 
+
 class ReactorCallLaterTest(ReactorTestCase):
     def test_callLater(self):
         self._laterCalled = False
@@ -91,6 +96,7 @@ class ReactorCallLaterTest(ReactorTestCase):
         self._called = self._reactor.seconds()
         self._reactor.stop()
 
+
 class ReactorTwoCallLaterTest(ReactorTestCase):
     def test_callLater(self):
         self._later1Called = False
@@ -118,6 +124,7 @@ class ReactorTwoCallLaterTest(ReactorTestCase):
         self._called2 = self._reactor.seconds()
         self._reactor.stop()
 
+
 class ReactorCallFromThreadTest(ReactorTestCase):
     def setUp(self):
         super(ReactorCallFromThreadTest, self).setUp()
@@ -145,6 +152,7 @@ class ReactorCallFromThreadTest(ReactorTestCase):
         self._reactor.callWhenRunning(self._whenRunningCallback)
         self._reactor.run()
 
+
 class ReactorCallInThread(ReactorTestCase):
     def setUp(self):
         super(ReactorCallInThread, self).setUp()
@@ -161,6 +169,7 @@ class ReactorCallInThread(ReactorTestCase):
         self._reactor.callWhenRunning(self._whenRunningCallback)
         self._reactor.run()
 
+
 class Reader:
     implements(IReadDescriptor)
 
@@ -168,7 +177,8 @@ class Reader:
         self._fd = fd
         self._callback = callback
 
-    def logPrefix(self): return "Reader"
+    def logPrefix(self):
+        return "Reader"
 
     def close(self):
         self._fd.close()
@@ -182,6 +192,7 @@ class Reader:
     def doRead(self):
         self._callback(self._fd)
 
+
 class Writer:
     implements(IWriteDescriptor)
 
@@ -189,7 +200,8 @@ class Writer:
         self._fd = fd
         self._callback = callback
 
-    def logPrefix(self): return "Writer"
+    def logPrefix(self):
+        return "Writer"
 
     def close(self):
         self._fd.close()
@@ -203,6 +215,7 @@ class Writer:
     def doWrite(self):
         self._callback(self._fd)
 
+
 class ReactorReaderWriterTest(ReactorTestCase):
     def _set_nonblocking(self, fd):
         flags = fcntl.fcntl(fd, fcntl.F_GETFL)
@@ -229,9 +242,11 @@ class ReactorReaderWriterTest(ReactorTestCase):
         reads it, check the value and ends the test.
         """
         self.shouldWrite = True
+
         def checkReadInput(fd):
             self.assertEquals(fd.read(), 'x')
             self._reactor.stop()
+
         def writeOnce(fd):
             if self.shouldWrite:
                 self.shouldWrite = False
@@ -285,6 +300,8 @@ class ReactorReaderWriterTest(ReactorTestCase):
 
 # Test various combinations of twisted and tornado http servers,
 # http clients, and event loop interfaces.
+
+
 class CompatibilityTests(unittest.TestCase):
     def setUp(self):
         self.io_loop = IOLoop()
@@ -297,6 +314,7 @@ class CompatibilityTests(unittest.TestCase):
     def start_twisted_server(self):
         class HelloResource(Resource):
             isLeaf = True
+
             def render_GET(self, request):
                 return "Hello from twisted!"
         site = Site(HelloResource())
@@ -325,6 +343,7 @@ class CompatibilityTests(unittest.TestCase):
     def tornado_fetch(self, url, runner):
         responses = []
         client = AsyncHTTPClient(self.io_loop)
+
         def callback(response):
             responses.append(response)
             self.stop_loop()
@@ -339,18 +358,23 @@ class CompatibilityTests(unittest.TestCase):
         chunks = []
         client = Agent(self.reactor)
         d = client.request('GET', url)
+
         class Accumulator(Protocol):
             def __init__(self, finished):
                 self.finished = finished
+
             def dataReceived(self, data):
                 chunks.append(data)
+
             def connectionLost(self, reason):
                 self.finished.callback(None)
+
         def callback(response):
             finished = Deferred()
             response.deliverBody(Accumulator(finished))
             return finished
         d.addCallback(callback)
+
         def shutdown(ignored):
             self.stop_loop()
         d.addBoth(shutdown)
@@ -448,9 +472,11 @@ else:
                 # The test_func may be defined in a mixin, so clobber
                 # it instead of delattr()
                 setattr(test_class, test_func, lambda self: None)
+
         def make_test_subclass(test_class):
             class TornadoTest(test_class):
                 _reactors = ["tornado.platform.twisted._TestReactor"]
+
                 def unbuildReactor(self, reactor):
                     test_class.unbuildReactor(self, reactor)
                     # Clean up file descriptors (especially epoll/kqueue
index 11a4d5609fd7e8dee11eee93d069da98b42b77a6..32d4188a4e58c95950627c56e68dabe2d7010945 100644 (file)
@@ -13,6 +13,7 @@ import re
 import socket
 import sys
 
+
 class CookieTestRequestHandler(RequestHandler):
     # stub out enough methods to make the secure_cookie functions work
     def __init__(self):
@@ -26,6 +27,7 @@ class CookieTestRequestHandler(RequestHandler):
     def set_cookie(self, name, value, expires_days=None):
         self._cookies[name] = value
 
+
 class SecureCookieTest(LogTrapTestCase):
     def test_round_trip(self):
         handler = CookieTestRequestHandler()
@@ -57,6 +59,7 @@ class SecureCookieTest(LogTrapTestCase):
         # it gets rejected
         assert handler.get_secure_cookie('foo') is None
 
+
 class CookieTest(AsyncHTTPTestCase, LogTrapTestCase):
     def get_app(self):
         class SetCookieHandler(RequestHandler):
@@ -84,7 +87,6 @@ class CookieTest(AsyncHTTPTestCase, LogTrapTestCase):
                 self.set_cookie("semicolon", "a;b")
                 self.set_cookie("quote", 'a"b')
 
-
         return Application([
                 ("/set", SetCookieHandler),
                 ("/get", GetCookieHandler),
@@ -137,6 +139,7 @@ class CookieTest(AsyncHTTPTestCase, LogTrapTestCase):
             response = self.fetch("/get", headers={"Cookie": header})
             self.assertEqual(response.body, utf8(expected))
 
+
 class AuthRedirectRequestHandler(RequestHandler):
     def initialize(self, login_url):
         self.login_url = login_url
@@ -149,6 +152,7 @@ class AuthRedirectRequestHandler(RequestHandler):
         # we'll never actually get here because the test doesn't follow redirects
         self.send_error(500)
 
+
 class AuthRedirectTest(AsyncHTTPTestCase, LogTrapTestCase):
     def get_app(self):
         return Application([('/relative', AuthRedirectRequestHandler,
@@ -184,6 +188,7 @@ class ConnectionCloseHandler(RequestHandler):
     def on_connection_close(self):
         self.test.on_connection_close()
 
+
 class ConnectionCloseTest(AsyncHTTPTestCase, LogTrapTestCase):
     def get_app(self):
         return Application([('/', ConnectionCloseHandler, dict(test=self))])
@@ -203,6 +208,7 @@ class ConnectionCloseTest(AsyncHTTPTestCase, LogTrapTestCase):
         logging.info('connection closed')
         self.stop()
 
+
 class EchoHandler(RequestHandler):
     def get(self, path):
         # Type checks: web.py interfaces convert argument values to
@@ -219,6 +225,7 @@ class EchoHandler(RequestHandler):
         self.write(dict(path=path,
                         args=recursive_unicode(self.request.arguments)))
 
+
 class RequestEncodingTest(AsyncHTTPTestCase, LogTrapTestCase):
     def get_app(self):
         return Application([("/(.*)", EchoHandler)])
@@ -233,9 +240,10 @@ class RequestEncodingTest(AsyncHTTPTestCase, LogTrapTestCase):
     def test_path_encoding(self):
         # Path components and query arguments should be decoded the same way
         self.assertEqual(json_decode(self.fetch('/%C3%A9?arg=%C3%A9').body),
-                         {u"path":u"\u00e9",
+                         {u"path": u"\u00e9",
                           u"args": {u"arg": [u"\u00e9"]}})
 
+
 class TypeCheckHandler(RequestHandler):
     def prepare(self):
         self.errors = {}
@@ -271,6 +279,7 @@ class TypeCheckHandler(RequestHandler):
             self.errors[name] = "expected %s, got %s" % (expected_type,
                                                          actual_type)
 
+
 class DecodeArgHandler(RequestHandler):
     def decode_argument(self, value, name=None):
         assert type(value) == bytes_type, repr(value)
@@ -291,18 +300,22 @@ class DecodeArgHandler(RequestHandler):
                     'query': describe(self.get_argument("foo")),
                     })
 
+
 class LinkifyHandler(RequestHandler):
     def get(self):
         self.render("linkify.html", message="http://example.com")
 
+
 class UIModuleResourceHandler(RequestHandler):
     def get(self):
-        self.render("page.html", entries=[1,2])
+        self.render("page.html", entries=[1, 2])
+
 
 class OptionalPathHandler(RequestHandler):
     def get(self, path):
         self.write({"path": path})
 
+
 class FlowControlHandler(RequestHandler):
     # These writes are too small to demonstrate real flow control,
     # but at least it shows that the callbacks get run.
@@ -319,6 +332,7 @@ class FlowControlHandler(RequestHandler):
         self.write("3")
         self.finish()
 
+
 class MultiHeaderHandler(RequestHandler):
     def get(self):
         self.set_header("x-overwrite", "1")
@@ -326,6 +340,7 @@ class MultiHeaderHandler(RequestHandler):
         self.add_header("x-multi", 3)
         self.add_header("x-multi", "4")
 
+
 class RedirectHandler(RequestHandler):
     def get(self):
         if self.get_argument('permanent', None) is not None:
@@ -460,14 +475,14 @@ class ErrorResponseTest(AsyncHTTPTestCase, LogTrapTestCase):
             def get(self):
                 if self.get_argument("status", None):
                     raise HTTPError(int(self.get_argument("status")))
-                1/0
+                1 / 0
 
         class WriteErrorHandler(RequestHandler):
             def get(self):
                 if self.get_argument("status", None):
                     self.send_error(int(self.get_argument("status")))
                 else:
-                    1/0
+                    1 / 0
 
             def write_error(self, status_code, **kwargs):
                 self.set_header("Content-Type", "text/plain")
@@ -481,7 +496,7 @@ class ErrorResponseTest(AsyncHTTPTestCase, LogTrapTestCase):
                 if self.get_argument("status", None):
                     self.send_error(int(self.get_argument("status")))
                 else:
-                    1/0
+                    1 / 0
 
             def get_error_html(self, status_code, **kwargs):
                 self.set_header("Content-Type", "text/plain")
@@ -492,12 +507,11 @@ class ErrorResponseTest(AsyncHTTPTestCase, LogTrapTestCase):
 
         class FailedWriteErrorHandler(RequestHandler):
             def get(self):
-                1/0
+                1 / 0
 
             def write_error(self, status_code, **kwargs):
                 raise Exception("exception in write_error")
 
-
         return Application([
                 url("/default", DefaultHandler),
                 url("/write_error", WriteErrorHandler),
@@ -537,6 +551,7 @@ class ErrorResponseTest(AsyncHTTPTestCase, LogTrapTestCase):
         self.assertEqual(response.code, 500)
         self.assertEqual(b(""), response.body)
 
+
 class StaticFileTest(AsyncHTTPTestCase, LogTrapTestCase):
     def get_app(self):
         class StaticUrlHandler(RequestHandler):
@@ -545,6 +560,7 @@ class StaticFileTest(AsyncHTTPTestCase, LogTrapTestCase):
 
         class AbsoluteStaticUrlHandler(RequestHandler):
             include_host = True
+
             def get(self, path):
                 self.write(self.static_url(path))
 
@@ -599,6 +615,7 @@ class StaticFileTest(AsyncHTTPTestCase, LogTrapTestCase):
         response = self.fetch(path % int(include_host))
         self.assertEqual(response.body, utf8(str(True)))
 
+
 class CustomStaticFileTest(AsyncHTTPTestCase, LogTrapTestCase):
     def get_app(self):
         class MyStaticFileHandler(StaticFileHandler):
index 185b4683ca8110c9f4eb84db48726ed686bc7da7..66996c0658dc06e359d8f0fa93c97dc7cd5040d9 100644 (file)
@@ -6,6 +6,7 @@ from tornado.util import b
 from tornado.web import RequestHandler
 from tornado.wsgi import WSGIApplication, WSGIContainer
 
+
 class WSGIContainerTest(AsyncHTTPTestCase, LogTrapTestCase):
     def wsgi_app(self, environ, start_response):
         status = "200 OK"
@@ -20,6 +21,7 @@ class WSGIContainerTest(AsyncHTTPTestCase, LogTrapTestCase):
         response = self.fetch("/")
         self.assertEqual(response.body, b("Hello world!"))
 
+
 class WSGIApplicationTest(AsyncHTTPTestCase, LogTrapTestCase):
     def get_app(self):
         class HelloHandler(RequestHandler):
@@ -52,6 +54,7 @@ class WSGIApplicationTest(AsyncHTTPTestCase, LogTrapTestCase):
 # repeated disassembly and reassembly.
 from tornado.test.httpserver_test import HTTPConnectionTest
 
+
 class WSGIConnectionTest(HTTPConnectionTest):
     def get_app(self):
         return WSGIContainer(validator(WSGIApplication(self.get_handlers())))
index f607de8431997e6d0299cdfd1f94cb40e10ab2bb..87448a4d0a1d31b5fdb4dcdbdffeee8b3aa1ccd8 100644 (file)
@@ -40,6 +40,8 @@ import time
 import unittest
 
 _next_port = 10000
+
+
 def get_unused_port():
     """Returns a (hopefully) unused port number."""
     global _next_port
@@ -47,6 +49,7 @@ def get_unused_port():
     _next_port = _next_port + 1
     return port
 
+
 class AsyncTestCase(unittest.TestCase):
     """TestCase subclass for testing IOLoop-based asynchronous code.
 
@@ -269,6 +272,7 @@ class AsyncHTTPTestCase(AsyncTestCase):
         self.http_client.close()
         super(AsyncHTTPTestCase, self).tearDown()
 
+
 class LogTrapTestCase(unittest.TestCase):
     """A test case that captures and discards all logging output
     if the test passes.
@@ -308,6 +312,7 @@ class LogTrapTestCase(unittest.TestCase):
         finally:
             handler.stream = old_stream
 
+
 def main():
     """A simple test runner.
 
index b08bd9ad6b2458ce84e2ffa943b8cae85b866643..23ffda1fadd87f1cf8de2baaff3fdc2dc6e68a75 100644 (file)
@@ -2,6 +2,7 @@
 
 from __future__ import absolute_import, division, with_statement
 
+
 class ObjectDict(dict):
     """Makes a dictionary behave like an object."""
     def __getattr__(self, name):
@@ -44,6 +45,7 @@ else:
         return s
     bytes_type = str
 
+
 def doctests():
     import doctest
     return doctest.DocTestSuite()
index aa42855f61b9a912f7facf0ac21bc28004e1c108..a616eb2c953bb218c7c242a12a2ec992adfcdd6e 100644 (file)
@@ -90,6 +90,7 @@ try:
 except ImportError:
     from cStringIO import StringIO as BytesIO  # python 2
 
+
 class RequestHandler(object):
     """Subclass this class and define get() or post() to make a handler.
 
@@ -208,7 +209,7 @@ class RequestHandler(object):
         """Resets all headers and content for this response."""
         # The performance cost of tornado.httputil.HTTPHeaders is significant
         # (slowing down a benchmark with a trivial handler by more than 10%),
-        # and its case-normalization is not generally necessary for 
+        # and its case-normalization is not generally necessary for
         # headers we generate on the server side, so use a plain dict
         # and list instead.
         self._headers = {
@@ -279,8 +280,8 @@ class RequestHandler(object):
             raise ValueError("Unsafe header value %r", value)
         return value
 
-
     _ARG_DEFAULT = []
+
     def get_argument(self, name, default=_ARG_DEFAULT, strip=True):
         """Returns the value of the argument with the given name.
 
@@ -375,7 +376,8 @@ class RequestHandler(object):
         if path:
             new_cookie[name]["path"] = path
         for k, v in kwargs.iteritems():
-            if k == 'max_age': k = 'max-age'
+            if k == 'max_age':
+                k = 'max-age'
             new_cookie[name][k] = v
 
     def clear_cookie(self, name, path="/", domain=None):
@@ -419,7 +421,8 @@ class RequestHandler(object):
     def get_secure_cookie(self, name, value=None, max_age_days=31):
         """Returns the given signed cookie if it validates, or None."""
         self.require_setting("cookie_secret", "secure cookies")
-        if value is None: value = self.get_cookie(name)
+        if value is None:
+            value = self.get_cookie(name)
         return decode_signed_value(self.application.settings["cookie_secret"],
                                    name, value, max_age_days=max_age_days)
 
@@ -482,7 +485,8 @@ class RequestHandler(object):
         html_bodies = []
         for module in getattr(self, "_active_modules", {}).itervalues():
             embed_part = module.embedded_javascript()
-            if embed_part: js_embed.append(utf8(embed_part))
+            if embed_part:
+                js_embed.append(utf8(embed_part))
             file_part = module.javascript_files()
             if file_part:
                 if isinstance(file_part, (unicode, bytes_type)):
@@ -490,7 +494,8 @@ class RequestHandler(object):
                 else:
                     js_files.extend(file_part)
             embed_part = module.embedded_css()
-            if embed_part: css_embed.append(utf8(embed_part))
+            if embed_part:
+                css_embed.append(utf8(embed_part))
             file_part = module.css_files()
             if file_part:
                 if isinstance(file_part, (unicode, bytes_type)):
@@ -498,9 +503,12 @@ class RequestHandler(object):
                 else:
                     css_files.extend(file_part)
             head_part = module.html_head()
-            if head_part: html_heads.append(utf8(head_part))
+            if head_part:
+                html_heads.append(utf8(head_part))
             body_part = module.html_body()
-            if body_part: html_bodies.append(utf8(body_part))
+            if body_part:
+                html_bodies.append(utf8(body_part))
+
         def is_absolute(path):
             return any(path.startswith(x) for x in ["/", "http:", "https:"])
         if js_files:
@@ -596,10 +604,9 @@ class RequestHandler(object):
             kwargs["autoescape"] = settings["autoescape"]
         return template.Loader(template_path, **kwargs)
 
-
     def flush(self, include_footers=False, callback=None):
         """Flushes the current output buffer to the network.
-        
+
         The ``callback`` argument, if given, can be used for flow control:
         it will be run when all flushed data has been written to the socket.
         Note that only one flush callback can be outstanding at a time;
@@ -624,7 +631,8 @@ class RequestHandler(object):
 
         # Ignore the chunk and only write the headers for HEAD requests
         if self.request.method == "HEAD":
-            if headers: self.request.write(headers, callback=callback)
+            if headers:
+                self.request.write(headers, callback=callback)
             return
 
         if headers or chunk:
@@ -637,7 +645,8 @@ class RequestHandler(object):
                                "by using async operations without the "
                                "@asynchronous decorator.")
 
-        if chunk is not None: self.write(chunk)
+        if chunk is not None:
+            self.write(chunk)
 
         # Automatically support ETags and add the Content-Length header if
         # we have not flushed any content yet.
@@ -733,7 +742,7 @@ class RequestHandler(object):
                 self.write(line)
             self.finish()
         else:
-            self.finish("<html><title>%(code)d: %(message)s</title>" 
+            self.finish("<html><title>%(code)d: %(message)s</title>"
                         "<body>%(code)d: %(message)s</body></html>" % {
                     "code": status_code,
                     "message": httplib.responses[status_code],
@@ -926,6 +935,7 @@ class RequestHandler(object):
             return None
         if args or kwargs:
             callback = functools.partial(callback, *args, **kwargs)
+
         def wrapper(*args, **kwargs):
             try:
                 return callback(*args, **kwargs)
@@ -984,7 +994,7 @@ class RequestHandler(object):
             if not self._finished:
                 args = [self.decode_argument(arg) for arg in args]
                 kwargs = dict((k, self.decode_argument(v, name=k))
-                              for (k,v) in kwargs.iteritems())
+                              for (k, v) in kwargs.iteritems())
                 getattr(self, self.request.method.lower())(*args, **kwargs)
                 if self._auto_finish and not self._finished:
                     self.finish()
@@ -995,7 +1005,7 @@ class RequestHandler(object):
         lines = [utf8(self.request.version + " " +
                       str(self._status_code) +
                       " " + httplib.responses[self._status_code])]
-        lines.extend([(utf8(n) + b(": ") + utf8(v)) for n, v in 
+        lines.extend([(utf8(n) + b(": ") + utf8(v)) for n, v in
                       itertools.chain(self._headers.iteritems(), self._list_headers)])
         for cookie_dict in getattr(self, "_new_cookies", []):
             for cookie in cookie_dict.values():
@@ -1088,7 +1098,8 @@ def removeslash(method):
             if self.request.method in ("GET", "HEAD"):
                 uri = self.request.path.rstrip("/")
                 if uri:  # don't try to redirect '/' to ''
-                    if self.request.query: uri += "?" + self.request.query
+                    if self.request.query:
+                        uri += "?" + self.request.query
                     self.redirect(uri)
                     return
             else:
@@ -1109,7 +1120,8 @@ def addslash(method):
         if not self.request.path.endswith("/"):
             if self.request.method in ("GET", "HEAD"):
                 uri = self.request.path + "/"
-                if self.request.query: uri += "?" + self.request.query
+                if self.request.query:
+                    uri += "?" + self.request.query
                 self.redirect(uri)
                 return
             raise HTTPError(404)
@@ -1200,7 +1212,8 @@ class Application(object):
                             r"/(favicon\.ico)", r"/(robots\.txt)"]:
                 handlers.insert(0, (pattern, static_handler_class,
                                     static_handler_args))
-        if handlers: self.add_handlers(".*$", handlers)
+        if handlers:
+            self.add_handlers(".*$", handlers)
 
         # Automatically reload modified modules
         if self.settings.get("debug") and not wsgi:
@@ -1292,7 +1305,8 @@ class Application(object):
             self._load_ui_methods(dict((n, getattr(methods, n))
                                        for n in dir(methods)))
         elif isinstance(methods, list):
-            for m in methods: self._load_ui_methods(m)
+            for m in methods:
+                self._load_ui_methods(m)
         else:
             for name, fn in methods.iteritems():
                 if not name.startswith("_") and hasattr(fn, "__call__") \
@@ -1304,7 +1318,8 @@ class Application(object):
             self._load_ui_modules(dict((n, getattr(modules, n))
                                        for n in dir(modules)))
         elif isinstance(modules, list):
-            for m in modules: self._load_ui_modules(m)
+            for m in modules:
+                self._load_ui_modules(m)
         else:
             assert isinstance(modules, dict)
             for name, cls in modules.iteritems():
@@ -1333,7 +1348,8 @@ class Application(object):
                         # None-safe wrapper around url_unescape to handle
                         # unmatched optional groups correctly
                         def unquote(s):
-                            if s is None: return s
+                            if s is None:
+                                return s
                             return escape.url_unescape(s, encoding=None)
                         # Pass matched groups to the handler.  Since
                         # match.groups() includes both named and unnamed groups,
@@ -1393,7 +1409,6 @@ class Application(object):
                    handler._request_summary(), request_time)
 
 
-
 class HTTPError(Exception):
     """An exception that will turn into an HTTP error response."""
     def __init__(self, status_code, log_message=None, *args):
@@ -1455,7 +1470,7 @@ class StaticFileHandler(RequestHandler):
     /static/images/myimage.png?v=xxx. Override ``get_cache_time`` method for
     more fine-grained cache control.
     """
-    CACHE_MAX_AGE = 86400*365*10 #10 years
+    CACHE_MAX_AGE = 86400 * 365 * 10  # 10 years
 
     _static_hashes = {}
     _lock = threading.Lock()  # protects _static_hashes
@@ -1554,7 +1569,7 @@ class StaticFileHandler(RequestHandler):
 
         This method may be overridden in subclasses (but note that it is
         a class method rather than an instance method).
-        
+
         ``settings`` is the `Application.settings` dictionary.  ``path``
         is the static path being requested.  The url returned should be
         relative to the current host.
@@ -1652,7 +1667,7 @@ class GZipContentEncoding(OutputTransform):
     See http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.11
     """
     CONTENT_TYPES = set([
-        "text/plain", "text/html", "text/css", "text/xml", "application/javascript", 
+        "text/plain", "text/html", "text/css", "text/xml", "application/javascript",
         "application/x-javascript", "application/xml", "application/atom+xml",
         "text/javascript", "application/json", "application/xhtml+xml"])
     MIN_LENGTH = 5
@@ -1786,14 +1801,17 @@ class UIModule(object):
         """Renders a template and returns it as a string."""
         return self.handler.render_string(path, **kwargs)
 
+
 class _linkify(UIModule):
     def render(self, text, **kwargs):
         return escape.linkify(text, **kwargs)
 
+
 class _xsrf_form_html(UIModule):
     def render(self):
         return self.handler.xsrf_form_html()
 
+
 class TemplateModule(UIModule):
     """UIModule that simply renders the given template.
 
@@ -1806,7 +1824,7 @@ class TemplateModule(UIModule):
     inside the template and give it keyword arguments corresponding to
     the methods on UIModule: {{ set_resources(js_files=static_url("my.js")) }}
     Note that these resources are output once per template file, not once
-    per instantiation of the template, so they must not depend on 
+    per instantiation of the template, so they must not depend on
     any arguments to the template.
     """
     def __init__(self, handler):
@@ -1862,7 +1880,6 @@ class TemplateModule(UIModule):
         return "".join(self._get_resources("html_body"))
 
 
-
 class URLSpec(object):
     """Specifies mappings between URLs and handlers."""
     def __init__(self, pattern, handler_class, kwargs={}, name=None):
@@ -1938,13 +1955,14 @@ def _time_independent_equals(a, b):
         return False
     result = 0
     if type(a[0]) is int:  # python3 byte strings
-        for x, y in zip(a,b):
+        for x, y in zip(a, b):
             result |= x ^ y
     else:  # python2
         for x, y in zip(a, b):
             result |= ord(x) ^ ord(y)
     return result == 0
 
+
 def create_signed_value(secret, name, value):
     timestamp = utf8(str(int(time.time())))
     value = base64.b64encode(utf8(value))
@@ -1952,10 +1970,13 @@ def create_signed_value(secret, name, value):
     value = b("|").join([value, timestamp, signature])
     return value
 
+
 def decode_signed_value(secret, name, value, max_age_days=31):
-    if not value: return None
+    if not value:
+        return None
     parts = utf8(value).split(b("|"))
-    if len(parts) != 3: return None
+    if len(parts) != 3:
+        return None
     signature = _create_signature(secret, name, parts[0], parts[1])
     if not _time_independent_equals(parts[2], signature):
         logging.warning("Invalid cookie signature %r", value)
@@ -1979,7 +2000,9 @@ def decode_signed_value(secret, name, value, max_age_days=31):
     except Exception:
         return None
 
+
 def _create_signature(secret, *parts):
     hash = hmac.new(utf8(secret), digestmod=hashlib.sha1)
-    for part in parts: hash.update(utf8(part))
+    for part in parts:
+        hash.update(utf8(part))
     return utf8(hash.hexdigest())
index 05ba286d7fba95e601f10726aed99a6b26cbb5b5..266b114b0306676cbe81d55556e455e6eeb3b119 100644 (file)
@@ -32,6 +32,7 @@ import tornado.web
 
 from tornado.util import bytes_type, b
 
+
 class WebSocketHandler(tornado.web.RequestHandler):
     """Subclass this class to create a basic WebSocket handler.
 
@@ -204,7 +205,7 @@ class WebSocketHandler(tornado.web.RequestHandler):
         may wish to override this if they are using an SSL proxy
         that does not provide the X-Scheme header as understood
         by HTTPServer.
-        
+
         Note that this is only used by the draft76 protocol.
         """
         return "wss" if self.request.protocol == "https" else "ws"
@@ -251,6 +252,7 @@ class WebSocketProtocol(object):
         """
         if args or kwargs:
             callback = functools.partial(callback, *args, **kwargs)
+
         def wrapper(*args, **kwargs):
             try:
                 return callback(*args, **kwargs)
@@ -473,7 +475,7 @@ class WebSocketProtocol13(WebSocketProtocol):
         sha1 = hashlib.sha1()
         sha1.update(tornado.escape.utf8(
                 self.request.headers.get("Sec-Websocket-Key")))
-        sha1.update(b("258EAFA5-E914-47DA-95CA-C5AB0DC85B11")) # Magic value
+        sha1.update(b("258EAFA5-E914-47DA-95CA-C5AB0DC85B11"))  # Magic value
         return tornado.escape.native_str(base64.b64encode(sha1.digest()))
 
     def _accept_connection(self):
@@ -554,12 +556,12 @@ class WebSocketProtocol13(WebSocketProtocol):
             self.stream.read_bytes(8, self._on_frame_length_64)
 
     def _on_frame_length_16(self, data):
-        self._frame_length = struct.unpack("!H", data)[0];
-        self.stream.read_bytes(4, self._on_masking_key);
+        self._frame_length = struct.unpack("!H", data)[0]
+        self.stream.read_bytes(4, self._on_masking_key)
 
     def _on_frame_length_64(self, data):
-        self._frame_length = struct.unpack("!Q", data)[0];
-        self.stream.read_bytes(4, self._on_masking_key);
+        self._frame_length = struct.unpack("!Q", data)[0]
+        self.stream.read_bytes(4, self._on_masking_key)
 
     def _on_masking_key(self, data):
         self._frame_mask = array.array("B", data)
@@ -606,9 +608,9 @@ class WebSocketProtocol13(WebSocketProtocol):
         if not self.client_terminated:
             self._receive_frame()
 
-
     def _handle_message(self, opcode, data):
-        if self.client_terminated: return
+        if self.client_terminated:
+            return
 
         if opcode == 0x1:
             # UTF-8 data
index 783b6b2e9fd650478a57a433bf1f7a77b306b933..7e793e9c2779f848db822e8e96a939e4cc091e80 100644 (file)
@@ -20,7 +20,7 @@ WSGI is the Python standard for web servers, and allows for interoperability
 between Tornado and other Python web frameworks and servers.  This module
 provides WSGI support in two ways:
 
-* `WSGIApplication` is a version of `tornado.web.Application` that can run 
+* `WSGIApplication` is a version of `tornado.web.Application` that can run
   inside a WSGI server.  This is useful for running a Tornado app on another
   HTTP server, such as Google App Engine.  See the `WSGIApplication` class
   documentation for limitations that apply.
@@ -51,6 +51,7 @@ try:
 except ImportError:
     from cStringIO import StringIO as BytesIO  # python 2
 
+
 class WSGIApplication(web.Application):
     """A WSGI equivalent of `tornado.web.Application`.
 
@@ -83,7 +84,7 @@ class WSGIApplication(web.Application):
     Since no asynchronous methods are available for WSGI applications, the
     httpclient and auth modules are both not available for WSGI applications.
     We support the same interface, but handlers running in a WSGIApplication
-    do not support flush() or asynchronous methods. 
+    do not support flush() or asynchronous methods.
     """
     def __init__(self, handlers=None, default_host="", **settings):
         web.Application.__init__(self, handlers, default_host, transforms=[],
@@ -99,7 +100,7 @@ class WSGIApplication(web.Application):
             for cookie in cookie_dict.values():
                 headers.append(("Set-Cookie", cookie.OutputString(None)))
         start_response(status,
-                       [(native_str(k), native_str(v)) for (k,v) in headers])
+                       [(native_str(k), native_str(v)) for (k, v) in headers])
         return handler._write_buffer
 
 
@@ -118,7 +119,8 @@ class HTTPRequest(object):
             arguments = cgi.parse_qs(self.query)
             for name, values in arguments.iteritems():
                 values = [v for v in values if v]
-                if values: self.arguments[name] = values
+                if values:
+                    self.arguments[name] = values
         self.version = "HTTP/1.1"
         self.headers = httputil.HTTPHeaders()
         if environ.get("CONTENT_TYPE"):
@@ -148,7 +150,7 @@ class HTTPRequest(object):
                 self.arguments.setdefault(name, []).extend(values)
         elif content_type.startswith("multipart/form-data"):
             if 'boundary=' in content_type:
-                boundary = content_type.split('boundary=',1)[1]
+                boundary = content_type.split('boundary=', 1)[1]
                 if boundary:
                     httputil.parse_multipart_form_data(
                         utf8(boundary), self.body, self.arguments, self.files)
@@ -217,6 +219,7 @@ class WSGIContainer(object):
     def __call__(self, request):
         data = {}
         response = []
+
         def start_response(status, response_headers, exc_info=None):
             data["status"] = status
             data["headers"] = response_headers
@@ -227,11 +230,12 @@ class WSGIContainer(object):
         body = b("").join(response)
         if hasattr(app_response, "close"):
             app_response.close()
-        if not data: raise Exception("WSGI app did not call start_response")
+        if not data:
+            raise Exception("WSGI app did not call start_response")
 
         status_code = int(data["status"].split()[0])
         headers = data["headers"]
-        header_set = set(k.lower() for (k,v) in headers)
+        header_set = set(k.lower() for (k, v) in headers)
         body = escape.utf8(body)
         if "content-length" not in header_set:
             headers.append(("Content-Length", str(len(body))))