Skip to content

Commit

Permalink
Merge pull request #7 from astrofrog/forbidden-class-attribute
Browse files Browse the repository at this point in the history
Make _forbidden a class attribute for ImportDenier
  • Loading branch information
astrofrog committed Feb 23, 2016
2 parents 8102c34 + a47fdf0 commit 6f51456
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 25 deletions.
36 changes: 22 additions & 14 deletions qt_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,20 +80,16 @@ def is_pyqt5():
is_pyqt = is_pyqt4
QT_API_PYQT = QT_API_PYQT4

_forbidden = set()


def deny_module(module):
_forbidden.add(module)


class ImportDenier(object):
"""
Import hook to protect importing of both PySide and PyQt.
"""

_forbidden = set()

def find_module(self, mod_name, pth):
if pth or not mod_name in _forbidden:
if pth or not mod_name in self._forbidden:
return
else:
return self
Expand All @@ -102,6 +98,8 @@ def load_module(self, mod_name):
raise ImportError("Importing %s forbidden by %s"
% (mod_name, __name__))

def deny_module(self, module):
self._forbidden.add(module)

_import_hook = ImportDenier()
sys.meta_path.append(_import_hook)
Expand Down Expand Up @@ -154,8 +152,8 @@ def _load_pyqt4():
global QT_API
QT_API = QT_API_PYQT4

deny_module('PySide')
deny_module('PyQt5')
_import_hook.deny_module('PySide')
_import_hook.deny_module('PyQt5')


def _load_pyqt5():
Expand Down Expand Up @@ -185,8 +183,8 @@ def _load_pyqt5():
global QT_API
QT_API = QT_API_PYQT5

deny_module('PySide')
deny_module('PyQt4')
_import_hook.deny_module('PySide')
_import_hook.deny_module('PyQt4')


def _load_pyside():
Expand All @@ -208,8 +206,8 @@ def setMargin(self, x):
global QT_API
QT_API = QT_API_PYSIDE

deny_module('PyQt4')
deny_module('PyQt5')
_import_hook.deny_module('PyQt4')
_import_hook.deny_module('PyQt5')


QtCore = None
Expand All @@ -227,7 +225,14 @@ def reload_qt():
e.g. PySide if PyQt4 is loaded).
"""

_forbidden.clear()
# Clear any forbidden modules
_import_hook._forbidden.clear()

# Quit app if active
global qapp
if qapp is not None:
qapp.quit()
qapp = None

global QtCore
global QtGui
Expand Down Expand Up @@ -298,7 +303,10 @@ def _load_ui_pyqt5(path, parent):
return loadUi(path, parent)


qapp = None

def get_qapp(icon_path=None):
global qapp
qapp = QtGui.QApplication.instance()
if qapp is None:
qapp = QtGui.QApplication([''])
Expand Down
13 changes: 2 additions & 11 deletions test_switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
class TestQT(object):

def teardown_class(cls):
for m in sys.modules.keys():
for m in list(sys.modules.keys()):
if m.startswith('PyQt4') or m.startswith('PySide'):
sys.modules.pop(m)

def setup_method(self, method):
qt.deny_module(None)
qt._import_hook.deny_module(None)
if 'QT_API' in os.environ:
os.environ.pop('QT_API')

Expand Down Expand Up @@ -62,16 +62,13 @@ def test_load_ui_qt4(self):
app = get_qapp()
load_ui('test.ui')
app.quit()
del app

def test_load_ui_pyside(self):
self._load_pyside()
from qt_helpers import load_ui, get_qapp
app = get_qapp()
load_ui('test.ui')
app.exit()
app.quit()
del app

def test_submodule_import(self):
self._load_qt4()
Expand Down Expand Up @@ -140,8 +137,6 @@ def test_launch_after_reload(self):
time.sleep(0.1)
app.quit()

del app

os.environ['QT_API'] = qt.QT_API_PYQT4
qt.reload_qt()

Expand All @@ -155,8 +150,6 @@ def test_launch_after_reload(self):
time.sleep(0.1)
app.quit()

del app

os.environ['QT_API'] = qt.QT_API_PYSIDE
qt.reload_qt()

Expand All @@ -169,5 +162,3 @@ def test_launch_after_reload(self):
app.flush()
time.sleep(0.1)
app.quit()

del app

0 comments on commit 6f51456

Please sign in to comment.