Skip to content

LogRecord should normalize falsy exc_info values #158839

Description

@srittau

Bug report

Pointed out by @tjkuson in python/typeshed#16466

According to the documentation, the exc_info argument to the log() group of function can be any falsy value to indicate that no exception information is provided. It's common to use either None or False here.

When a truthy value is provided, it is normalized to an exception info tuple before passing it to LogRecord. A falsy value is passed on unchanged:

if exc_info:
if isinstance(exc_info, BaseException):
exc_info = (type(exc_info), exc_info, exc_info.__traceback__)
elif not isinstance(exc_info, tuple):
exc_info = sys.exc_info()

This contradicts LogRecords documentation, which says that only None is accepted and that the exc_info field can be a tuple or None. The mypy primer run in python/typeshed#16466 shows that a few projects make this assumption. For example, sphinx:

        if record.exc_info is not None:
            raise SphinxWarning(message) from record.exc_info[1]

Solution: Normalize falsy values to None before passing them to makeRecord.

CPython versions tested on:

CPython main branch

Operating systems tested on:

No response

Linked PRs

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions