X-Git-Url: http://wagner.pp.ru/gitweb/?a=blobdiff_plain;f=ctypescrypto%2Fexception.py;h=18dc5a4aa17c3164b4bf3a0381e807976a25d994;hb=305f347018c18fa4183f8ec76177336881b713c9;hp=c4710eca32742a817bfb1204a0d7550bcce8d433;hpb=b4ee51a0aca14c0af5853545b9e524cc1b57b656;p=oss%2Fctypescrypto.git diff --git a/ctypescrypto/exception.py b/ctypescrypto/exception.py index c4710ec..18dc5a4 100644 --- a/ctypescrypto/exception.py +++ b/ctypescrypto/exception.py @@ -1,38 +1,44 @@ """ Exception which extracts libcrypto error information """ -from ctypes import * -from ctypescrypto import libcrypto -strings_loaded=False +from ctypes import c_ulong, c_char_p, create_string_buffer +from ctypescrypto import libcrypto, strings_loaded, pyver + +__all__ = ['LibCryptoError', 'clear_err_stack'] + +if pyver == 2: + def _get_error_str(err_code,buf): + return libcrypto.ERR_error_string(err_code,buf) +else: + def _get_error_str(err_code,buf): + return libcrypto.ERR_error_string(err_code,buf).decode('utf-8') class LibCryptoError(Exception): - """ - Exception for libcrypto errors. Adds all the info, which can be - extracted from internal (per-thread) libcrypto error stack to the message, - passed to the constructor. - """ - def __init__(self,msg): - global strings_loaded - if not strings_loaded: - libcrypto.ERR_load_crypto_strings() - strings_loaded = True - e=libcrypto.ERR_get_error() - m = msg - while e != 0: - m+="\n\t"+libcrypto.ERR_lib_error_string(e)+":"+\ - libcrypto.ERR_func_error_string(e)+":"+\ - libcrypto.ERR_reason_error_string(e) - e=libcrypto.ERR_get_error() - self.args=(m,) + """ + Exception for libcrypto errors. Adds all the info, which can be + extracted from internal (per-thread) libcrypto error stack to the message, + passed to the constructor. + """ + def __init__(self, msg): + global strings_loaded + if not strings_loaded: + libcrypto.ERR_load_crypto_strings() + strings_loaded = True + err_code = libcrypto.ERR_get_error() + mesg = msg + buf = create_string_buffer(128) + while err_code != 0: + mesg += "\n\t" + _get_error_str(err_code, buf) + err_code = libcrypto.ERR_get_error() + super(LibCryptoError, self).__init__(mesg) def clear_err_stack(): - """ - Clears internal libcrypto err stack. Call it if you've checked - return code and processed exceptional situation, so subsequent - raising of the LibCryptoError wouldn't list already handled errors - """ - libcrypto.ERR_clear_error() - + """ + Clears internal libcrypto err stack. Call it if you've checked + return code and processed exceptional situation, so subsequent + raising of the LibCryptoError wouldn't list already handled errors + """ + libcrypto.ERR_clear_error() -libcrypto.ERR_lib_error_string.restype=c_char_p -libcrypto.ERR_func_error_string.restype=c_char_p -libcrypto.ERR_reason_error_string.restype=c_char_p +libcrypto.ERR_get_error.restype = c_ulong +libcrypto.ERR_error_string.restype = c_char_p +libcrypto.ERR_error_string.argtypes = (c_ulong, c_char_p)