From fe46b03a2be7830bd2039f9478558752d0a37ce9 Mon Sep 17 00:00:00 2001 From: Bonu Krishna Chaitanya Date: Tue, 25 Nov 2025 07:15:10 +0530 Subject: [PATCH 1/3] fix --- Lib/ast.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/Lib/ast.py b/Lib/ast.py index d9743ba7ab40b1..c37736f3ba5c3e 100644 --- a/Lib/ast.py +++ b/Lib/ast.py @@ -22,6 +22,8 @@ """ from _ast import * +type_None = type(None) +type_Ellipsis = type(...) def parse(source, filename='', mode='exec', *, type_comments=False, feature_version=None, optimize=-1, module=None): @@ -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, + 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), From 0de79a84c9a556d3a402d2a74d4e9bf017a50503 Mon Sep 17 00:00:00 2001 From: Krishna Chaitanya <141550576+XChaitanyaX@users.noreply.github.com> Date: Fri, 19 Dec 2025 06:23:21 +0530 Subject: [PATCH 2/3] Update Lib/ast.py Co-authored-by: Sergey B Kirpichev --- Lib/ast.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/ast.py b/Lib/ast.py index c37736f3ba5c3e..34171cb544e11f 100644 --- a/Lib/ast.py +++ b/Lib/ast.py @@ -72,7 +72,7 @@ def _convert_literal(node): """ if ( isinstance(node, Constant) - and type(value := node.value) in (int, float, complex, bytes, bool, + and type(value := node.value) in (int, float, complex, bytes, bool, str, bytes, type_None, type_Ellipsis) ): return value From 81161a58da1e0a95b5304361d6bbb1dc386db9c1 Mon Sep 17 00:00:00 2001 From: Bonu Krishna Chaitanya Date: Fri, 19 Dec 2025 06:59:07 +0530 Subject: [PATCH 3/3] news --- .../next/Library/2025-12-19-06-51-09.gh-issue-141778.mPAupY.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2025-12-19-06-51-09.gh-issue-141778.mPAupY.rst diff --git a/Misc/NEWS.d/next/Library/2025-12-19-06-51-09.gh-issue-141778.mPAupY.rst b/Misc/NEWS.d/next/Library/2025-12-19-06-51-09.gh-issue-141778.mPAupY.rst new file mode 100644 index 00000000000000..a5d9cd256f899d --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-12-19-06-51-09.gh-issue-141778.mPAupY.rst @@ -0,0 +1 @@ +Add missing validation for allowed constant values in :func:`ast.literal_eval`.