Ejemplo n.º 1
0
def makeDictCreationOrConstant(keys, values, lazy_order, source_ref):
    # Create dictionary node. Tries to avoid it for constant values that are not
    # mutable.

    assert len(keys) == len(values)
    for key, value in zip(keys, values):
        if not key.isExpressionConstantRef():
            constant = False
            break

        if not value.isExpressionConstantRef():
            constant = False
            break
    else:
        constant = True

    # Note: This would happen in optimization instead, but lets just do it
    # immediately to save some time.
    if constant:
        # Unless tolder otherwise, create the dictionary in its full size, so
        # that no growing occurs and the constant becomes as similar as possible
        # before being marshalled.
        return ExpressionConstantRef(constant=Constants.createConstantDict(
            lazy_order=not lazy_order,
            keys=[key.getConstant() for key in keys],
            values=[value.getConstant() for value in values]),
                                     source_ref=source_ref,
                                     user_provided=True)
    else:
        return ExpressionMakeDict(pairs=[
            ExpressionKeyValuePair(key, value, key.getSourceReference())
            for key, value in zip(keys, values)
        ],
                                  lazy_order=lazy_order,
                                  source_ref=source_ref)
def buildCallNode( provider, node, source_ref ):
    positional_args = buildNodeList( provider, node.args, source_ref )

    # Only the values of keyword pairs have a real source ref, and those only really
    # matter, so that makes sense.
    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
    ]

    list_star_arg   = buildNode( provider, node.starargs, source_ref, True )
    dict_star_arg   = buildNode( provider, node.kwargs, source_ref, True )

    return _makeCallNode(
        provider        = provider,
        called          = buildNode( provider, node.func, source_ref ),
        positional_args = positional_args,
        pairs           = pairs,
        list_star_arg   = list_star_arg,
        dict_star_arg   = dict_star_arg,
        source_ref      = source_ref,
    )
def buildParameterKwDefaults( provider, node, function_body, source_ref ):
    # Build keyword only arguments default values. We are hiding here, that it is a
    # Python3 only feature.

    if Utils.python_version >= 300:
        kw_only_names = function_body.getParameters().getKwOnlyParameterNames()
        pairs = []

        for kw_name, kw_default in zip( kw_only_names, node.args.kw_defaults ):
            if kw_default is not None:
                pairs.append(
                    ExpressionKeyValuePair(
                        key = ExpressionConstantRef(
                            constant   = kw_name,
                            source_ref = source_ref
                        ),
                        value = buildNode( provider, kw_default, source_ref ),
                        source_ref = source_ref
                    )
                )

        if pairs:
            kw_defaults = ExpressionMakeDict(
                pairs = pairs,
                source_ref = source_ref
            )
        else:
            kw_defaults = None
    else:
        kw_defaults = None

    return kw_defaults
 def extractArg(arg):
     if getKind(arg) == "Name":
         assert arg.annotation is None
     elif getKind(arg) == "arg":
         if arg.annotation is not None:
             pairs.append(
                 ExpressionKeyValuePair(
                     key=ExpressionConstantRef(constant=arg.arg,
                                               source_ref=source_ref),
                     value=buildNode(provider, arg.annotation, source_ref),
                     source_ref=source_ref))
     elif getKind(arg) == "Tuple":
         for arg in arg.elts:
             extractArg(arg)
     else:
         assert False, getKind(arg)
Ejemplo n.º 5
0
def buildDictionaryNode(provider, node, source_ref):
    # Create dictionary node. Tries to avoid it for constant values that are not mutable.

    keys = []
    values = []

    constant = True

    for key, value in zip(node.keys, node.values):
        key_node = buildNode(provider, key, source_ref)
        value_node = buildNode(provider, value, source_ref)

        keys.append(key_node)
        values.append(value_node)

        constant = constant and key_node.isExpressionConstantRef()
        constant = constant and value_node.isExpressionConstantRef(
        ) and not value_node.isMutable()

    # Note: This would happen in optimization instead, but lets just do it immediately to
    # save some time.
    if constant:
        # Create the dictionary in its full size, so that no growing occurs and the
        # constant becomes as similar as possible before being marshalled.
        constant_value = dict.fromkeys([key.getConstant() for key in keys],
                                       None)

        for key, value in zip(keys, values):
            constant_value[key.getConstant()] = value.getConstant()

        return ExpressionConstantRef(constant=constant_value,
                                     source_ref=source_ref)
    else:
        return ExpressionMakeDict(pairs=[
            ExpressionKeyValuePair(key, value, key.getSourceReference())
            for key, value in zip(keys, values)
        ],
                                  source_ref=source_ref)
Ejemplo n.º 6
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
def buildParameterAnnotations( provider, node, source_ref ):
    # Too many branches, because there is too many cases, pylint: disable=R0912

    # Build annotations. We are hiding here, that it is a Python3 only feature.
    if Utils.python_version < 300:
        return None

    pairs = []

    def extractArg( arg ):
        if getKind( arg ) == "Name":
            assert arg.annotation is None
        elif getKind( arg ) == "arg":
            if arg.annotation is not None:
                pairs.append(
                    ExpressionKeyValuePair(
                        key        = ExpressionConstantRef(
                            constant   = arg.arg,
                            source_ref = source_ref
                        ),
                        value      = buildNode( provider, arg.annotation, source_ref ),
                        source_ref = source_ref
                    )
                )
        elif getKind( arg ) == "Tuple":
            for arg in arg.elts:
                extractArg( arg )
        else:
            assert False, getKind( arg )

    for arg in node.args.args:
        extractArg( arg )

    for arg in node.args.kwonlyargs:
        extractArg( arg )

    if node.args.varargannotation is not None:
        pairs.append(
            ExpressionKeyValuePair(
                key        = ExpressionConstantRef(
                    constant   = node.args.vararg,
                    source_ref = source_ref
                ),
                value      = buildNode( provider, node.args.varargannotation, source_ref ),
                source_ref = source_ref
            )
        )

    if node.args.kwargannotation is not None:
        pairs.append(
            ExpressionKeyValuePair(
                key        = ExpressionConstantRef(
                    constant   = node.args.kwarg,
                    source_ref = source_ref
                ),
                value      = buildNode( provider, node.args.kwargannotation, source_ref ),
                source_ref = source_ref
            )
        )

    # Return value annotation (not there for lambdas)
    if hasattr( node, "returns" ) and node.returns is not None:
        pairs.append(
            ExpressionKeyValuePair(
                key        = ExpressionConstantRef(
                    constant   = "return",
                    source_ref = source_ref
                ),
                value      = buildNode( provider, node.returns, source_ref ),
                source_ref = source_ref
            )
        )

    if pairs:
        return ExpressionMakeDict(
            pairs      = pairs,
            source_ref = source_ref
        )
    else:
        return None