From: Ben Darnell Date: Sun, 10 Apr 2016 19:27:33 +0000 (-0400) Subject: Switch to mypy-friendly conditional imports throughout X-Git-Tag: v4.4.0b1~35 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f568d276abc75d872522fad8797d71abf858c760;p=thirdparty%2Ftornado.git Switch to mypy-friendly conditional imports throughout --- diff --git a/tornado/auth.py b/tornado/auth.py index edc3e5ebe..9e6e1754d 100644 --- a/tornado/auth.py +++ b/tornado/auth.py @@ -82,17 +82,15 @@ from tornado import escape from tornado.httputil import url_concat from tornado.log import gen_log from tornado.stack_context import ExceptionStackContext -from tornado.util import unicode_type, ArgReplacer - -try: - import urlparse # py2 -except ImportError: - import urllib.parse as urlparse # py3 - -try: - import urllib.parse as urllib_parse # py3 -except ImportError: - import urllib as urllib_parse # py2 +from tornado.util import unicode_type, ArgReplacer, PY3 + +if PY3: + import urllib.parse as urlparse + import urllib.parse as urllib_parse + long = int +else: + import urlparse + import urllib as urllib_parse try: long # py2 diff --git a/tornado/httputil.py b/tornado/httputil.py index 471df54f9..061e71078 100644 --- a/tornado/httputil.py +++ b/tornado/httputil.py @@ -33,27 +33,22 @@ import time from tornado.escape import native_str, parse_qs_bytes, utf8 from tornado.log import gen_log -from tornado.util import ObjectDict +from tornado.util import ObjectDict, PY3 -try: - import Cookie # py2 -except ImportError: - import http.cookies as Cookie # py3 +if PY3: + import http.cookies as Cookie + from http.client import responses + from urllib.parse import urlencode +else: + import Cookie + from httplib import responses + from urllib import urlencode -try: - from httplib import responses # py2 -except ImportError: - from http.client import responses # py3 # responses is unused in this file, but we re-export it to other files. # Reference it so pyflakes doesn't complain. responses -try: - from urllib import urlencode # py2 -except ImportError: - from urllib.parse import urlencode # py3 - try: from ssl import SSLError except ImportError: diff --git a/tornado/process.py b/tornado/process.py index daa9677bb..e18ebbf05 100644 --- a/tornado/process.py +++ b/tornado/process.py @@ -35,7 +35,7 @@ from tornado.iostream import PipeIOStream from tornado.log import gen_log from tornado.platform.auto import set_close_exec from tornado import stack_context -from tornado.util import errno_from_exception +from tornado.util import errno_from_exception, PY3 try: import multiprocessing @@ -43,11 +43,8 @@ except ImportError: # Multiprocessing is not available on Google App Engine. multiprocessing = None -try: - long # py2 -except NameError: - long = int # py3 - +if PY3: + long = int # Re-export this exception for convenience. try: diff --git a/tornado/simple_httpclient.py b/tornado/simple_httpclient.py index 44e45c43f..82f868644 100644 --- a/tornado/simple_httpclient.py +++ b/tornado/simple_httpclient.py @@ -11,6 +11,7 @@ from tornado.netutil import Resolver, OverrideResolver, _client_ssl_defaults from tornado.log import gen_log from tornado import stack_context from tornado.tcpclient import TCPClient +from tornado.util import PY3 import base64 import collections @@ -22,10 +23,10 @@ import sys from io import BytesIO -try: - import urlparse # py2 -except ImportError: - import urllib.parse as urlparse # py3 +if PY3: + import urllib.parse as urlparse +else: + import urlparse try: import ssl diff --git a/tornado/test/options_test.py b/tornado/test/options_test.py index af0fc1170..fd2798fcd 100644 --- a/tornado/test/options_test.py +++ b/tornado/test/options_test.py @@ -6,13 +6,13 @@ import os import sys from tornado.options import OptionParser, Error -from tornado.util import basestring_type +from tornado.util import basestring_type, PY3 from tornado.test.util import unittest -try: - from cStringIO import StringIO # python 2 -except ImportError: - from io import StringIO # python 3 +if PY3: + from io import StringIO +else: + from cStringIO import StringIO try: from unittest import mock # python 3.3 diff --git a/tornado/test/twisted_test.py b/tornado/test/twisted_test.py index ed234c460..22e8b33d0 100644 --- a/tornado/test/twisted_test.py +++ b/tornado/test/twisted_test.py @@ -28,6 +28,8 @@ import tempfile import threading import warnings +from tornado.util import PY3 + try: import fcntl from twisted.internet.defer import Deferred, inlineCallbacks, returnValue @@ -52,10 +54,10 @@ try: except ImportError: have_twisted_web = False -try: - import thread # py2 -except ImportError: - import _thread as thread # py3 +if PY3: + import _thread as thread +else: + import thread from tornado.escape import utf8 from tornado import gen diff --git a/tornado/test/web_test.py b/tornado/test/web_test.py index 906dcf702..24e6b07a7 100644 --- a/tornado/test/web_test.py +++ b/tornado/test/web_test.py @@ -11,7 +11,7 @@ from tornado.simple_httpclient import SimpleAsyncHTTPClient from tornado.template import DictLoader from tornado.testing import AsyncHTTPTestCase, AsyncTestCase, ExpectLog, gen_test from tornado.test.util import unittest, skipBefore35, exec_test -from tornado.util import ObjectDict, unicode_type, timedelta_to_seconds +from tornado.util import ObjectDict, unicode_type, timedelta_to_seconds, PY3 from tornado.web import RequestHandler, authenticated, Application, asynchronous, url, HTTPError, StaticFileHandler, _create_signature_v1, create_signed_value, decode_signed_value, ErrorHandler, UIModule, MissingArgumentError, stream_request_body, Finish, removeslash, addslash, RedirectHandler as WebRedirectHandler, get_signature_key_version, GZipContentEncoding import binascii @@ -27,9 +27,9 @@ import os import re import socket -try: +if PY3: import urllib.parse as urllib_parse # py3 -except ImportError: +else: import urllib as urllib_parse # py2 wsgi_safe_tests = [] diff --git a/tornado/testing.py b/tornado/testing.py index 119234d04..f000771db 100644 --- a/tornado/testing.py +++ b/tornado/testing.py @@ -32,7 +32,7 @@ except ImportError: Subprocess = None from tornado.log import gen_log, app_log from tornado.stack_context import ExceptionStackContext -from tornado.util import raise_exc_info, basestring_type +from tornado.util import raise_exc_info, basestring_type, PY3 import functools import inspect import logging @@ -42,10 +42,10 @@ import signal import socket import sys -try: - from cStringIO import StringIO # py2 -except ImportError: - from io import StringIO # py3 +if PY3: + from io import StringIO +else: + from cStringIO import StringIO try: from collections.abc import Generator as GeneratorType # py35+ @@ -62,7 +62,7 @@ else: # (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 sys.version_info >= (3,): +if PY3: # On python 3, mixing unittest2 and unittest (including doctest) # doesn't seem to work, so always use unittest. import unittest diff --git a/tornado/web.py b/tornado/web.py index da774c7b9..14cadf696 100644 --- a/tornado/web.py +++ b/tornado/web.py @@ -90,24 +90,17 @@ from tornado import stack_context from tornado import template from tornado.escape import utf8, _unicode from tornado.util import (import_object, ObjectDict, raise_exc_info, - unicode_type, _websocket_mask, re_unescape) + unicode_type, _websocket_mask, re_unescape, PY3) from tornado.httputil import split_host_and_port - -try: - import Cookie # py2 -except ImportError: - import http.cookies as Cookie # py3 - -try: - import urlparse # py2 -except ImportError: - import urllib.parse as urlparse # py3 - -try: - from urllib import urlencode # py2 -except ImportError: - from urllib.parse import urlencode # py3 +if PY3: + import http.cookies as Cookie + import urllib.parse as urlparse + from urllib.parse import urlencode +else: + import Cookie + import urlparse + from urllib import urlencode MIN_SUPPORTED_SIGNED_VALUE_VERSION = 1 diff --git a/tornado/websocket.py b/tornado/websocket.py index f5e3dbd7f..3bbd08ab4 100644 --- a/tornado/websocket.py +++ b/tornado/websocket.py @@ -36,18 +36,14 @@ from tornado.iostream import StreamClosedError from tornado.log import gen_log, app_log from tornado import simple_httpclient from tornado.tcpclient import TCPClient -from tornado.util import _websocket_mask +from tornado.util import _websocket_mask, PY3 -try: +if PY3: from urllib.parse import urlparse # py2 -except ImportError: + xrange = range +else: from urlparse import urlparse # py3 -try: - xrange # py2 -except NameError: - xrange = range # py3 - class WebSocketError(Exception): pass diff --git a/tornado/wsgi.py b/tornado/wsgi.py index 59e6c559f..e9ead300d 100644 --- a/tornado/wsgi.py +++ b/tornado/wsgi.py @@ -41,12 +41,12 @@ from tornado import httputil from tornado.log import access_log from tornado import web from tornado.escape import native_str -from tornado.util import unicode_type +from tornado.util import unicode_type, PY3 -try: +if PY3: import urllib.parse as urllib_parse # py3 -except ImportError: +else: import urllib as urllib_parse # PEP 3333 specifies that WSGI on python 3 generally deals with byte strings