本文整理匯總了Python中builtins.__import__方法的典型用法代碼示例。如果您正苦於以下問題:Python builtins.__import__方法的具體用法?Python builtins.__import__怎麽用?Python builtins.__import__使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類builtins
的用法示例。
在下文中一共展示了builtins.__import__方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: __import__
# 需要導入模塊: import builtins [as 別名]
# 或者: from builtins import __import__ [as 別名]
def __import__(self, name, globals=None, locals=None, fromlist=(), level=0):
module = self._orig___import__(name, globals, locals, fromlist, level)
self.reload(module)
if fromlist:
from_names = [
name
for item in fromlist
for name in (
getattr(module, '__all__', []) if item == '*' else (item,)
)
]
for name in from_names:
value = getattr(module, name, None)
if ismodule(value):
self.reload(value)
return module
示例2: test_jupyter_no_ffmpy
# 需要導入模塊: import builtins [as 別名]
# 或者: from builtins import __import__ [as 別名]
def test_jupyter_no_ffmpy(benchmark_audio, monkeypatch):
import_orig = builtins.__import__
def mocked_import(name, globals_, locals_, fromlist, level):
if name == 'ffmpy':
raise ImportError()
return import_orig(name, globals_, locals_, fromlist, level)
monkeypatch.setattr(builtins, '__import__', mocked_import)
for key, path in benchmark_audio.items():
s1 = nussl.AudioSignal(path)
audio_element = nussl.play_utils.embed_audio(s1)
assert os.path.splitext(audio_element.filename)[-1] == '.wav'
monkeypatch.undo()
示例3: importing_fromlist_aggresively
# 需要導入模塊: import builtins [as 別名]
# 或者: from builtins import __import__ [as 別名]
def importing_fromlist_aggresively(modules):
orig___import__ = builtins.__import__
@functools.wraps(orig___import__)
def __import__(name, globals=None, locals=None, fromlist=(), level=0):
module = orig___import__(name, globals, locals, fromlist, level)
if fromlist and module.__name__ in modules:
if '*' in fromlist:
fromlist = list(fromlist)
fromlist.remove('*')
fromlist.extend(getattr(module, '__all__', []))
for x in fromlist:
if isinstance(getattr(module, x, None), types.ModuleType):
from_name = '{}.{}'.format(module.__name__, x)
if from_name in modules:
importlib.import_module(from_name)
return module
builtins.__import__ = __import__
try:
yield
finally:
builtins.__import__ = orig___import__
示例4: test_discovery_failed_discovery
# 需要導入模塊: import builtins [as 別名]
# 或者: from builtins import __import__ [as 別名]
def test_discovery_failed_discovery(self):
loader = unittest.TestLoader()
package = types.ModuleType('package')
orig_import = __import__
def _import(packagename, *args, **kwargs):
sys.modules[packagename] = package
return package
def cleanup():
builtins.__import__ = orig_import
self.addCleanup(cleanup)
builtins.__import__ = _import
with self.assertRaises(TypeError) as cm:
loader.discover('package')
self.assertEqual(str(cm.exception),
'don\'t know how to discover from {!r}'
.format(package))
示例5: test_failing_import_sticks
# 需要導入模塊: import builtins [as 別名]
# 或者: from builtins import __import__ [as 別名]
def test_failing_import_sticks(self):
source = TESTFN + ".py"
with open(source, "w") as f:
print("a = 1/0", file=f)
# New in 2.4, we shouldn't be able to import that no matter how often
# we try.
sys.path.insert(0, os.curdir)
importlib.invalidate_caches()
if TESTFN in sys.modules:
del sys.modules[TESTFN]
try:
for i in [1, 2, 3]:
self.assertRaises(ZeroDivisionError, __import__, TESTFN)
self.assertNotIn(TESTFN, sys.modules,
"damaged module in sys.modules on %i try" % i)
finally:
del sys.path[0]
remove_files(TESTFN)
示例6: test_file_to_source
# 需要導入模塊: import builtins [as 別名]
# 或者: from builtins import __import__ [as 別名]
def test_file_to_source(self):
# check if __file__ points to the source file where available
source = TESTFN + ".py"
with open(source, "w") as f:
f.write("test = None\n")
sys.path.insert(0, os.curdir)
try:
mod = __import__(TESTFN)
self.assertTrue(mod.__file__.endswith('.py'))
os.remove(source)
del sys.modules[TESTFN]
make_legacy_pyc(source)
importlib.invalidate_caches()
mod = __import__(TESTFN)
base, ext = os.path.splitext(mod.__file__)
self.assertEqual(ext, '.pyc')
finally:
del sys.path[0]
remove_files(TESTFN)
if TESTFN in sys.modules:
del sys.modules[TESTFN]
示例7: test_timestamp_overflow
# 需要導入模塊: import builtins [as 別名]
# 或者: from builtins import __import__ [as 別名]
def test_timestamp_overflow(self):
# A modification timestamp larger than 2**32 should not be a problem
# when importing a module (issue #11235).
sys.path.insert(0, os.curdir)
try:
source = TESTFN + ".py"
compiled = importlib.util.cache_from_source(source)
with open(source, 'w') as f:
pass
try:
os.utime(source, (2 ** 33 - 5, 2 ** 33 - 5))
except OverflowError:
self.skipTest("cannot set modification time to large integer")
except OSError as e:
if e.errno not in (getattr(errno, 'EOVERFLOW', None),
getattr(errno, 'EINVAL', None)):
raise
self.skipTest("cannot set modification time to large integer ({})".format(e))
__import__(TESTFN)
# The pyc file was created.
os.stat(compiled)
finally:
del sys.path[0]
remove_files(TESTFN)
示例8: test_override_builtin
# 需要導入模塊: import builtins [as 別名]
# 或者: from builtins import __import__ [as 別名]
def test_override_builtin(self):
# Test that overriding builtins.__import__ can bypass sys.modules.
import os
def foo():
import os
return os
self.assertEqual(foo(), os) # Quick sanity check.
with swap_attr(builtins, "__import__", lambda *x: 5):
self.assertEqual(foo(), 5)
# Test what happens when we shadow __import__ in globals(); this
# currently does not impact the import process, but if this changes,
# other code will need to change, so keep this test as a tripwire.
with swap_item(globals(), "__import__", lambda *x: 5):
self.assertEqual(foo(), os)
示例9: test_package___cached__
# 需要導入模塊: import builtins [as 別名]
# 或者: from builtins import __import__ [as 別名]
def test_package___cached__(self):
# Like test___cached__ but for packages.
def cleanup():
rmtree('pep3147')
unload('pep3147.foo')
unload('pep3147')
os.mkdir('pep3147')
self.addCleanup(cleanup)
# Touch the __init__.py
with open(os.path.join('pep3147', '__init__.py'), 'w'):
pass
with open(os.path.join('pep3147', 'foo.py'), 'w'):
pass
importlib.invalidate_caches()
m = __import__('pep3147.foo')
init_pyc = importlib.util.cache_from_source(
os.path.join('pep3147', '__init__.py'))
self.assertEqual(m.__cached__, os.path.join(os.curdir, init_pyc))
foo_pyc = importlib.util.cache_from_source(os.path.join('pep3147', 'foo.py'))
self.assertEqual(sys.modules['pep3147.foo'].__cached__,
os.path.join(os.curdir, foo_pyc))
示例10: test_file_to_source
# 需要導入模塊: import builtins [as 別名]
# 或者: from builtins import __import__ [as 別名]
def test_file_to_source(self):
# check if __file__ points to the source file where available
source = TESTFN + ".py"
with open(source, "w") as f:
f.write("test = None\n")
sys.path.insert(0, os.curdir)
try:
mod = __import__(TESTFN)
self.assertTrue(mod.__file__.endswith('.py'))
os.remove(source)
del sys.modules[TESTFN]
make_legacy_pyc(source)
importlib.invalidate_caches()
mod = __import__(TESTFN)
base, ext = os.path.splitext(mod.__file__)
self.assertIn(ext, ('.pyc', '.pyo'))
finally:
del sys.path[0]
remove_files(TESTFN)
if TESTFN in sys.modules:
del sys.modules[TESTFN]
示例11: test_timestamp_overflow
# 需要導入模塊: import builtins [as 別名]
# 或者: from builtins import __import__ [as 別名]
def test_timestamp_overflow(self):
# A modification timestamp larger than 2**32 should not be a problem
# when importing a module (issue #11235).
sys.path.insert(0, os.curdir)
try:
source = TESTFN + ".py"
compiled = importlib.util.cache_from_source(source)
with open(source, 'w') as f:
pass
try:
os.utime(source, (2 ** 33 - 5, 2 ** 33 - 5))
except OverflowError:
self.skipTest("cannot set modification time to large integer")
except OSError as e:
if e.errno != getattr(errno, 'EOVERFLOW', None):
raise
self.skipTest("cannot set modification time to large integer ({})".format(e))
__import__(TESTFN)
# The pyc file was created.
os.stat(compiled)
finally:
del sys.path[0]
remove_files(TESTFN)
示例12: test_import_from_custom
# 需要導入模塊: import builtins [as 別名]
# 或者: from builtins import __import__ [as 別名]
def test_import_from_custom(self):
import builtins
try:
class foo(object):
b = 'abc'
def __import__(name, globals, locals, fromlist, level):
global received
received = name, fromlist
return foo()
saved = builtins.__import__
builtins.__import__ = __import__
from a import b
self.assertEqual(received, ('a', ('b', )))
finally:
builtins.__import__ = saved
示例13: restore___import__
# 需要導入模塊: import builtins [as 別名]
# 或者: from builtins import __import__ [as 別名]
def restore___import__(self, import_):
builtins.__import__ = import_
示例14: _import
# 需要導入模塊: import builtins [as 別名]
# 或者: from builtins 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)
示例15: __import__
# 需要導入模塊: import builtins [as 別名]
# 或者: from builtins import __import__ [as 別名]
def __import__(name, globals={}, locals={}, fromlist=[], level=-1):
"""Compatibility definition for Python 2.4.
Silently ignore the `level` argument missing in Python < 2.5.
"""
# we need the level arg because the default changed in Python 3.3
return builtins.__import__(name, globals, locals, fromlist)