GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-122461: Document that compile() and ast.parse() raise SyntaxError for null bytes by devdanzin · Pull Request #122462 · python/cpython

Conversation

@devdanzin

@devdanzin devdanzin commented Jul 30, 2024 •
edited by github-actions Bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

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

  • Issue: Incorrect information about exception raised if source contains null bytes for ast.parse() and compile() #122461

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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@devdanzin
Update docs for compile() and ast.parse() stating they raise SyntaxEr…
b68ea99
…ror for null bytes.
@bedevere-app bedevere-app Bot added docs Documentation in the Doc dir skip news labels Jul 30, 2024
@bedevere-app bedevere-app Bot mentioned this pull request Jul 30, 2024
Incorrect information about exception raised if source contains null bytes for ast.parse() and compile() #122461
Open
@bedevere-app bedevere-app Bot added the awaiting review label Jul 30, 2024
@devdanzin

devdanzin commented Jul 30, 2024

Copy link
Copy Markdown
Member Author

Requesting review from @JelleZijlstra.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@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 only security fixes labels Jul 30, 2024
JelleZijlstra
JelleZijlstra reviewed Jul 31, 2024
View reviewed changes
Comment thread Doc/library/ast.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Doc/library/ast.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Doc/library/functions.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Doc/library/functions.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

@devdanzin
Address review for docs of compile() and ast.parse() regarding null c…
a7dd6e9
…haracters.
@devdanzin

devdanzin commented Jul 31, 2024

Copy link
Copy Markdown
Member Author

Thank you both for the reviews!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

serhiy-storchaka
serhiy-storchaka reviewed Jul 31, 2024
View reviewed changes

@serhiy-storchaka serhiy-storchaka left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this 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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@serhiy-storchaka

serhiy-storchaka commented Jul 31, 2024

Copy link
Copy Markdown
Member

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@devdanzin

devdanzin commented Jul 31, 2024

Copy link
Copy Markdown
Member 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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@serhiy-storchaka

serhiy-storchaka commented Jul 31, 2024

Copy link
Copy Markdown
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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@devdanzin

devdanzin commented Jul 31, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member Author

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
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@devdanzin
Document that ast.parse() and compile() can raise ValueError, Overflo…
1615c89
…wError and RecursionError.
@hugovk hugovk removed the needs backport to 3.12 only security fixes label Apr 10, 2025
@python-cla-bot

python-cla-bot Bot commented Apr 18, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@serhiy-storchaka serhiy-storchaka added the needs backport to 3.14 bugs and security fixes label May 8, 2025
@github-actions

github-actions Bot commented Apr 17, 2026

Copy link
Copy Markdown

This PR is stale because it has been open for 30 days with no activity.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label Apr 17, 2026
@serhiy-storchaka serhiy-storchaka added the needs backport to 3.15 bugs and security fixes label May 30, 2026
@github-actions github-actions Bot removed the stale Stale PR or inactive for long period of time. label Jun 6, 2026
@github-actions

github-actions Bot commented Sep 8, 2026

Copy link
Copy Markdown

This PR is stale because it has been open for 90 days with no activity.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label Sep 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@JelleZijlstra JelleZijlstra JelleZijlstra left review comments
@serhiy-storchaka serhiy-storchaka serhiy-storchaka left review comments

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

awaiting review docs Documentation in the Doc dir needs backport to 3.13 only security fixes needs backport to 3.14 bugs and security fixes needs backport to 3.15 bugs and security fixes skip news stale Stale PR or inactive for long period of time.

Projects

Docs PRs
Status: Todo

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

5 participants

@devdanzin @serhiy-storchaka @JelleZijlstra @hugovk @Eclips4
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.