Skip to content

Commit

Permalink
Re-raise exceptions explicitly using the 'from' keyword
Browse files Browse the repository at this point in the history
Versions 2.6.0 and later of pylint adhere to PEP 3134
and trigger a 'raise-missing-from' warning (W0707) when
chained exceptions are raised implicitly.

The 'from' keyword is a Python3.x feature, that is why
six.raise_from is used for Python2.x compatibility.

Signed-off-by Teodora Sechkova <tsechkova@vmware.com>
  • Loading branch information
sechkova committed Aug 24, 2020
1 parent e3ff011 commit e487b5f
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 26 deletions.
7 changes: 4 additions & 3 deletions tuf/client/updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -1585,9 +1585,10 @@ def _get_metadata_file(self, metadata_role, remote_filename,
str(metadata_spec_version) +
". The update will continue as the major versions match.")

except (ValueError, TypeError):
raise securesystemslib.exceptions.FormatError('Improperly'
' formatted spec_version, which must be in major.minor.fix format')
except (ValueError, TypeError) as error:
six.raise_from(securesystemslib.exceptions.FormatError('Improperly'
' formatted spec_version, which must be in major.minor.fix format'),
error)

# If the version number is unspecified, ensure that the version number
# downloaded is greater than the currently trusted version number for
Expand Down
11 changes: 6 additions & 5 deletions tuf/formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -954,15 +954,16 @@ def check_signable_object_format(signable):
try:
role_type = signable['signed']['_type']

except (KeyError, TypeError):
raise securesystemslib.exceptions.FormatError('Untyped signable object.')
except (KeyError, TypeError) as error:
six.raise_from(securesystemslib.exceptions.FormatError(
'Untyped signable object.'), error)

try:
schema = SCHEMAS_BY_TYPE[role_type]

except KeyError:
raise securesystemslib.exceptions.FormatError('Unrecognized'
' type ' + repr(role_type))
except KeyError as error:
six.raise_from(securesystemslib.exceptions.FormatError(
'Unrecognized type ' + repr(role_type)), error)

# 'securesystemslib.exceptions.FormatError' raised if 'signable' does not
# have a properly formatted role schema.
Expand Down
4 changes: 2 additions & 2 deletions tuf/keydb.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,8 +341,8 @@ def get_key(keyid, repository_name='default'):
try:
return copy.deepcopy(_keydb_dict[repository_name][keyid])

except KeyError:
raise tuf.exceptions.UnknownKeyError('Key: ' + keyid)
except KeyError as error:
six.raise_from(tuf.exceptions.UnknownKeyError('Key: ' + keyid), error)



Expand Down
24 changes: 12 additions & 12 deletions tuf/repository_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -534,9 +534,9 @@ def _load_top_level_metadata(repository, top_level_filenames, repository_name):
# Ensure the 'consistent_snapshot' field is extracted.
consistent_snapshot = root_metadata['consistent_snapshot']

except securesystemslib.exceptions.StorageError:
raise tuf.exceptions.RepositoryError('Cannot load the required'
' root file: ' + repr(root_filename))
except securesystemslib.exceptions.StorageError as error:
six.raise_from(tuf.exceptions.RepositoryError('Cannot load the required'
' root file: ' + repr(root_filename)), error)

# Load 'timestamp.json'. A Timestamp role file without a version number is
# always written.
Expand All @@ -563,9 +563,9 @@ def _load_top_level_metadata(repository, top_level_filenames, repository_name):
tuf.roledb.update_roleinfo('timestamp', roleinfo, mark_role_as_dirty=False,
repository_name=repository_name)

except securesystemslib.exceptions.StorageError:
raise tuf.exceptions.RepositoryError('Cannot load the Timestamp file: '
+ repr(timestamp_filename))
except securesystemslib.exceptions.StorageError as error:
six.raise_from(tuf.exceptions.RepositoryError('Cannot load the Timestamp '
'file: ' + repr(timestamp_filename)), error)

# Load 'snapshot.json'. A consistent snapshot.json must be calculated if
# 'consistent_snapshot' is True.
Expand Down Expand Up @@ -603,9 +603,9 @@ def _load_top_level_metadata(repository, top_level_filenames, repository_name):
tuf.roledb.update_roleinfo('snapshot', roleinfo, mark_role_as_dirty=False,
repository_name=repository_name)

except securesystemslib.exceptions.StorageError:
raise tuf.exceptions.RepositoryError('The Snapshot file cannot be loaded: '
+ repr(snapshot_filename))
except securesystemslib.exceptions.StorageError as error:
six.raise_from(tuf.exceptions.RepositoryError('The Snapshot file '
'cannot be loaded: '+ repr(snapshot_filename)), error)

# Load 'targets.json'. A consistent snapshot of the Targets role must be
# calculated if 'consistent_snapshot' is True.
Expand Down Expand Up @@ -659,9 +659,9 @@ def _load_top_level_metadata(repository, top_level_filenames, repository_name):
except tuf.exceptions.KeyAlreadyExistsError:
pass

except securesystemslib.exceptions.StorageError:
raise tuf.exceptions.RepositoryError('The Targets file can not be loaded: '
+ repr(targets_filename))
except securesystemslib.exceptions.StorageError as error:
six.raise_from(tuf.exceptions.RepositoryError('The Targets file '
'can not be loaded: ' + repr(targets_filename)), error)

return repository, consistent_snapshot

Expand Down
8 changes: 4 additions & 4 deletions tuf/scripts/repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -454,10 +454,10 @@ def import_privatekey_from_file(keypath, password=None):
key_object = securesystemslib.keys.import_rsakey_from_private_pem(
encrypted_key, 'rsassa-pss-sha256', password)

except securesystemslib.exceptions.CryptoError:
raise tuf.exceptions.Error(repr(keypath) + ' cannot be imported, possibly'
' because an invalid key file is given or the decryption password is'
' incorrect.')
except securesystemslib.exceptions.CryptoError as error:
six.raise_from(tuf.exceptions.Error(repr(keypath) + ' cannot be '
' imported, possibly because an invalid key file is given or '
' the decryption password is incorrect.'), error)

if key_object['keytype'] not in SUPPORTED_KEY_TYPES:
raise tuf.exceptions.Error('Trying to import an unsupported key'
Expand Down

0 comments on commit e487b5f

Please sign in to comment.