sys.modules.copy

Here are the examples of the python api sys.modules.copy taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

118 Examples 7

Example 101

Project: ironpython3
Source File: AutoComplete.py
View license
    def get_entity(self, name):
        """Lookup name in a namespace spanning sys.modules and __main.dict__"""
        namespace = sys.modules.copy()
        namespace.update(__main__.__dict__)
        return eval(name, namespace)

Example 102

Project: kbengine
Source File: AutoComplete.py
View license
    def get_entity(self, name):
        """Lookup name in a namespace spanning sys.modules and __main.dict__"""
        namespace = sys.modules.copy()
        namespace.update(__main__.__dict__)
        return eval(name, namespace)

Example 103

Project: kbengine
Source File: AutoComplete.py
View license
    def get_entity(self, name):
        """Lookup name in a namespace spanning sys.modules and __main.dict__"""
        namespace = sys.modules.copy()
        namespace.update(__main__.__dict__)
        return eval(name, namespace)

Example 104

Project: xtraceback
Source File: support.py
View license
def modules_setup():
    return sys.modules.copy(),

Example 105

Project: xtraceback
Source File: support.py
View license
def modules_setup():
    return sys.modules.copy(),

Example 106

Project: backdoorme
Source File: splitbrain.py
View license
@contextmanager
def splitbrain(conn):
    """Enter a splitbrain context in which imports take place over the given RPyC connection (expected to 
    be a SlaveService). You can enter this context only after calling ``enable()``"""
    if not _enabled:
        enable_splitbrain()
        #raise ValueError("Splitbrain not enabled")
    prev_conn = getattr(router, "conn", None)
    prev_modules = sys.modules.copy()
    router.conn = conn
    prev_stdin = conn.modules.sys.stdin
    prev_stdout = conn.modules.sys.stdout
    prev_stderr = conn.modules.sys.stderr
    conn.modules["sys"].stdin = sys.stdin
    conn.modules["sys"].stdout = sys.stdout
    conn.modules["sys"].stderr = sys.stderr
    try:
        yield
    finally:
        conn.modules["sys"].stdin = prev_stdin
        conn.modules["sys"].stdout = prev_stdout
        conn.modules["sys"].stderr = prev_stderr
        sys.modules.clear()
        sys.modules.update(prev_modules)
        router.conn = prev_conn
        if not router.conn:
            del router.conn

Example 107

Project: backdoorme
Source File: splitbrain.py
View license
@contextmanager
def splitbrain(conn):
    """Enter a splitbrain context in which imports take place over the given RPyC connection (expected to 
    be a SlaveService). You can enter this context only after calling ``enable()``"""
    if not _enabled:
        enable_splitbrain()
        #raise ValueError("Splitbrain not enabled")
    prev_conn = getattr(router, "conn", None)
    prev_modules = sys.modules.copy()
    router.conn = conn
    prev_stdin = conn.modules.sys.stdin
    prev_stdout = conn.modules.sys.stdout
    prev_stderr = conn.modules.sys.stderr
    conn.modules["sys"].stdin = sys.stdin
    conn.modules["sys"].stdout = sys.stdout
    conn.modules["sys"].stderr = sys.stderr
    try:
        yield
    finally:
        conn.modules["sys"].stdin = prev_stdin
        conn.modules["sys"].stdout = prev_stdout
        conn.modules["sys"].stderr = prev_stderr
        sys.modules.clear()
        sys.modules.update(prev_modules)
        router.conn = prev_conn
        if not router.conn:
            del router.conn

Example 108

Project: supernova
Source File: test_imports.py
View license
        def test_import_gi(self):
            def import_mock(name, *args, **kwargs):
                if name == 'gi.require_version':
                    return MockGi
                return orig_import(name, *args, **kwargs)

            mods = sys.modules.copy()
            tmpmod = {}
            loading = []
            for mod in mods:
                if mod.startswith('supernova'):
                    if mod.startswith('supernova'):
                        loading.append(mod.split('.')[-1])
                    tmpmod[mod] = sys.modules[mod]
                    del sys.modules[mod]
            builtins.__import__ = import_mock
            __import__('supernova', fromlist=loading)
            sys.modules.update(tmpmod)
            assert MockGi.called == 1
            builtins.__import__ = orig_import

Example 109

Project: supernova
Source File: test_imports.py
View license
        def test_import_gi(self):
            def import_mock(name, *args, **kwargs):
                if name == 'gi.require_version':
                    return MockGi
                return orig_import(name, *args, **kwargs)

            mods = sys.modules.copy()
            tmpmod = {}
            loading = []
            for mod in mods:
                if mod.startswith('supernova'):
                    if mod.startswith('supernova'):
                        loading.append(mod.split('.')[-1])
                    tmpmod[mod] = sys.modules[mod]
                    del sys.modules[mod]
            builtins.__import__ = import_mock
            __import__('supernova', fromlist=loading)
            sys.modules.update(tmpmod)
            assert MockGi.called == 1
            builtins.__import__ = orig_import

