| 180 | |
| 181 | |
| 182 | class Jinja2TemplateFrame(object): |
| 183 | IS_PLUGIN_FRAME = True |
| 184 | |
| 185 | def __init__(self, frame, original_filename=None, template_lineno=None): |
| 186 | if original_filename is None: |
| 187 | original_filename = _get_jinja2_template_original_filename(frame) |
| 188 | |
| 189 | if template_lineno is None: |
| 190 | template_lineno = _get_jinja2_template_line(frame) |
| 191 | |
| 192 | self.back_context = None |
| 193 | if "context" in frame.f_locals: |
| 194 | # sometimes we don't have 'context', e.g. in macros |
| 195 | self.back_context = frame.f_locals["context"] |
| 196 | self.f_code = FCode("template", original_filename) |
| 197 | self.f_lineno = template_lineno |
| 198 | self.f_back = frame |
| 199 | self.f_globals = {} |
| 200 | self.f_locals = self.collect_context(frame) |
| 201 | self.f_trace = None |
| 202 | |
| 203 | def _get_real_var_name(self, orig_name): |
| 204 | # replace leading number for local variables |
| 205 | parts = orig_name.split("_") |
| 206 | if len(parts) > 1 and parts[0].isdigit(): |
| 207 | return parts[1] |
| 208 | return orig_name |
| 209 | |
| 210 | def collect_context(self, frame): |
| 211 | res = {} |
| 212 | for k, v in frame.f_locals.items(): |
| 213 | if not k.startswith("l_"): |
| 214 | res[k] = v |
| 215 | elif v and not _is_missing(v): |
| 216 | res[self._get_real_var_name(k[2:])] = v |
| 217 | if self.back_context is not None: |
| 218 | for k, v in self.back_context.items(): |
| 219 | res[k] = v |
| 220 | return res |
| 221 | |
| 222 | def _change_variable(self, frame, name, value): |
| 223 | in_vars_or_parents = False |
| 224 | if "context" in frame.f_locals: |
| 225 | if name in frame.f_locals["context"].parent: |
| 226 | self.back_context.parent[name] = value |
| 227 | in_vars_or_parents = True |
| 228 | if name in frame.f_locals["context"].vars: |
| 229 | self.back_context.vars[name] = value |
| 230 | in_vars_or_parents = True |
| 231 | |
| 232 | l_name = "l_" + name |
| 233 | if l_name in frame.f_locals: |
| 234 | if in_vars_or_parents: |
| 235 | frame.f_locals[l_name] = self.back_context.resolve(name) |
| 236 | else: |
| 237 | frame.f_locals[l_name] = value |
| 238 | |
| 239 |
no outgoing calls
no test coverage detected