]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Switch to mypy-friendly conditional imports throughout
authorBen Darnell <ben@bendarnell.com>
Sun, 10 Apr 2016 19:27:33 +0000 (15:27 -0400)
committerBen Darnell <ben@bendarnell.com>
Sun, 10 Apr 2016 19:27:48 +0000 (15:27 -0400)
tornado/auth.py
tornado/httputil.py
tornado/process.py
tornado/simple_httpclient.py
tornado/test/options_test.py
tornado/test/twisted_test.py
tornado/test/web_test.py
tornado/testing.py
tornado/web.py
tornado/websocket.py
tornado/wsgi.py

index edc3e5ebe725747a1fefabeecb9192216b01512f..9e6e1754d03127a7f689dd3706f39dbf1b640210 100644 (file)
@@ -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
index 471df54f967c8792ce5aae9d058533db4ebc285a..061e71078c245992340e9fe8d3eba897465f12e5 100644 (file)
@@ -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:
index daa9677bb500af3cadc3a06fc7a043611d063841..e18ebbf05f3ac3b53aeb6d3af8325258dc43250e 100644 (file)
@@ -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:
index 44e45c43fc7325d29fdcfbe71f6f4c55285c590c..82f8686446614e2f0e062617829ea323b0b89a3d 100644 (file)
@@ -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
index af0fc11701ee2299769a350f8c541852bc9b93c9..fd2798fcdba70c248367b11deeff0d4a472ff79b 100644 (file)
@@ -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
index ed234c460a70fe77c1418609e6fc1ef119ae85e3..22e8b33d05bb5fa8c60c21de1f86f7d4d7fc406e 100644 (file)
@@ -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
index 906dcf702b4a39c4e32816ca57437f761be9071f..24e6b07a7298c94a98d75bd1803b4d55ca811473 100644 (file)
@@ -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 = []
index 119234d0471d6db8417bfec8f24d29ea6072cd68..f000771db085bbfd595f6bd81e03823fd43c0fc0 100644 (file)
@@ -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
index da774c7b9187b75b9fb756e327e853b8f270baf9..14cadf6968a66ab4d65b7933491103dcfffa1e69 100644 (file)
@@ -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
index f5e3dbd7f80a6d939c3323396c89afca28ee410f..3bbd08ab40dafbf59edf8127cac86e459d72a446 100644 (file)
@@ -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
index 59e6c559f1289695ffc99627ee65ab3398562e5c..e9ead300da036fbb3156feb7df69e26b2cd41e86 100644 (file)
@@ -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