From: Ben Darnell Date: Sat, 15 Mar 2014 04:45:09 +0000 (-0400) Subject: Refactor unittest/unittest2 imports to fix issues with unittest2 on py3. X-Git-Tag: v4.0.0b1~103 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=99474fe89b4cea763e3fd449113a28a8d92d6729;p=thirdparty%2Ftornado.git Refactor unittest/unittest2 imports to fix issues with unittest2 on py3. Never use unittest2 on python 3 (it appears to not interoperate with the standard unittest module). On python 2, use the same logic in tornado.testing and tornado.test.util to select an implementation. Fixes #1005. --- diff --git a/tornado/test/util.py b/tornado/test/util.py index 360431040..6a0fe8073 100644 --- a/tornado/test/util.py +++ b/tornado/test/util.py @@ -5,10 +5,13 @@ import sys # Encapsulate the choice of unittest or unittest2 here. # To be used as 'from tornado.test.util import unittest'. -if sys.version_info >= (2, 7): - import unittest -else: +if sys.version_info < (2, 7): + # In py26, we must always use unittest2. import unittest2 as unittest +else: + # Otherwise, use whichever version of unittest was imported in + # tornado.testing. + from tornado.testing import unittest skipIfNonUnix = unittest.skipIf(os.name != 'posix' or sys.platform == 'cygwin', "non-unix platform") diff --git a/tornado/testing.py b/tornado/testing.py index 96fdd32b0..647f349c1 100644 --- a/tornado/testing.py +++ b/tornado/testing.py @@ -49,10 +49,16 @@ except ImportError: # (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. -try: - import unittest2 as unittest -except ImportError: +if sys.version_info >= (3,): + # 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 + except ImportError: + import unittest _next_port = 10000 diff --git a/tox.ini b/tox.ini index 9f7cf3797..3d67726c2 100644 --- a/tox.ini +++ b/tox.ini @@ -36,6 +36,7 @@ envlist = py2-opt, py3-opt, py3-utf8, py2-locale, + py27-unittest2, py33-unittest2, # Ensure the sphinx build has no errors or warnings py2-docs @@ -78,6 +79,12 @@ deps = pycurl twisted>=11.0.0 +# unittest2 doesn't add anything we need on 2.7, but we should ensure that +# its existence doesn't break anything due to conditional imports. +[testenv:py27-unittest2] +basepython = python2.7 +deps = unittest2 + # In python 3, opening files in text mode uses a system-dependent encoding by # default. Run the tests with "C" (ascii) and "utf-8" locales to ensure # we don't have hidden dependencies on this setting. @@ -104,6 +111,12 @@ setenv = TORNADO_EXTENSION=1 deps = pycurl>=7.19.3 +# See comment on py27-unittest2. +[testenv:py33-unittest2] +basepython = python3.3 +setenv = TORNADO_EXTENSION=1 +deps = unittest2py3k + [testenv:py34-full] basepython = python3.4 setenv = TORNADO_EXTENSION=1