Example #1
0
def buildConditionalExpressionNode(provider, node, source_ref):
    return ExpressionConditional(
        condition=buildNode(provider, node.test, source_ref),
        expression_yes=buildNode(provider, node.body, source_ref),
        expression_no=buildNode(provider, node.orelse, source_ref),
        source_ref=source_ref,
    )
def buildOrNode(provider, values, source_ref):
    result = values[-1]
    del values[-1]

    while True:
        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,
            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 buildOrNode(provider, values, source_ref):
    values = list(values)

    result = values[-1]
    del values[-1]

    temp_scope = None
    count = 1

    while values:
        if temp_scope is None:
            temp_scope = provider.allocateTempScope(name="or")

        keeper_variable = provider.allocateTempVariable(temp_scope=temp_scope,
                                                        name="value_%d" %
                                                        count)
        count += 1

        tried = StatementAssignmentVariable(
            variable_ref=ExpressionTargetTempVariableRef(
                variable=keeper_variable, source_ref=source_ref),
            source=values[-1],
            source_ref=source_ref,
        )

        result = makeTryFinallyExpression(
            tried=tried,
            final=None,
            expression=ExpressionConditional(
                condition=ExpressionTempVariableRef(variable=keeper_variable,
                                                    source_ref=source_ref),
                yes_expression=ExpressionTempVariableRef(
                    variable=keeper_variable, source_ref=source_ref),
                no_expression=makeTryFinallyExpression(
                    expression=result,
                    final=None,
                    tried=StatementDelVariable(
                        variable_ref=ExpressionTargetTempVariableRef(
                            variable=keeper_variable, source_ref=source_ref),
                        tolerant=False,
                        source_ref=source_ref,
                    ),
                    source_ref=source_ref),
                source_ref=source_ref),
            source_ref=source_ref)

        wrapTryFinallyLater(
            result,
            StatementDelVariable(
                variable_ref=ExpressionTargetTempVariableRef(
                    variable=keeper_variable, source_ref=source_ref),
                tolerant=True,
                source_ref=source_ref,
            ))

        del values[-1]

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

    while values:
        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,
            no_expression=ExpressionTempKeeperRef(
                variable=keeper_variable.makeReference(provider),
                source_ref=source_ref),
            yes_expression=result,
            source_ref=source_ref)

    return result
Example #5
0
def _buildClassNode2(provider, node, source_ref):
    # This function is the Python2 special case with special re-formulation as
    # according to developer manual, and it's very detailed, pylint: disable=R0914
    class_statement_nodes, class_doc = extractDocFromBody(node)

    function_body = ExpressionClassBody(provider=provider,
                                        name=node.name,
                                        doc=class_doc,
                                        flags=set(),
                                        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=function_body,
                          nodes=class_statement_nodes,
                          code_object=code_object,
                          source_ref=source_ref)
    if body is not None:
        # The frame guard has nothing to tell its line number to.
        body.source_ref = source_ref.atInternal()

    # The class body is basically a function that implicitly, at the end
    # returns its locals and cannot have other return statements contained, and
    # starts out with a variables "__module__" and potentially "__doc__" set.
    statements = [
        StatementAssignmentVariable(
            variable_ref=ExpressionTargetVariableRef(
                variable_name="__module__", source_ref=source_ref),
            source=makeConstantRefNode(
                constant=provider.getParentModule().getFullName(),
                source_ref=source_ref,
                user_provided=True),
            source_ref=source_ref.atInternal())
    ]

    if class_doc is not None:
        statements.append(
            StatementAssignmentVariable(
                variable_ref=ExpressionTargetVariableRef(
                    variable_name="__doc__", source_ref=source_ref),
                source=makeConstantRefNode(constant=class_doc,
                                           source_ref=source_ref,
                                           user_provided=True),
                source_ref=source_ref.atInternal()))

    statements += [
        body,
        StatementReturn(
            expression=ExpressionBuiltinLocals(source_ref=source_ref),
            source_ref=source_ref.atInternal())
    ]

    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.

    function_body.setBody(body)

    temp_scope = provider.allocateTempScope("class_creation")

    tmp_bases = provider.allocateTempVariable(temp_scope, "bases")
    tmp_class_dict = provider.allocateTempVariable(temp_scope, "class_dict")
    tmp_metaclass = provider.allocateTempVariable(temp_scope, "metaclass")
    tmp_class = provider.allocateTempVariable(temp_scope, "class")

    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_dict, source_ref=source_ref),
            source=ExpressionFunctionCall(function=ExpressionFunctionCreation(
                function_ref=ExpressionFunctionRef(function_body=function_body,
                                                   source_ref=source_ref),
                code_object=None,
                defaults=(),
                kw_defaults=None,
                annotations=None,
                source_ref=source_ref),
                                          values=(),
                                          source_ref=source_ref),
            source_ref=source_ref),
        StatementAssignmentVariable(
            variable_ref=ExpressionTargetTempVariableRef(
                variable=tmp_metaclass, source_ref=source_ref),
            source=ExpressionConditional(
                condition=ExpressionComparisonIn(
                    left=makeConstantRefNode(constant="__metaclass__",
                                             source_ref=source_ref,
                                             user_provided=True),
                    right=ExpressionTempVariableRef(variable=tmp_class_dict,
                                                    source_ref=source_ref),
                    source_ref=source_ref),
                expression_yes=ExpressionDictOperationGet(
                    dict_arg=ExpressionTempVariableRef(variable=tmp_class_dict,
                                                       source_ref=source_ref),
                    key=makeConstantRefNode(constant="__metaclass__",
                                            source_ref=source_ref,
                                            user_provided=True),
                    source_ref=source_ref),
                expression_no=ExpressionSelectMetaclass(
                    metaclass=None,
                    bases=ExpressionTempVariableRef(variable=tmp_bases,
                                                    source_ref=source_ref),
                    source_ref=source_ref),
                source_ref=source_ref),
            source_ref=source_ref),
        StatementAssignmentVariable(
            variable_ref=ExpressionTargetTempVariableRef(
                variable=tmp_class, source_ref=source_ref),
            source=ExpressionCallNoKeywords(
                called=ExpressionTempVariableRef(variable=tmp_metaclass,
                                                 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),
                              ExpressionTempVariableRef(
                                  variable=tmp_class_dict,
                                  source_ref=source_ref)),
                    source_ref=source_ref),
                source_ref=source_ref),
            source_ref=source_ref),
    ]

    for decorator in buildNodeList(provider, reversed(node.decorator_list),
                                   source_ref):
        statements.append(
            StatementAssignmentVariable(
                variable_ref=ExpressionTargetTempVariableRef(
                    variable=tmp_class, source_ref=source_ref),
                source=ExpressionCallNoKeywords(
                    called=decorator,
                    args=ExpressionMakeTuple(
                        elements=(ExpressionTempVariableRef(
                            variable=tmp_class, source_ref=source_ref), ),
                        source_ref=source_ref),
                    source_ref=decorator.getSourceReference()),
                source_ref=decorator.getSourceReference()))

    statements.append(
        StatementAssignmentVariable(
            variable_ref=ExpressionTargetVariableRef(variable_name=node.name,
                                                     source_ref=source_ref),
            source=ExpressionTempVariableRef(variable=tmp_class,
                                             source_ref=source_ref),
            source_ref=source_ref))

    final = (StatementReleaseVariable(variable=tmp_class,
                                      source_ref=source_ref),
             StatementReleaseVariable(variable=tmp_bases,
                                      source_ref=source_ref),
             StatementReleaseVariable(variable=tmp_class_dict,
                                      source_ref=source_ref),
             StatementReleaseVariable(variable=tmp_metaclass,
                                      source_ref=source_ref))

    return makeTryFinallyStatement(provider=function_body,
                                   tried=statements,
                                   final=final,
                                   source_ref=source_ref)
    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=source_variable, tolerant=True, source_ref=source_ref), ))

        strip_choice = makeConstantRefNode(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=makeExpressionBuiltinRef(builtin_name="bytes",
                                                   source_ref=source_ref),
                    source_ref=source_ref,
                ),
                expression_yes=makeConstantRefNode(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=source_variable,
            source=makeExpressionCall(
                called=ExpressionAttributeLookup(
                    source=ExpressionTempVariableRef(variable=source_variable,
                                                     source_ref=source_ref),
                    attribute_name="strip",
                    source_ref=source_ref,
                ),
                args=strip_choice,  # This is a tuple
                kw=None,
                source_ref=source_ref,
            ),
            source_ref=source_ref,
        )

        acceptable_builtin_types = [
            ExpressionBuiltinAnonymousRef(builtin_name="code",
                                          source_ref=source_ref)
        ]

        if python_version >= 270:
            acceptable_builtin_types.append(
                makeExpressionBuiltinRef(builtin_name="memoryview",
                                         source_ref=source_ref))

        statements = (
            StatementAssignmentVariable(variable=source_variable,
                                        source=source,
                                        source_ref=source_ref),
            makeStatementConditional(
                condition=ExpressionOperationNOT(
                    operand=ExpressionBuiltinIsinstance(
                        instance=ExpressionTempVariableRef(
                            variable=source_variable, source_ref=source_ref),
                        classes=makeSequenceCreationOrConstant(
                            sequence_kind="tuple",
                            elements=acceptable_builtin_types,
                            source_ref=source_ref,
                        ),
                        source_ref=source_ref,
                    ),
                    source_ref=source_ref,
                ),
                yes_branch=string_fixup,
                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
Example #7
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),
        makeStatementConditional(
            condition=ExpressionComparisonIs(
                left=ExpressionTempVariableRef(variable=locals_keeper_variable,
                                               source_ref=source_ref),
                right=ExpressionConstantNoneRef(source_ref=source_ref),
                source_ref=source_ref,
            ),
            yes_branch=StatementAssignmentVariable(
                variable=locals_keeper_variable,
                source=locals_default,
                source_ref=source_ref,
            ),
            no_branch=None,
            source_ref=source_ref,
        ),
        makeStatementConditional(
            condition=ExpressionComparisonIs(
                left=ExpressionTempVariableRef(
                    variable=globals_keeper_variable, source_ref=source_ref),
                right=ExpressionConstantNoneRef(source_ref=source_ref),
                source_ref=source_ref,
            ),
            yes_branch=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),
    )
