Skip to content
This repository has been archived by the owner on Feb 1, 2024. It is now read-only.

Commit

Permalink
Fix pylint W0707 errors
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Beck-Buysse <[email protected]>
  • Loading branch information
rbuysse committed Aug 25, 2020
1 parent 19bfdee commit c27b962
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ def __init__(self, url, keyfile=None):
fd.close()
except OSError as err:
raise IntkeyClientException(
'Failed to read private key: {}'.format(str(err)))
'Failed to read private key: {}'.format(str(err))) from err

try:
private_key = Secp256k1PrivateKey.from_hex(private_key_str)
except ParseError as e:
raise IntkeyClientException(
'Unable to load private key: {}'.format(str(e)))
'Unable to load private key: {}'.format(str(e))) from e

self._signer = CryptoFactory(
create_context('secp256k1')).new_signer(private_key)
Expand Down Expand Up @@ -105,7 +105,7 @@ def _get_status(self, batch_id, wait):
'batch_statuses?id={}&wait={}'.format(batch_id, wait),)
return yaml.safe_load(result)['data'][0]['status']
except BaseException as err:
raise IntkeyClientException(err)
raise IntkeyClientException(err) from err

def _get_prefix(self):
return _sha512('intkey'.encode('utf-8'))[0:6]
Expand Down Expand Up @@ -141,10 +141,10 @@ def _send_request(self, suffix, data=None, content_type=None, name=None):

except requests.ConnectionError as err:
raise IntkeyClientException(
'Failed to connect to REST API: {}'.format(err))
'Failed to connect to REST API: {}'.format(err)) from err

except BaseException as err:
raise IntkeyClientException(err)
raise IntkeyClientException(err) from err

return result.text

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ def __init__(self, delegate, args):
self._signer = crypto_factory.new_signer(
private_key=private_key)
except ParseError as pe:
raise IntKeyCliException(str(pe))
raise IntKeyCliException(str(pe)) from pe
except IOError as ioe:
raise IntKeyCliException(str(ioe))
raise IntKeyCliException(str(ioe)) from ioe
else:
self._signer = crypto_factory.new_signer(
context.new_random_private_key())
Expand Down
16 changes: 8 additions & 8 deletions examples/intkey_python/sawtooth_intkey/processor/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,23 +83,23 @@ def _unpack_transaction(transaction):
def _decode_transaction(transaction):
try:
content = cbor.loads(transaction.payload)
except:
raise InvalidTransaction('Invalid payload serialization')
except Exception as e:
raise InvalidTransaction('Invalid payload serialization') from e

try:
verb = content['Verb']
except AttributeError:
raise InvalidTransaction('Verb is required')
raise InvalidTransaction('Verb is required') from AttributeError

This comment has been minimized.

Copy link
@cool-RR

cool-RR Jan 22, 2021

Heads up, this code is probably an error. Similar lines as well. You should give the exception a name like you do above this line.

More info: https://bugs.python.org/issue43002 FYI, I implemented the W0707 check in PyLint.

Also, looking at your code a bit deeper, I'm not sure why content['Verb'] would raise AttributeError rather than KeyError. But I'm not familiar with the object used so I could be wrong.


try:
name = content['Name']
except AttributeError:
raise InvalidTransaction('Name is required')
raise InvalidTransaction('Name is required') from AttributeError

try:
value = content['Value']
except AttributeError:
raise InvalidTransaction('Value is required')
raise InvalidTransaction('Value is required') from AttributeError

return verb, name, value

Expand Down Expand Up @@ -134,8 +134,8 @@ def _get_state_data(name, context):
return cbor.loads(state_entries[0].data)
except IndexError:
return {}
except:
raise InternalError('Failed to load state data')
except Exception as e:
raise InternalError('Failed to load state data') from e


def _set_state_data(name, state, context):
Expand All @@ -160,7 +160,7 @@ def _do_intkey(verb, name, value, state):
return verbs[verb](name, value, state)
except KeyError:
# This would be a programming error.
raise InternalError('Unhandled verb: {}'.format(verb))
raise InternalError('Unhandled verb: {}'.format(verb)) from KeyError


def _do_set(name, value, state):
Expand Down
2 changes: 1 addition & 1 deletion examples/xo_python/sawtooth_xo/processor/config/xo.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def load_toml_xo_config(filename):
except IOError as e:
raise LocalConfigurationError(
"Unable to load transaction processor configuration file:"
" {}".format(str(e)))
" {}".format(str(e))) from e

