##// END OF EJS Templates
ImportDenier: implement modern interface
Nikita Kniazev -
Show More
1 NO CONTENT: new file 100644
NO CONTENT: new file 100644
@@ -0,0 +1,11 b''
1 import importlib
2 import pytest
3 from IPython.external.qt_loaders import ID
4
5
6 def test_import_denier():
7 ID.forbid("ipython_denied_module")
8 with pytest.raises(ImportError, match="disabled by IPython"):
9 import ipython_denied_module
10 with pytest.raises(ImportError, match="disabled by IPython"):
11 importlib.import_module("ipython_denied_module")
@@ -8,6 +8,7 b' bindings, which is unstable and likely to crash'
8 This is used primarily by qt and qt_for_kernel, and shouldn't
8 This is used primarily by qt and qt_for_kernel, and shouldn't
9 be accessed directly from the outside
9 be accessed directly from the outside
10 """
10 """
11 import importlib.abc
11 import sys
12 import sys
12 import types
13 import types
13 from functools import partial, lru_cache
14 from functools import partial, lru_cache
@@ -47,7 +48,7 b' api_to_module = {'
47 }
48 }
48
49
49
50
50 class ImportDenier(object):
51 class ImportDenier(importlib.abc.MetaPathFinder):
51 """Import Hook that will guard against bad Qt imports
52 """Import Hook that will guard against bad Qt imports
52 once IPython commits to a specific binding
53 once IPython commits to a specific binding
53 """
54 """
@@ -59,14 +60,12 b' class ImportDenier(object):'
59 sys.modules.pop(module_name, None)
60 sys.modules.pop(module_name, None)
60 self.__forbidden.add(module_name)
61 self.__forbidden.add(module_name)
61
62
62 def find_module(self, fullname, path=None):
63 def find_spec(self, fullname, path, target=None):
63 if path:
64 if path:
64 return
65 return
65 if fullname in self.__forbidden:
66 if fullname in self.__forbidden:
66 return self
67 raise ImportError(
67
68 """
68 def load_module(self, fullname):
69 raise ImportError("""
70 Importing %s disabled by IPython, which has
69 Importing %s disabled by IPython, which has
71 already imported an Incompatible QT Binding: %s
70 already imported an Incompatible QT Binding: %s
72 """ % (fullname, loaded_api()))
71 """ % (fullname, loaded_api()))
General Comments 0
You need to be logged in to leave comments. Login now