• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Python new.module函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Python中new.module函数的典型用法代码示例。如果您正苦于以下问题:Python module函数的具体用法?Python module怎么用?Python module使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了module函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。

示例1: remote_supply_code

	def remote_supply_code(self, name, module, sourceaddr):
		if Pyro.config.PYRO_MOBILE_CODE and self.codeValidator(name,module,sourceaddr):
			import imp,marshal,new
			Log.msg('ObjBase','loading supplied code: ',name,'from',str(sourceaddr))
			name=name.split('.')
			# make the module hierarchy and add all names to sys.modules
			path=''
			mod=new.module("pyro-agent-context")

			for m in name:
				path+='.'+m
				# use already loaded modules instead of overwriting them
				real_path = path[1:]
				if sys.modules.has_key(real_path):
					mod = sys.modules[real_path]
				else:
					setattr(mod,m,new.module(path[1:]))
					mod=getattr(mod,m)
					sys.modules[path[1:]]=mod
				
			if module[0:4]!=imp.get_magic():
				# compile source code
				code=compile(module,'<downloaded>','exec')
			else:
				# read bytecode from the client
				code=marshal.loads(module[8:])
			# finally, execute the module code in the right module.	A
			exec code in mod.__dict__
		else:
			Log.warn('ObjBase','attempt to supply code denied: ',name,'from',str(sourceaddr))
			raise PyroError('attempt to supply code denied')
开发者ID:LLNL,项目名称:WVL,代码行数:31,代码来源:core.py


示例2: loadscript

    def loadscript(self, scriptfile):
        """Execute scriptfile in this sandbox.  Before it is executed, change
        to script directory and shadow pdffit and pdffit2 modules

        scriptfile  --  path to the script to be loaded
        """
        # go to script directory
        orgdir = os.getcwd()
        scriptdir = os.path.dirname(scriptfile) or "."
        scriptbase = os.path.basename(scriptfile)
        os.chdir(scriptdir)
        # shadow pdffit and pdffit2 modules
        savemodules = { }
        for modname in [ 'pdffit', 'pdffit2' ]:
            if modname in sys.modules:
                savemodules[modname] = sys.modules[modname]
        import new
        sys.modules['pdffit'] = new.module('pdffit')
        sys.modules['pdffit2'] = pdffit2 = new.module('pdffit2')
        # shadow PdfFit class
        pdffit2.PdfFit = None
        # execute the file
        execfile(scriptbase, self.sandbox())
        # restore modules
        del sys.modules['pdffit']
        del sys.modules['pdffit2']
        for modname, mod in savemodules.iteritems():
            sys.modules[modname] = mod
        # get to the original directory
        os.chdir(orgdir)
        return
开发者ID:XiaohaoYang,项目名称:diffpy.pdfgui,代码行数:31,代码来源:pdffitsandbox.py


示例3: get_hacked_sre_compile

def get_hacked_sre_compile(my_compile):
    """Return a copy of the sre_compile module for which the _sre
    module is a custom module that has _sre.compile == my_compile
    and CODESIZE == rsre_char.CODESIZE.
    """
    import sre_compile, sre_constants, __builtin__, new

    sre_hacked = new.module("_sre_hacked")
    sre_hacked.compile = my_compile
    sre_hacked.MAGIC = sre_compile.MAGIC
    sre_hacked.CODESIZE = rsre_char.CODESIZE
    sre_hacked.MAXREPEAT = sre_constants.MAX_REPEAT
    sre_hacked.getlower = rsre_char.getlower

    def my_import(name, *args):
        if name == "_sre":
            return sre_hacked
        else:
            return default_import(name, *args)

    src = sre_compile.__file__
    if src.lower().endswith(".pyc") or src.lower().endswith(".pyo"):
        src = src[:-1]
    mod = new.module("sre_compile_hacked")
    default_import = __import__
    try:
        __builtin__.__import__ = my_import
        execfile(src, mod.__dict__)
    finally:
        __builtin__.__import__ = default_import
    return mod
开发者ID:sota,项目名称:pypy,代码行数:31,代码来源:rpy.py


