def makeValueComparisonReturn(left, right, comparator):
        yield StatementAssignmentVariable(
            variable   = tmp_variable,
            source     = makeComparisonNode(
                left       = left,
                right      = right,
                comparator = comparator,
                source_ref = source_ref
            ),
            source_ref = source_ref,
        )

        yield StatementConditional(
            condition  = ExpressionOperationNOT(
                operand    = ExpressionTempVariableRef(
                    variable   = tmp_variable,
                    source_ref = source_ref
                ),
                source_ref = source_ref
            ),
            yes_branch = makeStatementsSequenceFromStatement(
                statement = StatementReturn(
                    expression = ExpressionTempVariableRef(
                        variable   = tmp_variable,
                        source_ref = source_ref
                    ),
                    source_ref = source_ref
                )
            ),
            no_branch  = None,
            source_ref = source_ref
        )
示例#2
0
def makeConditionalStatement(condition, yes_branch, no_branch, source_ref):
    """ Create conditional statement, with yes_branch not being empty.

        May have to invert condition to achieve that.
    """

    if yes_branch is None:
        condition = ExpressionOperationNOT(
            operand    = condition,
            source_ref = condition.getSourceReference()
        )

        yes_branch, no_branch = no_branch, yes_branch

    if not yes_branch.isStatementsSequence():
        yes_branch = makeStatementsSequenceFromStatement(yes_branch)

    if no_branch is not None and not no_branch.isStatementsSequence():
        no_branch = makeStatementsSequenceFromStatement(no_branch)

    return StatementConditional(
        condition  = condition,
        yes_branch = yes_branch,
        no_branch  = no_branch,
        source_ref = source_ref
    )
示例#3
0
def _buildInplaceAssignAttributeNode(provider, lookup_source, attribute_name,
                                     tmp_variable1, tmp_variable2, operator,
                                     expression, source_ref):
    # First assign the target value to a temporary variable.
    preserve_to_tmp = StatementAssignmentVariable(
        variable=tmp_variable1,
        source=ExpressionAttributeLookup(source=lookup_source.makeClone(),
                                         attribute_name=attribute_name,
                                         source_ref=source_ref),
        source_ref=source_ref)

    # Second assign the in-place result to a temporary variable
    inplace_to_tmp = StatementAssignmentVariable(
        variable=tmp_variable2,
        source=makeExpressionOperationBinaryInplace(
            operator=operator,
            left=ExpressionTempVariableRef(variable=tmp_variable1,
                                           source_ref=source_ref),
            right=expression,
            source_ref=source_ref),
        source_ref=source_ref)

    # Third, copy it over, if the reference values change, i.e. IsNot is true.
    copy_back_from_tmp = StatementConditional(
        condition=ExpressionComparisonIsNOT(
            left=ExpressionTempVariableRef(variable=tmp_variable1,
                                           source_ref=source_ref),
            right=ExpressionTempVariableRef(variable=tmp_variable2,
                                            source_ref=source_ref),
            source_ref=source_ref),
        yes_branch=makeStatementsSequenceFromStatement(
            statement=StatementAssignmentAttribute(
                expression=lookup_source.makeClone(),
                attribute_name=attribute_name,
                source=ExpressionTempVariableRef(variable=tmp_variable2,
                                                 source_ref=source_ref),
                source_ref=source_ref)),
        no_branch=None,
        source_ref=source_ref)

    copy_back_from_tmp = makeTryFinallyStatement(
        provider=provider,
        tried=copy_back_from_tmp,
        final=StatementReleaseVariable(variable=tmp_variable2,
                                       source_ref=source_ref),
        source_ref=source_ref)

    return (
        preserve_to_tmp,
        # making sure the above temporary variable is deleted in any case.
        makeTryFinallyStatement(provider=provider,
                                tried=(
                                    inplace_to_tmp,
                                    copy_back_from_tmp,
                                ),
                                final=StatementReleaseVariable(
                                    variable=tmp_variable1,
                                    source_ref=source_ref),
                                source_ref=source_ref))
def buildAssertNode(provider, node, source_ref):
    # Build assert statements. These are re-formulated as described in the
    # developer manual too. They end up as conditional statement with raises of
    # AssertionError exceptions.

    # Underlying assumption:
    #
    # Assert x, y is the same as:
    # if not x:
    #     raise AssertionError, y

    # Therefore assert statements are really just conditional statements with a
    # static raise contained.
    #
    # Starting with CPython2.7, it is, which means the creation of the exception
    # object is no more delayed:
    # if not x:
    #     raise AssertionError( y )

    if Utils.python_version < 270 or node.msg is None:
        raise_statement = StatementRaiseException(
            exception_type  = ExpressionBuiltinExceptionRef(
                exception_name = "AssertionError",
                source_ref     = source_ref
                ),
            exception_value = buildNode( provider, node.msg, source_ref, True ),
            exception_trace = None,
            exception_cause = None,
            source_ref      = source_ref
        )
    else:
        raise_statement = StatementRaiseException(
            exception_type  =  ExpressionBuiltinMakeException(
                exception_name = "AssertionError",
                args           = (
                    buildNode( provider, node.msg, source_ref, True ),
                ),
                source_ref     = source_ref
            ),
            exception_value = None,
            exception_trace = None,
            exception_cause = None,
            source_ref      = source_ref
        )

    return StatementConditional(
        condition  = ExpressionOperationNOT(
            operand    = buildNode( provider, node.test, source_ref ),
            source_ref = source_ref
        ),
        yes_branch = StatementsSequence(
            statements = (
                raise_statement,
            ),
            source_ref = source_ref
        ),
        no_branch  = None,
        source_ref = source_ref
    )
def _buildInplaceAssignVariableNode(variable_ref, tmp_variable1, tmp_variable2,
                                    operator, expression, source_ref):
    assert variable_ref.isExpressionTargetVariableRef(), variable_ref

    # First assign the target value to a temporary variable.
    preserve_to_tmp = StatementAssignmentVariable(
        variable_ref=ExpressionTargetTempVariableRef(variable=tmp_variable1,
                                                     source_ref=source_ref),
        source=ExpressionVariableRef(
            variable_name=variable_ref.getVariableName(),
            source_ref=source_ref),
        source_ref=source_ref)
    # Second assign the inplace result to a temporary variable
    inplace_to_tmp = StatementAssignmentVariable(
        variable_ref=ExpressionTargetTempVariableRef(variable=tmp_variable2,
                                                     source_ref=source_ref),
        source=ExpressionOperationBinaryInplace(operator=operator,
                                                left=ExpressionTempVariableRef(
                                                    variable=tmp_variable1,
                                                    source_ref=source_ref),
                                                right=expression,
                                                source_ref=source_ref),
        source_ref=source_ref)

    # Third, copy it over, if the reference values change, i.e. IsNot is true.
    copy_back_from_tmp = StatementConditional(
        condition=ExpressionComparisonIsNOT(
            left=ExpressionTempVariableRef(variable=tmp_variable1,
                                           source_ref=source_ref),
            right=ExpressionTempVariableRef(variable=tmp_variable2,
                                            source_ref=source_ref),
            source_ref=source_ref),
        yes_branch=makeStatementsSequenceFromStatement(
            statement=StatementAssignmentVariable(
                variable_ref=variable_ref.makeCloneAt(source_ref),
                source=ExpressionTempVariableRef(variable=tmp_variable2,
                                                 source_ref=source_ref),
                source_ref=source_ref)),
        no_branch=None,
        source_ref=source_ref)

    return (
        preserve_to_tmp,
        # making sure the above temporary variable is deleted in any case.
        makeTryFinallyStatement(
            tried=(inplace_to_tmp, copy_back_from_tmp),
            final=(
                StatementDelVariable(
                    variable_ref=ExpressionTargetTempVariableRef(
                        variable=tmp_variable1, source_ref=source_ref),
                    tolerant=False,
                    source_ref=source_ref),
                StatementDelVariable(
                    variable_ref=ExpressionTargetTempVariableRef(
                        variable=tmp_variable2, source_ref=source_ref),
                    tolerant=True,
                    source_ref=source_ref),
            ),
            source_ref=source_ref))
def makeTryExceptNoRaise(provider, temp_scope, tried, handling, no_raise,
                         public_exc, source_ref):
    # This helper executes the core re-formulation of "no_raise" blocks, which
    # are the "else" blocks of "try"/"except" statements. In order to limit the
    # execution, we use an indicator variable instead, which will signal that
    # the tried block executed up to the end. And then we make the else block be
    # a conditional statement checking that.

    assert no_raise is not None

    tmp_handler_indicator_variable = provider.allocateTempVariable(
        temp_scope=temp_scope, name="unhandled_indicator")

    statements = mergeStatements((StatementAssignmentVariable(
        variable_ref=ExpressionTargetTempVariableRef(
            variable=tmp_handler_indicator_variable.makeReference(provider),
            source_ref=source_ref.atInternal()),
        source=ExpressionConstantRef(constant=False, source_ref=source_ref),
        source_ref=no_raise.getSourceReference().atInternal()), handling),
                                 allow_none=True)

    handling = StatementsSequence(statements=statements, source_ref=source_ref)

    tried = (StatementTryExcept(tried=tried,
                                handling=handling,
                                public_exc=public_exc,
                                source_ref=source_ref),
             StatementConditional(condition=ExpressionComparisonIs(
                 left=ExpressionTempVariableRef(
                     variable=tmp_handler_indicator_variable.makeReference(
                         provider),
                     source_ref=source_ref),
                 right=ExpressionConstantRef(constant=True,
                                             source_ref=source_ref),
                 source_ref=source_ref),
                                  yes_branch=no_raise,
                                  no_branch=None,
                                  source_ref=source_ref))

    final = StatementDelVariable(variable_ref=ExpressionTargetTempVariableRef(
        variable=tmp_handler_indicator_variable.makeReference(provider),
        source_ref=source_ref.atInternal()),
                                 tolerant=False,
                                 source_ref=source_ref.atInternal()),

    return StatementsSequence(statements=(StatementAssignmentVariable(
        variable_ref=ExpressionTargetTempVariableRef(
            variable=tmp_handler_indicator_variable.makeReference(provider),
            source_ref=source_ref.atInternal()),
        source=ExpressionConstantRef(constant=True, source_ref=source_ref),
        source_ref=source_ref.atInternal()),
                                          makeTryFinallyStatement(
                                              tried=tried,
                                              final=final,
                                              source_ref=source_ref)),
                              source_ref=source_ref)
def makeTryExceptNoRaise(provider, temp_scope, tried, handlers, no_raise,
                         source_ref):
    # This helper executes the core re-formulation of "no_raise" blocks, which
    # are the "else" blocks of "try"/"except" statements. In order to limit the
    # execution, we use an indicator variable instead, which will signal that
    # the tried block executed up to the end. And then we make the else block be
    # a conditional statement checking that.

    # This is a separate function, so it can be re-used in other
    # re-formulations, e.g. with statements.

    assert no_raise is not None
    assert len(handlers) > 0

    tmp_handler_indicator_variable = provider.allocateTempVariable(
        temp_scope=temp_scope, name="unhandled_indicator")

    for handler in handlers:
        statements = (StatementAssignmentVariable(
            variable_ref=ExpressionTargetTempVariableRef(
                variable=tmp_handler_indicator_variable.makeReference(
                    provider),
                source_ref=source_ref.atInternal()),
            source=ExpressionConstantRef(constant=False,
                                         source_ref=source_ref),
            source_ref=no_raise.getSourceReference().atInternal()),
                      handler.getExceptionBranch())

        handler.setExceptionBranch(
            makeStatementsSequence(statements=statements,
                                   allow_none=True,
                                   source_ref=source_ref))

    statements = (StatementAssignmentVariable(
        variable_ref=ExpressionTargetTempVariableRef(
            variable=tmp_handler_indicator_variable.makeReference(provider),
            source_ref=source_ref.atInternal()),
        source=ExpressionConstantRef(constant=True, source_ref=source_ref),
        source_ref=source_ref),
                  StatementTryExcept(tried=tried,
                                     handlers=handlers,
                                     source_ref=source_ref),
                  StatementConditional(condition=ExpressionComparisonIs(
                      left=ExpressionTempVariableRef(
                          variable=tmp_handler_indicator_variable.
                          makeReference(provider),
                          source_ref=source_ref),
                      right=ExpressionConstantRef(constant=True,
                                                  source_ref=source_ref),
                      source_ref=source_ref),
                                       yes_branch=no_raise,
                                       no_branch=None,
                                       source_ref=source_ref))

    return StatementsSequence(statements=statements, source_ref=source_ref)
示例#8
0
def makeTryExceptSingleHandlerNode(tried, exception_name, handler_body,
                                   public_exc, source_ref):
    if public_exc:
        statements = [
            StatementPreserveFrameException(
                source_ref = source_ref.atInternal()
            ),
            StatementPublishException(
                source_ref = source_ref.atInternal()
            )
        ]
    else:
        statements = []

    statements.append(
        StatementConditional(
            condition  = ExpressionComparisonExceptionMatch(
                left       = ExpressionCaughtExceptionTypeRef(
                    source_ref = source_ref
                ),
                right      = ExpressionBuiltinExceptionRef(
                    exception_name = exception_name,
                    source_ref     = source_ref
                ),
                source_ref = source_ref
            ),
            yes_branch = handler_body,
            no_branch  = makeReraiseExceptionStatement(
                source_ref = source_ref
            ),
            source_ref = source_ref
        )
    )

    if Utils.python_version >= 300 and public_exc:
        statements = [
            makeTryFinallyStatement(
                tried      = statements,
                final      = StatementRestoreFrameException(
                    source_ref = source_ref.atInternal()
                ),
                source_ref = source_ref.atInternal()
            )
        ]

    return StatementTryExcept(
        tried      = tried,
        handling   = StatementsSequence(
            statements = statements,
            source_ref = source_ref
        ),
        public_exc = public_exc,
        source_ref = source_ref
    )
示例#9
0
def buildAssertNode(provider, node, source_ref):
    # Build assert statements. These are re-formulated as described in the
    # developer manual too. They end up as conditional statement with raises of
    # AssertionError exceptions.

    # Underlying assumption:
    #
    # Assert x, y is the same as:
    # if not x:
    #     raise AssertionError, y

    # Therefore assert statements are really just conditional statements with a
    # static raise contained.
    #

    exception_value = buildNode(provider, node.msg, source_ref, True)

    if "no_asserts" in getPythonFlags():
        return None

    if exception_value is not None and python_version > 272:
        exception_value = ExpressionMakeTuple(
            elements   = (exception_value,),
            source_ref = source_ref
        )

    raise_statement = StatementRaiseException(
        exception_type  = ExpressionBuiltinExceptionRef(
            exception_name = "AssertionError",
            source_ref     = source_ref
        ),
        exception_value = exception_value,
        exception_trace = None,
        exception_cause = None,
        source_ref      = source_ref
    )

    return StatementConditional(
        condition  = ExpressionOperationNOT(
            operand    = buildNode(provider, node.test, source_ref),
            source_ref = source_ref
        ),
        yes_branch = StatementsSequence(
            statements = (
                raise_statement,
            ),
            source_ref = source_ref
        ),
        no_branch  = None,
        source_ref = source_ref
    )
def _makeTryExceptSingleHandlerNode(provider, public_exc, tried,
                                    exception_name, handler_body, source_ref):
    # No need to create this in the first place if nothing is tried.
    if tried is None:
        return None

    if public_exc:
        preserver_id = provider.allocatePreserverId()

        handling = [
            StatementPreserveFrameException(preserver_id=preserver_id,
                                            source_ref=source_ref),
            StatementPublishException(source_ref=source_ref)
        ]
    else:
        handling = []

    if not handler_body.isStatementsSequence():
        handler_body = makeStatementsSequenceFromStatement(
            statement=handler_body)

    if not tried.isStatementsSequence():
        tried = makeStatementsSequenceFromStatement(statement=tried)

    handling.append(
        StatementConditional(condition=ExpressionComparisonExceptionMatch(
            left=ExpressionCaughtExceptionTypeRef(source_ref=source_ref),
            right=ExpressionBuiltinExceptionRef(exception_name=exception_name,
                                                source_ref=source_ref),
            source_ref=source_ref),
                             yes_branch=handler_body,
                             no_branch=makeReraiseExceptionStatement(
                                 source_ref=source_ref),
                             source_ref=source_ref))

    if python_version >= 300 and public_exc:
        handling = (makeTryFinallyStatement(
            provider=provider,
            tried=handling,
            final=StatementRestoreFrameException(
                preserver_id=preserver_id, source_ref=source_ref.atInternal()),
            source_ref=source_ref.atInternal()), )

    handling = makeStatementsSequenceFromStatements(*handling)

    return StatementTry(tried=tried,
                        except_handler=handling,
                        break_handler=None,
                        continue_handler=None,
                        return_handler=None,
                        source_ref=source_ref)
示例#11
0
def buildConditionNode(provider, node, source_ref):
    # Conditional statements may have one or two branches. We will never see an
    # "elif", because that's already dealt with by module "ast", which turns it
    # into nested conditional statements.

    return StatementConditional(
        condition=buildNode(provider, node.test, source_ref),
        yes_branch=buildStatementsNode(provider=provider,
                                       nodes=node.body,
                                       source_ref=source_ref),
        no_branch=buildStatementsNode(
            provider=provider,
            nodes=node.orelse if node.orelse else None,
            source_ref=source_ref),
        source_ref=source_ref)
def _buildInplaceAssignVariableNode(result, variable_ref, tmp_variable1,
                                    tmp_variable2, operator, expression,
                                    source_ref):
    assert variable_ref.isExpressionTargetVariableRef(), variable_ref

    return (
        # First assign the target value to a temporary variable.
        StatementAssignmentVariable(
            variable_ref=ExpressionTargetTempVariableRef(
                variable=tmp_variable1.makeReference(result),
                source_ref=source_ref),
            source=ExpressionVariableRef(
                variable_name=variable_ref.getVariableName(),
                source_ref=source_ref),
            source_ref=source_ref),
        # Second assign the inplace result to a temporary variable.
        StatementAssignmentVariable(
            variable_ref=ExpressionTargetTempVariableRef(
                variable=tmp_variable2.makeReference(result),
                source_ref=source_ref),
            source=ExpressionOperationBinaryInplace(
                operator=operator,
                left=ExpressionTempVariableRef(
                    variable=tmp_variable1.makeReference(result),
                    source_ref=source_ref),
                right=expression,
                source_ref=source_ref),
            source_ref=source_ref),
        # Copy it over, if the reference values change, i.e. IsNot is true.
        StatementConditional(
            condition=ExpressionComparisonIsNOT(
                left=ExpressionTempVariableRef(
                    variable=tmp_variable1.makeReference(result),
                    source_ref=source_ref),
                right=ExpressionTempVariableRef(
                    variable=tmp_variable2.makeReference(result),
                    source_ref=source_ref),
                source_ref=source_ref),
            yes_branch=makeStatementsSequenceFromStatement(
                statement=StatementAssignmentVariable(
                    variable_ref=variable_ref.makeCloneAt(source_ref),
                    source=ExpressionTempVariableRef(
                        variable=tmp_variable2.makeReference(result),
                        source_ref=source_ref),
                    source_ref=source_ref)),
            no_branch=None,
            source_ref=source_ref))
