Example #1
0
def install():
    loader_details = HookedLoader, [".py"]
    # insert the path hook ahead of other path hooks
    sys.path_hooks.insert(0, FileFinder.path_hook(loader_details))
    # clear any loaders that might already be in use by the FileFinder
    sys.path_importer_cache.clear()
    importlib.invalidate_caches()
Example #2
0
def install(haxe_bin='haxe'):
    HaxeLoader.haxe_bin = haxe_bin
    loader_details = HaxeLoader, [".hx"]

    # insert the path hook ahead of other path hooks
    sys.path_hooks.insert(0, FileFinder.path_hook(loader_details))
    # clear any loaders that might already be in use by the FileFinder
    sys.path_importer_cache.clear()
    invalidate_caches()
Example #3
0
def install() -> None:
    """Installs a loader which is capable of loading and validating strict modules"""
    supported_loaders = _get_supported_file_loaders()

    for index, hook in enumerate(sys.path_hooks):
        if not isinstance(hook, type):
            # pyre-fixme[6]: For 1st param expected `Tuple[Type[Loader], List[str]]`
            #  but got `Tuple[Loader, List[str]]`.
            sys.path_hooks.insert(index, FileFinder.path_hook(*supported_loaders))
            break
    else:
        # pyre-fixme[6]: For 1st param expected `Tuple[Type[Loader], List[str]]` but
        #  got `Tuple[Loader, List[str]]`.
        sys.path_hooks.insert(0, FileFinder.path_hook(*supported_loaders))

    # We need to clear the path_importer_cache so that our new FileFinder will
    # start being used for existing directories we've loaded modules from.
    sys.path_importer_cache.clear()
Example #4
0
def _install_source_loader_helper(source_loader_type):
    extensions = ExtensionFileLoader, _imp.extension_suffixes()
    source = source_loader_type, SOURCE_SUFFIXES
    bytecode = SourcelessFileLoader, BYTECODE_SUFFIXES
    supported_loaders = [extensions, source, bytecode]
    sys.path_hooks[:] = [
        zipimport.zipimporter,
        FileFinder.path_hook(*supported_loaders),
    ]
    sys.path_importer_cache.clear()
Example #5
0
def lib2to3importer(fixers, prefix=None):
    """Returns finder closure to be prepended to sys.path_hooks.

    :param fixers: e.g. ["lib2to3.fixes.fix_exec", "lib2to3.fixes.fix_long"]
    :param prefix: Prefix of module or package name to apply fixes.
        Unless provided, fixes are applied to all of modules and packages.
    """
    extensions = ExtensionFileLoader, EXTENSION_SUFFIXES
    source = Lib2to3FileLoader.apply(fixers, prefix), SOURCE_SUFFIXES
    bytecode = SourcelessFileLoader, BYTECODE_SUFFIXES
    return FileFinder.path_hook(extensions, source, bytecode)
Example #6
0
def file_loader(loader: object) -> Generator[None, None, None]:
    orig_hooks = sys.path_hooks[:]
    sys.path_hooks[:] = [
        FileFinder.path_hook(
            loader)  # pyre-ignore[6]: Loader type isn't expose
    ]
    sys.path_importer_cache.clear()
    try:
        yield
    finally:
        sys.path_hooks[:] = orig_hooks
        sys.path_importer_cache.clear()
Example #7
0
def install_hooks():
    _ctx.update({"start_time": time.time(), "depth": 0})

    ExtensionFileLoader.exec_module = profile(ExtensionFileLoader.exec_module)
    SourceFileLoader.exec_module = profile(SourceFileLoader.exec_module)
    SourcelessFileLoader.exec_module = profile(
        SourcelessFileLoader.exec_module)
    sys.path_hooks.insert(
        0,
        FileFinder.path_hook(
            (ExtensionFileLoader, EXTENSION_SUFFIXES),
            (SourceFileLoader, SOURCE_SUFFIXES),
            (SourcelessFileLoader, BYTECODE_SUFFIXES),
        ),
    )
