Skip to content
Myr

Optimize the case where we know about defined variables

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

authored by Armin Ronacher · committed by Armin Ronacher · parent b9655a4 · Browse files · 2 files+17 added−4 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+17 added−4 removed

  • other:+17 added−4 removed

2 files+17 added−4 removed

modified:jinja2/compiler.py+11 added-4 removed
1265 unmodified lines
1266 if node.ctx == 'store' and frame.toplevel:1267 if self._assign_stack and node.name[:1] != '_':1268 self._assign_stack[-1].add(node.name)1269 ref = frame.symbols.ref(node.name)12701271 # If we are looking up a variable we might have to deal with the1272 # case where it's undefined. We can skip that case if the load1273 # instruction indicates a parameter which are always defined.1274 if node.ctx == 'load':1271 self.write('(undefined(name=%r) if %s is missing else %s)' %1272 (node.name, ref, ref))1273 else:1274 self.write(ref)1275 load = frame.symbols.find_load(ref)1276 if load is None or load[0] != VAR_LOAD_PARAMETER:1277 self.write('(undefined(name=%r) if %s is missing else %s)' %1278 (node.name, ref, ref))1279 return12801281 self.write(ref)12821283 def visit_Const(self, node, frame):1284 val = node.value1285 if isinstance(val, float):1286 self.write(str(val))
264 unmodified lines
modified:jinja2/idtracking.py+6 added
43 unmodified lines
44 if load is not None:45 self.loads[ident] = load46 return ident4748 def find_load(self, target):49 if target in self.loads:50 return self.loads[target]51 if self.parent is not None:52 return self.parent.find_load(target)5354 def find_ref(self, name):55 if name in self.refs:56 return self.refs[name]57 if self.parent is not None:
181 unmodified lines