##// END OF EJS Templates
ImportDenier: implement modern interface
Nikita Kniazev -
Show More
1 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 8 This is used primarily by qt and qt_for_kernel, and shouldn't
9 9 be accessed directly from the outside
10 10 """
11 import importlib.abc
11 12 import sys
12 13 import types
13 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 52 """Import Hook that will guard against bad Qt imports
52 53 once IPython commits to a specific binding
53 54 """
@@ -59,14 +60,12 b' class ImportDenier(object):'
59 60 sys.modules.pop(module_name, None)
60 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 64 if path:
64 65 return
65 66 if fullname in self.__forbidden:
66 return self
67
68 def load_module(self, fullname):
69 raise ImportError("""
67 raise ImportError(
68 """
70 69 Importing %s disabled by IPython, which has
71 70 already imported an Incompatible QT Binding: %s
72 71 """ % (fullname, loaded_api()))
General Comments 0
You need to be logged in to leave comments. Login now