Commits

Anonymous committed 6c82ae8

[gsoc2009-testing] Adding my global importer which can force a reload of all modules. (so coverage can account for startup as well)

  • Participants
  • Parent commits 9a6f988
  • Branches soc2009/test-improvements

Comments (0)

Files changed (1)

File django/test/rollback_importer.py

+class RollbackImporter:
+    def __init__(self):
+        "Creates an instance and installs as the global importer"
+        self.previousModules = sys.modules.copy()
+        self.realImport = __builtin__.__import__
+        __builtin__.__import__ = self._import
+        self.newModules = {}
+
+    def _import(self, name, globals=None, locals=None, fromlist=[]):
+        result = apply(self.realImport, (name, globals, locals, fromlist))
+        self.newModules[name] = 1
+        return result
+        
+    def uninstall(self):
+        for modname in self.newModules.keys():
+            if not self.previousModules.has_key(modname):
+                # Force reload when modname next imported
+                del(sys.modules[modname])
+        __builtin__.__import__ = self.realImport