Skip to content

Commit b15472a

Browse files
vsajipaisk
authored andcommitted
pythongh-111615: Fix regression in QueueHandler configuration. (pythonGH-111638)
1 parent c2218e2 commit b15472a

File tree

3 files changed

+40
-17
lines changed

3 files changed

+40
-17
lines changed

Lib/logging/config.py

+19-17
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2001-2022 by Vinay Sajip. All Rights Reserved.
1+
# Copyright 2001-2023 by Vinay Sajip. All Rights Reserved.
22
#
33
# Permission to use, copy, modify, and distribute this software and its
44
# documentation for any purpose and without fee is hereby granted,
@@ -734,7 +734,7 @@ def _configure_queue_handler(self, klass, **kwargs):
734734
lklass = kwargs['listener']
735735
else:
736736
lklass = logging.handlers.QueueListener
737-
listener = lklass(q, *kwargs['handlers'], respect_handler_level=rhl)
737+
listener = lklass(q, *kwargs.get('handlers', []), respect_handler_level=rhl)
738738
handler = klass(q)
739739
handler.listener = listener
740740
return handler
@@ -776,11 +776,12 @@ def configure_handler(self, config):
776776
raise ValueError('Unable to set target handler %r' % tn) from e
777777
elif issubclass(klass, logging.handlers.QueueHandler):
778778
# Another special case for handler which refers to other handlers
779-
if 'handlers' not in config:
780-
raise ValueError('No handlers specified for a QueueHandler')
779+
# if 'handlers' not in config:
780+
# raise ValueError('No handlers specified for a QueueHandler')
781781
if 'queue' in config:
782+
from multiprocessing.queues import Queue as MPQueue
782783
qspec = config['queue']
783-
if not isinstance(qspec, queue.Queue):
784+
if not isinstance(qspec, (queue.Queue, MPQueue)):
784785
if isinstance(qspec, str):
785786
q = self.resolve(qspec)
786787
if not callable(q):
@@ -813,18 +814,19 @@ def configure_handler(self, config):
813814
if not callable(listener):
814815
raise TypeError('Invalid listener specifier %r' % lspec)
815816
config['listener'] = listener
816-
hlist = []
817-
try:
818-
for hn in config['handlers']:
819-
h = self.config['handlers'][hn]
820-
if not isinstance(h, logging.Handler):
821-
config.update(config_copy) # restore for deferred cfg
822-
raise TypeError('Required handler %r '
823-
'is not configured yet' % hn)
824-
hlist.append(h)
825-
except Exception as e:
826-
raise ValueError('Unable to set required handler %r' % hn) from e
827-
config['handlers'] = hlist
817+
if 'handlers' in config:
818+
hlist = []
819+
try:
820+
for hn in config['handlers']:
821+
h = self.config['handlers'][hn]
822+
if not isinstance(h, logging.Handler):
823+
config.update(config_copy) # restore for deferred cfg
824+
raise TypeError('Required handler %r '
825+
'is not configured yet' % hn)
826+
hlist.append(h)
827+
except Exception as e:
828+
raise ValueError('Unable to set required handler %r' % hn) from e
829+
config['handlers'] = hlist
828830
elif issubclass(klass, logging.handlers.SMTPHandler) and\
829831
'mailhost' in config:
830832
config['mailhost'] = self.as_tuple(config['mailhost'])

Lib/test/test_logging.py

+19
Original file line numberDiff line numberDiff line change
@@ -3922,6 +3922,25 @@ def test_90195(self):
39223922
# Logger should be enabled, since explicitly mentioned
39233923
self.assertFalse(logger.disabled)
39243924

3925+
def test_111615(self):
3926+
# See gh-111615
3927+
import multiprocessing as mp
3928+
3929+
config = {
3930+
'version': 1,
3931+
'handlers': {
3932+
'sink': {
3933+
'class': 'logging.handlers.QueueHandler',
3934+
'queue': mp.get_context('spawn').Queue(),
3935+
},
3936+
},
3937+
'root': {
3938+
'handlers': ['sink'],
3939+
'level': 'DEBUG',
3940+
},
3941+
}
3942+
logging.config.dictConfig(config)
3943+
39253944
class ManagerTest(BaseTest):
39263945
def test_manager_loggerclass(self):
39273946
logged = []
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix a regression caused by a fix to gh-93162 whereby you couldn't configure
2+
a :class:`QueueHandler` without specifying handlers.

0 commit comments

Comments
 (0)