예제 #1
0
def buildExecNode(provider, node, source_ref):
    # "exec" statements, should only occur with Python2.

    exec_globals = node.globals
    exec_locals = node.locals
    body = node.body

    orig_globals = exec_globals

    # Handle exec(a,b,c) to be same as exec a, b, c
    if exec_locals is None and exec_globals is None and getKind(
            body) == "Tuple":
        parts = body.elts
        body = parts[0]

        if len(parts) > 1:
            exec_globals = parts[1]

            if len(parts) > 2:
                exec_locals = parts[2]
        else:
            return StatementRaiseException(
                exception_type=ExpressionBuiltinExceptionRef(
                    exception_name="TypeError", source_ref=source_ref),
                exception_value=ExpressionConstantRef(
                    constant=
                    "exec: arg 1 must be a string, file, or code object",
                    source_ref=source_ref),
                exception_trace=None,
                exception_cause=None,
                source_ref=source_ref)

    globals_node = buildNode(provider, exec_globals, source_ref, True)
    locals_node = buildNode(provider, exec_locals, source_ref, True)

    if provider.isExpressionFunctionBody():
        provider.markAsExecContaining()

        if orig_globals is None:
            provider.markAsUnqualifiedExecContaining(source_ref)

    if locals_node is not None and locals_node.isExpressionConstantRef(
    ) and locals_node.getConstant() is None:
        locals_node = None

    if locals_node is None and globals_node is not None:
        if globals_node.isExpressionConstantRef(
        ) and globals_node.getConstant() is None:
            globals_node = None

    return StatementExec(source_code=buildNode(provider, body, source_ref),
                         globals_arg=globals_node,
                         locals_arg=locals_node,
                         source_ref=source_ref)
