Ejemplo n.º 1
0
        def wrapExpressionBuiltinExecfileCreation(filename, globals_arg,
                                                  locals_arg, source_ref):
            outline_body = ExpressionOutlineBody(
                provider   = node.getParentVariableProvider(),
                name       = "execfile_call",
                source_ref = source_ref
            )

            globals_ref, locals_ref, tried, final = wrapEvalGlobalsAndLocals(
                provider     = node.getParentVariableProvider(),
                globals_node = globals_arg,
                locals_node  = locals_arg,
                temp_scope   = outline_body.getOutlineTempScope(),
                source_ref   = source_ref
            )

            tried = makeStatementsSequence(
                statements = (
                    tried,
                    StatementReturn(
                        expression = ExpressionBuiltinExecfile(
                            source_code = ExpressionCallEmpty(
                                called     = ExpressionAttributeLookup(
                                    source         = ExpressionBuiltinOpen(
                                        filename   = filename,
                                        mode       = ExpressionConstantRef(
                                            constant   = "rU",
                                            source_ref = source_ref
                                        ),
                                        buffering  = None,
                                        source_ref = source_ref
                                    ),
                                    attribute_name = "read",
                                    source_ref     = source_ref
                                ),
                                source_ref = source_ref
                            ),
                            globals_arg = globals_ref,
                            locals_arg  = locals_ref,
                            source_ref  = source_ref
                        ),
                        source_ref = source_ref
                    )
                ),
                allow_none = False,
                source_ref = source_ref
            )

            outline_body.setBody(
                makeStatementsSequenceFromStatement(
                    statement = makeTryFinallyStatement(
                        provider   = outline_body,
                        tried      = tried,
                        final      = final,
                        source_ref = source_ref
                    )
                )
            )

            return outline_body
Ejemplo n.º 2
0
        def wrapExpressionBuiltinExecCreation(source, globals_arg, locals_arg,
                                              source_ref):
            provider = node.getParentVariableProvider()

            outline_body = ExpressionOutlineBody(
                provider   = provider,
                name       = "exec_call",
                body       = None, # later
                source_ref = source_ref
            )

            # TODO: Can't really be true, can it?
            if provider.isExpressionFunctionBody():
                provider.markAsExecContaining()

                if provider.isExpressionClassBody():
                    provider.markAsUnqualifiedExecContaining(source_ref)

            globals_ref, locals_ref, tried, final = wrapEvalGlobalsAndLocals(
                provider     = provider,
                globals_node = globals_arg,
                locals_node  = locals_arg,
                temp_scope   = outline_body.getOutlineTempScope(),
                source_ref   = source_ref
            )

            tried = makeStatementsSequence(
                statements = (
                    tried,
                    StatementReturn(
                        expression = ExpressionBuiltinExec(
                            source_code = source,
                            globals_arg = globals_ref,
                            locals_arg  = locals_ref,
                            source_ref  = source_ref
                        ),
                        source_ref = source_ref
                    ),
                ),
                allow_none = False,
                source_ref = source_ref
            )

            # Hack: Allow some APIs to work already
            tried.parent = outline_body

            outline_body.setBody(
                makeStatementsSequenceFromStatement(
                    statement = makeTryFinallyStatement(
                        provider   = provider,
                        tried      = tried,
                        final      = final,
                        source_ref = source_ref
                    )
                )
            )

            return outline_body
Ejemplo n.º 3
0
def convertFunctionCallToOutline(provider, function_ref, values):
    # This has got to have pretty man details, pylint: disable=R0914

    function_body = function_ref.getFunctionBody()

    # TODO: Use the call location
    source_ref = function_body.getSourceReference()

    outline_body = ExpressionOutlineBody(provider=provider,
                                         name="inline",
                                         body=None,
                                         source_ref=source_ref)

    clone = function_body.getBody().makeClone()

    temp_scope = outline_body.getOutlineTempScope()

    translation = {}

    for variable in function_body.getLocalVariables():
        # TODO: Later we should be able to do that too.
        assert not variable.isSharedTechnically()

        new_variable = outline_body.allocateTempVariable(
            temp_scope=temp_scope, name=variable.getName())

        # TODO: Lets update all at once maybe, it would take less visits.
        updateVariableUsage(clone,
                            old_variable=variable,
                            new_variable=new_variable)

        translation[variable.getName()] = new_variable

    statements = []

    argument_names = function_body.getParameters().getAllNames()

    assert len(argument_names) == len(values), (argument_names, values)

    for argument_name, value in zip(argument_names, values):
        statements.append(
            StatementAssignmentVariable(
                variable_ref=makeVariableTargetRefNode(
                    variable=translation[argument_name],
                    source_ref=source_ref),
                source=value,
                source_ref=source_ref,
            ))

    body = makeStatementsSequence(statements=(statements, clone),
                                  allow_none=False,
                                  source_ref=source_ref)
    outline_body.setBody(body)

    return outline_body
