#NEWLINE# def generateTryCode( statement , emit , context ) : #NEWLINE# #INDENT# if generateTryNextExceptStopIterationCode ( statement , emit , context ) : #NEWLINE# #INDENT# return #NEWLINE# #UNINDENT# from . CodeGeneration import generateStatementSequenceCode #NEWLINE# tried_block = statement . getBlockTry ( ) #NEWLINE# except_handler = statement . getBlockExceptHandler ( ) #NEWLINE# continue_handler = statement . getBlockContinueHandler ( ) #NEWLINE# break_handler = statement . getBlockBreakHandler ( ) #NEWLINE# return_handler = statement . getBlockReturnHandler ( ) #NEWLINE# tried_block_may_raise = tried_block . mayRaiseException ( BaseException ) #NEWLINE# assert ( tried_block_may_raise or ( continue_handler is not None ) or ( break_handler is not None ) or ( return_handler is not None ) ) , statement . asXmlText ( ) #NEWLINE# tried_handler_escape = context . allocateLabel ( 'try_except_handler' ) #NEWLINE# if tried_block_may_raise : #NEWLINE# #INDENT# old_exception_escape = context . setExceptionEscape ( tried_handler_escape ) #NEWLINE# #UNINDENT# continue_handler_escape = context . allocateLabel ( 'try_continue_handler' ) #NEWLINE# if ( continue_handler is not None ) : #NEWLINE# #INDENT# old_continue_target = context . setLoopContinueTarget ( continue_handler_escape ) #NEWLINE# #UNINDENT# break_handler_escape = context . allocateLabel ( 'try_break_handler' ) #NEWLINE# if ( break_handler is not None ) : #NEWLINE# #INDENT# old_break_target = context . setLoopBreakTarget ( break_handler_escape ) #NEWLINE# #UNINDENT# return_handler_escape = context . allocateLabel ( 'try_return_handler' ) #NEWLINE# if ( return_handler is not None ) : #NEWLINE# #INDENT# old_return_target = context . setReturnTarget ( return_handler_escape ) #NEWLINE# #UNINDENT# emit ( '// Tried code:' ) #NEWLINE# generateStatementSequenceCode ( statement_sequence = tried_block , emit = emit , allow_none = False , context = context ) #NEWLINE# if tried_block_may_raise : #NEWLINE# #INDENT# context . setExceptionEscape ( old_exception_escape ) #NEWLINE# #UNINDENT# if continue_handler : #NEWLINE# #INDENT# context . setLoopContinueTarget ( old_continue_target ) #NEWLINE# #UNINDENT# if break_handler : #NEWLINE# #INDENT# context . setLoopBreakTarget ( old_break_target ) #NEWLINE# #UNINDENT# if return_handler : #NEWLINE# #INDENT# context . setReturnTarget ( old_return_target ) #NEWLINE# #UNINDENT# post_label = None #NEWLINE# if ( not tried_block . isStatementAborting ( ) ) : #NEWLINE# #INDENT# if ( post_label is None ) : #NEWLINE# #INDENT# post_label = context . allocateLabel ( 'try_end' ) #NEWLINE# #UNINDENT# getGotoCode ( post_label , emit ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# getMustNotGetHereCode ( reason = 'tried codes exits in all cases' , context = context , emit = emit ) #NEWLINE# #UNINDENT# if ( return_handler is not None ) : #NEWLINE# #INDENT# assert tried_block . mayReturn ( ) #NEWLINE# emit ( '// Return handler code:' ) #NEWLINE# getLabelCode ( return_handler_escape , emit ) #NEWLINE# old_return_value_release = context . setReturnReleaseMode ( True ) #NEWLINE# generateStatementSequenceCode ( statement_sequence = return_handler , emit = emit , allow_none = False , context = context ) #NEWLINE# context . setReturnReleaseMode ( old_return_value_release ) #NEWLINE# assert return_handler . isStatementAborting ( ) #NEWLINE# #UNINDENT# if tried_block_may_raise : #NEWLINE# #INDENT# emit ( '// Exception handler code:' ) #NEWLINE# getLabelCode ( tried_handler_escape , emit ) #NEWLINE# ( keeper_type , keeper_value , keeper_tb , keeper_lineno ) = context . allocateExceptionKeeperVariables ( ) #NEWLINE# old_keepers = context . setExceptionKeeperVariables ( ( keeper_type , keeper_value , keeper_tb , keeper_lineno ) ) #NEWLINE# assert ( keeper_type is not None ) #NEWLINE# emit ( ( '%(keeper_type)s = exception_type;\n%(keeper_value)s = exception_value;\n%(keeper_tb)s = exception_tb;\n%(keeper_lineno)s = exception_lineno;\nexception_type = NULL;\nexception_value = NULL;\nexception_tb = NULL;\nexception_lineno = -1;\n' % { #NEWLINE# #INDENT# 'keeper_type' : keeper_type , #NEWLINE# 'keeper_value' : keeper_value , #NEWLINE# 'keeper_tb' : keeper_tb , #NEWLINE# 'keeper_lineno' : keeper_lineno , #NEWLINE# #UNINDENT# } ) ) #NEWLINE# generateStatementSequenceCode ( statement_sequence = except_handler , emit = emit , allow_none = True , context = context ) #NEWLINE# if ( ( except_handler is None ) or ( not except_handler . isStatementAborting ( ) ) ) : #NEWLINE# #INDENT# getExceptionUnpublishedReleaseCode ( emit , context ) #NEWLINE# if ( post_label is None ) : #NEWLINE# #INDENT# post_label = context . allocateLabel ( 'try_end' ) #NEWLINE# #UNINDENT# getGotoCode ( post_label , emit ) #NEWLINE# getMustNotGetHereCode ( reason = 'exception handler codes exits in all cases' , context = context , emit = emit ) #NEWLINE# #UNINDENT# context . setExceptionKeeperVariables ( old_keepers ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# assert ( except_handler is None ) #NEWLINE# #UNINDENT# if ( break_handler is not None ) : #NEWLINE# #INDENT# assert tried_block . mayBreak ( ) #NEWLINE# emit ( '// try break handler code:' ) #NEWLINE# getLabelCode ( break_handler_escape , emit ) #NEWLINE# generateStatementSequenceCode ( statement_sequence = break_handler , emit = emit , allow_none = False , context = context ) #NEWLINE# assert break_handler . isStatementAborting ( ) #NEWLINE# #UNINDENT# if ( continue_handler is not None ) : #NEWLINE# #INDENT# assert tried_block . mayContinue ( ) #NEWLINE# emit ( '// try continue handler code:' ) #NEWLINE# getLabelCode ( continue_handler_escape , emit ) #NEWLINE# generateStatementSequenceCode ( statement_sequence = continue_handler , emit = emit , allow_none = False , context = context ) #NEWLINE# assert continue_handler . isStatementAborting ( ) #NEWLINE# #UNINDENT# emit ( '// End of try:' ) #NEWLINE# if ( post_label is not None ) : #NEWLINE# #INDENT# getLabelCode ( post_label , emit )