From: Ben Darnell Date: Sun, 8 Feb 2015 20:02:49 +0000 (-0500) Subject: More flake8 cleanup. X-Git-Tag: v4.2.0b1~126 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e6da0c010a98034a10b3cd3c0c0c67562653692f;p=thirdparty%2Ftornado.git More flake8 cleanup. The only remaining flake8 errors are for line length. --- diff --git a/tornado/auth.py b/tornado/auth.py index f28b1ee25..00c833aa2 100644 --- a/tornado/auth.py +++ b/tornado/auth.py @@ -123,6 +123,7 @@ def _auth_return_future(f): if callback is not None: future.add_done_callback( functools.partial(_auth_future_to_callback, callback)) + def handle_exception(typ, value, tb): if future.done(): return False diff --git a/tornado/curl_httpclient.py b/tornado/curl_httpclient.py index 87d2ba488..bced9499e 100644 --- a/tornado/curl_httpclient.py +++ b/tornado/curl_httpclient.py @@ -289,8 +289,8 @@ class CurlAsyncHTTPClient(AsyncHTTPClient): functools.partial(self._curl_header_callback, headers, request.header_callback)) if request.streaming_callback: - write_function = lambda chunk: self.io_loop.add_callback( - request.streaming_callback, chunk) + def write_function(chunk): + self.io_loop.add_callback(request.streaming_callback, chunk) else: write_function = buffer.write if bytes is str: # py2 @@ -382,6 +382,7 @@ class CurlAsyncHTTPClient(AsyncHTTPClient): % request.method) request_buffer = BytesIO(utf8(request.body)) + def ioctl(cmd): if cmd == curl.IOCMD_RESTARTREAD: request_buffer.seek(0) diff --git a/tornado/gen.py b/tornado/gen.py index 5c40c6ed6..f59f0d893 100644 --- a/tornado/gen.py +++ b/tornado/gen.py @@ -124,9 +124,11 @@ def engine(func): which use ``self.finish()`` in place of a callback argument. """ func = _make_coroutine_wrapper(func, replace_callback=False) + @functools.wraps(func) def wrapper(*args, **kwargs): future = func(*args, **kwargs) + def final_callback(future): if future.result() is not None: raise ReturnValueIgnoredError( @@ -479,11 +481,13 @@ def Task(func, *args, **kwargs): yielded. """ future = Future() + def handle_exception(typ, value, tb): if future.done(): return False future.set_exc_info((typ, value, tb)) return True + def set_result(result): if future.done(): return @@ -600,6 +604,7 @@ def multi_future(children): future = Future() if not children: future.set_result({} if keys is not None else []) + def callback(f): unfinished_children.remove(f) if not unfinished_children: @@ -665,6 +670,7 @@ def with_timeout(timeout, future, io_loop=None, quiet_exceptions=()): chain_future(future, result) if io_loop is None: io_loop = IOLoop.current() + def error_callback(future): try: future.result() @@ -672,6 +678,7 @@ def with_timeout(timeout, future, io_loop=None, quiet_exceptions=()): if not isinstance(e, quiet_exceptions): app_log.error("Exception in Future %r after timeout", future, exc_info=True) + def timeout_callback(): result.set_exception(TimeoutError("Timeout")) # In case the wrapped future goes on to fail, log it. @@ -857,6 +864,7 @@ class Runner(object): # YieldPoints are too closely coupled to the Runner to go # through the generic convert_yielded mechanism. self.future = TracebackFuture() + def start_yield_point(): try: yielded.start(self) @@ -875,6 +883,7 @@ class Runner(object): with stack_context.ExceptionStackContext( self.handle_exception) as deactivate: self.stack_context_deactivate = deactivate + def cb(): start_yield_point() self.run() diff --git a/tornado/iostream.py b/tornado/iostream.py index 1bd33b42a..371a02d3e 100644 --- a/tornado/iostream.py +++ b/tornado/iostream.py @@ -1104,6 +1104,7 @@ class IOStream(BaseIOStream): # If we had an "unwrap" counterpart to this method we would need # to restore the original callback after our Future resolves # so that repeated wrap/unwrap calls don't build up layers. + def close_callback(): if not future.done(): future.set_exception(ssl_stream.error or StreamClosedError()) diff --git a/tornado/options.py b/tornado/options.py index c855407c2..89a9e4326 100644 --- a/tornado/options.py +++ b/tornado/options.py @@ -256,7 +256,7 @@ class OptionParser(object): arg = args[i].lstrip("-") name, equals, value = arg.partition("=") name = name.replace('-', '_') - if not name in self._options: + if name not in self._options: self.print_help() raise Error('Unrecognized command line option: %r' % name) option = self._options[name] diff --git a/tornado/platform/auto.py b/tornado/platform/auto.py index ddfe06b4a..d3eeb56b2 100644 --- a/tornado/platform/auto.py +++ b/tornado/platform/auto.py @@ -32,6 +32,7 @@ if os.name == 'nt': from tornado.platform.windows import set_close_exec elif 'APPENGINE_RUNTIME' in os.environ: from tornado.platform.common import Waker + def set_close_exec(fd): pass else: @@ -41,9 +42,13 @@ try: # monotime monkey-patches the time module to have a monotonic function # in versions of python before 3.3. import monotime + # Silence pyflakes warning about this unused import + monotime except ImportError: pass try: from time import monotonic as monotonic_time except ImportError: monotonic_time = None + +__all__ = ['Waker', 'set_close_exec', 'monotonic_time'] diff --git a/tornado/platform/twisted.py b/tornado/platform/twisted.py index 09b328366..3d7eba422 100644 --- a/tornado/platform/twisted.py +++ b/tornado/platform/twisted.py @@ -572,6 +572,7 @@ if hasattr(gen.convert_yielded, 'register'): @gen.convert_yielded.register(Deferred) def _(d): f = Future() + def errback(failure): try: failure.raiseException() diff --git a/tornado/test/curl_httpclient_test.py b/tornado/test/curl_httpclient_test.py index 8d7065dfe..3ac21f4d7 100644 --- a/tornado/test/curl_httpclient_test.py +++ b/tornado/test/curl_httpclient_test.py @@ -8,7 +8,7 @@ from tornado.stack_context import ExceptionStackContext from tornado.testing import AsyncHTTPTestCase from tornado.test import httpclient_test from tornado.test.util import unittest -from tornado.web import Application, RequestHandler, URLSpec +from tornado.web import Application, RequestHandler try: diff --git a/tornado/test/gen_test.py b/tornado/test/gen_test.py index a5d78b5a9..31acadd8b 100644 --- a/tornado/test/gen_test.py +++ b/tornado/test/gen_test.py @@ -816,6 +816,7 @@ class GenCoroutineTest(AsyncTestCase): @gen_test def test_moment(self): calls = [] + @gen.coroutine def f(name, yieldable): for i in range(5): diff --git a/tornado/test/gettext_translations/extract_me.py b/tornado/test/gettext_translations/extract_me.py index 75406ecc7..db70fb937 100644 --- a/tornado/test/gettext_translations/extract_me.py +++ b/tornado/test/gettext_translations/extract_me.py @@ -1,3 +1,4 @@ +# flake8: noqa # Dummy source file to allow creation of the initial .po file in the # same way as a real project. I'm not entirely sure about the real # workflow here, but this seems to work. diff --git a/tornado/test/httpserver_test.py b/tornado/test/httpserver_test.py index 64ef96d45..62ef6ca3d 100644 --- a/tornado/test/httpserver_test.py +++ b/tornado/test/httpserver_test.py @@ -32,6 +32,7 @@ def read_stream_body(stream, callback): """Reads an HTTP response from `stream` and runs callback with its headers and body.""" chunks = [] + class Delegate(HTTPMessageDelegate): def headers_received(self, start_line, headers): self.headers = headers @@ -589,6 +590,7 @@ class KeepAliveTest(AsyncHTTPTestCase): class HelloHandler(RequestHandler): def get(self): self.finish('Hello world') + def post(self): self.finish('Hello world') @@ -863,6 +865,7 @@ class StreamingChunkSizeTest(AsyncHTTPTestCase): def test_chunked_compressed(self): compressed = self.compress(self.BODY) self.assertGreater(len(compressed), 20) + def body_producer(write): write(compressed[:20]) write(compressed[20:]) diff --git a/tornado/test/import_test.py b/tornado/test/import_test.py index de7cc0b9f..1be6427f1 100644 --- a/tornado/test/import_test.py +++ b/tornado/test/import_test.py @@ -1,3 +1,4 @@ +# flake8: noqa from __future__ import absolute_import, division, print_function, with_statement from tornado.test.util import unittest diff --git a/tornado/test/ioloop_test.py b/tornado/test/ioloop_test.py index 15f8a2cdb..0bad57588 100644 --- a/tornado/test/ioloop_test.py +++ b/tornado/test/ioloop_test.py @@ -180,10 +180,12 @@ class TestIOLoop(AsyncTestCase): # t2 should be cancelled by t1, even though it is already scheduled to # be run before the ioloop even looks at it. now = self.io_loop.time() + def t1(): calls[0] = True self.io_loop.remove_timeout(t2_handle) self.io_loop.add_timeout(now + 0.01, t1) + def t2(): calls[1] = True t2_handle = self.io_loop.add_timeout(now + 0.02, t2) @@ -252,6 +254,7 @@ class TestIOLoop(AsyncTestCase): """The handler callback receives the same fd object it passed in.""" server_sock, port = bind_unused_port() fds = [] + def handle_connection(fd, events): fds.append(fd) conn, addr = server_sock.accept() @@ -274,6 +277,7 @@ class TestIOLoop(AsyncTestCase): def test_mixed_fd_fileobj(self): server_sock, port = bind_unused_port() + def f(fd, events): pass self.io_loop.add_handler(server_sock, f, IOLoop.READ) @@ -288,6 +292,7 @@ class TestIOLoop(AsyncTestCase): """Calling start() twice should raise an error, not deadlock.""" returned_from_start = [False] got_exception = [False] + def callback(): try: self.io_loop.start() @@ -344,6 +349,7 @@ class TestIOLoop(AsyncTestCase): # After reading from one fd, remove the other from the IOLoop. chunks = [] + def handle_read(fd, events): chunks.append(fd.recv(1024)) if fd is client: diff --git a/tornado/test/iostream_test.py b/tornado/test/iostream_test.py index 9d0bd4f69..81ce4cac2 100644 --- a/tornado/test/iostream_test.py +++ b/tornado/test/iostream_test.py @@ -310,6 +310,7 @@ class TestIOStreamMixin(object): def streaming_callback(data): chunks.append(data) self.stop() + def close_callback(data): assert not data, data closed[0] = True @@ -355,6 +356,7 @@ class TestIOStreamMixin(object): def test_future_delayed_close_callback(self): # Same as test_delayed_close_callback, but with the future interface. server, client = self.make_iostream_pair() + # We can't call make_iostream_pair inside a gen_test function # because the ioloop is not reentrant. @gen_test @@ -534,6 +536,7 @@ class TestIOStreamMixin(object): # and IOStream._maybe_add_error_listener. server, client = self.make_iostream_pair() closed = [False] + def close_callback(): closed[0] = True self.stop() diff --git a/tornado/test/twisted_test.py b/tornado/test/twisted_test.py index 315b3b758..cc445247c 100644 --- a/tornado/test/twisted_test.py +++ b/tornado/test/twisted_test.py @@ -453,6 +453,7 @@ class CompatibilityTests(unittest.TestCase): def twisted_coroutine_fetch(self, url, runner): body = [None] + @gen.coroutine def f(): # This is simpler than the non-coroutine version, but it cheats @@ -553,7 +554,7 @@ if have_twisted: 'test_changeUID', ], # Process tests appear to work on OSX 10.7, but not 10.6 - #'twisted.internet.test.test_process.PTYProcessTestsBuilder': [ + # 'twisted.internet.test.test_process.PTYProcessTestsBuilder': [ # 'test_systemCallUninterruptedByChildExit', # ], 'twisted.internet.test.test_tcp.TCPClientTestsBuilder': [ @@ -572,7 +573,7 @@ if have_twisted: 'twisted.internet.test.test_threads.ThreadTestsBuilder': [], 'twisted.internet.test.test_time.TimeTestsBuilder': [], # Extra third-party dependencies (pyOpenSSL) - #'twisted.internet.test.test_tls.SSLClientTestsMixin': [], + # 'twisted.internet.test.test_tls.SSLClientTestsMixin': [], 'twisted.internet.test.test_udp.UDPServerTestsBuilder': [], 'twisted.internet.test.test_unix.UNIXTestsBuilder': [ # Platform-specific. These tests would be skipped automatically diff --git a/tornado/test/web_test.py b/tornado/test/web_test.py index 3c470428b..a643435c6 100644 --- a/tornado/test/web_test.py +++ b/tornado/test/web_test.py @@ -1549,6 +1549,7 @@ class UIMethodUIModuleTest(SimpleHandlerTestCase): def my_ui_method(handler, x): return "In my_ui_method(%s) with handler value %s." % ( x, handler.value()) + class MyModule(UIModule): def render(self, x): return "In MyModule(%s) with handler value %s." % ( @@ -2167,6 +2168,7 @@ class SignedValueTest(unittest.TestCase): def test_payload_tampering(self): # These cookies are variants of the one in test_known_values. sig = "3d4e60b996ff9c5d5788e333a0cba6f238a22c6c0f94788870e1a9ecd482e152" + def validate(prefix): return (b'value' == decode_signed_value(SignedValueTest.SECRET, "key", @@ -2181,6 +2183,7 @@ class SignedValueTest(unittest.TestCase): def test_signature_tampering(self): prefix = "2|1:0|10:1300000000|3:key|8:dmFsdWU=|" + def validate(sig): return (b'value' == decode_signed_value(SignedValueTest.SECRET, "key", diff --git a/tornado/test/websocket_test.py b/tornado/test/websocket_test.py index 42599382d..6b182d076 100644 --- a/tornado/test/websocket_test.py +++ b/tornado/test/websocket_test.py @@ -12,7 +12,7 @@ from tornado.web import Application, RequestHandler from tornado.util import u try: - import tornado.websocket + import tornado.websocket # noqa from tornado.util import _websocket_mask_python except ImportError: # The unittest module presents misleading errors on ImportError diff --git a/tornado/util.py b/tornado/util.py index bc66beaeb..d943ce2ba 100644 --- a/tornado/util.py +++ b/tornado/util.py @@ -83,7 +83,7 @@ class GzipDecompressor(object): # unicode_literals" have other problems (see PEP 414). u() can be applied # to ascii strings that include \u escapes (but they must not contain # literal non-ascii characters). -if type('') is not type(b''): +if not isinstance(b'', type('')): def u(s): return s unicode_type = str @@ -91,8 +91,10 @@ if type('') is not type(b''): else: def u(s): return s.decode('unicode_escape') - unicode_type = unicode - basestring_type = basestring + # These names don't exist in py3, so use noqa comments to disable + # warnings in flake8. + unicode_type = unicode # noqa + basestring_type = basestring # noqa def import_object(name): diff --git a/tornado/web.py b/tornado/web.py index f6cb99429..c1614db71 100644 --- a/tornado/web.py +++ b/tornado/web.py @@ -1487,6 +1487,7 @@ def asynchronous(method): """ # Delay the IOLoop import because it's not available on app engine. from tornado.ioloop import IOLoop + @functools.wraps(method) def wrapper(self, *args, **kwargs): self._auto_finish = False