8000 Implement backend for PyQt5 + modify Qt4 backends to use Qt5 module via shim by mfitzp · Pull Request #3072 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Implement backend for PyQt5 + modify Qt4 backends to use Qt5 module via shim #3072

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 27, 2014
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix setupext.py for the case where both PyQt4 and PyQt5 are installed
If both PyQt4 and PyQt5 are installed in the same Python the setup check
for the modules will cause a RuntimeError as they are both imported.

The Gtk module uses a multiprocessing pool to perform the imports, and this
approach is used here. However, if we import PyQt4 normally, then PyQt5
using multiprocessing the import is incomplete (no version information).
Instead, we default to multiprocessing for both PyQt4 and PyQt5 and only
fall to normal imports if that is not available.

In the case of a RuntimeError on a normal-style import the error is raised
as `CheckFailed("Could not import: are PyQt4 & PyQt5 both installed?")` to
give a hint as to the problem.
  • Loading branch information
Martin Fitzpatrick committed May 20, 2014
commit c98406da768ac03d50c8582efe8b21ee1e7e4e5f
105 changes: 75 additions & 30 deletions setupext.py
Original file line number Diff line number Diff line change
Expand Up @@ -1852,8 +1852,7 @@ def get_extension(self):
return ext


class BackendQt4(OptionalBackendPackage):
name = "qt4agg"
class BackendQtBase(OptionalBackendPackage):

def convert_qt_version(self, version):
version = '%x' % version
Expand All @@ -1864,38 +1863,84 @@ def convert_qt_version(self, version):
return '.'.join(temp)

def check_requirements(self):
'''
If PyQt4/PyQt5 is already imported, importing PyQt5/PyQt4 will fail
so we need to test in a subprocess (as for Gtk3).
'''
try:
from PyQt4 import QtCore
except ImportError:
raise CheckFailed("PyQt4 not found")
# Import may still be broken for our python
try:
qt_version = QtCore.QT_VERSION
pyqt_version_str = QtCore.PYQT_VERSION_STR
except AttributeError:
raise CheckFailed('PyQt4 not correctly imported')
BackendAgg.force = True
return ("Qt: %s, PyQt4: %s" %
(self.convert_qt_version(
qt_version),
pyqt_version_str))
p = multiprocessing.Pool()

except:
# Can't do multiprocessing, fall back to normal approach ( this will fail if importing both PyQt4 and PyQt5 )
try:
# Try in-process
msg = self.callback(self)

except RuntimeError:
raise CheckFailed("Could not import: are PyQt4 & PyQt5 both installed?")

except:
# Raise any other exceptions
raise

else:
# Multiprocessing OK
try:
msg = p.map(self.callback, [self])[0]
except:
# If we hit an error on multiprocessing raise it
raise
finally:
# Tidy up multiprocessing
p.close()
p.join()

return msg


def backend_qt4_internal_check(self):
try:
from PyQt4 import QtCore
except ImportError:
raise CheckFailed("PyQt4 not found")
try:
qt_version = QtCore.QT_VERSION
pyqt_version_str = QtCore.QT_VERSION_STR
except AttributeError:
raise CheckFailed('PyQt4 not correctly imported')

return ("Qt: %s, PyQt: %s" % (self.convert_qt_version(qt_version), pyqt_version_str))


class BackendQt5(BackendQt4):
class BackendQt4(BackendQtBase):
name = "qt4agg"

def __init__(self, *args, **kwargs):
BackendQtBase.__init__(self, *args, **kwargs)
self.callback = backend_qt4_internal_check


def backend_qt5_internal_check(self):
try:
from PyQt5 import QtCore
except ImportError:
raise CheckFailed("PyQt5 not found")
try:
qt_version = QtCore.QT_VERSION
pyqt_version_str = QtCore.QT_VERSION_STR
except AttributeError:
raise CheckFailed('PyQt5 not correctly imported')

return ("Qt: %s, PyQt: %s" % (self.convert_qt_version(qt_version), pyqt_version_str))


class BackendQt5(BackendQtBase):
name = "qt5agg"

def check_requirements(self):
try:
from PyQt5 import QtCore
except ImportError:
raise CheckFailed("PyQt5 not found")
# Import may still be broken for our python
try:
qtconfig = QtCore.PYQT_CONFIGURATION
except AttributeError:
raise CheckFailed('PyQt5 not correctly imported')
BackendAgg.force = True
# FIXME: How to return correct version information?
return ("Qt: 5, PyQt5: %s" % (QtCore.PYQT_VERSION_STR) )
def __init__(self, *args, **kwargs):
BackendQtBase.__init__(self, *args, **kwargs)
self.callback = backend_qt5_internal_check


class BackendPySide(OptionalBackendPackage):
name = "pyside"
Expand Down
0