From: Bryan Stitt Date: Sun, 10 Nov 2013 05:23:57 +0000 (-0800) Subject: don't use bare except X-Git-Url: https://git.novaco.in/?a=commitdiff_plain;h=225d8b83a386fffc3d9ad8bb154cac2d82bcd45b;p=electrum-nvc.git don't use bare except --- diff --git a/lib/bitcoin.py b/lib/bitcoin.py index 9780a65..36758f9 100644 --- a/lib/bitcoin.py +++ b/lib/bitcoin.py @@ -115,7 +115,7 @@ def hash_160(public_key): md = hashlib.new('ripemd160') md.update(hashlib.sha256(public_key).digest()) return md.digest() - except: + except Exception: import ripemd md = ripemd.new(hashlib.sha256(public_key).digest()) return md.digest() @@ -268,7 +268,7 @@ def is_valid(addr): if not ADDRESS_RE.match(addr): return False try: addrtype, h = bc_address_to_hash_160(addr) - except: + except Exception: return False return addr == hash_160_to_bc_address(h, addrtype) @@ -277,7 +277,7 @@ def is_valid(addr): try: from ecdsa.ecdsa import curve_secp256k1, generator_secp256k1 -except: +except Exception: print "cannot import ecdsa.curve_secp256k1. You probably need to upgrade ecdsa.\nTry: sudo pip install --upgrade ecdsa" exit() from ecdsa.curves import SECP256k1 @@ -316,7 +316,7 @@ class EC_KEY(object): try: self.verify_message( address, sig, message) return sig - except: + except Exception: continue else: raise Exception("error: cannot sign message") diff --git a/lib/blockchain.py b/lib/blockchain.py index c7dab20..08461d0 100644 --- a/lib/blockchain.py +++ b/lib/blockchain.py @@ -119,7 +119,7 @@ class Blockchain(threading.Thread): assert prev_hash == header.get('prev_block_hash') assert bits == header.get('bits') assert eval('0x'+_hash) < target - except: + except Exception: return False prev_header = header @@ -176,7 +176,7 @@ class Blockchain(threading.Thread): assert prev_hash == header.get('prev_block_hash') assert bits == header.get('bits') assert eval('0x'+_hash) < target - except: + except Exception: # this can be caused by a reorg. print_error("verify header failed"+ repr(header)) verifier.undo_verifications() @@ -227,7 +227,7 @@ class Blockchain(threading.Thread): print_error("downloading ", self.headers_url ) urllib.urlretrieve(self.headers_url, filename) print_error("done.") - except: + except Exception: print_error( "download failed. creating file", filename ) open(filename,'wb+').close() @@ -411,7 +411,7 @@ class Blockchain(threading.Thread): index = params[0] try: self.verify_chunk(index, result) - except: + except Exception: print_error('Verify chunk failed!!') return False requested_chunks.remove(index) diff --git a/lib/commands.py b/lib/commands.py index aef1e9c..f34c10a 100644 --- a/lib/commands.py +++ b/lib/commands.py @@ -306,7 +306,7 @@ class Commands: tx_hash, conf, is_mine, value, fee, balance, timestamp = item try: time_str = datetime.datetime.fromtimestamp( timestamp).isoformat(' ')[:-3] - except: + except Exception: time_str = "----" label, is_default_label = self.wallet.get_label(tx_hash) diff --git a/lib/interface.py b/lib/interface.py index 360b82c..44de285 100644 --- a/lib/interface.py +++ b/lib/interface.py @@ -49,7 +49,7 @@ def check_cert(host, cert): def cert_has_expired(cert_path): try: import OpenSSL - except: + except Exception: print_error("Warning: cannot import OpenSSL") return False from OpenSSL import crypto as c @@ -112,7 +112,7 @@ class Interface(threading.Thread): try: host, port, protocol = self.server.split(':') port = int(port) - except: + except Exception: self.server = None return @@ -196,7 +196,7 @@ class Interface(threading.Thread): self.connection_msg = ('https' if self.use_ssl else 'http') + '://%s:%d'%( self.host, self.port ) try: self.poll() - except: + except Exception: print_error("http init session failed") self.is_connected = False return @@ -218,7 +218,7 @@ class Interface(threading.Thread): break except socket.error: break - except: + except Exception: traceback.print_exc(file=sys.stdout) break @@ -265,7 +265,7 @@ class Interface(threading.Thread): try: req = urllib2.Request(self.connection_msg, data_json, headers) response_stream = urllib2.urlopen(req, timeout=DEFAULT_TIMEOUT) - except: + except Exception: return for index, cookie in enumerate(cj): @@ -318,7 +318,7 @@ class Interface(threading.Thread): s = socket.socket( socket.AF_INET, socket.SOCK_STREAM ) try: s.connect((self.host, self.port)) - except: + except Exception: # print_error("failed to connect", self.host, self.port) return @@ -346,7 +346,7 @@ class Interface(threading.Thread): try: s.connect(( self.host.encode('ascii'), int(self.port))) - except: + except Exception: print_error("failed to connect", self.host, self.port) return @@ -370,7 +370,7 @@ class Interface(threading.Thread): else: print_msg("wrong certificate", self.host) return - except: + except Exception: print_error("wrap_socket failed", self.host) traceback.print_exc(file=sys.stdout) return @@ -424,7 +424,7 @@ class Interface(threading.Thread): c = json.loads(c) self.queue_json_response(c) - except: + except Exception: traceback.print_exc(file=sys.stdout) self.is_connected = False diff --git a/lib/network.py b/lib/network.py index 82bdcb4..c900cad 100644 --- a/lib/network.py +++ b/lib/network.py @@ -385,7 +385,7 @@ class Network(threading.Thread): if pruning_level == '': pruning_level = '0' try: is_recent = float(version)>=float(PROTOCOL_VERSION) - except: + except Exception: is_recent = False if out and is_recent: diff --git a/lib/plugins.py b/lib/plugins.py index 0e7e520..a3ba5bc 100644 --- a/lib/plugins.py +++ b/lib/plugins.py @@ -24,7 +24,7 @@ def init_plugins(self): for name, p in zip(plugin_names, plugin_modules): try: plugins.append( p.Plugin(self, name) ) - except: + except Exception: print_msg(_("Error: cannot initialize plugin"),p) traceback.print_exc(file=sys.stdout) @@ -45,7 +45,7 @@ def run_hook(name, *args): try: f(*args) - except: + except Exception: print_error("Plugin error") traceback.print_exc(file=sys.stdout) diff --git a/lib/simple_config.py b/lib/simple_config.py index e3614f5..1ae6579 100644 --- a/lib/simple_config.py +++ b/lib/simple_config.py @@ -103,7 +103,7 @@ a SimpleConfig instance then reads the wallet file. import ast try: out = ast.literal_eval(out) - except: + except Exception: print "type error for '%s': using default value"%key out = default @@ -154,7 +154,7 @@ a SimpleConfig instance then reads the wallet file. return try: d = ast.literal_eval( data ) #parse raw data from reading wallet file - except: + except Exception: raise IOError("Cannot read config file.") self.user_config = d diff --git a/lib/transaction.py b/lib/transaction.py index 7e36369..b1c4bf5 100644 --- a/lib/transaction.py +++ b/lib/transaction.py @@ -298,7 +298,7 @@ def match_decoded(decoded, to_match): def get_address_from_input_script(bytes): try: decoded = [ x for x in script_GetOp(bytes) ] - except: + except Exception: # coinbase transactions raise an exception print_error("cannot find address in input script", bytes.encode('hex')) return [], [], "(None)" diff --git a/lib/wallet.py b/lib/wallet.py index 4297f2c..4cb7bd4 100644 --- a/lib/wallet.py +++ b/lib/wallet.py @@ -55,7 +55,7 @@ def pw_decode(s, password): secret = Hash(password) try: d = DecodeAES(secret, s) - except: + except Exception: raise Exception('Invalid password') return d else: @@ -117,7 +117,7 @@ class WalletStorage: return try: d = ast.literal_eval( data ) #parse raw data from reading wallet file - except: + except Exception: raise IOError("Cannot read wallet file.") self.data = d @@ -192,7 +192,7 @@ class Wallet: for k,v in tx_list.items(): try: tx = Transaction(v) - except: + except Exception: print_msg("Warning: Cannot deserialize transactions. skipping") continue @@ -256,7 +256,7 @@ class Wallet: seed = self.get_seed(password) try: address = address_from_private_key(sec) - except: + except Exception: raise Exception('Invalid private key') if self.is_mine(address): @@ -314,7 +314,7 @@ class Wallet: self.seed_version = 4 self.seed = str(seed) return - except: + except Exception: pass words = seed.split() @@ -324,7 +324,7 @@ class Wallet: #try: # mnemonic.mn_decode(words) # uses_electrum_words = True - #except: + #except Exception: # uses_electrum_words = False # #if uses_electrum_words and len(words) != 13: @@ -608,7 +608,7 @@ class Wallet: try: K, Kc = get_pubkeys_from_secret(master_k.decode('hex')) assert K.encode('hex') == master_K - except: + except Exception: raise Exception("Invalid password") return master_k