예제 #2
0
def buildExecNode(provider, node, source_ref):
    # "exec" statements, should only occur with Python2.

    # This is using many variables, due to the many details this is
    # dealing with. The locals and globals need to be dealt with in
    # temporary variables, and we need handling of indicators, so
    # that is just the complexity, pylint: disable=too-many-locals

    exec_globals = node.globals
    exec_locals = node.locals
    body = node.body

    # Handle exec(a,b,c) to be same as exec a, b, c
    if exec_locals is None and exec_globals is None and getKind(
            body) == "Tuple":
        parts = body.elts
        body = parts[0]

        if len(parts) > 1:
            exec_globals = parts[1]

            if len(parts) > 2:
                exec_locals = parts[2]
        else:
            return StatementRaiseException(
                exception_type=ExpressionBuiltinExceptionRef(
                    exception_name="TypeError", source_ref=source_ref),
                exception_value=makeConstantRefNode(
                    constant="""\
exec: arg 1 must be a string, file, or code object""",
                    source_ref=source_ref,
                ),
                exception_trace=None,
                exception_cause=None,
                source_ref=source_ref,
            )

    temp_scope = provider.allocateTempScope("exec")

    locals_value = buildNode(provider, exec_locals, source_ref, True)

    if locals_value is None:
        locals_value = ExpressionConstantNoneRef(source_ref=source_ref)

    globals_value = buildNode(provider, exec_globals, source_ref, True)

    if globals_value is None:
        globals_value = ExpressionConstantNoneRef(source_ref=source_ref)

    source_code = buildNode(provider, body, source_ref)

    source_variable = provider.allocateTempVariable(temp_scope=temp_scope,
                                                    name="exec_source")

    globals_keeper_variable = provider.allocateTempVariable(
        temp_scope=temp_scope, name="globals")

    locals_keeper_variable = provider.allocateTempVariable(
        temp_scope=temp_scope, name="locals")

    plain_indicator_variable = provider.allocateTempVariable(
        temp_scope=temp_scope, name="plain")

    tried = (
        # First evaluate the source code expressions.
        StatementAssignmentVariable(variable=source_variable,
                                    source=source_code,
                                    source_ref=source_ref),
        # Assign globals and locals temporary the values given, then fix it
        # up, taking note in the "plain" temporary variable, if it was an
        # "exec" statement with None arguments, in which case the copy back
        # will be necessary.
        StatementAssignmentVariable(
            variable=globals_keeper_variable,
            source=globals_value,
            source_ref=source_ref,
        ),
        StatementAssignmentVariable(variable=locals_keeper_variable,
                                    source=locals_value,
                                    source_ref=source_ref),
        StatementAssignmentVariable(
            variable=plain_indicator_variable,
            source=makeConstantRefNode(constant=False, source_ref=source_ref),
            source_ref=source_ref,
        ),
        makeStatementConditional(
            condition=ExpressionComparisonIs(
                left=ExpressionTempVariableRef(
                    variable=globals_keeper_variable, source_ref=source_ref),
                right=ExpressionConstantNoneRef(source_ref=source_ref),
                source_ref=source_ref,
            ),
            yes_branch=makeStatementsSequenceFromStatements(
                StatementAssignmentVariable(
                    variable=globals_keeper_variable,
                    source=ExpressionBuiltinGlobals(source_ref=source_ref),
                    source_ref=source_ref,
                ),
                makeStatementConditional(
                    condition=ExpressionComparisonIs(
                        left=ExpressionTempVariableRef(
                            variable=locals_keeper_variable,
                            source_ref=source_ref),
                        right=ExpressionConstantNoneRef(source_ref=source_ref),
                        source_ref=source_ref,
                    ),
                    yes_branch=makeStatementsSequenceFromStatements(
                        StatementAssignmentVariable(
                            variable=locals_keeper_variable,
                            source=makeExpressionBuiltinLocals(
                                provider=provider, source_ref=source_ref),
                            source_ref=source_ref,
                        ),
                        StatementAssignmentVariable(
                            variable=plain_indicator_variable,
                            source=makeConstantRefNode(constant=True,
                                                       source_ref=source_ref),
                            source_ref=source_ref,
                        ),
                    ),
                    no_branch=None,
                    source_ref=source_ref,
                ),
            ),
            no_branch=makeStatementsSequenceFromStatements(
                makeStatementConditional(
                    condition=ExpressionComparisonIs(
                        left=ExpressionTempVariableRef(
                            variable=locals_keeper_variable,
                            source_ref=source_ref),
                        right=ExpressionConstantNoneRef(source_ref=source_ref),
                        source_ref=source_ref,
                    ),
                    yes_branch=makeStatementsSequenceFromStatement(
                        statement=StatementAssignmentVariable(
                            variable=locals_keeper_variable,
                            source=ExpressionTempVariableRef(
                                variable=globals_keeper_variable,
                                source_ref=source_ref),
                            source_ref=source_ref,
                        )),
                    no_branch=None,
                    source_ref=source_ref,
                )),
            source_ref=source_ref,
        ),
        makeTryFinallyStatement(
            provider=provider,
            tried=StatementExec(
                source_code=ExpressionTempVariableRef(variable=source_variable,
                                                      source_ref=source_ref),
                globals_arg=ExpressionTempVariableRef(
                    variable=globals_keeper_variable, source_ref=source_ref),
                locals_arg=ExpressionTempVariableRef(
                    variable=locals_keeper_variable, source_ref=source_ref),
                source_ref=source_ref,
            ),
            final=makeStatementConditional(
                condition=ExpressionComparisonIs(
                    left=ExpressionTempVariableRef(
                        variable=plain_indicator_variable,
                        source_ref=source_ref),
                    right=makeConstantRefNode(constant=True,
                                              source_ref=source_ref),
                    source_ref=source_ref,
                ),
                yes_branch=StatementLocalsDictSync(
                    locals_arg=ExpressionTempVariableRef(
                        variable=locals_keeper_variable,
                        source_ref=source_ref),
                    source_ref=source_ref,
                ),
                no_branch=None,
                source_ref=source_ref,
            ),
            source_ref=source_ref,
        ),
    )

    final = (
        StatementReleaseVariable(variable=source_variable,
                                 source_ref=source_ref),
        StatementReleaseVariable(variable=globals_keeper_variable,
                                 source_ref=source_ref),
        StatementReleaseVariable(variable=locals_keeper_variable,
                                 source_ref=source_ref),
        StatementReleaseVariable(variable=plain_indicator_variable,
                                 source_ref=source_ref),
    )

    return makeTryFinallyStatement(provider=provider,
                                   tried=tried,
                                   final=final,
                                   source_ref=source_ref)
