From b68ea99bb7cf57945c8904d1ae66a5b68a752f21 Mon Sep 17 00:00:00 2001 From: devdanzin <74280297+devdanzin@users.noreply.github.com> Date: Tue, 30 Jul 2024 10:40:05 -0300 Subject: [PATCH 1/3] Update docs for compile() and ast.parse() stating they raise SyntaxError for null bytes. --- Doc/library/ast.rst | 6 +++++- Doc/library/functions.rst | 7 +++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/Doc/library/ast.rst b/Doc/library/ast.rst index dd5dd5ca4e9e32..560ffe4b2f8cba 100644 --- a/Doc/library/ast.rst +++ b/Doc/library/ast.rst @@ -2155,7 +2155,7 @@ and classes for traversing abstract syntax trees: is no guarantee that the parse (or success of the parse) is the same as when run on the Python version corresponding to ``feature_version``. - If source contains a null character (``\0``), :exc:`ValueError` is raised. + If source contains a null character (``\0``), :exc:`SyntaxError` is raised. .. warning:: Note that successfully parsing source code into an AST object doesn't @@ -2176,6 +2176,10 @@ and classes for traversing abstract syntax trees: .. versionchanged:: 3.8 Added ``type_comments``, ``mode='func_type'`` and ``feature_version``. + .. versionchanged:: 3.12 + Previously, :exc:`ValueError` was raised when null bytes were encountered + in *source*. + .. versionchanged:: 3.13 The minimum supported version for ``feature_version`` is now ``(3, 7)``. The ``optimize`` argument was added. diff --git a/Doc/library/functions.rst b/Doc/library/functions.rst index 2c649376efee70..fd5a1d120817b0 100644 --- a/Doc/library/functions.rst +++ b/Doc/library/functions.rst @@ -334,8 +334,8 @@ are always available. They are listed here in alphabetical order. ``__debug__`` is true), ``1`` (asserts are removed, ``__debug__`` is false) or ``2`` (docstrings are removed too). - This function raises :exc:`SyntaxError` if the compiled source is invalid, - and :exc:`ValueError` if the source contains null bytes. + This function raises :exc:`SyntaxError` if the compiled source is invalid or + contains null bytes. If you want to parse Python code into its AST representation, see :func:`ast.parse`. @@ -371,6 +371,9 @@ are always available. They are listed here in alphabetical order. ``ast.PyCF_ALLOW_TOP_LEVEL_AWAIT`` can now be passed in flags to enable support for top-level ``await``, ``async for``, and ``async with``. + .. versionchanged:: 3.12 + Previously, :exc:`ValueError` was raised when null bytes were encountered + in *source*. .. class:: complex(number=0, /) complex(string, /) From a7dd6e9ffe28ebb4b13d571fa8b7eda819726e5b Mon Sep 17 00:00:00 2001 From: devdanzin <74280297+devdanzin@users.noreply.github.com> Date: Wed, 31 Jul 2024 06:22:24 -0300 Subject: [PATCH 2/3] Address review for docs of compile() and ast.parse() regarding null characters. --- Doc/library/ast.rst | 6 ++---- Doc/library/functions.rst | 7 +++---- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/Doc/library/ast.rst b/Doc/library/ast.rst index 560ffe4b2f8cba..175a65af0b9484 100644 --- a/Doc/library/ast.rst +++ b/Doc/library/ast.rst @@ -2155,8 +2155,6 @@ and classes for traversing abstract syntax trees: is no guarantee that the parse (or success of the parse) is the same as when run on the Python version corresponding to ``feature_version``. - If source contains a null character (``\0``), :exc:`SyntaxError` is raised. - .. warning:: Note that successfully parsing source code into an AST object doesn't guarantee that the source code provided is valid Python code that can @@ -2177,8 +2175,8 @@ and classes for traversing abstract syntax trees: Added ``type_comments``, ``mode='func_type'`` and ``feature_version``. .. versionchanged:: 3.12 - Previously, :exc:`ValueError` was raised when null bytes were encountered - in *source*. + Previously, :exc:`ValueError` was raised instead of :exc:`SyntaxError` + when null characters (``\0``) were encountered in *source*. .. versionchanged:: 3.13 The minimum supported version for ``feature_version`` is now ``(3, 7)``. diff --git a/Doc/library/functions.rst b/Doc/library/functions.rst index fd5a1d120817b0..5b72d7b910d34e 100644 --- a/Doc/library/functions.rst +++ b/Doc/library/functions.rst @@ -334,8 +334,7 @@ are always available. They are listed here in alphabetical order. ``__debug__`` is true), ``1`` (asserts are removed, ``__debug__`` is false) or ``2`` (docstrings are removed too). - This function raises :exc:`SyntaxError` if the compiled source is invalid or - contains null bytes. + This function raises :exc:`SyntaxError` if the compiled source is invalid. If you want to parse Python code into its AST representation, see :func:`ast.parse`. @@ -372,8 +371,8 @@ are always available. They are listed here in alphabetical order. support for top-level ``await``, ``async for``, and ``async with``. .. versionchanged:: 3.12 - Previously, :exc:`ValueError` was raised when null bytes were encountered - in *source*. + Previously, :exc:`ValueError` was raised instead of :exc:`SyntaxError` when + null characters (``\0``) were encountered in *source*. .. class:: complex(number=0, /) complex(string, /) From 1615c89cd63b450870eefcd42a6e3c50926de1b7 Mon Sep 17 00:00:00 2001 From: devdanzin <74280297+devdanzin@users.noreply.github.com> Date: Wed, 31 Jul 2024 09:28:24 -0300 Subject: [PATCH 3/3] Document that ast.parse() and compile() can raise ValueError, OverflowError and RecursionError. --- Doc/library/ast.rst | 4 ++++ Doc/library/functions.rst | 2 ++ 2 files changed, 6 insertions(+) diff --git a/Doc/library/ast.rst b/Doc/library/ast.rst index 175a65af0b9484..b43b61b69b6b9b 100644 --- a/Doc/library/ast.rst +++ b/Doc/library/ast.rst @@ -2155,6 +2155,10 @@ and classes for traversing abstract syntax trees: is no guarantee that the parse (or success of the parse) is the same as when run on the Python version corresponding to ``feature_version``. + This function raises :exc:`SyntaxError` if the parsed source is invalid. + It can also raise :exc:`ValueError`, :exc:`OverflowError` and + :exc:`RecursionError`. + .. warning:: Note that successfully parsing source code into an AST object doesn't guarantee that the source code provided is valid Python code that can diff --git a/Doc/library/functions.rst b/Doc/library/functions.rst index 5b72d7b910d34e..8b92471cf4f289 100644 --- a/Doc/library/functions.rst +++ b/Doc/library/functions.rst @@ -335,6 +335,8 @@ are always available. They are listed here in alphabetical order. or ``2`` (docstrings are removed too). This function raises :exc:`SyntaxError` if the compiled source is invalid. + It can also raise :exc:`ValueError`, :exc:`OverflowError` and + :exc:`RecursionError`. If you want to parse Python code into its AST representation, see :func:`ast.parse`.