From 4bccb29b50e2eb0ea3e10298435f28723a09ea1d Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Fri, 18 Dec 2020 14:51:06 +0100 Subject: [PATCH] utils: fix subscript checking for python 3.9 (#49608) --- src/authentic2/utils/evaluate.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/authentic2/utils/evaluate.py b/src/authentic2/utils/evaluate.py index 039e8aad..a7957770 100644 --- a/src/authentic2/utils/evaluate.py +++ b/src/authentic2/utils/evaluate.py @@ -183,10 +183,18 @@ class ConditionValidator(BaseExpressionValidator): def check_Subscript(self, node): # check subscript are constant number or strings - if (not isinstance(node.slice, ast.Index) - or not isinstance(node.slice.value, CONSTANT_CLASSES) - # with python <3.8 the node class is enough to determine the value - or (sys.version_info >= (3, 8) and not isinstance(node.slice.value.value, (int, str, bytes)))): + ok = True + if sys.version_info >= (3, 9): + ok = isinstance(node.slice, CONSTANT_CLASSES) + elif sys.version_info >= (3, 8): + ok = ( + isinstance(node.slice, ast.Index) + and isinstance(node.slice.value, CONSTANT_CLASSES) + and isinstance(node.slice.value.value, (int, str, bytes)) + ) + else: + ok = isinstance(node.slice, ast.Index) and isinstance(node.slice.value, CONSTANT_CLASSES) + if not ok: raise ExpressionError(_('subscript index MUST be a constant'), code='invalid-subscript', node=node) -- 2.29.2