def buildStatementsNode(provider, nodes, source_ref, code_object=None): # 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 code_object: if provider.isExpressionGeneratorObjectBody(): # TODO: Could do this earlier and on the outside. statements.insert(0, StatementGeneratorEntry(source_ref=source_ref)) result = StatementsFrame(statements=statements, guard_mode="generator", code_object=code_object, source_ref=source_ref) elif provider.isExpressionCoroutineObjectBody(): # TODO: That might be wrong result = StatementsFrame(statements=statements, guard_mode="generator", code_object=code_object, source_ref=source_ref) elif provider.isExpressionFunctionBody() or \ provider.isExpressionClassBody(): result = StatementsFrame(statements=statements, guard_mode="full", code_object=code_object, source_ref=source_ref) else: result = makeModuleFrame(module=provider, statements=statements, source_ref=source_ref) else: result = StatementsSequence(statements=statements, source_ref=source_ref) return result
def buildFrameNode(provider, nodes, code_object, source_ref): # 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 if provider.isExpressionGeneratorObjectBody(): result = StatementsFrame( statements = statements, guard_mode = "generator", code_object = code_object, source_ref = source_ref ) elif provider.isExpressionCoroutineObjectBody(): # TODO: That might be wrong result = StatementsFrame( statements = statements, guard_mode = "generator", code_object = code_object, source_ref = source_ref ) elif provider.isExpressionFunctionBody() or \ provider.isExpressionClassBody(): result = StatementsFrame( statements = statements, guard_mode = "full", code_object = code_object, source_ref = source_ref ) else: assert False return result
def _buildPython2ListContraction(provider, node, 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. # Note: The assign_provider is only to cover Python2 list contractions, # assigning one of the loop variables to the outside scope. function_body = ExpressionOutlineBody(provider=provider, name="list_contraction", source_ref=source_ref) iter_tmp = function_body.allocateTempVariable(temp_scope=None, name=".0") container_tmp = function_body.allocateTempVariable( temp_scope=None, name="contraction_result") statements, release_statements = _buildContractionBodyNode( function_body=function_body, assign_provider=True, provider=provider, node=node, emit_class=StatementListOperationAppend, iter_tmp=iter_tmp, temp_scope=None, start_value=makeConstantRefNode(constant=[], source_ref=source_ref), container_tmp=container_tmp, source_ref=source_ref, ) statements.append( StatementReturn(expression=ExpressionTempVariableRef( variable=container_tmp, source_ref=source_ref), source_ref=source_ref)) statements = (makeTryFinallyStatement( provider=function_body, tried=statements, final=release_statements, source_ref=source_ref.atInternal()), ) function_body.setBody( makeStatementsSequenceFromStatement(statement=StatementsFrame( statements=mergeStatements(statements, False), guard_mode="pass_through", code_object=None, source_ref=source_ref))) return function_body
def makeModuleFrame(module, statements, source_ref): assert module.isCompiledPythonModule() 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 makeModuleFrame(module, statements, source_ref): assert module.isCompiledPythonModule() if module.isMainModule(): code_name = "<module>" else: code_name = module.getName() return StatementsFrame(statements=statements, guard_mode="once", code_object=CodeObjectSpec( co_name=code_name, co_kind="Module", co_varnames=(), co_argcount=0, co_kwonlyargcount=0, co_has_starlist=False, co_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(): 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) elif provider.isExpressionCoroutineBody(): # TODO: That might be wrong parameters = provider.getParentVariableProvider().getParameters() arg_names = parameters.getCoArgNames() kw_only_count = parameters.getKwOnlyParameterCount() code_name = provider.getFunctionName() guard_mode = "generator", # TODO: Might be more special. has_starlist = parameters.getStarListArgumentName() is not None has_stardict = parameters.getStarDictArgumentName() is not None 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 buildLambdaNode(provider, node, source_ref): # Many details to deal with, pylint: disable=R0914 assert getKind(node) == "Lambda" function_kind, flags, _written_variables, _non_local_declarations, _global_declarations = \ detectFunctionBodyKind( nodes = (node.body,) ) outer_body, function_body, code_object = buildFunctionWithParsing( provider=provider, function_kind=function_kind, name="<lambda>", function_doc=None, flags=flags, node=node, source_ref=source_ref) if function_kind == "Function": code_body = function_body else: code_body = ExpressionGeneratorObjectBody(provider=function_body, name="<lambda>", flags=set(), source_ref=source_ref) if function_kind == "Generator": function_body.setBody( makeStatementsSequenceFromStatement(statement=StatementReturn( expression=ExpressionMakeGeneratorObject( generator_ref=ExpressionFunctionRef( function_body=code_body, source_ref=source_ref), code_object=code_object, source_ref=source_ref), 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=code_body, node=node.body, source_ref=source_ref, ) if function_kind == "Generator": if python_version < 270: tmp_return_value = code_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(provider=provider, tried=statements, final=StatementReleaseVariable( variable=tmp_return_value, 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, )), code_object=code_object, guard_mode="generator" if function_kind == "Generator" else "full", source_ref=body.getSourceReference()) body = makeStatementsSequenceFromStatement(statement=body, ) code_body.setBody(body) annotations = buildParameterAnnotations(provider, node, source_ref) return ExpressionFunctionCreation(function_ref=ExpressionFunctionRef( function_body=outer_body, source_ref=source_ref), code_object=code_object, defaults=defaults, kw_defaults=kw_defaults, annotations=annotations, source_ref=source_ref)
def _buildContractionNode(provider, node, name, emit_class, start_value, 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. # TODO: No function ought to be necessary. function_body = ExpressionFunctionBody(provider=provider, name=name, doc=None, parameters=ParameterSpec( ps_name=name, ps_normal_args=(".0", ), ps_list_star_arg=None, ps_dict_star_arg=None, ps_default_count=0, ps_kw_only_args=()), flags=set(), source_ref=source_ref) iter_tmp = function_body.getVariableForAssignment(variable_name=".0") assert iter_tmp.isParameterVariable() code_object = CodeObjectSpec( co_name=name, co_kind="Generator" if emit_class is ExpressionYield else "Function", co_varnames=function_body.getParameters().getParameterNames(), co_argcount=len(function_body.getParameters().getParameterNames()), co_kwonlyargcount=0, co_has_starlist=False, co_has_stardict=False, ) if emit_class is ExpressionYield: code_body = ExpressionGeneratorObjectBody(provider=function_body, name="<genexpr>", flags=set(), source_ref=source_ref) iter_tmp = code_body.getVariableForReference(variable_name=".0") assert iter_tmp.isLocalVariable() function_body.setBody( makeStatementsSequenceFromStatement(statement=StatementReturn( expression=ExpressionMakeGeneratorObject( generator_ref=ExpressionFunctionRef( function_body=code_body, source_ref=source_ref), code_object=code_object, source_ref=source_ref), source_ref=source_ref))) else: code_body = function_body if start_value is not None: container_tmp = code_body.allocateTempVariable( temp_scope=None, name="contraction_result") else: container_tmp = None statements, release_statements = _buildContractionBodyNode( function_body=code_body, provider=provider, node=node, emit_class=emit_class, iter_tmp=iter_tmp, temp_scope=None, start_value=start_value, container_tmp=container_tmp, assign_provider=False, source_ref=source_ref, ) if start_value is not None: statements.append( StatementReturn(expression=ExpressionTempVariableRef( variable=container_tmp, source_ref=source_ref), source_ref=source_ref)) statements = (makeTryFinallyStatement( provider=function_body, tried=statements, final=release_statements, source_ref=source_ref.atInternal()), ) if emit_class is ExpressionYield: guard_mode = "generator" else: guard_mode = "full" code_body.setBody( makeStatementsSequenceFromStatement(statement=StatementsFrame( statements=mergeStatements(statements, False), guard_mode=guard_mode, code_object=code_object, source_ref=source_ref))) return ExpressionFunctionCall(function=ExpressionFunctionCreation( function_ref=ExpressionFunctionRef(function_body=function_body, source_ref=source_ref), code_object=code_object, 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 _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. # Note: The assign_provider is only to cover Python2 list contractions, # assigning one of the loop variables to the outside scope. if assign_provider: function_body = ExpressionOutlineBody( provider=provider, name=name, body=None, # later source_ref=source_ref) iter_tmp = function_body.allocateTempVariable(temp_scope=None, name=".0") else: function_body = ExpressionFunctionBody(provider=provider, name=name, doc=None, parameters=ParameterSpec( name="contraction", normal_args=(".0", ), list_star_arg=None, dict_star_arg=None, default_count=0, kw_only_args=()), is_class=False, source_ref=source_ref) iter_tmp = function_body.getVariableForAssignment(variable_name=".0") assert iter_tmp.isParameterVariable() if start_value is not None: container_tmp = function_body.allocateTempVariable( temp_scope=None, name="contraction_result") else: container_tmp = None statements, release_statements = _buildContractionBodyNode( function_body=function_body, assign_provider=assign_provider, provider=provider, node=node, emit_class=emit_class, iter_tmp=iter_tmp, 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, source_ref=source_ref), source_ref=source_ref)) statements = (makeTryFinallyStatement( provider=function_body, tried=statements, final=release_statements, source_ref=source_ref.atInternal()), ) function_body.setBody( makeStatementsSequenceFromStatement(statement=StatementsFrame( statements=mergeStatements(statements, False), 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))) if not assign_provider: 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) else: return function_body
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, is_class=False, 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(provider=provider, tried=statements, final=StatementReleaseVariable( variable=tmp_return_value, 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)