Python sys 模块,module() 实例源码

我们从Python开源项目中,提取了以下25个代码示例,用于说明如何使用sys.module()

项目:zenchmarks    作者:squeaky-pl    | 项目源码 | 文件源码
def _setWarningRegistryToNone(modules):
    """
    Disable the per-module cache for every module found in C{modules}, typically
    C{sys.modules}.

    @param modules: Dictionary of modules, typically sys.module dict
    """
    for v in list(modules.values()):
        if v is not None:
            try:
                v.__warningregistry__ = None
            except:
                # Don't specify a particular exception type to handle in case
                # some wacky object raises some wacky exception in response to
                # the setattr attempt.
                pass
项目:trojan    作者:Hackerl    | 项目源码 | 文件源码
def get_trojan_config():
    global configured
    #??github????????json??
    config_json = get_file_contents(trojan_config)
    #base64?????json??
    config = json.loads(base64.b64decode(config_json))
    configured = True

    for task in config:
        #??sys.modules?????
        if task['module'] not in sys.modules:
            #??import??,??github????
            exec ("import %s" % task['module'])
    #??????
    return config

#?????github
项目:zippy    作者:securesystemslab    | 项目源码 | 文件源码
def setUpClass(cls_, module=module):
            cls_._save_sys_modules = sys.modules.copy()
            sys.modules[TESTS] = module
            sys.modules['datetime'] = module.datetime_module
            sys.modules['_strptime'] = module._strptime
项目:specto    作者:mrknow    | 项目源码 | 文件源码
def dict_contains(d, key):
                return d.has_key(key)


#----------------------------------------------------------------------------------------------------------------------- 
#now that we've finished the needed pydev sitecustomize, let's run the default one (if available)

#Ok, some weirdness going on in Python 3k: when removing this module from the sys.module to import the 'real'
#sitecustomize, all the variables in this scope become None (as if it was garbage-collected), so, the the reference
#below is now being kept to create a cyclic reference so that it neven dies)
项目:web_ctp    作者:molebot    | 项目源码 | 文件源码
def test_name_requires_rparition(self):
        # Raise TypeError if a non-string is passed in for the module name.
        with self.assertRaises(TypeError):
            util.import_(42)
项目:web_ctp    作者:molebot    | 项目源码 | 文件源码
def test_negative_level(self):
        # Raise ValueError when a negative level is specified.
        # PEP 328 did away with sys.module None entries and the ambiguity of
        # absolute/relative imports.
        with self.assertRaises(ValueError):
            util.import_('os', globals(), level=-1)
项目:web_ctp    作者:molebot    | 项目源码 | 文件源码
def setUpClass(cls_, module=module):
            cls_._save_sys_modules = sys.modules.copy()
            sys.modules[TESTS] = module
            sys.modules['datetime'] = module.datetime_module
            sys.modules['_strptime'] = module._strptime
项目:ouroboros    作者:pybee    | 项目源码 | 文件源码
def exec_module(module):
        if module.__name__ == SUBMOD_NAME:
            raise ImportError('I cannot be loaded!')
项目:ouroboros    作者:pybee    | 项目源码 | 文件源码
def test_name_requires_rparition(self):
        # Raise TypeError if a non-string is passed in for the module name.
        with self.assertRaises(TypeError):
            self.__import__(42)
项目:ouroboros    作者:pybee    | 项目源码 | 文件源码
def test_negative_level(self):
        # Raise ValueError when a negative level is specified.
        # PEP 328 did away with sys.module None entries and the ambiguity of
        # absolute/relative imports.
        with self.assertRaises(ValueError):
            self.__import__('os', globals(), level=-1)
项目:ouroboros    作者:pybee    | 项目源码 | 文件源码
def setUpClass(cls_, module=module):
            cls_._save_sys_modules = sys.modules.copy()
            sys.modules[TESTS] = module
            sys.modules['datetime'] = module.datetime_module
            sys.modules['_strptime'] = module._strptime
项目:zenchmarks    作者:squeaky-pl    | 项目源码 | 文件源码
def _collectWarnings(observeWarning, f, *args, **kwargs):
    """
    Call C{f} with C{args} positional arguments and C{kwargs} keyword arguments
    and collect all warnings which are emitted as a result in a list.

    @param observeWarning: A callable which will be invoked with a L{_Warning}
        instance each time a warning is emitted.

    @return: The return value of C{f(*args, **kwargs)}.
    """
    def showWarning(message, category, filename, lineno, file=None, line=None):
        assert isinstance(message, Warning)
        observeWarning(_Warning(
                str(message), category, filename, lineno))

    # Disable the per-module cache for every module otherwise if the warning
    # which the caller is expecting us to collect was already emitted it won't
    # be re-emitted by the call to f which happens below.
    _setWarningRegistryToNone(sys.modules)

    origFilters = warnings.filters[:]
    origShow = warnings.showwarning
    warnings.simplefilter('always')
    try:
        warnings.showwarning = showWarning
        result = f(*args, **kwargs)
    finally:
        warnings.filters[:] = origFilters
        warnings.showwarning = origShow
    return result
项目:zenchmarks    作者:squeaky-pl    | 项目源码 | 文件源码
def getSkip(self):
        """
        Return the skip reason set on this test, if any is set. Checks on the
        instance first, then the class, then the module, then packages. As
        soon as it finds something with a C{skip} attribute, returns that.
        Returns L{None} if it cannot find anything. See L{TestCase} docstring
        for more details.
        """
        return util.acquireAttribute(self._parents, 'skip', None)
