]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-30637: Improve the docs of ast.parse regarding differences with compile() (GH...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Sun, 19 Sep 2021 23:13:56 +0000 (16:13 -0700)
committerGitHub <noreply@github.com>
Sun, 19 Sep 2021 23:13:56 +0000 (16:13 -0700)
(cherry picked from commit e6d05a4092b4176a30d1d1596585df13c2ab676d)

Co-authored-by: Pablo Galindo Salgado <Pablogsal@gmail.com>
Doc/library/ast.rst

index b8eecbda5e408e9844e3d4701898d3570a1cf003..39a08416bad7279ab20c0b49c9dc05e458bae3d4 100644 (file)
@@ -1549,6 +1549,19 @@ and classes for traversing abstract syntax trees:
    ``await`` as variable names.  The lowest supported version is
    ``(3, 4)``; the highest is ``sys.version_info[0:2]``.
 
+   If source contains a null character ('\0'), :exc:`ValueError` is raised.
+
+    .. warning::
+      Note that succesfully parsing souce code into an AST object doesn't
+      guarantee that the source code provided is valid Python code that can
+      be executed as the compilation step can raise further :exc:`SyntaxError`
+      exceptions. For instance, the source ``return 42`` generates a valid
+      AST node for a return statement, but it cannot be compiled alone (it needs
+      to be inside a function node).
+
+      In particular, :func:`ast.parse` won't do any scoping checks, which the
+      compilation step does.
+
    .. warning::
       It is possible to crash the Python interpreter with a
       sufficiently large/complex string due to stack depth limitations