"""Reproduce bug with exceptions in a generator in doctest tests. This bug has been seen to occur in: Python 2.4.1 Python 2.3.5 Python 2.2.2 (using from __future__ import generators) """ __author__ = 'Tim Wegener ' def error_generator(): """Yield 0 to 2 and then try and raise an exception. >>> for j in error_generator(): ... print j 0 1 2 Traceback (most recent call last): Exception: Contrived exception for sake of example """ # Note: This is obviously a contrived example of generator use. for i in range(3): yield i if 1: raise Exception("Contrived exception for sake of example") raise StopIteration if __name__ == '__main__': # Run the doctest tests. import sys import doctest doctest.testmod(sys.modules['__main__']) print print 'What should have happened...' for j in error_generator(): print j