toml_config = toml.loads(raw_config)
invalid_keys = set(toml_config.keys()).difference(
Expand Down
6 changes: 3 additions & 3 deletions examples/xo_python/sawtooth_xo/processor/xo_payload.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ def __init__(self, payload):
try:
# The payload is csv utf-8 encoded string
name, action, space = payload.decode().split(",")
except ValueError:
raise InvalidTransaction("Invalid payload serialization")
except ValueError as e:
raise InvalidTransaction("Invalid payload serialization") from e

if not name:
raise InvalidTransaction('Name is required')
Expand All @@ -45,7 +45,7 @@ def __init__(self, payload):
"Space must be an integer from 1 to 9")
except ValueError:
raise InvalidTransaction(
'Space must be an integer from 1 to 9')
'Space must be an integer from 1 to 9') from ValueError

if action == 'take':
space = int(space)
Expand Down
4 changes: 2 additions & 2 deletions examples/xo_python/sawtooth_xo/processor/xo_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@ def _deserialize(self, data):
name, board, state, player1, player2 = game.split(",")

games[name] = Game(name, board, state, player1, player2)
except ValueError:
raise InternalError("Failed to deserialize game data")
except ValueError as e:
raise InternalError("Failed to deserialize game data") from e

return games

Expand Down
10 changes: 5 additions & 5 deletions examples/xo_python/sawtooth_xo/xo_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@ def __init__(self, base_url, keyfile=None):
except OSError as err:
raise XoException(
'Failed to read private key {}: {}'.format(
keyfile, str(err)))
keyfile, str(err))) from err

try:
private_key = Secp256k1PrivateKey.from_hex(private_key_str)
except ParseError as e:
raise XoException(
'Unable to load private key: {}'.format(str(e)))
'Unable to load private key: {}'.format(str(e))) from e

self._signer = CryptoFactory(create_context('secp256k1')) \
.new_signer(private_key)
Expand Down Expand Up @@ -130,7 +130,7 @@ def _get_status(self, batch_id, wait, auth_user=None, auth_password=None):
auth_password=auth_password)
return yaml.safe_load(result)['data'][0]['status']
except BaseException as err:
raise XoException(err)
raise XoException(err) from err

def _get_prefix(self):
return _sha512('xo'.encode('utf-8'))[0:6]
Expand Down Expand Up @@ -177,10 +177,10 @@ def _send_request(self,

except requests.ConnectionError as err:
raise XoException(
'Failed to connect to {}: {}'.format(url, str(err)))
'Failed to connect to {}: {}'.format(url, str(err))) from err

except BaseException as err:
raise XoException(err)
raise XoException(err) from err

return result.text

Expand Down
9 changes: 6 additions & 3 deletions sawtooth_signing/secp256k1.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ def from_hex(hex_str):
try:
return Secp256k1PrivateKey.from_bytes(binascii.unhexlify(hex_str))
except Exception as e:
raise ParseError('Unable to parse hex private key: {}'.format(e))
raise ParseError('Unable to parse hex private key: {}'.format(
e)) from e

@staticmethod
def new_random():
Expand Down Expand Up @@ -91,7 +92,8 @@ def from_hex(hex_str):
try:
return Secp256k1PublicKey.from_bytes(binascii.unhexlify(hex_str))
except Exception as e:
raise ParseError('Unable to parse hex public key: {}'.format(e))
raise ParseError('Unable to parse hex public key: {}'.format(
e)) from e


class Secp256k1Context(Context):
Expand All @@ -109,7 +111,8 @@ def sign(self, message, private_key):

return signature.hex()
except Exception as e:
raise SigningError('Unable to sign message: {}'.format(str(e)))
raise SigningError('Unable to sign message: {}'.format(
str(e))) from e

def verify(self, signature, message, public_key):
try:
Expand Down
4 changes: 2 additions & 2 deletions tests/sawtooth_integration/tests/integration_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,11 @@ def _submit_request(self, url, params=None, data=None,
except requests.exceptions.HTTPError as excp:
return (excp.response.status_code, excp.response.reason)
except RemoteDisconnected as excp:
raise Exception(excp)
raise Exception(excp) from excp
except requests.exceptions.ConnectionError as excp:
raise Exception(
('Unable to connect to "{}": '
'make sure URL is correct').format(self.url))
'make sure URL is correct').format(self.url)) from excp

@staticmethod
def _format_queries(queries):
Expand Down

0 comments on commit c27b962

Please sign in to comment.