如何实现可以使用importlib动态修改源代码的导入钩子?

时间:2017-04-23 13:23:05

标签: python python-3.x python-importlib import-hooks

使用不推荐使用的模块imp,我可以编写一个自定义导入钩子,在Python导入/执行之前动态修改模块的源代码。鉴于源代码为下面名为source的字符串,创建模块所需的基本代码如下:

module = imp.new_module(name)
sys.modules[name] = module
exec(source, module.__dict__)

由于imp已被弃用,我想与importlib做类似的事情。 [编辑:还有其他imp方法需要替换以构建自定义导入钩子 - 所以我要找的答案不仅仅是替换上面的代码。]

但是,我还没弄清楚如何做到这一点。 importlib documentation有一个function to create modules from "specs",根据我的意思,它是包含自己的加载器的对象,没有明显的方法来重新定义它们,以便能够从字符串创建模块。

我创建了一个minimal example来证明这一点;有关详细信息,请参阅自述文件。

2 个答案:

答案 0 :(得分:12)

find_moduleload_module均已弃用。您需要分别切换到find_spec和(create_moduleexec_module)模块。有关详细信息,请参阅importlib documentation

您还需要检查是否要使用MetaPathFinderPathEntryFinder,因为系统调用它们是不同的。也就是说,元路径查找器首先可以覆盖内置模块,而路径条目查找器专门用于sys.path上的模块。

以下是一个非常基本的导入器,它试图替换整个导入机器。它显示了如何使用这些函数(find_speccreate_moduleexec_module)。

import sys
import os.path

from importlib.abc import Loader, MetaPathFinder
from importlib.util import spec_from_file_location

class MyMetaFinder(MetaPathFinder):
    def find_spec(self, fullname, path, target=None):
        if path is None or path == "":
            path = [os.getcwd()] # top level import -- 
        if "." in fullname:
            *parents, name = fullname.split(".")
        else:
            name = fullname
        for entry in path:
            if os.path.isdir(os.path.join(entry, name)):
                # this module has child modules
                filename = os.path.join(entry, name, "__init__.py")
                submodule_locations = [os.path.join(entry, name)]
            else:
                filename = os.path.join(entry, name + ".py")
                submodule_locations = None
            if not os.path.exists(filename):
                continue

            return spec_from_file_location(fullname, filename, loader=MyLoader(filename),
                submodule_search_locations=submodule_locations)

        return None # we don't know how to import this

class MyLoader(Loader):
    def __init__(self, filename):
        self.filename = filename

    def create_module(self, spec):
        return None # use default module creation semantics

    def exec_module(self, module):
        with open(self.filename) as f:
            data = f.read()

        # manipulate data some way...

        exec(data, vars(module))

def install():
    """Inserts the finder into the import machinery"""
    sys.meta_path.insert(0, MyMetaFinder())

接下来是一个稍微更精致的版本,试图重用更多的导入机制。因此,您只需要定义如何获取模块的源。

import sys
from os.path import isdir
from importlib import invalidate_caches
from importlib.abc import SourceLoader
from importlib.machinery import FileFinder


class MyLoader(SourceLoader):
    def __init__(self, fullname, path):
        self.fullname = fullname
        self.path = path

    def get_filename(self, fullname):
        return self.path

    def get_data(self, filename):
        """exec_module is already defined for us, we just have to provide a way
        of getting the source code of the module"""
        with open(filename) as f:
            data = f.read()
        # do something with data ...
        # eg. ignore it... return "print('hello world')"
        return data


loader_details = MyLoader, [".py"]

def install():
    # 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()

答案 1 :(得分:0)

另见这个不错的项目https://pypi.org/project/importhook/

pip install importhook
import importhook

# Setup hook to be called any time the `socket` module is imported and loaded into module cache
@importhook.on_import('socket')
def on_socket_import(socket):
    new_socket = importhook.copy_module(socket)
    setattr(new_socket, 'gethostname', lambda: 'patched-hostname')
    return new_socket

# Import module
import socket

# Prints: 'patched-hostname'
print(socket.gethostname())