added `MemoryGuard` to jdebug
authorArmin Ronacher <armin.ronacher@active-4.com>
Sat, 10 Nov 2007 11:40:38 +0000 (12:40 +0100)
committerArmin Ronacher <armin.ronacher@active-4.com>
Sat, 10 Nov 2007 11:40:38 +0000 (12:40 +0100)
--HG--
branch : trunk

jdebug.py
jinja/datastructure.py

index 22ea06d0c1bce42201333fa20c25da2dd589a2bc..5c4f7818ffd730341ea0110c85e1f7228e3f7440 100644 (file)
--- a/jdebug.py
+++ b/jdebug.py
 """
 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
index 8ad6f5a66bdb94bc26131f7d1095c686bd2801a0..8e07a0318087fb9532f7c5ec447ea77fe71a7fe1 100644 (file)
@@ -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