def _buildInplaceAssignVariableNode(variable_ref, operator, expression,
                                    source_ref):
    assert variable_ref.isExpressionTargetVariableRef(), variable_ref

    inplace_node = makeExpressionOperationBinaryInplace(
        operator   = operator,
        left       = ExpressionVariableRef(
            variable_name = variable_ref.getVariableName(),
            source_ref    = source_ref
        ),
        right      = expression,
        source_ref = source_ref
    )

    inplace_node.markAsInplaceSuspect()

    result = (
        StatementAssignmentVariable(
            variable_ref = ExpressionTargetVariableRef(
                variable_name = variable_ref.getVariableName(),
                source_ref    = source_ref
            ),
            source       = inplace_node,
            source_ref   = source_ref
        ),
    )

    return result
コード例 #2
0
def createPathAssignment(source_ref):
    if Options.getFileReferenceMode() == "original":
        path_value = ExpressionConstantRef(
            constant=[dirname(source_ref.getFilename())],
            source_ref=source_ref,
            user_provided=True)
    else:
        path_value = ExpressionMakeList(elements=(ExpressionCallNoKeywords(
            called=ExpressionAttributeLookup(source=ExpressionImportModuleHard(
                module_name="os", import_name="path", source_ref=source_ref),
                                             attribute_name="dirname",
                                             source_ref=source_ref),
            args=ExpressionMakeTuple(
                elements=(ExpressionModuleFileAttributeRef(
                    source_ref=source_ref, ), ),
                source_ref=source_ref,
            ),
            source_ref=source_ref,
        ), ),
                                        source_ref=source_ref)

    return StatementAssignmentVariable(
        variable_ref=ExpressionTargetVariableRef(variable_name="__path__",
                                                 source_ref=source_ref),
        source=path_value,
        source_ref=source_ref)
コード例 #3
0
def createPython3NamespacePath(package_name, module_relpath, source_ref):
    return StatementAssignmentVariable(
        variable_ref = ExpressionTargetVariableRef(
            variable_name = "__path__",
            source_ref    = source_ref
        ),
        source       = ExpressionCallNoKeywords(
            called     = ExpressionImportName(
                module      = ExpressionImportModule(
                    module_name = "_frozen_importlib"
                                    if python_version < 350 else
                                  "_frozen_importlib_external",
                    import_list = (),
                    level       = 0,
                    source_ref  = source_ref
                ),
                import_name = "_NamespacePath",
                source_ref  = source_ref
            ),
            args       = makeConstantRefNode(
                constant   = (
                    package_name,
                    [module_relpath],
                    None
                ),
                source_ref =  source_ref
            ),
            source_ref =  source_ref
        ),
        source_ref   = source_ref
    )
コード例 #4
0
ファイル: Building.py プロジェクト: gitter-badger/Nuitka
def buildImportModulesNode(provider, node, source_ref):
    # Import modules statement. As described in the developer manual, these
    # statements can be treated as several ones.

    import_names = [(import_desc.name, import_desc.asname)
                    for import_desc in node.names]

    import_nodes = []

    for import_desc in import_names:
        module_name, local_name = import_desc

        module_topname = module_name.split('.')[0]

        # Note: The "level" of import is influenced by the future absolute
        # imports.
        level = 0 if source_ref.getFutureSpec().isAbsoluteImport() else -1

        if local_name:
            # If is gets a local name, the real name must be used as a
            # temporary value only, being looked up recursively.

            import_node = ExpressionImportModule(module_name=module_name,
                                                 import_list=None,
                                                 level=level,
                                                 source_ref=source_ref)

            for import_name in module_name.split('.')[1:]:
                import_node = ExpressionImportName(module=import_node,
                                                   import_name=import_name,
                                                   source_ref=source_ref)
        else:
            import_node = ExpressionImportModule(module_name=module_name,
                                                 import_list=None,
                                                 level=level,
                                                 source_ref=source_ref)

        # If a name was given, use the one provided, otherwise the import gives
        # the top level package name given for assignment of the imported
        # module.

        import_nodes.append(
            StatementAssignmentVariable(
                variable_ref=ExpressionTargetVariableRef(
                    variable_name=mangleName(
                        local_name if local_name is not None else
                        module_topname, provider),
                    source_ref=source_ref),
                source=import_node,
                source_ref=source_ref))

    # Note: Each import is sequential. It will potentially succeed, and the
    # failure of a later one is not changing that one bit . We can therefore
    # have a sequence of imports that only import one thing therefore.
    return makeStatementsSequenceOrStatement(statements=import_nodes,
                                             source_ref=source_ref)
