From 5e64e05c9370f82528654dcacee0d162889ce94a Mon Sep 17 00:00:00 2001 From: cocolato Date: Sun, 27 Oct 2024 23:47:53 +0800 Subject: [PATCH] Revise the variable names and reduce the redundant code --- mako/codegen.py | 63 ++++++++++++++++++++++++---------------------- mako/filters.py | 3 ++- mako/pyparser.py | 12 ++++++--- test/test_ast.py | 18 +++++++++++-- test/test_lexer.py | 2 +- 5 files changed, 60 insertions(+), 38 deletions(-) diff --git a/mako/codegen.py b/mako/codegen.py index ff94748..c3804e6 100644 --- a/mako/codegen.py +++ b/mako/codegen.py @@ -16,7 +16,7 @@ from mako import filters from mako import parsetree from mako import util -from mako.filters import CONFLICT_PREFIX +from mako.filters import DEFAULT_ESCAPE_PREFIX from mako.pygen import PythonPrinter @@ -27,6 +27,7 @@ # context itself TOPLEVEL_DECLARED = {"UNDEFINED", "STOP_RENDERING"} RESERVED_NAMES = {"context", "loop"}.union(TOPLEVEL_DECLARED) +DEFAULT_ESCAPED_N = "%sn" % DEFAULT_ESCAPE_PREFIX def compile( # noqa @@ -523,8 +524,7 @@ def write_variable_declares(self, identifiers, toplevel=False, limit=None): self.printer.writeline("loop = __M_loop = runtime.LoopStack()") for ident in to_write: - if ident.startswith(CONFLICT_PREFIX): - ident = ident.replace(CONFLICT_PREFIX, "") + ident = ident.replace(DEFAULT_ESCAPE_PREFIX, "") if ident in comp_idents: comp = comp_idents[ident] if comp.is_block: @@ -788,45 +788,48 @@ def locate_encode(name): else: return filters.DEFAULT_ESCAPES.get(name, name) - filter_args = [] - conflict_n = "%sn" % CONFLICT_PREFIX - if conflict_n not in args: + filter_args = set() + if DEFAULT_ESCAPED_N not in args: if is_expression: if self.compiler.pagetag: args = self.compiler.pagetag.filter_args.args + args - filter_args = self.compiler.pagetag.filter_args.args - if self.compiler.default_filters and conflict_n not in args: + filter_args = set(self.compiler.pagetag.filter_args.args) + if ( + self.compiler.default_filters + and DEFAULT_ESCAPED_N not in args + ): args = self.compiler.default_filters + args for e in args: - if e == conflict_n: + if e == DEFAULT_ESCAPED_N: continue - if e.startswith(CONFLICT_PREFIX): - if e not in filter_args: - ident = e.replace(CONFLICT_PREFIX, "") - m = re.match(r"(.+?)(\(.*\))", e) - if m: - target = "%s(%s)" % (ident, target) - continue - target = "%s(%s) if %s is not UNDEFINED else %s(%s)" % ( - ident, - target, - ident, - locate_encode(ident), - target, - ) - continue - e = e.replace(CONFLICT_PREFIX, "") + + if e.startswith(DEFAULT_ESCAPE_PREFIX): + render_e = e.replace(DEFAULT_ESCAPE_PREFIX, "") + is_default_filter = True + else: + render_e = e + is_default_filter = False # if filter given as a function, get just the identifier portion m = re.match(r"(.+?)(\(.*\))", e) if m: - ident, fargs = m.group(1, 2) - f = locate_encode(ident) - e = f + fargs + if not is_default_filter: + ident, fargs = m.group(1, 2) + f = locate_encode(ident) + render_e = f + fargs + target = "%s(%s)" % (render_e, target) + elif is_default_filter and e not in filter_args: + target = "%s(%s) if %s is not UNDEFINED else %s(%s)" % ( + render_e, + target, + render_e, + locate_encode(render_e), + target, + ) else: - e = locate_encode(e) + e = locate_encode(render_e) assert e is not None - target = "%s(%s)" % (e, target) + target = "%s(%s)" % (e, target) return target def visitExpression(self, node): diff --git a/mako/filters.py b/mako/filters.py index 47faa42..33575e0 100644 --- a/mako/filters.py +++ b/mako/filters.py @@ -162,4 +162,5 @@ def htmlentityreplace_errors(ex): "n": "n", } -CONFLICT_PREFIX = "__ALIAS_" + +DEFAULT_ESCAPE_PREFIX = "__DEFAULT_ESCAPE_" diff --git a/mako/pyparser.py b/mako/pyparser.py index 3786c80..f5ed367 100644 --- a/mako/pyparser.py +++ b/mako/pyparser.py @@ -18,7 +18,7 @@ from mako import compat from mako import exceptions from mako import util -from mako.filters import CONFLICT_PREFIX +from mako.filters import DEFAULT_ESCAPE_PREFIX from mako.filters import DEFAULT_ESCAPES # words that cannot be assigned to (notably @@ -203,13 +203,17 @@ def visit_Tuple(self, node): DEFAULT_ESCAPES ) if conflict_identifiers: - _map = {i: CONFLICT_PREFIX + i for i in conflict_identifiers} + _map = { + i: DEFAULT_ESCAPE_PREFIX + i for i in conflict_identifiers + } for i, arg in enumerate(self.listener.args): if arg in _map: self.listener.args[i] = _map[arg] self.listener.undeclared_identifiers = ( - undeclared_identifiers ^ conflict_identifiers - ).union(_map.values()) + undeclared_identifiers.symmetric_difference( + conflict_identifiers + ).union(_map.values()) + ) else: self.listener.undeclared_identifiers = undeclared_identifiers diff --git a/test/test_ast.py b/test/test_ast.py index 884c99d..079c411 100644 --- a/test/test_ast.py +++ b/test/test_ast.py @@ -298,9 +298,23 @@ def test_argument_list(self): def test_conflict_argument_list(self): parsed = ast.ArgumentList( - "3, 5, 'hi', n+5, " "context.get('lala')", **exception_kwargs + "x-2, h*2, '(u)', n+5, trim, entity, unicode, decode, str, other", + **exception_kwargs, + ) + eq_( + parsed.undeclared_identifiers, + { + "__DEFAULT_ESCAPE_trim", + "__DEFAULT_ESCAPE_h", + "__DEFAULT_ESCAPE_decode", + "__DEFAULT_ESCAPE_unicode", + "__DEFAULT_ESCAPE_x", + "__DEFAULT_ESCAPE_str", + "__DEFAULT_ESCAPE_entity", + "__DEFAULT_ESCAPE_n", + "other", + }, ) - eq_(parsed.undeclared_identifiers, {"__ALIAS_n", "context"}) def test_function_decl(self): """test getting the arguments from a function""" diff --git a/test/test_lexer.py b/test/test_lexer.py index c0a9a8e..28ba3cc 100644 --- a/test/test_lexer.py +++ b/test/test_lexer.py @@ -1327,7 +1327,7 @@ def test_integration(self): Text(" \n", (14, 1)), ControlLine("for", "for x in j:", False, (15, 1)), Text(" Hello ", (16, 1)), - Expression("x", ["__ALIAS_h"], (16, 23)), + Expression("x", ["__DEFAULT_ESCAPE_h"], (16, 23)), Text("\n", (16, 30)), ControlLine("for", "endfor", True, (17, 1)), Text(" \n", (18, 1)),