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 fails to compare tracebacks with notes #111159

Closed
sobolevn opened this issue Oct 21, 2023 · 4 comments
Closed

doctest fails to compare tracebacks with notes #111159

sobolevn opened this issue Oct 21, 2023 · 4 comments
Assignees
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@sobolevn
Copy link
Member

sobolevn commented Oct 21, 2023

Bug report

Minimal reproducer:

"""
Module doctest

>>> raise_with_note()
Traceback (most recent call last):
  ...
ValueError: Text
Note
"""

def raise_with_note():
    err = ValueError('Text')
    err.add_note('Note')
    raise err

Now, run it, you will get a very strange error:

» ./python.exe -m doctest ex.py
**********************************************************************
File "/Users/sobolev/Desktop/cpython/ex.py", line 4, in ex
Failed example:
    raise_with_note()
Expected:
    Traceback (most recent call last):
      ...
    ValueError: Text
    Note
Got:
    Traceback (most recent call last):
      File "/Users/sobolev/Desktop/cpython/Lib/doctest.py", line 1374, in __run
        exec(compile(example.source, filename, "single",
      File "<doctest ex[0]>", line 1, in <module>
        raise_with_note()
      File "/Users/sobolev/Desktop/cpython/ex.py", line 14, in raise_with_note
        raise err
    ValueError: Text
    Note
**********************************************************************
1 items had failures:
   1 of   1 in ex
***Test Failed*** 1 failures.

Things that do not help:

  • IGNORE_EXCEPTION_DETAIL
  • ELLIPSIS
  • Coping the whole traceback

Related to how exceptions with notes are represented in

exc_msg = traceback.format_exception_only(*exception[:2])[-1]

I am working on this issue right now :)

Refs #111157

Linked PRs

@sobolevn sobolevn added type-bug An unexpected behavior, bug, or error stdlib Python modules in the Lib dir labels Oct 21, 2023
@sobolevn sobolevn self-assigned this Oct 21, 2023
sobolevn added a commit to sobolevn/cpython that referenced this issue Oct 21, 2023
sobolevn added a commit to sobolevn/cpython that referenced this issue Oct 21, 2023
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Oct 21, 2023
…notes (pythonGH-111160)

(cherry picked from commit fd60549)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Oct 21, 2023
…notes (pythonGH-111160)

(cherry picked from commit fd60549)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
iritkatriel pushed a commit that referenced this issue Oct 21, 2023
… notes (GH-111160) (#111170)

gh-111159: Fix `doctest` output comparison for exceptions with notes (GH-111160)
(cherry picked from commit fd60549)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
iritkatriel pushed a commit that referenced this issue Oct 21, 2023
… notes (GH-111160) (#111169)

gh-111159: Fix `doctest` output comparison for exceptions with notes (GH-111160)
(cherry picked from commit fd60549)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
@scoder
Copy link
Contributor

scoder commented Oct 31, 2023

With this change in place, I now get a (reproducible) doctest failure in lxml that seems rather random:
https://github.com/lxml/lxml/blob/7356acba91bcf37b2c1c6ab5b7c9e37a4f1145b9/doc/xpathxslt.txt

Doctest: xpathxslt.txt ... 
======================================================================
ERROR: txt (xpathxslt)
Doctest: xpathxslt.txt
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/opt/python3.13/lib/python3.13/unittest/case.py", line 58, in testPartExecutor
    yield
  File "/opt/python3.13/lib/python3.13/unittest/case.py", line 636, in run
    self._callTestMethod(testMethod)
  File "/opt/python3.13/lib/python3.13/unittest/case.py", line 589, in _callTestMethod
    if method() is not None:
       ^^^^^^^^
  File "/opt/python3.13/lib/python3.13/doctest.py", line 2257, in runTest
    failures, tries = runner.run(
                      ^^^^^^^^^^^
  File "/opt/python3.13/lib/python3.13/doctest.py", line 1537, in run
    return self.__run(test, compileflags, out)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/python3.13/lib/python3.13/doctest.py", line 1402, in __run
    exc_msg_index = next(
                    ^^^^^
StopIteration

I'm not using "notes" anywhere in the file (and wouldn't know what I'd use them for), so this breaks existing tests.

@scoder scoder reopened this Oct 31, 2023
@sobolevn
Copy link
Member Author

Thanks, I will have a look!

@scoder
Copy link
Contributor

scoder commented Oct 31, 2023

I think the problem is with subclasses of SyntaxError. If they live in packages, the startswith() comparison in the newly added code can fail.

sobolevn added a commit to sobolevn/cpython that referenced this issue Oct 31, 2023
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Nov 4, 2023
… classes (pythonGH-111541)

(cherry picked from commit 18c9548)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Nov 4, 2023
… classes (pythonGH-111541)

(cherry picked from commit 18c9548)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
iritkatriel pushed a commit that referenced this issue Nov 4, 2023
…n classes (GH-111541) (#111733)

gh-111159: Fix `SyntaxError` doctests for non-builtin exception classes (GH-111541)
(cherry picked from commit 18c9548)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
iritkatriel pushed a commit that referenced this issue Nov 4, 2023
…n classes (GH-111541) (#111732)

gh-111159: Fix `SyntaxError` doctests for non-builtin exception classes (GH-111541)
(cherry picked from commit 18c9548)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
@hugovk
Copy link
Member

hugovk commented Nov 9, 2023

Anything left for this issue or can it be closed? Thanks!

@sobolevn sobolevn closed this as completed Nov 9, 2023
aisk pushed a commit to aisk/cpython that referenced this issue Feb 11, 2024
aisk pushed a commit to aisk/cpython that referenced this issue Feb 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

5 participants
@scoder @hugovk @sobolevn @terryjreedy and others