Ejemplo n.º 4
0
        def wrapExpressionBuiltinExecCreation(source, globals_arg, locals_arg,
                                              source_ref):
            provider = node.getParentVariableProvider()

            outline_body = ExpressionOutlineBody(
                provider   = provider,
                name       = "exec_call",
                source_ref = source_ref
            )

            globals_ref, locals_ref, tried, final = wrapEvalGlobalsAndLocals(
                provider     = provider,
                globals_node = globals_arg,
                locals_node  = locals_arg,
                temp_scope   = outline_body.getOutlineTempScope(),
                source_ref   = source_ref
            )

            tried = makeStatementsSequence(
                statements = (
                    tried,
                    StatementReturn(
                        expression = ExpressionBuiltinExec(
                            source_code = source,
                            globals_arg = globals_ref,
                            locals_arg  = locals_ref,
                            source_ref  = source_ref
                        ),
                        source_ref = source_ref
                    ),
                ),
                allow_none = False,
                source_ref = source_ref
            )

            # Hack: Allow some APIs to work already
            tried.parent = outline_body

            outline_body.setBody(
                makeStatementsSequenceFromStatement(
                    statement = makeTryFinallyStatement(
                        provider   = provider,
                        tried      = tried,
                        final      = final,
                        source_ref = source_ref
                    )
                )
            )

            return outline_body
Ejemplo n.º 5
0
        def wrapExpressionBuiltinExecCreation(source, globals_arg, locals_arg,
                                              source_ref):
            provider = node.getParentVariableProvider()

            outline_body = ExpressionOutlineBody(
                provider=provider,
                name="exec_call",
                body=None,  # later
                source_ref=source_ref)

            # TODO: Can't really be true, can it?
            if provider.isExpressionFunctionBody():
                provider.markAsExecContaining()

                if provider.isExpressionClassBody():
                    provider.markAsUnqualifiedExecContaining(source_ref)

            globals_ref, locals_ref, tried, final = wrapEvalGlobalsAndLocals(
                provider=provider,
                globals_node=globals_arg,
                locals_node=locals_arg,
                temp_scope=outline_body.getOutlineTempScope(),
                source_ref=source_ref)

            tried = makeStatementsSequence(statements=(
                tried,
                StatementReturn(expression=ExpressionBuiltinExec(
                    source_code=source,
                    globals_arg=globals_ref,
                    locals_arg=locals_ref,
                    source_ref=source_ref),
                                source_ref=source_ref),
            ),
                                           allow_none=False,
                                           source_ref=source_ref)

            # Hack: Allow some APIs to work already
            tried.parent = outline_body

            outline_body.setBody(
                makeStatementsSequenceFromStatement(
                    statement=makeTryFinallyStatement(provider=provider,
                                                      tried=tried,
                                                      final=final,
                                                      source_ref=source_ref)))

            return outline_body