Example 110

Project: popcorn_maker
Source File: isolate.py
View license
    def beforeContext(self):
        """Copy sys.modules onto my mod stack
        """
        mods = sys.modules.copy()
        self._mod_stack.append(mods)

Example 111

Project: popcorn_maker
Source File: isolate.py
View license
    def beforeContext(self):
        """Copy sys.modules onto my mod stack
        """
        mods = sys.modules.copy()
        self._mod_stack.append(mods)

Example 112

Project: iot-utilities
Source File: AutoComplete.py
View license
    def get_entity(self, name):
        """Lookup name in a namespace spanning sys.modules and __main.dict__"""
        namespace = sys.modules.copy()
        namespace.update(__main__.__dict__)
        return eval(name, namespace)

Example 113

Project: iot-utilities
Source File: AutoComplete.py
View license
    def get_entity(self, name):
        """Lookup name in a namespace spanning sys.modules and __main.dict__"""
        namespace = sys.modules.copy()
        namespace.update(__main__.__dict__)
        return eval(name, namespace)

Example 114

Project: iot-utilities
Source File: AutoComplete.py
View license
    def get_entity(self, name):
        """Lookup name in a namespace spanning sys.modules and __main.dict__"""
        namespace = sys.modules.copy()
        namespace.update(__main__.__dict__)
        return eval(name, namespace)

Example 115

Project: iot-utilities
Source File: AutoComplete.py
View license
    def get_entity(self, name):
        """Lookup name in a namespace spanning sys.modules and __main.dict__"""
        namespace = sys.modules.copy()
        namespace.update(__main__.__dict__)
        return eval(name, namespace)

Example 116

Project: pytest-nodev
Source File: plugin.py
View license
def make_candidate_index(config):
    if config.getoption('candidates_from_all') and os.environ.get('PYTEST_NODEV_MODE') != 'FEARLESS':
        raise ValueError("Use of --candidates-from-all may be very dangerous, see the docs.")

    if not hasattr(config, '_candidate_index'):
        # take over collect logging
        collect.logger.propagate = False
        collect.logger.setLevel(logging.DEBUG)  # FIXME: loglevel should be configurable
        collect.logger.addHandler(utils.EmitHandler(config._warn))

        # delegate interrupting hanging tests to pytest-timeout
        os.environ['PYTEST_TIMEOUT'] = os.environ.get('PYTEST_TIMEOUT', '1')

        # build the object index
        distributions = collections.OrderedDict()

        if config.getoption('candidates_from_stdlib') or config.getoption('candidates_from_all'):
            distributions.update(collect.collect_stdlib_distributions())

        if config.getoption('candidates_from_all'):
            distributions.update(collect.collect_installed_distributions())

        distributions.update(collect.collect_distributions(config.getoption('candidates_from_specs')))

        if config.getoption('candidates_from_modules'):
            distributions['unknown distribution'] = config.getoption('candidates_from_modules')

        top_level_modules = collect.import_distributions(distributions.items())

        includes = config.getoption('candidates_includes')
        if not includes:
            includes = ['.'] if config.getoption('candidates_from_all') else sorted(top_level_modules)
        excludes = config.getoption('candidates_excludes')
        predicate = config.getoption('candidates_predicate')

        # NOTE: 'copy' is needed here because indexing may unexpectedly trigger a module load
        modules = sys.modules.copy()
        object_index = dict(
            collect.generate_objects_from_modules(modules, includes, excludes, predicate)
        )

        # store index
        config._candidate_index = list(zip(*sorted(object_index.items()))) or [(), ()]

    return config._candidate_index

Example 117

Project: PyAutoC
Source File: AutoComplete.py
View license
    def get_entity(self, name):
        """Lookup name in a namespace spanning sys.modules and __main.dict__"""
        namespace = sys.modules.copy()
        namespace.update(__main__.__dict__)
        return eval(name, namespace)

Example 118

Project: yadapy
Source File: sandbox.py
View license
@contextlib.contextmanager
def save_modules():
    """
    Context in which imported modules are saved.

    Translates exceptions internal to the context into the equivalent exception
    outside the context.
    """
    saved = sys.modules.copy()
    with ExceptionSaver() as saved_exc:
        yield saved

    sys.modules.update(saved)
    # remove any modules imported since
    del_modules = (
        mod_name for mod_name in sys.modules
        if mod_name not in saved
        # exclude any encodings modules. See #285
        and not mod_name.startswith('encodings.')
    )
    _clear_modules(del_modules)

    saved_exc.resume()