Skip to content

Docs: missing mention of bytes-like object in exec() documentation #148942

@goroshirow

Description

@goroshirow

Documentation

Documentation

In Doc/library/functions.rst, the sentences that describe the source parameter for the exec() function read:

This function supports dynamic execution of Python code. source must be either a string or a code object. If it is a string, the string is parsed as a suite of Python statements which is then executed (unless a syntax error occurs). [1]_ If it is a code object, it is simply executed.

This explicitly restricts the source parameter to a string or a code object. However, the actual implementation accepts bytes-like objects (such as bytes, bytearray, and memoryview) as well, parsing them internally via the buffer protocol. Because the sentence omits this, users can easily misunderstand that passing bytes is unsupported.

Suggested change

This function supports dynamic execution of Python code. source must be a string, a bytes-like object, or a code object. If it is a string or a bytes-like object, it is parsed as a suite of Python statements which is then executed (unless a syntax error occurs). [1]_ If it is a code object, it is simply executed.

Adding "a bytes-like object" makes the documentation accurately reflect the underlying implementation. Changing the subsequent sentence ensures logical consistency for how these types are parsed.

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsDocumentation in the Doc dir

    Projects

    Status

    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions