Skip to content

Conversation

@decorator-factory
Copy link
Contributor

@decorator-factory decorator-factory commented Dec 8, 2025

See issue #142411 for details.

  1. Document that compile can now raise UnicodeDecodeError

  2. Fix paragraph about docstring indentation in the tutorial. The REPL output was fixed in Docs: Fix indent in controlflow.rst #134008, but the preceding paragraph hasn't been adjusted

I believe this should be backported to 3.13 and 3.14, not sure how that works.


📚 Documentation preview 📚: https://cpython-previews--142413.org.readthedocs.build/

@bedevere-app bedevere-app bot added docs Documentation in the Doc dir skip news labels Dec 8, 2025
@python-cla-bot
Copy link

python-cla-bot bot commented Dec 8, 2025

All commit authors signed the Contributor License Agreement.

CLA signed

indented less should not occur, but if they occur all their leading whitespace
should be stripped. Equivalence of whitespace should be tested after expansion
of tabs (to 8 spaces, normally).
The Python parser strips indentation from multi-line string literals when they
Copy link
Member

Choose a reason for hiding this comment

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

TIL!


- :exc:`SyntaxError` if the compiled source is invalid
- :exc:`ValueError` if the source contains null bytes
- :exc:`UnicodeDecodeError` if the source code contains docstrings that
Copy link
Member

Choose a reason for hiding this comment

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

I wouldn't try to keep this list precise and complete. The exceptions change over time, and it's not like people are going to write except blocks for these specific exceptions. Details are in the versionchanged blocks below. Here can we say that SyntaxError happens if the source is invalid, and other exceptions if the text can't be read in various ways?

Copy link
Contributor Author

@decorator-factory decorator-factory Dec 10, 2025

Choose a reason for hiding this comment

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

I now changed the description to this:

This function raises :exc:`SyntaxError` or :exc:`ValueError` if the compiled source is invalid.

To summarize some things about compile's interface:

  • If the source contains null bytes (whether it's a bytes object or a str object), SyntaxError is raised since Python 3.11, and not ValueError, contrary to current documentation.
  • If the source is an ast.Module, null bytes and surrogates seem to be allowed (but lead to bizzarre behaviour)
  • If the source is a bytes, SyntaxError will be raise if the source is encoded incorrectly
  • If the source is valid, -OO is not enabled, and any docstings would have a surrogate, UnicodeDecodeError (subclass of ValueError) is raised

It seems like the error conditions are a bit complicated and may change from version to version, so maybe it's good to keep it a bit opaque? Meaning: if you want to use this function and you expect the input to sometimes be invalid, prepare to catch both SyntaxError and ValueError

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

Labels

awaiting review docs Documentation in the Doc dir skip news

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

2 participants