示例#13
0
def _buildInplaceAssignAttributeNode(provider, lookup_source, attribute_name,
                                     tmp_variable1, tmp_variable2, operator,
                                     expression, source_ref):
    return (
        # First assign the target value to a temporary variable.
        StatementAssignmentVariable(
            variable_ref=ExpressionTargetTempVariableRef(
                variable=tmp_variable1.makeReference(provider),
                source_ref=source_ref),
            source=ExpressionAttributeLookup(
                expression=lookup_source.makeCloneAt(source_ref),
                attribute_name=attribute_name,
                source_ref=source_ref),
            source_ref=source_ref),
        # Second assign the inplace result to a temporary variable.
        StatementAssignmentVariable(
            variable_ref=ExpressionTargetTempVariableRef(
                variable=tmp_variable2.makeReference(provider),
                source_ref=source_ref),
            source=ExpressionOperationBinaryInplace(
                operator=operator,
                left=ExpressionTempVariableRef(
                    variable=tmp_variable1.makeReference(provider),
                    source_ref=source_ref),
                right=expression,
                source_ref=source_ref),
            source_ref=source_ref),
        # Copy it over, if the reference values change, i.e. IsNot is true.
        StatementConditional(
            condition=ExpressionComparisonIsNOT(
                left=ExpressionTempVariableRef(
                    variable=tmp_variable1.makeReference(provider),
                    source_ref=source_ref),
                right=ExpressionTempVariableRef(
                    variable=tmp_variable2.makeReference(provider),
                    source_ref=source_ref),
                source_ref=source_ref),
            yes_branch=makeStatementsSequenceFromStatement(
                statement=StatementAssignmentAttribute(
                    expression=lookup_source.makeCloneAt(source_ref),
                    attribute_name=attribute_name,
                    source=ExpressionTempVariableRef(
                        variable=tmp_variable2.makeReference(provider),
                        source_ref=source_ref),
                    source_ref=source_ref)),
            no_branch=None,
            source_ref=source_ref))
示例#14
0
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 = ExpressionConstantNoneRef(source_ref=source_ref)

    if globals_node is None:
        globals_node = ExpressionConstantNoneRef(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=ExpressionConstantNoneRef(source_ref=source_ref),
            source_ref=source_ref),
        expression_no=ExpressionTempVariableRef(
            variable=globals_keeper_variable, source_ref=source_ref),
        expression_yes=makeExpressionBuiltinLocals(provider=provider,
                                                   source_ref=source_ref),
        source_ref=source_ref)

    pre_statements = [
        # First assign globals and locals temporary the values given.
        StatementAssignmentVariable(
            variable=globals_keeper_variable,
            source=globals_node,
            source_ref=source_ref,
        ),
        StatementAssignmentVariable(
            variable=locals_keeper_variable,
            source=locals_node,
            source_ref=source_ref,
        ),
        StatementConditional(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(
                                 StatementAssignmentVariable(
                                     variable=locals_keeper_variable,
                                     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=ExpressionConstantNoneRef(source_ref=source_ref),
            source_ref=source_ref),
                             yes_branch=makeStatementsSequenceFromStatement(
                                 StatementAssignmentVariable(
                                     variable=globals_keeper_variable,
                                     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 buildPrintNode(provider, node, source_ref):
    # "print" statements, should only occur with Python2.

    def wrapValue(value):
        if value.isExpressionConstantRef():
            str_value = value.getStrValue()

            if str_value is not None:
                return str_value

        return ExpressionBuiltinStr(value=value,
                                    source_ref=value.getSourceReference())

    if node.dest is not None:
        temp_scope = provider.allocateTempScope("print")

        tmp_target_variable = provider.allocateTempVariable(
            temp_scope=temp_scope, name="target")

        target_default_statement = StatementAssignmentVariable(
            variable_ref=ExpressionTargetTempVariableRef(
                variable=tmp_target_variable, source_ref=source_ref),
            source=ExpressionImportModuleHard(module_name="sys",
                                              import_name="stdout",
                                              source_ref=source_ref),
            source_ref=source_ref)

        statements = [
            StatementAssignmentVariable(
                variable_ref=ExpressionTargetTempVariableRef(
                    variable=tmp_target_variable, source_ref=source_ref),
                source=buildNode(provider=provider,
                                 node=node.dest,
                                 source_ref=source_ref),
                source_ref=source_ref),
            StatementConditional(
                condition=ExpressionComparisonIs(
                    left=ExpressionTempVariableRef(
                        variable=tmp_target_variable, source_ref=source_ref),
                    right=ExpressionConstantRef(constant=None,
                                                source_ref=source_ref),
                    source_ref=source_ref),
                yes_branch=makeStatementsSequenceFromStatement(
                    statement=target_default_statement),
                no_branch=None,
                source_ref=source_ref)
        ]

    values = buildNodeList(provider=provider,
                           nodes=node.values,
                           source_ref=source_ref)

    values = [wrapValue(value) for value in values]

    if node.dest is not None:
        print_statements = [
            StatementPrintValue(dest=ExpressionTempVariableRef(
                variable=tmp_target_variable, source_ref=source_ref),
                                value=value,
                                source_ref=source_ref) for value in values
        ]

        if node.nl:
            print_statements.append(
                StatementPrintNewline(dest=ExpressionTempVariableRef(
                    variable=tmp_target_variable, source_ref=source_ref),
                                      source_ref=source_ref))

        statements.append(
            makeTryFinallyStatement(
                tried=print_statements,
                final=StatementDelVariable(
                    variable_ref=ExpressionTargetTempVariableRef(
                        variable=tmp_target_variable, source_ref=source_ref),
                    tolerant=False,
                    source_ref=source_ref),
                source_ref=source_ref))
    else:
        statements = [
            StatementPrintValue(dest=None, value=value, source_ref=source_ref)
            for value in values
        ]

        if node.nl:
            statements.append(
                StatementPrintNewline(dest=None, source_ref=source_ref))

    return StatementsSequence(statements=statements, source_ref=source_ref)
示例#16
0
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 _buildWithNode(provider, context_expr, assign_target, body, source_ref):
    with_source = buildNode(provider, context_expr, source_ref)

    temp_scope = provider.allocateTempScope("with")

    tmp_source_variable = provider.allocateTempVariable(temp_scope=temp_scope,
                                                        name="source")
    tmp_exit_variable = provider.allocateTempVariable(temp_scope=temp_scope,
                                                      name="exit")
    tmp_enter_variable = provider.allocateTempVariable(temp_scope=temp_scope,
                                                       name="enter")
    tmp_indicator_variable = provider.allocateTempVariable(
        temp_scope=temp_scope, name="indicator")

    statements = (buildAssignmentStatements(provider=provider,
                                            node=assign_target,
                                            allow_none=True,
                                            source=ExpressionTempVariableRef(
                                                variable=tmp_enter_variable,
                                                source_ref=source_ref),
                                            source_ref=source_ref), body)

    with_body = makeStatementsSequence(statements=statements,
                                       allow_none=True,
                                       source_ref=source_ref)

    if Options.isFullCompat() and with_body is not None:
        with_exit_source_ref = with_body.getStatements()[-1].\
          getSourceReference()
    else:
        with_exit_source_ref = source_ref

    # The "__enter__" and "__exit__" were normal attribute lookups under
    # CPython2.6, but that changed with CPython2.7.
    if Utils.python_version < 270:
        attribute_lookup_class = ExpressionAttributeLookup
    else:
        attribute_lookup_class = ExpressionSpecialAttributeLookup

    statements = [
        # First assign the with context to a temporary variable.
        StatementAssignmentVariable(
            variable_ref=ExpressionTargetTempVariableRef(
                variable=tmp_source_variable, source_ref=source_ref),
            source=with_source,
            source_ref=source_ref),
        # Next, assign "__enter__" and "__exit__" attributes to temporary
        # variables.
        StatementAssignmentVariable(
            variable_ref=ExpressionTargetTempVariableRef(
                variable=tmp_exit_variable, source_ref=source_ref),
            source=attribute_lookup_class(source=ExpressionTempVariableRef(
                variable=tmp_source_variable, source_ref=source_ref),
                                          attribute_name="__exit__",
                                          source_ref=source_ref),
            source_ref=source_ref),
        StatementAssignmentVariable(
            variable_ref=ExpressionTargetTempVariableRef(
                variable=tmp_enter_variable, source_ref=source_ref),
            source=ExpressionCallEmpty(called=attribute_lookup_class(
                source=ExpressionTempVariableRef(variable=tmp_source_variable,
                                                 source_ref=source_ref),
                attribute_name="__enter__",
                source_ref=source_ref),
                                       source_ref=source_ref),
            source_ref=source_ref),
        StatementAssignmentVariable(
            variable_ref=ExpressionTargetTempVariableRef(
                variable=tmp_indicator_variable, source_ref=source_ref),
            source=ExpressionConstantRef(constant=True, source_ref=source_ref),
            source_ref=source_ref),
    ]

    source_ref = source_ref.atInternal()

    statements += [
        makeTryFinallyStatement(
            tried=makeTryExceptSingleHandlerNode(
                tried=with_body,
                exception_name="BaseException",
                handler_body=StatementsSequence(
                    statements=(
                        # Prevents final block from calling __exit__ as
                        # well.
                        StatementAssignmentVariable(
                            variable_ref=ExpressionTargetTempVariableRef(
                                variable=tmp_indicator_variable,
                                source_ref=source_ref),
                            source=ExpressionConstantRef(
                                constant=False, source_ref=source_ref),
                            source_ref=source_ref),
                        StatementConditional(
                            condition=ExpressionCallNoKeywords(
                                called=ExpressionTempVariableRef(
                                    variable=tmp_exit_variable,
                                    source_ref=source_ref),
                                args=ExpressionMakeTuple(
                                    elements=(
                                        ExpressionCaughtExceptionTypeRef(
                                            source_ref=source_ref),
                                        ExpressionCaughtExceptionValueRef(
                                            source_ref=source_ref),
                                        ExpressionCaughtExceptionTracebackRef(
                                            source_ref=source_ref),
                                    ),
                                    source_ref=source_ref),
                                source_ref=source_ref),
                            no_branch=makeStatementsSequenceFromStatement(
                                statement=StatementRaiseException(
                                    exception_type=None,
                                    exception_value=None,
                                    exception_trace=None,
                                    exception_cause=None,
                                    source_ref=source_ref)),
                            yes_branch=None,
                            source_ref=source_ref),
                    ),
                    source_ref=source_ref),
                public_exc=Utils.python_version >= 270,
                source_ref=source_ref),
            final=StatementConditional(
                condition=ExpressionComparisonIs(
                    left=ExpressionTempVariableRef(
                        variable=tmp_indicator_variable,
                        source_ref=source_ref),
                    right=ExpressionConstantRef(constant=True,
                                                source_ref=source_ref),
                    source_ref=source_ref),
                yes_branch=makeStatementsSequenceFromStatement(
                    statement=StatementExpressionOnly(
                        expression=ExpressionCallNoKeywords(
                            called=ExpressionTempVariableRef(
                                variable=tmp_exit_variable,
                                source_ref=source_ref),
                            args=ExpressionConstantRef(constant=(None, None,
                                                                 None),
                                                       source_ref=source_ref),
                            source_ref=with_exit_source_ref),
                        source_ref=source_ref)),
                no_branch=None,
                source_ref=source_ref),
            source_ref=source_ref)
    ]

    return makeTryFinallyStatement(
        tried=statements,
        final=(
            StatementDelVariable(variable_ref=ExpressionTargetTempVariableRef(
                variable=tmp_source_variable, source_ref=source_ref),
                                 tolerant=True,
                                 source_ref=source_ref),
            StatementDelVariable(variable_ref=ExpressionTargetTempVariableRef(
                variable=tmp_enter_variable, source_ref=source_ref),
                                 tolerant=True,
                                 source_ref=source_ref),
            StatementDelVariable(variable_ref=ExpressionTargetTempVariableRef(
                variable=tmp_exit_variable, source_ref=source_ref),
                                 tolerant=True,
                                 source_ref=source_ref),
            StatementDelVariable(variable_ref=ExpressionTargetTempVariableRef(
                variable=tmp_indicator_variable, source_ref=source_ref),
                                 tolerant=True,
                                 source_ref=source_ref),
        ),
        source_ref=source_ref)
def _buildContractionBodyNode(provider, node, emit_class, start_value,
                              container_tmp, outer_iter_ref, temp_scope,
                              assign_provider, source_ref, function_body):

    if start_value is not None:
        statements = [
            StatementAssignmentVariable(
                variable_ref=ExpressionTargetTempVariableRef(
                    variable=container_tmp.makeReference(function_body),
                    source_ref=source_ref),
                source=start_value,
                source_ref=source_ref.atInternal())
        ]

        if assign_provider:
            tmp_variables = []
        else:
            tmp_variables = [container_tmp]
    else:
        statements = []
        tmp_variables = []

    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_value_variable = function_body.allocateTempVariable(
            temp_scope=temp_scope, name="iter_value_%d" % count)

        tmp_variables.append(tmp_value_variable)

        # 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]:

            def makeIteratorRef():
                return outer_iter_ref.makeCloneAt(source_ref)

            tmp_iter_variable = None

            nested_statements = []
        else:
            # First create the iterator and store it, next should be loop body
            value_iterator = ExpressionBuiltinIter1(value=buildNode(
                provider=function_body, node=qual.iter, source_ref=source_ref),
                                                    source_ref=source_ref)

            tmp_iter_variable = function_body.allocateTempVariable(
                temp_scope=temp_scope, name="contraction_iter_%d" % count)

            tmp_variables.append(tmp_iter_variable)

            nested_statements = [
                StatementAssignmentVariable(
                    variable_ref=ExpressionTargetTempVariableRef(
                        variable=tmp_iter_variable.makeReference(
                            function_body),
                        source_ref=source_ref),
                    source=value_iterator,
                    source_ref=source_ref)
            ]

            def makeIteratorRef():
                return ExpressionTempVariableRef(
                    variable=tmp_iter_variable.makeReference(function_body),
                    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=makeIteratorRef(),
                                                      source_ref=source_ref),
                        source_ref=source_ref)),
                exception_name="StopIteration",
                handler_body=makeStatementsSequenceFromStatement(
                    statement=StatementBreakLoop(
                        source_ref=source_ref.atInternal())),
                public_exc=False,
                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))

        if tmp_iter_variable is not None:
            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)
    statements = mergeStatements(statements)

    if emit_class is ExpressionYield:
        statements.insert(0, StatementGeneratorEntry(source_ref=source_ref))

    del_statements = []
    for tmp_variable in tmp_variables:
        del_statements.append(
            StatementDelVariable(variable_ref=ExpressionTargetTempVariableRef(
                variable=tmp_variable.makeReference(function_body),
                source_ref=source_ref),
                                 tolerant=True,
                                 source_ref=source_ref.atInternal()))

    return statements, del_statements
