Skip to content

Commit

Permalink
TST: Fixed block_import not blocking subpackages
Browse files Browse the repository at this point in the history
  • Loading branch information
richardjgowers authored and kain88-de committed Jan 13, 2017
1 parent 981cc7a commit d2453ac
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
2 changes: 1 addition & 1 deletion testsuite/MDAnalysisTests/analysis/test_encore.py
Original file line number Diff line number Diff line change
Expand Up @@ -850,7 +850,7 @@ class TestEncoreImportWarnings(object):
def setUp(self):
# clear cache of encore module
for mod in list(sys.modules): # list as we're changing as we iterate
if '.encore' in mod or 'sklearn' in mod or 'scipy' in mod:
if 'encore' in mod:
sys.modules.pop(mod, None)

@block_import('sklearn')
Expand Down
7 changes: 5 additions & 2 deletions testsuite/MDAnalysisTests/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,13 @@ def block_import(package):
eg:
@blocker('numpy')
@block_import('numpy')
def try_and_do_something():
import numpy as np # this will fail!
Will also block imports of subpackages ie block_import('numpy') should
block 'import numpy.matrix'
Shadows the builtin import method, sniffs import requests
and blocks the designated package.
"""
Expand All @@ -55,7 +58,7 @@ def func_wrapper(*args, **kwargs):
with mock.patch('{}.__import__'.format(builtins_name),
wraps=importer) as mbi:
def blocker(*args, **kwargs):
if package in args:
if package in args[0]:
raise ImportError("Blocked by block_import")
else:
# returning DEFAULT allows the real function to continue
Expand Down

0 comments on commit d2453ac

Please sign in to comment.