@@ -1990,20 +1990,28 @@ and classes for traversing abstract syntax trees:
1990
1990
1991
1991
.. function :: literal_eval(node_or_string)
1992
1992
1993
- Safely evaluate an expression node or a string containing a Python literal or
1993
+ Evaluate an expression node or a string containing only a Python literal or
1994
1994
container display. The string or node provided may only consist of the
1995
1995
following Python literal structures: strings, bytes, numbers, tuples, lists,
1996
1996
dicts, sets, booleans, ``None `` and ``Ellipsis ``.
1997
1997
1998
- This can be used for safely evaluating strings containing Python values from
1999
- untrusted sources without the need to parse the values oneself. It is not
2000
- capable of evaluating arbitrarily complex expressions, for example involving
2001
- operators or indexing.
1998
+ This can be used for evaluating strings containing Python values without the
1999
+ need to parse the values oneself. It is not capable of evaluating
2000
+ arbitrarily complex expressions, for example involving operators or
2001
+ indexing.
2002
+
2003
+ This function had been documented as "safe" in the past without defining
2004
+ what that meant. That was misleading. This is specifically designed not to
2005
+ execute Python code, unlike the more general :func: `eval `. There is no
2006
+ namespace, no name lookups, or ability to call out. But it is not free from
2007
+ attack: A relatively small input can lead to memory exhaustion or to C stack
2008
+ exhaustion, crashing the process. There is also the possibility for
2009
+ excessive CPU consumption denial of service on some inputs. Calling it on
2010
+ untrusted data is thus not recommended.
2002
2011
2003
2012
.. warning ::
2004
- It is possible to crash the Python interpreter with a
2005
- sufficiently large/complex string due to stack depth limitations
2006
- in Python's AST compiler.
2013
+ It is possible to crash the Python interpreter due to stack depth
2014
+ limitations in Python's AST compiler.
2007
2015
2008
2016
It can raise :exc: `ValueError `, :exc: `TypeError `, :exc: `SyntaxError `,
2009
2017
:exc: `MemoryError ` and :exc: `RecursionError ` depending on the malformed
0 commit comments