Skip to content
Myr

more comments about nsref assignment

Before adoption: this commit came in with the history the project adopted, so no landing records it.

only emit nsref instance check once per ref name refactor primary name parsing a bit

authored by David Lord · committed by David Lord · parent ee83219 · Browse files · 2 files+27 added−15 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+27 added−15 removed

  • other:+27 added−15 removed

2 files+27 added−15 removed

modified:src/jinja2/compiler.py+16 added-8 removed
1580 unmodified lines
15811582 def visit_Assign(self, node: nodes.Assign, frame: Frame) -> None:1583 self.push_assign_tracking()15841585 # NSRef can only ever be used during assignment so we need to check1586 # to make sure that it is only being used to assign using a Namespace.1587 # This check is done here because it is used an expression during the1588 # assignment and therefore cannot have this check done when the NSRef1589 # node is visited1585 # ``a.b`` is allowed for assignment, and is parsed as an NSRef. However,1586 # it is only valid if it references a Namespace object. Emit a check for1587 # that for each ref here, before assignment code is emitted. This can't1588 # be done in visit_NSRef as the ref could be in the middle of a tuple.1589 seen_refs: t.Set[str] = set()15901591 for nsref in node.find_all(nodes.NSRef):1592 if nsref.name in seen_refs:1593 # Only emit the check for each reference once, in case the same1594 # ref is used multiple times in a tuple, `ns.a, ns.b = c, d`.1595 continue15961597 seen_refs.add(nsref.name)1598 ref = frame.symbols.ref(nsref.name)1599 self.writeline(f"if not isinstance({ref}, Namespace):")1600 self.indent()1601 self.writeline(
57 unmodified lines
16591660 self.write(ref)16611662 def visit_NSRef(self, node: nodes.NSRef, frame: Frame) -> None:1656 # NSRefs can only be used to store values; since they use the normal1657 # `foo.bar` notation they will be parsed as a normal attribute access1658 # when used anywhere but in a `set` context1663 # NSRef is a dotted assignment target a.b=c, but uses a[b]=c internally.1664 # visit_Assign emits code to validate that each ref is to a Namespace1665 # object only. That can't be emitted here as the ref could be in the1666 # middle of a tuple assignment.1667 ref = frame.symbols.ref(node.name)1668 self.writeline(f"{ref}[{node.attr!r}]")16691670 def visit_Const(self, node: nodes.Const, frame: Frame) -> None:
328 unmodified lines
modified:src/jinja2/parser.py+11 added-7 removed
639 unmodified lines
640 node = self.parse_filter_expr(node)641 return node642643 def parse_primary(self, with_namespace: bool = False) -> nodes.Expr:644 """Parse a name or literal value. If ``with_namespace`` is enabled, also645 parse namespace attr refs, for use in assignments."""646 token = self.stream.current647 node: nodes.Expr648 if token.type == "name":649 next(self.stream)650 if token.value in ("true", "false", "True", "False"):651 node = nodes.Const(token.value in ("true", "True"), lineno=token.lineno)652 elif token.value in ("none", "None"):653 node = nodes.Const(None, lineno=token.lineno)651 elif with_namespace and self.stream.look().type == "dot":652 next(self.stream) # token653 next(self.stream) # dot654 attr = self.stream.current654 elif with_namespace and self.stream.current.type == "dot":655 # If namespace attributes are allowed at this point, and the next656 # token is a dot, produce a namespace reference.657 next(self.stream)658 attr = self.stream.expect("name")659 node = nodes.NSRef(token.value, attr.value, lineno=token.lineno)660 else:661 node = nodes.Name(token.value, "load", lineno=token.lineno)658 next(self.stream)662 elif token.type == "string":663 next(self.stream)664 buf = [token.value]665 lineno = token.lineno
29 unmodified lines
695 This method could also return a regular expression instead of a tuple696 if no commas where found.697698 The default parsing mode is a full tuple. If `simplified` is `True`696 only names and literals are parsed. The `no_condexpr` parameter is697 forwarded to :meth:`parse_expression`.699 only names and literals are parsed; ``with_namespace`` allows namespace700 attr refs as well. The `no_condexpr` parameter is forwarded to701 :meth:`parse_expression`.702703 Because tuples do not require delimiters and may end in a bogus comma704 an extra hint is needed that marks the end of a tuple. For example705 for loops support tuples between `for` and `in`. In that case the
344 unmodified lines