Skip to content
This repository was archived by the owner on Apr 20, 2025. It is now read-only.

Commit 9b1bb0d

Browse files
committed
Fix exception causes all over the codebase
1 parent 4beb68d commit 9b1bb0d

File tree

4 files changed

+11
-11
lines changed

4 files changed

+11
-11
lines changed

rsa/cli.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,10 @@ def keygen() -> None:
5858

5959
try:
6060
keysize = int(cli_args[0])
61-
except ValueError:
61+
except ValueError as ex:
6262
parser.print_help()
6363
print('Not a valid number: %s' % cli_args[0], file=sys.stderr)
64-
raise SystemExit(1)
64+
raise SystemExit(1) from ex
6565

6666
print('Generating %i-bit key' % keysize, file=sys.stderr)
6767
(pub_key, priv_key) = rsa.newkeys(keysize)
@@ -280,8 +280,8 @@ def perform_operation(self, indata: bytes, pub_key: rsa.key.AbstractKey,
280280

281281
try:
282282
rsa.verify(indata, signature, pub_key)
283-
except rsa.VerificationError:
284-
raise SystemExit('Verification failed.')
283+
except rsa.VerificationError as ex:
284+
raise SystemExit('Verification failed.') from ex
285285

286286
print('Verification OK', file=sys.stderr)
287287

rsa/key.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,10 @@ def _assert_format_exists(file_format: str, methods: typing.Mapping[str, typing.
123123

124124
try:
125125
return methods[file_format]
126-
except KeyError:
126+
except KeyError as ex:
127127
formats = ', '.join(sorted(methods.keys()))
128128
raise ValueError('Unsupported format: %r, try one of %s' % (file_format,
129-
formats))
129+
formats)) from ex
130130

131131
def save_pkcs1(self, format: str = 'PEM') -> bytes:
132132
"""Saves the key in PKCS#1 DER or PEM format.
@@ -675,7 +675,7 @@ def calculate_keys_custom_exponent(p: int, q: int, exponent: int) -> typing.Tupl
675675
raise rsa.common.NotRelativePrimeError(
676676
exponent, phi_n, ex.d,
677677
msg="e (%d) and phi_n (%d) are not relatively prime (divider=%i)" %
678-
(exponent, phi_n, ex.d))
678+
(exponent, phi_n, ex.d)) from ex
679679

680680
if (exponent * d) % phi_n != 1:
681681
raise ValueError("e (%d) and d (%d) are not mult. inv. modulo "

rsa/pkcs1.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,8 +261,8 @@ def decrypt(crypto: bytes, priv_key: key.PrivateKey) -> bytes:
261261
# Find the 00 separator between the padding and the message
262262
try:
263263
sep_idx = cleartext.index(b'\x00', 2)
264-
except ValueError:
265-
raise DecryptionError('Decryption failed')
264+
except ValueError as ex:
265+
raise DecryptionError('Decryption failed') from ex
266266

267267
return cleartext[sep_idx + 1:]
268268

rsa/pkcs1_v2.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,12 @@ def mgf1(seed: bytes, length: int, hasher: str = 'SHA-1') -> bytes:
4949

5050
try:
5151
hash_length = pkcs1.HASH_METHODS[hasher]().digest_size
52-
except KeyError:
52+
except KeyError as ex:
5353
raise ValueError(
5454
'Invalid `hasher` specified. Please select one of: {hash_list}'.format(
5555
hash_list=', '.join(sorted(pkcs1.HASH_METHODS.keys()))
5656
)
57-
)
57+
) from ex
5858

5959
# If l > 2^32(hLen), output "mask too long" and stop.
6060
if length > (2**32 * hash_length):

0 commit comments

Comments
 (0)