From 55d4519fe02d92fd3878554a21c05693de38fe21 Mon Sep 17 00:00:00 2001 From: Bryan Stitt Date: Sun, 17 Nov 2013 13:40:24 -0800 Subject: [PATCH] use Exception instead of BaseException or nothing --- backends/bitcoind/blockchain_processor.py | 48 +++++++++++------------ backends/bitcoind/deserialize.py | 4 +- backends/irc/__init__.py | 10 ++--- processor.py | 8 ++-- server.py | 4 +- transports/stratum_http.py | 6 +-- transports/stratum_tcp.py | 14 +++---- utils/__init__.py | 2 +- 8 files changed, 48 insertions(+), 48 deletions(-) diff --git a/backends/bitcoind/blockchain_processor.py b/backends/bitcoind/blockchain_processor.py index 03de6ee1..e30a6558 100644 --- a/backends/bitcoind/blockchain_processor.py +++ b/backends/bitcoind/blockchain_processor.py @@ -43,7 +43,7 @@ def __init__(self, config, shared): self.dblock = threading.Lock() try: self.db = leveldb.LevelDB(self.dbpath, paranoid_checks=True) - except: + except Exception: traceback.print_exc(file=sys.stdout) self.shared.stop() @@ -57,7 +57,7 @@ def __init__(self, config, shared): try: self.bitcoind('getinfo') break - except: + except Exception: print_log('cannot contact bitcoind...') time.sleep(5) continue @@ -72,7 +72,7 @@ def __init__(self, config, shared): self.last_hash, self.height, db_version = hist[0] print_log("Database version", self.db_version) print_log("Blockchain height", self.height) - except: + except Exception: traceback.print_exc(file=sys.stdout) print_log('initializing database') self.height = 0 @@ -92,7 +92,7 @@ def __init__(self, config, shared): while not shared.stopped() and not self.up_to_date: try: time.sleep(1) - except: + except Exception: print "keyboard interrupt: stopping threads" shared.stop() sys.exit(0) @@ -107,13 +107,13 @@ def bitcoind(self, method, params=[]): postdata = dumps({"method": method, 'params': params, 'id': 'jsonrpc'}) try: respdata = urllib.urlopen(self.bitcoind_url, postdata).read() - except: + except Exception: traceback.print_exc(file=sys.stdout) self.shared.stop() r = loads(respdata) if r['error'] is not None: - raise BaseException(r['error']) + raise Exception(r['error']) return r.get('result') def serialize(self, h): @@ -251,14 +251,14 @@ def get_chunk(self, i): def get_mempool_transaction(self, txid): try: raw_tx = self.bitcoind('getrawtransaction', [txid, 0]) - except: + except Exception: return None vds = deserialize.BCDataStream() vds.write(raw_tx.decode('hex')) try: return deserialize.parse_Transaction(vds, is_coinbase=False) - except: + except Exception: print_log("ERROR: cannot parse", txid) return None @@ -274,7 +274,7 @@ def get_history(self, addr, cache_only=False): try: hist = self.deserialize(self.db.Get(addr)) is_known = True - except: + except Exception: hist = [] is_known = False @@ -443,7 +443,7 @@ def set_spent_bit(self, addr, txi, is_spent, txid=None, index=None, height=None) else: self.shared.stop() hist = self.deserialize(serialized_hist) - raise BaseException("prevout not found", addr, hist, txi.encode('hex')) + raise Exception("prevout not found", addr, hist, txi.encode('hex')) self.batch_list[addr] = serialized_hist @@ -464,7 +464,7 @@ def deserialize_block(self, block): vds.write(raw_tx.decode('hex')) try: tx = deserialize.parse_Transaction(vds, is_coinbase) - except: + except Exception: print_log("ERROR: cannot parse", tx_hash) continue tx_hashes.append(tx_hash) @@ -516,7 +516,7 @@ def import_block(self, block, block_hash, block_height, sync, revert=False): except KeyError: # the input could come from the same block continue - except: + except Exception: traceback.print_exc(file=sys.stdout) self.shared.stop() raise @@ -551,7 +551,7 @@ def import_block(self, block, block_hash, block_height, sync, revert=False): self.batch_list[addr] = self.db.Get(addr) except KeyError: self.batch_list[addr] = '' - except: + except Exception: traceback.print_exc(file=sys.stdout) self.shared.stop() raise @@ -689,7 +689,7 @@ def process(self, request, cache_only=False): address = params[0] result = self.get_status(address, cache_only) self.watch_address(address) - except BaseException, e: + except Exception as e: error = str(e) + ': ' + address print_log("error:", error) @@ -704,7 +704,7 @@ def process(self, request, cache_only=False): else: print_log('incorrect password') result = "authentication error" - except BaseException, e: + except Exception as e: error = str(e) + ': ' + address print_log("error:", error) @@ -712,7 +712,7 @@ def process(self, request, cache_only=False): try: address = params[0] result = self.get_history(address, cache_only) - except BaseException, e: + except Exception as e: error = str(e) + ': ' + address print_log("error:", error) @@ -723,7 +723,7 @@ def process(self, request, cache_only=False): try: height = params[0] result = self.get_header(height) - except BaseException, e: + except Exception as e: error = str(e) + ': %d' % height print_log("error:", error) @@ -734,7 +734,7 @@ def process(self, request, cache_only=False): try: index = params[0] result = self.get_chunk(index) - except BaseException, e: + except Exception as e: error = str(e) + ': %d' % index print_log("error:", error) @@ -743,7 +743,7 @@ def process(self, request, cache_only=False): txo = self.bitcoind('sendrawtransaction', params) print_log("sent tx:", txo) result = txo - except BaseException, e: + except Exception as e: result = str(e) # do not send an error print_log("error:", result, params) @@ -755,7 +755,7 @@ def process(self, request, cache_only=False): tx_hash = params[0] tx_height = params[1] result = self.get_merkle(tx_hash, tx_height) - except BaseException, e: + except Exception as e: error = str(e) + ': ' + repr(params) print_log("get_merkle error:", error) @@ -763,7 +763,7 @@ def process(self, request, cache_only=False): try: tx_hash = params[0] result = self.bitcoind('getrawtransaction', [tx_hash, 0]) - except BaseException, e: + except Exception as e: error = str(e) + ': ' + repr(params) print_log("tx get error:", error) @@ -798,7 +798,7 @@ def getfullblock(self, block_hash): postdata = dumps(rawtxreq) try: respdata = urllib.urlopen(self.bitcoind_url, postdata).read() - except: + except Exception: traceback.print_exc(file=sys.stdout) self.shared.stop() @@ -806,7 +806,7 @@ def getfullblock(self, block_hash): rawtxdata = [] for ir in r: if ir['error'] is not None: - raise BaseException(ir['error']) + raise Exception(ir['error']) rawtxdata.append(ir['result']) block['tx'] = rawtxdata return block @@ -957,7 +957,7 @@ def main_iteration(self): while True: try: addr = self.address_queue.get(False) - except: + except Exception: break if addr in self.watched_addresses: status = self.get_status(addr) diff --git a/backends/bitcoind/deserialize.py b/backends/bitcoind/deserialize.py index 7e010d60..df43deec 100644 --- a/backends/bitcoind/deserialize.py +++ b/backends/bitcoind/deserialize.py @@ -348,7 +348,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 return [], [], None @@ -390,7 +390,7 @@ def get_address_from_input_script(bytes): def get_address_from_output_script(bytes): try: decoded = [ x for x in script_GetOp(bytes) ] - except: + except Exception: return "None" # The Genesis Block, self-payments, and pay-by-IP-address payments look like: diff --git a/backends/irc/__init__.py b/backends/irc/__init__.py index f0056d09..da328cd4 100644 --- a/backends/irc/__init__.py +++ b/backends/irc/__init__.py @@ -78,7 +78,7 @@ def run(self): s = socket.socket() s.connect(('irc.freenode.net', 6667)) s.settimeout(300) - except: + except Exception: s.close() time.sleep(10) continue @@ -92,7 +92,7 @@ def run(self): while not self.processor.shared.stopped(): try: data = s.recv(2048) - except: + except Exception: print_log( "irc: socket error" ) time.sleep(1) break @@ -119,7 +119,7 @@ def run(self): k = line.index('352') try: ip = socket.gethostbyname(line[k+4]) - except: + except Exception: print_log("gethostbyname error", line[k+4]) continue name = line[k+6] @@ -132,7 +132,7 @@ def run(self): s.send('NAMES #electrum\n') t = time.time() self.peers = {} - except: + except Exception: traceback.print_exc(file=sys.stdout) finally: s.close() @@ -172,7 +172,7 @@ def process(self, request): if method in ['server.stop', 'server.info']: try: password = request['params'][0] - except: + except Exception: password = None if password != self.password: diff --git a/processor.py b/processor.py index ec62d482..bc3d3cf7 100644 --- a/processor.py +++ b/processor.py @@ -49,7 +49,7 @@ def run(self): request = self.queue.get(10000000000) try: self.process(request) - except: + except Exception: traceback.print_exc(file=sys.stdout) print_log("processor terminating") @@ -122,7 +122,7 @@ def run(self): session, request = self.pop_request() try: self.do_dispatch(session, request) - except: + except Exception: traceback.print_exc(file=sys.stdout) self.stop() @@ -147,7 +147,7 @@ def do_dispatch(self, session, request): prefix = request['method'].split('.')[0] try: p = self.processors[prefix] - except: + except Exception: print_log("error: no processor for", prefix) return @@ -157,7 +157,7 @@ def do_dispatch(self, session, request): session.version = params[0] try: session.protocol_version = float(params[1]) - except: + except Exception: pass diff --git a/server.py b/server.py index 0af6cc61..529bc328 100755 --- a/server.py +++ b/server.py @@ -85,7 +85,7 @@ def run_rpc_command(command, stratum_tcp_port): try: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((host, int(stratum_tcp_port))) - except: + except Exception: print "cannot connect to server." return @@ -189,7 +189,7 @@ def run_rpc_command(command, stratum_tcp_port): while not shared.stopped(): try: time.sleep(1) - except: + except Exception: shared.stop() print_log("Electrum Server stopped") diff --git a/transports/stratum_http.py b/transports/stratum_http.py index eace4abf..763de8cd 100644 --- a/transports/stratum_http.py +++ b/transports/stratum_http.py @@ -87,7 +87,7 @@ def _marshaled_dispatch(self, session_id, data, dispatch_method=None): response = None try: request = jsonrpclib.loads(data) - except Exception, e: + except Exception as e: fault = Fault(-32700, 'Request %s invalid. (%s)' % (data, e)) response = fault.response() return response @@ -170,7 +170,7 @@ def do_GET(self): data = json.dumps([]) response = self.server._marshaled_dispatch(session_id, data) self.send_response(200) - except Exception, e: + except Exception as e: self.send_response(500) err_lines = traceback.format_exc().splitlines() trace_string = '%s | %s' % (err_lines[-3], err_lines[-1]) @@ -218,7 +218,7 @@ def do_POST(self): response = self.server._marshaled_dispatch(session_id, data) self.send_response(200) - except Exception, e: + except Exception as e: self.send_response(500) err_lines = traceback.format_exc().splitlines() trace_string = '%s | %s' % (err_lines[-3], err_lines[-1]) diff --git a/transports/stratum_tcp.py b/transports/stratum_tcp.py index 5fb26a55..d7e4fe61 100644 --- a/transports/stratum_tcp.py +++ b/transports/stratum_tcp.py @@ -46,7 +46,7 @@ def stop(self): try: self._connection.shutdown(socket.SHUT_RDWR) - except: + except Exception: # print_log("problem shutting down", self.address) # traceback.print_exc(file=sys.stdout) pass @@ -73,7 +73,7 @@ def run(self): while data: l = self.session.connection().send(data) data = data[l:] - except: + except Exception: self.session.stop() @@ -90,7 +90,7 @@ def __init__(self, dispatcher, session): def run(self): try: self.session.do_handshake() - except: + except Exception: return while not self.shared.stopped(): @@ -115,7 +115,7 @@ def update(self): def receive(self): try: return self.session.connection().recv(2048) - except: + except Exception: return '' def parse(self): @@ -131,7 +131,7 @@ def parse(self): try: command = json.loads(raw_command) - except: + except Exception: self.dispatcher.push_response({"error": "bad JSON", "request": raw_command}) return True @@ -175,7 +175,7 @@ def run(self): #if self.use_ssl: print_log("SSL: socket listening") try: connection, address = sock.accept() - except: + except Exception: traceback.print_exc(file=sys.stdout) time.sleep(0.1) continue @@ -183,7 +183,7 @@ def run(self): #if self.use_ssl: print_log("SSL: new session", address) try: session = TcpSession(connection, address, use_ssl=self.use_ssl, ssl_certfile=self.ssl_certfile, ssl_keyfile=self.ssl_keyfile) - except BaseException, e: + except Exception as e: error = str(e) print_log("cannot start TCP session", error, address) connection.close() diff --git a/utils/__init__.py b/utils/__init__.py index f6e64f2b..5a4567ae 100644 --- a/utils/__init__.py +++ b/utils/__init__.py @@ -97,7 +97,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()