Ejemplo n.º 1
0
def buildOrNode(provider, values, source_ref):
    result = values[-1]
    del values[-1]

    while True:
        keeper_variable = provider.allocateTempKeeperVariable()

        tmp_assign = ExpressionAssignmentTempKeeper(
            variable=keeper_variable.makeReference(provider),
            source=values[-1],
            source_ref=source_ref)
        del values[-1]

        result = ExpressionConditional(
            condition=tmp_assign,
            yes_expression=ExpressionTempKeeperRef(
                variable=keeper_variable.makeReference(provider),
                source_ref=source_ref),
            no_expression=result,
            source_ref=source_ref)

        if not values:
            break

    return result
def buildComparisonNode(provider, node, source_ref):
    from nuitka.nodes.NodeMakingHelpers import makeComparisonNode

    assert len(node.comparators) == len(node.ops)

    # Comparisons are re-formulated as described in the developer manual. When
    # having multiple compators, things require assignment expressions and
    # references of them to work properly. Then they can become normal "and"
    # code.

    # The operands are split out
    left = buildNode(provider, node.left, source_ref)
    rights = [
        buildNode(provider, comparator, source_ref)
        for comparator in node.comparators
    ]

    # Only the first comparison has as left operands as the real thing, the
    # others must reference the previous comparison right one temp variable ref.
    result = []

    # For PyLint to like it, this will hold the previous one, normally.
    keeper_variable = None

    for comparator, right in zip(node.ops, rights):
        if result:
            # Now we know it's not the only one, so we change the "left" to be a
            # reference to the previously saved right side.
            left = ExpressionTempKeeperRef(
                variable=keeper_variable.makeReference(provider),
                source_ref=source_ref)

            keeper_variable = None

        if right is not rights[-1]:
            # Now we know it's not the last one, so we ought to preseve the
            # "right" so it can be referenced by the next part that will
            # come. We do it by assining it to a temp variable to be shared with
            # the next part.
            keeper_variable = provider.allocateTempKeeperVariable()

            right = ExpressionAssignmentTempKeeper(
                variable=keeper_variable.makeReference(provider),
                source=right,
                source_ref=source_ref)

        comparator = getKind(comparator)

        result.append(
            makeComparisonNode(left=left,
                               right=right,
                               comparator=comparator,
                               source_ref=source_ref))

    assert keeper_variable is None

    return buildAndNode(provider=provider,
                        values=result,
                        source_ref=source_ref)
def buildAndNode(provider, values, source_ref):
    result = values[-1]
    del values[-1]

    while values:
        keeper_variable = provider.getTempKeeperVariable()

        tmp_assign = ExpressionAssignmentTempKeeper(
            variable=keeper_variable.makeReference(provider),
            source=values[-1],
            source_ref=source_ref)
        del values[-1]

        result = ExpressionConditional(
            condition=tmp_assign,
            no_expression=ExpressionTempKeeperRef(
                variable=keeper_variable.makeReference(provider),
                source_ref=source_ref),
            yes_expression=result,
            source_ref=source_ref)

    return result
