Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions Lib/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
"""
from _ast import *

type_None = type(None)
type_Ellipsis = type(...)

def parse(source, filename='<unknown>', mode='exec', *,
type_comments=False, feature_version=None, optimize=-1, module=None):
Expand Down Expand Up @@ -68,8 +70,12 @@ def _convert_literal(node):
"""
Used by `literal_eval` to convert an AST node into a value.
"""
if isinstance(node, Constant):
return node.value
if (
isinstance(node, Constant)
and type(value := node.value) in (int, float, complex, bytes, bool, str, bytes,
type_None, type_Ellipsis)
):
return value
if isinstance(node, Dict) and len(node.keys) == len(node.values):
return dict(zip(
map(_convert_literal, node.keys),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add missing validation for allowed constant values in :func:`ast.literal_eval`.
Loading