Skip to content

Commit

Permalink
Merge pull request #94 from henry232323/master
Browse files Browse the repository at this point in the history
Minor Changes for 3.7 Compatability
  • Loading branch information
harvimt authored Jul 23, 2018
2 parents 2191e4c + a5354e3 commit 39f2807
Show file tree
Hide file tree
Showing 5 changed files with 239 additions and 235 deletions.
26 changes: 15 additions & 11 deletions quamash/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from queue import Queue
from concurrent.futures import Future
import logging
import importlib
logger = logging.getLogger('quamash')

try:
Expand All @@ -25,12 +26,12 @@
QtModule = None
else:
logger.info('Forcing use of {} as Qt Implementation'.format(QtModuleName))
QtModule = __import__(QtModuleName)
QtModule = importlib.import_module(QtModuleName)

if not QtModule:
for QtModuleName in ('PyQt5', 'PyQt4', 'PySide'):
try:
QtModule = __import__(QtModuleName)
QtModule = importlib.import_module(QtModuleName)
except ImportError:
continue
else:
Expand All @@ -40,8 +41,8 @@

logger.info('Using Qt Implementation: {}'.format(QtModuleName))

QtCore = __import__(QtModuleName + '.QtCore', fromlist=(QtModuleName,))
QtGui = __import__(QtModuleName + '.QtGui', fromlist=(QtModuleName,))
QtCore = importlib.import_module(QtModuleName + '.QtCore', package=QtModuleName)
QtGui = importlib.import_module(QtModuleName + '.QtGui', package=QtModuleName)
if QtModuleName == 'PyQt5':
from PyQt5 import QtWidgets
QApplication = QtWidgets.QApplication
Expand Down Expand Up @@ -272,7 +273,7 @@ def run_forever(self):
def run_until_complete(self, future):
"""Run until Future is complete."""
self._logger.debug('Running {} until complete'.format(future))
future = asyncio.async(future, loop=self)
future = asyncio.ensure_future(future, loop=self)

def stop(*args): self.stop() # noqa
future.add_done_callback(stop)
Expand Down Expand Up @@ -328,7 +329,7 @@ def close(self):
self._read_notifiers = None
self._write_notifiers = None

def call_later(self, delay, callback, *args):
def call_later(self, delay, callback, *args, context=None):
"""Register callback to be invoked after a certain delay."""
if asyncio.iscoroutinefunction(callback):
raise TypeError("coroutines cannot be used with call_later")
Expand All @@ -338,18 +339,21 @@ def call_later(self, delay, callback, *args):
self._logger.debug(
'Registering callback {} to be invoked with arguments {} after {} second(s)'
.format(callback, args, delay))

if sys.version_info >= (3, 7):
return self._add_callback(asyncio.Handle(callback, args, self, context=context), delay)
return self._add_callback(asyncio.Handle(callback, args, self), delay)

def _add_callback(self, handle, delay=0):
return self._timer.add_callback(handle, delay)

def call_soon(self, callback, *args):
def call_soon(self, callback, *args, context=None):
"""Register a callback to be run on the next iteration of the event loop."""
return self.call_later(0, callback, *args)
return self.call_later(0, callback, *args, context=context)

def call_at(self, when, callback, *args):
def call_at(self, when, callback, *args, context=None):
"""Register callback to be invoked at a certain time."""
return self.call_later(when - self.time(), callback, *args)
return self.call_later(when - self.time(), callback, *args, context=context)

def time(self):
"""Get time according to event loop's clock."""
Expand Down Expand Up @@ -464,7 +468,7 @@ def __on_notifier_ready(self, notifiers, notifier, fd, callback, args):

# Methods for interacting with threads.

def call_soon_threadsafe(self, callback, *args):
def call_soon_threadsafe(self, callback, *args, context=None):
"""Thread-safe version of call_soon."""
self.__call_soon_signal.emit(callback, args)

Expand Down
Loading

0 comments on commit 39f2807

Please sign in to comment.