本文整理汇总了Python中test.support.DirsOnSysPath方法的典型用法代码示例。如果您正苦于以下问题:Python support.DirsOnSysPath方法的具体用法?Python support.DirsOnSysPath怎么用?Python support.DirsOnSysPath使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类test.support
的用法示例。
在下文中一共展示了support.DirsOnSysPath方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_no_import_lock_contention
# 需要导入模块: from test import support [as 别名]
# 或者: from test.support import DirsOnSysPath [as 别名]
def test_no_import_lock_contention(self):
with support.temp_cwd():
module_name = 'imported_by_an_imported_module'
with open(module_name + '.py', 'w') as f:
f.write("""if 1:
import multiprocessing
q = multiprocessing.Queue()
q.put('knock knock')
q.get(timeout=3)
q.close()
""")
with support.DirsOnSysPath(os.getcwd()):
try:
__import__(module_name)
except Queue.Empty:
self.fail("Probable regression on import lock contention;"
" see Issue #22853")
示例2: temp_module
# 需要导入模块: from test import support [as 别名]
# 或者: from test.support import DirsOnSysPath [as 别名]
def temp_module(name, content='', *, pkg=False):
conflicts = [n for n in sys.modules if n.partition('.')[0] == name]
with support.temp_cwd(None) as cwd:
with uncache(name, *conflicts):
with support.DirsOnSysPath(cwd):
invalidate_caches()
location = os.path.join(cwd, name)
if pkg:
modpath = os.path.join(location, '__init__.py')
os.mkdir(name)
else:
modpath = location + '.py'
if content is None:
# Make sure the module file gets created.
content = ''
if content is not None:
# not a namespace package
with open(modpath, 'w') as modfile:
modfile.write(content)
yield location
示例3: test_discovery_failed_discovery
# 需要导入模块: from test import support [as 别名]
# 或者: from test.support import DirsOnSysPath [as 别名]
def test_discovery_failed_discovery(self):
loader = unittest.TestLoader()
package = types.ModuleType('package')
def _import(packagename, *args, **kwargs):
sys.modules[packagename] = package
return package
with unittest.mock.patch('builtins.__import__', _import):
# Since loader.discover() can modify sys.path, restore it when done.
with support.DirsOnSysPath():
# Make sure to remove 'package' from sys.modules when done.
with test.test_importlib.util.uncache('package'):
with self.assertRaises(TypeError) as cm:
loader.discover('package')
self.assertEqual(str(cm.exception),
'don\'t know how to discover from {!r}'
.format(package))
示例4: test_DirsOnSysPath
# 需要导入模块: from test import support [as 别名]
# 或者: from test.support import DirsOnSysPath [as 别名]
def test_DirsOnSysPath(self):
with support.DirsOnSysPath('foo', 'bar'):
self.assertIn("foo", sys.path)
self.assertIn("bar", sys.path)
self.assertNotIn("foo", sys.path)
self.assertNotIn("bar", sys.path)
示例5: import_tool
# 需要导入模块: from test import support [as 别名]
# 或者: from test.support import DirsOnSysPath [as 别名]
def import_tool(toolname):
with support.DirsOnSysPath(scriptsdir):
return importlib.import_module(toolname)
示例6: setUp
# 需要导入模块: from test import support [as 别名]
# 或者: from test.support import DirsOnSysPath [as 别名]
def setUp(self):
self.tempdir = TESTFN + '_dir'
os.mkdir(self.tempdir)
with open(os.path.join(self.tempdir,
'inspect_fodder3%spy' % os.extsep), 'w') as f:
f.write("class X:\n pass # No EOL")
with DirsOnSysPath(self.tempdir):
import inspect_fodder3 as mod3
self.fodderModule = mod3
super().setUp()
示例7: test_discovery_from_dotted_namespace_packages
# 需要导入模块: from test import support [as 别名]
# 或者: from test.support import DirsOnSysPath [as 别名]
def test_discovery_from_dotted_namespace_packages(self):
loader = unittest.TestLoader()
package = types.ModuleType('package')
package.__path__ = ['/a', '/b']
package.__spec__ = types.SimpleNamespace(
loader=None,
submodule_search_locations=['/a', '/b']
)
def _import(packagename, *args, **kwargs):
sys.modules[packagename] = package
return package
_find_tests_args = []
def _find_tests(start_dir, pattern, namespace=None):
_find_tests_args.append((start_dir, pattern))
return ['%s/tests' % start_dir]
loader._find_tests = _find_tests
loader.suiteClass = list
with unittest.mock.patch('builtins.__import__', _import):
# Since loader.discover() can modify sys.path, restore it when done.
with support.DirsOnSysPath():
# Make sure to remove 'package' from sys.modules when done.
with test.test_importlib.util.uncache('package'):
suite = loader.discover('package')
self.assertEqual(suite, ['/a/tests', '/b/tests'])
示例8: __init__
# 需要导入模块: from test import support [as 别名]
# 或者: from test.support import DirsOnSysPath [as 别名]
def __init__(self, *args, **kwargs):
self.tempdir = TESTFN + '_dir'
os.mkdir(self.tempdir)
with open(os.path.join(self.tempdir,
'inspect_fodder3%spy' % os.extsep), 'w') as f:
f.write("class X:\n pass # No EOL")
with DirsOnSysPath(self.tempdir):
import inspect_fodder3 as mod3
self.fodderModule = mod3
GetSourceBase.__init__(self, *args, **kwargs)
示例9: test_reload_location_changed
# 需要导入模块: from test import support [as 别名]
# 或者: from test.support import DirsOnSysPath [as 别名]
def test_reload_location_changed(self):
name = 'spam'
with support.temp_cwd(None) as cwd:
with test_util.uncache('spam'):
with support.DirsOnSysPath(cwd):
# Start as a plain module.
self.init.invalidate_caches()
path = os.path.join(cwd, name + '.py')
cached = self.util.cache_from_source(path)
expected = {'__name__': name,
'__package__': '',
'__file__': path,
'__cached__': cached,
'__doc__': None,
}
support.create_empty_file(path)
module = self.init.import_module(name)
ns = vars(module).copy()
loader = ns.pop('__loader__')
spec = ns.pop('__spec__')
ns.pop('__builtins__', None) # An implementation detail.
self.assertEqual(spec.name, name)
self.assertEqual(spec.loader, loader)
self.assertEqual(loader.path, path)
self.assertEqual(ns, expected)
# Change to a package.
self.init.invalidate_caches()
init_path = os.path.join(cwd, name, '__init__.py')
cached = self.util.cache_from_source(init_path)
expected = {'__name__': name,
'__package__': name,
'__file__': init_path,
'__cached__': cached,
'__path__': [os.path.dirname(init_path)],
'__doc__': None,
}
os.mkdir(name)
os.rename(path, init_path)
reloaded = self.init.reload(module)
ns = vars(reloaded).copy()
loader = ns.pop('__loader__')
spec = ns.pop('__spec__')
ns.pop('__builtins__', None) # An implementation detail.
self.assertEqual(spec.name, name)
self.assertEqual(spec.loader, loader)
self.assertIs(reloaded, module)
self.assertEqual(loader.path, init_path)
self.maxDiff = None
self.assertEqual(ns, expected)
示例10: test_reload_location_changed
# 需要导入模块: from test import support [as 别名]
# 或者: from test.support import DirsOnSysPath [as 别名]
def test_reload_location_changed(self):
name = 'spam'
with support.temp_cwd(None) as cwd:
with util.uncache('spam'):
with support.DirsOnSysPath(cwd):
# Start as a plain module.
self.init.invalidate_caches()
path = os.path.join(cwd, name + '.py')
cached = self.util.cache_from_source(path)
expected = {'__name__': name,
'__package__': '',
'__file__': path,
'__cached__': cached,
'__doc__': None,
}
support.create_empty_file(path)
module = self.init.import_module(name)
ns = vars(module).copy()
loader = ns.pop('__loader__')
spec = ns.pop('__spec__')
ns.pop('__builtins__', None) # An implementation detail.
self.assertEqual(spec.name, name)
self.assertEqual(spec.loader, loader)
self.assertEqual(loader.path, path)
self.assertEqual(ns, expected)
# Change to a package.
self.init.invalidate_caches()
init_path = os.path.join(cwd, name, '__init__.py')
cached = self.util.cache_from_source(init_path)
expected = {'__name__': name,
'__package__': name,
'__file__': init_path,
'__cached__': cached,
'__path__': [os.path.dirname(init_path)],
'__doc__': None,
}
os.mkdir(name)
os.rename(path, init_path)
reloaded = self.init.reload(module)
ns = vars(reloaded).copy()
loader = ns.pop('__loader__')
spec = ns.pop('__spec__')
ns.pop('__builtins__', None) # An implementation detail.
self.assertEqual(spec.name, name)
self.assertEqual(spec.loader, loader)
self.assertIs(reloaded, module)
self.assertEqual(loader.path, init_path)
self.maxDiff = None
self.assertEqual(ns, expected)