def install_loader():
    """
    Install the import hook

    Once this has been run, the enhancements will be available in any imported code

    This will run automatically when the package is imported; to disable::

        builtins.__dict__["__perl__disable_automatic_import"] = True
    """
    # Set up import hook
    loader_details = PerlLoader, [".py"]
    sys.path_hooks.insert(0, FileFinder.path_hook(loader_details))
    sys.path_importer_cache.clear()
    invalidate_caches()

    # Inject dependencies for rewritten code
    builtins.__dict__["re"] = re
    builtins.__dict__["__perl__re_match"] = re_match
    builtins.__dict__["__perl__reset_vars"] = reset_vars
Example #9
0
def hotfix(module_names):
    global OLD_MODULES
    global BUILTINS_OBJ

    OLD_MODULES.clear()
    for name in module_names:
        if name in sys.modules:
            OLD_MODULES[name] = sys.modules[name]

    gc.disable()
    OLD_MODULES = sys.modules.copy()
    BUILTINS_OBJ = object
    sys.modules["builtins"].object = HotfixerObject

    sys.path_hooks.insert(0, FileFinder.path_hook((MetaPathLoader, [".py"])))
    # clear any loaders that might already be in use by the FileFinder
    sys.path_importer_cache.clear()
    importlib.invalidate_caches()

    for name in module_names:
        try:
            old_module = sys.modules.pop(name, None)
            new_module = importlib.import_module(name)
            if old_module and not is_skip_hotfix(new_module):
                for attr_name, new_attr in inspect.getmembers(new_module):
                    setattr(old_module, attr_name, new_attr)
        except Exception as ex:
            traceback.print_exc()
        finally:
            if name in OLD_MODULES:
                sys.modules[name] = OLD_MODULES[name]

    sys.path_hooks.pop(0)
    sys.modules["builtins"].object = BUILTINS_OBJ
    BUILTINS_OBJ = None

    for obj in NEW_OBJECTS:
        hotfix_obj(obj)

    gc.enable()
    gc.collect()
Example #10
0
def update_hooks(*loaders):
    """Append custom loaders to the `sys.path_hooks`, they must 
    have a tuple attribute EXTENSION_SUFFIXES to discover the correct path.
    
    _NATIVE_HOOK resides in the global scope to reset the original sys.path_hooks 
    if necessary..
    """
    global _NATIVE_HOOK
    from importlib.machinery import FileFinder
    
    if loaders:
        for i, hook in enumerate(sys.path_hooks):
            __closure__ = getattr(hook, '__closure__', None)
            if __closure__ and issubclass(__closure__[0].cell_contents, FileFinder):
                _NATIVE_HOOK = globals().get('_NATIVE_HOOK', (i, hook))
                sys.path_hooks[i] = FileFinder.path_hook(
                    *_NATIVE_HOOK[1].__closure__[1].cell_contents,
                    *((loader, loader.EXTENSION_SUFFIXES) for loader in loaders
                ))
    else:
        sys.path_hooks[_NATIVE_HOOK[0]] = _NATIVE_HOOK[1]
            
    """https://docs.python.org/3/library/sys.html#sys.path_importer_cache"""
    sys.path_importer_cache.clear()
Example #11
0
def install(loader_details):
    # Insert the path hook ahead of other path hooks.
    sys.path_hooks.insert(0, FileFinder.path_hook(loader_details))
    # Clear any loaders that might already be in use by the FileFinder.
    sys.path_importer_cache.clear()
    invalidate_caches()
Example #12
0
def init_enforce() -> None:
    loader_details = (Loader, SOURCE_SUFFIXES)
    sys.path_hooks.insert(0,
                          FileFinder.path_hook(loader_details))  # type: ignore
Example #13
0
def installImportOverride():
    # insert the path hook ahead of other path hooks
    sys.path_hooks.insert(0, FileFinder.path_hook(det))
    # clear any loaders that might already be in use by the FileFinder
    sys.path_importer_cache.clear()
    invalidate_caches()