Closes issue #11557: removal of non-idiomatic code in test_logging.

This commit is contained in:
Vinay Sajip 2011-06-05 09:31:34 +01:00
parent d64b2bae9b
commit 5056c8c718
1 changed files with 25 additions and 31 deletions

View File

@ -158,8 +158,7 @@ class BaseTest(unittest.TestCase):
except AttributeError:
# StringIO.StringIO lacks a reset() method.
actual_lines = stream.getvalue().splitlines()
self.assertEqual(len(actual_lines), len(expected_values),
'%s vs. %s' % (actual_lines, expected_values))
self.assertEqual(len(actual_lines), len(expected_values))
for actual, expected in zip(actual_lines, expected_values):
match = pat.search(actual)
if not match:
@ -599,8 +598,8 @@ class StreamHandlerTest(BaseTest):
h = logging.StreamHandler(BadStream())
sys.stderr = sio = io.StringIO()
h.handle(r)
self.assertTrue('\nRuntimeError: '
'deliberate mistake\n' in sio.getvalue())
self.assertIn('\nRuntimeError: deliberate mistake\n',
sio.getvalue())
logging.raiseExceptions = False
sys.stderr = sio = io.StringIO()
h.handle(r)
@ -940,7 +939,7 @@ class SMTPHandlerTest(BaseTest):
peer, mailfrom, rcpttos, data = self.messages[0]
self.assertEqual(mailfrom, 'me')
self.assertEqual(rcpttos, ['you'])
self.assertTrue('\nSubject: Log\n' in data)
self.assertIn('\nSubject: Log\n', data)
self.assertTrue(data.endswith('\n\nHello'))
h.close()
@ -1692,7 +1691,7 @@ class WarningsTest(BaseTest):
def test_warnings(self):
with warnings.catch_warnings():
logging.captureWarnings(True)
self.addCleanup(lambda: logging.captureWarnings(False))
self.addCleanup(logging.captureWarnings, False)
warnings.filterwarnings("always", category=UserWarning)
stream = io.StringIO()
h = logging.StreamHandler(stream)
@ -1716,14 +1715,14 @@ class WarningsTest(BaseTest):
def test_warnings_no_handlers(self):
with warnings.catch_warnings():
logging.captureWarnings(True)
self.addCleanup(lambda: logging.captureWarnings(False))
self.addCleanup(logging.captureWarnings, False)
# confirm our assumption: no loggers are set
logger = logging.getLogger("py.warnings")
assert logger.handlers == []
self.assertEqual(logger.handlers, [])
warnings.showwarning("Explicit", UserWarning, "dummy.py", 42)
self.assertTrue(len(logger.handlers) == 1)
self.assertEqual(len(logger.handlers), 1)
self.assertIsInstance(logger.handlers[0], logging.NullHandler)
@ -2971,7 +2970,7 @@ class ShutdownTest(BaseTest):
self.called = []
raise_exceptions = logging.raiseExceptions
self.addCleanup(lambda: setattr(logging, 'raiseExceptions', raise_exceptions))
self.addCleanup(setattr, logging, 'raiseExceptions', raise_exceptions)
def raise_error(self, error):
def inner():
@ -3056,8 +3055,8 @@ class ModuleLevelMiscTest(BaseTest):
def test_disable(self):
old_disable = logging.root.manager.disable
# confirm our assumptions are correct
assert old_disable == 0
self.addCleanup(lambda: logging.disable(old_disable))
self.assertEqual(old_disable, 0)
self.addCleanup(logging.disable, old_disable)
logging.disable(83)
self.assertEqual(logging.root.manager.disable, 83)
@ -3266,7 +3265,7 @@ class BasicConfigTest(unittest.TestCase):
def test_level(self):
old_level = logging.root.level
self.addCleanup(lambda: logging.root.setLevel(old_level))
self.addCleanup(logging.root.setLevel, old_level)
logging.basicConfig(level=57)
self.assertEqual(logging.root.level, 57)
@ -3311,7 +3310,7 @@ class BasicConfigTest(unittest.TestCase):
old_basic_config()
old_level = logging.root.level
logging.root.setLevel(100) # avoid having messages in stderr
self.addCleanup(lambda: logging.root.setLevel(old_level))
self.addCleanup(logging.root.setLevel, old_level)
called.append((a, kw))
patch(self, logging, 'basicConfig', my_basic_config)
@ -3353,7 +3352,7 @@ class LoggerAdapterTest(unittest.TestCase):
self.recording = RecordingHandler()
self.logger = logging.root
self.logger.addHandler(self.recording)
self.addCleanup(lambda: self.logger.removeHandler(self.recording))
self.addCleanup(self.logger.removeHandler, self.recording)
self.addCleanup(self.recording.close)
def cleanup():
@ -3367,8 +3366,8 @@ class LoggerAdapterTest(unittest.TestCase):
msg = 'testing exception: %r'
exc = None
try:
assert False
except AssertionError as e:
1 / 0
except ZeroDivisionError as e:
exc = e
self.adapter.exception(msg, self.recording)
@ -3393,8 +3392,8 @@ class LoggerAdapterTest(unittest.TestCase):
def test_is_enabled_for(self):
old_disable = self.adapter.logger.manager.disable
self.adapter.logger.manager.disable = 33
self.addCleanup(lambda: setattr(self.adapter.logger.manager,
'disable', old_disable))
self.addCleanup(setattr, self.adapter.logger.manager, 'disable',
old_disable)
self.assertFalse(self.adapter.isEnabledFor(32))
def test_has_handlers(self):
@ -3402,8 +3401,8 @@ class LoggerAdapterTest(unittest.TestCase):
for handler in self.logger.handlers:
self.logger.removeHandler(handler)
assert not self.logger.hasHandlers()
self.assertFalse(self.logger.hasHandlers())
self.assertFalse(self.adapter.hasHandlers())
@ -3414,7 +3413,7 @@ class LoggerTest(BaseTest):
self.recording = RecordingHandler()
self.logger = logging.Logger(name='blah')
self.logger.addHandler(self.recording)
self.addCleanup(lambda: self.logger.removeHandler(self.recording))
self.addCleanup(self.logger.removeHandler, self.recording)
self.addCleanup(self.recording.close)
self.addCleanup(logging.shutdown)
@ -3425,8 +3424,8 @@ class LoggerTest(BaseTest):
msg = 'testing exception: %r'
exc = None
try:
assert False
except AssertionError as e:
1 / 0
except ZeroDivisionError as e:
exc = e
self.logger.exception(msg, self.recording)
@ -3440,14 +3439,14 @@ class LoggerTest(BaseTest):
def test_log_invalid_level_with_raise(self):
old_raise = logging.raiseExceptions
self.addCleanup(lambda: setattr(logging, 'raiseExecptions', old_raise))
self.addCleanup(setattr, logging, 'raiseExecptions', old_raise)
logging.raiseExceptions = True
self.assertRaises(TypeError, self.logger.log, '10', 'test message')
def test_log_invalid_level_no_raise(self):
old_raise = logging.raiseExceptions
self.addCleanup(lambda: setattr(logging, 'raiseExecptions', old_raise))
self.addCleanup(setattr, logging, 'raiseExecptions', old_raise)
logging.raiseExceptions = False
self.logger.log('10', 'test message') # no exception happens
@ -3489,22 +3488,17 @@ class LoggerTest(BaseTest):
for handler in self.logger.handlers:
self.logger.removeHandler(handler)
assert not self.logger.hasHandlers()
self.assertFalse(self.logger.hasHandlers())
def test_has_handlers_no_propagate(self):
child_logger = logging.getLogger('blah.child')
child_logger.propagate = False
assert child_logger.handlers == []
self.assertFalse(child_logger.hasHandlers())
def test_is_enabled_for(self):
old_disable = self.logger.manager.disable
self.logger.manager.disable = 23
self.addCleanup(lambda: setattr(self.logger.manager,
'disable', old_disable))
self.addCleanup(setattr, self.logger.manager, 'disable', old_disable)
self.assertFalse(self.logger.isEnabledFor(22))