本文整理汇总了Python中types.ModuleType.__loader__方法的典型用法代码示例。如果您正苦于以下问题:Python ModuleType.__loader__方法的具体用法?Python ModuleType.__loader__怎么用?Python ModuleType.__loader__使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类types.ModuleType
的用法示例。
在下文中一共展示了ModuleType.__loader__方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: new_module
# 需要导入模块: from types import ModuleType [as 别名]
# 或者: from types.ModuleType import __loader__ [as 别名]
def new_module(self, module_name, file_path):
self.module_name = module_name
module = ModuleType(module_name)
module.__package__ = module_name.rpartition('.')[0]
module.__file__ = file_path
module.__loader__ = FileWithMacrosLoader(module_name, module)
return module
示例2: compile
# 需要导入模块: from types import ModuleType [as 别名]
# 或者: from types.ModuleType import __loader__ [as 别名]
def compile(self, filename=None, mode='exec',
flag=generators.compiler_flag,
dont_inherit=0, _genframe=None):
""" return compiled code object. if filename is None
invent an artificial filename which displays
the source/line position of the caller frame.
"""
if not filename or py.path.local(filename).check(file=0):
if _genframe is None:
_genframe = sys._getframe(1) # the caller
fn,lineno = _genframe.f_code.co_filename, _genframe.f_lineno
base = "<%d-codegen " % self._compilecounter
self.__class__._compilecounter += 1
if not filename:
filename = base + '%s:%d>' % (fn, lineno)
else:
filename = base + '%r %s:%d>' % (filename, fn, lineno)
source = "\n".join(self.lines) + '\n'
try:
co = cpy_compile(source, filename, mode, flag)
except SyntaxError:
ex = sys.exc_info()[1]
# re-represent syntax errors from parsing python strings
msglines = self.lines[:ex.lineno]
if ex.offset:
msglines.append(" "*ex.offset + '^')
msglines.append("(code was compiled probably from here: %s)" % filename)
newex = SyntaxError('\n'.join(msglines))
newex.offset = ex.offset
newex.lineno = ex.lineno
newex.text = ex.text
raise newex
else:
if flag & _AST_FLAG:
return co
lines = [(x + "\n") for x in self.lines]
if sys.version_info[0] >= 3:
# XXX py3's inspect.getsourcefile() checks for a module
# and a pep302 __loader__ ... we don't have a module
# at code compile-time so we need to fake it here
m = ModuleType("_pycodecompile_pseudo_module")
py.std.inspect.modulesbyfile[filename] = None
py.std.sys.modules[None] = m
m.__loader__ = 1
py.std.linecache.cache[filename] = (1, None, lines, filename)
return co
示例3: makeMastBuilder
# 需要导入模块: from types import ModuleType [as 别名]
# 或者: from types.ModuleType import __loader__ [as 别名]
def makeMastBuilder():
"NOT_RPYTHON"
import itertools
methods = []
wrapperClasses = []
for groupName, group in MastIR.nonterms.items():
wrapperClasses.append(wrapperCls(groupName, "Object", None))
for name, paramInfo in group.items():
wrapperClasses.append(wrapperCls(name, groupName, paramInfo))
params = [n[0] for n in paramInfo] + ['span']
checks = [paramCheck(pname, typ, name)
for pname, typ in paramInfo]
checks.append(checkSpan())
ps = [p + "_0" for p in params]
wrapper = "ASTWrapper.%s(MastIR.%s(%s))" % (name, name, ', '.join(ps))
methods.append("\n @method(%s)\n def %s(%s):%s\n return %s" % (
', '.join(['"%s"' % g for g in guardNames(name, paramInfo)]),
name, ', '.join(['self'] + params), ''.join(checks), wrapper))
src = """
class ASTWrapper(object):
__module__ = 'typhon.nano.mast_generatedwrapper'
%s
@autohelp
@audited.DF
class ASTBuilder(Object):
__module__ = 'typhon.nano.mast_generatedwrapper'
_immutable=True
%s
""" % (''.join(wrapperClasses),''.join(methods))
d = {'ConstList': ConstList, 'userError': userError, 'Object': Object, 'audited': audited, 'method': method, 'autohelp': autohelp, 'SourceSpan': SourceSpan, 'rbigint': rbigint, 'NullObject': NullObject, 'MastIR': MastIR}
modname = "typhon.nano.mast_generatedwrapper"
fname = "typhon/nano/mast_generatedwrapper.py"
open(fname, 'w').write(src)
mod = ModuleType(modname)
mod.__loader__ = GeneratedCodeLoader(src)
mod.__dict__ .update(d)
mod.__file__ = fname
sys.modules[modname] = mod
exec py.code.Source(src).compile(fname) in mod.__dict__
linecache.getlines(fname, mod.__dict__)
return mod.ASTBuilder, mod.ASTWrapper
示例4: imports
# 需要导入模块: from types import ModuleType [as 别名]
# 或者: from types.ModuleType import __loader__ [as 别名]
# All the required objects have been loaded, acquire import lock to modify
# sys.modules and execute PEP302 requisites
imp.acquire_lock()
try:
# The module *must* be in sys.modules before executing the code in case
# the module code imports (directly or indirectly) itself (see PEP 302)
sys.modules[module_fullname] = module
if module_fullname_alias:
sys.modules[module_fullname_alias] = module
# This must be set for imports at least (see PEP 302)
module.__file__ = '<' + relative_url + '>'
# Only useful for get_source(), do it before exec'ing the source code
# so that the source code is properly display in case of error
module.__loader__ = self
module.__path__ = []
module.__name__ = module_fullname
self.__fullname_source_code_dict[module_fullname] = source_code_str
try:
# XXX: Any loading from ZODB while exec'ing the source code will result
# in a deadlock
source_code_obj = compile(source_code_str, module.__file__, 'exec')
exec source_code_obj in module.__dict__
except Exception, error:
del sys.modules[module_fullname]
if module_fullname_alias:
del sys.modules[module_fullname_alias]
raise ImportError(