commit 308517d473d18fdc5a377e7296e0e6ab3f6c92b8
parent fa9b997c70586b2ddcd44b996ec04e35820f5653
Author: SomberNight <somber.night@protonmail.com>
Date: Wed, 11 Dec 2019 23:07:47 +0100
python 3.8: adapt to breaking changes re asyncio.CancelledError
(and TimeoutError)
closes #5798
Diffstat:
5 files changed, 13 insertions(+), 7 deletions(-)
diff --git a/electrum/lnworker.py b/electrum/lnworker.py
@@ -15,6 +15,7 @@ from datetime import datetime, timezone
from functools import partial
from collections import defaultdict
import concurrent
+from concurrent import futures
import dns.resolver
import dns.exception
diff --git a/electrum/network.py b/electrum/network.py
@@ -35,6 +35,8 @@ import ipaddress
import asyncio
from typing import NamedTuple, Optional, Sequence, List, Dict, Tuple, TYPE_CHECKING
import traceback
+import concurrent
+from concurrent import futures
import dns
import dns.resolver
@@ -1204,7 +1206,7 @@ class Network(Logger):
fut = asyncio.run_coroutine_threadsafe(self._stop(full_shutdown=True), self.asyncio_loop)
try:
fut.result(timeout=2)
- except (asyncio.TimeoutError, asyncio.CancelledError): pass
+ except (concurrent.futures.TimeoutError, concurrent.futures.CancelledError): pass
async def _ensure_there_is_a_main_interface(self):
if self.is_connected():
diff --git a/electrum/tests/test_lnpeer.py b/electrum/tests/test_lnpeer.py
@@ -5,6 +5,8 @@ import os
from contextlib import contextmanager
from collections import defaultdict
import logging
+import concurrent
+from concurrent import futures
from electrum.network import Network
from electrum.ecc import ECPrivkey
@@ -236,7 +238,7 @@ class TestPeer(ElectrumTestCase):
gath = asyncio.gather(pay(), p1._message_loop(), p2._message_loop())
async def f():
await gath
- with self.assertRaises(asyncio.CancelledError):
+ with self.assertRaises(concurrent.futures.CancelledError):
run(f())
def test_channel_usage_after_closing(self):
diff --git a/electrum/tests/test_lntransport.py b/electrum/tests/test_lntransport.py
@@ -38,7 +38,7 @@ class TestLNTransport(ElectrumTestCase):
asyncio.get_event_loop().run_until_complete(transport.handshake(epriv=e_priv))
def test_loop(self):
- l = asyncio.get_event_loop()
+ loop = asyncio.get_event_loop()
responder_shaked = asyncio.Event()
server_shaked = asyncio.Event()
responder_key = ECPrivkey.generate_random_key()
@@ -50,7 +50,7 @@ class TestLNTransport(ElectrumTestCase):
self.assertEqual(await t.read_messages().__anext__(), b'hello from client')
responder_shaked.set()
server_future = asyncio.ensure_future(asyncio.start_server(cb, '127.0.0.1', 42898))
- l.run_until_complete(server_future)
+ loop.run_until_complete(server_future)
async def connect():
peer_addr = LNPeerAddr('127.0.0.1', 42898, responder_key.get_public_key_bytes())
t = LNTransport(initiator_key.get_secret_bytes(), peer_addr)
@@ -59,6 +59,6 @@ class TestLNTransport(ElectrumTestCase):
self.assertEqual(await t.read_messages().__anext__(), b'hello from server')
server_shaked.set()
- asyncio.ensure_future(connect())
- l.run_until_complete(responder_shaked.wait())
- l.run_until_complete(server_shaked.wait())
+ connect_future = asyncio.ensure_future(connect())
+ loop.run_until_complete(responder_shaked.wait())
+ loop.run_until_complete(server_shaked.wait())
diff --git a/electrum/util.py b/electrum/util.py
@@ -1020,6 +1020,7 @@ def ignore_exceptions(func):
try:
return await func(*args, **kwargs)
except asyncio.CancelledError:
+ # note: with python 3.8, CancelledError no longer inherits Exception, so this catch is redundant
raise
except Exception as e:
pass