Index: unittest.py =================================================================== RCS file: /cvsroot/python/python/dist/src/Lib/unittest.py,v retrieving revision 1.32 diff -c -r1.32 unittest.py *** unittest.py 26 Oct 2003 16:38:16 -0000 1.32 --- unittest.py 5 Dec 2003 20:30:20 -0000 *************** *** 90,95 **** --- 90,103 ---- def _strclass(cls): return "%s.%s" % (cls.__module__, cls.__name__) + def _traceback_len(tb): + "Return the number of frames in a traceback" + i = 1 + while tb.tb_next is not None: + tb = tb.tb_next + i += 1 + return i + class TestResult: """Holder for test result information. *************** *** 119,130 **** """Called when an error has occurred. 'err' is a tuple of values as returned by sys.exc_info(). """ ! self.errors.append((test, self._exc_info_to_string(err))) def addFailure(self, test, err): """Called when an error has occurred. 'err' is a tuple of values as returned by sys.exc_info().""" ! self.failures.append((test, self._exc_info_to_string(err))) def addSuccess(self, test): "Called when a test has completed successfully" --- 127,138 ---- """Called when an error has occurred. 'err' is a tuple of values as returned by sys.exc_info(). """ ! self.errors.append((test, self._exc_info_to_string(err, True))) def addFailure(self, test, err): """Called when an error has occurred. 'err' is a tuple of values as returned by sys.exc_info().""" ! self.failures.append((test, self._exc_info_to_string(err, False))) def addSuccess(self, test): "Called when a test has completed successfully" *************** *** 138,146 **** "Indicates that the tests should be aborted" self.shouldStop = True ! def _exc_info_to_string(self, err): ! """Converts a sys.exc_info()-style tuple of values into a string.""" ! return ''.join(traceback.format_exception(*err)) def __repr__(self): return "<%s run=%i errors=%i failures=%i>" % \ --- 146,167 ---- "Indicates that the tests should be aborted" self.shouldStop = True ! def _exc_info_to_string(self, err, is_error=False): ! """Converts a sys.exc_info()-style tuple of values into a ! string. ! ! If is_error is true, a full traceback is returned, otherwise ! the innermost frame is not included because it refers to a ! failxxx() method. ! """ ! if is_error: ! info = traceback.format_exception(*err) ! return "".join(info) ! else: ! etype, evalue, etb = err ! info = traceback.format_exception(etype, evalue, etb, ! _traceback_len(etb)-1) ! return "".join(info) def __repr__(self): return "<%s run=%i errors=%i failures=%i>" % \ *************** *** 303,312 **** callableObj(*args, **kwargs) except excClass: return else: ! if hasattr(excClass,'__name__'): excName = excClass.__name__ ! else: excName = str(excClass) ! raise self.failureException, "%s not raised" % excName def failUnlessEqual(self, first, second, msg=None): """Fail if the two objects are unequal as determined by the '==' --- 324,347 ---- callableObj(*args, **kwargs) except excClass: return + except Exception, detail: + etype, evalue, etb = sys.exc_info() + err = traceback.format_exception_only(etype, evalue)[-1] + raise self.failureException, "wrong exception, expected %s\ngot: %r" % \ + (self._exc_names(excClass), err.strip()) else: ! raise self.failureException, "no exception, expected %s" % self._exc_names(excClass) ! ! def _exc_names(self, excspec): ! # format excspec into a nice string ! try: ! n = len(excspec) ! except TypeError: # not a sequence ! return excspec.__name__ ! names = [cls.__name__ for cls in excspec] ! if n > 2: ! return ", ".join(names[:-1]) + ", or %s" % names[-1] ! return ", ".join(names) def failUnlessEqual(self, first, second, msg=None): """Fail if the two objects are unequal as determined by the '=='