我们从Python开源项目中,提取了以下8个代码示例,用于说明如何使用warnings.WarningMessage()。
def __enter__(self, *args, **kwargs): rv = super(WarningTestMixin._AssertWarnsContext, self).__enter__(*args, **kwargs) if self._showwarning is not self._module.showwarning: super_showwarning = self._module.showwarning else: super_showwarning = None def showwarning(*args, **kwargs): if super_showwarning is not None: super_showwarning(*args, **kwargs) self._warning_log.append(warnings.WarningMessage(*args, **kwargs)) self._module.showwarning = showwarning return rv
def __enter__(self): clean_warning_registry() # be safe and not propagate state + chaos warnings.simplefilter('always') if self._entered: raise RuntimeError("Cannot enter %r twice" % self) self._entered = True self._filters = self._module.filters self._module.filters = self._filters[:] self._showwarning = self._module.showwarning if self._record: self.log = [] def showwarning(*args, **kwargs): self.log.append(warnings.WarningMessage(*args, **kwargs)) self._module.showwarning = showwarning return self.log else: return None
def warns(expected_warning, *args, **kwargs): """Assert that code raises a particular class of warning. Specifically, the input @expected_warning can be a warning class or tuple of warning classes, and the code must return that warning (if a single class) or one of those warnings (if a tuple). This helper produces a list of ``warnings.WarningMessage`` objects, one for each warning raised. This function can be used as a context manager, or any of the other ways ``pytest.raises`` can be used:: >>> with warns(RuntimeWarning): ... warnings.warn("my warning", RuntimeWarning) """ wcheck = WarningsChecker(expected_warning) if not args: return wcheck elif isinstance(args[0], str): code, = args assert isinstance(code, str) frame = sys._getframe(1) loc = frame.f_locals.copy() loc.update(kwargs) with wcheck: code = _pytest._code.Source(code).compile() py.builtin.exec_(code, frame.f_globals, loc) else: func = args[0] with wcheck: return func(*args[1:], **kwargs)
def warns(expected_warning, *args, **kwargs): """Assert that code raises a particular class of warning. Specifically, the input @expected_warning can be a warning class or tuple of warning classes, and the code must return that warning (if a single class) or one of those warnings (if a tuple). This helper produces a list of ``warnings.WarningMessage`` objects, one for each warning raised. This function can be used as a context manager, or any of the other ways ``pytest.raises`` can be used:: >>> with warns(RuntimeWarning): ... warnings.warn("my warning", RuntimeWarning) """ wcheck = WarningsChecker(expected_warning) if not args: return wcheck elif isinstance(args[0], str): code, = args assert isinstance(code, str) frame = sys._getframe(1) loc = frame.f_locals.copy() loc.update(kwargs) with wcheck: code = py.code.Source(code).compile() py.builtin.exec_(code, frame.f_globals, loc) else: func = args[0] with wcheck: return func(*args[1:], **kwargs)
def test_wait_for_occupied_port_INADDR_ANY(self): """ Wait on INADDR_ANY should not raise IOError In cases where the loopback interface does not exist, CherryPy cannot effectively determine if a port binding to INADDR_ANY was effected. In this situation, CherryPy should assume that it failed to detect the binding (not that the binding failed) and only warn that it could not verify it. """ # At such a time that CherryPy can reliably determine one or more # viable IP addresses of the host, this test may be removed. # Simulate the behavior we observe when no loopback interface is # present by: finding a port that's not occupied, then wait on it. free_port = self.find_free_port() servers = cherrypy.process.servers def with_shorter_timeouts(func): """ A context where occupied_port_timeout is much smaller to speed test runs. """ # When we have Python 2.5, simplify using the with_statement. orig_timeout = servers.occupied_port_timeout servers.occupied_port_timeout = .07 try: func() finally: servers.occupied_port_timeout = orig_timeout def do_waiting(): # Wait on the free port that's unbound with warnings.catch_warnings(record=True) as w: servers.wait_for_occupied_port('0.0.0.0', free_port) self.assertEqual(len(w), 1) self.assertTrue(isinstance(w[0], warnings.WarningMessage)) self.assertTrue( 'Unable to verify that the server is bound on ' in str(w[0])) # The wait should still raise an IO error if INADDR_ANY was # not supplied. self.assertRaises(IOError, servers.wait_for_occupied_port, '127.0.0.1', free_port) with_shorter_timeouts(do_waiting)