示例#19
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
示例#20
0
def buildTryExceptionNode(provider, node, source_ref):
    # Try/except nodes. Re-formulated as described in the developer
    # manual. Exception handlers made the assignment to variables explicit. Same
    # for the "del" as done for Python3. Also catches always work a tuple of
    # exception types and hides away that they may be built or not.

    # Many variables and branches, due to the re-formulation that is going on
    # here, which just has the complexity, pylint: disable=R0912,R0914

    tried = buildStatementsNode(
        provider   = provider,
        nodes      = node.body,
        source_ref = source_ref
    )

    handlers = []

    for handler in node.handlers:
        exception_expression, exception_assign, exception_block = (
            handler.type,
            handler.name,
            handler.body
        )

        if exception_assign is None:
            statements = [
                buildStatementsNode(
                    provider   = provider,
                    nodes      = exception_block,
                    source_ref = source_ref
                )
            ]
        elif Utils.python_version < 300:
            statements = [
                buildAssignmentStatements(
                    provider   = provider,
                    node       = exception_assign,
                    source     = ExpressionCaughtExceptionValueRef(
                        source_ref = source_ref.atInternal()
                    ),
                    source_ref = source_ref.atInternal()
                ),
                buildStatementsNode(
                    provider   = provider,
                    nodes      = exception_block,
                    source_ref = source_ref
                )
            ]
        else:
            target_info = decodeAssignTarget(
                provider   = provider,
                node       = exception_assign,
                source_ref = source_ref,
            )

            # We didn't allow None, therefore it cannot be None, and
            # the unpack is safe: pylint: disable=W0633
            kind, detail = target_info

            assert kind == "Name", kind
            kind = "Name_Exception"

            statements = [
                buildAssignmentStatements(
                    provider   = provider,
                    node       = exception_assign,
                    source     = ExpressionCaughtExceptionValueRef(
                        source_ref = source_ref.atInternal()
                    ),
                    source_ref = source_ref.atInternal()
                ),
                makeTryFinallyStatement(
                    tried      = buildStatementsNode(
                        provider   = provider,
                        nodes      = exception_block,
                        source_ref = source_ref
                    ),
                    final      = buildDeleteStatementFromDecoded(
                        kind       = kind,
                        detail     = detail,
                        source_ref = source_ref
                    ),
                    source_ref = source_ref
                )
            ]

        handler_body = makeStatementsSequence(
            statements = statements,
            allow_none = True,
            source_ref = source_ref
        )

        exception_types = buildNode(
            provider   = provider,
            node       = exception_expression,
            source_ref = source_ref,
            allow_none = True
        )

        # The exception types should be a tuple, so as to be most general.
        if exception_types is None:
            if handler is not node.handlers[-1]:
                SyntaxErrors.raiseSyntaxError(
                    reason     = "default 'except:' must be last",
                    source_ref = source_ref.atLineNumber(
                        handler.lineno-1
                          if Options.isFullCompat() else
                        handler.lineno
                    )
                )

        handlers.append(
            (
                exception_types,
                handler_body,
            )
        )

    # Reraise by default
    exception_handling = makeReraiseExceptionStatement(
        source_ref = source_ref
    )

    for exception_type, handler in reversed(handlers):
        if exception_type is None:
            # A default handler was given, so use that indead.
            exception_handling = handler
        else:
            exception_handling = StatementsSequence(
                statements = (
                    StatementConditional(
                        condition  = ExpressionComparisonExceptionMatch(
                            left       = ExpressionCaughtExceptionTypeRef(
                                source_ref = exception_type.source_ref
                            ),
                            right      = exception_type,
                            source_ref = exception_type.source_ref
                        ),
                        yes_branch = handler,
                        no_branch  = exception_handling,
                        source_ref = exception_type.source_ref
                    ),
                ),
                source_ref = exception_type.source_ref
            )

    prelude = (
        StatementPreserveFrameException(
            source_ref = source_ref.atInternal()
        ),
        StatementPublishException(
            source_ref = source_ref.atInternal()
        )
    )

    if exception_handling is None:
        # For Python3, we need not publish at all, if all we do is to revert
        # that immediately. For Python2, the publish may release previously
        # published exception, which has side effects potentially.
        if Utils.python_version < 300:
            exception_handling = StatementsSequence(
                statements = prelude,
                source_ref = source_ref.atInternal()
            )

            public_exc = True
        else:
            public_exc = False
    else:
        public_exc = True

        if Utils.python_version < 300:
            exception_handling.setStatements(
                prelude + exception_handling.getStatements()
            )
        else:
            exception_handling = StatementsSequence(
                statements = prelude + (
                    makeTryFinallyStatement(
                        tried      = exception_handling,
                        final      = StatementRestoreFrameException(
                            source_ref = source_ref.atInternal()
                        ),
                        source_ref = source_ref
                    ),
                ),
                source_ref = source_ref.atInternal()
            )

    no_raise = buildStatementsNode(
        provider   = provider,
        nodes      = node.orelse,
        source_ref = source_ref
    )

    if no_raise is None:
        return StatementTryExcept(
            tried      = tried,
            handling   = exception_handling,
            public_exc = public_exc,
            source_ref = source_ref
        )
    else:
        return makeTryExceptNoRaise(
            provider   = provider,
            temp_scope = provider.allocateTempScope("try_except"),
            handling   = exception_handling,
            tried      = tried,
            public_exc = public_exc,
            no_raise   = no_raise,
            source_ref = source_ref
        )