コード例 #5
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)
コード例 #6
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)
コード例 #7
0
def buildFunctionNode(provider, node, source_ref):
    # Functions have way too many details, pylint: disable=R0912,R0914

    assert getKind(node) == "FunctionDef"

    function_statement_nodes, function_doc = extractDocFromBody(node)

    function_kind, flags, _written_variables, _non_local_declarations, _global_declarations = \
      detectFunctionBodyKind(
        nodes = function_statement_nodes
    )

    outer_body, function_body, code_object = buildFunctionWithParsing(
        provider=provider,
        function_kind=function_kind,
        name=node.name,
        function_doc=function_doc,
        flags=flags,
        node=node,
        source_ref=source_ref)

    if function_kind == "Function":
        code_body = function_body
    elif function_kind == "Generator":
        code_body = ExpressionGeneratorObjectBody(provider=function_body,
                                                  name=node.name,
                                                  flags=flags,
                                                  source_ref=source_ref)

        for variable in function_body.getVariables():
            code_body.getVariableForReference(variable.getName())
    else:
        assert False, function_kind

    if function_kind == "Generator":
        function_body.setBody(
            makeStatementsSequenceFromStatement(statement=StatementReturn(
                expression=ExpressionMakeGeneratorObject(
                    generator_ref=ExpressionFunctionRef(
                        function_body=code_body, source_ref=source_ref),
                    code_object=code_object,
                    source_ref=source_ref),
                source_ref=source_ref)))

    decorators = buildNodeList(provider=provider,
                               nodes=reversed(node.decorator_list),
                               source_ref=source_ref)

    defaults = buildNodeList(provider=provider,
                             nodes=node.args.defaults,
                             source_ref=source_ref)

    kw_defaults = buildParameterKwDefaults(provider=provider,
                                           node=node,
                                           function_body=function_body,
                                           source_ref=source_ref)

    function_statements_body = buildFrameNode(provider=code_body,
                                              nodes=function_statement_nodes,
                                              code_object=code_object,
                                              source_ref=source_ref)

    if function_kind == "Function":
        # TODO: Generators might have to raise GeneratorExit instead.
        function_statements_body = _insertFinalReturnStatement(
            function_statements_body=function_statements_body,
            return_class=StatementReturn,
            source_ref=source_ref)

    if function_statements_body.isStatementsFrame():
        function_statements_body = makeStatementsSequenceFromStatement(
            statement=function_statements_body)

    code_body.setBody(function_statements_body)

    annotations = buildParameterAnnotations(provider, node, source_ref)

    function_creation = ExpressionFunctionCreation(
        function_ref=ExpressionFunctionRef(function_body=outer_body,
                                           source_ref=source_ref),
        code_object=code_object,
        defaults=defaults,
        kw_defaults=kw_defaults,
        annotations=annotations,
        source_ref=source_ref)

    # Add the "staticmethod" decorator to __new__ methods if not provided.

    # CPython made these optional, but secretly applies them when it does
    # "class __new__".  We add them earlier, so our optimization will see it.
    if node.name == "__new__" and \
       provider.isExpressionClassBody():

        for decorator in decorators:
            if decorator.isExpressionVariableRef() and \
               decorator.getVariableName() == "staticmethod":
                break
        else:
            decorators.append(
                ExpressionBuiltinRef(builtin_name="staticmethod",
                                     source_ref=source_ref))

    if python_version >= 360 and \
       node.name == "__init_subclass__" and \
       provider.isExpressionClassBody():

        for decorator in decorators:
            if decorator.isExpressionVariableRef() and \
               decorator.getVariableName() == "classmethod":
                break
        else:
            decorators.append(
                ExpressionBuiltinRef(builtin_name="classmethod",
                                     source_ref=source_ref))

    decorated_function = function_creation
    for decorator in decorators:
        decorated_function = ExpressionCallNoKeywords(
            called=decorator,
            args=ExpressionMakeTuple(elements=(decorated_function, ),
                                     source_ref=source_ref),
            source_ref=decorator.getSourceReference())

    result = StatementAssignmentVariable(
        variable_ref=ExpressionTargetVariableRef(variable_name=mangleName(
            node.name, provider),
                                                 source_ref=source_ref),
        source=decorated_function,
        source_ref=source_ref)

    if python_version >= 340:
        function_body.qualname_setup = result.getTargetVariableRef()

    return result
