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

@hugovk hugovk added needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes labels Dec 13, 2025
@hugovk hugovk merged commit f6b6a99 into python:main Dec 13, 2025
32 checks passed
@github-project-automation github-project-automation bot moved this from Todo to Done in Docs PRs Dec 13, 2025
@miss-islington-app
Copy link

Thanks @decorator-factory for the PR, and @hugovk for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14.
🐍🍒⛏🤖

@miss-islington-app
Copy link

Sorry, @decorator-factory and @hugovk, I could not cleanly backport this to 3.14 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker f6b6a99aa5d63702b8e8101864ae08e615131702 3.14

@miss-islington-app
Copy link

Sorry, @decorator-factory and @hugovk, I could not cleanly backport this to 3.13 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker f6b6a99aa5d63702b8e8101864ae08e615131702 3.13

@hugovk
Copy link
Member

hugovk commented Dec 13, 2025

@decorator-factory Would you like to have a go at backporting? See instructions above.

@decorator-factory
Copy link
Contributor Author

@hugovk Yes, I'll try to do that. Backports are just pull requests to branches other than main, correct? And in this case, miss Islington failed to generate a PR due to conflicts, and now I just need to make those PRs to 3.13 and 3.14 branches?

decorator-factory added a commit to decorator-factory-clones/cpython that referenced this pull request Dec 14, 2025
…ring adjustments in 3.13 (pythonGH-142413)

(cherry picked from commit f6b6a99)

Co-authored-by: decorator-factory <42166884+decorator-factory@users.noreply.github.com>
@bedevere-app
Copy link

bedevere-app bot commented Dec 14, 2025

GH-142689 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.14 bugs and security fixes label Dec 14, 2025
@bedevere-app
Copy link

bedevere-app bot commented Dec 14, 2025

GH-142690 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.13 bugs and security fixes label Dec 14, 2025
hugovk pushed a commit that referenced this pull request Dec 14, 2025
hugovk pushed a commit that referenced this pull request Dec 14, 2025
@hugovk
Copy link
Member

hugovk commented Dec 14, 2025

@hugovk Yes, I'll try to do that. Backports are just pull requests to branches other than main, correct? And in this case, miss Islington failed to generate a PR due to conflicts, and now I just need to make those PRs to 3.13 and 3.14 branches?

Exactly so. Thanks, merged!

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

Labels

docs Documentation in the Doc dir skip news

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants