Skip to content
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

doctest handling of multiline strings is broken #84475

Closed
filiprem mannequin opened this issue Apr 15, 2020 · 6 comments
Closed

doctest handling of multiline strings is broken #84475

filiprem mannequin opened this issue Apr 15, 2020 · 6 comments
Labels
3.7 (EOL) end of life 3.8 only security fixes 3.9 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@filiprem
Copy link
Mannequin

filiprem mannequin commented Apr 15, 2020

BPO 40295
Nosy @stevendaprano, @serhiy-storchaka, @filiprem
Files
  • doctest-bugs.py: isolated test case
  • doctest-bugs-2.py: simplified test case to reproduce the 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 2020-04-16.02:30:42.343>
    created_at = <Date 2020-04-15.20:37:13.193>
    labels = ['type-bug', '3.8', '3.9', 'invalid', '3.7', 'library']
    title = 'doctest handling of multiline strings is broken'
    updated_at = <Date 2020-04-16.02:46:10.324>
    user = 'https://github.com/filiprem'

    bugs.python.org fields:

    activity = <Date 2020-04-16.02:46:10.324>
    actor = 'steven.daprano'
    assignee = 'none'
    closed = True
    closed_date = <Date 2020-04-16.02:30:42.343>
    closer = 'steven.daprano'
    components = ['Library (Lib)']
    creation = <Date 2020-04-15.20:37:13.193>
    creator = 'Filip Rembia\xc5\x82kowski'
    dependencies = []
    files = ['49065', '49066']
    hgrepos = []
    issue_num = 40295
    keywords = []
    message_count = 6.0
    messages = ['366554', '366560', '366567', '366569', '366574', '366575']
    nosy_count = 3.0
    nosy_names = ['steven.daprano', 'serhiy.storchaka', 'Filip Rembia\xc5\x82kowski']
    pr_nums = []
    priority = 'normal'
    resolution = 'not a bug'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue40295'
    versions = ['Python 2.7', 'Python 3.5', 'Python 3.6', 'Python 3.7', 'Python 3.8', 'Python 3.9']

    @filiprem
    Copy link
    Mannequin Author

    filiprem mannequin commented Apr 15, 2020

    The doctest module does not compare multiline strings properly, as attached example proves.

    Tested on 2.7, 3.6 and 3.9.0a5+. (platform: Ubuntu 18.04).

    Related: https://stackoverflow.com/questions/60956015/unexpected-errors-while-testing-python3-code-with-doctest

    @filiprem filiprem mannequin added 3.7 (EOL) end of life 3.8 only security fixes 3.9 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Apr 15, 2020
    @serhiy-storchaka
    Copy link
    Member

    Did you try to run the tested function in the interactive Python interpreter? Did you get what you expected?

    @filiprem
    Copy link
    Mannequin Author

    filiprem mannequin commented Apr 15, 2020

    @serhiy, Thank you for feedback.

    Yes the "testme" function (indeed trivial) works as expected - both in interactive Python interpreter and in script file.

    If you go to Lib/doctest.py, search for "string-identical" and debug my example there, you will see the problem is real.

    I don't see an easy fix for the problem, I only suspect it's related to io.StringIO getvalue() method.

    @filiprem
    Copy link
    Mannequin Author

    filiprem mannequin commented Apr 15, 2020

    Actually, the behavior does not depend on leading spaces, and test case can be isolated even further. Sample is attached [doctest-bugs-2.py].

    @stevendaprano
    Copy link
    Member

    Have you tried calling multiline_output() in the REPL?

    It does *not* show your expected output:

    # expected
    First line
    Second line
    

    but the string repr():

    # actual
    'First line\nSecond line\n'
    

    Change your doctest to either:

        >>> multiline_output()
        'First line\\nSecond line\\n'

    (note that you must escape the backslashes) or:

        >>> print(multiline_output())
        First line
        Second line
        <BLANKLINE>

    Note that the "<BLANKLINE>" needs to be written literally, as described here:

    https://docs.python.org/3/library/doctest.html#doctest.DONT_ACCEPT_BLANKLINE

    @stevendaprano
    Copy link
    Member

    By the way Filip, you were told on the Stackoverflow page that the output was correct and that you were not using doctest correctly. Serhiy also hinted to you that you should check the output in the REPL and you falsely claimed that it gave the expected output, but it doesn't:

    py> multiline_output()
    'First line\nSecond line\n'
    

    which is nothing like the output you put in your doctest.

    This is not a bug in doctest. The very first line of the doctest documentation says:

    The doctest module searches for pieces of text that look like
    interactive Python sessions
    

    so this is expected and documented behaviour, not a bug.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.7 (EOL) end of life 3.8 only security fixes 3.9 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants