python中的for in finder.find_Python importlib 模块,find_loader() 实例源码 - 编程字典

def find_module(self, fullname, path=None):

# If the module being imported is not one we have registered

# post import hooks for, we can return immediately. We will

# take no further part in the importing of this module.

if not fullname in _post_import_hooks:

return None

# When we are interested in a specific module, we will call back

# into the import system a second time to defer to the import

# finder that is supposed to handle the importing of the module.

# We set an in progress flag for the target module so that on

# the second time through we don't trigger another call back

# into the import system and cause a infinite loop.

if fullname in self.in_progress:

return None

self.in_progress[fullname] = True

# Now call back into the import system again.

try:

if PY3:

# For Python 3 we need to use find_loader() from

# the importlib module. It doesn't actually

# import the target module and only finds the

# loader. If a loader is found, we need to return

# our own loader which will then in turn call the

# real loader to import the module and invoke the

# post import hooks.

loader = importlib.find_loader(fullname, path)

if loader:

return _ImportHookChainedLoader(loader)

else:

# For Python 2 we don't have much choice but to

# call back in to __import__(). This will

# actually cause the module to be imported. If no

# module could be found then ImportError will be

# raised. Otherwise we return a loader which

# returns the already loaded module and invokes

# the post import hooks.

__import__(fullname)

return _ImportHookLoader()

finally:

del self.in_progress[fullname]

# Decorator for marking that a function should be called as a post

# import hook when the target module is imported.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值