Skip to content

Commit e20d226

Browse files
authored
bpo-30378: Fix the problem that SysLogHandler can't handle IPv6 addresses (#1676) (#1902)
1 parent 3419fc2 commit e20d226

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
@@ -815,11 +815,26 @@ def __init__(self, address=('localhost', SYSLOG_UDP_PORT),
815815
self.unixsocket = False
816816
if socktype is None:
817817
socktype = socket.SOCK_DGRAM
818-
self.socket = socket.socket(socket.AF_INET, socktype)
819-
if socktype == socket.SOCK_STREAM:
820-
self.socket.connect(address)
818+
host, port = address
819+
ress = socket.getaddrinfo(host, port, 0, socktype)
820+
if not ress:
821+
raise OSError("getaddrinfo returns an empty list")
822+
for res in ress:
823+
af, socktype, proto, _, sa = res
824+
err = sock = None
825+
try:
826+
sock = socket.socket(af, socktype, proto)
827+
if socktype == socket.SOCK_STREAM:
828+
sock.connect(sa)
829+
break
830+
except OSError as exc:
831+
err = exc
832+
if sock is not None:
833+
sock.close()
834+
if err is not None:
835+
raise err
836+
self.socket = sock
821837
self.socktype = socktype
822-
self.formatter = None
823838

824839
def _connect_unixsocket(self, address):
825840
use_socktype = self.socktype
@@ -858,7 +873,7 @@ def encodePriority(self, facility, priority):
858873
priority = self.priority_names[priority]
859874
return (facility << 3) | priority
860875

861-
def close (self):
876+
def close(self):
862877
"""
863878
Closes the socket.
864879
"""

Lib/test/test_logging.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1571,7 +1571,7 @@ def setUp(self):
15711571
server.ready.wait()
15721572
hcls = logging.handlers.SysLogHandler
15731573
if isinstance(server.server_address, tuple):
1574-
self.sl_hdlr = hcls(('localhost', server.port))
1574+
self.sl_hdlr = hcls((server.server_address[0], server.port))
15751575
else:
15761576
self.sl_hdlr = hcls(server.server_address)
15771577
self.log_output = ''
@@ -1627,6 +1627,24 @@ def tearDown(self):
16271627
SysLogHandlerTest.tearDown(self)
16281628
os.remove(self.address)
16291629

1630+
@unittest.skipUnless(support.IPV6_ENABLED,
1631+
'IPv6 support required for this test.')
1632+
@unittest.skipUnless(threading, 'Threading required for this test.')
1633+
class IPv6SysLogHandlerTest(SysLogHandlerTest):
1634+
1635+
"""Test for SysLogHandler with IPv6 host."""
1636+
1637+
server_class = TestUDPServer
1638+
address = ('::1', 0)
1639+
1640+
def setUp(self):
1641+
self.server_class.address_family = socket.AF_INET6
1642+
super(IPv6SysLogHandlerTest, self).setUp()
1643+
1644+
def tearDown(self):
1645+
self.server_class.address_family = socket.AF_INET
1646+
super(IPv6SysLogHandlerTest, self).tearDown()
1647+
16301648
@unittest.skipUnless(threading, 'Threading required for this test.')
16311649
class HTTPHandlerTest(BaseTest):
16321650
"""Test for HTTPHandler."""
@@ -4271,7 +4289,7 @@ def test_main():
42714289
QueueHandlerTest, ShutdownTest, ModuleLevelMiscTest, BasicConfigTest,
42724290
LoggerAdapterTest, LoggerTest, SMTPHandlerTest, FileHandlerTest,
42734291
RotatingFileHandlerTest, LastResortTest, LogRecordTest,
4274-
ExceptionTest, SysLogHandlerTest, HTTPHandlerTest,
4292+
ExceptionTest, SysLogHandlerTest, IPv6SysLogHandlerTest, HTTPHandlerTest,
42754293
NTEventLogHandlerTest, TimedRotatingFileHandlerTest,
42764294
UnixSocketHandlerTest, UnixDatagramHandlerTest, UnixSysLogHandlerTest,
42774295
]

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ Extension Modules
5656
Library
5757
-------
5858

59+
- bpo-30378: Fix the problem that logging.handlers.SysLogHandler cannot
60+
handle IPv6 addresses.
61+
5962
- bpo-29960: Preserve generator state when _random.Random.setstate()
6063
raises an exception. Patch by Bryan Olson.
6164

0 commit comments

Comments
 (0)