コード例 #8
0
def buildAsyncFunctionNode(provider, node, source_ref):
    # We are creating a function here that creates coroutine objects, with
    # many details each, pylint: disable=R0914
    assert getKind(node) == "AsyncFunctionDef"

    function_statement_nodes, function_doc = extractDocFromBody(node)

    creator_function_body, _, code_object = buildFunctionWithParsing(
        provider=provider,
        function_kind="Coroutine",
        name=node.name,
        flags=set(),
        function_doc=function_doc,
        node=node,
        source_ref=source_ref)

    function_body = ExpressionCoroutineObjectBody(
        provider=creator_function_body,
        name=node.name,
        flags=set(),
        source_ref=source_ref)

    decorators = buildNodeList(provider=provider,
                               nodes=reversed(node.decorator_list),
                               source_ref=source_ref)

    defaults = buildNodeList(provider=provider,
                             nodes=node.args.defaults,
                             source_ref=source_ref)

    function_statements_body = buildFrameNode(provider=function_body,
                                              nodes=function_statement_nodes,
                                              code_object=code_object,
                                              source_ref=source_ref)

    function_statements_body = _insertFinalReturnStatement(
        function_statements_body=function_statements_body,
        return_class=StatementGeneratorReturn,
        source_ref=source_ref)

    if function_statements_body.isStatementsFrame():
        function_statements_body = makeStatementsSequenceFromStatement(
            statement=function_statements_body)

    function_body.setBody(function_statements_body)

    annotations = buildParameterAnnotations(provider, node, source_ref)

    kw_defaults = buildParameterKwDefaults(provider=provider,
                                           node=node,
                                           function_body=creator_function_body,
                                           source_ref=source_ref)

    creator_function_body.setBody(
        makeStatementsSequenceFromStatement(
            statement=StatementReturn(expression=ExpressionMakeCoroutineObject(
                coroutine_ref=ExpressionFunctionRef(
                    function_body=function_body, source_ref=source_ref),
                code_object=code_object,
                source_ref=source_ref),
                                      source_ref=source_ref)))

    function_creation = ExpressionFunctionCreation(
        function_ref=ExpressionFunctionRef(function_body=creator_function_body,
                                           source_ref=source_ref),
        code_object=code_object,
        defaults=defaults,
        kw_defaults=kw_defaults,
        annotations=annotations,
        source_ref=source_ref)

    decorated_function = function_creation
    for decorator in decorators:
        decorated_function = ExpressionCallNoKeywords(
            called=decorator,
            args=ExpressionMakeTuple(elements=(decorated_function, ),
                                     source_ref=source_ref),
            source_ref=decorator.getSourceReference())

    result = StatementAssignmentVariable(
        variable_ref=ExpressionTargetVariableRef(variable_name=mangleName(
            node.name, provider),
                                                 source_ref=source_ref),
        source=decorated_function,
        source_ref=source_ref)

    function_body.qualname_setup = result.getTargetVariableRef()

    return result
