本文整理匯總了Python中__builtin__.__import__方法的典型用法代碼示例。如果您正苦於以下問題:Python __builtin__.__import__方法的具體用法?Python __builtin__.__import__怎麽用?Python __builtin__.__import__使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類__builtin__
的用法示例。
在下文中一共展示了__builtin__.__import__方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: import_it
# 需要導入模塊: import __builtin__ [as 別名]
# 或者: from __builtin__ import __import__ [as 別名]
def import_it(self, partname, fqname, parent, force_load=0):
if not partname:
# completely empty module name should only happen in
# 'from . import' or __import__("")
return parent
if not force_load:
try:
return self.modules[fqname]
except KeyError:
pass
try:
path = parent and parent.__path__
except AttributeError:
return None
partname = str(partname)
stuff = self.loader.find_module(partname, path)
if not stuff:
return None
fqname = str(fqname)
m = self.loader.load_module(fqname, stuff)
if parent:
setattr(parent, partname, m)
return m
示例2: test_import_from_custom
# 需要導入模塊: import __builtin__ [as 別名]
# 或者: from __builtin__ import __import__ [as 別名]
def test_import_from_custom(self):
import __builtin__
try:
class foo(object):
b = 'abc'
def __import__(name, globals, locals, fromlist):
global received
received = name, fromlist
return foo()
saved = __builtin__.__import__
__builtin__.__import__ = __import__
from a import b
self.assertEqual(received, ('a', ('b', )))
finally:
__builtin__.__import__ = saved
示例3: deep_import_hook
# 需要導入模塊: import __builtin__ [as 別名]
# 或者: from __builtin__ import __import__ [as 別名]
def deep_import_hook(name, globals=None, locals=None, fromlist=None, level=-1):
"""Replacement for __import__()"""
parent, buf = get_parent(globals, level)
head, name, buf = load_next(parent, None if level < 0 else parent, name, buf)
tail = head
while name:
tail, name, buf = load_next(tail, tail, name, buf)
# If tail is None, both get_parent and load_next found
# an empty module name: someone called __import__("") or
# doctored faulty bytecode
if tail is None:
raise ValueError('Empty module name')
if not fromlist:
return head
ensure_fromlist(tail, fromlist, buf, 0)
return tail
示例4: do_import
# 需要導入模塊: import __builtin__ [as 別名]
# 或者: from __builtin__ import __import__ [as 別名]
def do_import(self, name, *args, **kwargs):
module = self._system_import(name, *args, **kwargs)
try:
activate_func = self._modules_to_patch.pop(name, None)
if activate_func:
activate_func() # call activate function
except:
if DebugInfoHolder.DEBUG_TRACE_LEVEL >= 2:
traceback.print_exc()
# Restore normal system importer to reduce performance impact
# of calling this method every time an import statement is invoked
if not self._modules_to_patch:
builtins.__import__ = self._system_import
return module
示例5: disable_splitbrain
# 需要導入模塊: import __builtin__ [as 別名]
# 或者: from __builtin__ import __import__ [as 別名]
def disable_splitbrain():
"""Disables (deactivates) the Splitbrain machinery"""
global _enabled
if not _enabled:
return
_enabled = False
for funcname, origfunc in _prev_builtins.items():
setattr(builtins, funcname, origfunc)
for modname, mod in sys.modules.items():
if isinstance(mod, RoutedModule):
sys.modules[modname] = mod.__realmod__
for ref in gc.get_referrers(mod):
if isinstance(ref, dict) and "__name__" in ref and ref.get("__file__") is not None:
for k, v in ref.items():
if v is mod:
ref[k] = mod.__realmod__
sys.modules["sys"] = sys
builtins.__import__ = _orig_import
示例6: _setup_base_package
# 需要導入模塊: import __builtin__ [as 別名]
# 或者: from __builtin__ import __import__ [as 別名]
def _setup_base_package(module_name):
try:
mod = __import__(module_name, None, None, ['__name__'])
except ImportError:
mod = None
if '.' in module_name:
parent_mod = __import__(module_name.rsplit('.', 1)[0],
None, None, ['__name__'])
else:
parent_mod = None
if mod is None:
mod = _IntentionallyEmptyModule(module_name)
if parent_mod is not None:
setattr(parent_mod, module_name.rsplit('.', 1)[-1], mod)
sys.modules[module_name] = mod
示例7: testWithUserDefinedImport
# 需要導入模塊: import __builtin__ [as 別名]
# 或者: from __builtin__ import __import__ [as 別名]
def testWithUserDefinedImport(self):
"""test cPickle calling a user defined import function."""
# This tests the fix for http://bugs.jython.org/issue1665
# setup
original_import = __builtin__.__import__
def import_hook(name, _globals=None, locals=None, fromlist=None, level= -1):
return original_import(name, _globals, locals, fromlist, level)
# test
__builtin__.__import__ = import_hook
try:
if "no_such_module" in sys.modules:
del sys.modules["no_such_module"] # force cPickle to call __import__
self.assertRaises(ImportError, cPickle.loads, pickle.GLOBAL + "no_such_module\n" + "no_such_class\n")
finally:
__builtin__.__import__ = original_import
示例8: test_renamed_bytecode
# 需要導入模塊: import __builtin__ [as 別名]
# 或者: from __builtin__ import __import__ [as 別名]
def test_renamed_bytecode(self):
source = safe_mktemp(dir=self.dir, suffix='.py')
fp = open(source, 'w')
fp.write("test = 'imported'")
fp.close()
module = os.path.basename(source)[:-3]
compiled = module + COMPILED_SUFFIX
# Compile to bytecode
module_obj = __import__(module)
self.assertEquals(module_obj.test, 'imported')
self.assert_(os.path.exists(compiled))
# Rename the bytecode
compiled_moved = safe_mktemp(dir=self.dir, suffix=COMPILED_SUFFIX)
os.rename(compiled, compiled_moved)
# Ensure we can still import the renamed bytecode
moved_module = os.path.basename(compiled_moved)[:-len(COMPILED_SUFFIX)]
module_obj = __import__(moved_module)
self.assertEquals(module_obj.__file__, os.path.basename(compiled_moved))
self.assertEquals(module_obj.test, 'imported')
示例9: test_symlinks
# 需要導入模塊: import __builtin__ [as 別名]
# 或者: from __builtin__ import __import__ [as 別名]
def test_symlinks(self):
# Ensure imports work over symlinks. Did not work in Jython from
# 2.1 to 2.5.0, fixed in 2.5.1 See
# http://bugs.jython.org/issue645615.
sym = test_support.TESTFN+"1"
try:
os.mkdir(test_support.TESTFN)
init = os.path.join(test_support.TESTFN, "__init__.py")
fp = open(init, 'w')
fp.write("test = 'imported'")
fp.close()
os.symlink(test_support.TESTFN, sym)
module = os.path.basename(sym)
module_obj = __import__(module)
self.assertEquals(module_obj.test, 'imported')
finally:
shutil.rmtree(test_support.TESTFN)
test_support.unlink(sym)
示例10: _import
# 需要導入模塊: import __builtin__ [as 別名]
# 或者: from __builtin__ import __import__ [as 別名]
def _import(self, mod, **kwds):
"""
Request the remote process import a module (or symbols from a module)
and return the proxied results. Uses built-in __import__() function, but
adds a bit more processing:
_import('module') => returns module
_import('module.submodule') => returns submodule
(note this differs from behavior of __import__)
_import('module', fromlist=[name1, name2, ...]) => returns [module.name1, module.name2, ...]
(this also differs from behavior of __import__)
"""
return self.send(request='import', callSync='sync', opts=dict(module=mod), **kwds)
示例11: install
# 需要導入模塊: import __builtin__ [as 別名]
# 或者: from __builtin__ import __import__ [as 別名]
def install(self):
self.save_import_module = __builtin__.__import__
self.save_reload = __builtin__.reload
if not hasattr(__builtin__, 'unload'):
__builtin__.unload = None
self.save_unload = __builtin__.unload
__builtin__.__import__ = self.import_module
__builtin__.reload = self.reload
__builtin__.unload = self.unload
示例12: uninstall
# 需要導入模塊: import __builtin__ [as 別名]
# 或者: from __builtin__ import __import__ [as 別名]
def uninstall(self):
__builtin__.__import__ = self.save_import_module
__builtin__.reload = self.save_reload
__builtin__.unload = self.save_unload
if not __builtin__.unload:
del __builtin__.unload
示例13: test_redefine_import
# 需要導入模塊: import __builtin__ [as 別名]
# 或者: from __builtin__ import __import__ [as 別名]
def test_redefine_import(self):
# redefining global __import__ shouldn't change import semantics
global __import__
global called
called = False
def __import__(*args):
global called
called = True
self.assertEqual(called, False)
del __import__
called = False
self.assertEqual(called, False)
示例14: test_import_hooks_import_precence
# 需要導入模塊: import __builtin__ [as 別名]
# 或者: from __builtin__ import __import__ [as 別名]
def test_import_hooks_import_precence(self):
"""__import__ takes precedence over import hooks"""
global myimpCalled
myimpCalled = None
class myimp(object):
def find_module(self, fullname, path=None):
global myimpCalled
myimpCalled = fullname, path
def myimport(*args):
return 'myimport'
import distutils
import distutils.command
mi = myimp()
sys.meta_path.append(mi)
builtinimp = get_builtins_dict()['__import__']
try:
get_builtins_dict()['__import__'] = myimport
import abc
self.assertEqual(abc, 'myimport')
self.assertEqual(myimpCalled, None)
# reload on a built-in hits the loader protocol
reload(distutils)
self.assertEqual(myimpCalled, ('distutils', None))
reload(distutils.command)
self.assertEqual(myimpCalled[0], 'distutils.command')
self.assertEqual(myimpCalled[1][0][-7:], 'distutils')
finally:
get_builtins_dict()['__import__'] = builtinimp
sys.meta_path.remove(mi)
示例15: test_import_kw_args
# 需要導入模塊: import __builtin__ [as 別名]
# 或者: from __builtin__ import __import__ [as 別名]
def test_import_kw_args(self):
self.assertEqual(__import__(name = 'sys', globals = globals(), locals = locals(), fromlist = [], level = -1), sys)