Skip to content

Commit b971362

Browse files
gh-96339: Reject socktype and timeout when address is None
They are not applicable to the syslog module and were silently ignored. Also set unixsocket to False in this case. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent b319d9f commit b971362

3 files changed

Lines changed: 18 additions & 2 deletions

File tree

Doc/library/logging.handlers.rst

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -635,8 +635,11 @@ supports sending logging messages to a remote or local Unix syslog.
635635
In this case, a Unix domain socket is used to
636636
send the message to the syslog.
637637
If *address* is ``None``, the :mod:`syslog` module is used to log to the
638-
local system logger; this works even where there is no syslog socket, such
639-
as on recent versions of macOS.
638+
local system logger;
639+
this works even where there is no syslog socket,
640+
such as on recent versions of macOS.
641+
In this case *socktype* and *timeout* are not applicable
642+
and passing them raises :exc:`ValueError`.
640643
If *facility* is not specified,
641644
:const:`LOG_USER` is used. The type of socket opened depends on the
642645
*socktype* argument, which defaults to :const:`socket.SOCK_DGRAM` and thus

Lib/logging/handlers.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -907,7 +907,12 @@ def __init__(self, address=('localhost', SYSLOG_UDP_PORT),
907907
self.socket = None
908908
if address is None:
909909
# Use the syslog module to log to the local system logger.
910+
if socktype is not None:
911+
raise ValueError("socktype is not supported if address is None")
912+
if timeout is not None:
913+
raise ValueError("timeout is not supported if address is None")
910914
import syslog
915+
self.unixsocket = False
911916
self.syslog = syslog
912917
else:
913918
self.createSocket()

Lib/test/test_logging.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2207,6 +2207,7 @@ class LocalSysLogHandlerTest(BaseTest):
22072207
def test_emit(self):
22082208
h = logging.handlers.SysLogHandler(address=None)
22092209
self.addCleanup(h.close)
2210+
self.assertFalse(h.unixsocket)
22102211
h.setFormatter(logging.Formatter('%(message)s'))
22112212
record = self.next_message()
22122213
logrec = logging.makeLogRecord({'msg': record, 'levelname': 'WARNING',
@@ -2216,6 +2217,13 @@ def test_emit(self):
22162217
mock_syslog.assert_called_once_with(
22172218
syslog.LOG_USER | syslog.LOG_WARNING, record)
22182219

2220+
def test_reject_socktype_and_timeout(self):
2221+
with self.assertRaises(ValueError):
2222+
logging.handlers.SysLogHandler(address=None,
2223+
socktype=socket.SOCK_DGRAM)
2224+
with self.assertRaises(ValueError):
2225+
logging.handlers.SysLogHandler(address=None, timeout=1)
2226+
22192227
@support.requires_working_socket()
22202228
@threading_helper.requires_working_threading()
22212229
class HTTPHandlerTest(BaseTest):

0 commit comments

Comments
 (0)