Skip to content

Commit 0b4b57d

Browse files
authored
bpo-30378: Fix the problem that SysLogHandler can't handle IPv6 addresses (#1676)
1 parent 4e624ca commit 0b4b57d

File tree

3 files changed

+43
-7
lines changed

3 files changed

+43
-7
lines changed

Lib/logging/handlers.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -827,11 +827,26 @@ def __init__(self, address=('localhost', SYSLOG_UDP_PORT),
827827
self.unixsocket = False
828828
if socktype is None:
829829
socktype = socket.SOCK_DGRAM
830-
self.socket = socket.socket(socket.AF_INET, socktype)
831-
if socktype == socket.SOCK_STREAM:
832-
self.socket.connect(address)
830+
host, port = address
831+
ress = socket.getaddrinfo(host, port, 0, socktype)
832+
if not ress:
833+
raise OSError("getaddrinfo returns an empty list")
834+
for res in ress:
835+
af, socktype, proto, _, sa = res
836+
err = sock = None
837+
try:
838+
sock = socket.socket(af, socktype, proto)
839+
if socktype == socket.SOCK_STREAM:
840+
sock.connect(sa)
841+
break
842+
except OSError as exc:
843+
err = exc
844+
if sock is not None:
845+
sock.close()
846+
if err is not None:
847+
raise err
848+
self.socket = sock
833849
self.socktype = socktype
834-
self.formatter = None
835850

836851
def _connect_unixsocket(self, address):
837852
use_socktype = self.socktype
@@ -870,7 +885,7 @@ def encodePriority(self, facility, priority):
870885
priority = self.priority_names[priority]
871886
return (facility << 3) | priority
872887

873-
def close (self):
888+
def close(self):
874889
"""
875890
Closes the socket.
876891
"""

Lib/test/test_logging.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1678,7 +1678,7 @@ def setUp(self):
16781678
server.ready.wait()
16791679
hcls = logging.handlers.SysLogHandler
16801680
if isinstance(server.server_address, tuple):
1681-
self.sl_hdlr = hcls(('localhost', server.port))
1681+
self.sl_hdlr = hcls((server.server_address[0], server.port))
16821682
else:
16831683
self.sl_hdlr = hcls(server.server_address)
16841684
self.log_output = ''
@@ -1738,6 +1738,24 @@ def tearDown(self):
17381738
SysLogHandlerTest.tearDown(self)
17391739
support.unlink(self.address)
17401740

1741+
@unittest.skipUnless(support.IPV6_ENABLED,
1742+
'IPv6 support required for this test.')
1743+
@unittest.skipUnless(threading, 'Threading required for this test.')
1744+
class IPv6SysLogHandlerTest(SysLogHandlerTest):
1745+
1746+
"""Test for SysLogHandler with IPv6 host."""
1747+
1748+
server_class = TestUDPServer
1749+
address = ('::1', 0)
1750+
1751+
def setUp(self):
1752+
self.server_class.address_family = socket.AF_INET6
1753+
super(IPv6SysLogHandlerTest, self).setUp()
1754+
1755+
def tearDown(self):
1756+
self.server_class.address_family = socket.AF_INET
1757+
super(IPv6SysLogHandlerTest, self).tearDown()
1758+
17411759
@unittest.skipUnless(threading, 'Threading required for this test.')
17421760
class HTTPHandlerTest(BaseTest):
17431761
"""Test for HTTPHandler."""
@@ -4404,7 +4422,7 @@ def test_main():
44044422
QueueHandlerTest, ShutdownTest, ModuleLevelMiscTest, BasicConfigTest,
44054423
LoggerAdapterTest, LoggerTest, SMTPHandlerTest, FileHandlerTest,
44064424
RotatingFileHandlerTest, LastResortTest, LogRecordTest,
4407-
ExceptionTest, SysLogHandlerTest, HTTPHandlerTest,
4425+
ExceptionTest, SysLogHandlerTest, IPv6SysLogHandlerTest, HTTPHandlerTest,
44084426
NTEventLogHandlerTest, TimedRotatingFileHandlerTest,
44094427
UnixSocketHandlerTest, UnixDatagramHandlerTest, UnixSysLogHandlerTest,
44104428
MiscTestCase

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,9 @@ Extension Modules
345345
Library
346346
-------
347347

348+
- bpo-30378: Fix the problem that logging.handlers.SysLogHandler cannot
349+
handle IPv6 addresses.
350+
348351
- bpo-16500: Allow registering at-fork handlers.
349352

350353
- bpo-30470: Deprecate invalid ctypes call protection on Windows. Patch by

0 commit comments

Comments
 (0)