コード例 #9
0
ファイル: Building.py プロジェクト: iplayfast/Nuitka
def buildParseTree(provider, source_code, source_ref, is_module, is_main):
    # There are a bunch of branches here, mostly to deal with version
    # differences for module default variables.

    body = parseSourceCodeToAst(
        source_code = source_code,
        filename    = source_ref.getFilename(),
        line_offset = source_ref.getLineNumber() - 1
    )
    body, doc = extractDocFromBody(body)

    result = buildStatementsNode(
        provider   = provider,
        nodes      = body,
        source_ref = source_ref
    )

    checkFutureImportsOnlyAtStart(body)

    internal_source_ref = source_ref.atInternal()

    statements = []

    if is_module:
        # Add import of "site" module of main programs visibly in the node tree,
        # so recursion and optimization can pick it up, checking its effects.
        if is_main and "no_site" not in Options.getPythonFlags():
            for path_imported_name in getPthImportedPackages():
                statements.append(
                    StatementExpressionOnly(
                        expression = ExpressionImportModule(
                            module_name = path_imported_name,
                            import_list = (),
                            level       = 0,
                            source_ref  = source_ref,
                        ),
                        source_ref = source_ref
                    )
                )

            statements.append(
                StatementExpressionOnly(
                    expression = ExpressionImportModule(
                        module_name = "site",
                        import_list = (),
                        level       = 0,
                        source_ref  = source_ref,
                    ),
                    source_ref = source_ref
                )
            )

        statements.append(
            StatementAssignmentVariable(
                variable_ref = ExpressionTargetVariableRef(
                    variable_name = "__doc__",
                    source_ref    = internal_source_ref
                ),
                source       = makeConstantRefNode(
                    constant      = doc,
                    source_ref    = internal_source_ref,
                    user_provided = True
                ),
                source_ref   = internal_source_ref
            )
        )

        statements.append(
            StatementAssignmentVariable(
                variable_ref = ExpressionTargetVariableRef(
                    variable_name = "__file__",
                    source_ref    = internal_source_ref
                ),
                source       = ExpressionModuleFileAttributeRef(
                    source_ref = internal_source_ref,
                ),
                source_ref   = internal_source_ref
            )
        )

        if provider.isCompiledPythonPackage():
            # This assigns "__path__" value.
            statements.append(
                createPathAssignment(internal_source_ref)
            )

    if python_version >= 300:
        statements.append(
            StatementAssignmentVariable(
                variable_ref = ExpressionTargetVariableRef(
                    variable_name = "__cached__",
                    source_ref    = internal_source_ref
                ),
                source       = ExpressionConstantNoneRef(
                    source_ref    = internal_source_ref,
                    user_provided = True
                ),
                source_ref   = internal_source_ref
            )
        )


    if python_version >= 330:
        # For Python3.3, it's set for both packages and non-packages.
        statements.append(
            StatementAssignmentVariable(
                variable_ref = ExpressionTargetVariableRef(
                    variable_name = "__package__",
                    source_ref    = internal_source_ref
                ),
                source       = makeConstantRefNode(
                    constant      = provider.getFullName()
                                      if provider.isCompiledPythonPackage() else
                                    provider.getPackage(),
                    source_ref    = internal_source_ref,
                    user_provided = True
                ),
                source_ref   = internal_source_ref
            )
        )

    needs__initializing__ = not provider.isMainModule() and \
      (python_version >= 330 and python_version < 340)

    if needs__initializing__:
        # Set "__initializing__" at the beginning to True
        statements.append(
            StatementAssignmentVariable(
                variable_ref = ExpressionTargetVariableRef(
                    variable_name = "__initializing__",
                    source_ref    = internal_source_ref
                ),
                source       = makeConstantRefNode(
                    constant      = True,
                    source_ref    = internal_source_ref,
                    user_provided = True
                ),
                source_ref   = internal_source_ref
            )
        )

    if python_version >= 360:
        # Set "__annotations__" on module level to {}
        statements.append(
            StatementAssignmentVariable(
                variable_ref = ExpressionTargetVariableRef(
                    variable_name = "__annotations__",
                    source_ref    = internal_source_ref
                ),
                source       = makeConstantRefNode(
                    constant      = {},
                    source_ref    = internal_source_ref,
                    user_provided = True
                ),
                source_ref   = internal_source_ref
            )
        )


    # Now the module body if there is any at all.
    if result is not None:
        statements.extend(
            result.getStatements()
        )

    if needs__initializing__:
        # Set "__initializing__" at the end to False
        statements.append(
            StatementAssignmentVariable(
                variable_ref = ExpressionTargetVariableRef(
                    variable_name = "__initializing__",
                    source_ref    = internal_source_ref
                ),
                source       = makeConstantRefNode(
                    constant      = False,
                    source_ref    = internal_source_ref,
                    user_provided = True
                ),
                source_ref   = internal_source_ref
            )
        )


    if is_module:
        return makeModuleFrame(
            module     = provider,
            statements = statements,
            source_ref = source_ref
        )
    else:
        assert False
