X-Git-Url: https://www.wagner.pp.ru/gitweb/?a=blobdiff_plain;f=ctypescrypto%2Fexception.py;h=5eec20a0a471eb38c6460a14bd40914b619161ef;hb=287e8a5b1d7f5a8129619f733adbfc8b2de3e4c7;hp=0e6fc438ad8a4dcb6076915eae2470be55191c3f;hpb=954b6dc9e3312f8d8b49f20f8466e6d2a8342f35;p=oss%2Fctypescrypto.git diff --git a/ctypescrypto/exception.py b/ctypescrypto/exception.py index 0e6fc43..5eec20a 100644 --- a/ctypescrypto/exception.py +++ b/ctypescrypto/exception.py @@ -1,50 +1,39 @@ """ Exception which extracts libcrypto error information """ -from ctypes import * +from ctypes import c_ulong, c_char_p, create_string_buffer from ctypescrypto import libcrypto -strings_loaded=False +strings_loaded = False -__all__ = ['LibCryptoError','clear_err_stack'] - -def _check_null(s): - """ - Handle transparently NULL returned from error reporting functions - instead of strings - """ - if s is None: - return "" - return s +__all__ = ['LibCryptoError', 'clear_err_stack'] 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. + 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): + 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"+_check_null(libcrypto.ERR_lib_error_string(e))+":"+\ - _check_null(libcrypto.ERR_func_error_string(e))+":"+\ - _check_null(libcrypto.ERR_reason_error_string(e)) - e=libcrypto.ERR_get_error() - self.args=(m,) + err_code = libcrypto.ERR_get_error() + mesg = msg + buf = create_string_buffer(128) + while err_code != 0: + mesg += "\n\t" + libcrypto.ERR_error_string(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 + 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)