def onModuleInitialSet(self):
        from nuitka.importing.ImportCache import addImportedModule
        from nuitka.ModuleRegistry import getRootTopModule
        from nuitka.plugins.Plugins import Plugins
        from nuitka.tree.Building import (
            CompiledPythonModule,
            createModuleTree,
            readSourceCodeFromFilename,
        )

        # First, build the module node and then read again from the
        # source code.
        root_module = getRootTopModule()

        module_name = ModuleName("__parents_main__")
        source_ref = root_module.getSourceReference()

        mode = Plugins.decideCompilation(module_name, source_ref)

        slave_main_module = CompiledPythonModule(
            module_name=module_name,
            is_top=False,
            mode=mode,
            future_spec=None,
            source_ref=root_module.getSourceReference(),
        )

        source_code = readSourceCodeFromFilename(module_name,
                                                 root_module.getFilename())

        # For the call stack, this may look bad or different to what
        # CPython does. Using the "__import__" built-in to not spoil
        # or use the module namespace. The forking module was split up
        # into multiple modules in Python 3.4.
        if python_version >= 0x340:
            source_code += """
__import__("sys").modules["__main__"] = __import__("sys").modules[__name__]
__import__("multiprocessing.spawn").spawn.freeze_support()"""
        else:
            source_code += """
__import__("sys").modules["__main__"] = __import__("sys").modules[__name__]
__import__("multiprocessing.forking").forking.freeze_support()"""

        createModuleTree(
            module=slave_main_module,
            source_ref=root_module.getSourceReference(),
            source_code=source_code,
            is_main=False,
        )

        addImportedModule(imported_module=slave_main_module)

        yield slave_main_module
Example #2
0
    def createFakeModuleDependency(module):
        full_name = module.getFullName()

        if full_name != "multiprocessing":
            return

        # First, build the module node and then read again from the
        # source code.
        root_module = getRootTopModule()

        module_name = ModuleName("__parents_main__")

        source_code = readSourceCodeFromFilename(module_name,
                                                 root_module.getFilename())

        # For the call stack, this may look bad or different to what
        # CPython does. Using the "__import__" built-in to not spoil
        # or use the module namespace. The forking module was split up
        # into multiple modules in Python 3.4.
        if python_version >= 0x340:
            source_code += """
__import__("sys").modules["__main__"] = __import__("sys").modules[__name__]
# Not needed, and can crash from minor __file__ differences, depending on invocation
__import__("multiprocessing.spawn").spawn._fixup_main_from_path = lambda mod_name : None
__import__("multiprocessing.spawn").spawn.freeze_support()"""
        else:
            source_code += """
__import__("sys").modules["__main__"] = __import__("sys").modules[__name__]
__import__("multiprocessing.forking").forking.freeze_support()"""

        yield (
            module_name,
            source_code,
            root_module.getCompileTimeFilename(),
            "Autoenable multiprocessing freeze support",
        )
Example #3
0
def getInternalModule():
    """Get the singleton internal module."""

    return getRootTopModule()
Example #4
0
def decideRecursion(module_filename,
                    module_name,
                    module_kind,
                    extra_recursion=False):
    # Many branches, which make decisions immediately, by returning
    # pylint: disable=too-many-branches,too-many-return-statements
    if module_name == "__main__":
        return False, "Main program is not followed to a second time."

    # In -m mode, when including the package, do not duplicate main program.
    if (Options.hasPythonFlagPackageMode() and not Options.shallMakeModule()
            and module_name.getBasename() == "__main__"):
        if module_name.getPackageName() == getRootTopModule(
        ).getRuntimePackageValue():
            return False, "Main program is already included in package mode."

    plugin_decision = Plugins.onModuleEncounter(
        module_filename=module_filename,
        module_name=module_name,
        module_kind=module_kind,
    )

    if plugin_decision is not None:
        return plugin_decision

    if module_kind == "extension":
        if Options.isStandaloneMode():
            return True, "Extension module needed for standalone mode."
        else:
            return False, "Extension module cannot be inspected."

    # PGO decisions are not overruling plugins, but all command line options, they are
    # supposed to be applied already.
    is_stdlib = StandardLibrary.isStandardLibraryPath(module_filename)

    if not is_stdlib or Options.shallFollowStandardLibrary():
        # TODO: Bad placement of this function or should PGO also know about
        # bytecode modules loaded or not.
        from nuitka.tree.Building import decideCompilationMode

        if (decideCompilationMode(is_top=False,
                                  module_name=module_name,
                                  for_pgo=True) == "compiled"):
            pgo_decision = decideInclusionFromPGO(
                module_name=module_name,
                module_kind=module_kind,
            )

            if pgo_decision is not None:
                return pgo_decision, "PGO based decision"

    no_case, reason = module_name.matchesToShellPatterns(
        patterns=Options.getShallFollowInNoCase())

    if no_case:
        return (False,
                "Module %s instructed by user to not follow to." % reason)

    any_case, reason = module_name.matchesToShellPatterns(
        patterns=Options.getShallFollowModules())

    if any_case:
        return (True, "Module %s instructed by user to follow to." % reason)

    if Options.shallFollowNoImports():
        return (False, "Instructed by user to not follow at all.")

    if is_stdlib and Options.shallFollowStandardLibrary():
        return (True, "Instructed by user to follow to standard library.")

    if Options.shallFollowAllImports():
        if is_stdlib:
            if StandardLibrary.isStandardLibraryNoAutoInclusionModule(
                    module_name):
                return (
                    True,
                    "Instructed by user to follow all modules, including non-automatic standard library modules.",
                )
        else:
            return (
                True,
                "Instructed by user to follow to all non-standard library modules.",
            )

    # Means, we were not given instructions how to handle things.
    if extra_recursion:
        return (True, "Lives in plug-in directory.")

    if Options.shallMakeModule():
        return (False,
                "Making a module, not following any imports by default.")

    return (None, "Default behavior, not recursing without request.")