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)
Exemple #2
0
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 not sys.flags.no_site:
            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.isPythonPackage():
            # This assigns "__path__" value.
            statements.append(
                createPathAssignment(internal_source_ref)
            )

    if Utils.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 Utils.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.isPythonPackage() else
                                    provider.getPackage(),
                    source_ref    = internal_source_ref,
                    user_provided = True
                ),
                source_ref   = internal_source_ref
            )
        )

    needs__initializing__ = not provider.isMainModule() and \
      (Utils.python_version >= 330 and Utils.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:
        result = makeModuleFrame(
            module     = provider,
            statements = statements,
            source_ref = source_ref
        )

        applyLaterWrappers()

        return result
    else:
        assert False
Exemple #3
0
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=too-many-branches

    pushFutureSpec()
    if is_module:
        provider.future_spec = getFutureSpec()

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

    if is_module and is_main and python_version >= 360:
        provider.markAsNeedsAnnotationsDictionary()

    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=makeAbsoluteImportNode(
                        module_name=path_imported_name,
                        source_ref=source_ref,
                    ),
                                            source_ref=source_ref))

            statements.append(
                StatementExpressionOnly(expression=makeAbsoluteImportNode(
                    module_name="site",
                    source_ref=source_ref,
                ),
                                        source_ref=source_ref))

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

        statements.append(
            StatementAssignmentVariableName(
                variable_name="__file__",
                source=ExpressionModuleFileAttributeRef(
                    source_ref=internal_source_ref, ),
                source_ref=internal_source_ref))

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

    if python_version >= 330:
        statements.append(
            StatementAssignmentVariableName(
                variable_name="__loader__",
                source=ExpressionModuleLoaderRef(
                    source_ref=internal_source_ref, ),
                source_ref=internal_source_ref))

    if python_version >= 340:
        if provider.isMainModule():
            spec_value = makeConstantRefNode(constant=None,
                                             source_ref=internal_source_ref)
        else:
            spec_value = makeCallNode(
                ExpressionImportModuleNameHard(
                    module_name="importlib._bootstrap",
                    import_name="ModuleSpec",
                    source_ref=internal_source_ref,
                ),
                makeConstantRefNode(constant=provider.getFullName(),
                                    source_ref=internal_source_ref,
                                    user_provided=True),
                ExpressionModuleLoaderRef(source_ref=internal_source_ref, ),
                internal_source_ref)

        statements.append(
            StatementAssignmentVariableName(variable_name="__spec__",
                                            source=spec_value,
                                            source_ref=internal_source_ref))

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

    # The "__package__" attribute is set to proper value for 3.3 or higher even
    # for normal modules, previously for packages only.
    statements.append(
        StatementAssignmentVariableName(
            variable_name="__package__",
            source=makeConstantRefNode(
                constant=((None if '.' not in provider.getFullName() or
                           python_version >= 320 else provider.getFullName())
                          if python_version < 330 else provider.getFullName())
                if provider.isCompiledPythonPackage() else
                (provider.getPackage() if python_version >= 330 else None),
                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(
            StatementAssignmentVariableName(variable_name="__initializing__",
                                            source=makeConstantRefNode(
                                                constant=True,
                                                source_ref=internal_source_ref,
                                                user_provided=True),
                                            source_ref=internal_source_ref))

    if provider.needsAnnotationsDictionary():
        # Set "__annotations__" on module level to {}
        statements.append(
            StatementAssignmentVariableName(variable_name="__annotations__",
                                            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(
            StatementAssignmentVariableName(variable_name="__initializing__",
                                            source=makeConstantRefNode(
                                                constant=False,
                                                source_ref=internal_source_ref,
                                                user_provided=True),
                                            source_ref=internal_source_ref))

    if is_module:
        result = makeModuleFrame(module=provider,
                                 statements=statements,
                                 source_ref=source_ref)

        popFutureSpec()

        return result
    else:
        assert False
Exemple #4
0
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
def createPathAssignment(package, source_ref):
    if Options.getFileReferenceMode() == "original":
        path_value = makeConstantRefNode(
            constant      = [
                os.path.dirname(
                    source_ref.getFilename()
                )
            ],
            source_ref    = source_ref,
            user_provided = True
        )
    else:
        elements = [
            ExpressionCallNoKeywords(
                called     = ExpressionAttributeLookup(
                    source         = ExpressionImportModuleNameHard(
                        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,
            )
        ]

        def makeCall(module_name, import_name, attribute_name, *args):
            return ExpressionCallNoKeywords(
                called     = ExpressionAttributeLookup(
                    source         = ExpressionImportModuleNameHard(
                        module_name = module_name,
                        import_name = import_name,
                        source_ref  = source_ref
                    ),
                    attribute_name = attribute_name,
                    source_ref     = source_ref
                ),
                args       = ExpressionMakeTuple(
                    elements   = args,
                    source_ref = source_ref
                ),
                source_ref = source_ref
            )

        if package.canHaveExternalImports():
            parts = package.getFullName().split('.')

            for count in range(len(parts)):
                path_part = makeCall(
                    "os", "environ", "get",
                        makeConstantRefNode(
                            constant   = "NUITKA_PACKAGE_%s" % '_'.join(
                                parts[:count+1]
                            ),
                            source_ref = source_ref,
                        ),
                        makeConstantRefNode(
                            constant   = "/notexist",
                            source_ref = source_ref,
                        )
                )

                if parts[count+1:]:
                    path_part = makeCall(
                        "os", "path", "join",
                        path_part,
                        makeConstantRefNode(
                            constant   = os.path.join(*parts[count+1:]),
                            source_ref = source_ref,
                        )
                    )

                elements.append(path_part)

        path_value = ExpressionMakeList(
            elements   = elements,
            source_ref = source_ref
        )

    return StatementAssignmentVariableName(
        variable_name = "__path__",
        source        = path_value,
        source_ref    = source_ref
    )