def inject(module_name, new_globals, *additional_modules):
    """Inject greenified modules into an imported module

    This method imports the module specified in `module_name`, arranging things so that the
    already-imported modules in `additional_modules` are used when `module_name` makes its imports.

    `new_globals` is either None or a globals dictionary that gets populated with the contents of
    the `module_name` module. This is useful when creating a "green" version of some other module.

    `additional_modules` should be a collection of two-element tuples, of the form
    ``(name: str,  module: str)``. If it's not specified, a default selection of name/module pairs
    is used, which should cover all use cases but may be slower because there are inevitably
    redundant or unnecessary imports.
    """
    patched_name = '__patched_module_' + module_name
    if patched_name in sys.modules:
        # returning already-patched module so as not to destroy existing
        # references to patched modules
        return sys.modules[patched_name]

    if not additional_modules:
        # supply some defaults
        additional_modules = (
            _green_os_modules() +
            _green_select_modules() +
            _green_socket_modules() +
            _green_thread_modules() +
            _green_time_modules()
        )

    # after this, we will be modifying sys.modules, so save the state
    # of all modules that will be modified, and lock
    saver = SysModulesSaver([name for name, m in additional_modules])
    saver.save(module_name)

    # cover the target modules, so that when you import the module, it will import
    # the patched version
    for name, mod in additional_modules:
        sys.modules[name] = mod

    # remove the old module from sys.modules and reimport it while
    # the specified modules are in place
    sys.modules.pop(module_name, None)
    try:
        module = __import__(module_name, {}, {}, module_name.split('.')[:-1])

        if new_globals is not None:
            # update the given globals dictionary with everything from this new module
            for name in dir(module):
                if name not in __exclude:
                    new_globals[name] = getattr(module, name)

        # keep a reference to the new module to prevent it from dying
        sys.modules[patched_name] = module
    finally:
        saver.restore()  # Put the original modules back

    return module
dataset/ETHPy150Open veegee/guv/guv/patcher.py/inject/original