コード例 #10
0
ファイル: Building.py プロジェクト: gitter-badger/Nuitka
def buildParseTree(provider, source_code, source_ref, is_module, is_main):
    # There are a bunch of branches here, mostly to deal with version
    # differences for module default variables. pylint: disable=R0912

    # Workaround: ast.parse cannot cope with some situations where a file is not
    # terminated by a new line.
    if not source_code.endswith('\n'):
        source_code = source_code + '\n'

    try:
        body = ast.parse(source_code, source_ref.getFilename())
    except SyntaxError as e:
        _makeSyntaxErrorCompatible(e)

        raise e

    assert getKind(body) == "Module"

    line_offset = source_ref.getLineNumber() - 1

    if line_offset > 0:
        for created_node in ast.walk(body):
            if hasattr(created_node, "lineno"):
                created_node.lineno += line_offset

    body, doc = extractDocFromBody(body)

    result = buildStatementsNode(provider=provider,
                                 nodes=body,
                                 source_ref=source_ref)

    checkFutureImportsOnlyAtStart(body)

    internal_source_ref = source_ref.atInternal()

    statements = []

    if is_module:
        # Add import of "site" module of main programs visibly in the node tree,
        # so recursion and optimization can pick it up, checking its effects.
        if is_main and "no_site" not in Options.getPythonFlags():
            for path_imported_name in getPthImportedPackages():
                statements.append(
                    StatementExpressionOnly(expression=ExpressionImportModule(
                        module_name=path_imported_name,
                        import_list=(),
                        level=0,
                        source_ref=source_ref,
                    ),
                                            source_ref=source_ref))

            statements.append(
                StatementExpressionOnly(expression=ExpressionImportModule(
                    module_name="site",
                    import_list=(),
                    level=0,
                    source_ref=source_ref,
                ),
                                        source_ref=source_ref))

        statements.append(
            StatementAssignmentVariable(
                variable_ref=ExpressionTargetVariableRef(
                    variable_name="__doc__", source_ref=internal_source_ref),
                source=ExpressionConstantRef(constant=doc,
                                             source_ref=internal_source_ref,
                                             user_provided=True),
                source_ref=internal_source_ref))

        statements.append(
            StatementAssignmentVariable(
                variable_ref=ExpressionTargetVariableRef(
                    variable_name="__file__", source_ref=internal_source_ref),
                source=ExpressionModuleFileAttributeRef(
                    source_ref=internal_source_ref, ),
                source_ref=internal_source_ref))

        if provider.isCompiledPythonPackage():
            # This assigns "__path__" value.
            statements.append(createPathAssignment(internal_source_ref))

    if python_version >= 300:
        statements.append(
            StatementAssignmentVariable(
                variable_ref=ExpressionTargetVariableRef(
                    variable_name="__cached__",
                    source_ref=internal_source_ref),
                source=ExpressionConstantRef(constant=None,
                                             source_ref=internal_source_ref,
                                             user_provided=True),
                source_ref=internal_source_ref))

    if python_version >= 330:
        # For Python3.3, it's set for both packages and non-packages.
        statements.append(
            StatementAssignmentVariable(
                variable_ref=ExpressionTargetVariableRef(
                    variable_name="__package__",
                    source_ref=internal_source_ref),
                source=ExpressionConstantRef(
                    constant=provider.getFullName()
                    if provider.isCompiledPythonPackage() else
                    provider.getPackage(),
                    source_ref=internal_source_ref,
                    user_provided=True),
                source_ref=internal_source_ref))

    needs__initializing__ = not provider.isMainModule() and \
      (python_version >= 330 and python_version < 340)

    if needs__initializing__:
        # Set "__initializing__" at the beginning to True
        statements.append(
            StatementAssignmentVariable(
                variable_ref=ExpressionTargetVariableRef(
                    variable_name="__initializing__",
                    source_ref=internal_source_ref),
                source=ExpressionConstantRef(constant=True,
                                             source_ref=internal_source_ref,
                                             user_provided=True),
                source_ref=internal_source_ref))

    # Now the module body if there is any at all.
    if result is not None:
        statements.extend(result.getStatements())

    if needs__initializing__:
        # Set "__initializing__" at the end to False
        statements.append(
            StatementAssignmentVariable(
                variable_ref=ExpressionTargetVariableRef(
                    variable_name="__initializing__",
                    source_ref=internal_source_ref),
                source=ExpressionConstantRef(constant=False,
                                             source_ref=internal_source_ref,
                                             user_provided=True),
                source_ref=internal_source_ref))

    if is_module:
        return makeModuleFrame(module=provider,
                               statements=statements,
                               source_ref=source_ref)
    else:
        assert False
