@@ -170,8 +170,8 @@ def _unpack_by_int(self, data, current_position):
170170 # Unpack length of data field
171171 try :
172172 requested_data_length = struct .unpack ('>I' , data [current_position :current_position + self .INT_LEN ])[0 ]
173- except struct .error :
174- raise MalformedDataError ("Unable to unpack %s bytes from the data" % self .INT_LEN )
173+ except struct .error as ex :
174+ raise MalformedDataError ("Unable to unpack %s bytes from the data" % self .INT_LEN ) from ex
175175
176176 # Move pointer to the beginning of the data field
177177 current_position += self .INT_LEN
@@ -240,8 +240,8 @@ def decode_key(cls, pubkey_content):
240240 """Decode base64 coded part of the key."""
241241 try :
242242 decoded_key = base64 .b64decode (pubkey_content .encode ("ascii" ))
243- except (TypeError , binascii .Error ):
244- raise MalformedDataError ("Unable to decode the key" )
243+ except (TypeError , binascii .Error ) as ex :
244+ raise MalformedDataError ("Unable to decode the key" ) from ex
245245 return decoded_key
246246
247247 @classmethod
@@ -363,8 +363,8 @@ def _process_ecdsa_sha(self, data):
363363 try :
364364 # data starts with \x04, which should be discarded.
365365 ecdsa_key = ecdsa .VerifyingKey .from_string (key_data [1 :], curve , hash_algorithm )
366- except AssertionError :
367- raise InvalidKeyError ("Invalid ecdsa key" )
366+ except AssertionError as ex :
367+ raise InvalidKeyError ("Invalid ecdsa key" ) from ex
368368 self .bits = int (curve_information .replace (b"nistp" , b"" ))
369369 self .ecdsa = ecdsa_key
370370 return current_position
0 commit comments