本文整理汇总了Python中new.module函数的典型用法代码示例。如果您正苦于以下问题:Python module函数的具体用法?Python module怎么用?Python module使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了module函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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')
示例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
示例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
示例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'],
]))
示例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'],
]))
示例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
示例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)
示例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
示例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
示例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)
示例11: unimport_module
def unimport_module(name):
old = sys.modules[name]
sys.modules[name] = new.module('jinja2')
try:
yield
finally:
sys.modules[name] = old
示例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
示例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"]
示例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
示例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)