18
19
"is_resource_enabled", "requires", "find_unused_port", "bind_port",
19
20
"fcmp", "have_unicode", "is_jython", "TESTFN", "HOST", "FUZZ",
20
21
"findfile", "verify", "vereq", "sortdict", "check_syntax_error",
21
"open_urlresource", "check_warnings", "CleanImport",
22
"EnvironmentVarGuard", "captured_output",
22
"open_urlresource", "check_warnings", "_check_py3k_warnings",
23
"CleanImport", "EnvironmentVarGuard", "captured_output",
23
24
"captured_stdout", "TransientResource", "transient_internet",
24
25
"run_with_locale", "set_memlimit", "bigmemtest", "bigaddrspacetest",
25
26
"BasicTestRunner", "run_unittest", "run_doctest", "threading_setup",
406
407
entry to the warnings.catch_warnings() context manager.
408
409
def __init__(self, warnings_list):
409
self.warnings = warnings_list
410
self._warnings = warnings_list
411
413
def __getattr__(self, attr):
413
return getattr(self.warnings[-1], attr)
414
if len(self._warnings) > self._last:
415
return getattr(self._warnings[-1], attr)
414
416
elif attr in warnings.WarningMessage._WARNING_DETAILS:
416
418
raise AttributeError("%r has no attribute %r" % (self, attr))
422
return self._warnings[self._last:]
421
@contextlib.contextmanager
422
def check_warnings():
425
self._last = len(self._warnings)
428
def _filterwarnings(filters, quiet=False):
429
"""Catch the warnings, then check if all the expected
430
warnings have been raised and re-raise unexpected warnings.
431
If 'quiet' is True, only re-raise the unexpected warnings.
433
# Clear the warning registry of the calling module
434
# in order to re-raise the warnings.
435
frame = sys._getframe(2)
436
registry = frame.f_globals.get('__warningregistry__')
423
439
with warnings.catch_warnings(record=True) as w:
440
# Set filter "always" to record all warnings. Because
441
# test_warnings swap the module, we need to look up in
442
# the sys.modules dictionary.
443
sys.modules['warnings'].simplefilter("always")
424
444
yield WarningsRecorder(w)
445
# Filter the recorded warnings
446
reraise = [warning.message for warning in w]
448
for msg, cat in filters:
450
for exc in reraise[:]:
452
# Filter out the matching messages
453
if (re.match(msg, message, re.I) and
454
issubclass(exc.__class__, cat)):
457
if not seen and not quiet:
458
# This filter caught nothing
459
missing.append((msg, cat.__name__))
461
raise AssertionError("unhandled warning %r" % reraise[0])
463
raise AssertionError("filter (%r, %s) did not catch any warning" %
467
@contextlib.contextmanager
468
def check_warnings(*filters, **kwargs):
469
"""Context manager to silence warnings.
471
Accept 2-tuples as positional arguments:
472
("message regexp", WarningCategory)
475
- if 'quiet' is True, it does not fail if a filter catches nothing
476
(default True without argument,
477
default False if some filters are defined)
479
Without argument, it defaults to:
480
check_warnings(("", Warning), quiet=True)
482
quiet = kwargs.get('quiet')
484
filters = (("", Warning),)
485
# Preserve backward compatibility
488
return _filterwarnings(filters, quiet)
491
@contextlib.contextmanager
492
def _check_py3k_warnings(*filters, **kwargs):
493
"""Context manager to silence py3k warnings.
495
Accept 2-tuples as positional arguments:
496
("message regexp", WarningCategory)
499
- if 'quiet' is True, it does not fail if a filter catches nothing
502
Without argument, it defaults to:
503
_check_py3k_warnings(("", DeprecationWarning), quiet=False)
507
filters = (("", DeprecationWarning),)
509
# It should not raise any py3k warning
511
return _filterwarnings(filters, kwargs.get('quiet'))
427
514
class CleanImport(object):