Example #8
0
def computeBuiltinCall(call_node, called):
    # There is some dispatching for how to output various types of changes,
    # with lots of cases, pylint: disable=R0912

    builtin_name = called.getBuiltinName()

    if builtin_name in _dispatch_dict:
        new_node = _dispatch_dict[builtin_name](call_node)

        # Lets just have this contract to return "None" when no change is meant
        # to be done.
        assert new_node is not call_node
        if new_node is None:
            return call_node, None, None

        # For traces, we are going to ignore side effects, and output traces
        # only based on the basis of it.
        inspect_node = new_node
        if inspect_node.isExpressionSideEffects():
            inspect_node = inspect_node.getExpression()

        if inspect_node.isExpressionBuiltinImport():
            tags    = "new_import"
            message = """\
Replaced dynamic __import__ %s with static module import.""" % (
                inspect_node.kind,
            )
        elif inspect_node.isExpressionBuiltin() or \
             inspect_node.isStatementExec():
            tags = "new_builtin"
            message = "Replaced call to built-in '%s' with built-in call '%s'." % (
                builtin_name,
                inspect_node.kind,
            )
        elif inspect_node.isExpressionRaiseException():
            tags = "new_raise"
            message = """\
Replaced call to built-in '%s' with exception raising call.""" % (
                inspect_node.kind,
            )
        elif inspect_node.isExpressionOperationUnary():
            tags = "new_expression"
            message = """\
Replaced call to built-in '%s' with unary operation '%s'.""" % (
                inspect_node.kind,
                inspect_node.getOperator()
            )
        elif inspect_node.isExpressionCall():
            tags = "new_expression"
            message = """\
Replaced call to built-in '%s' with call.""" % (
                inspect_node.kind,
            )
        elif inspect_node.isExpressionOutlineBody():
            tags = "new_expression"
            message = """\
Replaced call to built-in '%s' with outlined call.""" % builtin_name
        else:

            assert False, (builtin_name, "->", inspect_node)

        # TODO: One day, this should be enabled by default and call either the
        # original built-in or the optimized above one. That should be done,
        # once we can eliminate the condition for most cases.
        if False and isDebug() and not shallMakeModule() and builtin_name:
            source_ref = called.getSourceReference()

            new_node = ExpressionConditional(
                condition      = ExpressionComparisonIs(
                    left       = ExpressionBuiltinRef(
                        builtin_name = builtin_name,
                        source_ref   = source_ref
                    ),
                    right      = ExpressionBuiltinOriginalRef(
                        builtin_name = builtin_name,
                        source_ref   = source_ref
                    ),
                    source_ref = source_ref
                ),
                expression_yes = new_node,
                expression_no  = makeRaiseExceptionReplacementExpression(
                    exception_type  = "RuntimeError",
                    exception_value = "Built-in '%s' cannot be replaced." % (
                        builtin_name
                    ),
                    expression      = call_node
                ),
                source_ref     = source_ref
            )

        assert tags != ""

        return new_node, tags, message
    else:
        if False and isDebug() and builtin_name not in _builtin_white_list:
            warning(
                "Not handling built-in '%s', consider support." % builtin_name
            )

        return call_node, None, None
Example #9
0
def computeBuiltinCall(call_node, called):
    builtin_name = called.getBuiltinName()

    if builtin_name in _dispatch_dict:
        new_node = _dispatch_dict[builtin_name](call_node)

        # Lets just have this contract to return "None" when no change is meant
        # to be done.
        assert new_node is not call_node
        if new_node is None:
            return call_node, None, None

        # For traces, we are going to ignore side effects, and output traces
        # only based on the basis of it.
        inspect_node = new_node
        if inspect_node.isExpressionSideEffects():
            inspect_node = inspect_node.getExpression()

        if inspect_node.isExpressionBuiltinImport():
            tags    = "new_import"
            message = """\
Replaced dynamic __import__ %s with static module import.""" % (
                inspect_node.kind,
            )
        elif inspect_node.isExpressionBuiltin() or \
             inspect_node.isStatementExec():
            tags = "new_builtin"
            message = "Replaced call to builtin %s with builtin call %s." % (
                builtin_name,
                inspect_node.kind,
            )
        elif inspect_node.isExpressionRaiseException():
            tags = "new_raise"
            message = """\
Replaced call to builtin %s with exception raising call.""" % (
                inspect_node.kind,
            )
        elif inspect_node.isExpressionOperationUnary():
            tags = "new_expression"
            message = """\
Replaced call to builtin %s with unary operation %s.""" % (
                inspect_node.kind,
                inspect_node.getOperator()
            )
        elif inspect_node.isExpressionCall():
            tags = "new_expression"
            message = """\
Replaced call to builtin %s with call.""" % (
                inspect_node.kind,
            )
        elif inspect_node.isExpressionTryFinally():
            tags = "new_expression"
            message = """\
Replaced call to builtin %s with try/finally guarded call.""" % (
                inspect_node.getExpression().kind,
            )
        else:

            assert False, ( builtin_name, "->", inspect_node )

        # TODO: One day, this should be enabled by default and call either the
        # original built-in or the optimized above one. That should be done,
        # once we can eliminate the condition for most cases.
        if False and isDebug() and not shallMakeModule() and builtin_name:
            from nuitka.nodes.NodeMakingHelpers import \
              makeRaiseExceptionReplacementExpression

            source_ref = called.getSourceReference()

            new_node = ExpressionConditional(
                condition      = ExpressionComparisonIs(
                    left  = ExpressionBuiltinRef(
                        builtin_name = builtin_name,
                        source_ref   = source_ref
                    ),
                    right = ExpressionBuiltinOriginalRef(
                        builtin_name = builtin_name,
                        source_ref   = source_ref
                    ),
                    source_ref   = source_ref
                ),
                yes_expression = new_node,
                no_expression  = makeRaiseExceptionReplacementExpression(
                    exception_type  = "RuntimeError",
                    exception_value = "Builtin '%s' was overloaded'" % (
                        builtin_name
                    ),
                    expression      = call_node
                ),
                source_ref     = source_ref
            )

        assert tags != ""

        return new_node, tags, message
    else:
        # TODO: Consider giving warnings, whitelisted potentially
        return call_node, None, None