项目:zenchmarks    作者:squeaky-pl    | 项目源码 | 文件源码
def getTodo(self):
        """
        Return a L{Todo} object if the test is marked todo. Checks on the
        instance first, then the class, then the module, then packages. As
        soon as it finds something with a C{todo} attribute, returns that.
        Returns L{None} if it cannot find anything. See L{TestCase} docstring
        for more details.
        """
        todo = util.acquireAttribute(self._parents, 'todo', None)
        if todo is None:
            return None
        return makeTodo(todo)
项目:zenchmarks    作者:squeaky-pl    | 项目源码 | 文件源码
def _getSuppress(self):
        """
        Returns any warning suppressions set for this test. Checks on the
        instance first, then the class, then the module, then packages. As
        soon as it finds something with a C{suppress} attribute, returns that.
        Returns any empty list (i.e. suppress no warnings) if it cannot find
        anything. See L{TestCase} docstring for more details.
        """
        return util.acquireAttribute(self._parents, 'suppress', [])
项目:kbe_server    作者:xiaohaoppy    | 项目源码 | 文件源码
def exec_module(module):
        if module.__name__ == SUBMOD_NAME:
            raise ImportError('I cannot be loaded!')
项目:kbe_server    作者:xiaohaoppy    | 项目源码 | 文件源码
def test_name_requires_rparition(self):
        # Raise TypeError if a non-string is passed in for the module name.
        with self.assertRaises(TypeError):
            self.__import__(42)
项目:kbe_server    作者:xiaohaoppy    | 项目源码 | 文件源码
def test_negative_level(self):
        # Raise ValueError when a negative level is specified.
        # PEP 328 did away with sys.module None entries and the ambiguity of
        # absolute/relative imports.
        with self.assertRaises(ValueError):
            self.__import__('os', globals(), level=-1)
项目:kbe_server    作者:xiaohaoppy    | 项目源码 | 文件源码
def setUpClass(cls_, module=module):
            cls_._save_sys_modules = sys.modules.copy()
            sys.modules[TESTS] = module
            sys.modules['datetime'] = module.datetime_module
            sys.modules['_strptime'] = module._strptime
项目:trojan    作者:Hackerl    | 项目源码 | 文件源码
def load_module(self, name):
        #?????
        module = imp.new_module(name)
        #???????????
        exec self.current_module_code in module.__dict__

        #????sys
        sys.modules[name] = module

        return module

#??github
项目:trojan    作者:Hackerl    | 项目源码 | 文件源码
def module_runner(module):
    task_queue.put(1)
    #??????run?????????
    result = sys.modules[module].run()
    task_queue.get()
    #????
    # store the result in our repo
    store_module_result(result)

    return


# main trojan loop
#??sys.meta_path?github????????????sys.path
项目:Playground3    作者:CrimsonVista    | 项目源码 | 文件源码
def reloadConnectors(self, force=False):
        if self._loaded and not force: return

        configPath = Configure.CurrentPath()
        connectorsLocation = os.path.join(configPath, "connectors")
        connectorsInitPath = os.path.join(connectorsLocation, "__init__.py")

        oldPath = sys.path
        if configPath not in sys.path:
            sys.path.insert(0, configPath)
        if not os.path.exists(connectorsInitPath):
            with open(connectorsInitPath, "w+") as f:
                f.write("#dummy init for connectors module")

        for pathName in os.listdir(connectorsLocation):
            pathName = os.path.join(connectorsLocation, pathName)
            moduleName = os.path.basename(pathName)
            if os.path.exists(os.path.join(pathName, "__init__.py")):
                dottedName = "connectors.{}".format(moduleName)
                with PacketDefinitionSilo():
                    if dottedName in sys.modules:
                        #TODO: Test if this even works.
                        importlib.reload(sys.module[dottedName])
                    else:
                        importlib.import_module(dottedName)
        sys.path = oldPath
        self._loaded = True
        #    print("Loading module {}".format(pathName))
        #    self._loadConnectorModule(os.path.join(connectorLocation, pathName))
项目:utilspie    作者:moin18    | 项目源码 | 文件源码
def delete_module(modname):
    """
    Delete module and sub-modules from `sys.module`
    """
    try:
        _ = sys.modules[modname]
    except KeyError:
        raise ValueError("Module not found in sys.modules: '{}'".format(modname))

    for module in list(sys.modules.keys()):
        if module and module.startswith(modname):
            del sys.modules[module]
项目:utilspie    作者:moin18    | 项目源码 | 文件源码
def reload_module(module):
    """
    Reload the Python module
    """
    try:
        # For Python 2.x
        reload(module)
    except (ImportError, NameError):
        # For <= Python3.3:
        import imp
        imp.reload(module)
    except (ImportError, NameError):
        # For >= Python3.4
        import importlib
        importlib.reload(module)
项目:utilspie    作者:moin18    | 项目源码 | 文件源码
def lazy_load_modules(*modules):
    """
    Decorator to load module to perform related operation for specific function
    and delete the  module from imports once the task is done. GC frees the memory
    related to module during clean-up.
    """
    def decorator(function):
        def wrapper(*args, **kwargs):

            module_dict = {}
            for module_string in modules:
                module = __import__(module_string)

                # Add `module` entry in `sys.modules`. After deleting the module
                # from `sys.modules` and re-importing the module don't update
                # the module entry in `sys.modules` dict
                sys.modules[module.__package__] = module
                reload_module(module)
                module_dict[module_string] = module

            func_response = function(*args, **kwargs)

            for module_string, module in module_dict.items():
                # delete idna module
                delete_module(module_string)
                del module  # delete reference to idna

            return func_response
        return wrapper
    return decorator