示例4: testImportBuiltInProtorpcClasses

  def testImportBuiltInProtorpcClasses(self):
    """Test that built in Protorpc classes are skipped."""
    file_set = descriptor.FileSet()
    file_set.files = [self.MakeFileDescriptor(u'standalone'),
                      self.MakeFileDescriptor(u'root.nested'),
                      self.MakeFileDescriptor(u'root.nested.nested'),
                      descriptor.describe_file(descriptor),
    ]

    root = new.module('root')
    nested = new.module('root.nested')
    root.nested = nested
    modules = {
        'root': root,
        'root.nested': nested,
        'protorpc.descriptor': descriptor,
    }

    definition.import_file_set(file_set, modules=modules)

    self.assertEquals(root, modules['root'])
    self.assertEquals(nested, modules['root.nested'])
    self.assertEquals(nested.nested, modules['root.nested.nested'])
    self.assertEquals(descriptor, modules['protorpc.descriptor'])

    self.assertEquals(file_set,
                      descriptor.describe_file_set(
                          [modules['standalone'],
                           modules['root.nested'],
                           modules['root.nested.nested'],
                           modules['protorpc.descriptor'],
                          ]))
开发者ID:jeremydw,项目名称:protorpc-standalone,代码行数:32,代码来源:definition_test.py


示例5: testImportFileSet

  def testImportFileSet(self):
    """Test importing a whole file set."""
    file_set = descriptor.FileSet()
    file_set.files = [self.MakeFileDescriptor(u'standalone'),
                      self.MakeFileDescriptor(u'root.nested'),
                      self.MakeFileDescriptor(u'root.nested.nested'),
                     ]

    root = new.module('root')
    nested = new.module('root.nested')
    root.nested = nested
    modules = {
        'root': root,
        'root.nested': nested,
    }

    definition.import_file_set(file_set, modules=modules)

    self.assertEquals(root, modules['root'])
    self.assertEquals(nested, modules['root.nested'])
    self.assertEquals(nested.nested, modules['root.nested.nested'])

    self.assertEquals(file_set,
                      descriptor.describe_file_set(
                          [modules['standalone'],
                           modules['root.nested'],
                           modules['root.nested.nested'],
                          ]))
开发者ID:jeremydw,项目名称:protorpc-standalone,代码行数:28,代码来源:definition_test.py


示例6: fake_import

def fake_import(fake_module):
    module_name = 'martiantest.fake.' + fake_module.__name__
    module = new.module(module_name)
    module_name_parts = module_name.split('.')
    module.__file__ =  '/' + '/'.join(module_name_parts)
    
    glob = {}
    for name in dir(fake_module):
        if name.startswith('__') and '.' not in name:
            continue
        obj = getattr(fake_module, name)
        glob[name] = obj
        try:
            obj = obj.im_func
        except AttributeError:
            pass
        __module__ = None
        try:
            __module__ == obj.__dict__.get('__module__')
        except AttributeError:
            try:
                __module__ = obj.__module__
            except AttributeError:
                pass
        if __module__ is None or __module__ == '__builtin__':
            try:
                obj.__module__ = module.__name__
            except AttributeError:
                pass
        setattr(module, name, obj)

    # provide correct globals for functions
    for name in dir(module):
        if name.startswith('__'):
            continue
        obj = getattr(module, name)
        try:
            code = obj.func_code
            new_func = new.function(code, glob, name)
            new_func.__module__ = module.__name__
            setattr(module, name, new_func)
            glob[name] = new_func
        except AttributeError:
            pass

    if not 'martiantest' in sys.modules:
        sys.modules['martiantest'] = new.module('martiantest')
        sys.modules['martiantest.fake'] = new.module('martiantest.fake')
        sys.modules['martiantest'].fake = sys.modules['martiantest.fake']

    sys.modules[module_name] = module
    setattr(sys.modules['martiantest.fake'], module_name.split('.')[-1],
            module)
    
    return module
开发者ID:CGTIC,项目名称:Plone_SP,代码行数:55,代码来源:testing.py


示例7: test_import_settings

 def test_import_settings(self):
     # import_settings() copies settings from another module into the
     # caller's global scope.
     source = new.module(b"source")
     source.SETTING = factory.getRandomString()
     target = new.module(b"target")
     target._source = source
     target._import_settings = import_settings
     eval("_import_settings(_source)", vars(target))
     expected = {"SETTING": source.SETTING}
     observed = find_settings(target)
     self.assertEqual(expected, observed)
开发者ID:cloudbase,项目名称:maas,代码行数:12,代码来源:test_maas.py