コード例 #11
0
def buildImportFromNode(provider, node, source_ref):
    # "from .. import .." statements. This may trigger a star import, or
    # multiple names being looked up from the given module variable name.

    module_name = node.module if node.module is not None else ""
    level = node.level

    # Importing from "__future__" module may enable flags to the parser,
    # that we need to know.
    if module_name == "__future__":
        # Future imports we see are all legal, and known to work.

        if not provider.isCompiledPythonModule():
            SyntaxErrors.raiseSyntaxError(
                reason     = """\
from __future__ imports must occur at the beginning of the file""",
                col_offset = 8
                  if Utils.python_version >= 300 or \
                  not Options.isFullCompat()
                else None,
                source_ref = source_ref
            )

        for import_desc in node.names:
            object_name, _local_name = import_desc.name, import_desc.asname

            enableFutureFeature(object_name=object_name,
                                future_spec=source_ref.getFutureSpec(),
                                source_ref=source_ref)

        # Remember it for checks to be applied once module is complete.
        node.source_ref = source_ref
        _future_import_nodes.append(node)

    target_names = []
    import_names = []

    for import_desc in node.names:
        object_name, local_name = import_desc.name, import_desc.asname

        if object_name == '*':
            target_names.append(None)
        else:
            target_names.append(
                local_name if local_name is not None else object_name)

        import_names.append(object_name)

    if None in target_names:
        # More than "*" is a syntax error in Python, need not care about this at
        # all, it's only allowed value for import list in  this case.
        assert target_names == [None]

        # Python3 made this a syntax error unfortunately.
        if not provider.isCompiledPythonModule(
        ) and Utils.python_version >= 300:
            SyntaxErrors.raiseSyntaxError(
                "import * only allowed at module level",
                provider.getSourceReference())

        if provider.isExpressionFunctionBody():
            provider.markAsStarImportContaining()

        return StatementImportStar(module_import=ExpressionImportModule(
            module_name=module_name,
            import_list=('*', ),
            level=level,
            source_ref=source_ref),
                                   source_ref=source_ref)
    else:
        # Make __future__ imports "hard" immediately, they cannot be any other
        # way.
        def makeImportName(import_name):
            if module_name == "__future__":
                return ExpressionImportModuleHard(module_name="__future__",
                                                  import_name=import_name,
                                                  source_ref=source_ref)
            else:
                # TODO: This ought to use a temporary variable for multiple
                # names, instead of importing multiple times.

                return ExpressionImportName(module=ExpressionImportModule(
                    module_name=module_name,
                    import_list=tuple(import_names),
                    level=level,
                    source_ref=source_ref),
                                            import_name=import_name,
                                            source_ref=source_ref)

        import_nodes = []

        for target_name, import_name in zip(target_names, import_names):
            import_nodes.append(
                StatementAssignmentVariable(
                    variable_ref=ExpressionTargetVariableRef(
                        variable_name=mangleName(target_name, provider),
                        source_ref=source_ref),
                    source=makeImportName(import_name=import_name, ),
                    source_ref=source_ref))

        # Note: Each import is sequential. It can succeed, and the failure of a
        # later one is not changing one. We can therefore have a sequence of
        # imports that only import one thing therefore.
        return StatementsSequence(statements=import_nodes,
                                  source_ref=source_ref)