示例#21
0
def buildTryFinallyNode(provider, build_tried, node, source_ref):

    if Utils.python_version < 300:
        # Prevent "continue" statements in the final blocks
        pushBuildContext("finally")
        final = buildStatementsNode(
            provider   = provider,
            nodes      = node.finalbody,
            source_ref = source_ref
        )
        popBuildContext()

        return StatementTryFinally(
            tried      = build_tried(),
            final      = final,
            public_exc = Utils.python_version >= 300, # TODO: Use below code
            source_ref = source_ref
        )
    else:
        temp_scope = provider.allocateTempScope("try_finally")

        tmp_indicator_var = provider.allocateTempVariable(
            temp_scope = temp_scope,
            name       = "unhandled_indicator"
        )

        pushIndicatorVariable(tmp_indicator_var)

        statements = (
            StatementAssignmentVariable(
                variable_ref = ExpressionTargetTempVariableRef(
                    variable   = tmp_indicator_var.makeReference(
                        provider
                    ),
                    source_ref = source_ref.atInternal()
                ),
                source       = ExpressionConstantRef(
                    constant   = False,
                    source_ref = source_ref
                ),
                source_ref   = source_ref.atInternal()
            ),
            build_tried(),
            StatementAssignmentVariable(
                variable_ref = ExpressionTargetTempVariableRef(
                    variable   = tmp_indicator_var.makeReference(
                        provider
                    ),
                    source_ref = source_ref.atInternal()
                ),
                source       = ExpressionConstantRef(
                    constant   = True,
                    source_ref = source_ref
                ),
                source_ref   = source_ref.atInternal()
            )
        )

        # Prevent "continue" statements in the final blocks
        pushBuildContext("finally")
        final = buildStatementsNode(
            provider   = provider,
            nodes      = node.finalbody,
            source_ref = source_ref
        )
        popBuildContext()

        popIndicatorVariable()

        tried = StatementsSequence(
            statements = mergeStatements(statements, allow_none = True),
            source_ref = source_ref
        )

        prelude = StatementConditional(
            condition = ExpressionComparisonIs(
                left       = ExpressionTempVariableRef(
                    variable   = tmp_indicator_var.makeReference(
                        provider
                    ),
                    source_ref = source_ref.atInternal()
                ),
                right      = ExpressionConstantRef(
                    constant = False,
                    source_ref = source_ref
                ),
                source_ref = source_ref
            ),
            yes_branch = StatementsSequence(
                statements = (
                    StatementPreserveFrameException(
                        source_ref = source_ref.atInternal()
                    ),
                    StatementPublishException(
                        source_ref = source_ref.atInternal()
                    )
                ),
                source_ref = source_ref.atInternal()
            ),
            no_branch  = None,
            source_ref = source_ref.atInternal()
        )

        postlude = (
            StatementConditional(
                condition = ExpressionComparisonIs(
                    left       = ExpressionTempVariableRef(
                        variable   = tmp_indicator_var.makeReference(
                            provider
                        ),
                        source_ref = source_ref.atInternal()
                    ),
                    right      = ExpressionConstantRef(
                        constant = False,
                        source_ref = source_ref
                    ),
                    source_ref = source_ref
                ),
                yes_branch = StatementsSequence(
                    statements = (
                        StatementDelVariable(
                            variable_ref = ExpressionTargetTempVariableRef(
                                variable   = tmp_indicator_var.makeReference(
                                    provider
                                ),
                                source_ref = source_ref.atInternal()
                            ),
                            tolerant   = False,
                            source_ref = source_ref.atInternal()
                        ),
                        StatementReraiseFrameException(
                            source_ref = source_ref.atInternal()
                        ),
                    ),
                    source_ref = source_ref.atInternal()
                ),
                no_branch  = StatementsSequence(
                    statements = (
                        StatementDelVariable(
                            variable_ref = ExpressionTargetTempVariableRef(
                                variable   = tmp_indicator_var.makeReference(
                                    provider
                                ),
                                source_ref = source_ref.atInternal()
                            ),
                            tolerant   = False,
                            source_ref = source_ref.atInternal()
                        ),
                    ),
                    source_ref = source_ref.atInternal()
                ),
                source_ref = source_ref.atInternal()
            ),
        )

        final = StatementsSequence(
            statements = mergeStatements(
                (
                    prelude,
                    makeTryFinallyStatement(
                        tried      = final,
                        final      = postlude,
                        source_ref = source_ref.atInternal()
                    ),
                )
            ),
            source_ref = source_ref.atInternal()
        )

        return StatementTryFinally(
            tried      = tried,
            final      = final,
            public_exc = True,
            source_ref = source_ref
        )
示例#22
0
def _buildForLoopNode(provider, node, sync, source_ref):
    # The for loop is re-formulated according to developer manual. An iterator
    # is created, and looped until it gives StopIteration. The else block is
    # taken if a for loop exits normally, i.e. because of iterator
    # exhaustion. We do this by introducing an indicator variable.

    # We handle async and sync both here, leading to cases, pylint: disable=too-many-locals

    source = buildNode(provider, node.iter, source_ref)

    # Temporary variables, we need one for the iterator, and one for the current
    # value.
    temp_scope = provider.allocateTempScope("for_loop")

    tmp_iter_variable = provider.allocateTempVariable(
        temp_scope = temp_scope,
        name       = "for_iterator"
    )
    tmp_value_variable = provider.allocateTempVariable(
        temp_scope = temp_scope,
        name       = "iter_value"
    )

    else_block = buildStatementsNode(
        provider   = provider,
        nodes      = node.orelse if node.orelse else None,
        source_ref = source_ref
    )

    if else_block is not None:
        tmp_break_indicator = provider.allocateTempVariable(
            temp_scope = temp_scope,
            name       = "break_indicator"
        )

        statements = [
            StatementAssignmentVariable(
                variable_ref = ExpressionTargetTempVariableRef(
                    variable   = tmp_break_indicator,
                    source_ref = source_ref
                ),
                source       = makeConstantRefNode(
                    constant   = True,
                    source_ref = source_ref
                ),
                source_ref   = source_ref
            )
        ]
    else:
        statements = []

    statements.append(
        StatementLoopBreak(
            source_ref = source_ref
        )
    )

    handler_body = makeStatementsSequence(
        statements = statements,
        allow_none = False,
        source_ref = source_ref
    )

    if sync:
        next_node = ExpressionBuiltinNext1(
            value      = ExpressionTempVariableRef(
                variable   = tmp_iter_variable,
                source_ref = source_ref
            ),
            source_ref = source_ref
        )
    else:
        next_node = ExpressionAsyncNext(
            value      = ExpressionTempVariableRef(
                variable   = tmp_iter_variable,
                source_ref = source_ref
            ),
            source_ref = source_ref
        )

    statements = (
        makeTryExceptSingleHandlerNode(
            tried          = StatementAssignmentVariable(
                variable_ref = ExpressionTargetTempVariableRef(
                    variable   = tmp_value_variable,
                    source_ref = source_ref
                ),
                source       = next_node,
                source_ref   = source_ref
            ),
            exception_name = "StopIteration" if sync else "StopAsyncIteration",
            handler_body   = handler_body,
            source_ref     = source_ref
        ),
        buildAssignmentStatements(
            provider   = provider,
            node       = node.target,
            source     = ExpressionTempVariableRef(
                variable   = tmp_value_variable,
                source_ref = source_ref
            ),
            source_ref = source_ref
        )
    )

    pushBuildContext("loop_body")
    statements += (
        buildStatementsNode(
            provider   = provider,
            nodes      = node.body,
            source_ref = source_ref
        ),
    )
    popBuildContext()

    loop_body = makeStatementsSequence(
        statements = statements,
        allow_none = True,
        source_ref = source_ref
    )

    cleanup_statements = [
        StatementReleaseVariable(
            variable   = tmp_value_variable,
            source_ref = source_ref
        ),
        StatementReleaseVariable(
            variable   = tmp_iter_variable,
            source_ref = source_ref
        )
    ]

    if else_block is not None:
        statements = [
            StatementAssignmentVariable(
                variable_ref = ExpressionTargetTempVariableRef(
                    variable   = tmp_break_indicator,
                    source_ref = source_ref
                ),
                source       = makeConstantRefNode(
                    constant   = False,
                    source_ref = source_ref
                ),
                source_ref   = source_ref
            )
        ]
    else:
        statements = []

    if sync:
        iter_source = ExpressionBuiltinIter1(
            value      = source,
            source_ref = source.getSourceReference()
        )
    else:
        iter_source = ExpressionAsyncIter(
            value      = source,
            source_ref = source.getSourceReference()
        )

    statements += [
        # First create the iterator and store it.
        StatementAssignmentVariable(
            variable_ref = ExpressionTargetTempVariableRef(
                variable   = tmp_iter_variable,
                source_ref = source_ref
            ),
            source       = iter_source,
            source_ref   = source_ref
        ),
        makeTryFinallyStatement(
            provider   = provider,
            tried      = StatementLoop(
                body       = loop_body,
                source_ref = source_ref
            ),
            final      = StatementsSequence(
                statements = cleanup_statements,
                source_ref = source_ref
            ),
            source_ref = source_ref
        )
    ]

    if else_block is not None:
        statements += [
            StatementConditional(
                condition  = ExpressionComparisonIs(
                    left       = ExpressionTempVariableRef(
                        variable   = tmp_break_indicator,
                        source_ref = source_ref
                    ),
                    right      = makeConstantRefNode(
                        constant   = True,
                        source_ref = source_ref
                    ),
                    source_ref = source_ref
                ),
                yes_branch = else_block,
                no_branch  = None,
                source_ref = source_ref
            )
        ]

        statements = (
            makeTryFinallyStatement(
                provider   = provider,
                tried      = statements,
                final      = StatementReleaseVariable(
                    variable   = tmp_break_indicator,
                    source_ref = source_ref
                ),
                source_ref = source_ref
            ),
        )

    return makeStatementsSequenceFromStatements(
        *statements
    )
