Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit1a5b2d1

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 beingcaught and replaced with a more user-friendly error. In these cases thesyntax `raise new_error from old_error` needs to be used.Python's exception chaining means it shows not only the traceback of thecurrent exception, but that of the original exception (and possiblymore.) 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 theexception-handling code itself, which is a separate situation thanwrapping an exception.
1 parentb8ac79f commit1a5b2d1

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-
exceptValueError:
61+
exceptValueErrorasex:
6262
parser.print_help()
6363
print('Not a valid number: %s'%cli_args[0],file=sys.stderr)
64-
raiseSystemExit(1)
64+
raiseSystemExit(1)fromex
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-
exceptrsa.VerificationError:
284-
raiseSystemExit('Verification failed.')
283+
exceptrsa.VerificationErrorasex:
284+
raiseSystemExit('Verification failed.')fromex
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
returnmethods[file_format]
134-
exceptKeyError:
134+
exceptKeyErrorasex:
135135
formats=', '.join(sorted(methods.keys()))
136136
raiseValueError('Unsupported format: %r, try one of %s'% (file_format,
137-
formats))
137+
formats))fromex
138138

139139
defsave_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
raisersa.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))fromex
707707

708708
if (exponent*d)%phi_n!=1:
709709
raiseValueError("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-
exceptKeyError:
52+
exceptKeyErrorasex:
5353
raiseValueError(
5454
'Invalid `hasher` specified. Please select one of: {hash_list}'.format(
5555
hash_list=', '.join(sorted(pkcs1.HASH_METHODS.keys()))
5656
)
57-
)
57+
)fromex
5858

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

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp