Skip to content

doctest mishandles exceptions raised within generators #42523

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
twegener mannequin opened this issue Oct 26, 2005 · 2 comments
Closed

doctest mishandles exceptions raised within generators #42523

twegener mannequin opened this issue Oct 26, 2005 · 2 comments
Labels
docs Documentation in the Doc dir

Comments

@twegener
Copy link
Mannequin

twegener mannequin commented Oct 26, 2005

BPO 1337990
Nosy @tim-one
Files
  • doctest_generator_bug.py: Code to reproduce doctest bug
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2006-04-24.02:13:57.000>
    created_at = <Date 2005-10-26.02:18:27.000>
    labels = ['docs']
    title = 'doctest mishandles exceptions raised within generators'
    updated_at = <Date 2006-04-24.02:13:57.000>
    user = 'https://bugs.python.org/twegener'

    bugs.python.org fields:

    activity = <Date 2006-04-24.02:13:57.000>
    actor = 'tim.peters'
    assignee = 'none'
    closed = True
    closed_date = None
    closer = None
    components = ['Documentation']
    creation = <Date 2005-10-26.02:18:27.000>
    creator = 'twegener'
    dependencies = []
    files = ['1826']
    hgrepos = []
    issue_num = 1337990
    keywords = []
    message_count = 2.0
    messages = ['26710', '26711']
    nosy_count = 2.0
    nosy_names = ['tim.peters', 'twegener']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = None
    status = 'closed'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue1337990'
    versions = []

    @twegener
    Copy link
    Mannequin Author

    twegener mannequin commented Oct 26, 2005

    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
    

    @twegener twegener mannequin closed this as completed Oct 26, 2005
    @twegener twegener mannequin closed this as completed Oct 26, 2005
    @twegener twegener mannequin added docs Documentation in the Doc dir labels Oct 26, 2005
    @tim-one
    Copy link
    Member

    tim-one commented Apr 24, 2006

    Logged In: YES
    user_id=31435

    doctest doesn't support mixing "expected normal output" with
    an exception, regardless of how such a thing may arise. For
    example, this one-liner can't work either as a doctest:

    >>> print 'hi', 1/0
    hi
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ZeroDivisionError: integer division or modulo by zero

    So there's nothing specific to generators in this, rather
    it's a doctest limitation. The docs do say, wrt testing
    exceptions:

    """
    No problem, provided that the traceback is the only output
    produced by the example: just paste in the traceback.
    """

    In any case, it wasn't intended that doctest support this,
    and the docs do try to communicate that. I added text
    spelling out the other half (if there's expected output in
    addition to the traceback, problem ;-)), in rev 45677 on the
    trunk and rev 45678 on the 2.4 branch, and am closing this
    report as a doc bug.

    Edward Loper tried supporting this, as a new feature, before
    2.4 was released, but it so grossly complicated the docs and
    the implementation that we agreed to drop it. So you could
    re-open this as a feature request, but it's unlikely to change.

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    docs Documentation in the Doc dir
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant