From 116b91c07ad2f2db449803759fdb27e5595e4a2c Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Sat, 10 Nov 2007 12:40:38 +0100 Subject: [PATCH] added `MemoryGuard` to jdebug --HG-- branch : trunk --- jdebug.py | 29 ++++++++++++++++++++++++++++- jinja/datastructure.py | 1 + 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/jdebug.py b/jdebug.py index 22ea06d..5c4f781 100644 --- a/jdebug.py +++ b/jdebug.py @@ -10,13 +10,14 @@ """ import os import sys +import gc from jinja import Environment from jinja.parser import Parser from jinja.lexer import Lexer from jinja.translators.python import PythonTranslator -__all__ = ['e', 't', 'p', 'l'] +__all__ = ['e', 't', 'p', 'l', 'm'] e = Environment() t = e.from_string @@ -44,6 +45,32 @@ def l(x): item.type, item.value) +class MemoryGuard(object): + + def __init__(self): + self.guarded_objects = {} + self.clear = self.guarded_objects.clear + + def freeze(self): + self.clear() + self.update() + + def update(self): + self.guarded_objects.clear() + for obj in gc.get_objects(): + self.guarded_objects[id(obj)] = True + + def get_delta(self): + result = [] + for obj in gc.get_objects(): + if id(obj) not in self.guarded_objects: + result.append(obj) + return result + + +m = MemoryGuard() + + if __name__ == '__main__': if len(sys.argv) > 1: from jinja import FileSystemLoader diff --git a/jinja/datastructure.py b/jinja/datastructure.py index 8ad6f5a..8e07a03 100644 --- a/jinja/datastructure.py +++ b/jinja/datastructure.py @@ -535,6 +535,7 @@ class TokenStreamIterator(object): def next(self): token = self._stream.current if token.type == 'eof': + self._stream.close() raise StopIteration() self._stream.next() return token -- 2.26.2