示例#23
0
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))
示例#24
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)
示例#25
0
def buildTryFinallyNode(provider, build_tried, node, source_ref):

    if Utils.python_version < 300:
        # Prevent "continue" statements in the final blocks
        pushBuildContext("finally")
        final = buildStatementsNode(provider=provider,
                                    nodes=node.finalbody,
                                    source_ref=source_ref)
        popBuildContext()

        return StatementTryFinally(
            tried=build_tried(),
            final=final,
            public_exc=Utils.python_version >= 300,  # TODO: Use below code
            source_ref=source_ref)
    else:
        temp_scope = provider.allocateTempScope("try_finally")

        tmp_indicator_var = provider.allocateTempVariable(
            temp_scope=temp_scope, name="unhandled_indicator")

        # This makes sure, we set the indicator variables for "break",
        # "continue" and "return" exits as well to true, so we can
        # know if an exception occurred or not.
        pushIndicatorVariable(tmp_indicator_var)

        statements = (StatementAssignmentVariable(
            variable_ref=ExpressionTargetTempVariableRef(
                variable=tmp_indicator_var,
                source_ref=source_ref.atInternal()),
            source=ExpressionConstantRef(constant=False,
                                         source_ref=source_ref),
            source_ref=source_ref.atInternal()), build_tried(),
                      StatementAssignmentVariable(
                          variable_ref=ExpressionTargetTempVariableRef(
                              variable=tmp_indicator_var,
                              source_ref=source_ref.atInternal()),
                          source=ExpressionConstantRef(
                              constant=True,
                              source_ref=source_ref.atLineNumber(99)),
                          source_ref=source_ref.atInternal()))

        # Prevent "continue" statements in the final blocks, these have to
        # become "SyntaxError".
        pushBuildContext("finally")
        final = buildStatementsNode(provider=provider,
                                    nodes=node.finalbody,
                                    source_ref=source_ref)
        popBuildContext()

        popIndicatorVariable()

        tried = StatementsSequence(statements=mergeStatements(statements,
                                                              allow_none=True),
                                   source_ref=source_ref)

        prelude = StatementConditional(
            condition=ExpressionComparisonIs(
                left=ExpressionTempVariableRef(
                    variable=tmp_indicator_var,
                    source_ref=source_ref.atInternal()),
                right=ExpressionConstantRef(constant=False,
                                            source_ref=source_ref),
                source_ref=source_ref),
            yes_branch=StatementsSequence(
                statements=(StatementPreserveFrameException(
                    source_ref=source_ref.atInternal()),
                            StatementPublishException(
                                source_ref=source_ref.atInternal())),
                source_ref=source_ref.atInternal()),
            no_branch=None,
            source_ref=source_ref.atInternal())

        postlude = (StatementConditional(
            condition=ExpressionComparisonIs(
                left=ExpressionTempVariableRef(
                    variable=tmp_indicator_var,
                    source_ref=source_ref.atInternal()),
                right=ExpressionConstantRef(constant=False,
                                            source_ref=source_ref),
                source_ref=source_ref),
            yes_branch=StatementsSequence(statements=(
                StatementReleaseVariable(variable=tmp_indicator_var,
                                         tolerant=False,
                                         source_ref=source_ref.atInternal()),
                StatementReraiseFrameException(
                    source_ref=source_ref.atInternal()),
            ),
                                          source_ref=source_ref.atInternal()),
            no_branch=StatementsSequence(statements=(StatementReleaseVariable(
                variable=tmp_indicator_var,
                tolerant=False,
                source_ref=source_ref.atInternal()), ),
                                         source_ref=source_ref.atInternal()),
            source_ref=source_ref.atInternal()), )

        final = StatementsSequence(statements=mergeStatements((
            prelude,
            makeTryFinallyStatement(tried=final,
                                    final=postlude,
                                    source_ref=source_ref.atInternal()),
        )),
                                   source_ref=source_ref.atInternal())

        return StatementTryFinally(tried=tried,
                                   final=final,
                                   public_exc=True,
                                   source_ref=source_ref)
