bring uncache helper across from cpython
diff --git a/mock/tests/support.py b/mock/tests/support.py
index c7ad20b..2bbc8a2 100644
--- a/mock/tests/support.py
+++ b/mock/tests/support.py
@@ -1,3 +1,7 @@
+import contextlib
+import sys
+
+
def is_instance(obj, klass):
"""Version of is_instance that doesn't access __class__"""
return issubclass(type(obj), klass)
@@ -12,3 +16,29 @@
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 d4aef87..434e780 100644
--- a/mock/tests/testpatch.py
+++ b/mock/tests/testpatch.py
@@ -9,7 +9,7 @@
import unittest
from mock.tests import support
-from mock.tests.support import SomeClass, is_instance
+from mock.tests.support import SomeClass, is_instance, uncache
from mock import (
NonCallableMock, CallableMixin, patch, sentinel,