def test_warningToFile(self): """ L{twisted.python.log.showwarning} passes warnings with an explicit file target on to the underlying Python warning system. """ message = "another unique message" category = FakeWarning filename = "warning-filename.py" lineno = 31 output = StringIO() log.showwarning(message, category, filename, lineno, file=output) self.assertEqual( output.getvalue(), warnings.formatwarning(message, category, filename, lineno)) # In Python 2.6 and higher, warnings.showwarning accepts # a "line" argument which gives the source line the warning # message is to include. line = "hello world" output = StringIO() log.showwarning(message, category, filename, lineno, file=output, line=line) self.assertEqual( output.getvalue(), warnings.formatwarning(message, category, filename, lineno, line))
def test_showwarning(self): """ L{twisted.python.log.showwarning} emits the warning as a message to the Twisted logging system. """ log.showwarning(FakeWarning("unique warning message"), FakeWarning, "warning-filename.py", 27) event = self.catcher.pop() self.assertEqual( event['format'] % event, 'warning-filename.py:27: twisted.test.test_log.FakeWarning: ' 'unique warning message') self.assertEqual(self.catcher, []) # Python 2.6 requires that any function used to override the # warnings.showwarning API accept a "line" parameter or a # deprecation warning is emitted. log.showwarning(FakeWarning("unique warning message"), FakeWarning, "warning-filename.py", 27, line=object()) event = self.catcher.pop() self.assertEqual( event['format'] % event, 'warning-filename.py:27: twisted.test.test_log.FakeWarning: ' 'unique warning message') self.assertEqual(self.catcher, [])
def test_warningToFile(self): """ L{twisted.python.log.showwarning} passes warnings with an explicit file target on to the underlying Python warning system. """ # log.showwarning depends on _oldshowwarning being set, which only # happens in startLogging(), which doesn't happen if you're not # running under trial. So this test only passes by accident of runner # environment. if log._oldshowwarning is None: raise unittest.SkipTest("Currently this test only runs under trial.") message = "another unique message" category = FakeWarning filename = "warning-filename.py" lineno = 31 output = StringIO() log.showwarning(message, category, filename, lineno, file=output) self.assertEqual( output.getvalue(), warnings.formatwarning(message, category, filename, lineno)) # In Python 2.6, warnings.showwarning accepts a "line" argument which # gives the source line the warning message is to include. if sys.version_info >= (2, 6): line = "hello world" output = StringIO() log.showwarning(message, category, filename, lineno, file=output, line=line) self.assertEqual( output.getvalue(), warnings.formatwarning(message, category, filename, lineno, line))
def test_showwarning(self): """ L{twisted.python.log.showwarning} emits the warning as a message to the Twisted logging system. """ log.showwarning( FakeWarning("unique warning message"), FakeWarning, "warning-filename.py", 27) event = self.catcher.pop() self.assertEqual( event['format'] % event, 'warning-filename.py:27: twisted.test.test_log.FakeWarning: ' 'unique warning message') self.assertEqual(self.catcher, []) # Python 2.6 requires that any function used to override the # warnings.showwarning API accept a "line" parameter or a # deprecation warning is emitted. log.showwarning( FakeWarning("unique warning message"), FakeWarning, "warning-filename.py", 27, line=object()) event = self.catcher.pop() self.assertEqual( event['format'] % event, 'warning-filename.py:27: twisted.test.test_log.FakeWarning: ' 'unique warning message') self.assertEqual(self.catcher, [])