Skip to content

gh-122461: Document that compile() and ast.parse() raise SyntaxError for null bytes #122462

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

devdanzin
Copy link
Contributor

@devdanzin devdanzin commented Jul 30, 2024

Update docs for compile() and ast.parse() because they raise SyntaxError instead of ValueError for null bytes since #97594.


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

@devdanzin
Copy link
Contributor Author

Requesting review from @JelleZijlstra.

@JelleZijlstra JelleZijlstra self-requested a review July 30, 2024 13:50
@Eclips4 Eclips4 added needs backport to 3.12 only security fixes needs backport to 3.13 bugs and security fixes labels Jul 30, 2024
@devdanzin
Copy link
Contributor Author

Thank you both for the reviews!

Copy link
Member

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

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

Note that UnicodeEncodeError (which is a subclass of ValueError) can still be raised if the source string is not encodable.

@serhiy-storchaka
Copy link
Member

There are also mentions of ValueError in the codeop documentation and docstrings. I do not know whether they are still valid.

@devdanzin
Copy link
Contributor Author

Note that UnicodeEncodeError (which is a subclass of ValueError) can still be raised if the source string is not encodable.

Yes, for code such as ast.parse("\ud800"). Should this possibility be added to the documentation?

Plus MemoryError and RecursionError are possible for too deeply nested code.

@serhiy-storchaka
Copy link
Member

OverflowError is documented in codeop, and it can still be raised. For example:

>>> import ast
>>> try: ast.parse('x'*2**31, '?', 'exec')
... except BaseException as err: e = err
... 
>>> str(e)
'Parser column offset overflow - source line is too big'
>>> type(e)
<class 'OverflowError'>

I do not know whether ValueError other than UnicodeEncodeError can be raised, but it is safer to document ValueError, OverflowError and RecursionError in all these places. I do not think that it is worth to document MemoryError or say KeyboardInterrupt specially, because they depend rather on the environment than on the input and you cannot fix them.

@devdanzin
Copy link
Contributor Author

devdanzin commented Jul 31, 2024

Would this count?

>>> ast.parse("", feature_version=(2,2))
Traceback (most recent call last):
  File "<python-input-2>", line 1, in <module>
    ast.parse("", feature_version=(2,2))
    ~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "~\PycharmProjects\cpython\Lib\ast.py", line 50, in parse
    raise ValueError(f"Unsupported major version: {major}")
ValueError: Unsupported major version: 2

Edit: here's another one:

>>> ast.parse("", "\0")
Traceback (most recent call last):
  File "<python-input-9>", line 1, in <module>
    ast.parse("", "\0")
    ~~~~~~~~~^^^^^^^^^^
  File "~\PycharmProjects\cpython\Lib\ast.py", line 53, in parse
    return compile(source, filename, mode, flags,
                   _feature_version=feature_version, optimize=optimize)
ValueError: embedded null character

@hugovk hugovk removed the needs backport to 3.12 only security fixes label Apr 10, 2025
@python-cla-bot
Copy link

python-cla-bot bot commented Apr 18, 2025

All commit authors signed the Contributor License Agreement.

CLA signed

@serhiy-storchaka serhiy-storchaka added the needs backport to 3.14 bugs and security fixes label May 8, 2025
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 needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes skip news
Projects
Status: Todo
Development

Successfully merging this pull request may close these issues.

5 participants