def buildReturnNode(provider, node, source_ref): if not provider.isExpressionFunctionBody() or \ provider.isClassDictCreation(): SyntaxErrors.raiseSyntaxError( "'return' outside function", source_ref, None if Utils.python_version < 300 else ( node.col_offset if provider.isPythonModule() else node.col_offset+4 ) ) expression = buildNode(provider, node.value, source_ref, allow_none = True) if expression is None: expression = ExpressionConstantRef( constant = None, source_ref = source_ref, user_provided = True ) # Indicate exceptions to potentially try/finally structures. indicator_statements = makeTryFinallyIndicatorStatements( is_loop_exit = False, source_ref = source_ref ) if indicator_statements and expression.mayRaiseException(BaseException): tmp_variable = provider.allocateTempVariable( temp_scope = provider.allocateTempScope("return"), name = "value" ) statements = [ StatementAssignmentVariable( variable_ref = ExpressionTargetTempVariableRef( variable = tmp_variable, source_ref = expression.getSourceReference() ), source = expression, source_ref = source_ref ) ] + indicator_statements + [ StatementReturn( expression = ExpressionTempVariableRef( variable = tmp_variable, source_ref = expression.getSourceReference() ), source_ref = source_ref ) ] return makeTryFinallyStatement( tried = statements, final = StatementReleaseVariable( variable = tmp_variable, tolerant = True, source_ref = source_ref ), source_ref = source_ref ) else: return makeStatementsSequenceOrStatement( statements = indicator_statements + [ StatementReturn( expression = expression, source_ref = source_ref ) ], source_ref = source_ref )
def wrapEvalGlobalsAndLocals(provider, globals_node, locals_node, temp_scope, source_ref): """ Wrap the locals and globals arguments for "eval". This is called from the outside, and when the node tree already exists. """ globals_keeper_variable = provider.allocateTempVariable( temp_scope = temp_scope, name = "globals" ) locals_keeper_variable = provider.allocateTempVariable( temp_scope = temp_scope, name = "locals" ) if locals_node is None: locals_node = ExpressionConstantRef( constant = None, source_ref = source_ref ) if globals_node is None: globals_node = ExpressionConstantRef( constant = None, source_ref = source_ref ) post_statements = [] if provider.isExpressionClassBody(): post_statements.append( StatementLocalsDictSync( locals_arg = ExpressionTempVariableRef( variable = locals_keeper_variable, source_ref = source_ref, ), source_ref = source_ref.atInternal() ) ) post_statements += [ StatementReleaseVariable( variable = globals_keeper_variable, source_ref = source_ref ), StatementReleaseVariable( variable = locals_keeper_variable, source_ref = source_ref ) ] # The locals default is dependent on exec_mode, globals or locals. locals_default = ExpressionConditional( condition = ExpressionComparisonIs( left = ExpressionTempVariableRef( variable = globals_keeper_variable, source_ref = source_ref ), right = ExpressionConstantRef( constant = None, source_ref = source_ref ), source_ref = source_ref ), expression_no = ExpressionTempVariableRef( variable = globals_keeper_variable, source_ref = source_ref ), expression_yes = _getLocalsClassNode(provider)( source_ref = source_ref ), source_ref = source_ref ) pre_statements = [ # First assign globals and locals temporary the values given. StatementAssignmentVariable( variable_ref = ExpressionTargetTempVariableRef( variable = globals_keeper_variable, source_ref = source_ref ), source = globals_node, source_ref = source_ref, ), StatementAssignmentVariable( variable_ref = ExpressionTargetTempVariableRef( variable = locals_keeper_variable, source_ref = source_ref ), source = locals_node, source_ref = source_ref, ), StatementConditional( condition = ExpressionComparisonIs( left = ExpressionTempVariableRef( variable = locals_keeper_variable, source_ref = source_ref ), right = ExpressionConstantRef( constant = None, source_ref = source_ref ), source_ref = source_ref ), yes_branch = makeStatementsSequenceFromStatement( StatementAssignmentVariable( variable_ref = ExpressionTargetTempVariableRef( variable = locals_keeper_variable, source_ref = source_ref ), source = locals_default, source_ref = source_ref, ) ), no_branch = None, source_ref = source_ref ), StatementConditional( condition = ExpressionComparisonIs( left = ExpressionTempVariableRef( variable = globals_keeper_variable, source_ref = source_ref ), right = ExpressionConstantRef( constant = None, source_ref = source_ref ), source_ref = source_ref ), yes_branch = makeStatementsSequenceFromStatement( StatementAssignmentVariable( variable_ref = ExpressionTargetTempVariableRef( variable = globals_keeper_variable, source_ref = source_ref ), source = ExpressionBuiltinGlobals( source_ref = source_ref ), source_ref = source_ref, ) ), no_branch = None, source_ref = source_ref ) ] return ( ExpressionTempVariableRef( variable = globals_keeper_variable, source_ref = source_ref if globals_node is None else globals_node.getSourceReference() ), ExpressionTempVariableRef( variable = locals_keeper_variable, source_ref = source_ref if locals_node is None else locals_node.getSourceReference() ), makeStatementsSequence(pre_statements, False, source_ref), makeStatementsSequence(post_statements, False, source_ref) )
def wrapEvalGlobalsAndLocals(provider, globals_node, locals_node, temp_scope, source_ref): """ Wrap the locals and globals arguments for eval and exec. For eval, this is called from the outside, and when the node tree already exists. """ globals_keeper_variable = provider.allocateTempVariable( temp_scope = temp_scope, name = "globals" ) locals_keeper_variable = provider.allocateTempVariable( temp_scope = temp_scope, name = "locals" ) if locals_node is None: locals_node = ExpressionConstantRef( constant = None, source_ref = source_ref ) if globals_node is None: globals_node = ExpressionConstantRef( constant = None, source_ref = source_ref ) post_statements = [ StatementDelVariable( variable_ref = ExpressionTargetTempVariableRef( variable = globals_keeper_variable.makeReference( provider ), source_ref = globals_node.getSourceReference() ), tolerant = False, source_ref = source_ref ), StatementDelVariable( variable_ref = ExpressionTargetTempVariableRef( variable = locals_keeper_variable.makeReference( provider ), source_ref = locals_node.getSourceReference() ), tolerant = False, source_ref = source_ref ) ] # The locals default is dependant on exec_mode, globals or locals. locals_default = ExpressionConditional( condition = ExpressionComparisonIs( left = ExpressionTempVariableRef( variable = globals_keeper_variable.makeReference( provider ), source_ref = source_ref ), right = ExpressionConstantRef( constant = None, source_ref = source_ref ), source_ref = source_ref ), no_expression = ExpressionTempVariableRef( variable = globals_keeper_variable.makeReference( provider ), source_ref = source_ref ), yes_expression = ExpressionBuiltinLocals( source_ref = source_ref ), source_ref = source_ref ) pre_statements = [ # First assign globals and locals temporary the values given. StatementAssignmentVariable( variable_ref = ExpressionTargetTempVariableRef( variable = globals_keeper_variable.makeReference( provider ), source_ref = source_ref ), source = globals_node, source_ref = source_ref, ), StatementAssignmentVariable( variable_ref = ExpressionTargetTempVariableRef( variable = locals_keeper_variable.makeReference( provider ), source_ref = source_ref ), source = locals_node, source_ref = source_ref, ), StatementConditional( condition = ExpressionComparisonIs( left = ExpressionTempVariableRef( variable = locals_keeper_variable.makeReference( provider ), source_ref = source_ref ), right = ExpressionConstantRef( constant = None, source_ref = source_ref ), source_ref = source_ref ), yes_branch = makeStatementsSequenceFromStatement( StatementAssignmentVariable( variable_ref = ExpressionTargetTempVariableRef( variable = locals_keeper_variable.makeReference( provider ), source_ref = source_ref ), source = locals_default, source_ref = source_ref, ) ), no_branch = None, source_ref = source_ref ), StatementConditional( condition = ExpressionComparisonIs( left = ExpressionTempVariableRef( variable = globals_keeper_variable.makeReference( provider ), source_ref = source_ref ), right = ExpressionConstantRef( constant = None, source_ref = source_ref ), source_ref = source_ref ), yes_branch = makeStatementsSequenceFromStatement( StatementAssignmentVariable( variable_ref = ExpressionTargetTempVariableRef( variable = globals_keeper_variable.makeReference( provider ), source_ref = source_ref ), source = ExpressionBuiltinGlobals( source_ref = source_ref ), source_ref = source_ref, ) ), no_branch = None, source_ref = source_ref ) ] return ( ExpressionTempVariableRef( variable = globals_keeper_variable.makeReference( provider ), source_ref = source_ref if globals_node is None else globals_node.getSourceReference() ), ExpressionTempVariableRef( variable = locals_keeper_variable.makeReference( provider ), source_ref = source_ref if locals_node is None else locals_node.getSourceReference() ), makeStatementsSequence(pre_statements, False, source_ref), makeStatementsSequence(post_statements, False, source_ref) )
def wrapEvalGlobalsAndLocals(provider, globals_node, locals_node, temp_scope, source_ref): """ Wrap the locals and globals arguments for eval and exec. For eval, this is called from the outside, and when the node tree already exists. """ globals_keeper_variable = provider.allocateTempVariable( temp_scope=temp_scope, name="globals") locals_keeper_variable = provider.allocateTempVariable( temp_scope=temp_scope, name="locals") if locals_node is None: locals_node = ExpressionConstantRef(constant=None, source_ref=source_ref) if globals_node is None: globals_node = ExpressionConstantRef(constant=None, source_ref=source_ref) post_statements = [ StatementDelVariable(variable_ref=ExpressionTargetTempVariableRef( variable=globals_keeper_variable.makeReference(provider), source_ref=globals_node.getSourceReference()), tolerant=False, source_ref=source_ref), StatementDelVariable(variable_ref=ExpressionTargetTempVariableRef( variable=locals_keeper_variable.makeReference(provider), source_ref=locals_node.getSourceReference()), tolerant=False, source_ref=source_ref) ] # The locals default is dependant on exec_mode, globals or locals. locals_default = ExpressionConditional( condition=ExpressionComparisonIs(left=ExpressionTempVariableRef( variable=globals_keeper_variable.makeReference(provider), source_ref=source_ref), right=ExpressionConstantRef( constant=None, source_ref=source_ref), source_ref=source_ref), no_expression=ExpressionTempVariableRef( variable=globals_keeper_variable.makeReference(provider), source_ref=source_ref), yes_expression=ExpressionBuiltinLocals(source_ref=source_ref), source_ref=source_ref) pre_statements = [ # First assign globals and locals temporary the values given. StatementAssignmentVariable( variable_ref=ExpressionTargetTempVariableRef( variable=globals_keeper_variable.makeReference(provider), source_ref=source_ref), source=globals_node, source_ref=source_ref, ), StatementAssignmentVariable( variable_ref=ExpressionTargetTempVariableRef( variable=locals_keeper_variable.makeReference(provider), source_ref=source_ref), source=locals_node, source_ref=source_ref, ), StatementConditional( condition=ExpressionComparisonIs(left=ExpressionTempVariableRef( variable=locals_keeper_variable.makeReference(provider), source_ref=source_ref), right=ExpressionConstantRef( constant=None, source_ref=source_ref), source_ref=source_ref), yes_branch=makeStatementsSequenceFromStatement( StatementAssignmentVariable( variable_ref=ExpressionTargetTempVariableRef( variable=locals_keeper_variable.makeReference( provider), source_ref=source_ref), source=locals_default, source_ref=source_ref, )), no_branch=None, source_ref=source_ref), StatementConditional( condition=ExpressionComparisonIs(left=ExpressionTempVariableRef( variable=globals_keeper_variable.makeReference(provider), source_ref=source_ref), right=ExpressionConstantRef( constant=None, source_ref=source_ref), source_ref=source_ref), yes_branch=makeStatementsSequenceFromStatement( StatementAssignmentVariable( variable_ref=ExpressionTargetTempVariableRef( variable=globals_keeper_variable.makeReference( provider), source_ref=source_ref), source=ExpressionBuiltinGlobals(source_ref=source_ref), source_ref=source_ref, )), no_branch=None, source_ref=source_ref) ] return (ExpressionTempVariableRef( variable=globals_keeper_variable.makeReference(provider), source_ref=source_ref if globals_node is None else globals_node.getSourceReference()), ExpressionTempVariableRef( variable=locals_keeper_variable.makeReference(provider), source_ref=source_ref if locals_node is None else locals_node.getSourceReference()), makeStatementsSequence(pre_statements, False, source_ref), makeStatementsSequence(post_statements, False, source_ref))
def wrapEvalGlobalsAndLocals(provider, globals_node, locals_node, temp_scope, source_ref): """ Wrap the locals and globals arguments for "eval". This is called from the outside, and when the node tree already exists. """ globals_keeper_variable = provider.allocateTempVariable( temp_scope=temp_scope, name="globals") locals_keeper_variable = provider.allocateTempVariable( temp_scope=temp_scope, name="locals") if locals_node is None: locals_node = ExpressionConstantRef(constant=None, source_ref=source_ref) if globals_node is None: globals_node = ExpressionConstantRef(constant=None, source_ref=source_ref) post_statements = [] if provider.isExpressionClassBody(): post_statements.append( StatementLocalsDictSync(locals_arg=ExpressionTempVariableRef( variable=locals_keeper_variable, source_ref=source_ref, ), source_ref=source_ref.atInternal())) post_statements += [ StatementReleaseVariable(variable=globals_keeper_variable, source_ref=source_ref), StatementReleaseVariable(variable=locals_keeper_variable, source_ref=source_ref) ] # The locals default is dependent on exec_mode, globals or locals. locals_default = ExpressionConditional( condition=ExpressionComparisonIs( left=ExpressionTempVariableRef(variable=globals_keeper_variable, source_ref=source_ref), right=ExpressionConstantRef(constant=None, source_ref=source_ref), source_ref=source_ref), expression_no=ExpressionTempVariableRef( variable=globals_keeper_variable, source_ref=source_ref), expression_yes=_getLocalsClassNode(provider)(source_ref=source_ref), source_ref=source_ref) pre_statements = [ # First assign globals and locals temporary the values given. StatementAssignmentVariable( variable_ref=ExpressionTargetTempVariableRef( variable=globals_keeper_variable, source_ref=source_ref), source=globals_node, source_ref=source_ref, ), StatementAssignmentVariable( variable_ref=ExpressionTargetTempVariableRef( variable=locals_keeper_variable, source_ref=source_ref), source=locals_node, source_ref=source_ref, ), StatementConditional( condition=ExpressionComparisonIs( left=ExpressionTempVariableRef(variable=locals_keeper_variable, source_ref=source_ref), right=ExpressionConstantRef(constant=None, source_ref=source_ref), source_ref=source_ref), yes_branch=makeStatementsSequenceFromStatement( StatementAssignmentVariable( variable_ref=ExpressionTargetTempVariableRef( variable=locals_keeper_variable, source_ref=source_ref), source=locals_default, source_ref=source_ref, )), no_branch=None, source_ref=source_ref), StatementConditional( condition=ExpressionComparisonIs(left=ExpressionTempVariableRef( variable=globals_keeper_variable, source_ref=source_ref), right=ExpressionConstantRef( constant=None, source_ref=source_ref), source_ref=source_ref), yes_branch=makeStatementsSequenceFromStatement( StatementAssignmentVariable( variable_ref=ExpressionTargetTempVariableRef( variable=globals_keeper_variable, source_ref=source_ref), source=ExpressionBuiltinGlobals(source_ref=source_ref), source_ref=source_ref, )), no_branch=None, source_ref=source_ref) ] return (ExpressionTempVariableRef( variable=globals_keeper_variable, source_ref=source_ref if globals_node is None else globals_node.getSourceReference()), ExpressionTempVariableRef( variable=locals_keeper_variable, source_ref=source_ref if locals_node is None else locals_node.getSourceReference()), makeStatementsSequence(pre_statements, False, source_ref), makeStatementsSequence(post_statements, False, source_ref))