[Pkg-bitcoin-commits] [python-quamash] 97/269: Now working on both PyQt5 and PySide (on windows at least)

Jonas Smedegaard dr at jones.dk
Fri Nov 24 11:26:20 UTC 2017


This is an automated email from the git hooks/post-receive script.

js pushed a commit to branch master
in repository python-quamash.

commit 8416818047ecdb9fcc55b408938a90d78935c0df
Author: Mark Harviston <mark.harviston at gmail.com>
Date:   Thu Jul 17 15:03:40 2014 -0700

    Now working on both PyQt5 and PySide (on windows at least)
    
    - PySide can only have one QApplication per process period (not PyQt5's one app running at a time)
    - PyQt5 preferred over PySide in all cases (systems shouldn't have both installed, this is more for consistency)
    - QCoreApplication.instance() won't create an app if one doesn't exist.
    - getting an app instance moved to a helper function (real apps should pass in a QApplication instance specifically and use sys.argv and all that)
---
 quamash/__init__.py       | 34 +++++++++++++++++++---------------
 tests/test_qeventloop.py  |  4 +++-
 tests/test_qthreadexec.py |  7 -------
 3 files changed, 22 insertions(+), 23 deletions(-)

diff --git a/quamash/__init__.py b/quamash/__init__.py
index 63895e4..ee3cffc 100644
--- a/quamash/__init__.py
+++ b/quamash/__init__.py
@@ -19,14 +19,24 @@ from queue import Queue
 from concurrent.futures import Future
 
 try:
-	from PySide import QtCore
-except ImportError:
 	from PyQt5 import QtCore
 	QtCore.Signal = QtCore.pyqtSignal
+except ImportError:
+	from PySide import QtCore
 
 from ._common import with_logger
 
 
+def _get_test_app():
+	"""Helper function to get an app instance."""
+	try:
+		from PyQt5.QtWidgets import QApplication
+	except ImportError:
+		from PySide.QtGui import QApplication
+
+	return QtCore.QCoreApplication.instance() or QApplication([])
+
+
 @with_logger
 class _QThreadWorker(QtCore.QThread):
 	"""
@@ -143,11 +153,7 @@ def _easycallback(fn):
 	>>> try: from PyQt5.QtCore import QThread, QObject
 	... except ImportError: from PySide.QtCore import QThread, QObject
 	>>>
-	>>> try: from PyQt5.QtWidgets import QApplication
-	... except ImportError: from PySide.QtGui import QApplication
-	>>>
-	>>> app = QApplication([])
-	>>>
+	>>> import quamash
 	>>> from quamash import QEventLoop, _easycallback
 	>>> import asyncio
 	>>>
@@ -169,7 +175,7 @@ def _easycallback(fn):
 	... def mycoroutine():
 	...     myobject.mycallback()
 	>>>
-	>>> loop = QEventLoop(app)
+	>>> loop = QEventLoop(quamash._get_test_app())
 	>>> with loop:
 	...     loop.run_until_complete(mycoroutine())
 	"""
@@ -198,10 +204,6 @@ class QEventLoop(_baseclass):
 	"""
 	Implementation of asyncio event loop that uses the Qt Event loop
 	>>> import quamash, asyncio
-	>>> try: from PyQt5.QtWidgets import QApplication
-	... except ImportError: from PySide.QtCore import QApplication
-	>>>
-	>>> app = QApplication([])
 	>>>
 	>>> @asyncio.coroutine
 	... def xplusy(x, y):
@@ -209,17 +211,19 @@ class QEventLoop(_baseclass):
 	...     assert x + y == 4
 	...     yield from asyncio.sleep(.1)
 	>>>
-	>>> with QEventLoop(app) as loop:
+	>>> with QEventLoop(quamash._get_test_app()) as loop:
 	...     loop.run_until_complete(xplusy(2,2))
 	"""
-	def __init__(self, app):
+	def __init__(self, app=None):
 		self.__timers = []
-		self.__app = app
+		self.__app = app or QtCore.QCoreApplication.instance()
 		self.__is_running = False
 		self.__debug_enabled = False
 		self.__default_executor = None
 		self.__exception_handler = None
 
+		assert self.__app is not None
+
 		super().__init__()
 
 	def run_forever(self):
diff --git a/tests/test_qeventloop.py b/tests/test_qeventloop.py
index f2d9658..0553405 100644
--- a/tests/test_qeventloop.py
+++ b/tests/test_qeventloop.py
@@ -6,8 +6,10 @@ from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor  # noqa
 
 try:
 	from PyQt5.QtWidgets import QApplication
+	from PyQt5.QtCore import QCoreApplication
 except ImportError:
 	from PySide.QtGui import QApplication
+	from PySide.QtCore import QCoreApplication
 import pytest
 
 import quamash
@@ -29,7 +31,7 @@ class _SubprocessProtocol(asyncio.SubprocessProtocol):
 
 @pytest.fixture(scope='session')
 def application():
-	app = QApplication([])
+	app = QCoreApplication.instance() or QApplication([])
 	return app
 
 
diff --git a/tests/test_qthreadexec.py b/tests/test_qthreadexec.py
index fc03810..1fd3526 100644
--- a/tests/test_qthreadexec.py
+++ b/tests/test_qthreadexec.py
@@ -1,10 +1,3 @@
-'''
-try:
-	from PyQt5.QtWidgets import QApplication
-except ImportError:
-	from PySide.QtGui import QApplication
-'''
-
 import pytest
 import quamash
 

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-bitcoin/python-quamash.git



More information about the Pkg-bitcoin-commits mailing list