#NEWLINE# def exit_with_exc_info( code = 1 , message = '' , print_tb = False , exception = None ) : #NEWLINE# #INDENT# 'Exits the program, printing information about the last exception (if\n    any) and an optional error message.  Uses *exception* instead if provided.\n\n    :param code: Exit code.\n    :type code: integer (valid exit code, 0-255)\n    :param message: Message to be printed after the exception information.\n    :type message: string\n    :param print_tb: If set to True, prints the exception traceback; otherwise, suppresses it.\n    :type print_tb: boolean\n    :type exception: an exception to use in place of the last exception raised\n    ' #NEWLINE# ( exc_type , exc_value ) = ( ( exception . __class__ , exception ) if ( exception is not None ) else sys . exc_info ( ) [ : 2 ] ) #NEWLINE# if ( exc_type is not None ) : #NEWLINE# #INDENT# if print_tb : #NEWLINE# #INDENT# traceback . print_exc ( ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if isinstance ( exc_value , KeyboardInterrupt ) : #NEWLINE# #INDENT# sys . stderr . write ( '^C\n' ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# for line in traceback . format_exception_only ( exc_type , exc_value ) : #NEWLINE# #INDENT# sys . stderr . write ( line ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# sys . stderr . write ( message ) #NEWLINE# if ( ( message != '' ) and ( not message . endswith ( '\n' ) ) ) : #NEWLINE# #INDENT# sys . stderr . write ( '\n' ) #NEWLINE# #UNINDENT# sys . exit ( code )