예제 #3
0
def buildExecNode(provider, node, source_ref):
    # "exec" statements, should only occur with Python2.

    exec_globals = node.globals
    exec_locals = node.locals
    body = node.body

    orig_globals = exec_globals

    # Handle exec(a,b,c) to be same as exec a, b, c
    if exec_locals is None and exec_globals is None and \
       getKind(body) == "Tuple":
        parts = body.elts
        body = parts[0]

        if len(parts) > 1:
            exec_globals = parts[1]

            if len(parts) > 2:
                exec_locals = parts[2]
        else:
            return StatementRaiseException(
                exception_type=ExpressionBuiltinExceptionRef(
                    exception_name="TypeError", source_ref=source_ref),
                exception_value=ExpressionConstantRef(constant="""\
exec: arg 1 must be a string, file, or code object""",
                                                      source_ref=source_ref),
                exception_trace=None,
                exception_cause=None,
                source_ref=source_ref)

    if provider.isExpressionFunctionBody():
        provider.markAsExecContaining()

        if orig_globals is None:
            provider.markAsUnqualifiedExecContaining(source_ref)

    temp_scope = provider.allocateTempScope("exec")

    globals_ref, locals_ref, tried, final = wrapEvalGlobalsAndLocals(
        provider=provider,
        globals_node=buildNode(provider, exec_globals, source_ref, True),
        locals_node=buildNode(provider, exec_locals, source_ref, True),
        temp_scope=temp_scope,
        source_ref=source_ref)

    source_code = buildNode(provider, body, source_ref)

    source_variable = provider.allocateTempVariable(temp_scope=temp_scope,
                                                    name="source")

    # Source needs some special treatment for eval, if it's a string, it
    # must be stripped.
    file_fixup = [
        StatementAssignmentVariable(
            variable_ref=ExpressionTargetTempVariableRef(
                variable=source_variable.makeReference(provider),
                source_ref=source_ref),
            source=ExpressionCallEmpty(called=ExpressionAttributeLookup(
                expression=ExpressionTempVariableRef(
                    variable=source_variable.makeReference(provider),
                    source_ref=source_ref),
                attribute_name="read",
                source_ref=source_ref),
                                       source_ref=source_ref),
            source_ref=source_ref)
    ]

    statements = (StatementAssignmentVariable(
        variable_ref=ExpressionTargetTempVariableRef(
            variable=source_variable.makeReference(provider),
            source_ref=source_ref),
        source=source_code,
        source_ref=source_ref,
    ),
                  StatementConditional(condition=ExpressionBuiltinIsinstance(
                      cls=ExpressionBuiltinAnonymousRef(
                          builtin_name="file",
                          source_ref=source_ref,
                      ),
                      instance=ExpressionTempVariableRef(
                          variable=source_variable.makeReference(provider),
                          source_ref=source_ref),
                      source_ref=source_ref),
                                       yes_branch=StatementsSequence(
                                           statements=file_fixup,
                                           source_ref=source_ref),
                                       no_branch=None,
                                       source_ref=source_ref),
                  StatementExec(source_code=ExpressionTempVariableRef(
                      variable=source_variable.makeReference(provider),
                      source_ref=source_ref),
                                globals_arg=globals_ref,
                                locals_arg=locals_ref,
                                source_ref=source_ref))

    tried.setChild("statements", tried.getStatements() + statements)

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

    return StatementTryFinally(tried=tried,
                               final=final,
                               public_exc=False,
                               source_ref=source_ref)
