本文整理汇总了Python中astroid.manager.AstroidManager.register_failed_import_hook方法的典型用法代码示例。如果您正苦于以下问题:Python AstroidManager.register_failed_import_hook方法的具体用法?Python AstroidManager.register_failed_import_hook怎么用?Python AstroidManager.register_failed_import_hook使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类astroid.manager.AstroidManager
的用法示例。
在下文中一共展示了AstroidManager.register_failed_import_hook方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: AstroidManagerTest
# 需要导入模块: from astroid.manager import AstroidManager [as 别名]
# 或者: from astroid.manager.AstroidManager import register_failed_import_hook [as 别名]
#.........这里部分代码省略.........
def test_ast_from_module(self):
astroid = self.manager.ast_from_module(unittest)
self.assertEqual(astroid.pure_python, True)
import time
astroid = self.manager.ast_from_module(time)
self.assertEqual(astroid.pure_python, False)
def test_ast_from_module_cache(self):
"""check if the module is in the cache manager"""
astroid = self.manager.ast_from_module(unittest)
self.assertEqual(astroid.name, 'unittest')
self.assertIn('unittest', self.manager.astroid_cache)
def test_ast_from_class(self):
astroid = self.manager.ast_from_class(int)
self.assertEqual(astroid.name, 'int')
self.assertEqual(astroid.parent.frame().name, BUILTINS)
astroid = self.manager.ast_from_class(object)
self.assertEqual(astroid.name, 'object')
self.assertEqual(astroid.parent.frame().name, BUILTINS)
self.assertIn('__setattr__', astroid)
def test_ast_from_class_with_module(self):
"""check if the method works with the module name"""
astroid = self.manager.ast_from_class(int, int.__module__)
self.assertEqual(astroid.name, 'int')
self.assertEqual(astroid.parent.frame().name, BUILTINS)
astroid = self.manager.ast_from_class(object, object.__module__)
self.assertEqual(astroid.name, 'object')
self.assertEqual(astroid.parent.frame().name, BUILTINS)
self.assertIn('__setattr__', astroid)
def test_ast_from_class_attr_error(self):
"""give a wrong class at the ast_from_class method"""
self.assertRaises(AstroidBuildingException, self.manager.ast_from_class, None)
def test_from_directory(self):
self.assertEqual(self.project.name, 'data')
self.assertEqual(self.project.path,
os.path.abspath(resources.find('data/__init__.py')))
def test_project_node(self):
expected = [
'data',
'data.SSL1',
'data.SSL1.Connection1',
'data.absimp',
'data.absimp.sidepackage',
'data.absimp.string',
'data.absimport',
'data.all',
'data.appl',
'data.appl.myConnection',
'data.clientmodule_test',
'data.descriptor_crash',
'data.email',
'data.find_test',
'data.find_test.module',
'data.find_test.module2',
'data.find_test.noendingnewline',
'data.find_test.nonregr',
'data.format',
'data.lmfp',
'data.lmfp.foo',
'data.module',
'data.module1abs',
'data.module1abs.core',
'data.module2',
'data.noendingnewline',
'data.nonregr',
'data.notall',
'data.package',
'data.package.absimport',
'data.package.hello',
'data.package.import_package_subpackage_module',
'data.package.subpackage',
'data.package.subpackage.module',
'data.recursion',
'data.suppliermodule_test',
'data.unicode_package',
'data.unicode_package.core']
self.assertListEqual(sorted(self.project.keys()), expected)
def testFailedImportHooks(self):
def hook(modname):
if modname == 'foo.bar':
return unittest
else:
raise AstroidBuildingException()
with self.assertRaises(AstroidBuildingException):
self.manager.ast_from_module_name('foo.bar')
self.manager.register_failed_import_hook(hook)
self.assertEqual(unittest, self.manager.ast_from_module_name('foo.bar'))
with self.assertRaises(AstroidBuildingException):
self.manager.ast_from_module_name('foo.bar.baz')
del self.manager._failed_import_hooks[0]