コード例 #12
0
def buildImportFromNode(provider, node, source_ref):
    # "from .. import .." statements. This may trigger a star import, or
    # multiple names being looked up from the given module variable name.
    # This is pretty complex, pylint: disable=R0912,R0914

    module_name = node.module if node.module is not None else ""
    level = node.level

    # Importing from "__future__" module may enable flags to the parser,
    # that we need to know about, handle that.
    if module_name == "__future__":
        _handleFutureImport(provider, node, source_ref)

    target_names = []
    import_names = []

    # Mapping imported "fromlist" to assigned "fromlist" if any, handling the
    # star case as well.
    for import_desc in node.names:
        object_name, local_name = import_desc.name, import_desc.asname

        if object_name == '*':
            target_names.append(None)
            assert local_name is None
        else:
            target_names.append(
                local_name if local_name is not None else object_name)

        import_names.append(object_name)

    # Star imports get special treatment.
    if None in target_names:
        # More than "*" is a syntax error in Python, need not care about this at
        # all, it's only allowed value for import list in  this case.
        assert target_names == [None]

        # Python3 made it so that these can only occur on the module level,
        # so this a syntax error if not there. For Python2 it is OK to
        # occur everywhere though.
        if not provider.isCompiledPythonModule() and python_version >= 300:
            SyntaxErrors.raiseSyntaxError(
                "import * only allowed at module level",
                provider.getSourceReference())

        # Functions with star imports get a marker.
        if provider.isExpressionFunctionBody():
            provider.markAsStarImportContaining()

        return StatementImportStar(module_import=ExpressionImportModule(
            module_name=module_name,
            import_list=('*', ),
            level=level,
            source_ref=source_ref),
                                   source_ref=source_ref)
    else:

        def makeImportName(import_name):
            if module_name == "__future__":
                # Make "__future__" imports tie hard immediately, they cannot be
                # any other way.
                return ExpressionImportModuleHard(module_name="__future__",
                                                  import_name=import_name,
                                                  source_ref=source_ref)
            else:
                # Refer to be module, or a clone of the reference if need be.
                return ExpressionImportName(module=imported_from_module,
                                            import_name=import_name,
                                            source_ref=source_ref)

        imported_from_module = ExpressionImportModule(
            module_name=module_name,
            import_list=tuple(import_names),
            level=level,
            source_ref=source_ref)

        # If we have multiple names to import, consider each.
        multi_names = len(target_names) > 1

        statements = []

        if multi_names:
            tmp_import_from = provider.allocateTempVariable(
                temp_scope=provider.allocateTempScope("import_from"),
                name="module")

            statements.append(
                StatementAssignmentVariable(
                    variable_ref=ExpressionTargetTempVariableRef(
                        variable=tmp_import_from, source_ref=source_ref),
                    source=imported_from_module,
                    source_ref=source_ref))

            imported_from_module = ExpressionTempVariableRef(
                variable=tmp_import_from, source_ref=source_ref)

        import_statements = []
        first = True

        for target_name, import_name in zip(target_names, import_names):
            # Make a clone of the variable reference, if we are going to use
            # another one.
            if not first:
                imported_from_module = imported_from_module.makeClone()
            first = False

            import_statements.append(
                StatementAssignmentVariable(
                    variable_ref=ExpressionTargetVariableRef(
                        variable_name=mangleName(target_name, provider),
                        source_ref=source_ref),
                    source=makeImportName(import_name=import_name, ),
                    source_ref=source_ref))

        # Release the temporary module value as well.
        if multi_names:
            statements.append(
                makeTryFinallyStatement(provider=provider,
                                        tried=import_statements,
                                        final=(StatementReleaseVariable(
                                            variable=tmp_import_from,
                                            source_ref=source_ref), ),
                                        source_ref=source_ref))
        else:
            statements.extend(import_statements)

        # Note: Each import is sequential. It can succeed, and the failure of a
        # later one is not undoing previous ones. We can therefore have a
        # sequence of imports that each only import one thing therefore.
        return StatementsSequence(statements=mergeStatements(statements),
                                  source_ref=source_ref)
