本文整理汇总了Python中test.test_support.CleanImport方法的典型用法代码示例。如果您正苦于以下问题:Python test_support.CleanImport方法的具体用法?Python test_support.CleanImport怎么用?Python test_support.CleanImport使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类test.test_support
的用法示例。
在下文中一共展示了test_support.CleanImport方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_imp_module
# 需要导入模块: from test import test_support [as 别名]
# 或者: from test.test_support import CleanImport [as 别名]
def test_imp_module(self):
# Verify that the imp module can correctly load and find .py files
# XXX (ncoghlan): It would be nice to use test_support.CleanImport
# here, but that breaks because the os module registers some
# handlers in copy_reg on import. Since CleanImport doesn't
# revert that registration, the module is left in a broken
# state after reversion. Reinitialising the module contents
# and just reverting os.environ to its previous state is an OK
# workaround
orig_path = os.path
orig_getenv = os.getenv
with EnvironmentVarGuard():
x = imp.find_module("os")
new_os = imp.load_module("os", *x)
self.assertIs(os, new_os)
self.assertIs(orig_path, new_os.path)
self.assertIsNot(orig_getenv, new_os.getenv)
示例2: check_removal
# 需要导入模块: from test import test_support [as 别名]
# 或者: from test.test_support import CleanImport [as 别名]
def check_removal(self, module_name, optional=False):
"""Make sure the specified module, when imported, raises a
DeprecationWarning and specifies itself in the message."""
with CleanImport(module_name), warnings.catch_warnings():
warnings.filterwarnings("error", ".+ (module|package) .+ removed",
DeprecationWarning, __name__)
warnings.filterwarnings("error", ".+ removed .+ (module|package)",
DeprecationWarning, __name__)
try:
__import__(module_name, level=0)
except DeprecationWarning as exc:
self.assertIn(module_name, exc.args[0],
"%s warning didn't contain module name"
% module_name)
except ImportError:
if not optional:
self.fail("Non-optional module {0} raised an "
"ImportError.".format(module_name))
else:
# For extension modules, check the __warningregistry__.
# They won't rerun their init code even with CleanImport.
if not check_deprecated_module(module_name):
self.fail("DeprecationWarning not raised for {0}"
.format(module_name))
示例3: check_removal
# 需要导入模块: from test import test_support [as 别名]
# 或者: from test.test_support import CleanImport [as 别名]
def check_removal(self, module_name, optional=False):
"""Make sure the specified module, when imported, raises a
DeprecationWarning and specifies itself in the message."""
if module_name in sys.modules:
mod = sys.modules[module_name]
filename = getattr(mod, '__file__', '')
mod = None
# the module is not implemented in C?
if not filename.endswith(('.py', '.pyc', '.pyo')):
# Issue #23375: If the module was already loaded, reimporting
# the module will not emit again the warning. The warning is
# emited when the module is loaded, but C modules cannot
# unloaded.
if test_support.verbose:
print("Cannot test the Python 3 DeprecationWarning of the "
"%s module, the C module is already loaded"
% module_name)
return
with CleanImport(module_name), warnings.catch_warnings():
warnings.filterwarnings("error", ".+ (module|package) .+ removed",
DeprecationWarning, __name__)
warnings.filterwarnings("error", ".+ removed .+ (module|package)",
DeprecationWarning, __name__)
try:
__import__(module_name, level=0)
except DeprecationWarning as exc:
self.assertIn(module_name, exc.args[0],
"%s warning didn't contain module name"
% module_name)
except ImportError:
if not optional:
self.fail("Non-optional module {0} raised an "
"ImportError.".format(module_name))
else:
# For extension modules, check the __warningregistry__.
# They won't rerun their init code even with CleanImport.
if not check_deprecated_module(module_name):
self.fail("DeprecationWarning not raised for {0}"
.format(module_name))
示例4: test_source
# 需要导入模块: from test import test_support [as 别名]
# 或者: from test.test_support import CleanImport [as 别名]
def test_source(self):
# XXX (ncoghlan): It would be nice to use test_support.CleanImport
# here, but that breaks because the os module registers some
# handlers in copy_reg on import. Since CleanImport doesn't
# revert that registration, the module is left in a broken
# state after reversion. Reinitialising the module contents
# and just reverting os.environ to its previous state is an OK
# workaround
with test_support.EnvironmentVarGuard():
import os
imp.reload(os)
示例5: test_extension
# 需要导入模块: from test import test_support [as 别名]
# 或者: from test.test_support import CleanImport [as 别名]
def test_extension(self):
with test_support.CleanImport('time'):
import time
imp.reload(time)
示例6: test_builtin
# 需要导入模块: from test import test_support [as 别名]
# 或者: from test.test_support import CleanImport [as 别名]
def test_builtin(self):
with test_support.CleanImport('marshal'):
import marshal
imp.reload(marshal)