Skip to content
This repository has been archived by the owner on Jul 12, 2021. It is now read-only.

use Exception instead of BaseException or nothing #45

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 24 additions & 24 deletions backends/bitcoind/blockchain_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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):
Expand Down Expand Up @@ -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

Expand All @@ -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

Expand Down Expand Up @@ -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

Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand All @@ -704,15 +704,15 @@ 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)

elif method == 'blockchain.address.get_history':
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)

Expand All @@ -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)

Expand All @@ -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)

Expand All @@ -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)

Expand All @@ -755,15 +755,15 @@ 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)

elif method == 'blockchain.transaction.get':
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)

Expand Down Expand Up @@ -798,15 +798,15 @@ 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()

r = loads(respdata)
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
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions backends/bitcoind/deserialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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:
Expand Down
10 changes: 5 additions & 5 deletions backends/irc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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]
Expand All @@ -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()
Expand Down Expand Up @@ -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:
Expand Down
8 changes: 4 additions & 4 deletions processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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()
Expand All @@ -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

Expand All @@ -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


Expand Down
4 changes: 2 additions & 2 deletions server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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")
6 changes: 3 additions & 3 deletions transports/stratum_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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])
Expand Down Expand Up @@ -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])
Expand Down
Loading