Ejemplo n.º 1
0
def getModuleCode(module_context, template_values):
    header = template_global_copyright % {
        "name"    : module_context.getName(),
        "version" : getNuitkaVersion(),
        "year"    : getNuitkaVersionYear()
    }

    decls, inits, checks = getConstantInitCodes(module_context)

    if module_context.needsModuleFilenameObject():
        decls.append("static PyObject *module_filename_obj;")

    template_values["constant_decl_codes"] = indented(
        decls,
        0
    )

    template_values["constant_init_codes"] = indented(
        inits,
        1
    )

    template_values["constant_check_codes"] = indented(
        checks,
        1
    )

    return header + template_module_body_template % template_values
Ejemplo n.º 2
0
def getModuleCode(module_context, template_values):
    header = template_global_copyright % {
        "name": module_context.getName(),
        "version": getNuitkaVersion(),
        "year": getNuitkaVersionYear(),
    }

    decls, inits, checks = getConstantInitCodes(module_context)

    if module_context.needsModuleFilenameObject():
        decls.append("static PyObject *module_filename_obj;")

    template_values["constant_decl_codes"] = indented(decls, 0)

    template_values["constant_init_codes"] = indented(inits, 1)

    template_values["constant_check_codes"] = indented(checks, 1)

    is_top = template_values["is_top"]
    del template_values["is_top"]

    result = header + template_module_body_template % template_values

    if is_top == 1 and Options.shallMakeModule():
        result += template_module_external_entry_point % {
            "module_name": template_values["module_name"],
            "module_identifier": template_values["module_identifier"],
        }

    return result
Ejemplo n.º 3
0
def getModuleCode(module_context, template_values):
    header = template_global_copyright % {
        "name": module_context.getName(),
        "version": getNuitkaVersion(),
        "year": getNuitkaVersionYear(),
    }

    decls, inits, checks = getConstantInitCodes(module_context)

    if module_context.needsModuleFilenameObject():
        decls.append("static PyObject *module_filename_obj;")

    template_values["constant_decl_codes"] = indented(decls, 0)

    template_values["constant_init_codes"] = indented(inits, 1)

    template_values["constant_check_codes"] = indented(checks, 1)

    return header + template_module_body_template % template_values
Ejemplo n.º 4
0
def getModuleCode(module, function_decl_codes, function_body_codes, context):

    # For the module code, lots of arguments and attributes come together.
    # pylint: disable=too-many-locals

    # Temporary variable initializations
    # TODO: Move that to a place outside of functions.
    from .FunctionCodes import (
        finalizeFunctionLocalVariables,
        setupFunctionLocalVariables,
    )

    setupFunctionLocalVariables(
        context=context,
        parameters=None,
        closure_variables=(),
        user_variables=module.getOutlineLocalVariables(),
        temp_variables=module.getTempVariables(),
    )

    module_codes = Emission.SourceCodeCollector()

    module = context.getOwner()
    module_body = module.getBody()

    generateStatementSequenceCode(
        statement_sequence=module_body,
        emit=module_codes,
        allow_none=True,
        context=context,
    )

    for _identifier, code in sorted(iterItems(context.getHelperCodes())):
        function_body_codes.append(code)

    for _identifier, code in sorted(iterItems(context.getDeclarations())):
        function_decl_codes.append(code)

    function_body_codes = "\n\n".join(function_body_codes)
    function_decl_codes = "\n\n".join(function_decl_codes)

    _cleanup = finalizeFunctionLocalVariables(context)

    # TODO: Seems like a bug, classes could produce those.
    # assert not _cleanup, _cleanup

    local_var_inits = context.variable_storage.makeCFunctionLevelDeclarations()

    if module_body is not None and module_body.mayRaiseException(
            BaseException):
        module_exit = template_module_exception_exit
    else:
        module_exit = template_module_noexception_exit

    function_table_entries_decl = []
    for func_impl_identifier in context.getFunctionCreationInfos():
        function_table_entries_decl.append("%s," % func_impl_identifier)

    module_name = module.getFullName()

    is_package = module.isCompiledPythonPackage()
    is_top = module.isTopModule()

    module_identifier = module.getCodeName()

    template = template_global_copyright + template_module_body_template

    if is_top == 1 and Options.shallMakeModule():
        template += template_module_external_entry_point

    module_code_objects_decl = getCodeObjectsDeclCode(context)
    module_code_objects_init = getCodeObjectsInitCode(context)

    return template % {
        "module_name":
        module_name,
        "version":
        getNuitkaVersion(),
        "year":
        getNuitkaVersionYear(),
        "is_main_module":
        1 if module.isMainModule() else 0,
        "is_dunder_main":
        1 if module_name == "__main__" and os.path.basename(
            module.getCompileTimeFilename()) == "__main__.py" else 0,
        "is_package":
        1 if is_package else 0,
        "module_identifier":
        module_identifier,
        "module_functions_decl":
        function_decl_codes,
        "module_functions_code":
        function_body_codes,
        "module_function_table_entries":
        indented(function_table_entries_decl),
        "temps_decl":
        indented(local_var_inits),
        "module_code":
        indented(module_codes.codes),
        "module_exit":
        module_exit,
        "module_code_objects_decl":
        indented(module_code_objects_decl, 0),
        "module_code_objects_init":
        indented(module_code_objects_init, 1),
        "constants_count":
        context.getConstantsCount(),
    }