From a7d3175799018f9a378e8b2bcb5508dcf30c5be4 Mon Sep 17 00:00:00 2001 From: ThomasV Date: Mon, 15 Feb 2016 09:33:38 +0100 Subject: [PATCH] replace semantically relevant instances of 'assert' with exceptions --- lib/util.py | 3 ++- lib/wallet.py | 26 +++++++++++++------------- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/lib/util.py b/lib/util.py index 1520bd820..1eae1d2d1 100644 --- a/lib/util.py +++ b/lib/util.py @@ -352,7 +352,8 @@ def parse_URI(uri, on_pr=None): out = {k: v[0] for k, v in pq.items()} if address: - assert bitcoin.is_address(address) + if not bitcoin.is_address(address): + raise BaseException("Invalid bitcoin address:" + address) out['address'] = address if 'amount' in out: am = out['amount'] diff --git a/lib/wallet.py b/lib/wallet.py index ae878a2b8..1bf0cafcb 100644 --- a/lib/wallet.py +++ b/lib/wallet.py @@ -354,7 +354,8 @@ class Abstract_Wallet(PrintError): return account is not None def import_key(self, sec, password): - assert self.can_import(), 'This wallet cannot import private keys' + if not self.can_import(): + raise BaseException('This wallet cannot import private keys') try: pubkey = public_key_from_private_key(sec) address = public_key_to_bc_address(pubkey.decode('hex')) @@ -929,7 +930,8 @@ class Abstract_Wallet(PrintError): # check outputs for type, data, value in outputs: if type == TYPE_ADDRESS: - assert is_address(data), "Address " + data + " is invalid!" + if not is_address(data): + raise BaseException("Invalid bitcoin address:" + data) # Avoid index-out-of-range with coins[0] below if not coins: @@ -1040,7 +1042,8 @@ class Abstract_Wallet(PrintError): # asynchronous self.tx_event.clear() # fixme: this does not handle the case where server does not answer - assert self.network.interface, "Not connected." + if not self.network.interface: + raise BaseException("Not connected") self.network.send([('blockchain.transaction.broadcast', [str(tx)])], self.on_broadcast) return tx.hash() @@ -1864,18 +1867,16 @@ class OldWallet(Deterministic_Wallet): import old_mnemonic # see if seed was entered as hex seed = seed.strip() - try: - assert seed - seed.decode('hex') - return OLD_SEED_VERSION, str(seed) - except Exception: - pass - + if seed: + try: + seed.decode('hex') + return OLD_SEED_VERSION, str(seed) + except Exception: + pass words = seed.split() seed = old_mnemonic.mn_decode(words) if not seed: raise Exception("Invalid seed") - return OLD_SEED_VERSION, seed def create_master_keys(self, password): @@ -2001,10 +2002,9 @@ class Wallet(object): def is_old_mpk(mpk): try: int(mpk, 16) - assert len(mpk) == 128 - return True except: return False + return len(mpk) == 128 @staticmethod def is_xpub(text):