various Python 3.9 changes

This commit is contained in:
The MMGen Project 2024-02-10 15:10:43 +00:00
commit ab71e8581d
Signed by: mmgen
GPG key ID: 3F8B1861E32B7DA2
9 changed files with 10 additions and 28 deletions

View file

@ -75,10 +75,7 @@ class GlobalConstants(Lockable):
# Resource Access without the performance overhead of that package.
# https://importlib-resources.readthedocs.io/en/latest/migration.html
# https://setuptools.readthedocs.io/en/latest/pkg_resources.html
try:
from importlib.resources import files # Python 3.9
except ImportError:
from importlib_resources import files
from importlib.resources import files
return files(package).joinpath('data',filename).read_text()
@property

View file

@ -50,7 +50,7 @@ Actions: [q]uit menu, r[e]draw, add [l]abel:
msg('done')
amt0 = self.proto.coin_amt('0')
self.total = sum((v['amt'] for v in addrs.values())) or amt0 # Python 3.8: start=amt0
self.total = sum((v['amt'] for v in addrs.values()), start=amt0)
msg_r('Getting labels and associated addresses...')
pairs = await self.get_addr_label_pairs()

View file

@ -92,7 +92,7 @@ class EthereumTwJSON(TwJSON):
def gen_data(data):
for d in data:
if hasattr(d,'address'):
if d.amount is None: # Python 3.9: {} | {}
if d.amount is None:
yield (d.address, {'mmid':d.mmgen_id,'comment':TwComment(d.comment)})
else:
yield (d.address, {'mmid':d.mmgen_id,'comment':TwComment(d.comment),'balance':d.amount})

View file

@ -247,9 +247,8 @@ class RPCBackends:
from subprocess import run,PIPE
from .color import set_vt100
res = run(exec_cmd,stdout=PIPE,check=True).stdout.decode()
res = run(exec_cmd,stdout=PIPE,check=True,text=True).stdout
set_vt100()
# res = run(exec_cmd,stdout=PIPE,check=True,text='UTF-8').stdout # Python 3.7+
return (res[:-3],int(res[-3:]))
class RPCClient(MMGenObject):

View file

@ -30,12 +30,6 @@ def die_pause(ev=0,s=''):
input('Press ENTER to exit')
sys.exit(ev)
def removeprefix(s,pfx): # workaround for pre-Python 3.9
return s[len(pfx):] if s.startswith(pfx) else s
def removesuffix(s,sfx): # workaround for pre-Python 3.9
return s[:-len(sfx)] if s.endswith(sfx) else s
# monkey-patch function for monero-python: permits its use with pycryptodome (e.g. MSYS2)
# instead of the expected pycryptodomex
def load_cryptodomex():

View file

@ -51,12 +51,8 @@ def exec_wrapper_write_traceback(e,exit_val):
overlay_path_pfx = os.path.relpath(get_overlay_tree_dir(cwd)) + '/'
def fixup_fn(fn_in):
from mmgen.util2 import removeprefix,removesuffix
fn = removeprefix(removeprefix(fn_in,cwd+'/'),overlay_path_pfx)
return removesuffix(fn,'_orig.py') + '.py' if fn.endswith('_orig.py') else fn
# Python 3.9:
# fn = fn_in.removeprefix(cwd+'/').removeprefix('test/overlay/tree/')
# return fn.removesuffix('_orig.py') + '.py' if fn.endswith('_orig.py') else fn
fn = fn_in.removeprefix(cwd+'/').removeprefix(overlay_path_pfx)
return fn.removesuffix('_orig.py') + '.py' if fn.endswith('_orig.py') else fn
def gen_output():
yield 'Traceback (most recent call last):'

View file

@ -39,7 +39,6 @@ python_requires = >=3.9
include_package_data = True
install_requires =
importlib-resources; python_version < "3.9"
gmpy2
cryptography
pynacl

View file

@ -406,9 +406,7 @@ class CmdGroupMgr:
def gen():
for name,data in cls.cmd_group_in:
if name.startswith('subgroup.'):
from mmgen.util2 import removeprefix
sg_key = removeprefix(name,'subgroup.')
# sg_key = name.removeprefix('subgroup.') # Python 3.9
sg_key = name.removeprefix('subgroup.')
if sg_name in (None,sg_key):
for e in add_entries(
sg_key,

View file

@ -31,12 +31,11 @@ def overlay_setup(repo_root):
d == f'{pkgname}.data' or
(d == 'mmgen.proto.secp256k1' and fn.startswith('secp256k1'))
):
if os.path.exists(os.path.join(fakemod_dir,fn)):
if fn.endswith('.py') and os.path.exists(os.path.join(fakemod_dir,fn)):
make_link(
os.path.join(fakemod_dir,fn),
os.path.join(destdir,fn) )
# link_fn = fn.removesuffix('.py') + '_orig.py' # Python 3.9
link_fn = fn[:-3] + '_orig.py'
link_fn = fn.removesuffix('.py') + '_orig.py'
else:
link_fn = fn
make_link(
@ -47,7 +46,7 @@ def overlay_setup(repo_root):
fakemod_root = os.path.join(repo_root,'test','overlay','fakemods')
common_path = os.path.join(os.path.sep,'test','overlay','fakemods')
pkgdata = ((
os.path.realpath(e.path)[:-len(os.path.join(common_path,e.name))], # Python 3.9: removesuffix()
os.path.realpath(e.path).removesuffix(os.path.join(common_path,e.name)),
e.name
) for e in os.scandir(fakemod_root) if e.is_dir() )