From b41d6ae3ebd0f112e6409b95546c11639f8d917e Mon Sep 17 00:00:00 2001 From: MMGen Date: Wed, 31 Oct 2018 15:52:05 +0000 Subject: [PATCH] py3port: fix altcoin_subclass module loading magic --- mmgen/addr.py | 2 +- mmgen/protocol.py | 14 ++++++++------ mmgen/tw.py | 6 +++--- mmgen/tx.py | 2 +- mmgen/util.py | 11 ++++++++--- 5 files changed, 21 insertions(+), 14 deletions(-) diff --git a/mmgen/addr.py b/mmgen/addr.py index a9f2d58a..d0a0b4ca 100755 --- a/mmgen/addr.py +++ b/mmgen/addr.py @@ -877,7 +877,7 @@ re-import your addresses. } def __new__(cls,*args,**kwargs): - return MMGenObject.__new__(altcoin_subclass(cls,'tw','AddrData'),*args,**kwargs) + return MMGenObject.__new__(altcoin_subclass(cls,'tw','AddrData')) def __init__(self,source=None): self.al_ids = {} diff --git a/mmgen/protocol.py b/mmgen/protocol.py index 66d32e67..4c13b4bd 100755 --- a/mmgen/protocol.py +++ b/mmgen/protocol.py @@ -479,7 +479,7 @@ def init_genonly_altcoins(usr_coin,trust_level=None): data = {} for k in ('mainnet','testnet'): data[k] = [e for e in ci.coin_constants[k] if e[6] >= trust_level] - exec(make_init_genonly_altcoins_str(data)) + exec(make_init_genonly_altcoins_str(data),globals(),globals()) return trust_level def make_init_genonly_altcoins_str(data): @@ -490,16 +490,18 @@ def make_init_genonly_altcoins_str(data): if proto[0] in '0123456789': proto = 'X_'+proto if proto in globals(): return '' if coin.lower() in CoinProtocol.coins: return '' - def num2hexstr(n): - return '{:0{}x}'.format(n,2 if n < 256 else 4) + + def num2hexbytes(n): + return "b'{:0{}x}'".format(n,(4,2)[n < 256]) + o = ['class {}(Bitcoin{}ProtocolAddrgen):'.format(proto,tn_str)] o += ["base_coin = '{}'".format(coin)] o += ["name = '{}'".format(e[0].lower())] o += ["nameCaps = '{}'".format(e[0])] - a = "addr_ver_num = {{ 'p2pkh': ({!r},{!r})".format(num2hexstr(e[3][0]),e[3][1]) - b = ", 'p2sh': ({!r},{!r})".format(num2hexstr(e[4][0]),e[4][1]) if e[4] else '' + a = "addr_ver_num = {{ 'p2pkh': ({},{!r})".format(num2hexbytes(e[3][0]),e[3][1]) + b = ", 'p2sh': ({},{!r})".format(num2hexbytes(e[4][0]),e[4][1]) if e[4] else '' o += [a+b+' }'] - o += ["wif_ver_num = {{ 'std': {!r} }}".format(num2hexstr(e[2]))] + o += ["wif_ver_num = {{ 'std': {} }}".format(num2hexbytes(e[2]))] o += ["mmtypes = ('L','C'{})".format(",'S'" if e[5] else '')] o += ["dfl_mmtype = '{}'".format('L')] return '\n\t'.join(o) + '\n' diff --git a/mmgen/tw.py b/mmgen/tw.py index d051ad05..c8bb544e 100755 --- a/mmgen/tw.py +++ b/mmgen/tw.py @@ -30,7 +30,7 @@ def CUR_RIGHT(n): return '\033[{}C'.format(n) class TwUnspentOutputs(MMGenObject): def __new__(cls,*args,**kwargs): - return MMGenObject.__new__(altcoin_subclass(cls,'tw','TwUnspentOutputs'),*args,**kwargs) + return MMGenObject.__new__(altcoin_subclass(cls,'tw','TwUnspentOutputs')) txid_w = 64 disp_type = 'btc' @@ -508,7 +508,7 @@ class TwAddrList(MMGenDict): class TrackingWallet(MMGenObject): def __new__(cls,*args,**kwargs): - return MMGenObject.__new__(altcoin_subclass(cls,'tw','TrackingWallet'),*args,**kwargs) + return MMGenObject.__new__(altcoin_subclass(cls,'tw','TrackingWallet')) mode = 'r' caps = ('rescan','batch') @@ -609,7 +609,7 @@ class TwGetBalance(MMGenObject): fs = '{w:13} {u:<16} {p:<16} {c}\n' def __new__(cls,*args,**kwargs): - return MMGenObject.__new__(altcoin_subclass(cls,'tw','TwGetBalance'),*args,**kwargs) + return MMGenObject.__new__(altcoin_subclass(cls,'tw','TwGetBalance')) def __init__(self,minconf,quiet): diff --git a/mmgen/tx.py b/mmgen/tx.py index 21f7b7cb..db423471 100755 --- a/mmgen/tx.py +++ b/mmgen/tx.py @@ -205,7 +205,7 @@ txio_attrs = { class MMGenTX(MMGenObject): def __new__(cls,*args,**kwargs): - return MMGenObject.__new__(altcoin_subclass(cls,'tx','MMGenTX'),*args,**kwargs) + return MMGenObject.__new__(altcoin_subclass(cls,'tx','MMGenTX')) ext = 'rawtx' raw_ext = 'rawtx' diff --git a/mmgen/util.py b/mmgen/util.py index b27bb46c..5ead48b4 100755 --- a/mmgen/util.py +++ b/mmgen/util.py @@ -930,9 +930,14 @@ def altcoin_subclass(cls,mod_id,cls_name): pname = g.proto.class_pfx if hasattr(g.proto,'class_pfx') else capfirst(g.proto.name) tname = 'Token' if g.token else '' e1 = 'from mmgen.altcoins.{}.{} import {}{}{}'.format(mod_dir,mod_id,pname,tname,cls_name) - e2 = 'cls = {}{}{}'.format(pname,tname,cls_name) - try: exec(e1); exec(e2); return cls - except ImportError: return cls + e2 = 'alt_cls = {}{}{}'.format(pname,tname,cls_name) + gl = globals() + try: + exec(e1,gl,gl) + exec(e2,gl,gl) + return alt_cls + except ImportError: + return cls # decorator for TrackingWallet def write_mode(orig_func):