def buildExecNode(provider, node, source_ref):
    # "exec" statements, should only occur with Python2.

    # This is using many variables, due to the many details this is
    # dealing with. The locals and globals need to be dealt with in
    # temporary variables, and we need handling of indicators, so
    # that is just the complexity, pylint: disable=R0914

    exec_globals = node.globals
    exec_locals = node.locals
    body = node.body

    orig_globals = exec_globals

    # Handle exec(a,b,c) to be same as exec a, b, c
    if exec_locals is None and exec_globals is None and \
       getKind(body) == "Tuple":
        parts = body.elts
        body = parts[0]

        if len(parts) > 1:
            exec_globals = parts[1]

            if len(parts) > 2:
                exec_locals = parts[2]
        else:
            return StatementRaiseException(
                exception_type=ExpressionBuiltinExceptionRef(
                    exception_name="TypeError", source_ref=source_ref),
                exception_value=ExpressionConstantRef(constant="""\
exec: arg 1 must be a string, file, or code object""",
                                                      source_ref=source_ref),
                exception_trace=None,
                exception_cause=None,
                source_ref=source_ref)

    if provider.isExpressionFunctionBody():
        provider.markAsExecContaining()

        if orig_globals is None:
            provider.markAsUnqualifiedExecContaining(source_ref)

    temp_scope = provider.allocateTempScope("exec")

    locals_value = buildNode(provider, exec_locals, source_ref, True)

    if locals_value is None:
        locals_value = ExpressionConstantRef(constant=None,
                                             source_ref=source_ref)

    globals_value = buildNode(provider, exec_globals, source_ref, True)

    if globals_value is None:
        globals_value = ExpressionConstantRef(constant=None,
                                              source_ref=source_ref)

    source_code = buildNode(provider, body, source_ref)

    source_variable = provider.allocateTempVariable(temp_scope=temp_scope,
                                                    name="exec_source")

    globals_keeper_variable = provider.allocateTempVariable(
        temp_scope=temp_scope, name="globals")

    locals_keeper_variable = provider.allocateTempVariable(
        temp_scope=temp_scope, name="locals")

    plain_indicator_variable = provider.allocateTempVariable(
        temp_scope=temp_scope, name="plain")

    tried = makeStatementsSequenceFromStatements(
        # First evaluate the source code expressions.
        StatementAssignmentVariable(
            variable_ref=ExpressionTargetTempVariableRef(
                variable=source_variable, source_ref=source_ref),
            source=source_code,
            source_ref=source_ref),
        # Assign globals and locals temporary the values given, then fix it
        # up, taking note in the "plain" temporary variable, if it was an
        # "exec" statement with None arguments, in which case the copy back
        # will be necessary.
        StatementAssignmentVariable(
            variable_ref=ExpressionTargetTempVariableRef(
                variable=globals_keeper_variable, source_ref=source_ref),
            source=globals_value,
            source_ref=source_ref),
        StatementAssignmentVariable(
            variable_ref=ExpressionTargetTempVariableRef(
                variable=locals_keeper_variable, source_ref=source_ref),
            source=locals_value,
            source_ref=source_ref),
        StatementAssignmentVariable(
            variable_ref=ExpressionTargetTempVariableRef(
                variable=plain_indicator_variable, source_ref=source_ref),
            source=ExpressionConstantRef(constant=False,
                                         source_ref=source_ref),
            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=makeStatementsSequenceFromStatements(
                StatementAssignmentVariable(
                    variable_ref=ExpressionTargetTempVariableRef(
                        variable=globals_keeper_variable,
                        source_ref=source_ref),
                    source=ExpressionBuiltinGlobals(source_ref=source_ref),
                    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=makeStatementsSequenceFromStatements(
                        StatementAssignmentVariable(
                            variable_ref=ExpressionTargetTempVariableRef(
                                variable=locals_keeper_variable,
                                source_ref=source_ref),
                            source=ExpressionBuiltinLocals(
                                source_ref=source_ref),
                            source_ref=source_ref,
                        ),
                        StatementAssignmentVariable(
                            variable_ref=ExpressionTargetTempVariableRef(
                                variable=plain_indicator_variable,
                                source_ref=source_ref),
                            source=ExpressionConstantRef(
                                constant=True, source_ref=source_ref),
                            source_ref=source_ref,
                        )),
                    no_branch=None,
                    source_ref=source_ref),
            ),
            no_branch=makeStatementsSequenceFromStatements(
                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(
                        statement=StatementAssignmentVariable(
                            variable_ref=ExpressionTargetTempVariableRef(
                                variable=locals_keeper_variable,
                                source_ref=source_ref),
                            source=ExpressionTempVariableRef(
                                variable=globals_keeper_variable,
                                source_ref=source_ref),
                            source_ref=source_ref,
                        )),
                    no_branch=None,
                    source_ref=source_ref)),
            source_ref=source_ref),
        # Source needs some special treatment for not done for "eval", if it's a
        # file object, then  must be read.
        StatementConditional(
            condition=ExpressionBuiltinIsinstance(
                instance=ExpressionTempVariableRef(variable=source_variable,
                                                   source_ref=source_ref),
                classes=ExpressionBuiltinAnonymousRef(
                    builtin_name="file",
                    source_ref=source_ref,
                ),
                source_ref=source_ref),
            yes_branch=makeStatementsSequenceFromStatement(
                statement=StatementAssignmentVariable(
                    variable_ref=ExpressionTargetTempVariableRef(
                        variable=source_variable, source_ref=source_ref),
                    source=ExpressionCallEmpty(
                        called=ExpressionAttributeLookup(
                            source=ExpressionTempVariableRef(
                                variable=source_variable,
                                source_ref=source_ref),
                            attribute_name="read",
                            source_ref=source_ref),
                        source_ref=source_ref),
                    source_ref=source_ref)),
            no_branch=None,
            source_ref=source_ref),
        StatementTryFinally(
            tried=makeStatementsSequenceFromStatement(statement=StatementExec(
                source_code=ExpressionTempVariableRef(variable=source_variable,
                                                      source_ref=source_ref),
                globals_arg=ExpressionTempVariableRef(
                    variable=globals_keeper_variable, source_ref=source_ref),
                locals_arg=ExpressionTempVariableRef(
                    variable=locals_keeper_variable, source_ref=source_ref),
                source_ref=source_ref)),
            final=makeStatementsSequenceFromStatements(
                StatementConditional(
                    condition=ExpressionComparisonIs(
                        left=ExpressionTempVariableRef(
                            variable=plain_indicator_variable,
                            source_ref=source_ref),
                        right=ExpressionConstantRef(constant=True,
                                                    source_ref=source_ref),
                        source_ref=source_ref),
                    yes_branch=makeStatementsSequenceFromStatement(
                        statement=StatementLocalsDictSync(
                            locals_arg=ExpressionTempVariableRef(
                                variable=locals_keeper_variable,
                                source_ref=source_ref,
                            ),
                            source_ref=source_ref.atInternal())),
                    no_branch=None,
                    source_ref=source_ref), ),
            public_exc=False,
            source_ref=source_ref))

    final = makeStatementsSequenceFromStatements(
        StatementDelVariable(variable_ref=ExpressionTargetTempVariableRef(
            variable=source_variable, source_ref=source_ref),
                             tolerant=True,
                             source_ref=source_ref),
        StatementDelVariable(variable_ref=ExpressionTargetTempVariableRef(
            variable=globals_keeper_variable, source_ref=source_ref),
                             tolerant=True,
                             source_ref=source_ref),
        StatementDelVariable(variable_ref=ExpressionTargetTempVariableRef(
            variable=locals_keeper_variable, source_ref=source_ref),
                             tolerant=True,
                             source_ref=source_ref),
        StatementDelVariable(variable_ref=ExpressionTargetTempVariableRef(
            variable=plain_indicator_variable, source_ref=source_ref),
                             tolerant=True,
                             source_ref=source_ref),
    )

    return StatementTryFinally(tried=tried,
                               final=final,
                               public_exc=False,
                               source_ref=source_ref)