def decodeAssignTarget(provider, node, source_ref, allow_none = False):
    # Many cases to deal with, because of the different assign targets,
    # pylint: disable=too-many-branches,too-many-return-statements

    if node is None and allow_none:
        return None

    if type(node) is str:
        return "Name", ExpressionTargetVariableRef(
            variable_name = mangleName(node, provider),
            source_ref    = source_ref
        )

    kind = getKind(node)

    if hasattr(node, "ctx"):
        assert getKind(node.ctx) in ("Store", "Del")

    if kind == "Name":
        return kind, ExpressionTargetVariableRef(
            variable_name = mangleName(node.id, provider),
            source_ref    = source_ref
        )
    elif kind == "Attribute":
        return kind, (
            buildNode(provider, node.value, source_ref),
            node.attr
        )
    elif kind == "Subscript":
        slice_kind = getKind(node.slice)

        if slice_kind == "Index":
            return "Subscript", (
                buildNode(provider, node.value, source_ref),
                buildNode(provider, node.slice.value, source_ref)
            )
        elif slice_kind == "Slice":
            lower = buildNode(provider, node.slice.lower, source_ref, True)
            upper = buildNode(provider, node.slice.upper, source_ref, True)

            if node.slice.step is not None:
                step = buildNode(provider, node.slice.step, source_ref)

                return "Subscript", (
                    buildNode(provider, node.value, source_ref),
                    ExpressionBuiltinSlice(
                        start      = lower,
                        stop       = upper,
                        step       = step,
                        source_ref = source_ref
                    )
                )
            else:
                return "Slice", (
                    buildNode(provider, node.value, source_ref),
                    lower,
                    upper
                )
        elif slice_kind == "ExtSlice":
            return "Subscript", (
                buildNode(provider, node.value, source_ref),
                buildExtSliceNode(provider, node, source_ref)
            )
        elif slice_kind == "Ellipsis":
            return "Subscript", (
                buildNode(provider, node.value, source_ref),
                ExpressionConstantEllipsisRef(
                    source_ref = source_ref
                )
            )
        else:
            assert False, slice_kind
    elif kind in ("Tuple", "List"):
        return "Tuple", tuple(
            decodeAssignTarget(
                provider   = provider,
                node       = sub_node,
                source_ref = source_ref,
                allow_none = False
            )
            for sub_node in
            node.elts
        )
    elif kind == "Starred":
        return "Starred", decodeAssignTarget(
            provider   = provider,
            node       = node.value,
            source_ref = source_ref,
            allow_none = False
        )
    else:
        assert False, (source_ref, kind)
コード例 #14
0
def buildFunctionNode(provider, node, source_ref):
    assert getKind(node) == "FunctionDef"

    function_statements, function_doc = extractDocFromBody(node)

    function_body = ExpressionFunctionBody(provider=provider,
                                           name=node.name,
                                           doc=function_doc,
                                           parameters=buildParameterSpec(
                                               provider, node.name, node,
                                               source_ref),
                                           is_class=False,
                                           source_ref=source_ref)

    decorators = buildNodeList(provider=provider,
                               nodes=reversed(node.decorator_list),
                               source_ref=source_ref)

    defaults = buildNodeList(provider=provider,
                             nodes=node.args.defaults,
                             source_ref=source_ref)

    kw_defaults = buildParameterKwDefaults(provider=provider,
                                           node=node,
                                           function_body=function_body,
                                           source_ref=source_ref)

    function_statements_body = buildStatementsNode(provider=function_body,
                                                   nodes=function_statements,
                                                   frame=True,
                                                   source_ref=source_ref)

    if function_body.isGenerator():
        # TODO: raise generator exit?
        pass
    else:
        function_statements_body = _insertFinalReturnStatement(
            function_statements_body=function_statements_body,
            source_ref=source_ref)

    if function_statements_body.isStatementsFrame():
        function_statements_body = makeStatementsSequenceFromStatement(
            statement=function_statements_body)

    function_body.setBody(function_statements_body)

    annotations = buildParameterAnnotations(provider, node, source_ref)

    function_creation = ExpressionFunctionCreation(
        function_ref=ExpressionFunctionRef(function_body=function_body,
                                           source_ref=source_ref),
        defaults=defaults,
        kw_defaults=kw_defaults,
        annotations=annotations,
        source_ref=source_ref)

    # Add the "staticmethod" decorator to __new__ methods if not provided.

    # CPython made these optional, but secretly applies them when it does
    # "class __new__".  We add them earlier, so our optimization will see it.
    if node.name == "__new__" and \
       not decorators and \
       provider.isExpressionClassBody():

        decorators = (ExpressionBuiltinRef(builtin_name="staticmethod",
                                           source_ref=source_ref), )

    decorated_function = function_creation
    for decorator in decorators:
        decorated_function = ExpressionCallNoKeywords(
            called=decorator,
            args=ExpressionMakeTuple(elements=(decorated_function, ),
                                     source_ref=source_ref),
            source_ref=decorator.getSourceReference())

    result = StatementAssignmentVariable(
        variable_ref=ExpressionTargetVariableRef(variable_name=mangleName(
            node.name, provider),
                                                 source_ref=source_ref),
        source=decorated_function,
        source_ref=source_ref)

    if Utils.python_version >= 340:
        function_body.qualname_setup = result.getTargetVariableRef()

    return result