Ejemplo n.º 6
0
    def wrapEvalBuiltin(source, globals_arg, locals_arg, source_ref):
        provider = node.getParentVariableProvider()

        outline_body = ExpressionOutlineBody(
            provider=node.getParentVariableProvider(), name="eval_call", body=None, source_ref=source_ref  # later
        )

        globals_ref, locals_ref, tried, final = wrapEvalGlobalsAndLocals(
            provider=provider,
            globals_node=globals_arg,
            locals_node=locals_arg,
            temp_scope=outline_body.getOutlineTempScope(),
            source_ref=source_ref,
        )

        # The wrapping should not relocate to the "source_ref".
        assert globals_arg is None or globals_ref.getSourceReference() == globals_arg.getSourceReference()
        assert locals_arg is None or locals_ref.getSourceReference() == locals_arg.getSourceReference()

        source_variable = outline_body.allocateTempVariable(temp_scope=None, name="source")

        final.setStatements(
            final.getStatements()
            + (
                StatementDelVariable(
                    variable_ref=ExpressionTargetTempVariableRef(variable=source_variable, source_ref=source_ref),
                    tolerant=True,
                    source_ref=source_ref,
                ),
            )
        )

        strip_choice = ExpressionConstantRef(constant=(" \t",), source_ref=source_ref)

        if python_version >= 300:
            strip_choice = ExpressionConditional(
                condition=ExpressionComparisonIs(
                    left=ExpressionBuiltinType1(
                        value=ExpressionTempVariableRef(variable=source_variable, source_ref=source_ref),
                        source_ref=source_ref,
                    ),
                    right=ExpressionBuiltinRef(builtin_name="bytes", source_ref=source_ref),
                    source_ref=source_ref,
                ),
                expression_yes=ExpressionConstantRef(constant=(b" \t",), source_ref=source_ref),
                expression_no=strip_choice,
                source_ref=source_ref,
            )

        # Source needs some special treatment for eval, if it's a string, it
        # must be stripped.
        string_fixup = [
            StatementAssignmentVariable(
                variable_ref=ExpressionTargetTempVariableRef(variable=source_variable, source_ref=source_ref),
                source=ExpressionCallNoKeywords(
                    called=ExpressionAttributeLookup(
                        source=ExpressionTempVariableRef(variable=source_variable, source_ref=source_ref),
                        attribute_name="strip",
                        source_ref=source_ref,
                    ),
                    args=strip_choice,
                    source_ref=source_ref,
                ),
                source_ref=source_ref,
            )
        ]

        statements = (
            StatementAssignmentVariable(
                variable_ref=ExpressionTargetTempVariableRef(variable=source_variable, source_ref=source_ref),
                source=source,
                source_ref=source_ref,
            ),
            StatementConditional(
                condition=ExpressionOperationNOT(
                    operand=ExpressionBuiltinIsinstance(
                        instance=ExpressionTempVariableRef(variable=source_variable, source_ref=source_ref),
                        classes=ExpressionBuiltinAnonymousRef(builtin_name="code", source_ref=source_ref),
                        source_ref=source_ref,
                    ),
                    source_ref=source_ref,
                ),
                yes_branch=StatementsSequence(statements=string_fixup, source_ref=source_ref),
                no_branch=None,
                source_ref=source_ref,
            ),
            StatementReturn(
                expression=ExpressionBuiltinEval(
                    source_code=ExpressionTempVariableRef(variable=source_variable, source_ref=source_ref),
                    globals_arg=globals_ref,
                    locals_arg=locals_ref,
                    source_ref=source_ref,
                ),
                source_ref=source_ref,
            ),
        )

        tried = makeStatementsSequence(statements=(tried,) + statements, allow_none=False, source_ref=source_ref)

        outline_body.setBody(
            makeStatementsSequenceFromStatement(
                statement=makeTryFinallyStatement(
                    provider=outline_body, tried=tried, final=final, source_ref=source_ref
                )
            )
        )

        return outline_body
