def buildStatementsNode(provider, nodes, source_ref, frame = False): # We are not creating empty statement sequences. if nodes is None: return None # Build as list of statements, throw away empty ones, and remove useless # nesting. statements = buildNodeList(provider, nodes, source_ref, allow_none = True) statements = mergeStatements(statements) # We are not creating empty statement sequences. Might be empty, because # e.g. a global node generates not really a statement, or pass statements. if not statements: return None # In case of a frame is desired, build it instead. if frame: if provider.isExpressionFunctionBody(): parameters = provider.getParameters() arg_names = parameters.getCoArgNames() kw_only_count = parameters.getKwOnlyParameterCount() code_name = provider.getFunctionName() guard_mode = "generator" if provider.isGenerator() else "full" has_starlist = parameters.getStarListArgumentName() is not None has_stardict = parameters.getStarDictArgumentName() is not None if provider.isGenerator(): statements.insert( 0, StatementGeneratorEntry( source_ref = source_ref ) ) return StatementsFrame( statements = statements, guard_mode = guard_mode, var_names = arg_names, arg_count = len(arg_names), kw_only_count = kw_only_count, code_name = code_name, has_starlist = has_starlist, has_stardict = has_stardict, source_ref = source_ref ) else: return makeModuleFrame( module = provider, statements = statements, source_ref = source_ref ) else: return StatementsSequence( statements = statements, source_ref = source_ref )
def makeModuleFrame(module, statements, source_ref): assert module.isPythonModule() if module.isMainModule(): code_name = "<module>" else: code_name = module.getName() return StatementsFrame(statements=statements, guard_mode="once", var_names=(), arg_count=0, kw_only_count=0, code_name=code_name, has_starlist=False, has_stardict=False, source_ref=source_ref)
def buildStatementsNode(provider, nodes, source_ref, frame=False): # We are not creating empty statement sequences. if nodes is None: return None # Build as list of statements, throw away empty ones, and remove useless nesting. statements = buildNodeList(provider, nodes, source_ref, allow_none=True) statements = mergeStatements(statements) # We are not creating empty statement sequences. Might be empty, because e.g. a global # node generates not really a statement, or pass statements. if not statements: return None # In case of a frame is desired, build it instead. if frame: if provider.isExpressionFunctionBody(): arg_names = provider.getParameters().getCoArgNames() kw_only_count = provider.getParameters().getKwOnlyParameterCount() code_name = provider.getFunctionName() guard_mode = "generator" if provider.isGenerator() else "full" else: assert provider.isPythonModule() arg_names = () kw_only_count = 0 code_name = "<module>" if provider.isMainModule( ) else provider.getName() guard_mode = "once" return StatementsFrame(statements=statements, guard_mode=guard_mode, arg_names=arg_names, kw_only_count=kw_only_count, code_name=code_name, source_ref=source_ref) else: return StatementsSequence(statements=statements, source_ref=source_ref)
def _buildContractionNode(provider, node, name, emit_class, start_value, assign_provider, source_ref): # The contraction nodes are reformulated to function bodies, with loops as # described in the developer manual. They use a lot of temporary names, # nested blocks, etc. and so a lot of variable names. There is no good way # around that, and we deal with many cases, due to having generator # expressions sharing this code, pylint: disable=R0912,R0914 # Note: The assign_provider is only to cover Python2 list contractions, # assigning one of the loop variables to the outside scope. assert provider.isParentVariableProvider(), provider function_body = ExpressionFunctionBody(provider=provider, name=name, doc=None, parameters=ParameterSpec( name="contraction", normal_args=("__iterator", ), list_star_arg=None, dict_star_arg=None, default_count=0, kw_only_args=()), source_ref=source_ref) if start_value is not None: container_tmp = function_body.allocateTempVariable( temp_scope=None, name="contraction_result") else: container_tmp = None outer_iter_ref = ExpressionVariableRef(variable_name="__iterator", source_ref=source_ref) statements, del_statements = _buildContractionBodyNode( function_body=function_body, assign_provider=assign_provider, provider=provider, node=node, emit_class=emit_class, outer_iter_ref=outer_iter_ref, temp_scope=None, start_value=start_value, container_tmp=container_tmp, source_ref=source_ref, ) if start_value is not None: statements.append( StatementReturn(expression=ExpressionTempVariableRef( variable=container_tmp.makeReference(function_body), source_ref=source_ref), source_ref=source_ref)) statements = (makeTryFinallyStatement( tried=statements, final=del_statements, source_ref=source_ref.atInternal()), ) function_body.setBody( StatementsFrame(statements=statements, guard_mode="pass_through" if emit_class is not ExpressionYield else "generator", var_names=(), arg_count=0, kw_only_count=0, has_starlist=False, has_stardict=False, code_name="contraction", source_ref=source_ref)) return ExpressionFunctionCall(function=ExpressionFunctionCreation( function_ref=ExpressionFunctionRef(function_body=function_body, source_ref=source_ref), defaults=(), kw_defaults=None, annotations=None, source_ref=source_ref), values=(ExpressionBuiltinIter1( value=buildNode( provider=provider, node=node.generators[0].iter, source_ref=source_ref), source_ref=source_ref), ), source_ref=source_ref)
def buildLambdaNode(provider, node, source_ref): assert getKind(node) == "Lambda" parameters = buildParameterSpec(provider, "<lambda>", node, source_ref) function_body = ExpressionFunctionBody( provider=provider, name="<lambda>", doc=None, parameters=parameters, source_ref=source_ref, ) defaults = buildNodeList(provider, node.args.defaults, source_ref) kw_defaults = buildParameterKwDefaults(provider=provider, node=node, function_body=function_body, source_ref=source_ref) body = buildNode( provider=function_body, node=node.body, source_ref=source_ref, ) if function_body.isGenerator(): if Utils.python_version < 270: tmp_return_value = function_body.allocateTempVariable( temp_scope=None, name="yield_return") statements = (StatementAssignmentVariable( variable_ref=ExpressionTargetTempVariableRef( variable=tmp_return_value, source_ref=source_ref, ), source=body, source_ref=source_ref), StatementConditional( condition=ExpressionComparisonIsNOT( left=ExpressionTempVariableRef( variable=tmp_return_value, source_ref=source_ref, ), right=ExpressionConstantRef( constant=None, source_ref=source_ref), source_ref=source_ref), yes_branch=makeStatementsSequenceFromStatement( statement=StatementExpressionOnly( expression=ExpressionYield( expression=ExpressionTempVariableRef( variable=tmp_return_value, source_ref=source_ref, ), source_ref=source_ref), source_ref=source_ref)), no_branch=None, source_ref=source_ref)) body = makeTryFinallyStatement(tried=statements, final=StatementReleaseVariable( variable=tmp_return_value, tolerant=True, source_ref=source_ref), source_ref=source_ref) else: body = StatementExpressionOnly(expression=body, source_ref=source_ref) else: body = StatementReturn(expression=body, source_ref=source_ref) body = StatementsFrame( statements=mergeStatements((body, )), guard_mode="generator" if function_body.isGenerator() else "full", var_names=parameters.getCoArgNames(), arg_count=parameters.getArgumentCount(), kw_only_count=parameters.getKwOnlyParameterCount(), has_starlist=parameters.getStarListArgumentName() is not None, has_stardict=parameters.getStarDictArgumentName() is not None, code_name="<lambda>", source_ref=body.getSourceReference()) body = makeStatementsSequenceFromStatement(statement=body, ) function_body.setBody(body) annotations = buildParameterAnnotations(provider, node, source_ref) return ExpressionFunctionCreation(function_ref=ExpressionFunctionRef( function_body=function_body, source_ref=source_ref), defaults=defaults, kw_defaults=kw_defaults, annotations=annotations, source_ref=source_ref)
def buildLambdaNode(provider, node, source_ref): assert getKind(node) == "Lambda" function_body = ExpressionFunctionBody( provider=provider, name="<lambda>", doc=None, parameters=buildParameterSpec("<lambda>", node, source_ref), source_ref=source_ref, ) defaults = buildNodeList(provider, node.args.defaults, source_ref) kw_defaults = buildParameterKwDefaults(provider, node, function_body, source_ref) body = buildNode( provider=function_body, node=node.body, source_ref=source_ref, ) if function_body.isGenerator(): if Utils.python_version < 270: temp_block = StatementTempBlock(source_ref=source_ref, ) tmp_return_value = temp_block.getTempVariable("yield_return") statements = (StatementAssignmentVariable( variable_ref=ExpressionTargetTempVariableRef( variable=tmp_return_value.makeReference(temp_block), source_ref=source_ref, ), source=body, source_ref=source_ref), StatementConditional( condition=ExpressionComparisonIsNOT( left=ExpressionTempVariableRef( variable=tmp_return_value.makeReference( temp_block), source_ref=source_ref, ), right=ExpressionConstantRef( constant=None, source_ref=source_ref), source_ref=source_ref), yes_branch=makeStatementsSequenceFromStatement( statement=StatementExpressionOnly( expression=ExpressionYield( expression=ExpressionTempVariableRef( variable=tmp_return_value. makeReference(temp_block), source_ref=source_ref, ), source_ref=source_ref), source_ref=source_ref)), no_branch=None, source_ref=source_ref)) temp_block.setBody( StatementsSequence(statements=statements, source_ref=source_ref)) body = temp_block else: body = StatementExpressionOnly(expression=body, source_ref=source_ref) else: body = StatementReturn(expression=body, source_ref=source_ref) body = StatementsFrame( statements=(body, ), guard_mode="generator" if function_body.isGenerator() else "full", arg_names=function_body.getParameters().getCoArgNames(), kw_only_count=function_body.getParameters().getKwOnlyParameterCount(), code_name="<lambda>", source_ref=body.getSourceReference()) function_body.setBody(body) annotations = buildParameterAnnotations(provider, node, source_ref) return ExpressionFunctionCreation(function_ref=ExpressionFunctionRef( function_body=function_body, source_ref=source_ref), defaults=defaults, kw_defaults=kw_defaults, annotations=annotations, source_ref=source_ref)
def _buildContractionNode(provider, node, name, emit_class, start_value, assign_provider, source_ref): # The contraction nodes are reformulated to function bodies, with loops as # described in the developer manual. They use a lot of temporary names, # nested blocks, etc. and so a lot of variable names. There is no good way # around that, and we deal with many cases, due to having generator # expressions sharing this code, pylint: disable=R0912,R0914 # Note: The assign_provider is only to cover Python2 list contractions, # assigning one of the loop variables to the outside scope. assert provider.isParentVariableProvider(), provider function_body = ExpressionFunctionBody( provider=provider, name=name, doc=None, parameters=make_contraction_parameters, source_ref=source_ref) if start_value is not None: container_tmp = function_body.allocateTempVariable(None, "result") statements = [ StatementAssignmentVariable( variable_ref=ExpressionTargetTempVariableRef( variable=container_tmp.makeReference(function_body), source_ref=source_ref), source=start_value, source_ref=source_ref.atInternal()) ] else: statements = [] if hasattr(node, "elt"): if start_value is not None: current_body = emit_class(ExpressionTempVariableRef( variable=container_tmp.makeReference(function_body), source_ref=source_ref), buildNode(provider=function_body, node=node.elt, source_ref=source_ref), source_ref=source_ref) else: assert emit_class is ExpressionYield function_body.markAsGenerator() current_body = emit_class(buildNode(provider=function_body, node=node.elt, source_ref=source_ref), source_ref=source_ref) else: assert emit_class is ExpressionDictOperationSet current_body = emit_class(ExpressionTempVariableRef( variable=container_tmp.makeReference(function_body), source_ref=source_ref), key=buildNode( provider=function_body, node=node.key, source_ref=source_ref, ), value=buildNode( provider=function_body, node=node.value, source_ref=source_ref, ), source_ref=source_ref) current_body = StatementExpressionOnly(expression=current_body, source_ref=source_ref) for count, qual in enumerate(reversed(node.generators)): tmp_iter_variable = function_body.allocateTempVariable( temp_scope=None, name="contraction_iter_%d" % count) tmp_value_variable = function_body.allocateTempVariable( temp_scope=None, name="iter_value_%d" % count) # The first iterated value is to be calculated outside of the function # and will be given as a parameter "_iterated", the others are built # inside the function. if qual is node.generators[0]: value_iterator = ExpressionVariableRef(variable_name="__iterator", source_ref=source_ref) else: value_iterator = ExpressionBuiltinIter1(value=buildNode( provider=function_body, node=qual.iter, source_ref=source_ref), source_ref=source_ref) # First create the iterator and store it, next should be loop body nested_statements = [ StatementAssignmentVariable( variable_ref=ExpressionTargetTempVariableRef( variable=tmp_iter_variable.makeReference(function_body), source_ref=source_ref), source=value_iterator, source_ref=source_ref) ] loop_statements = [ makeTryExceptSingleHandlerNode( tried=makeStatementsSequenceFromStatement( statement=StatementAssignmentVariable( variable_ref=ExpressionTargetTempVariableRef( variable=tmp_value_variable.makeReference( function_body), source_ref=source_ref), source=ExpressionBuiltinNext1( value=ExpressionTempVariableRef( variable=tmp_iter_variable.makeReference( function_body), source_ref=source_ref), source_ref=source_ref), source_ref=source_ref)), exception_name="StopIteration", handler_body=makeStatementsSequenceFromStatement( statement=StatementBreakLoop( source_ref=source_ref.atInternal())), source_ref=source_ref), buildAssignmentStatements( provider=provider if assign_provider else function_body, temp_provider=function_body, node=qual.target, source=ExpressionTempVariableRef( variable=tmp_value_variable.makeReference(function_body), source_ref=source_ref), source_ref=source_ref) ] conditions = buildNodeList(provider=function_body, nodes=qual.ifs, source_ref=source_ref) if len(conditions) == 1: loop_statements.append( StatementConditional( condition=conditions[0], yes_branch=makeStatementsSequenceFromStatement( statement=current_body), no_branch=None, source_ref=source_ref)) elif len(conditions) > 1: loop_statements.append( StatementConditional( condition=buildAndNode(provider=function_body, values=conditions, source_ref=source_ref), yes_branch=makeStatementsSequenceFromStatement( statement=current_body), no_branch=None, source_ref=source_ref)) else: loop_statements.append(current_body) nested_statements.append( StatementLoop(body=StatementsSequence( statements=mergeStatements(loop_statements), source_ref=source_ref), source_ref=source_ref)) nested_statements.append( StatementDelVariable(variable_ref=ExpressionTargetTempVariableRef( variable=tmp_iter_variable.makeReference(function_body), source_ref=source_ref), tolerant=False, source_ref=source_ref)) current_body = StatementsSequence(statements=nested_statements, source_ref=source_ref) statements.append(current_body) if start_value is not None: statements.append( StatementReturn(expression=ExpressionTempVariableRef( variable=container_tmp.makeReference(function_body), source_ref=source_ref), source_ref=source_ref)) function_body.setBody( StatementsFrame(statements=mergeStatements(statements), guard_mode="pass_through" if emit_class is not ExpressionYield else "generator", var_names=(), arg_count=0, kw_only_count=0, has_starlist=False, has_stardict=False, code_name="contraction", source_ref=source_ref)) return ExpressionFunctionCall(function=ExpressionFunctionCreation( function_ref=ExpressionFunctionRef(function_body=function_body, source_ref=source_ref), defaults=(), kw_defaults=None, annotations=None, source_ref=source_ref), values=(ExpressionBuiltinIter1( value=buildNode( provider=provider, node=node.generators[0].iter, source_ref=source_ref), source_ref=source_ref), ), source_ref=source_ref)