From: Ben Darnell Date: Sat, 7 Jul 2018 04:24:59 +0000 (-0400) Subject: testing: Simplify imports for python 3 X-Git-Tag: v6.0.0b1~48^2~12 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=40c33fd75475486ef4f2c13f28f872f8e78b4270;p=thirdparty%2Ftornado.git testing: Simplify imports for python 3 --- diff --git a/tornado/testing.py b/tornado/testing.py index d6e5e9473..86cbf128b 100644 --- a/tornado/testing.py +++ b/tornado/testing.py @@ -11,27 +11,7 @@ from __future__ import absolute_import, division, print_function -try: - from tornado import gen - from tornado.httpclient import AsyncHTTPClient - from tornado.httpserver import HTTPServer - from tornado.simple_httpclient import SimpleAsyncHTTPClient - from tornado.ioloop import IOLoop, TimeoutError - from tornado import netutil - from tornado.process import Subprocess -except ImportError: - # These modules are not importable on app engine. Parts of this module - # won't work, but e.g. main() will. - AsyncHTTPClient = None # type: ignore - gen = None # type: ignore - HTTPServer = None # type: ignore - IOLoop = None # type: ignore - netutil = None # type: ignore - SimpleAsyncHTTPClient = None # type: ignore - Subprocess = None # type: ignore -from tornado.log import app_log -from tornado.stack_context import ExceptionStackContext -from tornado.util import raise_exc_info, basestring_type, PY3 +from collections.abc import Generator import functools import inspect import logging @@ -40,45 +20,21 @@ import re import signal import socket import sys - -try: - import asyncio -except ImportError: - asyncio = None - - -try: - from collections.abc import Generator as GeneratorType # type: ignore -except ImportError: - from types import GeneratorType # type: ignore - -if sys.version_info >= (3, 5): - iscoroutine = inspect.iscoroutine # type: ignore - iscoroutinefunction = inspect.iscoroutinefunction # type: ignore -else: - iscoroutine = iscoroutinefunction = lambda f: False - -# Tornado's own test suite requires the updated unittest module -# (either py27+ or unittest2) so tornado.test.util enforces -# this requirement, but for other users of tornado.testing we want -# to allow the older version if unitest2 is not available. -if PY3: - # On python 3, mixing unittest2 and unittest (including doctest) - # doesn't seem to work, so always use unittest. - import unittest -else: - # On python 2, prefer unittest2 when available. - try: - import unittest2 as unittest # type: ignore - except ImportError: - import unittest # type: ignore +import unittest + +from tornado import gen +from tornado.httpclient import AsyncHTTPClient +from tornado.httpserver import HTTPServer +from tornado.ioloop import IOLoop, TimeoutError +from tornado import netutil +from tornado.platform.asyncio import AsyncIOMainLoop +from tornado.process import Subprocess +from tornado.log import app_log +from tornado.stack_context import ExceptionStackContext +from tornado.util import raise_exc_info, basestring_type, PY3 -if asyncio is None: - _NON_OWNED_IOLOOPS = () -else: - import tornado.platform.asyncio - _NON_OWNED_IOLOOPS = tornado.platform.asyncio.AsyncIOMainLoop +_NON_OWNED_IOLOOPS = AsyncIOMainLoop def bind_unused_port(reuse_port=False): @@ -123,7 +79,7 @@ class _TestMethodWrapper(object): def __call__(self, *args, **kwargs): result = self.orig_method(*args, **kwargs) - if isinstance(result, GeneratorType) or iscoroutine(result): + if isinstance(result, Generator) or inspect.iscoroutine(result): raise TypeError("Generator and coroutine test methods should be" " decorated with tornado.testing.gen_test") elif result is not None: @@ -531,13 +487,13 @@ def gen_test(func=None, timeout=None): @functools.wraps(f) def pre_coroutine(self, *args, **kwargs): result = f(self, *args, **kwargs) - if isinstance(result, GeneratorType) or iscoroutine(result): + if isinstance(result, Generator) or inspect.iscoroutine(result): self._test_generator = result else: self._test_generator = None return result - if iscoroutinefunction(f): + if inspect.iscoroutinefunction(f): coro = pre_coroutine else: coro = gen.coroutine(pre_coroutine)