]> www.wagner.pp.ru Git - oss/ctypescrypto.git/blob - ctypescrypto/exception.py
c4710eca32742a817bfb1204a0d7550bcce8d433
[oss/ctypescrypto.git] / ctypescrypto / exception.py
1 """
2 Exception which extracts libcrypto error information
3 """
4 from ctypes import *
5 from ctypescrypto import libcrypto
6 strings_loaded=False
7 class LibCryptoError(Exception):
8         """
9                 Exception for libcrypto errors. Adds all the info, which can be
10                 extracted from internal (per-thread) libcrypto error stack to the message,
11                 passed to the constructor.
12         """
13         def __init__(self,msg):
14                 global strings_loaded
15                 if not strings_loaded:
16                         libcrypto.ERR_load_crypto_strings()
17                         strings_loaded = True
18                 e=libcrypto.ERR_get_error()
19                 m = msg
20                 while e != 0:
21                         m+="\n\t"+libcrypto.ERR_lib_error_string(e)+":"+\
22                           libcrypto.ERR_func_error_string(e)+":"+\
23                           libcrypto.ERR_reason_error_string(e)
24                         e=libcrypto.ERR_get_error()
25                 self.args=(m,)
26
27 def clear_err_stack():
28         """
29           Clears internal libcrypto err stack. Call it if you've checked
30           return code and processed exceptional situation, so subsequent
31           raising of the LibCryptoError wouldn't list already handled errors
32         """
33         libcrypto.ERR_clear_error()
34
35
36 libcrypto.ERR_lib_error_string.restype=c_char_p
37 libcrypto.ERR_func_error_string.restype=c_char_p
38 libcrypto.ERR_reason_error_string.restype=c_char_p