From: Ben Darnell Date: Sun, 3 Mar 2013 00:08:22 +0000 (-0500) Subject: All functions that take an IOLoop default to current() instead of instance(). X-Git-Tag: v3.0.0~78 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2ad9659f0175e6d036a074eec52409278575bfa0;p=thirdparty%2Ftornado.git All functions that take an IOLoop default to current() instead of instance(). This means among other things that it's no longer necessary to pass IOLoops explicitly in tests. --- diff --git a/tornado/autoreload.py b/tornado/autoreload.py index 4e424878f..90a8a8027 100644 --- a/tornado/autoreload.py +++ b/tornado/autoreload.py @@ -99,7 +99,7 @@ def start(io_loop=None, check_time=500): We run on the I/O loop, and restarting is a destructive operation, so will terminate any pending requests. """ - io_loop = io_loop or ioloop.IOLoop.instance() + io_loop = io_loop or ioloop.IOLoop.current() if io_loop in _io_loops: return _io_loops[io_loop] = True diff --git a/tornado/httpclient.py b/tornado/httpclient.py index c325a63bd..a9ac0e70a 100644 --- a/tornado/httpclient.py +++ b/tornado/httpclient.py @@ -131,7 +131,7 @@ class AsyncHTTPClient(Configurable): return getattr(cls, attr_name) def __new__(cls, io_loop=None, force_instance=False, **kwargs): - io_loop = io_loop or IOLoop.instance() + io_loop = io_loop or IOLoop.current() if io_loop in cls._async_clients() and not force_instance: return cls._async_clients()[io_loop] instance = super(AsyncHTTPClient, cls).__new__(cls, io_loop=io_loop, diff --git a/tornado/ioloop.py b/tornado/ioloop.py index 4fc43badd..a431d5d0c 100644 --- a/tornado/ioloop.py +++ b/tornado/ioloop.py @@ -164,7 +164,7 @@ class IOLoop(Configurable): def current(): current = getattr(IOLoop._current, "instance", None) if current is None: - raise ValueError("no current IOLoop") + return IOLoop.instance() return current def make_current(self): @@ -733,7 +733,7 @@ class PeriodicCallback(object): if callback_time <= 0: raise ValueError("Periodic callback must have a positive callback_time") self.callback_time = callback_time - self.io_loop = io_loop or IOLoop.instance() + self.io_loop = io_loop or IOLoop.current() self._running = False self._timeout = None diff --git a/tornado/iostream.py b/tornado/iostream.py index 9d9fb7156..28bc18f4a 100644 --- a/tornado/iostream.py +++ b/tornado/iostream.py @@ -66,7 +66,7 @@ class BaseIOStream(object): """ def __init__(self, io_loop=None, max_buffer_size=104857600, read_chunk_size=4096): - self.io_loop = io_loop or ioloop.IOLoop.instance() + self.io_loop = io_loop or ioloop.IOLoop.current() self.max_buffer_size = max_buffer_size self.read_chunk_size = read_chunk_size self.error = None diff --git a/tornado/netutil.py b/tornado/netutil.py index 46e4227ec..06c88ab61 100644 --- a/tornado/netutil.py +++ b/tornado/netutil.py @@ -128,7 +128,7 @@ def add_accept_handler(sock, callback, io_loop=None): ``IOLoop`` handlers. """ if io_loop is None: - io_loop = IOLoop.instance() + io_loop = IOLoop.current() def accept_handler(fd, events): while True: @@ -186,7 +186,7 @@ class Resolver(Configurable): class ExecutorResolver(Resolver): def initialize(self, io_loop=None, executor=None): - self.io_loop = io_loop or IOLoop.instance() + self.io_loop = io_loop or IOLoop.current() self.executor = executor or dummy_executor @run_on_executor diff --git a/tornado/platform/caresresolver.py b/tornado/platform/caresresolver.py index 729be3c41..7442b33c3 100644 --- a/tornado/platform/caresresolver.py +++ b/tornado/platform/caresresolver.py @@ -19,7 +19,7 @@ class CaresResolver(Resolver): may default to ``AF_UNSPEC``. """ def initialize(self, io_loop=None): - self.io_loop = io_loop or IOLoop.instance() + self.io_loop = io_loop or IOLoop.current() self.channel = pycares.Channel(sock_state_cb=self._sock_state_cb) self.fds = {} diff --git a/tornado/platform/twisted.py b/tornado/platform/twisted.py index e044701cd..a95b009fd 100644 --- a/tornado/platform/twisted.py +++ b/tornado/platform/twisted.py @@ -149,7 +149,7 @@ class TornadoReactor(PosixReactorBase): """ def __init__(self, io_loop=None): if not io_loop: - io_loop = tornado.ioloop.IOLoop.instance() + io_loop = tornado.ioloop.IOLoop.current() self._io_loop = io_loop self._readers = {} # map of reader objects to fd self._writers = {} # map of writer objects to fd @@ -357,7 +357,7 @@ class _TestReactor(TornadoReactor): def install(io_loop=None): """Install this package as the default Twisted reactor.""" if not io_loop: - io_loop = tornado.ioloop.IOLoop.instance() + io_loop = tornado.ioloop.IOLoop.current() reactor = TornadoReactor(io_loop) from twisted.internet.main import installReactor installReactor(reactor) @@ -504,7 +504,7 @@ class TwistedResolver(Resolver): Requires Twisted 12.1 or newer. """ def initialize(self, io_loop=None): - self.io_loop = io_loop or IOLoop.instance() + self.io_loop = io_loop or IOLoop.current() # partial copy of twisted.names.client.createResolver, which doesn't # allow for a reactor to be passed in. self.reactor = tornado.platform.twisted.TornadoReactor(io_loop) diff --git a/tornado/process.py b/tornado/process.py index 0509eb3a9..55481a22c 100644 --- a/tornado/process.py +++ b/tornado/process.py @@ -237,7 +237,7 @@ class Subprocess(object): if cls._initialized: return if io_loop is None: - io_loop = ioloop.IOLoop.instance() + io_loop = ioloop.IOLoop.current() cls._old_sigchld = signal.signal( signal.SIGCHLD, lambda sig, frame: io_loop.add_callback_from_signal(cls._cleanup)) diff --git a/tornado/tcpserver.py b/tornado/tcpserver.py index 52ed70b1d..41a5eceab 100644 --- a/tornado/tcpserver.py +++ b/tornado/tcpserver.py @@ -123,7 +123,7 @@ class TCPServer(object): control over the initialization of a multi-process server. """ if self.io_loop is None: - self.io_loop = IOLoop.instance() + self.io_loop = IOLoop.current() for sock in sockets: self._sockets[sock.fileno()] = sock diff --git a/tornado/test/iostream_test.py b/tornado/test/iostream_test.py index c2e9cab94..1aa85a6fa 100644 --- a/tornado/test/iostream_test.py +++ b/tornado/test/iostream_test.py @@ -468,10 +468,10 @@ class TestIOStreamWebHTTPS(TestIOStreamWebMixin, AsyncHTTPSTestCase): class TestIOStream(TestIOStreamMixin, AsyncTestCase): def _make_server_iostream(self, connection, **kwargs): - return IOStream(connection, io_loop=self.io_loop, **kwargs) + return IOStream(connection, **kwargs) def _make_client_iostream(self, connection, **kwargs): - return IOStream(connection, io_loop=self.io_loop, **kwargs) + return IOStream(connection, **kwargs) class TestIOStreamSSL(TestIOStreamMixin, AsyncTestCase): diff --git a/tornado/websocket.py b/tornado/websocket.py index 235a1102e..b47a37afe 100644 --- a/tornado/websocket.py +++ b/tornado/websocket.py @@ -795,7 +795,7 @@ class _WebSocketClientConnection(simple_httpclient._HTTPConnection): def WebSocketConnect(url, io_loop=None, callback=None): if io_loop is None: - io_loop = IOLoop.instance() + io_loop = IOLoop.current() request = simple_httpclient.HTTPRequest(url) request = simple_httpclient._RequestProxy( request, simple_httpclient.HTTPRequest._DEFAULTS)