looking for a jinja memleak
Before adoption: this commit came in with the history the project adopted, so no landing records it.
--HG-- branch : trunk
authored by Armin Ronacher · committed by Armin Ronacher · parent 116b91c · Browse files · 3 files·+36 added−40 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+36 added−40 removed
- other:+36 added−40 removed
Provenance
- Commit
525c704- Tree
11a4cc2- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
116b91c
3 files changed+36 added−40 removed
modified:jinja/translators/python.py+1 added-2 removed
267 unmodified lines268 """269 if self.closed:270 raise RuntimeError('translator is closed')271 if node.__class__ in self.handlers:272- out = self.handlers[node.__class__](node)272+ return self.handlers[node.__class__](node)273 else:274 raise AssertionError('unhandled node %r' % node.__class__)275- return out275 276 def close(self):277 """278 Clean up stuff.888 unmodified linesmodified:jinja/parser.py+30 added-22 removed
1115 unmodified lines1116 self.stream.lineno, self.filename)1117 1118 return assemble_list()1119 1120+ def sanitize_tree(self, body, extends):1121+ self._sanitize_tree([body], [body], extends, body)1122+ return body1123+1124+ def _sanitize_tree(self, nodelist, stack, extends, body):1125+ """1126+ This is not a closure because python leaks memory if it is. It's used1127+ by `parse()` to make sure blocks do not trigger unexpected behavior.1128+ """1129+ for node in nodelist:1130+ if extends is not None and \1131+ node.__class__ is nodes.Block and \1132+ stack[-1] is not body:1133+ for n in stack:1134+ if n.__class__ is nodes.Block:1135+ break1136+ else:1137+ raise TemplateSyntaxError('misplaced block %r, '1138+ 'blocks in child '1139+ 'templates must be '1140+ 'either top level or '1141+ 'located in a block '1142+ 'tag.' % node.name,1143+ node.lineno,1144+ self.filename)1145+ stack.append(node)1146+ self._sanitize_tree(node.get_child_nodes(), stack, extends, body)1147+ stack.pop()1148+1149 def parse(self):1150 """1151 Parse the template and return a Template node. This also does some1152 post processing sanitizing and parses for an extends tag.17 unmodified lines1170 extends = None1171 if leading_whitespace:1172 self.stream.shift(leading_whitespace)1173 1145- body = self.subparse(None)1146- def walk(nodelist, stack):1147- for node in nodelist:1148- if extends is not None and \1149- node.__class__ is nodes.Block and \1150- stack[-1] is not body:1151- for n in stack:1152- if n.__class__ is nodes.Block:1153- break1154- else:1155- raise TemplateSyntaxError('misplaced block %r, '1156- 'blocks in child '1157- 'templates must be '1158- 'either top level or '1159- 'located in a block '1160- 'tag.' % node.name,1161- node.lineno,1162- self.filename)1163- stack.append(node)1164- walk(node.get_child_nodes(), stack)1165- stack.pop()1166- walk([body], [body])1174+ body = self.sanitize_tree(self.subparse(None), extends)1175 return nodes.Template(extends, body, 1, self.filename)1176 finally:1177 self.close()1178 5 unmodified linesmodified:jdebug.py+5 added-16 removed
17 unmodified lines18 19 20 __all__ = ['e', 't', 'p', 'l', 'm']21 22+23+_global_frame = sys._getframe()24 e = Environment()25 t = e.from_string26 27 26-if os.environ.get('JDEBUG_SOURCEPRINT'):27- original_translate = PythonTranslator.translate28-29- def debug_translate(self):30- rv = original_translate(self)31- sys.stderr.write('## GENERATED SOURCE:\n%s\n' % rv)32- return rv33-34- PythonTranslator.translate = debug_translate35-36-28 def p(x=None, f=None):29 if x is None and f is not None:30 x = e.loader.get_source(f)31 print PythonTranslator(e, Parser(e, x, f).parse(), None).translate()11 unmodified lines43 self.clear = self.guarded_objects.clear44 45 def freeze(self):46 self.clear()56- self.update()57-58- def update(self):59- self.guarded_objects.clear()47 for obj in gc.get_objects():48 self.guarded_objects[id(obj)] = True49 50 def get_delta(self):51+ frm = sys._getframe()52 result = []53 for obj in gc.get_objects():66- if id(obj) not in self.guarded_objects:54+ if id(obj) not in self.guarded_objects and \55+ obj is not frm and obj is not result:56 result.append(obj)57 return result58 59 11 unmodified lines