From 32081edec87ab0d859b58ac5a9f2542df4e41e68 Mon Sep 17 00:00:00 2001 From: Jarrod Johnson Date: Fri, 11 Mar 2022 12:21:09 -0500 Subject: [PATCH] Workaround ':' format specifier syntax Older python will break by assuming that : always means a format expression is coming. Move the field value fetch to format_field, and ascertain if some of the expression was shunted to format specification by mistake. --- .../confluent/config/configmanager.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/confluent_server/confluent/config/configmanager.py b/confluent_server/confluent/config/configmanager.py index f1428dca..d42c6f96 100644 --- a/confluent_server/confluent/config/configmanager.py +++ b/confluent_server/confluent/config/configmanager.py @@ -1011,8 +1011,17 @@ class _ExpressionFormat(string.Formatter): self._numbers = None def get_field(self, field_name, args, kwargs): + return field_name, field_name + + def format_field(self, val, format_spec): + if ']' in format_spec: + field_name = val + ':' + format_spec + format_spec = '' + else: + field_name = val parsed = ast.parse(field_name) - return self._handle_ast_node(parsed.body[0].value), field_name + val = self._handle_ast_node(parsed.body[0].value) + return format(val, format_spec) def _handle_ast_node(self, node): if isinstance(node, ast.Num): @@ -1061,8 +1070,6 @@ class _ExpressionFormat(string.Formatter): op = self._supported_ops[optype] return op(int(self._handle_ast_node(node.left)), int(self._handle_ast_node(node.right))) - elif isinstance(node, ast.Constant): - return node.value elif isinstance(node, ast.Index): return self._handle_ast_node(node.value) elif isinstance(node, ast.UnaryOp): @@ -1087,6 +1094,8 @@ class _ExpressionFormat(string.Formatter): else: index = self._handle_ast_node(node.slice) return strval[index] + elif isinstance(node, ast.Constant): + return node.value else: raise ValueError("Unrecognized expression syntax")