| 244 | class CodeGenerator(NodeVisitor): |
| 245 | |
| 246 | def __init__(self, environment, name, filename, stream=None, |
| 247 | defer_init=False, optimized=True): |
| 248 | if stream is None: |
| 249 | stream = NativeStringIO() |
| 250 | self.environment = environment |
| 251 | self.name = name |
| 252 | self.filename = filename |
| 253 | self.stream = stream |
| 254 | self.created_block_context = False |
| 255 | self.defer_init = defer_init |
| 256 | self.optimized = optimized |
| 257 | if optimized: |
| 258 | self.optimizer = Optimizer(environment) |
| 259 | |
| 260 | # aliases for imports |
| 261 | self.import_aliases = {} |
| 262 | |
| 263 | # a registry for all blocks. Because blocks are moved out |
| 264 | # into the global python scope they are registered here |
| 265 | self.blocks = {} |
| 266 | |
| 267 | # the number of extends statements so far |
| 268 | self.extends_so_far = 0 |
| 269 | |
| 270 | # some templates have a rootlevel extends. In this case we |
| 271 | # can safely assume that we're a child template and do some |
| 272 | # more optimizations. |
| 273 | self.has_known_extends = False |
| 274 | |
| 275 | # the current line number |
| 276 | self.code_lineno = 1 |
| 277 | |
| 278 | # registry of all filters and tests (global, not block local) |
| 279 | self.tests = {} |
| 280 | self.filters = {} |
| 281 | |
| 282 | # the debug information |
| 283 | self.debug_info = [] |
| 284 | self._write_debug_info = None |
| 285 | |
| 286 | # the number of new lines before the next write() |
| 287 | self._new_lines = 0 |
| 288 | |
| 289 | # the line number of the last written statement |
| 290 | self._last_line = 0 |
| 291 | |
| 292 | # true if nothing was written so far. |
| 293 | self._first_write = True |
| 294 | |
| 295 | # used by the `temporary_identifier` method to get new |
| 296 | # unique, temporary identifier |
| 297 | self._last_identifier = 0 |
| 298 | |
| 299 | # the current indentation |
| 300 | self._indentation = 0 |
| 301 | |
| 302 | # Tracks toplevel assignments |
| 303 | self._assign_stack = [] |