diff options
author | Vinay Sajip <vinay_sajip@yahoo.co.uk> | 2020-01-01 19:32:11 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-01-01 19:32:11 +0000 |
commit | 46abfc1416ff8e450999611ef8f231ff871ab133 (patch) | |
tree | 9e5ed3f91d0aa64434d1c05067b70648e0147fa2 /Lib/test/test_logging.py | |
parent | 22424c02e51fab3b62cbe255d0b87d1b55b9a6c3 (diff) | |
download | cpython-46abfc1416ff8e450999611ef8f231ff871ab133.tar.gz cpython-46abfc1416ff8e450999611ef8f231ff871ab133.zip |
bpo-39142: Avoid converting namedtuple instances to ConvertingTuple. (GH-17773)
This uses the heuristic of assuming a named tuple is a subclass of
tuple with a _fields attribute. This change means that contents of
a named tuple wouldn't be converted - if a user wants to have
ConvertingTuple functionality from a namedtuple, they will have to
implement it themselves.
Diffstat (limited to 'Lib/test/test_logging.py')
-rw-r--r-- | Lib/test/test_logging.py | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/Lib/test/test_logging.py b/Lib/test/test_logging.py index 4feed03fec2..c38fdae0338 100644 --- a/Lib/test/test_logging.py +++ b/Lib/test/test_logging.py @@ -3379,6 +3379,37 @@ class ConfigDictTest(BaseTest): self.assertRaises(ValueError, bc.convert, 'cfg://!') self.assertRaises(KeyError, bc.convert, 'cfg://adict[2]') + def test_namedtuple(self): + # see bpo-39142 + from collections import namedtuple + + class MyHandler(logging.StreamHandler): + def __init__(self, resource, *args, **kwargs): + super().__init__(*args, **kwargs) + self.resource: namedtuple = resource + + def emit(self, record): + record.msg += f' {self.resource.type}' + return super().emit(record) + + Resource = namedtuple('Resource', ['type', 'labels']) + resource = Resource(type='my_type', labels=['a']) + + config = { + 'version': 1, + 'handlers': { + 'myhandler': { + '()': MyHandler, + 'resource': resource + } + }, + 'root': {'level': 'INFO', 'handlers': ['myhandler']}, + } + with support.captured_stderr() as stderr: + self.apply_config(config) + logging.info('some log') + self.assertEqual(stderr.getvalue(), 'some log my_type\n') + class ManagerTest(BaseTest): def test_manager_loggerclass(self): logged = [] |