我们从Python开源项目中,提取了以下22个代码示例,用于说明如何使用test.__name__()。
def test_replace_parent_in_sys_modules(self): dir_name = os.path.abspath(TESTFN) os.mkdir(dir_name) try: pkg_dir = os.path.join(dir_name, 'sa') os.mkdir(pkg_dir) with open(os.path.join(pkg_dir, '__init__.py'), 'w') as init_file: init_file.write("import v1") with open(os.path.join(pkg_dir, 'v1.py'), 'w') as v1_file: v1_file.write("import sys;" "sys.modules['sa'] = sys.modules[__name__];" "import sa") sys.path.insert(0, dir_name) # a segfault means the test failed! import sa finally: rmtree(dir_name)
def test_import_name_binding(self): # import x.y.z binds x in the current namespace import test as x import test.support self.assertTrue(x is test, x.__name__) self.assertTrue(hasattr(test.support, "__file__")) # import x.y.z as w binds z as w import test.support as y self.assertTrue(y is test.support, y.__name__)
def test_import_name_binding(self): # import x.y.z binds x in the current namespace. import test as x import test.support self.assertIs(x, test, x.__name__) self.assertTrue(hasattr(test.support, "__file__")) # import x.y.z as w binds z as w. import test.support as y self.assertIs(y, test.support, y.__name__)
def test_issue3221(self): # Note for mergers: the 'absolute' tests from the 2.x branch # are missing in Py3k because implicit relative imports are # a thing of the past # # Regression test for http://bugs.python.org/issue3221. def check_relative(): exec("from . import relimport", ns) # Check relative import OK with __package__ and __name__ correct ns = dict(__package__='test', __name__='test.notarealmodule') check_relative() # Check relative import OK with only __name__ wrong ns = dict(__package__='test', __name__='notarealpkg.notarealmodule') check_relative() # Check relative import fails with only __package__ wrong ns = dict(__package__='foo', __name__='test.notarealmodule') self.assertRaises(SystemError, check_relative) # Check relative import fails with __package__ and __name__ wrong ns = dict(__package__='foo', __name__='notarealpkg.notarealmodule') self.assertRaises(SystemError, check_relative) # Check relative import fails with package set to a non-string ns = dict(__package__=object()) self.assertRaises(ValueError, check_relative)
def test_import_name_binding(self): # import x.y.z binds x in the current namespace. import test as x import test.test_support self.assertIs(x, test, x.__name__) self.assertTrue(hasattr(test.test_support, "__file__")) # import x.y.z as w binds z as w. import test.test_support as y self.assertIs(y, test.test_support, y.__name__)
def test_issue3221(self): # Regression test for http://bugs.python.org/issue3221. def check_absolute(): exec "from os import path" in ns def check_relative(): exec "from . import relimport" in ns # Check both OK with __package__ and __name__ correct ns = dict(__package__='test', __name__='test.notarealmodule') check_absolute() check_relative() # Check both OK with only __name__ wrong ns = dict(__package__='test', __name__='notarealpkg.notarealmodule') check_absolute() check_relative() # Check relative fails with only __package__ wrong ns = dict(__package__='foo', __name__='test.notarealmodule') with check_warnings(('.+foo', RuntimeWarning)): check_absolute() self.assertRaises(SystemError, check_relative) # Check relative fails with __package__ and __name__ wrong ns = dict(__package__='foo', __name__='notarealpkg.notarealmodule') with check_warnings(('.+foo', RuntimeWarning)): check_absolute() self.assertRaises(SystemError, check_relative) # Check both fail with package set to a non-string ns = dict(__package__=object()) self.assertRaises(ValueError, check_absolute) self.assertRaises(ValueError, check_relative)
def test_issue3221(self): # Note for mergers: the 'absolute' tests from the 2.x branch # are missing in Py3k because implicit relative imports are # a thing of the past # # Regression test for http://bugs.python.org/issue3221. def check_relative(): exec("from . import relimport", ns) # Check relative import OK with __package__ and __name__ correct ns = dict(__package__='test', __name__='test.notarealmodule') check_relative() # Check relative import OK with only __name__ wrong ns = dict(__package__='test', __name__='notarealpkg.notarealmodule') check_relative() # Check relative import fails with only __package__ wrong ns = dict(__package__='foo', __name__='test.notarealmodule') self.assertRaises(SystemError, check_relative) # Check relative import fails with __package__ and __name__ wrong ns = dict(__package__='foo', __name__='notarealpkg.notarealmodule') self.assertRaises(SystemError, check_relative) # Check relative import fails with package set to a non-string ns = dict(__package__=object()) self.assertRaises(TypeError, check_relative)
def test_frozen_importlib_is_bootstrap(self): from importlib import _bootstrap mod = sys.modules['_frozen_importlib'] self.assertIs(mod, _bootstrap) self.assertEqual(mod.__name__, 'importlib._bootstrap') self.assertEqual(mod.__package__, 'importlib') self.assertTrue(mod.__file__.endswith('_bootstrap.py'), mod.__file__)
def test_import_name_binding(self): # import x.y.z binds x in the current namespace import test as x import test.support self.assertIs(x, test, x.__name__) self.assertTrue(hasattr(test.support, "__file__")) # import x.y.z as w binds z as w import test.support as y self.assertIs(y, test.support, y.__name__)