示例#26
0
def _buildClassNode3(provider, node, source_ref):
    # Many variables, due to the huge re-formulation that is going on here,
    # which just has the complexity, pylint: disable=R0914,R0915

    # This function is the Python3 special case with special re-formulation as
    # according to developer manual.
    class_statement_nodes, class_doc = extractDocFromBody(node)

    # We need a scope for the temporary variables, and they might be closured.
    temp_scope = provider.allocateTempScope(name="class_creation",
                                            allow_closure=True)

    tmp_bases = provider.allocateTempVariable(temp_scope=temp_scope,
                                              name="bases")
    tmp_class_decl_dict = provider.allocateTempVariable(temp_scope=temp_scope,
                                                        name="class_decl_dict")
    tmp_metaclass = provider.allocateTempVariable(temp_scope=temp_scope,
                                                  name="metaclass")
    tmp_prepared = provider.allocateTempVariable(temp_scope=temp_scope,
                                                 name="prepared")

    class_creation_function = ExpressionClassBody(provider=provider,
                                                  name=node.name,
                                                  doc=class_doc,
                                                  flags=set(),
                                                  source_ref=source_ref)

    if python_version >= 340 and False:  # TODO: Temporarily reverted:
        tmp_class = class_creation_function.allocateTempVariable(
            temp_scope=None, name="__class__")

        class_target_variable_ref = ExpressionTargetTempVariableRef(
            variable=tmp_class, source_ref=source_ref)
        class_variable_ref = ExpressionTempVariableRef(variable=tmp_class,
                                                       source_ref=source_ref)
    else:
        class_variable = class_creation_function.getVariableForAssignment(
            "__class__")

        class_target_variable_ref = ExpressionTargetVariableRef(
            variable_name="__class__",
            variable=class_variable,
            source_ref=source_ref)
        class_variable_ref = ExpressionVariableRef(variable_name="__class__",
                                                   variable=class_variable,
                                                   source_ref=source_ref)

    code_object = CodeObjectSpec(co_name=node.name,
                                 co_kind="Class",
                                 co_varnames=(),
                                 co_argcount=0,
                                 co_kwonlyargcount=0,
                                 co_has_starlist=False,
                                 co_has_stardict=False)

    body = buildFrameNode(provider=class_creation_function,
                          nodes=class_statement_nodes,
                          code_object=code_object,
                          source_ref=source_ref)

    source_ref_orig = source_ref

    if body is not None:
        # The frame guard has nothing to tell its line number to.
        body.source_ref = source_ref

    module_variable = class_creation_function.getVariableForAssignment(
        "__module__")

    statements = [
        StatementSetLocals(new_locals=ExpressionTempVariableRef(
            variable=tmp_prepared, source_ref=source_ref),
                           source_ref=source_ref),
        StatementAssignmentVariable(
            variable_ref=ExpressionTargetVariableRef(
                variable_name="__module__",
                variable=module_variable,
                source_ref=source_ref),
            source=makeConstantRefNode(
                constant=provider.getParentModule().getFullName(),
                source_ref=source_ref,
                user_provided=True),
            source_ref=source_ref)
    ]

    if class_doc is not None:
        doc_variable = class_creation_function.getVariableForAssignment(
            "__doc__")

        statements.append(
            StatementAssignmentVariable(
                variable_ref=ExpressionTargetVariableRef(
                    variable_name="__doc__",
                    variable=doc_variable,
                    source_ref=source_ref),
                source=makeConstantRefNode(constant=class_doc,
                                           source_ref=source_ref,
                                           user_provided=True),
                source_ref=source_ref))

    # The "__qualname__" attribute is new in Python 3.3.
    if python_version >= 330:
        qualname = class_creation_function.getFunctionQualname()
        qualname_variable = class_creation_function.getVariableForAssignment(
            "__qualname__")

        if python_version < 340:
            qualname_ref = makeConstantRefNode(constant=qualname,
                                               source_ref=source_ref,
                                               user_provided=True)
        else:
            qualname_ref = ExpressionFunctionQualnameRef(
                function_body=class_creation_function,
                source_ref=source_ref,
            )

        statements.append(
            StatementAssignmentVariable(
                variable_ref=ExpressionTargetVariableRef(
                    variable_name="__qualname__",
                    variable=qualname_variable,
                    source_ref=source_ref),
                source=qualname_ref,
                source_ref=source_ref))

        if python_version >= 340:
            qualname_assign = statements[-1]

    if python_version >= 360 and \
       class_creation_function.needsAnnotationsDictionary():
        annotations_variable = class_creation_function.getVariableForAssignment(
            "__annotations__")

        statements.append(
            StatementAssignmentVariable(
                variable_ref=ExpressionTargetVariableRef(
                    variable_name="__annotations__",
                    variable=annotations_variable,
                    source_ref=source_ref),
                source=makeConstantRefNode(constant={},
                                           source_ref=source_ref,
                                           user_provided=True),
                source_ref=source_ref))

    statements.append(body)

    statements += [
        StatementAssignmentVariable(
            variable_ref=class_target_variable_ref,
            source=ExpressionCall(
                called=ExpressionTempVariableRef(variable=tmp_metaclass,
                                                 source_ref=source_ref),
                args=makeSequenceCreationOrConstant(
                    sequence_kind="tuple",
                    elements=(makeConstantRefNode(constant=node.name,
                                                  source_ref=source_ref,
                                                  user_provided=True),
                              ExpressionTempVariableRef(variable=tmp_bases,
                                                        source_ref=source_ref),
                              ExpressionBuiltinLocals(source_ref=source_ref)),
                    source_ref=source_ref),
                kw=ExpressionTempVariableRef(variable=tmp_class_decl_dict,
                                             source_ref=source_ref),
                source_ref=source_ref),
            source_ref=source_ref),
        StatementReturn(expression=class_variable_ref, source_ref=source_ref)
    ]

    body = makeStatementsSequence(statements=statements,
                                  allow_none=True,
                                  source_ref=source_ref)

    # The class body is basically a function that implicitly, at the end
    # returns its locals and cannot have other return statements contained.

    class_creation_function.setBody(body)

    class_creation_function.registerProvidedVariable(tmp_bases)
    class_creation_function.registerProvidedVariable(tmp_class_decl_dict)
    class_creation_function.registerProvidedVariable(tmp_metaclass)
    class_creation_function.registerProvidedVariable(tmp_prepared)

    # The class body is basically a function that implicitly, at the end
    # returns its created class and cannot have other return statements
    # contained.

    decorated_body = ExpressionFunctionCall(
        function=ExpressionFunctionCreation(function_ref=ExpressionFunctionRef(
            function_body=class_creation_function, source_ref=source_ref),
                                            code_object=code_object,
                                            defaults=(),
                                            kw_defaults=None,
                                            annotations=None,
                                            source_ref=source_ref),
        values=(),
        source_ref=source_ref)

    for decorator in buildNodeList(provider, reversed(node.decorator_list),
                                   source_ref):
        decorated_body = ExpressionCallNoKeywords(
            called=decorator,
            args=ExpressionMakeTuple(elements=(decorated_body, ),
                                     source_ref=source_ref),
            source_ref=decorator.getSourceReference())

    statements = (
        StatementAssignmentVariable(
            variable_ref=ExpressionTargetTempVariableRef(
                variable=tmp_bases, source_ref=source_ref),
            source=makeSequenceCreationOrConstant(sequence_kind="tuple",
                                                  elements=buildNodeList(
                                                      provider, node.bases,
                                                      source_ref),
                                                  source_ref=source_ref),
            source_ref=source_ref),
        StatementAssignmentVariable(
            variable_ref=ExpressionTargetTempVariableRef(
                variable=tmp_class_decl_dict, source_ref=source_ref),
            source=makeDictCreationOrConstant(keys=[
                makeConstantRefNode(constant=keyword.arg,
                                    source_ref=source_ref,
                                    user_provided=True)
                for keyword in node.keywords
            ],
                                              values=[
                                                  buildNode(
                                                      provider, keyword.value,
                                                      source_ref)
                                                  for keyword in node.keywords
                                              ],
                                              source_ref=source_ref),
            source_ref=source_ref),
        StatementAssignmentVariable(
            variable_ref=ExpressionTargetTempVariableRef(
                variable=tmp_metaclass, source_ref=source_ref),
            source=ExpressionSelectMetaclass(metaclass=ExpressionConditional(
                condition=ExpressionComparisonIn(
                    left=makeConstantRefNode(constant="metaclass",
                                             source_ref=source_ref,
                                             user_provided=True),
                    right=ExpressionTempVariableRef(
                        variable=tmp_class_decl_dict, source_ref=source_ref),
                    source_ref=source_ref),
                expression_yes=ExpressionDictOperationGet(
                    dict_arg=ExpressionTempVariableRef(
                        variable=tmp_class_decl_dict, source_ref=source_ref),
                    key=makeConstantRefNode(constant="metaclass",
                                            source_ref=source_ref,
                                            user_provided=True),
                    source_ref=source_ref),
                expression_no=ExpressionConditional(
                    condition=ExpressionTempVariableRef(variable=tmp_bases,
                                                        source_ref=source_ref),
                    expression_no=ExpressionBuiltinRef(builtin_name="type",
                                                       source_ref=source_ref),
                    expression_yes=ExpressionBuiltinType1(
                        value=ExpressionSubscriptLookup(
                            subscribed=ExpressionTempVariableRef(
                                variable=tmp_bases, source_ref=source_ref),
                            subscript=makeConstantRefNode(
                                constant=0,
                                source_ref=source_ref,
                                user_provided=True),
                            source_ref=source_ref),
                        source_ref=source_ref),
                    source_ref=source_ref),
                source_ref=source_ref),
                                             bases=ExpressionTempVariableRef(
                                                 variable=tmp_bases,
                                                 source_ref=source_ref),
                                             source_ref=source_ref),
            source_ref=source_ref_orig),
        StatementConditional(
            condition=ExpressionComparisonIn(
                left=makeConstantRefNode(constant="metaclass",
                                         source_ref=source_ref,
                                         user_provided=True),
                right=ExpressionTempVariableRef(variable=tmp_class_decl_dict,
                                                source_ref=source_ref),
                source_ref=source_ref),
            no_branch=None,
            yes_branch=makeStatementsSequenceFromStatement(
                statement=StatementDictOperationRemove(
                    dict_arg=ExpressionTempVariableRef(
                        variable=tmp_class_decl_dict, source_ref=source_ref),
                    key=makeConstantRefNode(constant="metaclass",
                                            source_ref=source_ref,
                                            user_provided=True),
                    source_ref=source_ref)),
            source_ref=source_ref),
        StatementAssignmentVariable(
            variable_ref=ExpressionTargetTempVariableRef(
                variable=tmp_prepared, source_ref=source_ref),
            source=ExpressionConditional(
                condition=ExpressionBuiltinHasattr(  # pylint: disable=E1120,E1123
                    object=ExpressionTempVariableRef(variable=tmp_metaclass,
                                                     source_ref=source_ref),
                    name=makeConstantRefNode(constant="__prepare__",
                                             source_ref=source_ref,
                                             user_provided=True),
                    source_ref=source_ref),
                expression_no=makeConstantRefNode(constant={},
                                                  source_ref=source_ref,
                                                  user_provided=True),
                expression_yes=ExpressionCall(
                    called=ExpressionAttributeLookup(
                        source=ExpressionTempVariableRef(
                            variable=tmp_metaclass, source_ref=source_ref),
                        attribute_name="__prepare__",
                        source_ref=source_ref),
                    args=ExpressionMakeTuple(
                        elements=(makeConstantRefNode(constant=node.name,
                                                      source_ref=source_ref,
                                                      user_provided=True),
                                  ExpressionTempVariableRef(
                                      variable=tmp_bases,
                                      source_ref=source_ref)),
                        source_ref=source_ref),
                    kw=ExpressionTempVariableRef(variable=tmp_class_decl_dict,
                                                 source_ref=source_ref),
                    source_ref=source_ref),
                source_ref=source_ref),
            source_ref=source_ref),
        StatementAssignmentVariable(variable_ref=ExpressionTargetVariableRef(
            variable_name=node.name, source_ref=source_ref),
                                    source=decorated_body,
                                    source_ref=source_ref),
    )

    if python_version >= 340:
        class_assign = statements[-1]

        class_creation_function.qualname_setup = class_assign, qualname_assign

    final = (StatementReleaseVariable(variable=tmp_bases,
                                      source_ref=source_ref),
             StatementReleaseVariable(variable=tmp_class_decl_dict,
                                      source_ref=source_ref),
             StatementReleaseVariable(variable=tmp_metaclass,
                                      source_ref=source_ref),
             StatementReleaseVariable(variable=tmp_prepared,
                                      source_ref=source_ref))

    return makeTryFinallyStatement(provider=provider,
                                   tried=statements,
                                   final=final,
                                   source_ref=source_ref)
示例#27
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

    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=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)

    if not provider.isCompiledPythonModule():
        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 = 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),
        StatementConditional(
            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,
                ),
                StatementConditional(
                    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(
                StatementConditional(
                    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),
        # 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=source_variable,
                                     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),
        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=StatementConditional(
                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=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),
            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)