示例8: _retrieveCode

	def _retrieveCode(self, mname, level):
		import imp, marshal, new

		# Call the special method on the server to retrieve the code.
		# No need for complex exception stuff like when the server needs
		# code from the client (see handleInvocation): because the server
		# is a Pyro object we can actually *call* it :-)
		module = self.remoteInvocation("remote_retrieve_code",0,mname)
		mname = mname.split('.')
		path = ''
		mod = new.module("pyro-server-context")
		for m in mname:
			path += '.' + m
			# use already loaded modules instead of overwriting them
			real_path = path[1:]
			if sys.modules.has_key(real_path):
				mod = sys.modules[real_path]
			else:
				setattr(mod, m, new.module(real_path))
				mod = getattr(mod, m)
				sys.modules[real_path] = mod
				
		if module[0:4] != imp.get_magic():
			code = compile(module, "<downloaded>", "exec")
		else:
			code = marshal.loads(module[8:])
 
		try:
			loaded = 0
			# XXX probably want maxtries here...
			while not loaded:
				import __builtin__
				importer = agent_import(__builtin__.__import__)
				__builtin__.__import__ = importer
 
				try:
					exec code in mod.__dict__
					loaded = 1
				except ImportError, x:
					mname = importer.name
 
					if importer is not None:
						__builtin__.__import__ = importer.orig_import
						importer = None
 
					# XXX probably want maxrecursion here...
					self._retrieveCode(mname, level+1)
 
		finally:
			if importer is not None:
				__builtin__.__import__ = importer.orig_import
开发者ID:LLNL,项目名称:WVL,代码行数:51,代码来源:protocol.py


示例9: test_pickle_submodule

    def test_pickle_submodule(self):
        import pickle
        import sys, new

        mod = new.module('pack.mod')
        sys.modules['pack.mod'] = mod
        pack = new.module('pack')
        pack.mod = mod
        sys.modules['pack'] = pack

        import pack.mod
        pckl   = pickle.dumps(pack.mod)
        result = pickle.loads(pckl)
        assert pack.mod is result
开发者ID:cha0s-repo,项目名称:pypy,代码行数:14,代码来源:test_zzpickle_and_slow.py


示例10: testWithModules

  def testWithModules(self):
    """Test what happens when no modules provided."""
    modules = [new.module('package1'), new.module('package1')]

    file1 = descriptor.FileDescriptor()
    file1.package = 'package1'
    file2 = descriptor.FileDescriptor()
    file2.package = 'package2'

    expected = descriptor.FileSet()
    expected.files = [file1, file1]

    described = descriptor.describe_file_set(modules)
    described.check_initialized()
    self.assertEquals(expected, described)
开发者ID:JoaoVasques,项目名称:MiniGoogle-PageRank,代码行数:15,代码来源:descriptor_test.py


示例11: unimport_module

def unimport_module(name):
    old = sys.modules[name]
    sys.modules[name] = new.module('jinja2')
    try:
        yield
    finally:
        sys.modules[name] = old
开发者ID:EnTeQuAk,项目名称:logbook,代码行数:7,代码来源:test_logbook.py


示例12: import_module

 def import_module(self, path):
     if os.path.isfile(path):
         sys.path.insert(0, os.path.dirname(path))
         name = os.path.split(path)[-1].split('.')[0]
         filename, pathname, description = imp.find_module(name, [os.path.dirname(path)])
         module = imp.load_module(name, filename, pathname, description)
         module.functest_module_path = path
         module.__file__ = os.path.abspath(path)
         sys.path.pop(0)
     elif os.path.isdir(path):
         if os.path.isfile(os.path.join(path, '__init__.py')):
             sys.path.insert(0, os.path.abspath(os.path.join(path, os.path.pardir)))
             name = os.path.split(path)[-1]
             filename, pathname, description = imp.find_module(
                 name, [os.path.abspath(os.path.join(path, os.path.pardir))])
             module = imp.load_module(name, filename, pathname, description)
             module.functest_module_path = path
             module.__file__ = os.path.abspath(os.path.join(path, '__init__.py'))
             sys.path.pop(0)
         else:
             module = new.module(os.path.split(path)[-1])
             module.functest_module_path = path
     else:
         raise ImportError('path is not file or directory')
     return module
开发者ID:AMITHSHAH,项目名称:Test,代码行数:25,代码来源:collector.py


示例13: test_pickle2

    def test_pickle2(self):
        # To test a bug where too much stuff gets pickled when
        # a tasklet halted on stackless.schedule() is pickled.
        import new, sys

        mod = new.module("mod")
        sys.modules["mod"] = mod
        try:
            exec """
import pickle, sys
import stackless
import socket

def task_should_be_picklable():
    stackless.schedule()

def task_socket():
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    stackless.schedule()

def task_pickle(ref_task):
    p = pickle.dumps(ref_task)
    
ref_task = stackless.tasklet(task_should_be_picklable)()
stackless.tasklet(task_socket)()
stackless.tasklet(task_pickle)(ref_task)
stackless.run()
""" in mod.__dict__
        finally:
            del sys.modules["mod"]
开发者ID:cimarieta,项目名称:usp,代码行数:30,代码来源:test_stackless_pickle.py


示例14: _create_module

def _create_module(code, name, filename, store=True, ns={}, exec_module=None):
    for recompiled in range(2):
        name = get_template_name(name, filename)
        mod = new.module(name)
        mod.__file__ = filename
        mod.__ctime__ = time.time()
        mod.__dict__.update(ns)
        try:
            if exec_module:
                exec_module(mod, code)
            else:
                exec code in mod.__dict__
        except Exception:
            if store:
                sys.modules[name] = mod
            raise_template_error(module=name, filename=filename)
        if getattr(mod, "kid_version", None) == __version__:
            break
        # the module has been compiled against an old Kid version,
        # recompile to ensure compatibility and best performance
        if recompiled:  # already tried recompiling, to no avail
            raise TemplateImportError(
                "Cannot recompile template file" " %r for Kid version %s" % (filename, __version__)
            )
        template = KidFile(filename)
        template.stale = True
        template._python = template._code = None
        code = template.compile(dump_source=environ.get("KID_OUTPUT_PY"))
    if store:
        sys.modules[name] = mod
    return mod
开发者ID:eginhard,项目名称:gsl-en,代码行数:31,代码来源:importer.py


示例15: load_sysconfigs

 def load_sysconfigs(self):
     configs = self.sysconfigs[:]
     configs.reverse()
     self.sysconfig_modules = []
     for index, (explicit, name) in enumerate(configs):
         # @@: At some point I'd like to give the specialized
         # modules some access to the values in earlier modules,
         # e.g., to specialize those values or functions.  That's
         # why these modules are loaded backwards.
         if name.endswith(".py"):
             if not os.path.exists(name):
                 if explicit:
                     raise BadCommand, ("sysconfig file %s does not exist" % name)
                 else:
                     continue
             globs = {}
             execfile(name, globs)
             mod = new.module("__sysconfig_%i__" % index)
             for name, value in globs.items():
                 setattr(mod, name, value)
             mod.__file__ = name
         else:
             try:
                 mod = import_string.simple_import(name)
             except ImportError, e:
                 if explicit:
                     raise
                 else:
                     continue
         mod.paste_command = self
         self.sysconfig_modules.insert(0, mod)
开发者ID:songmm19900210,项目名称:galaxy-tools-prok,代码行数:31,代码来源:appinstall.py


示例16: remote_retrieve_code

	def remote_retrieve_code(self, name):
		# XXX codeValidator: can we somehow get the client's address it is sent to?
		# XXX this code is ugly. And duplicated in protocol.py remoteInvocation.
		if Pyro.config.PYRO_MOBILE_CODE and self.codeValidator(name,None,None):
			Log.msg("ObjBase","supplying code: ",name)
			try:
				importmodule=new.module("pyro-server-import")
				try:
					exec "import " + name in importmodule.__dict__
				except ImportError:
					Log.error("ObjBase","Client wanted a non-existing module:", name)
					raise PyroError("Client wanted a non-existing module", name)
				m=eval("importmodule."+name)
				# try to load the module's compiled source, or the real .py source if that fails.
				# note that the source code (.py) is opened with universal newline mode
				(filebase,ext)=os.path.splitext(m.__file__)
				if ext.startswith(".PY"):
					exts = ( (".PYO","rb"), (".PYC","rb"), (".PY","rU") )	# uppercase
				else:
					exts = ( (".pyo","rb"), (".pyc","rb"), (".py","rU") )	# lowercase
				for ext,mode in exts:
					try:
						m=open(filebase+ext, mode).read()
						return m  # supply the module to the client!
					except:
						pass
				Log.error("ObjBase","cannot read module source code for module:", name)
				raise PyroError("cannot read module source code")
			finally:
				del importmodule
		else:
			Log.error("ObjBase","attempt to retrieve code denied:", name)
			raise PyroError("attempt to retrieve code denied")
开发者ID:avenet,项目名称:tagfs,代码行数:33,代码来源:core.py


示例17: importString

def importString(source, filename, globals_=None, locals_=None):
    """construct a module from source code.
    
    The module will I{not} appear in L{sys.modules}.
    
    @arg source: source code of the module
    @type source: string
    
    @arg filename: a filename to use for the module's __file__
    @type filename: string
    
    @arg globals_: globals to pass to L{eval}(). Defaults to globals()
    @type globals_: dict

    @arg locals_: locals to pass to L{eval}(). Defaults to empty dict
    @type locals_: dict    
    
    @rtype: a module
    """
    if globals_ is None: globals_ = globals()
    if locals_ is None: locals_ = {}
    locals_['__file__'] = filename
    co = compile(source, filename, 'exec')
    eval(co, globals_, locals_)
    mod = new.module(os.path.splitext(os.path.basename(filename))[0])
    mod.__dict__.update(locals_)
    return mod
开发者ID:tjlee,项目名称:factory,代码行数:27,代码来源:util.py


示例18: compile_source

def compile_source(source, name):
    user = users.get_current_user()
    m = new.module(str(name))
    try:
        exec source in m.__dict__
    except Exception:
        logging.warn("Compilation failed for [%s]" % name, exc_info=True)
        to = CodeTO()
        to.id = None
        to.timestamp = -1
        to.author = None
        to.name = None
        to.source = None
        to.functions = []
        to.version = -1
        to.compile_error = unicode(traceback.format_exc())
        return to

    functions = inspect.getmembers(m, lambda x: inspect.isfunction(x) and x.__module__ == m.__name__)
    code = Code().all().filter("name =", name).get()
    if not code:
        code = Code()
    code.author = user
    code.timestamp = now()
    code.name = name
    code.source = source
    code.functions = [unicode(f[0]) for f in functions]
    code.version = code.version + 1 if code.version else 1
    code.put()
    to = CodeTO.fromDBCode(code)
    to.compile_error = None
    return to
开发者ID:gitter-badger,项目名称:rogerthat-backend,代码行数:32,代码来源:explorer.py


示例19: _get_script_function_models

def _get_script_function_models(script_name, source, old_functions, ignore_errors=False):
    script_module = new.module(str(script_name))
    try:
        exec source in script_module.__dict__
    except Exception:
        logging.warn('Compilation failed for \'%s\'', script_name, exc_info=True)
        if ignore_errors:
            return []
        raise HttpBadRequestException('compilation_error', {'traceback': traceback.format_exc()})
    functions = inspect.getmembers(script_module,
                                   lambda x: inspect.isfunction(x) and x.__module__ == script_module.__name__)
    function_models = []
    old_funcs = {f.name: f for f in old_functions}

    lines = source.splitlines()
    for f in functions:
        f_name = unicode(f[0])
        line_number = 1
        for i, line in enumerate(lines):
            if 'def %s' % f_name in line:
                line_number = i + 1
                break
        if f_name in old_funcs:
            updated_function = old_funcs[f_name]
            updated_function.line_number = line_number
            function_models.append(updated_function)
        else:
            function_models.append(ScriptFunction(name=f_name, line_number=line_number))
    return function_models
开发者ID:our-city-app,项目名称:plugin-interactive-explorer,代码行数:29,代码来源:scripts.py


示例20: load_module

    def load_module(self, fullname, loadingShared = False):
        #print >>sys.stderr, "load_module(%s), dir_path = %s, filename = %s" % (fullname, self.dir_path, self.filename)
        if self.fileType == FTFrozenModule:
            return self._import_frozen_module(fullname)
        if self.fileType == FTExtensionModule:
            return self._import_extension_module(fullname)

        # Check if this is a child of a shared package.
        if not loadingShared and self.packagePath and '.' in fullname:
            parentname = fullname.rsplit('.', 1)[0]
            if parentname in sharedPackages:
                # It is.  That means it's a shared package too.
                parent = sys.modules[parentname]
                path = getattr(parent, '__path__', None)
                importer = VFSSharedImporter()
                sharedPackages[fullname] = True
                loader = importer.find_module(fullname, path = path)
                assert loader
                return loader.load_module(fullname)
        
        code = self._read_code()
        if not code:
            raise ImportError, 'No Python code in %s' % (fullname)
        
        mod = sys.modules.setdefault(fullname, new.module(fullname))
        mod.__file__ = self.filename.toOsSpecific()
        mod.__loader__ = self
        if self.packagePath:
            mod.__path__ = [self.packagePath.toOsSpecific()]
            #print >> sys.stderr, "loaded %s, path = %s" % (fullname, mod.__path__)

        exec code in mod.__dict__
        return mod
开发者ID:Ed-von-Schleck,项目名称:panda3d,代码行数:33,代码来源:VFSImporter.py



注:本文中的new.module函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python new.new_instancemethod函数代码示例发布时间:2022-05-27
下一篇:
Python new.instancemethod函数代码示例发布时间:2022-05-27
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap