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