Skip to content

Commit

Permalink
vendor uncache helper in to our package.
Browse files Browse the repository at this point in the history
  • Loading branch information
cjw296 committed Jan 13, 2020
1 parent 389e48e commit b3d20d4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
29 changes: 29 additions & 0 deletions mock/tests/support.py
@@ -1,3 +1,6 @@
import contextlib
import sys

target = {'foo': 'FOO'}


Expand All @@ -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
2 changes: 1 addition & 1 deletion mock/tests/testpatch.py
Expand Up @@ -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,
Expand Down

0 comments on commit b3d20d4

Please sign in to comment.