[Python-checkins] r64855 - python/trunk/Lib/unittest.py

raymond.hettinger python-checkins at python.org
Thu Jul 10 18:06:41 CEST 2008


Author: raymond.hettinger
Date: Thu Jul 10 18:06:41 2008
New Revision: 64855
Log:
Suppress -3 warnings in unittest.py
Modified:
 python/trunk/Lib/unittest.py
Modified: python/trunk/Lib/unittest.py
==============================================================================
--- python/trunk/Lib/unittest.py	(original)
+++ python/trunk/Lib/unittest.py	Thu Jul 10 18:06:41 2008
@@ -68,7 +68,6 @@
 # Backward compatibility
 ##############################################################################
 if sys.version_info[:2] < (2, 2):
- False, True = 0, 1
 def isinstance(obj, clsinfo):
 import __builtin__
 if type(clsinfo) in (tuple, list):
@@ -79,6 +78,14 @@
 return 0
 else: return __builtin__.isinstance(obj, clsinfo)
 
+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
 
 ##############################################################################
 # Test framework core
@@ -429,7 +436,7 @@
 
 def addTest(self, test):
 # sanity checks
- if not callable(test):
+ if not hasattr(test, '__call__'):
 raise TypeError("the test to add must be callable")
 if (isinstance(test, (type, types.ClassType)) and
 issubclass(test, (TestCase, TestSuite))):
@@ -584,7 +591,7 @@
 return TestSuite([parent(obj.__name__)])
 elif isinstance(obj, TestSuite):
 return obj
- elif callable(obj):
+ elif hasattr(obj, '__call__'):
 test = obj()
 if isinstance(test, TestSuite):
 return test
@@ -607,10 +614,10 @@
 """Return a sorted sequence of method names found within testCaseClass
 """
 def isTestMethod(attrname, testCaseClass=testCaseClass, prefix=self.testMethodPrefix):
- return attrname.startswith(prefix) and callable(getattr(testCaseClass, attrname))
+ return attrname.startswith(prefix) and hasattr(getattr(testCaseClass, attrname), '__call__')
 testFnNames = filter(isTestMethod, dir(testCaseClass))
 if self.sortTestMethodsUsing:
- testFnNames.sort(self.sortTestMethodsUsing)
+ testFnNames.sort(key=_CmpToKey(self.sortTestMethodsUsing))
 return testFnNames
 
 


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /