Skip to content

code: add ExceptionInfo.from_exception#10901

Merged
bluetech merged 2 commits intopytest-dev:mainfrom
bluetech:exceptioninfo-from-exception
Apr 13, 2023
Merged

code: add ExceptionInfo.from_exception#10901
bluetech merged 2 commits intopytest-dev:mainfrom
bluetech:exceptioninfo-from-exception

Conversation

@bluetech
Copy link
Copy Markdown
Member

The old-style sys.exc_info() triplet is redundant nowadays with (type(exc), exc, exc.__traceback__), and is beginning to get soft-deprecated in Python 3.12.

Add a nicer API to ExceptionInfo which takes just the exc instead of the triplet. There are already a few internal uses which benefit.

Comment thread src/_pytest/_code/code.py
"""Return an ExceptionInfo for an existing exc_info tuple.
"""Return an ExceptionInfo for an existing exception.

The exception must have a non-``None`` ``__traceback__`` attribute,
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only hitch here is this subtlety. An alternative is to just allow ExcInfo to have a missing traceback...

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm I personally prefer to get a clear error at this point, rather than make ExceptionInfo.__traceback__ be Optional.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, agreed.

@bluetech bluetech force-pushed the exceptioninfo-from-exception branch from 9fd7d5b to dc5dd8c Compare April 12, 2023 09:57
The old-style `sys.exc_info()` triplet is redundant nowadays with
`(type(exc), exc, exc.__traceback__)`, and is beginning to get
soft-deprecated in Python 3.12.

Add a nicer API to ExceptionInfo which takes just the exc instead of the
triplet. There are already a few internal uses which benefit.
@bluetech bluetech force-pushed the exceptioninfo-from-exception branch from dc5dd8c to 424c3ee Compare April 12, 2023 10:16
Copy link
Copy Markdown
Member

@nicoddemus nicoddemus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work!

Comment thread src/_pytest/_code/code.py
"""Return an ExceptionInfo for an existing exc_info tuple.
"""Return an ExceptionInfo for an existing exception.

The exception must have a non-``None`` ``__traceback__`` attribute,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm I personally prefer to get a clear error at this point, rather than make ExceptionInfo.__traceback__ be Optional.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants