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