[ python-Bugs-1337990 ] doctest mishandles exceptions raised within generators

SourceForge.net noreply at sourceforge.net
Wed Oct 26 04:18:28 CEST 2005


Bugs item #1337990, was opened at 2005-10-26 11:48
Message generated for change (Tracker Item Submitted) made by Item Submitter
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1337990&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Python Library
Group: None
Status: Open
Resolution: None
Priority: 5
Submitted By: Tim Wegener (twegener)
Assigned to: Nobody/Anonymous (nobody)
Summary: doctest mishandles exceptions raised within generators

Initial Comment:
If a generator raises an exception while iterating over it, doctest 
will only register the exception output, and will miss output that 
occurred during previous loop iterations.

The following code clarifies and reproduces the problem:
(also included as an attachment)

"""Reproduce bug with exceptions in a generator in doctest tests.

This bug has been seen to occur in:

Linux (RH9):
Python 2.4.1
Python 2.3.5
Python 2.2.2 (using from __future__ import generators)

Windows XP:
Python 2.4.2
Python 2.3.5

"""

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
    


----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1337990&group_id=5470


More information about the Python-bugs-list mailing list