Python sys.module() Examples

The following are code examples for showing how to use sys.module(). They are extracted from open source Python projects. You can vote up the examples you like or vote down the ones you don't like. You can also save this page to your account.

Example 1
Project: zenchmarks   Author: squeaky-pl   File: _synctest.py    (license) View Source Project 6 votes vote down vote up
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 
Example 2
Project: trojan   Author: Hackerl   File: trojan.py    (license) View Source Project 6 votes vote down vote up
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 
Example 3
Project: zippy   Author: securesystemslab   File: test_datetime.py    (license) View Source Project 5 votes vote down vote up
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 
Example 4
Project: specto   Author: mrknow   File: sitecustomize.py    (license) View Source Project 5 votes vote down vote up
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) 
Example 5
Project: web_ctp   Author: molebot   File: test_api.py    (license) View Source Project 5 votes vote down vote up
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) 
Example 6
Project: web_ctp   Author: molebot   File: test_api.py    (license) View Source Project 5 votes vote down vote up
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) 
Example 7
Project: web_ctp   Author: molebot   File: test_datetime.py    (license) View Source Project 5 votes vote down vote up
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 
Example 8
Project: ouroboros   Author: pybee   File: test_api.py    (license) View Source Project 5 votes vote down vote up
def exec_module(module):
        if module.__name__ == SUBMOD_NAME:
            raise ImportError('I cannot be loaded!') 
Example 9
Project: ouroboros   Author: pybee   File: test_api.py    (license) View Source Project 5 votes vote down vote up
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) 
Example 10
Project: ouroboros   Author: pybee   File: test_api.py    (license) View Source Project 5 votes vote down vote up
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) 
Example 11
Project: ouroboros   Author: pybee   File: test_datetime.py    (license) View Source Project 5 votes vote down vote up
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 
Example 12
Project: zenchmarks   Author: squeaky-pl   File: _synctest.py    (license) View Source Project 5 votes vote down vote up
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 
Example 13
Project: zenchmarks   Author: squeaky-pl   File: _synctest.py    (license) View Source Project 5 votes vote down vote up
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) 
Example 14
Project: zenchmarks   Author: squeaky-pl   File: _synctest.py    (license) View Source Project 5 votes vote down vote up
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) 
Example 15
Project: zenchmarks   Author: squeaky-pl   File: _synctest.py    (license) View Source Project 5 votes vote down vote up
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', []) 
Example 16
Project: kbe_server   Author: xiaohaoppy   File: test_api.py    (license) View Source Project 5 votes vote down vote up
def exec_module(module):
        if module.__name__ == SUBMOD_NAME:
            raise ImportError('I cannot be loaded!') 
Example 17
Project: kbe_server   Author: xiaohaoppy   File: test_api.py    (license) View Source Project 5 votes vote down vote up
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) 
Example 18
Project: kbe_server   Author: xiaohaoppy   File: test_api.py    (license) View Source Project 5 votes vote down vote up
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) 
Example 19
Project: kbe_server   Author: xiaohaoppy   File: test_datetime.py    (license) View Source Project 5 votes vote down vote up
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 
Example 20
Project: trojan   Author: Hackerl   File: trojan.py    (license) View Source Project 5 votes vote down vote up
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 
Example 21
Project: trojan   Author: Hackerl   File: trojan.py    (license) View Source Project 5 votes vote down vote up
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 
Example 22
Project: Playground3   Author: CrimsonVista   File: connect.py    (license) View Source Project 5 votes vote down vote up
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)) 
Example 23
Project: utilspie   Author: moin18   File: import_utils.py    (license) View Source Project 5 votes vote down vote up
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] 
Example 24
Project: utilspie   Author: moin18   File: import_utils.py    (license) View Source Project 5 votes vote down vote up
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) 
Example 25
Project: utilspie   Author: moin18   File: import_utils.py    (license) View Source Project 5 votes vote down vote up
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