diff --git a/mock/tests/support.py b/mock/tests/support.py index 49986d65..40fd3a02 100644 --- a/mock/tests/support.py +++ b/mock/tests/support.py @@ -1,3 +1,6 @@ +import contextlib +import sys + target = {'foo': 'FOO'} @@ -14,3 +17,29 @@ def wibble(self): pass class X(object): pass + + +@contextlib.contextmanager +def uncache(*names): + """Uncache a module from sys.modules. + + A basic sanity check is performed to prevent uncaching modules that either + cannot/shouldn't be uncached. + + """ + for name in names: + if name in ('sys', 'marshal', 'imp'): + raise ValueError( + "cannot uncache {0}".format(name)) + try: + del sys.modules[name] + except KeyError: + pass + try: + yield + finally: + for name in names: + try: + del sys.modules[name] + except KeyError: + pass diff --git a/mock/tests/testpatch.py b/mock/tests/testpatch.py index 15b30688..e9f76a4c 100644 --- a/mock/tests/testpatch.py +++ b/mock/tests/testpatch.py @@ -9,7 +9,7 @@ from mock.tests import support from mock.tests.support import SomeClass, is_instance -from test.test_importlib.util import uncache +from .support import uncache from mock import ( NonCallableMock, sentinel, MagicMock, Mock, NonCallableMagicMock, patch,