Python 3 - Fix some issues with class / instance variables in unittest2.

Explanation from a Python wizard (not me) about why this exhibited
different behavior under Python 2 and Python 3.

    `cmp` is a builtin_function_or_method in Python 2.7, which doesn't
    have a __get__ and doesn't qualify as a "descriptor".  Your lambda is a
    regular function which qualifies as a descriptor whose __get__ method
    returns a bound instance.

His suggested fix was to write

    sortTestMethodsUsing = staticmethod(cmp_)

However, I don't think `sortTestMethodsUsing` (or any of the other fields
of `TestLoader`) should be class attributes anyway.  They are all accessed
through self, so they should be instance attributes.  So the fix employed
here is to convert them to instance attributes.

Differential Revision: http://reviews.llvm.org/D14453
Reviewed By: Todd Fiala

llvm-svn: 252346
This commit is contained in:
Zachary Turner
2015-11-06 21:37:07 +00:00
parent 3cb66c85b9
commit 059e52c44c
3 changed files with 16 additions and 37 deletions

View File

@@ -34,6 +34,8 @@ if sys.version_info[0] >= 3:
else:
cmp_ = cmp
reversed_cmp_ = lambda x, y: -cmp_(x,y)
__all__ = ['TestResult', 'TestCase', 'TestSuite',
'TextTestRunner', 'TestLoader', 'FunctionTestCase', 'main',
'defaultTestLoader', 'SkipTest', 'skip', 'skipIf', 'skipUnless',

View File

@@ -1,5 +1,6 @@
"""Loading unittests."""
import functools
import os
import re
import sys
@@ -18,17 +19,6 @@ except ImportError:
__unittest = True
def _CmpToKey(mycmp):
'Convert a cmp= function into a key= function'
class K(object):
def __init__(self, obj):
self.obj = obj
def __lt__(self, other):
return mycmp(self.obj, other.obj) == -1
return K
# what about .pyc or .pyo (etc)
# we would need to avoid loading the same tests multiple times
# from '.py', '.pyc' *and* '.pyo'
@@ -60,10 +50,12 @@ class TestLoader(unittest.TestLoader):
This class is responsible for loading tests according to various criteria
and returning them wrapped in a TestSuite
"""
testMethodPrefix = 'test'
sortTestMethodsUsing = cmp_
suiteClass = suite.TestSuite
_top_level_dir = None
def __init__(self):
self.testMethodPrefix = 'test'
self.sortTestMethodsUsing = cmp_
self.suiteClass = suite.TestSuite
self._top_level_dir = None
def loadTestsFromTestCase(self, testCaseClass):
"""Return a suite of all tests cases contained in testCaseClass"""
@@ -157,7 +149,7 @@ class TestLoader(unittest.TestLoader):
hasattr(getattr(testCaseClass, attrname), '__call__')
testFnNames = list(filter(isTestMethod, dir(testCaseClass)))
if self.sortTestMethodsUsing:
testFnNames.sort(key=_CmpToKey(self.sortTestMethodsUsing))
testFnNames.sort(key=functools.cmp_to_key(self.sortTestMethodsUsing))
return testFnNames
def discover(self, start_dir, pattern='test*.py', top_level_dir=None):

View File

@@ -1104,15 +1104,12 @@ class Test_TestLoader(unittest2.TestCase):
# "Function to be used to compare method names when sorting them in
# getTestCaseNames() and all the loadTestsFromX() methods"
def test_sortTestMethodsUsing__loadTestsFromTestCase(self):
def reversed_cmp(x, y):
return -cmp(x, y)
class Foo(unittest2.TestCase):
def test_1(self): pass
def test_2(self): pass
loader = unittest2.TestLoader()
loader.sortTestMethodsUsing = reversed_cmp
loader.sortTestMethodsUsing = unittest2.reversed_cmp_
tests = loader.suiteClass([Foo('test_2'), Foo('test_1')])
self.assertEqual(loader.loadTestsFromTestCase(Foo), tests)
@@ -1120,9 +1117,6 @@ class Test_TestLoader(unittest2.TestCase):
# "Function to be used to compare method names when sorting them in
# getTestCaseNames() and all the loadTestsFromX() methods"
def test_sortTestMethodsUsing__loadTestsFromModule(self):
def reversed_cmp(x, y):
return -cmp(x, y)
m = types.ModuleType('m')
class Foo(unittest2.TestCase):
def test_1(self): pass
@@ -1130,7 +1124,7 @@ class Test_TestLoader(unittest2.TestCase):
m.Foo = Foo
loader = unittest2.TestLoader()
loader.sortTestMethodsUsing = reversed_cmp
loader.sortTestMethodsUsing = unittest2.reversed_cmp_
tests = [loader.suiteClass([Foo('test_2'), Foo('test_1')])]
self.assertEqual(list(loader.loadTestsFromModule(m)), tests)
@@ -1138,9 +1132,6 @@ class Test_TestLoader(unittest2.TestCase):
# "Function to be used to compare method names when sorting them in
# getTestCaseNames() and all the loadTestsFromX() methods"
def test_sortTestMethodsUsing__loadTestsFromName(self):
def reversed_cmp(x, y):
return -cmp(x, y)
m = types.ModuleType('m')
class Foo(unittest2.TestCase):
def test_1(self): pass
@@ -1148,7 +1139,7 @@ class Test_TestLoader(unittest2.TestCase):
m.Foo = Foo
loader = unittest2.TestLoader()
loader.sortTestMethodsUsing = reversed_cmp
loader.sortTestMethodsUsing = unittest2.reversed_cmp_
tests = loader.suiteClass([Foo('test_2'), Foo('test_1')])
self.assertEqual(loader.loadTestsFromName('Foo', m), tests)
@@ -1156,9 +1147,6 @@ class Test_TestLoader(unittest2.TestCase):
# "Function to be used to compare method names when sorting them in
# getTestCaseNames() and all the loadTestsFromX() methods"
def test_sortTestMethodsUsing__loadTestsFromNames(self):
def reversed_cmp(x, y):
return -cmp(x, y)
m = types.ModuleType('m')
class Foo(unittest2.TestCase):
def test_1(self): pass
@@ -1166,7 +1154,7 @@ class Test_TestLoader(unittest2.TestCase):
m.Foo = Foo
loader = unittest2.TestLoader()
loader.sortTestMethodsUsing = reversed_cmp
loader.sortTestMethodsUsing = unittest2.reversed_cmp_
tests = [loader.suiteClass([Foo('test_2'), Foo('test_1')])]
self.assertEqual(list(loader.loadTestsFromNames(['Foo'], m)), tests)
@@ -1176,15 +1164,12 @@ class Test_TestLoader(unittest2.TestCase):
#
# Does it actually affect getTestCaseNames()?
def test_sortTestMethodsUsing__getTestCaseNames(self):
def reversed_cmp(x, y):
return -cmp(x, y)
class Foo(unittest2.TestCase):
def test_1(self): pass
def test_2(self): pass
loader = unittest2.TestLoader()
loader.sortTestMethodsUsing = reversed_cmp
loader.sortTestMethodsUsing = unittest2.reversed_cmp_
test_names = ['test_2', 'test_1']
self.assertEqual(loader.getTestCaseNames(Foo), test_names)
@@ -1192,7 +1177,7 @@ class Test_TestLoader(unittest2.TestCase):
# "The default value is the built-in cmp() function"
def test_sortTestMethodsUsing__default_value(self):
loader = unittest2.TestLoader()
self.assertTrue(loader.sortTestMethodsUsing is cmp)
self.assertTrue(loader.sortTestMethodsUsing is unittest2.cmp_)
# "it can be set to None to disable the sort."
#