Python imp.new_module() Examples

The following are 30 code examples of imp.new_module(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module imp , or try the search function .
Example #1
Source File: test_importlib.py    From BinderFilter with MIT License 6 votes vote down vote up
def __init__(self, *names):
        self.modules = {}
        for name in names:
            if not name.endswith('.__init__'):
                import_name = name
            else:
                import_name = name[:-len('.__init__')]
            if '.' not in name:
                package = None
            elif import_name == name:
                package = name.rsplit('.', 1)[0]
            else:
                package = import_name
            module = imp.new_module(import_name)
            module.__loader__ = self
            module.__file__ = '<mock __file__>'
            module.__package__ = package
            module.attr = name
            if import_name != name:
                module.__path__ = ['<mock __path__>']
            self.modules[import_name] = module 
Example #2
Source File: run_interactive.py    From ironpython2 with Apache License 2.0 6 votes vote down vote up
def __init__(self, fileName):
        scriptEnv = Python.CreateRuntime()
        self.fileName = fileName
        self.engine = scriptEnv.GetEngine("python")        
        self.context = HostingHelpers.GetLanguageContext(self.engine) 
        scriptEnv.LoadAssembly(Type.GetType("System.String").Assembly) #mscorlib.dll
        scriptEnv.LoadAssembly(UriBuilder().GetType().Assembly)  #System.dll
                
        self.InitializePath()
        
        executable = Assembly.GetEntryAssembly().Location
        prefix = Path.GetDirectoryName(executable)
        
        self.context.SystemState.executable = executable
        self.context.SystemState.exec_prefix = self.context.SystemState.prefix = prefix
        
        import imp
        mod = imp.new_module('__main__')
        mod.__file__ = fileName
        mod.__builtins__ = sys.modules['__builtin__']
        self.context.SystemState.modules['__main__'] = mod
        self.mainScope = scriptEnv.CreateScope(mod.__dict__) 
Example #3
Source File: test_imp.py    From ironpython2 with Apache License 2.0 6 votes vote down vote up
def test_module_name(self):
        import imp
        m = imp.new_module('foo')
        self.assertEqual(m.__str__(), "<module 'foo' (built-in)>")
        m.__name__ = 'bar'
        self.assertEqual(m.__str__(), "<module 'bar' (built-in)>")
        m.__name__ = None
        self.assertEqual(m.__str__(), "<module '?' (built-in)>")
        m.__name__ = []
        self.assertEqual(m.__str__(), "<module '?' (built-in)>")
        m.__file__ = None
        self.assertEqual(m.__str__(), "<module '?' (built-in)>")
        m.__file__ = []
        self.assertEqual(m.__str__(), "<module '?' (built-in)>")
        m.__file__ = 'foo.py'
        self.assertEqual(m.__str__(), "<module '?' from 'foo.py'>") 
Example #4
Source File: livetest.py    From sndlatr with Apache License 2.0 6 votes vote down vote up
def main():
    host_config = parse_argv()

    namespace = probe_host(host_config)
    host_config.namespace = namespace

    live_test_mod = imp.new_module('livetests')
    sys.modules['livetests'] = live_test_mod

    def add_test_class(klass, name=None):
        if name is None:
            name = klass.__name__
        else:
            if not PY3:
                name = name.encode('ascii')
            klass.__name__ = name
        setattr(live_test_mod, name, klass)

    TestGeneral.conf = host_config
    add_test_class(TestGeneral)
    add_test_class(createUidTestClass(host_config, use_uid=True), 'TestWithUIDs')
    add_test_class(createUidTestClass(host_config, use_uid=False), 'TestWithoutUIDs')

    unittest.main(module='livetests') 
Example #5
Source File: pydevd_utils.py    From PyDev.Debugger with Eclipse Public License 1.0 6 votes vote down vote up
def save_main_module(file, module_name):
    # patch provided by: Scott Schlesier - when script is run, it does not
    # use globals from pydevd:
    # This will prevent the pydevd script from contaminating the namespace for the script to be debugged
    # pretend pydevd is not the main module, and
    # convince the file to be debugged that it was loaded as main
    sys.modules[module_name] = sys.modules['__main__']
    sys.modules[module_name].__name__ = module_name

    with warnings.catch_warnings():
        warnings.simplefilter("ignore", category=DeprecationWarning)
        warnings.simplefilter("ignore", category=PendingDeprecationWarning)
        from imp import new_module

    m = new_module('__main__')
    sys.modules['__main__'] = m
    if hasattr(sys.modules[module_name], '__loader__'):
        m.__loader__ = getattr(sys.modules[module_name], '__loader__')
    m.__file__ = file

    return m 
Example #6
Source File: settings_wrapper.py    From scrapy-cluster with MIT License 6 votes vote down vote up
def load_from_string(self, settings_string='', module_name='customsettings'):
        '''
        Loads settings from a settings_string. Expects an escaped string like
        the following:
            "NAME=\'stuff\'\nTYPE=[\'item\']\n"

        @param settings_string: The string with your settings
        @return: A dict of loaded settings
        '''
        try:
            mod = imp.new_module(module_name)
            exec(settings_string, mod.__dict__)
        except TypeError:
            log.warning("Could not import settings")
        self.my_settings = {}
        try:
            self.my_settings = self._convert_to_dict(mod)
        except ImportError:
            log.warning("Settings unable to be loaded")

        return self.settings() 
Example #7
Source File: test_importlib.py    From ironpython2 with Apache License 2.0 6 votes vote down vote up
def __init__(self, *names):
        self.modules = {}
        for name in names:
            if not name.endswith('.__init__'):
                import_name = name
            else:
                import_name = name[:-len('.__init__')]
            if '.' not in name:
                package = None
            elif import_name == name:
                package = name.rsplit('.', 1)[0]
            else:
                package = import_name
            module = imp.new_module(import_name)
            module.__loader__ = self
            module.__file__ = '<mock __file__>'
            module.__package__ = package
            module.attr = name
            if import_name != name:
                module.__path__ = ['<mock __path__>']
            self.modules[import_name] = module 
Example #8
Source File: Context.py    From 802.11ah-ns3 with GNU General Public License v2.0 6 votes vote down vote up
def load_module(path,encoding=None):
	try:
		return cache_modules[path]
	except KeyError:
		pass
	module=imp.new_module(WSCRIPT_FILE)
	try:
		code=Utils.readf(path,m='rU',encoding=encoding)
	except EnvironmentError:
		raise Errors.WafError('Could not read the file %r'%path)
	module_dir=os.path.dirname(path)
	sys.path.insert(0,module_dir)
	exec(compile(code,path,'exec'),module.__dict__)
	sys.path.remove(module_dir)
	cache_modules[path]=module
	return module 
Example #9
Source File: script.py    From RAFCON with Eclipse Public License 1.0 6 votes vote down vote up
def compile_module(self):
        """Builds a temporary module from the script file

        :raises exceptions.IOError: if the compilation of the script module failed
        """
        try:
            imp.acquire_lock()

            code = compile(self.script, '%s (%s)' % (self.filename, self._script_id), 'exec')
            # load module
            module_name = os.path.splitext(self.filename)[0] + str(self._script_id)
            tmp_module = imp.new_module(module_name)
            exec(code, tmp_module.__dict__)
            # return the module
            self.compiled_module = tmp_module
        except Exception as e:
            self.compiled_module = None
            raise
        finally:
            imp.release_lock() 
Example #10
Source File: Context.py    From royal-chaos with MIT License 6 votes vote down vote up
def load_module(path,encoding=None):
	try:
		return cache_modules[path]
	except KeyError:
		pass
	module=imp.new_module(WSCRIPT_FILE)
	try:
		code=Utils.readf(path,m='rU',encoding=encoding)
	except EnvironmentError:
		raise Errors.WafError('Could not read the file %r'%path)
	module_dir=os.path.dirname(path)
	sys.path.insert(0,module_dir)
	try:exec(compile(code,path,'exec'),module.__dict__)
	finally:sys.path.remove(module_dir)
	cache_modules[path]=module
	return module 
Example #11
Source File: sandbox_test.py    From browserscope with Apache License 2.0 6 votes vote down vote up
def test_load_with_path_hook(self):

    class DummyPathHook(object):

      def __init__(self, path):
        if path != 'dummy/path':
          raise ImportError

      def find_module(self, unused_fullname):
        return self

      def load_module(self, fullname):
        return imp.new_module('fake name: %s' % fullname)

    self.test_policies['distutils.util'] = sandbox.ModuleOverridePolicy(
        None, [], {}, default_pass_through=True)
    sys.path_hooks = [DummyPathHook]
    util = self.hook.load_module('distutils.util')
    self.assertEqual('fake name: distutils.util', util.__name__) 
Example #12
Source File: sandbox.py    From browserscope with Apache License 2.0 6 votes vote down vote up
def import_stub_module(self, name):
    """Import the stub module replacement for the specified module."""
    # Do the equivalent of
    # ``from google.appengine.dist import <name>``.
    providing_dist = dist
    # When using the Py27 runtime, modules in dist27 have priority.
    # (They have already been vetted.)
    if name in dist27.__all__:
      providing_dist = dist27
    fullname = '%s.%s' % (providing_dist.__name__, name)
    __import__(fullname, {}, {})
    module = imp.new_module(fullname)
    module.__dict__.update(sys.modules[fullname].__dict__)
    module.__loader__ = self
    module.__name__ = name
    module.__package__ = None
    module.__name__ = name
    sys.modules[name] = module
    return module 
Example #13
Source File: common_client.py    From script-languages with MIT License 6 votes vote down vote up
def import_script(self, script):
        """ Import script from the EXASolution database """
        z = self._z
        getattr(self._comm.req, 'import').script_name = script
        try:
            self._comm(z.MT_IMPORT, z.MT_IMPORT)
            if getattr(self._comm.rep, 'import').exception_message != '':
                message = getattr(self._comm.rep, 'import').exception_message
                raise RuntimeError(message)
            code = getattr(self._comm.rep, 'import').source_code
        except Exception as err:
            raise ImportError(u'importing script %s failed: %s' % (script, str(err)))
        print("IMPORT", self._comm.connection_id, repr(code), "cache", repr(self), repr(self.__modules))
        if self.__modules.has_key(code):            
            return self.__modules[code]
        obj = imp.new_module(script)
        obj.__file__ = '<%s>' % script
        obj.__dict__['exa'] = self
        self.__modules[code] = obj
        try: exec(compile(code, script, 'exec'),obj.__dict__)
        except Exception as err:
            raise ImportError(u'importing module %s failed: %s' % (script, str(err)))
        return obj 
Example #14
Source File: __init__.py    From ibex with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def load_module(self, full_name):
        orig = full_name.split('.')[-1]

        if orig not in sklearn.__all__:
            raise ImportError('%s not in sklearn' % orig)

        mod = sys.modules.setdefault(full_name, imp.new_module(full_name))
        mod.__file__ = ''
        mod.__name__ = full_name
        mod.__path__ = ''
        mod.__loader__ = self
        mod.__package__ = '.'.join(full_name.split('.')[:-1])

        code = _code.substitute({'mod_name': orig})

        six.exec_(code, mod.__dict__)

        return mod 
Example #15
Source File: functions.py    From xcessiv with Apache License 2.0 6 votes vote down vote up
def import_string_code_as_module(code):
    """Used to run arbitrary passed code as a module

    Args:
        code (string): Python code to import as module

    Returns:
        module: Python module
    """
    sha256 = hashlib.sha256(code.encode('UTF-8')).hexdigest()
    module = imp.new_module(sha256)
    try:
        exec_(code, module.__dict__)
    except Exception as e:
        raise exceptions.UserError('User code exception', exception_message=str(e))
    sys.modules[sha256] = module
    return module 
Example #16
Source File: functions.py    From xcessiv with Apache License 2.0 6 votes vote down vote up
def import_object_from_string_code(code, object):
    """Used to import an object from arbitrary passed code.

    Passed in code is treated as a module and is imported and added
    to `sys.modules` with its SHA256 hash as key.

    Args:
        code (string): Python code to import as module

        object (string): Name of object to extract from imported module
    """
    sha256 = hashlib.sha256(code.encode('UTF-8')).hexdigest()
    module = imp.new_module(sha256)
    try:
        exec_(code, module.__dict__)
    except Exception as e:
        raise exceptions.UserError('User code exception', exception_message=str(e))
    sys.modules[sha256] = module
    try:
        return getattr(module, object)
    except AttributeError:
        raise exceptions.UserError("{} not found in code".format(object)) 
Example #17
Source File: test_importlib.py    From oss-ftp with MIT License 6 votes vote down vote up
def __init__(self, *names):
        self.modules = {}
        for name in names:
            if not name.endswith('.__init__'):
                import_name = name
            else:
                import_name = name[:-len('.__init__')]
            if '.' not in name:
                package = None
            elif import_name == name:
                package = name.rsplit('.', 1)[0]
            else:
                package = import_name
            module = imp.new_module(import_name)
            module.__loader__ = self
            module.__file__ = '<mock __file__>'
            module.__package__ = package
            module.attr = name
            if import_name != name:
                module.__path__ = ['<mock __path__>']
            self.modules[import_name] = module 
Example #18
Source File: factory_module.py    From marsnake with GNU General Public License v3.0 6 votes vote down vote up
def load_compiled(self, name, filename, code, ispackage = False):
		#if data[:4] != imp.get_magic():
		#	raise ImportError('Bad magic number in %s' % filename)
		# Ignore timestamp in data[4:8]
		#code = marshal.loads(data[8:])
		imp.acquire_lock() # Required in threaded applications

		try:
			mod = imp.new_module(name)
			sys.modules[name] = mod 	# To handle circular and submodule imports
										# it should come before exec.
			try:
				mod.__file__ = filename # Is not so important.
				# For package you have to set mod.__path__ here.
				# Here I handle simple cases only.
				if ispackage:
					mod.__path__ = [name.replace('.', '/')]
				exec(code in mod.__dict__)
			except:
				del sys.modules[name]
				raise
		finally:
			imp.release_lock()

		return mod 
Example #19
Source File: PupyClient.py    From NoobSec-Toolkit with GNU General Public License v2.0 6 votes vote down vote up
def load_pupyimporter(self):
		""" load pupyimporter in case it is not """
		if "pupyimporter" not in self.conn.modules.sys.modules:
			pupyimporter_code=""
			with open(os.path.join("packages","all","pupyimporter.py"),'rb') as f:
				pupyimporter_code=f.read()
			self.conn.execute(textwrap.dedent(
			"""
			import imp
			import sys
			def pupyimporter_preimporter(code):
				mod = imp.new_module("pupyimporter")
				mod.__name__="pupyimporter"
				mod.__file__="<memimport>\\\\pupyimporter"
				mod.__package__="pupyimporter"
				sys.modules["pupyimporter"]=mod
				exec code+"\\n" in mod.__dict__
				mod.install()
				"""))
			self.conn.namespace["pupyimporter_preimporter"](pupyimporter_code) 
Example #20
Source File: config_file.py    From GaragePi with MIT License 5 votes vote down vote up
def __read_file(self, file_name):
        d = imp.new_module('config')
        d.__file__ = file_name
        with open(file_name) as config_file:
            exec(compile(config_file.read(), file_name, 'exec'), d.__dict__)

        for key in dir(d):
            if key.isupper():
                self[key] = getattr(d, key) 
Example #21
Source File: cpp.py    From training_results_v0.6 with Apache License 2.0 5 votes vote down vote up
def _create_module(name):
    fullname = __name__ + "." + name
    mod = _new_module(fullname)
    sys.modules[fullname] = mod
    return mod

# pylint: disable-msg=C0103 
Example #22
Source File: cogapp.py    From cog with MIT License 5 votes vote down vote up
def installCogModule(self):
        """ Magic mumbo-jumbo so that imported Python modules
            can say "import cog" and get our state.
        """
        self.cogmodule = imp.new_module('cog')
        self.cogmodule.path = [] 
Example #23
Source File: runpy.py    From oss-ftp with MIT License 5 votes vote down vote up
def __init__(self, mod_name):
        self.mod_name = mod_name
        self.module = imp.new_module(mod_name)
        self._saved_module = [] 
Example #24
Source File: module.py    From judge-server with GNU Affero General Public License v3.0 5 votes vote down vote up
def load_module(name, code, filename=None):
    mod = imp.new_module(name)
    if filename is not None:
        mod.__file__ = filename
    exec(compile(code, filename or '<string>', 'exec'), mod.__dict__)
    return mod 
Example #25
Source File: backdoros.py    From backdoros with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def __import__(*args, **kwargs):
    global _is_debug

    if _is_debug:
        print "*** HOOKED *** IMPORT: args = %s, kwargs = %s" % (args, kwargs)

    name = args[0]

    try:
        return _real_import(*args, **kwargs)
    except ImportError:
        # TODO: Add support for more extensions? (e.g. *.pyc)
        if _mem_storage.has_key(args[0] + '.py'):
            name = args[0] + '.py'
        if _mem_storage.has_key(name):
            # It's a Virtual File!
            new_mod = imp.new_module(name)
            exec _mem_storage[name].read() in new_mod.__dict__
            sys.modules[args[0]] = new_mod
            return new_mod
        else:
            # It's a bust!
            raise ImportError('ImportError: No module named %s' % name)


# TODO: Monkey patch https://docs.python.org/2/library/os.html#file-descriptor-operations 
Example #26
Source File: pack.py    From mimipy with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def get_load_module_code(code, modulename):
    loader="""
import imp, sys
fullname={}
mod = imp.new_module(fullname)
mod.__file__ = "<bootloader>\\%s" % fullname
exec {} in mod.__dict__
sys.modules[fullname]=mod
    """.format(repr(modulename),repr(code))
    return loader 
Example #27
Source File: ihooks.py    From oss-ftp with MIT License 5 votes vote down vote up
def add_module(self, name):
        d = self.modules_dict()
        if name in d: return d[name]
        d[name] = m = self.new_module(name)
        return m

    # sys interface 
Example #28
Source File: ihooks.py    From oss-ftp with MIT License 5 votes vote down vote up
def new_module(self, name): return imp.new_module(name) 
Example #29
Source File: test_importhooks.py    From oss-ftp with MIT License 5 votes vote down vote up
def load_module(self, fullname):
        ispkg, code = self.modules[fullname]
        mod = sys.modules.setdefault(fullname,imp.new_module(fullname))
        mod.__file__ = "<%s>" % self.__class__.__name__
        mod.__loader__ = self
        if ispkg:
            mod.__path__ = self._get__path__()
        exec code in mod.__dict__
        return mod 
Example #30
Source File: test_pkgutil.py    From BinderFilter with MIT License 5 votes vote down vote up
def load_module(self, fullname):
            # Create an empty module
            mod = sys.modules.setdefault(fullname, imp.new_module(fullname))
            mod.__file__ = "<%s>" % self.__class__.__name__
            mod.__loader__ = self
            # Make it a package
            mod.__path__ = []
            # Count how many times the module is reloaded
            mod.__dict__['loads'] = mod.__dict__.get('loads',0) + 1
            return mod