| 52 | self.modules_to_cleanup = set() # Populated by mkhier(). |
| 53 | |
| 54 | def tearDown(self): |
| 55 | sys.path[:] = self.syspath |
| 56 | for modulename in self.modules_to_cleanup: |
| 57 | if modulename in sys.modules: |
| 58 | del sys.modules[modulename] |
| 59 | if self.root: # Only clean if the test was actually run |
| 60 | cleanout(self.root) |
| 61 | |
| 62 | # delete all modules concerning the tested hierarchy |
| 63 | if self.pkgname: |
| 64 | modules = [name for name in sys.modules |
| 65 | if self.pkgname in name.split('.')] |
| 66 | for name in modules: |
| 67 | del sys.modules[name] |
| 68 | |
| 69 | def run_code(self, code): |
| 70 | exec(textwrap.dedent(code), globals(), {"self": self}) |