本文整理汇总了Python中sys.module方法的典型用法代码示例。如果您正苦于以下问题:Python sys.module方法的具体用法?Python sys.module怎么用?Python sys.module使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sys
的用法示例。
在下文中一共展示了sys.module方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: inline_runsource
# 需要导入模块: import sys [as 别名]
# 或者: from sys import module [as 别名]
def inline_runsource(self, source, *cmdlineargs):
"""Run a test module in process using ``pytest.main()``.
This run writes "source" into a temporary file and runs
``pytest.main()`` on it, returning a :py:class:`HookRecorder` instance
for the result.
:param source: the source code of the test module
:param cmdlineargs: any extra command line arguments to use
:return: :py:class:`HookRecorder` instance of the result
"""
p = self.makepyfile(source)
values = list(cmdlineargs) + [p]
return self.inline_run(*values)
示例2: getitem
# 需要导入模块: import sys [as 别名]
# 或者: from sys import module [as 别名]
def getitem(self, source, funcname="test_func"):
"""Return the test item for a test function.
This writes the source to a python file and runs pytest's collection on
the resulting module, returning the test item for the requested
function name.
:param source: the module source
:param funcname: the name of the test function for which to return a
test item
"""
items = self.getitems(source)
for item in items:
if item.name == funcname:
return item
assert 0, "{!r} item not found in module:\n{}\nitems: {}".format(
funcname, source, items
)
示例3: _setWarningRegistryToNone
# 需要导入模块: import sys [as 别名]
# 或者: from sys import module [as 别名]
def _setWarningRegistryToNone(modules):
"""
Disable the per-module cache for every module found in C{modules}, typically
C{sys.modules}.
@param modules: Dictionary of modules, typically sys.module dict
"""
for v in list(modules.values()):
if v is not None:
try:
v.__warningregistry__ = None
except:
# Don't specify a particular exception type to handle in case
# some wacky object raises some wacky exception in response to
# the setattr attempt.
pass
示例4: run_module
# 需要导入模块: import sys [as 别名]
# 或者: from sys import module [as 别名]
def run_module(mod_name, init_globals=None,
run_name=None, alter_sys=False):
"""Execute a module's code without importing it
Returns the resulting top level namespace dictionary
"""
loader = get_loader(mod_name)
if loader is None:
raise ImportError("No module named " + mod_name)
code = loader.get_code(mod_name)
if code is None:
raise ImportError("No code object available for " + mod_name)
filename = _get_filename(loader, mod_name)
if run_name is None:
run_name = mod_name
return _run_module_code(code, init_globals, run_name,
filename, loader, alter_sys)
示例5: getitems
# 需要导入模块: import sys [as 别名]
# 或者: from sys import module [as 别名]
def getitems(self, source):
"""Return all test items collected from the module.
This writes the source to a python file and runs pytest's collection on
the resulting module, returning all test items contained within.
"""
modcol = self.getmodulecol(source)
return self.genitems([modcol])
示例6: getmodulecol
# 需要导入模块: import sys [as 别名]
# 或者: from sys import module [as 别名]
def getmodulecol(self, source, configargs=(), withinit=False):
"""Return the module collection node for ``source``.
This writes ``source`` to a file using :py:meth:`makepyfile` and then
runs the pytest collection on it, returning the collection node for the
test module.
:param source: the source code of the module to collect
:param configargs: any extra arguments to pass to
:py:meth:`parseconfigure`
:param withinit: whether to also write an ``__init__.py`` file to the
same directory to ensure it is a package
"""
if isinstance(source, Path):
path = self.tmpdir.join(str(source))
assert not withinit, "not supported for paths"
else:
kw = {self.request.function.__name__: Source(source).strip()}
path = self.makepyfile(**kw)
if withinit:
self.makepyfile(__init__="#")
self.config = config = self.parseconfigure(path, *configargs)
return self.getnode(config, path)
示例7: _collectWarnings
# 需要导入模块: import sys [as 别名]
# 或者: from sys import module [as 别名]
def _collectWarnings(observeWarning, f, *args, **kwargs):
"""
Call C{f} with C{args} positional arguments and C{kwargs} keyword arguments
and collect all warnings which are emitted as a result in a list.
@param observeWarning: A callable which will be invoked with a L{_Warning}
instance each time a warning is emitted.
@return: The return value of C{f(*args, **kwargs)}.
"""
def showWarning(message, category, filename, lineno, file=None, line=None):
assert isinstance(message, Warning)
observeWarning(_Warning(
str(message), category, filename, lineno))
# Disable the per-module cache for every module otherwise if the warning
# which the caller is expecting us to collect was already emitted it won't
# be re-emitted by the call to f which happens below.
_setWarningRegistryToNone(sys.modules)
origFilters = warnings.filters[:]
origShow = warnings.showwarning
warnings.simplefilter('always')
try:
warnings.showwarning = showWarning
result = f(*args, **kwargs)
finally:
warnings.filters[:] = origFilters
warnings.showwarning = origShow
return result
示例8: getSkip
# 需要导入模块: import sys [as 别名]
# 或者: from sys import module [as 别名]
def getSkip(self):
"""
Return the skip reason set on this test, if any is set. Checks on the
instance first, then the class, then the module, then packages. As
soon as it finds something with a C{skip} attribute, returns that.
Returns L{None} if it cannot find anything. See L{TestCase} docstring
for more details.
"""
return util.acquireAttribute(self._parents, 'skip', None)
示例9: getTodo
# 需要导入模块: import sys [as 别名]
# 或者: from sys import module [as 别名]
def getTodo(self):
"""
Return a L{Todo} object if the test is marked todo. Checks on the
instance first, then the class, then the module, then packages. As
soon as it finds something with a C{todo} attribute, returns that.
Returns L{None} if it cannot find anything. See L{TestCase} docstring
for more details.
"""
todo = util.acquireAttribute(self._parents, 'todo', None)
if todo is None:
return None
return makeTodo(todo)
示例10: _getSuppress
# 需要导入模块: import sys [as 别名]
# 或者: from sys import module [as 别名]
def _getSuppress(self):
"""
Returns any warning suppressions set for this test. Checks on the
instance first, then the class, then the module, then packages. As
soon as it finds something with a C{suppress} attribute, returns that.
Returns any empty list (i.e. suppress no warnings) if it cannot find
anything. See L{TestCase} docstring for more details.
"""
return util.acquireAttribute(self._parents, 'suppress', [])
示例11: getmodulecol
# 需要导入模块: import sys [as 别名]
# 或者: from sys import module [as 别名]
def getmodulecol(self, source, configargs=(), withinit=False):
"""Return the module collection node for ``source``.
This writes ``source`` to a file using :py:meth:`makepyfile` and then
runs the pytest collection on it, returning the collection node for the
test module.
:param source: the source code of the module to collect
:param configargs: any extra arguments to pass to
:py:meth:`parseconfigure`
:param withinit: whether to also write an ``__init__.py`` file to the
same directory to ensure it is a package
"""
if isinstance(source, Path):
path = self.tmpdir.join(str(source))
assert not withinit, "not supported for paths"
else:
kw = {self._name: Source(source).strip()}
path = self.makepyfile(**kw)
if withinit:
self.makepyfile(__init__="#")
self.config = config = self.parseconfigure(path, *configargs)
return self.getnode(config, path)
示例12: setUpClass
# 需要导入模块: import sys [as 别名]
# 或者: from sys import module [as 别名]
def setUpClass(cls_, module=module):
cls_._save_sys_modules = sys.modules.copy()
sys.modules[TESTS] = module
sys.modules['datetime'] = module.datetime_module
sys.modules['_strptime'] = module._strptime
示例13: exec_module
# 需要导入模块: import sys [as 别名]
# 或者: from sys import module [as 别名]
def exec_module(module):
if module.__name__ == SUBMOD_NAME:
raise ImportError('I cannot be loaded!')
示例14: test_name_requires_rparition
# 需要导入模块: import sys [as 别名]
# 或者: from sys import module [as 别名]
def test_name_requires_rparition(self):
# Raise TypeError if a non-string is passed in for the module name.
with self.assertRaises(TypeError):
self.__import__(42)
示例15: test_negative_level
# 需要导入模块: import sys [as 别名]
# 或者: from sys import module [as 别名]
def test_negative_level(self):
# Raise ValueError when a negative level is specified.
# PEP 328 did away with sys.module None entries and the ambiguity of
# absolute/relative imports.
with self.assertRaises(ValueError):
self.__import__('os', globals(), level=-1)