Skip to content

Commit 41e2a31

Browse files
bpo-30637: Improve the docs of ast.parse regarding differences with compile() (GH-28459)
(cherry picked from commit e6d05a4) Co-authored-by: Pablo Galindo Salgado <[email protected]>
1 parent 7e465a6 commit 41e2a31

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

Doc/library/ast.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1549,6 +1549,19 @@ and classes for traversing abstract syntax trees:
15491549
``await`` as variable names. The lowest supported version is
15501550
``(3, 4)``; the highest is ``sys.version_info[0:2]``.
15511551

1552+
If source contains a null character ('\0'), :exc:`ValueError` is raised.
1553+
1554+
.. warning::
1555+
Note that succesfully parsing souce code into an AST object doesn't
1556+
guarantee that the source code provided is valid Python code that can
1557+
be executed as the compilation step can raise further :exc:`SyntaxError`
1558+
exceptions. For instance, the source ``return 42`` generates a valid
1559+
AST node for a return statement, but it cannot be compiled alone (it needs
1560+
to be inside a function node).
1561+
1562+
In particular, :func:`ast.parse` won't do any scoping checks, which the
1563+
compilation step does.
1564+
15521565
.. warning::
15531566
It is possible to crash the Python interpreter with a
15541567
sufficiently large/complex string due to stack depth limitations

0 commit comments

Comments
 (0)