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

Commit 1a5b2d1

Browse files
cool-RRsybrenstuvel
authored andcommitted
Fix exception causes all over the codebase
The mistake is this: In some parts of the code, an exception is being caught and replaced with a more user-friendly error. In these cases the syntax `raise new_error from old_error` needs to be used. Python's exception chaining means it shows not only the traceback of the current exception, but that of the original exception (and possibly more.) This is regardless of `raise from`. The usage of `raise from` tells Python to put a more accurate message between the tracebacks. Instead of this: During handling of the above exception, another exception occurred: You'll get this: The above exception was the direct cause of the following exception: The first is inaccurate, because it signifies a bug in the exception-handling code itself, which is a separate situation than wrapping an exception.
1 parent b8ac79f commit 1a5b2d1

File tree

3 files changed

+9
-9
lines changed

3 files changed

+9
-9
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
@@ -131,10 +131,10 @@ def _assert_format_exists(file_format: str, methods: typing.Mapping[str, typing.
131131

132132
try:
133133
return methods[file_format]
134-
except KeyError:
134+
except KeyError as ex:
135135
formats = ', '.join(sorted(methods.keys()))
136136
raise ValueError('Unsupported format: %r, try one of %s' % (file_format,
137-
formats))
137+
formats)) from ex
138138

139139
def save_pkcs1(self, format: str = 'PEM') -> bytes:
140140
"""Saves the key in PKCS#1 DER or PEM format.
@@ -703,7 +703,7 @@ def calculate_keys_custom_exponent(p: int, q: int, exponent: int) -> typing.Tupl
703703
raise rsa.common.NotRelativePrimeError(
704704
exponent, phi_n, ex.d,
705705
msg="e (%d) and phi_n (%d) are not relatively prime (divider=%i)" %
706-
(exponent, phi_n, ex.d))
706+
(exponent, phi_n, ex.d)) from ex
707707

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

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)