From bdb7d2af1560e68b5a4cd3c3d3ca591e8c61ae59 Mon Sep 17 00:00:00 2001 From: Pablo Galindo Date: Sun, 19 Sep 2021 20:39:47 +0100 Subject: [PATCH] bpo-30637: Improve the docs of ast.parse regarding differences with compile() --- Doc/library/ast.rst | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/Doc/library/ast.rst b/Doc/library/ast.rst index 19e7bcc414b84b..e21151bd4ef792 100644 --- a/Doc/library/ast.rst +++ b/Doc/library/ast.rst @@ -1917,6 +1917,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