Example #10
0
def buildClassNode2(provider, node, source_ref):
    # This function is the Python2 special case with special re-formulation as
    # according to developer manual, and it's very detailed, pylint: disable=too-many-locals
    class_statement_nodes, class_doc = extractDocFromBody(node)

    function_body = ExpressionClassBody(provider=provider,
                                        name=node.name,
                                        doc=class_doc,
                                        source_ref=source_ref)

    parent_module = provider.getParentModule()

    code_object = CodeObjectSpec(
        co_name=node.name,
        co_kind="Class",
        co_varnames=(),
        co_argcount=0,
        co_posonlyargcount=0,
        co_kwonlyargcount=0,
        co_has_starlist=False,
        co_has_stardict=False,
        co_filename=parent_module.getRunTimeFilename(),
        co_lineno=source_ref.getLineNumber(),
        future_spec=parent_module.getFutureSpec(),
    )

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

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

    locals_scope = function_body.getLocalsScope()

    # The class body is basically a function that implicitly, at the end
    # returns its locals and cannot have other return statements contained, and
    # starts out with a variables "__module__" and potentially "__doc__" set.
    statements = [
        StatementSetLocalsDictionary(locals_scope=locals_scope,
                                     source_ref=source_ref),
        StatementAssignmentVariableName(
            provider=function_body,
            variable_name="__module__",
            source=ExpressionModuleAttributeNameRef(
                variable=provider.getParentModule().getVariableForReference(
                    "__name__"),
                source_ref=source_ref,
            ),
            source_ref=source_ref.atInternal(),
        ),
    ]

    if class_doc is not None:
        statements.append(
            StatementAssignmentVariableName(
                provider=function_body,
                variable_name="__doc__",
                source=makeConstantRefNode(constant=class_doc,
                                           source_ref=source_ref,
                                           user_provided=True),
                source_ref=source_ref.atInternal(),
            ))

    statements += (
        body,
        StatementReturn(
            expression=ExpressionBuiltinLocalsRef(locals_scope=locals_scope,
                                                  source_ref=source_ref),
            source_ref=source_ref,
        ),
    )

    body = makeStatementsSequenceFromStatement(
        statement=makeTryFinallyStatement(
            provider=function_body,
            tried=mergeStatements(statements, True),
            final=StatementReleaseLocals(locals_scope=locals_scope,
                                         source_ref=source_ref),
            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.

    function_body.setBody(body)

    temp_scope = provider.allocateTempScope("class_creation")

    tmp_bases = provider.allocateTempVariable(temp_scope, "bases")
    tmp_class_dict = provider.allocateTempVariable(temp_scope, "class_dict")
    tmp_metaclass = provider.allocateTempVariable(temp_scope, "metaclass")
    tmp_class = provider.allocateTempVariable(temp_scope, "class")

    select_metaclass = ExpressionOutlineBody(provider=provider,
                                             name="select_metaclass",
                                             body=None,
                                             source_ref=source_ref)

    if node.bases:
        tmp_base = select_metaclass.allocateTempVariable(temp_scope=None,
                                                         name="base")

        statements = (
            StatementAssignmentVariable(
                variable=tmp_base,
                source=ExpressionSubscriptLookup(
                    expression=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,
            ),
            makeTryFinallyStatement(
                provider,
                tried=StatementTry(
                    tried=makeStatementsSequenceFromStatement(
                        statement=StatementReturn(
                            expression=ExpressionAttributeLookup(
                                expression=ExpressionTempVariableRef(
                                    variable=tmp_base, source_ref=source_ref),
                                attribute_name="__class__",
                                source_ref=source_ref,
                            ),
                            source_ref=source_ref,
                        )),
                    except_handler=makeStatementsSequenceFromStatement(
                        statement=StatementReturn(
                            expression=ExpressionBuiltinType1(
                                value=ExpressionTempVariableRef(
                                    variable=tmp_base, source_ref=source_ref),
                                source_ref=source_ref,
                            ),
                            source_ref=source_ref,
                        )),
                    break_handler=None,
                    continue_handler=None,
                    return_handler=None,
                    source_ref=source_ref,
                ),
                final=StatementReleaseVariable(variable=tmp_base,
                                               source_ref=source_ref),
                source_ref=source_ref,
                public_exc=False,
            ),
        )
    else:
        statements = (
            StatementTry(
                tried=makeStatementsSequenceFromStatement(
                    statement=StatementReturn(
                        # TODO: Should avoid checking __builtins__ for this.
                        expression=ExpressionVariableNameRef(
                            variable_name="__metaclass__",
                            provider=parent_module,
                            source_ref=source_ref,
                        ),
                        source_ref=source_ref,
                    )),
                except_handler=makeStatementsSequenceFromStatement(
                    statement=StatementReturn(
                        expression=ExpressionBuiltinAnonymousRef(
                            builtin_name="classobj", source_ref=source_ref),
                        source_ref=source_ref,
                    )),
                break_handler=None,
                continue_handler=None,
                return_handler=None,
                source_ref=source_ref,
            ), )

    select_metaclass.setBody(
        makeStatementsSequence(statements=statements,
                               allow_none=False,
                               source_ref=source_ref))

    statements = [
        StatementAssignmentVariable(
            variable=tmp_bases,
            source=makeSequenceCreationOrConstant(
                sequence_kind="tuple",
                elements=buildNodeList(provider=provider,
                                       nodes=node.bases,
                                       source_ref=source_ref),
                source_ref=source_ref,
            ),
            source_ref=source_ref,
        ),
        StatementAssignmentVariable(variable=tmp_class_dict,
                                    source=function_body,
                                    source_ref=source_ref),
        StatementAssignmentVariable(
            variable=tmp_metaclass,
            source=ExpressionConditional(
                condition=ExpressionDictOperationIn(
                    key=makeConstantRefNode(
                        constant="__metaclass__",
                        source_ref=source_ref,
                        user_provided=True,
                    ),
                    dict_arg=ExpressionTempVariableRef(variable=tmp_class_dict,
                                                       source_ref=source_ref),
                    source_ref=source_ref,
                ),
                expression_yes=ExpressionDictOperationGet(
                    dict_arg=ExpressionTempVariableRef(variable=tmp_class_dict,
                                                       source_ref=source_ref),
                    key=makeConstantRefNode(
                        constant="__metaclass__",
                        source_ref=source_ref,
                        user_provided=True,
                    ),
                    source_ref=source_ref,
                ),
                expression_no=select_metaclass,
                source_ref=source_ref,
            ),
            source_ref=source_ref,
        ),
        StatementAssignmentVariable(
            variable=tmp_class,
            source=makeExpressionCall(
                called=ExpressionTempVariableRef(variable=tmp_metaclass,
                                                 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),
                        ExpressionTempVariableRef(variable=tmp_class_dict,
                                                  source_ref=source_ref),
                    ),
                    source_ref=source_ref,
                ),
                kw=None,
                source_ref=source_ref,
            ),
            source_ref=source_ref,
        ),
    ]

    for decorator in buildNodeList(provider, reversed(node.decorator_list),
                                   source_ref):
        statements.append(
            StatementAssignmentVariable(
                variable=tmp_class,
                source=makeExpressionCall(
                    called=decorator,
                    args=ExpressionMakeTuple(
                        elements=(ExpressionTempVariableRef(
                            variable=tmp_class, source_ref=source_ref), ),
                        source_ref=source_ref,
                    ),
                    kw=None,
                    source_ref=decorator.getSourceReference(),
                ),
                source_ref=decorator.getSourceReference(),
            ))

    statements.append(
        StatementAssignmentVariableName(
            provider=provider,
            variable_name=mangleName(node.name, provider),
            source=ExpressionTempVariableRef(variable=tmp_class,
                                             source_ref=source_ref),
            source_ref=source_ref,
        ))

    final = (
        StatementReleaseVariable(variable=tmp_class, source_ref=source_ref),
        StatementReleaseVariable(variable=tmp_bases, source_ref=source_ref),
        StatementReleaseVariable(variable=tmp_class_dict,
                                 source_ref=source_ref),
        StatementReleaseVariable(variable=tmp_metaclass,
                                 source_ref=source_ref),
    )

    return makeTryFinallyStatement(provider=function_body,
                                   tried=statements,
                                   final=final,
                                   source_ref=source_ref)
Example #11
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
Example #12
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

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

    # The result will be a temp block that holds the temporary variables.
    result = StatementTempBlock(source_ref=source_ref)

    tmp_bases = result.getTempVariable("bases")
    tmp_class_decl_dict = result.getTempVariable("class_decl_dict")
    tmp_metaclass = result.getTempVariable("metaclass")
    tmp_prepared = result.getTempVariable("prepared")

    class_creation_function = ExpressionFunctionBody(
        provider=provider,
        is_class=True,
        parameters=make_class_parameters,
        name=node.name,
        doc=class_doc,
        source_ref=source_ref)

    # Hack:
    class_creation_function.parent = provider

    body = buildStatementsNode(provider=class_creation_function,
                               nodes=class_statements,
                               frame=True,
                               source_ref=source_ref)

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

    statements = [
        StatementSetLocals(new_locals=ExpressionTempVariableRef(
            variable=tmp_prepared.makeReference(result),
            source_ref=source_ref),
                           source_ref=source_ref.atInternal()),
        StatementAssignmentVariable(
            variable_ref=ExpressionTargetVariableRef(
                variable_name="__module__", source_ref=source_ref),
            source=ExpressionConstantRef(
                constant=provider.getParentModule().getName(),
                source_ref=source_ref),
            source_ref=source_ref.atInternal())
    ]

    if class_doc is not None:
        statements.append(
            StatementAssignmentVariable(
                variable_ref=ExpressionTargetVariableRef(
                    variable_name="__doc__", source_ref=source_ref),
                source=ExpressionConstantRef(constant=class_doc,
                                             source_ref=source_ref),
                source_ref=source_ref.atInternal()))

    if Utils.python_version >= 330:
        if provider.isExpressionFunctionBody():
            qualname = provider.getName() + ".<locals>." + node.name
        else:
            qualname = node.name

        statements.append(
            StatementAssignmentVariable(
                variable_ref=ExpressionTargetVariableRef(
                    variable_name="__qualname__", source_ref=source_ref),
                source=ExpressionConstantRef(constant=qualname,
                                             source_ref=source_ref),
                source_ref=source_ref.atInternal()))

    statements += [
        body,
        StatementAssignmentVariable(
            variable_ref=ExpressionTargetVariableRef(variable_name="__class__",
                                                     source_ref=source_ref),
            source=ExpressionCall(
                called=ExpressionTempVariableRef(
                    variable=tmp_metaclass.makeReference(result),
                    source_ref=source_ref),
                args=ExpressionMakeTuple(
                    elements=(ExpressionConstantRef(constant=node.name,
                                                    source_ref=source_ref),
                              ExpressionTempVariableRef(
                                  variable=tmp_bases.makeReference(result),
                                  source_ref=source_ref),
                              ExpressionBuiltinLocals(source_ref=source_ref)),
                    source_ref=source_ref),
                kw=ExpressionTempVariableRef(
                    variable=tmp_class_decl_dict.makeReference(result),
                    source_ref=source_ref),
                source_ref=source_ref),
            source_ref=source_ref.atInternal()),
        StatementReturn(expression=ExpressionVariableRef(
            variable_name="__class__", source_ref=source_ref),
                        source_ref=source_ref.atInternal())
    ]

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

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

    class_creation_function.setBody(body)

    # The class body is basically a function that implicitely, 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),
                                            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.makeReference(result),
                source_ref=source_ref),
            source=ExpressionMakeTuple(elements=buildNodeList(
                provider, node.bases, source_ref),
                                       source_ref=source_ref),
            source_ref=source_ref),
        StatementAssignmentVariable(
            variable_ref=ExpressionTargetTempVariableRef(
                variable=tmp_class_decl_dict.makeReference(result),
                source_ref=source_ref),
            source=ExpressionMakeDict(pairs=[
                ExpressionKeyValuePair(
                    key=ExpressionConstantRef(constant=keyword.arg,
                                              source_ref=source_ref),
                    value=buildNode(provider, keyword.value, source_ref),
                    source_ref=source_ref) for keyword in node.keywords
            ],
                                      source_ref=source_ref),
            source_ref=source_ref),
        StatementAssignmentVariable(
            variable_ref=ExpressionTargetTempVariableRef(
                variable=tmp_metaclass.makeReference(result),
                source_ref=source_ref),
            source=ExpressionSelectMetaclass(
                metaclass=ExpressionConditional(
                    condition=ExpressionComparison(
                        comparator="In",
                        left=ExpressionConstantRef(constant="metaclass",
                                                   source_ref=source_ref),
                        right=ExpressionTempVariableRef(
                            variable=tmp_class_decl_dict.makeReference(result),
                            source_ref=source_ref),
                        source_ref=source_ref),
                    yes_expression=ExpressionDictOperationGet(
                        dicte=ExpressionTempVariableRef(
                            variable=tmp_class_decl_dict.makeReference(result),
                            source_ref=source_ref),
                        key=ExpressionConstantRef(constant="metaclass",
                                                  source_ref=source_ref),
                        source_ref=source_ref),
                    no_expression=ExpressionConditional(
                        condition=ExpressionTempVariableRef(
                            variable=tmp_bases.makeReference(result),
                            source_ref=source_ref),
                        no_expression=ExpressionBuiltinRef(
                            builtin_name="type", source_ref=source_ref),
                        yes_expression=ExpressionBuiltinType1(
                            value=ExpressionSubscriptLookup(
                                expression=ExpressionTempVariableRef(
                                    variable=tmp_bases.makeReference(result),
                                    source_ref=source_ref),
                                subscript=ExpressionConstantRef(
                                    constant=0, source_ref=source_ref),
                                source_ref=source_ref),
                            source_ref=source_ref),
                        source_ref=source_ref),
                    source_ref=source_ref),
                bases=ExpressionTempVariableRef(
                    variable=tmp_bases.makeReference(result),
                    source_ref=source_ref),
                source_ref=source_ref),
            source_ref=source_ref),
        StatementConditional(
            condition=ExpressionComparison(
                comparator="In",
                left=ExpressionConstantRef(constant="metaclass",
                                           source_ref=source_ref),
                right=ExpressionTempVariableRef(
                    variable=tmp_class_decl_dict.makeReference(result),
                    source_ref=source_ref),
                source_ref=source_ref),
            no_branch=None,
            yes_branch=makeStatementsSequenceFromStatement(
                statement=StatementDictOperationRemove(
                    dicte=ExpressionTempVariableRef(
                        variable=tmp_class_decl_dict.makeReference(result),
                        source_ref=source_ref),
                    key=ExpressionConstantRef(constant="metaclass",
                                              source_ref=source_ref),
                    source_ref=source_ref)),
            source_ref=source_ref),
        StatementAssignmentVariable(
            variable_ref=ExpressionTargetTempVariableRef(
                variable=tmp_prepared.makeReference(result),
                source_ref=source_ref),
            source=ExpressionConditional(
                condition=ExpressionBuiltinHasattr(
                    object=ExpressionTempVariableRef(
                        variable=tmp_metaclass.makeReference(result),
                        source_ref=source_ref),
                    name=ExpressionConstantRef(constant="__prepare__",
                                               source_ref=source_ref),
                    source_ref=source_ref),
                no_expression=ExpressionConstantRef(constant={},
                                                    source_ref=source_ref),
                yes_expression=ExpressionCall(
                    called=ExpressionAttributeLookup(
                        expression=ExpressionTempVariableRef(
                            variable=tmp_metaclass.makeReference(result),
                            source_ref=source_ref),
                        attribute_name="__prepare__",
                        source_ref=source_ref),
                    args=ExpressionMakeTuple(
                        elements=(ExpressionConstantRef(constant=node.name,
                                                        source_ref=source_ref),
                                  ExpressionTempVariableRef(
                                      variable=tmp_bases.makeReference(result),
                                      source_ref=source_ref)),
                        source_ref=source_ref),
                    kw=ExpressionTempVariableRef(
                        variable=tmp_class_decl_dict.makeReference(result),
                        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)
    ]

    result.setBody(
        StatementsSequence(statements=statements, source_ref=source_ref))

    return result
Example #13
0
def _buildClassNode2(provider, node, source_ref):
    class_statements, class_doc = extractDocFromBody(node)

    # This function is the Python3 special case with special re-formulation as according
    # to developer manual.

    # The result will be a temp block that holds the temporary variables.
    result = StatementTempBlock(source_ref=source_ref)

    tmp_bases = result.getTempVariable("bases")
    tmp_class_dict = result.getTempVariable("class_dict")
    tmp_metaclass = result.getTempVariable("metaclass")
    tmp_class = result.getTempVariable("class")

    class_creation_function = ExpressionFunctionBody(
        provider=provider,
        is_class=True,
        parameters=make_class_parameters,
        name=node.name,
        doc=class_doc,
        source_ref=source_ref)

    body = buildStatementsNode(provider=class_creation_function,
                               nodes=class_statements,
                               frame=True,
                               source_ref=source_ref)

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

    # The class body is basically a function that implicitely, at the end returns its
    # locals and cannot have other return statements contained, and starts out with a
    # variables "__module__" and potentially "__doc__" set.
    statements = [
        StatementAssignmentVariable(
            variable_ref=ExpressionTargetVariableRef(
                variable_name="__module__", source_ref=source_ref),
            source=ExpressionConstantRef(
                constant=provider.getParentModule().getName(),
                source_ref=source_ref),
            source_ref=source_ref.atInternal())
    ]

    if class_doc is not None:
        statements.append(
            StatementAssignmentVariable(
                variable_ref=ExpressionTargetVariableRef(
                    variable_name="__doc__", source_ref=source_ref),
                source=ExpressionConstantRef(constant=class_doc,
                                             source_ref=source_ref),
                source_ref=source_ref.atInternal()))

    statements += [
        body,
        StatementReturn(
            expression=ExpressionBuiltinLocals(source_ref=source_ref),
            source_ref=source_ref.atInternal())
    ]

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

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

    class_creation_function.setBody(body)

    statements = [
        StatementAssignmentVariable(
            variable_ref=ExpressionTargetTempVariableRef(
                variable=tmp_bases.makeReference(result),
                source_ref=source_ref),
            source=ExpressionMakeTuple(elements=buildNodeList(
                provider, node.bases, source_ref),
                                       source_ref=source_ref),
            source_ref=source_ref),
        StatementAssignmentVariable(
            variable_ref=ExpressionTargetTempVariableRef(
                variable=tmp_class_dict.makeReference(result),
                source_ref=source_ref),
            source=ExpressionFunctionCall(function=ExpressionFunctionCreation(
                function_ref=ExpressionFunctionRef(
                    function_body=class_creation_function,
                    source_ref=source_ref),
                defaults=(),
                kw_defaults=None,
                annotations=None,
                source_ref=source_ref),
                                          values=(),
                                          source_ref=source_ref),
            source_ref=source_ref),
        StatementAssignmentVariable(
            variable_ref=ExpressionTargetTempVariableRef(
                variable=tmp_metaclass.makeReference(result),
                source_ref=source_ref),
            source=ExpressionConditional(
                condition=ExpressionComparison(
                    comparator="In",
                    left=ExpressionConstantRef(constant="__metaclass__",
                                               source_ref=source_ref),
                    right=ExpressionTempVariableRef(
                        variable=tmp_class_dict.makeReference(result),
                        source_ref=source_ref),
                    source_ref=source_ref),
                yes_expression=ExpressionDictOperationGet(
                    dicte=ExpressionTempVariableRef(
                        variable=tmp_class_dict.makeReference(result),
                        source_ref=source_ref),
                    key=ExpressionConstantRef(constant="__metaclass__",
                                              source_ref=source_ref),
                    source_ref=source_ref),
                no_expression=ExpressionSelectMetaclass(
                    metaclass=None,
                    bases=ExpressionTempVariableRef(
                        variable=tmp_bases.makeReference(result),
                        source_ref=source_ref),
                    source_ref=source_ref),
                source_ref=source_ref),
            source_ref=source_ref),
        StatementAssignmentVariable(
            variable_ref=ExpressionTargetTempVariableRef(
                variable=tmp_class.makeReference(result),
                source_ref=source_ref),
            source=ExpressionCallNoKeywords(
                called=ExpressionTempVariableRef(
                    variable=tmp_metaclass.makeReference(result),
                    source_ref=source_ref),
                args=ExpressionMakeTuple(elements=(
                    ExpressionConstantRef(constant=node.name,
                                          source_ref=source_ref),
                    ExpressionTempVariableRef(
                        variable=tmp_bases.makeReference(result),
                        source_ref=source_ref),
                    ExpressionTempVariableRef(
                        variable=tmp_class_dict.makeReference(result),
                        source_ref=source_ref)),
                                         source_ref=source_ref),
                source_ref=source_ref),
            source_ref=source_ref)
    ]

    for decorator in buildNodeList(provider, reversed(node.decorator_list),
                                   source_ref):
        statements.append(
            StatementAssignmentVariable(
                variable_ref=ExpressionTargetTempVariableRef(
                    variable=tmp_class.makeReference(result),
                    source_ref=source_ref),
                source=ExpressionCallNoKeywords(
                    called=decorator,
                    args=ExpressionMakeTuple(
                        elements=(ExpressionTempVariableRef(
                            variable=tmp_class.makeReference(result),
                            source_ref=source_ref), ),
                        source_ref=source_ref),
                    source_ref=decorator.getSourceReference()),
                source_ref=decorator.getSourceReference()))

    statements.append(
        StatementAssignmentVariable(
            variable_ref=ExpressionTargetVariableRef(variable_name=node.name,
                                                     source_ref=source_ref),
            source=ExpressionTempVariableRef(
                variable=tmp_class.makeReference(result),
                source_ref=source_ref),
            source_ref=source_ref))

    result.setBody(
        StatementsSequence(statements=statements, source_ref=source_ref))

    return result
Example #14
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

    # This function is the Python3 special case with special re-formulation as
    # according to developer manual.
    class_statements, 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 = ExpressionFunctionBody(
        provider=provider,
        is_class=True,
        parameters=make_class_parameters,
        name=node.name,
        doc=class_doc,
        source_ref=source_ref)

    # Hack: This allows some APIs to work although this is not yet officially a
    # child yet.
    class_creation_function.parent = provider

    body = buildStatementsNode(provider=class_creation_function,
                               nodes=class_statements,
                               frame=True,
                               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.makeReference(provider),
            source_ref=source_ref),
                           source_ref=source_ref),
        StatementAssignmentVariable(
            variable_ref=ExpressionTargetVariableRef(
                variable_name="__module__",
                variable=module_variable,
                source_ref=source_ref),
            source=ExpressionConstantRef(
                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=ExpressionConstantRef(constant=class_doc,
                                             source_ref=source_ref,
                                             user_provided=True),
                source_ref=source_ref))

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

        statements.append(
            StatementAssignmentVariable(
                variable_ref=ExpressionTargetVariableRef(
                    variable_name="__qualname__",
                    variable=qualname_variable,
                    source_ref=source_ref),
                source=ExpressionConstantRef(constant=qualname,
                                             source_ref=source_ref,
                                             user_provided=True),
                source_ref=source_ref))

    if Utils.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.makeReference(class_creation_function),
            source_ref=source_ref)
        class_variable_ref = ExpressionTempVariableRef(
            variable=tmp_class.makeReference(class_creation_function),
            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)

    statements += [
        body,
        StatementAssignmentVariable(
            variable_ref=class_target_variable_ref,
            source=ExpressionCall(
                called=ExpressionTempVariableRef(
                    variable=tmp_metaclass.makeReference(provider),
                    source_ref=source_ref),
                args=makeSequenceCreationOrConstant(
                    sequence_kind="tuple",
                    elements=(ExpressionConstantRef(constant=node.name,
                                                    source_ref=source_ref,
                                                    user_provided=True),
                              ExpressionTempVariableRef(
                                  variable=tmp_bases.makeReference(provider),
                                  source_ref=source_ref),
                              ExpressionBuiltinLocals(source_ref=source_ref)),
                    source_ref=source_ref),
                kw=ExpressionTempVariableRef(
                    variable=tmp_class_decl_dict.makeReference(provider),
                    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 implicitely, at the end
    # returns its locals and cannot have other return statements contained.

    class_creation_function.setBody(body)

    # The class body is basically a function that implicitely, 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),
                                            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.makeReference(provider),
                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.makeReference(provider),
                source_ref=source_ref),
            source=makeDictCreationOrConstant(keys=[
                ExpressionConstantRef(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
                                              ],
                                              lazy_order=False,
                                              source_ref=source_ref),
            source_ref=source_ref),
        StatementAssignmentVariable(
            variable_ref=ExpressionTargetTempVariableRef(
                variable=tmp_metaclass.makeReference(provider),
                source_ref=source_ref),
            source=ExpressionSelectMetaclass(metaclass=ExpressionConditional(
                condition=ExpressionComparison(
                    comparator="In",
                    left=ExpressionConstantRef(constant="metaclass",
                                               source_ref=source_ref,
                                               user_provided=True),
                    right=ExpressionTempVariableRef(
                        variable=tmp_class_decl_dict.makeReference(provider),
                        source_ref=source_ref),
                    source_ref=source_ref),
                yes_expression=ExpressionDictOperationGet(
                    dicte=ExpressionTempVariableRef(
                        variable=tmp_class_decl_dict.makeReference(provider),
                        source_ref=source_ref),
                    key=ExpressionConstantRef(constant="metaclass",
                                              source_ref=source_ref,
                                              user_provided=True),
                    source_ref=source_ref),
                no_expression=ExpressionConditional(
                    condition=ExpressionTempVariableRef(
                        variable=tmp_bases.makeReference(provider),
                        source_ref=source_ref),
                    no_expression=ExpressionBuiltinRef(builtin_name="type",
                                                       source_ref=source_ref),
                    yes_expression=ExpressionBuiltinType1(
                        value=ExpressionSubscriptLookup(
                            expression=ExpressionTempVariableRef(
                                variable=tmp_bases.makeReference(provider),
                                source_ref=source_ref),
                            subscript=ExpressionConstantRef(
                                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.
                                                 makeReference(provider),
                                                 source_ref=source_ref),
                                             source_ref=source_ref),
            source_ref=source_ref_orig),
        StatementConditional(
            condition=ExpressionComparison(
                comparator="In",
                left=ExpressionConstantRef(constant="metaclass",
                                           source_ref=source_ref,
                                           user_provided=True),
                right=ExpressionTempVariableRef(
                    variable=tmp_class_decl_dict.makeReference(provider),
                    source_ref=source_ref),
                source_ref=source_ref),
            no_branch=None,
            yes_branch=makeStatementsSequenceFromStatement(
                statement=StatementDictOperationRemove(
                    dicte=ExpressionTempVariableRef(
                        variable=tmp_class_decl_dict.makeReference(provider),
                        source_ref=source_ref),
                    key=ExpressionConstantRef(constant="metaclass",
                                              source_ref=source_ref,
                                              user_provided=True),
                    source_ref=source_ref)),
            source_ref=source_ref),
        StatementAssignmentVariable(
            variable_ref=ExpressionTargetTempVariableRef(
                variable=tmp_prepared.makeReference(provider),
                source_ref=source_ref),
            source=ExpressionConditional(
                condition=ExpressionBuiltinHasattr(
                    object=ExpressionTempVariableRef(
                        variable=tmp_metaclass.makeReference(provider),
                        source_ref=source_ref),
                    name=ExpressionConstantRef(constant="__prepare__",
                                               source_ref=source_ref,
                                               user_provided=True),
                    source_ref=source_ref),
                no_expression=ExpressionConstantRef(constant={},
                                                    source_ref=source_ref,
                                                    user_provided=True),
                yes_expression=ExpressionCall(
                    called=ExpressionAttributeLookup(
                        expression=ExpressionTempVariableRef(
                            variable=tmp_metaclass.makeReference(provider),
                            source_ref=source_ref),
                        attribute_name="__prepare__",
                        source_ref=source_ref),
                    args=ExpressionMakeTuple(elements=(
                        ExpressionConstantRef(constant=node.name,
                                              source_ref=source_ref,
                                              user_provided=True),
                        ExpressionTempVariableRef(
                            variable=tmp_bases.makeReference(provider),
                            source_ref=source_ref)),
                                             source_ref=source_ref),
                    kw=ExpressionTempVariableRef(
                        variable=tmp_class_decl_dict.makeReference(provider),
                        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),
    )

    final = (StatementDelVariable(variable_ref=ExpressionTargetTempVariableRef(
        variable=tmp_bases.makeReference(provider), source_ref=source_ref),
                                  tolerant=True,
                                  source_ref=source_ref),
             StatementDelVariable(variable_ref=ExpressionTargetTempVariableRef(
                 variable=tmp_class_decl_dict.makeReference(provider),
                 source_ref=source_ref),
                                  tolerant=True,
                                  source_ref=source_ref),
             StatementDelVariable(variable_ref=ExpressionTargetTempVariableRef(
                 variable=tmp_metaclass.makeReference(provider),
                 source_ref=source_ref),
                                  tolerant=True,
                                  source_ref=source_ref),
             StatementDelVariable(variable_ref=ExpressionTargetTempVariableRef(
                 variable=tmp_prepared.makeReference(provider),
                 source_ref=source_ref),
                                  tolerant=True,
                                  source_ref=source_ref))

    return makeTryFinallyStatement(tried=statements,
                                   final=final,
                                   source_ref=source_ref)
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
Example #16
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)
Example #17
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 and optimization checks:
    # pylint: disable=I0021,too-many-branches,too-many-locals,too-many-statements

    # 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")

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

    class_locals_scope = class_creation_function.getLocalsScope()

    # Only local variable, for provision to methods.
    class_variable = class_locals_scope.getLocalVariable(
        owner=class_creation_function, variable_name="__class__")
    class_locals_scope.registerProvidedVariable(class_variable)

    class_variable_ref = ExpressionVariableRef(variable=class_variable,
                                               source_ref=source_ref)

    parent_module = provider.getParentModule()

    code_object = CodeObjectSpec(
        co_name=node.name,
        co_kind="Class",
        co_varnames=(),
        co_freevars=(),
        co_argcount=0,
        co_posonlyargcount=0,
        co_kwonlyargcount=0,
        co_has_starlist=False,
        co_has_stardict=False,
        co_filename=parent_module.getRunTimeFilename(),
        co_lineno=source_ref.getLineNumber(),
        future_spec=parent_module.getFutureSpec(),
    )

    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

    locals_scope = class_creation_function.getLocalsScope()

    statements = [
        StatementSetLocals(
            locals_scope=locals_scope,
            new_locals=ExpressionTempVariableRef(variable=tmp_prepared,
                                                 source_ref=source_ref),
            source_ref=source_ref,
        ),
        StatementAssignmentVariableName(
            provider=class_creation_function,
            variable_name="__module__",
            source=ExpressionModuleAttributeNameRef(
                variable=provider.getParentModule().getVariableForReference(
                    "__name__"),
                source_ref=source_ref,
            ),
            source_ref=source_ref,
        ),
    ]

    if class_doc is not None:
        statements.append(
            StatementAssignmentVariableName(
                provider=class_creation_function,
                variable_name="__doc__",
                source=makeConstantRefNode(constant=class_doc,
                                           source_ref=source_ref,
                                           user_provided=True),
                source_ref=source_ref,
            ))

    # The "__qualname__" attribute is new in Python3.
    qualname = class_creation_function.getFunctionQualname()

    if python_version < 0x340:
        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(
        StatementLocalsDictOperationSet(
            locals_scope=locals_scope,
            variable_name="__qualname__",
            value=qualname_ref,
            source_ref=source_ref,
        ))

    if python_version >= 0x340:
        qualname_assign = statements[-1]

    if python_version >= 0x360 and class_creation_function.needsAnnotationsDictionary(
    ):
        statements.append(
            StatementLocalsDictOperationSet(
                locals_scope=locals_scope,
                variable_name="__annotations__",
                value=makeConstantRefNode(constant={},
                                          source_ref=source_ref,
                                          user_provided=True),
                source_ref=source_ref,
            ))

    statements.append(body)

    if node.bases:
        tmp_bases = provider.allocateTempVariable(temp_scope=temp_scope,
                                                  name="bases")

        if python_version >= 0x370:
            tmp_bases_orig = provider.allocateTempVariable(
                temp_scope=temp_scope, name="bases_orig")

        def makeBasesRef():
            return ExpressionTempVariableRef(variable=tmp_bases,
                                             source_ref=source_ref)

    else:

        def makeBasesRef():
            return makeConstantRefNode(constant=(), source_ref=source_ref)

    if python_version >= 0x370 and node.bases:
        statements.append(
            makeStatementConditional(
                condition=makeComparisonExpression(
                    comparator="NotEq",
                    left=ExpressionTempVariableRef(variable=tmp_bases,
                                                   source_ref=source_ref),
                    right=ExpressionTempVariableRef(variable=tmp_bases_orig,
                                                    source_ref=source_ref),
                    source_ref=source_ref,
                ),
                yes_branch=StatementLocalsDictOperationSet(
                    locals_scope=locals_scope,
                    variable_name="__orig_bases__",
                    value=ExpressionTempVariableRef(variable=tmp_bases_orig,
                                                    source_ref=source_ref),
                    source_ref=source_ref,
                ),
                no_branch=None,
                source_ref=source_ref,
            ))

    statements += (
        StatementAssignmentVariable(
            variable=class_variable,
            source=makeExpressionCall(
                called=ExpressionTempVariableRef(variable=tmp_metaclass,
                                                 source_ref=source_ref),
                args=makeExpressionMakeTuple(
                    elements=(
                        makeConstantRefNode(
                            constant=node.name,
                            source_ref=source_ref,
                            user_provided=True,
                        ),
                        makeBasesRef(),
                        ExpressionBuiltinLocalsRef(locals_scope=locals_scope,
                                                   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 = makeStatementsSequenceFromStatement(
        statement=makeTryFinallyStatement(
            provider=class_creation_function,
            tried=mergeStatements(statements, True),
            final=StatementReleaseLocals(locals_scope=locals_scope,
                                         source_ref=source_ref),
            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.setChild("body", body)

    # 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 = class_creation_function

    for decorator in buildNodeList(provider, reversed(node.decorator_list),
                                   source_ref):
        decorated_body = makeExpressionCall(
            called=decorator,
            args=makeExpressionMakeTuple(elements=(decorated_body, ),
                                         source_ref=source_ref),
            kw=None,
            source_ref=decorator.getSourceReference(),
        )

    if node.keywords and node.keywords[-1].arg is None:
        keywords = node.keywords[:-1]
    else:
        keywords = node.keywords

    statements = []

    if node.bases:
        statements.append(
            StatementAssignmentVariable(
                variable=tmp_bases
                if python_version < 0x370 else tmp_bases_orig,
                source=_buildBasesTupleCreationNode(provider=provider,
                                                    elements=node.bases,
                                                    source_ref=source_ref),
                source_ref=source_ref,
            ))

        if python_version >= 0x370:
            bases_conversion = ExpressionFunctionCall(
                function=ExpressionFunctionCreation(
                    function_ref=ExpressionFunctionRef(
                        function_body=getClassBasesMroConversionHelper(),
                        source_ref=source_ref,
                    ),
                    defaults=(),
                    kw_defaults=None,
                    annotations=None,
                    source_ref=source_ref,
                ),
                values=(ExpressionTempVariableRef(variable=tmp_bases_orig,
                                                  source_ref=source_ref), ),
                source_ref=source_ref,
            )

            statements.append(
                StatementAssignmentVariable(variable=tmp_bases,
                                            source=bases_conversion,
                                            source_ref=source_ref))

    statements.append(
        StatementAssignmentVariable(
            variable=tmp_class_decl_dict,
            source=makeDictCreationOrConstant2(
                keys=[keyword.arg for keyword in keywords],
                values=[
                    buildNode(provider, keyword.value, source_ref)
                    for keyword in keywords
                ],
                source_ref=source_ref,
            ),
            source_ref=source_ref,
        ))

    if node.keywords and node.keywords[-1].arg is None:
        statements.append(
            StatementDictOperationUpdate(
                dict_arg=ExpressionVariableRef(variable=tmp_class_decl_dict,
                                               source_ref=source_ref),
                value=buildNode(provider, node.keywords[-1].value, source_ref),
                source_ref=source_ref,
            ))

    # Check if there are bases, and if there are, go with the type of the
    # first base class as a metaclass unless it was specified in the class
    # decl dict of course.
    if node.bases:
        unspecified_metaclass_expression = ExpressionBuiltinType1(
            value=ExpressionSubscriptLookup(
                expression=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,
        )

        # Might become empty behind our back during conversion, therefore make the
        # check at run time for 3.7 or higher.
        if python_version >= 0x370:
            unspecified_metaclass_expression = ExpressionConditional(
                condition=ExpressionTempVariableRef(variable=tmp_bases,
                                                    source_ref=source_ref),
                expression_yes=unspecified_metaclass_expression,
                expression_no=makeExpressionBuiltinTypeRef(
                    builtin_name="type", source_ref=source_ref),
                source_ref=source_ref,
            )
    else:
        unspecified_metaclass_expression = makeExpressionBuiltinTypeRef(
            builtin_name="type", source_ref=source_ref)

    call_prepare = StatementAssignmentVariable(
        variable=tmp_prepared,
        source=makeExpressionCall(
            called=ExpressionAttributeLookup(
                expression=ExpressionTempVariableRef(variable=tmp_metaclass,
                                                     source_ref=source_ref),
                attribute_name="__prepare__",
                source_ref=source_ref,
            ),
            args=makeExpressionMakeTuple(
                elements=(
                    makeConstantRefNode(constant=node.name,
                                        source_ref=source_ref,
                                        user_provided=True),
                    makeBasesRef(),
                ),
                source_ref=source_ref,
            ),
            kw=ExpressionTempVariableRef(variable=tmp_class_decl_dict,
                                         source_ref=source_ref),
            source_ref=source_ref,
        ),
        source_ref=source_ref,
    )

    if python_version >= 0x364:
        call_prepare = makeStatementsSequenceFromStatements(
            call_prepare,
            makeStatementConditional(
                condition=ExpressionAttributeCheck(
                    expression=ExpressionTempVariableRef(
                        variable=tmp_prepared, source_ref=source_ref),
                    attribute_name="__getitem__",
                    source_ref=source_ref,
                ),
                yes_branch=None,
                no_branch=makeRaiseExceptionExpressionFromTemplate(
                    exception_type="TypeError",
                    template="%s.__prepare__() must return a mapping, not %s",
                    template_args=(
                        ExpressionBuiltinGetattr(
                            expression=ExpressionTempVariableRef(
                                variable=tmp_metaclass, source_ref=source_ref),
                            name=makeConstantRefNode(constant="__name__",
                                                     source_ref=source_ref),
                            default=makeConstantRefNode(constant="<metaclass>",
                                                        source_ref=source_ref),
                            source_ref=source_ref,
                        ),
                        ExpressionAttributeLookup(
                            expression=ExpressionBuiltinType1(
                                value=ExpressionTempVariableRef(
                                    variable=tmp_prepared,
                                    source_ref=source_ref),
                                source_ref=source_ref,
                            ),
                            attribute_name="__name__",
                            source_ref=source_ref,
                        ),
                    ),
                    source_ref=source_ref,
                ).asStatement(),
                source_ref=source_ref,
            ),
        )

    statements += (
        StatementAssignmentVariable(
            variable=tmp_metaclass,
            source=ExpressionSelectMetaclass(
                metaclass=ExpressionConditional(
                    condition=ExpressionDictOperationIn(
                        key=makeConstantRefNode(
                            constant="metaclass",
                            source_ref=source_ref,
                            user_provided=True,
                        ),
                        dict_arg=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=unspecified_metaclass_expression,
                    source_ref=source_ref,
                ),
                bases=makeBasesRef(),
                source_ref=source_ref,
            ),
            source_ref=source_ref_orig,
        ),
        makeStatementConditional(
            condition=ExpressionDictOperationIn(
                key=makeConstantRefNode(constant="metaclass",
                                        source_ref=source_ref,
                                        user_provided=True),
                dict_arg=ExpressionTempVariableRef(
                    variable=tmp_class_decl_dict, source_ref=source_ref),
                source_ref=source_ref,
            ),
            no_branch=None,
            yes_branch=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,
        ),
        makeStatementConditional(
            condition=ExpressionAttributeCheck(
                expression=ExpressionTempVariableRef(variable=tmp_metaclass,
                                                     source_ref=source_ref),
                attribute_name="__prepare__",
                source_ref=source_ref,
            ),
            yes_branch=call_prepare,
            no_branch=StatementAssignmentVariable(
                variable=tmp_prepared,
                source=makeConstantRefNode(constant={},
                                           source_ref=source_ref,
                                           user_provided=True),
                source_ref=source_ref,
            ),
            source_ref=source_ref,
        ),
        StatementAssignmentVariableName(
            provider=provider,
            variable_name=mangleName(node.name, provider),
            source=decorated_body,
            source_ref=source_ref,
        ),
    )

    if python_version >= 0x340:
        class_creation_function.qualname_setup = node.name, qualname_assign

    final = [tmp_class_decl_dict, tmp_metaclass, tmp_prepared]
    if node.bases:
        final.insert(0, tmp_bases)
        if python_version >= 0x370:
            final.insert(0, tmp_bases_orig)

    return makeTryFinallyStatement(
        provider=provider,
        tried=statements,
        final=tuple(
            StatementReleaseVariable(variable=variable, source_ref=source_ref)
            for variable in final),
        source_ref=source_ref,
    )
Example #18
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))
Example #19
0
def computeBuiltinCall(call_node, called):
    builtin_name = called.getBuiltinName()

    if builtin_name in _dispatch_dict:
        new_node = _dispatch_dict[builtin_name](call_node)

        if new_node is None:
            return call_node, None, None

        inspect_node = new_node

        if inspect_node.isExpressionSideEffects():
            inspect_node = inspect_node.getExpression()

        if inspect_node.isExpressionBuiltinImport():
            tags = "new_import"
            message = "Replaced dynamic builtin import %s with static module import." % inspect_node.kind
        elif inspect_node.isExpressionBuiltin(
        ) or inspect_node.isStatementExec():
            tags = "new_builtin"
            message = "Replaced call to builtin with builtin call %s." % inspect_node.kind
        elif inspect_node.isExpressionRaiseException():
            tags = "new_raise"
            message = "Replaced call to builtin %s with exception raising call." % inspect_node.kind
        elif inspect_node.isExpressionOperationUnary():
            tags = "new_expression"
            message = "Replaced call to builtin %s with unary operation %s." % (
                inspect_node.kind, inspect_node.getOperator())
        else:
            assert False, (builtin_name, "->", inspect_node)

        # TODO: One day, this should be enabled by default and call either the original
        # built-in or the optimized above one. That should be done, once we can eliminate
        # the condition for most cases.
        if False and isDebug() and not shallMakeModule() and builtin_name:
            from nuitka.nodes.ConditionalNodes import ExpressionConditional
            from nuitka.nodes.ComparisonNodes import ExpressionComparisonIs
            from nuitka.nodes.BuiltinRefNodes import (
                ExpressionBuiltinExceptionRef,
                ExpressionBuiltinOriginalRef,
                ExpressionBuiltinRef,
            )
            from nuitka.nodes.ExceptionNodes import ExpressionRaiseException

            source_ref = called.getSourceReference()

            new_node = ExpressionConditional(
                condition=ExpressionComparisonIs(
                    left=ExpressionBuiltinRef(builtin_name=builtin_name,
                                              source_ref=source_ref),
                    right=ExpressionBuiltinOriginalRef(
                        builtin_name=builtin_name, source_ref=source_ref),
                    source_ref=source_ref),
                yes_expression=new_node,
                no_expression=makeRaiseExceptionReplacementExpression(
                    exception_type="RuntimeError",
                    exception_value="Builtin '%s' was overloaded'" %
                    builtin_name,
                    expression=call_node),
                source_ref=source_ref)

        return new_node, tags, message
    else:
        # TODO: Consider giving warnings, whitelisted potentially
        return call_node, None, None
Example #20
0
    def wrapEvalBuiltin(source, globals, locals, source_ref):
        provider = node.getParentVariableProvider()

        temp_scope = provider.allocateTempScope("eval")

        globals_ref, locals_ref, tried, final = wrapEvalGlobalsAndLocals(
            provider     = provider,
            globals_node = globals,
            locals_node  = locals,
            temp_scope   = temp_scope,
            source_ref   = source_ref
        )

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

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

        final.setStatements(
            final.getStatements() + (
                StatementDelVariable(
                    variable_ref = ExpressionTargetTempVariableRef(
                        variable   = source_variable.makeReference(
                            provider
                        ),
                        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.makeReference(
                                provider
                            ),
                            source_ref = source_ref
                        ),
                        source_ref = source_ref
                    ),
                    right      = ExpressionBuiltinRef(
                        builtin_name = "bytes",
                        source_ref   = source_ref
                    ),
                    source_ref = source_ref
                ),
                yes_expression = ExpressionConstantRef(
                    constant = (b" \t",),
                    source_ref = source_ref
                ),
                no_expression  = 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.makeReference(
                        provider
                    ),
                    source_ref = source_ref
                ),
                source = ExpressionCallNoKeywords(
                    called = ExpressionAttributeLookup(
                        expression     = ExpressionTempVariableRef(
                            variable   = source_variable.makeReference(
                                provider
                            ),
                            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.makeReference(
                        provider
                    ),
                    source_ref = source_ref
                ),
                source       = source,
                source_ref   = source_ref,
            ),
            StatementConditional(
                condition = ExpressionOperationNOT(
                    operand    = ExpressionBuiltinIsinstance(
                        cls = ExpressionBuiltinAnonymousRef(
                            builtin_name = "code",
                            source_ref   = source_ref,
                        ),
                        instance = ExpressionTempVariableRef(
                            variable   = source_variable.makeReference(
                                provider
                            ),
                            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
            )
        )

        tried.setStatements(
            tried.getStatements() + statements
        )

        return ExpressionTryFinally(
            tried      = tried,
            expression = ExpressionBuiltinEval(
                source_code = ExpressionTempVariableRef(
                    variable   = source_variable.makeReference(
                        provider
                    ),
                    source_ref = source_ref
                ),
                globals_arg = globals_ref,
                locals_arg  = locals_ref,
                source_ref  = source_ref
            ),
            final      = final,
            source_ref = source_ref
        )