1
0

some clean-ups now that we require python 3.10

This commit is contained in:
SomberNight
2025-01-10 13:26:39 +00:00
parent fb5a1af666
commit be2cd02e54
5 changed files with 9 additions and 45 deletions

View File

@@ -46,7 +46,7 @@ from .network import Network
from .util import (json_decode, to_bytes, to_string, profiler, standardize_path, constant_time_compare, InvalidPassword) from .util import (json_decode, to_bytes, to_string, profiler, standardize_path, constant_time_compare, InvalidPassword)
from .invoices import PR_PAID, PR_EXPIRED from .invoices import PR_PAID, PR_EXPIRED
from .util import log_exceptions, ignore_exceptions, randrange, OldTaskGroup, UserFacingException, JsonRPCError from .util import log_exceptions, ignore_exceptions, randrange, OldTaskGroup, UserFacingException, JsonRPCError
from .util import EventListener, event_listener, traceback_format_exception from .util import EventListener, event_listener
from .wallet import Wallet, Abstract_Wallet from .wallet import Wallet, Abstract_Wallet
from .storage import WalletStorage from .storage import WalletStorage
from .wallet_db import WalletDB, WalletRequiresSplit, WalletRequiresUpgrade, WalletUnfinished from .wallet_db import WalletDB, WalletRequiresSplit, WalletRequiresUpgrade, WalletUnfinished
@@ -264,7 +264,7 @@ class AuthenticatedServer(Logger):
'message': "internal error while executing RPC", 'message': "internal error while executing RPC",
'data': { 'data': {
"exception": repr(e), "exception": repr(e),
"traceback": "".join(traceback_format_exception(e)), "traceback": "".join(traceback.format_exception(e)),
}, },
} }
return web.json_response(response) return web.json_response(response)

View File

@@ -38,6 +38,7 @@ from concurrent import futures
import copy import copy
import functools import functools
from enum import IntEnum from enum import IntEnum
from contextlib import nullcontext
import aiorpcx import aiorpcx
from aiorpcx import ignore_after, NetAddress from aiorpcx import ignore_after, NetAddress
@@ -47,7 +48,7 @@ from . import util
from .util import (log_exceptions, ignore_exceptions, OldTaskGroup, from .util import (log_exceptions, ignore_exceptions, OldTaskGroup,
bfh, make_aiohttp_session, send_exception_to_crash_reporter, bfh, make_aiohttp_session, send_exception_to_crash_reporter,
is_hash256_str, is_non_negative_integer, MyEncoder, NetworkRetryManager, is_hash256_str, is_non_negative_integer, MyEncoder, NetworkRetryManager,
nullcontext, error_text_str_to_safe_str) error_text_str_to_safe_str)
from .bitcoin import COIN, DummyAddress, DummyAddressUsedInTxException from .bitcoin import COIN, DummyAddress, DummyAddressUsedInTxException
from . import constants from . import constants
from . import blockchain from . import blockchain

View File

@@ -127,10 +127,7 @@ class Plugins(DaemonThread):
# sys.modules needs to be modified for relative imports to work # sys.modules needs to be modified for relative imports to work
# see https://stackoverflow.com/a/50395128 # see https://stackoverflow.com/a/50395128
sys.modules[path] = module sys.modules[path] = module
if sys.version_info >= (3, 10): spec.loader.exec_module(module)
spec.loader.exec_module(module)
else:
module = spec.loader.load_module(path)
except Exception as e: except Exception as e:
raise Exception(f"Error pre-loading {path}: {repr(e)}") from e raise Exception(f"Error pre-loading {path}: {repr(e)}") from e
return module return module
@@ -209,12 +206,8 @@ class Plugins(DaemonThread):
if name in self.external_plugin_metadata: if name in self.external_plugin_metadata:
raise Exception(f"duplicate plugins for name={name}") raise Exception(f"duplicate plugins for name={name}")
module_path = f'electrum_external_plugins.{name}' module_path = f'electrum_external_plugins.{name}'
if sys.version_info >= (3, 10): spec = zipfile.find_spec(name)
spec = zipfile.find_spec(name) module = self.exec_module_from_spec(spec, module_path)
module = self.exec_module_from_spec(spec, module_path)
else:
module = zipfile.load_module(name)
sys.modules[module_path] = module
d = module.__dict__ d = module.__dict__
gui_good = self.gui_name in d.get('available_for', []) gui_good = self.gui_name in d.get('available_for', [])
if not gui_good: if not gui_good:

View File

@@ -52,6 +52,7 @@ from functools import partial
from abc import abstractmethod, ABC from abc import abstractmethod, ABC
import socket import socket
import enum import enum
from contextlib import nullcontext
import attr import attr
import aiohttp import aiohttp
@@ -2067,35 +2068,6 @@ def test_read_write_permissions(path) -> None:
raise IOError('echo sanity-check failed') raise IOError('echo sanity-check failed')
class nullcontext:
"""Context manager that does no additional processing.
This is a ~backport of contextlib.nullcontext from Python 3.10
"""
def __init__(self, enter_result=None):
self.enter_result = enter_result
def __enter__(self):
return self.enter_result
def __exit__(self, *excinfo):
pass
async def __aenter__(self):
return self.enter_result
async def __aexit__(self, *excinfo):
pass
def traceback_format_exception(exc: BaseException) -> Sequence[str]:
"""Compatibility wrapper for stdlib traceback.format_exception using python 3.10+ API."""
if sys.version_info[:3] >= (3, 10):
return traceback.format_exception(exc)
else:
return traceback.format_exception(type(exc), value=exc, tb=exc.__traceback__)
class classproperty(property): class classproperty(property):
"""~read-only class-level @property """~read-only class-level @property
from https://stackoverflow.com/a/13624858 by denis-ryzhkov from https://stackoverflow.com/a/13624858 by denis-ryzhkov

View File

@@ -6,8 +6,6 @@ from unittest import SkipTest
from PyQt6.QtCore import QCoreApplication, QMetaObject, Qt, pyqtSlot, QObject from PyQt6.QtCore import QCoreApplication, QMetaObject, Qt, pyqtSlot, QObject
from electrum.util import traceback_format_exception
class TestQCoreApplication(QCoreApplication): class TestQCoreApplication(QCoreApplication):
@pyqtSlot() @pyqtSlot()
@@ -78,7 +76,7 @@ def qt_test(func):
if not res: if not res:
self._e = Exception('testcase timed out') self._e = Exception('testcase timed out')
if self._e: if self._e:
print("".join(traceback_format_exception(self._e))) print("".join(traceback.format_exception(self._e)))
# deallocate stored exception from qt thread otherwise we SEGV garbage collector # deallocate stored exception from qt thread otherwise we SEGV garbage collector
# instead, re-create using the exception message, special casing AssertionError and SkipTest # instead, re-create using the exception message, special casing AssertionError and SkipTest
e = None e = None