Ejemplo n.º 7
0
    def wrapEvalBuiltin(source, globals_arg, locals_arg, source_ref):
        provider = node.getParentVariableProvider()

        outline_body = ExpressionOutlineBody(
            provider   = node.getParentVariableProvider(),
            name       = "eval_call",
            source_ref = source_ref
        )

        globals_ref, locals_ref, tried, final = wrapEvalGlobalsAndLocals(
            provider     = provider,
            globals_node = globals_arg,
            locals_node  = locals_arg,
            temp_scope   = outline_body.getOutlineTempScope(),
            source_ref   = source_ref
        )

        # The wrapping should not relocate to the "source_ref".
        assert globals_arg is None or \
               globals_ref.getSourceReference() == \
               globals_arg.getSourceReference()
        assert locals_arg is None or \
               locals_ref.getSourceReference() == \
               locals_arg.getSourceReference()

        source_variable = outline_body.allocateTempVariable(
            temp_scope = None,
            name       = "source"
        )

        final.setStatements(
            final.getStatements() + (
                StatementDelVariable(
                    variable_ref = ExpressionTargetTempVariableRef(
                        variable   = source_variable,
                        source_ref = source_ref
                    ),
                    tolerant     = True,
                    source_ref   = source_ref
                ),
            )
        )

        strip_choice =  ExpressionConstantRef(
            constant   = (" \t",),
            source_ref = source_ref
        )

        if python_version >= 300:
            strip_choice = ExpressionConditional(
                condition      = ExpressionComparisonIs(
                    left       = ExpressionBuiltinType1(
                        value      = ExpressionTempVariableRef(
                            variable   = source_variable,
                            source_ref = source_ref
                        ),
                        source_ref = source_ref
                    ),
                    right      = ExpressionBuiltinRef(
                        builtin_name = "bytes",
                        source_ref   = source_ref
                    ),
                    source_ref = source_ref
                ),
                expression_yes = ExpressionConstantRef(
                    constant   = (b" \t",),
                    source_ref = source_ref
                ),
                expression_no  = strip_choice,
                source_ref     = source_ref
            )


        # Source needs some special treatment for eval, if it's a string, it
        # must be stripped.
        string_fixup = [
            StatementAssignmentVariable(
                variable_ref = ExpressionTargetTempVariableRef(
                    variable   = source_variable,
                    source_ref = source_ref
                ),
                source       = ExpressionCallNoKeywords(
                    called     = ExpressionAttributeLookup(
                        source         = ExpressionTempVariableRef(
                            variable   = source_variable,
                            source_ref = source_ref
                        ),
                        attribute_name = "strip",
                        source_ref     = source_ref
                    ),
                    args       = strip_choice,
                    source_ref = source_ref
                ),
                source_ref   = source_ref
            )
        ]

        statements = (
            StatementAssignmentVariable(
                variable_ref = ExpressionTargetTempVariableRef(
                    variable   = source_variable,
                    source_ref = source_ref
                ),
                source       = source,
                source_ref   = source_ref,
            ),
            StatementConditional(
                condition  = ExpressionOperationNOT(
                    operand    = ExpressionBuiltinIsinstance(
                        instance   = ExpressionTempVariableRef(
                            variable   = source_variable,
                            source_ref = source_ref
                        ),
                        classes    = ExpressionBuiltinAnonymousRef(
                            builtin_name = "code",
                            source_ref   = source_ref,
                        ),
                        source_ref = source_ref
                    ),
                    source_ref = source_ref
                ),
                yes_branch = StatementsSequence(
                    statements = string_fixup,
                    source_ref = source_ref
                ),
                no_branch  = None,
                source_ref = source_ref
            ),
            StatementReturn(
                expression = ExpressionBuiltinEval(
                    source_code = ExpressionTempVariableRef(
                        variable   = source_variable,
                        source_ref = source_ref
                    ),
                    globals_arg = globals_ref,
                    locals_arg  = locals_ref,
                    source_ref  = source_ref
                ),
                source_ref = source_ref
            )
        )

        tried = makeStatementsSequence(
            statements = (
                tried,
            ) + statements,
            allow_none = False,
            source_ref = source_ref
        )

        outline_body.setBody(
            makeStatementsSequenceFromStatement(
                statement = makeTryFinallyStatement(
                    provider   = outline_body,
                    tried      = tried,
                    final      = final,
                    source_ref = source_ref
                )
            )
        )

        return outline_body
Ejemplo n.º 8
0
def convertFunctionCallToOutline(provider, function_ref, values):
    # This has got to have pretty man details, pylint: disable=R0914

    function_body = function_ref.getFunctionBody()

    # TODO: Use the call location
    source_ref = function_body.getSourceReference()

    outline_body = ExpressionOutlineBody(
        provider   = provider,
        name       = "inline",
        body       = None,
        source_ref = source_ref

    )

    clone = function_body.getBody().makeClone()

    temp_scope = outline_body.getOutlineTempScope()

    translation = {}

    for variable in function_body.getLocalVariables():
        # TODO: Later we should be able to do that too.
        assert not variable.isSharedTechnically()

        new_variable = outline_body.allocateTempVariable(
            temp_scope = temp_scope,
            name       = variable.getName()
        )

        # TODO: Lets update all at once maybe, it would take less visits.
        updateVariableUsage(
            clone,
            old_variable = variable,
            new_variable = new_variable
        )

        translation[variable.getName()] = new_variable

    statements = []

    argument_names = function_body.getParameters().getAllNames()

    assert len(argument_names) == len(values), (argument_names, values)

    for argument_name, value in zip(argument_names, values):
        statements.append(
            StatementAssignmentVariable(
                variable_ref = ExpressionTargetTempVariableRef(
                    variable   = translation[argument_name],
                    source_ref = source_ref
                ),
                source       = value,
                source_ref   = source_ref,
            )
        )

    body = makeStatementsSequence(
        statements = (statements, clone),
        allow_none = False,
        source_ref = source_ref
    )
    outline_body.setBody(body)

    return outline_body