def buildTryExceptionNode(provider, node, source_ref):
    # Try/except nodes. Re-formulated as described in the developer
    # manual. Exception handlers made the assignment to variables explicit. Same
    # for the "del" as done for Python3. Also catches always work a tuple of
    # exception types and hides away that they may be built or not.

    # Many variables and branches, due to the re-formulation that is going on
    # here, which just has the complexity, pylint: disable=too-many-branches,too-many-locals

    tried = buildStatementsNode(provider=provider,
                                nodes=node.body,
                                source_ref=source_ref)

    handlers = []

    for handler in node.handlers:
        exception_expression, exception_assign, exception_block = (
            handler.type, handler.name, handler.body)

        if exception_assign is None:
            statements = [
                buildStatementsNode(provider=provider,
                                    nodes=exception_block,
                                    source_ref=source_ref)
            ]
        elif python_version < 300:
            statements = [
                buildAssignmentStatements(
                    provider=provider,
                    node=exception_assign,
                    source=ExpressionCaughtExceptionValueRef(
                        source_ref=source_ref.atInternal()),
                    source_ref=source_ref.atInternal()),
                buildStatementsNode(provider=provider,
                                    nodes=exception_block,
                                    source_ref=source_ref)
            ]
        else:
            # Python3 requires temporary assignment of exception assignment.
            target_info = decodeAssignTarget(
                provider=provider,
                node=exception_assign,
                source_ref=source_ref,
            )

            kind, detail = target_info

            assert kind == "Name", kind
            kind = "Name_Exception"

            statements = [
                buildAssignmentStatements(
                    provider=provider,
                    node=exception_assign,
                    source=ExpressionCaughtExceptionValueRef(
                        source_ref=source_ref.atInternal()),
                    source_ref=source_ref.atInternal()),
                makeTryFinallyStatement(provider=provider,
                                        tried=buildStatementsNode(
                                            provider=provider,
                                            nodes=exception_block,
                                            source_ref=source_ref),
                                        final=buildDeleteStatementFromDecoded(
                                            kind=kind,
                                            detail=detail,
                                            source_ref=source_ref),
                                        source_ref=source_ref)
            ]

        handler_body = makeStatementsSequence(statements=statements,
                                              allow_none=True,
                                              source_ref=source_ref)

        exception_types = buildNode(provider=provider,
                                    node=exception_expression,
                                    source_ref=source_ref,
                                    allow_none=True)

        # The exception types should be a tuple, so as to be most general.
        if exception_types is None:
            if handler is not node.handlers[-1]:
                raiseSyntaxError(
                    "default 'except:' must be last",
                    source_ref.atLineNumber(handler.lineno).atColumnNumber(
                        handler.col_offset))

        handlers.append((
            exception_types,
            handler_body,
        ))

    # Re-raise by default
    exception_handling = makeReraiseExceptionStatement(source_ref=source_ref)

    for exception_type, handler in reversed(handlers):
        if exception_type is None:
            # A default handler was given, so use that indeed.
            exception_handling = handler
        else:
            exception_handling = StatementsSequence(
                statements=(StatementConditional(
                    condition=ExpressionComparisonExceptionMatch(
                        left=ExpressionCaughtExceptionTypeRef(
                            source_ref=exception_type.source_ref),
                        right=exception_type,
                        source_ref=exception_type.source_ref),
                    yes_branch=handler,
                    no_branch=exception_handling,
                    source_ref=exception_type.source_ref), ),
                source_ref=exception_type.source_ref)

    if exception_handling is None:
        # For Python3, we need not publish at all, if all we do is to revert
        # that immediately. For Python2, the publish may release previously
        # published exception, which has side effects potentially.
        if python_version < 300:
            exception_handling = StatementsSequence(
                statements=(
                    StatementPreserveFrameException(
                        preserver_id=0,  # unused with Python2
                        source_ref=source_ref.atInternal()),
                    StatementPublishException(
                        source_ref=source_ref.atInternal()),
                ),
                source_ref=source_ref.atInternal())
    else:
        if python_version < 300:
            exception_handling.setStatements((
                StatementPreserveFrameException(
                    preserver_id=0,  # unused with Python2
                    source_ref=source_ref.atInternal()),
                StatementPublishException(source_ref=source_ref.atInternal()),
            ) + exception_handling.getStatements())
        else:
            preserver_id = provider.allocatePreserverId()

            exception_handling = makeStatementsSequenceFromStatements(
                StatementPreserveFrameException(
                    preserver_id=preserver_id,
                    source_ref=source_ref.atInternal()),
                StatementPublishException(source_ref=source_ref.atInternal()),
                makeTryFinallyStatement(
                    provider=provider,
                    tried=exception_handling,
                    final=StatementRestoreFrameException(
                        preserver_id=preserver_id,
                        source_ref=source_ref.atInternal()),
                    source_ref=source_ref),
            )

    no_raise = buildStatementsNode(provider=provider,
                                   nodes=node.orelse,
                                   source_ref=source_ref)

    if no_raise is None:
        if tried is None:
            return None

        return StatementTry(tried=tried,
                            except_handler=exception_handling,
                            break_handler=None,
                            continue_handler=None,
                            return_handler=None,
                            source_ref=source_ref)
    else:
        if tried is None:
            return no_raise

        return makeTryExceptNoRaise(
            provider=provider,
            temp_scope=provider.allocateTempScope("try_except"),
            handling=exception_handling,
            tried=tried,
            no_raise=no_raise,
            source_ref=source_ref)
示例#29
0
def _buildWithNode(provider, context_expr, assign_target, body, body_lineno,
                   sync, source_ref):
    # Many details, pylint: disable=too-many-locals
    with_source = buildNode(provider, context_expr, source_ref)

    if Options.isFullCompat():
        source_ref = with_source.getCompatibleSourceReference()

    temp_scope = provider.allocateTempScope("with")

    tmp_source_variable = provider.allocateTempVariable(
        temp_scope = temp_scope,
        name       = "source"
    )
    tmp_exit_variable = provider.allocateTempVariable(
        temp_scope = temp_scope,
        name       = "exit"
    )
    tmp_enter_variable = provider.allocateTempVariable(
        temp_scope = temp_scope,
        name       = "enter"
    )
    tmp_indicator_variable = provider.allocateTempVariable(
        temp_scope = temp_scope,
        name       = "indicator"
    )

    statements = (
        buildAssignmentStatements(
            provider   = provider,
            node       = assign_target,
            allow_none = True,
            source     = ExpressionTempVariableRef(
                variable   = tmp_enter_variable,
                source_ref = source_ref
            ),
            source_ref = source_ref
        ),
        body
    )

    with_body = makeStatementsSequence(
        statements = statements,
        allow_none = True,
        source_ref = source_ref
    )

    if Options.isFullCompat():
        if body:
            deepest = body

            while deepest.getVisitableNodes():
                deepest = deepest.getVisitableNodes()[-1]

            body_lineno = deepest.getCompatibleSourceReference().getLineNumber()

        with_exit_source_ref = source_ref.atLineNumber(body_lineno)
    else:
        with_exit_source_ref = source_ref

    # The "__enter__" and "__exit__" were normal attribute lookups under
    # CPython2.6, but that changed with CPython2.7.
    if python_version < 270:
        attribute_lookup_class = ExpressionAttributeLookup
    else:
        attribute_lookup_class = ExpressionAttributeLookupSpecial

    enter_value = ExpressionCallEmpty(
        called     = attribute_lookup_class(
            source         = ExpressionTempVariableRef(
                variable   = tmp_source_variable,
                source_ref = source_ref
            ),
            attribute_name = "__enter__" if sync else "__aenter__",
            source_ref     = source_ref
        ),
        source_ref = source_ref
    )

    exit_value_exception = ExpressionCallNoKeywords(
        called     = ExpressionTempVariableRef(
            variable   = tmp_exit_variable,
            source_ref = with_exit_source_ref
        ),
        args       = ExpressionMakeTuple(
            elements   = (
                ExpressionCaughtExceptionTypeRef(
                    source_ref = with_exit_source_ref
                ),
                ExpressionCaughtExceptionValueRef(
                    source_ref = with_exit_source_ref
                ),
                ExpressionCaughtExceptionTracebackRef(
                    source_ref = source_ref
                ),
            ),
            source_ref = source_ref
        ),
        source_ref = with_exit_source_ref
    )

    exit_value_no_exception = ExpressionCallNoKeywords(
        called     = ExpressionTempVariableRef(
            variable   = tmp_exit_variable,
            source_ref = source_ref
        ),
        args       = makeConstantRefNode(
            constant   = (None, None, None),
            source_ref = source_ref
        ),
        source_ref = with_exit_source_ref
    )

    # For "async with", await the entered value and exit value must be awaited.
    if not sync:
        enter_value = ExpressionAsyncWait(
            expression = enter_value,
            source_ref = source_ref
        )
        exit_value_exception = ExpressionAsyncWait(
            expression = exit_value_exception,
            source_ref = source_ref
        )
        exit_value_no_exception = ExpressionAsyncWait(
            expression = exit_value_no_exception,
            source_ref = source_ref
        )

    statements = [
        # First assign the with context to a temporary variable.
        StatementAssignmentVariable(
            variable   = tmp_source_variable,
            source     = with_source,
            source_ref = source_ref
        )
    ]

    attribute_assignments = [
        # Next, assign "__enter__" and "__exit__" attributes to temporary
        # variables.
        StatementAssignmentVariable(
            variable   = tmp_exit_variable,
            source     = attribute_lookup_class(
                source         = ExpressionTempVariableRef(
                    variable   = tmp_source_variable,
                    source_ref = source_ref
                ),
                attribute_name = "__exit__" if sync else "__aexit__",
                source_ref     = source_ref
            ),
            source_ref = source_ref
        ),
        StatementAssignmentVariable(
            variable   = tmp_enter_variable,
            source     = enter_value,
            source_ref = source_ref
        )
    ]

    if python_version >= 360 and sync:
        attribute_assignments.reverse()

    statements += attribute_assignments

    statements.append(
        StatementAssignmentVariable(
            variable   = tmp_indicator_variable,
            source     = makeConstantRefNode(
                constant   = True,
                source_ref = source_ref
            ),
            source_ref = source_ref
        )
    )

    statements += [
        makeTryFinallyStatement(
            provider   = provider,
            tried      = makeTryExceptSingleHandlerNodeWithPublish(
                provider       = provider,
                tried          = with_body,
                exception_name = "BaseException",
                handler_body   = StatementsSequence(
                    statements = (
                        # Prevents final block from calling __exit__ as
                        # well.
                        StatementAssignmentVariable(
                            variable   = tmp_indicator_variable,
                            source     = makeConstantRefNode(
                                constant   = False,
                                source_ref = source_ref
                            ),
                            source_ref = source_ref
                        ),
                        makeConditionalStatement(
                            condition  = exit_value_exception,
                            no_branch  = makeReraiseExceptionStatement(
                                source_ref = with_exit_source_ref
                            ),
                            yes_branch = None,
                            source_ref = with_exit_source_ref
                        ),
                    ),
                    source_ref = source_ref
                ),
                public_exc     = python_version >= 270,
                source_ref     = source_ref
            ),
            final      = StatementConditional(
                condition  = ExpressionComparisonIs(
                    left       = ExpressionTempVariableRef(
                        variable   = tmp_indicator_variable,
                        source_ref = source_ref
                    ),
                    right      = makeConstantRefNode(
                        constant   = True,
                        source_ref = source_ref
                    ),
                    source_ref = source_ref
                ),
                yes_branch = makeStatementsSequenceFromStatement(
                    statement = StatementExpressionOnly(
                        expression = exit_value_no_exception,
                        source_ref = source_ref
                    )
                ),
                no_branch  = None,
                source_ref = source_ref
            ),
            source_ref = source_ref
        )
    ]

    return makeTryFinallyStatement(
        provider   = provider,
        tried      = statements,
        final      = (
            StatementReleaseVariable(
                variable   = tmp_source_variable,
                source_ref = with_exit_source_ref
            ),
            StatementReleaseVariable(
                variable   = tmp_enter_variable,
                source_ref = with_exit_source_ref
            ),
            StatementReleaseVariable(
                variable   = tmp_exit_variable,
                source_ref = with_exit_source_ref
            ),
            StatementReleaseVariable(
                variable   = tmp_indicator_variable,
                source_ref = with_exit_source_ref
            ),
        ),
        source_ref = source_ref
    )
示例#30
0
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)