Ejemplo n.º 4
0
def wrapEvalGlobalsAndLocals(provider, globals, locals, exec_mode, source_ref):
    if globals is not None:
        global_keeper_variable = provider.getTempKeeperVariable()
        tmp_global_assign = ExpressionAssignmentTempKeeper(
            variable=global_keeper_variable.makeReference(provider),
            source=globals,
            source_ref=source_ref)

        globals_wrap = ExpressionConditional(
            condition=ExpressionComparisonIs(left=tmp_global_assign,
                                             right=ExpressionConstantRef(
                                                 constant=None,
                                                 source_ref=source_ref),
                                             source_ref=source_ref),
            no_expression=ExpressionTempKeeperRef(
                variable=global_keeper_variable.makeReference(provider),
                source_ref=source_ref),
            yes_expression=ExpressionBuiltinGlobals(source_ref=source_ref),
            source_ref=source_ref)
    else:
        globals_wrap = ExpressionBuiltinGlobals(source_ref=source_ref)

    if locals is not None:
        local_keeper_variable = provider.getTempKeeperVariable()
        tmp_local_assign = ExpressionAssignmentTempKeeper(
            variable=local_keeper_variable.makeReference(provider),
            source=locals,
            source_ref=source_ref)

        if exec_mode:
            locals_fallback = ExpressionBuiltinLocals(source_ref=source_ref)
        else:
            locals_fallback = ExpressionConditional(
                condition=ExpressionComparisonIs(left=ExpressionTempKeeperRef(
                    variable=global_keeper_variable.makeReference(provider),
                    source_ref=source_ref),
                                                 right=ExpressionConstantRef(
                                                     constant=None,
                                                     source_ref=source_ref),
                                                 source_ref=source_ref),
                no_expression=ExpressionTempKeeperRef(
                    variable=global_keeper_variable.makeReference(provider),
                    source_ref=source_ref),
                yes_expression=ExpressionBuiltinLocals(source_ref=source_ref),
                source_ref=source_ref)

        locals_wrap = ExpressionConditional(
            condition=ExpressionComparisonIs(left=tmp_local_assign,
                                             right=ExpressionConstantRef(
                                                 constant=None,
                                                 source_ref=source_ref),
                                             source_ref=source_ref),
            no_expression=ExpressionTempKeeperRef(
                variable=local_keeper_variable.makeReference(provider),
                source_ref=source_ref),
            yes_expression=locals_fallback,
            source_ref=source_ref)
    else:
        if globals is None:
            locals_wrap = ExpressionBuiltinLocals(source_ref=source_ref)
        else:
            locals_wrap = ExpressionTempKeeperRef(
                variable=global_keeper_variable.makeReference(provider),
                source_ref=source_ref)

    return globals_wrap, locals_wrap
Ejemplo n.º 5
0
def wrapEvalGlobalsAndLocals(provider, globals_node, locals_node, exec_mode,
                             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.
    """

    if globals_node is not None:
        global_keeper_variable = provider.allocateTempKeeperVariable()
        tmp_global_assign = ExpressionAssignmentTempKeeper(
            variable=global_keeper_variable.makeReference(provider),
            source=globals_node,
            source_ref=source_ref)

        globals_wrap = ExpressionConditional(
            condition=ExpressionComparisonIs(left=tmp_global_assign,
                                             right=ExpressionConstantRef(
                                                 constant=None,
                                                 source_ref=source_ref),
                                             source_ref=source_ref),
            no_expression=ExpressionTempKeeperRef(
                variable=global_keeper_variable.makeReference(provider),
                source_ref=source_ref),
            yes_expression=ExpressionBuiltinGlobals(source_ref=source_ref),
            source_ref=source_ref)
    else:
        globals_wrap = ExpressionBuiltinGlobals(source_ref=source_ref)

    if locals_node is not None:
        local_keeper_variable = provider.allocateTempKeeperVariable()
        tmp_local_assign = ExpressionAssignmentTempKeeper(
            variable=local_keeper_variable.makeReference(provider),
            source=locals_node,
            source_ref=source_ref)

        if exec_mode:
            locals_fallback = ExpressionBuiltinLocals(source_ref=source_ref)
        else:
            locals_fallback = ExpressionConditional(
                condition=ExpressionComparisonIs(left=ExpressionTempKeeperRef(
                    variable=global_keeper_variable.makeReference(provider),
                    source_ref=source_ref),
                                                 right=ExpressionConstantRef(
                                                     constant=None,
                                                     source_ref=source_ref),
                                                 source_ref=source_ref),
                no_expression=ExpressionTempKeeperRef(
                    variable=global_keeper_variable.makeReference(provider),
                    source_ref=source_ref),
                yes_expression=ExpressionBuiltinLocals(source_ref=source_ref),
                source_ref=source_ref)

        locals_wrap = ExpressionConditional(
            condition=ExpressionComparisonIs(left=tmp_local_assign,
                                             right=ExpressionConstantRef(
                                                 constant=None,
                                                 source_ref=source_ref),
                                             source_ref=source_ref),
            no_expression=ExpressionTempKeeperRef(
                variable=local_keeper_variable.makeReference(provider),
                source_ref=source_ref),
            yes_expression=locals_fallback,
            source_ref=source_ref)
    else:
        if globals_node is None:
            locals_wrap = ExpressionBuiltinLocals(source_ref=source_ref)
        else:
            locals_wrap = ExpressionTempKeeperRef(
                variable=global_keeper_variable.makeReference(provider),
                source_ref=source_ref)

    return globals_wrap, locals_wrap