mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
forward port r66386
This commit is contained in:
parent
d31fdc547b
commit
fcf5d639f5
9 changed files with 157 additions and 60 deletions
|
@ -19,7 +19,7 @@ __all__ = ["Error", "TestFailed", "TestSkipped", "ResourceDenied", "import_modul
|
|||
"is_resource_enabled", "requires", "find_unused_port", "bind_port",
|
||||
"fcmp", "is_jython", "TESTFN", "HOST", "FUZZ", "findfile", "verify",
|
||||
"vereq", "sortdict", "check_syntax_error", "open_urlresource",
|
||||
"catch_warning", "CleanImport", "EnvironmentVarGuard",
|
||||
"check_warnings", "CleanImport", "EnvironmentVarGuard",
|
||||
"TransientResource", "captured_output", "captured_stdout",
|
||||
"TransientResource", "transient_internet", "run_with_locale",
|
||||
"set_memlimit", "bigmemtest", "bigaddrspacetest", "BasicTestRunner",
|
||||
|
@ -53,7 +53,7 @@ class ResourceDenied(TestSkipped):
|
|||
def import_module(name, deprecated=False):
|
||||
"""Import the module to be tested, raising TestSkipped if it is not
|
||||
available."""
|
||||
with catch_warning(record=False):
|
||||
with warnings.catch_warnings():
|
||||
if deprecated:
|
||||
warnings.filterwarnings("ignore", ".+ (module|package)",
|
||||
DeprecationWarning)
|
||||
|
@ -368,17 +368,27 @@ def open_urlresource(url, *args, **kw):
|
|||
return open(fn, *args, **kw)
|
||||
|
||||
|
||||
def catch_warning(module=warnings, record=True):
|
||||
"""Guard the warnings filter from being permanently changed and
|
||||
optionally record the details of any warnings that are issued.
|
||||
|
||||
Use like this:
|
||||
|
||||
with catch_warning() as w:
|
||||
warnings.warn("foo")
|
||||
assert str(w.message) == "foo"
|
||||
class WarningsRecorder(object):
|
||||
"""Convenience wrapper for the warnings list returned on
|
||||
entry to the warnings.catch_warnings() context manager.
|
||||
"""
|
||||
return warnings.catch_warnings(record=record, module=module)
|
||||
def __init__(self, warnings_list):
|
||||
self.warnings = warnings_list
|
||||
|
||||
def __getattr__(self, attr):
|
||||
if self.warnings:
|
||||
return getattr(self.warnings[-1], attr)
|
||||
elif attr in warnings.WarningMessage._WARNING_DETAILS:
|
||||
return None
|
||||
raise AttributeError("%r has no attribute %r" % (self, attr))
|
||||
|
||||
def reset(self):
|
||||
del self.warnings[:]
|
||||
|
||||
@contextlib.contextmanager
|
||||
def check_warnings():
|
||||
with warnings.catch_warnings(record=True) as w:
|
||||
yield WarningsRecorder(w)
|
||||
|
||||
|
||||
class CleanImport(object):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue