[Pkg-bitcoin-commits] [python-quamash] 23/269: Fix context manager logic

Jonas Smedegaard dr at jones.dk
Fri Nov 24 11:26:12 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 b0fe17555eb7680481fb1dcb19b602e74b2e6312
Author: Arve Knudsen <arve.knudsen at gmail.com>
Date:   Mon Jun 30 15:12:46 2014 +0200

    Fix context manager logic
---
 quamash/__init__.py      | 11 ++++-------
 tests/test_qeventloop.py | 38 +++++++++++++++++++++++++++++++-------
 2 files changed, 35 insertions(+), 14 deletions(-)

diff --git a/quamash/__init__.py b/quamash/__init__.py
index 4c06760..ff12108 100644
--- a/quamash/__init__.py
+++ b/quamash/__init__.py
@@ -13,14 +13,11 @@ __license__ = 'BSD 2 Clause License'
 import sys
 import os
 import asyncio
-from asyncio import futures
-import socket
 import time
-from functools import partial, wraps
+from functools import wraps
 import logging
 from queue import Queue
 from concurrent.futures import Future
-import subprocess
 import threading
 
 try:
@@ -111,7 +108,7 @@ class QThreadExecutor(QtCore.QObject):
             w.stop()
 
     def __enter__(self, *args):
-        pass
+        return self
 
     def __exit__(self, *args):
         self.close()
@@ -193,7 +190,6 @@ class QEventLoop(QtCore.QObject, _baseclass):
             return rslt
         finally:
             self.__io_event_loop.call_soon_threadsafe(self.__io_event_loop.stop)
-            super(QEventLoop, self).stop()
             self.__is_running = False
 
     def run_until_complete(self, future):
@@ -289,7 +285,7 @@ class QEventLoop(QtCore.QObject, _baseclass):
             assert not args
             assert not isinstance(callback, asyncio.TimerHandle)
             if callback.cancelled:
-                f = futures.Future()
+                f = asyncio.Future()
                 f.set_result(None)
                 return f
             callback, args = callback.callback, callback.args
@@ -387,6 +383,7 @@ class QEventLoop(QtCore.QObject, _baseclass):
 
     def __enter__(self):
         asyncio.set_event_loop(self)
+        return self
 
     def __exit__(self, *args):
         try:
diff --git a/tests/test_qeventloop.py b/tests/test_qeventloop.py
index a6c75e1..d9ff424 100644
--- a/tests/test_qeventloop.py
+++ b/tests/test_qeventloop.py
@@ -2,7 +2,11 @@ import asyncio
 import os.path
 import logging
 import sys
-from PyQt5.QtWidgets import QApplication
+try:
+    from PyQt5.QtWidgets import QApplication
+except ImportError:
+    from PySide.QtGui import QApplication
+import pytest
 
 sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
 import quamash
@@ -12,8 +16,24 @@ logging.basicConfig(level=logging.DEBUG,
                     format='%(asctime)s - %(levelname)s - %(name)s - %(message)s')
 
 
+ at pytest.fixture
+def loop(request):
+    app = QApplication([])
+    lp = quamash.QEventLoop(app)
+    asyncio.set_event_loop(lp)
+
+    def fin():
+        try:
+            lp.close()
+        finally:
+            asyncio.set_event_loop(None)
+
+    request.addfinalizer(fin)
+    return lp
+
+
 class TestQEventLoop:
-    def test_can_run_tasks_in_default_executor(self):
+    def test_can_run_tasks_in_default_executor(self, loop):
         """Verify that tasks can be run in default (threaded) executor."""
         def blocking_func():
             nonlocal was_invoked
@@ -23,11 +43,15 @@ class TestQEventLoop:
         def blocking_task():
             yield from loop.run_in_executor(None, blocking_func)
 
-        app = QApplication([])
         was_invoked = False
+        loop.run_until_complete(blocking_task())
+
+        assert was_invoked
 
-        loop = quamash.QEventLoop(app)
-        with loop:
-            loop.run_until_complete(blocking_task())
+    def test_can_function_as_context_manager(self):
+        app = QApplication([])
 
-        assert was_invoked
\ No newline at end of file
+        with quamash.QEventLoop(app) as loop:
+            assert isinstance(loop, quamash.QEventLoop)
+            loop.call_soon(loop.stop)
+            loop.run_forever()

-- 
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