MCPcopy Index your code
hub / github.com/RustPython/RustPython / _AssertWarnsContext

Class _AssertWarnsContext

Lib/unittest/case.py:297–346  ·  view source on GitHub ↗

A context manager used to implement TestCase.assertWarns* methods.

Source from the content-addressed store, hash-verified

295
296
297class _AssertWarnsContext(_AssertRaisesBaseContext):
298 """A context manager used to implement TestCase.assertWarns* methods."""
299
300 _base_type = Warning
301 _base_type_str = 'a warning type or tuple of warning types'
302
303 def __enter__(self):
304 # The __warningregistry__'s need to be in a pristine state for tests
305 # to work properly.
306 for v in list(sys.modules.values()):
307 if getattr(v, '__warningregistry__', None):
308 v.__warningregistry__ = {}
309 self.warnings_manager = warnings.catch_warnings(record=True)
310 self.warnings = self.warnings_manager.__enter__()
311 warnings.simplefilter("always", self.expected)
312 return self
313
314 def __exit__(self, exc_type, exc_value, tb):
315 self.warnings_manager.__exit__(exc_type, exc_value, tb)
316 if exc_type is not None:
317 # let unexpected exceptions pass through
318 return
319 try:
320 exc_name = self.expected.__name__
321 except AttributeError:
322 exc_name = str(self.expected)
323 first_matching = None
324 for m in self.warnings:
325 w = m.message
326 if not isinstance(w, self.expected):
327 continue
328 if first_matching is None:
329 first_matching = w
330 if (self.expected_regex is not None and
331 not self.expected_regex.search(str(w))):
332 continue
333 # store warning for later retrieval
334 self.warning = w
335 self.filename = m.filename
336 self.lineno = m.lineno
337 return
338 # Now we simply try to choose a helpful failure message
339 if first_matching is not None:
340 self._raiseFailure('"{}" does not match "{}"'.format(
341 self.expected_regex.pattern, str(first_matching)))
342 if self.obj_name:
343 self._raiseFailure("{} not triggered by {}".format(exc_name,
344 self.obj_name))
345 else:
346 self._raiseFailure("{} not triggered".format(exc_name))
347
348
349class _AssertNotWarnsContext(_AssertWarnsContext):

Callers 2

assertWarnsMethod · 0.85
assertWarnsRegexMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected